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