blob: 829fae6edbdf08a0d70beabd6df7e8b8af941287 [file] [log] [blame]
Bill Wendling0f940c92007-12-07 21:42:31 +00001//===-- MachineLICM.cpp - Machine Loop Invariant Code Motion Pass ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Wendling0f940c92007-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 Gohmanc475c362009-01-15 22:01:38 +000013// This pass does not attempt to throttle itself to limit register pressure.
14// The register allocation phases are expected to perform rematerialization
15// to recover when register pressure is high.
16//
17// This pass is not intended to be a replacement or a complete alternative
18// for the LLVM-IR-level LICM pass. It is only designed to hoist simple
19// constructs that are not exposed before lowering and instruction selection.
20//
Bill Wendling0f940c92007-12-07 21:42:31 +000021//===----------------------------------------------------------------------===//
22
23#define DEBUG_TYPE "machine-licm"
Chris Lattnerac695822008-01-04 06:41:45 +000024#include "llvm/CodeGen/Passes.h"
Bill Wendling0f940c92007-12-07 21:42:31 +000025#include "llvm/CodeGen/MachineDominators.h"
Evan Chengd94671a2010-04-07 00:41:17 +000026#include "llvm/CodeGen/MachineFrameInfo.h"
Bill Wendling0f940c92007-12-07 21:42:31 +000027#include "llvm/CodeGen/MachineLoopInfo.h"
Dan Gohman589f1f52009-10-28 03:21:57 +000028#include "llvm/CodeGen/MachineMemOperand.h"
Bill Wendling9258cd32008-01-02 19:32:43 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman589f1f52009-10-28 03:21:57 +000030#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Cheng0e673912010-10-14 01:16:09 +000031#include "llvm/Target/TargetLowering.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000032#include "llvm/Target/TargetRegisterInfo.h"
Bill Wendlingefe2be72007-12-11 23:27:51 +000033#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng0e673912010-10-14 01:16:09 +000034#include "llvm/Target/TargetInstrItineraries.h"
Bill Wendling0f940c92007-12-07 21:42:31 +000035#include "llvm/Target/TargetMachine.h"
Dan Gohmane33f44c2009-10-07 17:38:06 +000036#include "llvm/Analysis/AliasAnalysis.h"
Evan Chengaf6949d2009-02-05 08:45:46 +000037#include "llvm/ADT/DenseMap.h"
Evan Chengd94671a2010-04-07 00:41:17 +000038#include "llvm/ADT/SmallSet.h"
Chris Lattnerac695822008-01-04 06:41:45 +000039#include "llvm/ADT/Statistic.h"
Evan Cheng0e673912010-10-14 01:16:09 +000040#include "llvm/Support/CommandLine.h"
Chris Lattnerac695822008-01-04 06:41:45 +000041#include "llvm/Support/Debug.h"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000042#include "llvm/Support/raw_ostream.h"
Bill Wendling0f940c92007-12-07 21:42:31 +000043
44using namespace llvm;
45
Evan Cheng03a9fdf2010-10-16 02:20:26 +000046STATISTIC(NumHoisted,
47 "Number of machine instructions hoisted out of loops");
48STATISTIC(NumLowRP,
49 "Number of instructions hoisted in low reg pressure situation");
50STATISTIC(NumHighLatency,
51 "Number of high latency instructions hoisted");
52STATISTIC(NumCSEed,
53 "Number of hoisted machine instructions CSEed");
Evan Chengd94671a2010-04-07 00:41:17 +000054STATISTIC(NumPostRAHoisted,
55 "Number of machine instructions hoisted out of loops post regalloc");
Bill Wendlingb48519c2007-12-08 01:47:01 +000056
Bill Wendling0f940c92007-12-07 21:42:31 +000057namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000058 class MachineLICM : public MachineFunctionPass {
Evan Chengd94671a2010-04-07 00:41:17 +000059 bool PreRegAlloc;
60
Bill Wendling9258cd32008-01-02 19:32:43 +000061 const TargetMachine *TM;
Bill Wendlingefe2be72007-12-11 23:27:51 +000062 const TargetInstrInfo *TII;
Evan Cheng0e673912010-10-14 01:16:09 +000063 const TargetLowering *TLI;
Dan Gohmana8fb3362009-09-25 23:58:45 +000064 const TargetRegisterInfo *TRI;
Evan Chengd94671a2010-04-07 00:41:17 +000065 const MachineFrameInfo *MFI;
Evan Cheng0e673912010-10-14 01:16:09 +000066 MachineRegisterInfo *MRI;
67 const InstrItineraryData *InstrItins;
Bill Wendling12ebf142007-12-11 19:40:06 +000068
Bill Wendling0f940c92007-12-07 21:42:31 +000069 // Various analyses that we use...
Dan Gohmane33f44c2009-10-07 17:38:06 +000070 AliasAnalysis *AA; // Alias analysis info.
Evan Cheng4038f9c2010-04-08 01:03:47 +000071 MachineLoopInfo *MLI; // Current MachineLoopInfo
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +000072 MachineDominatorTree *DT; // Machine dominator tree for the cur loop
Bill Wendling0f940c92007-12-07 21:42:31 +000073
Bill Wendling0f940c92007-12-07 21:42:31 +000074 // State that is updated as we process loops
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +000075 bool Changed; // True if a loop is changed.
Evan Cheng82e0a1a2010-05-29 00:06:36 +000076 bool FirstInLoop; // True if it's the first LICM in the loop.
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +000077 MachineLoop *CurLoop; // The current loop we are working on.
Dan Gohmanc475c362009-01-15 22:01:38 +000078 MachineBasicBlock *CurPreheader; // The preheader for CurLoop.
Evan Chengaf6949d2009-02-05 08:45:46 +000079
Evan Chengd94671a2010-04-07 00:41:17 +000080 BitVector AllocatableSet;
81
Evan Cheng0e673912010-10-14 01:16:09 +000082 // Track 'estimated' register pressure.
Evan Cheng03a9fdf2010-10-16 02:20:26 +000083 SmallSet<unsigned, 32> RegSeen;
Evan Cheng0e673912010-10-14 01:16:09 +000084 SmallVector<unsigned, 8> RegPressure;
Evan Cheng03a9fdf2010-10-16 02:20:26 +000085
86 // Register pressure "limit" per register class. If the pressure
87 // is higher than the limit, then it's considered high.
Evan Cheng0e673912010-10-14 01:16:09 +000088 SmallVector<unsigned, 8> RegLimit;
89
Evan Cheng03a9fdf2010-10-16 02:20:26 +000090 // Register pressure on path leading from loop preheader to current BB.
91 SmallVector<SmallVector<unsigned, 8>, 16> BackTrace;
92
Dale Johannesenc46a5f22010-07-29 17:45:24 +000093 // For each opcode, keep a list of potential CSE instructions.
Evan Cheng777c6b72009-11-03 21:40:02 +000094 DenseMap<unsigned, std::vector<const MachineInstr*> > CSEMap;
Evan Chengd94671a2010-04-07 00:41:17 +000095
Bill Wendling0f940c92007-12-07 21:42:31 +000096 public:
97 static char ID; // Pass identification, replacement for typeid
Evan Chengd94671a2010-04-07 00:41:17 +000098 MachineLICM() :
Owen Anderson90c579d2010-08-06 18:33:48 +000099 MachineFunctionPass(ID), PreRegAlloc(true) {}
Evan Chengd94671a2010-04-07 00:41:17 +0000100
101 explicit MachineLICM(bool PreRA) :
Owen Anderson90c579d2010-08-06 18:33:48 +0000102 MachineFunctionPass(ID), PreRegAlloc(PreRA) {}
Bill Wendling0f940c92007-12-07 21:42:31 +0000103
104 virtual bool runOnMachineFunction(MachineFunction &MF);
105
Dan Gohman72241702008-12-18 01:37:56 +0000106 const char *getPassName() const { return "Machine Instruction LICM"; }
107
Bill Wendling0f940c92007-12-07 21:42:31 +0000108 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
109 AU.setPreservesCFG();
110 AU.addRequired<MachineLoopInfo>();
111 AU.addRequired<MachineDominatorTree>();
Dan Gohmane33f44c2009-10-07 17:38:06 +0000112 AU.addRequired<AliasAnalysis>();
Bill Wendlingd5da7042008-01-04 08:48:49 +0000113 AU.addPreserved<MachineLoopInfo>();
114 AU.addPreserved<MachineDominatorTree>();
115 MachineFunctionPass::getAnalysisUsage(AU);
Bill Wendling0f940c92007-12-07 21:42:31 +0000116 }
Evan Chengaf6949d2009-02-05 08:45:46 +0000117
118 virtual void releaseMemory() {
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000119 RegSeen.clear();
Evan Cheng0e673912010-10-14 01:16:09 +0000120 RegPressure.clear();
121 RegLimit.clear();
Evan Cheng11e8b742010-10-19 00:55:07 +0000122 BackTrace.clear();
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000123 for (DenseMap<unsigned,std::vector<const MachineInstr*> >::iterator
124 CI = CSEMap.begin(), CE = CSEMap.end(); CI != CE; ++CI)
125 CI->second.clear();
Evan Chengaf6949d2009-02-05 08:45:46 +0000126 CSEMap.clear();
127 }
128
Bill Wendling0f940c92007-12-07 21:42:31 +0000129 private:
Evan Cheng4038f9c2010-04-08 01:03:47 +0000130 /// CandidateInfo - Keep track of information about hoisting candidates.
131 struct CandidateInfo {
132 MachineInstr *MI;
Evan Cheng4038f9c2010-04-08 01:03:47 +0000133 unsigned Def;
Evan Cheng5dc57ce2010-04-13 18:16:00 +0000134 int FI;
135 CandidateInfo(MachineInstr *mi, unsigned def, int fi)
136 : MI(mi), Def(def), FI(fi) {}
Evan Cheng4038f9c2010-04-08 01:03:47 +0000137 };
138
139 /// HoistRegionPostRA - Walk the specified region of the CFG and hoist loop
140 /// invariants out to the preheader.
Evan Cheng94d1d9c2010-04-17 07:07:11 +0000141 void HoistRegionPostRA();
Evan Cheng4038f9c2010-04-08 01:03:47 +0000142
143 /// HoistPostRA - When an instruction is found to only use loop invariant
144 /// operands that is safe to hoist, this instruction is called to do the
145 /// dirty work.
146 void HoistPostRA(MachineInstr *MI, unsigned Def);
147
148 /// ProcessMI - Examine the instruction for potentai LICM candidate. Also
149 /// gather register def and frame object update information.
150 void ProcessMI(MachineInstr *MI, unsigned *PhysRegDefs,
151 SmallSet<int, 32> &StoredFIs,
152 SmallVector<CandidateInfo, 32> &Candidates);
153
Evan Cheng94d1d9c2010-04-17 07:07:11 +0000154 /// AddToLiveIns - Add register 'Reg' to the livein sets of BBs in the
155 /// current loop.
156 void AddToLiveIns(unsigned Reg);
Evan Cheng4038f9c2010-04-08 01:03:47 +0000157
Evan Cheng5dc57ce2010-04-13 18:16:00 +0000158 /// IsLICMCandidate - Returns true if the instruction may be a suitable
Chris Lattner77910802010-07-12 00:00:35 +0000159 /// candidate for LICM. e.g. If the instruction is a call, then it's
160 /// obviously not safe to hoist it.
Evan Cheng5dc57ce2010-04-13 18:16:00 +0000161 bool IsLICMCandidate(MachineInstr &I);
162
Bill Wendling041b3f82007-12-08 23:58:46 +0000163 /// IsLoopInvariantInst - Returns true if the instruction is loop
Bill Wendling0f940c92007-12-07 21:42:31 +0000164 /// invariant. I.e., all virtual register operands are defined outside of
165 /// the loop, physical registers aren't accessed (explicitly or implicitly),
166 /// and the instruction is hoistable.
167 ///
Bill Wendling041b3f82007-12-08 23:58:46 +0000168 bool IsLoopInvariantInst(MachineInstr &I);
Bill Wendling0f940c92007-12-07 21:42:31 +0000169
Evan Cheng11e8b742010-10-19 00:55:07 +0000170 /// HasHighOperandLatency - Compute operand latency between a def of 'Reg'
171 /// and an use in the current loop, return true if the target considered
172 /// it 'high'.
173 bool HasHighOperandLatency(MachineInstr &MI, unsigned DefIdx, unsigned Reg);
Evan Cheng0e673912010-10-14 01:16:09 +0000174
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000175 /// IncreaseHighRegPressure - Visit BBs from preheader to current BB, check
176 /// if hoisting an instruction of the given cost matrix can cause high
177 /// register pressure.
178 bool IncreaseHighRegPressure(DenseMap<unsigned, int> &Cost);
179
Evan Cheng45e94d62009-02-04 09:19:56 +0000180 /// IsProfitableToHoist - Return true if it is potentially profitable to
181 /// hoist the given loop invariant.
Evan Chengc26abd92009-11-20 23:31:34 +0000182 bool IsProfitableToHoist(MachineInstr &MI);
Evan Cheng45e94d62009-02-04 09:19:56 +0000183
Bill Wendling0f940c92007-12-07 21:42:31 +0000184 /// HoistRegion - Walk the specified region of the CFG (defined by all
185 /// blocks dominated by the specified block, and that are in the current
186 /// loop) in depth first order w.r.t the DominatorTree. This allows us to
187 /// visit definitions before uses, allowing us to hoist a loop body in one
188 /// pass without iteration.
189 ///
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000190 void HoistRegion(MachineDomTreeNode *N, bool IsHeader = false);
Bill Wendling0f940c92007-12-07 21:42:31 +0000191
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000192 /// InitRegPressure - Find all virtual register references that are liveout
193 /// of the preheader to initialize the starting "register pressure". Note
194 /// this does not count live through (livein but not used) registers.
Evan Cheng0e673912010-10-14 01:16:09 +0000195 void InitRegPressure(MachineBasicBlock *BB);
196
197 /// UpdateRegPressureBefore / UpdateRegPressureAfter - Update estimate of
198 /// register pressure before and after executing a specifi instruction.
199 void UpdateRegPressureBefore(const MachineInstr *MI);
200 void UpdateRegPressureAfter(const MachineInstr *MI);
201
Evan Cheng87b75ba2009-11-20 19:55:37 +0000202 /// isLoadFromConstantMemory - Return true if the given instruction is a
203 /// load from constant memory.
204 bool isLoadFromConstantMemory(MachineInstr *MI);
205
Dan Gohman5c952302009-10-29 17:47:20 +0000206 /// ExtractHoistableLoad - Unfold a load from the given machineinstr if
207 /// the load itself could be hoisted. Return the unfolded and hoistable
208 /// load, or null if the load couldn't be unfolded or if it wouldn't
209 /// be hoistable.
210 MachineInstr *ExtractHoistableLoad(MachineInstr *MI);
211
Evan Cheng78e5c112009-11-07 03:52:02 +0000212 /// LookForDuplicate - Find an instruction amount PrevMIs that is a
213 /// duplicate of MI. Return this instruction if it's found.
214 const MachineInstr *LookForDuplicate(const MachineInstr *MI,
215 std::vector<const MachineInstr*> &PrevMIs);
216
Evan Cheng9fb744e2009-11-05 00:51:13 +0000217 /// EliminateCSE - Given a LICM'ed instruction, look for an instruction on
218 /// the preheader that compute the same value. If it's found, do a RAU on
219 /// with the definition of the existing instruction rather than hoisting
220 /// the instruction to the preheader.
221 bool EliminateCSE(MachineInstr *MI,
222 DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator &CI);
223
Bill Wendling0f940c92007-12-07 21:42:31 +0000224 /// Hoist - When an instruction is found to only use loop invariant operands
225 /// that is safe to hoist, this instruction is called to do the dirty work.
226 ///
Evan Cheng0e673912010-10-14 01:16:09 +0000227 void Hoist(MachineInstr *MI, MachineBasicBlock *Preheader);
Evan Cheng777c6b72009-11-03 21:40:02 +0000228
229 /// InitCSEMap - Initialize the CSE map with instructions that are in the
230 /// current loop preheader that may become duplicates of instructions that
231 /// are hoisted out of the loop.
232 void InitCSEMap(MachineBasicBlock *BB);
Dan Gohman853d3fb2010-06-22 17:25:57 +0000233
234 /// getCurPreheader - Get the preheader for the current loop, splitting
235 /// a critical edge if needed.
236 MachineBasicBlock *getCurPreheader();
Bill Wendling0f940c92007-12-07 21:42:31 +0000237 };
Bill Wendling0f940c92007-12-07 21:42:31 +0000238} // end anonymous namespace
239
Dan Gohman844731a2008-05-13 00:00:25 +0000240char MachineLICM::ID = 0;
Owen Anderson2ab36d32010-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)
245INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
246INITIALIZE_PASS_END(MachineLICM, "machinelicm",
Owen Andersonce665bd2010-10-07 22:25:06 +0000247 "Machine Loop Invariant Code Motion", false, false)
Dan Gohman844731a2008-05-13 00:00:25 +0000248
Evan Chengd94671a2010-04-07 00:41:17 +0000249FunctionPass *llvm::createMachineLICMPass(bool PreRegAlloc) {
250 return new MachineLICM(PreRegAlloc);
251}
Bill Wendling0f940c92007-12-07 21:42:31 +0000252
Dan Gohman853d3fb2010-06-22 17:25:57 +0000253/// LoopIsOuterMostWithPredecessor - Test if the given loop is the outer-most
254/// loop that has a unique predecessor.
255static bool LoopIsOuterMostWithPredecessor(MachineLoop *CurLoop) {
Dan Gohmanaa742602010-07-09 18:49:45 +0000256 // Check whether this loop even has a unique predecessor.
257 if (!CurLoop->getLoopPredecessor())
258 return false;
259 // Ok, now check to see if any of its outer loops do.
Dan Gohmanc475c362009-01-15 22:01:38 +0000260 for (MachineLoop *L = CurLoop->getParentLoop(); L; L = L->getParentLoop())
Dan Gohman853d3fb2010-06-22 17:25:57 +0000261 if (L->getLoopPredecessor())
Dan Gohmanc475c362009-01-15 22:01:38 +0000262 return false;
Dan Gohmanaa742602010-07-09 18:49:45 +0000263 // None of them did, so this is the outermost with a unique predecessor.
Dan Gohmanc475c362009-01-15 22:01:38 +0000264 return true;
265}
266
Bill Wendling0f940c92007-12-07 21:42:31 +0000267bool MachineLICM::runOnMachineFunction(MachineFunction &MF) {
Evan Chengd94671a2010-04-07 00:41:17 +0000268 if (PreRegAlloc)
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000269 DEBUG(dbgs() << "******** Pre-regalloc Machine LICM: ");
Evan Chengd94671a2010-04-07 00:41:17 +0000270 else
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000271 DEBUG(dbgs() << "******** Post-regalloc Machine LICM: ");
272 DEBUG(dbgs() << MF.getFunction()->getName() << " ********\n");
Bill Wendlinga17ad592007-12-11 22:22:22 +0000273
Evan Cheng82e0a1a2010-05-29 00:06:36 +0000274 Changed = FirstInLoop = false;
Bill Wendlingacb04ec2008-08-31 02:30:23 +0000275 TM = &MF.getTarget();
Bill Wendling9258cd32008-01-02 19:32:43 +0000276 TII = TM->getInstrInfo();
Evan Cheng0e673912010-10-14 01:16:09 +0000277 TLI = TM->getTargetLowering();
Dan Gohmana8fb3362009-09-25 23:58:45 +0000278 TRI = TM->getRegisterInfo();
Evan Chengd94671a2010-04-07 00:41:17 +0000279 MFI = MF.getFrameInfo();
Evan Cheng0e673912010-10-14 01:16:09 +0000280 MRI = &MF.getRegInfo();
281 InstrItins = TM->getInstrItineraryData();
Dan Gohman45094e32009-09-26 02:34:00 +0000282 AllocatableSet = TRI->getAllocatableSet(MF);
Bill Wendling0f940c92007-12-07 21:42:31 +0000283
Evan Cheng0e673912010-10-14 01:16:09 +0000284 if (PreRegAlloc) {
285 // Estimate register pressure during pre-regalloc pass.
286 unsigned NumRC = TRI->getNumRegClasses();
287 RegPressure.resize(NumRC);
Evan Cheng0e673912010-10-14 01:16:09 +0000288 std::fill(RegPressure.begin(), RegPressure.end(), 0);
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000289 RegLimit.resize(NumRC);
Evan Cheng0e673912010-10-14 01:16:09 +0000290 for (TargetRegisterInfo::regclass_iterator I = TRI->regclass_begin(),
291 E = TRI->regclass_end(); I != E; ++I)
292 RegLimit[(*I)->getID()] = TLI->getRegPressureLimit(*I, MF);
293 }
294
Bill Wendling0f940c92007-12-07 21:42:31 +0000295 // Get our Loop information...
Evan Cheng4038f9c2010-04-08 01:03:47 +0000296 MLI = &getAnalysis<MachineLoopInfo>();
297 DT = &getAnalysis<MachineDominatorTree>();
298 AA = &getAnalysis<AliasAnalysis>();
Bill Wendling0f940c92007-12-07 21:42:31 +0000299
Dan Gohmanaa742602010-07-09 18:49:45 +0000300 SmallVector<MachineLoop *, 8> Worklist(MLI->begin(), MLI->end());
301 while (!Worklist.empty()) {
302 CurLoop = Worklist.pop_back_val();
Dan Gohman853d3fb2010-06-22 17:25:57 +0000303 CurPreheader = 0;
Bill Wendling0f940c92007-12-07 21:42:31 +0000304
Evan Cheng4038f9c2010-04-08 01:03:47 +0000305 // If this is done before regalloc, only visit outer-most preheader-sporting
306 // loops.
Dan Gohmanaa742602010-07-09 18:49:45 +0000307 if (PreRegAlloc && !LoopIsOuterMostWithPredecessor(CurLoop)) {
308 Worklist.append(CurLoop->begin(), CurLoop->end());
Dan Gohmanc475c362009-01-15 22:01:38 +0000309 continue;
Dan Gohmanaa742602010-07-09 18:49:45 +0000310 }
Dan Gohmanc475c362009-01-15 22:01:38 +0000311
Evan Chengd94671a2010-04-07 00:41:17 +0000312 if (!PreRegAlloc)
Evan Cheng94d1d9c2010-04-17 07:07:11 +0000313 HoistRegionPostRA();
Evan Chengd94671a2010-04-07 00:41:17 +0000314 else {
Evan Cheng94d1d9c2010-04-17 07:07:11 +0000315 // CSEMap is initialized for loop header when the first instruction is
316 // being hoisted.
317 MachineDomTreeNode *N = DT->getNode(CurLoop->getHeader());
Evan Cheng82e0a1a2010-05-29 00:06:36 +0000318 FirstInLoop = true;
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000319 HoistRegion(N, true);
Evan Chengd94671a2010-04-07 00:41:17 +0000320 CSEMap.clear();
321 }
Bill Wendling0f940c92007-12-07 21:42:31 +0000322 }
323
324 return Changed;
325}
326
Evan Cheng4038f9c2010-04-08 01:03:47 +0000327/// InstructionStoresToFI - Return true if instruction stores to the
328/// specified frame.
329static bool InstructionStoresToFI(const MachineInstr *MI, int FI) {
330 for (MachineInstr::mmo_iterator o = MI->memoperands_begin(),
331 oe = MI->memoperands_end(); o != oe; ++o) {
332 if (!(*o)->isStore() || !(*o)->getValue())
333 continue;
334 if (const FixedStackPseudoSourceValue *Value =
335 dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) {
336 if (Value->getFrameIndex() == FI)
337 return true;
338 }
339 }
340 return false;
341}
342
343/// ProcessMI - Examine the instruction for potentai LICM candidate. Also
344/// gather register def and frame object update information.
345void MachineLICM::ProcessMI(MachineInstr *MI,
346 unsigned *PhysRegDefs,
347 SmallSet<int, 32> &StoredFIs,
348 SmallVector<CandidateInfo, 32> &Candidates) {
349 bool RuledOut = false;
Evan Chengaeb2f4a2010-04-13 20:21:05 +0000350 bool HasNonInvariantUse = false;
Evan Cheng4038f9c2010-04-08 01:03:47 +0000351 unsigned Def = 0;
352 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
353 const MachineOperand &MO = MI->getOperand(i);
354 if (MO.isFI()) {
355 // Remember if the instruction stores to the frame index.
356 int FI = MO.getIndex();
357 if (!StoredFIs.count(FI) &&
358 MFI->isSpillSlotObjectIndex(FI) &&
359 InstructionStoresToFI(MI, FI))
360 StoredFIs.insert(FI);
Evan Chengaeb2f4a2010-04-13 20:21:05 +0000361 HasNonInvariantUse = true;
Evan Cheng4038f9c2010-04-08 01:03:47 +0000362 continue;
363 }
364
365 if (!MO.isReg())
366 continue;
367 unsigned Reg = MO.getReg();
368 if (!Reg)
369 continue;
370 assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
371 "Not expecting virtual register!");
372
Evan Cheng5dc57ce2010-04-13 18:16:00 +0000373 if (!MO.isDef()) {
Evan Cheng63275372010-04-13 22:13:34 +0000374 if (Reg && PhysRegDefs[Reg])
Evan Chengaeb2f4a2010-04-13 20:21:05 +0000375 // If it's using a non-loop-invariant register, then it's obviously not
376 // safe to hoist.
377 HasNonInvariantUse = true;
Evan Cheng4038f9c2010-04-08 01:03:47 +0000378 continue;
Evan Cheng5dc57ce2010-04-13 18:16:00 +0000379 }
Evan Cheng4038f9c2010-04-08 01:03:47 +0000380
381 if (MO.isImplicit()) {
382 ++PhysRegDefs[Reg];
383 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS)
384 ++PhysRegDefs[*AS];
385 if (!MO.isDead())
386 // Non-dead implicit def? This cannot be hoisted.
387 RuledOut = true;
388 // No need to check if a dead implicit def is also defined by
389 // another instruction.
390 continue;
391 }
392
393 // FIXME: For now, avoid instructions with multiple defs, unless
394 // it's a dead implicit def.
395 if (Def)
396 RuledOut = true;
397 else
398 Def = Reg;
399
400 // If we have already seen another instruction that defines the same
401 // register, then this is not safe.
402 if (++PhysRegDefs[Reg] > 1)
403 // MI defined register is seen defined by another instruction in
404 // the loop, it cannot be a LICM candidate.
405 RuledOut = true;
406 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS)
407 if (++PhysRegDefs[*AS] > 1)
408 RuledOut = true;
409 }
410
Evan Cheng5dc57ce2010-04-13 18:16:00 +0000411 // Only consider reloads for now and remats which do not have register
412 // operands. FIXME: Consider unfold load folding instructions.
Evan Cheng4038f9c2010-04-08 01:03:47 +0000413 if (Def && !RuledOut) {
Evan Cheng5dc57ce2010-04-13 18:16:00 +0000414 int FI = INT_MIN;
Evan Chengaeb2f4a2010-04-13 20:21:05 +0000415 if ((!HasNonInvariantUse && IsLICMCandidate(*MI)) ||
Evan Cheng5dc57ce2010-04-13 18:16:00 +0000416 (TII->isLoadFromStackSlot(MI, FI) && MFI->isSpillSlotObjectIndex(FI)))
417 Candidates.push_back(CandidateInfo(MI, Def, FI));
Evan Cheng4038f9c2010-04-08 01:03:47 +0000418 }
419}
420
421/// HoistRegionPostRA - Walk the specified region of the CFG and hoist loop
422/// invariants out to the preheader.
Evan Cheng94d1d9c2010-04-17 07:07:11 +0000423void MachineLICM::HoistRegionPostRA() {
Evan Chengd94671a2010-04-07 00:41:17 +0000424 unsigned NumRegs = TRI->getNumRegs();
425 unsigned *PhysRegDefs = new unsigned[NumRegs];
426 std::fill(PhysRegDefs, PhysRegDefs + NumRegs, 0);
427
Evan Cheng4038f9c2010-04-08 01:03:47 +0000428 SmallVector<CandidateInfo, 32> Candidates;
Evan Chengd94671a2010-04-07 00:41:17 +0000429 SmallSet<int, 32> StoredFIs;
430
431 // Walk the entire region, count number of defs for each register, and
Evan Cheng94d1d9c2010-04-17 07:07:11 +0000432 // collect potential LICM candidates.
433 const std::vector<MachineBasicBlock*> Blocks = CurLoop->getBlocks();
434 for (unsigned i = 0, e = Blocks.size(); i != e; ++i) {
435 MachineBasicBlock *BB = Blocks[i];
Evan Chengd94671a2010-04-07 00:41:17 +0000436 // Conservatively treat live-in's as an external def.
Evan Cheng4038f9c2010-04-08 01:03:47 +0000437 // FIXME: That means a reload that're reused in successor block(s) will not
438 // be LICM'ed.
Dan Gohman81bf03e2010-04-13 16:57:55 +0000439 for (MachineBasicBlock::livein_iterator I = BB->livein_begin(),
Evan Chengd94671a2010-04-07 00:41:17 +0000440 E = BB->livein_end(); I != E; ++I) {
441 unsigned Reg = *I;
442 ++PhysRegDefs[Reg];
Evan Cheng4038f9c2010-04-08 01:03:47 +0000443 for (const unsigned *AS = TRI->getAliasSet(Reg); *AS; ++AS)
444 ++PhysRegDefs[*AS];
Evan Chengd94671a2010-04-07 00:41:17 +0000445 }
446
447 for (MachineBasicBlock::iterator
448 MII = BB->begin(), E = BB->end(); MII != E; ++MII) {
Evan Chengd94671a2010-04-07 00:41:17 +0000449 MachineInstr *MI = &*MII;
Evan Cheng4038f9c2010-04-08 01:03:47 +0000450 ProcessMI(MI, PhysRegDefs, StoredFIs, Candidates);
Evan Chengd94671a2010-04-07 00:41:17 +0000451 }
Evan Cheng94d1d9c2010-04-17 07:07:11 +0000452 }
Evan Chengd94671a2010-04-07 00:41:17 +0000453
454 // Now evaluate whether the potential candidates qualify.
455 // 1. Check if the candidate defined register is defined by another
456 // instruction in the loop.
457 // 2. If the candidate is a load from stack slot (always true for now),
458 // check if the slot is stored anywhere in the loop.
459 for (unsigned i = 0, e = Candidates.size(); i != e; ++i) {
Evan Cheng5dc57ce2010-04-13 18:16:00 +0000460 if (Candidates[i].FI != INT_MIN &&
461 StoredFIs.count(Candidates[i].FI))
Evan Chengd94671a2010-04-07 00:41:17 +0000462 continue;
463
Evan Chengaeb2f4a2010-04-13 20:21:05 +0000464 if (PhysRegDefs[Candidates[i].Def] == 1) {
465 bool Safe = true;
466 MachineInstr *MI = Candidates[i].MI;
Evan Chengc15d9132010-04-13 20:25:29 +0000467 for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
468 const MachineOperand &MO = MI->getOperand(j);
Evan Cheng63275372010-04-13 22:13:34 +0000469 if (!MO.isReg() || MO.isDef() || !MO.getReg())
Evan Chengaeb2f4a2010-04-13 20:21:05 +0000470 continue;
471 if (PhysRegDefs[MO.getReg()]) {
472 // If it's using a non-loop-invariant register, then it's obviously
473 // not safe to hoist.
474 Safe = false;
475 break;
476 }
477 }
478 if (Safe)
479 HoistPostRA(MI, Candidates[i].Def);
480 }
Evan Chengd94671a2010-04-07 00:41:17 +0000481 }
Benjamin Kramer678d9b72010-04-12 11:38:35 +0000482
483 delete[] PhysRegDefs;
Evan Chengd94671a2010-04-07 00:41:17 +0000484}
485
Jakob Stoklund Olesen9196ab62010-04-20 18:45:47 +0000486/// AddToLiveIns - Add register 'Reg' to the livein sets of BBs in the current
487/// loop, and make sure it is not killed by any instructions in the loop.
Evan Cheng94d1d9c2010-04-17 07:07:11 +0000488void MachineLICM::AddToLiveIns(unsigned Reg) {
489 const std::vector<MachineBasicBlock*> Blocks = CurLoop->getBlocks();
Jakob Stoklund Olesen9196ab62010-04-20 18:45:47 +0000490 for (unsigned i = 0, e = Blocks.size(); i != e; ++i) {
491 MachineBasicBlock *BB = Blocks[i];
492 if (!BB->isLiveIn(Reg))
493 BB->addLiveIn(Reg);
494 for (MachineBasicBlock::iterator
495 MII = BB->begin(), E = BB->end(); MII != E; ++MII) {
496 MachineInstr *MI = &*MII;
497 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
498 MachineOperand &MO = MI->getOperand(i);
499 if (!MO.isReg() || !MO.getReg() || MO.isDef()) continue;
500 if (MO.getReg() == Reg || TRI->isSuperRegister(Reg, MO.getReg()))
501 MO.setIsKill(false);
502 }
503 }
504 }
Evan Cheng4038f9c2010-04-08 01:03:47 +0000505}
506
507/// HoistPostRA - When an instruction is found to only use loop invariant
508/// operands that is safe to hoist, this instruction is called to do the
509/// dirty work.
510void MachineLICM::HoistPostRA(MachineInstr *MI, unsigned Def) {
Dan Gohman853d3fb2010-06-22 17:25:57 +0000511 MachineBasicBlock *Preheader = getCurPreheader();
512 if (!Preheader) return;
513
Evan Chengd94671a2010-04-07 00:41:17 +0000514 // Now move the instructions to the predecessor, inserting it before any
515 // terminator instructions.
516 DEBUG({
517 dbgs() << "Hoisting " << *MI;
Dan Gohman853d3fb2010-06-22 17:25:57 +0000518 if (Preheader->getBasicBlock())
Evan Chengd94671a2010-04-07 00:41:17 +0000519 dbgs() << " to MachineBasicBlock "
Dan Gohman853d3fb2010-06-22 17:25:57 +0000520 << Preheader->getName();
Evan Chengd94671a2010-04-07 00:41:17 +0000521 if (MI->getParent()->getBasicBlock())
522 dbgs() << " from MachineBasicBlock "
523 << MI->getParent()->getName();
524 dbgs() << "\n";
525 });
526
527 // Splice the instruction to the preheader.
Evan Cheng4038f9c2010-04-08 01:03:47 +0000528 MachineBasicBlock *MBB = MI->getParent();
Dan Gohman853d3fb2010-06-22 17:25:57 +0000529 Preheader->splice(Preheader->getFirstTerminator(), MBB, MI);
Evan Cheng4038f9c2010-04-08 01:03:47 +0000530
Evan Cheng94d1d9c2010-04-17 07:07:11 +0000531 // Add register to livein list to all the BBs in the current loop since a
532 // loop invariant must be kept live throughout the whole loop. This is
533 // important to ensure later passes do not scavenge the def register.
534 AddToLiveIns(Def);
Evan Chengd94671a2010-04-07 00:41:17 +0000535
536 ++NumPostRAHoisted;
537 Changed = true;
538}
539
Bill Wendling0f940c92007-12-07 21:42:31 +0000540/// HoistRegion - Walk the specified region of the CFG (defined by all blocks
541/// dominated by the specified block, and that are in the current loop) in depth
542/// first order w.r.t the DominatorTree. This allows us to visit definitions
543/// before uses, allowing us to hoist a loop body in one pass without iteration.
544///
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000545void MachineLICM::HoistRegion(MachineDomTreeNode *N, bool IsHeader) {
Bill Wendling0f940c92007-12-07 21:42:31 +0000546 assert(N != 0 && "Null dominator tree node?");
547 MachineBasicBlock *BB = N->getBlock();
548
549 // If this subregion is not in the top level loop at all, exit.
550 if (!CurLoop->contains(BB)) return;
551
Evan Cheng0e673912010-10-14 01:16:09 +0000552 MachineBasicBlock *Preheader = getCurPreheader();
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000553 if (!Preheader)
554 return;
Evan Cheng0e673912010-10-14 01:16:09 +0000555
Evan Cheng11e8b742010-10-19 00:55:07 +0000556 if (IsHeader) {
557 // Compute registers which are liveout of preheader.
558 RegSeen.clear();
559 BackTrace.clear();
560 InitRegPressure(Preheader);
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000561 }
562
Evan Cheng11e8b742010-10-19 00:55:07 +0000563 // Remember livein register pressure.
564 BackTrace.push_back(RegPressure);
565
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000566 for (MachineBasicBlock::iterator
567 MII = BB->begin(), E = BB->end(); MII != E; ) {
568 MachineBasicBlock::iterator NextMII = MII; ++NextMII;
569 MachineInstr *MI = &*MII;
570
Evan Cheng11e8b742010-10-19 00:55:07 +0000571 UpdateRegPressureBefore(MI);
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000572 Hoist(MI, Preheader);
Evan Cheng11e8b742010-10-19 00:55:07 +0000573 UpdateRegPressureAfter(MI);
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000574
575 MII = NextMII;
Dan Gohmanc475c362009-01-15 22:01:38 +0000576 }
Bill Wendling0f940c92007-12-07 21:42:31 +0000577
Dale Johannesenbf1ae5e2010-07-20 00:50:13 +0000578 // Don't hoist things out of a large switch statement. This often causes
579 // code to be hoisted that wasn't going to be executed, and increases
580 // register pressure in a situation where it's likely to matter.
Dale Johannesen21d35c12010-07-20 21:29:12 +0000581 if (BB->succ_size() < 25) {
582 const std::vector<MachineDomTreeNode*> &Children = N->getChildren();
Dale Johannesenbf1ae5e2010-07-20 00:50:13 +0000583 for (unsigned I = 0, E = Children.size(); I != E; ++I)
584 HoistRegion(Children[I]);
Dale Johannesen21d35c12010-07-20 21:29:12 +0000585 }
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000586
Evan Cheng11e8b742010-10-19 00:55:07 +0000587 BackTrace.pop_back();
Bill Wendling0f940c92007-12-07 21:42:31 +0000588}
589
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000590/// InitRegPressure - Find all virtual register references that are liveout of
591/// the preheader to initialize the starting "register pressure". Note this
592/// does not count live through (livein but not used) registers.
Evan Cheng0e673912010-10-14 01:16:09 +0000593void MachineLICM::InitRegPressure(MachineBasicBlock *BB) {
Evan Cheng0e673912010-10-14 01:16:09 +0000594 std::fill(RegPressure.begin(), RegPressure.end(), 0);
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000595
Evan Cheng0e673912010-10-14 01:16:09 +0000596 for (MachineBasicBlock::iterator MII = BB->begin(), E = BB->end();
597 MII != E; ++MII) {
598 MachineInstr *MI = &*MII;
599 for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) {
600 const MachineOperand &MO = MI->getOperand(i);
601 if (!MO.isReg() || MO.isImplicit())
602 continue;
603 unsigned Reg = MO.getReg();
604 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg))
605 continue;
Evan Cheng0e673912010-10-14 01:16:09 +0000606
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000607 bool isNew = !RegSeen.insert(Reg);
Evan Cheng0e673912010-10-14 01:16:09 +0000608 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
609 EVT VT = *RC->vt_begin();
610 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000611 if (MO.isDef())
612 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
613 else {
614 if (isNew && !MO.isKill())
615 // Haven't seen this, it must be a livein.
616 RegPressure[RCId] += TLI->getRepRegClassCostFor(VT);
617 else if (!isNew && MO.isKill())
618 RegPressure[RCId] -= TLI->getRepRegClassCostFor(VT);
619 }
Evan Cheng0e673912010-10-14 01:16:09 +0000620 }
621 }
622}
623
624/// UpdateRegPressureBefore / UpdateRegPressureAfter - Update estimate of
625/// register pressure before and after executing a specifi instruction.
626void MachineLICM::UpdateRegPressureBefore(const MachineInstr *MI) {
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000627 bool NoImpact = MI->isImplicitDef() || MI->isPHI();
Evan Cheng0e673912010-10-14 01:16:09 +0000628
629 for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) {
630 const MachineOperand &MO = MI->getOperand(i);
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000631 if (!MO.isReg() || MO.isImplicit() || !MO.isUse())
Evan Cheng0e673912010-10-14 01:16:09 +0000632 continue;
633 unsigned Reg = MO.getReg();
634 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg))
635 continue;
636
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000637 bool isNew = !RegSeen.insert(Reg);
638 if (NoImpact)
639 continue;
Evan Cheng0e673912010-10-14 01:16:09 +0000640
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000641 if (!isNew && MO.isKill()) {
642 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
643 EVT VT = *RC->vt_begin();
644 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
645 unsigned RCCost = TLI->getRepRegClassCostFor(VT);
646
647 assert(RCCost <= RegPressure[RCId]);
648 RegPressure[RCId] -= RCCost;
649 }
Evan Cheng0e673912010-10-14 01:16:09 +0000650 }
651}
652
653void MachineLICM::UpdateRegPressureAfter(const MachineInstr *MI) {
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000654 bool NoImpact = MI->isImplicitDef() || MI->isPHI();
Evan Cheng0e673912010-10-14 01:16:09 +0000655
656 for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) {
657 const MachineOperand &MO = MI->getOperand(i);
658 if (!MO.isReg() || MO.isImplicit() || !MO.isDef())
659 continue;
660 unsigned Reg = MO.getReg();
661 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg))
662 continue;
663
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000664 RegSeen.insert(Reg);
665 if (NoImpact)
666 continue;
667
Evan Cheng0e673912010-10-14 01:16:09 +0000668 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
669 EVT VT = *RC->vt_begin();
670 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
671 unsigned RCCost = TLI->getRepRegClassCostFor(VT);
672 RegPressure[RCId] += RCCost;
673 }
674}
675
Evan Cheng5dc57ce2010-04-13 18:16:00 +0000676/// IsLICMCandidate - Returns true if the instruction may be a suitable
677/// candidate for LICM. e.g. If the instruction is a call, then it's obviously
678/// not safe to hoist it.
679bool MachineLICM::IsLICMCandidate(MachineInstr &I) {
Chris Lattner77910802010-07-12 00:00:35 +0000680 // Check if it's safe to move the instruction.
681 bool DontMoveAcrossStore = true;
682 if (!I.isSafeToMove(TII, AA, DontMoveAcrossStore))
Chris Lattnera22edc82008-01-10 23:08:24 +0000683 return false;
Chris Lattner77910802010-07-12 00:00:35 +0000684
Evan Cheng5dc57ce2010-04-13 18:16:00 +0000685 return true;
686}
687
688/// IsLoopInvariantInst - Returns true if the instruction is loop
689/// invariant. I.e., all virtual register operands are defined outside of the
690/// loop, physical registers aren't accessed explicitly, and there are no side
691/// effects that aren't captured by the operands or other flags.
692///
693bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) {
694 if (!IsLICMCandidate(I))
695 return false;
Bill Wendling074223a2008-03-10 08:13:01 +0000696
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000697 // The instruction is loop invariant if all of its operands are.
Bill Wendling0f940c92007-12-07 21:42:31 +0000698 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
699 const MachineOperand &MO = I.getOperand(i);
700
Dan Gohmand735b802008-10-03 15:45:36 +0000701 if (!MO.isReg())
Bill Wendlingfb018d02008-08-20 20:32:05 +0000702 continue;
703
Dan Gohmanc475c362009-01-15 22:01:38 +0000704 unsigned Reg = MO.getReg();
705 if (Reg == 0) continue;
706
707 // Don't hoist an instruction that uses or defines a physical register.
Dan Gohmana8fb3362009-09-25 23:58:45 +0000708 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
Dan Gohmana8fb3362009-09-25 23:58:45 +0000709 if (MO.isUse()) {
710 // If the physreg has no defs anywhere, it's just an ambient register
Dan Gohman45094e32009-09-26 02:34:00 +0000711 // and we can freely move its uses. Alternatively, if it's allocatable,
712 // it could get allocated to something with a def during allocation.
Evan Cheng0e673912010-10-14 01:16:09 +0000713 if (!MRI->def_empty(Reg))
Dan Gohmana8fb3362009-09-25 23:58:45 +0000714 return false;
Dan Gohman45094e32009-09-26 02:34:00 +0000715 if (AllocatableSet.test(Reg))
716 return false;
Dan Gohmana8fb3362009-09-25 23:58:45 +0000717 // Check for a def among the register's aliases too.
Dan Gohman45094e32009-09-26 02:34:00 +0000718 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
719 unsigned AliasReg = *Alias;
Evan Cheng0e673912010-10-14 01:16:09 +0000720 if (!MRI->def_empty(AliasReg))
Dan Gohmana8fb3362009-09-25 23:58:45 +0000721 return false;
Dan Gohman45094e32009-09-26 02:34:00 +0000722 if (AllocatableSet.test(AliasReg))
723 return false;
724 }
Dan Gohmana8fb3362009-09-25 23:58:45 +0000725 // Otherwise it's safe to move.
726 continue;
727 } else if (!MO.isDead()) {
728 // A def that isn't dead. We can't move it.
729 return false;
Dan Gohmana363a9b2010-02-28 00:08:44 +0000730 } else if (CurLoop->getHeader()->isLiveIn(Reg)) {
731 // If the reg is live into the loop, we can't hoist an instruction
732 // which would clobber it.
733 return false;
Dan Gohmana8fb3362009-09-25 23:58:45 +0000734 }
735 }
Bill Wendlingfb018d02008-08-20 20:32:05 +0000736
737 if (!MO.isUse())
Bill Wendling0f940c92007-12-07 21:42:31 +0000738 continue;
739
Evan Cheng0e673912010-10-14 01:16:09 +0000740 assert(MRI->getVRegDef(Reg) &&
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +0000741 "Machine instr not mapped for this vreg?!");
Bill Wendling0f940c92007-12-07 21:42:31 +0000742
743 // If the loop contains the definition of an operand, then the instruction
744 // isn't loop invariant.
Evan Cheng0e673912010-10-14 01:16:09 +0000745 if (CurLoop->contains(MRI->getVRegDef(Reg)))
Bill Wendling0f940c92007-12-07 21:42:31 +0000746 return false;
747 }
748
749 // If we got this far, the instruction is loop invariant!
750 return true;
751}
752
Evan Chengaf6949d2009-02-05 08:45:46 +0000753
754/// HasPHIUses - Return true if the specified register has any PHI use.
Evan Cheng0e673912010-10-14 01:16:09 +0000755static bool HasPHIUses(unsigned Reg, MachineRegisterInfo *MRI) {
756 for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg),
757 UE = MRI->use_end(); UI != UE; ++UI) {
Evan Cheng45e94d62009-02-04 09:19:56 +0000758 MachineInstr *UseMI = &*UI;
Chris Lattner518bb532010-02-09 19:54:29 +0000759 if (UseMI->isPHI())
Evan Chengaf6949d2009-02-05 08:45:46 +0000760 return true;
Evan Cheng45e94d62009-02-04 09:19:56 +0000761 }
Evan Chengaf6949d2009-02-05 08:45:46 +0000762 return false;
Evan Cheng45e94d62009-02-04 09:19:56 +0000763}
764
Evan Cheng87b75ba2009-11-20 19:55:37 +0000765/// isLoadFromConstantMemory - Return true if the given instruction is a
766/// load from constant memory. Machine LICM will hoist these even if they are
767/// not re-materializable.
768bool MachineLICM::isLoadFromConstantMemory(MachineInstr *MI) {
769 if (!MI->getDesc().mayLoad()) return false;
770 if (!MI->hasOneMemOperand()) return false;
771 MachineMemOperand *MMO = *MI->memoperands_begin();
772 if (MMO->isVolatile()) return false;
773 if (!MMO->getValue()) return false;
774 const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(MMO->getValue());
775 if (PSV) {
776 MachineFunction &MF = *MI->getParent()->getParent();
777 return PSV->isConstant(MF.getFrameInfo());
778 } else {
779 return AA->pointsToConstantMemory(MMO->getValue());
780 }
781}
782
Evan Cheng11e8b742010-10-19 00:55:07 +0000783/// HasHighOperandLatency - Compute operand latency between a def of 'Reg'
784/// and an use in the current loop, return true if the target considered
785/// it 'high'.
786bool MachineLICM::HasHighOperandLatency(MachineInstr &MI,
787 unsigned DefIdx, unsigned Reg) {
Evan Cheng0e673912010-10-14 01:16:09 +0000788 if (MRI->use_nodbg_empty(Reg))
Evan Cheng11e8b742010-10-19 00:55:07 +0000789 return false;
Evan Cheng0e673912010-10-14 01:16:09 +0000790
Evan Cheng0e673912010-10-14 01:16:09 +0000791 for (MachineRegisterInfo::use_nodbg_iterator I = MRI->use_nodbg_begin(Reg),
792 E = MRI->use_nodbg_end(); I != E; ++I) {
793 MachineInstr *UseMI = &*I;
794 if (!CurLoop->contains(UseMI->getParent()))
795 continue;
796 for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
797 const MachineOperand &MO = UseMI->getOperand(i);
798 if (!MO.isReg() || !MO.isUse())
799 continue;
800 unsigned MOReg = MO.getReg();
801 if (MOReg != Reg)
802 continue;
803
Evan Cheng11e8b742010-10-19 00:55:07 +0000804 if (TII->hasHighOperandLatency(InstrItins, MRI, &MI, DefIdx, UseMI, i))
805 return true;
Evan Cheng0e673912010-10-14 01:16:09 +0000806 }
807
Evan Cheng11e8b742010-10-19 00:55:07 +0000808 // Only look at the first in loop use.
809 break;
Evan Cheng0e673912010-10-14 01:16:09 +0000810 }
811
Evan Cheng11e8b742010-10-19 00:55:07 +0000812 return false;
Evan Cheng0e673912010-10-14 01:16:09 +0000813}
814
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000815/// IncreaseHighRegPressure - Visit BBs from preheader to current BB, check
816/// if hoisting an instruction of the given cost matrix can cause high
817/// register pressure.
818bool MachineLICM::IncreaseHighRegPressure(DenseMap<unsigned, int> &Cost) {
819 for (unsigned i = BackTrace.size(); i != 0; --i) {
820 bool AnyIncrease = false;
821 SmallVector<unsigned, 8> &RP = BackTrace[i-1];
822 for (DenseMap<unsigned, int>::iterator CI = Cost.begin(), CE = Cost.end();
823 CI != CE; ++CI) {
824 if (CI->second <= 0)
825 continue;
826 AnyIncrease = true;
827 unsigned RCId = CI->first;
828 if (RP[RCId] + CI->second >= RegLimit[RCId])
829 return true;
830 }
831
832 if (!AnyIncrease)
833 // Hoisting the instruction doesn't increase register pressure.
834 return false;
835 }
836
837 return false;
838}
839
Evan Cheng45e94d62009-02-04 09:19:56 +0000840/// IsProfitableToHoist - Return true if it is potentially profitable to hoist
841/// the given loop invariant.
Evan Chengc26abd92009-11-20 23:31:34 +0000842bool MachineLICM::IsProfitableToHoist(MachineInstr &MI) {
Evan Cheng0e673912010-10-14 01:16:09 +0000843 if (MI.isImplicitDef())
844 return true;
845
Evan Cheng11e8b742010-10-19 00:55:07 +0000846 // If the instruction is cheap, only hoist if it is re-materilizable. LICM
847 // will increase register pressure. It's probably not worth it if the
848 // instruction is cheap.
Evan Cheng87b75ba2009-11-20 19:55:37 +0000849 // Also hoist loads from constant memory, e.g. load from stubs, GOT. Hoisting
850 // these tend to help performance in low register pressure situation. The
851 // trade off is it may cause spill in high pressure situation. It will end up
852 // adding a store in the loop preheader. But the reload is no more expensive.
853 // The side benefit is these loads are frequently CSE'ed.
Evan Cheng11e8b742010-10-19 00:55:07 +0000854 if (MI.getDesc().isAsCheapAsAMove()) {
855 if (!TII->isTriviallyReMaterializable(&MI, AA))
Evan Cheng0e673912010-10-14 01:16:09 +0000856 return false;
857 } else {
Evan Cheng11e8b742010-10-19 00:55:07 +0000858 // Estimate register pressure to determine whether to LICM the instruction.
Evan Cheng0e673912010-10-14 01:16:09 +0000859 // In low register pressure situation, we can be more aggressive about
860 // hoisting. Also, favors hoisting long latency instructions even in
861 // moderately high pressure situation.
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000862 DenseMap<unsigned, int> Cost;
Evan Cheng0e673912010-10-14 01:16:09 +0000863 for (unsigned i = 0, e = MI.getDesc().getNumOperands(); i != e; ++i) {
864 const MachineOperand &MO = MI.getOperand(i);
865 if (!MO.isReg() || MO.isImplicit())
866 continue;
867 unsigned Reg = MO.getReg();
868 if (!Reg || TargetRegisterInfo::isPhysicalRegister(Reg))
869 continue;
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000870 if (MO.isDef()) {
Evan Cheng11e8b742010-10-19 00:55:07 +0000871 if (HasHighOperandLatency(MI, i, Reg)) {
872 ++NumHighLatency;
873 return true;
Evan Cheng0e673912010-10-14 01:16:09 +0000874 }
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000875
876 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
877 EVT VT = *RC->vt_begin();
878 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
879 unsigned RCCost = TLI->getRepRegClassCostFor(VT);
880 DenseMap<unsigned, int>::iterator CI = Cost.find(RCId);
881 // If the instruction is not register pressure neutrail (or better),
882 // check if hoisting it will cause high register pressure in BB's
883 // leading up to this point.
884 if (CI != Cost.end())
885 CI->second += RCCost;
886 else
887 Cost.insert(std::make_pair(RCId, RCCost));
888 } else if (MO.isKill()) {
889 // Is a virtual register use is a kill, hoisting it out of the loop
890 // may actually reduce register pressure or be register pressure
891 // neutral
892 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
893 EVT VT = *RC->vt_begin();
894 unsigned RCId = TLI->getRepRegClassFor(VT)->getID();
895 unsigned RCCost = TLI->getRepRegClassCostFor(VT);
896 DenseMap<unsigned, int>::iterator CI = Cost.find(RCId);
897 if (CI != Cost.end())
898 CI->second -= RCCost;
899 else
900 Cost.insert(std::make_pair(RCId, -RCCost));
Evan Cheng0e673912010-10-14 01:16:09 +0000901 }
902 }
903
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000904 // Visit BBs from preheader to current BB, if hoisting this doesn't cause
905 // high register pressure, then it's safe to proceed.
906 if (!IncreaseHighRegPressure(Cost)) {
907 ++NumLowRP;
Evan Cheng0e673912010-10-14 01:16:09 +0000908 return true;
Evan Cheng03a9fdf2010-10-16 02:20:26 +0000909 }
Evan Cheng0e673912010-10-14 01:16:09 +0000910
911 // High register pressure situation, only hoist if the instruction is going to
912 // be remat'ed.
913 if (!TII->isTriviallyReMaterializable(&MI, AA) &&
914 !isLoadFromConstantMemory(&MI))
Evan Cheng87b75ba2009-11-20 19:55:37 +0000915 return false;
Evan Cheng87b75ba2009-11-20 19:55:37 +0000916 }
Evan Cheng45e94d62009-02-04 09:19:56 +0000917
Evan Chengaf6949d2009-02-05 08:45:46 +0000918 // If result(s) of this instruction is used by PHIs, then don't hoist it.
919 // The presence of joins makes it difficult for current register allocator
920 // implementation to perform remat.
Evan Cheng45e94d62009-02-04 09:19:56 +0000921 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
922 const MachineOperand &MO = MI.getOperand(i);
923 if (!MO.isReg() || !MO.isDef())
924 continue;
Evan Cheng0e673912010-10-14 01:16:09 +0000925 if (HasPHIUses(MO.getReg(), MRI))
Evan Chengaf6949d2009-02-05 08:45:46 +0000926 return false;
Evan Cheng45e94d62009-02-04 09:19:56 +0000927 }
Evan Chengaf6949d2009-02-05 08:45:46 +0000928
929 return true;
930}
931
Dan Gohman5c952302009-10-29 17:47:20 +0000932MachineInstr *MachineLICM::ExtractHoistableLoad(MachineInstr *MI) {
Evan Chenge95f3192010-10-08 18:59:19 +0000933 // Don't unfold simple loads.
934 if (MI->getDesc().canFoldAsLoad())
935 return 0;
936
Dan Gohman5c952302009-10-29 17:47:20 +0000937 // If not, we may be able to unfold a load and hoist that.
938 // First test whether the instruction is loading from an amenable
939 // memory location.
Evan Cheng87b75ba2009-11-20 19:55:37 +0000940 if (!isLoadFromConstantMemory(MI))
941 return 0;
942
Dan Gohman5c952302009-10-29 17:47:20 +0000943 // Next determine the register class for a temporary register.
Dan Gohman0115e162009-10-30 22:18:41 +0000944 unsigned LoadRegIndex;
Dan Gohman5c952302009-10-29 17:47:20 +0000945 unsigned NewOpc =
946 TII->getOpcodeAfterMemoryUnfold(MI->getOpcode(),
947 /*UnfoldLoad=*/true,
Dan Gohman0115e162009-10-30 22:18:41 +0000948 /*UnfoldStore=*/false,
949 &LoadRegIndex);
Dan Gohman5c952302009-10-29 17:47:20 +0000950 if (NewOpc == 0) return 0;
951 const TargetInstrDesc &TID = TII->get(NewOpc);
952 if (TID.getNumDefs() != 1) return 0;
Dan Gohman0115e162009-10-30 22:18:41 +0000953 const TargetRegisterClass *RC = TID.OpInfo[LoadRegIndex].getRegClass(TRI);
Dan Gohman5c952302009-10-29 17:47:20 +0000954 // Ok, we're unfolding. Create a temporary register and do the unfold.
Evan Cheng0e673912010-10-14 01:16:09 +0000955 unsigned Reg = MRI->createVirtualRegister(RC);
Evan Cheng87b75ba2009-11-20 19:55:37 +0000956
957 MachineFunction &MF = *MI->getParent()->getParent();
Dan Gohman5c952302009-10-29 17:47:20 +0000958 SmallVector<MachineInstr *, 2> NewMIs;
959 bool Success =
960 TII->unfoldMemoryOperand(MF, MI, Reg,
961 /*UnfoldLoad=*/true, /*UnfoldStore=*/false,
962 NewMIs);
963 (void)Success;
964 assert(Success &&
965 "unfoldMemoryOperand failed when getOpcodeAfterMemoryUnfold "
966 "succeeded!");
967 assert(NewMIs.size() == 2 &&
968 "Unfolded a load into multiple instructions!");
969 MachineBasicBlock *MBB = MI->getParent();
970 MBB->insert(MI, NewMIs[0]);
971 MBB->insert(MI, NewMIs[1]);
972 // If unfolding produced a load that wasn't loop-invariant or profitable to
973 // hoist, discard the new instructions and bail.
Evan Chengc26abd92009-11-20 23:31:34 +0000974 if (!IsLoopInvariantInst(*NewMIs[0]) || !IsProfitableToHoist(*NewMIs[0])) {
Dan Gohman5c952302009-10-29 17:47:20 +0000975 NewMIs[0]->eraseFromParent();
976 NewMIs[1]->eraseFromParent();
977 return 0;
978 }
979 // Otherwise we successfully unfolded a load that we can hoist.
980 MI->eraseFromParent();
981 return NewMIs[0];
982}
983
Evan Cheng777c6b72009-11-03 21:40:02 +0000984void MachineLICM::InitCSEMap(MachineBasicBlock *BB) {
985 for (MachineBasicBlock::iterator I = BB->begin(),E = BB->end(); I != E; ++I) {
986 const MachineInstr *MI = &*I;
987 // FIXME: For now, only hoist re-materilizable instructions. LICM will
988 // increase register pressure. We want to make sure it doesn't increase
989 // spilling.
990 if (TII->isTriviallyReMaterializable(MI, AA)) {
991 unsigned Opcode = MI->getOpcode();
992 DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator
993 CI = CSEMap.find(Opcode);
994 if (CI != CSEMap.end())
995 CI->second.push_back(MI);
996 else {
997 std::vector<const MachineInstr*> CSEMIs;
998 CSEMIs.push_back(MI);
999 CSEMap.insert(std::make_pair(Opcode, CSEMIs));
1000 }
1001 }
1002 }
1003}
1004
Evan Cheng78e5c112009-11-07 03:52:02 +00001005const MachineInstr*
1006MachineLICM::LookForDuplicate(const MachineInstr *MI,
1007 std::vector<const MachineInstr*> &PrevMIs) {
Evan Cheng9fb744e2009-11-05 00:51:13 +00001008 for (unsigned i = 0, e = PrevMIs.size(); i != e; ++i) {
1009 const MachineInstr *PrevMI = PrevMIs[i];
Evan Cheng506049f2010-03-03 01:44:33 +00001010 if (TII->produceSameValue(MI, PrevMI))
Evan Cheng9fb744e2009-11-05 00:51:13 +00001011 return PrevMI;
1012 }
1013 return 0;
1014}
1015
1016bool MachineLICM::EliminateCSE(MachineInstr *MI,
1017 DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator &CI) {
Evan Chengdb898092010-07-14 01:22:19 +00001018 // Do not CSE implicit_def so ProcessImplicitDefs can properly propagate
1019 // the undef property onto uses.
1020 if (CI == CSEMap.end() || MI->isImplicitDef())
Evan Cheng78e5c112009-11-07 03:52:02 +00001021 return false;
1022
1023 if (const MachineInstr *Dup = LookForDuplicate(MI, CI->second)) {
David Greene65a41eb2010-01-05 00:03:48 +00001024 DEBUG(dbgs() << "CSEing " << *MI << " with " << *Dup);
Dan Gohman6ac33b42010-02-28 01:33:43 +00001025
1026 // Replace virtual registers defined by MI by their counterparts defined
1027 // by Dup.
Evan Cheng78e5c112009-11-07 03:52:02 +00001028 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1029 const MachineOperand &MO = MI->getOperand(i);
Dan Gohman6ac33b42010-02-28 01:33:43 +00001030
1031 // Physical registers may not differ here.
1032 assert((!MO.isReg() || MO.getReg() == 0 ||
1033 !TargetRegisterInfo::isPhysicalRegister(MO.getReg()) ||
1034 MO.getReg() == Dup->getOperand(i).getReg()) &&
1035 "Instructions with different phys regs are not identical!");
1036
1037 if (MO.isReg() && MO.isDef() &&
Dan Gohmane6cd7572010-05-13 20:34:42 +00001038 !TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Evan Cheng0e673912010-10-14 01:16:09 +00001039 MRI->replaceRegWith(MO.getReg(), Dup->getOperand(i).getReg());
1040 MRI->clearKillFlags(Dup->getOperand(i).getReg());
Dan Gohmane6cd7572010-05-13 20:34:42 +00001041 }
Evan Cheng9fb744e2009-11-05 00:51:13 +00001042 }
Evan Cheng78e5c112009-11-07 03:52:02 +00001043 MI->eraseFromParent();
1044 ++NumCSEed;
1045 return true;
Evan Cheng9fb744e2009-11-05 00:51:13 +00001046 }
1047 return false;
1048}
1049
Bill Wendlinge4fc1cc2008-05-12 19:38:32 +00001050/// Hoist - When an instruction is found to use only loop invariant operands
1051/// that are safe to hoist, this instruction is called to do the dirty work.
Bill Wendling0f940c92007-12-07 21:42:31 +00001052///
Evan Cheng0e673912010-10-14 01:16:09 +00001053void MachineLICM::Hoist(MachineInstr *MI, MachineBasicBlock *Preheader) {
Dan Gohman589f1f52009-10-28 03:21:57 +00001054 // First check whether we should hoist this instruction.
Evan Chengc26abd92009-11-20 23:31:34 +00001055 if (!IsLoopInvariantInst(*MI) || !IsProfitableToHoist(*MI)) {
Dan Gohman5c952302009-10-29 17:47:20 +00001056 // If not, try unfolding a hoistable load.
1057 MI = ExtractHoistableLoad(MI);
1058 if (!MI) return;
Dan Gohman589f1f52009-10-28 03:21:57 +00001059 }
Bill Wendling0f940c92007-12-07 21:42:31 +00001060
Dan Gohmanc475c362009-01-15 22:01:38 +00001061 // Now move the instructions to the predecessor, inserting it before any
1062 // terminator instructions.
1063 DEBUG({
David Greene65a41eb2010-01-05 00:03:48 +00001064 dbgs() << "Hoisting " << *MI;
Dan Gohman853d3fb2010-06-22 17:25:57 +00001065 if (Preheader->getBasicBlock())
David Greene65a41eb2010-01-05 00:03:48 +00001066 dbgs() << " to MachineBasicBlock "
Dan Gohman853d3fb2010-06-22 17:25:57 +00001067 << Preheader->getName();
Dan Gohman589f1f52009-10-28 03:21:57 +00001068 if (MI->getParent()->getBasicBlock())
David Greene65a41eb2010-01-05 00:03:48 +00001069 dbgs() << " from MachineBasicBlock "
Jakob Stoklund Olesen324da762009-11-20 01:17:03 +00001070 << MI->getParent()->getName();
David Greene65a41eb2010-01-05 00:03:48 +00001071 dbgs() << "\n";
Dan Gohmanc475c362009-01-15 22:01:38 +00001072 });
Bill Wendling0f940c92007-12-07 21:42:31 +00001073
Evan Cheng777c6b72009-11-03 21:40:02 +00001074 // If this is the first instruction being hoisted to the preheader,
1075 // initialize the CSE map with potential common expressions.
Evan Cheng82e0a1a2010-05-29 00:06:36 +00001076 if (FirstInLoop) {
Dan Gohman853d3fb2010-06-22 17:25:57 +00001077 InitCSEMap(Preheader);
Evan Cheng82e0a1a2010-05-29 00:06:36 +00001078 FirstInLoop = false;
1079 }
Evan Cheng777c6b72009-11-03 21:40:02 +00001080
Evan Chengaf6949d2009-02-05 08:45:46 +00001081 // Look for opportunity to CSE the hoisted instruction.
Evan Cheng777c6b72009-11-03 21:40:02 +00001082 unsigned Opcode = MI->getOpcode();
1083 DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator
1084 CI = CSEMap.find(Opcode);
Evan Cheng9fb744e2009-11-05 00:51:13 +00001085 if (!EliminateCSE(MI, CI)) {
1086 // Otherwise, splice the instruction to the preheader.
Dan Gohman853d3fb2010-06-22 17:25:57 +00001087 Preheader->splice(Preheader->getFirstTerminator(),MI->getParent(),MI);
Evan Cheng777c6b72009-11-03 21:40:02 +00001088
Dan Gohmane6cd7572010-05-13 20:34:42 +00001089 // Clear the kill flags of any register this instruction defines,
1090 // since they may need to be live throughout the entire loop
1091 // rather than just live for part of it.
1092 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1093 MachineOperand &MO = MI->getOperand(i);
1094 if (MO.isReg() && MO.isDef() && !MO.isDead())
Evan Cheng0e673912010-10-14 01:16:09 +00001095 MRI->clearKillFlags(MO.getReg());
Dan Gohmane6cd7572010-05-13 20:34:42 +00001096 }
1097
Evan Chengaf6949d2009-02-05 08:45:46 +00001098 // Add to the CSE map.
1099 if (CI != CSEMap.end())
Dan Gohman589f1f52009-10-28 03:21:57 +00001100 CI->second.push_back(MI);
Evan Chengaf6949d2009-02-05 08:45:46 +00001101 else {
1102 std::vector<const MachineInstr*> CSEMIs;
Dan Gohman589f1f52009-10-28 03:21:57 +00001103 CSEMIs.push_back(MI);
Evan Cheng777c6b72009-11-03 21:40:02 +00001104 CSEMap.insert(std::make_pair(Opcode, CSEMIs));
Evan Chengaf6949d2009-02-05 08:45:46 +00001105 }
1106 }
Bill Wendling0f940c92007-12-07 21:42:31 +00001107
Dan Gohmanc475c362009-01-15 22:01:38 +00001108 ++NumHoisted;
Bill Wendling0f940c92007-12-07 21:42:31 +00001109 Changed = true;
Bill Wendling0f940c92007-12-07 21:42:31 +00001110}
Dan Gohman853d3fb2010-06-22 17:25:57 +00001111
1112MachineBasicBlock *MachineLICM::getCurPreheader() {
1113 // Determine the block to which to hoist instructions. If we can't find a
1114 // suitable loop predecessor, we can't do any hoisting.
1115
1116 // If we've tried to get a preheader and failed, don't try again.
1117 if (CurPreheader == reinterpret_cast<MachineBasicBlock *>(-1))
1118 return 0;
1119
1120 if (!CurPreheader) {
1121 CurPreheader = CurLoop->getLoopPreheader();
1122 if (!CurPreheader) {
1123 MachineBasicBlock *Pred = CurLoop->getLoopPredecessor();
1124 if (!Pred) {
1125 CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1);
1126 return 0;
1127 }
1128
1129 CurPreheader = Pred->SplitCriticalEdge(CurLoop->getHeader(), this);
1130 if (!CurPreheader) {
1131 CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1);
1132 return 0;
1133 }
1134 }
1135 }
1136 return CurPreheader;
1137}