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