xref: /llvm-project/llvm/utils/TableGen/Common/PredicateExpander.h (revision c92137e474d504159bd9d51f125c8a9ba9141109)
1 //===--------------------- PredicateExpander.h ----------------------------===//
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 /// \file
9 /// Functionalities used by the Tablegen backends to expand machine predicates.
10 ///
11 /// See file llvm/Target/TargetInstrPredicate.td for a full list and description
12 /// of all the supported MCInstPredicate classes.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_UTILS_TABLEGEN_COMMON_PREDICATEEXPANDER_H
17 #define LLVM_UTILS_TABLEGEN_COMMON_PREDICATEEXPANDER_H
18 
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/raw_ostream.h"
22 
23 namespace llvm {
24 
25 class Record;
26 
27 class PredicateExpander {
28   bool EmitCallsByRef;
29   bool NegatePredicate;
30   bool ExpandForMC;
31   StringRef TargetName;
32 
33   PredicateExpander(const PredicateExpander &) = delete;
34   PredicateExpander &operator=(const PredicateExpander &) = delete;
35 
36 protected:
37   indent Indent;
38 
39 public:
40   explicit PredicateExpander(StringRef Target, unsigned Indent = 1)
41       : EmitCallsByRef(true), NegatePredicate(false), ExpandForMC(false),
42         TargetName(Target), Indent(Indent, 2) {}
43   bool isByRef() const { return EmitCallsByRef; }
44   bool shouldNegate() const { return NegatePredicate; }
45   bool shouldExpandForMC() const { return ExpandForMC; }
46   indent &getIndent() { return Indent; }
47   StringRef getTargetName() const { return TargetName; }
48 
49   void setByRef(bool Value) { EmitCallsByRef = Value; }
50   void flipNegatePredicate() { NegatePredicate = !NegatePredicate; }
51   void setNegatePredicate(bool Value) { NegatePredicate = Value; }
52   void setExpandForMC(bool Value) { ExpandForMC = Value; }
53 
54   void expandTrue(raw_ostream &OS);
55   void expandFalse(raw_ostream &OS);
56   void expandCheckImmOperand(raw_ostream &OS, int OpIndex, int ImmVal,
57                              StringRef FunctionMapper);
58   void expandCheckImmOperand(raw_ostream &OS, int OpIndex, StringRef ImmVal,
59                              StringRef FunctionMapperer);
60   void expandCheckImmOperandSimple(raw_ostream &OS, int OpIndex,
61                                    StringRef FunctionMapper);
62   void expandCheckImmOperandLT(raw_ostream &OS, int OpIndex, int ImmVal,
63                                StringRef FunctionMapper);
64   void expandCheckImmOperandGT(raw_ostream &OS, int OpIndex, int ImmVal,
65                                StringRef FunctionMapper);
66   void expandCheckRegOperand(raw_ostream &OS, int OpIndex, const Record *Reg,
67                              StringRef FunctionMapper);
68   void expandCheckRegOperandSimple(raw_ostream &OS, int OpIndex,
69                                    StringRef FunctionMapper);
70   void expandCheckSameRegOperand(raw_ostream &OS, int First, int Second);
71   void expandCheckNumOperands(raw_ostream &OS, int NumOps);
72   void expandCheckOpcode(raw_ostream &OS, const Record *Inst);
73 
74   void expandCheckPseudo(raw_ostream &OS, ArrayRef<const Record *> Opcodes);
75   void expandCheckOpcode(raw_ostream &OS, ArrayRef<const Record *> Opcodes);
76   void expandPredicateSequence(raw_ostream &OS,
77                                ArrayRef<const Record *> Sequence,
78                                bool IsCheckAll);
79   void expandTIIFunctionCall(raw_ostream &OS, StringRef MethodName);
80   void expandCheckIsRegOperand(raw_ostream &OS, int OpIndex);
81   void expandCheckIsVRegOperand(raw_ostream &OS, int OpIndex);
82   void expandCheckIsImmOperand(raw_ostream &OS, int OpIndex);
83   void expandCheckInvalidRegOperand(raw_ostream &OS, int OpIndex);
84   void expandCheckFunctionPredicate(raw_ostream &OS, StringRef MCInstFn,
85                                     StringRef MachineInstrFn);
86   void expandCheckFunctionPredicateWithTII(raw_ostream &OS, StringRef MCInstFn,
87                                            StringRef MachineInstrFn,
88                                            StringRef TIIPtr);
89   void expandCheckNonPortable(raw_ostream &OS, StringRef CodeBlock);
90   void expandPredicate(raw_ostream &OS, const Record *Rec);
91   void expandReturnStatement(raw_ostream &OS, const Record *Rec);
92   void expandOpcodeSwitchCase(raw_ostream &OS, const Record *Rec);
93   void expandOpcodeSwitchStatement(raw_ostream &OS,
94                                    ArrayRef<const Record *> Cases,
95                                    const Record *Default);
96   void expandStatement(raw_ostream &OS, const Record *Rec);
97 };
98 
99 // Forward declarations.
100 class STIPredicateFunction;
101 class OpcodeGroup;
102 
103 class STIPredicateExpander : public PredicateExpander {
104   StringRef ClassPrefix;
105   bool ExpandDefinition;
106 
107   STIPredicateExpander(const PredicateExpander &) = delete;
108   STIPredicateExpander &operator=(const PredicateExpander &) = delete;
109 
110   void expandHeader(raw_ostream &OS, const STIPredicateFunction &Fn);
111   void expandPrologue(raw_ostream &OS, const STIPredicateFunction &Fn);
112   void expandOpcodeGroup(raw_ostream &OS, const OpcodeGroup &Group,
113                          bool ShouldUpdateOpcodeMask);
114   void expandBody(raw_ostream &OS, const STIPredicateFunction &Fn);
115   void expandEpilogue(raw_ostream &OS, const STIPredicateFunction &Fn);
116 
117 public:
118   explicit STIPredicateExpander(StringRef Target, unsigned Indent = 1)
119       : PredicateExpander(Target, Indent), ExpandDefinition(false) {}
120 
121   bool shouldExpandDefinition() const { return ExpandDefinition; }
122   StringRef getClassPrefix() const { return ClassPrefix; }
123   void setClassPrefix(StringRef S) { ClassPrefix = S; }
124   void setExpandDefinition(bool Value) { ExpandDefinition = Value; }
125 
126   void expandSTIPredicate(raw_ostream &OS, const STIPredicateFunction &Fn);
127 };
128 
129 } // namespace llvm
130 
131 #endif // LLVM_UTILS_TABLEGEN_COMMON_PREDICATEEXPANDER_H
132