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 | // |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 13 | // This pass does not attempt to throttle itself to limit register pressure. |
| 14 | // The register allocation phases are expected to perform rematerialization |
| 15 | // to recover when register pressure is high. |
| 16 | // |
| 17 | // This pass is not intended to be a replacement or a complete alternative |
| 18 | // for the LLVM-IR-level LICM pass. It is only designed to hoist simple |
| 19 | // constructs that are not exposed before lowering and instruction selection. |
| 20 | // |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 21 | //===----------------------------------------------------------------------===// |
| 22 | |
| 23 | #define DEBUG_TYPE "machine-licm" |
Chris Lattner | ac69582 | 2008-01-04 06:41:45 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/Passes.h" |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineDominators.h" |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetRegisterInfo.h" |
Bill Wendling | efe2be7 | 2007-12-11 23:27:51 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetInstrInfo.h" |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | ac69582 | 2008-01-04 06:41:45 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/Statistic.h" |
| 32 | #include "llvm/Support/CommandLine.h" |
| 33 | #include "llvm/Support/Compiler.h" |
| 34 | #include "llvm/Support/Debug.h" |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 35 | |
| 36 | using namespace llvm; |
| 37 | |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 38 | STATISTIC(NumHoisted, "Number of machine instructions hoisted out of loops"); |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 39 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 40 | namespace { |
| 41 | class VISIBILITY_HIDDEN MachineLICM : public MachineFunctionPass { |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 42 | const TargetMachine *TM; |
Bill Wendling | efe2be7 | 2007-12-11 23:27:51 +0000 | [diff] [blame] | 43 | const TargetInstrInfo *TII; |
Bill Wendling | 12ebf14 | 2007-12-11 19:40:06 +0000 | [diff] [blame] | 44 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 45 | // Various analyses that we use... |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 46 | MachineLoopInfo *LI; // Current MachineLoopInfo |
| 47 | MachineDominatorTree *DT; // Machine dominator tree for the cur loop |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 48 | MachineRegisterInfo *RegInfo; // Machine register information |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 49 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 50 | // State that is updated as we process loops |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 51 | bool Changed; // True if a loop is changed. |
| 52 | MachineLoop *CurLoop; // The current loop we are working on. |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 53 | MachineBasicBlock *CurPreheader; // The preheader for CurLoop. |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 54 | public: |
| 55 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 56 | MachineLICM() : MachineFunctionPass(&ID) {} |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 57 | |
| 58 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 59 | |
Dan Gohman | 7224170 | 2008-12-18 01:37:56 +0000 | [diff] [blame] | 60 | const char *getPassName() const { return "Machine Instruction LICM"; } |
| 61 | |
Bill Wendling | 074223a | 2008-03-10 08:13:01 +0000 | [diff] [blame] | 62 | // FIXME: Loop preheaders? |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 63 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 64 | AU.setPreservesCFG(); |
| 65 | AU.addRequired<MachineLoopInfo>(); |
| 66 | AU.addRequired<MachineDominatorTree>(); |
Bill Wendling | d5da704 | 2008-01-04 08:48:49 +0000 | [diff] [blame] | 67 | AU.addPreserved<MachineLoopInfo>(); |
| 68 | AU.addPreserved<MachineDominatorTree>(); |
| 69 | MachineFunctionPass::getAnalysisUsage(AU); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 70 | } |
| 71 | private: |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 72 | /// IsLoopInvariantInst - Returns true if the instruction is loop |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 73 | /// invariant. I.e., all virtual register operands are defined outside of |
| 74 | /// the loop, physical registers aren't accessed (explicitly or implicitly), |
| 75 | /// and the instruction is hoistable. |
| 76 | /// |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 77 | bool IsLoopInvariantInst(MachineInstr &I); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 78 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 79 | /// HoistRegion - Walk the specified region of the CFG (defined by all |
| 80 | /// blocks dominated by the specified block, and that are in the current |
| 81 | /// loop) in depth first order w.r.t the DominatorTree. This allows us to |
| 82 | /// visit definitions before uses, allowing us to hoist a loop body in one |
| 83 | /// pass without iteration. |
| 84 | /// |
| 85 | void HoistRegion(MachineDomTreeNode *N); |
| 86 | |
| 87 | /// Hoist - When an instruction is found to only use loop invariant operands |
| 88 | /// that is safe to hoist, this instruction is called to do the dirty work. |
| 89 | /// |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 90 | void Hoist(MachineInstr &MI); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 91 | }; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 92 | } // end anonymous namespace |
| 93 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 94 | char MachineLICM::ID = 0; |
| 95 | static RegisterPass<MachineLICM> |
Bill Wendling | 8870ce9 | 2008-07-07 05:42:27 +0000 | [diff] [blame] | 96 | X("machinelicm", "Machine Loop Invariant Code Motion"); |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 97 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 98 | FunctionPass *llvm::createMachineLICMPass() { return new MachineLICM(); } |
| 99 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 100 | /// LoopIsOuterMostWithPreheader - Test if the given loop is the outer-most |
| 101 | /// loop that has a preheader. |
| 102 | static bool LoopIsOuterMostWithPreheader(MachineLoop *CurLoop) { |
| 103 | for (MachineLoop *L = CurLoop->getParentLoop(); L; L = L->getParentLoop()) |
| 104 | if (L->getLoopPreheader()) |
| 105 | return false; |
| 106 | return true; |
| 107 | } |
| 108 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 109 | /// Hoist expressions out of the specified loop. Note, alias info for inner loop |
| 110 | /// is not preserved so it is not a good idea to run LICM multiple times on one |
| 111 | /// loop. |
| 112 | /// |
| 113 | bool MachineLICM::runOnMachineFunction(MachineFunction &MF) { |
Bill Wendling | a17ad59 | 2007-12-11 22:22:22 +0000 | [diff] [blame] | 114 | DOUT << "******** Machine LICM ********\n"; |
| 115 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 116 | Changed = false; |
Bill Wendling | acb04ec | 2008-08-31 02:30:23 +0000 | [diff] [blame] | 117 | TM = &MF.getTarget(); |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 118 | TII = TM->getInstrInfo(); |
Bill Wendling | acb04ec | 2008-08-31 02:30:23 +0000 | [diff] [blame] | 119 | RegInfo = &MF.getRegInfo(); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 120 | |
| 121 | // Get our Loop information... |
| 122 | LI = &getAnalysis<MachineLoopInfo>(); |
| 123 | DT = &getAnalysis<MachineDominatorTree>(); |
| 124 | |
| 125 | for (MachineLoopInfo::iterator |
| 126 | I = LI->begin(), E = LI->end(); I != E; ++I) { |
Bill Wendling | a17ad59 | 2007-12-11 22:22:22 +0000 | [diff] [blame] | 127 | CurLoop = *I; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 128 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 129 | // Only visit outer-most preheader-sporting loops. |
| 130 | if (!LoopIsOuterMostWithPreheader(CurLoop)) |
| 131 | continue; |
| 132 | |
| 133 | // Determine the block to which to hoist instructions. If we can't find a |
| 134 | // suitable loop preheader, we can't do any hoisting. |
| 135 | // |
| 136 | // FIXME: We are only hoisting if the basic block coming into this loop |
| 137 | // has only one successor. This isn't the case in general because we haven't |
| 138 | // broken critical edges or added preheaders. |
| 139 | CurPreheader = CurLoop->getLoopPreheader(); |
| 140 | if (!CurPreheader) |
| 141 | continue; |
| 142 | |
| 143 | HoistRegion(DT->getNode(CurLoop->getHeader())); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | return Changed; |
| 147 | } |
| 148 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 149 | /// HoistRegion - Walk the specified region of the CFG (defined by all blocks |
| 150 | /// dominated by the specified block, and that are in the current loop) in depth |
| 151 | /// first order w.r.t the DominatorTree. This allows us to visit definitions |
| 152 | /// before uses, allowing us to hoist a loop body in one pass without iteration. |
| 153 | /// |
| 154 | void MachineLICM::HoistRegion(MachineDomTreeNode *N) { |
| 155 | assert(N != 0 && "Null dominator tree node?"); |
| 156 | MachineBasicBlock *BB = N->getBlock(); |
| 157 | |
| 158 | // If this subregion is not in the top level loop at all, exit. |
| 159 | if (!CurLoop->contains(BB)) return; |
| 160 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 161 | for (MachineBasicBlock::iterator |
| 162 | I = BB->begin(), E = BB->end(); I != E; ) { |
| 163 | MachineInstr &MI = *I++; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 164 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 165 | // Try hoisting the instruction out of the loop. We can only do this if |
| 166 | // all of the operands of the instruction are loop invariant and if it is |
| 167 | // safe to hoist the instruction. |
| 168 | Hoist(MI); |
| 169 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 170 | |
| 171 | const std::vector<MachineDomTreeNode*> &Children = N->getChildren(); |
| 172 | |
| 173 | for (unsigned I = 0, E = Children.size(); I != E; ++I) |
| 174 | HoistRegion(Children[I]); |
| 175 | } |
| 176 | |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 177 | /// IsLoopInvariantInst - Returns true if the instruction is loop |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 178 | /// invariant. I.e., all virtual register operands are defined outside of the |
Bill Wendling | 60ff1a3 | 2007-12-20 01:08:10 +0000 | [diff] [blame] | 179 | /// loop, physical registers aren't accessed explicitly, and there are no side |
| 180 | /// effects that aren't captured by the operands or other flags. |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 181 | /// |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 182 | bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) { |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 183 | const TargetInstrDesc &TID = I.getDesc(); |
| 184 | |
| 185 | // Ignore stuff that we obviously can't hoist. |
Dan Gohman | 237dee1 | 2008-12-23 17:28:50 +0000 | [diff] [blame] | 186 | if (TID.mayStore() || TID.isCall() || TID.isTerminator() || |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 187 | TID.hasUnmodeledSideEffects()) |
| 188 | return false; |
Evan Cheng | 9b61f33 | 2009-02-04 07:17:49 +0000 | [diff] [blame^] | 189 | |
| 190 | bool isInvLoad = false; |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 191 | if (TID.mayLoad()) { |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 192 | // Okay, this instruction does a load. As a refinement, we allow the target |
| 193 | // to decide whether the loaded value is actually a constant. If so, we can |
| 194 | // actually use it as a load. |
Evan Cheng | 9b61f33 | 2009-02-04 07:17:49 +0000 | [diff] [blame^] | 195 | isInvLoad = TII->isInvariantLoad(&I); |
| 196 | if (!isInvLoad) |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 197 | // FIXME: we should be able to sink loads with no other side effects if |
| 198 | // 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] | 199 | // block. This is a trivial form of alias analysis. |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 200 | return false; |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 201 | } |
Bill Wendling | 074223a | 2008-03-10 08:13:01 +0000 | [diff] [blame] | 202 | |
Evan Cheng | 9b61f33 | 2009-02-04 07:17:49 +0000 | [diff] [blame^] | 203 | // FIXME: For now, only hoist re-materilizable instructions. LICM will |
| 204 | // increase register pressure. We want to make sure it doesn't increase |
| 205 | // spilling. |
| 206 | if (!isInvLoad && (!TID.isRematerializable() || |
| 207 | !TII->isTriviallyReMaterializable(&I))) |
| 208 | return false; |
| 209 | |
Bill Wendling | 280f456 | 2007-12-18 21:38:04 +0000 | [diff] [blame] | 210 | DEBUG({ |
| 211 | DOUT << "--- Checking if we can hoist " << I; |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 212 | if (I.getDesc().getImplicitUses()) { |
Bill Wendling | 280f456 | 2007-12-18 21:38:04 +0000 | [diff] [blame] | 213 | DOUT << " * Instruction has implicit uses:\n"; |
| 214 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 215 | const TargetRegisterInfo *TRI = TM->getRegisterInfo(); |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 216 | for (const unsigned *ImpUses = I.getDesc().getImplicitUses(); |
Chris Lattner | 6924430 | 2008-01-07 01:56:04 +0000 | [diff] [blame] | 217 | *ImpUses; ++ImpUses) |
Bill Wendling | e6d088a | 2008-02-26 21:47:57 +0000 | [diff] [blame] | 218 | DOUT << " -> " << TRI->getName(*ImpUses) << "\n"; |
Bill Wendling | 280f456 | 2007-12-18 21:38:04 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 221 | if (I.getDesc().getImplicitDefs()) { |
Bill Wendling | 280f456 | 2007-12-18 21:38:04 +0000 | [diff] [blame] | 222 | DOUT << " * Instruction has implicit defines:\n"; |
| 223 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 224 | const TargetRegisterInfo *TRI = TM->getRegisterInfo(); |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 225 | for (const unsigned *ImpDefs = I.getDesc().getImplicitDefs(); |
Chris Lattner | 6924430 | 2008-01-07 01:56:04 +0000 | [diff] [blame] | 226 | *ImpDefs; ++ImpDefs) |
Bill Wendling | e6d088a | 2008-02-26 21:47:57 +0000 | [diff] [blame] | 227 | DOUT << " -> " << TRI->getName(*ImpDefs) << "\n"; |
Bill Wendling | 280f456 | 2007-12-18 21:38:04 +0000 | [diff] [blame] | 228 | } |
Bill Wendling | 280f456 | 2007-12-18 21:38:04 +0000 | [diff] [blame] | 229 | }); |
| 230 | |
Bill Wendling | d3361e9 | 2008-08-18 00:33:49 +0000 | [diff] [blame] | 231 | if (I.getDesc().getImplicitDefs() || I.getDesc().getImplicitUses()) { |
| 232 | DOUT << "Cannot hoist with implicit defines or uses\n"; |
| 233 | return false; |
| 234 | } |
| 235 | |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 236 | // The instruction is loop invariant if all of its operands are. |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 237 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
| 238 | const MachineOperand &MO = I.getOperand(i); |
| 239 | |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 240 | if (!MO.isReg()) |
Bill Wendling | fb018d0 | 2008-08-20 20:32:05 +0000 | [diff] [blame] | 241 | continue; |
| 242 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 243 | unsigned Reg = MO.getReg(); |
| 244 | if (Reg == 0) continue; |
| 245 | |
| 246 | // Don't hoist an instruction that uses or defines a physical register. |
| 247 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) |
Bill Wendling | fb018d0 | 2008-08-20 20:32:05 +0000 | [diff] [blame] | 248 | return false; |
| 249 | |
| 250 | if (!MO.isUse()) |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 251 | continue; |
| 252 | |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 253 | assert(RegInfo->getVRegDef(Reg) && |
| 254 | "Machine instr not mapped for this vreg?!"); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 255 | |
| 256 | // If the loop contains the definition of an operand, then the instruction |
| 257 | // isn't loop invariant. |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 258 | if (CurLoop->contains(RegInfo->getVRegDef(Reg)->getParent())) |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 259 | return false; |
| 260 | } |
| 261 | |
| 262 | // If we got this far, the instruction is loop invariant! |
| 263 | return true; |
| 264 | } |
| 265 | |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 266 | /// Hoist - When an instruction is found to use only loop invariant operands |
| 267 | /// 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] | 268 | /// |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 269 | void MachineLICM::Hoist(MachineInstr &MI) { |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 270 | if (!IsLoopInvariantInst(MI)) return; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 271 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 272 | // Now move the instructions to the predecessor, inserting it before any |
| 273 | // terminator instructions. |
| 274 | DEBUG({ |
| 275 | DOUT << "Hoisting " << MI; |
| 276 | if (CurPreheader->getBasicBlock()) |
| 277 | DOUT << " to MachineBasicBlock " |
| 278 | << CurPreheader->getBasicBlock()->getName(); |
| 279 | if (MI.getParent()->getBasicBlock()) |
| 280 | DOUT << " from MachineBasicBlock " |
| 281 | << MI.getParent()->getBasicBlock()->getName(); |
| 282 | DOUT << "\n"; |
| 283 | }); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 284 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 285 | CurPreheader->splice(CurPreheader->getFirstTerminator(), MI.getParent(), &MI); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 286 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 287 | ++NumHoisted; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 288 | Changed = true; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 289 | } |