LLVM  13.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 
20 using 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  }
28  }
29 }
30 
31 void LiveRegUnits::addRegsInMask(const uint32_t *RegMask) {
32  for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) {
33  for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) {
34  if (MachineOperand::clobbersPhysReg(RegMask, *RootReg))
35  Units.set(U);
36  }
37  }
38 }
39 
41  // Remove defined registers and regmask kills from the set.
42  for (const MachineOperand &MOP : phys_regs_and_masks(MI)) {
43  if (MOP.isRegMask()) {
44  removeRegsNotPreserved(MOP.getRegMask());
45  continue;
46  }
47 
48  if (MOP.isDef())
49  removeReg(MOP.getReg());
50  }
51 
52  // Add uses to the set.
53  for (const MachineOperand &MOP : phys_regs_and_masks(MI)) {
54  if (!MOP.isReg() || !MOP.readsReg())
55  continue;
56  addReg(MOP.getReg());
57  }
58 }
59 
61  // Add defs, uses and regmask clobbers to the set.
62  for (const MachineOperand &MOP : phys_regs_and_masks(MI)) {
63  if (MOP.isRegMask()) {
64  addRegsInMask(MOP.getRegMask());
65  continue;
66  }
67  if (!MOP.isDef() && !MOP.readsReg())
68  continue;
69  addReg(MOP.getReg());
70  }
71 }
72 
73 /// Add live-in registers of basic block \p MBB to \p LiveUnits.
74 static void addBlockLiveIns(LiveRegUnits &LiveUnits,
75  const MachineBasicBlock &MBB) {
76  for (const auto &LI : MBB.liveins())
77  LiveUnits.addRegMasked(LI.PhysReg, LI.LaneMask);
78 }
79 
80 /// Adds all callee saved registers to \p LiveUnits.
81 static void addCalleeSavedRegs(LiveRegUnits &LiveUnits,
82  const MachineFunction &MF) {
83  const MachineRegisterInfo &MRI = MF.getRegInfo();
84  const MachineFrameInfo &MFI = MF.getFrameInfo();
85  for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR; ++CSR) {
86  const unsigned N = *CSR;
87 
88  const auto &CSI = MFI.getCalleeSavedInfo();
89  auto Info =
90  llvm::find_if(CSI, [N](auto Info) { return Info.getReg() == N; });
91  // If we have no info for this callee-saved register, assume it is liveout
92  if (Info == CSI.end() || Info->isRestored())
93  LiveUnits.addReg(N);
94  }
95 }
96 
97 void LiveRegUnits::addPristines(const MachineFunction &MF) {
98  const MachineFrameInfo &MFI = MF.getFrameInfo();
99  if (!MFI.isCalleeSavedInfoValid())
100  return;
101  /// This function will usually be called on an empty object, handle this
102  /// as a special case.
103  if (empty()) {
104  /// Add all callee saved regs, then remove the ones that are saved and
105  /// restored.
106  addCalleeSavedRegs(*this, MF);
107  /// Remove the ones that are not saved/restored; they are pristine.
108  for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
109  removeReg(Info.getReg());
110  return;
111  }
112  /// If a callee-saved register that is not pristine is already present
113  /// in the set, we should make sure that it stays in it. Precompute the
114  /// set of pristine registers in a separate object.
115  /// Add all callee saved regs, then remove the ones that are saved+restored.
116  LiveRegUnits Pristine(*TRI);
117  addCalleeSavedRegs(Pristine, MF);
118  /// Remove the ones that are not saved/restored; they are pristine.
119  for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
120  Pristine.removeReg(Info.getReg());
121  addUnits(Pristine.getBitVector());
122 }
123 
125  const MachineFunction &MF = *MBB.getParent();
126 
127  addPristines(MF);
128 
129  // To get the live-outs we simply merge the live-ins of all successors.
130  for (const MachineBasicBlock *Succ : MBB.successors())
131  addBlockLiveIns(*this, *Succ);
132 
133  // For the return block: Add all callee saved registers.
134  if (MBB.isReturnBlock()) {
135  const MachineFrameInfo &MFI = MF.getFrameInfo();
136  if (MFI.isCalleeSavedInfoValid())
137  addCalleeSavedRegs(*this, MF);
138  }
139 }
140 
142  const MachineFunction &MF = *MBB.getParent();
143  addPristines(MF);
144  addBlockLiveIns(*this, MBB);
145 }
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:100
llvm
Definition: AllocatorList.h:23
llvm::MachineRegisterInfo
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Definition: MachineRegisterInfo.h:52
llvm::BitVector::set
BitVector & set()
Definition: BitVector.h:343
MachineBasicBlock.h
llvm::MachineBasicBlock::isReturnBlock
bool isReturnBlock() const
Convenience function that returns true if the block ends in a return instruction.
Definition: MachineBasicBlock.h:718
llvm::LiveRegUnits::addReg
void addReg(MCPhysReg Reg)
Adds register units covered by physical register Reg.
Definition: LiveRegUnits.h:86
llvm::MachineBasicBlock::liveins
iterator_range< livein_iterator > liveins() const
Definition: MachineBasicBlock.h:412
llvm::LiveRegUnits::addUnits
void addUnits(const BitVector &RegUnits)
Adds all register units marked in the bitvector RegUnits.
Definition: LiveRegUnits.h:144
llvm::LiveRegUnits::addLiveIns
void addLiveIns(const MachineBasicBlock &MBB)
Adds registers living into block MBB.
Definition: LiveRegUnits.cpp:141
llvm::LiveRegUnits::addRegMasked
void addRegMasked(MCPhysReg Reg, LaneBitmask Mask)
Adds register units covered by physical register Reg that are part of the lanemask Mask.
Definition: LiveRegUnits.h:93
llvm::MCRegisterInfo::getNumRegUnits
unsigned getNumRegUnits() const
Return the number of (native) register units in the target.
Definition: MCRegisterInfo.h:505
llvm::LiveRegUnits::empty
bool empty() const
Returns true if the set is empty.
Definition: LiveRegUnits.h:83
MachineRegisterInfo.h
addBlockLiveIns
static void addBlockLiveIns(LiveRegUnits &LiveUnits, const MachineBasicBlock &MBB)
Add live-in registers of basic block MBB to LiveUnits.
Definition: LiveRegUnits.cpp:74
llvm::LiveRegUnits::addLiveOuts
void addLiveOuts(const MachineBasicBlock &MBB)
Adds registers living out of block MBB.
Definition: LiveRegUnits.cpp:124
llvm::MachineFunction::getRegInfo
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Definition: MachineFunction.h:565
llvm::LiveRegUnits::accumulate
void accumulate(const MachineInstr &MI)
Adds all register units used, defined or clobbered in MI.
Definition: LiveRegUnits.cpp:60
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::phys_regs_and_masks
iterator_range< filter_iterator< ConstMIBundleOperands, std::function< 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...
Definition: LiveRegUnits.h:166
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:49
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:26
llvm::LiveRegUnits
A set of register units used to track register liveness.
Definition: LiveRegUnits.h:30
llvm::LiveRegUnits::removeReg
void removeReg(MCPhysReg Reg)
Removes all register units covered by physical register Reg.
Definition: LiveRegUnits.h:102
llvm::MachineFrameInfo::isCalleeSavedInfoValid
bool isCalleeSavedInfoValid() const
Has the callee saved info been calculated yet?
Definition: MachineFrameInfo.h:809
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
llvm::MCRegUnitRootIterator::isValid
bool isValid() const
Check if the iterator is at the end of the list.
Definition: MCRegisterInfo.h:765
llvm::LiveRegUnits::stepBackward
void stepBackward(const MachineInstr &MI)
Updates liveness when stepping backwards over the instruction MI.
Definition: LiveRegUnits.cpp:40
llvm::MachineOperand::clobbersPhysReg
static bool clobbersPhysReg(const uint32_t *RegMask, MCRegister PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
Definition: MachineOperand.h:614
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
llvm::MachineRegisterInfo::getCalleeSavedRegs
const MCPhysReg * getCalleeSavedRegs() const
Returns list of callee saved registers.
Definition: MachineRegisterInfo.cpp:620
llvm::MCPhysReg
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition: MCRegister.h:19
addCalleeSavedRegs
static void addCalleeSavedRegs(LiveRegUnits &LiveUnits, const MachineFunction &MF)
Adds all callee saved registers to LiveUnits.
Definition: LiveRegUnits.cpp:81
llvm::MachineFunction::getFrameInfo
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Definition: MachineFunction.h:571
llvm::MachineBasicBlock::getParent
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
Definition: MachineBasicBlock.h:225
llvm::MachineFunction
Definition: MachineFunction.h:227
llvm::MachineFrameInfo::getCalleeSavedInfo
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
Definition: MachineFrameInfo.h:796
llvm::MachineBasicBlock::successors
iterator_range< succ_iterator > successors()
Definition: MachineBasicBlock.h:355
uint32_t
LiveRegUnits.h
MRI
unsigned const MachineRegisterInfo * MRI
Definition: AArch64AdvSIMDScalarPass.cpp:105
llvm::find_if
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:1532
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::CalleeSavedInfo
The CalleeSavedInfo class tracks the information need to locate where a callee saved register is in t...
Definition: MachineFrameInfo.h:34
MachineFrameInfo.h
llvm::LiveRegUnits::removeRegsNotPreserved
void removeRegsNotPreserved(const uint32_t *RegMask)
Removes register units not preserved by the regmask RegMask.
Definition: LiveRegUnits.cpp:22
llvm::BitVector::reset
BitVector & reset()
Definition: BitVector.h:384
llvm::MCRegUnitRootIterator
MCRegUnitRootIterator enumerates the root registers of a register unit.
Definition: MCRegisterInfo.h:746
llvm::MachineFrameInfo
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
Definition: MachineFrameInfo.h:107
N
#define N
MachineOperand.h
llvm::LiveRegUnits::addRegsInMask
void addRegsInMask(const uint32_t *RegMask)
Adds register units not preserved by the regmask RegMask.
Definition: LiveRegUnits.cpp:31
MachineFunction.h