1//===- GIMatchDagInstr.h - Represent a instruction to be matched ----------===//
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#ifndef LLVM_UTILS_TABLEGEN_GIMATCHDAGINSTR_H
10#define LLVM_UTILS_TABLEGEN_GIMATCHDAGINSTR_H
11
12#include "GIMatchDagOperands.h"
13#include "llvm/ADT/DenseMap.h"
14
15namespace llvm {
16class GIMatchDag;
17
18/// Represents an instruction in the match DAG. This object knows very little
19/// about the actual instruction to be matched as the bulk of that is in
20/// predicates that are associated with the match DAG. It merely knows the names
21/// and indices of any operands that need to be matched in order to allow edges
22/// to link to them.
23///
24/// Instances of this class objects are owned by the GIMatchDag and are not
25/// shareable between instances of GIMatchDag. This is because the Name,
26/// IsMatchRoot, and OpcodeAnnotation are likely to differ between GIMatchDag
27/// instances.
28class GIMatchDagInstr {
29public:
30  using const_user_assigned_operand_names_iterator =
31      DenseMap<unsigned, StringRef>::const_iterator;
32
33protected:
34  /// The match DAG this instruction belongs to.
35  GIMatchDag &Dag;
36
37  /// The name of the instruction in the pattern. For example:
38  ///     (FOO $a, $b, $c):$name
39  /// will cause name to be assigned to this member. Anonymous instructions will
40  /// have a name assigned for debugging purposes.
41  StringRef Name;
42
43  /// The name of the instruction in the pattern as assigned by the user. For
44  /// example:
45  ///     (FOO $a, $b, $c):$name
46  /// will cause name to be assigned to this member. If a name is not provided,
47  /// this will be empty. This name is used to bind variables from rules to the
48  /// matched instruction.
49  StringRef UserAssignedName;
50
51  /// The name of each operand (if any) that was assigned by the user. For
52  /// example:
53  ///     (FOO $a, $b, $c):$name
54  /// will cause {0, "a"}, {1, "b"}, {2, "c} to be inserted into this map.
55  DenseMap<unsigned, StringRef> UserAssignedNamesForOperands;
56
57  /// The operand list for this instruction. This object may be shared with
58  /// other instructions of a similar 'shape'.
59  const GIMatchDagOperandList &OperandInfo;
60
61  /// For debugging purposes, it's helpful to have access to a description of
62  /// the Opcode. However, this object shouldn't use it for more than debugging
63  /// output since predicates are expected to be handled outside the DAG.
64  CodeGenInstruction *OpcodeAnnotation = 0;
65
66  /// When true, this instruction will be a starting point for a match attempt.
67  bool IsMatchRoot = false;
68
69public:
70  GIMatchDagInstr(GIMatchDag &Dag, StringRef Name, StringRef UserAssignedName,
71                  const GIMatchDagOperandList &OperandInfo)
72      : Dag(Dag), Name(Name), UserAssignedName(UserAssignedName),
73        OperandInfo(OperandInfo) {}
74
75  const GIMatchDagOperandList &getOperandInfo() const { return OperandInfo; }
76  StringRef getName() const { return Name; }
77  StringRef getUserAssignedName() const { return UserAssignedName; }
78  void assignNameToOperand(unsigned Idx, StringRef Name) {
79    assert(UserAssignedNamesForOperands[Idx].empty() && "Cannot assign twice");
80    UserAssignedNamesForOperands[Idx] = Name;
81  }
82
83  const_user_assigned_operand_names_iterator
84  user_assigned_operand_names_begin() const {
85    return UserAssignedNamesForOperands.begin();
86  }
87  const_user_assigned_operand_names_iterator
88  user_assigned_operand_names_end() const {
89    return UserAssignedNamesForOperands.end();
90  }
91  iterator_range<const_user_assigned_operand_names_iterator>
92  user_assigned_operand_names() const {
93    return make_range(user_assigned_operand_names_begin(),
94                      user_assigned_operand_names_end());
95  }
96
97  /// Mark this instruction as being a root of the match. This means that the
98  /// matcher will start from this node when attempting to match MIR.
99  void setMatchRoot();
100  bool isMatchRoot() const { return IsMatchRoot; }
101
102  void setOpcodeAnnotation(CodeGenInstruction *I) { OpcodeAnnotation = I; }
103  CodeGenInstruction *getOpcodeAnnotation() const { return OpcodeAnnotation; }
104
105  void print(raw_ostream &OS) const;
106
107#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
108  LLVM_DUMP_METHOD void dump() const { print(errs()); }
109#endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
110};
111
112raw_ostream &operator<<(raw_ostream &OS, const GIMatchDagInstr &N);
113
114} // end namespace llvm
115#endif // ifndef LLVM_UTILS_TABLEGEN_GIMATCHDAGINSTR_H
116