blob: f0f93285254a2f3f18f24a2e4ba8f9500380586c [file] [log] [blame]
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001//===--- RDFLiveness.cpp --------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Computation of the liveness information from the data-flow graph.
11//
12// The main functionality of this code is to compute block live-in
13// information. With the live-in information in place, the placement
14// of kill flags can also be recalculated.
15//
16// The block live-in calculation is based on the ideas from the following
17// publication:
18//
19// Dibyendu Das, Ramakrishna Upadrasta, Benoit Dupont de Dinechin.
20// "Efficient Liveness Computation Using Merge Sets and DJ-Graphs."
21// ACM Transactions on Architecture and Code Optimization, Association for
22// Computing Machinery, 2012, ACM TACO Special Issue on "High-Performance
23// and Embedded Architectures and Compilers", 8 (4),
24// <10.1145/2086696.2086706>. <hal-00647369>
25//
26#include "RDFGraph.h"
27#include "RDFLiveness.h"
28#include "llvm/ADT/SetVector.h"
29#include "llvm/CodeGen/MachineBasicBlock.h"
30#include "llvm/CodeGen/MachineDominanceFrontier.h"
31#include "llvm/CodeGen/MachineDominators.h"
32#include "llvm/CodeGen/MachineFunction.h"
33#include "llvm/CodeGen/MachineRegisterInfo.h"
34#include "llvm/Target/TargetRegisterInfo.h"
35
36using namespace llvm;
37using namespace rdf;
38
Benjamin Kramer922efd72016-05-27 10:06:40 +000039namespace llvm {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +000040namespace rdf {
41 template<>
42 raw_ostream &operator<< (raw_ostream &OS, const Print<Liveness::RefMap> &P) {
43 OS << '{';
Krzysztof Parzyszek459a1c92016-10-06 13:05:46 +000044 for (auto &I : P.Obj) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +000045 OS << ' ' << PrintReg(I.first, &P.G.getTRI()) << '{';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +000046 for (auto J = I.second.begin(), E = I.second.end(); J != E; ) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +000047 OS << Print<NodeId>(J->first, P.G) << PrintLaneMaskOpt(J->second);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +000048 if (++J != E)
49 OS << ',';
50 }
51 OS << '}';
52 }
53 OS << " }";
54 return OS;
55 }
Benjamin Kramer922efd72016-05-27 10:06:40 +000056} // namespace rdf
57} // namespace llvm
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +000058
59// The order in the returned sequence is the order of reaching defs in the
60// upward traversal: the first def is the closest to the given reference RefA,
61// the next one is further up, and so on.
62// The list ends at a reaching phi def, or when the reference from RefA is
63// covered by the defs in the list (see FullChain).
64// This function provides two modes of operation:
65// (1) Returning the sequence of reaching defs for a particular reference
66// node. This sequence will terminate at the first phi node [1].
67// (2) Returning a partial sequence of reaching defs, where the final goal
68// is to traverse past phi nodes to the actual defs arising from the code
69// itself.
70// In mode (2), the register reference for which the search was started
71// may be different from the reference node RefA, for which this call was
72// made, hence the argument RefRR, which holds the original register.
73// Also, some definitions may have already been encountered in a previous
74// call that will influence register covering. The register references
75// already defined are passed in through DefRRs.
76// In mode (1), the "continuation" considerations do not apply, and the
77// RefRR is the same as the register in RefA, and the set DefRRs is empty.
78//
79// [1] It is possible for multiple phi nodes to be included in the returned
80// sequence:
81// SubA = phi ...
82// SubB = phi ...
83// ... = SuperAB(rdef:SubA), SuperAB"(rdef:SubB)
84// However, these phi nodes are independent from one another in terms of
85// the data-flow.
86
87NodeList Liveness::getAllReachingDefs(RegisterRef RefRR,
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +000088 NodeAddr<RefNode*> RefA, bool FullChain, const RegisterAggr &DefRRs) {
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +000089 NodeList RDefs; // Return value.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +000090 SetVector<NodeId> DefQ;
91 SetVector<NodeId> Owners;
92
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +000093 // Dead defs will be treated as if they were live, since they are actually
94 // on the data-flow path. They cannot be ignored because even though they
95 // do not generate meaningful values, they still modify registers.
96
97 // If the reference is undefined, there is nothing to do.
98 if (RefA.Addr->getFlags() & NodeAttrs::Undef)
99 return RDefs;
100
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000101 // The initial queue should not have reaching defs for shadows. The
102 // whole point of a shadow is that it will have a reaching def that
103 // is not aliased to the reaching defs of the related shadows.
104 NodeId Start = RefA.Id;
105 auto SNA = DFG.addr<RefNode*>(Start);
106 if (NodeId RD = SNA.Addr->getReachingDef())
107 DefQ.insert(RD);
108
109 // Collect all the reaching defs, going up until a phi node is encountered,
110 // or there are no more reaching defs. From this set, the actual set of
111 // reaching defs will be selected.
112 // The traversal upwards must go on until a covering def is encountered.
113 // It is possible that a collection of non-covering (individually) defs
114 // will be sufficient, but keep going until a covering one is found.
115 for (unsigned i = 0; i < DefQ.size(); ++i) {
116 auto TA = DFG.addr<DefNode*>(DefQ[i]);
117 if (TA.Addr->getFlags() & NodeAttrs::PhiRef)
118 continue;
119 // Stop at the covering/overwriting def of the initial register reference.
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000120 RegisterRef RR = TA.Addr->getRegRef(DFG);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000121 if (!DFG.IsPreservingDef(TA))
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000122 if (RegisterAggr::isCoverOf(RR, RefRR, TRI))
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000123 continue;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000124 // Get the next level of reaching defs. This will include multiple
125 // reaching defs for shadows.
126 for (auto S : DFG.getRelatedRefs(TA.Addr->getOwner(DFG), TA))
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000127 if (NodeId RD = NodeAddr<RefNode*>(S).Addr->getReachingDef())
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000128 DefQ.insert(RD);
129 }
130
131 // Remove all non-phi defs that are not aliased to RefRR, and collect
132 // the owners of the remaining defs.
133 SetVector<NodeId> Defs;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000134 for (NodeId N : DefQ) {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000135 auto TA = DFG.addr<DefNode*>(N);
136 bool IsPhi = TA.Addr->getFlags() & NodeAttrs::PhiRef;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000137 if (!IsPhi && !DFG.alias(RefRR, TA.Addr->getRegRef(DFG)))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000138 continue;
139 Defs.insert(TA.Id);
140 Owners.insert(TA.Addr->getOwner(DFG).Id);
141 }
142
143 // Return the MachineBasicBlock containing a given instruction.
144 auto Block = [this] (NodeAddr<InstrNode*> IA) -> MachineBasicBlock* {
145 if (IA.Addr->getKind() == NodeAttrs::Stmt)
146 return NodeAddr<StmtNode*>(IA).Addr->getCode()->getParent();
147 assert(IA.Addr->getKind() == NodeAttrs::Phi);
148 NodeAddr<PhiNode*> PA = IA;
149 NodeAddr<BlockNode*> BA = PA.Addr->getOwner(DFG);
150 return BA.Addr->getCode();
151 };
152 // Less(A,B) iff instruction A is further down in the dominator tree than B.
153 auto Less = [&Block,this] (NodeId A, NodeId B) -> bool {
154 if (A == B)
155 return false;
156 auto OA = DFG.addr<InstrNode*>(A), OB = DFG.addr<InstrNode*>(B);
157 MachineBasicBlock *BA = Block(OA), *BB = Block(OB);
158 if (BA != BB)
159 return MDT.dominates(BB, BA);
160 // They are in the same block.
161 bool StmtA = OA.Addr->getKind() == NodeAttrs::Stmt;
162 bool StmtB = OB.Addr->getKind() == NodeAttrs::Stmt;
163 if (StmtA) {
164 if (!StmtB) // OB is a phi and phis dominate statements.
165 return true;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000166 MachineInstr *CA = NodeAddr<StmtNode*>(OA).Addr->getCode();
167 MachineInstr *CB = NodeAddr<StmtNode*>(OB).Addr->getCode();
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000168 // The order must be linear, so tie-break such equalities.
169 if (CA == CB)
170 return A < B;
171 return MDT.dominates(CB, CA);
172 } else {
173 // OA is a phi.
174 if (StmtB)
175 return false;
176 // Both are phis. There is no ordering between phis (in terms of
177 // the data-flow), so tie-break this via node id comparison.
178 return A < B;
179 }
180 };
181
182 std::vector<NodeId> Tmp(Owners.begin(), Owners.end());
183 std::sort(Tmp.begin(), Tmp.end(), Less);
184
185 // The vector is a list of instructions, so that defs coming from
186 // the same instruction don't need to be artificially ordered.
187 // Then, when computing the initial segment, and iterating over an
188 // instruction, pick the defs that contribute to the covering (i.e. is
189 // not covered by previously added defs). Check the defs individually,
190 // i.e. first check each def if is covered or not (without adding them
191 // to the tracking set), and then add all the selected ones.
192
193 // The reason for this is this example:
194 // *d1<A>, *d2<B>, ... Assume A and B are aliased (can happen in phi nodes).
195 // *d3<C> If A \incl BuC, and B \incl AuC, then *d2 would be
196 // covered if we added A first, and A would be covered
197 // if we added B first.
198
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000199 RegisterAggr RRs(DefRRs);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000200
201 auto DefInSet = [&Defs] (NodeAddr<RefNode*> TA) -> bool {
202 return TA.Addr->getKind() == NodeAttrs::Def &&
203 Defs.count(TA.Id);
204 };
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000205 for (NodeId T : Tmp) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000206 if (!FullChain && RRs.hasCoverOf(RefRR))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000207 break;
208 auto TA = DFG.addr<InstrNode*>(T);
209 bool IsPhi = DFG.IsCode<NodeAttrs::Phi>(TA);
210 NodeList Ds;
211 for (NodeAddr<DefNode*> DA : TA.Addr->members_if(DefInSet, DFG)) {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000212 RegisterRef QR = DA.Addr->getRegRef(DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000213 // Add phi defs even if they are covered by subsequent defs. This is
214 // for cases where the reached use is not covered by any of the defs
215 // encountered so far: the phi def is needed to expose the liveness
216 // of that use to the entry of the block.
217 // Example:
218 // phi d1<R3>(,d2,), ... Phi def d1 is covered by d2.
219 // d2<R3>(d1,,u3), ...
220 // ..., u3<D1>(d2) This use needs to be live on entry.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000221 if (FullChain || IsPhi || !RRs.hasCoverOf(QR))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000222 Ds.push_back(DA);
223 }
224 RDefs.insert(RDefs.end(), Ds.begin(), Ds.end());
225 for (NodeAddr<DefNode*> DA : Ds) {
226 // When collecting a full chain of definitions, do not consider phi
227 // defs to actually define a register.
228 uint16_t Flags = DA.Addr->getFlags();
229 if (!FullChain || !(Flags & NodeAttrs::PhiRef))
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000230 if (!(Flags & NodeAttrs::Preserving)) // Don't care about Undef here.
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000231 RRs.insert(DA.Addr->getRegRef(DFG));
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000232 }
233 }
234
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +0000235 auto DeadP = [](const NodeAddr<DefNode*> DA) -> bool {
236 return DA.Addr->getFlags() & NodeAttrs::Dead;
237 };
238 RDefs.resize(std::distance(RDefs.begin(), remove_if(RDefs, DeadP)));
239
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000240 return RDefs;
241}
242
243
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000244NodeSet Liveness::getAllReachingDefsRec(RegisterRef RefRR,
245 NodeAddr<RefNode*> RefA, NodeSet &Visited, const NodeSet &Defs) {
246 // Collect all defined registers. Do not consider phis to be defining
247 // anything, only collect "real" definitions.
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000248 RegisterAggr DefRRs(TRI);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000249 for (NodeId D : Defs) {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000250 const auto DA = DFG.addr<const DefNode*>(D);
251 if (!(DA.Addr->getFlags() & NodeAttrs::PhiRef))
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000252 DefRRs.insert(DA.Addr->getRegRef(DFG));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000253 }
254
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000255 NodeList RDs = getAllReachingDefs(RefRR, RefA, true, DefRRs);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000256 if (RDs.empty())
257 return Defs;
258
259 // Make a copy of the preexisting definitions and add the newly found ones.
260 NodeSet TmpDefs = Defs;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000261 for (NodeAddr<NodeBase*> R : RDs)
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000262 TmpDefs.insert(R.Id);
263
264 NodeSet Result = Defs;
265
266 for (NodeAddr<DefNode*> DA : RDs) {
267 Result.insert(DA.Id);
268 if (!(DA.Addr->getFlags() & NodeAttrs::PhiRef))
269 continue;
270 NodeAddr<PhiNode*> PA = DA.Addr->getOwner(DFG);
271 if (Visited.count(PA.Id))
272 continue;
273 Visited.insert(PA.Id);
274 // Go over all phi uses and get the reaching defs for each use.
275 for (auto U : PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG)) {
276 const auto &T = getAllReachingDefsRec(RefRR, U, Visited, TmpDefs);
277 Result.insert(T.begin(), T.end());
278 }
279 }
280
281 return Result;
282}
283
284
285NodeSet Liveness::getAllReachedUses(RegisterRef RefRR,
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000286 NodeAddr<DefNode*> DefA, const RegisterAggr &DefRRs) {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000287 NodeSet Uses;
288
289 // If the original register is already covered by all the intervening
290 // defs, no more uses can be reached.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000291 if (DefRRs.hasCoverOf(RefRR))
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000292 return Uses;
293
294 // Add all directly reached uses.
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +0000295 // If the def is dead, it does not provide a value for any use.
296 bool IsDead = DefA.Addr->getFlags() & NodeAttrs::Dead;
297 NodeId U = !IsDead ? DefA.Addr->getReachedUse() : 0;
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000298 while (U != 0) {
299 auto UA = DFG.addr<UseNode*>(U);
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000300 if (!(UA.Addr->getFlags() & NodeAttrs::Undef)) {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000301 RegisterRef UR = UA.Addr->getRegRef(DFG);
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000302 if (DFG.alias(RefRR, UR) && !DefRRs.hasCoverOf(UR))
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000303 Uses.insert(U);
304 }
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000305 U = UA.Addr->getSibling();
306 }
307
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +0000308 // Traverse all reached defs. This time dead defs cannot be ignored.
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000309 for (NodeId D = DefA.Addr->getReachedDef(), NextD; D != 0; D = NextD) {
310 auto DA = DFG.addr<DefNode*>(D);
311 NextD = DA.Addr->getSibling();
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000312 RegisterRef DR = DA.Addr->getRegRef(DFG);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000313 // If this def is already covered, it cannot reach anything new.
314 // Similarly, skip it if it is not aliased to the interesting register.
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000315 if (DefRRs.hasCoverOf(DR) || !DFG.alias(RefRR, DR))
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000316 continue;
317 NodeSet T;
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000318 if (DFG.IsPreservingDef(DA)) {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000319 // If it is a preserving def, do not update the set of intervening defs.
320 T = getAllReachedUses(RefRR, DA, DefRRs);
321 } else {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000322 RegisterAggr NewDefRRs = DefRRs;
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000323 NewDefRRs.insert(DR);
324 T = getAllReachedUses(RefRR, DA, NewDefRRs);
325 }
326 Uses.insert(T.begin(), T.end());
327 }
328 return Uses;
329}
330
331
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000332void Liveness::computePhiInfo() {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000333 RealUseMap.clear();
334
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000335 NodeList Phis;
336 NodeAddr<FuncNode*> FA = DFG.getFunc();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000337 NodeList Blocks = FA.Addr->members(DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000338 for (NodeAddr<BlockNode*> BA : Blocks) {
339 auto Ps = BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG);
340 Phis.insert(Phis.end(), Ps.begin(), Ps.end());
341 }
342
343 // phi use -> (map: reaching phi -> set of registers defined in between)
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000344 std::map<NodeId,std::map<NodeId,RegisterAggr>> PhiUp;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000345 std::vector<NodeId> PhiUQ; // Work list of phis for upward propagation.
346
347 // Go over all phis.
348 for (NodeAddr<PhiNode*> PhiA : Phis) {
349 // Go over all defs and collect the reached uses that are non-phi uses
350 // (i.e. the "real uses").
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000351 RefMap &RealUses = RealUseMap[PhiA.Id];
352 NodeList PhiRefs = PhiA.Addr->members(DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000353
354 // Have a work queue of defs whose reached uses need to be found.
355 // For each def, add to the queue all reached (non-phi) defs.
356 SetVector<NodeId> DefQ;
357 NodeSet PhiDefs;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000358 for (NodeAddr<RefNode*> R : PhiRefs) {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000359 if (!DFG.IsRef<NodeAttrs::Def>(R))
360 continue;
361 DefQ.insert(R.Id);
362 PhiDefs.insert(R.Id);
363 }
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000364
365 // Collect the super-set of all possible reached uses. This set will
366 // contain all uses reached from this phi, either directly from the
367 // phi defs, or (recursively) via non-phi defs reached by the phi defs.
368 // This set of uses will later be trimmed to only contain these uses that
369 // are actually reached by the phi defs.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000370 for (unsigned i = 0; i < DefQ.size(); ++i) {
371 NodeAddr<DefNode*> DA = DFG.addr<DefNode*>(DefQ[i]);
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +0000372 // Visit all reached uses. Phi defs should not really have the "dead"
373 // flag set, but check it anyway for consistency.
374 bool IsDead = DA.Addr->getFlags() & NodeAttrs::Dead;
375 NodeId UN = !IsDead ? DA.Addr->getReachedUse() : 0;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000376 while (UN != 0) {
377 NodeAddr<UseNode*> A = DFG.addr<UseNode*>(UN);
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000378 uint16_t F = A.Addr->getFlags();
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000379 if ((F & (NodeAttrs::Undef | NodeAttrs::PhiRef)) == 0) {
Krzysztof Parzyszek09a86382017-01-23 23:03:49 +0000380 RegisterRef R = DFG.normalizeRef(getRestrictedRegRef(A));
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000381 RealUses[R.Reg].insert({A.Id,R.Mask});
Krzysztof Parzyszek09a86382017-01-23 23:03:49 +0000382 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000383 UN = A.Addr->getSibling();
384 }
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000385 // Visit all reached defs, and add them to the queue. These defs may
386 // override some of the uses collected here, but that will be handled
387 // later.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000388 NodeId DN = DA.Addr->getReachedDef();
389 while (DN != 0) {
390 NodeAddr<DefNode*> A = DFG.addr<DefNode*>(DN);
391 for (auto T : DFG.getRelatedRefs(A.Addr->getOwner(DFG), A)) {
392 uint16_t Flags = NodeAddr<DefNode*>(T).Addr->getFlags();
393 // Must traverse the reached-def chain. Consider:
394 // def(D0) -> def(R0) -> def(R0) -> use(D0)
395 // The reachable use of D0 passes through a def of R0.
396 if (!(Flags & NodeAttrs::PhiRef))
397 DefQ.insert(T.Id);
398 }
399 DN = A.Addr->getSibling();
400 }
401 }
402 // Filter out these uses that appear to be reachable, but really
403 // are not. For example:
404 //
405 // R1:0 = d1
406 // = R1:0 u2 Reached by d1.
407 // R0 = d3
408 // = R1:0 u4 Still reached by d1: indirectly through
409 // the def d3.
410 // R1 = d5
411 // = R1:0 u6 Not reached by d1 (covered collectively
412 // by d3 and d5), but following reached
413 // defs and uses from d1 will lead here.
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000414 auto InPhiDefs = [&PhiDefs] (NodeAddr<DefNode*> DA) -> bool {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000415 return PhiDefs.count(DA.Id);
416 };
417 for (auto UI = RealUses.begin(), UE = RealUses.end(); UI != UE; ) {
418 // For each reached register UI->first, there is a set UI->second, of
419 // uses of it. For each such use, check if it is reached by this phi,
420 // i.e. check if the set of its reaching uses intersects the set of
421 // this phi's defs.
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000422 NodeRefSet &Uses = UI->second;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000423 for (auto I = Uses.begin(), E = Uses.end(); I != E; ) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000424 auto UA = DFG.addr<UseNode*>(I->first);
Krzysztof Parzyszek586fc122016-09-27 18:24:33 +0000425 // Undef flag is checked above.
426 assert((UA.Addr->getFlags() & NodeAttrs::Undef) == 0);
Krzysztof Parzyszek09a86382017-01-23 23:03:49 +0000427 RegisterRef R(UI->first, I->second);
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000428 NodeList RDs = getAllReachingDefs(R, UA);
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000429 if (any_of(RDs, InPhiDefs))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000430 ++I;
431 else
432 I = Uses.erase(I);
433 }
434 if (Uses.empty())
435 UI = RealUses.erase(UI);
436 else
437 ++UI;
438 }
439
440 // If this phi reaches some "real" uses, add it to the queue for upward
441 // propagation.
442 if (!RealUses.empty())
443 PhiUQ.push_back(PhiA.Id);
444
445 // Go over all phi uses and check if the reaching def is another phi.
446 // Collect the phis that are among the reaching defs of these uses.
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000447 // While traversing the list of reaching defs for each phi use, accumulate
448 // the set of registers defined between this phi (PhiA) and the owner phi
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000449 // of the reaching def.
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000450 NodeSet SeenUses;
Krzysztof Parzyszeka1218722016-09-08 20:48:42 +0000451
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000452 for (auto I : PhiRefs) {
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000453 if (!DFG.IsRef<NodeAttrs::Use>(I) || SeenUses.count(I.Id))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000454 continue;
455 NodeAddr<UseNode*> UA = I;
Krzysztof Parzyszeka1218722016-09-08 20:48:42 +0000456
457 // Given a phi use UA, traverse all related phi uses (including UA).
458 // The related phi uses may reach different phi nodes or may reach the
459 // same phi node. If multiple uses reach the same phi P, the intervening
460 // defs must be accumulated for all such uses. To group all such uses
461 // into one set, map their node ids to the first use id that reaches P.
462 std::map<NodeId,NodeId> FirstUse; // Phi reached up -> first phi use.
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000463
464 for (NodeAddr<UseNode*> VA : DFG.getRelatedRefs(PhiA, UA)) {
465 SeenUses.insert(VA.Id);
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000466 RegisterAggr DefRRs(TRI);
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000467 for (NodeAddr<DefNode*> DA : getAllReachingDefs(VA)) {
468 if (DA.Addr->getFlags() & NodeAttrs::PhiRef) {
Krzysztof Parzyszeka1218722016-09-08 20:48:42 +0000469 NodeId RP = DA.Addr->getOwner(DFG).Id;
470 NodeId FU = FirstUse.insert({RP,VA.Id}).first->second;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000471 std::map<NodeId,RegisterAggr> &M = PhiUp[FU];
472 auto F = M.find(RP);
473 if (F == M.end())
474 M.insert(std::make_pair(RP, DefRRs));
475 else
476 F->second.insert(DefRRs);
Krzysztof Parzyszeka1218722016-09-08 20:48:42 +0000477 }
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000478 DefRRs.insert(DA.Addr->getRegRef(DFG));
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000479 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000480 }
481 }
482 }
483
484 if (Trace) {
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000485 dbgs() << "Phi-up-to-phi map with intervening defs:\n";
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000486 for (auto I : PhiUp) {
487 dbgs() << "phi " << Print<NodeId>(I.first, DFG) << " -> {";
488 for (auto R : I.second)
489 dbgs() << ' ' << Print<NodeId>(R.first, DFG)
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000490 << Print<RegisterAggr>(R.second, DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000491 dbgs() << " }\n";
492 }
493 }
494
495 // Propagate the reached registers up in the phi chain.
496 //
497 // The following type of situation needs careful handling:
498 //
499 // phi d1<R1:0> (1)
500 // |
501 // ... d2<R1>
502 // |
503 // phi u3<R1:0> (2)
504 // |
505 // ... u4<R1>
506 //
507 // The phi node (2) defines a register pair R1:0, and reaches a "real"
508 // use u4 of just R1. The same phi node is also known to reach (upwards)
509 // the phi node (1). However, the use u4 is not reached by phi (1),
510 // because of the intervening definition d2 of R1. The data flow between
511 // phis (1) and (2) is restricted to R1:0 minus R1, i.e. R0.
512 //
513 // When propagating uses up the phi chains, get the all reaching defs
514 // for a given phi use, and traverse the list until the propagated ref
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000515 // is covered, or until reaching the final phi. Only assume that the
516 // reference reaches the phi in the latter case.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000517
518 for (unsigned i = 0; i < PhiUQ.size(); ++i) {
519 auto PA = DFG.addr<PhiNode*>(PhiUQ[i]);
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000520 NodeList PUs = PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG);
521 RefMap &RUM = RealUseMap[PA.Id];
522
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000523 for (NodeAddr<UseNode*> UA : PUs) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000524 std::map<NodeId,RegisterAggr> &PUM = PhiUp[UA.Id];
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000525 RegisterRef UR = DFG.normalizeRef(getRestrictedRegRef(UA));
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000526 for (const std::pair<NodeId,RegisterAggr> &P : PUM) {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000527 bool Changed = false;
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000528 const RegisterAggr &MidDefs = P.second;
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000529
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000530 // Collect the set PropUp of uses that are reached by the current
531 // phi PA, and are not covered by any intervening def between the
532 // currently visited use UA and the the upward phi P.
533
534 if (MidDefs.hasCoverOf(UR))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000535 continue;
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000536
537 // General algorithm:
538 // for each (R,U) : U is use node of R, U is reached by PA
539 // if MidDefs does not cover (R,U)
540 // then add (R-MidDefs,U) to RealUseMap[P]
541 //
542 for (const std::pair<RegisterId,NodeRefSet> &T : RUM) {
543 RegisterRef R = DFG.restrictRef(RegisterRef(T.first), UR);
544 if (!R)
545 continue;
546 for (std::pair<NodeId,LaneBitmask> V : T.second) {
547 RegisterRef S = DFG.restrictRef(RegisterRef(R.Reg, V.second), R);
548 if (!S)
549 continue;
550 if (RegisterRef SS = MidDefs.clearIn(S)) {
551 NodeRefSet &RS = RealUseMap[P.first][SS.Reg];
552 Changed |= RS.insert({V.first,SS.Mask}).second;
553 }
554 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000555 }
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000556
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000557 if (Changed)
Krzysztof Parzyszek2db0c8b2016-09-07 20:37:05 +0000558 PhiUQ.push_back(P.first);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000559 }
560 }
561 }
562
563 if (Trace) {
564 dbgs() << "Real use map:\n";
565 for (auto I : RealUseMap) {
566 dbgs() << "phi " << Print<NodeId>(I.first, DFG);
567 NodeAddr<PhiNode*> PA = DFG.addr<PhiNode*>(I.first);
568 NodeList Ds = PA.Addr->members_if(DFG.IsRef<NodeAttrs::Def>, DFG);
569 if (!Ds.empty()) {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000570 RegisterRef RR = NodeAddr<DefNode*>(Ds[0]).Addr->getRegRef(DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000571 dbgs() << '<' << Print<RegisterRef>(RR, DFG) << '>';
572 } else {
573 dbgs() << "<noreg>";
574 }
575 dbgs() << " -> " << Print<RefMap>(I.second, DFG) << '\n';
576 }
577 }
578}
579
580
581void Liveness::computeLiveIns() {
582 // Populate the node-to-block map. This speeds up the calculations
583 // significantly.
584 NBMap.clear();
585 for (NodeAddr<BlockNode*> BA : DFG.getFunc().Addr->members(DFG)) {
586 MachineBasicBlock *BB = BA.Addr->getCode();
587 for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG)) {
588 for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG))
589 NBMap.insert(std::make_pair(RA.Id, BB));
590 NBMap.insert(std::make_pair(IA.Id, BB));
591 }
592 }
593
594 MachineFunction &MF = DFG.getMF();
595
596 // Compute IDF first, then the inverse.
597 decltype(IIDF) IDF;
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000598 for (MachineBasicBlock &B : MF) {
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000599 auto F1 = MDF.find(&B);
600 if (F1 == MDF.end())
601 continue;
602 SetVector<MachineBasicBlock*> IDFB(F1->second.begin(), F1->second.end());
603 for (unsigned i = 0; i < IDFB.size(); ++i) {
604 auto F2 = MDF.find(IDFB[i]);
605 if (F2 != MDF.end())
606 IDFB.insert(F2->second.begin(), F2->second.end());
607 }
608 // Add B to the IDF(B). This will put B in the IIDF(B).
609 IDFB.insert(&B);
610 IDF[&B].insert(IDFB.begin(), IDFB.end());
611 }
612
613 for (auto I : IDF)
614 for (auto S : I.second)
615 IIDF[S].insert(I.first);
616
617 computePhiInfo();
618
619 NodeAddr<FuncNode*> FA = DFG.getFunc();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000620 NodeList Blocks = FA.Addr->members(DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000621
622 // Build the phi live-on-entry map.
623 for (NodeAddr<BlockNode*> BA : Blocks) {
624 MachineBasicBlock *MB = BA.Addr->getCode();
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000625 RefMap &LON = PhiLON[MB];
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000626 for (auto P : BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG))
Krzysztof Parzyszek459a1c92016-10-06 13:05:46 +0000627 for (const RefMap::value_type &S : RealUseMap[P.Id])
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000628 LON[S.first].insert(S.second.begin(), S.second.end());
629 }
630
631 if (Trace) {
632 dbgs() << "Phi live-on-entry map:\n";
Krzysztof Parzyszek459a1c92016-10-06 13:05:46 +0000633 for (auto &I : PhiLON)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000634 dbgs() << "block #" << I.first->getNumber() << " -> "
635 << Print<RefMap>(I.second, DFG) << '\n';
636 }
637
638 // Build the phi live-on-exit map. Each phi node has some set of reached
639 // "real" uses. Propagate this set backwards into the block predecessors
640 // through the reaching defs of the corresponding phi uses.
641 for (NodeAddr<BlockNode*> BA : Blocks) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000642 NodeList Phis = BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000643 for (NodeAddr<PhiNode*> PA : Phis) {
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000644 RefMap &RUs = RealUseMap[PA.Id];
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000645 if (RUs.empty())
646 continue;
647
648 for (auto U : PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG)) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000649 NodeAddr<PhiUseNode*> PUA = U;
650 if (PUA.Addr->getReachingDef() == 0)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000651 continue;
652
653 // Mark all reached "real" uses of P as live on exit in the
654 // predecessor.
655 // Remap all the RUs so that they have a correct reaching def.
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000656 auto PrA = DFG.addr<BlockNode*>(PUA.Addr->getPredecessor());
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000657 RefMap &LOX = PhiLOX[PrA.Addr->getCode()];
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000658
659 RegisterRef UR = DFG.normalizeRef(getRestrictedRegRef(PUA));
660 for (const std::pair<RegisterId,NodeRefSet> &T : RUs) {
661 // Check if T.first aliases UR?
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000662 LaneBitmask M;
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000663 for (std::pair<NodeId,LaneBitmask> P : T.second)
664 M |= P.second;
665
666 RegisterRef S = DFG.restrictRef(RegisterRef(T.first, M), UR);
667 if (!S)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000668 continue;
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000669 for (NodeAddr<DefNode*> D : getAllReachingDefs(S, PUA))
670 LOX[S.Reg].insert({D.Id, S.Mask});
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000671 }
672 } // for U : phi uses
673 } // for P : Phis
674 } // for B : Blocks
675
676 if (Trace) {
677 dbgs() << "Phi live-on-exit map:\n";
Krzysztof Parzyszek459a1c92016-10-06 13:05:46 +0000678 for (auto &I : PhiLOX)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000679 dbgs() << "block #" << I.first->getNumber() << " -> "
680 << Print<RefMap>(I.second, DFG) << '\n';
681 }
682
683 RefMap LiveIn;
684 traverse(&MF.front(), LiveIn);
685
686 // Add function live-ins to the live-in set of the function entry block.
687 auto &EntryIn = LiveMap[&MF.front()];
688 for (auto I = MRI.livein_begin(), E = MRI.livein_end(); I != E; ++I)
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000689 EntryIn.insert(RegisterRef(I->first));
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000690
691 if (Trace) {
692 // Dump the liveness map
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000693 for (MachineBasicBlock &B : MF) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000694 std::vector<RegisterRef> LV;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000695 for (auto I = B.livein_begin(), E = B.livein_end(); I != E; ++I)
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000696 LV.push_back(RegisterRef(I->PhysReg, I->LaneMask));
697 std::sort(LV.begin(), LV.end());
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000698 dbgs() << "BB#" << B.getNumber() << "\t rec = {";
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000699 for (auto I : LV)
700 dbgs() << ' ' << Print<RegisterRef>(I, DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000701 dbgs() << " }\n";
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000702 //dbgs() << "\tcomp = " << Print<RegisterAggr>(LiveMap[&B], DFG) << '\n';
703
704 LV.clear();
705 for (std::pair<RegisterId,LaneBitmask> P : LiveMap[&B]) {
706 MCSubRegIndexIterator S(P.first, &TRI);
707 if (!S.isValid()) {
708 LV.push_back(RegisterRef(P.first));
709 continue;
710 }
711 do {
712 LaneBitmask M = TRI.getSubRegIndexLaneMask(S.getSubRegIndex());
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000713 if ((M & P.second).any())
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000714 LV.push_back(RegisterRef(S.getSubReg()));
715 ++S;
716 } while (S.isValid());
717 }
718 std::sort(LV.begin(), LV.end());
719 dbgs() << "\tcomp = {";
720 for (auto I : LV)
721 dbgs() << ' ' << Print<RegisterRef>(I, DFG);
722 dbgs() << " }\n";
723
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000724 }
725 }
726}
727
728
729void Liveness::resetLiveIns() {
730 for (auto &B : DFG.getMF()) {
731 // Remove all live-ins.
732 std::vector<unsigned> T;
733 for (auto I = B.livein_begin(), E = B.livein_end(); I != E; ++I)
734 T.push_back(I->PhysReg);
735 for (auto I : T)
736 B.removeLiveIn(I);
737 // Add the newly computed live-ins.
738 auto &LiveIns = LiveMap[&B];
739 for (auto I : LiveIns) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000740 B.addLiveIn({MCPhysReg(I.first), I.second});
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000741 }
742 }
743}
744
745
746void Liveness::resetKills() {
747 for (auto &B : DFG.getMF())
748 resetKills(&B);
749}
750
751
752void Liveness::resetKills(MachineBasicBlock *B) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000753 auto CopyLiveIns = [this] (MachineBasicBlock *B, BitVector &LV) -> void {
754 for (auto I : B->liveins()) {
755 MCSubRegIndexIterator S(I.PhysReg, &TRI);
756 if (!S.isValid()) {
757 LV.set(I.PhysReg);
758 continue;
759 }
760 do {
761 LaneBitmask M = TRI.getSubRegIndexLaneMask(S.getSubRegIndex());
Krzysztof Parzyszekea9f8ce2016-12-16 19:11:56 +0000762 if ((M & I.LaneMask).any())
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000763 LV.set(S.getSubReg());
764 ++S;
765 } while (S.isValid());
766 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000767 };
768
769 BitVector LiveIn(TRI.getNumRegs()), Live(TRI.getNumRegs());
770 CopyLiveIns(B, LiveIn);
771 for (auto SI : B->successors())
772 CopyLiveIns(SI, Live);
773
774 for (auto I = B->rbegin(), E = B->rend(); I != E; ++I) {
775 MachineInstr *MI = &*I;
776 if (MI->isDebugValue())
777 continue;
778
779 MI->clearKillInfo();
780 for (auto &Op : MI->operands()) {
Krzysztof Parzyszekf69ff712016-06-02 14:30:09 +0000781 // An implicit def of a super-register may not necessarily start a
782 // live range of it, since an implicit use could be used to keep parts
783 // of it live. Instead of analyzing the implicit operands, ignore
784 // implicit defs.
785 if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000786 continue;
787 unsigned R = Op.getReg();
788 if (!TargetRegisterInfo::isPhysicalRegister(R))
789 continue;
790 for (MCSubRegIterator SR(R, &TRI, true); SR.isValid(); ++SR)
791 Live.reset(*SR);
792 }
793 for (auto &Op : MI->operands()) {
794 if (!Op.isReg() || !Op.isUse())
795 continue;
796 unsigned R = Op.getReg();
797 if (!TargetRegisterInfo::isPhysicalRegister(R))
798 continue;
799 bool IsLive = false;
Krzysztof Parzyszek16331f02016-04-20 14:33:23 +0000800 for (MCRegAliasIterator AR(R, &TRI, true); AR.isValid(); ++AR) {
801 if (!Live[*AR])
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000802 continue;
803 IsLive = true;
804 break;
805 }
Krzysztof Parzyszek09a86382017-01-23 23:03:49 +0000806 if (!IsLive)
807 Op.setIsKill(true);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000808 for (MCSubRegIterator SR(R, &TRI, true); SR.isValid(); ++SR)
809 Live.set(*SR);
810 }
811 }
812}
813
814
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000815RegisterRef Liveness::getRestrictedRegRef(NodeAddr<RefNode*> RA) const {
816 assert(DFG.IsRef<NodeAttrs::Use>(RA));
817 if (RA.Addr->getFlags() & NodeAttrs::Shadow) {
818 NodeId RD = RA.Addr->getReachingDef();
819 assert(RD);
820 RA = DFG.addr<DefNode*>(RD);
821 }
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000822 return RA.Addr->getRegRef(DFG);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000823}
824
825
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000826// Helper function to obtain the basic block containing the reaching def
827// of the given use.
828MachineBasicBlock *Liveness::getBlockWithRef(NodeId RN) const {
829 auto F = NBMap.find(RN);
830 if (F != NBMap.end())
831 return F->second;
832 llvm_unreachable("Node id not in map");
833}
834
835
836void Liveness::traverse(MachineBasicBlock *B, RefMap &LiveIn) {
837 // The LiveIn map, for each (physical) register, contains the set of live
838 // reaching defs of that register that are live on entry to the associated
839 // block.
840
841 // The summary of the traversal algorithm:
842 //
843 // R is live-in in B, if there exists a U(R), such that rdef(R) dom B
844 // and (U \in IDF(B) or B dom U).
845 //
846 // for (C : children) {
847 // LU = {}
848 // traverse(C, LU)
849 // LiveUses += LU
850 // }
851 //
852 // LiveUses -= Defs(B);
853 // LiveUses += UpwardExposedUses(B);
854 // for (C : IIDF[B])
855 // for (U : LiveUses)
856 // if (Rdef(U) dom C)
857 // C.addLiveIn(U)
858 //
859
860 // Go up the dominator tree (depth-first).
861 MachineDomTreeNode *N = MDT.getNode(B);
862 for (auto I : *N) {
863 RefMap L;
864 MachineBasicBlock *SB = I->getBlock();
865 traverse(SB, L);
866
867 for (auto S : L)
868 LiveIn[S.first].insert(S.second.begin(), S.second.end());
869 }
870
871 if (Trace) {
Reid Kleckner40d72302016-10-20 00:22:23 +0000872 dbgs() << "\n-- BB#" << B->getNumber() << ": " << __func__
Krzysztof Parzyszekc8b6eca2016-10-03 20:17:20 +0000873 << " after recursion into: {";
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000874 for (auto I : *N)
875 dbgs() << ' ' << I->getBlock()->getNumber();
Krzysztof Parzyszekc8b6eca2016-10-03 20:17:20 +0000876 dbgs() << " }\n";
877 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000878 dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000879 }
880
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000881 // Add reaching defs of phi uses that are live on exit from this block.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000882 RefMap &PUs = PhiLOX[B];
Krzysztof Parzyszek459a1c92016-10-06 13:05:46 +0000883 for (auto &S : PUs)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000884 LiveIn[S.first].insert(S.second.begin(), S.second.end());
885
886 if (Trace) {
887 dbgs() << "after LOX\n";
888 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000889 dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000890 }
891
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000892 // The LiveIn map at this point has all defs that are live-on-exit from B,
893 // as if they were live-on-entry to B. First, we need to filter out all
894 // defs that are present in this block. Then we will add reaching defs of
895 // all upward-exposed uses.
896
897 // To filter out the defs, first make a copy of LiveIn, and then re-populate
898 // LiveIn with the defs that should remain.
899 RefMap LiveInCopy = LiveIn;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000900 LiveIn.clear();
901
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000902 for (const std::pair<RegisterId,NodeRefSet> &LE : LiveInCopy) {
903 RegisterRef LRef(LE.first);
904 NodeRefSet &NewDefs = LiveIn[LRef.Reg]; // To be filled.
905 const NodeRefSet &OldDefs = LE.second;
906 for (NodeRef OR : OldDefs) {
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000907 // R is a def node that was live-on-exit
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000908 auto DA = DFG.addr<DefNode*>(OR.first);
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000909 NodeAddr<InstrNode*> IA = DA.Addr->getOwner(DFG);
910 NodeAddr<BlockNode*> BA = IA.Addr->getOwner(DFG);
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000911 if (B != BA.Addr->getCode()) {
912 // Defs from a different block need to be preserved. Defs from this
913 // block will need to be processed further, except for phi defs, the
914 // liveness of which is handled through the PhiLON/PhiLOX maps.
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000915 NewDefs.insert(OR);
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000916 continue;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000917 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000918
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000919 // Defs from this block need to stop the liveness from being
920 // propagated upwards. This only applies to non-preserving defs,
921 // and to the parts of the register actually covered by those defs.
922 // (Note that phi defs should always be preserving.)
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000923 RegisterAggr RRs(TRI);
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000924 LRef.Mask = OR.second;
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000925
926 if (!DFG.IsPreservingDef(DA)) {
927 assert(!(IA.Addr->getFlags() & NodeAttrs::Phi));
928 // DA is a non-phi def that is live-on-exit from this block, and
929 // that is also located in this block. LRef is a register ref
930 // whose use this def reaches. If DA covers LRef, then no part
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000931 // of LRef is exposed upwards.A
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000932 if (RRs.insert(DA.Addr->getRegRef(DFG)).hasCoverOf(LRef))
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000933 continue;
934 }
935
936 // DA itself was not sufficient to cover LRef. In general, it is
937 // the last in a chain of aliased defs before the exit from this block.
938 // There could be other defs in this block that are a part of that
939 // chain. Check that now: accumulate the registers from these defs,
940 // and if they all together cover LRef, it is not live-on-entry.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000941 for (NodeAddr<DefNode*> TA : getAllReachingDefs(DA)) {
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000942 // DefNode -> InstrNode -> BlockNode.
943 NodeAddr<InstrNode*> ITA = TA.Addr->getOwner(DFG);
944 NodeAddr<BlockNode*> BTA = ITA.Addr->getOwner(DFG);
945 // Reaching defs are ordered in the upward direction.
946 if (BTA.Addr->getCode() != B) {
947 // We have reached past the beginning of B, and the accumulated
948 // registers are not covering LRef. The first def from the
949 // upward chain will be live.
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000950 // Subtract all accumulated defs (RRs) from LRef.
951 RegisterAggr L(TRI);
952 L.insert(LRef).clear(RRs);
953 assert(!L.empty());
954 NewDefs.insert({TA.Id,L.begin()->second});
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000955 break;
956 }
957
958 // TA is in B. Only add this def to the accumulated cover if it is
959 // not preserving.
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000960 if (!(TA.Addr->getFlags() & NodeAttrs::Preserving))
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000961 RRs.insert(TA.Addr->getRegRef(DFG));
Krzysztof Parzyszek3b6cbd52016-10-05 20:08:09 +0000962 // If this is enough to cover LRef, then stop.
963 if (RRs.hasCoverOf(LRef))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000964 break;
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000965 }
966 }
967 }
968
969 emptify(LiveIn);
970
971 if (Trace) {
972 dbgs() << "after defs in block\n";
973 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000974 dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000975 }
976
977 // Scan the block for upward-exposed uses and add them to the tracking set.
978 for (auto I : DFG.getFunc().Addr->findBlock(B, DFG).Addr->members(DFG)) {
979 NodeAddr<InstrNode*> IA = I;
980 if (IA.Addr->getKind() != NodeAttrs::Stmt)
981 continue;
982 for (NodeAddr<UseNode*> UA : IA.Addr->members_if(DFG.IsUse, DFG)) {
Krzysztof Parzyszek1ff99522016-09-07 20:10:56 +0000983 if (UA.Addr->getFlags() & NodeAttrs::Undef)
984 continue;
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000985 RegisterRef RR = DFG.normalizeRef(UA.Addr->getRegRef(DFG));
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +0000986 for (NodeAddr<DefNode*> D : getAllReachingDefs(UA))
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000987 if (getBlockWithRef(D.Id) != B)
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000988 LiveIn[RR.Reg].insert({D.Id,RR.Mask});
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000989 }
990 }
991
992 if (Trace) {
993 dbgs() << "after uses in block\n";
994 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000995 dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +0000996 }
997
998 // Phi uses should not be propagated up the dominator tree, since they
999 // are not dominated by their corresponding reaching defs.
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001000 RegisterAggr &Local = LiveMap[B];
Krzysztof Parzyszek61d90322016-10-06 13:05:13 +00001001 RefMap &LON = PhiLON[B];
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001002 for (auto &R : LON) {
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +00001003 LaneBitmask M;
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001004 for (auto P : R.second)
1005 M |= P.second;
1006 Local.insert(RegisterRef(R.first,M));
1007 }
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001008
1009 if (Trace) {
1010 dbgs() << "after phi uses in block\n";
1011 dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001012 dbgs() << " Local: " << Print<RegisterAggr>(Local, DFG) << '\n';
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001013 }
1014
1015 for (auto C : IIDF[B]) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001016 RegisterAggr &LiveC = LiveMap[C];
1017 for (const std::pair<RegisterId,NodeRefSet> &S : LiveIn)
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001018 for (auto R : S.second)
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +00001019 if (MDT.properlyDominates(getBlockWithRef(R.first), C))
1020 LiveC.insert(RegisterRef(S.first, R.second));
Krzysztof Parzyszekacdff462016-01-12 15:56:33 +00001021 }
1022}
1023
1024
1025void Liveness::emptify(RefMap &M) {
1026 for (auto I = M.begin(), E = M.end(); I != E; )
1027 I = I->second.empty() ? M.erase(I) : std::next(I);
1028}
1029