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