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" |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | ac69582 | 2008-01-04 06:41:45 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | ac69582 | 2008-01-04 06:41:45 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Compiler.h" |
| 34 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 35 | #include "llvm/Support/raw_ostream.h" |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 36 | |
| 37 | using namespace llvm; |
| 38 | |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 39 | STATISTIC(NumHoisted, "Number of machine instructions hoisted out of loops"); |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 40 | STATISTIC(NumCSEed, "Number of hoisted machine instructions CSEed"); |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 41 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 42 | namespace { |
| 43 | class VISIBILITY_HIDDEN MachineLICM : public MachineFunctionPass { |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 44 | const TargetMachine *TM; |
Bill Wendling | efe2be7 | 2007-12-11 23:27:51 +0000 | [diff] [blame] | 45 | const TargetInstrInfo *TII; |
Bill Wendling | 12ebf14 | 2007-12-11 19:40:06 +0000 | [diff] [blame] | 46 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 47 | // Various analyses that we use... |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 48 | MachineLoopInfo *LI; // Current MachineLoopInfo |
| 49 | MachineDominatorTree *DT; // Machine dominator tree for the cur loop |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 50 | MachineRegisterInfo *RegInfo; // Machine register information |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 51 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 52 | // State that is updated as we process loops |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 53 | bool Changed; // True if a loop is changed. |
| 54 | MachineLoop *CurLoop; // The current loop we are working on. |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 55 | MachineBasicBlock *CurPreheader; // The preheader for CurLoop. |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 56 | |
| 57 | // For each BB and opcode pair, keep a list of hoisted instructions. |
| 58 | DenseMap<std::pair<unsigned, unsigned>, |
| 59 | std::vector<const MachineInstr*> > CSEMap; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 60 | public: |
| 61 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 62 | MachineLICM() : MachineFunctionPass(&ID) {} |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 63 | |
| 64 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 65 | |
Dan Gohman | 7224170 | 2008-12-18 01:37:56 +0000 | [diff] [blame] | 66 | const char *getPassName() const { return "Machine Instruction LICM"; } |
| 67 | |
Bill Wendling | 074223a | 2008-03-10 08:13:01 +0000 | [diff] [blame] | 68 | // FIXME: Loop preheaders? |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 69 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 70 | AU.setPreservesCFG(); |
| 71 | AU.addRequired<MachineLoopInfo>(); |
| 72 | AU.addRequired<MachineDominatorTree>(); |
Bill Wendling | d5da704 | 2008-01-04 08:48:49 +0000 | [diff] [blame] | 73 | AU.addPreserved<MachineLoopInfo>(); |
| 74 | AU.addPreserved<MachineDominatorTree>(); |
| 75 | MachineFunctionPass::getAnalysisUsage(AU); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 76 | } |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 77 | |
| 78 | virtual void releaseMemory() { |
| 79 | CSEMap.clear(); |
| 80 | } |
| 81 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 82 | private: |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 83 | /// IsLoopInvariantInst - Returns true if the instruction is loop |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 84 | /// invariant. I.e., all virtual register operands are defined outside of |
| 85 | /// the loop, physical registers aren't accessed (explicitly or implicitly), |
| 86 | /// and the instruction is hoistable. |
| 87 | /// |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 88 | bool IsLoopInvariantInst(MachineInstr &I); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 89 | |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 90 | /// IsProfitableToHoist - Return true if it is potentially profitable to |
| 91 | /// hoist the given loop invariant. |
| 92 | bool IsProfitableToHoist(MachineInstr &MI); |
| 93 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 94 | /// HoistRegion - Walk the specified region of the CFG (defined by all |
| 95 | /// blocks dominated by the specified block, and that are in the current |
| 96 | /// loop) in depth first order w.r.t the DominatorTree. This allows us to |
| 97 | /// visit definitions before uses, allowing us to hoist a loop body in one |
| 98 | /// pass without iteration. |
| 99 | /// |
| 100 | void HoistRegion(MachineDomTreeNode *N); |
| 101 | |
| 102 | /// Hoist - When an instruction is found to only use loop invariant operands |
| 103 | /// that is safe to hoist, this instruction is called to do the dirty work. |
| 104 | /// |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 105 | void Hoist(MachineInstr &MI); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 106 | }; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 107 | } // end anonymous namespace |
| 108 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 109 | char MachineLICM::ID = 0; |
| 110 | static RegisterPass<MachineLICM> |
Bill Wendling | 8870ce9 | 2008-07-07 05:42:27 +0000 | [diff] [blame] | 111 | X("machinelicm", "Machine Loop Invariant Code Motion"); |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 112 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 113 | FunctionPass *llvm::createMachineLICMPass() { return new MachineLICM(); } |
| 114 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 115 | /// LoopIsOuterMostWithPreheader - Test if the given loop is the outer-most |
| 116 | /// loop that has a preheader. |
| 117 | static bool LoopIsOuterMostWithPreheader(MachineLoop *CurLoop) { |
| 118 | for (MachineLoop *L = CurLoop->getParentLoop(); L; L = L->getParentLoop()) |
| 119 | if (L->getLoopPreheader()) |
| 120 | return false; |
| 121 | return true; |
| 122 | } |
| 123 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 124 | /// Hoist expressions out of the specified loop. Note, alias info for inner loop |
| 125 | /// is not preserved so it is not a good idea to run LICM multiple times on one |
| 126 | /// loop. |
| 127 | /// |
| 128 | bool MachineLICM::runOnMachineFunction(MachineFunction &MF) { |
Evan Cheng | 740854b | 2009-02-05 08:51:13 +0000 | [diff] [blame] | 129 | const Function *F = MF.getFunction(); |
| 130 | if (F->hasFnAttr(Attribute::OptimizeForSize)) |
| 131 | return false; |
| 132 | |
Bill Wendling | b7a8992 | 2009-08-22 20:25:44 +0000 | [diff] [blame^] | 133 | DEBUG(errs() << "******** Machine LICM ********\n"); |
Bill Wendling | a17ad59 | 2007-12-11 22:22:22 +0000 | [diff] [blame] | 134 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 135 | Changed = false; |
Bill Wendling | acb04ec | 2008-08-31 02:30:23 +0000 | [diff] [blame] | 136 | TM = &MF.getTarget(); |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 137 | TII = TM->getInstrInfo(); |
Bill Wendling | acb04ec | 2008-08-31 02:30:23 +0000 | [diff] [blame] | 138 | RegInfo = &MF.getRegInfo(); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 139 | |
| 140 | // Get our Loop information... |
| 141 | LI = &getAnalysis<MachineLoopInfo>(); |
| 142 | DT = &getAnalysis<MachineDominatorTree>(); |
| 143 | |
| 144 | for (MachineLoopInfo::iterator |
| 145 | I = LI->begin(), E = LI->end(); I != E; ++I) { |
Bill Wendling | a17ad59 | 2007-12-11 22:22:22 +0000 | [diff] [blame] | 146 | CurLoop = *I; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 147 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 148 | // Only visit outer-most preheader-sporting loops. |
| 149 | if (!LoopIsOuterMostWithPreheader(CurLoop)) |
| 150 | continue; |
| 151 | |
| 152 | // Determine the block to which to hoist instructions. If we can't find a |
| 153 | // suitable loop preheader, we can't do any hoisting. |
| 154 | // |
| 155 | // FIXME: We are only hoisting if the basic block coming into this loop |
| 156 | // has only one successor. This isn't the case in general because we haven't |
| 157 | // broken critical edges or added preheaders. |
| 158 | CurPreheader = CurLoop->getLoopPreheader(); |
| 159 | if (!CurPreheader) |
| 160 | continue; |
| 161 | |
| 162 | HoistRegion(DT->getNode(CurLoop->getHeader())); |
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 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 180 | for (MachineBasicBlock::iterator |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 181 | MII = BB->begin(), E = BB->end(); MII != E; ) { |
| 182 | MachineBasicBlock::iterator NextMII = MII; ++NextMII; |
| 183 | MachineInstr &MI = *MII; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 184 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 185 | Hoist(MI); |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 186 | |
| 187 | MII = NextMII; |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 188 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 189 | |
| 190 | const std::vector<MachineDomTreeNode*> &Children = N->getChildren(); |
| 191 | |
| 192 | for (unsigned I = 0, E = Children.size(); I != E; ++I) |
| 193 | HoistRegion(Children[I]); |
| 194 | } |
| 195 | |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 196 | /// IsLoopInvariantInst - Returns true if the instruction is loop |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 197 | /// invariant. I.e., all virtual register operands are defined outside of the |
Bill Wendling | 60ff1a3 | 2007-12-20 01:08:10 +0000 | [diff] [blame] | 198 | /// loop, physical registers aren't accessed explicitly, and there are no side |
| 199 | /// effects that aren't captured by the operands or other flags. |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 200 | /// |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 201 | bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) { |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 202 | const TargetInstrDesc &TID = I.getDesc(); |
| 203 | |
| 204 | // Ignore stuff that we obviously can't hoist. |
Dan Gohman | 237dee1 | 2008-12-23 17:28:50 +0000 | [diff] [blame] | 205 | if (TID.mayStore() || TID.isCall() || TID.isTerminator() || |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 206 | TID.hasUnmodeledSideEffects()) |
| 207 | return false; |
Evan Cheng | 9b61f33 | 2009-02-04 07:17:49 +0000 | [diff] [blame] | 208 | |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 209 | if (TID.mayLoad()) { |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 210 | // Okay, this instruction does a load. As a refinement, we allow the target |
| 211 | // to decide whether the loaded value is actually a constant. If so, we can |
| 212 | // actually use it as a load. |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 213 | if (!TII->isInvariantLoad(&I)) |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 214 | // FIXME: we should be able to sink loads with no other side effects if |
| 215 | // 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] | 216 | // block. This is a trivial form of alias analysis. |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 217 | return false; |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 218 | } |
Bill Wendling | 074223a | 2008-03-10 08:13:01 +0000 | [diff] [blame] | 219 | |
Bill Wendling | 280f456 | 2007-12-18 21:38:04 +0000 | [diff] [blame] | 220 | DEBUG({ |
Bill Wendling | b7a8992 | 2009-08-22 20:25:44 +0000 | [diff] [blame^] | 221 | errs() << "--- Checking if we can hoist " << I; |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 222 | if (I.getDesc().getImplicitUses()) { |
Bill Wendling | b7a8992 | 2009-08-22 20:25:44 +0000 | [diff] [blame^] | 223 | errs() << " * Instruction has implicit uses:\n"; |
Bill Wendling | 280f456 | 2007-12-18 21:38:04 +0000 | [diff] [blame] | 224 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 225 | const TargetRegisterInfo *TRI = TM->getRegisterInfo(); |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 226 | for (const unsigned *ImpUses = I.getDesc().getImplicitUses(); |
Chris Lattner | 6924430 | 2008-01-07 01:56:04 +0000 | [diff] [blame] | 227 | *ImpUses; ++ImpUses) |
Bill Wendling | b7a8992 | 2009-08-22 20:25:44 +0000 | [diff] [blame^] | 228 | errs() << " -> " << TRI->getName(*ImpUses) << "\n"; |
Bill Wendling | 280f456 | 2007-12-18 21:38:04 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 231 | if (I.getDesc().getImplicitDefs()) { |
Bill Wendling | b7a8992 | 2009-08-22 20:25:44 +0000 | [diff] [blame^] | 232 | errs() << " * Instruction has implicit defines:\n"; |
Bill Wendling | 280f456 | 2007-12-18 21:38:04 +0000 | [diff] [blame] | 233 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 234 | const TargetRegisterInfo *TRI = TM->getRegisterInfo(); |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 235 | for (const unsigned *ImpDefs = I.getDesc().getImplicitDefs(); |
Chris Lattner | 6924430 | 2008-01-07 01:56:04 +0000 | [diff] [blame] | 236 | *ImpDefs; ++ImpDefs) |
Bill Wendling | b7a8992 | 2009-08-22 20:25:44 +0000 | [diff] [blame^] | 237 | errs() << " -> " << TRI->getName(*ImpDefs) << "\n"; |
Bill Wendling | 280f456 | 2007-12-18 21:38:04 +0000 | [diff] [blame] | 238 | } |
Bill Wendling | 280f456 | 2007-12-18 21:38:04 +0000 | [diff] [blame] | 239 | }); |
| 240 | |
Bill Wendling | d3361e9 | 2008-08-18 00:33:49 +0000 | [diff] [blame] | 241 | if (I.getDesc().getImplicitDefs() || I.getDesc().getImplicitUses()) { |
Bill Wendling | b7a8992 | 2009-08-22 20:25:44 +0000 | [diff] [blame^] | 242 | DEBUG(errs() << "Cannot hoist with implicit defines or uses\n"); |
Bill Wendling | d3361e9 | 2008-08-18 00:33:49 +0000 | [diff] [blame] | 243 | return false; |
| 244 | } |
| 245 | |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 246 | // The instruction is loop invariant if all of its operands are. |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 247 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
| 248 | const MachineOperand &MO = I.getOperand(i); |
| 249 | |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 250 | if (!MO.isReg()) |
Bill Wendling | fb018d0 | 2008-08-20 20:32:05 +0000 | [diff] [blame] | 251 | continue; |
| 252 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 253 | unsigned Reg = MO.getReg(); |
| 254 | if (Reg == 0) continue; |
| 255 | |
| 256 | // Don't hoist an instruction that uses or defines a physical register. |
| 257 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) |
Bill Wendling | fb018d0 | 2008-08-20 20:32:05 +0000 | [diff] [blame] | 258 | return false; |
| 259 | |
| 260 | if (!MO.isUse()) |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 261 | continue; |
| 262 | |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 263 | assert(RegInfo->getVRegDef(Reg) && |
| 264 | "Machine instr not mapped for this vreg?!"); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 265 | |
| 266 | // If the loop contains the definition of an operand, then the instruction |
| 267 | // isn't loop invariant. |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 268 | if (CurLoop->contains(RegInfo->getVRegDef(Reg)->getParent())) |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 269 | return false; |
| 270 | } |
| 271 | |
| 272 | // If we got this far, the instruction is loop invariant! |
| 273 | return true; |
| 274 | } |
| 275 | |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 276 | |
| 277 | /// HasPHIUses - Return true if the specified register has any PHI use. |
| 278 | static bool HasPHIUses(unsigned Reg, MachineRegisterInfo *RegInfo) { |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 279 | for (MachineRegisterInfo::use_iterator UI = RegInfo->use_begin(Reg), |
| 280 | UE = RegInfo->use_end(); UI != UE; ++UI) { |
| 281 | MachineInstr *UseMI = &*UI; |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 282 | if (UseMI->getOpcode() == TargetInstrInfo::PHI) |
| 283 | return true; |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 284 | } |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 285 | return false; |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /// IsProfitableToHoist - Return true if it is potentially profitable to hoist |
| 289 | /// the given loop invariant. |
| 290 | bool MachineLICM::IsProfitableToHoist(MachineInstr &MI) { |
Evan Cheng | efc7839 | 2009-02-27 00:02:22 +0000 | [diff] [blame] | 291 | if (MI.getOpcode() == TargetInstrInfo::IMPLICIT_DEF) |
| 292 | return false; |
| 293 | |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 294 | const TargetInstrDesc &TID = MI.getDesc(); |
| 295 | |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 296 | // FIXME: For now, only hoist re-materilizable instructions. LICM will |
| 297 | // increase register pressure. We want to make sure it doesn't increase |
| 298 | // spilling. |
Evan Cheng | 5caa883 | 2009-02-04 09:21:58 +0000 | [diff] [blame] | 299 | if (!TID.mayLoad() && (!TID.isRematerializable() || |
| 300 | !TII->isTriviallyReMaterializable(&MI))) |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 301 | return false; |
| 302 | |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 303 | // If result(s) of this instruction is used by PHIs, then don't hoist it. |
| 304 | // The presence of joins makes it difficult for current register allocator |
| 305 | // implementation to perform remat. |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 306 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 307 | const MachineOperand &MO = MI.getOperand(i); |
| 308 | if (!MO.isReg() || !MO.isDef()) |
| 309 | continue; |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 310 | if (HasPHIUses(MO.getReg(), RegInfo)) |
| 311 | return false; |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 312 | } |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 313 | |
| 314 | return true; |
| 315 | } |
| 316 | |
| 317 | static const MachineInstr *LookForDuplicate(const MachineInstr *MI, |
Evan Cheng | efc7839 | 2009-02-27 00:02:22 +0000 | [diff] [blame] | 318 | std::vector<const MachineInstr*> &PrevMIs, |
| 319 | MachineRegisterInfo *RegInfo) { |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 320 | unsigned NumOps = MI->getNumOperands(); |
| 321 | for (unsigned i = 0, e = PrevMIs.size(); i != e; ++i) { |
| 322 | const MachineInstr *PrevMI = PrevMIs[i]; |
| 323 | unsigned NumOps2 = PrevMI->getNumOperands(); |
| 324 | if (NumOps != NumOps2) |
| 325 | continue; |
| 326 | bool IsSame = true; |
| 327 | for (unsigned j = 0; j != NumOps; ++j) { |
| 328 | const MachineOperand &MO = MI->getOperand(j); |
Evan Cheng | efc7839 | 2009-02-27 00:02:22 +0000 | [diff] [blame] | 329 | if (MO.isReg() && MO.isDef()) { |
| 330 | if (RegInfo->getRegClass(MO.getReg()) != |
| 331 | RegInfo->getRegClass(PrevMI->getOperand(j).getReg())) { |
| 332 | IsSame = false; |
| 333 | break; |
| 334 | } |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 335 | continue; |
Evan Cheng | efc7839 | 2009-02-27 00:02:22 +0000 | [diff] [blame] | 336 | } |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 337 | if (!MO.isIdenticalTo(PrevMI->getOperand(j))) { |
| 338 | IsSame = false; |
| 339 | break; |
| 340 | } |
| 341 | } |
| 342 | if (IsSame) |
| 343 | return PrevMI; |
| 344 | } |
| 345 | return 0; |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 348 | /// Hoist - When an instruction is found to use only loop invariant operands |
| 349 | /// 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] | 350 | /// |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 351 | void MachineLICM::Hoist(MachineInstr &MI) { |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 352 | if (!IsLoopInvariantInst(MI)) return; |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 353 | if (!IsProfitableToHoist(MI)) return; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 354 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 355 | // Now move the instructions to the predecessor, inserting it before any |
| 356 | // terminator instructions. |
| 357 | DEBUG({ |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 358 | errs() << "Hoisting " << MI; |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 359 | if (CurPreheader->getBasicBlock()) |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 360 | errs() << " to MachineBasicBlock " |
| 361 | << CurPreheader->getBasicBlock()->getName(); |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 362 | if (MI.getParent()->getBasicBlock()) |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 363 | errs() << " from MachineBasicBlock " |
| 364 | << MI.getParent()->getBasicBlock()->getName(); |
| 365 | errs() << "\n"; |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 366 | }); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 367 | |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 368 | // Look for opportunity to CSE the hoisted instruction. |
| 369 | std::pair<unsigned, unsigned> BBOpcPair = |
| 370 | std::make_pair(CurPreheader->getNumber(), MI.getOpcode()); |
| 371 | DenseMap<std::pair<unsigned, unsigned>, |
| 372 | std::vector<const MachineInstr*> >::iterator CI = CSEMap.find(BBOpcPair); |
| 373 | bool DoneCSE = false; |
| 374 | if (CI != CSEMap.end()) { |
Evan Cheng | efc7839 | 2009-02-27 00:02:22 +0000 | [diff] [blame] | 375 | const MachineInstr *Dup = LookForDuplicate(&MI, CI->second, RegInfo); |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 376 | if (Dup) { |
Bill Wendling | b7a8992 | 2009-08-22 20:25:44 +0000 | [diff] [blame^] | 377 | DEBUG(errs() << "CSEing " << MI << " with " << *Dup); |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 378 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 379 | const MachineOperand &MO = MI.getOperand(i); |
| 380 | if (MO.isReg() && MO.isDef()) |
| 381 | RegInfo->replaceRegWith(MO.getReg(), Dup->getOperand(i).getReg()); |
| 382 | } |
| 383 | MI.eraseFromParent(); |
| 384 | DoneCSE = true; |
| 385 | ++NumCSEed; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | // Otherwise, splice the instruction to the preheader. |
| 390 | if (!DoneCSE) { |
| 391 | CurPreheader->splice(CurPreheader->getFirstTerminator(), |
| 392 | MI.getParent(), &MI); |
| 393 | // Add to the CSE map. |
| 394 | if (CI != CSEMap.end()) |
| 395 | CI->second.push_back(&MI); |
| 396 | else { |
| 397 | std::vector<const MachineInstr*> CSEMIs; |
| 398 | CSEMIs.push_back(&MI); |
| 399 | CSEMap.insert(std::make_pair(BBOpcPair, CSEMIs)); |
| 400 | } |
| 401 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 402 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 403 | ++NumHoisted; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 404 | Changed = true; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 405 | } |