| 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 | // | 
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | 
|  | 4 | // See https://llvm.org/LICENSE.txt for license information. | 
|  | 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 6 | // | 
|  | 7 | //===----------------------------------------------------------------------===// | 
|  | 8 | // | 
|  | 9 | // This file defines the RABasic function pass, which provides a minimal | 
|  | 10 | // implementation of the basic register allocator. | 
|  | 11 | // | 
|  | 12 | //===----------------------------------------------------------------------===// | 
|  | 13 |  | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 14 | #include "AllocationOrder.h" | 
| Jakob Stoklund Olesen | 6aa0fbf | 2011-04-05 21:40:37 +0000 | [diff] [blame] | 15 | #include "LiveDebugVariables.h" | 
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "RegAllocBase.h" | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 17 | #include "Spiller.h" | 
| Andrew Trick | f11344d | 2010-11-11 17:46:29 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/AliasAnalysis.h" | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/CalcSpillWeights.h" | 
| Matthias Braun | f842297 | 2017-12-13 02:51:04 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/LiveIntervals.h" | 
| Pete Cooper | 3ca96f9 | 2012-04-02 22:44:18 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/LiveRangeEdit.h" | 
| Jakob Stoklund Olesen | 26c9d70 | 2012-11-28 19:13:06 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/LiveRegMatrix.h" | 
| Matthias Braun | ef95969 | 2017-12-18 23:19:44 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/LiveStacks.h" | 
| Benjamin Kramer | e2a1d89 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineFunctionPass.h" | 
|  | 26 | #include "llvm/CodeGen/MachineInstr.h" | 
|  | 27 | #include "llvm/CodeGen/MachineLoopInfo.h" | 
|  | 28 | #include "llvm/CodeGen/MachineRegisterInfo.h" | 
| Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/Passes.h" | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/RegAllocRegistry.h" | 
| David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/TargetRegisterInfo.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" | 
| Jakob Stoklund Olesen | db357d7 | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 36 | #include <cstdlib> | 
| Jakob Stoklund Olesen | 2329c54 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 37 | #include <queue> | 
| Andrew Trick | 84aef49 | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 38 |  | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 39 | using namespace llvm; | 
|  | 40 |  | 
| Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 41 | #define DEBUG_TYPE "regalloc" | 
|  | 42 |  | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 43 | static RegisterRegAlloc basicRegAlloc("basic", "basic register allocator", | 
|  | 44 | createBasicRegisterAllocator); | 
|  | 45 |  | 
| Benjamin Kramer | aef5bd0 | 2010-11-25 16:42:51 +0000 | [diff] [blame] | 46 | namespace { | 
| Jakob Stoklund Olesen | 2329c54 | 2011-02-22 23:01:52 +0000 | [diff] [blame] | 47 | struct CompSpillWeight { | 
|  | 48 | bool operator()(LiveInterval *A, LiveInterval *B) const { | 
|  | 49 | return A->weight < B->weight; | 
|  | 50 | } | 
|  | 51 | }; | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | namespace { | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 55 | /// RABasic provides a minimal implementation of the basic register allocation | 
|  | 56 | /// algorithm. It prioritizes live virtual registers by spill weight and spills | 
|  | 57 | /// whenever a register is unavailable. This is not practical in production but | 
|  | 58 | /// provides a useful baseline both for measuring other allocators and comparing | 
|  | 59 | /// the speed of the basic algorithm against other styles of allocators. | 
| Quentin Colombet | 2145cf3 | 2017-06-02 22:46:31 +0000 | [diff] [blame] | 60 | class RABasic : public MachineFunctionPass, | 
|  | 61 | public RegAllocBase, | 
|  | 62 | private LiveRangeEdit::Delegate { | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 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 |  | 
| Quentin Colombet | 2145cf3 | 2017-06-02 22:46:31 +0000 | [diff] [blame] | 75 | bool LRE_CanEraseVirtReg(unsigned) override; | 
|  | 76 | void LRE_WillShrinkVirtReg(unsigned) override; | 
|  | 77 |  | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 78 | public: | 
|  | 79 | RABasic(); | 
|  | 80 |  | 
|  | 81 | /// Return the pass name. | 
| Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 82 | StringRef getPassName() const override { return "Basic Register Allocator"; } | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 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 |  | 
| Matthias Braun | 90799ce | 2016-08-23 21:19:49 +0000 | [diff] [blame] | 109 | MachineFunctionProperties getRequiredProperties() const override { | 
|  | 110 | return MachineFunctionProperties().set( | 
|  | 111 | MachineFunctionProperties::Property::NoPHIs); | 
|  | 112 | } | 
|  | 113 |  | 
| Jakob Stoklund Olesen | 73edbf1 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 114 | // Helper for spilling all live virtual registers currently unified under preg | 
|  | 115 | // that interfere with the most recently queried lvr.  Return true if spilling | 
|  | 116 | // was successful, and append any new spilled/split intervals to splitLVRs. | 
|  | 117 | bool spillInterferences(LiveInterval &VirtReg, unsigned PhysReg, | 
| Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 118 | SmallVectorImpl<unsigned> &SplitVRegs); | 
| Jakob Stoklund Olesen | 73edbf1 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 119 |  | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 120 | static char ID; | 
|  | 121 | }; | 
|  | 122 |  | 
|  | 123 | char RABasic::ID = 0; | 
|  | 124 |  | 
|  | 125 | } // end anonymous namespace | 
|  | 126 |  | 
| Quentin Colombet | ebbaed6 | 2017-06-02 22:46:26 +0000 | [diff] [blame] | 127 | char &llvm::RABasicID = RABasic::ID; | 
|  | 128 |  | 
|  | 129 | INITIALIZE_PASS_BEGIN(RABasic, "regallocbasic", "Basic Register Allocator", | 
|  | 130 | false, false) | 
|  | 131 | INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables) | 
|  | 132 | INITIALIZE_PASS_DEPENDENCY(SlotIndexes) | 
|  | 133 | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) | 
|  | 134 | INITIALIZE_PASS_DEPENDENCY(RegisterCoalescer) | 
|  | 135 | INITIALIZE_PASS_DEPENDENCY(MachineScheduler) | 
|  | 136 | INITIALIZE_PASS_DEPENDENCY(LiveStacks) | 
|  | 137 | INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) | 
|  | 138 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) | 
|  | 139 | INITIALIZE_PASS_DEPENDENCY(VirtRegMap) | 
|  | 140 | INITIALIZE_PASS_DEPENDENCY(LiveRegMatrix) | 
|  | 141 | INITIALIZE_PASS_END(RABasic, "regallocbasic", "Basic Register Allocator", false, | 
|  | 142 | false) | 
|  | 143 |  | 
| Quentin Colombet | 2145cf3 | 2017-06-02 22:46:31 +0000 | [diff] [blame] | 144 | bool RABasic::LRE_CanEraseVirtReg(unsigned VirtReg) { | 
| Jonas Paulsson | 6188f32 | 2017-09-15 07:47:38 +0000 | [diff] [blame] | 145 | LiveInterval &LI = LIS->getInterval(VirtReg); | 
| Quentin Colombet | 2145cf3 | 2017-06-02 22:46:31 +0000 | [diff] [blame] | 146 | if (VRM->hasPhys(VirtReg)) { | 
| Quentin Colombet | 2145cf3 | 2017-06-02 22:46:31 +0000 | [diff] [blame] | 147 | Matrix->unassign(LI); | 
|  | 148 | aboutToRemoveInterval(LI); | 
|  | 149 | return true; | 
|  | 150 | } | 
|  | 151 | // Unassigned virtreg is probably in the priority queue. | 
|  | 152 | // RegAllocBase will erase it after dequeueing. | 
| Jonas Paulsson | 6188f32 | 2017-09-15 07:47:38 +0000 | [diff] [blame] | 153 | // Nonetheless, clear the live-range so that the debug | 
|  | 154 | // dump will show the right state for that VirtReg. | 
|  | 155 | LI.clear(); | 
| Quentin Colombet | 2145cf3 | 2017-06-02 22:46:31 +0000 | [diff] [blame] | 156 | return false; | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | void RABasic::LRE_WillShrinkVirtReg(unsigned VirtReg) { | 
|  | 160 | if (!VRM->hasPhys(VirtReg)) | 
|  | 161 | return; | 
|  | 162 |  | 
|  | 163 | // Register is assigned, put it back on the queue for reassignment. | 
|  | 164 | LiveInterval &LI = LIS->getInterval(VirtReg); | 
|  | 165 | Matrix->unassign(LI); | 
|  | 166 | enqueue(&LI); | 
|  | 167 | } | 
|  | 168 |  | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 169 | RABasic::RABasic(): MachineFunctionPass(ID) { | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 170 | } | 
|  | 171 |  | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 172 | void RABasic::getAnalysisUsage(AnalysisUsage &AU) const { | 
|  | 173 | AU.setPreservesCFG(); | 
| Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 174 | AU.addRequired<AAResultsWrapperPass>(); | 
|  | 175 | AU.addPreserved<AAResultsWrapperPass>(); | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 176 | AU.addRequired<LiveIntervals>(); | 
| Jakob Stoklund Olesen | 1224312 | 2012-06-08 23:44:45 +0000 | [diff] [blame] | 177 | AU.addPreserved<LiveIntervals>(); | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 178 | AU.addPreserved<SlotIndexes>(); | 
| Jakob Stoklund Olesen | 6aa0fbf | 2011-04-05 21:40:37 +0000 | [diff] [blame] | 179 | AU.addRequired<LiveDebugVariables>(); | 
|  | 180 | AU.addPreserved<LiveDebugVariables>(); | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 181 | AU.addRequired<LiveStacks>(); | 
|  | 182 | AU.addPreserved<LiveStacks>(); | 
| Benjamin Kramer | e2a1d89 | 2013-06-17 19:00:36 +0000 | [diff] [blame] | 183 | AU.addRequired<MachineBlockFrequencyInfo>(); | 
|  | 184 | AU.addPreserved<MachineBlockFrequencyInfo>(); | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 185 | AU.addRequiredID(MachineDominatorsID); | 
|  | 186 | AU.addPreservedID(MachineDominatorsID); | 
|  | 187 | AU.addRequired<MachineLoopInfo>(); | 
|  | 188 | AU.addPreserved<MachineLoopInfo>(); | 
|  | 189 | AU.addRequired<VirtRegMap>(); | 
|  | 190 | AU.addPreserved<VirtRegMap>(); | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 191 | AU.addRequired<LiveRegMatrix>(); | 
|  | 192 | AU.addPreserved<LiveRegMatrix>(); | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 193 | MachineFunctionPass::getAnalysisUsage(AU); | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 194 | } | 
|  | 195 |  | 
|  | 196 | void RABasic::releaseMemory() { | 
| David Blaikie | b61064e | 2014-07-19 01:05:11 +0000 | [diff] [blame] | 197 | SpillerInstance.reset(); | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 198 | } | 
|  | 199 |  | 
| Jakob Stoklund Olesen | 73edbf1 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 200 |  | 
|  | 201 | // Spill or split all live virtual registers currently unified under PhysReg | 
|  | 202 | // that interfere with VirtReg. The newly spilled or split live intervals are | 
|  | 203 | // returned by appending them to SplitVRegs. | 
|  | 204 | bool RABasic::spillInterferences(LiveInterval &VirtReg, unsigned PhysReg, | 
| Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 205 | SmallVectorImpl<unsigned> &SplitVRegs) { | 
| Jakob Stoklund Olesen | 73edbf1 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 206 | // Record each interference and determine if all are spillable before mutating | 
|  | 207 | // either the union or live intervals. | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 208 | SmallVector<LiveInterval*, 8> Intfs; | 
|  | 209 |  | 
| Jakob Stoklund Olesen | 73edbf1 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 210 | // Collect interferences assigned to any alias of the physical register. | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 211 | for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { | 
|  | 212 | LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units); | 
|  | 213 | Q.collectInterferingVRegs(); | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 214 | for (unsigned i = Q.interferingVRegs().size(); i; --i) { | 
|  | 215 | LiveInterval *Intf = Q.interferingVRegs()[i - 1]; | 
|  | 216 | if (!Intf->isSpillable() || Intf->weight > VirtReg.weight) | 
|  | 217 | return false; | 
|  | 218 | Intfs.push_back(Intf); | 
| Jakob Stoklund Olesen | 73edbf1 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 219 | } | 
|  | 220 | } | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 221 | LLVM_DEBUG(dbgs() << "spilling " << printReg(PhysReg, TRI) | 
|  | 222 | << " interferences with " << VirtReg << "\n"); | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 223 | assert(!Intfs.empty() && "expected interference"); | 
| Jakob Stoklund Olesen | 73edbf1 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 224 |  | 
|  | 225 | // Spill each interfering vreg allocated to PhysReg or an alias. | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 226 | for (unsigned i = 0, e = Intfs.size(); i != e; ++i) { | 
|  | 227 | LiveInterval &Spill = *Intfs[i]; | 
|  | 228 |  | 
|  | 229 | // Skip duplicates. | 
|  | 230 | if (!VRM->hasPhys(Spill.reg)) | 
|  | 231 | continue; | 
|  | 232 |  | 
|  | 233 | // Deallocate the interfering vreg by removing it from the union. | 
|  | 234 | // A LiveInterval instance may not be in a union during modification! | 
|  | 235 | Matrix->unassign(Spill); | 
|  | 236 |  | 
|  | 237 | // Spill the extracted interval. | 
| Quentin Colombet | 2145cf3 | 2017-06-02 22:46:31 +0000 | [diff] [blame] | 238 | LiveRangeEdit LRE(&Spill, SplitVRegs, *MF, *LIS, VRM, this, &DeadRemats); | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 239 | spiller().spill(LRE); | 
|  | 240 | } | 
| Jakob Stoklund Olesen | 73edbf1 | 2012-01-11 22:52:14 +0000 | [diff] [blame] | 241 | return true; | 
|  | 242 | } | 
|  | 243 |  | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 244 | // Driver for the register assignment and splitting heuristics. | 
|  | 245 | // Manages iteration over the LiveIntervalUnions. | 
| Andrew Trick | 799ec1c | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 246 | // | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 247 | // This is a minimal implementation of register assignment and splitting that | 
|  | 248 | // spills whenever we run out of registers. | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 249 | // | 
|  | 250 | // selectOrSplit can only be called once per live virtual register. We then do a | 
|  | 251 | // single interference test for each register the correct class until we find an | 
|  | 252 | // available register. So, the number of interference tests in the worst case is | 
|  | 253 | // |vregs| * |machineregs|. And since the number of interference tests is | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 254 | // minimal, there is no value in caching them outside the scope of | 
|  | 255 | // selectOrSplit(). | 
|  | 256 | unsigned RABasic::selectOrSplit(LiveInterval &VirtReg, | 
| Mark Lacey | f9ea885 | 2013-08-14 23:50:04 +0000 | [diff] [blame] | 257 | SmallVectorImpl<unsigned> &SplitVRegs) { | 
| Andrew Trick | 89eb6a8 | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 258 | // Populate a list of physical register spill candidates. | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 259 | SmallVector<unsigned, 8> PhysRegSpillCands; | 
| Andrew Trick | 3528465 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 260 |  | 
| Andrew Trick | 799ec1c | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 261 | // Check for an available register in this class. | 
| Matthias Braun | 5d1f12d | 2015-07-15 22:16:00 +0000 | [diff] [blame] | 262 | AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo, Matrix); | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 263 | while (unsigned PhysReg = Order.next()) { | 
|  | 264 | // Check for interference in PhysReg | 
|  | 265 | switch (Matrix->checkInterference(VirtReg, PhysReg)) { | 
|  | 266 | case LiveRegMatrix::IK_Free: | 
|  | 267 | // PhysReg is available, allocate it. | 
|  | 268 | return PhysReg; | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 269 |  | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 270 | case LiveRegMatrix::IK_VirtReg: | 
|  | 271 | // Only virtual registers in the way, we may be able to spill them. | 
|  | 272 | PhysRegSpillCands.push_back(PhysReg); | 
| Jakob Stoklund Olesen | 0c1eea2 | 2012-02-08 18:54:35 +0000 | [diff] [blame] | 273 | continue; | 
|  | 274 |  | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 275 | default: | 
|  | 276 | // RegMask or RegUnit interference. | 
|  | 277 | continue; | 
| Andrew Trick | 3528465 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 278 | } | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 279 | } | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 280 |  | 
| Andrew Trick | 89eb6a8 | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 281 | // Try to spill another interfering reg with less spill weight. | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 282 | for (SmallVectorImpl<unsigned>::iterator PhysRegI = PhysRegSpillCands.begin(), | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 283 | PhysRegE = PhysRegSpillCands.end(); PhysRegI != PhysRegE; ++PhysRegI) { | 
|  | 284 | if (!spillInterferences(VirtReg, *PhysRegI, SplitVRegs)) | 
|  | 285 | continue; | 
| Andrew Trick | 89eb6a8 | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 286 |  | 
| Jakob Stoklund Olesen | 03b87d5 | 2012-06-20 22:52:24 +0000 | [diff] [blame] | 287 | assert(!Matrix->checkInterference(VirtReg, *PhysRegI) && | 
| Jakob Stoklund Olesen | fb207c1 | 2010-12-07 18:51:27 +0000 | [diff] [blame] | 288 | "Interference after spill."); | 
| Andrew Trick | 89eb6a8 | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 289 | // Tell the caller to allocate to this newly freed physical register. | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 290 | return *PhysRegI; | 
| Andrew Trick | 3528465 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 291 | } | 
| Jakob Stoklund Olesen | a5c8899 | 2011-05-06 21:58:30 +0000 | [diff] [blame] | 292 |  | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 293 | // No other spill candidates were found, so spill the current VirtReg. | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 294 | LLVM_DEBUG(dbgs() << "spilling: " << VirtReg << '\n'); | 
| Jakob Stoklund Olesen | a5c8899 | 2011-05-06 21:58:30 +0000 | [diff] [blame] | 295 | if (!VirtReg.isSpillable()) | 
|  | 296 | return ~0u; | 
| Quentin Colombet | 2145cf3 | 2017-06-02 22:46:31 +0000 | [diff] [blame] | 297 | LiveRangeEdit LRE(&VirtReg, SplitVRegs, *MF, *LIS, VRM, this, &DeadRemats); | 
| Jakob Stoklund Olesen | 4d6eafa | 2011-03-10 01:51:42 +0000 | [diff] [blame] | 298 | spiller().spill(LRE); | 
| Andrew Trick | 799ec1c | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 299 |  | 
| Andrew Trick | 89eb6a8 | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 300 | // The live virtual register requesting allocation was spilled, so tell | 
|  | 301 | // the caller not to allocate anything during this round. | 
|  | 302 | return 0; | 
| Andrew Trick | 3528465 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 303 | } | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 304 |  | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 305 | bool RABasic::runOnMachineFunction(MachineFunction &mf) { | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 306 | LLVM_DEBUG(dbgs() << "********** BASIC REGISTER ALLOCATION **********\n" | 
|  | 307 | << "********** Function: " << mf.getName() << '\n'); | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 308 |  | 
| Andrew Trick | fce64c9 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 309 | MF = &mf; | 
| Jakob Stoklund Olesen | 2d2dec9 | 2012-06-20 22:52:29 +0000 | [diff] [blame] | 310 | RegAllocBase::init(getAnalysis<VirtRegMap>(), | 
|  | 311 | getAnalysis<LiveIntervals>(), | 
|  | 312 | getAnalysis<LiveRegMatrix>()); | 
| Arnaud A. de Grandmaison | 760c1e0 | 2013-11-10 17:46:31 +0000 | [diff] [blame] | 313 |  | 
| Robert Lougher | 11a44b7 | 2015-08-10 11:59:44 +0000 | [diff] [blame] | 314 | calculateSpillWeightsAndHints(*LIS, *MF, VRM, | 
| Arnaud A. de Grandmaison | ea3ac16 | 2013-11-11 19:04:45 +0000 | [diff] [blame] | 315 | getAnalysis<MachineLoopInfo>(), | 
|  | 316 | getAnalysis<MachineBlockFrequencyInfo>()); | 
| Arnaud A. de Grandmaison | 760c1e0 | 2013-11-10 17:46:31 +0000 | [diff] [blame] | 317 |  | 
| Jakob Stoklund Olesen | 6e597dc | 2011-03-31 23:02:17 +0000 | [diff] [blame] | 318 | SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM)); | 
| Andrew Trick | 799ec1c | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 319 |  | 
| Andrew Trick | 84aef49 | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 320 | allocatePhysRegs(); | 
| Wei Mi | 9a16d65 | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 321 | postOptimization(); | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 322 |  | 
|  | 323 | // Diagnostic output before rewriting | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 324 | LLVM_DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *VRM << "\n"); | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 325 |  | 
| Andrew Trick | 84aef49 | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 326 | releaseMemory(); | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 327 | return true; | 
|  | 328 | } | 
|  | 329 |  | 
| Andrew Trick | 799ec1c | 2010-11-20 02:43:55 +0000 | [diff] [blame] | 330 | FunctionPass* llvm::createBasicRegisterAllocator() | 
| Andrew Trick | 1c24605 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 331 | { | 
|  | 332 | return new RABasic(); | 
|  | 333 | } |