LLVM  6.0.0svn
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 
23 #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 void LiveRegUnits::addPristines(const MachineFunction &MF) {
101  const MachineFrameInfo &MFI = MF.getFrameInfo();
102  if (!MFI.isCalleeSavedInfoValid())
103  return;
104  /// This function will usually be called on an empty object, handle this
105  /// as a special case.
106  if (empty()) {
107  /// Add all callee saved regs, then remove the ones that are saved and
108  /// restored.
109  addCalleeSavedRegs(*this, MF);
110  /// Remove the ones that are not saved/restored; they are pristine.
111  for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
112  removeReg(Info.getReg());
113  return;
114  }
115  /// If a callee-saved register that is not pristine is already present
116  /// in the set, we should make sure that it stays in it. Precompute the
117  /// set of pristine registers in a separate object.
118  /// Add all callee saved regs, then remove the ones that are saved+restored.
119  LiveRegUnits Pristine(*TRI);
120  addCalleeSavedRegs(Pristine, MF);
121  /// Remove the ones that are not saved/restored; they are pristine.
122  for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
123  Pristine.removeReg(Info.getReg());
124  addUnits(Pristine.getBitVector());
125 }
126 
128  const MachineFunction &MF = *MBB.getParent();
129  if (!MBB.succ_empty()) {
130  addPristines(MF);
131  // To get the live-outs we simply merge the live-ins of all successors.
132  for (const MachineBasicBlock *Succ : MBB.successors())
133  addBlockLiveIns(*this, *Succ);
134  } else if (MBB.isReturnBlock()) {
135  // For the return block: Add all callee saved registers.
136  const MachineFrameInfo &MFI = MF.getFrameInfo();
137  if (MFI.isCalleeSavedInfoValid())
138  addCalleeSavedRegs(*this, MF);
139  }
140 }
141 
143  const MachineFunction &MF = *MBB.getParent();
144  addPristines(MF);
145  addBlockLiveIns(*this, MBB);
146 }
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:54
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
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:123
iterator_range< succ_iterator > successors()
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 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...
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:115
A set of register units.
void addReg(unsigned Reg)
Adds register units covered by physical register Reg.
Definition: LiveRegUnits.h:57
ConstMIBundleOperands - Iterate over all operands in a const bundle of machine instructions.
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
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.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
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.