Jakob Stoklund Olesen | ccc9581 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 1 | //===-- RegAllocBasic.cpp - Basic Register Allocator ----------------------===// |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 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" |
Jakob Stoklund Olesen | 5f2316a | 2011-06-03 20:34:53 +0000 | [diff] [blame] | 16 | #include "RegAllocBase.h" |
Jakob Stoklund Olesen | cfafc54 | 2011-04-05 21:40:37 +0000 | [diff] [blame] | 17 | #include "LiveDebugVariables.h" |
Jakob Stoklund Olesen | 47dbf6c | 2011-03-10 01:51:42 +0000 | [diff] [blame] | 18 | #include "LiveRangeEdit.h" |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 19 | #include "RenderMachineFunction.h" |
| 20 | #include "Spiller.h" |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 21 | #include "VirtRegMap.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" |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 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 | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Debug.h" |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 38 | #include "llvm/Support/raw_ostream.h" |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 39 | |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 40 | #include <cstdlib> |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 41 | #include <queue> |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 42 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 43 | using namespace llvm; |
| 44 | |
| 45 | static RegisterRegAlloc basicRegAlloc("basic", "basic register allocator", |
| 46 | createBasicRegisterAllocator); |
| 47 | |
Benjamin Kramer | c62feda | 2010-11-25 16:42:51 +0000 | [diff] [blame] | 48 | namespace { |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 49 | struct CompSpillWeight { |
| 50 | bool operator()(LiveInterval *A, LiveInterval *B) const { |
| 51 | return A->weight < B->weight; |
| 52 | } |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | namespace { |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 57 | /// RABasic provides a minimal implementation of the basic register allocation |
| 58 | /// algorithm. It prioritizes live virtual registers by spill weight and spills |
| 59 | /// whenever a register is unavailable. This is not practical in production but |
| 60 | /// provides a useful baseline both for measuring other allocators and comparing |
| 61 | /// the speed of the basic algorithm against other styles of allocators. |
| 62 | class RABasic : public MachineFunctionPass, public RegAllocBase |
| 63 | { |
| 64 | // context |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 65 | MachineFunction *MF; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 66 | |
| 67 | // analyses |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 68 | LiveStacks *LS; |
| 69 | RenderMachineFunction *RMF; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 70 | |
| 71 | // state |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 72 | std::auto_ptr<Spiller> SpillerInstance; |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 73 | std::priority_queue<LiveInterval*, std::vector<LiveInterval*>, |
| 74 | CompSpillWeight> Queue; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 75 | public: |
| 76 | RABasic(); |
| 77 | |
| 78 | /// Return the pass name. |
| 79 | virtual const char* getPassName() const { |
| 80 | return "Basic Register Allocator"; |
| 81 | } |
| 82 | |
| 83 | /// RABasic analysis usage. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 84 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 85 | |
| 86 | virtual void releaseMemory(); |
| 87 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 88 | virtual Spiller &spiller() { return *SpillerInstance; } |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 89 | |
Jakob Stoklund Olesen | d0bec3e | 2010-12-08 22:22:41 +0000 | [diff] [blame] | 90 | virtual float getPriority(LiveInterval *LI) { return LI->weight; } |
| 91 | |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 92 | virtual void enqueue(LiveInterval *LI) { |
| 93 | Queue.push(LI); |
| 94 | } |
| 95 | |
| 96 | virtual LiveInterval *dequeue() { |
| 97 | if (Queue.empty()) |
| 98 | return 0; |
| 99 | LiveInterval *LI = Queue.top(); |
| 100 | Queue.pop(); |
| 101 | return LI; |
| 102 | } |
| 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 | |
Jakob Stoklund Olesen | a8bd9a6 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 110 | // Helper for spilling all live virtual registers currently unified under preg |
| 111 | // that interfere with the most recently queried lvr. Return true if spilling |
| 112 | // was successful, and append any new spilled/split intervals to splitLVRs. |
| 113 | bool spillInterferences(LiveInterval &VirtReg, unsigned PhysReg, |
| 114 | SmallVectorImpl<LiveInterval*> &SplitVRegs); |
| 115 | |
| 116 | void spillReg(LiveInterval &VirtReg, unsigned PhysReg, |
| 117 | SmallVectorImpl<LiveInterval*> &SplitVRegs); |
| 118 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 119 | static char ID; |
| 120 | }; |
| 121 | |
| 122 | char RABasic::ID = 0; |
| 123 | |
| 124 | } // end anonymous namespace |
| 125 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 126 | RABasic::RABasic(): MachineFunctionPass(ID) { |
Jakob Stoklund Olesen | cfafc54 | 2011-04-05 21:40:37 +0000 | [diff] [blame] | 127 | initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry()); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 128 | initializeLiveIntervalsPass(*PassRegistry::getPassRegistry()); |
| 129 | initializeSlotIndexesPass(*PassRegistry::getPassRegistry()); |
| 130 | initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry()); |
Rafael Espindola | 5b22021 | 2011-06-26 22:34:10 +0000 | [diff] [blame] | 131 | initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry()); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 132 | initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry()); |
| 133 | initializeLiveStacksPass(*PassRegistry::getPassRegistry()); |
Jakob Stoklund Olesen | 964bc25 | 2010-11-03 20:39:26 +0000 | [diff] [blame] | 134 | initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry()); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 135 | initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry()); |
| 136 | initializeVirtRegMapPass(*PassRegistry::getPassRegistry()); |
| 137 | initializeRenderMachineFunctionPass(*PassRegistry::getPassRegistry()); |
| 138 | } |
| 139 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 140 | void RABasic::getAnalysisUsage(AnalysisUsage &AU) const { |
| 141 | AU.setPreservesCFG(); |
| 142 | AU.addRequired<AliasAnalysis>(); |
| 143 | AU.addPreserved<AliasAnalysis>(); |
| 144 | AU.addRequired<LiveIntervals>(); |
| 145 | AU.addPreserved<SlotIndexes>(); |
Jakob Stoklund Olesen | cfafc54 | 2011-04-05 21:40:37 +0000 | [diff] [blame] | 146 | AU.addRequired<LiveDebugVariables>(); |
| 147 | AU.addPreserved<LiveDebugVariables>(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 148 | if (StrongPHIElim) |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 149 | AU.addRequiredID(StrongPHIEliminationID); |
Jakob Stoklund Olesen | 2721567 | 2011-08-09 00:29:53 +0000 | [diff] [blame] | 150 | AU.addRequiredTransitiveID(RegisterCoalescerPassID); |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 151 | AU.addRequired<CalculateSpillWeights>(); |
| 152 | AU.addRequired<LiveStacks>(); |
| 153 | AU.addPreserved<LiveStacks>(); |
| 154 | AU.addRequiredID(MachineDominatorsID); |
| 155 | AU.addPreservedID(MachineDominatorsID); |
| 156 | AU.addRequired<MachineLoopInfo>(); |
| 157 | AU.addPreserved<MachineLoopInfo>(); |
| 158 | AU.addRequired<VirtRegMap>(); |
| 159 | AU.addPreserved<VirtRegMap>(); |
| 160 | DEBUG(AU.addRequired<RenderMachineFunction>()); |
| 161 | MachineFunctionPass::getAnalysisUsage(AU); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void RABasic::releaseMemory() { |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 165 | SpillerInstance.reset(0); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 166 | RegAllocBase::releaseMemory(); |
| 167 | } |
| 168 | |
Jakob Stoklund Olesen | a8bd9a6 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 169 | // Helper for spillInterferences() that spills all interfering vregs currently |
| 170 | // assigned to this physical register. |
| 171 | void RABasic::spillReg(LiveInterval& VirtReg, unsigned PhysReg, |
| 172 | SmallVectorImpl<LiveInterval*> &SplitVRegs) { |
| 173 | LiveIntervalUnion::Query &Q = query(VirtReg, PhysReg); |
| 174 | assert(Q.seenAllInterferences() && "need collectInterferences()"); |
| 175 | const SmallVectorImpl<LiveInterval*> &PendingSpills = Q.interferingVRegs(); |
| 176 | |
| 177 | for (SmallVectorImpl<LiveInterval*>::const_iterator I = PendingSpills.begin(), |
| 178 | E = PendingSpills.end(); I != E; ++I) { |
| 179 | LiveInterval &SpilledVReg = **I; |
| 180 | DEBUG(dbgs() << "extracting from " << |
| 181 | TRI->getName(PhysReg) << " " << SpilledVReg << '\n'); |
| 182 | |
| 183 | // Deallocate the interfering vreg by removing it from the union. |
| 184 | // A LiveInterval instance may not be in a union during modification! |
| 185 | unassign(SpilledVReg, PhysReg); |
| 186 | |
| 187 | // Spill the extracted interval. |
| 188 | LiveRangeEdit LRE(SpilledVReg, SplitVRegs, 0, &PendingSpills); |
| 189 | spiller().spill(LRE); |
| 190 | } |
| 191 | // After extracting segments, the query's results are invalid. But keep the |
| 192 | // contents valid until we're done accessing pendingSpills. |
| 193 | Q.clear(); |
| 194 | } |
| 195 | |
| 196 | // Spill or split all live virtual registers currently unified under PhysReg |
| 197 | // that interfere with VirtReg. The newly spilled or split live intervals are |
| 198 | // returned by appending them to SplitVRegs. |
| 199 | bool RABasic::spillInterferences(LiveInterval &VirtReg, unsigned PhysReg, |
| 200 | SmallVectorImpl<LiveInterval*> &SplitVRegs) { |
| 201 | // Record each interference and determine if all are spillable before mutating |
| 202 | // either the union or live intervals. |
| 203 | unsigned NumInterferences = 0; |
| 204 | // Collect interferences assigned to any alias of the physical register. |
| 205 | for (const unsigned *asI = TRI->getOverlaps(PhysReg); *asI; ++asI) { |
| 206 | LiveIntervalUnion::Query &QAlias = query(VirtReg, *asI); |
| 207 | NumInterferences += QAlias.collectInterferingVRegs(); |
| 208 | if (QAlias.seenUnspillableVReg()) { |
| 209 | return false; |
| 210 | } |
| 211 | } |
| 212 | DEBUG(dbgs() << "spilling " << TRI->getName(PhysReg) << |
| 213 | " interferences with " << VirtReg << "\n"); |
| 214 | assert(NumInterferences > 0 && "expect interference"); |
| 215 | |
| 216 | // Spill each interfering vreg allocated to PhysReg or an alias. |
| 217 | for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) |
| 218 | spillReg(VirtReg, *AliasI, SplitVRegs); |
| 219 | return true; |
| 220 | } |
| 221 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 222 | // Driver for the register assignment and splitting heuristics. |
| 223 | // Manages iteration over the LiveIntervalUnions. |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 224 | // |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 225 | // This is a minimal implementation of register assignment and splitting that |
| 226 | // spills whenever we run out of registers. |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 227 | // |
| 228 | // selectOrSplit can only be called once per live virtual register. We then do a |
| 229 | // single interference test for each register the correct class until we find an |
| 230 | // available register. So, the number of interference tests in the worst case is |
| 231 | // |vregs| * |machineregs|. And since the number of interference tests is |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 232 | // minimal, there is no value in caching them outside the scope of |
| 233 | // selectOrSplit(). |
| 234 | unsigned RABasic::selectOrSplit(LiveInterval &VirtReg, |
| 235 | SmallVectorImpl<LiveInterval*> &SplitVRegs) { |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 236 | // Populate a list of physical register spill candidates. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 237 | SmallVector<unsigned, 8> PhysRegSpillCands; |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 238 | |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 239 | // Check for an available register in this class. |
Jakob Stoklund Olesen | 5f2316a | 2011-06-03 20:34:53 +0000 | [diff] [blame] | 240 | ArrayRef<unsigned> Order = |
| 241 | RegClassInfo.getOrder(MRI->getRegClass(VirtReg.reg)); |
| 242 | for (ArrayRef<unsigned>::iterator I = Order.begin(), E = Order.end(); I != E; |
| 243 | ++I) { |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 244 | unsigned PhysReg = *I; |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 245 | |
| 246 | // Check interference and as a side effect, intialize queries for this |
| 247 | // VirtReg and its aliases. |
| 248 | unsigned interfReg = checkPhysRegInterference(VirtReg, PhysReg); |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 249 | if (interfReg == 0) { |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 250 | // Found an available register. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 251 | return PhysReg; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 252 | } |
Jakob Stoklund Olesen | 98985f9 | 2011-08-11 21:00:42 +0000 | [diff] [blame] | 253 | Queries[interfReg].collectInterferingVRegs(1); |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 254 | LiveInterval *interferingVirtReg = |
Jakob Stoklund Olesen | 98985f9 | 2011-08-11 21:00:42 +0000 | [diff] [blame] | 255 | Queries[interfReg].interferingVRegs().front(); |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 256 | |
Andrew Trick | b853e6c | 2010-12-09 18:15:21 +0000 | [diff] [blame] | 257 | // The current VirtReg must either be spillable, or one of its interferences |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 258 | // must have less spill weight. |
| 259 | if (interferingVirtReg->weight < VirtReg.weight ) { |
| 260 | PhysRegSpillCands.push_back(PhysReg); |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 261 | } |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 262 | } |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 263 | // Try to spill another interfering reg with less spill weight. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 264 | for (SmallVectorImpl<unsigned>::iterator PhysRegI = PhysRegSpillCands.begin(), |
| 265 | PhysRegE = PhysRegSpillCands.end(); PhysRegI != PhysRegE; ++PhysRegI) { |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 266 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 267 | if (!spillInterferences(VirtReg, *PhysRegI, SplitVRegs)) continue; |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 268 | |
Jakob Stoklund Olesen | 2b38c51 | 2010-12-07 18:51:27 +0000 | [diff] [blame] | 269 | assert(checkPhysRegInterference(VirtReg, *PhysRegI) == 0 && |
| 270 | "Interference after spill."); |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 271 | // Tell the caller to allocate to this newly freed physical register. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 272 | return *PhysRegI; |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 273 | } |
Jakob Stoklund Olesen | bf4e10f | 2011-05-06 21:58:30 +0000 | [diff] [blame] | 274 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 275 | // No other spill candidates were found, so spill the current VirtReg. |
| 276 | DEBUG(dbgs() << "spilling: " << VirtReg << '\n'); |
Jakob Stoklund Olesen | bf4e10f | 2011-05-06 21:58:30 +0000 | [diff] [blame] | 277 | if (!VirtReg.isSpillable()) |
| 278 | return ~0u; |
Jakob Stoklund Olesen | 47dbf6c | 2011-03-10 01:51:42 +0000 | [diff] [blame] | 279 | LiveRangeEdit LRE(VirtReg, SplitVRegs); |
| 280 | spiller().spill(LRE); |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 281 | |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 282 | // The live virtual register requesting allocation was spilled, so tell |
| 283 | // the caller not to allocate anything during this round. |
| 284 | return 0; |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 285 | } |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 286 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 287 | bool RABasic::runOnMachineFunction(MachineFunction &mf) { |
| 288 | DEBUG(dbgs() << "********** BASIC REGISTER ALLOCATION **********\n" |
| 289 | << "********** Function: " |
| 290 | << ((Value*)mf.getFunction())->getName() << '\n'); |
| 291 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 292 | MF = &mf; |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 293 | DEBUG(RMF = &getAnalysis<RenderMachineFunction>()); |
Andrew Trick | 8a83d54 | 2010-11-11 17:46:29 +0000 | [diff] [blame] | 294 | |
Jakob Stoklund Olesen | 4680dec | 2010-12-10 23:49:00 +0000 | [diff] [blame] | 295 | RegAllocBase::init(getAnalysis<VirtRegMap>(), getAnalysis<LiveIntervals>()); |
Jakob Stoklund Olesen | 8427596 | 2011-03-31 23:02:17 +0000 | [diff] [blame] | 296 | SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM)); |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 297 | |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 298 | allocatePhysRegs(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 299 | |
Jakob Stoklund Olesen | 1b19dc1 | 2010-12-08 01:06:06 +0000 | [diff] [blame] | 300 | addMBBLiveIns(MF); |
Andrew Trick | 316df4b | 2010-11-20 02:57:05 +0000 | [diff] [blame] | 301 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 302 | // Diagnostic output before rewriting |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 303 | DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *VRM << "\n"); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 304 | |
| 305 | // optional HTML output |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 306 | DEBUG(RMF->renderMachineFunction("After basic register allocation.", VRM)); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 307 | |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 308 | // FIXME: Verification currently must run before VirtRegRewriter. We should |
| 309 | // make the rewriter a separate pass and override verifyAnalysis instead. When |
| 310 | // that happens, verification naturally falls under VerifyMachineCode. |
| 311 | #ifndef NDEBUG |
Jakob Stoklund Olesen | af24964 | 2010-12-17 23:16:35 +0000 | [diff] [blame] | 312 | if (VerifyEnabled) { |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 313 | // Verify accuracy of LiveIntervals. The standard machine code verifier |
| 314 | // ensures that each LiveIntervals covers all uses of the virtual reg. |
| 315 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 316 | // FIXME: MachineVerifier is badly broken when using the standard |
| 317 | // spiller. Always use -spiller=inline with -verify-regalloc. Even with the |
| 318 | // inline spiller, some tests fail to verify because the coalescer does not |
| 319 | // always generate verifiable code. |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 320 | MF->verify(this, "In RABasic::verify"); |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 321 | |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 322 | // Verify that LiveIntervals are partitioned into unions and disjoint within |
| 323 | // the unions. |
| 324 | verify(); |
| 325 | } |
| 326 | #endif // !NDEBUG |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 327 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 328 | // Run rewriter |
Jakob Stoklund Olesen | ba05c01 | 2011-02-18 22:03:18 +0000 | [diff] [blame] | 329 | VRM->rewrite(LIS->getSlotIndexes()); |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 330 | |
Jakob Stoklund Olesen | cfafc54 | 2011-04-05 21:40:37 +0000 | [diff] [blame] | 331 | // Write out new DBG_VALUE instructions. |
| 332 | getAnalysis<LiveDebugVariables>().emitDebugValues(VRM); |
| 333 | |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 334 | // The pass output is in VirtRegMap. Release all the transient data. |
| 335 | releaseMemory(); |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 336 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 337 | return true; |
| 338 | } |
| 339 | |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 340 | FunctionPass* llvm::createBasicRegisterAllocator() |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 341 | { |
| 342 | return new RABasic(); |
| 343 | } |