LLVM 20.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"
25#include <cassert>
26
27namespace llvm {
28
29class MachineFunction;
30class MachineRegisterInfo;
31class raw_ostream;
32class TargetInstrInfo;
33
35 MachineRegisterInfo *MRI = nullptr;
36 const TargetInstrInfo *TII = nullptr;
37 const TargetRegisterInfo *TRI = nullptr;
38 MachineFunction *MF = nullptr;
39
40 /// Virt2PhysMap - This is a virtual to physical register
41 /// mapping. Each virtual register is required to have an entry in
42 /// it; even spilled virtual registers (the register mapped to a
43 /// spilled register is the temporary used to load it from the
44 /// stack).
46
47 /// Virt2StackSlotMap - This is virtual register to stack slot
48 /// mapping. Each spilled virtual register has an entry in it
49 /// which corresponds to the stack slot this register is spilled
50 /// at.
52
53 /// Virt2SplitMap - This is virtual register to splitted virtual register
54 /// mapping.
56
57 /// Virt2ShapeMap - For X86 AMX register whose register is bound shape
58 /// information.
59 DenseMap<Register, ShapeT> Virt2ShapeMap;
60
61 /// createSpillSlot - Allocate a spill slot for RC from MFI.
62 unsigned createSpillSlot(const TargetRegisterClass *RC);
63
64public:
65 static constexpr int NO_STACK_SLOT = INT_MAX;
66
67 VirtRegMap() : Virt2StackSlotMap(NO_STACK_SLOT) {}
68 VirtRegMap(const VirtRegMap &) = delete;
69 VirtRegMap &operator=(const VirtRegMap &) = delete;
70 VirtRegMap(VirtRegMap &&) = default;
71
73
75 assert(MF && "getMachineFunction called before runOnMachineFunction");
76 return *MF;
77 }
78
79 MachineRegisterInfo &getRegInfo() const { return *MRI; }
80 const TargetRegisterInfo &getTargetRegInfo() const { return *TRI; }
81
82 void grow();
83
84 /// returns true if the specified virtual register is
85 /// mapped to a physical register
86 bool hasPhys(Register virtReg) const { return getPhys(virtReg).isValid(); }
87
88 /// returns the physical register mapped to the specified
89 /// virtual register
90 MCRegister getPhys(Register virtReg) const {
91 assert(virtReg.isVirtual());
92 return Virt2PhysMap[virtReg];
93 }
94
95 /// creates a mapping for the specified virtual register to
96 /// the specified physical register
97 void assignVirt2Phys(Register virtReg, MCPhysReg physReg);
98
99 bool isShapeMapEmpty() const { return Virt2ShapeMap.empty(); }
100
101 bool hasShape(Register virtReg) const {
102 return Virt2ShapeMap.contains(virtReg);
103 }
104
105 ShapeT getShape(Register virtReg) const {
106 assert(virtReg.isVirtual());
107 return Virt2ShapeMap.lookup(virtReg);
108 }
109
110 void assignVirt2Shape(Register virtReg, ShapeT shape) {
111 Virt2ShapeMap[virtReg] = shape;
112 }
113
114 /// clears the specified virtual register's, physical
115 /// register mapping
116 void clearVirt(Register virtReg) {
117 assert(virtReg.isVirtual());
118 assert(Virt2PhysMap[virtReg] &&
119 "attempt to clear a not assigned virtual register");
120 Virt2PhysMap[virtReg] = MCRegister();
121 }
122
123 /// clears all virtual to physical register mappings
125 Virt2PhysMap.clear();
126 grow();
127 }
128
129 /// returns true if VirtReg is assigned to its preferred physreg.
130 bool hasPreferredPhys(Register VirtReg) const;
131
132 /// returns true if VirtReg has a known preferred register.
133 /// This returns false if VirtReg has a preference that is a virtual
134 /// register that hasn't been assigned yet.
135 bool hasKnownPreference(Register VirtReg) const;
136
137 /// records virtReg is a split live interval from SReg.
138 void setIsSplitFromReg(Register virtReg, Register SReg) {
139 Virt2SplitMap[virtReg] = SReg;
140 if (hasShape(SReg)) {
141 Virt2ShapeMap[virtReg] = getShape(SReg);
142 }
143 }
144
145 /// returns the live interval virtReg is split from.
147 return Virt2SplitMap[virtReg];
148 }
149
150 /// getOriginal - Return the original virtual register that VirtReg descends
151 /// from through splitting.
152 /// A register that was not created by splitting is its own original.
153 /// This operation is idempotent.
155 Register Orig = getPreSplitReg(VirtReg);
156 return Orig ? Orig : VirtReg;
157 }
158
159 /// returns true if the specified virtual register is not
160 /// mapped to a stack slot or rematerialized.
161 bool isAssignedReg(Register virtReg) const {
162 if (getStackSlot(virtReg) == NO_STACK_SLOT)
163 return true;
164 // Split register can be assigned a physical register as well as a
165 // stack slot or remat id.
166 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg]);
167 }
168
169 /// returns the stack slot mapped to the specified virtual
170 /// register
171 int getStackSlot(Register virtReg) const {
172 assert(virtReg.isVirtual());
173 return Virt2StackSlotMap[virtReg];
174 }
175
176 /// create a mapping for the specifed virtual register to
177 /// the next available stack slot
178 int assignVirt2StackSlot(Register virtReg);
179
180 /// create a mapping for the specified virtual register to
181 /// the specified stack slot
182 void assignVirt2StackSlot(Register virtReg, int SS);
183
184 void print(raw_ostream &OS, const Module *M = nullptr) const;
185 void dump() const;
186};
187
189 VRM.print(OS);
190 return OS;
191}
192
194 VirtRegMap VRM;
195
196public:
197 static char ID;
198
200
201 void print(raw_ostream &OS, const Module *M = nullptr) const override {
202 VRM.print(OS, M);
203 }
204
205 VirtRegMap &getVRM() { return VRM; }
206 const VirtRegMap &getVRM() const { return VRM; }
207
209 VRM.init(MF);
210 return false;
211 }
212
213 void getAnalysisUsage(AnalysisUsage &AU) const override {
214 AU.setPreservesAll();
216 }
217};
218
219class VirtRegMapAnalysis : public AnalysisInfoMixin<VirtRegMapAnalysis> {
221 static AnalysisKey Key;
222
223public:
225
227};
228
229class VirtRegMapPrinterPass : public PassInfoMixin<VirtRegMapPrinterPass> {
230 raw_ostream &OS;
231
232public:
236 static bool isRequired() { return true; }
237};
238} // end llvm namespace
239
240#endif // LLVM_CODEGEN_VIRTREGMAP_H
unsigned const MachineRegisterInfo * MRI
This header defines various interfaces for pass management in LLVM.
This file implements an indexed map.
unsigned const TargetRegisterInfo * TRI
ModuleAnalysisManager MAM
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
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:194
bool empty() const
Definition: DenseMap.h:98
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition: DenseMap.h:147
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:33
constexpr bool isValid() const
Definition: MCRegister.h:81
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.
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:65
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
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:91
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
VirtRegMap run(MachineFunction &MF, MachineFunctionAnalysisManager &MAM)
Definition: VirtRegMap.cpp:184
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Definition: VirtRegMap.cpp:178
VirtRegMapPrinterPass(raw_ostream &OS)
Definition: VirtRegMap.h:233
const VirtRegMap & getVRM() const
Definition: VirtRegMap.h:206
void print(raw_ostream &OS, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
Definition: VirtRegMap.h:201
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition: VirtRegMap.h:208
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Definition: VirtRegMap.h:213
bool hasKnownPreference(Register VirtReg) const
returns true if VirtReg has a known preferred register.
Definition: VirtRegMap.cpp:121
void init(MachineFunction &MF)
int assignVirt2StackSlot(Register virtReg)
create a mapping for the specifed virtual register to the next available stack slot
Definition: VirtRegMap.cpp:130
void clearVirt(Register virtReg)
clears the specified virtual register's, physical register mapping
Definition: VirtRegMap.h:116
int getStackSlot(Register virtReg) const
returns the stack slot mapped to the specified virtual register
Definition: VirtRegMap.h:171
void clearAllVirt()
clears all virtual to physical register mappings
Definition: VirtRegMap.h:124
void setIsSplitFromReg(Register virtReg, Register SReg)
records virtReg is a split live interval from SReg.
Definition: VirtRegMap.h:138
bool hasShape(Register virtReg) const
Definition: VirtRegMap.h:101
bool hasPreferredPhys(Register VirtReg) const
returns true if VirtReg is assigned to its preferred physreg.
Definition: VirtRegMap.cpp:112
Register getOriginal(Register VirtReg) const
getOriginal - Return the original virtual register that VirtReg descends from through splitting.
Definition: VirtRegMap.h:154
void dump() const
Definition: VirtRegMap.cpp:170
Register getPreSplitReg(Register virtReg) const
returns the live interval virtReg is split from.
Definition: VirtRegMap.h:146
MachineRegisterInfo & getRegInfo() const
Definition: VirtRegMap.h:79
ShapeT getShape(Register virtReg) const
Definition: VirtRegMap.h:105
MachineFunction & getMachineFunction() const
Definition: VirtRegMap.h:74
MCRegister getPhys(Register virtReg) const
returns the physical register mapped to the specified virtual register
Definition: VirtRegMap.h:90
void print(raw_ostream &OS, const Module *M=nullptr) const
Definition: VirtRegMap.cpp:148
VirtRegMap(VirtRegMap &&)=default
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:86
void assignVirt2Shape(Register virtReg, ShapeT shape)
Definition: VirtRegMap.h:110
bool isAssignedReg(Register virtReg) const
returns true if the specified virtual register is not mapped to a stack slot or rematerialized.
Definition: VirtRegMap.h:161
static constexpr int NO_STACK_SLOT
Definition: VirtRegMap.h:65
const TargetRegisterInfo & getTargetRegInfo() const
Definition: VirtRegMap.h:80
VirtRegMap & operator=(const VirtRegMap &)=delete
void assignVirt2Phys(Register virtReg, MCPhysReg physReg)
creates a mapping for the specified virtual register to the specified physical register
Definition: VirtRegMap.cpp:86
bool isShapeMapEmpty() const
Definition: VirtRegMap.h:99
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
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:303
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:92
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:28
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:69