| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 1 | //===-- RegAllocBasic.cpp - Basic Register Allocator ----------------------===// | 
| Andrew Trick | 1c24605 | 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 |  | 
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/Passes.h" | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 16 | #include "AllocationOrder.h" | 
| Jakob Stoklund Olesen | 6aa0fbf | 2011-04-05 21:40:37 +0000 | [diff] [blame] | 17 | #include "LiveDebugVariables.h" | 
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "RegAllocBase.h" | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 19 | #include "Spiller.h" | 
| Andrew Trick | f11344d | 2010-11-11 17:46:29 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/AliasAnalysis.h" | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/CalcSpillWeights.h" | 
| Andrew Trick | 3528465 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" | 
| Pete Cooper | 3ca96f9 | 2012-04-02 22:44:18 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/LiveRangeEdit.h" | 
| Jakob Stoklund Olesen | 26c9d70 | 2012-11-28 19:13:06 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/LiveRegMatrix.h" | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/LiveStackAnalysis.h" | 
| Benjamin Kramer | e2a1d89 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFunctionPass.h" | 
|  | 28 | #include "llvm/CodeGen/MachineInstr.h" | 
|  | 29 | #include "llvm/CodeGen/MachineLoopInfo.h" | 
|  | 30 | #include "llvm/CodeGen/MachineRegisterInfo.h" | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/RegAllocRegistry.h" | 
| Jakob Stoklund Olesen | 26c9d70 | 2012-11-28 19:13:06 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/VirtRegMap.h" | 
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 33 | #include "llvm/PassAnalysisSupport.h" | 
|  | 34 | #include "llvm/Support/Debug.h" | 
|  | 35 | #include "llvm/Support/raw_ostream.h" | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetMachine.h" | 
| Andrew Trick | 84aef49 | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetRegisterInfo.h" | 
| Jakob Stoklund Olesen | db357d7 | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 38 | #include <cstdlib> | 
| Jakob Stoklund Olesen | 2329c54 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 39 | #include <queue> | 
| Andrew Trick | 84aef49 | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 40 |  | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 41 | using namespace llvm; | 
|  | 42 |  | 
| Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 43 | #define DEBUG_TYPE "regalloc" | 
|  | 44 |  | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 45 | static RegisterRegAlloc basicRegAlloc("basic", "basic register allocator", | 
|  | 46 | createBasicRegisterAllocator); | 
|  | 47 |  | 
| Benjamin Kramer | aef5bd0 | 2010-11-25 16:42:51 +0000 | [diff] [blame] | 48 | namespace { | 
| Jakob Stoklund Olesen | 2329c54 | 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 | 1c24605 | 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 | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 65 | MachineFunction *MF; | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 66 |  | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 67 | // state | 
| Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 68 | std::unique_ptr<Spiller> SpillerInstance; | 
| Jakob Stoklund Olesen | 2329c54 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 69 | std::priority_queue<LiveInterval*, std::vector<LiveInterval*>, | 
|  | 70 | CompSpillWeight> Queue; | 
| Jakob Stoklund Olesen | 0c1eea2 | 2012-02-08 18:54:35 +0000 | [diff] [blame] | 71 |  | 
|  | 72 | // Scratch space.  Allocated here to avoid repeated malloc calls in | 
|  | 73 | // selectOrSplit(). | 
|  | 74 | BitVector UsableRegs; | 
|  | 75 |  | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 76 | public: | 
|  | 77 | RABasic(); | 
|  | 78 |  | 
|  | 79 | /// Return the pass name. | 
| Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 80 | const char* getPassName() const override { | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 81 | return "Basic Register Allocator"; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | /// RABasic analysis usage. | 
| Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 85 | void getAnalysisUsage(AnalysisUsage &AU) const override; | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 86 |  | 
| Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 87 | void releaseMemory() override; | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 88 |  | 
| Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 89 | Spiller &spiller() override { return *SpillerInstance; } | 
| Andrew Trick | 89eb6a8 | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 90 |  | 
| Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 91 | void enqueue(LiveInterval *LI) override { | 
| Jakob Stoklund Olesen | 2329c54 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 92 | Queue.push(LI); | 
|  | 93 | } | 
|  | 94 |  | 
| Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 95 | LiveInterval *dequeue() override { | 
| Jakob Stoklund Olesen | 2329c54 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 96 | if (Queue.empty()) | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 97 | return nullptr; | 
| Jakob Stoklund Olesen | 2329c54 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 98 | LiveInterval *LI = Queue.top(); | 
|  | 99 | Queue.pop(); | 
|  | 100 | return LI; | 
|  | 101 | } | 
|  | 102 |  | 
| Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 103 | unsigned selectOrSplit(LiveInterval &VirtReg, | 
|  | 104 | SmallVectorImpl<unsigned> &SplitVRegs) override; | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 105 |  | 
|  | 106 | /// Perform register allocation. | 
| Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 107 | bool runOnMachineFunction(MachineFunction &mf) override; | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 108 |  | 
| Jakob Stoklund Olesen | 73edbf1 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 109 | // Helper for spilling all live virtual registers currently unified under preg | 
|  | 110 | // that interfere with the most recently queried lvr.  Return true if spilling | 
|  | 111 | // was successful, and append any new spilled/split intervals to splitLVRs. | 
|  | 112 | bool spillInterferences(LiveInterval &VirtReg, unsigned PhysReg, | 
| Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 113 | SmallVectorImpl<unsigned> &SplitVRegs); | 
| Jakob Stoklund Olesen | 73edbf1 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 114 |  | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 115 | static char ID; | 
|  | 116 | }; | 
|  | 117 |  | 
|  | 118 | char RABasic::ID = 0; | 
|  | 119 |  | 
|  | 120 | } // end anonymous namespace | 
|  | 121 |  | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 122 | RABasic::RABasic(): MachineFunctionPass(ID) { | 
| Jakob Stoklund Olesen | 6aa0fbf | 2011-04-05 21:40:37 +0000 | [diff] [blame] | 123 | initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry()); | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 124 | initializeLiveIntervalsPass(*PassRegistry::getPassRegistry()); | 
|  | 125 | initializeSlotIndexesPass(*PassRegistry::getPassRegistry()); | 
| Rafael Espindola | 676c405 | 2011-06-26 22:34:10 +0000 | [diff] [blame] | 126 | initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry()); | 
| Andrew Trick | e1c034f | 2012-01-17 06:55:03 +0000 | [diff] [blame] | 127 | initializeMachineSchedulerPass(*PassRegistry::getPassRegistry()); | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 128 | initializeLiveStacksPass(*PassRegistry::getPassRegistry()); | 
| Jakob Stoklund Olesen | c913201 | 2010-11-03 20:39:26 +0000 | [diff] [blame] | 129 | initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry()); | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 130 | initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry()); | 
|  | 131 | initializeVirtRegMapPass(*PassRegistry::getPassRegistry()); | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 132 | initializeLiveRegMatrixPass(*PassRegistry::getPassRegistry()); | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 133 | } | 
|  | 134 |  | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 135 | void RABasic::getAnalysisUsage(AnalysisUsage &AU) const { | 
|  | 136 | AU.setPreservesCFG(); | 
|  | 137 | AU.addRequired<AliasAnalysis>(); | 
|  | 138 | AU.addPreserved<AliasAnalysis>(); | 
|  | 139 | AU.addRequired<LiveIntervals>(); | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 140 | AU.addPreserved<LiveIntervals>(); | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 141 | AU.addPreserved<SlotIndexes>(); | 
| Jakob Stoklund Olesen | 6aa0fbf | 2011-04-05 21:40:37 +0000 | [diff] [blame] | 142 | AU.addRequired<LiveDebugVariables>(); | 
|  | 143 | AU.addPreserved<LiveDebugVariables>(); | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 144 | AU.addRequired<LiveStacks>(); | 
|  | 145 | AU.addPreserved<LiveStacks>(); | 
| Benjamin Kramer | e2a1d89 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 146 | AU.addRequired<MachineBlockFrequencyInfo>(); | 
|  | 147 | AU.addPreserved<MachineBlockFrequencyInfo>(); | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 148 | AU.addRequiredID(MachineDominatorsID); | 
|  | 149 | AU.addPreservedID(MachineDominatorsID); | 
|  | 150 | AU.addRequired<MachineLoopInfo>(); | 
|  | 151 | AU.addPreserved<MachineLoopInfo>(); | 
|  | 152 | AU.addRequired<VirtRegMap>(); | 
|  | 153 | AU.addPreserved<VirtRegMap>(); | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 154 | AU.addRequired<LiveRegMatrix>(); | 
|  | 155 | AU.addPreserved<LiveRegMatrix>(); | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 156 | MachineFunctionPass::getAnalysisUsage(AU); | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 157 | } | 
|  | 158 |  | 
|  | 159 | void RABasic::releaseMemory() { | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 160 | SpillerInstance.reset(nullptr); | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 161 | } | 
|  | 162 |  | 
| Jakob Stoklund Olesen | 73edbf1 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 163 |  | 
|  | 164 | // Spill or split all live virtual registers currently unified under PhysReg | 
|  | 165 | // that interfere with VirtReg. The newly spilled or split live intervals are | 
|  | 166 | // returned by appending them to SplitVRegs. | 
|  | 167 | bool RABasic::spillInterferences(LiveInterval &VirtReg, unsigned PhysReg, | 
| Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 168 | SmallVectorImpl<unsigned> &SplitVRegs) { | 
| Jakob Stoklund Olesen | 73edbf1 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 169 | // Record each interference and determine if all are spillable before mutating | 
|  | 170 | // either the union or live intervals. | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 171 | SmallVector<LiveInterval*, 8> Intfs; | 
|  | 172 |  | 
| Jakob Stoklund Olesen | 73edbf1 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 173 | // Collect interferences assigned to any alias of the physical register. | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 174 | for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { | 
|  | 175 | LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units); | 
|  | 176 | Q.collectInterferingVRegs(); | 
|  | 177 | if (Q.seenUnspillableVReg()) | 
| Jakob Stoklund Olesen | 73edbf1 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 178 | return false; | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 179 | for (unsigned i = Q.interferingVRegs().size(); i; --i) { | 
|  | 180 | LiveInterval *Intf = Q.interferingVRegs()[i - 1]; | 
|  | 181 | if (!Intf->isSpillable() || Intf->weight > VirtReg.weight) | 
|  | 182 | return false; | 
|  | 183 | Intfs.push_back(Intf); | 
| Jakob Stoklund Olesen | 73edbf1 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 184 | } | 
|  | 185 | } | 
|  | 186 | DEBUG(dbgs() << "spilling " << TRI->getName(PhysReg) << | 
|  | 187 | " interferences with " << VirtReg << "\n"); | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 188 | assert(!Intfs.empty() && "expected interference"); | 
| Jakob Stoklund Olesen | 73edbf1 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 189 |  | 
|  | 190 | // Spill each interfering vreg allocated to PhysReg or an alias. | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 191 | for (unsigned i = 0, e = Intfs.size(); i != e; ++i) { | 
|  | 192 | LiveInterval &Spill = *Intfs[i]; | 
|  | 193 |  | 
|  | 194 | // Skip duplicates. | 
|  | 195 | if (!VRM->hasPhys(Spill.reg)) | 
|  | 196 | continue; | 
|  | 197 |  | 
|  | 198 | // Deallocate the interfering vreg by removing it from the union. | 
|  | 199 | // A LiveInterval instance may not be in a union during modification! | 
|  | 200 | Matrix->unassign(Spill); | 
|  | 201 |  | 
|  | 202 | // Spill the extracted interval. | 
|  | 203 | LiveRangeEdit LRE(&Spill, SplitVRegs, *MF, *LIS, VRM); | 
|  | 204 | spiller().spill(LRE); | 
|  | 205 | } | 
| Jakob Stoklund Olesen | 73edbf1 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 206 | return true; | 
|  | 207 | } | 
|  | 208 |  | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 209 | // Driver for the register assignment and splitting heuristics. | 
|  | 210 | // Manages iteration over the LiveIntervalUnions. | 
| Andrew Trick | 799ec1c | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 211 | // | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 212 | // This is a minimal implementation of register assignment and splitting that | 
|  | 213 | // spills whenever we run out of registers. | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 214 | // | 
|  | 215 | // selectOrSplit can only be called once per live virtual register. We then do a | 
|  | 216 | // single interference test for each register the correct class until we find an | 
|  | 217 | // available register. So, the number of interference tests in the worst case is | 
|  | 218 | // |vregs| * |machineregs|. And since the number of interference tests is | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 219 | // minimal, there is no value in caching them outside the scope of | 
|  | 220 | // selectOrSplit(). | 
|  | 221 | unsigned RABasic::selectOrSplit(LiveInterval &VirtReg, | 
| Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 222 | SmallVectorImpl<unsigned> &SplitVRegs) { | 
| Andrew Trick | 89eb6a8 | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 223 | // Populate a list of physical register spill candidates. | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 224 | SmallVector<unsigned, 8> PhysRegSpillCands; | 
| Andrew Trick | 3528465 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 225 |  | 
| Andrew Trick | 799ec1c | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 226 | // Check for an available register in this class. | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 227 | AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo); | 
|  | 228 | while (unsigned PhysReg = Order.next()) { | 
|  | 229 | // Check for interference in PhysReg | 
|  | 230 | switch (Matrix->checkInterference(VirtReg, PhysReg)) { | 
|  | 231 | case LiveRegMatrix::IK_Free: | 
|  | 232 | // PhysReg is available, allocate it. | 
|  | 233 | return PhysReg; | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 234 |  | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 235 | case LiveRegMatrix::IK_VirtReg: | 
|  | 236 | // Only virtual registers in the way, we may be able to spill them. | 
|  | 237 | PhysRegSpillCands.push_back(PhysReg); | 
| Jakob Stoklund Olesen | 0c1eea2 | 2012-02-08 18:54:35 +0000 | [diff] [blame] | 238 | continue; | 
|  | 239 |  | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 240 | default: | 
|  | 241 | // RegMask or RegUnit interference. | 
|  | 242 | continue; | 
| Andrew Trick | 3528465 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 243 | } | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 244 | } | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 245 |  | 
| Andrew Trick | 89eb6a8 | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 246 | // Try to spill another interfering reg with less spill weight. | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 247 | for (SmallVectorImpl<unsigned>::iterator PhysRegI = PhysRegSpillCands.begin(), | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 248 | PhysRegE = PhysRegSpillCands.end(); PhysRegI != PhysRegE; ++PhysRegI) { | 
|  | 249 | if (!spillInterferences(VirtReg, *PhysRegI, SplitVRegs)) | 
|  | 250 | continue; | 
| Andrew Trick | 89eb6a8 | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 251 |  | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 252 | assert(!Matrix->checkInterference(VirtReg, *PhysRegI) && | 
| Jakob Stoklund Olesen | fb207c1 | 2010-12-07 18:51:27 +0000 | [diff] [blame] | 253 | "Interference after spill."); | 
| Andrew Trick | 89eb6a8 | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 254 | // Tell the caller to allocate to this newly freed physical register. | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 255 | return *PhysRegI; | 
| Andrew Trick | 3528465 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 256 | } | 
| Jakob Stoklund Olesen | a5c8899 | 2011-05-06 21:58:30 +0000 | [diff] [blame] | 257 |  | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 258 | // No other spill candidates were found, so spill the current VirtReg. | 
|  | 259 | DEBUG(dbgs() << "spilling: " << VirtReg << '\n'); | 
| Jakob Stoklund Olesen | a5c8899 | 2011-05-06 21:58:30 +0000 | [diff] [blame] | 260 | if (!VirtReg.isSpillable()) | 
|  | 261 | return ~0u; | 
| Jakob Stoklund Olesen | e5bbe37 | 2012-05-19 05:25:46 +0000 | [diff] [blame] | 262 | LiveRangeEdit LRE(&VirtReg, SplitVRegs, *MF, *LIS, VRM); | 
| Jakob Stoklund Olesen | 4d6eafa | 2011-03-10 01:51:42 +0000 | [diff] [blame] | 263 | spiller().spill(LRE); | 
| Andrew Trick | 799ec1c | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 264 |  | 
| Andrew Trick | 89eb6a8 | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 265 | // The live virtual register requesting allocation was spilled, so tell | 
|  | 266 | // the caller not to allocate anything during this round. | 
|  | 267 | return 0; | 
| Andrew Trick | 3528465 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 268 | } | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 269 |  | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 270 | bool RABasic::runOnMachineFunction(MachineFunction &mf) { | 
|  | 271 | DEBUG(dbgs() << "********** BASIC REGISTER ALLOCATION **********\n" | 
|  | 272 | << "********** Function: " | 
| David Blaikie | c8c2920 | 2012-08-22 17:18:53 +0000 | [diff] [blame] | 273 | << mf.getName() << '\n'); | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 274 |  | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 275 | MF = &mf; | 
| Jakob Stoklund Olesen | 2d2dec9 | 2012-06-20 22:52:29 +0000 | [diff] [blame] | 276 | RegAllocBase::init(getAnalysis<VirtRegMap>(), | 
|  | 277 | getAnalysis<LiveIntervals>(), | 
|  | 278 | getAnalysis<LiveRegMatrix>()); | 
| Arnaud A. de Grandmaison | 760c1e0 | 2013-11-10 17:46:31 +0000 | [diff] [blame] | 279 |  | 
| Arnaud A. de Grandmaison | ea3ac16 | 2013-11-11 19:04:45 +0000 | [diff] [blame] | 280 | calculateSpillWeightsAndHints(*LIS, *MF, | 
|  | 281 | getAnalysis<MachineLoopInfo>(), | 
|  | 282 | getAnalysis<MachineBlockFrequencyInfo>()); | 
| Arnaud A. de Grandmaison | 760c1e0 | 2013-11-10 17:46:31 +0000 | [diff] [blame] | 283 |  | 
| Jakob Stoklund Olesen | 6e597dc | 2011-03-31 23:02:17 +0000 | [diff] [blame] | 284 | SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM)); | 
| Andrew Trick | 799ec1c | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 285 |  | 
| Andrew Trick | 84aef49 | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 286 | allocatePhysRegs(); | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 287 |  | 
|  | 288 | // Diagnostic output before rewriting | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 289 | DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *VRM << "\n"); | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 290 |  | 
| Andrew Trick | 84aef49 | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 291 | releaseMemory(); | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 292 | return true; | 
|  | 293 | } | 
|  | 294 |  | 
| Andrew Trick | 799ec1c | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 295 | FunctionPass* llvm::createBasicRegisterAllocator() | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 296 | { | 
|  | 297 | return new RABasic(); | 
|  | 298 | } |