1 //===- GIMatchDagPredicate - Represent a predicate to check ---------------===//
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_GIMATCHDAGPREDICATE_H
10 #define LLVM_UTILS_TABLEGEN_GIMATCHDAGPREDICATE_H
12 #include "llvm/ADT/StringRef.h"
13 #include "GIMatchDag.h"
17 class CodeGenInstruction
;
18 class GIMatchDagOperandList
;
19 class GIMatchDagContext
;
22 /// Represents a predicate on the match DAG. This records the details of the
23 /// predicate. The dependencies are stored in the GIMatchDag as edges.
25 /// Instances of this class objects are owned by the GIMatchDag and are not
26 /// shareable between instances of GIMatchDag.
27 class GIMatchDagPredicate
{
29 enum GIMatchDagPredicateKind
{
30 GIMatchDagPredicateKind_Opcode
,
31 GIMatchDagPredicateKind_OneOfOpcodes
,
32 GIMatchDagPredicateKind_SameMO
,
36 const GIMatchDagPredicateKind Kind
;
38 /// The name of the predicate. For example:
39 /// (FOO $a:s32, $b, $c)
40 /// will cause 's32' to be assigned to this member for the $a predicate.
41 /// Similarly, the opcode predicate will cause 'FOO' to be assigned to this
42 /// member. Anonymous instructions will have a name assigned for debugging
46 /// The operand list for this predicate. This object may be shared with
47 /// other predicates of a similar 'shape'.
48 const GIMatchDagOperandList
&OperandInfo
;
51 GIMatchDagPredicate(GIMatchDagPredicateKind Kind
, StringRef Name
,
52 const GIMatchDagOperandList
&OperandInfo
)
53 : Kind(Kind
), Name(Name
), OperandInfo(OperandInfo
) {}
54 virtual ~GIMatchDagPredicate() {}
56 GIMatchDagPredicateKind
getKind() const { return Kind
; }
58 StringRef
getName() const { return Name
; }
59 const GIMatchDagOperandList
&getOperandInfo() const { return OperandInfo
; }
61 // Generate C++ code to check this predicate. If a partitioner has already
62 // tested this predicate then this function won't be called. If this function
63 // is called, it must emit code and return true to indicate that it did so. If
64 // it ever returns false, then the caller will abort due to an untested
66 virtual bool generateCheckCode(raw_ostream
&OS
, StringRef Indent
,
67 const CodeExpansions
&Expansions
) const {
71 virtual void print(raw_ostream
&OS
) const;
72 virtual void printDescription(raw_ostream
&OS
) const;
74 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
75 virtual LLVM_DUMP_METHOD
void dump() const { print(errs()); }
76 #endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
79 class GIMatchDagOpcodePredicate
: public GIMatchDagPredicate
{
80 const CodeGenInstruction
&Instr
;
83 GIMatchDagOpcodePredicate(GIMatchDagContext
&Ctx
, StringRef Name
,
84 const CodeGenInstruction
&Instr
);
86 static bool classof(const GIMatchDagPredicate
*P
) {
87 return P
->getKind() == GIMatchDagPredicateKind_Opcode
;
90 const CodeGenInstruction
*getInstr() const { return &Instr
; }
92 void printDescription(raw_ostream
&OS
) const override
;
94 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
95 virtual LLVM_DUMP_METHOD
void dump() const override
{ print(errs()); }
96 #endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
99 class GIMatchDagOneOfOpcodesPredicate
: public GIMatchDagPredicate
{
100 SmallVector
<const CodeGenInstruction
*, 4> Instrs
;
103 GIMatchDagOneOfOpcodesPredicate(GIMatchDagContext
&Ctx
, StringRef Name
);
105 void addOpcode(const CodeGenInstruction
*Instr
) { Instrs
.push_back(Instr
); }
107 static bool classof(const GIMatchDagPredicate
*P
) {
108 return P
->getKind() == GIMatchDagPredicateKind_OneOfOpcodes
;
111 const SmallVectorImpl
<const CodeGenInstruction
*> &getInstrs() const {
115 void printDescription(raw_ostream
&OS
) const override
;
117 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
118 virtual LLVM_DUMP_METHOD
void dump() const override
{ print(errs()); }
119 #endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
122 class GIMatchDagSameMOPredicate
: public GIMatchDagPredicate
{
124 GIMatchDagSameMOPredicate(GIMatchDagContext
&Ctx
, StringRef Name
);
126 static bool classof(const GIMatchDagPredicate
*P
) {
127 return P
->getKind() == GIMatchDagPredicateKind_SameMO
;
130 void printDescription(raw_ostream
&OS
) const override
;
132 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
133 virtual LLVM_DUMP_METHOD
void dump() const override
{ print(errs()); }
134 #endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
137 raw_ostream
&operator<<(raw_ostream
&OS
, const GIMatchDagPredicate
&N
);
138 raw_ostream
&operator<<(raw_ostream
&OS
, const GIMatchDagOpcodePredicate
&N
);
140 } // end namespace llvm
141 #endif // ifndef LLVM_UTILS_TABLEGEN_GIMATCHDAGPREDICATE_H