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