1080dd10fSScott Constable //===- RDFGraph.cpp -------------------------------------------------------===//
2080dd10fSScott Constable //
3080dd10fSScott Constable // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4080dd10fSScott Constable // See https://llvm.org/LICENSE.txt for license information.
5080dd10fSScott Constable // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6080dd10fSScott Constable //
7080dd10fSScott Constable //===----------------------------------------------------------------------===//
8080dd10fSScott Constable //
9080dd10fSScott Constable // Target-independent, SSA-based data flow graph for register data flow (RDF).
10080dd10fSScott Constable //
11080dd10fSScott Constable #include "llvm/ADT/BitVector.h"
12080dd10fSScott Constable #include "llvm/ADT/STLExtras.h"
13080dd10fSScott Constable #include "llvm/ADT/SetVector.h"
14080dd10fSScott Constable #include "llvm/CodeGen/MachineBasicBlock.h"
15080dd10fSScott Constable #include "llvm/CodeGen/MachineDominanceFrontier.h"
16080dd10fSScott Constable #include "llvm/CodeGen/MachineDominators.h"
17080dd10fSScott Constable #include "llvm/CodeGen/MachineFunction.h"
18080dd10fSScott Constable #include "llvm/CodeGen/MachineInstr.h"
19080dd10fSScott Constable #include "llvm/CodeGen/MachineOperand.h"
20080dd10fSScott Constable #include "llvm/CodeGen/MachineRegisterInfo.h"
21080dd10fSScott Constable #include "llvm/CodeGen/RDFGraph.h"
22080dd10fSScott Constable #include "llvm/CodeGen/RDFRegisters.h"
23080dd10fSScott Constable #include "llvm/CodeGen/TargetInstrInfo.h"
24080dd10fSScott Constable #include "llvm/CodeGen/TargetLowering.h"
25080dd10fSScott Constable #include "llvm/CodeGen/TargetRegisterInfo.h"
26080dd10fSScott Constable #include "llvm/CodeGen/TargetSubtargetInfo.h"
27080dd10fSScott Constable #include "llvm/IR/Function.h"
28080dd10fSScott Constable #include "llvm/MC/LaneBitmask.h"
29080dd10fSScott Constable #include "llvm/MC/MCInstrDesc.h"
30080dd10fSScott Constable #include "llvm/MC/MCRegisterInfo.h"
31080dd10fSScott Constable #include "llvm/Support/Debug.h"
32080dd10fSScott Constable #include "llvm/Support/ErrorHandling.h"
33080dd10fSScott Constable #include "llvm/Support/raw_ostream.h"
34080dd10fSScott Constable #include <algorithm>
35080dd10fSScott Constable #include <cassert>
36080dd10fSScott Constable #include <cstdint>
37080dd10fSScott Constable #include <cstring>
38080dd10fSScott Constable #include <iterator>
39080dd10fSScott Constable #include <set>
40080dd10fSScott Constable #include <utility>
41080dd10fSScott Constable #include <vector>
42080dd10fSScott Constable 
43080dd10fSScott Constable using namespace llvm;
44080dd10fSScott Constable using namespace rdf;
45080dd10fSScott Constable 
46080dd10fSScott Constable // Printing functions. Have them here first, so that the rest of the code
47080dd10fSScott Constable // can use them.
48080dd10fSScott Constable namespace llvm {
49080dd10fSScott Constable namespace rdf {
50080dd10fSScott Constable 
51080dd10fSScott Constable raw_ostream &operator<< (raw_ostream &OS, const PrintLaneMaskOpt &P) {
52080dd10fSScott Constable   if (!P.Mask.all())
53080dd10fSScott Constable     OS << ':' << PrintLaneMask(P.Mask);
54080dd10fSScott Constable   return OS;
55080dd10fSScott Constable }
56080dd10fSScott Constable 
57080dd10fSScott Constable raw_ostream &operator<< (raw_ostream &OS, const Print<RegisterRef> &P) {
58080dd10fSScott Constable   auto &TRI = P.G.getTRI();
59080dd10fSScott Constable   if (P.Obj.Reg > 0 && P.Obj.Reg < TRI.getNumRegs())
60080dd10fSScott Constable     OS << TRI.getName(P.Obj.Reg);
61080dd10fSScott Constable   else
62080dd10fSScott Constable     OS << '#' << P.Obj.Reg;
63080dd10fSScott Constable   OS << PrintLaneMaskOpt(P.Obj.Mask);
64080dd10fSScott Constable   return OS;
65080dd10fSScott Constable }
66080dd10fSScott Constable 
67080dd10fSScott Constable raw_ostream &operator<< (raw_ostream &OS, const Print<NodeId> &P) {
68080dd10fSScott Constable   auto NA = P.G.addr<NodeBase*>(P.Obj);
69080dd10fSScott Constable   uint16_t Attrs = NA.Addr->getAttrs();
70080dd10fSScott Constable   uint16_t Kind = NodeAttrs::kind(Attrs);
71080dd10fSScott Constable   uint16_t Flags = NodeAttrs::flags(Attrs);
72080dd10fSScott Constable   switch (NodeAttrs::type(Attrs)) {
73080dd10fSScott Constable     case NodeAttrs::Code:
74080dd10fSScott Constable       switch (Kind) {
75080dd10fSScott Constable         case NodeAttrs::Func:   OS << 'f'; break;
76080dd10fSScott Constable         case NodeAttrs::Block:  OS << 'b'; break;
77080dd10fSScott Constable         case NodeAttrs::Stmt:   OS << 's'; break;
78080dd10fSScott Constable         case NodeAttrs::Phi:    OS << 'p'; break;
79080dd10fSScott Constable         default:                OS << "c?"; break;
80080dd10fSScott Constable       }
81080dd10fSScott Constable       break;
82080dd10fSScott Constable     case NodeAttrs::Ref:
83080dd10fSScott Constable       if (Flags & NodeAttrs::Undef)
84080dd10fSScott Constable         OS << '/';
85080dd10fSScott Constable       if (Flags & NodeAttrs::Dead)
86080dd10fSScott Constable         OS << '\\';
87080dd10fSScott Constable       if (Flags & NodeAttrs::Preserving)
88080dd10fSScott Constable         OS << '+';
89080dd10fSScott Constable       if (Flags & NodeAttrs::Clobbering)
90080dd10fSScott Constable         OS << '~';
91080dd10fSScott Constable       switch (Kind) {
92080dd10fSScott Constable         case NodeAttrs::Use:    OS << 'u'; break;
93080dd10fSScott Constable         case NodeAttrs::Def:    OS << 'd'; break;
94080dd10fSScott Constable         case NodeAttrs::Block:  OS << 'b'; break;
95080dd10fSScott Constable         default:                OS << "r?"; break;
96080dd10fSScott Constable       }
97080dd10fSScott Constable       break;
98080dd10fSScott Constable     default:
99080dd10fSScott Constable       OS << '?';
100080dd10fSScott Constable       break;
101080dd10fSScott Constable   }
102080dd10fSScott Constable   OS << P.Obj;
103080dd10fSScott Constable   if (Flags & NodeAttrs::Shadow)
104080dd10fSScott Constable     OS << '"';
105080dd10fSScott Constable   return OS;
106080dd10fSScott Constable }
107080dd10fSScott Constable 
108080dd10fSScott Constable static void printRefHeader(raw_ostream &OS, const NodeAddr<RefNode*> RA,
109080dd10fSScott Constable                 const DataFlowGraph &G) {
110080dd10fSScott Constable   OS << Print<NodeId>(RA.Id, G) << '<'
111080dd10fSScott Constable      << Print<RegisterRef>(RA.Addr->getRegRef(G), G) << '>';
112080dd10fSScott Constable   if (RA.Addr->getFlags() & NodeAttrs::Fixed)
113080dd10fSScott Constable     OS << '!';
114080dd10fSScott Constable }
115080dd10fSScott Constable 
116080dd10fSScott Constable raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<DefNode*>> &P) {
117080dd10fSScott Constable   printRefHeader(OS, P.Obj, P.G);
118080dd10fSScott Constable   OS << '(';
119080dd10fSScott Constable   if (NodeId N = P.Obj.Addr->getReachingDef())
120080dd10fSScott Constable     OS << Print<NodeId>(N, P.G);
121080dd10fSScott Constable   OS << ',';
122080dd10fSScott Constable   if (NodeId N = P.Obj.Addr->getReachedDef())
123080dd10fSScott Constable     OS << Print<NodeId>(N, P.G);
124080dd10fSScott Constable   OS << ',';
125080dd10fSScott Constable   if (NodeId N = P.Obj.Addr->getReachedUse())
126080dd10fSScott Constable     OS << Print<NodeId>(N, P.G);
127080dd10fSScott Constable   OS << "):";
128080dd10fSScott Constable   if (NodeId N = P.Obj.Addr->getSibling())
129080dd10fSScott Constable     OS << Print<NodeId>(N, P.G);
130080dd10fSScott Constable   return OS;
131080dd10fSScott Constable }
132080dd10fSScott Constable 
133080dd10fSScott Constable raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<UseNode*>> &P) {
134080dd10fSScott Constable   printRefHeader(OS, P.Obj, P.G);
135080dd10fSScott Constable   OS << '(';
136080dd10fSScott Constable   if (NodeId N = P.Obj.Addr->getReachingDef())
137080dd10fSScott Constable     OS << Print<NodeId>(N, P.G);
138080dd10fSScott Constable   OS << "):";
139080dd10fSScott Constable   if (NodeId N = P.Obj.Addr->getSibling())
140080dd10fSScott Constable     OS << Print<NodeId>(N, P.G);
141080dd10fSScott Constable   return OS;
142080dd10fSScott Constable }
143080dd10fSScott Constable 
144080dd10fSScott Constable raw_ostream &operator<< (raw_ostream &OS,
145080dd10fSScott Constable       const Print<NodeAddr<PhiUseNode*>> &P) {
146080dd10fSScott Constable   printRefHeader(OS, P.Obj, P.G);
147080dd10fSScott Constable   OS << '(';
148080dd10fSScott Constable   if (NodeId N = P.Obj.Addr->getReachingDef())
149080dd10fSScott Constable     OS << Print<NodeId>(N, P.G);
150080dd10fSScott Constable   OS << ',';
151080dd10fSScott Constable   if (NodeId N = P.Obj.Addr->getPredecessor())
152080dd10fSScott Constable     OS << Print<NodeId>(N, P.G);
153080dd10fSScott Constable   OS << "):";
154080dd10fSScott Constable   if (NodeId N = P.Obj.Addr->getSibling())
155080dd10fSScott Constable     OS << Print<NodeId>(N, P.G);
156080dd10fSScott Constable   return OS;
157080dd10fSScott Constable }
158080dd10fSScott Constable 
159080dd10fSScott Constable raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<RefNode*>> &P) {
160080dd10fSScott Constable   switch (P.Obj.Addr->getKind()) {
161080dd10fSScott Constable     case NodeAttrs::Def:
162080dd10fSScott Constable       OS << PrintNode<DefNode*>(P.Obj, P.G);
163080dd10fSScott Constable       break;
164080dd10fSScott Constable     case NodeAttrs::Use:
165080dd10fSScott Constable       if (P.Obj.Addr->getFlags() & NodeAttrs::PhiRef)
166080dd10fSScott Constable         OS << PrintNode<PhiUseNode*>(P.Obj, P.G);
167080dd10fSScott Constable       else
168080dd10fSScott Constable         OS << PrintNode<UseNode*>(P.Obj, P.G);
169080dd10fSScott Constable       break;
170080dd10fSScott Constable   }
171080dd10fSScott Constable   return OS;
172080dd10fSScott Constable }
173080dd10fSScott Constable 
174080dd10fSScott Constable raw_ostream &operator<< (raw_ostream &OS, const Print<NodeList> &P) {
175080dd10fSScott Constable   unsigned N = P.Obj.size();
176080dd10fSScott Constable   for (auto I : P.Obj) {
177080dd10fSScott Constable     OS << Print<NodeId>(I.Id, P.G);
178080dd10fSScott Constable     if (--N)
179080dd10fSScott Constable       OS << ' ';
180080dd10fSScott Constable   }
181080dd10fSScott Constable   return OS;
182080dd10fSScott Constable }
183080dd10fSScott Constable 
184080dd10fSScott Constable raw_ostream &operator<< (raw_ostream &OS, const Print<NodeSet> &P) {
185080dd10fSScott Constable   unsigned N = P.Obj.size();
186080dd10fSScott Constable   for (auto I : P.Obj) {
187080dd10fSScott Constable     OS << Print<NodeId>(I, P.G);
188080dd10fSScott Constable     if (--N)
189080dd10fSScott Constable       OS << ' ';
190080dd10fSScott Constable   }
191080dd10fSScott Constable   return OS;
192080dd10fSScott Constable }
193080dd10fSScott Constable 
194080dd10fSScott Constable namespace {
195080dd10fSScott Constable 
196080dd10fSScott Constable   template <typename T>
197080dd10fSScott Constable   struct PrintListV {
198080dd10fSScott Constable     PrintListV(const NodeList &L, const DataFlowGraph &G) : List(L), G(G) {}
199080dd10fSScott Constable 
200080dd10fSScott Constable     using Type = T;
201080dd10fSScott Constable     const NodeList &List;
202080dd10fSScott Constable     const DataFlowGraph &G;
203080dd10fSScott Constable   };
204080dd10fSScott Constable 
205080dd10fSScott Constable   template <typename T>
206080dd10fSScott Constable   raw_ostream &operator<< (raw_ostream &OS, const PrintListV<T> &P) {
207080dd10fSScott Constable     unsigned N = P.List.size();
208080dd10fSScott Constable     for (NodeAddr<T> A : P.List) {
209080dd10fSScott Constable       OS << PrintNode<T>(A, P.G);
210080dd10fSScott Constable       if (--N)
211080dd10fSScott Constable         OS << ", ";
212080dd10fSScott Constable     }
213080dd10fSScott Constable     return OS;
214080dd10fSScott Constable   }
215080dd10fSScott Constable 
216080dd10fSScott Constable } // end anonymous namespace
217080dd10fSScott Constable 
218080dd10fSScott Constable raw_ostream &operator<< (raw_ostream &OS, const Print<NodeAddr<PhiNode*>> &P) {
219080dd10fSScott Constable   OS << Print<NodeId>(P.Obj.Id, P.G) << ": phi ["
220080dd10fSScott Constable      << PrintListV<RefNode*>(P.Obj.Addr->members(P.G), P.G) << ']';
221080dd10fSScott Constable   return OS;
222080dd10fSScott Constable }
223080dd10fSScott Constable 
224080dd10fSScott Constable raw_ostream &operator<<(raw_ostream &OS, const Print<NodeAddr<StmtNode *>> &P) {
225080dd10fSScott Constable   const MachineInstr &MI = *P.Obj.Addr->getCode();
226080dd10fSScott Constable   unsigned Opc = MI.getOpcode();
227080dd10fSScott Constable   OS << Print<NodeId>(P.Obj.Id, P.G) << ": " << P.G.getTII().getName(Opc);
228080dd10fSScott Constable   // Print the target for calls and branches (for readability).
229080dd10fSScott Constable   if (MI.isCall() || MI.isBranch()) {
230080dd10fSScott Constable     MachineInstr::const_mop_iterator T =
231080dd10fSScott Constable           llvm::find_if(MI.operands(),
232080dd10fSScott Constable                         [] (const MachineOperand &Op) -> bool {
233080dd10fSScott Constable                           return Op.isMBB() || Op.isGlobal() || Op.isSymbol();
234080dd10fSScott Constable                         });
235080dd10fSScott Constable     if (T != MI.operands_end()) {
236080dd10fSScott Constable       OS << ' ';
237080dd10fSScott Constable       if (T->isMBB())
238080dd10fSScott Constable         OS << printMBBReference(*T->getMBB());
239080dd10fSScott Constable       else if (T->isGlobal())
240080dd10fSScott Constable         OS << T->getGlobal()->getName();
241080dd10fSScott Constable       else if (T->isSymbol())
242080dd10fSScott Constable         OS << T->getSymbolName();
243080dd10fSScott Constable     }
244080dd10fSScott Constable   }
245080dd10fSScott Constable   OS << " [" << PrintListV<RefNode*>(P.Obj.Addr->members(P.G), P.G) << ']';
246080dd10fSScott Constable   return OS;
247080dd10fSScott Constable }
248080dd10fSScott Constable 
249080dd10fSScott Constable raw_ostream &operator<< (raw_ostream &OS,
250080dd10fSScott Constable       const Print<NodeAddr<InstrNode*>> &P) {
251080dd10fSScott Constable   switch (P.Obj.Addr->getKind()) {
252080dd10fSScott Constable     case NodeAttrs::Phi:
253080dd10fSScott Constable       OS << PrintNode<PhiNode*>(P.Obj, P.G);
254080dd10fSScott Constable       break;
255080dd10fSScott Constable     case NodeAttrs::Stmt:
256080dd10fSScott Constable       OS << PrintNode<StmtNode*>(P.Obj, P.G);
257080dd10fSScott Constable       break;
258080dd10fSScott Constable     default:
259080dd10fSScott Constable       OS << "instr? " << Print<NodeId>(P.Obj.Id, P.G);
260080dd10fSScott Constable       break;
261080dd10fSScott Constable   }
262080dd10fSScott Constable   return OS;
263080dd10fSScott Constable }
264080dd10fSScott Constable 
265080dd10fSScott Constable raw_ostream &operator<< (raw_ostream &OS,
266080dd10fSScott Constable       const Print<NodeAddr<BlockNode*>> &P) {
267080dd10fSScott Constable   MachineBasicBlock *BB = P.Obj.Addr->getCode();
268080dd10fSScott Constable   unsigned NP = BB->pred_size();
269080dd10fSScott Constable   std::vector<int> Ns;
270080dd10fSScott Constable   auto PrintBBs = [&OS] (std::vector<int> Ns) -> void {
271080dd10fSScott Constable     unsigned N = Ns.size();
272080dd10fSScott Constable     for (int I : Ns) {
273080dd10fSScott Constable       OS << "%bb." << I;
274080dd10fSScott Constable       if (--N)
275080dd10fSScott Constable         OS << ", ";
276080dd10fSScott Constable     }
277080dd10fSScott Constable   };
278080dd10fSScott Constable 
279080dd10fSScott Constable   OS << Print<NodeId>(P.Obj.Id, P.G) << ": --- " << printMBBReference(*BB)
280080dd10fSScott Constable      << " --- preds(" << NP << "): ";
281080dd10fSScott Constable   for (MachineBasicBlock *B : BB->predecessors())
282080dd10fSScott Constable     Ns.push_back(B->getNumber());
283080dd10fSScott Constable   PrintBBs(Ns);
284080dd10fSScott Constable 
285080dd10fSScott Constable   unsigned NS = BB->succ_size();
286080dd10fSScott Constable   OS << "  succs(" << NS << "): ";
287080dd10fSScott Constable   Ns.clear();
288080dd10fSScott Constable   for (MachineBasicBlock *B : BB->successors())
289080dd10fSScott Constable     Ns.push_back(B->getNumber());
290080dd10fSScott Constable   PrintBBs(Ns);
291080dd10fSScott Constable   OS << '\n';
292080dd10fSScott Constable 
293080dd10fSScott Constable   for (auto I : P.Obj.Addr->members(P.G))
294080dd10fSScott Constable     OS << PrintNode<InstrNode*>(I, P.G) << '\n';
295080dd10fSScott Constable   return OS;
296080dd10fSScott Constable }
297080dd10fSScott Constable 
298080dd10fSScott Constable raw_ostream &operator<<(raw_ostream &OS, const Print<NodeAddr<FuncNode *>> &P) {
299080dd10fSScott Constable   OS << "DFG dump:[\n" << Print<NodeId>(P.Obj.Id, P.G) << ": Function: "
300080dd10fSScott Constable      << P.Obj.Addr->getCode()->getName() << '\n';
301080dd10fSScott Constable   for (auto I : P.Obj.Addr->members(P.G))
302080dd10fSScott Constable     OS << PrintNode<BlockNode*>(I, P.G) << '\n';
303080dd10fSScott Constable   OS << "]\n";
304080dd10fSScott Constable   return OS;
305080dd10fSScott Constable }
306080dd10fSScott Constable 
307080dd10fSScott Constable raw_ostream &operator<< (raw_ostream &OS, const Print<RegisterSet> &P) {
308080dd10fSScott Constable   OS << '{';
309080dd10fSScott Constable   for (auto I : P.Obj)
310080dd10fSScott Constable     OS << ' ' << Print<RegisterRef>(I, P.G);
311080dd10fSScott Constable   OS << " }";
312080dd10fSScott Constable   return OS;
313080dd10fSScott Constable }
314080dd10fSScott Constable 
315080dd10fSScott Constable raw_ostream &operator<< (raw_ostream &OS, const Print<RegisterAggr> &P) {
316080dd10fSScott Constable   P.Obj.print(OS);
317080dd10fSScott Constable   return OS;
318080dd10fSScott Constable }
319080dd10fSScott Constable 
320080dd10fSScott Constable raw_ostream &operator<< (raw_ostream &OS,
321080dd10fSScott Constable       const Print<DataFlowGraph::DefStack> &P) {
322080dd10fSScott Constable   for (auto I = P.Obj.top(), E = P.Obj.bottom(); I != E; ) {
323080dd10fSScott Constable     OS << Print<NodeId>(I->Id, P.G)
324080dd10fSScott Constable        << '<' << Print<RegisterRef>(I->Addr->getRegRef(P.G), P.G) << '>';
325080dd10fSScott Constable     I.down();
326080dd10fSScott Constable     if (I != E)
327080dd10fSScott Constable       OS << ' ';
328080dd10fSScott Constable   }
329080dd10fSScott Constable   return OS;
330080dd10fSScott Constable }
331080dd10fSScott Constable 
332080dd10fSScott Constable } // end namespace rdf
333080dd10fSScott Constable } // end namespace llvm
334080dd10fSScott Constable 
335080dd10fSScott Constable // Node allocation functions.
336080dd10fSScott Constable //
337080dd10fSScott Constable // Node allocator is like a slab memory allocator: it allocates blocks of
338080dd10fSScott Constable // memory in sizes that are multiples of the size of a node. Each block has
339080dd10fSScott Constable // the same size. Nodes are allocated from the currently active block, and
340080dd10fSScott Constable // when it becomes full, a new one is created.
341080dd10fSScott Constable // There is a mapping scheme between node id and its location in a block,
342080dd10fSScott Constable // and within that block is described in the header file.
343080dd10fSScott Constable //
344080dd10fSScott Constable void NodeAllocator::startNewBlock() {
345080dd10fSScott Constable   void *T = MemPool.Allocate(NodesPerBlock*NodeMemSize, NodeMemSize);
346080dd10fSScott Constable   char *P = static_cast<char*>(T);
347080dd10fSScott Constable   Blocks.push_back(P);
348080dd10fSScott Constable   // Check if the block index is still within the allowed range, i.e. less
349080dd10fSScott Constable   // than 2^N, where N is the number of bits in NodeId for the block index.
350080dd10fSScott Constable   // BitsPerIndex is the number of bits per node index.
351080dd10fSScott Constable   assert((Blocks.size() < ((size_t)1 << (8*sizeof(NodeId)-BitsPerIndex))) &&
352080dd10fSScott Constable          "Out of bits for block index");
353080dd10fSScott Constable   ActiveEnd = P;
354080dd10fSScott Constable }
355080dd10fSScott Constable 
356080dd10fSScott Constable bool NodeAllocator::needNewBlock() {
357080dd10fSScott Constable   if (Blocks.empty())
358080dd10fSScott Constable     return true;
359080dd10fSScott Constable 
360080dd10fSScott Constable   char *ActiveBegin = Blocks.back();
361080dd10fSScott Constable   uint32_t Index = (ActiveEnd-ActiveBegin)/NodeMemSize;
362080dd10fSScott Constable   return Index >= NodesPerBlock;
363080dd10fSScott Constable }
364080dd10fSScott Constable 
365080dd10fSScott Constable NodeAddr<NodeBase*> NodeAllocator::New() {
366080dd10fSScott Constable   if (needNewBlock())
367080dd10fSScott Constable     startNewBlock();
368080dd10fSScott Constable 
369080dd10fSScott Constable   uint32_t ActiveB = Blocks.size()-1;
370080dd10fSScott Constable   uint32_t Index = (ActiveEnd - Blocks[ActiveB])/NodeMemSize;
371080dd10fSScott Constable   NodeAddr<NodeBase*> NA = { reinterpret_cast<NodeBase*>(ActiveEnd),
372080dd10fSScott Constable                              makeId(ActiveB, Index) };
373080dd10fSScott Constable   ActiveEnd += NodeMemSize;
374080dd10fSScott Constable   return NA;
375080dd10fSScott Constable }
376080dd10fSScott Constable 
377080dd10fSScott Constable NodeId NodeAllocator::id(const NodeBase *P) const {
378080dd10fSScott Constable   uintptr_t A = reinterpret_cast<uintptr_t>(P);
379080dd10fSScott Constable   for (unsigned i = 0, n = Blocks.size(); i != n; ++i) {
380080dd10fSScott Constable     uintptr_t B = reinterpret_cast<uintptr_t>(Blocks[i]);
381080dd10fSScott Constable     if (A < B || A >= B + NodesPerBlock*NodeMemSize)
382080dd10fSScott Constable       continue;
383080dd10fSScott Constable     uint32_t Idx = (A-B)/NodeMemSize;
384080dd10fSScott Constable     return makeId(i, Idx);
385080dd10fSScott Constable   }
386080dd10fSScott Constable   llvm_unreachable("Invalid node address");
387080dd10fSScott Constable }
388080dd10fSScott Constable 
389080dd10fSScott Constable void NodeAllocator::clear() {
390080dd10fSScott Constable   MemPool.Reset();
391080dd10fSScott Constable   Blocks.clear();
392080dd10fSScott Constable   ActiveEnd = nullptr;
393080dd10fSScott Constable }
394080dd10fSScott Constable 
395080dd10fSScott Constable // Insert node NA after "this" in the circular chain.
396080dd10fSScott Constable void NodeBase::append(NodeAddr<NodeBase*> NA) {
397080dd10fSScott Constable   NodeId Nx = Next;
398080dd10fSScott Constable   // If NA is already "next", do nothing.
399080dd10fSScott Constable   if (Next != NA.Id) {
400080dd10fSScott Constable     Next = NA.Id;
401080dd10fSScott Constable     NA.Addr->Next = Nx;
402080dd10fSScott Constable   }
403080dd10fSScott Constable }
404080dd10fSScott Constable 
405080dd10fSScott Constable // Fundamental node manipulator functions.
406080dd10fSScott Constable 
407080dd10fSScott Constable // Obtain the register reference from a reference node.
408080dd10fSScott Constable RegisterRef RefNode::getRegRef(const DataFlowGraph &G) const {
409080dd10fSScott Constable   assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref);
410080dd10fSScott Constable   if (NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef)
411080dd10fSScott Constable     return G.unpack(Ref.PR);
412080dd10fSScott Constable   assert(Ref.Op != nullptr);
413080dd10fSScott Constable   return G.makeRegRef(*Ref.Op);
414080dd10fSScott Constable }
415080dd10fSScott Constable 
416080dd10fSScott Constable // Set the register reference in the reference node directly (for references
417080dd10fSScott Constable // in phi nodes).
418080dd10fSScott Constable void RefNode::setRegRef(RegisterRef RR, DataFlowGraph &G) {
419080dd10fSScott Constable   assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref);
420080dd10fSScott Constable   assert(NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef);
421080dd10fSScott Constable   Ref.PR = G.pack(RR);
422080dd10fSScott Constable }
423080dd10fSScott Constable 
424080dd10fSScott Constable // Set the register reference in the reference node based on a machine
425080dd10fSScott Constable // operand (for references in statement nodes).
426080dd10fSScott Constable void RefNode::setRegRef(MachineOperand *Op, DataFlowGraph &G) {
427080dd10fSScott Constable   assert(NodeAttrs::type(Attrs) == NodeAttrs::Ref);
428080dd10fSScott Constable   assert(!(NodeAttrs::flags(Attrs) & NodeAttrs::PhiRef));
429080dd10fSScott Constable   (void)G;
430080dd10fSScott Constable   Ref.Op = Op;
431080dd10fSScott Constable }
432080dd10fSScott Constable 
433080dd10fSScott Constable // Get the owner of a given reference node.
434080dd10fSScott Constable NodeAddr<NodeBase*> RefNode::getOwner(const DataFlowGraph &G) {
435080dd10fSScott Constable   NodeAddr<NodeBase*> NA = G.addr<NodeBase*>(getNext());
436080dd10fSScott Constable 
437080dd10fSScott Constable   while (NA.Addr != this) {
438080dd10fSScott Constable     if (NA.Addr->getType() == NodeAttrs::Code)
439080dd10fSScott Constable       return NA;
440080dd10fSScott Constable     NA = G.addr<NodeBase*>(NA.Addr->getNext());
441080dd10fSScott Constable   }
442080dd10fSScott Constable   llvm_unreachable("No owner in circular list");
443080dd10fSScott Constable }
444080dd10fSScott Constable 
445080dd10fSScott Constable // Connect the def node to the reaching def node.
446080dd10fSScott Constable void DefNode::linkToDef(NodeId Self, NodeAddr<DefNode*> DA) {
447080dd10fSScott Constable   Ref.RD = DA.Id;
448080dd10fSScott Constable   Ref.Sib = DA.Addr->getReachedDef();
449080dd10fSScott Constable   DA.Addr->setReachedDef(Self);
450080dd10fSScott Constable }
451080dd10fSScott Constable 
452080dd10fSScott Constable // Connect the use node to the reaching def node.
453080dd10fSScott Constable void UseNode::linkToDef(NodeId Self, NodeAddr<DefNode*> DA) {
454080dd10fSScott Constable   Ref.RD = DA.Id;
455080dd10fSScott Constable   Ref.Sib = DA.Addr->getReachedUse();
456080dd10fSScott Constable   DA.Addr->setReachedUse(Self);
457080dd10fSScott Constable }
458080dd10fSScott Constable 
459080dd10fSScott Constable // Get the first member of the code node.
460080dd10fSScott Constable NodeAddr<NodeBase*> CodeNode::getFirstMember(const DataFlowGraph &G) const {
461080dd10fSScott Constable   if (Code.FirstM == 0)
462080dd10fSScott Constable     return NodeAddr<NodeBase*>();
463080dd10fSScott Constable   return G.addr<NodeBase*>(Code.FirstM);
464080dd10fSScott Constable }
465080dd10fSScott Constable 
466080dd10fSScott Constable // Get the last member of the code node.
467080dd10fSScott Constable NodeAddr<NodeBase*> CodeNode::getLastMember(const DataFlowGraph &G) const {
468080dd10fSScott Constable   if (Code.LastM == 0)
469080dd10fSScott Constable     return NodeAddr<NodeBase*>();
470080dd10fSScott Constable   return G.addr<NodeBase*>(Code.LastM);
471080dd10fSScott Constable }
472080dd10fSScott Constable 
473080dd10fSScott Constable // Add node NA at the end of the member list of the given code node.
474080dd10fSScott Constable void CodeNode::addMember(NodeAddr<NodeBase*> NA, const DataFlowGraph &G) {
475080dd10fSScott Constable   NodeAddr<NodeBase*> ML = getLastMember(G);
476080dd10fSScott Constable   if (ML.Id != 0) {
477080dd10fSScott Constable     ML.Addr->append(NA);
478080dd10fSScott Constable   } else {
479080dd10fSScott Constable     Code.FirstM = NA.Id;
480080dd10fSScott Constable     NodeId Self = G.id(this);
481080dd10fSScott Constable     NA.Addr->setNext(Self);
482080dd10fSScott Constable   }
483080dd10fSScott Constable   Code.LastM = NA.Id;
484080dd10fSScott Constable }
485080dd10fSScott Constable 
486080dd10fSScott Constable // Add node NA after member node MA in the given code node.
487080dd10fSScott Constable void CodeNode::addMemberAfter(NodeAddr<NodeBase*> MA, NodeAddr<NodeBase*> NA,
488080dd10fSScott Constable       const DataFlowGraph &G) {
489080dd10fSScott Constable   MA.Addr->append(NA);
490080dd10fSScott Constable   if (Code.LastM == MA.Id)
491080dd10fSScott Constable     Code.LastM = NA.Id;
492080dd10fSScott Constable }
493080dd10fSScott Constable 
494080dd10fSScott Constable // Remove member node NA from the given code node.
495080dd10fSScott Constable void CodeNode::removeMember(NodeAddr<NodeBase*> NA, const DataFlowGraph &G) {
496080dd10fSScott Constable   NodeAddr<NodeBase*> MA = getFirstMember(G);
497080dd10fSScott Constable   assert(MA.Id != 0);
498080dd10fSScott Constable 
499080dd10fSScott Constable   // Special handling if the member to remove is the first member.
500080dd10fSScott Constable   if (MA.Id == NA.Id) {
501080dd10fSScott Constable     if (Code.LastM == MA.Id) {
502080dd10fSScott Constable       // If it is the only member, set both first and last to 0.
503080dd10fSScott Constable       Code.FirstM = Code.LastM = 0;
504080dd10fSScott Constable     } else {
505080dd10fSScott Constable       // Otherwise, advance the first member.
506080dd10fSScott Constable       Code.FirstM = MA.Addr->getNext();
507080dd10fSScott Constable     }
508080dd10fSScott Constable     return;
509080dd10fSScott Constable   }
510080dd10fSScott Constable 
511080dd10fSScott Constable   while (MA.Addr != this) {
512080dd10fSScott Constable     NodeId MX = MA.Addr->getNext();
513080dd10fSScott Constable     if (MX == NA.Id) {
514080dd10fSScott Constable       MA.Addr->setNext(NA.Addr->getNext());
515080dd10fSScott Constable       // If the member to remove happens to be the last one, update the
516080dd10fSScott Constable       // LastM indicator.
517080dd10fSScott Constable       if (Code.LastM == NA.Id)
518080dd10fSScott Constable         Code.LastM = MA.Id;
519080dd10fSScott Constable       return;
520080dd10fSScott Constable     }
521080dd10fSScott Constable     MA = G.addr<NodeBase*>(MX);
522080dd10fSScott Constable   }
523080dd10fSScott Constable   llvm_unreachable("No such member");
524080dd10fSScott Constable }
525080dd10fSScott Constable 
526080dd10fSScott Constable // Return the list of all members of the code node.
527080dd10fSScott Constable NodeList CodeNode::members(const DataFlowGraph &G) const {
528080dd10fSScott Constable   static auto True = [] (NodeAddr<NodeBase*>) -> bool { return true; };
529080dd10fSScott Constable   return members_if(True, G);
530080dd10fSScott Constable }
531080dd10fSScott Constable 
532080dd10fSScott Constable // Return the owner of the given instr node.
533080dd10fSScott Constable NodeAddr<NodeBase*> InstrNode::getOwner(const DataFlowGraph &G) {
534080dd10fSScott Constable   NodeAddr<NodeBase*> NA = G.addr<NodeBase*>(getNext());
535080dd10fSScott Constable 
536080dd10fSScott Constable   while (NA.Addr != this) {
537080dd10fSScott Constable     assert(NA.Addr->getType() == NodeAttrs::Code);
538080dd10fSScott Constable     if (NA.Addr->getKind() == NodeAttrs::Block)
539080dd10fSScott Constable       return NA;
540080dd10fSScott Constable     NA = G.addr<NodeBase*>(NA.Addr->getNext());
541080dd10fSScott Constable   }
542080dd10fSScott Constable   llvm_unreachable("No owner in circular list");
543080dd10fSScott Constable }
544080dd10fSScott Constable 
545080dd10fSScott Constable // Add the phi node PA to the given block node.
546080dd10fSScott Constable void BlockNode::addPhi(NodeAddr<PhiNode*> PA, const DataFlowGraph &G) {
547080dd10fSScott Constable   NodeAddr<NodeBase*> M = getFirstMember(G);
548080dd10fSScott Constable   if (M.Id == 0) {
549080dd10fSScott Constable     addMember(PA, G);
550080dd10fSScott Constable     return;
551080dd10fSScott Constable   }
552080dd10fSScott Constable 
553080dd10fSScott Constable   assert(M.Addr->getType() == NodeAttrs::Code);
554080dd10fSScott Constable   if (M.Addr->getKind() == NodeAttrs::Stmt) {
555080dd10fSScott Constable     // If the first member of the block is a statement, insert the phi as
556080dd10fSScott Constable     // the first member.
557080dd10fSScott Constable     Code.FirstM = PA.Id;
558080dd10fSScott Constable     PA.Addr->setNext(M.Id);
559080dd10fSScott Constable   } else {
560080dd10fSScott Constable     // If the first member is a phi, find the last phi, and append PA to it.
561080dd10fSScott Constable     assert(M.Addr->getKind() == NodeAttrs::Phi);
562080dd10fSScott Constable     NodeAddr<NodeBase*> MN = M;
563080dd10fSScott Constable     do {
564080dd10fSScott Constable       M = MN;
565080dd10fSScott Constable       MN = G.addr<NodeBase*>(M.Addr->getNext());
566080dd10fSScott Constable       assert(MN.Addr->getType() == NodeAttrs::Code);
567080dd10fSScott Constable     } while (MN.Addr->getKind() == NodeAttrs::Phi);
568080dd10fSScott Constable 
569080dd10fSScott Constable     // M is the last phi.
570080dd10fSScott Constable     addMemberAfter(M, PA, G);
571080dd10fSScott Constable   }
572080dd10fSScott Constable }
573080dd10fSScott Constable 
574080dd10fSScott Constable // Find the block node corresponding to the machine basic block BB in the
575080dd10fSScott Constable // given func node.
576080dd10fSScott Constable NodeAddr<BlockNode*> FuncNode::findBlock(const MachineBasicBlock *BB,
577080dd10fSScott Constable       const DataFlowGraph &G) const {
578080dd10fSScott Constable   auto EqBB = [BB] (NodeAddr<NodeBase*> NA) -> bool {
579080dd10fSScott Constable     return NodeAddr<BlockNode*>(NA).Addr->getCode() == BB;
580080dd10fSScott Constable   };
581080dd10fSScott Constable   NodeList Ms = members_if(EqBB, G);
582080dd10fSScott Constable   if (!Ms.empty())
583080dd10fSScott Constable     return Ms[0];
584080dd10fSScott Constable   return NodeAddr<BlockNode*>();
585080dd10fSScott Constable }
586080dd10fSScott Constable 
587080dd10fSScott Constable // Get the block node for the entry block in the given function.
588080dd10fSScott Constable NodeAddr<BlockNode*> FuncNode::getEntryBlock(const DataFlowGraph &G) {
589080dd10fSScott Constable   MachineBasicBlock *EntryB = &getCode()->front();
590080dd10fSScott Constable   return findBlock(EntryB, G);
591080dd10fSScott Constable }
592080dd10fSScott Constable 
593080dd10fSScott Constable // Target operand information.
594080dd10fSScott Constable //
595080dd10fSScott Constable 
596080dd10fSScott Constable // For a given instruction, check if there are any bits of RR that can remain
597080dd10fSScott Constable // unchanged across this def.
598080dd10fSScott Constable bool TargetOperandInfo::isPreserving(const MachineInstr &In, unsigned OpNum)
599080dd10fSScott Constable       const {
600080dd10fSScott Constable   return TII.isPredicated(In);
601080dd10fSScott Constable }
602080dd10fSScott Constable 
603080dd10fSScott Constable // Check if the definition of RR produces an unspecified value.
604080dd10fSScott Constable bool TargetOperandInfo::isClobbering(const MachineInstr &In, unsigned OpNum)
605080dd10fSScott Constable       const {
606080dd10fSScott Constable   const MachineOperand &Op = In.getOperand(OpNum);
607080dd10fSScott Constable   if (Op.isRegMask())
608080dd10fSScott Constable     return true;
609080dd10fSScott Constable   assert(Op.isReg());
610080dd10fSScott Constable   if (In.isCall())
611080dd10fSScott Constable     if (Op.isDef() && Op.isDead())
612080dd10fSScott Constable       return true;
613080dd10fSScott Constable   return false;
614080dd10fSScott Constable }
615080dd10fSScott Constable 
616080dd10fSScott Constable // Check if the given instruction specifically requires
617080dd10fSScott Constable bool TargetOperandInfo::isFixedReg(const MachineInstr &In, unsigned OpNum)
618080dd10fSScott Constable       const {
619080dd10fSScott Constable   if (In.isCall() || In.isReturn() || In.isInlineAsm())
620080dd10fSScott Constable     return true;
621080dd10fSScott Constable   // Check for a tail call.
622080dd10fSScott Constable   if (In.isBranch())
623080dd10fSScott Constable     for (const MachineOperand &O : In.operands())
624080dd10fSScott Constable       if (O.isGlobal() || O.isSymbol())
625080dd10fSScott Constable         return true;
626080dd10fSScott Constable 
627080dd10fSScott Constable   const MCInstrDesc &D = In.getDesc();
628080dd10fSScott Constable   if (!D.getImplicitDefs() && !D.getImplicitUses())
629080dd10fSScott Constable     return false;
630080dd10fSScott Constable   const MachineOperand &Op = In.getOperand(OpNum);
631080dd10fSScott Constable   // If there is a sub-register, treat the operand as non-fixed. Currently,
632080dd10fSScott Constable   // fixed registers are those that are listed in the descriptor as implicit
633080dd10fSScott Constable   // uses or defs, and those lists do not allow sub-registers.
634080dd10fSScott Constable   if (Op.getSubReg() != 0)
635080dd10fSScott Constable     return false;
636080dd10fSScott Constable   Register Reg = Op.getReg();
637080dd10fSScott Constable   const MCPhysReg *ImpR = Op.isDef() ? D.getImplicitDefs()
638080dd10fSScott Constable                                      : D.getImplicitUses();
639080dd10fSScott Constable   if (!ImpR)
640080dd10fSScott Constable     return false;
641080dd10fSScott Constable   while (*ImpR)
642080dd10fSScott Constable     if (*ImpR++ == Reg)
643080dd10fSScott Constable       return true;
644080dd10fSScott Constable   return false;
645080dd10fSScott Constable }
646080dd10fSScott Constable 
647080dd10fSScott Constable //
648080dd10fSScott Constable // The data flow graph construction.
649080dd10fSScott Constable //
650080dd10fSScott Constable 
651080dd10fSScott Constable DataFlowGraph::DataFlowGraph(MachineFunction &mf, const TargetInstrInfo &tii,
652080dd10fSScott Constable       const TargetRegisterInfo &tri, const MachineDominatorTree &mdt,
653080dd10fSScott Constable       const MachineDominanceFrontier &mdf, const TargetOperandInfo &toi)
654080dd10fSScott Constable     : MF(mf), TII(tii), TRI(tri), PRI(tri, mf), MDT(mdt), MDF(mdf), TOI(toi),
655080dd10fSScott Constable       LiveIns(PRI) {
656080dd10fSScott Constable }
657080dd10fSScott Constable 
658080dd10fSScott Constable // The implementation of the definition stack.
659080dd10fSScott Constable // Each register reference has its own definition stack. In particular,
660080dd10fSScott Constable // for a register references "Reg" and "Reg:subreg" will each have their
661080dd10fSScott Constable // own definition stacks.
662080dd10fSScott Constable 
663080dd10fSScott Constable // Construct a stack iterator.
664080dd10fSScott Constable DataFlowGraph::DefStack::Iterator::Iterator(const DataFlowGraph::DefStack &S,
665080dd10fSScott Constable       bool Top) : DS(S) {
666080dd10fSScott Constable   if (!Top) {
667080dd10fSScott Constable     // Initialize to bottom.
668080dd10fSScott Constable     Pos = 0;
669080dd10fSScott Constable     return;
670080dd10fSScott Constable   }
671080dd10fSScott Constable   // Initialize to the top, i.e. top-most non-delimiter (or 0, if empty).
672080dd10fSScott Constable   Pos = DS.Stack.size();
673080dd10fSScott Constable   while (Pos > 0 && DS.isDelimiter(DS.Stack[Pos-1]))
674080dd10fSScott Constable     Pos--;
675080dd10fSScott Constable }
676080dd10fSScott Constable 
677080dd10fSScott Constable // Return the size of the stack, including block delimiters.
678080dd10fSScott Constable unsigned DataFlowGraph::DefStack::size() const {
679080dd10fSScott Constable   unsigned S = 0;
680080dd10fSScott Constable   for (auto I = top(), E = bottom(); I != E; I.down())
681080dd10fSScott Constable     S++;
682080dd10fSScott Constable   return S;
683080dd10fSScott Constable }
684080dd10fSScott Constable 
685080dd10fSScott Constable // Remove the top entry from the stack. Remove all intervening delimiters
686080dd10fSScott Constable // so that after this, the stack is either empty, or the top of the stack
687080dd10fSScott Constable // is a non-delimiter.
688080dd10fSScott Constable void DataFlowGraph::DefStack::pop() {
689080dd10fSScott Constable   assert(!empty());
690080dd10fSScott Constable   unsigned P = nextDown(Stack.size());
691080dd10fSScott Constable   Stack.resize(P);
692080dd10fSScott Constable }
693080dd10fSScott Constable 
694080dd10fSScott Constable // Push a delimiter for block node N on the stack.
695080dd10fSScott Constable void DataFlowGraph::DefStack::start_block(NodeId N) {
696080dd10fSScott Constable   assert(N != 0);
697080dd10fSScott Constable   Stack.push_back(NodeAddr<DefNode*>(nullptr, N));
698080dd10fSScott Constable }
699080dd10fSScott Constable 
700080dd10fSScott Constable // Remove all nodes from the top of the stack, until the delimited for
701080dd10fSScott Constable // block node N is encountered. Remove the delimiter as well. In effect,
702080dd10fSScott Constable // this will remove from the stack all definitions from block N.
703080dd10fSScott Constable void DataFlowGraph::DefStack::clear_block(NodeId N) {
704080dd10fSScott Constable   assert(N != 0);
705080dd10fSScott Constable   unsigned P = Stack.size();
706080dd10fSScott Constable   while (P > 0) {
707080dd10fSScott Constable     bool Found = isDelimiter(Stack[P-1], N);
708080dd10fSScott Constable     P--;
709080dd10fSScott Constable     if (Found)
710080dd10fSScott Constable       break;
711080dd10fSScott Constable   }
712080dd10fSScott Constable   // This will also remove the delimiter, if found.
713080dd10fSScott Constable   Stack.resize(P);
714080dd10fSScott Constable }
715080dd10fSScott Constable 
716080dd10fSScott Constable // Move the stack iterator up by one.
717080dd10fSScott Constable unsigned DataFlowGraph::DefStack::nextUp(unsigned P) const {
718080dd10fSScott Constable   // Get the next valid position after P (skipping all delimiters).
719080dd10fSScott Constable   // The input position P does not have to point to a non-delimiter.
720080dd10fSScott Constable   unsigned SS = Stack.size();
721080dd10fSScott Constable   bool IsDelim;
722080dd10fSScott Constable   assert(P < SS);
723080dd10fSScott Constable   do {
724080dd10fSScott Constable     P++;
725080dd10fSScott Constable     IsDelim = isDelimiter(Stack[P-1]);
726080dd10fSScott Constable   } while (P < SS && IsDelim);
727080dd10fSScott Constable   assert(!IsDelim);
728080dd10fSScott Constable   return P;
729080dd10fSScott Constable }
730080dd10fSScott Constable 
731080dd10fSScott Constable // Move the stack iterator down by one.
732080dd10fSScott Constable unsigned DataFlowGraph::DefStack::nextDown(unsigned P) const {
733080dd10fSScott Constable   // Get the preceding valid position before P (skipping all delimiters).
734080dd10fSScott Constable   // The input position P does not have to point to a non-delimiter.
735080dd10fSScott Constable   assert(P > 0 && P <= Stack.size());
736080dd10fSScott Constable   bool IsDelim = isDelimiter(Stack[P-1]);
737080dd10fSScott Constable   do {
738080dd10fSScott Constable     if (--P == 0)
739080dd10fSScott Constable       break;
740080dd10fSScott Constable     IsDelim = isDelimiter(Stack[P-1]);
741080dd10fSScott Constable   } while (P > 0 && IsDelim);
742080dd10fSScott Constable   assert(!IsDelim);
743080dd10fSScott Constable   return P;
744080dd10fSScott Constable }
745080dd10fSScott Constable 
746080dd10fSScott Constable // Register information.
747080dd10fSScott Constable 
748080dd10fSScott Constable RegisterSet DataFlowGraph::getLandingPadLiveIns() const {
749080dd10fSScott Constable   RegisterSet LR;
750080dd10fSScott Constable   const Function &F = MF.getFunction();
751080dd10fSScott Constable   const Constant *PF = F.hasPersonalityFn() ? F.getPersonalityFn()
752080dd10fSScott Constable                                             : nullptr;
753080dd10fSScott Constable   const TargetLowering &TLI = *MF.getSubtarget().getTargetLowering();
754080dd10fSScott Constable   if (RegisterId R = TLI.getExceptionPointerRegister(PF))
755080dd10fSScott Constable     LR.insert(RegisterRef(R));
756080dd10fSScott Constable   if (!isFuncletEHPersonality(classifyEHPersonality(PF))) {
757080dd10fSScott Constable     if (RegisterId R = TLI.getExceptionSelectorRegister(PF))
758080dd10fSScott Constable       LR.insert(RegisterRef(R));
759080dd10fSScott Constable   }
760080dd10fSScott Constable   return LR;
761080dd10fSScott Constable }
762080dd10fSScott Constable 
763080dd10fSScott Constable // Node management functions.
764080dd10fSScott Constable 
765080dd10fSScott Constable // Get the pointer to the node with the id N.
766080dd10fSScott Constable NodeBase *DataFlowGraph::ptr(NodeId N) const {
767080dd10fSScott Constable   if (N == 0)
768080dd10fSScott Constable     return nullptr;
769080dd10fSScott Constable   return Memory.ptr(N);
770080dd10fSScott Constable }
771080dd10fSScott Constable 
772080dd10fSScott Constable // Get the id of the node at the address P.
773080dd10fSScott Constable NodeId DataFlowGraph::id(const NodeBase *P) const {
774080dd10fSScott Constable   if (P == nullptr)
775080dd10fSScott Constable     return 0;
776080dd10fSScott Constable   return Memory.id(P);
777080dd10fSScott Constable }
778080dd10fSScott Constable 
779080dd10fSScott Constable // Allocate a new node and set the attributes to Attrs.
780080dd10fSScott Constable NodeAddr<NodeBase*> DataFlowGraph::newNode(uint16_t Attrs) {
781080dd10fSScott Constable   NodeAddr<NodeBase*> P = Memory.New();
782080dd10fSScott Constable   P.Addr->init();
783080dd10fSScott Constable   P.Addr->setAttrs(Attrs);
784080dd10fSScott Constable   return P;
785080dd10fSScott Constable }
786080dd10fSScott Constable 
787080dd10fSScott Constable // Make a copy of the given node B, except for the data-flow links, which
788080dd10fSScott Constable // are set to 0.
789080dd10fSScott Constable NodeAddr<NodeBase*> DataFlowGraph::cloneNode(const NodeAddr<NodeBase*> B) {
790080dd10fSScott Constable   NodeAddr<NodeBase*> NA = newNode(0);
791080dd10fSScott Constable   memcpy(NA.Addr, B.Addr, sizeof(NodeBase));
792080dd10fSScott Constable   // Ref nodes need to have the data-flow links reset.
793080dd10fSScott Constable   if (NA.Addr->getType() == NodeAttrs::Ref) {
794080dd10fSScott Constable     NodeAddr<RefNode*> RA = NA;
795080dd10fSScott Constable     RA.Addr->setReachingDef(0);
796080dd10fSScott Constable     RA.Addr->setSibling(0);
797080dd10fSScott Constable     if (NA.Addr->getKind() == NodeAttrs::Def) {
798080dd10fSScott Constable       NodeAddr<DefNode*> DA = NA;
799080dd10fSScott Constable       DA.Addr->setReachedDef(0);
800080dd10fSScott Constable       DA.Addr->setReachedUse(0);
801080dd10fSScott Constable     }
802080dd10fSScott Constable   }
803080dd10fSScott Constable   return NA;
804080dd10fSScott Constable }
805080dd10fSScott Constable 
806080dd10fSScott Constable // Allocation routines for specific node types/kinds.
807080dd10fSScott Constable 
808080dd10fSScott Constable NodeAddr<UseNode*> DataFlowGraph::newUse(NodeAddr<InstrNode*> Owner,
809080dd10fSScott Constable       MachineOperand &Op, uint16_t Flags) {
810080dd10fSScott Constable   NodeAddr<UseNode*> UA = newNode(NodeAttrs::Ref | NodeAttrs::Use | Flags);
811080dd10fSScott Constable   UA.Addr->setRegRef(&Op, *this);
812080dd10fSScott Constable   return UA;
813080dd10fSScott Constable }
814080dd10fSScott Constable 
815080dd10fSScott Constable NodeAddr<PhiUseNode*> DataFlowGraph::newPhiUse(NodeAddr<PhiNode*> Owner,
816080dd10fSScott Constable       RegisterRef RR, NodeAddr<BlockNode*> PredB, uint16_t Flags) {
817080dd10fSScott Constable   NodeAddr<PhiUseNode*> PUA = newNode(NodeAttrs::Ref | NodeAttrs::Use | Flags);
818080dd10fSScott Constable   assert(Flags & NodeAttrs::PhiRef);
819080dd10fSScott Constable   PUA.Addr->setRegRef(RR, *this);
820080dd10fSScott Constable   PUA.Addr->setPredecessor(PredB.Id);
821080dd10fSScott Constable   return PUA;
822080dd10fSScott Constable }
823080dd10fSScott Constable 
824080dd10fSScott Constable NodeAddr<DefNode*> DataFlowGraph::newDef(NodeAddr<InstrNode*> Owner,
825080dd10fSScott Constable       MachineOperand &Op, uint16_t Flags) {
826080dd10fSScott Constable   NodeAddr<DefNode*> DA = newNode(NodeAttrs::Ref | NodeAttrs::Def | Flags);
827080dd10fSScott Constable   DA.Addr->setRegRef(&Op, *this);
828080dd10fSScott Constable   return DA;
829080dd10fSScott Constable }
830080dd10fSScott Constable 
831080dd10fSScott Constable NodeAddr<DefNode*> DataFlowGraph::newDef(NodeAddr<InstrNode*> Owner,
832080dd10fSScott Constable       RegisterRef RR, uint16_t Flags) {
833080dd10fSScott Constable   NodeAddr<DefNode*> DA = newNode(NodeAttrs::Ref | NodeAttrs::Def | Flags);
834080dd10fSScott Constable   assert(Flags & NodeAttrs::PhiRef);
835080dd10fSScott Constable   DA.Addr->setRegRef(RR, *this);
836080dd10fSScott Constable   return DA;
837080dd10fSScott Constable }
838080dd10fSScott Constable 
839080dd10fSScott Constable NodeAddr<PhiNode*> DataFlowGraph::newPhi(NodeAddr<BlockNode*> Owner) {
840080dd10fSScott Constable   NodeAddr<PhiNode*> PA = newNode(NodeAttrs::Code | NodeAttrs::Phi);
841080dd10fSScott Constable   Owner.Addr->addPhi(PA, *this);
842080dd10fSScott Constable   return PA;
843080dd10fSScott Constable }
844080dd10fSScott Constable 
845080dd10fSScott Constable NodeAddr<StmtNode*> DataFlowGraph::newStmt(NodeAddr<BlockNode*> Owner,
846080dd10fSScott Constable       MachineInstr *MI) {
847080dd10fSScott Constable   NodeAddr<StmtNode*> SA = newNode(NodeAttrs::Code | NodeAttrs::Stmt);
848080dd10fSScott Constable   SA.Addr->setCode(MI);
849080dd10fSScott Constable   Owner.Addr->addMember(SA, *this);
850080dd10fSScott Constable   return SA;
851080dd10fSScott Constable }
852080dd10fSScott Constable 
853080dd10fSScott Constable NodeAddr<BlockNode*> DataFlowGraph::newBlock(NodeAddr<FuncNode*> Owner,
854080dd10fSScott Constable       MachineBasicBlock *BB) {
855080dd10fSScott Constable   NodeAddr<BlockNode*> BA = newNode(NodeAttrs::Code | NodeAttrs::Block);
856080dd10fSScott Constable   BA.Addr->setCode(BB);
857080dd10fSScott Constable   Owner.Addr->addMember(BA, *this);
858080dd10fSScott Constable   return BA;
859080dd10fSScott Constable }
860080dd10fSScott Constable 
861080dd10fSScott Constable NodeAddr<FuncNode*> DataFlowGraph::newFunc(MachineFunction *MF) {
862080dd10fSScott Constable   NodeAddr<FuncNode*> FA = newNode(NodeAttrs::Code | NodeAttrs::Func);
863080dd10fSScott Constable   FA.Addr->setCode(MF);
864080dd10fSScott Constable   return FA;
865080dd10fSScott Constable }
866080dd10fSScott Constable 
867080dd10fSScott Constable // Build the data flow graph.
868080dd10fSScott Constable void DataFlowGraph::build(unsigned Options) {
869080dd10fSScott Constable   reset();
870080dd10fSScott Constable   Func = newFunc(&MF);
871080dd10fSScott Constable 
872080dd10fSScott Constable   if (MF.empty())
873080dd10fSScott Constable     return;
874080dd10fSScott Constable 
875080dd10fSScott Constable   for (MachineBasicBlock &B : MF) {
876080dd10fSScott Constable     NodeAddr<BlockNode*> BA = newBlock(Func, &B);
877080dd10fSScott Constable     BlockNodes.insert(std::make_pair(&B, BA));
878080dd10fSScott Constable     for (MachineInstr &I : B) {
879080dd10fSScott Constable       if (I.isDebugInstr())
880080dd10fSScott Constable         continue;
881080dd10fSScott Constable       buildStmt(BA, I);
882080dd10fSScott Constable     }
883080dd10fSScott Constable   }
884080dd10fSScott Constable 
885080dd10fSScott Constable   NodeAddr<BlockNode*> EA = Func.Addr->getEntryBlock(*this);
886080dd10fSScott Constable   NodeList Blocks = Func.Addr->members(*this);
887080dd10fSScott Constable 
888080dd10fSScott Constable   // Collect information about block references.
889080dd10fSScott Constable   RegisterSet AllRefs;
890080dd10fSScott Constable   for (NodeAddr<BlockNode*> BA : Blocks)
891080dd10fSScott Constable     for (NodeAddr<InstrNode*> IA : BA.Addr->members(*this))
892080dd10fSScott Constable       for (NodeAddr<RefNode*> RA : IA.Addr->members(*this))
893080dd10fSScott Constable         AllRefs.insert(RA.Addr->getRegRef(*this));
894080dd10fSScott Constable 
895080dd10fSScott Constable   // Collect function live-ins and entry block live-ins.
896080dd10fSScott Constable   MachineRegisterInfo &MRI = MF.getRegInfo();
897080dd10fSScott Constable   MachineBasicBlock &EntryB = *EA.Addr->getCode();
898080dd10fSScott Constable   assert(EntryB.pred_empty() && "Function entry block has predecessors");
899080dd10fSScott Constable   for (std::pair<unsigned,unsigned> P : MRI.liveins())
900080dd10fSScott Constable     LiveIns.insert(RegisterRef(P.first));
901080dd10fSScott Constable   if (MRI.tracksLiveness()) {
902080dd10fSScott Constable     for (auto I : EntryB.liveins())
903080dd10fSScott Constable       LiveIns.insert(RegisterRef(I.PhysReg, I.LaneMask));
904080dd10fSScott Constable   }
905080dd10fSScott Constable 
906080dd10fSScott Constable   // Add function-entry phi nodes for the live-in registers.
907080dd10fSScott Constable   //for (std::pair<RegisterId,LaneBitmask> P : LiveIns) {
908080dd10fSScott Constable   for (auto I = LiveIns.rr_begin(), E = LiveIns.rr_end(); I != E; ++I) {
909080dd10fSScott Constable     RegisterRef RR = *I;
910080dd10fSScott Constable     NodeAddr<PhiNode*> PA = newPhi(EA);
911080dd10fSScott Constable     uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving;
912080dd10fSScott Constable     NodeAddr<DefNode*> DA = newDef(PA, RR, PhiFlags);
913080dd10fSScott Constable     PA.Addr->addMember(DA, *this);
914080dd10fSScott Constable   }
915080dd10fSScott Constable 
916080dd10fSScott Constable   // Add phis for landing pads.
917080dd10fSScott Constable   // Landing pads, unlike usual backs blocks, are not entered through
918080dd10fSScott Constable   // branches in the program, or fall-throughs from other blocks. They
919080dd10fSScott Constable   // are entered from the exception handling runtime and target's ABI
920080dd10fSScott Constable   // may define certain registers as defined on entry to such a block.
921080dd10fSScott Constable   RegisterSet EHRegs = getLandingPadLiveIns();
922080dd10fSScott Constable   if (!EHRegs.empty()) {
923080dd10fSScott Constable     for (NodeAddr<BlockNode*> BA : Blocks) {
924080dd10fSScott Constable       const MachineBasicBlock &B = *BA.Addr->getCode();
925080dd10fSScott Constable       if (!B.isEHPad())
926080dd10fSScott Constable         continue;
927080dd10fSScott Constable 
928080dd10fSScott Constable       // Prepare a list of NodeIds of the block's predecessors.
929080dd10fSScott Constable       NodeList Preds;
930080dd10fSScott Constable       for (MachineBasicBlock *PB : B.predecessors())
931080dd10fSScott Constable         Preds.push_back(findBlock(PB));
932080dd10fSScott Constable 
933080dd10fSScott Constable       // Build phi nodes for each live-in.
934080dd10fSScott Constable       for (RegisterRef RR : EHRegs) {
935080dd10fSScott Constable         NodeAddr<PhiNode*> PA = newPhi(BA);
936080dd10fSScott Constable         uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving;
937080dd10fSScott Constable         // Add def:
938080dd10fSScott Constable         NodeAddr<DefNode*> DA = newDef(PA, RR, PhiFlags);
939080dd10fSScott Constable         PA.Addr->addMember(DA, *this);
940080dd10fSScott Constable         // Add uses (no reaching defs for phi uses):
941080dd10fSScott Constable         for (NodeAddr<BlockNode*> PBA : Preds) {
942080dd10fSScott Constable           NodeAddr<PhiUseNode*> PUA = newPhiUse(PA, RR, PBA);
943080dd10fSScott Constable           PA.Addr->addMember(PUA, *this);
944080dd10fSScott Constable         }
945080dd10fSScott Constable       }
946080dd10fSScott Constable     }
947080dd10fSScott Constable   }
948080dd10fSScott Constable 
949080dd10fSScott Constable   // Build a map "PhiM" which will contain, for each block, the set
950080dd10fSScott Constable   // of references that will require phi definitions in that block.
951080dd10fSScott Constable   BlockRefsMap PhiM;
952080dd10fSScott Constable   for (NodeAddr<BlockNode*> BA : Blocks)
953080dd10fSScott Constable     recordDefsForDF(PhiM, BA);
954080dd10fSScott Constable   for (NodeAddr<BlockNode*> BA : Blocks)
955080dd10fSScott Constable     buildPhis(PhiM, AllRefs, BA);
956080dd10fSScott Constable 
957080dd10fSScott Constable   // Link all the refs. This will recursively traverse the dominator tree.
958080dd10fSScott Constable   DefStackMap DM;
959080dd10fSScott Constable   linkBlockRefs(DM, EA);
960080dd10fSScott Constable 
961080dd10fSScott Constable   // Finally, remove all unused phi nodes.
962080dd10fSScott Constable   if (!(Options & BuildOptions::KeepDeadPhis))
963080dd10fSScott Constable     removeUnusedPhis();
964080dd10fSScott Constable }
965080dd10fSScott Constable 
966080dd10fSScott Constable RegisterRef DataFlowGraph::makeRegRef(unsigned Reg, unsigned Sub) const {
967080dd10fSScott Constable   assert(PhysicalRegisterInfo::isRegMaskId(Reg) ||
968080dd10fSScott Constable          Register::isPhysicalRegister(Reg));
969080dd10fSScott Constable   assert(Reg != 0);
970080dd10fSScott Constable   if (Sub != 0)
971080dd10fSScott Constable     Reg = TRI.getSubReg(Reg, Sub);
972080dd10fSScott Constable   return RegisterRef(Reg);
973080dd10fSScott Constable }
974080dd10fSScott Constable 
975080dd10fSScott Constable RegisterRef DataFlowGraph::makeRegRef(const MachineOperand &Op) const {
976080dd10fSScott Constable   assert(Op.isReg() || Op.isRegMask());
977080dd10fSScott Constable   if (Op.isReg())
978080dd10fSScott Constable     return makeRegRef(Op.getReg(), Op.getSubReg());
979080dd10fSScott Constable   return RegisterRef(PRI.getRegMaskId(Op.getRegMask()), LaneBitmask::getAll());
980080dd10fSScott Constable }
981080dd10fSScott Constable 
982080dd10fSScott Constable RegisterRef DataFlowGraph::restrictRef(RegisterRef AR, RegisterRef BR) const {
983080dd10fSScott Constable   if (AR.Reg == BR.Reg) {
984080dd10fSScott Constable     LaneBitmask M = AR.Mask & BR.Mask;
985080dd10fSScott Constable     return M.any() ? RegisterRef(AR.Reg, M) : RegisterRef();
986080dd10fSScott Constable   }
987080dd10fSScott Constable   // This isn't strictly correct, because the overlap may happen in the
988080dd10fSScott Constable   // part masked out.
989080dd10fSScott Constable   if (PRI.alias(AR, BR))
990080dd10fSScott Constable     return AR;
991080dd10fSScott Constable   return RegisterRef();
992080dd10fSScott Constable }
993080dd10fSScott Constable 
994080dd10fSScott Constable // For each stack in the map DefM, push the delimiter for block B on it.
995080dd10fSScott Constable void DataFlowGraph::markBlock(NodeId B, DefStackMap &DefM) {
996080dd10fSScott Constable   // Push block delimiters.
99761efa3d9SKazu Hirata   for (auto &P : DefM)
99861efa3d9SKazu Hirata     P.second.start_block(B);
999080dd10fSScott Constable }
1000080dd10fSScott Constable 
1001080dd10fSScott Constable // Remove all definitions coming from block B from each stack in DefM.
1002080dd10fSScott Constable void DataFlowGraph::releaseBlock(NodeId B, DefStackMap &DefM) {
1003080dd10fSScott Constable   // Pop all defs from this block from the definition stack. Defs that were
1004080dd10fSScott Constable   // added to the map during the traversal of instructions will not have a
1005080dd10fSScott Constable   // delimiter, but for those, the whole stack will be emptied.
100661efa3d9SKazu Hirata   for (auto &P : DefM)
100761efa3d9SKazu Hirata     P.second.clear_block(B);
1008080dd10fSScott Constable 
1009080dd10fSScott Constable   // Finally, remove empty stacks from the map.
1010080dd10fSScott Constable   for (auto I = DefM.begin(), E = DefM.end(), NextI = I; I != E; I = NextI) {
1011080dd10fSScott Constable     NextI = std::next(I);
1012080dd10fSScott Constable     // This preserves the validity of iterators other than I.
1013080dd10fSScott Constable     if (I->second.empty())
1014080dd10fSScott Constable       DefM.erase(I);
1015080dd10fSScott Constable   }
1016080dd10fSScott Constable }
1017080dd10fSScott Constable 
1018080dd10fSScott Constable // Push all definitions from the instruction node IA to an appropriate
1019080dd10fSScott Constable // stack in DefM.
1020080dd10fSScott Constable void DataFlowGraph::pushAllDefs(NodeAddr<InstrNode*> IA, DefStackMap &DefM) {
1021080dd10fSScott Constable   pushClobbers(IA, DefM);
1022080dd10fSScott Constable   pushDefs(IA, DefM);
1023080dd10fSScott Constable }
1024080dd10fSScott Constable 
1025080dd10fSScott Constable // Push all definitions from the instruction node IA to an appropriate
1026080dd10fSScott Constable // stack in DefM.
1027080dd10fSScott Constable void DataFlowGraph::pushClobbers(NodeAddr<InstrNode*> IA, DefStackMap &DefM) {
1028080dd10fSScott Constable   NodeSet Visited;
1029080dd10fSScott Constable   std::set<RegisterId> Defined;
1030080dd10fSScott Constable 
1031080dd10fSScott Constable   // The important objectives of this function are:
1032080dd10fSScott Constable   // - to be able to handle instructions both while the graph is being
1033080dd10fSScott Constable   //   constructed, and after the graph has been constructed, and
1034080dd10fSScott Constable   // - maintain proper ordering of definitions on the stack for each
1035080dd10fSScott Constable   //   register reference:
1036080dd10fSScott Constable   //   - if there are two or more related defs in IA (i.e. coming from
1037080dd10fSScott Constable   //     the same machine operand), then only push one def on the stack,
1038080dd10fSScott Constable   //   - if there are multiple unrelated defs of non-overlapping
1039080dd10fSScott Constable   //     subregisters of S, then the stack for S will have both (in an
1040080dd10fSScott Constable   //     unspecified order), but the order does not matter from the data-
1041080dd10fSScott Constable   //     -flow perspective.
1042080dd10fSScott Constable 
1043080dd10fSScott Constable   for (NodeAddr<DefNode*> DA : IA.Addr->members_if(IsDef, *this)) {
1044080dd10fSScott Constable     if (Visited.count(DA.Id))
1045080dd10fSScott Constable       continue;
1046080dd10fSScott Constable     if (!(DA.Addr->getFlags() & NodeAttrs::Clobbering))
1047080dd10fSScott Constable       continue;
1048080dd10fSScott Constable 
1049080dd10fSScott Constable     NodeList Rel = getRelatedRefs(IA, DA);
1050080dd10fSScott Constable     NodeAddr<DefNode*> PDA = Rel.front();
1051080dd10fSScott Constable     RegisterRef RR = PDA.Addr->getRegRef(*this);
1052080dd10fSScott Constable 
1053080dd10fSScott Constable     // Push the definition on the stack for the register and all aliases.
1054080dd10fSScott Constable     // The def stack traversal in linkNodeUp will check the exact aliasing.
1055080dd10fSScott Constable     DefM[RR.Reg].push(DA);
1056080dd10fSScott Constable     Defined.insert(RR.Reg);
1057080dd10fSScott Constable     for (RegisterId A : PRI.getAliasSet(RR.Reg)) {
1058080dd10fSScott Constable       // Check that we don't push the same def twice.
1059080dd10fSScott Constable       assert(A != RR.Reg);
1060080dd10fSScott Constable       if (!Defined.count(A))
1061080dd10fSScott Constable         DefM[A].push(DA);
1062080dd10fSScott Constable     }
1063080dd10fSScott Constable     // Mark all the related defs as visited.
1064080dd10fSScott Constable     for (NodeAddr<NodeBase*> T : Rel)
1065080dd10fSScott Constable       Visited.insert(T.Id);
1066080dd10fSScott Constable   }
1067080dd10fSScott Constable }
1068080dd10fSScott Constable 
1069080dd10fSScott Constable // Push all definitions from the instruction node IA to an appropriate
1070080dd10fSScott Constable // stack in DefM.
1071080dd10fSScott Constable void DataFlowGraph::pushDefs(NodeAddr<InstrNode*> IA, DefStackMap &DefM) {
1072080dd10fSScott Constable   NodeSet Visited;
1073080dd10fSScott Constable #ifndef NDEBUG
1074080dd10fSScott Constable   std::set<RegisterId> Defined;
1075080dd10fSScott Constable #endif
1076080dd10fSScott Constable 
1077080dd10fSScott Constable   // The important objectives of this function are:
1078080dd10fSScott Constable   // - to be able to handle instructions both while the graph is being
1079080dd10fSScott Constable   //   constructed, and after the graph has been constructed, and
1080080dd10fSScott Constable   // - maintain proper ordering of definitions on the stack for each
1081080dd10fSScott Constable   //   register reference:
1082080dd10fSScott Constable   //   - if there are two or more related defs in IA (i.e. coming from
1083080dd10fSScott Constable   //     the same machine operand), then only push one def on the stack,
1084080dd10fSScott Constable   //   - if there are multiple unrelated defs of non-overlapping
1085080dd10fSScott Constable   //     subregisters of S, then the stack for S will have both (in an
1086080dd10fSScott Constable   //     unspecified order), but the order does not matter from the data-
1087080dd10fSScott Constable   //     -flow perspective.
1088080dd10fSScott Constable 
1089080dd10fSScott Constable   for (NodeAddr<DefNode*> DA : IA.Addr->members_if(IsDef, *this)) {
1090080dd10fSScott Constable     if (Visited.count(DA.Id))
1091080dd10fSScott Constable       continue;
1092080dd10fSScott Constable     if (DA.Addr->getFlags() & NodeAttrs::Clobbering)
1093080dd10fSScott Constable       continue;
1094080dd10fSScott Constable 
1095080dd10fSScott Constable     NodeList Rel = getRelatedRefs(IA, DA);
1096080dd10fSScott Constable     NodeAddr<DefNode*> PDA = Rel.front();
1097080dd10fSScott Constable     RegisterRef RR = PDA.Addr->getRegRef(*this);
1098080dd10fSScott Constable #ifndef NDEBUG
1099080dd10fSScott Constable     // Assert if the register is defined in two or more unrelated defs.
1100080dd10fSScott Constable     // This could happen if there are two or more def operands defining it.
1101080dd10fSScott Constable     if (!Defined.insert(RR.Reg).second) {
1102080dd10fSScott Constable       MachineInstr *MI = NodeAddr<StmtNode*>(IA).Addr->getCode();
1103080dd10fSScott Constable       dbgs() << "Multiple definitions of register: "
1104080dd10fSScott Constable              << Print<RegisterRef>(RR, *this) << " in\n  " << *MI << "in "
1105080dd10fSScott Constable              << printMBBReference(*MI->getParent()) << '\n';
1106080dd10fSScott Constable       llvm_unreachable(nullptr);
1107080dd10fSScott Constable     }
1108080dd10fSScott Constable #endif
1109080dd10fSScott Constable     // Push the definition on the stack for the register and all aliases.
1110080dd10fSScott Constable     // The def stack traversal in linkNodeUp will check the exact aliasing.
1111080dd10fSScott Constable     DefM[RR.Reg].push(DA);
1112080dd10fSScott Constable     for (RegisterId A : PRI.getAliasSet(RR.Reg)) {
1113080dd10fSScott Constable       // Check that we don't push the same def twice.
1114080dd10fSScott Constable       assert(A != RR.Reg);
1115080dd10fSScott Constable       DefM[A].push(DA);
1116080dd10fSScott Constable     }
1117080dd10fSScott Constable     // Mark all the related defs as visited.
1118080dd10fSScott Constable     for (NodeAddr<NodeBase*> T : Rel)
1119080dd10fSScott Constable       Visited.insert(T.Id);
1120080dd10fSScott Constable   }
1121080dd10fSScott Constable }
1122080dd10fSScott Constable 
1123080dd10fSScott Constable // Return the list of all reference nodes related to RA, including RA itself.
1124080dd10fSScott Constable // See "getNextRelated" for the meaning of a "related reference".
1125080dd10fSScott Constable NodeList DataFlowGraph::getRelatedRefs(NodeAddr<InstrNode*> IA,
1126080dd10fSScott Constable       NodeAddr<RefNode*> RA) const {
1127080dd10fSScott Constable   assert(IA.Id != 0 && RA.Id != 0);
1128080dd10fSScott Constable 
1129080dd10fSScott Constable   NodeList Refs;
1130080dd10fSScott Constable   NodeId Start = RA.Id;
1131080dd10fSScott Constable   do {
1132080dd10fSScott Constable     Refs.push_back(RA);
1133080dd10fSScott Constable     RA = getNextRelated(IA, RA);
1134080dd10fSScott Constable   } while (RA.Id != 0 && RA.Id != Start);
1135080dd10fSScott Constable   return Refs;
1136080dd10fSScott Constable }
1137080dd10fSScott Constable 
1138080dd10fSScott Constable // Clear all information in the graph.
1139080dd10fSScott Constable void DataFlowGraph::reset() {
1140080dd10fSScott Constable   Memory.clear();
1141080dd10fSScott Constable   BlockNodes.clear();
1142080dd10fSScott Constable   Func = NodeAddr<FuncNode*>();
1143080dd10fSScott Constable }
1144080dd10fSScott Constable 
1145080dd10fSScott Constable // Return the next reference node in the instruction node IA that is related
1146080dd10fSScott Constable // to RA. Conceptually, two reference nodes are related if they refer to the
1147080dd10fSScott Constable // same instance of a register access, but differ in flags or other minor
1148080dd10fSScott Constable // characteristics. Specific examples of related nodes are shadow reference
1149080dd10fSScott Constable // nodes.
1150080dd10fSScott Constable // Return the equivalent of nullptr if there are no more related references.
1151080dd10fSScott Constable NodeAddr<RefNode*> DataFlowGraph::getNextRelated(NodeAddr<InstrNode*> IA,
1152080dd10fSScott Constable       NodeAddr<RefNode*> RA) const {
1153080dd10fSScott Constable   assert(IA.Id != 0 && RA.Id != 0);
1154080dd10fSScott Constable 
1155080dd10fSScott Constable   auto Related = [this,RA](NodeAddr<RefNode*> TA) -> bool {
1156080dd10fSScott Constable     if (TA.Addr->getKind() != RA.Addr->getKind())
1157080dd10fSScott Constable       return false;
1158080dd10fSScott Constable     if (TA.Addr->getRegRef(*this) != RA.Addr->getRegRef(*this))
1159080dd10fSScott Constable       return false;
1160080dd10fSScott Constable     return true;
1161080dd10fSScott Constable   };
1162080dd10fSScott Constable   auto RelatedStmt = [&Related,RA](NodeAddr<RefNode*> TA) -> bool {
1163080dd10fSScott Constable     return Related(TA) &&
1164080dd10fSScott Constable            &RA.Addr->getOp() == &TA.Addr->getOp();
1165080dd10fSScott Constable   };
1166080dd10fSScott Constable   auto RelatedPhi = [&Related,RA](NodeAddr<RefNode*> TA) -> bool {
1167080dd10fSScott Constable     if (!Related(TA))
1168080dd10fSScott Constable       return false;
1169080dd10fSScott Constable     if (TA.Addr->getKind() != NodeAttrs::Use)
1170080dd10fSScott Constable       return true;
1171080dd10fSScott Constable     // For phi uses, compare predecessor blocks.
1172080dd10fSScott Constable     const NodeAddr<const PhiUseNode*> TUA = TA;
1173080dd10fSScott Constable     const NodeAddr<const PhiUseNode*> RUA = RA;
1174080dd10fSScott Constable     return TUA.Addr->getPredecessor() == RUA.Addr->getPredecessor();
1175080dd10fSScott Constable   };
1176080dd10fSScott Constable 
1177080dd10fSScott Constable   RegisterRef RR = RA.Addr->getRegRef(*this);
1178080dd10fSScott Constable   if (IA.Addr->getKind() == NodeAttrs::Stmt)
1179080dd10fSScott Constable     return RA.Addr->getNextRef(RR, RelatedStmt, true, *this);
1180080dd10fSScott Constable   return RA.Addr->getNextRef(RR, RelatedPhi, true, *this);
1181080dd10fSScott Constable }
1182080dd10fSScott Constable 
1183080dd10fSScott Constable // Find the next node related to RA in IA that satisfies condition P.
1184080dd10fSScott Constable // If such a node was found, return a pair where the second element is the
1185080dd10fSScott Constable // located node. If such a node does not exist, return a pair where the
1186080dd10fSScott Constable // first element is the element after which such a node should be inserted,
1187080dd10fSScott Constable // and the second element is a null-address.
1188080dd10fSScott Constable template <typename Predicate>
1189080dd10fSScott Constable std::pair<NodeAddr<RefNode*>,NodeAddr<RefNode*>>
1190080dd10fSScott Constable DataFlowGraph::locateNextRef(NodeAddr<InstrNode*> IA, NodeAddr<RefNode*> RA,
1191080dd10fSScott Constable       Predicate P) const {
1192080dd10fSScott Constable   assert(IA.Id != 0 && RA.Id != 0);
1193080dd10fSScott Constable 
1194080dd10fSScott Constable   NodeAddr<RefNode*> NA;
1195080dd10fSScott Constable   NodeId Start = RA.Id;
1196080dd10fSScott Constable   while (true) {
1197080dd10fSScott Constable     NA = getNextRelated(IA, RA);
1198080dd10fSScott Constable     if (NA.Id == 0 || NA.Id == Start)
1199080dd10fSScott Constable       break;
1200080dd10fSScott Constable     if (P(NA))
1201080dd10fSScott Constable       break;
1202080dd10fSScott Constable     RA = NA;
1203080dd10fSScott Constable   }
1204080dd10fSScott Constable 
1205080dd10fSScott Constable   if (NA.Id != 0 && NA.Id != Start)
1206080dd10fSScott Constable     return std::make_pair(RA, NA);
1207080dd10fSScott Constable   return std::make_pair(RA, NodeAddr<RefNode*>());
1208080dd10fSScott Constable }
1209080dd10fSScott Constable 
1210080dd10fSScott Constable // Get the next shadow node in IA corresponding to RA, and optionally create
1211080dd10fSScott Constable // such a node if it does not exist.
1212080dd10fSScott Constable NodeAddr<RefNode*> DataFlowGraph::getNextShadow(NodeAddr<InstrNode*> IA,
1213080dd10fSScott Constable       NodeAddr<RefNode*> RA, bool Create) {
1214080dd10fSScott Constable   assert(IA.Id != 0 && RA.Id != 0);
1215080dd10fSScott Constable 
1216080dd10fSScott Constable   uint16_t Flags = RA.Addr->getFlags() | NodeAttrs::Shadow;
1217080dd10fSScott Constable   auto IsShadow = [Flags] (NodeAddr<RefNode*> TA) -> bool {
1218080dd10fSScott Constable     return TA.Addr->getFlags() == Flags;
1219080dd10fSScott Constable   };
1220080dd10fSScott Constable   auto Loc = locateNextRef(IA, RA, IsShadow);
1221080dd10fSScott Constable   if (Loc.second.Id != 0 || !Create)
1222080dd10fSScott Constable     return Loc.second;
1223080dd10fSScott Constable 
1224080dd10fSScott Constable   // Create a copy of RA and mark is as shadow.
1225080dd10fSScott Constable   NodeAddr<RefNode*> NA = cloneNode(RA);
1226080dd10fSScott Constable   NA.Addr->setFlags(Flags | NodeAttrs::Shadow);
1227080dd10fSScott Constable   IA.Addr->addMemberAfter(Loc.first, NA, *this);
1228080dd10fSScott Constable   return NA;
1229080dd10fSScott Constable }
1230080dd10fSScott Constable 
1231080dd10fSScott Constable // Get the next shadow node in IA corresponding to RA. Return null-address
1232080dd10fSScott Constable // if such a node does not exist.
1233080dd10fSScott Constable NodeAddr<RefNode*> DataFlowGraph::getNextShadow(NodeAddr<InstrNode*> IA,
1234080dd10fSScott Constable       NodeAddr<RefNode*> RA) const {
1235080dd10fSScott Constable   assert(IA.Id != 0 && RA.Id != 0);
1236080dd10fSScott Constable   uint16_t Flags = RA.Addr->getFlags() | NodeAttrs::Shadow;
1237080dd10fSScott Constable   auto IsShadow = [Flags] (NodeAddr<RefNode*> TA) -> bool {
1238080dd10fSScott Constable     return TA.Addr->getFlags() == Flags;
1239080dd10fSScott Constable   };
1240080dd10fSScott Constable   return locateNextRef(IA, RA, IsShadow).second;
1241080dd10fSScott Constable }
1242080dd10fSScott Constable 
1243080dd10fSScott Constable // Create a new statement node in the block node BA that corresponds to
1244080dd10fSScott Constable // the machine instruction MI.
1245080dd10fSScott Constable void DataFlowGraph::buildStmt(NodeAddr<BlockNode*> BA, MachineInstr &In) {
1246080dd10fSScott Constable   NodeAddr<StmtNode*> SA = newStmt(BA, &In);
1247080dd10fSScott Constable 
1248080dd10fSScott Constable   auto isCall = [] (const MachineInstr &In) -> bool {
1249080dd10fSScott Constable     if (In.isCall())
1250080dd10fSScott Constable       return true;
1251080dd10fSScott Constable     // Is tail call?
1252080dd10fSScott Constable     if (In.isBranch()) {
1253080dd10fSScott Constable       for (const MachineOperand &Op : In.operands())
1254080dd10fSScott Constable         if (Op.isGlobal() || Op.isSymbol())
1255080dd10fSScott Constable           return true;
1256080dd10fSScott Constable       // Assume indirect branches are calls. This is for the purpose of
1257080dd10fSScott Constable       // keeping implicit operands, and so it won't hurt on intra-function
1258080dd10fSScott Constable       // indirect branches.
1259080dd10fSScott Constable       if (In.isIndirectBranch())
1260080dd10fSScott Constable         return true;
1261080dd10fSScott Constable     }
1262080dd10fSScott Constable     return false;
1263080dd10fSScott Constable   };
1264080dd10fSScott Constable 
1265080dd10fSScott Constable   auto isDefUndef = [this] (const MachineInstr &In, RegisterRef DR) -> bool {
1266080dd10fSScott Constable     // This instruction defines DR. Check if there is a use operand that
1267080dd10fSScott Constable     // would make DR live on entry to the instruction.
1268080dd10fSScott Constable     for (const MachineOperand &Op : In.operands()) {
1269080dd10fSScott Constable       if (!Op.isReg() || Op.getReg() == 0 || !Op.isUse() || Op.isUndef())
1270080dd10fSScott Constable         continue;
1271080dd10fSScott Constable       RegisterRef UR = makeRegRef(Op);
1272080dd10fSScott Constable       if (PRI.alias(DR, UR))
1273080dd10fSScott Constable         return false;
1274080dd10fSScott Constable     }
1275080dd10fSScott Constable     return true;
1276080dd10fSScott Constable   };
1277080dd10fSScott Constable 
1278080dd10fSScott Constable   bool IsCall = isCall(In);
1279080dd10fSScott Constable   unsigned NumOps = In.getNumOperands();
1280080dd10fSScott Constable 
1281080dd10fSScott Constable   // Avoid duplicate implicit defs. This will not detect cases of implicit
1282080dd10fSScott Constable   // defs that define registers that overlap, but it is not clear how to
1283080dd10fSScott Constable   // interpret that in the absence of explicit defs. Overlapping explicit
1284080dd10fSScott Constable   // defs are likely illegal already.
1285080dd10fSScott Constable   BitVector DoneDefs(TRI.getNumRegs());
1286080dd10fSScott Constable   // Process explicit defs first.
1287080dd10fSScott Constable   for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1288080dd10fSScott Constable     MachineOperand &Op = In.getOperand(OpN);
1289080dd10fSScott Constable     if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1290080dd10fSScott Constable       continue;
1291080dd10fSScott Constable     Register R = Op.getReg();
1292080dd10fSScott Constable     if (!R || !Register::isPhysicalRegister(R))
1293080dd10fSScott Constable       continue;
1294080dd10fSScott Constable     uint16_t Flags = NodeAttrs::None;
1295080dd10fSScott Constable     if (TOI.isPreserving(In, OpN)) {
1296080dd10fSScott Constable       Flags |= NodeAttrs::Preserving;
1297080dd10fSScott Constable       // If the def is preserving, check if it is also undefined.
1298080dd10fSScott Constable       if (isDefUndef(In, makeRegRef(Op)))
1299080dd10fSScott Constable         Flags |= NodeAttrs::Undef;
1300080dd10fSScott Constable     }
1301080dd10fSScott Constable     if (TOI.isClobbering(In, OpN))
1302080dd10fSScott Constable       Flags |= NodeAttrs::Clobbering;
1303080dd10fSScott Constable     if (TOI.isFixedReg(In, OpN))
1304080dd10fSScott Constable       Flags |= NodeAttrs::Fixed;
1305080dd10fSScott Constable     if (IsCall && Op.isDead())
1306080dd10fSScott Constable       Flags |= NodeAttrs::Dead;
1307080dd10fSScott Constable     NodeAddr<DefNode*> DA = newDef(SA, Op, Flags);
1308080dd10fSScott Constable     SA.Addr->addMember(DA, *this);
1309080dd10fSScott Constable     assert(!DoneDefs.test(R));
1310080dd10fSScott Constable     DoneDefs.set(R);
1311080dd10fSScott Constable   }
1312080dd10fSScott Constable 
1313080dd10fSScott Constable   // Process reg-masks (as clobbers).
1314080dd10fSScott Constable   BitVector DoneClobbers(TRI.getNumRegs());
1315080dd10fSScott Constable   for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1316080dd10fSScott Constable     MachineOperand &Op = In.getOperand(OpN);
1317080dd10fSScott Constable     if (!Op.isRegMask())
1318080dd10fSScott Constable       continue;
1319080dd10fSScott Constable     uint16_t Flags = NodeAttrs::Clobbering | NodeAttrs::Fixed |
1320080dd10fSScott Constable                      NodeAttrs::Dead;
1321080dd10fSScott Constable     NodeAddr<DefNode*> DA = newDef(SA, Op, Flags);
1322080dd10fSScott Constable     SA.Addr->addMember(DA, *this);
1323080dd10fSScott Constable     // Record all clobbered registers in DoneDefs.
1324080dd10fSScott Constable     const uint32_t *RM = Op.getRegMask();
1325080dd10fSScott Constable     for (unsigned i = 1, e = TRI.getNumRegs(); i != e; ++i)
1326080dd10fSScott Constable       if (!(RM[i/32] & (1u << (i%32))))
1327080dd10fSScott Constable         DoneClobbers.set(i);
1328080dd10fSScott Constable   }
1329080dd10fSScott Constable 
1330080dd10fSScott Constable   // Process implicit defs, skipping those that have already been added
1331080dd10fSScott Constable   // as explicit.
1332080dd10fSScott Constable   for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1333080dd10fSScott Constable     MachineOperand &Op = In.getOperand(OpN);
1334080dd10fSScott Constable     if (!Op.isReg() || !Op.isDef() || !Op.isImplicit())
1335080dd10fSScott Constable       continue;
1336080dd10fSScott Constable     Register R = Op.getReg();
1337080dd10fSScott Constable     if (!R || !Register::isPhysicalRegister(R) || DoneDefs.test(R))
1338080dd10fSScott Constable       continue;
1339080dd10fSScott Constable     RegisterRef RR = makeRegRef(Op);
1340080dd10fSScott Constable     uint16_t Flags = NodeAttrs::None;
1341080dd10fSScott Constable     if (TOI.isPreserving(In, OpN)) {
1342080dd10fSScott Constable       Flags |= NodeAttrs::Preserving;
1343080dd10fSScott Constable       // If the def is preserving, check if it is also undefined.
1344080dd10fSScott Constable       if (isDefUndef(In, RR))
1345080dd10fSScott Constable         Flags |= NodeAttrs::Undef;
1346080dd10fSScott Constable     }
1347080dd10fSScott Constable     if (TOI.isClobbering(In, OpN))
1348080dd10fSScott Constable       Flags |= NodeAttrs::Clobbering;
1349080dd10fSScott Constable     if (TOI.isFixedReg(In, OpN))
1350080dd10fSScott Constable       Flags |= NodeAttrs::Fixed;
1351080dd10fSScott Constable     if (IsCall && Op.isDead()) {
1352080dd10fSScott Constable       if (DoneClobbers.test(R))
1353080dd10fSScott Constable         continue;
1354080dd10fSScott Constable       Flags |= NodeAttrs::Dead;
1355080dd10fSScott Constable     }
1356080dd10fSScott Constable     NodeAddr<DefNode*> DA = newDef(SA, Op, Flags);
1357080dd10fSScott Constable     SA.Addr->addMember(DA, *this);
1358080dd10fSScott Constable     DoneDefs.set(R);
1359080dd10fSScott Constable   }
1360080dd10fSScott Constable 
1361080dd10fSScott Constable   for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1362080dd10fSScott Constable     MachineOperand &Op = In.getOperand(OpN);
1363080dd10fSScott Constable     if (!Op.isReg() || !Op.isUse())
1364080dd10fSScott Constable       continue;
1365080dd10fSScott Constable     Register R = Op.getReg();
1366080dd10fSScott Constable     if (!R || !Register::isPhysicalRegister(R))
1367080dd10fSScott Constable       continue;
1368080dd10fSScott Constable     uint16_t Flags = NodeAttrs::None;
1369080dd10fSScott Constable     if (Op.isUndef())
1370080dd10fSScott Constable       Flags |= NodeAttrs::Undef;
1371080dd10fSScott Constable     if (TOI.isFixedReg(In, OpN))
1372080dd10fSScott Constable       Flags |= NodeAttrs::Fixed;
1373080dd10fSScott Constable     NodeAddr<UseNode*> UA = newUse(SA, Op, Flags);
1374080dd10fSScott Constable     SA.Addr->addMember(UA, *this);
1375080dd10fSScott Constable   }
1376080dd10fSScott Constable }
1377080dd10fSScott Constable 
1378080dd10fSScott Constable // Scan all defs in the block node BA and record in PhiM the locations of
1379080dd10fSScott Constable // phi nodes corresponding to these defs.
1380080dd10fSScott Constable void DataFlowGraph::recordDefsForDF(BlockRefsMap &PhiM,
1381080dd10fSScott Constable       NodeAddr<BlockNode*> BA) {
1382080dd10fSScott Constable   // Check all defs from block BA and record them in each block in BA's
1383080dd10fSScott Constable   // iterated dominance frontier. This information will later be used to
1384080dd10fSScott Constable   // create phi nodes.
1385080dd10fSScott Constable   MachineBasicBlock *BB = BA.Addr->getCode();
1386080dd10fSScott Constable   assert(BB);
1387080dd10fSScott Constable   auto DFLoc = MDF.find(BB);
1388080dd10fSScott Constable   if (DFLoc == MDF.end() || DFLoc->second.empty())
1389080dd10fSScott Constable     return;
1390080dd10fSScott Constable 
1391080dd10fSScott Constable   // Traverse all instructions in the block and collect the set of all
1392080dd10fSScott Constable   // defined references. For each reference there will be a phi created
1393080dd10fSScott Constable   // in the block's iterated dominance frontier.
1394080dd10fSScott Constable   // This is done to make sure that each defined reference gets only one
1395080dd10fSScott Constable   // phi node, even if it is defined multiple times.
1396080dd10fSScott Constable   RegisterSet Defs;
1397080dd10fSScott Constable   for (NodeAddr<InstrNode*> IA : BA.Addr->members(*this))
1398080dd10fSScott Constable     for (NodeAddr<RefNode*> RA : IA.Addr->members_if(IsDef, *this))
1399080dd10fSScott Constable       Defs.insert(RA.Addr->getRegRef(*this));
1400080dd10fSScott Constable 
1401080dd10fSScott Constable   // Calculate the iterated dominance frontier of BB.
1402080dd10fSScott Constable   const MachineDominanceFrontier::DomSetType &DF = DFLoc->second;
1403080dd10fSScott Constable   SetVector<MachineBasicBlock*> IDF(DF.begin(), DF.end());
1404080dd10fSScott Constable   for (unsigned i = 0; i < IDF.size(); ++i) {
1405080dd10fSScott Constable     auto F = MDF.find(IDF[i]);
1406080dd10fSScott Constable     if (F != MDF.end())
1407080dd10fSScott Constable       IDF.insert(F->second.begin(), F->second.end());
1408080dd10fSScott Constable   }
1409080dd10fSScott Constable 
1410080dd10fSScott Constable   // Finally, add the set of defs to each block in the iterated dominance
1411080dd10fSScott Constable   // frontier.
1412080dd10fSScott Constable   for (auto DB : IDF) {
1413080dd10fSScott Constable     NodeAddr<BlockNode*> DBA = findBlock(DB);
1414080dd10fSScott Constable     PhiM[DBA.Id].insert(Defs.begin(), Defs.end());
1415080dd10fSScott Constable   }
1416080dd10fSScott Constable }
1417080dd10fSScott Constable 
1418080dd10fSScott Constable // Given the locations of phi nodes in the map PhiM, create the phi nodes
1419080dd10fSScott Constable // that are located in the block node BA.
1420080dd10fSScott Constable void DataFlowGraph::buildPhis(BlockRefsMap &PhiM, RegisterSet &AllRefs,
1421080dd10fSScott Constable       NodeAddr<BlockNode*> BA) {
1422080dd10fSScott Constable   // Check if this blocks has any DF defs, i.e. if there are any defs
1423080dd10fSScott Constable   // that this block is in the iterated dominance frontier of.
1424080dd10fSScott Constable   auto HasDF = PhiM.find(BA.Id);
1425080dd10fSScott Constable   if (HasDF == PhiM.end() || HasDF->second.empty())
1426080dd10fSScott Constable     return;
1427080dd10fSScott Constable 
1428080dd10fSScott Constable   // First, remove all R in Refs in such that there exists T in Refs
1429080dd10fSScott Constable   // such that T covers R. In other words, only leave those refs that
1430080dd10fSScott Constable   // are not covered by another ref (i.e. maximal with respect to covering).
1431080dd10fSScott Constable 
1432080dd10fSScott Constable   auto MaxCoverIn = [this] (RegisterRef RR, RegisterSet &RRs) -> RegisterRef {
1433080dd10fSScott Constable     for (RegisterRef I : RRs)
1434080dd10fSScott Constable       if (I != RR && RegisterAggr::isCoverOf(I, RR, PRI))
1435080dd10fSScott Constable         RR = I;
1436080dd10fSScott Constable     return RR;
1437080dd10fSScott Constable   };
1438080dd10fSScott Constable 
1439080dd10fSScott Constable   RegisterSet MaxDF;
1440080dd10fSScott Constable   for (RegisterRef I : HasDF->second)
1441080dd10fSScott Constable     MaxDF.insert(MaxCoverIn(I, HasDF->second));
1442080dd10fSScott Constable 
1443080dd10fSScott Constable   std::vector<RegisterRef> MaxRefs;
1444080dd10fSScott Constable   for (RegisterRef I : MaxDF)
1445080dd10fSScott Constable     MaxRefs.push_back(MaxCoverIn(I, AllRefs));
1446080dd10fSScott Constable 
1447080dd10fSScott Constable   // Now, for each R in MaxRefs, get the alias closure of R. If the closure
1448080dd10fSScott Constable   // only has R in it, create a phi a def for R. Otherwise, create a phi,
1449080dd10fSScott Constable   // and add a def for each S in the closure.
1450080dd10fSScott Constable 
1451080dd10fSScott Constable   // Sort the refs so that the phis will be created in a deterministic order.
1452080dd10fSScott Constable   llvm::sort(MaxRefs);
1453080dd10fSScott Constable   // Remove duplicates.
1454080dd10fSScott Constable   auto NewEnd = std::unique(MaxRefs.begin(), MaxRefs.end());
1455080dd10fSScott Constable   MaxRefs.erase(NewEnd, MaxRefs.end());
1456080dd10fSScott Constable 
1457080dd10fSScott Constable   auto Aliased = [this,&MaxRefs](RegisterRef RR,
1458080dd10fSScott Constable                                  std::vector<unsigned> &Closure) -> bool {
1459080dd10fSScott Constable     for (unsigned I : Closure)
1460080dd10fSScott Constable       if (PRI.alias(RR, MaxRefs[I]))
1461080dd10fSScott Constable         return true;
1462080dd10fSScott Constable     return false;
1463080dd10fSScott Constable   };
1464080dd10fSScott Constable 
1465080dd10fSScott Constable   // Prepare a list of NodeIds of the block's predecessors.
1466080dd10fSScott Constable   NodeList Preds;
1467080dd10fSScott Constable   const MachineBasicBlock *MBB = BA.Addr->getCode();
1468080dd10fSScott Constable   for (MachineBasicBlock *PB : MBB->predecessors())
1469080dd10fSScott Constable     Preds.push_back(findBlock(PB));
1470080dd10fSScott Constable 
1471080dd10fSScott Constable   while (!MaxRefs.empty()) {
1472080dd10fSScott Constable     // Put the first element in the closure, and then add all subsequent
1473080dd10fSScott Constable     // elements from MaxRefs to it, if they alias at least one element
1474080dd10fSScott Constable     // already in the closure.
1475080dd10fSScott Constable     // ClosureIdx: vector of indices in MaxRefs of members of the closure.
1476080dd10fSScott Constable     std::vector<unsigned> ClosureIdx = { 0 };
1477080dd10fSScott Constable     for (unsigned i = 1; i != MaxRefs.size(); ++i)
1478080dd10fSScott Constable       if (Aliased(MaxRefs[i], ClosureIdx))
1479080dd10fSScott Constable         ClosureIdx.push_back(i);
1480080dd10fSScott Constable 
1481080dd10fSScott Constable     // Build a phi for the closure.
1482080dd10fSScott Constable     unsigned CS = ClosureIdx.size();
1483080dd10fSScott Constable     NodeAddr<PhiNode*> PA = newPhi(BA);
1484080dd10fSScott Constable 
1485080dd10fSScott Constable     // Add defs.
1486080dd10fSScott Constable     for (unsigned X = 0; X != CS; ++X) {
1487080dd10fSScott Constable       RegisterRef RR = MaxRefs[ClosureIdx[X]];
1488080dd10fSScott Constable       uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving;
1489080dd10fSScott Constable       NodeAddr<DefNode*> DA = newDef(PA, RR, PhiFlags);
1490080dd10fSScott Constable       PA.Addr->addMember(DA, *this);
1491080dd10fSScott Constable     }
1492080dd10fSScott Constable     // Add phi uses.
1493080dd10fSScott Constable     for (NodeAddr<BlockNode*> PBA : Preds) {
1494080dd10fSScott Constable       for (unsigned X = 0; X != CS; ++X) {
1495080dd10fSScott Constable         RegisterRef RR = MaxRefs[ClosureIdx[X]];
1496080dd10fSScott Constable         NodeAddr<PhiUseNode*> PUA = newPhiUse(PA, RR, PBA);
1497080dd10fSScott Constable         PA.Addr->addMember(PUA, *this);
1498080dd10fSScott Constable       }
1499080dd10fSScott Constable     }
1500080dd10fSScott Constable 
1501080dd10fSScott Constable     // Erase from MaxRefs all elements in the closure.
1502080dd10fSScott Constable     auto Begin = MaxRefs.begin();
1503*bb6447a7SKazu Hirata     for (unsigned Idx : llvm::reverse(ClosureIdx))
1504*bb6447a7SKazu Hirata       MaxRefs.erase(Begin + Idx);
1505080dd10fSScott Constable   }
1506080dd10fSScott Constable }
1507080dd10fSScott Constable 
1508080dd10fSScott Constable // Remove any unneeded phi nodes that were created during the build process.
1509080dd10fSScott Constable void DataFlowGraph::removeUnusedPhis() {
1510080dd10fSScott Constable   // This will remove unused phis, i.e. phis where each def does not reach
1511080dd10fSScott Constable   // any uses or other defs. This will not detect or remove circular phi
1512080dd10fSScott Constable   // chains that are otherwise dead. Unused/dead phis are created during
1513080dd10fSScott Constable   // the build process and this function is intended to remove these cases
1514080dd10fSScott Constable   // that are easily determinable to be unnecessary.
1515080dd10fSScott Constable 
1516080dd10fSScott Constable   SetVector<NodeId> PhiQ;
1517080dd10fSScott Constable   for (NodeAddr<BlockNode*> BA : Func.Addr->members(*this)) {
1518080dd10fSScott Constable     for (auto P : BA.Addr->members_if(IsPhi, *this))
1519080dd10fSScott Constable       PhiQ.insert(P.Id);
1520080dd10fSScott Constable   }
1521080dd10fSScott Constable 
1522080dd10fSScott Constable   static auto HasUsedDef = [](NodeList &Ms) -> bool {
1523080dd10fSScott Constable     for (NodeAddr<NodeBase*> M : Ms) {
1524080dd10fSScott Constable       if (M.Addr->getKind() != NodeAttrs::Def)
1525080dd10fSScott Constable         continue;
1526080dd10fSScott Constable       NodeAddr<DefNode*> DA = M;
1527080dd10fSScott Constable       if (DA.Addr->getReachedDef() != 0 || DA.Addr->getReachedUse() != 0)
1528080dd10fSScott Constable         return true;
1529080dd10fSScott Constable     }
1530080dd10fSScott Constable     return false;
1531080dd10fSScott Constable   };
1532080dd10fSScott Constable 
1533080dd10fSScott Constable   // Any phi, if it is removed, may affect other phis (make them dead).
1534080dd10fSScott Constable   // For each removed phi, collect the potentially affected phis and add
1535080dd10fSScott Constable   // them back to the queue.
1536080dd10fSScott Constable   while (!PhiQ.empty()) {
1537080dd10fSScott Constable     auto PA = addr<PhiNode*>(PhiQ[0]);
1538080dd10fSScott Constable     PhiQ.remove(PA.Id);
1539080dd10fSScott Constable     NodeList Refs = PA.Addr->members(*this);
1540080dd10fSScott Constable     if (HasUsedDef(Refs))
1541080dd10fSScott Constable       continue;
1542080dd10fSScott Constable     for (NodeAddr<RefNode*> RA : Refs) {
1543080dd10fSScott Constable       if (NodeId RD = RA.Addr->getReachingDef()) {
1544080dd10fSScott Constable         auto RDA = addr<DefNode*>(RD);
1545080dd10fSScott Constable         NodeAddr<InstrNode*> OA = RDA.Addr->getOwner(*this);
1546080dd10fSScott Constable         if (IsPhi(OA))
1547080dd10fSScott Constable           PhiQ.insert(OA.Id);
1548080dd10fSScott Constable       }
1549080dd10fSScott Constable       if (RA.Addr->isDef())
1550080dd10fSScott Constable         unlinkDef(RA, true);
1551080dd10fSScott Constable       else
1552080dd10fSScott Constable         unlinkUse(RA, true);
1553080dd10fSScott Constable     }
1554080dd10fSScott Constable     NodeAddr<BlockNode*> BA = PA.Addr->getOwner(*this);
1555080dd10fSScott Constable     BA.Addr->removeMember(PA, *this);
1556080dd10fSScott Constable   }
1557080dd10fSScott Constable }
1558080dd10fSScott Constable 
1559080dd10fSScott Constable // For a given reference node TA in an instruction node IA, connect the
1560080dd10fSScott Constable // reaching def of TA to the appropriate def node. Create any shadow nodes
1561080dd10fSScott Constable // as appropriate.
1562080dd10fSScott Constable template <typename T>
1563080dd10fSScott Constable void DataFlowGraph::linkRefUp(NodeAddr<InstrNode*> IA, NodeAddr<T> TA,
1564080dd10fSScott Constable       DefStack &DS) {
1565080dd10fSScott Constable   if (DS.empty())
1566080dd10fSScott Constable     return;
1567080dd10fSScott Constable   RegisterRef RR = TA.Addr->getRegRef(*this);
1568080dd10fSScott Constable   NodeAddr<T> TAP;
1569080dd10fSScott Constable 
1570080dd10fSScott Constable   // References from the def stack that have been examined so far.
1571080dd10fSScott Constable   RegisterAggr Defs(PRI);
1572080dd10fSScott Constable 
1573080dd10fSScott Constable   for (auto I = DS.top(), E = DS.bottom(); I != E; I.down()) {
1574080dd10fSScott Constable     RegisterRef QR = I->Addr->getRegRef(*this);
1575080dd10fSScott Constable 
1576080dd10fSScott Constable     // Skip all defs that are aliased to any of the defs that we have already
1577080dd10fSScott Constable     // seen. If this completes a cover of RR, stop the stack traversal.
1578080dd10fSScott Constable     bool Alias = Defs.hasAliasOf(QR);
1579080dd10fSScott Constable     bool Cover = Defs.insert(QR).hasCoverOf(RR);
1580080dd10fSScott Constable     if (Alias) {
1581080dd10fSScott Constable       if (Cover)
1582080dd10fSScott Constable         break;
1583080dd10fSScott Constable       continue;
1584080dd10fSScott Constable     }
1585080dd10fSScott Constable 
1586080dd10fSScott Constable     // The reaching def.
1587080dd10fSScott Constable     NodeAddr<DefNode*> RDA = *I;
1588080dd10fSScott Constable 
1589080dd10fSScott Constable     // Pick the reached node.
1590080dd10fSScott Constable     if (TAP.Id == 0) {
1591080dd10fSScott Constable       TAP = TA;
1592080dd10fSScott Constable     } else {
1593080dd10fSScott Constable       // Mark the existing ref as "shadow" and create a new shadow.
1594080dd10fSScott Constable       TAP.Addr->setFlags(TAP.Addr->getFlags() | NodeAttrs::Shadow);
1595080dd10fSScott Constable       TAP = getNextShadow(IA, TAP, true);
1596080dd10fSScott Constable     }
1597080dd10fSScott Constable 
1598080dd10fSScott Constable     // Create the link.
1599080dd10fSScott Constable     TAP.Addr->linkToDef(TAP.Id, RDA);
1600080dd10fSScott Constable 
1601080dd10fSScott Constable     if (Cover)
1602080dd10fSScott Constable       break;
1603080dd10fSScott Constable   }
1604080dd10fSScott Constable }
1605080dd10fSScott Constable 
1606080dd10fSScott Constable // Create data-flow links for all reference nodes in the statement node SA.
1607080dd10fSScott Constable template <typename Predicate>
1608080dd10fSScott Constable void DataFlowGraph::linkStmtRefs(DefStackMap &DefM, NodeAddr<StmtNode*> SA,
1609080dd10fSScott Constable       Predicate P) {
1610080dd10fSScott Constable #ifndef NDEBUG
1611080dd10fSScott Constable   RegisterSet Defs;
1612080dd10fSScott Constable #endif
1613080dd10fSScott Constable 
1614080dd10fSScott Constable   // Link all nodes (upwards in the data-flow) with their reaching defs.
1615080dd10fSScott Constable   for (NodeAddr<RefNode*> RA : SA.Addr->members_if(P, *this)) {
1616080dd10fSScott Constable     uint16_t Kind = RA.Addr->getKind();
1617080dd10fSScott Constable     assert(Kind == NodeAttrs::Def || Kind == NodeAttrs::Use);
1618080dd10fSScott Constable     RegisterRef RR = RA.Addr->getRegRef(*this);
1619080dd10fSScott Constable #ifndef NDEBUG
1620080dd10fSScott Constable     // Do not expect multiple defs of the same reference.
1621080dd10fSScott Constable     assert(Kind != NodeAttrs::Def || !Defs.count(RR));
1622080dd10fSScott Constable     Defs.insert(RR);
1623080dd10fSScott Constable #endif
1624080dd10fSScott Constable 
1625080dd10fSScott Constable     auto F = DefM.find(RR.Reg);
1626080dd10fSScott Constable     if (F == DefM.end())
1627080dd10fSScott Constable       continue;
1628080dd10fSScott Constable     DefStack &DS = F->second;
1629080dd10fSScott Constable     if (Kind == NodeAttrs::Use)
1630080dd10fSScott Constable       linkRefUp<UseNode*>(SA, RA, DS);
1631080dd10fSScott Constable     else if (Kind == NodeAttrs::Def)
1632080dd10fSScott Constable       linkRefUp<DefNode*>(SA, RA, DS);
1633080dd10fSScott Constable     else
1634080dd10fSScott Constable       llvm_unreachable("Unexpected node in instruction");
1635080dd10fSScott Constable   }
1636080dd10fSScott Constable }
1637080dd10fSScott Constable 
1638080dd10fSScott Constable // Create data-flow links for all instructions in the block node BA. This
1639080dd10fSScott Constable // will include updating any phi nodes in BA.
1640080dd10fSScott Constable void DataFlowGraph::linkBlockRefs(DefStackMap &DefM, NodeAddr<BlockNode*> BA) {
1641080dd10fSScott Constable   // Push block delimiters.
1642080dd10fSScott Constable   markBlock(BA.Id, DefM);
1643080dd10fSScott Constable 
1644080dd10fSScott Constable   auto IsClobber = [] (NodeAddr<RefNode*> RA) -> bool {
1645080dd10fSScott Constable     return IsDef(RA) && (RA.Addr->getFlags() & NodeAttrs::Clobbering);
1646080dd10fSScott Constable   };
1647080dd10fSScott Constable   auto IsNoClobber = [] (NodeAddr<RefNode*> RA) -> bool {
1648080dd10fSScott Constable     return IsDef(RA) && !(RA.Addr->getFlags() & NodeAttrs::Clobbering);
1649080dd10fSScott Constable   };
1650080dd10fSScott Constable 
1651080dd10fSScott Constable   assert(BA.Addr && "block node address is needed to create a data-flow link");
1652080dd10fSScott Constable   // For each non-phi instruction in the block, link all the defs and uses
1653080dd10fSScott Constable   // to their reaching defs. For any member of the block (including phis),
1654080dd10fSScott Constable   // push the defs on the corresponding stacks.
1655080dd10fSScott Constable   for (NodeAddr<InstrNode*> IA : BA.Addr->members(*this)) {
1656080dd10fSScott Constable     // Ignore phi nodes here. They will be linked part by part from the
1657080dd10fSScott Constable     // predecessors.
1658080dd10fSScott Constable     if (IA.Addr->getKind() == NodeAttrs::Stmt) {
1659080dd10fSScott Constable       linkStmtRefs(DefM, IA, IsUse);
1660080dd10fSScott Constable       linkStmtRefs(DefM, IA, IsClobber);
1661080dd10fSScott Constable     }
1662080dd10fSScott Constable 
1663080dd10fSScott Constable     // Push the definitions on the stack.
1664080dd10fSScott Constable     pushClobbers(IA, DefM);
1665080dd10fSScott Constable 
1666080dd10fSScott Constable     if (IA.Addr->getKind() == NodeAttrs::Stmt)
1667080dd10fSScott Constable       linkStmtRefs(DefM, IA, IsNoClobber);
1668080dd10fSScott Constable 
1669080dd10fSScott Constable     pushDefs(IA, DefM);
1670080dd10fSScott Constable   }
1671080dd10fSScott Constable 
1672080dd10fSScott Constable   // Recursively process all children in the dominator tree.
1673080dd10fSScott Constable   MachineDomTreeNode *N = MDT.getNode(BA.Addr->getCode());
1674080dd10fSScott Constable   for (auto I : *N) {
1675080dd10fSScott Constable     MachineBasicBlock *SB = I->getBlock();
1676080dd10fSScott Constable     NodeAddr<BlockNode*> SBA = findBlock(SB);
1677080dd10fSScott Constable     linkBlockRefs(DefM, SBA);
1678080dd10fSScott Constable   }
1679080dd10fSScott Constable 
1680080dd10fSScott Constable   // Link the phi uses from the successor blocks.
1681080dd10fSScott Constable   auto IsUseForBA = [BA](NodeAddr<NodeBase*> NA) -> bool {
1682080dd10fSScott Constable     if (NA.Addr->getKind() != NodeAttrs::Use)
1683080dd10fSScott Constable       return false;
1684080dd10fSScott Constable     assert(NA.Addr->getFlags() & NodeAttrs::PhiRef);
1685080dd10fSScott Constable     NodeAddr<PhiUseNode*> PUA = NA;
1686080dd10fSScott Constable     return PUA.Addr->getPredecessor() == BA.Id;
1687080dd10fSScott Constable   };
1688080dd10fSScott Constable 
1689080dd10fSScott Constable   RegisterSet EHLiveIns = getLandingPadLiveIns();
1690080dd10fSScott Constable   MachineBasicBlock *MBB = BA.Addr->getCode();
1691080dd10fSScott Constable 
1692080dd10fSScott Constable   for (MachineBasicBlock *SB : MBB->successors()) {
1693080dd10fSScott Constable     bool IsEHPad = SB->isEHPad();
1694080dd10fSScott Constable     NodeAddr<BlockNode*> SBA = findBlock(SB);
1695080dd10fSScott Constable     for (NodeAddr<InstrNode*> IA : SBA.Addr->members_if(IsPhi, *this)) {
1696080dd10fSScott Constable       // Do not link phi uses for landing pad live-ins.
1697080dd10fSScott Constable       if (IsEHPad) {
1698080dd10fSScott Constable         // Find what register this phi is for.
1699080dd10fSScott Constable         NodeAddr<RefNode*> RA = IA.Addr->getFirstMember(*this);
1700080dd10fSScott Constable         assert(RA.Id != 0);
1701080dd10fSScott Constable         if (EHLiveIns.count(RA.Addr->getRegRef(*this)))
1702080dd10fSScott Constable           continue;
1703080dd10fSScott Constable       }
1704080dd10fSScott Constable       // Go over each phi use associated with MBB, and link it.
1705080dd10fSScott Constable       for (auto U : IA.Addr->members_if(IsUseForBA, *this)) {
1706080dd10fSScott Constable         NodeAddr<PhiUseNode*> PUA = U;
1707080dd10fSScott Constable         RegisterRef RR = PUA.Addr->getRegRef(*this);
1708080dd10fSScott Constable         linkRefUp<UseNode*>(IA, PUA, DefM[RR.Reg]);
1709080dd10fSScott Constable       }
1710080dd10fSScott Constable     }
1711080dd10fSScott Constable   }
1712080dd10fSScott Constable 
1713080dd10fSScott Constable   // Pop all defs from this block from the definition stacks.
1714080dd10fSScott Constable   releaseBlock(BA.Id, DefM);
1715080dd10fSScott Constable }
1716080dd10fSScott Constable 
1717080dd10fSScott Constable // Remove the use node UA from any data-flow and structural links.
1718080dd10fSScott Constable void DataFlowGraph::unlinkUseDF(NodeAddr<UseNode*> UA) {
1719080dd10fSScott Constable   NodeId RD = UA.Addr->getReachingDef();
1720080dd10fSScott Constable   NodeId Sib = UA.Addr->getSibling();
1721080dd10fSScott Constable 
1722080dd10fSScott Constable   if (RD == 0) {
1723080dd10fSScott Constable     assert(Sib == 0);
1724080dd10fSScott Constable     return;
1725080dd10fSScott Constable   }
1726080dd10fSScott Constable 
1727080dd10fSScott Constable   auto RDA = addr<DefNode*>(RD);
1728080dd10fSScott Constable   auto TA = addr<UseNode*>(RDA.Addr->getReachedUse());
1729080dd10fSScott Constable   if (TA.Id == UA.Id) {
1730080dd10fSScott Constable     RDA.Addr->setReachedUse(Sib);
1731080dd10fSScott Constable     return;
1732080dd10fSScott Constable   }
1733080dd10fSScott Constable 
1734080dd10fSScott Constable   while (TA.Id != 0) {
1735080dd10fSScott Constable     NodeId S = TA.Addr->getSibling();
1736080dd10fSScott Constable     if (S == UA.Id) {
1737080dd10fSScott Constable       TA.Addr->setSibling(UA.Addr->getSibling());
1738080dd10fSScott Constable       return;
1739080dd10fSScott Constable     }
1740080dd10fSScott Constable     TA = addr<UseNode*>(S);
1741080dd10fSScott Constable   }
1742080dd10fSScott Constable }
1743080dd10fSScott Constable 
1744080dd10fSScott Constable // Remove the def node DA from any data-flow and structural links.
1745080dd10fSScott Constable void DataFlowGraph::unlinkDefDF(NodeAddr<DefNode*> DA) {
1746080dd10fSScott Constable   //
1747080dd10fSScott Constable   //         RD
1748080dd10fSScott Constable   //         | reached
1749080dd10fSScott Constable   //         | def
1750080dd10fSScott Constable   //         :
1751080dd10fSScott Constable   //         .
1752080dd10fSScott Constable   //        +----+
1753080dd10fSScott Constable   // ... -- | DA | -- ... -- 0  : sibling chain of DA
1754080dd10fSScott Constable   //        +----+
1755080dd10fSScott Constable   //         |  | reached
1756080dd10fSScott Constable   //         |  : def
1757080dd10fSScott Constable   //         |  .
1758080dd10fSScott Constable   //         | ...  : Siblings (defs)
1759080dd10fSScott Constable   //         |
1760080dd10fSScott Constable   //         : reached
1761080dd10fSScott Constable   //         . use
1762080dd10fSScott Constable   //        ... : sibling chain of reached uses
1763080dd10fSScott Constable 
1764080dd10fSScott Constable   NodeId RD = DA.Addr->getReachingDef();
1765080dd10fSScott Constable 
1766080dd10fSScott Constable   // Visit all siblings of the reached def and reset their reaching defs.
1767080dd10fSScott Constable   // Also, defs reached by DA are now "promoted" to being reached by RD,
1768080dd10fSScott Constable   // so all of them will need to be spliced into the sibling chain where
1769080dd10fSScott Constable   // DA belongs.
1770080dd10fSScott Constable   auto getAllNodes = [this] (NodeId N) -> NodeList {
1771080dd10fSScott Constable     NodeList Res;
1772080dd10fSScott Constable     while (N) {
1773080dd10fSScott Constable       auto RA = addr<RefNode*>(N);
1774080dd10fSScott Constable       // Keep the nodes in the exact sibling order.
1775080dd10fSScott Constable       Res.push_back(RA);
1776080dd10fSScott Constable       N = RA.Addr->getSibling();
1777080dd10fSScott Constable     }
1778080dd10fSScott Constable     return Res;
1779080dd10fSScott Constable   };
1780080dd10fSScott Constable   NodeList ReachedDefs = getAllNodes(DA.Addr->getReachedDef());
1781080dd10fSScott Constable   NodeList ReachedUses = getAllNodes(DA.Addr->getReachedUse());
1782080dd10fSScott Constable 
1783080dd10fSScott Constable   if (RD == 0) {
1784080dd10fSScott Constable     for (NodeAddr<RefNode*> I : ReachedDefs)
1785080dd10fSScott Constable       I.Addr->setSibling(0);
1786080dd10fSScott Constable     for (NodeAddr<RefNode*> I : ReachedUses)
1787080dd10fSScott Constable       I.Addr->setSibling(0);
1788080dd10fSScott Constable   }
1789080dd10fSScott Constable   for (NodeAddr<DefNode*> I : ReachedDefs)
1790080dd10fSScott Constable     I.Addr->setReachingDef(RD);
1791080dd10fSScott Constable   for (NodeAddr<UseNode*> I : ReachedUses)
1792080dd10fSScott Constable     I.Addr->setReachingDef(RD);
1793080dd10fSScott Constable 
1794080dd10fSScott Constable   NodeId Sib = DA.Addr->getSibling();
1795080dd10fSScott Constable   if (RD == 0) {
1796080dd10fSScott Constable     assert(Sib == 0);
1797080dd10fSScott Constable     return;
1798080dd10fSScott Constable   }
1799080dd10fSScott Constable 
1800080dd10fSScott Constable   // Update the reaching def node and remove DA from the sibling list.
1801080dd10fSScott Constable   auto RDA = addr<DefNode*>(RD);
1802080dd10fSScott Constable   auto TA = addr<DefNode*>(RDA.Addr->getReachedDef());
1803080dd10fSScott Constable   if (TA.Id == DA.Id) {
1804080dd10fSScott Constable     // If DA is the first reached def, just update the RD's reached def
1805080dd10fSScott Constable     // to the DA's sibling.
1806080dd10fSScott Constable     RDA.Addr->setReachedDef(Sib);
1807080dd10fSScott Constable   } else {
1808080dd10fSScott Constable     // Otherwise, traverse the sibling list of the reached defs and remove
1809080dd10fSScott Constable     // DA from it.
1810080dd10fSScott Constable     while (TA.Id != 0) {
1811080dd10fSScott Constable       NodeId S = TA.Addr->getSibling();
1812080dd10fSScott Constable       if (S == DA.Id) {
1813080dd10fSScott Constable         TA.Addr->setSibling(Sib);
1814080dd10fSScott Constable         break;
1815080dd10fSScott Constable       }
1816080dd10fSScott Constable       TA = addr<DefNode*>(S);
1817080dd10fSScott Constable     }
1818080dd10fSScott Constable   }
1819080dd10fSScott Constable 
1820080dd10fSScott Constable   // Splice the DA's reached defs into the RDA's reached def chain.
1821080dd10fSScott Constable   if (!ReachedDefs.empty()) {
1822080dd10fSScott Constable     auto Last = NodeAddr<DefNode*>(ReachedDefs.back());
1823080dd10fSScott Constable     Last.Addr->setSibling(RDA.Addr->getReachedDef());
1824080dd10fSScott Constable     RDA.Addr->setReachedDef(ReachedDefs.front().Id);
1825080dd10fSScott Constable   }
1826080dd10fSScott Constable   // Splice the DA's reached uses into the RDA's reached use chain.
1827080dd10fSScott Constable   if (!ReachedUses.empty()) {
1828080dd10fSScott Constable     auto Last = NodeAddr<UseNode*>(ReachedUses.back());
1829080dd10fSScott Constable     Last.Addr->setSibling(RDA.Addr->getReachedUse());
1830080dd10fSScott Constable     RDA.Addr->setReachedUse(ReachedUses.front().Id);
1831080dd10fSScott Constable   }
1832080dd10fSScott Constable }
1833