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