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