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 | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 21 | #include "SplitKit.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 22 | #include "VirtRegMap.h" |
| 23 | #include "VirtRegRewriter.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/AliasAnalysis.h" |
| 25 | #include "llvm/Function.h" |
| 26 | #include "llvm/PassAnalysisSupport.h" |
| 27 | #include "llvm/CodeGen/CalcSpillWeights.h" |
| 28 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
| 29 | #include "llvm/CodeGen/LiveStackAnalysis.h" |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineDominators.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Jakob Stoklund Olesen | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineLoopRanges.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 35 | #include "llvm/CodeGen/Passes.h" |
| 36 | #include "llvm/CodeGen/RegAllocRegistry.h" |
| 37 | #include "llvm/CodeGen/RegisterCoalescer.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetOptions.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Debug.h" |
| 40 | #include "llvm/Support/ErrorHandling.h" |
| 41 | #include "llvm/Support/raw_ostream.h" |
Jakob Stoklund Olesen | 533f58e | 2010-12-11 00:19:56 +0000 | [diff] [blame] | 42 | #include "llvm/Support/Timer.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 43 | |
| 44 | using namespace llvm; |
| 45 | |
| 46 | static RegisterRegAlloc greedyRegAlloc("greedy", "greedy register allocator", |
| 47 | createGreedyRegisterAllocator); |
| 48 | |
| 49 | namespace { |
| 50 | class RAGreedy : public MachineFunctionPass, public RegAllocBase { |
| 51 | // context |
| 52 | MachineFunction *MF; |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 53 | BitVector ReservedRegs; |
| 54 | |
| 55 | // analyses |
| 56 | LiveStacks *LS; |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 57 | MachineDominatorTree *DomTree; |
Jakob Stoklund Olesen | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 58 | MachineLoopInfo *Loops; |
| 59 | MachineLoopRanges *LoopRanges; |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 60 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 61 | // state |
| 62 | std::auto_ptr<Spiller> SpillerInstance; |
Jakob Stoklund Olesen | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 63 | std::auto_ptr<SplitAnalysis> SA; |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 64 | |
| 65 | public: |
| 66 | RAGreedy(); |
| 67 | |
| 68 | /// Return the pass name. |
| 69 | virtual const char* getPassName() const { |
Jakob Stoklund Olesen | 533f58e | 2010-12-11 00:19:56 +0000 | [diff] [blame] | 70 | return "Greedy Register Allocator"; |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /// RAGreedy analysis usage. |
| 74 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 75 | |
| 76 | virtual void releaseMemory(); |
| 77 | |
| 78 | virtual Spiller &spiller() { return *SpillerInstance; } |
| 79 | |
Jakob Stoklund Olesen | 90c1d7d | 2010-12-08 22:57:16 +0000 | [diff] [blame] | 80 | virtual float getPriority(LiveInterval *LI); |
Jakob Stoklund Olesen | d0bec3e | 2010-12-08 22:22:41 +0000 | [diff] [blame] | 81 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 82 | virtual unsigned selectOrSplit(LiveInterval &VirtReg, |
| 83 | SmallVectorImpl<LiveInterval*> &SplitVRegs); |
| 84 | |
| 85 | /// Perform register allocation. |
| 86 | virtual bool runOnMachineFunction(MachineFunction &mf); |
| 87 | |
| 88 | static char ID; |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 89 | |
| 90 | private: |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 91 | bool checkUncachedInterference(LiveInterval&, unsigned); |
| 92 | LiveInterval *getSingleInterference(LiveInterval&, unsigned); |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 93 | bool reassignVReg(LiveInterval &InterferingVReg, unsigned OldPhysReg); |
| 94 | bool reassignInterferences(LiveInterval &VirtReg, unsigned PhysReg); |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 95 | unsigned findInterferenceFreeReg(MachineLoopRange*, |
| 96 | LiveInterval&, AllocationOrder&); |
Jakob Stoklund Olesen | 770d42d | 2010-12-22 22:01:30 +0000 | [diff] [blame] | 97 | float calcInterferenceWeight(LiveInterval&, unsigned); |
Jakob Stoklund Olesen | b64d92e | 2010-12-14 00:37:44 +0000 | [diff] [blame] | 98 | |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 99 | unsigned tryReassign(LiveInterval&, AllocationOrder&); |
Jakob Stoklund Olesen | b64d92e | 2010-12-14 00:37:44 +0000 | [diff] [blame] | 100 | unsigned trySplit(LiveInterval&, AllocationOrder&, |
| 101 | SmallVectorImpl<LiveInterval*>&); |
Jakob Stoklund Olesen | 770d42d | 2010-12-22 22:01:30 +0000 | [diff] [blame] | 102 | unsigned trySpillInterferences(LiveInterval&, AllocationOrder&, |
| 103 | SmallVectorImpl<LiveInterval*>&); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 104 | }; |
| 105 | } // end anonymous namespace |
| 106 | |
| 107 | char RAGreedy::ID = 0; |
| 108 | |
| 109 | FunctionPass* llvm::createGreedyRegisterAllocator() { |
| 110 | return new RAGreedy(); |
| 111 | } |
| 112 | |
| 113 | RAGreedy::RAGreedy(): MachineFunctionPass(ID) { |
| 114 | initializeLiveIntervalsPass(*PassRegistry::getPassRegistry()); |
| 115 | initializeSlotIndexesPass(*PassRegistry::getPassRegistry()); |
| 116 | initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry()); |
| 117 | initializeRegisterCoalescerAnalysisGroup(*PassRegistry::getPassRegistry()); |
| 118 | initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry()); |
| 119 | initializeLiveStacksPass(*PassRegistry::getPassRegistry()); |
| 120 | initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry()); |
| 121 | initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry()); |
Jakob Stoklund Olesen | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 122 | initializeMachineLoopRangesPass(*PassRegistry::getPassRegistry()); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 123 | initializeVirtRegMapPass(*PassRegistry::getPassRegistry()); |
| 124 | } |
| 125 | |
| 126 | void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const { |
| 127 | AU.setPreservesCFG(); |
| 128 | AU.addRequired<AliasAnalysis>(); |
| 129 | AU.addPreserved<AliasAnalysis>(); |
| 130 | AU.addRequired<LiveIntervals>(); |
| 131 | AU.addPreserved<SlotIndexes>(); |
| 132 | if (StrongPHIElim) |
| 133 | AU.addRequiredID(StrongPHIEliminationID); |
| 134 | AU.addRequiredTransitive<RegisterCoalescer>(); |
| 135 | AU.addRequired<CalculateSpillWeights>(); |
| 136 | AU.addRequired<LiveStacks>(); |
| 137 | AU.addPreserved<LiveStacks>(); |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 138 | AU.addRequired<MachineDominatorTree>(); |
| 139 | AU.addPreserved<MachineDominatorTree>(); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 140 | AU.addRequired<MachineLoopInfo>(); |
| 141 | AU.addPreserved<MachineLoopInfo>(); |
Jakob Stoklund Olesen | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 142 | AU.addRequired<MachineLoopRanges>(); |
| 143 | AU.addPreserved<MachineLoopRanges>(); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 144 | AU.addRequired<VirtRegMap>(); |
| 145 | AU.addPreserved<VirtRegMap>(); |
| 146 | MachineFunctionPass::getAnalysisUsage(AU); |
| 147 | } |
| 148 | |
| 149 | void RAGreedy::releaseMemory() { |
| 150 | SpillerInstance.reset(0); |
| 151 | RegAllocBase::releaseMemory(); |
| 152 | } |
| 153 | |
Jakob Stoklund Olesen | 90c1d7d | 2010-12-08 22:57:16 +0000 | [diff] [blame] | 154 | float RAGreedy::getPriority(LiveInterval *LI) { |
| 155 | float Priority = LI->weight; |
| 156 | |
| 157 | // Prioritize hinted registers so they are allocated first. |
| 158 | std::pair<unsigned, unsigned> Hint; |
| 159 | if (Hint.first || Hint.second) { |
| 160 | // The hint can be target specific, a virtual register, or a physreg. |
| 161 | Priority *= 2; |
| 162 | |
| 163 | // Prefer physreg hints above anything else. |
| 164 | if (Hint.first == 0 && TargetRegisterInfo::isPhysicalRegister(Hint.second)) |
| 165 | Priority *= 2; |
| 166 | } |
| 167 | return Priority; |
| 168 | } |
| 169 | |
Jakob Stoklund Olesen | 770d42d | 2010-12-22 22:01:30 +0000 | [diff] [blame] | 170 | |
| 171 | //===----------------------------------------------------------------------===// |
| 172 | // Register Reassignment |
| 173 | //===----------------------------------------------------------------------===// |
| 174 | |
Jakob Stoklund Olesen | 6ce219e | 2010-12-10 20:45:04 +0000 | [diff] [blame] | 175 | // Check interference without using the cache. |
| 176 | bool RAGreedy::checkUncachedInterference(LiveInterval &VirtReg, |
| 177 | unsigned PhysReg) { |
Jakob Stoklund Olesen | 257c556 | 2010-12-14 23:38:19 +0000 | [diff] [blame] | 178 | for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) { |
| 179 | LiveIntervalUnion::Query subQ(&VirtReg, &PhysReg2LiveUnion[*AliasI]); |
Jakob Stoklund Olesen | 6ce219e | 2010-12-10 20:45:04 +0000 | [diff] [blame] | 180 | if (subQ.checkInterference()) |
| 181 | return true; |
| 182 | } |
| 183 | return false; |
| 184 | } |
| 185 | |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 186 | /// getSingleInterference - Return the single interfering virtual register |
| 187 | /// assigned to PhysReg. Return 0 if more than one virtual register is |
| 188 | /// interfering. |
| 189 | LiveInterval *RAGreedy::getSingleInterference(LiveInterval &VirtReg, |
| 190 | unsigned PhysReg) { |
Jakob Stoklund Olesen | 257c556 | 2010-12-14 23:38:19 +0000 | [diff] [blame] | 191 | // Check physreg and aliases. |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 192 | LiveInterval *Interference = 0; |
Jakob Stoklund Olesen | 257c556 | 2010-12-14 23:38:19 +0000 | [diff] [blame] | 193 | for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) { |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 194 | LiveIntervalUnion::Query &Q = query(VirtReg, *AliasI); |
| 195 | if (Q.checkInterference()) { |
Jakob Stoklund Olesen | d84de8c | 2010-12-14 17:47:36 +0000 | [diff] [blame] | 196 | if (Interference) |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 197 | return 0; |
| 198 | Q.collectInterferingVRegs(1); |
Jakob Stoklund Olesen | d84de8c | 2010-12-14 17:47:36 +0000 | [diff] [blame] | 199 | if (!Q.seenAllInterferences()) |
| 200 | return 0; |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 201 | Interference = Q.interferingVRegs().front(); |
| 202 | } |
| 203 | } |
| 204 | return Interference; |
| 205 | } |
| 206 | |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 207 | // Attempt to reassign this virtual register to a different physical register. |
| 208 | // |
| 209 | // FIXME: we are not yet caching these "second-level" interferences discovered |
| 210 | // in the sub-queries. These interferences can change with each call to |
| 211 | // selectOrSplit. However, we could implement a "may-interfere" cache that |
| 212 | // could be conservatively dirtied when we reassign or split. |
| 213 | // |
| 214 | // FIXME: This may result in a lot of alias queries. We could summarize alias |
| 215 | // live intervals in their parent register's live union, but it's messy. |
| 216 | bool RAGreedy::reassignVReg(LiveInterval &InterferingVReg, |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 217 | unsigned WantedPhysReg) { |
| 218 | assert(TargetRegisterInfo::isVirtualRegister(InterferingVReg.reg) && |
| 219 | "Can only reassign virtual registers"); |
| 220 | assert(TRI->regsOverlap(WantedPhysReg, VRM->getPhys(InterferingVReg.reg)) && |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 221 | "inconsistent phys reg assigment"); |
| 222 | |
Jakob Stoklund Olesen | dd479e9 | 2010-12-10 22:21:05 +0000 | [diff] [blame] | 223 | AllocationOrder Order(InterferingVReg.reg, *VRM, ReservedRegs); |
| 224 | while (unsigned PhysReg = Order.next()) { |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 225 | // Don't reassign to a WantedPhysReg alias. |
| 226 | if (TRI->regsOverlap(PhysReg, WantedPhysReg)) |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 227 | continue; |
| 228 | |
Jakob Stoklund Olesen | 6ce219e | 2010-12-10 20:45:04 +0000 | [diff] [blame] | 229 | if (checkUncachedInterference(InterferingVReg, PhysReg)) |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 230 | continue; |
| 231 | |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 232 | // Reassign the interfering virtual reg to this physical reg. |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 233 | unsigned OldAssign = VRM->getPhys(InterferingVReg.reg); |
| 234 | DEBUG(dbgs() << "reassigning: " << InterferingVReg << " from " << |
| 235 | TRI->getName(OldAssign) << " to " << TRI->getName(PhysReg) << '\n'); |
| 236 | PhysReg2LiveUnion[OldAssign].extract(InterferingVReg); |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 237 | VRM->clearVirt(InterferingVReg.reg); |
| 238 | VRM->assignVirt2Phys(InterferingVReg.reg, PhysReg); |
| 239 | PhysReg2LiveUnion[PhysReg].unify(InterferingVReg); |
| 240 | |
| 241 | return true; |
| 242 | } |
| 243 | return false; |
| 244 | } |
| 245 | |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 246 | /// reassignInterferences - Reassign all interferences to different physical |
| 247 | /// registers such that Virtreg can be assigned to PhysReg. |
| 248 | /// Currently this only works with a single interference. |
| 249 | /// @param VirtReg Currently unassigned virtual register. |
| 250 | /// @param PhysReg Physical register to be cleared. |
| 251 | /// @return True on success, false if nothing was changed. |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 252 | bool RAGreedy::reassignInterferences(LiveInterval &VirtReg, unsigned PhysReg) { |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 253 | LiveInterval *InterferingVReg = getSingleInterference(VirtReg, PhysReg); |
| 254 | if (!InterferingVReg) |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 255 | return false; |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 256 | if (TargetRegisterInfo::isPhysicalRegister(InterferingVReg->reg)) |
| 257 | return false; |
| 258 | return reassignVReg(*InterferingVReg, PhysReg); |
| 259 | } |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 260 | |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 261 | /// tryReassign - Try to reassign interferences to different physregs. |
| 262 | /// @param VirtReg Currently unassigned virtual register. |
| 263 | /// @param Order Physregs to try. |
| 264 | /// @return Physreg to assign VirtReg, or 0. |
| 265 | unsigned RAGreedy::tryReassign(LiveInterval &VirtReg, AllocationOrder &Order) { |
| 266 | NamedRegionTimer T("Reassign", TimerGroupName, TimePassesIsEnabled); |
| 267 | Order.rewind(); |
| 268 | while (unsigned PhysReg = Order.next()) |
| 269 | if (reassignInterferences(VirtReg, PhysReg)) |
| 270 | return PhysReg; |
| 271 | return 0; |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Jakob Stoklund Olesen | 770d42d | 2010-12-22 22:01:30 +0000 | [diff] [blame] | 274 | |
| 275 | //===----------------------------------------------------------------------===// |
| 276 | // Loop Splitting |
| 277 | //===----------------------------------------------------------------------===// |
| 278 | |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 279 | /// findInterferenceFreeReg - Find a physical register in Order where Loop has |
| 280 | /// no interferences with VirtReg. |
| 281 | unsigned RAGreedy::findInterferenceFreeReg(MachineLoopRange *Loop, |
| 282 | LiveInterval &VirtReg, |
| 283 | AllocationOrder &Order) { |
| 284 | Order.rewind(); |
| 285 | while (unsigned PhysReg = Order.next()) { |
| 286 | bool interference = false; |
| 287 | for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) { |
| 288 | if (query(VirtReg, *AI).checkLoopInterference(Loop)) { |
| 289 | interference = true; |
| 290 | break; |
| 291 | } |
| 292 | } |
| 293 | if (!interference) |
| 294 | return PhysReg; |
| 295 | } |
| 296 | // No physreg found. |
| 297 | return 0; |
| 298 | } |
| 299 | |
Jakob Stoklund Olesen | b64d92e | 2010-12-14 00:37:44 +0000 | [diff] [blame] | 300 | /// trySplit - Try to split VirtReg or one of its interferences, making it |
| 301 | /// assignable. |
| 302 | /// @return Physreg when VirtReg may be assigned and/or new SplitVRegs. |
| 303 | unsigned RAGreedy::trySplit(LiveInterval &VirtReg, AllocationOrder &Order, |
| 304 | SmallVectorImpl<LiveInterval*>&SplitVRegs) { |
| 305 | NamedRegionTimer T("Splitter", TimerGroupName, TimePassesIsEnabled); |
Jakob Stoklund Olesen | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 306 | SA->analyze(&VirtReg); |
| 307 | |
| 308 | // Get the set of loops that have VirtReg uses and are splittable. |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 309 | SplitAnalysis::LoopPtrSet SplitLoopSet; |
| 310 | SA->getSplitLoops(SplitLoopSet); |
Jakob Stoklund Olesen | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 311 | |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 312 | // Order loops by descending area. |
| 313 | SmallVector<MachineLoopRange*, 8> SplitLoops; |
| 314 | for (SplitAnalysis::LoopPtrSet::const_iterator I = SplitLoopSet.begin(), |
| 315 | E = SplitLoopSet.end(); I != E; ++I) |
| 316 | SplitLoops.push_back(LoopRanges->getLoopRange(*I)); |
| 317 | array_pod_sort(SplitLoops.begin(), SplitLoops.end(), |
| 318 | MachineLoopRange::byAreaDesc); |
| 319 | |
| 320 | // Find the first loop that is interference-free for some register in the |
| 321 | // allocation order. |
| 322 | MachineLoopRange *Loop = 0; |
| 323 | for (unsigned i = 0, e = SplitLoops.size(); i != e; ++i) { |
Jakob Stoklund Olesen | 87c6d25 | 2010-12-18 03:04:11 +0000 | [diff] [blame] | 324 | DEBUG(dbgs() << " Checking " << *SplitLoops[i]); |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 325 | if (unsigned PhysReg = findInterferenceFreeReg(SplitLoops[i], |
| 326 | VirtReg, Order)) { |
Nick Lewycky | bb1744e | 2010-12-18 01:05:55 +0000 | [diff] [blame] | 327 | (void)PhysReg; |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 328 | Loop = SplitLoops[i]; |
Jakob Stoklund Olesen | 87c6d25 | 2010-12-18 03:04:11 +0000 | [diff] [blame] | 329 | DEBUG(dbgs() << ": Use %" << TRI->getName(PhysReg) << '\n'); |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 330 | break; |
Jakob Stoklund Olesen | 87c6d25 | 2010-12-18 03:04:11 +0000 | [diff] [blame] | 331 | } else { |
| 332 | DEBUG(dbgs() << ": Interference.\n"); |
Matt Beaumont-Gay | 3ef9f3d | 2010-12-14 21:14:55 +0000 | [diff] [blame] | 333 | } |
Jakob Stoklund Olesen | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 334 | } |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 335 | |
| 336 | if (!Loop) { |
| 337 | DEBUG(dbgs() << " All candidate loops have interference.\n"); |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | // Execute the split around Loop. |
| 342 | SmallVector<LiveInterval*, 4> SpillRegs; |
| 343 | LiveRangeEdit LREdit(VirtReg, SplitVRegs, SpillRegs); |
| 344 | SplitEditor(*SA, *LIS, *VRM, *DomTree, LREdit) |
| 345 | .splitAroundLoop(Loop->getLoop()); |
| 346 | |
Jakob Stoklund Olesen | af24964 | 2010-12-17 23:16:35 +0000 | [diff] [blame] | 347 | if (VerifyEnabled) |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 348 | MF->verify(this, "After splitting live range around loop"); |
Jakob Stoklund Olesen | af24964 | 2010-12-17 23:16:35 +0000 | [diff] [blame] | 349 | |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 350 | // We have new split regs, don't assign anything. |
Jakob Stoklund Olesen | b64d92e | 2010-12-14 00:37:44 +0000 | [diff] [blame] | 351 | return 0; |
| 352 | } |
| 353 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 354 | |
Jakob Stoklund Olesen | 770d42d | 2010-12-22 22:01:30 +0000 | [diff] [blame] | 355 | //===----------------------------------------------------------------------===// |
| 356 | // Spilling |
| 357 | //===----------------------------------------------------------------------===// |
| 358 | |
| 359 | /// calcInterferenceWeight - Calculate the combined spill weight of |
| 360 | /// interferences when assigning VirtReg to PhysReg. |
| 361 | float RAGreedy::calcInterferenceWeight(LiveInterval &VirtReg, unsigned PhysReg){ |
| 362 | float Sum = 0; |
| 363 | for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) { |
| 364 | LiveIntervalUnion::Query &Q = query(VirtReg, *AI); |
| 365 | Q.collectInterferingVRegs(); |
| 366 | if (Q.seenUnspillableVReg()) |
| 367 | return HUGE_VALF; |
| 368 | for (unsigned i = 0, e = Q.interferingVRegs().size(); i != e; ++i) |
| 369 | Sum += Q.interferingVRegs()[i]->weight; |
| 370 | } |
| 371 | return Sum; |
| 372 | } |
| 373 | |
| 374 | /// trySpillInterferences - Try to spill interfering registers instead of the |
| 375 | /// current one. Only do it if the accumulated spill weight is smaller than the |
| 376 | /// current spill weight. |
| 377 | unsigned RAGreedy::trySpillInterferences(LiveInterval &VirtReg, |
| 378 | AllocationOrder &Order, |
| 379 | SmallVectorImpl<LiveInterval*> &NewVRegs) { |
| 380 | NamedRegionTimer T("Spill Interference", TimerGroupName, TimePassesIsEnabled); |
| 381 | unsigned BestPhys = 0; |
Duncan Sands | 2aea490 | 2010-12-28 10:07:15 +0000 | [diff] [blame] | 382 | float BestWeight = 0; |
Jakob Stoklund Olesen | 770d42d | 2010-12-22 22:01:30 +0000 | [diff] [blame] | 383 | |
| 384 | Order.rewind(); |
| 385 | while (unsigned PhysReg = Order.next()) { |
| 386 | float Weight = calcInterferenceWeight(VirtReg, PhysReg); |
| 387 | if (Weight == HUGE_VALF || Weight >= VirtReg.weight) |
| 388 | continue; |
| 389 | if (!BestPhys || Weight < BestWeight) |
| 390 | BestPhys = PhysReg, BestWeight = Weight; |
| 391 | } |
| 392 | |
| 393 | // No candidates found. |
| 394 | if (!BestPhys) |
| 395 | return 0; |
| 396 | |
| 397 | // Collect all interfering registers. |
| 398 | SmallVector<LiveInterval*, 8> Spills; |
| 399 | for (const unsigned *AI = TRI->getOverlaps(BestPhys); *AI; ++AI) { |
| 400 | LiveIntervalUnion::Query &Q = query(VirtReg, *AI); |
| 401 | Spills.append(Q.interferingVRegs().begin(), Q.interferingVRegs().end()); |
| 402 | for (unsigned i = 0, e = Q.interferingVRegs().size(); i != e; ++i) { |
| 403 | LiveInterval *VReg = Q.interferingVRegs()[i]; |
| 404 | PhysReg2LiveUnion[*AI].extract(*VReg); |
| 405 | VRM->clearVirt(VReg->reg); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | // Spill them all. |
| 410 | DEBUG(dbgs() << "spilling " << Spills.size() << " interferences with weight " |
| 411 | << BestWeight << '\n'); |
| 412 | for (unsigned i = 0, e = Spills.size(); i != e; ++i) |
| 413 | spiller().spill(Spills[i], NewVRegs, Spills); |
| 414 | return BestPhys; |
| 415 | } |
| 416 | |
| 417 | |
| 418 | //===----------------------------------------------------------------------===// |
| 419 | // Main Entry Point |
| 420 | //===----------------------------------------------------------------------===// |
| 421 | |
| 422 | unsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg, |
| 423 | SmallVectorImpl<LiveInterval*> &SplitVRegs) { |
| 424 | // First try assigning a free register. |
Jakob Stoklund Olesen | dd479e9 | 2010-12-10 22:21:05 +0000 | [diff] [blame] | 425 | AllocationOrder Order(VirtReg.reg, *VRM, ReservedRegs); |
| 426 | while (unsigned PhysReg = Order.next()) { |
Jakob Stoklund Olesen | 770d42d | 2010-12-22 22:01:30 +0000 | [diff] [blame] | 427 | if (!checkPhysRegInterference(VirtReg, PhysReg)) |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 428 | return PhysReg; |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 429 | } |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 430 | |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 431 | // Try to reassign interferences. |
| 432 | if (unsigned PhysReg = tryReassign(VirtReg, Order)) |
| 433 | return PhysReg; |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 434 | |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 435 | // Try splitting VirtReg or interferences. |
Jakob Stoklund Olesen | b64d92e | 2010-12-14 00:37:44 +0000 | [diff] [blame] | 436 | unsigned PhysReg = trySplit(VirtReg, Order, SplitVRegs); |
| 437 | if (PhysReg || !SplitVRegs.empty()) |
| 438 | return PhysReg; |
| 439 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 440 | // Try to spill another interfering reg with less spill weight. |
Jakob Stoklund Olesen | 770d42d | 2010-12-22 22:01:30 +0000 | [diff] [blame] | 441 | PhysReg = trySpillInterferences(VirtReg, Order, SplitVRegs); |
| 442 | if (PhysReg) |
| 443 | return PhysReg; |
| 444 | |
| 445 | // Finally spill VirtReg itself. |
Jakob Stoklund Olesen | 533f58e | 2010-12-11 00:19:56 +0000 | [diff] [blame] | 446 | NamedRegionTimer T("Spiller", TimerGroupName, TimePassesIsEnabled); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 447 | SmallVector<LiveInterval*, 1> pendingSpills; |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 448 | spiller().spill(&VirtReg, SplitVRegs, pendingSpills); |
| 449 | |
| 450 | // The live virtual register requesting allocation was spilled, so tell |
| 451 | // the caller not to allocate anything during this round. |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | bool RAGreedy::runOnMachineFunction(MachineFunction &mf) { |
| 456 | DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n" |
| 457 | << "********** Function: " |
| 458 | << ((Value*)mf.getFunction())->getName() << '\n'); |
| 459 | |
| 460 | MF = &mf; |
Jakob Stoklund Olesen | af24964 | 2010-12-17 23:16:35 +0000 | [diff] [blame] | 461 | if (VerifyEnabled) |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 462 | MF->verify(this, "Before greedy register allocator"); |
Jakob Stoklund Olesen | af24964 | 2010-12-17 23:16:35 +0000 | [diff] [blame] | 463 | |
Jakob Stoklund Olesen | 4680dec | 2010-12-10 23:49:00 +0000 | [diff] [blame] | 464 | RegAllocBase::init(getAnalysis<VirtRegMap>(), getAnalysis<LiveIntervals>()); |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 465 | DomTree = &getAnalysis<MachineDominatorTree>(); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 466 | ReservedRegs = TRI->getReservedRegs(*MF); |
Jakob Stoklund Olesen | f6dff84 | 2010-12-10 22:54:44 +0000 | [diff] [blame] | 467 | SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM)); |
Jakob Stoklund Olesen | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 468 | Loops = &getAnalysis<MachineLoopInfo>(); |
| 469 | LoopRanges = &getAnalysis<MachineLoopRanges>(); |
| 470 | SA.reset(new SplitAnalysis(*MF, *LIS, *Loops)); |
| 471 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 472 | allocatePhysRegs(); |
| 473 | addMBBLiveIns(MF); |
| 474 | |
| 475 | // Run rewriter |
Jakob Stoklund Olesen | 533f58e | 2010-12-11 00:19:56 +0000 | [diff] [blame] | 476 | { |
| 477 | NamedRegionTimer T("Rewriter", TimerGroupName, TimePassesIsEnabled); |
| 478 | std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter()); |
| 479 | rewriter->runOnMachineFunction(*MF, *VRM, LIS); |
| 480 | } |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 481 | |
| 482 | // The pass output is in VirtRegMap. Release all the transient data. |
| 483 | releaseMemory(); |
| 484 | |
| 485 | return true; |
| 486 | } |