blob: 3434a92bf35f3f5f9a7a37037475dc6d3002df87 [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 Olesenf428eb62010-12-17 23:16:32 +000019#include "LiveRangeEdit.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000020#include "RegAllocBase.h"
21#include "Spiller.h"
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000022#include "SpillPlacement.h"
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +000023#include "SplitKit.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000024#include "VirtRegMap.h"
Rafael Espindolafdf16ca2011-06-26 21:41:06 +000025#include "RegisterCoalescer.h"
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +000026#include "llvm/ADT/Statistic.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000027#include "llvm/Analysis/AliasAnalysis.h"
28#include "llvm/Function.h"
29#include "llvm/PassAnalysisSupport.h"
30#include "llvm/CodeGen/CalcSpillWeights.h"
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000031#include "llvm/CodeGen/EdgeBundles.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000032#include "llvm/CodeGen/LiveIntervalAnalysis.h"
33#include "llvm/CodeGen/LiveStackAnalysis.h"
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +000034#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000035#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000036#include "llvm/CodeGen/MachineLoopInfo.h"
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +000037#include "llvm/CodeGen/MachineLoopRanges.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000038#include "llvm/CodeGen/MachineRegisterInfo.h"
39#include "llvm/CodeGen/Passes.h"
40#include "llvm/CodeGen/RegAllocRegistry.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000041#include "llvm/Target/TargetOptions.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000042#include "llvm/Support/Debug.h"
43#include "llvm/Support/ErrorHandling.h"
44#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +000045#include "llvm/Support/Timer.h"
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000046
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000047#include <queue>
48
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000049using namespace llvm;
50
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +000051STATISTIC(NumGlobalSplits, "Number of split global live ranges");
52STATISTIC(NumLocalSplits, "Number of split local live ranges");
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +000053STATISTIC(NumEvicted, "Number of interferences evicted");
54
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000055static RegisterRegAlloc greedyRegAlloc("greedy", "greedy register allocator",
56 createGreedyRegisterAllocator);
57
58namespace {
Jakob Stoklund Olesen92a55f42011-03-09 00:57:29 +000059class RAGreedy : public MachineFunctionPass,
60 public RegAllocBase,
61 private LiveRangeEdit::Delegate {
62
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000063 // context
64 MachineFunction *MF;
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000065
66 // analyses
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000067 SlotIndexes *Indexes;
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000068 LiveStacks *LS;
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +000069 MachineDominatorTree *DomTree;
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +000070 MachineLoopInfo *Loops;
71 MachineLoopRanges *LoopRanges;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +000072 EdgeBundles *Bundles;
73 SpillPlacement *SpillPlacer;
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +000074 LiveDebugVariables *DebugVars;
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +000075
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +000076 // state
77 std::auto_ptr<Spiller> SpillerInstance;
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +000078 std::priority_queue<std::pair<unsigned, unsigned> > Queue;
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +000079 unsigned NextCascade;
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +000080
81 // Live ranges pass through a number of stages as we try to allocate them.
82 // Some of the stages may also create new live ranges:
83 //
84 // - Region splitting.
85 // - Per-block splitting.
86 // - Local splitting.
87 // - Spilling.
88 //
89 // Ranges produced by one of the stages skip the previous stages when they are
90 // dequeued. This improves performance because we can skip interference checks
91 // that are unlikely to give any results. It also guarantees that the live
92 // range splitting algorithm terminates, something that is otherwise hard to
93 // ensure.
94 enum LiveRangeStage {
Jakob Stoklund Olesenf22ca3f2011-03-30 02:52:39 +000095 RS_New, ///< Never seen before.
96 RS_First, ///< First time in the queue.
Jakob Stoklund Olesen76395c92011-06-01 18:45:02 +000097 RS_Second, ///< Second time in the queue.
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +000098 RS_Global, ///< Produced by global splitting.
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +000099 RS_Local, ///< Produced by local splitting.
100 RS_Spill ///< Produced by spilling.
101 };
102
Jakob Stoklund Olesenb8d936b2011-05-25 23:58:36 +0000103 static const char *const StageName[];
104
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000105 // RegInfo - Keep additional information about each live range.
106 struct RegInfo {
107 LiveRangeStage Stage;
108
109 // Cascade - Eviction loop prevention. See canEvictInterference().
110 unsigned Cascade;
111
112 RegInfo() : Stage(RS_New), Cascade(0) {}
113 };
114
115 IndexedMap<RegInfo, VirtReg2IndexFunctor> ExtraRegInfo;
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +0000116
117 LiveRangeStage getStage(const LiveInterval &VirtReg) const {
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000118 return ExtraRegInfo[VirtReg.reg].Stage;
119 }
120
121 void setStage(const LiveInterval &VirtReg, LiveRangeStage Stage) {
122 ExtraRegInfo.resize(MRI->getNumVirtRegs());
123 ExtraRegInfo[VirtReg.reg].Stage = Stage;
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +0000124 }
125
126 template<typename Iterator>
127 void setStage(Iterator Begin, Iterator End, LiveRangeStage NewStage) {
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000128 ExtraRegInfo.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesenf22ca3f2011-03-30 02:52:39 +0000129 for (;Begin != End; ++Begin) {
130 unsigned Reg = (*Begin)->reg;
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000131 if (ExtraRegInfo[Reg].Stage == RS_New)
132 ExtraRegInfo[Reg].Stage = NewStage;
Jakob Stoklund Olesenf22ca3f2011-03-30 02:52:39 +0000133 }
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +0000134 }
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000135
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000136 // splitting state.
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +0000137 std::auto_ptr<SplitAnalysis> SA;
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000138 std::auto_ptr<SplitEditor> SE;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000139
Jakob Stoklund Oleseneda0fe82011-04-02 06:03:38 +0000140 /// Cached per-block interference maps
141 InterferenceCache IntfCache;
142
Jakob Stoklund Olesen7b41fbe2011-04-07 17:27:46 +0000143 /// All basic blocks where the current register has uses.
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000144 SmallVector<SpillPlacement::BlockConstraint, 8> SplitConstraints;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000145
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000146 /// Global live range splitting candidate info.
147 struct GlobalSplitCandidate {
148 unsigned PhysReg;
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000149 BitVector LiveBundles;
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000150 SmallVector<unsigned, 8> ActiveBlocks;
151
152 void reset(unsigned Reg) {
153 PhysReg = Reg;
154 LiveBundles.clear();
155 ActiveBlocks.clear();
156 }
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000157 };
158
159 /// Candidate info for for each PhysReg in AllocationOrder.
160 /// This vector never shrinks, but grows to the size of the largest register
161 /// class.
162 SmallVector<GlobalSplitCandidate, 32> GlobalCand;
163
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000164public:
165 RAGreedy();
166
167 /// Return the pass name.
168 virtual const char* getPassName() const {
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +0000169 return "Greedy Register Allocator";
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000170 }
171
172 /// RAGreedy analysis usage.
173 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000174 virtual void releaseMemory();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000175 virtual Spiller &spiller() { return *SpillerInstance; }
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +0000176 virtual void enqueue(LiveInterval *LI);
177 virtual LiveInterval *dequeue();
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000178 virtual unsigned selectOrSplit(LiveInterval&,
179 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000180
181 /// Perform register allocation.
182 virtual bool runOnMachineFunction(MachineFunction &mf);
183
184 static char ID;
Andrew Trickb853e6c2010-12-09 18:15:21 +0000185
186private:
Jakob Stoklund Olesen92a55f42011-03-09 00:57:29 +0000187 void LRE_WillEraseInstruction(MachineInstr*);
Jakob Stoklund Olesen7792e982011-03-13 01:23:11 +0000188 bool LRE_CanEraseVirtReg(unsigned);
Jakob Stoklund Olesen1d5b8452011-03-16 22:56:16 +0000189 void LRE_WillShrinkVirtReg(unsigned);
Jakob Stoklund Olesenf22ca3f2011-03-30 02:52:39 +0000190 void LRE_DidCloneVirtReg(unsigned, unsigned);
Jakob Stoklund Olesen92a55f42011-03-09 00:57:29 +0000191
Jakob Stoklund Olesen20072982011-04-22 22:47:40 +0000192 float calcSpillCost();
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000193 bool addSplitConstraints(InterferenceCache::Cursor, float&);
194 void addThroughConstraints(InterferenceCache::Cursor, ArrayRef<unsigned>);
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000195 void growRegion(GlobalSplitCandidate &Cand, InterferenceCache::Cursor);
196 float calcGlobalSplitCost(GlobalSplitCandidate&, InterferenceCache::Cursor);
197 void splitAroundRegion(LiveInterval&, GlobalSplitCandidate&,
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000198 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +0000199 void calcGapWeights(unsigned, SmallVectorImpl<float>&);
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000200 bool canEvict(LiveInterval &A, LiveInterval &B);
Jakob Stoklund Olesen76395c92011-06-01 18:45:02 +0000201 bool canEvictInterference(LiveInterval&, unsigned, float&);
Jakob Stoklund Olesenb64d92e2010-12-14 00:37:44 +0000202
Jakob Stoklund Olesen6bfba2e2011-04-20 18:19:48 +0000203 unsigned tryAssign(LiveInterval&, AllocationOrder&,
204 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000205 unsigned tryEvict(LiveInterval&, AllocationOrder&,
Jakob Stoklund Olesen6bfba2e2011-04-20 18:19:48 +0000206 SmallVectorImpl<LiveInterval*>&, unsigned = ~0u);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000207 unsigned tryRegionSplit(LiveInterval&, AllocationOrder&,
208 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +0000209 unsigned tryLocalSplit(LiveInterval&, AllocationOrder&,
210 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesenb64d92e2010-12-14 00:37:44 +0000211 unsigned trySplit(LiveInterval&, AllocationOrder&,
212 SmallVectorImpl<LiveInterval*>&);
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000213};
214} // end anonymous namespace
215
216char RAGreedy::ID = 0;
217
Jakob Stoklund Olesenb8d936b2011-05-25 23:58:36 +0000218#ifndef NDEBUG
219const char *const RAGreedy::StageName[] = {
220 "RS_New",
221 "RS_First",
222 "RS_Second",
223 "RS_Global",
224 "RS_Local",
225 "RS_Spill"
226};
227#endif
228
Jakob Stoklund Olesen20072982011-04-22 22:47:40 +0000229// Hysteresis to use when comparing floats.
230// This helps stabilize decisions based on float comparisons.
231const float Hysteresis = 0.98f;
232
233
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000234FunctionPass* llvm::createGreedyRegisterAllocator() {
235 return new RAGreedy();
236}
237
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000238RAGreedy::RAGreedy(): MachineFunctionPass(ID) {
Jakob Stoklund Olesencfafc542011-04-05 21:40:37 +0000239 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000240 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000241 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
242 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
243 initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry());
Rafael Espindola5b220212011-06-26 22:34:10 +0000244 initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000245 initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry());
246 initializeLiveStacksPass(*PassRegistry::getPassRegistry());
247 initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
248 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +0000249 initializeMachineLoopRangesPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000250 initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000251 initializeEdgeBundlesPass(*PassRegistry::getPassRegistry());
252 initializeSpillPlacementPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000253}
254
255void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const {
256 AU.setPreservesCFG();
257 AU.addRequired<AliasAnalysis>();
258 AU.addPreserved<AliasAnalysis>();
259 AU.addRequired<LiveIntervals>();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000260 AU.addRequired<SlotIndexes>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000261 AU.addPreserved<SlotIndexes>();
Jakob Stoklund Olesencfafc542011-04-05 21:40:37 +0000262 AU.addRequired<LiveDebugVariables>();
263 AU.addPreserved<LiveDebugVariables>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000264 if (StrongPHIElim)
265 AU.addRequiredID(StrongPHIEliminationID);
266 AU.addRequiredTransitive<RegisterCoalescer>();
267 AU.addRequired<CalculateSpillWeights>();
268 AU.addRequired<LiveStacks>();
269 AU.addPreserved<LiveStacks>();
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +0000270 AU.addRequired<MachineDominatorTree>();
271 AU.addPreserved<MachineDominatorTree>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000272 AU.addRequired<MachineLoopInfo>();
273 AU.addPreserved<MachineLoopInfo>();
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +0000274 AU.addRequired<MachineLoopRanges>();
275 AU.addPreserved<MachineLoopRanges>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000276 AU.addRequired<VirtRegMap>();
277 AU.addPreserved<VirtRegMap>();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000278 AU.addRequired<EdgeBundles>();
279 AU.addRequired<SpillPlacement>();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000280 MachineFunctionPass::getAnalysisUsage(AU);
281}
282
Jakob Stoklund Olesen92a55f42011-03-09 00:57:29 +0000283
284//===----------------------------------------------------------------------===//
285// LiveRangeEdit delegate methods
286//===----------------------------------------------------------------------===//
287
288void RAGreedy::LRE_WillEraseInstruction(MachineInstr *MI) {
289 // LRE itself will remove from SlotIndexes and parent basic block.
290 VRM->RemoveMachineInstrFromMaps(MI);
291}
292
Jakob Stoklund Olesen7792e982011-03-13 01:23:11 +0000293bool RAGreedy::LRE_CanEraseVirtReg(unsigned VirtReg) {
294 if (unsigned PhysReg = VRM->getPhys(VirtReg)) {
295 unassign(LIS->getInterval(VirtReg), PhysReg);
296 return true;
297 }
298 // Unassigned virtreg is probably in the priority queue.
299 // RegAllocBase will erase it after dequeueing.
300 return false;
301}
Jakob Stoklund Olesen92a55f42011-03-09 00:57:29 +0000302
Jakob Stoklund Olesen1d5b8452011-03-16 22:56:16 +0000303void RAGreedy::LRE_WillShrinkVirtReg(unsigned VirtReg) {
304 unsigned PhysReg = VRM->getPhys(VirtReg);
305 if (!PhysReg)
306 return;
307
308 // Register is assigned, put it back on the queue for reassignment.
309 LiveInterval &LI = LIS->getInterval(VirtReg);
310 unassign(LI, PhysReg);
311 enqueue(&LI);
312}
313
Jakob Stoklund Olesenf22ca3f2011-03-30 02:52:39 +0000314void RAGreedy::LRE_DidCloneVirtReg(unsigned New, unsigned Old) {
315 // LRE may clone a virtual register because dead code elimination causes it to
316 // be split into connected components. Ensure that the new register gets the
317 // same stage as the parent.
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000318 ExtraRegInfo.grow(New);
319 ExtraRegInfo[New] = ExtraRegInfo[Old];
Jakob Stoklund Olesenf22ca3f2011-03-30 02:52:39 +0000320}
321
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000322void RAGreedy::releaseMemory() {
323 SpillerInstance.reset(0);
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000324 ExtraRegInfo.clear();
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000325 GlobalCand.clear();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +0000326 RegAllocBase::releaseMemory();
327}
328
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +0000329void RAGreedy::enqueue(LiveInterval *LI) {
330 // Prioritize live ranges by size, assigning larger ranges first.
331 // The queue holds (size, reg) pairs.
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +0000332 const unsigned Size = LI->getSize();
333 const unsigned Reg = LI->reg;
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +0000334 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
335 "Can only enqueue virtual registers");
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +0000336 unsigned Prio;
Jakob Stoklund Olesen90c1d7d2010-12-08 22:57:16 +0000337
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000338 ExtraRegInfo.grow(Reg);
339 if (ExtraRegInfo[Reg].Stage == RS_New)
340 ExtraRegInfo[Reg].Stage = RS_First;
Jakob Stoklund Olesenf22ca3f2011-03-30 02:52:39 +0000341
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000342 if (ExtraRegInfo[Reg].Stage == RS_Second)
Jakob Stoklund Oleseneb291572011-03-27 22:49:21 +0000343 // Unsplit ranges that couldn't be allocated immediately are deferred until
344 // everything else has been allocated. Long ranges are allocated last so
345 // they are split against realistic interference.
346 Prio = (1u << 31) - Size;
347 else {
348 // Everything else is allocated in long->short order. Long ranges that don't
349 // fit should be spilled ASAP so they don't create interference.
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +0000350 Prio = (1u << 31) + Size;
Jakob Stoklund Olesend2a50732011-02-23 00:56:56 +0000351
Jakob Stoklund Oleseneb291572011-03-27 22:49:21 +0000352 // Boost ranges that have a physical register hint.
353 if (TargetRegisterInfo::isPhysicalRegister(VRM->getRegAllocPref(Reg)))
354 Prio |= (1u << 30);
355 }
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +0000356
357 Queue.push(std::make_pair(Prio, Reg));
Jakob Stoklund Olesen90c1d7d2010-12-08 22:57:16 +0000358}
359
Jakob Stoklund Olesen98d96482011-02-22 23:01:52 +0000360LiveInterval *RAGreedy::dequeue() {
361 if (Queue.empty())
362 return 0;
363 LiveInterval *LI = &LIS->getInterval(Queue.top().second);
364 Queue.pop();
365 return LI;
366}
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +0000367
Jakob Stoklund Olesen6bfba2e2011-04-20 18:19:48 +0000368
369//===----------------------------------------------------------------------===//
370// Direct Assignment
371//===----------------------------------------------------------------------===//
372
373/// tryAssign - Try to assign VirtReg to an available register.
374unsigned RAGreedy::tryAssign(LiveInterval &VirtReg,
375 AllocationOrder &Order,
376 SmallVectorImpl<LiveInterval*> &NewVRegs) {
377 Order.rewind();
378 unsigned PhysReg;
379 while ((PhysReg = Order.next()))
380 if (!checkPhysRegInterference(VirtReg, PhysReg))
381 break;
382 if (!PhysReg || Order.isHint(PhysReg))
383 return PhysReg;
384
385 // PhysReg is available. Try to evict interference from a cheaper alternative.
386 unsigned Cost = TRI->getCostPerUse(PhysReg);
387
388 // Most registers have 0 additional cost.
389 if (!Cost)
390 return PhysReg;
391
392 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is available at cost " << Cost
393 << '\n');
394 unsigned CheapReg = tryEvict(VirtReg, Order, NewVRegs, Cost);
395 return CheapReg ? CheapReg : PhysReg;
396}
397
398
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +0000399//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000400// Interference eviction
401//===----------------------------------------------------------------------===//
402
Jakob Stoklund Olesenb8d936b2011-05-25 23:58:36 +0000403/// canEvict - determine if A can evict the assigned live range B. The eviction
404/// policy defined by this function together with the allocation order defined
405/// by enqueue() decides which registers ultimately end up being split and
406/// spilled.
407///
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000408/// Cascade numbers are used to prevent infinite loops if this function is a
409/// cyclic relation.
410bool RAGreedy::canEvict(LiveInterval &A, LiveInterval &B) {
411 return A.weight > B.weight;
Jakob Stoklund Olesenb8d936b2011-05-25 23:58:36 +0000412}
413
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000414/// canEvict - Return true if all interferences between VirtReg and PhysReg can
Jakob Stoklund Olesen3f5bedf2011-04-11 21:47:01 +0000415/// be evicted.
416/// Return false if any interference is heavier than MaxWeight.
417/// On return, set MaxWeight to the maximal spill weight of an interference.
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000418bool RAGreedy::canEvictInterference(LiveInterval &VirtReg, unsigned PhysReg,
Jakob Stoklund Olesen76395c92011-06-01 18:45:02 +0000419 float &MaxWeight) {
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000420 // Find VirtReg's cascade number. This will be unassigned if VirtReg was never
421 // involved in an eviction before. If a cascade number was assigned, deny
422 // evicting anything with the same or a newer cascade number. This prevents
423 // infinite eviction loops.
424 //
425 // This works out so a register without a cascade number is allowed to evict
426 // anything, and it can be evicted by anything.
427 unsigned Cascade = ExtraRegInfo[VirtReg.reg].Cascade;
428 if (!Cascade)
429 Cascade = NextCascade;
430
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000431 float Weight = 0;
432 for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) {
433 LiveIntervalUnion::Query &Q = query(VirtReg, *AliasI);
Jakob Stoklund Olesen3f5bedf2011-04-11 21:47:01 +0000434 // If there is 10 or more interferences, chances are one is heavier.
435 if (Q.collectInterferingVRegs(10, MaxWeight) >= 10)
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000436 return false;
437
Jakob Stoklund Olesen3f5bedf2011-04-11 21:47:01 +0000438 // Check if any interfering live range is heavier than MaxWeight.
439 for (unsigned i = Q.interferingVRegs().size(); i; --i) {
440 LiveInterval *Intf = Q.interferingVRegs()[i - 1];
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000441 if (TargetRegisterInfo::isPhysicalRegister(Intf->reg))
442 return false;
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000443 if (Cascade <= ExtraRegInfo[Intf->reg].Cascade)
444 return false;
Jakob Stoklund Olesen3f5bedf2011-04-11 21:47:01 +0000445 if (Intf->weight >= MaxWeight)
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000446 return false;
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000447 if (!canEvict(VirtReg, *Intf))
Jakob Stoklund Olesend2056e52011-05-31 21:02:44 +0000448 return false;
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000449 Weight = std::max(Weight, Intf->weight);
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000450 }
451 }
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000452 MaxWeight = Weight;
453 return true;
454}
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000455
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000456/// tryEvict - Try to evict all interferences for a physreg.
Jakob Stoklund Olesen76395c92011-06-01 18:45:02 +0000457/// @param VirtReg Currently unassigned virtual register.
458/// @param Order Physregs to try.
459/// @return Physreg to assign VirtReg, or 0.
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000460unsigned RAGreedy::tryEvict(LiveInterval &VirtReg,
461 AllocationOrder &Order,
Jakob Stoklund Olesen6bfba2e2011-04-20 18:19:48 +0000462 SmallVectorImpl<LiveInterval*> &NewVRegs,
463 unsigned CostPerUseLimit) {
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000464 NamedRegionTimer T("Evict", TimerGroupName, TimePassesIsEnabled);
465
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000466 // Keep track of the lightest single interference seen so far.
Jakob Stoklund Olesen76395c92011-06-01 18:45:02 +0000467 float BestWeight = HUGE_VALF;
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000468 unsigned BestPhys = 0;
469
470 Order.rewind();
471 while (unsigned PhysReg = Order.next()) {
Jakob Stoklund Olesen6bfba2e2011-04-20 18:19:48 +0000472 if (TRI->getCostPerUse(PhysReg) >= CostPerUseLimit)
473 continue;
474 // The first use of a register in a function has cost 1.
475 if (CostPerUseLimit == 1 && !MRI->isPhysRegUsed(PhysReg))
476 continue;
477
Jakob Stoklund Olesen3f5bedf2011-04-11 21:47:01 +0000478 float Weight = BestWeight;
Jakob Stoklund Olesen76395c92011-06-01 18:45:02 +0000479 if (!canEvictInterference(VirtReg, PhysReg, Weight))
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000480 continue;
481
482 // This is an eviction candidate.
Jakob Stoklund Olesen3f5bedf2011-04-11 21:47:01 +0000483 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " interference = "
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000484 << Weight << '\n');
485 if (BestPhys && Weight >= BestWeight)
486 continue;
487
488 // Best so far.
489 BestPhys = PhysReg;
490 BestWeight = Weight;
Jakob Stoklund Olesen57f1e2c2011-02-25 01:04:22 +0000491 // Stop if the hint can be used.
492 if (Order.isHint(PhysReg))
493 break;
Jakob Stoklund Olesen27106382011-02-09 01:14:03 +0000494 }
495
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000496 if (!BestPhys)
497 return 0;
498
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000499 // We will evict interference. Make sure that VirtReg has a cascade number,
500 // and assign that cascade number to every evicted register. These live
501 // ranges than then only be evicted by a newer cascade, preventing infinite
502 // loops.
503 unsigned Cascade = ExtraRegInfo[VirtReg.reg].Cascade;
504 if (!Cascade)
505 Cascade = ExtraRegInfo[VirtReg.reg].Cascade = NextCascade++;
506
507 DEBUG(dbgs() << "evicting " << PrintReg(BestPhys, TRI)
508 << " interference: Cascade " << Cascade << '\n');
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000509 for (const unsigned *AliasI = TRI->getOverlaps(BestPhys); *AliasI; ++AliasI) {
510 LiveIntervalUnion::Query &Q = query(VirtReg, *AliasI);
511 assert(Q.seenAllInterferences() && "Didn't check all interfererences.");
512 for (unsigned i = 0, e = Q.interferingVRegs().size(); i != e; ++i) {
513 LiveInterval *Intf = Q.interferingVRegs()[i];
514 unassign(*Intf, VRM->getPhys(Intf->reg));
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +0000515 assert(ExtraRegInfo[Intf->reg].Cascade < Cascade &&
516 "Cannot decrease cascade number, illegal eviction");
517 ExtraRegInfo[Intf->reg].Cascade = Cascade;
Jakob Stoklund Olesen98c81412011-02-23 00:29:52 +0000518 ++NumEvicted;
519 NewVRegs.push_back(Intf);
520 }
521 }
522 return BestPhys;
Andrew Trickb853e6c2010-12-09 18:15:21 +0000523}
524
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +0000525
526//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000527// Region Splitting
528//===----------------------------------------------------------------------===//
529
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +0000530/// addSplitConstraints - Fill out the SplitConstraints vector based on the
531/// interference pattern in Physreg and its aliases. Add the constraints to
532/// SpillPlacement and return the static cost of this split in Cost, assuming
533/// that all preferences in SplitConstraints are met.
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000534/// Return false if there are no bundles with positive bias.
535bool RAGreedy::addSplitConstraints(InterferenceCache::Cursor Intf,
536 float &Cost) {
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000537 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
Jakob Stoklund Oleseneda0fe82011-04-02 06:03:38 +0000538
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000539 // Reset interference dependent info.
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000540 SplitConstraints.resize(UseBlocks.size());
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000541 float StaticCost = 0;
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000542 for (unsigned i = 0; i != UseBlocks.size(); ++i) {
543 const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000544 SpillPlacement::BlockConstraint &BC = SplitConstraints[i];
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000545
Jakob Stoklund Olesenf0ac26c2011-02-09 22:50:26 +0000546 BC.Number = BI.MBB->getNumber();
Jakob Stoklund Oleseneda0fe82011-04-02 06:03:38 +0000547 Intf.moveToBlock(BC.Number);
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000548 BC.Entry = BI.LiveIn ? SpillPlacement::PrefReg : SpillPlacement::DontCare;
549 BC.Exit = BI.LiveOut ? SpillPlacement::PrefReg : SpillPlacement::DontCare;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000550
Jakob Stoklund Oleseneda0fe82011-04-02 06:03:38 +0000551 if (!Intf.hasInterference())
552 continue;
553
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000554 // Number of spill code instructions to insert.
555 unsigned Ins = 0;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000556
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000557 // Interference for the live-in value.
Jakob Stoklund Oleseneda0fe82011-04-02 06:03:38 +0000558 if (BI.LiveIn) {
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000559 if (Intf.first() <= Indexes->getMBBStartIdx(BC.Number))
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000560 BC.Entry = SpillPlacement::MustSpill, ++Ins;
Jakob Stoklund Oleseneda0fe82011-04-02 06:03:38 +0000561 else if (Intf.first() < BI.FirstUse)
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000562 BC.Entry = SpillPlacement::PrefSpill, ++Ins;
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000563 else if (Intf.first() < BI.LastUse)
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000564 ++Ins;
Jakob Stoklund Olesena50c5392011-02-08 23:02:58 +0000565 }
566
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000567 // Interference for the live-out value.
Jakob Stoklund Oleseneda0fe82011-04-02 06:03:38 +0000568 if (BI.LiveOut) {
Jakob Stoklund Olesen612f7802011-04-05 04:20:29 +0000569 if (Intf.last() >= SA->getLastSplitPoint(BC.Number))
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000570 BC.Exit = SpillPlacement::MustSpill, ++Ins;
Jakob Stoklund Oleseneda0fe82011-04-02 06:03:38 +0000571 else if (Intf.last() > BI.LastUse)
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000572 BC.Exit = SpillPlacement::PrefSpill, ++Ins;
Jakob Stoklund Olesena2e79ef2011-05-30 01:33:26 +0000573 else if (Intf.last() > BI.FirstUse)
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000574 ++Ins;
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000575 }
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000576
577 // Accumulate the total frequency of inserted spill code.
578 if (Ins)
579 StaticCost += Ins * SpillPlacer->getBlockFrequency(BC.Number);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000580 }
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000581 Cost = StaticCost;
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000582
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +0000583 // Add constraints for use-blocks. Note that these are the only constraints
584 // that may add a positive bias, it is downhill from here.
585 SpillPlacer->addConstraints(SplitConstraints);
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000586 return SpillPlacer->scanActiveBundles();
587}
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +0000588
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +0000589
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000590/// addThroughConstraints - Add constraints and links to SpillPlacer from the
591/// live-through blocks in Blocks.
592void RAGreedy::addThroughConstraints(InterferenceCache::Cursor Intf,
593 ArrayRef<unsigned> Blocks) {
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +0000594 const unsigned GroupSize = 8;
595 SpillPlacement::BlockConstraint BCS[GroupSize];
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000596 unsigned TBS[GroupSize];
597 unsigned B = 0, T = 0;
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +0000598
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000599 for (unsigned i = 0; i != Blocks.size(); ++i) {
600 unsigned Number = Blocks[i];
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +0000601 Intf.moveToBlock(Number);
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000602
Jakob Stoklund Olesen7b41fbe2011-04-07 17:27:46 +0000603 if (!Intf.hasInterference()) {
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000604 assert(T < GroupSize && "Array overflow");
605 TBS[T] = Number;
606 if (++T == GroupSize) {
607 SpillPlacer->addLinks(ArrayRef<unsigned>(TBS, T));
608 T = 0;
609 }
Jakob Stoklund Olesen7b41fbe2011-04-07 17:27:46 +0000610 continue;
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +0000611 }
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000612
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000613 assert(B < GroupSize && "Array overflow");
614 BCS[B].Number = Number;
615
Jakob Stoklund Olesen7b41fbe2011-04-07 17:27:46 +0000616 // Interference for the live-in value.
617 if (Intf.first() <= Indexes->getMBBStartIdx(Number))
618 BCS[B].Entry = SpillPlacement::MustSpill;
619 else
620 BCS[B].Entry = SpillPlacement::PrefSpill;
621
622 // Interference for the live-out value.
623 if (Intf.last() >= SA->getLastSplitPoint(Number))
624 BCS[B].Exit = SpillPlacement::MustSpill;
625 else
626 BCS[B].Exit = SpillPlacement::PrefSpill;
627
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +0000628 if (++B == GroupSize) {
629 ArrayRef<SpillPlacement::BlockConstraint> Array(BCS, B);
630 SpillPlacer->addConstraints(Array);
631 B = 0;
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +0000632 }
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000633 }
634
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +0000635 ArrayRef<SpillPlacement::BlockConstraint> Array(BCS, B);
636 SpillPlacer->addConstraints(Array);
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000637 SpillPlacer->addLinks(ArrayRef<unsigned>(TBS, T));
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000638}
639
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000640void RAGreedy::growRegion(GlobalSplitCandidate &Cand,
641 InterferenceCache::Cursor Intf) {
642 // Keep track of through blocks that have not been added to SpillPlacer.
643 BitVector Todo = SA->getThroughBlocks();
644 SmallVectorImpl<unsigned> &ActiveBlocks = Cand.ActiveBlocks;
645 unsigned AddedTo = 0;
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000646#ifndef NDEBUG
647 unsigned Visited = 0;
648#endif
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000649
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000650 for (;;) {
651 ArrayRef<unsigned> NewBundles = SpillPlacer->getRecentPositive();
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000652 // Find new through blocks in the periphery of PrefRegBundles.
653 for (int i = 0, e = NewBundles.size(); i != e; ++i) {
654 unsigned Bundle = NewBundles[i];
655 // Look at all blocks connected to Bundle in the full graph.
656 ArrayRef<unsigned> Blocks = Bundles->getBlocks(Bundle);
657 for (ArrayRef<unsigned>::iterator I = Blocks.begin(), E = Blocks.end();
658 I != E; ++I) {
659 unsigned Block = *I;
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000660 if (!Todo.test(Block))
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000661 continue;
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000662 Todo.reset(Block);
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000663 // This is a new through block. Add it to SpillPlacer later.
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000664 ActiveBlocks.push_back(Block);
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000665#ifndef NDEBUG
666 ++Visited;
667#endif
668 }
669 }
670 // Any new blocks to add?
Jakob Stoklund Olesen54901972011-07-05 18:46:42 +0000671 if (ActiveBlocks.size() == AddedTo)
672 break;
673 addThroughConstraints(Intf,
674 ArrayRef<unsigned>(ActiveBlocks).slice(AddedTo));
675 AddedTo = ActiveBlocks.size();
676
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000677 // Perhaps iterating can enable more bundles?
678 SpillPlacer->iterate();
679 }
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +0000680 DEBUG(dbgs() << ", v=" << Visited);
681}
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000682
Jakob Stoklund Olesen20072982011-04-22 22:47:40 +0000683/// calcSpillCost - Compute how expensive it would be to split the live range in
684/// SA around all use blocks instead of forming bundle regions.
685float RAGreedy::calcSpillCost() {
686 float Cost = 0;
687 const LiveInterval &LI = SA->getParent();
688 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
689 for (unsigned i = 0; i != UseBlocks.size(); ++i) {
690 const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
691 unsigned Number = BI.MBB->getNumber();
692 // We normally only need one spill instruction - a load or a store.
693 Cost += SpillPlacer->getBlockFrequency(Number);
694
695 // Unless the value is redefined in the block.
696 if (BI.LiveIn && BI.LiveOut) {
697 SlotIndex Start, Stop;
698 tie(Start, Stop) = Indexes->getMBBRange(Number);
699 LiveInterval::const_iterator I = LI.find(Start);
700 assert(I != LI.end() && "Expected live-in value");
701 // Is there a different live-out value? If so, we need an extra spill
702 // instruction.
703 if (I->end < Stop)
704 Cost += SpillPlacer->getBlockFrequency(Number);
705 }
706 }
707 return Cost;
708}
709
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000710/// calcGlobalSplitCost - Return the global split cost of following the split
711/// pattern in LiveBundles. This cost should be added to the local cost of the
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000712/// interference pattern in SplitConstraints.
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000713///
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000714float RAGreedy::calcGlobalSplitCost(GlobalSplitCandidate &Cand,
715 InterferenceCache::Cursor Intf) {
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000716 float GlobalCost = 0;
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000717 const BitVector &LiveBundles = Cand.LiveBundles;
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000718 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
719 for (unsigned i = 0; i != UseBlocks.size(); ++i) {
720 const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +0000721 SpillPlacement::BlockConstraint &BC = SplitConstraints[i];
Jakob Stoklund Olesen874be742011-03-05 03:28:51 +0000722 bool RegIn = LiveBundles[Bundles->getBundle(BC.Number, 0)];
723 bool RegOut = LiveBundles[Bundles->getBundle(BC.Number, 1)];
724 unsigned Ins = 0;
725
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000726 if (BI.LiveIn)
727 Ins += RegIn != (BC.Entry == SpillPlacement::PrefReg);
728 if (BI.LiveOut)
729 Ins += RegOut != (BC.Exit == SpillPlacement::PrefReg);
Jakob Stoklund Olesen874be742011-03-05 03:28:51 +0000730 if (Ins)
731 GlobalCost += Ins * SpillPlacer->getBlockFrequency(BC.Number);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000732 }
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000733
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000734 for (unsigned i = 0, e = Cand.ActiveBlocks.size(); i != e; ++i) {
735 unsigned Number = Cand.ActiveBlocks[i];
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000736 bool RegIn = LiveBundles[Bundles->getBundle(Number, 0)];
737 bool RegOut = LiveBundles[Bundles->getBundle(Number, 1)];
Jakob Stoklund Olesen9a543522011-04-06 21:32:41 +0000738 if (!RegIn && !RegOut)
739 continue;
740 if (RegIn && RegOut) {
741 // We need double spill code if this block has interference.
742 Intf.moveToBlock(Number);
743 if (Intf.hasInterference())
744 GlobalCost += 2*SpillPlacer->getBlockFrequency(Number);
745 continue;
746 }
747 // live-in / stack-out or stack-in live-out.
748 GlobalCost += SpillPlacer->getBlockFrequency(Number);
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000749 }
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +0000750 return GlobalCost;
751}
752
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000753/// splitAroundRegion - Split VirtReg around the region determined by
754/// LiveBundles. Make an effort to avoid interference from PhysReg.
755///
756/// The 'register' interval is going to contain as many uses as possible while
757/// avoiding interference. The 'stack' interval is the complement constructed by
758/// SplitEditor. It will contain the rest.
759///
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000760void RAGreedy::splitAroundRegion(LiveInterval &VirtReg,
761 GlobalSplitCandidate &Cand,
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000762 SmallVectorImpl<LiveInterval*> &NewVRegs) {
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000763 const BitVector &LiveBundles = Cand.LiveBundles;
764
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000765 DEBUG({
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000766 dbgs() << "Splitting around region for " << PrintReg(Cand.PhysReg, TRI)
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000767 << " with bundles";
768 for (int i = LiveBundles.find_first(); i>=0; i = LiveBundles.find_next(i))
769 dbgs() << " EB#" << i;
770 dbgs() << ".\n";
771 });
772
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +0000773 InterferenceCache::Cursor Intf(IntfCache, Cand.PhysReg);
Jakob Stoklund Olesen92a55f42011-03-09 00:57:29 +0000774 LiveRangeEdit LREdit(VirtReg, NewVRegs, this);
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +0000775 SE->reset(LREdit);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000776
777 // Create the main cross-block interval.
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +0000778 const unsigned MainIntv = SE->openIntv();
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000779
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000780 // First handle all the blocks with uses.
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +0000781 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
782 for (unsigned i = 0; i != UseBlocks.size(); ++i) {
783 const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000784 bool RegIn = BI.LiveIn &&
785 LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 0)];
786 bool RegOut = BI.LiveOut &&
787 LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 1)];
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000788
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +0000789 // Create separate intervals for isolated blocks with multiple uses.
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000790 //
791 // |---o---o---| Enter and leave on the stack.
792 // ____-----____ Create local interval for uses.
793 //
794 // | o---o---| Defined in block, leave on stack.
795 // -----____ Create local interval for uses.
796 //
797 // |---o---x | Enter on stack, killed in block.
798 // ____----- Create local interval for uses.
799 //
800 if (!RegIn && !RegOut) {
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +0000801 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " isolated.\n");
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000802 if (!BI.isOneInstr()) {
803 SE->splitSingleBlock(BI);
804 SE->selectIntv(MainIntv);
805 }
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +0000806 continue;
807 }
808
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000809 SlotIndex Start, Stop;
810 tie(Start, Stop) = Indexes->getMBBRange(BI.MBB);
Jakob Stoklund Oleseneda0fe82011-04-02 06:03:38 +0000811 Intf.moveToBlock(BI.MBB->getNumber());
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000812 DEBUG(dbgs() << "EB#" << Bundles->getBundle(BI.MBB->getNumber(), 0)
Jakob Stoklund Olesen736a0112011-07-04 00:05:28 +0000813 << (BI.LiveIn ? (RegIn ? " => " : " -> ") : " ")
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000814 << "BB#" << BI.MBB->getNumber()
Jakob Stoklund Olesen736a0112011-07-04 00:05:28 +0000815 << (BI.LiveOut ? (RegOut ? " => " : " -> ") : " ")
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000816 << " EB#" << Bundles->getBundle(BI.MBB->getNumber(), 1)
Jakob Stoklund Olesen612f7802011-04-05 04:20:29 +0000817 << " [" << Start << ';'
818 << SA->getLastSplitPoint(BI.MBB->getNumber()) << '-' << Stop
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000819 << ") uses [" << BI.FirstUse << ';' << BI.LastUse
Jakob Stoklund Olesen612f7802011-04-05 04:20:29 +0000820 << ") intf [" << Intf.first() << ';' << Intf.last() << ')');
Jakob Stoklund Olesen2dfbb3e2011-02-03 20:29:43 +0000821
822 // The interference interval should either be invalid or overlap MBB.
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000823 assert((!Intf.hasInterference() || Intf.first() < Stop)
Jakob Stoklund Oleseneda0fe82011-04-02 06:03:38 +0000824 && "Bad interference");
Jakob Stoklund Olesen6c8afd72011-04-04 15:32:15 +0000825 assert((!Intf.hasInterference() || Intf.last() > Start)
Jakob Stoklund Olesen36d61862011-03-03 03:41:29 +0000826 && "Bad interference");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000827
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000828 // We are now ready to decide where to split in the current block. There
829 // are many variables guiding the decision:
830 //
831 // - RegIn / RegOut: The global splitting algorithm's decisions for our
832 // ingoing and outgoing bundles.
833 //
834 // - BI.BlockIn / BI.BlockOut: Is the live range live-in and/or live-out
835 // from this block.
836 //
837 // - Intf.hasInterference(): Is there interference in this block.
838 //
839 // - Intf.first() / Inft.last(): The range of interference.
840 //
841 // The live range should be split such that MainIntv is live-in when RegIn
842 // is set, and live-out when RegOut is set. MainIntv should never overlap
843 // the interference, and the stack interval should never have more than one
844 // use per block.
845
846 // No splits can be inserted after LastSplitPoint, overlap instead.
847 SlotIndex LastSplitPoint = Stop;
848 if (BI.LiveOut)
849 LastSplitPoint = SA->getLastSplitPoint(BI.MBB->getNumber());
850
851 // At this point, we know that either RegIn or RegOut is set. We dealt with
852 // the all-stack case above.
853
854 // Blocks without interference are relatively easy.
Jakob Stoklund Oleseneda0fe82011-04-02 06:03:38 +0000855 if (!Intf.hasInterference()) {
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000856 DEBUG(dbgs() << ", no interference.\n");
857 SE->selectIntv(MainIntv);
858 // The easiest case has MainIntv live through.
859 //
860 // |---o---o---| Live-in, live-out.
861 // ============= Use MainIntv everywhere.
862 //
863 SlotIndex From = Start, To = Stop;
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000864
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000865 // Block entry. Reload before the first use if MainIntv is not live-in.
866 //
867 // |---o-- Enter on stack.
868 // ____=== Reload before first use.
869 //
870 // | o-- Defined in block.
871 // === Use MainIntv from def.
872 //
873 if (!RegIn)
874 From = SE->enterIntvBefore(BI.FirstUse);
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000875
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000876 // Block exit. Handle cases where MainIntv is not live-out.
877 if (!BI.LiveOut)
878 //
879 // --x | Killed in block.
880 // === Use MainIntv up to kill.
881 //
882 To = SE->leaveIntvAfter(BI.LastUse);
883 else if (!RegOut) {
884 //
885 // --o---| Live-out on stack.
886 // ===____ Use MainIntv up to last use, switch to stack.
887 //
888 // -----o| Live-out on stack, last use after last split point.
889 // ====== Extend MainIntv to last use, overlapping.
890 // \____ Copy to stack interval before last split point.
891 //
892 if (BI.LastUse < LastSplitPoint)
893 To = SE->leaveIntvAfter(BI.LastUse);
894 else {
895 // The last use is after the last split point, it is probably an
896 // indirect branch.
897 To = SE->leaveIntvBefore(LastSplitPoint);
898 // Run a double interval from the split to the last use. This makes
899 // it possible to spill the complement without affecting the indirect
900 // branch.
901 SE->overlapIntv(To, BI.LastUse);
Jakob Stoklund Olesen5c716bd2011-02-08 18:50:21 +0000902 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000903 }
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000904
905 // Paint in MainIntv liveness for this block.
906 SE->useIntv(From, To);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000907 continue;
908 }
909
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000910 // We are now looking at a block with interference, and we know that either
911 // RegIn or RegOut is set.
912 assert(Intf.hasInterference() && (RegIn || RegOut) && "Bad invariant");
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000913
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000914 // If the live range is not live through the block, it is possible that the
915 // interference doesn't even overlap. Deal with those cases first. Since
916 // no copy instructions are required, we can tolerate interference starting
917 // or ending at the same instruction that kills or defines our live range.
918
919 // Live-in, killed before interference.
920 //
921 // ~~~ Interference after kill.
922 // |---o---x | Killed in block.
923 // ========= Use MainIntv everywhere.
924 //
925 if (RegIn && !BI.LiveOut && BI.LastUse <= Intf.first()) {
926 DEBUG(dbgs() << ", live-in, killed before interference.\n");
927 SE->selectIntv(MainIntv);
928 SlotIndex To = SE->leaveIntvAfter(BI.LastUse);
929 SE->useIntv(Start, To);
Jakob Stoklund Olesenfe3f99f2011-02-05 01:06:39 +0000930 continue;
931 }
932
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000933 // Live-out, defined after interference.
934 //
935 // ~~~ Interference before def.
936 // | o---o---| Defined in block.
937 // ========= Use MainIntv everywhere.
938 //
939 if (RegOut && !BI.LiveIn && BI.FirstUse >= Intf.last()) {
940 DEBUG(dbgs() << ", live-out, defined after interference.\n");
941 SE->selectIntv(MainIntv);
942 SlotIndex From = SE->enterIntvBefore(BI.FirstUse);
943 SE->useIntv(From, Stop);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +0000944 continue;
945 }
946
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000947 // The interference is now known to overlap the live range, but it may
948 // still be easy to avoid if all the interference is on one side of the
949 // uses, and we enter or leave on the stack.
950
951 // Live-out on stack, interference after last use.
952 //
953 // ~~~ Interference after last use.
954 // |---o---o---| Live-out on stack.
955 // =========____ Leave MainIntv after last use.
956 //
957 // ~ Interference after last use.
958 // |---o---o--o| Live-out on stack, late last use.
Jakob Stoklund Olesen2b0f4ab2011-07-05 15:38:37 +0000959 // ============ Copy to stack after LSP, overlap MainIntv.
960 // \_____ Stack interval is live-out.
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +0000961 //
962 if (!RegOut && Intf.first() > BI.LastUse.getBoundaryIndex()) {
963 assert(RegIn && "Stack-in, stack-out should already be handled");
964 if (BI.LastUse < LastSplitPoint) {
965 DEBUG(dbgs() << ", live-in, stack-out, interference after last use.\n");
966 SE->selectIntv(MainIntv);
967 SlotIndex To = SE->leaveIntvAfter(BI.LastUse);
968 assert(To <= Intf.first() && "Expected to avoid interference");
969 SE->useIntv(Start, To);
970 } else {
971 DEBUG(dbgs() << ", live-in, stack-out, avoid last split point\n");
972 SE->selectIntv(MainIntv);
973 SlotIndex To = SE->leaveIntvBefore(LastSplitPoint);
974 assert(To <= Intf.first() && "Expected to avoid interference");
975 SE->overlapIntv(To, BI.LastUse);
976 SE->useIntv(Start, To);
977 }
978 continue;
979 }
980
981 // Live-in on stack, interference before first use.
982 //
983 // ~~~ Interference before first use.
984 // |---o---o---| Live-in on stack.
985 // ____========= Enter MainIntv before first use.
986 //
987 if (!RegIn && Intf.last() < BI.FirstUse.getBaseIndex()) {
988 assert(RegOut && "Stack-in, stack-out should already be handled");
989 DEBUG(dbgs() << ", stack-in, interference before first use.\n");
990 SE->selectIntv(MainIntv);
991 SlotIndex From = SE->enterIntvBefore(BI.FirstUse);
992 assert(From >= Intf.last() && "Expected to avoid interference");
993 SE->useIntv(From, Stop);
994 continue;
995 }
996
997 // The interference is overlapping somewhere we wanted to use MainIntv. That
998 // means we need to create a local interval that can be allocated a
999 // different register.
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +00001000 unsigned LocalIntv = SE->openIntv();
Jakob Stoklund Olesen2b0f4ab2011-07-05 15:38:37 +00001001 DEBUG(dbgs() << ", creating local interval " << LocalIntv << ".\n");
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +00001002
1003 // We may be creating copies directly between MainIntv and LocalIntv,
1004 // bypassing the stack interval. When we do that, we should never use the
1005 // leaveIntv* methods as they define values in the stack interval. By
1006 // starting from the end of the block and working our way backwards, we can
1007 // get by with only enterIntv* methods.
1008 //
1009 // When selecting split points, we generally try to maximize the stack
1010 // interval as long at it contains no uses, maximize the main interval as
1011 // long as it doesn't overlap interference, and minimize the local interval
1012 // that we don't know how to allocate yet.
1013
1014 // Handle the block exit, set Pos to the first handled slot.
1015 SlotIndex Pos = BI.LastUse;
1016 if (RegOut) {
1017 assert(Intf.last() < LastSplitPoint && "Cannot be live-out in register");
1018 // Create a snippet of MainIntv that is live-out.
1019 //
1020 // ~~~ Interference overlapping uses.
1021 // --o---| Live-out in MainIntv.
1022 // ----=== Switch from LocalIntv to MainIntv after interference.
1023 //
1024 SE->selectIntv(MainIntv);
1025 Pos = SE->enterIntvAfter(Intf.last());
1026 assert(Pos >= Intf.last() && "Expected to avoid interference");
1027 SE->useIntv(Pos, Stop);
1028 SE->selectIntv(LocalIntv);
1029 } else if (BI.LiveOut) {
1030 if (BI.LastUse < LastSplitPoint) {
1031 // Live-out on the stack.
1032 //
1033 // ~~~ Interference overlapping uses.
1034 // --o---| Live-out on stack.
1035 // ---____ Switch from LocalIntv to stack after last use.
1036 //
1037 Pos = SE->leaveIntvAfter(BI.LastUse);
1038 } else {
1039 // Live-out on the stack, last use after last split point.
1040 //
1041 // ~~~ Interference overlapping uses.
1042 // --o--o| Live-out on stack, late use.
1043 // ------ Copy to stack before LSP, overlap LocalIntv.
1044 // \__
1045 //
1046 Pos = SE->leaveIntvBefore(LastSplitPoint);
1047 // We need to overlap LocalIntv so it can reach LastUse.
1048 SE->overlapIntv(Pos, BI.LastUse);
1049 }
1050 }
1051
1052 // When not live-out, leave Pos at LastUse. We have handled everything from
1053 // Pos to Stop. Find the starting point for LocalIntv.
1054 assert(SE->currentIntv() == LocalIntv && "Expecting local interval");
1055
1056 if (RegIn) {
1057 assert(Start < Intf.first() && "Cannot be live-in with interference");
1058 // Live-in in MainIntv, only use LocalIntv for interference.
1059 //
1060 // ~~~ Interference overlapping uses.
1061 // |---o-- Live-in in MainIntv.
1062 // ====--- Switch to LocalIntv before interference.
1063 //
Jakob Stoklund Olesen736a0112011-07-04 00:05:28 +00001064 SlotIndex Switch = SE->enterIntvBefore(std::min(Pos, Intf.first()));
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +00001065 assert(Switch <= Intf.first() && "Expected to avoid interference");
1066 SE->useIntv(Switch, Pos);
1067 SE->selectIntv(MainIntv);
1068 SE->useIntv(Start, Switch);
1069 } else {
1070 // Live-in on stack, enter LocalIntv before first use.
1071 //
1072 // ~~~ Interference overlapping uses.
1073 // |---o-- Live-in in MainIntv.
1074 // ____--- Reload to LocalIntv before interference.
1075 //
1076 // Defined in block.
1077 //
1078 // ~~~ Interference overlapping uses.
1079 // | o-- Defined in block.
1080 // --- Begin LocalIntv at first use.
1081 //
Jakob Stoklund Olesen736a0112011-07-04 00:05:28 +00001082 SlotIndex Switch = SE->enterIntvBefore(std::min(Pos, BI.FirstUse));
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +00001083 SE->useIntv(Switch, Pos);
1084 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001085 }
1086
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +00001087 // Handle live-through blocks.
Jakob Stoklund Olesen87360f72011-06-30 01:30:39 +00001088 SE->selectIntv(MainIntv);
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +00001089 for (unsigned i = 0, e = Cand.ActiveBlocks.size(); i != e; ++i) {
1090 unsigned Number = Cand.ActiveBlocks[i];
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +00001091 bool RegIn = LiveBundles[Bundles->getBundle(Number, 0)];
1092 bool RegOut = LiveBundles[Bundles->getBundle(Number, 1)];
1093 DEBUG(dbgs() << "Live through BB#" << Number << '\n');
1094 if (RegIn && RegOut) {
1095 Intf.moveToBlock(Number);
1096 if (!Intf.hasInterference()) {
1097 SE->useIntv(Indexes->getMBBStartIdx(Number),
1098 Indexes->getMBBEndIdx(Number));
1099 continue;
1100 }
1101 }
1102 MachineBasicBlock *MBB = MF->getBlockNumbered(Number);
1103 if (RegIn)
1104 SE->leaveIntvAtTop(*MBB);
1105 if (RegOut)
1106 SE->enterIntvAtEnd(*MBB);
1107 }
1108
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +00001109 ++NumGlobalSplits;
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001110
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001111 SmallVector<unsigned, 8> IntvMap;
1112 SE->finish(&IntvMap);
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +00001113 DebugVars->splitRegister(VirtReg.reg, LREdit.regs());
1114
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +00001115 ExtraRegInfo.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesenb2abfa02011-05-28 02:32:57 +00001116 unsigned OrigBlocks = SA->getNumLiveBlocks();
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001117
1118 // Sort out the new intervals created by splitting. We get four kinds:
1119 // - Remainder intervals should not be split again.
1120 // - Candidate intervals can be assigned to Cand.PhysReg.
1121 // - Block-local splits are candidates for local splitting.
1122 // - DCE leftovers should go back on the queue.
1123 for (unsigned i = 0, e = LREdit.size(); i != e; ++i) {
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +00001124 LiveInterval &Reg = *LREdit.get(i);
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001125
1126 // Ignore old intervals from DCE.
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +00001127 if (getStage(Reg) != RS_New)
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001128 continue;
1129
1130 // Remainder interval. Don't try splitting again, spill if it doesn't
1131 // allocate.
1132 if (IntvMap[i] == 0) {
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +00001133 setStage(Reg, RS_Global);
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001134 continue;
1135 }
1136
Jakob Stoklund Olesen9f4b8932011-04-26 22:33:12 +00001137 // Main interval. Allow repeated splitting as long as the number of live
1138 // blocks is strictly decreasing.
1139 if (IntvMap[i] == MainIntv) {
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +00001140 if (SA->countLiveBlocks(&Reg) >= OrigBlocks) {
Jakob Stoklund Olesen9f4b8932011-04-26 22:33:12 +00001141 DEBUG(dbgs() << "Main interval covers the same " << OrigBlocks
1142 << " blocks as original.\n");
1143 // Don't allow repeated splitting as a safe guard against looping.
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +00001144 setStage(Reg, RS_Global);
Jakob Stoklund Olesen9f4b8932011-04-26 22:33:12 +00001145 }
1146 continue;
1147 }
1148
1149 // Other intervals are treated as new. This includes local intervals created
1150 // for blocks with multiple uses, and anything created by DCE.
Jakob Stoklund Olesen59280462011-04-21 18:38:15 +00001151 }
1152
Jakob Stoklund Oleseneb291572011-03-27 22:49:21 +00001153 if (VerifyEnabled)
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001154 MF->verify(this, "After splitting live range around region");
1155}
1156
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001157unsigned RAGreedy::tryRegionSplit(LiveInterval &VirtReg, AllocationOrder &Order,
1158 SmallVectorImpl<LiveInterval*> &NewVRegs) {
Jakob Stoklund Olesen20072982011-04-22 22:47:40 +00001159 float BestCost = Hysteresis * calcSpillCost();
1160 DEBUG(dbgs() << "Cost of isolating all blocks = " << BestCost << '\n');
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +00001161 const unsigned NoCand = ~0u;
1162 unsigned BestCand = NoCand;
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +00001163
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001164 Order.rewind();
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +00001165 for (unsigned Cand = 0; unsigned PhysReg = Order.next(); ++Cand) {
1166 if (GlobalCand.size() <= Cand)
1167 GlobalCand.resize(Cand+1);
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +00001168 GlobalCand[Cand].reset(PhysReg);
Jakob Stoklund Olesen96dcd952011-03-05 01:10:31 +00001169
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +00001170 SpillPlacer->prepare(GlobalCand[Cand].LiveBundles);
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +00001171 float Cost;
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +00001172 InterferenceCache::Cursor Intf(IntfCache, PhysReg);
1173 if (!addSplitConstraints(Intf, Cost)) {
1174 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << "\tno positive bundles\n");
Jakob Stoklund Olesen1b400e82011-04-06 21:32:38 +00001175 continue;
1176 }
Jakob Stoklund Olesenf4afdfc2011-04-09 02:59:09 +00001177 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << "\tstatic = " << Cost);
Jakob Stoklund Olesen20072982011-04-22 22:47:40 +00001178 if (Cost >= BestCost) {
1179 DEBUG({
1180 if (BestCand == NoCand)
1181 dbgs() << " worse than no bundles\n";
1182 else
1183 dbgs() << " worse than "
1184 << PrintReg(GlobalCand[BestCand].PhysReg, TRI) << '\n';
1185 });
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001186 continue;
Jakob Stoklund Olesen874be742011-03-05 03:28:51 +00001187 }
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +00001188 growRegion(GlobalCand[Cand], Intf);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001189
Jakob Stoklund Olesen9efa2a22011-04-06 19:13:57 +00001190 SpillPlacer->finish();
1191
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001192 // No live bundles, defer to splitSingleBlocks().
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +00001193 if (!GlobalCand[Cand].LiveBundles.any()) {
Jakob Stoklund Olesen874be742011-03-05 03:28:51 +00001194 DEBUG(dbgs() << " no bundles.\n");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001195 continue;
Jakob Stoklund Olesen874be742011-03-05 03:28:51 +00001196 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001197
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +00001198 Cost += calcGlobalSplitCost(GlobalCand[Cand], Intf);
Jakob Stoklund Olesen874be742011-03-05 03:28:51 +00001199 DEBUG({
1200 dbgs() << ", total = " << Cost << " with bundles";
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +00001201 for (int i = GlobalCand[Cand].LiveBundles.find_first(); i>=0;
1202 i = GlobalCand[Cand].LiveBundles.find_next(i))
Jakob Stoklund Olesen874be742011-03-05 03:28:51 +00001203 dbgs() << " EB#" << i;
1204 dbgs() << ".\n";
1205 });
Jakob Stoklund Olesen20072982011-04-22 22:47:40 +00001206 if (Cost < BestCost) {
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +00001207 BestCand = Cand;
Jakob Stoklund Olesen20072982011-04-22 22:47:40 +00001208 BestCost = Hysteresis * Cost; // Prevent rounding effects.
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001209 }
1210 }
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001211
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +00001212 if (BestCand == NoCand)
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001213 return 0;
1214
Jakob Stoklund Olesen5db42892011-04-12 21:30:53 +00001215 splitAroundRegion(VirtReg, GlobalCand[BestCand], NewVRegs);
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001216 return 0;
1217}
1218
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001219
1220//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001221// Local Splitting
1222//===----------------------------------------------------------------------===//
1223
1224
1225/// calcGapWeights - Compute the maximum spill weight that needs to be evicted
1226/// in order to use PhysReg between two entries in SA->UseSlots.
1227///
1228/// GapWeight[i] represents the gap between UseSlots[i] and UseSlots[i+1].
1229///
1230void RAGreedy::calcGapWeights(unsigned PhysReg,
1231 SmallVectorImpl<float> &GapWeight) {
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +00001232 assert(SA->getUseBlocks().size() == 1 && "Not a local interval");
1233 const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front();
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001234 const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
1235 const unsigned NumGaps = Uses.size()-1;
1236
1237 // Start and end points for the interference check.
1238 SlotIndex StartIdx = BI.LiveIn ? BI.FirstUse.getBaseIndex() : BI.FirstUse;
1239 SlotIndex StopIdx = BI.LiveOut ? BI.LastUse.getBoundaryIndex() : BI.LastUse;
1240
1241 GapWeight.assign(NumGaps, 0.0f);
1242
1243 // Add interference from each overlapping register.
1244 for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) {
1245 if (!query(const_cast<LiveInterval&>(SA->getParent()), *AI)
1246 .checkInterference())
1247 continue;
1248
1249 // We know that VirtReg is a continuous interval from FirstUse to LastUse,
1250 // so we don't need InterferenceQuery.
1251 //
1252 // Interference that overlaps an instruction is counted in both gaps
1253 // surrounding the instruction. The exception is interference before
1254 // StartIdx and after StopIdx.
1255 //
1256 LiveIntervalUnion::SegmentIter IntI = PhysReg2LiveUnion[*AI].find(StartIdx);
1257 for (unsigned Gap = 0; IntI.valid() && IntI.start() < StopIdx; ++IntI) {
1258 // Skip the gaps before IntI.
1259 while (Uses[Gap+1].getBoundaryIndex() < IntI.start())
1260 if (++Gap == NumGaps)
1261 break;
1262 if (Gap == NumGaps)
1263 break;
1264
1265 // Update the gaps covered by IntI.
1266 const float weight = IntI.value()->weight;
1267 for (; Gap != NumGaps; ++Gap) {
1268 GapWeight[Gap] = std::max(GapWeight[Gap], weight);
1269 if (Uses[Gap+1].getBaseIndex() >= IntI.stop())
1270 break;
1271 }
1272 if (Gap == NumGaps)
1273 break;
1274 }
1275 }
1276}
1277
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001278/// tryLocalSplit - Try to split VirtReg into smaller intervals inside its only
1279/// basic block.
1280///
1281unsigned RAGreedy::tryLocalSplit(LiveInterval &VirtReg, AllocationOrder &Order,
1282 SmallVectorImpl<LiveInterval*> &NewVRegs) {
Jakob Stoklund Olesendb529a82011-04-06 03:57:00 +00001283 assert(SA->getUseBlocks().size() == 1 && "Not a local interval");
1284 const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front();
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001285
1286 // Note that it is possible to have an interval that is live-in or live-out
1287 // while only covering a single block - A phi-def can use undef values from
1288 // predecessors, and the block could be a single-block loop.
1289 // We don't bother doing anything clever about such a case, we simply assume
1290 // that the interval is continuous from FirstUse to LastUse. We should make
1291 // sure that we don't do anything illegal to such an interval, though.
1292
1293 const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots;
1294 if (Uses.size() <= 2)
1295 return 0;
1296 const unsigned NumGaps = Uses.size()-1;
1297
1298 DEBUG({
1299 dbgs() << "tryLocalSplit: ";
1300 for (unsigned i = 0, e = Uses.size(); i != e; ++i)
1301 dbgs() << ' ' << SA->UseSlots[i];
1302 dbgs() << '\n';
1303 });
1304
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001305 // Since we allow local split results to be split again, there is a risk of
1306 // creating infinite loops. It is tempting to require that the new live
1307 // ranges have less instructions than the original. That would guarantee
1308 // convergence, but it is too strict. A live range with 3 instructions can be
1309 // split 2+3 (including the COPY), and we want to allow that.
1310 //
1311 // Instead we use these rules:
1312 //
1313 // 1. Allow any split for ranges with getStage() < RS_Local. (Except for the
1314 // noop split, of course).
1315 // 2. Require progress be made for ranges with getStage() >= RS_Local. All
1316 // the new ranges must have fewer instructions than before the split.
1317 // 3. New ranges with the same number of instructions are marked RS_Local,
1318 // smaller ranges are marked RS_New.
1319 //
1320 // These rules allow a 3 -> 2+3 split once, which we need. They also prevent
1321 // excessive splitting and infinite loops.
1322 //
1323 bool ProgressRequired = getStage(VirtReg) >= RS_Local;
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001324
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001325 // Best split candidate.
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001326 unsigned BestBefore = NumGaps;
1327 unsigned BestAfter = 0;
1328 float BestDiff = 0;
1329
Jakob Stoklund Olesen40a42a22011-03-04 00:58:40 +00001330 const float blockFreq = SpillPlacer->getBlockFrequency(BI.MBB->getNumber());
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001331 SmallVector<float, 8> GapWeight;
1332
1333 Order.rewind();
1334 while (unsigned PhysReg = Order.next()) {
1335 // Keep track of the largest spill weight that would need to be evicted in
1336 // order to make use of PhysReg between UseSlots[i] and UseSlots[i+1].
1337 calcGapWeights(PhysReg, GapWeight);
1338
1339 // Try to find the best sequence of gaps to close.
1340 // The new spill weight must be larger than any gap interference.
1341
1342 // We will split before Uses[SplitBefore] and after Uses[SplitAfter].
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001343 unsigned SplitBefore = 0, SplitAfter = 1;
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001344
1345 // MaxGap should always be max(GapWeight[SplitBefore..SplitAfter-1]).
1346 // It is the spill weight that needs to be evicted.
1347 float MaxGap = GapWeight[0];
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001348
1349 for (;;) {
1350 // Live before/after split?
1351 const bool LiveBefore = SplitBefore != 0 || BI.LiveIn;
1352 const bool LiveAfter = SplitAfter != NumGaps || BI.LiveOut;
1353
1354 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << ' '
1355 << Uses[SplitBefore] << '-' << Uses[SplitAfter]
1356 << " i=" << MaxGap);
1357
1358 // Stop before the interval gets so big we wouldn't be making progress.
1359 if (!LiveBefore && !LiveAfter) {
1360 DEBUG(dbgs() << " all\n");
1361 break;
1362 }
1363 // Should the interval be extended or shrunk?
1364 bool Shrink = true;
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001365
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001366 // How many gaps would the new range have?
1367 unsigned NewGaps = LiveBefore + SplitAfter - SplitBefore + LiveAfter;
1368
1369 // Legally, without causing looping?
1370 bool Legal = !ProgressRequired || NewGaps < NumGaps;
1371
1372 if (Legal && MaxGap < HUGE_VALF) {
1373 // Estimate the new spill weight. Each instruction reads or writes the
1374 // register. Conservatively assume there are no read-modify-write
1375 // instructions.
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001376 //
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001377 // Try to guess the size of the new interval.
1378 const float EstWeight = normalizeSpillWeight(blockFreq * (NewGaps + 1),
1379 Uses[SplitBefore].distance(Uses[SplitAfter]) +
1380 (LiveBefore + LiveAfter)*SlotIndex::InstrDist);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001381 // Would this split be possible to allocate?
1382 // Never allocate all gaps, we wouldn't be making progress.
Jakob Stoklund Olesen66446c82011-04-30 05:07:46 +00001383 DEBUG(dbgs() << " w=" << EstWeight);
1384 if (EstWeight * Hysteresis >= MaxGap) {
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001385 Shrink = false;
Jakob Stoklund Olesen66446c82011-04-30 05:07:46 +00001386 float Diff = EstWeight - MaxGap;
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001387 if (Diff > BestDiff) {
1388 DEBUG(dbgs() << " (best)");
Jakob Stoklund Olesen66446c82011-04-30 05:07:46 +00001389 BestDiff = Hysteresis * Diff;
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001390 BestBefore = SplitBefore;
1391 BestAfter = SplitAfter;
1392 }
1393 }
1394 }
1395
1396 // Try to shrink.
1397 if (Shrink) {
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001398 if (++SplitBefore < SplitAfter) {
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001399 DEBUG(dbgs() << " shrink\n");
1400 // Recompute the max when necessary.
1401 if (GapWeight[SplitBefore - 1] >= MaxGap) {
1402 MaxGap = GapWeight[SplitBefore];
1403 for (unsigned i = SplitBefore + 1; i != SplitAfter; ++i)
1404 MaxGap = std::max(MaxGap, GapWeight[i]);
1405 }
1406 continue;
1407 }
1408 MaxGap = 0;
1409 }
1410
1411 // Try to extend the interval.
1412 if (SplitAfter >= NumGaps) {
1413 DEBUG(dbgs() << " end\n");
1414 break;
1415 }
1416
1417 DEBUG(dbgs() << " extend\n");
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001418 MaxGap = std::max(MaxGap, GapWeight[SplitAfter++]);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001419 }
1420 }
1421
1422 // Didn't find any candidates?
1423 if (BestBefore == NumGaps)
1424 return 0;
1425
1426 DEBUG(dbgs() << "Best local split range: " << Uses[BestBefore]
1427 << '-' << Uses[BestAfter] << ", " << BestDiff
1428 << ", " << (BestAfter - BestBefore + 1) << " instrs\n");
1429
Jakob Stoklund Olesen92a55f42011-03-09 00:57:29 +00001430 LiveRangeEdit LREdit(VirtReg, NewVRegs, this);
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +00001431 SE->reset(LREdit);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001432
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +00001433 SE->openIntv();
1434 SlotIndex SegStart = SE->enterIntvBefore(Uses[BestBefore]);
1435 SlotIndex SegStop = SE->leaveIntvAfter(Uses[BestAfter]);
1436 SE->useIntv(SegStart, SegStop);
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001437 SmallVector<unsigned, 8> IntvMap;
1438 SE->finish(&IntvMap);
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +00001439 DebugVars->splitRegister(VirtReg.reg, LREdit.regs());
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001440
1441 // If the new range has the same number of instructions as before, mark it as
1442 // RS_Local so the next split will be forced to make progress. Otherwise,
1443 // leave the new intervals as RS_New so they can compete.
1444 bool LiveBefore = BestBefore != 0 || BI.LiveIn;
1445 bool LiveAfter = BestAfter != NumGaps || BI.LiveOut;
1446 unsigned NewGaps = LiveBefore + BestAfter - BestBefore + LiveAfter;
1447 if (NewGaps >= NumGaps) {
1448 DEBUG(dbgs() << "Tagging non-progress ranges: ");
1449 assert(!ProgressRequired && "Didn't make progress when it was required.");
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001450 for (unsigned i = 0, e = IntvMap.size(); i != e; ++i)
1451 if (IntvMap[i] == 1) {
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +00001452 setStage(*LREdit.get(i), RS_Local);
Jakob Stoklund Olesenb3e705f2011-06-06 23:55:20 +00001453 DEBUG(dbgs() << PrintReg(LREdit.get(i)->reg));
1454 }
1455 DEBUG(dbgs() << '\n');
1456 }
Jakob Stoklund Olesen0db841f2011-02-17 22:53:48 +00001457 ++NumLocalSplits;
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001458
1459 return 0;
1460}
1461
1462//===----------------------------------------------------------------------===//
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001463// Live Range Splitting
1464//===----------------------------------------------------------------------===//
1465
1466/// trySplit - Try to split VirtReg or one of its interferences, making it
1467/// assignable.
1468/// @return Physreg when VirtReg may be assigned and/or new NewVRegs.
1469unsigned RAGreedy::trySplit(LiveInterval &VirtReg, AllocationOrder &Order,
1470 SmallVectorImpl<LiveInterval*>&NewVRegs) {
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001471 // Local intervals are handled separately.
Jakob Stoklund Olesena2ebf602011-02-19 00:38:40 +00001472 if (LIS->intervalIsInOneMBB(VirtReg)) {
1473 NamedRegionTimer T("Local Splitting", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001474 SA->analyze(&VirtReg);
Jakob Stoklund Olesen034a80d2011-02-17 19:13:53 +00001475 return tryLocalSplit(VirtReg, Order, NewVRegs);
Jakob Stoklund Olesena2ebf602011-02-19 00:38:40 +00001476 }
1477
1478 NamedRegionTimer T("Global Splitting", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001479
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001480 // Don't iterate global splitting.
1481 // Move straight to spilling if this range was produced by a global split.
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001482 if (getStage(VirtReg) >= RS_Global)
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001483 return 0;
1484
1485 SA->analyze(&VirtReg);
1486
Jakob Stoklund Olesen7d6b6a02011-05-03 20:42:13 +00001487 // FIXME: SplitAnalysis may repair broken live ranges coming from the
1488 // coalescer. That may cause the range to become allocatable which means that
1489 // tryRegionSplit won't be making progress. This check should be replaced with
1490 // an assertion when the coalescer is fixed.
1491 if (SA->didRepairRange()) {
1492 // VirtReg has changed, so all cached queries are invalid.
Jakob Stoklund Olesenbdda37d2011-05-10 17:37:41 +00001493 invalidateVirtRegs();
Jakob Stoklund Olesen7d6b6a02011-05-03 20:42:13 +00001494 if (unsigned PhysReg = tryAssign(VirtReg, Order, NewVRegs))
1495 return PhysReg;
1496 }
1497
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001498 // First try to split around a region spanning multiple blocks.
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001499 unsigned PhysReg = tryRegionSplit(VirtReg, Order, NewVRegs);
1500 if (PhysReg || !NewVRegs.empty())
1501 return PhysReg;
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001502
1503 // Then isolate blocks with multiple uses.
Jakob Stoklund Olesenfd5c5132011-04-12 19:32:53 +00001504 SplitAnalysis::BlockPtrSet Blocks;
1505 if (SA->getMultiUseBlocks(Blocks)) {
1506 LiveRangeEdit LREdit(VirtReg, NewVRegs, this);
1507 SE->reset(LREdit);
1508 SE->splitSingleBlocks(Blocks);
1509 setStage(NewVRegs.begin(), NewVRegs.end(), RS_Global);
1510 if (VerifyEnabled)
1511 MF->verify(this, "After splitting live range around basic blocks");
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001512 }
1513
1514 // Don't assign any physregs.
1515 return 0;
1516}
1517
1518
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001519//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001520// Main Entry Point
1521//===----------------------------------------------------------------------===//
1522
1523unsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg,
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001524 SmallVectorImpl<LiveInterval*> &NewVRegs) {
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001525 // First try assigning a free register.
Jakob Stoklund Olesen5f2316a2011-06-03 20:34:53 +00001526 AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo);
Jakob Stoklund Olesen6bfba2e2011-04-20 18:19:48 +00001527 if (unsigned PhysReg = tryAssign(VirtReg, Order, NewVRegs))
1528 return PhysReg;
Andrew Trickb853e6c2010-12-09 18:15:21 +00001529
Jakob Stoklund Olesenb8d936b2011-05-25 23:58:36 +00001530 LiveRangeStage Stage = getStage(VirtReg);
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +00001531 DEBUG(dbgs() << StageName[Stage]
1532 << " Cascade " << ExtraRegInfo[VirtReg.reg].Cascade << '\n');
Jakob Stoklund Olesenb8d936b2011-05-25 23:58:36 +00001533
Jakob Stoklund Olesen76395c92011-06-01 18:45:02 +00001534 // Try to evict a less worthy live range, but only for ranges from the primary
1535 // queue. The RS_Second ranges already failed to do this, and they should not
1536 // get a second chance until they have been split.
1537 if (Stage != RS_Second)
1538 if (unsigned PhysReg = tryEvict(VirtReg, Order, NewVRegs))
1539 return PhysReg;
Andrew Trickb853e6c2010-12-09 18:15:21 +00001540
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001541 assert(NewVRegs.empty() && "Cannot append to existing NewVRegs");
1542
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +00001543 // The first time we see a live range, don't try to split or spill.
1544 // Wait until the second time, when all smaller ranges have been allocated.
1545 // This gives a better picture of the interference to split around.
Jakob Stoklund Olesenf22ca3f2011-03-30 02:52:39 +00001546 if (Stage == RS_First) {
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +00001547 setStage(VirtReg, RS_Second);
Jakob Stoklund Olesenc1655e12011-03-19 23:02:47 +00001548 DEBUG(dbgs() << "wait for second round\n");
Jakob Stoklund Olesen107d3662011-02-24 23:21:36 +00001549 NewVRegs.push_back(&VirtReg);
1550 return 0;
1551 }
1552
Jakob Stoklund Olesenbf4e10f2011-05-06 21:58:30 +00001553 // If we couldn't allocate a register from spilling, there is probably some
1554 // invalid inline assembly. The base class wil report it.
1555 if (Stage >= RS_Spill)
1556 return ~0u;
Jakob Stoklund Olesen22a1df62011-03-01 21:10:07 +00001557
Jakob Stoklund Olesen46c83c82010-12-14 00:37:49 +00001558 // Try splitting VirtReg or interferences.
Jakob Stoklund Olesenccdb3fc2011-01-19 22:11:48 +00001559 unsigned PhysReg = trySplit(VirtReg, Order, NewVRegs);
1560 if (PhysReg || !NewVRegs.empty())
Jakob Stoklund Olesenb64d92e2010-12-14 00:37:44 +00001561 return PhysReg;
1562
Jakob Stoklund Olesen770d42d2010-12-22 22:01:30 +00001563 // Finally spill VirtReg itself.
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +00001564 NamedRegionTimer T("Spiller", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesen47dbf6c2011-03-10 01:51:42 +00001565 LiveRangeEdit LRE(VirtReg, NewVRegs, this);
1566 spiller().spill(LRE);
Jakob Stoklund Olesen6094bd82011-03-29 21:20:19 +00001567 setStage(NewVRegs.begin(), NewVRegs.end(), RS_Spill);
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001568
Jakob Stoklund Olesenc46570d2011-03-16 22:56:08 +00001569 if (VerifyEnabled)
1570 MF->verify(this, "After spilling");
1571
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001572 // The live virtual register requesting allocation was spilled, so tell
1573 // the caller not to allocate anything during this round.
1574 return 0;
1575}
1576
1577bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
1578 DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n"
1579 << "********** Function: "
1580 << ((Value*)mf.getFunction())->getName() << '\n');
1581
1582 MF = &mf;
Jakob Stoklund Olesenaf249642010-12-17 23:16:35 +00001583 if (VerifyEnabled)
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +00001584 MF->verify(this, "Before greedy register allocator");
Jakob Stoklund Olesenaf249642010-12-17 23:16:35 +00001585
Jakob Stoklund Olesen4680dec2010-12-10 23:49:00 +00001586 RegAllocBase::init(getAnalysis<VirtRegMap>(), getAnalysis<LiveIntervals>());
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001587 Indexes = &getAnalysis<SlotIndexes>();
Jakob Stoklund Olesenf428eb62010-12-17 23:16:32 +00001588 DomTree = &getAnalysis<MachineDominatorTree>();
Jakob Stoklund Olesenf6dff842010-12-10 22:54:44 +00001589 SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM));
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +00001590 Loops = &getAnalysis<MachineLoopInfo>();
1591 LoopRanges = &getAnalysis<MachineLoopRanges>();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001592 Bundles = &getAnalysis<EdgeBundles>();
1593 SpillPlacer = &getAnalysis<SpillPlacement>();
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +00001594 DebugVars = &getAnalysis<LiveDebugVariables>();
Jakob Stoklund Olesenb5fa9332011-01-18 21:13:27 +00001595
Jakob Stoklund Olesen1b847de2011-02-19 00:53:42 +00001596 SA.reset(new SplitAnalysis(*VRM, *LIS, *Loops));
Jakob Stoklund Olesenbece06f2011-03-03 01:29:13 +00001597 SE.reset(new SplitEditor(*SA, *LIS, *VRM, *DomTree));
Jakob Stoklund Olesen1a988002011-07-02 01:37:09 +00001598 ExtraRegInfo.clear();
1599 ExtraRegInfo.resize(MRI->getNumVirtRegs());
1600 NextCascade = 1;
Jakob Stoklund Oleseneda0fe82011-04-02 06:03:38 +00001601 IntfCache.init(MF, &PhysReg2LiveUnion[0], Indexes, TRI);
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +00001602
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001603 allocatePhysRegs();
1604 addMBBLiveIns(MF);
Jakob Stoklund Olesen8a61da82011-02-08 21:13:03 +00001605 LIS->addKillFlags();
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001606
1607 // Run rewriter
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +00001608 {
1609 NamedRegionTimer T("Rewriter", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesenba05c012011-02-18 22:03:18 +00001610 VRM->rewrite(Indexes);
Jakob Stoklund Olesen533f58e2010-12-11 00:19:56 +00001611 }
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001612
Jakob Stoklund Olesencfafc542011-04-05 21:40:37 +00001613 // Write out new DBG_VALUE instructions.
Jakob Stoklund Olesenf42b6612011-05-06 18:00:02 +00001614 DebugVars->emitDebugValues(VRM);
Jakob Stoklund Olesencfafc542011-04-05 21:40:37 +00001615
Jakob Stoklund Olesencba2e062010-12-08 03:26:16 +00001616 // The pass output is in VirtRegMap. Release all the transient data.
1617 releaseMemory();
1618
1619 return true;
1620}