blob: d41b782d9bdf21659d0cc2a4e32185ee8f404a0b [file] [log] [blame]
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +00001//===---- LiveRangeCalc.h - Calculate live ranges ---------------*- 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// The LiveRangeCalc class can be used to compute live ranges from scratch. It
11// caches information about values in the CFG to speed up repeated operations
12// on the same live range. The cache can be shared by non-overlapping live
13// ranges. SplitKit uses that when computing the live range of split products.
14//
15// A low-level interface is available to clients that know where a variable is
16// live, but don't know which value it has as every point. LiveRangeCalc will
17// propagate values down the dominator tree, and even insert PHI-defs where
18// needed. SplitKit uses this faster interface when possible.
19//
20//===----------------------------------------------------------------------===//
21
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000022#ifndef LLVM_LIB_CODEGEN_LIVERANGECALC_H
23#define LLVM_LIB_CODEGEN_LIVERANGECALC_H
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000024
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +000025#include "llvm/ADT/ArrayRef.h"
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000026#include "llvm/ADT/BitVector.h"
Matthias Brauna6e77402017-06-27 18:05:26 +000027#include "llvm/ADT/DenseMap.h"
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000028#include "llvm/ADT/IndexedMap.h"
29#include "llvm/CodeGen/LiveInterval.h"
30
31namespace llvm {
32
33/// Forward declarations for MachineDominators.h:
34class MachineDominatorTree;
35template <class NodeT> class DomTreeNodeBase;
36typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
37
Benjamin Kramer079b96e2013-09-11 18:05:11 +000038class LiveRangeCalc {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +000039 const MachineFunction *MF;
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +000040 const MachineRegisterInfo *MRI;
41 SlotIndexes *Indexes;
42 MachineDominatorTree *DomTree;
43 VNInfo::Allocator *Alloc;
44
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000045 /// LiveOutPair - A value and the block that defined it. The domtree node is
46 /// redundant, it can be computed as: MDT[Indexes.getMBBFromIndex(VNI->def)].
47 typedef std::pair<VNInfo*, MachineDomTreeNode*> LiveOutPair;
48
49 /// LiveOutMap - Map basic blocks to the value leaving the block.
50 typedef IndexedMap<LiveOutPair, MBB2NumberFunctor> LiveOutMap;
51
Matthias Braun1aed6ff2014-12-16 04:03:38 +000052 /// Bit vector of active entries in LiveOut, also used as a visited set by
53 /// findReachingDefs. One entry per basic block, indexed by block number.
54 /// This is kept as a separate bit vector because it can be cleared quickly
55 /// when switching live ranges.
56 BitVector Seen;
Matthias Braun2f662322014-12-10 01:12:12 +000057
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +000058 /// Map LiveRange to sets of blocks (represented by bit vectors) that
59 /// in the live range are defined on entry and undefined on entry.
60 /// A block is defined on entry if there is a path from at least one of
61 /// the defs in the live range to the entry of the block, and conversely,
62 /// a block is undefined on entry, if there is no such path (i.e. no
63 /// definition reaches the entry of the block). A single LiveRangeCalc
64 /// object is used to track live-out information for multiple registers
65 /// in live range splitting (which is ok, since the live ranges of these
66 /// registers do not overlap), but the defined/undefined information must
67 /// be kept separate for each individual range.
68 /// By convention, EntryInfoMap[&LR] = { Defined, Undefined }.
Matthias Brauna6e77402017-06-27 18:05:26 +000069 typedef DenseMap<LiveRange*,std::pair<BitVector,BitVector>> EntryInfoMap;
70 EntryInfoMap EntryInfos;
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +000071
Matthias Braun1aed6ff2014-12-16 04:03:38 +000072 /// Map each basic block where a live range is live out to the live-out value
73 /// and its defining block.
74 ///
75 /// For every basic block, MBB, one of these conditions shall be true:
76 ///
77 /// 1. !Seen.count(MBB->getNumber())
78 /// Blocks without a Seen bit are ignored.
79 /// 2. LiveOut[MBB].second.getNode() == MBB
80 /// The live-out value is defined in MBB.
81 /// 3. forall P in preds(MBB): LiveOut[P] == LiveOut[MBB]
82 /// The live-out value passses through MBB. All predecessors must carry
83 /// the same value.
84 ///
85 /// The domtree node may be null, it can be computed.
86 ///
87 /// The map can be shared by multiple live ranges as long as no two are
88 /// live-out of the same block.
89 LiveOutMap Map;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000090
91 /// LiveInBlock - Information about a basic block where a live range is known
92 /// to be live-in, but the value has not yet been determined.
93 struct LiveInBlock {
Matthias Braun2d5c32b2013-10-10 21:28:57 +000094 // The live range set that is live-in to this block. The algorithms can
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000095 // handle multiple non-overlapping live ranges simultaneously.
Matthias Braun2d5c32b2013-10-10 21:28:57 +000096 LiveRange &LR;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000097
98 // DomNode - Dominator tree node for the block.
99 // Cleared when the final value has been determined and LI has been updated.
100 MachineDomTreeNode *DomNode;
101
102 // Position in block where the live-in range ends, or SlotIndex() if the
103 // range passes through the block. When the final value has been
104 // determined, the range from the block start to Kill will be added to LI.
105 SlotIndex Kill;
106
107 // Live-in value filled in by updateSSA once it is known.
108 VNInfo *Value;
109
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000110 LiveInBlock(LiveRange &LR, MachineDomTreeNode *node, SlotIndex kill)
Craig Topperada08572014-04-16 04:21:27 +0000111 : LR(LR), DomNode(node), Kill(kill), Value(nullptr) {}
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000112 };
113
114 /// LiveIn - Work list of blocks where the live-in value has yet to be
115 /// determined. This list is typically computed by findReachingDefs() and
116 /// used as a work list by updateSSA(). The low-level interface may also be
117 /// used to add entries directly.
118 SmallVector<LiveInBlock, 16> LiveIn;
119
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000120 /// Check if the entry to block @p MBB can be reached by any of the defs
121 /// in @p LR. Return true if none of the defs reach the entry to @p MBB.
122 bool isDefOnEntry(LiveRange &LR, ArrayRef<SlotIndex> Undefs,
123 MachineBasicBlock &MBB, BitVector &DefOnEntry,
124 BitVector &UndefOnEntry);
125
126 /// Find the set of defs that can reach @p Kill. @p Kill must belong to
127 /// @p UseMBB.
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000128 ///
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000129 /// If exactly one def can reach @p UseMBB, and the def dominates @p Kill,
130 /// all paths from the def to @p UseMBB are added to @p LR, and the function
131 /// returns true.
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000132 ///
Matthias Braun11042c82015-02-18 01:50:52 +0000133 /// If multiple values can reach @p UseMBB, the blocks that need @p LR to be
134 /// live in are added to the LiveIn array, and the function returns false.
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000135 ///
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000136 /// The array @p Undef provides the locations where the range @p LR becomes
137 /// undefined by <def,read-undef> operands on other subranges. If @p Undef
138 /// is non-empty and @p Kill is jointly dominated only by the entries of
139 /// @p Undef, the function returns false.
140 ///
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000141 /// PhysReg, when set, is used to verify live-in lists on basic blocks.
Matthias Braun11042c82015-02-18 01:50:52 +0000142 bool findReachingDefs(LiveRange &LR, MachineBasicBlock &UseMBB,
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000143 SlotIndex Kill, unsigned PhysReg,
144 ArrayRef<SlotIndex> Undefs);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000145
146 /// updateSSA - Compute the values that will be live in to all requested
147 /// blocks in LiveIn. Create PHI-def values as required to preserve SSA form.
148 ///
149 /// Every live-in block must be jointly dominated by the added live-out
150 /// blocks. No values are read from the live ranges.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000151 void updateSSA();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000152
Matthias Braun2f662322014-12-10 01:12:12 +0000153 /// Transfer information from the LiveIn vector to the live ranges and update
154 /// the given @p LiveOuts.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000155 void updateFromLiveIns();
156
157 /// Extend the live range of @p LR to reach all uses of Reg.
158 ///
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000159 /// If @p LR is a main range, or if @p LI is null, then all uses must be
160 /// jointly dominated by the definitions from @p LR. If @p LR is a subrange
161 /// of the live interval @p LI, corresponding to lane mask @p LaneMask,
162 /// all uses must be jointly dominated by the definitions from @p LR
163 /// together with definitions of other lanes where @p LR becomes undefined
164 /// (via <def,read-undef> operands).
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000165 /// If @p LR is a main range, the @p LaneMask should be set to ~0, i.e.
166 /// LaneBitmask::getAll().
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000167 void extendToUses(LiveRange &LR, unsigned Reg, LaneBitmask LaneMask,
168 LiveInterval *LI = nullptr);
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000169
170 /// Reset Map and Seen fields.
171 void resetLiveOutMap();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000172
173public:
Craig Topperada08572014-04-16 04:21:27 +0000174 LiveRangeCalc() : MF(nullptr), MRI(nullptr), Indexes(nullptr),
175 DomTree(nullptr), Alloc(nullptr) {}
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000176
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000177 //===--------------------------------------------------------------------===//
178 // High-level interface.
179 //===--------------------------------------------------------------------===//
180 //
181 // Calculate live ranges from scratch.
182 //
183
184 /// reset - Prepare caches for a new set of non-overlapping live ranges. The
185 /// caches must be reset before attempting calculations with a live range
186 /// that may overlap a previously computed live range, and before the first
187 /// live range in a function. If live ranges are not known to be
188 /// non-overlapping, call reset before each.
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000189 void reset(const MachineFunction *MF,
190 SlotIndexes*,
191 MachineDominatorTree*,
192 VNInfo::Allocator*);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000193
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000194 //===--------------------------------------------------------------------===//
195 // Mid-level interface.
196 //===--------------------------------------------------------------------===//
197 //
198 // Modify existing live ranges.
199 //
200
Matthias Braun11042c82015-02-18 01:50:52 +0000201 /// Extend the live range of @p LR to reach @p Use.
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000202 ///
Matthias Braun11042c82015-02-18 01:50:52 +0000203 /// The existing values in @p LR must be live so they jointly dominate @p Use.
204 /// If @p Use is not dominated by a single existing value, PHI-defs are
205 /// inserted as required to preserve SSA form.
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000206 ///
207 /// PhysReg, when set, is used to verify live-in lists on basic blocks.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000208 void extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg,
209 ArrayRef<SlotIndex> Undefs);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000210
Matthias Braunc3a72c22014-12-15 21:36:35 +0000211 /// createDeadDefs - Create a dead def in LI for every def operand of Reg.
212 /// Each instruction defining Reg gets a new VNInfo with a corresponding
213 /// minimal live range.
214 void createDeadDefs(LiveRange &LR, unsigned Reg);
215
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000216 /// Extend the live range of @p LR to reach all uses of Reg.
Matthias Braunc3a72c22014-12-15 21:36:35 +0000217 ///
218 /// All uses must be jointly dominated by existing liveness. PHI-defs are
219 /// inserted as needed to preserve SSA form.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000220 void extendToUses(LiveRange &LR, unsigned PhysReg) {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000221 extendToUses(LR, PhysReg, LaneBitmask::getAll());
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000222 }
Matthias Braunc3a72c22014-12-15 21:36:35 +0000223
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000224 /// Calculates liveness for the register specified in live interval @p LI.
225 /// Creates subregister live ranges as needed if subreg liveness tracking is
226 /// enabled.
Matthias Brauna25e13a2015-03-19 00:21:58 +0000227 void calculate(LiveInterval &LI, bool TrackSubRegs);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000228
Matthias Braun71f95642016-05-20 23:14:56 +0000229 /// For live interval \p LI with correct SubRanges construct matching
230 /// information for the main live range. Expects the main live range to not
231 /// have any segments or value numbers.
232 void constructMainRangeFromSubranges(LiveInterval &LI);
233
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000234 //===--------------------------------------------------------------------===//
235 // Low-level interface.
236 //===--------------------------------------------------------------------===//
237 //
238 // These functions can be used to compute live ranges where the live-in and
239 // live-out blocks are already known, but the SSA value in each block is
240 // unknown.
241 //
242 // After calling reset(), add known live-out values and known live-in blocks.
243 // Then call calculateValues() to compute the actual value that is
244 // live-in to each block, and add liveness to the live ranges.
245 //
246
247 /// setLiveOutValue - Indicate that VNI is live out from MBB. The
248 /// calculateValues() function will not add liveness for MBB, the caller
249 /// should take care of that.
250 ///
251 /// VNI may be null only if MBB is a live-through block also passed to
252 /// addLiveInBlock().
253 void setLiveOutValue(MachineBasicBlock *MBB, VNInfo *VNI) {
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000254 Seen.set(MBB->getNumber());
255 Map[MBB] = LiveOutPair(VNI, nullptr);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000256 }
257
258 /// addLiveInBlock - Add a block with an unknown live-in value. This
259 /// function can only be called once per basic block. Once the live-in value
260 /// has been determined, calculateValues() will add liveness to LI.
261 ///
NAKAMURA Takumid5d16d52013-10-11 04:52:03 +0000262 /// @param LR The live range that is live-in to the block.
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000263 /// @param DomNode The domtree node for the block.
264 /// @param Kill Index in block where LI is killed. If the value is
265 /// live-through, set Kill = SLotIndex() and also call
266 /// setLiveOutValue(MBB, 0).
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000267 void addLiveInBlock(LiveRange &LR,
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000268 MachineDomTreeNode *DomNode,
269 SlotIndex Kill = SlotIndex()) {
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000270 LiveIn.push_back(LiveInBlock(LR, DomNode, Kill));
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000271 }
272
273 /// calculateValues - Calculate the value that will be live-in to each block
274 /// added with addLiveInBlock. Add PHI-def values as needed to preserve SSA
275 /// form. Add liveness to all live-in blocks up to the Kill point, or the
276 /// whole block for live-through blocks.
277 ///
278 /// Every predecessor of a live-in block must have been given a value with
279 /// setLiveOutValue, the value may be null for live-trough blocks.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000280 void calculateValues();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000281};
282
283} // end namespace llvm
284
285#endif