Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1 | //===-- MachineLICM.cpp - Machine Loop Invariant Code Motion Pass ---------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass performs loop invariant code motion on machine instructions. We |
| 11 | // attempt to remove as much code from the body of a loop as possible. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "machine-licm" |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/IndexedMap.h" |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
| 18 | #include "llvm/ADT/Statistic.h" |
| 19 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 20 | #include "llvm/CodeGen/MachineDominators.h" |
| 21 | #include "llvm/CodeGen/MachineInstr.h" |
| 22 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/Passes.h" |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CFG.h" |
| 26 | #include "llvm/Support/CommandLine.h" |
| 27 | #include "llvm/Support/Compiler.h" |
| 28 | #include "llvm/Support/Debug.h" |
| 29 | #include "llvm/Target/MRegisterInfo.h" |
Bill Wendling | efe2be7 | 2007-12-11 23:27:51 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetInstrInfo.h" |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetMachine.h" |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace llvm; |
| 34 | |
| 35 | namespace { |
| 36 | // Hidden options to help debugging |
| 37 | cl::opt<bool> |
| 38 | PerformLICM("machine-licm", |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 39 | cl::init(false), cl::Hidden, |
| 40 | cl::desc("Perform loop-invariant code motion on machine code")); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 43 | STATISTIC(NumHoisted, "Number of machine instructions hoisted out of loops"); |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 44 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 45 | namespace { |
| 46 | class VISIBILITY_HIDDEN MachineLICM : public MachineFunctionPass { |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 47 | const TargetMachine *TM; |
Bill Wendling | efe2be7 | 2007-12-11 23:27:51 +0000 | [diff] [blame] | 48 | const TargetInstrInfo *TII; |
| 49 | MachineFunction *CurMF; // Current MachineFunction |
Bill Wendling | 12ebf14 | 2007-12-11 19:40:06 +0000 | [diff] [blame] | 50 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 51 | // Various analyses that we use... |
| 52 | MachineLoopInfo *LI; // Current MachineLoopInfo |
| 53 | MachineDominatorTree *DT; // Machine dominator tree for the current Loop |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 54 | MachineRegisterInfo *RegInfo; // Machine register information |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 55 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 56 | // State that is updated as we process loops |
| 57 | bool Changed; // True if a loop is changed. |
| 58 | MachineLoop *CurLoop; // The current loop we are working on. |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 59 | public: |
| 60 | static char ID; // Pass identification, replacement for typeid |
| 61 | MachineLICM() : MachineFunctionPass((intptr_t)&ID) {} |
| 62 | |
| 63 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 64 | |
| 65 | /// FIXME: Loop preheaders? |
| 66 | /// |
| 67 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 68 | AU.setPreservesCFG(); |
| 69 | AU.addRequired<MachineLoopInfo>(); |
| 70 | AU.addRequired<MachineDominatorTree>(); |
| 71 | } |
| 72 | private: |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 73 | /// VisitAllLoops - Visit all of the loops in depth first order and try to |
| 74 | /// hoist invariant instructions from them. |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 75 | /// |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 76 | void VisitAllLoops(MachineLoop *L) { |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 77 | const std::vector<MachineLoop*> &SubLoops = L->getSubLoops(); |
| 78 | |
| 79 | for (MachineLoop::iterator |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 80 | I = SubLoops.begin(), E = SubLoops.end(); I != E; ++I) { |
| 81 | MachineLoop *ML = *I; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 82 | |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 83 | // Traverse the body of the loop in depth first order on the dominator |
| 84 | // tree so that we are guaranteed to see definitions before we see uses. |
| 85 | VisitAllLoops(ML); |
| 86 | HoistRegion(DT->getNode(ML->getHeader())); |
| 87 | } |
| 88 | |
| 89 | HoistRegion(DT->getNode(L->getHeader())); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 92 | /// IsInSubLoop - A little predicate that returns true if the specified |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 93 | /// basic block is in a subloop of the current one, not the current one |
| 94 | /// itself. |
| 95 | /// |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 96 | bool IsInSubLoop(MachineBasicBlock *BB) { |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 97 | assert(CurLoop->contains(BB) && "Only valid if BB is IN the loop"); |
Bill Wendling | 650b052 | 2007-12-11 18:45:11 +0000 | [diff] [blame] | 98 | return LI->getLoopFor(BB) != CurLoop; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 101 | /// IsLoopInvariantInst - Returns true if the instruction is loop |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 102 | /// invariant. I.e., all virtual register operands are defined outside of |
| 103 | /// the loop, physical registers aren't accessed (explicitly or implicitly), |
| 104 | /// and the instruction is hoistable. |
| 105 | /// |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 106 | bool IsLoopInvariantInst(MachineInstr &I); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 107 | |
| 108 | /// FindPredecessors - Get all of the predecessors of the loop that are not |
| 109 | /// back-edges. |
| 110 | /// |
Bill Wendling | 650b052 | 2007-12-11 18:45:11 +0000 | [diff] [blame] | 111 | void FindPredecessors(std::vector<MachineBasicBlock*> &Preds) { |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 112 | const MachineBasicBlock *Header = CurLoop->getHeader(); |
| 113 | |
| 114 | for (MachineBasicBlock::const_pred_iterator |
| 115 | I = Header->pred_begin(), E = Header->pred_end(); I != E; ++I) |
| 116 | if (!CurLoop->contains(*I)) |
| 117 | Preds.push_back(*I); |
| 118 | } |
| 119 | |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 120 | /// MoveInstToEndOfBlock - Moves the machine instruction to the bottom of |
| 121 | /// the predecessor basic block (but before the terminator instructions). |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 122 | /// |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 123 | void MoveInstToEndOfBlock(MachineBasicBlock *ToMBB, |
| 124 | MachineBasicBlock *FromMBB, |
| 125 | MachineInstr *MI) { |
Bill Wendling | efe2be7 | 2007-12-11 23:27:51 +0000 | [diff] [blame] | 126 | DEBUG({ |
| 127 | DOUT << "Hoisting " << *MI; |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 128 | if (ToMBB->getBasicBlock()) |
Bill Wendling | efe2be7 | 2007-12-11 23:27:51 +0000 | [diff] [blame] | 129 | DOUT << " to MachineBasicBlock " |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 130 | << ToMBB->getBasicBlock()->getName(); |
Bill Wendling | efe2be7 | 2007-12-11 23:27:51 +0000 | [diff] [blame] | 131 | DOUT << "\n"; |
| 132 | }); |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 133 | |
| 134 | MachineBasicBlock::iterator WhereIter = ToMBB->getFirstTerminator(); |
| 135 | MachineBasicBlock::iterator To, From = FromMBB->begin(); |
| 136 | |
| 137 | while (&*From != MI) |
| 138 | ++From; |
| 139 | |
| 140 | assert(From != FromMBB->end() && "Didn't find instr in BB!"); |
| 141 | |
| 142 | To = From; |
| 143 | ToMBB->splice(WhereIter, FromMBB, From, ++To); |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 144 | ++NumHoisted; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /// HoistRegion - Walk the specified region of the CFG (defined by all |
| 148 | /// blocks dominated by the specified block, and that are in the current |
| 149 | /// loop) in depth first order w.r.t the DominatorTree. This allows us to |
| 150 | /// visit definitions before uses, allowing us to hoist a loop body in one |
| 151 | /// pass without iteration. |
| 152 | /// |
| 153 | void HoistRegion(MachineDomTreeNode *N); |
| 154 | |
| 155 | /// Hoist - When an instruction is found to only use loop invariant operands |
| 156 | /// that is safe to hoist, this instruction is called to do the dirty work. |
| 157 | /// |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 158 | void Hoist(MachineInstr &MI); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | char MachineLICM::ID = 0; |
| 162 | RegisterPass<MachineLICM> X("machine-licm", |
| 163 | "Machine Loop Invariant Code Motion"); |
| 164 | } // end anonymous namespace |
| 165 | |
| 166 | FunctionPass *llvm::createMachineLICMPass() { return new MachineLICM(); } |
| 167 | |
| 168 | /// Hoist expressions out of the specified loop. Note, alias info for inner loop |
| 169 | /// is not preserved so it is not a good idea to run LICM multiple times on one |
| 170 | /// loop. |
| 171 | /// |
| 172 | bool MachineLICM::runOnMachineFunction(MachineFunction &MF) { |
| 173 | if (!PerformLICM) return false; // For debugging. |
| 174 | |
Bill Wendling | a17ad59 | 2007-12-11 22:22:22 +0000 | [diff] [blame] | 175 | DOUT << "******** Machine LICM ********\n"; |
| 176 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 177 | Changed = false; |
Bill Wendling | 12ebf14 | 2007-12-11 19:40:06 +0000 | [diff] [blame] | 178 | CurMF = &MF; |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 179 | TM = &CurMF->getTarget(); |
| 180 | TII = TM->getInstrInfo(); |
Bill Wendling | dde059a | 2008-01-02 21:10:54 +0000 | [diff] [blame] | 181 | RegInfo = &CurMF->getRegInfo(); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 182 | |
| 183 | // Get our Loop information... |
| 184 | LI = &getAnalysis<MachineLoopInfo>(); |
| 185 | DT = &getAnalysis<MachineDominatorTree>(); |
| 186 | |
| 187 | for (MachineLoopInfo::iterator |
| 188 | I = LI->begin(), E = LI->end(); I != E; ++I) { |
Bill Wendling | a17ad59 | 2007-12-11 22:22:22 +0000 | [diff] [blame] | 189 | CurLoop = *I; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 190 | |
| 191 | // Visit all of the instructions of the loop. We want to visit the subloops |
| 192 | // first, though, so that we can hoist their invariants first into their |
| 193 | // containing loop before we process that loop. |
Bill Wendling | a17ad59 | 2007-12-11 22:22:22 +0000 | [diff] [blame] | 194 | VisitAllLoops(CurLoop); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | return Changed; |
| 198 | } |
| 199 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 200 | /// HoistRegion - Walk the specified region of the CFG (defined by all blocks |
| 201 | /// dominated by the specified block, and that are in the current loop) in depth |
| 202 | /// first order w.r.t the DominatorTree. This allows us to visit definitions |
| 203 | /// before uses, allowing us to hoist a loop body in one pass without iteration. |
| 204 | /// |
| 205 | void MachineLICM::HoistRegion(MachineDomTreeNode *N) { |
| 206 | assert(N != 0 && "Null dominator tree node?"); |
| 207 | MachineBasicBlock *BB = N->getBlock(); |
| 208 | |
| 209 | // If this subregion is not in the top level loop at all, exit. |
| 210 | if (!CurLoop->contains(BB)) return; |
| 211 | |
| 212 | // Only need to process the contents of this block if it is not part of a |
| 213 | // subloop (which would already have been processed). |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 214 | if (!IsInSubLoop(BB)) |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 215 | for (MachineBasicBlock::iterator |
| 216 | I = BB->begin(), E = BB->end(); I != E; ) { |
| 217 | MachineInstr &MI = *I++; |
| 218 | |
| 219 | // Try hoisting the instruction out of the loop. We can only do this if |
| 220 | // all of the operands of the instruction are loop invariant and if it is |
| 221 | // safe to hoist the instruction. |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 222 | Hoist(MI); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | const std::vector<MachineDomTreeNode*> &Children = N->getChildren(); |
| 226 | |
| 227 | for (unsigned I = 0, E = Children.size(); I != E; ++I) |
| 228 | HoistRegion(Children[I]); |
| 229 | } |
| 230 | |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 231 | /// IsLoopInvariantInst - Returns true if the instruction is loop |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 232 | /// invariant. I.e., all virtual register operands are defined outside of the |
Bill Wendling | 60ff1a3 | 2007-12-20 01:08:10 +0000 | [diff] [blame] | 233 | /// loop, physical registers aren't accessed explicitly, and there are no side |
| 234 | /// effects that aren't captured by the operands or other flags. |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 235 | /// |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 236 | bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) { |
Bill Wendling | 280f456 | 2007-12-18 21:38:04 +0000 | [diff] [blame] | 237 | DEBUG({ |
| 238 | DOUT << "--- Checking if we can hoist " << I; |
| 239 | if (I.getInstrDescriptor()->ImplicitUses) { |
| 240 | DOUT << " * Instruction has implicit uses:\n"; |
| 241 | |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 242 | const MRegisterInfo *MRI = TM->getRegisterInfo(); |
Bill Wendling | 280f456 | 2007-12-18 21:38:04 +0000 | [diff] [blame] | 243 | const unsigned *ImpUses = I.getInstrDescriptor()->ImplicitUses; |
| 244 | |
| 245 | for (; *ImpUses; ++ImpUses) |
| 246 | DOUT << " -> " << MRI->getName(*ImpUses) << "\n"; |
| 247 | } |
| 248 | |
| 249 | if (I.getInstrDescriptor()->ImplicitDefs) { |
| 250 | DOUT << " * Instruction has implicit defines:\n"; |
| 251 | |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 252 | const MRegisterInfo *MRI = TM->getRegisterInfo(); |
Bill Wendling | 280f456 | 2007-12-18 21:38:04 +0000 | [diff] [blame] | 253 | const unsigned *ImpDefs = I.getInstrDescriptor()->ImplicitDefs; |
| 254 | |
| 255 | for (; *ImpDefs; ++ImpDefs) |
| 256 | DOUT << " -> " << MRI->getName(*ImpDefs) << "\n"; |
| 257 | } |
| 258 | |
| 259 | if (TII->hasUnmodelledSideEffects(&I)) |
| 260 | DOUT << " * Instruction has side effects.\n"; |
| 261 | }); |
| 262 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 263 | // The instruction is loop invariant if all of its operands are loop-invariant |
| 264 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
| 265 | const MachineOperand &MO = I.getOperand(i); |
| 266 | |
Bill Wendling | 280f456 | 2007-12-18 21:38:04 +0000 | [diff] [blame] | 267 | if (!(MO.isRegister() && MO.getReg() && MO.isUse())) |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 268 | continue; |
| 269 | |
| 270 | unsigned Reg = MO.getReg(); |
| 271 | |
| 272 | // Don't hoist instructions that access physical registers. |
| 273 | if (!MRegisterInfo::isVirtualRegister(Reg)) |
| 274 | return false; |
| 275 | |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 276 | assert(RegInfo->getVRegDef(Reg)&&"Machine instr not mapped for this vreg?"); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 277 | |
| 278 | // If the loop contains the definition of an operand, then the instruction |
| 279 | // isn't loop invariant. |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 280 | if (CurLoop->contains(RegInfo->getVRegDef(Reg)->getParent())) |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 281 | return false; |
| 282 | } |
| 283 | |
Bill Wendling | 60ff1a3 | 2007-12-20 01:08:10 +0000 | [diff] [blame] | 284 | // Don't hoist something that has unmodelled side effects. |
Bill Wendling | 280f456 | 2007-12-18 21:38:04 +0000 | [diff] [blame] | 285 | if (TII->hasUnmodelledSideEffects(&I)) return false; |
| 286 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 287 | // If we got this far, the instruction is loop invariant! |
| 288 | return true; |
| 289 | } |
| 290 | |
| 291 | /// Hoist - When an instruction is found to only use loop invariant operands |
| 292 | /// that is safe to hoist, this instruction is called to do the dirty work. |
| 293 | /// |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 294 | void MachineLICM::Hoist(MachineInstr &MI) { |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 295 | if (!IsLoopInvariantInst(MI)) return; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 296 | |
| 297 | std::vector<MachineBasicBlock*> Preds; |
| 298 | |
| 299 | // Non-back-edge predecessors. |
| 300 | FindPredecessors(Preds); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 301 | |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 302 | // Either we don't have any predecessors(?!) or we have more than one, which |
| 303 | // is forbidden. |
| 304 | if (Preds.empty() || Preds.size() != 1) return; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 305 | |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 306 | // Check that the predecessor is qualified to take the hoisted |
| 307 | // instruction. I.e., there is only one edge from the predecessor, and it's to |
| 308 | // the loop header. |
| 309 | MachineBasicBlock *MBB = Preds.front(); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 310 | |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 311 | // FIXME: We are assuming at first that the basic block coming into this loop |
| 312 | // has only one successor. This isn't the case in general because we haven't |
| 313 | // broken critical edges or added preheaders. |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 314 | if (MBB->succ_size() != 1) return; |
| 315 | assert(*MBB->succ_begin() == CurLoop->getHeader() && |
| 316 | "The predecessor doesn't feed directly into the loop header!"); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 317 | |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 318 | // Now move the instructions to the predecessor. |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 319 | MoveInstToEndOfBlock(MBB, MI.getParent(), &MI); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 320 | Changed = true; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 321 | } |