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