1//===- RegisterClassInfo.cpp - Dynamic Register Class Info ----------------===//
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 implements the RegisterClassInfo class which provides dynamic
10// information about target register classes. Callee-saved vs. caller-saved and
11// reserved registers depend on calling conventions and other dynamic
12// information, so some things cannot be determined statically.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/CodeGen/RegisterClassInfo.h"
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/BitVector.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
22#include "llvm/CodeGen/TargetRegisterInfo.h"
23#include "llvm/CodeGen/TargetSubtargetInfo.h"
24#include "llvm/InitializePasses.h"
25#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/raw_ostream.h"
28#include <algorithm>
29#include <cassert>
30#include <cstdint>
31
32using namespace llvm;
33
34#define DEBUG_TYPE "regalloc"
35
36static cl::opt<unsigned>
37StressRA("stress-regalloc", cl::Hidden, cl::init(Val: 0), cl::value_desc("N"),
38 cl::desc("Limit all regclasses to N registers"));
39
40RegisterClassInfo::RegisterClassInfo() = default;
41
42void RegisterClassInfo::runOnMachineFunction(const MachineFunction &mf,
43 bool Rev) {
44 bool Update = false;
45 MF = &mf;
46
47 auto &STI = MF->getSubtarget();
48
49 // Allocate new array the first time we see a new target.
50 if (STI.getRegisterInfo() != TRI || Reverse != Rev) {
51 Reverse = Rev;
52 TRI = STI.getRegisterInfo();
53 RegClass.reset(p: new RCInfo[TRI->getNumRegClasses()]);
54 Update = true;
55 }
56
57 // Test if CSRs have changed from the previous function.
58 const MachineRegisterInfo &MRI = MF->getRegInfo();
59 const MCPhysReg *CSR = MRI.getCalleeSavedRegs();
60 bool CSRChanged = true;
61 if (!Update) {
62 CSRChanged = false;
63 size_t LastSize = LastCalleeSavedRegs.size();
64 for (unsigned I = 0;; ++I) {
65 if (CSR[I] == 0) {
66 CSRChanged = I != LastSize;
67 break;
68 }
69 if (I >= LastSize) {
70 CSRChanged = true;
71 break;
72 }
73 if (CSR[I] != LastCalleeSavedRegs[I]) {
74 CSRChanged = true;
75 break;
76 }
77 }
78 }
79
80 // Get the callee saved registers.
81 if (CSRChanged) {
82 LastCalleeSavedRegs.clear();
83 // Build a CSRAlias map. Every CSR alias saves the last
84 // overlapping CSR.
85 CalleeSavedAliases.assign(NumElts: TRI->getNumRegUnits(), Elt: 0);
86 for (const MCPhysReg *I = CSR; *I; ++I) {
87 for (MCRegUnit U : TRI->regunits(Reg: *I))
88 CalleeSavedAliases[static_cast<unsigned>(U)] = *I;
89 LastCalleeSavedRegs.push_back(Elt: *I);
90 }
91
92 Update = true;
93 }
94
95 // Even if CSR list is same, we could have had a different allocation order
96 // if the target's CSR allocation-order mask changes.
97 BitVector CSRHintsForAllocOrder;
98 STI.getCSRAllocationOrderMask(MF: mf, Mask&: CSRHintsForAllocOrder);
99 if (IgnoreCSRForAllocOrder != CSRHintsForAllocOrder) {
100 Update = true;
101 IgnoreCSRForAllocOrder = std::move(CSRHintsForAllocOrder);
102 }
103
104 RegCosts = TRI->getRegisterCosts(MF: *MF);
105
106 // Different reserved registers?
107 const BitVector &RR = MF->getRegInfo().getReservedRegs();
108 if (RR != Reserved) {
109 Update = true;
110 Reserved = RR;
111 }
112
113 // Invalidate cached information from previous function.
114 if (Update) {
115 unsigned NumPSets = TRI->getNumRegPressureSets();
116 PSetLimits.reset(p: new unsigned[NumPSets]);
117 std::fill(first: &PSetLimits[0], last: &PSetLimits[NumPSets], value: 0);
118 ++Tag;
119 }
120}
121
122bool RegisterClassInfo::hasCurrentReservedRegs() const {
123 return Reserved == MF->getRegInfo().getReservedRegs();
124}
125
126void RegisterClassInfo::updateReservedRegs(const BitVector &ReservedInput) {
127 assert(MF && TRI && RegClass &&
128 "RegisterClassInfo must be initialized before updating reserved regs");
129 assert(ReservedInput.size() == Reserved.size() &&
130 "Reserved register bit vectors must have the same size");
131 if (ReservedInput == Reserved)
132 return;
133
134 // Cached orders cannot regain unreserved registers; recompute them lazily.
135 bool OnlyNewReservations = Reserved.subsetOf(RHS: ReservedInput);
136
137 Reserved = ReservedInput;
138
139 // Pressure limits depend on the number of allocatable registers.
140 std::fill_n(first: PSetLimits.get(), n: TRI->getNumRegPressureSets(), value: 0);
141
142 // NumRegs may hide entries beyond the stress limit, so those orders cannot
143 // safely be compacted using only their visible prefix.
144 if (!OnlyNewReservations || StressRA) {
145 ++Tag;
146 return;
147 }
148
149 for (const TargetRegisterClass &RC : TRI->regclasses()) {
150 RCInfo &Info = RegClass[RC.getID()];
151
152 // Skip stale class information.
153 if (Info.Tag != Tag)
154 continue;
155
156 unsigned NewNumRegs = 0;
157 uint8_t MinCost = uint8_t(~0u);
158 uint8_t LastCost = uint8_t(~0u);
159 unsigned LastCostChange = 0;
160
161 for (unsigned I = 0; I != Info.NumRegs; ++I) {
162 MCPhysReg PhysReg = Info.Order[I];
163 if (Reserved.test(Idx: PhysReg))
164 continue;
165
166 uint8_t Cost = RegCosts[PhysReg];
167 MinCost = std::min(a: MinCost, b: Cost);
168 if (Cost != LastCost)
169 LastCostChange = NewNumRegs;
170
171 Info.Order[NewNumRegs++] = PhysReg;
172 LastCost = Cost;
173 }
174
175 Info.NumRegs = NewNumRegs;
176 Info.MinCost = MinCost;
177 Info.LastCostChange = LastCostChange;
178
179 Info.ProperSubClass = false;
180 if (const TargetRegisterClass *Super =
181 TRI->getLargestLegalSuperClass(RC: &RC, *MF))
182 if (Super != &RC && getNumAllocatableRegs(RC: Super) > Info.NumRegs)
183 Info.ProperSubClass = true;
184 }
185}
186
187/// compute - Compute the preferred allocation order for RC with reserved
188/// registers filtered out. Volatile registers come first followed by CSR
189/// aliases ordered according to the CSR order specified by the target.
190void RegisterClassInfo::compute(const TargetRegisterClass *RC) const {
191 assert(RC && "no register class given");
192 RCInfo &RCI = RegClass[RC->getID()];
193
194 // Raw register count, including all reserved regs.
195 unsigned NumRegs = RC->getNumRegs();
196
197 if (!RCI.Order)
198 RCI.Order.reset(p: new MCPhysReg[NumRegs]);
199
200 unsigned N = 0;
201 SmallVector<MCPhysReg, 16> CSRAlias;
202 uint8_t MinCost = uint8_t(~0u);
203 uint8_t LastCost = uint8_t(~0u);
204 unsigned LastCostChange = 0;
205
206 // FIXME: Once targets reserve registers instead of removing them from the
207 // allocation order, we can simply use begin/end here.
208 ArrayRef<MCPhysReg> RawOrder = TRI->getRawAllocationOrder(RC: *RC, *MF, Reverse);
209 for (unsigned PhysReg : reverse_conditionally(C&: RawOrder, ShouldReverse: Reverse)) {
210 // Remove reserved registers from the allocation order.
211 if (Reserved.test(Idx: PhysReg))
212 continue;
213 uint8_t Cost = RegCosts[PhysReg];
214 MinCost = std::min(a: MinCost, b: Cost);
215
216 if (getLastCalleeSavedAlias(PhysReg) &&
217 (IgnoreCSRForAllocOrder.empty() ||
218 !IgnoreCSRForAllocOrder.test(Idx: PhysReg)))
219 // PhysReg aliases a CSR, save it for later.
220 CSRAlias.push_back(Elt: PhysReg);
221 else {
222 if (Cost != LastCost)
223 LastCostChange = N;
224 RCI.Order[N++] = PhysReg;
225 LastCost = Cost;
226 }
227 }
228 RCI.NumRegs = N + CSRAlias.size();
229 assert(RCI.NumRegs <= NumRegs && "Allocation order larger than regclass");
230
231 // CSR aliases go after the volatile registers, preserve the target's order.
232 for (unsigned PhysReg : CSRAlias) {
233 uint8_t Cost = RegCosts[PhysReg];
234 if (Cost != LastCost)
235 LastCostChange = N;
236 RCI.Order[N++] = PhysReg;
237 LastCost = Cost;
238 }
239
240 // Register allocator stress test. Clip register class to N registers.
241 if (StressRA && RCI.NumRegs > StressRA)
242 RCI.NumRegs = StressRA;
243
244 // Check if RC is a proper sub-class.
245 if (const TargetRegisterClass *Super =
246 TRI->getLargestLegalSuperClass(RC, *MF))
247 if (Super != RC && getNumAllocatableRegs(RC: Super) > RCI.NumRegs)
248 RCI.ProperSubClass = true;
249
250 RCI.MinCost = MinCost;
251 RCI.LastCostChange = LastCostChange;
252
253 LLVM_DEBUG({
254 dbgs() << "AllocationOrder(" << TRI->getRegClassName(RC) << ") = [";
255 for (unsigned I = 0; I != RCI.NumRegs; ++I)
256 dbgs() << ' ' << printReg(RCI.Order[I], TRI);
257 dbgs() << (RCI.ProperSubClass ? " ] (sub-class)\n" : " ]\n");
258 });
259
260 // RCI is now up-to-date.
261 RCI.Tag = Tag;
262}
263
264/// This is not accurate because two overlapping register sets may have some
265/// nonoverlapping reserved registers. However, computing the allocation order
266/// for all register classes would be too expensive.
267unsigned RegisterClassInfo::computePSetLimit(unsigned Idx) const {
268 const TargetRegisterClass *RC = TRI->getLargestRegClassForRegPressureSet(Idx);
269 assert(RC && "Failed to find register class");
270 unsigned NAllocatableRegs = getNumAllocatableRegs(RC);
271 unsigned RegPressureSetLimit = TRI->getRegPressureSetLimit(MF: *MF, Idx);
272 // If all the regs are reserved, return raw RegPressureSetLimit.
273 // One example is VRSAVERC in PowerPC.
274 // Avoid returning zero, getRegPressureSetLimit(Idx) assumes computePSetLimit
275 // return non-zero value.
276 if (NAllocatableRegs == 0)
277 return RegPressureSetLimit;
278 unsigned NReserved = RC->getNumRegs() - NAllocatableRegs;
279 unsigned ReservedRegWeight = TRI->getRegClassWeight(RC).RegWeight * NReserved;
280 // A target-provided limit may already account for restricted register
281 // availability, such as an AMDGPU occupancy requirement. If the additional
282 // reserved-register adjustment would not leave a positive limit, bound the
283 // target limit by the remaining allocatable register weight.
284 if (ReservedRegWeight >= RegPressureSetLimit) {
285 return std::min(a: RegPressureSetLimit,
286 b: NAllocatableRegs * TRI->getRegClassWeight(RC).RegWeight);
287 }
288 return RegPressureSetLimit - ReservedRegWeight;
289}
290
291INITIALIZE_PASS(MachineRegisterClassInfoWrapperPass,
292 "machine-register-class-info",
293 "Machine Register Class Info Analysis", true, true)
294
295MachineRegisterClassAnalysis::Result
296MachineRegisterClassAnalysis::run(MachineFunction &MF,
297 MachineFunctionAnalysisManager &) {
298 RegisterClassInfo RCI;
299 RCI.runOnMachineFunction(mf: MF);
300 return RCI;
301}
302
303char MachineRegisterClassInfoWrapperPass::ID = 0;
304
305MachineRegisterClassInfoWrapperPass::MachineRegisterClassInfoWrapperPass()
306 : MachineFunctionPass(ID), RCI() {
307 PassRegistry &Registry = *PassRegistry::getPassRegistry();
308 initializeMachineRegisterClassInfoWrapperPassPass(Registry);
309}
310
311bool MachineRegisterClassInfoWrapperPass::runOnMachineFunction(
312 MachineFunction &MF) {
313 RCI.runOnMachineFunction(mf: MF);
314 return false;
315}
316
317void MachineRegisterClassInfoWrapperPass::anchor() {}
318
319AnalysisKey MachineRegisterClassAnalysis::Key;
320