blob: 00dec821158fb31a08605a86accba66eef588b40 [file] [log] [blame]
Bill Wendlingfb706bc2007-12-07 21:42:31 +00001//===-- MachineLICM.cpp - Machine Loop Invariant Code Motion Pass ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bill Wendlingfb706bc2007-12-07 21:42:31 +00007//
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 Gohman79618d12009-01-15 22:01:38 +000013// 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 Wendlingfb706bc2007-12-07 21:42:31 +000017//===----------------------------------------------------------------------===//
18
Chris Lattnerb5c1d9b2008-01-04 06:41:45 +000019#include "llvm/CodeGen/Passes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/SmallSet.h"
22#include "llvm/ADT/Statistic.h"
23#include "llvm/Analysis/AliasAnalysis.h"
Bill Wendlingfb706bc2007-12-07 21:42:31 +000024#include "llvm/CodeGen/MachineDominators.h"
Evan Cheng6ea59492010-04-07 00:41:17 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
Bill Wendlingfb706bc2007-12-07 21:42:31 +000026#include "llvm/CodeGen/MachineLoopInfo.h"
Dan Gohman1b44f102009-10-28 03:21:57 +000027#include "llvm/CodeGen/MachineMemOperand.h"
Bill Wendling5da19452008-01-02 19:32:43 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman1b44f102009-10-28 03:21:57 +000029#include "llvm/CodeGen/PseudoSourceValue.h"
Matthias Braun88e21312015-06-13 03:42:11 +000030#include "llvm/CodeGen/TargetSchedule.h"
Evan Chengb35afca2011-10-12 21:33:49 +000031#include "llvm/Support/CommandLine.h"
Chris Lattnerb5c1d9b2008-01-04 06:41:45 +000032#include "llvm/Support/Debug.h"
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +000033#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Target/TargetInstrInfo.h"
35#include "llvm/Target/TargetLowering.h"
36#include "llvm/Target/TargetMachine.h"
37#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000038#include "llvm/Target/TargetSubtargetInfo.h"
Bill Wendlingfb706bc2007-12-07 21:42:31 +000039using namespace llvm;
40
Chandler Carruth1b9dde02014-04-22 02:02:50 +000041#define DEBUG_TYPE "machine-licm"
42
Evan Chengb35afca2011-10-12 21:33:49 +000043static cl::opt<bool>
44AvoidSpeculation("avoid-speculation",
45 cl::desc("MachineLICM should avoid speculation"),
Evan Cheng73133372011-10-26 01:26:57 +000046 cl::init(true), cl::Hidden);
Evan Chengb35afca2011-10-12 21:33:49 +000047
Hal Finkel0709f512015-01-08 22:10:48 +000048static cl::opt<bool>
49HoistCheapInsts("hoist-cheap-insts",
50 cl::desc("MachineLICM should hoist even cheap instructions"),
51 cl::init(false), cl::Hidden);
52
Daniel Jasper15e69542015-03-14 10:58:38 +000053static cl::opt<bool>
54SinkInstsToAvoidSpills("sink-insts-to-avoid-spills",
55 cl::desc("MachineLICM should sink instructions into "
56 "loops to avoid register spills"),
57 cl::init(false), cl::Hidden);
58
Evan Cheng44436302010-10-16 02:20:26 +000059STATISTIC(NumHoisted,
60 "Number of machine instructions hoisted out of loops");
61STATISTIC(NumLowRP,
62 "Number of instructions hoisted in low reg pressure situation");
63STATISTIC(NumHighLatency,
64 "Number of high latency instructions hoisted");
65STATISTIC(NumCSEed,
66 "Number of hoisted machine instructions CSEed");
Evan Cheng6ea59492010-04-07 00:41:17 +000067STATISTIC(NumPostRAHoisted,
68 "Number of machine instructions hoisted out of loops post regalloc");
Bill Wendling43751732007-12-08 01:47:01 +000069
Bill Wendlingfb706bc2007-12-07 21:42:31 +000070namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000071 class MachineLICM : public MachineFunctionPass {
Bill Wendling38236ef2007-12-11 23:27:51 +000072 const TargetInstrInfo *TII;
Benjamin Kramer56b31bd2013-01-11 20:05:37 +000073 const TargetLoweringBase *TLI;
Dan Gohmane30d63f2009-09-25 23:58:45 +000074 const TargetRegisterInfo *TRI;
Evan Cheng6ea59492010-04-07 00:41:17 +000075 const MachineFrameInfo *MFI;
Evan Chengd62719c2010-10-14 01:16:09 +000076 MachineRegisterInfo *MRI;
Matthias Braun88e21312015-06-13 03:42:11 +000077 TargetSchedModel SchedModel;
Andrew Trickc40815d2012-02-08 21:23:03 +000078 bool PreRegAlloc;
Bill Wendlingb678ae72007-12-11 19:40:06 +000079
Bill Wendlingfb706bc2007-12-07 21:42:31 +000080 // Various analyses that we use...
Dan Gohmanbe8137b2009-10-07 17:38:06 +000081 AliasAnalysis *AA; // Alias analysis info.
Evan Cheng058b9f02010-04-08 01:03:47 +000082 MachineLoopInfo *MLI; // Current MachineLoopInfo
Bill Wendling70613b82008-05-12 19:38:32 +000083 MachineDominatorTree *DT; // Machine dominator tree for the cur loop
Bill Wendlingfb706bc2007-12-07 21:42:31 +000084
Bill Wendlingfb706bc2007-12-07 21:42:31 +000085 // State that is updated as we process loops
Bill Wendling70613b82008-05-12 19:38:32 +000086 bool Changed; // True if a loop is changed.
Evan Cheng032f3262010-05-29 00:06:36 +000087 bool FirstInLoop; // True if it's the first LICM in the loop.
Bill Wendling70613b82008-05-12 19:38:32 +000088 MachineLoop *CurLoop; // The current loop we are working on.
Dan Gohman79618d12009-01-15 22:01:38 +000089 MachineBasicBlock *CurPreheader; // The preheader for CurLoop.
Evan Cheng399660c2009-02-05 08:45:46 +000090
Jakob Stoklund Olesena3e86a62012-04-11 00:00:26 +000091 // Exit blocks for CurLoop.
92 SmallVector<MachineBasicBlock*, 8> ExitBlocks;
93
94 bool isExitBlock(const MachineBasicBlock *MBB) const {
95 return std::find(ExitBlocks.begin(), ExitBlocks.end(), MBB) !=
96 ExitBlocks.end();
97 }
98
Evan Chengd62719c2010-10-14 01:16:09 +000099 // Track 'estimated' register pressure.
Evan Cheng44436302010-10-16 02:20:26 +0000100 SmallSet<unsigned, 32> RegSeen;
Evan Chengd62719c2010-10-14 01:16:09 +0000101 SmallVector<unsigned, 8> RegPressure;
Evan Cheng44436302010-10-16 02:20:26 +0000102
Daniel Jasper274928f2015-04-14 11:56:25 +0000103 // Register pressure "limit" per register pressure set. If the pressure
Evan Cheng44436302010-10-16 02:20:26 +0000104 // is higher than the limit, then it's considered high.
Evan Chengd62719c2010-10-14 01:16:09 +0000105 SmallVector<unsigned, 8> RegLimit;
106
Evan Cheng44436302010-10-16 02:20:26 +0000107 // Register pressure on path leading from loop preheader to current BB.
108 SmallVector<SmallVector<unsigned, 8>, 16> BackTrace;
109
Dale Johannesen329d4742010-07-29 17:45:24 +0000110 // For each opcode, keep a list of potential CSE instructions.
Evan Chengf42b5af2009-11-03 21:40:02 +0000111 DenseMap<unsigned, std::vector<const MachineInstr*> > CSEMap;
Evan Cheng6ea59492010-04-07 00:41:17 +0000112
Evan Chengf192ca02011-10-11 23:48:44 +0000113 enum {
114 SpeculateFalse = 0,
115 SpeculateTrue = 1,
116 SpeculateUnknown = 2
117 };
118
Devang Patel453d4012011-10-11 18:09:58 +0000119 // If a MBB does not dominate loop exiting blocks then it may not safe
120 // to hoist loads from this block.
Evan Chengf192ca02011-10-11 23:48:44 +0000121 // Tri-state: 0 - false, 1 - true, 2 - unknown
122 unsigned SpeculationState;
Devang Patel453d4012011-10-11 18:09:58 +0000123
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000124 public:
125 static char ID; // Pass identification, replacement for typeid
Evan Cheng6ea59492010-04-07 00:41:17 +0000126 MachineLICM() :
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000127 MachineFunctionPass(ID), PreRegAlloc(true) {
128 initializeMachineLICMPass(*PassRegistry::getPassRegistry());
129 }
Evan Cheng6ea59492010-04-07 00:41:17 +0000130
131 explicit MachineLICM(bool PreRA) :
Owen Anderson6c18d1a2010-10-19 17:21:58 +0000132 MachineFunctionPass(ID), PreRegAlloc(PreRA) {
133 initializeMachineLICMPass(*PassRegistry::getPassRegistry());
134 }
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000135
Craig Topper4584cd52014-03-07 09:26:03 +0000136 bool runOnMachineFunction(MachineFunction &MF) override;
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000137
Craig Topper4584cd52014-03-07 09:26:03 +0000138 void getAnalysisUsage(AnalysisUsage &AU) const override {
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000139 AU.addRequired<MachineLoopInfo>();
140 AU.addRequired<MachineDominatorTree>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000141 AU.addRequired<AAResultsWrapperPass>();
Bill Wendling3bf56032008-01-04 08:48:49 +0000142 AU.addPreserved<MachineLoopInfo>();
143 AU.addPreserved<MachineDominatorTree>();
144 MachineFunctionPass::getAnalysisUsage(AU);
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000145 }
Evan Cheng399660c2009-02-05 08:45:46 +0000146
Craig Topper4584cd52014-03-07 09:26:03 +0000147 void releaseMemory() override {
Evan Cheng44436302010-10-16 02:20:26 +0000148 RegSeen.clear();
Evan Chengd62719c2010-10-14 01:16:09 +0000149 RegPressure.clear();
150 RegLimit.clear();
Evan Cheng63c76082010-10-19 18:58:51 +0000151 BackTrace.clear();
Evan Cheng399660c2009-02-05 08:45:46 +0000152 CSEMap.clear();
153 }
154
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000155 private:
Sanjay Patel87c6c072015-12-10 16:34:21 +0000156 /// Keep track of information about hoisting candidates.
Evan Cheng058b9f02010-04-08 01:03:47 +0000157 struct CandidateInfo {
158 MachineInstr *MI;
Evan Cheng058b9f02010-04-08 01:03:47 +0000159 unsigned Def;
Evan Cheng0a2aff22010-04-13 18:16:00 +0000160 int FI;
161 CandidateInfo(MachineInstr *mi, unsigned def, int fi)
162 : MI(mi), Def(def), FI(fi) {}
Evan Cheng058b9f02010-04-08 01:03:47 +0000163 };
164
Evan Cheng5fdb57c2010-04-17 07:07:11 +0000165 void HoistRegionPostRA();
Evan Cheng058b9f02010-04-08 01:03:47 +0000166
Evan Cheng058b9f02010-04-08 01:03:47 +0000167 void HoistPostRA(MachineInstr *MI, unsigned Def);
168
Sanjay Patel87c6c072015-12-10 16:34:21 +0000169 void ProcessMI(MachineInstr *MI, BitVector &PhysRegDefs,
170 BitVector &PhysRegClobbers, SmallSet<int, 32> &StoredFIs,
Craig Topper2cd5ff82013-07-11 16:22:38 +0000171 SmallVectorImpl<CandidateInfo> &Candidates);
Evan Cheng058b9f02010-04-08 01:03:47 +0000172
Evan Cheng5fdb57c2010-04-17 07:07:11 +0000173 void AddToLiveIns(unsigned Reg);
Evan Cheng058b9f02010-04-08 01:03:47 +0000174
Evan Cheng0a2aff22010-04-13 18:16:00 +0000175 bool IsLICMCandidate(MachineInstr &I);
176
Bill Wendling3f19dfe72007-12-08 23:58:46 +0000177 bool IsLoopInvariantInst(MachineInstr &I);
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000178
Jakob Stoklund Olesena3e86a62012-04-11 00:00:26 +0000179 bool HasLoopPHIUse(const MachineInstr *MI) const;
Evan Chengef42bea2011-04-11 21:09:18 +0000180
Evan Chenge96b8d72010-10-26 02:08:50 +0000181 bool HasHighOperandLatency(MachineInstr &MI, unsigned DefIdx,
182 unsigned Reg) const;
183
184 bool IsCheapInstruction(MachineInstr &MI) const;
Evan Chengd62719c2010-10-14 01:16:09 +0000185
Daniel Jasperefece522015-04-03 16:19:48 +0000186 bool CanCauseHighRegPressure(const DenseMap<unsigned, int> &Cost,
187 bool Cheap);
Evan Cheng87066f02010-10-20 22:03:58 +0000188
Evan Cheng87066f02010-10-20 22:03:58 +0000189 void UpdateBackTraceRegPressure(const MachineInstr *MI);
Evan Cheng44436302010-10-16 02:20:26 +0000190
Evan Cheng73f9a9e2009-11-20 23:31:34 +0000191 bool IsProfitableToHoist(MachineInstr &MI);
Evan Cheng1d9f7ac2009-02-04 09:19:56 +0000192
Devang Patel453d4012011-10-11 18:09:58 +0000193 bool IsGuaranteedToExecute(MachineBasicBlock *BB);
194
Pete Cooper1eed5b52011-12-22 02:05:40 +0000195 void EnterScope(MachineBasicBlock *MBB);
196
197 void ExitScope(MachineBasicBlock *MBB);
198
Sanjay Patel87c6c072015-12-10 16:34:21 +0000199 void ExitScopeIfDone(
200 MachineDomTreeNode *Node,
201 DenseMap<MachineDomTreeNode *, unsigned> &OpenChildren,
202 DenseMap<MachineDomTreeNode *, MachineDomTreeNode *> &ParentMap);
Pete Cooper1eed5b52011-12-22 02:05:40 +0000203
Pete Cooper1eed5b52011-12-22 02:05:40 +0000204 void HoistOutOfLoop(MachineDomTreeNode *LoopHeaderNode);
Sanjay Patel87c6c072015-12-10 16:34:21 +0000205
Pete Cooper1eed5b52011-12-22 02:05:40 +0000206 void HoistRegion(MachineDomTreeNode *N, bool IsHeader);
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000207
Daniel Jasper15e69542015-03-14 10:58:38 +0000208 void SinkIntoLoop();
209
Evan Chengd62719c2010-10-14 01:16:09 +0000210 void InitRegPressure(MachineBasicBlock *BB);
211
Daniel Jaspere87e82b2015-04-07 16:42:35 +0000212 DenseMap<unsigned, int> calcRegisterCost(const MachineInstr *MI,
213 bool ConsiderSeen,
214 bool ConsiderUnseenAsDef);
215
Daniel Jaspere87e82b2015-04-07 16:42:35 +0000216 void UpdateRegPressure(const MachineInstr *MI,
217 bool ConsiderUnseenAsDef = false);
Evan Chengd62719c2010-10-14 01:16:09 +0000218
Dan Gohman104f57c2009-10-29 17:47:20 +0000219 MachineInstr *ExtractHoistableLoad(MachineInstr *MI);
220
Sanjay Patel87c6c072015-12-10 16:34:21 +0000221 const MachineInstr *
222 LookForDuplicate(const MachineInstr *MI,
223 std::vector<const MachineInstr *> &PrevMIs);
Evan Cheng7ff83192009-11-07 03:52:02 +0000224
Sanjay Patel87c6c072015-12-10 16:34:21 +0000225 bool EliminateCSE(
226 MachineInstr *MI,
227 DenseMap<unsigned, std::vector<const MachineInstr *>>::iterator &CI);
Evan Cheng921152f2009-11-05 00:51:13 +0000228
Evan Chengaf138952011-10-12 00:09:14 +0000229 bool MayCSE(MachineInstr *MI);
230
Evan Cheng87066f02010-10-20 22:03:58 +0000231 bool Hoist(MachineInstr *MI, MachineBasicBlock *Preheader);
Evan Chengf42b5af2009-11-03 21:40:02 +0000232
Evan Chengf42b5af2009-11-03 21:40:02 +0000233 void InitCSEMap(MachineBasicBlock *BB);
Dan Gohman3570f812010-06-22 17:25:57 +0000234
Dan Gohman3570f812010-06-22 17:25:57 +0000235 MachineBasicBlock *getCurPreheader();
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000236 };
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000237} // end anonymous namespace
238
Dan Gohmand78c4002008-05-13 00:00:25 +0000239char MachineLICM::ID = 0;
Andrew Trick1fa5bcb2012-02-08 21:23:13 +0000240char &llvm::MachineLICMID = MachineLICM::ID;
Owen Anderson8ac477f2010-10-12 19:48:12 +0000241INITIALIZE_PASS_BEGIN(MachineLICM, "machinelicm",
242 "Machine Loop Invariant Code Motion", false, false)
243INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
244INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000245INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Owen Anderson8ac477f2010-10-12 19:48:12 +0000246INITIALIZE_PASS_END(MachineLICM, "machinelicm",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000247 "Machine Loop Invariant Code Motion", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +0000248
Sanjay Patel87c6c072015-12-10 16:34:21 +0000249/// Test if the given loop is the outer-most loop that has a unique predecessor.
Dan Gohman3570f812010-06-22 17:25:57 +0000250static bool LoopIsOuterMostWithPredecessor(MachineLoop *CurLoop) {
Dan Gohman7929c442010-07-09 18:49:45 +0000251 // Check whether this loop even has a unique predecessor.
252 if (!CurLoop->getLoopPredecessor())
253 return false;
254 // Ok, now check to see if any of its outer loops do.
Dan Gohman79618d12009-01-15 22:01:38 +0000255 for (MachineLoop *L = CurLoop->getParentLoop(); L; L = L->getParentLoop())
Dan Gohman3570f812010-06-22 17:25:57 +0000256 if (L->getLoopPredecessor())
Dan Gohman79618d12009-01-15 22:01:38 +0000257 return false;
Dan Gohman7929c442010-07-09 18:49:45 +0000258 // None of them did, so this is the outermost with a unique predecessor.
Dan Gohman79618d12009-01-15 22:01:38 +0000259 return true;
260}
261
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000262bool MachineLICM::runOnMachineFunction(MachineFunction &MF) {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000263 if (skipFunction(*MF.getFunction()))
Paul Robinson7c99ec52014-03-31 17:43:35 +0000264 return false;
265
Evan Cheng032f3262010-05-29 00:06:36 +0000266 Changed = FirstInLoop = false;
Matthias Braun88e21312015-06-13 03:42:11 +0000267 const TargetSubtargetInfo &ST = MF.getSubtarget();
268 TII = ST.getInstrInfo();
269 TLI = ST.getTargetLowering();
270 TRI = ST.getRegisterInfo();
Evan Cheng6ea59492010-04-07 00:41:17 +0000271 MFI = MF.getFrameInfo();
Evan Chengd62719c2010-10-14 01:16:09 +0000272 MRI = &MF.getRegInfo();
Matthias Braun88e21312015-06-13 03:42:11 +0000273 SchedModel.init(ST.getSchedModel(), &ST, TII);
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000274
Andrew Trickc40815d2012-02-08 21:23:03 +0000275 PreRegAlloc = MRI->isSSA();
276
Jakob Stoklund Olesenc8046c02012-02-11 00:40:36 +0000277 if (PreRegAlloc)
278 DEBUG(dbgs() << "******** Pre-regalloc Machine LICM: ");
279 else
280 DEBUG(dbgs() << "******** Post-regalloc Machine LICM: ");
Craig Toppera538d832012-08-22 06:07:19 +0000281 DEBUG(dbgs() << MF.getName() << " ********\n");
Jakob Stoklund Olesenc8046c02012-02-11 00:40:36 +0000282
Evan Chengd62719c2010-10-14 01:16:09 +0000283 if (PreRegAlloc) {
284 // Estimate register pressure during pre-regalloc pass.
Daniel Jasper274928f2015-04-14 11:56:25 +0000285 unsigned NumRPS = TRI->getNumRegPressureSets();
286 RegPressure.resize(NumRPS);
Evan Chengd62719c2010-10-14 01:16:09 +0000287 std::fill(RegPressure.begin(), RegPressure.end(), 0);
Daniel Jasper274928f2015-04-14 11:56:25 +0000288 RegLimit.resize(NumRPS);
289 for (unsigned i = 0, e = NumRPS; i != e; ++i)
290 RegLimit[i] = TRI->getRegPressureSetLimit(MF, i);
Evan Chengd62719c2010-10-14 01:16:09 +0000291 }
292
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000293 // Get our Loop information...
Evan Cheng058b9f02010-04-08 01:03:47 +0000294 MLI = &getAnalysis<MachineLoopInfo>();
295 DT = &getAnalysis<MachineDominatorTree>();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000296 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000297
Dan Gohman7929c442010-07-09 18:49:45 +0000298 SmallVector<MachineLoop *, 8> Worklist(MLI->begin(), MLI->end());
299 while (!Worklist.empty()) {
300 CurLoop = Worklist.pop_back_val();
Craig Topperc0196b12014-04-14 00:51:57 +0000301 CurPreheader = nullptr;
Jakob Stoklund Olesena3e86a62012-04-11 00:00:26 +0000302 ExitBlocks.clear();
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000303
Evan Cheng058b9f02010-04-08 01:03:47 +0000304 // If this is done before regalloc, only visit outer-most preheader-sporting
305 // loops.
Dan Gohman7929c442010-07-09 18:49:45 +0000306 if (PreRegAlloc && !LoopIsOuterMostWithPredecessor(CurLoop)) {
307 Worklist.append(CurLoop->begin(), CurLoop->end());
Dan Gohman79618d12009-01-15 22:01:38 +0000308 continue;
Dan Gohman7929c442010-07-09 18:49:45 +0000309 }
Dan Gohman79618d12009-01-15 22:01:38 +0000310
Jakob Stoklund Olesena3e86a62012-04-11 00:00:26 +0000311 CurLoop->getExitBlocks(ExitBlocks);
312
Evan Cheng6ea59492010-04-07 00:41:17 +0000313 if (!PreRegAlloc)
Evan Cheng5fdb57c2010-04-17 07:07:11 +0000314 HoistRegionPostRA();
Evan Cheng6ea59492010-04-07 00:41:17 +0000315 else {
Evan Cheng5fdb57c2010-04-17 07:07:11 +0000316 // CSEMap is initialized for loop header when the first instruction is
317 // being hoisted.
318 MachineDomTreeNode *N = DT->getNode(CurLoop->getHeader());
Evan Cheng032f3262010-05-29 00:06:36 +0000319 FirstInLoop = true;
Pete Cooper1eed5b52011-12-22 02:05:40 +0000320 HoistOutOfLoop(N);
Evan Cheng6ea59492010-04-07 00:41:17 +0000321 CSEMap.clear();
Daniel Jasper15e69542015-03-14 10:58:38 +0000322
323 if (SinkInstsToAvoidSpills)
324 SinkIntoLoop();
Evan Cheng6ea59492010-04-07 00:41:17 +0000325 }
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000326 }
327
328 return Changed;
329}
330
Sanjay Patel87c6c072015-12-10 16:34:21 +0000331/// Return true if instruction stores to the specified frame.
Evan Cheng058b9f02010-04-08 01:03:47 +0000332static bool InstructionStoresToFI(const MachineInstr *MI, int FI) {
Philip Reames42bd26f2015-12-23 17:05:57 +0000333 // If we lost memory operands, conservatively assume that the instruction
334 // writes to all slots.
335 if (MI->memoperands_empty())
336 return true;
Sanjay Patel882a8ee2016-01-06 23:45:05 +0000337 for (const MachineMemOperand *MemOp : MI->memoperands()) {
338 if (!MemOp->isStore() || !MemOp->getPseudoValue())
Evan Cheng058b9f02010-04-08 01:03:47 +0000339 continue;
340 if (const FixedStackPseudoSourceValue *Value =
Sanjay Patel882a8ee2016-01-06 23:45:05 +0000341 dyn_cast<FixedStackPseudoSourceValue>(MemOp->getPseudoValue())) {
Evan Cheng058b9f02010-04-08 01:03:47 +0000342 if (Value->getFrameIndex() == FI)
343 return true;
344 }
345 }
346 return false;
347}
348
Sanjay Patel87c6c072015-12-10 16:34:21 +0000349/// Examine the instruction for potentai LICM candidate. Also
Evan Cheng058b9f02010-04-08 01:03:47 +0000350/// gather register def and frame object update information.
351void MachineLICM::ProcessMI(MachineInstr *MI,
Jakob Stoklund Olesen6b17ef52012-01-20 22:27:12 +0000352 BitVector &PhysRegDefs,
353 BitVector &PhysRegClobbers,
Evan Cheng058b9f02010-04-08 01:03:47 +0000354 SmallSet<int, 32> &StoredFIs,
Craig Topper2cd5ff82013-07-11 16:22:38 +0000355 SmallVectorImpl<CandidateInfo> &Candidates) {
Evan Cheng058b9f02010-04-08 01:03:47 +0000356 bool RuledOut = false;
Evan Cheng89e74792010-04-13 20:21:05 +0000357 bool HasNonInvariantUse = false;
Evan Cheng058b9f02010-04-08 01:03:47 +0000358 unsigned Def = 0;
Sanjay Patel882a8ee2016-01-06 23:45:05 +0000359 for (const MachineOperand &MO : MI->operands()) {
Evan Cheng058b9f02010-04-08 01:03:47 +0000360 if (MO.isFI()) {
361 // Remember if the instruction stores to the frame index.
362 int FI = MO.getIndex();
363 if (!StoredFIs.count(FI) &&
364 MFI->isSpillSlotObjectIndex(FI) &&
365 InstructionStoresToFI(MI, FI))
366 StoredFIs.insert(FI);
Evan Cheng89e74792010-04-13 20:21:05 +0000367 HasNonInvariantUse = true;
Evan Cheng058b9f02010-04-08 01:03:47 +0000368 continue;
369 }
370
Jakob Stoklund Olesen6b17ef52012-01-20 22:27:12 +0000371 // We can't hoist an instruction defining a physreg that is clobbered in
372 // the loop.
373 if (MO.isRegMask()) {
Jakob Stoklund Olesen5e1ac452012-02-02 23:52:57 +0000374 PhysRegClobbers.setBitsNotInMask(MO.getRegMask());
Jakob Stoklund Olesen6b17ef52012-01-20 22:27:12 +0000375 continue;
376 }
377
Evan Cheng058b9f02010-04-08 01:03:47 +0000378 if (!MO.isReg())
379 continue;
380 unsigned Reg = MO.getReg();
381 if (!Reg)
382 continue;
383 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
384 "Not expecting virtual register!");
385
Evan Cheng0a2aff22010-04-13 18:16:00 +0000386 if (!MO.isDef()) {
Jakob Stoklund Olesen6b17ef52012-01-20 22:27:12 +0000387 if (Reg && (PhysRegDefs.test(Reg) || PhysRegClobbers.test(Reg)))
Evan Cheng89e74792010-04-13 20:21:05 +0000388 // If it's using a non-loop-invariant register, then it's obviously not
389 // safe to hoist.
390 HasNonInvariantUse = true;
Evan Cheng058b9f02010-04-08 01:03:47 +0000391 continue;
Evan Cheng0a2aff22010-04-13 18:16:00 +0000392 }
Evan Cheng058b9f02010-04-08 01:03:47 +0000393
394 if (MO.isImplicit()) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000395 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
396 PhysRegClobbers.set(*AI);
Evan Cheng058b9f02010-04-08 01:03:47 +0000397 if (!MO.isDead())
398 // Non-dead implicit def? This cannot be hoisted.
399 RuledOut = true;
400 // No need to check if a dead implicit def is also defined by
401 // another instruction.
402 continue;
403 }
404
405 // FIXME: For now, avoid instructions with multiple defs, unless
406 // it's a dead implicit def.
407 if (Def)
408 RuledOut = true;
409 else
410 Def = Reg;
411
412 // If we have already seen another instruction that defines the same
Jakob Stoklund Olesen6b17ef52012-01-20 22:27:12 +0000413 // register, then this is not safe. Two defs is indicated by setting a
414 // PhysRegClobbers bit.
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000415 for (MCRegAliasIterator AS(Reg, TRI, true); AS.isValid(); ++AS) {
Jakob Stoklund Olesen20948fa2012-01-23 21:01:15 +0000416 if (PhysRegDefs.test(*AS))
417 PhysRegClobbers.set(*AS);
Jakob Stoklund Olesen20948fa2012-01-23 21:01:15 +0000418 PhysRegDefs.set(*AS);
Jakob Stoklund Olesen6b17ef52012-01-20 22:27:12 +0000419 }
Richard Sandiford96aa93d2013-08-20 09:11:13 +0000420 if (PhysRegClobbers.test(Reg))
421 // MI defined register is seen defined by another instruction in
422 // the loop, it cannot be a LICM candidate.
423 RuledOut = true;
Evan Cheng058b9f02010-04-08 01:03:47 +0000424 }
425
Evan Cheng0a2aff22010-04-13 18:16:00 +0000426 // Only consider reloads for now and remats which do not have register
427 // operands. FIXME: Consider unfold load folding instructions.
Evan Cheng058b9f02010-04-08 01:03:47 +0000428 if (Def && !RuledOut) {
Evan Cheng0a2aff22010-04-13 18:16:00 +0000429 int FI = INT_MIN;
Evan Cheng89e74792010-04-13 20:21:05 +0000430 if ((!HasNonInvariantUse && IsLICMCandidate(*MI)) ||
Evan Cheng0a2aff22010-04-13 18:16:00 +0000431 (TII->isLoadFromStackSlot(MI, FI) && MFI->isSpillSlotObjectIndex(FI)))
432 Candidates.push_back(CandidateInfo(MI, Def, FI));
Evan Cheng058b9f02010-04-08 01:03:47 +0000433 }
434}
435
Sanjay Patel87c6c072015-12-10 16:34:21 +0000436/// Walk the specified region of the CFG and hoist loop invariants out to the
437/// preheader.
Evan Cheng5fdb57c2010-04-17 07:07:11 +0000438void MachineLICM::HoistRegionPostRA() {
Evan Cheng7fede872012-03-27 01:50:58 +0000439 MachineBasicBlock *Preheader = getCurPreheader();
440 if (!Preheader)
441 return;
442
Evan Cheng6ea59492010-04-07 00:41:17 +0000443 unsigned NumRegs = TRI->getNumRegs();
Jakob Stoklund Olesen6b17ef52012-01-20 22:27:12 +0000444 BitVector PhysRegDefs(NumRegs); // Regs defined once in the loop.
445 BitVector PhysRegClobbers(NumRegs); // Regs defined more than once.
Evan Cheng6ea59492010-04-07 00:41:17 +0000446
Evan Cheng058b9f02010-04-08 01:03:47 +0000447 SmallVector<CandidateInfo, 32> Candidates;
Evan Cheng6ea59492010-04-07 00:41:17 +0000448 SmallSet<int, 32> StoredFIs;
449
450 // Walk the entire region, count number of defs for each register, and
Evan Cheng5fdb57c2010-04-17 07:07:11 +0000451 // collect potential LICM candidates.
Benjamin Kramer7d605262013-09-15 22:04:42 +0000452 const std::vector<MachineBasicBlock *> &Blocks = CurLoop->getBlocks();
Sanjay Patel882a8ee2016-01-06 23:45:05 +0000453 for (MachineBasicBlock *BB : Blocks) {
Bill Wendling918cea22011-10-12 02:58:01 +0000454 // If the header of the loop containing this basic block is a landing pad,
455 // then don't try to hoist instructions out of this loop.
456 const MachineLoop *ML = MLI->getLoopFor(BB);
Reid Kleckner0e288232015-08-27 23:27:47 +0000457 if (ML && ML->getHeader()->isEHPad()) continue;
Bill Wendling918cea22011-10-12 02:58:01 +0000458
Evan Cheng6ea59492010-04-07 00:41:17 +0000459 // Conservatively treat live-in's as an external def.
Evan Cheng058b9f02010-04-08 01:03:47 +0000460 // FIXME: That means a reload that're reused in successor block(s) will not
461 // be LICM'ed.
Matthias Braund9da1622015-09-09 18:08:03 +0000462 for (const auto &LI : BB->liveins()) {
463 for (MCRegAliasIterator AI(LI.PhysReg, TRI, true); AI.isValid(); ++AI)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000464 PhysRegDefs.set(*AI);
Evan Cheng6ea59492010-04-07 00:41:17 +0000465 }
466
Evan Chengf192ca02011-10-11 23:48:44 +0000467 SpeculationState = SpeculateUnknown;
Sanjay Patel882a8ee2016-01-06 23:45:05 +0000468 for (MachineInstr &MI : *BB)
469 ProcessMI(&MI, PhysRegDefs, PhysRegClobbers, StoredFIs, Candidates);
Evan Cheng5fdb57c2010-04-17 07:07:11 +0000470 }
Evan Cheng6ea59492010-04-07 00:41:17 +0000471
Evan Cheng7fede872012-03-27 01:50:58 +0000472 // Gather the registers read / clobbered by the terminator.
473 BitVector TermRegs(NumRegs);
474 MachineBasicBlock::iterator TI = Preheader->getFirstTerminator();
475 if (TI != Preheader->end()) {
Sanjay Patel882a8ee2016-01-06 23:45:05 +0000476 for (const MachineOperand &MO : TI->operands()) {
Evan Cheng7fede872012-03-27 01:50:58 +0000477 if (!MO.isReg())
478 continue;
479 unsigned Reg = MO.getReg();
480 if (!Reg)
481 continue;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000482 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
483 TermRegs.set(*AI);
Evan Cheng7fede872012-03-27 01:50:58 +0000484 }
485 }
486
Evan Cheng6ea59492010-04-07 00:41:17 +0000487 // Now evaluate whether the potential candidates qualify.
488 // 1. Check if the candidate defined register is defined by another
489 // instruction in the loop.
490 // 2. If the candidate is a load from stack slot (always true for now),
491 // check if the slot is stored anywhere in the loop.
Evan Cheng7fede872012-03-27 01:50:58 +0000492 // 3. Make sure candidate def should not clobber
493 // registers read by the terminator. Similarly its def should not be
494 // clobbered by the terminator.
Sanjay Patel882a8ee2016-01-06 23:45:05 +0000495 for (CandidateInfo &Candidate : Candidates) {
496 if (Candidate.FI != INT_MIN &&
497 StoredFIs.count(Candidate.FI))
Evan Cheng6ea59492010-04-07 00:41:17 +0000498 continue;
499
Sanjay Patel882a8ee2016-01-06 23:45:05 +0000500 unsigned Def = Candidate.Def;
Evan Cheng7fede872012-03-27 01:50:58 +0000501 if (!PhysRegClobbers.test(Def) && !TermRegs.test(Def)) {
Evan Cheng89e74792010-04-13 20:21:05 +0000502 bool Safe = true;
Sanjay Patel882a8ee2016-01-06 23:45:05 +0000503 MachineInstr *MI = Candidate.MI;
504 for (const MachineOperand &MO : MI->operands()) {
Evan Cheng87585d72010-04-13 22:13:34 +0000505 if (!MO.isReg() || MO.isDef() || !MO.getReg())
Evan Cheng89e74792010-04-13 20:21:05 +0000506 continue;
Evan Cheng7fede872012-03-27 01:50:58 +0000507 unsigned Reg = MO.getReg();
508 if (PhysRegDefs.test(Reg) ||
509 PhysRegClobbers.test(Reg)) {
Evan Cheng89e74792010-04-13 20:21:05 +0000510 // If it's using a non-loop-invariant register, then it's obviously
511 // not safe to hoist.
512 Safe = false;
513 break;
514 }
515 }
516 if (Safe)
Sanjay Patel882a8ee2016-01-06 23:45:05 +0000517 HoistPostRA(MI, Candidate.Def);
Evan Cheng89e74792010-04-13 20:21:05 +0000518 }
Evan Cheng6ea59492010-04-07 00:41:17 +0000519 }
520}
521
Sanjay Patel87c6c072015-12-10 16:34:21 +0000522/// Add register 'Reg' to the livein sets of BBs in the current loop, and make
523/// sure it is not killed by any instructions in the loop.
Evan Cheng5fdb57c2010-04-17 07:07:11 +0000524void MachineLICM::AddToLiveIns(unsigned Reg) {
Benjamin Kramer7d605262013-09-15 22:04:42 +0000525 const std::vector<MachineBasicBlock *> &Blocks = CurLoop->getBlocks();
Sanjay Patel882a8ee2016-01-06 23:45:05 +0000526 for (MachineBasicBlock *BB : Blocks) {
Jakob Stoklund Olesen011207a2010-04-20 18:45:47 +0000527 if (!BB->isLiveIn(Reg))
528 BB->addLiveIn(Reg);
Sanjay Patel882a8ee2016-01-06 23:45:05 +0000529 for (MachineInstr &MI : *BB) {
530 for (MachineOperand &MO : MI.operands()) {
Jakob Stoklund Olesen011207a2010-04-20 18:45:47 +0000531 if (!MO.isReg() || !MO.getReg() || MO.isDef()) continue;
532 if (MO.getReg() == Reg || TRI->isSuperRegister(Reg, MO.getReg()))
533 MO.setIsKill(false);
534 }
535 }
536 }
Evan Cheng058b9f02010-04-08 01:03:47 +0000537}
538
Sanjay Patel87c6c072015-12-10 16:34:21 +0000539/// When an instruction is found to only use loop invariant operands that is
540/// safe to hoist, this instruction is called to do the dirty work.
Evan Cheng058b9f02010-04-08 01:03:47 +0000541void MachineLICM::HoistPostRA(MachineInstr *MI, unsigned Def) {
Dan Gohman3570f812010-06-22 17:25:57 +0000542 MachineBasicBlock *Preheader = getCurPreheader();
Dan Gohman3570f812010-06-22 17:25:57 +0000543
Evan Cheng6ea59492010-04-07 00:41:17 +0000544 // Now move the instructions to the predecessor, inserting it before any
545 // terminator instructions.
Jakob Stoklund Olesen90823532012-01-23 21:01:11 +0000546 DEBUG(dbgs() << "Hoisting to BB#" << Preheader->getNumber() << " from BB#"
547 << MI->getParent()->getNumber() << ": " << *MI);
Evan Cheng6ea59492010-04-07 00:41:17 +0000548
549 // Splice the instruction to the preheader.
Evan Cheng058b9f02010-04-08 01:03:47 +0000550 MachineBasicBlock *MBB = MI->getParent();
Dan Gohman3570f812010-06-22 17:25:57 +0000551 Preheader->splice(Preheader->getFirstTerminator(), MBB, MI);
Evan Cheng058b9f02010-04-08 01:03:47 +0000552
Andrew Trick5209c732012-02-08 21:23:00 +0000553 // Add register to livein list to all the BBs in the current loop since a
Evan Cheng5fdb57c2010-04-17 07:07:11 +0000554 // loop invariant must be kept live throughout the whole loop. This is
555 // important to ensure later passes do not scavenge the def register.
556 AddToLiveIns(Def);
Evan Cheng6ea59492010-04-07 00:41:17 +0000557
558 ++NumPostRAHoisted;
559 Changed = true;
560}
561
Sanjay Patel87c6c072015-12-10 16:34:21 +0000562/// Check if this mbb is guaranteed to execute. If not then a load from this mbb
563/// may not be safe to hoist.
Devang Patel453d4012011-10-11 18:09:58 +0000564bool MachineLICM::IsGuaranteedToExecute(MachineBasicBlock *BB) {
Evan Chengf192ca02011-10-11 23:48:44 +0000565 if (SpeculationState != SpeculateUnknown)
566 return SpeculationState == SpeculateFalse;
Andrew Trick5209c732012-02-08 21:23:00 +0000567
Devang Patel453d4012011-10-11 18:09:58 +0000568 if (BB != CurLoop->getHeader()) {
569 // Check loop exiting blocks.
570 SmallVector<MachineBasicBlock*, 8> CurrentLoopExitingBlocks;
571 CurLoop->getExitingBlocks(CurrentLoopExitingBlocks);
Sanjay Patel882a8ee2016-01-06 23:45:05 +0000572 for (MachineBasicBlock *CurrentLoopExitingBlock : CurrentLoopExitingBlocks)
573 if (!DT->dominates(BB, CurrentLoopExitingBlock)) {
Nick Lewycky404feb92011-10-13 01:09:50 +0000574 SpeculationState = SpeculateTrue;
575 return false;
Devang Patel453d4012011-10-11 18:09:58 +0000576 }
577 }
578
Evan Chengf192ca02011-10-11 23:48:44 +0000579 SpeculationState = SpeculateFalse;
580 return true;
Devang Patel453d4012011-10-11 18:09:58 +0000581}
582
Pete Cooper1eed5b52011-12-22 02:05:40 +0000583void MachineLICM::EnterScope(MachineBasicBlock *MBB) {
Justin Lebarf6f4a2a2016-05-23 18:56:07 +0000584 DEBUG(dbgs() << "Entering BB#" << MBB->getNumber() << '\n');
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000585
Pete Cooper1eed5b52011-12-22 02:05:40 +0000586 // Remember livein register pressure.
587 BackTrace.push_back(RegPressure);
588}
Bill Wendling918cea22011-10-12 02:58:01 +0000589
Pete Cooper1eed5b52011-12-22 02:05:40 +0000590void MachineLICM::ExitScope(MachineBasicBlock *MBB) {
Justin Lebarf6f4a2a2016-05-23 18:56:07 +0000591 DEBUG(dbgs() << "Exiting BB#" << MBB->getNumber() << '\n');
Pete Cooper1eed5b52011-12-22 02:05:40 +0000592 BackTrace.pop_back();
593}
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000594
Sanjay Patel87c6c072015-12-10 16:34:21 +0000595/// Destroy scope for the MBB that corresponds to the given dominator tree node
596/// if its a leaf or all of its children are done. Walk up the dominator tree to
597/// destroy ancestors which are now done.
Pete Cooper1eed5b52011-12-22 02:05:40 +0000598void MachineLICM::ExitScopeIfDone(MachineDomTreeNode *Node,
Evan Chengda468322012-01-10 22:27:32 +0000599 DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren,
600 DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> &ParentMap) {
Pete Cooper1eed5b52011-12-22 02:05:40 +0000601 if (OpenChildren[Node])
Evan Cheng44436302010-10-16 02:20:26 +0000602 return;
Evan Chengd62719c2010-10-14 01:16:09 +0000603
Pete Cooper1eed5b52011-12-22 02:05:40 +0000604 // Pop scope.
605 ExitScope(Node->getBlock());
606
607 // Now traverse upwards to pop ancestors whose offsprings are all done.
608 while (MachineDomTreeNode *Parent = ParentMap[Node]) {
609 unsigned Left = --OpenChildren[Parent];
610 if (Left != 0)
611 break;
612 ExitScope(Parent->getBlock());
613 Node = Parent;
614 }
615}
616
Sanjay Patel87c6c072015-12-10 16:34:21 +0000617/// Walk the specified loop in the CFG (defined by all blocks dominated by the
618/// specified header block, and that are in the current loop) in depth first
619/// order w.r.t the DominatorTree. This allows us to visit definitions before
620/// uses, allowing us to hoist a loop body in one pass without iteration.
Pete Cooper1eed5b52011-12-22 02:05:40 +0000621///
622void MachineLICM::HoistOutOfLoop(MachineDomTreeNode *HeaderN) {
Daniel Jasper4bb224d2015-02-05 22:39:46 +0000623 MachineBasicBlock *Preheader = getCurPreheader();
624 if (!Preheader)
625 return;
626
Pete Cooper1eed5b52011-12-22 02:05:40 +0000627 SmallVector<MachineDomTreeNode*, 32> Scopes;
628 SmallVector<MachineDomTreeNode*, 8> WorkList;
629 DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> ParentMap;
630 DenseMap<MachineDomTreeNode*, unsigned> OpenChildren;
631
632 // Perform a DFS walk to determine the order of visit.
633 WorkList.push_back(HeaderN);
Daniel Jasper4bb224d2015-02-05 22:39:46 +0000634 while (!WorkList.empty()) {
Pete Cooper1eed5b52011-12-22 02:05:40 +0000635 MachineDomTreeNode *Node = WorkList.pop_back_val();
Craig Topperc0196b12014-04-14 00:51:57 +0000636 assert(Node && "Null dominator tree node?");
Pete Cooper1eed5b52011-12-22 02:05:40 +0000637 MachineBasicBlock *BB = Node->getBlock();
638
639 // If the header of the loop containing this basic block is a landing pad,
640 // then don't try to hoist instructions out of this loop.
641 const MachineLoop *ML = MLI->getLoopFor(BB);
Reid Kleckner0e288232015-08-27 23:27:47 +0000642 if (ML && ML->getHeader()->isEHPad())
Pete Cooper1eed5b52011-12-22 02:05:40 +0000643 continue;
644
645 // If this subregion is not in the top level loop at all, exit.
646 if (!CurLoop->contains(BB))
647 continue;
648
649 Scopes.push_back(Node);
650 const std::vector<MachineDomTreeNode*> &Children = Node->getChildren();
651 unsigned NumChildren = Children.size();
652
653 // Don't hoist things out of a large switch statement. This often causes
654 // code to be hoisted that wasn't going to be executed, and increases
655 // register pressure in a situation where it's likely to matter.
656 if (BB->succ_size() >= 25)
657 NumChildren = 0;
658
659 OpenChildren[Node] = NumChildren;
660 // Add children in reverse order as then the next popped worklist node is
661 // the first child of this node. This means we ultimately traverse the
662 // DOM tree in exactly the same order as if we'd recursed.
663 for (int i = (int)NumChildren-1; i >= 0; --i) {
664 MachineDomTreeNode *Child = Children[i];
665 ParentMap[Child] = Node;
666 WorkList.push_back(Child);
667 }
Daniel Dunbar418204e2010-10-19 17:14:24 +0000668 }
Evan Cheng8249dfe2010-10-19 00:55:07 +0000669
Daniel Jasper4bb224d2015-02-05 22:39:46 +0000670 if (Scopes.size() == 0)
671 return;
672
673 // Compute registers which are livein into the loop headers.
674 RegSeen.clear();
675 BackTrace.clear();
676 InitRegPressure(Preheader);
677
Pete Cooper1eed5b52011-12-22 02:05:40 +0000678 // Now perform LICM.
Sanjay Patel882a8ee2016-01-06 23:45:05 +0000679 for (MachineDomTreeNode *Node : Scopes) {
Pete Cooper1eed5b52011-12-22 02:05:40 +0000680 MachineBasicBlock *MBB = Node->getBlock();
Evan Cheng63c76082010-10-19 18:58:51 +0000681
Pete Cooper1eed5b52011-12-22 02:05:40 +0000682 EnterScope(MBB);
683
684 // Process the block
685 SpeculationState = SpeculateUnknown;
686 for (MachineBasicBlock::iterator
687 MII = MBB->begin(), E = MBB->end(); MII != E; ) {
688 MachineBasicBlock::iterator NextMII = MII; ++NextMII;
689 MachineInstr *MI = &*MII;
690 if (!Hoist(MI, Preheader))
691 UpdateRegPressure(MI);
692 MII = NextMII;
693 }
694
695 // If it's a leaf node, it's done. Traverse upwards to pop ancestors.
696 ExitScopeIfDone(Node, OpenChildren, ParentMap);
Dan Gohman79618d12009-01-15 22:01:38 +0000697 }
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000698}
699
Sanjay Patel87c6c072015-12-10 16:34:21 +0000700/// Sink instructions into loops if profitable. This especially tries to prevent
701/// register spills caused by register pressure if there is little to no
702/// overhead moving instructions into loops.
Daniel Jasper15e69542015-03-14 10:58:38 +0000703void MachineLICM::SinkIntoLoop() {
704 MachineBasicBlock *Preheader = getCurPreheader();
705 if (!Preheader)
706 return;
707
708 SmallVector<MachineInstr *, 8> Candidates;
709 for (MachineBasicBlock::instr_iterator I = Preheader->instr_begin();
710 I != Preheader->instr_end(); ++I) {
711 // We need to ensure that we can safely move this instruction into the loop.
712 // As such, it must not have side-effects, e.g. such as a call has.
Duncan P. N. Exon Smith5ec15682015-10-09 19:40:45 +0000713 if (IsLoopInvariantInst(*I) && !HasLoopPHIUse(&*I))
714 Candidates.push_back(&*I);
Daniel Jasper15e69542015-03-14 10:58:38 +0000715 }
716
717 for (MachineInstr *I : Candidates) {
718 const MachineOperand &MO = I->getOperand(0);
719 if (!MO.isDef() || !MO.isReg() || !MO.getReg())
720 continue;
721 if (!MRI->hasOneDef(MO.getReg()))
722 continue;
723 bool CanSink = true;
724 MachineBasicBlock *B = nullptr;
725 for (MachineInstr &MI : MRI->use_instructions(MO.getReg())) {
726 // FIXME: Come up with a proper cost model that estimates whether sinking
727 // the instruction (and thus possibly executing it on every loop
728 // iteration) is more expensive than a register.
729 // For now assumes that copies are cheap and thus almost always worth it.
730 if (!MI.isCopy()) {
731 CanSink = false;
732 break;
733 }
734 if (!B) {
735 B = MI.getParent();
736 continue;
737 }
738 B = DT->findNearestCommonDominator(B, MI.getParent());
739 if (!B) {
740 CanSink = false;
741 break;
742 }
743 }
744 if (!CanSink || !B || B == Preheader)
745 continue;
746 B->splice(B->getFirstNonPHI(), Preheader, I);
747 }
748}
749
Evan Cheng87066f02010-10-20 22:03:58 +0000750static bool isOperandKill(const MachineOperand &MO, MachineRegisterInfo *MRI) {
751 return MO.isKill() || MRI->hasOneNonDBGUse(MO.getReg());
752}
753
Sanjay Patel87c6c072015-12-10 16:34:21 +0000754/// Find all virtual register references that are liveout of the preheader to
755/// initialize the starting "register pressure". Note this does not count live
756/// through (livein but not used) registers.
Evan Chengd62719c2010-10-14 01:16:09 +0000757void MachineLICM::InitRegPressure(MachineBasicBlock *BB) {
Evan Chengd62719c2010-10-14 01:16:09 +0000758 std::fill(RegPressure.begin(), RegPressure.end(), 0);
Evan Cheng44436302010-10-16 02:20:26 +0000759
Evan Cheng87066f02010-10-20 22:03:58 +0000760 // If the preheader has only a single predecessor and it ends with a
761 // fallthrough or an unconditional branch, then scan its predecessor for live
762 // defs as well. This happens whenever the preheader is created by splitting
763 // the critical edge from the loop predecessor to the loop header.
764 if (BB->pred_size() == 1) {
Craig Topperc0196b12014-04-14 00:51:57 +0000765 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
Evan Cheng87066f02010-10-20 22:03:58 +0000766 SmallVector<MachineOperand, 4> Cond;
767 if (!TII->AnalyzeBranch(*BB, TBB, FBB, Cond, false) && Cond.empty())
768 InitRegPressure(*BB->pred_begin());
769 }
770
Daniel Jaspere87e82b2015-04-07 16:42:35 +0000771 for (const MachineInstr &MI : *BB)
772 UpdateRegPressure(&MI, /*ConsiderUnseenAsDef=*/true);
Evan Chengd62719c2010-10-14 01:16:09 +0000773}
774
Sanjay Patel87c6c072015-12-10 16:34:21 +0000775/// Update estimate of register pressure after the specified instruction.
Daniel Jaspere87e82b2015-04-07 16:42:35 +0000776void MachineLICM::UpdateRegPressure(const MachineInstr *MI,
777 bool ConsiderUnseenAsDef) {
778 auto Cost = calcRegisterCost(MI, /*ConsiderSeen=*/true, ConsiderUnseenAsDef);
Daniel Jasper274928f2015-04-14 11:56:25 +0000779 for (const auto &RPIdAndCost : Cost) {
780 unsigned Class = RPIdAndCost.first;
781 if (static_cast<int>(RegPressure[Class]) < -RPIdAndCost.second)
Daniel Jaspere87e82b2015-04-07 16:42:35 +0000782 RegPressure[Class] = 0;
783 else
Daniel Jasper274928f2015-04-14 11:56:25 +0000784 RegPressure[Class] += RPIdAndCost.second;
Daniel Jaspere87e82b2015-04-07 16:42:35 +0000785 }
786}
Evan Chengd62719c2010-10-14 01:16:09 +0000787
Sanjay Patel87c6c072015-12-10 16:34:21 +0000788/// Calculate the additional register pressure that the registers used in MI
789/// cause.
790///
791/// If 'ConsiderSeen' is true, updates 'RegSeen' and uses the information to
792/// figure out which usages are live-ins.
793/// FIXME: Figure out a way to consider 'RegSeen' from all code paths.
Daniel Jaspere87e82b2015-04-07 16:42:35 +0000794DenseMap<unsigned, int>
795MachineLICM::calcRegisterCost(const MachineInstr *MI, bool ConsiderSeen,
796 bool ConsiderUnseenAsDef) {
797 DenseMap<unsigned, int> Cost;
798 if (MI->isImplicitDef())
799 return Cost;
Evan Chengd62719c2010-10-14 01:16:09 +0000800 for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) {
801 const MachineOperand &MO = MI->getOperand(i);
Evan Cheng63c76082010-10-19 18:58:51 +0000802 if (!MO.isReg() || MO.isImplicit())
Evan Chengd62719c2010-10-14 01:16:09 +0000803 continue;
804 unsigned Reg = MO.getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +0000805 if (!TargetRegisterInfo::isVirtualRegister(Reg))
Evan Chengd62719c2010-10-14 01:16:09 +0000806 continue;
807
Daniel Jaspere87e82b2015-04-07 16:42:35 +0000808 // FIXME: It seems bad to use RegSeen only for some of these calculations.
809 bool isNew = ConsiderSeen ? RegSeen.insert(Reg).second : false;
Daniel Jasper274928f2015-04-14 11:56:25 +0000810 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
811
812 RegClassWeight W = TRI->getRegClassWeight(RC);
813 int RCCost = 0;
Evan Cheng63c76082010-10-19 18:58:51 +0000814 if (MO.isDef())
Daniel Jasper274928f2015-04-14 11:56:25 +0000815 RCCost = W.RegWeight;
Daniel Jaspere87e82b2015-04-07 16:42:35 +0000816 else {
817 bool isKill = isOperandKill(MO, MRI);
818 if (isNew && !isKill && ConsiderUnseenAsDef)
819 // Haven't seen this, it must be a livein.
Daniel Jasper274928f2015-04-14 11:56:25 +0000820 RCCost = W.RegWeight;
Daniel Jaspere87e82b2015-04-07 16:42:35 +0000821 else if (!isNew && isKill)
Daniel Jasper274928f2015-04-14 11:56:25 +0000822 RCCost = -W.RegWeight;
823 }
824 if (RCCost == 0)
825 continue;
826 const int *PS = TRI->getRegClassPressureSets(RC);
827 for (; *PS != -1; ++PS) {
828 if (Cost.find(*PS) == Cost.end())
829 Cost[*PS] = RCCost;
830 else
831 Cost[*PS] += RCCost;
Evan Cheng44436302010-10-16 02:20:26 +0000832 }
Evan Chengd62719c2010-10-14 01:16:09 +0000833 }
Daniel Jaspere87e82b2015-04-07 16:42:35 +0000834 return Cost;
Evan Chengd62719c2010-10-14 01:16:09 +0000835}
836
Sanjay Patel87c6c072015-12-10 16:34:21 +0000837/// Return true if this machine instruction loads from global offset table or
838/// constant pool.
Philip Reames42bd26f2015-12-23 17:05:57 +0000839static bool mayLoadFromGOTOrConstantPool(MachineInstr &MI) {
Evan Cheng7f8e5632011-12-07 07:15:52 +0000840 assert (MI.mayLoad() && "Expected MI that loads!");
Philip Reames42bd26f2015-12-23 17:05:57 +0000841
842 // If we lost memory operands, conservatively assume that the instruction
843 // reads from everything..
844 if (MI.memoperands_empty())
845 return true;
846
Sanjay Patel882a8ee2016-01-06 23:45:05 +0000847 for (MachineMemOperand *MemOp : MI.memoperands())
848 if (const PseudoSourceValue *PSV = MemOp->getPseudoValue())
Alex Lorenze40c8a22015-08-11 23:09:45 +0000849 if (PSV->isGOT() || PSV->isConstantPool())
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000850 return true;
Sanjay Patel882a8ee2016-01-06 23:45:05 +0000851
Devang Patel69a45652011-10-17 17:35:01 +0000852 return false;
853}
854
Sanjay Patel87c6c072015-12-10 16:34:21 +0000855/// Returns true if the instruction may be a suitable candidate for LICM.
856/// e.g. If the instruction is a call, then it's obviously not safe to hoist it.
Evan Cheng0a2aff22010-04-13 18:16:00 +0000857bool MachineLICM::IsLICMCandidate(MachineInstr &I) {
Chris Lattner0b7ae202010-07-12 00:00:35 +0000858 // Check if it's safe to move the instruction.
859 bool DontMoveAcrossStore = true;
Matthias Braun07066cc2015-05-19 21:22:20 +0000860 if (!I.isSafeToMove(AA, DontMoveAcrossStore))
Chris Lattnerc8226f32008-01-10 23:08:24 +0000861 return false;
Devang Patel453d4012011-10-11 18:09:58 +0000862
863 // If it is load then check if it is guaranteed to execute by making sure that
864 // it dominates all exiting blocks. If it doesn't, then there is a path out of
Devang Patel830c7762011-10-20 17:31:18 +0000865 // the loop which does not execute this load, so we can't hoist it. Loads
866 // from constant memory are not safe to speculate all the time, for example
867 // indexed load from a jump table.
Devang Patel453d4012011-10-11 18:09:58 +0000868 // Stores and side effects are already checked by isSafeToMove.
Philip Reames42bd26f2015-12-23 17:05:57 +0000869 if (I.mayLoad() && !mayLoadFromGOTOrConstantPool(I) &&
Devang Patel69a45652011-10-17 17:35:01 +0000870 !IsGuaranteedToExecute(I.getParent()))
Devang Patel453d4012011-10-11 18:09:58 +0000871 return false;
872
Evan Cheng0a2aff22010-04-13 18:16:00 +0000873 return true;
874}
875
Sanjay Patel87c6c072015-12-10 16:34:21 +0000876/// Returns true if the instruction is loop invariant.
877/// I.e., all virtual register operands are defined outside of the loop,
878/// physical registers aren't accessed explicitly, and there are no side
Evan Cheng0a2aff22010-04-13 18:16:00 +0000879/// effects that aren't captured by the operands or other flags.
Andrew Trick5209c732012-02-08 21:23:00 +0000880///
Evan Cheng0a2aff22010-04-13 18:16:00 +0000881bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) {
882 if (!IsLICMCandidate(I))
883 return false;
Bill Wendling2823eae2008-03-10 08:13:01 +0000884
Bill Wendling70613b82008-05-12 19:38:32 +0000885 // The instruction is loop invariant if all of its operands are.
Sanjay Patel882a8ee2016-01-06 23:45:05 +0000886 for (const MachineOperand &MO : I.operands()) {
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000887 if (!MO.isReg())
Bill Wendlingcd01e892008-08-20 20:32:05 +0000888 continue;
889
Dan Gohman79618d12009-01-15 22:01:38 +0000890 unsigned Reg = MO.getReg();
891 if (Reg == 0) continue;
892
893 // Don't hoist an instruction that uses or defines a physical register.
Dan Gohmane30d63f2009-09-25 23:58:45 +0000894 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Dan Gohmane30d63f2009-09-25 23:58:45 +0000895 if (MO.isUse()) {
896 // If the physreg has no defs anywhere, it's just an ambient register
Dan Gohman2f5bdcb2009-09-26 02:34:00 +0000897 // and we can freely move its uses. Alternatively, if it's allocatable,
898 // it could get allocated to something with a def during allocation.
Jakob Stoklund Olesen86ae07f2012-01-16 22:34:08 +0000899 if (!MRI->isConstantPhysReg(Reg, *I.getParent()->getParent()))
Dan Gohmane30d63f2009-09-25 23:58:45 +0000900 return false;
Dan Gohmane30d63f2009-09-25 23:58:45 +0000901 // Otherwise it's safe to move.
902 continue;
903 } else if (!MO.isDead()) {
904 // A def that isn't dead. We can't move it.
905 return false;
Dan Gohman6fb6a592010-02-28 00:08:44 +0000906 } else if (CurLoop->getHeader()->isLiveIn(Reg)) {
907 // If the reg is live into the loop, we can't hoist an instruction
908 // which would clobber it.
909 return false;
Dan Gohmane30d63f2009-09-25 23:58:45 +0000910 }
911 }
Bill Wendlingcd01e892008-08-20 20:32:05 +0000912
913 if (!MO.isUse())
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000914 continue;
915
Evan Chengd62719c2010-10-14 01:16:09 +0000916 assert(MRI->getVRegDef(Reg) &&
Bill Wendling70613b82008-05-12 19:38:32 +0000917 "Machine instr not mapped for this vreg?!");
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000918
919 // If the loop contains the definition of an operand, then the instruction
920 // isn't loop invariant.
Evan Chengd62719c2010-10-14 01:16:09 +0000921 if (CurLoop->contains(MRI->getVRegDef(Reg)))
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000922 return false;
923 }
924
925 // If we got this far, the instruction is loop invariant!
926 return true;
927}
928
Evan Cheng399660c2009-02-05 08:45:46 +0000929
Sanjay Patel87c6c072015-12-10 16:34:21 +0000930/// Return true if the specified instruction is used by a phi node and hoisting
931/// it could cause a copy to be inserted.
Jakob Stoklund Olesena3e86a62012-04-11 00:00:26 +0000932bool MachineLICM::HasLoopPHIUse(const MachineInstr *MI) const {
933 SmallVector<const MachineInstr*, 8> Work(1, MI);
934 do {
935 MI = Work.pop_back_val();
Matthias Braune41e1462015-05-29 02:56:46 +0000936 for (const MachineOperand &MO : MI->operands()) {
937 if (!MO.isReg() || !MO.isDef())
Jakob Stoklund Olesena3e86a62012-04-11 00:00:26 +0000938 continue;
Matthias Braune41e1462015-05-29 02:56:46 +0000939 unsigned Reg = MO.getReg();
Jakob Stoklund Olesena3e86a62012-04-11 00:00:26 +0000940 if (!TargetRegisterInfo::isVirtualRegister(Reg))
941 continue;
Owen Andersonb36376e2014-03-17 19:36:09 +0000942 for (MachineInstr &UseMI : MRI->use_instructions(Reg)) {
Jakob Stoklund Olesena3e86a62012-04-11 00:00:26 +0000943 // A PHI may cause a copy to be inserted.
Owen Andersonb36376e2014-03-17 19:36:09 +0000944 if (UseMI.isPHI()) {
Jakob Stoklund Olesena3e86a62012-04-11 00:00:26 +0000945 // A PHI inside the loop causes a copy because the live range of Reg is
946 // extended across the PHI.
Owen Andersonb36376e2014-03-17 19:36:09 +0000947 if (CurLoop->contains(&UseMI))
Jakob Stoklund Olesena3e86a62012-04-11 00:00:26 +0000948 return true;
949 // A PHI in an exit block can cause a copy to be inserted if the PHI
950 // has multiple predecessors in the loop with different values.
951 // For now, approximate by rejecting all exit blocks.
Owen Andersonb36376e2014-03-17 19:36:09 +0000952 if (isExitBlock(UseMI.getParent()))
Jakob Stoklund Olesena3e86a62012-04-11 00:00:26 +0000953 return true;
954 continue;
955 }
956 // Look past copies as well.
Owen Andersonb36376e2014-03-17 19:36:09 +0000957 if (UseMI.isCopy() && CurLoop->contains(&UseMI))
958 Work.push_back(&UseMI);
Jakob Stoklund Olesena3e86a62012-04-11 00:00:26 +0000959 }
Evan Chengef42bea2011-04-11 21:09:18 +0000960 }
Jakob Stoklund Olesena3e86a62012-04-11 00:00:26 +0000961 } while (!Work.empty());
Evan Cheng399660c2009-02-05 08:45:46 +0000962 return false;
Evan Cheng1d9f7ac2009-02-04 09:19:56 +0000963}
964
Sanjay Patel87c6c072015-12-10 16:34:21 +0000965/// Compute operand latency between a def of 'Reg' and an use in the current
966/// loop, return true if the target considered it high.
Evan Cheng63c76082010-10-19 18:58:51 +0000967bool MachineLICM::HasHighOperandLatency(MachineInstr &MI,
Evan Chenge96b8d72010-10-26 02:08:50 +0000968 unsigned DefIdx, unsigned Reg) const {
Matthias Braun88e21312015-06-13 03:42:11 +0000969 if (MRI->use_nodbg_empty(Reg))
Evan Cheng63c76082010-10-19 18:58:51 +0000970 return false;
Evan Chengd62719c2010-10-14 01:16:09 +0000971
Owen Andersonb36376e2014-03-17 19:36:09 +0000972 for (MachineInstr &UseMI : MRI->use_nodbg_instructions(Reg)) {
973 if (UseMI.isCopyLike())
Evan Chenge96b8d72010-10-26 02:08:50 +0000974 continue;
Owen Andersonb36376e2014-03-17 19:36:09 +0000975 if (!CurLoop->contains(UseMI.getParent()))
Evan Chengd62719c2010-10-14 01:16:09 +0000976 continue;
Owen Andersonb36376e2014-03-17 19:36:09 +0000977 for (unsigned i = 0, e = UseMI.getNumOperands(); i != e; ++i) {
978 const MachineOperand &MO = UseMI.getOperand(i);
Evan Chengd62719c2010-10-14 01:16:09 +0000979 if (!MO.isReg() || !MO.isUse())
980 continue;
981 unsigned MOReg = MO.getReg();
982 if (MOReg != Reg)
983 continue;
984
Matthias Braun88e21312015-06-13 03:42:11 +0000985 if (TII->hasHighOperandLatency(SchedModel, MRI, &MI, DefIdx, &UseMI, i))
Evan Cheng63c76082010-10-19 18:58:51 +0000986 return true;
Evan Chengd62719c2010-10-14 01:16:09 +0000987 }
988
Evan Cheng63c76082010-10-19 18:58:51 +0000989 // Only look at the first in loop use.
990 break;
Evan Chengd62719c2010-10-14 01:16:09 +0000991 }
992
Evan Cheng63c76082010-10-19 18:58:51 +0000993 return false;
Evan Chengd62719c2010-10-14 01:16:09 +0000994}
995
Sanjay Patel87c6c072015-12-10 16:34:21 +0000996/// Return true if the instruction is marked "cheap" or the operand latency
997/// between its def and a use is one or less.
Evan Chenge96b8d72010-10-26 02:08:50 +0000998bool MachineLICM::IsCheapInstruction(MachineInstr &MI) const {
Jiangning Liuc3053122014-07-29 01:55:19 +0000999 if (TII->isAsCheapAsAMove(&MI) || MI.isCopyLike())
Evan Chenge96b8d72010-10-26 02:08:50 +00001000 return true;
Evan Chenge96b8d72010-10-26 02:08:50 +00001001
1002 bool isCheap = false;
1003 unsigned NumDefs = MI.getDesc().getNumDefs();
1004 for (unsigned i = 0, e = MI.getNumOperands(); NumDefs && i != e; ++i) {
1005 MachineOperand &DefMO = MI.getOperand(i);
1006 if (!DefMO.isReg() || !DefMO.isDef())
1007 continue;
1008 --NumDefs;
1009 unsigned Reg = DefMO.getReg();
1010 if (TargetRegisterInfo::isPhysicalRegister(Reg))
1011 continue;
1012
Matthias Braun88e21312015-06-13 03:42:11 +00001013 if (!TII->hasLowDefLatency(SchedModel, &MI, i))
Evan Chenge96b8d72010-10-26 02:08:50 +00001014 return false;
1015 isCheap = true;
1016 }
1017
1018 return isCheap;
1019}
1020
Sanjay Patel87c6c072015-12-10 16:34:21 +00001021/// Visit BBs from header to current BB, check if hoisting an instruction of the
1022/// given cost matrix can cause high register pressure.
Daniel Jasperefece522015-04-03 16:19:48 +00001023bool MachineLICM::CanCauseHighRegPressure(const DenseMap<unsigned, int>& Cost,
Jakob Stoklund Olesen645bdd42012-04-11 00:00:28 +00001024 bool CheapInstr) {
Daniel Jasper274928f2015-04-14 11:56:25 +00001025 for (const auto &RPIdAndCost : Cost) {
1026 if (RPIdAndCost.second <= 0)
Evan Cheng87066f02010-10-20 22:03:58 +00001027 continue;
1028
Daniel Jasper274928f2015-04-14 11:56:25 +00001029 unsigned Class = RPIdAndCost.first;
Daniel Jasperefece522015-04-03 16:19:48 +00001030 int Limit = RegLimit[Class];
Jakob Stoklund Olesen645bdd42012-04-11 00:00:28 +00001031
1032 // Don't hoist cheap instructions if they would increase register pressure,
1033 // even if we're under the limit.
Hal Finkel0709f512015-01-08 22:10:48 +00001034 if (CheapInstr && !HoistCheapInsts)
Jakob Stoklund Olesen645bdd42012-04-11 00:00:28 +00001035 return true;
1036
Daniel Jasperefece522015-04-03 16:19:48 +00001037 for (const auto &RP : BackTrace)
Daniel Jasper274928f2015-04-14 11:56:25 +00001038 if (static_cast<int>(RP[Class]) + RPIdAndCost.second >= Limit)
Evan Cheng44436302010-10-16 02:20:26 +00001039 return true;
Evan Cheng44436302010-10-16 02:20:26 +00001040 }
1041
1042 return false;
1043}
1044
Sanjay Patel87c6c072015-12-10 16:34:21 +00001045/// Traverse the back trace from header to the current block and update their
1046/// register pressures to reflect the effect of hoisting MI from the current
1047/// block to the preheader.
Evan Cheng87066f02010-10-20 22:03:58 +00001048void MachineLICM::UpdateBackTraceRegPressure(const MachineInstr *MI) {
Evan Cheng87066f02010-10-20 22:03:58 +00001049 // First compute the 'cost' of the instruction, i.e. its contribution
1050 // to register pressure.
Daniel Jaspere87e82b2015-04-07 16:42:35 +00001051 auto Cost = calcRegisterCost(MI, /*ConsiderSeen=*/false,
1052 /*ConsiderUnseenAsDef=*/false);
Evan Cheng87066f02010-10-20 22:03:58 +00001053
1054 // Update register pressure of blocks from loop header to current block.
Daniel Jaspere87e82b2015-04-07 16:42:35 +00001055 for (auto &RP : BackTrace)
Daniel Jasper274928f2015-04-14 11:56:25 +00001056 for (const auto &RPIdAndCost : Cost)
1057 RP[RPIdAndCost.first] += RPIdAndCost.second;
Evan Cheng87066f02010-10-20 22:03:58 +00001058}
1059
Sanjay Patel87c6c072015-12-10 16:34:21 +00001060/// Return true if it is potentially profitable to hoist the given loop
1061/// invariant.
Evan Cheng73f9a9e2009-11-20 23:31:34 +00001062bool MachineLICM::IsProfitableToHoist(MachineInstr &MI) {
Evan Chengd62719c2010-10-14 01:16:09 +00001063 if (MI.isImplicitDef())
1064 return true;
1065
Jakob Stoklund Olesen645bdd42012-04-11 00:00:28 +00001066 // Besides removing computation from the loop, hoisting an instruction has
1067 // these effects:
1068 //
1069 // - The value defined by the instruction becomes live across the entire
1070 // loop. This increases register pressure in the loop.
1071 //
1072 // - If the value is used by a PHI in the loop, a copy will be required for
1073 // lowering the PHI after extending the live range.
1074 //
1075 // - When hoisting the last use of a value in the loop, that value no longer
1076 // needs to be live in the loop. This lowers register pressure in the loop.
Evan Cheng90da66b2011-09-01 01:45:00 +00001077
Jakob Stoklund Olesen645bdd42012-04-11 00:00:28 +00001078 bool CheapInstr = IsCheapInstruction(MI);
1079 bool CreatesCopy = HasLoopPHIUse(&MI);
Evan Cheng44436302010-10-16 02:20:26 +00001080
Jakob Stoklund Olesen645bdd42012-04-11 00:00:28 +00001081 // Don't hoist a cheap instruction if it would create a copy in the loop.
1082 if (CheapInstr && CreatesCopy) {
1083 DEBUG(dbgs() << "Won't hoist cheap instr with loop PHI use: " << MI);
1084 return false;
Evan Chengb39a9fd2009-11-20 19:55:37 +00001085 }
Evan Cheng1d9f7ac2009-02-04 09:19:56 +00001086
Jakob Stoklund Olesen645bdd42012-04-11 00:00:28 +00001087 // Rematerializable instructions should always be hoisted since the register
1088 // allocator can just pull them down again when needed.
1089 if (TII->isTriviallyReMaterializable(&MI, AA))
1090 return true;
1091
Jakob Stoklund Olesen645bdd42012-04-11 00:00:28 +00001092 // FIXME: If there are long latency loop-invariant instructions inside the
1093 // loop at this point, why didn't the optimizer's LICM hoist them?
Jakob Stoklund Olesen645bdd42012-04-11 00:00:28 +00001094 for (unsigned i = 0, e = MI.getDesc().getNumOperands(); i != e; ++i) {
1095 const MachineOperand &MO = MI.getOperand(i);
1096 if (!MO.isReg() || MO.isImplicit())
1097 continue;
1098 unsigned Reg = MO.getReg();
1099 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1100 continue;
Daniel Jaspere87e82b2015-04-07 16:42:35 +00001101 if (MO.isDef() && HasHighOperandLatency(MI, i, Reg)) {
1102 DEBUG(dbgs() << "Hoist High Latency: " << MI);
1103 ++NumHighLatency;
1104 return true;
Jakob Stoklund Olesen645bdd42012-04-11 00:00:28 +00001105 }
1106 }
1107
Daniel Jaspere87e82b2015-04-07 16:42:35 +00001108 // Estimate register pressure to determine whether to LICM the instruction.
1109 // In low register pressure situation, we can be more aggressive about
1110 // hoisting. Also, favors hoisting long latency instructions even in
1111 // moderately high pressure situation.
1112 // Cheap instructions will only be hoisted if they don't increase register
1113 // pressure at all.
1114 auto Cost = calcRegisterCost(&MI, /*ConsiderSeen=*/false,
1115 /*ConsiderUnseenAsDef=*/false);
1116
Jakob Stoklund Olesen645bdd42012-04-11 00:00:28 +00001117 // Visit BBs from header to current BB, if hoisting this doesn't cause
1118 // high register pressure, then it's safe to proceed.
1119 if (!CanCauseHighRegPressure(Cost, CheapInstr)) {
1120 DEBUG(dbgs() << "Hoist non-reg-pressure: " << MI);
1121 ++NumLowRP;
1122 return true;
1123 }
1124
1125 // Don't risk increasing register pressure if it would create copies.
1126 if (CreatesCopy) {
1127 DEBUG(dbgs() << "Won't hoist instr with loop PHI use: " << MI);
Jakob Stoklund Olesena3e86a62012-04-11 00:00:26 +00001128 return false;
Jakob Stoklund Olesen645bdd42012-04-11 00:00:28 +00001129 }
1130
1131 // Do not "speculate" in high register pressure situation. If an
1132 // instruction is not guaranteed to be executed in the loop, it's best to be
1133 // conservative.
1134 if (AvoidSpeculation &&
1135 (!IsGuaranteedToExecute(MI.getParent()) && !MayCSE(&MI))) {
1136 DEBUG(dbgs() << "Won't speculate: " << MI);
1137 return false;
1138 }
1139
1140 // High register pressure situation, only hoist if the instruction is going
1141 // to be remat'ed.
1142 if (!TII->isTriviallyReMaterializable(&MI, AA) &&
1143 !MI.isInvariantLoad(AA)) {
1144 DEBUG(dbgs() << "Can't remat / high reg-pressure: " << MI);
1145 return false;
1146 }
Evan Cheng399660c2009-02-05 08:45:46 +00001147
1148 return true;
1149}
1150
Sanjay Patel87c6c072015-12-10 16:34:21 +00001151/// Unfold a load from the given machineinstr if the load itself could be
1152/// hoisted. Return the unfolded and hoistable load, or null if the load
1153/// couldn't be unfolded or if it wouldn't be hoistable.
Dan Gohman104f57c2009-10-29 17:47:20 +00001154MachineInstr *MachineLICM::ExtractHoistableLoad(MachineInstr *MI) {
Evan Cheng4ac0d162010-10-08 18:59:19 +00001155 // Don't unfold simple loads.
Evan Cheng7f8e5632011-12-07 07:15:52 +00001156 if (MI->canFoldAsLoad())
Craig Topperc0196b12014-04-14 00:51:57 +00001157 return nullptr;
Evan Cheng4ac0d162010-10-08 18:59:19 +00001158
Dan Gohman104f57c2009-10-29 17:47:20 +00001159 // If not, we may be able to unfold a load and hoist that.
1160 // First test whether the instruction is loading from an amenable
1161 // memory location.
Evan Chengb8b0ad82011-01-20 08:34:58 +00001162 if (!MI->isInvariantLoad(AA))
Craig Topperc0196b12014-04-14 00:51:57 +00001163 return nullptr;
Evan Chengb39a9fd2009-11-20 19:55:37 +00001164
Dan Gohman104f57c2009-10-29 17:47:20 +00001165 // Next determine the register class for a temporary register.
Dan Gohman49fa51d2009-10-30 22:18:41 +00001166 unsigned LoadRegIndex;
Dan Gohman104f57c2009-10-29 17:47:20 +00001167 unsigned NewOpc =
1168 TII->getOpcodeAfterMemoryUnfold(MI->getOpcode(),
1169 /*UnfoldLoad=*/true,
Dan Gohman49fa51d2009-10-30 22:18:41 +00001170 /*UnfoldStore=*/false,
1171 &LoadRegIndex);
Craig Topperc0196b12014-04-14 00:51:57 +00001172 if (NewOpc == 0) return nullptr;
Evan Cheng6cc775f2011-06-28 19:10:37 +00001173 const MCInstrDesc &MID = TII->get(NewOpc);
Craig Topperc0196b12014-04-14 00:51:57 +00001174 if (MID.getNumDefs() != 1) return nullptr;
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +00001175 MachineFunction &MF = *MI->getParent()->getParent();
1176 const TargetRegisterClass *RC = TII->getRegClass(MID, LoadRegIndex, TRI, MF);
Dan Gohman104f57c2009-10-29 17:47:20 +00001177 // Ok, we're unfolding. Create a temporary register and do the unfold.
Evan Chengd62719c2010-10-14 01:16:09 +00001178 unsigned Reg = MRI->createVirtualRegister(RC);
Evan Chengb39a9fd2009-11-20 19:55:37 +00001179
Dan Gohman104f57c2009-10-29 17:47:20 +00001180 SmallVector<MachineInstr *, 2> NewMIs;
1181 bool Success =
1182 TII->unfoldMemoryOperand(MF, MI, Reg,
1183 /*UnfoldLoad=*/true, /*UnfoldStore=*/false,
1184 NewMIs);
1185 (void)Success;
1186 assert(Success &&
1187 "unfoldMemoryOperand failed when getOpcodeAfterMemoryUnfold "
1188 "succeeded!");
1189 assert(NewMIs.size() == 2 &&
1190 "Unfolded a load into multiple instructions!");
1191 MachineBasicBlock *MBB = MI->getParent();
Evan Cheng2a81dd42011-12-06 22:12:01 +00001192 MachineBasicBlock::iterator Pos = MI;
1193 MBB->insert(Pos, NewMIs[0]);
1194 MBB->insert(Pos, NewMIs[1]);
Dan Gohman104f57c2009-10-29 17:47:20 +00001195 // If unfolding produced a load that wasn't loop-invariant or profitable to
1196 // hoist, discard the new instructions and bail.
Evan Cheng73f9a9e2009-11-20 23:31:34 +00001197 if (!IsLoopInvariantInst(*NewMIs[0]) || !IsProfitableToHoist(*NewMIs[0])) {
Dan Gohman104f57c2009-10-29 17:47:20 +00001198 NewMIs[0]->eraseFromParent();
1199 NewMIs[1]->eraseFromParent();
Craig Topperc0196b12014-04-14 00:51:57 +00001200 return nullptr;
Dan Gohman104f57c2009-10-29 17:47:20 +00001201 }
Evan Cheng87066f02010-10-20 22:03:58 +00001202
1203 // Update register pressure for the unfolded instruction.
1204 UpdateRegPressure(NewMIs[1]);
1205
Dan Gohman104f57c2009-10-29 17:47:20 +00001206 // Otherwise we successfully unfolded a load that we can hoist.
1207 MI->eraseFromParent();
1208 return NewMIs[0];
1209}
1210
Sanjay Patel87c6c072015-12-10 16:34:21 +00001211/// Initialize the CSE map with instructions that are in the current loop
1212/// preheader that may become duplicates of instructions that are hoisted
1213/// out of the loop.
Evan Chengf42b5af2009-11-03 21:40:02 +00001214void MachineLICM::InitCSEMap(MachineBasicBlock *BB) {
Sanjay Patel882a8ee2016-01-06 23:45:05 +00001215 for (MachineInstr &MI : *BB)
1216 CSEMap[MI.getOpcode()].push_back(&MI);
Evan Chengf42b5af2009-11-03 21:40:02 +00001217}
1218
Sanjay Patel87c6c072015-12-10 16:34:21 +00001219/// Find an instruction amount PrevMIs that is a duplicate of MI.
1220/// Return this instruction if it's found.
Evan Cheng7ff83192009-11-07 03:52:02 +00001221const MachineInstr*
1222MachineLICM::LookForDuplicate(const MachineInstr *MI,
1223 std::vector<const MachineInstr*> &PrevMIs) {
Sanjay Patel882a8ee2016-01-06 23:45:05 +00001224 for (const MachineInstr *PrevMI : PrevMIs)
Craig Topperc0196b12014-04-14 00:51:57 +00001225 if (TII->produceSameValue(MI, PrevMI, (PreRegAlloc ? MRI : nullptr)))
Evan Cheng921152f2009-11-05 00:51:13 +00001226 return PrevMI;
Sanjay Patel882a8ee2016-01-06 23:45:05 +00001227
Craig Topperc0196b12014-04-14 00:51:57 +00001228 return nullptr;
Evan Cheng921152f2009-11-05 00:51:13 +00001229}
1230
Sanjay Patel87c6c072015-12-10 16:34:21 +00001231/// Given a LICM'ed instruction, look for an instruction on the preheader that
1232/// computes the same value. If it's found, do a RAU on with the definition of
1233/// the existing instruction rather than hoisting the instruction to the
1234/// preheader.
Evan Cheng921152f2009-11-05 00:51:13 +00001235bool MachineLICM::EliminateCSE(MachineInstr *MI,
1236 DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator &CI) {
Evan Chengd5424142010-07-14 01:22:19 +00001237 // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate
1238 // the undef property onto uses.
1239 if (CI == CSEMap.end() || MI->isImplicitDef())
Evan Cheng7ff83192009-11-07 03:52:02 +00001240 return false;
1241
1242 if (const MachineInstr *Dup = LookForDuplicate(MI, CI->second)) {
David Greene55cf95c2010-01-05 00:03:48 +00001243 DEBUG(dbgs() << "CSEing " << *MI << " with " << *Dup);
Dan Gohman34021b72010-02-28 01:33:43 +00001244
1245 // Replace virtual registers defined by MI by their counterparts defined
1246 // by Dup.
Evan Chengaa563df2011-10-17 19:50:12 +00001247 SmallVector<unsigned, 2> Defs;
Evan Cheng7ff83192009-11-07 03:52:02 +00001248 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1249 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman34021b72010-02-28 01:33:43 +00001250
1251 // Physical registers may not differ here.
1252 assert((!MO.isReg() || MO.getReg() == 0 ||
1253 !TargetRegisterInfo::isPhysicalRegister(MO.getReg()) ||
1254 MO.getReg() == Dup->getOperand(i).getReg()) &&
1255 "Instructions with different phys regs are not identical!");
1256
1257 if (MO.isReg() && MO.isDef() &&
Evan Chengaa563df2011-10-17 19:50:12 +00001258 !TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
1259 Defs.push_back(i);
1260 }
1261
1262 SmallVector<const TargetRegisterClass*, 2> OrigRCs;
1263 for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
1264 unsigned Idx = Defs[i];
1265 unsigned Reg = MI->getOperand(Idx).getReg();
1266 unsigned DupReg = Dup->getOperand(Idx).getReg();
1267 OrigRCs.push_back(MRI->getRegClass(DupReg));
1268
1269 if (!MRI->constrainRegClass(DupReg, MRI->getRegClass(Reg))) {
1270 // Restore old RCs if more than one defs.
1271 for (unsigned j = 0; j != i; ++j)
1272 MRI->setRegClass(Dup->getOperand(Defs[j]).getReg(), OrigRCs[j]);
1273 return false;
Dan Gohmanc90f51c2010-05-13 20:34:42 +00001274 }
Evan Cheng921152f2009-11-05 00:51:13 +00001275 }
Evan Chengaa563df2011-10-17 19:50:12 +00001276
Sanjay Patel882a8ee2016-01-06 23:45:05 +00001277 for (unsigned Idx : Defs) {
Evan Chengaa563df2011-10-17 19:50:12 +00001278 unsigned Reg = MI->getOperand(Idx).getReg();
1279 unsigned DupReg = Dup->getOperand(Idx).getReg();
1280 MRI->replaceRegWith(Reg, DupReg);
1281 MRI->clearKillFlags(DupReg);
1282 }
1283
Evan Cheng7ff83192009-11-07 03:52:02 +00001284 MI->eraseFromParent();
1285 ++NumCSEed;
1286 return true;
Evan Cheng921152f2009-11-05 00:51:13 +00001287 }
1288 return false;
1289}
1290
Sanjay Patel87c6c072015-12-10 16:34:21 +00001291/// Return true if the given instruction will be CSE'd if it's hoisted out of
1292/// the loop.
Evan Chengaf138952011-10-12 00:09:14 +00001293bool MachineLICM::MayCSE(MachineInstr *MI) {
1294 unsigned Opcode = MI->getOpcode();
1295 DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator
1296 CI = CSEMap.find(Opcode);
1297 // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate
1298 // the undef property onto uses.
1299 if (CI == CSEMap.end() || MI->isImplicitDef())
1300 return false;
1301
Craig Topperc0196b12014-04-14 00:51:57 +00001302 return LookForDuplicate(MI, CI->second) != nullptr;
Evan Chengaf138952011-10-12 00:09:14 +00001303}
1304
Sanjay Patel87c6c072015-12-10 16:34:21 +00001305/// When an instruction is found to use only loop invariant operands
Bill Wendling70613b82008-05-12 19:38:32 +00001306/// that are safe to hoist, this instruction is called to do the dirty work.
Sanjay Patel87c6c072015-12-10 16:34:21 +00001307/// It returns true if the instruction is hoisted.
Evan Cheng87066f02010-10-20 22:03:58 +00001308bool MachineLICM::Hoist(MachineInstr *MI, MachineBasicBlock *Preheader) {
Dan Gohman1b44f102009-10-28 03:21:57 +00001309 // First check whether we should hoist this instruction.
Evan Cheng73f9a9e2009-11-20 23:31:34 +00001310 if (!IsLoopInvariantInst(*MI) || !IsProfitableToHoist(*MI)) {
Dan Gohman104f57c2009-10-29 17:47:20 +00001311 // If not, try unfolding a hoistable load.
1312 MI = ExtractHoistableLoad(MI);
Evan Cheng87066f02010-10-20 22:03:58 +00001313 if (!MI) return false;
Dan Gohman1b44f102009-10-28 03:21:57 +00001314 }
Bill Wendlingfb706bc2007-12-07 21:42:31 +00001315
Dan Gohman79618d12009-01-15 22:01:38 +00001316 // Now move the instructions to the predecessor, inserting it before any
1317 // terminator instructions.
1318 DEBUG({
David Greene55cf95c2010-01-05 00:03:48 +00001319 dbgs() << "Hoisting " << *MI;
Dan Gohman1b44f102009-10-28 03:21:57 +00001320 if (MI->getParent()->getBasicBlock())
Justin Lebarf6f4a2a2016-05-23 18:56:07 +00001321 dbgs() << " from BB#" << MI->getParent()->getNumber();
1322 if (Preheader->getBasicBlock())
1323 dbgs() << " to BB#" << Preheader->getNumber();
David Greene55cf95c2010-01-05 00:03:48 +00001324 dbgs() << "\n";
Dan Gohman79618d12009-01-15 22:01:38 +00001325 });
Bill Wendlingfb706bc2007-12-07 21:42:31 +00001326
Evan Chengf42b5af2009-11-03 21:40:02 +00001327 // If this is the first instruction being hoisted to the preheader,
1328 // initialize the CSE map with potential common expressions.
Evan Cheng032f3262010-05-29 00:06:36 +00001329 if (FirstInLoop) {
Dan Gohman3570f812010-06-22 17:25:57 +00001330 InitCSEMap(Preheader);
Evan Cheng032f3262010-05-29 00:06:36 +00001331 FirstInLoop = false;
1332 }
Evan Chengf42b5af2009-11-03 21:40:02 +00001333
Evan Cheng399660c2009-02-05 08:45:46 +00001334 // Look for opportunity to CSE the hoisted instruction.
Evan Chengf42b5af2009-11-03 21:40:02 +00001335 unsigned Opcode = MI->getOpcode();
1336 DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator
1337 CI = CSEMap.find(Opcode);
Evan Cheng921152f2009-11-05 00:51:13 +00001338 if (!EliminateCSE(MI, CI)) {
1339 // Otherwise, splice the instruction to the preheader.
Dan Gohman3570f812010-06-22 17:25:57 +00001340 Preheader->splice(Preheader->getFirstTerminator(),MI->getParent(),MI);
Evan Chengf42b5af2009-11-03 21:40:02 +00001341
Evan Cheng87066f02010-10-20 22:03:58 +00001342 // Update register pressure for BBs from header to this block.
1343 UpdateBackTraceRegPressure(MI);
1344
Dan Gohmanc90f51c2010-05-13 20:34:42 +00001345 // Clear the kill flags of any register this instruction defines,
1346 // since they may need to be live throughout the entire loop
1347 // rather than just live for part of it.
Sanjay Patel882a8ee2016-01-06 23:45:05 +00001348 for (MachineOperand &MO : MI->operands())
Dan Gohmanc90f51c2010-05-13 20:34:42 +00001349 if (MO.isReg() && MO.isDef() && !MO.isDead())
Evan Chengd62719c2010-10-14 01:16:09 +00001350 MRI->clearKillFlags(MO.getReg());
Dan Gohmanc90f51c2010-05-13 20:34:42 +00001351
Evan Cheng399660c2009-02-05 08:45:46 +00001352 // Add to the CSE map.
1353 if (CI != CSEMap.end())
Dan Gohman1b44f102009-10-28 03:21:57 +00001354 CI->second.push_back(MI);
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00001355 else
1356 CSEMap[Opcode].push_back(MI);
Evan Cheng399660c2009-02-05 08:45:46 +00001357 }
Bill Wendlingfb706bc2007-12-07 21:42:31 +00001358
Dan Gohman79618d12009-01-15 22:01:38 +00001359 ++NumHoisted;
Bill Wendlingfb706bc2007-12-07 21:42:31 +00001360 Changed = true;
Evan Cheng87066f02010-10-20 22:03:58 +00001361
1362 return true;
Bill Wendlingfb706bc2007-12-07 21:42:31 +00001363}
Dan Gohman3570f812010-06-22 17:25:57 +00001364
Sanjay Patel87c6c072015-12-10 16:34:21 +00001365/// Get the preheader for the current loop, splitting a critical edge if needed.
Dan Gohman3570f812010-06-22 17:25:57 +00001366MachineBasicBlock *MachineLICM::getCurPreheader() {
1367 // Determine the block to which to hoist instructions. If we can't find a
1368 // suitable loop predecessor, we can't do any hoisting.
1369
1370 // If we've tried to get a preheader and failed, don't try again.
1371 if (CurPreheader == reinterpret_cast<MachineBasicBlock *>(-1))
Craig Topperc0196b12014-04-14 00:51:57 +00001372 return nullptr;
Dan Gohman3570f812010-06-22 17:25:57 +00001373
1374 if (!CurPreheader) {
1375 CurPreheader = CurLoop->getLoopPreheader();
1376 if (!CurPreheader) {
1377 MachineBasicBlock *Pred = CurLoop->getLoopPredecessor();
1378 if (!Pred) {
1379 CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1);
Craig Topperc0196b12014-04-14 00:51:57 +00001380 return nullptr;
Dan Gohman3570f812010-06-22 17:25:57 +00001381 }
1382
Quentin Colombet23341a82016-04-21 21:01:13 +00001383 CurPreheader = Pred->SplitCriticalEdge(CurLoop->getHeader(), *this);
Dan Gohman3570f812010-06-22 17:25:57 +00001384 if (!CurPreheader) {
1385 CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1);
Craig Topperc0196b12014-04-14 00:51:57 +00001386 return nullptr;
Dan Gohman3570f812010-06-22 17:25:57 +00001387 }
1388 }
1389 }
1390 return CurPreheader;
1391}