LLVM  3.7.0
LivePhysRegs.h
Go to the documentation of this file.
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 the LivePhysRegs utility for tracking liveness of
11 // physical registers. This can be used for ad-hoc liveness tracking after
12 // register allocation. You can start with the live-ins/live-outs at the
13 // beginning/end of a block and update the information while walking the
14 // instructions inside the block. This implementation tracks the liveness on a
15 // sub-register granularity.
16 //
17 // We assume that the high bits of a physical super-register are not preserved
18 // unless the instruction has an implicit-use operand reading the super-
19 // register.
20 //
21 // X86 Example:
22 // %YMM0<def> = ...
23 // %XMM0<def> = ... (Kills %XMM0, all %XMM0s sub-registers, and %YMM0)
24 //
25 // %YMM0<def> = ...
26 // %XMM0<def> = ..., %YMM0<imp-use> (%YMM0 and all its sub-registers are alive)
27 //===----------------------------------------------------------------------===//
28 
29 #ifndef LLVM_CODEGEN_LIVEPHYSREGS_H
30 #define LLVM_CODEGEN_LIVEPHYSREGS_H
31 
32 #include "llvm/ADT/SparseSet.h"
35 #include <cassert>
36 
37 namespace llvm {
38 
39 class MachineInstr;
40 
41 /// \brief A set of live physical registers with functions to track liveness
42 /// when walking backward/forward through a basic block.
43 class LivePhysRegs {
44  const TargetRegisterInfo *TRI;
45  SparseSet<unsigned> LiveRegs;
46 
47  LivePhysRegs(const LivePhysRegs&) = delete;
48  LivePhysRegs &operator=(const LivePhysRegs&) = delete;
49 public:
50  /// \brief Constructs a new empty LivePhysRegs set.
51  LivePhysRegs() : TRI(nullptr), LiveRegs() {}
52 
53  /// \brief Constructs and initialize an empty LivePhysRegs set.
54  LivePhysRegs(const TargetRegisterInfo *TRI) : TRI(TRI) {
55  assert(TRI && "Invalid TargetRegisterInfo pointer.");
56  LiveRegs.setUniverse(TRI->getNumRegs());
57  }
58 
59  /// \brief Clear and initialize the LivePhysRegs set.
60  void init(const TargetRegisterInfo *TRI) {
61  assert(TRI && "Invalid TargetRegisterInfo pointer.");
62  this->TRI = TRI;
63  LiveRegs.clear();
64  LiveRegs.setUniverse(TRI->getNumRegs());
65  }
66 
67  /// \brief Clears the LivePhysRegs set.
68  void clear() { LiveRegs.clear(); }
69 
70  /// \brief Returns true if the set is empty.
71  bool empty() const { return LiveRegs.empty(); }
72 
73  /// \brief Adds a physical register and all its sub-registers to the set.
74  void addReg(unsigned Reg) {
75  assert(TRI && "LivePhysRegs is not initialized.");
76  assert(Reg <= TRI->getNumRegs() && "Expected a physical register.");
77  for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
78  SubRegs.isValid(); ++SubRegs)
79  LiveRegs.insert(*SubRegs);
80  }
81 
82  /// \brief Removes a physical register, all its sub-registers, and all its
83  /// super-registers from the set.
84  void removeReg(unsigned Reg) {
85  assert(TRI && "LivePhysRegs is not initialized.");
86  assert(Reg <= TRI->getNumRegs() && "Expected a physical register.");
87  for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
88  SubRegs.isValid(); ++SubRegs)
89  LiveRegs.erase(*SubRegs);
90  for (MCSuperRegIterator SuperRegs(Reg, TRI, /*IncludeSelf=*/false);
91  SuperRegs.isValid(); ++SuperRegs)
92  LiveRegs.erase(*SuperRegs);
93  }
94 
95  /// \brief Removes physical registers clobbered by the regmask operand @p MO.
96  void removeRegsInMask(const MachineOperand &MO,
97  SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> *Clobbers);
98 
99  /// \brief Returns true if register @p Reg is contained in the set. This also
100  /// works if only the super register of @p Reg has been defined, because we
101  /// always add also all sub-registers to the set.
102  bool contains(unsigned Reg) const { return LiveRegs.count(Reg); }
103 
104  /// \brief Simulates liveness when stepping backwards over an
105  /// instruction(bundle): Remove Defs, add uses. This is the recommended way of
106  /// calculating liveness.
107  void stepBackward(const MachineInstr &MI);
108 
109  /// \brief Simulates liveness when stepping forward over an
110  /// instruction(bundle): Remove killed-uses, add defs. This is the not
111  /// recommended way, because it depends on accurate kill flags. If possible
112  /// use stepBackwards() instead of this function.
113  /// The clobbers set will be the list of registers either defined or clobbered
114  /// by a regmask. The operand will identify whether this is a regmask or
115  /// register operand.
116  void stepForward(const MachineInstr &MI,
117  SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> &Clobbers);
118 
119  /// \brief Adds all live-in registers of basic block @p MBB; After prologue/
120  /// epilogue insertion \p AddPristines should be set to true to insert the
121  /// pristine registers.
122  void addLiveIns(const MachineBasicBlock *MBB, bool AddPristines = false);
123 
124  /// \brief Adds all live-out registers of basic block @p MBB; After prologue/
125  /// epilogue insertion \p AddPristines should be set to true to insert the
126  /// pristine registers.
127  void addLiveOuts(const MachineBasicBlock *MBB, bool AddPristines = false);
128 
130  const_iterator begin() const { return LiveRegs.begin(); }
131  const_iterator end() const { return LiveRegs.end(); }
132 
133  /// \brief Prints the currently live registers to @p OS.
134  void print(raw_ostream &OS) const;
135 
136  /// \brief Dumps the currently live registers to the debug output.
137  void dump() const;
138 };
139 
141  LR.print(OS);
142  return OS;
143 }
144 
145 } // namespace llvm
146 
147 #endif
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
std::pair< iterator, bool > insert(const ValueT &Val)
insert - Attempts to insert a new element.
Definition: SparseSet.h:249
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
void init(const TargetRegisterInfo *TRI)
Clear and initialize the LivePhysRegs set.
Definition: LivePhysRegs.h:60
SparseSet< unsigned >::const_iterator const_iterator
Definition: LivePhysRegs.h:129
bool empty() const
empty - Returns true if the set is empty.
Definition: SparseSet.h:183
MCSuperRegIterator enumerates all super-registers of Reg.
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.
void print(raw_ostream &OS) const
Prints the currently live registers to OS.
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
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.
LivePhysRegs(const TargetRegisterInfo *TRI)
Constructs and initialize an empty LivePhysRegs set.
Definition: LivePhysRegs.h:54
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...
void stepBackward(const MachineInstr &MI)
Simulates liveness when stepping backwards over an instruction(bundle): Remove Defs, add uses.
bool contains(unsigned Reg) const
Returns true if register Reg is contained in the set.
Definition: LivePhysRegs.h:102
void setUniverse(unsigned U)
setUniverse - Set the universe size which determines the largest key the set can hold.
Definition: SparseSet.h:155
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
MCSubRegIterator enumerates all sub-registers of Reg.
const_iterator begin() const
Definition: SparseSet.h:174
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...
bool empty() const
Returns true if the set is empty.
Definition: LivePhysRegs.h:71
MachineOperand class - Representation of each machine instruction operand.
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
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.
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:1738
void clear()
clear - Clears the set.
Definition: SparseSet.h:194
void clear()
Clears the LivePhysRegs set.
Definition: LivePhysRegs.h:68
LivePhysRegs()
Constructs a new empty LivePhysRegs set.
Definition: LivePhysRegs.h:51
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