blob: 27808e2325894c83141479b3d1ea393c93ce4a5c [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,
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +000088 NodeAddr<RefNode*> RefA, bool FullChain, const RegisterAggr &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 Parzyszeka77fe4e2016-10-03 17:14:48 +0000121 if (!DFG.IsPreservingDef(TA))
122 if (RegisterAggr::isCoverOf(RR, RefRR, DFG.getLMI(), TRI))
123 continue;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000124 // Get the next level of reaching defs. This will include multiple
125 // reaching defs for shadows.
126 for (auto S : DFG.getRelatedRefs(TA.Addr->getOwner(DFG), TA))
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000127 if (NodeId RD = NodeAddr<RefNode*>(S).Addr->getReachingDef())
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000128 DefQ.insert(RD);
129 }
130
131 // Remove all non-phi defs that are not aliased to RefRR, and collect
132 // the owners of the remaining defs.
133 SetVector<NodeId> Defs;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000134 for (NodeId N : DefQ) {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000135 auto TA = DFG.addr<DefNode*>(N);
136 bool IsPhi = TA.Addr->getFlags() & NodeAttrs::PhiRef;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000137 if (!IsPhi && !DFG.alias(RefRR, TA.Addr->getRegRef()))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000138 continue;
139 Defs.insert(TA.Id);
140 Owners.insert(TA.Addr->getOwner(DFG).Id);
141 }
142
143 // Return the MachineBasicBlock containing a given instruction.
144 auto Block = [this] (NodeAddr<InstrNode*> IA) -> MachineBasicBlock* {
145 if (IA.Addr->getKind() == NodeAttrs::Stmt)
146 return NodeAddr<StmtNode*>(IA).Addr->getCode()->getParent();
147 assert(IA.Addr->getKind() == NodeAttrs::Phi);
148 NodeAddr<PhiNode*> PA = IA;
149 NodeAddr<BlockNode*> BA = PA.Addr->getOwner(DFG);
150 return BA.Addr->getCode();
151 };
152 // Less(A,B) iff instruction A is further down in the dominator tree than B.
153 auto Less = [&Block,this] (NodeId A, NodeId B) -> bool {
154 if (A == B)
155 return false;
156 auto OA = DFG.addr<InstrNode*>(A), OB = DFG.addr<InstrNode*>(B);
157 MachineBasicBlock *BA = Block(OA), *BB = Block(OB);
158 if (BA != BB)
159 return MDT.dominates(BB, BA);
160 // They are in the same block.
161 bool StmtA = OA.Addr->getKind() == NodeAttrs::Stmt;
162 bool StmtB = OB.Addr->getKind() == NodeAttrs::Stmt;
163 if (StmtA) {
164 if (!StmtB) // OB is a phi and phis dominate statements.
165 return true;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000166 MachineInstr *CA = NodeAddr<StmtNode*>(OA).Addr->getCode();
167 MachineInstr *CB = NodeAddr<StmtNode*>(OB).Addr->getCode();
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000168 // The order must be linear, so tie-break such equalities.
169 if (CA == CB)
170 return A < B;
171 return MDT.dominates(CB, CA);
172 } else {
173 // OA is a phi.
174 if (StmtB)
175 return false;
176 // Both are phis. There is no ordering between phis (in terms of
177 // the data-flow), so tie-break this via node id comparison.
178 return A < B;
179 }
180 };
181
182 std::vector<NodeId> Tmp(Owners.begin(), Owners.end());
183 std::sort(Tmp.begin(), Tmp.end(), Less);
184
185 // The vector is a list of instructions, so that defs coming from
186 // the same instruction don't need to be artificially ordered.
187 // Then, when computing the initial segment, and iterating over an
188 // instruction, pick the defs that contribute to the covering (i.e. is
189 // not covered by previously added defs). Check the defs individually,
190 // i.e. first check each def if is covered or not (without adding them
191 // to the tracking set), and then add all the selected ones.
192
193 // The reason for this is this example:
194 // *d1<A>, *d2<B>, ... Assume A and B are aliased (can happen in phi nodes).
195 // *d3<C> If A \incl BuC, and B \incl AuC, then *d2 would be
196 // covered if we added A first, and A would be covered
197 // if we added B first.
198
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000199 RegisterAggr RRs(DefRRs);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000200
201 auto DefInSet = [&Defs] (NodeAddr<RefNode*> TA) -> bool {
202 return TA.Addr->getKind() == NodeAttrs::Def &&
203 Defs.count(TA.Id);
204 };
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000205 for (NodeId T : Tmp) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000206 if (!FullChain && RRs.hasCoverOf(RefRR))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000207 break;
208 auto TA = DFG.addr<InstrNode*>(T);
209 bool IsPhi = DFG.IsCode<NodeAttrs::Phi>(TA);
210 NodeList Ds;
211 for (NodeAddr<DefNode*> DA : TA.Addr->members_if(DefInSet, DFG)) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000212 RegisterRef QR = DA.Addr->getRegRef();
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000213 // Add phi defs even if they are covered by subsequent defs. This is
214 // for cases where the reached use is not covered by any of the defs
215 // encountered so far: the phi def is needed to expose the liveness
216 // of that use to the entry of the block.
217 // Example:
218 // phi d1<R3>(,d2,), ... Phi def d1 is covered by d2.
219 // d2<R3>(d1,,u3), ...
220 // ..., u3<D1>(d2) This use needs to be live on entry.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000221 if (FullChain || IsPhi || !RRs.hasCoverOf(QR))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000222 Ds.push_back(DA);
223 }
224 RDefs.insert(RDefs.end(), Ds.begin(), Ds.end());
225 for (NodeAddr<DefNode*> DA : Ds) {
226 // When collecting a full chain of definitions, do not consider phi
227 // defs to actually define a register.
228 uint16_t Flags = DA.Addr->getFlags();
229 if (!FullChain || !(Flags & NodeAttrs::PhiRef))
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000230 if (!(Flags & NodeAttrs::Preserving)) // Don't care about Undef here.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000231 RRs.insert(DA.Addr->getRegRef());
232 }
233 }
234
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +0000235 auto DeadP = [](const NodeAddr<DefNode*> DA) -> bool {
236 return DA.Addr->getFlags() & NodeAttrs::Dead;
237 };
238 RDefs.resize(std::distance(RDefs.begin(), remove_if(RDefs, DeadP)));
239
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000240 return RDefs;
241}
242
243
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000244NodeSet Liveness::getAllReachingDefsRec(RegisterRef RefRR,
245 NodeAddr<RefNode*> RefA, NodeSet &Visited, const NodeSet &Defs) {
246 // Collect all defined registers. Do not consider phis to be defining
247 // anything, only collect "real" definitions.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000248 RegisterAggr DefRRs(DFG.getLMI(), TRI);
249 for (NodeId D : Defs) {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000250 const auto DA = DFG.addr<const DefNode*>(D);
251 if (!(DA.Addr->getFlags() & NodeAttrs::PhiRef))
252 DefRRs.insert(DA.Addr->getRegRef());
253 }
254
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000255 NodeList RDs = getAllReachingDefs(RefRR, RefA, true, DefRRs);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000256 if (RDs.empty())
257 return Defs;
258
259 // Make a copy of the preexisting definitions and add the newly found ones.
260 NodeSet TmpDefs = Defs;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000261 for (NodeAddr<NodeBase*> R : RDs)
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000262 TmpDefs.insert(R.Id);
263
264 NodeSet Result = Defs;
265
266 for (NodeAddr<DefNode*> DA : RDs) {
267 Result.insert(DA.Id);
268 if (!(DA.Addr->getFlags() & NodeAttrs::PhiRef))
269 continue;
270 NodeAddr<PhiNode*> PA = DA.Addr->getOwner(DFG);
271 if (Visited.count(PA.Id))
272 continue;
273 Visited.insert(PA.Id);
274 // Go over all phi uses and get the reaching defs for each use.
275 for (auto U : PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG)) {
276 const auto &T = getAllReachingDefsRec(RefRR, U, Visited, TmpDefs);
277 Result.insert(T.begin(), T.end());
278 }
279 }
280
281 return Result;
282}
283
284
285NodeSet Liveness::getAllReachedUses(RegisterRef RefRR,
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000286 NodeAddr<DefNode*> DefA, const RegisterAggr &DefRRs) {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000287 NodeSet Uses;
288
289 // If the original register is already covered by all the intervening
290 // defs, no more uses can be reached.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000291 if (DefRRs.hasCoverOf(RefRR))
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000292 return Uses;
293
294 // Add all directly reached uses.
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +0000295 // If the def is dead, it does not provide a value for any use.
296 bool IsDead = DefA.Addr->getFlags() & NodeAttrs::Dead;
297 NodeId U = !IsDead ? DefA.Addr->getReachedUse() : 0;
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000298 while (U != 0) {
299 auto UA = DFG.addr<UseNode*>(U);
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000300 if (!(UA.Addr->getFlags() & NodeAttrs::Undef)) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000301 RegisterRef UR = UA.Addr->getRegRef();
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000302 if (DFG.alias(RefRR, UR) && !DefRRs.hasCoverOf(UR))
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000303 Uses.insert(U);
304 }
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000305 U = UA.Addr->getSibling();
306 }
307
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +0000308 // Traverse all reached defs. This time dead defs cannot be ignored.
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000309 for (NodeId D = DefA.Addr->getReachedDef(), NextD; D != 0; D = NextD) {
310 auto DA = DFG.addr<DefNode*>(D);
311 NextD = DA.Addr->getSibling();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000312 RegisterRef DR = DA.Addr->getRegRef();
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000313 // If this def is already covered, it cannot reach anything new.
314 // Similarly, skip it if it is not aliased to the interesting register.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000315 if (DefRRs.hasCoverOf(DR) || !DFG.alias(RefRR, DR))
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000316 continue;
317 NodeSet T;
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000318 if (DFG.IsPreservingDef(DA)) {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000319 // If it is a preserving def, do not update the set of intervening defs.
320 T = getAllReachedUses(RefRR, DA, DefRRs);
321 } else {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000322 RegisterAggr NewDefRRs = DefRRs;
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000323 NewDefRRs.insert(DR);
324 T = getAllReachedUses(RefRR, DA, NewDefRRs);
325 }
326 Uses.insert(T.begin(), T.end());
327 }
328 return Uses;
329}
330
331
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000332void Liveness::computePhiInfo() {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000333 RealUseMap.clear();
334
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000335 NodeList Phis;
336 NodeAddr<FuncNode*> FA = DFG.getFunc();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000337 NodeList Blocks = FA.Addr->members(DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000338 for (NodeAddr<BlockNode*> BA : Blocks) {
339 auto Ps = BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG);
340 Phis.insert(Phis.end(), Ps.begin(), Ps.end());
341 }
342
343 // phi use -> (map: reaching phi -> set of registers defined in between)
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000344 std::map<NodeId,std::map<NodeId,RegisterAggr>> PhiUp;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000345 std::vector<NodeId> PhiUQ; // Work list of phis for upward propagation.
346
347 // Go over all phis.
348 for (NodeAddr<PhiNode*> PhiA : Phis) {
349 // Go over all defs and collect the reached uses that are non-phi uses
350 // (i.e. the "real uses").
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000351 RefMap &RealUses = RealUseMap[PhiA.Id];
352 NodeList PhiRefs = PhiA.Addr->members(DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000353
354 // Have a work queue of defs whose reached uses need to be found.
355 // For each def, add to the queue all reached (non-phi) defs.
356 SetVector<NodeId> DefQ;
357 NodeSet PhiDefs;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000358 for (NodeAddr<RefNode*> R : PhiRefs) {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000359 if (!DFG.IsRef<NodeAttrs::Def>(R))
360 continue;
361 DefQ.insert(R.Id);
362 PhiDefs.insert(R.Id);
363 }
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000364
365 // Collect the super-set of all possible reached uses. This set will
366 // contain all uses reached from this phi, either directly from the
367 // phi defs, or (recursively) via non-phi defs reached by the phi defs.
368 // This set of uses will later be trimmed to only contain these uses that
369 // are actually reached by the phi defs.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000370 for (unsigned i = 0; i < DefQ.size(); ++i) {
371 NodeAddr<DefNode*> DA = DFG.addr<DefNode*>(DefQ[i]);
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +0000372 // Visit all reached uses. Phi defs should not really have the "dead"
373 // flag set, but check it anyway for consistency.
374 bool IsDead = DA.Addr->getFlags() & NodeAttrs::Dead;
375 NodeId UN = !IsDead ? DA.Addr->getReachedUse() : 0;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000376 while (UN != 0) {
377 NodeAddr<UseNode*> A = DFG.addr<UseNode*>(UN);
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000378 uint16_t F = A.Addr->getFlags();
379 if ((F & (NodeAttrs::Undef | NodeAttrs::PhiRef)) == 0)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000380 RealUses[getRestrictedRegRef(A)].insert(A.Id);
381 UN = A.Addr->getSibling();
382 }
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000383 // Visit all reached defs, and add them to the queue. These defs may
384 // override some of the uses collected here, but that will be handled
385 // later.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000386 NodeId DN = DA.Addr->getReachedDef();
387 while (DN != 0) {
388 NodeAddr<DefNode*> A = DFG.addr<DefNode*>(DN);
389 for (auto T : DFG.getRelatedRefs(A.Addr->getOwner(DFG), A)) {
390 uint16_t Flags = NodeAddr<DefNode*>(T).Addr->getFlags();
391 // Must traverse the reached-def chain. Consider:
392 // def(D0) -> def(R0) -> def(R0) -> use(D0)
393 // The reachable use of D0 passes through a def of R0.
394 if (!(Flags & NodeAttrs::PhiRef))
395 DefQ.insert(T.Id);
396 }
397 DN = A.Addr->getSibling();
398 }
399 }
400 // Filter out these uses that appear to be reachable, but really
401 // are not. For example:
402 //
403 // R1:0 = d1
404 // = R1:0 u2 Reached by d1.
405 // R0 = d3
406 // = R1:0 u4 Still reached by d1: indirectly through
407 // the def d3.
408 // R1 = d5
409 // = R1:0 u6 Not reached by d1 (covered collectively
410 // by d3 and d5), but following reached
411 // defs and uses from d1 will lead here.
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000412 auto InPhiDefs = [&PhiDefs] (NodeAddr<DefNode*> DA) -> bool {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000413 return PhiDefs.count(DA.Id);
414 };
415 for (auto UI = RealUses.begin(), UE = RealUses.end(); UI != UE; ) {
416 // For each reached register UI->first, there is a set UI->second, of
417 // uses of it. For each such use, check if it is reached by this phi,
418 // i.e. check if the set of its reaching uses intersects the set of
419 // this phi's defs.
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000420 NodeSet &Uses = UI->second;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000421 for (auto I = Uses.begin(), E = Uses.end(); I != E; ) {
422 auto UA = DFG.addr<UseNode*>(*I);
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +0000423 // Undef flag is checked above.
424 assert((UA.Addr->getFlags() & NodeAttrs::Undef) == 0);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000425 NodeList RDs = getAllReachingDefs(UI->first, UA);
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000426 if (any_of(RDs, InPhiDefs))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000427 ++I;
428 else
429 I = Uses.erase(I);
430 }
431 if (Uses.empty())
432 UI = RealUses.erase(UI);
433 else
434 ++UI;
435 }
436
437 // If this phi reaches some "real" uses, add it to the queue for upward
438 // propagation.
439 if (!RealUses.empty())
440 PhiUQ.push_back(PhiA.Id);
441
442 // Go over all phi uses and check if the reaching def is another phi.
443 // Collect the phis that are among the reaching defs of these uses.
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000444 // While traversing the list of reaching defs for each phi use, accumulate
445 // the set of registers defined between this phi (PhiA) and the owner phi
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000446 // of the reaching def.
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000447 NodeSet SeenUses;
Krzysztof Parzyszeka1218722016-09-08 20:48:42 +0000448
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000449 for (auto I : PhiRefs) {
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000450 if (!DFG.IsRef<NodeAttrs::Use>(I) || SeenUses.count(I.Id))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000451 continue;
452 NodeAddr<UseNode*> UA = I;
Krzysztof Parzyszeka1218722016-09-08 20:48:42 +0000453
454 // Given a phi use UA, traverse all related phi uses (including UA).
455 // The related phi uses may reach different phi nodes or may reach the
456 // same phi node. If multiple uses reach the same phi P, the intervening
457 // defs must be accumulated for all such uses. To group all such uses
458 // into one set, map their node ids to the first use id that reaches P.
459 std::map<NodeId,NodeId> FirstUse; // Phi reached up -> first phi use.
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000460
461 for (NodeAddr<UseNode*> VA : DFG.getRelatedRefs(PhiA, UA)) {
462 SeenUses.insert(VA.Id);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000463 RegisterAggr DefRRs(DFG.getLMI(), TRI);
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000464 for (NodeAddr<DefNode*> DA : getAllReachingDefs(VA)) {
465 if (DA.Addr->getFlags() & NodeAttrs::PhiRef) {
Krzysztof Parzyszeka1218722016-09-08 20:48:42 +0000466 NodeId RP = DA.Addr->getOwner(DFG).Id;
467 NodeId FU = FirstUse.insert({RP,VA.Id}).first->second;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000468 std::map<NodeId,RegisterAggr> &M = PhiUp[FU];
469 auto F = M.find(RP);
470 if (F == M.end())
471 M.insert(std::make_pair(RP, DefRRs));
472 else
473 F->second.insert(DefRRs);
Krzysztof Parzyszeka1218722016-09-08 20:48:42 +0000474 }
475 DefRRs.insert(DA.Addr->getRegRef());
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000476 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000477 }
478 }
479 }
480
481 if (Trace) {
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000482 dbgs() << "Phi-up-to-phi map with intervening defs:\n";
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000483 for (auto I : PhiUp) {
484 dbgs() << "phi " << Print<NodeId>(I.first, DFG) << " -> {";
485 for (auto R : I.second)
486 dbgs() << ' ' << Print<NodeId>(R.first, DFG)
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000487 << Print<RegisterAggr>(R.second, DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000488 dbgs() << " }\n";
489 }
490 }
491
492 // Propagate the reached registers up in the phi chain.
493 //
494 // The following type of situation needs careful handling:
495 //
496 // phi d1<R1:0> (1)
497 // |
498 // ... d2<R1>
499 // |
500 // phi u3<R1:0> (2)
501 // |
502 // ... u4<R1>
503 //
504 // The phi node (2) defines a register pair R1:0, and reaches a "real"
505 // use u4 of just R1. The same phi node is also known to reach (upwards)
506 // the phi node (1). However, the use u4 is not reached by phi (1),
507 // because of the intervening definition d2 of R1. The data flow between
508 // phis (1) and (2) is restricted to R1:0 minus R1, i.e. R0.
509 //
510 // When propagating uses up the phi chains, get the all reaching defs
511 // for a given phi use, and traverse the list until the propagated ref
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000512 // is covered, or until reaching the final phi. Only assume that the
513 // reference reaches the phi in the latter case.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000514
515 for (unsigned i = 0; i < PhiUQ.size(); ++i) {
516 auto PA = DFG.addr<PhiNode*>(PhiUQ[i]);
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000517 NodeList PUs = PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG);
518 RefMap &RUM = RealUseMap[PA.Id];
519
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000520 for (NodeAddr<UseNode*> UA : PUs) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000521 std::map<NodeId,RegisterAggr> &PUM = PhiUp[UA.Id];
522 for (const std::pair<NodeId,RegisterAggr> &P : PUM) {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000523 bool Changed = false;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000524 const RegisterAggr &MidDefs = P.second;
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000525
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000526 // Collect the set UpReached of uses that are reached by the current
527 // phi PA, and are not covered by any intervening def between PA and
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000528 // the upward phi P.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000529 RegisterSet UpReached;
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000530 for (const std::pair<RegisterRef,NodeSet> &T : RUM) {
531 RegisterRef R = T.first;
Krzysztof Parzyszekc8b6eca2016-10-03 20:17:20 +0000532 if (UA.Addr->getFlags() & NodeAttrs::Shadow)
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000533 R = getRestrictedRegRef(UA);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000534 if (!MidDefs.hasCoverOf(R))
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000535 UpReached.insert(R);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000536 }
537 if (UpReached.empty())
538 continue;
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000539 // Update the set PRUs of real uses reached by the upward phi P with
540 // the actual set of uses (UpReached) that the P phi reaches.
541 RefMap &PRUs = RealUseMap[P.first];
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000542 for (RegisterRef R : UpReached) {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000543 unsigned Z = PRUs[R].size();
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000544 PRUs[R].insert(RUM[R].begin(), RUM[R].end());
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000545 Changed |= (PRUs[R].size() != Z);
546 }
547 if (Changed)
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000548 PhiUQ.push_back(P.first);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000549 }
550 }
551 }
552
553 if (Trace) {
554 dbgs() << "Real use map:\n";
555 for (auto I : RealUseMap) {
556 dbgs() << "phi " << Print<NodeId>(I.first, DFG);
557 NodeAddr<PhiNode*> PA = DFG.addr<PhiNode*>(I.first);
558 NodeList Ds = PA.Addr->members_if(DFG.IsRef<NodeAttrs::Def>, DFG);
559 if (!Ds.empty()) {
560 RegisterRef RR = NodeAddr<DefNode*>(Ds[0]).Addr->getRegRef();
561 dbgs() << '<' << Print<RegisterRef>(RR, DFG) << '>';
562 } else {
563 dbgs() << "<noreg>";
564 }
565 dbgs() << " -> " << Print<RefMap>(I.second, DFG) << '\n';
566 }
567 }
568}
569
570
571void Liveness::computeLiveIns() {
572 // Populate the node-to-block map. This speeds up the calculations
573 // significantly.
574 NBMap.clear();
575 for (NodeAddr<BlockNode*> BA : DFG.getFunc().Addr->members(DFG)) {
576 MachineBasicBlock *BB = BA.Addr->getCode();
577 for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG)) {
578 for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG))
579 NBMap.insert(std::make_pair(RA.Id, BB));
580 NBMap.insert(std::make_pair(IA.Id, BB));
581 }
582 }
583
584 MachineFunction &MF = DFG.getMF();
585
586 // Compute IDF first, then the inverse.
587 decltype(IIDF) IDF;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000588 for (MachineBasicBlock &B : MF) {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000589 auto F1 = MDF.find(&B);
590 if (F1 == MDF.end())
591 continue;
592 SetVector<MachineBasicBlock*> IDFB(F1->second.begin(), F1->second.end());
593 for (unsigned i = 0; i < IDFB.size(); ++i) {
594 auto F2 = MDF.find(IDFB[i]);
595 if (F2 != MDF.end())
596 IDFB.insert(F2->second.begin(), F2->second.end());
597 }
598 // Add B to the IDF(B). This will put B in the IIDF(B).
599 IDFB.insert(&B);
600 IDF[&B].insert(IDFB.begin(), IDFB.end());
601 }
602
603 for (auto I : IDF)
604 for (auto S : I.second)
605 IIDF[S].insert(I.first);
606
607 computePhiInfo();
608
609 NodeAddr<FuncNode*> FA = DFG.getFunc();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000610 NodeList Blocks = FA.Addr->members(DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000611
612 // Build the phi live-on-entry map.
613 for (NodeAddr<BlockNode*> BA : Blocks) {
614 MachineBasicBlock *MB = BA.Addr->getCode();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000615 RefMap &LON = PhiLON[MB];
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000616 for (auto P : BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG))
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000617 for (RefMap::value_type S : RealUseMap[P.Id])
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000618 LON[S.first].insert(S.second.begin(), S.second.end());
619 }
620
621 if (Trace) {
622 dbgs() << "Phi live-on-entry map:\n";
623 for (auto I : PhiLON)
624 dbgs() << "block #" << I.first->getNumber() << " -> "
625 << Print<RefMap>(I.second, DFG) << '\n';
626 }
627
628 // Build the phi live-on-exit map. Each phi node has some set of reached
629 // "real" uses. Propagate this set backwards into the block predecessors
630 // through the reaching defs of the corresponding phi uses.
631 for (NodeAddr<BlockNode*> BA : Blocks) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000632 NodeList Phis = BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000633 for (NodeAddr<PhiNode*> PA : Phis) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000634 RefMap &RUs = RealUseMap[PA.Id];
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000635 if (RUs.empty())
636 continue;
637
638 for (auto U : PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG)) {
639 NodeAddr<PhiUseNode*> UA = U;
640 if (UA.Addr->getReachingDef() == 0)
641 continue;
642
643 // Mark all reached "real" uses of P as live on exit in the
644 // predecessor.
645 // Remap all the RUs so that they have a correct reaching def.
646 auto PrA = DFG.addr<BlockNode*>(UA.Addr->getPredecessor());
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000647 RefMap &LOX = PhiLOX[PrA.Addr->getCode()];
648 for (RefMap::value_type R : RUs) {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000649 RegisterRef RR = R.first;
Krzysztof Parzyszekc8b6eca2016-10-03 20:17:20 +0000650 if (UA.Addr->getFlags() & NodeAttrs::Shadow)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000651 RR = getRestrictedRegRef(UA);
652 // The restricted ref may be different from the ref that was
653 // accessed in the "real use". This means that this phi use
654 // is not the one that carries this reference, so skip it.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000655 if (!DFG.alias(R.first, RR))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000656 continue;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000657 for (NodeAddr<DefNode*> D : getAllReachingDefs(RR, UA))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000658 LOX[RR].insert(D.Id);
659 }
660 } // for U : phi uses
661 } // for P : Phis
662 } // for B : Blocks
663
664 if (Trace) {
665 dbgs() << "Phi live-on-exit map:\n";
666 for (auto I : PhiLOX)
667 dbgs() << "block #" << I.first->getNumber() << " -> "
668 << Print<RefMap>(I.second, DFG) << '\n';
669 }
670
671 RefMap LiveIn;
672 traverse(&MF.front(), LiveIn);
673
674 // Add function live-ins to the live-in set of the function entry block.
675 auto &EntryIn = LiveMap[&MF.front()];
676 for (auto I = MRI.livein_begin(), E = MRI.livein_end(); I != E; ++I)
677 EntryIn.insert({I->first,0});
678
679 if (Trace) {
680 // Dump the liveness map
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000681 for (MachineBasicBlock &B : MF) {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000682 BitVector LV(TRI.getNumRegs());
683 for (auto I = B.livein_begin(), E = B.livein_end(); I != E; ++I)
684 LV.set(I->PhysReg);
685 dbgs() << "BB#" << B.getNumber() << "\t rec = {";
686 for (int x = LV.find_first(); x >= 0; x = LV.find_next(x))
687 dbgs() << ' ' << Print<RegisterRef>({unsigned(x),0}, DFG);
688 dbgs() << " }\n";
689 dbgs() << "\tcomp = " << Print<RegisterSet>(LiveMap[&B], DFG) << '\n';
690 }
691 }
692}
693
694
695void Liveness::resetLiveIns() {
696 for (auto &B : DFG.getMF()) {
697 // Remove all live-ins.
698 std::vector<unsigned> T;
699 for (auto I = B.livein_begin(), E = B.livein_end(); I != E; ++I)
700 T.push_back(I->PhysReg);
701 for (auto I : T)
702 B.removeLiveIn(I);
703 // Add the newly computed live-ins.
704 auto &LiveIns = LiveMap[&B];
705 for (auto I : LiveIns) {
706 assert(I.Sub == 0);
707 B.addLiveIn(I.Reg);
708 }
709 }
710}
711
712
713void Liveness::resetKills() {
714 for (auto &B : DFG.getMF())
715 resetKills(&B);
716}
717
718
719void Liveness::resetKills(MachineBasicBlock *B) {
720 auto CopyLiveIns = [] (MachineBasicBlock *B, BitVector &LV) -> void {
721 for (auto I = B->livein_begin(), E = B->livein_end(); I != E; ++I)
722 LV.set(I->PhysReg);
723 };
724
725 BitVector LiveIn(TRI.getNumRegs()), Live(TRI.getNumRegs());
726 CopyLiveIns(B, LiveIn);
727 for (auto SI : B->successors())
728 CopyLiveIns(SI, Live);
729
730 for (auto I = B->rbegin(), E = B->rend(); I != E; ++I) {
731 MachineInstr *MI = &*I;
732 if (MI->isDebugValue())
733 continue;
734
735 MI->clearKillInfo();
736 for (auto &Op : MI->operands()) {
Krzysztof Parzyszekf69ff712016-06-02 14:30:09 +0000737 // An implicit def of a super-register may not necessarily start a
738 // live range of it, since an implicit use could be used to keep parts
739 // of it live. Instead of analyzing the implicit operands, ignore
740 // implicit defs.
741 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000742 continue;
743 unsigned R = Op.getReg();
744 if (!TargetRegisterInfo::isPhysicalRegister(R))
745 continue;
746 for (MCSubRegIterator SR(R, &TRI, true); SR.isValid(); ++SR)
747 Live.reset(*SR);
748 }
749 for (auto &Op : MI->operands()) {
750 if (!Op.isReg() || !Op.isUse())
751 continue;
752 unsigned R = Op.getReg();
753 if (!TargetRegisterInfo::isPhysicalRegister(R))
754 continue;
755 bool IsLive = false;
Krzysztof Parzyszek16331f02016-04-20 14:33:23 +0000756 for (MCRegAliasIterator AR(R, &TRI, true); AR.isValid(); ++AR) {
757 if (!Live[*AR])
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000758 continue;
759 IsLive = true;
760 break;
761 }
762 if (IsLive)
763 continue;
764 Op.setIsKill(true);
765 for (MCSubRegIterator SR(R, &TRI, true); SR.isValid(); ++SR)
766 Live.set(*SR);
767 }
768 }
769}
770
771
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000772RegisterRef Liveness::getRestrictedRegRef(NodeAddr<RefNode*> RA) const {
773 assert(DFG.IsRef<NodeAttrs::Use>(RA));
774 if (RA.Addr->getFlags() & NodeAttrs::Shadow) {
775 NodeId RD = RA.Addr->getReachingDef();
776 assert(RD);
777 RA = DFG.addr<DefNode*>(RD);
778 }
779 return RA.Addr->getRegRef();
780}
781
782
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000783// Helper function to obtain the basic block containing the reaching def
784// of the given use.
785MachineBasicBlock *Liveness::getBlockWithRef(NodeId RN) const {
786 auto F = NBMap.find(RN);
787 if (F != NBMap.end())
788 return F->second;
789 llvm_unreachable("Node id not in map");
790}
791
792
793void Liveness::traverse(MachineBasicBlock *B, RefMap &LiveIn) {
794 // The LiveIn map, for each (physical) register, contains the set of live
795 // reaching defs of that register that are live on entry to the associated
796 // block.
797
798 // The summary of the traversal algorithm:
799 //
800 // R is live-in in B, if there exists a U(R), such that rdef(R) dom B
801 // and (U \in IDF(B) or B dom U).
802 //
803 // for (C : children) {
804 // LU = {}
805 // traverse(C, LU)
806 // LiveUses += LU
807 // }
808 //
809 // LiveUses -= Defs(B);
810 // LiveUses += UpwardExposedUses(B);
811 // for (C : IIDF[B])
812 // for (U : LiveUses)
813 // if (Rdef(U) dom C)
814 // C.addLiveIn(U)
815 //
816
817 // Go up the dominator tree (depth-first).
818 MachineDomTreeNode *N = MDT.getNode(B);
819 for (auto I : *N) {
820 RefMap L;
821 MachineBasicBlock *SB = I->getBlock();
822 traverse(SB, L);
823
824 for (auto S : L)
825 LiveIn[S.first].insert(S.second.begin(), S.second.end());
826 }
827
828 if (Trace) {
Krzysztof Parzyszekc8b6eca2016-10-03 20:17:20 +0000829 dbgs() << "\n-- BB#" << B->getNumber() << ": " << LLVM_FUNCTION_NAME
830 << " after recursion into: {";
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000831 for (auto I : *N)
832 dbgs() << ' ' << I->getBlock()->getNumber();
Krzysztof Parzyszekc8b6eca2016-10-03 20:17:20 +0000833 dbgs() << " }\n";
834 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
835 dbgs() << " Local: " << Print<RegisterSet>(LiveMap[B], DFG) << '\n';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000836 }
837
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000838 // Add reaching defs of phi uses that are live on exit from this block.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000839 RefMap &PUs = PhiLOX[B];
840 for (auto S : PUs)
841 LiveIn[S.first].insert(S.second.begin(), S.second.end());
842
843 if (Trace) {
844 dbgs() << "after LOX\n";
845 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
846 dbgs() << " Local: " << Print<RegisterSet>(LiveMap[B], DFG) << '\n';
847 }
848
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000849 // The LiveIn map at this point has all defs that are live-on-exit from B,
850 // as if they were live-on-entry to B. First, we need to filter out all
851 // defs that are present in this block. Then we will add reaching defs of
852 // all upward-exposed uses.
853
854 // To filter out the defs, first make a copy of LiveIn, and then re-populate
855 // LiveIn with the defs that should remain.
856 RefMap LiveInCopy = LiveIn;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000857 LiveIn.clear();
858
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000859 for (const std::pair<RegisterRef,NodeSet> &LE : LiveInCopy) {
860 RegisterRef LRef = LE.first;
861 NodeSet &NewDefs = LiveIn[LRef]; // To be filled.
862 const NodeSet &OldDefs = LE.second;
863 for (NodeId R : OldDefs) {
864 // R is a def node that was live-on-exit
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000865 auto DA = DFG.addr<DefNode*>(R);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000866 NodeAddr<InstrNode*> IA = DA.Addr->getOwner(DFG);
867 NodeAddr<BlockNode*> BA = IA.Addr->getOwner(DFG);
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000868 if (B != BA.Addr->getCode()) {
869 // Defs from a different block need to be preserved. Defs from this
870 // block will need to be processed further, except for phi defs, the
871 // liveness of which is handled through the PhiLON/PhiLOX maps.
872 NewDefs.insert(R);
873 continue;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000874 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000875
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000876 // Defs from this block need to stop the liveness from being
877 // propagated upwards. This only applies to non-preserving defs,
878 // and to the parts of the register actually covered by those defs.
879 // (Note that phi defs should always be preserving.)
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000880 RegisterAggr RRs(DFG.getLMI(), TRI);
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000881
882 if (!DFG.IsPreservingDef(DA)) {
883 assert(!(IA.Addr->getFlags() & NodeAttrs::Phi));
884 // DA is a non-phi def that is live-on-exit from this block, and
885 // that is also located in this block. LRef is a register ref
886 // whose use this def reaches. If DA covers LRef, then no part
887 // of LRef is exposed upwards.
888 if (RRs.insert(DA.Addr->getRegRef()).hasCoverOf(LRef))
889 continue;
890 }
891
892 // DA itself was not sufficient to cover LRef. In general, it is
893 // the last in a chain of aliased defs before the exit from this block.
894 // There could be other defs in this block that are a part of that
895 // chain. Check that now: accumulate the registers from these defs,
896 // and if they all together cover LRef, it is not live-on-entry.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000897 for (NodeAddr<DefNode*> TA : getAllReachingDefs(DA)) {
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000898 // DefNode -> InstrNode -> BlockNode.
899 NodeAddr<InstrNode*> ITA = TA.Addr->getOwner(DFG);
900 NodeAddr<BlockNode*> BTA = ITA.Addr->getOwner(DFG);
901 // Reaching defs are ordered in the upward direction.
902 if (BTA.Addr->getCode() != B) {
903 // We have reached past the beginning of B, and the accumulated
904 // registers are not covering LRef. The first def from the
905 // upward chain will be live.
906 // FIXME: This is where the live-in lane mask could be set.
907 // It cannot be be changed directly, because it is a part
908 // of the map key (LRef).
909 NewDefs.insert(TA.Id);
910 break;
911 }
912
913 // TA is in B. Only add this def to the accumulated cover if it is
914 // not preserving.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000915 if (!(TA.Addr->getFlags() & NodeAttrs::Preserving))
916 RRs.insert(TA.Addr->getRegRef());
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000917 // If this is enough to cover LRef, then stop.
918 if (RRs.hasCoverOf(LRef))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000919 break;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000920 }
921 }
922 }
923
924 emptify(LiveIn);
925
926 if (Trace) {
927 dbgs() << "after defs in block\n";
928 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
929 dbgs() << " Local: " << Print<RegisterSet>(LiveMap[B], DFG) << '\n';
930 }
931
932 // Scan the block for upward-exposed uses and add them to the tracking set.
933 for (auto I : DFG.getFunc().Addr->findBlock(B, DFG).Addr->members(DFG)) {
934 NodeAddr<InstrNode*> IA = I;
935 if (IA.Addr->getKind() != NodeAttrs::Stmt)
936 continue;
937 for (NodeAddr<UseNode*> UA : IA.Addr->members_if(DFG.IsUse, DFG)) {
938 RegisterRef RR = UA.Addr->getRegRef();
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000939 if (UA.Addr->getFlags() & NodeAttrs::Undef)
940 continue;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000941 for (NodeAddr<DefNode*> D : getAllReachingDefs(UA))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000942 if (getBlockWithRef(D.Id) != B)
943 LiveIn[RR].insert(D.Id);
944 }
945 }
946
947 if (Trace) {
948 dbgs() << "after uses in block\n";
949 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
950 dbgs() << " Local: " << Print<RegisterSet>(LiveMap[B], DFG) << '\n';
951 }
952
953 // Phi uses should not be propagated up the dominator tree, since they
954 // are not dominated by their corresponding reaching defs.
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000955 RegisterSet &Local = LiveMap[B];
956 RefMap &LON = PhiLON[B];
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000957 for (auto R : LON)
958 Local.insert(R.first);
959
960 if (Trace) {
961 dbgs() << "after phi uses in block\n";
962 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
963 dbgs() << " Local: " << Print<RegisterSet>(Local, DFG) << '\n';
964 }
965
966 for (auto C : IIDF[B]) {
967 auto &LiveC = LiveMap[C];
968 for (auto S : LiveIn)
969 for (auto R : S.second)
970 if (MDT.properlyDominates(getBlockWithRef(R), C))
971 LiveC.insert(S.first);
972 }
973}
974
975
976void Liveness::emptify(RefMap &M) {
977 for (auto I = M.begin(), E = M.end(); I != E; )
978 I = I->second.empty() ? M.erase(I) : std::next(I);
979}
980