1080dd10fSScott Constable //===- RDFLiveness.cpp ----------------------------------------------------===//
2080dd10fSScott Constable //
3080dd10fSScott Constable // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4080dd10fSScott Constable // See https://llvm.org/LICENSE.txt for license information.
5080dd10fSScott Constable // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6080dd10fSScott Constable //
7080dd10fSScott Constable //===----------------------------------------------------------------------===//
8080dd10fSScott Constable //
9080dd10fSScott Constable // Computation of the liveness information from the data-flow graph.
10080dd10fSScott Constable //
11080dd10fSScott Constable // The main functionality of this code is to compute block live-in
12080dd10fSScott Constable // information. With the live-in information in place, the placement
13080dd10fSScott Constable // of kill flags can also be recalculated.
14080dd10fSScott Constable //
15080dd10fSScott Constable // The block live-in calculation is based on the ideas from the following
16080dd10fSScott Constable // publication:
17080dd10fSScott Constable //
18080dd10fSScott Constable // Dibyendu Das, Ramakrishna Upadrasta, Benoit Dupont de Dinechin.
19080dd10fSScott Constable // "Efficient Liveness Computation Using Merge Sets and DJ-Graphs."
20080dd10fSScott Constable // ACM Transactions on Architecture and Code Optimization, Association for
21080dd10fSScott Constable // Computing Machinery, 2012, ACM TACO Special Issue on "High-Performance
22080dd10fSScott Constable // and Embedded Architectures and Compilers", 8 (4),
23080dd10fSScott Constable // <10.1145/2086696.2086706>. <hal-00647369>
24080dd10fSScott Constable //
25989f1c72Sserge-sans-paille #include "llvm/CodeGen/RDFLiveness.h"
26080dd10fSScott Constable #include "llvm/ADT/BitVector.h"
2747fe1b63SKrzysztof Parzyszek #include "llvm/ADT/DenseMap.h"
28080dd10fSScott Constable #include "llvm/ADT/STLExtras.h"
29080dd10fSScott Constable #include "llvm/ADT/SetVector.h"
3047fe1b63SKrzysztof Parzyszek #include "llvm/ADT/SmallSet.h"
31080dd10fSScott Constable #include "llvm/CodeGen/MachineBasicBlock.h"
32080dd10fSScott Constable #include "llvm/CodeGen/MachineDominanceFrontier.h"
33080dd10fSScott Constable #include "llvm/CodeGen/MachineDominators.h"
34080dd10fSScott Constable #include "llvm/CodeGen/MachineFunction.h"
35080dd10fSScott Constable #include "llvm/CodeGen/MachineInstr.h"
36080dd10fSScott Constable #include "llvm/CodeGen/RDFGraph.h"
37080dd10fSScott Constable #include "llvm/CodeGen/RDFRegisters.h"
38080dd10fSScott Constable #include "llvm/CodeGen/TargetRegisterInfo.h"
39080dd10fSScott Constable #include "llvm/MC/LaneBitmask.h"
40080dd10fSScott Constable #include "llvm/MC/MCRegisterInfo.h"
41080dd10fSScott Constable #include "llvm/Support/CommandLine.h"
42080dd10fSScott Constable #include "llvm/Support/ErrorHandling.h"
43080dd10fSScott Constable #include "llvm/Support/raw_ostream.h"
44080dd10fSScott Constable #include <algorithm>
45080dd10fSScott Constable #include <cassert>
46080dd10fSScott Constable #include <cstdint>
47080dd10fSScott Constable #include <iterator>
48080dd10fSScott Constable #include <map>
4995217045SKrzysztof Parzyszek #include <unordered_map>
50080dd10fSScott Constable #include <utility>
51080dd10fSScott Constable #include <vector>
52080dd10fSScott Constable
53080dd10fSScott Constable using namespace llvm;
54080dd10fSScott Constable using namespace rdf;
55080dd10fSScott Constable
56080dd10fSScott Constable static cl::opt<unsigned> MaxRecNest("rdf-liveness-max-rec", cl::init(25),
57080dd10fSScott Constable cl::Hidden, cl::desc("Maximum recursion level"));
58080dd10fSScott Constable
59080dd10fSScott Constable namespace llvm {
60080dd10fSScott Constable namespace rdf {
61080dd10fSScott Constable
operator <<(raw_ostream & OS,const Print<Liveness::RefMap> & P)62080dd10fSScott Constable raw_ostream &operator<< (raw_ostream &OS, const Print<Liveness::RefMap> &P) {
63080dd10fSScott Constable OS << '{';
64*9e6d1f4bSKazu Hirata for (const auto &I : P.Obj) {
65080dd10fSScott Constable OS << ' ' << printReg(I.first, &P.G.getTRI()) << '{';
66080dd10fSScott Constable for (auto J = I.second.begin(), E = I.second.end(); J != E; ) {
67080dd10fSScott Constable OS << Print<NodeId>(J->first, P.G) << PrintLaneMaskOpt(J->second);
68080dd10fSScott Constable if (++J != E)
69080dd10fSScott Constable OS << ',';
70080dd10fSScott Constable }
71080dd10fSScott Constable OS << '}';
72080dd10fSScott Constable }
73080dd10fSScott Constable OS << " }";
74080dd10fSScott Constable return OS;
75080dd10fSScott Constable }
76080dd10fSScott Constable
77080dd10fSScott Constable } // end namespace rdf
78080dd10fSScott Constable } // end namespace llvm
79080dd10fSScott Constable
80080dd10fSScott Constable // The order in the returned sequence is the order of reaching defs in the
81080dd10fSScott Constable // upward traversal: the first def is the closest to the given reference RefA,
82080dd10fSScott Constable // the next one is further up, and so on.
83080dd10fSScott Constable // The list ends at a reaching phi def, or when the reference from RefA is
84080dd10fSScott Constable // covered by the defs in the list (see FullChain).
85080dd10fSScott Constable // This function provides two modes of operation:
86080dd10fSScott Constable // (1) Returning the sequence of reaching defs for a particular reference
87080dd10fSScott Constable // node. This sequence will terminate at the first phi node [1].
88080dd10fSScott Constable // (2) Returning a partial sequence of reaching defs, where the final goal
89080dd10fSScott Constable // is to traverse past phi nodes to the actual defs arising from the code
90080dd10fSScott Constable // itself.
91080dd10fSScott Constable // In mode (2), the register reference for which the search was started
92080dd10fSScott Constable // may be different from the reference node RefA, for which this call was
93080dd10fSScott Constable // made, hence the argument RefRR, which holds the original register.
94080dd10fSScott Constable // Also, some definitions may have already been encountered in a previous
95080dd10fSScott Constable // call that will influence register covering. The register references
96080dd10fSScott Constable // already defined are passed in through DefRRs.
97080dd10fSScott Constable // In mode (1), the "continuation" considerations do not apply, and the
98080dd10fSScott Constable // RefRR is the same as the register in RefA, and the set DefRRs is empty.
99080dd10fSScott Constable //
100080dd10fSScott Constable // [1] It is possible for multiple phi nodes to be included in the returned
101080dd10fSScott Constable // sequence:
102080dd10fSScott Constable // SubA = phi ...
103080dd10fSScott Constable // SubB = phi ...
104080dd10fSScott Constable // ... = SuperAB(rdef:SubA), SuperAB"(rdef:SubB)
105080dd10fSScott Constable // However, these phi nodes are independent from one another in terms of
106080dd10fSScott Constable // the data-flow.
107080dd10fSScott Constable
getAllReachingDefs(RegisterRef RefRR,NodeAddr<RefNode * > RefA,bool TopShadows,bool FullChain,const RegisterAggr & DefRRs)108080dd10fSScott Constable NodeList Liveness::getAllReachingDefs(RegisterRef RefRR,
109080dd10fSScott Constable NodeAddr<RefNode*> RefA, bool TopShadows, bool FullChain,
110080dd10fSScott Constable const RegisterAggr &DefRRs) {
111080dd10fSScott Constable NodeList RDefs; // Return value.
112080dd10fSScott Constable SetVector<NodeId> DefQ;
11347fe1b63SKrzysztof Parzyszek DenseMap<MachineInstr*, uint32_t> OrdMap;
114080dd10fSScott Constable
115080dd10fSScott Constable // Dead defs will be treated as if they were live, since they are actually
116080dd10fSScott Constable // on the data-flow path. They cannot be ignored because even though they
117080dd10fSScott Constable // do not generate meaningful values, they still modify registers.
118080dd10fSScott Constable
119080dd10fSScott Constable // If the reference is undefined, there is nothing to do.
120080dd10fSScott Constable if (RefA.Addr->getFlags() & NodeAttrs::Undef)
121080dd10fSScott Constable return RDefs;
122080dd10fSScott Constable
123080dd10fSScott Constable // The initial queue should not have reaching defs for shadows. The
124080dd10fSScott Constable // whole point of a shadow is that it will have a reaching def that
125080dd10fSScott Constable // is not aliased to the reaching defs of the related shadows.
126080dd10fSScott Constable NodeId Start = RefA.Id;
127080dd10fSScott Constable auto SNA = DFG.addr<RefNode*>(Start);
128080dd10fSScott Constable if (NodeId RD = SNA.Addr->getReachingDef())
129080dd10fSScott Constable DefQ.insert(RD);
130080dd10fSScott Constable if (TopShadows) {
131080dd10fSScott Constable for (auto S : DFG.getRelatedRefs(RefA.Addr->getOwner(DFG), RefA))
132080dd10fSScott Constable if (NodeId RD = NodeAddr<RefNode*>(S).Addr->getReachingDef())
133080dd10fSScott Constable DefQ.insert(RD);
134080dd10fSScott Constable }
135080dd10fSScott Constable
136080dd10fSScott Constable // Collect all the reaching defs, going up until a phi node is encountered,
137080dd10fSScott Constable // or there are no more reaching defs. From this set, the actual set of
138080dd10fSScott Constable // reaching defs will be selected.
139080dd10fSScott Constable // The traversal upwards must go on until a covering def is encountered.
140080dd10fSScott Constable // It is possible that a collection of non-covering (individually) defs
141080dd10fSScott Constable // will be sufficient, but keep going until a covering one is found.
142080dd10fSScott Constable for (unsigned i = 0; i < DefQ.size(); ++i) {
143080dd10fSScott Constable auto TA = DFG.addr<DefNode*>(DefQ[i]);
144080dd10fSScott Constable if (TA.Addr->getFlags() & NodeAttrs::PhiRef)
145080dd10fSScott Constable continue;
146080dd10fSScott Constable // Stop at the covering/overwriting def of the initial register reference.
147080dd10fSScott Constable RegisterRef RR = TA.Addr->getRegRef(DFG);
148080dd10fSScott Constable if (!DFG.IsPreservingDef(TA))
149080dd10fSScott Constable if (RegisterAggr::isCoverOf(RR, RefRR, PRI))
150080dd10fSScott Constable continue;
151080dd10fSScott Constable // Get the next level of reaching defs. This will include multiple
152080dd10fSScott Constable // reaching defs for shadows.
153080dd10fSScott Constable for (auto S : DFG.getRelatedRefs(TA.Addr->getOwner(DFG), TA))
154080dd10fSScott Constable if (NodeId RD = NodeAddr<RefNode*>(S).Addr->getReachingDef())
155080dd10fSScott Constable DefQ.insert(RD);
15647fe1b63SKrzysztof Parzyszek // Don't visit sibling defs. They share the same reaching def (which
15747fe1b63SKrzysztof Parzyszek // will be visited anyway), but they define something not aliased to
15847fe1b63SKrzysztof Parzyszek // this ref.
159080dd10fSScott Constable }
160080dd10fSScott Constable
161080dd10fSScott Constable // Return the MachineBasicBlock containing a given instruction.
162080dd10fSScott Constable auto Block = [this] (NodeAddr<InstrNode*> IA) -> MachineBasicBlock* {
163080dd10fSScott Constable if (IA.Addr->getKind() == NodeAttrs::Stmt)
164080dd10fSScott Constable return NodeAddr<StmtNode*>(IA).Addr->getCode()->getParent();
165080dd10fSScott Constable assert(IA.Addr->getKind() == NodeAttrs::Phi);
166080dd10fSScott Constable NodeAddr<PhiNode*> PA = IA;
167080dd10fSScott Constable NodeAddr<BlockNode*> BA = PA.Addr->getOwner(DFG);
168080dd10fSScott Constable return BA.Addr->getCode();
169080dd10fSScott Constable };
17047fe1b63SKrzysztof Parzyszek
17147fe1b63SKrzysztof Parzyszek SmallSet<NodeId,32> Defs;
17247fe1b63SKrzysztof Parzyszek
1730bd6a9f2SZarko Todorovski // Remove all non-phi defs that are not aliased to RefRR, and separate
17447fe1b63SKrzysztof Parzyszek // the the remaining defs into buckets for containing blocks.
17547fe1b63SKrzysztof Parzyszek std::map<NodeId, NodeAddr<InstrNode*>> Owners;
17647fe1b63SKrzysztof Parzyszek std::map<MachineBasicBlock*, SmallVector<NodeId,32>> Blocks;
17747fe1b63SKrzysztof Parzyszek for (NodeId N : DefQ) {
17847fe1b63SKrzysztof Parzyszek auto TA = DFG.addr<DefNode*>(N);
17947fe1b63SKrzysztof Parzyszek bool IsPhi = TA.Addr->getFlags() & NodeAttrs::PhiRef;
18047fe1b63SKrzysztof Parzyszek if (!IsPhi && !PRI.alias(RefRR, TA.Addr->getRegRef(DFG)))
18147fe1b63SKrzysztof Parzyszek continue;
18247fe1b63SKrzysztof Parzyszek Defs.insert(TA.Id);
18347fe1b63SKrzysztof Parzyszek NodeAddr<InstrNode*> IA = TA.Addr->getOwner(DFG);
18447fe1b63SKrzysztof Parzyszek Owners[TA.Id] = IA;
18547fe1b63SKrzysztof Parzyszek Blocks[Block(IA)].push_back(IA.Id);
18647fe1b63SKrzysztof Parzyszek }
18747fe1b63SKrzysztof Parzyszek
18847fe1b63SKrzysztof Parzyszek auto Precedes = [this,&OrdMap] (NodeId A, NodeId B) {
189080dd10fSScott Constable if (A == B)
190080dd10fSScott Constable return false;
19147fe1b63SKrzysztof Parzyszek NodeAddr<InstrNode*> OA = DFG.addr<InstrNode*>(A);
19247fe1b63SKrzysztof Parzyszek NodeAddr<InstrNode*> OB = DFG.addr<InstrNode*>(B);
193080dd10fSScott Constable bool StmtA = OA.Addr->getKind() == NodeAttrs::Stmt;
194080dd10fSScott Constable bool StmtB = OB.Addr->getKind() == NodeAttrs::Stmt;
19547fe1b63SKrzysztof Parzyszek if (StmtA && StmtB) {
19647fe1b63SKrzysztof Parzyszek const MachineInstr *InA = NodeAddr<StmtNode*>(OA).Addr->getCode();
19747fe1b63SKrzysztof Parzyszek const MachineInstr *InB = NodeAddr<StmtNode*>(OB).Addr->getCode();
19847fe1b63SKrzysztof Parzyszek assert(InA->getParent() == InB->getParent());
19947fe1b63SKrzysztof Parzyszek auto FA = OrdMap.find(InA);
20047fe1b63SKrzysztof Parzyszek if (FA != OrdMap.end())
20147fe1b63SKrzysztof Parzyszek return FA->second < OrdMap.find(InB)->second;
20247fe1b63SKrzysztof Parzyszek const MachineBasicBlock *BB = InA->getParent();
20347fe1b63SKrzysztof Parzyszek for (auto It = BB->begin(), E = BB->end(); It != E; ++It) {
20447fe1b63SKrzysztof Parzyszek if (It == InA->getIterator())
205080dd10fSScott Constable return true;
20647fe1b63SKrzysztof Parzyszek if (It == InB->getIterator())
207080dd10fSScott Constable return false;
20847fe1b63SKrzysztof Parzyszek }
20947fe1b63SKrzysztof Parzyszek llvm_unreachable("InA and InB should be in the same block");
21047fe1b63SKrzysztof Parzyszek }
21147fe1b63SKrzysztof Parzyszek // One of them is a phi node.
21247fe1b63SKrzysztof Parzyszek if (!StmtA && !StmtB) {
21347fe1b63SKrzysztof Parzyszek // Both are phis, which are unordered. Break the tie by id numbers.
214080dd10fSScott Constable return A < B;
215080dd10fSScott Constable }
21647fe1b63SKrzysztof Parzyszek // Only one of them is a phi. Phis always precede statements.
21747fe1b63SKrzysztof Parzyszek return !StmtA;
218080dd10fSScott Constable };
219080dd10fSScott Constable
22047fe1b63SKrzysztof Parzyszek auto GetOrder = [&OrdMap] (MachineBasicBlock &B) {
22147fe1b63SKrzysztof Parzyszek uint32_t Pos = 0;
22247fe1b63SKrzysztof Parzyszek for (MachineInstr &In : B)
22347fe1b63SKrzysztof Parzyszek OrdMap.insert({&In, ++Pos});
22447fe1b63SKrzysztof Parzyszek };
22547fe1b63SKrzysztof Parzyszek
22647fe1b63SKrzysztof Parzyszek // For each block, sort the nodes in it.
22747fe1b63SKrzysztof Parzyszek std::vector<MachineBasicBlock*> TmpBB;
22847fe1b63SKrzysztof Parzyszek for (auto &Bucket : Blocks) {
22947fe1b63SKrzysztof Parzyszek TmpBB.push_back(Bucket.first);
23047fe1b63SKrzysztof Parzyszek if (Bucket.second.size() > 2)
23147fe1b63SKrzysztof Parzyszek GetOrder(*Bucket.first);
2329bcc0d10SKazu Hirata llvm::sort(Bucket.second, Precedes);
23347fe1b63SKrzysztof Parzyszek }
23447fe1b63SKrzysztof Parzyszek
23547fe1b63SKrzysztof Parzyszek // Sort the blocks with respect to dominance.
2369bcc0d10SKazu Hirata llvm::sort(TmpBB,
2370b342263SAlina Sbirlea [this](auto A, auto B) { return MDT.properlyDominates(A, B); });
23847fe1b63SKrzysztof Parzyszek
23947fe1b63SKrzysztof Parzyszek std::vector<NodeId> TmpInst;
2400b417ba2SKazu Hirata for (MachineBasicBlock *MBB : llvm::reverse(TmpBB)) {
2410b417ba2SKazu Hirata auto &Bucket = Blocks[MBB];
24247fe1b63SKrzysztof Parzyszek TmpInst.insert(TmpInst.end(), Bucket.rbegin(), Bucket.rend());
24347fe1b63SKrzysztof Parzyszek }
244080dd10fSScott Constable
245080dd10fSScott Constable // The vector is a list of instructions, so that defs coming from
246080dd10fSScott Constable // the same instruction don't need to be artificially ordered.
247080dd10fSScott Constable // Then, when computing the initial segment, and iterating over an
248080dd10fSScott Constable // instruction, pick the defs that contribute to the covering (i.e. is
249080dd10fSScott Constable // not covered by previously added defs). Check the defs individually,
250080dd10fSScott Constable // i.e. first check each def if is covered or not (without adding them
251080dd10fSScott Constable // to the tracking set), and then add all the selected ones.
252080dd10fSScott Constable
253080dd10fSScott Constable // The reason for this is this example:
254080dd10fSScott Constable // *d1<A>, *d2<B>, ... Assume A and B are aliased (can happen in phi nodes).
255080dd10fSScott Constable // *d3<C> If A \incl BuC, and B \incl AuC, then *d2 would be
256080dd10fSScott Constable // covered if we added A first, and A would be covered
257080dd10fSScott Constable // if we added B first.
25847fe1b63SKrzysztof Parzyszek // In this example we want both A and B, because we don't want to give
25947fe1b63SKrzysztof Parzyszek // either one priority over the other, since they belong to the same
26047fe1b63SKrzysztof Parzyszek // statement.
261080dd10fSScott Constable
262080dd10fSScott Constable RegisterAggr RRs(DefRRs);
263080dd10fSScott Constable
264080dd10fSScott Constable auto DefInSet = [&Defs] (NodeAddr<RefNode*> TA) -> bool {
265080dd10fSScott Constable return TA.Addr->getKind() == NodeAttrs::Def &&
266080dd10fSScott Constable Defs.count(TA.Id);
267080dd10fSScott Constable };
26847fe1b63SKrzysztof Parzyszek
26947fe1b63SKrzysztof Parzyszek for (NodeId T : TmpInst) {
270080dd10fSScott Constable if (!FullChain && RRs.hasCoverOf(RefRR))
271080dd10fSScott Constable break;
272080dd10fSScott Constable auto TA = DFG.addr<InstrNode*>(T);
273080dd10fSScott Constable bool IsPhi = DFG.IsCode<NodeAttrs::Phi>(TA);
274080dd10fSScott Constable NodeList Ds;
275080dd10fSScott Constable for (NodeAddr<DefNode*> DA : TA.Addr->members_if(DefInSet, DFG)) {
276080dd10fSScott Constable RegisterRef QR = DA.Addr->getRegRef(DFG);
277080dd10fSScott Constable // Add phi defs even if they are covered by subsequent defs. This is
278080dd10fSScott Constable // for cases where the reached use is not covered by any of the defs
279080dd10fSScott Constable // encountered so far: the phi def is needed to expose the liveness
280080dd10fSScott Constable // of that use to the entry of the block.
281080dd10fSScott Constable // Example:
282080dd10fSScott Constable // phi d1<R3>(,d2,), ... Phi def d1 is covered by d2.
283080dd10fSScott Constable // d2<R3>(d1,,u3), ...
284080dd10fSScott Constable // ..., u3<D1>(d2) This use needs to be live on entry.
285080dd10fSScott Constable if (FullChain || IsPhi || !RRs.hasCoverOf(QR))
286080dd10fSScott Constable Ds.push_back(DA);
287080dd10fSScott Constable }
2881e3ed091SKazu Hirata llvm::append_range(RDefs, Ds);
289080dd10fSScott Constable for (NodeAddr<DefNode*> DA : Ds) {
290080dd10fSScott Constable // When collecting a full chain of definitions, do not consider phi
291080dd10fSScott Constable // defs to actually define a register.
292080dd10fSScott Constable uint16_t Flags = DA.Addr->getFlags();
293080dd10fSScott Constable if (!FullChain || !(Flags & NodeAttrs::PhiRef))
294080dd10fSScott Constable if (!(Flags & NodeAttrs::Preserving)) // Don't care about Undef here.
295080dd10fSScott Constable RRs.insert(DA.Addr->getRegRef(DFG));
296080dd10fSScott Constable }
297080dd10fSScott Constable }
298080dd10fSScott Constable
299080dd10fSScott Constable auto DeadP = [](const NodeAddr<DefNode*> DA) -> bool {
300080dd10fSScott Constable return DA.Addr->getFlags() & NodeAttrs::Dead;
301080dd10fSScott Constable };
3023285ee14SKazu Hirata llvm::erase_if(RDefs, DeadP);
303080dd10fSScott Constable
304080dd10fSScott Constable return RDefs;
305080dd10fSScott Constable }
306080dd10fSScott Constable
307080dd10fSScott Constable std::pair<NodeSet,bool>
getAllReachingDefsRec(RegisterRef RefRR,NodeAddr<RefNode * > RefA,NodeSet & Visited,const NodeSet & Defs)308080dd10fSScott Constable Liveness::getAllReachingDefsRec(RegisterRef RefRR, NodeAddr<RefNode*> RefA,
309080dd10fSScott Constable NodeSet &Visited, const NodeSet &Defs) {
310080dd10fSScott Constable return getAllReachingDefsRecImpl(RefRR, RefA, Visited, Defs, 0, MaxRecNest);
311080dd10fSScott Constable }
312080dd10fSScott Constable
313080dd10fSScott Constable std::pair<NodeSet,bool>
getAllReachingDefsRecImpl(RegisterRef RefRR,NodeAddr<RefNode * > RefA,NodeSet & Visited,const NodeSet & Defs,unsigned Nest,unsigned MaxNest)314080dd10fSScott Constable Liveness::getAllReachingDefsRecImpl(RegisterRef RefRR, NodeAddr<RefNode*> RefA,
315080dd10fSScott Constable NodeSet &Visited, const NodeSet &Defs, unsigned Nest, unsigned MaxNest) {
316080dd10fSScott Constable if (Nest > MaxNest)
317080dd10fSScott Constable return { NodeSet(), false };
318080dd10fSScott Constable // Collect all defined registers. Do not consider phis to be defining
319080dd10fSScott Constable // anything, only collect "real" definitions.
320080dd10fSScott Constable RegisterAggr DefRRs(PRI);
321080dd10fSScott Constable for (NodeId D : Defs) {
322080dd10fSScott Constable const auto DA = DFG.addr<const DefNode*>(D);
323080dd10fSScott Constable if (!(DA.Addr->getFlags() & NodeAttrs::PhiRef))
324080dd10fSScott Constable DefRRs.insert(DA.Addr->getRegRef(DFG));
325080dd10fSScott Constable }
326080dd10fSScott Constable
327080dd10fSScott Constable NodeList RDs = getAllReachingDefs(RefRR, RefA, false, true, DefRRs);
328080dd10fSScott Constable if (RDs.empty())
329080dd10fSScott Constable return { Defs, true };
330080dd10fSScott Constable
331080dd10fSScott Constable // Make a copy of the preexisting definitions and add the newly found ones.
332080dd10fSScott Constable NodeSet TmpDefs = Defs;
333080dd10fSScott Constable for (NodeAddr<NodeBase*> R : RDs)
334080dd10fSScott Constable TmpDefs.insert(R.Id);
335080dd10fSScott Constable
336080dd10fSScott Constable NodeSet Result = Defs;
337080dd10fSScott Constable
338080dd10fSScott Constable for (NodeAddr<DefNode*> DA : RDs) {
339080dd10fSScott Constable Result.insert(DA.Id);
340080dd10fSScott Constable if (!(DA.Addr->getFlags() & NodeAttrs::PhiRef))
341080dd10fSScott Constable continue;
342080dd10fSScott Constable NodeAddr<PhiNode*> PA = DA.Addr->getOwner(DFG);
343b254d671SKazu Hirata if (!Visited.insert(PA.Id).second)
344080dd10fSScott Constable continue;
345080dd10fSScott Constable // Go over all phi uses and get the reaching defs for each use.
346080dd10fSScott Constable for (auto U : PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG)) {
347080dd10fSScott Constable const auto &T = getAllReachingDefsRecImpl(RefRR, U, Visited, TmpDefs,
348080dd10fSScott Constable Nest+1, MaxNest);
349080dd10fSScott Constable if (!T.second)
350080dd10fSScott Constable return { T.first, false };
351080dd10fSScott Constable Result.insert(T.first.begin(), T.first.end());
352080dd10fSScott Constable }
353080dd10fSScott Constable }
354080dd10fSScott Constable
355080dd10fSScott Constable return { Result, true };
356080dd10fSScott Constable }
357080dd10fSScott Constable
358080dd10fSScott Constable /// Find the nearest ref node aliased to RefRR, going upwards in the data
359080dd10fSScott Constable /// flow, starting from the instruction immediately preceding Inst.
getNearestAliasedRef(RegisterRef RefRR,NodeAddr<InstrNode * > IA)360080dd10fSScott Constable NodeAddr<RefNode*> Liveness::getNearestAliasedRef(RegisterRef RefRR,
361080dd10fSScott Constable NodeAddr<InstrNode*> IA) {
362080dd10fSScott Constable NodeAddr<BlockNode*> BA = IA.Addr->getOwner(DFG);
363080dd10fSScott Constable NodeList Ins = BA.Addr->members(DFG);
364080dd10fSScott Constable NodeId FindId = IA.Id;
365080dd10fSScott Constable auto E = Ins.rend();
366080dd10fSScott Constable auto B = std::find_if(Ins.rbegin(), E,
367080dd10fSScott Constable [FindId] (const NodeAddr<InstrNode*> T) {
368080dd10fSScott Constable return T.Id == FindId;
369080dd10fSScott Constable });
370080dd10fSScott Constable // Do not scan IA (which is what B would point to).
371080dd10fSScott Constable if (B != E)
372080dd10fSScott Constable ++B;
373080dd10fSScott Constable
374080dd10fSScott Constable do {
375080dd10fSScott Constable // Process the range of instructions from B to E.
376080dd10fSScott Constable for (NodeAddr<InstrNode*> I : make_range(B, E)) {
377080dd10fSScott Constable NodeList Refs = I.Addr->members(DFG);
378080dd10fSScott Constable NodeAddr<RefNode*> Clob, Use;
379080dd10fSScott Constable // Scan all the refs in I aliased to RefRR, and return the one that
380080dd10fSScott Constable // is the closest to the output of I, i.e. def > clobber > use.
381080dd10fSScott Constable for (NodeAddr<RefNode*> R : Refs) {
382080dd10fSScott Constable if (!PRI.alias(R.Addr->getRegRef(DFG), RefRR))
383080dd10fSScott Constable continue;
384080dd10fSScott Constable if (DFG.IsDef(R)) {
385080dd10fSScott Constable // If it's a non-clobbering def, just return it.
386080dd10fSScott Constable if (!(R.Addr->getFlags() & NodeAttrs::Clobbering))
387080dd10fSScott Constable return R;
388080dd10fSScott Constable Clob = R;
389080dd10fSScott Constable } else {
390080dd10fSScott Constable Use = R;
391080dd10fSScott Constable }
392080dd10fSScott Constable }
393080dd10fSScott Constable if (Clob.Id != 0)
394080dd10fSScott Constable return Clob;
395080dd10fSScott Constable if (Use.Id != 0)
396080dd10fSScott Constable return Use;
397080dd10fSScott Constable }
398080dd10fSScott Constable
399080dd10fSScott Constable // Go up to the immediate dominator, if any.
400080dd10fSScott Constable MachineBasicBlock *BB = BA.Addr->getCode();
401080dd10fSScott Constable BA = NodeAddr<BlockNode*>();
402080dd10fSScott Constable if (MachineDomTreeNode *N = MDT.getNode(BB)) {
403080dd10fSScott Constable if ((N = N->getIDom()))
404080dd10fSScott Constable BA = DFG.findBlock(N->getBlock());
405080dd10fSScott Constable }
406080dd10fSScott Constable if (!BA.Id)
407080dd10fSScott Constable break;
408080dd10fSScott Constable
409080dd10fSScott Constable Ins = BA.Addr->members(DFG);
410080dd10fSScott Constable B = Ins.rbegin();
411080dd10fSScott Constable E = Ins.rend();
412080dd10fSScott Constable } while (true);
413080dd10fSScott Constable
414080dd10fSScott Constable return NodeAddr<RefNode*>();
415080dd10fSScott Constable }
416080dd10fSScott Constable
getAllReachedUses(RegisterRef RefRR,NodeAddr<DefNode * > DefA,const RegisterAggr & DefRRs)417080dd10fSScott Constable NodeSet Liveness::getAllReachedUses(RegisterRef RefRR,
418080dd10fSScott Constable NodeAddr<DefNode*> DefA, const RegisterAggr &DefRRs) {
419080dd10fSScott Constable NodeSet Uses;
420080dd10fSScott Constable
421080dd10fSScott Constable // If the original register is already covered by all the intervening
422080dd10fSScott Constable // defs, no more uses can be reached.
423080dd10fSScott Constable if (DefRRs.hasCoverOf(RefRR))
424080dd10fSScott Constable return Uses;
425080dd10fSScott Constable
426080dd10fSScott Constable // Add all directly reached uses.
427080dd10fSScott Constable // If the def is dead, it does not provide a value for any use.
428080dd10fSScott Constable bool IsDead = DefA.Addr->getFlags() & NodeAttrs::Dead;
429080dd10fSScott Constable NodeId U = !IsDead ? DefA.Addr->getReachedUse() : 0;
430080dd10fSScott Constable while (U != 0) {
431080dd10fSScott Constable auto UA = DFG.addr<UseNode*>(U);
432080dd10fSScott Constable if (!(UA.Addr->getFlags() & NodeAttrs::Undef)) {
433080dd10fSScott Constable RegisterRef UR = UA.Addr->getRegRef(DFG);
434080dd10fSScott Constable if (PRI.alias(RefRR, UR) && !DefRRs.hasCoverOf(UR))
435080dd10fSScott Constable Uses.insert(U);
436080dd10fSScott Constable }
437080dd10fSScott Constable U = UA.Addr->getSibling();
438080dd10fSScott Constable }
439080dd10fSScott Constable
440080dd10fSScott Constable // Traverse all reached defs. This time dead defs cannot be ignored.
441080dd10fSScott Constable for (NodeId D = DefA.Addr->getReachedDef(), NextD; D != 0; D = NextD) {
442080dd10fSScott Constable auto DA = DFG.addr<DefNode*>(D);
443080dd10fSScott Constable NextD = DA.Addr->getSibling();
444080dd10fSScott Constable RegisterRef DR = DA.Addr->getRegRef(DFG);
445080dd10fSScott Constable // If this def is already covered, it cannot reach anything new.
446080dd10fSScott Constable // Similarly, skip it if it is not aliased to the interesting register.
447080dd10fSScott Constable if (DefRRs.hasCoverOf(DR) || !PRI.alias(RefRR, DR))
448080dd10fSScott Constable continue;
449080dd10fSScott Constable NodeSet T;
450080dd10fSScott Constable if (DFG.IsPreservingDef(DA)) {
451080dd10fSScott Constable // If it is a preserving def, do not update the set of intervening defs.
452080dd10fSScott Constable T = getAllReachedUses(RefRR, DA, DefRRs);
453080dd10fSScott Constable } else {
454080dd10fSScott Constable RegisterAggr NewDefRRs = DefRRs;
455080dd10fSScott Constable NewDefRRs.insert(DR);
456080dd10fSScott Constable T = getAllReachedUses(RefRR, DA, NewDefRRs);
457080dd10fSScott Constable }
458080dd10fSScott Constable Uses.insert(T.begin(), T.end());
459080dd10fSScott Constable }
460080dd10fSScott Constable return Uses;
461080dd10fSScott Constable }
462080dd10fSScott Constable
computePhiInfo()463080dd10fSScott Constable void Liveness::computePhiInfo() {
464080dd10fSScott Constable RealUseMap.clear();
465080dd10fSScott Constable
466080dd10fSScott Constable NodeList Phis;
467080dd10fSScott Constable NodeAddr<FuncNode*> FA = DFG.getFunc();
468080dd10fSScott Constable NodeList Blocks = FA.Addr->members(DFG);
469080dd10fSScott Constable for (NodeAddr<BlockNode*> BA : Blocks) {
470080dd10fSScott Constable auto Ps = BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG);
4711e3ed091SKazu Hirata llvm::append_range(Phis, Ps);
472080dd10fSScott Constable }
473080dd10fSScott Constable
474080dd10fSScott Constable // phi use -> (map: reaching phi -> set of registers defined in between)
475080dd10fSScott Constable std::map<NodeId,std::map<NodeId,RegisterAggr>> PhiUp;
476080dd10fSScott Constable std::vector<NodeId> PhiUQ; // Work list of phis for upward propagation.
47795217045SKrzysztof Parzyszek std::unordered_map<NodeId,RegisterAggr> PhiDRs; // Phi -> registers defined by it.
478080dd10fSScott Constable
479080dd10fSScott Constable // Go over all phis.
480080dd10fSScott Constable for (NodeAddr<PhiNode*> PhiA : Phis) {
481080dd10fSScott Constable // Go over all defs and collect the reached uses that are non-phi uses
482080dd10fSScott Constable // (i.e. the "real uses").
483080dd10fSScott Constable RefMap &RealUses = RealUseMap[PhiA.Id];
484080dd10fSScott Constable NodeList PhiRefs = PhiA.Addr->members(DFG);
485080dd10fSScott Constable
486080dd10fSScott Constable // Have a work queue of defs whose reached uses need to be found.
487080dd10fSScott Constable // For each def, add to the queue all reached (non-phi) defs.
488080dd10fSScott Constable SetVector<NodeId> DefQ;
489080dd10fSScott Constable NodeSet PhiDefs;
490080dd10fSScott Constable RegisterAggr DRs(PRI);
491080dd10fSScott Constable for (NodeAddr<RefNode*> R : PhiRefs) {
492080dd10fSScott Constable if (!DFG.IsRef<NodeAttrs::Def>(R))
493080dd10fSScott Constable continue;
494080dd10fSScott Constable DRs.insert(R.Addr->getRegRef(DFG));
495080dd10fSScott Constable DefQ.insert(R.Id);
496080dd10fSScott Constable PhiDefs.insert(R.Id);
497080dd10fSScott Constable }
498080dd10fSScott Constable PhiDRs.insert(std::make_pair(PhiA.Id, DRs));
499080dd10fSScott Constable
500080dd10fSScott Constable // Collect the super-set of all possible reached uses. This set will
501080dd10fSScott Constable // contain all uses reached from this phi, either directly from the
502080dd10fSScott Constable // phi defs, or (recursively) via non-phi defs reached by the phi defs.
503080dd10fSScott Constable // This set of uses will later be trimmed to only contain these uses that
504080dd10fSScott Constable // are actually reached by the phi defs.
505080dd10fSScott Constable for (unsigned i = 0; i < DefQ.size(); ++i) {
506080dd10fSScott Constable NodeAddr<DefNode*> DA = DFG.addr<DefNode*>(DefQ[i]);
507080dd10fSScott Constable // Visit all reached uses. Phi defs should not really have the "dead"
508080dd10fSScott Constable // flag set, but check it anyway for consistency.
509080dd10fSScott Constable bool IsDead = DA.Addr->getFlags() & NodeAttrs::Dead;
510080dd10fSScott Constable NodeId UN = !IsDead ? DA.Addr->getReachedUse() : 0;
511080dd10fSScott Constable while (UN != 0) {
512080dd10fSScott Constable NodeAddr<UseNode*> A = DFG.addr<UseNode*>(UN);
513080dd10fSScott Constable uint16_t F = A.Addr->getFlags();
514080dd10fSScott Constable if ((F & (NodeAttrs::Undef | NodeAttrs::PhiRef)) == 0) {
5154b25f672SKrzysztof Parzyszek RegisterRef R = A.Addr->getRegRef(DFG);
516080dd10fSScott Constable RealUses[R.Reg].insert({A.Id,R.Mask});
517080dd10fSScott Constable }
518080dd10fSScott Constable UN = A.Addr->getSibling();
519080dd10fSScott Constable }
520080dd10fSScott Constable // Visit all reached defs, and add them to the queue. These defs may
521080dd10fSScott Constable // override some of the uses collected here, but that will be handled
522080dd10fSScott Constable // later.
523080dd10fSScott Constable NodeId DN = DA.Addr->getReachedDef();
524080dd10fSScott Constable while (DN != 0) {
525080dd10fSScott Constable NodeAddr<DefNode*> A = DFG.addr<DefNode*>(DN);
526080dd10fSScott Constable for (auto T : DFG.getRelatedRefs(A.Addr->getOwner(DFG), A)) {
527080dd10fSScott Constable uint16_t Flags = NodeAddr<DefNode*>(T).Addr->getFlags();
528080dd10fSScott Constable // Must traverse the reached-def chain. Consider:
529080dd10fSScott Constable // def(D0) -> def(R0) -> def(R0) -> use(D0)
530080dd10fSScott Constable // The reachable use of D0 passes through a def of R0.
531080dd10fSScott Constable if (!(Flags & NodeAttrs::PhiRef))
532080dd10fSScott Constable DefQ.insert(T.Id);
533080dd10fSScott Constable }
534080dd10fSScott Constable DN = A.Addr->getSibling();
535080dd10fSScott Constable }
536080dd10fSScott Constable }
537080dd10fSScott Constable // Filter out these uses that appear to be reachable, but really
538080dd10fSScott Constable // are not. For example:
539080dd10fSScott Constable //
540080dd10fSScott Constable // R1:0 = d1
541080dd10fSScott Constable // = R1:0 u2 Reached by d1.
542080dd10fSScott Constable // R0 = d3
543080dd10fSScott Constable // = R1:0 u4 Still reached by d1: indirectly through
544080dd10fSScott Constable // the def d3.
545080dd10fSScott Constable // R1 = d5
546080dd10fSScott Constable // = R1:0 u6 Not reached by d1 (covered collectively
547080dd10fSScott Constable // by d3 and d5), but following reached
548080dd10fSScott Constable // defs and uses from d1 will lead here.
549080dd10fSScott Constable for (auto UI = RealUses.begin(), UE = RealUses.end(); UI != UE; ) {
550080dd10fSScott Constable // For each reached register UI->first, there is a set UI->second, of
551080dd10fSScott Constable // uses of it. For each such use, check if it is reached by this phi,
552080dd10fSScott Constable // i.e. check if the set of its reaching uses intersects the set of
553080dd10fSScott Constable // this phi's defs.
554080dd10fSScott Constable NodeRefSet Uses = UI->second;
555080dd10fSScott Constable UI->second.clear();
556080dd10fSScott Constable for (std::pair<NodeId,LaneBitmask> I : Uses) {
557080dd10fSScott Constable auto UA = DFG.addr<UseNode*>(I.first);
558080dd10fSScott Constable // Undef flag is checked above.
559080dd10fSScott Constable assert((UA.Addr->getFlags() & NodeAttrs::Undef) == 0);
560080dd10fSScott Constable RegisterRef R(UI->first, I.second);
561080dd10fSScott Constable // Calculate the exposed part of the reached use.
562080dd10fSScott Constable RegisterAggr Covered(PRI);
563080dd10fSScott Constable for (NodeAddr<DefNode*> DA : getAllReachingDefs(R, UA)) {
564080dd10fSScott Constable if (PhiDefs.count(DA.Id))
565080dd10fSScott Constable break;
566080dd10fSScott Constable Covered.insert(DA.Addr->getRegRef(DFG));
567080dd10fSScott Constable }
568080dd10fSScott Constable if (RegisterRef RC = Covered.clearIn(R)) {
569080dd10fSScott Constable // We are updating the map for register UI->first, so we need
570080dd10fSScott Constable // to map RC to be expressed in terms of that register.
571080dd10fSScott Constable RegisterRef S = PRI.mapTo(RC, UI->first);
572080dd10fSScott Constable UI->second.insert({I.first, S.Mask});
573080dd10fSScott Constable }
574080dd10fSScott Constable }
575080dd10fSScott Constable UI = UI->second.empty() ? RealUses.erase(UI) : std::next(UI);
576080dd10fSScott Constable }
577080dd10fSScott Constable
578080dd10fSScott Constable // If this phi reaches some "real" uses, add it to the queue for upward
579080dd10fSScott Constable // propagation.
580080dd10fSScott Constable if (!RealUses.empty())
581080dd10fSScott Constable PhiUQ.push_back(PhiA.Id);
582080dd10fSScott Constable
583080dd10fSScott Constable // Go over all phi uses and check if the reaching def is another phi.
584080dd10fSScott Constable // Collect the phis that are among the reaching defs of these uses.
585080dd10fSScott Constable // While traversing the list of reaching defs for each phi use, accumulate
586080dd10fSScott Constable // the set of registers defined between this phi (PhiA) and the owner phi
587080dd10fSScott Constable // of the reaching def.
588080dd10fSScott Constable NodeSet SeenUses;
589080dd10fSScott Constable
590080dd10fSScott Constable for (auto I : PhiRefs) {
591080dd10fSScott Constable if (!DFG.IsRef<NodeAttrs::Use>(I) || SeenUses.count(I.Id))
592080dd10fSScott Constable continue;
593080dd10fSScott Constable NodeAddr<PhiUseNode*> PUA = I;
594080dd10fSScott Constable if (PUA.Addr->getReachingDef() == 0)
595080dd10fSScott Constable continue;
596080dd10fSScott Constable
597080dd10fSScott Constable RegisterRef UR = PUA.Addr->getRegRef(DFG);
598080dd10fSScott Constable NodeList Ds = getAllReachingDefs(UR, PUA, true, false, NoRegs);
599080dd10fSScott Constable RegisterAggr DefRRs(PRI);
600080dd10fSScott Constable
601080dd10fSScott Constable for (NodeAddr<DefNode*> D : Ds) {
602080dd10fSScott Constable if (D.Addr->getFlags() & NodeAttrs::PhiRef) {
603080dd10fSScott Constable NodeId RP = D.Addr->getOwner(DFG).Id;
604080dd10fSScott Constable std::map<NodeId,RegisterAggr> &M = PhiUp[PUA.Id];
605080dd10fSScott Constable auto F = M.find(RP);
606080dd10fSScott Constable if (F == M.end())
607080dd10fSScott Constable M.insert(std::make_pair(RP, DefRRs));
608080dd10fSScott Constable else
609080dd10fSScott Constable F->second.insert(DefRRs);
610080dd10fSScott Constable }
611080dd10fSScott Constable DefRRs.insert(D.Addr->getRegRef(DFG));
612080dd10fSScott Constable }
613080dd10fSScott Constable
614080dd10fSScott Constable for (NodeAddr<PhiUseNode*> T : DFG.getRelatedRefs(PhiA, PUA))
615080dd10fSScott Constable SeenUses.insert(T.Id);
616080dd10fSScott Constable }
617080dd10fSScott Constable }
618080dd10fSScott Constable
619080dd10fSScott Constable if (Trace) {
620080dd10fSScott Constable dbgs() << "Phi-up-to-phi map with intervening defs:\n";
621080dd10fSScott Constable for (auto I : PhiUp) {
622080dd10fSScott Constable dbgs() << "phi " << Print<NodeId>(I.first, DFG) << " -> {";
623080dd10fSScott Constable for (auto R : I.second)
624080dd10fSScott Constable dbgs() << ' ' << Print<NodeId>(R.first, DFG)
625080dd10fSScott Constable << Print<RegisterAggr>(R.second, DFG);
626080dd10fSScott Constable dbgs() << " }\n";
627080dd10fSScott Constable }
628080dd10fSScott Constable }
629080dd10fSScott Constable
630080dd10fSScott Constable // Propagate the reached registers up in the phi chain.
631080dd10fSScott Constable //
632080dd10fSScott Constable // The following type of situation needs careful handling:
633080dd10fSScott Constable //
634080dd10fSScott Constable // phi d1<R1:0> (1)
635080dd10fSScott Constable // |
636080dd10fSScott Constable // ... d2<R1>
637080dd10fSScott Constable // |
638080dd10fSScott Constable // phi u3<R1:0> (2)
639080dd10fSScott Constable // |
640080dd10fSScott Constable // ... u4<R1>
641080dd10fSScott Constable //
642080dd10fSScott Constable // The phi node (2) defines a register pair R1:0, and reaches a "real"
643080dd10fSScott Constable // use u4 of just R1. The same phi node is also known to reach (upwards)
644080dd10fSScott Constable // the phi node (1). However, the use u4 is not reached by phi (1),
645080dd10fSScott Constable // because of the intervening definition d2 of R1. The data flow between
646080dd10fSScott Constable // phis (1) and (2) is restricted to R1:0 minus R1, i.e. R0.
647080dd10fSScott Constable //
648080dd10fSScott Constable // When propagating uses up the phi chains, get the all reaching defs
649080dd10fSScott Constable // for a given phi use, and traverse the list until the propagated ref
650080dd10fSScott Constable // is covered, or until reaching the final phi. Only assume that the
651080dd10fSScott Constable // reference reaches the phi in the latter case.
652080dd10fSScott Constable
65395217045SKrzysztof Parzyszek // The operation "clearIn" can be expensive. For a given set of intervening
65495217045SKrzysztof Parzyszek // defs, cache the result of subtracting these defs from a given register
65595217045SKrzysztof Parzyszek // ref.
65695217045SKrzysztof Parzyszek using SubMap = std::unordered_map<RegisterRef, RegisterRef>;
65795217045SKrzysztof Parzyszek std::unordered_map<RegisterAggr, SubMap> Subs;
65895217045SKrzysztof Parzyszek auto ClearIn = [] (RegisterRef RR, const RegisterAggr &Mid, SubMap &SM) {
65995217045SKrzysztof Parzyszek if (Mid.empty())
66095217045SKrzysztof Parzyszek return RR;
66195217045SKrzysztof Parzyszek auto F = SM.find(RR);
66295217045SKrzysztof Parzyszek if (F != SM.end())
66395217045SKrzysztof Parzyszek return F->second;
66495217045SKrzysztof Parzyszek RegisterRef S = Mid.clearIn(RR);
66595217045SKrzysztof Parzyszek SM.insert({RR, S});
66695217045SKrzysztof Parzyszek return S;
66795217045SKrzysztof Parzyszek };
66895217045SKrzysztof Parzyszek
66995217045SKrzysztof Parzyszek // Go over all phis.
670080dd10fSScott Constable for (unsigned i = 0; i < PhiUQ.size(); ++i) {
671080dd10fSScott Constable auto PA = DFG.addr<PhiNode*>(PhiUQ[i]);
672080dd10fSScott Constable NodeList PUs = PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG);
673080dd10fSScott Constable RefMap &RUM = RealUseMap[PA.Id];
674080dd10fSScott Constable
675080dd10fSScott Constable for (NodeAddr<UseNode*> UA : PUs) {
676080dd10fSScott Constable std::map<NodeId,RegisterAggr> &PUM = PhiUp[UA.Id];
6774b25f672SKrzysztof Parzyszek RegisterRef UR = UA.Addr->getRegRef(DFG);
678080dd10fSScott Constable for (const std::pair<const NodeId, RegisterAggr> &P : PUM) {
679080dd10fSScott Constable bool Changed = false;
680080dd10fSScott Constable const RegisterAggr &MidDefs = P.second;
681080dd10fSScott Constable // Collect the set PropUp of uses that are reached by the current
682080dd10fSScott Constable // phi PA, and are not covered by any intervening def between the
683080dd10fSScott Constable // currently visited use UA and the upward phi P.
684080dd10fSScott Constable
685080dd10fSScott Constable if (MidDefs.hasCoverOf(UR))
686080dd10fSScott Constable continue;
68795217045SKrzysztof Parzyszek SubMap &SM = Subs[MidDefs];
688080dd10fSScott Constable
689080dd10fSScott Constable // General algorithm:
690080dd10fSScott Constable // for each (R,U) : U is use node of R, U is reached by PA
691080dd10fSScott Constable // if MidDefs does not cover (R,U)
692080dd10fSScott Constable // then add (R-MidDefs,U) to RealUseMap[P]
693080dd10fSScott Constable //
694080dd10fSScott Constable for (const std::pair<const RegisterId, NodeRefSet> &T : RUM) {
695080dd10fSScott Constable RegisterRef R(T.first);
696080dd10fSScott Constable // The current phi (PA) could be a phi for a regmask. It could
697080dd10fSScott Constable // reach a whole variety of uses that are not related to the
698080dd10fSScott Constable // specific upward phi (P.first).
699080dd10fSScott Constable const RegisterAggr &DRs = PhiDRs.at(P.first);
700080dd10fSScott Constable if (!DRs.hasAliasOf(R))
701080dd10fSScott Constable continue;
702080dd10fSScott Constable R = PRI.mapTo(DRs.intersectWith(R), T.first);
703080dd10fSScott Constable for (std::pair<NodeId,LaneBitmask> V : T.second) {
704080dd10fSScott Constable LaneBitmask M = R.Mask & V.second;
705080dd10fSScott Constable if (M.none())
706080dd10fSScott Constable continue;
70795217045SKrzysztof Parzyszek if (RegisterRef SS = ClearIn(RegisterRef(R.Reg, M), MidDefs, SM)) {
708080dd10fSScott Constable NodeRefSet &RS = RealUseMap[P.first][SS.Reg];
709080dd10fSScott Constable Changed |= RS.insert({V.first,SS.Mask}).second;
710080dd10fSScott Constable }
711080dd10fSScott Constable }
712080dd10fSScott Constable }
713080dd10fSScott Constable
714080dd10fSScott Constable if (Changed)
715080dd10fSScott Constable PhiUQ.push_back(P.first);
716080dd10fSScott Constable }
717080dd10fSScott Constable }
718080dd10fSScott Constable }
719080dd10fSScott Constable
720080dd10fSScott Constable if (Trace) {
721080dd10fSScott Constable dbgs() << "Real use map:\n";
722080dd10fSScott Constable for (auto I : RealUseMap) {
723080dd10fSScott Constable dbgs() << "phi " << Print<NodeId>(I.first, DFG);
724080dd10fSScott Constable NodeAddr<PhiNode*> PA = DFG.addr<PhiNode*>(I.first);
725080dd10fSScott Constable NodeList Ds = PA.Addr->members_if(DFG.IsRef<NodeAttrs::Def>, DFG);
726080dd10fSScott Constable if (!Ds.empty()) {
727080dd10fSScott Constable RegisterRef RR = NodeAddr<DefNode*>(Ds[0]).Addr->getRegRef(DFG);
728080dd10fSScott Constable dbgs() << '<' << Print<RegisterRef>(RR, DFG) << '>';
729080dd10fSScott Constable } else {
730080dd10fSScott Constable dbgs() << "<noreg>";
731080dd10fSScott Constable }
732080dd10fSScott Constable dbgs() << " -> " << Print<RefMap>(I.second, DFG) << '\n';
733080dd10fSScott Constable }
734080dd10fSScott Constable }
735080dd10fSScott Constable }
736080dd10fSScott Constable
computeLiveIns()737080dd10fSScott Constable void Liveness::computeLiveIns() {
738080dd10fSScott Constable // Populate the node-to-block map. This speeds up the calculations
739080dd10fSScott Constable // significantly.
740080dd10fSScott Constable NBMap.clear();
741080dd10fSScott Constable for (NodeAddr<BlockNode*> BA : DFG.getFunc().Addr->members(DFG)) {
742080dd10fSScott Constable MachineBasicBlock *BB = BA.Addr->getCode();
743080dd10fSScott Constable for (NodeAddr<InstrNode*> IA : BA.Addr->members(DFG)) {
744080dd10fSScott Constable for (NodeAddr<RefNode*> RA : IA.Addr->members(DFG))
745080dd10fSScott Constable NBMap.insert(std::make_pair(RA.Id, BB));
746080dd10fSScott Constable NBMap.insert(std::make_pair(IA.Id, BB));
747080dd10fSScott Constable }
748080dd10fSScott Constable }
749080dd10fSScott Constable
750080dd10fSScott Constable MachineFunction &MF = DFG.getMF();
751080dd10fSScott Constable
752080dd10fSScott Constable // Compute IDF first, then the inverse.
753080dd10fSScott Constable decltype(IIDF) IDF;
754080dd10fSScott Constable for (MachineBasicBlock &B : MF) {
755080dd10fSScott Constable auto F1 = MDF.find(&B);
756080dd10fSScott Constable if (F1 == MDF.end())
757080dd10fSScott Constable continue;
758080dd10fSScott Constable SetVector<MachineBasicBlock*> IDFB(F1->second.begin(), F1->second.end());
759080dd10fSScott Constable for (unsigned i = 0; i < IDFB.size(); ++i) {
760080dd10fSScott Constable auto F2 = MDF.find(IDFB[i]);
761080dd10fSScott Constable if (F2 != MDF.end())
762080dd10fSScott Constable IDFB.insert(F2->second.begin(), F2->second.end());
763080dd10fSScott Constable }
764080dd10fSScott Constable // Add B to the IDF(B). This will put B in the IIDF(B).
765080dd10fSScott Constable IDFB.insert(&B);
766080dd10fSScott Constable IDF[&B].insert(IDFB.begin(), IDFB.end());
767080dd10fSScott Constable }
768080dd10fSScott Constable
769080dd10fSScott Constable for (auto I : IDF)
770*9e6d1f4bSKazu Hirata for (auto *S : I.second)
771080dd10fSScott Constable IIDF[S].insert(I.first);
772080dd10fSScott Constable
773080dd10fSScott Constable computePhiInfo();
774080dd10fSScott Constable
775080dd10fSScott Constable NodeAddr<FuncNode*> FA = DFG.getFunc();
776080dd10fSScott Constable NodeList Blocks = FA.Addr->members(DFG);
777080dd10fSScott Constable
778080dd10fSScott Constable // Build the phi live-on-entry map.
779080dd10fSScott Constable for (NodeAddr<BlockNode*> BA : Blocks) {
780080dd10fSScott Constable MachineBasicBlock *MB = BA.Addr->getCode();
781080dd10fSScott Constable RefMap &LON = PhiLON[MB];
782080dd10fSScott Constable for (auto P : BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG))
783080dd10fSScott Constable for (const RefMap::value_type &S : RealUseMap[P.Id])
784080dd10fSScott Constable LON[S.first].insert(S.second.begin(), S.second.end());
785080dd10fSScott Constable }
786080dd10fSScott Constable
787080dd10fSScott Constable if (Trace) {
788080dd10fSScott Constable dbgs() << "Phi live-on-entry map:\n";
789080dd10fSScott Constable for (auto &I : PhiLON)
790080dd10fSScott Constable dbgs() << "block #" << I.first->getNumber() << " -> "
791080dd10fSScott Constable << Print<RefMap>(I.second, DFG) << '\n';
792080dd10fSScott Constable }
793080dd10fSScott Constable
794080dd10fSScott Constable // Build the phi live-on-exit map. Each phi node has some set of reached
795080dd10fSScott Constable // "real" uses. Propagate this set backwards into the block predecessors
796080dd10fSScott Constable // through the reaching defs of the corresponding phi uses.
797080dd10fSScott Constable for (NodeAddr<BlockNode*> BA : Blocks) {
798080dd10fSScott Constable NodeList Phis = BA.Addr->members_if(DFG.IsCode<NodeAttrs::Phi>, DFG);
799080dd10fSScott Constable for (NodeAddr<PhiNode*> PA : Phis) {
800080dd10fSScott Constable RefMap &RUs = RealUseMap[PA.Id];
801080dd10fSScott Constable if (RUs.empty())
802080dd10fSScott Constable continue;
803080dd10fSScott Constable
804080dd10fSScott Constable NodeSet SeenUses;
805080dd10fSScott Constable for (auto U : PA.Addr->members_if(DFG.IsRef<NodeAttrs::Use>, DFG)) {
806080dd10fSScott Constable if (!SeenUses.insert(U.Id).second)
807080dd10fSScott Constable continue;
808080dd10fSScott Constable NodeAddr<PhiUseNode*> PUA = U;
809080dd10fSScott Constable if (PUA.Addr->getReachingDef() == 0)
810080dd10fSScott Constable continue;
811080dd10fSScott Constable
812080dd10fSScott Constable // Each phi has some set (possibly empty) of reached "real" uses,
813080dd10fSScott Constable // that is, uses that are part of the compiled program. Such a use
814080dd10fSScott Constable // may be located in some farther block, but following a chain of
815080dd10fSScott Constable // reaching defs will eventually lead to this phi.
816080dd10fSScott Constable // Any chain of reaching defs may fork at a phi node, but there
817080dd10fSScott Constable // will be a path upwards that will lead to this phi. Now, this
818080dd10fSScott Constable // chain will need to fork at this phi, since some of the reached
819080dd10fSScott Constable // uses may have definitions joining in from multiple predecessors.
820080dd10fSScott Constable // For each reached "real" use, identify the set of reaching defs
821080dd10fSScott Constable // coming from each predecessor P, and add them to PhiLOX[P].
822080dd10fSScott Constable //
823080dd10fSScott Constable auto PrA = DFG.addr<BlockNode*>(PUA.Addr->getPredecessor());
824080dd10fSScott Constable RefMap &LOX = PhiLOX[PrA.Addr->getCode()];
825080dd10fSScott Constable
826080dd10fSScott Constable for (const std::pair<const RegisterId, NodeRefSet> &RS : RUs) {
827080dd10fSScott Constable // We need to visit each individual use.
828080dd10fSScott Constable for (std::pair<NodeId,LaneBitmask> P : RS.second) {
829080dd10fSScott Constable // Create a register ref corresponding to the use, and find
830080dd10fSScott Constable // all reaching defs starting from the phi use, and treating
831080dd10fSScott Constable // all related shadows as a single use cluster.
832080dd10fSScott Constable RegisterRef S(RS.first, P.second);
833080dd10fSScott Constable NodeList Ds = getAllReachingDefs(S, PUA, true, false, NoRegs);
834080dd10fSScott Constable for (NodeAddr<DefNode*> D : Ds) {
835080dd10fSScott Constable // Calculate the mask corresponding to the visited def.
836080dd10fSScott Constable RegisterAggr TA(PRI);
837080dd10fSScott Constable TA.insert(D.Addr->getRegRef(DFG)).intersect(S);
838080dd10fSScott Constable LaneBitmask TM = TA.makeRegRef().Mask;
839080dd10fSScott Constable LOX[S.Reg].insert({D.Id, TM});
840080dd10fSScott Constable }
841080dd10fSScott Constable }
842080dd10fSScott Constable }
843080dd10fSScott Constable
844080dd10fSScott Constable for (NodeAddr<PhiUseNode*> T : DFG.getRelatedRefs(PA, PUA))
845080dd10fSScott Constable SeenUses.insert(T.Id);
846080dd10fSScott Constable } // for U : phi uses
847080dd10fSScott Constable } // for P : Phis
848080dd10fSScott Constable } // for B : Blocks
849080dd10fSScott Constable
850080dd10fSScott Constable if (Trace) {
851080dd10fSScott Constable dbgs() << "Phi live-on-exit map:\n";
852080dd10fSScott Constable for (auto &I : PhiLOX)
853080dd10fSScott Constable dbgs() << "block #" << I.first->getNumber() << " -> "
854080dd10fSScott Constable << Print<RefMap>(I.second, DFG) << '\n';
855080dd10fSScott Constable }
856080dd10fSScott Constable
857080dd10fSScott Constable RefMap LiveIn;
858080dd10fSScott Constable traverse(&MF.front(), LiveIn);
859080dd10fSScott Constable
860080dd10fSScott Constable // Add function live-ins to the live-in set of the function entry block.
861080dd10fSScott Constable LiveMap[&MF.front()].insert(DFG.getLiveIns());
862080dd10fSScott Constable
863080dd10fSScott Constable if (Trace) {
864080dd10fSScott Constable // Dump the liveness map
865080dd10fSScott Constable for (MachineBasicBlock &B : MF) {
866080dd10fSScott Constable std::vector<RegisterRef> LV;
867a205fa5cSKazu Hirata for (const MachineBasicBlock::RegisterMaskPair &LI : B.liveins())
868a205fa5cSKazu Hirata LV.push_back(RegisterRef(LI.PhysReg, LI.LaneMask));
869080dd10fSScott Constable llvm::sort(LV);
870080dd10fSScott Constable dbgs() << printMBBReference(B) << "\t rec = {";
871080dd10fSScott Constable for (auto I : LV)
872080dd10fSScott Constable dbgs() << ' ' << Print<RegisterRef>(I, DFG);
873080dd10fSScott Constable dbgs() << " }\n";
874080dd10fSScott Constable //dbgs() << "\tcomp = " << Print<RegisterAggr>(LiveMap[&B], DFG) << '\n';
875080dd10fSScott Constable
876080dd10fSScott Constable LV.clear();
877080dd10fSScott Constable const RegisterAggr &LG = LiveMap[&B];
878080dd10fSScott Constable for (auto I = LG.rr_begin(), E = LG.rr_end(); I != E; ++I)
879080dd10fSScott Constable LV.push_back(*I);
880080dd10fSScott Constable llvm::sort(LV);
881080dd10fSScott Constable dbgs() << "\tcomp = {";
882080dd10fSScott Constable for (auto I : LV)
883080dd10fSScott Constable dbgs() << ' ' << Print<RegisterRef>(I, DFG);
884080dd10fSScott Constable dbgs() << " }\n";
885080dd10fSScott Constable
886080dd10fSScott Constable }
887080dd10fSScott Constable }
888080dd10fSScott Constable }
889080dd10fSScott Constable
resetLiveIns()890080dd10fSScott Constable void Liveness::resetLiveIns() {
891080dd10fSScott Constable for (auto &B : DFG.getMF()) {
892080dd10fSScott Constable // Remove all live-ins.
893080dd10fSScott Constable std::vector<unsigned> T;
894a205fa5cSKazu Hirata for (const MachineBasicBlock::RegisterMaskPair &LI : B.liveins())
895a205fa5cSKazu Hirata T.push_back(LI.PhysReg);
896080dd10fSScott Constable for (auto I : T)
897080dd10fSScott Constable B.removeLiveIn(I);
898080dd10fSScott Constable // Add the newly computed live-ins.
899080dd10fSScott Constable const RegisterAggr &LiveIns = LiveMap[&B];
9004691405bSAmara Emerson for (const RegisterRef R : make_range(LiveIns.rr_begin(), LiveIns.rr_end()))
901080dd10fSScott Constable B.addLiveIn({MCPhysReg(R.Reg), R.Mask});
902080dd10fSScott Constable }
903080dd10fSScott Constable }
904080dd10fSScott Constable
resetKills()905080dd10fSScott Constable void Liveness::resetKills() {
906080dd10fSScott Constable for (auto &B : DFG.getMF())
907080dd10fSScott Constable resetKills(&B);
908080dd10fSScott Constable }
909080dd10fSScott Constable
resetKills(MachineBasicBlock * B)910080dd10fSScott Constable void Liveness::resetKills(MachineBasicBlock *B) {
911080dd10fSScott Constable auto CopyLiveIns = [this] (MachineBasicBlock *B, BitVector &LV) -> void {
912080dd10fSScott Constable for (auto I : B->liveins()) {
913080dd10fSScott Constable MCSubRegIndexIterator S(I.PhysReg, &TRI);
914080dd10fSScott Constable if (!S.isValid()) {
915080dd10fSScott Constable LV.set(I.PhysReg);
916080dd10fSScott Constable continue;
917080dd10fSScott Constable }
918080dd10fSScott Constable do {
919080dd10fSScott Constable LaneBitmask M = TRI.getSubRegIndexLaneMask(S.getSubRegIndex());
920080dd10fSScott Constable if ((M & I.LaneMask).any())
921080dd10fSScott Constable LV.set(S.getSubReg());
922080dd10fSScott Constable ++S;
923080dd10fSScott Constable } while (S.isValid());
924080dd10fSScott Constable }
925080dd10fSScott Constable };
926080dd10fSScott Constable
927080dd10fSScott Constable BitVector LiveIn(TRI.getNumRegs()), Live(TRI.getNumRegs());
928080dd10fSScott Constable CopyLiveIns(B, LiveIn);
929*9e6d1f4bSKazu Hirata for (auto *SI : B->successors())
930080dd10fSScott Constable CopyLiveIns(SI, Live);
931080dd10fSScott Constable
9320b417ba2SKazu Hirata for (MachineInstr &MI : llvm::reverse(*B)) {
9330b417ba2SKazu Hirata if (MI.isDebugInstr())
934080dd10fSScott Constable continue;
935080dd10fSScott Constable
9360b417ba2SKazu Hirata MI.clearKillInfo();
9370b417ba2SKazu Hirata for (auto &Op : MI.operands()) {
938080dd10fSScott Constable // An implicit def of a super-register may not necessarily start a
939080dd10fSScott Constable // live range of it, since an implicit use could be used to keep parts
940080dd10fSScott Constable // of it live. Instead of analyzing the implicit operands, ignore
941080dd10fSScott Constable // implicit defs.
942080dd10fSScott Constable if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
943080dd10fSScott Constable continue;
944080dd10fSScott Constable Register R = Op.getReg();
945080dd10fSScott Constable if (!Register::isPhysicalRegister(R))
946080dd10fSScott Constable continue;
947080dd10fSScott Constable for (MCSubRegIterator SR(R, &TRI, true); SR.isValid(); ++SR)
948080dd10fSScott Constable Live.reset(*SR);
949080dd10fSScott Constable }
9500b417ba2SKazu Hirata for (auto &Op : MI.operands()) {
951080dd10fSScott Constable if (!Op.isReg() || !Op.isUse() || Op.isUndef())
952080dd10fSScott Constable continue;
953080dd10fSScott Constable Register R = Op.getReg();
954080dd10fSScott Constable if (!Register::isPhysicalRegister(R))
955080dd10fSScott Constable continue;
956080dd10fSScott Constable bool IsLive = false;
957080dd10fSScott Constable for (MCRegAliasIterator AR(R, &TRI, true); AR.isValid(); ++AR) {
958080dd10fSScott Constable if (!Live[*AR])
959080dd10fSScott Constable continue;
960080dd10fSScott Constable IsLive = true;
961080dd10fSScott Constable break;
962080dd10fSScott Constable }
963080dd10fSScott Constable if (!IsLive)
964080dd10fSScott Constable Op.setIsKill(true);
965080dd10fSScott Constable for (MCSubRegIterator SR(R, &TRI, true); SR.isValid(); ++SR)
966080dd10fSScott Constable Live.set(*SR);
967080dd10fSScott Constable }
968080dd10fSScott Constable }
969080dd10fSScott Constable }
970080dd10fSScott Constable
971080dd10fSScott Constable // Helper function to obtain the basic block containing the reaching def
972080dd10fSScott Constable // of the given use.
getBlockWithRef(NodeId RN) const973080dd10fSScott Constable MachineBasicBlock *Liveness::getBlockWithRef(NodeId RN) const {
974080dd10fSScott Constable auto F = NBMap.find(RN);
975080dd10fSScott Constable if (F != NBMap.end())
976080dd10fSScott Constable return F->second;
977080dd10fSScott Constable llvm_unreachable("Node id not in map");
978080dd10fSScott Constable }
979080dd10fSScott Constable
traverse(MachineBasicBlock * B,RefMap & LiveIn)980080dd10fSScott Constable void Liveness::traverse(MachineBasicBlock *B, RefMap &LiveIn) {
981080dd10fSScott Constable // The LiveIn map, for each (physical) register, contains the set of live
982080dd10fSScott Constable // reaching defs of that register that are live on entry to the associated
983080dd10fSScott Constable // block.
984080dd10fSScott Constable
985080dd10fSScott Constable // The summary of the traversal algorithm:
986080dd10fSScott Constable //
987080dd10fSScott Constable // R is live-in in B, if there exists a U(R), such that rdef(R) dom B
988080dd10fSScott Constable // and (U \in IDF(B) or B dom U).
989080dd10fSScott Constable //
990080dd10fSScott Constable // for (C : children) {
991080dd10fSScott Constable // LU = {}
992080dd10fSScott Constable // traverse(C, LU)
993080dd10fSScott Constable // LiveUses += LU
994080dd10fSScott Constable // }
995080dd10fSScott Constable //
996080dd10fSScott Constable // LiveUses -= Defs(B);
997080dd10fSScott Constable // LiveUses += UpwardExposedUses(B);
998080dd10fSScott Constable // for (C : IIDF[B])
999080dd10fSScott Constable // for (U : LiveUses)
1000080dd10fSScott Constable // if (Rdef(U) dom C)
1001080dd10fSScott Constable // C.addLiveIn(U)
1002080dd10fSScott Constable //
1003080dd10fSScott Constable
1004080dd10fSScott Constable // Go up the dominator tree (depth-first).
1005080dd10fSScott Constable MachineDomTreeNode *N = MDT.getNode(B);
1006*9e6d1f4bSKazu Hirata for (auto *I : *N) {
1007080dd10fSScott Constable RefMap L;
1008080dd10fSScott Constable MachineBasicBlock *SB = I->getBlock();
1009080dd10fSScott Constable traverse(SB, L);
1010080dd10fSScott Constable
1011080dd10fSScott Constable for (auto S : L)
1012080dd10fSScott Constable LiveIn[S.first].insert(S.second.begin(), S.second.end());
1013080dd10fSScott Constable }
1014080dd10fSScott Constable
1015080dd10fSScott Constable if (Trace) {
1016080dd10fSScott Constable dbgs() << "\n-- " << printMBBReference(*B) << ": " << __func__
1017080dd10fSScott Constable << " after recursion into: {";
1018*9e6d1f4bSKazu Hirata for (auto *I : *N)
1019080dd10fSScott Constable dbgs() << ' ' << I->getBlock()->getNumber();
1020080dd10fSScott Constable dbgs() << " }\n";
1021080dd10fSScott Constable dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
1022080dd10fSScott Constable dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
1023080dd10fSScott Constable }
1024080dd10fSScott Constable
1025080dd10fSScott Constable // Add reaching defs of phi uses that are live on exit from this block.
1026080dd10fSScott Constable RefMap &PUs = PhiLOX[B];
1027080dd10fSScott Constable for (auto &S : PUs)
1028080dd10fSScott Constable LiveIn[S.first].insert(S.second.begin(), S.second.end());
1029080dd10fSScott Constable
1030080dd10fSScott Constable if (Trace) {
1031080dd10fSScott Constable dbgs() << "after LOX\n";
1032080dd10fSScott Constable dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
1033080dd10fSScott Constable dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
1034080dd10fSScott Constable }
1035080dd10fSScott Constable
1036080dd10fSScott Constable // The LiveIn map at this point has all defs that are live-on-exit from B,
1037080dd10fSScott Constable // as if they were live-on-entry to B. First, we need to filter out all
1038080dd10fSScott Constable // defs that are present in this block. Then we will add reaching defs of
1039080dd10fSScott Constable // all upward-exposed uses.
1040080dd10fSScott Constable
1041080dd10fSScott Constable // To filter out the defs, first make a copy of LiveIn, and then re-populate
1042080dd10fSScott Constable // LiveIn with the defs that should remain.
1043080dd10fSScott Constable RefMap LiveInCopy = LiveIn;
1044080dd10fSScott Constable LiveIn.clear();
1045080dd10fSScott Constable
1046080dd10fSScott Constable for (const std::pair<const RegisterId, NodeRefSet> &LE : LiveInCopy) {
1047080dd10fSScott Constable RegisterRef LRef(LE.first);
1048080dd10fSScott Constable NodeRefSet &NewDefs = LiveIn[LRef.Reg]; // To be filled.
1049080dd10fSScott Constable const NodeRefSet &OldDefs = LE.second;
1050080dd10fSScott Constable for (NodeRef OR : OldDefs) {
1051080dd10fSScott Constable // R is a def node that was live-on-exit
1052080dd10fSScott Constable auto DA = DFG.addr<DefNode*>(OR.first);
1053080dd10fSScott Constable NodeAddr<InstrNode*> IA = DA.Addr->getOwner(DFG);
1054080dd10fSScott Constable NodeAddr<BlockNode*> BA = IA.Addr->getOwner(DFG);
1055080dd10fSScott Constable if (B != BA.Addr->getCode()) {
1056080dd10fSScott Constable // Defs from a different block need to be preserved. Defs from this
1057080dd10fSScott Constable // block will need to be processed further, except for phi defs, the
1058080dd10fSScott Constable // liveness of which is handled through the PhiLON/PhiLOX maps.
1059080dd10fSScott Constable NewDefs.insert(OR);
1060080dd10fSScott Constable continue;
1061080dd10fSScott Constable }
1062080dd10fSScott Constable
1063080dd10fSScott Constable // Defs from this block need to stop the liveness from being
1064080dd10fSScott Constable // propagated upwards. This only applies to non-preserving defs,
1065080dd10fSScott Constable // and to the parts of the register actually covered by those defs.
1066080dd10fSScott Constable // (Note that phi defs should always be preserving.)
1067080dd10fSScott Constable RegisterAggr RRs(PRI);
1068080dd10fSScott Constable LRef.Mask = OR.second;
1069080dd10fSScott Constable
1070080dd10fSScott Constable if (!DFG.IsPreservingDef(DA)) {
1071080dd10fSScott Constable assert(!(IA.Addr->getFlags() & NodeAttrs::Phi));
1072080dd10fSScott Constable // DA is a non-phi def that is live-on-exit from this block, and
1073080dd10fSScott Constable // that is also located in this block. LRef is a register ref
1074080dd10fSScott Constable // whose use this def reaches. If DA covers LRef, then no part
1075080dd10fSScott Constable // of LRef is exposed upwards.A
1076080dd10fSScott Constable if (RRs.insert(DA.Addr->getRegRef(DFG)).hasCoverOf(LRef))
1077080dd10fSScott Constable continue;
1078080dd10fSScott Constable }
1079080dd10fSScott Constable
1080080dd10fSScott Constable // DA itself was not sufficient to cover LRef. In general, it is
1081080dd10fSScott Constable // the last in a chain of aliased defs before the exit from this block.
1082080dd10fSScott Constable // There could be other defs in this block that are a part of that
1083080dd10fSScott Constable // chain. Check that now: accumulate the registers from these defs,
1084080dd10fSScott Constable // and if they all together cover LRef, it is not live-on-entry.
1085080dd10fSScott Constable for (NodeAddr<DefNode*> TA : getAllReachingDefs(DA)) {
1086080dd10fSScott Constable // DefNode -> InstrNode -> BlockNode.
1087080dd10fSScott Constable NodeAddr<InstrNode*> ITA = TA.Addr->getOwner(DFG);
1088080dd10fSScott Constable NodeAddr<BlockNode*> BTA = ITA.Addr->getOwner(DFG);
1089080dd10fSScott Constable // Reaching defs are ordered in the upward direction.
1090080dd10fSScott Constable if (BTA.Addr->getCode() != B) {
1091080dd10fSScott Constable // We have reached past the beginning of B, and the accumulated
1092080dd10fSScott Constable // registers are not covering LRef. The first def from the
1093080dd10fSScott Constable // upward chain will be live.
1094080dd10fSScott Constable // Subtract all accumulated defs (RRs) from LRef.
1095080dd10fSScott Constable RegisterRef T = RRs.clearIn(LRef);
1096080dd10fSScott Constable assert(T);
1097080dd10fSScott Constable NewDefs.insert({TA.Id,T.Mask});
1098080dd10fSScott Constable break;
1099080dd10fSScott Constable }
1100080dd10fSScott Constable
1101080dd10fSScott Constable // TA is in B. Only add this def to the accumulated cover if it is
1102080dd10fSScott Constable // not preserving.
1103080dd10fSScott Constable if (!(TA.Addr->getFlags() & NodeAttrs::Preserving))
1104080dd10fSScott Constable RRs.insert(TA.Addr->getRegRef(DFG));
1105080dd10fSScott Constable // If this is enough to cover LRef, then stop.
1106080dd10fSScott Constable if (RRs.hasCoverOf(LRef))
1107080dd10fSScott Constable break;
1108080dd10fSScott Constable }
1109080dd10fSScott Constable }
1110080dd10fSScott Constable }
1111080dd10fSScott Constable
1112080dd10fSScott Constable emptify(LiveIn);
1113080dd10fSScott Constable
1114080dd10fSScott Constable if (Trace) {
1115080dd10fSScott Constable dbgs() << "after defs in block\n";
1116080dd10fSScott Constable dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
1117080dd10fSScott Constable dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
1118080dd10fSScott Constable }
1119080dd10fSScott Constable
1120080dd10fSScott Constable // Scan the block for upward-exposed uses and add them to the tracking set.
1121080dd10fSScott Constable for (auto I : DFG.getFunc().Addr->findBlock(B, DFG).Addr->members(DFG)) {
1122080dd10fSScott Constable NodeAddr<InstrNode*> IA = I;
1123080dd10fSScott Constable if (IA.Addr->getKind() != NodeAttrs::Stmt)
1124080dd10fSScott Constable continue;
1125080dd10fSScott Constable for (NodeAddr<UseNode*> UA : IA.Addr->members_if(DFG.IsUse, DFG)) {
1126080dd10fSScott Constable if (UA.Addr->getFlags() & NodeAttrs::Undef)
1127080dd10fSScott Constable continue;
11284b25f672SKrzysztof Parzyszek RegisterRef RR = UA.Addr->getRegRef(DFG);
1129080dd10fSScott Constable for (NodeAddr<DefNode*> D : getAllReachingDefs(UA))
1130080dd10fSScott Constable if (getBlockWithRef(D.Id) != B)
1131080dd10fSScott Constable LiveIn[RR.Reg].insert({D.Id,RR.Mask});
1132080dd10fSScott Constable }
1133080dd10fSScott Constable }
1134080dd10fSScott Constable
1135080dd10fSScott Constable if (Trace) {
1136080dd10fSScott Constable dbgs() << "after uses in block\n";
1137080dd10fSScott Constable dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
1138080dd10fSScott Constable dbgs() << " Local: " << Print<RegisterAggr>(LiveMap[B], DFG) << '\n';
1139080dd10fSScott Constable }
1140080dd10fSScott Constable
1141080dd10fSScott Constable // Phi uses should not be propagated up the dominator tree, since they
1142080dd10fSScott Constable // are not dominated by their corresponding reaching defs.
1143080dd10fSScott Constable RegisterAggr &Local = LiveMap[B];
1144080dd10fSScott Constable RefMap &LON = PhiLON[B];
1145080dd10fSScott Constable for (auto &R : LON) {
1146080dd10fSScott Constable LaneBitmask M;
1147080dd10fSScott Constable for (auto P : R.second)
1148080dd10fSScott Constable M |= P.second;
1149080dd10fSScott Constable Local.insert(RegisterRef(R.first,M));
1150080dd10fSScott Constable }
1151080dd10fSScott Constable
1152080dd10fSScott Constable if (Trace) {
1153080dd10fSScott Constable dbgs() << "after phi uses in block\n";
1154080dd10fSScott Constable dbgs() << " LiveIn: " << Print<RefMap>(LiveIn, DFG) << '\n';
1155080dd10fSScott Constable dbgs() << " Local: " << Print<RegisterAggr>(Local, DFG) << '\n';
1156080dd10fSScott Constable }
1157080dd10fSScott Constable
1158*9e6d1f4bSKazu Hirata for (auto *C : IIDF[B]) {
1159080dd10fSScott Constable RegisterAggr &LiveC = LiveMap[C];
1160080dd10fSScott Constable for (const std::pair<const RegisterId, NodeRefSet> &S : LiveIn)
1161080dd10fSScott Constable for (auto R : S.second)
1162080dd10fSScott Constable if (MDT.properlyDominates(getBlockWithRef(R.first), C))
1163080dd10fSScott Constable LiveC.insert(RegisterRef(S.first, R.second));
1164080dd10fSScott Constable }
1165080dd10fSScott Constable }
1166080dd10fSScott Constable
emptify(RefMap & M)1167080dd10fSScott Constable void Liveness::emptify(RefMap &M) {
1168080dd10fSScott Constable for (auto I = M.begin(), E = M.end(); I != E; )
1169080dd10fSScott Constable I = I->second.empty() ? M.erase(I) : std::next(I);
1170080dd10fSScott Constable }
1171