1*9880d681SAndroid Build Coastguard Worker //===--- RDFLiveness.cpp --------------------------------------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // Computation of the liveness information from the data-flow graph.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker // The main functionality of this code is to compute block live-in
13*9880d681SAndroid Build Coastguard Worker // information. With the live-in information in place, the placement
14*9880d681SAndroid Build Coastguard Worker // of kill flags can also be recalculated.
15*9880d681SAndroid Build Coastguard Worker //
16*9880d681SAndroid Build Coastguard Worker // The block live-in calculation is based on the ideas from the following
17*9880d681SAndroid Build Coastguard Worker // publication:
18*9880d681SAndroid Build Coastguard Worker //
19*9880d681SAndroid Build Coastguard Worker // Dibyendu Das, Ramakrishna Upadrasta, Benoit Dupont de Dinechin.
20*9880d681SAndroid Build Coastguard Worker // "Efficient Liveness Computation Using Merge Sets and DJ-Graphs."
21*9880d681SAndroid Build Coastguard Worker // ACM Transactions on Architecture and Code Optimization, Association for
22*9880d681SAndroid Build Coastguard Worker // Computing Machinery, 2012, ACM TACO Special Issue on "High-Performance
23*9880d681SAndroid Build Coastguard Worker // and Embedded Architectures and Compilers", 8 (4),
24*9880d681SAndroid Build Coastguard Worker // <10.1145/2086696.2086706>. <hal-00647369>
25*9880d681SAndroid Build Coastguard Worker //
26*9880d681SAndroid Build Coastguard Worker #include "RDFGraph.h"
27*9880d681SAndroid Build Coastguard Worker #include "RDFLiveness.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SetVector.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineBasicBlock.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineDominanceFrontier.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineDominators.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunction.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetRegisterInfo.h"
35*9880d681SAndroid Build Coastguard Worker
36*9880d681SAndroid Build Coastguard Worker using namespace llvm;
37*9880d681SAndroid Build Coastguard Worker using namespace rdf;
38*9880d681SAndroid Build Coastguard Worker
39*9880d681SAndroid Build Coastguard Worker namespace llvm {
40*9880d681SAndroid Build Coastguard Worker namespace rdf {
41*9880d681SAndroid Build Coastguard Worker template<>
operator <<(raw_ostream & OS,const Print<Liveness::RefMap> & P)42*9880d681SAndroid Build Coastguard Worker raw_ostream &operator<< (raw_ostream &OS, const Print<Liveness::RefMap> &P) {
43*9880d681SAndroid Build Coastguard Worker OS << '{';
44*9880d681SAndroid Build Coastguard Worker for (auto I : P.Obj) {
45*9880d681SAndroid Build Coastguard Worker OS << ' ' << Print<RegisterRef>(I.first, P.G) << '{';
46*9880d681SAndroid Build Coastguard Worker for (auto J = I.second.begin(), E = I.second.end(); J != E; ) {
47*9880d681SAndroid Build Coastguard Worker OS << Print<NodeId>(*J, P.G);
48*9880d681SAndroid Build Coastguard Worker if (++J != E)
49*9880d681SAndroid Build Coastguard Worker OS << ',';
50*9880d681SAndroid Build Coastguard Worker }
51*9880d681SAndroid Build Coastguard Worker OS << '}';
52*9880d681SAndroid Build Coastguard Worker }
53*9880d681SAndroid Build Coastguard Worker OS << " }";
54*9880d681SAndroid Build Coastguard Worker return OS;
55*9880d681SAndroid Build Coastguard Worker }
56*9880d681SAndroid Build Coastguard Worker } // namespace rdf
57*9880d681SAndroid Build Coastguard Worker } // namespace llvm
58*9880d681SAndroid Build Coastguard Worker
59*9880d681SAndroid Build Coastguard Worker // The order in the returned sequence is the order of reaching defs in the
60*9880d681SAndroid Build Coastguard Worker // upward traversal: the first def is the closest to the given reference RefA,
61*9880d681SAndroid Build Coastguard Worker // the next one is further up, and so on.
62*9880d681SAndroid Build Coastguard Worker // The list ends at a reaching phi def, or when the reference from RefA is
63*9880d681SAndroid Build Coastguard Worker // covered by the defs in the list (see FullChain).
64*9880d681SAndroid Build Coastguard Worker // This function provides two modes of operation:
65*9880d681SAndroid Build Coastguard Worker // (1) Returning the sequence of reaching defs for a particular reference
66*9880d681SAndroid Build Coastguard Worker // node. This sequence will terminate at the first phi node [1].
67*9880d681SAndroid Build Coastguard Worker // (2) Returning a partial sequence of reaching defs, where the final goal
68*9880d681SAndroid Build Coastguard Worker // is to traverse past phi nodes to the actual defs arising from the code
69*9880d681SAndroid Build Coastguard Worker // itself.
70*9880d681SAndroid Build Coastguard Worker // In mode (2), the register reference for which the search was started
71*9880d681SAndroid Build Coastguard Worker // may be different from the reference node RefA, for which this call was
72*9880d681SAndroid Build Coastguard Worker // made, hence the argument RefRR, which holds the original register.
73*9880d681SAndroid Build Coastguard Worker // Also, some definitions may have already been encountered in a previous
74*9880d681SAndroid Build Coastguard Worker // call that will influence register covering. The register references
75*9880d681SAndroid Build Coastguard Worker // already defined are passed in through DefRRs.
76*9880d681SAndroid Build Coastguard Worker // In mode (1), the "continuation" considerations do not apply, and the
77*9880d681SAndroid Build Coastguard Worker // RefRR is the same as the register in RefA, and the set DefRRs is empty.
78*9880d681SAndroid Build Coastguard Worker //
79*9880d681SAndroid Build Coastguard Worker // [1] It is possible for multiple phi nodes to be included in the returned
80*9880d681SAndroid Build Coastguard Worker // sequence:
81*9880d681SAndroid Build Coastguard Worker // SubA = phi ...
82*9880d681SAndroid Build Coastguard Worker // SubB = phi ...
83*9880d681SAndroid Build Coastguard Worker // ... = SuperAB(rdef:SubA), SuperAB"(rdef:SubB)
84*9880d681SAndroid Build Coastguard Worker // However, these phi nodes are independent from one another in terms of
85*9880d681SAndroid Build Coastguard Worker // the data-flow.
86*9880d681SAndroid Build Coastguard Worker
getAllReachingDefs(RegisterRef RefRR,NodeAddr<RefNode * > RefA,bool FullChain,const RegisterSet & DefRRs)87*9880d681SAndroid Build Coastguard Worker NodeList Liveness::getAllReachingDefs(RegisterRef RefRR,
88*9880d681SAndroid Build Coastguard Worker NodeAddr<RefNode*> RefA, bool FullChain, const RegisterSet &DefRRs) {
89*9880d681SAndroid Build Coastguard Worker SetVector<NodeId> DefQ;
90*9880d681SAndroid Build Coastguard Worker SetVector<NodeId> Owners;
91*9880d681SAndroid Build Coastguard Worker
92*9880d681SAndroid Build Coastguard Worker // The initial queue should not have reaching defs for shadows. The
93*9880d681SAndroid Build Coastguard Worker // whole point of a shadow is that it will have a reaching def that
94*9880d681SAndroid Build Coastguard Worker // is not aliased to the reaching defs of the related shadows.
95*9880d681SAndroid Build Coastguard Worker NodeId Start = RefA.Id;
96*9880d681SAndroid Build Coastguard Worker auto SNA = DFG.addr<RefNode*>(Start);
97*9880d681SAndroid Build Coastguard Worker if (NodeId RD = SNA.Addr->getReachingDef())
98*9880d681SAndroid Build Coastguard Worker DefQ.insert(RD);
99*9880d681SAndroid Build Coastguard Worker
100*9880d681SAndroid Build Coastguard Worker // Collect all the reaching defs, going up until a phi node is encountered,
101*9880d681SAndroid Build Coastguard Worker // or there are no more reaching defs. From this set, the actual set of
102*9880d681SAndroid Build Coastguard Worker // reaching defs will be selected.
103*9880d681SAndroid Build Coastguard Worker // The traversal upwards must go on until a covering def is encountered.
104*9880d681SAndroid Build Coastguard Worker // It is possible that a collection of non-covering (individually) defs
105*9880d681SAndroid Build Coastguard Worker // will be sufficient, but keep going until a covering one is found.
106*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i < DefQ.size(); ++i) {
107*9880d681SAndroid Build Coastguard Worker auto TA = DFG.addr<DefNode*>(DefQ[i]);
108*9880d681SAndroid Build Coastguard Worker if (TA.Addr->getFlags() & NodeAttrs::PhiRef)
109*9880d681SAndroid Build Coastguard Worker continue;
110*9880d681SAndroid Build Coastguard Worker // Stop at the covering/overwriting def of the initial register reference.
111*9880d681SAndroid Build Coastguard Worker RegisterRef RR = TA.Addr->getRegRef();
112*9880d681SAndroid Build Coastguard Worker if (RAI.covers(RR, RefRR)) {
113*9880d681SAndroid Build Coastguard Worker uint16_t Flags = TA.Addr->getFlags();
114*9880d681SAndroid Build Coastguard Worker if (!(Flags & NodeAttrs::Preserving))
115*9880d681SAndroid Build Coastguard Worker continue;
116*9880d681SAndroid Build Coastguard Worker }
117*9880d681SAndroid Build Coastguard Worker // Get the next level of reaching defs. This will include multiple
118*9880d681SAndroid Build Coastguard Worker // reaching defs for shadows.
119*9880d681SAndroid Build Coastguard Worker for (auto S : DFG.getRelatedRefs(TA.Addr->getOwner(DFG), TA))
120*9880d681SAndroid Build Coastguard Worker if (auto RD = NodeAddr<RefNode*>(S).Addr->getReachingDef())
121*9880d681SAndroid Build Coastguard Worker DefQ.insert(RD);
122*9880d681SAndroid Build Coastguard Worker }
123*9880d681SAndroid Build Coastguard Worker
124*9880d681SAndroid Build Coastguard Worker // Remove all non-phi defs that are not aliased to RefRR, and collect
125*9880d681SAndroid Build Coastguard Worker // the owners of the remaining defs.
126*9880d681SAndroid Build Coastguard Worker SetVector<NodeId> Defs;
127*9880d681SAndroid Build Coastguard Worker for (auto N : DefQ) {
128*9880d681SAndroid Build Coastguard Worker auto TA = DFG.addr<DefNode*>(N);
129*9880d681SAndroid Build Coastguard Worker bool IsPhi = TA.Addr->getFlags() & NodeAttrs::PhiRef;
130*9880d681SAndroid Build Coastguard Worker if (!IsPhi && !RAI.alias(RefRR, TA.Addr->getRegRef()))
131*9880d681SAndroid Build Coastguard Worker continue;
132*9880d681SAndroid Build Coastguard Worker Defs.insert(TA.Id);
133*9880d681SAndroid Build Coastguard Worker Owners.insert(TA.Addr->getOwner(DFG).Id);
134*9880d681SAndroid Build Coastguard Worker }
135*9880d681SAndroid Build Coastguard Worker
136*9880d681SAndroid Build Coastguard Worker // Return the MachineBasicBlock containing a given instruction.
137*9880d681SAndroid Build Coastguard Worker auto Block = [this] (NodeAddr<InstrNode*> IA) -> MachineBasicBlock* {
138*9880d681SAndroid Build Coastguard Worker if (IA.Addr->getKind() == NodeAttrs::Stmt)
139*9880d681SAndroid Build Coastguard Worker return NodeAddr<StmtNode*>(IA).Addr->getCode()->getParent();
140*9880d681SAndroid Build Coastguard Worker assert(IA.Addr->getKind() == NodeAttrs::Phi);
141*9880d681SAndroid Build Coastguard Worker NodeAddr<PhiNode*> PA = IA;
142*9880d681SAndroid Build Coastguard Worker NodeAddr<BlockNode*> BA = PA.Addr->getOwner(DFG);
143*9880d681SAndroid Build Coastguard Worker return BA.Addr->getCode();
144*9880d681SAndroid Build Coastguard Worker };
145*9880d681SAndroid Build Coastguard Worker // Less(A,B) iff instruction A is further down in the dominator tree than B.
146*9880d681SAndroid Build Coastguard Worker auto Less = [&Block,this] (NodeId A, NodeId B) -> bool {
147*9880d681SAndroid Build Coastguard Worker if (A == B)
148*9880d681SAndroid Build Coastguard Worker return false;
149*9880d681SAndroid Build Coastguard Worker auto OA = DFG.addr<InstrNode*>(A), OB = DFG.addr<InstrNode*>(B);
150*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *BA = Block(OA), *BB = Block(OB);
151*9880d681SAndroid Build Coastguard Worker if (BA != BB)
152*9880d681SAndroid Build Coastguard Worker return MDT.dominates(BB, BA);
153*9880d681SAndroid Build Coastguard Worker // They are in the same block.
154*9880d681SAndroid Build Coastguard Worker bool StmtA = OA.Addr->getKind() == NodeAttrs::Stmt;
155*9880d681SAndroid Build Coastguard Worker bool StmtB = OB.Addr->getKind() == NodeAttrs::Stmt;
156*9880d681SAndroid Build Coastguard Worker if (StmtA) {
157*9880d681SAndroid Build Coastguard Worker if (!StmtB) // OB is a phi and phis dominate statements.
158*9880d681SAndroid Build Coastguard Worker return true;
159*9880d681SAndroid Build Coastguard Worker auto CA = NodeAddr<StmtNode*>(OA).Addr->getCode();
160*9880d681SAndroid Build Coastguard Worker auto CB = NodeAddr<StmtNode*>(OB).Addr->getCode();
161*9880d681SAndroid Build Coastguard Worker // The order must be linear, so tie-break such equalities.
162*9880d681SAndroid Build Coastguard Worker if (CA == CB)
163*9880d681SAndroid Build Coastguard Worker return A < B;
164*9880d681SAndroid Build Coastguard Worker return MDT.dominates(CB, CA);
165*9880d681SAndroid Build Coastguard Worker } else {
166*9880d681SAndroid Build Coastguard Worker // OA is a phi.
167*9880d681SAndroid Build Coastguard Worker if (StmtB)
168*9880d681SAndroid Build Coastguard Worker return false;
169*9880d681SAndroid Build Coastguard Worker // Both are phis. There is no ordering between phis (in terms of
170*9880d681SAndroid Build Coastguard Worker // the data-flow), so tie-break this via node id comparison.
171*9880d681SAndroid Build Coastguard Worker return A < B;
172*9880d681SAndroid Build Coastguard Worker }
173*9880d681SAndroid Build Coastguard Worker };
174*9880d681SAndroid Build Coastguard Worker
175*9880d681SAndroid Build Coastguard Worker std::vector<NodeId> Tmp(Owners.begin(), Owners.end());
176*9880d681SAndroid Build Coastguard Worker std::sort(Tmp.begin(), Tmp.end(), Less);
177*9880d681SAndroid Build Coastguard Worker
178*9880d681SAndroid Build Coastguard Worker // The vector is a list of instructions, so that defs coming from
179*9880d681SAndroid Build Coastguard Worker // the same instruction don't need to be artificially ordered.
180*9880d681SAndroid Build Coastguard Worker // Then, when computing the initial segment, and iterating over an
181*9880d681SAndroid Build Coastguard Worker // instruction, pick the defs that contribute to the covering (i.e. is
182*9880d681SAndroid Build Coastguard Worker // not covered by previously added defs). Check the defs individually,
183*9880d681SAndroid Build Coastguard Worker // i.e. first check each def if is covered or not (without adding them
184*9880d681SAndroid Build Coastguard Worker // to the tracking set), and then add all the selected ones.
185*9880d681SAndroid Build Coastguard Worker
186*9880d681SAndroid Build Coastguard Worker // The reason for this is this example:
187*9880d681SAndroid Build Coastguard Worker // *d1<A>, *d2<B>, ... Assume A and B are aliased (can happen in phi nodes).
188*9880d681SAndroid Build Coastguard Worker // *d3<C> If A \incl BuC, and B \incl AuC, then *d2 would be
189*9880d681SAndroid Build Coastguard Worker // covered if we added A first, and A would be covered
190*9880d681SAndroid Build Coastguard Worker // if we added B first.
191*9880d681SAndroid Build Coastguard Worker
192*9880d681SAndroid Build Coastguard Worker NodeList RDefs;
193*9880d681SAndroid Build Coastguard Worker RegisterSet RRs = DefRRs;
194*9880d681SAndroid Build Coastguard Worker
195*9880d681SAndroid Build Coastguard Worker auto DefInSet = [&Defs] (NodeAddr<RefNode*> TA) -> bool {
196*9880d681SAndroid Build Coastguard Worker return TA.Addr->getKind() == NodeAttrs::Def &&
197*9880d681SAndroid Build Coastguard Worker Defs.count(TA.Id);
198*9880d681SAndroid Build Coastguard Worker };
199*9880d681SAndroid Build Coastguard Worker for (auto T : Tmp) {
200*9880d681SAndroid Build Coastguard Worker if (!FullChain && RAI.covers(RRs, RefRR))
201*9880d681SAndroid Build Coastguard Worker break;
202*9880d681SAndroid Build Coastguard Worker auto TA = DFG.addr<InstrNode*>(T);
203*9880d681SAndroid Build Coastguard Worker bool IsPhi = DFG.IsCode<NodeAttrs::Phi>(TA);
204*9880d681SAndroid Build Coastguard Worker NodeList Ds;
205*9880d681SAndroid Build Coastguard Worker for (NodeAddr<DefNode*> DA : TA.Addr->members_if(DefInSet, DFG)) {
206*9880d681SAndroid Build Coastguard Worker auto QR = DA.Addr->getRegRef();
207*9880d681SAndroid Build Coastguard Worker // Add phi defs even if they are covered by subsequent defs. This is
208*9880d681SAndroid Build Coastguard Worker // for cases where the reached use is not covered by any of the defs
209*9880d681SAndroid Build Coastguard Worker // encountered so far: the phi def is needed to expose the liveness
210*9880d681SAndroid Build Coastguard Worker // of that use to the entry of the block.
211*9880d681SAndroid Build Coastguard Worker // Example:
212*9880d681SAndroid Build Coastguard Worker // phi d1<R3>(,d2,), ... Phi def d1 is covered by d2.
213*9880d681SAndroid Build Coastguard Worker // d2<R3>(d1,,u3), ...
214*9880d681SAndroid Build Coastguard Worker // ..., u3<D1>(d2) This use needs to be live on entry.
215*9880d681SAndroid Build Coastguard Worker if (FullChain || IsPhi || !RAI.covers(RRs, QR))
216*9880d681SAndroid Build Coastguard Worker Ds.push_back(DA);
217*9880d681SAndroid Build Coastguard Worker }
218*9880d681SAndroid Build Coastguard Worker RDefs.insert(RDefs.end(), Ds.begin(), Ds.end());
219*9880d681SAndroid Build Coastguard Worker for (NodeAddr<DefNode*> DA : Ds) {
220*9880d681SAndroid Build Coastguard Worker // When collecting a full chain of definitions, do not consider phi
221*9880d681SAndroid Build Coastguard Worker // defs to actually define a register.
222*9880d681SAndroid Build Coastguard Worker uint16_t Flags = DA.Addr->getFlags();
223*9880d681SAndroid Build Coastguard Worker if (!FullChain || !(Flags & NodeAttrs::PhiRef))
224*9880d681SAndroid Build Coastguard Worker if (!(Flags & NodeAttrs::Preserving))
225*9880d681SAndroid Build Coastguard Worker RRs.insert(DA.Addr->getRegRef());
226*9880d681SAndroid Build Coastguard Worker }
227*9880d681SAndroid Build Coastguard Worker }
228*9880d681SAndroid Build Coastguard Worker
229*9880d681SAndroid Build Coastguard Worker return RDefs;
230*9880d681SAndroid Build Coastguard Worker }
231*9880d681SAndroid Build Coastguard Worker
232*9880d681SAndroid Build Coastguard Worker
233*9880d681SAndroid Build Coastguard Worker static const RegisterSet NoRegs;
234*9880d681SAndroid Build Coastguard Worker
getAllReachingDefs(NodeAddr<RefNode * > RefA)235*9880d681SAndroid Build Coastguard Worker NodeList Liveness::getAllReachingDefs(NodeAddr<RefNode*> RefA) {
236*9880d681SAndroid Build Coastguard Worker return getAllReachingDefs(RefA.Addr->getRegRef(), RefA, false, NoRegs);
237*9880d681SAndroid Build Coastguard Worker }
238*9880d681SAndroid Build Coastguard Worker
239*9880d681SAndroid Build Coastguard Worker
getAllReachingDefsRec(RegisterRef RefRR,NodeAddr<RefNode * > RefA,NodeSet & Visited,const NodeSet & Defs)240*9880d681SAndroid Build Coastguard Worker NodeSet Liveness::getAllReachingDefsRec(RegisterRef RefRR,
241*9880d681SAndroid Build Coastguard Worker NodeAddr<RefNode*> RefA, NodeSet &Visited, const NodeSet &Defs) {
242*9880d681SAndroid Build Coastguard Worker // Collect all defined registers. Do not consider phis to be defining
243*9880d681SAndroid Build Coastguard Worker // anything, only collect "real" definitions.
244*9880d681SAndroid Build Coastguard Worker RegisterSet DefRRs;
245*9880d681SAndroid Build Coastguard Worker for (const auto D : Defs) {
246*9880d681SAndroid Build Coastguard Worker const auto DA = DFG.addr<const DefNode*>(D);
247*9880d681SAndroid Build Coastguard Worker if (!(DA.Addr->getFlags() & NodeAttrs::PhiRef))
248*9880d681SAndroid Build Coastguard Worker DefRRs.insert(DA.Addr->getRegRef());
249*9880d681SAndroid Build Coastguard Worker }
250*9880d681SAndroid Build Coastguard Worker
251*9880d681SAndroid Build Coastguard Worker auto RDs = getAllReachingDefs(RefRR, RefA, true, DefRRs);
252*9880d681SAndroid Build Coastguard Worker if (RDs.empty())
253*9880d681SAndroid Build Coastguard Worker return Defs;
254*9880d681SAndroid Build Coastguard Worker
255*9880d681SAndroid Build Coastguard Worker // Make a copy of the preexisting definitions and add the newly found ones.
256*9880d681SAndroid Build Coastguard Worker NodeSet TmpDefs = Defs;
257*9880d681SAndroid Build Coastguard Worker for (auto R : RDs)
258*9880d681SAndroid Build Coastguard Worker TmpDefs.insert(R.Id);
259*9880d681SAndroid Build Coastguard Worker
260*9880d681SAndroid Build Coastguard Worker NodeSet Result = Defs;
261*9880d681SAndroid Build Coastguard Worker
262*9880d681SAndroid Build Coastguard Worker for (NodeAddr<DefNode*> DA : RDs) {
263*9880d681SAndroid Build Coastguard Worker Result.insert(DA.Id);
264*9880d681SAndroid Build Coastguard Worker if (!(DA.Addr->getFlags() & NodeAttrs::PhiRef))
265*9880d681SAndroid Build Coastguard Worker continue;
266*9880d681SAndroid Build Coastguard Worker NodeAddr<PhiNode*> PA = DA.Addr->getOwner(DFG);
267*9880d681SAndroid Build Coastguard Worker if (Visited.count(PA.Id))
268*9880d681SAndroid Build Coastguard Worker continue;
269*9880d681SAndroid Build Coastguard Worker Visited.insert(PA.Id);
270*9880d681SAndroid Build Coastguard Worker // Go over all phi uses and get the reaching defs for each use.
271*9880d681SAndroid Build Coastguard Worker for (auto U : PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG)) {
272*9880d681SAndroid Build Coastguard Worker const auto &T = getAllReachingDefsRec(RefRR, U, Visited, TmpDefs);
273*9880d681SAndroid Build Coastguard Worker Result.insert(T.begin(), T.end());
274*9880d681SAndroid Build Coastguard Worker }
275*9880d681SAndroid Build Coastguard Worker }
276*9880d681SAndroid Build Coastguard Worker
277*9880d681SAndroid Build Coastguard Worker return Result;
278*9880d681SAndroid Build Coastguard Worker }
279*9880d681SAndroid Build Coastguard Worker
280*9880d681SAndroid Build Coastguard Worker
getAllReachedUses(RegisterRef RefRR,NodeAddr<DefNode * > DefA,const RegisterSet & DefRRs)281*9880d681SAndroid Build Coastguard Worker NodeSet Liveness::getAllReachedUses(RegisterRef RefRR,
282*9880d681SAndroid Build Coastguard Worker NodeAddr<DefNode*> DefA, const RegisterSet &DefRRs) {
283*9880d681SAndroid Build Coastguard Worker NodeSet Uses;
284*9880d681SAndroid Build Coastguard Worker
285*9880d681SAndroid Build Coastguard Worker // If the original register is already covered by all the intervening
286*9880d681SAndroid Build Coastguard Worker // defs, no more uses can be reached.
287*9880d681SAndroid Build Coastguard Worker if (RAI.covers(DefRRs, RefRR))
288*9880d681SAndroid Build Coastguard Worker return Uses;
289*9880d681SAndroid Build Coastguard Worker
290*9880d681SAndroid Build Coastguard Worker // Add all directly reached uses.
291*9880d681SAndroid Build Coastguard Worker NodeId U = DefA.Addr->getReachedUse();
292*9880d681SAndroid Build Coastguard Worker while (U != 0) {
293*9880d681SAndroid Build Coastguard Worker auto UA = DFG.addr<UseNode*>(U);
294*9880d681SAndroid Build Coastguard Worker auto UR = UA.Addr->getRegRef();
295*9880d681SAndroid Build Coastguard Worker if (RAI.alias(RefRR, UR) && !RAI.covers(DefRRs, UR))
296*9880d681SAndroid Build Coastguard Worker Uses.insert(U);
297*9880d681SAndroid Build Coastguard Worker U = UA.Addr->getSibling();
298*9880d681SAndroid Build Coastguard Worker }
299*9880d681SAndroid Build Coastguard Worker
300*9880d681SAndroid Build Coastguard Worker // Traverse all reached defs.
301*9880d681SAndroid Build Coastguard Worker for (NodeId D = DefA.Addr->getReachedDef(), NextD; D != 0; D = NextD) {
302*9880d681SAndroid Build Coastguard Worker auto DA = DFG.addr<DefNode*>(D);
303*9880d681SAndroid Build Coastguard Worker NextD = DA.Addr->getSibling();
304*9880d681SAndroid Build Coastguard Worker auto DR = DA.Addr->getRegRef();
305*9880d681SAndroid Build Coastguard Worker // If this def is already covered, it cannot reach anything new.
306*9880d681SAndroid Build Coastguard Worker // Similarly, skip it if it is not aliased to the interesting register.
307*9880d681SAndroid Build Coastguard Worker if (RAI.covers(DefRRs, DR) || !RAI.alias(RefRR, DR))
308*9880d681SAndroid Build Coastguard Worker continue;
309*9880d681SAndroid Build Coastguard Worker NodeSet T;
310*9880d681SAndroid Build Coastguard Worker if (DA.Addr->getFlags() & NodeAttrs::Preserving) {
311*9880d681SAndroid Build Coastguard Worker // If it is a preserving def, do not update the set of intervening defs.
312*9880d681SAndroid Build Coastguard Worker T = getAllReachedUses(RefRR, DA, DefRRs);
313*9880d681SAndroid Build Coastguard Worker } else {
314*9880d681SAndroid Build Coastguard Worker RegisterSet NewDefRRs = DefRRs;
315*9880d681SAndroid Build Coastguard Worker NewDefRRs.insert(DR);
316*9880d681SAndroid Build Coastguard Worker T = getAllReachedUses(RefRR, DA, NewDefRRs);
317*9880d681SAndroid Build Coastguard Worker }
318*9880d681SAndroid Build Coastguard Worker Uses.insert(T.begin(), T.end());
319*9880d681SAndroid Build Coastguard Worker }
320*9880d681SAndroid Build Coastguard Worker return Uses;
321*9880d681SAndroid Build Coastguard Worker }
322*9880d681SAndroid Build Coastguard Worker
323*9880d681SAndroid Build Coastguard Worker
computePhiInfo()324*9880d681SAndroid Build Coastguard Worker void Liveness::computePhiInfo() {
325*9880d681SAndroid Build Coastguard Worker RealUseMap.clear();
326*9880d681SAndroid Build Coastguard Worker
327*9880d681SAndroid Build Coastguard Worker NodeList Phis;
328*9880d681SAndroid Build Coastguard Worker NodeAddr<FuncNode*> FA = DFG.getFunc();
329*9880d681SAndroid Build Coastguard Worker auto Blocks = FA.Addr->members(DFG);
330*9880d681SAndroid Build Coastguard Worker for (NodeAddr<BlockNode*> BA : Blocks) {
331*9880d681SAndroid Build Coastguard Worker auto Ps = BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG);
332*9880d681SAndroid Build Coastguard Worker Phis.insert(Phis.end(), Ps.begin(), Ps.end());
333*9880d681SAndroid Build Coastguard Worker }
334*9880d681SAndroid Build Coastguard Worker
335*9880d681SAndroid Build Coastguard Worker // phi use -> (map: reaching phi -> set of registers defined in between)
336*9880d681SAndroid Build Coastguard Worker std::map<NodeId,std::map<NodeId,RegisterSet>> PhiUp;
337*9880d681SAndroid Build Coastguard Worker std::vector<NodeId> PhiUQ; // Work list of phis for upward propagation.
338*9880d681SAndroid Build Coastguard Worker
339*9880d681SAndroid Build Coastguard Worker // Go over all phis.
340*9880d681SAndroid Build Coastguard Worker for (NodeAddr<PhiNode*> PhiA : Phis) {
341*9880d681SAndroid Build Coastguard Worker // Go over all defs and collect the reached uses that are non-phi uses
342*9880d681SAndroid Build Coastguard Worker // (i.e. the "real uses").
343*9880d681SAndroid Build Coastguard Worker auto &RealUses = RealUseMap[PhiA.Id];
344*9880d681SAndroid Build Coastguard Worker auto PhiRefs = PhiA.Addr->members(DFG);
345*9880d681SAndroid Build Coastguard Worker
346*9880d681SAndroid Build Coastguard Worker // Have a work queue of defs whose reached uses need to be found.
347*9880d681SAndroid Build Coastguard Worker // For each def, add to the queue all reached (non-phi) defs.
348*9880d681SAndroid Build Coastguard Worker SetVector<NodeId> DefQ;
349*9880d681SAndroid Build Coastguard Worker NodeSet PhiDefs;
350*9880d681SAndroid Build Coastguard Worker for (auto R : PhiRefs) {
351*9880d681SAndroid Build Coastguard Worker if (!DFG.IsRef<NodeAttrs::Def>(R))
352*9880d681SAndroid Build Coastguard Worker continue;
353*9880d681SAndroid Build Coastguard Worker DefQ.insert(R.Id);
354*9880d681SAndroid Build Coastguard Worker PhiDefs.insert(R.Id);
355*9880d681SAndroid Build Coastguard Worker }
356*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i < DefQ.size(); ++i) {
357*9880d681SAndroid Build Coastguard Worker NodeAddr<DefNode*> DA = DFG.addr<DefNode*>(DefQ[i]);
358*9880d681SAndroid Build Coastguard Worker NodeId UN = DA.Addr->getReachedUse();
359*9880d681SAndroid Build Coastguard Worker while (UN != 0) {
360*9880d681SAndroid Build Coastguard Worker NodeAddr<UseNode*> A = DFG.addr<UseNode*>(UN);
361*9880d681SAndroid Build Coastguard Worker if (!(A.Addr->getFlags() & NodeAttrs::PhiRef))
362*9880d681SAndroid Build Coastguard Worker RealUses[getRestrictedRegRef(A)].insert(A.Id);
363*9880d681SAndroid Build Coastguard Worker UN = A.Addr->getSibling();
364*9880d681SAndroid Build Coastguard Worker }
365*9880d681SAndroid Build Coastguard Worker NodeId DN = DA.Addr->getReachedDef();
366*9880d681SAndroid Build Coastguard Worker while (DN != 0) {
367*9880d681SAndroid Build Coastguard Worker NodeAddr<DefNode*> A = DFG.addr<DefNode*>(DN);
368*9880d681SAndroid Build Coastguard Worker for (auto T : DFG.getRelatedRefs(A.Addr->getOwner(DFG), A)) {
369*9880d681SAndroid Build Coastguard Worker uint16_t Flags = NodeAddr<DefNode*>(T).Addr->getFlags();
370*9880d681SAndroid Build Coastguard Worker // Must traverse the reached-def chain. Consider:
371*9880d681SAndroid Build Coastguard Worker // def(D0) -> def(R0) -> def(R0) -> use(D0)
372*9880d681SAndroid Build Coastguard Worker // The reachable use of D0 passes through a def of R0.
373*9880d681SAndroid Build Coastguard Worker if (!(Flags & NodeAttrs::PhiRef))
374*9880d681SAndroid Build Coastguard Worker DefQ.insert(T.Id);
375*9880d681SAndroid Build Coastguard Worker }
376*9880d681SAndroid Build Coastguard Worker DN = A.Addr->getSibling();
377*9880d681SAndroid Build Coastguard Worker }
378*9880d681SAndroid Build Coastguard Worker }
379*9880d681SAndroid Build Coastguard Worker // Filter out these uses that appear to be reachable, but really
380*9880d681SAndroid Build Coastguard Worker // are not. For example:
381*9880d681SAndroid Build Coastguard Worker //
382*9880d681SAndroid Build Coastguard Worker // R1:0 = d1
383*9880d681SAndroid Build Coastguard Worker // = R1:0 u2 Reached by d1.
384*9880d681SAndroid Build Coastguard Worker // R0 = d3
385*9880d681SAndroid Build Coastguard Worker // = R1:0 u4 Still reached by d1: indirectly through
386*9880d681SAndroid Build Coastguard Worker // the def d3.
387*9880d681SAndroid Build Coastguard Worker // R1 = d5
388*9880d681SAndroid Build Coastguard Worker // = R1:0 u6 Not reached by d1 (covered collectively
389*9880d681SAndroid Build Coastguard Worker // by d3 and d5), but following reached
390*9880d681SAndroid Build Coastguard Worker // defs and uses from d1 will lead here.
391*9880d681SAndroid Build Coastguard Worker auto HasDef = [&PhiDefs] (NodeAddr<DefNode*> DA) -> bool {
392*9880d681SAndroid Build Coastguard Worker return PhiDefs.count(DA.Id);
393*9880d681SAndroid Build Coastguard Worker };
394*9880d681SAndroid Build Coastguard Worker for (auto UI = RealUses.begin(), UE = RealUses.end(); UI != UE; ) {
395*9880d681SAndroid Build Coastguard Worker // For each reached register UI->first, there is a set UI->second, of
396*9880d681SAndroid Build Coastguard Worker // uses of it. For each such use, check if it is reached by this phi,
397*9880d681SAndroid Build Coastguard Worker // i.e. check if the set of its reaching uses intersects the set of
398*9880d681SAndroid Build Coastguard Worker // this phi's defs.
399*9880d681SAndroid Build Coastguard Worker auto &Uses = UI->second;
400*9880d681SAndroid Build Coastguard Worker for (auto I = Uses.begin(), E = Uses.end(); I != E; ) {
401*9880d681SAndroid Build Coastguard Worker auto UA = DFG.addr<UseNode*>(*I);
402*9880d681SAndroid Build Coastguard Worker NodeList RDs = getAllReachingDefs(UI->first, UA);
403*9880d681SAndroid Build Coastguard Worker if (std::any_of(RDs.begin(), RDs.end(), HasDef))
404*9880d681SAndroid Build Coastguard Worker ++I;
405*9880d681SAndroid Build Coastguard Worker else
406*9880d681SAndroid Build Coastguard Worker I = Uses.erase(I);
407*9880d681SAndroid Build Coastguard Worker }
408*9880d681SAndroid Build Coastguard Worker if (Uses.empty())
409*9880d681SAndroid Build Coastguard Worker UI = RealUses.erase(UI);
410*9880d681SAndroid Build Coastguard Worker else
411*9880d681SAndroid Build Coastguard Worker ++UI;
412*9880d681SAndroid Build Coastguard Worker }
413*9880d681SAndroid Build Coastguard Worker
414*9880d681SAndroid Build Coastguard Worker // If this phi reaches some "real" uses, add it to the queue for upward
415*9880d681SAndroid Build Coastguard Worker // propagation.
416*9880d681SAndroid Build Coastguard Worker if (!RealUses.empty())
417*9880d681SAndroid Build Coastguard Worker PhiUQ.push_back(PhiA.Id);
418*9880d681SAndroid Build Coastguard Worker
419*9880d681SAndroid Build Coastguard Worker // Go over all phi uses and check if the reaching def is another phi.
420*9880d681SAndroid Build Coastguard Worker // Collect the phis that are among the reaching defs of these uses.
421*9880d681SAndroid Build Coastguard Worker // While traversing the list of reaching defs for each phi use, collect
422*9880d681SAndroid Build Coastguard Worker // the set of registers defined between this phi (Phi) and the owner phi
423*9880d681SAndroid Build Coastguard Worker // of the reaching def.
424*9880d681SAndroid Build Coastguard Worker for (auto I : PhiRefs) {
425*9880d681SAndroid Build Coastguard Worker if (!DFG.IsRef<NodeAttrs::Use>(I))
426*9880d681SAndroid Build Coastguard Worker continue;
427*9880d681SAndroid Build Coastguard Worker NodeAddr<UseNode*> UA = I;
428*9880d681SAndroid Build Coastguard Worker auto &UpMap = PhiUp[UA.Id];
429*9880d681SAndroid Build Coastguard Worker RegisterSet DefRRs;
430*9880d681SAndroid Build Coastguard Worker for (NodeAddr<DefNode*> DA : getAllReachingDefs(UA)) {
431*9880d681SAndroid Build Coastguard Worker if (DA.Addr->getFlags() & NodeAttrs::PhiRef)
432*9880d681SAndroid Build Coastguard Worker UpMap[DA.Addr->getOwner(DFG).Id] = DefRRs;
433*9880d681SAndroid Build Coastguard Worker else
434*9880d681SAndroid Build Coastguard Worker DefRRs.insert(DA.Addr->getRegRef());
435*9880d681SAndroid Build Coastguard Worker }
436*9880d681SAndroid Build Coastguard Worker }
437*9880d681SAndroid Build Coastguard Worker }
438*9880d681SAndroid Build Coastguard Worker
439*9880d681SAndroid Build Coastguard Worker if (Trace) {
440*9880d681SAndroid Build Coastguard Worker dbgs() << "Phi-up-to-phi map:\n";
441*9880d681SAndroid Build Coastguard Worker for (auto I : PhiUp) {
442*9880d681SAndroid Build Coastguard Worker dbgs() << "phi " << Print<NodeId>(I.first, DFG) << " -> {";
443*9880d681SAndroid Build Coastguard Worker for (auto R : I.second)
444*9880d681SAndroid Build Coastguard Worker dbgs() << ' ' << Print<NodeId>(R.first, DFG)
445*9880d681SAndroid Build Coastguard Worker << Print<RegisterSet>(R.second, DFG);
446*9880d681SAndroid Build Coastguard Worker dbgs() << " }\n";
447*9880d681SAndroid Build Coastguard Worker }
448*9880d681SAndroid Build Coastguard Worker }
449*9880d681SAndroid Build Coastguard Worker
450*9880d681SAndroid Build Coastguard Worker // Propagate the reached registers up in the phi chain.
451*9880d681SAndroid Build Coastguard Worker //
452*9880d681SAndroid Build Coastguard Worker // The following type of situation needs careful handling:
453*9880d681SAndroid Build Coastguard Worker //
454*9880d681SAndroid Build Coastguard Worker // phi d1<R1:0> (1)
455*9880d681SAndroid Build Coastguard Worker // |
456*9880d681SAndroid Build Coastguard Worker // ... d2<R1>
457*9880d681SAndroid Build Coastguard Worker // |
458*9880d681SAndroid Build Coastguard Worker // phi u3<R1:0> (2)
459*9880d681SAndroid Build Coastguard Worker // |
460*9880d681SAndroid Build Coastguard Worker // ... u4<R1>
461*9880d681SAndroid Build Coastguard Worker //
462*9880d681SAndroid Build Coastguard Worker // The phi node (2) defines a register pair R1:0, and reaches a "real"
463*9880d681SAndroid Build Coastguard Worker // use u4 of just R1. The same phi node is also known to reach (upwards)
464*9880d681SAndroid Build Coastguard Worker // the phi node (1). However, the use u4 is not reached by phi (1),
465*9880d681SAndroid Build Coastguard Worker // because of the intervening definition d2 of R1. The data flow between
466*9880d681SAndroid Build Coastguard Worker // phis (1) and (2) is restricted to R1:0 minus R1, i.e. R0.
467*9880d681SAndroid Build Coastguard Worker //
468*9880d681SAndroid Build Coastguard Worker // When propagating uses up the phi chains, get the all reaching defs
469*9880d681SAndroid Build Coastguard Worker // for a given phi use, and traverse the list until the propagated ref
470*9880d681SAndroid Build Coastguard Worker // is covered, or until or until reaching the final phi. Only assume
471*9880d681SAndroid Build Coastguard Worker // that the reference reaches the phi in the latter case.
472*9880d681SAndroid Build Coastguard Worker
473*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i < PhiUQ.size(); ++i) {
474*9880d681SAndroid Build Coastguard Worker auto PA = DFG.addr<PhiNode*>(PhiUQ[i]);
475*9880d681SAndroid Build Coastguard Worker auto &RealUses = RealUseMap[PA.Id];
476*9880d681SAndroid Build Coastguard Worker for (auto U : PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG)) {
477*9880d681SAndroid Build Coastguard Worker NodeAddr<UseNode*> UA = U;
478*9880d681SAndroid Build Coastguard Worker auto &UpPhis = PhiUp[UA.Id];
479*9880d681SAndroid Build Coastguard Worker for (auto UP : UpPhis) {
480*9880d681SAndroid Build Coastguard Worker bool Changed = false;
481*9880d681SAndroid Build Coastguard Worker auto &MidDefs = UP.second;
482*9880d681SAndroid Build Coastguard Worker // Collect the set UpReached of uses that are reached by the current
483*9880d681SAndroid Build Coastguard Worker // phi PA, and are not covered by any intervening def between PA and
484*9880d681SAndroid Build Coastguard Worker // the upward phi UP.
485*9880d681SAndroid Build Coastguard Worker RegisterSet UpReached;
486*9880d681SAndroid Build Coastguard Worker for (auto T : RealUses) {
487*9880d681SAndroid Build Coastguard Worker if (!isRestricted(PA, UA, T.first))
488*9880d681SAndroid Build Coastguard Worker continue;
489*9880d681SAndroid Build Coastguard Worker if (!RAI.covers(MidDefs, T.first))
490*9880d681SAndroid Build Coastguard Worker UpReached.insert(T.first);
491*9880d681SAndroid Build Coastguard Worker }
492*9880d681SAndroid Build Coastguard Worker if (UpReached.empty())
493*9880d681SAndroid Build Coastguard Worker continue;
494*9880d681SAndroid Build Coastguard Worker // Update the set PRUs of real uses reached by the upward phi UP with
495*9880d681SAndroid Build Coastguard Worker // the actual set of uses (UpReached) that the UP phi reaches.
496*9880d681SAndroid Build Coastguard Worker auto &PRUs = RealUseMap[UP.first];
497*9880d681SAndroid Build Coastguard Worker for (auto R : UpReached) {
498*9880d681SAndroid Build Coastguard Worker unsigned Z = PRUs[R].size();
499*9880d681SAndroid Build Coastguard Worker PRUs[R].insert(RealUses[R].begin(), RealUses[R].end());
500*9880d681SAndroid Build Coastguard Worker Changed |= (PRUs[R].size() != Z);
501*9880d681SAndroid Build Coastguard Worker }
502*9880d681SAndroid Build Coastguard Worker if (Changed)
503*9880d681SAndroid Build Coastguard Worker PhiUQ.push_back(UP.first);
504*9880d681SAndroid Build Coastguard Worker }
505*9880d681SAndroid Build Coastguard Worker }
506*9880d681SAndroid Build Coastguard Worker }
507*9880d681SAndroid Build Coastguard Worker
508*9880d681SAndroid Build Coastguard Worker if (Trace) {
509*9880d681SAndroid Build Coastguard Worker dbgs() << "Real use map:\n";
510*9880d681SAndroid Build Coastguard Worker for (auto I : RealUseMap) {
511*9880d681SAndroid Build Coastguard Worker dbgs() << "phi " << Print<NodeId>(I.first, DFG);
512*9880d681SAndroid Build Coastguard Worker NodeAddr<PhiNode*> PA = DFG.addr<PhiNode*>(I.first);
513*9880d681SAndroid Build Coastguard Worker NodeList Ds = PA.Addr->members_if(DFG.IsRef<NodeAttrs::Def>, DFG);
514*9880d681SAndroid Build Coastguard Worker if (!Ds.empty()) {
515*9880d681SAndroid Build Coastguard Worker RegisterRef RR = NodeAddr<DefNode*>(Ds[0]).Addr->getRegRef();
516*9880d681SAndroid Build Coastguard Worker dbgs() << '<' << Print<RegisterRef>(RR, DFG) << '>';
517*9880d681SAndroid Build Coastguard Worker } else {
518*9880d681SAndroid Build Coastguard Worker dbgs() << "<noreg>";
519*9880d681SAndroid Build Coastguard Worker }
520*9880d681SAndroid Build Coastguard Worker dbgs() << " -> " << Print<RefMap>(I.second, DFG) << '\n';
521*9880d681SAndroid Build Coastguard Worker }
522*9880d681SAndroid Build Coastguard Worker }
523*9880d681SAndroid Build Coastguard Worker }
524*9880d681SAndroid Build Coastguard Worker
525*9880d681SAndroid Build Coastguard Worker
computeLiveIns()526*9880d681SAndroid Build Coastguard Worker void Liveness::computeLiveIns() {
527*9880d681SAndroid Build Coastguard Worker // Populate the node-to-block map. This speeds up the calculations
528*9880d681SAndroid Build Coastguard Worker // significantly.
529*9880d681SAndroid Build Coastguard Worker NBMap.clear();
530*9880d681SAndroid Build Coastguard Worker for (NodeAddr<BlockNode*> BA : DFG.getFunc().Addr->members(DFG)) {
531*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *BB = BA.Addr->getCode();
532*9880d681SAndroid Build Coastguard Worker for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG)) {
533*9880d681SAndroid Build Coastguard Worker for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG))
534*9880d681SAndroid Build Coastguard Worker NBMap.insert(std::make_pair(RA.Id, BB));
535*9880d681SAndroid Build Coastguard Worker NBMap.insert(std::make_pair(IA.Id, BB));
536*9880d681SAndroid Build Coastguard Worker }
537*9880d681SAndroid Build Coastguard Worker }
538*9880d681SAndroid Build Coastguard Worker
539*9880d681SAndroid Build Coastguard Worker MachineFunction &MF = DFG.getMF();
540*9880d681SAndroid Build Coastguard Worker
541*9880d681SAndroid Build Coastguard Worker // Compute IDF first, then the inverse.
542*9880d681SAndroid Build Coastguard Worker decltype(IIDF) IDF;
543*9880d681SAndroid Build Coastguard Worker for (auto &B : MF) {
544*9880d681SAndroid Build Coastguard Worker auto F1 = MDF.find(&B);
545*9880d681SAndroid Build Coastguard Worker if (F1 == MDF.end())
546*9880d681SAndroid Build Coastguard Worker continue;
547*9880d681SAndroid Build Coastguard Worker SetVector<MachineBasicBlock*> IDFB(F1->second.begin(), F1->second.end());
548*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i < IDFB.size(); ++i) {
549*9880d681SAndroid Build Coastguard Worker auto F2 = MDF.find(IDFB[i]);
550*9880d681SAndroid Build Coastguard Worker if (F2 != MDF.end())
551*9880d681SAndroid Build Coastguard Worker IDFB.insert(F2->second.begin(), F2->second.end());
552*9880d681SAndroid Build Coastguard Worker }
553*9880d681SAndroid Build Coastguard Worker // Add B to the IDF(B). This will put B in the IIDF(B).
554*9880d681SAndroid Build Coastguard Worker IDFB.insert(&B);
555*9880d681SAndroid Build Coastguard Worker IDF[&B].insert(IDFB.begin(), IDFB.end());
556*9880d681SAndroid Build Coastguard Worker }
557*9880d681SAndroid Build Coastguard Worker
558*9880d681SAndroid Build Coastguard Worker for (auto I : IDF)
559*9880d681SAndroid Build Coastguard Worker for (auto S : I.second)
560*9880d681SAndroid Build Coastguard Worker IIDF[S].insert(I.first);
561*9880d681SAndroid Build Coastguard Worker
562*9880d681SAndroid Build Coastguard Worker computePhiInfo();
563*9880d681SAndroid Build Coastguard Worker
564*9880d681SAndroid Build Coastguard Worker NodeAddr<FuncNode*> FA = DFG.getFunc();
565*9880d681SAndroid Build Coastguard Worker auto Blocks = FA.Addr->members(DFG);
566*9880d681SAndroid Build Coastguard Worker
567*9880d681SAndroid Build Coastguard Worker // Build the phi live-on-entry map.
568*9880d681SAndroid Build Coastguard Worker for (NodeAddr<BlockNode*> BA : Blocks) {
569*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MB = BA.Addr->getCode();
570*9880d681SAndroid Build Coastguard Worker auto &LON = PhiLON[MB];
571*9880d681SAndroid Build Coastguard Worker for (auto P : BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG))
572*9880d681SAndroid Build Coastguard Worker for (auto S : RealUseMap[P.Id])
573*9880d681SAndroid Build Coastguard Worker LON[S.first].insert(S.second.begin(), S.second.end());
574*9880d681SAndroid Build Coastguard Worker }
575*9880d681SAndroid Build Coastguard Worker
576*9880d681SAndroid Build Coastguard Worker if (Trace) {
577*9880d681SAndroid Build Coastguard Worker dbgs() << "Phi live-on-entry map:\n";
578*9880d681SAndroid Build Coastguard Worker for (auto I : PhiLON)
579*9880d681SAndroid Build Coastguard Worker dbgs() << "block #" << I.first->getNumber() << " -> "
580*9880d681SAndroid Build Coastguard Worker << Print<RefMap>(I.second, DFG) << '\n';
581*9880d681SAndroid Build Coastguard Worker }
582*9880d681SAndroid Build Coastguard Worker
583*9880d681SAndroid Build Coastguard Worker // Build the phi live-on-exit map. Each phi node has some set of reached
584*9880d681SAndroid Build Coastguard Worker // "real" uses. Propagate this set backwards into the block predecessors
585*9880d681SAndroid Build Coastguard Worker // through the reaching defs of the corresponding phi uses.
586*9880d681SAndroid Build Coastguard Worker for (NodeAddr<BlockNode*> BA : Blocks) {
587*9880d681SAndroid Build Coastguard Worker auto Phis = BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG);
588*9880d681SAndroid Build Coastguard Worker for (NodeAddr<PhiNode*> PA : Phis) {
589*9880d681SAndroid Build Coastguard Worker auto &RUs = RealUseMap[PA.Id];
590*9880d681SAndroid Build Coastguard Worker if (RUs.empty())
591*9880d681SAndroid Build Coastguard Worker continue;
592*9880d681SAndroid Build Coastguard Worker
593*9880d681SAndroid Build Coastguard Worker for (auto U : PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG)) {
594*9880d681SAndroid Build Coastguard Worker NodeAddr<PhiUseNode*> UA = U;
595*9880d681SAndroid Build Coastguard Worker if (UA.Addr->getReachingDef() == 0)
596*9880d681SAndroid Build Coastguard Worker continue;
597*9880d681SAndroid Build Coastguard Worker
598*9880d681SAndroid Build Coastguard Worker // Mark all reached "real" uses of P as live on exit in the
599*9880d681SAndroid Build Coastguard Worker // predecessor.
600*9880d681SAndroid Build Coastguard Worker // Remap all the RUs so that they have a correct reaching def.
601*9880d681SAndroid Build Coastguard Worker auto PrA = DFG.addr<BlockNode*>(UA.Addr->getPredecessor());
602*9880d681SAndroid Build Coastguard Worker auto &LOX = PhiLOX[PrA.Addr->getCode()];
603*9880d681SAndroid Build Coastguard Worker for (auto R : RUs) {
604*9880d681SAndroid Build Coastguard Worker RegisterRef RR = R.first;
605*9880d681SAndroid Build Coastguard Worker if (!isRestricted(PA, UA, RR))
606*9880d681SAndroid Build Coastguard Worker RR = getRestrictedRegRef(UA);
607*9880d681SAndroid Build Coastguard Worker // The restricted ref may be different from the ref that was
608*9880d681SAndroid Build Coastguard Worker // accessed in the "real use". This means that this phi use
609*9880d681SAndroid Build Coastguard Worker // is not the one that carries this reference, so skip it.
610*9880d681SAndroid Build Coastguard Worker if (!RAI.alias(R.first, RR))
611*9880d681SAndroid Build Coastguard Worker continue;
612*9880d681SAndroid Build Coastguard Worker for (auto D : getAllReachingDefs(RR, UA))
613*9880d681SAndroid Build Coastguard Worker LOX[RR].insert(D.Id);
614*9880d681SAndroid Build Coastguard Worker }
615*9880d681SAndroid Build Coastguard Worker } // for U : phi uses
616*9880d681SAndroid Build Coastguard Worker } // for P : Phis
617*9880d681SAndroid Build Coastguard Worker } // for B : Blocks
618*9880d681SAndroid Build Coastguard Worker
619*9880d681SAndroid Build Coastguard Worker if (Trace) {
620*9880d681SAndroid Build Coastguard Worker dbgs() << "Phi live-on-exit map:\n";
621*9880d681SAndroid Build Coastguard Worker for (auto I : PhiLOX)
622*9880d681SAndroid Build Coastguard Worker dbgs() << "block #" << I.first->getNumber() << " -> "
623*9880d681SAndroid Build Coastguard Worker << Print<RefMap>(I.second, DFG) << '\n';
624*9880d681SAndroid Build Coastguard Worker }
625*9880d681SAndroid Build Coastguard Worker
626*9880d681SAndroid Build Coastguard Worker RefMap LiveIn;
627*9880d681SAndroid Build Coastguard Worker traverse(&MF.front(), LiveIn);
628*9880d681SAndroid Build Coastguard Worker
629*9880d681SAndroid Build Coastguard Worker // Add function live-ins to the live-in set of the function entry block.
630*9880d681SAndroid Build Coastguard Worker auto &EntryIn = LiveMap[&MF.front()];
631*9880d681SAndroid Build Coastguard Worker for (auto I = MRI.livein_begin(), E = MRI.livein_end(); I != E; ++I)
632*9880d681SAndroid Build Coastguard Worker EntryIn.insert({I->first,0});
633*9880d681SAndroid Build Coastguard Worker
634*9880d681SAndroid Build Coastguard Worker if (Trace) {
635*9880d681SAndroid Build Coastguard Worker // Dump the liveness map
636*9880d681SAndroid Build Coastguard Worker for (auto &B : MF) {
637*9880d681SAndroid Build Coastguard Worker BitVector LV(TRI.getNumRegs());
638*9880d681SAndroid Build Coastguard Worker for (auto I = B.livein_begin(), E = B.livein_end(); I != E; ++I)
639*9880d681SAndroid Build Coastguard Worker LV.set(I->PhysReg);
640*9880d681SAndroid Build Coastguard Worker dbgs() << "BB#" << B.getNumber() << "\t rec = {";
641*9880d681SAndroid Build Coastguard Worker for (int x = LV.find_first(); x >= 0; x = LV.find_next(x))
642*9880d681SAndroid Build Coastguard Worker dbgs() << ' ' << Print<RegisterRef>({unsigned(x),0}, DFG);
643*9880d681SAndroid Build Coastguard Worker dbgs() << " }\n";
644*9880d681SAndroid Build Coastguard Worker dbgs() << "\tcomp = " << Print<RegisterSet>(LiveMap[&B], DFG) << '\n';
645*9880d681SAndroid Build Coastguard Worker }
646*9880d681SAndroid Build Coastguard Worker }
647*9880d681SAndroid Build Coastguard Worker }
648*9880d681SAndroid Build Coastguard Worker
649*9880d681SAndroid Build Coastguard Worker
resetLiveIns()650*9880d681SAndroid Build Coastguard Worker void Liveness::resetLiveIns() {
651*9880d681SAndroid Build Coastguard Worker for (auto &B : DFG.getMF()) {
652*9880d681SAndroid Build Coastguard Worker // Remove all live-ins.
653*9880d681SAndroid Build Coastguard Worker std::vector<unsigned> T;
654*9880d681SAndroid Build Coastguard Worker for (auto I = B.livein_begin(), E = B.livein_end(); I != E; ++I)
655*9880d681SAndroid Build Coastguard Worker T.push_back(I->PhysReg);
656*9880d681SAndroid Build Coastguard Worker for (auto I : T)
657*9880d681SAndroid Build Coastguard Worker B.removeLiveIn(I);
658*9880d681SAndroid Build Coastguard Worker // Add the newly computed live-ins.
659*9880d681SAndroid Build Coastguard Worker auto &LiveIns = LiveMap[&B];
660*9880d681SAndroid Build Coastguard Worker for (auto I : LiveIns) {
661*9880d681SAndroid Build Coastguard Worker assert(I.Sub == 0);
662*9880d681SAndroid Build Coastguard Worker B.addLiveIn(I.Reg);
663*9880d681SAndroid Build Coastguard Worker }
664*9880d681SAndroid Build Coastguard Worker }
665*9880d681SAndroid Build Coastguard Worker }
666*9880d681SAndroid Build Coastguard Worker
667*9880d681SAndroid Build Coastguard Worker
resetKills()668*9880d681SAndroid Build Coastguard Worker void Liveness::resetKills() {
669*9880d681SAndroid Build Coastguard Worker for (auto &B : DFG.getMF())
670*9880d681SAndroid Build Coastguard Worker resetKills(&B);
671*9880d681SAndroid Build Coastguard Worker }
672*9880d681SAndroid Build Coastguard Worker
673*9880d681SAndroid Build Coastguard Worker
resetKills(MachineBasicBlock * B)674*9880d681SAndroid Build Coastguard Worker void Liveness::resetKills(MachineBasicBlock *B) {
675*9880d681SAndroid Build Coastguard Worker auto CopyLiveIns = [] (MachineBasicBlock *B, BitVector &LV) -> void {
676*9880d681SAndroid Build Coastguard Worker for (auto I = B->livein_begin(), E = B->livein_end(); I != E; ++I)
677*9880d681SAndroid Build Coastguard Worker LV.set(I->PhysReg);
678*9880d681SAndroid Build Coastguard Worker };
679*9880d681SAndroid Build Coastguard Worker
680*9880d681SAndroid Build Coastguard Worker BitVector LiveIn(TRI.getNumRegs()), Live(TRI.getNumRegs());
681*9880d681SAndroid Build Coastguard Worker CopyLiveIns(B, LiveIn);
682*9880d681SAndroid Build Coastguard Worker for (auto SI : B->successors())
683*9880d681SAndroid Build Coastguard Worker CopyLiveIns(SI, Live);
684*9880d681SAndroid Build Coastguard Worker
685*9880d681SAndroid Build Coastguard Worker for (auto I = B->rbegin(), E = B->rend(); I != E; ++I) {
686*9880d681SAndroid Build Coastguard Worker MachineInstr *MI = &*I;
687*9880d681SAndroid Build Coastguard Worker if (MI->isDebugValue())
688*9880d681SAndroid Build Coastguard Worker continue;
689*9880d681SAndroid Build Coastguard Worker
690*9880d681SAndroid Build Coastguard Worker MI->clearKillInfo();
691*9880d681SAndroid Build Coastguard Worker for (auto &Op : MI->operands()) {
692*9880d681SAndroid Build Coastguard Worker // An implicit def of a super-register may not necessarily start a
693*9880d681SAndroid Build Coastguard Worker // live range of it, since an implicit use could be used to keep parts
694*9880d681SAndroid Build Coastguard Worker // of it live. Instead of analyzing the implicit operands, ignore
695*9880d681SAndroid Build Coastguard Worker // implicit defs.
696*9880d681SAndroid Build Coastguard Worker if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
697*9880d681SAndroid Build Coastguard Worker continue;
698*9880d681SAndroid Build Coastguard Worker unsigned R = Op.getReg();
699*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isPhysicalRegister(R))
700*9880d681SAndroid Build Coastguard Worker continue;
701*9880d681SAndroid Build Coastguard Worker for (MCSubRegIterator SR(R, &TRI, true); SR.isValid(); ++SR)
702*9880d681SAndroid Build Coastguard Worker Live.reset(*SR);
703*9880d681SAndroid Build Coastguard Worker }
704*9880d681SAndroid Build Coastguard Worker for (auto &Op : MI->operands()) {
705*9880d681SAndroid Build Coastguard Worker if (!Op.isReg() || !Op.isUse())
706*9880d681SAndroid Build Coastguard Worker continue;
707*9880d681SAndroid Build Coastguard Worker unsigned R = Op.getReg();
708*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isPhysicalRegister(R))
709*9880d681SAndroid Build Coastguard Worker continue;
710*9880d681SAndroid Build Coastguard Worker bool IsLive = false;
711*9880d681SAndroid Build Coastguard Worker for (MCRegAliasIterator AR(R, &TRI, true); AR.isValid(); ++AR) {
712*9880d681SAndroid Build Coastguard Worker if (!Live[*AR])
713*9880d681SAndroid Build Coastguard Worker continue;
714*9880d681SAndroid Build Coastguard Worker IsLive = true;
715*9880d681SAndroid Build Coastguard Worker break;
716*9880d681SAndroid Build Coastguard Worker }
717*9880d681SAndroid Build Coastguard Worker if (IsLive)
718*9880d681SAndroid Build Coastguard Worker continue;
719*9880d681SAndroid Build Coastguard Worker Op.setIsKill(true);
720*9880d681SAndroid Build Coastguard Worker for (MCSubRegIterator SR(R, &TRI, true); SR.isValid(); ++SR)
721*9880d681SAndroid Build Coastguard Worker Live.set(*SR);
722*9880d681SAndroid Build Coastguard Worker }
723*9880d681SAndroid Build Coastguard Worker }
724*9880d681SAndroid Build Coastguard Worker }
725*9880d681SAndroid Build Coastguard Worker
726*9880d681SAndroid Build Coastguard Worker
727*9880d681SAndroid Build Coastguard Worker // For shadows, determine if RR is aliased to a reaching def of any other
728*9880d681SAndroid Build Coastguard Worker // shadow associated with RA. If it is not, then RR is "restricted" to RA,
729*9880d681SAndroid Build Coastguard Worker // and so it can be considered a value specific to RA. This is important
730*9880d681SAndroid Build Coastguard Worker // for accurately determining values associated with phi uses.
731*9880d681SAndroid Build Coastguard Worker // For non-shadows, this function returns "true".
isRestricted(NodeAddr<InstrNode * > IA,NodeAddr<RefNode * > RA,RegisterRef RR) const732*9880d681SAndroid Build Coastguard Worker bool Liveness::isRestricted(NodeAddr<InstrNode*> IA, NodeAddr<RefNode*> RA,
733*9880d681SAndroid Build Coastguard Worker RegisterRef RR) const {
734*9880d681SAndroid Build Coastguard Worker NodeId Start = RA.Id;
735*9880d681SAndroid Build Coastguard Worker for (NodeAddr<RefNode*> TA = DFG.getNextShadow(IA, RA);
736*9880d681SAndroid Build Coastguard Worker TA.Id != 0 && TA.Id != Start; TA = DFG.getNextShadow(IA, TA)) {
737*9880d681SAndroid Build Coastguard Worker NodeId RD = TA.Addr->getReachingDef();
738*9880d681SAndroid Build Coastguard Worker if (RD == 0)
739*9880d681SAndroid Build Coastguard Worker continue;
740*9880d681SAndroid Build Coastguard Worker if (RAI.alias(RR, DFG.addr<DefNode*>(RD).Addr->getRegRef()))
741*9880d681SAndroid Build Coastguard Worker return false;
742*9880d681SAndroid Build Coastguard Worker }
743*9880d681SAndroid Build Coastguard Worker return true;
744*9880d681SAndroid Build Coastguard Worker }
745*9880d681SAndroid Build Coastguard Worker
746*9880d681SAndroid Build Coastguard Worker
getRestrictedRegRef(NodeAddr<RefNode * > RA) const747*9880d681SAndroid Build Coastguard Worker RegisterRef Liveness::getRestrictedRegRef(NodeAddr<RefNode*> RA) const {
748*9880d681SAndroid Build Coastguard Worker assert(DFG.IsRef<NodeAttrs::Use>(RA));
749*9880d681SAndroid Build Coastguard Worker if (RA.Addr->getFlags() & NodeAttrs::Shadow) {
750*9880d681SAndroid Build Coastguard Worker NodeId RD = RA.Addr->getReachingDef();
751*9880d681SAndroid Build Coastguard Worker assert(RD);
752*9880d681SAndroid Build Coastguard Worker RA = DFG.addr<DefNode*>(RD);
753*9880d681SAndroid Build Coastguard Worker }
754*9880d681SAndroid Build Coastguard Worker return RA.Addr->getRegRef();
755*9880d681SAndroid Build Coastguard Worker }
756*9880d681SAndroid Build Coastguard Worker
757*9880d681SAndroid Build Coastguard Worker
getPhysReg(RegisterRef RR) const758*9880d681SAndroid Build Coastguard Worker unsigned Liveness::getPhysReg(RegisterRef RR) const {
759*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isPhysicalRegister(RR.Reg))
760*9880d681SAndroid Build Coastguard Worker return 0;
761*9880d681SAndroid Build Coastguard Worker return RR.Sub ? TRI.getSubReg(RR.Reg, RR.Sub) : RR.Reg;
762*9880d681SAndroid Build Coastguard Worker }
763*9880d681SAndroid Build Coastguard Worker
764*9880d681SAndroid Build Coastguard Worker
765*9880d681SAndroid Build Coastguard Worker // Helper function to obtain the basic block containing the reaching def
766*9880d681SAndroid Build Coastguard Worker // of the given use.
getBlockWithRef(NodeId RN) const767*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *Liveness::getBlockWithRef(NodeId RN) const {
768*9880d681SAndroid Build Coastguard Worker auto F = NBMap.find(RN);
769*9880d681SAndroid Build Coastguard Worker if (F != NBMap.end())
770*9880d681SAndroid Build Coastguard Worker return F->second;
771*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Node id not in map");
772*9880d681SAndroid Build Coastguard Worker }
773*9880d681SAndroid Build Coastguard Worker
774*9880d681SAndroid Build Coastguard Worker
traverse(MachineBasicBlock * B,RefMap & LiveIn)775*9880d681SAndroid Build Coastguard Worker void Liveness::traverse(MachineBasicBlock *B, RefMap &LiveIn) {
776*9880d681SAndroid Build Coastguard Worker // The LiveIn map, for each (physical) register, contains the set of live
777*9880d681SAndroid Build Coastguard Worker // reaching defs of that register that are live on entry to the associated
778*9880d681SAndroid Build Coastguard Worker // block.
779*9880d681SAndroid Build Coastguard Worker
780*9880d681SAndroid Build Coastguard Worker // The summary of the traversal algorithm:
781*9880d681SAndroid Build Coastguard Worker //
782*9880d681SAndroid Build Coastguard Worker // R is live-in in B, if there exists a U(R), such that rdef(R) dom B
783*9880d681SAndroid Build Coastguard Worker // and (U \in IDF(B) or B dom U).
784*9880d681SAndroid Build Coastguard Worker //
785*9880d681SAndroid Build Coastguard Worker // for (C : children) {
786*9880d681SAndroid Build Coastguard Worker // LU = {}
787*9880d681SAndroid Build Coastguard Worker // traverse(C, LU)
788*9880d681SAndroid Build Coastguard Worker // LiveUses += LU
789*9880d681SAndroid Build Coastguard Worker // }
790*9880d681SAndroid Build Coastguard Worker //
791*9880d681SAndroid Build Coastguard Worker // LiveUses -= Defs(B);
792*9880d681SAndroid Build Coastguard Worker // LiveUses += UpwardExposedUses(B);
793*9880d681SAndroid Build Coastguard Worker // for (C : IIDF[B])
794*9880d681SAndroid Build Coastguard Worker // for (U : LiveUses)
795*9880d681SAndroid Build Coastguard Worker // if (Rdef(U) dom C)
796*9880d681SAndroid Build Coastguard Worker // C.addLiveIn(U)
797*9880d681SAndroid Build Coastguard Worker //
798*9880d681SAndroid Build Coastguard Worker
799*9880d681SAndroid Build Coastguard Worker // Go up the dominator tree (depth-first).
800*9880d681SAndroid Build Coastguard Worker MachineDomTreeNode *N = MDT.getNode(B);
801*9880d681SAndroid Build Coastguard Worker for (auto I : *N) {
802*9880d681SAndroid Build Coastguard Worker RefMap L;
803*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *SB = I->getBlock();
804*9880d681SAndroid Build Coastguard Worker traverse(SB, L);
805*9880d681SAndroid Build Coastguard Worker
806*9880d681SAndroid Build Coastguard Worker for (auto S : L)
807*9880d681SAndroid Build Coastguard Worker LiveIn[S.first].insert(S.second.begin(), S.second.end());
808*9880d681SAndroid Build Coastguard Worker }
809*9880d681SAndroid Build Coastguard Worker
810*9880d681SAndroid Build Coastguard Worker if (Trace) {
811*9880d681SAndroid Build Coastguard Worker dbgs() << LLVM_FUNCTION_NAME << " in BB#" << B->getNumber()
812*9880d681SAndroid Build Coastguard Worker << " after recursion into";
813*9880d681SAndroid Build Coastguard Worker for (auto I : *N)
814*9880d681SAndroid Build Coastguard Worker dbgs() << ' ' << I->getBlock()->getNumber();
815*9880d681SAndroid Build Coastguard Worker dbgs() << "\n LiveIn: " << Print<RefMap>(LiveIn, DFG);
816*9880d681SAndroid Build Coastguard Worker dbgs() << "\n Local: " << Print<RegisterSet>(LiveMap[B], DFG) << '\n';
817*9880d681SAndroid Build Coastguard Worker }
818*9880d681SAndroid Build Coastguard Worker
819*9880d681SAndroid Build Coastguard Worker // Add phi uses that are live on exit from this block.
820*9880d681SAndroid Build Coastguard Worker RefMap &PUs = PhiLOX[B];
821*9880d681SAndroid Build Coastguard Worker for (auto S : PUs)
822*9880d681SAndroid Build Coastguard Worker LiveIn[S.first].insert(S.second.begin(), S.second.end());
823*9880d681SAndroid Build Coastguard Worker
824*9880d681SAndroid Build Coastguard Worker if (Trace) {
825*9880d681SAndroid Build Coastguard Worker dbgs() << "after LOX\n";
826*9880d681SAndroid Build Coastguard Worker dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
827*9880d681SAndroid Build Coastguard Worker dbgs() << " Local: " << Print<RegisterSet>(LiveMap[B], DFG) << '\n';
828*9880d681SAndroid Build Coastguard Worker }
829*9880d681SAndroid Build Coastguard Worker
830*9880d681SAndroid Build Coastguard Worker // Stop tracking all uses defined in this block: erase those records
831*9880d681SAndroid Build Coastguard Worker // where the reaching def is located in B and which cover all reached
832*9880d681SAndroid Build Coastguard Worker // uses.
833*9880d681SAndroid Build Coastguard Worker auto Copy = LiveIn;
834*9880d681SAndroid Build Coastguard Worker LiveIn.clear();
835*9880d681SAndroid Build Coastguard Worker
836*9880d681SAndroid Build Coastguard Worker for (auto I : Copy) {
837*9880d681SAndroid Build Coastguard Worker auto &Defs = LiveIn[I.first];
838*9880d681SAndroid Build Coastguard Worker NodeSet Rest;
839*9880d681SAndroid Build Coastguard Worker for (auto R : I.second) {
840*9880d681SAndroid Build Coastguard Worker auto DA = DFG.addr<DefNode*>(R);
841*9880d681SAndroid Build Coastguard Worker RegisterRef DDR = DA.Addr->getRegRef();
842*9880d681SAndroid Build Coastguard Worker NodeAddr<InstrNode*> IA = DA.Addr->getOwner(DFG);
843*9880d681SAndroid Build Coastguard Worker NodeAddr<BlockNode*> BA = IA.Addr->getOwner(DFG);
844*9880d681SAndroid Build Coastguard Worker // Defs from a different block need to be preserved. Defs from this
845*9880d681SAndroid Build Coastguard Worker // block will need to be processed further, except for phi defs, the
846*9880d681SAndroid Build Coastguard Worker // liveness of which is handled through the PhiLON/PhiLOX maps.
847*9880d681SAndroid Build Coastguard Worker if (B != BA.Addr->getCode())
848*9880d681SAndroid Build Coastguard Worker Defs.insert(R);
849*9880d681SAndroid Build Coastguard Worker else {
850*9880d681SAndroid Build Coastguard Worker bool IsPreserving = DA.Addr->getFlags() & NodeAttrs::Preserving;
851*9880d681SAndroid Build Coastguard Worker if (IA.Addr->getKind() != NodeAttrs::Phi && !IsPreserving) {
852*9880d681SAndroid Build Coastguard Worker bool Covering = RAI.covers(DDR, I.first);
853*9880d681SAndroid Build Coastguard Worker NodeId U = DA.Addr->getReachedUse();
854*9880d681SAndroid Build Coastguard Worker while (U && Covering) {
855*9880d681SAndroid Build Coastguard Worker auto DUA = DFG.addr<UseNode*>(U);
856*9880d681SAndroid Build Coastguard Worker RegisterRef Q = DUA.Addr->getRegRef();
857*9880d681SAndroid Build Coastguard Worker Covering = RAI.covers(DA.Addr->getRegRef(), Q);
858*9880d681SAndroid Build Coastguard Worker U = DUA.Addr->getSibling();
859*9880d681SAndroid Build Coastguard Worker }
860*9880d681SAndroid Build Coastguard Worker if (!Covering)
861*9880d681SAndroid Build Coastguard Worker Rest.insert(R);
862*9880d681SAndroid Build Coastguard Worker }
863*9880d681SAndroid Build Coastguard Worker }
864*9880d681SAndroid Build Coastguard Worker }
865*9880d681SAndroid Build Coastguard Worker
866*9880d681SAndroid Build Coastguard Worker // Non-covering defs from B.
867*9880d681SAndroid Build Coastguard Worker for (auto R : Rest) {
868*9880d681SAndroid Build Coastguard Worker auto DA = DFG.addr<DefNode*>(R);
869*9880d681SAndroid Build Coastguard Worker RegisterRef DRR = DA.Addr->getRegRef();
870*9880d681SAndroid Build Coastguard Worker RegisterSet RRs;
871*9880d681SAndroid Build Coastguard Worker for (NodeAddr<DefNode*> TA : getAllReachingDefs(DA)) {
872*9880d681SAndroid Build Coastguard Worker NodeAddr<InstrNode*> IA = TA.Addr->getOwner(DFG);
873*9880d681SAndroid Build Coastguard Worker NodeAddr<BlockNode*> BA = IA.Addr->getOwner(DFG);
874*9880d681SAndroid Build Coastguard Worker // Preserving defs do not count towards covering.
875*9880d681SAndroid Build Coastguard Worker if (!(TA.Addr->getFlags() & NodeAttrs::Preserving))
876*9880d681SAndroid Build Coastguard Worker RRs.insert(TA.Addr->getRegRef());
877*9880d681SAndroid Build Coastguard Worker if (BA.Addr->getCode() == B)
878*9880d681SAndroid Build Coastguard Worker continue;
879*9880d681SAndroid Build Coastguard Worker if (RAI.covers(RRs, DRR))
880*9880d681SAndroid Build Coastguard Worker break;
881*9880d681SAndroid Build Coastguard Worker Defs.insert(TA.Id);
882*9880d681SAndroid Build Coastguard Worker }
883*9880d681SAndroid Build Coastguard Worker }
884*9880d681SAndroid Build Coastguard Worker }
885*9880d681SAndroid Build Coastguard Worker
886*9880d681SAndroid Build Coastguard Worker emptify(LiveIn);
887*9880d681SAndroid Build Coastguard Worker
888*9880d681SAndroid Build Coastguard Worker if (Trace) {
889*9880d681SAndroid Build Coastguard Worker dbgs() << "after defs in block\n";
890*9880d681SAndroid Build Coastguard Worker dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
891*9880d681SAndroid Build Coastguard Worker dbgs() << " Local: " << Print<RegisterSet>(LiveMap[B], DFG) << '\n';
892*9880d681SAndroid Build Coastguard Worker }
893*9880d681SAndroid Build Coastguard Worker
894*9880d681SAndroid Build Coastguard Worker // Scan the block for upward-exposed uses and add them to the tracking set.
895*9880d681SAndroid Build Coastguard Worker for (auto I : DFG.getFunc().Addr->findBlock(B, DFG).Addr->members(DFG)) {
896*9880d681SAndroid Build Coastguard Worker NodeAddr<InstrNode*> IA = I;
897*9880d681SAndroid Build Coastguard Worker if (IA.Addr->getKind() != NodeAttrs::Stmt)
898*9880d681SAndroid Build Coastguard Worker continue;
899*9880d681SAndroid Build Coastguard Worker for (NodeAddr<UseNode*> UA : IA.Addr->members_if(DFG.IsUse, DFG)) {
900*9880d681SAndroid Build Coastguard Worker RegisterRef RR = UA.Addr->getRegRef();
901*9880d681SAndroid Build Coastguard Worker for (auto D : getAllReachingDefs(UA))
902*9880d681SAndroid Build Coastguard Worker if (getBlockWithRef(D.Id) != B)
903*9880d681SAndroid Build Coastguard Worker LiveIn[RR].insert(D.Id);
904*9880d681SAndroid Build Coastguard Worker }
905*9880d681SAndroid Build Coastguard Worker }
906*9880d681SAndroid Build Coastguard Worker
907*9880d681SAndroid Build Coastguard Worker if (Trace) {
908*9880d681SAndroid Build Coastguard Worker dbgs() << "after uses in block\n";
909*9880d681SAndroid Build Coastguard Worker dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
910*9880d681SAndroid Build Coastguard Worker dbgs() << " Local: " << Print<RegisterSet>(LiveMap[B], DFG) << '\n';
911*9880d681SAndroid Build Coastguard Worker }
912*9880d681SAndroid Build Coastguard Worker
913*9880d681SAndroid Build Coastguard Worker // Phi uses should not be propagated up the dominator tree, since they
914*9880d681SAndroid Build Coastguard Worker // are not dominated by their corresponding reaching defs.
915*9880d681SAndroid Build Coastguard Worker auto &Local = LiveMap[B];
916*9880d681SAndroid Build Coastguard Worker auto &LON = PhiLON[B];
917*9880d681SAndroid Build Coastguard Worker for (auto R : LON)
918*9880d681SAndroid Build Coastguard Worker Local.insert(R.first);
919*9880d681SAndroid Build Coastguard Worker
920*9880d681SAndroid Build Coastguard Worker if (Trace) {
921*9880d681SAndroid Build Coastguard Worker dbgs() << "after phi uses in block\n";
922*9880d681SAndroid Build Coastguard Worker dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
923*9880d681SAndroid Build Coastguard Worker dbgs() << " Local: " << Print<RegisterSet>(Local, DFG) << '\n';
924*9880d681SAndroid Build Coastguard Worker }
925*9880d681SAndroid Build Coastguard Worker
926*9880d681SAndroid Build Coastguard Worker for (auto C : IIDF[B]) {
927*9880d681SAndroid Build Coastguard Worker auto &LiveC = LiveMap[C];
928*9880d681SAndroid Build Coastguard Worker for (auto S : LiveIn)
929*9880d681SAndroid Build Coastguard Worker for (auto R : S.second)
930*9880d681SAndroid Build Coastguard Worker if (MDT.properlyDominates(getBlockWithRef(R), C))
931*9880d681SAndroid Build Coastguard Worker LiveC.insert(S.first);
932*9880d681SAndroid Build Coastguard Worker }
933*9880d681SAndroid Build Coastguard Worker }
934*9880d681SAndroid Build Coastguard Worker
935*9880d681SAndroid Build Coastguard Worker
emptify(RefMap & M)936*9880d681SAndroid Build Coastguard Worker void Liveness::emptify(RefMap &M) {
937*9880d681SAndroid Build Coastguard Worker for (auto I = M.begin(), E = M.end(); I != E; )
938*9880d681SAndroid Build Coastguard Worker I = I->second.empty() ? M.erase(I) : std::next(I);
939*9880d681SAndroid Build Coastguard Worker }
940*9880d681SAndroid Build Coastguard Worker
941