LLVM 22.0.0git
VirtRegMap.h
Go to the documentation of this file.
1//===- llvm/CodeGen/VirtRegMap.h - Virtual Register Map ---------*- 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 a virtual register map. This maps virtual registers to
10// physical registers and virtual registers to stack slots. It is created and
11// updated by a register allocator and then used by a machine code rewriter that
12// adds spill code and rewrites virtual into physical register references.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_CODEGEN_VIRTREGMAP_H
17#define LLVM_CODEGEN_VIRTREGMAP_H
18
19#include "llvm/ADT/IndexedMap.h"
23#include "llvm/IR/PassManager.h"
24#include "llvm/Pass.h"
26#include <cassert>
27
28namespace llvm {
29
30class MachineFunction;
31class MachineRegisterInfo;
32class raw_ostream;
33class TargetInstrInfo;
34
36 MachineRegisterInfo *MRI = nullptr;
37 const TargetInstrInfo *TII = nullptr;
38 const TargetRegisterInfo *TRI = nullptr;
39 MachineFunction *MF = nullptr;
40
41 /// Virt2PhysMap - This is a virtual to physical register
42 /// mapping. Each virtual register is required to have an entry in
43 /// it; even spilled virtual registers (the register mapped to a
44 /// spilled register is the temporary used to load it from the
45 /// stack).
47
48 /// Virt2StackSlotMap - This is virtual register to stack slot
49 /// mapping. Each spilled virtual register has an entry in it
50 /// which corresponds to the stack slot this register is spilled
51 /// at.
53
54 /// Virt2SplitMap - This is virtual register to splitted virtual register
55 /// mapping.
57
58 /// Virt2ShapeMap - For X86 AMX register whose register is bound shape
59 /// information.
60 DenseMap<Register, ShapeT> Virt2ShapeMap;
61
62 /// createSpillSlot - Allocate a spill slot for RC from MFI.
63 unsigned createSpillSlot(const TargetRegisterClass *RC);
64
65public:
66 static constexpr int NO_STACK_SLOT = INT_MAX;
67
68 VirtRegMap() : Virt2StackSlotMap(NO_STACK_SLOT) {}
69 VirtRegMap(const VirtRegMap &) = delete;
70 VirtRegMap &operator=(const VirtRegMap &) = delete;
71 VirtRegMap(VirtRegMap &&) = default;
72
74
76 assert(MF && "getMachineFunction called before runOnMachineFunction");
77 return *MF;
78 }
79
80 MachineRegisterInfo &getRegInfo() const { return *MRI; }
81 const TargetRegisterInfo &getTargetRegInfo() const { return *TRI; }
82
83 LLVM_ABI void grow();
84
85 /// returns true if the specified virtual register is
86 /// mapped to a physical register
87 bool hasPhys(Register virtReg) const { return getPhys(virtReg).isValid(); }
88
89 /// returns the physical register mapped to the specified
90 /// virtual register
91 MCRegister getPhys(Register virtReg) const {
92 assert(virtReg.isVirtual());
93 return Virt2PhysMap[virtReg];
94 }
95
96 /// creates a mapping for the specified virtual register to
97 /// the specified physical register
98 LLVM_ABI void assignVirt2Phys(Register virtReg, MCRegister physReg);
99
100 bool isShapeMapEmpty() const { return Virt2ShapeMap.empty(); }
101
102 bool hasShape(Register virtReg) const {
103 return Virt2ShapeMap.contains(virtReg);
104 }
105
106 ShapeT getShape(Register virtReg) const {
107 assert(virtReg.isVirtual());
108 return Virt2ShapeMap.lookup(virtReg);
109 }
110
111 void assignVirt2Shape(Register virtReg, ShapeT shape) {
112 Virt2ShapeMap[virtReg] = shape;
113 }
114
115 /// clears the specified virtual register's, physical
116 /// register mapping
117 void clearVirt(Register virtReg) {
118 assert(virtReg.isVirtual());
119 assert(Virt2PhysMap[virtReg] &&
120 "attempt to clear a not assigned virtual register");
121 Virt2PhysMap[virtReg] = MCRegister();
122 }
123
124 /// clears all virtual to physical register mappings
126 Virt2PhysMap.clear();
127 grow();
128 }
129
130 /// returns true if VirtReg is assigned to its preferred physreg.
131 LLVM_ABI bool hasPreferredPhys(Register VirtReg) const;
132
133 /// returns true if VirtReg has a known preferred register.
134 /// This returns false if VirtReg has a preference that is a virtual
135 /// register that hasn't been assigned yet.
136 LLVM_ABI bool hasKnownPreference(Register VirtReg) const;
137
138 /// records virtReg is a split live interval from SReg.
139 void setIsSplitFromReg(Register virtReg, Register SReg) {
140 Virt2SplitMap[virtReg] = SReg;
141 if (hasShape(SReg)) {
142 Virt2ShapeMap[virtReg] = getShape(SReg);
143 }
144 }
145
146 /// returns the live interval virtReg is split from.
148 return Virt2SplitMap[virtReg];
149 }
150
151 /// getOriginal - Return the original virtual register that VirtReg descends
152 /// from through splitting.
153 /// A register that was not created by splitting is its own original.
154 /// This operation is idempotent.
156 Register Orig = getPreSplitReg(VirtReg);
157 return Orig ? Orig : VirtReg;
158 }
159
160 /// returns true if the specified virtual register is not
161 /// mapped to a stack slot or rematerialized.
162 bool isAssignedReg(Register virtReg) const {
163 if (getStackSlot(virtReg) == NO_STACK_SLOT)
164 return true;
165 // Split register can be assigned a physical register as well as a
166 // stack slot or remat id.
167 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg]);
168 }
169
170 /// returns the stack slot mapped to the specified virtual
171 /// register
172 int getStackSlot(Register virtReg) const {
173 assert(virtReg.isVirtual());
174 return Virt2StackSlotMap[virtReg];
175 }
176
177 /// create a mapping for the specifed virtual register to
178 /// the next available stack slot
180
181 /// create a mapping for the specified virtual register to
182 /// the specified stack slot
183 LLVM_ABI void assignVirt2StackSlot(Register virtReg, int SS);
184
185 LLVM_ABI void print(raw_ostream &OS, const Module *M = nullptr) const;
186 LLVM_ABI void dump() const;
187};
188
190 VRM.print(OS);
191 return OS;
192}
193
195 VirtRegMap VRM;
196
197public:
198 LLVM_ABI static char ID;
199
201
202 void print(raw_ostream &OS, const Module *M = nullptr) const override {
203 VRM.print(OS, M);
204 }
205
206 VirtRegMap &getVRM() { return VRM; }
207 const VirtRegMap &getVRM() const { return VRM; }
208
210 VRM.init(MF);
211 return false;
212 }
213
214 void getAnalysisUsage(AnalysisUsage &AU) const override {
215 AU.setPreservesAll();
217 }
218};
219
220class VirtRegMapAnalysis : public AnalysisInfoMixin<VirtRegMapAnalysis> {
222 LLVM_ABI static AnalysisKey Key;
223
224public:
226
229};
230
231class VirtRegMapPrinterPass : public PassInfoMixin<VirtRegMapPrinterPass> {
232 raw_ostream &OS;
233
234public:
238 static bool isRequired() { return true; }
239};
240
241class VirtRegRewriterPass : public PassInfoMixin<VirtRegRewriterPass> {
242 bool ClearVirtRegs = true;
243
244public:
245 VirtRegRewriterPass(bool ClearVirtRegs = true)
246 : ClearVirtRegs(ClearVirtRegs) {}
249
250 static bool isRequired() { return true; }
251
254
256 if (ClearVirtRegs)
257 return MachineFunctionProperties().setNoVRegs();
258 return {};
259 }
260};
261
262} // end llvm namespace
263
264#endif // LLVM_CODEGEN_VIRTREGMAP_H
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition: Compiler.h:213
This header defines various interfaces for pass management in LLVM.
This file implements an indexed map.
Register const TargetRegisterInfo * TRI
ModuleAnalysisManager MAM
raw_pwrite_stream & OS
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:203
bool empty() const
Definition: DenseMap.h:119
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition: DenseMap.h:168
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:33
constexpr bool isValid() const
Definition: MCRegister.h:76
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Properties which a MachineFunction may have at a given point in time.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:112
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition: Register.h:74
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
LLVM_ABI VirtRegMap run(MachineFunction &MF, MachineFunctionAnalysisManager &MAM)
Definition: VirtRegMap.cpp:182
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Definition: VirtRegMap.cpp:176
VirtRegMapPrinterPass(raw_ostream &OS)
Definition: VirtRegMap.h:235
const VirtRegMap & getVRM() const
Definition: VirtRegMap.h:207
void print(raw_ostream &OS, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
Definition: VirtRegMap.h:202
static LLVM_ABI char ID
Definition: VirtRegMap.h:198
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition: VirtRegMap.h:209
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Definition: VirtRegMap.h:214
LLVM_ABI bool hasKnownPreference(Register VirtReg) const
returns true if VirtReg has a known preferred register.
Definition: VirtRegMap.cpp:119
LLVM_ABI int assignVirt2StackSlot(Register virtReg)
create a mapping for the specifed virtual register to the next available stack slot
Definition: VirtRegMap.cpp:128
void clearVirt(Register virtReg)
clears the specified virtual register's, physical register mapping
Definition: VirtRegMap.h:117
int getStackSlot(Register virtReg) const
returns the stack slot mapped to the specified virtual register
Definition: VirtRegMap.h:172
void clearAllVirt()
clears all virtual to physical register mappings
Definition: VirtRegMap.h:125
void setIsSplitFromReg(Register virtReg, Register SReg)
records virtReg is a split live interval from SReg.
Definition: VirtRegMap.h:139
bool hasShape(Register virtReg) const
Definition: VirtRegMap.h:102
LLVM_ABI void init(MachineFunction &MF)
LLVM_ABI bool hasPreferredPhys(Register VirtReg) const
returns true if VirtReg is assigned to its preferred physreg.
Definition: VirtRegMap.cpp:110
Register getOriginal(Register VirtReg) const
getOriginal - Return the original virtual register that VirtReg descends from through splitting.
Definition: VirtRegMap.h:155
LLVM_ABI void dump() const
Definition: VirtRegMap.cpp:168
Register getPreSplitReg(Register virtReg) const
returns the live interval virtReg is split from.
Definition: VirtRegMap.h:147
MachineRegisterInfo & getRegInfo() const
Definition: VirtRegMap.h:80
ShapeT getShape(Register virtReg) const
Definition: VirtRegMap.h:106
LLVM_ABI void assignVirt2Phys(Register virtReg, MCRegister physReg)
creates a mapping for the specified virtual register to the specified physical register
Definition: VirtRegMap.cpp:86
MachineFunction & getMachineFunction() const
Definition: VirtRegMap.h:75
MCRegister getPhys(Register virtReg) const
returns the physical register mapped to the specified virtual register
Definition: VirtRegMap.h:91
LLVM_ABI void print(raw_ostream &OS, const Module *M=nullptr) const
Definition: VirtRegMap.cpp:146
VirtRegMap(VirtRegMap &&)=default
LLVM_ABI void grow()
Definition: VirtRegMap.cpp:79
VirtRegMap(const VirtRegMap &)=delete
bool hasPhys(Register virtReg) const
returns true if the specified virtual register is mapped to a physical register
Definition: VirtRegMap.h:87
void assignVirt2Shape(Register virtReg, ShapeT shape)
Definition: VirtRegMap.h:111
bool isAssignedReg(Register virtReg) const
returns true if the specified virtual register is not mapped to a stack slot or rematerialized.
Definition: VirtRegMap.h:162
static constexpr int NO_STACK_SLOT
Definition: VirtRegMap.h:66
const TargetRegisterInfo & getTargetRegInfo() const
Definition: VirtRegMap.h:81
VirtRegMap & operator=(const VirtRegMap &)=delete
bool isShapeMapEmpty() const
Definition: VirtRegMap.h:100
static bool isRequired()
Definition: VirtRegMap.h:250
VirtRegRewriterPass(bool ClearVirtRegs=true)
Definition: VirtRegMap.h:245
LLVM_ABI void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)>) const
Definition: VirtRegMap.cpp:775
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Definition: VirtRegMap.cpp:302
MachineFunctionProperties getSetProperties() const
Definition: VirtRegMap.h:255
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:312
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:93
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:29
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:70