1 //===- llvm/CodeGen/LivePhysRegs.h - Live Physical Register Set -*- C++ -*-===//
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 a set of live physical registers. This can be used for
11 // ad hoc liveness tracking after register allocation. You can start with the
12 // live-ins/live-outs at the beginning/end of a block and update the information
13 // while walking the instructions inside the block.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_CODEGEN_LIVE_PHYS_REGS_H
18 #define LLVM_CODEGEN_LIVE_PHYS_REGS_H
20 #include "llvm/ADT/SparseSet.h"
21 #include "llvm/CodeGen/MachineBasicBlock.h"
22 #include "llvm/Target/TargetRegisterInfo.h"
29 /// \brief A set of live physical registers with functions to track liveness
30 /// when walking backward/forward through a basic block.
32 const TargetRegisterInfo *TRI;
33 SparseSet<unsigned> LiveRegs;
35 LivePhysRegs(const LivePhysRegs&) LLVM_DELETED_FUNCTION;
36 LivePhysRegs &operator=(const LivePhysRegs&) LLVM_DELETED_FUNCTION;
38 /// \brief Constructs a new empty LivePhysRegs set.
39 LivePhysRegs() : TRI(0), LiveRegs() {}
41 /// \brief Constructs and initialize an empty LivePhysRegs set.
42 LivePhysRegs(const TargetRegisterInfo *TRI) : TRI(TRI) {
43 LiveRegs.setUniverse(TRI->getNumRegs());
46 /// \brief Clear and initialize the LivePhysRegs set.
47 void init(const TargetRegisterInfo *_TRI) {
50 LiveRegs.setUniverse(TRI->getNumRegs());
53 /// \brief Clears the LivePhysRegs set.
54 void clear() { LiveRegs.clear(); }
56 /// \brief Returns true if the set is empty.
57 bool empty() const { return LiveRegs.empty(); }
59 /// \brief Adds a physical register and all its sub-registers to the set.
60 void addReg(unsigned Reg) {
61 assert(TRI && "LivePhysRegs is not initialized.");
62 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
63 SubRegs.isValid(); ++SubRegs)
64 LiveRegs.insert(*SubRegs);
67 /// \brief Removes a physical register, all its sub-registers, and all its
68 /// super-registers from the set.
69 void removeReg(unsigned Reg) {
70 assert(TRI && "LivePhysRegs is not initialized.");
71 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
72 SubRegs.isValid(); ++SubRegs)
73 LiveRegs.erase(*SubRegs);
74 for (MCSuperRegIterator SuperRegs(Reg, TRI, /*IncludeSelf=*/false);
75 SuperRegs.isValid(); ++SuperRegs)
76 LiveRegs.erase(*SuperRegs);
79 /// \brief Removes physical registers clobbered by the regmask operand @p MO.
80 void removeRegsInMask(const MachineOperand &MO);
82 /// \brief Returns true if register @p Reg is contained in the set. This also
83 /// works if only the super register of @p Reg has been defined, because we
84 /// always add also all sub-registers to the set.
85 bool contains(unsigned Reg) const { return LiveRegs.count(Reg); }
87 /// \brief Simulates liveness when stepping backwards over an
88 /// instruction(bundle): Remove Defs, add uses. This is the recommended way of
89 /// calculating liveness.
90 void stepBackward(const MachineInstr &MI);
92 /// \brief Simulates liveness when stepping forward over an
93 /// instruction(bundle): Remove killed-uses, add defs. This is the not
94 /// recommended way, because it depends on accurate kill flags. If possible
95 /// use stepBackwards() instead of this function.
96 void stepForward(const MachineInstr &MI);
98 /// \brief Adds all live-in registers of basic block @p MBB.
99 void addLiveIns(const MachineBasicBlock *MBB) {
100 for (MachineBasicBlock::livein_iterator LI = MBB->livein_begin(),
101 LE = MBB->livein_end(); LI != LE; ++LI)
105 /// \brief Adds all live-out registers of basic block @p MBB.
106 void addLiveOuts(const MachineBasicBlock *MBB) {
107 for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(),
108 SE = MBB->succ_end(); SI != SE; ++SI)
112 typedef SparseSet<unsigned>::const_iterator const_iterator;
113 const_iterator begin() const { return LiveRegs.begin(); }
114 const_iterator end() const { return LiveRegs.end(); }