blob: 90bf97172e525e9befc15f48a2819cf5c975bbb5 [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
25#include "llvm/ADT/BitVector.h"
26#include "llvm/ADT/IndexedMap.h"
27#include "llvm/CodeGen/LiveInterval.h"
28
29namespace llvm {
30
31/// Forward declarations for MachineDominators.h:
32class MachineDominatorTree;
33template <class NodeT> class DomTreeNodeBase;
34typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
35
Benjamin Kramer079b96e2013-09-11 18:05:11 +000036class LiveRangeCalc {
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +000037 const MachineFunction *MF;
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +000038 const MachineRegisterInfo *MRI;
39 SlotIndexes *Indexes;
40 MachineDominatorTree *DomTree;
41 VNInfo::Allocator *Alloc;
42
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000043 /// LiveOutPair - A value and the block that defined it. The domtree node is
44 /// redundant, it can be computed as: MDT[Indexes.getMBBFromIndex(VNI->def)].
45 typedef std::pair<VNInfo*, MachineDomTreeNode*> LiveOutPair;
46
47 /// LiveOutMap - Map basic blocks to the value leaving the block.
48 typedef IndexedMap<LiveOutPair, MBB2NumberFunctor> LiveOutMap;
49
Matthias Braun1aed6ff2014-12-16 04:03:38 +000050 /// Bit vector of active entries in LiveOut, also used as a visited set by
51 /// findReachingDefs. One entry per basic block, indexed by block number.
52 /// This is kept as a separate bit vector because it can be cleared quickly
53 /// when switching live ranges.
54 BitVector Seen;
Matthias Braun2f662322014-12-10 01:12:12 +000055
Matthias Braun1aed6ff2014-12-16 04:03:38 +000056 /// Map each basic block where a live range is live out to the live-out value
57 /// and its defining block.
58 ///
59 /// For every basic block, MBB, one of these conditions shall be true:
60 ///
61 /// 1. !Seen.count(MBB->getNumber())
62 /// Blocks without a Seen bit are ignored.
63 /// 2. LiveOut[MBB].second.getNode() == MBB
64 /// The live-out value is defined in MBB.
65 /// 3. forall P in preds(MBB): LiveOut[P] == LiveOut[MBB]
66 /// The live-out value passses through MBB. All predecessors must carry
67 /// the same value.
68 ///
69 /// The domtree node may be null, it can be computed.
70 ///
71 /// The map can be shared by multiple live ranges as long as no two are
72 /// live-out of the same block.
73 LiveOutMap Map;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000074
75 /// LiveInBlock - Information about a basic block where a live range is known
76 /// to be live-in, but the value has not yet been determined.
77 struct LiveInBlock {
Matthias Braun2d5c32b2013-10-10 21:28:57 +000078 // The live range set that is live-in to this block. The algorithms can
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000079 // handle multiple non-overlapping live ranges simultaneously.
Matthias Braun2d5c32b2013-10-10 21:28:57 +000080 LiveRange &LR;
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000081
82 // DomNode - Dominator tree node for the block.
83 // Cleared when the final value has been determined and LI has been updated.
84 MachineDomTreeNode *DomNode;
85
86 // Position in block where the live-in range ends, or SlotIndex() if the
87 // range passes through the block. When the final value has been
88 // determined, the range from the block start to Kill will be added to LI.
89 SlotIndex Kill;
90
91 // Live-in value filled in by updateSSA once it is known.
92 VNInfo *Value;
93
Matthias Braun2d5c32b2013-10-10 21:28:57 +000094 LiveInBlock(LiveRange &LR, MachineDomTreeNode *node, SlotIndex kill)
Craig Topperada08572014-04-16 04:21:27 +000095 : LR(LR), DomNode(node), Kill(kill), Value(nullptr) {}
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +000096 };
97
98 /// LiveIn - Work list of blocks where the live-in value has yet to be
99 /// determined. This list is typically computed by findReachingDefs() and
100 /// used as a work list by updateSSA(). The low-level interface may also be
101 /// used to add entries directly.
102 SmallVector<LiveInBlock, 16> LiveIn;
103
Matthias Braun11042c82015-02-18 01:50:52 +0000104 /// Assuming that @p LR is live-in to @p UseMBB, find the set of defs that can
105 /// reach it.
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000106 ///
Matthias Braun11042c82015-02-18 01:50:52 +0000107 /// If only one def can reach @p UseMBB, all paths from the def to @p UseMBB
108 /// are added to @p LR, and the function returns true.
Jakob Stoklund Olesenb3892712013-02-20 23:08:26 +0000109 ///
Matthias Braun11042c82015-02-18 01:50:52 +0000110 /// If multiple values can reach @p UseMBB, the blocks that need @p LR to be
111 /// live in are added to the LiveIn array, and the function returns false.
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000112 ///
113 /// PhysReg, when set, is used to verify live-in lists on basic blocks.
Matthias Braun11042c82015-02-18 01:50:52 +0000114 bool findReachingDefs(LiveRange &LR, MachineBasicBlock &UseMBB,
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000115 SlotIndex Kill, unsigned PhysReg);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000116
117 /// updateSSA - Compute the values that will be live in to all requested
118 /// blocks in LiveIn. Create PHI-def values as required to preserve SSA form.
119 ///
120 /// Every live-in block must be jointly dominated by the added live-out
121 /// blocks. No values are read from the live ranges.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000122 void updateSSA();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000123
Matthias Braun2f662322014-12-10 01:12:12 +0000124 /// Transfer information from the LiveIn vector to the live ranges and update
125 /// the given @p LiveOuts.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000126 void updateFromLiveIns();
127
128 /// Extend the live range of @p LR to reach all uses of Reg.
129 ///
130 /// All uses must be jointly dominated by existing liveness. PHI-defs are
131 /// inserted as needed to preserve SSA form.
132 void extendToUses(LiveRange &LR, unsigned Reg, unsigned LaneMask);
133
134 /// Reset Map and Seen fields.
135 void resetLiveOutMap();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000136
137public:
Craig Topperada08572014-04-16 04:21:27 +0000138 LiveRangeCalc() : MF(nullptr), MRI(nullptr), Indexes(nullptr),
139 DomTree(nullptr), Alloc(nullptr) {}
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000140
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000141 //===--------------------------------------------------------------------===//
142 // High-level interface.
143 //===--------------------------------------------------------------------===//
144 //
145 // Calculate live ranges from scratch.
146 //
147
148 /// reset - Prepare caches for a new set of non-overlapping live ranges. The
149 /// caches must be reset before attempting calculations with a live range
150 /// that may overlap a previously computed live range, and before the first
151 /// live range in a function. If live ranges are not known to be
152 /// non-overlapping, call reset before each.
Jakob Stoklund Olesen5ef0e0b2012-06-04 18:21:16 +0000153 void reset(const MachineFunction *MF,
154 SlotIndexes*,
155 MachineDominatorTree*,
156 VNInfo::Allocator*);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000157
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000158 //===--------------------------------------------------------------------===//
159 // Mid-level interface.
160 //===--------------------------------------------------------------------===//
161 //
162 // Modify existing live ranges.
163 //
164
Matthias Braun11042c82015-02-18 01:50:52 +0000165 /// Extend the live range of @p LR to reach @p Use.
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000166 ///
Matthias Braun11042c82015-02-18 01:50:52 +0000167 /// The existing values in @p LR must be live so they jointly dominate @p Use.
168 /// If @p Use is not dominated by a single existing value, PHI-defs are
169 /// inserted as required to preserve SSA form.
Jakob Stoklund Olesen3d604ab2012-07-13 23:39:05 +0000170 ///
171 /// PhysReg, when set, is used to verify live-in lists on basic blocks.
Matthias Braun11042c82015-02-18 01:50:52 +0000172 void extend(LiveRange &LR, SlotIndex Use, unsigned PhysReg = 0);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000173
Matthias Braunc3a72c22014-12-15 21:36:35 +0000174 /// createDeadDefs - Create a dead def in LI for every def operand of Reg.
175 /// Each instruction defining Reg gets a new VNInfo with a corresponding
176 /// minimal live range.
177 void createDeadDefs(LiveRange &LR, unsigned Reg);
178
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000179 /// Extend the live range of @p LR to reach all uses of Reg.
Matthias Braunc3a72c22014-12-15 21:36:35 +0000180 ///
181 /// All uses must be jointly dominated by existing liveness. PHI-defs are
182 /// inserted as needed to preserve SSA form.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000183 void extendToUses(LiveRange &LR, unsigned PhysReg) {
184 extendToUses(LR, PhysReg, ~0u);
185 }
Matthias Braunc3a72c22014-12-15 21:36:35 +0000186
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000187 /// Calculates liveness for the register specified in live interval @p LI.
188 /// Creates subregister live ranges as needed if subreg liveness tracking is
189 /// enabled.
190 void calculate(LiveInterval &LI);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000191
192 //===--------------------------------------------------------------------===//
193 // Low-level interface.
194 //===--------------------------------------------------------------------===//
195 //
196 // These functions can be used to compute live ranges where the live-in and
197 // live-out blocks are already known, but the SSA value in each block is
198 // unknown.
199 //
200 // After calling reset(), add known live-out values and known live-in blocks.
201 // Then call calculateValues() to compute the actual value that is
202 // live-in to each block, and add liveness to the live ranges.
203 //
204
205 /// setLiveOutValue - Indicate that VNI is live out from MBB. The
206 /// calculateValues() function will not add liveness for MBB, the caller
207 /// should take care of that.
208 ///
209 /// VNI may be null only if MBB is a live-through block also passed to
210 /// addLiveInBlock().
211 void setLiveOutValue(MachineBasicBlock *MBB, VNInfo *VNI) {
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000212 Seen.set(MBB->getNumber());
213 Map[MBB] = LiveOutPair(VNI, nullptr);
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000214 }
215
216 /// addLiveInBlock - Add a block with an unknown live-in value. This
217 /// function can only be called once per basic block. Once the live-in value
218 /// has been determined, calculateValues() will add liveness to LI.
219 ///
NAKAMURA Takumid5d16d52013-10-11 04:52:03 +0000220 /// @param LR The live range that is live-in to the block.
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000221 /// @param DomNode The domtree node for the block.
222 /// @param Kill Index in block where LI is killed. If the value is
223 /// live-through, set Kill = SLotIndex() and also call
224 /// setLiveOutValue(MBB, 0).
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000225 void addLiveInBlock(LiveRange &LR,
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000226 MachineDomTreeNode *DomNode,
227 SlotIndex Kill = SlotIndex()) {
Matthias Braun2d5c32b2013-10-10 21:28:57 +0000228 LiveIn.push_back(LiveInBlock(LR, DomNode, Kill));
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000229 }
230
231 /// calculateValues - Calculate the value that will be live-in to each block
232 /// added with addLiveInBlock. Add PHI-def values as needed to preserve SSA
233 /// form. Add liveness to all live-in blocks up to the Kill point, or the
234 /// whole block for live-through blocks.
235 ///
236 /// Every predecessor of a live-in block must have been given a value with
237 /// setLiveOutValue, the value may be null for live-trough blocks.
Matthias Braun1aed6ff2014-12-16 04:03:38 +0000238 void calculateValues();
Jakob Stoklund Olesen487f2a32011-09-13 01:34:21 +0000239};
240
241} // end namespace llvm
242
243#endif