1 //===- GIMatchDagInstr.h - Represent a instruction to be matched ----------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_UTILS_TABLEGEN_GIMATCHDAGINSTR_H
10 #define LLVM_UTILS_TABLEGEN_GIMATCHDAGINSTR_H
12 #include "GIMatchDagOperands.h"
13 #include "llvm/ADT/DenseMap.h"
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
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
28 class GIMatchDagInstr
{
30 using const_user_assigned_operand_names_iterator
=
31 DenseMap
<unsigned, StringRef
>::const_iterator
;
34 /// The match DAG this instruction belongs to.
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.
43 /// The name of the instruction in the pattern as assigned by the user. For
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
;
51 /// The name of each operand (if any) that was assigned by the user. For
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
;
57 /// The operand list for this instruction. This object may be shared with
58 /// other instructions of a similar 'shape'.
59 const GIMatchDagOperandList
&OperandInfo
;
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;
66 /// When true, this instruction will be a starting point for a match attempt.
67 bool IsMatchRoot
= false;
70 GIMatchDagInstr(GIMatchDag
&Dag
, StringRef Name
, StringRef UserAssignedName
,
71 const GIMatchDagOperandList
&OperandInfo
)
72 : Dag(Dag
), Name(Name
), UserAssignedName(UserAssignedName
),
73 OperandInfo(OperandInfo
) {}
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
;
83 const_user_assigned_operand_names_iterator
84 user_assigned_operand_names_begin() const {
85 return UserAssignedNamesForOperands
.begin();
87 const_user_assigned_operand_names_iterator
88 user_assigned_operand_names_end() const {
89 return UserAssignedNamesForOperands
.end();
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());
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.
100 bool isMatchRoot() const { return IsMatchRoot
; }
102 void setOpcodeAnnotation(CodeGenInstruction
*I
) { OpcodeAnnotation
= I
; }
103 CodeGenInstruction
*getOpcodeAnnotation() const { return OpcodeAnnotation
; }
105 void print(raw_ostream
&OS
) const;
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)
112 raw_ostream
&operator<<(raw_ostream
&OS
, const GIMatchDagInstr
&N
);
114 } // end namespace llvm
115 #endif // ifndef LLVM_UTILS_TABLEGEN_GIMATCHDAGINSTR_H