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