xref: /llvm-project/llvm/lib/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.cpp (revision 0c9f7ef52739b28f42c03c2bd1c87b744b687e6f)
1 //===- DependencyGraph.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 #include "llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h"
10 #include "llvm/ADT/ArrayRef.h"
11 
12 using namespace llvm::sandboxir;
13 
14 #ifndef NDEBUG
15 void DGNode::print(raw_ostream &OS, bool PrintDeps) const {
16   I->dumpOS(OS);
17   if (PrintDeps) {
18     OS << "\n";
19     // Print memory preds.
20     static constexpr const unsigned Indent = 4;
21     for (auto *Pred : MemPreds) {
22       OS.indent(Indent) << "<-";
23       Pred->print(OS, false);
24       OS << "\n";
25     }
26   }
27 }
28 void DGNode::dump() const {
29   print(dbgs());
30   dbgs() << "\n";
31 }
32 #endif // NDEBUG
33 
34 InstrInterval DependencyGraph::extend(ArrayRef<Instruction *> Instrs) {
35   if (Instrs.empty())
36     return {};
37   // TODO: For now create a chain of dependencies.
38   InstrInterval Interval(Instrs);
39   auto *TopI = Interval.top();
40   auto *BotI = Interval.bottom();
41   DGNode *LastN = getOrCreateNode(TopI);
42   for (Instruction *I = TopI->getNextNode(), *E = BotI->getNextNode(); I != E;
43        I = I->getNextNode()) {
44     auto *N = getOrCreateNode(I);
45     N->addMemPred(LastN);
46     LastN = N;
47   }
48   return Interval;
49 }
50 
51 #ifndef NDEBUG
52 void DependencyGraph::print(raw_ostream &OS) const {
53   // InstrToNodeMap is unordered so we need to create an ordered vector.
54   SmallVector<DGNode *> Nodes;
55   Nodes.reserve(InstrToNodeMap.size());
56   for (const auto &Pair : InstrToNodeMap)
57     Nodes.push_back(Pair.second.get());
58   // Sort them based on which one comes first in the BB.
59   sort(Nodes, [](DGNode *N1, DGNode *N2) {
60     return N1->getInstruction()->comesBefore(N2->getInstruction());
61   });
62   for (auto *N : Nodes)
63     N->print(OS, /*PrintDeps=*/true);
64 }
65 
66 void DependencyGraph::dump() const {
67   print(dbgs());
68   dbgs() << "\n";
69 }
70 #endif // NDEBUG
71