llvm.org GIT mirror llvm / b5a457c
Extract live range calculations from SplitKit. SplitKit will soon need two copies of these data structures, and the algorithms will also be useful when LiveIntervalAnalysis becomes independent of LiveVariables. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@139572 91177308-0d34-0410-b5e6-96231b3b80d8 Jakob Stoklund Olesen 9 years ago
5 changed file(s) with 516 addition(s) and 306 deletion(s). Raw diff Collapse all Expand all
2929 LiveIntervalUnion.cpp
3030 LiveStackAnalysis.cpp
3131 LiveVariables.cpp
32 LiveRangeCalc.cpp
3233 LiveRangeEdit.cpp
3334 LocalStackSlotAllocation.cpp
3435 LowerSubregs.cpp
0 //===---- LiveRangeCalc.cpp - Calculate live ranges -----------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implementation of the LiveRangeCalc class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #define DEBUG_TYPE "regalloc"
14 #include "LiveRangeCalc.h"
15 #include "llvm/CodeGen/MachineDominators.h"
16
17 using namespace llvm;
18
19 void LiveRangeCalc::reset(const MachineFunction *MF) {
20 unsigned N = MF->getNumBlockIDs();
21 Seen.clear();
22 Seen.resize(N);
23 LiveOut.resize(N);
24 LiveIn.clear();
25 }
26
27
28 // Transfer information from the LiveIn vector to the live ranges.
29 void LiveRangeCalc::updateLiveIns(VNInfo *OverrideVNI, SlotIndexes *Indexes) {
30 for (SmallVectorImpl::iterator I = LiveIn.begin(),
31 E = LiveIn.end(); I != E; ++I) {
32 if (!I->DomNode)
33 continue;
34 MachineBasicBlock *MBB = I->DomNode->getBlock();
35
36 VNInfo *VNI = OverrideVNI ? OverrideVNI : I->Value;
37 assert(VNI && "No live-in value found");
38
39 SlotIndex Start, End;
40 tie(Start, End) = Indexes->getMBBRange(MBB);
41
42 if (I->Kill.isValid())
43 I->LI->addRange(LiveRange(Start, I->Kill, VNI));
44 else {
45 I->LI->addRange(LiveRange(Start, End, VNI));
46 // The value is live-through, update LiveOut as well. Defer the Domtree
47 // lookup until it is needed.
48 assert(Seen.test(MBB->getNumber()));
49 LiveOut[MBB] = LiveOutPair(VNI, 0);
50 }
51 }
52 LiveIn.clear();
53 }
54
55
56 void LiveRangeCalc::extend(LiveInterval *LI,
57 SlotIndex Kill,
58 SlotIndexes *Indexes,
59 MachineDominatorTree *DomTree,
60 VNInfo::Allocator *Alloc) {
61 assert(LI && "Missing live range");
62 assert(Kill.isValid() && "Invalid SlotIndex");
63 assert(Indexes && "Missing SlotIndexes");
64 assert(DomTree && "Missing dominator tree");
65 SlotIndex LastUse = Kill.getPrevSlot();
66
67 MachineBasicBlock *KillMBB = Indexes->getMBBFromIndex(LastUse);
68 assert(Kill && "No MBB at Kill");
69
70 // Is there a def in the same MBB we can extend?
71 if (LI->extendInBlock(Indexes->getMBBStartIdx(KillMBB), LastUse))
72 return;
73
74 // Find the single reaching def, or determine if Kill is jointly dominated by
75 // multiple values, and we may need to create even more phi-defs to preserve
76 // VNInfo SSA form. Perform a search for all predecessor blocks where we
77 // know the dominating VNInfo.
78 VNInfo *VNI = findReachingDefs(LI, KillMBB, Kill, Indexes, DomTree);
79
80 // When there were multiple different values, we may need new PHIs.
81 if (!VNI)
82 updateSSA(Indexes, DomTree, Alloc);
83
84 updateLiveIns(VNI, Indexes);
85 }
86
87
88 // This function is called by a client after using the low-level API to add
89 // live-out and live-in blocks. The unique value optimization is not
90 // available, SplitEditor::transferValues handles that case directly anyway.
91 void LiveRangeCalc::calculateValues(SlotIndexes *Indexes,
92 MachineDominatorTree *DomTree,
93 VNInfo::Allocator *Alloc) {
94 assert(Indexes && "Missing SlotIndexes");
95 assert(DomTree && "Missing dominator tree");
96 updateSSA(Indexes, DomTree, Alloc);
97 updateLiveIns(0, Indexes);
98 }
99
100
101 VNInfo *LiveRangeCalc::findReachingDefs(LiveInterval *LI,
102 MachineBasicBlock *KillMBB,
103 SlotIndex Kill,
104 SlotIndexes *Indexes,
105 MachineDominatorTree *DomTree) {
106 // Blocks where LI should be live-in.
107 SmallVector WorkList(1, KillMBB);
108
109 // Remember if we have seen more than one value.
110 bool UniqueVNI = true;
111 VNInfo *TheVNI = 0;
112
113 // Using Seen as a visited set, perform a BFS for all reaching defs.
114 for (unsigned i = 0; i != WorkList.size(); ++i) {
115 MachineBasicBlock *MBB = WorkList[i];
116 assert(!MBB->pred_empty() && "Value live-in to entry block?");
117 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
118 PE = MBB->pred_end(); PI != PE; ++PI) {
119 MachineBasicBlock *Pred = *PI;
120
121 // Is this a known live-out block?
122 if (Seen.test(Pred->getNumber())) {
123 if (VNInfo *VNI = LiveOut[Pred].first) {
124 if (TheVNI && TheVNI != VNI)
125 UniqueVNI = false;
126 TheVNI = VNI;
127 }
128 continue;
129 }
130
131 SlotIndex Start, End;
132 tie(Start, End) = Indexes->getMBBRange(Pred);
133
134 // First time we see Pred. Try to determine the live-out value, but set
135 // it as null if Pred is live-through with an unknown value.
136 VNInfo *VNI = LI->extendInBlock(Start, End.getPrevSlot());
137 setLiveOutValue(Pred, VNI);
138 if (VNI) {
139 if (TheVNI && TheVNI != VNI)
140 UniqueVNI = false;
141 TheVNI = VNI;
142 continue;
143 }
144
145 // No, we need a live-in value for Pred as well
146 if (Pred != KillMBB)
147 WorkList.push_back(Pred);
148 else
149 // Loopback to KillMBB, so value is really live through.
150 Kill = SlotIndex();
151 }
152 }
153
154 // Transfer WorkList to LiveInBlocks in reverse order.
155 // This ordering works best with updateSSA().
156 LiveIn.clear();
157 LiveIn.reserve(WorkList.size());
158 while(!WorkList.empty())
159 addLiveInBlock(LI, DomTree->getNode(WorkList.pop_back_val()));
160
161 // The kill block may not be live-through.
162 assert(LiveIn.back().DomNode->getBlock() == KillMBB);
163 LiveIn.back().Kill = Kill;
164
165 return UniqueVNI ? TheVNI : 0;
166 }
167
168
169 // This is essentially the same iterative algorithm that SSAUpdater uses,
170 // except we already have a dominator tree, so we don't have to recompute it.
171 void LiveRangeCalc::updateSSA(SlotIndexes *Indexes,
172 MachineDominatorTree *DomTree,
173 VNInfo::Allocator *Alloc) {
174 assert(Indexes && "Missing SlotIndexes");
175 assert(DomTree && "Missing dominator tree");
176
177 // Interate until convergence.
178 unsigned Changes;
179 do {
180 Changes = 0;
181 // Propagate live-out values down the dominator tree, inserting phi-defs
182 // when necessary.
183 for (SmallVectorImpl::iterator I = LiveIn.begin(),
184 E = LiveIn.end(); I != E; ++I) {
185 MachineDomTreeNode *Node = I->DomNode;
186 // Skip block if the live-in value has already been determined.
187 if (!Node)
188 continue;
189 MachineBasicBlock *MBB = Node->getBlock();
190 MachineDomTreeNode *IDom = Node->getIDom();
191 LiveOutPair IDomValue;
192
193 // We need a live-in value to a block with no immediate dominator?
194 // This is probably an unreachable block that has survived somehow.
195 bool needPHI = !IDom || !Seen.test(IDom->getBlock()->getNumber());
196
197 // IDom dominates all of our predecessors, but it may not be their
198 // immediate dominator. Check if any of them have live-out values that are
199 // properly dominated by IDom. If so, we need a phi-def here.
200 if (!needPHI) {
201 IDomValue = LiveOut[IDom->getBlock()];
202
203 // Cache the DomTree node that defined the value.
204 if (IDomValue.first && !IDomValue.second)
205 LiveOut[IDom->getBlock()].second = IDomValue.second =
206 DomTree->getNode(Indexes->getMBBFromIndex(IDomValue.first->def));
207
208 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
209 PE = MBB->pred_end(); PI != PE; ++PI) {
210 LiveOutPair &Value = LiveOut[*PI];
211 if (!Value.first || Value.first == IDomValue.first)
212 continue;
213
214 // Cache the DomTree node that defined the value.
215 if (!Value.second)
216 Value.second =
217 DomTree->getNode(Indexes->getMBBFromIndex(Value.first->def));
218
219 // This predecessor is carrying something other than IDomValue.
220 // It could be because IDomValue hasn't propagated yet, or it could be
221 // because MBB is in the dominance frontier of that value.
222 if (DomTree->dominates(IDom, Value.second)) {
223 needPHI = true;
224 break;
225 }
226 }
227 }
228
229 // The value may be live-through even if Kill is set, as can happen when
230 // we are called from extendRange. In that case LiveOutSeen is true, and
231 // LiveOut indicates a foreign or missing value.
232 LiveOutPair &LOP = LiveOut[MBB];
233
234 // Create a phi-def if required.
235 if (needPHI) {
236 ++Changes;
237 assert(Alloc && "Need VNInfo allocator to create PHI-defs");
238 SlotIndex Start, End;
239 tie(Start, End) = Indexes->getMBBRange(MBB);
240 VNInfo *VNI = I->LI->getNextValue(Start, 0, *Alloc);
241 VNI->setIsPHIDef(true);
242 I->Value = VNI;
243 // This block is done, we know the final value.
244 I->DomNode = 0;
245
246 // Add liveness since updateLiveIns now skips this node.
247 if (I->Kill.isValid())
248 I->LI->addRange(LiveRange(Start, I->Kill, VNI));
249 else {
250 I->LI->addRange(LiveRange(Start, End, VNI));
251 LOP = LiveOutPair(VNI, Node);
252 }
253 } else if (IDomValue.first) {
254 // No phi-def here. Remember incoming value.
255 I->Value = IDomValue.first;
256
257 // If the IDomValue is killed in the block, don't propagate through.
258 if (I->Kill.isValid())
259 continue;
260
261 // Propagate IDomValue if it isn't killed:
262 // MBB is live-out and doesn't define its own value.
263 if (LOP.first == IDomValue.first)
264 continue;
265 ++Changes;
266 LOP = IDomValue;
267 }
268 }
269 } while (Changes);
270 }
0 //===---- LiveRangeCalc.h - Calculate live ranges ---------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
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_CODEGEN_LIVERANGECALC_H
22 #define LLVM_CODEGEN_LIVERANGECALC_H
23
24 #include "llvm/ADT/BitVector.h"
25 #include "llvm/ADT/IndexedMap.h"
26 #include "llvm/CodeGen/LiveInterval.h"
27
28 namespace llvm {
29
30 /// Forward declarations for MachineDominators.h:
31 class MachineDominatorTree;
32 template class DomTreeNodeBase;
33 typedef DomTreeNodeBase MachineDomTreeNode;
34
35 class LiveRangeCalc {
36 /// Seen - Bit vector of active entries in LiveOut, also used as a visited
37 /// set by findReachingDefs. One entry per basic block, indexed by block
38 /// number. This is kept as a separate bit vector because it can be cleared
39 /// quickly when switching live ranges.
40 BitVector Seen;
41
42 /// LiveOutPair - A value and the block that defined it. The domtree node is
43 /// redundant, it can be computed as: MDT[Indexes.getMBBFromIndex(VNI->def)].
44 typedef std::pair LiveOutPair;
45
46 /// LiveOutMap - Map basic blocks to the value leaving the block.
47 typedef IndexedMap LiveOutMap;
48
49 /// LiveOut - Map each basic block where a live range is live out to the
50 /// live-out value and its defining block.
51 ///
52 /// For every basic block, MBB, one of these conditions shall be true:
53 ///
54 /// 1. !Seen.count(MBB->getNumber())
55 /// Blocks without a Seen bit are ignored.
56 /// 2. LiveOut[MBB].second.getNode() == MBB
57 /// The live-out value is defined in MBB.
58 /// 3. forall P in preds(MBB): LiveOut[P] == LiveOut[MBB]
59 /// The live-out value passses through MBB. All predecessors must carry
60 /// the same value.
61 ///
62 /// The domtree node may be null, it can be computed.
63 ///
64 /// The map can be shared by multiple live ranges as long as no two are
65 /// live-out of the same block.
66 LiveOutMap LiveOut;
67
68 /// LiveInBlock - Information about a basic block where a live range is known
69 /// to be live-in, but the value has not yet been determined.
70 struct LiveInBlock {
71 // LI - The live range that is live-in to this block. The algorithms can
72 // handle multiple non-overlapping live ranges simultaneously.
73 LiveInterval *LI;
74
75 // DomNode - Dominator tree node for the block.
76 // Cleared when the final value has been determined and LI has been updated.
77 MachineDomTreeNode *DomNode;
78
79 // Position in block where the live-in range ends, or SlotIndex() if the
80 // range passes through the block. When the final value has been
81 // determined, the range from the block start to Kill will be added to LI.
82 SlotIndex Kill;
83
84 // Live-in value filled in by updateSSA once it is known.
85 VNInfo *Value;
86
87 LiveInBlock(LiveInterval *li, MachineDomTreeNode *node, SlotIndex kill)
88 : LI(li), DomNode(node), Kill(kill), Value(0) {}
89 };
90
91 /// LiveIn - Work list of blocks where the live-in value has yet to be
92 /// determined. This list is typically computed by findReachingDefs() and
93 /// used as a work list by updateSSA(). The low-level interface may also be
94 /// used to add entries directly.
95 SmallVector LiveIn;
96
97 /// findReachingDefs - Assuming that LI is live-in to KillMBB and killed at
98 /// Kill, search for values that can reach KillMBB. All blocks that need LI
99 /// to be live-in are added to LiveIn. If a unique reaching def is found,
100 /// its value is returned, if Kill is jointly dominated by multiple values,
101 /// NULL is returned.
102 VNInfo *findReachingDefs(LiveInterval *LI,
103 MachineBasicBlock *KillMBB,
104 SlotIndex Kill,
105 SlotIndexes *Indexes,
106 MachineDominatorTree *DomTree);
107
108 /// updateSSA - Compute the values that will be live in to all requested
109 /// blocks in LiveIn. Create PHI-def values as required to preserve SSA form.
110 ///
111 /// Every live-in block must be jointly dominated by the added live-out
112 /// blocks. No values are read from the live ranges.
113 void updateSSA(SlotIndexes *Indexes,
114 MachineDominatorTree *DomTree,
115 VNInfo::Allocator *Alloc);
116
117 /// updateLiveIns - Add liveness as specified in the LiveIn vector, using VNI
118 /// as a wildcard value for LiveIn entries without a value.
119 void updateLiveIns(VNInfo *VNI, SlotIndexes*);
120
121 public:
122 //===--------------------------------------------------------------------===//
123 // High-level interface.
124 //===--------------------------------------------------------------------===//
125 //
126 // Calculate live ranges from scratch.
127 //
128
129 /// reset - Prepare caches for a new set of non-overlapping live ranges. The
130 /// caches must be reset before attempting calculations with a live range
131 /// that may overlap a previously computed live range, and before the first
132 /// live range in a function. If live ranges are not known to be
133 /// non-overlapping, call reset before each.
134 void reset(const MachineFunction *MF);
135
136 /// calculate - Calculate the live range of a virtual register from its defs
137 /// and uses. LI must be empty with no values.
138 void calculate(LiveInterval *LI,
139 MachineRegisterInfo *MRI,
140 SlotIndexes *Indexes,
141 VNInfo::Allocator *Alloc);
142
143 //===--------------------------------------------------------------------===//
144 // Mid-level interface.
145 //===--------------------------------------------------------------------===//
146 //
147 // Modify existing live ranges.
148 //
149
150 /// extend - Extend the live range of LI to reach Kill.
151 ///
152 /// The existing values in LI must be live so they jointly dominate Kill. If
153 /// Kill is not dominated by a single existing value, PHI-defs are inserted
154 /// as required to preserve SSA form. If Kill is known to be dominated by a
155 /// single existing value, Alloc may be null.
156 void extend(LiveInterval *LI,
157 SlotIndex Kill,
158 SlotIndexes *Indexes,
159 MachineDominatorTree *DomTree,
160 VNInfo::Allocator *Alloc);
161
162 /// extendToUses - Extend the live range of LI to reach all uses.
163 ///
164 /// All uses must be jointly dominated by existing liveness. PHI-defs are
165 /// inserted as needed to preserve SSA form.
166 void extendToUses(LiveInterval *LI,
167 MachineRegisterInfo *MRI,
168 SlotIndexes *Indexes,
169 MachineDominatorTree *DomTree,
170 VNInfo::Allocator *Alloc);
171
172 //===--------------------------------------------------------------------===//
173 // Low-level interface.
174 //===--------------------------------------------------------------------===//
175 //
176 // These functions can be used to compute live ranges where the live-in and
177 // live-out blocks are already known, but the SSA value in each block is
178 // unknown.
179 //
180 // After calling reset(), add known live-out values and known live-in blocks.
181 // Then call calculateValues() to compute the actual value that is
182 // live-in to each block, and add liveness to the live ranges.
183 //
184
185 /// setLiveOutValue - Indicate that VNI is live out from MBB. The
186 /// calculateValues() function will not add liveness for MBB, the caller
187 /// should take care of that.
188 ///
189 /// VNI may be null only if MBB is a live-through block also passed to
190 /// addLiveInBlock().
191 void setLiveOutValue(MachineBasicBlock *MBB, VNInfo *VNI) {
192 Seen.set(MBB->getNumber());
193 LiveOut[MBB] = LiveOutPair(VNI, 0);
194 }
195
196 /// addLiveInBlock - Add a block with an unknown live-in value. This
197 /// function can only be called once per basic block. Once the live-in value
198 /// has been determined, calculateValues() will add liveness to LI.
199 ///
200 /// @param LI The live range that is live-in to the block.
201 /// @param DomNode The domtree node for the block.
202 /// @param Kill Index in block where LI is killed. If the value is
203 /// live-through, set Kill = SLotIndex() and also call
204 /// setLiveOutValue(MBB, 0).
205 void addLiveInBlock(LiveInterval *LI,
206 MachineDomTreeNode *DomNode,
207 SlotIndex Kill = SlotIndex()) {
208 LiveIn.push_back(LiveInBlock(LI, DomNode, Kill));
209 }
210
211 /// calculateValues - Calculate the value that will be live-in to each block
212 /// added with addLiveInBlock. Add PHI-def values as needed to preserve SSA
213 /// form. Add liveness to all live-in blocks up to the Kill point, or the
214 /// whole block for live-through blocks.
215 ///
216 /// Every predecessor of a live-in block must have been given a value with
217 /// setLiveOutValue, the value may be null for live-trough blocks.
218 void calculateValues(SlotIndexes *Indexes,
219 MachineDominatorTree *DomTree,
220 VNInfo::Allocator *Alloc);
221 };
222
223 } // end namespace llvm
224
225 #endif
318318 OpenIdx = 0;
319319 RegAssign.clear();
320320 Values.clear();
321
322 // We don't need to clear LiveOutCache, only LiveOutSeen entries are read.
323 LiveOutSeen.clear();
321 LRCalc.reset(&VRM.getMachineFunction());
324322
325323 // We don't need an AliasAnalysis since we will only be performing
326324 // cheap-as-a-copy remats anyway.
391389 // extendRange - Extend the live range to reach Idx.
392390 // Potentially create phi-def values.
393391 void SplitEditor::extendRange(unsigned RegIdx, SlotIndex Idx) {
394 assert(Idx.isValid() && "Invalid SlotIndex");
395 MachineBasicBlock *IdxMBB = LIS.getMBBFromIndex(Idx);
396 assert(IdxMBB && "No MBB at Idx");
397 LiveInterval *LI = Edit->get(RegIdx);
398
399 // Is there a def in the same MBB we can extend?
400 if (LI->extendInBlock(LIS.getMBBStartIdx(IdxMBB), Idx))
401 return;
402
403 // Now for the fun part. We know that ParentVNI potentially has multiple defs,
404 // and we may need to create even more phi-defs to preserve VNInfo SSA form.
405 // Perform a search for all predecessor blocks where we know the dominating
406 // VNInfo.
407 VNInfo *VNI = findReachingDefs(LI, IdxMBB, Idx.getNextSlot());
408
409 // When there were multiple different values, we may need new PHIs.
410 if (!VNI)
411 return updateSSA();
412
413 // Poor man's SSA update for the single-value case.
414 LiveOutPair LOP(VNI, MDT[LIS.getMBBFromIndex(VNI->def)]);
415 for (SmallVectorImpl::iterator I = LiveInBlocks.begin(),
416 E = LiveInBlocks.end(); I != E; ++I) {
417 MachineBasicBlock *MBB = I->DomNode->getBlock();
418 SlotIndex Start = LIS.getMBBStartIdx(MBB);
419 if (I->Kill.isValid())
420 LI->addRange(LiveRange(Start, I->Kill, VNI));
421 else {
422 LiveOutCache[MBB] = LOP;
423 LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI));
424 }
425 }
426 }
427
428 /// findReachingDefs - Search the CFG for known live-out values.
429 /// Add required live-in blocks to LiveInBlocks.
430 VNInfo *SplitEditor::findReachingDefs(LiveInterval *LI,
431 MachineBasicBlock *KillMBB,
432 SlotIndex Kill) {
433 // Initialize the live-out cache the first time it is needed.
434 if (LiveOutSeen.empty()) {
435 unsigned N = VRM.getMachineFunction().getNumBlockIDs();
436 LiveOutSeen.resize(N);
437 LiveOutCache.resize(N);
438 }
439
440 // Blocks where LI should be live-in.
441 SmallVector WorkList(1, KillMBB);
442
443 // Remember if we have seen more than one value.
444 bool UniqueVNI = true;
445 VNInfo *TheVNI = 0;
446
447 // Using LiveOutCache as a visited set, perform a BFS for all reaching defs.
448 for (unsigned i = 0; i != WorkList.size(); ++i) {
449 MachineBasicBlock *MBB = WorkList[i];
450 assert(!MBB->pred_empty() && "Value live-in to entry block?");
451 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
452 PE = MBB->pred_end(); PI != PE; ++PI) {
453 MachineBasicBlock *Pred = *PI;
454 LiveOutPair &LOP = LiveOutCache[Pred];
455
456 // Is this a known live-out block?
457 if (LiveOutSeen.test(Pred->getNumber())) {
458 if (VNInfo *VNI = LOP.first) {
459 if (TheVNI && TheVNI != VNI)
460 UniqueVNI = false;
461 TheVNI = VNI;
462 }
463 continue;
464 }
465
466 // First time. LOP is garbage and must be cleared below.
467 LiveOutSeen.set(Pred->getNumber());
468
469 // Does Pred provide a live-out value?
470 SlotIndex Start, Last;
471 tie(Start, Last) = LIS.getSlotIndexes()->getMBBRange(Pred);
472 Last = Last.getPrevSlot();
473 VNInfo *VNI = LI->extendInBlock(Start, Last);
474 LOP.first = VNI;
475 if (VNI) {
476 LOP.second = MDT[LIS.getMBBFromIndex(VNI->def)];
477 if (TheVNI && TheVNI != VNI)
478 UniqueVNI = false;
479 TheVNI = VNI;
480 continue;
481 }
482 LOP.second = 0;
483
484 // No, we need a live-in value for Pred as well
485 if (Pred != KillMBB)
486 WorkList.push_back(Pred);
487 else
488 // Loopback to KillMBB, so value is really live through.
489 Kill = SlotIndex();
490 }
491 }
492
493 // Transfer WorkList to LiveInBlocks in reverse order.
494 // This ordering works best with updateSSA().
495 LiveInBlocks.clear();
496 LiveInBlocks.reserve(WorkList.size());
497 while(!WorkList.empty())
498 LiveInBlocks.push_back(MDT[WorkList.pop_back_val()]);
499
500 // The kill block may not be live-through.
501 assert(LiveInBlocks.back().DomNode->getBlock() == KillMBB);
502 LiveInBlocks.back().Kill = Kill;
503
504 return UniqueVNI ? TheVNI : 0;
505 }
506
507 void SplitEditor::updateSSA() {
508 // This is essentially the same iterative algorithm that SSAUpdater uses,
509 // except we already have a dominator tree, so we don't have to recompute it.
510 unsigned Changes;
511 do {
512 Changes = 0;
513 // Propagate live-out values down the dominator tree, inserting phi-defs
514 // when necessary.
515 for (SmallVectorImpl::iterator I = LiveInBlocks.begin(),
516 E = LiveInBlocks.end(); I != E; ++I) {
517 MachineDomTreeNode *Node = I->DomNode;
518 // Skip block if the live-in value has already been determined.
519 if (!Node)
520 continue;
521 MachineBasicBlock *MBB = Node->getBlock();
522 MachineDomTreeNode *IDom = Node->getIDom();
523 LiveOutPair IDomValue;
524
525 // We need a live-in value to a block with no immediate dominator?
526 // This is probably an unreachable block that has survived somehow.
527 bool needPHI = !IDom || !LiveOutSeen.test(IDom->getBlock()->getNumber());
528
529 // IDom dominates all of our predecessors, but it may not be their
530 // immediate dominator. Check if any of them have live-out values that are
531 // properly dominated by IDom. If so, we need a phi-def here.
532 if (!needPHI) {
533 IDomValue = LiveOutCache[IDom->getBlock()];
534 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
535 PE = MBB->pred_end(); PI != PE; ++PI) {
536 LiveOutPair Value = LiveOutCache[*PI];
537 if (!Value.first || Value.first == IDomValue.first)
538 continue;
539 // This predecessor is carrying something other than IDomValue.
540 // It could be because IDomValue hasn't propagated yet, or it could be
541 // because MBB is in the dominance frontier of that value.
542 if (MDT.dominates(IDom, Value.second)) {
543 needPHI = true;
544 break;
545 }
546 }
547 }
548
549 // The value may be live-through even if Kill is set, as can happen when
550 // we are called from extendRange. In that case LiveOutSeen is true, and
551 // LiveOutCache indicates a foreign or missing value.
552 LiveOutPair &LOP = LiveOutCache[MBB];
553
554 // Create a phi-def if required.
555 if (needPHI) {
556 ++Changes;
557 SlotIndex Start = LIS.getMBBStartIdx(MBB);
558 unsigned RegIdx = RegAssign.lookup(Start);
559 LiveInterval *LI = Edit->get(RegIdx);
560 VNInfo *VNI = LI->getNextValue(Start, 0, LIS.getVNInfoAllocator());
561 VNI->setIsPHIDef(true);
562 I->Value = VNI;
563 // This block is done, we know the final value.
564 I->DomNode = 0;
565 if (I->Kill.isValid())
566 LI->addRange(LiveRange(Start, I->Kill, VNI));
567 else {
568 LI->addRange(LiveRange(Start, LIS.getMBBEndIdx(MBB), VNI));
569 LOP = LiveOutPair(VNI, Node);
570 }
571 } else if (IDomValue.first) {
572 // No phi-def here. Remember incoming value.
573 I->Value = IDomValue.first;
574 if (I->Kill.isValid())
575 continue;
576 // Propagate IDomValue if needed:
577 // MBB is live-out and doesn't define its own value.
578 if (LOP.second != Node && LOP.first != IDomValue.first) {
579 ++Changes;
580 LOP = IDomValue;
581 }
582 }
583 }
584 } while (Changes);
585
586 // The values in LiveInBlocks are now accurate. No more phi-defs are needed
587 // for these blocks, so we can color the live ranges.
588 for (SmallVectorImpl::iterator I = LiveInBlocks.begin(),
589 E = LiveInBlocks.end(); I != E; ++I) {
590 if (!I->DomNode)
591 continue;
592 assert(I->Value && "No live-in value found");
593 MachineBasicBlock *MBB = I->DomNode->getBlock();
594 SlotIndex Start = LIS.getMBBStartIdx(MBB);
595 unsigned RegIdx = RegAssign.lookup(Start);
596 LiveInterval *LI = Edit->get(RegIdx);
597 LI->addRange(LiveRange(Start, I->Kill.isValid() ?
598 I->Kill : LIS.getMBBEndIdx(MBB), I->Value));
599 }
392 LRCalc.extend(Edit->get(RegIdx), Idx.getNextSlot(),
393 LIS.getSlotIndexes(), &MDT, &LIS.getVNInfoAllocator());
600394 }
601395
602396 VNInfo *SplitEditor::defFromParent(unsigned RegIdx,
793587 /// Values that were rematerialized are left alone, they need extendRange().
794588 bool SplitEditor::transferValues() {
795589 bool Skipped = false;
796 LiveInBlocks.clear();
797590 RegAssignMap::const_iterator AssignI = RegAssign.begin();
798591 for (LiveInterval::const_iterator ParentI = Edit->getParent().begin(),
799592 ParentE = Edit->getParent().end(); ParentI != ParentE; ++ParentI) {
839632 continue;
840633 }
841634
842 // Initialize the live-out cache the first time it is needed.
843 if (LiveOutSeen.empty()) {
844 unsigned N = VRM.getMachineFunction().getNumBlockIDs();
845 LiveOutSeen.resize(N);
846 LiveOutCache.resize(N);
847 }
848
849635 // This value has multiple defs in RegIdx, but it wasn't rematerialized,
850636 // so the live range is accurate. Add live-in blocks in [Start;End) to the
851637 // LiveInBlocks.
860646 assert(VNI && "Missing def for complex mapped value");
861647 DEBUG(dbgs() << ':' << VNI->id << "*BB#" << MBB->getNumber());
862648 // MBB has its own def. Is it also live-out?
863 if (BlockEnd <= End) {
864 LiveOutSeen.set(MBB->getNumber());
865 LiveOutCache[MBB] = LiveOutPair(VNI, MDT[MBB]);
866 }
649 if (BlockEnd <= End)
650 LRCalc.setLiveOutValue(MBB, VNI);
651
867652 // Skip to the next block for live-in.
868653 ++MBB;
869654 BlockStart = BlockEnd;
880665 VNInfo *VNI = LI->extendInBlock(BlockStart,
881666 std::min(BlockEnd, End).getPrevSlot());
882667 assert(VNI && "Missing def for complex mapped parent PHI");
883 if (End >= BlockEnd) {
884 // Live-out as well.
885 LiveOutSeen.set(MBB->getNumber());
886 LiveOutCache[MBB] = LiveOutPair(VNI, MDT[MBB]);
887 }
668 if (End >= BlockEnd)
669 LRCalc.setLiveOutValue(MBB, VNI); // Live-out as well.
888670 } else {
889 // This block needs a live-in value.
890 LiveInBlocks.push_back(MDT[MBB]);
891 // The last block covered may not be live-out.
671 // This block needs a live-in value. The last block covered may not
672 // be live-out.
892673 if (End < BlockEnd)
893 LiveInBlocks.back().Kill = End;
674 LRCalc.addLiveInBlock(LI, MDT[MBB], End);
894675 else {
895 // Live-out, but we need updateSSA to tell us the value.
896 LiveOutSeen.set(MBB->getNumber());
897 LiveOutCache[MBB] = LiveOutPair((VNInfo*)0,
898 (MachineDomTreeNode*)0);
676 // Live-through, and we don't know the value.
677 LRCalc.addLiveInBlock(LI, MDT[MBB]);
678 LRCalc.setLiveOutValue(MBB, 0);
899679 }
900680 }
901681 BlockStart = BlockEnd;
906686 DEBUG(dbgs() << '\n');
907687 }
908688
909 if (!LiveInBlocks.empty())
910 updateSSA();
689 LRCalc.calculateValues(LIS.getSlotIndexes(), &MDT, &LIS.getVNInfoAllocator());
911690
912691 return Skipped;
913692 }
1414 #ifndef LLVM_CODEGEN_SPLITKIT_H
1515 #define LLVM_CODEGEN_SPLITKIT_H
1616
17 #include "LiveRangeCalc.h"
1718 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/BitVector.h"
1919 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/IndexedMap.h"
2120 #include "llvm/ADT/IntervalMap.h"
2221 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/CodeGen/SlotIndexes.h"
2422
2523 namespace llvm {
2624
3634 class VirtRegMap;
3735 class VNInfo;
3836 class raw_ostream;
39
40 /// At some point we should just include MachineDominators.h:
41 class MachineDominatorTree;
42 template class DomTreeNodeBase;
43 typedef DomTreeNodeBase MachineDomTreeNode;
44
4537
4638 /// SplitAnalysis - Analyze a LiveInterval, looking for live range splitting
4739 /// opportunities.
280272 /// The new value has no live ranges anywhere.
281273 ValueMap Values;
282274
283 typedef std::pair LiveOutPair;
284 typedef IndexedMap LiveOutMap;
285
286 // LiveOutCache - Map each basic block where a new register is live out to the
287 // live-out value and its defining block.
288 // One of these conditions shall be true:
289 //
290 // 1. !LiveOutSeen.count(MBB->getNumber())
291 // 2. LiveOutCache[MBB].second.getNode() == MBB
292 // 3. forall P in preds(MBB): LiveOutCache[P] == LiveOutCache[MBB]
293 //
294 // This is only a cache, the values can be computed as:
295 //
296 // VNI = Edit.get(RegIdx)->getVNInfoAt(LIS.getMBBEndIdx(MBB))
297 // Node = mbt_[LIS.getMBBFromIndex(VNI->def)]
298 //
299 // The cache can be shared by all the new registers because at most one is
300 // live out of each block.
301 LiveOutMap LiveOutCache;
302
303 // LiveOutSeen - Indexed by MBB->getNumber(), a bit is set for each valid
304 // entry in LiveOutCache. This is also used as a visited set for
305 // findReachingDefs().
306 BitVector LiveOutSeen;
307
308 /// LiveInBlock - Info for updateSSA() about a block where a register is
309 /// live-in.
310 /// The updateSSA caller provides DomNode and Kill inside MBB, updateSSA()
311 /// adds the computed live-in value.
312 struct LiveInBlock {
313 // Dominator tree node for the block.
314 // Cleared by updateSSA when the final value has been determined.
315 MachineDomTreeNode *DomNode;
316
317 // Live-in value filled in by updateSSA once it is known.
318 VNInfo *Value;
319
320 // Position in block where the live-in range ends, or SlotIndex() if the
321 // range passes through the block.
322 SlotIndex Kill;
323
324 LiveInBlock(MachineDomTreeNode *node) : DomNode(node), Value(0) {}
325 };
326
327 /// LiveInBlocks - List of live-in blocks used by findReachingDefs() and
328 /// updateSSA(). This list is usually empty, it exists here to avoid frequent
329 /// reallocations.
330 SmallVector LiveInBlocks;
275 /// LRCalc - Cache for computing live ranges and SSA update.
276 LiveRangeCalc LRCalc;
331277
332278 /// defValue - define a value in RegIdx from ParentVNI at Idx.
333279 /// Idx does not have to be ParentVNI->def, but it must be contained within
351297 /// extendRange - Extend the live range of Edit.get(RegIdx) so it reaches Idx.
352298 /// Insert PHIDefs as needed to preserve SSA form.
353299 void extendRange(unsigned RegIdx, SlotIndex Idx);
354
355 /// findReachingDefs - Starting from MBB, add blocks to LiveInBlocks until all
356 /// reaching defs for LI are found.
357 /// @param LI Live interval whose value is needed.
358 /// @param MBB Block where LI should be live-in.
359 /// @param Kill Kill point in MBB.
360 /// @return Unique value seen, or NULL.
361 VNInfo *findReachingDefs(LiveInterval *LI, MachineBasicBlock *MBB,
362 SlotIndex Kill);
363
364 /// updateSSA - Compute and insert PHIDefs such that all blocks in
365 // LiveInBlocks get a known live-in value. Add live ranges to the blocks.
366 void updateSSA();
367300
368301 /// transferValues - Transfer values to the new ranges.
369302 /// Return true if any ranges were skipped.