Jim Grosbach | 31c24bf | 2009-11-07 22:00:39 +0000 | [diff] [blame] | 1 | //===-- Thumb2ITBlockPass.cpp - Insert Thumb IT blocks ----------*- C++ -*-===// |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 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" |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 12 | #include "ARMMachineFunctionInfo.h" |
Evan Cheng | ed338e8 | 2009-07-11 07:26:20 +0000 | [diff] [blame] | 13 | #include "Thumb2InstrInfo.h" |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineInstr.h" |
| 15 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 16 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallSet.h" |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Statistic.h" |
| 19 | using namespace llvm; |
| 20 | |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 21 | STATISTIC(NumITs, "Number of IT blocks inserted"); |
| 22 | STATISTIC(NumMovedInsts, "Number of predicated instructions moved"); |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 23 | |
| 24 | namespace { |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 25 | class Thumb2ITBlockPass : public MachineFunctionPass { |
| 26 | bool PreRegAlloc; |
| 27 | |
| 28 | public: |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 29 | static char ID; |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 30 | Thumb2ITBlockPass(bool PreRA) : |
| 31 | MachineFunctionPass(&ID), PreRegAlloc(PreRA) {} |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 32 | |
Evan Cheng | ed338e8 | 2009-07-11 07:26:20 +0000 | [diff] [blame] | 33 | const Thumb2InstrInfo *TII; |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame^] | 34 | const TargetRegisterInfo *TRI; |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 35 | ARMFunctionInfo *AFI; |
| 36 | |
| 37 | virtual bool runOnMachineFunction(MachineFunction &Fn); |
| 38 | |
| 39 | virtual const char *getPassName() const { |
| 40 | return "Thumb IT blocks insertion pass"; |
| 41 | } |
| 42 | |
| 43 | private: |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 44 | bool MoveCPSRUseUp(MachineBasicBlock &MBB, |
| 45 | MachineBasicBlock::iterator MBBI, |
| 46 | MachineBasicBlock::iterator E, |
| 47 | unsigned PredReg, |
| 48 | ARMCC::CondCodes CC, ARMCC::CondCodes OCC, |
| 49 | bool &Done); |
| 50 | |
| 51 | void FindITBlockRanges(MachineBasicBlock &MBB, |
| 52 | SmallVector<MachineInstr*,4> &FirstUses, |
| 53 | SmallVector<MachineInstr*,4> &LastUses); |
| 54 | bool InsertITBlock(MachineInstr *First, MachineInstr *Last); |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 55 | bool InsertITBlocks(MachineBasicBlock &MBB); |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame^] | 56 | bool MoveCopyOutOfITBlock(MachineInstr *MI, |
| 57 | ARMCC::CondCodes CC, ARMCC::CondCodes OCC, |
| 58 | SmallSet<unsigned, 4> &Defs, |
| 59 | SmallSet<unsigned, 4> &Uses); |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 60 | bool InsertITInstructions(MachineBasicBlock &MBB); |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 61 | }; |
| 62 | char Thumb2ITBlockPass::ID = 0; |
| 63 | } |
| 64 | |
Evan Cheng | 5adb66a | 2009-09-28 09:14:39 +0000 | [diff] [blame] | 65 | static ARMCC::CondCodes getPredicate(const MachineInstr *MI, unsigned &PredReg){ |
Evan Cheng | ed338e8 | 2009-07-11 07:26:20 +0000 | [diff] [blame] | 66 | unsigned Opc = MI->getOpcode(); |
| 67 | if (Opc == ARM::tBcc || Opc == ARM::t2Bcc) |
| 68 | return ARMCC::AL; |
Evan Cheng | 68fc2da | 2010-06-09 19:26:01 +0000 | [diff] [blame] | 69 | return llvm::getInstrPredicate(MI, PredReg); |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | bool |
| 73 | Thumb2ITBlockPass::MoveCPSRUseUp(MachineBasicBlock &MBB, |
| 74 | MachineBasicBlock::iterator MBBI, |
| 75 | MachineBasicBlock::iterator E, |
| 76 | unsigned PredReg, |
| 77 | ARMCC::CondCodes CC, ARMCC::CondCodes OCC, |
| 78 | bool &Done) { |
| 79 | SmallSet<unsigned, 4> Defs, Uses; |
| 80 | MachineBasicBlock::iterator I = MBBI; |
| 81 | // Look for next CPSR use by scanning up to 4 instructions. |
| 82 | for (unsigned i = 0; i < 4; ++i) { |
| 83 | MachineInstr *MI = &*I; |
| 84 | unsigned MPredReg = 0; |
| 85 | ARMCC::CondCodes MCC = getPredicate(MI, MPredReg); |
| 86 | if (MCC != ARMCC::AL) { |
| 87 | if (MPredReg != PredReg || (MCC != CC && MCC != OCC)) |
| 88 | return false; |
| 89 | |
| 90 | // Check if the instruction is using any register that's defined |
| 91 | // below the previous predicated instruction. Also return false if |
| 92 | // it defines any register which is used in between. |
| 93 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 94 | const MachineOperand &MO = MI->getOperand(i); |
| 95 | if (!MO.isReg()) |
| 96 | continue; |
| 97 | unsigned Reg = MO.getReg(); |
| 98 | if (!Reg) |
| 99 | continue; |
| 100 | if (MO.isDef()) { |
| 101 | if (Reg == PredReg || Uses.count(Reg)) |
| 102 | return false; |
| 103 | } else { |
| 104 | if (Defs.count(Reg)) |
| 105 | return false; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | Done = (I == E); |
| 110 | MBB.remove(MI); |
| 111 | MBB.insert(MBBI, MI); |
| 112 | ++NumMovedInsts; |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 117 | const MachineOperand &MO = MI->getOperand(i); |
| 118 | if (!MO.isReg()) |
| 119 | continue; |
| 120 | unsigned Reg = MO.getReg(); |
| 121 | if (!Reg) |
| 122 | continue; |
| 123 | if (MO.isDef()) { |
| 124 | if (Reg == PredReg) |
| 125 | return false; |
| 126 | Defs.insert(Reg); |
| 127 | } else |
| 128 | Uses.insert(Reg); |
| 129 | } |
| 130 | |
| 131 | if (I == E) |
| 132 | break; |
| 133 | ++I; |
| 134 | } |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | static bool isCPSRLiveout(MachineBasicBlock &MBB) { |
| 139 | for (MachineBasicBlock::succ_iterator I = MBB.succ_begin(), |
| 140 | E = MBB.succ_end(); I != E; ++I) { |
| 141 | if ((*I)->isLiveIn(ARM::CPSR)) |
| 142 | return true; |
| 143 | } |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | void Thumb2ITBlockPass::FindITBlockRanges(MachineBasicBlock &MBB, |
| 148 | SmallVector<MachineInstr*,4> &FirstUses, |
| 149 | SmallVector<MachineInstr*,4> &LastUses) { |
| 150 | bool SeenUse = false; |
| 151 | MachineOperand *LastDef = 0; |
| 152 | MachineOperand *LastUse = 0; |
| 153 | MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end(); |
| 154 | while (MBBI != E) { |
| 155 | MachineInstr *MI = &*MBBI; |
| 156 | ++MBBI; |
| 157 | |
| 158 | MachineOperand *Def = 0; |
| 159 | MachineOperand *Use = 0; |
| 160 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 161 | MachineOperand &MO = MI->getOperand(i); |
| 162 | if (!MO.isReg() || MO.getReg() != ARM::CPSR) |
| 163 | continue; |
| 164 | if (MO.isDef()) { |
| 165 | assert(Def == 0 && "Multiple defs of CPSR?"); |
| 166 | Def = &MO; |
| 167 | } else { |
| 168 | assert(Use == 0 && "Multiple uses of CPSR?"); |
| 169 | Use = &MO; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if (Use) { |
| 174 | LastUse = Use; |
| 175 | if (!SeenUse) { |
| 176 | FirstUses.push_back(MI); |
| 177 | SeenUse = true; |
| 178 | } |
| 179 | } |
| 180 | if (Def) { |
| 181 | if (LastUse) { |
| 182 | LastUses.push_back(LastUse->getParent()); |
| 183 | LastUse = 0; |
| 184 | } |
| 185 | LastDef = Def; |
| 186 | SeenUse = false; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | if (LastUse) { |
| 191 | // Is the last use a kill? |
| 192 | if (isCPSRLiveout(MBB)) |
| 193 | LastUses.push_back(0); |
| 194 | else |
| 195 | LastUses.push_back(LastUse->getParent()); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | bool Thumb2ITBlockPass::InsertITBlock(MachineInstr *First, MachineInstr *Last) { |
| 200 | if (First == Last) |
| 201 | return false; |
| 202 | |
| 203 | bool Modified = false; |
| 204 | MachineBasicBlock *MBB = First->getParent(); |
| 205 | MachineBasicBlock::iterator MBBI = First; |
| 206 | MachineBasicBlock::iterator E = Last; |
| 207 | |
| 208 | if (First->getDesc().isBranch() || First->getDesc().isReturn()) |
| 209 | return false; |
| 210 | |
| 211 | unsigned PredReg = 0; |
| 212 | ARMCC::CondCodes CC = getPredicate(First, PredReg); |
| 213 | if (CC == ARMCC::AL) |
| 214 | return Modified; |
| 215 | |
| 216 | // Move uses of the CPSR together if possible. |
| 217 | ARMCC::CondCodes OCC = ARMCC::getOppositeCondition(CC); |
| 218 | |
| 219 | do { |
| 220 | ++MBBI; |
| 221 | if (MBBI->getDesc().isBranch() || MBBI->getDesc().isReturn()) |
| 222 | return Modified; |
| 223 | MachineInstr *NMI = &*MBBI; |
| 224 | unsigned NPredReg = 0; |
| 225 | ARMCC::CondCodes NCC = getPredicate(NMI, NPredReg); |
| 226 | if (NCC != CC && NCC != OCC) { |
| 227 | if (NCC != ARMCC::AL) |
| 228 | return Modified; |
| 229 | assert(MBBI != E); |
| 230 | bool Done = false; |
| 231 | if (!MoveCPSRUseUp(*MBB, MBBI, E, PredReg, CC, OCC, Done)) |
| 232 | return Modified; |
| 233 | Modified = true; |
| 234 | if (Done) |
| 235 | MBBI = E; |
| 236 | } |
| 237 | } while (MBBI != E); |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 238 | return true; |
Evan Cheng | ed338e8 | 2009-07-11 07:26:20 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 241 | bool Thumb2ITBlockPass::InsertITBlocks(MachineBasicBlock &MBB) { |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 242 | SmallVector<MachineInstr*, 4> FirstUses; |
| 243 | SmallVector<MachineInstr*, 4> LastUses; |
| 244 | FindITBlockRanges(MBB, FirstUses, LastUses); |
| 245 | assert(FirstUses.size() == LastUses.size() && "Incorrect range information!"); |
| 246 | |
| 247 | bool Modified = false; |
| 248 | for (unsigned i = 0, e = FirstUses.size(); i != e; ++i) { |
| 249 | if (LastUses[i] == 0) |
| 250 | // Must be the last pair where CPSR is live out of the block. |
| 251 | return Modified; |
| 252 | Modified |= InsertITBlock(FirstUses[i], LastUses[i]); |
| 253 | } |
| 254 | return Modified; |
| 255 | } |
| 256 | |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame^] | 257 | /// TrackDefUses - Tracking what registers are being defined and used by |
| 258 | /// instructions in the IT block. This also tracks "dependencies", i.e. uses |
| 259 | /// in the IT block that are defined before the IT instruction. |
| 260 | static void TrackDefUses(MachineInstr *MI, |
| 261 | SmallSet<unsigned, 4> &Defs, |
| 262 | SmallSet<unsigned, 4> &Uses, |
| 263 | const TargetRegisterInfo *TRI) { |
| 264 | SmallVector<unsigned, 4> LocalDefs; |
| 265 | SmallVector<unsigned, 4> LocalUses; |
| 266 | |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 267 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 268 | MachineOperand &MO = MI->getOperand(i); |
| 269 | if (!MO.isReg()) |
| 270 | continue; |
| 271 | unsigned Reg = MO.getReg(); |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame^] | 272 | if (!Reg || Reg == ARM::ITSTATE || Reg == ARM::SP) |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 273 | continue; |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame^] | 274 | if (MO.isUse()) |
| 275 | LocalUses.push_back(Reg); |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 276 | else |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame^] | 277 | LocalDefs.push_back(Reg); |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 278 | } |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame^] | 279 | |
| 280 | for (unsigned i = 0, e = LocalUses.size(); i != e; ++i) { |
| 281 | unsigned Reg = LocalUses[i]; |
| 282 | Uses.insert(Reg); |
| 283 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 284 | *Subreg; ++Subreg) |
| 285 | Uses.insert(*Subreg); |
| 286 | } |
| 287 | |
| 288 | for (unsigned i = 0, e = LocalDefs.size(); i != e; ++i) { |
| 289 | unsigned Reg = LocalDefs[i]; |
| 290 | Defs.insert(Reg); |
| 291 | for (const unsigned *Subreg = TRI->getSubRegisters(Reg); |
| 292 | *Subreg; ++Subreg) |
| 293 | Defs.insert(*Subreg); |
| 294 | if (Reg == ARM::CPSR) |
| 295 | continue; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | bool |
| 300 | Thumb2ITBlockPass::MoveCopyOutOfITBlock(MachineInstr *MI, |
| 301 | ARMCC::CondCodes CC, ARMCC::CondCodes OCC, |
| 302 | SmallSet<unsigned, 4> &Defs, |
| 303 | SmallSet<unsigned, 4> &Uses) { |
| 304 | unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx; |
| 305 | if (TII->isMoveInstr(*MI, SrcReg, DstReg, SrcSubIdx, DstSubIdx)) { |
| 306 | assert(SrcSubIdx == 0 && DstSubIdx == 0 && |
| 307 | "Sub-register indices still around?"); |
| 308 | // llvm models select's as two-address instructions. That means a copy |
| 309 | // is inserted before a t2MOVccr, etc. If the copy is scheduled in |
| 310 | // between selects we would end up creating multiple IT blocks. |
| 311 | |
| 312 | // First check if it's safe to move it. |
| 313 | if (Uses.count(DstReg) || Defs.count(SrcReg)) |
| 314 | return false; |
| 315 | |
| 316 | // Then peek at the next instruction to see if it's predicated on CC or OCC. |
| 317 | // If not, then there is nothing to be gained by moving the copy. |
| 318 | MachineBasicBlock::iterator I = MI; ++I; |
| 319 | MachineBasicBlock::iterator E = MI->getParent()->end(); |
| 320 | while (I != E && I->isDebugValue()) |
| 321 | ++I; |
| 322 | unsigned NPredReg = 0; |
| 323 | ARMCC::CondCodes NCC = getPredicate(I, NPredReg); |
| 324 | if (NCC == CC || NCC == OCC) |
| 325 | return true; |
| 326 | } |
| 327 | return false; |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | bool Thumb2ITBlockPass::InsertITInstructions(MachineBasicBlock &MBB) { |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 331 | bool Modified = false; |
| 332 | |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 333 | SmallSet<unsigned, 4> Defs; |
| 334 | SmallSet<unsigned, 4> Uses; |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 335 | MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end(); |
| 336 | while (MBBI != E) { |
| 337 | MachineInstr *MI = &*MBBI; |
Evan Cheng | 5adb66a | 2009-09-28 09:14:39 +0000 | [diff] [blame] | 338 | DebugLoc dl = MI->getDebugLoc(); |
| 339 | unsigned PredReg = 0; |
| 340 | ARMCC::CondCodes CC = getPredicate(MI, PredReg); |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 341 | if (CC == ARMCC::AL) { |
| 342 | ++MBBI; |
| 343 | continue; |
| 344 | } |
| 345 | |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 346 | Defs.clear(); |
| 347 | Uses.clear(); |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame^] | 348 | TrackDefUses(MI, Defs, Uses, TRI); |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 349 | |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 350 | // Insert an IT instruction. |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 351 | MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII->get(ARM::t2IT)) |
| 352 | .addImm(CC); |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame^] | 353 | |
| 354 | // Add implicit use of ITSTATE to IT block instructions. |
| 355 | MI->addOperand(MachineOperand::CreateReg(ARM::ITSTATE, false/*ifDef*/, |
| 356 | true/*isImp*/, false/*isKill*/)); |
| 357 | |
| 358 | MachineInstr *LastITMI = MI; |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 359 | MachineBasicBlock::iterator InsertPos = MIB; |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 360 | ++MBBI; |
| 361 | |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame^] | 362 | // Form IT block. |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 363 | ARMCC::CondCodes OCC = ARMCC::getOppositeCondition(CC); |
Evan Cheng | bc9b754 | 2009-08-15 07:59:10 +0000 | [diff] [blame] | 364 | unsigned Mask = 0, Pos = 3; |
Sandeep Patel | 452b54a | 2009-10-15 22:25:32 +0000 | [diff] [blame] | 365 | // Branches, including tricky ones like LDM_RET, need to end an IT |
| 366 | // block so check the instruction we just put in the block. |
Jim Grosbach | 8077e76 | 2010-06-07 21:48:47 +0000 | [diff] [blame] | 367 | for (; MBBI != E && Pos && |
| 368 | (!MI->getDesc().isBranch() && !MI->getDesc().isReturn()) ; ++MBBI) { |
| 369 | if (MBBI->isDebugValue()) |
| 370 | continue; |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 371 | |
Evan Cheng | fd84711 | 2009-09-28 20:47:15 +0000 | [diff] [blame] | 372 | MachineInstr *NMI = &*MBBI; |
Sandeep Patel | 452b54a | 2009-10-15 22:25:32 +0000 | [diff] [blame] | 373 | MI = NMI; |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 374 | |
Evan Cheng | fd84711 | 2009-09-28 20:47:15 +0000 | [diff] [blame] | 375 | unsigned NPredReg = 0; |
| 376 | ARMCC::CondCodes NCC = getPredicate(NMI, NPredReg); |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame^] | 377 | if (NCC == CC || NCC == OCC) { |
Johnny Chen | b675e25 | 2010-03-17 23:14:23 +0000 | [diff] [blame] | 378 | Mask |= (NCC & 1) << Pos; |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame^] | 379 | // Add implicit use of ITSTATE. |
| 380 | NMI->addOperand(MachineOperand::CreateReg(ARM::ITSTATE, false/*ifDef*/, |
| 381 | true/*isImp*/, false/*isKill*/)); |
| 382 | LastITMI = NMI; |
| 383 | } else { |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 384 | if (NCC == ARMCC::AL && |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame^] | 385 | MoveCopyOutOfITBlock(NMI, CC, OCC, Defs, Uses)) { |
| 386 | --MBBI; |
| 387 | MBB.remove(NMI); |
| 388 | MBB.insert(InsertPos, NMI); |
| 389 | ++NumMovedInsts; |
| 390 | continue; |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 391 | } |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 392 | break; |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 393 | } |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame^] | 394 | TrackDefUses(NMI, Defs, Uses, TRI); |
Evan Cheng | bc9b754 | 2009-08-15 07:59:10 +0000 | [diff] [blame] | 395 | --Pos; |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 396 | } |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 397 | |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame^] | 398 | // Finalize IT mask. |
Evan Cheng | bc9b754 | 2009-08-15 07:59:10 +0000 | [diff] [blame] | 399 | Mask |= (1 << Pos); |
Johnny Chen | b675e25 | 2010-03-17 23:14:23 +0000 | [diff] [blame] | 400 | // Tag along (firstcond[0] << 4) with the mask. |
| 401 | Mask |= (CC & 1) << 4; |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 402 | MIB.addImm(Mask); |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame^] | 403 | |
| 404 | // Last instruction in IT block kills ITSTATE. |
| 405 | LastITMI->findRegisterUseOperand(ARM::ITSTATE)->setIsKill(); |
| 406 | |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 407 | Modified = true; |
| 408 | ++NumITs; |
| 409 | } |
| 410 | |
| 411 | return Modified; |
| 412 | } |
| 413 | |
| 414 | bool Thumb2ITBlockPass::runOnMachineFunction(MachineFunction &Fn) { |
| 415 | const TargetMachine &TM = Fn.getTarget(); |
| 416 | AFI = Fn.getInfo<ARMFunctionInfo>(); |
Evan Cheng | ed338e8 | 2009-07-11 07:26:20 +0000 | [diff] [blame] | 417 | TII = static_cast<const Thumb2InstrInfo*>(TM.getInstrInfo()); |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame^] | 418 | TRI = TM.getRegisterInfo(); |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 419 | |
| 420 | if (!AFI->isThumbFunction()) |
| 421 | return false; |
| 422 | |
| 423 | bool Modified = false; |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 424 | for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E; ) { |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 425 | MachineBasicBlock &MBB = *MFI; |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 426 | ++MFI; |
| 427 | if (PreRegAlloc) |
| 428 | Modified |= InsertITBlocks(MBB); |
| 429 | else |
| 430 | Modified |= InsertITInstructions(MBB); |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Evan Cheng | 86050dc | 2010-06-18 23:09:54 +0000 | [diff] [blame^] | 433 | if (Modified && !PreRegAlloc) |
| 434 | AFI->setHasITBlocks(true); |
| 435 | |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 436 | return Modified; |
| 437 | } |
| 438 | |
Evan Cheng | 34f8a02 | 2009-08-08 02:54:37 +0000 | [diff] [blame] | 439 | /// createThumb2ITBlockPass - Returns an instance of the Thumb2 IT blocks |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 440 | /// insertion pass. |
Evan Cheng | d847124 | 2010-06-09 01:46:50 +0000 | [diff] [blame] | 441 | FunctionPass *llvm::createThumb2ITBlockPass(bool PreAlloc) { |
| 442 | return new Thumb2ITBlockPass(PreAlloc); |
Evan Cheng | 06e1658 | 2009-07-10 01:54:42 +0000 | [diff] [blame] | 443 | } |