LLVM 24.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 for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR; ++CSR)
109 LiveUnits.addReg(*CSR);
110}
111
112void LiveRegUnits::addPristines(const MachineFunction &MF) {
113 const MachineFrameInfo &MFI = MF.getFrameInfo();
114 if (!MFI.isCalleeSavedInfoValid())
115 return;
116 /// This function will usually be called on an empty object, handle this
117 /// as a special case.
118 if (empty()) {
119 /// Add all callee saved regs, then remove the ones that are saved and
120 /// restored.
121 addCalleeSavedRegs(*this, MF);
122 /// Remove the ones that are not saved/restored; they are pristine.
123 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
124 removeReg(Info.getReg());
125 return;
126 }
127 /// If a callee-saved register that is not pristine is already present
128 /// in the set, we should make sure that it stays in it. Precompute the
129 /// set of pristine registers in a separate object.
130 /// Add all callee saved regs, then remove the ones that are saved+restored.
131 LiveRegUnits Pristine(*TRI);
132 addCalleeSavedRegs(Pristine, MF);
133 /// Remove the ones that are not saved/restored; they are pristine.
134 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
135 Pristine.removeReg(Info.getReg());
136 addUnits(Pristine.getBitVector());
137}
138
140 const MachineFunction &MF = *MBB.getParent();
141 addPristines(MF);
142 addBlockLiveOuts(*this, MBB);
143
144 // For the return block: Add all callee saved registers.
145 if (MBB.isReturnBlock()) {
146 const MachineFrameInfo &MFI = MF.getFrameInfo();
147 if (MFI.isCalleeSavedInfoValid()) {
148 addCalleeSavedRegs(*this, MF);
149 // We assume callee-saved registers without CalleeSavedInfo are liveout.
150 for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo()) {
151 if (!Info.isRestored())
152 removeReg(Info.getReg());
153 }
154 }
155 }
156}
157
159 const MachineFunction &MF = *MBB.getParent();
160 addPristines(MF);
161 addBlockLiveIns(*this, MBB);
162}
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.
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.
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