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