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