LLVM 24.0.0git
RegisterClassInfo.h
Go to the documentation of this file.
1//===- RegisterClassInfo.h - Dynamic Register Class Info --------*- C++ -*-===//
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 and reserved
11// registers depends on calling conventions and other dynamic information, so
12// some things cannot be determined statically.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_CODEGEN_REGISTERCLASSINFO_H
17#define LLVM_CODEGEN_REGISTERCLASSINFO_H
18
19#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/BitVector.h"
25#include "llvm/MC/MCRegister.h"
27#include <cstdint>
28#include <memory>
29
30namespace llvm {
31
33
35 struct RCInfo {
36 unsigned Tag = 0;
37 unsigned NumRegs = 0;
38 bool ProperSubClass = false;
39 uint8_t MinCost = 0;
40 uint16_t LastCostChange = 0;
41 std::unique_ptr<MCPhysReg[]> Order;
42
43 RCInfo() = default;
44
45 operator ArrayRef<MCPhysReg>() const {
46 return ArrayRef(Order.get(), NumRegs);
47 }
48 };
49
50 // Brief cached information for each register class.
51 std::unique_ptr<RCInfo[]> RegClass;
52
53 // Tag changes whenever cached information needs to be recomputed. An RCInfo
54 // entry is valid when its tag matches.
55 unsigned Tag = 0;
56
57 bool Reverse = false;
58
59 const MachineFunction *MF = nullptr;
60 const TargetRegisterInfo *TRI = nullptr;
61
62 // Callee saved registers of last MF.
63 // Used only to determine if an update for CalleeSavedAliases is necessary.
64 SmallVector<MCPhysReg, 16> LastCalleeSavedRegs;
65
66 // Map regunit to the callee saved Register.
67 SmallVector<MCPhysReg> CalleeSavedAliases;
68
69 // Indicate if a specified callee saved register be in the allocation order
70 // exactly as written in the tablegen descriptions or listed later.
71 BitVector IgnoreCSRForAllocOrder;
72
73 // Reserved registers in the current MF.
74 BitVector Reserved;
75
76 std::unique_ptr<unsigned[]> PSetLimits;
77
78 // The register cost values.
79 ArrayRef<uint8_t> RegCosts;
80
81 // Compute all information about RC.
82 LLVM_ABI void compute(const TargetRegisterClass *RC) const;
83
84 // Return an up-to-date RCInfo for RC.
85 const RCInfo &get(const TargetRegisterClass *RC) const {
86 const RCInfo &RCI = RegClass[RC->getID()];
87 if (Tag != RCI.Tag)
88 compute(RC);
89 return RCI;
90 }
91
92public:
94
95 /// runOnFunction - Prepare to answer questions about MF. Rev indicates to
96 /// use reversed raw order when compute register order. This must be called
97 /// before any other methods are used.
99 bool Rev = false);
100
101 /// Update cached register class information using ReservedInput, MRI's
102 /// current reserved-register set. Cached orders are compacted when
103 /// only registers are added and `-stress-regalloc` is disabled; otherwise,
104 /// they are invalidated and recomputed on demand.
105 ///
106 /// RegisterClassInfo must be initialized. Target information, callee-saved
107 /// registers, register costs, and allocation orders must remain unchanged.
108 LLVM_ABI void updateReservedRegs(const BitVector &ReservedInput);
109
110 /// Whether the cached information was filtered by MRI's frozen reserved
111 /// registers, i.e. no change to them was left unpublished.
112 LLVM_ABI bool hasCurrentReservedRegs() const;
113
115 MachineFunctionAnalysisManager::Invalidator &) {
117 return !PAC.preservedWhenStateless();
118 }
119
120 /// getNumAllocatableRegs - Returns the number of actually allocatable
121 /// registers in RC in the current function.
122 unsigned getNumAllocatableRegs(const TargetRegisterClass *RC) const {
123 return get(RC).NumRegs;
124 }
125
126 /// getOrder - Returns the preferred allocation order for RC. The order
127 /// contains no reserved registers, and registers that alias callee saved
128 /// registers come last.
130 return get(RC);
131 }
132
133 /// isProperSubClass - Returns true if RC has a legal super-class with more
134 /// allocatable registers.
135 ///
136 /// Register classes like GR32_NOSP are not proper sub-classes because %esp
137 /// is not allocatable. Similarly, tGPR is not a proper sub-class in Thumb
138 /// mode because the GPR super-class is not legal.
139 bool isProperSubClass(const TargetRegisterClass *RC) const {
140 return get(RC).ProperSubClass;
141 }
142
143 /// Return the callee-saved register containing \p Unit, or NoRegister if
144 /// there is none.
145 MCRegister getCalleeSavedAlias(MCRegUnit Unit) const {
146 return CalleeSavedAliases[static_cast<unsigned>(Unit)];
147 }
148
149 /// getLastCalleeSavedAlias - Returns the last callee saved register that
150 /// overlaps PhysReg, or NoRegister if PhysReg doesn't overlap a
151 /// CalleeSavedAliases.
153 MCRegister CSR;
154 for (MCRegUnit Unit : TRI->regunits(PhysReg)) {
155 CSR = getCalleeSavedAlias(Unit);
156 if (CSR)
157 break;
158 }
159 return CSR;
160 }
161
162 /// Get the minimum register cost in RC's allocation order.
163 /// This is the smallest value in RegCosts[Reg] for all
164 /// the registers in getOrder(RC).
166 return get(RC).MinCost;
167 }
168
169 /// Get the position of the last cost change in getOrder(RC).
170 ///
171 /// All registers in getOrder(RC).slice(getLastCostChange(RC)) will have the
172 /// same cost according to RegCosts[Reg].
173 unsigned getLastCostChange(const TargetRegisterClass *RC) const {
174 return get(RC).LastCostChange;
175 }
176
177 /// Get the register unit limit for the given pressure set index.
178 ///
179 /// RegisterClassInfo adjusts this limit for reserved registers.
180 unsigned getRegPressureSetLimit(unsigned Idx) const {
181 if (!PSetLimits[Idx])
182 PSetLimits[Idx] = computePSetLimit(Idx);
183 return PSetLimits[Idx];
184 }
185
186protected:
187 LLVM_ABI unsigned computePSetLimit(unsigned Idx) const;
188};
189
191 : public AnalysisInfoMixin<MachineRegisterClassAnalysis> {
193
194 static AnalysisKey Key;
195
196public:
198
200};
201
203 : public MachineFunctionPass {
204 virtual void anchor();
205
207
208public:
209 static char ID;
210
212
213 void getAnalysisUsage(AnalysisUsage &AU) const override {
214 AU.setPreservesAll();
216 }
217
218 bool runOnMachineFunction(MachineFunction &MF) override;
219
220 RegisterClassInfo &getRCI() { return RCI; }
221 const RegisterClassInfo &getRCI() const { return RCI; }
222};
223
224} // end namespace llvm
225
226#endif // LLVM_CODEGEN_REGISTERCLASSINFO_H
This file implements the BitVector class.
#define LLVM_ABI
Definition Compiler.h:211
This file defines the SmallVector class.
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
unsigned getID() const
getID() - Return the register class ID number.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
LLVM_ABI Result run(MachineFunction &, MachineFunctionAnalysisManager &)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const RegisterClassInfo & getRCI() const
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition Analysis.h:275
bool invalidate(MachineFunction &, const PreservedAnalyses &PA, MachineFunctionAnalysisManager::Invalidator &)
unsigned getLastCostChange(const TargetRegisterClass *RC) const
Get the position of the last cost change in getOrder(RC).
bool isProperSubClass(const TargetRegisterClass *RC) const
isProperSubClass - Returns true if RC has a legal super-class with more allocatable registers.
unsigned getNumAllocatableRegs(const TargetRegisterClass *RC) const
getNumAllocatableRegs - Returns the number of actually allocatable registers in RC in the current fun...
MCRegister getCalleeSavedAlias(MCRegUnit Unit) const
Return the callee-saved register containing Unit, or NoRegister if there is none.
unsigned getRegPressureSetLimit(unsigned Idx) const
Get the register unit limit for the given pressure set index.
LLVM_ABI void runOnMachineFunction(const MachineFunction &MF, bool Rev=false)
runOnFunction - Prepare to answer questions about MF.
LLVM_ABI void updateReservedRegs(const BitVector &ReservedInput)
Update cached register class information using ReservedInput, MRI's current reserved-register set.
uint8_t getMinCost(const TargetRegisterClass *RC) const
Get the minimum register cost in RC's allocation order.
MCRegister getLastCalleeSavedAlias(MCRegister PhysReg) const
getLastCalleeSavedAlias - Returns the last callee saved register that overlaps PhysReg,...
LLVM_ABI RegisterClassInfo()
LLVM_ABI bool hasCurrentReservedRegs() const
Whether the cached information was filtered by MRI's frozen reserved registers, i....
ArrayRef< MCPhysReg > getOrder(const TargetRegisterClass *RC) const
getOrder - Returns the preferred allocation order for RC.
LLVM_ABI unsigned computePSetLimit(unsigned Idx) const
This is not accurate because two overlapping register sets may have some nonoverlapping reserved regi...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
ArrayRef(const T &OneElt) -> ArrayRef< T >
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
A CRTP mix-in that provides informational APIs needed for analysis passes.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29