LLVM  4.0.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"
34 #include "llvm/MC/MCRegisterInfo.h"
36 #include <cassert>
37 #include <utility>
38 
39 namespace llvm {
40 
41 class MachineInstr;
42 
43 /// \brief A set of live physical registers with functions to track liveness
44 /// when walking backward/forward through a basic block.
45 class LivePhysRegs {
46  const TargetRegisterInfo *TRI = nullptr;
47  SparseSet<unsigned> LiveRegs;
48 
49  LivePhysRegs(const LivePhysRegs&) = delete;
50  LivePhysRegs &operator=(const LivePhysRegs&) = delete;
51 
52 public:
53  /// \brief Constructs a new empty LivePhysRegs set.
54  LivePhysRegs() = default;
55 
56  /// \brief Constructs and initialize an empty LivePhysRegs set.
57  LivePhysRegs(const TargetRegisterInfo *TRI) : TRI(TRI) {
58  assert(TRI && "Invalid TargetRegisterInfo pointer.");
59  LiveRegs.setUniverse(TRI->getNumRegs());
60  }
61 
62  /// \brief Clear and initialize the LivePhysRegs set.
63  void init(const TargetRegisterInfo &TRI) {
64  this->TRI = &TRI;
65  LiveRegs.clear();
66  LiveRegs.setUniverse(TRI.getNumRegs());
67  }
68 
69  /// \brief Clears the LivePhysRegs set.
70  void clear() { LiveRegs.clear(); }
71 
72  /// \brief Returns true if the set is empty.
73  bool empty() const { return LiveRegs.empty(); }
74 
75  /// \brief Adds a physical register and all its sub-registers to the set.
76  void addReg(unsigned Reg) {
77  assert(TRI && "LivePhysRegs is not initialized.");
78  assert(Reg <= TRI->getNumRegs() && "Expected a physical register.");
79  for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
80  SubRegs.isValid(); ++SubRegs)
81  LiveRegs.insert(*SubRegs);
82  }
83 
84  /// \brief Removes a physical register, all its sub-registers, and all its
85  /// super-registers from the set.
86  void removeReg(unsigned Reg) {
87  assert(TRI && "LivePhysRegs is not initialized.");
88  assert(Reg <= TRI->getNumRegs() && "Expected a physical register.");
89  for (MCRegAliasIterator R(Reg, TRI, true); R.isValid(); ++R)
90  LiveRegs.erase(*R);
91  }
92 
93  /// \brief Removes physical registers clobbered by the regmask operand @p MO.
94  void removeRegsInMask(const MachineOperand &MO,
95  SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> *Clobbers);
96 
97  /// \brief Returns true if register @p Reg is contained in the set. This also
98  /// works if only the super register of @p Reg has been defined, because
99  /// addReg() always adds all sub-registers to the set as well.
100  /// Note: Returns false if just some sub registers are live, use available()
101  /// when searching a free register.
102  bool contains(unsigned Reg) const { return LiveRegs.count(Reg); }
103 
104  /// Returns true if register \p Reg and no aliasing register is in the set.
105  bool available(const MachineRegisterInfo &MRI, unsigned Reg) const;
106 
107  /// \brief Simulates liveness when stepping backwards over an
108  /// instruction(bundle): Remove Defs, add uses. This is the recommended way of
109  /// calculating liveness.
110  void stepBackward(const MachineInstr &MI);
111 
112  /// \brief Simulates liveness when stepping forward over an
113  /// instruction(bundle): Remove killed-uses, add defs. This is the not
114  /// recommended way, because it depends on accurate kill flags. If possible
115  /// use stepBackward() instead of this function.
116  /// The clobbers set will be the list of registers either defined or clobbered
117  /// by a regmask. The operand will identify whether this is a regmask or
118  /// register operand.
119  void stepForward(const MachineInstr &MI,
120  SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> &Clobbers);
121 
122  /// Adds all live-in registers of basic block @p MBB.
123  /// Live in registers are the registers in the blocks live-in list and the
124  /// pristine registers.
125  void addLiveIns(const MachineBasicBlock &MBB);
126 
127  /// Adds all live-out registers of basic block @p MBB.
128  /// Live out registers are the union of the live-in registers of the successor
129  /// blocks and pristine registers. Live out registers of the end block are the
130  /// callee saved registers.
131  void addLiveOuts(const MachineBasicBlock &MBB);
132 
133  /// Like addLiveOuts() but does not add pristine registers/callee saved
134  /// registers.
136 
138  const_iterator begin() const { return LiveRegs.begin(); }
139  const_iterator end() const { return LiveRegs.end(); }
140 
141  /// \brief Prints the currently live registers to @p OS.
142  void print(raw_ostream &OS) const;
143 
144  /// \brief Dumps the currently live registers to the debug output.
145  void dump() const;
146 
147 private:
148  /// Adds live-in registers from basic block @p MBB, taking associated
149  /// lane masks into consideration.
150  void addBlockLiveIns(const MachineBasicBlock &MBB);
151 };
152 
154  LR.print(OS);
155  return OS;
156 }
157 
158 /// Compute the live-in list for \p MBB assuming all of its successors live-in
159 /// lists are up-to-date. Uses the given LivePhysReg instance \p LiveRegs; This
160 /// is just here to avoid repeated heap allocations when calling this multiple
161 /// times in a pass.
162 void computeLiveIns(LivePhysRegs &LiveRegs, const TargetRegisterInfo &TRI,
163  MachineBasicBlock &MBB);
164 
165 } // end namespace llvm
166 
167 #endif // LLVM_CODEGEN_LIVEPHYSREGS_H
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 computeLiveIns(LivePhysRegs &LiveRegs, const TargetRegisterInfo &TRI, MachineBasicBlock &MBB)
Compute the live-in list for MBB assuming all of its successors live-in lists are up-to-date...
LivePhysRegs()=default
Constructs a new empty LivePhysRegs set.
SparseSet< unsigned >::const_iterator const_iterator
Definition: LivePhysRegs.h:137
bool empty() const
empty - Returns true if the set is empty.
Definition: SparseSet.h:183
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:32
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...
MachineBasicBlock * MBB
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:57
iterator erase(iterator I)
erase - Erases an existing element identified by a valid iterator.
Definition: SparseSet.h:285
unsigned const MachineRegisterInfo * MRI
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)
Clear and initialize the LivePhysRegs set.
Definition: LivePhysRegs.h:63
MCRegAliasIterator enumerates all registers aliasing Reg.
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.
void addLiveOutsNoPristines(const MachineBasicBlock &MBB)
Like addLiveOuts() but does not add pristine registers/callee saved registers.
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.
bool empty() const
Returns true if the set is empty.
Definition: LivePhysRegs.h:73
MachineOperand class - Representation of each machine instruction operand.
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:52
void removeReg(unsigned Reg)
Removes a physical register, all its sub-registers, and all its super-registers from the set...
Definition: LivePhysRegs.h:86
A set of live physical registers with functions to track liveness when walking backward/forward throu...
Definition: LivePhysRegs.h:45
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:1726
void clear()
clear - Clears the set.
Definition: SparseSet.h:194
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void clear()
Clears the LivePhysRegs set.
Definition: LivePhysRegs.h:70
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.
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
const_iterator end() const
Definition: LivePhysRegs.h:139
const_iterator begin() const
Definition: LivePhysRegs.h:138
void addReg(unsigned Reg)
Adds a physical register and all its sub-registers to the set.
Definition: LivePhysRegs.h:76