LLVM  10.0.0svn
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"
22 #include "llvm/MC/MCRegisterInfo.h"
23 #include "llvm/Pass.h"
24 #include <cassert>
25 
26 namespace llvm {
27 
28 class MachineFunction;
29 class MachineRegisterInfo;
30 class raw_ostream;
31 class TargetInstrInfo;
32 
34  public:
35  enum {
37  NO_STACK_SLOT = (1L << 30)-1,
38  MAX_STACK_SLOT = (1L << 18)-1
39  };
40 
41  private:
43  const TargetInstrInfo *TII;
44  const TargetRegisterInfo *TRI;
45  MachineFunction *MF;
46 
47  /// Virt2PhysMap - This is a virtual to physical register
48  /// mapping. Each virtual register is required to have an entry in
49  /// it; even spilled virtual registers (the register mapped to a
50  /// spilled register is the temporary used to load it from the
51  /// stack).
53 
54  /// Virt2StackSlotMap - This is virtual register to stack slot
55  /// mapping. Each spilled virtual register has an entry in it
56  /// which corresponds to the stack slot this register is spilled
57  /// at.
58  IndexedMap<int, VirtReg2IndexFunctor> Virt2StackSlotMap;
59 
60  /// Virt2SplitMap - This is virtual register to splitted virtual register
61  /// mapping.
63 
64  /// createSpillSlot - Allocate a spill slot for RC from MFI.
65  unsigned createSpillSlot(const TargetRegisterClass *RC);
66 
67  public:
68  static char ID;
69 
71  : MachineFunctionPass(ID), MRI(nullptr), TII(nullptr), TRI(nullptr),
72  MF(nullptr), Virt2PhysMap(NO_PHYS_REG),
73  Virt2StackSlotMap(NO_STACK_SLOT), Virt2SplitMap(0) {}
74  VirtRegMap(const VirtRegMap &) = delete;
75  VirtRegMap &operator=(const VirtRegMap &) = delete;
76 
77  bool runOnMachineFunction(MachineFunction &MF) override;
78 
79  void getAnalysisUsage(AnalysisUsage &AU) const override {
80  AU.setPreservesAll();
82  }
83 
85  assert(MF && "getMachineFunction called before runOnMachineFunction");
86  return *MF;
87  }
88 
89  MachineRegisterInfo &getRegInfo() const { return *MRI; }
90  const TargetRegisterInfo &getTargetRegInfo() const { return *TRI; }
91 
92  void grow();
93 
94  /// returns true if the specified virtual register is
95  /// mapped to a physical register
96  bool hasPhys(unsigned virtReg) const {
97  return getPhys(virtReg) != NO_PHYS_REG;
98  }
99 
100  /// returns the physical register mapped to the specified
101  /// virtual register
102  Register getPhys(Register virtReg) const {
103  assert(virtReg.isVirtual());
104  return Virt2PhysMap[virtReg];
105  }
106 
107  /// creates a mapping for the specified virtual register to
108  /// the specified physical register
109  void assignVirt2Phys(unsigned virtReg, MCPhysReg physReg);
110 
111  /// clears the specified virtual register's, physical
112  /// register mapping
113  void clearVirt(unsigned virtReg) {
115  assert(Virt2PhysMap[virtReg] != NO_PHYS_REG &&
116  "attempt to clear a not assigned virtual register");
117  Virt2PhysMap[virtReg] = NO_PHYS_REG;
118  }
119 
120  /// clears all virtual to physical register mappings
121  void clearAllVirt() {
122  Virt2PhysMap.clear();
123  grow();
124  }
125 
126  /// returns true if VirtReg is assigned to its preferred physreg.
127  bool hasPreferredPhys(unsigned VirtReg);
128 
129  /// returns true if VirtReg has a known preferred register.
130  /// This returns false if VirtReg has a preference that is a virtual
131  /// register that hasn't been assigned yet.
132  bool hasKnownPreference(unsigned VirtReg);
133 
134  /// records virtReg is a split live interval from SReg.
135  void setIsSplitFromReg(unsigned virtReg, unsigned SReg) {
136  Virt2SplitMap[virtReg] = SReg;
137  }
138 
139  /// returns the live interval virtReg is split from.
140  unsigned getPreSplitReg(unsigned virtReg) const {
141  return Virt2SplitMap[virtReg];
142  }
143 
144  /// getOriginal - Return the original virtual register that VirtReg descends
145  /// from through splitting.
146  /// A register that was not created by splitting is its own original.
147  /// This operation is idempotent.
148  unsigned getOriginal(unsigned VirtReg) const {
149  unsigned Orig = getPreSplitReg(VirtReg);
150  return Orig ? Orig : VirtReg;
151  }
152 
153  /// returns true if the specified virtual register is not
154  /// mapped to a stack slot or rematerialized.
155  bool isAssignedReg(unsigned virtReg) const {
156  if (getStackSlot(virtReg) == NO_STACK_SLOT)
157  return true;
158  // Split register can be assigned a physical register as well as a
159  // stack slot or remat id.
160  return (Virt2SplitMap[virtReg] && Virt2PhysMap[virtReg] != NO_PHYS_REG);
161  }
162 
163  /// returns the stack slot mapped to the specified virtual
164  /// register
165  int getStackSlot(unsigned virtReg) const {
167  return Virt2StackSlotMap[virtReg];
168  }
169 
170  /// create a mapping for the specifed virtual register to
171  /// the next available stack slot
172  int assignVirt2StackSlot(unsigned virtReg);
173 
174  /// create a mapping for the specified virtual register to
175  /// the specified stack slot
176  void assignVirt2StackSlot(unsigned virtReg, int SS);
177 
178  void print(raw_ostream &OS, const Module* M = nullptr) const override;
179  void dump() const;
180  };
181 
182  inline raw_ostream &operator<<(raw_ostream &OS, const VirtRegMap &VRM) {
183  VRM.print(OS);
184  return OS;
185  }
186 
187 } // end llvm namespace
188 
189 #endif // LLVM_CODEGEN_VIRTREGMAP_H
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
void print(raw_ostream &OS, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
Definition: VirtRegMap.cpp:138
const TargetRegisterInfo & getTargetRegInfo() const
Definition: VirtRegMap.h:90
void dump() const
Definition: VirtRegMap.cpp:160
MachineFunction & getMachineFunction() const
Definition: VirtRegMap.h:84
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
unsigned getPreSplitReg(unsigned virtReg) const
returns the live interval virtReg is split from.
Definition: VirtRegMap.h:140
void setIsSplitFromReg(unsigned virtReg, unsigned SReg)
records virtReg is a split live interval from SReg.
Definition: VirtRegMap.h:135
bool isAssignedReg(unsigned virtReg) const
returns true if the specified virtual register is not mapped to a stack slot or rematerialized.
Definition: VirtRegMap.h:155
bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition: Register.h:26
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
TargetInstrInfo - Interface to description of machine instruction set.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MachineRegisterInfo & getRegInfo() const
Definition: VirtRegMap.h:89
bool hasPreferredPhys(unsigned VirtReg)
returns true if VirtReg is assigned to its preferred physreg.
Definition: VirtRegMap.cpp:102
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition: VirtRegMap.cpp:62
Represent the analysis usage information of a pass.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Definition: VirtRegMap.h:79
bool hasKnownPreference(unsigned VirtReg)
returns true if VirtReg has a known preferred register.
Definition: VirtRegMap.cpp:111
VirtRegMap & operator=(const VirtRegMap &)=delete
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
void assignVirt2Phys(unsigned virtReg, MCPhysReg physReg)
creates a mapping for the specified virtual register to the specified physical register ...
Definition: VirtRegMap.cpp:83
void clearVirt(unsigned virtReg)
clears the specified virtual register&#39;s, physical register mapping
Definition: VirtRegMap.h:113
void setPreservesAll()
Set by analyses that do not transform their input at all.
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
static char ID
Definition: VirtRegMap.h:68
Register getPhys(Register virtReg) const
returns the physical register mapped to the specified virtual register
Definition: VirtRegMap.h:102
bool hasPhys(unsigned virtReg) const
returns true if the specified virtual register is mapped to a physical register
Definition: VirtRegMap.h:96
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2038
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void clearAllVirt()
clears all virtual to physical register mappings
Definition: VirtRegMap.h:121
int getStackSlot(unsigned virtReg) const
returns the stack slot mapped to the specified virtual register
Definition: VirtRegMap.h:165
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
int assignVirt2StackSlot(unsigned virtReg)
create a mapping for the specifed virtual register to the next available stack slot ...
Definition: VirtRegMap.cpp:120
unsigned getOriginal(unsigned VirtReg) const
getOriginal - Return the original virtual register that VirtReg descends from through splitting...
Definition: VirtRegMap.h:148
Wrapper class representing virtual and physical registers.
Definition: Register.h:18