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
111 MachineFunctionAnalysisManager::Invalidator &) {
113 return !PAC.preservedWhenStateless();
114 }
115
116 /// getNumAllocatableRegs - Returns the number of actually allocatable
117 /// registers in RC in the current function.
118 unsigned getNumAllocatableRegs(const TargetRegisterClass *RC) const {
119 return get(RC).NumRegs;
120 }
121
122 /// getOrder - Returns the preferred allocation order for RC. The order
123 /// contains no reserved registers, and registers that alias callee saved
124 /// registers come last.
126 return get(RC);
127 }
128
129 /// isProperSubClass - Returns true if RC has a legal super-class with more
130 /// allocatable registers.
131 ///
132 /// Register classes like GR32_NOSP are not proper sub-classes because %esp
133 /// is not allocatable. Similarly, tGPR is not a proper sub-class in Thumb
134 /// mode because the GPR super-class is not legal.
135 bool isProperSubClass(const TargetRegisterClass *RC) const {
136 return get(RC).ProperSubClass;
137 }
138
139 /// getLastCalleeSavedAlias - Returns the last callee saved register that
140 /// overlaps PhysReg, or NoRegister if PhysReg doesn't overlap a
141 /// CalleeSavedAliases.
143 MCRegister CSR;
144 for (MCRegUnit Unit : TRI->regunits(PhysReg)) {
145 CSR = CalleeSavedAliases[static_cast<unsigned>(Unit)];
146 if (CSR)
147 break;
148 }
149 return CSR;
150 }
151
152 /// Get the minimum register cost in RC's allocation order.
153 /// This is the smallest value in RegCosts[Reg] for all
154 /// the registers in getOrder(RC).
156 return get(RC).MinCost;
157 }
158
159 /// Get the position of the last cost change in getOrder(RC).
160 ///
161 /// All registers in getOrder(RC).slice(getLastCostChange(RC)) will have the
162 /// same cost according to RegCosts[Reg].
163 unsigned getLastCostChange(const TargetRegisterClass *RC) const {
164 return get(RC).LastCostChange;
165 }
166
167 /// Get the register unit limit for the given pressure set index.
168 ///
169 /// RegisterClassInfo adjusts this limit for reserved registers.
170 unsigned getRegPressureSetLimit(unsigned Idx) const {
171 if (!PSetLimits[Idx])
172 PSetLimits[Idx] = computePSetLimit(Idx);
173 return PSetLimits[Idx];
174 }
175
176protected:
177 LLVM_ABI unsigned computePSetLimit(unsigned Idx) const;
178};
179
181 : public AnalysisInfoMixin<MachineRegisterClassAnalysis> {
183
184 static AnalysisKey Key;
185
186public:
188
190};
191
193 virtual void anchor();
194
196
197public:
198 static char ID;
199
201
202 void getAnalysisUsage(AnalysisUsage &AU) const override {
203 AU.setPreservesAll();
205 }
206
207 bool runOnMachineFunction(MachineFunction &MF) override;
208
209 RegisterClassInfo &getRCI() { return RCI; }
210 const RegisterClassInfo &getRCI() const { return RCI; }
211};
212
213} // end namespace llvm
214
215#endif // LLVM_CODEGEN_REGISTERCLASSINFO_H
This file implements the BitVector class.
#define LLVM_ABI
Definition Compiler.h:215
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.
Result run(MachineFunction &, MachineFunctionAnalysisManager &)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const RegisterClassInfo & getRCI() const
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
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
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...
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 invalidate(MachineFunction &, const PreservedAnalyses &PA, MachineFunctionAnalysisManager::Invalidator &)
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