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" |
Evan Cheng | ab8be96 | 2011-06-29 01:14:12 +0000 | [diff] [blame] | 31 | #include "llvm/MC/MCInstrItineraries.h" |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetLowering.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetRegisterInfo.h" |
Bill Wendling | efe2be7 | 2007-12-11 23:27:51 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetInstrInfo.h" |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetMachine.h" |
Dan Gohman | e33f44c | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 36 | #include "llvm/Analysis/AliasAnalysis.h" |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/DenseMap.h" |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/SmallSet.h" |
Chris Lattner | ac69582 | 2008-01-04 06:41:45 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/Statistic.h" |
Evan Cheng | 7007e4c | 2011-10-12 21:33:49 +0000 | [diff] [blame] | 40 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | ac69582 | 2008-01-04 06:41:45 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 42 | #include "llvm/Support/raw_ostream.h" |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 43 | using namespace llvm; |
| 44 | |
Evan Cheng | 7007e4c | 2011-10-12 21:33:49 +0000 | [diff] [blame] | 45 | static cl::opt<bool> |
| 46 | AvoidSpeculation("avoid-speculation", |
| 47 | cl::desc("MachineLICM should avoid speculation"), |
| 48 | cl::init(false), cl::Hidden); |
| 49 | |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 50 | STATISTIC(NumHoisted, |
| 51 | "Number of machine instructions hoisted out of loops"); |
| 52 | STATISTIC(NumLowRP, |
| 53 | "Number of instructions hoisted in low reg pressure situation"); |
| 54 | STATISTIC(NumHighLatency, |
| 55 | "Number of high latency instructions hoisted"); |
| 56 | STATISTIC(NumCSEed, |
| 57 | "Number of hoisted machine instructions CSEed"); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 58 | STATISTIC(NumPostRAHoisted, |
| 59 | "Number of machine instructions hoisted out of loops post regalloc"); |
Bill Wendling | b48519c | 2007-12-08 01:47:01 +0000 | [diff] [blame] | 60 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 61 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 62 | class MachineLICM : public MachineFunctionPass { |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 63 | bool PreRegAlloc; |
| 64 | |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 65 | const TargetMachine *TM; |
Bill Wendling | efe2be7 | 2007-12-11 23:27:51 +0000 | [diff] [blame] | 66 | const TargetInstrInfo *TII; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 67 | const TargetLowering *TLI; |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 68 | const TargetRegisterInfo *TRI; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 69 | const MachineFrameInfo *MFI; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 70 | MachineRegisterInfo *MRI; |
| 71 | const InstrItineraryData *InstrItins; |
Bill Wendling | 12ebf14 | 2007-12-11 19:40:06 +0000 | [diff] [blame] | 72 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 73 | // Various analyses that we use... |
Dan Gohman | e33f44c | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 74 | AliasAnalysis *AA; // Alias analysis info. |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 75 | MachineLoopInfo *MLI; // Current MachineLoopInfo |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 76 | MachineDominatorTree *DT; // Machine dominator tree for the cur loop |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 77 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 78 | // State that is updated as we process loops |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 79 | bool Changed; // True if a loop is changed. |
Evan Cheng | 82e0a1a | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 80 | bool FirstInLoop; // True if it's the first LICM in the loop. |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 81 | MachineLoop *CurLoop; // The current loop we are working on. |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 82 | MachineBasicBlock *CurPreheader; // The preheader for CurLoop. |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 83 | |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 84 | BitVector AllocatableSet; |
| 85 | |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 86 | // Track 'estimated' register pressure. |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 87 | SmallSet<unsigned, 32> RegSeen; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 88 | SmallVector<unsigned, 8> RegPressure; |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 89 | |
| 90 | // Register pressure "limit" per register class. If the pressure |
| 91 | // is higher than the limit, then it's considered high. |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 92 | SmallVector<unsigned, 8> RegLimit; |
| 93 | |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 94 | // Register pressure on path leading from loop preheader to current BB. |
| 95 | SmallVector<SmallVector<unsigned, 8>, 16> BackTrace; |
| 96 | |
Dale Johannesen | c46a5f2 | 2010-07-29 17:45:24 +0000 | [diff] [blame] | 97 | // For each opcode, keep a list of potential CSE instructions. |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 98 | DenseMap<unsigned, std::vector<const MachineInstr*> > CSEMap; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 99 | |
Evan Cheng | fad6287 | 2011-10-11 23:48:44 +0000 | [diff] [blame] | 100 | enum { |
| 101 | SpeculateFalse = 0, |
| 102 | SpeculateTrue = 1, |
| 103 | SpeculateUnknown = 2 |
| 104 | }; |
| 105 | |
Devang Patel | 2e35047 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 106 | // If a MBB does not dominate loop exiting blocks then it may not safe |
| 107 | // to hoist loads from this block. |
Evan Cheng | fad6287 | 2011-10-11 23:48:44 +0000 | [diff] [blame] | 108 | // Tri-state: 0 - false, 1 - true, 2 - unknown |
| 109 | unsigned SpeculationState; |
Devang Patel | 2e35047 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 110 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 111 | public: |
| 112 | static char ID; // Pass identification, replacement for typeid |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 113 | MachineLICM() : |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 114 | MachineFunctionPass(ID), PreRegAlloc(true) { |
| 115 | initializeMachineLICMPass(*PassRegistry::getPassRegistry()); |
| 116 | } |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 117 | |
| 118 | explicit MachineLICM(bool PreRA) : |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 119 | MachineFunctionPass(ID), PreRegAlloc(PreRA) { |
| 120 | initializeMachineLICMPass(*PassRegistry::getPassRegistry()); |
| 121 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 122 | |
| 123 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 124 | |
Dan Gohman | 7224170 | 2008-12-18 01:37:56 +0000 | [diff] [blame] | 125 | const char *getPassName() const { return "Machine Instruction LICM"; } |
| 126 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 127 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 128 | AU.addRequired<MachineLoopInfo>(); |
| 129 | AU.addRequired<MachineDominatorTree>(); |
Dan Gohman | e33f44c | 2009-10-07 17:38:06 +0000 | [diff] [blame] | 130 | AU.addRequired<AliasAnalysis>(); |
Bill Wendling | d5da704 | 2008-01-04 08:48:49 +0000 | [diff] [blame] | 131 | AU.addPreserved<MachineLoopInfo>(); |
| 132 | AU.addPreserved<MachineDominatorTree>(); |
| 133 | MachineFunctionPass::getAnalysisUsage(AU); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 134 | } |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 135 | |
| 136 | virtual void releaseMemory() { |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 137 | RegSeen.clear(); |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 138 | RegPressure.clear(); |
| 139 | RegLimit.clear(); |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 140 | BackTrace.clear(); |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 141 | for (DenseMap<unsigned,std::vector<const MachineInstr*> >::iterator |
| 142 | CI = CSEMap.begin(), CE = CSEMap.end(); CI != CE; ++CI) |
| 143 | CI->second.clear(); |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 144 | CSEMap.clear(); |
| 145 | } |
| 146 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 147 | private: |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 148 | /// CandidateInfo - Keep track of information about hoisting candidates. |
| 149 | struct CandidateInfo { |
| 150 | MachineInstr *MI; |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 151 | unsigned Def; |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 152 | int FI; |
| 153 | CandidateInfo(MachineInstr *mi, unsigned def, int fi) |
| 154 | : MI(mi), Def(def), FI(fi) {} |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | /// HoistRegionPostRA - Walk the specified region of the CFG and hoist loop |
| 158 | /// invariants out to the preheader. |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 159 | void HoistRegionPostRA(); |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 160 | |
| 161 | /// HoistPostRA - When an instruction is found to only use loop invariant |
| 162 | /// operands that is safe to hoist, this instruction is called to do the |
| 163 | /// dirty work. |
| 164 | void HoistPostRA(MachineInstr *MI, unsigned Def); |
| 165 | |
| 166 | /// ProcessMI - Examine the instruction for potentai LICM candidate. Also |
| 167 | /// gather register def and frame object update information. |
| 168 | void ProcessMI(MachineInstr *MI, unsigned *PhysRegDefs, |
| 169 | SmallSet<int, 32> &StoredFIs, |
| 170 | SmallVector<CandidateInfo, 32> &Candidates); |
| 171 | |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 172 | /// AddToLiveIns - Add register 'Reg' to the livein sets of BBs in the |
| 173 | /// current loop. |
| 174 | void AddToLiveIns(unsigned Reg); |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 175 | |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 176 | /// IsLICMCandidate - Returns true if the instruction may be a suitable |
Chris Lattner | 7791080 | 2010-07-12 00:00:35 +0000 | [diff] [blame] | 177 | /// candidate for LICM. e.g. If the instruction is a call, then it's |
| 178 | /// obviously not safe to hoist it. |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 179 | bool IsLICMCandidate(MachineInstr &I); |
| 180 | |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 181 | /// IsLoopInvariantInst - Returns true if the instruction is loop |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 182 | /// invariant. I.e., all virtual register operands are defined outside of |
| 183 | /// the loop, physical registers aren't accessed (explicitly or implicitly), |
| 184 | /// and the instruction is hoistable. |
| 185 | /// |
Bill Wendling | 041b3f8 | 2007-12-08 23:58:46 +0000 | [diff] [blame] | 186 | bool IsLoopInvariantInst(MachineInstr &I); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 187 | |
Evan Cheng | d67705f | 2011-04-11 21:09:18 +0000 | [diff] [blame] | 188 | /// HasAnyPHIUse - Return true if the specified register is used by any |
| 189 | /// phi node. |
| 190 | bool HasAnyPHIUse(unsigned Reg) const; |
| 191 | |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 192 | /// HasHighOperandLatency - Compute operand latency between a def of 'Reg' |
| 193 | /// and an use in the current loop, return true if the target considered |
| 194 | /// it 'high'. |
Evan Cheng | c8141df | 2010-10-26 02:08:50 +0000 | [diff] [blame] | 195 | bool HasHighOperandLatency(MachineInstr &MI, unsigned DefIdx, |
| 196 | unsigned Reg) const; |
| 197 | |
| 198 | bool IsCheapInstruction(MachineInstr &MI) const; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 199 | |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 200 | /// CanCauseHighRegPressure - Visit BBs from header to current BB, |
| 201 | /// check if hoisting an instruction of the given cost matrix can cause high |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 202 | /// register pressure. |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 203 | bool CanCauseHighRegPressure(DenseMap<unsigned, int> &Cost); |
| 204 | |
| 205 | /// UpdateBackTraceRegPressure - Traverse the back trace from header to |
| 206 | /// the current block and update their register pressures to reflect the |
| 207 | /// effect of hoisting MI from the current block to the preheader. |
| 208 | void UpdateBackTraceRegPressure(const MachineInstr *MI); |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 209 | |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 210 | /// IsProfitableToHoist - Return true if it is potentially profitable to |
| 211 | /// hoist the given loop invariant. |
Evan Cheng | c26abd9 | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 212 | bool IsProfitableToHoist(MachineInstr &MI); |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 213 | |
Devang Patel | 2e35047 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 214 | /// IsGuaranteedToExecute - Check if this mbb is guaranteed to execute. |
| 215 | /// If not then a load from this mbb may not be safe to hoist. |
| 216 | bool IsGuaranteedToExecute(MachineBasicBlock *BB); |
| 217 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 218 | /// HoistRegion - Walk the specified region of the CFG (defined by all |
| 219 | /// blocks dominated by the specified block, and that are in the current |
| 220 | /// loop) in depth first order w.r.t the DominatorTree. This allows us to |
| 221 | /// visit definitions before uses, allowing us to hoist a loop body in one |
| 222 | /// pass without iteration. |
| 223 | /// |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 224 | void HoistRegion(MachineDomTreeNode *N, bool IsHeader = false); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 225 | |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 226 | /// getRegisterClassIDAndCost - For a given MI, register, and the operand |
| 227 | /// index, return the ID and cost of its representative register class by |
| 228 | /// reference. |
| 229 | void getRegisterClassIDAndCost(const MachineInstr *MI, |
| 230 | unsigned Reg, unsigned OpIdx, |
| 231 | unsigned &RCId, unsigned &RCCost) const; |
| 232 | |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 233 | /// InitRegPressure - Find all virtual register references that are liveout |
| 234 | /// of the preheader to initialize the starting "register pressure". Note |
| 235 | /// this does not count live through (livein but not used) registers. |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 236 | void InitRegPressure(MachineBasicBlock *BB); |
| 237 | |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 238 | /// UpdateRegPressure - Update estimate of register pressure after the |
| 239 | /// specified instruction. |
| 240 | void UpdateRegPressure(const MachineInstr *MI); |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 241 | |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 242 | /// ExtractHoistableLoad - Unfold a load from the given machineinstr if |
| 243 | /// the load itself could be hoisted. Return the unfolded and hoistable |
| 244 | /// load, or null if the load couldn't be unfolded or if it wouldn't |
| 245 | /// be hoistable. |
| 246 | MachineInstr *ExtractHoistableLoad(MachineInstr *MI); |
| 247 | |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 248 | /// LookForDuplicate - Find an instruction amount PrevMIs that is a |
| 249 | /// duplicate of MI. Return this instruction if it's found. |
| 250 | const MachineInstr *LookForDuplicate(const MachineInstr *MI, |
| 251 | std::vector<const MachineInstr*> &PrevMIs); |
| 252 | |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 253 | /// EliminateCSE - Given a LICM'ed instruction, look for an instruction on |
| 254 | /// the preheader that compute the same value. If it's found, do a RAU on |
| 255 | /// with the definition of the existing instruction rather than hoisting |
| 256 | /// the instruction to the preheader. |
| 257 | bool EliminateCSE(MachineInstr *MI, |
| 258 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator &CI); |
| 259 | |
Evan Cheng | 7efba85 | 2011-10-12 00:09:14 +0000 | [diff] [blame] | 260 | /// MayCSE - Return true if the given instruction will be CSE'd if it's |
| 261 | /// hoisted out of the loop. |
| 262 | bool MayCSE(MachineInstr *MI); |
| 263 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 264 | /// Hoist - When an instruction is found to only use loop invariant operands |
| 265 | /// that is safe to hoist, this instruction is called to do the dirty work. |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 266 | /// It returns true if the instruction is hoisted. |
| 267 | bool Hoist(MachineInstr *MI, MachineBasicBlock *Preheader); |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 268 | |
| 269 | /// InitCSEMap - Initialize the CSE map with instructions that are in the |
| 270 | /// current loop preheader that may become duplicates of instructions that |
| 271 | /// are hoisted out of the loop. |
| 272 | void InitCSEMap(MachineBasicBlock *BB); |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 273 | |
| 274 | /// getCurPreheader - Get the preheader for the current loop, splitting |
| 275 | /// a critical edge if needed. |
| 276 | MachineBasicBlock *getCurPreheader(); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 277 | }; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 278 | } // end anonymous namespace |
| 279 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 280 | char MachineLICM::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 281 | INITIALIZE_PASS_BEGIN(MachineLICM, "machinelicm", |
| 282 | "Machine Loop Invariant Code Motion", false, false) |
| 283 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) |
| 284 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
| 285 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 286 | INITIALIZE_PASS_END(MachineLICM, "machinelicm", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 287 | "Machine Loop Invariant Code Motion", false, false) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 288 | |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 289 | FunctionPass *llvm::createMachineLICMPass(bool PreRegAlloc) { |
| 290 | return new MachineLICM(PreRegAlloc); |
| 291 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 292 | |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 293 | /// LoopIsOuterMostWithPredecessor - Test if the given loop is the outer-most |
| 294 | /// loop that has a unique predecessor. |
| 295 | static bool LoopIsOuterMostWithPredecessor(MachineLoop *CurLoop) { |
Dan Gohman | aa74260 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 296 | // Check whether this loop even has a unique predecessor. |
| 297 | if (!CurLoop->getLoopPredecessor()) |
| 298 | return false; |
| 299 | // Ok, now check to see if any of its outer loops do. |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 300 | for (MachineLoop *L = CurLoop->getParentLoop(); L; L = L->getParentLoop()) |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 301 | if (L->getLoopPredecessor()) |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 302 | return false; |
Dan Gohman | aa74260 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 303 | // 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] | 304 | return true; |
| 305 | } |
| 306 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 307 | bool MachineLICM::runOnMachineFunction(MachineFunction &MF) { |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 308 | if (PreRegAlloc) |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 309 | DEBUG(dbgs() << "******** Pre-regalloc Machine LICM: "); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 310 | else |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 311 | DEBUG(dbgs() << "******** Post-regalloc Machine LICM: "); |
| 312 | DEBUG(dbgs() << MF.getFunction()->getName() << " ********\n"); |
Bill Wendling | a17ad59 | 2007-12-11 22:22:22 +0000 | [diff] [blame] | 313 | |
Evan Cheng | 82e0a1a | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 314 | Changed = FirstInLoop = false; |
Bill Wendling | acb04ec | 2008-08-31 02:30:23 +0000 | [diff] [blame] | 315 | TM = &MF.getTarget(); |
Bill Wendling | 9258cd3 | 2008-01-02 19:32:43 +0000 | [diff] [blame] | 316 | TII = TM->getInstrInfo(); |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 317 | TLI = TM->getTargetLowering(); |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 318 | TRI = TM->getRegisterInfo(); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 319 | MFI = MF.getFrameInfo(); |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 320 | MRI = &MF.getRegInfo(); |
| 321 | InstrItins = TM->getInstrItineraryData(); |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 322 | AllocatableSet = TRI->getAllocatableSet(MF); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 323 | |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 324 | if (PreRegAlloc) { |
| 325 | // Estimate register pressure during pre-regalloc pass. |
| 326 | unsigned NumRC = TRI->getNumRegClasses(); |
| 327 | RegPressure.resize(NumRC); |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 328 | std::fill(RegPressure.begin(), RegPressure.end(), 0); |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 329 | RegLimit.resize(NumRC); |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 330 | for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(), |
| 331 | E = TRI->regclass_end(); I != E; ++I) |
Cameron Zwarich | be2119e | 2011-03-07 21:56:36 +0000 | [diff] [blame] | 332 | RegLimit[(*I)->getID()] = TRI->getRegPressureLimit(*I, MF); |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 335 | // Get our Loop information... |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 336 | MLI = &getAnalysis<MachineLoopInfo>(); |
| 337 | DT = &getAnalysis<MachineDominatorTree>(); |
| 338 | AA = &getAnalysis<AliasAnalysis>(); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 339 | |
Dan Gohman | aa74260 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 340 | SmallVector<MachineLoop *, 8> Worklist(MLI->begin(), MLI->end()); |
| 341 | while (!Worklist.empty()) { |
| 342 | CurLoop = Worklist.pop_back_val(); |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 343 | CurPreheader = 0; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 344 | |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 345 | // If this is done before regalloc, only visit outer-most preheader-sporting |
| 346 | // loops. |
Dan Gohman | aa74260 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 347 | if (PreRegAlloc && !LoopIsOuterMostWithPredecessor(CurLoop)) { |
| 348 | Worklist.append(CurLoop->begin(), CurLoop->end()); |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 349 | continue; |
Dan Gohman | aa74260 | 2010-07-09 18:49:45 +0000 | [diff] [blame] | 350 | } |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 351 | |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 352 | if (!PreRegAlloc) |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 353 | HoistRegionPostRA(); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 354 | else { |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 355 | // CSEMap is initialized for loop header when the first instruction is |
| 356 | // being hoisted. |
| 357 | MachineDomTreeNode *N = DT->getNode(CurLoop->getHeader()); |
Evan Cheng | 82e0a1a | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 358 | FirstInLoop = true; |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 359 | HoistRegion(N, true); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 360 | CSEMap.clear(); |
| 361 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | return Changed; |
| 365 | } |
| 366 | |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 367 | /// InstructionStoresToFI - Return true if instruction stores to the |
| 368 | /// specified frame. |
| 369 | static bool InstructionStoresToFI(const MachineInstr *MI, int FI) { |
| 370 | for (MachineInstr::mmo_iterator o = MI->memoperands_begin(), |
| 371 | oe = MI->memoperands_end(); o != oe; ++o) { |
| 372 | if (!(*o)->isStore() || !(*o)->getValue()) |
| 373 | continue; |
| 374 | if (const FixedStackPseudoSourceValue *Value = |
| 375 | dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) { |
| 376 | if (Value->getFrameIndex() == FI) |
| 377 | return true; |
| 378 | } |
| 379 | } |
| 380 | return false; |
| 381 | } |
| 382 | |
| 383 | /// ProcessMI - Examine the instruction for potentai LICM candidate. Also |
| 384 | /// gather register def and frame object update information. |
| 385 | void MachineLICM::ProcessMI(MachineInstr *MI, |
| 386 | unsigned *PhysRegDefs, |
| 387 | SmallSet<int, 32> &StoredFIs, |
| 388 | SmallVector<CandidateInfo, 32> &Candidates) { |
| 389 | bool RuledOut = false; |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 390 | bool HasNonInvariantUse = false; |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 391 | unsigned Def = 0; |
| 392 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 393 | const MachineOperand &MO = MI->getOperand(i); |
| 394 | if (MO.isFI()) { |
| 395 | // Remember if the instruction stores to the frame index. |
| 396 | int FI = MO.getIndex(); |
| 397 | if (!StoredFIs.count(FI) && |
| 398 | MFI->isSpillSlotObjectIndex(FI) && |
| 399 | InstructionStoresToFI(MI, FI)) |
| 400 | StoredFIs.insert(FI); |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 401 | HasNonInvariantUse = true; |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 402 | continue; |
| 403 | } |
| 404 | |
| 405 | if (!MO.isReg()) |
| 406 | continue; |
| 407 | unsigned Reg = MO.getReg(); |
| 408 | if (!Reg) |
| 409 | continue; |
| 410 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && |
| 411 | "Not expecting virtual register!"); |
| 412 | |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 413 | if (!MO.isDef()) { |
Evan Cheng | 6327537 | 2010-04-13 22:13:34 +0000 | [diff] [blame] | 414 | if (Reg && PhysRegDefs[Reg]) |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 415 | // If it's using a non-loop-invariant register, then it's obviously not |
| 416 | // safe to hoist. |
| 417 | HasNonInvariantUse = true; |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 418 | continue; |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 419 | } |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 420 | |
| 421 | if (MO.isImplicit()) { |
| 422 | ++PhysRegDefs[Reg]; |
| 423 | for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) |
| 424 | ++PhysRegDefs[*AS]; |
| 425 | if (!MO.isDead()) |
| 426 | // Non-dead implicit def? This cannot be hoisted. |
| 427 | RuledOut = true; |
| 428 | // No need to check if a dead implicit def is also defined by |
| 429 | // another instruction. |
| 430 | continue; |
| 431 | } |
| 432 | |
| 433 | // FIXME: For now, avoid instructions with multiple defs, unless |
| 434 | // it's a dead implicit def. |
| 435 | if (Def) |
| 436 | RuledOut = true; |
| 437 | else |
| 438 | Def = Reg; |
| 439 | |
| 440 | // If we have already seen another instruction that defines the same |
| 441 | // register, then this is not safe. |
| 442 | if (++PhysRegDefs[Reg] > 1) |
| 443 | // MI defined register is seen defined by another instruction in |
| 444 | // the loop, it cannot be a LICM candidate. |
| 445 | RuledOut = true; |
| 446 | for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) |
| 447 | if (++PhysRegDefs[*AS] > 1) |
| 448 | RuledOut = true; |
| 449 | } |
| 450 | |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 451 | // Only consider reloads for now and remats which do not have register |
| 452 | // operands. FIXME: Consider unfold load folding instructions. |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 453 | if (Def && !RuledOut) { |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 454 | int FI = INT_MIN; |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 455 | if ((!HasNonInvariantUse && IsLICMCandidate(*MI)) || |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 456 | (TII->isLoadFromStackSlot(MI, FI) && MFI->isSpillSlotObjectIndex(FI))) |
| 457 | Candidates.push_back(CandidateInfo(MI, Def, FI)); |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
| 461 | /// HoistRegionPostRA - Walk the specified region of the CFG and hoist loop |
| 462 | /// invariants out to the preheader. |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 463 | void MachineLICM::HoistRegionPostRA() { |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 464 | unsigned NumRegs = TRI->getNumRegs(); |
| 465 | unsigned *PhysRegDefs = new unsigned[NumRegs]; |
| 466 | std::fill(PhysRegDefs, PhysRegDefs + NumRegs, 0); |
| 467 | |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 468 | SmallVector<CandidateInfo, 32> Candidates; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 469 | SmallSet<int, 32> StoredFIs; |
| 470 | |
| 471 | // Walk the entire region, count number of defs for each register, and |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 472 | // collect potential LICM candidates. |
| 473 | const std::vector<MachineBasicBlock*> Blocks = CurLoop->getBlocks(); |
| 474 | for (unsigned i = 0, e = Blocks.size(); i != e; ++i) { |
| 475 | MachineBasicBlock *BB = Blocks[i]; |
Bill Wendling | a2e8791 | 2011-10-12 02:58:01 +0000 | [diff] [blame] | 476 | |
| 477 | // If the header of the loop containing this basic block is a landing pad, |
| 478 | // then don't try to hoist instructions out of this loop. |
| 479 | const MachineLoop *ML = MLI->getLoopFor(BB); |
| 480 | if (ML && ML->getHeader()->isLandingPad()) continue; |
| 481 | |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 482 | // Conservatively treat live-in's as an external def. |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 483 | // FIXME: That means a reload that're reused in successor block(s) will not |
| 484 | // be LICM'ed. |
Dan Gohman | 81bf03e | 2010-04-13 16:57:55 +0000 | [diff] [blame] | 485 | for (MachineBasicBlock::livein_iterator I = BB->livein_begin(), |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 486 | E = BB->livein_end(); I != E; ++I) { |
| 487 | unsigned Reg = *I; |
| 488 | ++PhysRegDefs[Reg]; |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 489 | for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS) |
| 490 | ++PhysRegDefs[*AS]; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Evan Cheng | fad6287 | 2011-10-11 23:48:44 +0000 | [diff] [blame] | 493 | SpeculationState = SpeculateUnknown; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 494 | for (MachineBasicBlock::iterator |
| 495 | MII = BB->begin(), E = BB->end(); MII != E; ++MII) { |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 496 | MachineInstr *MI = &*MII; |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 497 | ProcessMI(MI, PhysRegDefs, StoredFIs, Candidates); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 498 | } |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 499 | } |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 500 | |
| 501 | // Now evaluate whether the potential candidates qualify. |
| 502 | // 1. Check if the candidate defined register is defined by another |
| 503 | // instruction in the loop. |
| 504 | // 2. If the candidate is a load from stack slot (always true for now), |
| 505 | // check if the slot is stored anywhere in the loop. |
| 506 | for (unsigned i = 0, e = Candidates.size(); i != e; ++i) { |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 507 | if (Candidates[i].FI != INT_MIN && |
| 508 | StoredFIs.count(Candidates[i].FI)) |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 509 | continue; |
| 510 | |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 511 | if (PhysRegDefs[Candidates[i].Def] == 1) { |
| 512 | bool Safe = true; |
| 513 | MachineInstr *MI = Candidates[i].MI; |
Evan Cheng | c15d913 | 2010-04-13 20:25:29 +0000 | [diff] [blame] | 514 | for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) { |
| 515 | const MachineOperand &MO = MI->getOperand(j); |
Evan Cheng | 6327537 | 2010-04-13 22:13:34 +0000 | [diff] [blame] | 516 | if (!MO.isReg() || MO.isDef() || !MO.getReg()) |
Evan Cheng | aeb2f4a | 2010-04-13 20:21:05 +0000 | [diff] [blame] | 517 | continue; |
| 518 | if (PhysRegDefs[MO.getReg()]) { |
| 519 | // If it's using a non-loop-invariant register, then it's obviously |
| 520 | // not safe to hoist. |
| 521 | Safe = false; |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | if (Safe) |
| 526 | HoistPostRA(MI, Candidates[i].Def); |
| 527 | } |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 528 | } |
Benjamin Kramer | 678d9b7 | 2010-04-12 11:38:35 +0000 | [diff] [blame] | 529 | |
| 530 | delete[] PhysRegDefs; |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Jakob Stoklund Olesen | 9196ab6 | 2010-04-20 18:45:47 +0000 | [diff] [blame] | 533 | /// AddToLiveIns - Add register 'Reg' to the livein sets of BBs in the current |
| 534 | /// 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] | 535 | void MachineLICM::AddToLiveIns(unsigned Reg) { |
| 536 | const std::vector<MachineBasicBlock*> Blocks = CurLoop->getBlocks(); |
Jakob Stoklund Olesen | 9196ab6 | 2010-04-20 18:45:47 +0000 | [diff] [blame] | 537 | for (unsigned i = 0, e = Blocks.size(); i != e; ++i) { |
| 538 | MachineBasicBlock *BB = Blocks[i]; |
| 539 | if (!BB->isLiveIn(Reg)) |
| 540 | BB->addLiveIn(Reg); |
| 541 | for (MachineBasicBlock::iterator |
| 542 | MII = BB->begin(), E = BB->end(); MII != E; ++MII) { |
| 543 | MachineInstr *MI = &*MII; |
| 544 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 545 | MachineOperand &MO = MI->getOperand(i); |
| 546 | if (!MO.isReg() || !MO.getReg() || MO.isDef()) continue; |
| 547 | if (MO.getReg() == Reg || TRI->isSuperRegister(Reg, MO.getReg())) |
| 548 | MO.setIsKill(false); |
| 549 | } |
| 550 | } |
| 551 | } |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | /// HoistPostRA - When an instruction is found to only use loop invariant |
| 555 | /// operands that is safe to hoist, this instruction is called to do the |
| 556 | /// dirty work. |
| 557 | void MachineLICM::HoistPostRA(MachineInstr *MI, unsigned Def) { |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 558 | MachineBasicBlock *Preheader = getCurPreheader(); |
| 559 | if (!Preheader) return; |
| 560 | |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 561 | // Now move the instructions to the predecessor, inserting it before any |
| 562 | // terminator instructions. |
| 563 | DEBUG({ |
| 564 | dbgs() << "Hoisting " << *MI; |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 565 | if (Preheader->getBasicBlock()) |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 566 | dbgs() << " to MachineBasicBlock " |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 567 | << Preheader->getName(); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 568 | if (MI->getParent()->getBasicBlock()) |
| 569 | dbgs() << " from MachineBasicBlock " |
| 570 | << MI->getParent()->getName(); |
| 571 | dbgs() << "\n"; |
| 572 | }); |
| 573 | |
| 574 | // Splice the instruction to the preheader. |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 575 | MachineBasicBlock *MBB = MI->getParent(); |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 576 | Preheader->splice(Preheader->getFirstTerminator(), MBB, MI); |
Evan Cheng | 4038f9c | 2010-04-08 01:03:47 +0000 | [diff] [blame] | 577 | |
Evan Cheng | 94d1d9c | 2010-04-17 07:07:11 +0000 | [diff] [blame] | 578 | // Add register to livein list to all the BBs in the current loop since a |
| 579 | // loop invariant must be kept live throughout the whole loop. This is |
| 580 | // important to ensure later passes do not scavenge the def register. |
| 581 | AddToLiveIns(Def); |
Evan Cheng | d94671a | 2010-04-07 00:41:17 +0000 | [diff] [blame] | 582 | |
| 583 | ++NumPostRAHoisted; |
| 584 | Changed = true; |
| 585 | } |
| 586 | |
Devang Patel | 2e35047 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 587 | // IsGuaranteedToExecute - Check if this mbb is guaranteed to execute. |
| 588 | // If not then a load from this mbb may not be safe to hoist. |
| 589 | bool MachineLICM::IsGuaranteedToExecute(MachineBasicBlock *BB) { |
Evan Cheng | fad6287 | 2011-10-11 23:48:44 +0000 | [diff] [blame] | 590 | if (SpeculationState != SpeculateUnknown) |
| 591 | return SpeculationState == SpeculateFalse; |
| 592 | |
Devang Patel | 2e35047 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 593 | if (BB != CurLoop->getHeader()) { |
| 594 | // Check loop exiting blocks. |
| 595 | SmallVector<MachineBasicBlock*, 8> CurrentLoopExitingBlocks; |
| 596 | CurLoop->getExitingBlocks(CurrentLoopExitingBlocks); |
| 597 | for (unsigned i = 0, e = CurrentLoopExitingBlocks.size(); i != e; ++i) |
| 598 | if (!DT->dominates(BB, CurrentLoopExitingBlocks[i])) { |
Nick Lewycky | ea3abd5 | 2011-10-13 01:09:50 +0000 | [diff] [blame] | 599 | SpeculationState = SpeculateTrue; |
| 600 | return false; |
Devang Patel | 2e35047 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
Evan Cheng | fad6287 | 2011-10-11 23:48:44 +0000 | [diff] [blame] | 604 | SpeculationState = SpeculateFalse; |
| 605 | return true; |
Devang Patel | 2e35047 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 608 | /// HoistRegion - Walk the specified region of the CFG (defined by all blocks |
| 609 | /// dominated by the specified block, and that are in the current loop) in depth |
| 610 | /// first order w.r.t the DominatorTree. This allows us to visit definitions |
| 611 | /// before uses, allowing us to hoist a loop body in one pass without iteration. |
| 612 | /// |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 613 | void MachineLICM::HoistRegion(MachineDomTreeNode *N, bool IsHeader) { |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 614 | assert(N != 0 && "Null dominator tree node?"); |
| 615 | MachineBasicBlock *BB = N->getBlock(); |
| 616 | |
Bill Wendling | a2e8791 | 2011-10-12 02:58:01 +0000 | [diff] [blame] | 617 | // If the header of the loop containing this basic block is a landing pad, |
| 618 | // then don't try to hoist instructions out of this loop. |
| 619 | const MachineLoop *ML = MLI->getLoopFor(BB); |
| 620 | if (ML && ML->getHeader()->isLandingPad()) return; |
| 621 | |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 622 | // If this subregion is not in the top level loop at all, exit. |
| 623 | if (!CurLoop->contains(BB)) return; |
| 624 | |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 625 | MachineBasicBlock *Preheader = getCurPreheader(); |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 626 | if (!Preheader) |
| 627 | return; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 628 | |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 629 | if (IsHeader) { |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 630 | // Compute registers which are livein into the loop headers. |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 631 | RegSeen.clear(); |
| 632 | BackTrace.clear(); |
| 633 | InitRegPressure(Preheader); |
Daniel Dunbar | 9869413 | 2010-10-19 17:14:24 +0000 | [diff] [blame] | 634 | } |
Evan Cheng | 11e8b74 | 2010-10-19 00:55:07 +0000 | [diff] [blame] | 635 | |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 636 | // Remember livein register pressure. |
| 637 | BackTrace.push_back(RegPressure); |
| 638 | |
Evan Cheng | fad6287 | 2011-10-11 23:48:44 +0000 | [diff] [blame] | 639 | SpeculationState = SpeculateUnknown; |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 640 | for (MachineBasicBlock::iterator |
| 641 | MII = BB->begin(), E = BB->end(); MII != E; ) { |
| 642 | MachineBasicBlock::iterator NextMII = MII; ++NextMII; |
| 643 | MachineInstr *MI = &*MII; |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 644 | if (!Hoist(MI, Preheader)) |
| 645 | UpdateRegPressure(MI); |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 646 | MII = NextMII; |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 647 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 648 | |
Dale Johannesen | bf1ae5e | 2010-07-20 00:50:13 +0000 | [diff] [blame] | 649 | // Don't hoist things out of a large switch statement. This often causes |
| 650 | // code to be hoisted that wasn't going to be executed, and increases |
| 651 | // register pressure in a situation where it's likely to matter. |
Dale Johannesen | 21d35c1 | 2010-07-20 21:29:12 +0000 | [diff] [blame] | 652 | if (BB->succ_size() < 25) { |
| 653 | const std::vector<MachineDomTreeNode*> &Children = N->getChildren(); |
Dale Johannesen | bf1ae5e | 2010-07-20 00:50:13 +0000 | [diff] [blame] | 654 | for (unsigned I = 0, E = Children.size(); I != E; ++I) |
| 655 | HoistRegion(Children[I]); |
Dale Johannesen | 21d35c1 | 2010-07-20 21:29:12 +0000 | [diff] [blame] | 656 | } |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 657 | |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 658 | BackTrace.pop_back(); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 661 | static bool isOperandKill(const MachineOperand &MO, MachineRegisterInfo *MRI) { |
| 662 | return MO.isKill() || MRI->hasOneNonDBGUse(MO.getReg()); |
| 663 | } |
| 664 | |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 665 | /// getRegisterClassIDAndCost - For a given MI, register, and the operand |
| 666 | /// index, return the ID and cost of its representative register class. |
| 667 | void |
| 668 | MachineLICM::getRegisterClassIDAndCost(const MachineInstr *MI, |
| 669 | unsigned Reg, unsigned OpIdx, |
| 670 | unsigned &RCId, unsigned &RCCost) const { |
| 671 | const TargetRegisterClass *RC = MRI->getRegClass(Reg); |
| 672 | EVT VT = *RC->vt_begin(); |
| 673 | if (VT == MVT::untyped) { |
| 674 | RCId = RC->getID(); |
| 675 | RCCost = 1; |
| 676 | } else { |
| 677 | RCId = TLI->getRepRegClassFor(VT)->getID(); |
| 678 | RCCost = TLI->getRepRegClassCostFor(VT); |
| 679 | } |
| 680 | } |
| 681 | |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 682 | /// InitRegPressure - Find all virtual register references that are liveout of |
| 683 | /// the preheader to initialize the starting "register pressure". Note this |
| 684 | /// does not count live through (livein but not used) registers. |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 685 | void MachineLICM::InitRegPressure(MachineBasicBlock *BB) { |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 686 | std::fill(RegPressure.begin(), RegPressure.end(), 0); |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 687 | |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 688 | // If the preheader has only a single predecessor and it ends with a |
| 689 | // fallthrough or an unconditional branch, then scan its predecessor for live |
| 690 | // defs as well. This happens whenever the preheader is created by splitting |
| 691 | // the critical edge from the loop predecessor to the loop header. |
| 692 | if (BB->pred_size() == 1) { |
| 693 | MachineBasicBlock *TBB = 0, *FBB = 0; |
| 694 | SmallVector<MachineOperand, 4> Cond; |
| 695 | if (!TII->AnalyzeBranch(*BB, TBB, FBB, Cond, false) && Cond.empty()) |
| 696 | InitRegPressure(*BB->pred_begin()); |
| 697 | } |
| 698 | |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 699 | for (MachineBasicBlock::iterator MII = BB->begin(), E = BB->end(); |
| 700 | MII != E; ++MII) { |
| 701 | MachineInstr *MI = &*MII; |
| 702 | for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) { |
| 703 | const MachineOperand &MO = MI->getOperand(i); |
| 704 | if (!MO.isReg() || MO.isImplicit()) |
| 705 | continue; |
| 706 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 707 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 708 | continue; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 709 | |
Andrew Trick | dc986d2 | 2010-10-19 02:50:50 +0000 | [diff] [blame] | 710 | bool isNew = RegSeen.insert(Reg); |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 711 | unsigned RCId, RCCost; |
| 712 | getRegisterClassIDAndCost(MI, Reg, i, RCId, RCCost); |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 713 | if (MO.isDef()) |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 714 | RegPressure[RCId] += RCCost; |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 715 | else { |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 716 | bool isKill = isOperandKill(MO, MRI); |
| 717 | if (isNew && !isKill) |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 718 | // Haven't seen this, it must be a livein. |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 719 | RegPressure[RCId] += RCCost; |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 720 | else if (!isNew && isKill) |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 721 | RegPressure[RCId] -= RCCost; |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 722 | } |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | } |
| 726 | |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 727 | /// UpdateRegPressure - Update estimate of register pressure after the |
| 728 | /// specified instruction. |
| 729 | void MachineLICM::UpdateRegPressure(const MachineInstr *MI) { |
| 730 | if (MI->isImplicitDef()) |
| 731 | return; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 732 | |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 733 | SmallVector<unsigned, 4> Defs; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 734 | for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) { |
| 735 | const MachineOperand &MO = MI->getOperand(i); |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 736 | if (!MO.isReg() || MO.isImplicit()) |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 737 | continue; |
| 738 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 739 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 740 | continue; |
| 741 | |
Andrew Trick | dc986d2 | 2010-10-19 02:50:50 +0000 | [diff] [blame] | 742 | bool isNew = RegSeen.insert(Reg); |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 743 | if (MO.isDef()) |
| 744 | Defs.push_back(Reg); |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 745 | else if (!isNew && isOperandKill(MO, MRI)) { |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 746 | unsigned RCId, RCCost; |
| 747 | getRegisterClassIDAndCost(MI, Reg, i, RCId, RCCost); |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 748 | if (RCCost > RegPressure[RCId]) |
| 749 | RegPressure[RCId] = 0; |
| 750 | else |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 751 | RegPressure[RCId] -= RCCost; |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 752 | } |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 753 | } |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 754 | |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 755 | unsigned Idx = 0; |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 756 | while (!Defs.empty()) { |
| 757 | unsigned Reg = Defs.pop_back_val(); |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 758 | unsigned RCId, RCCost; |
| 759 | getRegisterClassIDAndCost(MI, Reg, Idx, RCId, RCCost); |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 760 | RegPressure[RCId] += RCCost; |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 761 | ++Idx; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | |
Devang Patel | 6c15fec | 2011-10-17 17:35:01 +0000 | [diff] [blame] | 765 | /// isLoadFromGOT - Return true if this machine instruction loads from |
| 766 | /// global offset table. |
| 767 | static bool isLoadFromGOT(MachineInstr &MI) { |
| 768 | assert (MI.getDesc().mayLoad() && "Expected MI that loads!"); |
| 769 | for (MachineInstr::mmo_iterator I = MI.memoperands_begin(), |
| 770 | E = MI.memoperands_end(); I != E; ++I) { |
| 771 | if (const Value *V = (*I)->getValue()) { |
| 772 | if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) |
| 773 | if (PSV == PSV->getGOT()) |
| 774 | return true; |
| 775 | } |
| 776 | } |
| 777 | return false; |
| 778 | } |
| 779 | |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 780 | /// IsLICMCandidate - Returns true if the instruction may be a suitable |
| 781 | /// candidate for LICM. e.g. If the instruction is a call, then it's obviously |
| 782 | /// not safe to hoist it. |
| 783 | bool MachineLICM::IsLICMCandidate(MachineInstr &I) { |
Chris Lattner | 7791080 | 2010-07-12 00:00:35 +0000 | [diff] [blame] | 784 | // Check if it's safe to move the instruction. |
| 785 | bool DontMoveAcrossStore = true; |
| 786 | if (!I.isSafeToMove(TII, AA, DontMoveAcrossStore)) |
Chris Lattner | a22edc8 | 2008-01-10 23:08:24 +0000 | [diff] [blame] | 787 | return false; |
Devang Patel | 2e35047 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 788 | |
| 789 | // If it is load then check if it is guaranteed to execute by making sure that |
| 790 | // it dominates all exiting blocks. If it doesn't, then there is a path out of |
Devang Patel | e6de9f3 | 2011-10-20 17:31:18 +0000 | [diff] [blame^] | 791 | // the loop which does not execute this load, so we can't hoist it. Loads |
| 792 | // from constant memory are not safe to speculate all the time, for example |
| 793 | // indexed load from a jump table. |
Devang Patel | 2e35047 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 794 | // Stores and side effects are already checked by isSafeToMove. |
Devang Patel | 6c15fec | 2011-10-17 17:35:01 +0000 | [diff] [blame] | 795 | if (I.getDesc().mayLoad() && !isLoadFromGOT(I) && |
| 796 | !IsGuaranteedToExecute(I.getParent())) |
Devang Patel | 2e35047 | 2011-10-11 18:09:58 +0000 | [diff] [blame] | 797 | return false; |
| 798 | |
Evan Cheng | 5dc57ce | 2010-04-13 18:16:00 +0000 | [diff] [blame] | 799 | return true; |
| 800 | } |
| 801 | |
| 802 | /// IsLoopInvariantInst - Returns true if the instruction is loop |
| 803 | /// invariant. I.e., all virtual register operands are defined outside of the |
| 804 | /// loop, physical registers aren't accessed explicitly, and there are no side |
| 805 | /// effects that aren't captured by the operands or other flags. |
| 806 | /// |
| 807 | bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) { |
| 808 | if (!IsLICMCandidate(I)) |
| 809 | return false; |
Bill Wendling | 074223a | 2008-03-10 08:13:01 +0000 | [diff] [blame] | 810 | |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 811 | // The instruction is loop invariant if all of its operands are. |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 812 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
| 813 | const MachineOperand &MO = I.getOperand(i); |
| 814 | |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 815 | if (!MO.isReg()) |
Bill Wendling | fb018d0 | 2008-08-20 20:32:05 +0000 | [diff] [blame] | 816 | continue; |
| 817 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 818 | unsigned Reg = MO.getReg(); |
| 819 | if (Reg == 0) continue; |
| 820 | |
| 821 | // Don't hoist an instruction that uses or defines a physical register. |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 822 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 823 | if (MO.isUse()) { |
| 824 | // 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] | 825 | // and we can freely move its uses. Alternatively, if it's allocatable, |
| 826 | // it could get allocated to something with a def during allocation. |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 827 | if (!MRI->def_empty(Reg)) |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 828 | return false; |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 829 | if (AllocatableSet.test(Reg)) |
| 830 | return false; |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 831 | // Check for a def among the register's aliases too. |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 832 | for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) { |
| 833 | unsigned AliasReg = *Alias; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 834 | if (!MRI->def_empty(AliasReg)) |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 835 | return false; |
Dan Gohman | 45094e3 | 2009-09-26 02:34:00 +0000 | [diff] [blame] | 836 | if (AllocatableSet.test(AliasReg)) |
| 837 | return false; |
| 838 | } |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 839 | // Otherwise it's safe to move. |
| 840 | continue; |
| 841 | } else if (!MO.isDead()) { |
| 842 | // A def that isn't dead. We can't move it. |
| 843 | return false; |
Dan Gohman | a363a9b | 2010-02-28 00:08:44 +0000 | [diff] [blame] | 844 | } else if (CurLoop->getHeader()->isLiveIn(Reg)) { |
| 845 | // If the reg is live into the loop, we can't hoist an instruction |
| 846 | // which would clobber it. |
| 847 | return false; |
Dan Gohman | a8fb336 | 2009-09-25 23:58:45 +0000 | [diff] [blame] | 848 | } |
| 849 | } |
Bill Wendling | fb018d0 | 2008-08-20 20:32:05 +0000 | [diff] [blame] | 850 | |
| 851 | if (!MO.isUse()) |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 852 | continue; |
| 853 | |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 854 | assert(MRI->getVRegDef(Reg) && |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 855 | "Machine instr not mapped for this vreg?!"); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 856 | |
| 857 | // If the loop contains the definition of an operand, then the instruction |
| 858 | // isn't loop invariant. |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 859 | if (CurLoop->contains(MRI->getVRegDef(Reg))) |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 860 | return false; |
| 861 | } |
| 862 | |
| 863 | // If we got this far, the instruction is loop invariant! |
| 864 | return true; |
| 865 | } |
| 866 | |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 867 | |
Evan Cheng | d67705f | 2011-04-11 21:09:18 +0000 | [diff] [blame] | 868 | /// HasAnyPHIUse - Return true if the specified register is used by any |
| 869 | /// phi node. |
| 870 | bool MachineLICM::HasAnyPHIUse(unsigned Reg) const { |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 871 | for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg), |
| 872 | UE = MRI->use_end(); UI != UE; ++UI) { |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 873 | MachineInstr *UseMI = &*UI; |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 874 | if (UseMI->isPHI()) |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 875 | return true; |
Evan Cheng | d67705f | 2011-04-11 21:09:18 +0000 | [diff] [blame] | 876 | // Look pass copies as well. |
| 877 | if (UseMI->isCopy()) { |
| 878 | unsigned Def = UseMI->getOperand(0).getReg(); |
| 879 | if (TargetRegisterInfo::isVirtualRegister(Def) && |
| 880 | HasAnyPHIUse(Def)) |
| 881 | return true; |
| 882 | } |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 883 | } |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 884 | return false; |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 887 | /// HasHighOperandLatency - Compute operand latency between a def of 'Reg' |
| 888 | /// and an use in the current loop, return true if the target considered |
| 889 | /// it 'high'. |
| 890 | bool MachineLICM::HasHighOperandLatency(MachineInstr &MI, |
Evan Cheng | c8141df | 2010-10-26 02:08:50 +0000 | [diff] [blame] | 891 | unsigned DefIdx, unsigned Reg) const { |
| 892 | if (!InstrItins || InstrItins->isEmpty() || MRI->use_nodbg_empty(Reg)) |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 893 | return false; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 894 | |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 895 | for (MachineRegisterInfo::use_nodbg_iterator I = MRI->use_nodbg_begin(Reg), |
| 896 | E = MRI->use_nodbg_end(); I != E; ++I) { |
| 897 | MachineInstr *UseMI = &*I; |
Evan Cheng | c8141df | 2010-10-26 02:08:50 +0000 | [diff] [blame] | 898 | if (UseMI->isCopyLike()) |
| 899 | continue; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 900 | if (!CurLoop->contains(UseMI->getParent())) |
| 901 | continue; |
| 902 | for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) { |
| 903 | const MachineOperand &MO = UseMI->getOperand(i); |
| 904 | if (!MO.isReg() || !MO.isUse()) |
| 905 | continue; |
| 906 | unsigned MOReg = MO.getReg(); |
| 907 | if (MOReg != Reg) |
| 908 | continue; |
| 909 | |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 910 | if (TII->hasHighOperandLatency(InstrItins, MRI, &MI, DefIdx, UseMI, i)) |
| 911 | return true; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 914 | // Only look at the first in loop use. |
| 915 | break; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 918 | return false; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Evan Cheng | c8141df | 2010-10-26 02:08:50 +0000 | [diff] [blame] | 921 | /// IsCheapInstruction - Return true if the instruction is marked "cheap" or |
| 922 | /// the operand latency between its def and a use is one or less. |
| 923 | bool MachineLICM::IsCheapInstruction(MachineInstr &MI) const { |
| 924 | if (MI.getDesc().isAsCheapAsAMove() || MI.isCopyLike()) |
| 925 | return true; |
| 926 | if (!InstrItins || InstrItins->isEmpty()) |
| 927 | return false; |
| 928 | |
| 929 | bool isCheap = false; |
| 930 | unsigned NumDefs = MI.getDesc().getNumDefs(); |
| 931 | for (unsigned i = 0, e = MI.getNumOperands(); NumDefs && i != e; ++i) { |
| 932 | MachineOperand &DefMO = MI.getOperand(i); |
| 933 | if (!DefMO.isReg() || !DefMO.isDef()) |
| 934 | continue; |
| 935 | --NumDefs; |
| 936 | unsigned Reg = DefMO.getReg(); |
| 937 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) |
| 938 | continue; |
| 939 | |
| 940 | if (!TII->hasLowDefLatency(InstrItins, &MI, i)) |
| 941 | return false; |
| 942 | isCheap = true; |
| 943 | } |
| 944 | |
| 945 | return isCheap; |
| 946 | } |
| 947 | |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 948 | /// CanCauseHighRegPressure - Visit BBs from header to current BB, check |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 949 | /// if hoisting an instruction of the given cost matrix can cause high |
| 950 | /// register pressure. |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 951 | bool MachineLICM::CanCauseHighRegPressure(DenseMap<unsigned, int> &Cost) { |
| 952 | for (DenseMap<unsigned, int>::iterator CI = Cost.begin(), CE = Cost.end(); |
| 953 | CI != CE; ++CI) { |
| 954 | if (CI->second <= 0) |
| 955 | continue; |
| 956 | |
| 957 | unsigned RCId = CI->first; |
| 958 | for (unsigned i = BackTrace.size(); i != 0; --i) { |
| 959 | SmallVector<unsigned, 8> &RP = BackTrace[i-1]; |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 960 | if (RP[RCId] + CI->second >= RegLimit[RCId]) |
| 961 | return true; |
| 962 | } |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | return false; |
| 966 | } |
| 967 | |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 968 | /// UpdateBackTraceRegPressure - Traverse the back trace from header to the |
| 969 | /// current block and update their register pressures to reflect the effect |
| 970 | /// of hoisting MI from the current block to the preheader. |
| 971 | void MachineLICM::UpdateBackTraceRegPressure(const MachineInstr *MI) { |
| 972 | if (MI->isImplicitDef()) |
| 973 | return; |
| 974 | |
| 975 | // First compute the 'cost' of the instruction, i.e. its contribution |
| 976 | // to register pressure. |
| 977 | DenseMap<unsigned, int> Cost; |
| 978 | for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) { |
| 979 | const MachineOperand &MO = MI->getOperand(i); |
| 980 | if (!MO.isReg() || MO.isImplicit()) |
| 981 | continue; |
| 982 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 983 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 984 | continue; |
| 985 | |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 986 | unsigned RCId, RCCost; |
| 987 | getRegisterClassIDAndCost(MI, Reg, i, RCId, RCCost); |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 988 | if (MO.isDef()) { |
| 989 | DenseMap<unsigned, int>::iterator CI = Cost.find(RCId); |
| 990 | if (CI != Cost.end()) |
| 991 | CI->second += RCCost; |
| 992 | else |
| 993 | Cost.insert(std::make_pair(RCId, RCCost)); |
| 994 | } else if (isOperandKill(MO, MRI)) { |
| 995 | DenseMap<unsigned, int>::iterator CI = Cost.find(RCId); |
| 996 | if (CI != Cost.end()) |
| 997 | CI->second -= RCCost; |
| 998 | else |
| 999 | Cost.insert(std::make_pair(RCId, -RCCost)); |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | // Update register pressure of blocks from loop header to current block. |
| 1004 | for (unsigned i = 0, e = BackTrace.size(); i != e; ++i) { |
| 1005 | SmallVector<unsigned, 8> &RP = BackTrace[i]; |
| 1006 | for (DenseMap<unsigned, int>::iterator CI = Cost.begin(), CE = Cost.end(); |
| 1007 | CI != CE; ++CI) { |
| 1008 | unsigned RCId = CI->first; |
| 1009 | RP[RCId] += CI->second; |
| 1010 | } |
| 1011 | } |
| 1012 | } |
| 1013 | |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 1014 | /// IsProfitableToHoist - Return true if it is potentially profitable to hoist |
| 1015 | /// the given loop invariant. |
Evan Cheng | c26abd9 | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 1016 | bool MachineLICM::IsProfitableToHoist(MachineInstr &MI) { |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1017 | if (MI.isImplicitDef()) |
| 1018 | return true; |
| 1019 | |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 1020 | // If the instruction is cheap, only hoist if it is re-materilizable. LICM |
| 1021 | // will increase register pressure. It's probably not worth it if the |
| 1022 | // instruction is cheap. |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 1023 | // Also hoist loads from constant memory, e.g. load from stubs, GOT. Hoisting |
| 1024 | // these tend to help performance in low register pressure situation. The |
| 1025 | // trade off is it may cause spill in high pressure situation. It will end up |
| 1026 | // adding a store in the loop preheader. But the reload is no more expensive. |
| 1027 | // The side benefit is these loads are frequently CSE'ed. |
Evan Cheng | c8141df | 2010-10-26 02:08:50 +0000 | [diff] [blame] | 1028 | if (IsCheapInstruction(MI)) { |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 1029 | if (!TII->isTriviallyReMaterializable(&MI, AA)) |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1030 | return false; |
| 1031 | } else { |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 1032 | // Estimate register pressure to determine whether to LICM the instruction. |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1033 | // In low register pressure situation, we can be more aggressive about |
| 1034 | // hoisting. Also, favors hoisting long latency instructions even in |
| 1035 | // moderately high pressure situation. |
Dan Gohman | fca0b10 | 2010-11-11 18:08:43 +0000 | [diff] [blame] | 1036 | // FIXME: If there are long latency loop-invariant instructions inside the |
| 1037 | // loop at this point, why didn't the optimizer's LICM hoist them? |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1038 | DenseMap<unsigned, int> Cost; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1039 | for (unsigned i = 0, e = MI.getDesc().getNumOperands(); i != e; ++i) { |
| 1040 | const MachineOperand &MO = MI.getOperand(i); |
| 1041 | if (!MO.isReg() || MO.isImplicit()) |
| 1042 | continue; |
| 1043 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 1044 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1045 | continue; |
Evan Cheng | 61560e2 | 2011-09-01 01:45:00 +0000 | [diff] [blame] | 1046 | |
| 1047 | unsigned RCId, RCCost; |
| 1048 | getRegisterClassIDAndCost(&MI, Reg, i, RCId, RCCost); |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1049 | if (MO.isDef()) { |
Evan Cheng | 2312842 | 2010-10-19 18:58:51 +0000 | [diff] [blame] | 1050 | if (HasHighOperandLatency(MI, i, Reg)) { |
| 1051 | ++NumHighLatency; |
| 1052 | return true; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1053 | } |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1054 | |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1055 | DenseMap<unsigned, int>::iterator CI = Cost.find(RCId); |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1056 | if (CI != Cost.end()) |
| 1057 | CI->second += RCCost; |
| 1058 | else |
| 1059 | Cost.insert(std::make_pair(RCId, RCCost)); |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1060 | } else if (isOperandKill(MO, MRI)) { |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1061 | // Is a virtual register use is a kill, hoisting it out of the loop |
| 1062 | // may actually reduce register pressure or be register pressure |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1063 | // neutral. |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1064 | DenseMap<unsigned, int>::iterator CI = Cost.find(RCId); |
| 1065 | if (CI != Cost.end()) |
| 1066 | CI->second -= RCCost; |
| 1067 | else |
| 1068 | Cost.insert(std::make_pair(RCId, -RCCost)); |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1072 | // Visit BBs from header to current BB, if hoisting this doesn't cause |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1073 | // high register pressure, then it's safe to proceed. |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1074 | if (!CanCauseHighRegPressure(Cost)) { |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1075 | ++NumLowRP; |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1076 | return true; |
Evan Cheng | 03a9fdf | 2010-10-16 02:20:26 +0000 | [diff] [blame] | 1077 | } |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1078 | |
Evan Cheng | 7007e4c | 2011-10-12 21:33:49 +0000 | [diff] [blame] | 1079 | // Do not "speculate" in high register pressure situation. If an |
Evan Cheng | fad6287 | 2011-10-11 23:48:44 +0000 | [diff] [blame] | 1080 | // instruction is not guaranteed to be executed in the loop, it's best to be |
| 1081 | // conservative. |
Evan Cheng | 7007e4c | 2011-10-12 21:33:49 +0000 | [diff] [blame] | 1082 | if (AvoidSpeculation && |
| 1083 | (!IsGuaranteedToExecute(MI.getParent()) && !MayCSE(&MI))) |
| 1084 | return false; |
| 1085 | |
| 1086 | // High register pressure situation, only hoist if the instruction is going to |
| 1087 | // be remat'ed. |
| 1088 | if (!TII->isTriviallyReMaterializable(&MI, AA) && |
| 1089 | !MI.isInvariantLoad(AA)) |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 1090 | return false; |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 1091 | } |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 1092 | |
Evan Cheng | d67705f | 2011-04-11 21:09:18 +0000 | [diff] [blame] | 1093 | // If result(s) of this instruction is used by PHIs outside of the loop, then |
| 1094 | // don't hoist it if the instruction because it will introduce an extra copy. |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 1095 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 1096 | const MachineOperand &MO = MI.getOperand(i); |
| 1097 | if (!MO.isReg() || !MO.isDef()) |
| 1098 | continue; |
Evan Cheng | d67705f | 2011-04-11 21:09:18 +0000 | [diff] [blame] | 1099 | if (HasAnyPHIUse(MO.getReg())) |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 1100 | return false; |
Evan Cheng | 45e94d6 | 2009-02-04 09:19:56 +0000 | [diff] [blame] | 1101 | } |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 1102 | |
| 1103 | return true; |
| 1104 | } |
| 1105 | |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1106 | MachineInstr *MachineLICM::ExtractHoistableLoad(MachineInstr *MI) { |
Evan Cheng | e95f319 | 2010-10-08 18:59:19 +0000 | [diff] [blame] | 1107 | // Don't unfold simple loads. |
| 1108 | if (MI->getDesc().canFoldAsLoad()) |
| 1109 | return 0; |
| 1110 | |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1111 | // If not, we may be able to unfold a load and hoist that. |
| 1112 | // First test whether the instruction is loading from an amenable |
| 1113 | // memory location. |
Evan Cheng | 9fe2009 | 2011-01-20 08:34:58 +0000 | [diff] [blame] | 1114 | if (!MI->isInvariantLoad(AA)) |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 1115 | return 0; |
| 1116 | |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1117 | // Next determine the register class for a temporary register. |
Dan Gohman | 0115e16 | 2009-10-30 22:18:41 +0000 | [diff] [blame] | 1118 | unsigned LoadRegIndex; |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1119 | unsigned NewOpc = |
| 1120 | TII->getOpcodeAfterMemoryUnfold(MI->getOpcode(), |
| 1121 | /*UnfoldLoad=*/true, |
Dan Gohman | 0115e16 | 2009-10-30 22:18:41 +0000 | [diff] [blame] | 1122 | /*UnfoldStore=*/false, |
| 1123 | &LoadRegIndex); |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1124 | if (NewOpc == 0) return 0; |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1125 | const MCInstrDesc &MID = TII->get(NewOpc); |
| 1126 | if (MID.getNumDefs() != 1) return 0; |
| 1127 | const TargetRegisterClass *RC = TII->getRegClass(MID, LoadRegIndex, TRI); |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1128 | // Ok, we're unfolding. Create a temporary register and do the unfold. |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1129 | unsigned Reg = MRI->createVirtualRegister(RC); |
Evan Cheng | 87b75ba | 2009-11-20 19:55:37 +0000 | [diff] [blame] | 1130 | |
| 1131 | MachineFunction &MF = *MI->getParent()->getParent(); |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1132 | SmallVector<MachineInstr *, 2> NewMIs; |
| 1133 | bool Success = |
| 1134 | TII->unfoldMemoryOperand(MF, MI, Reg, |
| 1135 | /*UnfoldLoad=*/true, /*UnfoldStore=*/false, |
| 1136 | NewMIs); |
| 1137 | (void)Success; |
| 1138 | assert(Success && |
| 1139 | "unfoldMemoryOperand failed when getOpcodeAfterMemoryUnfold " |
| 1140 | "succeeded!"); |
| 1141 | assert(NewMIs.size() == 2 && |
| 1142 | "Unfolded a load into multiple instructions!"); |
| 1143 | MachineBasicBlock *MBB = MI->getParent(); |
| 1144 | MBB->insert(MI, NewMIs[0]); |
| 1145 | MBB->insert(MI, NewMIs[1]); |
| 1146 | // If unfolding produced a load that wasn't loop-invariant or profitable to |
| 1147 | // hoist, discard the new instructions and bail. |
Evan Cheng | c26abd9 | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 1148 | if (!IsLoopInvariantInst(*NewMIs[0]) || !IsProfitableToHoist(*NewMIs[0])) { |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1149 | NewMIs[0]->eraseFromParent(); |
| 1150 | NewMIs[1]->eraseFromParent(); |
| 1151 | return 0; |
| 1152 | } |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1153 | |
| 1154 | // Update register pressure for the unfolded instruction. |
| 1155 | UpdateRegPressure(NewMIs[1]); |
| 1156 | |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1157 | // Otherwise we successfully unfolded a load that we can hoist. |
| 1158 | MI->eraseFromParent(); |
| 1159 | return NewMIs[0]; |
| 1160 | } |
| 1161 | |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1162 | void MachineLICM::InitCSEMap(MachineBasicBlock *BB) { |
| 1163 | for (MachineBasicBlock::iterator I = BB->begin(),E = BB->end(); I != E; ++I) { |
| 1164 | const MachineInstr *MI = &*I; |
Evan Cheng | 9fe2009 | 2011-01-20 08:34:58 +0000 | [diff] [blame] | 1165 | unsigned Opcode = MI->getOpcode(); |
| 1166 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator |
| 1167 | CI = CSEMap.find(Opcode); |
| 1168 | if (CI != CSEMap.end()) |
| 1169 | CI->second.push_back(MI); |
| 1170 | else { |
| 1171 | std::vector<const MachineInstr*> CSEMIs; |
| 1172 | CSEMIs.push_back(MI); |
| 1173 | CSEMap.insert(std::make_pair(Opcode, CSEMIs)); |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1174 | } |
| 1175 | } |
| 1176 | } |
| 1177 | |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 1178 | const MachineInstr* |
| 1179 | MachineLICM::LookForDuplicate(const MachineInstr *MI, |
| 1180 | std::vector<const MachineInstr*> &PrevMIs) { |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 1181 | for (unsigned i = 0, e = PrevMIs.size(); i != e; ++i) { |
| 1182 | const MachineInstr *PrevMI = PrevMIs[i]; |
Evan Cheng | 9fe2009 | 2011-01-20 08:34:58 +0000 | [diff] [blame] | 1183 | if (TII->produceSameValue(MI, PrevMI, (PreRegAlloc ? MRI : 0))) |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 1184 | return PrevMI; |
| 1185 | } |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
| 1189 | bool MachineLICM::EliminateCSE(MachineInstr *MI, |
| 1190 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator &CI) { |
Evan Cheng | db89809 | 2010-07-14 01:22:19 +0000 | [diff] [blame] | 1191 | // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate |
| 1192 | // the undef property onto uses. |
| 1193 | if (CI == CSEMap.end() || MI->isImplicitDef()) |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 1194 | return false; |
| 1195 | |
| 1196 | if (const MachineInstr *Dup = LookForDuplicate(MI, CI->second)) { |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 1197 | DEBUG(dbgs() << "CSEing " << *MI << " with " << *Dup); |
Dan Gohman | 6ac33b4 | 2010-02-28 01:33:43 +0000 | [diff] [blame] | 1198 | |
| 1199 | // Replace virtual registers defined by MI by their counterparts defined |
| 1200 | // by Dup. |
Evan Cheng | 1025cce | 2011-10-17 19:50:12 +0000 | [diff] [blame] | 1201 | SmallVector<unsigned, 2> Defs; |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 1202 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1203 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | 6ac33b4 | 2010-02-28 01:33:43 +0000 | [diff] [blame] | 1204 | |
| 1205 | // Physical registers may not differ here. |
| 1206 | assert((!MO.isReg() || MO.getReg() == 0 || |
| 1207 | !TargetRegisterInfo::isPhysicalRegister(MO.getReg()) || |
| 1208 | MO.getReg() == Dup->getOperand(i).getReg()) && |
| 1209 | "Instructions with different phys regs are not identical!"); |
| 1210 | |
| 1211 | if (MO.isReg() && MO.isDef() && |
Evan Cheng | 1025cce | 2011-10-17 19:50:12 +0000 | [diff] [blame] | 1212 | !TargetRegisterInfo::isPhysicalRegister(MO.getReg())) |
| 1213 | Defs.push_back(i); |
| 1214 | } |
| 1215 | |
| 1216 | SmallVector<const TargetRegisterClass*, 2> OrigRCs; |
| 1217 | for (unsigned i = 0, e = Defs.size(); i != e; ++i) { |
| 1218 | unsigned Idx = Defs[i]; |
| 1219 | unsigned Reg = MI->getOperand(Idx).getReg(); |
| 1220 | unsigned DupReg = Dup->getOperand(Idx).getReg(); |
| 1221 | OrigRCs.push_back(MRI->getRegClass(DupReg)); |
| 1222 | |
| 1223 | if (!MRI->constrainRegClass(DupReg, MRI->getRegClass(Reg))) { |
| 1224 | // Restore old RCs if more than one defs. |
| 1225 | for (unsigned j = 0; j != i; ++j) |
| 1226 | MRI->setRegClass(Dup->getOperand(Defs[j]).getReg(), OrigRCs[j]); |
| 1227 | return false; |
Dan Gohman | e6cd757 | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 1228 | } |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 1229 | } |
Evan Cheng | 1025cce | 2011-10-17 19:50:12 +0000 | [diff] [blame] | 1230 | |
| 1231 | for (unsigned i = 0, e = Defs.size(); i != e; ++i) { |
| 1232 | unsigned Idx = Defs[i]; |
| 1233 | unsigned Reg = MI->getOperand(Idx).getReg(); |
| 1234 | unsigned DupReg = Dup->getOperand(Idx).getReg(); |
| 1235 | MRI->replaceRegWith(Reg, DupReg); |
| 1236 | MRI->clearKillFlags(DupReg); |
| 1237 | } |
| 1238 | |
Evan Cheng | 78e5c11 | 2009-11-07 03:52:02 +0000 | [diff] [blame] | 1239 | MI->eraseFromParent(); |
| 1240 | ++NumCSEed; |
| 1241 | return true; |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 1242 | } |
| 1243 | return false; |
| 1244 | } |
| 1245 | |
Evan Cheng | 7efba85 | 2011-10-12 00:09:14 +0000 | [diff] [blame] | 1246 | /// MayCSE - Return true if the given instruction will be CSE'd if it's |
| 1247 | /// hoisted out of the loop. |
| 1248 | bool MachineLICM::MayCSE(MachineInstr *MI) { |
| 1249 | unsigned Opcode = MI->getOpcode(); |
| 1250 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator |
| 1251 | CI = CSEMap.find(Opcode); |
| 1252 | // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate |
| 1253 | // the undef property onto uses. |
| 1254 | if (CI == CSEMap.end() || MI->isImplicitDef()) |
| 1255 | return false; |
| 1256 | |
| 1257 | return LookForDuplicate(MI, CI->second) != 0; |
| 1258 | } |
| 1259 | |
Bill Wendling | e4fc1cc | 2008-05-12 19:38:32 +0000 | [diff] [blame] | 1260 | /// Hoist - When an instruction is found to use only loop invariant operands |
| 1261 | /// 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] | 1262 | /// |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1263 | bool MachineLICM::Hoist(MachineInstr *MI, MachineBasicBlock *Preheader) { |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 1264 | // First check whether we should hoist this instruction. |
Evan Cheng | c26abd9 | 2009-11-20 23:31:34 +0000 | [diff] [blame] | 1265 | if (!IsLoopInvariantInst(*MI) || !IsProfitableToHoist(*MI)) { |
Dan Gohman | 5c95230 | 2009-10-29 17:47:20 +0000 | [diff] [blame] | 1266 | // If not, try unfolding a hoistable load. |
| 1267 | MI = ExtractHoistableLoad(MI); |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1268 | if (!MI) return false; |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 1269 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1270 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 1271 | // Now move the instructions to the predecessor, inserting it before any |
| 1272 | // terminator instructions. |
| 1273 | DEBUG({ |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 1274 | dbgs() << "Hoisting " << *MI; |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 1275 | if (Preheader->getBasicBlock()) |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 1276 | dbgs() << " to MachineBasicBlock " |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 1277 | << Preheader->getName(); |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 1278 | if (MI->getParent()->getBasicBlock()) |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 1279 | dbgs() << " from MachineBasicBlock " |
Jakob Stoklund Olesen | 324da76 | 2009-11-20 01:17:03 +0000 | [diff] [blame] | 1280 | << MI->getParent()->getName(); |
David Greene | 65a41eb | 2010-01-05 00:03:48 +0000 | [diff] [blame] | 1281 | dbgs() << "\n"; |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 1282 | }); |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1283 | |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1284 | // If this is the first instruction being hoisted to the preheader, |
| 1285 | // initialize the CSE map with potential common expressions. |
Evan Cheng | 82e0a1a | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 1286 | if (FirstInLoop) { |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 1287 | InitCSEMap(Preheader); |
Evan Cheng | 82e0a1a | 2010-05-29 00:06:36 +0000 | [diff] [blame] | 1288 | FirstInLoop = false; |
| 1289 | } |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1290 | |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 1291 | // Look for opportunity to CSE the hoisted instruction. |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1292 | unsigned Opcode = MI->getOpcode(); |
| 1293 | DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator |
| 1294 | CI = CSEMap.find(Opcode); |
Evan Cheng | 9fb744e | 2009-11-05 00:51:13 +0000 | [diff] [blame] | 1295 | if (!EliminateCSE(MI, CI)) { |
| 1296 | // Otherwise, splice the instruction to the preheader. |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 1297 | Preheader->splice(Preheader->getFirstTerminator(),MI->getParent(),MI); |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1298 | |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1299 | // Update register pressure for BBs from header to this block. |
| 1300 | UpdateBackTraceRegPressure(MI); |
| 1301 | |
Dan Gohman | e6cd757 | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 1302 | // Clear the kill flags of any register this instruction defines, |
| 1303 | // since they may need to be live throughout the entire loop |
| 1304 | // rather than just live for part of it. |
| 1305 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 1306 | MachineOperand &MO = MI->getOperand(i); |
| 1307 | if (MO.isReg() && MO.isDef() && !MO.isDead()) |
Evan Cheng | 0e67391 | 2010-10-14 01:16:09 +0000 | [diff] [blame] | 1308 | MRI->clearKillFlags(MO.getReg()); |
Dan Gohman | e6cd757 | 2010-05-13 20:34:42 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 1311 | // Add to the CSE map. |
| 1312 | if (CI != CSEMap.end()) |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 1313 | CI->second.push_back(MI); |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 1314 | else { |
| 1315 | std::vector<const MachineInstr*> CSEMIs; |
Dan Gohman | 589f1f5 | 2009-10-28 03:21:57 +0000 | [diff] [blame] | 1316 | CSEMIs.push_back(MI); |
Evan Cheng | 777c6b7 | 2009-11-03 21:40:02 +0000 | [diff] [blame] | 1317 | CSEMap.insert(std::make_pair(Opcode, CSEMIs)); |
Evan Cheng | af6949d | 2009-02-05 08:45:46 +0000 | [diff] [blame] | 1318 | } |
| 1319 | } |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1320 | |
Dan Gohman | c475c36 | 2009-01-15 22:01:38 +0000 | [diff] [blame] | 1321 | ++NumHoisted; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1322 | Changed = true; |
Evan Cheng | 134982d | 2010-10-20 22:03:58 +0000 | [diff] [blame] | 1323 | |
| 1324 | return true; |
Bill Wendling | 0f940c9 | 2007-12-07 21:42:31 +0000 | [diff] [blame] | 1325 | } |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 1326 | |
| 1327 | MachineBasicBlock *MachineLICM::getCurPreheader() { |
| 1328 | // Determine the block to which to hoist instructions. If we can't find a |
| 1329 | // suitable loop predecessor, we can't do any hoisting. |
| 1330 | |
| 1331 | // If we've tried to get a preheader and failed, don't try again. |
| 1332 | if (CurPreheader == reinterpret_cast<MachineBasicBlock *>(-1)) |
| 1333 | return 0; |
| 1334 | |
| 1335 | if (!CurPreheader) { |
| 1336 | CurPreheader = CurLoop->getLoopPreheader(); |
| 1337 | if (!CurPreheader) { |
| 1338 | MachineBasicBlock *Pred = CurLoop->getLoopPredecessor(); |
| 1339 | if (!Pred) { |
| 1340 | CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1); |
| 1341 | return 0; |
| 1342 | } |
| 1343 | |
| 1344 | CurPreheader = Pred->SplitCriticalEdge(CurLoop->getHeader(), this); |
| 1345 | if (!CurPreheader) { |
| 1346 | CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1); |
| 1347 | return 0; |
| 1348 | } |
| 1349 | } |
| 1350 | } |
| 1351 | return CurPreheader; |
| 1352 | } |