aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm14/utils/TableGen/GlobalISel/GIMatchDag.h
blob: 4c3c610aff745722239551940b0da3ef3381d34b (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//===- GIMatchDag.h - Represent a DAG 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
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_UTILS_TABLEGEN_GIMATCHDAG_H
#define LLVM_UTILS_TABLEGEN_GIMATCHDAG_H

#include "GIMatchDagEdge.h"
#include "GIMatchDagInstr.h"
#include "GIMatchDagOperands.h"
#include "GIMatchDagPredicate.h"
#include "GIMatchDagPredicateDependencyEdge.h"

namespace llvm {

/// This class manages lifetimes for data associated with the GIMatchDag object.
class GIMatchDagContext {
  GIMatchDagOperandListContext OperandListCtx;

public:
  const GIMatchDagOperandList &makeEmptyOperandList() {
    return OperandListCtx.makeEmptyOperandList();
  }

  const GIMatchDagOperandList &makeOperandList(const CodeGenInstruction &I) {
    return OperandListCtx.makeOperandList(I);
  }

  const GIMatchDagOperandList &makeMIPredicateOperandList() {
    return OperandListCtx.makeMIPredicateOperandList();
  }


  const GIMatchDagOperandList &makeTwoMOPredicateOperandList() {
    return OperandListCtx.makeTwoMOPredicateOperandList();
  }

  void print(raw_ostream &OS) const {
    OperandListCtx.print(OS);
  }

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  LLVM_DUMP_METHOD void dump() const { print(errs()); }
#endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
};

class GIMatchDag {
public:
  using InstrNodesVec = std::vector<std::unique_ptr<GIMatchDagInstr>>;
  using instr_node_iterator = raw_pointer_iterator<InstrNodesVec::iterator>;
  using const_instr_node_iterator =
      raw_pointer_iterator<InstrNodesVec::const_iterator>;

  using EdgesVec = std::vector<std::unique_ptr<GIMatchDagEdge>>;
  using edge_iterator = raw_pointer_iterator<EdgesVec::iterator>;
  using const_edge_iterator = raw_pointer_iterator<EdgesVec::const_iterator>;

  using PredicateNodesVec = std::vector<std::unique_ptr<GIMatchDagPredicate>>;
  using predicate_iterator = raw_pointer_iterator<PredicateNodesVec::iterator>;
  using const_predicate_iterator =
      raw_pointer_iterator<PredicateNodesVec::const_iterator>;

  using PredicateDependencyEdgesVec =
      std::vector<std::unique_ptr<GIMatchDagPredicateDependencyEdge>>;
  using predicate_edge_iterator =
      raw_pointer_iterator<PredicateDependencyEdgesVec::iterator>;
  using const_predicate_edge_iterator =
      raw_pointer_iterator<PredicateDependencyEdgesVec::const_iterator>;

protected:
  GIMatchDagContext &Ctx;
  InstrNodesVec InstrNodes;
  PredicateNodesVec PredicateNodes;
  EdgesVec Edges;
  PredicateDependencyEdgesVec PredicateDependencies;
  std::vector<GIMatchDagInstr *> MatchRoots;
  // FIXME: This is a temporary measure while we still accept arbitrary code
  //        blocks to fix up the matcher while it's being developed.
  bool HasPostMatchPredicate = false;

public:
  GIMatchDag(GIMatchDagContext &Ctx) : Ctx(Ctx) {}
  GIMatchDag(const GIMatchDag &) = delete;

  GIMatchDagContext &getContext() const { return Ctx; }
  edge_iterator edges_begin() {
    return raw_pointer_iterator<EdgesVec::iterator>(Edges.begin());
  }
  edge_iterator edges_end() {
    return raw_pointer_iterator<EdgesVec::iterator>(Edges.end());
  }
  const_edge_iterator edges_begin() const {
    return raw_pointer_iterator<EdgesVec::const_iterator>(Edges.begin());
  }
  const_edge_iterator edges_end() const {
    return raw_pointer_iterator<EdgesVec::const_iterator>(Edges.end());
  }
  iterator_range<edge_iterator> edges() {
    return make_range(edges_begin(), edges_end());
  }
  iterator_range<const_edge_iterator> edges() const {
    return make_range(edges_begin(), edges_end());
  }
  iterator_range<std::vector<GIMatchDagInstr *>::iterator> roots() {
    return make_range(MatchRoots.begin(), MatchRoots.end());
  }
  iterator_range<std::vector<GIMatchDagInstr *>::const_iterator> roots() const {
    return make_range(MatchRoots.begin(), MatchRoots.end());
  }

  instr_node_iterator instr_nodes_begin() {
    return raw_pointer_iterator<InstrNodesVec::iterator>(InstrNodes.begin());
  }
  instr_node_iterator instr_nodes_end() {
    return raw_pointer_iterator<InstrNodesVec::iterator>(InstrNodes.end());
  }
  const_instr_node_iterator instr_nodes_begin() const {
    return raw_pointer_iterator<InstrNodesVec::const_iterator>(
        InstrNodes.begin());
  }
  const_instr_node_iterator instr_nodes_end() const {
    return raw_pointer_iterator<InstrNodesVec::const_iterator>(
        InstrNodes.end());
  }
  iterator_range<instr_node_iterator> instr_nodes() {
    return make_range(instr_nodes_begin(), instr_nodes_end());
  }
  iterator_range<const_instr_node_iterator> instr_nodes() const {
    return make_range(instr_nodes_begin(), instr_nodes_end());
  }
  predicate_edge_iterator predicate_edges_begin() {
    return raw_pointer_iterator<PredicateDependencyEdgesVec::iterator>(
        PredicateDependencies.begin());
  }
  predicate_edge_iterator predicate_edges_end() {
    return raw_pointer_iterator<PredicateDependencyEdgesVec::iterator>(
        PredicateDependencies.end());
  }
  const_predicate_edge_iterator predicate_edges_begin() const {
    return raw_pointer_iterator<PredicateDependencyEdgesVec::const_iterator>(
        PredicateDependencies.begin());
  }
  const_predicate_edge_iterator predicate_edges_end() const {
    return raw_pointer_iterator<PredicateDependencyEdgesVec::const_iterator>(
        PredicateDependencies.end());
  }
  iterator_range<predicate_edge_iterator> predicate_edges() {
    return make_range(predicate_edges_begin(), predicate_edges_end());
  }
  iterator_range<const_predicate_edge_iterator> predicate_edges() const {
    return make_range(predicate_edges_begin(), predicate_edges_end());
  }
  predicate_iterator predicates_begin() {
    return raw_pointer_iterator<PredicateNodesVec::iterator>(
        PredicateNodes.begin());
  }
  predicate_iterator predicates_end() {
    return raw_pointer_iterator<PredicateNodesVec::iterator>(
        PredicateNodes.end());
  }
  const_predicate_iterator predicates_begin() const {
    return raw_pointer_iterator<PredicateNodesVec::const_iterator>(
        PredicateNodes.begin());
  }
  const_predicate_iterator predicates_end() const {
    return raw_pointer_iterator<PredicateNodesVec::const_iterator>(
        PredicateNodes.end());
  }
  iterator_range<predicate_iterator> predicates() {
    return make_range(predicates_begin(), predicates_end());
  }
  iterator_range<const_predicate_iterator> predicates() const {
    return make_range(predicates_begin(), predicates_end());
  }

  template <class... Args> GIMatchDagInstr *addInstrNode(Args &&... args) {
    auto Obj =
        std::make_unique<GIMatchDagInstr>(*this, std::forward<Args>(args)...);
    auto ObjRaw = Obj.get();
    InstrNodes.push_back(std::move(Obj));
    return ObjRaw;
  }

  template <class T, class... Args>
  T *addPredicateNode(Args &&... args) {
    auto Obj = std::make_unique<T>(getContext(), std::forward<Args>(args)...);
    auto ObjRaw = Obj.get();
    PredicateNodes.push_back(std::move(Obj));
    return ObjRaw;
  }

  template <class... Args> GIMatchDagEdge *addEdge(Args &&... args) {
    auto Obj = std::make_unique<GIMatchDagEdge>(std::forward<Args>(args)...);
    auto ObjRaw = Obj.get();
    Edges.push_back(std::move(Obj));
    return ObjRaw;
  }

  template <class... Args>
  GIMatchDagPredicateDependencyEdge *addPredicateDependency(Args &&... args) {
    auto Obj = std::make_unique<GIMatchDagPredicateDependencyEdge>(
        std::forward<Args>(args)...);
    auto ObjRaw = Obj.get();
    PredicateDependencies.push_back(std::move(Obj));
    return ObjRaw;
  }

  size_t getInstrNodeIdx(instr_node_iterator I) {
    return std::distance(instr_nodes_begin(), I);
  }
  size_t getInstrNodeIdx(const_instr_node_iterator I) const {
    return std::distance(instr_nodes_begin(), I);
  }
  size_t getNumInstrNodes() const { return InstrNodes.size(); }
  size_t getNumEdges() const { return Edges.size(); }
  size_t getNumPredicates() const { return PredicateNodes.size(); }

  void setHasPostMatchPredicate(bool V) { HasPostMatchPredicate = V; }
  bool hasPostMatchPredicate() const { return HasPostMatchPredicate; }

  void addMatchRoot(GIMatchDagInstr *N) { MatchRoots.push_back(N); }

  LLVM_DUMP_METHOD void print(raw_ostream &OS) const;

#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  LLVM_DUMP_METHOD void dump() const { print(errs()); }
#endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)

  void writeDOTGraph(raw_ostream &OS, StringRef ID) const;
};

raw_ostream &operator<<(raw_ostream &OS, const GIMatchDag &G);

} // end namespace llvm

#endif // ifndef LLVM_UTILS_TABLEGEN_GIMATCHDAG_H