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