1 //===--- LivePhysRegs.cpp - Live Physical Register Set --------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the LivePhysRegs utility for tracking liveness of
10 // physical registers across machine instructions in forward or backward order.
11 // A more detailed description can be found in the corresponding header file.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/LivePhysRegs.h"
16 #include "llvm/CodeGen/MachineFrameInfo.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineInstrBundle.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/Config/llvm-config.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
26 /// Remove all registers from the set that get clobbered by the register
28 /// The clobbers set will be the list of live registers clobbered
30 void LivePhysRegs::removeRegsInMask(const MachineOperand
&MO
,
31 SmallVectorImpl
<std::pair
<MCPhysReg
, const MachineOperand
*>> *Clobbers
) {
32 RegisterSet::iterator LRI
= LiveRegs
.begin();
33 while (LRI
!= LiveRegs
.end()) {
34 if (MO
.clobbersPhysReg(*LRI
)) {
36 Clobbers
->push_back(std::make_pair(*LRI
, &MO
));
37 LRI
= LiveRegs
.erase(LRI
);
43 /// Remove defined registers and regmask kills from the set.
44 void LivePhysRegs::removeDefs(const MachineInstr
&MI
) {
45 for (ConstMIBundleOperands
O(MI
); O
.isValid(); ++O
) {
47 if (!O
->isDef() || O
->isDebug())
49 unsigned Reg
= O
->getReg();
50 if (!TargetRegisterInfo::isPhysicalRegister(Reg
))
53 } else if (O
->isRegMask())
58 /// Add uses to the set.
59 void LivePhysRegs::addUses(const MachineInstr
&MI
) {
60 for (ConstMIBundleOperands
O(MI
); O
.isValid(); ++O
) {
61 if (!O
->isReg() || !O
->readsReg() || O
->isDebug())
63 unsigned Reg
= O
->getReg();
64 if (!TargetRegisterInfo::isPhysicalRegister(Reg
))
70 /// Simulates liveness when stepping backwards over an instruction(bundle):
71 /// Remove Defs, add uses. This is the recommended way of calculating liveness.
72 void LivePhysRegs::stepBackward(const MachineInstr
&MI
) {
73 // Remove defined registers and regmask kills from the set.
76 // Add uses to the set.
80 /// Simulates liveness when stepping forward over an instruction(bundle): Remove
81 /// killed-uses, add defs. This is the not recommended way, because it depends
82 /// on accurate kill flags. If possible use stepBackward() instead of this
84 void LivePhysRegs::stepForward(const MachineInstr
&MI
,
85 SmallVectorImpl
<std::pair
<MCPhysReg
, const MachineOperand
*>> &Clobbers
) {
86 // Remove killed registers from the set.
87 for (ConstMIBundleOperands
O(MI
); O
.isValid(); ++O
) {
88 if (O
->isReg() && !O
->isDebug()) {
89 unsigned Reg
= O
->getReg();
90 if (!TargetRegisterInfo::isPhysicalRegister(Reg
))
93 // Note, dead defs are still recorded. The caller should decide how to
95 Clobbers
.push_back(std::make_pair(Reg
, &*O
));
102 } else if (O
->isRegMask())
103 removeRegsInMask(*O
, &Clobbers
);
106 // Add defs to the set.
107 for (auto Reg
: Clobbers
) {
108 // Skip dead defs and registers clobbered by regmasks. They shouldn't
109 // be added to the set.
110 if (Reg
.second
->isReg() && Reg
.second
->isDead())
112 if (Reg
.second
->isRegMask() &&
113 MachineOperand::clobbersPhysReg(Reg
.second
->getRegMask(), Reg
.first
))
119 /// Prin the currently live registers to OS.
120 void LivePhysRegs::print(raw_ostream
&OS
) const {
121 OS
<< "Live Registers:";
123 OS
<< " (uninitialized)\n";
132 for (const_iterator I
= begin(), E
= end(); I
!= E
; ++I
)
133 OS
<< " " << printReg(*I
, TRI
);
137 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
138 LLVM_DUMP_METHOD
void LivePhysRegs::dump() const {
139 dbgs() << " " << *this;
143 bool LivePhysRegs::available(const MachineRegisterInfo
&MRI
,
144 MCPhysReg Reg
) const {
145 if (LiveRegs
.count(Reg
))
147 if (MRI
.isReserved(Reg
))
149 for (MCRegAliasIterator
R(Reg
, TRI
, false); R
.isValid(); ++R
) {
150 if (LiveRegs
.count(*R
))
156 /// Add live-in registers of basic block \p MBB to \p LiveRegs.
157 void LivePhysRegs::addBlockLiveIns(const MachineBasicBlock
&MBB
) {
158 for (const auto &LI
: MBB
.liveins()) {
159 MCPhysReg Reg
= LI
.PhysReg
;
160 LaneBitmask Mask
= LI
.LaneMask
;
161 MCSubRegIndexIterator
S(Reg
, TRI
);
162 assert(Mask
.any() && "Invalid livein mask");
163 if (Mask
.all() || !S
.isValid()) {
167 for (; S
.isValid(); ++S
) {
168 unsigned SI
= S
.getSubRegIndex();
169 if ((Mask
& TRI
->getSubRegIndexLaneMask(SI
)).any())
170 addReg(S
.getSubReg());
175 /// Adds all callee saved registers to \p LiveRegs.
176 static void addCalleeSavedRegs(LivePhysRegs
&LiveRegs
,
177 const MachineFunction
&MF
) {
178 const MachineRegisterInfo
&MRI
= MF
.getRegInfo();
179 for (const MCPhysReg
*CSR
= MRI
.getCalleeSavedRegs(); CSR
&& *CSR
; ++CSR
)
180 LiveRegs
.addReg(*CSR
);
183 void LivePhysRegs::addPristines(const MachineFunction
&MF
) {
184 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
185 if (!MFI
.isCalleeSavedInfoValid())
187 /// This function will usually be called on an empty object, handle this
188 /// as a special case.
190 /// Add all callee saved regs, then remove the ones that are saved and
192 addCalleeSavedRegs(*this, MF
);
193 /// Remove the ones that are not saved/restored; they are pristine.
194 for (const CalleeSavedInfo
&Info
: MFI
.getCalleeSavedInfo())
195 removeReg(Info
.getReg());
198 /// If a callee-saved register that is not pristine is already present
199 /// in the set, we should make sure that it stays in it. Precompute the
200 /// set of pristine registers in a separate object.
201 /// Add all callee saved regs, then remove the ones that are saved+restored.
202 LivePhysRegs
Pristine(*TRI
);
203 addCalleeSavedRegs(Pristine
, MF
);
204 /// Remove the ones that are not saved/restored; they are pristine.
205 for (const CalleeSavedInfo
&Info
: MFI
.getCalleeSavedInfo())
206 Pristine
.removeReg(Info
.getReg());
207 for (MCPhysReg R
: Pristine
)
211 void LivePhysRegs::addLiveOutsNoPristines(const MachineBasicBlock
&MBB
) {
212 // To get the live-outs we simply merge the live-ins of all successors.
213 for (const MachineBasicBlock
*Succ
: MBB
.successors())
214 addBlockLiveIns(*Succ
);
215 if (MBB
.isReturnBlock()) {
216 // Return blocks are a special case because we currently don't mark up
217 // return instructions completely: specifically, there is no explicit
218 // use for callee-saved registers. So we add all callee saved registers
219 // that are saved and restored (somewhere). This does not include
220 // callee saved registers that are unused and hence not saved and
221 // restored; they are called pristine.
222 // FIXME: PEI should add explicit markings to return instructions
223 // instead of implicitly handling them here.
224 const MachineFunction
&MF
= *MBB
.getParent();
225 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
226 if (MFI
.isCalleeSavedInfoValid()) {
227 for (const CalleeSavedInfo
&Info
: MFI
.getCalleeSavedInfo())
228 if (Info
.isRestored())
229 addReg(Info
.getReg());
234 void LivePhysRegs::addLiveOuts(const MachineBasicBlock
&MBB
) {
235 const MachineFunction
&MF
= *MBB
.getParent();
237 addLiveOutsNoPristines(MBB
);
240 void LivePhysRegs::addLiveIns(const MachineBasicBlock
&MBB
) {
241 const MachineFunction
&MF
= *MBB
.getParent();
243 addBlockLiveIns(MBB
);
246 void llvm::computeLiveIns(LivePhysRegs
&LiveRegs
,
247 const MachineBasicBlock
&MBB
) {
248 const MachineFunction
&MF
= *MBB
.getParent();
249 const MachineRegisterInfo
&MRI
= MF
.getRegInfo();
250 const TargetRegisterInfo
&TRI
= *MRI
.getTargetRegisterInfo();
252 LiveRegs
.addLiveOutsNoPristines(MBB
);
253 for (const MachineInstr
&MI
: make_range(MBB
.rbegin(), MBB
.rend()))
254 LiveRegs
.stepBackward(MI
);
257 void llvm::addLiveIns(MachineBasicBlock
&MBB
, const LivePhysRegs
&LiveRegs
) {
258 assert(MBB
.livein_empty() && "Expected empty live-in list");
259 const MachineFunction
&MF
= *MBB
.getParent();
260 const MachineRegisterInfo
&MRI
= MF
.getRegInfo();
261 const TargetRegisterInfo
&TRI
= *MRI
.getTargetRegisterInfo();
262 for (MCPhysReg Reg
: LiveRegs
) {
263 if (MRI
.isReserved(Reg
))
265 // Skip the register if we are about to add one of its super registers.
266 bool ContainsSuperReg
= false;
267 for (MCSuperRegIterator
SReg(Reg
, &TRI
); SReg
.isValid(); ++SReg
) {
268 if (LiveRegs
.contains(*SReg
) && !MRI
.isReserved(*SReg
)) {
269 ContainsSuperReg
= true;
273 if (ContainsSuperReg
)
279 void llvm::recomputeLivenessFlags(MachineBasicBlock
&MBB
) {
280 const MachineFunction
&MF
= *MBB
.getParent();
281 const MachineRegisterInfo
&MRI
= MF
.getRegInfo();
282 const TargetRegisterInfo
&TRI
= *MRI
.getTargetRegisterInfo();
284 // We walk through the block backwards and start with the live outs.
285 LivePhysRegs LiveRegs
;
287 LiveRegs
.addLiveOutsNoPristines(MBB
);
289 for (MachineInstr
&MI
: make_range(MBB
.rbegin(), MBB
.rend())) {
290 // Recompute dead flags.
291 for (MIBundleOperands
MO(MI
); MO
.isValid(); ++MO
) {
292 if (!MO
->isReg() || !MO
->isDef() || MO
->isDebug())
295 unsigned Reg
= MO
->getReg();
298 assert(TargetRegisterInfo::isPhysicalRegister(Reg
));
300 bool IsNotLive
= LiveRegs
.available(MRI
, Reg
);
301 MO
->setIsDead(IsNotLive
);
304 // Step backward over defs.
305 LiveRegs
.removeDefs(MI
);
307 // Recompute kill flags.
308 for (MIBundleOperands
MO(MI
); MO
.isValid(); ++MO
) {
309 if (!MO
->isReg() || !MO
->readsReg() || MO
->isDebug())
312 unsigned Reg
= MO
->getReg();
315 assert(TargetRegisterInfo::isPhysicalRegister(Reg
));
317 bool IsNotLive
= LiveRegs
.available(MRI
, Reg
);
318 MO
->setIsKill(IsNotLive
);
321 // Complete the stepbackward.
322 LiveRegs
.addUses(MI
);
326 void llvm::computeAndAddLiveIns(LivePhysRegs
&LiveRegs
,
327 MachineBasicBlock
&MBB
) {
328 computeLiveIns(LiveRegs
, MBB
);
329 addLiveIns(MBB
, LiveRegs
);