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