LLVM 22.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 (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) {
24 for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) {
25 if (MachineOperand::clobbersPhysReg(RegMask, *RootReg)) {
26 Units.reset(U);
27 break;
28 }
29 }
30 }
31}
32
34 for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) {
35 for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) {
36 if (MachineOperand::clobbersPhysReg(RegMask, *RootReg)) {
37 Units.set(U);
38 break;
39 }
40 }
41 }
42}
43
45 // Remove defined registers and regmask kills from the set.
46 for (const MachineOperand &MOP : MI.operands()) {
47 if (MOP.isReg()) {
48 if (MOP.isDef() && MOP.getReg().isPhysical())
49 removeReg(MOP.getReg());
50 continue;
51 }
52
53 if (MOP.isRegMask()) {
54 removeRegsNotPreserved(MOP.getRegMask());
55 continue;
56 }
57 }
58
59 // Add uses to the set.
60 for (const MachineOperand &MOP : MI.operands()) {
61 if (!MOP.isReg() || !MOP.readsReg())
62 continue;
63
64 if (MOP.getReg().isPhysical())
65 addReg(MOP.getReg());
66 }
67}
68
70 // Add defs, uses and regmask clobbers to the set.
71 for (const MachineOperand &MOP : MI.operands()) {
72 if (MOP.isReg()) {
73 if (!MOP.getReg().isPhysical())
74 continue;
75 if (MOP.isDef() || MOP.readsReg())
76 addReg(MOP.getReg());
77 continue;
78 }
79
80 if (MOP.isRegMask()) {
81 addRegsInMask(MOP.getRegMask());
82 continue;
83 }
84 }
85}
86
87/// Add live-in registers of basic block \p MBB to \p LiveUnits.
88static void addBlockLiveIns(LiveRegUnits &LiveUnits,
89 const MachineBasicBlock &MBB) {
90 for (const auto &LI : MBB.liveins())
91 LiveUnits.addRegMasked(LI.PhysReg, LI.LaneMask);
92}
93
94/// Add live-out registers of basic block \p MBB to \p LiveUnits.
95static void addBlockLiveOuts(LiveRegUnits &LiveUnits,
96 const MachineBasicBlock &MBB) {
97 for (const auto &LO : MBB.liveouts())
98 LiveUnits.addRegMasked(LO.PhysReg, LO.LaneMask);
99}
100
101/// Adds all callee saved registers to \p LiveUnits.
102static void addCalleeSavedRegs(LiveRegUnits &LiveUnits,
103 const MachineFunction &MF) {
104 const MachineRegisterInfo &MRI = MF.getRegInfo();
105 const MachineFrameInfo &MFI = MF.getFrameInfo();
106 for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR; ++CSR) {
107 const unsigned N = *CSR;
108
109 const auto &CSI = MFI.getCalleeSavedInfo();
110 auto Info =
111 llvm::find_if(CSI, [N](auto Info) { return Info.getReg() == N; });
112 // If we have no info for this callee-saved register, assume it is liveout
113 if (Info == CSI.end() || Info->isRestored())
114 LiveUnits.addReg(N);
115 }
116}
117
118void LiveRegUnits::addPristines(const MachineFunction &MF) {
119 const MachineFrameInfo &MFI = MF.getFrameInfo();
120 if (!MFI.isCalleeSavedInfoValid())
121 return;
122 /// This function will usually be called on an empty object, handle this
123 /// as a special case.
124 if (empty()) {
125 /// Add all callee saved regs, then remove the ones that are saved and
126 /// restored.
127 addCalleeSavedRegs(*this, MF);
128 /// Remove the ones that are not saved/restored; they are pristine.
129 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
130 removeReg(Info.getReg());
131 return;
132 }
133 /// If a callee-saved register that is not pristine is already present
134 /// in the set, we should make sure that it stays in it. Precompute the
135 /// set of pristine registers in a separate object.
136 /// Add all callee saved regs, then remove the ones that are saved+restored.
137 LiveRegUnits Pristine(*TRI);
138 addCalleeSavedRegs(Pristine, MF);
139 /// Remove the ones that are not saved/restored; they are pristine.
140 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
141 Pristine.removeReg(Info.getReg());
142 addUnits(Pristine.getBitVector());
143}
144
146 const MachineFunction &MF = *MBB.getParent();
147 addPristines(MF);
148 addBlockLiveOuts(*this, MBB);
149
150 // For the return block: Add all callee saved registers.
151 if (MBB.isReturnBlock()) {
152 const MachineFrameInfo &MFI = MF.getFrameInfo();
153 if (MFI.isCalleeSavedInfoValid())
154 addCalleeSavedRegs(*this, MF);
155 }
156}
157
159 const MachineFunction &MF = *MBB.getParent();
160 addPristines(MF);
161 addBlockLiveIns(*this, MBB);
162}
unsigned const MachineRegisterInfo * MRI
MachineBasicBlock & MBB
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
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.
BitVector & reset()
Definition: BitVector.h:392
BitVector & set()
Definition: BitVector.h:351
The CalleeSavedInfo class tracks the information need to locate where a callee saved register is in t...
A set of register units used to track register liveness.
Definition: LiveRegUnits.h:31
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.
Definition: LiveRegUnits.h:87
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.
Definition: LiveRegUnits.h:145
void removeReg(MCRegister Reg)
Removes all register units covered by physical register Reg.
Definition: LiveRegUnits.h:103
void addRegMasked(MCRegister Reg, LaneBitmask Mask)
Adds register units covered by physical register Reg that are part of the lanemask Mask.
Definition: LiveRegUnits.h:94
bool empty() const
Returns true if the set is empty.
Definition: LiveRegUnits.h:84
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.
unsigned getNumRegUnits() const
Return the number of (native) register units in the target.
iterator_range< liveout_iterator > liveouts() const
iterator_range< livein_iterator > liveins() const
bool isReturnBlock() const
Convenience function that returns true if the block ends in a return instruction.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
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.
Definition: MachineInstr.h:72
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,...
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:1777
#define N