TargetInstrPredicate.td revision 337149
1//===- TargetInstrPredicate.td - ---------------------------*- tablegen -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines MCInstPredicate classes and its subclasses.
11//
12// MCInstPredicate is used to describe constraints on the opcode/operand(s) of
13// an instruction. Each MCInstPredicate class has a well-known semantic, and it
14// is used by a PredicateExpander to generate code for MachineInstr and/or
15// MCInst.
16//
17// MCInstPredicate definitions can be used to construct MCSchedPredicate
18// definitions. An MCSchedPredicate can be used in place of a SchedPredicate
19// when defining SchedReadVariant and SchedWriteVariant used by a processor
20// scheduling model.
21//
22// Here is an example of MCInstPredicate definition:
23//
24// def MCInstPredicateExample : CheckAll<[
25//    CheckOpcode<[BLR]>,
26//    CheckIsRegOperand<0>,
27//    CheckNot<CheckRegOperand<0, LR>>]>;
28//
29// Predicate `MCInstPredicateExample` checks that the machine instruction in
30// input is a BLR, and that operand at index 0 is register `LR`.
31//
32// That predicate could be used to rewrite the following definition (from
33// AArch64SchedExynosM3.td):
34//
35// def M3BranchLinkFastPred  : SchedPredicate<[{
36//    MI->getOpcode() == AArch64::BLR &&
37//    MI->getOperand(0).isReg() &&
38//    MI->getOperand(0).getReg() != AArch64::LR}]>;
39//
40// MCInstPredicate definitions are used to construct MCSchedPredicate (see the
41// definition of class MCSchedPredicate in llvm/Target/TargetSchedule.td).  An
42// MCSchedPredicate can be used by a `SchedVar` to associate a predicate with a
43// list of SchedReadWrites. Note that `SchedVar` are used to create SchedVariant
44// definitions.
45//
46// Each MCInstPredicate class has a well known semantic. For example,
47// `CheckOpcode` is only used to check the instruction opcode value.
48//
49// MCInstPredicate classes allow the definition of predicates in a declarative
50// way.  These predicates don't require a custom block of C++, and can be used
51// to define conditions on instructions without being bound to a particular
52// representation (i.e. MachineInstr vs MCInst).
53//
54// It also means that tablegen backends must know how to parse and expand them
55// into code that works on MCInst (or MachineInst).
56//
57// Instances of class PredicateExpander (see utils/Tablegen/PredicateExpander.h)
58// know how to expand a predicate. For each MCInstPredicate class, there must be
59// an "expand" method available in the PredicateExpander interface.
60//
61// For example, a `CheckOpcode` predicate is expanded using method
62// `PredicateExpander::expandCheckOpcode()`.
63//
64// New MCInstPredicate classes must be added to this file. For each new class
65// XYZ, an "expandXYZ" method must be added to the PredicateExpander.
66//
67//===----------------------------------------------------------------------===//
68
69// Forward declarations.
70class Instruction;
71
72// A generic machine instruction predicate.
73class MCInstPredicate;
74
75class MCTrue  : MCInstPredicate;   // A predicate that always evaluates to True.
76class MCFalse : MCInstPredicate;   // A predicate that always evaluates to False.
77def TruePred  : MCTrue;
78def FalsePred : MCFalse;
79
80// A predicate used to negate the outcome of another predicate.
81// It allows to easily express "set difference" operations. For example, it
82// makes it easy to describe a check that tests if an opcode is not part of a
83// set of opcodes.
84class CheckNot<MCInstPredicate P> : MCInstPredicate {
85  MCInstPredicate Pred = P;
86}
87
88// This class is used as a building block to define predicates on instruction
89// operands. It is used to reference a specific machine operand.
90class MCOperandPredicate<int Index> : MCInstPredicate {
91  int OpIndex = Index;
92}
93
94// Return true if machine operand at position `Index` is a register operand.
95class CheckIsRegOperand<int Index> : MCOperandPredicate<Index>;
96
97// Return true if machine operand at position `Index` is an immediate operand.
98class CheckIsImmOperand<int Index> : MCOperandPredicate<Index>;
99
100// Check if machine operands at index `First` and index `Second` both reference
101// the same register.
102class CheckSameRegOperand<int First, int Second> : MCInstPredicate {
103  int FirstIndex = First;
104  int SecondIndex = Second;
105}
106
107// Check that the machine register operand at position `Index` references
108// register R. This predicate assumes that we already checked that the machine
109// operand at position `Index` is a register operand.
110class CheckRegOperand<int Index, Register R> : MCOperandPredicate<Index> {
111  Register Reg = R;
112}
113
114// Check if register operand at index `Index` is the invalid register.
115class CheckInvalidRegOperand<int Index> : MCOperandPredicate<Index>;
116
117// Check that the operand at position `Index` is immediate `Imm`.
118class CheckImmOperand<int Index, int Imm> : MCOperandPredicate<Index> {
119  int ImmVal = Imm;
120}
121
122// Similar to CheckImmOperand, however the immediate is not a literal number.
123// This is useful when we want to compare the value of an operand against an
124// enum value, and we know the actual integer value of that enum.
125class CheckImmOperand_s<int Index, string Value> : MCOperandPredicate<Index> {
126  string ImmVal = Value;
127}
128
129// Check that the operand at position `Index` is immediate value zero.
130class CheckZeroOperand<int Index> : CheckImmOperand<Index, 0>;
131
132// Check that the instruction has exactly `Num` operands.
133class CheckNumOperands<int Num> : MCInstPredicate {
134  int NumOps = Num;
135}
136
137// Check that the instruction opcode is one of the opcodes in set `Opcodes`.
138// This is a simple set membership query. The easier way to check if an opcode
139// is not a member of the set is by using a `CheckNot<CheckOpcode<[...]>>`
140// sequence.
141class CheckOpcode<list<Instruction> Opcodes> : MCInstPredicate {
142  list<Instruction> ValidOpcodes = Opcodes;
143}
144
145// Check that the instruction opcode is a pseudo opcode member of the set
146// `Opcodes`.  This check is always expanded to "false" if we are generating
147// code for MCInst.
148class CheckPseudo<list<Instruction> Opcodes> : CheckOpcode<Opcodes>;
149
150// A non-portable predicate. Only to use as a last resort when a block of code
151// cannot possibly be converted in a declarative way using other MCInstPredicate
152// classes. This check is always expanded to "false" when generating code for
153// MCInst.
154class CheckNonPortable<string Code> : MCInstPredicate {
155  string CodeBlock = Code;
156}
157
158// A sequence of predicates. It is used as the base class for CheckAll, and
159// CheckAny. It allows to describe compositions of predicates.
160class CheckPredicateSequence<list<MCInstPredicate> Preds> : MCInstPredicate {
161  list<MCInstPredicate> Predicates = Preds;
162}
163
164// Check that all of the predicates in `Preds` evaluate to true.
165class CheckAll<list<MCInstPredicate> Sequence>
166    : CheckPredicateSequence<Sequence>;
167
168// Check that at least one of the predicates in `Preds` evaluates to true.
169class CheckAny<list<MCInstPredicate> Sequence>
170    : CheckPredicateSequence<Sequence>;
171
172// Check that a call to method `Name` in class "XXXGenInstrInfo" (where XXX is
173// the `Target` name) returns true.
174//
175// TIIPredicate definitions are used to model calls to the target-specific
176// InstrInfo. A TIIPredicate is treated specially by the InstrInfoEmitter
177// tablegen backend, which will use it to automatically generate a definition in
178// the target specific `GenInstrInfo` class.
179class TIIPredicate<string Target, string Name, MCInstPredicate P> : MCInstPredicate {
180  string TargetName = Target;
181  string FunctionName = Name;
182  MCInstPredicate Pred = P;
183}
184
185// A function predicate that takes as input a machine instruction, and returns
186// a boolean value.
187//
188// This predicate is expanded into a function call by the PredicateExpander.
189// In particular, the PredicateExpander would either expand this predicate into
190// a call to `MCInstFn`, or into a call to`MachineInstrFn` depending on whether
191// it is lowering predicates for MCInst or MachineInstr.
192//
193// In this context, `MCInstFn` and `MachineInstrFn` are both function names.
194class CheckFunctionPredicate<string MCInstFn, string MachineInstrFn> : MCInstPredicate {
195  string MCInstFnName = MCInstFn;
196  string MachineInstrFnName = MachineInstrFn;
197}
198