LiveRangeCalc.h revision 360784
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
35namespace llvm {
36
37template <class NodeT> class DomTreeNodeBase;
38class MachineDominatorTree;
39class MachineFunction;
40class MachineRegisterInfo;
41
42using MachineDomTreeNode = DomTreeNodeBase<MachineBasicBlock>;
43
44class 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        : 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, SlotIndex Use,
149                        unsigned PhysReg, ArrayRef<SlotIndex> Undefs);
150
151  /// updateSSA - Compute the values that will be live in to all requested
152  /// blocks in LiveIn.  Create PHI-def values as required to preserve SSA form.
153  ///
154  /// Every live-in block must be jointly dominated by the added live-out
155  /// blocks.  No values are read from the live ranges.
156  void updateSSA();
157
158  /// Transfer information from the LiveIn vector to the live ranges and update
159  /// the given @p LiveOuts.
160  void updateFromLiveIns();
161
162  /// Extend the live range of @p LR to reach all uses of Reg.
163  ///
164  /// If @p LR is a main range, or if @p LI is null, then all uses must be
165  /// jointly dominated by the definitions from @p LR. If @p LR is a subrange
166  /// of the live interval @p LI, corresponding to lane mask @p LaneMask,
167  /// all uses must be jointly dominated by the definitions from @p LR
168  /// together with definitions of other lanes where @p LR becomes undefined
169  /// (via <def,read-undef> operands).
170  /// If @p LR is a main range, the @p LaneMask should be set to ~0, i.e.
171  /// LaneBitmask::getAll().
172  void extendToUses(LiveRange &LR, unsigned Reg, LaneBitmask LaneMask,
173                    LiveInterval *LI = nullptr);
174
175  /// Reset Map and Seen fields.
176  void resetLiveOutMap();
177
178public:
179  LiveRangeCalc() = default;
180
181  //===--------------------------------------------------------------------===//
182  // High-level interface.
183  //===--------------------------------------------------------------------===//
184  //
185  // Calculate live ranges from scratch.
186  //
187
188  /// reset - Prepare caches for a new set of non-overlapping live ranges.  The
189  /// caches must be reset before attempting calculations with a live range
190  /// that may overlap a previously computed live range, and before the first
191  /// live range in a function.  If live ranges are not known to be
192  /// non-overlapping, call reset before each.
193  void reset(const MachineFunction *mf, SlotIndexes *SI,
194             MachineDominatorTree *MDT, VNInfo::Allocator *VNIA);
195
196  //===--------------------------------------------------------------------===//
197  // Mid-level interface.
198  //===--------------------------------------------------------------------===//
199  //
200  // Modify existing live ranges.
201  //
202
203  /// Extend the live range of @p LR to reach @p Use.
204  ///
205  /// The existing values in @p LR must be live so they jointly dominate @p Use.
206  /// If @p Use is not dominated by a single existing value, PHI-defs are
207  /// inserted as required to preserve SSA form.
208  ///
209  /// PhysReg, when set, is used to verify live-in lists on basic blocks.
210  void extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg,
211              ArrayRef<SlotIndex> Undefs);
212
213  /// createDeadDefs - Create a dead def in LI for every def operand of Reg.
214  /// Each instruction defining Reg gets a new VNInfo with a corresponding
215  /// minimal live range.
216  void createDeadDefs(LiveRange &LR, unsigned Reg);
217
218  /// Extend the live range of @p LR to reach all uses of Reg.
219  ///
220  /// All uses must be jointly dominated by existing liveness.  PHI-defs are
221  /// inserted as needed to preserve SSA form.
222  void extendToUses(LiveRange &LR, unsigned PhysReg) {
223    extendToUses(LR, PhysReg, LaneBitmask::getAll());
224  }
225
226  /// Calculates liveness for the register specified in live interval @p LI.
227  /// Creates subregister live ranges as needed if subreg liveness tracking is
228  /// enabled.
229  void calculate(LiveInterval &LI, bool TrackSubRegs);
230
231  /// For live interval \p LI with correct SubRanges construct matching
232  /// information for the main live range. Expects the main live range to not
233  /// have any segments or value numbers.
234  void constructMainRangeFromSubranges(LiveInterval &LI);
235
236  //===--------------------------------------------------------------------===//
237  // Low-level interface.
238  //===--------------------------------------------------------------------===//
239  //
240  // These functions can be used to compute live ranges where the live-in and
241  // live-out blocks are already known, but the SSA value in each block is
242  // unknown.
243  //
244  // After calling reset(), add known live-out values and known live-in blocks.
245  // Then call calculateValues() to compute the actual value that is
246  // live-in to each block, and add liveness to the live ranges.
247  //
248
249  /// setLiveOutValue - Indicate that VNI is live out from MBB.  The
250  /// calculateValues() function will not add liveness for MBB, the caller
251  /// should take care of that.
252  ///
253  /// VNI may be null only if MBB is a live-through block also passed to
254  /// addLiveInBlock().
255  void setLiveOutValue(MachineBasicBlock *MBB, VNInfo *VNI) {
256    Seen.set(MBB->getNumber());
257    Map[MBB] = LiveOutPair(VNI, nullptr);
258  }
259
260  /// addLiveInBlock - Add a block with an unknown live-in value.  This
261  /// function can only be called once per basic block.  Once the live-in value
262  /// has been determined, calculateValues() will add liveness to LI.
263  ///
264  /// @param LR      The live range that is live-in to the block.
265  /// @param DomNode The domtree node for the block.
266  /// @param Kill    Index in block where LI is killed.  If the value is
267  ///                live-through, set Kill = SLotIndex() and also call
268  ///                setLiveOutValue(MBB, 0).
269  void addLiveInBlock(LiveRange &LR, MachineDomTreeNode *DomNode,
270                      SlotIndex Kill = SlotIndex()) {
271    LiveIn.push_back(LiveInBlock(LR, DomNode, Kill));
272  }
273
274  /// calculateValues - Calculate the value that will be live-in to each block
275  /// added with addLiveInBlock.  Add PHI-def values as needed to preserve SSA
276  /// form.  Add liveness to all live-in blocks up to the Kill point, or the
277  /// whole block for live-through blocks.
278  ///
279  /// Every predecessor of a live-in block must have been given a value with
280  /// setLiveOutValue, the value may be null for live-trough blocks.
281  void calculateValues();
282
283  /// A diagnostic function to check if the end of the block @p MBB is
284  /// jointly dominated by the blocks corresponding to the slot indices
285  /// in @p Defs. This function is mainly for use in self-verification
286  /// checks.
287  LLVM_ATTRIBUTE_UNUSED
288  static bool isJointlyDominated(const MachineBasicBlock *MBB,
289                                 ArrayRef<SlotIndex> Defs,
290                                 const SlotIndexes &Indexes);
291};
292
293} // end namespace llvm
294
295#endif // LLVM_LIB_CODEGEN_LIVERANGECALC_H
296