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; |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 104 | unsigned Def; |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 105 | int FI; |
| 106 | CandidateInfo(MachineInstr *mi, unsigned def, int fi) |
| 107 | : MI(mi), Def(def), FI(fi) {} |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | /// HoistRegionPostRA - Walk the specified region of the CFG and hoist loop |
| 111 | /// invariants out to the preheader. |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 112 | void HoistRegionPostRA(); |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 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 | |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 125 | /// AddToLiveIns - Add register 'Reg' to the livein sets of BBs in the |
| 126 | /// current loop. |
| 127 | void AddToLiveIns(unsigned Reg); |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 128 | |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 129 | /// IsLICMCandidate - Returns true if the instruction may be a suitable |
| 130 | /// candidate for LICM. e.g. If the instruction is a call, then it's obviously |
| 131 | /// not safe to hoist it. |
| 132 | bool IsLICMCandidate(MachineInstr &I); |
| 133 | |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 134 | /// IsLoopInvariantInst - Returns true if the instruction is loop |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 135 | /// invariant. I.e., all virtual register operands are defined outside of |
| 136 | /// the loop, physical registers aren't accessed (explicitly or implicitly), |
| 137 | /// and the instruction is hoistable. |
| 138 | /// |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 139 | bool IsLoopInvariantInst(MachineInstr &I); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 140 | |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 141 | /// IsProfitableToHoist - Return true if it is potentially profitable to |
| 142 | /// hoist the given loop invariant. |
Evan Cheng | c26abd9 | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 143 | bool IsProfitableToHoist(MachineInstr &MI); |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 144 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 145 | /// HoistRegion - Walk the specified region of the CFG (defined by all |
| 146 | /// blocks dominated by the specified block, and that are in the current |
| 147 | /// loop) in depth first order w.r.t the DominatorTree. This allows us to |
| 148 | /// visit definitions before uses, allowing us to hoist a loop body in one |
| 149 | /// pass without iteration. |
| 150 | /// |
| 151 | void HoistRegion(MachineDomTreeNode *N); |
| 152 | |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 153 | /// isLoadFromConstantMemory - Return true if the given instruction is a |
| 154 | /// load from constant memory. |
| 155 | bool isLoadFromConstantMemory(MachineInstr *MI); |
| 156 | |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 157 | /// ExtractHoistableLoad - Unfold a load from the given machineinstr if |
| 158 | /// the load itself could be hoisted. Return the unfolded and hoistable |
| 159 | /// load, or null if the load couldn't be unfolded or if it wouldn't |
| 160 | /// be hoistable. |
| 161 | MachineInstr *ExtractHoistableLoad(MachineInstr *MI); |
| 162 | |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 163 | /// LookForDuplicate - Find an instruction amount PrevMIs that is a |
| 164 | /// duplicate of MI. Return this instruction if it's found. |
| 165 | const MachineInstr *LookForDuplicate(const MachineInstr *MI, |
| 166 | std::vector<const MachineInstr*> &PrevMIs); |
| 167 | |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 168 | /// EliminateCSE - Given a LICM'ed instruction, look for an instruction on |
| 169 | /// the preheader that compute the same value. If it's found, do a RAU on |
| 170 | /// with the definition of the existing instruction rather than hoisting |
| 171 | /// the instruction to the preheader. |
| 172 | bool EliminateCSE(MachineInstr *MI, |
| 173 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator &CI); |
| 174 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 175 | /// Hoist - When an instruction is found to only use loop invariant operands |
| 176 | /// that is safe to hoist, this instruction is called to do the dirty work. |
| 177 | /// |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 178 | void Hoist(MachineInstr *MI); |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 179 | |
| 180 | /// InitCSEMap - Initialize the CSE map with instructions that are in the |
| 181 | /// current loop preheader that may become duplicates of instructions that |
| 182 | /// are hoisted out of the loop. |
| 183 | void InitCSEMap(MachineBasicBlock *BB); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 184 | }; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 185 | } // end anonymous namespace |
| 186 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 187 | char MachineLICM::ID = 0; |
| 188 | static RegisterPass<MachineLICM> |
Bill Wendling | 8870ce9 | 2008-07-07 05:42:27 +0000 | [diff] [blame] | 189 | X("machinelicm", "Machine Loop Invariant Code Motion"); |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 190 | |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 191 | FunctionPass *llvm::createMachineLICMPass(bool PreRegAlloc) { |
| 192 | return new MachineLICM(PreRegAlloc); |
| 193 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 194 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 195 | /// LoopIsOuterMostWithPreheader - Test if the given loop is the outer-most |
| 196 | /// loop that has a preheader. |
| 197 | static bool LoopIsOuterMostWithPreheader(MachineLoop *CurLoop) { |
| 198 | for (MachineLoop *L = CurLoop->getParentLoop(); L; L = L->getParentLoop()) |
| 199 | if (L->getLoopPreheader()) |
| 200 | return false; |
| 201 | return true; |
| 202 | } |
| 203 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 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 | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 241 | if (!PreRegAlloc) |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 242 | HoistRegionPostRA(); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 243 | else { |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 244 | // CSEMap is initialized for loop header when the first instruction is |
| 245 | // being hoisted. |
| 246 | MachineDomTreeNode *N = DT->getNode(CurLoop->getHeader()); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 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; |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 278 | bool HasNonInvariantUse = false; |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 279 | unsigned Def = 0; |
| 280 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 281 | const MachineOperand &MO = MI->getOperand(i); |
| 282 | if (MO.isFI()) { |
| 283 | // Remember if the instruction stores to the frame index. |
| 284 | int FI = MO.getIndex(); |
| 285 | if (!StoredFIs.count(FI) && |
| 286 | MFI->isSpillSlotObjectIndex(FI) && |
| 287 | InstructionStoresToFI(MI, FI)) |
| 288 | StoredFIs.insert(FI); |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 289 | HasNonInvariantUse = true; |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 290 | continue; |
| 291 | } |
| 292 | |
| 293 | if (!MO.isReg()) |
| 294 | continue; |
| 295 | unsigned Reg = MO.getReg(); |
| 296 | if (!Reg) |
| 297 | continue; |
| 298 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && |
| 299 | "Not expecting virtual register!"); |
| 300 | |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 301 | if (!MO.isDef()) { |
Evan Cheng | 6327537 | 2010-04-13 22:13:34 +0000 | [diff] [blame] | 302 | if (Reg && PhysRegDefs[Reg]) |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 303 | // If it's using a non-loop-invariant register, then it's obviously not |
| 304 | // safe to hoist. |
| 305 | HasNonInvariantUse = true; |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 306 | continue; |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 307 | } |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 308 | |
| 309 | if (MO.isImplicit()) { |
| 310 | ++PhysRegDefs[Reg]; |
| 311 | for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) |
| 312 | ++PhysRegDefs[*AS]; |
| 313 | if (!MO.isDead()) |
| 314 | // Non-dead implicit def? This cannot be hoisted. |
| 315 | RuledOut = true; |
| 316 | // No need to check if a dead implicit def is also defined by |
| 317 | // another instruction. |
| 318 | continue; |
| 319 | } |
| 320 | |
| 321 | // FIXME: For now, avoid instructions with multiple defs, unless |
| 322 | // it's a dead implicit def. |
| 323 | if (Def) |
| 324 | RuledOut = true; |
| 325 | else |
| 326 | Def = Reg; |
| 327 | |
| 328 | // If we have already seen another instruction that defines the same |
| 329 | // register, then this is not safe. |
| 330 | if (++PhysRegDefs[Reg] > 1) |
| 331 | // MI defined register is seen defined by another instruction in |
| 332 | // the loop, it cannot be a LICM candidate. |
| 333 | RuledOut = true; |
| 334 | for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) |
| 335 | if (++PhysRegDefs[*AS] > 1) |
| 336 | RuledOut = true; |
| 337 | } |
| 338 | |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 339 | // Only consider reloads for now and remats which do not have register |
| 340 | // operands. FIXME: Consider unfold load folding instructions. |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 341 | if (Def && !RuledOut) { |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 342 | int FI = INT_MIN; |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 343 | if ((!HasNonInvariantUse && IsLICMCandidate(*MI)) || |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 344 | (TII->isLoadFromStackSlot(MI, FI) && MFI->isSpillSlotObjectIndex(FI))) |
| 345 | Candidates.push_back(CandidateInfo(MI, Def, FI)); |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
| 349 | /// HoistRegionPostRA - Walk the specified region of the CFG and hoist loop |
| 350 | /// invariants out to the preheader. |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 351 | void MachineLICM::HoistRegionPostRA() { |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 352 | unsigned NumRegs = TRI->getNumRegs(); |
| 353 | unsigned *PhysRegDefs = new unsigned[NumRegs]; |
| 354 | std::fill(PhysRegDefs, PhysRegDefs + NumRegs, 0); |
| 355 | |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 356 | SmallVector<CandidateInfo, 32> Candidates; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 357 | SmallSet<int, 32> StoredFIs; |
| 358 | |
| 359 | // Walk the entire region, count number of defs for each register, and |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 360 | // collect potential LICM candidates. |
| 361 | const std::vector<MachineBasicBlock*> Blocks = CurLoop->getBlocks(); |
| 362 | for (unsigned i = 0, e = Blocks.size(); i != e; ++i) { |
| 363 | MachineBasicBlock *BB = Blocks[i]; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 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. |
Dan Gohman | 81bf03e | 2010-04-13 16:57:55 +0000 | [diff] [blame] | 367 | for (MachineBasicBlock::livein_iterator I = BB->livein_begin(), |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 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 | } |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 380 | } |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 381 | |
| 382 | // Now evaluate whether the potential candidates qualify. |
| 383 | // 1. Check if the candidate defined register is defined by another |
| 384 | // instruction in the loop. |
| 385 | // 2. If the candidate is a load from stack slot (always true for now), |
| 386 | // check if the slot is stored anywhere in the loop. |
| 387 | for (unsigned i = 0, e = Candidates.size(); i != e; ++i) { |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 388 | if (Candidates[i].FI != INT_MIN && |
| 389 | StoredFIs.count(Candidates[i].FI)) |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 390 | continue; |
| 391 | |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 392 | if (PhysRegDefs[Candidates[i].Def] == 1) { |
| 393 | bool Safe = true; |
| 394 | MachineInstr *MI = Candidates[i].MI; |
Evan Cheng | c15d913 | 2010-04-13 20:25:29 +0000 | [diff] [blame] | 395 | for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) { |
| 396 | const MachineOperand &MO = MI->getOperand(j); |
Evan Cheng | 6327537 | 2010-04-13 22:13:34 +0000 | [diff] [blame] | 397 | if (!MO.isReg() || MO.isDef() || !MO.getReg()) |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 398 | continue; |
| 399 | if (PhysRegDefs[MO.getReg()]) { |
| 400 | // If it's using a non-loop-invariant register, then it's obviously |
| 401 | // not safe to hoist. |
| 402 | Safe = false; |
| 403 | break; |
| 404 | } |
| 405 | } |
| 406 | if (Safe) |
| 407 | HoistPostRA(MI, Candidates[i].Def); |
| 408 | } |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 409 | } |
Benjamin Kramer | 678d9b7 | 2010-04-12 11:38:35 +0000 | [diff] [blame] | 410 | |
| 411 | delete[] PhysRegDefs; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Jakob Stoklund Olesen | 9196ab6 | 2010-04-20 18:45:47 +0000 | [diff] [blame] | 414 | /// AddToLiveIns - Add register 'Reg' to the livein sets of BBs in the current |
| 415 | /// loop, and make sure it is not killed by any instructions in the loop. |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 416 | void MachineLICM::AddToLiveIns(unsigned Reg) { |
| 417 | const std::vector<MachineBasicBlock*> Blocks = CurLoop->getBlocks(); |
Jakob Stoklund Olesen | 9196ab6 | 2010-04-20 18:45:47 +0000 | [diff] [blame] | 418 | for (unsigned i = 0, e = Blocks.size(); i != e; ++i) { |
| 419 | MachineBasicBlock *BB = Blocks[i]; |
| 420 | if (!BB->isLiveIn(Reg)) |
| 421 | BB->addLiveIn(Reg); |
| 422 | for (MachineBasicBlock::iterator |
| 423 | MII = BB->begin(), E = BB->end(); MII != E; ++MII) { |
| 424 | MachineInstr *MI = &*MII; |
| 425 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 426 | MachineOperand &MO = MI->getOperand(i); |
| 427 | if (!MO.isReg() || !MO.getReg() || MO.isDef()) continue; |
| 428 | if (MO.getReg() == Reg || TRI->isSuperRegister(Reg, MO.getReg())) |
| 429 | MO.setIsKill(false); |
| 430 | } |
| 431 | } |
| 432 | } |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /// HoistPostRA - When an instruction is found to only use loop invariant |
| 436 | /// operands that is safe to hoist, this instruction is called to do the |
| 437 | /// dirty work. |
| 438 | void MachineLICM::HoistPostRA(MachineInstr *MI, unsigned Def) { |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 439 | // Now move the instructions to the predecessor, inserting it before any |
| 440 | // terminator instructions. |
| 441 | DEBUG({ |
| 442 | dbgs() << "Hoisting " << *MI; |
| 443 | if (CurPreheader->getBasicBlock()) |
| 444 | dbgs() << " to MachineBasicBlock " |
| 445 | << CurPreheader->getName(); |
| 446 | if (MI->getParent()->getBasicBlock()) |
| 447 | dbgs() << " from MachineBasicBlock " |
| 448 | << MI->getParent()->getName(); |
| 449 | dbgs() << "\n"; |
| 450 | }); |
| 451 | |
| 452 | // Splice the instruction to the preheader. |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 453 | MachineBasicBlock *MBB = MI->getParent(); |
| 454 | CurPreheader->splice(CurPreheader->getFirstTerminator(), MBB, MI); |
| 455 | |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 456 | // Add register to livein list to all the BBs in the current loop since a |
| 457 | // loop invariant must be kept live throughout the whole loop. This is |
| 458 | // important to ensure later passes do not scavenge the def register. |
| 459 | AddToLiveIns(Def); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 460 | |
| 461 | ++NumPostRAHoisted; |
| 462 | Changed = true; |
| 463 | } |
| 464 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 465 | /// HoistRegion - Walk the specified region of the CFG (defined by all blocks |
| 466 | /// dominated by the specified block, and that are in the current loop) in depth |
| 467 | /// first order w.r.t the DominatorTree. This allows us to visit definitions |
| 468 | /// before uses, allowing us to hoist a loop body in one pass without iteration. |
| 469 | /// |
| 470 | void MachineLICM::HoistRegion(MachineDomTreeNode *N) { |
| 471 | assert(N != 0 && "Null dominator tree node?"); |
| 472 | MachineBasicBlock *BB = N->getBlock(); |
| 473 | |
| 474 | // If this subregion is not in the top level loop at all, exit. |
| 475 | if (!CurLoop->contains(BB)) return; |
| 476 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 477 | for (MachineBasicBlock::iterator |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 478 | MII = BB->begin(), E = BB->end(); MII != E; ) { |
| 479 | MachineBasicBlock::iterator NextMII = MII; ++NextMII; |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 480 | Hoist(&*MII); |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 481 | MII = NextMII; |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 482 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 483 | |
| 484 | const std::vector<MachineDomTreeNode*> &Children = N->getChildren(); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 485 | for (unsigned I = 0, E = Children.size(); I != E; ++I) |
| 486 | HoistRegion(Children[I]); |
| 487 | } |
| 488 | |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 489 | /// IsLICMCandidate - Returns true if the instruction may be a suitable |
| 490 | /// candidate for LICM. e.g. If the instruction is a call, then it's obviously |
| 491 | /// not safe to hoist it. |
| 492 | bool MachineLICM::IsLICMCandidate(MachineInstr &I) { |
Evan Cheng | 6327537 | 2010-04-13 22:13:34 +0000 | [diff] [blame] | 493 | if (I.isImplicitDef()) |
| 494 | return false; |
| 495 | |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 496 | const TargetInstrDesc &TID = I.getDesc(); |
| 497 | |
| 498 | // Ignore stuff that we obviously can't hoist. |
Dan Gohman | 237dee1 | 2008-12-23 17:28:50 +0000 | [diff] [blame] | 499 | if (TID.mayStore() || TID.isCall() || TID.isTerminator() || |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 500 | TID.hasUnmodeledSideEffects()) |
| 501 | return false; |
Evan Cheng | 9b61f33 | 2009-02-04 07:17:49 +0000 | [diff] [blame] | 502 | |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 503 | if (TID.mayLoad()) { |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 504 | // Okay, this instruction does a load. As a refinement, we allow the target |
| 505 | // to decide whether the loaded value is actually a constant. If so, we can |
| 506 | // actually use it as a load. |
Dan Gohman | e33f44c | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 507 | if (!I.isInvariantLoad(AA)) |
Evan Cheng | 7adcdc3 | 2009-11-17 19:19:01 +0000 | [diff] [blame] | 508 | // FIXME: we should be able to hoist loads with no other side effects if |
| 509 | // there are no other instructions which can change memory in this loop. |
| 510 | // This is a trivial form of alias analysis. |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 511 | return false; |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 512 | } |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 513 | return true; |
| 514 | } |
| 515 | |
| 516 | /// IsLoopInvariantInst - Returns true if the instruction is loop |
| 517 | /// invariant. I.e., all virtual register operands are defined outside of the |
| 518 | /// loop, physical registers aren't accessed explicitly, and there are no side |
| 519 | /// effects that aren't captured by the operands or other flags. |
| 520 | /// |
| 521 | bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) { |
| 522 | if (!IsLICMCandidate(I)) |
| 523 | return false; |
Bill Wendling | 074223a | 2008-03-10 08:13:01 +0000 | [diff] [blame] | 524 | |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 525 | // The instruction is loop invariant if all of its operands are. |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 526 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
| 527 | const MachineOperand &MO = I.getOperand(i); |
| 528 | |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 529 | if (!MO.isReg()) |
Bill Wendling | fb018d0 | 2008-08-20 20:32:05 +0000 | [diff] [blame] | 530 | continue; |
| 531 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 532 | unsigned Reg = MO.getReg(); |
| 533 | if (Reg == 0) continue; |
| 534 | |
| 535 | // Don't hoist an instruction that uses or defines a physical register. |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 536 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 537 | if (MO.isUse()) { |
| 538 | // 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] | 539 | // and we can freely move its uses. Alternatively, if it's allocatable, |
| 540 | // it could get allocated to something with a def during allocation. |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 541 | if (!RegInfo->def_empty(Reg)) |
| 542 | return false; |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 543 | if (AllocatableSet.test(Reg)) |
| 544 | return false; |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 545 | // Check for a def among the register's aliases too. |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 546 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 547 | unsigned AliasReg = *Alias; |
| 548 | if (!RegInfo->def_empty(AliasReg)) |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 549 | return false; |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 550 | if (AllocatableSet.test(AliasReg)) |
| 551 | return false; |
| 552 | } |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 553 | // Otherwise it's safe to move. |
| 554 | continue; |
| 555 | } else if (!MO.isDead()) { |
| 556 | // A def that isn't dead. We can't move it. |
| 557 | return false; |
Dan Gohman | a363a9b | 2010-02-28 00:08:44 +0000 | [diff] [blame] | 558 | } else if (CurLoop->getHeader()->isLiveIn(Reg)) { |
| 559 | // If the reg is live into the loop, we can't hoist an instruction |
| 560 | // which would clobber it. |
| 561 | return false; |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 562 | } |
| 563 | } |
Bill Wendling | fb018d0 | 2008-08-20 20:32:05 +0000 | [diff] [blame] | 564 | |
| 565 | if (!MO.isUse()) |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 566 | continue; |
| 567 | |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 568 | assert(RegInfo->getVRegDef(Reg) && |
| 569 | "Machine instr not mapped for this vreg?!"); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 570 | |
| 571 | // If the loop contains the definition of an operand, then the instruction |
| 572 | // isn't loop invariant. |
Dan Gohman | 92329c7 | 2009-12-18 01:24:09 +0000 | [diff] [blame] | 573 | if (CurLoop->contains(RegInfo->getVRegDef(Reg))) |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 574 | return false; |
| 575 | } |
| 576 | |
| 577 | // If we got this far, the instruction is loop invariant! |
| 578 | return true; |
| 579 | } |
| 580 | |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 581 | |
| 582 | /// HasPHIUses - Return true if the specified register has any PHI use. |
| 583 | static bool HasPHIUses(unsigned Reg, MachineRegisterInfo *RegInfo) { |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 584 | for (MachineRegisterInfo::use_iterator UI = RegInfo->use_begin(Reg), |
| 585 | UE = RegInfo->use_end(); UI != UE; ++UI) { |
| 586 | MachineInstr *UseMI = &*UI; |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 587 | if (UseMI->isPHI()) |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 588 | return true; |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 589 | } |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 590 | return false; |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 593 | /// isLoadFromConstantMemory - Return true if the given instruction is a |
| 594 | /// load from constant memory. Machine LICM will hoist these even if they are |
| 595 | /// not re-materializable. |
| 596 | bool MachineLICM::isLoadFromConstantMemory(MachineInstr *MI) { |
| 597 | if (!MI->getDesc().mayLoad()) return false; |
| 598 | if (!MI->hasOneMemOperand()) return false; |
| 599 | MachineMemOperand *MMO = *MI->memoperands_begin(); |
| 600 | if (MMO->isVolatile()) return false; |
| 601 | if (!MMO->getValue()) return false; |
| 602 | const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(MMO->getValue()); |
| 603 | if (PSV) { |
| 604 | MachineFunction &MF = *MI->getParent()->getParent(); |
| 605 | return PSV->isConstant(MF.getFrameInfo()); |
| 606 | } else { |
| 607 | return AA->pointsToConstantMemory(MMO->getValue()); |
| 608 | } |
| 609 | } |
| 610 | |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 611 | /// IsProfitableToHoist - Return true if it is potentially profitable to hoist |
| 612 | /// the given loop invariant. |
Evan Cheng | c26abd9 | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 613 | bool MachineLICM::IsProfitableToHoist(MachineInstr &MI) { |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 614 | // FIXME: For now, only hoist re-materilizable instructions. LICM will |
| 615 | // increase register pressure. We want to make sure it doesn't increase |
| 616 | // spilling. |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 617 | // Also hoist loads from constant memory, e.g. load from stubs, GOT. Hoisting |
| 618 | // these tend to help performance in low register pressure situation. The |
| 619 | // trade off is it may cause spill in high pressure situation. It will end up |
| 620 | // adding a store in the loop preheader. But the reload is no more expensive. |
| 621 | // The side benefit is these loads are frequently CSE'ed. |
| 622 | if (!TII->isTriviallyReMaterializable(&MI, AA)) { |
Evan Cheng | c26abd9 | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 623 | if (!isLoadFromConstantMemory(&MI)) |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 624 | return false; |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 625 | } |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 626 | |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 627 | // If result(s) of this instruction is used by PHIs, then don't hoist it. |
| 628 | // The presence of joins makes it difficult for current register allocator |
| 629 | // implementation to perform remat. |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 630 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 631 | const MachineOperand &MO = MI.getOperand(i); |
| 632 | if (!MO.isReg() || !MO.isDef()) |
| 633 | continue; |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 634 | if (HasPHIUses(MO.getReg(), RegInfo)) |
| 635 | return false; |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 636 | } |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 637 | |
| 638 | return true; |
| 639 | } |
| 640 | |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 641 | MachineInstr *MachineLICM::ExtractHoistableLoad(MachineInstr *MI) { |
| 642 | // If not, we may be able to unfold a load and hoist that. |
| 643 | // First test whether the instruction is loading from an amenable |
| 644 | // memory location. |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 645 | if (!isLoadFromConstantMemory(MI)) |
| 646 | return 0; |
| 647 | |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 648 | // Next determine the register class for a temporary register. |
Dan Gohman | 0115e16 | 2009-10-30 22:18:41 +0000 | [diff] [blame] | 649 | unsigned LoadRegIndex; |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 650 | unsigned NewOpc = |
| 651 | TII->getOpcodeAfterMemoryUnfold(MI->getOpcode(), |
| 652 | /*UnfoldLoad=*/true, |
Dan Gohman | 0115e16 | 2009-10-30 22:18:41 +0000 | [diff] [blame] | 653 | /*UnfoldStore=*/false, |
| 654 | &LoadRegIndex); |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 655 | if (NewOpc == 0) return 0; |
| 656 | const TargetInstrDesc &TID = TII->get(NewOpc); |
| 657 | if (TID.getNumDefs() != 1) return 0; |
Dan Gohman | 0115e16 | 2009-10-30 22:18:41 +0000 | [diff] [blame] | 658 | const TargetRegisterClass *RC = TID.OpInfo[LoadRegIndex].getRegClass(TRI); |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 659 | // Ok, we're unfolding. Create a temporary register and do the unfold. |
| 660 | unsigned Reg = RegInfo->createVirtualRegister(RC); |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 661 | |
| 662 | MachineFunction &MF = *MI->getParent()->getParent(); |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 663 | SmallVector<MachineInstr *, 2> NewMIs; |
| 664 | bool Success = |
| 665 | TII->unfoldMemoryOperand(MF, MI, Reg, |
| 666 | /*UnfoldLoad=*/true, /*UnfoldStore=*/false, |
| 667 | NewMIs); |
| 668 | (void)Success; |
| 669 | assert(Success && |
| 670 | "unfoldMemoryOperand failed when getOpcodeAfterMemoryUnfold " |
| 671 | "succeeded!"); |
| 672 | assert(NewMIs.size() == 2 && |
| 673 | "Unfolded a load into multiple instructions!"); |
| 674 | MachineBasicBlock *MBB = MI->getParent(); |
| 675 | MBB->insert(MI, NewMIs[0]); |
| 676 | MBB->insert(MI, NewMIs[1]); |
| 677 | // If unfolding produced a load that wasn't loop-invariant or profitable to |
| 678 | // hoist, discard the new instructions and bail. |
Evan Cheng | c26abd9 | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 679 | if (!IsLoopInvariantInst(*NewMIs[0]) || !IsProfitableToHoist(*NewMIs[0])) { |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 680 | NewMIs[0]->eraseFromParent(); |
| 681 | NewMIs[1]->eraseFromParent(); |
| 682 | return 0; |
| 683 | } |
| 684 | // Otherwise we successfully unfolded a load that we can hoist. |
| 685 | MI->eraseFromParent(); |
| 686 | return NewMIs[0]; |
| 687 | } |
| 688 | |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 689 | void MachineLICM::InitCSEMap(MachineBasicBlock *BB) { |
| 690 | for (MachineBasicBlock::iterator I = BB->begin(),E = BB->end(); I != E; ++I) { |
| 691 | const MachineInstr *MI = &*I; |
| 692 | // FIXME: For now, only hoist re-materilizable instructions. LICM will |
| 693 | // increase register pressure. We want to make sure it doesn't increase |
| 694 | // spilling. |
| 695 | if (TII->isTriviallyReMaterializable(MI, AA)) { |
| 696 | unsigned Opcode = MI->getOpcode(); |
| 697 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator |
| 698 | CI = CSEMap.find(Opcode); |
| 699 | if (CI != CSEMap.end()) |
| 700 | CI->second.push_back(MI); |
| 701 | else { |
| 702 | std::vector<const MachineInstr*> CSEMIs; |
| 703 | CSEMIs.push_back(MI); |
| 704 | CSEMap.insert(std::make_pair(Opcode, CSEMIs)); |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 710 | const MachineInstr* |
| 711 | MachineLICM::LookForDuplicate(const MachineInstr *MI, |
| 712 | std::vector<const MachineInstr*> &PrevMIs) { |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 713 | for (unsigned i = 0, e = PrevMIs.size(); i != e; ++i) { |
| 714 | const MachineInstr *PrevMI = PrevMIs[i]; |
Evan Cheng | 506049f | 2010-03-03 01:44:33 +0000 | [diff] [blame] | 715 | if (TII->produceSameValue(MI, PrevMI)) |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 716 | return PrevMI; |
| 717 | } |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | bool MachineLICM::EliminateCSE(MachineInstr *MI, |
| 722 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator &CI) { |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 723 | if (CI == CSEMap.end()) |
| 724 | return false; |
| 725 | |
| 726 | if (const MachineInstr *Dup = LookForDuplicate(MI, CI->second)) { |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 727 | DEBUG(dbgs() << "CSEing " << *MI << " with " << *Dup); |
Dan Gohman | 6ac33b4 | 2010-02-28 01:33:43 +0000 | [diff] [blame] | 728 | |
| 729 | // Replace virtual registers defined by MI by their counterparts defined |
| 730 | // by Dup. |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 731 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 732 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | 6ac33b4 | 2010-02-28 01:33:43 +0000 | [diff] [blame] | 733 | |
| 734 | // Physical registers may not differ here. |
| 735 | assert((!MO.isReg() || MO.getReg() == 0 || |
| 736 | !TargetRegisterInfo::isPhysicalRegister(MO.getReg()) || |
| 737 | MO.getReg() == Dup->getOperand(i).getReg()) && |
| 738 | "Instructions with different phys regs are not identical!"); |
| 739 | |
| 740 | if (MO.isReg() && MO.isDef() && |
| 741 | !TargetRegisterInfo::isPhysicalRegister(MO.getReg())) |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 742 | RegInfo->replaceRegWith(MO.getReg(), Dup->getOperand(i).getReg()); |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 743 | } |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 744 | MI->eraseFromParent(); |
| 745 | ++NumCSEed; |
| 746 | return true; |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 747 | } |
| 748 | return false; |
| 749 | } |
| 750 | |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 751 | /// Hoist - When an instruction is found to use only loop invariant operands |
| 752 | /// 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] | 753 | /// |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 754 | void MachineLICM::Hoist(MachineInstr *MI) { |
| 755 | // First check whether we should hoist this instruction. |
Evan Cheng | c26abd9 | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 756 | if (!IsLoopInvariantInst(*MI) || !IsProfitableToHoist(*MI)) { |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 757 | // If not, try unfolding a hoistable load. |
| 758 | MI = ExtractHoistableLoad(MI); |
| 759 | if (!MI) return; |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 760 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 761 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 762 | // Now move the instructions to the predecessor, inserting it before any |
| 763 | // terminator instructions. |
| 764 | DEBUG({ |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 765 | dbgs() << "Hoisting " << *MI; |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 766 | if (CurPreheader->getBasicBlock()) |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 767 | dbgs() << " to MachineBasicBlock " |
Jakob Stoklund Olesen | 324da76 | 2009-11-20 01:17:03 +0000 | [diff] [blame] | 768 | << CurPreheader->getName(); |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 769 | if (MI->getParent()->getBasicBlock()) |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 770 | dbgs() << " from MachineBasicBlock " |
Jakob Stoklund Olesen | 324da76 | 2009-11-20 01:17:03 +0000 | [diff] [blame] | 771 | << MI->getParent()->getName(); |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 772 | dbgs() << "\n"; |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 773 | }); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 774 | |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 775 | // If this is the first instruction being hoisted to the preheader, |
| 776 | // initialize the CSE map with potential common expressions. |
| 777 | InitCSEMap(CurPreheader); |
| 778 | |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 779 | // Look for opportunity to CSE the hoisted instruction. |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 780 | unsigned Opcode = MI->getOpcode(); |
| 781 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator |
| 782 | CI = CSEMap.find(Opcode); |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 783 | if (!EliminateCSE(MI, CI)) { |
| 784 | // Otherwise, splice the instruction to the preheader. |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 785 | CurPreheader->splice(CurPreheader->getFirstTerminator(),MI->getParent(),MI); |
| 786 | |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 787 | // Add to the CSE map. |
| 788 | if (CI != CSEMap.end()) |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 789 | CI->second.push_back(MI); |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 790 | else { |
| 791 | std::vector<const MachineInstr*> CSEMIs; |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 792 | CSEMIs.push_back(MI); |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 793 | CSEMap.insert(std::make_pair(Opcode, CSEMIs)); |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 794 | } |
| 795 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 796 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 797 | ++NumHoisted; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 798 | Changed = true; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 799 | } |