Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 1 | //===-- RegAllocBasic.cpp - basic 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 RABasic function pass, which provides a minimal |
| 11 | // implementation of the basic register allocator. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "regalloc" |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 16 | #include "LiveIntervalUnion.h" |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 17 | #include "RegAllocBase.h" |
| 18 | #include "RenderMachineFunction.h" |
| 19 | #include "Spiller.h" |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 20 | #include "VirtRegMap.h" |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 21 | #include "VirtRegRewriter.h" |
Andrew Trick | 8a83d54 | 2010-11-11 17:46:29 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/AliasAnalysis.h" |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 23 | #include "llvm/Function.h" |
| 24 | #include "llvm/PassAnalysisSupport.h" |
| 25 | #include "llvm/CodeGen/CalcSpillWeights.h" |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/LiveStackAnalysis.h" |
| 28 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 29 | #include "llvm/CodeGen/MachineInstr.h" |
| 30 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 31 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 32 | #include "llvm/CodeGen/Passes.h" |
| 33 | #include "llvm/CodeGen/RegAllocRegistry.h" |
| 34 | #include "llvm/CodeGen/RegisterCoalescer.h" |
| 35 | #include "llvm/Target/TargetMachine.h" |
| 36 | #include "llvm/Target/TargetOptions.h" |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetRegisterInfo.h" |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 38 | #ifndef NDEBUG |
| 39 | #include "llvm/ADT/SparseBitVector.h" |
| 40 | #endif |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Debug.h" |
| 42 | #include "llvm/Support/ErrorHandling.h" |
| 43 | #include "llvm/Support/raw_ostream.h" |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 44 | |
| 45 | #include <vector> |
| 46 | #include <queue> |
| 47 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 48 | using namespace llvm; |
| 49 | |
| 50 | static RegisterRegAlloc basicRegAlloc("basic", "basic register allocator", |
| 51 | createBasicRegisterAllocator); |
| 52 | |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 53 | // Temporary verification option until we can put verification inside |
| 54 | // MachineVerifier. |
| 55 | static cl::opt<bool> |
| 56 | VerifyRegAlloc("verify-regalloc", |
| 57 | cl::desc("Verify live intervals before renaming")); |
| 58 | |
Benjamin Kramer | c62feda | 2010-11-25 16:42:51 +0000 | [diff] [blame] | 59 | namespace { |
| 60 | |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 61 | class PhysicalRegisterDescription : public AbstractRegisterDescription { |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 62 | const TargetRegisterInfo *TRI; |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 63 | public: |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 64 | PhysicalRegisterDescription(const TargetRegisterInfo *T): TRI(T) {} |
| 65 | virtual const char *getName(unsigned Reg) const { return TRI->getName(Reg); } |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 68 | /// RABasic provides a minimal implementation of the basic register allocation |
| 69 | /// algorithm. It prioritizes live virtual registers by spill weight and spills |
| 70 | /// whenever a register is unavailable. This is not practical in production but |
| 71 | /// provides a useful baseline both for measuring other allocators and comparing |
| 72 | /// the speed of the basic algorithm against other styles of allocators. |
| 73 | class RABasic : public MachineFunctionPass, public RegAllocBase |
| 74 | { |
| 75 | // context |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 76 | MachineFunction *MF; |
| 77 | const TargetMachine *TM; |
| 78 | MachineRegisterInfo *MRI; |
| 79 | |
| 80 | BitVector ReservedRegs; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 81 | |
| 82 | // analyses |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 83 | LiveStacks *LS; |
| 84 | RenderMachineFunction *RMF; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 85 | |
| 86 | // state |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 87 | std::auto_ptr<Spiller> SpillerInstance; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 88 | |
| 89 | public: |
| 90 | RABasic(); |
| 91 | |
| 92 | /// Return the pass name. |
| 93 | virtual const char* getPassName() const { |
| 94 | return "Basic Register Allocator"; |
| 95 | } |
| 96 | |
| 97 | /// RABasic analysis usage. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 98 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 99 | |
| 100 | virtual void releaseMemory(); |
| 101 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 102 | virtual Spiller &spiller() { return *SpillerInstance; } |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 103 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 104 | virtual unsigned selectOrSplit(LiveInterval &VirtReg, |
| 105 | SmallVectorImpl<LiveInterval*> &SplitVRegs); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 106 | |
| 107 | /// Perform register allocation. |
| 108 | virtual bool runOnMachineFunction(MachineFunction &mf); |
| 109 | |
| 110 | static char ID; |
Andrew Trick | 316df4b | 2010-11-20 02:57:05 +0000 | [diff] [blame] | 111 | |
| 112 | private: |
| 113 | void addMBBLiveIns(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | char RABasic::ID = 0; |
| 117 | |
| 118 | } // end anonymous namespace |
| 119 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 120 | RABasic::RABasic(): MachineFunctionPass(ID) { |
| 121 | initializeLiveIntervalsPass(*PassRegistry::getPassRegistry()); |
| 122 | initializeSlotIndexesPass(*PassRegistry::getPassRegistry()); |
| 123 | initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry()); |
| 124 | initializeRegisterCoalescerAnalysisGroup(*PassRegistry::getPassRegistry()); |
| 125 | initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry()); |
| 126 | initializeLiveStacksPass(*PassRegistry::getPassRegistry()); |
Jakob Stoklund Olesen | 964bc25 | 2010-11-03 20:39:26 +0000 | [diff] [blame] | 127 | initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry()); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 128 | initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry()); |
| 129 | initializeVirtRegMapPass(*PassRegistry::getPassRegistry()); |
| 130 | initializeRenderMachineFunctionPass(*PassRegistry::getPassRegistry()); |
| 131 | } |
| 132 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 133 | void RABasic::getAnalysisUsage(AnalysisUsage &AU) const { |
| 134 | AU.setPreservesCFG(); |
| 135 | AU.addRequired<AliasAnalysis>(); |
| 136 | AU.addPreserved<AliasAnalysis>(); |
| 137 | AU.addRequired<LiveIntervals>(); |
| 138 | AU.addPreserved<SlotIndexes>(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 139 | if (StrongPHIElim) |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 140 | AU.addRequiredID(StrongPHIEliminationID); |
| 141 | AU.addRequiredTransitive<RegisterCoalescer>(); |
| 142 | AU.addRequired<CalculateSpillWeights>(); |
| 143 | AU.addRequired<LiveStacks>(); |
| 144 | AU.addPreserved<LiveStacks>(); |
| 145 | AU.addRequiredID(MachineDominatorsID); |
| 146 | AU.addPreservedID(MachineDominatorsID); |
| 147 | AU.addRequired<MachineLoopInfo>(); |
| 148 | AU.addPreserved<MachineLoopInfo>(); |
| 149 | AU.addRequired<VirtRegMap>(); |
| 150 | AU.addPreserved<VirtRegMap>(); |
| 151 | DEBUG(AU.addRequired<RenderMachineFunction>()); |
| 152 | MachineFunctionPass::getAnalysisUsage(AU); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void RABasic::releaseMemory() { |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 156 | SpillerInstance.reset(0); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 157 | RegAllocBase::releaseMemory(); |
| 158 | } |
| 159 | |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 160 | #ifndef NDEBUG |
| 161 | // Verify each LiveIntervalUnion. |
| 162 | void RegAllocBase::verify() { |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 163 | LiveVirtRegBitSet VisitedVRegs; |
| 164 | OwningArrayPtr<LiveVirtRegBitSet> |
| 165 | unionVRegs(new LiveVirtRegBitSet[PhysReg2LiveUnion.numRegs()]); |
| 166 | |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 167 | // Verify disjoint unions. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 168 | for (unsigned PhysReg = 0; PhysReg < PhysReg2LiveUnion.numRegs(); ++PhysReg) { |
| 169 | DEBUG(PhysicalRegisterDescription PRD(TRI); |
| 170 | PhysReg2LiveUnion[PhysReg].dump(&PRD)); |
| 171 | LiveVirtRegBitSet &VRegs = unionVRegs[PhysReg]; |
| 172 | PhysReg2LiveUnion[PhysReg].verify(VRegs); |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 173 | // Union + intersection test could be done efficiently in one pass, but |
| 174 | // don't add a method to SparseBitVector unless we really need it. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 175 | assert(!VisitedVRegs.intersects(VRegs) && "vreg in multiple unions"); |
| 176 | VisitedVRegs |= VRegs; |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 177 | } |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 178 | |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 179 | // Verify vreg coverage. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 180 | for (LiveIntervals::iterator liItr = LIS->begin(), liEnd = LIS->end(); |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 181 | liItr != liEnd; ++liItr) { |
| 182 | unsigned reg = liItr->first; |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 183 | if (TargetRegisterInfo::isPhysicalRegister(reg)) continue; |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 184 | if (!VRM->hasPhys(reg)) continue; // spilled? |
| 185 | unsigned PhysReg = VRM->getPhys(reg); |
| 186 | if (!unionVRegs[PhysReg].test(reg)) { |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 187 | dbgs() << "LiveVirtReg " << reg << " not in union " << |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 188 | TRI->getName(PhysReg) << "\n"; |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 189 | llvm_unreachable("unallocated live vreg"); |
| 190 | } |
| 191 | } |
| 192 | // FIXME: I'm not sure how to verify spilled intervals. |
| 193 | } |
| 194 | #endif //!NDEBUG |
| 195 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 196 | //===----------------------------------------------------------------------===// |
| 197 | // RegAllocBase Implementation |
| 198 | //===----------------------------------------------------------------------===// |
| 199 | |
| 200 | // Instantiate a LiveIntervalUnion for each physical register. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 201 | void RegAllocBase::LiveUnionArray::init(unsigned NRegs) { |
| 202 | Array.reset(new LiveIntervalUnion[NRegs]); |
| 203 | NumRegs = NRegs; |
| 204 | for (unsigned RegNum = 0; RegNum < NRegs; ++RegNum) { |
| 205 | Array[RegNum].init(RegNum); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | void RegAllocBase::init(const TargetRegisterInfo &tri, VirtRegMap &vrm, |
| 210 | LiveIntervals &lis) { |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 211 | TRI = &tri; |
| 212 | VRM = &vrm; |
| 213 | LIS = &lis; |
| 214 | PhysReg2LiveUnion.init(TRI->getNumRegs()); |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 215 | // Cache an interferece query for each physical reg |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 216 | Queries.reset(new LiveIntervalUnion::Query[PhysReg2LiveUnion.numRegs()]); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 219 | void RegAllocBase::LiveUnionArray::clear() { |
| 220 | NumRegs = 0; |
| 221 | Array.reset(0); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | void RegAllocBase::releaseMemory() { |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 225 | PhysReg2LiveUnion.clear(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 228 | namespace llvm { |
| 229 | /// This class defines a queue of live virtual registers prioritized by spill |
| 230 | /// weight. The heaviest vreg is popped first. |
| 231 | /// |
| 232 | /// Currently, this is trivial wrapper that gives us an opaque type in the |
| 233 | /// header, but we may later give it a virtual interface for register allocators |
| 234 | /// to override the priority queue comparator. |
| 235 | class LiveVirtRegQueue { |
| 236 | typedef std::priority_queue |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 237 | <LiveInterval*, std::vector<LiveInterval*>, LessSpillWeightPriority> |
| 238 | PriorityQ; |
| 239 | PriorityQ PQ; |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 240 | |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 241 | public: |
| 242 | // Is the queue empty? |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 243 | bool empty() { return PQ.empty(); } |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 244 | |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 245 | // Get the highest priority lvr (top + pop) |
| 246 | LiveInterval *get() { |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 247 | LiveInterval *VirtReg = PQ.top(); |
| 248 | PQ.pop(); |
| 249 | return VirtReg; |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 250 | } |
| 251 | // Add this lvr to the queue |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 252 | void push(LiveInterval *VirtReg) { |
| 253 | PQ.push(VirtReg); |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 254 | } |
| 255 | }; |
| 256 | } // end namespace llvm |
| 257 | |
| 258 | // Visit all the live virtual registers. If they are already assigned to a |
| 259 | // physical register, unify them with the corresponding LiveIntervalUnion, |
| 260 | // otherwise push them on the priority queue for later assignment. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 261 | void RegAllocBase::seedLiveVirtRegs(LiveVirtRegQueue &VirtRegQ) { |
| 262 | for (LiveIntervals::iterator I = LIS->begin(), E = LIS->end(); I != E; ++I) { |
| 263 | unsigned RegNum = I->first; |
| 264 | LiveInterval &VirtReg = *I->second; |
| 265 | if (TargetRegisterInfo::isPhysicalRegister(RegNum)) { |
| 266 | PhysReg2LiveUnion[RegNum].unify(VirtReg); |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 267 | } |
| 268 | else { |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 269 | VirtRegQ.push(&VirtReg); |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 274 | // Top-level driver to manage the queue of unassigned VirtRegs and call the |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 275 | // selectOrSplit implementation. |
| 276 | void RegAllocBase::allocatePhysRegs() { |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 277 | |
| 278 | // Push each vreg onto a queue or "precolor" by adding it to a physreg union. |
| 279 | LiveVirtRegQueue VirtRegQ; |
| 280 | seedLiveVirtRegs(VirtRegQ); |
| 281 | |
| 282 | // Continue assigning vregs one at a time to available physical registers. |
| 283 | while (!VirtRegQ.empty()) { |
| 284 | // Pop the highest priority vreg. |
| 285 | LiveInterval *VirtReg = VirtRegQ.get(); |
| 286 | |
| 287 | // selectOrSplit requests the allocator to return an available physical |
| 288 | // register if possible and populate a list of new live intervals that |
| 289 | // result from splitting. |
| 290 | typedef SmallVector<LiveInterval*, 4> VirtRegVec; |
| 291 | VirtRegVec SplitVRegs; |
| 292 | unsigned AvailablePhysReg = selectOrSplit(*VirtReg, SplitVRegs); |
| 293 | |
| 294 | if (AvailablePhysReg) { |
| 295 | DEBUG(dbgs() << "allocating: " << TRI->getName(AvailablePhysReg) << |
| 296 | " " << *VirtReg << '\n'); |
| 297 | assert(!VRM->hasPhys(VirtReg->reg) && "duplicate vreg in union"); |
| 298 | VRM->assignVirt2Phys(VirtReg->reg, AvailablePhysReg); |
| 299 | PhysReg2LiveUnion[AvailablePhysReg].unify(*VirtReg); |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 300 | } |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 301 | for (VirtRegVec::iterator I = SplitVRegs.begin(), E = SplitVRegs.end(); |
| 302 | I != E; ++I) { |
| 303 | LiveInterval* SplitVirtReg = *I; |
| 304 | if (SplitVirtReg->empty()) continue; |
| 305 | DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n"); |
| 306 | assert(TargetRegisterInfo::isVirtualRegister(SplitVirtReg->reg) && |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 307 | "expect split value in virtual register"); |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 308 | VirtRegQ.push(SplitVirtReg); |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 313 | // Check if this live virtual register interferes with a physical register. If |
| 314 | // not, then check for interference on each register that aliases with the |
| 315 | // physical register. Return the interfering register. |
| 316 | unsigned RegAllocBase::checkPhysRegInterference(LiveInterval &VirtReg, |
| 317 | unsigned PhysReg) { |
| 318 | if (query(VirtReg, PhysReg).checkInterference()) |
| 319 | return PhysReg; |
| 320 | for (const unsigned *AliasI = TRI->getAliasSet(PhysReg); *AliasI; ++AliasI) { |
| 321 | if (query(VirtReg, *AliasI).checkInterference()) |
| 322 | return *AliasI; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 323 | } |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 324 | return 0; |
| 325 | } |
| 326 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 327 | // Helper for spillInteferences() that spills all interfering vregs currently |
| 328 | // assigned to this physical register. |
| 329 | void RegAllocBase::spillReg(LiveInterval& VirtReg, unsigned PhysReg, |
| 330 | SmallVectorImpl<LiveInterval*> &SplitVRegs) { |
| 331 | LiveIntervalUnion::Query &Q = query(VirtReg, PhysReg); |
| 332 | assert(Q.seenAllInterferences() && "need collectInterferences()"); |
| 333 | const SmallVectorImpl<LiveInterval*> &PendingSpills = Q.interferingVRegs(); |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 334 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 335 | for (SmallVectorImpl<LiveInterval*>::const_iterator I = PendingSpills.begin(), |
| 336 | E = PendingSpills.end(); I != E; ++I) { |
| 337 | LiveInterval &SpilledVReg = **I; |
Andrew Trick | 8a83d54 | 2010-11-11 17:46:29 +0000 | [diff] [blame] | 338 | DEBUG(dbgs() << "extracting from " << |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 339 | TRI->getName(PhysReg) << " " << SpilledVReg << '\n'); |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 340 | |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 341 | // Deallocate the interfering vreg by removing it from the union. |
| 342 | // A LiveInterval instance may not be in a union during modification! |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 343 | PhysReg2LiveUnion[PhysReg].extract(SpilledVReg); |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 344 | |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 345 | // Clear the vreg assignment. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 346 | VRM->clearVirt(SpilledVReg.reg); |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 347 | |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 348 | // Spill the extracted interval. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 349 | spiller().spill(&SpilledVReg, SplitVRegs, PendingSpills); |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 350 | } |
Andrew Trick | 8a83d54 | 2010-11-11 17:46:29 +0000 | [diff] [blame] | 351 | // After extracting segments, the query's results are invalid. But keep the |
| 352 | // contents valid until we're done accessing pendingSpills. |
| 353 | Q.clear(); |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 356 | // Spill or split all live virtual registers currently unified under PhysReg |
| 357 | // that interfere with VirtReg. The newly spilled or split live intervals are |
| 358 | // returned by appending them to SplitVRegs. |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 359 | bool |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 360 | RegAllocBase::spillInterferences(LiveInterval &VirtReg, unsigned PhysReg, |
| 361 | SmallVectorImpl<LiveInterval*> &SplitVRegs) { |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 362 | // Record each interference and determine if all are spillable before mutating |
| 363 | // either the union or live intervals. |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 364 | |
Andrew Trick | 8a83d54 | 2010-11-11 17:46:29 +0000 | [diff] [blame] | 365 | // Collect interferences assigned to the requested physical register. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 366 | LiveIntervalUnion::Query &QPreg = query(VirtReg, PhysReg); |
| 367 | unsigned NumInterferences = QPreg.collectInterferingVRegs(); |
Andrew Trick | 8a83d54 | 2010-11-11 17:46:29 +0000 | [diff] [blame] | 368 | if (QPreg.seenUnspillableVReg()) { |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 369 | return false; |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 370 | } |
Andrew Trick | 8a83d54 | 2010-11-11 17:46:29 +0000 | [diff] [blame] | 371 | // Collect interferences assigned to any alias of the physical register. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 372 | for (const unsigned *asI = TRI->getAliasSet(PhysReg); *asI; ++asI) { |
| 373 | LiveIntervalUnion::Query &QAlias = query(VirtReg, *asI); |
| 374 | NumInterferences += QAlias.collectInterferingVRegs(); |
Andrew Trick | 8a83d54 | 2010-11-11 17:46:29 +0000 | [diff] [blame] | 375 | if (QAlias.seenUnspillableVReg()) { |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 376 | return false; |
| 377 | } |
| 378 | } |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 379 | DEBUG(dbgs() << "spilling " << TRI->getName(PhysReg) << |
| 380 | " interferences with " << VirtReg << "\n"); |
| 381 | assert(NumInterferences > 0 && "expect interference"); |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 382 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 383 | // Spill each interfering vreg allocated to PhysReg or an alias. |
| 384 | spillReg(VirtReg, PhysReg, SplitVRegs); |
| 385 | for (const unsigned *AliasI = TRI->getAliasSet(PhysReg); *AliasI; ++AliasI) |
| 386 | spillReg(VirtReg, *AliasI, SplitVRegs); |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 387 | return true; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | //===----------------------------------------------------------------------===// |
| 391 | // RABasic Implementation |
| 392 | //===----------------------------------------------------------------------===// |
| 393 | |
| 394 | // Driver for the register assignment and splitting heuristics. |
| 395 | // Manages iteration over the LiveIntervalUnions. |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 396 | // |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 397 | // This is a minimal implementation of register assignment and splitting that |
| 398 | // spills whenever we run out of registers. |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 399 | // |
| 400 | // selectOrSplit can only be called once per live virtual register. We then do a |
| 401 | // single interference test for each register the correct class until we find an |
| 402 | // available register. So, the number of interference tests in the worst case is |
| 403 | // |vregs| * |machineregs|. And since the number of interference tests is |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 404 | // minimal, there is no value in caching them outside the scope of |
| 405 | // selectOrSplit(). |
| 406 | unsigned RABasic::selectOrSplit(LiveInterval &VirtReg, |
| 407 | SmallVectorImpl<LiveInterval*> &SplitVRegs) { |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 408 | // Populate a list of physical register spill candidates. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 409 | SmallVector<unsigned, 8> PhysRegSpillCands; |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 410 | |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 411 | // Check for an available register in this class. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 412 | const TargetRegisterClass *TRC = MRI->getRegClass(VirtReg.reg); |
| 413 | DEBUG(dbgs() << "RegClass: " << TRC->getName() << ' '); |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 414 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 415 | for (TargetRegisterClass::iterator I = TRC->allocation_order_begin(*MF), |
| 416 | E = TRC->allocation_order_end(*MF); |
| 417 | I != E; ++I) { |
| 418 | |
| 419 | unsigned PhysReg = *I; |
| 420 | if (ReservedRegs.test(PhysReg)) continue; |
| 421 | |
| 422 | // Check interference and as a side effect, intialize queries for this |
| 423 | // VirtReg and its aliases. |
| 424 | unsigned interfReg = checkPhysRegInterference(VirtReg, PhysReg); |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 425 | if (interfReg == 0) { |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 426 | // Found an available register. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 427 | return PhysReg; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 428 | } |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 429 | LiveInterval *interferingVirtReg = |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 430 | Queries[interfReg].firstInterference().liveUnionPos()->VirtReg; |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 431 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 432 | // The current VirtReg must either spillable, or one of its interferences |
| 433 | // must have less spill weight. |
| 434 | if (interferingVirtReg->weight < VirtReg.weight ) { |
| 435 | PhysRegSpillCands.push_back(PhysReg); |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 436 | } |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 437 | } |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 438 | // Try to spill another interfering reg with less spill weight. |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 439 | // |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 440 | // FIXME: RAGreedy will sort this list by spill weight. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 441 | for (SmallVectorImpl<unsigned>::iterator PhysRegI = PhysRegSpillCands.begin(), |
| 442 | PhysRegE = PhysRegSpillCands.end(); PhysRegI != PhysRegE; ++PhysRegI) { |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 443 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 444 | if (!spillInterferences(VirtReg, *PhysRegI, SplitVRegs)) continue; |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 445 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 446 | unsigned InterferingReg = checkPhysRegInterference(VirtReg, *PhysRegI); |
| 447 | if (InterferingReg != 0) { |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 448 | const LiveSegment &seg = |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 449 | *Queries[InterferingReg].firstInterference().liveUnionPos(); |
| 450 | |
| 451 | dbgs() << "spilling cannot free " << TRI->getName(*PhysRegI) << |
| 452 | " for " << VirtReg.reg << " with interference " << *seg.VirtReg << "\n"; |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 453 | llvm_unreachable("Interference after spill."); |
| 454 | } |
| 455 | // Tell the caller to allocate to this newly freed physical register. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 456 | return *PhysRegI; |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 457 | } |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 458 | // No other spill candidates were found, so spill the current VirtReg. |
| 459 | DEBUG(dbgs() << "spilling: " << VirtReg << '\n'); |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 460 | SmallVector<LiveInterval*, 1> pendingSpills; |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 461 | |
| 462 | spiller().spill(&VirtReg, SplitVRegs, pendingSpills); |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 463 | |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 464 | // The live virtual register requesting allocation was spilled, so tell |
| 465 | // the caller not to allocate anything during this round. |
| 466 | return 0; |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 467 | } |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 468 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 469 | // Add newly allocated physical registers to the MBB live in sets. |
Andrew Trick | 316df4b | 2010-11-20 02:57:05 +0000 | [diff] [blame] | 470 | void RABasic::addMBBLiveIns() { |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 471 | typedef SmallVector<MachineBasicBlock*, 8> MBBVec; |
| 472 | MBBVec liveInMBBs; |
| 473 | MachineBasicBlock &entryMBB = *MF->begin(); |
Andrew Trick | 316df4b | 2010-11-20 02:57:05 +0000 | [diff] [blame] | 474 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 475 | for (unsigned PhysReg = 0; PhysReg < PhysReg2LiveUnion.numRegs(); ++PhysReg) { |
| 476 | LiveIntervalUnion &LiveUnion = PhysReg2LiveUnion[PhysReg]; |
| 477 | |
| 478 | for (LiveIntervalUnion::SegmentIter SI = LiveUnion.begin(), |
| 479 | SegEnd = LiveUnion.end(); |
| 480 | SI != SegEnd; ++SI) { |
| 481 | |
Andrew Trick | 316df4b | 2010-11-20 02:57:05 +0000 | [diff] [blame] | 482 | // Find the set of basic blocks which this range is live into... |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 483 | liveInMBBs.clear(); |
| 484 | if (!LIS->findLiveInMBBs(SI->Start, SI->End, liveInMBBs)) continue; |
| 485 | |
| 486 | // And add the physreg for this interval to their live-in sets. |
| 487 | for (MBBVec::iterator I = liveInMBBs.begin(), E = liveInMBBs.end(); |
| 488 | I != E; ++I) { |
| 489 | MachineBasicBlock *MBB = *I; |
| 490 | if (MBB == &entryMBB) continue; |
| 491 | if (MBB->isLiveIn(PhysReg)) continue; |
| 492 | MBB->addLiveIn(PhysReg); |
Andrew Trick | 316df4b | 2010-11-20 02:57:05 +0000 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 498 | bool RABasic::runOnMachineFunction(MachineFunction &mf) { |
| 499 | DEBUG(dbgs() << "********** BASIC REGISTER ALLOCATION **********\n" |
| 500 | << "********** Function: " |
| 501 | << ((Value*)mf.getFunction())->getName() << '\n'); |
| 502 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 503 | MF = &mf; |
| 504 | TM = &mf.getTarget(); |
| 505 | MRI = &mf.getRegInfo(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 506 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 507 | DEBUG(RMF = &getAnalysis<RenderMachineFunction>()); |
Andrew Trick | 8a83d54 | 2010-11-11 17:46:29 +0000 | [diff] [blame] | 508 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 509 | const TargetRegisterInfo *TRI = TM->getRegisterInfo(); |
Andrew Trick | 8a83d54 | 2010-11-11 17:46:29 +0000 | [diff] [blame] | 510 | RegAllocBase::init(*TRI, getAnalysis<VirtRegMap>(), |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 511 | getAnalysis<LiveIntervals>()); |
| 512 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 513 | ReservedRegs = TRI->getReservedRegs(*MF); |
Andrew Trick | 8a83d54 | 2010-11-11 17:46:29 +0000 | [diff] [blame] | 514 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 515 | SpillerInstance.reset(createSpiller(*this, *MF, *VRM)); |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 516 | |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 517 | allocatePhysRegs(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 518 | |
Andrew Trick | 316df4b | 2010-11-20 02:57:05 +0000 | [diff] [blame] | 519 | addMBBLiveIns(); |
| 520 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 521 | // Diagnostic output before rewriting |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 522 | DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *VRM << "\n"); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 523 | |
| 524 | // optional HTML output |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 525 | DEBUG(RMF->renderMachineFunction("After basic register allocation.", VRM)); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 526 | |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 527 | // FIXME: Verification currently must run before VirtRegRewriter. We should |
| 528 | // make the rewriter a separate pass and override verifyAnalysis instead. When |
| 529 | // that happens, verification naturally falls under VerifyMachineCode. |
| 530 | #ifndef NDEBUG |
| 531 | if (VerifyRegAlloc) { |
| 532 | // Verify accuracy of LiveIntervals. The standard machine code verifier |
| 533 | // ensures that each LiveIntervals covers all uses of the virtual reg. |
| 534 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 535 | // FIXME: MachineVerifier is badly broken when using the standard |
| 536 | // spiller. Always use -spiller=inline with -verify-regalloc. Even with the |
| 537 | // inline spiller, some tests fail to verify because the coalescer does not |
| 538 | // always generate verifiable code. |
| 539 | MF->verify(this); |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 540 | |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 541 | // Verify that LiveIntervals are partitioned into unions and disjoint within |
| 542 | // the unions. |
| 543 | verify(); |
| 544 | } |
| 545 | #endif // !NDEBUG |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 546 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 547 | // Run rewriter |
| 548 | std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter()); |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 549 | rewriter->runOnMachineFunction(*MF, *VRM, LIS); |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 550 | |
| 551 | // The pass output is in VirtRegMap. Release all the transient data. |
| 552 | releaseMemory(); |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 553 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 554 | return true; |
| 555 | } |
| 556 | |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 557 | FunctionPass* llvm::createBasicRegisterAllocator() |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 558 | { |
| 559 | return new RABasic(); |
| 560 | } |