LLVM 24.0.0git
RegisterClassInfo.cpp
Go to the documentation of this file.
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
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/BitVector.h"
27#include "llvm/Support/Debug.h"
29#include <algorithm>
30#include <cassert>
31#include <cstdint>
32
33using namespace llvm;
34
35#define DEBUG_TYPE "regalloc"
36
38StressRA("stress-regalloc", cl::Hidden, cl::init(0), cl::value_desc("N"),
39 cl::desc("Limit all regclasses to N registers"));
40
42
44 bool Rev) {
45 bool Update = false;
46 MF = &mf;
47
48 auto &STI = MF->getSubtarget();
49
50 // Allocate new array the first time we see a new target.
51 if (STI.getRegisterInfo() != TRI || Reverse != Rev) {
52 Reverse = Rev;
53 TRI = STI.getRegisterInfo();
54 RegClass.reset(new RCInfo[TRI->getNumRegClasses()]);
55 Update = true;
56 }
57
58 // Test if CSRs have changed from the previous function.
59 const MachineRegisterInfo &MRI = MF->getRegInfo();
60 const MCPhysReg *CSR = MRI.getCalleeSavedRegs();
61 bool CSRChanged = true;
62 if (!Update) {
63 CSRChanged = false;
64 size_t LastSize = LastCalleeSavedRegs.size();
65 for (unsigned I = 0;; ++I) {
66 if (CSR[I] == 0) {
67 CSRChanged = I != LastSize;
68 break;
69 }
70 if (I >= LastSize) {
71 CSRChanged = true;
72 break;
73 }
74 if (CSR[I] != LastCalleeSavedRegs[I]) {
75 CSRChanged = true;
76 break;
77 }
78 }
79 }
80
81 // Get the callee saved registers.
82 if (CSRChanged) {
83 LastCalleeSavedRegs.clear();
84 // Build a CSRAlias map. Every CSR alias saves the last
85 // overlapping CSR.
86 CalleeSavedAliases.assign(TRI->getNumRegUnits(), 0);
87 for (const MCPhysReg *I = CSR; *I; ++I) {
88 for (MCRegUnit U : TRI->regunits(*I))
89 CalleeSavedAliases[static_cast<unsigned>(U)] = *I;
90 LastCalleeSavedRegs.push_back(*I);
91 }
92
93 Update = true;
94 }
95
96 // Even if CSR list is same, we could have had a different allocation order
97 // if ignoreCSRForAllocationOrder is evaluated differently.
98 BitVector CSRHintsForAllocOrder(TRI->getNumRegs());
99 for (const MCPhysReg *I = CSR; *I; ++I)
100 for (MCRegAliasIterator AI(*I, TRI, true); AI.isValid(); ++AI)
101 CSRHintsForAllocOrder[(*AI).id()] =
102 STI.ignoreCSRForAllocationOrder(mf, *AI);
103 if (IgnoreCSRForAllocOrder != CSRHintsForAllocOrder) {
104 Update = true;
105 IgnoreCSRForAllocOrder = std::move(CSRHintsForAllocOrder);
106 }
107
108 RegCosts = TRI->getRegisterCosts(*MF);
109
110 // Different reserved registers?
111 const BitVector &RR = MF->getRegInfo().getReservedRegs();
112 if (RR != Reserved) {
113 Update = true;
114 Reserved = RR;
115 }
116
117 // Invalidate cached information from previous function.
118 if (Update) {
119 unsigned NumPSets = TRI->getNumRegPressureSets();
120 PSetLimits.reset(new unsigned[NumPSets]);
121 std::fill(&PSetLimits[0], &PSetLimits[NumPSets], 0);
122 ++Tag;
123 }
124}
125
126/// compute - Compute the preferred allocation order for RC with reserved
127/// registers filtered out. Volatile registers come first followed by CSR
128/// aliases ordered according to the CSR order specified by the target.
129void RegisterClassInfo::compute(const TargetRegisterClass *RC) const {
130 assert(RC && "no register class given");
131 RCInfo &RCI = RegClass[RC->getID()];
132 auto &STI = MF->getSubtarget();
133
134 // Raw register count, including all reserved regs.
135 unsigned NumRegs = RC->getNumRegs();
136
137 if (!RCI.Order)
138 RCI.Order.reset(new MCPhysReg[NumRegs]);
139
140 unsigned N = 0;
142 uint8_t MinCost = uint8_t(~0u);
143 uint8_t LastCost = uint8_t(~0u);
144 unsigned LastCostChange = 0;
145
146 // FIXME: Once targets reserve registers instead of removing them from the
147 // allocation order, we can simply use begin/end here.
148 ArrayRef<MCPhysReg> RawOrder = TRI->getRawAllocationOrder(*RC, *MF, Reverse);
149 for (unsigned PhysReg : reverse_conditionally(RawOrder, Reverse)) {
150 // Remove reserved registers from the allocation order.
151 if (Reserved.test(PhysReg))
152 continue;
153 uint8_t Cost = RegCosts[PhysReg];
154 MinCost = std::min(MinCost, Cost);
155
156 if (getLastCalleeSavedAlias(PhysReg) &&
157 !STI.ignoreCSRForAllocationOrder(*MF, PhysReg))
158 // PhysReg aliases a CSR, save it for later.
159 CSRAlias.push_back(PhysReg);
160 else {
161 if (Cost != LastCost)
162 LastCostChange = N;
163 RCI.Order[N++] = PhysReg;
164 LastCost = Cost;
165 }
166 }
167 RCI.NumRegs = N + CSRAlias.size();
168 assert(RCI.NumRegs <= NumRegs && "Allocation order larger than regclass");
169
170 // CSR aliases go after the volatile registers, preserve the target's order.
171 for (unsigned PhysReg : CSRAlias) {
172 uint8_t Cost = RegCosts[PhysReg];
173 if (Cost != LastCost)
174 LastCostChange = N;
175 RCI.Order[N++] = PhysReg;
176 LastCost = Cost;
177 }
178
179 // Register allocator stress test. Clip register class to N registers.
180 if (StressRA && RCI.NumRegs > StressRA)
181 RCI.NumRegs = StressRA;
182
183 // Check if RC is a proper sub-class.
184 if (const TargetRegisterClass *Super =
185 TRI->getLargestLegalSuperClass(RC, *MF))
186 if (Super != RC && getNumAllocatableRegs(Super) > RCI.NumRegs)
187 RCI.ProperSubClass = true;
188
189 RCI.MinCost = MinCost;
190 RCI.LastCostChange = LastCostChange;
191
192 LLVM_DEBUG({
193 dbgs() << "AllocationOrder(" << TRI->getRegClassName(RC) << ") = [";
194 for (unsigned I = 0; I != RCI.NumRegs; ++I)
195 dbgs() << ' ' << printReg(RCI.Order[I], TRI);
196 dbgs() << (RCI.ProperSubClass ? " ] (sub-class)\n" : " ]\n");
197 });
198
199 // RCI is now up-to-date.
200 RCI.Tag = Tag;
201}
202
203/// This is not accurate because two overlapping register sets may have some
204/// nonoverlapping reserved registers. However, computing the allocation order
205/// for all register classes would be too expensive.
206unsigned RegisterClassInfo::computePSetLimit(unsigned Idx) const {
207 const TargetRegisterClass *RC = TRI->getLargestRegClassForRegPressureSet(Idx);
208 assert(RC && "Failed to find register class");
209 compute(RC);
210 unsigned NAllocatableRegs = getNumAllocatableRegs(RC);
211 unsigned RegPressureSetLimit = TRI->getRegPressureSetLimit(*MF, Idx);
212 // If all the regs are reserved, return raw RegPressureSetLimit.
213 // One example is VRSAVERC in PowerPC.
214 // Avoid returning zero, getRegPressureSetLimit(Idx) assumes computePSetLimit
215 // return non-zero value.
216 if (NAllocatableRegs == 0)
217 return RegPressureSetLimit;
218 unsigned NReserved = RC->getNumRegs() - NAllocatableRegs;
219 return RegPressureSetLimit - TRI->getRegClassWeight(RC).RegWeight * NReserved;
220}
221
223 "machine-register-class-info",
224 "Machine Register Class Info Analysis", true, true)
225
230 RCI.runOnMachineFunction(MF);
231 return RCI;
232}
233
235
241
243 MachineFunction &MF) {
244 RCI.runOnMachineFunction(MF);
245 return false;
246}
247
248void MachineRegisterClassInfoWrapperPass::anchor() {}
249
250AnalysisKey MachineRegisterClassAnalysis::Key;
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements the BitVector class.
#define I(x, y, z)
Definition MD5.cpp:57
Register const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static cl::opt< unsigned > StressRA("stress-regalloc", cl::Hidden, cl::init(0), cl::value_desc("N"), cl::desc("Limit all regclasses to N registers"))
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
MCRegAliasIterator enumerates all registers aliasing Reg.
unsigned getID() const
getID() - Return the register class ID number.
unsigned getNumRegs() const
getNumRegs - Return the number of registers in this class.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI const MCPhysReg * getCalleeSavedRegs() const
Returns list of callee saved registers.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
unsigned getNumAllocatableRegs(const TargetRegisterClass *RC) const
getNumAllocatableRegs - Returns the number of actually allocatable registers in RC in the current fun...
LLVM_ABI void runOnMachineFunction(const MachineFunction &MF, bool Rev=false)
runOnFunction - Prepare to answer questions about MF.
MCRegister getLastCalleeSavedAlias(MCRegister PhysReg) const
getLastCalleeSavedAlias - Returns the last callee saved register that overlaps PhysReg,...
LLVM_ABI RegisterClassInfo()
LLVM_ABI unsigned computePSetLimit(unsigned Idx) const
This is not accurate because two overlapping register sets may have some nonoverlapping reserved regi...
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition Registry.h:116
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
InstructionCost Cost
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI void initializeMachineRegisterClassInfoWrapperPassPass(PassRegistry &)
auto reverse_conditionally(ContainerTy &&C, bool ShouldReverse)
Return a range that conditionally reverses C.
Definition STLExtras.h:1423
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
#define N
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29