Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/CodeGen/LiveRangeCalc.h
Line
Count
Source
1
//===- LiveRangeCalc.h - Calculate live ranges ------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// The LiveRangeCalc class can be used to compute live ranges from scratch.  It
10
// caches information about values in the CFG to speed up repeated operations
11
// on the same live range.  The cache can be shared by non-overlapping live
12
// ranges.  SplitKit uses that when computing the live range of split products.
13
//
14
// A low-level interface is available to clients that know where a variable is
15
// live, but don't know which value it has as every point.  LiveRangeCalc will
16
// propagate values down the dominator tree, and even insert PHI-defs where
17
// needed.  SplitKit uses this faster interface when possible.
18
//
19
//===----------------------------------------------------------------------===//
20
21
#ifndef LLVM_LIB_CODEGEN_LIVERANGECALC_H
22
#define LLVM_LIB_CODEGEN_LIVERANGECALC_H
23
24
#include "llvm/ADT/ArrayRef.h"
25
#include "llvm/ADT/BitVector.h"
26
#include "llvm/ADT/DenseMap.h"
27
#include "llvm/ADT/IndexedMap.h"
28
#include "llvm/ADT/SmallVector.h"
29
#include "llvm/CodeGen/LiveInterval.h"
30
#include "llvm/CodeGen/MachineBasicBlock.h"
31
#include "llvm/CodeGen/SlotIndexes.h"
32
#include "llvm/MC/LaneBitmask.h"
33
#include <utility>
34
35
namespace llvm {
36
37
template <class NodeT> class DomTreeNodeBase;
38
class MachineDominatorTree;
39
class MachineFunction;
40
class MachineRegisterInfo;
41
42
using MachineDomTreeNode = DomTreeNodeBase<MachineBasicBlock>;
43
44
class LiveRangeCalc {
45
  const MachineFunction *MF = nullptr;
46
  const MachineRegisterInfo *MRI = nullptr;
47
  SlotIndexes *Indexes = nullptr;
48
  MachineDominatorTree *DomTree = nullptr;
49
  VNInfo::Allocator *Alloc = nullptr;
50
51
  /// LiveOutPair - A value and the block that defined it.  The domtree node is
52
  /// redundant, it can be computed as: MDT[Indexes.getMBBFromIndex(VNI->def)].
53
  using LiveOutPair = std::pair<VNInfo *, MachineDomTreeNode *>;
54
55
  /// LiveOutMap - Map basic blocks to the value leaving the block.
56
  using LiveOutMap = IndexedMap<LiveOutPair, MBB2NumberFunctor>;
57
58
  /// Bit vector of active entries in LiveOut, also used as a visited set by
59
  /// findReachingDefs.  One entry per basic block, indexed by block number.
60
  /// This is kept as a separate bit vector because it can be cleared quickly
61
  /// when switching live ranges.
62
  BitVector Seen;
63
64
  /// Map LiveRange to sets of blocks (represented by bit vectors) that
65
  /// in the live range are defined on entry and undefined on entry.
66
  /// A block is defined on entry if there is a path from at least one of
67
  /// the defs in the live range to the entry of the block, and conversely,
68
  /// a block is undefined on entry, if there is no such path (i.e. no
69
  /// definition reaches the entry of the block). A single LiveRangeCalc
70
  /// object is used to track live-out information for multiple registers
71
  /// in live range splitting (which is ok, since the live ranges of these
72
  /// registers do not overlap), but the defined/undefined information must
73
  /// be kept separate for each individual range.
74
  /// By convention, EntryInfoMap[&LR] = { Defined, Undefined }.
75
  using EntryInfoMap = DenseMap<LiveRange *, std::pair<BitVector, BitVector>>;
76
  EntryInfoMap EntryInfos;
77
78
  /// Map each basic block where a live range is live out to the live-out value
79
  /// and its defining block.
80
  ///
81
  /// For every basic block, MBB, one of these conditions shall be true:
82
  ///
83
  ///  1. !Seen.count(MBB->getNumber())
84
  ///     Blocks without a Seen bit are ignored.
85
  ///  2. LiveOut[MBB].second.getNode() == MBB
86
  ///     The live-out value is defined in MBB.
87
  ///  3. forall P in preds(MBB): LiveOut[P] == LiveOut[MBB]
88
  ///     The live-out value passses through MBB. All predecessors must carry
89
  ///     the same value.
90
  ///
91
  /// The domtree node may be null, it can be computed.
92
  ///
93
  /// The map can be shared by multiple live ranges as long as no two are
94
  /// live-out of the same block.
95
  LiveOutMap Map;
96
97
  /// LiveInBlock - Information about a basic block where a live range is known
98
  /// to be live-in, but the value has not yet been determined.
99
  struct LiveInBlock {
100
    // The live range set that is live-in to this block.  The algorithms can
101
    // handle multiple non-overlapping live ranges simultaneously.
102
    LiveRange &LR;
103
104
    // DomNode - Dominator tree node for the block.
105
    // Cleared when the final value has been determined and LI has been updated.
106
    MachineDomTreeNode *DomNode;
107
108
    // Position in block where the live-in range ends, or SlotIndex() if the
109
    // range passes through the block.  When the final value has been
110
    // determined, the range from the block start to Kill will be added to LI.
111
    SlotIndex Kill;
112
113
    // Live-in value filled in by updateSSA once it is known.
114
    VNInfo *Value = nullptr;
115
116
    LiveInBlock(LiveRange &LR, MachineDomTreeNode *node, SlotIndex kill)
117
2.46M
      : LR(LR), DomNode(node), Kill(kill) {}
118
  };
119
120
  /// LiveIn - Work list of blocks where the live-in value has yet to be
121
  /// determined.  This list is typically computed by findReachingDefs() and
122
  /// used as a work list by updateSSA().  The low-level interface may also be
123
  /// used to add entries directly.
124
  SmallVector<LiveInBlock, 16> LiveIn;
125
126
  /// Check if the entry to block @p MBB can be reached by any of the defs
127
  /// in @p LR. Return true if none of the defs reach the entry to @p MBB.
128
  bool isDefOnEntry(LiveRange &LR, ArrayRef<SlotIndex> Undefs,
129
                    MachineBasicBlock &MBB, BitVector &DefOnEntry,
130
                    BitVector &UndefOnEntry);
131
132
  /// Find the set of defs that can reach @p Kill. @p Kill must belong to
133
  /// @p UseMBB.
134
  ///
135
  /// If exactly one def can reach @p UseMBB, and the def dominates @p Kill,
136
  /// all paths from the def to @p UseMBB are added to @p LR, and the function
137
  /// returns true.
138
  ///
139
  /// If multiple values can reach @p UseMBB, the blocks that need @p LR to be
140
  /// live in are added to the LiveIn array, and the function returns false.
141
  ///
142
  /// The array @p Undef provides the locations where the range @p LR becomes
143
  /// undefined by <def,read-undef> operands on other subranges. If @p Undef
144
  /// is non-empty and @p Kill is jointly dominated only by the entries of
145
  /// @p Undef, the function returns false.
146
  ///
147
  /// PhysReg, when set, is used to verify live-in lists on basic blocks.
148
  bool findReachingDefs(LiveRange &LR, MachineBasicBlock &UseMBB,
149
                        SlotIndex Use, unsigned PhysReg,
150
                        ArrayRef<SlotIndex> Undefs);
151
152
  /// updateSSA - Compute the values that will be live in to all requested
153
  /// blocks in LiveIn.  Create PHI-def values as required to preserve SSA form.
154
  ///
155
  /// Every live-in block must be jointly dominated by the added live-out
156
  /// blocks.  No values are read from the live ranges.
157
  void updateSSA();
158
159
  /// Transfer information from the LiveIn vector to the live ranges and update
160
  /// the given @p LiveOuts.
161
  void updateFromLiveIns();
162
163
  /// Extend the live range of @p LR to reach all uses of Reg.
164
  ///
165
  /// If @p LR is a main range, or if @p LI is null, then all uses must be
166
  /// jointly dominated by the definitions from @p LR. If @p LR is a subrange
167
  /// of the live interval @p LI, corresponding to lane mask @p LaneMask,
168
  /// all uses must be jointly dominated by the definitions from @p LR
169
  /// together with definitions of other lanes where @p LR becomes undefined
170
  /// (via <def,read-undef> operands).
171
  /// If @p LR is a main range, the @p LaneMask should be set to ~0, i.e.
172
  /// LaneBitmask::getAll().
173
  void extendToUses(LiveRange &LR, unsigned Reg, LaneBitmask LaneMask,
174
                    LiveInterval *LI = nullptr);
175
176
  /// Reset Map and Seen fields.
177
  void resetLiveOutMap();
178
179
public:
180
1.67M
  LiveRangeCalc() = default;
181
182
  //===--------------------------------------------------------------------===//
183
  // High-level interface.
184
  //===--------------------------------------------------------------------===//
185
  //
186
  // Calculate live ranges from scratch.
187
  //
188
189
  /// reset - Prepare caches for a new set of non-overlapping live ranges.  The
190
  /// caches must be reset before attempting calculations with a live range
191
  /// that may overlap a previously computed live range, and before the first
192
  /// live range in a function.  If live ranges are not known to be
193
  /// non-overlapping, call reset before each.
194
  void reset(const MachineFunction *mf, SlotIndexes *SI,
195
             MachineDominatorTree *MDT, VNInfo::Allocator *VNIA);
196
197
  //===--------------------------------------------------------------------===//
198
  // Mid-level interface.
199
  //===--------------------------------------------------------------------===//
200
  //
201
  // Modify existing live ranges.
202
  //
203
204
  /// Extend the live range of @p LR to reach @p Use.
205
  ///
206
  /// The existing values in @p LR must be live so they jointly dominate @p Use.
207
  /// If @p Use is not dominated by a single existing value, PHI-defs are
208
  /// inserted as required to preserve SSA form.
209
  ///
210
  /// PhysReg, when set, is used to verify live-in lists on basic blocks.
211
  void extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg,
212
              ArrayRef<SlotIndex> Undefs);
213
214
  /// createDeadDefs - Create a dead def in LI for every def operand of Reg.
215
  /// Each instruction defining Reg gets a new VNInfo with a corresponding
216
  /// minimal live range.
217
  void createDeadDefs(LiveRange &LR, unsigned Reg);
218
219
  /// Extend the live range of @p LR to reach all uses of Reg.
220
  ///
221
  /// All uses must be jointly dominated by existing liveness.  PHI-defs are
222
  /// inserted as needed to preserve SSA form.
223
2.08M
  void extendToUses(LiveRange &LR, unsigned PhysReg) {
224
2.08M
    extendToUses(LR, PhysReg, LaneBitmask::getAll());
225
2.08M
  }
226
227
  /// Calculates liveness for the register specified in live interval @p LI.
228
  /// Creates subregister live ranges as needed if subreg liveness tracking is
229
  /// enabled.
230
  void calculate(LiveInterval &LI, bool TrackSubRegs);
231
232
  /// For live interval \p LI with correct SubRanges construct matching
233
  /// information for the main live range. Expects the main live range to not
234
  /// have any segments or value numbers.
235
  void constructMainRangeFromSubranges(LiveInterval &LI);
236
237
  //===--------------------------------------------------------------------===//
238
  // Low-level interface.
239
  //===--------------------------------------------------------------------===//
240
  //
241
  // These functions can be used to compute live ranges where the live-in and
242
  // live-out blocks are already known, but the SSA value in each block is
243
  // unknown.
244
  //
245
  // After calling reset(), add known live-out values and known live-in blocks.
246
  // Then call calculateValues() to compute the actual value that is
247
  // live-in to each block, and add liveness to the live ranges.
248
  //
249
250
  /// setLiveOutValue - Indicate that VNI is live out from MBB.  The
251
  /// calculateValues() function will not add liveness for MBB, the caller
252
  /// should take care of that.
253
  ///
254
  /// VNI may be null only if MBB is a live-through block also passed to
255
  /// addLiveInBlock().
256
30.0M
  void setLiveOutValue(MachineBasicBlock *MBB, VNInfo *VNI) {
257
30.0M
    Seen.set(MBB->getNumber());
258
30.0M
    Map[MBB] = LiveOutPair(VNI, nullptr);
259
30.0M
  }
260
261
  /// addLiveInBlock - Add a block with an unknown live-in value.  This
262
  /// function can only be called once per basic block.  Once the live-in value
263
  /// has been determined, calculateValues() will add liveness to LI.
264
  ///
265
  /// @param LR      The live range that is live-in to the block.
266
  /// @param DomNode The domtree node for the block.
267
  /// @param Kill    Index in block where LI is killed.  If the value is
268
  ///                live-through, set Kill = SLotIndex() and also call
269
  ///                setLiveOutValue(MBB, 0).
270
  void addLiveInBlock(LiveRange &LR,
271
                      MachineDomTreeNode *DomNode,
272
2.46M
                      SlotIndex Kill = SlotIndex()) {
273
2.46M
    LiveIn.push_back(LiveInBlock(LR, DomNode, Kill));
274
2.46M
  }
275
276
  /// calculateValues - Calculate the value that will be live-in to each block
277
  /// added with addLiveInBlock.  Add PHI-def values as needed to preserve SSA
278
  /// form.  Add liveness to all live-in blocks up to the Kill point, or the
279
  /// whole block for live-through blocks.
280
  ///
281
  /// Every predecessor of a live-in block must have been given a value with
282
  /// setLiveOutValue, the value may be null for live-trough blocks.
283
  void calculateValues();
284
285
  /// A diagnostic function to check if the end of the block @p MBB is
286
  /// jointly dominated by the blocks corresponding to the slot indices
287
  /// in @p Defs. This function is mainly for use in self-verification
288
  /// checks.
289
  LLVM_ATTRIBUTE_UNUSED
290
  static bool isJointlyDominated(const MachineBasicBlock *MBB,
291
                                 ArrayRef<SlotIndex> Defs,
292
                                 const SlotIndexes &Indexes);
293
};
294
295
} // end namespace llvm
296
297
#endif // LLVM_LIB_CODEGEN_LIVERANGECALC_H