Coverage Report

Created: 2018-11-16 02:38

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/CodeGen/LivePhysRegs.h
Line
Count
Source (jump to first uncovered line)
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
/// \file
11
/// This file implements the LivePhysRegs utility for tracking liveness of
12
/// physical registers. This can be used for ad-hoc liveness tracking after
13
/// register allocation. You can start with the live-ins/live-outs at the
14
/// beginning/end of a block and update the information while walking the
15
/// instructions inside the block. This implementation tracks the liveness on a
16
/// sub-register granularity.
17
///
18
/// We assume that the high bits of a physical super-register are not preserved
19
/// unless the instruction has an implicit-use operand reading the super-
20
/// register.
21
///
22
/// X86 Example:
23
/// %ymm0 = ...
24
/// %xmm0 = ... (Kills %xmm0, all %xmm0s sub-registers, and %ymm0)
25
///
26
/// %ymm0 = ...
27
/// %xmm0 = ..., implicit %ymm0 (%ymm0 and all its sub-registers are alive)
28
//===----------------------------------------------------------------------===//
29
30
#ifndef LLVM_CODEGEN_LIVEPHYSREGS_H
31
#define LLVM_CODEGEN_LIVEPHYSREGS_H
32
33
#include "llvm/ADT/SparseSet.h"
34
#include "llvm/CodeGen/MachineBasicBlock.h"
35
#include "llvm/CodeGen/TargetRegisterInfo.h"
36
#include "llvm/MC/MCRegisterInfo.h"
37
#include <cassert>
38
#include <utility>
39
40
namespace llvm {
41
42
class MachineInstr;
43
class MachineOperand;
44
class MachineRegisterInfo;
45
class raw_ostream;
46
47
/// A set of physical registers with utility functions to track liveness
48
/// when walking backward/forward through a basic block.
49
class LivePhysRegs {
50
  const TargetRegisterInfo *TRI = nullptr;
51
  using RegisterSet = SparseSet<MCPhysReg, identity<MCPhysReg>>;
52
  RegisterSet LiveRegs;
53
54
public:
55
  /// Constructs an unitialized set. init() needs to be called to initialize it.
56
1.24M
  LivePhysRegs() = default;
57
58
  /// Constructs and initializes an empty set.
59
122k
  LivePhysRegs(const TargetRegisterInfo &TRI) : TRI(&TRI) {
60
122k
    LiveRegs.setUniverse(TRI.getNumRegs());
61
122k
  }
62
63
  LivePhysRegs(const LivePhysRegs&) = delete;
64
  LivePhysRegs &operator=(const LivePhysRegs&) = delete;
65
66
  /// (re-)initializes and clears the set.
67
579k
  void init(const TargetRegisterInfo &TRI) {
68
579k
    this->TRI = &TRI;
69
579k
    LiveRegs.clear();
70
579k
    LiveRegs.setUniverse(TRI.getNumRegs());
71
579k
  }
72
73
  /// Clears the set.
74
713k
  void clear() { LiveRegs.clear(); }
75
76
  /// Returns true if the set is empty.
77
865k
  bool empty() const { return LiveRegs.empty(); }
78
79
  /// Adds a physical register and all its sub-registers to the set.
80
22.8M
  void addReg(MCPhysReg Reg) {
81
22.8M
    assert(TRI && "LivePhysRegs is not initialized.");
82
22.8M
    assert(Reg <= TRI->getNumRegs() && "Expected a physical register.");
83
22.8M
    for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
84
94.6M
         SubRegs.isValid(); 
++SubRegs71.7M
)
85
71.7M
      LiveRegs.insert(*SubRegs);
86
22.8M
  }
87
88
  /// Removes a physical register, all its sub-registers, and all its
89
  /// super-registers from the set.
90
9.56M
  void removeReg(MCPhysReg Reg) {
91
9.56M
    assert(TRI && "LivePhysRegs is not initialized.");
92
9.56M
    assert(Reg <= TRI->getNumRegs() && "Expected a physical register.");
93
77.6M
    for (MCRegAliasIterator R(Reg, TRI, true); R.isValid(); 
++R68.0M
)
94
68.0M
      LiveRegs.erase(*R);
95
9.56M
  }
96
97
  /// Removes physical registers clobbered by the regmask operand \p MO.
98
  void removeRegsInMask(const MachineOperand &MO,
99
        SmallVectorImpl<std::pair<MCPhysReg, const MachineOperand*>> *Clobbers =
100
        nullptr);
101
102
  /// Returns true if register \p Reg is contained in the set. This also
103
  /// works if only the super register of \p Reg has been defined, because
104
  /// addReg() always adds all sub-registers to the set as well.
105
  /// Note: Returns false if just some sub registers are live, use available()
106
  /// when searching a free register.
107
5.03M
  bool contains(MCPhysReg Reg) const { return LiveRegs.count(Reg); }
108
109
  /// Returns true if register \p Reg and no aliasing register is in the set.
110
  bool available(const MachineRegisterInfo &MRI, MCPhysReg Reg) const;
111
112
  /// Remove defined registers and regmask kills from the set.
113
  void removeDefs(const MachineInstr &MI);
114
115
  /// Add uses to the set.
116
  void addUses(const MachineInstr &MI);
117
118
  /// Simulates liveness when stepping backwards over an instruction(bundle).
119
  /// Remove Defs, add uses. This is the recommended way of calculating
120
  /// liveness.
121
  void stepBackward(const MachineInstr &MI);
122
123
  /// Simulates liveness when stepping forward over an instruction(bundle).
124
  /// Remove killed-uses, add defs. This is the not recommended way, because it
125
  /// depends on accurate kill flags. If possible use stepBackward() instead of
126
  /// this function. The clobbers set will be the list of registers either
127
  /// defined or clobbered by a regmask.  The operand will identify whether this
128
  /// is a regmask or register operand.
129
  void stepForward(const MachineInstr &MI,
130
        SmallVectorImpl<std::pair<MCPhysReg, const MachineOperand*>> &Clobbers);
131
132
  /// Adds all live-in registers of basic block \p MBB.
133
  /// Live in registers are the registers in the blocks live-in list and the
134
  /// pristine registers.
135
  void addLiveIns(const MachineBasicBlock &MBB);
136
137
  /// Adds all live-out registers of basic block \p MBB.
138
  /// Live out registers are the union of the live-in registers of the successor
139
  /// blocks and pristine registers. Live out registers of the end block are the
140
  /// callee saved registers.
141
  void addLiveOuts(const MachineBasicBlock &MBB);
142
143
  /// Adds all live-out registers of basic block \p MBB but skips pristine
144
  /// registers.
145
  void addLiveOutsNoPristines(const MachineBasicBlock &MBB);
146
147
  using const_iterator = RegisterSet::const_iterator;
148
149
330k
  const_iterator begin() const { return LiveRegs.begin(); }
150
330k
  const_iterator end() const { return LiveRegs.end(); }
151
152
  /// Prints the currently live registers to \p OS.
153
  void print(raw_ostream &OS) const;
154
155
  /// Dumps the currently live registers to the debug output.
156
  void dump() const;
157
158
private:
159
  /// Adds live-in registers from basic block \p MBB, taking associated
160
  /// lane masks into consideration.
161
  void addBlockLiveIns(const MachineBasicBlock &MBB);
162
163
  /// Adds pristine registers. Pristine registers are callee saved registers
164
  /// that are unused in the function.
165
  void addPristines(const MachineFunction &MF);
166
};
167
168
0
inline raw_ostream &operator<<(raw_ostream &OS, const LivePhysRegs& LR) {
169
0
  LR.print(OS);
170
0
  return OS;
171
0
}
172
173
/// Computes registers live-in to \p MBB assuming all of its successors
174
/// live-in lists are up-to-date. Puts the result into the given LivePhysReg
175
/// instance \p LiveRegs.
176
void computeLiveIns(LivePhysRegs &LiveRegs, const MachineBasicBlock &MBB);
177
178
/// Recomputes dead and kill flags in \p MBB.
179
void recomputeLivenessFlags(MachineBasicBlock &MBB);
180
181
/// Adds registers contained in \p LiveRegs to the block live-in list of \p MBB.
182
/// Does not add reserved registers.
183
void addLiveIns(MachineBasicBlock &MBB, const LivePhysRegs &LiveRegs);
184
185
/// Convenience function combining computeLiveIns() and addLiveIns().
186
void computeAndAddLiveIns(LivePhysRegs &LiveRegs,
187
                          MachineBasicBlock &MBB);
188
189
/// Convenience function for recomputing live-in's for \p MBB.
190
18.5k
static inline void recomputeLiveIns(MachineBasicBlock &MBB) {
191
18.5k
  LivePhysRegs LPR;
192
18.5k
  MBB.clearLiveIns();
193
18.5k
  computeAndAddLiveIns(LPR, MBB);
194
18.5k
}
195
196
} // end namespace llvm
197
198
#endif // LLVM_CODEGEN_LIVEPHYSREGS_H