1//===-- RegAllocBasic.cpp - Basic Register Allocator ----------------------===//
2//
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
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
14#include "AllocationOrder.h"
15#include "RegAllocBase.h"
16#include "llvm/Analysis/AliasAnalysis.h"
17#include "llvm/CodeGen/CalcSpillWeights.h"
18#include "llvm/CodeGen/LiveDebugVariables.h"
19#include "llvm/CodeGen/LiveIntervals.h"
20#include "llvm/CodeGen/LiveRangeEdit.h"
21#include "llvm/CodeGen/LiveRegMatrix.h"
22#include "llvm/CodeGen/LiveStacks.h"
23#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
24#include "llvm/CodeGen/MachineFunctionPass.h"
25#include "llvm/CodeGen/MachineLoopInfo.h"
26#include "llvm/CodeGen/Passes.h"
27#include "llvm/CodeGen/RegAllocRegistry.h"
28#include "llvm/CodeGen/Spiller.h"
29#include "llvm/CodeGen/TargetRegisterInfo.h"
30#include "llvm/CodeGen/VirtRegMap.h"
31#include "llvm/Pass.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/Support/raw_ostream.h"
34#include <queue>
35
36using namespace llvm;
37
38#define DEBUG_TYPE "regalloc"
39
40static RegisterRegAlloc basicRegAlloc("basic", "basic register allocator",
41 createBasicRegisterAllocator);
42
43namespace {
44 struct CompSpillWeight {
45 bool operator()(const LiveInterval *A, const LiveInterval *B) const {
46 return A->weight() < B->weight();
47 }
48 };
49}
50
51namespace {
52/// RABasic provides a minimal implementation of the basic register allocation
53/// algorithm. It prioritizes live virtual registers by spill weight and spills
54/// whenever a register is unavailable. This is not practical in production but
55/// provides a useful baseline both for measuring other allocators and comparing
56/// the speed of the basic algorithm against other styles of allocators.
57class RABasic : public MachineFunctionPass,
58 public RegAllocBase,
59 private LiveRangeEdit::Delegate {
60 // context
61 MachineFunction *MF = nullptr;
62
63 // state
64 std::unique_ptr<Spiller> SpillerInstance;
65 std::priority_queue<const LiveInterval *, std::vector<const LiveInterval *>,
66 CompSpillWeight>
67 Queue;
68
69 // Scratch space. Allocated here to avoid repeated malloc calls in
70 // selectOrSplit().
71 BitVector UsableRegs;
72
73 bool LRE_CanEraseVirtReg(Register) override;
74 void LRE_WillShrinkVirtReg(Register) override;
75
76public:
77 RABasic(const RegAllocFilterFunc F = nullptr);
78
79 /// Return the pass name.
80 StringRef getPassName() const override { return "Basic Register Allocator"; }
81
82 /// RABasic analysis usage.
83 void getAnalysisUsage(AnalysisUsage &AU) const override;
84
85 void releaseMemory() override;
86
87 Spiller &spiller() override { return *SpillerInstance; }
88
89 void enqueueImpl(const LiveInterval *LI) override { Queue.push(x: LI); }
90
91 const LiveInterval *dequeue() override {
92 if (Queue.empty())
93 return nullptr;
94 const LiveInterval *LI = Queue.top();
95 Queue.pop();
96 return LI;
97 }
98
99 MCRegister selectOrSplit(const LiveInterval &VirtReg,
100 SmallVectorImpl<Register> &SplitVRegs) override;
101
102 /// Perform register allocation.
103 bool runOnMachineFunction(MachineFunction &mf) override;
104
105 MachineFunctionProperties getRequiredProperties() const override {
106 return MachineFunctionProperties().set(
107 MachineFunctionProperties::Property::NoPHIs);
108 }
109
110 MachineFunctionProperties getClearedProperties() const override {
111 return MachineFunctionProperties().set(
112 MachineFunctionProperties::Property::IsSSA);
113 }
114
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(const LiveInterval &VirtReg, MCRegister PhysReg,
119 SmallVectorImpl<Register> &SplitVRegs);
120
121 static char ID;
122};
123
124char RABasic::ID = 0;
125
126} // end anonymous namespace
127
128char &llvm::RABasicID = RABasic::ID;
129
130INITIALIZE_PASS_BEGIN(RABasic, "regallocbasic", "Basic Register Allocator",
131 false, false)
132INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables)
133INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
134INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
135INITIALIZE_PASS_DEPENDENCY(RegisterCoalescer)
136INITIALIZE_PASS_DEPENDENCY(MachineScheduler)
137INITIALIZE_PASS_DEPENDENCY(LiveStacks)
138INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
139INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
140INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
141INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
142INITIALIZE_PASS_DEPENDENCY(LiveRegMatrix)
143INITIALIZE_PASS_END(RABasic, "regallocbasic", "Basic Register Allocator", false,
144 false)
145
146bool RABasic::LRE_CanEraseVirtReg(Register VirtReg) {
147 LiveInterval &LI = LIS->getInterval(Reg: VirtReg);
148 if (VRM->hasPhys(virtReg: VirtReg)) {
149 Matrix->unassign(VirtReg: LI);
150 aboutToRemoveInterval(LI);
151 return true;
152 }
153 // Unassigned virtreg is probably in the priority queue.
154 // RegAllocBase will erase it after dequeueing.
155 // Nonetheless, clear the live-range so that the debug
156 // dump will show the right state for that VirtReg.
157 LI.clear();
158 return false;
159}
160
161void RABasic::LRE_WillShrinkVirtReg(Register VirtReg) {
162 if (!VRM->hasPhys(virtReg: VirtReg))
163 return;
164
165 // Register is assigned, put it back on the queue for reassignment.
166 LiveInterval &LI = LIS->getInterval(Reg: VirtReg);
167 Matrix->unassign(VirtReg: LI);
168 enqueue(LI: &LI);
169}
170
171RABasic::RABasic(RegAllocFilterFunc F)
172 : MachineFunctionPass(ID), RegAllocBase(F) {}
173
174void RABasic::getAnalysisUsage(AnalysisUsage &AU) const {
175 AU.setPreservesCFG();
176 AU.addRequired<AAResultsWrapperPass>();
177 AU.addPreserved<AAResultsWrapperPass>();
178 AU.addRequired<LiveIntervalsWrapperPass>();
179 AU.addPreserved<LiveIntervalsWrapperPass>();
180 AU.addPreserved<SlotIndexesWrapperPass>();
181 AU.addRequired<LiveDebugVariables>();
182 AU.addPreserved<LiveDebugVariables>();
183 AU.addRequired<LiveStacks>();
184 AU.addPreserved<LiveStacks>();
185 AU.addRequired<MachineBlockFrequencyInfoWrapperPass>();
186 AU.addPreserved<MachineBlockFrequencyInfoWrapperPass>();
187 AU.addRequiredID(ID&: MachineDominatorsID);
188 AU.addPreservedID(ID&: MachineDominatorsID);
189 AU.addRequired<MachineLoopInfoWrapperPass>();
190 AU.addPreserved<MachineLoopInfoWrapperPass>();
191 AU.addRequired<VirtRegMap>();
192 AU.addPreserved<VirtRegMap>();
193 AU.addRequired<LiveRegMatrix>();
194 AU.addPreserved<LiveRegMatrix>();
195 MachineFunctionPass::getAnalysisUsage(AU);
196}
197
198void RABasic::releaseMemory() {
199 SpillerInstance.reset();
200}
201
202
203// Spill or split all live virtual registers currently unified under PhysReg
204// that interfere with VirtReg. The newly spilled or split live intervals are
205// returned by appending them to SplitVRegs.
206bool RABasic::spillInterferences(const LiveInterval &VirtReg,
207 MCRegister PhysReg,
208 SmallVectorImpl<Register> &SplitVRegs) {
209 // Record each interference and determine if all are spillable before mutating
210 // either the union or live intervals.
211 SmallVector<const LiveInterval *, 8> Intfs;
212
213 // Collect interferences assigned to any alias of the physical register.
214 for (MCRegUnit Unit : TRI->regunits(Reg: PhysReg)) {
215 LiveIntervalUnion::Query &Q = Matrix->query(LR: VirtReg, RegUnit: Unit);
216 for (const auto *Intf : reverse(C: Q.interferingVRegs())) {
217 if (!Intf->isSpillable() || Intf->weight() > VirtReg.weight())
218 return false;
219 Intfs.push_back(Elt: Intf);
220 }
221 }
222 LLVM_DEBUG(dbgs() << "spilling " << printReg(PhysReg, TRI)
223 << " interferences with " << VirtReg << "\n");
224 assert(!Intfs.empty() && "expected interference");
225
226 // Spill each interfering vreg allocated to PhysReg or an alias.
227 for (const LiveInterval *Spill : Intfs) {
228 // Skip duplicates.
229 if (!VRM->hasPhys(virtReg: Spill->reg()))
230 continue;
231
232 // Deallocate the interfering vreg by removing it from the union.
233 // A LiveInterval instance may not be in a union during modification!
234 Matrix->unassign(VirtReg: *Spill);
235
236 // Spill the extracted interval.
237 LiveRangeEdit LRE(Spill, SplitVRegs, *MF, *LIS, VRM, this, &DeadRemats);
238 spiller().spill(LRE);
239 }
240 return true;
241}
242
243// Driver for the register assignment and splitting heuristics.
244// Manages iteration over the LiveIntervalUnions.
245//
246// This is a minimal implementation of register assignment and splitting that
247// spills whenever we run out of registers.
248//
249// selectOrSplit can only be called once per live virtual register. We then do a
250// single interference test for each register the correct class until we find an
251// available register. So, the number of interference tests in the worst case is
252// |vregs| * |machineregs|. And since the number of interference tests is
253// minimal, there is no value in caching them outside the scope of
254// selectOrSplit().
255MCRegister RABasic::selectOrSplit(const LiveInterval &VirtReg,
256 SmallVectorImpl<Register> &SplitVRegs) {
257 // Populate a list of physical register spill candidates.
258 SmallVector<MCRegister, 8> PhysRegSpillCands;
259
260 // Check for an available register in this class.
261 auto Order =
262 AllocationOrder::create(VirtReg: VirtReg.reg(), VRM: *VRM, RegClassInfo, Matrix);
263 for (MCRegister PhysReg : Order) {
264 assert(PhysReg.isValid());
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;
270
271 case LiveRegMatrix::IK_VirtReg:
272 // Only virtual registers in the way, we may be able to spill them.
273 PhysRegSpillCands.push_back(Elt: PhysReg);
274 continue;
275
276 default:
277 // RegMask or RegUnit interference.
278 continue;
279 }
280 }
281
282 // Try to spill another interfering reg with less spill weight.
283 for (MCRegister &PhysReg : PhysRegSpillCands) {
284 if (!spillInterferences(VirtReg, PhysReg, SplitVRegs))
285 continue;
286
287 assert(!Matrix->checkInterference(VirtReg, PhysReg) &&
288 "Interference after spill.");
289 // Tell the caller to allocate to this newly freed physical register.
290 return PhysReg;
291 }
292
293 // No other spill candidates were found, so spill the current VirtReg.
294 LLVM_DEBUG(dbgs() << "spilling: " << VirtReg << '\n');
295 if (!VirtReg.isSpillable())
296 return ~0u;
297 LiveRangeEdit LRE(&VirtReg, SplitVRegs, *MF, *LIS, VRM, this, &DeadRemats);
298 spiller().spill(LRE);
299
300 // The live virtual register requesting allocation was spilled, so tell
301 // the caller not to allocate anything during this round.
302 return 0;
303}
304
305bool RABasic::runOnMachineFunction(MachineFunction &mf) {
306 LLVM_DEBUG(dbgs() << "********** BASIC REGISTER ALLOCATION **********\n"
307 << "********** Function: " << mf.getName() << '\n');
308
309 MF = &mf;
310 RegAllocBase::init(vrm&: getAnalysis<VirtRegMap>(),
311 lis&: getAnalysis<LiveIntervalsWrapperPass>().getLIS(),
312 mat&: getAnalysis<LiveRegMatrix>());
313 VirtRegAuxInfo VRAI(
314 *MF, *LIS, *VRM, getAnalysis<MachineLoopInfoWrapperPass>().getLI(),
315 getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI());
316 VRAI.calculateSpillWeightsAndHints();
317
318 SpillerInstance.reset(p: createInlineSpiller(Pass&: *this, MF&: *MF, VRM&: *VRM, VRAI));
319
320 allocatePhysRegs();
321 postOptimization();
322
323 // Diagnostic output before rewriting
324 LLVM_DEBUG(dbgs() << "Post alloc VirtRegMap:\n" << *VRM << "\n");
325
326 releaseMemory();
327 return true;
328}
329
330FunctionPass* llvm::createBasicRegisterAllocator() {
331 return new RABasic();
332}
333
334FunctionPass *llvm::createBasicRegisterAllocator(RegAllocFilterFunc F) {
335 return new RABasic(F);
336}
337