LLVM 23.0.0git
LivePhysRegs.h
Go to the documentation of this file.
1//===- llvm/CodeGen/LivePhysRegs.h - Live Physical Register Set -*- 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/// \file
10/// This file implements the LivePhysRegs utility for tracking liveness of
11/// physical registers. This can be used for ad-hoc liveness tracking after
12/// register allocation. You can start with the live-ins/live-outs at the
13/// beginning/end of a block and update the information while walking the
14/// instructions inside the block. This implementation tracks the liveness on a
15/// sub-register granularity.
16///
17/// We assume that the high bits of a physical super-register are not preserved
18/// unless the instruction has an implicit-use operand reading the super-
19/// register.
20///
21/// X86 Example:
22/// %ymm0 = ...
23/// %xmm0 = ... (Kills %xmm0, all %xmm0s sub-registers, and %ymm0)
24///
25/// %ymm0 = ...
26/// %xmm0 = ..., implicit %ymm0 (%ymm0 and all its sub-registers are alive)
27//===----------------------------------------------------------------------===//
28
29#ifndef LLVM_CODEGEN_LIVEPHYSREGS_H
30#define LLVM_CODEGEN_LIVEPHYSREGS_H
31
32#include "llvm/ADT/SparseSet.h"
35#include "llvm/MC/MCRegister.h"
37#include <cassert>
38#include <utility>
39
40namespace llvm {
41
42template <typename T> class ArrayRef;
43
44class MachineInstr;
45class MachineFunction;
46class MachineOperand;
48class raw_ostream;
49
50/// A set of physical registers with utility functions to track liveness
51/// when walking backward/forward through a basic block.
53 const TargetRegisterInfo *TRI = nullptr;
54 using RegisterSet = SparseSet<MCPhysReg, MCPhysReg>;
55 RegisterSet LiveRegs;
56
57public:
58 /// Constructs an unitialized set. init() needs to be called to initialize it.
59 LivePhysRegs() = default;
60
61 /// Constructs and initializes an empty set.
62 LivePhysRegs(const TargetRegisterInfo &TRI) : TRI(&TRI) {
63 LiveRegs.setUniverse(TRI.getNumRegs());
64 }
65
66 LivePhysRegs(const LivePhysRegs&) = delete;
68
69 /// (re-)initializes and clears the set.
70 void init(const TargetRegisterInfo &TRI) {
71 this->TRI = &TRI;
72 LiveRegs.clear();
73 LiveRegs.setUniverse(TRI.getNumRegs());
74 }
75
76 /// Clears the set.
77 void clear() { LiveRegs.clear(); }
78
79 /// Returns true if the set is empty.
80 bool empty() const { return LiveRegs.empty(); }
81
82 /// Adds a physical register and all its sub-registers to the set.
84 assert(TRI && "LivePhysRegs is not initialized.");
85 assert(Reg < TRI->getNumRegs() && "Expected a physical register.");
86 // Constant registers are never considered live.
87 if (TRI->isConstantPhysReg(Reg))
88 return;
89 for (MCPhysReg SubReg : TRI->subregs_inclusive(Reg))
90 LiveRegs.insert(SubReg);
91 }
92
93 /// Removes a physical register, all its sub-registers, and all its
94 /// super-registers from the set.
96 assert(TRI && "LivePhysRegs is not initialized.");
97 assert(Reg < TRI->getNumRegs() && "Expected a physical register.");
98 for (MCRegAliasIterator R(Reg, TRI, true); R.isValid(); ++R)
99 LiveRegs.erase((*R).id());
100 }
101
102 /// Removes physical registers clobbered by the regmask operand \p MO.
104 const MachineOperand &MO,
105 SmallVectorImpl<std::pair<MCPhysReg, const MachineOperand *>> *Clobbers =
106 nullptr);
107
108 /// Returns true if register \p Reg is contained in the set. This also
109 /// works if only the super register of \p Reg has been defined, because
110 /// addReg() always adds all sub-registers to the set as well.
111 /// Note: Returns false if just some sub registers are live, use available()
112 /// when searching a free register.
113 bool contains(MCRegister Reg) const { return LiveRegs.count(Reg.id()); }
114
115 /// Returns true if register \p Reg and no aliasing register is in the set.
116 LLVM_ABI bool available(const MachineRegisterInfo &MRI, MCRegister Reg) const;
117
118 /// Remove defined registers and regmask kills from the set.
119 LLVM_ABI void removeDefs(const MachineInstr &MI);
120
121 /// Add uses to the set.
122 LLVM_ABI void addUses(const MachineInstr &MI);
123
124 /// Simulates liveness when stepping backwards over an instruction(bundle).
125 /// Remove Defs, add uses. This is the recommended way of calculating
126 /// liveness.
128
129 /// Simulates liveness when stepping forward over an instruction(bundle).
130 /// Remove killed-uses, add defs. This is the not recommended way, because it
131 /// depends on accurate kill flags. If possible use stepBackward() instead of
132 /// this function. The clobbers set will be the list of registers either
133 /// defined or clobbered by a regmask. The operand will identify whether this
134 /// is a regmask or register operand.
136 const MachineInstr &MI,
137 SmallVectorImpl<std::pair<MCPhysReg, const MachineOperand *>> &Clobbers);
138
139 /// Adds all live-in registers of basic block \p MBB.
140 /// Live in registers are the registers in the blocks live-in list and the
141 /// pristine registers.
143
144 /// Adds all live-in registers of basic block \p MBB but skips pristine
145 /// registers.
147
148 /// Adds all live-out registers of basic block \p MBB.
149 /// Live out registers are the union of the live-in registers of the successor
150 /// blocks and pristine registers. Live out registers of the end block are the
151 /// callee saved registers.
152 /// If a register is not added by this method, it is guaranteed to not be
153 /// live out from MBB, although a sub-register may be. This is true
154 /// both before and after regalloc.
156
157 /// Adds all live-out registers of basic block \p MBB but skips pristine
158 /// registers.
160
162
163 const_iterator begin() const { return LiveRegs.begin(); }
164 const_iterator end() const { return LiveRegs.end(); }
165
166 /// Prints the currently live registers to \p OS.
167 LLVM_ABI void print(raw_ostream &OS) const;
168
169 /// Dumps the currently live registers to the debug output.
170 LLVM_ABI void dump() const;
171
172private:
173 /// Adds a register, taking the lane mask into consideration.
174 void addRegMaskPair(const MachineBasicBlock::RegisterMaskPair &Pair);
175
176 /// Adds live-in registers from basic block \p MBB, taking associated
177 /// lane masks into consideration.
179
180 /// Adds live-out registers from basic block \p MBB, taking associated
181 /// lane masks into consideration.
183
184 /// Adds pristine registers. Pristine registers are callee saved registers
185 /// that are unused in the function.
186 void addPristines(const MachineFunction &MF);
187};
188
190 LR.print(OS);
191 return OS;
192}
193
194/// Computes registers live-in to \p MBB assuming all of its successors
195/// live-in lists are up-to-date. Puts the result into the given LivePhysReg
196/// instance \p LiveRegs.
197LLVM_ABI void computeLiveIns(LivePhysRegs &LiveRegs,
198 const MachineBasicBlock &MBB);
199
200/// Recomputes dead and kill flags in \p MBB.
201LLVM_ABI void recomputeLivenessFlags(MachineBasicBlock &MBB);
202
203/// Adds registers contained in \p LiveRegs to the block live-in list of \p MBB.
204/// Does not add reserved registers.
205LLVM_ABI void addLiveIns(MachineBasicBlock &MBB, const LivePhysRegs &LiveRegs);
206
207/// Convenience function combining computeLiveIns() and addLiveIns().
208LLVM_ABI void computeAndAddLiveIns(LivePhysRegs &LiveRegs,
209 MachineBasicBlock &MBB);
210
211/// Check if physical register \p Reg is used after \p MBI.
213
214/// Convenience function for recomputing live-in's for a MBB. Returns true if
215/// any changes were made.
217 LivePhysRegs LPR;
218 std::vector<MachineBasicBlock::RegisterMaskPair> OldLiveIns;
219
220 MBB.clearLiveIns(OldLiveIns);
222 MBB.sortUniqueLiveIns();
223
224 const std::vector<MachineBasicBlock::RegisterMaskPair> &NewLiveIns =
225 MBB.getLiveIns();
226 return OldLiveIns != NewLiveIns;
227}
228
229/// Convenience function for recomputing live-in's for a set of MBBs until the
230/// computation converges.
232 bool Change = false;
233 do {
234 Change = false;
235 for (MachineBasicBlock *MBB : MBBs)
236 Change |= recomputeLiveIns(*MBB);
237 } while (Change);
238}
239
240} // end namespace llvm
241
242#endif // LLVM_CODEGEN_LIVEPHYSREGS_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
#define LLVM_ABI
Definition Compiler.h:213
IRTranslator LLVM IR MI
static void addBlockLiveOuts(LiveRegUnits &LiveUnits, const MachineBasicBlock &MBB)
Add live-out registers of basic block MBB to LiveUnits.
static void addBlockLiveIns(LiveRegUnits &LiveUnits, const MachineBasicBlock &MBB)
Add live-in registers of basic block MBB to LiveUnits.
Register Reg
Promote Memory to Register
Definition Mem2Reg.cpp:110
This file defines the SparseSet class derived from the version described in Briggs,...
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
A set of physical registers with utility functions to track liveness when walking backward/forward th...
void addReg(MCRegister Reg)
Adds a physical register and all its sub-registers to the set.
RegisterSet::const_iterator const_iterator
const_iterator end() const
void clear()
Clears the set.
LLVM_ABI void stepForward(const MachineInstr &MI, SmallVectorImpl< std::pair< MCPhysReg, const MachineOperand * > > &Clobbers)
Simulates liveness when stepping forward over an instruction(bundle).
LivePhysRegs(const LivePhysRegs &)=delete
LLVM_ABI void print(raw_ostream &OS) const
Prints the currently live registers to OS.
LLVM_ABI void stepBackward(const MachineInstr &MI)
Simulates liveness when stepping backwards over an instruction(bundle).
void init(const TargetRegisterInfo &TRI)
(re-)initializes and clears the set.
LLVM_ABI void addLiveIns(const MachineBasicBlock &MBB)
Adds all live-in registers of basic block MBB.
LLVM_ABI bool available(const MachineRegisterInfo &MRI, MCRegister Reg) const
Returns true if register Reg and no aliasing register is in the set.
LLVM_ABI void addUses(const MachineInstr &MI)
Add uses to the set.
LLVM_ABI void removeDefs(const MachineInstr &MI)
Remove defined registers and regmask kills from the set.
LLVM_ABI void addLiveOutsNoPristines(const MachineBasicBlock &MBB)
Adds all live-out registers of basic block MBB but skips pristine registers.
LivePhysRegs(const TargetRegisterInfo &TRI)
Constructs and initializes an empty set.
LLVM_ABI void addLiveOuts(const MachineBasicBlock &MBB)
Adds all live-out registers of basic block MBB.
LivePhysRegs()=default
Constructs an unitialized set. init() needs to be called to initialize it.
void removeReg(MCRegister Reg)
Removes a physical register, all its sub-registers, and all its super-registers from the set.
const_iterator begin() const
LLVM_ABI void addLiveInsNoPristines(const MachineBasicBlock &MBB)
Adds all live-in registers of basic block MBB but skips pristine registers.
bool contains(MCRegister Reg) const
Returns true if register Reg is contained in the set.
LivePhysRegs & operator=(const LivePhysRegs &)=delete
LLVM_ABI void removeRegsInMask(const MachineOperand &MO, SmallVectorImpl< std::pair< MCPhysReg, const MachineOperand * > > *Clobbers=nullptr)
Removes physical registers clobbered by the regmask operand MO.
bool empty() const
Returns true if the set is empty.
LLVM_ABI void dump() const
Dumps the currently live registers to the debug output.
MCRegAliasIterator enumerates all registers aliasing Reg.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
MachineInstrBundleIterator< MachineInstr > iterator
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
SparseSet - Fast set implementation for objects that can be identified by small unsigned keys.
Definition SparseSet.h:117
typename DenseT::const_iterator const_iterator
Definition SparseSet.h:168
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
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.
LLVM_ABI void recomputeLivenessFlags(MachineBasicBlock &MBB)
Recomputes dead and kill flags in MBB.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
LLVM_ABI bool isPhysRegUsedAfter(Register Reg, MachineBasicBlock::iterator MBI)
Check if physical register Reg is used after MBI.
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
LLVM_ABI void computeAndAddLiveIns(LivePhysRegs &LiveRegs, MachineBasicBlock &MBB)
Convenience function combining computeLiveIns() and addLiveIns().
LLVM_ABI void computeLiveIns(LivePhysRegs &LiveRegs, const MachineBasicBlock &MBB)
Computes registers live-in to MBB assuming all of its successors live-in lists are up-to-date.
void fullyRecomputeLiveIns(ArrayRef< MachineBasicBlock * > MBBs)
Convenience function for recomputing live-in's for a set of MBBs until the computation converges.
LLVM_ABI void addLiveIns(MachineBasicBlock &MBB, const LivePhysRegs &LiveRegs)
Adds registers contained in LiveRegs to the block live-in list of MBB.
static bool recomputeLiveIns(MachineBasicBlock &MBB)
Convenience function for recomputing live-in's for a MBB.
Pair of physical register and lane mask.