blob: 699f49fad96508d8630880f8cd1d88cf97dc0884 [file] [log] [blame]
Jakob Stoklund Olesenb8812a12010-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
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/CodeGen/Passes.h"
Jakob Stoklund Olesen4d7432e2010-12-10 22:21:05 +000016#include "AllocationOrder.h"
Jakob Stoklund Olesen91cbcaf2011-04-02 06:03:35 +000017#include "InterferenceCache.h"
Jakob Stoklund Olesen6aa0fbf2011-04-05 21:40:37 +000018#include "LiveDebugVariables.h"
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +000019#include "RegAllocBase.h"
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +000020#include "SpillPlacement.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "Spiller.h"
Jakob Stoklund Olesene7601e92010-12-15 23:46:13 +000022#include "SplitKit.h"
Jakob Stoklund Olesen99827e82011-02-17 22:53:48 +000023#include "llvm/ADT/Statistic.h"
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +000024#include "llvm/Analysis/AliasAnalysis.h"
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +000025#include "llvm/CodeGen/CalcSpillWeights.h"
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +000026#include "llvm/CodeGen/EdgeBundles.h"
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +000027#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Pete Cooper3ca96f92012-04-02 22:44:18 +000028#include "llvm/CodeGen/LiveRangeEdit.h"
Jakob Stoklund Olesen26c9d702012-11-28 19:13:06 +000029#include "llvm/CodeGen/LiveRegMatrix.h"
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +000030#include "llvm/CodeGen/LiveStackAnalysis.h"
Benjamin Kramere2a1d892013-06-17 19:00:36 +000031#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
Jakob Stoklund Olesen1740e002010-12-17 23:16:32 +000032#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +000033#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +000034#include "llvm/CodeGen/MachineLoopInfo.h"
35#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +000036#include "llvm/CodeGen/RegAllocRegistry.h"
Quentin Colombet1fb3362a2014-01-02 22:47:22 +000037#include "llvm/CodeGen/RegisterClassInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000038#include "llvm/CodeGen/VirtRegMap.h"
Quentin Colombet96bd2a12014-04-04 02:05:21 +000039#include "llvm/IR/LLVMContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000040#include "llvm/PassAnalysisSupport.h"
Duncan P. N. Exon Smitha5df8132014-04-08 19:18:56 +000041#include "llvm/Support/BranchProbability.h"
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +000042#include "llvm/Support/CommandLine.h"
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +000043#include "llvm/Support/Debug.h"
44#include "llvm/Support/ErrorHandling.h"
Jakob Stoklund Olesen92da7052010-12-11 00:19:56 +000045#include "llvm/Support/Timer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000046#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen2329c542011-02-22 23:01:52 +000047#include <queue>
48
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +000049using namespace llvm;
50
Chandler Carruth1b9dde02014-04-22 02:02:50 +000051#define DEBUG_TYPE "regalloc"
52
Jakob Stoklund Olesen99827e82011-02-17 22:53:48 +000053STATISTIC(NumGlobalSplits, "Number of split global live ranges");
54STATISTIC(NumLocalSplits, "Number of split local live ranges");
Jakob Stoklund Olesen99827e82011-02-17 22:53:48 +000055STATISTIC(NumEvicted, "Number of interferences evicted");
56
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +000057static cl::opt<SplitEditor::ComplementSpillMode>
58SplitSpillMode("split-spill-mode", cl::Hidden,
59 cl::desc("Spill mode for splitting live ranges"),
60 cl::values(clEnumValN(SplitEditor::SM_Partition, "default", "Default"),
61 clEnumValN(SplitEditor::SM_Size, "size", "Optimize for size"),
62 clEnumValN(SplitEditor::SM_Speed, "speed", "Optimize for speed"),
63 clEnumValEnd),
64 cl::init(SplitEditor::SM_Partition));
65
Quentin Colombet87769712014-02-05 22:13:59 +000066static cl::opt<unsigned>
67LastChanceRecoloringMaxDepth("lcr-max-depth", cl::Hidden,
68 cl::desc("Last chance recoloring max depth"),
69 cl::init(5));
70
71static cl::opt<unsigned> LastChanceRecoloringMaxInterference(
72 "lcr-max-interf", cl::Hidden,
73 cl::desc("Last chance recoloring maximum number of considered"
74 " interference at a time"),
75 cl::init(8));
76
Quentin Colombet567e30b2014-04-11 21:39:44 +000077static cl::opt<bool>
Quentin Colombet4344da12014-04-11 21:51:09 +000078ExhaustiveSearch("exhaustive-register-search", cl::NotHidden,
Quentin Colombet567e30b2014-04-11 21:39:44 +000079 cl::desc("Exhaustive Search for registers bypassing the depth "
80 "and interference cutoffs of last chance recoloring"));
81
Quentin Colombete1a36632014-07-01 14:08:37 +000082static cl::opt<bool> EnableLocalReassignment(
83 "enable-local-reassign", cl::Hidden,
84 cl::desc("Local reassignment can yield better allocation decisions, but "
85 "may be compile time intensive"),
86 cl::init(true));
87
Manman Ren78cf02a2014-03-25 00:16:25 +000088// FIXME: Find a good default for this flag and remove the flag.
89static cl::opt<unsigned>
90CSRFirstTimeCost("regalloc-csr-first-time-cost",
91 cl::desc("Cost for first time use of callee-saved register."),
92 cl::init(0), cl::Hidden);
93
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +000094static RegisterRegAlloc greedyRegAlloc("greedy", "greedy register allocator",
95 createGreedyRegisterAllocator);
96
97namespace {
Jakob Stoklund Olesen8e089642011-03-09 00:57:29 +000098class RAGreedy : public MachineFunctionPass,
99 public RegAllocBase,
100 private LiveRangeEdit::Delegate {
Quentin Colombet87769712014-02-05 22:13:59 +0000101 // Convenient shortcuts.
102 typedef std::priority_queue<std::pair<unsigned, unsigned> > PQueue;
103 typedef SmallPtrSet<LiveInterval *, 4> SmallLISet;
104 typedef SmallSet<unsigned, 16> SmallVirtRegSet;
Jakob Stoklund Olesen8e089642011-03-09 00:57:29 +0000105
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +0000106 // context
107 MachineFunction *MF;
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +0000108
Quentin Colombet1fb3362a2014-01-02 22:47:22 +0000109 // Shortcuts to some useful interface.
110 const TargetInstrInfo *TII;
111 const TargetRegisterInfo *TRI;
112 RegisterClassInfo RCI;
113
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +0000114 // analyses
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000115 SlotIndexes *Indexes;
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000116 MachineBlockFrequencyInfo *MBFI;
Jakob Stoklund Olesen1740e002010-12-17 23:16:32 +0000117 MachineDominatorTree *DomTree;
Jakob Stoklund Olesene7601e92010-12-15 23:46:13 +0000118 MachineLoopInfo *Loops;
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000119 EdgeBundles *Bundles;
120 SpillPlacement *SpillPlacer;
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +0000121 LiveDebugVariables *DebugVars;
Jakob Stoklund Olesen1740e002010-12-17 23:16:32 +0000122
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +0000123 // state
Ahmed Charles56440fd2014-03-06 05:51:42 +0000124 std::unique_ptr<Spiller> SpillerInstance;
Quentin Colombet87769712014-02-05 22:13:59 +0000125 PQueue Queue;
Jakob Stoklund Olesen30a85632011-07-02 01:37:09 +0000126 unsigned NextCascade;
Jakob Stoklund Olesen5f9f0812011-03-01 21:10:07 +0000127
128 // Live ranges pass through a number of stages as we try to allocate them.
129 // Some of the stages may also create new live ranges:
130 //
131 // - Region splitting.
132 // - Per-block splitting.
133 // - Local splitting.
134 // - Spilling.
135 //
136 // Ranges produced by one of the stages skip the previous stages when they are
137 // dequeued. This improves performance because we can skip interference checks
138 // that are unlikely to give any results. It also guarantees that the live
139 // range splitting algorithm terminates, something that is otherwise hard to
140 // ensure.
141 enum LiveRangeStage {
Jakob Stoklund Olesen3ef8cf12011-07-25 15:25:41 +0000142 /// Newly created live range that has never been queued.
143 RS_New,
144
145 /// Only attempt assignment and eviction. Then requeue as RS_Split.
146 RS_Assign,
147
148 /// Attempt live range splitting if assignment is impossible.
149 RS_Split,
150
Jakob Stoklund Olesen45011172011-07-25 15:25:43 +0000151 /// Attempt more aggressive live range splitting that is guaranteed to make
152 /// progress. This is used for split products that may not be making
153 /// progress.
154 RS_Split2,
155
Jakob Stoklund Olesen3ef8cf12011-07-25 15:25:41 +0000156 /// Live range will be spilled. No more splitting will be attempted.
157 RS_Spill,
158
159 /// There is nothing more we can do to this live range. Abort compilation
160 /// if it can't be assigned.
161 RS_Done
Jakob Stoklund Olesen5f9f0812011-03-01 21:10:07 +0000162 };
163
Quentin Colombet96bd2a12014-04-04 02:05:21 +0000164 // Enum CutOffStage to keep a track whether the register allocation failed
165 // because of the cutoffs encountered in last chance recoloring.
166 // Note: This is used as bitmask. New value should be next power of 2.
167 enum CutOffStage {
168 // No cutoffs encountered
169 CO_None = 0,
170
171 // lcr-max-depth cutoff encountered
172 CO_Depth = 1,
173
174 // lcr-max-interf cutoff encountered
175 CO_Interf = 2
176 };
177
178 uint8_t CutOffInfo;
179
Eli Friedman78bffa52013-09-10 23:18:14 +0000180#ifndef NDEBUG
Jakob Stoklund Olesen25d57452011-05-25 23:58:36 +0000181 static const char *const StageName[];
Eli Friedman78bffa52013-09-10 23:18:14 +0000182#endif
Jakob Stoklund Olesen25d57452011-05-25 23:58:36 +0000183
Jakob Stoklund Olesen30a85632011-07-02 01:37:09 +0000184 // RegInfo - Keep additional information about each live range.
185 struct RegInfo {
186 LiveRangeStage Stage;
187
188 // Cascade - Eviction loop prevention. See canEvictInterference().
189 unsigned Cascade;
190
191 RegInfo() : Stage(RS_New), Cascade(0) {}
192 };
193
194 IndexedMap<RegInfo, VirtReg2IndexFunctor> ExtraRegInfo;
Jakob Stoklund Olesen5f9f0812011-03-01 21:10:07 +0000195
196 LiveRangeStage getStage(const LiveInterval &VirtReg) const {
Jakob Stoklund Olesen30a85632011-07-02 01:37:09 +0000197 return ExtraRegInfo[VirtReg.reg].Stage;
198 }
199
200 void setStage(const LiveInterval &VirtReg, LiveRangeStage Stage) {
201 ExtraRegInfo.resize(MRI->getNumVirtRegs());
202 ExtraRegInfo[VirtReg.reg].Stage = Stage;
Jakob Stoklund Olesen5f9f0812011-03-01 21:10:07 +0000203 }
204
205 template<typename Iterator>
206 void setStage(Iterator Begin, Iterator End, LiveRangeStage NewStage) {
Jakob Stoklund Olesen30a85632011-07-02 01:37:09 +0000207 ExtraRegInfo.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesendd9a2ec2011-03-30 02:52:39 +0000208 for (;Begin != End; ++Begin) {
Mark Laceyf9ea8852013-08-14 23:50:04 +0000209 unsigned Reg = *Begin;
Jakob Stoklund Olesen30a85632011-07-02 01:37:09 +0000210 if (ExtraRegInfo[Reg].Stage == RS_New)
211 ExtraRegInfo[Reg].Stage = NewStage;
Jakob Stoklund Olesendd9a2ec2011-03-30 02:52:39 +0000212 }
Jakob Stoklund Olesen5f9f0812011-03-01 21:10:07 +0000213 }
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +0000214
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000215 /// Cost of evicting interference.
216 struct EvictionCost {
217 unsigned BrokenHints; ///< Total number of broken hints.
218 float MaxWeight; ///< Maximum spill weight evicted.
219
Andrew Trick3621b8a2013-11-22 19:07:38 +0000220 EvictionCost(): BrokenHints(0), MaxWeight(0) {}
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000221
Andrew Trick84852572013-07-25 18:35:14 +0000222 bool isMax() const { return BrokenHints == ~0u; }
223
Andrew Trick3621b8a2013-11-22 19:07:38 +0000224 void setMax() { BrokenHints = ~0u; }
225
226 void setBrokenHints(unsigned NHints) { BrokenHints = NHints; }
227
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000228 bool operator<(const EvictionCost &O) const {
Benjamin Kramerb2f034b2014-03-03 19:58:30 +0000229 return std::tie(BrokenHints, MaxWeight) <
230 std::tie(O.BrokenHints, O.MaxWeight);
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000231 }
232 };
233
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000234 // splitting state.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000235 std::unique_ptr<SplitAnalysis> SA;
236 std::unique_ptr<SplitEditor> SE;
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000237
Jakob Stoklund Olesenca26e0a2011-04-02 06:03:38 +0000238 /// Cached per-block interference maps
239 InterferenceCache IntfCache;
240
Jakob Stoklund Olesen6d2bbc12011-04-07 17:27:46 +0000241 /// All basic blocks where the current register has uses.
Jakob Stoklund Olesen4b598e12011-03-05 01:10:31 +0000242 SmallVector<SpillPlacement::BlockConstraint, 8> SplitConstraints;
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000243
Jakob Stoklund Olesen4b598e12011-03-05 01:10:31 +0000244 /// Global live range splitting candidate info.
245 struct GlobalSplitCandidate {
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +0000246 // Register intended for assignment, or 0.
Jakob Stoklund Olesen4b598e12011-03-05 01:10:31 +0000247 unsigned PhysReg;
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +0000248
249 // SplitKit interval index for this candidate.
250 unsigned IntvIdx;
251
252 // Interference for PhysReg.
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +0000253 InterferenceCache::Cursor Intf;
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +0000254
255 // Bundles where this candidate should be live.
Jakob Stoklund Olesen4b598e12011-03-05 01:10:31 +0000256 BitVector LiveBundles;
Jakob Stoklund Olesenc49df2c2011-04-12 21:30:53 +0000257 SmallVector<unsigned, 8> ActiveBlocks;
258
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +0000259 void reset(InterferenceCache &Cache, unsigned Reg) {
Jakob Stoklund Olesenc49df2c2011-04-12 21:30:53 +0000260 PhysReg = Reg;
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +0000261 IntvIdx = 0;
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +0000262 Intf.setPhysReg(Cache, Reg);
Jakob Stoklund Olesenc49df2c2011-04-12 21:30:53 +0000263 LiveBundles.clear();
264 ActiveBlocks.clear();
265 }
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +0000266
267 // Set B[i] = C for every live bundle where B[i] was NoCand.
268 unsigned getBundles(SmallVectorImpl<unsigned> &B, unsigned C) {
269 unsigned Count = 0;
270 for (int i = LiveBundles.find_first(); i >= 0;
271 i = LiveBundles.find_next(i))
272 if (B[i] == NoCand) {
273 B[i] = C;
274 Count++;
275 }
276 return Count;
277 }
Jakob Stoklund Olesen4b598e12011-03-05 01:10:31 +0000278 };
279
Aditya Nandakumarc1fd0dd2013-11-19 23:51:32 +0000280 /// Candidate info for each PhysReg in AllocationOrder.
Jakob Stoklund Olesen4b598e12011-03-05 01:10:31 +0000281 /// This vector never shrinks, but grows to the size of the largest register
282 /// class.
283 SmallVector<GlobalSplitCandidate, 32> GlobalCand;
284
Alp Toker61007d82014-03-02 03:20:38 +0000285 enum : unsigned { NoCand = ~0u };
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +0000286
287 /// Candidate map. Each edge bundle is assigned to a GlobalCand entry, or to
288 /// NoCand which indicates the stack interval.
289 SmallVector<unsigned, 32> BundleCand;
290
Duncan P. N. Exon Smitha5df8132014-04-08 19:18:56 +0000291 /// Callee-save register cost, calculated once per machine function.
292 BlockFrequency CSRCost;
293
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +0000294public:
295 RAGreedy();
296
297 /// Return the pass name.
Craig Topper4584cd52014-03-07 09:26:03 +0000298 const char* getPassName() const override {
Jakob Stoklund Olesen92da7052010-12-11 00:19:56 +0000299 return "Greedy Register Allocator";
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +0000300 }
301
302 /// RAGreedy analysis usage.
Craig Topper4584cd52014-03-07 09:26:03 +0000303 void getAnalysisUsage(AnalysisUsage &AU) const override;
304 void releaseMemory() override;
305 Spiller &spiller() override { return *SpillerInstance; }
306 void enqueue(LiveInterval *LI) override;
307 LiveInterval *dequeue() override;
308 unsigned selectOrSplit(LiveInterval&, SmallVectorImpl<unsigned>&) override;
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +0000309
310 /// Perform register allocation.
Craig Topper4584cd52014-03-07 09:26:03 +0000311 bool runOnMachineFunction(MachineFunction &mf) override;
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +0000312
313 static char ID;
Andrew Trickccef0982010-12-09 18:15:21 +0000314
315private:
Quentin Colombet87769712014-02-05 22:13:59 +0000316 unsigned selectOrSplitImpl(LiveInterval &, SmallVectorImpl<unsigned> &,
317 SmallVirtRegSet &, unsigned = 0);
318
Craig Topper4584cd52014-03-07 09:26:03 +0000319 bool LRE_CanEraseVirtReg(unsigned) override;
320 void LRE_WillShrinkVirtReg(unsigned) override;
321 void LRE_DidCloneVirtReg(unsigned, unsigned) override;
Quentin Colombet87769712014-02-05 22:13:59 +0000322 void enqueue(PQueue &CurQueue, LiveInterval *LI);
323 LiveInterval *dequeue(PQueue &CurQueue);
Jakob Stoklund Olesen8e089642011-03-09 00:57:29 +0000324
Jakob Stoklund Olesenefeb3a12013-07-16 18:26:18 +0000325 BlockFrequency calcSpillCost();
326 bool addSplitConstraints(InterferenceCache::Cursor, BlockFrequency&);
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000327 void addThroughConstraints(InterferenceCache::Cursor, ArrayRef<unsigned>);
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +0000328 void growRegion(GlobalSplitCandidate &Cand);
Jakob Stoklund Olesenefeb3a12013-07-16 18:26:18 +0000329 BlockFrequency calcGlobalSplitCost(GlobalSplitCandidate&);
Jakob Stoklund Olesenecad62f2011-07-23 03:41:57 +0000330 bool calcCompactRegion(GlobalSplitCandidate&);
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +0000331 void splitAroundRegion(LiveRangeEdit&, ArrayRef<unsigned>);
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +0000332 void calcGapWeights(unsigned, SmallVectorImpl<float>&);
Andrew Trick8bb0a252013-07-25 18:35:19 +0000333 unsigned canReassign(LiveInterval &VirtReg, unsigned PhysReg);
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000334 bool shouldEvict(LiveInterval &A, bool, LiveInterval &B, bool);
335 bool canEvictInterference(LiveInterval&, unsigned, bool, EvictionCost&);
336 void evictInterference(LiveInterval&, unsigned,
Mark Laceyf9ea8852013-08-14 23:50:04 +0000337 SmallVectorImpl<unsigned>&);
Quentin Colombet87769712014-02-05 22:13:59 +0000338 bool mayRecolorAllInterferences(unsigned PhysReg, LiveInterval &VirtReg,
339 SmallLISet &RecoloringCandidates,
340 const SmallVirtRegSet &FixedRegisters);
Jakob Stoklund Olesen3d7b8062010-12-14 00:37:44 +0000341
Jakob Stoklund Olesen0e34c1d2011-04-20 18:19:48 +0000342 unsigned tryAssign(LiveInterval&, AllocationOrder&,
Mark Laceyf9ea8852013-08-14 23:50:04 +0000343 SmallVectorImpl<unsigned>&);
Jakob Stoklund Olesen6bd68cd2011-02-23 00:29:52 +0000344 unsigned tryEvict(LiveInterval&, AllocationOrder&,
Mark Laceyf9ea8852013-08-14 23:50:04 +0000345 SmallVectorImpl<unsigned>&, unsigned = ~0u);
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000346 unsigned tryRegionSplit(LiveInterval&, AllocationOrder&,
Mark Laceyf9ea8852013-08-14 23:50:04 +0000347 SmallVectorImpl<unsigned>&);
Manman Ren9db66b32014-03-24 23:23:42 +0000348 /// Calculate cost of region splitting.
349 unsigned calculateRegionSplitCost(LiveInterval &VirtReg,
350 AllocationOrder &Order,
351 BlockFrequency &BestCost,
Manman Ren78cf02a2014-03-25 00:16:25 +0000352 unsigned &NumCands, bool IgnoreCSR);
Manman Ren9db66b32014-03-24 23:23:42 +0000353 /// Perform region splitting.
354 unsigned doRegionSplit(LiveInterval &VirtReg, unsigned BestCand,
355 bool HasCompact,
356 SmallVectorImpl<unsigned> &NewVRegs);
Manman Ren9dee4492014-03-27 21:21:57 +0000357 /// Check other options before using a callee-saved register for the first
358 /// time.
359 unsigned tryAssignCSRFirstTime(LiveInterval &VirtReg, AllocationOrder &Order,
360 unsigned PhysReg, unsigned &CostPerUseLimit,
361 SmallVectorImpl<unsigned> &NewVRegs);
Duncan P. N. Exon Smitha5df8132014-04-08 19:18:56 +0000362 void initializeCSRCost();
Jakob Stoklund Olesencef5d8f2011-08-05 23:04:18 +0000363 unsigned tryBlockSplit(LiveInterval&, AllocationOrder&,
Mark Laceyf9ea8852013-08-14 23:50:04 +0000364 SmallVectorImpl<unsigned>&);
Jakob Stoklund Olesen0ce90492012-05-23 22:37:27 +0000365 unsigned tryInstructionSplit(LiveInterval&, AllocationOrder&,
Mark Laceyf9ea8852013-08-14 23:50:04 +0000366 SmallVectorImpl<unsigned>&);
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +0000367 unsigned tryLocalSplit(LiveInterval&, AllocationOrder&,
Mark Laceyf9ea8852013-08-14 23:50:04 +0000368 SmallVectorImpl<unsigned>&);
Jakob Stoklund Olesen3d7b8062010-12-14 00:37:44 +0000369 unsigned trySplit(LiveInterval&, AllocationOrder&,
Mark Laceyf9ea8852013-08-14 23:50:04 +0000370 SmallVectorImpl<unsigned>&);
Quentin Colombet87769712014-02-05 22:13:59 +0000371 unsigned tryLastChanceRecoloring(LiveInterval &, AllocationOrder &,
372 SmallVectorImpl<unsigned> &,
373 SmallVirtRegSet &, unsigned);
374 bool tryRecoloringCandidates(PQueue &, SmallVectorImpl<unsigned> &,
375 SmallVirtRegSet &, unsigned);
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +0000376};
377} // end anonymous namespace
378
379char RAGreedy::ID = 0;
380
Jakob Stoklund Olesen25d57452011-05-25 23:58:36 +0000381#ifndef NDEBUG
382const char *const RAGreedy::StageName[] = {
Jakob Stoklund Olesen3ef8cf12011-07-25 15:25:41 +0000383 "RS_New",
384 "RS_Assign",
385 "RS_Split",
Jakob Stoklund Olesen45011172011-07-25 15:25:43 +0000386 "RS_Split2",
Jakob Stoklund Olesen3ef8cf12011-07-25 15:25:41 +0000387 "RS_Spill",
388 "RS_Done"
Jakob Stoklund Olesen25d57452011-05-25 23:58:36 +0000389};
390#endif
391
Jakob Stoklund Olesen032891b2011-04-22 22:47:40 +0000392// Hysteresis to use when comparing floats.
393// This helps stabilize decisions based on float comparisons.
NAKAMURA Takumia71003a2014-02-04 06:29:38 +0000394const float Hysteresis = (2007 / 2048.0f); // 0.97998046875
Jakob Stoklund Olesen032891b2011-04-22 22:47:40 +0000395
396
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +0000397FunctionPass* llvm::createGreedyRegisterAllocator() {
398 return new RAGreedy();
399}
400
Jakob Stoklund Olesen30a85632011-07-02 01:37:09 +0000401RAGreedy::RAGreedy(): MachineFunctionPass(ID) {
Jakob Stoklund Olesen6aa0fbf2011-04-05 21:40:37 +0000402 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000403 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +0000404 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
405 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
Rafael Espindola676c4052011-06-26 22:34:10 +0000406 initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry());
Andrew Tricke1c034f2012-01-17 06:55:03 +0000407 initializeMachineSchedulerPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +0000408 initializeLiveStacksPass(*PassRegistry::getPassRegistry());
409 initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry());
410 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
411 initializeVirtRegMapPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000412 initializeLiveRegMatrixPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000413 initializeEdgeBundlesPass(*PassRegistry::getPassRegistry());
414 initializeSpillPlacementPass(*PassRegistry::getPassRegistry());
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +0000415}
416
417void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const {
418 AU.setPreservesCFG();
Benjamin Kramere2a1d892013-06-17 19:00:36 +0000419 AU.addRequired<MachineBlockFrequencyInfo>();
420 AU.addPreserved<MachineBlockFrequencyInfo>();
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +0000421 AU.addRequired<AliasAnalysis>();
422 AU.addPreserved<AliasAnalysis>();
423 AU.addRequired<LiveIntervals>();
Jakob Stoklund Olesen12243122012-06-08 23:44:45 +0000424 AU.addPreserved<LiveIntervals>();
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000425 AU.addRequired<SlotIndexes>();
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +0000426 AU.addPreserved<SlotIndexes>();
Jakob Stoklund Olesen6aa0fbf2011-04-05 21:40:37 +0000427 AU.addRequired<LiveDebugVariables>();
428 AU.addPreserved<LiveDebugVariables>();
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +0000429 AU.addRequired<LiveStacks>();
430 AU.addPreserved<LiveStacks>();
Jakob Stoklund Olesen1740e002010-12-17 23:16:32 +0000431 AU.addRequired<MachineDominatorTree>();
432 AU.addPreserved<MachineDominatorTree>();
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +0000433 AU.addRequired<MachineLoopInfo>();
434 AU.addPreserved<MachineLoopInfo>();
435 AU.addRequired<VirtRegMap>();
436 AU.addPreserved<VirtRegMap>();
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000437 AU.addRequired<LiveRegMatrix>();
438 AU.addPreserved<LiveRegMatrix>();
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000439 AU.addRequired<EdgeBundles>();
440 AU.addRequired<SpillPlacement>();
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +0000441 MachineFunctionPass::getAnalysisUsage(AU);
442}
443
Jakob Stoklund Olesen8e089642011-03-09 00:57:29 +0000444
445//===----------------------------------------------------------------------===//
446// LiveRangeEdit delegate methods
447//===----------------------------------------------------------------------===//
448
Jakob Stoklund Olesen43a87502011-03-13 01:23:11 +0000449bool RAGreedy::LRE_CanEraseVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000450 if (VRM->hasPhys(VirtReg)) {
451 Matrix->unassign(LIS->getInterval(VirtReg));
Jakob Stoklund Olesen43a87502011-03-13 01:23:11 +0000452 return true;
453 }
454 // Unassigned virtreg is probably in the priority queue.
455 // RegAllocBase will erase it after dequeueing.
456 return false;
457}
Jakob Stoklund Olesen8e089642011-03-09 00:57:29 +0000458
Jakob Stoklund Olesene14b2b22011-03-16 22:56:16 +0000459void RAGreedy::LRE_WillShrinkVirtReg(unsigned VirtReg) {
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000460 if (!VRM->hasPhys(VirtReg))
Jakob Stoklund Olesene14b2b22011-03-16 22:56:16 +0000461 return;
462
463 // Register is assigned, put it back on the queue for reassignment.
464 LiveInterval &LI = LIS->getInterval(VirtReg);
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000465 Matrix->unassign(LI);
Jakob Stoklund Olesene14b2b22011-03-16 22:56:16 +0000466 enqueue(&LI);
467}
468
Jakob Stoklund Olesendd9a2ec2011-03-30 02:52:39 +0000469void RAGreedy::LRE_DidCloneVirtReg(unsigned New, unsigned Old) {
Jakob Stoklund Olesen811b9c42011-09-14 17:34:37 +0000470 // Cloning a register we haven't even heard about yet? Just ignore it.
471 if (!ExtraRegInfo.inBounds(Old))
472 return;
473
Jakob Stoklund Olesendd9a2ec2011-03-30 02:52:39 +0000474 // LRE may clone a virtual register because dead code elimination causes it to
Jakob Stoklund Olesen5387bd32011-07-26 00:54:56 +0000475 // be split into connected components. The new components are much smaller
476 // than the original, so they should get a new chance at being assigned.
Jakob Stoklund Olesendd9a2ec2011-03-30 02:52:39 +0000477 // same stage as the parent.
Jakob Stoklund Olesen5387bd32011-07-26 00:54:56 +0000478 ExtraRegInfo[Old].Stage = RS_Assign;
Jakob Stoklund Olesen30a85632011-07-02 01:37:09 +0000479 ExtraRegInfo.grow(New);
480 ExtraRegInfo[New] = ExtraRegInfo[Old];
Jakob Stoklund Olesendd9a2ec2011-03-30 02:52:39 +0000481}
482
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +0000483void RAGreedy::releaseMemory() {
Craig Topperc0196b12014-04-14 00:51:57 +0000484 SpillerInstance.reset(nullptr);
Jakob Stoklund Olesen30a85632011-07-02 01:37:09 +0000485 ExtraRegInfo.clear();
Jakob Stoklund Olesenc49df2c2011-04-12 21:30:53 +0000486 GlobalCand.clear();
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +0000487}
488
Quentin Colombet87769712014-02-05 22:13:59 +0000489void RAGreedy::enqueue(LiveInterval *LI) { enqueue(Queue, LI); }
490
491void RAGreedy::enqueue(PQueue &CurQueue, LiveInterval *LI) {
Jakob Stoklund Olesen2329c542011-02-22 23:01:52 +0000492 // Prioritize live ranges by size, assigning larger ranges first.
493 // The queue holds (size, reg) pairs.
Jakob Stoklund Olesene68a27e2011-02-24 23:21:36 +0000494 const unsigned Size = LI->getSize();
495 const unsigned Reg = LI->reg;
Jakob Stoklund Olesen2329c542011-02-22 23:01:52 +0000496 assert(TargetRegisterInfo::isVirtualRegister(Reg) &&
497 "Can only enqueue virtual registers");
Jakob Stoklund Olesene68a27e2011-02-24 23:21:36 +0000498 unsigned Prio;
Jakob Stoklund Oleseneaa650a2010-12-08 22:57:16 +0000499
Jakob Stoklund Olesen30a85632011-07-02 01:37:09 +0000500 ExtraRegInfo.grow(Reg);
501 if (ExtraRegInfo[Reg].Stage == RS_New)
Jakob Stoklund Olesen3ef8cf12011-07-25 15:25:41 +0000502 ExtraRegInfo[Reg].Stage = RS_Assign;
Jakob Stoklund Olesendd9a2ec2011-03-30 02:52:39 +0000503
Jakob Stoklund Olesencad845f2011-07-28 20:48:23 +0000504 if (ExtraRegInfo[Reg].Stage == RS_Split) {
Jakob Stoklund Olesen28d79cd2011-03-27 22:49:21 +0000505 // Unsplit ranges that couldn't be allocated immediately are deferred until
Jakob Stoklund Olesen45df7e02011-09-12 16:54:42 +0000506 // everything else has been allocated.
507 Prio = Size;
Jakob Stoklund Olesencad845f2011-07-28 20:48:23 +0000508 } else {
Andrew Trick52a00932014-02-26 22:07:26 +0000509 // Giant live ranges fall back to the global assignment heuristic, which
510 // prevents excessive spilling in pathological cases.
511 bool ReverseLocal = TRI->reverseLocalAssignment();
Andrew Trickb1531e52014-02-27 21:37:33 +0000512 bool ForceGlobal = !ReverseLocal && TRI->mayOverrideLocalAssignment() &&
Andrew Trick52a00932014-02-26 22:07:26 +0000513 (Size / SlotIndex::InstrDist) > (2 * MRI->getRegClass(Reg)->getNumRegs());
514
515 if (ExtraRegInfo[Reg].Stage == RS_Assign && !ForceGlobal && !LI->empty() &&
Andrew Trick84852572013-07-25 18:35:14 +0000516 LIS->intervalIsInOneMBB(*LI)) {
517 // Allocate original local ranges in linear instruction order. Since they
518 // are singly defined, this produces optimal coloring in the absence of
519 // global interference and other constraints.
Andrew Trick52a00932014-02-26 22:07:26 +0000520 if (!ReverseLocal)
Andrew Trick2d8826a2013-12-11 03:40:15 +0000521 Prio = LI->beginIndex().getInstrDistance(Indexes->getLastIndex());
522 else {
523 // Allocating bottom up may allow many short LRGs to be assigned first
524 // to one of the cheap registers. This could be much faster for very
525 // large blocks on targets with many physical registers.
526 Prio = Indexes->getZeroIndex().getInstrDistance(LI->beginIndex());
527 }
Andrew Trick84852572013-07-25 18:35:14 +0000528 }
529 else {
530 // Allocate global and split ranges in long->short order. Long ranges that
531 // don't fit should be spilled (or split) ASAP so they don't create
532 // interference. Mark a bit to prioritize global above local ranges.
533 Prio = (1u << 29) + Size;
534 }
535 // Mark a higher bit to prioritize global and local above RS_Split.
536 Prio |= (1u << 31);
Jakob Stoklund Olesenb51f65c2011-02-23 00:56:56 +0000537
Jakob Stoklund Olesen28d79cd2011-03-27 22:49:21 +0000538 // Boost ranges that have a physical register hint.
Jakob Stoklund Olesen74052b02012-12-03 23:23:50 +0000539 if (VRM->hasKnownPreference(Reg))
Jakob Stoklund Olesen28d79cd2011-03-27 22:49:21 +0000540 Prio |= (1u << 30);
541 }
Andrew Trickf4b1ee32013-07-25 18:35:22 +0000542 // The virtual register number is a tie breaker for same-sized ranges.
543 // Give lower vreg numbers higher priority to assign them first.
Quentin Colombet87769712014-02-05 22:13:59 +0000544 CurQueue.push(std::make_pair(Prio, ~Reg));
Jakob Stoklund Oleseneaa650a2010-12-08 22:57:16 +0000545}
546
Quentin Colombet87769712014-02-05 22:13:59 +0000547LiveInterval *RAGreedy::dequeue() { return dequeue(Queue); }
548
549LiveInterval *RAGreedy::dequeue(PQueue &CurQueue) {
550 if (CurQueue.empty())
Craig Topperc0196b12014-04-14 00:51:57 +0000551 return nullptr;
Quentin Colombet87769712014-02-05 22:13:59 +0000552 LiveInterval *LI = &LIS->getInterval(~CurQueue.top().second);
553 CurQueue.pop();
Jakob Stoklund Olesen2329c542011-02-22 23:01:52 +0000554 return LI;
555}
Jakob Stoklund Olesen0acb69d2010-12-22 22:01:30 +0000556
Jakob Stoklund Olesen0e34c1d2011-04-20 18:19:48 +0000557
558//===----------------------------------------------------------------------===//
559// Direct Assignment
560//===----------------------------------------------------------------------===//
561
562/// tryAssign - Try to assign VirtReg to an available register.
563unsigned RAGreedy::tryAssign(LiveInterval &VirtReg,
564 AllocationOrder &Order,
Mark Laceyf9ea8852013-08-14 23:50:04 +0000565 SmallVectorImpl<unsigned> &NewVRegs) {
Jakob Stoklund Olesen0e34c1d2011-04-20 18:19:48 +0000566 Order.rewind();
567 unsigned PhysReg;
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000568 while ((PhysReg = Order.next()))
569 if (!Matrix->checkInterference(VirtReg, PhysReg))
Jakob Stoklund Olesen0e34c1d2011-04-20 18:19:48 +0000570 break;
Jakob Stoklund Olesen3cb2cb82012-12-04 22:25:16 +0000571 if (!PhysReg || Order.isHint())
Jakob Stoklund Olesen0e34c1d2011-04-20 18:19:48 +0000572 return PhysReg;
573
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000574 // PhysReg is available, but there may be a better choice.
575
576 // If we missed a simple hint, try to cheaply evict interference from the
577 // preferred register.
578 if (unsigned Hint = MRI->getSimpleHint(VirtReg.reg))
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000579 if (Order.isHint(Hint)) {
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000580 DEBUG(dbgs() << "missed hint " << PrintReg(Hint, TRI) << '\n');
Andrew Trick3621b8a2013-11-22 19:07:38 +0000581 EvictionCost MaxCost;
582 MaxCost.setBrokenHints(1);
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000583 if (canEvictInterference(VirtReg, Hint, true, MaxCost)) {
584 evictInterference(VirtReg, Hint, NewVRegs);
585 return Hint;
586 }
587 }
588
589 // Try to evict interference from a cheaper alternative.
Jakob Stoklund Olesen0e34c1d2011-04-20 18:19:48 +0000590 unsigned Cost = TRI->getCostPerUse(PhysReg);
591
592 // Most registers have 0 additional cost.
593 if (!Cost)
594 return PhysReg;
595
596 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is available at cost " << Cost
597 << '\n');
598 unsigned CheapReg = tryEvict(VirtReg, Order, NewVRegs, Cost);
599 return CheapReg ? CheapReg : PhysReg;
600}
601
602
Jakob Stoklund Olesen0acb69d2010-12-22 22:01:30 +0000603//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen6bd68cd2011-02-23 00:29:52 +0000604// Interference eviction
605//===----------------------------------------------------------------------===//
606
Andrew Trick8bb0a252013-07-25 18:35:19 +0000607unsigned RAGreedy::canReassign(LiveInterval &VirtReg, unsigned PrevReg) {
608 AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo);
609 unsigned PhysReg;
610 while ((PhysReg = Order.next())) {
611 if (PhysReg == PrevReg)
612 continue;
613
614 MCRegUnitIterator Units(PhysReg, TRI);
615 for (; Units.isValid(); ++Units) {
616 // Instantiate a "subquery", not to be confused with the Queries array.
617 LiveIntervalUnion::Query subQ(&VirtReg, &Matrix->getLiveUnions()[*Units]);
618 if (subQ.checkInterference())
619 break;
620 }
621 // If no units have interference, break out with the current PhysReg.
622 if (!Units.isValid())
623 break;
624 }
625 if (PhysReg)
626 DEBUG(dbgs() << "can reassign: " << VirtReg << " from "
627 << PrintReg(PrevReg, TRI) << " to " << PrintReg(PhysReg, TRI)
628 << '\n');
629 return PhysReg;
630}
631
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000632/// shouldEvict - determine if A should evict the assigned live range B. The
633/// eviction policy defined by this function together with the allocation order
634/// defined by enqueue() decides which registers ultimately end up being split
635/// and spilled.
Jakob Stoklund Olesen25d57452011-05-25 23:58:36 +0000636///
Jakob Stoklund Olesen30a85632011-07-02 01:37:09 +0000637/// Cascade numbers are used to prevent infinite loops if this function is a
638/// cyclic relation.
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000639///
640/// @param A The live range to be assigned.
641/// @param IsHint True when A is about to be assigned to its preferred
642/// register.
643/// @param B The live range to be evicted.
644/// @param BreaksHint True when B is already assigned to its preferred register.
645bool RAGreedy::shouldEvict(LiveInterval &A, bool IsHint,
646 LiveInterval &B, bool BreaksHint) {
Jakob Stoklund Olesen45011172011-07-25 15:25:43 +0000647 bool CanSplit = getStage(B) < RS_Spill;
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000648
649 // Be fairly aggressive about following hints as long as the evictee can be
650 // split.
651 if (CanSplit && IsHint && !BreaksHint)
652 return true;
653
Andrew Trick059e8002013-11-22 19:07:42 +0000654 if (A.weight > B.weight) {
655 DEBUG(dbgs() << "should evict: " << B << " w= " << B.weight << '\n');
656 return true;
657 }
658 return false;
Jakob Stoklund Olesen25d57452011-05-25 23:58:36 +0000659}
660
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000661/// canEvictInterference - Return true if all interferences between VirtReg and
Manman Renfa32ca12014-02-25 19:47:15 +0000662/// PhysReg can be evicted.
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000663///
664/// @param VirtReg Live range that is about to be assigned.
665/// @param PhysReg Desired register for assignment.
Dmitri Gribenko881929c2012-09-12 16:59:47 +0000666/// @param IsHint True when PhysReg is VirtReg's preferred register.
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000667/// @param MaxCost Only look for cheaper candidates and update with new cost
668/// when returning true.
669/// @returns True when interference can be evicted cheaper than MaxCost.
Jakob Stoklund Olesen6bd68cd2011-02-23 00:29:52 +0000670bool RAGreedy::canEvictInterference(LiveInterval &VirtReg, unsigned PhysReg,
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000671 bool IsHint, EvictionCost &MaxCost) {
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000672 // It is only possible to evict virtual register interference.
673 if (Matrix->checkInterference(VirtReg, PhysReg) > LiveRegMatrix::IK_VirtReg)
674 return false;
675
Andrew Trick84852572013-07-25 18:35:14 +0000676 bool IsLocal = LIS->intervalIsInOneMBB(VirtReg);
677
Jakob Stoklund Olesen30a85632011-07-02 01:37:09 +0000678 // Find VirtReg's cascade number. This will be unassigned if VirtReg was never
679 // involved in an eviction before. If a cascade number was assigned, deny
680 // evicting anything with the same or a newer cascade number. This prevents
681 // infinite eviction loops.
682 //
683 // This works out so a register without a cascade number is allowed to evict
684 // anything, and it can be evicted by anything.
685 unsigned Cascade = ExtraRegInfo[VirtReg.reg].Cascade;
686 if (!Cascade)
687 Cascade = NextCascade;
688
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000689 EvictionCost Cost;
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000690 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
691 LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units);
Jakob Stoklund Olesen0f175eb2011-04-11 21:47:01 +0000692 // If there is 10 or more interferences, chances are one is heavier.
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000693 if (Q.collectInterferingVRegs(10) >= 10)
Jakob Stoklund Olesen6bd68cd2011-02-23 00:29:52 +0000694 return false;
695
Jakob Stoklund Olesen0f175eb2011-04-11 21:47:01 +0000696 // Check if any interfering live range is heavier than MaxWeight.
697 for (unsigned i = Q.interferingVRegs().size(); i; --i) {
698 LiveInterval *Intf = Q.interferingVRegs()[i - 1];
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000699 assert(TargetRegisterInfo::isVirtualRegister(Intf->reg) &&
700 "Only expecting virtual register interference from query");
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000701 // Never evict spill products. They cannot split or spill.
Jakob Stoklund Olesen3ef8cf12011-07-25 15:25:41 +0000702 if (getStage(*Intf) == RS_Done)
Jakob Stoklund Olesen30a85632011-07-02 01:37:09 +0000703 return false;
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000704 // Once a live range becomes small enough, it is urgent that we find a
705 // register for it. This is indicated by an infinite spill weight. These
706 // urgent live ranges get to evict almost anything.
Jakob Stoklund Olesen05e22452012-05-30 21:46:58 +0000707 //
708 // Also allow urgent evictions of unspillable ranges from a strictly
709 // larger allocation order.
710 bool Urgent = !VirtReg.isSpillable() &&
711 (Intf->isSpillable() ||
712 RegClassInfo.getNumAllocatableRegs(MRI->getRegClass(VirtReg.reg)) <
713 RegClassInfo.getNumAllocatableRegs(MRI->getRegClass(Intf->reg)));
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000714 // Only evict older cascades or live ranges without a cascade.
715 unsigned IntfCascade = ExtraRegInfo[Intf->reg].Cascade;
716 if (Cascade <= IntfCascade) {
717 if (!Urgent)
718 return false;
719 // We permit breaking cascades for urgent evictions. It should be the
720 // last resort, though, so make it really expensive.
721 Cost.BrokenHints += 10;
722 }
723 // Would this break a satisfied hint?
724 bool BreaksHint = VRM->hasPreferredPhys(Intf->reg);
725 // Update eviction cost.
726 Cost.BrokenHints += BreaksHint;
727 Cost.MaxWeight = std::max(Cost.MaxWeight, Intf->weight);
728 // Abort if this would be too expensive.
729 if (!(Cost < MaxCost))
Jakob Stoklund Olesen6bd68cd2011-02-23 00:29:52 +0000730 return false;
Andrew Trick84852572013-07-25 18:35:14 +0000731 if (Urgent)
732 continue;
Andrew Trickc2ab53a2013-11-29 23:49:38 +0000733 // Apply the eviction policy for non-urgent evictions.
734 if (!shouldEvict(VirtReg, IsHint, *Intf, BreaksHint))
735 return false;
Andrew Trick84852572013-07-25 18:35:14 +0000736 // If !MaxCost.isMax(), then we're just looking for a cheap register.
737 // Evicting another local live range in this case could lead to suboptimal
738 // coloring.
Andrew Trick8bb0a252013-07-25 18:35:19 +0000739 if (!MaxCost.isMax() && IsLocal && LIS->intervalIsInOneMBB(*Intf) &&
Quentin Colombete1a36632014-07-01 14:08:37 +0000740 (!EnableLocalReassignment || !canReassign(*Intf, PhysReg))) {
Andrew Trick84852572013-07-25 18:35:14 +0000741 return false;
Andrew Trick8bb0a252013-07-25 18:35:19 +0000742 }
Jakob Stoklund Olesen1305bc02011-02-09 01:14:03 +0000743 }
744 }
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000745 MaxCost = Cost;
Jakob Stoklund Olesen6bd68cd2011-02-23 00:29:52 +0000746 return true;
747}
Jakob Stoklund Olesen1305bc02011-02-09 01:14:03 +0000748
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000749/// evictInterference - Evict any interferring registers that prevent VirtReg
750/// from being assigned to Physreg. This assumes that canEvictInterference
751/// returned true.
752void RAGreedy::evictInterference(LiveInterval &VirtReg, unsigned PhysReg,
Mark Laceyf9ea8852013-08-14 23:50:04 +0000753 SmallVectorImpl<unsigned> &NewVRegs) {
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000754 // Make sure that VirtReg has a cascade number, and assign that cascade
755 // number to every evicted register. These live ranges than then only be
756 // evicted by a newer cascade, preventing infinite loops.
757 unsigned Cascade = ExtraRegInfo[VirtReg.reg].Cascade;
758 if (!Cascade)
759 Cascade = ExtraRegInfo[VirtReg.reg].Cascade = NextCascade++;
760
761 DEBUG(dbgs() << "evicting " << PrintReg(PhysReg, TRI)
762 << " interference: Cascade " << Cascade << '\n');
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000763
764 // Collect all interfering virtregs first.
765 SmallVector<LiveInterval*, 8> Intfs;
766 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
767 LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units);
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000768 assert(Q.seenAllInterferences() && "Didn't check all interfererences.");
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +0000769 ArrayRef<LiveInterval*> IVR = Q.interferingVRegs();
770 Intfs.append(IVR.begin(), IVR.end());
771 }
772
773 // Evict them second. This will invalidate the queries.
774 for (unsigned i = 0, e = Intfs.size(); i != e; ++i) {
775 LiveInterval *Intf = Intfs[i];
776 // The same VirtReg may be present in multiple RegUnits. Skip duplicates.
777 if (!VRM->hasPhys(Intf->reg))
778 continue;
779 Matrix->unassign(*Intf);
780 assert((ExtraRegInfo[Intf->reg].Cascade < Cascade ||
781 VirtReg.isSpillable() < Intf->isSpillable()) &&
782 "Cannot decrease cascade number, illegal eviction");
783 ExtraRegInfo[Intf->reg].Cascade = Cascade;
784 ++NumEvicted;
Mark Laceyf9ea8852013-08-14 23:50:04 +0000785 NewVRegs.push_back(Intf->reg);
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000786 }
787}
788
Jakob Stoklund Olesen6bd68cd2011-02-23 00:29:52 +0000789/// tryEvict - Try to evict all interferences for a physreg.
Jakob Stoklund Olesene9cc8e92011-06-01 18:45:02 +0000790/// @param VirtReg Currently unassigned virtual register.
791/// @param Order Physregs to try.
792/// @return Physreg to assign VirtReg, or 0.
Jakob Stoklund Olesen6bd68cd2011-02-23 00:29:52 +0000793unsigned RAGreedy::tryEvict(LiveInterval &VirtReg,
794 AllocationOrder &Order,
Mark Laceyf9ea8852013-08-14 23:50:04 +0000795 SmallVectorImpl<unsigned> &NewVRegs,
Jakob Stoklund Olesen0e34c1d2011-04-20 18:19:48 +0000796 unsigned CostPerUseLimit) {
Jakob Stoklund Olesen6bd68cd2011-02-23 00:29:52 +0000797 NamedRegionTimer T("Evict", TimerGroupName, TimePassesIsEnabled);
798
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000799 // Keep track of the cheapest interference seen so far.
Andrew Trick3621b8a2013-11-22 19:07:38 +0000800 EvictionCost BestCost;
801 BestCost.setMax();
Jakob Stoklund Olesen6bd68cd2011-02-23 00:29:52 +0000802 unsigned BestPhys = 0;
Jakob Stoklund Olesen3dd236c2013-01-12 00:57:44 +0000803 unsigned OrderLimit = Order.getOrder().size();
Jakob Stoklund Olesen6bd68cd2011-02-23 00:29:52 +0000804
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000805 // When we are just looking for a reduced cost per use, don't break any
806 // hints, and only evict smaller spill weights.
807 if (CostPerUseLimit < ~0u) {
808 BestCost.BrokenHints = 0;
809 BestCost.MaxWeight = VirtReg.weight;
Jakob Stoklund Olesen3dd236c2013-01-12 00:57:44 +0000810
811 // Check of any registers in RC are below CostPerUseLimit.
812 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg.reg);
813 unsigned MinCost = RegClassInfo.getMinCost(RC);
814 if (MinCost >= CostPerUseLimit) {
815 DEBUG(dbgs() << RC->getName() << " minimum cost = " << MinCost
816 << ", no cheaper registers to be found.\n");
817 return 0;
818 }
819
820 // It is normal for register classes to have a long tail of registers with
821 // the same cost. We don't need to look at them if they're too expensive.
822 if (TRI->getCostPerUse(Order.getOrder().back()) >= CostPerUseLimit) {
823 OrderLimit = RegClassInfo.getLastCostChange(RC);
824 DEBUG(dbgs() << "Only trying the first " << OrderLimit << " regs.\n");
825 }
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000826 }
827
Jakob Stoklund Olesen6bd68cd2011-02-23 00:29:52 +0000828 Order.rewind();
Aditya Nandakumar73f3d332013-12-05 21:18:40 +0000829 while (unsigned PhysReg = Order.next(OrderLimit)) {
Jakob Stoklund Olesen0e34c1d2011-04-20 18:19:48 +0000830 if (TRI->getCostPerUse(PhysReg) >= CostPerUseLimit)
831 continue;
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000832 // The first use of a callee-saved register in a function has cost 1.
833 // Don't start using a CSR when the CostPerUseLimit is low.
834 if (CostPerUseLimit == 1)
835 if (unsigned CSR = RegClassInfo.getLastCalleeSavedAlias(PhysReg))
836 if (!MRI->isPhysRegUsed(CSR)) {
837 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " would clobber CSR "
838 << PrintReg(CSR, TRI) << '\n');
839 continue;
840 }
Jakob Stoklund Olesen0e34c1d2011-04-20 18:19:48 +0000841
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000842 if (!canEvictInterference(VirtReg, PhysReg, false, BestCost))
Jakob Stoklund Olesen6bd68cd2011-02-23 00:29:52 +0000843 continue;
844
845 // Best so far.
846 BestPhys = PhysReg;
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000847
Jakob Stoklund Olesen9918b332011-02-25 01:04:22 +0000848 // Stop if the hint can be used.
Jakob Stoklund Olesen3cb2cb82012-12-04 22:25:16 +0000849 if (Order.isHint())
Jakob Stoklund Olesen9918b332011-02-25 01:04:22 +0000850 break;
Jakob Stoklund Olesen1305bc02011-02-09 01:14:03 +0000851 }
852
Jakob Stoklund Olesen6bd68cd2011-02-23 00:29:52 +0000853 if (!BestPhys)
854 return 0;
855
Jakob Stoklund Olesen4931bbc2011-07-08 20:46:18 +0000856 evictInterference(VirtReg, BestPhys, NewVRegs);
Jakob Stoklund Olesen6bd68cd2011-02-23 00:29:52 +0000857 return BestPhys;
Andrew Trickccef0982010-12-09 18:15:21 +0000858}
859
Jakob Stoklund Olesen0acb69d2010-12-22 22:01:30 +0000860
861//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000862// Region Splitting
863//===----------------------------------------------------------------------===//
864
Jakob Stoklund Olesen81439a82011-04-06 21:32:38 +0000865/// addSplitConstraints - Fill out the SplitConstraints vector based on the
866/// interference pattern in Physreg and its aliases. Add the constraints to
867/// SpillPlacement and return the static cost of this split in Cost, assuming
868/// that all preferences in SplitConstraints are met.
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000869/// Return false if there are no bundles with positive bias.
870bool RAGreedy::addSplitConstraints(InterferenceCache::Cursor Intf,
Jakob Stoklund Olesenefeb3a12013-07-16 18:26:18 +0000871 BlockFrequency &Cost) {
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000872 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
Jakob Stoklund Olesenca26e0a2011-04-02 06:03:38 +0000873
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000874 // Reset interference dependent info.
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000875 SplitConstraints.resize(UseBlocks.size());
Jakob Stoklund Olesenefeb3a12013-07-16 18:26:18 +0000876 BlockFrequency StaticCost = 0;
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000877 for (unsigned i = 0; i != UseBlocks.size(); ++i) {
878 const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
Jakob Stoklund Olesen4b598e12011-03-05 01:10:31 +0000879 SpillPlacement::BlockConstraint &BC = SplitConstraints[i];
Jakob Stoklund Olesen4b598e12011-03-05 01:10:31 +0000880
Jakob Stoklund Olesenb1b76ad2011-02-09 22:50:26 +0000881 BC.Number = BI.MBB->getNumber();
Jakob Stoklund Olesenca26e0a2011-04-02 06:03:38 +0000882 Intf.moveToBlock(BC.Number);
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000883 BC.Entry = BI.LiveIn ? SpillPlacement::PrefReg : SpillPlacement::DontCare;
884 BC.Exit = BI.LiveOut ? SpillPlacement::PrefReg : SpillPlacement::DontCare;
David Blaikie041f1aa2013-05-15 07:36:59 +0000885 BC.ChangesValue = BI.FirstDef.isValid();
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000886
Jakob Stoklund Olesenca26e0a2011-04-02 06:03:38 +0000887 if (!Intf.hasInterference())
888 continue;
889
Jakob Stoklund Olesen4b598e12011-03-05 01:10:31 +0000890 // Number of spill code instructions to insert.
891 unsigned Ins = 0;
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000892
Jakob Stoklund Olesen4b598e12011-03-05 01:10:31 +0000893 // Interference for the live-in value.
Jakob Stoklund Olesenca26e0a2011-04-02 06:03:38 +0000894 if (BI.LiveIn) {
Jakob Stoklund Olesen89339072011-04-04 15:32:15 +0000895 if (Intf.first() <= Indexes->getMBBStartIdx(BC.Number))
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000896 BC.Entry = SpillPlacement::MustSpill, ++Ins;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000897 else if (Intf.first() < BI.FirstInstr)
Jakob Stoklund Olesen4b598e12011-03-05 01:10:31 +0000898 BC.Entry = SpillPlacement::PrefSpill, ++Ins;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000899 else if (Intf.first() < BI.LastInstr)
Jakob Stoklund Olesen4b598e12011-03-05 01:10:31 +0000900 ++Ins;
Jakob Stoklund Olesenf248b202011-02-08 23:02:58 +0000901 }
902
Jakob Stoklund Olesen4b598e12011-03-05 01:10:31 +0000903 // Interference for the live-out value.
Jakob Stoklund Olesenca26e0a2011-04-02 06:03:38 +0000904 if (BI.LiveOut) {
Jakob Stoklund Olesend93b0e32011-04-05 04:20:29 +0000905 if (Intf.last() >= SA->getLastSplitPoint(BC.Number))
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000906 BC.Exit = SpillPlacement::MustSpill, ++Ins;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000907 else if (Intf.last() > BI.LastInstr)
Jakob Stoklund Olesen4b598e12011-03-05 01:10:31 +0000908 BC.Exit = SpillPlacement::PrefSpill, ++Ins;
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +0000909 else if (Intf.last() > BI.FirstInstr)
Jakob Stoklund Olesen4b598e12011-03-05 01:10:31 +0000910 ++Ins;
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000911 }
Jakob Stoklund Olesen4b598e12011-03-05 01:10:31 +0000912
913 // Accumulate the total frequency of inserted spill code.
Jakob Stoklund Olesenefeb3a12013-07-16 18:26:18 +0000914 while (Ins--)
915 StaticCost += SpillPlacer->getBlockFrequency(BC.Number);
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000916 }
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000917 Cost = StaticCost;
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000918
Jakob Stoklund Olesen81439a82011-04-06 21:32:38 +0000919 // Add constraints for use-blocks. Note that these are the only constraints
920 // that may add a positive bias, it is downhill from here.
921 SpillPlacer->addConstraints(SplitConstraints);
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000922 return SpillPlacer->scanActiveBundles();
923}
Jakob Stoklund Olesen81439a82011-04-06 21:32:38 +0000924
Jakob Stoklund Olesen81439a82011-04-06 21:32:38 +0000925
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000926/// addThroughConstraints - Add constraints and links to SpillPlacer from the
927/// live-through blocks in Blocks.
928void RAGreedy::addThroughConstraints(InterferenceCache::Cursor Intf,
929 ArrayRef<unsigned> Blocks) {
Jakob Stoklund Olesen81439a82011-04-06 21:32:38 +0000930 const unsigned GroupSize = 8;
931 SpillPlacement::BlockConstraint BCS[GroupSize];
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000932 unsigned TBS[GroupSize];
933 unsigned B = 0, T = 0;
Jakob Stoklund Olesen81439a82011-04-06 21:32:38 +0000934
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000935 for (unsigned i = 0; i != Blocks.size(); ++i) {
936 unsigned Number = Blocks[i];
Jakob Stoklund Olesen81439a82011-04-06 21:32:38 +0000937 Intf.moveToBlock(Number);
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000938
Jakob Stoklund Olesen6d2bbc12011-04-07 17:27:46 +0000939 if (!Intf.hasInterference()) {
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000940 assert(T < GroupSize && "Array overflow");
941 TBS[T] = Number;
942 if (++T == GroupSize) {
Frits van Bommel717d7ed2011-07-18 12:00:32 +0000943 SpillPlacer->addLinks(makeArrayRef(TBS, T));
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000944 T = 0;
945 }
Jakob Stoklund Olesen6d2bbc12011-04-07 17:27:46 +0000946 continue;
Jakob Stoklund Olesen81439a82011-04-06 21:32:38 +0000947 }
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000948
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000949 assert(B < GroupSize && "Array overflow");
950 BCS[B].Number = Number;
951
Jakob Stoklund Olesen6d2bbc12011-04-07 17:27:46 +0000952 // Interference for the live-in value.
953 if (Intf.first() <= Indexes->getMBBStartIdx(Number))
954 BCS[B].Entry = SpillPlacement::MustSpill;
955 else
956 BCS[B].Entry = SpillPlacement::PrefSpill;
957
958 // Interference for the live-out value.
959 if (Intf.last() >= SA->getLastSplitPoint(Number))
960 BCS[B].Exit = SpillPlacement::MustSpill;
961 else
962 BCS[B].Exit = SpillPlacement::PrefSpill;
963
Jakob Stoklund Olesen81439a82011-04-06 21:32:38 +0000964 if (++B == GroupSize) {
965 ArrayRef<SpillPlacement::BlockConstraint> Array(BCS, B);
966 SpillPlacer->addConstraints(Array);
967 B = 0;
Jakob Stoklund Olesen81439a82011-04-06 21:32:38 +0000968 }
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +0000969 }
970
Jakob Stoklund Olesen81439a82011-04-06 21:32:38 +0000971 ArrayRef<SpillPlacement::BlockConstraint> Array(BCS, B);
972 SpillPlacer->addConstraints(Array);
Frits van Bommel717d7ed2011-07-18 12:00:32 +0000973 SpillPlacer->addLinks(makeArrayRef(TBS, T));
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +0000974}
975
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +0000976void RAGreedy::growRegion(GlobalSplitCandidate &Cand) {
Jakob Stoklund Olesenc49df2c2011-04-12 21:30:53 +0000977 // Keep track of through blocks that have not been added to SpillPlacer.
978 BitVector Todo = SA->getThroughBlocks();
979 SmallVectorImpl<unsigned> &ActiveBlocks = Cand.ActiveBlocks;
980 unsigned AddedTo = 0;
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000981#ifndef NDEBUG
982 unsigned Visited = 0;
983#endif
Jakob Stoklund Olesenc49df2c2011-04-12 21:30:53 +0000984
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000985 for (;;) {
986 ArrayRef<unsigned> NewBundles = SpillPlacer->getRecentPositive();
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000987 // Find new through blocks in the periphery of PrefRegBundles.
988 for (int i = 0, e = NewBundles.size(); i != e; ++i) {
989 unsigned Bundle = NewBundles[i];
990 // Look at all blocks connected to Bundle in the full graph.
991 ArrayRef<unsigned> Blocks = Bundles->getBlocks(Bundle);
992 for (ArrayRef<unsigned>::iterator I = Blocks.begin(), E = Blocks.end();
993 I != E; ++I) {
994 unsigned Block = *I;
Jakob Stoklund Olesenc49df2c2011-04-12 21:30:53 +0000995 if (!Todo.test(Block))
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000996 continue;
Jakob Stoklund Olesenc49df2c2011-04-12 21:30:53 +0000997 Todo.reset(Block);
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +0000998 // This is a new through block. Add it to SpillPlacer later.
Jakob Stoklund Olesenc49df2c2011-04-12 21:30:53 +0000999 ActiveBlocks.push_back(Block);
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +00001000#ifndef NDEBUG
1001 ++Visited;
1002#endif
1003 }
1004 }
1005 // Any new blocks to add?
Jakob Stoklund Olesen91f3a302011-07-05 18:46:42 +00001006 if (ActiveBlocks.size() == AddedTo)
1007 break;
Jakob Stoklund Olesena953bf12011-07-23 03:22:33 +00001008
1009 // Compute through constraints from the interference, or assume that all
1010 // through blocks prefer spilling when forming compact regions.
1011 ArrayRef<unsigned> NewBlocks = makeArrayRef(ActiveBlocks).slice(AddedTo);
1012 if (Cand.PhysReg)
1013 addThroughConstraints(Cand.Intf, NewBlocks);
1014 else
Jakob Stoklund Olesen86954522011-08-03 23:09:38 +00001015 // Provide a strong negative bias on through blocks to prevent unwanted
1016 // liveness on loop backedges.
1017 SpillPlacer->addPrefSpill(NewBlocks, /* Strong= */ true);
Jakob Stoklund Olesen91f3a302011-07-05 18:46:42 +00001018 AddedTo = ActiveBlocks.size();
1019
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +00001020 // Perhaps iterating can enable more bundles?
1021 SpillPlacer->iterate();
1022 }
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +00001023 DEBUG(dbgs() << ", v=" << Visited);
1024}
Jakob Stoklund Olesen4b598e12011-03-05 01:10:31 +00001025
Jakob Stoklund Olesenecad62f2011-07-23 03:41:57 +00001026/// calcCompactRegion - Compute the set of edge bundles that should be live
1027/// when splitting the current live range into compact regions. Compact
1028/// regions can be computed without looking at interference. They are the
1029/// regions formed by removing all the live-through blocks from the live range.
1030///
1031/// Returns false if the current live range is already compact, or if the
1032/// compact regions would form single block regions anyway.
1033bool RAGreedy::calcCompactRegion(GlobalSplitCandidate &Cand) {
1034 // Without any through blocks, the live range is already compact.
1035 if (!SA->getNumThroughBlocks())
1036 return false;
1037
1038 // Compact regions don't correspond to any physreg.
1039 Cand.reset(IntfCache, 0);
1040
1041 DEBUG(dbgs() << "Compact region bundles");
1042
1043 // Use the spill placer to determine the live bundles. GrowRegion pretends
1044 // that all the through blocks have interference when PhysReg is unset.
1045 SpillPlacer->prepare(Cand.LiveBundles);
1046
1047 // The static split cost will be zero since Cand.Intf reports no interference.
Jakob Stoklund Olesenefeb3a12013-07-16 18:26:18 +00001048 BlockFrequency Cost;
Jakob Stoklund Olesenecad62f2011-07-23 03:41:57 +00001049 if (!addSplitConstraints(Cand.Intf, Cost)) {
1050 DEBUG(dbgs() << ", none.\n");
1051 return false;
1052 }
1053
1054 growRegion(Cand);
1055 SpillPlacer->finish();
1056
1057 if (!Cand.LiveBundles.any()) {
1058 DEBUG(dbgs() << ", none.\n");
1059 return false;
1060 }
1061
1062 DEBUG({
1063 for (int i = Cand.LiveBundles.find_first(); i>=0;
1064 i = Cand.LiveBundles.find_next(i))
1065 dbgs() << " EB#" << i;
1066 dbgs() << ".\n";
1067 });
1068 return true;
1069}
1070
Jakob Stoklund Olesen032891b2011-04-22 22:47:40 +00001071/// calcSpillCost - Compute how expensive it would be to split the live range in
1072/// SA around all use blocks instead of forming bundle regions.
Jakob Stoklund Olesenefeb3a12013-07-16 18:26:18 +00001073BlockFrequency RAGreedy::calcSpillCost() {
1074 BlockFrequency Cost = 0;
Jakob Stoklund Olesen032891b2011-04-22 22:47:40 +00001075 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
1076 for (unsigned i = 0; i != UseBlocks.size(); ++i) {
1077 const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
1078 unsigned Number = BI.MBB->getNumber();
1079 // We normally only need one spill instruction - a load or a store.
1080 Cost += SpillPlacer->getBlockFrequency(Number);
1081
1082 // Unless the value is redefined in the block.
Jakob Stoklund Olesen3c145052011-08-02 23:04:08 +00001083 if (BI.LiveIn && BI.LiveOut && BI.FirstDef)
1084 Cost += SpillPlacer->getBlockFrequency(Number);
Jakob Stoklund Olesen032891b2011-04-22 22:47:40 +00001085 }
1086 return Cost;
1087}
1088
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +00001089/// calcGlobalSplitCost - Return the global split cost of following the split
1090/// pattern in LiveBundles. This cost should be added to the local cost of the
Jakob Stoklund Olesen4b598e12011-03-05 01:10:31 +00001091/// interference pattern in SplitConstraints.
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +00001092///
Jakob Stoklund Olesenefeb3a12013-07-16 18:26:18 +00001093BlockFrequency RAGreedy::calcGlobalSplitCost(GlobalSplitCandidate &Cand) {
1094 BlockFrequency GlobalCost = 0;
Jakob Stoklund Olesenc49df2c2011-04-12 21:30:53 +00001095 const BitVector &LiveBundles = Cand.LiveBundles;
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +00001096 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
1097 for (unsigned i = 0; i != UseBlocks.size(); ++i) {
1098 const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
Jakob Stoklund Olesen4b598e12011-03-05 01:10:31 +00001099 SpillPlacement::BlockConstraint &BC = SplitConstraints[i];
Jakob Stoklund Olesen1a9b66c2011-03-05 03:28:51 +00001100 bool RegIn = LiveBundles[Bundles->getBundle(BC.Number, 0)];
1101 bool RegOut = LiveBundles[Bundles->getBundle(BC.Number, 1)];
1102 unsigned Ins = 0;
1103
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +00001104 if (BI.LiveIn)
1105 Ins += RegIn != (BC.Entry == SpillPlacement::PrefReg);
1106 if (BI.LiveOut)
1107 Ins += RegOut != (BC.Exit == SpillPlacement::PrefReg);
Jakob Stoklund Olesenefeb3a12013-07-16 18:26:18 +00001108 while (Ins--)
1109 GlobalCost += SpillPlacer->getBlockFrequency(BC.Number);
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +00001110 }
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +00001111
Jakob Stoklund Olesenc49df2c2011-04-12 21:30:53 +00001112 for (unsigned i = 0, e = Cand.ActiveBlocks.size(); i != e; ++i) {
1113 unsigned Number = Cand.ActiveBlocks[i];
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +00001114 bool RegIn = LiveBundles[Bundles->getBundle(Number, 0)];
1115 bool RegOut = LiveBundles[Bundles->getBundle(Number, 1)];
Jakob Stoklund Olesen8ce2f432011-04-06 21:32:41 +00001116 if (!RegIn && !RegOut)
1117 continue;
1118 if (RegIn && RegOut) {
1119 // We need double spill code if this block has interference.
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +00001120 Cand.Intf.moveToBlock(Number);
Jakob Stoklund Olesenefeb3a12013-07-16 18:26:18 +00001121 if (Cand.Intf.hasInterference()) {
1122 GlobalCost += SpillPlacer->getBlockFrequency(Number);
1123 GlobalCost += SpillPlacer->getBlockFrequency(Number);
1124 }
Jakob Stoklund Olesen8ce2f432011-04-06 21:32:41 +00001125 continue;
1126 }
1127 // live-in / stack-out or stack-in live-out.
1128 GlobalCost += SpillPlacer->getBlockFrequency(Number);
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +00001129 }
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +00001130 return GlobalCost;
1131}
1132
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +00001133/// splitAroundRegion - Split the current live range around the regions
1134/// determined by BundleCand and GlobalCand.
Jakob Stoklund Olesen9fb04012011-01-19 22:11:48 +00001135///
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +00001136/// Before calling this function, GlobalCand and BundleCand must be initialized
1137/// so each bundle is assigned to a valid candidate, or NoCand for the
1138/// stack-bound bundles. The shared SA/SE SplitAnalysis and SplitEditor
1139/// objects must be initialized for the current live range, and intervals
1140/// created for the used candidates.
Jakob Stoklund Olesen9fb04012011-01-19 22:11:48 +00001141///
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +00001142/// @param LREdit The LiveRangeEdit object handling the current split.
1143/// @param UsedCands List of used GlobalCand entries. Every BundleCand value
1144/// must appear in this list.
1145void RAGreedy::splitAroundRegion(LiveRangeEdit &LREdit,
1146 ArrayRef<unsigned> UsedCands) {
1147 // These are the intervals created for new global ranges. We may create more
1148 // intervals for local ranges.
1149 const unsigned NumGlobalIntvs = LREdit.size();
1150 DEBUG(dbgs() << "splitAroundRegion with " << NumGlobalIntvs << " globals.\n");
1151 assert(NumGlobalIntvs && "No global intervals configured");
Jakob Stoklund Olesen9fb04012011-01-19 22:11:48 +00001152
Jakob Stoklund Olesen8627ea92011-08-05 22:20:45 +00001153 // Isolate even single instructions when dealing with a proper sub-class.
Jakob Stoklund Olesen22f37a12011-08-06 18:20:24 +00001154 // That guarantees register class inflation for the stack interval because it
Jakob Stoklund Olesen8627ea92011-08-05 22:20:45 +00001155 // is all copies.
1156 unsigned Reg = SA->getParent().reg;
1157 bool SingleInstrs = RegClassInfo.isProperSubClass(MRI->getRegClass(Reg));
1158
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +00001159 // First handle all the blocks with uses.
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +00001160 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
1161 for (unsigned i = 0; i != UseBlocks.size(); ++i) {
1162 const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +00001163 unsigned Number = BI.MBB->getNumber();
1164 unsigned IntvIn = 0, IntvOut = 0;
1165 SlotIndex IntfIn, IntfOut;
1166 if (BI.LiveIn) {
1167 unsigned CandIn = BundleCand[Bundles->getBundle(Number, 0)];
1168 if (CandIn != NoCand) {
1169 GlobalSplitCandidate &Cand = GlobalCand[CandIn];
1170 IntvIn = Cand.IntvIdx;
1171 Cand.Intf.moveToBlock(Number);
1172 IntfIn = Cand.Intf.first();
1173 }
1174 }
1175 if (BI.LiveOut) {
1176 unsigned CandOut = BundleCand[Bundles->getBundle(Number, 1)];
1177 if (CandOut != NoCand) {
1178 GlobalSplitCandidate &Cand = GlobalCand[CandOut];
1179 IntvOut = Cand.IntvIdx;
1180 Cand.Intf.moveToBlock(Number);
1181 IntfOut = Cand.Intf.last();
1182 }
1183 }
Jakob Stoklund Olesen9fb04012011-01-19 22:11:48 +00001184
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001185 // Create separate intervals for isolated blocks with multiple uses.
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +00001186 if (!IntvIn && !IntvOut) {
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001187 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " isolated.\n");
Jakob Stoklund Olesen8627ea92011-08-05 22:20:45 +00001188 if (SA->shouldSplitSingleBlock(BI, SingleInstrs))
Jakob Stoklund Olesenadc6a4c2011-06-30 01:30:39 +00001189 SE->splitSingleBlock(BI);
Jakob Stoklund Olesenc70b6972011-04-12 19:32:53 +00001190 continue;
1191 }
1192
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +00001193 if (IntvIn && IntvOut)
1194 SE->splitLiveThroughBlock(Number, IntvIn, IntfIn, IntvOut, IntfOut);
1195 else if (IntvIn)
1196 SE->splitRegInBlock(BI, IntvIn, IntfIn);
Jakob Stoklund Olesen795da1c2011-07-15 21:47:57 +00001197 else
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +00001198 SE->splitRegOutBlock(BI, IntvOut, IntfOut);
Jakob Stoklund Olesen9fb04012011-01-19 22:11:48 +00001199 }
1200
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +00001201 // Handle live-through blocks. The relevant live-through blocks are stored in
1202 // the ActiveBlocks list with each candidate. We need to filter out
1203 // duplicates.
1204 BitVector Todo = SA->getThroughBlocks();
1205 for (unsigned c = 0; c != UsedCands.size(); ++c) {
1206 ArrayRef<unsigned> Blocks = GlobalCand[UsedCands[c]].ActiveBlocks;
1207 for (unsigned i = 0, e = Blocks.size(); i != e; ++i) {
1208 unsigned Number = Blocks[i];
1209 if (!Todo.test(Number))
1210 continue;
1211 Todo.reset(Number);
1212
1213 unsigned IntvIn = 0, IntvOut = 0;
1214 SlotIndex IntfIn, IntfOut;
1215
1216 unsigned CandIn = BundleCand[Bundles->getBundle(Number, 0)];
1217 if (CandIn != NoCand) {
1218 GlobalSplitCandidate &Cand = GlobalCand[CandIn];
1219 IntvIn = Cand.IntvIdx;
1220 Cand.Intf.moveToBlock(Number);
1221 IntfIn = Cand.Intf.first();
1222 }
1223
1224 unsigned CandOut = BundleCand[Bundles->getBundle(Number, 1)];
1225 if (CandOut != NoCand) {
1226 GlobalSplitCandidate &Cand = GlobalCand[CandOut];
1227 IntvOut = Cand.IntvIdx;
1228 Cand.Intf.moveToBlock(Number);
1229 IntfOut = Cand.Intf.last();
1230 }
1231 if (!IntvIn && !IntvOut)
1232 continue;
1233 SE->splitLiveThroughBlock(Number, IntvIn, IntfIn, IntvOut, IntfOut);
1234 }
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +00001235 }
1236
Jakob Stoklund Olesen99827e82011-02-17 22:53:48 +00001237 ++NumGlobalSplits;
Jakob Stoklund Olesen9fb04012011-01-19 22:11:48 +00001238
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001239 SmallVector<unsigned, 8> IntvMap;
1240 SE->finish(&IntvMap);
Mark Laceyf9ea8852013-08-14 23:50:04 +00001241 DebugVars->splitRegister(Reg, LREdit.regs(), *LIS);
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00001242
Jakob Stoklund Olesen30a85632011-07-02 01:37:09 +00001243 ExtraRegInfo.resize(MRI->getNumVirtRegs());
Jakob Stoklund Olesen5cc91b22011-05-28 02:32:57 +00001244 unsigned OrigBlocks = SA->getNumLiveBlocks();
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001245
1246 // Sort out the new intervals created by splitting. We get four kinds:
1247 // - Remainder intervals should not be split again.
1248 // - Candidate intervals can be assigned to Cand.PhysReg.
1249 // - Block-local splits are candidates for local splitting.
1250 // - DCE leftovers should go back on the queue.
1251 for (unsigned i = 0, e = LREdit.size(); i != e; ++i) {
Mark Laceyf9ea8852013-08-14 23:50:04 +00001252 LiveInterval &Reg = LIS->getInterval(LREdit.get(i));
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001253
1254 // Ignore old intervals from DCE.
Jakob Stoklund Olesen30a85632011-07-02 01:37:09 +00001255 if (getStage(Reg) != RS_New)
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001256 continue;
1257
1258 // Remainder interval. Don't try splitting again, spill if it doesn't
1259 // allocate.
1260 if (IntvMap[i] == 0) {
Jakob Stoklund Olesen3ef8cf12011-07-25 15:25:41 +00001261 setStage(Reg, RS_Spill);
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001262 continue;
1263 }
1264
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +00001265 // Global intervals. Allow repeated splitting as long as the number of live
1266 // blocks is strictly decreasing.
1267 if (IntvMap[i] < NumGlobalIntvs) {
Jakob Stoklund Olesen30a85632011-07-02 01:37:09 +00001268 if (SA->countLiveBlocks(&Reg) >= OrigBlocks) {
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +00001269 DEBUG(dbgs() << "Main interval covers the same " << OrigBlocks
1270 << " blocks as original.\n");
1271 // Don't allow repeated splitting as a safe guard against looping.
Jakob Stoklund Olesen45011172011-07-25 15:25:43 +00001272 setStage(Reg, RS_Split2);
Jakob Stoklund Oleseneef23272011-04-26 22:33:12 +00001273 }
1274 continue;
1275 }
1276
1277 // Other intervals are treated as new. This includes local intervals created
1278 // for blocks with multiple uses, and anything created by DCE.
Jakob Stoklund Olesen6a663b82011-04-21 18:38:15 +00001279 }
1280
Jakob Stoklund Olesen28d79cd2011-03-27 22:49:21 +00001281 if (VerifyEnabled)
Jakob Stoklund Olesen9fb04012011-01-19 22:11:48 +00001282 MF->verify(this, "After splitting live range around region");
1283}
1284
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +00001285unsigned RAGreedy::tryRegionSplit(LiveInterval &VirtReg, AllocationOrder &Order,
Mark Laceyf9ea8852013-08-14 23:50:04 +00001286 SmallVectorImpl<unsigned> &NewVRegs) {
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +00001287 unsigned NumCands = 0;
Jakob Stoklund Olesenefeb3a12013-07-16 18:26:18 +00001288 BlockFrequency BestCost;
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +00001289
1290 // Check if we can split this live range around a compact region.
Jakob Stoklund Olesen45df7e02011-09-12 16:54:42 +00001291 bool HasCompact = calcCompactRegion(GlobalCand.front());
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +00001292 if (HasCompact) {
1293 // Yes, keep GlobalCand[0] as the compact region candidate.
1294 NumCands = 1;
Jakob Stoklund Olesenefeb3a12013-07-16 18:26:18 +00001295 BestCost = BlockFrequency::getMaxFrequency();
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +00001296 } else {
1297 // No benefit from the compact region, our fallback will be per-block
1298 // splitting. Make sure we find a solution that is cheaper than spilling.
Jakob Stoklund Olesenefeb3a12013-07-16 18:26:18 +00001299 BestCost = calcSpillCost();
Michael Gottesmanb78dec82013-12-14 00:25:45 +00001300 DEBUG(dbgs() << "Cost of isolating all blocks = ";
1301 MBFI->printBlockFreq(dbgs(), BestCost) << '\n');
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +00001302 }
Jakob Stoklund Olesen4b598e12011-03-05 01:10:31 +00001303
Manman Ren9db66b32014-03-24 23:23:42 +00001304 unsigned BestCand =
Manman Ren78cf02a2014-03-25 00:16:25 +00001305 calculateRegionSplitCost(VirtReg, Order, BestCost, NumCands,
1306 false/*IgnoreCSR*/);
Manman Ren9db66b32014-03-24 23:23:42 +00001307
1308 // No solutions found, fall back to single block splitting.
1309 if (!HasCompact && BestCand == NoCand)
1310 return 0;
1311
1312 return doRegionSplit(VirtReg, BestCand, HasCompact, NewVRegs);
1313}
1314
1315unsigned RAGreedy::calculateRegionSplitCost(LiveInterval &VirtReg,
1316 AllocationOrder &Order,
1317 BlockFrequency &BestCost,
Manman Ren78cf02a2014-03-25 00:16:25 +00001318 unsigned &NumCands,
1319 bool IgnoreCSR) {
Manman Ren9db66b32014-03-24 23:23:42 +00001320 unsigned BestCand = NoCand;
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +00001321 Order.rewind();
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +00001322 while (unsigned PhysReg = Order.next()) {
Manman Ren78cf02a2014-03-25 00:16:25 +00001323 if (unsigned CSR = RegClassInfo.getLastCalleeSavedAlias(PhysReg))
1324 if (IgnoreCSR && !MRI->isPhysRegUsed(CSR))
1325 continue;
1326
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +00001327 // Discard bad candidates before we run out of interference cache cursors.
1328 // This will only affect register classes with a lot of registers (>32).
1329 if (NumCands == IntfCache.getMaxCursors()) {
1330 unsigned WorstCount = ~0u;
1331 unsigned Worst = 0;
1332 for (unsigned i = 0; i != NumCands; ++i) {
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +00001333 if (i == BestCand || !GlobalCand[i].PhysReg)
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +00001334 continue;
1335 unsigned Count = GlobalCand[i].LiveBundles.count();
1336 if (Count < WorstCount)
1337 Worst = i, WorstCount = Count;
1338 }
1339 --NumCands;
1340 GlobalCand[Worst] = GlobalCand[NumCands];
Jakob Stoklund Olesen559d4dc2011-11-01 00:02:31 +00001341 if (BestCand == NumCands)
1342 BestCand = Worst;
Jakob Stoklund Olesena153ca52011-07-14 05:35:11 +00001343 }
1344
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +00001345 if (GlobalCand.size() <= NumCands)
1346 GlobalCand.resize(NumCands+1);
1347 GlobalSplitCandidate &Cand = GlobalCand[NumCands];
1348 Cand.reset(IntfCache, PhysReg);
Jakob Stoklund Olesen4b598e12011-03-05 01:10:31 +00001349
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +00001350 SpillPlacer->prepare(Cand.LiveBundles);
Jakob Stoklund Olesenefeb3a12013-07-16 18:26:18 +00001351 BlockFrequency Cost;
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +00001352 if (!addSplitConstraints(Cand.Intf, Cost)) {
Jakob Stoklund Olesened47ed42011-04-09 02:59:09 +00001353 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << "\tno positive bundles\n");
Jakob Stoklund Olesen81439a82011-04-06 21:32:38 +00001354 continue;
1355 }
Michael Gottesmanb78dec82013-12-14 00:25:45 +00001356 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << "\tstatic = ";
1357 MBFI->printBlockFreq(dbgs(), Cost));
Jakob Stoklund Olesen032891b2011-04-22 22:47:40 +00001358 if (Cost >= BestCost) {
1359 DEBUG({
1360 if (BestCand == NoCand)
1361 dbgs() << " worse than no bundles\n";
1362 else
1363 dbgs() << " worse than "
1364 << PrintReg(GlobalCand[BestCand].PhysReg, TRI) << '\n';
1365 });
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +00001366 continue;
Jakob Stoklund Olesen1a9b66c2011-03-05 03:28:51 +00001367 }
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +00001368 growRegion(Cand);
Jakob Stoklund Olesen9fb04012011-01-19 22:11:48 +00001369
Jakob Stoklund Olesen36b5d8a2011-04-06 19:13:57 +00001370 SpillPlacer->finish();
1371
Jakob Stoklund Olesen9fb04012011-01-19 22:11:48 +00001372 // No live bundles, defer to splitSingleBlocks().
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +00001373 if (!Cand.LiveBundles.any()) {
Jakob Stoklund Olesen1a9b66c2011-03-05 03:28:51 +00001374 DEBUG(dbgs() << " no bundles.\n");
Jakob Stoklund Olesen9fb04012011-01-19 22:11:48 +00001375 continue;
Jakob Stoklund Olesen1a9b66c2011-03-05 03:28:51 +00001376 }
Jakob Stoklund Olesen9fb04012011-01-19 22:11:48 +00001377
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +00001378 Cost += calcGlobalSplitCost(Cand);
Jakob Stoklund Olesen1a9b66c2011-03-05 03:28:51 +00001379 DEBUG({
Michael Gottesmanb78dec82013-12-14 00:25:45 +00001380 dbgs() << ", total = "; MBFI->printBlockFreq(dbgs(), Cost)
1381 << " with bundles";
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +00001382 for (int i = Cand.LiveBundles.find_first(); i>=0;
1383 i = Cand.LiveBundles.find_next(i))
Jakob Stoklund Olesen1a9b66c2011-03-05 03:28:51 +00001384 dbgs() << " EB#" << i;
1385 dbgs() << ".\n";
1386 });
Jakob Stoklund Olesen032891b2011-04-22 22:47:40 +00001387 if (Cost < BestCost) {
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +00001388 BestCand = NumCands;
Jakob Stoklund Olesenefeb3a12013-07-16 18:26:18 +00001389 BestCost = Cost;
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +00001390 }
Jakob Stoklund Olesend7e99372011-07-14 00:17:10 +00001391 ++NumCands;
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +00001392 }
Manman Ren9db66b32014-03-24 23:23:42 +00001393 return BestCand;
1394}
Jakob Stoklund Olesen9fb04012011-01-19 22:11:48 +00001395
Manman Ren9db66b32014-03-24 23:23:42 +00001396unsigned RAGreedy::doRegionSplit(LiveInterval &VirtReg, unsigned BestCand,
1397 bool HasCompact,
1398 SmallVectorImpl<unsigned> &NewVRegs) {
1399 SmallVector<unsigned, 8> UsedCands;
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +00001400 // Prepare split editor.
Jakob Stoklund Olesene5bbe372012-05-19 05:25:46 +00001401 LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this);
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +00001402 SE->reset(LREdit, SplitSpillMode);
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +00001403
1404 // Assign all edge bundles to the preferred candidate, or NoCand.
1405 BundleCand.assign(Bundles->getNumBundles(), NoCand);
1406
1407 // Assign bundles for the best candidate region.
1408 if (BestCand != NoCand) {
1409 GlobalSplitCandidate &Cand = GlobalCand[BestCand];
1410 if (unsigned B = Cand.getBundles(BundleCand, BestCand)) {
1411 UsedCands.push_back(BestCand);
1412 Cand.IntvIdx = SE->openIntv();
1413 DEBUG(dbgs() << "Split for " << PrintReg(Cand.PhysReg, TRI) << " in "
1414 << B << " bundles, intv " << Cand.IntvIdx << ".\n");
Chandler Carruth77eb5a02011-08-03 23:07:27 +00001415 (void)B;
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +00001416 }
1417 }
1418
1419 // Assign bundles for the compact region.
1420 if (HasCompact) {
1421 GlobalSplitCandidate &Cand = GlobalCand.front();
1422 assert(!Cand.PhysReg && "Compact region has no physreg");
1423 if (unsigned B = Cand.getBundles(BundleCand, 0)) {
1424 UsedCands.push_back(0);
1425 Cand.IntvIdx = SE->openIntv();
1426 DEBUG(dbgs() << "Split for compact region in " << B << " bundles, intv "
1427 << Cand.IntvIdx << ".\n");
Chandler Carruth77eb5a02011-08-03 23:07:27 +00001428 (void)B;
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +00001429 }
1430 }
1431
1432 splitAroundRegion(LREdit, UsedCands);
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +00001433 return 0;
1434}
1435
Jakob Stoklund Olesen9fb04012011-01-19 22:11:48 +00001436
1437//===----------------------------------------------------------------------===//
Jakob Stoklund Olesencef5d8f2011-08-05 23:04:18 +00001438// Per-Block Splitting
1439//===----------------------------------------------------------------------===//
1440
1441/// tryBlockSplit - Split a global live range around every block with uses. This
1442/// creates a lot of local live ranges, that will be split by tryLocalSplit if
1443/// they don't allocate.
1444unsigned RAGreedy::tryBlockSplit(LiveInterval &VirtReg, AllocationOrder &Order,
Mark Laceyf9ea8852013-08-14 23:50:04 +00001445 SmallVectorImpl<unsigned> &NewVRegs) {
Jakob Stoklund Olesencef5d8f2011-08-05 23:04:18 +00001446 assert(&SA->getParent() == &VirtReg && "Live range wasn't analyzed");
1447 unsigned Reg = VirtReg.reg;
1448 bool SingleInstrs = RegClassInfo.isProperSubClass(MRI->getRegClass(Reg));
Jakob Stoklund Olesene5bbe372012-05-19 05:25:46 +00001449 LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this);
Jakob Stoklund Oleseneecb2fb2011-09-12 16:49:21 +00001450 SE->reset(LREdit, SplitSpillMode);
Jakob Stoklund Olesencef5d8f2011-08-05 23:04:18 +00001451 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
1452 for (unsigned i = 0; i != UseBlocks.size(); ++i) {
1453 const SplitAnalysis::BlockInfo &BI = UseBlocks[i];
1454 if (SA->shouldSplitSingleBlock(BI, SingleInstrs))
1455 SE->splitSingleBlock(BI);
1456 }
1457 // No blocks were split.
1458 if (LREdit.empty())
1459 return 0;
1460
1461 // We did split for some blocks.
Jakob Stoklund Olesen02cf10b2011-08-05 23:50:31 +00001462 SmallVector<unsigned, 8> IntvMap;
1463 SE->finish(&IntvMap);
Jakob Stoklund Olesen0de95ef2011-08-05 23:10:40 +00001464
1465 // Tell LiveDebugVariables about the new ranges.
Mark Laceyf9ea8852013-08-14 23:50:04 +00001466 DebugVars->splitRegister(Reg, LREdit.regs(), *LIS);
Jakob Stoklund Olesen0de95ef2011-08-05 23:10:40 +00001467
Jakob Stoklund Olesen02cf10b2011-08-05 23:50:31 +00001468 ExtraRegInfo.resize(MRI->getNumVirtRegs());
1469
1470 // Sort out the new intervals created by splitting. The remainder interval
1471 // goes straight to spilling, the new local ranges get to stay RS_New.
1472 for (unsigned i = 0, e = LREdit.size(); i != e; ++i) {
Mark Laceyf9ea8852013-08-14 23:50:04 +00001473 LiveInterval &LI = LIS->getInterval(LREdit.get(i));
Jakob Stoklund Olesen02cf10b2011-08-05 23:50:31 +00001474 if (getStage(LI) == RS_New && IntvMap[i] == 0)
1475 setStage(LI, RS_Spill);
1476 }
1477
Jakob Stoklund Olesencef5d8f2011-08-05 23:04:18 +00001478 if (VerifyEnabled)
1479 MF->verify(this, "After splitting live range around basic blocks");
1480 return 0;
1481}
1482
Jakob Stoklund Olesen0ce90492012-05-23 22:37:27 +00001483
1484//===----------------------------------------------------------------------===//
1485// Per-Instruction Splitting
1486//===----------------------------------------------------------------------===//
1487
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001488/// Get the number of allocatable registers that match the constraints of \p Reg
1489/// on \p MI and that are also in \p SuperRC.
1490static unsigned getNumAllocatableRegsForConstraints(
1491 const MachineInstr *MI, unsigned Reg, const TargetRegisterClass *SuperRC,
1492 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI,
1493 const RegisterClassInfo &RCI) {
1494 assert(SuperRC && "Invalid register class");
1495
1496 const TargetRegisterClass *ConstrainedRC =
1497 MI->getRegClassConstraintEffectForVReg(Reg, SuperRC, TII, TRI,
1498 /* ExploreBundle */ true);
1499 if (!ConstrainedRC)
1500 return 0;
1501 return RCI.getNumAllocatableRegs(ConstrainedRC);
1502}
1503
Jakob Stoklund Olesen0ce90492012-05-23 22:37:27 +00001504/// tryInstructionSplit - Split a live range around individual instructions.
1505/// This is normally not worthwhile since the spiller is doing essentially the
1506/// same thing. However, when the live range is in a constrained register
1507/// class, it may help to insert copies such that parts of the live range can
1508/// be moved to a larger register class.
1509///
1510/// This is similar to spilling to a larger register class.
1511unsigned
1512RAGreedy::tryInstructionSplit(LiveInterval &VirtReg, AllocationOrder &Order,
Mark Laceyf9ea8852013-08-14 23:50:04 +00001513 SmallVectorImpl<unsigned> &NewVRegs) {
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001514 const TargetRegisterClass *CurRC = MRI->getRegClass(VirtReg.reg);
Jakob Stoklund Olesen0ce90492012-05-23 22:37:27 +00001515 // There is no point to this if there are no larger sub-classes.
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001516 if (!RegClassInfo.isProperSubClass(CurRC))
Jakob Stoklund Olesen0ce90492012-05-23 22:37:27 +00001517 return 0;
1518
1519 // Always enable split spill mode, since we're effectively spilling to a
1520 // register.
1521 LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this);
1522 SE->reset(LREdit, SplitEditor::SM_Size);
1523
1524 ArrayRef<SlotIndex> Uses = SA->getUseSlots();
1525 if (Uses.size() <= 1)
1526 return 0;
1527
1528 DEBUG(dbgs() << "Split around " << Uses.size() << " individual instrs.\n");
1529
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001530 const TargetRegisterClass *SuperRC = TRI->getLargestLegalSuperClass(CurRC);
1531 unsigned SuperRCNumAllocatableRegs = RCI.getNumAllocatableRegs(SuperRC);
1532 // Split around every non-copy instruction if this split will relax
1533 // the constraints on the virtual register.
1534 // Otherwise, splitting just inserts uncoalescable copies that do not help
1535 // the allocation.
Jakob Stoklund Olesen0ce90492012-05-23 22:37:27 +00001536 for (unsigned i = 0; i != Uses.size(); ++i) {
1537 if (const MachineInstr *MI = Indexes->getInstructionFromIndex(Uses[i]))
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00001538 if (MI->isFullCopy() ||
1539 SuperRCNumAllocatableRegs ==
1540 getNumAllocatableRegsForConstraints(MI, VirtReg.reg, SuperRC, TII,
1541 TRI, RCI)) {
Jakob Stoklund Olesen0ce90492012-05-23 22:37:27 +00001542 DEBUG(dbgs() << " skip:\t" << Uses[i] << '\t' << *MI);
1543 continue;
1544 }
1545 SE->openIntv();
1546 SlotIndex SegStart = SE->enterIntvBefore(Uses[i]);
1547 SlotIndex SegStop = SE->leaveIntvAfter(Uses[i]);
1548 SE->useIntv(SegStart, SegStop);
1549 }
1550
1551 if (LREdit.empty()) {
1552 DEBUG(dbgs() << "All uses were copies.\n");
1553 return 0;
1554 }
1555
1556 SmallVector<unsigned, 8> IntvMap;
1557 SE->finish(&IntvMap);
Mark Laceyf9ea8852013-08-14 23:50:04 +00001558 DebugVars->splitRegister(VirtReg.reg, LREdit.regs(), *LIS);
Jakob Stoklund Olesen0ce90492012-05-23 22:37:27 +00001559 ExtraRegInfo.resize(MRI->getNumVirtRegs());
1560
1561 // Assign all new registers to RS_Spill. This was the last chance.
1562 setStage(LREdit.begin(), LREdit.end(), RS_Spill);
1563 return 0;
1564}
1565
1566
Jakob Stoklund Olesencef5d8f2011-08-05 23:04:18 +00001567//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001568// Local Splitting
1569//===----------------------------------------------------------------------===//
1570
1571
1572/// calcGapWeights - Compute the maximum spill weight that needs to be evicted
1573/// in order to use PhysReg between two entries in SA->UseSlots.
1574///
1575/// GapWeight[i] represents the gap between UseSlots[i] and UseSlots[i+1].
1576///
1577void RAGreedy::calcGapWeights(unsigned PhysReg,
1578 SmallVectorImpl<float> &GapWeight) {
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +00001579 assert(SA->getUseBlocks().size() == 1 && "Not a local interval");
1580 const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front();
Jakob Stoklund Olesen994fed62012-01-12 17:53:44 +00001581 ArrayRef<SlotIndex> Uses = SA->getUseSlots();
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001582 const unsigned NumGaps = Uses.size()-1;
1583
1584 // Start and end points for the interference check.
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001585 SlotIndex StartIdx =
1586 BI.LiveIn ? BI.FirstInstr.getBaseIndex() : BI.FirstInstr;
1587 SlotIndex StopIdx =
1588 BI.LiveOut ? BI.LastInstr.getBoundaryIndex() : BI.LastInstr;
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001589
1590 GapWeight.assign(NumGaps, 0.0f);
1591
1592 // Add interference from each overlapping register.
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +00001593 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
1594 if (!Matrix->query(const_cast<LiveInterval&>(SA->getParent()), *Units)
1595 .checkInterference())
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001596 continue;
1597
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001598 // We know that VirtReg is a continuous interval from FirstInstr to
1599 // LastInstr, so we don't need InterferenceQuery.
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001600 //
1601 // Interference that overlaps an instruction is counted in both gaps
1602 // surrounding the instruction. The exception is interference before
1603 // StartIdx and after StopIdx.
1604 //
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +00001605 LiveIntervalUnion::SegmentIter IntI =
1606 Matrix->getLiveUnions()[*Units] .find(StartIdx);
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001607 for (unsigned Gap = 0; IntI.valid() && IntI.start() < StopIdx; ++IntI) {
1608 // Skip the gaps before IntI.
1609 while (Uses[Gap+1].getBoundaryIndex() < IntI.start())
1610 if (++Gap == NumGaps)
1611 break;
1612 if (Gap == NumGaps)
1613 break;
1614
1615 // Update the gaps covered by IntI.
1616 const float weight = IntI.value()->weight;
1617 for (; Gap != NumGaps; ++Gap) {
1618 GapWeight[Gap] = std::max(GapWeight[Gap], weight);
1619 if (Uses[Gap+1].getBaseIndex() >= IntI.stop())
1620 break;
1621 }
1622 if (Gap == NumGaps)
1623 break;
1624 }
1625 }
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +00001626
1627 // Add fixed interference.
1628 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
Matthias Braun34e1be92013-10-10 21:29:02 +00001629 const LiveRange &LR = LIS->getRegUnit(*Units);
1630 LiveRange::const_iterator I = LR.find(StartIdx);
1631 LiveRange::const_iterator E = LR.end();
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +00001632
1633 // Same loop as above. Mark any overlapped gaps as HUGE_VALF.
1634 for (unsigned Gap = 0; I != E && I->start < StopIdx; ++I) {
1635 while (Uses[Gap+1].getBoundaryIndex() < I->start)
1636 if (++Gap == NumGaps)
1637 break;
1638 if (Gap == NumGaps)
1639 break;
1640
1641 for (; Gap != NumGaps; ++Gap) {
Aaron Ballman04999042013-11-13 00:15:44 +00001642 GapWeight[Gap] = llvm::huge_valf;
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +00001643 if (Uses[Gap+1].getBaseIndex() >= I->end)
1644 break;
1645 }
1646 if (Gap == NumGaps)
1647 break;
1648 }
1649 }
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001650}
1651
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001652/// tryLocalSplit - Try to split VirtReg into smaller intervals inside its only
1653/// basic block.
1654///
1655unsigned RAGreedy::tryLocalSplit(LiveInterval &VirtReg, AllocationOrder &Order,
Mark Laceyf9ea8852013-08-14 23:50:04 +00001656 SmallVectorImpl<unsigned> &NewVRegs) {
Jakob Stoklund Olesenbf91c4e2011-04-06 03:57:00 +00001657 assert(SA->getUseBlocks().size() == 1 && "Not a local interval");
1658 const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front();
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001659
1660 // Note that it is possible to have an interval that is live-in or live-out
1661 // while only covering a single block - A phi-def can use undef values from
1662 // predecessors, and the block could be a single-block loop.
1663 // We don't bother doing anything clever about such a case, we simply assume
Jakob Stoklund Olesen43859a62011-08-02 22:54:14 +00001664 // that the interval is continuous from FirstInstr to LastInstr. We should
1665 // make sure that we don't do anything illegal to such an interval, though.
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001666
Jakob Stoklund Olesen994fed62012-01-12 17:53:44 +00001667 ArrayRef<SlotIndex> Uses = SA->getUseSlots();
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001668 if (Uses.size() <= 2)
1669 return 0;
1670 const unsigned NumGaps = Uses.size()-1;
1671
1672 DEBUG({
1673 dbgs() << "tryLocalSplit: ";
1674 for (unsigned i = 0, e = Uses.size(); i != e; ++i)
Jakob Stoklund Olesen994fed62012-01-12 17:53:44 +00001675 dbgs() << ' ' << Uses[i];
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001676 dbgs() << '\n';
1677 });
1678
Jakob Stoklund Olesen17402e32012-02-11 00:42:18 +00001679 // If VirtReg is live across any register mask operands, compute a list of
1680 // gaps with register masks.
1681 SmallVector<unsigned, 8> RegMaskGaps;
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +00001682 if (Matrix->checkRegMaskInterference(VirtReg)) {
Jakob Stoklund Olesen17402e32012-02-11 00:42:18 +00001683 // Get regmask slots for the whole block.
1684 ArrayRef<SlotIndex> RMS = LIS->getRegMaskSlotsInBlock(BI.MBB->getNumber());
Jakob Stoklund Olesenb0c0d342012-02-14 23:51:27 +00001685 DEBUG(dbgs() << RMS.size() << " regmasks in block:");
Jakob Stoklund Olesen17402e32012-02-11 00:42:18 +00001686 // Constrain to VirtReg's live range.
Jakob Stoklund Olesenb0c0d342012-02-14 23:51:27 +00001687 unsigned ri = std::lower_bound(RMS.begin(), RMS.end(),
1688 Uses.front().getRegSlot()) - RMS.begin();
Jakob Stoklund Olesen17402e32012-02-11 00:42:18 +00001689 unsigned re = RMS.size();
1690 for (unsigned i = 0; i != NumGaps && ri != re; ++i) {
Jakob Stoklund Olesenb0c0d342012-02-14 23:51:27 +00001691 // Look for Uses[i] <= RMS <= Uses[i+1].
1692 assert(!SlotIndex::isEarlierInstr(RMS[ri], Uses[i]));
1693 if (SlotIndex::isEarlierInstr(Uses[i+1], RMS[ri]))
Jakob Stoklund Olesen17402e32012-02-11 00:42:18 +00001694 continue;
Jakob Stoklund Olesenb0c0d342012-02-14 23:51:27 +00001695 // Skip a regmask on the same instruction as the last use. It doesn't
1696 // overlap the live range.
1697 if (SlotIndex::isSameInstr(Uses[i+1], RMS[ri]) && i+1 == NumGaps)
1698 break;
1699 DEBUG(dbgs() << ' ' << RMS[ri] << ':' << Uses[i] << '-' << Uses[i+1]);
Jakob Stoklund Olesen17402e32012-02-11 00:42:18 +00001700 RegMaskGaps.push_back(i);
Jakob Stoklund Olesenb0c0d342012-02-14 23:51:27 +00001701 // Advance ri to the next gap. A regmask on one of the uses counts in
1702 // both gaps.
1703 while (ri != re && SlotIndex::isEarlierInstr(RMS[ri], Uses[i+1]))
1704 ++ri;
Jakob Stoklund Olesen17402e32012-02-11 00:42:18 +00001705 }
Jakob Stoklund Olesenb0c0d342012-02-14 23:51:27 +00001706 DEBUG(dbgs() << '\n');
Jakob Stoklund Olesen17402e32012-02-11 00:42:18 +00001707 }
1708
Jakob Stoklund Olesendf476272011-06-06 23:55:20 +00001709 // Since we allow local split results to be split again, there is a risk of
1710 // creating infinite loops. It is tempting to require that the new live
1711 // ranges have less instructions than the original. That would guarantee
1712 // convergence, but it is too strict. A live range with 3 instructions can be
1713 // split 2+3 (including the COPY), and we want to allow that.
1714 //
1715 // Instead we use these rules:
1716 //
Jakob Stoklund Olesen45011172011-07-25 15:25:43 +00001717 // 1. Allow any split for ranges with getStage() < RS_Split2. (Except for the
Jakob Stoklund Olesendf476272011-06-06 23:55:20 +00001718 // noop split, of course).
Jakob Stoklund Olesen45011172011-07-25 15:25:43 +00001719 // 2. Require progress be made for ranges with getStage() == RS_Split2. All
Jakob Stoklund Olesendf476272011-06-06 23:55:20 +00001720 // the new ranges must have fewer instructions than before the split.
Jakob Stoklund Olesen45011172011-07-25 15:25:43 +00001721 // 3. New ranges with the same number of instructions are marked RS_Split2,
Jakob Stoklund Olesendf476272011-06-06 23:55:20 +00001722 // smaller ranges are marked RS_New.
1723 //
1724 // These rules allow a 3 -> 2+3 split once, which we need. They also prevent
1725 // excessive splitting and infinite loops.
1726 //
Jakob Stoklund Olesen45011172011-07-25 15:25:43 +00001727 bool ProgressRequired = getStage(VirtReg) >= RS_Split2;
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001728
Jakob Stoklund Olesendf476272011-06-06 23:55:20 +00001729 // Best split candidate.
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001730 unsigned BestBefore = NumGaps;
1731 unsigned BestAfter = 0;
1732 float BestDiff = 0;
1733
Jakob Stoklund Olesenefeb3a12013-07-16 18:26:18 +00001734 const float blockFreq =
1735 SpillPlacer->getBlockFrequency(BI.MBB->getNumber()).getFrequency() *
Michael Gottesman5e985ee2013-12-14 02:37:38 +00001736 (1.0f / MBFI->getEntryFreq());
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001737 SmallVector<float, 8> GapWeight;
1738
1739 Order.rewind();
1740 while (unsigned PhysReg = Order.next()) {
1741 // Keep track of the largest spill weight that would need to be evicted in
1742 // order to make use of PhysReg between UseSlots[i] and UseSlots[i+1].
1743 calcGapWeights(PhysReg, GapWeight);
1744
Jakob Stoklund Olesen17402e32012-02-11 00:42:18 +00001745 // Remove any gaps with regmask clobbers.
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +00001746 if (Matrix->checkRegMaskInterference(VirtReg, PhysReg))
Jakob Stoklund Olesen17402e32012-02-11 00:42:18 +00001747 for (unsigned i = 0, e = RegMaskGaps.size(); i != e; ++i)
Aaron Ballman04999042013-11-13 00:15:44 +00001748 GapWeight[RegMaskGaps[i]] = llvm::huge_valf;
Jakob Stoklund Olesen17402e32012-02-11 00:42:18 +00001749
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001750 // Try to find the best sequence of gaps to close.
1751 // The new spill weight must be larger than any gap interference.
1752
1753 // We will split before Uses[SplitBefore] and after Uses[SplitAfter].
Jakob Stoklund Olesendf476272011-06-06 23:55:20 +00001754 unsigned SplitBefore = 0, SplitAfter = 1;
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001755
1756 // MaxGap should always be max(GapWeight[SplitBefore..SplitAfter-1]).
1757 // It is the spill weight that needs to be evicted.
1758 float MaxGap = GapWeight[0];
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001759
1760 for (;;) {
1761 // Live before/after split?
1762 const bool LiveBefore = SplitBefore != 0 || BI.LiveIn;
1763 const bool LiveAfter = SplitAfter != NumGaps || BI.LiveOut;
1764
1765 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << ' '
1766 << Uses[SplitBefore] << '-' << Uses[SplitAfter]
1767 << " i=" << MaxGap);
1768
1769 // Stop before the interval gets so big we wouldn't be making progress.
1770 if (!LiveBefore && !LiveAfter) {
1771 DEBUG(dbgs() << " all\n");
1772 break;
1773 }
1774 // Should the interval be extended or shrunk?
1775 bool Shrink = true;
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001776
Jakob Stoklund Olesendf476272011-06-06 23:55:20 +00001777 // How many gaps would the new range have?
1778 unsigned NewGaps = LiveBefore + SplitAfter - SplitBefore + LiveAfter;
1779
1780 // Legally, without causing looping?
1781 bool Legal = !ProgressRequired || NewGaps < NumGaps;
1782
Aaron Ballman04999042013-11-13 00:15:44 +00001783 if (Legal && MaxGap < llvm::huge_valf) {
Jakob Stoklund Olesendf476272011-06-06 23:55:20 +00001784 // Estimate the new spill weight. Each instruction reads or writes the
1785 // register. Conservatively assume there are no read-modify-write
1786 // instructions.
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001787 //
Jakob Stoklund Olesendf476272011-06-06 23:55:20 +00001788 // Try to guess the size of the new interval.
1789 const float EstWeight = normalizeSpillWeight(blockFreq * (NewGaps + 1),
1790 Uses[SplitBefore].distance(Uses[SplitAfter]) +
1791 (LiveBefore + LiveAfter)*SlotIndex::InstrDist);
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001792 // Would this split be possible to allocate?
1793 // Never allocate all gaps, we wouldn't be making progress.
Jakob Stoklund Olesen357dd362011-04-30 05:07:46 +00001794 DEBUG(dbgs() << " w=" << EstWeight);
1795 if (EstWeight * Hysteresis >= MaxGap) {
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001796 Shrink = false;
Jakob Stoklund Olesen357dd362011-04-30 05:07:46 +00001797 float Diff = EstWeight - MaxGap;
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001798 if (Diff > BestDiff) {
1799 DEBUG(dbgs() << " (best)");
Jakob Stoklund Olesen357dd362011-04-30 05:07:46 +00001800 BestDiff = Hysteresis * Diff;
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001801 BestBefore = SplitBefore;
1802 BestAfter = SplitAfter;
1803 }
1804 }
1805 }
1806
1807 // Try to shrink.
1808 if (Shrink) {
Jakob Stoklund Olesendf476272011-06-06 23:55:20 +00001809 if (++SplitBefore < SplitAfter) {
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001810 DEBUG(dbgs() << " shrink\n");
1811 // Recompute the max when necessary.
1812 if (GapWeight[SplitBefore - 1] >= MaxGap) {
1813 MaxGap = GapWeight[SplitBefore];
1814 for (unsigned i = SplitBefore + 1; i != SplitAfter; ++i)
1815 MaxGap = std::max(MaxGap, GapWeight[i]);
1816 }
1817 continue;
1818 }
1819 MaxGap = 0;
1820 }
1821
1822 // Try to extend the interval.
1823 if (SplitAfter >= NumGaps) {
1824 DEBUG(dbgs() << " end\n");
1825 break;
1826 }
1827
1828 DEBUG(dbgs() << " extend\n");
Jakob Stoklund Olesendf476272011-06-06 23:55:20 +00001829 MaxGap = std::max(MaxGap, GapWeight[SplitAfter++]);
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001830 }
1831 }
1832
1833 // Didn't find any candidates?
1834 if (BestBefore == NumGaps)
1835 return 0;
1836
1837 DEBUG(dbgs() << "Best local split range: " << Uses[BestBefore]
1838 << '-' << Uses[BestAfter] << ", " << BestDiff
1839 << ", " << (BestAfter - BestBefore + 1) << " instrs\n");
1840
Jakob Stoklund Olesene5bbe372012-05-19 05:25:46 +00001841 LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this);
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +00001842 SE->reset(LREdit);
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001843
Jakob Stoklund Olesenc9601982011-03-03 01:29:13 +00001844 SE->openIntv();
1845 SlotIndex SegStart = SE->enterIntvBefore(Uses[BestBefore]);
1846 SlotIndex SegStop = SE->leaveIntvAfter(Uses[BestAfter]);
1847 SE->useIntv(SegStart, SegStop);
Jakob Stoklund Olesendf476272011-06-06 23:55:20 +00001848 SmallVector<unsigned, 8> IntvMap;
1849 SE->finish(&IntvMap);
Mark Laceyf9ea8852013-08-14 23:50:04 +00001850 DebugVars->splitRegister(VirtReg.reg, LREdit.regs(), *LIS);
Jakob Stoklund Olesendf476272011-06-06 23:55:20 +00001851
1852 // If the new range has the same number of instructions as before, mark it as
Jakob Stoklund Olesen45011172011-07-25 15:25:43 +00001853 // RS_Split2 so the next split will be forced to make progress. Otherwise,
Jakob Stoklund Olesendf476272011-06-06 23:55:20 +00001854 // leave the new intervals as RS_New so they can compete.
1855 bool LiveBefore = BestBefore != 0 || BI.LiveIn;
1856 bool LiveAfter = BestAfter != NumGaps || BI.LiveOut;
1857 unsigned NewGaps = LiveBefore + BestAfter - BestBefore + LiveAfter;
1858 if (NewGaps >= NumGaps) {
1859 DEBUG(dbgs() << "Tagging non-progress ranges: ");
1860 assert(!ProgressRequired && "Didn't make progress when it was required.");
Jakob Stoklund Olesendf476272011-06-06 23:55:20 +00001861 for (unsigned i = 0, e = IntvMap.size(); i != e; ++i)
1862 if (IntvMap[i] == 1) {
Mark Laceyf9ea8852013-08-14 23:50:04 +00001863 setStage(LIS->getInterval(LREdit.get(i)), RS_Split2);
1864 DEBUG(dbgs() << PrintReg(LREdit.get(i)));
Jakob Stoklund Olesendf476272011-06-06 23:55:20 +00001865 }
1866 DEBUG(dbgs() << '\n');
1867 }
Jakob Stoklund Olesen99827e82011-02-17 22:53:48 +00001868 ++NumLocalSplits;
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001869
1870 return 0;
1871}
1872
1873//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen9fb04012011-01-19 22:11:48 +00001874// Live Range Splitting
1875//===----------------------------------------------------------------------===//
1876
1877/// trySplit - Try to split VirtReg or one of its interferences, making it
1878/// assignable.
1879/// @return Physreg when VirtReg may be assigned and/or new NewVRegs.
1880unsigned RAGreedy::trySplit(LiveInterval &VirtReg, AllocationOrder &Order,
Mark Laceyf9ea8852013-08-14 23:50:04 +00001881 SmallVectorImpl<unsigned>&NewVRegs) {
Jakob Stoklund Olesend4bb1d42011-08-05 23:50:33 +00001882 // Ranges must be Split2 or less.
1883 if (getStage(VirtReg) >= RS_Spill)
1884 return 0;
1885
Jakob Stoklund Olesen93c87362011-02-17 19:13:53 +00001886 // Local intervals are handled separately.
Jakob Stoklund Olesen609bc442011-02-19 00:38:40 +00001887 if (LIS->intervalIsInOneMBB(VirtReg)) {
1888 NamedRegionTimer T("Local Splitting", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesen5f9f0812011-03-01 21:10:07 +00001889 SA->analyze(&VirtReg);
Jakob Stoklund Olesen0ce90492012-05-23 22:37:27 +00001890 unsigned PhysReg = tryLocalSplit(VirtReg, Order, NewVRegs);
1891 if (PhysReg || !NewVRegs.empty())
1892 return PhysReg;
1893 return tryInstructionSplit(VirtReg, Order, NewVRegs);
Jakob Stoklund Olesen609bc442011-02-19 00:38:40 +00001894 }
1895
1896 NamedRegionTimer T("Global Splitting", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesen9fb04012011-01-19 22:11:48 +00001897
Jakob Stoklund Olesen5f9f0812011-03-01 21:10:07 +00001898 SA->analyze(&VirtReg);
1899
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +00001900 // FIXME: SplitAnalysis may repair broken live ranges coming from the
1901 // coalescer. That may cause the range to become allocatable which means that
1902 // tryRegionSplit won't be making progress. This check should be replaced with
1903 // an assertion when the coalescer is fixed.
1904 if (SA->didRepairRange()) {
1905 // VirtReg has changed, so all cached queries are invalid.
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +00001906 Matrix->invalidateVirtRegs();
Jakob Stoklund Oleseneaa6ed12011-05-03 20:42:13 +00001907 if (unsigned PhysReg = tryAssign(VirtReg, Order, NewVRegs))
1908 return PhysReg;
1909 }
1910
Jakob Stoklund Olesen45011172011-07-25 15:25:43 +00001911 // First try to split around a region spanning multiple blocks. RS_Split2
1912 // ranges already made dubious progress with region splitting, so they go
1913 // straight to single block splitting.
1914 if (getStage(VirtReg) < RS_Split2) {
1915 unsigned PhysReg = tryRegionSplit(VirtReg, Order, NewVRegs);
1916 if (PhysReg || !NewVRegs.empty())
1917 return PhysReg;
1918 }
Jakob Stoklund Olesen9fb04012011-01-19 22:11:48 +00001919
Jakob Stoklund Olesencef5d8f2011-08-05 23:04:18 +00001920 // Then isolate blocks.
1921 return tryBlockSplit(VirtReg, Order, NewVRegs);
Jakob Stoklund Olesen9fb04012011-01-19 22:11:48 +00001922}
1923
Quentin Colombet87769712014-02-05 22:13:59 +00001924//===----------------------------------------------------------------------===//
1925// Last Chance Recoloring
1926//===----------------------------------------------------------------------===//
1927
1928/// mayRecolorAllInterferences - Check if the virtual registers that
1929/// interfere with \p VirtReg on \p PhysReg (or one of its aliases) may be
1930/// recolored to free \p PhysReg.
1931/// When true is returned, \p RecoloringCandidates has been augmented with all
1932/// the live intervals that need to be recolored in order to free \p PhysReg
1933/// for \p VirtReg.
1934/// \p FixedRegisters contains all the virtual registers that cannot be
1935/// recolored.
1936bool
1937RAGreedy::mayRecolorAllInterferences(unsigned PhysReg, LiveInterval &VirtReg,
1938 SmallLISet &RecoloringCandidates,
1939 const SmallVirtRegSet &FixedRegisters) {
1940 const TargetRegisterClass *CurRC = MRI->getRegClass(VirtReg.reg);
1941
1942 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
1943 LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units);
1944 // If there is LastChanceRecoloringMaxInterference or more interferences,
1945 // chances are one would not be recolorable.
1946 if (Q.collectInterferingVRegs(LastChanceRecoloringMaxInterference) >=
Quentin Colombet567e30b2014-04-11 21:39:44 +00001947 LastChanceRecoloringMaxInterference && !ExhaustiveSearch) {
Quentin Colombet87769712014-02-05 22:13:59 +00001948 DEBUG(dbgs() << "Early abort: too many interferences.\n");
Quentin Colombet96bd2a12014-04-04 02:05:21 +00001949 CutOffInfo |= CO_Interf;
Quentin Colombet87769712014-02-05 22:13:59 +00001950 return false;
1951 }
1952 for (unsigned i = Q.interferingVRegs().size(); i; --i) {
1953 LiveInterval *Intf = Q.interferingVRegs()[i - 1];
1954 // If Intf is done and sit on the same register class as VirtReg,
1955 // it would not be recolorable as it is in the same state as VirtReg.
1956 if ((getStage(*Intf) == RS_Done &&
1957 MRI->getRegClass(Intf->reg) == CurRC) ||
1958 FixedRegisters.count(Intf->reg)) {
1959 DEBUG(dbgs() << "Early abort: the inteference is not recolorable.\n");
1960 return false;
1961 }
1962 RecoloringCandidates.insert(Intf);
1963 }
1964 }
1965 return true;
1966}
1967
1968/// tryLastChanceRecoloring - Try to assign a color to \p VirtReg by recoloring
1969/// its interferences.
1970/// Last chance recoloring chooses a color for \p VirtReg and recolors every
1971/// virtual register that was using it. The recoloring process may recursively
1972/// use the last chance recoloring. Therefore, when a virtual register has been
1973/// assigned a color by this mechanism, it is marked as Fixed, i.e., it cannot
1974/// be last-chance-recolored again during this recoloring "session".
1975/// E.g.,
1976/// Let
1977/// vA can use {R1, R2 }
1978/// vB can use { R2, R3}
1979/// vC can use {R1 }
1980/// Where vA, vB, and vC cannot be split anymore (they are reloads for
1981/// instance) and they all interfere.
1982///
1983/// vA is assigned R1
1984/// vB is assigned R2
1985/// vC tries to evict vA but vA is already done.
1986/// Regular register allocation fails.
1987///
1988/// Last chance recoloring kicks in:
1989/// vC does as if vA was evicted => vC uses R1.
1990/// vC is marked as fixed.
1991/// vA needs to find a color.
1992/// None are available.
1993/// vA cannot evict vC: vC is a fixed virtual register now.
1994/// vA does as if vB was evicted => vA uses R2.
1995/// vB needs to find a color.
1996/// R3 is available.
1997/// Recoloring => vC = R1, vA = R2, vB = R3
1998///
Alp Toker70b36992014-02-25 04:21:15 +00001999/// \p Order defines the preferred allocation order for \p VirtReg.
Quentin Colombet87769712014-02-05 22:13:59 +00002000/// \p NewRegs will contain any new virtual register that have been created
2001/// (split, spill) during the process and that must be assigned.
2002/// \p FixedRegisters contains all the virtual registers that cannot be
2003/// recolored.
2004/// \p Depth gives the current depth of the last chance recoloring.
2005/// \return a physical register that can be used for VirtReg or ~0u if none
2006/// exists.
2007unsigned RAGreedy::tryLastChanceRecoloring(LiveInterval &VirtReg,
2008 AllocationOrder &Order,
2009 SmallVectorImpl<unsigned> &NewVRegs,
2010 SmallVirtRegSet &FixedRegisters,
2011 unsigned Depth) {
2012 DEBUG(dbgs() << "Try last chance recoloring for " << VirtReg << '\n');
2013 // Ranges must be Done.
Quentin Colombet0e3b5e02014-02-13 05:17:37 +00002014 assert((getStage(VirtReg) >= RS_Done || !VirtReg.isSpillable()) &&
Quentin Colombet87769712014-02-05 22:13:59 +00002015 "Last chance recoloring should really be last chance");
2016 // Set the max depth to LastChanceRecoloringMaxDepth.
2017 // We may want to reconsider that if we end up with a too large search space
2018 // for target with hundreds of registers.
2019 // Indeed, in that case we may want to cut the search space earlier.
Quentin Colombet567e30b2014-04-11 21:39:44 +00002020 if (Depth >= LastChanceRecoloringMaxDepth && !ExhaustiveSearch) {
Quentin Colombet87769712014-02-05 22:13:59 +00002021 DEBUG(dbgs() << "Abort because max depth has been reached.\n");
Quentin Colombet96bd2a12014-04-04 02:05:21 +00002022 CutOffInfo |= CO_Depth;
Quentin Colombet87769712014-02-05 22:13:59 +00002023 return ~0u;
2024 }
2025
2026 // Set of Live intervals that will need to be recolored.
2027 SmallLISet RecoloringCandidates;
2028 // Record the original mapping virtual register to physical register in case
2029 // the recoloring fails.
2030 DenseMap<unsigned, unsigned> VirtRegToPhysReg;
2031 // Mark VirtReg as fixed, i.e., it will not be recolored pass this point in
2032 // this recoloring "session".
2033 FixedRegisters.insert(VirtReg.reg);
2034
2035 Order.rewind();
2036 while (unsigned PhysReg = Order.next()) {
2037 DEBUG(dbgs() << "Try to assign: " << VirtReg << " to "
2038 << PrintReg(PhysReg, TRI) << '\n');
2039 RecoloringCandidates.clear();
2040 VirtRegToPhysReg.clear();
2041
2042 // It is only possible to recolor virtual register interference.
2043 if (Matrix->checkInterference(VirtReg, PhysReg) >
2044 LiveRegMatrix::IK_VirtReg) {
2045 DEBUG(dbgs() << "Some inteferences are not with virtual registers.\n");
2046
2047 continue;
2048 }
2049
2050 // Early give up on this PhysReg if it is obvious we cannot recolor all
2051 // the interferences.
2052 if (!mayRecolorAllInterferences(PhysReg, VirtReg, RecoloringCandidates,
2053 FixedRegisters)) {
2054 DEBUG(dbgs() << "Some inteferences cannot be recolored.\n");
2055 continue;
2056 }
2057
2058 // RecoloringCandidates contains all the virtual registers that interfer
2059 // with VirtReg on PhysReg (or one of its aliases).
2060 // Enqueue them for recoloring and perform the actual recoloring.
2061 PQueue RecoloringQueue;
2062 for (SmallLISet::iterator It = RecoloringCandidates.begin(),
2063 EndIt = RecoloringCandidates.end();
2064 It != EndIt; ++It) {
2065 unsigned ItVirtReg = (*It)->reg;
2066 enqueue(RecoloringQueue, *It);
2067 assert(VRM->hasPhys(ItVirtReg) &&
2068 "Interferences are supposed to be with allocated vairables");
2069
2070 // Record the current allocation.
2071 VirtRegToPhysReg[ItVirtReg] = VRM->getPhys(ItVirtReg);
2072 // unset the related struct.
2073 Matrix->unassign(**It);
2074 }
2075
2076 // Do as if VirtReg was assigned to PhysReg so that the underlying
2077 // recoloring has the right information about the interferes and
2078 // available colors.
2079 Matrix->assign(VirtReg, PhysReg);
2080
2081 // Save the current recoloring state.
2082 // If we cannot recolor all the interferences, we will have to start again
2083 // at this point for the next physical register.
2084 SmallVirtRegSet SaveFixedRegisters(FixedRegisters);
2085 if (tryRecoloringCandidates(RecoloringQueue, NewVRegs, FixedRegisters,
2086 Depth)) {
2087 // Do not mess up with the global assignment process.
2088 // I.e., VirtReg must be unassigned.
2089 Matrix->unassign(VirtReg);
2090 return PhysReg;
2091 }
2092
2093 DEBUG(dbgs() << "Fail to assign: " << VirtReg << " to "
2094 << PrintReg(PhysReg, TRI) << '\n');
2095
2096 // The recoloring attempt failed, undo the changes.
2097 FixedRegisters = SaveFixedRegisters;
2098 Matrix->unassign(VirtReg);
2099
2100 for (SmallLISet::iterator It = RecoloringCandidates.begin(),
2101 EndIt = RecoloringCandidates.end();
2102 It != EndIt; ++It) {
2103 unsigned ItVirtReg = (*It)->reg;
2104 if (VRM->hasPhys(ItVirtReg))
2105 Matrix->unassign(**It);
2106 Matrix->assign(**It, VirtRegToPhysReg[ItVirtReg]);
2107 }
2108 }
2109
2110 // Last chance recoloring did not worked either, give up.
2111 return ~0u;
2112}
2113
2114/// tryRecoloringCandidates - Try to assign a new color to every register
2115/// in \RecoloringQueue.
2116/// \p NewRegs will contain any new virtual register created during the
2117/// recoloring process.
2118/// \p FixedRegisters[in/out] contains all the registers that have been
2119/// recolored.
2120/// \return true if all virtual registers in RecoloringQueue were successfully
2121/// recolored, false otherwise.
2122bool RAGreedy::tryRecoloringCandidates(PQueue &RecoloringQueue,
2123 SmallVectorImpl<unsigned> &NewVRegs,
2124 SmallVirtRegSet &FixedRegisters,
2125 unsigned Depth) {
2126 while (!RecoloringQueue.empty()) {
2127 LiveInterval *LI = dequeue(RecoloringQueue);
2128 DEBUG(dbgs() << "Try to recolor: " << *LI << '\n');
2129 unsigned PhysReg;
2130 PhysReg = selectOrSplitImpl(*LI, NewVRegs, FixedRegisters, Depth + 1);
2131 if (PhysReg == ~0u || !PhysReg)
2132 return false;
2133 DEBUG(dbgs() << "Recoloring of " << *LI
2134 << " succeeded with: " << PrintReg(PhysReg, TRI) << '\n');
2135 Matrix->assign(*LI, PhysReg);
2136 FixedRegisters.insert(LI->reg);
2137 }
2138 return true;
2139}
Jakob Stoklund Olesen9fb04012011-01-19 22:11:48 +00002140
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +00002141//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen0acb69d2010-12-22 22:01:30 +00002142// Main Entry Point
2143//===----------------------------------------------------------------------===//
2144
2145unsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg,
Mark Laceyf9ea8852013-08-14 23:50:04 +00002146 SmallVectorImpl<unsigned> &NewVRegs) {
Quentin Colombet96bd2a12014-04-04 02:05:21 +00002147 CutOffInfo = CO_None;
2148 LLVMContext &Ctx = MF->getFunction()->getContext();
Quentin Colombet87769712014-02-05 22:13:59 +00002149 SmallVirtRegSet FixedRegisters;
Quentin Colombet96bd2a12014-04-04 02:05:21 +00002150 unsigned Reg = selectOrSplitImpl(VirtReg, NewVRegs, FixedRegisters);
2151 if (Reg == ~0U && (CutOffInfo != CO_None)) {
2152 uint8_t CutOffEncountered = CutOffInfo & (CO_Depth | CO_Interf);
2153 if (CutOffEncountered == CO_Depth)
Quentin Colombet567e30b2014-04-11 21:39:44 +00002154 Ctx.emitError("register allocation failed: maximum depth for recoloring "
2155 "reached. Use -fexhaustive-register-search to skip "
2156 "cutoffs");
Quentin Colombet96bd2a12014-04-04 02:05:21 +00002157 else if (CutOffEncountered == CO_Interf)
2158 Ctx.emitError("register allocation failed: maximum interference for "
Quentin Colombet567e30b2014-04-11 21:39:44 +00002159 "recoloring reached. Use -fexhaustive-register-search "
2160 "to skip cutoffs");
Quentin Colombet96bd2a12014-04-04 02:05:21 +00002161 else if (CutOffEncountered == (CO_Depth | CO_Interf))
2162 Ctx.emitError("register allocation failed: maximum interference and "
Quentin Colombet567e30b2014-04-11 21:39:44 +00002163 "depth for recoloring reached. Use "
2164 "-fexhaustive-register-search to skip cutoffs");
Quentin Colombet96bd2a12014-04-04 02:05:21 +00002165 }
2166 return Reg;
Quentin Colombet87769712014-02-05 22:13:59 +00002167}
2168
Manman Ren9dee4492014-03-27 21:21:57 +00002169/// Using a CSR for the first time has a cost because it causes push|pop
2170/// to be added to prologue|epilogue. Splitting a cold section of the live
2171/// range can have lower cost than using the CSR for the first time;
2172/// Spilling a live range in the cold path can have lower cost than using
2173/// the CSR for the first time. Returns the physical register if we decide
2174/// to use the CSR; otherwise return 0.
2175unsigned RAGreedy::tryAssignCSRFirstTime(LiveInterval &VirtReg,
2176 AllocationOrder &Order,
2177 unsigned PhysReg,
2178 unsigned &CostPerUseLimit,
2179 SmallVectorImpl<unsigned> &NewVRegs) {
Manman Ren9dee4492014-03-27 21:21:57 +00002180 if (getStage(VirtReg) == RS_Spill && VirtReg.isSpillable()) {
2181 // We choose spill over using the CSR for the first time if the spill cost
2182 // is lower than CSRCost.
2183 SA->analyze(&VirtReg);
2184 if (calcSpillCost() >= CSRCost)
2185 return PhysReg;
2186
2187 // We are going to spill, set CostPerUseLimit to 1 to make sure that
2188 // we will not use a callee-saved register in tryEvict.
2189 CostPerUseLimit = 1;
2190 return 0;
2191 }
2192 if (getStage(VirtReg) < RS_Split) {
2193 // We choose pre-splitting over using the CSR for the first time if
2194 // the cost of splitting is lower than CSRCost.
2195 SA->analyze(&VirtReg);
2196 unsigned NumCands = 0;
Duncan P. N. Exon Smitha5df8132014-04-08 19:18:56 +00002197 BlockFrequency BestCost = CSRCost; // Don't modify CSRCost.
2198 unsigned BestCand = calculateRegionSplitCost(VirtReg, Order, BestCost,
2199 NumCands, true /*IgnoreCSR*/);
Manman Ren9dee4492014-03-27 21:21:57 +00002200 if (BestCand == NoCand)
2201 // Use the CSR if we can't find a region split below CSRCost.
2202 return PhysReg;
2203
2204 // Perform the actual pre-splitting.
2205 doRegionSplit(VirtReg, BestCand, false/*HasCompact*/, NewVRegs);
2206 return 0;
2207 }
2208 return PhysReg;
2209}
2210
Duncan P. N. Exon Smitha5df8132014-04-08 19:18:56 +00002211void RAGreedy::initializeCSRCost() {
2212 // We use the larger one out of the command-line option and the value report
2213 // by TRI.
2214 CSRCost = BlockFrequency(
2215 std::max((unsigned)CSRFirstTimeCost, TRI->getCSRFirstUseCost()));
2216 if (!CSRCost.getFrequency())
2217 return;
2218
2219 // Raw cost is relative to Entry == 2^14; scale it appropriately.
2220 uint64_t ActualEntry = MBFI->getEntryFreq();
2221 if (!ActualEntry) {
2222 CSRCost = 0;
2223 return;
2224 }
2225 uint64_t FixedEntry = 1 << 14;
2226 if (ActualEntry < FixedEntry)
2227 CSRCost *= BranchProbability(ActualEntry, FixedEntry);
2228 else if (ActualEntry <= UINT32_MAX)
2229 // Invert the fraction and divide.
2230 CSRCost /= BranchProbability(FixedEntry, ActualEntry);
2231 else
2232 // Can't use BranchProbability in general, since it takes 32-bit numbers.
2233 CSRCost = CSRCost.getFrequency() * (ActualEntry / FixedEntry);
2234}
2235
Quentin Colombet87769712014-02-05 22:13:59 +00002236unsigned RAGreedy::selectOrSplitImpl(LiveInterval &VirtReg,
2237 SmallVectorImpl<unsigned> &NewVRegs,
2238 SmallVirtRegSet &FixedRegisters,
2239 unsigned Depth) {
Manman Ren78cf02a2014-03-25 00:16:25 +00002240 unsigned CostPerUseLimit = ~0u;
Jakob Stoklund Olesen0acb69d2010-12-22 22:01:30 +00002241 // First try assigning a free register.
Jakob Stoklund Olesenb8bf3c02011-06-03 20:34:53 +00002242 AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo);
Manman Ren78cf02a2014-03-25 00:16:25 +00002243 if (unsigned PhysReg = tryAssign(VirtReg, Order, NewVRegs)) {
2244 // We check other options if we are using a CSR for the first time.
2245 bool CSRFirstUse = false;
2246 if (unsigned CSR = RegClassInfo.getLastCalleeSavedAlias(PhysReg))
2247 if (!MRI->isPhysRegUsed(CSR))
2248 CSRFirstUse = true;
2249
Manman Ren9dee4492014-03-27 21:21:57 +00002250 // When NewVRegs is not empty, we may have made decisions such as evicting
2251 // a virtual register, go with the earlier decisions and use the physical
2252 // register.
Duncan P. N. Exon Smitha5df8132014-04-08 19:18:56 +00002253 if (CSRCost.getFrequency() && CSRFirstUse && NewVRegs.empty()) {
Manman Ren9dee4492014-03-27 21:21:57 +00002254 unsigned CSRReg = tryAssignCSRFirstTime(VirtReg, Order, PhysReg,
2255 CostPerUseLimit, NewVRegs);
2256 if (CSRReg || !NewVRegs.empty())
2257 // Return now if we decide to use a CSR or create new vregs due to
2258 // pre-splitting.
2259 return CSRReg;
Manman Ren78cf02a2014-03-25 00:16:25 +00002260 } else
2261 return PhysReg;
2262 }
Andrew Trickccef0982010-12-09 18:15:21 +00002263
Jakob Stoklund Olesen25d57452011-05-25 23:58:36 +00002264 LiveRangeStage Stage = getStage(VirtReg);
Jakob Stoklund Olesen30a85632011-07-02 01:37:09 +00002265 DEBUG(dbgs() << StageName[Stage]
2266 << " Cascade " << ExtraRegInfo[VirtReg.reg].Cascade << '\n');
Jakob Stoklund Olesen25d57452011-05-25 23:58:36 +00002267
Jakob Stoklund Olesene9cc8e92011-06-01 18:45:02 +00002268 // Try to evict a less worthy live range, but only for ranges from the primary
Jakob Stoklund Olesen3ef8cf12011-07-25 15:25:41 +00002269 // queue. The RS_Split ranges already failed to do this, and they should not
Jakob Stoklund Olesene9cc8e92011-06-01 18:45:02 +00002270 // get a second chance until they have been split.
Jakob Stoklund Olesen3ef8cf12011-07-25 15:25:41 +00002271 if (Stage != RS_Split)
Manman Ren78cf02a2014-03-25 00:16:25 +00002272 if (unsigned PhysReg = tryEvict(VirtReg, Order, NewVRegs, CostPerUseLimit))
Jakob Stoklund Olesene9cc8e92011-06-01 18:45:02 +00002273 return PhysReg;
Andrew Trickccef0982010-12-09 18:15:21 +00002274
Jakob Stoklund Olesen9fb04012011-01-19 22:11:48 +00002275 assert(NewVRegs.empty() && "Cannot append to existing NewVRegs");
2276
Jakob Stoklund Olesene68a27e2011-02-24 23:21:36 +00002277 // The first time we see a live range, don't try to split or spill.
2278 // Wait until the second time, when all smaller ranges have been allocated.
2279 // This gives a better picture of the interference to split around.
Jakob Stoklund Olesen3ef8cf12011-07-25 15:25:41 +00002280 if (Stage < RS_Split) {
2281 setStage(VirtReg, RS_Split);
Jakob Stoklund Olesen86985072011-03-19 23:02:47 +00002282 DEBUG(dbgs() << "wait for second round\n");
Mark Laceyf9ea8852013-08-14 23:50:04 +00002283 NewVRegs.push_back(VirtReg.reg);
Jakob Stoklund Olesene68a27e2011-02-24 23:21:36 +00002284 return 0;
2285 }
2286
Jakob Stoklund Olesena5c88992011-05-06 21:58:30 +00002287 // If we couldn't allocate a register from spilling, there is probably some
2288 // invalid inline assembly. The base class wil report it.
Jakob Stoklund Olesen3ef8cf12011-07-25 15:25:41 +00002289 if (Stage >= RS_Done || !VirtReg.isSpillable())
Quentin Colombet87769712014-02-05 22:13:59 +00002290 return tryLastChanceRecoloring(VirtReg, Order, NewVRegs, FixedRegisters,
2291 Depth);
Jakob Stoklund Olesen5f9f0812011-03-01 21:10:07 +00002292
Jakob Stoklund Olesen903b6d32010-12-14 00:37:49 +00002293 // Try splitting VirtReg or interferences.
Jakob Stoklund Olesen9fb04012011-01-19 22:11:48 +00002294 unsigned PhysReg = trySplit(VirtReg, Order, NewVRegs);
2295 if (PhysReg || !NewVRegs.empty())
Jakob Stoklund Olesen3d7b8062010-12-14 00:37:44 +00002296 return PhysReg;
2297
Jakob Stoklund Olesen0acb69d2010-12-22 22:01:30 +00002298 // Finally spill VirtReg itself.
Jakob Stoklund Olesen92da7052010-12-11 00:19:56 +00002299 NamedRegionTimer T("Spiller", TimerGroupName, TimePassesIsEnabled);
Jakob Stoklund Olesene5bbe372012-05-19 05:25:46 +00002300 LiveRangeEdit LRE(&VirtReg, NewVRegs, *MF, *LIS, VRM, this);
Jakob Stoklund Olesen4d6eafa2011-03-10 01:51:42 +00002301 spiller().spill(LRE);
Jakob Stoklund Olesen3ef8cf12011-07-25 15:25:41 +00002302 setStage(NewVRegs.begin(), NewVRegs.end(), RS_Done);
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +00002303
Jakob Stoklund Olesen557a82c2011-03-16 22:56:08 +00002304 if (VerifyEnabled)
2305 MF->verify(this, "After spilling");
2306
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +00002307 // The live virtual register requesting allocation was spilled, so tell
2308 // the caller not to allocate anything during this round.
2309 return 0;
2310}
2311
2312bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
2313 DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n"
David Blaikiec8c29202012-08-22 17:18:53 +00002314 << "********** Function: " << mf.getName() << '\n');
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +00002315
2316 MF = &mf;
Quentin Colombet1fb3362a2014-01-02 22:47:22 +00002317 TRI = MF->getTarget().getRegisterInfo();
2318 TII = MF->getTarget().getInstrInfo();
2319 RCI.runOnMachineFunction(mf);
Jakob Stoklund Olesen2e98ee32010-12-17 23:16:35 +00002320 if (VerifyEnabled)
Jakob Stoklund Olesenbf4550e2010-12-18 00:06:56 +00002321 MF->verify(this, "Before greedy register allocator");
Jakob Stoklund Olesen2e98ee32010-12-17 23:16:35 +00002322
Jakob Stoklund Olesen2d2dec92012-06-20 22:52:29 +00002323 RegAllocBase::init(getAnalysis<VirtRegMap>(),
2324 getAnalysis<LiveIntervals>(),
2325 getAnalysis<LiveRegMatrix>());
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +00002326 Indexes = &getAnalysis<SlotIndexes>();
Benjamin Kramere2a1d892013-06-17 19:00:36 +00002327 MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
Jakob Stoklund Olesen1740e002010-12-17 23:16:32 +00002328 DomTree = &getAnalysis<MachineDominatorTree>();
Jakob Stoklund Olesenadecb5e2010-12-10 22:54:44 +00002329 SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM));
Jakob Stoklund Olesene7601e92010-12-15 23:46:13 +00002330 Loops = &getAnalysis<MachineLoopInfo>();
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +00002331 Bundles = &getAnalysis<EdgeBundles>();
2332 SpillPlacer = &getAnalysis<SpillPlacement>();
Jakob Stoklund Olesenf8da0282011-05-06 18:00:02 +00002333 DebugVars = &getAnalysis<LiveDebugVariables>();
Jakob Stoklund Olesen267f6c12011-01-18 21:13:27 +00002334
Duncan P. N. Exon Smitha5df8132014-04-08 19:18:56 +00002335 initializeCSRCost();
2336
Arnaud A. de Grandmaisonea3ac162013-11-11 19:04:45 +00002337 calculateSpillWeightsAndHints(*LIS, mf, *Loops, *MBFI);
Arnaud A. de Grandmaison760c1e02013-11-10 17:46:31 +00002338
Andrew Trick97064962013-07-25 07:26:26 +00002339 DEBUG(LIS->dump());
2340
Jakob Stoklund Olesenf1a60a62011-02-19 00:53:42 +00002341 SA.reset(new SplitAnalysis(*VRM, *LIS, *Loops));
Benjamin Kramere2a1d892013-06-17 19:00:36 +00002342 SE.reset(new SplitEditor(*SA, *LIS, *VRM, *DomTree, *MBFI));
Jakob Stoklund Olesen30a85632011-07-02 01:37:09 +00002343 ExtraRegInfo.clear();
2344 ExtraRegInfo.resize(MRI->getNumVirtRegs());
2345 NextCascade = 1;
Jakob Stoklund Olesen96eebf02012-06-20 22:52:26 +00002346 IntfCache.init(MF, Matrix->getLiveUnions(), Indexes, LIS, TRI);
Jakob Stoklund Olesendab4b9a2011-07-26 23:41:46 +00002347 GlobalCand.resize(32); // This will grow as needed.
Jakob Stoklund Olesene7601e92010-12-15 23:46:13 +00002348
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +00002349 allocatePhysRegs();
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +00002350 releaseMemory();
Jakob Stoklund Olesenb8812a12010-12-08 03:26:16 +00002351 return true;
2352}