blob: 1a7598f8044a5fb0582cbee0ef161d8b18495268 [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"
27#include "llvm/ADT/IndexedMap.h"
28#include "llvm/CodeGen/LiveInterval.h"
29
30namespace llvm {
31
32/// Forward declarations for MachineDominators.h:
33class MachineDominatorTree;
34template <class NodeT> class DomTreeNodeBase;
35typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
36
Benjamin Kramer079b96e2013-09-11 18:05:11 +000037class LiveRangeCalc {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +000038 const MachineFunction *MF;
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +000039 const MachineRegisterInfo *MRI;
40 SlotIndexes *Indexes;
41 MachineDominatorTree *DomTree;
42 VNInfo::Allocator *Alloc;
43
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000044 /// LiveOutPair - A value and the block that defined it. The domtree node is
45 /// redundant, it can be computed as: MDT[Indexes.getMBBFromIndex(VNI->def)].
46 typedef std::pair<VNInfo*, MachineDomTreeNode*> LiveOutPair;
47
48 /// LiveOutMap - Map basic blocks to the value leaving the block.
49 typedef IndexedMap<LiveOutPair, MBB2NumberFunctor> LiveOutMap;
50
Matthias Braun1aed6ff2014-12-16 04:03:38 +000051 /// Bit vector of active entries in LiveOut, also used as a visited set by
52 /// findReachingDefs. One entry per basic block, indexed by block number.
53 /// This is kept as a separate bit vector because it can be cleared quickly
54 /// when switching live ranges.
55 BitVector Seen;
Matthias Braun2f662322014-12-10 01:12:12 +000056
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +000057 /// Map LiveRange to sets of blocks (represented by bit vectors) that
58 /// in the live range are defined on entry and undefined on entry.
59 /// A block is defined on entry if there is a path from at least one of
60 /// the defs in the live range to the entry of the block, and conversely,
61 /// a block is undefined on entry, if there is no such path (i.e. no
62 /// definition reaches the entry of the block). A single LiveRangeCalc
63 /// object is used to track live-out information for multiple registers
64 /// in live range splitting (which is ok, since the live ranges of these
65 /// registers do not overlap), but the defined/undefined information must
66 /// be kept separate for each individual range.
67 /// By convention, EntryInfoMap[&LR] = { Defined, Undefined }.
68 std::map<LiveRange*,std::pair<BitVector,BitVector>> EntryInfoMap;
69
Matthias Braun1aed6ff2014-12-16 04:03:38 +000070 /// Map each basic block where a live range is live out to the live-out value
71 /// and its defining block.
72 ///
73 /// For every basic block, MBB, one of these conditions shall be true:
74 ///
75 /// 1. !Seen.count(MBB->getNumber())
76 /// Blocks without a Seen bit are ignored.
77 /// 2. LiveOut[MBB].second.getNode() == MBB
78 /// The live-out value is defined in MBB.
79 /// 3. forall P in preds(MBB): LiveOut[P] == LiveOut[MBB]
80 /// The live-out value passses through MBB. All predecessors must carry
81 /// the same value.
82 ///
83 /// The domtree node may be null, it can be computed.
84 ///
85 /// The map can be shared by multiple live ranges as long as no two are
86 /// live-out of the same block.
87 LiveOutMap Map;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000088
89 /// LiveInBlock - Information about a basic block where a live range is known
90 /// to be live-in, but the value has not yet been determined.
91 struct LiveInBlock {
Matthias Braun2d5c32b2013-10-10 21:28:57 +000092 // The live range set that is live-in to this block. The algorithms can
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000093 // handle multiple non-overlapping live ranges simultaneously.
Matthias Braun2d5c32b2013-10-10 21:28:57 +000094 LiveRange &LR;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000095
96 // DomNode - Dominator tree node for the block.
97 // Cleared when the final value has been determined and LI has been updated.
98 MachineDomTreeNode *DomNode;
99
100 // Position in block where the live-in range ends, or SlotIndex() if the
101 // range passes through the block. When the final value has been
102 // determined, the range from the block start to Kill will be added to LI.
103 SlotIndex Kill;
104
105 // Live-in value filled in by updateSSA once it is known.
106 VNInfo *Value;
107
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000108 LiveInBlock(LiveRange &LR, MachineDomTreeNode *node, SlotIndex kill)
Craig Topperada08572014-04-16 04:21:27 +0000109 : LR(LR), DomNode(node), Kill(kill), Value(nullptr) {}
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000110 };
111
112 /// LiveIn - Work list of blocks where the live-in value has yet to be
113 /// determined. This list is typically computed by findReachingDefs() and
114 /// used as a work list by updateSSA(). The low-level interface may also be
115 /// used to add entries directly.
116 SmallVector<LiveInBlock, 16> LiveIn;
117
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000118 /// Check if the entry to block @p MBB can be reached by any of the defs
119 /// in @p LR. Return true if none of the defs reach the entry to @p MBB.
120 bool isDefOnEntry(LiveRange &LR, ArrayRef<SlotIndex> Undefs,
121 MachineBasicBlock &MBB, BitVector &DefOnEntry,
122 BitVector &UndefOnEntry);
123
124 /// Find the set of defs that can reach @p Kill. @p Kill must belong to
125 /// @p UseMBB.
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000126 ///
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000127 /// If exactly one def can reach @p UseMBB, and the def dominates @p Kill,
128 /// all paths from the def to @p UseMBB are added to @p LR, and the function
129 /// returns true.
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000130 ///
Matthias Braun11042c82015-02-18 01:50:52 +0000131 /// If multiple values can reach @p UseMBB, the blocks that need @p LR to be
132 /// live in are added to the LiveIn array, and the function returns false.
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000133 ///
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000134 /// The array @p Undef provides the locations where the range @p LR becomes
135 /// undefined by <def,read-undef> operands on other subranges. If @p Undef
136 /// is non-empty and @p Kill is jointly dominated only by the entries of
137 /// @p Undef, the function returns false.
138 ///
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000139 /// PhysReg, when set, is used to verify live-in lists on basic blocks.
Matthias Braun11042c82015-02-18 01:50:52 +0000140 bool findReachingDefs(LiveRange &LR, MachineBasicBlock &UseMBB,
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000141 SlotIndex Kill, unsigned PhysReg,
142 ArrayRef<SlotIndex> Undefs);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000143
144 /// updateSSA - Compute the values that will be live in to all requested
145 /// blocks in LiveIn. Create PHI-def values as required to preserve SSA form.
146 ///
147 /// Every live-in block must be jointly dominated by the added live-out
148 /// blocks. No values are read from the live ranges.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000149 void updateSSA();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000150
Matthias Braun2f662322014-12-10 01:12:12 +0000151 /// Transfer information from the LiveIn vector to the live ranges and update
152 /// the given @p LiveOuts.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000153 void updateFromLiveIns();
154
155 /// Extend the live range of @p LR to reach all uses of Reg.
156 ///
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000157 /// If @p LR is a main range, or if @p LI is null, then all uses must be
158 /// jointly dominated by the definitions from @p LR. If @p LR is a subrange
159 /// of the live interval @p LI, corresponding to lane mask @p LaneMask,
160 /// all uses must be jointly dominated by the definitions from @p LR
161 /// together with definitions of other lanes where @p LR becomes undefined
162 /// (via <def,read-undef> operands).
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000163 /// If @p LR is a main range, the @p LaneMask should be set to ~0, i.e.
164 /// LaneBitmask::getAll().
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000165 void extendToUses(LiveRange &LR, unsigned Reg, LaneBitmask LaneMask,
166 LiveInterval *LI = nullptr);
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000167
168 /// Reset Map and Seen fields.
169 void resetLiveOutMap();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000170
171public:
Craig Topperada08572014-04-16 04:21:27 +0000172 LiveRangeCalc() : MF(nullptr), MRI(nullptr), Indexes(nullptr),
173 DomTree(nullptr), Alloc(nullptr) {}
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000174
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000175 //===--------------------------------------------------------------------===//
176 // High-level interface.
177 //===--------------------------------------------------------------------===//
178 //
179 // Calculate live ranges from scratch.
180 //
181
182 /// reset - Prepare caches for a new set of non-overlapping live ranges. The
183 /// caches must be reset before attempting calculations with a live range
184 /// that may overlap a previously computed live range, and before the first
185 /// live range in a function. If live ranges are not known to be
186 /// non-overlapping, call reset before each.
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000187 void reset(const MachineFunction *MF,
188 SlotIndexes*,
189 MachineDominatorTree*,
190 VNInfo::Allocator*);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000191
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000192 //===--------------------------------------------------------------------===//
193 // Mid-level interface.
194 //===--------------------------------------------------------------------===//
195 //
196 // Modify existing live ranges.
197 //
198
Matthias Braun11042c82015-02-18 01:50:52 +0000199 /// Extend the live range of @p LR to reach @p Use.
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000200 ///
Matthias Braun11042c82015-02-18 01:50:52 +0000201 /// The existing values in @p LR must be live so they jointly dominate @p Use.
202 /// If @p Use is not dominated by a single existing value, PHI-defs are
203 /// inserted as required to preserve SSA form.
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000204 ///
205 /// PhysReg, when set, is used to verify live-in lists on basic blocks.
Krzysztof Parzyszeka7ed0902016-08-24 13:37:55 +0000206 void extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg,
207 ArrayRef<SlotIndex> Undefs);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000208
Matthias Braunc3a72c22014-12-15 21:36:35 +0000209 /// createDeadDefs - Create a dead def in LI for every def operand of Reg.
210 /// Each instruction defining Reg gets a new VNInfo with a corresponding
211 /// minimal live range.
212 void createDeadDefs(LiveRange &LR, unsigned Reg);
213
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000214 /// Extend the live range of @p LR to reach all uses of Reg.
Matthias Braunc3a72c22014-12-15 21:36:35 +0000215 ///
216 /// All uses must be jointly dominated by existing liveness. PHI-defs are
217 /// inserted as needed to preserve SSA form.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000218 void extendToUses(LiveRange &LR, unsigned PhysReg) {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000219 extendToUses(LR, PhysReg, LaneBitmask::getAll());
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000220 }
Matthias Braunc3a72c22014-12-15 21:36:35 +0000221
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000222 /// Calculates liveness for the register specified in live interval @p LI.
223 /// Creates subregister live ranges as needed if subreg liveness tracking is
224 /// enabled.
Matthias Brauna25e13a2015-03-19 00:21:58 +0000225 void calculate(LiveInterval &LI, bool TrackSubRegs);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000226
Matthias Braun71f95642016-05-20 23:14:56 +0000227 /// For live interval \p LI with correct SubRanges construct matching
228 /// information for the main live range. Expects the main live range to not
229 /// have any segments or value numbers.
230 void constructMainRangeFromSubranges(LiveInterval &LI);
231
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000232 //===--------------------------------------------------------------------===//
233 // Low-level interface.
234 //===--------------------------------------------------------------------===//
235 //
236 // These functions can be used to compute live ranges where the live-in and
237 // live-out blocks are already known, but the SSA value in each block is
238 // unknown.
239 //
240 // After calling reset(), add known live-out values and known live-in blocks.
241 // Then call calculateValues() to compute the actual value that is
242 // live-in to each block, and add liveness to the live ranges.
243 //
244
245 /// setLiveOutValue - Indicate that VNI is live out from MBB. The
246 /// calculateValues() function will not add liveness for MBB, the caller
247 /// should take care of that.
248 ///
249 /// VNI may be null only if MBB is a live-through block also passed to
250 /// addLiveInBlock().
251 void setLiveOutValue(MachineBasicBlock *MBB, VNInfo *VNI) {
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000252 Seen.set(MBB->getNumber());
253 Map[MBB] = LiveOutPair(VNI, nullptr);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000254 }
255
256 /// addLiveInBlock - Add a block with an unknown live-in value. This
257 /// function can only be called once per basic block. Once the live-in value
258 /// has been determined, calculateValues() will add liveness to LI.
259 ///
NAKAMURA Takumid5d16d52013-10-11 04:52:03 +0000260 /// @param LR The live range that is live-in to the block.
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000261 /// @param DomNode The domtree node for the block.
262 /// @param Kill Index in block where LI is killed. If the value is
263 /// live-through, set Kill = SLotIndex() and also call
264 /// setLiveOutValue(MBB, 0).
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000265 void addLiveInBlock(LiveRange &LR,
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000266 MachineDomTreeNode *DomNode,
267 SlotIndex Kill = SlotIndex()) {
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000268 LiveIn.push_back(LiveInBlock(LR, DomNode, Kill));
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000269 }
270
271 /// calculateValues - Calculate the value that will be live-in to each block
272 /// added with addLiveInBlock. Add PHI-def values as needed to preserve SSA
273 /// form. Add liveness to all live-in blocks up to the Kill point, or the
274 /// whole block for live-through blocks.
275 ///
276 /// Every predecessor of a live-in block must have been given a value with
277 /// setLiveOutValue, the value may be null for live-trough blocks.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000278 void calculateValues();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000279};
280
281} // end namespace llvm
282
283#endif