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" |
| 18 | #include "RegAllocBase.h" |
| 19 | #include "Spiller.h" |
| 20 | #include "VirtRegMap.h" |
| 21 | #include "VirtRegRewriter.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/AliasAnalysis.h" |
| 23 | #include "llvm/Function.h" |
| 24 | #include "llvm/PassAnalysisSupport.h" |
| 25 | #include "llvm/CodeGen/CalcSpillWeights.h" |
| 26 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
| 27 | #include "llvm/CodeGen/LiveStackAnalysis.h" |
| 28 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 30 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 31 | #include "llvm/CodeGen/Passes.h" |
| 32 | #include "llvm/CodeGen/RegAllocRegistry.h" |
| 33 | #include "llvm/CodeGen/RegisterCoalescer.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetOptions.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
| 36 | #include "llvm/Support/ErrorHandling.h" |
| 37 | #include "llvm/Support/raw_ostream.h" |
Jakob Stoklund Olesen | 533f58e | 2010-12-11 00:19:56 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Timer.h" |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 39 | |
| 40 | using namespace llvm; |
| 41 | |
| 42 | static RegisterRegAlloc greedyRegAlloc("greedy", "greedy register allocator", |
| 43 | createGreedyRegisterAllocator); |
| 44 | |
| 45 | namespace { |
| 46 | class RAGreedy : public MachineFunctionPass, public RegAllocBase { |
| 47 | // context |
| 48 | MachineFunction *MF; |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 49 | BitVector ReservedRegs; |
| 50 | |
| 51 | // analyses |
| 52 | LiveStacks *LS; |
| 53 | |
| 54 | // state |
| 55 | std::auto_ptr<Spiller> SpillerInstance; |
| 56 | |
| 57 | public: |
| 58 | RAGreedy(); |
| 59 | |
| 60 | /// Return the pass name. |
| 61 | virtual const char* getPassName() const { |
Jakob Stoklund Olesen | 533f58e | 2010-12-11 00:19:56 +0000 | [diff] [blame] | 62 | return "Greedy Register Allocator"; |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /// RAGreedy analysis usage. |
| 66 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 67 | |
| 68 | virtual void releaseMemory(); |
| 69 | |
| 70 | virtual Spiller &spiller() { return *SpillerInstance; } |
| 71 | |
Jakob Stoklund Olesen | 90c1d7d | 2010-12-08 22:57:16 +0000 | [diff] [blame] | 72 | virtual float getPriority(LiveInterval *LI); |
Jakob Stoklund Olesen | d0bec3e | 2010-12-08 22:22:41 +0000 | [diff] [blame] | 73 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 74 | virtual unsigned selectOrSplit(LiveInterval &VirtReg, |
| 75 | SmallVectorImpl<LiveInterval*> &SplitVRegs); |
| 76 | |
| 77 | /// Perform register allocation. |
| 78 | virtual bool runOnMachineFunction(MachineFunction &mf); |
| 79 | |
| 80 | static char ID; |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 81 | |
| 82 | private: |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame^] | 83 | bool checkUncachedInterference(LiveInterval&, unsigned); |
| 84 | LiveInterval *getSingleInterference(LiveInterval&, unsigned); |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 85 | bool reassignVReg(LiveInterval &InterferingVReg, unsigned OldPhysReg); |
| 86 | bool reassignInterferences(LiveInterval &VirtReg, unsigned PhysReg); |
Jakob Stoklund Olesen | b64d92e | 2010-12-14 00:37:44 +0000 | [diff] [blame] | 87 | |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame^] | 88 | unsigned tryReassign(LiveInterval&, AllocationOrder&); |
Jakob Stoklund Olesen | b64d92e | 2010-12-14 00:37:44 +0000 | [diff] [blame] | 89 | unsigned trySplit(LiveInterval&, AllocationOrder&, |
| 90 | SmallVectorImpl<LiveInterval*>&); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 91 | }; |
| 92 | } // end anonymous namespace |
| 93 | |
| 94 | char RAGreedy::ID = 0; |
| 95 | |
| 96 | FunctionPass* llvm::createGreedyRegisterAllocator() { |
| 97 | return new RAGreedy(); |
| 98 | } |
| 99 | |
| 100 | RAGreedy::RAGreedy(): MachineFunctionPass(ID) { |
| 101 | initializeLiveIntervalsPass(*PassRegistry::getPassRegistry()); |
| 102 | initializeSlotIndexesPass(*PassRegistry::getPassRegistry()); |
| 103 | initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry()); |
| 104 | initializeRegisterCoalescerAnalysisGroup(*PassRegistry::getPassRegistry()); |
| 105 | initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry()); |
| 106 | initializeLiveStacksPass(*PassRegistry::getPassRegistry()); |
| 107 | initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry()); |
| 108 | initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry()); |
| 109 | initializeVirtRegMapPass(*PassRegistry::getPassRegistry()); |
| 110 | } |
| 111 | |
| 112 | void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const { |
| 113 | AU.setPreservesCFG(); |
| 114 | AU.addRequired<AliasAnalysis>(); |
| 115 | AU.addPreserved<AliasAnalysis>(); |
| 116 | AU.addRequired<LiveIntervals>(); |
| 117 | AU.addPreserved<SlotIndexes>(); |
| 118 | if (StrongPHIElim) |
| 119 | AU.addRequiredID(StrongPHIEliminationID); |
| 120 | AU.addRequiredTransitive<RegisterCoalescer>(); |
| 121 | AU.addRequired<CalculateSpillWeights>(); |
| 122 | AU.addRequired<LiveStacks>(); |
| 123 | AU.addPreserved<LiveStacks>(); |
| 124 | AU.addRequiredID(MachineDominatorsID); |
| 125 | AU.addPreservedID(MachineDominatorsID); |
| 126 | AU.addRequired<MachineLoopInfo>(); |
| 127 | AU.addPreserved<MachineLoopInfo>(); |
| 128 | AU.addRequired<VirtRegMap>(); |
| 129 | AU.addPreserved<VirtRegMap>(); |
| 130 | MachineFunctionPass::getAnalysisUsage(AU); |
| 131 | } |
| 132 | |
| 133 | void RAGreedy::releaseMemory() { |
| 134 | SpillerInstance.reset(0); |
| 135 | RegAllocBase::releaseMemory(); |
| 136 | } |
| 137 | |
Jakob Stoklund Olesen | 90c1d7d | 2010-12-08 22:57:16 +0000 | [diff] [blame] | 138 | float RAGreedy::getPriority(LiveInterval *LI) { |
| 139 | float Priority = LI->weight; |
| 140 | |
| 141 | // Prioritize hinted registers so they are allocated first. |
| 142 | std::pair<unsigned, unsigned> Hint; |
| 143 | if (Hint.first || Hint.second) { |
| 144 | // The hint can be target specific, a virtual register, or a physreg. |
| 145 | Priority *= 2; |
| 146 | |
| 147 | // Prefer physreg hints above anything else. |
| 148 | if (Hint.first == 0 && TargetRegisterInfo::isPhysicalRegister(Hint.second)) |
| 149 | Priority *= 2; |
| 150 | } |
| 151 | return Priority; |
| 152 | } |
| 153 | |
Jakob Stoklund Olesen | 6ce219e | 2010-12-10 20:45:04 +0000 | [diff] [blame] | 154 | // Check interference without using the cache. |
| 155 | bool RAGreedy::checkUncachedInterference(LiveInterval &VirtReg, |
| 156 | unsigned PhysReg) { |
| 157 | LiveIntervalUnion::Query subQ(&VirtReg, &PhysReg2LiveUnion[PhysReg]); |
| 158 | if (subQ.checkInterference()) |
| 159 | return true; |
| 160 | for (const unsigned *AliasI = TRI->getAliasSet(PhysReg); *AliasI; ++AliasI) { |
| 161 | subQ.init(&VirtReg, &PhysReg2LiveUnion[*AliasI]); |
| 162 | if (subQ.checkInterference()) |
| 163 | return true; |
| 164 | } |
| 165 | return false; |
| 166 | } |
| 167 | |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame^] | 168 | /// getSingleInterference - Return the single interfering virtual register |
| 169 | /// assigned to PhysReg. Return 0 if more than one virtual register is |
| 170 | /// interfering. |
| 171 | LiveInterval *RAGreedy::getSingleInterference(LiveInterval &VirtReg, |
| 172 | unsigned PhysReg) { |
| 173 | LiveInterval *Interference = 0; |
| 174 | |
| 175 | // Check direct interferences. |
| 176 | LiveIntervalUnion::Query &Q = query(VirtReg, PhysReg); |
| 177 | if (Q.checkInterference()) { |
| 178 | if (!Q.seenAllInterferences()) |
| 179 | return 0; |
| 180 | Q.collectInterferingVRegs(1); |
| 181 | Interference = Q.interferingVRegs().front(); |
| 182 | } |
| 183 | |
| 184 | // Check aliases. |
| 185 | for (const unsigned *AliasI = TRI->getAliasSet(PhysReg); *AliasI; ++AliasI) { |
| 186 | LiveIntervalUnion::Query &Q = query(VirtReg, *AliasI); |
| 187 | if (Q.checkInterference()) { |
| 188 | if (Interference || !Q.seenAllInterferences()) |
| 189 | return 0; |
| 190 | Q.collectInterferingVRegs(1); |
| 191 | Interference = Q.interferingVRegs().front(); |
| 192 | } |
| 193 | } |
| 194 | return Interference; |
| 195 | } |
| 196 | |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 197 | // Attempt to reassign this virtual register to a different physical register. |
| 198 | // |
| 199 | // FIXME: we are not yet caching these "second-level" interferences discovered |
| 200 | // in the sub-queries. These interferences can change with each call to |
| 201 | // selectOrSplit. However, we could implement a "may-interfere" cache that |
| 202 | // could be conservatively dirtied when we reassign or split. |
| 203 | // |
| 204 | // FIXME: This may result in a lot of alias queries. We could summarize alias |
| 205 | // live intervals in their parent register's live union, but it's messy. |
| 206 | bool RAGreedy::reassignVReg(LiveInterval &InterferingVReg, |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame^] | 207 | unsigned WantedPhysReg) { |
| 208 | assert(TargetRegisterInfo::isVirtualRegister(InterferingVReg.reg) && |
| 209 | "Can only reassign virtual registers"); |
| 210 | assert(TRI->regsOverlap(WantedPhysReg, VRM->getPhys(InterferingVReg.reg)) && |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 211 | "inconsistent phys reg assigment"); |
| 212 | |
Jakob Stoklund Olesen | dd479e9 | 2010-12-10 22:21:05 +0000 | [diff] [blame] | 213 | AllocationOrder Order(InterferingVReg.reg, *VRM, ReservedRegs); |
| 214 | while (unsigned PhysReg = Order.next()) { |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame^] | 215 | // Don't reassign to a WantedPhysReg alias. |
| 216 | if (TRI->regsOverlap(PhysReg, WantedPhysReg)) |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 217 | continue; |
| 218 | |
Jakob Stoklund Olesen | 6ce219e | 2010-12-10 20:45:04 +0000 | [diff] [blame] | 219 | if (checkUncachedInterference(InterferingVReg, PhysReg)) |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 220 | continue; |
| 221 | |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 222 | // Reassign the interfering virtual reg to this physical reg. |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame^] | 223 | unsigned OldAssign = VRM->getPhys(InterferingVReg.reg); |
| 224 | DEBUG(dbgs() << "reassigning: " << InterferingVReg << " from " << |
| 225 | TRI->getName(OldAssign) << " to " << TRI->getName(PhysReg) << '\n'); |
| 226 | PhysReg2LiveUnion[OldAssign].extract(InterferingVReg); |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 227 | VRM->clearVirt(InterferingVReg.reg); |
| 228 | VRM->assignVirt2Phys(InterferingVReg.reg, PhysReg); |
| 229 | PhysReg2LiveUnion[PhysReg].unify(InterferingVReg); |
| 230 | |
| 231 | return true; |
| 232 | } |
| 233 | return false; |
| 234 | } |
| 235 | |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame^] | 236 | /// reassignInterferences - Reassign all interferences to different physical |
| 237 | /// registers such that Virtreg can be assigned to PhysReg. |
| 238 | /// Currently this only works with a single interference. |
| 239 | /// @param VirtReg Currently unassigned virtual register. |
| 240 | /// @param PhysReg Physical register to be cleared. |
| 241 | /// @return True on success, false if nothing was changed. |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 242 | bool RAGreedy::reassignInterferences(LiveInterval &VirtReg, unsigned PhysReg) { |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame^] | 243 | LiveInterval *InterferingVReg = getSingleInterference(VirtReg, PhysReg); |
| 244 | if (!InterferingVReg) |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 245 | return false; |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame^] | 246 | if (TargetRegisterInfo::isPhysicalRegister(InterferingVReg->reg)) |
| 247 | return false; |
| 248 | return reassignVReg(*InterferingVReg, PhysReg); |
| 249 | } |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 250 | |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame^] | 251 | /// tryReassign - Try to reassign interferences to different physregs. |
| 252 | /// @param VirtReg Currently unassigned virtual register. |
| 253 | /// @param Order Physregs to try. |
| 254 | /// @return Physreg to assign VirtReg, or 0. |
| 255 | unsigned RAGreedy::tryReassign(LiveInterval &VirtReg, AllocationOrder &Order) { |
| 256 | NamedRegionTimer T("Reassign", TimerGroupName, TimePassesIsEnabled); |
| 257 | Order.rewind(); |
| 258 | while (unsigned PhysReg = Order.next()) |
| 259 | if (reassignInterferences(VirtReg, PhysReg)) |
| 260 | return PhysReg; |
| 261 | return 0; |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Jakob Stoklund Olesen | b64d92e | 2010-12-14 00:37:44 +0000 | [diff] [blame] | 264 | /// trySplit - Try to split VirtReg or one of its interferences, making it |
| 265 | /// assignable. |
| 266 | /// @return Physreg when VirtReg may be assigned and/or new SplitVRegs. |
| 267 | unsigned RAGreedy::trySplit(LiveInterval &VirtReg, AllocationOrder &Order, |
| 268 | SmallVectorImpl<LiveInterval*>&SplitVRegs) { |
| 269 | NamedRegionTimer T("Splitter", TimerGroupName, TimePassesIsEnabled); |
| 270 | return 0; |
| 271 | } |
| 272 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 273 | unsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg, |
| 274 | SmallVectorImpl<LiveInterval*> &SplitVRegs) { |
| 275 | // Populate a list of physical register spill candidates. |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 276 | SmallVector<unsigned, 8> PhysRegSpillCands, ReassignCands; |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 277 | |
| 278 | // Check for an available register in this class. |
Jakob Stoklund Olesen | dd479e9 | 2010-12-10 22:21:05 +0000 | [diff] [blame] | 279 | AllocationOrder Order(VirtReg.reg, *VRM, ReservedRegs); |
| 280 | while (unsigned PhysReg = Order.next()) { |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 281 | // Check interference and as a side effect, intialize queries for this |
| 282 | // VirtReg and its aliases. |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 283 | unsigned InterfReg = checkPhysRegInterference(VirtReg, PhysReg); |
| 284 | if (InterfReg == 0) { |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 285 | // Found an available register. |
| 286 | return PhysReg; |
| 287 | } |
Jakob Stoklund Olesen | 9b0c4f8 | 2010-12-08 23:51:35 +0000 | [diff] [blame] | 288 | assert(!VirtReg.empty() && "Empty VirtReg has interference"); |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 289 | LiveInterval *InterferingVirtReg = |
| 290 | Queries[InterfReg].firstInterference().liveUnionPos().value(); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 291 | |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 292 | // 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] | 293 | // must have less spill weight. |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame^] | 294 | if (InterferingVirtReg->weight < VirtReg.weight ) |
| 295 | PhysRegSpillCands.push_back(PhysReg); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 296 | } |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 297 | |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame^] | 298 | // Try to reassign interferences. |
| 299 | if (unsigned PhysReg = tryReassign(VirtReg, Order)) |
| 300 | return PhysReg; |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 301 | |
Jakob Stoklund Olesen | 46c83c8 | 2010-12-14 00:37:49 +0000 | [diff] [blame^] | 302 | // Try splitting VirtReg or interferences. |
Jakob Stoklund Olesen | b64d92e | 2010-12-14 00:37:44 +0000 | [diff] [blame] | 303 | unsigned PhysReg = trySplit(VirtReg, Order, SplitVRegs); |
| 304 | if (PhysReg || !SplitVRegs.empty()) |
| 305 | return PhysReg; |
| 306 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 307 | // Try to spill another interfering reg with less spill weight. |
Jakob Stoklund Olesen | 533f58e | 2010-12-11 00:19:56 +0000 | [diff] [blame] | 308 | NamedRegionTimer T("Spiller", TimerGroupName, TimePassesIsEnabled); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 309 | // |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 310 | // FIXME: do this in two steps: (1) check for unspillable interferences while |
| 311 | // accumulating spill weight; (2) spill the interferences with lowest |
| 312 | // aggregate spill weight. |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 313 | for (SmallVectorImpl<unsigned>::iterator PhysRegI = PhysRegSpillCands.begin(), |
| 314 | PhysRegE = PhysRegSpillCands.end(); PhysRegI != PhysRegE; ++PhysRegI) { |
| 315 | |
| 316 | if (!spillInterferences(VirtReg, *PhysRegI, SplitVRegs)) continue; |
| 317 | |
| 318 | assert(checkPhysRegInterference(VirtReg, *PhysRegI) == 0 && |
| 319 | "Interference after spill."); |
| 320 | // Tell the caller to allocate to this newly freed physical register. |
| 321 | return *PhysRegI; |
| 322 | } |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 323 | |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 324 | // No other spill candidates were found, so spill the current VirtReg. |
| 325 | DEBUG(dbgs() << "spilling: " << VirtReg << '\n'); |
| 326 | SmallVector<LiveInterval*, 1> pendingSpills; |
| 327 | |
| 328 | spiller().spill(&VirtReg, SplitVRegs, pendingSpills); |
| 329 | |
| 330 | // The live virtual register requesting allocation was spilled, so tell |
| 331 | // the caller not to allocate anything during this round. |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | bool RAGreedy::runOnMachineFunction(MachineFunction &mf) { |
| 336 | DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n" |
| 337 | << "********** Function: " |
| 338 | << ((Value*)mf.getFunction())->getName() << '\n'); |
| 339 | |
| 340 | MF = &mf; |
Jakob Stoklund Olesen | 4680dec | 2010-12-10 23:49:00 +0000 | [diff] [blame] | 341 | RegAllocBase::init(getAnalysis<VirtRegMap>(), getAnalysis<LiveIntervals>()); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 342 | |
| 343 | ReservedRegs = TRI->getReservedRegs(*MF); |
Jakob Stoklund Olesen | f6dff84 | 2010-12-10 22:54:44 +0000 | [diff] [blame] | 344 | SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM)); |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 345 | allocatePhysRegs(); |
| 346 | addMBBLiveIns(MF); |
| 347 | |
| 348 | // Run rewriter |
Jakob Stoklund Olesen | 533f58e | 2010-12-11 00:19:56 +0000 | [diff] [blame] | 349 | { |
| 350 | NamedRegionTimer T("Rewriter", TimerGroupName, TimePassesIsEnabled); |
| 351 | std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter()); |
| 352 | rewriter->runOnMachineFunction(*MF, *VRM, LIS); |
| 353 | } |
Jakob Stoklund Olesen | cba2e06 | 2010-12-08 03:26:16 +0000 | [diff] [blame] | 354 | |
| 355 | // The pass output is in VirtRegMap. Release all the transient data. |
| 356 | releaseMemory(); |
| 357 | |
| 358 | return true; |
| 359 | } |