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