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