LLVM  9.0.0svn
LiveRangeCalc.h
Go to the documentation of this file.
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"
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 
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.
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 }.
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.
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  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,
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  void extendToUses(LiveRange &LR, unsigned PhysReg) {
224  extendToUses(LR, PhysReg, LaneBitmask::getAll());
225  }
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.
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().
257  Seen.set(MBB->getNumber());
258  Map[MBB] = LiveOutPair(VNI, nullptr);
259  }
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).
271  MachineDomTreeNode *DomNode,
272  SlotIndex Kill = SlotIndex()) {
273  LiveIn.push_back(LiveInBlock(LR, DomNode, Kill));
274  }
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.
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
BitVector & set()
Definition: BitVector.h:397
A common definition of LaneBitmask for use in TableGen and CodeGen.
void createDeadDefs(LiveRange &LR, unsigned Reg)
createDeadDefs - Create a dead def in LI for every def operand of Reg.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void setLiveOutValue(MachineBasicBlock *MBB, VNInfo *VNI)
setLiveOutValue - Indicate that VNI is live out from MBB.
void push_back(const T &Elt)
Definition: SmallVector.h:211
LiveInterval - This class represents the liveness of a register, or stack slot.
Definition: LiveInterval.h:675
unsigned Reg
void constructMainRangeFromSubranges(LiveInterval &LI)
For live interval LI with correct SubRanges construct matching information for the main live range...
VNInfo - Value Number Information.
Definition: LiveInterval.h:52
This class represents the liveness of a register, stack slot, etc.
Definition: LiveInterval.h:156
DomTreeNodeBase< MachineBasicBlock > MachineDomTreeNode
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
static constexpr LaneBitmask getAll()
Definition: LaneBitmask.h:83
LiveRangeCalc()=default
SlotIndexes pass.
Definition: SlotIndexes.h:314
void addLiveInBlock(LiveRange &LR, MachineDomTreeNode *DomNode, SlotIndex Kill=SlotIndex())
addLiveInBlock - Add a block with an unknown live-in value.
Base class for the actual dominator tree node.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
void extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg, ArrayRef< SlotIndex > Undefs)
Extend the live range of LR to reach Use.
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they&#39;re not in a MachineFuncti...
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:140
#define LLVM_ATTRIBUTE_UNUSED
Definition: Compiler.h:159
void reset(const MachineFunction *mf, SlotIndexes *SI, MachineDominatorTree *MDT, VNInfo::Allocator *VNIA)
reset - Prepare caches for a new set of non-overlapping live ranges.
void calculate(LiveInterval &LI, bool TrackSubRegs)
Calculates liveness for the register specified in live interval LI.
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
void extendToUses(LiveRange &LR, unsigned PhysReg)
Extend the live range of LR to reach all uses of Reg.
LLVM Value Representation.
Definition: Value.h:72
SlotIndex - An opaque wrapper around machine indexes.
Definition: SlotIndexes.h:83
void calculateValues()
calculateValues - Calculate the value that will be live-in to each block added with addLiveInBlock...
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
static LLVM_ATTRIBUTE_UNUSED bool isJointlyDominated(const MachineBasicBlock *MBB, ArrayRef< SlotIndex > Defs, const SlotIndexes &Indexes)
A diagnostic function to check if the end of the block MBB is jointly dominated by the blocks corresp...