blob: b0532f933b16fbe5873d6c66f586119c34ec0784 [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 Parzyszek2db0c8b2016-09-07 20:37:05 +0000500 auto InPhiDefs = [&PhiDefs] (NodeAddr<DefNode*> DA) -> bool {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000501 return PhiDefs.count(DA.Id);
502 };
503 for (auto UI = RealUses.begin(), UE = RealUses.end(); UI != UE; ) {
504 // For each reached register UI->first, there is a set UI->second, of
505 // uses of it. For each such use, check if it is reached by this phi,
506 // i.e. check if the set of its reaching uses intersects the set of
507 // this phi's defs.
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000508 NodeRefSet &Uses = UI->second;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000509 for (auto I = Uses.begin(), E = Uses.end(); I != E; ) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000510 auto UA = DFG.addr<UseNode*>(I->first);
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +0000511 // Undef flag is checked above.
512 assert((UA.Addr->getFlags() & NodeAttrs::Undef) == 0);
Krzysztof Parzyszek09a86382017-01-23 23:03:49 +0000513 RegisterRef R(UI->first, I->second);
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000514 NodeList RDs = getAllReachingDefs(R, UA);
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000515 // If none of the reaching defs of R are from this phi, remove this
516 // use of R.
517 I = any_of(RDs, InPhiDefs) ? std::next(I) : Uses.erase(I);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000518 }
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000519 UI = Uses.empty() ? RealUses.erase(UI) : std::next(UI);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000520 }
521
522 // If this phi reaches some "real" uses, add it to the queue for upward
523 // propagation.
524 if (!RealUses.empty())
525 PhiUQ.push_back(PhiA.Id);
526
527 // Go over all phi uses and check if the reaching def is another phi.
528 // Collect the phis that are among the reaching defs of these uses.
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000529 // While traversing the list of reaching defs for each phi use, accumulate
530 // the set of registers defined between this phi (PhiA) and the owner phi
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000531 // of the reaching def.
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000532 NodeSet SeenUses;
Krzysztof Parzyszeka1218722016-09-08 20:48:42 +0000533
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000534 for (auto I : PhiRefs) {
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000535 if (!DFG.IsRef<NodeAttrs::Use>(I) || SeenUses.count(I.Id))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000536 continue;
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000537 NodeAddr<PhiUseNode*> PUA = I;
538 if (PUA.Addr->getReachingDef() == 0)
539 continue;
Krzysztof Parzyszeka1218722016-09-08 20:48:42 +0000540
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000541 RegisterRef UR = PUA.Addr->getRegRef(DFG);
542 NodeList Ds = getAllReachingDefs(UR, PUA, true, false, NoRegs);
543 RegisterAggr DefRRs(PRI);
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000544
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000545 for (NodeAddr<DefNode*> D : Ds) {
546 if (D.Addr->getFlags() & NodeAttrs::PhiRef) {
547 NodeId RP = D.Addr->getOwner(DFG).Id;
548 std::map<NodeId,RegisterAggr> &M = PhiUp[PUA.Id];
549 auto F = M.find(RP);
550 if (F == M.end())
551 M.insert(std::make_pair(RP, DefRRs));
552 else
553 F->second.insert(DefRRs);
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000554 }
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000555 DefRRs.insert(D.Addr->getRegRef(DFG));
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000556 }
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000557
558 for (NodeAddr<PhiUseNode*> T : DFG.getRelatedRefs(PhiA, PUA))
559 SeenUses.insert(T.Id);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000560 }
561 }
562
563 if (Trace) {
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000564 dbgs() << "Phi-up-to-phi map with intervening defs:\n";
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000565 for (auto I : PhiUp) {
566 dbgs() << "phi " << Print<NodeId>(I.first, DFG) << " -> {";
567 for (auto R : I.second)
568 dbgs() << ' ' << Print<NodeId>(R.first, DFG)
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000569 << Print<RegisterAggr>(R.second, DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000570 dbgs() << " }\n";
571 }
572 }
573
574 // Propagate the reached registers up in the phi chain.
575 //
576 // The following type of situation needs careful handling:
577 //
578 // phi d1<R1:0> (1)
579 // |
580 // ... d2<R1>
581 // |
582 // phi u3<R1:0> (2)
583 // |
584 // ... u4<R1>
585 //
586 // The phi node (2) defines a register pair R1:0, and reaches a "real"
587 // use u4 of just R1. The same phi node is also known to reach (upwards)
588 // the phi node (1). However, the use u4 is not reached by phi (1),
589 // because of the intervening definition d2 of R1. The data flow between
590 // phis (1) and (2) is restricted to R1:0 minus R1, i.e. R0.
591 //
592 // When propagating uses up the phi chains, get the all reaching defs
593 // for a given phi use, and traverse the list until the propagated ref
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000594 // is covered, or until reaching the final phi. Only assume that the
595 // reference reaches the phi in the latter case.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000596
597 for (unsigned i = 0; i < PhiUQ.size(); ++i) {
598 auto PA = DFG.addr<PhiNode*>(PhiUQ[i]);
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000599 NodeList PUs = PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG);
600 RefMap &RUM = RealUseMap[PA.Id];
601
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000602 for (NodeAddr<UseNode*> UA : PUs) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000603 std::map<NodeId,RegisterAggr> &PUM = PhiUp[UA.Id];
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000604 RegisterRef UR = PRI.normalize(UA.Addr->getRegRef(DFG));
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000605 for (const std::pair<NodeId,RegisterAggr> &P : PUM) {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000606 bool Changed = false;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000607 const RegisterAggr &MidDefs = P.second;
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000608
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000609 // Collect the set PropUp of uses that are reached by the current
610 // phi PA, and are not covered by any intervening def between the
611 // currently visited use UA and the the upward phi P.
612
613 if (MidDefs.hasCoverOf(UR))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000614 continue;
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000615
616 // General algorithm:
617 // for each (R,U) : U is use node of R, U is reached by PA
618 // if MidDefs does not cover (R,U)
619 // then add (R-MidDefs,U) to RealUseMap[P]
620 //
621 for (const std::pair<RegisterId,NodeRefSet> &T : RUM) {
Krzysztof Parzyszek4fe9d6c2017-04-14 16:33:54 +0000622 RegisterRef R(T.first);
623 // The current phi (PA) could be a phi for a regmask. It could
624 // reach a whole variety of uses that are not related to the
625 // specific upward phi (P.first).
626 const RegisterAggr &DRs = PhiDRs.at(P.first);
627 if (!DRs.hasAliasOf(R))
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000628 continue;
Krzysztof Parzyszek4fe9d6c2017-04-14 16:33:54 +0000629 R = DRs.intersectWith(R);
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000630 for (std::pair<NodeId,LaneBitmask> V : T.second) {
Krzysztof Parzyszek4fe9d6c2017-04-14 16:33:54 +0000631 LaneBitmask M = R.Mask & V.second;
632 if (M.none())
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000633 continue;
Krzysztof Parzyszek4fe9d6c2017-04-14 16:33:54 +0000634 if (RegisterRef SS = MidDefs.clearIn(RegisterRef(R.Reg, M))) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000635 NodeRefSet &RS = RealUseMap[P.first][SS.Reg];
636 Changed |= RS.insert({V.first,SS.Mask}).second;
637 }
638 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000639 }
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000640
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000641 if (Changed)
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000642 PhiUQ.push_back(P.first);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000643 }
644 }
645 }
646
647 if (Trace) {
648 dbgs() << "Real use map:\n";
649 for (auto I : RealUseMap) {
650 dbgs() << "phi " << Print<NodeId>(I.first, DFG);
651 NodeAddr<PhiNode*> PA = DFG.addr<PhiNode*>(I.first);
652 NodeList Ds = PA.Addr->members_if(DFG.IsRef<NodeAttrs::Def>, DFG);
653 if (!Ds.empty()) {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000654 RegisterRef RR = NodeAddr<DefNode*>(Ds[0]).Addr->getRegRef(DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000655 dbgs() << '<' << Print<RegisterRef>(RR, DFG) << '>';
656 } else {
657 dbgs() << "<noreg>";
658 }
659 dbgs() << " -> " << Print<RefMap>(I.second, DFG) << '\n';
660 }
661 }
662}
663
664
665void Liveness::computeLiveIns() {
666 // Populate the node-to-block map. This speeds up the calculations
667 // significantly.
668 NBMap.clear();
669 for (NodeAddr<BlockNode*> BA : DFG.getFunc().Addr->members(DFG)) {
670 MachineBasicBlock *BB = BA.Addr->getCode();
671 for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG)) {
672 for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG))
673 NBMap.insert(std::make_pair(RA.Id, BB));
674 NBMap.insert(std::make_pair(IA.Id, BB));
675 }
676 }
677
678 MachineFunction &MF = DFG.getMF();
679
680 // Compute IDF first, then the inverse.
681 decltype(IIDF) IDF;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000682 for (MachineBasicBlock &B : MF) {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000683 auto F1 = MDF.find(&B);
684 if (F1 == MDF.end())
685 continue;
686 SetVector<MachineBasicBlock*> IDFB(F1->second.begin(), F1->second.end());
687 for (unsigned i = 0; i < IDFB.size(); ++i) {
688 auto F2 = MDF.find(IDFB[i]);
689 if (F2 != MDF.end())
690 IDFB.insert(F2->second.begin(), F2->second.end());
691 }
692 // Add B to the IDF(B). This will put B in the IIDF(B).
693 IDFB.insert(&B);
694 IDF[&B].insert(IDFB.begin(), IDFB.end());
695 }
696
697 for (auto I : IDF)
698 for (auto S : I.second)
699 IIDF[S].insert(I.first);
700
701 computePhiInfo();
702
703 NodeAddr<FuncNode*> FA = DFG.getFunc();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000704 NodeList Blocks = FA.Addr->members(DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000705
706 // Build the phi live-on-entry map.
707 for (NodeAddr<BlockNode*> BA : Blocks) {
708 MachineBasicBlock *MB = BA.Addr->getCode();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000709 RefMap &LON = PhiLON[MB];
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000710 for (auto P : BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG))
Krzysztof Parzyszek459a1c92016-10-06 13:05:46 +0000711 for (const RefMap::value_type &S : RealUseMap[P.Id])
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000712 LON[S.first].insert(S.second.begin(), S.second.end());
713 }
714
715 if (Trace) {
716 dbgs() << "Phi live-on-entry map:\n";
Krzysztof Parzyszek459a1c92016-10-06 13:05:46 +0000717 for (auto &I : PhiLON)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000718 dbgs() << "block #" << I.first->getNumber() << " -> "
719 << Print<RefMap>(I.second, DFG) << '\n';
720 }
721
722 // Build the phi live-on-exit map. Each phi node has some set of reached
723 // "real" uses. Propagate this set backwards into the block predecessors
724 // through the reaching defs of the corresponding phi uses.
725 for (NodeAddr<BlockNode*> BA : Blocks) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000726 NodeList Phis = BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000727 for (NodeAddr<PhiNode*> PA : Phis) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000728 RefMap &RUs = RealUseMap[PA.Id];
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000729 if (RUs.empty())
730 continue;
731
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000732 NodeSet SeenUses;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000733 for (auto U : PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG)) {
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000734 if (!SeenUses.insert(U.Id).second)
735 continue;
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000736 NodeAddr<PhiUseNode*> PUA = U;
737 if (PUA.Addr->getReachingDef() == 0)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000738 continue;
739
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000740 // Each phi has some set (possibly empty) of reached "real" uses,
741 // that is, uses that are part of the compiled program. Such a use
742 // may be located in some farther block, but following a chain of
743 // reaching defs will eventually lead to this phi.
744 // Any chain of reaching defs may fork at a phi node, but there
745 // will be a path upwards that will lead to this phi. Now, this
746 // chain will need to fork at this phi, since some of the reached
747 // uses may have definitions joining in from multiple predecessors.
748 // For each reached "real" use, identify the set of reaching defs
749 // coming from each predecessor P, and add them to PhiLOX[P].
750 //
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000751 auto PrA = DFG.addr<BlockNode*>(PUA.Addr->getPredecessor());
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000752 RefMap &LOX = PhiLOX[PrA.Addr->getCode()];
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000753
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000754 for (const std::pair<RegisterId,NodeRefSet> &RS : RUs) {
755 // We need to visit each individual use.
756 for (std::pair<NodeId,LaneBitmask> P : RS.second) {
757 // Create a register ref corresponding to the use, and find
758 // all reaching defs starting from the phi use, and treating
759 // all related shadows as a single use cluster.
760 RegisterRef S(RS.first, P.second);
761 NodeList Ds = getAllReachingDefs(S, PUA, true, false, NoRegs);
762 for (NodeAddr<DefNode*> D : Ds)
763 LOX[S.Reg].insert({D.Id, S.Mask});
764 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000765 }
Krzysztof Parzyszekcac10f92017-02-16 19:28:06 +0000766
767 for (NodeAddr<PhiUseNode*> T : DFG.getRelatedRefs(PA, PUA))
768 SeenUses.insert(T.Id);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000769 } // for U : phi uses
770 } // for P : Phis
771 } // for B : Blocks
772
773 if (Trace) {
774 dbgs() << "Phi live-on-exit map:\n";
Krzysztof Parzyszek459a1c92016-10-06 13:05:46 +0000775 for (auto &I : PhiLOX)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000776 dbgs() << "block #" << I.first->getNumber() << " -> "
777 << Print<RefMap>(I.second, DFG) << '\n';
778 }
779
780 RefMap LiveIn;
781 traverse(&MF.front(), LiveIn);
782
783 // Add function live-ins to the live-in set of the function entry block.
Krzysztof Parzyszekb561cf92017-01-30 16:20:30 +0000784 LiveMap[&MF.front()].insert(DFG.getLiveIns());
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000785
786 if (Trace) {
787 // Dump the liveness map
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000788 for (MachineBasicBlock &B : MF) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000789 std::vector<RegisterRef> LV;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000790 for (auto I = B.livein_begin(), E = B.livein_end(); I != E; ++I)
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000791 LV.push_back(RegisterRef(I->PhysReg, I->LaneMask));
792 std::sort(LV.begin(), LV.end());
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000793 dbgs() << "BB#" << B.getNumber() << "\t rec = {";
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000794 for (auto I : LV)
795 dbgs() << ' ' << Print<RegisterRef>(I, DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000796 dbgs() << " }\n";
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000797 //dbgs() << "\tcomp = " << Print<RegisterAggr>(LiveMap[&B], DFG) << '\n';
798
799 LV.clear();
Krzysztof Parzyszek74b1f252017-04-14 17:25:13 +0000800 const RegisterAggr &LG = LiveMap[&B];
801 for (auto I = LG.rr_begin(), E = LG.rr_end(); I != E; ++I)
802 LV.push_back(*I);
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000803 std::sort(LV.begin(), LV.end());
804 dbgs() << "\tcomp = {";
805 for (auto I : LV)
806 dbgs() << ' ' << Print<RegisterRef>(I, DFG);
807 dbgs() << " }\n";
808
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000809 }
810 }
811}
812
813
814void Liveness::resetLiveIns() {
815 for (auto &B : DFG.getMF()) {
816 // Remove all live-ins.
817 std::vector<unsigned> T;
818 for (auto I = B.livein_begin(), E = B.livein_end(); I != E; ++I)
819 T.push_back(I->PhysReg);
820 for (auto I : T)
821 B.removeLiveIn(I);
822 // Add the newly computed live-ins.
Krzysztof Parzyszek74b1f252017-04-14 17:25:13 +0000823 const RegisterAggr &LiveIns = LiveMap[&B];
824 for (auto I = LiveIns.rr_begin(), E = LiveIns.rr_end(); I != E; ++I) {
825 RegisterRef R = *I;
826 B.addLiveIn({MCPhysReg(R.Reg), R.Mask});
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000827 }
828 }
829}
830
831
832void Liveness::resetKills() {
833 for (auto &B : DFG.getMF())
834 resetKills(&B);
835}
836
837
838void Liveness::resetKills(MachineBasicBlock *B) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000839 auto CopyLiveIns = [this] (MachineBasicBlock *B, BitVector &LV) -> void {
840 for (auto I : B->liveins()) {
841 MCSubRegIndexIterator S(I.PhysReg, &TRI);
842 if (!S.isValid()) {
843 LV.set(I.PhysReg);
844 continue;
845 }
846 do {
847 LaneBitmask M = TRI.getSubRegIndexLaneMask(S.getSubRegIndex());
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000848 if ((M & I.LaneMask).any())
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000849 LV.set(S.getSubReg());
850 ++S;
851 } while (S.isValid());
852 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000853 };
854
855 BitVector LiveIn(TRI.getNumRegs()), Live(TRI.getNumRegs());
856 CopyLiveIns(B, LiveIn);
857 for (auto SI : B->successors())
858 CopyLiveIns(SI, Live);
859
860 for (auto I = B->rbegin(), E = B->rend(); I != E; ++I) {
861 MachineInstr *MI = &*I;
862 if (MI->isDebugValue())
863 continue;
864
865 MI->clearKillInfo();
866 for (auto &Op : MI->operands()) {
Krzysztof Parzyszekf69ff712016-06-02 14:30:09 +0000867 // An implicit def of a super-register may not necessarily start a
868 // live range of it, since an implicit use could be used to keep parts
869 // of it live. Instead of analyzing the implicit operands, ignore
870 // implicit defs.
871 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000872 continue;
873 unsigned R = Op.getReg();
874 if (!TargetRegisterInfo::isPhysicalRegister(R))
875 continue;
876 for (MCSubRegIterator SR(R, &TRI, true); SR.isValid(); ++SR)
877 Live.reset(*SR);
878 }
879 for (auto &Op : MI->operands()) {
Krzysztof Parzyszekace1b892017-02-22 18:29:16 +0000880 if (!Op.isReg() || !Op.isUse() || Op.isUndef())
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000881 continue;
882 unsigned R = Op.getReg();
883 if (!TargetRegisterInfo::isPhysicalRegister(R))
884 continue;
885 bool IsLive = false;
Krzysztof Parzyszek16331f02016-04-20 14:33:23 +0000886 for (MCRegAliasIterator AR(R, &TRI, true); AR.isValid(); ++AR) {
887 if (!Live[*AR])
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000888 continue;
889 IsLive = true;
890 break;
891 }
Krzysztof Parzyszek09a86382017-01-23 23:03:49 +0000892 if (!IsLive)
893 Op.setIsKill(true);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000894 for (MCSubRegIterator SR(R, &TRI, true); SR.isValid(); ++SR)
895 Live.set(*SR);
896 }
897 }
898}
899
900
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000901// Helper function to obtain the basic block containing the reaching def
902// of the given use.
903MachineBasicBlock *Liveness::getBlockWithRef(NodeId RN) const {
904 auto F = NBMap.find(RN);
905 if (F != NBMap.end())
906 return F->second;
907 llvm_unreachable("Node id not in map");
908}
909
910
911void Liveness::traverse(MachineBasicBlock *B, RefMap &LiveIn) {
912 // The LiveIn map, for each (physical) register, contains the set of live
913 // reaching defs of that register that are live on entry to the associated
914 // block.
915
916 // The summary of the traversal algorithm:
917 //
918 // R is live-in in B, if there exists a U(R), such that rdef(R) dom B
919 // and (U \in IDF(B) or B dom U).
920 //
921 // for (C : children) {
922 // LU = {}
923 // traverse(C, LU)
924 // LiveUses += LU
925 // }
926 //
927 // LiveUses -= Defs(B);
928 // LiveUses += UpwardExposedUses(B);
929 // for (C : IIDF[B])
930 // for (U : LiveUses)
931 // if (Rdef(U) dom C)
932 // C.addLiveIn(U)
933 //
934
935 // Go up the dominator tree (depth-first).
936 MachineDomTreeNode *N = MDT.getNode(B);
937 for (auto I : *N) {
938 RefMap L;
939 MachineBasicBlock *SB = I->getBlock();
940 traverse(SB, L);
941
942 for (auto S : L)
943 LiveIn[S.first].insert(S.second.begin(), S.second.end());
944 }
945
946 if (Trace) {
Reid Kleckner40d72302016-10-20 00:22:23 +0000947 dbgs() << "\n-- BB#" << B->getNumber() << ": " << __func__
Krzysztof Parzyszekc8b6eca2016-10-03 20:17:20 +0000948 << " after recursion into: {";
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000949 for (auto I : *N)
950 dbgs() << ' ' << I->getBlock()->getNumber();
Krzysztof Parzyszekc8b6eca2016-10-03 20:17:20 +0000951 dbgs() << " }\n";
952 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000953 dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000954 }
955
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000956 // Add reaching defs of phi uses that are live on exit from this block.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000957 RefMap &PUs = PhiLOX[B];
Krzysztof Parzyszek459a1c92016-10-06 13:05:46 +0000958 for (auto &S : PUs)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000959 LiveIn[S.first].insert(S.second.begin(), S.second.end());
960
961 if (Trace) {
962 dbgs() << "after LOX\n";
963 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000964 dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000965 }
966
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000967 // The LiveIn map at this point has all defs that are live-on-exit from B,
968 // as if they were live-on-entry to B. First, we need to filter out all
969 // defs that are present in this block. Then we will add reaching defs of
970 // all upward-exposed uses.
971
972 // To filter out the defs, first make a copy of LiveIn, and then re-populate
973 // LiveIn with the defs that should remain.
974 RefMap LiveInCopy = LiveIn;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000975 LiveIn.clear();
976
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000977 for (const std::pair<RegisterId,NodeRefSet> &LE : LiveInCopy) {
978 RegisterRef LRef(LE.first);
979 NodeRefSet &NewDefs = LiveIn[LRef.Reg]; // To be filled.
980 const NodeRefSet &OldDefs = LE.second;
981 for (NodeRef OR : OldDefs) {
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000982 // R is a def node that was live-on-exit
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000983 auto DA = DFG.addr<DefNode*>(OR.first);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000984 NodeAddr<InstrNode*> IA = DA.Addr->getOwner(DFG);
985 NodeAddr<BlockNode*> BA = IA.Addr->getOwner(DFG);
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000986 if (B != BA.Addr->getCode()) {
987 // Defs from a different block need to be preserved. Defs from this
988 // block will need to be processed further, except for phi defs, the
989 // liveness of which is handled through the PhiLON/PhiLOX maps.
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000990 NewDefs.insert(OR);
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000991 continue;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000992 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000993
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000994 // Defs from this block need to stop the liveness from being
995 // propagated upwards. This only applies to non-preserving defs,
996 // and to the parts of the register actually covered by those defs.
997 // (Note that phi defs should always be preserving.)
Krzysztof Parzyszek49ffff12017-01-30 17:46:56 +0000998 RegisterAggr RRs(PRI);
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000999 LRef.Mask = OR.second;
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +00001000
1001 if (!DFG.IsPreservingDef(DA)) {
1002 assert(!(IA.Addr->getFlags() & NodeAttrs::Phi));
1003 // DA is a non-phi def that is live-on-exit from this block, and
1004 // that is also located in this block. LRef is a register ref
1005 // whose use this def reaches. If DA covers LRef, then no part
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001006 // of LRef is exposed upwards.A
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001007 if (RRs.insert(DA.Addr->getRegRef(DFG)).hasCoverOf(LRef))
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +00001008 continue;
1009 }
1010
1011 // DA itself was not sufficient to cover LRef. In general, it is
1012 // the last in a chain of aliased defs before the exit from this block.
1013 // There could be other defs in this block that are a part of that
1014 // chain. Check that now: accumulate the registers from these defs,
1015 // and if they all together cover LRef, it is not live-on-entry.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001016 for (NodeAddr<DefNode*> TA : getAllReachingDefs(DA)) {
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +00001017 // DefNode -> InstrNode -> BlockNode.
1018 NodeAddr<InstrNode*> ITA = TA.Addr->getOwner(DFG);
1019 NodeAddr<BlockNode*> BTA = ITA.Addr->getOwner(DFG);
1020 // Reaching defs are ordered in the upward direction.
1021 if (BTA.Addr->getCode() != B) {
1022 // We have reached past the beginning of B, and the accumulated
1023 // registers are not covering LRef. The first def from the
1024 // upward chain will be live.
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001025 // Subtract all accumulated defs (RRs) from LRef.
Krzysztof Parzyszek74b1f252017-04-14 17:25:13 +00001026 RegisterRef T = RRs.clearIn(LRef);
1027 assert(T);
1028 NewDefs.insert({TA.Id,T.Mask});
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +00001029 break;
1030 }
1031
1032 // TA is in B. Only add this def to the accumulated cover if it is
1033 // not preserving.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001034 if (!(TA.Addr->getFlags() & NodeAttrs::Preserving))
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +00001035 RRs.insert(TA.Addr->getRegRef(DFG));
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +00001036 // If this is enough to cover LRef, then stop.
1037 if (RRs.hasCoverOf(LRef))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001038 break;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001039 }
1040 }
1041 }
1042
1043 emptify(LiveIn);
1044
1045 if (Trace) {
1046 dbgs() << "after defs in block\n";
1047 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001048 dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001049 }
1050
1051 // Scan the block for upward-exposed uses and add them to the tracking set.
1052 for (auto I : DFG.getFunc().Addr->findBlock(B, DFG).Addr->members(DFG)) {
1053 NodeAddr<InstrNode*> IA = I;
1054 if (IA.Addr->getKind() != NodeAttrs::Stmt)
1055 continue;
1056 for (NodeAddr<UseNode*> UA : IA.Addr->members_if(DFG.IsUse, DFG)) {
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +00001057 if (UA.Addr->getFlags() & NodeAttrs::Undef)
1058 continue;
Krzysztof Parzyszek5226ba82017-02-16 18:45:23 +00001059 RegisterRef RR = PRI.normalize(UA.Addr->getRegRef(DFG));
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001060 for (NodeAddr<DefNode*> D : getAllReachingDefs(UA))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001061 if (getBlockWithRef(D.Id) != B)
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001062 LiveIn[RR.Reg].insert({D.Id,RR.Mask});
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001063 }
1064 }
1065
1066 if (Trace) {
1067 dbgs() << "after uses in block\n";
1068 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001069 dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001070 }
1071
1072 // Phi uses should not be propagated up the dominator tree, since they
1073 // are not dominated by their corresponding reaching defs.
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001074 RegisterAggr &Local = LiveMap[B];
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001075 RefMap &LON = PhiLON[B];
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001076 for (auto &R : LON) {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001077 LaneBitmask M;
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001078 for (auto P : R.second)
1079 M |= P.second;
1080 Local.insert(RegisterRef(R.first,M));
1081 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001082
1083 if (Trace) {
1084 dbgs() << "after phi uses in block\n";
1085 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001086 dbgs() << " Local: " << Print<RegisterAggr>(Local, DFG) << '\n';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001087 }
1088
1089 for (auto C : IIDF[B]) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001090 RegisterAggr &LiveC = LiveMap[C];
1091 for (const std::pair<RegisterId,NodeRefSet> &S : LiveIn)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001092 for (auto R : S.second)
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001093 if (MDT.properlyDominates(getBlockWithRef(R.first), C))
1094 LiveC.insert(RegisterRef(S.first, R.second));
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001095 }
1096}
1097
1098
1099void Liveness::emptify(RefMap &M) {
1100 for (auto I = M.begin(), E = M.end(); I != E; )
1101 I = I->second.empty() ? M.erase(I) : std::next(I);
1102}
1103