blob: 9d8a3881797bcae24223944f56bb226b0466b1ae [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"
Krzysztof Parzyszekebabd992017-03-01 19:30:42 +000034#include "llvm/Support/CommandLine.h"
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +000035#include "llvm/Target/TargetRegisterInfo.h"
36
37using namespace llvm;
38using namespace rdf;
39
Krzysztof Parzyszekebabd992017-03-01 19:30:42 +000040static cl::opt<unsigned> MaxRecNest("rdf-liveness-max-rec", cl::init(25),
41 cl::Hidden, cl::desc("Maximum recursion level"));
42
Benjamin Kramer922efd72016-05-27 10:06:40 +000043namespace llvm {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +000044namespace rdf {
45 template<>
46 raw_ostream &operator<< (raw_ostream &OS, const Print<Liveness::RefMap> &P) {
47 OS << '{';
Krzysztof Parzyszek459a1c92016-10-06 13:05:46 +000048 for (auto &I : P.Obj) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +000049 OS << ' ' << PrintReg(I.first, &P.G.getTRI()) << '{';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +000050 for (auto J = I.second.begin(), E = I.second.end(); J != E; ) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +000051 OS << Print<NodeId>(J->first, P.G) << PrintLaneMaskOpt(J->second);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +000052 if (++J != E)
53 OS << ',';
54 }
55 OS << '}';
56 }
57 OS << " }";
58 return OS;
59 }
Benjamin Kramer922efd72016-05-27 10:06:40 +000060} // namespace rdf
61} // namespace llvm
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +000062
63// The order in the returned sequence is the order of reaching defs in the
64// upward traversal: the first def is the closest to the given reference RefA,
65// the next one is further up, and so on.
66// The list ends at a reaching phi def, or when the reference from RefA is
67// covered by the defs in the list (see FullChain).
68// This function provides two modes of operation:
69// (1) Returning the sequence of reaching defs for a particular reference
70// node. This sequence will terminate at the first phi node [1].
71// (2) Returning a partial sequence of reaching defs, where the final goal
72// is to traverse past phi nodes to the actual defs arising from the code
73// itself.
74// In mode (2), the register reference for which the search was started
75// may be different from the reference node RefA, for which this call was
76// made, hence the argument RefRR, which holds the original register.
77// Also, some definitions may have already been encountered in a previous
78// call that will influence register covering. The register references
79// already defined are passed in through DefRRs.
80// In mode (1), the "continuation" considerations do not apply, and the
81// RefRR is the same as the register in RefA, and the set DefRRs is empty.
82//
83// [1] It is possible for multiple phi nodes to be included in the returned
84// sequence:
85// SubA = phi ...
86// SubB = phi ...
87// ... = SuperAB(rdef:SubA), SuperAB"(rdef:SubB)
88// However, these phi nodes are independent from one another in terms of
89// the data-flow.
90
91NodeList Liveness::getAllReachingDefs(RegisterRef RefRR,
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +000092 NodeAddr<RefNode*> RefA, bool TopShadows, bool FullChain,
93 const RegisterAggr &DefRRs) {
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +000094 NodeList RDefs; // Return value.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +000095 SetVector<NodeId> DefQ;
96 SetVector<NodeId> Owners;
97
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +000098 // Dead defs will be treated as if they were live, since they are actually
99 // on the data-flow path. They cannot be ignored because even though they
100 // do not generate meaningful values, they still modify registers.
101
102 // If the reference is undefined, there is nothing to do.
103 if (RefA.Addr->getFlags() & NodeAttrs::Undef)
104 return RDefs;
105
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000106 // The initial queue should not have reaching defs for shadows. The
107 // whole point of a shadow is that it will have a reaching def that
108 // is not aliased to the reaching defs of the related shadows.
109 NodeId Start = RefA.Id;
110 auto SNA = DFG.addr<RefNode*>(Start);
111 if (NodeId RD = SNA.Addr->getReachingDef())
112 DefQ.insert(RD);
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000113 if (TopShadows) {
114 for (auto S : DFG.getRelatedRefs(RefA.Addr->getOwner(DFG), RefA))
115 if (NodeId RD = NodeAddr<RefNode*>(S).Addr->getReachingDef())
116 DefQ.insert(RD);
117 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000118
119 // Collect all the reaching defs, going up until a phi node is encountered,
120 // or there are no more reaching defs. From this set, the actual set of
121 // reaching defs will be selected.
122 // The traversal upwards must go on until a covering def is encountered.
123 // It is possible that a collection of non-covering (individually) defs
124 // will be sufficient, but keep going until a covering one is found.
125 for (unsigned i = 0; i < DefQ.size(); ++i) {
126 auto TA = DFG.addr<DefNode*>(DefQ[i]);
127 if (TA.Addr->getFlags() & NodeAttrs::PhiRef)
128 continue;
129 // Stop at the covering/overwriting def of the initial register reference.
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000130 RegisterRef RR = TA.Addr->getRegRef(DFG);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000131 if (!DFG.IsPreservingDef(TA))
Krzysztof Parzyszek49ffff12017-01-30 17:46:56 +0000132 if (RegisterAggr::isCoverOf(RR, RefRR, PRI))
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000133 continue;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000134 // Get the next level of reaching defs. This will include multiple
135 // reaching defs for shadows.
136 for (auto S : DFG.getRelatedRefs(TA.Addr->getOwner(DFG), TA))
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000137 if (NodeId RD = NodeAddr<RefNode*>(S).Addr->getReachingDef())
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000138 DefQ.insert(RD);
139 }
140
141 // Remove all non-phi defs that are not aliased to RefRR, and collect
142 // the owners of the remaining defs.
143 SetVector<NodeId> Defs;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000144 for (NodeId N : DefQ) {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000145 auto TA = DFG.addr<DefNode*>(N);
146 bool IsPhi = TA.Addr->getFlags() & NodeAttrs::PhiRef;
Krzysztof Parzyszek49ffff12017-01-30 17:46:56 +0000147 if (!IsPhi && !PRI.alias(RefRR, TA.Addr->getRegRef(DFG)))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000148 continue;
149 Defs.insert(TA.Id);
150 Owners.insert(TA.Addr->getOwner(DFG).Id);
151 }
152
153 // Return the MachineBasicBlock containing a given instruction.
154 auto Block = [this] (NodeAddr<InstrNode*> IA) -> MachineBasicBlock* {
155 if (IA.Addr->getKind() == NodeAttrs::Stmt)
156 return NodeAddr<StmtNode*>(IA).Addr->getCode()->getParent();
157 assert(IA.Addr->getKind() == NodeAttrs::Phi);
158 NodeAddr<PhiNode*> PA = IA;
159 NodeAddr<BlockNode*> BA = PA.Addr->getOwner(DFG);
160 return BA.Addr->getCode();
161 };
162 // Less(A,B) iff instruction A is further down in the dominator tree than B.
163 auto Less = [&Block,this] (NodeId A, NodeId B) -> bool {
164 if (A == B)
165 return false;
166 auto OA = DFG.addr<InstrNode*>(A), OB = DFG.addr<InstrNode*>(B);
167 MachineBasicBlock *BA = Block(OA), *BB = Block(OB);
168 if (BA != BB)
169 return MDT.dominates(BB, BA);
170 // They are in the same block.
171 bool StmtA = OA.Addr->getKind() == NodeAttrs::Stmt;
172 bool StmtB = OB.Addr->getKind() == NodeAttrs::Stmt;
173 if (StmtA) {
174 if (!StmtB) // OB is a phi and phis dominate statements.
175 return true;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000176 MachineInstr *CA = NodeAddr<StmtNode*>(OA).Addr->getCode();
177 MachineInstr *CB = NodeAddr<StmtNode*>(OB).Addr->getCode();
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000178 // The order must be linear, so tie-break such equalities.
179 if (CA == CB)
180 return A < B;
181 return MDT.dominates(CB, CA);
182 } else {
183 // OA is a phi.
184 if (StmtB)
185 return false;
186 // Both are phis. There is no ordering between phis (in terms of
187 // the data-flow), so tie-break this via node id comparison.
188 return A < B;
189 }
190 };
191
192 std::vector<NodeId> Tmp(Owners.begin(), Owners.end());
193 std::sort(Tmp.begin(), Tmp.end(), Less);
194
195 // The vector is a list of instructions, so that defs coming from
196 // the same instruction don't need to be artificially ordered.
197 // Then, when computing the initial segment, and iterating over an
198 // instruction, pick the defs that contribute to the covering (i.e. is
199 // not covered by previously added defs). Check the defs individually,
200 // i.e. first check each def if is covered or not (without adding them
201 // to the tracking set), and then add all the selected ones.
202
203 // The reason for this is this example:
204 // *d1<A>, *d2<B>, ... Assume A and B are aliased (can happen in phi nodes).
205 // *d3<C> If A \incl BuC, and B \incl AuC, then *d2 would be
206 // covered if we added A first, and A would be covered
207 // if we added B first.
208
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000209 RegisterAggr RRs(DefRRs);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000210
211 auto DefInSet = [&Defs] (NodeAddr<RefNode*> TA) -> bool {
212 return TA.Addr->getKind() == NodeAttrs::Def &&
213 Defs.count(TA.Id);
214 };
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000215 for (NodeId T : Tmp) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000216 if (!FullChain && RRs.hasCoverOf(RefRR))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000217 break;
218 auto TA = DFG.addr<InstrNode*>(T);
219 bool IsPhi = DFG.IsCode<NodeAttrs::Phi>(TA);
220 NodeList Ds;
221 for (NodeAddr<DefNode*> DA : TA.Addr->members_if(DefInSet, DFG)) {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000222 RegisterRef QR = DA.Addr->getRegRef(DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000223 // Add phi defs even if they are covered by subsequent defs. This is
224 // for cases where the reached use is not covered by any of the defs
225 // encountered so far: the phi def is needed to expose the liveness
226 // of that use to the entry of the block.
227 // Example:
228 // phi d1<R3>(,d2,), ... Phi def d1 is covered by d2.
229 // d2<R3>(d1,,u3), ...
230 // ..., u3<D1>(d2) This use needs to be live on entry.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000231 if (FullChain || IsPhi || !RRs.hasCoverOf(QR))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000232 Ds.push_back(DA);
233 }
234 RDefs.insert(RDefs.end(), Ds.begin(), Ds.end());
235 for (NodeAddr<DefNode*> DA : Ds) {
236 // When collecting a full chain of definitions, do not consider phi
237 // defs to actually define a register.
238 uint16_t Flags = DA.Addr->getFlags();
239 if (!FullChain || !(Flags & NodeAttrs::PhiRef))
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000240 if (!(Flags & NodeAttrs::Preserving)) // Don't care about Undef here.
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000241 RRs.insert(DA.Addr->getRegRef(DFG));
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000242 }
243 }
244
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +0000245 auto DeadP = [](const NodeAddr<DefNode*> DA) -> bool {
246 return DA.Addr->getFlags() & NodeAttrs::Dead;
247 };
248 RDefs.resize(std::distance(RDefs.begin(), remove_if(RDefs, DeadP)));
249
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000250 return RDefs;
251}
252
253
Krzysztof Parzyszekebabd992017-03-01 19:30:42 +0000254std::pair<NodeSet,bool>
255Liveness::getAllReachingDefsRec(RegisterRef RefRR, NodeAddr<RefNode*> RefA,
256 NodeSet &Visited, const NodeSet &Defs) {
257 return getAllReachingDefsRecImpl(RefRR, RefA, Visited, Defs, 0, MaxRecNest);
258}
259
260
261std::pair<NodeSet,bool>
262Liveness::getAllReachingDefsRecImpl(RegisterRef RefRR, NodeAddr<RefNode*> RefA,
263 NodeSet &Visited, const NodeSet &Defs, unsigned Nest, unsigned MaxNest) {
264 if (Nest > MaxNest)
Krzysztof Parzyszek8144f372017-03-01 19:59:28 +0000265 return { NodeSet(), false };
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000266 // Collect all defined registers. Do not consider phis to be defining
267 // anything, only collect "real" definitions.
Krzysztof Parzyszek49ffff12017-01-30 17:46:56 +0000268 RegisterAggr DefRRs(PRI);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000269 for (NodeId D : Defs) {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000270 const auto DA = DFG.addr<const DefNode*>(D);
271 if (!(DA.Addr->getFlags() & NodeAttrs::PhiRef))
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000272 DefRRs.insert(DA.Addr->getRegRef(DFG));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000273 }
274
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000275 NodeList RDs = getAllReachingDefs(RefRR, RefA, false, true, DefRRs);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000276 if (RDs.empty())
Krzysztof Parzyszekebabd992017-03-01 19:30:42 +0000277 return { Defs, true };
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000278
279 // Make a copy of the preexisting definitions and add the newly found ones.
280 NodeSet TmpDefs = Defs;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000281 for (NodeAddr<NodeBase*> R : RDs)
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000282 TmpDefs.insert(R.Id);
283
284 NodeSet Result = Defs;
285
286 for (NodeAddr<DefNode*> DA : RDs) {
287 Result.insert(DA.Id);
288 if (!(DA.Addr->getFlags() & NodeAttrs::PhiRef))
289 continue;
290 NodeAddr<PhiNode*> PA = DA.Addr->getOwner(DFG);
291 if (Visited.count(PA.Id))
292 continue;
293 Visited.insert(PA.Id);
294 // Go over all phi uses and get the reaching defs for each use.
295 for (auto U : PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG)) {
Krzysztof Parzyszekebabd992017-03-01 19:30:42 +0000296 const auto &T = getAllReachingDefsRecImpl(RefRR, U, Visited, TmpDefs,
297 Nest+1, MaxNest);
298 if (!T.second)
299 return { T.first, false };
300 Result.insert(T.first.begin(), T.first.end());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000301 }
302 }
303
Krzysztof Parzyszekebabd992017-03-01 19:30:42 +0000304 return { Result, true };
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000305}
306
Krzysztof Parzyszek0b8f1842017-03-10 22:42:17 +0000307/// Find the nearest ref node aliased to RefRR, going upwards in the data
308/// flow, starting from the instruction immediately preceding Inst.
309NodeAddr<RefNode*> Liveness::getNearestAliasedRef(RegisterRef RefRR,
310 NodeAddr<InstrNode*> IA) {
311 NodeAddr<BlockNode*> BA = IA.Addr->getOwner(DFG);
312 NodeList Ins = BA.Addr->members(DFG);
313 NodeId FindId = IA.Id;
314 auto E = Ins.rend();
315 auto B = std::find_if(Ins.rbegin(), E,
316 [FindId] (const NodeAddr<InstrNode*> T) {
317 return T.Id == FindId;
318 });
319 // Do not scan IA (which is what B would point to).
320 if (B != E)
321 ++B;
322
323 do {
324 // Process the range of instructions from B to E.
325 for (NodeAddr<InstrNode*> I : make_range(B, E)) {
326 NodeList Refs = I.Addr->members(DFG);
327 NodeAddr<RefNode*> Clob, Use;
328 // Scan all the refs in I aliased to RefRR, and return the one that
329 // is the closest to the output of I, i.e. def > clobber > use.
330 for (NodeAddr<RefNode*> R : Refs) {
331 if (!PRI.alias(R.Addr->getRegRef(DFG), RefRR))
332 continue;
333 if (DFG.IsDef(R)) {
334 // If it's a non-clobbering def, just return it.
335 if (!(R.Addr->getFlags() & NodeAttrs::Clobbering))
336 return R;
337 Clob = R;
338 } else {
339 Use = R;
340 }
341 }
342 if (Clob.Id != 0)
343 return Clob;
344 if (Use.Id != 0)
345 return Use;
346 }
347
348 // Go up to the immediate dominator, if any.
349 MachineBasicBlock *BB = BA.Addr->getCode();
350 BA = NodeAddr<BlockNode*>();
351 if (MachineDomTreeNode *N = MDT.getNode(BB)) {
352 if ((N = N->getIDom()))
353 BA = DFG.findBlock(N->getBlock());
354 }
355 if (!BA.Id)
356 break;
357
358 Ins = BA.Addr->members(DFG);
359 B = Ins.rbegin();
360 E = Ins.rend();
361 } while (true);
362
363 return NodeAddr<RefNode*>();
364}
365
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000366
367NodeSet Liveness::getAllReachedUses(RegisterRef RefRR,
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000368 NodeAddr<DefNode*> DefA, const RegisterAggr &DefRRs) {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000369 NodeSet Uses;
370
371 // If the original register is already covered by all the intervening
372 // defs, no more uses can be reached.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000373 if (DefRRs.hasCoverOf(RefRR))
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000374 return Uses;
375
376 // Add all directly reached uses.
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +0000377 // If the def is dead, it does not provide a value for any use.
378 bool IsDead = DefA.Addr->getFlags() & NodeAttrs::Dead;
379 NodeId U = !IsDead ? DefA.Addr->getReachedUse() : 0;
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000380 while (U != 0) {
381 auto UA = DFG.addr<UseNode*>(U);
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000382 if (!(UA.Addr->getFlags() & NodeAttrs::Undef)) {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000383 RegisterRef UR = UA.Addr->getRegRef(DFG);
Krzysztof Parzyszek49ffff12017-01-30 17:46:56 +0000384 if (PRI.alias(RefRR, UR) && !DefRRs.hasCoverOf(UR))
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000385 Uses.insert(U);
386 }
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000387 U = UA.Addr->getSibling();
388 }
389
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +0000390 // Traverse all reached defs. This time dead defs cannot be ignored.
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000391 for (NodeId D = DefA.Addr->getReachedDef(), NextD; D != 0; D = NextD) {
392 auto DA = DFG.addr<DefNode*>(D);
393 NextD = DA.Addr->getSibling();
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000394 RegisterRef DR = DA.Addr->getRegRef(DFG);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000395 // If this def is already covered, it cannot reach anything new.
396 // Similarly, skip it if it is not aliased to the interesting register.
Krzysztof Parzyszek49ffff12017-01-30 17:46:56 +0000397 if (DefRRs.hasCoverOf(DR) || !PRI.alias(RefRR, DR))
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000398 continue;
399 NodeSet T;
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000400 if (DFG.IsPreservingDef(DA)) {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000401 // If it is a preserving def, do not update the set of intervening defs.
402 T = getAllReachedUses(RefRR, DA, DefRRs);
403 } else {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000404 RegisterAggr NewDefRRs = DefRRs;
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000405 NewDefRRs.insert(DR);
406 T = getAllReachedUses(RefRR, DA, NewDefRRs);
407 }
408 Uses.insert(T.begin(), T.end());
409 }
410 return Uses;
411}
412
413
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000414void Liveness::computePhiInfo() {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000415 RealUseMap.clear();
416
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000417 NodeList Phis;
418 NodeAddr<FuncNode*> FA = DFG.getFunc();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000419 NodeList Blocks = FA.Addr->members(DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000420 for (NodeAddr<BlockNode*> BA : Blocks) {
421 auto Ps = BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG);
422 Phis.insert(Phis.end(), Ps.begin(), Ps.end());
423 }
424
425 // phi use -> (map: reaching phi -> set of registers defined in between)
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000426 std::map<NodeId,std::map<NodeId,RegisterAggr>> PhiUp;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000427 std::vector<NodeId> PhiUQ; // Work list of phis for upward propagation.
Krzysztof Parzyszek4fe9d6c2017-04-14 16:33:54 +0000428 std::map<NodeId,RegisterAggr> PhiDRs; // Phi -> registers defined by it.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000429
430 // Go over all phis.
431 for (NodeAddr<PhiNode*> PhiA : Phis) {
432 // Go over all defs and collect the reached uses that are non-phi uses
433 // (i.e. the "real uses").
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000434 RefMap &RealUses = RealUseMap[PhiA.Id];
435 NodeList PhiRefs = PhiA.Addr->members(DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000436
437 // Have a work queue of defs whose reached uses need to be found.
438 // For each def, add to the queue all reached (non-phi) defs.
439 SetVector<NodeId> DefQ;
440 NodeSet PhiDefs;
Krzysztof Parzyszek4fe9d6c2017-04-14 16:33:54 +0000441 RegisterAggr DRs(PRI);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000442 for (NodeAddr<RefNode*> R : PhiRefs) {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000443 if (!DFG.IsRef<NodeAttrs::Def>(R))
444 continue;
Krzysztof Parzyszek4fe9d6c2017-04-14 16:33:54 +0000445 DRs.insert(R.Addr->getRegRef(DFG));
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000446 DefQ.insert(R.Id);
447 PhiDefs.insert(R.Id);
448 }
Krzysztof Parzyszek4fe9d6c2017-04-14 16:33:54 +0000449 PhiDRs.insert(std::make_pair(PhiA.Id, DRs));
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000450
451 // Collect the super-set of all possible reached uses. This set will
452 // contain all uses reached from this phi, either directly from the
453 // phi defs, or (recursively) via non-phi defs reached by the phi defs.
454 // This set of uses will later be trimmed to only contain these uses that
455 // are actually reached by the phi defs.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000456 for (unsigned i = 0; i < DefQ.size(); ++i) {
457 NodeAddr<DefNode*> DA = DFG.addr<DefNode*>(DefQ[i]);
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +0000458 // Visit all reached uses. Phi defs should not really have the "dead"
459 // flag set, but check it anyway for consistency.
460 bool IsDead = DA.Addr->getFlags() & NodeAttrs::Dead;
461 NodeId UN = !IsDead ? DA.Addr->getReachedUse() : 0;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000462 while (UN != 0) {
463 NodeAddr<UseNode*> A = DFG.addr<UseNode*>(UN);
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000464 uint16_t F = A.Addr->getFlags();
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000465 if ((F & (NodeAttrs::Undef | NodeAttrs::PhiRef)) == 0) {
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000466 RegisterRef R = PRI.normalize(A.Addr->getRegRef(DFG));
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000467 RealUses[R.Reg].insert({A.Id,R.Mask});
Krzysztof Parzyszek09a86382017-01-23 23:03:49 +0000468 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000469 UN = A.Addr->getSibling();
470 }
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000471 // Visit all reached defs, and add them to the queue. These defs may
472 // override some of the uses collected here, but that will be handled
473 // later.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000474 NodeId DN = DA.Addr->getReachedDef();
475 while (DN != 0) {
476 NodeAddr<DefNode*> A = DFG.addr<DefNode*>(DN);
477 for (auto T : DFG.getRelatedRefs(A.Addr->getOwner(DFG), A)) {
478 uint16_t Flags = NodeAddr<DefNode*>(T).Addr->getFlags();
479 // Must traverse the reached-def chain. Consider:
480 // def(D0) -> def(R0) -> def(R0) -> use(D0)
481 // The reachable use of D0 passes through a def of R0.
482 if (!(Flags & NodeAttrs::PhiRef))
483 DefQ.insert(T.Id);
484 }
485 DN = A.Addr->getSibling();
486 }
487 }
488 // Filter out these uses that appear to be reachable, but really
489 // are not. For example:
490 //
491 // R1:0 = d1
492 // = R1:0 u2 Reached by d1.
493 // R0 = d3
494 // = R1:0 u4 Still reached by d1: indirectly through
495 // the def d3.
496 // R1 = d5
497 // = R1:0 u6 Not reached by d1 (covered collectively
498 // by d3 and d5), but following reached
499 // defs and uses from d1 will lead here.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000500 for (auto UI = RealUses.begin(), UE = RealUses.end(); UI != UE; ) {
501 // For each reached register UI->first, there is a set UI->second, of
502 // uses of it. For each such use, check if it is reached by this phi,
503 // i.e. check if the set of its reaching uses intersects the set of
504 // this phi's defs.
Krzysztof Parzyszekd0c71ef2017-05-05 22:10:32 +0000505 NodeRefSet Uses = UI->second;
506 UI->second.clear();
507 for (std::pair<NodeId,LaneBitmask> I : Uses) {
508 auto UA = DFG.addr<UseNode*>(I.first);
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +0000509 // Undef flag is checked above.
510 assert((UA.Addr->getFlags() & NodeAttrs::Undef) == 0);
Krzysztof Parzyszekd0c71ef2017-05-05 22:10:32 +0000511 RegisterRef R(UI->first, I.second);
512 // Calculate the exposed part of the reached use.
513 RegisterAggr Covered(PRI);
514 for (NodeAddr<DefNode*> DA : getAllReachingDefs(R, UA)) {
515 if (PhiDefs.count(DA.Id))
516 break;
517 Covered.insert(DA.Addr->getRegRef(DFG));
518 }
519 if (RegisterRef RC = Covered.clearIn(R)) {
520 // We are updating the map for register UI->first, so we need
521 // to map RC to be expressed in terms of that register.
522 RegisterRef S = PRI.mapTo(RC, UI->first);
523 UI->second.insert({I.first, S.Mask});
524 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000525 }
Krzysztof Parzyszekd0c71ef2017-05-05 22:10:32 +0000526 UI = UI->second.empty() ? RealUses.erase(UI) : std::next(UI);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000527 }
528
529 // If this phi reaches some "real" uses, add it to the queue for upward
530 // propagation.
531 if (!RealUses.empty())
532 PhiUQ.push_back(PhiA.Id);
533
534 // Go over all phi uses and check if the reaching def is another phi.
535 // Collect the phis that are among the reaching defs of these uses.
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000536 // While traversing the list of reaching defs for each phi use, accumulate
537 // the set of registers defined between this phi (PhiA) and the owner phi
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000538 // of the reaching def.
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000539 NodeSet SeenUses;
Krzysztof Parzyszeka1218722016-09-08 20:48:42 +0000540
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000541 for (auto I : PhiRefs) {
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000542 if (!DFG.IsRef<NodeAttrs::Use>(I) || SeenUses.count(I.Id))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000543 continue;
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000544 NodeAddr<PhiUseNode*> PUA = I;
545 if (PUA.Addr->getReachingDef() == 0)
546 continue;
Krzysztof Parzyszeka1218722016-09-08 20:48:42 +0000547
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000548 RegisterRef UR = PUA.Addr->getRegRef(DFG);
549 NodeList Ds = getAllReachingDefs(UR, PUA, true, false, NoRegs);
550 RegisterAggr DefRRs(PRI);
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000551
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000552 for (NodeAddr<DefNode*> D : Ds) {
553 if (D.Addr->getFlags() & NodeAttrs::PhiRef) {
554 NodeId RP = D.Addr->getOwner(DFG).Id;
555 std::map<NodeId,RegisterAggr> &M = PhiUp[PUA.Id];
556 auto F = M.find(RP);
557 if (F == M.end())
558 M.insert(std::make_pair(RP, DefRRs));
559 else
560 F->second.insert(DefRRs);
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000561 }
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000562 DefRRs.insert(D.Addr->getRegRef(DFG));
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000563 }
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000564
565 for (NodeAddr<PhiUseNode*> T : DFG.getRelatedRefs(PhiA, PUA))
566 SeenUses.insert(T.Id);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000567 }
568 }
569
570 if (Trace) {
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000571 dbgs() << "Phi-up-to-phi map with intervening defs:\n";
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000572 for (auto I : PhiUp) {
573 dbgs() << "phi " << Print<NodeId>(I.first, DFG) << " -> {";
574 for (auto R : I.second)
575 dbgs() << ' ' << Print<NodeId>(R.first, DFG)
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000576 << Print<RegisterAggr>(R.second, DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000577 dbgs() << " }\n";
578 }
579 }
580
581 // Propagate the reached registers up in the phi chain.
582 //
583 // The following type of situation needs careful handling:
584 //
585 // phi d1<R1:0> (1)
586 // |
587 // ... d2<R1>
588 // |
589 // phi u3<R1:0> (2)
590 // |
591 // ... u4<R1>
592 //
593 // The phi node (2) defines a register pair R1:0, and reaches a "real"
594 // use u4 of just R1. The same phi node is also known to reach (upwards)
595 // the phi node (1). However, the use u4 is not reached by phi (1),
596 // because of the intervening definition d2 of R1. The data flow between
597 // phis (1) and (2) is restricted to R1:0 minus R1, i.e. R0.
598 //
599 // When propagating uses up the phi chains, get the all reaching defs
600 // for a given phi use, and traverse the list until the propagated ref
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000601 // is covered, or until reaching the final phi. Only assume that the
602 // reference reaches the phi in the latter case.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000603
604 for (unsigned i = 0; i < PhiUQ.size(); ++i) {
605 auto PA = DFG.addr<PhiNode*>(PhiUQ[i]);
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000606 NodeList PUs = PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG);
607 RefMap &RUM = RealUseMap[PA.Id];
608
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000609 for (NodeAddr<UseNode*> UA : PUs) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000610 std::map<NodeId,RegisterAggr> &PUM = PhiUp[UA.Id];
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000611 RegisterRef UR = PRI.normalize(UA.Addr->getRegRef(DFG));
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000612 for (const std::pair<NodeId,RegisterAggr> &P : PUM) {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000613 bool Changed = false;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000614 const RegisterAggr &MidDefs = P.second;
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000615
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000616 // Collect the set PropUp of uses that are reached by the current
617 // phi PA, and are not covered by any intervening def between the
618 // currently visited use UA and the the upward phi P.
619
620 if (MidDefs.hasCoverOf(UR))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000621 continue;
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000622
623 // General algorithm:
624 // for each (R,U) : U is use node of R, U is reached by PA
625 // if MidDefs does not cover (R,U)
626 // then add (R-MidDefs,U) to RealUseMap[P]
627 //
628 for (const std::pair<RegisterId,NodeRefSet> &T : RUM) {
Krzysztof Parzyszek4fe9d6c2017-04-14 16:33:54 +0000629 RegisterRef R(T.first);
630 // The current phi (PA) could be a phi for a regmask. It could
631 // reach a whole variety of uses that are not related to the
632 // specific upward phi (P.first).
633 const RegisterAggr &DRs = PhiDRs.at(P.first);
634 if (!DRs.hasAliasOf(R))
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000635 continue;
Krzysztof Parzyszekd0c71ef2017-05-05 22:10:32 +0000636 R = PRI.mapTo(DRs.intersectWith(R), T.first);
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000637 for (std::pair<NodeId,LaneBitmask> V : T.second) {
Krzysztof Parzyszek4fe9d6c2017-04-14 16:33:54 +0000638 LaneBitmask M = R.Mask & V.second;
639 if (M.none())
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000640 continue;
Krzysztof Parzyszek4fe9d6c2017-04-14 16:33:54 +0000641 if (RegisterRef SS = MidDefs.clearIn(RegisterRef(R.Reg, M))) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000642 NodeRefSet &RS = RealUseMap[P.first][SS.Reg];
643 Changed |= RS.insert({V.first,SS.Mask}).second;
644 }
645 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000646 }
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000647
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000648 if (Changed)
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000649 PhiUQ.push_back(P.first);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000650 }
651 }
652 }
653
654 if (Trace) {
655 dbgs() << "Real use map:\n";
656 for (auto I : RealUseMap) {
657 dbgs() << "phi " << Print<NodeId>(I.first, DFG);
658 NodeAddr<PhiNode*> PA = DFG.addr<PhiNode*>(I.first);
659 NodeList Ds = PA.Addr->members_if(DFG.IsRef<NodeAttrs::Def>, DFG);
660 if (!Ds.empty()) {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000661 RegisterRef RR = NodeAddr<DefNode*>(Ds[0]).Addr->getRegRef(DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000662 dbgs() << '<' << Print<RegisterRef>(RR, DFG) << '>';
663 } else {
664 dbgs() << "<noreg>";
665 }
666 dbgs() << " -> " << Print<RefMap>(I.second, DFG) << '\n';
667 }
668 }
669}
670
671
672void Liveness::computeLiveIns() {
673 // Populate the node-to-block map. This speeds up the calculations
674 // significantly.
675 NBMap.clear();
676 for (NodeAddr<BlockNode*> BA : DFG.getFunc().Addr->members(DFG)) {
677 MachineBasicBlock *BB = BA.Addr->getCode();
678 for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG)) {
679 for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG))
680 NBMap.insert(std::make_pair(RA.Id, BB));
681 NBMap.insert(std::make_pair(IA.Id, BB));
682 }
683 }
684
685 MachineFunction &MF = DFG.getMF();
686
687 // Compute IDF first, then the inverse.
688 decltype(IIDF) IDF;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000689 for (MachineBasicBlock &B : MF) {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000690 auto F1 = MDF.find(&B);
691 if (F1 == MDF.end())
692 continue;
693 SetVector<MachineBasicBlock*> IDFB(F1->second.begin(), F1->second.end());
694 for (unsigned i = 0; i < IDFB.size(); ++i) {
695 auto F2 = MDF.find(IDFB[i]);
696 if (F2 != MDF.end())
697 IDFB.insert(F2->second.begin(), F2->second.end());
698 }
699 // Add B to the IDF(B). This will put B in the IIDF(B).
700 IDFB.insert(&B);
701 IDF[&B].insert(IDFB.begin(), IDFB.end());
702 }
703
704 for (auto I : IDF)
705 for (auto S : I.second)
706 IIDF[S].insert(I.first);
707
708 computePhiInfo();
709
710 NodeAddr<FuncNode*> FA = DFG.getFunc();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000711 NodeList Blocks = FA.Addr->members(DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000712
713 // Build the phi live-on-entry map.
714 for (NodeAddr<BlockNode*> BA : Blocks) {
715 MachineBasicBlock *MB = BA.Addr->getCode();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000716 RefMap &LON = PhiLON[MB];
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000717 for (auto P : BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG))
Krzysztof Parzyszek459a1c92016-10-06 13:05:46 +0000718 for (const RefMap::value_type &S : RealUseMap[P.Id])
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000719 LON[S.first].insert(S.second.begin(), S.second.end());
720 }
721
722 if (Trace) {
723 dbgs() << "Phi live-on-entry map:\n";
Krzysztof Parzyszek459a1c92016-10-06 13:05:46 +0000724 for (auto &I : PhiLON)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000725 dbgs() << "block #" << I.first->getNumber() << " -> "
726 << Print<RefMap>(I.second, DFG) << '\n';
727 }
728
729 // Build the phi live-on-exit map. Each phi node has some set of reached
730 // "real" uses. Propagate this set backwards into the block predecessors
731 // through the reaching defs of the corresponding phi uses.
732 for (NodeAddr<BlockNode*> BA : Blocks) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000733 NodeList Phis = BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000734 for (NodeAddr<PhiNode*> PA : Phis) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000735 RefMap &RUs = RealUseMap[PA.Id];
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000736 if (RUs.empty())
737 continue;
738
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000739 NodeSet SeenUses;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000740 for (auto U : PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG)) {
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000741 if (!SeenUses.insert(U.Id).second)
742 continue;
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000743 NodeAddr<PhiUseNode*> PUA = U;
744 if (PUA.Addr->getReachingDef() == 0)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000745 continue;
746
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000747 // Each phi has some set (possibly empty) of reached "real" uses,
748 // that is, uses that are part of the compiled program. Such a use
749 // may be located in some farther block, but following a chain of
750 // reaching defs will eventually lead to this phi.
751 // Any chain of reaching defs may fork at a phi node, but there
752 // will be a path upwards that will lead to this phi. Now, this
753 // chain will need to fork at this phi, since some of the reached
754 // uses may have definitions joining in from multiple predecessors.
755 // For each reached "real" use, identify the set of reaching defs
756 // coming from each predecessor P, and add them to PhiLOX[P].
757 //
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000758 auto PrA = DFG.addr<BlockNode*>(PUA.Addr->getPredecessor());
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000759 RefMap &LOX = PhiLOX[PrA.Addr->getCode()];
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000760
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000761 for (const std::pair<RegisterId,NodeRefSet> &RS : RUs) {
762 // We need to visit each individual use.
763 for (std::pair<NodeId,LaneBitmask> P : RS.second) {
764 // Create a register ref corresponding to the use, and find
765 // all reaching defs starting from the phi use, and treating
766 // all related shadows as a single use cluster.
767 RegisterRef S(RS.first, P.second);
768 NodeList Ds = getAllReachingDefs(S, PUA, true, false, NoRegs);
Krzysztof Parzyszek072ddb32017-04-28 21:57:53 +0000769 for (NodeAddr<DefNode*> D : Ds) {
770 // Calculate the mask corresponding to the visited def.
771 RegisterAggr TA(PRI);
772 TA.insert(D.Addr->getRegRef(DFG)).intersect(S);
773 LaneBitmask TM = TA.makeRegRef().Mask;
774 LOX[S.Reg].insert({D.Id, TM});
775 }
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000776 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000777 }
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000778
779 for (NodeAddr<PhiUseNode*> T : DFG.getRelatedRefs(PA, PUA))
780 SeenUses.insert(T.Id);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000781 } // for U : phi uses
782 } // for P : Phis
783 } // for B : Blocks
784
785 if (Trace) {
786 dbgs() << "Phi live-on-exit map:\n";
Krzysztof Parzyszek459a1c92016-10-06 13:05:46 +0000787 for (auto &I : PhiLOX)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000788 dbgs() << "block #" << I.first->getNumber() << " -> "
789 << Print<RefMap>(I.second, DFG) << '\n';
790 }
791
792 RefMap LiveIn;
793 traverse(&MF.front(), LiveIn);
794
795 // Add function live-ins to the live-in set of the function entry block.
Krzysztof Parzyszekb561cf92017-01-30 16:20:30 +0000796 LiveMap[&MF.front()].insert(DFG.getLiveIns());
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000797
798 if (Trace) {
799 // Dump the liveness map
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000800 for (MachineBasicBlock &B : MF) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000801 std::vector<RegisterRef> LV;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000802 for (auto I = B.livein_begin(), E = B.livein_end(); I != E; ++I)
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000803 LV.push_back(RegisterRef(I->PhysReg, I->LaneMask));
804 std::sort(LV.begin(), LV.end());
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000805 dbgs() << "BB#" << B.getNumber() << "\t rec = {";
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000806 for (auto I : LV)
807 dbgs() << ' ' << Print<RegisterRef>(I, DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000808 dbgs() << " }\n";
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000809 //dbgs() << "\tcomp = " << Print<RegisterAggr>(LiveMap[&B], DFG) << '\n';
810
811 LV.clear();
Krzysztof Parzyszek74b1f252017-04-14 17:25:13 +0000812 const RegisterAggr &LG = LiveMap[&B];
813 for (auto I = LG.rr_begin(), E = LG.rr_end(); I != E; ++I)
814 LV.push_back(*I);
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000815 std::sort(LV.begin(), LV.end());
816 dbgs() << "\tcomp = {";
817 for (auto I : LV)
818 dbgs() << ' ' << Print<RegisterRef>(I, DFG);
819 dbgs() << " }\n";
820
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000821 }
822 }
823}
824
825
826void Liveness::resetLiveIns() {
827 for (auto &B : DFG.getMF()) {
828 // Remove all live-ins.
829 std::vector<unsigned> T;
830 for (auto I = B.livein_begin(), E = B.livein_end(); I != E; ++I)
831 T.push_back(I->PhysReg);
832 for (auto I : T)
833 B.removeLiveIn(I);
834 // Add the newly computed live-ins.
Krzysztof Parzyszek74b1f252017-04-14 17:25:13 +0000835 const RegisterAggr &LiveIns = LiveMap[&B];
836 for (auto I = LiveIns.rr_begin(), E = LiveIns.rr_end(); I != E; ++I) {
837 RegisterRef R = *I;
838 B.addLiveIn({MCPhysReg(R.Reg), R.Mask});
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000839 }
840 }
841}
842
843
844void Liveness::resetKills() {
845 for (auto &B : DFG.getMF())
846 resetKills(&B);
847}
848
849
850void Liveness::resetKills(MachineBasicBlock *B) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000851 auto CopyLiveIns = [this] (MachineBasicBlock *B, BitVector &LV) -> void {
852 for (auto I : B->liveins()) {
853 MCSubRegIndexIterator S(I.PhysReg, &TRI);
854 if (!S.isValid()) {
855 LV.set(I.PhysReg);
856 continue;
857 }
858 do {
859 LaneBitmask M = TRI.getSubRegIndexLaneMask(S.getSubRegIndex());
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000860 if ((M & I.LaneMask).any())
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000861 LV.set(S.getSubReg());
862 ++S;
863 } while (S.isValid());
864 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000865 };
866
867 BitVector LiveIn(TRI.getNumRegs()), Live(TRI.getNumRegs());
868 CopyLiveIns(B, LiveIn);
869 for (auto SI : B->successors())
870 CopyLiveIns(SI, Live);
871
872 for (auto I = B->rbegin(), E = B->rend(); I != E; ++I) {
873 MachineInstr *MI = &*I;
874 if (MI->isDebugValue())
875 continue;
876
877 MI->clearKillInfo();
878 for (auto &Op : MI->operands()) {
Krzysztof Parzyszekf69ff712016-06-02 14:30:09 +0000879 // An implicit def of a super-register may not necessarily start a
880 // live range of it, since an implicit use could be used to keep parts
881 // of it live. Instead of analyzing the implicit operands, ignore
882 // implicit defs.
883 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000884 continue;
885 unsigned R = Op.getReg();
886 if (!TargetRegisterInfo::isPhysicalRegister(R))
887 continue;
888 for (MCSubRegIterator SR(R, &TRI, true); SR.isValid(); ++SR)
889 Live.reset(*SR);
890 }
891 for (auto &Op : MI->operands()) {
Krzysztof Parzyszekace1b892017-02-22 18:29:16 +0000892 if (!Op.isReg() || !Op.isUse() || Op.isUndef())
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000893 continue;
894 unsigned R = Op.getReg();
895 if (!TargetRegisterInfo::isPhysicalRegister(R))
896 continue;
897 bool IsLive = false;
Krzysztof Parzyszek16331f02016-04-20 14:33:23 +0000898 for (MCRegAliasIterator AR(R, &TRI, true); AR.isValid(); ++AR) {
899 if (!Live[*AR])
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000900 continue;
901 IsLive = true;
902 break;
903 }
Krzysztof Parzyszek09a86382017-01-23 23:03:49 +0000904 if (!IsLive)
905 Op.setIsKill(true);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000906 for (MCSubRegIterator SR(R, &TRI, true); SR.isValid(); ++SR)
907 Live.set(*SR);
908 }
909 }
910}
911
912
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000913// Helper function to obtain the basic block containing the reaching def
914// of the given use.
915MachineBasicBlock *Liveness::getBlockWithRef(NodeId RN) const {
916 auto F = NBMap.find(RN);
917 if (F != NBMap.end())
918 return F->second;
919 llvm_unreachable("Node id not in map");
920}
921
922
923void Liveness::traverse(MachineBasicBlock *B, RefMap &LiveIn) {
924 // The LiveIn map, for each (physical) register, contains the set of live
925 // reaching defs of that register that are live on entry to the associated
926 // block.
927
928 // The summary of the traversal algorithm:
929 //
930 // R is live-in in B, if there exists a U(R), such that rdef(R) dom B
931 // and (U \in IDF(B) or B dom U).
932 //
933 // for (C : children) {
934 // LU = {}
935 // traverse(C, LU)
936 // LiveUses += LU
937 // }
938 //
939 // LiveUses -= Defs(B);
940 // LiveUses += UpwardExposedUses(B);
941 // for (C : IIDF[B])
942 // for (U : LiveUses)
943 // if (Rdef(U) dom C)
944 // C.addLiveIn(U)
945 //
946
947 // Go up the dominator tree (depth-first).
948 MachineDomTreeNode *N = MDT.getNode(B);
949 for (auto I : *N) {
950 RefMap L;
951 MachineBasicBlock *SB = I->getBlock();
952 traverse(SB, L);
953
954 for (auto S : L)
955 LiveIn[S.first].insert(S.second.begin(), S.second.end());
956 }
957
958 if (Trace) {
Reid Kleckner40d72302016-10-20 00:22:23 +0000959 dbgs() << "\n-- BB#" << B->getNumber() << ": " << __func__
Krzysztof Parzyszekc8b6eca2016-10-03 20:17:20 +0000960 << " after recursion into: {";
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000961 for (auto I : *N)
962 dbgs() << ' ' << I->getBlock()->getNumber();
Krzysztof Parzyszekc8b6eca2016-10-03 20:17:20 +0000963 dbgs() << " }\n";
964 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000965 dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000966 }
967
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000968 // Add reaching defs of phi uses that are live on exit from this block.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000969 RefMap &PUs = PhiLOX[B];
Krzysztof Parzyszek459a1c92016-10-06 13:05:46 +0000970 for (auto &S : PUs)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000971 LiveIn[S.first].insert(S.second.begin(), S.second.end());
972
973 if (Trace) {
974 dbgs() << "after LOX\n";
975 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000976 dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000977 }
978
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000979 // The LiveIn map at this point has all defs that are live-on-exit from B,
980 // as if they were live-on-entry to B. First, we need to filter out all
981 // defs that are present in this block. Then we will add reaching defs of
982 // all upward-exposed uses.
983
984 // To filter out the defs, first make a copy of LiveIn, and then re-populate
985 // LiveIn with the defs that should remain.
986 RefMap LiveInCopy = LiveIn;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000987 LiveIn.clear();
988
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000989 for (const std::pair<RegisterId,NodeRefSet> &LE : LiveInCopy) {
990 RegisterRef LRef(LE.first);
991 NodeRefSet &NewDefs = LiveIn[LRef.Reg]; // To be filled.
992 const NodeRefSet &OldDefs = LE.second;
993 for (NodeRef OR : OldDefs) {
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000994 // R is a def node that was live-on-exit
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000995 auto DA = DFG.addr<DefNode*>(OR.first);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000996 NodeAddr<InstrNode*> IA = DA.Addr->getOwner(DFG);
997 NodeAddr<BlockNode*> BA = IA.Addr->getOwner(DFG);
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000998 if (B != BA.Addr->getCode()) {
999 // Defs from a different block need to be preserved. Defs from this
1000 // block will need to be processed further, except for phi defs, the
1001 // liveness of which is handled through the PhiLON/PhiLOX maps.
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001002 NewDefs.insert(OR);
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +00001003 continue;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001004 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001005
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +00001006 // Defs from this block need to stop the liveness from being
1007 // propagated upwards. This only applies to non-preserving defs,
1008 // and to the parts of the register actually covered by those defs.
1009 // (Note that phi defs should always be preserving.)
Krzysztof Parzyszek49ffff12017-01-30 17:46:56 +00001010 RegisterAggr RRs(PRI);
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001011 LRef.Mask = OR.second;
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +00001012
1013 if (!DFG.IsPreservingDef(DA)) {
1014 assert(!(IA.Addr->getFlags() & NodeAttrs::Phi));
1015 // DA is a non-phi def that is live-on-exit from this block, and
1016 // that is also located in this block. LRef is a register ref
1017 // whose use this def reaches. If DA covers LRef, then no part
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001018 // of LRef is exposed upwards.A
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001019 if (RRs.insert(DA.Addr->getRegRef(DFG)).hasCoverOf(LRef))
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +00001020 continue;
1021 }
1022
1023 // DA itself was not sufficient to cover LRef. In general, it is
1024 // the last in a chain of aliased defs before the exit from this block.
1025 // There could be other defs in this block that are a part of that
1026 // chain. Check that now: accumulate the registers from these defs,
1027 // and if they all together cover LRef, it is not live-on-entry.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001028 for (NodeAddr<DefNode*> TA : getAllReachingDefs(DA)) {
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +00001029 // DefNode -> InstrNode -> BlockNode.
1030 NodeAddr<InstrNode*> ITA = TA.Addr->getOwner(DFG);
1031 NodeAddr<BlockNode*> BTA = ITA.Addr->getOwner(DFG);
1032 // Reaching defs are ordered in the upward direction.
1033 if (BTA.Addr->getCode() != B) {
1034 // We have reached past the beginning of B, and the accumulated
1035 // registers are not covering LRef. The first def from the
1036 // upward chain will be live.
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001037 // Subtract all accumulated defs (RRs) from LRef.
Krzysztof Parzyszek74b1f252017-04-14 17:25:13 +00001038 RegisterRef T = RRs.clearIn(LRef);
1039 assert(T);
1040 NewDefs.insert({TA.Id,T.Mask});
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +00001041 break;
1042 }
1043
1044 // TA is in B. Only add this def to the accumulated cover if it is
1045 // not preserving.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001046 if (!(TA.Addr->getFlags() & NodeAttrs::Preserving))
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001047 RRs.insert(TA.Addr->getRegRef(DFG));
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +00001048 // If this is enough to cover LRef, then stop.
1049 if (RRs.hasCoverOf(LRef))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001050 break;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001051 }
1052 }
1053 }
1054
1055 emptify(LiveIn);
1056
1057 if (Trace) {
1058 dbgs() << "after defs in block\n";
1059 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001060 dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001061 }
1062
1063 // Scan the block for upward-exposed uses and add them to the tracking set.
1064 for (auto I : DFG.getFunc().Addr->findBlock(B, DFG).Addr->members(DFG)) {
1065 NodeAddr<InstrNode*> IA = I;
1066 if (IA.Addr->getKind() != NodeAttrs::Stmt)
1067 continue;
1068 for (NodeAddr<UseNode*> UA : IA.Addr->members_if(DFG.IsUse, DFG)) {
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001069 if (UA.Addr->getFlags() & NodeAttrs::Undef)
1070 continue;
Krzysztof Parzyszek5226ba82017-02-16 18:45:23 +00001071 RegisterRef RR = PRI.normalize(UA.Addr->getRegRef(DFG));
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001072 for (NodeAddr<DefNode*> D : getAllReachingDefs(UA))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001073 if (getBlockWithRef(D.Id) != B)
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001074 LiveIn[RR.Reg].insert({D.Id,RR.Mask});
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001075 }
1076 }
1077
1078 if (Trace) {
1079 dbgs() << "after uses in block\n";
1080 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001081 dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001082 }
1083
1084 // Phi uses should not be propagated up the dominator tree, since they
1085 // are not dominated by their corresponding reaching defs.
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001086 RegisterAggr &Local = LiveMap[B];
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001087 RefMap &LON = PhiLON[B];
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001088 for (auto &R : LON) {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001089 LaneBitmask M;
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001090 for (auto P : R.second)
1091 M |= P.second;
1092 Local.insert(RegisterRef(R.first,M));
1093 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001094
1095 if (Trace) {
1096 dbgs() << "after phi uses in block\n";
1097 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001098 dbgs() << " Local: " << Print<RegisterAggr>(Local, DFG) << '\n';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001099 }
1100
1101 for (auto C : IIDF[B]) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001102 RegisterAggr &LiveC = LiveMap[C];
1103 for (const std::pair<RegisterId,NodeRefSet> &S : LiveIn)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001104 for (auto R : S.second)
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001105 if (MDT.properlyDominates(getBlockWithRef(R.first), C))
1106 LiveC.insert(RegisterRef(S.first, R.second));
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001107 }
1108}
1109
1110
1111void Liveness::emptify(RefMap &M) {
1112 for (auto I = M.begin(), E = M.end(); I != E; )
1113 I = I->second.empty() ? M.erase(I) : std::next(I);
1114}
1115