1 //===---- LiveRangeCalc.h - Calculate live ranges ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
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.
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_CODEGEN_LIVERANGECALC_H
23 #define LLVM_CODEGEN_LIVERANGECALC_H
25 #include "llvm/ADT/BitVector.h"
26 #include "llvm/ADT/IndexedMap.h"
27 #include "llvm/CodeGen/LiveInterval.h"
31 /// Forward declarations for MachineDominators.h:
32 class MachineDominatorTree;
33 template <class NodeT> class DomTreeNodeBase;
34 typedef DomTreeNodeBase<MachineBasicBlock> MachineDomTreeNode;
37 /// Seen - Bit vector of active entries in LiveOut, also used as a visited
38 /// set by findReachingDefs. One entry per basic block, indexed by block
39 /// number. This is kept as a separate bit vector because it can be cleared
40 /// quickly when switching live ranges.
43 /// 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;
47 /// LiveOutMap - Map basic blocks to the value leaving the block.
48 typedef IndexedMap<LiveOutPair, MBB2NumberFunctor> LiveOutMap;
50 /// LiveOut - Map each basic block where a live range is live out to the
51 /// live-out value and its defining block.
53 /// For every basic block, MBB, one of these conditions shall be true:
55 /// 1. !Seen.count(MBB->getNumber())
56 /// Blocks without a Seen bit are ignored.
57 /// 2. LiveOut[MBB].second.getNode() == MBB
58 /// The live-out value is defined in MBB.
59 /// 3. forall P in preds(MBB): LiveOut[P] == LiveOut[MBB]
60 /// The live-out value passses through MBB. All predecessors must carry
63 /// The domtree node may be null, it can be computed.
65 /// The map can be shared by multiple live ranges as long as no two are
66 /// live-out of the same block.
69 /// LiveInBlock - Information about a basic block where a live range is known
70 /// to be live-in, but the value has not yet been determined.
72 // LI - The live range that is live-in to this block. The algorithms can
73 // handle multiple non-overlapping live ranges simultaneously.
76 // DomNode - Dominator tree node for the block.
77 // Cleared when the final value has been determined and LI has been updated.
78 MachineDomTreeNode *DomNode;
80 // Position in block where the live-in range ends, or SlotIndex() if the
81 // range passes through the block. When the final value has been
82 // determined, the range from the block start to Kill will be added to LI.
85 // Live-in value filled in by updateSSA once it is known.
88 LiveInBlock(LiveInterval *li, MachineDomTreeNode *node, SlotIndex kill)
89 : LI(li), DomNode(node), Kill(kill), Value(0) {}
92 /// LiveIn - Work list of blocks where the live-in value has yet to be
93 /// determined. This list is typically computed by findReachingDefs() and
94 /// used as a work list by updateSSA(). The low-level interface may also be
95 /// used to add entries directly.
96 SmallVector<LiveInBlock, 16> LiveIn;
98 /// findReachingDefs - Assuming that LI is live-in to KillMBB and killed at
99 /// Kill, search for values that can reach KillMBB. All blocks that need LI
100 /// to be live-in are added to LiveIn. If a unique reaching def is found,
101 /// its value is returned, if Kill is jointly dominated by multiple values,
102 /// NULL is returned.
103 VNInfo *findReachingDefs(LiveInterval *LI,
104 MachineBasicBlock *KillMBB,
106 SlotIndexes *Indexes,
107 MachineDominatorTree *DomTree);
109 /// updateSSA - Compute the values that will be live in to all requested
110 /// blocks in LiveIn. Create PHI-def values as required to preserve SSA form.
112 /// Every live-in block must be jointly dominated by the added live-out
113 /// blocks. No values are read from the live ranges.
114 void updateSSA(SlotIndexes *Indexes,
115 MachineDominatorTree *DomTree,
116 VNInfo::Allocator *Alloc);
118 /// updateLiveIns - Add liveness as specified in the LiveIn vector, using VNI
119 /// as a wildcard value for LiveIn entries without a value.
120 void updateLiveIns(VNInfo *VNI, SlotIndexes*);
123 //===--------------------------------------------------------------------===//
124 // High-level interface.
125 //===--------------------------------------------------------------------===//
127 // Calculate live ranges from scratch.
130 /// reset - Prepare caches for a new set of non-overlapping live ranges. The
131 /// caches must be reset before attempting calculations with a live range
132 /// that may overlap a previously computed live range, and before the first
133 /// live range in a function. If live ranges are not known to be
134 /// non-overlapping, call reset before each.
135 void reset(const MachineFunction *MF);
137 /// calculate - Calculate the live range of a virtual register from its defs
138 /// and uses. LI must be empty with no values.
139 void calculate(LiveInterval *LI,
140 MachineRegisterInfo *MRI,
141 SlotIndexes *Indexes,
142 VNInfo::Allocator *Alloc);
144 //===--------------------------------------------------------------------===//
145 // Mid-level interface.
146 //===--------------------------------------------------------------------===//
148 // Modify existing live ranges.
151 /// extend - Extend the live range of LI to reach Kill.
153 /// The existing values in LI must be live so they jointly dominate Kill. If
154 /// Kill is not dominated by a single existing value, PHI-defs are inserted
155 /// as required to preserve SSA form. If Kill is known to be dominated by a
156 /// single existing value, Alloc may be null.
157 void extend(LiveInterval *LI,
159 SlotIndexes *Indexes,
160 MachineDominatorTree *DomTree,
161 VNInfo::Allocator *Alloc);
163 /// extendToUses - Extend the live range of LI to reach all uses.
165 /// All uses must be jointly dominated by existing liveness. PHI-defs are
166 /// inserted as needed to preserve SSA form.
167 void extendToUses(LiveInterval *LI,
168 MachineRegisterInfo *MRI,
169 SlotIndexes *Indexes,
170 MachineDominatorTree *DomTree,
171 VNInfo::Allocator *Alloc);
173 //===--------------------------------------------------------------------===//
174 // Low-level interface.
175 //===--------------------------------------------------------------------===//
177 // These functions can be used to compute live ranges where the live-in and
178 // live-out blocks are already known, but the SSA value in each block is
181 // After calling reset(), add known live-out values and known live-in blocks.
182 // Then call calculateValues() to compute the actual value that is
183 // live-in to each block, and add liveness to the live ranges.
186 /// setLiveOutValue - Indicate that VNI is live out from MBB. The
187 /// calculateValues() function will not add liveness for MBB, the caller
188 /// should take care of that.
190 /// VNI may be null only if MBB is a live-through block also passed to
191 /// addLiveInBlock().
192 void setLiveOutValue(MachineBasicBlock *MBB, VNInfo *VNI) {
193 Seen.set(MBB->getNumber());
194 LiveOut[MBB] = LiveOutPair(VNI, (MachineDomTreeNode *)0);
197 /// addLiveInBlock - Add a block with an unknown live-in value. This
198 /// function can only be called once per basic block. Once the live-in value
199 /// has been determined, calculateValues() will add liveness to LI.
201 /// @param LI The live range that is live-in to the block.
202 /// @param DomNode The domtree node for the block.
203 /// @param Kill Index in block where LI is killed. If the value is
204 /// live-through, set Kill = SLotIndex() and also call
205 /// setLiveOutValue(MBB, 0).
206 void addLiveInBlock(LiveInterval *LI,
207 MachineDomTreeNode *DomNode,
208 SlotIndex Kill = SlotIndex()) {
209 LiveIn.push_back(LiveInBlock(LI, DomNode, Kill));
212 /// calculateValues - Calculate the value that will be live-in to each block
213 /// added with addLiveInBlock. Add PHI-def values as needed to preserve SSA
214 /// form. Add liveness to all live-in blocks up to the Kill point, or the
215 /// whole block for live-through blocks.
217 /// Every predecessor of a live-in block must have been given a value with
218 /// setLiveOutValue, the value may be null for live-trough blocks.
219 void calculateValues(SlotIndexes *Indexes,
220 MachineDominatorTree *DomTree,
221 VNInfo::Allocator *Alloc);
224 } // end namespace llvm