Convert liveness tracking to work on a sub-register level instead of just register...
[oota-llvm.git] / include / llvm / CodeGen / LivePhysRegs.h
1 //===- llvm/CodeGen/LivePhysRegs.h - Live Physical Register Set -*- C++ -*-===//
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 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.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_CODEGEN_LIVE_PHYS_REGS_H
18 #define LLVM_CODEGEN_LIVE_PHYS_REGS_H
19
20 #include "llvm/ADT/SparseSet.h"
21 #include "llvm/CodeGen/MachineBasicBlock.h"
22 #include "llvm/Target/TargetRegisterInfo.h"
23 #include <cassert>
24
25 namespace llvm {
26
27 class MachineInstr;
28
29 /// \brief A set of live physical registers with functions to track liveness
30 /// when walking backward/forward through a basic block.
31 class LivePhysRegs {
32   const TargetRegisterInfo *TRI;
33   SparseSet<unsigned> LiveRegs;
34
35   LivePhysRegs(const LivePhysRegs&) LLVM_DELETED_FUNCTION;
36   LivePhysRegs &operator=(const LivePhysRegs&) LLVM_DELETED_FUNCTION;
37 public:
38   /// \brief Constructs a new empty LivePhysRegs set.
39   LivePhysRegs() : TRI(0), LiveRegs() {}
40
41   /// \brief Constructs and initialize an empty LivePhysRegs set.
42   LivePhysRegs(const TargetRegisterInfo *TRI) : TRI(TRI) {
43     LiveRegs.setUniverse(TRI->getNumRegs());
44   }
45
46   /// \brief Clear and initialize the LivePhysRegs set.
47   void init(const TargetRegisterInfo *_TRI) {
48     TRI = _TRI;
49     LiveRegs.clear();
50     LiveRegs.setUniverse(TRI->getNumRegs());
51   }
52
53   /// \brief Clears the LivePhysRegs set.
54   void clear() { LiveRegs.clear(); }
55
56   /// \brief Returns true if the set is empty.
57   bool empty() const { return LiveRegs.empty(); }
58
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);
65   }
66
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);
77   }
78
79   /// \brief Removes physical registers clobbered by the regmask operand @p MO.
80   void removeRegsInMask(const MachineOperand &MO);
81
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); }
86
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);
91
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);
97
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)
102       addReg(*LI);
103   }
104
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)
109       addLiveIns(*SI);
110   }
111
112   typedef SparseSet<unsigned>::const_iterator const_iterator;
113   const_iterator begin() const { return LiveRegs.begin(); }
114   const_iterator end() const { return LiveRegs.end(); }
115 };
116
117 } // namespace llvm
118
119 #endif