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