Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 1 | //===-- RegAllocGreedy.cpp - greedy register allocator --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the RAGreedy function pass for register allocation in |
| 11 | // optimized builds. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "regalloc" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/Passes.h" |
Jakob Stoklund Olesen | dd479e9 | 2010-12-10 22:21:05 +0000 | [diff] [blame] | 17 | #include "AllocationOrder.h" |
Jakob Stoklund Olesen | 5907d86 | 2011-04-02 06:03:35 +0000 | [diff] [blame] | 18 | #include "InterferenceCache.h" |
Jakob Stoklund Olesen | cfafc54 | 2011-04-05 21:40:37 +0000 | [diff] [blame] | 19 | #include "LiveDebugVariables.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 20 | #include "RegAllocBase.h" |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 21 | #include "SpillPlacement.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "Spiller.h" |
Jakob Stoklund Olesen | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 23 | #include "SplitKit.h" |
Jakob Stoklund Olesen | 0db841f | 2011-02-17 22:53:48 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Statistic.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/AliasAnalysis.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/CalcSpillWeights.h" |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/EdgeBundles.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Pete Cooper | 789d5d8 | 2012-04-02 22:44:18 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/LiveRangeEdit.h" |
Jakob Stoklund Olesen | 1ead68d | 2012-11-28 19:13:06 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/LiveRegMatrix.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/LiveStackAnalysis.h" |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineDominators.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 35 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/RegAllocRegistry.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/VirtRegMap.h" |
| 38 | #include "llvm/PassAnalysisSupport.h" |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 39 | #include "llvm/Support/CommandLine.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Debug.h" |
| 41 | #include "llvm/Support/ErrorHandling.h" |
Jakob Stoklund Olesen | 533f58e | 2010-12-11 00:19:56 +0000 | [diff] [blame] | 42 | #include "llvm/Support/Timer.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 43 | #include "llvm/Support/raw_ostream.h" |
| 44 | #include "llvm/Target/TargetOptions.h" |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 45 | #include <queue> |
| 46 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 47 | using namespace llvm; |
| 48 | |
Jakob Stoklund Olesen | 0db841f | 2011-02-17 22:53:48 +0000 | [diff] [blame] | 49 | STATISTIC(NumGlobalSplits, "Number of split global live ranges"); |
| 50 | STATISTIC(NumLocalSplits, "Number of split local live ranges"); |
Jakob Stoklund Olesen | 0db841f | 2011-02-17 22:53:48 +0000 | [diff] [blame] | 51 | STATISTIC(NumEvicted, "Number of interferences evicted"); |
| 52 | |
Jakob Stoklund Olesen | 708d06f | 2011-09-12 16:49:21 +0000 | [diff] [blame] | 53 | static cl::opt<SplitEditor::ComplementSpillMode> |
| 54 | SplitSpillMode("split-spill-mode", cl::Hidden, |
| 55 | cl::desc("Spill mode for splitting live ranges"), |
| 56 | cl::values(clEnumValN(SplitEditor::SM_Partition, "default", "Default"), |
| 57 | clEnumValN(SplitEditor::SM_Size, "size", "Optimize for size"), |
| 58 | clEnumValN(SplitEditor::SM_Speed, "speed", "Optimize for speed"), |
| 59 | clEnumValEnd), |
| 60 | cl::init(SplitEditor::SM_Partition)); |
| 61 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 62 | static RegisterRegAlloc greedyRegAlloc("greedy", "greedy register allocator", |
| 63 | createGreedyRegisterAllocator); |
| 64 | |
| 65 | namespace { |
Jakob Stoklund Olesen | 92a55f4 | 2011-03-09 00:57:29 +0000 | [diff] [blame] | 66 | class RAGreedy : public MachineFunctionPass, |
| 67 | public RegAllocBase, |
| 68 | private LiveRangeEdit::Delegate { |
| 69 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 70 | // context |
| 71 | MachineFunction *MF; |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 72 | |
| 73 | // analyses |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 74 | SlotIndexes *Indexes; |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 75 | MachineDominatorTree *DomTree; |
Jakob Stoklund Olesen | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 76 | MachineLoopInfo *Loops; |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 77 | EdgeBundles *Bundles; |
| 78 | SpillPlacement *SpillPlacer; |
Jakob Stoklund Olesen | f42b661 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 79 | LiveDebugVariables *DebugVars; |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 80 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 81 | // state |
| 82 | std::auto_ptr<Spiller> SpillerInstance; |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 83 | std::priority_queue<std::pair<unsigned, unsigned> > Queue; |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 84 | unsigned NextCascade; |
Jakob Stoklund Olesen | 22a1df6 | 2011-03-01 21:10:07 +0000 | [diff] [blame] | 85 | |
| 86 | // Live ranges pass through a number of stages as we try to allocate them. |
| 87 | // Some of the stages may also create new live ranges: |
| 88 | // |
| 89 | // - Region splitting. |
| 90 | // - Per-block splitting. |
| 91 | // - Local splitting. |
| 92 | // - Spilling. |
| 93 | // |
| 94 | // Ranges produced by one of the stages skip the previous stages when they are |
| 95 | // dequeued. This improves performance because we can skip interference checks |
| 96 | // that are unlikely to give any results. It also guarantees that the live |
| 97 | // range splitting algorithm terminates, something that is otherwise hard to |
| 98 | // ensure. |
| 99 | enum LiveRangeStage { |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 100 | /// Newly created live range that has never been queued. |
| 101 | RS_New, |
| 102 | |
| 103 | /// Only attempt assignment and eviction. Then requeue as RS_Split. |
| 104 | RS_Assign, |
| 105 | |
| 106 | /// Attempt live range splitting if assignment is impossible. |
| 107 | RS_Split, |
| 108 | |
Jakob Stoklund Olesen | 49743b1 | 2011-07-25 15:25:43 +0000 | [diff] [blame] | 109 | /// Attempt more aggressive live range splitting that is guaranteed to make |
| 110 | /// progress. This is used for split products that may not be making |
| 111 | /// progress. |
| 112 | RS_Split2, |
| 113 | |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 114 | /// Live range will be spilled. No more splitting will be attempted. |
| 115 | RS_Spill, |
| 116 | |
| 117 | /// There is nothing more we can do to this live range. Abort compilation |
| 118 | /// if it can't be assigned. |
| 119 | RS_Done |
Jakob Stoklund Olesen | 22a1df6 | 2011-03-01 21:10:07 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
Jakob Stoklund Olesen | b8d936b | 2011-05-25 23:58:36 +0000 | [diff] [blame] | 122 | static const char *const StageName[]; |
| 123 | |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 124 | // RegInfo - Keep additional information about each live range. |
| 125 | struct RegInfo { |
| 126 | LiveRangeStage Stage; |
| 127 | |
| 128 | // Cascade - Eviction loop prevention. See canEvictInterference(). |
| 129 | unsigned Cascade; |
| 130 | |
| 131 | RegInfo() : Stage(RS_New), Cascade(0) {} |
| 132 | }; |
| 133 | |
| 134 | IndexedMap<RegInfo, VirtReg2IndexFunctor> ExtraRegInfo; |
Jakob Stoklund Olesen | 22a1df6 | 2011-03-01 21:10:07 +0000 | [diff] [blame] | 135 | |
| 136 | LiveRangeStage getStage(const LiveInterval &VirtReg) const { |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 137 | return ExtraRegInfo[VirtReg.reg].Stage; |
| 138 | } |
| 139 | |
| 140 | void setStage(const LiveInterval &VirtReg, LiveRangeStage Stage) { |
| 141 | ExtraRegInfo.resize(MRI->getNumVirtRegs()); |
| 142 | ExtraRegInfo[VirtReg.reg].Stage = Stage; |
Jakob Stoklund Olesen | 22a1df6 | 2011-03-01 21:10:07 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | template<typename Iterator> |
| 146 | void setStage(Iterator Begin, Iterator End, LiveRangeStage NewStage) { |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 147 | ExtraRegInfo.resize(MRI->getNumVirtRegs()); |
Jakob Stoklund Olesen | f22ca3f | 2011-03-30 02:52:39 +0000 | [diff] [blame] | 148 | for (;Begin != End; ++Begin) { |
| 149 | unsigned Reg = (*Begin)->reg; |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 150 | if (ExtraRegInfo[Reg].Stage == RS_New) |
| 151 | ExtraRegInfo[Reg].Stage = NewStage; |
Jakob Stoklund Olesen | f22ca3f | 2011-03-30 02:52:39 +0000 | [diff] [blame] | 152 | } |
Jakob Stoklund Olesen | 22a1df6 | 2011-03-01 21:10:07 +0000 | [diff] [blame] | 153 | } |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 154 | |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 155 | /// Cost of evicting interference. |
| 156 | struct EvictionCost { |
| 157 | unsigned BrokenHints; ///< Total number of broken hints. |
| 158 | float MaxWeight; ///< Maximum spill weight evicted. |
| 159 | |
| 160 | EvictionCost(unsigned B = 0) : BrokenHints(B), MaxWeight(0) {} |
| 161 | |
| 162 | bool operator<(const EvictionCost &O) const { |
| 163 | if (BrokenHints != O.BrokenHints) |
| 164 | return BrokenHints < O.BrokenHints; |
| 165 | return MaxWeight < O.MaxWeight; |
| 166 | } |
| 167 | }; |
| 168 | |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 169 | // splitting state. |
Jakob Stoklund Olesen | 22a1df6 | 2011-03-01 21:10:07 +0000 | [diff] [blame] | 170 | std::auto_ptr<SplitAnalysis> SA; |
Jakob Stoklund Olesen | bece06f | 2011-03-03 01:29:13 +0000 | [diff] [blame] | 171 | std::auto_ptr<SplitEditor> SE; |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 172 | |
Jakob Stoklund Olesen | eda0fe8 | 2011-04-02 06:03:38 +0000 | [diff] [blame] | 173 | /// Cached per-block interference maps |
| 174 | InterferenceCache IntfCache; |
| 175 | |
Jakob Stoklund Olesen | 7b41fbe | 2011-04-07 17:27:46 +0000 | [diff] [blame] | 176 | /// All basic blocks where the current register has uses. |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 177 | SmallVector<SpillPlacement::BlockConstraint, 8> SplitConstraints; |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 178 | |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 179 | /// Global live range splitting candidate info. |
| 180 | struct GlobalSplitCandidate { |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 181 | // Register intended for assignment, or 0. |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 182 | unsigned PhysReg; |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 183 | |
| 184 | // SplitKit interval index for this candidate. |
| 185 | unsigned IntvIdx; |
| 186 | |
| 187 | // Interference for PhysReg. |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 188 | InterferenceCache::Cursor Intf; |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 189 | |
| 190 | // Bundles where this candidate should be live. |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 191 | BitVector LiveBundles; |
Jakob Stoklund Olesen | 5db4289 | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 192 | SmallVector<unsigned, 8> ActiveBlocks; |
| 193 | |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 194 | void reset(InterferenceCache &Cache, unsigned Reg) { |
Jakob Stoklund Olesen | 5db4289 | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 195 | PhysReg = Reg; |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 196 | IntvIdx = 0; |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 197 | Intf.setPhysReg(Cache, Reg); |
Jakob Stoklund Olesen | 5db4289 | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 198 | LiveBundles.clear(); |
| 199 | ActiveBlocks.clear(); |
| 200 | } |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 201 | |
| 202 | // Set B[i] = C for every live bundle where B[i] was NoCand. |
| 203 | unsigned getBundles(SmallVectorImpl<unsigned> &B, unsigned C) { |
| 204 | unsigned Count = 0; |
| 205 | for (int i = LiveBundles.find_first(); i >= 0; |
| 206 | i = LiveBundles.find_next(i)) |
| 207 | if (B[i] == NoCand) { |
| 208 | B[i] = C; |
| 209 | Count++; |
| 210 | } |
| 211 | return Count; |
| 212 | } |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | /// Candidate info for for each PhysReg in AllocationOrder. |
| 216 | /// This vector never shrinks, but grows to the size of the largest register |
| 217 | /// class. |
| 218 | SmallVector<GlobalSplitCandidate, 32> GlobalCand; |
| 219 | |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 220 | enum { NoCand = ~0u }; |
| 221 | |
| 222 | /// Candidate map. Each edge bundle is assigned to a GlobalCand entry, or to |
| 223 | /// NoCand which indicates the stack interval. |
| 224 | SmallVector<unsigned, 32> BundleCand; |
| 225 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 226 | public: |
| 227 | RAGreedy(); |
| 228 | |
| 229 | /// Return the pass name. |
| 230 | virtual const char* getPassName() const { |
Jakob Stoklund Olesen | 533f58e | 2010-12-11 00:19:56 +0000 | [diff] [blame] | 231 | return "Greedy Register Allocator"; |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /// RAGreedy analysis usage. |
| 235 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 236 | virtual void releaseMemory(); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 237 | virtual Spiller &spiller() { return *SpillerInstance; } |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 238 | virtual void enqueue(LiveInterval *LI); |
| 239 | virtual LiveInterval *dequeue(); |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 240 | virtual unsigned selectOrSplit(LiveInterval&, |
| 241 | SmallVectorImpl<LiveInterval*>&); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 242 | |
| 243 | /// Perform register allocation. |
| 244 | virtual bool runOnMachineFunction(MachineFunction &mf); |
| 245 | |
| 246 | static char ID; |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 247 | |
| 248 | private: |
Jakob Stoklund Olesen | 7792e98 | 2011-03-13 01:23:11 +0000 | [diff] [blame] | 249 | bool LRE_CanEraseVirtReg(unsigned); |
Jakob Stoklund Olesen | 1d5b845 | 2011-03-16 22:56:16 +0000 | [diff] [blame] | 250 | void LRE_WillShrinkVirtReg(unsigned); |
Jakob Stoklund Olesen | f22ca3f | 2011-03-30 02:52:39 +0000 | [diff] [blame] | 251 | void LRE_DidCloneVirtReg(unsigned, unsigned); |
Jakob Stoklund Olesen | 92a55f4 | 2011-03-09 00:57:29 +0000 | [diff] [blame] | 252 | |
Jakob Stoklund Olesen | 2007298 | 2011-04-22 22:47:40 +0000 | [diff] [blame] | 253 | float calcSpillCost(); |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 254 | bool addSplitConstraints(InterferenceCache::Cursor, float&); |
| 255 | void addThroughConstraints(InterferenceCache::Cursor, ArrayRef<unsigned>); |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 256 | void growRegion(GlobalSplitCandidate &Cand); |
| 257 | float calcGlobalSplitCost(GlobalSplitCandidate&); |
Jakob Stoklund Olesen | 87972fa | 2011-07-23 03:41:57 +0000 | [diff] [blame] | 258 | bool calcCompactRegion(GlobalSplitCandidate&); |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 259 | void splitAroundRegion(LiveRangeEdit&, ArrayRef<unsigned>); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 260 | void calcGapWeights(unsigned, SmallVectorImpl<float>&); |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 261 | bool shouldEvict(LiveInterval &A, bool, LiveInterval &B, bool); |
| 262 | bool canEvictInterference(LiveInterval&, unsigned, bool, EvictionCost&); |
| 263 | void evictInterference(LiveInterval&, unsigned, |
| 264 | SmallVectorImpl<LiveInterval*>&); |
Jakob Stoklund Olesen | b64d92e | 2010-12-14 00:37:44 +0000 | [diff] [blame] | 265 | |
Jakob Stoklund Olesen | 6bfba2e | 2011-04-20 18:19:48 +0000 | [diff] [blame] | 266 | unsigned tryAssign(LiveInterval&, AllocationOrder&, |
| 267 | SmallVectorImpl<LiveInterval*>&); |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 268 | unsigned tryEvict(LiveInterval&, AllocationOrder&, |
Jakob Stoklund Olesen | 6bfba2e | 2011-04-20 18:19:48 +0000 | [diff] [blame] | 269 | SmallVectorImpl<LiveInterval*>&, unsigned = ~0u); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 270 | unsigned tryRegionSplit(LiveInterval&, AllocationOrder&, |
| 271 | SmallVectorImpl<LiveInterval*>&); |
Jakob Stoklund Olesen | dab35d3 | 2011-08-05 23:04:18 +0000 | [diff] [blame] | 272 | unsigned tryBlockSplit(LiveInterval&, AllocationOrder&, |
| 273 | SmallVectorImpl<LiveInterval*>&); |
Jakob Stoklund Olesen | d74d284 | 2012-05-23 22:37:27 +0000 | [diff] [blame] | 274 | unsigned tryInstructionSplit(LiveInterval&, AllocationOrder&, |
| 275 | SmallVectorImpl<LiveInterval*>&); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 276 | unsigned tryLocalSplit(LiveInterval&, AllocationOrder&, |
| 277 | SmallVectorImpl<LiveInterval*>&); |
Jakob Stoklund Olesen | b64d92e | 2010-12-14 00:37:44 +0000 | [diff] [blame] | 278 | unsigned trySplit(LiveInterval&, AllocationOrder&, |
| 279 | SmallVectorImpl<LiveInterval*>&); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 280 | }; |
| 281 | } // end anonymous namespace |
| 282 | |
| 283 | char RAGreedy::ID = 0; |
| 284 | |
Jakob Stoklund Olesen | b8d936b | 2011-05-25 23:58:36 +0000 | [diff] [blame] | 285 | #ifndef NDEBUG |
| 286 | const char *const RAGreedy::StageName[] = { |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 287 | "RS_New", |
| 288 | "RS_Assign", |
| 289 | "RS_Split", |
Jakob Stoklund Olesen | 49743b1 | 2011-07-25 15:25:43 +0000 | [diff] [blame] | 290 | "RS_Split2", |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 291 | "RS_Spill", |
| 292 | "RS_Done" |
Jakob Stoklund Olesen | b8d936b | 2011-05-25 23:58:36 +0000 | [diff] [blame] | 293 | }; |
| 294 | #endif |
| 295 | |
Jakob Stoklund Olesen | 2007298 | 2011-04-22 22:47:40 +0000 | [diff] [blame] | 296 | // Hysteresis to use when comparing floats. |
| 297 | // This helps stabilize decisions based on float comparisons. |
| 298 | const float Hysteresis = 0.98f; |
| 299 | |
| 300 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 301 | FunctionPass* llvm::createGreedyRegisterAllocator() { |
| 302 | return new RAGreedy(); |
| 303 | } |
| 304 | |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 305 | RAGreedy::RAGreedy(): MachineFunctionPass(ID) { |
Jakob Stoklund Olesen | cfafc54 | 2011-04-05 21:40:37 +0000 | [diff] [blame] | 306 | initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry()); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 307 | initializeSlotIndexesPass(*PassRegistry::getPassRegistry()); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 308 | initializeLiveIntervalsPass(*PassRegistry::getPassRegistry()); |
| 309 | initializeSlotIndexesPass(*PassRegistry::getPassRegistry()); |
Rafael Espindola | 5b22021 | 2011-06-26 22:34:10 +0000 | [diff] [blame] | 310 | initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry()); |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 311 | initializeMachineSchedulerPass(*PassRegistry::getPassRegistry()); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 312 | initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry()); |
| 313 | initializeLiveStacksPass(*PassRegistry::getPassRegistry()); |
| 314 | initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry()); |
| 315 | initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry()); |
| 316 | initializeVirtRegMapPass(*PassRegistry::getPassRegistry()); |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 317 | initializeLiveRegMatrixPass(*PassRegistry::getPassRegistry()); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 318 | initializeEdgeBundlesPass(*PassRegistry::getPassRegistry()); |
| 319 | initializeSpillPlacementPass(*PassRegistry::getPassRegistry()); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const { |
| 323 | AU.setPreservesCFG(); |
| 324 | AU.addRequired<AliasAnalysis>(); |
| 325 | AU.addPreserved<AliasAnalysis>(); |
| 326 | AU.addRequired<LiveIntervals>(); |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 327 | AU.addPreserved<LiveIntervals>(); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 328 | AU.addRequired<SlotIndexes>(); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 329 | AU.addPreserved<SlotIndexes>(); |
Jakob Stoklund Olesen | cfafc54 | 2011-04-05 21:40:37 +0000 | [diff] [blame] | 330 | AU.addRequired<LiveDebugVariables>(); |
| 331 | AU.addPreserved<LiveDebugVariables>(); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 332 | AU.addRequired<LiveStacks>(); |
| 333 | AU.addPreserved<LiveStacks>(); |
Evan Cheng | bb36a43 | 2012-09-21 20:04:28 +0000 | [diff] [blame] | 334 | AU.addRequired<CalculateSpillWeights>(); |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 335 | AU.addRequired<MachineDominatorTree>(); |
| 336 | AU.addPreserved<MachineDominatorTree>(); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 337 | AU.addRequired<MachineLoopInfo>(); |
| 338 | AU.addPreserved<MachineLoopInfo>(); |
| 339 | AU.addRequired<VirtRegMap>(); |
| 340 | AU.addPreserved<VirtRegMap>(); |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 341 | AU.addRequired<LiveRegMatrix>(); |
| 342 | AU.addPreserved<LiveRegMatrix>(); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 343 | AU.addRequired<EdgeBundles>(); |
| 344 | AU.addRequired<SpillPlacement>(); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 345 | MachineFunctionPass::getAnalysisUsage(AU); |
| 346 | } |
| 347 | |
Jakob Stoklund Olesen | 92a55f4 | 2011-03-09 00:57:29 +0000 | [diff] [blame] | 348 | |
| 349 | //===----------------------------------------------------------------------===// |
| 350 | // LiveRangeEdit delegate methods |
| 351 | //===----------------------------------------------------------------------===// |
| 352 | |
Jakob Stoklund Olesen | 7792e98 | 2011-03-13 01:23:11 +0000 | [diff] [blame] | 353 | bool RAGreedy::LRE_CanEraseVirtReg(unsigned VirtReg) { |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 354 | if (VRM->hasPhys(VirtReg)) { |
| 355 | Matrix->unassign(LIS->getInterval(VirtReg)); |
Jakob Stoklund Olesen | 7792e98 | 2011-03-13 01:23:11 +0000 | [diff] [blame] | 356 | return true; |
| 357 | } |
| 358 | // Unassigned virtreg is probably in the priority queue. |
| 359 | // RegAllocBase will erase it after dequeueing. |
| 360 | return false; |
| 361 | } |
Jakob Stoklund Olesen | 92a55f4 | 2011-03-09 00:57:29 +0000 | [diff] [blame] | 362 | |
Jakob Stoklund Olesen | 1d5b845 | 2011-03-16 22:56:16 +0000 | [diff] [blame] | 363 | void RAGreedy::LRE_WillShrinkVirtReg(unsigned VirtReg) { |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 364 | if (!VRM->hasPhys(VirtReg)) |
Jakob Stoklund Olesen | 1d5b845 | 2011-03-16 22:56:16 +0000 | [diff] [blame] | 365 | return; |
| 366 | |
| 367 | // Register is assigned, put it back on the queue for reassignment. |
| 368 | LiveInterval &LI = LIS->getInterval(VirtReg); |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 369 | Matrix->unassign(LI); |
Jakob Stoklund Olesen | 1d5b845 | 2011-03-16 22:56:16 +0000 | [diff] [blame] | 370 | enqueue(&LI); |
| 371 | } |
| 372 | |
Jakob Stoklund Olesen | f22ca3f | 2011-03-30 02:52:39 +0000 | [diff] [blame] | 373 | void RAGreedy::LRE_DidCloneVirtReg(unsigned New, unsigned Old) { |
Jakob Stoklund Olesen | 0d4fea7 | 2011-09-14 17:34:37 +0000 | [diff] [blame] | 374 | // Cloning a register we haven't even heard about yet? Just ignore it. |
| 375 | if (!ExtraRegInfo.inBounds(Old)) |
| 376 | return; |
| 377 | |
Jakob Stoklund Olesen | f22ca3f | 2011-03-30 02:52:39 +0000 | [diff] [blame] | 378 | // LRE may clone a virtual register because dead code elimination causes it to |
Jakob Stoklund Olesen | 165e231 | 2011-07-26 00:54:56 +0000 | [diff] [blame] | 379 | // be split into connected components. The new components are much smaller |
| 380 | // than the original, so they should get a new chance at being assigned. |
Jakob Stoklund Olesen | f22ca3f | 2011-03-30 02:52:39 +0000 | [diff] [blame] | 381 | // same stage as the parent. |
Jakob Stoklund Olesen | 165e231 | 2011-07-26 00:54:56 +0000 | [diff] [blame] | 382 | ExtraRegInfo[Old].Stage = RS_Assign; |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 383 | ExtraRegInfo.grow(New); |
| 384 | ExtraRegInfo[New] = ExtraRegInfo[Old]; |
Jakob Stoklund Olesen | f22ca3f | 2011-03-30 02:52:39 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 387 | void RAGreedy::releaseMemory() { |
| 388 | SpillerInstance.reset(0); |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 389 | ExtraRegInfo.clear(); |
Jakob Stoklund Olesen | 5db4289 | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 390 | GlobalCand.clear(); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 393 | void RAGreedy::enqueue(LiveInterval *LI) { |
| 394 | // Prioritize live ranges by size, assigning larger ranges first. |
| 395 | // The queue holds (size, reg) pairs. |
Jakob Stoklund Olesen | 107d366 | 2011-02-24 23:21:36 +0000 | [diff] [blame] | 396 | const unsigned Size = LI->getSize(); |
| 397 | const unsigned Reg = LI->reg; |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 398 | assert(TargetRegisterInfo::isVirtualRegister(Reg) && |
| 399 | "Can only enqueue virtual registers"); |
Jakob Stoklund Olesen | 107d366 | 2011-02-24 23:21:36 +0000 | [diff] [blame] | 400 | unsigned Prio; |
Jakob Stoklund Olesen | 90c1d7d | 2010-12-08 22:57:16 +0000 | [diff] [blame] | 401 | |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 402 | ExtraRegInfo.grow(Reg); |
| 403 | if (ExtraRegInfo[Reg].Stage == RS_New) |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 404 | ExtraRegInfo[Reg].Stage = RS_Assign; |
Jakob Stoklund Olesen | f22ca3f | 2011-03-30 02:52:39 +0000 | [diff] [blame] | 405 | |
Jakob Stoklund Olesen | cc07e04 | 2011-07-28 20:48:23 +0000 | [diff] [blame] | 406 | if (ExtraRegInfo[Reg].Stage == RS_Split) { |
Jakob Stoklund Olesen | eb29157 | 2011-03-27 22:49:21 +0000 | [diff] [blame] | 407 | // Unsplit ranges that couldn't be allocated immediately are deferred until |
Jakob Stoklund Olesen | a16a25d | 2011-09-12 16:54:42 +0000 | [diff] [blame] | 408 | // everything else has been allocated. |
| 409 | Prio = Size; |
Jakob Stoklund Olesen | cc07e04 | 2011-07-28 20:48:23 +0000 | [diff] [blame] | 410 | } else { |
Jakob Stoklund Olesen | a16a25d | 2011-09-12 16:54:42 +0000 | [diff] [blame] | 411 | // Everything is allocated in long->short order. Long ranges that don't fit |
| 412 | // should be spilled (or split) ASAP so they don't create interference. |
Jakob Stoklund Olesen | 107d366 | 2011-02-24 23:21:36 +0000 | [diff] [blame] | 413 | Prio = (1u << 31) + Size; |
Jakob Stoklund Olesen | d2a5073 | 2011-02-23 00:56:56 +0000 | [diff] [blame] | 414 | |
Jakob Stoklund Olesen | eb29157 | 2011-03-27 22:49:21 +0000 | [diff] [blame] | 415 | // Boost ranges that have a physical register hint. |
Jakob Stoklund Olesen | fc63744 | 2012-12-03 23:23:50 +0000 | [diff] [blame] | 416 | if (VRM->hasKnownPreference(Reg)) |
Jakob Stoklund Olesen | eb29157 | 2011-03-27 22:49:21 +0000 | [diff] [blame] | 417 | Prio |= (1u << 30); |
| 418 | } |
Jakob Stoklund Olesen | 107d366 | 2011-02-24 23:21:36 +0000 | [diff] [blame] | 419 | |
Jakob Stoklund Olesen | e3b23cd | 2012-04-02 22:30:39 +0000 | [diff] [blame] | 420 | Queue.push(std::make_pair(Prio, ~Reg)); |
Jakob Stoklund Olesen | 90c1d7d | 2010-12-08 22:57:16 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 423 | LiveInterval *RAGreedy::dequeue() { |
| 424 | if (Queue.empty()) |
| 425 | return 0; |
Jakob Stoklund Olesen | e3b23cd | 2012-04-02 22:30:39 +0000 | [diff] [blame] | 426 | LiveInterval *LI = &LIS->getInterval(~Queue.top().second); |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 427 | Queue.pop(); |
| 428 | return LI; |
| 429 | } |
Jakob Stoklund Olesen | 770d42d | 2010-12-22 22:01:30 +0000 | [diff] [blame] | 430 | |
Jakob Stoklund Olesen | 6bfba2e | 2011-04-20 18:19:48 +0000 | [diff] [blame] | 431 | |
| 432 | //===----------------------------------------------------------------------===// |
| 433 | // Direct Assignment |
| 434 | //===----------------------------------------------------------------------===// |
| 435 | |
| 436 | /// tryAssign - Try to assign VirtReg to an available register. |
| 437 | unsigned RAGreedy::tryAssign(LiveInterval &VirtReg, |
| 438 | AllocationOrder &Order, |
| 439 | SmallVectorImpl<LiveInterval*> &NewVRegs) { |
| 440 | Order.rewind(); |
| 441 | unsigned PhysReg; |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 442 | while ((PhysReg = Order.next())) |
| 443 | if (!Matrix->checkInterference(VirtReg, PhysReg)) |
Jakob Stoklund Olesen | 6bfba2e | 2011-04-20 18:19:48 +0000 | [diff] [blame] | 444 | break; |
Jakob Stoklund Olesen | f7999fe | 2012-12-04 22:25:16 +0000 | [diff] [blame^] | 445 | if (!PhysReg || Order.isHint()) |
Jakob Stoklund Olesen | 6bfba2e | 2011-04-20 18:19:48 +0000 | [diff] [blame] | 446 | return PhysReg; |
| 447 | |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 448 | // PhysReg is available, but there may be a better choice. |
| 449 | |
| 450 | // If we missed a simple hint, try to cheaply evict interference from the |
| 451 | // preferred register. |
| 452 | if (unsigned Hint = MRI->getSimpleHint(VirtReg.reg)) |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 453 | if (Order.isHint(Hint)) { |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 454 | DEBUG(dbgs() << "missed hint " << PrintReg(Hint, TRI) << '\n'); |
| 455 | EvictionCost MaxCost(1); |
| 456 | if (canEvictInterference(VirtReg, Hint, true, MaxCost)) { |
| 457 | evictInterference(VirtReg, Hint, NewVRegs); |
| 458 | return Hint; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | // Try to evict interference from a cheaper alternative. |
Jakob Stoklund Olesen | 6bfba2e | 2011-04-20 18:19:48 +0000 | [diff] [blame] | 463 | unsigned Cost = TRI->getCostPerUse(PhysReg); |
| 464 | |
| 465 | // Most registers have 0 additional cost. |
| 466 | if (!Cost) |
| 467 | return PhysReg; |
| 468 | |
| 469 | DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is available at cost " << Cost |
| 470 | << '\n'); |
| 471 | unsigned CheapReg = tryEvict(VirtReg, Order, NewVRegs, Cost); |
| 472 | return CheapReg ? CheapReg : PhysReg; |
| 473 | } |
| 474 | |
| 475 | |
Jakob Stoklund Olesen | 770d42d | 2010-12-22 22:01:30 +0000 | [diff] [blame] | 476 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 477 | // Interference eviction |
| 478 | //===----------------------------------------------------------------------===// |
| 479 | |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 480 | /// shouldEvict - determine if A should evict the assigned live range B. The |
| 481 | /// eviction policy defined by this function together with the allocation order |
| 482 | /// defined by enqueue() decides which registers ultimately end up being split |
| 483 | /// and spilled. |
Jakob Stoklund Olesen | b8d936b | 2011-05-25 23:58:36 +0000 | [diff] [blame] | 484 | /// |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 485 | /// Cascade numbers are used to prevent infinite loops if this function is a |
| 486 | /// cyclic relation. |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 487 | /// |
| 488 | /// @param A The live range to be assigned. |
| 489 | /// @param IsHint True when A is about to be assigned to its preferred |
| 490 | /// register. |
| 491 | /// @param B The live range to be evicted. |
| 492 | /// @param BreaksHint True when B is already assigned to its preferred register. |
| 493 | bool RAGreedy::shouldEvict(LiveInterval &A, bool IsHint, |
| 494 | LiveInterval &B, bool BreaksHint) { |
Jakob Stoklund Olesen | 49743b1 | 2011-07-25 15:25:43 +0000 | [diff] [blame] | 495 | bool CanSplit = getStage(B) < RS_Spill; |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 496 | |
| 497 | // Be fairly aggressive about following hints as long as the evictee can be |
| 498 | // split. |
| 499 | if (CanSplit && IsHint && !BreaksHint) |
| 500 | return true; |
| 501 | |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 502 | return A.weight > B.weight; |
Jakob Stoklund Olesen | b8d936b | 2011-05-25 23:58:36 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 505 | /// canEvictInterference - Return true if all interferences between VirtReg and |
| 506 | /// PhysReg can be evicted. When OnlyCheap is set, don't do anything |
| 507 | /// |
| 508 | /// @param VirtReg Live range that is about to be assigned. |
| 509 | /// @param PhysReg Desired register for assignment. |
Dmitri Gribenko | 67c8978 | 2012-09-12 16:59:47 +0000 | [diff] [blame] | 510 | /// @param IsHint True when PhysReg is VirtReg's preferred register. |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 511 | /// @param MaxCost Only look for cheaper candidates and update with new cost |
| 512 | /// when returning true. |
| 513 | /// @returns True when interference can be evicted cheaper than MaxCost. |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 514 | bool RAGreedy::canEvictInterference(LiveInterval &VirtReg, unsigned PhysReg, |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 515 | bool IsHint, EvictionCost &MaxCost) { |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 516 | // It is only possible to evict virtual register interference. |
| 517 | if (Matrix->checkInterference(VirtReg, PhysReg) > LiveRegMatrix::IK_VirtReg) |
| 518 | return false; |
| 519 | |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 520 | // Find VirtReg's cascade number. This will be unassigned if VirtReg was never |
| 521 | // involved in an eviction before. If a cascade number was assigned, deny |
| 522 | // evicting anything with the same or a newer cascade number. This prevents |
| 523 | // infinite eviction loops. |
| 524 | // |
| 525 | // This works out so a register without a cascade number is allowed to evict |
| 526 | // anything, and it can be evicted by anything. |
| 527 | unsigned Cascade = ExtraRegInfo[VirtReg.reg].Cascade; |
| 528 | if (!Cascade) |
| 529 | Cascade = NextCascade; |
| 530 | |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 531 | EvictionCost Cost; |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 532 | for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { |
| 533 | LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units); |
Jakob Stoklund Olesen | 3f5bedf | 2011-04-11 21:47:01 +0000 | [diff] [blame] | 534 | // If there is 10 or more interferences, chances are one is heavier. |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 535 | if (Q.collectInterferingVRegs(10) >= 10) |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 536 | return false; |
| 537 | |
Jakob Stoklund Olesen | 3f5bedf | 2011-04-11 21:47:01 +0000 | [diff] [blame] | 538 | // Check if any interfering live range is heavier than MaxWeight. |
| 539 | for (unsigned i = Q.interferingVRegs().size(); i; --i) { |
| 540 | LiveInterval *Intf = Q.interferingVRegs()[i - 1]; |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 541 | assert(TargetRegisterInfo::isVirtualRegister(Intf->reg) && |
| 542 | "Only expecting virtual register interference from query"); |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 543 | // Never evict spill products. They cannot split or spill. |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 544 | if (getStage(*Intf) == RS_Done) |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 545 | return false; |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 546 | // Once a live range becomes small enough, it is urgent that we find a |
| 547 | // register for it. This is indicated by an infinite spill weight. These |
| 548 | // urgent live ranges get to evict almost anything. |
Jakob Stoklund Olesen | 9cda1be | 2012-05-30 21:46:58 +0000 | [diff] [blame] | 549 | // |
| 550 | // Also allow urgent evictions of unspillable ranges from a strictly |
| 551 | // larger allocation order. |
| 552 | bool Urgent = !VirtReg.isSpillable() && |
| 553 | (Intf->isSpillable() || |
| 554 | RegClassInfo.getNumAllocatableRegs(MRI->getRegClass(VirtReg.reg)) < |
| 555 | RegClassInfo.getNumAllocatableRegs(MRI->getRegClass(Intf->reg))); |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 556 | // Only evict older cascades or live ranges without a cascade. |
| 557 | unsigned IntfCascade = ExtraRegInfo[Intf->reg].Cascade; |
| 558 | if (Cascade <= IntfCascade) { |
| 559 | if (!Urgent) |
| 560 | return false; |
| 561 | // We permit breaking cascades for urgent evictions. It should be the |
| 562 | // last resort, though, so make it really expensive. |
| 563 | Cost.BrokenHints += 10; |
| 564 | } |
| 565 | // Would this break a satisfied hint? |
| 566 | bool BreaksHint = VRM->hasPreferredPhys(Intf->reg); |
| 567 | // Update eviction cost. |
| 568 | Cost.BrokenHints += BreaksHint; |
| 569 | Cost.MaxWeight = std::max(Cost.MaxWeight, Intf->weight); |
| 570 | // Abort if this would be too expensive. |
| 571 | if (!(Cost < MaxCost)) |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 572 | return false; |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 573 | // Finally, apply the eviction policy for non-urgent evictions. |
| 574 | if (!Urgent && !shouldEvict(VirtReg, IsHint, *Intf, BreaksHint)) |
Jakob Stoklund Olesen | d2056e5 | 2011-05-31 21:02:44 +0000 | [diff] [blame] | 575 | return false; |
Jakob Stoklund Olesen | 2710638 | 2011-02-09 01:14:03 +0000 | [diff] [blame] | 576 | } |
| 577 | } |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 578 | MaxCost = Cost; |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 579 | return true; |
| 580 | } |
Jakob Stoklund Olesen | 2710638 | 2011-02-09 01:14:03 +0000 | [diff] [blame] | 581 | |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 582 | /// evictInterference - Evict any interferring registers that prevent VirtReg |
| 583 | /// from being assigned to Physreg. This assumes that canEvictInterference |
| 584 | /// returned true. |
| 585 | void RAGreedy::evictInterference(LiveInterval &VirtReg, unsigned PhysReg, |
| 586 | SmallVectorImpl<LiveInterval*> &NewVRegs) { |
| 587 | // Make sure that VirtReg has a cascade number, and assign that cascade |
| 588 | // number to every evicted register. These live ranges than then only be |
| 589 | // evicted by a newer cascade, preventing infinite loops. |
| 590 | unsigned Cascade = ExtraRegInfo[VirtReg.reg].Cascade; |
| 591 | if (!Cascade) |
| 592 | Cascade = ExtraRegInfo[VirtReg.reg].Cascade = NextCascade++; |
| 593 | |
| 594 | DEBUG(dbgs() << "evicting " << PrintReg(PhysReg, TRI) |
| 595 | << " interference: Cascade " << Cascade << '\n'); |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 596 | |
| 597 | // Collect all interfering virtregs first. |
| 598 | SmallVector<LiveInterval*, 8> Intfs; |
| 599 | for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { |
| 600 | LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units); |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 601 | assert(Q.seenAllInterferences() && "Didn't check all interfererences."); |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 602 | ArrayRef<LiveInterval*> IVR = Q.interferingVRegs(); |
| 603 | Intfs.append(IVR.begin(), IVR.end()); |
| 604 | } |
| 605 | |
| 606 | // Evict them second. This will invalidate the queries. |
| 607 | for (unsigned i = 0, e = Intfs.size(); i != e; ++i) { |
| 608 | LiveInterval *Intf = Intfs[i]; |
| 609 | // The same VirtReg may be present in multiple RegUnits. Skip duplicates. |
| 610 | if (!VRM->hasPhys(Intf->reg)) |
| 611 | continue; |
| 612 | Matrix->unassign(*Intf); |
| 613 | assert((ExtraRegInfo[Intf->reg].Cascade < Cascade || |
| 614 | VirtReg.isSpillable() < Intf->isSpillable()) && |
| 615 | "Cannot decrease cascade number, illegal eviction"); |
| 616 | ExtraRegInfo[Intf->reg].Cascade = Cascade; |
| 617 | ++NumEvicted; |
| 618 | NewVRegs.push_back(Intf); |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 622 | /// tryEvict - Try to evict all interferences for a physreg. |
Jakob Stoklund Olesen | 76395c9 | 2011-06-01 18:45:02 +0000 | [diff] [blame] | 623 | /// @param VirtReg Currently unassigned virtual register. |
| 624 | /// @param Order Physregs to try. |
| 625 | /// @return Physreg to assign VirtReg, or 0. |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 626 | unsigned RAGreedy::tryEvict(LiveInterval &VirtReg, |
| 627 | AllocationOrder &Order, |
Jakob Stoklund Olesen | 6bfba2e | 2011-04-20 18:19:48 +0000 | [diff] [blame] | 628 | SmallVectorImpl<LiveInterval*> &NewVRegs, |
| 629 | unsigned CostPerUseLimit) { |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 630 | NamedRegionTimer T("Evict", TimerGroupName, TimePassesIsEnabled); |
| 631 | |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 632 | // Keep track of the cheapest interference seen so far. |
| 633 | EvictionCost BestCost(~0u); |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 634 | unsigned BestPhys = 0; |
| 635 | |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 636 | // When we are just looking for a reduced cost per use, don't break any |
| 637 | // hints, and only evict smaller spill weights. |
| 638 | if (CostPerUseLimit < ~0u) { |
| 639 | BestCost.BrokenHints = 0; |
| 640 | BestCost.MaxWeight = VirtReg.weight; |
| 641 | } |
| 642 | |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 643 | Order.rewind(); |
| 644 | while (unsigned PhysReg = Order.next()) { |
Jakob Stoklund Olesen | 6bfba2e | 2011-04-20 18:19:48 +0000 | [diff] [blame] | 645 | if (TRI->getCostPerUse(PhysReg) >= CostPerUseLimit) |
| 646 | continue; |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 647 | // The first use of a callee-saved register in a function has cost 1. |
| 648 | // Don't start using a CSR when the CostPerUseLimit is low. |
| 649 | if (CostPerUseLimit == 1) |
| 650 | if (unsigned CSR = RegClassInfo.getLastCalleeSavedAlias(PhysReg)) |
| 651 | if (!MRI->isPhysRegUsed(CSR)) { |
| 652 | DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " would clobber CSR " |
| 653 | << PrintReg(CSR, TRI) << '\n'); |
| 654 | continue; |
| 655 | } |
Jakob Stoklund Olesen | 6bfba2e | 2011-04-20 18:19:48 +0000 | [diff] [blame] | 656 | |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 657 | if (!canEvictInterference(VirtReg, PhysReg, false, BestCost)) |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 658 | continue; |
| 659 | |
| 660 | // Best so far. |
| 661 | BestPhys = PhysReg; |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 662 | |
Jakob Stoklund Olesen | 57f1e2c | 2011-02-25 01:04:22 +0000 | [diff] [blame] | 663 | // Stop if the hint can be used. |
Jakob Stoklund Olesen | f7999fe | 2012-12-04 22:25:16 +0000 | [diff] [blame^] | 664 | if (Order.isHint()) |
Jakob Stoklund Olesen | 57f1e2c | 2011-02-25 01:04:22 +0000 | [diff] [blame] | 665 | break; |
Jakob Stoklund Olesen | 2710638 | 2011-02-09 01:14:03 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 668 | if (!BestPhys) |
| 669 | return 0; |
| 670 | |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 671 | evictInterference(VirtReg, BestPhys, NewVRegs); |
Jakob Stoklund Olesen | 98c8141 | 2011-02-23 00:29:52 +0000 | [diff] [blame] | 672 | return BestPhys; |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Jakob Stoklund Olesen | 770d42d | 2010-12-22 22:01:30 +0000 | [diff] [blame] | 675 | |
| 676 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 677 | // Region Splitting |
| 678 | //===----------------------------------------------------------------------===// |
| 679 | |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 680 | /// addSplitConstraints - Fill out the SplitConstraints vector based on the |
| 681 | /// interference pattern in Physreg and its aliases. Add the constraints to |
| 682 | /// SpillPlacement and return the static cost of this split in Cost, assuming |
| 683 | /// that all preferences in SplitConstraints are met. |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 684 | /// Return false if there are no bundles with positive bias. |
| 685 | bool RAGreedy::addSplitConstraints(InterferenceCache::Cursor Intf, |
| 686 | float &Cost) { |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 687 | ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); |
Jakob Stoklund Olesen | eda0fe8 | 2011-04-02 06:03:38 +0000 | [diff] [blame] | 688 | |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 689 | // Reset interference dependent info. |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 690 | SplitConstraints.resize(UseBlocks.size()); |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 691 | float StaticCost = 0; |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 692 | for (unsigned i = 0; i != UseBlocks.size(); ++i) { |
| 693 | const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 694 | SpillPlacement::BlockConstraint &BC = SplitConstraints[i]; |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 695 | |
Jakob Stoklund Olesen | f0ac26c | 2011-02-09 22:50:26 +0000 | [diff] [blame] | 696 | BC.Number = BI.MBB->getNumber(); |
Jakob Stoklund Olesen | eda0fe8 | 2011-04-02 06:03:38 +0000 | [diff] [blame] | 697 | Intf.moveToBlock(BC.Number); |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 698 | BC.Entry = BI.LiveIn ? SpillPlacement::PrefReg : SpillPlacement::DontCare; |
| 699 | BC.Exit = BI.LiveOut ? SpillPlacement::PrefReg : SpillPlacement::DontCare; |
Jakob Stoklund Olesen | 5ebca79 | 2011-08-02 23:04:06 +0000 | [diff] [blame] | 700 | BC.ChangesValue = BI.FirstDef; |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 701 | |
Jakob Stoklund Olesen | eda0fe8 | 2011-04-02 06:03:38 +0000 | [diff] [blame] | 702 | if (!Intf.hasInterference()) |
| 703 | continue; |
| 704 | |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 705 | // Number of spill code instructions to insert. |
| 706 | unsigned Ins = 0; |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 707 | |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 708 | // Interference for the live-in value. |
Jakob Stoklund Olesen | eda0fe8 | 2011-04-02 06:03:38 +0000 | [diff] [blame] | 709 | if (BI.LiveIn) { |
Jakob Stoklund Olesen | 6c8afd7 | 2011-04-04 15:32:15 +0000 | [diff] [blame] | 710 | if (Intf.first() <= Indexes->getMBBStartIdx(BC.Number)) |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 711 | BC.Entry = SpillPlacement::MustSpill, ++Ins; |
Jakob Stoklund Olesen | fe62d92 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 712 | else if (Intf.first() < BI.FirstInstr) |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 713 | BC.Entry = SpillPlacement::PrefSpill, ++Ins; |
Jakob Stoklund Olesen | fe62d92 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 714 | else if (Intf.first() < BI.LastInstr) |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 715 | ++Ins; |
Jakob Stoklund Olesen | a50c539 | 2011-02-08 23:02:58 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 718 | // Interference for the live-out value. |
Jakob Stoklund Olesen | eda0fe8 | 2011-04-02 06:03:38 +0000 | [diff] [blame] | 719 | if (BI.LiveOut) { |
Jakob Stoklund Olesen | 612f780 | 2011-04-05 04:20:29 +0000 | [diff] [blame] | 720 | if (Intf.last() >= SA->getLastSplitPoint(BC.Number)) |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 721 | BC.Exit = SpillPlacement::MustSpill, ++Ins; |
Jakob Stoklund Olesen | fe62d92 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 722 | else if (Intf.last() > BI.LastInstr) |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 723 | BC.Exit = SpillPlacement::PrefSpill, ++Ins; |
Jakob Stoklund Olesen | fe62d92 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 724 | else if (Intf.last() > BI.FirstInstr) |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 725 | ++Ins; |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 726 | } |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 727 | |
| 728 | // Accumulate the total frequency of inserted spill code. |
| 729 | if (Ins) |
| 730 | StaticCost += Ins * SpillPlacer->getBlockFrequency(BC.Number); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 731 | } |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 732 | Cost = StaticCost; |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 733 | |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 734 | // Add constraints for use-blocks. Note that these are the only constraints |
| 735 | // that may add a positive bias, it is downhill from here. |
| 736 | SpillPlacer->addConstraints(SplitConstraints); |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 737 | return SpillPlacer->scanActiveBundles(); |
| 738 | } |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 739 | |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 740 | |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 741 | /// addThroughConstraints - Add constraints and links to SpillPlacer from the |
| 742 | /// live-through blocks in Blocks. |
| 743 | void RAGreedy::addThroughConstraints(InterferenceCache::Cursor Intf, |
| 744 | ArrayRef<unsigned> Blocks) { |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 745 | const unsigned GroupSize = 8; |
| 746 | SpillPlacement::BlockConstraint BCS[GroupSize]; |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 747 | unsigned TBS[GroupSize]; |
| 748 | unsigned B = 0, T = 0; |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 749 | |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 750 | for (unsigned i = 0; i != Blocks.size(); ++i) { |
| 751 | unsigned Number = Blocks[i]; |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 752 | Intf.moveToBlock(Number); |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 753 | |
Jakob Stoklund Olesen | 7b41fbe | 2011-04-07 17:27:46 +0000 | [diff] [blame] | 754 | if (!Intf.hasInterference()) { |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 755 | assert(T < GroupSize && "Array overflow"); |
| 756 | TBS[T] = Number; |
| 757 | if (++T == GroupSize) { |
Frits van Bommel | 39b5abf | 2011-07-18 12:00:32 +0000 | [diff] [blame] | 758 | SpillPlacer->addLinks(makeArrayRef(TBS, T)); |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 759 | T = 0; |
| 760 | } |
Jakob Stoklund Olesen | 7b41fbe | 2011-04-07 17:27:46 +0000 | [diff] [blame] | 761 | continue; |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 762 | } |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 763 | |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 764 | assert(B < GroupSize && "Array overflow"); |
| 765 | BCS[B].Number = Number; |
| 766 | |
Jakob Stoklund Olesen | 7b41fbe | 2011-04-07 17:27:46 +0000 | [diff] [blame] | 767 | // Interference for the live-in value. |
| 768 | if (Intf.first() <= Indexes->getMBBStartIdx(Number)) |
| 769 | BCS[B].Entry = SpillPlacement::MustSpill; |
| 770 | else |
| 771 | BCS[B].Entry = SpillPlacement::PrefSpill; |
| 772 | |
| 773 | // Interference for the live-out value. |
| 774 | if (Intf.last() >= SA->getLastSplitPoint(Number)) |
| 775 | BCS[B].Exit = SpillPlacement::MustSpill; |
| 776 | else |
| 777 | BCS[B].Exit = SpillPlacement::PrefSpill; |
| 778 | |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 779 | if (++B == GroupSize) { |
| 780 | ArrayRef<SpillPlacement::BlockConstraint> Array(BCS, B); |
| 781 | SpillPlacer->addConstraints(Array); |
| 782 | B = 0; |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 783 | } |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 786 | ArrayRef<SpillPlacement::BlockConstraint> Array(BCS, B); |
| 787 | SpillPlacer->addConstraints(Array); |
Frits van Bommel | 39b5abf | 2011-07-18 12:00:32 +0000 | [diff] [blame] | 788 | SpillPlacer->addLinks(makeArrayRef(TBS, T)); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 791 | void RAGreedy::growRegion(GlobalSplitCandidate &Cand) { |
Jakob Stoklund Olesen | 5db4289 | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 792 | // Keep track of through blocks that have not been added to SpillPlacer. |
| 793 | BitVector Todo = SA->getThroughBlocks(); |
| 794 | SmallVectorImpl<unsigned> &ActiveBlocks = Cand.ActiveBlocks; |
| 795 | unsigned AddedTo = 0; |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 796 | #ifndef NDEBUG |
| 797 | unsigned Visited = 0; |
| 798 | #endif |
Jakob Stoklund Olesen | 5db4289 | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 799 | |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 800 | for (;;) { |
| 801 | ArrayRef<unsigned> NewBundles = SpillPlacer->getRecentPositive(); |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 802 | // Find new through blocks in the periphery of PrefRegBundles. |
| 803 | for (int i = 0, e = NewBundles.size(); i != e; ++i) { |
| 804 | unsigned Bundle = NewBundles[i]; |
| 805 | // Look at all blocks connected to Bundle in the full graph. |
| 806 | ArrayRef<unsigned> Blocks = Bundles->getBlocks(Bundle); |
| 807 | for (ArrayRef<unsigned>::iterator I = Blocks.begin(), E = Blocks.end(); |
| 808 | I != E; ++I) { |
| 809 | unsigned Block = *I; |
Jakob Stoklund Olesen | 5db4289 | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 810 | if (!Todo.test(Block)) |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 811 | continue; |
Jakob Stoklund Olesen | 5db4289 | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 812 | Todo.reset(Block); |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 813 | // This is a new through block. Add it to SpillPlacer later. |
Jakob Stoklund Olesen | 5db4289 | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 814 | ActiveBlocks.push_back(Block); |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 815 | #ifndef NDEBUG |
| 816 | ++Visited; |
| 817 | #endif |
| 818 | } |
| 819 | } |
| 820 | // Any new blocks to add? |
Jakob Stoklund Olesen | 5490197 | 2011-07-05 18:46:42 +0000 | [diff] [blame] | 821 | if (ActiveBlocks.size() == AddedTo) |
| 822 | break; |
Jakob Stoklund Olesen | b466636 | 2011-07-23 03:22:33 +0000 | [diff] [blame] | 823 | |
| 824 | // Compute through constraints from the interference, or assume that all |
| 825 | // through blocks prefer spilling when forming compact regions. |
| 826 | ArrayRef<unsigned> NewBlocks = makeArrayRef(ActiveBlocks).slice(AddedTo); |
| 827 | if (Cand.PhysReg) |
| 828 | addThroughConstraints(Cand.Intf, NewBlocks); |
| 829 | else |
Jakob Stoklund Olesen | b87f91b | 2011-08-03 23:09:38 +0000 | [diff] [blame] | 830 | // Provide a strong negative bias on through blocks to prevent unwanted |
| 831 | // liveness on loop backedges. |
| 832 | SpillPlacer->addPrefSpill(NewBlocks, /* Strong= */ true); |
Jakob Stoklund Olesen | 5490197 | 2011-07-05 18:46:42 +0000 | [diff] [blame] | 833 | AddedTo = ActiveBlocks.size(); |
| 834 | |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 835 | // Perhaps iterating can enable more bundles? |
| 836 | SpillPlacer->iterate(); |
| 837 | } |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 838 | DEBUG(dbgs() << ", v=" << Visited); |
| 839 | } |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 840 | |
Jakob Stoklund Olesen | 87972fa | 2011-07-23 03:41:57 +0000 | [diff] [blame] | 841 | /// calcCompactRegion - Compute the set of edge bundles that should be live |
| 842 | /// when splitting the current live range into compact regions. Compact |
| 843 | /// regions can be computed without looking at interference. They are the |
| 844 | /// regions formed by removing all the live-through blocks from the live range. |
| 845 | /// |
| 846 | /// Returns false if the current live range is already compact, or if the |
| 847 | /// compact regions would form single block regions anyway. |
| 848 | bool RAGreedy::calcCompactRegion(GlobalSplitCandidate &Cand) { |
| 849 | // Without any through blocks, the live range is already compact. |
| 850 | if (!SA->getNumThroughBlocks()) |
| 851 | return false; |
| 852 | |
| 853 | // Compact regions don't correspond to any physreg. |
| 854 | Cand.reset(IntfCache, 0); |
| 855 | |
| 856 | DEBUG(dbgs() << "Compact region bundles"); |
| 857 | |
| 858 | // Use the spill placer to determine the live bundles. GrowRegion pretends |
| 859 | // that all the through blocks have interference when PhysReg is unset. |
| 860 | SpillPlacer->prepare(Cand.LiveBundles); |
| 861 | |
| 862 | // The static split cost will be zero since Cand.Intf reports no interference. |
| 863 | float Cost; |
| 864 | if (!addSplitConstraints(Cand.Intf, Cost)) { |
| 865 | DEBUG(dbgs() << ", none.\n"); |
| 866 | return false; |
| 867 | } |
| 868 | |
| 869 | growRegion(Cand); |
| 870 | SpillPlacer->finish(); |
| 871 | |
| 872 | if (!Cand.LiveBundles.any()) { |
| 873 | DEBUG(dbgs() << ", none.\n"); |
| 874 | return false; |
| 875 | } |
| 876 | |
| 877 | DEBUG({ |
| 878 | for (int i = Cand.LiveBundles.find_first(); i>=0; |
| 879 | i = Cand.LiveBundles.find_next(i)) |
| 880 | dbgs() << " EB#" << i; |
| 881 | dbgs() << ".\n"; |
| 882 | }); |
| 883 | return true; |
| 884 | } |
| 885 | |
Jakob Stoklund Olesen | 2007298 | 2011-04-22 22:47:40 +0000 | [diff] [blame] | 886 | /// calcSpillCost - Compute how expensive it would be to split the live range in |
| 887 | /// SA around all use blocks instead of forming bundle regions. |
| 888 | float RAGreedy::calcSpillCost() { |
| 889 | float Cost = 0; |
Jakob Stoklund Olesen | 2007298 | 2011-04-22 22:47:40 +0000 | [diff] [blame] | 890 | ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); |
| 891 | for (unsigned i = 0; i != UseBlocks.size(); ++i) { |
| 892 | const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; |
| 893 | unsigned Number = BI.MBB->getNumber(); |
| 894 | // We normally only need one spill instruction - a load or a store. |
| 895 | Cost += SpillPlacer->getBlockFrequency(Number); |
| 896 | |
| 897 | // Unless the value is redefined in the block. |
Jakob Stoklund Olesen | 3f5beed | 2011-08-02 23:04:08 +0000 | [diff] [blame] | 898 | if (BI.LiveIn && BI.LiveOut && BI.FirstDef) |
| 899 | Cost += SpillPlacer->getBlockFrequency(Number); |
Jakob Stoklund Olesen | 2007298 | 2011-04-22 22:47:40 +0000 | [diff] [blame] | 900 | } |
| 901 | return Cost; |
| 902 | } |
| 903 | |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 904 | /// calcGlobalSplitCost - Return the global split cost of following the split |
| 905 | /// pattern in LiveBundles. This cost should be added to the local cost of the |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 906 | /// interference pattern in SplitConstraints. |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 907 | /// |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 908 | float RAGreedy::calcGlobalSplitCost(GlobalSplitCandidate &Cand) { |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 909 | float GlobalCost = 0; |
Jakob Stoklund Olesen | 5db4289 | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 910 | const BitVector &LiveBundles = Cand.LiveBundles; |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 911 | ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); |
| 912 | for (unsigned i = 0; i != UseBlocks.size(); ++i) { |
| 913 | const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 914 | SpillPlacement::BlockConstraint &BC = SplitConstraints[i]; |
Jakob Stoklund Olesen | 874be74 | 2011-03-05 03:28:51 +0000 | [diff] [blame] | 915 | bool RegIn = LiveBundles[Bundles->getBundle(BC.Number, 0)]; |
| 916 | bool RegOut = LiveBundles[Bundles->getBundle(BC.Number, 1)]; |
| 917 | unsigned Ins = 0; |
| 918 | |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 919 | if (BI.LiveIn) |
| 920 | Ins += RegIn != (BC.Entry == SpillPlacement::PrefReg); |
| 921 | if (BI.LiveOut) |
| 922 | Ins += RegOut != (BC.Exit == SpillPlacement::PrefReg); |
Jakob Stoklund Olesen | 874be74 | 2011-03-05 03:28:51 +0000 | [diff] [blame] | 923 | if (Ins) |
| 924 | GlobalCost += Ins * SpillPlacer->getBlockFrequency(BC.Number); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 925 | } |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 926 | |
Jakob Stoklund Olesen | 5db4289 | 2011-04-12 21:30:53 +0000 | [diff] [blame] | 927 | for (unsigned i = 0, e = Cand.ActiveBlocks.size(); i != e; ++i) { |
| 928 | unsigned Number = Cand.ActiveBlocks[i]; |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 929 | bool RegIn = LiveBundles[Bundles->getBundle(Number, 0)]; |
| 930 | bool RegOut = LiveBundles[Bundles->getBundle(Number, 1)]; |
Jakob Stoklund Olesen | 9a54352 | 2011-04-06 21:32:41 +0000 | [diff] [blame] | 931 | if (!RegIn && !RegOut) |
| 932 | continue; |
| 933 | if (RegIn && RegOut) { |
| 934 | // We need double spill code if this block has interference. |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 935 | Cand.Intf.moveToBlock(Number); |
| 936 | if (Cand.Intf.hasInterference()) |
Jakob Stoklund Olesen | 9a54352 | 2011-04-06 21:32:41 +0000 | [diff] [blame] | 937 | GlobalCost += 2*SpillPlacer->getBlockFrequency(Number); |
| 938 | continue; |
| 939 | } |
| 940 | // live-in / stack-out or stack-in live-out. |
| 941 | GlobalCost += SpillPlacer->getBlockFrequency(Number); |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 942 | } |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 943 | return GlobalCost; |
| 944 | } |
| 945 | |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 946 | /// splitAroundRegion - Split the current live range around the regions |
| 947 | /// determined by BundleCand and GlobalCand. |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 948 | /// |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 949 | /// Before calling this function, GlobalCand and BundleCand must be initialized |
| 950 | /// so each bundle is assigned to a valid candidate, or NoCand for the |
| 951 | /// stack-bound bundles. The shared SA/SE SplitAnalysis and SplitEditor |
| 952 | /// objects must be initialized for the current live range, and intervals |
| 953 | /// created for the used candidates. |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 954 | /// |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 955 | /// @param LREdit The LiveRangeEdit object handling the current split. |
| 956 | /// @param UsedCands List of used GlobalCand entries. Every BundleCand value |
| 957 | /// must appear in this list. |
| 958 | void RAGreedy::splitAroundRegion(LiveRangeEdit &LREdit, |
| 959 | ArrayRef<unsigned> UsedCands) { |
| 960 | // These are the intervals created for new global ranges. We may create more |
| 961 | // intervals for local ranges. |
| 962 | const unsigned NumGlobalIntvs = LREdit.size(); |
| 963 | DEBUG(dbgs() << "splitAroundRegion with " << NumGlobalIntvs << " globals.\n"); |
| 964 | assert(NumGlobalIntvs && "No global intervals configured"); |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 965 | |
Jakob Stoklund Olesen | 2d6d86b | 2011-08-05 22:20:45 +0000 | [diff] [blame] | 966 | // Isolate even single instructions when dealing with a proper sub-class. |
Jakob Stoklund Olesen | 69145ba | 2011-08-06 18:20:24 +0000 | [diff] [blame] | 967 | // That guarantees register class inflation for the stack interval because it |
Jakob Stoklund Olesen | 2d6d86b | 2011-08-05 22:20:45 +0000 | [diff] [blame] | 968 | // is all copies. |
| 969 | unsigned Reg = SA->getParent().reg; |
| 970 | bool SingleInstrs = RegClassInfo.isProperSubClass(MRI->getRegClass(Reg)); |
| 971 | |
Jakob Stoklund Olesen | 87360f7 | 2011-06-30 01:30:39 +0000 | [diff] [blame] | 972 | // First handle all the blocks with uses. |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 973 | ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); |
| 974 | for (unsigned i = 0; i != UseBlocks.size(); ++i) { |
| 975 | const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 976 | unsigned Number = BI.MBB->getNumber(); |
| 977 | unsigned IntvIn = 0, IntvOut = 0; |
| 978 | SlotIndex IntfIn, IntfOut; |
| 979 | if (BI.LiveIn) { |
| 980 | unsigned CandIn = BundleCand[Bundles->getBundle(Number, 0)]; |
| 981 | if (CandIn != NoCand) { |
| 982 | GlobalSplitCandidate &Cand = GlobalCand[CandIn]; |
| 983 | IntvIn = Cand.IntvIdx; |
| 984 | Cand.Intf.moveToBlock(Number); |
| 985 | IntfIn = Cand.Intf.first(); |
| 986 | } |
| 987 | } |
| 988 | if (BI.LiveOut) { |
| 989 | unsigned CandOut = BundleCand[Bundles->getBundle(Number, 1)]; |
| 990 | if (CandOut != NoCand) { |
| 991 | GlobalSplitCandidate &Cand = GlobalCand[CandOut]; |
| 992 | IntvOut = Cand.IntvIdx; |
| 993 | Cand.Intf.moveToBlock(Number); |
| 994 | IntfOut = Cand.Intf.last(); |
| 995 | } |
| 996 | } |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 997 | |
Jakob Stoklund Olesen | fd5c513 | 2011-04-12 19:32:53 +0000 | [diff] [blame] | 998 | // Create separate intervals for isolated blocks with multiple uses. |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 999 | if (!IntvIn && !IntvOut) { |
Jakob Stoklund Olesen | fd5c513 | 2011-04-12 19:32:53 +0000 | [diff] [blame] | 1000 | DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " isolated.\n"); |
Jakob Stoklund Olesen | 2d6d86b | 2011-08-05 22:20:45 +0000 | [diff] [blame] | 1001 | if (SA->shouldSplitSingleBlock(BI, SingleInstrs)) |
Jakob Stoklund Olesen | 87360f7 | 2011-06-30 01:30:39 +0000 | [diff] [blame] | 1002 | SE->splitSingleBlock(BI); |
Jakob Stoklund Olesen | fd5c513 | 2011-04-12 19:32:53 +0000 | [diff] [blame] | 1003 | continue; |
| 1004 | } |
| 1005 | |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1006 | if (IntvIn && IntvOut) |
| 1007 | SE->splitLiveThroughBlock(Number, IntvIn, IntfIn, IntvOut, IntfOut); |
| 1008 | else if (IntvIn) |
| 1009 | SE->splitRegInBlock(BI, IntvIn, IntfIn); |
Jakob Stoklund Olesen | b4ddedc | 2011-07-15 21:47:57 +0000 | [diff] [blame] | 1010 | else |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1011 | SE->splitRegOutBlock(BI, IntvOut, IntfOut); |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1014 | // Handle live-through blocks. The relevant live-through blocks are stored in |
| 1015 | // the ActiveBlocks list with each candidate. We need to filter out |
| 1016 | // duplicates. |
| 1017 | BitVector Todo = SA->getThroughBlocks(); |
| 1018 | for (unsigned c = 0; c != UsedCands.size(); ++c) { |
| 1019 | ArrayRef<unsigned> Blocks = GlobalCand[UsedCands[c]].ActiveBlocks; |
| 1020 | for (unsigned i = 0, e = Blocks.size(); i != e; ++i) { |
| 1021 | unsigned Number = Blocks[i]; |
| 1022 | if (!Todo.test(Number)) |
| 1023 | continue; |
| 1024 | Todo.reset(Number); |
| 1025 | |
| 1026 | unsigned IntvIn = 0, IntvOut = 0; |
| 1027 | SlotIndex IntfIn, IntfOut; |
| 1028 | |
| 1029 | unsigned CandIn = BundleCand[Bundles->getBundle(Number, 0)]; |
| 1030 | if (CandIn != NoCand) { |
| 1031 | GlobalSplitCandidate &Cand = GlobalCand[CandIn]; |
| 1032 | IntvIn = Cand.IntvIdx; |
| 1033 | Cand.Intf.moveToBlock(Number); |
| 1034 | IntfIn = Cand.Intf.first(); |
| 1035 | } |
| 1036 | |
| 1037 | unsigned CandOut = BundleCand[Bundles->getBundle(Number, 1)]; |
| 1038 | if (CandOut != NoCand) { |
| 1039 | GlobalSplitCandidate &Cand = GlobalCand[CandOut]; |
| 1040 | IntvOut = Cand.IntvIdx; |
| 1041 | Cand.Intf.moveToBlock(Number); |
| 1042 | IntfOut = Cand.Intf.last(); |
| 1043 | } |
| 1044 | if (!IntvIn && !IntvOut) |
| 1045 | continue; |
| 1046 | SE->splitLiveThroughBlock(Number, IntvIn, IntfIn, IntvOut, IntfOut); |
| 1047 | } |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Jakob Stoklund Olesen | 0db841f | 2011-02-17 22:53:48 +0000 | [diff] [blame] | 1050 | ++NumGlobalSplits; |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1051 | |
Jakob Stoklund Olesen | 5928046 | 2011-04-21 18:38:15 +0000 | [diff] [blame] | 1052 | SmallVector<unsigned, 8> IntvMap; |
| 1053 | SE->finish(&IntvMap); |
Jakob Stoklund Olesen | 1f88042 | 2011-08-05 23:10:40 +0000 | [diff] [blame] | 1054 | DebugVars->splitRegister(Reg, LREdit.regs()); |
Jakob Stoklund Olesen | f42b661 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 1055 | |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 1056 | ExtraRegInfo.resize(MRI->getNumVirtRegs()); |
Jakob Stoklund Olesen | b2abfa0 | 2011-05-28 02:32:57 +0000 | [diff] [blame] | 1057 | unsigned OrigBlocks = SA->getNumLiveBlocks(); |
Jakob Stoklund Olesen | 5928046 | 2011-04-21 18:38:15 +0000 | [diff] [blame] | 1058 | |
| 1059 | // Sort out the new intervals created by splitting. We get four kinds: |
| 1060 | // - Remainder intervals should not be split again. |
| 1061 | // - Candidate intervals can be assigned to Cand.PhysReg. |
| 1062 | // - Block-local splits are candidates for local splitting. |
| 1063 | // - DCE leftovers should go back on the queue. |
| 1064 | for (unsigned i = 0, e = LREdit.size(); i != e; ++i) { |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 1065 | LiveInterval &Reg = *LREdit.get(i); |
Jakob Stoklund Olesen | 5928046 | 2011-04-21 18:38:15 +0000 | [diff] [blame] | 1066 | |
| 1067 | // Ignore old intervals from DCE. |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 1068 | if (getStage(Reg) != RS_New) |
Jakob Stoklund Olesen | 5928046 | 2011-04-21 18:38:15 +0000 | [diff] [blame] | 1069 | continue; |
| 1070 | |
| 1071 | // Remainder interval. Don't try splitting again, spill if it doesn't |
| 1072 | // allocate. |
| 1073 | if (IntvMap[i] == 0) { |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 1074 | setStage(Reg, RS_Spill); |
Jakob Stoklund Olesen | 5928046 | 2011-04-21 18:38:15 +0000 | [diff] [blame] | 1075 | continue; |
| 1076 | } |
| 1077 | |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1078 | // Global intervals. Allow repeated splitting as long as the number of live |
| 1079 | // blocks is strictly decreasing. |
| 1080 | if (IntvMap[i] < NumGlobalIntvs) { |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 1081 | if (SA->countLiveBlocks(&Reg) >= OrigBlocks) { |
Jakob Stoklund Olesen | 9f4b893 | 2011-04-26 22:33:12 +0000 | [diff] [blame] | 1082 | DEBUG(dbgs() << "Main interval covers the same " << OrigBlocks |
| 1083 | << " blocks as original.\n"); |
| 1084 | // Don't allow repeated splitting as a safe guard against looping. |
Jakob Stoklund Olesen | 49743b1 | 2011-07-25 15:25:43 +0000 | [diff] [blame] | 1085 | setStage(Reg, RS_Split2); |
Jakob Stoklund Olesen | 9f4b893 | 2011-04-26 22:33:12 +0000 | [diff] [blame] | 1086 | } |
| 1087 | continue; |
| 1088 | } |
| 1089 | |
| 1090 | // Other intervals are treated as new. This includes local intervals created |
| 1091 | // for blocks with multiple uses, and anything created by DCE. |
Jakob Stoklund Olesen | 5928046 | 2011-04-21 18:38:15 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Jakob Stoklund Olesen | eb29157 | 2011-03-27 22:49:21 +0000 | [diff] [blame] | 1094 | if (VerifyEnabled) |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1095 | MF->verify(this, "After splitting live range around region"); |
| 1096 | } |
| 1097 | |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1098 | unsigned RAGreedy::tryRegionSplit(LiveInterval &VirtReg, AllocationOrder &Order, |
| 1099 | SmallVectorImpl<LiveInterval*> &NewVRegs) { |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 1100 | unsigned NumCands = 0; |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1101 | unsigned BestCand = NoCand; |
| 1102 | float BestCost; |
| 1103 | SmallVector<unsigned, 8> UsedCands; |
| 1104 | |
| 1105 | // Check if we can split this live range around a compact region. |
Jakob Stoklund Olesen | a16a25d | 2011-09-12 16:54:42 +0000 | [diff] [blame] | 1106 | bool HasCompact = calcCompactRegion(GlobalCand.front()); |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1107 | if (HasCompact) { |
| 1108 | // Yes, keep GlobalCand[0] as the compact region candidate. |
| 1109 | NumCands = 1; |
| 1110 | BestCost = HUGE_VALF; |
| 1111 | } else { |
| 1112 | // No benefit from the compact region, our fallback will be per-block |
| 1113 | // splitting. Make sure we find a solution that is cheaper than spilling. |
| 1114 | BestCost = Hysteresis * calcSpillCost(); |
| 1115 | DEBUG(dbgs() << "Cost of isolating all blocks = " << BestCost << '\n'); |
| 1116 | } |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 1117 | |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1118 | Order.rewind(); |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 1119 | while (unsigned PhysReg = Order.next()) { |
Jakob Stoklund Olesen | f1c7098 | 2011-07-14 05:35:11 +0000 | [diff] [blame] | 1120 | // Discard bad candidates before we run out of interference cache cursors. |
| 1121 | // This will only affect register classes with a lot of registers (>32). |
| 1122 | if (NumCands == IntfCache.getMaxCursors()) { |
| 1123 | unsigned WorstCount = ~0u; |
| 1124 | unsigned Worst = 0; |
| 1125 | for (unsigned i = 0; i != NumCands; ++i) { |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1126 | if (i == BestCand || !GlobalCand[i].PhysReg) |
Jakob Stoklund Olesen | f1c7098 | 2011-07-14 05:35:11 +0000 | [diff] [blame] | 1127 | continue; |
| 1128 | unsigned Count = GlobalCand[i].LiveBundles.count(); |
| 1129 | if (Count < WorstCount) |
| 1130 | Worst = i, WorstCount = Count; |
| 1131 | } |
| 1132 | --NumCands; |
| 1133 | GlobalCand[Worst] = GlobalCand[NumCands]; |
Jakob Stoklund Olesen | 7bdf006 | 2011-11-01 00:02:31 +0000 | [diff] [blame] | 1134 | if (BestCand == NumCands) |
| 1135 | BestCand = Worst; |
Jakob Stoklund Olesen | f1c7098 | 2011-07-14 05:35:11 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 1138 | if (GlobalCand.size() <= NumCands) |
| 1139 | GlobalCand.resize(NumCands+1); |
| 1140 | GlobalSplitCandidate &Cand = GlobalCand[NumCands]; |
| 1141 | Cand.reset(IntfCache, PhysReg); |
Jakob Stoklund Olesen | 96dcd95 | 2011-03-05 01:10:31 +0000 | [diff] [blame] | 1142 | |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 1143 | SpillPlacer->prepare(Cand.LiveBundles); |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 1144 | float Cost; |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 1145 | if (!addSplitConstraints(Cand.Intf, Cost)) { |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 1146 | DEBUG(dbgs() << PrintReg(PhysReg, TRI) << "\tno positive bundles\n"); |
Jakob Stoklund Olesen | 1b400e8 | 2011-04-06 21:32:38 +0000 | [diff] [blame] | 1147 | continue; |
| 1148 | } |
Jakob Stoklund Olesen | f4afdfc | 2011-04-09 02:59:09 +0000 | [diff] [blame] | 1149 | DEBUG(dbgs() << PrintReg(PhysReg, TRI) << "\tstatic = " << Cost); |
Jakob Stoklund Olesen | 2007298 | 2011-04-22 22:47:40 +0000 | [diff] [blame] | 1150 | if (Cost >= BestCost) { |
| 1151 | DEBUG({ |
| 1152 | if (BestCand == NoCand) |
| 1153 | dbgs() << " worse than no bundles\n"; |
| 1154 | else |
| 1155 | dbgs() << " worse than " |
| 1156 | << PrintReg(GlobalCand[BestCand].PhysReg, TRI) << '\n'; |
| 1157 | }); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1158 | continue; |
Jakob Stoklund Olesen | 874be74 | 2011-03-05 03:28:51 +0000 | [diff] [blame] | 1159 | } |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 1160 | growRegion(Cand); |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1161 | |
Jakob Stoklund Olesen | 9efa2a2 | 2011-04-06 19:13:57 +0000 | [diff] [blame] | 1162 | SpillPlacer->finish(); |
| 1163 | |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1164 | // No live bundles, defer to splitSingleBlocks(). |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 1165 | if (!Cand.LiveBundles.any()) { |
Jakob Stoklund Olesen | 874be74 | 2011-03-05 03:28:51 +0000 | [diff] [blame] | 1166 | DEBUG(dbgs() << " no bundles.\n"); |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1167 | continue; |
Jakob Stoklund Olesen | 874be74 | 2011-03-05 03:28:51 +0000 | [diff] [blame] | 1168 | } |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1169 | |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 1170 | Cost += calcGlobalSplitCost(Cand); |
Jakob Stoklund Olesen | 874be74 | 2011-03-05 03:28:51 +0000 | [diff] [blame] | 1171 | DEBUG({ |
| 1172 | dbgs() << ", total = " << Cost << " with bundles"; |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 1173 | for (int i = Cand.LiveBundles.find_first(); i>=0; |
| 1174 | i = Cand.LiveBundles.find_next(i)) |
Jakob Stoklund Olesen | 874be74 | 2011-03-05 03:28:51 +0000 | [diff] [blame] | 1175 | dbgs() << " EB#" << i; |
| 1176 | dbgs() << ".\n"; |
| 1177 | }); |
Jakob Stoklund Olesen | 2007298 | 2011-04-22 22:47:40 +0000 | [diff] [blame] | 1178 | if (Cost < BestCost) { |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 1179 | BestCand = NumCands; |
Jakob Stoklund Olesen | 2007298 | 2011-04-22 22:47:40 +0000 | [diff] [blame] | 1180 | BestCost = Hysteresis * Cost; // Prevent rounding effects. |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1181 | } |
Jakob Stoklund Olesen | c66a37d | 2011-07-14 00:17:10 +0000 | [diff] [blame] | 1182 | ++NumCands; |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1183 | } |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1184 | |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1185 | // No solutions found, fall back to single block splitting. |
| 1186 | if (!HasCompact && BestCand == NoCand) |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1187 | return 0; |
| 1188 | |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1189 | // Prepare split editor. |
Jakob Stoklund Olesen | 20942dc | 2012-05-19 05:25:46 +0000 | [diff] [blame] | 1190 | LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this); |
Jakob Stoklund Olesen | 708d06f | 2011-09-12 16:49:21 +0000 | [diff] [blame] | 1191 | SE->reset(LREdit, SplitSpillMode); |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1192 | |
| 1193 | // Assign all edge bundles to the preferred candidate, or NoCand. |
| 1194 | BundleCand.assign(Bundles->getNumBundles(), NoCand); |
| 1195 | |
| 1196 | // Assign bundles for the best candidate region. |
| 1197 | if (BestCand != NoCand) { |
| 1198 | GlobalSplitCandidate &Cand = GlobalCand[BestCand]; |
| 1199 | if (unsigned B = Cand.getBundles(BundleCand, BestCand)) { |
| 1200 | UsedCands.push_back(BestCand); |
| 1201 | Cand.IntvIdx = SE->openIntv(); |
| 1202 | DEBUG(dbgs() << "Split for " << PrintReg(Cand.PhysReg, TRI) << " in " |
| 1203 | << B << " bundles, intv " << Cand.IntvIdx << ".\n"); |
Chandler Carruth | 32668ea | 2011-08-03 23:07:27 +0000 | [diff] [blame] | 1204 | (void)B; |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | // Assign bundles for the compact region. |
| 1209 | if (HasCompact) { |
| 1210 | GlobalSplitCandidate &Cand = GlobalCand.front(); |
| 1211 | assert(!Cand.PhysReg && "Compact region has no physreg"); |
| 1212 | if (unsigned B = Cand.getBundles(BundleCand, 0)) { |
| 1213 | UsedCands.push_back(0); |
| 1214 | Cand.IntvIdx = SE->openIntv(); |
| 1215 | DEBUG(dbgs() << "Split for compact region in " << B << " bundles, intv " |
| 1216 | << Cand.IntvIdx << ".\n"); |
Chandler Carruth | 32668ea | 2011-08-03 23:07:27 +0000 | [diff] [blame] | 1217 | (void)B; |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | splitAroundRegion(LREdit, UsedCands); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1222 | return 0; |
| 1223 | } |
| 1224 | |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1225 | |
| 1226 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | dab35d3 | 2011-08-05 23:04:18 +0000 | [diff] [blame] | 1227 | // Per-Block Splitting |
| 1228 | //===----------------------------------------------------------------------===// |
| 1229 | |
| 1230 | /// tryBlockSplit - Split a global live range around every block with uses. This |
| 1231 | /// creates a lot of local live ranges, that will be split by tryLocalSplit if |
| 1232 | /// they don't allocate. |
| 1233 | unsigned RAGreedy::tryBlockSplit(LiveInterval &VirtReg, AllocationOrder &Order, |
| 1234 | SmallVectorImpl<LiveInterval*> &NewVRegs) { |
| 1235 | assert(&SA->getParent() == &VirtReg && "Live range wasn't analyzed"); |
| 1236 | unsigned Reg = VirtReg.reg; |
| 1237 | bool SingleInstrs = RegClassInfo.isProperSubClass(MRI->getRegClass(Reg)); |
Jakob Stoklund Olesen | 20942dc | 2012-05-19 05:25:46 +0000 | [diff] [blame] | 1238 | LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this); |
Jakob Stoklund Olesen | 708d06f | 2011-09-12 16:49:21 +0000 | [diff] [blame] | 1239 | SE->reset(LREdit, SplitSpillMode); |
Jakob Stoklund Olesen | dab35d3 | 2011-08-05 23:04:18 +0000 | [diff] [blame] | 1240 | ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); |
| 1241 | for (unsigned i = 0; i != UseBlocks.size(); ++i) { |
| 1242 | const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; |
| 1243 | if (SA->shouldSplitSingleBlock(BI, SingleInstrs)) |
| 1244 | SE->splitSingleBlock(BI); |
| 1245 | } |
| 1246 | // No blocks were split. |
| 1247 | if (LREdit.empty()) |
| 1248 | return 0; |
| 1249 | |
| 1250 | // We did split for some blocks. |
Jakob Stoklund Olesen | a9c41d3 | 2011-08-05 23:50:31 +0000 | [diff] [blame] | 1251 | SmallVector<unsigned, 8> IntvMap; |
| 1252 | SE->finish(&IntvMap); |
Jakob Stoklund Olesen | 1f88042 | 2011-08-05 23:10:40 +0000 | [diff] [blame] | 1253 | |
| 1254 | // Tell LiveDebugVariables about the new ranges. |
| 1255 | DebugVars->splitRegister(Reg, LREdit.regs()); |
| 1256 | |
Jakob Stoklund Olesen | a9c41d3 | 2011-08-05 23:50:31 +0000 | [diff] [blame] | 1257 | ExtraRegInfo.resize(MRI->getNumVirtRegs()); |
| 1258 | |
| 1259 | // Sort out the new intervals created by splitting. The remainder interval |
| 1260 | // goes straight to spilling, the new local ranges get to stay RS_New. |
| 1261 | for (unsigned i = 0, e = LREdit.size(); i != e; ++i) { |
| 1262 | LiveInterval &LI = *LREdit.get(i); |
| 1263 | if (getStage(LI) == RS_New && IntvMap[i] == 0) |
| 1264 | setStage(LI, RS_Spill); |
| 1265 | } |
| 1266 | |
Jakob Stoklund Olesen | dab35d3 | 2011-08-05 23:04:18 +0000 | [diff] [blame] | 1267 | if (VerifyEnabled) |
| 1268 | MF->verify(this, "After splitting live range around basic blocks"); |
| 1269 | return 0; |
| 1270 | } |
| 1271 | |
Jakob Stoklund Olesen | d74d284 | 2012-05-23 22:37:27 +0000 | [diff] [blame] | 1272 | |
| 1273 | //===----------------------------------------------------------------------===// |
| 1274 | // Per-Instruction Splitting |
| 1275 | //===----------------------------------------------------------------------===// |
| 1276 | |
| 1277 | /// tryInstructionSplit - Split a live range around individual instructions. |
| 1278 | /// This is normally not worthwhile since the spiller is doing essentially the |
| 1279 | /// same thing. However, when the live range is in a constrained register |
| 1280 | /// class, it may help to insert copies such that parts of the live range can |
| 1281 | /// be moved to a larger register class. |
| 1282 | /// |
| 1283 | /// This is similar to spilling to a larger register class. |
| 1284 | unsigned |
| 1285 | RAGreedy::tryInstructionSplit(LiveInterval &VirtReg, AllocationOrder &Order, |
| 1286 | SmallVectorImpl<LiveInterval*> &NewVRegs) { |
| 1287 | // There is no point to this if there are no larger sub-classes. |
| 1288 | if (!RegClassInfo.isProperSubClass(MRI->getRegClass(VirtReg.reg))) |
| 1289 | return 0; |
| 1290 | |
| 1291 | // Always enable split spill mode, since we're effectively spilling to a |
| 1292 | // register. |
| 1293 | LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this); |
| 1294 | SE->reset(LREdit, SplitEditor::SM_Size); |
| 1295 | |
| 1296 | ArrayRef<SlotIndex> Uses = SA->getUseSlots(); |
| 1297 | if (Uses.size() <= 1) |
| 1298 | return 0; |
| 1299 | |
| 1300 | DEBUG(dbgs() << "Split around " << Uses.size() << " individual instrs.\n"); |
| 1301 | |
| 1302 | // Split around every non-copy instruction. |
| 1303 | for (unsigned i = 0; i != Uses.size(); ++i) { |
| 1304 | if (const MachineInstr *MI = Indexes->getInstructionFromIndex(Uses[i])) |
| 1305 | if (MI->isFullCopy()) { |
| 1306 | DEBUG(dbgs() << " skip:\t" << Uses[i] << '\t' << *MI); |
| 1307 | continue; |
| 1308 | } |
| 1309 | SE->openIntv(); |
| 1310 | SlotIndex SegStart = SE->enterIntvBefore(Uses[i]); |
| 1311 | SlotIndex SegStop = SE->leaveIntvAfter(Uses[i]); |
| 1312 | SE->useIntv(SegStart, SegStop); |
| 1313 | } |
| 1314 | |
| 1315 | if (LREdit.empty()) { |
| 1316 | DEBUG(dbgs() << "All uses were copies.\n"); |
| 1317 | return 0; |
| 1318 | } |
| 1319 | |
| 1320 | SmallVector<unsigned, 8> IntvMap; |
| 1321 | SE->finish(&IntvMap); |
| 1322 | DebugVars->splitRegister(VirtReg.reg, LREdit.regs()); |
| 1323 | ExtraRegInfo.resize(MRI->getNumVirtRegs()); |
| 1324 | |
| 1325 | // Assign all new registers to RS_Spill. This was the last chance. |
| 1326 | setStage(LREdit.begin(), LREdit.end(), RS_Spill); |
| 1327 | return 0; |
| 1328 | } |
| 1329 | |
| 1330 | |
Jakob Stoklund Olesen | dab35d3 | 2011-08-05 23:04:18 +0000 | [diff] [blame] | 1331 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1332 | // Local Splitting |
| 1333 | //===----------------------------------------------------------------------===// |
| 1334 | |
| 1335 | |
| 1336 | /// calcGapWeights - Compute the maximum spill weight that needs to be evicted |
| 1337 | /// in order to use PhysReg between two entries in SA->UseSlots. |
| 1338 | /// |
| 1339 | /// GapWeight[i] represents the gap between UseSlots[i] and UseSlots[i+1]. |
| 1340 | /// |
| 1341 | void RAGreedy::calcGapWeights(unsigned PhysReg, |
| 1342 | SmallVectorImpl<float> &GapWeight) { |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 1343 | assert(SA->getUseBlocks().size() == 1 && "Not a local interval"); |
| 1344 | const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front(); |
Jakob Stoklund Olesen | b20b518 | 2012-01-12 17:53:44 +0000 | [diff] [blame] | 1345 | ArrayRef<SlotIndex> Uses = SA->getUseSlots(); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1346 | const unsigned NumGaps = Uses.size()-1; |
| 1347 | |
| 1348 | // Start and end points for the interference check. |
Jakob Stoklund Olesen | fe62d92 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 1349 | SlotIndex StartIdx = |
| 1350 | BI.LiveIn ? BI.FirstInstr.getBaseIndex() : BI.FirstInstr; |
| 1351 | SlotIndex StopIdx = |
| 1352 | BI.LiveOut ? BI.LastInstr.getBoundaryIndex() : BI.LastInstr; |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1353 | |
| 1354 | GapWeight.assign(NumGaps, 0.0f); |
| 1355 | |
| 1356 | // Add interference from each overlapping register. |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 1357 | for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { |
| 1358 | if (!Matrix->query(const_cast<LiveInterval&>(SA->getParent()), *Units) |
| 1359 | .checkInterference()) |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1360 | continue; |
| 1361 | |
Jakob Stoklund Olesen | fe62d92 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 1362 | // We know that VirtReg is a continuous interval from FirstInstr to |
| 1363 | // LastInstr, so we don't need InterferenceQuery. |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1364 | // |
| 1365 | // Interference that overlaps an instruction is counted in both gaps |
| 1366 | // surrounding the instruction. The exception is interference before |
| 1367 | // StartIdx and after StopIdx. |
| 1368 | // |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 1369 | LiveIntervalUnion::SegmentIter IntI = |
| 1370 | Matrix->getLiveUnions()[*Units] .find(StartIdx); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1371 | for (unsigned Gap = 0; IntI.valid() && IntI.start() < StopIdx; ++IntI) { |
| 1372 | // Skip the gaps before IntI. |
| 1373 | while (Uses[Gap+1].getBoundaryIndex() < IntI.start()) |
| 1374 | if (++Gap == NumGaps) |
| 1375 | break; |
| 1376 | if (Gap == NumGaps) |
| 1377 | break; |
| 1378 | |
| 1379 | // Update the gaps covered by IntI. |
| 1380 | const float weight = IntI.value()->weight; |
| 1381 | for (; Gap != NumGaps; ++Gap) { |
| 1382 | GapWeight[Gap] = std::max(GapWeight[Gap], weight); |
| 1383 | if (Uses[Gap+1].getBaseIndex() >= IntI.stop()) |
| 1384 | break; |
| 1385 | } |
| 1386 | if (Gap == NumGaps) |
| 1387 | break; |
| 1388 | } |
| 1389 | } |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 1390 | |
| 1391 | // Add fixed interference. |
| 1392 | for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { |
| 1393 | const LiveInterval &LI = LIS->getRegUnit(*Units); |
| 1394 | LiveInterval::const_iterator I = LI.find(StartIdx); |
| 1395 | LiveInterval::const_iterator E = LI.end(); |
| 1396 | |
| 1397 | // Same loop as above. Mark any overlapped gaps as HUGE_VALF. |
| 1398 | for (unsigned Gap = 0; I != E && I->start < StopIdx; ++I) { |
| 1399 | while (Uses[Gap+1].getBoundaryIndex() < I->start) |
| 1400 | if (++Gap == NumGaps) |
| 1401 | break; |
| 1402 | if (Gap == NumGaps) |
| 1403 | break; |
| 1404 | |
| 1405 | for (; Gap != NumGaps; ++Gap) { |
| 1406 | GapWeight[Gap] = HUGE_VALF; |
| 1407 | if (Uses[Gap+1].getBaseIndex() >= I->end) |
| 1408 | break; |
| 1409 | } |
| 1410 | if (Gap == NumGaps) |
| 1411 | break; |
| 1412 | } |
| 1413 | } |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1416 | /// tryLocalSplit - Try to split VirtReg into smaller intervals inside its only |
| 1417 | /// basic block. |
| 1418 | /// |
| 1419 | unsigned RAGreedy::tryLocalSplit(LiveInterval &VirtReg, AllocationOrder &Order, |
| 1420 | SmallVectorImpl<LiveInterval*> &NewVRegs) { |
Jakob Stoklund Olesen | db529a8 | 2011-04-06 03:57:00 +0000 | [diff] [blame] | 1421 | assert(SA->getUseBlocks().size() == 1 && "Not a local interval"); |
| 1422 | const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front(); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1423 | |
| 1424 | // Note that it is possible to have an interval that is live-in or live-out |
| 1425 | // while only covering a single block - A phi-def can use undef values from |
| 1426 | // predecessors, and the block could be a single-block loop. |
| 1427 | // We don't bother doing anything clever about such a case, we simply assume |
Jakob Stoklund Olesen | fe62d92 | 2011-08-02 22:54:14 +0000 | [diff] [blame] | 1428 | // that the interval is continuous from FirstInstr to LastInstr. We should |
| 1429 | // make sure that we don't do anything illegal to such an interval, though. |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1430 | |
Jakob Stoklund Olesen | b20b518 | 2012-01-12 17:53:44 +0000 | [diff] [blame] | 1431 | ArrayRef<SlotIndex> Uses = SA->getUseSlots(); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1432 | if (Uses.size() <= 2) |
| 1433 | return 0; |
| 1434 | const unsigned NumGaps = Uses.size()-1; |
| 1435 | |
| 1436 | DEBUG({ |
| 1437 | dbgs() << "tryLocalSplit: "; |
| 1438 | for (unsigned i = 0, e = Uses.size(); i != e; ++i) |
Jakob Stoklund Olesen | b20b518 | 2012-01-12 17:53:44 +0000 | [diff] [blame] | 1439 | dbgs() << ' ' << Uses[i]; |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1440 | dbgs() << '\n'; |
| 1441 | }); |
| 1442 | |
Jakob Stoklund Olesen | a6d513f | 2012-02-11 00:42:18 +0000 | [diff] [blame] | 1443 | // If VirtReg is live across any register mask operands, compute a list of |
| 1444 | // gaps with register masks. |
| 1445 | SmallVector<unsigned, 8> RegMaskGaps; |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 1446 | if (Matrix->checkRegMaskInterference(VirtReg)) { |
Jakob Stoklund Olesen | a6d513f | 2012-02-11 00:42:18 +0000 | [diff] [blame] | 1447 | // Get regmask slots for the whole block. |
| 1448 | ArrayRef<SlotIndex> RMS = LIS->getRegMaskSlotsInBlock(BI.MBB->getNumber()); |
Jakob Stoklund Olesen | cac5fa3 | 2012-02-14 23:51:27 +0000 | [diff] [blame] | 1449 | DEBUG(dbgs() << RMS.size() << " regmasks in block:"); |
Jakob Stoklund Olesen | a6d513f | 2012-02-11 00:42:18 +0000 | [diff] [blame] | 1450 | // Constrain to VirtReg's live range. |
Jakob Stoklund Olesen | cac5fa3 | 2012-02-14 23:51:27 +0000 | [diff] [blame] | 1451 | unsigned ri = std::lower_bound(RMS.begin(), RMS.end(), |
| 1452 | Uses.front().getRegSlot()) - RMS.begin(); |
Jakob Stoklund Olesen | a6d513f | 2012-02-11 00:42:18 +0000 | [diff] [blame] | 1453 | unsigned re = RMS.size(); |
| 1454 | for (unsigned i = 0; i != NumGaps && ri != re; ++i) { |
Jakob Stoklund Olesen | cac5fa3 | 2012-02-14 23:51:27 +0000 | [diff] [blame] | 1455 | // Look for Uses[i] <= RMS <= Uses[i+1]. |
| 1456 | assert(!SlotIndex::isEarlierInstr(RMS[ri], Uses[i])); |
| 1457 | if (SlotIndex::isEarlierInstr(Uses[i+1], RMS[ri])) |
Jakob Stoklund Olesen | a6d513f | 2012-02-11 00:42:18 +0000 | [diff] [blame] | 1458 | continue; |
Jakob Stoklund Olesen | cac5fa3 | 2012-02-14 23:51:27 +0000 | [diff] [blame] | 1459 | // Skip a regmask on the same instruction as the last use. It doesn't |
| 1460 | // overlap the live range. |
| 1461 | if (SlotIndex::isSameInstr(Uses[i+1], RMS[ri]) && i+1 == NumGaps) |
| 1462 | break; |
| 1463 | DEBUG(dbgs() << ' ' << RMS[ri] << ':' << Uses[i] << '-' << Uses[i+1]); |
Jakob Stoklund Olesen | a6d513f | 2012-02-11 00:42:18 +0000 | [diff] [blame] | 1464 | RegMaskGaps.push_back(i); |
Jakob Stoklund Olesen | cac5fa3 | 2012-02-14 23:51:27 +0000 | [diff] [blame] | 1465 | // Advance ri to the next gap. A regmask on one of the uses counts in |
| 1466 | // both gaps. |
| 1467 | while (ri != re && SlotIndex::isEarlierInstr(RMS[ri], Uses[i+1])) |
| 1468 | ++ri; |
Jakob Stoklund Olesen | a6d513f | 2012-02-11 00:42:18 +0000 | [diff] [blame] | 1469 | } |
Jakob Stoklund Olesen | cac5fa3 | 2012-02-14 23:51:27 +0000 | [diff] [blame] | 1470 | DEBUG(dbgs() << '\n'); |
Jakob Stoklund Olesen | a6d513f | 2012-02-11 00:42:18 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 1473 | // Since we allow local split results to be split again, there is a risk of |
| 1474 | // creating infinite loops. It is tempting to require that the new live |
| 1475 | // ranges have less instructions than the original. That would guarantee |
| 1476 | // convergence, but it is too strict. A live range with 3 instructions can be |
| 1477 | // split 2+3 (including the COPY), and we want to allow that. |
| 1478 | // |
| 1479 | // Instead we use these rules: |
| 1480 | // |
Jakob Stoklund Olesen | 49743b1 | 2011-07-25 15:25:43 +0000 | [diff] [blame] | 1481 | // 1. Allow any split for ranges with getStage() < RS_Split2. (Except for the |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 1482 | // noop split, of course). |
Jakob Stoklund Olesen | 49743b1 | 2011-07-25 15:25:43 +0000 | [diff] [blame] | 1483 | // 2. Require progress be made for ranges with getStage() == RS_Split2. All |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 1484 | // the new ranges must have fewer instructions than before the split. |
Jakob Stoklund Olesen | 49743b1 | 2011-07-25 15:25:43 +0000 | [diff] [blame] | 1485 | // 3. New ranges with the same number of instructions are marked RS_Split2, |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 1486 | // smaller ranges are marked RS_New. |
| 1487 | // |
| 1488 | // These rules allow a 3 -> 2+3 split once, which we need. They also prevent |
| 1489 | // excessive splitting and infinite loops. |
| 1490 | // |
Jakob Stoklund Olesen | 49743b1 | 2011-07-25 15:25:43 +0000 | [diff] [blame] | 1491 | bool ProgressRequired = getStage(VirtReg) >= RS_Split2; |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1492 | |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 1493 | // Best split candidate. |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1494 | unsigned BestBefore = NumGaps; |
| 1495 | unsigned BestAfter = 0; |
| 1496 | float BestDiff = 0; |
| 1497 | |
Jakob Stoklund Olesen | 40a42a2 | 2011-03-04 00:58:40 +0000 | [diff] [blame] | 1498 | const float blockFreq = SpillPlacer->getBlockFrequency(BI.MBB->getNumber()); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1499 | SmallVector<float, 8> GapWeight; |
| 1500 | |
| 1501 | Order.rewind(); |
| 1502 | while (unsigned PhysReg = Order.next()) { |
| 1503 | // Keep track of the largest spill weight that would need to be evicted in |
| 1504 | // order to make use of PhysReg between UseSlots[i] and UseSlots[i+1]. |
| 1505 | calcGapWeights(PhysReg, GapWeight); |
| 1506 | |
Jakob Stoklund Olesen | a6d513f | 2012-02-11 00:42:18 +0000 | [diff] [blame] | 1507 | // Remove any gaps with regmask clobbers. |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 1508 | if (Matrix->checkRegMaskInterference(VirtReg, PhysReg)) |
Jakob Stoklund Olesen | a6d513f | 2012-02-11 00:42:18 +0000 | [diff] [blame] | 1509 | for (unsigned i = 0, e = RegMaskGaps.size(); i != e; ++i) |
| 1510 | GapWeight[RegMaskGaps[i]] = HUGE_VALF; |
| 1511 | |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1512 | // Try to find the best sequence of gaps to close. |
| 1513 | // The new spill weight must be larger than any gap interference. |
| 1514 | |
| 1515 | // We will split before Uses[SplitBefore] and after Uses[SplitAfter]. |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 1516 | unsigned SplitBefore = 0, SplitAfter = 1; |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1517 | |
| 1518 | // MaxGap should always be max(GapWeight[SplitBefore..SplitAfter-1]). |
| 1519 | // It is the spill weight that needs to be evicted. |
| 1520 | float MaxGap = GapWeight[0]; |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1521 | |
| 1522 | for (;;) { |
| 1523 | // Live before/after split? |
| 1524 | const bool LiveBefore = SplitBefore != 0 || BI.LiveIn; |
| 1525 | const bool LiveAfter = SplitAfter != NumGaps || BI.LiveOut; |
| 1526 | |
| 1527 | DEBUG(dbgs() << PrintReg(PhysReg, TRI) << ' ' |
| 1528 | << Uses[SplitBefore] << '-' << Uses[SplitAfter] |
| 1529 | << " i=" << MaxGap); |
| 1530 | |
| 1531 | // Stop before the interval gets so big we wouldn't be making progress. |
| 1532 | if (!LiveBefore && !LiveAfter) { |
| 1533 | DEBUG(dbgs() << " all\n"); |
| 1534 | break; |
| 1535 | } |
| 1536 | // Should the interval be extended or shrunk? |
| 1537 | bool Shrink = true; |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1538 | |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 1539 | // How many gaps would the new range have? |
| 1540 | unsigned NewGaps = LiveBefore + SplitAfter - SplitBefore + LiveAfter; |
| 1541 | |
| 1542 | // Legally, without causing looping? |
| 1543 | bool Legal = !ProgressRequired || NewGaps < NumGaps; |
| 1544 | |
| 1545 | if (Legal && MaxGap < HUGE_VALF) { |
| 1546 | // Estimate the new spill weight. Each instruction reads or writes the |
| 1547 | // register. Conservatively assume there are no read-modify-write |
| 1548 | // instructions. |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1549 | // |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 1550 | // Try to guess the size of the new interval. |
| 1551 | const float EstWeight = normalizeSpillWeight(blockFreq * (NewGaps + 1), |
| 1552 | Uses[SplitBefore].distance(Uses[SplitAfter]) + |
| 1553 | (LiveBefore + LiveAfter)*SlotIndex::InstrDist); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1554 | // Would this split be possible to allocate? |
| 1555 | // Never allocate all gaps, we wouldn't be making progress. |
Jakob Stoklund Olesen | 66446c8 | 2011-04-30 05:07:46 +0000 | [diff] [blame] | 1556 | DEBUG(dbgs() << " w=" << EstWeight); |
| 1557 | if (EstWeight * Hysteresis >= MaxGap) { |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1558 | Shrink = false; |
Jakob Stoklund Olesen | 66446c8 | 2011-04-30 05:07:46 +0000 | [diff] [blame] | 1559 | float Diff = EstWeight - MaxGap; |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1560 | if (Diff > BestDiff) { |
| 1561 | DEBUG(dbgs() << " (best)"); |
Jakob Stoklund Olesen | 66446c8 | 2011-04-30 05:07:46 +0000 | [diff] [blame] | 1562 | BestDiff = Hysteresis * Diff; |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1563 | BestBefore = SplitBefore; |
| 1564 | BestAfter = SplitAfter; |
| 1565 | } |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | // Try to shrink. |
| 1570 | if (Shrink) { |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 1571 | if (++SplitBefore < SplitAfter) { |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1572 | DEBUG(dbgs() << " shrink\n"); |
| 1573 | // Recompute the max when necessary. |
| 1574 | if (GapWeight[SplitBefore - 1] >= MaxGap) { |
| 1575 | MaxGap = GapWeight[SplitBefore]; |
| 1576 | for (unsigned i = SplitBefore + 1; i != SplitAfter; ++i) |
| 1577 | MaxGap = std::max(MaxGap, GapWeight[i]); |
| 1578 | } |
| 1579 | continue; |
| 1580 | } |
| 1581 | MaxGap = 0; |
| 1582 | } |
| 1583 | |
| 1584 | // Try to extend the interval. |
| 1585 | if (SplitAfter >= NumGaps) { |
| 1586 | DEBUG(dbgs() << " end\n"); |
| 1587 | break; |
| 1588 | } |
| 1589 | |
| 1590 | DEBUG(dbgs() << " extend\n"); |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 1591 | MaxGap = std::max(MaxGap, GapWeight[SplitAfter++]); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | // Didn't find any candidates? |
| 1596 | if (BestBefore == NumGaps) |
| 1597 | return 0; |
| 1598 | |
| 1599 | DEBUG(dbgs() << "Best local split range: " << Uses[BestBefore] |
| 1600 | << '-' << Uses[BestAfter] << ", " << BestDiff |
| 1601 | << ", " << (BestAfter - BestBefore + 1) << " instrs\n"); |
| 1602 | |
Jakob Stoklund Olesen | 20942dc | 2012-05-19 05:25:46 +0000 | [diff] [blame] | 1603 | LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this); |
Jakob Stoklund Olesen | bece06f | 2011-03-03 01:29:13 +0000 | [diff] [blame] | 1604 | SE->reset(LREdit); |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1605 | |
Jakob Stoklund Olesen | bece06f | 2011-03-03 01:29:13 +0000 | [diff] [blame] | 1606 | SE->openIntv(); |
| 1607 | SlotIndex SegStart = SE->enterIntvBefore(Uses[BestBefore]); |
| 1608 | SlotIndex SegStop = SE->leaveIntvAfter(Uses[BestAfter]); |
| 1609 | SE->useIntv(SegStart, SegStop); |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 1610 | SmallVector<unsigned, 8> IntvMap; |
| 1611 | SE->finish(&IntvMap); |
Jakob Stoklund Olesen | f42b661 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 1612 | DebugVars->splitRegister(VirtReg.reg, LREdit.regs()); |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 1613 | |
| 1614 | // If the new range has the same number of instructions as before, mark it as |
Jakob Stoklund Olesen | 49743b1 | 2011-07-25 15:25:43 +0000 | [diff] [blame] | 1615 | // RS_Split2 so the next split will be forced to make progress. Otherwise, |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 1616 | // leave the new intervals as RS_New so they can compete. |
| 1617 | bool LiveBefore = BestBefore != 0 || BI.LiveIn; |
| 1618 | bool LiveAfter = BestAfter != NumGaps || BI.LiveOut; |
| 1619 | unsigned NewGaps = LiveBefore + BestAfter - BestBefore + LiveAfter; |
| 1620 | if (NewGaps >= NumGaps) { |
| 1621 | DEBUG(dbgs() << "Tagging non-progress ranges: "); |
| 1622 | assert(!ProgressRequired && "Didn't make progress when it was required."); |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 1623 | for (unsigned i = 0, e = IntvMap.size(); i != e; ++i) |
| 1624 | if (IntvMap[i] == 1) { |
Jakob Stoklund Olesen | 49743b1 | 2011-07-25 15:25:43 +0000 | [diff] [blame] | 1625 | setStage(*LREdit.get(i), RS_Split2); |
Jakob Stoklund Olesen | b3e705f | 2011-06-06 23:55:20 +0000 | [diff] [blame] | 1626 | DEBUG(dbgs() << PrintReg(LREdit.get(i)->reg)); |
| 1627 | } |
| 1628 | DEBUG(dbgs() << '\n'); |
| 1629 | } |
Jakob Stoklund Olesen | 0db841f | 2011-02-17 22:53:48 +0000 | [diff] [blame] | 1630 | ++NumLocalSplits; |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1631 | |
| 1632 | return 0; |
| 1633 | } |
| 1634 | |
| 1635 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1636 | // Live Range Splitting |
| 1637 | //===----------------------------------------------------------------------===// |
| 1638 | |
| 1639 | /// trySplit - Try to split VirtReg or one of its interferences, making it |
| 1640 | /// assignable. |
| 1641 | /// @return Physreg when VirtReg may be assigned and/or new NewVRegs. |
| 1642 | unsigned RAGreedy::trySplit(LiveInterval &VirtReg, AllocationOrder &Order, |
| 1643 | SmallVectorImpl<LiveInterval*>&NewVRegs) { |
Jakob Stoklund Olesen | ccfa446 | 2011-08-05 23:50:33 +0000 | [diff] [blame] | 1644 | // Ranges must be Split2 or less. |
| 1645 | if (getStage(VirtReg) >= RS_Spill) |
| 1646 | return 0; |
| 1647 | |
Jakob Stoklund Olesen | 034a80d | 2011-02-17 19:13:53 +0000 | [diff] [blame] | 1648 | // Local intervals are handled separately. |
Jakob Stoklund Olesen | a2ebf60 | 2011-02-19 00:38:40 +0000 | [diff] [blame] | 1649 | if (LIS->intervalIsInOneMBB(VirtReg)) { |
| 1650 | NamedRegionTimer T("Local Splitting", TimerGroupName, TimePassesIsEnabled); |
Jakob Stoklund Olesen | 22a1df6 | 2011-03-01 21:10:07 +0000 | [diff] [blame] | 1651 | SA->analyze(&VirtReg); |
Jakob Stoklund Olesen | d74d284 | 2012-05-23 22:37:27 +0000 | [diff] [blame] | 1652 | unsigned PhysReg = tryLocalSplit(VirtReg, Order, NewVRegs); |
| 1653 | if (PhysReg || !NewVRegs.empty()) |
| 1654 | return PhysReg; |
| 1655 | return tryInstructionSplit(VirtReg, Order, NewVRegs); |
Jakob Stoklund Olesen | a2ebf60 | 2011-02-19 00:38:40 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | NamedRegionTimer T("Global Splitting", TimerGroupName, TimePassesIsEnabled); |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1659 | |
Jakob Stoklund Olesen | 22a1df6 | 2011-03-01 21:10:07 +0000 | [diff] [blame] | 1660 | SA->analyze(&VirtReg); |
| 1661 | |
Jakob Stoklund Olesen | 7d6b6a0 | 2011-05-03 20:42:13 +0000 | [diff] [blame] | 1662 | // FIXME: SplitAnalysis may repair broken live ranges coming from the |
| 1663 | // coalescer. That may cause the range to become allocatable which means that |
| 1664 | // tryRegionSplit won't be making progress. This check should be replaced with |
| 1665 | // an assertion when the coalescer is fixed. |
| 1666 | if (SA->didRepairRange()) { |
| 1667 | // VirtReg has changed, so all cached queries are invalid. |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 1668 | Matrix->invalidateVirtRegs(); |
Jakob Stoklund Olesen | 7d6b6a0 | 2011-05-03 20:42:13 +0000 | [diff] [blame] | 1669 | if (unsigned PhysReg = tryAssign(VirtReg, Order, NewVRegs)) |
| 1670 | return PhysReg; |
| 1671 | } |
| 1672 | |
Jakob Stoklund Olesen | 49743b1 | 2011-07-25 15:25:43 +0000 | [diff] [blame] | 1673 | // First try to split around a region spanning multiple blocks. RS_Split2 |
| 1674 | // ranges already made dubious progress with region splitting, so they go |
| 1675 | // straight to single block splitting. |
| 1676 | if (getStage(VirtReg) < RS_Split2) { |
| 1677 | unsigned PhysReg = tryRegionSplit(VirtReg, Order, NewVRegs); |
| 1678 | if (PhysReg || !NewVRegs.empty()) |
| 1679 | return PhysReg; |
| 1680 | } |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1681 | |
Jakob Stoklund Olesen | dab35d3 | 2011-08-05 23:04:18 +0000 | [diff] [blame] | 1682 | // Then isolate blocks. |
| 1683 | return tryBlockSplit(VirtReg, Order, NewVRegs); |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1687 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | 770d42d | 2010-12-22 22:01:30 +0000 | [diff] [blame] | 1688 | // Main Entry Point |
| 1689 | //===----------------------------------------------------------------------===// |
| 1690 | |
| 1691 | unsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg, |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1692 | SmallVectorImpl<LiveInterval*> &NewVRegs) { |
Jakob Stoklund Olesen | 770d42d | 2010-12-22 22:01:30 +0000 | [diff] [blame] | 1693 | // First try assigning a free register. |
Jakob Stoklund Olesen | 5f2316a | 2011-06-03 20:34:53 +0000 | [diff] [blame] | 1694 | AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo); |
Jakob Stoklund Olesen | 6bfba2e | 2011-04-20 18:19:48 +0000 | [diff] [blame] | 1695 | if (unsigned PhysReg = tryAssign(VirtReg, Order, NewVRegs)) |
| 1696 | return PhysReg; |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 1697 | |
Jakob Stoklund Olesen | b8d936b | 2011-05-25 23:58:36 +0000 | [diff] [blame] | 1698 | LiveRangeStage Stage = getStage(VirtReg); |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 1699 | DEBUG(dbgs() << StageName[Stage] |
| 1700 | << " Cascade " << ExtraRegInfo[VirtReg.reg].Cascade << '\n'); |
Jakob Stoklund Olesen | b8d936b | 2011-05-25 23:58:36 +0000 | [diff] [blame] | 1701 | |
Jakob Stoklund Olesen | 76395c9 | 2011-06-01 18:45:02 +0000 | [diff] [blame] | 1702 | // Try to evict a less worthy live range, but only for ranges from the primary |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 1703 | // queue. The RS_Split ranges already failed to do this, and they should not |
Jakob Stoklund Olesen | 76395c9 | 2011-06-01 18:45:02 +0000 | [diff] [blame] | 1704 | // get a second chance until they have been split. |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 1705 | if (Stage != RS_Split) |
Jakob Stoklund Olesen | 76395c9 | 2011-06-01 18:45:02 +0000 | [diff] [blame] | 1706 | if (unsigned PhysReg = tryEvict(VirtReg, Order, NewVRegs)) |
| 1707 | return PhysReg; |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 1708 | |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1709 | assert(NewVRegs.empty() && "Cannot append to existing NewVRegs"); |
| 1710 | |
Jakob Stoklund Olesen | 107d366 | 2011-02-24 23:21:36 +0000 | [diff] [blame] | 1711 | // The first time we see a live range, don't try to split or spill. |
| 1712 | // Wait until the second time, when all smaller ranges have been allocated. |
| 1713 | // This gives a better picture of the interference to split around. |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 1714 | if (Stage < RS_Split) { |
| 1715 | setStage(VirtReg, RS_Split); |
Jakob Stoklund Olesen | c1655e1 | 2011-03-19 23:02:47 +0000 | [diff] [blame] | 1716 | DEBUG(dbgs() << "wait for second round\n"); |
Jakob Stoklund Olesen | 107d366 | 2011-02-24 23:21:36 +0000 | [diff] [blame] | 1717 | NewVRegs.push_back(&VirtReg); |
| 1718 | return 0; |
| 1719 | } |
| 1720 | |
Jakob Stoklund Olesen | bf4e10f | 2011-05-06 21:58:30 +0000 | [diff] [blame] | 1721 | // If we couldn't allocate a register from spilling, there is probably some |
| 1722 | // invalid inline assembly. The base class wil report it. |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 1723 | if (Stage >= RS_Done || !VirtReg.isSpillable()) |
Jakob Stoklund Olesen | bf4e10f | 2011-05-06 21:58:30 +0000 | [diff] [blame] | 1724 | return ~0u; |
Jakob Stoklund Olesen | 22a1df6 | 2011-03-01 21:10:07 +0000 | [diff] [blame] | 1725 | |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 1726 | // Try splitting VirtReg or interferences. |
Jakob Stoklund Olesen | ccdb3fc | 2011-01-19 22:11:48 +0000 | [diff] [blame] | 1727 | unsigned PhysReg = trySplit(VirtReg, Order, NewVRegs); |
| 1728 | if (PhysReg || !NewVRegs.empty()) |
Jakob Stoklund Olesen | b64d92e | 2010-12-14 00:37:44 +0000 | [diff] [blame] | 1729 | return PhysReg; |
| 1730 | |
Jakob Stoklund Olesen | 770d42d | 2010-12-22 22:01:30 +0000 | [diff] [blame] | 1731 | // Finally spill VirtReg itself. |
Jakob Stoklund Olesen | 533f58e | 2010-12-11 00:19:56 +0000 | [diff] [blame] | 1732 | NamedRegionTimer T("Spiller", TimerGroupName, TimePassesIsEnabled); |
Jakob Stoklund Olesen | 20942dc | 2012-05-19 05:25:46 +0000 | [diff] [blame] | 1733 | LiveRangeEdit LRE(&VirtReg, NewVRegs, *MF, *LIS, VRM, this); |
Jakob Stoklund Olesen | 47dbf6c | 2011-03-10 01:51:42 +0000 | [diff] [blame] | 1734 | spiller().spill(LRE); |
Jakob Stoklund Olesen | fa89a03 | 2011-07-25 15:25:41 +0000 | [diff] [blame] | 1735 | setStage(NewVRegs.begin(), NewVRegs.end(), RS_Done); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 1736 | |
Jakob Stoklund Olesen | c46570d | 2011-03-16 22:56:08 +0000 | [diff] [blame] | 1737 | if (VerifyEnabled) |
| 1738 | MF->verify(this, "After spilling"); |
| 1739 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 1740 | // The live virtual register requesting allocation was spilled, so tell |
| 1741 | // the caller not to allocate anything during this round. |
| 1742 | return 0; |
| 1743 | } |
| 1744 | |
| 1745 | bool RAGreedy::runOnMachineFunction(MachineFunction &mf) { |
| 1746 | DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n" |
David Blaikie | 986d76d | 2012-08-22 17:18:53 +0000 | [diff] [blame] | 1747 | << "********** Function: " << mf.getName() << '\n'); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 1748 | |
| 1749 | MF = &mf; |
Jakob Stoklund Olesen | af24964 | 2010-12-17 23:16:35 +0000 | [diff] [blame] | 1750 | if (VerifyEnabled) |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 1751 | MF->verify(this, "Before greedy register allocator"); |
Jakob Stoklund Olesen | af24964 | 2010-12-17 23:16:35 +0000 | [diff] [blame] | 1752 | |
Jakob Stoklund Olesen | d4348a2 | 2012-06-20 22:52:29 +0000 | [diff] [blame] | 1753 | RegAllocBase::init(getAnalysis<VirtRegMap>(), |
| 1754 | getAnalysis<LiveIntervals>(), |
| 1755 | getAnalysis<LiveRegMatrix>()); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1756 | Indexes = &getAnalysis<SlotIndexes>(); |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 1757 | DomTree = &getAnalysis<MachineDominatorTree>(); |
Jakob Stoklund Olesen | f6dff84 | 2010-12-10 22:54:44 +0000 | [diff] [blame] | 1758 | SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM)); |
Jakob Stoklund Olesen | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 1759 | Loops = &getAnalysis<MachineLoopInfo>(); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1760 | Bundles = &getAnalysis<EdgeBundles>(); |
| 1761 | SpillPlacer = &getAnalysis<SpillPlacement>(); |
Jakob Stoklund Olesen | f42b661 | 2011-05-06 18:00:02 +0000 | [diff] [blame] | 1762 | DebugVars = &getAnalysis<LiveDebugVariables>(); |
Jakob Stoklund Olesen | b5fa933 | 2011-01-18 21:13:27 +0000 | [diff] [blame] | 1763 | |
Jakob Stoklund Olesen | 1b847de | 2011-02-19 00:53:42 +0000 | [diff] [blame] | 1764 | SA.reset(new SplitAnalysis(*VRM, *LIS, *Loops)); |
Jakob Stoklund Olesen | bece06f | 2011-03-03 01:29:13 +0000 | [diff] [blame] | 1765 | SE.reset(new SplitEditor(*SA, *LIS, *VRM, *DomTree)); |
Jakob Stoklund Olesen | 1a98800 | 2011-07-02 01:37:09 +0000 | [diff] [blame] | 1766 | ExtraRegInfo.clear(); |
| 1767 | ExtraRegInfo.resize(MRI->getNumVirtRegs()); |
| 1768 | NextCascade = 1; |
Jakob Stoklund Olesen | 042888d | 2012-06-20 22:52:26 +0000 | [diff] [blame] | 1769 | IntfCache.init(MF, Matrix->getLiveUnions(), Indexes, LIS, TRI); |
Jakob Stoklund Olesen | 0000578 | 2011-07-26 23:41:46 +0000 | [diff] [blame] | 1770 | GlobalCand.resize(32); // This will grow as needed. |
Jakob Stoklund Olesen | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 1771 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 1772 | allocatePhysRegs(); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 1773 | releaseMemory(); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 1774 | return true; |
| 1775 | } |