1 //===-- Thumb2ITBlockPass.cpp - Insert Thumb-2 IT blocks ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #define DEBUG_TYPE "thumb2-it"
12 #include "ARMMachineFunctionInfo.h"
13 #include "Thumb2InstrInfo.h"
14 #include "llvm/ADT/SmallSet.h"
15 #include "llvm/ADT/Statistic.h"
16 #include "llvm/CodeGen/MachineFunctionPass.h"
17 #include "llvm/CodeGen/MachineInstr.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/MachineInstrBundle.h"
22 STATISTIC(NumITs, "Number of IT blocks inserted");
23 STATISTIC(NumMovedInsts, "Number of predicated instructions moved");
26 class Thumb2ITBlockPass : public MachineFunctionPass {
29 Thumb2ITBlockPass() : MachineFunctionPass(ID) {}
31 const Thumb2InstrInfo *TII;
32 const TargetRegisterInfo *TRI;
35 virtual bool runOnMachineFunction(MachineFunction &Fn);
37 virtual const char *getPassName() const {
38 return "Thumb IT blocks insertion pass";
42 bool MoveCopyOutOfITBlock(MachineInstr *MI,
43 ARMCC::CondCodes CC, ARMCC::CondCodes OCC,
44 SmallSet<unsigned, 4> &Defs,
45 SmallSet<unsigned, 4> &Uses);
46 bool InsertITInstructions(MachineBasicBlock &MBB);
48 char Thumb2ITBlockPass::ID = 0;
51 /// TrackDefUses - Tracking what registers are being defined and used by
52 /// instructions in the IT block. This also tracks "dependencies", i.e. uses
53 /// in the IT block that are defined before the IT instruction.
54 static void TrackDefUses(MachineInstr *MI,
55 SmallSet<unsigned, 4> &Defs,
56 SmallSet<unsigned, 4> &Uses,
57 const TargetRegisterInfo *TRI) {
58 SmallVector<unsigned, 4> LocalDefs;
59 SmallVector<unsigned, 4> LocalUses;
61 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
62 MachineOperand &MO = MI->getOperand(i);
65 unsigned Reg = MO.getReg();
66 if (!Reg || Reg == ARM::ITSTATE || Reg == ARM::SP)
69 LocalUses.push_back(Reg);
71 LocalDefs.push_back(Reg);
74 for (unsigned i = 0, e = LocalUses.size(); i != e; ++i) {
75 unsigned Reg = LocalUses[i];
77 for (MCSubRegIterator Subreg(Reg, TRI); Subreg.isValid(); ++Subreg)
81 for (unsigned i = 0, e = LocalDefs.size(); i != e; ++i) {
82 unsigned Reg = LocalDefs[i];
84 for (MCSubRegIterator Subreg(Reg, TRI); Subreg.isValid(); ++Subreg)
91 static bool isCopy(MachineInstr *MI) {
92 switch (MI->getOpcode()) {
104 Thumb2ITBlockPass::MoveCopyOutOfITBlock(MachineInstr *MI,
105 ARMCC::CondCodes CC, ARMCC::CondCodes OCC,
106 SmallSet<unsigned, 4> &Defs,
107 SmallSet<unsigned, 4> &Uses) {
110 // llvm models select's as two-address instructions. That means a copy
111 // is inserted before a t2MOVccr, etc. If the copy is scheduled in
112 // between selects we would end up creating multiple IT blocks.
113 assert(MI->getOperand(0).getSubReg() == 0 &&
114 MI->getOperand(1).getSubReg() == 0 &&
115 "Sub-register indices still around?");
117 unsigned DstReg = MI->getOperand(0).getReg();
118 unsigned SrcReg = MI->getOperand(1).getReg();
120 // First check if it's safe to move it.
121 if (Uses.count(DstReg) || Defs.count(SrcReg))
124 // If the CPSR is defined by this copy, then we don't want to move it. E.g.,
132 // we don't want this to be converted to:
140 const MCInstrDesc &MCID = MI->getDesc();
141 if (MI->hasOptionalDef() &&
142 MI->getOperand(MCID.getNumOperands() - 1).getReg() == ARM::CPSR)
145 // Then peek at the next instruction to see if it's predicated on CC or OCC.
146 // If not, then there is nothing to be gained by moving the copy.
147 MachineBasicBlock::iterator I = MI; ++I;
148 MachineBasicBlock::iterator E = MI->getParent()->end();
149 while (I != E && I->isDebugValue())
152 unsigned NPredReg = 0;
153 ARMCC::CondCodes NCC = getITInstrPredicate(I, NPredReg);
154 if (NCC == CC || NCC == OCC)
160 bool Thumb2ITBlockPass::InsertITInstructions(MachineBasicBlock &MBB) {
161 bool Modified = false;
163 SmallSet<unsigned, 4> Defs;
164 SmallSet<unsigned, 4> Uses;
165 MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
167 MachineInstr *MI = &*MBBI;
168 DebugLoc dl = MI->getDebugLoc();
169 unsigned PredReg = 0;
170 ARMCC::CondCodes CC = getITInstrPredicate(MI, PredReg);
171 if (CC == ARMCC::AL) {
178 TrackDefUses(MI, Defs, Uses, TRI);
180 // Insert an IT instruction.
181 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII->get(ARM::t2IT))
184 // Add implicit use of ITSTATE to IT block instructions.
185 MI->addOperand(MachineOperand::CreateReg(ARM::ITSTATE, false/*ifDef*/,
186 true/*isImp*/, false/*isKill*/));
188 MachineInstr *LastITMI = MI;
189 MachineBasicBlock::iterator InsertPos = MIB;
193 ARMCC::CondCodes OCC = ARMCC::getOppositeCondition(CC);
194 unsigned Mask = 0, Pos = 3;
195 // Branches, including tricky ones like LDM_RET, need to end an IT
196 // block so check the instruction we just put in the block.
197 for (; MBBI != E && Pos &&
198 (!MI->isBranch() && !MI->isReturn()) ; ++MBBI) {
199 if (MBBI->isDebugValue())
202 MachineInstr *NMI = &*MBBI;
205 unsigned NPredReg = 0;
206 ARMCC::CondCodes NCC = getITInstrPredicate(NMI, NPredReg);
207 if (NCC == CC || NCC == OCC) {
208 Mask |= (NCC & 1) << Pos;
209 // Add implicit use of ITSTATE.
210 NMI->addOperand(MachineOperand::CreateReg(ARM::ITSTATE, false/*ifDef*/,
211 true/*isImp*/, false/*isKill*/));
214 if (NCC == ARMCC::AL &&
215 MoveCopyOutOfITBlock(NMI, CC, OCC, Defs, Uses)) {
218 MBB.insert(InsertPos, NMI);
224 TrackDefUses(NMI, Defs, Uses, TRI);
230 // Tag along (firstcond[0] << 4) with the mask.
231 Mask |= (CC & 1) << 4;
234 // Last instruction in IT block kills ITSTATE.
235 LastITMI->findRegisterUseOperand(ARM::ITSTATE)->setIsKill();
237 // Finalize the bundle.
238 MachineBasicBlock::instr_iterator LI = LastITMI;
239 finalizeBundle(MBB, InsertPos.getInstrIterator(), llvm::next(LI));
248 bool Thumb2ITBlockPass::runOnMachineFunction(MachineFunction &Fn) {
249 const TargetMachine &TM = Fn.getTarget();
250 AFI = Fn.getInfo<ARMFunctionInfo>();
251 TII = static_cast<const Thumb2InstrInfo*>(TM.getInstrInfo());
252 TRI = TM.getRegisterInfo();
254 if (!AFI->isThumbFunction())
257 bool Modified = false;
258 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E; ) {
259 MachineBasicBlock &MBB = *MFI;
261 Modified |= InsertITInstructions(MBB);
265 AFI->setHasITBlocks(true);
270 /// createThumb2ITBlockPass - Returns an instance of the Thumb2 IT blocks
272 FunctionPass *llvm::createThumb2ITBlockPass() {
273 return new Thumb2ITBlockPass();