1 //===---- LiveRangeCalc.cpp - Calculate live ranges -----------------------===//
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 // Implementation of the LiveRangeCalc class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "regalloc"
15 #include "LiveRangeCalc.h"
16 #include "llvm/CodeGen/MachineDominators.h"
17
18 using namespace llvm;
19
reset(const MachineFunction * MF)20 void LiveRangeCalc::reset(const MachineFunction *MF) {
21 unsigned N = MF->getNumBlockIDs();
22 Seen.clear();
23 Seen.resize(N);
24 LiveOut.resize(N);
25 LiveIn.clear();
26 }
27
28
29 // Transfer information from the LiveIn vector to the live ranges.
updateLiveIns(VNInfo * OverrideVNI,SlotIndexes * Indexes)30 void LiveRangeCalc::updateLiveIns(VNInfo *OverrideVNI, SlotIndexes *Indexes) {
31 for (SmallVectorImpl<LiveInBlock>::iterator I = LiveIn.begin(),
32 E = LiveIn.end(); I != E; ++I) {
33 if (!I->DomNode)
34 continue;
35 MachineBasicBlock *MBB = I->DomNode->getBlock();
36
37 VNInfo *VNI = OverrideVNI ? OverrideVNI : I->Value;
38 assert(VNI && "No live-in value found");
39
40 SlotIndex Start, End;
41 tie(Start, End) = Indexes->getMBBRange(MBB);
42
43 if (I->Kill.isValid())
44 I->LI->addRange(LiveRange(Start, I->Kill, VNI));
45 else {
46 I->LI->addRange(LiveRange(Start, End, VNI));
47 // The value is live-through, update LiveOut as well. Defer the Domtree
48 // lookup until it is needed.
49 assert(Seen.test(MBB->getNumber()));
50 LiveOut[MBB] = LiveOutPair(VNI, (MachineDomTreeNode *)0);
51 }
52 }
53 LiveIn.clear();
54 }
55
56
extend(LiveInterval * LI,SlotIndex Kill,SlotIndexes * Indexes,MachineDominatorTree * DomTree,VNInfo::Allocator * Alloc)57 void LiveRangeCalc::extend(LiveInterval *LI,
58 SlotIndex Kill,
59 SlotIndexes *Indexes,
60 MachineDominatorTree *DomTree,
61 VNInfo::Allocator *Alloc) {
62 assert(LI && "Missing live range");
63 assert(Kill.isValid() && "Invalid SlotIndex");
64 assert(Indexes && "Missing SlotIndexes");
65 assert(DomTree && "Missing dominator tree");
66
67 MachineBasicBlock *KillMBB = Indexes->getMBBFromIndex(Kill.getPrevSlot());
68 assert(KillMBB && "No MBB at Kill");
69
70 // Is there a def in the same MBB we can extend?
71 if (LI->extendInBlock(Indexes->getMBBStartIdx(KillMBB), Kill))
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.
calculateValues(SlotIndexes * Indexes,MachineDominatorTree * DomTree,VNInfo::Allocator * Alloc)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
findReachingDefs(LiveInterval * LI,MachineBasicBlock * KillMBB,SlotIndex Kill,SlotIndexes * Indexes,MachineDominatorTree * DomTree)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<MachineBasicBlock*, 16> 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);
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.
updateSSA(SlotIndexes * Indexes,MachineDominatorTree * DomTree,VNInfo::Allocator * Alloc)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<LiveInBlock>::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, *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 }
271