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