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