LLVM 23.0.0git
LiveRegUnits.cpp
Go to the documentation of this file.
1//===- LiveRegUnits.cpp - Register Unit Set -------------------------------===//
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 This file imlements the LiveRegUnits set.
10//
11//===----------------------------------------------------------------------===//
12
19
20using namespace llvm;
21
23 for (MCRegUnit U : TRI->regunits()) {
24 for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) {
25 if (MachineOperand::clobbersPhysReg(RegMask, *RootReg)) {
26 Units.reset(static_cast<unsigned>(U));
27 break;
28 }
29 }
30 }
31}
32
34 for (MCRegUnit U : TRI->regunits()) {
35 for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) {
36 if (MachineOperand::clobbersPhysReg(RegMask, *RootReg)) {
37 Units.set(static_cast<unsigned>(U));
38 break;
39 }
40 }
41 }
42}
43
45 assert(!MI.isDebugInstr() &&
46 "Debug instructions must not affect liveness calculation");
47
48 // Remove defined registers and regmask kills from the set.
49 for (const MachineOperand &MOP : MI.operands()) {
50 if (MOP.isReg()) {
51 if (MOP.isDef() && MOP.getReg().isPhysical())
52 removeReg(MOP.getReg());
53 continue;
54 }
55
56 if (MOP.isRegMask()) {
57 removeRegsNotPreserved(MOP.getRegMask());
58 continue;
59 }
60 }
61
62 // Add uses to the set.
63 for (const MachineOperand &MOP : MI.operands()) {
64 if (!MOP.isReg() || !MOP.readsReg())
65 continue;
66
67 if (MOP.getReg().isPhysical())
68 addReg(MOP.getReg());
69 }
70}
71
73 // Add defs, uses and regmask clobbers to the set.
74 for (const MachineOperand &MOP : MI.operands()) {
75 if (MOP.isReg()) {
76 if (!MOP.getReg().isPhysical())
77 continue;
78 if (MOP.isDef() || MOP.readsReg())
79 addReg(MOP.getReg());
80 continue;
81 }
82
83 if (MOP.isRegMask()) {
84 addRegsInMask(MOP.getRegMask());
85 continue;
86 }
87 }
88}
89
90/// Add live-in registers of basic block \p MBB to \p LiveUnits.
91static void addBlockLiveIns(LiveRegUnits &LiveUnits,
92 const MachineBasicBlock &MBB) {
93 for (const auto &LI : MBB.liveins())
94 LiveUnits.addRegMasked(LI.PhysReg, LI.LaneMask);
95}
96
97/// Add live-out registers of basic block \p MBB to \p LiveUnits.
98static void addBlockLiveOuts(LiveRegUnits &LiveUnits,
99 const MachineBasicBlock &MBB) {
100 for (const auto &LO : MBB.liveouts())
101 LiveUnits.addRegMasked(LO.PhysReg, LO.LaneMask);
102}
103
104/// Adds all callee saved registers to \p LiveUnits.
105static void addCalleeSavedRegs(LiveRegUnits &LiveUnits,
106 const MachineFunction &MF) {
107 const MachineRegisterInfo &MRI = MF.getRegInfo();
108 const MachineFrameInfo &MFI = MF.getFrameInfo();
109 for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR; ++CSR) {
110 const unsigned N = *CSR;
111
112 const auto &CSI = MFI.getCalleeSavedInfo();
113 auto Info =
114 llvm::find_if(CSI, [N](auto Info) { return Info.getReg() == N; });
115 // If we have no info for this callee-saved register, assume it is liveout
116 if (Info == CSI.end() || Info->isRestored())
117 LiveUnits.addReg(N);
118 }
119}
120
121void LiveRegUnits::addPristines(const MachineFunction &MF) {
122 const MachineFrameInfo &MFI = MF.getFrameInfo();
123 if (!MFI.isCalleeSavedInfoValid())
124 return;
125 /// This function will usually be called on an empty object, handle this
126 /// as a special case.
127 if (empty()) {
128 /// Add all callee saved regs, then remove the ones that are saved and
129 /// restored.
130 addCalleeSavedRegs(*this, MF);
131 /// Remove the ones that are not saved/restored; they are pristine.
132 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
133 removeReg(Info.getReg());
134 return;
135 }
136 /// If a callee-saved register that is not pristine is already present
137 /// in the set, we should make sure that it stays in it. Precompute the
138 /// set of pristine registers in a separate object.
139 /// Add all callee saved regs, then remove the ones that are saved+restored.
140 LiveRegUnits Pristine(*TRI);
141 addCalleeSavedRegs(Pristine, MF);
142 /// Remove the ones that are not saved/restored; they are pristine.
143 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
144 Pristine.removeReg(Info.getReg());
145 addUnits(Pristine.getBitVector());
146}
147
149 const MachineFunction &MF = *MBB.getParent();
150 addPristines(MF);
151 addBlockLiveOuts(*this, MBB);
152
153 // For the return block: Add all callee saved registers.
154 if (MBB.isReturnBlock()) {
155 const MachineFrameInfo &MFI = MF.getFrameInfo();
156 if (MFI.isCalleeSavedInfoValid())
157 addCalleeSavedRegs(*this, MF);
158 }
159}
160
162 const MachineFunction &MF = *MBB.getParent();
163 addPristines(MF);
164 addBlockLiveIns(*this, MBB);
165}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
IRTranslator LLVM IR MI
static void addCalleeSavedRegs(LivePhysRegs &LiveRegs, const MachineFunction &MF)
Adds all callee saved registers to LiveRegs.
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.
A set of register units.
A set of register units used to track register liveness.
LLVM_ABI void addRegsInMask(const uint32_t *RegMask)
Adds register units not preserved by the regmask RegMask.
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.
LLVM_ABI void removeRegsNotPreserved(const uint32_t *RegMask)
Removes register units not preserved by the regmask RegMask.
LLVM_ABI void accumulate(const MachineInstr &MI)
Adds all register units used, defined or clobbered in MI.
MCRegUnitRootIterator enumerates the root registers of a register unit.
bool isValid() const
Check if the iterator is at the end of the list.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
bool isCalleeSavedInfoValid() const
Has the callee saved info been calculated yet?
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
static bool clobbersPhysReg(const uint32_t *RegMask, MCRegister PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI const MCPhysReg * getCalleeSavedRegs() const
Returns list of callee saved registers.
This is an optimization pass for GlobalISel generic memory operations.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1771
#define N