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 | b64d92e | 2010-12-14 00:37:44 +0000 | [diff] [blame] | 97 | |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 98 | unsigned tryReassign(LiveInterval&, AllocationOrder&); |
Jakob Stoklund Olesen | b64d92e | 2010-12-14 00:37:44 +0000 | [diff] [blame] | 99 | unsigned trySplit(LiveInterval&, AllocationOrder&, |
| 100 | SmallVectorImpl<LiveInterval*>&); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 101 | }; |
| 102 | } // end anonymous namespace |
| 103 | |
| 104 | char RAGreedy::ID = 0; |
| 105 | |
| 106 | FunctionPass* llvm::createGreedyRegisterAllocator() { |
| 107 | return new RAGreedy(); |
| 108 | } |
| 109 | |
| 110 | RAGreedy::RAGreedy(): MachineFunctionPass(ID) { |
| 111 | initializeLiveIntervalsPass(*PassRegistry::getPassRegistry()); |
| 112 | initializeSlotIndexesPass(*PassRegistry::getPassRegistry()); |
| 113 | initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry()); |
| 114 | initializeRegisterCoalescerAnalysisGroup(*PassRegistry::getPassRegistry()); |
| 115 | initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry()); |
| 116 | initializeLiveStacksPass(*PassRegistry::getPassRegistry()); |
| 117 | initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry()); |
| 118 | initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry()); |
Jakob Stoklund Olesen | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 119 | initializeMachineLoopRangesPass(*PassRegistry::getPassRegistry()); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 120 | initializeVirtRegMapPass(*PassRegistry::getPassRegistry()); |
| 121 | } |
| 122 | |
| 123 | void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const { |
| 124 | AU.setPreservesCFG(); |
| 125 | AU.addRequired<AliasAnalysis>(); |
| 126 | AU.addPreserved<AliasAnalysis>(); |
| 127 | AU.addRequired<LiveIntervals>(); |
| 128 | AU.addPreserved<SlotIndexes>(); |
| 129 | if (StrongPHIElim) |
| 130 | AU.addRequiredID(StrongPHIEliminationID); |
| 131 | AU.addRequiredTransitive<RegisterCoalescer>(); |
| 132 | AU.addRequired<CalculateSpillWeights>(); |
| 133 | AU.addRequired<LiveStacks>(); |
| 134 | AU.addPreserved<LiveStacks>(); |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 135 | AU.addRequired<MachineDominatorTree>(); |
| 136 | AU.addPreserved<MachineDominatorTree>(); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 137 | AU.addRequired<MachineLoopInfo>(); |
| 138 | AU.addPreserved<MachineLoopInfo>(); |
Jakob Stoklund Olesen | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 139 | AU.addRequired<MachineLoopRanges>(); |
| 140 | AU.addPreserved<MachineLoopRanges>(); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 141 | AU.addRequired<VirtRegMap>(); |
| 142 | AU.addPreserved<VirtRegMap>(); |
| 143 | MachineFunctionPass::getAnalysisUsage(AU); |
| 144 | } |
| 145 | |
| 146 | void RAGreedy::releaseMemory() { |
| 147 | SpillerInstance.reset(0); |
| 148 | RegAllocBase::releaseMemory(); |
| 149 | } |
| 150 | |
Jakob Stoklund Olesen | 90c1d7d | 2010-12-08 22:57:16 +0000 | [diff] [blame] | 151 | float RAGreedy::getPriority(LiveInterval *LI) { |
| 152 | float Priority = LI->weight; |
| 153 | |
| 154 | // Prioritize hinted registers so they are allocated first. |
| 155 | std::pair<unsigned, unsigned> Hint; |
| 156 | if (Hint.first || Hint.second) { |
| 157 | // The hint can be target specific, a virtual register, or a physreg. |
| 158 | Priority *= 2; |
| 159 | |
| 160 | // Prefer physreg hints above anything else. |
| 161 | if (Hint.first == 0 && TargetRegisterInfo::isPhysicalRegister(Hint.second)) |
| 162 | Priority *= 2; |
| 163 | } |
| 164 | return Priority; |
| 165 | } |
| 166 | |
Jakob Stoklund Olesen | 6ce219e | 2010-12-10 20:45:04 +0000 | [diff] [blame] | 167 | // Check interference without using the cache. |
| 168 | bool RAGreedy::checkUncachedInterference(LiveInterval &VirtReg, |
| 169 | unsigned PhysReg) { |
Jakob Stoklund Olesen | 257c556 | 2010-12-14 23:38:19 +0000 | [diff] [blame] | 170 | for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) { |
| 171 | LiveIntervalUnion::Query subQ(&VirtReg, &PhysReg2LiveUnion[*AliasI]); |
Jakob Stoklund Olesen | 6ce219e | 2010-12-10 20:45:04 +0000 | [diff] [blame] | 172 | if (subQ.checkInterference()) |
| 173 | return true; |
| 174 | } |
| 175 | return false; |
| 176 | } |
| 177 | |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 178 | /// getSingleInterference - Return the single interfering virtual register |
| 179 | /// assigned to PhysReg. Return 0 if more than one virtual register is |
| 180 | /// interfering. |
| 181 | LiveInterval *RAGreedy::getSingleInterference(LiveInterval &VirtReg, |
| 182 | unsigned PhysReg) { |
Jakob Stoklund Olesen | 257c556 | 2010-12-14 23:38:19 +0000 | [diff] [blame] | 183 | // Check physreg and aliases. |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 184 | LiveInterval *Interference = 0; |
Jakob Stoklund Olesen | 257c556 | 2010-12-14 23:38:19 +0000 | [diff] [blame] | 185 | for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) { |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 186 | LiveIntervalUnion::Query &Q = query(VirtReg, *AliasI); |
| 187 | if (Q.checkInterference()) { |
Jakob Stoklund Olesen | d84de8c | 2010-12-14 17:47:36 +0000 | [diff] [blame] | 188 | if (Interference) |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 189 | return 0; |
| 190 | Q.collectInterferingVRegs(1); |
Jakob Stoklund Olesen | d84de8c | 2010-12-14 17:47:36 +0000 | [diff] [blame] | 191 | if (!Q.seenAllInterferences()) |
| 192 | return 0; |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 193 | Interference = Q.interferingVRegs().front(); |
| 194 | } |
| 195 | } |
| 196 | return Interference; |
| 197 | } |
| 198 | |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 199 | // Attempt to reassign this virtual register to a different physical register. |
| 200 | // |
| 201 | // FIXME: we are not yet caching these "second-level" interferences discovered |
| 202 | // in the sub-queries. These interferences can change with each call to |
| 203 | // selectOrSplit. However, we could implement a "may-interfere" cache that |
| 204 | // could be conservatively dirtied when we reassign or split. |
| 205 | // |
| 206 | // FIXME: This may result in a lot of alias queries. We could summarize alias |
| 207 | // live intervals in their parent register's live union, but it's messy. |
| 208 | bool RAGreedy::reassignVReg(LiveInterval &InterferingVReg, |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 209 | unsigned WantedPhysReg) { |
| 210 | assert(TargetRegisterInfo::isVirtualRegister(InterferingVReg.reg) && |
| 211 | "Can only reassign virtual registers"); |
| 212 | assert(TRI->regsOverlap(WantedPhysReg, VRM->getPhys(InterferingVReg.reg)) && |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 213 | "inconsistent phys reg assigment"); |
| 214 | |
Jakob Stoklund Olesen | dd479e9 | 2010-12-10 22:21:05 +0000 | [diff] [blame] | 215 | AllocationOrder Order(InterferingVReg.reg, *VRM, ReservedRegs); |
| 216 | while (unsigned PhysReg = Order.next()) { |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 217 | // Don't reassign to a WantedPhysReg alias. |
| 218 | if (TRI->regsOverlap(PhysReg, WantedPhysReg)) |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 219 | continue; |
| 220 | |
Jakob Stoklund Olesen | 6ce219e | 2010-12-10 20:45:04 +0000 | [diff] [blame] | 221 | if (checkUncachedInterference(InterferingVReg, PhysReg)) |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 222 | continue; |
| 223 | |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 224 | // Reassign the interfering virtual reg to this physical reg. |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 225 | unsigned OldAssign = VRM->getPhys(InterferingVReg.reg); |
| 226 | DEBUG(dbgs() << "reassigning: " << InterferingVReg << " from " << |
| 227 | TRI->getName(OldAssign) << " to " << TRI->getName(PhysReg) << '\n'); |
| 228 | PhysReg2LiveUnion[OldAssign].extract(InterferingVReg); |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 229 | VRM->clearVirt(InterferingVReg.reg); |
| 230 | VRM->assignVirt2Phys(InterferingVReg.reg, PhysReg); |
| 231 | PhysReg2LiveUnion[PhysReg].unify(InterferingVReg); |
| 232 | |
| 233 | return true; |
| 234 | } |
| 235 | return false; |
| 236 | } |
| 237 | |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 238 | /// reassignInterferences - Reassign all interferences to different physical |
| 239 | /// registers such that Virtreg can be assigned to PhysReg. |
| 240 | /// Currently this only works with a single interference. |
| 241 | /// @param VirtReg Currently unassigned virtual register. |
| 242 | /// @param PhysReg Physical register to be cleared. |
| 243 | /// @return True on success, false if nothing was changed. |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 244 | bool RAGreedy::reassignInterferences(LiveInterval &VirtReg, unsigned PhysReg) { |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 245 | LiveInterval *InterferingVReg = getSingleInterference(VirtReg, PhysReg); |
| 246 | if (!InterferingVReg) |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 247 | return false; |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 248 | if (TargetRegisterInfo::isPhysicalRegister(InterferingVReg->reg)) |
| 249 | return false; |
| 250 | return reassignVReg(*InterferingVReg, PhysReg); |
| 251 | } |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 252 | |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 253 | /// tryReassign - Try to reassign interferences to different physregs. |
| 254 | /// @param VirtReg Currently unassigned virtual register. |
| 255 | /// @param Order Physregs to try. |
| 256 | /// @return Physreg to assign VirtReg, or 0. |
| 257 | unsigned RAGreedy::tryReassign(LiveInterval &VirtReg, AllocationOrder &Order) { |
| 258 | NamedRegionTimer T("Reassign", TimerGroupName, TimePassesIsEnabled); |
| 259 | Order.rewind(); |
| 260 | while (unsigned PhysReg = Order.next()) |
| 261 | if (reassignInterferences(VirtReg, PhysReg)) |
| 262 | return PhysReg; |
| 263 | return 0; |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 266 | /// findInterferenceFreeReg - Find a physical register in Order where Loop has |
| 267 | /// no interferences with VirtReg. |
| 268 | unsigned RAGreedy::findInterferenceFreeReg(MachineLoopRange *Loop, |
| 269 | LiveInterval &VirtReg, |
| 270 | AllocationOrder &Order) { |
| 271 | Order.rewind(); |
| 272 | while (unsigned PhysReg = Order.next()) { |
| 273 | bool interference = false; |
| 274 | for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) { |
| 275 | if (query(VirtReg, *AI).checkLoopInterference(Loop)) { |
| 276 | interference = true; |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | if (!interference) |
| 281 | return PhysReg; |
| 282 | } |
| 283 | // No physreg found. |
| 284 | return 0; |
| 285 | } |
| 286 | |
Jakob Stoklund Olesen | b64d92e | 2010-12-14 00:37:44 +0000 | [diff] [blame] | 287 | /// trySplit - Try to split VirtReg or one of its interferences, making it |
| 288 | /// assignable. |
| 289 | /// @return Physreg when VirtReg may be assigned and/or new SplitVRegs. |
| 290 | unsigned RAGreedy::trySplit(LiveInterval &VirtReg, AllocationOrder &Order, |
| 291 | SmallVectorImpl<LiveInterval*>&SplitVRegs) { |
| 292 | NamedRegionTimer T("Splitter", TimerGroupName, TimePassesIsEnabled); |
Jakob Stoklund Olesen | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 293 | SA->analyze(&VirtReg); |
| 294 | |
| 295 | // 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] | 296 | SplitAnalysis::LoopPtrSet SplitLoopSet; |
| 297 | SA->getSplitLoops(SplitLoopSet); |
Jakob Stoklund Olesen | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 298 | |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 299 | // Order loops by descending area. |
| 300 | SmallVector<MachineLoopRange*, 8> SplitLoops; |
| 301 | for (SplitAnalysis::LoopPtrSet::const_iterator I = SplitLoopSet.begin(), |
| 302 | E = SplitLoopSet.end(); I != E; ++I) |
| 303 | SplitLoops.push_back(LoopRanges->getLoopRange(*I)); |
| 304 | array_pod_sort(SplitLoops.begin(), SplitLoops.end(), |
| 305 | MachineLoopRange::byAreaDesc); |
| 306 | |
| 307 | // Find the first loop that is interference-free for some register in the |
| 308 | // allocation order. |
| 309 | MachineLoopRange *Loop = 0; |
| 310 | for (unsigned i = 0, e = SplitLoops.size(); i != e; ++i) { |
| 311 | if (unsigned PhysReg = findInterferenceFreeReg(SplitLoops[i], |
| 312 | VirtReg, Order)) { |
| 313 | Loop = SplitLoops[i]; |
| 314 | DEBUG(dbgs() << " " << TRI->getName(PhysReg) |
| 315 | << " has no interferences in " << *Loop << '\n'); |
| 316 | break; |
Matt Beaumont-Gay | 3ef9f3d | 2010-12-14 21:14:55 +0000 | [diff] [blame] | 317 | } |
Jakob Stoklund Olesen | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 318 | } |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 319 | |
| 320 | if (!Loop) { |
| 321 | DEBUG(dbgs() << " All candidate loops have interference.\n"); |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | // Execute the split around Loop. |
| 326 | SmallVector<LiveInterval*, 4> SpillRegs; |
| 327 | LiveRangeEdit LREdit(VirtReg, SplitVRegs, SpillRegs); |
| 328 | SplitEditor(*SA, *LIS, *VRM, *DomTree, LREdit) |
| 329 | .splitAroundLoop(Loop->getLoop()); |
| 330 | |
Jakob Stoklund Olesen | af24964 | 2010-12-17 23:16:35 +0000 | [diff] [blame^] | 331 | if (VerifyEnabled) |
| 332 | MF->verify(this); |
| 333 | |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 334 | // We have new split regs, don't assign anything. |
Jakob Stoklund Olesen | b64d92e | 2010-12-14 00:37:44 +0000 | [diff] [blame] | 335 | return 0; |
| 336 | } |
| 337 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 338 | unsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg, |
| 339 | SmallVectorImpl<LiveInterval*> &SplitVRegs) { |
| 340 | // Populate a list of physical register spill candidates. |
Jakob Stoklund Olesen | 885b328 | 2010-12-14 00:58:47 +0000 | [diff] [blame] | 341 | SmallVector<unsigned, 8> PhysRegSpillCands; |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 342 | |
| 343 | // Check for an available register in this class. |
Jakob Stoklund Olesen | dd479e9 | 2010-12-10 22:21:05 +0000 | [diff] [blame] | 344 | AllocationOrder Order(VirtReg.reg, *VRM, ReservedRegs); |
| 345 | while (unsigned PhysReg = Order.next()) { |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 346 | // Check interference and as a side effect, intialize queries for this |
| 347 | // VirtReg and its aliases. |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 348 | unsigned InterfReg = checkPhysRegInterference(VirtReg, PhysReg); |
| 349 | if (InterfReg == 0) { |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 350 | // Found an available register. |
| 351 | return PhysReg; |
| 352 | } |
Jakob Stoklund Olesen | 9b0c4f8 | 2010-12-08 23:51:35 +0000 | [diff] [blame] | 353 | assert(!VirtReg.empty() && "Empty VirtReg has interference"); |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 354 | LiveInterval *InterferingVirtReg = |
| 355 | Queries[InterfReg].firstInterference().liveUnionPos().value(); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 356 | |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 357 | // The current VirtReg must either be spillable, or one of its interferences |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 358 | // must have less spill weight. |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 359 | if (InterferingVirtReg->weight < VirtReg.weight ) |
| 360 | PhysRegSpillCands.push_back(PhysReg); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 361 | } |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 362 | |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 363 | // Try to reassign interferences. |
| 364 | if (unsigned PhysReg = tryReassign(VirtReg, Order)) |
| 365 | return PhysReg; |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 366 | |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame] | 367 | // Try splitting VirtReg or interferences. |
Jakob Stoklund Olesen | b64d92e | 2010-12-14 00:37:44 +0000 | [diff] [blame] | 368 | unsigned PhysReg = trySplit(VirtReg, Order, SplitVRegs); |
| 369 | if (PhysReg || !SplitVRegs.empty()) |
| 370 | return PhysReg; |
| 371 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 372 | // Try to spill another interfering reg with less spill weight. |
Jakob Stoklund Olesen | 533f58e | 2010-12-11 00:19:56 +0000 | [diff] [blame] | 373 | NamedRegionTimer T("Spiller", TimerGroupName, TimePassesIsEnabled); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 374 | // |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 375 | // FIXME: do this in two steps: (1) check for unspillable interferences while |
| 376 | // accumulating spill weight; (2) spill the interferences with lowest |
| 377 | // aggregate spill weight. |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 378 | for (SmallVectorImpl<unsigned>::iterator PhysRegI = PhysRegSpillCands.begin(), |
| 379 | PhysRegE = PhysRegSpillCands.end(); PhysRegI != PhysRegE; ++PhysRegI) { |
| 380 | |
| 381 | if (!spillInterferences(VirtReg, *PhysRegI, SplitVRegs)) continue; |
| 382 | |
| 383 | assert(checkPhysRegInterference(VirtReg, *PhysRegI) == 0 && |
| 384 | "Interference after spill."); |
| 385 | // Tell the caller to allocate to this newly freed physical register. |
| 386 | return *PhysRegI; |
| 387 | } |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 388 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 389 | // No other spill candidates were found, so spill the current VirtReg. |
| 390 | DEBUG(dbgs() << "spilling: " << VirtReg << '\n'); |
| 391 | SmallVector<LiveInterval*, 1> pendingSpills; |
| 392 | |
| 393 | spiller().spill(&VirtReg, SplitVRegs, pendingSpills); |
| 394 | |
| 395 | // The live virtual register requesting allocation was spilled, so tell |
| 396 | // the caller not to allocate anything during this round. |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | bool RAGreedy::runOnMachineFunction(MachineFunction &mf) { |
| 401 | DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n" |
| 402 | << "********** Function: " |
| 403 | << ((Value*)mf.getFunction())->getName() << '\n'); |
| 404 | |
| 405 | MF = &mf; |
Jakob Stoklund Olesen | af24964 | 2010-12-17 23:16:35 +0000 | [diff] [blame^] | 406 | if (VerifyEnabled) |
| 407 | MF->verify(this); |
| 408 | |
Jakob Stoklund Olesen | 4680dec | 2010-12-10 23:49:00 +0000 | [diff] [blame] | 409 | RegAllocBase::init(getAnalysis<VirtRegMap>(), getAnalysis<LiveIntervals>()); |
Jakob Stoklund Olesen | f428eb6 | 2010-12-17 23:16:32 +0000 | [diff] [blame] | 410 | DomTree = &getAnalysis<MachineDominatorTree>(); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 411 | ReservedRegs = TRI->getReservedRegs(*MF); |
Jakob Stoklund Olesen | f6dff84 | 2010-12-10 22:54:44 +0000 | [diff] [blame] | 412 | SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM)); |
Jakob Stoklund Olesen | d0bb5e2 | 2010-12-15 23:46:13 +0000 | [diff] [blame] | 413 | Loops = &getAnalysis<MachineLoopInfo>(); |
| 414 | LoopRanges = &getAnalysis<MachineLoopRanges>(); |
| 415 | SA.reset(new SplitAnalysis(*MF, *LIS, *Loops)); |
| 416 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 417 | allocatePhysRegs(); |
| 418 | addMBBLiveIns(MF); |
| 419 | |
| 420 | // Run rewriter |
Jakob Stoklund Olesen | 533f58e | 2010-12-11 00:19:56 +0000 | [diff] [blame] | 421 | { |
| 422 | NamedRegionTimer T("Rewriter", TimerGroupName, TimePassesIsEnabled); |
| 423 | std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter()); |
| 424 | rewriter->runOnMachineFunction(*MF, *VRM, LIS); |
| 425 | } |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 426 | |
| 427 | // The pass output is in VirtRegMap. Release all the transient data. |
| 428 | releaseMemory(); |
| 429 | |
| 430 | return true; |
| 431 | } |