blob: 46a8247701585b6e3da3837cb7a517b26b097bdb [file] [log] [blame]
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001//===-- RegAllocGreedy.cpp - greedy register allocator --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the RAGreedy function pass for register allocation in
11// optimized builds.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "regalloc"
Jakob Stoklund Olesendd479e92010-12-10 22:21:05 +000016#include "AllocationOrder.h"
Jakob Stoklund Olesen5907d862011-04-02 06:03:35 +000017#include "InterferenceCache.h"
Jakob Stoklund Olesencfafc542011-04-05 21:40:37 +000018#include "LiveDebugVariables.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000019#include "RegAllocBase.h"
20#include "Spiller.h"
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000021#include "SpillPlacement.h"
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +000022#include "SplitKit.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000023#include "VirtRegMap.h"
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +000024#include "llvm/ADT/Statistic.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000025#include "llvm/Analysis/AliasAnalysis.h"
26#include "llvm/Function.h"
27#include "llvm/PassAnalysisSupport.h"
28#include "llvm/CodeGen/CalcSpillWeights.h"
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000029#include "llvm/CodeGen/EdgeBundles.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000030#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Pete Cooper789d5d82012-04-02 22:44:18 +000031#include "llvm/CodeGen/LiveRangeEdit.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000032#include "llvm/CodeGen/LiveStackAnalysis.h"
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +000033#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000034#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000035#include "llvm/CodeGen/MachineLoopInfo.h"
36#include "llvm/CodeGen/MachineRegisterInfo.h"
37#include "llvm/CodeGen/Passes.h"
38#include "llvm/CodeGen/RegAllocRegistry.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000039#include "llvm/Target/TargetOptions.h"
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +000040#include "llvm/Support/CommandLine.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000041#include "llvm/Support/Debug.h"
42#include "llvm/Support/ErrorHandling.h"
43#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +000044#include "llvm/Support/Timer.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000045
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000046#include <queue>
47
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000048using namespace llvm;
49
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +000050STATISTIC(NumGlobalSplits, "Number of split global live ranges");
51STATISTIC(NumLocalSplits, "Number of split local live ranges");
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +000052STATISTIC(NumEvicted, "Number of interferences evicted");
53
Jakob Stoklund Olesen708d06f2011-09-12 16:49:21 +000054static cl::opt<SplitEditor::ComplementSpillMode>
55SplitSpillMode("split-spill-mode", cl::Hidden,
56 cl::desc("Spill mode for splitting live ranges"),
57 cl::values(clEnumValN(SplitEditor::SM_Partition, "default", "Default"),
58 clEnumValN(SplitEditor::SM_Size, "size", "Optimize for size"),
59 clEnumValN(SplitEditor::SM_Speed, "speed", "Optimize for speed"),
60 clEnumValEnd),
61 cl::init(SplitEditor::SM_Partition));
62
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000063static RegisterRegAlloc greedyRegAlloc("greedy", "greedy register allocator",
64 createGreedyRegisterAllocator);
65
66namespace {
Jakob Stoklund Olesen92a55f42011-03-09 00:57:29 +000067class RAGreedy : public MachineFunctionPass,
68 public RegAllocBase,
69 private LiveRangeEdit::Delegate {
70
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000071 // context
72 MachineFunction *MF;
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000073
74 // analyses
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000075 SlotIndexes *Indexes;
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +000076 MachineDominatorTree *DomTree;
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +000077 MachineLoopInfo *Loops;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000078 EdgeBundles *Bundles;
79 SpillPlacement *SpillPlacer;
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +000080 LiveDebugVariables *DebugVars;
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +000081
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000082 // state
83 std::auto_ptr<Spiller> SpillerInstance;
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000084 std::priority_queue<std::pair<unsigned, unsigned> > Queue;
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +000085 unsigned NextCascade;
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +000086
87 // Live ranges pass through a number of stages as we try to allocate them.
88 // Some of the stages may also create new live ranges:
89 //
90 // - Region splitting.
91 // - Per-block splitting.
92 // - Local splitting.
93 // - Spilling.
94 //
95 // Ranges produced by one of the stages skip the previous stages when they are
96 // dequeued. This improves performance because we can skip interference checks
97 // that are unlikely to give any results. It also guarantees that the live
98 // range splitting algorithm terminates, something that is otherwise hard to
99 // ensure.
100 enum LiveRangeStage {
Jakob Stoklund Olesenfa89a032011-07-25 15:25:41 +0000101 /// Newly created live range that has never been queued.
102 RS_New,
103
104 /// Only attempt assignment and eviction. Then requeue as RS_Split.
105 RS_Assign,
106
107 /// Attempt live range splitting if assignment is impossible.
108 RS_Split,
109
Jakob Stoklund Olesen49743b12011-07-25 15:25:43 +0000110 /// Attempt more aggressive live range splitting that is guaranteed to make
111 /// progress. This is used for split products that may not be making
112 /// progress.
113 RS_Split2,
114
Jakob Stoklund Olesenfa89a032011-07-25 15:25:41 +0000115 /// Live range will be spilled. No more splitting will be attempted.
116 RS_Spill,
117
118 /// There is nothing more we can do to this live range. Abort compilation
119 /// if it can't be assigned.
120 RS_Done
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +0000121 };
122
Jakob Stoklund Olesenb8d936b2011-05-25 23:58:36 +0000123 static const char *const StageName[];
124
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000125 // RegInfo - Keep additional information about each live range.
126 struct RegInfo {
127 LiveRangeStage Stage;
128
129 // Cascade - Eviction loop prevention. See canEvictInterference().
130 unsigned Cascade;
131
132 RegInfo() : Stage(RS_New), Cascade(0) {}
133 };
134
135 IndexedMap<RegInfo, VirtReg2IndexFunctor> ExtraRegInfo;
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +0000136
137 LiveRangeStage getStage(const LiveInterval &VirtReg) const {
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000138 return ExtraRegInfo[VirtReg.reg].Stage;
139 }
140
141 void setStage(const LiveInterval &VirtReg, LiveRangeStage Stage) {
142 ExtraRegInfo.resize(MRI->getNumVirtRegs());
143 ExtraRegInfo[VirtReg.reg].Stage = Stage;
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +0000144 }
145
146 template<typename Iterator>
147 void setStage(Iterator Begin, Iterator End, LiveRangeStage NewStage) {
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000148 ExtraRegInfo.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesenf22ca3f2011-03-30 02:52:39 +0000149 for (;Begin != End; ++Begin) {
150 unsigned Reg = (*Begin)->reg;
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000151 if (ExtraRegInfo[Reg].Stage == RS_New)
152 ExtraRegInfo[Reg].Stage = NewStage;
Jakob Stoklund Olesenf22ca3f2011-03-30 02:52:39 +0000153 }
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +0000154 }
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000155
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000156 /// Cost of evicting interference.
157 struct EvictionCost {
158 unsigned BrokenHints; ///< Total number of broken hints.
159 float MaxWeight; ///< Maximum spill weight evicted.
160
161 EvictionCost(unsigned B = 0) : BrokenHints(B), MaxWeight(0) {}
162
163 bool operator<(const EvictionCost &O) const {
164 if (BrokenHints != O.BrokenHints)
165 return BrokenHints < O.BrokenHints;
166 return MaxWeight < O.MaxWeight;
167 }
168 };
169
Jakob Stoklund Olesene7c2c152012-02-09 18:25:05 +0000170 // Register mask interference. The current VirtReg is checked for register
171 // mask interference on entry to selectOrSplit(). If there is no
172 // interference, UsableRegs is left empty. If there is interference,
173 // UsableRegs has a bit mask of registers that can be used without register
174 // mask interference.
175 BitVector UsableRegs;
176
177 /// clobberedByRegMask - Returns true if PhysReg is not directly usable
178 /// because of register mask clobbers.
179 bool clobberedByRegMask(unsigned PhysReg) const {
180 return !UsableRegs.empty() && !UsableRegs.test(PhysReg);
181 }
182
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000183 // splitting state.
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +0000184 std::auto_ptr<SplitAnalysis> SA;
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000185 std::auto_ptr<SplitEditor> SE;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000186
Jakob Stoklund Oleseneda0fe82011-04-02 06:03:38 +0000187 /// Cached per-block interference maps
188 InterferenceCache IntfCache;
189
Jakob Stoklund Olesen7b41fbe2011-04-07 17:27:46 +0000190 /// All basic blocks where the current register has uses.
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000191 SmallVector<SpillPlacement::BlockConstraint, 8> SplitConstraints;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000192
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000193 /// Global live range splitting candidate info.
194 struct GlobalSplitCandidate {
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +0000195 // Register intended for assignment, or 0.
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000196 unsigned PhysReg;
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +0000197
198 // SplitKit interval index for this candidate.
199 unsigned IntvIdx;
200
201 // Interference for PhysReg.
Jakob Stoklund Olesenc66a37d2011-07-14 00:17:10 +0000202 InterferenceCache::Cursor Intf;
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +0000203
204 // Bundles where this candidate should be live.
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000205 BitVector LiveBundles;
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000206 SmallVector<unsigned, 8> ActiveBlocks;
207
Jakob Stoklund Olesenc66a37d2011-07-14 00:17:10 +0000208 void reset(InterferenceCache &Cache, unsigned Reg) {
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000209 PhysReg = Reg;
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +0000210 IntvIdx = 0;
Jakob Stoklund Olesenc66a37d2011-07-14 00:17:10 +0000211 Intf.setPhysReg(Cache, Reg);
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000212 LiveBundles.clear();
213 ActiveBlocks.clear();
214 }
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +0000215
216 // Set B[i] = C for every live bundle where B[i] was NoCand.
217 unsigned getBundles(SmallVectorImpl<unsigned> &B, unsigned C) {
218 unsigned Count = 0;
219 for (int i = LiveBundles.find_first(); i >= 0;
220 i = LiveBundles.find_next(i))
221 if (B[i] == NoCand) {
222 B[i] = C;
223 Count++;
224 }
225 return Count;
226 }
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000227 };
228
229 /// Candidate info for for each PhysReg in AllocationOrder.
230 /// This vector never shrinks, but grows to the size of the largest register
231 /// class.
232 SmallVector<GlobalSplitCandidate, 32> GlobalCand;
233
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +0000234 enum { NoCand = ~0u };
235
236 /// Candidate map. Each edge bundle is assigned to a GlobalCand entry, or to
237 /// NoCand which indicates the stack interval.
238 SmallVector<unsigned, 32> BundleCand;
239
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000240public:
241 RAGreedy();
242
243 /// Return the pass name.
244 virtual const char* getPassName() const {
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +0000245 return "Greedy Register Allocator";
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000246 }
247
248 /// RAGreedy analysis usage.
249 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000250 virtual void releaseMemory();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000251 virtual Spiller &spiller() { return *SpillerInstance; }
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +0000252 virtual void enqueue(LiveInterval *LI);
253 virtual LiveInterval *dequeue();
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000254 virtual unsigned selectOrSplit(LiveInterval&,
255 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000256
257 /// Perform register allocation.
258 virtual bool runOnMachineFunction(MachineFunction &mf);
259
260 static char ID;
Andrew Trickb853e6c2010-12-09 18:15:21 +0000261
262private:
Jakob Stoklund Olesen7792e982011-03-13 01:23:11 +0000263 bool LRE_CanEraseVirtReg(unsigned);
Jakob Stoklund Olesen1d5b8452011-03-16 22:56:16 +0000264 void LRE_WillShrinkVirtReg(unsigned);
Jakob Stoklund Olesenf22ca3f2011-03-30 02:52:39 +0000265 void LRE_DidCloneVirtReg(unsigned, unsigned);
Jakob Stoklund Olesen92a55f42011-03-09 00:57:29 +0000266
Jakob Stoklund Olesen20072982011-04-22 22:47:40 +0000267 float calcSpillCost();
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000268 bool addSplitConstraints(InterferenceCache::Cursor, float&);
269 void addThroughConstraints(InterferenceCache::Cursor, ArrayRef<unsigned>);
Jakob Stoklund Olesenc66a37d2011-07-14 00:17:10 +0000270 void growRegion(GlobalSplitCandidate &Cand);
271 float calcGlobalSplitCost(GlobalSplitCandidate&);
Jakob Stoklund Olesen87972fa2011-07-23 03:41:57 +0000272 bool calcCompactRegion(GlobalSplitCandidate&);
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +0000273 void splitAroundRegion(LiveRangeEdit&, ArrayRef<unsigned>);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +0000274 void calcGapWeights(unsigned, SmallVectorImpl<float>&);
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000275 bool shouldEvict(LiveInterval &A, bool, LiveInterval &B, bool);
276 bool canEvictInterference(LiveInterval&, unsigned, bool, EvictionCost&);
277 void evictInterference(LiveInterval&, unsigned,
278 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesenb64d92e2010-12-14 00:37:44 +0000279
Jakob Stoklund Olesen6bfba2e2011-04-20 18:19:48 +0000280 unsigned tryAssign(LiveInterval&, AllocationOrder&,
281 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000282 unsigned tryEvict(LiveInterval&, AllocationOrder&,
Jakob Stoklund Olesen6bfba2e2011-04-20 18:19:48 +0000283 SmallVectorImpl<LiveInterval*>&, unsigned = ~0u);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000284 unsigned tryRegionSplit(LiveInterval&, AllocationOrder&,
285 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesendab35d32011-08-05 23:04:18 +0000286 unsigned tryBlockSplit(LiveInterval&, AllocationOrder&,
287 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesend74d2842012-05-23 22:37:27 +0000288 unsigned tryInstructionSplit(LiveInterval&, AllocationOrder&,
289 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +0000290 unsigned tryLocalSplit(LiveInterval&, AllocationOrder&,
291 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesenb64d92e2010-12-14 00:37:44 +0000292 unsigned trySplit(LiveInterval&, AllocationOrder&,
293 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000294};
295} // end anonymous namespace
296
297char RAGreedy::ID = 0;
298
Jakob Stoklund Olesenb8d936b2011-05-25 23:58:36 +0000299#ifndef NDEBUG
300const char *const RAGreedy::StageName[] = {
Jakob Stoklund Olesenfa89a032011-07-25 15:25:41 +0000301 "RS_New",
302 "RS_Assign",
303 "RS_Split",
Jakob Stoklund Olesen49743b12011-07-25 15:25:43 +0000304 "RS_Split2",
Jakob Stoklund Olesenfa89a032011-07-25 15:25:41 +0000305 "RS_Spill",
306 "RS_Done"
Jakob Stoklund Olesenb8d936b2011-05-25 23:58:36 +0000307};
308#endif
309
Jakob Stoklund Olesen20072982011-04-22 22:47:40 +0000310// Hysteresis to use when comparing floats.
311// This helps stabilize decisions based on float comparisons.
312const float Hysteresis = 0.98f;
313
314
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000315FunctionPass* llvm::createGreedyRegisterAllocator() {
316 return new RAGreedy();
317}
318
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000319RAGreedy::RAGreedy(): MachineFunctionPass(ID) {
Jakob Stoklund Olesencfafc542011-04-05 21:40:37 +0000320 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000321 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000322 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
323 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
Rafael Espindola5b220212011-06-26 22:34:10 +0000324 initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry());
Andrew Trick42b7a712012-01-17 06:55:03 +0000325 initializeMachineSchedulerPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000326 initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
327 initializeLiveStacksPass(*PassRegistry::getPassRegistry());
328 initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
329 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
330 initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000331 initializeEdgeBundlesPass(*PassRegistry::getPassRegistry());
332 initializeSpillPlacementPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000333}
334
335void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const {
336 AU.setPreservesCFG();
337 AU.addRequired<AliasAnalysis>();
338 AU.addPreserved<AliasAnalysis>();
339 AU.addRequired<LiveIntervals>();
Jakob Stoklund Olesen05ec7122012-06-08 23:44:45 +0000340 AU.addPreserved<LiveIntervals>();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000341 AU.addRequired<SlotIndexes>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000342 AU.addPreserved<SlotIndexes>();
Jakob Stoklund Olesencfafc542011-04-05 21:40:37 +0000343 AU.addRequired<LiveDebugVariables>();
344 AU.addPreserved<LiveDebugVariables>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000345 AU.addRequired<CalculateSpillWeights>();
346 AU.addRequired<LiveStacks>();
347 AU.addPreserved<LiveStacks>();
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +0000348 AU.addRequired<MachineDominatorTree>();
349 AU.addPreserved<MachineDominatorTree>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000350 AU.addRequired<MachineLoopInfo>();
351 AU.addPreserved<MachineLoopInfo>();
352 AU.addRequired<VirtRegMap>();
353 AU.addPreserved<VirtRegMap>();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000354 AU.addRequired<EdgeBundles>();
355 AU.addRequired<SpillPlacement>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000356 MachineFunctionPass::getAnalysisUsage(AU);
357}
358
Jakob Stoklund Olesen92a55f42011-03-09 00:57:29 +0000359
360//===----------------------------------------------------------------------===//
361// LiveRangeEdit delegate methods
362//===----------------------------------------------------------------------===//
363
Jakob Stoklund Olesen7792e982011-03-13 01:23:11 +0000364bool RAGreedy::LRE_CanEraseVirtReg(unsigned VirtReg) {
365 if (unsigned PhysReg = VRM->getPhys(VirtReg)) {
366 unassign(LIS->getInterval(VirtReg), PhysReg);
367 return true;
368 }
369 // Unassigned virtreg is probably in the priority queue.
370 // RegAllocBase will erase it after dequeueing.
371 return false;
372}
Jakob Stoklund Olesen92a55f42011-03-09 00:57:29 +0000373
Jakob Stoklund Olesen1d5b8452011-03-16 22:56:16 +0000374void RAGreedy::LRE_WillShrinkVirtReg(unsigned VirtReg) {
375 unsigned PhysReg = VRM->getPhys(VirtReg);
376 if (!PhysReg)
377 return;
378
379 // Register is assigned, put it back on the queue for reassignment.
380 LiveInterval &LI = LIS->getInterval(VirtReg);
381 unassign(LI, PhysReg);
382 enqueue(&LI);
383}
384
Jakob Stoklund Olesenf22ca3f2011-03-30 02:52:39 +0000385void RAGreedy::LRE_DidCloneVirtReg(unsigned New, unsigned Old) {
Jakob Stoklund Olesen0d4fea72011-09-14 17:34:37 +0000386 // Cloning a register we haven't even heard about yet? Just ignore it.
387 if (!ExtraRegInfo.inBounds(Old))
388 return;
389
Jakob Stoklund Olesenf22ca3f2011-03-30 02:52:39 +0000390 // LRE may clone a virtual register because dead code elimination causes it to
Jakob Stoklund Olesen165e2312011-07-26 00:54:56 +0000391 // be split into connected components. The new components are much smaller
392 // than the original, so they should get a new chance at being assigned.
Jakob Stoklund Olesenf22ca3f2011-03-30 02:52:39 +0000393 // same stage as the parent.
Jakob Stoklund Olesen165e2312011-07-26 00:54:56 +0000394 ExtraRegInfo[Old].Stage = RS_Assign;
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000395 ExtraRegInfo.grow(New);
396 ExtraRegInfo[New] = ExtraRegInfo[Old];
Jakob Stoklund Olesenf22ca3f2011-03-30 02:52:39 +0000397}
398
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000399void RAGreedy::releaseMemory() {
400 SpillerInstance.reset(0);
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000401 ExtraRegInfo.clear();
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000402 GlobalCand.clear();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000403 RegAllocBase::releaseMemory();
404}
405
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +0000406void RAGreedy::enqueue(LiveInterval *LI) {
407 // Prioritize live ranges by size, assigning larger ranges first.
408 // The queue holds (size, reg) pairs.
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +0000409 const unsigned Size = LI->getSize();
410 const unsigned Reg = LI->reg;
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +0000411 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
412 "Can only enqueue virtual registers");
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +0000413 unsigned Prio;
Jakob Stoklund Olesen90c1d7d2010-12-08 22:57:16 +0000414
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000415 ExtraRegInfo.grow(Reg);
416 if (ExtraRegInfo[Reg].Stage == RS_New)
Jakob Stoklund Olesenfa89a032011-07-25 15:25:41 +0000417 ExtraRegInfo[Reg].Stage = RS_Assign;
Jakob Stoklund Olesenf22ca3f2011-03-30 02:52:39 +0000418
Jakob Stoklund Olesencc07e042011-07-28 20:48:23 +0000419 if (ExtraRegInfo[Reg].Stage == RS_Split) {
Jakob Stoklund Oleseneb291572011-03-27 22:49:21 +0000420 // Unsplit ranges that couldn't be allocated immediately are deferred until
Jakob Stoklund Olesena16a25d2011-09-12 16:54:42 +0000421 // everything else has been allocated.
422 Prio = Size;
Jakob Stoklund Olesencc07e042011-07-28 20:48:23 +0000423 } else {
Jakob Stoklund Olesena16a25d2011-09-12 16:54:42 +0000424 // Everything is allocated in long->short order. Long ranges that don't fit
425 // should be spilled (or split) ASAP so they don't create interference.
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +0000426 Prio = (1u << 31) + Size;
Jakob Stoklund Olesend2a50732011-02-23 00:56:56 +0000427
Jakob Stoklund Oleseneb291572011-03-27 22:49:21 +0000428 // Boost ranges that have a physical register hint.
429 if (TargetRegisterInfo::isPhysicalRegister(VRM->getRegAllocPref(Reg)))
430 Prio |= (1u << 30);
431 }
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +0000432
Jakob Stoklund Olesene3b23cd2012-04-02 22:30:39 +0000433 Queue.push(std::make_pair(Prio, ~Reg));
Jakob Stoklund Olesen90c1d7d2010-12-08 22:57:16 +0000434}
435
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +0000436LiveInterval *RAGreedy::dequeue() {
437 if (Queue.empty())
438 return 0;
Jakob Stoklund Olesene3b23cd2012-04-02 22:30:39 +0000439 LiveInterval *LI = &LIS->getInterval(~Queue.top().second);
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +0000440 Queue.pop();
441 return LI;
442}
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +0000443
Jakob Stoklund Olesen6bfba2e2011-04-20 18:19:48 +0000444
445//===----------------------------------------------------------------------===//
446// Direct Assignment
447//===----------------------------------------------------------------------===//
448
449/// tryAssign - Try to assign VirtReg to an available register.
450unsigned RAGreedy::tryAssign(LiveInterval &VirtReg,
451 AllocationOrder &Order,
452 SmallVectorImpl<LiveInterval*> &NewVRegs) {
453 Order.rewind();
454 unsigned PhysReg;
Jakob Stoklund Olesene7c2c152012-02-09 18:25:05 +0000455 while ((PhysReg = Order.next())) {
456 if (clobberedByRegMask(PhysReg))
457 continue;
Jakob Stoklund Olesen6bfba2e2011-04-20 18:19:48 +0000458 if (!checkPhysRegInterference(VirtReg, PhysReg))
459 break;
Jakob Stoklund Olesene7c2c152012-02-09 18:25:05 +0000460 }
Jakob Stoklund Olesen6bfba2e2011-04-20 18:19:48 +0000461 if (!PhysReg || Order.isHint(PhysReg))
462 return PhysReg;
463
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000464 // PhysReg is available, but there may be a better choice.
465
466 // If we missed a simple hint, try to cheaply evict interference from the
467 // preferred register.
468 if (unsigned Hint = MRI->getSimpleHint(VirtReg.reg))
Jakob Stoklund Olesene7c2c152012-02-09 18:25:05 +0000469 if (Order.isHint(Hint) && !clobberedByRegMask(Hint)) {
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000470 DEBUG(dbgs() << "missed hint " << PrintReg(Hint, TRI) << '\n');
471 EvictionCost MaxCost(1);
472 if (canEvictInterference(VirtReg, Hint, true, MaxCost)) {
473 evictInterference(VirtReg, Hint, NewVRegs);
474 return Hint;
475 }
476 }
477
478 // Try to evict interference from a cheaper alternative.
Jakob Stoklund Olesen6bfba2e2011-04-20 18:19:48 +0000479 unsigned Cost = TRI->getCostPerUse(PhysReg);
480
481 // Most registers have 0 additional cost.
482 if (!Cost)
483 return PhysReg;
484
485 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is available at cost " << Cost
486 << '\n');
487 unsigned CheapReg = tryEvict(VirtReg, Order, NewVRegs, Cost);
488 return CheapReg ? CheapReg : PhysReg;
489}
490
491
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +0000492//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000493// Interference eviction
494//===----------------------------------------------------------------------===//
495
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000496/// shouldEvict - determine if A should evict the assigned live range B. The
497/// eviction policy defined by this function together with the allocation order
498/// defined by enqueue() decides which registers ultimately end up being split
499/// and spilled.
Jakob Stoklund Olesenb8d936b2011-05-25 23:58:36 +0000500///
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000501/// Cascade numbers are used to prevent infinite loops if this function is a
502/// cyclic relation.
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000503///
504/// @param A The live range to be assigned.
505/// @param IsHint True when A is about to be assigned to its preferred
506/// register.
507/// @param B The live range to be evicted.
508/// @param BreaksHint True when B is already assigned to its preferred register.
509bool RAGreedy::shouldEvict(LiveInterval &A, bool IsHint,
510 LiveInterval &B, bool BreaksHint) {
Jakob Stoklund Olesen49743b12011-07-25 15:25:43 +0000511 bool CanSplit = getStage(B) < RS_Spill;
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000512
513 // Be fairly aggressive about following hints as long as the evictee can be
514 // split.
515 if (CanSplit && IsHint && !BreaksHint)
516 return true;
517
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000518 return A.weight > B.weight;
Jakob Stoklund Olesenb8d936b2011-05-25 23:58:36 +0000519}
520
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000521/// canEvictInterference - Return true if all interferences between VirtReg and
522/// PhysReg can be evicted. When OnlyCheap is set, don't do anything
523///
524/// @param VirtReg Live range that is about to be assigned.
525/// @param PhysReg Desired register for assignment.
526/// @prarm IsHint True when PhysReg is VirtReg's preferred register.
527/// @param MaxCost Only look for cheaper candidates and update with new cost
528/// when returning true.
529/// @returns True when interference can be evicted cheaper than MaxCost.
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000530bool RAGreedy::canEvictInterference(LiveInterval &VirtReg, unsigned PhysReg,
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000531 bool IsHint, EvictionCost &MaxCost) {
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000532 // Find VirtReg's cascade number. This will be unassigned if VirtReg was never
533 // involved in an eviction before. If a cascade number was assigned, deny
534 // evicting anything with the same or a newer cascade number. This prevents
535 // infinite eviction loops.
536 //
537 // This works out so a register without a cascade number is allowed to evict
538 // anything, and it can be evicted by anything.
539 unsigned Cascade = ExtraRegInfo[VirtReg.reg].Cascade;
540 if (!Cascade)
541 Cascade = NextCascade;
542
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000543 EvictionCost Cost;
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000544 for (MCRegAliasIterator AI(PhysReg, TRI, true); AI.isValid(); ++AI) {
545 LiveIntervalUnion::Query &Q = query(VirtReg, *AI);
Jakob Stoklund Olesen3f5bedf2011-04-11 21:47:01 +0000546 // If there is 10 or more interferences, chances are one is heavier.
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000547 if (Q.collectInterferingVRegs(10) >= 10)
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000548 return false;
549
Jakob Stoklund Olesen3f5bedf2011-04-11 21:47:01 +0000550 // Check if any interfering live range is heavier than MaxWeight.
551 for (unsigned i = Q.interferingVRegs().size(); i; --i) {
552 LiveInterval *Intf = Q.interferingVRegs()[i - 1];
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000553 if (TargetRegisterInfo::isPhysicalRegister(Intf->reg))
554 return false;
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000555 // Never evict spill products. They cannot split or spill.
Jakob Stoklund Olesenfa89a032011-07-25 15:25:41 +0000556 if (getStage(*Intf) == RS_Done)
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000557 return false;
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000558 // Once a live range becomes small enough, it is urgent that we find a
559 // register for it. This is indicated by an infinite spill weight. These
560 // urgent live ranges get to evict almost anything.
Jakob Stoklund Olesen9cda1be2012-05-30 21:46:58 +0000561 //
562 // Also allow urgent evictions of unspillable ranges from a strictly
563 // larger allocation order.
564 bool Urgent = !VirtReg.isSpillable() &&
565 (Intf->isSpillable() ||
566 RegClassInfo.getNumAllocatableRegs(MRI->getRegClass(VirtReg.reg)) <
567 RegClassInfo.getNumAllocatableRegs(MRI->getRegClass(Intf->reg)));
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000568 // Only evict older cascades or live ranges without a cascade.
569 unsigned IntfCascade = ExtraRegInfo[Intf->reg].Cascade;
570 if (Cascade <= IntfCascade) {
571 if (!Urgent)
572 return false;
573 // We permit breaking cascades for urgent evictions. It should be the
574 // last resort, though, so make it really expensive.
575 Cost.BrokenHints += 10;
576 }
577 // Would this break a satisfied hint?
578 bool BreaksHint = VRM->hasPreferredPhys(Intf->reg);
579 // Update eviction cost.
580 Cost.BrokenHints += BreaksHint;
581 Cost.MaxWeight = std::max(Cost.MaxWeight, Intf->weight);
582 // Abort if this would be too expensive.
583 if (!(Cost < MaxCost))
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000584 return false;
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000585 // Finally, apply the eviction policy for non-urgent evictions.
586 if (!Urgent && !shouldEvict(VirtReg, IsHint, *Intf, BreaksHint))
Jakob Stoklund Olesend2056e52011-05-31 21:02:44 +0000587 return false;
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000588 }
589 }
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000590 MaxCost = Cost;
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000591 return true;
592}
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000593
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000594/// evictInterference - Evict any interferring registers that prevent VirtReg
595/// from being assigned to Physreg. This assumes that canEvictInterference
596/// returned true.
597void RAGreedy::evictInterference(LiveInterval &VirtReg, unsigned PhysReg,
598 SmallVectorImpl<LiveInterval*> &NewVRegs) {
599 // Make sure that VirtReg has a cascade number, and assign that cascade
600 // number to every evicted register. These live ranges than then only be
601 // evicted by a newer cascade, preventing infinite loops.
602 unsigned Cascade = ExtraRegInfo[VirtReg.reg].Cascade;
603 if (!Cascade)
604 Cascade = ExtraRegInfo[VirtReg.reg].Cascade = NextCascade++;
605
606 DEBUG(dbgs() << "evicting " << PrintReg(PhysReg, TRI)
607 << " interference: Cascade " << Cascade << '\n');
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000608 for (MCRegAliasIterator AI(PhysReg, TRI, true); AI.isValid(); ++AI) {
609 LiveIntervalUnion::Query &Q = query(VirtReg, *AI);
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000610 assert(Q.seenAllInterferences() && "Didn't check all interfererences.");
611 for (unsigned i = 0, e = Q.interferingVRegs().size(); i != e; ++i) {
612 LiveInterval *Intf = Q.interferingVRegs()[i];
613 unassign(*Intf, VRM->getPhys(Intf->reg));
614 assert((ExtraRegInfo[Intf->reg].Cascade < Cascade ||
615 VirtReg.isSpillable() < Intf->isSpillable()) &&
616 "Cannot decrease cascade number, illegal eviction");
617 ExtraRegInfo[Intf->reg].Cascade = Cascade;
618 ++NumEvicted;
619 NewVRegs.push_back(Intf);
620 }
621 }
622}
623
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000624/// tryEvict - Try to evict all interferences for a physreg.
Jakob Stoklund Olesen76395c92011-06-01 18:45:02 +0000625/// @param VirtReg Currently unassigned virtual register.
626/// @param Order Physregs to try.
627/// @return Physreg to assign VirtReg, or 0.
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000628unsigned RAGreedy::tryEvict(LiveInterval &VirtReg,
629 AllocationOrder &Order,
Jakob Stoklund Olesen6bfba2e2011-04-20 18:19:48 +0000630 SmallVectorImpl<LiveInterval*> &NewVRegs,
631 unsigned CostPerUseLimit) {
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000632 NamedRegionTimer T("Evict", TimerGroupName, TimePassesIsEnabled);
633
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000634 // Keep track of the cheapest interference seen so far.
635 EvictionCost BestCost(~0u);
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000636 unsigned BestPhys = 0;
637
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000638 // When we are just looking for a reduced cost per use, don't break any
639 // hints, and only evict smaller spill weights.
640 if (CostPerUseLimit < ~0u) {
641 BestCost.BrokenHints = 0;
642 BestCost.MaxWeight = VirtReg.weight;
643 }
644
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000645 Order.rewind();
646 while (unsigned PhysReg = Order.next()) {
Jakob Stoklund Olesene7c2c152012-02-09 18:25:05 +0000647 if (clobberedByRegMask(PhysReg))
648 continue;
Jakob Stoklund Olesen6bfba2e2011-04-20 18:19:48 +0000649 if (TRI->getCostPerUse(PhysReg) >= CostPerUseLimit)
650 continue;
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000651 // The first use of a callee-saved register in a function has cost 1.
652 // Don't start using a CSR when the CostPerUseLimit is low.
653 if (CostPerUseLimit == 1)
654 if (unsigned CSR = RegClassInfo.getLastCalleeSavedAlias(PhysReg))
655 if (!MRI->isPhysRegUsed(CSR)) {
656 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " would clobber CSR "
657 << PrintReg(CSR, TRI) << '\n');
658 continue;
659 }
Jakob Stoklund Olesen6bfba2e2011-04-20 18:19:48 +0000660
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000661 if (!canEvictInterference(VirtReg, PhysReg, false, BestCost))
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000662 continue;
663
664 // Best so far.
665 BestPhys = PhysReg;
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000666
Jakob Stoklund Olesen57f1e2c2011-02-25 01:04:22 +0000667 // Stop if the hint can be used.
668 if (Order.isHint(PhysReg))
669 break;
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000670 }
671
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000672 if (!BestPhys)
673 return 0;
674
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000675 evictInterference(VirtReg, BestPhys, NewVRegs);
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000676 return BestPhys;
Andrew Trickb853e6c2010-12-09 18:15:21 +0000677}
678
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +0000679
680//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000681// Region Splitting
682//===----------------------------------------------------------------------===//
683
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +0000684/// addSplitConstraints - Fill out the SplitConstraints vector based on the
685/// interference pattern in Physreg and its aliases. Add the constraints to
686/// SpillPlacement and return the static cost of this split in Cost, assuming
687/// that all preferences in SplitConstraints are met.
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000688/// Return false if there are no bundles with positive bias.
689bool RAGreedy::addSplitConstraints(InterferenceCache::Cursor Intf,
690 float &Cost) {
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000691 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
Jakob Stoklund Oleseneda0fe82011-04-02 06:03:38 +0000692
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000693 // Reset interference dependent info.
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000694 SplitConstraints.resize(UseBlocks.size());
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000695 float StaticCost = 0;
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000696 for (unsigned i = 0; i != UseBlocks.size(); ++i) {
697 const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000698 SpillPlacement::BlockConstraint &BC = SplitConstraints[i];
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000699
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000700 BC.Number = BI.MBB->getNumber();
Jakob Stoklund Oleseneda0fe82011-04-02 06:03:38 +0000701 Intf.moveToBlock(BC.Number);
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000702 BC.Entry = BI.LiveIn ? SpillPlacement::PrefReg : SpillPlacement::DontCare;
703 BC.Exit = BI.LiveOut ? SpillPlacement::PrefReg : SpillPlacement::DontCare;
Jakob Stoklund Olesen5ebca792011-08-02 23:04:06 +0000704 BC.ChangesValue = BI.FirstDef;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000705
Jakob Stoklund Oleseneda0fe82011-04-02 06:03:38 +0000706 if (!Intf.hasInterference())
707 continue;
708
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000709 // Number of spill code instructions to insert.
710 unsigned Ins = 0;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000711
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000712 // Interference for the live-in value.
Jakob Stoklund Oleseneda0fe82011-04-02 06:03:38 +0000713 if (BI.LiveIn) {
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000714 if (Intf.first() <= Indexes->getMBBStartIdx(BC.Number))
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000715 BC.Entry = SpillPlacement::MustSpill, ++Ins;
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000716 else if (Intf.first() < BI.FirstInstr)
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000717 BC.Entry = SpillPlacement::PrefSpill, ++Ins;
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000718 else if (Intf.first() < BI.LastInstr)
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000719 ++Ins;
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000720 }
721
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000722 // Interference for the live-out value.
Jakob Stoklund Oleseneda0fe82011-04-02 06:03:38 +0000723 if (BI.LiveOut) {
Jakob Stoklund Olesen612f7802011-04-05 04:20:29 +0000724 if (Intf.last() >= SA->getLastSplitPoint(BC.Number))
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000725 BC.Exit = SpillPlacement::MustSpill, ++Ins;
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000726 else if (Intf.last() > BI.LastInstr)
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000727 BC.Exit = SpillPlacement::PrefSpill, ++Ins;
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +0000728 else if (Intf.last() > BI.FirstInstr)
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000729 ++Ins;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000730 }
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000731
732 // Accumulate the total frequency of inserted spill code.
733 if (Ins)
734 StaticCost += Ins * SpillPlacer->getBlockFrequency(BC.Number);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000735 }
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000736 Cost = StaticCost;
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000737
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +0000738 // Add constraints for use-blocks. Note that these are the only constraints
739 // that may add a positive bias, it is downhill from here.
740 SpillPlacer->addConstraints(SplitConstraints);
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000741 return SpillPlacer->scanActiveBundles();
742}
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +0000743
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +0000744
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000745/// addThroughConstraints - Add constraints and links to SpillPlacer from the
746/// live-through blocks in Blocks.
747void RAGreedy::addThroughConstraints(InterferenceCache::Cursor Intf,
748 ArrayRef<unsigned> Blocks) {
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +0000749 const unsigned GroupSize = 8;
750 SpillPlacement::BlockConstraint BCS[GroupSize];
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000751 unsigned TBS[GroupSize];
752 unsigned B = 0, T = 0;
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +0000753
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000754 for (unsigned i = 0; i != Blocks.size(); ++i) {
755 unsigned Number = Blocks[i];
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +0000756 Intf.moveToBlock(Number);
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000757
Jakob Stoklund Olesen7b41fbe2011-04-07 17:27:46 +0000758 if (!Intf.hasInterference()) {
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000759 assert(T < GroupSize && "Array overflow");
760 TBS[T] = Number;
761 if (++T == GroupSize) {
Frits van Bommel39b5abf2011-07-18 12:00:32 +0000762 SpillPlacer->addLinks(makeArrayRef(TBS, T));
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000763 T = 0;
764 }
Jakob Stoklund Olesen7b41fbe2011-04-07 17:27:46 +0000765 continue;
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +0000766 }
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000767
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000768 assert(B < GroupSize && "Array overflow");
769 BCS[B].Number = Number;
770
Jakob Stoklund Olesen7b41fbe2011-04-07 17:27:46 +0000771 // Interference for the live-in value.
772 if (Intf.first() <= Indexes->getMBBStartIdx(Number))
773 BCS[B].Entry = SpillPlacement::MustSpill;
774 else
775 BCS[B].Entry = SpillPlacement::PrefSpill;
776
777 // Interference for the live-out value.
778 if (Intf.last() >= SA->getLastSplitPoint(Number))
779 BCS[B].Exit = SpillPlacement::MustSpill;
780 else
781 BCS[B].Exit = SpillPlacement::PrefSpill;
782
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +0000783 if (++B == GroupSize) {
784 ArrayRef<SpillPlacement::BlockConstraint> Array(BCS, B);
785 SpillPlacer->addConstraints(Array);
786 B = 0;
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +0000787 }
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000788 }
789
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +0000790 ArrayRef<SpillPlacement::BlockConstraint> Array(BCS, B);
791 SpillPlacer->addConstraints(Array);
Frits van Bommel39b5abf2011-07-18 12:00:32 +0000792 SpillPlacer->addLinks(makeArrayRef(TBS, T));
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000793}
794
Jakob Stoklund Olesenc66a37d2011-07-14 00:17:10 +0000795void RAGreedy::growRegion(GlobalSplitCandidate &Cand) {
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000796 // Keep track of through blocks that have not been added to SpillPlacer.
797 BitVector Todo = SA->getThroughBlocks();
798 SmallVectorImpl<unsigned> &ActiveBlocks = Cand.ActiveBlocks;
799 unsigned AddedTo = 0;
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000800#ifndef NDEBUG
801 unsigned Visited = 0;
802#endif
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000803
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000804 for (;;) {
805 ArrayRef<unsigned> NewBundles = SpillPlacer->getRecentPositive();
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000806 // Find new through blocks in the periphery of PrefRegBundles.
807 for (int i = 0, e = NewBundles.size(); i != e; ++i) {
808 unsigned Bundle = NewBundles[i];
809 // Look at all blocks connected to Bundle in the full graph.
810 ArrayRef<unsigned> Blocks = Bundles->getBlocks(Bundle);
811 for (ArrayRef<unsigned>::iterator I = Blocks.begin(), E = Blocks.end();
812 I != E; ++I) {
813 unsigned Block = *I;
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000814 if (!Todo.test(Block))
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000815 continue;
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000816 Todo.reset(Block);
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000817 // This is a new through block. Add it to SpillPlacer later.
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000818 ActiveBlocks.push_back(Block);
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000819#ifndef NDEBUG
820 ++Visited;
821#endif
822 }
823 }
824 // Any new blocks to add?
Jakob Stoklund Olesen54901972011-07-05 18:46:42 +0000825 if (ActiveBlocks.size() == AddedTo)
826 break;
Jakob Stoklund Olesenb4666362011-07-23 03:22:33 +0000827
828 // Compute through constraints from the interference, or assume that all
829 // through blocks prefer spilling when forming compact regions.
830 ArrayRef<unsigned> NewBlocks = makeArrayRef(ActiveBlocks).slice(AddedTo);
831 if (Cand.PhysReg)
832 addThroughConstraints(Cand.Intf, NewBlocks);
833 else
Jakob Stoklund Olesenb87f91b2011-08-03 23:09:38 +0000834 // Provide a strong negative bias on through blocks to prevent unwanted
835 // liveness on loop backedges.
836 SpillPlacer->addPrefSpill(NewBlocks, /* Strong= */ true);
Jakob Stoklund Olesen54901972011-07-05 18:46:42 +0000837 AddedTo = ActiveBlocks.size();
838
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000839 // Perhaps iterating can enable more bundles?
840 SpillPlacer->iterate();
841 }
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000842 DEBUG(dbgs() << ", v=" << Visited);
843}
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000844
Jakob Stoklund Olesen87972fa2011-07-23 03:41:57 +0000845/// calcCompactRegion - Compute the set of edge bundles that should be live
846/// when splitting the current live range into compact regions. Compact
847/// regions can be computed without looking at interference. They are the
848/// regions formed by removing all the live-through blocks from the live range.
849///
850/// Returns false if the current live range is already compact, or if the
851/// compact regions would form single block regions anyway.
852bool RAGreedy::calcCompactRegion(GlobalSplitCandidate &Cand) {
853 // Without any through blocks, the live range is already compact.
854 if (!SA->getNumThroughBlocks())
855 return false;
856
857 // Compact regions don't correspond to any physreg.
858 Cand.reset(IntfCache, 0);
859
860 DEBUG(dbgs() << "Compact region bundles");
861
862 // Use the spill placer to determine the live bundles. GrowRegion pretends
863 // that all the through blocks have interference when PhysReg is unset.
864 SpillPlacer->prepare(Cand.LiveBundles);
865
866 // The static split cost will be zero since Cand.Intf reports no interference.
867 float Cost;
868 if (!addSplitConstraints(Cand.Intf, Cost)) {
869 DEBUG(dbgs() << ", none.\n");
870 return false;
871 }
872
873 growRegion(Cand);
874 SpillPlacer->finish();
875
876 if (!Cand.LiveBundles.any()) {
877 DEBUG(dbgs() << ", none.\n");
878 return false;
879 }
880
881 DEBUG({
882 for (int i = Cand.LiveBundles.find_first(); i>=0;
883 i = Cand.LiveBundles.find_next(i))
884 dbgs() << " EB#" << i;
885 dbgs() << ".\n";
886 });
887 return true;
888}
889
Jakob Stoklund Olesen20072982011-04-22 22:47:40 +0000890/// calcSpillCost - Compute how expensive it would be to split the live range in
891/// SA around all use blocks instead of forming bundle regions.
892float RAGreedy::calcSpillCost() {
893 float Cost = 0;
Jakob Stoklund Olesen20072982011-04-22 22:47:40 +0000894 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
895 for (unsigned i = 0; i != UseBlocks.size(); ++i) {
896 const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
897 unsigned Number = BI.MBB->getNumber();
898 // We normally only need one spill instruction - a load or a store.
899 Cost += SpillPlacer->getBlockFrequency(Number);
900
901 // Unless the value is redefined in the block.
Jakob Stoklund Olesen3f5beed2011-08-02 23:04:08 +0000902 if (BI.LiveIn && BI.LiveOut && BI.FirstDef)
903 Cost += SpillPlacer->getBlockFrequency(Number);
Jakob Stoklund Olesen20072982011-04-22 22:47:40 +0000904 }
905 return Cost;
906}
907
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000908/// calcGlobalSplitCost - Return the global split cost of following the split
909/// pattern in LiveBundles. This cost should be added to the local cost of the
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000910/// interference pattern in SplitConstraints.
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000911///
Jakob Stoklund Olesenc66a37d2011-07-14 00:17:10 +0000912float RAGreedy::calcGlobalSplitCost(GlobalSplitCandidate &Cand) {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000913 float GlobalCost = 0;
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000914 const BitVector &LiveBundles = Cand.LiveBundles;
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000915 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
916 for (unsigned i = 0; i != UseBlocks.size(); ++i) {
917 const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000918 SpillPlacement::BlockConstraint &BC = SplitConstraints[i];
Jakob Stoklund Olesen874be742011-03-05 03:28:51 +0000919 bool RegIn = LiveBundles[Bundles->getBundle(BC.Number, 0)];
920 bool RegOut = LiveBundles[Bundles->getBundle(BC.Number, 1)];
921 unsigned Ins = 0;
922
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000923 if (BI.LiveIn)
924 Ins += RegIn != (BC.Entry == SpillPlacement::PrefReg);
925 if (BI.LiveOut)
926 Ins += RegOut != (BC.Exit == SpillPlacement::PrefReg);
Jakob Stoklund Olesen874be742011-03-05 03:28:51 +0000927 if (Ins)
928 GlobalCost += Ins * SpillPlacer->getBlockFrequency(BC.Number);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000929 }
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000930
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000931 for (unsigned i = 0, e = Cand.ActiveBlocks.size(); i != e; ++i) {
932 unsigned Number = Cand.ActiveBlocks[i];
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000933 bool RegIn = LiveBundles[Bundles->getBundle(Number, 0)];
934 bool RegOut = LiveBundles[Bundles->getBundle(Number, 1)];
Jakob Stoklund Olesen9a543522011-04-06 21:32:41 +0000935 if (!RegIn && !RegOut)
936 continue;
937 if (RegIn && RegOut) {
938 // We need double spill code if this block has interference.
Jakob Stoklund Olesenc66a37d2011-07-14 00:17:10 +0000939 Cand.Intf.moveToBlock(Number);
940 if (Cand.Intf.hasInterference())
Jakob Stoklund Olesen9a543522011-04-06 21:32:41 +0000941 GlobalCost += 2*SpillPlacer->getBlockFrequency(Number);
942 continue;
943 }
944 // live-in / stack-out or stack-in live-out.
945 GlobalCost += SpillPlacer->getBlockFrequency(Number);
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000946 }
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000947 return GlobalCost;
948}
949
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +0000950/// splitAroundRegion - Split the current live range around the regions
951/// determined by BundleCand and GlobalCand.
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000952///
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +0000953/// Before calling this function, GlobalCand and BundleCand must be initialized
954/// so each bundle is assigned to a valid candidate, or NoCand for the
955/// stack-bound bundles. The shared SA/SE SplitAnalysis and SplitEditor
956/// objects must be initialized for the current live range, and intervals
957/// created for the used candidates.
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000958///
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +0000959/// @param LREdit The LiveRangeEdit object handling the current split.
960/// @param UsedCands List of used GlobalCand entries. Every BundleCand value
961/// must appear in this list.
962void RAGreedy::splitAroundRegion(LiveRangeEdit &LREdit,
963 ArrayRef<unsigned> UsedCands) {
964 // These are the intervals created for new global ranges. We may create more
965 // intervals for local ranges.
966 const unsigned NumGlobalIntvs = LREdit.size();
967 DEBUG(dbgs() << "splitAroundRegion with " << NumGlobalIntvs << " globals.\n");
968 assert(NumGlobalIntvs && "No global intervals configured");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000969
Jakob Stoklund Olesen2d6d86b2011-08-05 22:20:45 +0000970 // Isolate even single instructions when dealing with a proper sub-class.
Jakob Stoklund Olesen69145ba2011-08-06 18:20:24 +0000971 // That guarantees register class inflation for the stack interval because it
Jakob Stoklund Olesen2d6d86b2011-08-05 22:20:45 +0000972 // is all copies.
973 unsigned Reg = SA->getParent().reg;
974 bool SingleInstrs = RegClassInfo.isProperSubClass(MRI->getRegClass(Reg));
975
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000976 // First handle all the blocks with uses.
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000977 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
978 for (unsigned i = 0; i != UseBlocks.size(); ++i) {
979 const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +0000980 unsigned Number = BI.MBB->getNumber();
981 unsigned IntvIn = 0, IntvOut = 0;
982 SlotIndex IntfIn, IntfOut;
983 if (BI.LiveIn) {
984 unsigned CandIn = BundleCand[Bundles->getBundle(Number, 0)];
985 if (CandIn != NoCand) {
986 GlobalSplitCandidate &Cand = GlobalCand[CandIn];
987 IntvIn = Cand.IntvIdx;
988 Cand.Intf.moveToBlock(Number);
989 IntfIn = Cand.Intf.first();
990 }
991 }
992 if (BI.LiveOut) {
993 unsigned CandOut = BundleCand[Bundles->getBundle(Number, 1)];
994 if (CandOut != NoCand) {
995 GlobalSplitCandidate &Cand = GlobalCand[CandOut];
996 IntvOut = Cand.IntvIdx;
997 Cand.Intf.moveToBlock(Number);
998 IntfOut = Cand.Intf.last();
999 }
1000 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001001
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001002 // Create separate intervals for isolated blocks with multiple uses.
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +00001003 if (!IntvIn && !IntvOut) {
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001004 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " isolated.\n");
Jakob Stoklund Olesen2d6d86b2011-08-05 22:20:45 +00001005 if (SA->shouldSplitSingleBlock(BI, SingleInstrs))
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +00001006 SE->splitSingleBlock(BI);
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001007 continue;
1008 }
1009
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +00001010 if (IntvIn && IntvOut)
1011 SE->splitLiveThroughBlock(Number, IntvIn, IntfIn, IntvOut, IntfOut);
1012 else if (IntvIn)
1013 SE->splitRegInBlock(BI, IntvIn, IntfIn);
Jakob Stoklund Olesenb4ddedc2011-07-15 21:47:57 +00001014 else
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +00001015 SE->splitRegOutBlock(BI, IntvOut, IntfOut);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001016 }
1017
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +00001018 // Handle live-through blocks. The relevant live-through blocks are stored in
1019 // the ActiveBlocks list with each candidate. We need to filter out
1020 // duplicates.
1021 BitVector Todo = SA->getThroughBlocks();
1022 for (unsigned c = 0; c != UsedCands.size(); ++c) {
1023 ArrayRef<unsigned> Blocks = GlobalCand[UsedCands[c]].ActiveBlocks;
1024 for (unsigned i = 0, e = Blocks.size(); i != e; ++i) {
1025 unsigned Number = Blocks[i];
1026 if (!Todo.test(Number))
1027 continue;
1028 Todo.reset(Number);
1029
1030 unsigned IntvIn = 0, IntvOut = 0;
1031 SlotIndex IntfIn, IntfOut;
1032
1033 unsigned CandIn = BundleCand[Bundles->getBundle(Number, 0)];
1034 if (CandIn != NoCand) {
1035 GlobalSplitCandidate &Cand = GlobalCand[CandIn];
1036 IntvIn = Cand.IntvIdx;
1037 Cand.Intf.moveToBlock(Number);
1038 IntfIn = Cand.Intf.first();
1039 }
1040
1041 unsigned CandOut = BundleCand[Bundles->getBundle(Number, 1)];
1042 if (CandOut != NoCand) {
1043 GlobalSplitCandidate &Cand = GlobalCand[CandOut];
1044 IntvOut = Cand.IntvIdx;
1045 Cand.Intf.moveToBlock(Number);
1046 IntfOut = Cand.Intf.last();
1047 }
1048 if (!IntvIn && !IntvOut)
1049 continue;
1050 SE->splitLiveThroughBlock(Number, IntvIn, IntfIn, IntvOut, IntfOut);
1051 }
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +00001052 }
1053
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +00001054 ++NumGlobalSplits;
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001055
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001056 SmallVector<unsigned, 8> IntvMap;
1057 SE->finish(&IntvMap);
Jakob Stoklund Olesen1f880422011-08-05 23:10:40 +00001058 DebugVars->splitRegister(Reg, LREdit.regs());
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +00001059
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +00001060 ExtraRegInfo.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesenb2abfa02011-05-28 02:32:57 +00001061 unsigned OrigBlocks = SA->getNumLiveBlocks();
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001062
1063 // Sort out the new intervals created by splitting. We get four kinds:
1064 // - Remainder intervals should not be split again.
1065 // - Candidate intervals can be assigned to Cand.PhysReg.
1066 // - Block-local splits are candidates for local splitting.
1067 // - DCE leftovers should go back on the queue.
1068 for (unsigned i = 0, e = LREdit.size(); i != e; ++i) {
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +00001069 LiveInterval &Reg = *LREdit.get(i);
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001070
1071 // Ignore old intervals from DCE.
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +00001072 if (getStage(Reg) != RS_New)
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001073 continue;
1074
1075 // Remainder interval. Don't try splitting again, spill if it doesn't
1076 // allocate.
1077 if (IntvMap[i] == 0) {
Jakob Stoklund Olesenfa89a032011-07-25 15:25:41 +00001078 setStage(Reg, RS_Spill);
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001079 continue;
1080 }
1081
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +00001082 // Global intervals. Allow repeated splitting as long as the number of live
1083 // blocks is strictly decreasing.
1084 if (IntvMap[i] < NumGlobalIntvs) {
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +00001085 if (SA->countLiveBlocks(&Reg) >= OrigBlocks) {
Jakob Stoklund Olesen9f4b8932011-04-26 22:33:12 +00001086 DEBUG(dbgs() << "Main interval covers the same " << OrigBlocks
1087 << " blocks as original.\n");
1088 // Don't allow repeated splitting as a safe guard against looping.
Jakob Stoklund Olesen49743b12011-07-25 15:25:43 +00001089 setStage(Reg, RS_Split2);
Jakob Stoklund Olesen9f4b8932011-04-26 22:33:12 +00001090 }
1091 continue;
1092 }
1093
1094 // Other intervals are treated as new. This includes local intervals created
1095 // for blocks with multiple uses, and anything created by DCE.
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001096 }
1097
Jakob Stoklund Oleseneb291572011-03-27 22:49:21 +00001098 if (VerifyEnabled)
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001099 MF->verify(this, "After splitting live range around region");
1100}
1101
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001102unsigned RAGreedy::tryRegionSplit(LiveInterval &VirtReg, AllocationOrder &Order,
1103 SmallVectorImpl<LiveInterval*> &NewVRegs) {
Jakob Stoklund Olesenc66a37d2011-07-14 00:17:10 +00001104 unsigned NumCands = 0;
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +00001105 unsigned BestCand = NoCand;
1106 float BestCost;
1107 SmallVector<unsigned, 8> UsedCands;
1108
1109 // Check if we can split this live range around a compact region.
Jakob Stoklund Olesena16a25d2011-09-12 16:54:42 +00001110 bool HasCompact = calcCompactRegion(GlobalCand.front());
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +00001111 if (HasCompact) {
1112 // Yes, keep GlobalCand[0] as the compact region candidate.
1113 NumCands = 1;
1114 BestCost = HUGE_VALF;
1115 } else {
1116 // No benefit from the compact region, our fallback will be per-block
1117 // splitting. Make sure we find a solution that is cheaper than spilling.
1118 BestCost = Hysteresis * calcSpillCost();
1119 DEBUG(dbgs() << "Cost of isolating all blocks = " << BestCost << '\n');
1120 }
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +00001121
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001122 Order.rewind();
Jakob Stoklund Olesenc66a37d2011-07-14 00:17:10 +00001123 while (unsigned PhysReg = Order.next()) {
Jakob Stoklund Olesenf1c70982011-07-14 05:35:11 +00001124 // Discard bad candidates before we run out of interference cache cursors.
1125 // This will only affect register classes with a lot of registers (>32).
1126 if (NumCands == IntfCache.getMaxCursors()) {
1127 unsigned WorstCount = ~0u;
1128 unsigned Worst = 0;
1129 for (unsigned i = 0; i != NumCands; ++i) {
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +00001130 if (i == BestCand || !GlobalCand[i].PhysReg)
Jakob Stoklund Olesenf1c70982011-07-14 05:35:11 +00001131 continue;
1132 unsigned Count = GlobalCand[i].LiveBundles.count();
1133 if (Count < WorstCount)
1134 Worst = i, WorstCount = Count;
1135 }
1136 --NumCands;
1137 GlobalCand[Worst] = GlobalCand[NumCands];
Jakob Stoklund Olesen7bdf0062011-11-01 00:02:31 +00001138 if (BestCand == NumCands)
1139 BestCand = Worst;
Jakob Stoklund Olesenf1c70982011-07-14 05:35:11 +00001140 }
1141
Jakob Stoklund Olesenc66a37d2011-07-14 00:17:10 +00001142 if (GlobalCand.size() <= NumCands)
1143 GlobalCand.resize(NumCands+1);
1144 GlobalSplitCandidate &Cand = GlobalCand[NumCands];
1145 Cand.reset(IntfCache, PhysReg);
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +00001146
Jakob Stoklund Olesenc66a37d2011-07-14 00:17:10 +00001147 SpillPlacer->prepare(Cand.LiveBundles);
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +00001148 float Cost;
Jakob Stoklund Olesenc66a37d2011-07-14 00:17:10 +00001149 if (!addSplitConstraints(Cand.Intf, Cost)) {
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +00001150 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << "\tno positive bundles\n");
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +00001151 continue;
1152 }
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +00001153 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << "\tstatic = " << Cost);
Jakob Stoklund Olesen20072982011-04-22 22:47:40 +00001154 if (Cost >= BestCost) {
1155 DEBUG({
1156 if (BestCand == NoCand)
1157 dbgs() << " worse than no bundles\n";
1158 else
1159 dbgs() << " worse than "
1160 << PrintReg(GlobalCand[BestCand].PhysReg, TRI) << '\n';
1161 });
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001162 continue;
Jakob Stoklund Olesen874be742011-03-05 03:28:51 +00001163 }
Jakob Stoklund Olesenc66a37d2011-07-14 00:17:10 +00001164 growRegion(Cand);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001165
Jakob Stoklund Olesen9efa2a22011-04-06 19:13:57 +00001166 SpillPlacer->finish();
1167
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001168 // No live bundles, defer to splitSingleBlocks().
Jakob Stoklund Olesenc66a37d2011-07-14 00:17:10 +00001169 if (!Cand.LiveBundles.any()) {
Jakob Stoklund Olesen874be742011-03-05 03:28:51 +00001170 DEBUG(dbgs() << " no bundles.\n");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001171 continue;
Jakob Stoklund Olesen874be742011-03-05 03:28:51 +00001172 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001173
Jakob Stoklund Olesenc66a37d2011-07-14 00:17:10 +00001174 Cost += calcGlobalSplitCost(Cand);
Jakob Stoklund Olesen874be742011-03-05 03:28:51 +00001175 DEBUG({
1176 dbgs() << ", total = " << Cost << " with bundles";
Jakob Stoklund Olesenc66a37d2011-07-14 00:17:10 +00001177 for (int i = Cand.LiveBundles.find_first(); i>=0;
1178 i = Cand.LiveBundles.find_next(i))
Jakob Stoklund Olesen874be742011-03-05 03:28:51 +00001179 dbgs() << " EB#" << i;
1180 dbgs() << ".\n";
1181 });
Jakob Stoklund Olesen20072982011-04-22 22:47:40 +00001182 if (Cost < BestCost) {
Jakob Stoklund Olesenc66a37d2011-07-14 00:17:10 +00001183 BestCand = NumCands;
Jakob Stoklund Olesen20072982011-04-22 22:47:40 +00001184 BestCost = Hysteresis * Cost; // Prevent rounding effects.
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001185 }
Jakob Stoklund Olesenc66a37d2011-07-14 00:17:10 +00001186 ++NumCands;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001187 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001188
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +00001189 // No solutions found, fall back to single block splitting.
1190 if (!HasCompact && BestCand == NoCand)
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001191 return 0;
1192
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +00001193 // Prepare split editor.
Jakob Stoklund Olesen20942dc2012-05-19 05:25:46 +00001194 LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this);
Jakob Stoklund Olesen708d06f2011-09-12 16:49:21 +00001195 SE->reset(LREdit, SplitSpillMode);
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +00001196
1197 // Assign all edge bundles to the preferred candidate, or NoCand.
1198 BundleCand.assign(Bundles->getNumBundles(), NoCand);
1199
1200 // Assign bundles for the best candidate region.
1201 if (BestCand != NoCand) {
1202 GlobalSplitCandidate &Cand = GlobalCand[BestCand];
1203 if (unsigned B = Cand.getBundles(BundleCand, BestCand)) {
1204 UsedCands.push_back(BestCand);
1205 Cand.IntvIdx = SE->openIntv();
1206 DEBUG(dbgs() << "Split for " << PrintReg(Cand.PhysReg, TRI) << " in "
1207 << B << " bundles, intv " << Cand.IntvIdx << ".\n");
Chandler Carruth32668ea2011-08-03 23:07:27 +00001208 (void)B;
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +00001209 }
1210 }
1211
1212 // Assign bundles for the compact region.
1213 if (HasCompact) {
1214 GlobalSplitCandidate &Cand = GlobalCand.front();
1215 assert(!Cand.PhysReg && "Compact region has no physreg");
1216 if (unsigned B = Cand.getBundles(BundleCand, 0)) {
1217 UsedCands.push_back(0);
1218 Cand.IntvIdx = SE->openIntv();
1219 DEBUG(dbgs() << "Split for compact region in " << B << " bundles, intv "
1220 << Cand.IntvIdx << ".\n");
Chandler Carruth32668ea2011-08-03 23:07:27 +00001221 (void)B;
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +00001222 }
1223 }
1224
1225 splitAroundRegion(LREdit, UsedCands);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001226 return 0;
1227}
1228
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001229
1230//===----------------------------------------------------------------------===//
Jakob Stoklund Olesendab35d32011-08-05 23:04:18 +00001231// Per-Block Splitting
1232//===----------------------------------------------------------------------===//
1233
1234/// tryBlockSplit - Split a global live range around every block with uses. This
1235/// creates a lot of local live ranges, that will be split by tryLocalSplit if
1236/// they don't allocate.
1237unsigned RAGreedy::tryBlockSplit(LiveInterval &VirtReg, AllocationOrder &Order,
1238 SmallVectorImpl<LiveInterval*> &NewVRegs) {
1239 assert(&SA->getParent() == &VirtReg && "Live range wasn't analyzed");
1240 unsigned Reg = VirtReg.reg;
1241 bool SingleInstrs = RegClassInfo.isProperSubClass(MRI->getRegClass(Reg));
Jakob Stoklund Olesen20942dc2012-05-19 05:25:46 +00001242 LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this);
Jakob Stoklund Olesen708d06f2011-09-12 16:49:21 +00001243 SE->reset(LREdit, SplitSpillMode);
Jakob Stoklund Olesendab35d32011-08-05 23:04:18 +00001244 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
1245 for (unsigned i = 0; i != UseBlocks.size(); ++i) {
1246 const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
1247 if (SA->shouldSplitSingleBlock(BI, SingleInstrs))
1248 SE->splitSingleBlock(BI);
1249 }
1250 // No blocks were split.
1251 if (LREdit.empty())
1252 return 0;
1253
1254 // We did split for some blocks.
Jakob Stoklund Olesena9c41d32011-08-05 23:50:31 +00001255 SmallVector<unsigned, 8> IntvMap;
1256 SE->finish(&IntvMap);
Jakob Stoklund Olesen1f880422011-08-05 23:10:40 +00001257
1258 // Tell LiveDebugVariables about the new ranges.
1259 DebugVars->splitRegister(Reg, LREdit.regs());
1260
Jakob Stoklund Olesena9c41d32011-08-05 23:50:31 +00001261 ExtraRegInfo.resize(MRI->getNumVirtRegs());
1262
1263 // Sort out the new intervals created by splitting. The remainder interval
1264 // goes straight to spilling, the new local ranges get to stay RS_New.
1265 for (unsigned i = 0, e = LREdit.size(); i != e; ++i) {
1266 LiveInterval &LI = *LREdit.get(i);
1267 if (getStage(LI) == RS_New && IntvMap[i] == 0)
1268 setStage(LI, RS_Spill);
1269 }
1270
Jakob Stoklund Olesendab35d32011-08-05 23:04:18 +00001271 if (VerifyEnabled)
1272 MF->verify(this, "After splitting live range around basic blocks");
1273 return 0;
1274}
1275
Jakob Stoklund Olesend74d2842012-05-23 22:37:27 +00001276
1277//===----------------------------------------------------------------------===//
1278// Per-Instruction Splitting
1279//===----------------------------------------------------------------------===//
1280
1281/// tryInstructionSplit - Split a live range around individual instructions.
1282/// This is normally not worthwhile since the spiller is doing essentially the
1283/// same thing. However, when the live range is in a constrained register
1284/// class, it may help to insert copies such that parts of the live range can
1285/// be moved to a larger register class.
1286///
1287/// This is similar to spilling to a larger register class.
1288unsigned
1289RAGreedy::tryInstructionSplit(LiveInterval &VirtReg, AllocationOrder &Order,
1290 SmallVectorImpl<LiveInterval*> &NewVRegs) {
1291 // There is no point to this if there are no larger sub-classes.
1292 if (!RegClassInfo.isProperSubClass(MRI->getRegClass(VirtReg.reg)))
1293 return 0;
1294
1295 // Always enable split spill mode, since we're effectively spilling to a
1296 // register.
1297 LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this);
1298 SE->reset(LREdit, SplitEditor::SM_Size);
1299
1300 ArrayRef<SlotIndex> Uses = SA->getUseSlots();
1301 if (Uses.size() <= 1)
1302 return 0;
1303
1304 DEBUG(dbgs() << "Split around " << Uses.size() << " individual instrs.\n");
1305
1306 // Split around every non-copy instruction.
1307 for (unsigned i = 0; i != Uses.size(); ++i) {
1308 if (const MachineInstr *MI = Indexes->getInstructionFromIndex(Uses[i]))
1309 if (MI->isFullCopy()) {
1310 DEBUG(dbgs() << " skip:\t" << Uses[i] << '\t' << *MI);
1311 continue;
1312 }
1313 SE->openIntv();
1314 SlotIndex SegStart = SE->enterIntvBefore(Uses[i]);
1315 SlotIndex SegStop = SE->leaveIntvAfter(Uses[i]);
1316 SE->useIntv(SegStart, SegStop);
1317 }
1318
1319 if (LREdit.empty()) {
1320 DEBUG(dbgs() << "All uses were copies.\n");
1321 return 0;
1322 }
1323
1324 SmallVector<unsigned, 8> IntvMap;
1325 SE->finish(&IntvMap);
1326 DebugVars->splitRegister(VirtReg.reg, LREdit.regs());
1327 ExtraRegInfo.resize(MRI->getNumVirtRegs());
1328
1329 // Assign all new registers to RS_Spill. This was the last chance.
1330 setStage(LREdit.begin(), LREdit.end(), RS_Spill);
1331 return 0;
1332}
1333
1334
Jakob Stoklund Olesendab35d32011-08-05 23:04:18 +00001335//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001336// Local Splitting
1337//===----------------------------------------------------------------------===//
1338
1339
1340/// calcGapWeights - Compute the maximum spill weight that needs to be evicted
1341/// in order to use PhysReg between two entries in SA->UseSlots.
1342///
1343/// GapWeight[i] represents the gap between UseSlots[i] and UseSlots[i+1].
1344///
1345void RAGreedy::calcGapWeights(unsigned PhysReg,
1346 SmallVectorImpl<float> &GapWeight) {
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +00001347 assert(SA->getUseBlocks().size() == 1 && "Not a local interval");
1348 const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front();
Jakob Stoklund Olesenb20b5182012-01-12 17:53:44 +00001349 ArrayRef<SlotIndex> Uses = SA->getUseSlots();
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001350 const unsigned NumGaps = Uses.size()-1;
1351
1352 // Start and end points for the interference check.
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001353 SlotIndex StartIdx =
1354 BI.LiveIn ? BI.FirstInstr.getBaseIndex() : BI.FirstInstr;
1355 SlotIndex StopIdx =
1356 BI.LiveOut ? BI.LastInstr.getBoundaryIndex() : BI.LastInstr;
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001357
1358 GapWeight.assign(NumGaps, 0.0f);
1359
1360 // Add interference from each overlapping register.
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +00001361 for (MCRegAliasIterator AI(PhysReg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001362 if (!query(const_cast<LiveInterval&>(SA->getParent()), *AI)
1363 .checkInterference())
1364 continue;
1365
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001366 // We know that VirtReg is a continuous interval from FirstInstr to
1367 // LastInstr, so we don't need InterferenceQuery.
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001368 //
1369 // Interference that overlaps an instruction is counted in both gaps
1370 // surrounding the instruction. The exception is interference before
1371 // StartIdx and after StopIdx.
1372 //
Jakob Stoklund Olesen93841112012-01-11 23:19:08 +00001373 LiveIntervalUnion::SegmentIter IntI = getLiveUnion(*AI).find(StartIdx);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001374 for (unsigned Gap = 0; IntI.valid() && IntI.start() < StopIdx; ++IntI) {
1375 // Skip the gaps before IntI.
1376 while (Uses[Gap+1].getBoundaryIndex() < IntI.start())
1377 if (++Gap == NumGaps)
1378 break;
1379 if (Gap == NumGaps)
1380 break;
1381
1382 // Update the gaps covered by IntI.
1383 const float weight = IntI.value()->weight;
1384 for (; Gap != NumGaps; ++Gap) {
1385 GapWeight[Gap] = std::max(GapWeight[Gap], weight);
1386 if (Uses[Gap+1].getBaseIndex() >= IntI.stop())
1387 break;
1388 }
1389 if (Gap == NumGaps)
1390 break;
1391 }
1392 }
1393}
1394
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001395/// tryLocalSplit - Try to split VirtReg into smaller intervals inside its only
1396/// basic block.
1397///
1398unsigned RAGreedy::tryLocalSplit(LiveInterval &VirtReg, AllocationOrder &Order,
1399 SmallVectorImpl<LiveInterval*> &NewVRegs) {
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +00001400 assert(SA->getUseBlocks().size() == 1 && "Not a local interval");
1401 const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front();
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001402
1403 // Note that it is possible to have an interval that is live-in or live-out
1404 // while only covering a single block - A phi-def can use undef values from
1405 // predecessors, and the block could be a single-block loop.
1406 // We don't bother doing anything clever about such a case, we simply assume
Jakob Stoklund Olesenfe62d922011-08-02 22:54:14 +00001407 // that the interval is continuous from FirstInstr to LastInstr. We should
1408 // make sure that we don't do anything illegal to such an interval, though.
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001409
Jakob Stoklund Olesenb20b5182012-01-12 17:53:44 +00001410 ArrayRef<SlotIndex> Uses = SA->getUseSlots();
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001411 if (Uses.size() <= 2)
1412 return 0;
1413 const unsigned NumGaps = Uses.size()-1;
1414
1415 DEBUG({
1416 dbgs() << "tryLocalSplit: ";
1417 for (unsigned i = 0, e = Uses.size(); i != e; ++i)
Jakob Stoklund Olesenb20b5182012-01-12 17:53:44 +00001418 dbgs() << ' ' << Uses[i];
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001419 dbgs() << '\n';
1420 });
1421
Jakob Stoklund Olesena6d513f2012-02-11 00:42:18 +00001422 // If VirtReg is live across any register mask operands, compute a list of
1423 // gaps with register masks.
1424 SmallVector<unsigned, 8> RegMaskGaps;
1425 if (!UsableRegs.empty()) {
1426 // Get regmask slots for the whole block.
1427 ArrayRef<SlotIndex> RMS = LIS->getRegMaskSlotsInBlock(BI.MBB->getNumber());
Jakob Stoklund Olesencac5fa32012-02-14 23:51:27 +00001428 DEBUG(dbgs() << RMS.size() << " regmasks in block:");
Jakob Stoklund Olesena6d513f2012-02-11 00:42:18 +00001429 // Constrain to VirtReg's live range.
Jakob Stoklund Olesencac5fa32012-02-14 23:51:27 +00001430 unsigned ri = std::lower_bound(RMS.begin(), RMS.end(),
1431 Uses.front().getRegSlot()) - RMS.begin();
Jakob Stoklund Olesena6d513f2012-02-11 00:42:18 +00001432 unsigned re = RMS.size();
1433 for (unsigned i = 0; i != NumGaps && ri != re; ++i) {
Jakob Stoklund Olesencac5fa32012-02-14 23:51:27 +00001434 // Look for Uses[i] <= RMS <= Uses[i+1].
1435 assert(!SlotIndex::isEarlierInstr(RMS[ri], Uses[i]));
1436 if (SlotIndex::isEarlierInstr(Uses[i+1], RMS[ri]))
Jakob Stoklund Olesena6d513f2012-02-11 00:42:18 +00001437 continue;
Jakob Stoklund Olesencac5fa32012-02-14 23:51:27 +00001438 // Skip a regmask on the same instruction as the last use. It doesn't
1439 // overlap the live range.
1440 if (SlotIndex::isSameInstr(Uses[i+1], RMS[ri]) && i+1 == NumGaps)
1441 break;
1442 DEBUG(dbgs() << ' ' << RMS[ri] << ':' << Uses[i] << '-' << Uses[i+1]);
Jakob Stoklund Olesena6d513f2012-02-11 00:42:18 +00001443 RegMaskGaps.push_back(i);
Jakob Stoklund Olesencac5fa32012-02-14 23:51:27 +00001444 // Advance ri to the next gap. A regmask on one of the uses counts in
1445 // both gaps.
1446 while (ri != re && SlotIndex::isEarlierInstr(RMS[ri], Uses[i+1]))
1447 ++ri;
Jakob Stoklund Olesena6d513f2012-02-11 00:42:18 +00001448 }
Jakob Stoklund Olesencac5fa32012-02-14 23:51:27 +00001449 DEBUG(dbgs() << '\n');
Jakob Stoklund Olesena6d513f2012-02-11 00:42:18 +00001450 }
1451
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001452 // Since we allow local split results to be split again, there is a risk of
1453 // creating infinite loops. It is tempting to require that the new live
1454 // ranges have less instructions than the original. That would guarantee
1455 // convergence, but it is too strict. A live range with 3 instructions can be
1456 // split 2+3 (including the COPY), and we want to allow that.
1457 //
1458 // Instead we use these rules:
1459 //
Jakob Stoklund Olesen49743b12011-07-25 15:25:43 +00001460 // 1. Allow any split for ranges with getStage() < RS_Split2. (Except for the
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001461 // noop split, of course).
Jakob Stoklund Olesen49743b12011-07-25 15:25:43 +00001462 // 2. Require progress be made for ranges with getStage() == RS_Split2. All
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001463 // the new ranges must have fewer instructions than before the split.
Jakob Stoklund Olesen49743b12011-07-25 15:25:43 +00001464 // 3. New ranges with the same number of instructions are marked RS_Split2,
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001465 // smaller ranges are marked RS_New.
1466 //
1467 // These rules allow a 3 -> 2+3 split once, which we need. They also prevent
1468 // excessive splitting and infinite loops.
1469 //
Jakob Stoklund Olesen49743b12011-07-25 15:25:43 +00001470 bool ProgressRequired = getStage(VirtReg) >= RS_Split2;
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001471
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001472 // Best split candidate.
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001473 unsigned BestBefore = NumGaps;
1474 unsigned BestAfter = 0;
1475 float BestDiff = 0;
1476
Jakob Stoklund Olesen40a42a22011-03-04 00:58:40 +00001477 const float blockFreq = SpillPlacer->getBlockFrequency(BI.MBB->getNumber());
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001478 SmallVector<float, 8> GapWeight;
1479
1480 Order.rewind();
1481 while (unsigned PhysReg = Order.next()) {
1482 // Keep track of the largest spill weight that would need to be evicted in
1483 // order to make use of PhysReg between UseSlots[i] and UseSlots[i+1].
1484 calcGapWeights(PhysReg, GapWeight);
1485
Jakob Stoklund Olesena6d513f2012-02-11 00:42:18 +00001486 // Remove any gaps with regmask clobbers.
1487 if (clobberedByRegMask(PhysReg))
1488 for (unsigned i = 0, e = RegMaskGaps.size(); i != e; ++i)
1489 GapWeight[RegMaskGaps[i]] = HUGE_VALF;
1490
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001491 // Try to find the best sequence of gaps to close.
1492 // The new spill weight must be larger than any gap interference.
1493
1494 // We will split before Uses[SplitBefore] and after Uses[SplitAfter].
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001495 unsigned SplitBefore = 0, SplitAfter = 1;
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001496
1497 // MaxGap should always be max(GapWeight[SplitBefore..SplitAfter-1]).
1498 // It is the spill weight that needs to be evicted.
1499 float MaxGap = GapWeight[0];
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001500
1501 for (;;) {
1502 // Live before/after split?
1503 const bool LiveBefore = SplitBefore != 0 || BI.LiveIn;
1504 const bool LiveAfter = SplitAfter != NumGaps || BI.LiveOut;
1505
1506 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << ' '
1507 << Uses[SplitBefore] << '-' << Uses[SplitAfter]
1508 << " i=" << MaxGap);
1509
1510 // Stop before the interval gets so big we wouldn't be making progress.
1511 if (!LiveBefore && !LiveAfter) {
1512 DEBUG(dbgs() << " all\n");
1513 break;
1514 }
1515 // Should the interval be extended or shrunk?
1516 bool Shrink = true;
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001517
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001518 // How many gaps would the new range have?
1519 unsigned NewGaps = LiveBefore + SplitAfter - SplitBefore + LiveAfter;
1520
1521 // Legally, without causing looping?
1522 bool Legal = !ProgressRequired || NewGaps < NumGaps;
1523
1524 if (Legal && MaxGap < HUGE_VALF) {
1525 // Estimate the new spill weight. Each instruction reads or writes the
1526 // register. Conservatively assume there are no read-modify-write
1527 // instructions.
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001528 //
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001529 // Try to guess the size of the new interval.
1530 const float EstWeight = normalizeSpillWeight(blockFreq * (NewGaps + 1),
1531 Uses[SplitBefore].distance(Uses[SplitAfter]) +
1532 (LiveBefore + LiveAfter)*SlotIndex::InstrDist);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001533 // Would this split be possible to allocate?
1534 // Never allocate all gaps, we wouldn't be making progress.
Jakob Stoklund Olesen66446c82011-04-30 05:07:46 +00001535 DEBUG(dbgs() << " w=" << EstWeight);
1536 if (EstWeight * Hysteresis >= MaxGap) {
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001537 Shrink = false;
Jakob Stoklund Olesen66446c82011-04-30 05:07:46 +00001538 float Diff = EstWeight - MaxGap;
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001539 if (Diff > BestDiff) {
1540 DEBUG(dbgs() << " (best)");
Jakob Stoklund Olesen66446c82011-04-30 05:07:46 +00001541 BestDiff = Hysteresis * Diff;
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001542 BestBefore = SplitBefore;
1543 BestAfter = SplitAfter;
1544 }
1545 }
1546 }
1547
1548 // Try to shrink.
1549 if (Shrink) {
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001550 if (++SplitBefore < SplitAfter) {
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001551 DEBUG(dbgs() << " shrink\n");
1552 // Recompute the max when necessary.
1553 if (GapWeight[SplitBefore - 1] >= MaxGap) {
1554 MaxGap = GapWeight[SplitBefore];
1555 for (unsigned i = SplitBefore + 1; i != SplitAfter; ++i)
1556 MaxGap = std::max(MaxGap, GapWeight[i]);
1557 }
1558 continue;
1559 }
1560 MaxGap = 0;
1561 }
1562
1563 // Try to extend the interval.
1564 if (SplitAfter >= NumGaps) {
1565 DEBUG(dbgs() << " end\n");
1566 break;
1567 }
1568
1569 DEBUG(dbgs() << " extend\n");
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001570 MaxGap = std::max(MaxGap, GapWeight[SplitAfter++]);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001571 }
1572 }
1573
1574 // Didn't find any candidates?
1575 if (BestBefore == NumGaps)
1576 return 0;
1577
1578 DEBUG(dbgs() << "Best local split range: " << Uses[BestBefore]
1579 << '-' << Uses[BestAfter] << ", " << BestDiff
1580 << ", " << (BestAfter - BestBefore + 1) << " instrs\n");
1581
Jakob Stoklund Olesen20942dc2012-05-19 05:25:46 +00001582 LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this);
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +00001583 SE->reset(LREdit);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001584
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +00001585 SE->openIntv();
1586 SlotIndex SegStart = SE->enterIntvBefore(Uses[BestBefore]);
1587 SlotIndex SegStop = SE->leaveIntvAfter(Uses[BestAfter]);
1588 SE->useIntv(SegStart, SegStop);
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001589 SmallVector<unsigned, 8> IntvMap;
1590 SE->finish(&IntvMap);
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +00001591 DebugVars->splitRegister(VirtReg.reg, LREdit.regs());
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001592
1593 // If the new range has the same number of instructions as before, mark it as
Jakob Stoklund Olesen49743b12011-07-25 15:25:43 +00001594 // RS_Split2 so the next split will be forced to make progress. Otherwise,
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001595 // leave the new intervals as RS_New so they can compete.
1596 bool LiveBefore = BestBefore != 0 || BI.LiveIn;
1597 bool LiveAfter = BestAfter != NumGaps || BI.LiveOut;
1598 unsigned NewGaps = LiveBefore + BestAfter - BestBefore + LiveAfter;
1599 if (NewGaps >= NumGaps) {
1600 DEBUG(dbgs() << "Tagging non-progress ranges: ");
1601 assert(!ProgressRequired && "Didn't make progress when it was required.");
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001602 for (unsigned i = 0, e = IntvMap.size(); i != e; ++i)
1603 if (IntvMap[i] == 1) {
Jakob Stoklund Olesen49743b12011-07-25 15:25:43 +00001604 setStage(*LREdit.get(i), RS_Split2);
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001605 DEBUG(dbgs() << PrintReg(LREdit.get(i)->reg));
1606 }
1607 DEBUG(dbgs() << '\n');
1608 }
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +00001609 ++NumLocalSplits;
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001610
1611 return 0;
1612}
1613
1614//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001615// Live Range Splitting
1616//===----------------------------------------------------------------------===//
1617
1618/// trySplit - Try to split VirtReg or one of its interferences, making it
1619/// assignable.
1620/// @return Physreg when VirtReg may be assigned and/or new NewVRegs.
1621unsigned RAGreedy::trySplit(LiveInterval &VirtReg, AllocationOrder &Order,
1622 SmallVectorImpl<LiveInterval*>&NewVRegs) {
Jakob Stoklund Olesenccfa4462011-08-05 23:50:33 +00001623 // Ranges must be Split2 or less.
1624 if (getStage(VirtReg) >= RS_Spill)
1625 return 0;
1626
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001627 // Local intervals are handled separately.
Jakob Stoklund Olesena2ebf602011-02-19 00:38:40 +00001628 if (LIS->intervalIsInOneMBB(VirtReg)) {
1629 NamedRegionTimer T("Local Splitting", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001630 SA->analyze(&VirtReg);
Jakob Stoklund Olesend74d2842012-05-23 22:37:27 +00001631 unsigned PhysReg = tryLocalSplit(VirtReg, Order, NewVRegs);
1632 if (PhysReg || !NewVRegs.empty())
1633 return PhysReg;
1634 return tryInstructionSplit(VirtReg, Order, NewVRegs);
Jakob Stoklund Olesena2ebf602011-02-19 00:38:40 +00001635 }
1636
1637 NamedRegionTimer T("Global Splitting", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001638
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001639 SA->analyze(&VirtReg);
1640
Jakob Stoklund Olesen7d6b6a02011-05-03 20:42:13 +00001641 // FIXME: SplitAnalysis may repair broken live ranges coming from the
1642 // coalescer. That may cause the range to become allocatable which means that
1643 // tryRegionSplit won't be making progress. This check should be replaced with
1644 // an assertion when the coalescer is fixed.
1645 if (SA->didRepairRange()) {
1646 // VirtReg has changed, so all cached queries are invalid.
Jakob Stoklund Olesenbdda37d2011-05-10 17:37:41 +00001647 invalidateVirtRegs();
Jakob Stoklund Olesen7d6b6a02011-05-03 20:42:13 +00001648 if (unsigned PhysReg = tryAssign(VirtReg, Order, NewVRegs))
1649 return PhysReg;
1650 }
1651
Jakob Stoklund Olesen49743b12011-07-25 15:25:43 +00001652 // First try to split around a region spanning multiple blocks. RS_Split2
1653 // ranges already made dubious progress with region splitting, so they go
1654 // straight to single block splitting.
1655 if (getStage(VirtReg) < RS_Split2) {
1656 unsigned PhysReg = tryRegionSplit(VirtReg, Order, NewVRegs);
1657 if (PhysReg || !NewVRegs.empty())
1658 return PhysReg;
1659 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001660
Jakob Stoklund Olesendab35d32011-08-05 23:04:18 +00001661 // Then isolate blocks.
1662 return tryBlockSplit(VirtReg, Order, NewVRegs);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001663}
1664
1665
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001666//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001667// Main Entry Point
1668//===----------------------------------------------------------------------===//
1669
1670unsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg,
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001671 SmallVectorImpl<LiveInterval*> &NewVRegs) {
Jakob Stoklund Olesene7c2c152012-02-09 18:25:05 +00001672 // Check if VirtReg is live across any calls.
1673 UsableRegs.clear();
1674 if (LIS->checkRegMaskInterference(VirtReg, UsableRegs))
1675 DEBUG(dbgs() << "Live across regmasks.\n");
1676
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001677 // First try assigning a free register.
Jakob Stoklund Olesen5f2316a2011-06-03 20:34:53 +00001678 AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo);
Jakob Stoklund Olesen6bfba2e2011-04-20 18:19:48 +00001679 if (unsigned PhysReg = tryAssign(VirtReg, Order, NewVRegs))
1680 return PhysReg;
Andrew Trickb853e6c2010-12-09 18:15:21 +00001681
Jakob Stoklund Olesenb8d936b2011-05-25 23:58:36 +00001682 LiveRangeStage Stage = getStage(VirtReg);
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +00001683 DEBUG(dbgs() << StageName[Stage]
1684 << " Cascade " << ExtraRegInfo[VirtReg.reg].Cascade << '\n');
Jakob Stoklund Olesenb8d936b2011-05-25 23:58:36 +00001685
Jakob Stoklund Olesen76395c92011-06-01 18:45:02 +00001686 // Try to evict a less worthy live range, but only for ranges from the primary
Jakob Stoklund Olesenfa89a032011-07-25 15:25:41 +00001687 // queue. The RS_Split ranges already failed to do this, and they should not
Jakob Stoklund Olesen76395c92011-06-01 18:45:02 +00001688 // get a second chance until they have been split.
Jakob Stoklund Olesenfa89a032011-07-25 15:25:41 +00001689 if (Stage != RS_Split)
Jakob Stoklund Olesen76395c92011-06-01 18:45:02 +00001690 if (unsigned PhysReg = tryEvict(VirtReg, Order, NewVRegs))
1691 return PhysReg;
Andrew Trickb853e6c2010-12-09 18:15:21 +00001692
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001693 assert(NewVRegs.empty() && "Cannot append to existing NewVRegs");
1694
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +00001695 // The first time we see a live range, don't try to split or spill.
1696 // Wait until the second time, when all smaller ranges have been allocated.
1697 // This gives a better picture of the interference to split around.
Jakob Stoklund Olesenfa89a032011-07-25 15:25:41 +00001698 if (Stage < RS_Split) {
1699 setStage(VirtReg, RS_Split);
Jakob Stoklund Olesenc1655e12011-03-19 23:02:47 +00001700 DEBUG(dbgs() << "wait for second round\n");
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +00001701 NewVRegs.push_back(&VirtReg);
1702 return 0;
1703 }
1704
Jakob Stoklund Olesenbf4e10f2011-05-06 21:58:30 +00001705 // If we couldn't allocate a register from spilling, there is probably some
1706 // invalid inline assembly. The base class wil report it.
Jakob Stoklund Olesenfa89a032011-07-25 15:25:41 +00001707 if (Stage >= RS_Done || !VirtReg.isSpillable())
Jakob Stoklund Olesenbf4e10f2011-05-06 21:58:30 +00001708 return ~0u;
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001709
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +00001710 // Try splitting VirtReg or interferences.
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001711 unsigned PhysReg = trySplit(VirtReg, Order, NewVRegs);
1712 if (PhysReg || !NewVRegs.empty())
Jakob Stoklund Olesenb64d92e2010-12-14 00:37:44 +00001713 return PhysReg;
1714
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001715 // Finally spill VirtReg itself.
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +00001716 NamedRegionTimer T("Spiller", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesen20942dc2012-05-19 05:25:46 +00001717 LiveRangeEdit LRE(&VirtReg, NewVRegs, *MF, *LIS, VRM, this);
Jakob Stoklund Olesen47dbf6c2011-03-10 01:51:42 +00001718 spiller().spill(LRE);
Jakob Stoklund Olesenfa89a032011-07-25 15:25:41 +00001719 setStage(NewVRegs.begin(), NewVRegs.end(), RS_Done);
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001720
Jakob Stoklund Olesenc46570d2011-03-16 22:56:08 +00001721 if (VerifyEnabled)
1722 MF->verify(this, "After spilling");
1723
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001724 // The live virtual register requesting allocation was spilled, so tell
1725 // the caller not to allocate anything during this round.
1726 return 0;
1727}
1728
1729bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
1730 DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n"
1731 << "********** Function: "
1732 << ((Value*)mf.getFunction())->getName() << '\n');
1733
1734 MF = &mf;
Jakob Stoklund Olesenaf249642010-12-17 23:16:35 +00001735 if (VerifyEnabled)
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +00001736 MF->verify(this, "Before greedy register allocator");
Jakob Stoklund Olesenaf249642010-12-17 23:16:35 +00001737
Jakob Stoklund Olesen4680dec2010-12-10 23:49:00 +00001738 RegAllocBase::init(getAnalysis<VirtRegMap>(), getAnalysis<LiveIntervals>());
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001739 Indexes = &getAnalysis<SlotIndexes>();
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +00001740 DomTree = &getAnalysis<MachineDominatorTree>();
Jakob Stoklund Olesenf6dff842010-12-10 22:54:44 +00001741 SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM));
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +00001742 Loops = &getAnalysis<MachineLoopInfo>();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001743 Bundles = &getAnalysis<EdgeBundles>();
1744 SpillPlacer = &getAnalysis<SpillPlacement>();
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +00001745 DebugVars = &getAnalysis<LiveDebugVariables>();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001746
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +00001747 SA.reset(new SplitAnalysis(*VRM, *LIS, *Loops));
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +00001748 SE.reset(new SplitEditor(*SA, *LIS, *VRM, *DomTree));
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +00001749 ExtraRegInfo.clear();
1750 ExtraRegInfo.resize(MRI->getNumVirtRegs());
1751 NextCascade = 1;
Jakob Stoklund Olesen6ef7da02012-02-10 18:58:34 +00001752 IntfCache.init(MF, &getLiveUnion(0), Indexes, LIS, TRI);
Jakob Stoklund Olesen00005782011-07-26 23:41:46 +00001753 GlobalCand.resize(32); // This will grow as needed.
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +00001754
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001755 allocatePhysRegs();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001756 releaseMemory();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001757 return true;
1758}