Invalidate liveness in Thumb2ITBlockPass.
[oota-llvm.git] / lib / Target / ARM / Thumb2ITBlockPass.cpp
1 //===-- Thumb2ITBlockPass.cpp - Insert Thumb-2 IT blocks ------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #define DEBUG_TYPE "thumb2-it"
11 #include "ARM.h"
12 #include "ARMMachineFunctionInfo.h"
13 #include "Thumb2InstrInfo.h"
14 #include "llvm/CodeGen/MachineInstr.h"
15 #include "llvm/CodeGen/MachineInstrBuilder.h"
16 #include "llvm/CodeGen/MachineInstrBundle.h"
17 #include "llvm/CodeGen/MachineFunctionPass.h"
18 #include "llvm/CodeGen/MachineRegisterInfo.h"
19 #include "llvm/ADT/SmallSet.h"
20 #include "llvm/ADT/Statistic.h"
21 using namespace llvm;
22
23 STATISTIC(NumITs,        "Number of IT blocks inserted");
24 STATISTIC(NumMovedInsts, "Number of predicated instructions moved");
25
26 namespace {
27   class Thumb2ITBlockPass : public MachineFunctionPass {
28     bool PreRegAlloc;
29
30   public:
31     static char ID;
32     Thumb2ITBlockPass() : MachineFunctionPass(ID) {}
33
34     const Thumb2InstrInfo *TII;
35     const TargetRegisterInfo *TRI;
36     ARMFunctionInfo *AFI;
37
38     virtual bool runOnMachineFunction(MachineFunction &Fn);
39
40     virtual const char *getPassName() const {
41       return "Thumb IT blocks insertion pass";
42     }
43
44   private:
45     bool MoveCopyOutOfITBlock(MachineInstr *MI,
46                               ARMCC::CondCodes CC, ARMCC::CondCodes OCC,
47                               SmallSet<unsigned, 4> &Defs,
48                               SmallSet<unsigned, 4> &Uses);
49     bool InsertITInstructions(MachineBasicBlock &MBB);
50   };
51   char Thumb2ITBlockPass::ID = 0;
52 }
53
54 /// TrackDefUses - Tracking what registers are being defined and used by
55 /// instructions in the IT block. This also tracks "dependencies", i.e. uses
56 /// in the IT block that are defined before the IT instruction.
57 static void TrackDefUses(MachineInstr *MI,
58                          SmallSet<unsigned, 4> &Defs,
59                          SmallSet<unsigned, 4> &Uses,
60                          const TargetRegisterInfo *TRI) {
61   SmallVector<unsigned, 4> LocalDefs;
62   SmallVector<unsigned, 4> LocalUses;
63
64   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
65     MachineOperand &MO = MI->getOperand(i);
66     if (!MO.isReg())
67       continue;
68     unsigned Reg = MO.getReg();
69     if (!Reg || Reg == ARM::ITSTATE || Reg == ARM::SP)
70       continue;
71     if (MO.isUse())
72       LocalUses.push_back(Reg);
73     else
74       LocalDefs.push_back(Reg);
75   }
76
77   for (unsigned i = 0, e = LocalUses.size(); i != e; ++i) {
78     unsigned Reg = LocalUses[i];
79     Uses.insert(Reg);
80     for (const uint16_t *Subreg = TRI->getSubRegisters(Reg);
81          *Subreg; ++Subreg)
82       Uses.insert(*Subreg);
83   }
84
85   for (unsigned i = 0, e = LocalDefs.size(); i != e; ++i) {
86     unsigned Reg = LocalDefs[i];
87     Defs.insert(Reg);
88     for (const uint16_t *Subreg = TRI->getSubRegisters(Reg);
89          *Subreg; ++Subreg)
90       Defs.insert(*Subreg);
91     if (Reg == ARM::CPSR)
92       continue;
93   }
94 }
95
96 static bool isCopy(MachineInstr *MI) {
97   switch (MI->getOpcode()) {
98   default:
99     return false;
100   case ARM::MOVr:
101   case ARM::MOVr_TC:
102   case ARM::tMOVr:
103   case ARM::t2MOVr:
104     return true;
105   }
106 }
107
108 bool
109 Thumb2ITBlockPass::MoveCopyOutOfITBlock(MachineInstr *MI,
110                                       ARMCC::CondCodes CC, ARMCC::CondCodes OCC,
111                                         SmallSet<unsigned, 4> &Defs,
112                                         SmallSet<unsigned, 4> &Uses) {
113   if (!isCopy(MI))
114     return false;
115   // llvm models select's as two-address instructions. That means a copy
116   // is inserted before a t2MOVccr, etc. If the copy is scheduled in
117   // between selects we would end up creating multiple IT blocks.
118   assert(MI->getOperand(0).getSubReg() == 0 &&
119          MI->getOperand(1).getSubReg() == 0 &&
120          "Sub-register indices still around?");
121
122   unsigned DstReg = MI->getOperand(0).getReg();
123   unsigned SrcReg = MI->getOperand(1).getReg();
124
125   // First check if it's safe to move it.
126   if (Uses.count(DstReg) || Defs.count(SrcReg))
127     return false;
128
129   // If the CPSR is defined by this copy, then we don't want to move it. E.g.,
130   // if we have:
131   //
132   //   movs  r1, r1
133   //   rsb   r1, 0
134   //   movs  r2, r2
135   //   rsb   r2, 0
136   //
137   // we don't want this to be converted to:
138   //
139   //   movs  r1, r1
140   //   movs  r2, r2
141   //   itt   mi
142   //   rsb   r1, 0
143   //   rsb   r2, 0
144   //
145   const MCInstrDesc &MCID = MI->getDesc();
146   if (MI->hasOptionalDef() &&
147       MI->getOperand(MCID.getNumOperands() - 1).getReg() == ARM::CPSR)
148     return false;
149
150   // Then peek at the next instruction to see if it's predicated on CC or OCC.
151   // If not, then there is nothing to be gained by moving the copy.
152   MachineBasicBlock::iterator I = MI; ++I;
153   MachineBasicBlock::iterator E = MI->getParent()->end();
154   while (I != E && I->isDebugValue())
155     ++I;
156   if (I != E) {
157     unsigned NPredReg = 0;
158     ARMCC::CondCodes NCC = getITInstrPredicate(I, NPredReg);
159     if (NCC == CC || NCC == OCC)
160       return true;
161   }
162   return false;
163 }
164
165 bool Thumb2ITBlockPass::InsertITInstructions(MachineBasicBlock &MBB) {
166   bool Modified = false;
167
168   SmallSet<unsigned, 4> Defs;
169   SmallSet<unsigned, 4> Uses;
170   MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
171   while (MBBI != E) {
172     MachineInstr *MI = &*MBBI;
173     DebugLoc dl = MI->getDebugLoc();
174     unsigned PredReg = 0;
175     ARMCC::CondCodes CC = getITInstrPredicate(MI, PredReg);
176     if (CC == ARMCC::AL) {
177       ++MBBI;
178       continue;
179     }
180
181     Defs.clear();
182     Uses.clear();
183     TrackDefUses(MI, Defs, Uses, TRI);
184
185     // Insert an IT instruction.
186     MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII->get(ARM::t2IT))
187       .addImm(CC);
188
189     // Add implicit use of ITSTATE to IT block instructions.
190     MI->addOperand(MachineOperand::CreateReg(ARM::ITSTATE, false/*ifDef*/,
191                                              true/*isImp*/, false/*isKill*/));
192
193     MachineInstr *LastITMI = MI;
194     MachineBasicBlock::iterator InsertPos = MIB;
195     ++MBBI;
196
197     // Form IT block.
198     ARMCC::CondCodes OCC = ARMCC::getOppositeCondition(CC);
199     unsigned Mask = 0, Pos = 3;
200     // Branches, including tricky ones like LDM_RET, need to end an IT
201     // block so check the instruction we just put in the block.
202     for (; MBBI != E && Pos &&
203            (!MI->isBranch() && !MI->isReturn()) ; ++MBBI) {
204       if (MBBI->isDebugValue())
205         continue;
206
207       MachineInstr *NMI = &*MBBI;
208       MI = NMI;
209
210       unsigned NPredReg = 0;
211       ARMCC::CondCodes NCC = getITInstrPredicate(NMI, NPredReg);
212       if (NCC == CC || NCC == OCC) {
213         Mask |= (NCC & 1) << Pos;
214         // Add implicit use of ITSTATE.
215         NMI->addOperand(MachineOperand::CreateReg(ARM::ITSTATE, false/*ifDef*/,
216                                                true/*isImp*/, false/*isKill*/));
217         LastITMI = NMI;
218       } else {
219         if (NCC == ARMCC::AL &&
220             MoveCopyOutOfITBlock(NMI, CC, OCC, Defs, Uses)) {
221           --MBBI;
222           MBB.remove(NMI);
223           MBB.insert(InsertPos, NMI);
224           ++NumMovedInsts;
225           continue;
226         }
227         break;
228       }
229       TrackDefUses(NMI, Defs, Uses, TRI);
230       --Pos;
231     }
232
233     // Finalize IT mask.
234     Mask |= (1 << Pos);
235     // Tag along (firstcond[0] << 4) with the mask.
236     Mask |= (CC & 1) << 4;
237     MIB.addImm(Mask);
238
239     // Last instruction in IT block kills ITSTATE.
240     LastITMI->findRegisterUseOperand(ARM::ITSTATE)->setIsKill();
241
242     // Finalize the bundle.
243     MachineBasicBlock::instr_iterator LI = LastITMI;
244     finalizeBundle(MBB, InsertPos.getInstrIterator(), llvm::next(LI));
245
246     Modified = true;
247     ++NumITs;
248   }
249
250   return Modified;
251 }
252
253 bool Thumb2ITBlockPass::runOnMachineFunction(MachineFunction &Fn) {
254   const TargetMachine &TM = Fn.getTarget();
255   AFI = Fn.getInfo<ARMFunctionInfo>();
256   TII = static_cast<const Thumb2InstrInfo*>(TM.getInstrInfo());
257   TRI = TM.getRegisterInfo();
258
259   if (!AFI->isThumbFunction())
260     return false;
261
262   // IT block insertion invalidates accurate register liveness.
263   Fn.getRegInfo().invalidateLiveness();
264
265   bool Modified = false;
266   for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E; ) {
267     MachineBasicBlock &MBB = *MFI;
268     ++MFI;
269     Modified |= InsertITInstructions(MBB);
270   }
271
272   if (Modified)
273     AFI->setHasITBlocks(true);
274
275   return Modified;
276 }
277
278 /// createThumb2ITBlockPass - Returns an instance of the Thumb2 IT blocks
279 /// insertion pass.
280 FunctionPass *llvm::createThumb2ITBlockPass() {
281   return new Thumb2ITBlockPass();
282 }