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