xref: /aosp_15_r20/external/llvm/lib/Target/Hexagon/RDFGraph.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===--- RDFGraph.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 // Target-independent, SSA-based data flow graph for register data flow (RDF).
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker #include "RDFGraph.h"
13*9880d681SAndroid Build Coastguard Worker 
14*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SetVector.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineBasicBlock.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineDominanceFrontier.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineDominators.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunction.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetInstrInfo.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetRegisterInfo.h"
22*9880d681SAndroid Build Coastguard Worker 
23*9880d681SAndroid Build Coastguard Worker using namespace llvm;
24*9880d681SAndroid Build Coastguard Worker using namespace rdf;
25*9880d681SAndroid Build Coastguard Worker 
26*9880d681SAndroid Build Coastguard Worker // Printing functions. Have them here first, so that the rest of the code
27*9880d681SAndroid Build Coastguard Worker // can use them.
28*9880d681SAndroid Build Coastguard Worker namespace llvm {
29*9880d681SAndroid Build Coastguard Worker namespace rdf {
30*9880d681SAndroid Build Coastguard Worker 
31*9880d681SAndroid Build Coastguard Worker template<>
operator <<(raw_ostream & OS,const Print<RegisterRef> & P)32*9880d681SAndroid Build Coastguard Worker raw_ostream &operator<< (raw_ostream &OS, const Print<RegisterRef> &P) {
33*9880d681SAndroid Build Coastguard Worker   auto &TRI = P.G.getTRI();
34*9880d681SAndroid Build Coastguard Worker   if (P.Obj.Reg > 0 && P.Obj.Reg < TRI.getNumRegs())
35*9880d681SAndroid Build Coastguard Worker     OS << TRI.getName(P.Obj.Reg);
36*9880d681SAndroid Build Coastguard Worker   else
37*9880d681SAndroid Build Coastguard Worker     OS << '#' << P.Obj.Reg;
38*9880d681SAndroid Build Coastguard Worker   if (P.Obj.Sub > 0) {
39*9880d681SAndroid Build Coastguard Worker     OS << ':';
40*9880d681SAndroid Build Coastguard Worker     if (P.Obj.Sub < TRI.getNumSubRegIndices())
41*9880d681SAndroid Build Coastguard Worker       OS << TRI.getSubRegIndexName(P.Obj.Sub);
42*9880d681SAndroid Build Coastguard Worker     else
43*9880d681SAndroid Build Coastguard Worker       OS << '#' << P.Obj.Sub;
44*9880d681SAndroid Build Coastguard Worker   }
45*9880d681SAndroid Build Coastguard Worker   return OS;
46*9880d681SAndroid Build Coastguard Worker }
47*9880d681SAndroid Build Coastguard Worker 
48*9880d681SAndroid Build Coastguard Worker template<>
operator <<(raw_ostream & OS,const Print<NodeId> & P)49*9880d681SAndroid Build Coastguard Worker raw_ostream &operator<< (raw_ostream &OS, const Print<NodeId> &P) {
50*9880d681SAndroid Build Coastguard Worker   auto NA = P.G.addr<NodeBase*>(P.Obj);
51*9880d681SAndroid Build Coastguard Worker   uint16_t Attrs = NA.Addr->getAttrs();
52*9880d681SAndroid Build Coastguard Worker   uint16_t Kind = NodeAttrs::kind(Attrs);
53*9880d681SAndroid Build Coastguard Worker   uint16_t Flags = NodeAttrs::flags(Attrs);
54*9880d681SAndroid Build Coastguard Worker   switch (NodeAttrs::type(Attrs)) {
55*9880d681SAndroid Build Coastguard Worker     case NodeAttrs::Code:
56*9880d681SAndroid Build Coastguard Worker       switch (Kind) {
57*9880d681SAndroid Build Coastguard Worker         case NodeAttrs::Func:   OS << 'f'; break;
58*9880d681SAndroid Build Coastguard Worker         case NodeAttrs::Block:  OS << 'b'; break;
59*9880d681SAndroid Build Coastguard Worker         case NodeAttrs::Stmt:   OS << 's'; break;
60*9880d681SAndroid Build Coastguard Worker         case NodeAttrs::Phi:    OS << 'p'; break;
61*9880d681SAndroid Build Coastguard Worker         default:                OS << "c?"; break;
62*9880d681SAndroid Build Coastguard Worker       }
63*9880d681SAndroid Build Coastguard Worker       break;
64*9880d681SAndroid Build Coastguard Worker     case NodeAttrs::Ref:
65*9880d681SAndroid Build Coastguard Worker       if (Flags & NodeAttrs::Preserving)
66*9880d681SAndroid Build Coastguard Worker         OS << '+';
67*9880d681SAndroid Build Coastguard Worker       if (Flags & NodeAttrs::Clobbering)
68*9880d681SAndroid Build Coastguard Worker         OS << '~';
69*9880d681SAndroid Build Coastguard Worker       switch (Kind) {
70*9880d681SAndroid Build Coastguard Worker         case NodeAttrs::Use:    OS << 'u'; break;
71*9880d681SAndroid Build Coastguard Worker         case NodeAttrs::Def:    OS << 'd'; break;
72*9880d681SAndroid Build Coastguard Worker         case NodeAttrs::Block:  OS << 'b'; break;
73*9880d681SAndroid Build Coastguard Worker         default:                OS << "r?"; break;
74*9880d681SAndroid Build Coastguard Worker       }
75*9880d681SAndroid Build Coastguard Worker       break;
76*9880d681SAndroid Build Coastguard Worker     default:
77*9880d681SAndroid Build Coastguard Worker       OS << '?';
78*9880d681SAndroid Build Coastguard Worker       break;
79*9880d681SAndroid Build Coastguard Worker   }
80*9880d681SAndroid Build Coastguard Worker   OS << P.Obj;
81*9880d681SAndroid Build Coastguard Worker   if (Flags & NodeAttrs::Shadow)
82*9880d681SAndroid Build Coastguard Worker     OS << '"';
83*9880d681SAndroid Build Coastguard Worker   return OS;
84*9880d681SAndroid Build Coastguard Worker }
85*9880d681SAndroid Build Coastguard Worker 
86*9880d681SAndroid Build Coastguard Worker namespace {
printRefHeader(raw_ostream & OS,const NodeAddr<RefNode * > RA,const DataFlowGraph & G)87*9880d681SAndroid Build Coastguard Worker   void printRefHeader(raw_ostream &OS, const NodeAddr<RefNode*> RA,
88*9880d681SAndroid Build Coastguard Worker         const DataFlowGraph &G) {
89*9880d681SAndroid Build Coastguard Worker     OS << Print<NodeId>(RA.Id, G) << '<'
90*9880d681SAndroid Build Coastguard Worker        << Print<RegisterRef>(RA.Addr->getRegRef(), G) << '>';
91*9880d681SAndroid Build Coastguard Worker     if (RA.Addr->getFlags() & NodeAttrs::Fixed)
92*9880d681SAndroid Build Coastguard Worker       OS << '!';
93*9880d681SAndroid Build Coastguard Worker   }
94*9880d681SAndroid Build Coastguard Worker }
95*9880d681SAndroid Build Coastguard Worker 
96*9880d681SAndroid Build Coastguard Worker template<>
operator <<(raw_ostream & OS,const Print<NodeAddr<DefNode * >> & P)97*9880d681SAndroid Build Coastguard Worker raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<DefNode*>> &P) {
98*9880d681SAndroid Build Coastguard Worker   printRefHeader(OS, P.Obj, P.G);
99*9880d681SAndroid Build Coastguard Worker   OS << '(';
100*9880d681SAndroid Build Coastguard Worker   if (NodeId N = P.Obj.Addr->getReachingDef())
101*9880d681SAndroid Build Coastguard Worker     OS << Print<NodeId>(N, P.G);
102*9880d681SAndroid Build Coastguard Worker   OS << ',';
103*9880d681SAndroid Build Coastguard Worker   if (NodeId N = P.Obj.Addr->getReachedDef())
104*9880d681SAndroid Build Coastguard Worker     OS << Print<NodeId>(N, P.G);
105*9880d681SAndroid Build Coastguard Worker   OS << ',';
106*9880d681SAndroid Build Coastguard Worker   if (NodeId N = P.Obj.Addr->getReachedUse())
107*9880d681SAndroid Build Coastguard Worker     OS << Print<NodeId>(N, P.G);
108*9880d681SAndroid Build Coastguard Worker   OS << "):";
109*9880d681SAndroid Build Coastguard Worker   if (NodeId N = P.Obj.Addr->getSibling())
110*9880d681SAndroid Build Coastguard Worker     OS << Print<NodeId>(N, P.G);
111*9880d681SAndroid Build Coastguard Worker   return OS;
112*9880d681SAndroid Build Coastguard Worker }
113*9880d681SAndroid Build Coastguard Worker 
114*9880d681SAndroid Build Coastguard Worker template<>
operator <<(raw_ostream & OS,const Print<NodeAddr<UseNode * >> & P)115*9880d681SAndroid Build Coastguard Worker raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<UseNode*>> &P) {
116*9880d681SAndroid Build Coastguard Worker   printRefHeader(OS, P.Obj, P.G);
117*9880d681SAndroid Build Coastguard Worker   OS << '(';
118*9880d681SAndroid Build Coastguard Worker   if (NodeId N = P.Obj.Addr->getReachingDef())
119*9880d681SAndroid Build Coastguard Worker     OS << Print<NodeId>(N, P.G);
120*9880d681SAndroid Build Coastguard Worker   OS << "):";
121*9880d681SAndroid Build Coastguard Worker   if (NodeId N = P.Obj.Addr->getSibling())
122*9880d681SAndroid Build Coastguard Worker     OS << Print<NodeId>(N, P.G);
123*9880d681SAndroid Build Coastguard Worker   return OS;
124*9880d681SAndroid Build Coastguard Worker }
125*9880d681SAndroid Build Coastguard Worker 
126*9880d681SAndroid Build Coastguard Worker template<>
operator <<(raw_ostream & OS,const Print<NodeAddr<PhiUseNode * >> & P)127*9880d681SAndroid Build Coastguard Worker raw_ostream &operator<< (raw_ostream &OS,
128*9880d681SAndroid Build Coastguard Worker       const Print<NodeAddr<PhiUseNode*>> &P) {
129*9880d681SAndroid Build Coastguard Worker   printRefHeader(OS, P.Obj, P.G);
130*9880d681SAndroid Build Coastguard Worker   OS << '(';
131*9880d681SAndroid Build Coastguard Worker   if (NodeId N = P.Obj.Addr->getReachingDef())
132*9880d681SAndroid Build Coastguard Worker     OS << Print<NodeId>(N, P.G);
133*9880d681SAndroid Build Coastguard Worker   OS << ',';
134*9880d681SAndroid Build Coastguard Worker   if (NodeId N = P.Obj.Addr->getPredecessor())
135*9880d681SAndroid Build Coastguard Worker     OS << Print<NodeId>(N, P.G);
136*9880d681SAndroid Build Coastguard Worker   OS << "):";
137*9880d681SAndroid Build Coastguard Worker   if (NodeId N = P.Obj.Addr->getSibling())
138*9880d681SAndroid Build Coastguard Worker     OS << Print<NodeId>(N, P.G);
139*9880d681SAndroid Build Coastguard Worker   return OS;
140*9880d681SAndroid Build Coastguard Worker }
141*9880d681SAndroid Build Coastguard Worker 
142*9880d681SAndroid Build Coastguard Worker template<>
operator <<(raw_ostream & OS,const Print<NodeAddr<RefNode * >> & P)143*9880d681SAndroid Build Coastguard Worker raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<RefNode*>> &P) {
144*9880d681SAndroid Build Coastguard Worker   switch (P.Obj.Addr->getKind()) {
145*9880d681SAndroid Build Coastguard Worker     case NodeAttrs::Def:
146*9880d681SAndroid Build Coastguard Worker       OS << PrintNode<DefNode*>(P.Obj, P.G);
147*9880d681SAndroid Build Coastguard Worker       break;
148*9880d681SAndroid Build Coastguard Worker     case NodeAttrs::Use:
149*9880d681SAndroid Build Coastguard Worker       if (P.Obj.Addr->getFlags() & NodeAttrs::PhiRef)
150*9880d681SAndroid Build Coastguard Worker         OS << PrintNode<PhiUseNode*>(P.Obj, P.G);
151*9880d681SAndroid Build Coastguard Worker       else
152*9880d681SAndroid Build Coastguard Worker         OS << PrintNode<UseNode*>(P.Obj, P.G);
153*9880d681SAndroid Build Coastguard Worker       break;
154*9880d681SAndroid Build Coastguard Worker   }
155*9880d681SAndroid Build Coastguard Worker   return OS;
156*9880d681SAndroid Build Coastguard Worker }
157*9880d681SAndroid Build Coastguard Worker 
158*9880d681SAndroid Build Coastguard Worker template<>
operator <<(raw_ostream & OS,const Print<NodeList> & P)159*9880d681SAndroid Build Coastguard Worker raw_ostream &operator<< (raw_ostream &OS, const Print<NodeList> &P) {
160*9880d681SAndroid Build Coastguard Worker   unsigned N = P.Obj.size();
161*9880d681SAndroid Build Coastguard Worker   for (auto I : P.Obj) {
162*9880d681SAndroid Build Coastguard Worker     OS << Print<NodeId>(I.Id, P.G);
163*9880d681SAndroid Build Coastguard Worker     if (--N)
164*9880d681SAndroid Build Coastguard Worker       OS << ' ';
165*9880d681SAndroid Build Coastguard Worker   }
166*9880d681SAndroid Build Coastguard Worker   return OS;
167*9880d681SAndroid Build Coastguard Worker }
168*9880d681SAndroid Build Coastguard Worker 
169*9880d681SAndroid Build Coastguard Worker template<>
operator <<(raw_ostream & OS,const Print<NodeSet> & P)170*9880d681SAndroid Build Coastguard Worker raw_ostream &operator<< (raw_ostream &OS, const Print<NodeSet> &P) {
171*9880d681SAndroid Build Coastguard Worker   unsigned N = P.Obj.size();
172*9880d681SAndroid Build Coastguard Worker   for (auto I : P.Obj) {
173*9880d681SAndroid Build Coastguard Worker     OS << Print<NodeId>(I, P.G);
174*9880d681SAndroid Build Coastguard Worker     if (--N)
175*9880d681SAndroid Build Coastguard Worker       OS << ' ';
176*9880d681SAndroid Build Coastguard Worker   }
177*9880d681SAndroid Build Coastguard Worker   return OS;
178*9880d681SAndroid Build Coastguard Worker }
179*9880d681SAndroid Build Coastguard Worker 
180*9880d681SAndroid Build Coastguard Worker namespace {
181*9880d681SAndroid Build Coastguard Worker   template <typename T>
182*9880d681SAndroid Build Coastguard Worker   struct PrintListV {
PrintListVllvm::rdf::__anon9a45e4e50211::PrintListV183*9880d681SAndroid Build Coastguard Worker     PrintListV(const NodeList &L, const DataFlowGraph &G) : List(L), G(G) {}
184*9880d681SAndroid Build Coastguard Worker     typedef T Type;
185*9880d681SAndroid Build Coastguard Worker     const NodeList &List;
186*9880d681SAndroid Build Coastguard Worker     const DataFlowGraph &G;
187*9880d681SAndroid Build Coastguard Worker   };
188*9880d681SAndroid Build Coastguard Worker 
189*9880d681SAndroid Build Coastguard Worker   template <typename T>
operator <<(raw_ostream & OS,const PrintListV<T> & P)190*9880d681SAndroid Build Coastguard Worker   raw_ostream &operator<< (raw_ostream &OS, const PrintListV<T> &P) {
191*9880d681SAndroid Build Coastguard Worker     unsigned N = P.List.size();
192*9880d681SAndroid Build Coastguard Worker     for (NodeAddr<T> A : P.List) {
193*9880d681SAndroid Build Coastguard Worker       OS << PrintNode<T>(A, P.G);
194*9880d681SAndroid Build Coastguard Worker       if (--N)
195*9880d681SAndroid Build Coastguard Worker         OS << ", ";
196*9880d681SAndroid Build Coastguard Worker     }
197*9880d681SAndroid Build Coastguard Worker     return OS;
198*9880d681SAndroid Build Coastguard Worker   }
199*9880d681SAndroid Build Coastguard Worker }
200*9880d681SAndroid Build Coastguard Worker 
201*9880d681SAndroid Build Coastguard Worker template<>
operator <<(raw_ostream & OS,const Print<NodeAddr<PhiNode * >> & P)202*9880d681SAndroid Build Coastguard Worker raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<PhiNode*>> &P) {
203*9880d681SAndroid Build Coastguard Worker   OS << Print<NodeId>(P.Obj.Id, P.G) << ": phi ["
204*9880d681SAndroid Build Coastguard Worker      << PrintListV<RefNode*>(P.Obj.Addr->members(P.G), P.G) << ']';
205*9880d681SAndroid Build Coastguard Worker   return OS;
206*9880d681SAndroid Build Coastguard Worker }
207*9880d681SAndroid Build Coastguard Worker 
208*9880d681SAndroid Build Coastguard Worker template<>
operator <<(raw_ostream & OS,const Print<NodeAddr<StmtNode * >> & P)209*9880d681SAndroid Build Coastguard Worker raw_ostream &operator<< (raw_ostream &OS,
210*9880d681SAndroid Build Coastguard Worker       const Print<NodeAddr<StmtNode*>> &P) {
211*9880d681SAndroid Build Coastguard Worker   unsigned Opc = P.Obj.Addr->getCode()->getOpcode();
212*9880d681SAndroid Build Coastguard Worker   OS << Print<NodeId>(P.Obj.Id, P.G) << ": " << P.G.getTII().getName(Opc)
213*9880d681SAndroid Build Coastguard Worker      << " [" << PrintListV<RefNode*>(P.Obj.Addr->members(P.G), P.G) << ']';
214*9880d681SAndroid Build Coastguard Worker   return OS;
215*9880d681SAndroid Build Coastguard Worker }
216*9880d681SAndroid Build Coastguard Worker 
217*9880d681SAndroid Build Coastguard Worker template<>
operator <<(raw_ostream & OS,const Print<NodeAddr<InstrNode * >> & P)218*9880d681SAndroid Build Coastguard Worker raw_ostream &operator<< (raw_ostream &OS,
219*9880d681SAndroid Build Coastguard Worker       const Print<NodeAddr<InstrNode*>> &P) {
220*9880d681SAndroid Build Coastguard Worker   switch (P.Obj.Addr->getKind()) {
221*9880d681SAndroid Build Coastguard Worker     case NodeAttrs::Phi:
222*9880d681SAndroid Build Coastguard Worker       OS << PrintNode<PhiNode*>(P.Obj, P.G);
223*9880d681SAndroid Build Coastguard Worker       break;
224*9880d681SAndroid Build Coastguard Worker     case NodeAttrs::Stmt:
225*9880d681SAndroid Build Coastguard Worker       OS << PrintNode<StmtNode*>(P.Obj, P.G);
226*9880d681SAndroid Build Coastguard Worker       break;
227*9880d681SAndroid Build Coastguard Worker     default:
228*9880d681SAndroid Build Coastguard Worker       OS << "instr? " << Print<NodeId>(P.Obj.Id, P.G);
229*9880d681SAndroid Build Coastguard Worker       break;
230*9880d681SAndroid Build Coastguard Worker   }
231*9880d681SAndroid Build Coastguard Worker   return OS;
232*9880d681SAndroid Build Coastguard Worker }
233*9880d681SAndroid Build Coastguard Worker 
234*9880d681SAndroid Build Coastguard Worker template<>
operator <<(raw_ostream & OS,const Print<NodeAddr<BlockNode * >> & P)235*9880d681SAndroid Build Coastguard Worker raw_ostream &operator<< (raw_ostream &OS,
236*9880d681SAndroid Build Coastguard Worker       const Print<NodeAddr<BlockNode*>> &P) {
237*9880d681SAndroid Build Coastguard Worker   auto *BB = P.Obj.Addr->getCode();
238*9880d681SAndroid Build Coastguard Worker   unsigned NP = BB->pred_size();
239*9880d681SAndroid Build Coastguard Worker   std::vector<int> Ns;
240*9880d681SAndroid Build Coastguard Worker   auto PrintBBs = [&OS,&P] (std::vector<int> Ns) -> void {
241*9880d681SAndroid Build Coastguard Worker     unsigned N = Ns.size();
242*9880d681SAndroid Build Coastguard Worker     for (auto I : Ns) {
243*9880d681SAndroid Build Coastguard Worker       OS << "BB#" << I;
244*9880d681SAndroid Build Coastguard Worker       if (--N)
245*9880d681SAndroid Build Coastguard Worker         OS << ", ";
246*9880d681SAndroid Build Coastguard Worker     }
247*9880d681SAndroid Build Coastguard Worker   };
248*9880d681SAndroid Build Coastguard Worker 
249*9880d681SAndroid Build Coastguard Worker   OS << Print<NodeId>(P.Obj.Id, P.G) << ": === BB#" << BB->getNumber()
250*9880d681SAndroid Build Coastguard Worker      << " === preds(" << NP << "): ";
251*9880d681SAndroid Build Coastguard Worker   for (auto I : BB->predecessors())
252*9880d681SAndroid Build Coastguard Worker     Ns.push_back(I->getNumber());
253*9880d681SAndroid Build Coastguard Worker   PrintBBs(Ns);
254*9880d681SAndroid Build Coastguard Worker 
255*9880d681SAndroid Build Coastguard Worker   unsigned NS = BB->succ_size();
256*9880d681SAndroid Build Coastguard Worker   OS << "  succs(" << NS << "): ";
257*9880d681SAndroid Build Coastguard Worker   Ns.clear();
258*9880d681SAndroid Build Coastguard Worker   for (auto I : BB->successors())
259*9880d681SAndroid Build Coastguard Worker     Ns.push_back(I->getNumber());
260*9880d681SAndroid Build Coastguard Worker   PrintBBs(Ns);
261*9880d681SAndroid Build Coastguard Worker   OS << '\n';
262*9880d681SAndroid Build Coastguard Worker 
263*9880d681SAndroid Build Coastguard Worker   for (auto I : P.Obj.Addr->members(P.G))
264*9880d681SAndroid Build Coastguard Worker     OS << PrintNode<InstrNode*>(I, P.G) << '\n';
265*9880d681SAndroid Build Coastguard Worker   return OS;
266*9880d681SAndroid Build Coastguard Worker }
267*9880d681SAndroid Build Coastguard Worker 
268*9880d681SAndroid Build Coastguard Worker template<>
operator <<(raw_ostream & OS,const Print<NodeAddr<FuncNode * >> & P)269*9880d681SAndroid Build Coastguard Worker raw_ostream &operator<< (raw_ostream &OS,
270*9880d681SAndroid Build Coastguard Worker       const Print<NodeAddr<FuncNode*>> &P) {
271*9880d681SAndroid Build Coastguard Worker   OS << "DFG dump:[\n" << Print<NodeId>(P.Obj.Id, P.G) << ": Function: "
272*9880d681SAndroid Build Coastguard Worker      << P.Obj.Addr->getCode()->getName() << '\n';
273*9880d681SAndroid Build Coastguard Worker   for (auto I : P.Obj.Addr->members(P.G))
274*9880d681SAndroid Build Coastguard Worker     OS << PrintNode<BlockNode*>(I, P.G) << '\n';
275*9880d681SAndroid Build Coastguard Worker   OS << "]\n";
276*9880d681SAndroid Build Coastguard Worker   return OS;
277*9880d681SAndroid Build Coastguard Worker }
278*9880d681SAndroid Build Coastguard Worker 
279*9880d681SAndroid Build Coastguard Worker template<>
operator <<(raw_ostream & OS,const Print<RegisterSet> & P)280*9880d681SAndroid Build Coastguard Worker raw_ostream &operator<< (raw_ostream &OS, const Print<RegisterSet> &P) {
281*9880d681SAndroid Build Coastguard Worker   OS << '{';
282*9880d681SAndroid Build Coastguard Worker   for (auto I : P.Obj)
283*9880d681SAndroid Build Coastguard Worker     OS << ' ' << Print<RegisterRef>(I, P.G);
284*9880d681SAndroid Build Coastguard Worker   OS << " }";
285*9880d681SAndroid Build Coastguard Worker   return OS;
286*9880d681SAndroid Build Coastguard Worker }
287*9880d681SAndroid Build Coastguard Worker 
288*9880d681SAndroid Build Coastguard Worker template<>
operator <<(raw_ostream & OS,const Print<DataFlowGraph::DefStack> & P)289*9880d681SAndroid Build Coastguard Worker raw_ostream &operator<< (raw_ostream &OS,
290*9880d681SAndroid Build Coastguard Worker       const Print<DataFlowGraph::DefStack> &P) {
291*9880d681SAndroid Build Coastguard Worker   for (auto I = P.Obj.top(), E = P.Obj.bottom(); I != E; ) {
292*9880d681SAndroid Build Coastguard Worker     OS << Print<NodeId>(I->Id, P.G)
293*9880d681SAndroid Build Coastguard Worker        << '<' << Print<RegisterRef>(I->Addr->getRegRef(), P.G) << '>';
294*9880d681SAndroid Build Coastguard Worker     I.down();
295*9880d681SAndroid Build Coastguard Worker     if (I != E)
296*9880d681SAndroid Build Coastguard Worker       OS << ' ';
297*9880d681SAndroid Build Coastguard Worker   }
298*9880d681SAndroid Build Coastguard Worker   return OS;
299*9880d681SAndroid Build Coastguard Worker }
300*9880d681SAndroid Build Coastguard Worker 
301*9880d681SAndroid Build Coastguard Worker } // namespace rdf
302*9880d681SAndroid Build Coastguard Worker } // namespace llvm
303*9880d681SAndroid Build Coastguard Worker 
304*9880d681SAndroid Build Coastguard Worker // Node allocation functions.
305*9880d681SAndroid Build Coastguard Worker //
306*9880d681SAndroid Build Coastguard Worker // Node allocator is like a slab memory allocator: it allocates blocks of
307*9880d681SAndroid Build Coastguard Worker // memory in sizes that are multiples of the size of a node. Each block has
308*9880d681SAndroid Build Coastguard Worker // the same size. Nodes are allocated from the currently active block, and
309*9880d681SAndroid Build Coastguard Worker // when it becomes full, a new one is created.
310*9880d681SAndroid Build Coastguard Worker // There is a mapping scheme between node id and its location in a block,
311*9880d681SAndroid Build Coastguard Worker // and within that block is described in the header file.
312*9880d681SAndroid Build Coastguard Worker //
startNewBlock()313*9880d681SAndroid Build Coastguard Worker void NodeAllocator::startNewBlock() {
314*9880d681SAndroid Build Coastguard Worker   void *T = MemPool.Allocate(NodesPerBlock*NodeMemSize, NodeMemSize);
315*9880d681SAndroid Build Coastguard Worker   char *P = static_cast<char*>(T);
316*9880d681SAndroid Build Coastguard Worker   Blocks.push_back(P);
317*9880d681SAndroid Build Coastguard Worker   // Check if the block index is still within the allowed range, i.e. less
318*9880d681SAndroid Build Coastguard Worker   // than 2^N, where N is the number of bits in NodeId for the block index.
319*9880d681SAndroid Build Coastguard Worker   // BitsPerIndex is the number of bits per node index.
320*9880d681SAndroid Build Coastguard Worker   assert((Blocks.size() < ((size_t)1 << (8*sizeof(NodeId)-BitsPerIndex))) &&
321*9880d681SAndroid Build Coastguard Worker          "Out of bits for block index");
322*9880d681SAndroid Build Coastguard Worker   ActiveEnd = P;
323*9880d681SAndroid Build Coastguard Worker }
324*9880d681SAndroid Build Coastguard Worker 
needNewBlock()325*9880d681SAndroid Build Coastguard Worker bool NodeAllocator::needNewBlock() {
326*9880d681SAndroid Build Coastguard Worker   if (Blocks.empty())
327*9880d681SAndroid Build Coastguard Worker     return true;
328*9880d681SAndroid Build Coastguard Worker 
329*9880d681SAndroid Build Coastguard Worker   char *ActiveBegin = Blocks.back();
330*9880d681SAndroid Build Coastguard Worker   uint32_t Index = (ActiveEnd-ActiveBegin)/NodeMemSize;
331*9880d681SAndroid Build Coastguard Worker   return Index >= NodesPerBlock;
332*9880d681SAndroid Build Coastguard Worker }
333*9880d681SAndroid Build Coastguard Worker 
New()334*9880d681SAndroid Build Coastguard Worker NodeAddr<NodeBase*> NodeAllocator::New() {
335*9880d681SAndroid Build Coastguard Worker   if (needNewBlock())
336*9880d681SAndroid Build Coastguard Worker     startNewBlock();
337*9880d681SAndroid Build Coastguard Worker 
338*9880d681SAndroid Build Coastguard Worker   uint32_t ActiveB = Blocks.size()-1;
339*9880d681SAndroid Build Coastguard Worker   uint32_t Index = (ActiveEnd - Blocks[ActiveB])/NodeMemSize;
340*9880d681SAndroid Build Coastguard Worker   NodeAddr<NodeBase*> NA = { reinterpret_cast<NodeBase*>(ActiveEnd),
341*9880d681SAndroid Build Coastguard Worker                              makeId(ActiveB, Index) };
342*9880d681SAndroid Build Coastguard Worker   ActiveEnd += NodeMemSize;
343*9880d681SAndroid Build Coastguard Worker   return NA;
344*9880d681SAndroid Build Coastguard Worker }
345*9880d681SAndroid Build Coastguard Worker 
id(const NodeBase * P) const346*9880d681SAndroid Build Coastguard Worker NodeId NodeAllocator::id(const NodeBase *P) const {
347*9880d681SAndroid Build Coastguard Worker   uintptr_t A = reinterpret_cast<uintptr_t>(P);
348*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, n = Blocks.size(); i != n; ++i) {
349*9880d681SAndroid Build Coastguard Worker     uintptr_t B = reinterpret_cast<uintptr_t>(Blocks[i]);
350*9880d681SAndroid Build Coastguard Worker     if (A < B || A >= B + NodesPerBlock*NodeMemSize)
351*9880d681SAndroid Build Coastguard Worker       continue;
352*9880d681SAndroid Build Coastguard Worker     uint32_t Idx = (A-B)/NodeMemSize;
353*9880d681SAndroid Build Coastguard Worker     return makeId(i, Idx);
354*9880d681SAndroid Build Coastguard Worker   }
355*9880d681SAndroid Build Coastguard Worker   llvm_unreachable("Invalid node address");
356*9880d681SAndroid Build Coastguard Worker }
357*9880d681SAndroid Build Coastguard Worker 
clear()358*9880d681SAndroid Build Coastguard Worker void NodeAllocator::clear() {
359*9880d681SAndroid Build Coastguard Worker   MemPool.Reset();
360*9880d681SAndroid Build Coastguard Worker   Blocks.clear();
361*9880d681SAndroid Build Coastguard Worker   ActiveEnd = nullptr;
362*9880d681SAndroid Build Coastguard Worker }
363*9880d681SAndroid Build Coastguard Worker 
364*9880d681SAndroid Build Coastguard Worker 
365*9880d681SAndroid Build Coastguard Worker // Insert node NA after "this" in the circular chain.
append(NodeAddr<NodeBase * > NA)366*9880d681SAndroid Build Coastguard Worker void NodeBase::append(NodeAddr<NodeBase*> NA) {
367*9880d681SAndroid Build Coastguard Worker   NodeId Nx = Next;
368*9880d681SAndroid Build Coastguard Worker   // If NA is already "next", do nothing.
369*9880d681SAndroid Build Coastguard Worker   if (Next != NA.Id) {
370*9880d681SAndroid Build Coastguard Worker     Next = NA.Id;
371*9880d681SAndroid Build Coastguard Worker     NA.Addr->Next = Nx;
372*9880d681SAndroid Build Coastguard Worker   }
373*9880d681SAndroid Build Coastguard Worker }
374*9880d681SAndroid Build Coastguard Worker 
375*9880d681SAndroid Build Coastguard Worker 
376*9880d681SAndroid Build Coastguard Worker // Fundamental node manipulator functions.
377*9880d681SAndroid Build Coastguard Worker 
378*9880d681SAndroid Build Coastguard Worker // Obtain the register reference from a reference node.
getRegRef() const379*9880d681SAndroid Build Coastguard Worker RegisterRef RefNode::getRegRef() const {
380*9880d681SAndroid Build Coastguard Worker   assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref);
381*9880d681SAndroid Build Coastguard Worker   if (NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef)
382*9880d681SAndroid Build Coastguard Worker     return Ref.RR;
383*9880d681SAndroid Build Coastguard Worker   assert(Ref.Op != nullptr);
384*9880d681SAndroid Build Coastguard Worker   return { Ref.Op->getReg(), Ref.Op->getSubReg() };
385*9880d681SAndroid Build Coastguard Worker }
386*9880d681SAndroid Build Coastguard Worker 
387*9880d681SAndroid Build Coastguard Worker // Set the register reference in the reference node directly (for references
388*9880d681SAndroid Build Coastguard Worker // in phi nodes).
setRegRef(RegisterRef RR)389*9880d681SAndroid Build Coastguard Worker void RefNode::setRegRef(RegisterRef RR) {
390*9880d681SAndroid Build Coastguard Worker   assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref);
391*9880d681SAndroid Build Coastguard Worker   assert(NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef);
392*9880d681SAndroid Build Coastguard Worker   Ref.RR = RR;
393*9880d681SAndroid Build Coastguard Worker }
394*9880d681SAndroid Build Coastguard Worker 
395*9880d681SAndroid Build Coastguard Worker // Set the register reference in the reference node based on a machine
396*9880d681SAndroid Build Coastguard Worker // operand (for references in statement nodes).
setRegRef(MachineOperand * Op)397*9880d681SAndroid Build Coastguard Worker void RefNode::setRegRef(MachineOperand *Op) {
398*9880d681SAndroid Build Coastguard Worker   assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref);
399*9880d681SAndroid Build Coastguard Worker   assert(!(NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef));
400*9880d681SAndroid Build Coastguard Worker   Ref.Op = Op;
401*9880d681SAndroid Build Coastguard Worker }
402*9880d681SAndroid Build Coastguard Worker 
403*9880d681SAndroid Build Coastguard Worker // Get the owner of a given reference node.
getOwner(const DataFlowGraph & G)404*9880d681SAndroid Build Coastguard Worker NodeAddr<NodeBase*> RefNode::getOwner(const DataFlowGraph &G) {
405*9880d681SAndroid Build Coastguard Worker   NodeAddr<NodeBase*> NA = G.addr<NodeBase*>(getNext());
406*9880d681SAndroid Build Coastguard Worker 
407*9880d681SAndroid Build Coastguard Worker   while (NA.Addr != this) {
408*9880d681SAndroid Build Coastguard Worker     if (NA.Addr->getType() == NodeAttrs::Code)
409*9880d681SAndroid Build Coastguard Worker       return NA;
410*9880d681SAndroid Build Coastguard Worker     NA = G.addr<NodeBase*>(NA.Addr->getNext());
411*9880d681SAndroid Build Coastguard Worker   }
412*9880d681SAndroid Build Coastguard Worker   llvm_unreachable("No owner in circular list");
413*9880d681SAndroid Build Coastguard Worker }
414*9880d681SAndroid Build Coastguard Worker 
415*9880d681SAndroid Build Coastguard Worker // Connect the def node to the reaching def node.
linkToDef(NodeId Self,NodeAddr<DefNode * > DA)416*9880d681SAndroid Build Coastguard Worker void DefNode::linkToDef(NodeId Self, NodeAddr<DefNode*> DA) {
417*9880d681SAndroid Build Coastguard Worker   Ref.RD = DA.Id;
418*9880d681SAndroid Build Coastguard Worker   Ref.Sib = DA.Addr->getReachedDef();
419*9880d681SAndroid Build Coastguard Worker   DA.Addr->setReachedDef(Self);
420*9880d681SAndroid Build Coastguard Worker }
421*9880d681SAndroid Build Coastguard Worker 
422*9880d681SAndroid Build Coastguard Worker // Connect the use node to the reaching def node.
linkToDef(NodeId Self,NodeAddr<DefNode * > DA)423*9880d681SAndroid Build Coastguard Worker void UseNode::linkToDef(NodeId Self, NodeAddr<DefNode*> DA) {
424*9880d681SAndroid Build Coastguard Worker   Ref.RD = DA.Id;
425*9880d681SAndroid Build Coastguard Worker   Ref.Sib = DA.Addr->getReachedUse();
426*9880d681SAndroid Build Coastguard Worker   DA.Addr->setReachedUse(Self);
427*9880d681SAndroid Build Coastguard Worker }
428*9880d681SAndroid Build Coastguard Worker 
429*9880d681SAndroid Build Coastguard Worker // Get the first member of the code node.
getFirstMember(const DataFlowGraph & G) const430*9880d681SAndroid Build Coastguard Worker NodeAddr<NodeBase*> CodeNode::getFirstMember(const DataFlowGraph &G) const {
431*9880d681SAndroid Build Coastguard Worker   if (Code.FirstM == 0)
432*9880d681SAndroid Build Coastguard Worker     return NodeAddr<NodeBase*>();
433*9880d681SAndroid Build Coastguard Worker   return G.addr<NodeBase*>(Code.FirstM);
434*9880d681SAndroid Build Coastguard Worker }
435*9880d681SAndroid Build Coastguard Worker 
436*9880d681SAndroid Build Coastguard Worker // Get the last member of the code node.
getLastMember(const DataFlowGraph & G) const437*9880d681SAndroid Build Coastguard Worker NodeAddr<NodeBase*> CodeNode::getLastMember(const DataFlowGraph &G) const {
438*9880d681SAndroid Build Coastguard Worker   if (Code.LastM == 0)
439*9880d681SAndroid Build Coastguard Worker     return NodeAddr<NodeBase*>();
440*9880d681SAndroid Build Coastguard Worker   return G.addr<NodeBase*>(Code.LastM);
441*9880d681SAndroid Build Coastguard Worker }
442*9880d681SAndroid Build Coastguard Worker 
443*9880d681SAndroid Build Coastguard Worker // Add node NA at the end of the member list of the given code node.
addMember(NodeAddr<NodeBase * > NA,const DataFlowGraph & G)444*9880d681SAndroid Build Coastguard Worker void CodeNode::addMember(NodeAddr<NodeBase*> NA, const DataFlowGraph &G) {
445*9880d681SAndroid Build Coastguard Worker   auto ML = getLastMember(G);
446*9880d681SAndroid Build Coastguard Worker   if (ML.Id != 0) {
447*9880d681SAndroid Build Coastguard Worker     ML.Addr->append(NA);
448*9880d681SAndroid Build Coastguard Worker   } else {
449*9880d681SAndroid Build Coastguard Worker     Code.FirstM = NA.Id;
450*9880d681SAndroid Build Coastguard Worker     NodeId Self = G.id(this);
451*9880d681SAndroid Build Coastguard Worker     NA.Addr->setNext(Self);
452*9880d681SAndroid Build Coastguard Worker   }
453*9880d681SAndroid Build Coastguard Worker   Code.LastM = NA.Id;
454*9880d681SAndroid Build Coastguard Worker }
455*9880d681SAndroid Build Coastguard Worker 
456*9880d681SAndroid Build Coastguard Worker // Add node NA after member node MA in the given code node.
addMemberAfter(NodeAddr<NodeBase * > MA,NodeAddr<NodeBase * > NA,const DataFlowGraph & G)457*9880d681SAndroid Build Coastguard Worker void CodeNode::addMemberAfter(NodeAddr<NodeBase*> MA, NodeAddr<NodeBase*> NA,
458*9880d681SAndroid Build Coastguard Worker       const DataFlowGraph &G) {
459*9880d681SAndroid Build Coastguard Worker   MA.Addr->append(NA);
460*9880d681SAndroid Build Coastguard Worker   if (Code.LastM == MA.Id)
461*9880d681SAndroid Build Coastguard Worker     Code.LastM = NA.Id;
462*9880d681SAndroid Build Coastguard Worker }
463*9880d681SAndroid Build Coastguard Worker 
464*9880d681SAndroid Build Coastguard Worker // Remove member node NA from the given code node.
removeMember(NodeAddr<NodeBase * > NA,const DataFlowGraph & G)465*9880d681SAndroid Build Coastguard Worker void CodeNode::removeMember(NodeAddr<NodeBase*> NA, const DataFlowGraph &G) {
466*9880d681SAndroid Build Coastguard Worker   auto MA = getFirstMember(G);
467*9880d681SAndroid Build Coastguard Worker   assert(MA.Id != 0);
468*9880d681SAndroid Build Coastguard Worker 
469*9880d681SAndroid Build Coastguard Worker   // Special handling if the member to remove is the first member.
470*9880d681SAndroid Build Coastguard Worker   if (MA.Id == NA.Id) {
471*9880d681SAndroid Build Coastguard Worker     if (Code.LastM == MA.Id) {
472*9880d681SAndroid Build Coastguard Worker       // If it is the only member, set both first and last to 0.
473*9880d681SAndroid Build Coastguard Worker       Code.FirstM = Code.LastM = 0;
474*9880d681SAndroid Build Coastguard Worker     } else {
475*9880d681SAndroid Build Coastguard Worker       // Otherwise, advance the first member.
476*9880d681SAndroid Build Coastguard Worker       Code.FirstM = MA.Addr->getNext();
477*9880d681SAndroid Build Coastguard Worker     }
478*9880d681SAndroid Build Coastguard Worker     return;
479*9880d681SAndroid Build Coastguard Worker   }
480*9880d681SAndroid Build Coastguard Worker 
481*9880d681SAndroid Build Coastguard Worker   while (MA.Addr != this) {
482*9880d681SAndroid Build Coastguard Worker     NodeId MX = MA.Addr->getNext();
483*9880d681SAndroid Build Coastguard Worker     if (MX == NA.Id) {
484*9880d681SAndroid Build Coastguard Worker       MA.Addr->setNext(NA.Addr->getNext());
485*9880d681SAndroid Build Coastguard Worker       // If the member to remove happens to be the last one, update the
486*9880d681SAndroid Build Coastguard Worker       // LastM indicator.
487*9880d681SAndroid Build Coastguard Worker       if (Code.LastM == NA.Id)
488*9880d681SAndroid Build Coastguard Worker         Code.LastM = MA.Id;
489*9880d681SAndroid Build Coastguard Worker       return;
490*9880d681SAndroid Build Coastguard Worker     }
491*9880d681SAndroid Build Coastguard Worker     MA = G.addr<NodeBase*>(MX);
492*9880d681SAndroid Build Coastguard Worker   }
493*9880d681SAndroid Build Coastguard Worker   llvm_unreachable("No such member");
494*9880d681SAndroid Build Coastguard Worker }
495*9880d681SAndroid Build Coastguard Worker 
496*9880d681SAndroid Build Coastguard Worker // Return the list of all members of the code node.
members(const DataFlowGraph & G) const497*9880d681SAndroid Build Coastguard Worker NodeList CodeNode::members(const DataFlowGraph &G) const {
498*9880d681SAndroid Build Coastguard Worker   static auto True = [] (NodeAddr<NodeBase*>) -> bool { return true; };
499*9880d681SAndroid Build Coastguard Worker   return members_if(True, G);
500*9880d681SAndroid Build Coastguard Worker }
501*9880d681SAndroid Build Coastguard Worker 
502*9880d681SAndroid Build Coastguard Worker // Return the owner of the given instr node.
getOwner(const DataFlowGraph & G)503*9880d681SAndroid Build Coastguard Worker NodeAddr<NodeBase*> InstrNode::getOwner(const DataFlowGraph &G) {
504*9880d681SAndroid Build Coastguard Worker   NodeAddr<NodeBase*> NA = G.addr<NodeBase*>(getNext());
505*9880d681SAndroid Build Coastguard Worker 
506*9880d681SAndroid Build Coastguard Worker   while (NA.Addr != this) {
507*9880d681SAndroid Build Coastguard Worker     assert(NA.Addr->getType() == NodeAttrs::Code);
508*9880d681SAndroid Build Coastguard Worker     if (NA.Addr->getKind() == NodeAttrs::Block)
509*9880d681SAndroid Build Coastguard Worker       return NA;
510*9880d681SAndroid Build Coastguard Worker     NA = G.addr<NodeBase*>(NA.Addr->getNext());
511*9880d681SAndroid Build Coastguard Worker   }
512*9880d681SAndroid Build Coastguard Worker   llvm_unreachable("No owner in circular list");
513*9880d681SAndroid Build Coastguard Worker }
514*9880d681SAndroid Build Coastguard Worker 
515*9880d681SAndroid Build Coastguard Worker // Add the phi node PA to the given block node.
addPhi(NodeAddr<PhiNode * > PA,const DataFlowGraph & G)516*9880d681SAndroid Build Coastguard Worker void BlockNode::addPhi(NodeAddr<PhiNode*> PA, const DataFlowGraph &G) {
517*9880d681SAndroid Build Coastguard Worker   auto M = getFirstMember(G);
518*9880d681SAndroid Build Coastguard Worker   if (M.Id == 0) {
519*9880d681SAndroid Build Coastguard Worker     addMember(PA, G);
520*9880d681SAndroid Build Coastguard Worker     return;
521*9880d681SAndroid Build Coastguard Worker   }
522*9880d681SAndroid Build Coastguard Worker 
523*9880d681SAndroid Build Coastguard Worker   assert(M.Addr->getType() == NodeAttrs::Code);
524*9880d681SAndroid Build Coastguard Worker   if (M.Addr->getKind() == NodeAttrs::Stmt) {
525*9880d681SAndroid Build Coastguard Worker     // If the first member of the block is a statement, insert the phi as
526*9880d681SAndroid Build Coastguard Worker     // the first member.
527*9880d681SAndroid Build Coastguard Worker     Code.FirstM = PA.Id;
528*9880d681SAndroid Build Coastguard Worker     PA.Addr->setNext(M.Id);
529*9880d681SAndroid Build Coastguard Worker   } else {
530*9880d681SAndroid Build Coastguard Worker     // If the first member is a phi, find the last phi, and append PA to it.
531*9880d681SAndroid Build Coastguard Worker     assert(M.Addr->getKind() == NodeAttrs::Phi);
532*9880d681SAndroid Build Coastguard Worker     NodeAddr<NodeBase*> MN = M;
533*9880d681SAndroid Build Coastguard Worker     do {
534*9880d681SAndroid Build Coastguard Worker       M = MN;
535*9880d681SAndroid Build Coastguard Worker       MN = G.addr<NodeBase*>(M.Addr->getNext());
536*9880d681SAndroid Build Coastguard Worker       assert(MN.Addr->getType() == NodeAttrs::Code);
537*9880d681SAndroid Build Coastguard Worker     } while (MN.Addr->getKind() == NodeAttrs::Phi);
538*9880d681SAndroid Build Coastguard Worker 
539*9880d681SAndroid Build Coastguard Worker     // M is the last phi.
540*9880d681SAndroid Build Coastguard Worker     addMemberAfter(M, PA, G);
541*9880d681SAndroid Build Coastguard Worker   }
542*9880d681SAndroid Build Coastguard Worker }
543*9880d681SAndroid Build Coastguard Worker 
544*9880d681SAndroid Build Coastguard Worker // Find the block node corresponding to the machine basic block BB in the
545*9880d681SAndroid Build Coastguard Worker // given func node.
findBlock(const MachineBasicBlock * BB,const DataFlowGraph & G) const546*9880d681SAndroid Build Coastguard Worker NodeAddr<BlockNode*> FuncNode::findBlock(const MachineBasicBlock *BB,
547*9880d681SAndroid Build Coastguard Worker       const DataFlowGraph &G) const {
548*9880d681SAndroid Build Coastguard Worker   auto EqBB = [BB] (NodeAddr<NodeBase*> NA) -> bool {
549*9880d681SAndroid Build Coastguard Worker     return NodeAddr<BlockNode*>(NA).Addr->getCode() == BB;
550*9880d681SAndroid Build Coastguard Worker   };
551*9880d681SAndroid Build Coastguard Worker   NodeList Ms = members_if(EqBB, G);
552*9880d681SAndroid Build Coastguard Worker   if (!Ms.empty())
553*9880d681SAndroid Build Coastguard Worker     return Ms[0];
554*9880d681SAndroid Build Coastguard Worker   return NodeAddr<BlockNode*>();
555*9880d681SAndroid Build Coastguard Worker }
556*9880d681SAndroid Build Coastguard Worker 
557*9880d681SAndroid Build Coastguard Worker // Get the block node for the entry block in the given function.
getEntryBlock(const DataFlowGraph & G)558*9880d681SAndroid Build Coastguard Worker NodeAddr<BlockNode*> FuncNode::getEntryBlock(const DataFlowGraph &G) {
559*9880d681SAndroid Build Coastguard Worker   MachineBasicBlock *EntryB = &getCode()->front();
560*9880d681SAndroid Build Coastguard Worker   return findBlock(EntryB, G);
561*9880d681SAndroid Build Coastguard Worker }
562*9880d681SAndroid Build Coastguard Worker 
563*9880d681SAndroid Build Coastguard Worker 
564*9880d681SAndroid Build Coastguard Worker // Register aliasing information.
565*9880d681SAndroid Build Coastguard Worker //
566*9880d681SAndroid Build Coastguard Worker // In theory, the lane information could be used to determine register
567*9880d681SAndroid Build Coastguard Worker // covering (and aliasing), but depending on the sub-register structure,
568*9880d681SAndroid Build Coastguard Worker // the lane mask information may be missing. The covering information
569*9880d681SAndroid Build Coastguard Worker // must be available for this framework to work, so relying solely on
570*9880d681SAndroid Build Coastguard Worker // the lane data is not sufficient.
571*9880d681SAndroid Build Coastguard Worker 
572*9880d681SAndroid Build Coastguard Worker // Determine whether RA covers RB.
covers(RegisterRef RA,RegisterRef RB) const573*9880d681SAndroid Build Coastguard Worker bool RegisterAliasInfo::covers(RegisterRef RA, RegisterRef RB) const {
574*9880d681SAndroid Build Coastguard Worker   if (RA == RB)
575*9880d681SAndroid Build Coastguard Worker     return true;
576*9880d681SAndroid Build Coastguard Worker   if (TargetRegisterInfo::isVirtualRegister(RA.Reg)) {
577*9880d681SAndroid Build Coastguard Worker     assert(TargetRegisterInfo::isVirtualRegister(RB.Reg));
578*9880d681SAndroid Build Coastguard Worker     if (RA.Reg != RB.Reg)
579*9880d681SAndroid Build Coastguard Worker       return false;
580*9880d681SAndroid Build Coastguard Worker     if (RA.Sub == 0)
581*9880d681SAndroid Build Coastguard Worker       return true;
582*9880d681SAndroid Build Coastguard Worker     return TRI.composeSubRegIndices(RA.Sub, RB.Sub) == RA.Sub;
583*9880d681SAndroid Build Coastguard Worker   }
584*9880d681SAndroid Build Coastguard Worker 
585*9880d681SAndroid Build Coastguard Worker   assert(TargetRegisterInfo::isPhysicalRegister(RA.Reg) &&
586*9880d681SAndroid Build Coastguard Worker          TargetRegisterInfo::isPhysicalRegister(RB.Reg));
587*9880d681SAndroid Build Coastguard Worker   unsigned A = RA.Sub != 0 ? TRI.getSubReg(RA.Reg, RA.Sub) : RA.Reg;
588*9880d681SAndroid Build Coastguard Worker   unsigned B = RB.Sub != 0 ? TRI.getSubReg(RB.Reg, RB.Sub) : RB.Reg;
589*9880d681SAndroid Build Coastguard Worker   return TRI.isSubRegister(A, B);
590*9880d681SAndroid Build Coastguard Worker }
591*9880d681SAndroid Build Coastguard Worker 
592*9880d681SAndroid Build Coastguard Worker // Determine whether RR is covered by the set of references RRs.
covers(const RegisterSet & RRs,RegisterRef RR) const593*9880d681SAndroid Build Coastguard Worker bool RegisterAliasInfo::covers(const RegisterSet &RRs, RegisterRef RR) const {
594*9880d681SAndroid Build Coastguard Worker   if (RRs.count(RR))
595*9880d681SAndroid Build Coastguard Worker     return true;
596*9880d681SAndroid Build Coastguard Worker 
597*9880d681SAndroid Build Coastguard Worker   // For virtual registers, we cannot accurately determine covering based
598*9880d681SAndroid Build Coastguard Worker   // on subregisters. If RR itself is not present in RRs, but it has a sub-
599*9880d681SAndroid Build Coastguard Worker   // register reference, check for the super-register alone. Otherwise,
600*9880d681SAndroid Build Coastguard Worker   // assume non-covering.
601*9880d681SAndroid Build Coastguard Worker   if (TargetRegisterInfo::isVirtualRegister(RR.Reg)) {
602*9880d681SAndroid Build Coastguard Worker     if (RR.Sub != 0)
603*9880d681SAndroid Build Coastguard Worker       return RRs.count({RR.Reg, 0});
604*9880d681SAndroid Build Coastguard Worker     return false;
605*9880d681SAndroid Build Coastguard Worker   }
606*9880d681SAndroid Build Coastguard Worker 
607*9880d681SAndroid Build Coastguard Worker   // If any super-register of RR is present, then RR is covered.
608*9880d681SAndroid Build Coastguard Worker   unsigned Reg = RR.Sub == 0 ? RR.Reg : TRI.getSubReg(RR.Reg, RR.Sub);
609*9880d681SAndroid Build Coastguard Worker   for (MCSuperRegIterator SR(Reg, &TRI); SR.isValid(); ++SR)
610*9880d681SAndroid Build Coastguard Worker     if (RRs.count({*SR, 0}))
611*9880d681SAndroid Build Coastguard Worker       return true;
612*9880d681SAndroid Build Coastguard Worker 
613*9880d681SAndroid Build Coastguard Worker   return false;
614*9880d681SAndroid Build Coastguard Worker }
615*9880d681SAndroid Build Coastguard Worker 
616*9880d681SAndroid Build Coastguard Worker // Get the list of references aliased to RR.
getAliasSet(RegisterRef RR) const617*9880d681SAndroid Build Coastguard Worker std::vector<RegisterRef> RegisterAliasInfo::getAliasSet(RegisterRef RR) const {
618*9880d681SAndroid Build Coastguard Worker   // Do not include RR in the alias set. For virtual registers return an
619*9880d681SAndroid Build Coastguard Worker   // empty set.
620*9880d681SAndroid Build Coastguard Worker   std::vector<RegisterRef> AS;
621*9880d681SAndroid Build Coastguard Worker   if (TargetRegisterInfo::isVirtualRegister(RR.Reg))
622*9880d681SAndroid Build Coastguard Worker     return AS;
623*9880d681SAndroid Build Coastguard Worker   assert(TargetRegisterInfo::isPhysicalRegister(RR.Reg));
624*9880d681SAndroid Build Coastguard Worker   unsigned R = RR.Reg;
625*9880d681SAndroid Build Coastguard Worker   if (RR.Sub)
626*9880d681SAndroid Build Coastguard Worker     R = TRI.getSubReg(RR.Reg, RR.Sub);
627*9880d681SAndroid Build Coastguard Worker 
628*9880d681SAndroid Build Coastguard Worker   for (MCRegAliasIterator AI(R, &TRI, false); AI.isValid(); ++AI)
629*9880d681SAndroid Build Coastguard Worker     AS.push_back(RegisterRef({*AI, 0}));
630*9880d681SAndroid Build Coastguard Worker   return AS;
631*9880d681SAndroid Build Coastguard Worker }
632*9880d681SAndroid Build Coastguard Worker 
633*9880d681SAndroid Build Coastguard Worker // Check whether RA and RB are aliased.
alias(RegisterRef RA,RegisterRef RB) const634*9880d681SAndroid Build Coastguard Worker bool RegisterAliasInfo::alias(RegisterRef RA, RegisterRef RB) const {
635*9880d681SAndroid Build Coastguard Worker   bool VirtA = TargetRegisterInfo::isVirtualRegister(RA.Reg);
636*9880d681SAndroid Build Coastguard Worker   bool VirtB = TargetRegisterInfo::isVirtualRegister(RB.Reg);
637*9880d681SAndroid Build Coastguard Worker   bool PhysA = TargetRegisterInfo::isPhysicalRegister(RA.Reg);
638*9880d681SAndroid Build Coastguard Worker   bool PhysB = TargetRegisterInfo::isPhysicalRegister(RB.Reg);
639*9880d681SAndroid Build Coastguard Worker 
640*9880d681SAndroid Build Coastguard Worker   if (VirtA != VirtB)
641*9880d681SAndroid Build Coastguard Worker     return false;
642*9880d681SAndroid Build Coastguard Worker 
643*9880d681SAndroid Build Coastguard Worker   if (VirtA) {
644*9880d681SAndroid Build Coastguard Worker     if (RA.Reg != RB.Reg)
645*9880d681SAndroid Build Coastguard Worker       return false;
646*9880d681SAndroid Build Coastguard Worker     // RA and RB refer to the same register. If any of them refer to the
647*9880d681SAndroid Build Coastguard Worker     // whole register, they must be aliased.
648*9880d681SAndroid Build Coastguard Worker     if (RA.Sub == 0 || RB.Sub == 0)
649*9880d681SAndroid Build Coastguard Worker       return true;
650*9880d681SAndroid Build Coastguard Worker     unsigned SA = TRI.getSubRegIdxSize(RA.Sub);
651*9880d681SAndroid Build Coastguard Worker     unsigned OA = TRI.getSubRegIdxOffset(RA.Sub);
652*9880d681SAndroid Build Coastguard Worker     unsigned SB = TRI.getSubRegIdxSize(RB.Sub);
653*9880d681SAndroid Build Coastguard Worker     unsigned OB = TRI.getSubRegIdxOffset(RB.Sub);
654*9880d681SAndroid Build Coastguard Worker     if (OA <= OB && OA+SA > OB)
655*9880d681SAndroid Build Coastguard Worker       return true;
656*9880d681SAndroid Build Coastguard Worker     if (OB <= OA && OB+SB > OA)
657*9880d681SAndroid Build Coastguard Worker       return true;
658*9880d681SAndroid Build Coastguard Worker     return false;
659*9880d681SAndroid Build Coastguard Worker   }
660*9880d681SAndroid Build Coastguard Worker 
661*9880d681SAndroid Build Coastguard Worker   assert(PhysA && PhysB);
662*9880d681SAndroid Build Coastguard Worker   (void)PhysA, (void)PhysB;
663*9880d681SAndroid Build Coastguard Worker   unsigned A = RA.Sub ? TRI.getSubReg(RA.Reg, RA.Sub) : RA.Reg;
664*9880d681SAndroid Build Coastguard Worker   unsigned B = RB.Sub ? TRI.getSubReg(RB.Reg, RB.Sub) : RB.Reg;
665*9880d681SAndroid Build Coastguard Worker   for (MCRegAliasIterator I(A, &TRI, true); I.isValid(); ++I)
666*9880d681SAndroid Build Coastguard Worker     if (B == *I)
667*9880d681SAndroid Build Coastguard Worker       return true;
668*9880d681SAndroid Build Coastguard Worker   return false;
669*9880d681SAndroid Build Coastguard Worker }
670*9880d681SAndroid Build Coastguard Worker 
671*9880d681SAndroid Build Coastguard Worker 
672*9880d681SAndroid Build Coastguard Worker // Target operand information.
673*9880d681SAndroid Build Coastguard Worker //
674*9880d681SAndroid Build Coastguard Worker 
675*9880d681SAndroid Build Coastguard Worker // For a given instruction, check if there are any bits of RR that can remain
676*9880d681SAndroid Build Coastguard Worker // unchanged across this def.
isPreserving(const MachineInstr & In,unsigned OpNum) const677*9880d681SAndroid Build Coastguard Worker bool TargetOperandInfo::isPreserving(const MachineInstr &In, unsigned OpNum)
678*9880d681SAndroid Build Coastguard Worker       const {
679*9880d681SAndroid Build Coastguard Worker   return TII.isPredicated(In);
680*9880d681SAndroid Build Coastguard Worker }
681*9880d681SAndroid Build Coastguard Worker 
682*9880d681SAndroid Build Coastguard Worker // Check if the definition of RR produces an unspecified value.
isClobbering(const MachineInstr & In,unsigned OpNum) const683*9880d681SAndroid Build Coastguard Worker bool TargetOperandInfo::isClobbering(const MachineInstr &In, unsigned OpNum)
684*9880d681SAndroid Build Coastguard Worker       const {
685*9880d681SAndroid Build Coastguard Worker   if (In.isCall())
686*9880d681SAndroid Build Coastguard Worker     if (In.getOperand(OpNum).isImplicit())
687*9880d681SAndroid Build Coastguard Worker       return true;
688*9880d681SAndroid Build Coastguard Worker   return false;
689*9880d681SAndroid Build Coastguard Worker }
690*9880d681SAndroid Build Coastguard Worker 
691*9880d681SAndroid Build Coastguard Worker // Check if the given instruction specifically requires
isFixedReg(const MachineInstr & In,unsigned OpNum) const692*9880d681SAndroid Build Coastguard Worker bool TargetOperandInfo::isFixedReg(const MachineInstr &In, unsigned OpNum)
693*9880d681SAndroid Build Coastguard Worker       const {
694*9880d681SAndroid Build Coastguard Worker   if (In.isCall() || In.isReturn() || In.isInlineAsm())
695*9880d681SAndroid Build Coastguard Worker     return true;
696*9880d681SAndroid Build Coastguard Worker   // Check for a tail call.
697*9880d681SAndroid Build Coastguard Worker   if (In.isBranch())
698*9880d681SAndroid Build Coastguard Worker     for (auto &O : In.operands())
699*9880d681SAndroid Build Coastguard Worker       if (O.isGlobal() || O.isSymbol())
700*9880d681SAndroid Build Coastguard Worker         return true;
701*9880d681SAndroid Build Coastguard Worker 
702*9880d681SAndroid Build Coastguard Worker   const MCInstrDesc &D = In.getDesc();
703*9880d681SAndroid Build Coastguard Worker   if (!D.getImplicitDefs() && !D.getImplicitUses())
704*9880d681SAndroid Build Coastguard Worker     return false;
705*9880d681SAndroid Build Coastguard Worker   const MachineOperand &Op = In.getOperand(OpNum);
706*9880d681SAndroid Build Coastguard Worker   // If there is a sub-register, treat the operand as non-fixed. Currently,
707*9880d681SAndroid Build Coastguard Worker   // fixed registers are those that are listed in the descriptor as implicit
708*9880d681SAndroid Build Coastguard Worker   // uses or defs, and those lists do not allow sub-registers.
709*9880d681SAndroid Build Coastguard Worker   if (Op.getSubReg() != 0)
710*9880d681SAndroid Build Coastguard Worker     return false;
711*9880d681SAndroid Build Coastguard Worker   unsigned Reg = Op.getReg();
712*9880d681SAndroid Build Coastguard Worker   const MCPhysReg *ImpR = Op.isDef() ? D.getImplicitDefs()
713*9880d681SAndroid Build Coastguard Worker                                      : D.getImplicitUses();
714*9880d681SAndroid Build Coastguard Worker   if (!ImpR)
715*9880d681SAndroid Build Coastguard Worker     return false;
716*9880d681SAndroid Build Coastguard Worker   while (*ImpR)
717*9880d681SAndroid Build Coastguard Worker     if (*ImpR++ == Reg)
718*9880d681SAndroid Build Coastguard Worker       return true;
719*9880d681SAndroid Build Coastguard Worker   return false;
720*9880d681SAndroid Build Coastguard Worker }
721*9880d681SAndroid Build Coastguard Worker 
722*9880d681SAndroid Build Coastguard Worker 
723*9880d681SAndroid Build Coastguard Worker //
724*9880d681SAndroid Build Coastguard Worker // The data flow graph construction.
725*9880d681SAndroid Build Coastguard Worker //
726*9880d681SAndroid Build Coastguard Worker 
DataFlowGraph(MachineFunction & mf,const TargetInstrInfo & tii,const TargetRegisterInfo & tri,const MachineDominatorTree & mdt,const MachineDominanceFrontier & mdf,const RegisterAliasInfo & rai,const TargetOperandInfo & toi)727*9880d681SAndroid Build Coastguard Worker DataFlowGraph::DataFlowGraph(MachineFunction &mf, const TargetInstrInfo &tii,
728*9880d681SAndroid Build Coastguard Worker       const TargetRegisterInfo &tri, const MachineDominatorTree &mdt,
729*9880d681SAndroid Build Coastguard Worker       const MachineDominanceFrontier &mdf, const RegisterAliasInfo &rai,
730*9880d681SAndroid Build Coastguard Worker       const TargetOperandInfo &toi)
731*9880d681SAndroid Build Coastguard Worker     : TimeG("rdf"), MF(mf), TII(tii), TRI(tri), MDT(mdt), MDF(mdf), RAI(rai),
732*9880d681SAndroid Build Coastguard Worker       TOI(toi) {
733*9880d681SAndroid Build Coastguard Worker }
734*9880d681SAndroid Build Coastguard Worker 
735*9880d681SAndroid Build Coastguard Worker 
736*9880d681SAndroid Build Coastguard Worker // The implementation of the definition stack.
737*9880d681SAndroid Build Coastguard Worker // Each register reference has its own definition stack. In particular,
738*9880d681SAndroid Build Coastguard Worker // for a register references "Reg" and "Reg:subreg" will each have their
739*9880d681SAndroid Build Coastguard Worker // own definition stacks.
740*9880d681SAndroid Build Coastguard Worker 
741*9880d681SAndroid Build Coastguard Worker // Construct a stack iterator.
Iterator(const DataFlowGraph::DefStack & S,bool Top)742*9880d681SAndroid Build Coastguard Worker DataFlowGraph::DefStack::Iterator::Iterator(const DataFlowGraph::DefStack &S,
743*9880d681SAndroid Build Coastguard Worker       bool Top) : DS(S) {
744*9880d681SAndroid Build Coastguard Worker   if (!Top) {
745*9880d681SAndroid Build Coastguard Worker     // Initialize to bottom.
746*9880d681SAndroid Build Coastguard Worker     Pos = 0;
747*9880d681SAndroid Build Coastguard Worker     return;
748*9880d681SAndroid Build Coastguard Worker   }
749*9880d681SAndroid Build Coastguard Worker   // Initialize to the top, i.e. top-most non-delimiter (or 0, if empty).
750*9880d681SAndroid Build Coastguard Worker   Pos = DS.Stack.size();
751*9880d681SAndroid Build Coastguard Worker   while (Pos > 0 && DS.isDelimiter(DS.Stack[Pos-1]))
752*9880d681SAndroid Build Coastguard Worker     Pos--;
753*9880d681SAndroid Build Coastguard Worker }
754*9880d681SAndroid Build Coastguard Worker 
755*9880d681SAndroid Build Coastguard Worker // Return the size of the stack, including block delimiters.
size() const756*9880d681SAndroid Build Coastguard Worker unsigned DataFlowGraph::DefStack::size() const {
757*9880d681SAndroid Build Coastguard Worker   unsigned S = 0;
758*9880d681SAndroid Build Coastguard Worker   for (auto I = top(), E = bottom(); I != E; I.down())
759*9880d681SAndroid Build Coastguard Worker     S++;
760*9880d681SAndroid Build Coastguard Worker   return S;
761*9880d681SAndroid Build Coastguard Worker }
762*9880d681SAndroid Build Coastguard Worker 
763*9880d681SAndroid Build Coastguard Worker // Remove the top entry from the stack. Remove all intervening delimiters
764*9880d681SAndroid Build Coastguard Worker // so that after this, the stack is either empty, or the top of the stack
765*9880d681SAndroid Build Coastguard Worker // is a non-delimiter.
pop()766*9880d681SAndroid Build Coastguard Worker void DataFlowGraph::DefStack::pop() {
767*9880d681SAndroid Build Coastguard Worker   assert(!empty());
768*9880d681SAndroid Build Coastguard Worker   unsigned P = nextDown(Stack.size());
769*9880d681SAndroid Build Coastguard Worker   Stack.resize(P);
770*9880d681SAndroid Build Coastguard Worker }
771*9880d681SAndroid Build Coastguard Worker 
772*9880d681SAndroid Build Coastguard Worker // Push a delimiter for block node N on the stack.
start_block(NodeId N)773*9880d681SAndroid Build Coastguard Worker void DataFlowGraph::DefStack::start_block(NodeId N) {
774*9880d681SAndroid Build Coastguard Worker   assert(N != 0);
775*9880d681SAndroid Build Coastguard Worker   Stack.push_back(NodeAddr<DefNode*>(nullptr, N));
776*9880d681SAndroid Build Coastguard Worker }
777*9880d681SAndroid Build Coastguard Worker 
778*9880d681SAndroid Build Coastguard Worker // Remove all nodes from the top of the stack, until the delimited for
779*9880d681SAndroid Build Coastguard Worker // block node N is encountered. Remove the delimiter as well. In effect,
780*9880d681SAndroid Build Coastguard Worker // this will remove from the stack all definitions from block N.
clear_block(NodeId N)781*9880d681SAndroid Build Coastguard Worker void DataFlowGraph::DefStack::clear_block(NodeId N) {
782*9880d681SAndroid Build Coastguard Worker   assert(N != 0);
783*9880d681SAndroid Build Coastguard Worker   unsigned P = Stack.size();
784*9880d681SAndroid Build Coastguard Worker   while (P > 0) {
785*9880d681SAndroid Build Coastguard Worker     bool Found = isDelimiter(Stack[P-1], N);
786*9880d681SAndroid Build Coastguard Worker     P--;
787*9880d681SAndroid Build Coastguard Worker     if (Found)
788*9880d681SAndroid Build Coastguard Worker       break;
789*9880d681SAndroid Build Coastguard Worker   }
790*9880d681SAndroid Build Coastguard Worker   // This will also remove the delimiter, if found.
791*9880d681SAndroid Build Coastguard Worker   Stack.resize(P);
792*9880d681SAndroid Build Coastguard Worker }
793*9880d681SAndroid Build Coastguard Worker 
794*9880d681SAndroid Build Coastguard Worker // Move the stack iterator up by one.
nextUp(unsigned P) const795*9880d681SAndroid Build Coastguard Worker unsigned DataFlowGraph::DefStack::nextUp(unsigned P) const {
796*9880d681SAndroid Build Coastguard Worker   // Get the next valid position after P (skipping all delimiters).
797*9880d681SAndroid Build Coastguard Worker   // The input position P does not have to point to a non-delimiter.
798*9880d681SAndroid Build Coastguard Worker   unsigned SS = Stack.size();
799*9880d681SAndroid Build Coastguard Worker   bool IsDelim;
800*9880d681SAndroid Build Coastguard Worker   assert(P < SS);
801*9880d681SAndroid Build Coastguard Worker   do {
802*9880d681SAndroid Build Coastguard Worker     P++;
803*9880d681SAndroid Build Coastguard Worker     IsDelim = isDelimiter(Stack[P-1]);
804*9880d681SAndroid Build Coastguard Worker   } while (P < SS && IsDelim);
805*9880d681SAndroid Build Coastguard Worker   assert(!IsDelim);
806*9880d681SAndroid Build Coastguard Worker   return P;
807*9880d681SAndroid Build Coastguard Worker }
808*9880d681SAndroid Build Coastguard Worker 
809*9880d681SAndroid Build Coastguard Worker // Move the stack iterator down by one.
nextDown(unsigned P) const810*9880d681SAndroid Build Coastguard Worker unsigned DataFlowGraph::DefStack::nextDown(unsigned P) const {
811*9880d681SAndroid Build Coastguard Worker   // Get the preceding valid position before P (skipping all delimiters).
812*9880d681SAndroid Build Coastguard Worker   // The input position P does not have to point to a non-delimiter.
813*9880d681SAndroid Build Coastguard Worker   assert(P > 0 && P <= Stack.size());
814*9880d681SAndroid Build Coastguard Worker   bool IsDelim = isDelimiter(Stack[P-1]);
815*9880d681SAndroid Build Coastguard Worker   do {
816*9880d681SAndroid Build Coastguard Worker     if (--P == 0)
817*9880d681SAndroid Build Coastguard Worker       break;
818*9880d681SAndroid Build Coastguard Worker     IsDelim = isDelimiter(Stack[P-1]);
819*9880d681SAndroid Build Coastguard Worker   } while (P > 0 && IsDelim);
820*9880d681SAndroid Build Coastguard Worker   assert(!IsDelim);
821*9880d681SAndroid Build Coastguard Worker   return P;
822*9880d681SAndroid Build Coastguard Worker }
823*9880d681SAndroid Build Coastguard Worker 
824*9880d681SAndroid Build Coastguard Worker // Node management functions.
825*9880d681SAndroid Build Coastguard Worker 
826*9880d681SAndroid Build Coastguard Worker // Get the pointer to the node with the id N.
ptr(NodeId N) const827*9880d681SAndroid Build Coastguard Worker NodeBase *DataFlowGraph::ptr(NodeId N) const {
828*9880d681SAndroid Build Coastguard Worker   if (N == 0)
829*9880d681SAndroid Build Coastguard Worker     return nullptr;
830*9880d681SAndroid Build Coastguard Worker   return Memory.ptr(N);
831*9880d681SAndroid Build Coastguard Worker }
832*9880d681SAndroid Build Coastguard Worker 
833*9880d681SAndroid Build Coastguard Worker // Get the id of the node at the address P.
id(const NodeBase * P) const834*9880d681SAndroid Build Coastguard Worker NodeId DataFlowGraph::id(const NodeBase *P) const {
835*9880d681SAndroid Build Coastguard Worker   if (P == nullptr)
836*9880d681SAndroid Build Coastguard Worker     return 0;
837*9880d681SAndroid Build Coastguard Worker   return Memory.id(P);
838*9880d681SAndroid Build Coastguard Worker }
839*9880d681SAndroid Build Coastguard Worker 
840*9880d681SAndroid Build Coastguard Worker // Allocate a new node and set the attributes to Attrs.
newNode(uint16_t Attrs)841*9880d681SAndroid Build Coastguard Worker NodeAddr<NodeBase*> DataFlowGraph::newNode(uint16_t Attrs) {
842*9880d681SAndroid Build Coastguard Worker   NodeAddr<NodeBase*> P = Memory.New();
843*9880d681SAndroid Build Coastguard Worker   P.Addr->init();
844*9880d681SAndroid Build Coastguard Worker   P.Addr->setAttrs(Attrs);
845*9880d681SAndroid Build Coastguard Worker   return P;
846*9880d681SAndroid Build Coastguard Worker }
847*9880d681SAndroid Build Coastguard Worker 
848*9880d681SAndroid Build Coastguard Worker // Make a copy of the given node B, except for the data-flow links, which
849*9880d681SAndroid Build Coastguard Worker // are set to 0.
cloneNode(const NodeAddr<NodeBase * > B)850*9880d681SAndroid Build Coastguard Worker NodeAddr<NodeBase*> DataFlowGraph::cloneNode(const NodeAddr<NodeBase*> B) {
851*9880d681SAndroid Build Coastguard Worker   NodeAddr<NodeBase*> NA = newNode(0);
852*9880d681SAndroid Build Coastguard Worker   memcpy(NA.Addr, B.Addr, sizeof(NodeBase));
853*9880d681SAndroid Build Coastguard Worker   // Ref nodes need to have the data-flow links reset.
854*9880d681SAndroid Build Coastguard Worker   if (NA.Addr->getType() == NodeAttrs::Ref) {
855*9880d681SAndroid Build Coastguard Worker     NodeAddr<RefNode*> RA = NA;
856*9880d681SAndroid Build Coastguard Worker     RA.Addr->setReachingDef(0);
857*9880d681SAndroid Build Coastguard Worker     RA.Addr->setSibling(0);
858*9880d681SAndroid Build Coastguard Worker     if (NA.Addr->getKind() == NodeAttrs::Def) {
859*9880d681SAndroid Build Coastguard Worker       NodeAddr<DefNode*> DA = NA;
860*9880d681SAndroid Build Coastguard Worker       DA.Addr->setReachedDef(0);
861*9880d681SAndroid Build Coastguard Worker       DA.Addr->setReachedUse(0);
862*9880d681SAndroid Build Coastguard Worker     }
863*9880d681SAndroid Build Coastguard Worker   }
864*9880d681SAndroid Build Coastguard Worker   return NA;
865*9880d681SAndroid Build Coastguard Worker }
866*9880d681SAndroid Build Coastguard Worker 
867*9880d681SAndroid Build Coastguard Worker 
868*9880d681SAndroid Build Coastguard Worker // Allocation routines for specific node types/kinds.
869*9880d681SAndroid Build Coastguard Worker 
newUse(NodeAddr<InstrNode * > Owner,MachineOperand & Op,uint16_t Flags)870*9880d681SAndroid Build Coastguard Worker NodeAddr<UseNode*> DataFlowGraph::newUse(NodeAddr<InstrNode*> Owner,
871*9880d681SAndroid Build Coastguard Worker       MachineOperand &Op, uint16_t Flags) {
872*9880d681SAndroid Build Coastguard Worker   NodeAddr<UseNode*> UA = newNode(NodeAttrs::Ref | NodeAttrs::Use | Flags);
873*9880d681SAndroid Build Coastguard Worker   UA.Addr->setRegRef(&Op);
874*9880d681SAndroid Build Coastguard Worker   return UA;
875*9880d681SAndroid Build Coastguard Worker }
876*9880d681SAndroid Build Coastguard Worker 
newPhiUse(NodeAddr<PhiNode * > Owner,RegisterRef RR,NodeAddr<BlockNode * > PredB,uint16_t Flags)877*9880d681SAndroid Build Coastguard Worker NodeAddr<PhiUseNode*> DataFlowGraph::newPhiUse(NodeAddr<PhiNode*> Owner,
878*9880d681SAndroid Build Coastguard Worker       RegisterRef RR, NodeAddr<BlockNode*> PredB, uint16_t Flags) {
879*9880d681SAndroid Build Coastguard Worker   NodeAddr<PhiUseNode*> PUA = newNode(NodeAttrs::Ref | NodeAttrs::Use | Flags);
880*9880d681SAndroid Build Coastguard Worker   assert(Flags & NodeAttrs::PhiRef);
881*9880d681SAndroid Build Coastguard Worker   PUA.Addr->setRegRef(RR);
882*9880d681SAndroid Build Coastguard Worker   PUA.Addr->setPredecessor(PredB.Id);
883*9880d681SAndroid Build Coastguard Worker   return PUA;
884*9880d681SAndroid Build Coastguard Worker }
885*9880d681SAndroid Build Coastguard Worker 
newDef(NodeAddr<InstrNode * > Owner,MachineOperand & Op,uint16_t Flags)886*9880d681SAndroid Build Coastguard Worker NodeAddr<DefNode*> DataFlowGraph::newDef(NodeAddr<InstrNode*> Owner,
887*9880d681SAndroid Build Coastguard Worker       MachineOperand &Op, uint16_t Flags) {
888*9880d681SAndroid Build Coastguard Worker   NodeAddr<DefNode*> DA = newNode(NodeAttrs::Ref | NodeAttrs::Def | Flags);
889*9880d681SAndroid Build Coastguard Worker   DA.Addr->setRegRef(&Op);
890*9880d681SAndroid Build Coastguard Worker   return DA;
891*9880d681SAndroid Build Coastguard Worker }
892*9880d681SAndroid Build Coastguard Worker 
newDef(NodeAddr<InstrNode * > Owner,RegisterRef RR,uint16_t Flags)893*9880d681SAndroid Build Coastguard Worker NodeAddr<DefNode*> DataFlowGraph::newDef(NodeAddr<InstrNode*> Owner,
894*9880d681SAndroid Build Coastguard Worker       RegisterRef RR, uint16_t Flags) {
895*9880d681SAndroid Build Coastguard Worker   NodeAddr<DefNode*> DA = newNode(NodeAttrs::Ref | NodeAttrs::Def | Flags);
896*9880d681SAndroid Build Coastguard Worker   assert(Flags & NodeAttrs::PhiRef);
897*9880d681SAndroid Build Coastguard Worker   DA.Addr->setRegRef(RR);
898*9880d681SAndroid Build Coastguard Worker   return DA;
899*9880d681SAndroid Build Coastguard Worker }
900*9880d681SAndroid Build Coastguard Worker 
newPhi(NodeAddr<BlockNode * > Owner)901*9880d681SAndroid Build Coastguard Worker NodeAddr<PhiNode*> DataFlowGraph::newPhi(NodeAddr<BlockNode*> Owner) {
902*9880d681SAndroid Build Coastguard Worker   NodeAddr<PhiNode*> PA = newNode(NodeAttrs::Code | NodeAttrs::Phi);
903*9880d681SAndroid Build Coastguard Worker   Owner.Addr->addPhi(PA, *this);
904*9880d681SAndroid Build Coastguard Worker   return PA;
905*9880d681SAndroid Build Coastguard Worker }
906*9880d681SAndroid Build Coastguard Worker 
newStmt(NodeAddr<BlockNode * > Owner,MachineInstr * MI)907*9880d681SAndroid Build Coastguard Worker NodeAddr<StmtNode*> DataFlowGraph::newStmt(NodeAddr<BlockNode*> Owner,
908*9880d681SAndroid Build Coastguard Worker       MachineInstr *MI) {
909*9880d681SAndroid Build Coastguard Worker   NodeAddr<StmtNode*> SA = newNode(NodeAttrs::Code | NodeAttrs::Stmt);
910*9880d681SAndroid Build Coastguard Worker   SA.Addr->setCode(MI);
911*9880d681SAndroid Build Coastguard Worker   Owner.Addr->addMember(SA, *this);
912*9880d681SAndroid Build Coastguard Worker   return SA;
913*9880d681SAndroid Build Coastguard Worker }
914*9880d681SAndroid Build Coastguard Worker 
newBlock(NodeAddr<FuncNode * > Owner,MachineBasicBlock * BB)915*9880d681SAndroid Build Coastguard Worker NodeAddr<BlockNode*> DataFlowGraph::newBlock(NodeAddr<FuncNode*> Owner,
916*9880d681SAndroid Build Coastguard Worker       MachineBasicBlock *BB) {
917*9880d681SAndroid Build Coastguard Worker   NodeAddr<BlockNode*> BA = newNode(NodeAttrs::Code | NodeAttrs::Block);
918*9880d681SAndroid Build Coastguard Worker   BA.Addr->setCode(BB);
919*9880d681SAndroid Build Coastguard Worker   Owner.Addr->addMember(BA, *this);
920*9880d681SAndroid Build Coastguard Worker   return BA;
921*9880d681SAndroid Build Coastguard Worker }
922*9880d681SAndroid Build Coastguard Worker 
newFunc(MachineFunction * MF)923*9880d681SAndroid Build Coastguard Worker NodeAddr<FuncNode*> DataFlowGraph::newFunc(MachineFunction *MF) {
924*9880d681SAndroid Build Coastguard Worker   NodeAddr<FuncNode*> FA = newNode(NodeAttrs::Code | NodeAttrs::Func);
925*9880d681SAndroid Build Coastguard Worker   FA.Addr->setCode(MF);
926*9880d681SAndroid Build Coastguard Worker   return FA;
927*9880d681SAndroid Build Coastguard Worker }
928*9880d681SAndroid Build Coastguard Worker 
929*9880d681SAndroid Build Coastguard Worker // Build the data flow graph.
build(unsigned Options)930*9880d681SAndroid Build Coastguard Worker void DataFlowGraph::build(unsigned Options) {
931*9880d681SAndroid Build Coastguard Worker   reset();
932*9880d681SAndroid Build Coastguard Worker   Func = newFunc(&MF);
933*9880d681SAndroid Build Coastguard Worker 
934*9880d681SAndroid Build Coastguard Worker   if (MF.empty())
935*9880d681SAndroid Build Coastguard Worker     return;
936*9880d681SAndroid Build Coastguard Worker 
937*9880d681SAndroid Build Coastguard Worker   for (auto &B : MF) {
938*9880d681SAndroid Build Coastguard Worker     auto BA = newBlock(Func, &B);
939*9880d681SAndroid Build Coastguard Worker     for (auto &I : B) {
940*9880d681SAndroid Build Coastguard Worker       if (I.isDebugValue())
941*9880d681SAndroid Build Coastguard Worker         continue;
942*9880d681SAndroid Build Coastguard Worker       buildStmt(BA, I);
943*9880d681SAndroid Build Coastguard Worker     }
944*9880d681SAndroid Build Coastguard Worker   }
945*9880d681SAndroid Build Coastguard Worker 
946*9880d681SAndroid Build Coastguard Worker   // Collect information about block references.
947*9880d681SAndroid Build Coastguard Worker   NodeAddr<BlockNode*> EA = Func.Addr->getEntryBlock(*this);
948*9880d681SAndroid Build Coastguard Worker   BlockRefsMap RefM;
949*9880d681SAndroid Build Coastguard Worker   buildBlockRefs(EA, RefM);
950*9880d681SAndroid Build Coastguard Worker 
951*9880d681SAndroid Build Coastguard Worker   // Add function-entry phi nodes.
952*9880d681SAndroid Build Coastguard Worker   MachineRegisterInfo &MRI = MF.getRegInfo();
953*9880d681SAndroid Build Coastguard Worker   for (auto I = MRI.livein_begin(), E = MRI.livein_end(); I != E; ++I) {
954*9880d681SAndroid Build Coastguard Worker     NodeAddr<PhiNode*> PA = newPhi(EA);
955*9880d681SAndroid Build Coastguard Worker     RegisterRef RR = { I->first, 0 };
956*9880d681SAndroid Build Coastguard Worker     uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving;
957*9880d681SAndroid Build Coastguard Worker     NodeAddr<DefNode*> DA = newDef(PA, RR, PhiFlags);
958*9880d681SAndroid Build Coastguard Worker     PA.Addr->addMember(DA, *this);
959*9880d681SAndroid Build Coastguard Worker   }
960*9880d681SAndroid Build Coastguard Worker 
961*9880d681SAndroid Build Coastguard Worker   // Build a map "PhiM" which will contain, for each block, the set
962*9880d681SAndroid Build Coastguard Worker   // of references that will require phi definitions in that block.
963*9880d681SAndroid Build Coastguard Worker   BlockRefsMap PhiM;
964*9880d681SAndroid Build Coastguard Worker   auto Blocks = Func.Addr->members(*this);
965*9880d681SAndroid Build Coastguard Worker   for (NodeAddr<BlockNode*> BA : Blocks)
966*9880d681SAndroid Build Coastguard Worker     recordDefsForDF(PhiM, RefM, BA);
967*9880d681SAndroid Build Coastguard Worker   for (NodeAddr<BlockNode*> BA : Blocks)
968*9880d681SAndroid Build Coastguard Worker     buildPhis(PhiM, RefM, BA);
969*9880d681SAndroid Build Coastguard Worker 
970*9880d681SAndroid Build Coastguard Worker   // Link all the refs. This will recursively traverse the dominator tree.
971*9880d681SAndroid Build Coastguard Worker   DefStackMap DM;
972*9880d681SAndroid Build Coastguard Worker   linkBlockRefs(DM, EA);
973*9880d681SAndroid Build Coastguard Worker 
974*9880d681SAndroid Build Coastguard Worker   // Finally, remove all unused phi nodes.
975*9880d681SAndroid Build Coastguard Worker   if (!(Options & BuildOptions::KeepDeadPhis))
976*9880d681SAndroid Build Coastguard Worker     removeUnusedPhis();
977*9880d681SAndroid Build Coastguard Worker }
978*9880d681SAndroid Build Coastguard Worker 
979*9880d681SAndroid Build Coastguard Worker // For each stack in the map DefM, push the delimiter for block B on it.
markBlock(NodeId B,DefStackMap & DefM)980*9880d681SAndroid Build Coastguard Worker void DataFlowGraph::markBlock(NodeId B, DefStackMap &DefM) {
981*9880d681SAndroid Build Coastguard Worker   // Push block delimiters.
982*9880d681SAndroid Build Coastguard Worker   for (auto I = DefM.begin(), E = DefM.end(); I != E; ++I)
983*9880d681SAndroid Build Coastguard Worker     I->second.start_block(B);
984*9880d681SAndroid Build Coastguard Worker }
985*9880d681SAndroid Build Coastguard Worker 
986*9880d681SAndroid Build Coastguard Worker // Remove all definitions coming from block B from each stack in DefM.
releaseBlock(NodeId B,DefStackMap & DefM)987*9880d681SAndroid Build Coastguard Worker void DataFlowGraph::releaseBlock(NodeId B, DefStackMap &DefM) {
988*9880d681SAndroid Build Coastguard Worker   // Pop all defs from this block from the definition stack. Defs that were
989*9880d681SAndroid Build Coastguard Worker   // added to the map during the traversal of instructions will not have a
990*9880d681SAndroid Build Coastguard Worker   // delimiter, but for those, the whole stack will be emptied.
991*9880d681SAndroid Build Coastguard Worker   for (auto I = DefM.begin(), E = DefM.end(); I != E; ++I)
992*9880d681SAndroid Build Coastguard Worker     I->second.clear_block(B);
993*9880d681SAndroid Build Coastguard Worker 
994*9880d681SAndroid Build Coastguard Worker   // Finally, remove empty stacks from the map.
995*9880d681SAndroid Build Coastguard Worker   for (auto I = DefM.begin(), E = DefM.end(), NextI = I; I != E; I = NextI) {
996*9880d681SAndroid Build Coastguard Worker     NextI = std::next(I);
997*9880d681SAndroid Build Coastguard Worker     // This preserves the validity of iterators other than I.
998*9880d681SAndroid Build Coastguard Worker     if (I->second.empty())
999*9880d681SAndroid Build Coastguard Worker       DefM.erase(I);
1000*9880d681SAndroid Build Coastguard Worker   }
1001*9880d681SAndroid Build Coastguard Worker }
1002*9880d681SAndroid Build Coastguard Worker 
1003*9880d681SAndroid Build Coastguard Worker // Push all definitions from the instruction node IA to an appropriate
1004*9880d681SAndroid Build Coastguard Worker // stack in DefM.
pushDefs(NodeAddr<InstrNode * > IA,DefStackMap & DefM)1005*9880d681SAndroid Build Coastguard Worker void DataFlowGraph::pushDefs(NodeAddr<InstrNode*> IA, DefStackMap &DefM) {
1006*9880d681SAndroid Build Coastguard Worker   NodeList Defs = IA.Addr->members_if(IsDef, *this);
1007*9880d681SAndroid Build Coastguard Worker   NodeSet Visited;
1008*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
1009*9880d681SAndroid Build Coastguard Worker   RegisterSet Defined;
1010*9880d681SAndroid Build Coastguard Worker #endif
1011*9880d681SAndroid Build Coastguard Worker 
1012*9880d681SAndroid Build Coastguard Worker   // The important objectives of this function are:
1013*9880d681SAndroid Build Coastguard Worker   // - to be able to handle instructions both while the graph is being
1014*9880d681SAndroid Build Coastguard Worker   //   constructed, and after the graph has been constructed, and
1015*9880d681SAndroid Build Coastguard Worker   // - maintain proper ordering of definitions on the stack for each
1016*9880d681SAndroid Build Coastguard Worker   //   register reference:
1017*9880d681SAndroid Build Coastguard Worker   //   - if there are two or more related defs in IA (i.e. coming from
1018*9880d681SAndroid Build Coastguard Worker   //     the same machine operand), then only push one def on the stack,
1019*9880d681SAndroid Build Coastguard Worker   //   - if there are multiple unrelated defs of non-overlapping
1020*9880d681SAndroid Build Coastguard Worker   //     subregisters of S, then the stack for S will have both (in an
1021*9880d681SAndroid Build Coastguard Worker   //     unspecified order), but the order does not matter from the data-
1022*9880d681SAndroid Build Coastguard Worker   //     -flow perspective.
1023*9880d681SAndroid Build Coastguard Worker 
1024*9880d681SAndroid Build Coastguard Worker   for (NodeAddr<DefNode*> DA : Defs) {
1025*9880d681SAndroid Build Coastguard Worker     if (Visited.count(DA.Id))
1026*9880d681SAndroid Build Coastguard Worker       continue;
1027*9880d681SAndroid Build Coastguard Worker     NodeList Rel = getRelatedRefs(IA, DA);
1028*9880d681SAndroid Build Coastguard Worker     NodeAddr<DefNode*> PDA = Rel.front();
1029*9880d681SAndroid Build Coastguard Worker     // Push the definition on the stack for the register and all aliases.
1030*9880d681SAndroid Build Coastguard Worker     RegisterRef RR = PDA.Addr->getRegRef();
1031*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
1032*9880d681SAndroid Build Coastguard Worker     // Assert if the register is defined in two or more unrelated defs.
1033*9880d681SAndroid Build Coastguard Worker     // This could happen if there are two or more def operands defining it.
1034*9880d681SAndroid Build Coastguard Worker     if (!Defined.insert(RR).second) {
1035*9880d681SAndroid Build Coastguard Worker       auto *MI = NodeAddr<StmtNode*>(IA).Addr->getCode();
1036*9880d681SAndroid Build Coastguard Worker       dbgs() << "Multiple definitions of register: "
1037*9880d681SAndroid Build Coastguard Worker              << Print<RegisterRef>(RR, *this) << " in\n  " << *MI
1038*9880d681SAndroid Build Coastguard Worker              << "in BB#" << MI->getParent()->getNumber() << '\n';
1039*9880d681SAndroid Build Coastguard Worker       llvm_unreachable(nullptr);
1040*9880d681SAndroid Build Coastguard Worker     }
1041*9880d681SAndroid Build Coastguard Worker #endif
1042*9880d681SAndroid Build Coastguard Worker     DefM[RR].push(DA);
1043*9880d681SAndroid Build Coastguard Worker     for (auto A : RAI.getAliasSet(RR)) {
1044*9880d681SAndroid Build Coastguard Worker       assert(A != RR);
1045*9880d681SAndroid Build Coastguard Worker       DefM[A].push(DA);
1046*9880d681SAndroid Build Coastguard Worker     }
1047*9880d681SAndroid Build Coastguard Worker     // Mark all the related defs as visited.
1048*9880d681SAndroid Build Coastguard Worker     for (auto T : Rel)
1049*9880d681SAndroid Build Coastguard Worker       Visited.insert(T.Id);
1050*9880d681SAndroid Build Coastguard Worker   }
1051*9880d681SAndroid Build Coastguard Worker }
1052*9880d681SAndroid Build Coastguard Worker 
1053*9880d681SAndroid Build Coastguard Worker // Return the list of all reference nodes related to RA, including RA itself.
1054*9880d681SAndroid Build Coastguard Worker // See "getNextRelated" for the meaning of a "related reference".
getRelatedRefs(NodeAddr<InstrNode * > IA,NodeAddr<RefNode * > RA) const1055*9880d681SAndroid Build Coastguard Worker NodeList DataFlowGraph::getRelatedRefs(NodeAddr<InstrNode*> IA,
1056*9880d681SAndroid Build Coastguard Worker       NodeAddr<RefNode*> RA) const {
1057*9880d681SAndroid Build Coastguard Worker   assert(IA.Id != 0 && RA.Id != 0);
1058*9880d681SAndroid Build Coastguard Worker 
1059*9880d681SAndroid Build Coastguard Worker   NodeList Refs;
1060*9880d681SAndroid Build Coastguard Worker   NodeId Start = RA.Id;
1061*9880d681SAndroid Build Coastguard Worker   do {
1062*9880d681SAndroid Build Coastguard Worker     Refs.push_back(RA);
1063*9880d681SAndroid Build Coastguard Worker     RA = getNextRelated(IA, RA);
1064*9880d681SAndroid Build Coastguard Worker   } while (RA.Id != 0 && RA.Id != Start);
1065*9880d681SAndroid Build Coastguard Worker   return Refs;
1066*9880d681SAndroid Build Coastguard Worker }
1067*9880d681SAndroid Build Coastguard Worker 
1068*9880d681SAndroid Build Coastguard Worker 
1069*9880d681SAndroid Build Coastguard Worker // Clear all information in the graph.
reset()1070*9880d681SAndroid Build Coastguard Worker void DataFlowGraph::reset() {
1071*9880d681SAndroid Build Coastguard Worker   Memory.clear();
1072*9880d681SAndroid Build Coastguard Worker   Func = NodeAddr<FuncNode*>();
1073*9880d681SAndroid Build Coastguard Worker }
1074*9880d681SAndroid Build Coastguard Worker 
1075*9880d681SAndroid Build Coastguard Worker 
1076*9880d681SAndroid Build Coastguard Worker // Return the next reference node in the instruction node IA that is related
1077*9880d681SAndroid Build Coastguard Worker // to RA. Conceptually, two reference nodes are related if they refer to the
1078*9880d681SAndroid Build Coastguard Worker // same instance of a register access, but differ in flags or other minor
1079*9880d681SAndroid Build Coastguard Worker // characteristics. Specific examples of related nodes are shadow reference
1080*9880d681SAndroid Build Coastguard Worker // nodes.
1081*9880d681SAndroid Build Coastguard Worker // Return the equivalent of nullptr if there are no more related references.
getNextRelated(NodeAddr<InstrNode * > IA,NodeAddr<RefNode * > RA) const1082*9880d681SAndroid Build Coastguard Worker NodeAddr<RefNode*> DataFlowGraph::getNextRelated(NodeAddr<InstrNode*> IA,
1083*9880d681SAndroid Build Coastguard Worker       NodeAddr<RefNode*> RA) const {
1084*9880d681SAndroid Build Coastguard Worker   assert(IA.Id != 0 && RA.Id != 0);
1085*9880d681SAndroid Build Coastguard Worker 
1086*9880d681SAndroid Build Coastguard Worker   auto Related = [RA](NodeAddr<RefNode*> TA) -> bool {
1087*9880d681SAndroid Build Coastguard Worker     if (TA.Addr->getKind() != RA.Addr->getKind())
1088*9880d681SAndroid Build Coastguard Worker       return false;
1089*9880d681SAndroid Build Coastguard Worker     if (TA.Addr->getRegRef() != RA.Addr->getRegRef())
1090*9880d681SAndroid Build Coastguard Worker       return false;
1091*9880d681SAndroid Build Coastguard Worker     return true;
1092*9880d681SAndroid Build Coastguard Worker   };
1093*9880d681SAndroid Build Coastguard Worker   auto RelatedStmt = [&Related,RA](NodeAddr<RefNode*> TA) -> bool {
1094*9880d681SAndroid Build Coastguard Worker     return Related(TA) &&
1095*9880d681SAndroid Build Coastguard Worker            &RA.Addr->getOp() == &TA.Addr->getOp();
1096*9880d681SAndroid Build Coastguard Worker   };
1097*9880d681SAndroid Build Coastguard Worker   auto RelatedPhi = [&Related,RA](NodeAddr<RefNode*> TA) -> bool {
1098*9880d681SAndroid Build Coastguard Worker     if (!Related(TA))
1099*9880d681SAndroid Build Coastguard Worker       return false;
1100*9880d681SAndroid Build Coastguard Worker     if (TA.Addr->getKind() != NodeAttrs::Use)
1101*9880d681SAndroid Build Coastguard Worker       return true;
1102*9880d681SAndroid Build Coastguard Worker     // For phi uses, compare predecessor blocks.
1103*9880d681SAndroid Build Coastguard Worker     const NodeAddr<const PhiUseNode*> TUA = TA;
1104*9880d681SAndroid Build Coastguard Worker     const NodeAddr<const PhiUseNode*> RUA = RA;
1105*9880d681SAndroid Build Coastguard Worker     return TUA.Addr->getPredecessor() == RUA.Addr->getPredecessor();
1106*9880d681SAndroid Build Coastguard Worker   };
1107*9880d681SAndroid Build Coastguard Worker 
1108*9880d681SAndroid Build Coastguard Worker   RegisterRef RR = RA.Addr->getRegRef();
1109*9880d681SAndroid Build Coastguard Worker   if (IA.Addr->getKind() == NodeAttrs::Stmt)
1110*9880d681SAndroid Build Coastguard Worker     return RA.Addr->getNextRef(RR, RelatedStmt, true, *this);
1111*9880d681SAndroid Build Coastguard Worker   return RA.Addr->getNextRef(RR, RelatedPhi, true, *this);
1112*9880d681SAndroid Build Coastguard Worker }
1113*9880d681SAndroid Build Coastguard Worker 
1114*9880d681SAndroid Build Coastguard Worker // Find the next node related to RA in IA that satisfies condition P.
1115*9880d681SAndroid Build Coastguard Worker // If such a node was found, return a pair where the second element is the
1116*9880d681SAndroid Build Coastguard Worker // located node. If such a node does not exist, return a pair where the
1117*9880d681SAndroid Build Coastguard Worker // first element is the element after which such a node should be inserted,
1118*9880d681SAndroid Build Coastguard Worker // and the second element is a null-address.
1119*9880d681SAndroid Build Coastguard Worker template <typename Predicate>
1120*9880d681SAndroid Build Coastguard Worker std::pair<NodeAddr<RefNode*>,NodeAddr<RefNode*>>
locateNextRef(NodeAddr<InstrNode * > IA,NodeAddr<RefNode * > RA,Predicate P) const1121*9880d681SAndroid Build Coastguard Worker DataFlowGraph::locateNextRef(NodeAddr<InstrNode*> IA, NodeAddr<RefNode*> RA,
1122*9880d681SAndroid Build Coastguard Worker       Predicate P) const {
1123*9880d681SAndroid Build Coastguard Worker   assert(IA.Id != 0 && RA.Id != 0);
1124*9880d681SAndroid Build Coastguard Worker 
1125*9880d681SAndroid Build Coastguard Worker   NodeAddr<RefNode*> NA;
1126*9880d681SAndroid Build Coastguard Worker   NodeId Start = RA.Id;
1127*9880d681SAndroid Build Coastguard Worker   while (true) {
1128*9880d681SAndroid Build Coastguard Worker     NA = getNextRelated(IA, RA);
1129*9880d681SAndroid Build Coastguard Worker     if (NA.Id == 0 || NA.Id == Start)
1130*9880d681SAndroid Build Coastguard Worker       break;
1131*9880d681SAndroid Build Coastguard Worker     if (P(NA))
1132*9880d681SAndroid Build Coastguard Worker       break;
1133*9880d681SAndroid Build Coastguard Worker     RA = NA;
1134*9880d681SAndroid Build Coastguard Worker   }
1135*9880d681SAndroid Build Coastguard Worker 
1136*9880d681SAndroid Build Coastguard Worker   if (NA.Id != 0 && NA.Id != Start)
1137*9880d681SAndroid Build Coastguard Worker     return std::make_pair(RA, NA);
1138*9880d681SAndroid Build Coastguard Worker   return std::make_pair(RA, NodeAddr<RefNode*>());
1139*9880d681SAndroid Build Coastguard Worker }
1140*9880d681SAndroid Build Coastguard Worker 
1141*9880d681SAndroid Build Coastguard Worker // Get the next shadow node in IA corresponding to RA, and optionally create
1142*9880d681SAndroid Build Coastguard Worker // such a node if it does not exist.
getNextShadow(NodeAddr<InstrNode * > IA,NodeAddr<RefNode * > RA,bool Create)1143*9880d681SAndroid Build Coastguard Worker NodeAddr<RefNode*> DataFlowGraph::getNextShadow(NodeAddr<InstrNode*> IA,
1144*9880d681SAndroid Build Coastguard Worker       NodeAddr<RefNode*> RA, bool Create) {
1145*9880d681SAndroid Build Coastguard Worker   assert(IA.Id != 0 && RA.Id != 0);
1146*9880d681SAndroid Build Coastguard Worker 
1147*9880d681SAndroid Build Coastguard Worker   uint16_t Flags = RA.Addr->getFlags() | NodeAttrs::Shadow;
1148*9880d681SAndroid Build Coastguard Worker   auto IsShadow = [Flags] (NodeAddr<RefNode*> TA) -> bool {
1149*9880d681SAndroid Build Coastguard Worker     return TA.Addr->getFlags() == Flags;
1150*9880d681SAndroid Build Coastguard Worker   };
1151*9880d681SAndroid Build Coastguard Worker   auto Loc = locateNextRef(IA, RA, IsShadow);
1152*9880d681SAndroid Build Coastguard Worker   if (Loc.second.Id != 0 || !Create)
1153*9880d681SAndroid Build Coastguard Worker     return Loc.second;
1154*9880d681SAndroid Build Coastguard Worker 
1155*9880d681SAndroid Build Coastguard Worker   // Create a copy of RA and mark is as shadow.
1156*9880d681SAndroid Build Coastguard Worker   NodeAddr<RefNode*> NA = cloneNode(RA);
1157*9880d681SAndroid Build Coastguard Worker   NA.Addr->setFlags(Flags | NodeAttrs::Shadow);
1158*9880d681SAndroid Build Coastguard Worker   IA.Addr->addMemberAfter(Loc.first, NA, *this);
1159*9880d681SAndroid Build Coastguard Worker   return NA;
1160*9880d681SAndroid Build Coastguard Worker }
1161*9880d681SAndroid Build Coastguard Worker 
1162*9880d681SAndroid Build Coastguard Worker // Get the next shadow node in IA corresponding to RA. Return null-address
1163*9880d681SAndroid Build Coastguard Worker // if such a node does not exist.
getNextShadow(NodeAddr<InstrNode * > IA,NodeAddr<RefNode * > RA) const1164*9880d681SAndroid Build Coastguard Worker NodeAddr<RefNode*> DataFlowGraph::getNextShadow(NodeAddr<InstrNode*> IA,
1165*9880d681SAndroid Build Coastguard Worker       NodeAddr<RefNode*> RA) const {
1166*9880d681SAndroid Build Coastguard Worker   assert(IA.Id != 0 && RA.Id != 0);
1167*9880d681SAndroid Build Coastguard Worker   uint16_t Flags = RA.Addr->getFlags() | NodeAttrs::Shadow;
1168*9880d681SAndroid Build Coastguard Worker   auto IsShadow = [Flags] (NodeAddr<RefNode*> TA) -> bool {
1169*9880d681SAndroid Build Coastguard Worker     return TA.Addr->getFlags() == Flags;
1170*9880d681SAndroid Build Coastguard Worker   };
1171*9880d681SAndroid Build Coastguard Worker   return locateNextRef(IA, RA, IsShadow).second;
1172*9880d681SAndroid Build Coastguard Worker }
1173*9880d681SAndroid Build Coastguard Worker 
1174*9880d681SAndroid Build Coastguard Worker // Create a new statement node in the block node BA that corresponds to
1175*9880d681SAndroid Build Coastguard Worker // the machine instruction MI.
buildStmt(NodeAddr<BlockNode * > BA,MachineInstr & In)1176*9880d681SAndroid Build Coastguard Worker void DataFlowGraph::buildStmt(NodeAddr<BlockNode*> BA, MachineInstr &In) {
1177*9880d681SAndroid Build Coastguard Worker   auto SA = newStmt(BA, &In);
1178*9880d681SAndroid Build Coastguard Worker 
1179*9880d681SAndroid Build Coastguard Worker   auto isCall = [] (const MachineInstr &In) -> bool {
1180*9880d681SAndroid Build Coastguard Worker     if (In.isCall())
1181*9880d681SAndroid Build Coastguard Worker       return true;
1182*9880d681SAndroid Build Coastguard Worker     // Is tail call?
1183*9880d681SAndroid Build Coastguard Worker     if (In.isBranch())
1184*9880d681SAndroid Build Coastguard Worker       for (auto &Op : In.operands())
1185*9880d681SAndroid Build Coastguard Worker         if (Op.isGlobal() || Op.isSymbol())
1186*9880d681SAndroid Build Coastguard Worker           return true;
1187*9880d681SAndroid Build Coastguard Worker     return false;
1188*9880d681SAndroid Build Coastguard Worker   };
1189*9880d681SAndroid Build Coastguard Worker 
1190*9880d681SAndroid Build Coastguard Worker   // Collect a set of registers that this instruction implicitly uses
1191*9880d681SAndroid Build Coastguard Worker   // or defines. Implicit operands from an instruction will be ignored
1192*9880d681SAndroid Build Coastguard Worker   // unless they are listed here.
1193*9880d681SAndroid Build Coastguard Worker   RegisterSet ImpUses, ImpDefs;
1194*9880d681SAndroid Build Coastguard Worker   if (const uint16_t *ImpD = In.getDesc().getImplicitDefs())
1195*9880d681SAndroid Build Coastguard Worker     while (uint16_t R = *ImpD++)
1196*9880d681SAndroid Build Coastguard Worker       ImpDefs.insert({R, 0});
1197*9880d681SAndroid Build Coastguard Worker   if (const uint16_t *ImpU = In.getDesc().getImplicitUses())
1198*9880d681SAndroid Build Coastguard Worker     while (uint16_t R = *ImpU++)
1199*9880d681SAndroid Build Coastguard Worker       ImpUses.insert({R, 0});
1200*9880d681SAndroid Build Coastguard Worker 
1201*9880d681SAndroid Build Coastguard Worker   bool NeedsImplicit = isCall(In) || In.isInlineAsm() || In.isReturn();
1202*9880d681SAndroid Build Coastguard Worker   bool IsPredicated = TII.isPredicated(In);
1203*9880d681SAndroid Build Coastguard Worker   unsigned NumOps = In.getNumOperands();
1204*9880d681SAndroid Build Coastguard Worker 
1205*9880d681SAndroid Build Coastguard Worker   // Avoid duplicate implicit defs. This will not detect cases of implicit
1206*9880d681SAndroid Build Coastguard Worker   // defs that define registers that overlap, but it is not clear how to
1207*9880d681SAndroid Build Coastguard Worker   // interpret that in the absence of explicit defs. Overlapping explicit
1208*9880d681SAndroid Build Coastguard Worker   // defs are likely illegal already.
1209*9880d681SAndroid Build Coastguard Worker   RegisterSet DoneDefs;
1210*9880d681SAndroid Build Coastguard Worker   // Process explicit defs first.
1211*9880d681SAndroid Build Coastguard Worker   for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1212*9880d681SAndroid Build Coastguard Worker     MachineOperand &Op = In.getOperand(OpN);
1213*9880d681SAndroid Build Coastguard Worker     if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1214*9880d681SAndroid Build Coastguard Worker       continue;
1215*9880d681SAndroid Build Coastguard Worker     RegisterRef RR = { Op.getReg(), Op.getSubReg() };
1216*9880d681SAndroid Build Coastguard Worker     uint16_t Flags = NodeAttrs::None;
1217*9880d681SAndroid Build Coastguard Worker     if (TOI.isPreserving(In, OpN))
1218*9880d681SAndroid Build Coastguard Worker       Flags |= NodeAttrs::Preserving;
1219*9880d681SAndroid Build Coastguard Worker     if (TOI.isClobbering(In, OpN))
1220*9880d681SAndroid Build Coastguard Worker       Flags |= NodeAttrs::Clobbering;
1221*9880d681SAndroid Build Coastguard Worker     if (TOI.isFixedReg(In, OpN))
1222*9880d681SAndroid Build Coastguard Worker       Flags |= NodeAttrs::Fixed;
1223*9880d681SAndroid Build Coastguard Worker     NodeAddr<DefNode*> DA = newDef(SA, Op, Flags);
1224*9880d681SAndroid Build Coastguard Worker     SA.Addr->addMember(DA, *this);
1225*9880d681SAndroid Build Coastguard Worker     DoneDefs.insert(RR);
1226*9880d681SAndroid Build Coastguard Worker   }
1227*9880d681SAndroid Build Coastguard Worker 
1228*9880d681SAndroid Build Coastguard Worker   // Process implicit defs, skipping those that have already been added
1229*9880d681SAndroid Build Coastguard Worker   // as explicit.
1230*9880d681SAndroid Build Coastguard Worker   for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1231*9880d681SAndroid Build Coastguard Worker     MachineOperand &Op = In.getOperand(OpN);
1232*9880d681SAndroid Build Coastguard Worker     if (!Op.isReg() || !Op.isDef() || !Op.isImplicit())
1233*9880d681SAndroid Build Coastguard Worker       continue;
1234*9880d681SAndroid Build Coastguard Worker     RegisterRef RR = { Op.getReg(), Op.getSubReg() };
1235*9880d681SAndroid Build Coastguard Worker     if (!NeedsImplicit && !ImpDefs.count(RR))
1236*9880d681SAndroid Build Coastguard Worker       continue;
1237*9880d681SAndroid Build Coastguard Worker     if (DoneDefs.count(RR))
1238*9880d681SAndroid Build Coastguard Worker       continue;
1239*9880d681SAndroid Build Coastguard Worker     uint16_t Flags = NodeAttrs::None;
1240*9880d681SAndroid Build Coastguard Worker     if (TOI.isPreserving(In, OpN))
1241*9880d681SAndroid Build Coastguard Worker       Flags |= NodeAttrs::Preserving;
1242*9880d681SAndroid Build Coastguard Worker     if (TOI.isClobbering(In, OpN))
1243*9880d681SAndroid Build Coastguard Worker       Flags |= NodeAttrs::Clobbering;
1244*9880d681SAndroid Build Coastguard Worker     if (TOI.isFixedReg(In, OpN))
1245*9880d681SAndroid Build Coastguard Worker       Flags |= NodeAttrs::Fixed;
1246*9880d681SAndroid Build Coastguard Worker     NodeAddr<DefNode*> DA = newDef(SA, Op, Flags);
1247*9880d681SAndroid Build Coastguard Worker     SA.Addr->addMember(DA, *this);
1248*9880d681SAndroid Build Coastguard Worker     DoneDefs.insert(RR);
1249*9880d681SAndroid Build Coastguard Worker   }
1250*9880d681SAndroid Build Coastguard Worker 
1251*9880d681SAndroid Build Coastguard Worker   for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1252*9880d681SAndroid Build Coastguard Worker     MachineOperand &Op = In.getOperand(OpN);
1253*9880d681SAndroid Build Coastguard Worker     if (!Op.isReg() || !Op.isUse())
1254*9880d681SAndroid Build Coastguard Worker       continue;
1255*9880d681SAndroid Build Coastguard Worker     RegisterRef RR = { Op.getReg(), Op.getSubReg() };
1256*9880d681SAndroid Build Coastguard Worker     // Add implicit uses on return and call instructions, and on predicated
1257*9880d681SAndroid Build Coastguard Worker     // instructions regardless of whether or not they appear in the instruction
1258*9880d681SAndroid Build Coastguard Worker     // descriptor's list.
1259*9880d681SAndroid Build Coastguard Worker     bool Implicit = Op.isImplicit();
1260*9880d681SAndroid Build Coastguard Worker     bool TakeImplicit = NeedsImplicit || IsPredicated;
1261*9880d681SAndroid Build Coastguard Worker     if (Implicit && !TakeImplicit && !ImpUses.count(RR))
1262*9880d681SAndroid Build Coastguard Worker       continue;
1263*9880d681SAndroid Build Coastguard Worker     uint16_t Flags = NodeAttrs::None;
1264*9880d681SAndroid Build Coastguard Worker     if (TOI.isFixedReg(In, OpN))
1265*9880d681SAndroid Build Coastguard Worker       Flags |= NodeAttrs::Fixed;
1266*9880d681SAndroid Build Coastguard Worker     NodeAddr<UseNode*> UA = newUse(SA, Op, Flags);
1267*9880d681SAndroid Build Coastguard Worker     SA.Addr->addMember(UA, *this);
1268*9880d681SAndroid Build Coastguard Worker   }
1269*9880d681SAndroid Build Coastguard Worker }
1270*9880d681SAndroid Build Coastguard Worker 
1271*9880d681SAndroid Build Coastguard Worker // Build a map that for each block will have the set of all references from
1272*9880d681SAndroid Build Coastguard Worker // that block, and from all blocks dominated by it.
buildBlockRefs(NodeAddr<BlockNode * > BA,BlockRefsMap & RefM)1273*9880d681SAndroid Build Coastguard Worker void DataFlowGraph::buildBlockRefs(NodeAddr<BlockNode*> BA,
1274*9880d681SAndroid Build Coastguard Worker       BlockRefsMap &RefM) {
1275*9880d681SAndroid Build Coastguard Worker   auto &Refs = RefM[BA.Id];
1276*9880d681SAndroid Build Coastguard Worker   MachineDomTreeNode *N = MDT.getNode(BA.Addr->getCode());
1277*9880d681SAndroid Build Coastguard Worker   assert(N);
1278*9880d681SAndroid Build Coastguard Worker   for (auto I : *N) {
1279*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *SB = I->getBlock();
1280*9880d681SAndroid Build Coastguard Worker     auto SBA = Func.Addr->findBlock(SB, *this);
1281*9880d681SAndroid Build Coastguard Worker     buildBlockRefs(SBA, RefM);
1282*9880d681SAndroid Build Coastguard Worker     const auto &SRs = RefM[SBA.Id];
1283*9880d681SAndroid Build Coastguard Worker     Refs.insert(SRs.begin(), SRs.end());
1284*9880d681SAndroid Build Coastguard Worker   }
1285*9880d681SAndroid Build Coastguard Worker 
1286*9880d681SAndroid Build Coastguard Worker   for (NodeAddr<InstrNode*> IA : BA.Addr->members(*this))
1287*9880d681SAndroid Build Coastguard Worker     for (NodeAddr<RefNode*> RA : IA.Addr->members(*this))
1288*9880d681SAndroid Build Coastguard Worker       Refs.insert(RA.Addr->getRegRef());
1289*9880d681SAndroid Build Coastguard Worker }
1290*9880d681SAndroid Build Coastguard Worker 
1291*9880d681SAndroid Build Coastguard Worker // Scan all defs in the block node BA and record in PhiM the locations of
1292*9880d681SAndroid Build Coastguard Worker // phi nodes corresponding to these defs.
recordDefsForDF(BlockRefsMap & PhiM,BlockRefsMap & RefM,NodeAddr<BlockNode * > BA)1293*9880d681SAndroid Build Coastguard Worker void DataFlowGraph::recordDefsForDF(BlockRefsMap &PhiM, BlockRefsMap &RefM,
1294*9880d681SAndroid Build Coastguard Worker       NodeAddr<BlockNode*> BA) {
1295*9880d681SAndroid Build Coastguard Worker   // Check all defs from block BA and record them in each block in BA's
1296*9880d681SAndroid Build Coastguard Worker   // iterated dominance frontier. This information will later be used to
1297*9880d681SAndroid Build Coastguard Worker   // create phi nodes.
1298*9880d681SAndroid Build Coastguard Worker   MachineBasicBlock *BB = BA.Addr->getCode();
1299*9880d681SAndroid Build Coastguard Worker   assert(BB);
1300*9880d681SAndroid Build Coastguard Worker   auto DFLoc = MDF.find(BB);
1301*9880d681SAndroid Build Coastguard Worker   if (DFLoc == MDF.end() || DFLoc->second.empty())
1302*9880d681SAndroid Build Coastguard Worker     return;
1303*9880d681SAndroid Build Coastguard Worker 
1304*9880d681SAndroid Build Coastguard Worker   // Traverse all instructions in the block and collect the set of all
1305*9880d681SAndroid Build Coastguard Worker   // defined references. For each reference there will be a phi created
1306*9880d681SAndroid Build Coastguard Worker   // in the block's iterated dominance frontier.
1307*9880d681SAndroid Build Coastguard Worker   // This is done to make sure that each defined reference gets only one
1308*9880d681SAndroid Build Coastguard Worker   // phi node, even if it is defined multiple times.
1309*9880d681SAndroid Build Coastguard Worker   RegisterSet Defs;
1310*9880d681SAndroid Build Coastguard Worker   for (auto I : BA.Addr->members(*this)) {
1311*9880d681SAndroid Build Coastguard Worker     assert(I.Addr->getType() == NodeAttrs::Code);
1312*9880d681SAndroid Build Coastguard Worker     assert(I.Addr->getKind() == NodeAttrs::Phi ||
1313*9880d681SAndroid Build Coastguard Worker            I.Addr->getKind() == NodeAttrs::Stmt);
1314*9880d681SAndroid Build Coastguard Worker     NodeAddr<InstrNode*> IA = I;
1315*9880d681SAndroid Build Coastguard Worker     for (NodeAddr<RefNode*> RA : IA.Addr->members_if(IsDef, *this))
1316*9880d681SAndroid Build Coastguard Worker       Defs.insert(RA.Addr->getRegRef());
1317*9880d681SAndroid Build Coastguard Worker   }
1318*9880d681SAndroid Build Coastguard Worker 
1319*9880d681SAndroid Build Coastguard Worker   // Finally, add the set of defs to each block in the iterated dominance
1320*9880d681SAndroid Build Coastguard Worker   // frontier.
1321*9880d681SAndroid Build Coastguard Worker   const MachineDominanceFrontier::DomSetType &DF = DFLoc->second;
1322*9880d681SAndroid Build Coastguard Worker   SetVector<MachineBasicBlock*> IDF(DF.begin(), DF.end());
1323*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0; i < IDF.size(); ++i) {
1324*9880d681SAndroid Build Coastguard Worker     auto F = MDF.find(IDF[i]);
1325*9880d681SAndroid Build Coastguard Worker     if (F != MDF.end())
1326*9880d681SAndroid Build Coastguard Worker       IDF.insert(F->second.begin(), F->second.end());
1327*9880d681SAndroid Build Coastguard Worker   }
1328*9880d681SAndroid Build Coastguard Worker 
1329*9880d681SAndroid Build Coastguard Worker   // Get the register references that are reachable from this block.
1330*9880d681SAndroid Build Coastguard Worker   RegisterSet &Refs = RefM[BA.Id];
1331*9880d681SAndroid Build Coastguard Worker   for (auto DB : IDF) {
1332*9880d681SAndroid Build Coastguard Worker     auto DBA = Func.Addr->findBlock(DB, *this);
1333*9880d681SAndroid Build Coastguard Worker     const auto &Rs = RefM[DBA.Id];
1334*9880d681SAndroid Build Coastguard Worker     Refs.insert(Rs.begin(), Rs.end());
1335*9880d681SAndroid Build Coastguard Worker   }
1336*9880d681SAndroid Build Coastguard Worker 
1337*9880d681SAndroid Build Coastguard Worker   for (auto DB : IDF) {
1338*9880d681SAndroid Build Coastguard Worker     auto DBA = Func.Addr->findBlock(DB, *this);
1339*9880d681SAndroid Build Coastguard Worker     PhiM[DBA.Id].insert(Defs.begin(), Defs.end());
1340*9880d681SAndroid Build Coastguard Worker   }
1341*9880d681SAndroid Build Coastguard Worker }
1342*9880d681SAndroid Build Coastguard Worker 
1343*9880d681SAndroid Build Coastguard Worker // Given the locations of phi nodes in the map PhiM, create the phi nodes
1344*9880d681SAndroid Build Coastguard Worker // that are located in the block node BA.
buildPhis(BlockRefsMap & PhiM,BlockRefsMap & RefM,NodeAddr<BlockNode * > BA)1345*9880d681SAndroid Build Coastguard Worker void DataFlowGraph::buildPhis(BlockRefsMap &PhiM, BlockRefsMap &RefM,
1346*9880d681SAndroid Build Coastguard Worker       NodeAddr<BlockNode*> BA) {
1347*9880d681SAndroid Build Coastguard Worker   // Check if this blocks has any DF defs, i.e. if there are any defs
1348*9880d681SAndroid Build Coastguard Worker   // that this block is in the iterated dominance frontier of.
1349*9880d681SAndroid Build Coastguard Worker   auto HasDF = PhiM.find(BA.Id);
1350*9880d681SAndroid Build Coastguard Worker   if (HasDF == PhiM.end() || HasDF->second.empty())
1351*9880d681SAndroid Build Coastguard Worker     return;
1352*9880d681SAndroid Build Coastguard Worker 
1353*9880d681SAndroid Build Coastguard Worker   // First, remove all R in Refs in such that there exists T in Refs
1354*9880d681SAndroid Build Coastguard Worker   // such that T covers R. In other words, only leave those refs that
1355*9880d681SAndroid Build Coastguard Worker   // are not covered by another ref (i.e. maximal with respect to covering).
1356*9880d681SAndroid Build Coastguard Worker 
1357*9880d681SAndroid Build Coastguard Worker   auto MaxCoverIn = [this] (RegisterRef RR, RegisterSet &RRs) -> RegisterRef {
1358*9880d681SAndroid Build Coastguard Worker     for (auto I : RRs)
1359*9880d681SAndroid Build Coastguard Worker       if (I != RR && RAI.covers(I, RR))
1360*9880d681SAndroid Build Coastguard Worker         RR = I;
1361*9880d681SAndroid Build Coastguard Worker     return RR;
1362*9880d681SAndroid Build Coastguard Worker   };
1363*9880d681SAndroid Build Coastguard Worker 
1364*9880d681SAndroid Build Coastguard Worker   RegisterSet MaxDF;
1365*9880d681SAndroid Build Coastguard Worker   for (auto I : HasDF->second)
1366*9880d681SAndroid Build Coastguard Worker     MaxDF.insert(MaxCoverIn(I, HasDF->second));
1367*9880d681SAndroid Build Coastguard Worker 
1368*9880d681SAndroid Build Coastguard Worker   std::vector<RegisterRef> MaxRefs;
1369*9880d681SAndroid Build Coastguard Worker   auto &RefB = RefM[BA.Id];
1370*9880d681SAndroid Build Coastguard Worker   for (auto I : MaxDF)
1371*9880d681SAndroid Build Coastguard Worker     MaxRefs.push_back(MaxCoverIn(I, RefB));
1372*9880d681SAndroid Build Coastguard Worker 
1373*9880d681SAndroid Build Coastguard Worker   // Now, for each R in MaxRefs, get the alias closure of R. If the closure
1374*9880d681SAndroid Build Coastguard Worker   // only has R in it, create a phi a def for R. Otherwise, create a phi,
1375*9880d681SAndroid Build Coastguard Worker   // and add a def for each S in the closure.
1376*9880d681SAndroid Build Coastguard Worker 
1377*9880d681SAndroid Build Coastguard Worker   // Sort the refs so that the phis will be created in a deterministic order.
1378*9880d681SAndroid Build Coastguard Worker   std::sort(MaxRefs.begin(), MaxRefs.end());
1379*9880d681SAndroid Build Coastguard Worker   // Remove duplicates.
1380*9880d681SAndroid Build Coastguard Worker   auto NewEnd = std::unique(MaxRefs.begin(), MaxRefs.end());
1381*9880d681SAndroid Build Coastguard Worker   MaxRefs.erase(NewEnd, MaxRefs.end());
1382*9880d681SAndroid Build Coastguard Worker 
1383*9880d681SAndroid Build Coastguard Worker   auto Aliased = [this,&MaxRefs](RegisterRef RR,
1384*9880d681SAndroid Build Coastguard Worker                                  std::vector<unsigned> &Closure) -> bool {
1385*9880d681SAndroid Build Coastguard Worker     for (auto I : Closure)
1386*9880d681SAndroid Build Coastguard Worker       if (RAI.alias(RR, MaxRefs[I]))
1387*9880d681SAndroid Build Coastguard Worker         return true;
1388*9880d681SAndroid Build Coastguard Worker     return false;
1389*9880d681SAndroid Build Coastguard Worker   };
1390*9880d681SAndroid Build Coastguard Worker 
1391*9880d681SAndroid Build Coastguard Worker   // Prepare a list of NodeIds of the block's predecessors.
1392*9880d681SAndroid Build Coastguard Worker   std::vector<NodeId> PredList;
1393*9880d681SAndroid Build Coastguard Worker   const MachineBasicBlock *MBB = BA.Addr->getCode();
1394*9880d681SAndroid Build Coastguard Worker   for (auto PB : MBB->predecessors()) {
1395*9880d681SAndroid Build Coastguard Worker     auto B = Func.Addr->findBlock(PB, *this);
1396*9880d681SAndroid Build Coastguard Worker     PredList.push_back(B.Id);
1397*9880d681SAndroid Build Coastguard Worker   }
1398*9880d681SAndroid Build Coastguard Worker 
1399*9880d681SAndroid Build Coastguard Worker   while (!MaxRefs.empty()) {
1400*9880d681SAndroid Build Coastguard Worker     // Put the first element in the closure, and then add all subsequent
1401*9880d681SAndroid Build Coastguard Worker     // elements from MaxRefs to it, if they alias at least one element
1402*9880d681SAndroid Build Coastguard Worker     // already in the closure.
1403*9880d681SAndroid Build Coastguard Worker     // ClosureIdx: vector of indices in MaxRefs of members of the closure.
1404*9880d681SAndroid Build Coastguard Worker     std::vector<unsigned> ClosureIdx = { 0 };
1405*9880d681SAndroid Build Coastguard Worker     for (unsigned i = 1; i != MaxRefs.size(); ++i)
1406*9880d681SAndroid Build Coastguard Worker       if (Aliased(MaxRefs[i], ClosureIdx))
1407*9880d681SAndroid Build Coastguard Worker         ClosureIdx.push_back(i);
1408*9880d681SAndroid Build Coastguard Worker 
1409*9880d681SAndroid Build Coastguard Worker     // Build a phi for the closure.
1410*9880d681SAndroid Build Coastguard Worker     unsigned CS = ClosureIdx.size();
1411*9880d681SAndroid Build Coastguard Worker     NodeAddr<PhiNode*> PA = newPhi(BA);
1412*9880d681SAndroid Build Coastguard Worker 
1413*9880d681SAndroid Build Coastguard Worker     // Add defs.
1414*9880d681SAndroid Build Coastguard Worker     for (unsigned X = 0; X != CS; ++X) {
1415*9880d681SAndroid Build Coastguard Worker       RegisterRef RR = MaxRefs[ClosureIdx[X]];
1416*9880d681SAndroid Build Coastguard Worker       uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving;
1417*9880d681SAndroid Build Coastguard Worker       NodeAddr<DefNode*> DA = newDef(PA, RR, PhiFlags);
1418*9880d681SAndroid Build Coastguard Worker       PA.Addr->addMember(DA, *this);
1419*9880d681SAndroid Build Coastguard Worker     }
1420*9880d681SAndroid Build Coastguard Worker     // Add phi uses.
1421*9880d681SAndroid Build Coastguard Worker     for (auto P : PredList) {
1422*9880d681SAndroid Build Coastguard Worker       auto PBA = addr<BlockNode*>(P);
1423*9880d681SAndroid Build Coastguard Worker       for (unsigned X = 0; X != CS; ++X) {
1424*9880d681SAndroid Build Coastguard Worker         RegisterRef RR = MaxRefs[ClosureIdx[X]];
1425*9880d681SAndroid Build Coastguard Worker         NodeAddr<PhiUseNode*> PUA = newPhiUse(PA, RR, PBA);
1426*9880d681SAndroid Build Coastguard Worker         PA.Addr->addMember(PUA, *this);
1427*9880d681SAndroid Build Coastguard Worker       }
1428*9880d681SAndroid Build Coastguard Worker     }
1429*9880d681SAndroid Build Coastguard Worker 
1430*9880d681SAndroid Build Coastguard Worker     // Erase from MaxRefs all elements in the closure.
1431*9880d681SAndroid Build Coastguard Worker     auto Begin = MaxRefs.begin();
1432*9880d681SAndroid Build Coastguard Worker     for (unsigned i = ClosureIdx.size(); i != 0; --i)
1433*9880d681SAndroid Build Coastguard Worker       MaxRefs.erase(Begin + ClosureIdx[i-1]);
1434*9880d681SAndroid Build Coastguard Worker   }
1435*9880d681SAndroid Build Coastguard Worker }
1436*9880d681SAndroid Build Coastguard Worker 
1437*9880d681SAndroid Build Coastguard Worker // Remove any unneeded phi nodes that were created during the build process.
removeUnusedPhis()1438*9880d681SAndroid Build Coastguard Worker void DataFlowGraph::removeUnusedPhis() {
1439*9880d681SAndroid Build Coastguard Worker   // This will remove unused phis, i.e. phis where each def does not reach
1440*9880d681SAndroid Build Coastguard Worker   // any uses or other defs. This will not detect or remove circular phi
1441*9880d681SAndroid Build Coastguard Worker   // chains that are otherwise dead. Unused/dead phis are created during
1442*9880d681SAndroid Build Coastguard Worker   // the build process and this function is intended to remove these cases
1443*9880d681SAndroid Build Coastguard Worker   // that are easily determinable to be unnecessary.
1444*9880d681SAndroid Build Coastguard Worker 
1445*9880d681SAndroid Build Coastguard Worker   SetVector<NodeId> PhiQ;
1446*9880d681SAndroid Build Coastguard Worker   for (NodeAddr<BlockNode*> BA : Func.Addr->members(*this)) {
1447*9880d681SAndroid Build Coastguard Worker     for (auto P : BA.Addr->members_if(IsPhi, *this))
1448*9880d681SAndroid Build Coastguard Worker       PhiQ.insert(P.Id);
1449*9880d681SAndroid Build Coastguard Worker   }
1450*9880d681SAndroid Build Coastguard Worker 
1451*9880d681SAndroid Build Coastguard Worker   static auto HasUsedDef = [](NodeList &Ms) -> bool {
1452*9880d681SAndroid Build Coastguard Worker     for (auto M : Ms) {
1453*9880d681SAndroid Build Coastguard Worker       if (M.Addr->getKind() != NodeAttrs::Def)
1454*9880d681SAndroid Build Coastguard Worker         continue;
1455*9880d681SAndroid Build Coastguard Worker       NodeAddr<DefNode*> DA = M;
1456*9880d681SAndroid Build Coastguard Worker       if (DA.Addr->getReachedDef() != 0 || DA.Addr->getReachedUse() != 0)
1457*9880d681SAndroid Build Coastguard Worker         return true;
1458*9880d681SAndroid Build Coastguard Worker     }
1459*9880d681SAndroid Build Coastguard Worker     return false;
1460*9880d681SAndroid Build Coastguard Worker   };
1461*9880d681SAndroid Build Coastguard Worker 
1462*9880d681SAndroid Build Coastguard Worker   // Any phi, if it is removed, may affect other phis (make them dead).
1463*9880d681SAndroid Build Coastguard Worker   // For each removed phi, collect the potentially affected phis and add
1464*9880d681SAndroid Build Coastguard Worker   // them back to the queue.
1465*9880d681SAndroid Build Coastguard Worker   while (!PhiQ.empty()) {
1466*9880d681SAndroid Build Coastguard Worker     auto PA = addr<PhiNode*>(PhiQ[0]);
1467*9880d681SAndroid Build Coastguard Worker     PhiQ.remove(PA.Id);
1468*9880d681SAndroid Build Coastguard Worker     NodeList Refs = PA.Addr->members(*this);
1469*9880d681SAndroid Build Coastguard Worker     if (HasUsedDef(Refs))
1470*9880d681SAndroid Build Coastguard Worker       continue;
1471*9880d681SAndroid Build Coastguard Worker     for (NodeAddr<RefNode*> RA : Refs) {
1472*9880d681SAndroid Build Coastguard Worker       if (NodeId RD = RA.Addr->getReachingDef()) {
1473*9880d681SAndroid Build Coastguard Worker         auto RDA = addr<DefNode*>(RD);
1474*9880d681SAndroid Build Coastguard Worker         NodeAddr<InstrNode*> OA = RDA.Addr->getOwner(*this);
1475*9880d681SAndroid Build Coastguard Worker         if (IsPhi(OA))
1476*9880d681SAndroid Build Coastguard Worker           PhiQ.insert(OA.Id);
1477*9880d681SAndroid Build Coastguard Worker       }
1478*9880d681SAndroid Build Coastguard Worker       if (RA.Addr->isDef())
1479*9880d681SAndroid Build Coastguard Worker         unlinkDef(RA, true);
1480*9880d681SAndroid Build Coastguard Worker       else
1481*9880d681SAndroid Build Coastguard Worker         unlinkUse(RA, true);
1482*9880d681SAndroid Build Coastguard Worker     }
1483*9880d681SAndroid Build Coastguard Worker     NodeAddr<BlockNode*> BA = PA.Addr->getOwner(*this);
1484*9880d681SAndroid Build Coastguard Worker     BA.Addr->removeMember(PA, *this);
1485*9880d681SAndroid Build Coastguard Worker   }
1486*9880d681SAndroid Build Coastguard Worker }
1487*9880d681SAndroid Build Coastguard Worker 
1488*9880d681SAndroid Build Coastguard Worker // For a given reference node TA in an instruction node IA, connect the
1489*9880d681SAndroid Build Coastguard Worker // reaching def of TA to the appropriate def node. Create any shadow nodes
1490*9880d681SAndroid Build Coastguard Worker // as appropriate.
1491*9880d681SAndroid Build Coastguard Worker template <typename T>
linkRefUp(NodeAddr<InstrNode * > IA,NodeAddr<T> TA,DefStack & DS)1492*9880d681SAndroid Build Coastguard Worker void DataFlowGraph::linkRefUp(NodeAddr<InstrNode*> IA, NodeAddr<T> TA,
1493*9880d681SAndroid Build Coastguard Worker       DefStack &DS) {
1494*9880d681SAndroid Build Coastguard Worker   if (DS.empty())
1495*9880d681SAndroid Build Coastguard Worker     return;
1496*9880d681SAndroid Build Coastguard Worker   RegisterRef RR = TA.Addr->getRegRef();
1497*9880d681SAndroid Build Coastguard Worker   NodeAddr<T> TAP;
1498*9880d681SAndroid Build Coastguard Worker 
1499*9880d681SAndroid Build Coastguard Worker   // References from the def stack that have been examined so far.
1500*9880d681SAndroid Build Coastguard Worker   RegisterSet Defs;
1501*9880d681SAndroid Build Coastguard Worker 
1502*9880d681SAndroid Build Coastguard Worker   for (auto I = DS.top(), E = DS.bottom(); I != E; I.down()) {
1503*9880d681SAndroid Build Coastguard Worker     RegisterRef QR = I->Addr->getRegRef();
1504*9880d681SAndroid Build Coastguard Worker     auto AliasQR = [QR,this] (RegisterRef RR) -> bool {
1505*9880d681SAndroid Build Coastguard Worker       return RAI.alias(QR, RR);
1506*9880d681SAndroid Build Coastguard Worker     };
1507*9880d681SAndroid Build Coastguard Worker     bool PrecUp = RAI.covers(QR, RR);
1508*9880d681SAndroid Build Coastguard Worker     // Skip all defs that are aliased to any of the defs that we have already
1509*9880d681SAndroid Build Coastguard Worker     // seen. If we encounter a covering def, stop the stack traversal early.
1510*9880d681SAndroid Build Coastguard Worker     if (std::any_of(Defs.begin(), Defs.end(), AliasQR)) {
1511*9880d681SAndroid Build Coastguard Worker       if (PrecUp)
1512*9880d681SAndroid Build Coastguard Worker         break;
1513*9880d681SAndroid Build Coastguard Worker       continue;
1514*9880d681SAndroid Build Coastguard Worker     }
1515*9880d681SAndroid Build Coastguard Worker     // The reaching def.
1516*9880d681SAndroid Build Coastguard Worker     NodeAddr<DefNode*> RDA = *I;
1517*9880d681SAndroid Build Coastguard Worker 
1518*9880d681SAndroid Build Coastguard Worker     // Pick the reached node.
1519*9880d681SAndroid Build Coastguard Worker     if (TAP.Id == 0) {
1520*9880d681SAndroid Build Coastguard Worker       TAP = TA;
1521*9880d681SAndroid Build Coastguard Worker     } else {
1522*9880d681SAndroid Build Coastguard Worker       // Mark the existing ref as "shadow" and create a new shadow.
1523*9880d681SAndroid Build Coastguard Worker       TAP.Addr->setFlags(TAP.Addr->getFlags() | NodeAttrs::Shadow);
1524*9880d681SAndroid Build Coastguard Worker       TAP = getNextShadow(IA, TAP, true);
1525*9880d681SAndroid Build Coastguard Worker     }
1526*9880d681SAndroid Build Coastguard Worker 
1527*9880d681SAndroid Build Coastguard Worker     // Create the link.
1528*9880d681SAndroid Build Coastguard Worker     TAP.Addr->linkToDef(TAP.Id, RDA);
1529*9880d681SAndroid Build Coastguard Worker 
1530*9880d681SAndroid Build Coastguard Worker     if (PrecUp)
1531*9880d681SAndroid Build Coastguard Worker       break;
1532*9880d681SAndroid Build Coastguard Worker     Defs.insert(QR);
1533*9880d681SAndroid Build Coastguard Worker   }
1534*9880d681SAndroid Build Coastguard Worker }
1535*9880d681SAndroid Build Coastguard Worker 
1536*9880d681SAndroid Build Coastguard Worker // Create data-flow links for all reference nodes in the statement node SA.
linkStmtRefs(DefStackMap & DefM,NodeAddr<StmtNode * > SA)1537*9880d681SAndroid Build Coastguard Worker void DataFlowGraph::linkStmtRefs(DefStackMap &DefM, NodeAddr<StmtNode*> SA) {
1538*9880d681SAndroid Build Coastguard Worker   RegisterSet Defs;
1539*9880d681SAndroid Build Coastguard Worker 
1540*9880d681SAndroid Build Coastguard Worker   // Link all nodes (upwards in the data-flow) with their reaching defs.
1541*9880d681SAndroid Build Coastguard Worker   for (NodeAddr<RefNode*> RA : SA.Addr->members(*this)) {
1542*9880d681SAndroid Build Coastguard Worker     uint16_t Kind = RA.Addr->getKind();
1543*9880d681SAndroid Build Coastguard Worker     assert(Kind == NodeAttrs::Def || Kind == NodeAttrs::Use);
1544*9880d681SAndroid Build Coastguard Worker     RegisterRef RR = RA.Addr->getRegRef();
1545*9880d681SAndroid Build Coastguard Worker     // Do not process multiple defs of the same reference.
1546*9880d681SAndroid Build Coastguard Worker     if (Kind == NodeAttrs::Def && Defs.count(RR))
1547*9880d681SAndroid Build Coastguard Worker       continue;
1548*9880d681SAndroid Build Coastguard Worker     Defs.insert(RR);
1549*9880d681SAndroid Build Coastguard Worker 
1550*9880d681SAndroid Build Coastguard Worker     auto F = DefM.find(RR);
1551*9880d681SAndroid Build Coastguard Worker     if (F == DefM.end())
1552*9880d681SAndroid Build Coastguard Worker       continue;
1553*9880d681SAndroid Build Coastguard Worker     DefStack &DS = F->second;
1554*9880d681SAndroid Build Coastguard Worker     if (Kind == NodeAttrs::Use)
1555*9880d681SAndroid Build Coastguard Worker       linkRefUp<UseNode*>(SA, RA, DS);
1556*9880d681SAndroid Build Coastguard Worker     else if (Kind == NodeAttrs::Def)
1557*9880d681SAndroid Build Coastguard Worker       linkRefUp<DefNode*>(SA, RA, DS);
1558*9880d681SAndroid Build Coastguard Worker     else
1559*9880d681SAndroid Build Coastguard Worker       llvm_unreachable("Unexpected node in instruction");
1560*9880d681SAndroid Build Coastguard Worker   }
1561*9880d681SAndroid Build Coastguard Worker }
1562*9880d681SAndroid Build Coastguard Worker 
1563*9880d681SAndroid Build Coastguard Worker // Create data-flow links for all instructions in the block node BA. This
1564*9880d681SAndroid Build Coastguard Worker // will include updating any phi nodes in BA.
linkBlockRefs(DefStackMap & DefM,NodeAddr<BlockNode * > BA)1565*9880d681SAndroid Build Coastguard Worker void DataFlowGraph::linkBlockRefs(DefStackMap &DefM, NodeAddr<BlockNode*> BA) {
1566*9880d681SAndroid Build Coastguard Worker   // Push block delimiters.
1567*9880d681SAndroid Build Coastguard Worker   markBlock(BA.Id, DefM);
1568*9880d681SAndroid Build Coastguard Worker 
1569*9880d681SAndroid Build Coastguard Worker   assert(BA.Addr && "block node address is needed to create a data-flow link");
1570*9880d681SAndroid Build Coastguard Worker   // For each non-phi instruction in the block, link all the defs and uses
1571*9880d681SAndroid Build Coastguard Worker   // to their reaching defs. For any member of the block (including phis),
1572*9880d681SAndroid Build Coastguard Worker   // push the defs on the corresponding stacks.
1573*9880d681SAndroid Build Coastguard Worker   for (NodeAddr<InstrNode*> IA : BA.Addr->members(*this)) {
1574*9880d681SAndroid Build Coastguard Worker     // Ignore phi nodes here. They will be linked part by part from the
1575*9880d681SAndroid Build Coastguard Worker     // predecessors.
1576*9880d681SAndroid Build Coastguard Worker     if (IA.Addr->getKind() == NodeAttrs::Stmt)
1577*9880d681SAndroid Build Coastguard Worker       linkStmtRefs(DefM, IA);
1578*9880d681SAndroid Build Coastguard Worker 
1579*9880d681SAndroid Build Coastguard Worker     // Push the definitions on the stack.
1580*9880d681SAndroid Build Coastguard Worker     pushDefs(IA, DefM);
1581*9880d681SAndroid Build Coastguard Worker   }
1582*9880d681SAndroid Build Coastguard Worker 
1583*9880d681SAndroid Build Coastguard Worker   // Recursively process all children in the dominator tree.
1584*9880d681SAndroid Build Coastguard Worker   MachineDomTreeNode *N = MDT.getNode(BA.Addr->getCode());
1585*9880d681SAndroid Build Coastguard Worker   for (auto I : *N) {
1586*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *SB = I->getBlock();
1587*9880d681SAndroid Build Coastguard Worker     auto SBA = Func.Addr->findBlock(SB, *this);
1588*9880d681SAndroid Build Coastguard Worker     linkBlockRefs(DefM, SBA);
1589*9880d681SAndroid Build Coastguard Worker   }
1590*9880d681SAndroid Build Coastguard Worker 
1591*9880d681SAndroid Build Coastguard Worker   // Link the phi uses from the successor blocks.
1592*9880d681SAndroid Build Coastguard Worker   auto IsUseForBA = [BA](NodeAddr<NodeBase*> NA) -> bool {
1593*9880d681SAndroid Build Coastguard Worker     if (NA.Addr->getKind() != NodeAttrs::Use)
1594*9880d681SAndroid Build Coastguard Worker       return false;
1595*9880d681SAndroid Build Coastguard Worker     assert(NA.Addr->getFlags() & NodeAttrs::PhiRef);
1596*9880d681SAndroid Build Coastguard Worker     NodeAddr<PhiUseNode*> PUA = NA;
1597*9880d681SAndroid Build Coastguard Worker     return PUA.Addr->getPredecessor() == BA.Id;
1598*9880d681SAndroid Build Coastguard Worker   };
1599*9880d681SAndroid Build Coastguard Worker   MachineBasicBlock *MBB = BA.Addr->getCode();
1600*9880d681SAndroid Build Coastguard Worker   for (auto SB : MBB->successors()) {
1601*9880d681SAndroid Build Coastguard Worker     auto SBA = Func.Addr->findBlock(SB, *this);
1602*9880d681SAndroid Build Coastguard Worker     for (NodeAddr<InstrNode*> IA : SBA.Addr->members_if(IsPhi, *this)) {
1603*9880d681SAndroid Build Coastguard Worker       // Go over each phi use associated with MBB, and link it.
1604*9880d681SAndroid Build Coastguard Worker       for (auto U : IA.Addr->members_if(IsUseForBA, *this)) {
1605*9880d681SAndroid Build Coastguard Worker         NodeAddr<PhiUseNode*> PUA = U;
1606*9880d681SAndroid Build Coastguard Worker         RegisterRef RR = PUA.Addr->getRegRef();
1607*9880d681SAndroid Build Coastguard Worker         linkRefUp<UseNode*>(IA, PUA, DefM[RR]);
1608*9880d681SAndroid Build Coastguard Worker       }
1609*9880d681SAndroid Build Coastguard Worker     }
1610*9880d681SAndroid Build Coastguard Worker   }
1611*9880d681SAndroid Build Coastguard Worker 
1612*9880d681SAndroid Build Coastguard Worker   // Pop all defs from this block from the definition stacks.
1613*9880d681SAndroid Build Coastguard Worker   releaseBlock(BA.Id, DefM);
1614*9880d681SAndroid Build Coastguard Worker }
1615*9880d681SAndroid Build Coastguard Worker 
1616*9880d681SAndroid Build Coastguard Worker // Remove the use node UA from any data-flow and structural links.
unlinkUseDF(NodeAddr<UseNode * > UA)1617*9880d681SAndroid Build Coastguard Worker void DataFlowGraph::unlinkUseDF(NodeAddr<UseNode*> UA) {
1618*9880d681SAndroid Build Coastguard Worker   NodeId RD = UA.Addr->getReachingDef();
1619*9880d681SAndroid Build Coastguard Worker   NodeId Sib = UA.Addr->getSibling();
1620*9880d681SAndroid Build Coastguard Worker 
1621*9880d681SAndroid Build Coastguard Worker   if (RD == 0) {
1622*9880d681SAndroid Build Coastguard Worker     assert(Sib == 0);
1623*9880d681SAndroid Build Coastguard Worker     return;
1624*9880d681SAndroid Build Coastguard Worker   }
1625*9880d681SAndroid Build Coastguard Worker 
1626*9880d681SAndroid Build Coastguard Worker   auto RDA = addr<DefNode*>(RD);
1627*9880d681SAndroid Build Coastguard Worker   auto TA = addr<UseNode*>(RDA.Addr->getReachedUse());
1628*9880d681SAndroid Build Coastguard Worker   if (TA.Id == UA.Id) {
1629*9880d681SAndroid Build Coastguard Worker     RDA.Addr->setReachedUse(Sib);
1630*9880d681SAndroid Build Coastguard Worker     return;
1631*9880d681SAndroid Build Coastguard Worker   }
1632*9880d681SAndroid Build Coastguard Worker 
1633*9880d681SAndroid Build Coastguard Worker   while (TA.Id != 0) {
1634*9880d681SAndroid Build Coastguard Worker     NodeId S = TA.Addr->getSibling();
1635*9880d681SAndroid Build Coastguard Worker     if (S == UA.Id) {
1636*9880d681SAndroid Build Coastguard Worker       TA.Addr->setSibling(UA.Addr->getSibling());
1637*9880d681SAndroid Build Coastguard Worker       return;
1638*9880d681SAndroid Build Coastguard Worker     }
1639*9880d681SAndroid Build Coastguard Worker     TA = addr<UseNode*>(S);
1640*9880d681SAndroid Build Coastguard Worker   }
1641*9880d681SAndroid Build Coastguard Worker }
1642*9880d681SAndroid Build Coastguard Worker 
1643*9880d681SAndroid Build Coastguard Worker // Remove the def node DA from any data-flow and structural links.
unlinkDefDF(NodeAddr<DefNode * > DA)1644*9880d681SAndroid Build Coastguard Worker void DataFlowGraph::unlinkDefDF(NodeAddr<DefNode*> DA) {
1645*9880d681SAndroid Build Coastguard Worker   //
1646*9880d681SAndroid Build Coastguard Worker   //         RD
1647*9880d681SAndroid Build Coastguard Worker   //         | reached
1648*9880d681SAndroid Build Coastguard Worker   //         | def
1649*9880d681SAndroid Build Coastguard Worker   //         :
1650*9880d681SAndroid Build Coastguard Worker   //         .
1651*9880d681SAndroid Build Coastguard Worker   //        +----+
1652*9880d681SAndroid Build Coastguard Worker   // ... -- | DA | -- ... -- 0  : sibling chain of DA
1653*9880d681SAndroid Build Coastguard Worker   //        +----+
1654*9880d681SAndroid Build Coastguard Worker   //         |  | reached
1655*9880d681SAndroid Build Coastguard Worker   //         |  : def
1656*9880d681SAndroid Build Coastguard Worker   //         |  .
1657*9880d681SAndroid Build Coastguard Worker   //         | ...  : Siblings (defs)
1658*9880d681SAndroid Build Coastguard Worker   //         |
1659*9880d681SAndroid Build Coastguard Worker   //         : reached
1660*9880d681SAndroid Build Coastguard Worker   //         . use
1661*9880d681SAndroid Build Coastguard Worker   //        ... : sibling chain of reached uses
1662*9880d681SAndroid Build Coastguard Worker 
1663*9880d681SAndroid Build Coastguard Worker   NodeId RD = DA.Addr->getReachingDef();
1664*9880d681SAndroid Build Coastguard Worker 
1665*9880d681SAndroid Build Coastguard Worker   // Visit all siblings of the reached def and reset their reaching defs.
1666*9880d681SAndroid Build Coastguard Worker   // Also, defs reached by DA are now "promoted" to being reached by RD,
1667*9880d681SAndroid Build Coastguard Worker   // so all of them will need to be spliced into the sibling chain where
1668*9880d681SAndroid Build Coastguard Worker   // DA belongs.
1669*9880d681SAndroid Build Coastguard Worker   auto getAllNodes = [this] (NodeId N) -> NodeList {
1670*9880d681SAndroid Build Coastguard Worker     NodeList Res;
1671*9880d681SAndroid Build Coastguard Worker     while (N) {
1672*9880d681SAndroid Build Coastguard Worker       auto RA = addr<RefNode*>(N);
1673*9880d681SAndroid Build Coastguard Worker       // Keep the nodes in the exact sibling order.
1674*9880d681SAndroid Build Coastguard Worker       Res.push_back(RA);
1675*9880d681SAndroid Build Coastguard Worker       N = RA.Addr->getSibling();
1676*9880d681SAndroid Build Coastguard Worker     }
1677*9880d681SAndroid Build Coastguard Worker     return Res;
1678*9880d681SAndroid Build Coastguard Worker   };
1679*9880d681SAndroid Build Coastguard Worker   NodeList ReachedDefs = getAllNodes(DA.Addr->getReachedDef());
1680*9880d681SAndroid Build Coastguard Worker   NodeList ReachedUses = getAllNodes(DA.Addr->getReachedUse());
1681*9880d681SAndroid Build Coastguard Worker 
1682*9880d681SAndroid Build Coastguard Worker   if (RD == 0) {
1683*9880d681SAndroid Build Coastguard Worker     for (NodeAddr<RefNode*> I : ReachedDefs)
1684*9880d681SAndroid Build Coastguard Worker       I.Addr->setSibling(0);
1685*9880d681SAndroid Build Coastguard Worker     for (NodeAddr<RefNode*> I : ReachedUses)
1686*9880d681SAndroid Build Coastguard Worker       I.Addr->setSibling(0);
1687*9880d681SAndroid Build Coastguard Worker   }
1688*9880d681SAndroid Build Coastguard Worker   for (NodeAddr<DefNode*> I : ReachedDefs)
1689*9880d681SAndroid Build Coastguard Worker     I.Addr->setReachingDef(RD);
1690*9880d681SAndroid Build Coastguard Worker   for (NodeAddr<UseNode*> I : ReachedUses)
1691*9880d681SAndroid Build Coastguard Worker     I.Addr->setReachingDef(RD);
1692*9880d681SAndroid Build Coastguard Worker 
1693*9880d681SAndroid Build Coastguard Worker   NodeId Sib = DA.Addr->getSibling();
1694*9880d681SAndroid Build Coastguard Worker   if (RD == 0) {
1695*9880d681SAndroid Build Coastguard Worker     assert(Sib == 0);
1696*9880d681SAndroid Build Coastguard Worker     return;
1697*9880d681SAndroid Build Coastguard Worker   }
1698*9880d681SAndroid Build Coastguard Worker 
1699*9880d681SAndroid Build Coastguard Worker   // Update the reaching def node and remove DA from the sibling list.
1700*9880d681SAndroid Build Coastguard Worker   auto RDA = addr<DefNode*>(RD);
1701*9880d681SAndroid Build Coastguard Worker   auto TA = addr<DefNode*>(RDA.Addr->getReachedDef());
1702*9880d681SAndroid Build Coastguard Worker   if (TA.Id == DA.Id) {
1703*9880d681SAndroid Build Coastguard Worker     // If DA is the first reached def, just update the RD's reached def
1704*9880d681SAndroid Build Coastguard Worker     // to the DA's sibling.
1705*9880d681SAndroid Build Coastguard Worker     RDA.Addr->setReachedDef(Sib);
1706*9880d681SAndroid Build Coastguard Worker   } else {
1707*9880d681SAndroid Build Coastguard Worker     // Otherwise, traverse the sibling list of the reached defs and remove
1708*9880d681SAndroid Build Coastguard Worker     // DA from it.
1709*9880d681SAndroid Build Coastguard Worker     while (TA.Id != 0) {
1710*9880d681SAndroid Build Coastguard Worker       NodeId S = TA.Addr->getSibling();
1711*9880d681SAndroid Build Coastguard Worker       if (S == DA.Id) {
1712*9880d681SAndroid Build Coastguard Worker         TA.Addr->setSibling(Sib);
1713*9880d681SAndroid Build Coastguard Worker         break;
1714*9880d681SAndroid Build Coastguard Worker       }
1715*9880d681SAndroid Build Coastguard Worker       TA = addr<DefNode*>(S);
1716*9880d681SAndroid Build Coastguard Worker     }
1717*9880d681SAndroid Build Coastguard Worker   }
1718*9880d681SAndroid Build Coastguard Worker 
1719*9880d681SAndroid Build Coastguard Worker   // Splice the DA's reached defs into the RDA's reached def chain.
1720*9880d681SAndroid Build Coastguard Worker   if (!ReachedDefs.empty()) {
1721*9880d681SAndroid Build Coastguard Worker     auto Last = NodeAddr<DefNode*>(ReachedDefs.back());
1722*9880d681SAndroid Build Coastguard Worker     Last.Addr->setSibling(RDA.Addr->getReachedDef());
1723*9880d681SAndroid Build Coastguard Worker     RDA.Addr->setReachedDef(ReachedDefs.front().Id);
1724*9880d681SAndroid Build Coastguard Worker   }
1725*9880d681SAndroid Build Coastguard Worker   // Splice the DA's reached uses into the RDA's reached use chain.
1726*9880d681SAndroid Build Coastguard Worker   if (!ReachedUses.empty()) {
1727*9880d681SAndroid Build Coastguard Worker     auto Last = NodeAddr<UseNode*>(ReachedUses.back());
1728*9880d681SAndroid Build Coastguard Worker     Last.Addr->setSibling(RDA.Addr->getReachedUse());
1729*9880d681SAndroid Build Coastguard Worker     RDA.Addr->setReachedUse(ReachedUses.front().Id);
1730*9880d681SAndroid Build Coastguard Worker   }
1731*9880d681SAndroid Build Coastguard Worker }
1732