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" |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineMemOperand.h" |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetRegisterInfo.h" |
Bill Wendling | efe2be7 | 2007-12-11 23:27:51 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetInstrInfo.h" |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetMachine.h" |
Dan Gohman | e33f44c | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 34 | #include "llvm/Analysis/AliasAnalysis.h" |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/DenseMap.h" |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/SmallSet.h" |
Chris Lattner | ac69582 | 2008-01-04 06:41:45 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | ac69582 | 2008-01-04 06:41:45 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 39 | #include "llvm/Support/raw_ostream.h" |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 40 | |
| 41 | using namespace llvm; |
| 42 | |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 43 | STATISTIC(NumHoisted, "Number of machine instructions hoisted out of loops"); |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 44 | STATISTIC(NumCSEed, "Number of hoisted machine instructions CSEed"); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 45 | STATISTIC(NumPostRAHoisted, |
| 46 | "Number of machine instructions hoisted out of loops post regalloc"); |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 47 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 48 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 49 | class MachineLICM : public MachineFunctionPass { |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 50 | bool PreRegAlloc; |
| 51 | |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 52 | const TargetMachine *TM; |
Bill Wendling | efe2be7 | 2007-12-11 23:27:51 +0000 | [diff] [blame] | 53 | const TargetInstrInfo *TII; |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 54 | const TargetRegisterInfo *TRI; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 55 | const MachineFrameInfo *MFI; |
| 56 | MachineRegisterInfo *RegInfo; |
Bill Wendling | 12ebf14 | 2007-12-11 19:40:06 +0000 | [diff] [blame] | 57 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 58 | // Various analyses that we use... |
Dan Gohman | e33f44c | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 59 | AliasAnalysis *AA; // Alias analysis info. |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame^] | 60 | MachineLoopInfo *MLI; // Current MachineLoopInfo |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 61 | MachineDominatorTree *DT; // Machine dominator tree for the cur loop |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 62 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 63 | // State that is updated as we process loops |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 64 | bool Changed; // True if a loop is changed. |
| 65 | MachineLoop *CurLoop; // The current loop we are working on. |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 66 | MachineBasicBlock *CurPreheader; // The preheader for CurLoop. |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 67 | |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 68 | BitVector AllocatableSet; |
| 69 | |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 70 | // For each opcode, keep a list of potentail CSE instructions. |
| 71 | DenseMap<unsigned, std::vector<const MachineInstr*> > CSEMap; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 72 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 73 | public: |
| 74 | static char ID; // Pass identification, replacement for typeid |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 75 | MachineLICM() : |
| 76 | MachineFunctionPass(&ID), PreRegAlloc(true) {} |
| 77 | |
| 78 | explicit MachineLICM(bool PreRA) : |
| 79 | MachineFunctionPass(&ID), PreRegAlloc(PreRA) {} |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 80 | |
| 81 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 82 | |
Dan Gohman | 7224170 | 2008-12-18 01:37:56 +0000 | [diff] [blame] | 83 | const char *getPassName() const { return "Machine Instruction LICM"; } |
| 84 | |
Bill Wendling | 074223a | 2008-03-10 08:13:01 +0000 | [diff] [blame] | 85 | // FIXME: Loop preheaders? |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 86 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 87 | AU.setPreservesCFG(); |
| 88 | AU.addRequired<MachineLoopInfo>(); |
| 89 | AU.addRequired<MachineDominatorTree>(); |
Dan Gohman | e33f44c | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 90 | AU.addRequired<AliasAnalysis>(); |
Bill Wendling | d5da704 | 2008-01-04 08:48:49 +0000 | [diff] [blame] | 91 | AU.addPreserved<MachineLoopInfo>(); |
| 92 | AU.addPreserved<MachineDominatorTree>(); |
| 93 | MachineFunctionPass::getAnalysisUsage(AU); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 94 | } |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 95 | |
| 96 | virtual void releaseMemory() { |
| 97 | CSEMap.clear(); |
| 98 | } |
| 99 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 100 | private: |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame^] | 101 | /// CandidateInfo - Keep track of information about hoisting candidates. |
| 102 | struct CandidateInfo { |
| 103 | MachineInstr *MI; |
| 104 | int FI; |
| 105 | unsigned Def; |
| 106 | CandidateInfo(MachineInstr *mi, int fi, unsigned def) |
| 107 | : MI(mi), FI(fi), Def(def) {} |
| 108 | }; |
| 109 | |
| 110 | /// HoistRegionPostRA - Walk the specified region of the CFG and hoist loop |
| 111 | /// invariants out to the preheader. |
| 112 | void HoistRegionPostRA(MachineDomTreeNode *N); |
| 113 | |
| 114 | /// HoistPostRA - When an instruction is found to only use loop invariant |
| 115 | /// operands that is safe to hoist, this instruction is called to do the |
| 116 | /// dirty work. |
| 117 | void HoistPostRA(MachineInstr *MI, unsigned Def); |
| 118 | |
| 119 | /// ProcessMI - Examine the instruction for potentai LICM candidate. Also |
| 120 | /// gather register def and frame object update information. |
| 121 | void ProcessMI(MachineInstr *MI, unsigned *PhysRegDefs, |
| 122 | SmallSet<int, 32> &StoredFIs, |
| 123 | SmallVector<CandidateInfo, 32> &Candidates); |
| 124 | |
| 125 | /// AddToLiveIns - Add 'Reg' to the livein sets of BBs in the backedge path |
| 126 | /// from MBB to LoopHeader (inclusive). |
| 127 | void AddToLiveIns(unsigned Reg, |
| 128 | MachineBasicBlock *MBB, MachineBasicBlock *LoopHeader); |
| 129 | |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 130 | /// IsLoopInvariantInst - Returns true if the instruction is loop |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 131 | /// invariant. I.e., all virtual register operands are defined outside of |
| 132 | /// the loop, physical registers aren't accessed (explicitly or implicitly), |
| 133 | /// and the instruction is hoistable. |
| 134 | /// |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 135 | bool IsLoopInvariantInst(MachineInstr &I); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 136 | |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 137 | /// IsProfitableToHoist - Return true if it is potentially profitable to |
| 138 | /// hoist the given loop invariant. |
Evan Cheng | c26abd9 | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 139 | bool IsProfitableToHoist(MachineInstr &MI); |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 140 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 141 | /// HoistRegion - Walk the specified region of the CFG (defined by all |
| 142 | /// blocks dominated by the specified block, and that are in the current |
| 143 | /// loop) in depth first order w.r.t the DominatorTree. This allows us to |
| 144 | /// visit definitions before uses, allowing us to hoist a loop body in one |
| 145 | /// pass without iteration. |
| 146 | /// |
| 147 | void HoistRegion(MachineDomTreeNode *N); |
| 148 | |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 149 | /// isLoadFromConstantMemory - Return true if the given instruction is a |
| 150 | /// load from constant memory. |
| 151 | bool isLoadFromConstantMemory(MachineInstr *MI); |
| 152 | |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 153 | /// ExtractHoistableLoad - Unfold a load from the given machineinstr if |
| 154 | /// the load itself could be hoisted. Return the unfolded and hoistable |
| 155 | /// load, or null if the load couldn't be unfolded or if it wouldn't |
| 156 | /// be hoistable. |
| 157 | MachineInstr *ExtractHoistableLoad(MachineInstr *MI); |
| 158 | |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 159 | /// LookForDuplicate - Find an instruction amount PrevMIs that is a |
| 160 | /// duplicate of MI. Return this instruction if it's found. |
| 161 | const MachineInstr *LookForDuplicate(const MachineInstr *MI, |
| 162 | std::vector<const MachineInstr*> &PrevMIs); |
| 163 | |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 164 | /// EliminateCSE - Given a LICM'ed instruction, look for an instruction on |
| 165 | /// the preheader that compute the same value. If it's found, do a RAU on |
| 166 | /// with the definition of the existing instruction rather than hoisting |
| 167 | /// the instruction to the preheader. |
| 168 | bool EliminateCSE(MachineInstr *MI, |
| 169 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator &CI); |
| 170 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 171 | /// Hoist - When an instruction is found to only use loop invariant operands |
| 172 | /// that is safe to hoist, this instruction is called to do the dirty work. |
| 173 | /// |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 174 | void Hoist(MachineInstr *MI); |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 175 | |
| 176 | /// InitCSEMap - Initialize the CSE map with instructions that are in the |
| 177 | /// current loop preheader that may become duplicates of instructions that |
| 178 | /// are hoisted out of the loop. |
| 179 | void InitCSEMap(MachineBasicBlock *BB); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 180 | }; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 181 | } // end anonymous namespace |
| 182 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 183 | char MachineLICM::ID = 0; |
| 184 | static RegisterPass<MachineLICM> |
Bill Wendling | 8870ce9 | 2008-07-07 05:42:27 +0000 | [diff] [blame] | 185 | X("machinelicm", "Machine Loop Invariant Code Motion"); |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 186 | |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 187 | FunctionPass *llvm::createMachineLICMPass(bool PreRegAlloc) { |
| 188 | return new MachineLICM(PreRegAlloc); |
| 189 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 190 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 191 | /// LoopIsOuterMostWithPreheader - Test if the given loop is the outer-most |
| 192 | /// loop that has a preheader. |
| 193 | static bool LoopIsOuterMostWithPreheader(MachineLoop *CurLoop) { |
| 194 | for (MachineLoop *L = CurLoop->getParentLoop(); L; L = L->getParentLoop()) |
| 195 | if (L->getLoopPreheader()) |
| 196 | return false; |
| 197 | return true; |
| 198 | } |
| 199 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 200 | /// Hoist expressions out of the specified loop. Note, alias info for inner loop |
| 201 | /// is not preserved so it is not a good idea to run LICM multiple times on one |
| 202 | /// loop. |
| 203 | /// |
| 204 | bool MachineLICM::runOnMachineFunction(MachineFunction &MF) { |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 205 | if (PreRegAlloc) |
| 206 | DEBUG(dbgs() << "******** Pre-regalloc Machine LICM ********\n"); |
| 207 | else |
| 208 | DEBUG(dbgs() << "******** Post-regalloc Machine LICM ********\n"); |
Bill Wendling | a17ad59 | 2007-12-11 22:22:22 +0000 | [diff] [blame] | 209 | |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame^] | 210 | Changed = false; |
Bill Wendling | acb04ec | 2008-08-31 02:30:23 +0000 | [diff] [blame] | 211 | TM = &MF.getTarget(); |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 212 | TII = TM->getInstrInfo(); |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 213 | TRI = TM->getRegisterInfo(); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 214 | MFI = MF.getFrameInfo(); |
Bill Wendling | acb04ec | 2008-08-31 02:30:23 +0000 | [diff] [blame] | 215 | RegInfo = &MF.getRegInfo(); |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 216 | AllocatableSet = TRI->getAllocatableSet(MF); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 217 | |
| 218 | // Get our Loop information... |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame^] | 219 | MLI = &getAnalysis<MachineLoopInfo>(); |
| 220 | DT = &getAnalysis<MachineDominatorTree>(); |
| 221 | AA = &getAnalysis<AliasAnalysis>(); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 222 | |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame^] | 223 | for (MachineLoopInfo::iterator I = MLI->begin(), E = MLI->end(); I != E; ++I){ |
Bill Wendling | a17ad59 | 2007-12-11 22:22:22 +0000 | [diff] [blame] | 224 | CurLoop = *I; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 225 | |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame^] | 226 | // If this is done before regalloc, only visit outer-most preheader-sporting |
| 227 | // loops. |
| 228 | if (PreRegAlloc && !LoopIsOuterMostWithPreheader(CurLoop)) |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 229 | continue; |
| 230 | |
| 231 | // Determine the block to which to hoist instructions. If we can't find a |
| 232 | // suitable loop preheader, we can't do any hoisting. |
| 233 | // |
| 234 | // FIXME: We are only hoisting if the basic block coming into this loop |
| 235 | // has only one successor. This isn't the case in general because we haven't |
| 236 | // broken critical edges or added preheaders. |
| 237 | CurPreheader = CurLoop->getLoopPreheader(); |
| 238 | if (!CurPreheader) |
| 239 | continue; |
| 240 | |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 241 | // CSEMap is initialized for loop header when the first instruction is |
| 242 | // being hoisted. |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 243 | MachineDomTreeNode *N = DT->getNode(CurLoop->getHeader()); |
| 244 | if (!PreRegAlloc) |
| 245 | HoistRegionPostRA(N); |
| 246 | else { |
| 247 | HoistRegion(N); |
| 248 | CSEMap.clear(); |
| 249 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | return Changed; |
| 253 | } |
| 254 | |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame^] | 255 | /// InstructionStoresToFI - Return true if instruction stores to the |
| 256 | /// specified frame. |
| 257 | static bool InstructionStoresToFI(const MachineInstr *MI, int FI) { |
| 258 | for (MachineInstr::mmo_iterator o = MI->memoperands_begin(), |
| 259 | oe = MI->memoperands_end(); o != oe; ++o) { |
| 260 | if (!(*o)->isStore() || !(*o)->getValue()) |
| 261 | continue; |
| 262 | if (const FixedStackPseudoSourceValue *Value = |
| 263 | dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) { |
| 264 | if (Value->getFrameIndex() == FI) |
| 265 | return true; |
| 266 | } |
| 267 | } |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | /// ProcessMI - Examine the instruction for potentai LICM candidate. Also |
| 272 | /// gather register def and frame object update information. |
| 273 | void MachineLICM::ProcessMI(MachineInstr *MI, |
| 274 | unsigned *PhysRegDefs, |
| 275 | SmallSet<int, 32> &StoredFIs, |
| 276 | SmallVector<CandidateInfo, 32> &Candidates) { |
| 277 | bool RuledOut = false; |
| 278 | unsigned Def = 0; |
| 279 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 280 | const MachineOperand &MO = MI->getOperand(i); |
| 281 | if (MO.isFI()) { |
| 282 | // Remember if the instruction stores to the frame index. |
| 283 | int FI = MO.getIndex(); |
| 284 | if (!StoredFIs.count(FI) && |
| 285 | MFI->isSpillSlotObjectIndex(FI) && |
| 286 | InstructionStoresToFI(MI, FI)) |
| 287 | StoredFIs.insert(FI); |
| 288 | continue; |
| 289 | } |
| 290 | |
| 291 | if (!MO.isReg()) |
| 292 | continue; |
| 293 | unsigned Reg = MO.getReg(); |
| 294 | if (!Reg) |
| 295 | continue; |
| 296 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && |
| 297 | "Not expecting virtual register!"); |
| 298 | |
| 299 | if (!MO.isDef()) |
| 300 | continue; |
| 301 | |
| 302 | if (MO.isImplicit()) { |
| 303 | ++PhysRegDefs[Reg]; |
| 304 | for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) |
| 305 | ++PhysRegDefs[*AS]; |
| 306 | if (!MO.isDead()) |
| 307 | // Non-dead implicit def? This cannot be hoisted. |
| 308 | RuledOut = true; |
| 309 | // No need to check if a dead implicit def is also defined by |
| 310 | // another instruction. |
| 311 | continue; |
| 312 | } |
| 313 | |
| 314 | // FIXME: For now, avoid instructions with multiple defs, unless |
| 315 | // it's a dead implicit def. |
| 316 | if (Def) |
| 317 | RuledOut = true; |
| 318 | else |
| 319 | Def = Reg; |
| 320 | |
| 321 | // If we have already seen another instruction that defines the same |
| 322 | // register, then this is not safe. |
| 323 | if (++PhysRegDefs[Reg] > 1) |
| 324 | // MI defined register is seen defined by another instruction in |
| 325 | // the loop, it cannot be a LICM candidate. |
| 326 | RuledOut = true; |
| 327 | for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) |
| 328 | if (++PhysRegDefs[*AS] > 1) |
| 329 | RuledOut = true; |
| 330 | } |
| 331 | |
| 332 | // FIXME: Only consider reloads for now. We should be able to handle |
| 333 | // remats which does not have register operands. |
| 334 | if (Def && !RuledOut) { |
| 335 | int FI; |
| 336 | if (TII->isLoadFromStackSlot(MI, FI) && |
| 337 | MFI->isSpillSlotObjectIndex(FI)) |
| 338 | Candidates.push_back(CandidateInfo(MI, FI, Def)); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /// HoistRegionPostRA - Walk the specified region of the CFG and hoist loop |
| 343 | /// invariants out to the preheader. |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 344 | void MachineLICM::HoistRegionPostRA(MachineDomTreeNode *N) { |
| 345 | assert(N != 0 && "Null dominator tree node?"); |
| 346 | |
| 347 | unsigned NumRegs = TRI->getNumRegs(); |
| 348 | unsigned *PhysRegDefs = new unsigned[NumRegs]; |
| 349 | std::fill(PhysRegDefs, PhysRegDefs + NumRegs, 0); |
| 350 | |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame^] | 351 | SmallVector<CandidateInfo, 32> Candidates; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 352 | SmallSet<int, 32> StoredFIs; |
| 353 | |
| 354 | // Walk the entire region, count number of defs for each register, and |
| 355 | // return potential LICM candidates. |
| 356 | SmallVector<MachineDomTreeNode*, 8> WorkList; |
| 357 | WorkList.push_back(N); |
| 358 | do { |
| 359 | N = WorkList.pop_back_val(); |
| 360 | MachineBasicBlock *BB = N->getBlock(); |
| 361 | |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame^] | 362 | if (!CurLoop->contains(MLI->getLoopFor(BB))) |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 363 | continue; |
| 364 | // Conservatively treat live-in's as an external def. |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame^] | 365 | // FIXME: That means a reload that're reused in successor block(s) will not |
| 366 | // be LICM'ed. |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 367 | for (MachineBasicBlock::const_livein_iterator I = BB->livein_begin(), |
| 368 | E = BB->livein_end(); I != E; ++I) { |
| 369 | unsigned Reg = *I; |
| 370 | ++PhysRegDefs[Reg]; |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame^] | 371 | for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) |
| 372 | ++PhysRegDefs[*AS]; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | for (MachineBasicBlock::iterator |
| 376 | MII = BB->begin(), E = BB->end(); MII != E; ++MII) { |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 377 | MachineInstr *MI = &*MII; |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame^] | 378 | ProcessMI(MI, PhysRegDefs, StoredFIs, Candidates); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | const std::vector<MachineDomTreeNode*> &Children = N->getChildren(); |
| 382 | for (unsigned I = 0, E = Children.size(); I != E; ++I) |
| 383 | WorkList.push_back(Children[I]); |
| 384 | } while (!WorkList.empty()); |
| 385 | |
| 386 | // Now evaluate whether the potential candidates qualify. |
| 387 | // 1. Check if the candidate defined register is defined by another |
| 388 | // instruction in the loop. |
| 389 | // 2. If the candidate is a load from stack slot (always true for now), |
| 390 | // check if the slot is stored anywhere in the loop. |
| 391 | for (unsigned i = 0, e = Candidates.size(); i != e; ++i) { |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame^] | 392 | if (StoredFIs.count(Candidates[i].FI)) |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 393 | continue; |
| 394 | |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame^] | 395 | if (PhysRegDefs[Candidates[i].Def] == 1) |
| 396 | HoistPostRA(Candidates[i].MI, Candidates[i].Def); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame^] | 400 | /// AddToLiveIns - Add register 'Reg' to the livein sets of BBs in the |
| 401 | /// backedge path from MBB to LoopHeader. |
| 402 | void MachineLICM::AddToLiveIns(unsigned Reg, MachineBasicBlock *MBB, |
| 403 | MachineBasicBlock *LoopHeader) { |
| 404 | SmallPtrSet<MachineBasicBlock*, 4> Visited; |
| 405 | SmallVector<MachineBasicBlock*, 4> WorkList; |
| 406 | WorkList.push_back(MBB); |
| 407 | do { |
| 408 | MBB = WorkList.pop_back_val(); |
| 409 | if (!Visited.insert(MBB)) |
| 410 | continue; |
| 411 | MBB->addLiveIn(Reg); |
| 412 | if (MBB == LoopHeader) |
| 413 | continue; |
| 414 | for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), |
| 415 | E = MBB->pred_end(); PI != E; ++PI) |
| 416 | WorkList.push_back(*PI); |
| 417 | } while (!WorkList.empty()); |
| 418 | } |
| 419 | |
| 420 | /// HoistPostRA - When an instruction is found to only use loop invariant |
| 421 | /// operands that is safe to hoist, this instruction is called to do the |
| 422 | /// dirty work. |
| 423 | void MachineLICM::HoistPostRA(MachineInstr *MI, unsigned Def) { |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 424 | // Now move the instructions to the predecessor, inserting it before any |
| 425 | // terminator instructions. |
| 426 | DEBUG({ |
| 427 | dbgs() << "Hoisting " << *MI; |
| 428 | if (CurPreheader->getBasicBlock()) |
| 429 | dbgs() << " to MachineBasicBlock " |
| 430 | << CurPreheader->getName(); |
| 431 | if (MI->getParent()->getBasicBlock()) |
| 432 | dbgs() << " from MachineBasicBlock " |
| 433 | << MI->getParent()->getName(); |
| 434 | dbgs() << "\n"; |
| 435 | }); |
| 436 | |
| 437 | // Splice the instruction to the preheader. |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame^] | 438 | MachineBasicBlock *MBB = MI->getParent(); |
| 439 | CurPreheader->splice(CurPreheader->getFirstTerminator(), MBB, MI); |
| 440 | |
| 441 | // Add register to livein list to BBs in the path from loop header to original |
| 442 | // BB. Note, currently it's not necessary to worry about adding it to all BB's |
| 443 | // with uses. Reload that're reused in successor block(s) are not being |
| 444 | // hoisted. |
| 445 | AddToLiveIns(Def, MBB, CurLoop->getHeader()); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 446 | |
| 447 | ++NumPostRAHoisted; |
| 448 | Changed = true; |
| 449 | } |
| 450 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 451 | /// HoistRegion - Walk the specified region of the CFG (defined by all blocks |
| 452 | /// dominated by the specified block, and that are in the current loop) in depth |
| 453 | /// first order w.r.t the DominatorTree. This allows us to visit definitions |
| 454 | /// before uses, allowing us to hoist a loop body in one pass without iteration. |
| 455 | /// |
| 456 | void MachineLICM::HoistRegion(MachineDomTreeNode *N) { |
| 457 | assert(N != 0 && "Null dominator tree node?"); |
| 458 | MachineBasicBlock *BB = N->getBlock(); |
| 459 | |
| 460 | // If this subregion is not in the top level loop at all, exit. |
| 461 | if (!CurLoop->contains(BB)) return; |
| 462 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 463 | for (MachineBasicBlock::iterator |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 464 | MII = BB->begin(), E = BB->end(); MII != E; ) { |
| 465 | MachineBasicBlock::iterator NextMII = MII; ++NextMII; |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 466 | Hoist(&*MII); |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 467 | MII = NextMII; |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 468 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 469 | |
| 470 | const std::vector<MachineDomTreeNode*> &Children = N->getChildren(); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 471 | for (unsigned I = 0, E = Children.size(); I != E; ++I) |
| 472 | HoistRegion(Children[I]); |
| 473 | } |
| 474 | |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 475 | /// IsLoopInvariantInst - Returns true if the instruction is loop |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 476 | /// invariant. I.e., all virtual register operands are defined outside of the |
Bill Wendling | 60ff1a3 | 2007-12-20 01:08:10 +0000 | [diff] [blame] | 477 | /// loop, physical registers aren't accessed explicitly, and there are no side |
| 478 | /// effects that aren't captured by the operands or other flags. |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 479 | /// |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 480 | bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) { |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 481 | const TargetInstrDesc &TID = I.getDesc(); |
| 482 | |
| 483 | // Ignore stuff that we obviously can't hoist. |
Dan Gohman | 237dee1 | 2008-12-23 17:28:50 +0000 | [diff] [blame] | 484 | if (TID.mayStore() || TID.isCall() || TID.isTerminator() || |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 485 | TID.hasUnmodeledSideEffects()) |
| 486 | return false; |
Evan Cheng | 9b61f33 | 2009-02-04 07:17:49 +0000 | [diff] [blame] | 487 | |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 488 | if (TID.mayLoad()) { |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 489 | // Okay, this instruction does a load. As a refinement, we allow the target |
| 490 | // to decide whether the loaded value is actually a constant. If so, we can |
| 491 | // actually use it as a load. |
Dan Gohman | e33f44c | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 492 | if (!I.isInvariantLoad(AA)) |
Evan Cheng | 7adcdc3 | 2009-11-17 19:19:01 +0000 | [diff] [blame] | 493 | // FIXME: we should be able to hoist loads with no other side effects if |
| 494 | // there are no other instructions which can change memory in this loop. |
| 495 | // This is a trivial form of alias analysis. |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 496 | return false; |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 497 | } |
Bill Wendling | 074223a | 2008-03-10 08:13:01 +0000 | [diff] [blame] | 498 | |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 499 | // The instruction is loop invariant if all of its operands are. |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 500 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
| 501 | const MachineOperand &MO = I.getOperand(i); |
| 502 | |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 503 | if (!MO.isReg()) |
Bill Wendling | fb018d0 | 2008-08-20 20:32:05 +0000 | [diff] [blame] | 504 | continue; |
| 505 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 506 | unsigned Reg = MO.getReg(); |
| 507 | if (Reg == 0) continue; |
| 508 | |
| 509 | // Don't hoist an instruction that uses or defines a physical register. |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 510 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 511 | if (MO.isUse()) { |
| 512 | // If the physreg has no defs anywhere, it's just an ambient register |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 513 | // and we can freely move its uses. Alternatively, if it's allocatable, |
| 514 | // it could get allocated to something with a def during allocation. |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 515 | if (!RegInfo->def_empty(Reg)) |
| 516 | return false; |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 517 | if (AllocatableSet.test(Reg)) |
| 518 | return false; |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 519 | // Check for a def among the register's aliases too. |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 520 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 521 | unsigned AliasReg = *Alias; |
| 522 | if (!RegInfo->def_empty(AliasReg)) |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 523 | return false; |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 524 | if (AllocatableSet.test(AliasReg)) |
| 525 | return false; |
| 526 | } |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 527 | // Otherwise it's safe to move. |
| 528 | continue; |
| 529 | } else if (!MO.isDead()) { |
| 530 | // A def that isn't dead. We can't move it. |
| 531 | return false; |
Dan Gohman | a363a9b | 2010-02-28 00:08:44 +0000 | [diff] [blame] | 532 | } else if (CurLoop->getHeader()->isLiveIn(Reg)) { |
| 533 | // If the reg is live into the loop, we can't hoist an instruction |
| 534 | // which would clobber it. |
| 535 | return false; |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 536 | } |
| 537 | } |
Bill Wendling | fb018d0 | 2008-08-20 20:32:05 +0000 | [diff] [blame] | 538 | |
| 539 | if (!MO.isUse()) |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 540 | continue; |
| 541 | |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 542 | assert(RegInfo->getVRegDef(Reg) && |
| 543 | "Machine instr not mapped for this vreg?!"); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 544 | |
| 545 | // If the loop contains the definition of an operand, then the instruction |
| 546 | // isn't loop invariant. |
Dan Gohman | 92329c7 | 2009-12-18 01:24:09 +0000 | [diff] [blame] | 547 | if (CurLoop->contains(RegInfo->getVRegDef(Reg))) |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 548 | return false; |
| 549 | } |
| 550 | |
| 551 | // If we got this far, the instruction is loop invariant! |
| 552 | return true; |
| 553 | } |
| 554 | |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 555 | |
| 556 | /// HasPHIUses - Return true if the specified register has any PHI use. |
| 557 | static bool HasPHIUses(unsigned Reg, MachineRegisterInfo *RegInfo) { |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 558 | for (MachineRegisterInfo::use_iterator UI = RegInfo->use_begin(Reg), |
| 559 | UE = RegInfo->use_end(); UI != UE; ++UI) { |
| 560 | MachineInstr *UseMI = &*UI; |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 561 | if (UseMI->isPHI()) |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 562 | return true; |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 563 | } |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 564 | return false; |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 567 | /// isLoadFromConstantMemory - Return true if the given instruction is a |
| 568 | /// load from constant memory. Machine LICM will hoist these even if they are |
| 569 | /// not re-materializable. |
| 570 | bool MachineLICM::isLoadFromConstantMemory(MachineInstr *MI) { |
| 571 | if (!MI->getDesc().mayLoad()) return false; |
| 572 | if (!MI->hasOneMemOperand()) return false; |
| 573 | MachineMemOperand *MMO = *MI->memoperands_begin(); |
| 574 | if (MMO->isVolatile()) return false; |
| 575 | if (!MMO->getValue()) return false; |
| 576 | const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(MMO->getValue()); |
| 577 | if (PSV) { |
| 578 | MachineFunction &MF = *MI->getParent()->getParent(); |
| 579 | return PSV->isConstant(MF.getFrameInfo()); |
| 580 | } else { |
| 581 | return AA->pointsToConstantMemory(MMO->getValue()); |
| 582 | } |
| 583 | } |
| 584 | |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 585 | /// IsProfitableToHoist - Return true if it is potentially profitable to hoist |
| 586 | /// the given loop invariant. |
Evan Cheng | c26abd9 | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 587 | bool MachineLICM::IsProfitableToHoist(MachineInstr &MI) { |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 588 | if (MI.isImplicitDef()) |
Evan Cheng | efc7839 | 2009-02-27 00:02:22 +0000 | [diff] [blame] | 589 | return false; |
| 590 | |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 591 | // FIXME: For now, only hoist re-materilizable instructions. LICM will |
| 592 | // increase register pressure. We want to make sure it doesn't increase |
| 593 | // spilling. |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 594 | // Also hoist loads from constant memory, e.g. load from stubs, GOT. Hoisting |
| 595 | // these tend to help performance in low register pressure situation. The |
| 596 | // trade off is it may cause spill in high pressure situation. It will end up |
| 597 | // adding a store in the loop preheader. But the reload is no more expensive. |
| 598 | // The side benefit is these loads are frequently CSE'ed. |
| 599 | if (!TII->isTriviallyReMaterializable(&MI, AA)) { |
Evan Cheng | c26abd9 | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 600 | if (!isLoadFromConstantMemory(&MI)) |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 601 | return false; |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 602 | } |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 603 | |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 604 | // If result(s) of this instruction is used by PHIs, then don't hoist it. |
| 605 | // The presence of joins makes it difficult for current register allocator |
| 606 | // implementation to perform remat. |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 607 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 608 | const MachineOperand &MO = MI.getOperand(i); |
| 609 | if (!MO.isReg() || !MO.isDef()) |
| 610 | continue; |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 611 | if (HasPHIUses(MO.getReg(), RegInfo)) |
| 612 | return false; |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 613 | } |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 614 | |
| 615 | return true; |
| 616 | } |
| 617 | |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 618 | MachineInstr *MachineLICM::ExtractHoistableLoad(MachineInstr *MI) { |
| 619 | // If not, we may be able to unfold a load and hoist that. |
| 620 | // First test whether the instruction is loading from an amenable |
| 621 | // memory location. |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 622 | if (!isLoadFromConstantMemory(MI)) |
| 623 | return 0; |
| 624 | |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 625 | // Next determine the register class for a temporary register. |
Dan Gohman | 0115e16 | 2009-10-30 22:18:41 +0000 | [diff] [blame] | 626 | unsigned LoadRegIndex; |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 627 | unsigned NewOpc = |
| 628 | TII->getOpcodeAfterMemoryUnfold(MI->getOpcode(), |
| 629 | /*UnfoldLoad=*/true, |
Dan Gohman | 0115e16 | 2009-10-30 22:18:41 +0000 | [diff] [blame] | 630 | /*UnfoldStore=*/false, |
| 631 | &LoadRegIndex); |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 632 | if (NewOpc == 0) return 0; |
| 633 | const TargetInstrDesc &TID = TII->get(NewOpc); |
| 634 | if (TID.getNumDefs() != 1) return 0; |
Dan Gohman | 0115e16 | 2009-10-30 22:18:41 +0000 | [diff] [blame] | 635 | const TargetRegisterClass *RC = TID.OpInfo[LoadRegIndex].getRegClass(TRI); |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 636 | // Ok, we're unfolding. Create a temporary register and do the unfold. |
| 637 | unsigned Reg = RegInfo->createVirtualRegister(RC); |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 638 | |
| 639 | MachineFunction &MF = *MI->getParent()->getParent(); |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 640 | SmallVector<MachineInstr *, 2> NewMIs; |
| 641 | bool Success = |
| 642 | TII->unfoldMemoryOperand(MF, MI, Reg, |
| 643 | /*UnfoldLoad=*/true, /*UnfoldStore=*/false, |
| 644 | NewMIs); |
| 645 | (void)Success; |
| 646 | assert(Success && |
| 647 | "unfoldMemoryOperand failed when getOpcodeAfterMemoryUnfold " |
| 648 | "succeeded!"); |
| 649 | assert(NewMIs.size() == 2 && |
| 650 | "Unfolded a load into multiple instructions!"); |
| 651 | MachineBasicBlock *MBB = MI->getParent(); |
| 652 | MBB->insert(MI, NewMIs[0]); |
| 653 | MBB->insert(MI, NewMIs[1]); |
| 654 | // If unfolding produced a load that wasn't loop-invariant or profitable to |
| 655 | // hoist, discard the new instructions and bail. |
Evan Cheng | c26abd9 | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 656 | if (!IsLoopInvariantInst(*NewMIs[0]) || !IsProfitableToHoist(*NewMIs[0])) { |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 657 | NewMIs[0]->eraseFromParent(); |
| 658 | NewMIs[1]->eraseFromParent(); |
| 659 | return 0; |
| 660 | } |
| 661 | // Otherwise we successfully unfolded a load that we can hoist. |
| 662 | MI->eraseFromParent(); |
| 663 | return NewMIs[0]; |
| 664 | } |
| 665 | |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 666 | void MachineLICM::InitCSEMap(MachineBasicBlock *BB) { |
| 667 | for (MachineBasicBlock::iterator I = BB->begin(),E = BB->end(); I != E; ++I) { |
| 668 | const MachineInstr *MI = &*I; |
| 669 | // FIXME: For now, only hoist re-materilizable instructions. LICM will |
| 670 | // increase register pressure. We want to make sure it doesn't increase |
| 671 | // spilling. |
| 672 | if (TII->isTriviallyReMaterializable(MI, AA)) { |
| 673 | unsigned Opcode = MI->getOpcode(); |
| 674 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator |
| 675 | CI = CSEMap.find(Opcode); |
| 676 | if (CI != CSEMap.end()) |
| 677 | CI->second.push_back(MI); |
| 678 | else { |
| 679 | std::vector<const MachineInstr*> CSEMIs; |
| 680 | CSEMIs.push_back(MI); |
| 681 | CSEMap.insert(std::make_pair(Opcode, CSEMIs)); |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 687 | const MachineInstr* |
| 688 | MachineLICM::LookForDuplicate(const MachineInstr *MI, |
| 689 | std::vector<const MachineInstr*> &PrevMIs) { |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 690 | for (unsigned i = 0, e = PrevMIs.size(); i != e; ++i) { |
| 691 | const MachineInstr *PrevMI = PrevMIs[i]; |
Evan Cheng | 506049f | 2010-03-03 01:44:33 +0000 | [diff] [blame] | 692 | if (TII->produceSameValue(MI, PrevMI)) |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 693 | return PrevMI; |
| 694 | } |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | bool MachineLICM::EliminateCSE(MachineInstr *MI, |
| 699 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator &CI) { |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 700 | if (CI == CSEMap.end()) |
| 701 | return false; |
| 702 | |
| 703 | if (const MachineInstr *Dup = LookForDuplicate(MI, CI->second)) { |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 704 | DEBUG(dbgs() << "CSEing " << *MI << " with " << *Dup); |
Dan Gohman | 6ac33b4 | 2010-02-28 01:33:43 +0000 | [diff] [blame] | 705 | |
| 706 | // Replace virtual registers defined by MI by their counterparts defined |
| 707 | // by Dup. |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 708 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 709 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | 6ac33b4 | 2010-02-28 01:33:43 +0000 | [diff] [blame] | 710 | |
| 711 | // Physical registers may not differ here. |
| 712 | assert((!MO.isReg() || MO.getReg() == 0 || |
| 713 | !TargetRegisterInfo::isPhysicalRegister(MO.getReg()) || |
| 714 | MO.getReg() == Dup->getOperand(i).getReg()) && |
| 715 | "Instructions with different phys regs are not identical!"); |
| 716 | |
| 717 | if (MO.isReg() && MO.isDef() && |
| 718 | !TargetRegisterInfo::isPhysicalRegister(MO.getReg())) |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 719 | RegInfo->replaceRegWith(MO.getReg(), Dup->getOperand(i).getReg()); |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 720 | } |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 721 | MI->eraseFromParent(); |
| 722 | ++NumCSEed; |
| 723 | return true; |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 724 | } |
| 725 | return false; |
| 726 | } |
| 727 | |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 728 | /// Hoist - When an instruction is found to use only loop invariant operands |
| 729 | /// 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] | 730 | /// |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 731 | void MachineLICM::Hoist(MachineInstr *MI) { |
| 732 | // First check whether we should hoist this instruction. |
Evan Cheng | c26abd9 | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 733 | if (!IsLoopInvariantInst(*MI) || !IsProfitableToHoist(*MI)) { |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 734 | // If not, try unfolding a hoistable load. |
| 735 | MI = ExtractHoistableLoad(MI); |
| 736 | if (!MI) return; |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 737 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 738 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 739 | // Now move the instructions to the predecessor, inserting it before any |
| 740 | // terminator instructions. |
| 741 | DEBUG({ |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 742 | dbgs() << "Hoisting " << *MI; |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 743 | if (CurPreheader->getBasicBlock()) |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 744 | dbgs() << " to MachineBasicBlock " |
Jakob Stoklund Olesen | 324da76 | 2009-11-20 01:17:03 +0000 | [diff] [blame] | 745 | << CurPreheader->getName(); |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 746 | if (MI->getParent()->getBasicBlock()) |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 747 | dbgs() << " from MachineBasicBlock " |
Jakob Stoklund Olesen | 324da76 | 2009-11-20 01:17:03 +0000 | [diff] [blame] | 748 | << MI->getParent()->getName(); |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 749 | dbgs() << "\n"; |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 750 | }); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 751 | |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 752 | // If this is the first instruction being hoisted to the preheader, |
| 753 | // initialize the CSE map with potential common expressions. |
| 754 | InitCSEMap(CurPreheader); |
| 755 | |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 756 | // Look for opportunity to CSE the hoisted instruction. |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 757 | unsigned Opcode = MI->getOpcode(); |
| 758 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator |
| 759 | CI = CSEMap.find(Opcode); |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 760 | if (!EliminateCSE(MI, CI)) { |
| 761 | // Otherwise, splice the instruction to the preheader. |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 762 | CurPreheader->splice(CurPreheader->getFirstTerminator(),MI->getParent(),MI); |
| 763 | |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 764 | // Add to the CSE map. |
| 765 | if (CI != CSEMap.end()) |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 766 | CI->second.push_back(MI); |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 767 | else { |
| 768 | std::vector<const MachineInstr*> CSEMIs; |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 769 | CSEMIs.push_back(MI); |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 770 | CSEMap.insert(std::make_pair(Opcode, CSEMIs)); |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 771 | } |
| 772 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 773 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 774 | ++NumHoisted; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 775 | Changed = true; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 776 | } |