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