blob: 7e1c39df9b8804c4489e7b873b8656f0cc155e88 [file] [log] [blame]
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001//===--- RDFLiveness.cpp --------------------------------------------------===//
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// Computation of the liveness information from the data-flow graph.
11//
12// The main functionality of this code is to compute block live-in
13// information. With the live-in information in place, the placement
14// of kill flags can also be recalculated.
15//
16// The block live-in calculation is based on the ideas from the following
17// publication:
18//
19// Dibyendu Das, Ramakrishna Upadrasta, Benoit Dupont de Dinechin.
20// "Efficient Liveness Computation Using Merge Sets and DJ-Graphs."
21// ACM Transactions on Architecture and Code Optimization, Association for
22// Computing Machinery, 2012, ACM TACO Special Issue on "High-Performance
23// and Embedded Architectures and Compilers", 8 (4),
24// <10.1145/2086696.2086706>. <hal-00647369>
25//
26#include "RDFGraph.h"
27#include "RDFLiveness.h"
28#include "llvm/ADT/SetVector.h"
29#include "llvm/CodeGen/MachineBasicBlock.h"
30#include "llvm/CodeGen/MachineDominanceFrontier.h"
31#include "llvm/CodeGen/MachineDominators.h"
32#include "llvm/CodeGen/MachineFunction.h"
33#include "llvm/CodeGen/MachineRegisterInfo.h"
34#include "llvm/Target/TargetRegisterInfo.h"
35
36using namespace llvm;
37using namespace rdf;
38
Benjamin Kramer922efd72016-05-27 10:06:40 +000039namespace llvm {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +000040namespace rdf {
41 template<>
42 raw_ostream &operator<< (raw_ostream &OS, const Print<Liveness::RefMap> &P) {
43 OS << '{';
44 for (auto I : P.Obj) {
45 OS << ' ' << Print<RegisterRef>(I.first, P.G) << '{';
46 for (auto J = I.second.begin(), E = I.second.end(); J != E; ) {
47 OS << Print<NodeId>(*J, P.G);
48 if (++J != E)
49 OS << ',';
50 }
51 OS << '}';
52 }
53 OS << " }";
54 return OS;
55 }
Benjamin Kramer922efd72016-05-27 10:06:40 +000056} // namespace rdf
57} // namespace llvm
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +000058
59// The order in the returned sequence is the order of reaching defs in the
60// upward traversal: the first def is the closest to the given reference RefA,
61// the next one is further up, and so on.
62// The list ends at a reaching phi def, or when the reference from RefA is
63// covered by the defs in the list (see FullChain).
64// This function provides two modes of operation:
65// (1) Returning the sequence of reaching defs for a particular reference
66// node. This sequence will terminate at the first phi node [1].
67// (2) Returning a partial sequence of reaching defs, where the final goal
68// is to traverse past phi nodes to the actual defs arising from the code
69// itself.
70// In mode (2), the register reference for which the search was started
71// may be different from the reference node RefA, for which this call was
72// made, hence the argument RefRR, which holds the original register.
73// Also, some definitions may have already been encountered in a previous
74// call that will influence register covering. The register references
75// already defined are passed in through DefRRs.
76// In mode (1), the "continuation" considerations do not apply, and the
77// RefRR is the same as the register in RefA, and the set DefRRs is empty.
78//
79// [1] It is possible for multiple phi nodes to be included in the returned
80// sequence:
81// SubA = phi ...
82// SubB = phi ...
83// ... = SuperAB(rdef:SubA), SuperAB"(rdef:SubB)
84// However, these phi nodes are independent from one another in terms of
85// the data-flow.
86
87NodeList Liveness::getAllReachingDefs(RegisterRef RefRR,
88 NodeAddr<RefNode*> RefA, bool FullChain, const RegisterSet &DefRRs) {
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +000089 NodeList RDefs; // Return value.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +000090 SetVector<NodeId> DefQ;
91 SetVector<NodeId> Owners;
92
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +000093 // Dead defs will be treated as if they were live, since they are actually
94 // on the data-flow path. They cannot be ignored because even though they
95 // do not generate meaningful values, they still modify registers.
96
97 // If the reference is undefined, there is nothing to do.
98 if (RefA.Addr->getFlags() & NodeAttrs::Undef)
99 return RDefs;
100
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000101 // The initial queue should not have reaching defs for shadows. The
102 // whole point of a shadow is that it will have a reaching def that
103 // is not aliased to the reaching defs of the related shadows.
104 NodeId Start = RefA.Id;
105 auto SNA = DFG.addr<RefNode*>(Start);
106 if (NodeId RD = SNA.Addr->getReachingDef())
107 DefQ.insert(RD);
108
109 // Collect all the reaching defs, going up until a phi node is encountered,
110 // or there are no more reaching defs. From this set, the actual set of
111 // reaching defs will be selected.
112 // The traversal upwards must go on until a covering def is encountered.
113 // It is possible that a collection of non-covering (individually) defs
114 // will be sufficient, but keep going until a covering one is found.
115 for (unsigned i = 0; i < DefQ.size(); ++i) {
116 auto TA = DFG.addr<DefNode*>(DefQ[i]);
117 if (TA.Addr->getFlags() & NodeAttrs::PhiRef)
118 continue;
119 // Stop at the covering/overwriting def of the initial register reference.
120 RegisterRef RR = TA.Addr->getRegRef();
Krzysztof Parzyszek29e93f32016-09-22 21:01:24 +0000121 if (!DFG.IsPreservingDef(TA) && RAI.covers(RR, RefRR, DFG))
Krzysztof Parzyszeka1218722016-09-08 20:48:42 +0000122 continue;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000123 // Get the next level of reaching defs. This will include multiple
124 // reaching defs for shadows.
125 for (auto S : DFG.getRelatedRefs(TA.Addr->getOwner(DFG), TA))
126 if (auto RD = NodeAddr<RefNode*>(S).Addr->getReachingDef())
127 DefQ.insert(RD);
128 }
129
130 // Remove all non-phi defs that are not aliased to RefRR, and collect
131 // the owners of the remaining defs.
132 SetVector<NodeId> Defs;
133 for (auto N : DefQ) {
134 auto TA = DFG.addr<DefNode*>(N);
135 bool IsPhi = TA.Addr->getFlags() & NodeAttrs::PhiRef;
Krzysztof Parzyszek29e93f32016-09-22 21:01:24 +0000136 if (!IsPhi && !RAI.alias(RefRR, TA.Addr->getRegRef(), DFG))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000137 continue;
138 Defs.insert(TA.Id);
139 Owners.insert(TA.Addr->getOwner(DFG).Id);
140 }
141
142 // Return the MachineBasicBlock containing a given instruction.
143 auto Block = [this] (NodeAddr<InstrNode*> IA) -> MachineBasicBlock* {
144 if (IA.Addr->getKind() == NodeAttrs::Stmt)
145 return NodeAddr<StmtNode*>(IA).Addr->getCode()->getParent();
146 assert(IA.Addr->getKind() == NodeAttrs::Phi);
147 NodeAddr<PhiNode*> PA = IA;
148 NodeAddr<BlockNode*> BA = PA.Addr->getOwner(DFG);
149 return BA.Addr->getCode();
150 };
151 // Less(A,B) iff instruction A is further down in the dominator tree than B.
152 auto Less = [&Block,this] (NodeId A, NodeId B) -> bool {
153 if (A == B)
154 return false;
155 auto OA = DFG.addr<InstrNode*>(A), OB = DFG.addr<InstrNode*>(B);
156 MachineBasicBlock *BA = Block(OA), *BB = Block(OB);
157 if (BA != BB)
158 return MDT.dominates(BB, BA);
159 // They are in the same block.
160 bool StmtA = OA.Addr->getKind() == NodeAttrs::Stmt;
161 bool StmtB = OB.Addr->getKind() == NodeAttrs::Stmt;
162 if (StmtA) {
163 if (!StmtB) // OB is a phi and phis dominate statements.
164 return true;
165 auto CA = NodeAddr<StmtNode*>(OA).Addr->getCode();
166 auto CB = NodeAddr<StmtNode*>(OB).Addr->getCode();
167 // The order must be linear, so tie-break such equalities.
168 if (CA == CB)
169 return A < B;
170 return MDT.dominates(CB, CA);
171 } else {
172 // OA is a phi.
173 if (StmtB)
174 return false;
175 // Both are phis. There is no ordering between phis (in terms of
176 // the data-flow), so tie-break this via node id comparison.
177 return A < B;
178 }
179 };
180
181 std::vector<NodeId> Tmp(Owners.begin(), Owners.end());
182 std::sort(Tmp.begin(), Tmp.end(), Less);
183
184 // The vector is a list of instructions, so that defs coming from
185 // the same instruction don't need to be artificially ordered.
186 // Then, when computing the initial segment, and iterating over an
187 // instruction, pick the defs that contribute to the covering (i.e. is
188 // not covered by previously added defs). Check the defs individually,
189 // i.e. first check each def if is covered or not (without adding them
190 // to the tracking set), and then add all the selected ones.
191
192 // The reason for this is this example:
193 // *d1<A>, *d2<B>, ... Assume A and B are aliased (can happen in phi nodes).
194 // *d3<C> If A \incl BuC, and B \incl AuC, then *d2 would be
195 // covered if we added A first, and A would be covered
196 // if we added B first.
197
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000198 RegisterSet RRs = DefRRs;
199
200 auto DefInSet = [&Defs] (NodeAddr<RefNode*> TA) -> bool {
201 return TA.Addr->getKind() == NodeAttrs::Def &&
202 Defs.count(TA.Id);
203 };
204 for (auto T : Tmp) {
Krzysztof Parzyszek29e93f32016-09-22 21:01:24 +0000205 if (!FullChain && RAI.covers(RRs, RefRR, DFG))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000206 break;
207 auto TA = DFG.addr<InstrNode*>(T);
208 bool IsPhi = DFG.IsCode<NodeAttrs::Phi>(TA);
209 NodeList Ds;
210 for (NodeAddr<DefNode*> DA : TA.Addr->members_if(DefInSet, DFG)) {
211 auto QR = DA.Addr->getRegRef();
212 // Add phi defs even if they are covered by subsequent defs. This is
213 // for cases where the reached use is not covered by any of the defs
214 // encountered so far: the phi def is needed to expose the liveness
215 // of that use to the entry of the block.
216 // Example:
217 // phi d1<R3>(,d2,), ... Phi def d1 is covered by d2.
218 // d2<R3>(d1,,u3), ...
219 // ..., u3<D1>(d2) This use needs to be live on entry.
Krzysztof Parzyszek29e93f32016-09-22 21:01:24 +0000220 if (FullChain || IsPhi || !RAI.covers(RRs, QR, DFG))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000221 Ds.push_back(DA);
222 }
223 RDefs.insert(RDefs.end(), Ds.begin(), Ds.end());
224 for (NodeAddr<DefNode*> DA : Ds) {
225 // When collecting a full chain of definitions, do not consider phi
226 // defs to actually define a register.
227 uint16_t Flags = DA.Addr->getFlags();
228 if (!FullChain || !(Flags & NodeAttrs::PhiRef))
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000229 if (!(Flags & NodeAttrs::Preserving)) // Don't care about Undef here.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000230 RRs.insert(DA.Addr->getRegRef());
231 }
232 }
233
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +0000234 auto DeadP = [](const NodeAddr<DefNode*> DA) -> bool {
235 return DA.Addr->getFlags() & NodeAttrs::Dead;
236 };
237 RDefs.resize(std::distance(RDefs.begin(), remove_if(RDefs, DeadP)));
238
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000239 return RDefs;
240}
241
242
243static const RegisterSet NoRegs;
244
245NodeList Liveness::getAllReachingDefs(NodeAddr<RefNode*> RefA) {
246 return getAllReachingDefs(RefA.Addr->getRegRef(), RefA, false, NoRegs);
247}
248
249
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000250NodeSet Liveness::getAllReachingDefsRec(RegisterRef RefRR,
251 NodeAddr<RefNode*> RefA, NodeSet &Visited, const NodeSet &Defs) {
252 // Collect all defined registers. Do not consider phis to be defining
253 // anything, only collect "real" definitions.
254 RegisterSet DefRRs;
255 for (const auto D : Defs) {
256 const auto DA = DFG.addr<const DefNode*>(D);
257 if (!(DA.Addr->getFlags() & NodeAttrs::PhiRef))
258 DefRRs.insert(DA.Addr->getRegRef());
259 }
260
261 auto RDs = getAllReachingDefs(RefRR, RefA, true, DefRRs);
262 if (RDs.empty())
263 return Defs;
264
265 // Make a copy of the preexisting definitions and add the newly found ones.
266 NodeSet TmpDefs = Defs;
267 for (auto R : RDs)
268 TmpDefs.insert(R.Id);
269
270 NodeSet Result = Defs;
271
272 for (NodeAddr<DefNode*> DA : RDs) {
273 Result.insert(DA.Id);
274 if (!(DA.Addr->getFlags() & NodeAttrs::PhiRef))
275 continue;
276 NodeAddr<PhiNode*> PA = DA.Addr->getOwner(DFG);
277 if (Visited.count(PA.Id))
278 continue;
279 Visited.insert(PA.Id);
280 // Go over all phi uses and get the reaching defs for each use.
281 for (auto U : PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG)) {
282 const auto &T = getAllReachingDefsRec(RefRR, U, Visited, TmpDefs);
283 Result.insert(T.begin(), T.end());
284 }
285 }
286
287 return Result;
288}
289
290
291NodeSet Liveness::getAllReachedUses(RegisterRef RefRR,
292 NodeAddr<DefNode*> DefA, const RegisterSet &DefRRs) {
293 NodeSet Uses;
294
295 // If the original register is already covered by all the intervening
296 // defs, no more uses can be reached.
Krzysztof Parzyszek29e93f32016-09-22 21:01:24 +0000297 if (RAI.covers(DefRRs, RefRR, DFG))
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000298 return Uses;
299
300 // Add all directly reached uses.
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +0000301 // If the def is dead, it does not provide a value for any use.
302 bool IsDead = DefA.Addr->getFlags() & NodeAttrs::Dead;
303 NodeId U = !IsDead ? DefA.Addr->getReachedUse() : 0;
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000304 while (U != 0) {
305 auto UA = DFG.addr<UseNode*>(U);
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000306 if (!(UA.Addr->getFlags() & NodeAttrs::Undef)) {
307 auto UR = UA.Addr->getRegRef();
Krzysztof Parzyszek29e93f32016-09-22 21:01:24 +0000308 if (RAI.alias(RefRR, UR, DFG) && !RAI.covers(DefRRs, UR, DFG))
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000309 Uses.insert(U);
310 }
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000311 U = UA.Addr->getSibling();
312 }
313
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +0000314 // Traverse all reached defs. This time dead defs cannot be ignored.
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000315 for (NodeId D = DefA.Addr->getReachedDef(), NextD; D != 0; D = NextD) {
316 auto DA = DFG.addr<DefNode*>(D);
317 NextD = DA.Addr->getSibling();
318 auto DR = DA.Addr->getRegRef();
319 // If this def is already covered, it cannot reach anything new.
320 // Similarly, skip it if it is not aliased to the interesting register.
Krzysztof Parzyszek29e93f32016-09-22 21:01:24 +0000321 if (RAI.covers(DefRRs, DR, DFG) || !RAI.alias(RefRR, DR, DFG))
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000322 continue;
323 NodeSet T;
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000324 if (DFG.IsPreservingDef(DA)) {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000325 // If it is a preserving def, do not update the set of intervening defs.
326 T = getAllReachedUses(RefRR, DA, DefRRs);
327 } else {
328 RegisterSet NewDefRRs = DefRRs;
329 NewDefRRs.insert(DR);
330 T = getAllReachedUses(RefRR, DA, NewDefRRs);
331 }
332 Uses.insert(T.begin(), T.end());
333 }
334 return Uses;
335}
336
337
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000338void Liveness::computePhiInfo() {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000339 RealUseMap.clear();
340
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000341 NodeList Phis;
342 NodeAddr<FuncNode*> FA = DFG.getFunc();
343 auto Blocks = FA.Addr->members(DFG);
344 for (NodeAddr<BlockNode*> BA : Blocks) {
345 auto Ps = BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG);
346 Phis.insert(Phis.end(), Ps.begin(), Ps.end());
347 }
348
349 // phi use -> (map: reaching phi -> set of registers defined in between)
350 std::map<NodeId,std::map<NodeId,RegisterSet>> PhiUp;
351 std::vector<NodeId> PhiUQ; // Work list of phis for upward propagation.
352
353 // Go over all phis.
354 for (NodeAddr<PhiNode*> PhiA : Phis) {
355 // Go over all defs and collect the reached uses that are non-phi uses
356 // (i.e. the "real uses").
357 auto &RealUses = RealUseMap[PhiA.Id];
358 auto PhiRefs = PhiA.Addr->members(DFG);
359
360 // Have a work queue of defs whose reached uses need to be found.
361 // For each def, add to the queue all reached (non-phi) defs.
362 SetVector<NodeId> DefQ;
363 NodeSet PhiDefs;
364 for (auto R : PhiRefs) {
365 if (!DFG.IsRef<NodeAttrs::Def>(R))
366 continue;
367 DefQ.insert(R.Id);
368 PhiDefs.insert(R.Id);
369 }
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000370
371 // Collect the super-set of all possible reached uses. This set will
372 // contain all uses reached from this phi, either directly from the
373 // phi defs, or (recursively) via non-phi defs reached by the phi defs.
374 // This set of uses will later be trimmed to only contain these uses that
375 // are actually reached by the phi defs.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000376 for (unsigned i = 0; i < DefQ.size(); ++i) {
377 NodeAddr<DefNode*> DA = DFG.addr<DefNode*>(DefQ[i]);
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +0000378 // Visit all reached uses. Phi defs should not really have the "dead"
379 // flag set, but check it anyway for consistency.
380 bool IsDead = DA.Addr->getFlags() & NodeAttrs::Dead;
381 NodeId UN = !IsDead ? DA.Addr->getReachedUse() : 0;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000382 while (UN != 0) {
383 NodeAddr<UseNode*> A = DFG.addr<UseNode*>(UN);
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000384 uint16_t F = A.Addr->getFlags();
385 if ((F & (NodeAttrs::Undef | NodeAttrs::PhiRef)) == 0)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000386 RealUses[getRestrictedRegRef(A)].insert(A.Id);
387 UN = A.Addr->getSibling();
388 }
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000389 // Visit all reached defs, and add them to the queue. These defs may
390 // override some of the uses collected here, but that will be handled
391 // later.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000392 NodeId DN = DA.Addr->getReachedDef();
393 while (DN != 0) {
394 NodeAddr<DefNode*> A = DFG.addr<DefNode*>(DN);
395 for (auto T : DFG.getRelatedRefs(A.Addr->getOwner(DFG), A)) {
396 uint16_t Flags = NodeAddr<DefNode*>(T).Addr->getFlags();
397 // Must traverse the reached-def chain. Consider:
398 // def(D0) -> def(R0) -> def(R0) -> use(D0)
399 // The reachable use of D0 passes through a def of R0.
400 if (!(Flags & NodeAttrs::PhiRef))
401 DefQ.insert(T.Id);
402 }
403 DN = A.Addr->getSibling();
404 }
405 }
406 // Filter out these uses that appear to be reachable, but really
407 // are not. For example:
408 //
409 // R1:0 = d1
410 // = R1:0 u2 Reached by d1.
411 // R0 = d3
412 // = R1:0 u4 Still reached by d1: indirectly through
413 // the def d3.
414 // R1 = d5
415 // = R1:0 u6 Not reached by d1 (covered collectively
416 // by d3 and d5), but following reached
417 // defs and uses from d1 will lead here.
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000418 auto InPhiDefs = [&PhiDefs] (NodeAddr<DefNode*> DA) -> bool {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000419 return PhiDefs.count(DA.Id);
420 };
421 for (auto UI = RealUses.begin(), UE = RealUses.end(); UI != UE; ) {
422 // For each reached register UI->first, there is a set UI->second, of
423 // uses of it. For each such use, check if it is reached by this phi,
424 // i.e. check if the set of its reaching uses intersects the set of
425 // this phi's defs.
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000426 NodeSet &Uses = UI->second;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000427 for (auto I = Uses.begin(), E = Uses.end(); I != E; ) {
428 auto UA = DFG.addr<UseNode*>(*I);
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +0000429 // Undef flag is checked above.
430 assert((UA.Addr->getFlags() & NodeAttrs::Undef) == 0);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000431 NodeList RDs = getAllReachingDefs(UI->first, UA);
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000432 if (any_of(RDs, InPhiDefs))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000433 ++I;
434 else
435 I = Uses.erase(I);
436 }
437 if (Uses.empty())
438 UI = RealUses.erase(UI);
439 else
440 ++UI;
441 }
442
443 // If this phi reaches some "real" uses, add it to the queue for upward
444 // propagation.
445 if (!RealUses.empty())
446 PhiUQ.push_back(PhiA.Id);
447
448 // Go over all phi uses and check if the reaching def is another phi.
449 // Collect the phis that are among the reaching defs of these uses.
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000450 // While traversing the list of reaching defs for each phi use, accumulate
451 // the set of registers defined between this phi (PhiA) and the owner phi
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000452 // of the reaching def.
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000453 NodeSet SeenUses;
Krzysztof Parzyszeka1218722016-09-08 20:48:42 +0000454
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000455 for (auto I : PhiRefs) {
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000456 if (!DFG.IsRef<NodeAttrs::Use>(I) || SeenUses.count(I.Id))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000457 continue;
458 NodeAddr<UseNode*> UA = I;
Krzysztof Parzyszeka1218722016-09-08 20:48:42 +0000459
460 // Given a phi use UA, traverse all related phi uses (including UA).
461 // The related phi uses may reach different phi nodes or may reach the
462 // same phi node. If multiple uses reach the same phi P, the intervening
463 // defs must be accumulated for all such uses. To group all such uses
464 // into one set, map their node ids to the first use id that reaches P.
465 std::map<NodeId,NodeId> FirstUse; // Phi reached up -> first phi use.
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000466
467 for (NodeAddr<UseNode*> VA : DFG.getRelatedRefs(PhiA, UA)) {
468 SeenUses.insert(VA.Id);
Krzysztof Parzyszeka1218722016-09-08 20:48:42 +0000469 RegisterSet DefRRs;
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000470 for (NodeAddr<DefNode*> DA : getAllReachingDefs(VA)) {
471 if (DA.Addr->getFlags() & NodeAttrs::PhiRef) {
Krzysztof Parzyszeka1218722016-09-08 20:48:42 +0000472 NodeId RP = DA.Addr->getOwner(DFG).Id;
473 NodeId FU = FirstUse.insert({RP,VA.Id}).first->second;
474 PhiUp[FU][RP].insert(DefRRs.begin(), DefRRs.end());
475 }
476 DefRRs.insert(DA.Addr->getRegRef());
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000477 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000478 }
479 }
480 }
481
482 if (Trace) {
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000483 dbgs() << "Phi-up-to-phi map with intervening defs:\n";
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000484 for (auto I : PhiUp) {
485 dbgs() << "phi " << Print<NodeId>(I.first, DFG) << " -> {";
486 for (auto R : I.second)
487 dbgs() << ' ' << Print<NodeId>(R.first, DFG)
488 << Print<RegisterSet>(R.second, DFG);
489 dbgs() << " }\n";
490 }
491 }
492
493 // Propagate the reached registers up in the phi chain.
494 //
495 // The following type of situation needs careful handling:
496 //
497 // phi d1<R1:0> (1)
498 // |
499 // ... d2<R1>
500 // |
501 // phi u3<R1:0> (2)
502 // |
503 // ... u4<R1>
504 //
505 // The phi node (2) defines a register pair R1:0, and reaches a "real"
506 // use u4 of just R1. The same phi node is also known to reach (upwards)
507 // the phi node (1). However, the use u4 is not reached by phi (1),
508 // because of the intervening definition d2 of R1. The data flow between
509 // phis (1) and (2) is restricted to R1:0 minus R1, i.e. R0.
510 //
511 // When propagating uses up the phi chains, get the all reaching defs
512 // for a given phi use, and traverse the list until the propagated ref
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000513 // is covered, or until reaching the final phi. Only assume that the
514 // reference reaches the phi in the latter case.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000515
516 for (unsigned i = 0; i < PhiUQ.size(); ++i) {
517 auto PA = DFG.addr<PhiNode*>(PhiUQ[i]);
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000518 NodeList PUs = PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG);
519 RefMap &RUM = RealUseMap[PA.Id];
520
521 for (auto U : PUs) {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000522 NodeAddr<UseNode*> UA = U;
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000523 std::map<NodeId,RegisterSet> &PUM = PhiUp[UA.Id];
524 for (const std::pair<NodeId,RegisterSet> &P : PUM) {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000525 bool Changed = false;
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000526 RegisterSet MidDefs = P.second;
527
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000528 // Collect the set UpReached of uses that are reached by the current
529 // phi PA, and are not covered by any intervening def between PA and
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000530 // the upward phi P.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000531 RegisterSet UpReached;
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000532 for (const std::pair<RegisterRef,NodeSet> &T : RUM) {
533 RegisterRef R = T.first;
534 if (!isRestrictedToRef(PA, UA, R))
535 R = getRestrictedRegRef(UA);
Krzysztof Parzyszek29e93f32016-09-22 21:01:24 +0000536 if (!RAI.covers(MidDefs, R, DFG))
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000537 UpReached.insert(R);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000538 }
539 if (UpReached.empty())
540 continue;
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000541 // Update the set PRUs of real uses reached by the upward phi P with
542 // the actual set of uses (UpReached) that the P phi reaches.
543 RefMap &PRUs = RealUseMap[P.first];
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000544 for (auto R : UpReached) {
545 unsigned Z = PRUs[R].size();
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000546 PRUs[R].insert(RUM[R].begin(), RUM[R].end());
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000547 Changed |= (PRUs[R].size() != Z);
548 }
549 if (Changed)
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000550 PhiUQ.push_back(P.first);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000551 }
552 }
553 }
554
555 if (Trace) {
556 dbgs() << "Real use map:\n";
557 for (auto I : RealUseMap) {
558 dbgs() << "phi " << Print<NodeId>(I.first, DFG);
559 NodeAddr<PhiNode*> PA = DFG.addr<PhiNode*>(I.first);
560 NodeList Ds = PA.Addr->members_if(DFG.IsRef<NodeAttrs::Def>, DFG);
561 if (!Ds.empty()) {
562 RegisterRef RR = NodeAddr<DefNode*>(Ds[0]).Addr->getRegRef();
563 dbgs() << '<' << Print<RegisterRef>(RR, DFG) << '>';
564 } else {
565 dbgs() << "<noreg>";
566 }
567 dbgs() << " -> " << Print<RefMap>(I.second, DFG) << '\n';
568 }
569 }
570}
571
572
573void Liveness::computeLiveIns() {
574 // Populate the node-to-block map. This speeds up the calculations
575 // significantly.
576 NBMap.clear();
577 for (NodeAddr<BlockNode*> BA : DFG.getFunc().Addr->members(DFG)) {
578 MachineBasicBlock *BB = BA.Addr->getCode();
579 for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG)) {
580 for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG))
581 NBMap.insert(std::make_pair(RA.Id, BB));
582 NBMap.insert(std::make_pair(IA.Id, BB));
583 }
584 }
585
586 MachineFunction &MF = DFG.getMF();
587
588 // Compute IDF first, then the inverse.
589 decltype(IIDF) IDF;
590 for (auto &B : MF) {
591 auto F1 = MDF.find(&B);
592 if (F1 == MDF.end())
593 continue;
594 SetVector<MachineBasicBlock*> IDFB(F1->second.begin(), F1->second.end());
595 for (unsigned i = 0; i < IDFB.size(); ++i) {
596 auto F2 = MDF.find(IDFB[i]);
597 if (F2 != MDF.end())
598 IDFB.insert(F2->second.begin(), F2->second.end());
599 }
600 // Add B to the IDF(B). This will put B in the IIDF(B).
601 IDFB.insert(&B);
602 IDF[&B].insert(IDFB.begin(), IDFB.end());
603 }
604
605 for (auto I : IDF)
606 for (auto S : I.second)
607 IIDF[S].insert(I.first);
608
609 computePhiInfo();
610
611 NodeAddr<FuncNode*> FA = DFG.getFunc();
612 auto Blocks = FA.Addr->members(DFG);
613
614 // Build the phi live-on-entry map.
615 for (NodeAddr<BlockNode*> BA : Blocks) {
616 MachineBasicBlock *MB = BA.Addr->getCode();
617 auto &LON = PhiLON[MB];
618 for (auto P : BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG))
619 for (auto S : RealUseMap[P.Id])
620 LON[S.first].insert(S.second.begin(), S.second.end());
621 }
622
623 if (Trace) {
624 dbgs() << "Phi live-on-entry map:\n";
625 for (auto I : PhiLON)
626 dbgs() << "block #" << I.first->getNumber() << " -> "
627 << Print<RefMap>(I.second, DFG) << '\n';
628 }
629
630 // Build the phi live-on-exit map. Each phi node has some set of reached
631 // "real" uses. Propagate this set backwards into the block predecessors
632 // through the reaching defs of the corresponding phi uses.
633 for (NodeAddr<BlockNode*> BA : Blocks) {
634 auto Phis = BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG);
635 for (NodeAddr<PhiNode*> PA : Phis) {
636 auto &RUs = RealUseMap[PA.Id];
637 if (RUs.empty())
638 continue;
639
640 for (auto U : PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG)) {
641 NodeAddr<PhiUseNode*> UA = U;
642 if (UA.Addr->getReachingDef() == 0)
643 continue;
644
645 // Mark all reached "real" uses of P as live on exit in the
646 // predecessor.
647 // Remap all the RUs so that they have a correct reaching def.
648 auto PrA = DFG.addr<BlockNode*>(UA.Addr->getPredecessor());
649 auto &LOX = PhiLOX[PrA.Addr->getCode()];
650 for (auto R : RUs) {
651 RegisterRef RR = R.first;
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000652 if (!isRestrictedToRef(PA, UA, RR))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000653 RR = getRestrictedRegRef(UA);
654 // The restricted ref may be different from the ref that was
655 // accessed in the "real use". This means that this phi use
656 // is not the one that carries this reference, so skip it.
Krzysztof Parzyszek29e93f32016-09-22 21:01:24 +0000657 if (!RAI.alias(R.first, RR, DFG))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000658 continue;
659 for (auto D : getAllReachingDefs(RR, UA))
660 LOX[RR].insert(D.Id);
661 }
662 } // for U : phi uses
663 } // for P : Phis
664 } // for B : Blocks
665
666 if (Trace) {
667 dbgs() << "Phi live-on-exit map:\n";
668 for (auto I : PhiLOX)
669 dbgs() << "block #" << I.first->getNumber() << " -> "
670 << Print<RefMap>(I.second, DFG) << '\n';
671 }
672
673 RefMap LiveIn;
674 traverse(&MF.front(), LiveIn);
675
676 // Add function live-ins to the live-in set of the function entry block.
677 auto &EntryIn = LiveMap[&MF.front()];
678 for (auto I = MRI.livein_begin(), E = MRI.livein_end(); I != E; ++I)
679 EntryIn.insert({I->first,0});
680
681 if (Trace) {
682 // Dump the liveness map
683 for (auto &B : MF) {
684 BitVector LV(TRI.getNumRegs());
685 for (auto I = B.livein_begin(), E = B.livein_end(); I != E; ++I)
686 LV.set(I->PhysReg);
687 dbgs() << "BB#" << B.getNumber() << "\t rec = {";
688 for (int x = LV.find_first(); x >= 0; x = LV.find_next(x))
689 dbgs() << ' ' << Print<RegisterRef>({unsigned(x),0}, DFG);
690 dbgs() << " }\n";
691 dbgs() << "\tcomp = " << Print<RegisterSet>(LiveMap[&B], DFG) << '\n';
692 }
693 }
694}
695
696
697void Liveness::resetLiveIns() {
698 for (auto &B : DFG.getMF()) {
699 // Remove all live-ins.
700 std::vector<unsigned> T;
701 for (auto I = B.livein_begin(), E = B.livein_end(); I != E; ++I)
702 T.push_back(I->PhysReg);
703 for (auto I : T)
704 B.removeLiveIn(I);
705 // Add the newly computed live-ins.
706 auto &LiveIns = LiveMap[&B];
707 for (auto I : LiveIns) {
708 assert(I.Sub == 0);
709 B.addLiveIn(I.Reg);
710 }
711 }
712}
713
714
715void Liveness::resetKills() {
716 for (auto &B : DFG.getMF())
717 resetKills(&B);
718}
719
720
721void Liveness::resetKills(MachineBasicBlock *B) {
722 auto CopyLiveIns = [] (MachineBasicBlock *B, BitVector &LV) -> void {
723 for (auto I = B->livein_begin(), E = B->livein_end(); I != E; ++I)
724 LV.set(I->PhysReg);
725 };
726
727 BitVector LiveIn(TRI.getNumRegs()), Live(TRI.getNumRegs());
728 CopyLiveIns(B, LiveIn);
729 for (auto SI : B->successors())
730 CopyLiveIns(SI, Live);
731
732 for (auto I = B->rbegin(), E = B->rend(); I != E; ++I) {
733 MachineInstr *MI = &*I;
734 if (MI->isDebugValue())
735 continue;
736
737 MI->clearKillInfo();
738 for (auto &Op : MI->operands()) {
Krzysztof Parzyszekf69ff712016-06-02 14:30:09 +0000739 // An implicit def of a super-register may not necessarily start a
740 // live range of it, since an implicit use could be used to keep parts
741 // of it live. Instead of analyzing the implicit operands, ignore
742 // implicit defs.
743 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000744 continue;
745 unsigned R = Op.getReg();
746 if (!TargetRegisterInfo::isPhysicalRegister(R))
747 continue;
748 for (MCSubRegIterator SR(R, &TRI, true); SR.isValid(); ++SR)
749 Live.reset(*SR);
750 }
751 for (auto &Op : MI->operands()) {
752 if (!Op.isReg() || !Op.isUse())
753 continue;
754 unsigned R = Op.getReg();
755 if (!TargetRegisterInfo::isPhysicalRegister(R))
756 continue;
757 bool IsLive = false;
Krzysztof Parzyszek16331f02016-04-20 14:33:23 +0000758 for (MCRegAliasIterator AR(R, &TRI, true); AR.isValid(); ++AR) {
759 if (!Live[*AR])
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000760 continue;
761 IsLive = true;
762 break;
763 }
764 if (IsLive)
765 continue;
766 Op.setIsKill(true);
767 for (MCSubRegIterator SR(R, &TRI, true); SR.isValid(); ++SR)
768 Live.set(*SR);
769 }
770 }
771}
772
773
774// For shadows, determine if RR is aliased to a reaching def of any other
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000775// shadow associated with RA. The register ref on RA will be "larger" than
776// each individual reaching def, and to determine the data-flow between defs
777// and uses of RR it may be necessary to visit all shadows. If RR is not
778// aliased to the reaching def of any other shadow, then visiting only RA
779// is sufficient. In that sense, the data flow of RR would be restricted to
780// the reference RA.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000781// For non-shadows, this function returns "true".
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000782bool Liveness::isRestrictedToRef(NodeAddr<InstrNode*> IA, NodeAddr<RefNode*> RA,
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000783 RegisterRef RR) const {
784 NodeId Start = RA.Id;
785 for (NodeAddr<RefNode*> TA = DFG.getNextShadow(IA, RA);
786 TA.Id != 0 && TA.Id != Start; TA = DFG.getNextShadow(IA, TA)) {
787 NodeId RD = TA.Addr->getReachingDef();
788 if (RD == 0)
789 continue;
Krzysztof Parzyszek29e93f32016-09-22 21:01:24 +0000790 if (RAI.alias(RR, DFG.addr<DefNode*>(RD).Addr->getRegRef(), DFG))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000791 return false;
792 }
793 return true;
794}
795
796
797RegisterRef Liveness::getRestrictedRegRef(NodeAddr<RefNode*> RA) const {
798 assert(DFG.IsRef<NodeAttrs::Use>(RA));
799 if (RA.Addr->getFlags() & NodeAttrs::Shadow) {
800 NodeId RD = RA.Addr->getReachingDef();
801 assert(RD);
802 RA = DFG.addr<DefNode*>(RD);
803 }
804 return RA.Addr->getRegRef();
805}
806
807
808unsigned Liveness::getPhysReg(RegisterRef RR) const {
809 if (!TargetRegisterInfo::isPhysicalRegister(RR.Reg))
810 return 0;
811 return RR.Sub ? TRI.getSubReg(RR.Reg, RR.Sub) : RR.Reg;
812}
813
814
815// Helper function to obtain the basic block containing the reaching def
816// of the given use.
817MachineBasicBlock *Liveness::getBlockWithRef(NodeId RN) const {
818 auto F = NBMap.find(RN);
819 if (F != NBMap.end())
820 return F->second;
821 llvm_unreachable("Node id not in map");
822}
823
824
825void Liveness::traverse(MachineBasicBlock *B, RefMap &LiveIn) {
826 // The LiveIn map, for each (physical) register, contains the set of live
827 // reaching defs of that register that are live on entry to the associated
828 // block.
829
830 // The summary of the traversal algorithm:
831 //
832 // R is live-in in B, if there exists a U(R), such that rdef(R) dom B
833 // and (U \in IDF(B) or B dom U).
834 //
835 // for (C : children) {
836 // LU = {}
837 // traverse(C, LU)
838 // LiveUses += LU
839 // }
840 //
841 // LiveUses -= Defs(B);
842 // LiveUses += UpwardExposedUses(B);
843 // for (C : IIDF[B])
844 // for (U : LiveUses)
845 // if (Rdef(U) dom C)
846 // C.addLiveIn(U)
847 //
848
849 // Go up the dominator tree (depth-first).
850 MachineDomTreeNode *N = MDT.getNode(B);
851 for (auto I : *N) {
852 RefMap L;
853 MachineBasicBlock *SB = I->getBlock();
854 traverse(SB, L);
855
856 for (auto S : L)
857 LiveIn[S.first].insert(S.second.begin(), S.second.end());
858 }
859
860 if (Trace) {
861 dbgs() << LLVM_FUNCTION_NAME << " in BB#" << B->getNumber()
862 << " after recursion into";
863 for (auto I : *N)
864 dbgs() << ' ' << I->getBlock()->getNumber();
865 dbgs() << "\n LiveIn: " << Print<RefMap>(LiveIn, DFG);
866 dbgs() << "\n Local: " << Print<RegisterSet>(LiveMap[B], DFG) << '\n';
867 }
868
869 // Add phi uses that are live on exit from this block.
870 RefMap &PUs = PhiLOX[B];
871 for (auto S : PUs)
872 LiveIn[S.first].insert(S.second.begin(), S.second.end());
873
874 if (Trace) {
875 dbgs() << "after LOX\n";
876 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
877 dbgs() << " Local: " << Print<RegisterSet>(LiveMap[B], DFG) << '\n';
878 }
879
880 // Stop tracking all uses defined in this block: erase those records
881 // where the reaching def is located in B and which cover all reached
882 // uses.
883 auto Copy = LiveIn;
884 LiveIn.clear();
885
886 for (auto I : Copy) {
887 auto &Defs = LiveIn[I.first];
888 NodeSet Rest;
889 for (auto R : I.second) {
890 auto DA = DFG.addr<DefNode*>(R);
891 RegisterRef DDR = DA.Addr->getRegRef();
892 NodeAddr<InstrNode*> IA = DA.Addr->getOwner(DFG);
893 NodeAddr<BlockNode*> BA = IA.Addr->getOwner(DFG);
894 // Defs from a different block need to be preserved. Defs from this
895 // block will need to be processed further, except for phi defs, the
896 // liveness of which is handled through the PhiLON/PhiLOX maps.
897 if (B != BA.Addr->getCode())
898 Defs.insert(R);
899 else {
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000900 bool IsPreserving = DFG.IsPreservingDef(DA);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000901 if (IA.Addr->getKind() != NodeAttrs::Phi && !IsPreserving) {
Krzysztof Parzyszek29e93f32016-09-22 21:01:24 +0000902 bool Covering = RAI.covers(DDR, I.first, DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000903 NodeId U = DA.Addr->getReachedUse();
904 while (U && Covering) {
905 auto DUA = DFG.addr<UseNode*>(U);
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000906 if (!(DUA.Addr->getFlags() & NodeAttrs::Undef)) {
907 RegisterRef Q = DUA.Addr->getRegRef();
Krzysztof Parzyszek29e93f32016-09-22 21:01:24 +0000908 Covering = RAI.covers(DA.Addr->getRegRef(), Q, DFG);
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000909 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000910 U = DUA.Addr->getSibling();
911 }
912 if (!Covering)
913 Rest.insert(R);
914 }
915 }
916 }
917
918 // Non-covering defs from B.
919 for (auto R : Rest) {
920 auto DA = DFG.addr<DefNode*>(R);
921 RegisterRef DRR = DA.Addr->getRegRef();
922 RegisterSet RRs;
923 for (NodeAddr<DefNode*> TA : getAllReachingDefs(DA)) {
924 NodeAddr<InstrNode*> IA = TA.Addr->getOwner(DFG);
925 NodeAddr<BlockNode*> BA = IA.Addr->getOwner(DFG);
926 // Preserving defs do not count towards covering.
927 if (!(TA.Addr->getFlags() & NodeAttrs::Preserving))
928 RRs.insert(TA.Addr->getRegRef());
929 if (BA.Addr->getCode() == B)
930 continue;
Krzysztof Parzyszek29e93f32016-09-22 21:01:24 +0000931 if (RAI.covers(RRs, DRR, DFG))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000932 break;
933 Defs.insert(TA.Id);
934 }
935 }
936 }
937
938 emptify(LiveIn);
939
940 if (Trace) {
941 dbgs() << "after defs in block\n";
942 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
943 dbgs() << " Local: " << Print<RegisterSet>(LiveMap[B], DFG) << '\n';
944 }
945
946 // Scan the block for upward-exposed uses and add them to the tracking set.
947 for (auto I : DFG.getFunc().Addr->findBlock(B, DFG).Addr->members(DFG)) {
948 NodeAddr<InstrNode*> IA = I;
949 if (IA.Addr->getKind() != NodeAttrs::Stmt)
950 continue;
951 for (NodeAddr<UseNode*> UA : IA.Addr->members_if(DFG.IsUse, DFG)) {
952 RegisterRef RR = UA.Addr->getRegRef();
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000953 if (UA.Addr->getFlags() & NodeAttrs::Undef)
954 continue;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000955 for (auto D : getAllReachingDefs(UA))
956 if (getBlockWithRef(D.Id) != B)
957 LiveIn[RR].insert(D.Id);
958 }
959 }
960
961 if (Trace) {
962 dbgs() << "after uses in block\n";
963 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
964 dbgs() << " Local: " << Print<RegisterSet>(LiveMap[B], DFG) << '\n';
965 }
966
967 // Phi uses should not be propagated up the dominator tree, since they
968 // are not dominated by their corresponding reaching defs.
969 auto &Local = LiveMap[B];
970 auto &LON = PhiLON[B];
971 for (auto R : LON)
972 Local.insert(R.first);
973
974 if (Trace) {
975 dbgs() << "after phi uses in block\n";
976 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
977 dbgs() << " Local: " << Print<RegisterSet>(Local, DFG) << '\n';
978 }
979
980 for (auto C : IIDF[B]) {
981 auto &LiveC = LiveMap[C];
982 for (auto S : LiveIn)
983 for (auto R : S.second)
984 if (MDT.properlyDominates(getBlockWithRef(R), C))
985 LiveC.insert(S.first);
986 }
987}
988
989
990void Liveness::emptify(RefMap &M) {
991 for (auto I = M.begin(), E = M.end(); I != E; )
992 I = I->second.empty() ? M.erase(I) : std::next(I);
993}
994