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