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