LLVM 22.0.0git
LiveRegUnits.h
Go to the documentation of this file.
1//===- llvm/CodeGen/LiveRegUnits.h - Register Unit 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/// A set of register units. It is intended for register liveness tracking.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CODEGEN_LIVEREGUNITS_H
15#define LLVM_CODEGEN_LIVEREGUNITS_H
16
17#include "llvm/ADT/BitVector.h"
20#include "llvm/MC/LaneBitmask.h"
23#include <cstdint>
24
25namespace llvm {
26
27class MachineInstr;
29
30/// A set of register units used to track register liveness.
32 const TargetRegisterInfo *TRI = nullptr;
33 BitVector Units;
34
35public:
36 /// Constructs a new empty LiveRegUnits set.
37 LiveRegUnits() = default;
38
39 /// Constructs and initialize an empty LiveRegUnits set.
41 init(TRI);
42 }
43
44 /// For a machine instruction \p MI, adds all register units used in
45 /// \p UsedRegUnits and defined or clobbered in \p ModifiedRegUnits. This is
46 /// useful when walking over a range of instructions to track registers
47 /// used or defined separately.
49 LiveRegUnits &ModifiedRegUnits,
50 LiveRegUnits &UsedRegUnits,
51 const TargetRegisterInfo *TRI) {
52 for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
53 if (O->isRegMask())
54 ModifiedRegUnits.addRegsInMask(O->getRegMask());
55 if (!O->isReg())
56 continue;
57 Register Reg = O->getReg();
58 if (!Reg.isPhysical())
59 continue;
60 if (O->isDef()) {
61 // Some architectures (e.g. AArch64 XZR/WZR) have registers that are
62 // constant and may be used as destinations to indicate the generated
63 // value is discarded. No need to track such case as a def.
64 if (!TRI->isConstantPhysReg(Reg))
65 ModifiedRegUnits.addReg(Reg);
66 } else {
67 assert(O->isUse() && "Reg operand not a def and not a use");
68 UsedRegUnits.addReg(Reg);
69 }
70 }
71 }
72
73 /// Initialize and clear the set.
74 void init(const TargetRegisterInfo &TRI) {
75 this->TRI = &TRI;
76 Units.reset();
77 Units.resize(TRI.getNumRegUnits());
78 }
79
80 /// Clears the set.
81 void clear() { Units.reset(); }
82
83 /// Returns true if the set is empty.
84 bool empty() const { return Units.none(); }
85
86 /// Adds register units covered by physical register \p Reg.
88 for (MCRegUnit Unit : TRI->regunits(Reg))
89 Units.set(Unit);
90 }
91
92 /// Adds register units covered by physical register \p Reg that are
93 /// part of the lanemask \p Mask.
95 for (MCRegUnitMaskIterator Unit(Reg, TRI); Unit.isValid(); ++Unit) {
96 LaneBitmask UnitMask = (*Unit).second;
97 if ((UnitMask & Mask).any())
98 Units.set((*Unit).first);
99 }
100 }
101
102 /// Removes all register units covered by physical register \p Reg.
104 for (MCRegUnit Unit : TRI->regunits(Reg))
105 Units.reset(Unit);
106 }
107
108 /// Removes register units not preserved by the regmask \p RegMask.
109 /// The regmask has the same format as the one in the RegMask machine operand.
110 LLVM_ABI void removeRegsNotPreserved(const uint32_t *RegMask);
111
112 /// Adds register units not preserved by the regmask \p RegMask.
113 /// The regmask has the same format as the one in the RegMask machine operand.
114 LLVM_ABI void addRegsInMask(const uint32_t *RegMask);
115
116 /// Returns true if no part of physical register \p Reg is live.
118 for (MCRegUnit Unit : TRI->regunits(Reg)) {
119 if (Units.test(Unit))
120 return false;
121 }
122 return true;
123 }
124
125 /// Updates liveness when stepping backwards over the instruction \p MI.
126 /// This removes all register units defined or clobbered in \p MI and then
127 /// adds the units used (as in use operands) in \p MI.
129
130 /// Adds all register units used, defined or clobbered in \p MI.
131 /// This is useful when walking over a range of instruction to find registers
132 /// unused over the whole range.
133 LLVM_ABI void accumulate(const MachineInstr &MI);
134
135 /// Adds registers living out of block \p MBB.
136 /// Live out registers are the union of the live-in registers of the successor
137 /// blocks and pristine registers. Live out registers of the end block are the
138 /// callee saved registers.
140
141 /// Adds registers living into block \p MBB.
143
144 /// Adds all register units marked in the bitvector \p RegUnits.
145 void addUnits(const BitVector &RegUnits) {
146 Units |= RegUnits;
147 }
148 /// Removes all register units marked in the bitvector \p RegUnits.
149 void removeUnits(const BitVector &RegUnits) {
150 Units.reset(RegUnits);
151 }
152 /// Return the internal bitvector representation of the set.
153 const BitVector &getBitVector() const {
154 return Units;
155 }
156
157private:
158 /// Adds pristine registers. Pristine registers are callee saved registers
159 /// that are unused in the function.
160 void addPristines(const MachineFunction &MF);
161};
162
163/// Returns an iterator range over all physical register and mask operands for
164/// \p MI and bundled instructions. This also skips any debug operands.
165inline iterator_range<
166 filter_iterator<ConstMIBundleOperands, bool (*)(const MachineOperand &)>>
168 auto Pred = [](const MachineOperand &MOP) {
169 return MOP.isRegMask() ||
170 (MOP.isReg() && !MOP.isDebug() && MOP.getReg().isPhysical());
171 };
173 static_cast<bool (*)(const MachineOperand &)>(Pred));
174}
175
176} // end namespace llvm
177
178#endif // LLVM_CODEGEN_LIVEREGUNITS_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
This file implements the BitVector class.
#define LLVM_ABI
Definition Compiler.h:213
IRTranslator LLVM IR MI
A common definition of LaneBitmask for use in TableGen and CodeGen.
Register Reg
ConstMIBundleOperands - Iterate over all operands in a const bundle of machine instructions.
static void accumulateUsedDefed(const MachineInstr &MI, LiveRegUnits &ModifiedRegUnits, LiveRegUnits &UsedRegUnits, const TargetRegisterInfo *TRI)
For a machine instruction MI, adds all register units used in UsedRegUnits and defined or clobbered i...
LiveRegUnits(const TargetRegisterInfo &TRI)
Constructs and initialize an empty LiveRegUnits set.
bool available(MCRegister Reg) const
Returns true if no part of physical register Reg is live.
const BitVector & getBitVector() const
Return the internal bitvector representation of the set.
LLVM_ABI void addRegsInMask(const uint32_t *RegMask)
Adds register units not preserved by the regmask RegMask.
void init(const TargetRegisterInfo &TRI)
Initialize and clear the set.
void addReg(MCRegister Reg)
Adds register units covered by physical register Reg.
LiveRegUnits()=default
Constructs a new empty LiveRegUnits set.
LLVM_ABI void stepBackward(const MachineInstr &MI)
Updates liveness when stepping backwards over the instruction MI.
LLVM_ABI void addLiveOuts(const MachineBasicBlock &MBB)
Adds registers living out of block MBB.
void addUnits(const BitVector &RegUnits)
Adds all register units marked in the bitvector RegUnits.
void removeReg(MCRegister Reg)
Removes all register units covered by physical register Reg.
void addRegMasked(MCRegister Reg, LaneBitmask Mask)
Adds register units covered by physical register Reg that are part of the lanemask Mask.
bool empty() const
Returns true if the set is empty.
LLVM_ABI void addLiveIns(const MachineBasicBlock &MBB)
Adds registers living into block MBB.
void removeUnits(const BitVector &RegUnits)
Removes all register units marked in the bitvector RegUnits.
LLVM_ABI void removeRegsNotPreserved(const uint32_t *RegMask)
Removes register units not preserved by the regmask RegMask.
void clear()
Clears the set.
LLVM_ABI void accumulate(const MachineInstr &MI)
Adds all register units used, defined or clobbered in MI.
MCRegUnitMaskIterator enumerates a list of register units and their associated lane masks for Reg.
bool isValid() const
Returns true if this iterator is not yet at the end.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:33
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
Wrapper class representing virtual and physical registers.
Definition Register.h:19
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
This is an optimization pass for GlobalISel generic memory operations.
iterator_range< filter_iterator< ConstMIBundleOperands, bool(*)(const MachineOperand &)> > phys_regs_and_masks(const MachineInstr &MI)
Returns an iterator range over all physical register and mask operands for MI and bundled instruction...
iterator_range< filter_iterator< detail::IterOfRange< RangeT >, PredicateT > > make_filter_range(RangeT &&Range, PredicateT Pred)
Convenience function that takes a range of elements and a predicate, and return a new filter_iterator...
Definition STLExtras.h:544
unsigned MCRegUnit
Register units are used to compute register aliasing.
Definition MCRegister.h:30
iterator_range< ConstMIBundleOperands > const_mi_bundle_ops(const MachineInstr &MI)
iterator_range(Container &&) -> iterator_range< llvm::detail::IterOfRange< Container > >
filter_iterator_impl< WrappedIteratorT, PredicateT, detail::fwd_or_bidi_tag< WrappedIteratorT > > filter_iterator
Defines filter_iterator to a suitable specialization of filter_iterator_impl, based on the underlying...
Definition STLExtras.h:531