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" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/Passes.h" |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 17 | #include "AllocationOrder.h" |
Jakob Stoklund Olesen | cfafc54 | 2011-04-05 21:40:37 +0000 | [diff] [blame] | 18 | #include "LiveDebugVariables.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "RegAllocBase.h" |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 20 | #include "Spiller.h" |
Andrew Trick | 8a83d54 | 2010-11-11 17:46:29 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/AliasAnalysis.h" |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/CalcSpillWeights.h" |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Pete Cooper | 789d5d8 | 2012-04-02 22:44:18 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/LiveRangeEdit.h" |
Jakob Stoklund Olesen | 1ead68d | 2012-11-28 19:13:06 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/LiveRegMatrix.h" |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/LiveStackAnalysis.h" |
Benjamin Kramer | 4eed756 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 29 | #include "llvm/CodeGen/MachineInstr.h" |
| 30 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 31 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/RegAllocRegistry.h" |
Jakob Stoklund Olesen | 1ead68d | 2012-11-28 19:13:06 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/VirtRegMap.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 34 | #include "llvm/PassAnalysisSupport.h" |
| 35 | #include "llvm/Support/Debug.h" |
| 36 | #include "llvm/Support/raw_ostream.h" |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetMachine.h" |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetRegisterInfo.h" |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 39 | #include <cstdlib> |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 40 | #include <queue> |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 41 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 42 | using namespace llvm; |
| 43 | |
| 44 | static RegisterRegAlloc basicRegAlloc("basic", "basic register allocator", |
| 45 | createBasicRegisterAllocator); |
| 46 | |
Benjamin Kramer | c62feda | 2010-11-25 16:42:51 +0000 | [diff] [blame] | 47 | namespace { |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 48 | struct CompSpillWeight { |
| 49 | bool operator()(LiveInterval *A, LiveInterval *B) const { |
| 50 | return A->weight < B->weight; |
| 51 | } |
| 52 | }; |
| 53 | } |
| 54 | |
| 55 | namespace { |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 56 | /// RABasic provides a minimal implementation of the basic register allocation |
| 57 | /// algorithm. It prioritizes live virtual registers by spill weight and spills |
| 58 | /// whenever a register is unavailable. This is not practical in production but |
| 59 | /// provides a useful baseline both for measuring other allocators and comparing |
| 60 | /// the speed of the basic algorithm against other styles of allocators. |
| 61 | class RABasic : public MachineFunctionPass, public RegAllocBase |
| 62 | { |
| 63 | // context |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 64 | MachineFunction *MF; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 65 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 66 | // state |
Andy Gibbs | 200241e | 2013-04-12 10:56:28 +0000 | [diff] [blame] | 67 | OwningPtr<Spiller> SpillerInstance; |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 68 | std::priority_queue<LiveInterval*, std::vector<LiveInterval*>, |
| 69 | CompSpillWeight> Queue; |
Jakob Stoklund Olesen | a94e635 | 2012-02-08 18:54:35 +0000 | [diff] [blame] | 70 | |
| 71 | // Scratch space. Allocated here to avoid repeated malloc calls in |
| 72 | // selectOrSplit(). |
| 73 | BitVector UsableRegs; |
| 74 | |
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, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 105 | SmallVectorImpl<unsigned> &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, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 114 | SmallVectorImpl<unsigned> &SplitVRegs); |
Jakob Stoklund Olesen | a8bd9a6 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 115 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 116 | static char ID; |
| 117 | }; |
| 118 | |
| 119 | char RABasic::ID = 0; |
| 120 | |
| 121 | } // end anonymous namespace |
| 122 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 123 | RABasic::RABasic(): MachineFunctionPass(ID) { |
Jakob Stoklund Olesen | cfafc54 | 2011-04-05 21:40:37 +0000 | [diff] [blame] | 124 | initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry()); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 125 | initializeLiveIntervalsPass(*PassRegistry::getPassRegistry()); |
| 126 | initializeSlotIndexesPass(*PassRegistry::getPassRegistry()); |
Rafael Espindola | 5b22021 | 2011-06-26 22:34:10 +0000 | [diff] [blame] | 127 | initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry()); |
Andrew Trick | 42b7a71 | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 128 | initializeMachineSchedulerPass(*PassRegistry::getPassRegistry()); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 129 | initializeLiveStacksPass(*PassRegistry::getPassRegistry()); |
Jakob Stoklund Olesen | 964bc25 | 2010-11-03 20:39:26 +0000 | [diff] [blame] | 130 | initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry()); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 131 | initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry()); |
| 132 | initializeVirtRegMapPass(*PassRegistry::getPassRegistry()); |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 133 | initializeLiveRegMatrixPass(*PassRegistry::getPassRegistry()); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 136 | void RABasic::getAnalysisUsage(AnalysisUsage &AU) const { |
| 137 | AU.setPreservesCFG(); |
| 138 | AU.addRequired<AliasAnalysis>(); |
| 139 | AU.addPreserved<AliasAnalysis>(); |
| 140 | AU.addRequired<LiveIntervals>(); |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 141 | AU.addPreserved<LiveIntervals>(); |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 142 | AU.addPreserved<SlotIndexes>(); |
Jakob Stoklund Olesen | cfafc54 | 2011-04-05 21:40:37 +0000 | [diff] [blame] | 143 | AU.addRequired<LiveDebugVariables>(); |
| 144 | AU.addPreserved<LiveDebugVariables>(); |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 145 | AU.addRequired<LiveStacks>(); |
| 146 | AU.addPreserved<LiveStacks>(); |
Benjamin Kramer | 4eed756 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 147 | AU.addRequired<MachineBlockFrequencyInfo>(); |
| 148 | AU.addPreserved<MachineBlockFrequencyInfo>(); |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 149 | AU.addRequiredID(MachineDominatorsID); |
| 150 | AU.addPreservedID(MachineDominatorsID); |
| 151 | AU.addRequired<MachineLoopInfo>(); |
| 152 | AU.addPreserved<MachineLoopInfo>(); |
| 153 | AU.addRequired<VirtRegMap>(); |
| 154 | AU.addPreserved<VirtRegMap>(); |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 155 | AU.addRequired<LiveRegMatrix>(); |
| 156 | AU.addPreserved<LiveRegMatrix>(); |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 157 | MachineFunctionPass::getAnalysisUsage(AU); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void RABasic::releaseMemory() { |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 161 | SpillerInstance.reset(0); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Jakob Stoklund Olesen | a8bd9a6 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 164 | |
| 165 | // Spill or split all live virtual registers currently unified under PhysReg |
| 166 | // that interfere with VirtReg. The newly spilled or split live intervals are |
| 167 | // returned by appending them to SplitVRegs. |
| 168 | bool RABasic::spillInterferences(LiveInterval &VirtReg, unsigned PhysReg, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 169 | SmallVectorImpl<unsigned> &SplitVRegs) { |
Jakob Stoklund Olesen | a8bd9a6 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 170 | // Record each interference and determine if all are spillable before mutating |
| 171 | // either the union or live intervals. |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 172 | SmallVector<LiveInterval*, 8> Intfs; |
| 173 | |
Jakob Stoklund Olesen | a8bd9a6 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 174 | // Collect interferences assigned to any alias of the physical register. |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 175 | for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { |
| 176 | LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units); |
| 177 | Q.collectInterferingVRegs(); |
| 178 | if (Q.seenUnspillableVReg()) |
Jakob Stoklund Olesen | a8bd9a6 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 179 | return false; |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 180 | for (unsigned i = Q.interferingVRegs().size(); i; --i) { |
| 181 | LiveInterval *Intf = Q.interferingVRegs()[i - 1]; |
| 182 | if (!Intf->isSpillable() || Intf->weight > VirtReg.weight) |
| 183 | return false; |
| 184 | Intfs.push_back(Intf); |
Jakob Stoklund Olesen | a8bd9a6 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | DEBUG(dbgs() << "spilling " << TRI->getName(PhysReg) << |
| 188 | " interferences with " << VirtReg << "\n"); |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 189 | assert(!Intfs.empty() && "expected interference"); |
Jakob Stoklund Olesen | a8bd9a6 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 190 | |
| 191 | // Spill each interfering vreg allocated to PhysReg or an alias. |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 192 | for (unsigned i = 0, e = Intfs.size(); i != e; ++i) { |
| 193 | LiveInterval &Spill = *Intfs[i]; |
| 194 | |
| 195 | // Skip duplicates. |
| 196 | if (!VRM->hasPhys(Spill.reg)) |
| 197 | continue; |
| 198 | |
| 199 | // Deallocate the interfering vreg by removing it from the union. |
| 200 | // A LiveInterval instance may not be in a union during modification! |
| 201 | Matrix->unassign(Spill); |
| 202 | |
| 203 | // Spill the extracted interval. |
| 204 | LiveRangeEdit LRE(&Spill, SplitVRegs, *MF, *LIS, VRM); |
| 205 | spiller().spill(LRE); |
| 206 | } |
Jakob Stoklund Olesen | a8bd9a6 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 207 | return true; |
| 208 | } |
| 209 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 210 | // Driver for the register assignment and splitting heuristics. |
| 211 | // Manages iteration over the LiveIntervalUnions. |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 212 | // |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 213 | // This is a minimal implementation of register assignment and splitting that |
| 214 | // spills whenever we run out of registers. |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 215 | // |
| 216 | // selectOrSplit can only be called once per live virtual register. We then do a |
| 217 | // single interference test for each register the correct class until we find an |
| 218 | // available register. So, the number of interference tests in the worst case is |
| 219 | // |vregs| * |machineregs|. And since the number of interference tests is |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 220 | // minimal, there is no value in caching them outside the scope of |
| 221 | // selectOrSplit(). |
| 222 | unsigned RABasic::selectOrSplit(LiveInterval &VirtReg, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 223 | SmallVectorImpl<unsigned> &SplitVRegs) { |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 224 | // Populate a list of physical register spill candidates. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 225 | SmallVector<unsigned, 8> PhysRegSpillCands; |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 226 | |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 227 | // Check for an available register in this class. |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 228 | AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo); |
| 229 | while (unsigned PhysReg = Order.next()) { |
| 230 | // Check for interference in PhysReg |
| 231 | switch (Matrix->checkInterference(VirtReg, PhysReg)) { |
| 232 | case LiveRegMatrix::IK_Free: |
| 233 | // PhysReg is available, allocate it. |
| 234 | return PhysReg; |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 235 | |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 236 | case LiveRegMatrix::IK_VirtReg: |
| 237 | // Only virtual registers in the way, we may be able to spill them. |
| 238 | PhysRegSpillCands.push_back(PhysReg); |
Jakob Stoklund Olesen | a94e635 | 2012-02-08 18:54:35 +0000 | [diff] [blame] | 239 | continue; |
| 240 | |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 241 | default: |
| 242 | // RegMask or RegUnit interference. |
| 243 | continue; |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 244 | } |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 245 | } |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 246 | |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 247 | // Try to spill another interfering reg with less spill weight. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 248 | for (SmallVectorImpl<unsigned>::iterator PhysRegI = PhysRegSpillCands.begin(), |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 249 | PhysRegE = PhysRegSpillCands.end(); PhysRegI != PhysRegE; ++PhysRegI) { |
| 250 | if (!spillInterferences(VirtReg, *PhysRegI, SplitVRegs)) |
| 251 | continue; |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 252 | |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 253 | assert(!Matrix->checkInterference(VirtReg, *PhysRegI) && |
Jakob Stoklund Olesen | 2b38c51 | 2010-12-07 18:51:27 +0000 | [diff] [blame] | 254 | "Interference after spill."); |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 255 | // Tell the caller to allocate to this newly freed physical register. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 256 | return *PhysRegI; |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 257 | } |
Jakob Stoklund Olesen | bf4e10f | 2011-05-06 21:58:30 +0000 | [diff] [blame] | 258 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 259 | // No other spill candidates were found, so spill the current VirtReg. |
| 260 | DEBUG(dbgs() << "spilling: " << VirtReg << '\n'); |
Jakob Stoklund Olesen | bf4e10f | 2011-05-06 21:58:30 +0000 | [diff] [blame] | 261 | if (!VirtReg.isSpillable()) |
| 262 | return ~0u; |
Jakob Stoklund Olesen | 20942dc | 2012-05-19 05:25:46 +0000 | [diff] [blame] | 263 | LiveRangeEdit LRE(&VirtReg, SplitVRegs, *MF, *LIS, VRM); |
Jakob Stoklund Olesen | 47dbf6c | 2011-03-10 01:51:42 +0000 | [diff] [blame] | 264 | spiller().spill(LRE); |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 265 | |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 266 | // The live virtual register requesting allocation was spilled, so tell |
| 267 | // the caller not to allocate anything during this round. |
| 268 | return 0; |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 269 | } |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 270 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 271 | bool RABasic::runOnMachineFunction(MachineFunction &mf) { |
| 272 | DEBUG(dbgs() << "********** BASIC REGISTER ALLOCATION **********\n" |
| 273 | << "********** Function: " |
David Blaikie | 986d76d | 2012-08-22 17:18:53 +0000 | [diff] [blame] | 274 | << mf.getName() << '\n'); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 275 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 276 | MF = &mf; |
Jakob Stoklund Olesen | d4348a2 | 2012-06-20 22:52:29 +0000 | [diff] [blame] | 277 | RegAllocBase::init(getAnalysis<VirtRegMap>(), |
| 278 | getAnalysis<LiveIntervals>(), |
| 279 | getAnalysis<LiveRegMatrix>()); |
Arnaud A. de Grandmaison | a77da05 | 2013-11-10 17:46:31 +0000 | [diff] [blame] | 280 | |
| 281 | calculateSpillWeights(*LIS, *MF, |
| 282 | getAnalysis<MachineLoopInfo>(), |
| 283 | getAnalysis<MachineBlockFrequencyInfo>()); |
| 284 | |
Jakob Stoklund Olesen | 8427596 | 2011-03-31 23:02:17 +0000 | [diff] [blame] | 285 | SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM)); |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 286 | |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 287 | allocatePhysRegs(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 288 | |
| 289 | // Diagnostic output before rewriting |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 290 | DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *VRM << "\n"); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 291 | |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 292 | releaseMemory(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 293 | return true; |
| 294 | } |
| 295 | |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 296 | FunctionPass* llvm::createBasicRegisterAllocator() |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 297 | { |
| 298 | return new RABasic(); |
| 299 | } |