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. |
Evan Cheng | 82e0a1a | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 65 | bool FirstInLoop; // True if it's the first LICM in the loop. |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 66 | MachineLoop *CurLoop; // The current loop we are working on. |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 67 | MachineBasicBlock *CurPreheader; // The preheader for CurLoop. |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 68 | |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 69 | BitVector AllocatableSet; |
| 70 | |
Dale Johannesen | c46a5f2 | 2010-07-29 17:45:24 +0000 | [diff] [blame] | 71 | // For each opcode, keep a list of potential CSE instructions. |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 72 | DenseMap<unsigned, std::vector<const MachineInstr*> > CSEMap; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 73 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 74 | public: |
| 75 | static char ID; // Pass identification, replacement for typeid |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 76 | MachineLICM() : |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 77 | MachineFunctionPass(ID), PreRegAlloc(true) {} |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 78 | |
| 79 | explicit MachineLICM(bool PreRA) : |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 80 | MachineFunctionPass(ID), PreRegAlloc(PreRA) {} |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 81 | |
| 82 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 83 | |
Dan Gohman | 7224170 | 2008-12-18 01:37:56 +0000 | [diff] [blame] | 84 | const char *getPassName() const { return "Machine Instruction LICM"; } |
| 85 | |
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 |
Chris Lattner | 7791080 | 2010-07-12 00:00:35 +0000 | [diff] [blame] | 130 | /// candidate for LICM. e.g. If the instruction is a call, then it's |
| 131 | /// obviously not safe to hoist it. |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 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); |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 184 | |
| 185 | /// getCurPreheader - Get the preheader for the current loop, splitting |
| 186 | /// a critical edge if needed. |
| 187 | MachineBasicBlock *getCurPreheader(); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 188 | }; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 189 | } // end anonymous namespace |
| 190 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 191 | char MachineLICM::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame^] | 192 | INITIALIZE_PASS_BEGIN(MachineLICM, "machinelicm", |
| 193 | "Machine Loop Invariant Code Motion", false, false) |
| 194 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) |
| 195 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
| 196 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 197 | INITIALIZE_PASS_END(MachineLICM, "machinelicm", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 198 | "Machine Loop Invariant Code Motion", false, false) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 199 | |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 200 | FunctionPass *llvm::createMachineLICMPass(bool PreRegAlloc) { |
| 201 | return new MachineLICM(PreRegAlloc); |
| 202 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 203 | |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 204 | /// LoopIsOuterMostWithPredecessor - Test if the given loop is the outer-most |
| 205 | /// loop that has a unique predecessor. |
| 206 | static bool LoopIsOuterMostWithPredecessor(MachineLoop *CurLoop) { |
Dan Gohman | aa74260 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 207 | // Check whether this loop even has a unique predecessor. |
| 208 | if (!CurLoop->getLoopPredecessor()) |
| 209 | return false; |
| 210 | // Ok, now check to see if any of its outer loops do. |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 211 | for (MachineLoop *L = CurLoop->getParentLoop(); L; L = L->getParentLoop()) |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 212 | if (L->getLoopPredecessor()) |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 213 | return false; |
Dan Gohman | aa74260 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 214 | // None of them did, so this is the outermost with a unique predecessor. |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 215 | return true; |
| 216 | } |
| 217 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 218 | bool MachineLICM::runOnMachineFunction(MachineFunction &MF) { |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 219 | if (PreRegAlloc) |
| 220 | DEBUG(dbgs() << "******** Pre-regalloc Machine LICM ********\n"); |
| 221 | else |
| 222 | DEBUG(dbgs() << "******** Post-regalloc Machine LICM ********\n"); |
Bill Wendling | a17ad59 | 2007-12-11 22:22:22 +0000 | [diff] [blame] | 223 | |
Evan Cheng | 82e0a1a | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 224 | Changed = FirstInLoop = false; |
Bill Wendling | acb04ec | 2008-08-31 02:30:23 +0000 | [diff] [blame] | 225 | TM = &MF.getTarget(); |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 226 | TII = TM->getInstrInfo(); |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 227 | TRI = TM->getRegisterInfo(); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 228 | MFI = MF.getFrameInfo(); |
Bill Wendling | acb04ec | 2008-08-31 02:30:23 +0000 | [diff] [blame] | 229 | RegInfo = &MF.getRegInfo(); |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 230 | AllocatableSet = TRI->getAllocatableSet(MF); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 231 | |
| 232 | // Get our Loop information... |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 233 | MLI = &getAnalysis<MachineLoopInfo>(); |
| 234 | DT = &getAnalysis<MachineDominatorTree>(); |
| 235 | AA = &getAnalysis<AliasAnalysis>(); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 236 | |
Dan Gohman | aa74260 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 237 | SmallVector<MachineLoop *, 8> Worklist(MLI->begin(), MLI->end()); |
| 238 | while (!Worklist.empty()) { |
| 239 | CurLoop = Worklist.pop_back_val(); |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 240 | CurPreheader = 0; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 241 | |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 242 | // If this is done before regalloc, only visit outer-most preheader-sporting |
| 243 | // loops. |
Dan Gohman | aa74260 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 244 | if (PreRegAlloc && !LoopIsOuterMostWithPredecessor(CurLoop)) { |
| 245 | Worklist.append(CurLoop->begin(), CurLoop->end()); |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 246 | continue; |
Dan Gohman | aa74260 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 247 | } |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 248 | |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 249 | if (!PreRegAlloc) |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 250 | HoistRegionPostRA(); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 251 | else { |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 252 | // CSEMap is initialized for loop header when the first instruction is |
| 253 | // being hoisted. |
| 254 | MachineDomTreeNode *N = DT->getNode(CurLoop->getHeader()); |
Evan Cheng | 82e0a1a | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 255 | FirstInLoop = true; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 256 | HoistRegion(N); |
| 257 | CSEMap.clear(); |
| 258 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | return Changed; |
| 262 | } |
| 263 | |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 264 | /// InstructionStoresToFI - Return true if instruction stores to the |
| 265 | /// specified frame. |
| 266 | static bool InstructionStoresToFI(const MachineInstr *MI, int FI) { |
| 267 | for (MachineInstr::mmo_iterator o = MI->memoperands_begin(), |
| 268 | oe = MI->memoperands_end(); o != oe; ++o) { |
| 269 | if (!(*o)->isStore() || !(*o)->getValue()) |
| 270 | continue; |
| 271 | if (const FixedStackPseudoSourceValue *Value = |
| 272 | dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) { |
| 273 | if (Value->getFrameIndex() == FI) |
| 274 | return true; |
| 275 | } |
| 276 | } |
| 277 | return false; |
| 278 | } |
| 279 | |
| 280 | /// ProcessMI - Examine the instruction for potentai LICM candidate. Also |
| 281 | /// gather register def and frame object update information. |
| 282 | void MachineLICM::ProcessMI(MachineInstr *MI, |
| 283 | unsigned *PhysRegDefs, |
| 284 | SmallSet<int, 32> &StoredFIs, |
| 285 | SmallVector<CandidateInfo, 32> &Candidates) { |
| 286 | bool RuledOut = false; |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 287 | bool HasNonInvariantUse = false; |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 288 | unsigned Def = 0; |
| 289 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 290 | const MachineOperand &MO = MI->getOperand(i); |
| 291 | if (MO.isFI()) { |
| 292 | // Remember if the instruction stores to the frame index. |
| 293 | int FI = MO.getIndex(); |
| 294 | if (!StoredFIs.count(FI) && |
| 295 | MFI->isSpillSlotObjectIndex(FI) && |
| 296 | InstructionStoresToFI(MI, FI)) |
| 297 | StoredFIs.insert(FI); |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 298 | HasNonInvariantUse = true; |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 299 | continue; |
| 300 | } |
| 301 | |
| 302 | if (!MO.isReg()) |
| 303 | continue; |
| 304 | unsigned Reg = MO.getReg(); |
| 305 | if (!Reg) |
| 306 | continue; |
| 307 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && |
| 308 | "Not expecting virtual register!"); |
| 309 | |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 310 | if (!MO.isDef()) { |
Evan Cheng | 6327537 | 2010-04-13 22:13:34 +0000 | [diff] [blame] | 311 | if (Reg && PhysRegDefs[Reg]) |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 312 | // If it's using a non-loop-invariant register, then it's obviously not |
| 313 | // safe to hoist. |
| 314 | HasNonInvariantUse = true; |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 315 | continue; |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 316 | } |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 317 | |
| 318 | if (MO.isImplicit()) { |
| 319 | ++PhysRegDefs[Reg]; |
| 320 | for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) |
| 321 | ++PhysRegDefs[*AS]; |
| 322 | if (!MO.isDead()) |
| 323 | // Non-dead implicit def? This cannot be hoisted. |
| 324 | RuledOut = true; |
| 325 | // No need to check if a dead implicit def is also defined by |
| 326 | // another instruction. |
| 327 | continue; |
| 328 | } |
| 329 | |
| 330 | // FIXME: For now, avoid instructions with multiple defs, unless |
| 331 | // it's a dead implicit def. |
| 332 | if (Def) |
| 333 | RuledOut = true; |
| 334 | else |
| 335 | Def = Reg; |
| 336 | |
| 337 | // If we have already seen another instruction that defines the same |
| 338 | // register, then this is not safe. |
| 339 | if (++PhysRegDefs[Reg] > 1) |
| 340 | // MI defined register is seen defined by another instruction in |
| 341 | // the loop, it cannot be a LICM candidate. |
| 342 | RuledOut = true; |
| 343 | for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) |
| 344 | if (++PhysRegDefs[*AS] > 1) |
| 345 | RuledOut = true; |
| 346 | } |
| 347 | |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 348 | // Only consider reloads for now and remats which do not have register |
| 349 | // operands. FIXME: Consider unfold load folding instructions. |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 350 | if (Def && !RuledOut) { |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 351 | int FI = INT_MIN; |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 352 | if ((!HasNonInvariantUse && IsLICMCandidate(*MI)) || |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 353 | (TII->isLoadFromStackSlot(MI, FI) && MFI->isSpillSlotObjectIndex(FI))) |
| 354 | Candidates.push_back(CandidateInfo(MI, Def, FI)); |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
| 358 | /// HoistRegionPostRA - Walk the specified region of the CFG and hoist loop |
| 359 | /// invariants out to the preheader. |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 360 | void MachineLICM::HoistRegionPostRA() { |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 361 | unsigned NumRegs = TRI->getNumRegs(); |
| 362 | unsigned *PhysRegDefs = new unsigned[NumRegs]; |
| 363 | std::fill(PhysRegDefs, PhysRegDefs + NumRegs, 0); |
| 364 | |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 365 | SmallVector<CandidateInfo, 32> Candidates; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 366 | SmallSet<int, 32> StoredFIs; |
| 367 | |
| 368 | // Walk the entire region, count number of defs for each register, and |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 369 | // collect potential LICM candidates. |
| 370 | const std::vector<MachineBasicBlock*> Blocks = CurLoop->getBlocks(); |
| 371 | for (unsigned i = 0, e = Blocks.size(); i != e; ++i) { |
| 372 | MachineBasicBlock *BB = Blocks[i]; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 373 | // Conservatively treat live-in's as an external def. |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 374 | // FIXME: That means a reload that're reused in successor block(s) will not |
| 375 | // be LICM'ed. |
Dan Gohman | 81bf03e | 2010-04-13 16:57:55 +0000 | [diff] [blame] | 376 | for (MachineBasicBlock::livein_iterator I = BB->livein_begin(), |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 377 | E = BB->livein_end(); I != E; ++I) { |
| 378 | unsigned Reg = *I; |
| 379 | ++PhysRegDefs[Reg]; |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 380 | for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) |
| 381 | ++PhysRegDefs[*AS]; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | for (MachineBasicBlock::iterator |
| 385 | MII = BB->begin(), E = BB->end(); MII != E; ++MII) { |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 386 | MachineInstr *MI = &*MII; |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 387 | ProcessMI(MI, PhysRegDefs, StoredFIs, Candidates); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 388 | } |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 389 | } |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 390 | |
| 391 | // Now evaluate whether the potential candidates qualify. |
| 392 | // 1. Check if the candidate defined register is defined by another |
| 393 | // instruction in the loop. |
| 394 | // 2. If the candidate is a load from stack slot (always true for now), |
| 395 | // check if the slot is stored anywhere in the loop. |
| 396 | for (unsigned i = 0, e = Candidates.size(); i != e; ++i) { |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 397 | if (Candidates[i].FI != INT_MIN && |
| 398 | StoredFIs.count(Candidates[i].FI)) |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 399 | continue; |
| 400 | |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 401 | if (PhysRegDefs[Candidates[i].Def] == 1) { |
| 402 | bool Safe = true; |
| 403 | MachineInstr *MI = Candidates[i].MI; |
Evan Cheng | c15d913 | 2010-04-13 20:25:29 +0000 | [diff] [blame] | 404 | for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) { |
| 405 | const MachineOperand &MO = MI->getOperand(j); |
Evan Cheng | 6327537 | 2010-04-13 22:13:34 +0000 | [diff] [blame] | 406 | if (!MO.isReg() || MO.isDef() || !MO.getReg()) |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 407 | continue; |
| 408 | if (PhysRegDefs[MO.getReg()]) { |
| 409 | // If it's using a non-loop-invariant register, then it's obviously |
| 410 | // not safe to hoist. |
| 411 | Safe = false; |
| 412 | break; |
| 413 | } |
| 414 | } |
| 415 | if (Safe) |
| 416 | HoistPostRA(MI, Candidates[i].Def); |
| 417 | } |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 418 | } |
Benjamin Kramer | 678d9b7 | 2010-04-12 11:38:35 +0000 | [diff] [blame] | 419 | |
| 420 | delete[] PhysRegDefs; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Jakob Stoklund Olesen | 9196ab6 | 2010-04-20 18:45:47 +0000 | [diff] [blame] | 423 | /// AddToLiveIns - Add register 'Reg' to the livein sets of BBs in the current |
| 424 | /// 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] | 425 | void MachineLICM::AddToLiveIns(unsigned Reg) { |
| 426 | const std::vector<MachineBasicBlock*> Blocks = CurLoop->getBlocks(); |
Jakob Stoklund Olesen | 9196ab6 | 2010-04-20 18:45:47 +0000 | [diff] [blame] | 427 | for (unsigned i = 0, e = Blocks.size(); i != e; ++i) { |
| 428 | MachineBasicBlock *BB = Blocks[i]; |
| 429 | if (!BB->isLiveIn(Reg)) |
| 430 | BB->addLiveIn(Reg); |
| 431 | for (MachineBasicBlock::iterator |
| 432 | MII = BB->begin(), E = BB->end(); MII != E; ++MII) { |
| 433 | MachineInstr *MI = &*MII; |
| 434 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 435 | MachineOperand &MO = MI->getOperand(i); |
| 436 | if (!MO.isReg() || !MO.getReg() || MO.isDef()) continue; |
| 437 | if (MO.getReg() == Reg || TRI->isSuperRegister(Reg, MO.getReg())) |
| 438 | MO.setIsKill(false); |
| 439 | } |
| 440 | } |
| 441 | } |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /// HoistPostRA - When an instruction is found to only use loop invariant |
| 445 | /// operands that is safe to hoist, this instruction is called to do the |
| 446 | /// dirty work. |
| 447 | void MachineLICM::HoistPostRA(MachineInstr *MI, unsigned Def) { |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 448 | MachineBasicBlock *Preheader = getCurPreheader(); |
| 449 | if (!Preheader) return; |
| 450 | |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 451 | // Now move the instructions to the predecessor, inserting it before any |
| 452 | // terminator instructions. |
| 453 | DEBUG({ |
| 454 | dbgs() << "Hoisting " << *MI; |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 455 | if (Preheader->getBasicBlock()) |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 456 | dbgs() << " to MachineBasicBlock " |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 457 | << Preheader->getName(); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 458 | if (MI->getParent()->getBasicBlock()) |
| 459 | dbgs() << " from MachineBasicBlock " |
| 460 | << MI->getParent()->getName(); |
| 461 | dbgs() << "\n"; |
| 462 | }); |
| 463 | |
| 464 | // Splice the instruction to the preheader. |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 465 | MachineBasicBlock *MBB = MI->getParent(); |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 466 | Preheader->splice(Preheader->getFirstTerminator(), MBB, MI); |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 467 | |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 468 | // Add register to livein list to all the BBs in the current loop since a |
| 469 | // loop invariant must be kept live throughout the whole loop. This is |
| 470 | // important to ensure later passes do not scavenge the def register. |
| 471 | AddToLiveIns(Def); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 472 | |
| 473 | ++NumPostRAHoisted; |
| 474 | Changed = true; |
| 475 | } |
| 476 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 477 | /// HoistRegion - Walk the specified region of the CFG (defined by all blocks |
| 478 | /// dominated by the specified block, and that are in the current loop) in depth |
| 479 | /// first order w.r.t the DominatorTree. This allows us to visit definitions |
| 480 | /// before uses, allowing us to hoist a loop body in one pass without iteration. |
| 481 | /// |
| 482 | void MachineLICM::HoistRegion(MachineDomTreeNode *N) { |
| 483 | assert(N != 0 && "Null dominator tree node?"); |
| 484 | MachineBasicBlock *BB = N->getBlock(); |
| 485 | |
| 486 | // If this subregion is not in the top level loop at all, exit. |
| 487 | if (!CurLoop->contains(BB)) return; |
| 488 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 489 | for (MachineBasicBlock::iterator |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 490 | MII = BB->begin(), E = BB->end(); MII != E; ) { |
| 491 | MachineBasicBlock::iterator NextMII = MII; ++NextMII; |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 492 | Hoist(&*MII); |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 493 | MII = NextMII; |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 494 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 495 | |
Dale Johannesen | bf1ae5e | 2010-07-20 00:50:13 +0000 | [diff] [blame] | 496 | // Don't hoist things out of a large switch statement. This often causes |
| 497 | // code to be hoisted that wasn't going to be executed, and increases |
| 498 | // register pressure in a situation where it's likely to matter. |
Dale Johannesen | 21d35c1 | 2010-07-20 21:29:12 +0000 | [diff] [blame] | 499 | if (BB->succ_size() < 25) { |
| 500 | const std::vector<MachineDomTreeNode*> &Children = N->getChildren(); |
Dale Johannesen | bf1ae5e | 2010-07-20 00:50:13 +0000 | [diff] [blame] | 501 | for (unsigned I = 0, E = Children.size(); I != E; ++I) |
| 502 | HoistRegion(Children[I]); |
Dale Johannesen | 21d35c1 | 2010-07-20 21:29:12 +0000 | [diff] [blame] | 503 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 506 | /// IsLICMCandidate - Returns true if the instruction may be a suitable |
| 507 | /// candidate for LICM. e.g. If the instruction is a call, then it's obviously |
| 508 | /// not safe to hoist it. |
| 509 | bool MachineLICM::IsLICMCandidate(MachineInstr &I) { |
Chris Lattner | 7791080 | 2010-07-12 00:00:35 +0000 | [diff] [blame] | 510 | // Check if it's safe to move the instruction. |
| 511 | bool DontMoveAcrossStore = true; |
| 512 | if (!I.isSafeToMove(TII, AA, DontMoveAcrossStore)) |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 513 | return false; |
Chris Lattner | 7791080 | 2010-07-12 00:00:35 +0000 | [diff] [blame] | 514 | |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 515 | return true; |
| 516 | } |
| 517 | |
| 518 | /// IsLoopInvariantInst - Returns true if the instruction is loop |
| 519 | /// invariant. I.e., all virtual register operands are defined outside of the |
| 520 | /// loop, physical registers aren't accessed explicitly, and there are no side |
| 521 | /// effects that aren't captured by the operands or other flags. |
| 522 | /// |
| 523 | bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) { |
| 524 | if (!IsLICMCandidate(I)) |
| 525 | return false; |
Bill Wendling | 074223a | 2008-03-10 08:13:01 +0000 | [diff] [blame] | 526 | |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 527 | // The instruction is loop invariant if all of its operands are. |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 528 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
| 529 | const MachineOperand &MO = I.getOperand(i); |
| 530 | |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 531 | if (!MO.isReg()) |
Bill Wendling | fb018d0 | 2008-08-20 20:32:05 +0000 | [diff] [blame] | 532 | continue; |
| 533 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 534 | unsigned Reg = MO.getReg(); |
| 535 | if (Reg == 0) continue; |
| 536 | |
| 537 | // Don't hoist an instruction that uses or defines a physical register. |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 538 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 539 | if (MO.isUse()) { |
| 540 | // 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] | 541 | // and we can freely move its uses. Alternatively, if it's allocatable, |
| 542 | // it could get allocated to something with a def during allocation. |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 543 | if (!RegInfo->def_empty(Reg)) |
| 544 | return false; |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 545 | if (AllocatableSet.test(Reg)) |
| 546 | return false; |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 547 | // Check for a def among the register's aliases too. |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 548 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 549 | unsigned AliasReg = *Alias; |
| 550 | if (!RegInfo->def_empty(AliasReg)) |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 551 | return false; |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 552 | if (AllocatableSet.test(AliasReg)) |
| 553 | return false; |
| 554 | } |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 555 | // Otherwise it's safe to move. |
| 556 | continue; |
| 557 | } else if (!MO.isDead()) { |
| 558 | // A def that isn't dead. We can't move it. |
| 559 | return false; |
Dan Gohman | a363a9b | 2010-02-28 00:08:44 +0000 | [diff] [blame] | 560 | } else if (CurLoop->getHeader()->isLiveIn(Reg)) { |
| 561 | // If the reg is live into the loop, we can't hoist an instruction |
| 562 | // which would clobber it. |
| 563 | return false; |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 564 | } |
| 565 | } |
Bill Wendling | fb018d0 | 2008-08-20 20:32:05 +0000 | [diff] [blame] | 566 | |
| 567 | if (!MO.isUse()) |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 568 | continue; |
| 569 | |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 570 | assert(RegInfo->getVRegDef(Reg) && |
| 571 | "Machine instr not mapped for this vreg?!"); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 572 | |
| 573 | // If the loop contains the definition of an operand, then the instruction |
| 574 | // isn't loop invariant. |
Dan Gohman | 92329c7 | 2009-12-18 01:24:09 +0000 | [diff] [blame] | 575 | if (CurLoop->contains(RegInfo->getVRegDef(Reg))) |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 576 | return false; |
| 577 | } |
| 578 | |
| 579 | // If we got this far, the instruction is loop invariant! |
| 580 | return true; |
| 581 | } |
| 582 | |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 583 | |
| 584 | /// HasPHIUses - Return true if the specified register has any PHI use. |
| 585 | static bool HasPHIUses(unsigned Reg, MachineRegisterInfo *RegInfo) { |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 586 | for (MachineRegisterInfo::use_iterator UI = RegInfo->use_begin(Reg), |
| 587 | UE = RegInfo->use_end(); UI != UE; ++UI) { |
| 588 | MachineInstr *UseMI = &*UI; |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 589 | if (UseMI->isPHI()) |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 590 | return true; |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 591 | } |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 592 | return false; |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 595 | /// isLoadFromConstantMemory - Return true if the given instruction is a |
| 596 | /// load from constant memory. Machine LICM will hoist these even if they are |
| 597 | /// not re-materializable. |
| 598 | bool MachineLICM::isLoadFromConstantMemory(MachineInstr *MI) { |
| 599 | if (!MI->getDesc().mayLoad()) return false; |
| 600 | if (!MI->hasOneMemOperand()) return false; |
| 601 | MachineMemOperand *MMO = *MI->memoperands_begin(); |
| 602 | if (MMO->isVolatile()) return false; |
| 603 | if (!MMO->getValue()) return false; |
| 604 | const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(MMO->getValue()); |
| 605 | if (PSV) { |
| 606 | MachineFunction &MF = *MI->getParent()->getParent(); |
| 607 | return PSV->isConstant(MF.getFrameInfo()); |
| 608 | } else { |
| 609 | return AA->pointsToConstantMemory(MMO->getValue()); |
| 610 | } |
| 611 | } |
| 612 | |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 613 | /// IsProfitableToHoist - Return true if it is potentially profitable to hoist |
| 614 | /// the given loop invariant. |
Evan Cheng | c26abd9 | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 615 | bool MachineLICM::IsProfitableToHoist(MachineInstr &MI) { |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 616 | // FIXME: For now, only hoist re-materilizable instructions. LICM will |
| 617 | // increase register pressure. We want to make sure it doesn't increase |
| 618 | // spilling. |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 619 | // Also hoist loads from constant memory, e.g. load from stubs, GOT. Hoisting |
| 620 | // these tend to help performance in low register pressure situation. The |
| 621 | // trade off is it may cause spill in high pressure situation. It will end up |
| 622 | // adding a store in the loop preheader. But the reload is no more expensive. |
| 623 | // The side benefit is these loads are frequently CSE'ed. |
| 624 | if (!TII->isTriviallyReMaterializable(&MI, AA)) { |
Evan Cheng | c26abd9 | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 625 | if (!isLoadFromConstantMemory(&MI)) |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 626 | return false; |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 627 | } |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 628 | |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 629 | // If result(s) of this instruction is used by PHIs, then don't hoist it. |
| 630 | // The presence of joins makes it difficult for current register allocator |
| 631 | // implementation to perform remat. |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 632 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 633 | const MachineOperand &MO = MI.getOperand(i); |
| 634 | if (!MO.isReg() || !MO.isDef()) |
| 635 | continue; |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 636 | if (HasPHIUses(MO.getReg(), RegInfo)) |
| 637 | return false; |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 638 | } |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 639 | |
| 640 | return true; |
| 641 | } |
| 642 | |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 643 | MachineInstr *MachineLICM::ExtractHoistableLoad(MachineInstr *MI) { |
Evan Cheng | e95f319 | 2010-10-08 18:59:19 +0000 | [diff] [blame] | 644 | // Don't unfold simple loads. |
| 645 | if (MI->getDesc().canFoldAsLoad()) |
| 646 | return 0; |
| 647 | |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 648 | // If not, we may be able to unfold a load and hoist that. |
| 649 | // First test whether the instruction is loading from an amenable |
| 650 | // memory location. |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 651 | if (!isLoadFromConstantMemory(MI)) |
| 652 | return 0; |
| 653 | |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 654 | // Next determine the register class for a temporary register. |
Dan Gohman | 0115e16 | 2009-10-30 22:18:41 +0000 | [diff] [blame] | 655 | unsigned LoadRegIndex; |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 656 | unsigned NewOpc = |
| 657 | TII->getOpcodeAfterMemoryUnfold(MI->getOpcode(), |
| 658 | /*UnfoldLoad=*/true, |
Dan Gohman | 0115e16 | 2009-10-30 22:18:41 +0000 | [diff] [blame] | 659 | /*UnfoldStore=*/false, |
| 660 | &LoadRegIndex); |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 661 | if (NewOpc == 0) return 0; |
| 662 | const TargetInstrDesc &TID = TII->get(NewOpc); |
| 663 | if (TID.getNumDefs() != 1) return 0; |
Dan Gohman | 0115e16 | 2009-10-30 22:18:41 +0000 | [diff] [blame] | 664 | const TargetRegisterClass *RC = TID.OpInfo[LoadRegIndex].getRegClass(TRI); |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 665 | // Ok, we're unfolding. Create a temporary register and do the unfold. |
| 666 | unsigned Reg = RegInfo->createVirtualRegister(RC); |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 667 | |
| 668 | MachineFunction &MF = *MI->getParent()->getParent(); |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 669 | SmallVector<MachineInstr *, 2> NewMIs; |
| 670 | bool Success = |
| 671 | TII->unfoldMemoryOperand(MF, MI, Reg, |
| 672 | /*UnfoldLoad=*/true, /*UnfoldStore=*/false, |
| 673 | NewMIs); |
| 674 | (void)Success; |
| 675 | assert(Success && |
| 676 | "unfoldMemoryOperand failed when getOpcodeAfterMemoryUnfold " |
| 677 | "succeeded!"); |
| 678 | assert(NewMIs.size() == 2 && |
| 679 | "Unfolded a load into multiple instructions!"); |
| 680 | MachineBasicBlock *MBB = MI->getParent(); |
| 681 | MBB->insert(MI, NewMIs[0]); |
| 682 | MBB->insert(MI, NewMIs[1]); |
| 683 | // If unfolding produced a load that wasn't loop-invariant or profitable to |
| 684 | // hoist, discard the new instructions and bail. |
Evan Cheng | c26abd9 | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 685 | if (!IsLoopInvariantInst(*NewMIs[0]) || !IsProfitableToHoist(*NewMIs[0])) { |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 686 | NewMIs[0]->eraseFromParent(); |
| 687 | NewMIs[1]->eraseFromParent(); |
| 688 | return 0; |
| 689 | } |
| 690 | // Otherwise we successfully unfolded a load that we can hoist. |
| 691 | MI->eraseFromParent(); |
| 692 | return NewMIs[0]; |
| 693 | } |
| 694 | |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 695 | void MachineLICM::InitCSEMap(MachineBasicBlock *BB) { |
| 696 | for (MachineBasicBlock::iterator I = BB->begin(),E = BB->end(); I != E; ++I) { |
| 697 | const MachineInstr *MI = &*I; |
| 698 | // FIXME: For now, only hoist re-materilizable instructions. LICM will |
| 699 | // increase register pressure. We want to make sure it doesn't increase |
| 700 | // spilling. |
| 701 | if (TII->isTriviallyReMaterializable(MI, AA)) { |
| 702 | unsigned Opcode = MI->getOpcode(); |
| 703 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator |
| 704 | CI = CSEMap.find(Opcode); |
| 705 | if (CI != CSEMap.end()) |
| 706 | CI->second.push_back(MI); |
| 707 | else { |
| 708 | std::vector<const MachineInstr*> CSEMIs; |
| 709 | CSEMIs.push_back(MI); |
| 710 | CSEMap.insert(std::make_pair(Opcode, CSEMIs)); |
| 711 | } |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 716 | const MachineInstr* |
| 717 | MachineLICM::LookForDuplicate(const MachineInstr *MI, |
| 718 | std::vector<const MachineInstr*> &PrevMIs) { |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 719 | for (unsigned i = 0, e = PrevMIs.size(); i != e; ++i) { |
| 720 | const MachineInstr *PrevMI = PrevMIs[i]; |
Evan Cheng | 506049f | 2010-03-03 01:44:33 +0000 | [diff] [blame] | 721 | if (TII->produceSameValue(MI, PrevMI)) |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 722 | return PrevMI; |
| 723 | } |
| 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | bool MachineLICM::EliminateCSE(MachineInstr *MI, |
| 728 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator &CI) { |
Evan Cheng | db89809 | 2010-07-14 01:22:19 +0000 | [diff] [blame] | 729 | // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate |
| 730 | // the undef property onto uses. |
| 731 | if (CI == CSEMap.end() || MI->isImplicitDef()) |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 732 | return false; |
| 733 | |
| 734 | if (const MachineInstr *Dup = LookForDuplicate(MI, CI->second)) { |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 735 | DEBUG(dbgs() << "CSEing " << *MI << " with " << *Dup); |
Dan Gohman | 6ac33b4 | 2010-02-28 01:33:43 +0000 | [diff] [blame] | 736 | |
| 737 | // Replace virtual registers defined by MI by their counterparts defined |
| 738 | // by Dup. |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 739 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 740 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | 6ac33b4 | 2010-02-28 01:33:43 +0000 | [diff] [blame] | 741 | |
| 742 | // Physical registers may not differ here. |
| 743 | assert((!MO.isReg() || MO.getReg() == 0 || |
| 744 | !TargetRegisterInfo::isPhysicalRegister(MO.getReg()) || |
| 745 | MO.getReg() == Dup->getOperand(i).getReg()) && |
| 746 | "Instructions with different phys regs are not identical!"); |
| 747 | |
| 748 | if (MO.isReg() && MO.isDef() && |
Dan Gohman | e6cd757 | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 749 | !TargetRegisterInfo::isPhysicalRegister(MO.getReg())) { |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 750 | RegInfo->replaceRegWith(MO.getReg(), Dup->getOperand(i).getReg()); |
Dan Gohman | e6cd757 | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 751 | RegInfo->clearKillFlags(Dup->getOperand(i).getReg()); |
| 752 | } |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 753 | } |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 754 | MI->eraseFromParent(); |
| 755 | ++NumCSEed; |
| 756 | return true; |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 757 | } |
| 758 | return false; |
| 759 | } |
| 760 | |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 761 | /// Hoist - When an instruction is found to use only loop invariant operands |
| 762 | /// 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] | 763 | /// |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 764 | void MachineLICM::Hoist(MachineInstr *MI) { |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 765 | MachineBasicBlock *Preheader = getCurPreheader(); |
| 766 | if (!Preheader) return; |
| 767 | |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 768 | // First check whether we should hoist this instruction. |
Evan Cheng | c26abd9 | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 769 | if (!IsLoopInvariantInst(*MI) || !IsProfitableToHoist(*MI)) { |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 770 | // If not, try unfolding a hoistable load. |
| 771 | MI = ExtractHoistableLoad(MI); |
| 772 | if (!MI) return; |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 773 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 774 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 775 | // Now move the instructions to the predecessor, inserting it before any |
| 776 | // terminator instructions. |
| 777 | DEBUG({ |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 778 | dbgs() << "Hoisting " << *MI; |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 779 | if (Preheader->getBasicBlock()) |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 780 | dbgs() << " to MachineBasicBlock " |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 781 | << Preheader->getName(); |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 782 | if (MI->getParent()->getBasicBlock()) |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 783 | dbgs() << " from MachineBasicBlock " |
Jakob Stoklund Olesen | 324da76 | 2009-11-20 01:17:03 +0000 | [diff] [blame] | 784 | << MI->getParent()->getName(); |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 785 | dbgs() << "\n"; |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 786 | }); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 787 | |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 788 | // If this is the first instruction being hoisted to the preheader, |
| 789 | // initialize the CSE map with potential common expressions. |
Evan Cheng | 82e0a1a | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 790 | if (FirstInLoop) { |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 791 | InitCSEMap(Preheader); |
Evan Cheng | 82e0a1a | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 792 | FirstInLoop = false; |
| 793 | } |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 794 | |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 795 | // Look for opportunity to CSE the hoisted instruction. |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 796 | unsigned Opcode = MI->getOpcode(); |
| 797 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator |
| 798 | CI = CSEMap.find(Opcode); |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 799 | if (!EliminateCSE(MI, CI)) { |
| 800 | // Otherwise, splice the instruction to the preheader. |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 801 | Preheader->splice(Preheader->getFirstTerminator(),MI->getParent(),MI); |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 802 | |
Dan Gohman | e6cd757 | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 803 | // Clear the kill flags of any register this instruction defines, |
| 804 | // since they may need to be live throughout the entire loop |
| 805 | // rather than just live for part of it. |
| 806 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 807 | MachineOperand &MO = MI->getOperand(i); |
| 808 | if (MO.isReg() && MO.isDef() && !MO.isDead()) |
| 809 | RegInfo->clearKillFlags(MO.getReg()); |
| 810 | } |
| 811 | |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 812 | // Add to the CSE map. |
| 813 | if (CI != CSEMap.end()) |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 814 | CI->second.push_back(MI); |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 815 | else { |
| 816 | std::vector<const MachineInstr*> CSEMIs; |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 817 | CSEMIs.push_back(MI); |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 818 | CSEMap.insert(std::make_pair(Opcode, CSEMIs)); |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 819 | } |
| 820 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 821 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 822 | ++NumHoisted; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 823 | Changed = true; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 824 | } |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 825 | |
| 826 | MachineBasicBlock *MachineLICM::getCurPreheader() { |
| 827 | // Determine the block to which to hoist instructions. If we can't find a |
| 828 | // suitable loop predecessor, we can't do any hoisting. |
| 829 | |
| 830 | // If we've tried to get a preheader and failed, don't try again. |
| 831 | if (CurPreheader == reinterpret_cast<MachineBasicBlock *>(-1)) |
| 832 | return 0; |
| 833 | |
| 834 | if (!CurPreheader) { |
| 835 | CurPreheader = CurLoop->getLoopPreheader(); |
| 836 | if (!CurPreheader) { |
| 837 | MachineBasicBlock *Pred = CurLoop->getLoopPredecessor(); |
| 838 | if (!Pred) { |
| 839 | CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1); |
| 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | CurPreheader = Pred->SplitCriticalEdge(CurLoop->getHeader(), this); |
| 844 | if (!CurPreheader) { |
| 845 | CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1); |
| 846 | return 0; |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | return CurPreheader; |
| 851 | } |