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