blob: 6710e5083d53dff2c86e9614ec620403e6ffa728 [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 << '{';
Krzysztof Parzyszek459a1c92016-10-06 13:05:46 +000044 for (auto &I : P.Obj) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +000045 OS << ' ' << PrintReg(I.first, &P.G.getTRI()) << '{';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +000046 for (auto J = I.second.begin(), E = I.second.end(); J != E; ) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +000047 OS << Print<NodeId>(J->first, P.G) << PrintLaneMaskOpt(J->second);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +000048 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 Parzyszekcac10f92017-02-16 19:28:06 +000088 NodeAddr<RefNode*> RefA, bool TopShadows, bool FullChain,
89 const RegisterAggr &DefRRs) {
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +000090 NodeList RDefs; // Return value.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +000091 SetVector<NodeId> DefQ;
92 SetVector<NodeId> Owners;
93
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +000094 // Dead defs will be treated as if they were live, since they are actually
95 // on the data-flow path. They cannot be ignored because even though they
96 // do not generate meaningful values, they still modify registers.
97
98 // If the reference is undefined, there is nothing to do.
99 if (RefA.Addr->getFlags() & NodeAttrs::Undef)
100 return RDefs;
101
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000102 // The initial queue should not have reaching defs for shadows. The
103 // whole point of a shadow is that it will have a reaching def that
104 // is not aliased to the reaching defs of the related shadows.
105 NodeId Start = RefA.Id;
106 auto SNA = DFG.addr<RefNode*>(Start);
107 if (NodeId RD = SNA.Addr->getReachingDef())
108 DefQ.insert(RD);
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000109 if (TopShadows) {
110 for (auto S : DFG.getRelatedRefs(RefA.Addr->getOwner(DFG), RefA))
111 if (NodeId RD = NodeAddr<RefNode*>(S).Addr->getReachingDef())
112 DefQ.insert(RD);
113 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000114
115 // Collect all the reaching defs, going up until a phi node is encountered,
116 // or there are no more reaching defs. From this set, the actual set of
117 // reaching defs will be selected.
118 // The traversal upwards must go on until a covering def is encountered.
119 // It is possible that a collection of non-covering (individually) defs
120 // will be sufficient, but keep going until a covering one is found.
121 for (unsigned i = 0; i < DefQ.size(); ++i) {
122 auto TA = DFG.addr<DefNode*>(DefQ[i]);
123 if (TA.Addr->getFlags() & NodeAttrs::PhiRef)
124 continue;
125 // Stop at the covering/overwriting def of the initial register reference.
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000126 RegisterRef RR = TA.Addr->getRegRef(DFG);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000127 if (!DFG.IsPreservingDef(TA))
Krzysztof Parzyszek49ffff12017-01-30 17:46:56 +0000128 if (RegisterAggr::isCoverOf(RR, RefRR, PRI))
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000129 continue;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000130 // Get the next level of reaching defs. This will include multiple
131 // reaching defs for shadows.
132 for (auto S : DFG.getRelatedRefs(TA.Addr->getOwner(DFG), TA))
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000133 if (NodeId RD = NodeAddr<RefNode*>(S).Addr->getReachingDef())
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000134 DefQ.insert(RD);
135 }
136
137 // Remove all non-phi defs that are not aliased to RefRR, and collect
138 // the owners of the remaining defs.
139 SetVector<NodeId> Defs;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000140 for (NodeId N : DefQ) {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000141 auto TA = DFG.addr<DefNode*>(N);
142 bool IsPhi = TA.Addr->getFlags() & NodeAttrs::PhiRef;
Krzysztof Parzyszek49ffff12017-01-30 17:46:56 +0000143 if (!IsPhi && !PRI.alias(RefRR, TA.Addr->getRegRef(DFG)))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000144 continue;
145 Defs.insert(TA.Id);
146 Owners.insert(TA.Addr->getOwner(DFG).Id);
147 }
148
149 // Return the MachineBasicBlock containing a given instruction.
150 auto Block = [this] (NodeAddr<InstrNode*> IA) -> MachineBasicBlock* {
151 if (IA.Addr->getKind() == NodeAttrs::Stmt)
152 return NodeAddr<StmtNode*>(IA).Addr->getCode()->getParent();
153 assert(IA.Addr->getKind() == NodeAttrs::Phi);
154 NodeAddr<PhiNode*> PA = IA;
155 NodeAddr<BlockNode*> BA = PA.Addr->getOwner(DFG);
156 return BA.Addr->getCode();
157 };
158 // Less(A,B) iff instruction A is further down in the dominator tree than B.
159 auto Less = [&Block,this] (NodeId A, NodeId B) -> bool {
160 if (A == B)
161 return false;
162 auto OA = DFG.addr<InstrNode*>(A), OB = DFG.addr<InstrNode*>(B);
163 MachineBasicBlock *BA = Block(OA), *BB = Block(OB);
164 if (BA != BB)
165 return MDT.dominates(BB, BA);
166 // They are in the same block.
167 bool StmtA = OA.Addr->getKind() == NodeAttrs::Stmt;
168 bool StmtB = OB.Addr->getKind() == NodeAttrs::Stmt;
169 if (StmtA) {
170 if (!StmtB) // OB is a phi and phis dominate statements.
171 return true;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000172 MachineInstr *CA = NodeAddr<StmtNode*>(OA).Addr->getCode();
173 MachineInstr *CB = NodeAddr<StmtNode*>(OB).Addr->getCode();
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000174 // The order must be linear, so tie-break such equalities.
175 if (CA == CB)
176 return A < B;
177 return MDT.dominates(CB, CA);
178 } else {
179 // OA is a phi.
180 if (StmtB)
181 return false;
182 // Both are phis. There is no ordering between phis (in terms of
183 // the data-flow), so tie-break this via node id comparison.
184 return A < B;
185 }
186 };
187
188 std::vector<NodeId> Tmp(Owners.begin(), Owners.end());
189 std::sort(Tmp.begin(), Tmp.end(), Less);
190
191 // The vector is a list of instructions, so that defs coming from
192 // the same instruction don't need to be artificially ordered.
193 // Then, when computing the initial segment, and iterating over an
194 // instruction, pick the defs that contribute to the covering (i.e. is
195 // not covered by previously added defs). Check the defs individually,
196 // i.e. first check each def if is covered or not (without adding them
197 // to the tracking set), and then add all the selected ones.
198
199 // The reason for this is this example:
200 // *d1<A>, *d2<B>, ... Assume A and B are aliased (can happen in phi nodes).
201 // *d3<C> If A \incl BuC, and B \incl AuC, then *d2 would be
202 // covered if we added A first, and A would be covered
203 // if we added B first.
204
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000205 RegisterAggr RRs(DefRRs);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000206
207 auto DefInSet = [&Defs] (NodeAddr<RefNode*> TA) -> bool {
208 return TA.Addr->getKind() == NodeAttrs::Def &&
209 Defs.count(TA.Id);
210 };
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000211 for (NodeId T : Tmp) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000212 if (!FullChain && RRs.hasCoverOf(RefRR))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000213 break;
214 auto TA = DFG.addr<InstrNode*>(T);
215 bool IsPhi = DFG.IsCode<NodeAttrs::Phi>(TA);
216 NodeList Ds;
217 for (NodeAddr<DefNode*> DA : TA.Addr->members_if(DefInSet, DFG)) {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000218 RegisterRef QR = DA.Addr->getRegRef(DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000219 // Add phi defs even if they are covered by subsequent defs. This is
220 // for cases where the reached use is not covered by any of the defs
221 // encountered so far: the phi def is needed to expose the liveness
222 // of that use to the entry of the block.
223 // Example:
224 // phi d1<R3>(,d2,), ... Phi def d1 is covered by d2.
225 // d2<R3>(d1,,u3), ...
226 // ..., u3<D1>(d2) This use needs to be live on entry.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000227 if (FullChain || IsPhi || !RRs.hasCoverOf(QR))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000228 Ds.push_back(DA);
229 }
230 RDefs.insert(RDefs.end(), Ds.begin(), Ds.end());
231 for (NodeAddr<DefNode*> DA : Ds) {
232 // When collecting a full chain of definitions, do not consider phi
233 // defs to actually define a register.
234 uint16_t Flags = DA.Addr->getFlags();
235 if (!FullChain || !(Flags & NodeAttrs::PhiRef))
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000236 if (!(Flags & NodeAttrs::Preserving)) // Don't care about Undef here.
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000237 RRs.insert(DA.Addr->getRegRef(DFG));
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000238 }
239 }
240
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +0000241 auto DeadP = [](const NodeAddr<DefNode*> DA) -> bool {
242 return DA.Addr->getFlags() & NodeAttrs::Dead;
243 };
244 RDefs.resize(std::distance(RDefs.begin(), remove_if(RDefs, DeadP)));
245
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000246 return RDefs;
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.
Krzysztof Parzyszek49ffff12017-01-30 17:46:56 +0000254 RegisterAggr DefRRs(PRI);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000255 for (NodeId D : Defs) {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000256 const auto DA = DFG.addr<const DefNode*>(D);
257 if (!(DA.Addr->getFlags() & NodeAttrs::PhiRef))
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000258 DefRRs.insert(DA.Addr->getRegRef(DFG));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000259 }
260
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000261 NodeList RDs = getAllReachingDefs(RefRR, RefA, false, true, DefRRs);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000262 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;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000267 for (NodeAddr<NodeBase*> R : RDs)
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000268 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,
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000292 NodeAddr<DefNode*> DefA, const RegisterAggr &DefRRs) {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000293 NodeSet Uses;
294
295 // If the original register is already covered by all the intervening
296 // defs, no more uses can be reached.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000297 if (DefRRs.hasCoverOf(RefRR))
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)) {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000307 RegisterRef UR = UA.Addr->getRegRef(DFG);
Krzysztof Parzyszek49ffff12017-01-30 17:46:56 +0000308 if (PRI.alias(RefRR, UR) && !DefRRs.hasCoverOf(UR))
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();
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000318 RegisterRef DR = DA.Addr->getRegRef(DFG);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000319 // 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 Parzyszek49ffff12017-01-30 17:46:56 +0000321 if (DefRRs.hasCoverOf(DR) || !PRI.alias(RefRR, DR))
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 {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000328 RegisterAggr NewDefRRs = DefRRs;
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000329 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();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000343 NodeList Blocks = FA.Addr->members(DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000344 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)
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000350 std::map<NodeId,std::map<NodeId,RegisterAggr>> PhiUp;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000351 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").
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000357 RefMap &RealUses = RealUseMap[PhiA.Id];
358 NodeList PhiRefs = PhiA.Addr->members(DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000359
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;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000364 for (NodeAddr<RefNode*> R : PhiRefs) {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000365 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();
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000385 if ((F & (NodeAttrs::Undef | NodeAttrs::PhiRef)) == 0) {
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000386 RegisterRef R = PRI.normalize(A.Addr->getRegRef(DFG));
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000387 RealUses[R.Reg].insert({A.Id,R.Mask});
Krzysztof Parzyszek09a86382017-01-23 23:03:49 +0000388 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000389 UN = A.Addr->getSibling();
390 }
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000391 // Visit all reached defs, and add them to the queue. These defs may
392 // override some of the uses collected here, but that will be handled
393 // later.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000394 NodeId DN = DA.Addr->getReachedDef();
395 while (DN != 0) {
396 NodeAddr<DefNode*> A = DFG.addr<DefNode*>(DN);
397 for (auto T : DFG.getRelatedRefs(A.Addr->getOwner(DFG), A)) {
398 uint16_t Flags = NodeAddr<DefNode*>(T).Addr->getFlags();
399 // Must traverse the reached-def chain. Consider:
400 // def(D0) -> def(R0) -> def(R0) -> use(D0)
401 // The reachable use of D0 passes through a def of R0.
402 if (!(Flags & NodeAttrs::PhiRef))
403 DefQ.insert(T.Id);
404 }
405 DN = A.Addr->getSibling();
406 }
407 }
408 // Filter out these uses that appear to be reachable, but really
409 // are not. For example:
410 //
411 // R1:0 = d1
412 // = R1:0 u2 Reached by d1.
413 // R0 = d3
414 // = R1:0 u4 Still reached by d1: indirectly through
415 // the def d3.
416 // R1 = d5
417 // = R1:0 u6 Not reached by d1 (covered collectively
418 // by d3 and d5), but following reached
419 // defs and uses from d1 will lead here.
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000420 auto InPhiDefs = [&PhiDefs] (NodeAddr<DefNode*> DA) -> bool {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000421 return PhiDefs.count(DA.Id);
422 };
423 for (auto UI = RealUses.begin(), UE = RealUses.end(); UI != UE; ) {
424 // For each reached register UI->first, there is a set UI->second, of
425 // uses of it. For each such use, check if it is reached by this phi,
426 // i.e. check if the set of its reaching uses intersects the set of
427 // this phi's defs.
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000428 NodeRefSet &Uses = UI->second;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000429 for (auto I = Uses.begin(), E = Uses.end(); I != E; ) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000430 auto UA = DFG.addr<UseNode*>(I->first);
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +0000431 // Undef flag is checked above.
432 assert((UA.Addr->getFlags() & NodeAttrs::Undef) == 0);
Krzysztof Parzyszek09a86382017-01-23 23:03:49 +0000433 RegisterRef R(UI->first, I->second);
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000434 NodeList RDs = getAllReachingDefs(R, UA);
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000435 // If none of the reaching defs of R are from this phi, remove this
436 // use of R.
437 I = any_of(RDs, InPhiDefs) ? std::next(I) : Uses.erase(I);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000438 }
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000439 UI = Uses.empty() ? RealUses.erase(UI) : std::next(UI);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000440 }
441
442 // If this phi reaches some "real" uses, add it to the queue for upward
443 // propagation.
444 if (!RealUses.empty())
445 PhiUQ.push_back(PhiA.Id);
446
447 // Go over all phi uses and check if the reaching def is another phi.
448 // Collect the phis that are among the reaching defs of these uses.
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000449 // While traversing the list of reaching defs for each phi use, accumulate
450 // the set of registers defined between this phi (PhiA) and the owner phi
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000451 // of the reaching def.
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000452 NodeSet SeenUses;
Krzysztof Parzyszeka1218722016-09-08 20:48:42 +0000453
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000454 for (auto I : PhiRefs) {
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000455 if (!DFG.IsRef<NodeAttrs::Use>(I) || SeenUses.count(I.Id))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000456 continue;
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000457 NodeAddr<PhiUseNode*> PUA = I;
458 if (PUA.Addr->getReachingDef() == 0)
459 continue;
Krzysztof Parzyszeka1218722016-09-08 20:48:42 +0000460
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000461 RegisterRef UR = PUA.Addr->getRegRef(DFG);
462 NodeList Ds = getAllReachingDefs(UR, PUA, true, false, NoRegs);
463 RegisterAggr DefRRs(PRI);
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000464
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000465 for (NodeAddr<DefNode*> D : Ds) {
466 if (D.Addr->getFlags() & NodeAttrs::PhiRef) {
467 NodeId RP = D.Addr->getOwner(DFG).Id;
468 std::map<NodeId,RegisterAggr> &M = PhiUp[PUA.Id];
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 Parzyszek2db0c8b2016-09-07 20:37:05 +0000474 }
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000475 DefRRs.insert(D.Addr->getRegRef(DFG));
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000476 }
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000477
478 for (NodeAddr<PhiUseNode*> T : DFG.getRelatedRefs(PhiA, PUA))
479 SeenUses.insert(T.Id);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000480 }
481 }
482
483 if (Trace) {
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000484 dbgs() << "Phi-up-to-phi map with intervening defs:\n";
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000485 for (auto I : PhiUp) {
486 dbgs() << "phi " << Print<NodeId>(I.first, DFG) << " -> {";
487 for (auto R : I.second)
488 dbgs() << ' ' << Print<NodeId>(R.first, DFG)
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000489 << Print<RegisterAggr>(R.second, DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000490 dbgs() << " }\n";
491 }
492 }
493
494 // Propagate the reached registers up in the phi chain.
495 //
496 // The following type of situation needs careful handling:
497 //
498 // phi d1<R1:0> (1)
499 // |
500 // ... d2<R1>
501 // |
502 // phi u3<R1:0> (2)
503 // |
504 // ... u4<R1>
505 //
506 // The phi node (2) defines a register pair R1:0, and reaches a "real"
507 // use u4 of just R1. The same phi node is also known to reach (upwards)
508 // the phi node (1). However, the use u4 is not reached by phi (1),
509 // because of the intervening definition d2 of R1. The data flow between
510 // phis (1) and (2) is restricted to R1:0 minus R1, i.e. R0.
511 //
512 // When propagating uses up the phi chains, get the all reaching defs
513 // for a given phi use, and traverse the list until the propagated ref
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000514 // is covered, or until reaching the final phi. Only assume that the
515 // reference reaches the phi in the latter case.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000516
517 for (unsigned i = 0; i < PhiUQ.size(); ++i) {
518 auto PA = DFG.addr<PhiNode*>(PhiUQ[i]);
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000519 NodeList PUs = PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG);
520 RefMap &RUM = RealUseMap[PA.Id];
521
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000522 for (NodeAddr<UseNode*> UA : PUs) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000523 std::map<NodeId,RegisterAggr> &PUM = PhiUp[UA.Id];
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000524 RegisterRef UR = PRI.normalize(UA.Addr->getRegRef(DFG));
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000525 for (const std::pair<NodeId,RegisterAggr> &P : PUM) {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000526 bool Changed = false;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000527 const RegisterAggr &MidDefs = P.second;
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000528
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000529 // Collect the set PropUp of uses that are reached by the current
530 // phi PA, and are not covered by any intervening def between the
531 // currently visited use UA and the the upward phi P.
532
533 if (MidDefs.hasCoverOf(UR))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000534 continue;
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000535
536 // General algorithm:
537 // for each (R,U) : U is use node of R, U is reached by PA
538 // if MidDefs does not cover (R,U)
539 // then add (R-MidDefs,U) to RealUseMap[P]
540 //
541 for (const std::pair<RegisterId,NodeRefSet> &T : RUM) {
542 RegisterRef R = DFG.restrictRef(RegisterRef(T.first), UR);
543 if (!R)
544 continue;
545 for (std::pair<NodeId,LaneBitmask> V : T.second) {
546 RegisterRef S = DFG.restrictRef(RegisterRef(R.Reg, V.second), R);
547 if (!S)
548 continue;
549 if (RegisterRef SS = MidDefs.clearIn(S)) {
550 NodeRefSet &RS = RealUseMap[P.first][SS.Reg];
551 Changed |= RS.insert({V.first,SS.Mask}).second;
552 }
553 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000554 }
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000555
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000556 if (Changed)
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000557 PhiUQ.push_back(P.first);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000558 }
559 }
560 }
561
562 if (Trace) {
563 dbgs() << "Real use map:\n";
564 for (auto I : RealUseMap) {
565 dbgs() << "phi " << Print<NodeId>(I.first, DFG);
566 NodeAddr<PhiNode*> PA = DFG.addr<PhiNode*>(I.first);
567 NodeList Ds = PA.Addr->members_if(DFG.IsRef<NodeAttrs::Def>, DFG);
568 if (!Ds.empty()) {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000569 RegisterRef RR = NodeAddr<DefNode*>(Ds[0]).Addr->getRegRef(DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000570 dbgs() << '<' << Print<RegisterRef>(RR, DFG) << '>';
571 } else {
572 dbgs() << "<noreg>";
573 }
574 dbgs() << " -> " << Print<RefMap>(I.second, DFG) << '\n';
575 }
576 }
577}
578
579
580void Liveness::computeLiveIns() {
581 // Populate the node-to-block map. This speeds up the calculations
582 // significantly.
583 NBMap.clear();
584 for (NodeAddr<BlockNode*> BA : DFG.getFunc().Addr->members(DFG)) {
585 MachineBasicBlock *BB = BA.Addr->getCode();
586 for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG)) {
587 for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG))
588 NBMap.insert(std::make_pair(RA.Id, BB));
589 NBMap.insert(std::make_pair(IA.Id, BB));
590 }
591 }
592
593 MachineFunction &MF = DFG.getMF();
594
595 // Compute IDF first, then the inverse.
596 decltype(IIDF) IDF;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000597 for (MachineBasicBlock &B : MF) {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000598 auto F1 = MDF.find(&B);
599 if (F1 == MDF.end())
600 continue;
601 SetVector<MachineBasicBlock*> IDFB(F1->second.begin(), F1->second.end());
602 for (unsigned i = 0; i < IDFB.size(); ++i) {
603 auto F2 = MDF.find(IDFB[i]);
604 if (F2 != MDF.end())
605 IDFB.insert(F2->second.begin(), F2->second.end());
606 }
607 // Add B to the IDF(B). This will put B in the IIDF(B).
608 IDFB.insert(&B);
609 IDF[&B].insert(IDFB.begin(), IDFB.end());
610 }
611
612 for (auto I : IDF)
613 for (auto S : I.second)
614 IIDF[S].insert(I.first);
615
616 computePhiInfo();
617
618 NodeAddr<FuncNode*> FA = DFG.getFunc();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000619 NodeList Blocks = FA.Addr->members(DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000620
621 // Build the phi live-on-entry map.
622 for (NodeAddr<BlockNode*> BA : Blocks) {
623 MachineBasicBlock *MB = BA.Addr->getCode();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000624 RefMap &LON = PhiLON[MB];
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000625 for (auto P : BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG))
Krzysztof Parzyszek459a1c92016-10-06 13:05:46 +0000626 for (const RefMap::value_type &S : RealUseMap[P.Id])
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000627 LON[S.first].insert(S.second.begin(), S.second.end());
628 }
629
630 if (Trace) {
631 dbgs() << "Phi live-on-entry map:\n";
Krzysztof Parzyszek459a1c92016-10-06 13:05:46 +0000632 for (auto &I : PhiLON)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000633 dbgs() << "block #" << I.first->getNumber() << " -> "
634 << Print<RefMap>(I.second, DFG) << '\n';
635 }
636
637 // Build the phi live-on-exit map. Each phi node has some set of reached
638 // "real" uses. Propagate this set backwards into the block predecessors
639 // through the reaching defs of the corresponding phi uses.
640 for (NodeAddr<BlockNode*> BA : Blocks) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000641 NodeList Phis = BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000642 for (NodeAddr<PhiNode*> PA : Phis) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000643 RefMap &RUs = RealUseMap[PA.Id];
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000644 if (RUs.empty())
645 continue;
646
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000647 NodeSet SeenUses;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000648 for (auto U : PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG)) {
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000649 if (!SeenUses.insert(U.Id).second)
650 continue;
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000651 NodeAddr<PhiUseNode*> PUA = U;
652 if (PUA.Addr->getReachingDef() == 0)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000653 continue;
654
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000655 // Each phi has some set (possibly empty) of reached "real" uses,
656 // that is, uses that are part of the compiled program. Such a use
657 // may be located in some farther block, but following a chain of
658 // reaching defs will eventually lead to this phi.
659 // Any chain of reaching defs may fork at a phi node, but there
660 // will be a path upwards that will lead to this phi. Now, this
661 // chain will need to fork at this phi, since some of the reached
662 // uses may have definitions joining in from multiple predecessors.
663 // For each reached "real" use, identify the set of reaching defs
664 // coming from each predecessor P, and add them to PhiLOX[P].
665 //
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000666 auto PrA = DFG.addr<BlockNode*>(PUA.Addr->getPredecessor());
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000667 RefMap &LOX = PhiLOX[PrA.Addr->getCode()];
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000668
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000669 for (const std::pair<RegisterId,NodeRefSet> &RS : RUs) {
670 // We need to visit each individual use.
671 for (std::pair<NodeId,LaneBitmask> P : RS.second) {
672 // Create a register ref corresponding to the use, and find
673 // all reaching defs starting from the phi use, and treating
674 // all related shadows as a single use cluster.
675 RegisterRef S(RS.first, P.second);
676 NodeList Ds = getAllReachingDefs(S, PUA, true, false, NoRegs);
677 for (NodeAddr<DefNode*> D : Ds)
678 LOX[S.Reg].insert({D.Id, S.Mask});
679 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000680 }
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000681
682 for (NodeAddr<PhiUseNode*> T : DFG.getRelatedRefs(PA, PUA))
683 SeenUses.insert(T.Id);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000684 } // for U : phi uses
685 } // for P : Phis
686 } // for B : Blocks
687
688 if (Trace) {
689 dbgs() << "Phi live-on-exit map:\n";
Krzysztof Parzyszek459a1c92016-10-06 13:05:46 +0000690 for (auto &I : PhiLOX)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000691 dbgs() << "block #" << I.first->getNumber() << " -> "
692 << Print<RefMap>(I.second, DFG) << '\n';
693 }
694
695 RefMap LiveIn;
696 traverse(&MF.front(), LiveIn);
697
698 // Add function live-ins to the live-in set of the function entry block.
Krzysztof Parzyszekb561cf92017-01-30 16:20:30 +0000699 LiveMap[&MF.front()].insert(DFG.getLiveIns());
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000700
701 if (Trace) {
702 // Dump the liveness map
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000703 for (MachineBasicBlock &B : MF) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000704 std::vector<RegisterRef> LV;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000705 for (auto I = B.livein_begin(), E = B.livein_end(); I != E; ++I)
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000706 LV.push_back(RegisterRef(I->PhysReg, I->LaneMask));
707 std::sort(LV.begin(), LV.end());
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000708 dbgs() << "BB#" << B.getNumber() << "\t rec = {";
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000709 for (auto I : LV)
710 dbgs() << ' ' << Print<RegisterRef>(I, DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000711 dbgs() << " }\n";
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000712 //dbgs() << "\tcomp = " << Print<RegisterAggr>(LiveMap[&B], DFG) << '\n';
713
714 LV.clear();
715 for (std::pair<RegisterId,LaneBitmask> P : LiveMap[&B]) {
716 MCSubRegIndexIterator S(P.first, &TRI);
717 if (!S.isValid()) {
718 LV.push_back(RegisterRef(P.first));
719 continue;
720 }
721 do {
722 LaneBitmask M = TRI.getSubRegIndexLaneMask(S.getSubRegIndex());
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000723 if ((M & P.second).any())
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000724 LV.push_back(RegisterRef(S.getSubReg()));
725 ++S;
726 } while (S.isValid());
727 }
728 std::sort(LV.begin(), LV.end());
729 dbgs() << "\tcomp = {";
730 for (auto I : LV)
731 dbgs() << ' ' << Print<RegisterRef>(I, DFG);
732 dbgs() << " }\n";
733
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000734 }
735 }
736}
737
738
739void Liveness::resetLiveIns() {
740 for (auto &B : DFG.getMF()) {
741 // Remove all live-ins.
742 std::vector<unsigned> T;
743 for (auto I = B.livein_begin(), E = B.livein_end(); I != E; ++I)
744 T.push_back(I->PhysReg);
745 for (auto I : T)
746 B.removeLiveIn(I);
747 // Add the newly computed live-ins.
748 auto &LiveIns = LiveMap[&B];
749 for (auto I : LiveIns) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000750 B.addLiveIn({MCPhysReg(I.first), I.second});
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000751 }
752 }
753}
754
755
756void Liveness::resetKills() {
757 for (auto &B : DFG.getMF())
758 resetKills(&B);
759}
760
761
762void Liveness::resetKills(MachineBasicBlock *B) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000763 auto CopyLiveIns = [this] (MachineBasicBlock *B, BitVector &LV) -> void {
764 for (auto I : B->liveins()) {
765 MCSubRegIndexIterator S(I.PhysReg, &TRI);
766 if (!S.isValid()) {
767 LV.set(I.PhysReg);
768 continue;
769 }
770 do {
771 LaneBitmask M = TRI.getSubRegIndexLaneMask(S.getSubRegIndex());
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000772 if ((M & I.LaneMask).any())
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000773 LV.set(S.getSubReg());
774 ++S;
775 } while (S.isValid());
776 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000777 };
778
779 BitVector LiveIn(TRI.getNumRegs()), Live(TRI.getNumRegs());
780 CopyLiveIns(B, LiveIn);
781 for (auto SI : B->successors())
782 CopyLiveIns(SI, Live);
783
784 for (auto I = B->rbegin(), E = B->rend(); I != E; ++I) {
785 MachineInstr *MI = &*I;
786 if (MI->isDebugValue())
787 continue;
788
789 MI->clearKillInfo();
790 for (auto &Op : MI->operands()) {
Krzysztof Parzyszekf69ff712016-06-02 14:30:09 +0000791 // An implicit def of a super-register may not necessarily start a
792 // live range of it, since an implicit use could be used to keep parts
793 // of it live. Instead of analyzing the implicit operands, ignore
794 // implicit defs.
795 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000796 continue;
797 unsigned R = Op.getReg();
798 if (!TargetRegisterInfo::isPhysicalRegister(R))
799 continue;
800 for (MCSubRegIterator SR(R, &TRI, true); SR.isValid(); ++SR)
801 Live.reset(*SR);
802 }
803 for (auto &Op : MI->operands()) {
804 if (!Op.isReg() || !Op.isUse())
805 continue;
806 unsigned R = Op.getReg();
807 if (!TargetRegisterInfo::isPhysicalRegister(R))
808 continue;
809 bool IsLive = false;
Krzysztof Parzyszek16331f02016-04-20 14:33:23 +0000810 for (MCRegAliasIterator AR(R, &TRI, true); AR.isValid(); ++AR) {
811 if (!Live[*AR])
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000812 continue;
813 IsLive = true;
814 break;
815 }
Krzysztof Parzyszek09a86382017-01-23 23:03:49 +0000816 if (!IsLive)
817 Op.setIsKill(true);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000818 for (MCSubRegIterator SR(R, &TRI, true); SR.isValid(); ++SR)
819 Live.set(*SR);
820 }
821 }
822}
823
824
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000825// Helper function to obtain the basic block containing the reaching def
826// of the given use.
827MachineBasicBlock *Liveness::getBlockWithRef(NodeId RN) const {
828 auto F = NBMap.find(RN);
829 if (F != NBMap.end())
830 return F->second;
831 llvm_unreachable("Node id not in map");
832}
833
834
835void Liveness::traverse(MachineBasicBlock *B, RefMap &LiveIn) {
836 // The LiveIn map, for each (physical) register, contains the set of live
837 // reaching defs of that register that are live on entry to the associated
838 // block.
839
840 // The summary of the traversal algorithm:
841 //
842 // R is live-in in B, if there exists a U(R), such that rdef(R) dom B
843 // and (U \in IDF(B) or B dom U).
844 //
845 // for (C : children) {
846 // LU = {}
847 // traverse(C, LU)
848 // LiveUses += LU
849 // }
850 //
851 // LiveUses -= Defs(B);
852 // LiveUses += UpwardExposedUses(B);
853 // for (C : IIDF[B])
854 // for (U : LiveUses)
855 // if (Rdef(U) dom C)
856 // C.addLiveIn(U)
857 //
858
859 // Go up the dominator tree (depth-first).
860 MachineDomTreeNode *N = MDT.getNode(B);
861 for (auto I : *N) {
862 RefMap L;
863 MachineBasicBlock *SB = I->getBlock();
864 traverse(SB, L);
865
866 for (auto S : L)
867 LiveIn[S.first].insert(S.second.begin(), S.second.end());
868 }
869
870 if (Trace) {
Reid Kleckner40d72302016-10-20 00:22:23 +0000871 dbgs() << "\n-- BB#" << B->getNumber() << ": " << __func__
Krzysztof Parzyszekc8b6eca2016-10-03 20:17:20 +0000872 << " after recursion into: {";
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000873 for (auto I : *N)
874 dbgs() << ' ' << I->getBlock()->getNumber();
Krzysztof Parzyszekc8b6eca2016-10-03 20:17:20 +0000875 dbgs() << " }\n";
876 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000877 dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000878 }
879
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000880 // Add reaching defs of phi uses that are live on exit from this block.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000881 RefMap &PUs = PhiLOX[B];
Krzysztof Parzyszek459a1c92016-10-06 13:05:46 +0000882 for (auto &S : PUs)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000883 LiveIn[S.first].insert(S.second.begin(), S.second.end());
884
885 if (Trace) {
886 dbgs() << "after LOX\n";
887 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000888 dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000889 }
890
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000891 // The LiveIn map at this point has all defs that are live-on-exit from B,
892 // as if they were live-on-entry to B. First, we need to filter out all
893 // defs that are present in this block. Then we will add reaching defs of
894 // all upward-exposed uses.
895
896 // To filter out the defs, first make a copy of LiveIn, and then re-populate
897 // LiveIn with the defs that should remain.
898 RefMap LiveInCopy = LiveIn;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000899 LiveIn.clear();
900
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000901 for (const std::pair<RegisterId,NodeRefSet> &LE : LiveInCopy) {
902 RegisterRef LRef(LE.first);
903 NodeRefSet &NewDefs = LiveIn[LRef.Reg]; // To be filled.
904 const NodeRefSet &OldDefs = LE.second;
905 for (NodeRef OR : OldDefs) {
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000906 // R is a def node that was live-on-exit
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000907 auto DA = DFG.addr<DefNode*>(OR.first);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000908 NodeAddr<InstrNode*> IA = DA.Addr->getOwner(DFG);
909 NodeAddr<BlockNode*> BA = IA.Addr->getOwner(DFG);
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000910 if (B != BA.Addr->getCode()) {
911 // Defs from a different block need to be preserved. Defs from this
912 // block will need to be processed further, except for phi defs, the
913 // liveness of which is handled through the PhiLON/PhiLOX maps.
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000914 NewDefs.insert(OR);
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000915 continue;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000916 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000917
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000918 // Defs from this block need to stop the liveness from being
919 // propagated upwards. This only applies to non-preserving defs,
920 // and to the parts of the register actually covered by those defs.
921 // (Note that phi defs should always be preserving.)
Krzysztof Parzyszek49ffff12017-01-30 17:46:56 +0000922 RegisterAggr RRs(PRI);
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000923 LRef.Mask = OR.second;
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000924
925 if (!DFG.IsPreservingDef(DA)) {
926 assert(!(IA.Addr->getFlags() & NodeAttrs::Phi));
927 // DA is a non-phi def that is live-on-exit from this block, and
928 // that is also located in this block. LRef is a register ref
929 // whose use this def reaches. If DA covers LRef, then no part
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000930 // of LRef is exposed upwards.A
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000931 if (RRs.insert(DA.Addr->getRegRef(DFG)).hasCoverOf(LRef))
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000932 continue;
933 }
934
935 // DA itself was not sufficient to cover LRef. In general, it is
936 // the last in a chain of aliased defs before the exit from this block.
937 // There could be other defs in this block that are a part of that
938 // chain. Check that now: accumulate the registers from these defs,
939 // and if they all together cover LRef, it is not live-on-entry.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000940 for (NodeAddr<DefNode*> TA : getAllReachingDefs(DA)) {
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000941 // DefNode -> InstrNode -> BlockNode.
942 NodeAddr<InstrNode*> ITA = TA.Addr->getOwner(DFG);
943 NodeAddr<BlockNode*> BTA = ITA.Addr->getOwner(DFG);
944 // Reaching defs are ordered in the upward direction.
945 if (BTA.Addr->getCode() != B) {
946 // We have reached past the beginning of B, and the accumulated
947 // registers are not covering LRef. The first def from the
948 // upward chain will be live.
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000949 // Subtract all accumulated defs (RRs) from LRef.
Krzysztof Parzyszek49ffff12017-01-30 17:46:56 +0000950 RegisterAggr L(PRI);
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000951 L.insert(LRef).clear(RRs);
952 assert(!L.empty());
953 NewDefs.insert({TA.Id,L.begin()->second});
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000954 break;
955 }
956
957 // TA is in B. Only add this def to the accumulated cover if it is
958 // not preserving.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000959 if (!(TA.Addr->getFlags() & NodeAttrs::Preserving))
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000960 RRs.insert(TA.Addr->getRegRef(DFG));
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000961 // If this is enough to cover LRef, then stop.
962 if (RRs.hasCoverOf(LRef))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000963 break;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000964 }
965 }
966 }
967
968 emptify(LiveIn);
969
970 if (Trace) {
971 dbgs() << "after defs in block\n";
972 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000973 dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000974 }
975
976 // Scan the block for upward-exposed uses and add them to the tracking set.
977 for (auto I : DFG.getFunc().Addr->findBlock(B, DFG).Addr->members(DFG)) {
978 NodeAddr<InstrNode*> IA = I;
979 if (IA.Addr->getKind() != NodeAttrs::Stmt)
980 continue;
981 for (NodeAddr<UseNode*> UA : IA.Addr->members_if(DFG.IsUse, DFG)) {
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000982 if (UA.Addr->getFlags() & NodeAttrs::Undef)
983 continue;
Krzysztof Parzyszek5226ba82017-02-16 18:45:23 +0000984 RegisterRef RR = PRI.normalize(UA.Addr->getRegRef(DFG));
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000985 for (NodeAddr<DefNode*> D : getAllReachingDefs(UA))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000986 if (getBlockWithRef(D.Id) != B)
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000987 LiveIn[RR.Reg].insert({D.Id,RR.Mask});
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000988 }
989 }
990
991 if (Trace) {
992 dbgs() << "after uses in block\n";
993 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000994 dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000995 }
996
997 // Phi uses should not be propagated up the dominator tree, since they
998 // are not dominated by their corresponding reaching defs.
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000999 RegisterAggr &Local = LiveMap[B];
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001000 RefMap &LON = PhiLON[B];
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001001 for (auto &R : LON) {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001002 LaneBitmask M;
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001003 for (auto P : R.second)
1004 M |= P.second;
1005 Local.insert(RegisterRef(R.first,M));
1006 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001007
1008 if (Trace) {
1009 dbgs() << "after phi uses in block\n";
1010 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001011 dbgs() << " Local: " << Print<RegisterAggr>(Local, DFG) << '\n';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001012 }
1013
1014 for (auto C : IIDF[B]) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001015 RegisterAggr &LiveC = LiveMap[C];
1016 for (const std::pair<RegisterId,NodeRefSet> &S : LiveIn)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001017 for (auto R : S.second)
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001018 if (MDT.properlyDominates(getBlockWithRef(R.first), C))
1019 LiveC.insert(RegisterRef(S.first, R.second));
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001020 }
1021}
1022
1023
1024void Liveness::emptify(RefMap &M) {
1025 for (auto I = M.begin(), E = M.end(); I != E; )
1026 I = I->second.empty() ? M.erase(I) : std::next(I);
1027}
1028