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