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