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_PREDICATEEXPANDER_H
17 #define LLVM_UTILS_TABLEGEN_PREDICATEEXPANDER_H
18 
19 #include "llvm/ADT/StringRef.h"
20 #include <vector>
21 
22 namespace llvm {
23 
24 class raw_ostream;
25 class Record;
26 
27 class PredicateExpander {
28   bool EmitCallsByRef;
29   bool NegatePredicate;
30   bool ExpandForMC;
31   unsigned IndentLevel;
32   StringRef TargetName;
33 
34   PredicateExpander(const PredicateExpander &) = delete;
35   PredicateExpander &operator=(const PredicateExpander &) = delete;
36 
37 public:
38   PredicateExpander(StringRef Target)
39       : EmitCallsByRef(true), NegatePredicate(false), ExpandForMC(false),
40         IndentLevel(1U), TargetName(Target) {}
41   bool isByRef() const { return EmitCallsByRef; }
42   bool shouldNegate() const { return NegatePredicate; }
43   bool shouldExpandForMC() const { return ExpandForMC; }
44   unsigned getIndentLevel() const { return IndentLevel; }
45   StringRef getTargetName() const { return TargetName; }
46 
47   void setByRef(bool Value) { EmitCallsByRef = Value; }
48   void flipNegatePredicate() { NegatePredicate = !NegatePredicate; }
49   void setNegatePredicate(bool Value) { NegatePredicate = Value; }
50   void setExpandForMC(bool Value) { ExpandForMC = Value; }
51   void setIndentLevel(unsigned Level) { IndentLevel = Level; }
52   void increaseIndentLevel() { ++IndentLevel; }
53   void decreaseIndentLevel() { --IndentLevel; }
54 
55   using RecVec = std::vector<Record *>;
56   void expandTrue(raw_ostream &OS);
57   void expandFalse(raw_ostream &OS);
58   void expandCheckImmOperand(raw_ostream &OS, int OpIndex, int ImmVal,
59                              StringRef FunctionMapper);
60   void expandCheckImmOperand(raw_ostream &OS, int OpIndex, StringRef ImmVal,
61                              StringRef FunctionMapperer);
62   void expandCheckImmOperandSimple(raw_ostream &OS, int OpIndex,
63                                    StringRef FunctionMapper);
64   void expandCheckRegOperand(raw_ostream &OS, int OpIndex, const Record *Reg,
65                              StringRef FunctionMapper);
66   void expandCheckRegOperandSimple(raw_ostream &OS, int OpIndex,
67                                    StringRef FunctionMapper);
68   void expandCheckSameRegOperand(raw_ostream &OS, int First, int Second);
69   void expandCheckNumOperands(raw_ostream &OS, int NumOps);
70   void expandCheckOpcode(raw_ostream &OS, const Record *Inst);
71 
72   void expandCheckPseudo(raw_ostream &OS, const RecVec &Opcodes);
73   void expandCheckOpcode(raw_ostream &OS, const RecVec &Opcodes);
74   void expandPredicateSequence(raw_ostream &OS, const RecVec &Sequence,
75                                bool IsCheckAll);
76   void expandTIIFunctionCall(raw_ostream &OS, StringRef MethodName);
77   void expandCheckIsRegOperand(raw_ostream &OS, int OpIndex);
78   void expandCheckIsImmOperand(raw_ostream &OS, int OpIndex);
79   void expandCheckInvalidRegOperand(raw_ostream &OS, int OpIndex);
80   void expandCheckFunctionPredicate(raw_ostream &OS, StringRef MCInstFn,
81                                     StringRef MachineInstrFn);
82   void expandCheckFunctionPredicateWithTII(raw_ostream &OS, StringRef MCInstFn,
83                                            StringRef MachineInstrFn,
84                                            StringRef TIIPtr);
85   void expandCheckNonPortable(raw_ostream &OS, StringRef CodeBlock);
86   void expandPredicate(raw_ostream &OS, const Record *Rec);
87   void expandReturnStatement(raw_ostream &OS, const Record *Rec);
88   void expandOpcodeSwitchCase(raw_ostream &OS, const Record *Rec);
89   void expandOpcodeSwitchStatement(raw_ostream &OS, const RecVec &Cases,
90                                    const Record *Default);
91   void expandStatement(raw_ostream &OS, const Record *Rec);
92 };
93 
94 // Forward declarations.
95 class STIPredicateFunction;
96 class OpcodeGroup;
97 
98 class STIPredicateExpander : public PredicateExpander {
99   StringRef ClassPrefix;
100   bool ExpandDefinition;
101 
102   STIPredicateExpander(const PredicateExpander &) = delete;
103   STIPredicateExpander &operator=(const PredicateExpander &) = delete;
104 
105   void expandHeader(raw_ostream &OS, const STIPredicateFunction &Fn);
106   void expandPrologue(raw_ostream &OS, const STIPredicateFunction &Fn);
107   void expandOpcodeGroup(raw_ostream &OS, const OpcodeGroup &Group,
108                          bool ShouldUpdateOpcodeMask);
109   void expandBody(raw_ostream &OS, const STIPredicateFunction &Fn);
110   void expandEpilogue(raw_ostream &OS, const STIPredicateFunction &Fn);
111 
112 public:
113   STIPredicateExpander(StringRef Target)
114       : PredicateExpander(Target), ExpandDefinition(false) {}
115 
116   bool shouldExpandDefinition() const { return ExpandDefinition; }
117   StringRef getClassPrefix() const { return ClassPrefix; }
118   void setClassPrefix(StringRef S) { ClassPrefix = S; }
119   void setExpandDefinition(bool Value) { ExpandDefinition = Value; }
120 
121   void expandSTIPredicate(raw_ostream &OS, const STIPredicateFunction &Fn);
122 };
123 
124 } // namespace llvm
125 
126 #endif
127