1 //===--- LivePhysRegs.cpp - Live Physical Register Set --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/CodeGen/LivePhysRegs.h"
17 #include "llvm/CodeGen/MachineInstrBundle.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
23 /// \brief Remove all registers from the set that get clobbered by the register
25 /// The clobbers set will be the list of live registers clobbered
27 void LivePhysRegs::removeRegsInMask(const MachineOperand &MO,
28 SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> *Clobbers) {
29 SparseSet<unsigned>::iterator LRI = LiveRegs.begin();
30 while (LRI != LiveRegs.end()) {
31 if (MO.clobbersPhysReg(*LRI)) {
33 Clobbers->push_back(std::make_pair(*LRI, &MO));
34 LRI = LiveRegs.erase(LRI);
40 /// Simulates liveness when stepping backwards over an instruction(bundle):
41 /// Remove Defs, add uses. This is the recommended way of calculating liveness.
42 void LivePhysRegs::stepBackward(const MachineInstr &MI) {
43 // Remove defined registers and regmask kills from the set.
44 for (ConstMIBundleOperands O(&MI); O.isValid(); ++O) {
48 unsigned Reg = O->getReg();
52 } else if (O->isRegMask())
53 removeRegsInMask(*O, nullptr);
56 // Add uses to the set.
57 for (ConstMIBundleOperands O(&MI); O.isValid(); ++O) {
58 if (!O->isReg() || !O->readsReg() || O->isUndef())
60 unsigned Reg = O->getReg();
67 /// Simulates liveness when stepping forward over an instruction(bundle): Remove
68 /// killed-uses, add defs. This is the not recommended way, because it depends
69 /// on accurate kill flags. If possible use stepBackwards() instead of this
71 void LivePhysRegs::stepForward(const MachineInstr &MI,
72 SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> &Clobbers) {
73 // Remove killed registers from the set.
74 for (ConstMIBundleOperands O(&MI); O.isValid(); ++O) {
76 unsigned Reg = O->getReg();
80 // Note, dead defs are still recorded. The caller should decide how to
82 Clobbers.push_back(std::make_pair(Reg, &*O));
89 } else if (O->isRegMask())
90 removeRegsInMask(*O, &Clobbers);
93 // Add defs to the set.
94 for (auto Reg : Clobbers) {
95 // Skip dead defs. They shouldn't be added to the set.
96 if (Reg.second->isReg() && Reg.second->isDead())
102 /// Prin the currently live registers to OS.
103 void LivePhysRegs::print(raw_ostream &OS) const {
104 OS << "Live Registers:";
106 OS << " (uninitialized)\n";
115 for (const_iterator I = begin(), E = end(); I != E; ++I)
116 OS << " " << PrintReg(*I, TRI);
120 /// Dumps the currently live registers to the debug output.
121 void LivePhysRegs::dump() const {
122 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
123 dbgs() << " " << *this;