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 | |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 15 | #include "AllocationOrder.h" |
Jakob Stoklund Olesen | cfafc54 | 2011-04-05 21:40:37 +0000 | [diff] [blame] | 16 | #include "LiveDebugVariables.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "RegAllocBase.h" |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 18 | #include "Spiller.h" |
Andrew Trick | 8a83d54 | 2010-11-11 17:46:29 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/AliasAnalysis.h" |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/CalcSpillWeights.h" |
Matthias Braun | fa621d2 | 2017-12-13 02:51:04 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/LiveIntervals.h" |
Pete Cooper | 789d5d8 | 2012-04-02 22:44:18 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/LiveRangeEdit.h" |
Jakob Stoklund Olesen | 1ead68d | 2012-11-28 19:13:06 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/LiveRegMatrix.h" |
Matthias Braun | 209f048 | 2017-12-18 23:19:44 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/LiveStacks.h" |
Benjamin Kramer | 4eed756 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 27 | #include "llvm/CodeGen/MachineInstr.h" |
| 28 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 29 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/Passes.h" |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/RegAllocRegistry.h" |
David Blaikie | e3a9b4c | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/TargetRegisterInfo.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" |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 37 | #include <cstdlib> |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 38 | #include <queue> |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 39 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 40 | using namespace llvm; |
| 41 | |
Chandler Carruth | 8677f2f | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 42 | #define DEBUG_TYPE "regalloc" |
| 43 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 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. |
Quentin Colombet | 1067c96 | 2017-06-02 22:46:31 +0000 | [diff] [blame] | 61 | class RABasic : public MachineFunctionPass, |
| 62 | public RegAllocBase, |
| 63 | private LiveRangeEdit::Delegate { |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 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 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 67 | // state |
Ahmed Charles | f4ccd11 | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 68 | std::unique_ptr<Spiller> SpillerInstance; |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 69 | std::priority_queue<LiveInterval*, std::vector<LiveInterval*>, |
| 70 | CompSpillWeight> Queue; |
Jakob Stoklund Olesen | a94e635 | 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 | |
Quentin Colombet | 1067c96 | 2017-06-02 22:46:31 +0000 | [diff] [blame] | 76 | bool LRE_CanEraseVirtReg(unsigned) override; |
| 77 | void LRE_WillShrinkVirtReg(unsigned) override; |
| 78 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 79 | public: |
| 80 | RABasic(); |
| 81 | |
| 82 | /// Return the pass name. |
Mehdi Amini | 67f335d | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 83 | StringRef getPassName() const override { return "Basic Register Allocator"; } |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 84 | |
| 85 | /// RABasic analysis usage. |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 86 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 87 | |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 88 | void releaseMemory() override; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 89 | |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 90 | Spiller &spiller() override { return *SpillerInstance; } |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 91 | |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 92 | void enqueue(LiveInterval *LI) override { |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 93 | Queue.push(LI); |
| 94 | } |
| 95 | |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 96 | LiveInterval *dequeue() override { |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 97 | if (Queue.empty()) |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 98 | return nullptr; |
Jakob Stoklund Olesen | 98d9648 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 99 | LiveInterval *LI = Queue.top(); |
| 100 | Queue.pop(); |
| 101 | return LI; |
| 102 | } |
| 103 | |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 104 | unsigned selectOrSplit(LiveInterval &VirtReg, |
| 105 | SmallVectorImpl<unsigned> &SplitVRegs) override; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 106 | |
| 107 | /// Perform register allocation. |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 108 | bool runOnMachineFunction(MachineFunction &mf) override; |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 109 | |
Matthias Braun | db9ce2f | 2016-08-23 21:19:49 +0000 | [diff] [blame] | 110 | MachineFunctionProperties getRequiredProperties() const override { |
| 111 | return MachineFunctionProperties().set( |
| 112 | MachineFunctionProperties::Property::NoPHIs); |
| 113 | } |
| 114 | |
Jakob Stoklund Olesen | a8bd9a6 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 115 | // Helper for spilling all live virtual registers currently unified under preg |
| 116 | // that interfere with the most recently queried lvr. Return true if spilling |
| 117 | // was successful, and append any new spilled/split intervals to splitLVRs. |
| 118 | bool spillInterferences(LiveInterval &VirtReg, unsigned PhysReg, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 119 | SmallVectorImpl<unsigned> &SplitVRegs); |
Jakob Stoklund Olesen | a8bd9a6 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 120 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 121 | static char ID; |
| 122 | }; |
| 123 | |
| 124 | char RABasic::ID = 0; |
| 125 | |
| 126 | } // end anonymous namespace |
| 127 | |
Quentin Colombet | 9f43f26 | 2017-06-02 22:46:26 +0000 | [diff] [blame] | 128 | char &llvm::RABasicID = RABasic::ID; |
| 129 | |
| 130 | INITIALIZE_PASS_BEGIN(RABasic, "regallocbasic", "Basic Register Allocator", |
| 131 | false, false) |
| 132 | INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables) |
| 133 | INITIALIZE_PASS_DEPENDENCY(SlotIndexes) |
| 134 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) |
| 135 | INITIALIZE_PASS_DEPENDENCY(RegisterCoalescer) |
| 136 | INITIALIZE_PASS_DEPENDENCY(MachineScheduler) |
| 137 | INITIALIZE_PASS_DEPENDENCY(LiveStacks) |
| 138 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) |
| 139 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) |
| 140 | INITIALIZE_PASS_DEPENDENCY(VirtRegMap) |
| 141 | INITIALIZE_PASS_DEPENDENCY(LiveRegMatrix) |
| 142 | INITIALIZE_PASS_END(RABasic, "regallocbasic", "Basic Register Allocator", false, |
| 143 | false) |
| 144 | |
Quentin Colombet | 1067c96 | 2017-06-02 22:46:31 +0000 | [diff] [blame] | 145 | bool RABasic::LRE_CanEraseVirtReg(unsigned VirtReg) { |
Jonas Paulsson | 7913159 | 2017-09-15 07:47:38 +0000 | [diff] [blame] | 146 | LiveInterval &LI = LIS->getInterval(VirtReg); |
Quentin Colombet | 1067c96 | 2017-06-02 22:46:31 +0000 | [diff] [blame] | 147 | if (VRM->hasPhys(VirtReg)) { |
Quentin Colombet | 1067c96 | 2017-06-02 22:46:31 +0000 | [diff] [blame] | 148 | Matrix->unassign(LI); |
| 149 | aboutToRemoveInterval(LI); |
| 150 | return true; |
| 151 | } |
| 152 | // Unassigned virtreg is probably in the priority queue. |
| 153 | // RegAllocBase will erase it after dequeueing. |
Jonas Paulsson | 7913159 | 2017-09-15 07:47:38 +0000 | [diff] [blame] | 154 | // Nonetheless, clear the live-range so that the debug |
| 155 | // dump will show the right state for that VirtReg. |
| 156 | LI.clear(); |
Quentin Colombet | 1067c96 | 2017-06-02 22:46:31 +0000 | [diff] [blame] | 157 | return false; |
| 158 | } |
| 159 | |
| 160 | void RABasic::LRE_WillShrinkVirtReg(unsigned VirtReg) { |
| 161 | if (!VRM->hasPhys(VirtReg)) |
| 162 | return; |
| 163 | |
| 164 | // Register is assigned, put it back on the queue for reassignment. |
| 165 | LiveInterval &LI = LIS->getInterval(VirtReg); |
| 166 | Matrix->unassign(LI); |
| 167 | enqueue(&LI); |
| 168 | } |
| 169 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 170 | RABasic::RABasic(): MachineFunctionPass(ID) { |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 173 | void RABasic::getAnalysisUsage(AnalysisUsage &AU) const { |
| 174 | AU.setPreservesCFG(); |
Chandler Carruth | 9146833 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 175 | AU.addRequired<AAResultsWrapperPass>(); |
| 176 | AU.addPreserved<AAResultsWrapperPass>(); |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 177 | AU.addRequired<LiveIntervals>(); |
Jakob Stoklund Olesen | 05ec712 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 178 | AU.addPreserved<LiveIntervals>(); |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 179 | AU.addPreserved<SlotIndexes>(); |
Jakob Stoklund Olesen | cfafc54 | 2011-04-05 21:40:37 +0000 | [diff] [blame] | 180 | AU.addRequired<LiveDebugVariables>(); |
| 181 | AU.addPreserved<LiveDebugVariables>(); |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 182 | AU.addRequired<LiveStacks>(); |
| 183 | AU.addPreserved<LiveStacks>(); |
Benjamin Kramer | 4eed756 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 184 | AU.addRequired<MachineBlockFrequencyInfo>(); |
| 185 | AU.addPreserved<MachineBlockFrequencyInfo>(); |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 186 | AU.addRequiredID(MachineDominatorsID); |
| 187 | AU.addPreservedID(MachineDominatorsID); |
| 188 | AU.addRequired<MachineLoopInfo>(); |
| 189 | AU.addPreserved<MachineLoopInfo>(); |
| 190 | AU.addRequired<VirtRegMap>(); |
| 191 | AU.addPreserved<VirtRegMap>(); |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 192 | AU.addRequired<LiveRegMatrix>(); |
| 193 | AU.addPreserved<LiveRegMatrix>(); |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 194 | MachineFunctionPass::getAnalysisUsage(AU); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void RABasic::releaseMemory() { |
David Blaikie | ec31a30 | 2014-07-19 01:05:11 +0000 | [diff] [blame] | 198 | SpillerInstance.reset(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Jakob Stoklund Olesen | a8bd9a6 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 201 | |
| 202 | // Spill or split all live virtual registers currently unified under PhysReg |
| 203 | // that interfere with VirtReg. The newly spilled or split live intervals are |
| 204 | // returned by appending them to SplitVRegs. |
| 205 | bool RABasic::spillInterferences(LiveInterval &VirtReg, unsigned PhysReg, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 206 | SmallVectorImpl<unsigned> &SplitVRegs) { |
Jakob Stoklund Olesen | a8bd9a6 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 207 | // Record each interference and determine if all are spillable before mutating |
| 208 | // either the union or live intervals. |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 209 | SmallVector<LiveInterval*, 8> Intfs; |
| 210 | |
Jakob Stoklund Olesen | a8bd9a6 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 211 | // Collect interferences assigned to any alias of the physical register. |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 212 | for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { |
| 213 | LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units); |
| 214 | Q.collectInterferingVRegs(); |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 215 | for (unsigned i = Q.interferingVRegs().size(); i; --i) { |
| 216 | LiveInterval *Intf = Q.interferingVRegs()[i - 1]; |
| 217 | if (!Intf->isSpillable() || Intf->weight > VirtReg.weight) |
| 218 | return false; |
| 219 | Intfs.push_back(Intf); |
Jakob Stoklund Olesen | a8bd9a6 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 220 | } |
| 221 | } |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 222 | LLVM_DEBUG(dbgs() << "spilling " << printReg(PhysReg, TRI) |
| 223 | << " interferences with " << VirtReg << "\n"); |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 224 | assert(!Intfs.empty() && "expected interference"); |
Jakob Stoklund Olesen | a8bd9a6 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 225 | |
| 226 | // Spill each interfering vreg allocated to PhysReg or an alias. |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 227 | for (unsigned i = 0, e = Intfs.size(); i != e; ++i) { |
| 228 | LiveInterval &Spill = *Intfs[i]; |
| 229 | |
| 230 | // Skip duplicates. |
| 231 | if (!VRM->hasPhys(Spill.reg)) |
| 232 | continue; |
| 233 | |
| 234 | // Deallocate the interfering vreg by removing it from the union. |
| 235 | // A LiveInterval instance may not be in a union during modification! |
| 236 | Matrix->unassign(Spill); |
| 237 | |
| 238 | // Spill the extracted interval. |
Quentin Colombet | 1067c96 | 2017-06-02 22:46:31 +0000 | [diff] [blame] | 239 | LiveRangeEdit LRE(&Spill, SplitVRegs, *MF, *LIS, VRM, this, &DeadRemats); |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 240 | spiller().spill(LRE); |
| 241 | } |
Jakob Stoklund Olesen | a8bd9a6 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 242 | return true; |
| 243 | } |
| 244 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 245 | // Driver for the register assignment and splitting heuristics. |
| 246 | // Manages iteration over the LiveIntervalUnions. |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 247 | // |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 248 | // This is a minimal implementation of register assignment and splitting that |
| 249 | // spills whenever we run out of registers. |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 250 | // |
| 251 | // selectOrSplit can only be called once per live virtual register. We then do a |
| 252 | // single interference test for each register the correct class until we find an |
| 253 | // available register. So, the number of interference tests in the worst case is |
| 254 | // |vregs| * |machineregs|. And since the number of interference tests is |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 255 | // minimal, there is no value in caching them outside the scope of |
| 256 | // selectOrSplit(). |
| 257 | unsigned RABasic::selectOrSplit(LiveInterval &VirtReg, |
Mark Lacey | 1feb585 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 258 | SmallVectorImpl<unsigned> &SplitVRegs) { |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 259 | // Populate a list of physical register spill candidates. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 260 | SmallVector<unsigned, 8> PhysRegSpillCands; |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 261 | |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 262 | // Check for an available register in this class. |
Matthias Braun | 2aa5727 | 2015-07-15 22:16:00 +0000 | [diff] [blame] | 263 | AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo, Matrix); |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 264 | while (unsigned PhysReg = Order.next()) { |
| 265 | // Check for interference in PhysReg |
| 266 | switch (Matrix->checkInterference(VirtReg, PhysReg)) { |
| 267 | case LiveRegMatrix::IK_Free: |
| 268 | // PhysReg is available, allocate it. |
| 269 | return PhysReg; |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 270 | |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 271 | case LiveRegMatrix::IK_VirtReg: |
| 272 | // Only virtual registers in the way, we may be able to spill them. |
| 273 | PhysRegSpillCands.push_back(PhysReg); |
Jakob Stoklund Olesen | a94e635 | 2012-02-08 18:54:35 +0000 | [diff] [blame] | 274 | continue; |
| 275 | |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 276 | default: |
| 277 | // RegMask or RegUnit interference. |
| 278 | continue; |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 279 | } |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 280 | } |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 281 | |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 282 | // Try to spill another interfering reg with less spill weight. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 283 | for (SmallVectorImpl<unsigned>::iterator PhysRegI = PhysRegSpillCands.begin(), |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 284 | PhysRegE = PhysRegSpillCands.end(); PhysRegI != PhysRegE; ++PhysRegI) { |
| 285 | if (!spillInterferences(VirtReg, *PhysRegI, SplitVRegs)) |
| 286 | continue; |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 287 | |
Jakob Stoklund Olesen | 812cda9 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 288 | assert(!Matrix->checkInterference(VirtReg, *PhysRegI) && |
Jakob Stoklund Olesen | 2b38c51 | 2010-12-07 18:51:27 +0000 | [diff] [blame] | 289 | "Interference after spill."); |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 290 | // Tell the caller to allocate to this newly freed physical register. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 291 | return *PhysRegI; |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 292 | } |
Jakob Stoklund Olesen | bf4e10f | 2011-05-06 21:58:30 +0000 | [diff] [blame] | 293 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 294 | // No other spill candidates were found, so spill the current VirtReg. |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 295 | LLVM_DEBUG(dbgs() << "spilling: " << VirtReg << '\n'); |
Jakob Stoklund Olesen | bf4e10f | 2011-05-06 21:58:30 +0000 | [diff] [blame] | 296 | if (!VirtReg.isSpillable()) |
| 297 | return ~0u; |
Quentin Colombet | 1067c96 | 2017-06-02 22:46:31 +0000 | [diff] [blame] | 298 | LiveRangeEdit LRE(&VirtReg, SplitVRegs, *MF, *LIS, VRM, this, &DeadRemats); |
Jakob Stoklund Olesen | 47dbf6c | 2011-03-10 01:51:42 +0000 | [diff] [blame] | 299 | spiller().spill(LRE); |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 300 | |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 301 | // The live virtual register requesting allocation was spilled, so tell |
| 302 | // the caller not to allocate anything during this round. |
| 303 | return 0; |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 304 | } |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 305 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 306 | bool RABasic::runOnMachineFunction(MachineFunction &mf) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 307 | LLVM_DEBUG(dbgs() << "********** BASIC REGISTER ALLOCATION **********\n" |
| 308 | << "********** Function: " << mf.getName() << '\n'); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 309 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 310 | MF = &mf; |
Jakob Stoklund Olesen | d4348a2 | 2012-06-20 22:52:29 +0000 | [diff] [blame] | 311 | RegAllocBase::init(getAnalysis<VirtRegMap>(), |
| 312 | getAnalysis<LiveIntervals>(), |
| 313 | getAnalysis<LiveRegMatrix>()); |
Arnaud A. de Grandmaison | a77da05 | 2013-11-10 17:46:31 +0000 | [diff] [blame] | 314 | |
Robert Lougher | 0d87d63 | 2015-08-10 11:59:44 +0000 | [diff] [blame] | 315 | calculateSpillWeightsAndHints(*LIS, *MF, VRM, |
Arnaud A. de Grandmaison | 095f994 | 2013-11-11 19:04:45 +0000 | [diff] [blame] | 316 | getAnalysis<MachineLoopInfo>(), |
| 317 | getAnalysis<MachineBlockFrequencyInfo>()); |
Arnaud A. de Grandmaison | a77da05 | 2013-11-10 17:46:31 +0000 | [diff] [blame] | 318 | |
Jakob Stoklund Olesen | 8427596 | 2011-03-31 23:02:17 +0000 | [diff] [blame] | 319 | SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM)); |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 320 | |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 321 | allocatePhysRegs(); |
Wei Mi | 815b02e | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 322 | postOptimization(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 323 | |
| 324 | // Diagnostic output before rewriting |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 325 | LLVM_DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *VRM << "\n"); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 326 | |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 327 | releaseMemory(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 328 | return true; |
| 329 | } |
| 330 | |
Andrew Trick | 13bdbb0 | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 331 | FunctionPass* llvm::createBasicRegisterAllocator() |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 332 | { |
| 333 | return new RABasic(); |
| 334 | } |