aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm12/utils/TableGen/GlobalISel/GIMatchDag.cpp
blob: 1a4bd0a7b561a0d91695fc9eeb6043ddc33f73de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//===- GIMatchDag.cpp - A DAG representation of a pattern to be matched ---===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "GIMatchDag.h"

#include "llvm/Support/Format.h"
#include "llvm/TableGen/Record.h"
#include "../CodeGenInstruction.h"

using namespace llvm;

void GIMatchDag::writeDOTGraph(raw_ostream &OS, StringRef ID) const {
  const auto writePorts = [&](StringRef Prefix,
                              const GIMatchDagOperandList &Operands) {
    StringRef Separator = "";
    OS << "{";
    for (const auto &Op : enumerate(Operands)) {
      OS << Separator << "<" << Prefix << format("%d", Op.index()) << ">"
         << "#" << Op.index() << " $" << Op.value().getName();
      Separator = "|";
    }
    OS << "}";
  };

  OS << "digraph \"" << ID << "\" {\n"
     << "  rankdir=\"BT\"\n";
  for (const auto &N : InstrNodes) {
    OS << "  " << format("Node%p", &*N) << " [shape=record,label=\"{";
    writePorts("s", N->getOperandInfo());
    OS << "|" << N->getName();
    if (N->getOpcodeAnnotation())
      OS << "|" << N->getOpcodeAnnotation()->TheDef->getName();
    if (N->isMatchRoot())
      OS << "|Match starts here";
    OS << "|";
    SmallVector<std::pair<unsigned, StringRef>, 8> ToPrint;
    for (const auto &Assignment : N->user_assigned_operand_names())
      ToPrint.emplace_back(Assignment.first, Assignment.second);
    llvm::sort(ToPrint); 
    StringRef Separator = "";
    for (const auto &Assignment : ToPrint) {
      OS << Separator << "$" << Assignment.second << "=getOperand("
         << Assignment.first << ")";
      Separator = ", ";
    }
    OS << format("|%p|", &N);
    writePorts("d", N->getOperandInfo());
    OS << "}\"";
    if (N->isMatchRoot())
      OS << ",color=red";
    OS << "]\n";
  }

  for (const auto &E : Edges) {
    const char *FromFmt = "Node%p:s%d:n";
    const char *ToFmt = "Node%p:d%d:s";
    if (E->getFromMO()->isDef() && !E->getToMO()->isDef())
      std::swap(FromFmt, ToFmt);
    auto From = format(FromFmt, E->getFromMI(), E->getFromMO()->getIdx());
    auto To = format(ToFmt, E->getToMI(), E->getToMO()->getIdx());
    if (E->getFromMO()->isDef() && !E->getToMO()->isDef())
      std::swap(From, To);

    OS << "  " << From << " -> " << To << " [label=\"$" << E->getName();
    if (E->getFromMO()->isDef() == E->getToMO()->isDef())
      OS << " INVALID EDGE!";
    OS << "\"";
    if (E->getFromMO()->isDef() == E->getToMO()->isDef())
      OS << ",color=red";
    else if (E->getFromMO()->isDef() && !E->getToMO()->isDef())
      OS << ",dir=back,arrowtail=crow";
    OS << "]\n";
  }

  for (const auto &N : PredicateNodes) {
    OS << "  " << format("Pred%p", &*N) << " [shape=record,label=\"{";
    writePorts("s", N->getOperandInfo());
    OS << "|" << N->getName() << "|";
    N->printDescription(OS);
    OS << format("|%p|", &N);
    writePorts("d", N->getOperandInfo());
    OS << "}\",style=dotted]\n";
  }

  for (const auto &E : PredicateDependencies) {
    const char *FromMIFmt = "Node%p:e";
    const char *FromMOFmt = "Node%p:s%d:n";
    const char *ToFmt = "Pred%p:d%d:s";
    auto To = format(ToFmt, E->getPredicate(), E->getPredicateOp()->getIdx());
    auto Style = "[style=dotted]";
    if (E->getRequiredMO()) {
      auto From =
          format(FromMOFmt, E->getRequiredMI(), E->getRequiredMO()->getIdx());
      OS << "  " << From << " -> " << To << " " << Style << "\n";
      continue;
    }
    auto From = format(FromMIFmt, E->getRequiredMI());
    OS << "  " << From << " -> " << To << " " << Style << "\n";
  }

  OS << "}\n";
}

LLVM_DUMP_METHOD void GIMatchDag::print(raw_ostream &OS) const {
  OS << "matchdag {\n";
  for (const auto &N : InstrNodes) {
    OS << "  ";
    N->print(OS);
    OS << "\n";
  }
  for (const auto &E : Edges) {
    OS << "  ";
    E->print(OS);
    OS << "\n";
  }

  for (const auto &P : PredicateNodes) {
    OS << "  ";
    P->print(OS);
    OS << "\n";
  }
  for (const auto &D : PredicateDependencies) {
    OS << "  ";
    D->print(OS);
    OS << "\n";
  }
  OS << "}\n";
}

raw_ostream &llvm::operator<<(raw_ostream &OS, const GIMatchDag &G) {
  G.print(OS);
  return OS;
}