LLVM  5.0.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 
21 #include "llvm/Support/Debug.h"
23 using namespace llvm;
24 
25 
26 /// \brief Remove all registers from the set that get clobbered by the register
27 /// mask.
28 /// The clobbers set will be the list of live registers clobbered
29 /// by the regmask.
31  SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> *Clobbers) {
32  SparseSet<unsigned>::iterator LRI = LiveRegs.begin();
33  while (LRI != LiveRegs.end()) {
34  if (MO.clobbersPhysReg(*LRI)) {
35  if (Clobbers)
36  Clobbers->push_back(std::make_pair(*LRI, &MO));
37  LRI = LiveRegs.erase(LRI);
38  } else
39  ++LRI;
40  }
41 }
42 
43 /// Simulates liveness when stepping backwards over an instruction(bundle):
44 /// Remove Defs, add uses. This is the recommended way of calculating liveness.
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  removeRegsInMask(*O);
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 
70 /// Simulates liveness when stepping forward over an instruction(bundle): Remove
71 /// killed-uses, add defs. This is the not recommended way, because it depends
72 /// on accurate kill flags. If possible use stepBackward() instead of this
73 /// function.
75  SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> &Clobbers) {
76  // Remove killed registers from the set.
77  for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
78  if (O->isReg()) {
79  unsigned Reg = O->getReg();
81  continue;
82  if (O->isDef()) {
83  // Note, dead defs are still recorded. The caller should decide how to
84  // handle them.
85  Clobbers.push_back(std::make_pair(Reg, &*O));
86  } else {
87  if (!O->isKill())
88  continue;
89  assert(O->isUse());
90  removeReg(Reg);
91  }
92  } else if (O->isRegMask())
93  removeRegsInMask(*O, &Clobbers);
94  }
95 
96  // Add defs to the set.
97  for (auto Reg : Clobbers) {
98  // Skip dead defs. They shouldn't be added to the set.
99  if (Reg.second->isReg() && Reg.second->isDead())
100  continue;
101  addReg(Reg.first);
102  }
103 }
104 
105 /// Prin the currently live registers to OS.
107  OS << "Live Registers:";
108  if (!TRI) {
109  OS << " (uninitialized)\n";
110  return;
111  }
112 
113  if (empty()) {
114  OS << " (empty)\n";
115  return;
116  }
117 
118  for (const_iterator I = begin(), E = end(); I != E; ++I)
119  OS << " " << PrintReg(*I, TRI);
120  OS << "\n";
121 }
122 
123 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
125  dbgs() << " " << *this;
126 }
127 #endif
128 
130  unsigned Reg) const {
131  if (LiveRegs.count(Reg))
132  return false;
133  if (MRI.isReserved(Reg))
134  return false;
135  for (MCRegAliasIterator R(Reg, TRI, false); R.isValid(); ++R) {
136  if (LiveRegs.count(*R))
137  return false;
138  }
139  return true;
140 }
141 
142 /// Add live-in registers of basic block \p MBB to \p LiveRegs.
143 void LivePhysRegs::addBlockLiveIns(const MachineBasicBlock &MBB) {
144  for (const auto &LI : MBB.liveins()) {
145  unsigned Reg = LI.PhysReg;
146  LaneBitmask Mask = LI.LaneMask;
147  MCSubRegIndexIterator S(Reg, TRI);
148  assert(Mask.any() && "Invalid livein mask");
149  if (Mask.all() || !S.isValid()) {
150  addReg(Reg);
151  continue;
152  }
153  for (; S.isValid(); ++S) {
154  unsigned SI = S.getSubRegIndex();
155  if ((Mask & TRI->getSubRegIndexLaneMask(SI)).any())
156  addReg(S.getSubReg());
157  }
158  }
159 }
160 
161 /// Adds all callee saved registers to \p LiveRegs.
162 static void addCalleeSavedRegs(LivePhysRegs &LiveRegs,
163  const MachineFunction &MF) {
164  const MachineRegisterInfo &MRI = MF.getRegInfo();
165  for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR; ++CSR)
166  LiveRegs.addReg(*CSR);
167 }
168 
169 /// Adds pristine registers to the given \p LiveRegs. Pristine registers are
170 /// callee saved registers that are unused in the function.
171 static void addPristines(LivePhysRegs &LiveRegs, const MachineFunction &MF) {
172  const MachineFrameInfo &MFI = MF.getFrameInfo();
173  if (!MFI.isCalleeSavedInfoValid())
174  return;
175  /// Add all callee saved regs, then remove the ones that are saved+restored.
176  addCalleeSavedRegs(LiveRegs, MF);
177  /// Remove the ones that are not saved/restored; they are pristine.
178  for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
179  LiveRegs.removeReg(Info.getReg());
180 }
181 
183  if (!MBB.succ_empty()) {
184  // To get the live-outs we simply merge the live-ins of all successors.
185  for (const MachineBasicBlock *Succ : MBB.successors())
186  addBlockLiveIns(*Succ);
187  } else if (MBB.isReturnBlock()) {
188  // For the return block: Add all callee saved registers that are saved and
189  // restored (somewhere); This does not include callee saved registers that
190  // are unused and hence not saved and restored; they are called pristine.
191  const MachineFunction &MF = *MBB.getParent();
192  const MachineFrameInfo &MFI = MF.getFrameInfo();
193  if (MFI.isCalleeSavedInfoValid()) {
194  for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
195  addReg(Info.getReg());
196  }
197  }
198 }
199 
201  const MachineFunction &MF = *MBB.getParent();
202  if (!MBB.succ_empty()) {
203  addPristines(*this, MF);
205  } else if (MBB.isReturnBlock()) {
206  // For the return block: Add all callee saved registers.
207  const MachineFrameInfo &MFI = MF.getFrameInfo();
208  if (MFI.isCalleeSavedInfoValid())
209  addCalleeSavedRegs(*this, MF);
210  }
211 }
212 
214  const MachineFunction &MF = *MBB.getParent();
215  addPristines(*this, MF);
216  addBlockLiveIns(MBB);
217 }
218 
220  const MachineRegisterInfo &MRI,
221  MachineBasicBlock &MBB) {
222  const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
223  assert(MBB.livein_empty());
224  LiveRegs.init(TRI);
225  LiveRegs.addLiveOutsNoPristines(MBB);
226  for (MachineInstr &MI : make_range(MBB.rbegin(), MBB.rend()))
227  LiveRegs.stepBackward(MI);
228 
229  for (unsigned Reg : LiveRegs) {
230  if (MRI.isReserved(Reg))
231  continue;
232  // Skip the register if we are about to add one of its super registers.
233  bool ContainsSuperReg = false;
234  for (MCSuperRegIterator SReg(Reg, &TRI); SReg.isValid(); ++SReg) {
235  if (LiveRegs.contains(*SReg) && !MRI.isReserved(*SReg)) {
236  ContainsSuperReg = true;
237  break;
238  }
239  }
240  if (ContainsSuperReg)
241  continue;
242  MBB.addLiveIn(Reg);
243  }
244 }
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
iterator_range< livein_iterator > liveins() const
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:449
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
size_type count(const KeyT &Key) const
count - Returns 1 if this set contains an element identified by Key, 0 otherwise. ...
Definition: SparseSet.h:235
static void addCalleeSavedRegs(LivePhysRegs &LiveRegs, const MachineFunction &MF)
Adds all callee saved registers to LiveRegs.
LaneBitmask getSubRegIndexLaneMask(unsigned SubIdx) const
Return a bitmask representing the parts of a register that are covered by SubIdx. ...
iterator_range< succ_iterator > successors()
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
MCSuperRegIterator enumerates all super-registers of Reg.
const TargetRegisterInfo * getTargetRegisterInfo() const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
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...
constexpr bool any() const
Definition: LaneBitmask.h:53
void print(raw_ostream &OS) const
Prints the currently live registers to OS.
static void addPristines(LivePhysRegs &LiveRegs, const MachineFunction &MF)
Adds pristine registers to the given LiveRegs.
Printable PrintReg(unsigned Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubRegIdx=0)
Prints virtual and physical registers with or without a TRI instance.
void stepForward(const MachineInstr &MI, SmallVectorImpl< std::pair< unsigned, const MachineOperand * >> &Clobbers)
Simulates liveness when stepping forward over an instruction(bundle).
reverse_iterator rend()
reverse_iterator rbegin()
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
iterator erase(iterator I)
erase - Erases an existing element identified by a valid iterator.
Definition: SparseSet.h:285
const MCPhysReg * getCalleeSavedRegs() const
Returns list of callee saved registers.
void addLiveIn(MCPhysReg PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
void computeLiveIns(LivePhysRegs &LiveRegs, const MachineRegisterInfo &MRI, MachineBasicBlock &MBB)
Computes the live-in list for MBB assuming all of its successors live-in lists are up-to-date...
unsigned const MachineRegisterInfo * MRI
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Iterator that enumerates the sub-registers of a Reg and the associated sub-register indices...
bool isReserved(unsigned PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
void addLiveOuts(const MachineBasicBlock &MBB)
Adds all live-out registers of basic block MBB.
void addLiveIns(const MachineBasicBlock &MBB)
Adds all live-in registers of basic block MBB.
void init(const TargetRegisterInfo &TRI)
(re-)initializes and clears the set.
Definition: LivePhysRegs.h:66
MCRegAliasIterator enumerates all registers aliasing Reg.
void stepBackward(const MachineInstr &MI)
Simulates liveness when stepping backwards over an instruction(bundle).
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...
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
void addLiveOutsNoPristines(const MachineBasicBlock &MBB)
Adds all live-out registers of basic block MBB but skips pristine registers.
const_iterator begin() const
Definition: SparseSet.h:174
bool isReturnBlock() const
Convenience function that returns true if the block ends in a return instruction. ...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
ConstMIBundleOperands - Iterate over all operands in a const bundle of machine instructions.
#define E
Definition: LargeTest.cpp:27
bool empty() const
Returns true if the set is empty.
Definition: LivePhysRegs.h:76
MachineOperand class - Representation of each machine instruction operand.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
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.
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.
void removeReg(unsigned Reg)
Removes a physical register, all its sub-registers, and all its super-registers from the set...
Definition: LivePhysRegs.h:89
SparseSet - Fast set implmentation for objects that can be identified by small unsigned keys...
Definition: SparseSet.h:123
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
A set of physical registers with utility functions to track liveness when walking backward/forward th...
Definition: LivePhysRegs.h:49
#define I(x, y, z)
Definition: MD5.cpp:58
constexpr bool all() const
Definition: LaneBitmask.h:54
SparseSet< unsigned >::const_iterator const_iterator
Definition: LivePhysRegs.h:140
bool isValid() const
isValid - Returns true until all the operands have been visited.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void dump() const
Dumps the currently live registers to the debug output.
bool available(const MachineRegisterInfo &MRI, unsigned Reg) const
Returns true if register Reg and no aliasing register is in the set.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:81
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
IRTranslator LLVM IR MI
void removeRegsInMask(const MachineOperand &MO, SmallVectorImpl< std::pair< unsigned, const MachineOperand * >> *Clobbers=nullptr)
Removes physical registers clobbered by the regmask operand MO.
const_iterator end() const
Definition: LivePhysRegs.h:143
const_iterator begin() const
Definition: LivePhysRegs.h:142
void addReg(unsigned Reg)
Adds a physical register and all its sub-registers to the set.
Definition: LivePhysRegs.h:79