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