LLVM  5.0.0
LiveRegUnits.cpp
Go to the documentation of this file.
1 //===- LiveRegUnits.cpp - Register Unit Set -------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file This file imlements the LiveRegUnits set.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 
22 #include "llvm/MC/MCRegisterInfo.h"
24 
25 using namespace llvm;
26 
28  for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) {
29  for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) {
30  if (MachineOperand::clobbersPhysReg(RegMask, *RootReg))
31  Units.reset(U);
32  }
33  }
34 }
35 
36 void LiveRegUnits::addRegsInMask(const uint32_t *RegMask) {
37  for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) {
38  for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) {
39  if (MachineOperand::clobbersPhysReg(RegMask, *RootReg))
40  Units.set(U);
41  }
42  }
43 }
44 
46  // Remove defined registers and regmask kills from the set.
47  for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
48  if (O->isReg()) {
49  if (!O->isDef())
50  continue;
51  unsigned Reg = O->getReg();
53  continue;
54  removeReg(Reg);
55  } else if (O->isRegMask())
56  removeRegsNotPreserved(O->getRegMask());
57  }
58 
59  // Add uses to the set.
60  for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
61  if (!O->isReg() || !O->readsReg())
62  continue;
63  unsigned Reg = O->getReg();
65  continue;
66  addReg(Reg);
67  }
68 }
69 
71  // Add defs, uses and regmask clobbers to the set.
72  for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
73  if (O->isReg()) {
74  unsigned Reg = O->getReg();
76  continue;
77  if (!O->isDef() && !O->readsReg())
78  continue;
79  addReg(Reg);
80  } else if (O->isRegMask())
81  addRegsInMask(O->getRegMask());
82  }
83 }
84 
85 /// Add live-in registers of basic block \p MBB to \p LiveUnits.
86 static void addBlockLiveIns(LiveRegUnits &LiveUnits,
87  const MachineBasicBlock &MBB) {
88  for (const auto &LI : MBB.liveins())
89  LiveUnits.addRegMasked(LI.PhysReg, LI.LaneMask);
90 }
91 
92 /// Adds all callee saved registers to \p LiveUnits.
93 static void addCalleeSavedRegs(LiveRegUnits &LiveUnits,
94  const MachineFunction &MF) {
95  const MachineRegisterInfo &MRI = MF.getRegInfo();
96  for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR; ++CSR)
97  LiveUnits.addReg(*CSR);
98 }
99 
100 /// Adds pristine registers to the given \p LiveUnits. Pristine registers are
101 /// callee saved registers that are unused in the function.
102 static void addPristines(LiveRegUnits &LiveUnits, const MachineFunction &MF) {
103  const MachineFrameInfo &MFI = MF.getFrameInfo();
104  if (!MFI.isCalleeSavedInfoValid())
105  return;
106  /// Add all callee saved regs, then remove the ones that are saved+restored.
107  addCalleeSavedRegs(LiveUnits, MF);
108  /// Remove the ones that are not saved/restored; they are pristine.
109  for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
110  LiveUnits.removeReg(Info.getReg());
111 }
112 
114  const MachineFunction &MF = *MBB.getParent();
115  if (!MBB.succ_empty()) {
116  addPristines(*this, MF);
117  // To get the live-outs we simply merge the live-ins of all successors.
118  for (const MachineBasicBlock *Succ : MBB.successors())
119  addBlockLiveIns(*this, *Succ);
120  } else if (MBB.isReturnBlock()) {
121  // For the return block: Add all callee saved registers.
122  const MachineFrameInfo &MFI = MF.getFrameInfo();
123  if (MFI.isCalleeSavedInfoValid())
124  addCalleeSavedRegs(*this, MF);
125  }
126 }
127 
129  const MachineFunction &MF = *MBB.getParent();
130  addPristines(*this, MF);
131  addBlockLiveIns(*this, MBB);
132 }
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
BitVector & set()
Definition: BitVector.h:398
bool isValid() const
Check if the iterator is at the end of the list.
iterator_range< livein_iterator > liveins() const
static void addPristines(LiveRegUnits &LiveUnits, const MachineFunction &MF)
Adds pristine registers to the given LiveUnits.
static void addBlockLiveIns(LiveRegUnits &LiveUnits, const MachineBasicBlock &MBB)
Add live-in registers of basic block MBB to LiveUnits.
iterator_range< succ_iterator > successors()
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
void addLiveOuts(const MachineBasicBlock &MBB)
Adds registers living out of block MBB.
void removeReg(unsigned Reg)
Removes all register units covered by physical register Reg.
Definition: LiveRegUnits.h:73
Reg
All possible values of the reg field in the ModR/M byte.
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.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
const MCPhysReg * getCalleeSavedRegs() const
Returns list of callee saved registers.
unsigned const MachineRegisterInfo * MRI
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
BitVector & reset()
Definition: BitVector.h:439
void stepBackward(const MachineInstr &MI)
Updates liveness when stepping backwards over the instruction MI.
A set of register units.
bool isCalleeSavedInfoValid() const
Has the callee saved info been calculated yet?
void addReg(unsigned Reg)
Adds register units covered by physical register Reg.
Definition: LiveRegUnits.h:57
bool isReturnBlock() const
Convenience function that returns true if the block ends in a return instruction. ...
ConstMIBundleOperands - Iterate over all operands in a const bundle of machine instructions.
#define E
Definition: LargeTest.cpp:27
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...
static bool clobbersPhysReg(const uint32_t *RegMask, unsigned PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
void addRegMasked(unsigned Reg, LaneBitmask Mask)
Adds register units covered by physical register Reg that are part of the lanemask Mask...
Definition: LiveRegUnits.h:64
unsigned getNumRegUnits() const
Return the number of (native) register units in the target.
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:59
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
A set of register units used to track register liveness.
Definition: LiveRegUnits.h:30
bool isValid() const
isValid - Returns true until all the operands have been visited.
void removeRegsNotPreserved(const uint32_t *RegMask)
Removes register units not preserved by the regmask RegMask.
IRTranslator LLVM IR MI