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