xref: /llvm-project/llvm/lib/CodeGen/RDFGraph.cpp (revision ffa2bbc7f4af99ee839e20d6ee73b2d1a2a5ef73)
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 Related = [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   auto RelatedStmt = [&Related, RA](Ref TA) -> bool {
1132     return Related(TA) && &RA.Addr->getOp() == &TA.Addr->getOp();
1133   };
1134   auto RelatedPhi = [&Related, RA](Ref TA) -> bool {
1135     if (!Related(TA))
1136       return false;
1137     if (TA.Addr->getKind() != NodeAttrs::Use)
1138       return true;
1139     // For phi uses, compare predecessor blocks.
1140     const NodeAddr<const PhiUseNode *> TUA = TA;
1141     const NodeAddr<const PhiUseNode *> RUA = RA;
1142     return TUA.Addr->getPredecessor() == RUA.Addr->getPredecessor();
1143   };
1144 
1145   RegisterRef RR = RA.Addr->getRegRef(*this);
1146   if (IA.Addr->getKind() == NodeAttrs::Stmt)
1147     return RA.Addr->getNextRef(RR, RelatedStmt, true, *this);
1148   return RA.Addr->getNextRef(RR, RelatedPhi, true, *this);
1149 }
1150 
1151 // Find the next node related to RA in IA that satisfies condition P.
1152 // If such a node was found, return a pair where the second element is the
1153 // located node. If such a node does not exist, return a pair where the
1154 // first element is the element after which such a node should be inserted,
1155 // and the second element is a null-address.
1156 template <typename Predicate>
1157 std::pair<Ref, Ref> DataFlowGraph::locateNextRef(Instr IA, Ref RA,
1158                                                  Predicate P) const {
1159   assert(IA.Id != 0 && RA.Id != 0);
1160 
1161   Ref NA;
1162   NodeId Start = RA.Id;
1163   while (true) {
1164     NA = getNextRelated(IA, RA);
1165     if (NA.Id == 0 || NA.Id == Start)
1166       break;
1167     if (P(NA))
1168       break;
1169     RA = NA;
1170   }
1171 
1172   if (NA.Id != 0 && NA.Id != Start)
1173     return std::make_pair(RA, NA);
1174   return std::make_pair(RA, Ref());
1175 }
1176 
1177 // Get the next shadow node in IA corresponding to RA, and optionally create
1178 // such a node if it does not exist.
1179 Ref DataFlowGraph::getNextShadow(Instr IA, Ref RA, bool Create) {
1180   assert(IA.Id != 0 && RA.Id != 0);
1181 
1182   uint16_t Flags = RA.Addr->getFlags() | NodeAttrs::Shadow;
1183   auto IsShadow = [Flags](Ref TA) -> bool {
1184     return TA.Addr->getFlags() == Flags;
1185   };
1186   auto Loc = locateNextRef(IA, RA, IsShadow);
1187   if (Loc.second.Id != 0 || !Create)
1188     return Loc.second;
1189 
1190   // Create a copy of RA and mark is as shadow.
1191   Ref NA = cloneNode(RA);
1192   NA.Addr->setFlags(Flags | NodeAttrs::Shadow);
1193   IA.Addr->addMemberAfter(Loc.first, NA, *this);
1194   return NA;
1195 }
1196 
1197 // Get the next shadow node in IA corresponding to RA. Return null-address
1198 // if such a node does not exist.
1199 Ref DataFlowGraph::getNextShadow(Instr IA, Ref RA) const {
1200   assert(IA.Id != 0 && RA.Id != 0);
1201   uint16_t Flags = RA.Addr->getFlags() | NodeAttrs::Shadow;
1202   auto IsShadow = [Flags](Ref TA) -> bool {
1203     return TA.Addr->getFlags() == Flags;
1204   };
1205   return locateNextRef(IA, RA, IsShadow).second;
1206 }
1207 
1208 // Create a new statement node in the block node BA that corresponds to
1209 // the machine instruction MI.
1210 void DataFlowGraph::buildStmt(Block BA, MachineInstr &In) {
1211   Stmt SA = newStmt(BA, &In);
1212 
1213   auto isCall = [](const MachineInstr &In) -> bool {
1214     if (In.isCall())
1215       return true;
1216     // Is tail call?
1217     if (In.isBranch()) {
1218       for (const MachineOperand &Op : In.operands())
1219         if (Op.isGlobal() || Op.isSymbol())
1220           return true;
1221       // Assume indirect branches are calls. This is for the purpose of
1222       // keeping implicit operands, and so it won't hurt on intra-function
1223       // indirect branches.
1224       if (In.isIndirectBranch())
1225         return true;
1226     }
1227     return false;
1228   };
1229 
1230   auto isDefUndef = [this](const MachineInstr &In, RegisterRef DR) -> bool {
1231     // This instruction defines DR. Check if there is a use operand that
1232     // would make DR live on entry to the instruction.
1233     for (const MachineOperand &Op : In.all_uses()) {
1234       if (Op.getReg() == 0 || Op.isUndef())
1235         continue;
1236       RegisterRef UR = makeRegRef(Op);
1237       if (getPRI().alias(DR, UR))
1238         return false;
1239     }
1240     return true;
1241   };
1242 
1243   bool IsCall = isCall(In);
1244   unsigned NumOps = In.getNumOperands();
1245 
1246   // Avoid duplicate implicit defs. This will not detect cases of implicit
1247   // defs that define registers that overlap, but it is not clear how to
1248   // interpret that in the absence of explicit defs. Overlapping explicit
1249   // defs are likely illegal already.
1250   BitVector DoneDefs(TRI.getNumRegs());
1251   // Process explicit defs first.
1252   for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1253     MachineOperand &Op = In.getOperand(OpN);
1254     if (!Op.isReg() || !Op.isDef() || Op.isImplicit())
1255       continue;
1256     Register R = Op.getReg();
1257     if (!R || !R.isPhysical())
1258       continue;
1259     uint16_t Flags = NodeAttrs::None;
1260     if (TOI.isPreserving(In, OpN)) {
1261       Flags |= NodeAttrs::Preserving;
1262       // If the def is preserving, check if it is also undefined.
1263       if (isDefUndef(In, makeRegRef(Op)))
1264         Flags |= NodeAttrs::Undef;
1265     }
1266     if (TOI.isClobbering(In, OpN))
1267       Flags |= NodeAttrs::Clobbering;
1268     if (TOI.isFixedReg(In, OpN))
1269       Flags |= NodeAttrs::Fixed;
1270     if (IsCall && Op.isDead())
1271       Flags |= NodeAttrs::Dead;
1272     Def DA = newDef(SA, Op, Flags);
1273     SA.Addr->addMember(DA, *this);
1274     assert(!DoneDefs.test(R));
1275     DoneDefs.set(R);
1276   }
1277 
1278   // Process reg-masks (as clobbers).
1279   BitVector DoneClobbers(TRI.getNumRegs());
1280   for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1281     MachineOperand &Op = In.getOperand(OpN);
1282     if (!Op.isRegMask())
1283       continue;
1284     uint16_t Flags = NodeAttrs::Clobbering | NodeAttrs::Fixed | NodeAttrs::Dead;
1285     Def DA = newDef(SA, Op, Flags);
1286     SA.Addr->addMember(DA, *this);
1287     // Record all clobbered registers in DoneDefs.
1288     const uint32_t *RM = Op.getRegMask();
1289     for (unsigned i = 1, e = TRI.getNumRegs(); i != e; ++i)
1290       if (!(RM[i / 32] & (1u << (i % 32))))
1291         DoneClobbers.set(i);
1292   }
1293 
1294   // Process implicit defs, skipping those that have already been added
1295   // as explicit.
1296   for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1297     MachineOperand &Op = In.getOperand(OpN);
1298     if (!Op.isReg() || !Op.isDef() || !Op.isImplicit())
1299       continue;
1300     Register R = Op.getReg();
1301     if (!R || !R.isPhysical() || DoneDefs.test(R))
1302       continue;
1303     RegisterRef RR = makeRegRef(Op);
1304     uint16_t Flags = NodeAttrs::None;
1305     if (TOI.isPreserving(In, OpN)) {
1306       Flags |= NodeAttrs::Preserving;
1307       // If the def is preserving, check if it is also undefined.
1308       if (isDefUndef(In, RR))
1309         Flags |= NodeAttrs::Undef;
1310     }
1311     if (TOI.isClobbering(In, OpN))
1312       Flags |= NodeAttrs::Clobbering;
1313     if (TOI.isFixedReg(In, OpN))
1314       Flags |= NodeAttrs::Fixed;
1315     if (IsCall && Op.isDead()) {
1316       if (DoneClobbers.test(R))
1317         continue;
1318       Flags |= NodeAttrs::Dead;
1319     }
1320     Def DA = newDef(SA, Op, Flags);
1321     SA.Addr->addMember(DA, *this);
1322     DoneDefs.set(R);
1323   }
1324 
1325   for (unsigned OpN = 0; OpN < NumOps; ++OpN) {
1326     MachineOperand &Op = In.getOperand(OpN);
1327     if (!Op.isReg() || !Op.isUse())
1328       continue;
1329     Register R = Op.getReg();
1330     if (!R || !R.isPhysical())
1331       continue;
1332     uint16_t Flags = NodeAttrs::None;
1333     if (Op.isUndef())
1334       Flags |= NodeAttrs::Undef;
1335     if (TOI.isFixedReg(In, OpN))
1336       Flags |= NodeAttrs::Fixed;
1337     Use UA = newUse(SA, Op, Flags);
1338     SA.Addr->addMember(UA, *this);
1339   }
1340 }
1341 
1342 // Scan all defs in the block node BA and record in PhiM the locations of
1343 // phi nodes corresponding to these defs.
1344 void DataFlowGraph::recordDefsForDF(BlockRefsMap &PhiM, Block BA) {
1345   // Check all defs from block BA and record them in each block in BA's
1346   // iterated dominance frontier. This information will later be used to
1347   // create phi nodes.
1348   MachineBasicBlock *BB = BA.Addr->getCode();
1349   assert(BB);
1350   auto DFLoc = MDF.find(BB);
1351   if (DFLoc == MDF.end() || DFLoc->second.empty())
1352     return;
1353 
1354   // Traverse all instructions in the block and collect the set of all
1355   // defined references. For each reference there will be a phi created
1356   // in the block's iterated dominance frontier.
1357   // This is done to make sure that each defined reference gets only one
1358   // phi node, even if it is defined multiple times.
1359   RegisterAggr Defs(getPRI());
1360   for (Instr IA : BA.Addr->members(*this))
1361     for (Ref RA : IA.Addr->members_if(IsDef, *this))
1362       Defs.insert(RA.Addr->getRegRef(*this));
1363 
1364   // Calculate the iterated dominance frontier of BB.
1365   const MachineDominanceFrontier::DomSetType &DF = DFLoc->second;
1366   SetVector<MachineBasicBlock *> IDF(DF.begin(), DF.end());
1367   for (unsigned i = 0; i < IDF.size(); ++i) {
1368     auto F = MDF.find(IDF[i]);
1369     if (F != MDF.end())
1370       IDF.insert(F->second.begin(), F->second.end());
1371   }
1372 
1373   // Finally, add the set of defs to each block in the iterated dominance
1374   // frontier.
1375   for (auto *DB : IDF) {
1376     Block DBA = findBlock(DB);
1377     PhiM[DBA.Id].insert(Defs);
1378   }
1379 }
1380 
1381 // Given the locations of phi nodes in the map PhiM, create the phi nodes
1382 // that are located in the block node BA.
1383 void DataFlowGraph::buildPhis(BlockRefsMap &PhiM, Block BA) {
1384   // Check if this blocks has any DF defs, i.e. if there are any defs
1385   // that this block is in the iterated dominance frontier of.
1386   auto HasDF = PhiM.find(BA.Id);
1387   if (HasDF == PhiM.end() || HasDF->second.empty())
1388     return;
1389 
1390   // Prepare a list of NodeIds of the block's predecessors.
1391   NodeList Preds;
1392   const MachineBasicBlock *MBB = BA.Addr->getCode();
1393   for (MachineBasicBlock *PB : MBB->predecessors())
1394     Preds.push_back(findBlock(PB));
1395 
1396   const RegisterAggr &Defs = PhiM[BA.Id];
1397   uint16_t PhiFlags = NodeAttrs::PhiRef | NodeAttrs::Preserving;
1398 
1399   for (RegisterRef RR : Defs.refs()) {
1400     Phi PA = newPhi(BA);
1401     PA.Addr->addMember(newDef(PA, RR, PhiFlags), *this);
1402 
1403     // Add phi uses.
1404     for (Block PBA : Preds) {
1405       PA.Addr->addMember(newPhiUse(PA, RR, PBA), *this);
1406     }
1407   }
1408 }
1409 
1410 // Remove any unneeded phi nodes that were created during the build process.
1411 void DataFlowGraph::removeUnusedPhis() {
1412   // This will remove unused phis, i.e. phis where each def does not reach
1413   // any uses or other defs. This will not detect or remove circular phi
1414   // chains that are otherwise dead. Unused/dead phis are created during
1415   // the build process and this function is intended to remove these cases
1416   // that are easily determinable to be unnecessary.
1417 
1418   SetVector<NodeId> PhiQ;
1419   for (Block BA : TheFunc.Addr->members(*this)) {
1420     for (auto P : BA.Addr->members_if(IsPhi, *this))
1421       PhiQ.insert(P.Id);
1422   }
1423 
1424   static auto HasUsedDef = [](NodeList &Ms) -> bool {
1425     for (Node M : Ms) {
1426       if (M.Addr->getKind() != NodeAttrs::Def)
1427         continue;
1428       Def DA = M;
1429       if (DA.Addr->getReachedDef() != 0 || DA.Addr->getReachedUse() != 0)
1430         return true;
1431     }
1432     return false;
1433   };
1434 
1435   // Any phi, if it is removed, may affect other phis (make them dead).
1436   // For each removed phi, collect the potentially affected phis and add
1437   // them back to the queue.
1438   while (!PhiQ.empty()) {
1439     auto PA = addr<PhiNode *>(PhiQ[0]);
1440     PhiQ.remove(PA.Id);
1441     NodeList Refs = PA.Addr->members(*this);
1442     if (HasUsedDef(Refs))
1443       continue;
1444     for (Ref RA : Refs) {
1445       if (NodeId RD = RA.Addr->getReachingDef()) {
1446         auto RDA = addr<DefNode *>(RD);
1447         Instr OA = RDA.Addr->getOwner(*this);
1448         if (IsPhi(OA))
1449           PhiQ.insert(OA.Id);
1450       }
1451       if (RA.Addr->isDef())
1452         unlinkDef(RA, true);
1453       else
1454         unlinkUse(RA, true);
1455     }
1456     Block BA = PA.Addr->getOwner(*this);
1457     BA.Addr->removeMember(PA, *this);
1458   }
1459 }
1460 
1461 // For a given reference node TA in an instruction node IA, connect the
1462 // reaching def of TA to the appropriate def node. Create any shadow nodes
1463 // as appropriate.
1464 template <typename T>
1465 void DataFlowGraph::linkRefUp(Instr IA, NodeAddr<T> TA, DefStack &DS) {
1466   if (DS.empty())
1467     return;
1468   RegisterRef RR = TA.Addr->getRegRef(*this);
1469   NodeAddr<T> TAP;
1470 
1471   // References from the def stack that have been examined so far.
1472   RegisterAggr Defs(getPRI());
1473 
1474   for (auto I = DS.top(), E = DS.bottom(); I != E; I.down()) {
1475     RegisterRef QR = I->Addr->getRegRef(*this);
1476 
1477     // Skip all defs that are aliased to any of the defs that we have already
1478     // seen. If this completes a cover of RR, stop the stack traversal.
1479     bool Alias = Defs.hasAliasOf(QR);
1480     bool Cover = Defs.insert(QR).hasCoverOf(RR);
1481     if (Alias) {
1482       if (Cover)
1483         break;
1484       continue;
1485     }
1486 
1487     // The reaching def.
1488     Def RDA = *I;
1489 
1490     // Pick the reached node.
1491     if (TAP.Id == 0) {
1492       TAP = TA;
1493     } else {
1494       // Mark the existing ref as "shadow" and create a new shadow.
1495       TAP.Addr->setFlags(TAP.Addr->getFlags() | NodeAttrs::Shadow);
1496       TAP = getNextShadow(IA, TAP, true);
1497     }
1498 
1499     // Create the link.
1500     TAP.Addr->linkToDef(TAP.Id, RDA);
1501 
1502     if (Cover)
1503       break;
1504   }
1505 }
1506 
1507 // Create data-flow links for all reference nodes in the statement node SA.
1508 template <typename Predicate>
1509 void DataFlowGraph::linkStmtRefs(DefStackMap &DefM, Stmt SA, Predicate P) {
1510 #ifndef NDEBUG
1511   RegisterSet Defs(getPRI());
1512 #endif
1513 
1514   // Link all nodes (upwards in the data-flow) with their reaching defs.
1515   for (Ref RA : SA.Addr->members_if(P, *this)) {
1516     uint16_t Kind = RA.Addr->getKind();
1517     assert(Kind == NodeAttrs::Def || Kind == NodeAttrs::Use);
1518     RegisterRef RR = RA.Addr->getRegRef(*this);
1519 #ifndef NDEBUG
1520     // Do not expect multiple defs of the same reference.
1521     assert(Kind != NodeAttrs::Def || !Defs.count(RR));
1522     Defs.insert(RR);
1523 #endif
1524 
1525     auto F = DefM.find(RR.Reg);
1526     if (F == DefM.end())
1527       continue;
1528     DefStack &DS = F->second;
1529     if (Kind == NodeAttrs::Use)
1530       linkRefUp<UseNode *>(SA, RA, DS);
1531     else if (Kind == NodeAttrs::Def)
1532       linkRefUp<DefNode *>(SA, RA, DS);
1533     else
1534       llvm_unreachable("Unexpected node in instruction");
1535   }
1536 }
1537 
1538 // Create data-flow links for all instructions in the block node BA. This
1539 // will include updating any phi nodes in BA.
1540 void DataFlowGraph::linkBlockRefs(DefStackMap &DefM, Block BA) {
1541   // Push block delimiters.
1542   markBlock(BA.Id, DefM);
1543 
1544   auto IsClobber = [](Ref RA) -> bool {
1545     return IsDef(RA) && (RA.Addr->getFlags() & NodeAttrs::Clobbering);
1546   };
1547   auto IsNoClobber = [](Ref RA) -> bool {
1548     return IsDef(RA) && !(RA.Addr->getFlags() & NodeAttrs::Clobbering);
1549   };
1550 
1551   assert(BA.Addr && "block node address is needed to create a data-flow link");
1552   // For each non-phi instruction in the block, link all the defs and uses
1553   // to their reaching defs. For any member of the block (including phis),
1554   // push the defs on the corresponding stacks.
1555   for (Instr IA : BA.Addr->members(*this)) {
1556     // Ignore phi nodes here. They will be linked part by part from the
1557     // predecessors.
1558     if (IA.Addr->getKind() == NodeAttrs::Stmt) {
1559       linkStmtRefs(DefM, IA, IsUse);
1560       linkStmtRefs(DefM, IA, IsClobber);
1561     }
1562 
1563     // Push the definitions on the stack.
1564     pushClobbers(IA, DefM);
1565 
1566     if (IA.Addr->getKind() == NodeAttrs::Stmt)
1567       linkStmtRefs(DefM, IA, IsNoClobber);
1568 
1569     pushDefs(IA, DefM);
1570   }
1571 
1572   // Recursively process all children in the dominator tree.
1573   MachineDomTreeNode *N = MDT.getNode(BA.Addr->getCode());
1574   for (auto *I : *N) {
1575     MachineBasicBlock *SB = I->getBlock();
1576     Block SBA = findBlock(SB);
1577     linkBlockRefs(DefM, SBA);
1578   }
1579 
1580   // Link the phi uses from the successor blocks.
1581   auto IsUseForBA = [BA](Node NA) -> bool {
1582     if (NA.Addr->getKind() != NodeAttrs::Use)
1583       return false;
1584     assert(NA.Addr->getFlags() & NodeAttrs::PhiRef);
1585     PhiUse PUA = NA;
1586     return PUA.Addr->getPredecessor() == BA.Id;
1587   };
1588 
1589   RegisterAggr EHLiveIns = getLandingPadLiveIns();
1590   MachineBasicBlock *MBB = BA.Addr->getCode();
1591 
1592   for (MachineBasicBlock *SB : MBB->successors()) {
1593     bool IsEHPad = SB->isEHPad();
1594     Block SBA = findBlock(SB);
1595     for (Instr IA : SBA.Addr->members_if(IsPhi, *this)) {
1596       // Do not link phi uses for landing pad live-ins.
1597       if (IsEHPad) {
1598         // Find what register this phi is for.
1599         Ref RA = IA.Addr->getFirstMember(*this);
1600         assert(RA.Id != 0);
1601         if (EHLiveIns.hasCoverOf(RA.Addr->getRegRef(*this)))
1602           continue;
1603       }
1604       // Go over each phi use associated with MBB, and link it.
1605       for (auto U : IA.Addr->members_if(IsUseForBA, *this)) {
1606         PhiUse PUA = U;
1607         RegisterRef RR = PUA.Addr->getRegRef(*this);
1608         linkRefUp<UseNode *>(IA, PUA, DefM[RR.Reg]);
1609       }
1610     }
1611   }
1612 
1613   // Pop all defs from this block from the definition stacks.
1614   releaseBlock(BA.Id, DefM);
1615 }
1616 
1617 // Remove the use node UA from any data-flow and structural links.
1618 void DataFlowGraph::unlinkUseDF(Use UA) {
1619   NodeId RD = UA.Addr->getReachingDef();
1620   NodeId Sib = UA.Addr->getSibling();
1621 
1622   if (RD == 0) {
1623     assert(Sib == 0);
1624     return;
1625   }
1626 
1627   auto RDA = addr<DefNode *>(RD);
1628   auto TA = addr<UseNode *>(RDA.Addr->getReachedUse());
1629   if (TA.Id == UA.Id) {
1630     RDA.Addr->setReachedUse(Sib);
1631     return;
1632   }
1633 
1634   while (TA.Id != 0) {
1635     NodeId S = TA.Addr->getSibling();
1636     if (S == UA.Id) {
1637       TA.Addr->setSibling(UA.Addr->getSibling());
1638       return;
1639     }
1640     TA = addr<UseNode *>(S);
1641   }
1642 }
1643 
1644 // Remove the def node DA from any data-flow and structural links.
1645 void DataFlowGraph::unlinkDefDF(Def DA) {
1646   //
1647   //         RD
1648   //         | reached
1649   //         | def
1650   //         :
1651   //         .
1652   //        +----+
1653   // ... -- | DA | -- ... -- 0  : sibling chain of DA
1654   //        +----+
1655   //         |  | reached
1656   //         |  : def
1657   //         |  .
1658   //         | ...  : Siblings (defs)
1659   //         |
1660   //         : reached
1661   //         . use
1662   //        ... : sibling chain of reached uses
1663 
1664   NodeId RD = DA.Addr->getReachingDef();
1665 
1666   // Visit all siblings of the reached def and reset their reaching defs.
1667   // Also, defs reached by DA are now "promoted" to being reached by RD,
1668   // so all of them will need to be spliced into the sibling chain where
1669   // DA belongs.
1670   auto getAllNodes = [this](NodeId N) -> NodeList {
1671     NodeList Res;
1672     while (N) {
1673       auto RA = addr<RefNode *>(N);
1674       // Keep the nodes in the exact sibling order.
1675       Res.push_back(RA);
1676       N = RA.Addr->getSibling();
1677     }
1678     return Res;
1679   };
1680   NodeList ReachedDefs = getAllNodes(DA.Addr->getReachedDef());
1681   NodeList ReachedUses = getAllNodes(DA.Addr->getReachedUse());
1682 
1683   if (RD == 0) {
1684     for (Ref I : ReachedDefs)
1685       I.Addr->setSibling(0);
1686     for (Ref I : ReachedUses)
1687       I.Addr->setSibling(0);
1688   }
1689   for (Def I : ReachedDefs)
1690     I.Addr->setReachingDef(RD);
1691   for (Use I : ReachedUses)
1692     I.Addr->setReachingDef(RD);
1693 
1694   NodeId Sib = DA.Addr->getSibling();
1695   if (RD == 0) {
1696     assert(Sib == 0);
1697     return;
1698   }
1699 
1700   // Update the reaching def node and remove DA from the sibling list.
1701   auto RDA = addr<DefNode *>(RD);
1702   auto TA = addr<DefNode *>(RDA.Addr->getReachedDef());
1703   if (TA.Id == DA.Id) {
1704     // If DA is the first reached def, just update the RD's reached def
1705     // to the DA's sibling.
1706     RDA.Addr->setReachedDef(Sib);
1707   } else {
1708     // Otherwise, traverse the sibling list of the reached defs and remove
1709     // DA from it.
1710     while (TA.Id != 0) {
1711       NodeId S = TA.Addr->getSibling();
1712       if (S == DA.Id) {
1713         TA.Addr->setSibling(Sib);
1714         break;
1715       }
1716       TA = addr<DefNode *>(S);
1717     }
1718   }
1719 
1720   // Splice the DA's reached defs into the RDA's reached def chain.
1721   if (!ReachedDefs.empty()) {
1722     auto Last = Def(ReachedDefs.back());
1723     Last.Addr->setSibling(RDA.Addr->getReachedDef());
1724     RDA.Addr->setReachedDef(ReachedDefs.front().Id);
1725   }
1726   // Splice the DA's reached uses into the RDA's reached use chain.
1727   if (!ReachedUses.empty()) {
1728     auto Last = Use(ReachedUses.back());
1729     Last.Addr->setSibling(RDA.Addr->getReachedUse());
1730     RDA.Addr->setReachedUse(ReachedUses.front().Id);
1731   }
1732 }
1733 
1734 } // end namespace llvm::rdf
1735