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:
PredicateExpander(StringRef Target)38   PredicateExpander(StringRef Target)
39       : EmitCallsByRef(true), NegatePredicate(false), ExpandForMC(false),
40         IndentLevel(1U), TargetName(Target) {}
isByRef()41   bool isByRef() const { return EmitCallsByRef; }
shouldNegate()42   bool shouldNegate() const { return NegatePredicate; }
shouldExpandForMC()43   bool shouldExpandForMC() const { return ExpandForMC; }
getIndentLevel()44   unsigned getIndentLevel() const { return IndentLevel; }
getTargetName()45   StringRef getTargetName() const { return TargetName; }
46 
setByRef(bool Value)47   void setByRef(bool Value) { EmitCallsByRef = Value; }
flipNegatePredicate()48   void flipNegatePredicate() { NegatePredicate = !NegatePredicate; }
setNegatePredicate(bool Value)49   void setNegatePredicate(bool Value) { NegatePredicate = Value; }
setExpandForMC(bool Value)50   void setExpandForMC(bool Value) { ExpandForMC = Value; }
setIndentLevel(unsigned Level)51   void setIndentLevel(unsigned Level) { IndentLevel = Level; }
increaseIndentLevel()52   void increaseIndentLevel() { ++IndentLevel; }
decreaseIndentLevel()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 expandCheckImmOperandLT(raw_ostream &OS, int OpIndex, int ImmVal,
65                                StringRef FunctionMapper);
66   void expandCheckImmOperandGT(raw_ostream &OS, int OpIndex, int ImmVal,
67                                StringRef FunctionMapper);
68   void expandCheckRegOperand(raw_ostream &OS, int OpIndex, const Record *Reg,
69                              StringRef FunctionMapper);
70   void expandCheckRegOperandSimple(raw_ostream &OS, int OpIndex,
71                                    StringRef FunctionMapper);
72   void expandCheckSameRegOperand(raw_ostream &OS, int First, int Second);
73   void expandCheckNumOperands(raw_ostream &OS, int NumOps);
74   void expandCheckOpcode(raw_ostream &OS, const Record *Inst);
75 
76   void expandCheckPseudo(raw_ostream &OS, const RecVec &Opcodes);
77   void expandCheckOpcode(raw_ostream &OS, const RecVec &Opcodes);
78   void expandPredicateSequence(raw_ostream &OS, const RecVec &Sequence,
79                                bool IsCheckAll);
80   void expandTIIFunctionCall(raw_ostream &OS, StringRef MethodName);
81   void expandCheckIsRegOperand(raw_ostream &OS, int OpIndex);
82   void expandCheckIsVRegOperand(raw_ostream &OS, int OpIndex);
83   void expandCheckIsImmOperand(raw_ostream &OS, int OpIndex);
84   void expandCheckInvalidRegOperand(raw_ostream &OS, int OpIndex);
85   void expandCheckFunctionPredicate(raw_ostream &OS, StringRef MCInstFn,
86                                     StringRef MachineInstrFn);
87   void expandCheckFunctionPredicateWithTII(raw_ostream &OS, StringRef MCInstFn,
88                                            StringRef MachineInstrFn,
89                                            StringRef TIIPtr);
90   void expandCheckNonPortable(raw_ostream &OS, StringRef CodeBlock);
91   void expandPredicate(raw_ostream &OS, const Record *Rec);
92   void expandReturnStatement(raw_ostream &OS, const Record *Rec);
93   void expandOpcodeSwitchCase(raw_ostream &OS, const Record *Rec);
94   void expandOpcodeSwitchStatement(raw_ostream &OS, const RecVec &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:
STIPredicateExpander(StringRef Target)118   STIPredicateExpander(StringRef Target)
119       : PredicateExpander(Target), ExpandDefinition(false) {}
120 
shouldExpandDefinition()121   bool shouldExpandDefinition() const { return ExpandDefinition; }
getClassPrefix()122   StringRef getClassPrefix() const { return ClassPrefix; }
setClassPrefix(StringRef S)123   void setClassPrefix(StringRef S) { ClassPrefix = S; }
setExpandDefinition(bool Value)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
132