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" |
| 22 | #include "llvm/Function.h" |
| 23 | #include "llvm/PassAnalysisSupport.h" |
| 24 | #include "llvm/CodeGen/CalcSpillWeights.h" |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/LiveStackAnalysis.h" |
| 27 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 28 | #include "llvm/CodeGen/MachineInstr.h" |
| 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" |
| 34 | #include "llvm/Target/TargetMachine.h" |
| 35 | #include "llvm/Target/TargetOptions.h" |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetRegisterInfo.h" |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 37 | #ifndef NDEBUG |
| 38 | #include "llvm/ADT/SparseBitVector.h" |
| 39 | #endif |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Debug.h" |
| 41 | #include "llvm/Support/ErrorHandling.h" |
| 42 | #include "llvm/Support/raw_ostream.h" |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 43 | |
| 44 | #include <vector> |
| 45 | #include <queue> |
| 46 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 47 | using namespace llvm; |
| 48 | |
| 49 | static RegisterRegAlloc basicRegAlloc("basic", "basic register allocator", |
| 50 | createBasicRegisterAllocator); |
| 51 | |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 52 | // Temporary verification option until we can put verification inside |
| 53 | // MachineVerifier. |
| 54 | static cl::opt<bool> |
| 55 | VerifyRegAlloc("verify-regalloc", |
| 56 | cl::desc("Verify live intervals before renaming")); |
| 57 | |
| 58 | class PhysicalRegisterDescription : public AbstractRegisterDescription { |
| 59 | const TargetRegisterInfo *tri_; |
| 60 | public: |
| 61 | PhysicalRegisterDescription(const TargetRegisterInfo *tri): tri_(tri) {} |
| 62 | virtual const char *getName(unsigned reg) const { return tri_->getName(reg); } |
| 63 | }; |
| 64 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 65 | namespace { |
| 66 | |
| 67 | /// RABasic provides a minimal implementation of the basic register allocation |
| 68 | /// algorithm. It prioritizes live virtual registers by spill weight and spills |
| 69 | /// whenever a register is unavailable. This is not practical in production but |
| 70 | /// provides a useful baseline both for measuring other allocators and comparing |
| 71 | /// the speed of the basic algorithm against other styles of allocators. |
| 72 | class RABasic : public MachineFunctionPass, public RegAllocBase |
| 73 | { |
| 74 | // context |
| 75 | MachineFunction *mf_; |
| 76 | const TargetMachine *tm_; |
| 77 | MachineRegisterInfo *mri_; |
| 78 | |
| 79 | // analyses |
| 80 | LiveStacks *ls_; |
| 81 | RenderMachineFunction *rmf_; |
| 82 | |
| 83 | // state |
| 84 | std::auto_ptr<Spiller> spiller_; |
| 85 | |
| 86 | public: |
| 87 | RABasic(); |
| 88 | |
| 89 | /// Return the pass name. |
| 90 | virtual const char* getPassName() const { |
| 91 | return "Basic Register Allocator"; |
| 92 | } |
| 93 | |
| 94 | /// RABasic analysis usage. |
| 95 | virtual void getAnalysisUsage(AnalysisUsage &au) const; |
| 96 | |
| 97 | virtual void releaseMemory(); |
| 98 | |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 99 | virtual unsigned selectOrSplit(LiveInterval &lvr, |
| 100 | SmallVectorImpl<LiveInterval*> &splitLVRs); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 101 | |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 102 | void spillInterferences(unsigned preg, |
| 103 | SmallVectorImpl<LiveInterval*> &splitLVRs); |
| 104 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 105 | /// Perform register allocation. |
| 106 | virtual bool runOnMachineFunction(MachineFunction &mf); |
| 107 | |
| 108 | static char ID; |
| 109 | }; |
| 110 | |
| 111 | char RABasic::ID = 0; |
| 112 | |
| 113 | } // end anonymous namespace |
| 114 | |
| 115 | // We should not need to publish the initializer as long as no other passes |
| 116 | // require RABasic. |
| 117 | #if 0 // disable INITIALIZE_PASS |
| 118 | INITIALIZE_PASS_BEGIN(RABasic, "basic-regalloc", |
| 119 | "Basic Register Allocator", false, false) |
| 120 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) |
| 121 | INITIALIZE_PASS_DEPENDENCY(StrongPHIElimination) |
| 122 | INITIALIZE_AG_DEPENDENCY(RegisterCoalescer) |
| 123 | INITIALIZE_PASS_DEPENDENCY(CalculateSpillWeights) |
| 124 | INITIALIZE_PASS_DEPENDENCY(LiveStacks) |
| 125 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) |
| 126 | INITIALIZE_PASS_DEPENDENCY(VirtRegMap) |
| 127 | #ifndef NDEBUG |
| 128 | INITIALIZE_PASS_DEPENDENCY(RenderMachineFunction) |
| 129 | #endif |
| 130 | INITIALIZE_PASS_END(RABasic, "basic-regalloc", |
| 131 | "Basic Register Allocator", false, false) |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 132 | #endif // disable INITIALIZE_PASS |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 133 | |
| 134 | RABasic::RABasic(): MachineFunctionPass(ID) { |
| 135 | initializeLiveIntervalsPass(*PassRegistry::getPassRegistry()); |
| 136 | initializeSlotIndexesPass(*PassRegistry::getPassRegistry()); |
| 137 | initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry()); |
| 138 | initializeRegisterCoalescerAnalysisGroup(*PassRegistry::getPassRegistry()); |
| 139 | initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry()); |
| 140 | initializeLiveStacksPass(*PassRegistry::getPassRegistry()); |
Jakob Stoklund Olesen | 964bc25 | 2010-11-03 20:39:26 +0000 | [diff] [blame] | 141 | initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry()); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 142 | initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry()); |
| 143 | initializeVirtRegMapPass(*PassRegistry::getPassRegistry()); |
| 144 | initializeRenderMachineFunctionPass(*PassRegistry::getPassRegistry()); |
| 145 | } |
| 146 | |
| 147 | void RABasic::getAnalysisUsage(AnalysisUsage &au) const { |
| 148 | au.setPreservesCFG(); |
| 149 | au.addRequired<LiveIntervals>(); |
| 150 | au.addPreserved<SlotIndexes>(); |
| 151 | if (StrongPHIElim) |
| 152 | au.addRequiredID(StrongPHIEliminationID); |
| 153 | au.addRequiredTransitive<RegisterCoalescer>(); |
| 154 | au.addRequired<CalculateSpillWeights>(); |
| 155 | au.addRequired<LiveStacks>(); |
| 156 | au.addPreserved<LiveStacks>(); |
Jakob Stoklund Olesen | 964bc25 | 2010-11-03 20:39:26 +0000 | [diff] [blame] | 157 | au.addRequiredID(MachineDominatorsID); |
| 158 | au.addPreservedID(MachineDominatorsID); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 159 | au.addRequired<MachineLoopInfo>(); |
| 160 | au.addPreserved<MachineLoopInfo>(); |
| 161 | au.addRequired<VirtRegMap>(); |
| 162 | au.addPreserved<VirtRegMap>(); |
| 163 | DEBUG(au.addRequired<RenderMachineFunction>()); |
| 164 | MachineFunctionPass::getAnalysisUsage(au); |
| 165 | } |
| 166 | |
| 167 | void RABasic::releaseMemory() { |
| 168 | spiller_.reset(0); |
| 169 | RegAllocBase::releaseMemory(); |
| 170 | } |
| 171 | |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 172 | #ifndef NDEBUG |
| 173 | // Verify each LiveIntervalUnion. |
| 174 | void RegAllocBase::verify() { |
| 175 | LvrBitSet visitedVRegs; |
| 176 | OwningArrayPtr<LvrBitSet> unionVRegs(new LvrBitSet[physReg2liu_.numRegs()]); |
| 177 | // Verify disjoint unions. |
| 178 | for (unsigned preg = 0; preg < physReg2liu_.numRegs(); ++preg) { |
| 179 | DEBUG(PhysicalRegisterDescription prd(tri_); physReg2liu_[preg].dump(&prd)); |
| 180 | LvrBitSet &vregs = unionVRegs[preg]; |
| 181 | physReg2liu_[preg].verify(vregs); |
| 182 | // Union + intersection test could be done efficiently in one pass, but |
| 183 | // don't add a method to SparseBitVector unless we really need it. |
| 184 | assert(!visitedVRegs.intersects(vregs) && "vreg in multiple unions"); |
| 185 | visitedVRegs |= vregs; |
| 186 | } |
| 187 | // Verify vreg coverage. |
| 188 | for (LiveIntervals::iterator liItr = lis_->begin(), liEnd = lis_->end(); |
| 189 | liItr != liEnd; ++liItr) { |
| 190 | unsigned reg = liItr->first; |
| 191 | LiveInterval &li = *liItr->second; |
| 192 | if (li.empty() ) continue; |
| 193 | if (TargetRegisterInfo::isPhysicalRegister(reg)) continue; |
| 194 | if (!vrm_->hasPhys(reg)) continue; // spilled? |
| 195 | unsigned preg = vrm_->getPhys(reg); |
| 196 | if (!unionVRegs[preg].test(reg)) { |
| 197 | dbgs() << "LiveVirtReg " << reg << " not in union " << |
| 198 | tri_->getName(preg) << "\n"; |
| 199 | llvm_unreachable("unallocated live vreg"); |
| 200 | } |
| 201 | } |
| 202 | // FIXME: I'm not sure how to verify spilled intervals. |
| 203 | } |
| 204 | #endif //!NDEBUG |
| 205 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 206 | //===----------------------------------------------------------------------===// |
| 207 | // RegAllocBase Implementation |
| 208 | //===----------------------------------------------------------------------===// |
| 209 | |
| 210 | // Instantiate a LiveIntervalUnion for each physical register. |
| 211 | void RegAllocBase::LIUArray::init(unsigned nRegs) { |
| 212 | array_.reset(new LiveIntervalUnion[nRegs]); |
| 213 | nRegs_ = nRegs; |
| 214 | for (unsigned pr = 0; pr < nRegs; ++pr) { |
| 215 | array_[pr].init(pr); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void RegAllocBase::init(const TargetRegisterInfo &tri, VirtRegMap &vrm, |
| 220 | LiveIntervals &lis) { |
| 221 | tri_ = &tri; |
| 222 | vrm_ = &vrm; |
| 223 | lis_ = &lis; |
| 224 | physReg2liu_.init(tri_->getNumRegs()); |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 225 | // Cache an interferece query for each physical reg |
| 226 | queries_.reset(new LiveIntervalUnion::Query[physReg2liu_.numRegs()]); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | void RegAllocBase::LIUArray::clear() { |
| 230 | nRegs_ = 0; |
| 231 | array_.reset(0); |
| 232 | } |
| 233 | |
| 234 | void RegAllocBase::releaseMemory() { |
| 235 | physReg2liu_.clear(); |
| 236 | } |
| 237 | |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 238 | namespace llvm { |
| 239 | /// This class defines a queue of live virtual registers prioritized by spill |
| 240 | /// weight. The heaviest vreg is popped first. |
| 241 | /// |
| 242 | /// Currently, this is trivial wrapper that gives us an opaque type in the |
| 243 | /// header, but we may later give it a virtual interface for register allocators |
| 244 | /// to override the priority queue comparator. |
| 245 | class LiveVirtRegQueue { |
| 246 | typedef std::priority_queue |
| 247 | <LiveInterval*, std::vector<LiveInterval*>, LessSpillWeightPriority> PQ; |
| 248 | PQ pq_; |
| 249 | |
| 250 | public: |
| 251 | // Is the queue empty? |
| 252 | bool empty() { return pq_.empty(); } |
| 253 | |
| 254 | // Get the highest priority lvr (top + pop) |
| 255 | LiveInterval *get() { |
| 256 | LiveInterval *lvr = pq_.top(); |
| 257 | pq_.pop(); |
| 258 | return lvr; |
| 259 | } |
| 260 | // Add this lvr to the queue |
| 261 | void push(LiveInterval *lvr) { |
| 262 | pq_.push(lvr); |
| 263 | } |
| 264 | }; |
| 265 | } // end namespace llvm |
| 266 | |
| 267 | // Visit all the live virtual registers. If they are already assigned to a |
| 268 | // physical register, unify them with the corresponding LiveIntervalUnion, |
| 269 | // otherwise push them on the priority queue for later assignment. |
| 270 | void RegAllocBase::seedLiveVirtRegs(LiveVirtRegQueue &lvrQ) { |
| 271 | for (LiveIntervals::iterator liItr = lis_->begin(), liEnd = lis_->end(); |
| 272 | liItr != liEnd; ++liItr) { |
| 273 | unsigned reg = liItr->first; |
| 274 | LiveInterval &li = *liItr->second; |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 275 | if (li.empty()) continue; |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 276 | if (TargetRegisterInfo::isPhysicalRegister(reg)) { |
| 277 | physReg2liu_[reg].unify(li); |
| 278 | } |
| 279 | else { |
| 280 | lvrQ.push(&li); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // Top-level driver to manage the queue of unassigned LiveVirtRegs and call the |
| 286 | // selectOrSplit implementation. |
| 287 | void RegAllocBase::allocatePhysRegs() { |
| 288 | LiveVirtRegQueue lvrQ; |
| 289 | seedLiveVirtRegs(lvrQ); |
| 290 | while (!lvrQ.empty()) { |
| 291 | LiveInterval *lvr = lvrQ.get(); |
| 292 | typedef SmallVector<LiveInterval*, 4> LVRVec; |
| 293 | LVRVec splitLVRs; |
| 294 | unsigned availablePhysReg = selectOrSplit(*lvr, splitLVRs); |
| 295 | if (availablePhysReg) { |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 296 | DEBUG(dbgs() << "allocating: " << tri_->getName(availablePhysReg) << |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 297 | " " << *lvr << '\n'); |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 298 | assert(!vrm_->hasPhys(lvr->reg) && "duplicate vreg in interval unions"); |
| 299 | vrm_->assignVirt2Phys(lvr->reg, availablePhysReg); |
| 300 | physReg2liu_[availablePhysReg].unify(*lvr); |
| 301 | } |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 302 | for (LVRVec::iterator lvrI = splitLVRs.begin(), lvrEnd = splitLVRs.end(); |
| 303 | lvrI != lvrEnd; ++lvrI) { |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 304 | if ((*lvrI)->empty()) continue; |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 305 | DEBUG(dbgs() << "queuing new interval: " << **lvrI << "\n"); |
| 306 | assert(TargetRegisterInfo::isVirtualRegister((*lvrI)->reg) && |
| 307 | "expect split value in virtual register"); |
| 308 | lvrQ.push(*lvrI); |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 313 | // Check if this live virtual reg interferes with a physical register. If not, |
| 314 | // then check for interference on each register that aliases with the physical |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 315 | // register. Return the interfering register. |
| 316 | unsigned RegAllocBase::checkPhysRegInterference(LiveInterval &lvr, |
| 317 | unsigned preg) { |
| 318 | queries_[preg].init(&lvr, &physReg2liu_[preg]); |
| 319 | if (queries_[preg].checkInterference()) |
| 320 | return preg; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 321 | for (const unsigned *asI = tri_->getAliasSet(preg); *asI; ++asI) { |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 322 | queries_[*asI].init(&lvr, &physReg2liu_[*asI]); |
| 323 | if (queries_[*asI].checkInterference()) |
| 324 | return *asI; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 325 | } |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 326 | return 0; |
| 327 | } |
| 328 | |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 329 | // Spill or split all live virtual registers currently unified under preg that |
| 330 | // interfere with lvr. The newly spilled or split live intervals are returned by |
| 331 | // appending them to splitLVRs. |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 332 | void RABasic::spillInterferences(unsigned preg, |
| 333 | SmallVectorImpl<LiveInterval*> &splitLVRs) { |
| 334 | SmallPtrSet<LiveInterval*, 8> spilledLVRs; |
| 335 | LiveIntervalUnion::Query &query = queries_[preg]; |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 336 | // Record each interference before mutating either the union or live |
| 337 | // intervals. |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 338 | LiveIntervalUnion::InterferenceResult ir = query.firstInterference(); |
| 339 | assert(query.isInterference(ir) && "expect interference"); |
| 340 | do { |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 341 | spilledLVRs.insert(ir.liuSegPos()->liveVirtReg); |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 342 | } while (query.nextInterference(ir)); |
| 343 | for (SmallPtrSetIterator<LiveInterval*> lvrI = spilledLVRs.begin(), |
| 344 | lvrEnd = spilledLVRs.end(); |
| 345 | lvrI != lvrEnd; ++lvrI ) { |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 346 | LiveInterval& lvr = **lvrI; |
| 347 | // Spill the previously allocated lvr. |
| 348 | DEBUG(dbgs() << "extracting from " << preg << " " << lvr << '\n'); |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 349 | // Deallocate the interfering lvr by removing it from the preg union. |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 350 | // Live intervals may not be in a union during modification. |
| 351 | physReg2liu_[preg].extract(lvr); |
| 352 | // Spill the extracted interval. |
| 353 | SmallVector<LiveInterval*, 8> spillIs; |
| 354 | spiller_->spill(&lvr, splitLVRs, spillIs); |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 355 | } |
| 356 | // After extracting segments, the query's results are invalid. |
| 357 | query.clear(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | //===----------------------------------------------------------------------===// |
| 361 | // RABasic Implementation |
| 362 | //===----------------------------------------------------------------------===// |
| 363 | |
| 364 | // Driver for the register assignment and splitting heuristics. |
| 365 | // Manages iteration over the LiveIntervalUnions. |
| 366 | // |
| 367 | // Minimal implementation of register assignment and splitting--spills whenever |
| 368 | // we run out of registers. |
| 369 | // |
| 370 | // selectOrSplit can only be called once per live virtual register. We then do a |
| 371 | // single interference test for each register the correct class until we find an |
| 372 | // available register. So, the number of interference tests in the worst case is |
| 373 | // |vregs| * |machineregs|. And since the number of interference tests is |
| 374 | // minimal, there is no value in caching them. |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 375 | unsigned RABasic::selectOrSplit(LiveInterval &lvr, |
| 376 | SmallVectorImpl<LiveInterval*> &splitLVRs) { |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 377 | // Accumulate the min spill cost among the interferences, in case we spill. |
| 378 | unsigned minSpillReg = 0; |
| 379 | unsigned minSpillAlias = 0; |
| 380 | float minSpillWeight = lvr.weight; |
| 381 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 382 | // Check for an available reg in this class. |
| 383 | const TargetRegisterClass *trc = mri_->getRegClass(lvr.reg); |
| 384 | for (TargetRegisterClass::iterator trcI = trc->allocation_order_begin(*mf_), |
| 385 | trcEnd = trc->allocation_order_end(*mf_); |
| 386 | trcI != trcEnd; ++trcI) { |
| 387 | unsigned preg = *trcI; |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 388 | unsigned interfReg = checkPhysRegInterference(lvr, preg); |
| 389 | if (interfReg == 0) { |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 390 | return preg; |
| 391 | } |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 392 | LiveIntervalUnion::InterferenceResult interf = |
| 393 | queries_[interfReg].firstInterference(); |
| 394 | float interfWeight = interf.liuSegPos()->liveVirtReg->weight; |
| 395 | if (interfWeight < minSpillWeight ) { |
| 396 | minSpillReg = interfReg; |
| 397 | minSpillAlias = preg; |
| 398 | minSpillWeight = interfWeight; |
| 399 | } |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 400 | } |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 401 | if (minSpillReg == 0) { |
| 402 | DEBUG(dbgs() << "spilling: " << lvr << '\n'); |
| 403 | SmallVector<LiveInterval*, 1> spillIs; // ignored |
| 404 | spiller_->spill(&lvr, splitLVRs, spillIs); |
| 405 | // The live virtual register requesting to be allocated was spilled. So tell |
| 406 | // the caller not to allocate anything for this round. |
| 407 | return 0; |
| 408 | } |
| 409 | // Free the cheapest physical register. |
| 410 | spillInterferences(minSpillReg, splitLVRs); |
| 411 | // Tell the caller to allocate to this newly freed physical register. |
| 412 | assert(minSpillAlias != 0 && "need a free register after spilling"); |
| 413 | // We just spilled the first register that interferes with minSpillAlias. We |
| 414 | // now assume minSpillAlias is free because only one register alias may |
| 415 | // interfere at a time. e.g. we ignore predication. |
| 416 | unsigned interfReg = checkPhysRegInterference(lvr, minSpillAlias); |
| 417 | if (interfReg != 0) { |
| 418 | dbgs() << "spilling cannot free " << tri_->getName(minSpillAlias) << |
| 419 | " for " << lvr.reg << " with interference " << |
| 420 | *queries_[interfReg].firstInterference().liuSegPos()->liveVirtReg << "\n"; |
| 421 | llvm_unreachable("Interference after spill."); |
| 422 | } |
| 423 | return minSpillAlias; |
| 424 | } |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 425 | |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 426 | namespace llvm { |
| 427 | Spiller *createInlineSpiller(MachineFunctionPass &pass, |
| 428 | MachineFunction &mf, |
| 429 | VirtRegMap &vrm); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | bool RABasic::runOnMachineFunction(MachineFunction &mf) { |
| 433 | DEBUG(dbgs() << "********** BASIC REGISTER ALLOCATION **********\n" |
| 434 | << "********** Function: " |
| 435 | << ((Value*)mf.getFunction())->getName() << '\n'); |
| 436 | |
| 437 | mf_ = &mf; |
| 438 | tm_ = &mf.getTarget(); |
| 439 | mri_ = &mf.getRegInfo(); |
| 440 | |
| 441 | DEBUG(rmf_ = &getAnalysis<RenderMachineFunction>()); |
| 442 | |
| 443 | RegAllocBase::init(*tm_->getRegisterInfo(), getAnalysis<VirtRegMap>(), |
| 444 | getAnalysis<LiveIntervals>()); |
| 445 | |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 446 | // We may want to force InlineSpiller for this register allocator. For |
| 447 | // now we're also experimenting with the standard spiller. |
| 448 | // |
| 449 | //spiller_.reset(createInlineSpiller(*this, *mf_, *vrm_)); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 450 | spiller_.reset(createSpiller(*this, *mf_, *vrm_)); |
| 451 | |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 452 | allocatePhysRegs(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 453 | |
| 454 | // Diagnostic output before rewriting |
| 455 | DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *vrm_ << "\n"); |
| 456 | |
| 457 | // optional HTML output |
| 458 | DEBUG(rmf_->renderMachineFunction("After basic register allocation.", vrm_)); |
| 459 | |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 460 | // FIXME: Verification currently must run before VirtRegRewriter. We should |
| 461 | // make the rewriter a separate pass and override verifyAnalysis instead. When |
| 462 | // that happens, verification naturally falls under VerifyMachineCode. |
| 463 | #ifndef NDEBUG |
| 464 | if (VerifyRegAlloc) { |
| 465 | // Verify accuracy of LiveIntervals. The standard machine code verifier |
| 466 | // ensures that each LiveIntervals covers all uses of the virtual reg. |
| 467 | |
| 468 | // FIXME: MachineVerifier is currently broken when using the standard |
| 469 | // spiller. Enable it for InlineSpiller only. |
| 470 | // mf_->verify(this); |
| 471 | |
| 472 | // Verify that LiveIntervals are partitioned into unions and disjoint within |
| 473 | // the unions. |
| 474 | verify(); |
| 475 | } |
| 476 | #endif // !NDEBUG |
| 477 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 478 | // Run rewriter |
| 479 | std::auto_ptr<VirtRegRewriter> rewriter(createVirtRegRewriter()); |
| 480 | rewriter->runOnMachineFunction(*mf_, *vrm_, lis_); |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 481 | |
| 482 | // The pass output is in VirtRegMap. Release all the transient data. |
| 483 | releaseMemory(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 484 | |
| 485 | return true; |
| 486 | } |
| 487 | |
| 488 | FunctionPass* llvm::createBasicRegisterAllocator() |
| 489 | { |
| 490 | return new RABasic(); |
| 491 | } |