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