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