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
23namespace llvm {
24
25class Record;
26
27class PredicateExpander {
28 bool EmitCallsByRef = true;
29 bool NegatePredicate = false;
30 bool ExpandForMC = false;
31 StringRef TargetName;
32
33 PredicateExpander(const PredicateExpander &) = delete;
34 PredicateExpander &operator=(const PredicateExpander &) = delete;
35
36protected:
37 indent Indent;
38
39public:
40 explicit PredicateExpander(StringRef Target, unsigned Indent = 1)
41 : TargetName(Target), Indent(Indent, 2) {}
42 bool isByRef() const { return EmitCallsByRef; }
43 bool shouldNegate() const { return NegatePredicate; }
44 bool shouldExpandForMC() const { return ExpandForMC; }
45 indent &getIndent() { return Indent; }
46 StringRef getTargetName() const { return TargetName; }
47
48 void setByRef(bool Value) { EmitCallsByRef = Value; }
49 void flipNegatePredicate() { NegatePredicate = !NegatePredicate; }
50 void setNegatePredicate(bool Value) { NegatePredicate = Value; }
51 void setExpandForMC(bool Value) { ExpandForMC = Value; }
52
53 void expandTrue(raw_ostream &OS);
54 void expandFalse(raw_ostream &OS);
55 void expandCheckImmOperandCommon(raw_ostream &OS, int OpIndex, int ImmVal,
56 StringRef FunctionMapper,
57 StringRef CmpOperator);
58 void expandCheckImmOperand(raw_ostream &OS, int OpIndex, StringRef ImmVal,
59 StringRef FunctionMapper);
60 void expandCheckImmOperandSimple(raw_ostream &OS, int OpIndex,
61 StringRef FunctionMapper);
62 void expandCheckImmOperandRange(raw_ostream &OS, int OpIndex, int StartVal,
63 int EndVal, 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, ArrayRef<const Record *> Opcodes);
73 void expandCheckOpcode(raw_ostream &OS, ArrayRef<const Record *> Opcodes);
74 void expandPredicateSequence(raw_ostream &OS,
75 ArrayRef<const Record *> Sequence,
76 bool IsCheckAll);
77 void expandTIIFunctionCall(raw_ostream &OS, StringRef MethodName);
78 void expandCheckIsRegOperand(raw_ostream &OS, int OpIndex);
79 void expandCheckIsVRegOperand(raw_ostream &OS, int OpIndex);
80 void expandCheckIsImmOperand(raw_ostream &OS, int OpIndex);
81 void expandCheckInvalidRegOperand(raw_ostream &OS, int OpIndex);
82 void expandCheckFunctionPredicate(raw_ostream &OS, StringRef MCInstFn,
83 StringRef MachineInstrFn);
84 void expandCheckFunctionPredicateWithTII(raw_ostream &OS, StringRef MCInstFn,
85 StringRef MachineInstrFn,
86 StringRef TIIPtr);
87 void expandCheckNonPortable(raw_ostream &OS, StringRef CodeBlock);
88 void expandPredicate(raw_ostream &OS, const Record *Rec);
89 void expandReturnStatement(raw_ostream &OS, const Record *Rec);
90 void expandOpcodeSwitchCase(raw_ostream &OS, const Record *Rec);
91 void expandOpcodeSwitchStatement(raw_ostream &OS,
92 ArrayRef<const Record *> Cases,
93 const Record *Default);
94 void expandStatement(raw_ostream &OS, const Record *Rec);
95};
96
97// Forward declarations.
98class STIPredicateFunction;
99class OpcodeGroup;
100
101class STIPredicateExpander : public PredicateExpander {
102 StringRef ClassPrefix;
103 bool ExpandDefinition;
104
105 STIPredicateExpander(const PredicateExpander &) = delete;
106 STIPredicateExpander &operator=(const PredicateExpander &) = delete;
107
108 void expandHeader(raw_ostream &OS, const STIPredicateFunction &Fn);
109 void expandPrologue(raw_ostream &OS, const STIPredicateFunction &Fn);
110 void expandOpcodeGroup(raw_ostream &OS, const OpcodeGroup &Group,
111 bool ShouldUpdateOpcodeMask);
112 void expandBody(raw_ostream &OS, const STIPredicateFunction &Fn);
113 void expandEpilogue(raw_ostream &OS, const STIPredicateFunction &Fn);
114
115public:
116 explicit STIPredicateExpander(StringRef Target, unsigned Indent = 1)
117 : PredicateExpander(Target, Indent), ExpandDefinition(false) {}
118
119 bool shouldExpandDefinition() const { return ExpandDefinition; }
120 StringRef getClassPrefix() const { return ClassPrefix; }
121 void setClassPrefix(StringRef S) { ClassPrefix = S; }
122 void setExpandDefinition(bool Value) { ExpandDefinition = Value; }
123
124 void expandSTIPredicate(raw_ostream &OS, const STIPredicateFunction &Fn);
125};
126
127} // namespace llvm
128
129#endif // LLVM_UTILS_TABLEGEN_COMMON_PREDICATEEXPANDER_H
130