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