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