LLVM  3.7.0
LivePhysRegs.cpp
Go to the documentation of this file.
1 //===--- LivePhysRegs.cpp - Live Physical Register 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 // This file implements the LivePhysRegs utility for tracking liveness of
11 // physical registers across machine instructions in forward or backward order.
12 // A more detailed description can be found in the corresponding header file.
13 //
14 //===----------------------------------------------------------------------===//
15 
20 #include "llvm/Support/Debug.h"
22 using namespace llvm;
23 
24 
25 /// \brief Remove all registers from the set that get clobbered by the register
26 /// mask.
27 /// The clobbers set will be the list of live registers clobbered
28 /// by the regmask.
30  SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> *Clobbers) {
31  SparseSet<unsigned>::iterator LRI = LiveRegs.begin();
32  while (LRI != LiveRegs.end()) {
33  if (MO.clobbersPhysReg(*LRI)) {
34  if (Clobbers)
35  Clobbers->push_back(std::make_pair(*LRI, &MO));
36  LRI = LiveRegs.erase(LRI);
37  } else
38  ++LRI;
39  }
40 }
41 
42 /// Simulates liveness when stepping backwards over an instruction(bundle):
43 /// Remove Defs, add uses. This is the recommended way of calculating liveness.
45  // Remove defined registers and regmask kills from the set.
46  for (ConstMIBundleOperands O(&MI); O.isValid(); ++O) {
47  if (O->isReg()) {
48  if (!O->isDef())
49  continue;
50  unsigned Reg = O->getReg();
51  if (Reg == 0)
52  continue;
53  removeReg(Reg);
54  } else if (O->isRegMask())
55  removeRegsInMask(*O, nullptr);
56  }
57 
58  // Add uses to the set.
59  for (ConstMIBundleOperands O(&MI); O.isValid(); ++O) {
60  if (!O->isReg() || !O->readsReg() || O->isUndef())
61  continue;
62  unsigned Reg = O->getReg();
63  if (Reg == 0)
64  continue;
65  addReg(Reg);
66  }
67 }
68 
69 /// Simulates liveness when stepping forward over an instruction(bundle): Remove
70 /// killed-uses, add defs. This is the not recommended way, because it depends
71 /// on accurate kill flags. If possible use stepBackwards() instead of this
72 /// function.
74  SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> &Clobbers) {
75  // Remove killed registers from the set.
76  for (ConstMIBundleOperands O(&MI); O.isValid(); ++O) {
77  if (O->isReg()) {
78  unsigned Reg = O->getReg();
79  if (Reg == 0)
80  continue;
81  if (O->isDef()) {
82  // Note, dead defs are still recorded. The caller should decide how to
83  // handle them.
84  Clobbers.push_back(std::make_pair(Reg, &*O));
85  } else {
86  if (!O->isKill())
87  continue;
88  assert(O->isUse());
89  removeReg(Reg);
90  }
91  } else if (O->isRegMask())
92  removeRegsInMask(*O, &Clobbers);
93  }
94 
95  // Add defs to the set.
96  for (auto Reg : Clobbers) {
97  // Skip dead defs. They shouldn't be added to the set.
98  if (Reg.second->isReg() && Reg.second->isDead())
99  continue;
100  addReg(Reg.first);
101  }
102 }
103 
104 /// Prin the currently live registers to OS.
106  OS << "Live Registers:";
107  if (!TRI) {
108  OS << " (uninitialized)\n";
109  return;
110  }
111 
112  if (empty()) {
113  OS << " (empty)\n";
114  return;
115  }
116 
117  for (const_iterator I = begin(), E = end(); I != E; ++I)
118  OS << " " << PrintReg(*I, TRI);
119  OS << "\n";
120 }
121 
122 /// Dumps the currently live registers to the debug output.
123 void LivePhysRegs::dump() const {
124 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
125  dbgs() << " " << *this;
126 #endif
127 }
128 
129 /// Add live-in registers of basic block \p MBB to \p LiveRegs.
130 static void addLiveIns(LivePhysRegs &LiveRegs, const MachineBasicBlock &MBB) {
131  for (unsigned Reg : make_range(MBB.livein_begin(), MBB.livein_end()))
132  LiveRegs.addReg(Reg);
133 }
134 
135 /// Add pristine registers to the given \p LiveRegs. This function removes
136 /// actually saved callee save registers when \p InPrologueEpilogue is false.
137 static void addPristines(LivePhysRegs &LiveRegs, const MachineFunction &MF,
138  const TargetRegisterInfo &TRI) {
139  const MachineFrameInfo &MFI = *MF.getFrameInfo();
140  if (!MFI.isCalleeSavedInfoValid())
141  return;
142 
143  for (const MCPhysReg *CSR = TRI.getCalleeSavedRegs(&MF); CSR && *CSR; ++CSR)
144  LiveRegs.addReg(*CSR);
145  for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
146  LiveRegs.removeReg(Info.getReg());
147 }
148 
150  bool AddPristines) {
151  if (AddPristines) {
152  const MachineFunction &MF = *MBB->getParent();
153  addPristines(*this, MF, *TRI);
154  }
155  for (const MachineBasicBlock *Succ : MBB->successors())
156  ::addLiveIns(*this, *Succ);
157 }
158 
160  bool AddPristines) {
161  if (AddPristines) {
162  const MachineFunction &MF = *MBB->getParent();
163  addPristines(*this, MF, *TRI);
164  }
165  ::addLiveIns(*this, *MBB);
166 }
const MachineFunction * getParent() const
getParent - Return the MachineFunction containing this basic block.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
SparseSet< unsigned >::const_iterator const_iterator
Definition: LivePhysRegs.h:129
iterator_range< succ_iterator > successors()
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
livein_iterator livein_begin() const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APInt.h:33
const_iterator end() const
Definition: SparseSet.h:175
Reg
All possible values of the reg field in the ModR/M byte.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
void print(raw_ostream &OS) const
Prints the currently live registers to OS.
PrintReg - Helper class for printing registers on a raw_ostream.
void stepForward(const MachineInstr &MI, SmallVectorImpl< std::pair< unsigned, const MachineOperand * >> &Clobbers)
Simulates liveness when stepping forward over an instruction(bundle): Remove killed-uses, add defs.
virtual const MCPhysReg * getCalleeSavedRegs(const MachineFunction *MF) const =0
getCalleeSavedRegs - Return a null-terminated list of all of the callee saved registers on this targe...
static void addPristines(LivePhysRegs &LiveRegs, const MachineFunction &MF, const TargetRegisterInfo &TRI)
Add pristine registers to the given LiveRegs.
iterator erase(iterator I)
erase - Erases an existing element identified by a valid iterator.
Definition: SparseSet.h:280
void addLiveOuts(const MachineBasicBlock *MBB, bool AddPristines=false)
Adds all live-out registers of basic block MBB; After prologue/ epilogue insertion AddPristines shoul...
livein_iterator livein_end() const
static void addLiveIns(LivePhysRegs &LiveRegs, const MachineBasicBlock &MBB)
Add live-in registers of basic block MBB to LiveRegs.
void stepBackward(const MachineInstr &MI)
Simulates liveness when stepping backwards over an instruction(bundle): Remove Defs, add uses.
bool isCalleeSavedInfoValid() const
Has the callee saved info been calculated yet?
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
const_iterator begin() const
Definition: SparseSet.h:174
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void removeRegsInMask(const MachineOperand &MO, SmallVectorImpl< std::pair< unsigned, const MachineOperand * >> *Clobbers)
Removes physical registers clobbered by the regmask operand MO.
void addLiveIns(const MachineBasicBlock *MBB, bool AddPristines=false)
Adds all live-in registers of basic block MBB; After prologue/ epilogue insertion AddPristines should...
ConstMIBundleOperands - Iterate over all operands in a const bundle of machine instructions.
bool empty() const
Returns true if the set is empty.
Definition: LivePhysRegs.h:71
MachineOperand class - Representation of each machine instruction operand.
MachineFrameInfo * getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
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.
Representation of each machine instruction.
Definition: MachineInstr.h:51
void removeReg(unsigned Reg)
Removes a physical register, all its sub-registers, and all its super-registers from the set...
Definition: LivePhysRegs.h:84
SparseSet - Fast set implmentation for objects that can be identified by small unsigned keys...
Definition: SparseSet.h:120
A set of live physical registers with functions to track liveness when walking backward/forward throu...
Definition: LivePhysRegs.h:43
void dump() const
Dumps the currently live registers to the debug output.
#define I(x, y, z)
Definition: MD5.cpp:54
bool isValid() const
isValid - Returns true until all the operands have been visited.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
const_iterator end() const
Definition: LivePhysRegs.h:131
const_iterator begin() const
Definition: LivePhysRegs.h:130
void addReg(unsigned Reg)
Adds a physical register and all its sub-registers to the set.
Definition: LivePhysRegs.h:74