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