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