LLVM 24.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;
32class raw_ostream;
33
35 MachineRegisterInfo *MRI = nullptr;
36 const TargetRegisterInfo *TRI = nullptr;
37 MachineFunction *MF = nullptr;
38
39 /// Virt2PhysMap - This is a virtual to physical register
40 /// mapping. Each virtual register is required to have an entry in
41 /// it; even spilled virtual registers (the register mapped to a
42 /// spilled register is the temporary used to load it from the
43 /// stack).
45
46 /// Virt2StackSlotMap - This is virtual register to stack slot
47 /// mapping. Each spilled virtual register has an entry in it
48 /// which corresponds to the stack slot this register is spilled
49 /// at.
51
52 /// Virt2SplitMap - This is virtual register to splitted virtual register
53 /// mapping.
55
56 /// Virt2ShapeMap - For X86 AMX register whose register is bound shape
57 /// information.
58 DenseMap<Register, ShapeT> Virt2ShapeMap;
59
60 /// createSpillSlot - Allocate a spill slot for RC from MFI.
61 unsigned createSpillSlot(const TargetRegisterClass *RC);
62
63public:
64 static constexpr int NO_STACK_SLOT = INT_MAX;
65
66 VirtRegMap() : Virt2StackSlotMap(NO_STACK_SLOT) {}
67 VirtRegMap(const VirtRegMap &) = delete;
68 VirtRegMap &operator=(const VirtRegMap &) = delete;
69 VirtRegMap(VirtRegMap &&) = default;
70
72
74 assert(MF && "getMachineFunction called before runOnMachineFunction");
75 return *MF;
76 }
77
78 MachineRegisterInfo &getRegInfo() const { return *MRI; }
79 const TargetRegisterInfo &getTargetRegInfo() const { return *TRI; }
80
81 LLVM_ABI void grow();
82
83 /// returns true if the specified virtual register is
84 /// mapped to a physical register
85 bool hasPhys(Register virtReg) const { return getPhys(virtReg).isValid(); }
86
87 /// returns the physical register mapped to the specified
88 /// virtual register
89 MCRegister getPhys(Register virtReg) const {
90 assert(virtReg.isVirtual());
91 return Virt2PhysMap[virtReg];
92 }
93
94 /// creates a mapping for the specified virtual register to
95 /// the specified physical register
96 LLVM_ABI void assignVirt2Phys(Register virtReg, MCRegister physReg);
97
98 bool isShapeMapEmpty() const { return Virt2ShapeMap.empty(); }
99
100 bool hasShape(Register virtReg) const {
101 return Virt2ShapeMap.contains(virtReg);
102 }
103
104 ShapeT getShape(Register virtReg) const {
105 assert(virtReg.isVirtual());
106 return Virt2ShapeMap.lookup(virtReg);
107 }
108
109 void assignVirt2Shape(Register virtReg, ShapeT shape) {
110 Virt2ShapeMap[virtReg] = shape;
111 }
112
113 /// clears the specified virtual register's, physical
114 /// register mapping
115 void clearVirt(Register virtReg) {
116 assert(virtReg.isVirtual());
117 assert(Virt2PhysMap[virtReg] &&
118 "attempt to clear a not assigned virtual register");
119 Virt2PhysMap[virtReg] = MCRegister();
120 }
121
122 /// clears all virtual to physical register mappings
124 Virt2PhysMap.clear();
125 grow();
126 }
127
128 /// returns true if VirtReg is assigned to its preferred physreg.
129 LLVM_ABI bool hasPreferredPhys(Register VirtReg) const;
130
131 /// returns true if VirtReg has a known preferred register.
132 /// This returns false if VirtReg has a preference that is a virtual
133 /// register that hasn't been assigned yet.
134 LLVM_ABI bool hasKnownPreference(Register VirtReg) const;
135
136 /// records virtReg is a split live interval from SReg.
137 void setIsSplitFromReg(Register virtReg, Register SReg) {
138 Virt2SplitMap[virtReg] = SReg;
139 if (hasShape(SReg)) {
140 Virt2ShapeMap[virtReg] = getShape(SReg);
141 }
142 }
143
144 /// returns the live interval virtReg is split from.
146 return Virt2SplitMap[virtReg];
147 }
148
149 /// getOriginal - Return the original virtual register that VirtReg descends
150 /// from through splitting.
151 /// A register that was not created by splitting is its own original.
152 /// This operation is idempotent.
154 Register Orig = getPreSplitReg(VirtReg);
155 return Orig ? Orig : VirtReg;
156 }
157
158 /// returns true if the specified virtual register is not
159 /// mapped to a stack slot or rematerialized.
160 bool isAssignedReg(Register virtReg) const {
161 if (getStackSlot(virtReg) == NO_STACK_SLOT)
162 return true;
163 // Split register can be assigned a physical register as well as a
164 // stack slot or remat id.
165 return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg]);
166 }
167
168 /// returns the stack slot mapped to the specified virtual
169 /// register
170 int getStackSlot(Register virtReg) const {
171 assert(virtReg.isVirtual());
172 return Virt2StackSlotMap[virtReg];
173 }
174
175 /// create a mapping for the specifed virtual register to
176 /// the next available stack slot
178
179 /// create a mapping for the specified virtual register to
180 /// the specified stack slot
181 LLVM_ABI void assignVirt2StackSlot(Register virtReg, int SS);
182
183 LLVM_ABI void print(raw_ostream &OS, const Module *M = nullptr) const;
184 LLVM_ABI void dump() const;
185};
186
188 VRM.print(OS);
189 return OS;
190}
191
193 VirtRegMap VRM;
194
195public:
196 LLVM_ABI static char ID;
197
199
200 void print(raw_ostream &OS, const Module *M = nullptr) const override {
201 VRM.print(OS, M);
202 }
203
204 VirtRegMap &getVRM() { return VRM; }
205 const VirtRegMap &getVRM() const { return VRM; }
206
208 VRM.init(MF);
209 return false;
210 }
211
212 void getAnalysisUsage(AnalysisUsage &AU) const override {
213 AU.setPreservesAll();
215 }
216};
217
218class VirtRegMapAnalysis : public AnalysisInfoMixin<VirtRegMapAnalysis> {
220 LLVM_ABI static AnalysisKey Key;
221
222public:
224
227};
228
230 : public RequiredPassInfoMixin<VirtRegMapPrinterPass> {
231 raw_ostream &OS;
232
233public:
234 explicit VirtRegMapPrinterPass(raw_ostream &OS) : OS(OS) {}
237};
238
239class VirtRegRewriterPass : public RequiredPassInfoMixin<VirtRegRewriterPass> {
240 bool ClearVirtRegs = true;
241
242public:
243 VirtRegRewriterPass(bool ClearVirtRegs = true)
244 : ClearVirtRegs(ClearVirtRegs) {}
247
250
252 if (ClearVirtRegs)
253 return MachineFunctionProperties().setNoVRegs();
254 return {};
255 }
256};
257
258} // end llvm namespace
259
260#endif // LLVM_CODEGEN_VIRTREGMAP_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:211
This header defines various interfaces for pass management in LLVM.
This file implements an indexed map.
ModuleAnalysisManager MAM
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
constexpr bool isValid() const
Definition MCRegister.h:84
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:68
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:20
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
LLVM_ABI VirtRegMap run(MachineFunction &MF, MachineFunctionAnalysisManager &MAM)
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
VirtRegMapPrinterPass(raw_ostream &OS)
Definition VirtRegMap.h:234
const VirtRegMap & getVRM() const
Definition VirtRegMap.h:205
void print(raw_ostream &OS, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
Definition VirtRegMap.h:200
static LLVM_ABI char ID
Definition VirtRegMap.h:196
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition VirtRegMap.h:207
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Definition VirtRegMap.h:212
LLVM_ABI bool hasKnownPreference(Register VirtReg) const
returns true if VirtReg has a known preferred register.
LLVM_ABI int assignVirt2StackSlot(Register virtReg)
create a mapping for the specifed virtual register to the next available stack slot
void clearVirt(Register virtReg)
clears the specified virtual register's, physical register mapping
Definition VirtRegMap.h:115
int getStackSlot(Register virtReg) const
returns the stack slot mapped to the specified virtual register
Definition VirtRegMap.h:170
void clearAllVirt()
clears all virtual to physical register mappings
Definition VirtRegMap.h:123
void setIsSplitFromReg(Register virtReg, Register SReg)
records virtReg is a split live interval from SReg.
Definition VirtRegMap.h:137
bool hasShape(Register virtReg) const
Definition VirtRegMap.h:100
LLVM_ABI void init(MachineFunction &MF)
LLVM_ABI bool hasPreferredPhys(Register VirtReg) const
returns true if VirtReg is assigned to its preferred physreg.
Register getOriginal(Register VirtReg) const
getOriginal - Return the original virtual register that VirtReg descends from through splitting.
Definition VirtRegMap.h:153
LLVM_ABI void dump() const
Register getPreSplitReg(Register virtReg) const
returns the live interval virtReg is split from.
Definition VirtRegMap.h:145
MachineRegisterInfo & getRegInfo() const
Definition VirtRegMap.h:78
ShapeT getShape(Register virtReg) const
Definition VirtRegMap.h:104
LLVM_ABI void assignVirt2Phys(Register virtReg, MCRegister physReg)
creates a mapping for the specified virtual register to the specified physical register
MachineFunction & getMachineFunction() const
Definition VirtRegMap.h:73
MCRegister getPhys(Register virtReg) const
returns the physical register mapped to the specified virtual register
Definition VirtRegMap.h:89
LLVM_ABI void print(raw_ostream &OS, const Module *M=nullptr) const
VirtRegMap(VirtRegMap &&)=default
LLVM_ABI void grow()
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:85
void assignVirt2Shape(Register virtReg, ShapeT shape)
Definition VirtRegMap.h:109
bool isAssignedReg(Register virtReg) const
returns true if the specified virtual register is not mapped to a stack slot or rematerialized.
Definition VirtRegMap.h:160
static constexpr int NO_STACK_SLOT
Definition VirtRegMap.h:64
const TargetRegisterInfo & getTargetRegInfo() const
Definition VirtRegMap.h:79
VirtRegMap & operator=(const VirtRegMap &)=delete
bool isShapeMapEmpty() const
Definition VirtRegMap.h:98
VirtRegRewriterPass(bool ClearVirtRegs=true)
Definition VirtRegMap.h:243
LLVM_ABI void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)>) const
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
MachineFunctionProperties getSetProperties() const
Definition VirtRegMap.h:251
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.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
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
A CRTP mix-in for passes that should not be skipped.