Warning, /include/llvm/Target/TargetInstrPredicate.td is written in an unsupported language. File is not indexed.
0001 //===- TargetInstrPredicate.td - ---------------------------*- tablegen -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines class MCInstPredicate and its subclasses.
0010 //
0011 // MCInstPredicate definitions are used by target scheduling models to describe
0012 // constraints on instructions.
0013 //
0014 // Here is an example of an MCInstPredicate definition in TableGen:
0015 //
0016 // def MCInstPredicateExample : CheckAll<[
0017 // CheckOpcode<[BLR]>,
0018 // CheckIsRegOperand<0>,
0019 // CheckNot<CheckRegOperand<0, LR>>]>;
0020 //
0021 // The syntax for MCInstPredicate is declarative, and predicate definitions can
0022 // be composed together in order to generate more complex constraints.
0023 //
0024 // The `CheckAll` from the example defines a composition of three different
0025 // predicates. Definition `MCInstPredicateExample` identifies instructions
0026 // whose opcode is BLR, and whose first operand is a register different from
0027 // register `LR`.
0028 //
0029 // Every MCInstPredicate class has a well-known semantic in tablegen. For
0030 // example, `CheckOpcode` is a special type of predicate used to describe a
0031 // constraint on the value of an instruction opcode.
0032 //
0033 // MCInstPredicate definitions are typically used by scheduling models to
0034 // construct MCSchedPredicate definitions (see the definition of class
0035 // MCSchedPredicate in llvm/Target/TargetSchedule.td).
0036 // In particular, an MCSchedPredicate can be used instead of a SchedPredicate
0037 // when defining the set of SchedReadVariant and SchedWriteVariant of a
0038 // processor scheduling model.
0039 //
0040 // The `MCInstPredicateExample` definition above is equivalent (and therefore
0041 // could replace) the following definition from a previous ExynosM3 model (see
0042 // AArch64SchedExynosM3.td):
0043 //
0044 // def M3BranchLinkFastPred : SchedPredicate<[{
0045 // MI->getOpcode() == AArch64::BLR &&
0046 // MI->getOperand(0).isReg() &&
0047 // MI->getOperand(0).getReg() != AArch64::LR}]>;
0048 //
0049 // The main advantage of using MCInstPredicate instead of SchedPredicate is
0050 // portability: users don't need to specify predicates in C++. As a consequence
0051 // of this, MCInstPredicate definitions are not bound to a particular
0052 // representation (i.e. MachineInstr vs MCInst).
0053 //
0054 // Tablegen backends know how to expand MCInstPredicate definitions into actual
0055 // C++ code that works on MachineInstr (and/or MCInst).
0056 //
0057 // Instances of class PredicateExpander (see utils/Tablegen/PredicateExpander.h)
0058 // know how to expand a predicate. For each MCInstPredicate class, there must be
0059 // an "expand" method available in the PredicateExpander interface.
0060 //
0061 // For example, a `CheckOpcode` predicate is expanded using method
0062 // `PredicateExpander::expandCheckOpcode()`.
0063 //
0064 // New MCInstPredicate classes must be added to this file. For each new class
0065 // XYZ, an "expandXYZ" method must be added to the PredicateExpander.
0066 //
0067 //===----------------------------------------------------------------------===//
0068
0069 // Forward declarations.
0070 class Instruction;
0071 class SchedMachineModel;
0072
0073 // A generic machine instruction predicate.
0074 class MCInstPredicate;
0075
0076 class MCTrue : MCInstPredicate; // A predicate that always evaluates to True.
0077 class MCFalse : MCInstPredicate; // A predicate that always evaluates to False.
0078 def TruePred : MCTrue;
0079 def FalsePred : MCFalse;
0080
0081 // A predicate used to negate the outcome of another predicate.
0082 // It allows to easily express "set difference" operations. For example, it
0083 // makes it easy to describe a check that tests if an opcode is not part of a
0084 // set of opcodes.
0085 class CheckNot<MCInstPredicate P> : MCInstPredicate {
0086 MCInstPredicate Pred = P;
0087 }
0088
0089 // This class is used as a building block to define predicates on instruction
0090 // operands. It is used to reference a specific machine operand.
0091 class MCOperandPredicate<int Index> : MCInstPredicate {
0092 int OpIndex = Index;
0093 }
0094
0095 // Return true if machine operand at position `Index` is a register operand.
0096 class CheckIsRegOperand<int Index> : MCOperandPredicate<Index>;
0097
0098 // Return true if machine operand at position `Index` is a virtual register operand.
0099 class CheckIsVRegOperand<int Index> : MCOperandPredicate<Index>;
0100
0101 // Return true if machine operand at position `Index` is not a virtual register operand.
0102 class CheckIsNotVRegOperand<int Index> : CheckNot<CheckIsVRegOperand<Index>>;
0103
0104 // Return true if machine operand at position `Index` is an immediate operand.
0105 class CheckIsImmOperand<int Index> : MCOperandPredicate<Index>;
0106
0107 // Check if machine operands at index `First` and index `Second` both reference
0108 // the same register.
0109 class CheckSameRegOperand<int First, int Second> : MCInstPredicate {
0110 int FirstIndex = First;
0111 int SecondIndex = Second;
0112 }
0113
0114 // Base class for checks on register/immediate operands.
0115 // It allows users to define checks like:
0116 // MyFunction(MI->getOperand(Index).getImm()) == Val;
0117 //
0118 // In the example above, `MyFunction` is a function that takes as input an
0119 // immediate operand value, and returns another value. Field `FunctionMapper` is
0120 // the name of the function to call on the operand value.
0121 class CheckOperandBase<int Index, string Fn = ""> : MCOperandPredicate<Index> {
0122 string FunctionMapper = Fn;
0123 }
0124
0125 // Check that the machine register operand at position `Index` references
0126 // register R. This predicate assumes that we already checked that the machine
0127 // operand at position `Index` is a register operand.
0128 class CheckRegOperand<int Index, Register R> : CheckOperandBase<Index> {
0129 Register Reg = R;
0130 }
0131
0132 // Check if register operand at index `Index` is the invalid register.
0133 class CheckInvalidRegOperand<int Index> : CheckOperandBase<Index>;
0134
0135 // Return true if machine operand at position `Index` is a valid
0136 // register operand.
0137 class CheckValidRegOperand<int Index> :
0138 CheckNot<CheckInvalidRegOperand<Index>>;
0139
0140 // Check that the operand at position `Index` is immediate `Imm`.
0141 // If field `FunctionMapper` is a non-empty string, then function
0142 // `FunctionMapper` is applied to the operand value, and the return value is then
0143 // compared against `Imm`.
0144 class CheckImmOperand<int Index, int Imm> : CheckOperandBase<Index> {
0145 int ImmVal = Imm;
0146 }
0147
0148 // Similar to CheckImmOperand, however the immediate is not a literal number.
0149 // This is useful when we want to compare the value of an operand against an
0150 // enum value, and we know the actual integer value of that enum.
0151 class CheckImmOperand_s<int Index, string Value> : CheckOperandBase<Index> {
0152 string ImmVal = Value;
0153 }
0154
0155 // Check that the operand at position `Index` is less than `Imm`.
0156 // If field `FunctionMapper` is a non-empty string, then function
0157 // `FunctionMapper` is applied to the operand value, and the return value is then
0158 // compared against `Imm`.
0159 class CheckImmOperandLT<int Index, int Imm> : CheckOperandBase<Index> {
0160 int ImmVal = Imm;
0161 }
0162
0163 // Check that the operand at position `Index` is greater than `Imm`.
0164 // If field `FunctionMapper` is a non-empty string, then function
0165 // `FunctionMapper` is applied to the operand value, and the return value is then
0166 // compared against `Imm`.
0167 class CheckImmOperandGT<int Index, int Imm> : CheckOperandBase<Index> {
0168 int ImmVal = Imm;
0169 }
0170
0171 // Check that the operand at position `Index` is less than or equal to `Imm`.
0172 // If field `FunctionMapper` is a non-empty string, then function
0173 // `FunctionMapper` is applied to the operand value, and the return value is then
0174 // compared against `Imm`.
0175 class CheckImmOperandLE<int Index, int Imm> : CheckNot<CheckImmOperandGT<Index, Imm>>;
0176
0177 // Check that the operand at position `Index` is greater than or equal to `Imm`.
0178 // If field `FunctionMapper` is a non-empty string, then function
0179 // `FunctionMapper` is applied to the operand value, and the return value is then
0180 // compared against `Imm`.
0181 class CheckImmOperandGE<int Index, int Imm> : CheckNot<CheckImmOperandLT<Index, Imm>>;
0182
0183 // Expands to a call to `FunctionMapper` if field `FunctionMapper` is set.
0184 // Otherwise, it expands to a CheckNot<CheckInvalidRegOperand<Index>>.
0185 class CheckRegOperandSimple<int Index> : CheckOperandBase<Index>;
0186
0187 // Expands to a call to `FunctionMapper` if field `FunctionMapper` is set.
0188 // Otherwise, it simply evaluates to TruePred.
0189 class CheckImmOperandSimple<int Index> : CheckOperandBase<Index>;
0190
0191 // Check that the operand at position `Index` is immediate value zero.
0192 class CheckZeroOperand<int Index> : CheckImmOperand<Index, 0>;
0193
0194 // Check that the instruction has exactly `Num` operands.
0195 class CheckNumOperands<int Num> : MCInstPredicate {
0196 int NumOps = Num;
0197 }
0198
0199 // Check that the instruction opcode is one of the opcodes in set `Opcodes`.
0200 // This is a simple set membership query. The easier way to check if an opcode
0201 // is not a member of the set is by using a `CheckNot<CheckOpcode<[...]>>`
0202 // sequence.
0203 class CheckOpcode<list<Instruction> Opcodes> : MCInstPredicate {
0204 list<Instruction> ValidOpcodes = Opcodes;
0205 }
0206
0207 // Check that the instruction opcode is a pseudo opcode member of the set
0208 // `Opcodes`. This check is always expanded to "false" if we are generating
0209 // code for MCInst.
0210 class CheckPseudo<list<Instruction> Opcodes> : CheckOpcode<Opcodes>;
0211
0212 // A non-portable predicate. Only to use as a last resort when a block of code
0213 // cannot possibly be converted in a declarative way using other MCInstPredicate
0214 // classes. This check is always expanded to "false" when generating code for
0215 // MCInst.
0216 class CheckNonPortable<string Code> : MCInstPredicate {
0217 string CodeBlock = Code;
0218 }
0219
0220 // A sequence of predicates. It is used as the base class for CheckAll, and
0221 // CheckAny. It allows to describe compositions of predicates.
0222 class CheckPredicateSequence<list<MCInstPredicate> Preds> : MCInstPredicate {
0223 list<MCInstPredicate> Predicates = Preds;
0224 }
0225
0226 // Check that all of the predicates in `Preds` evaluate to true.
0227 class CheckAll<list<MCInstPredicate> Sequence>
0228 : CheckPredicateSequence<Sequence>;
0229
0230 // Check that at least one of the predicates in `Preds` evaluates to true.
0231 class CheckAny<list<MCInstPredicate> Sequence>
0232 : CheckPredicateSequence<Sequence>;
0233
0234 // Check that the operand at position `Index` is in range [Start, End].
0235 // If field `FunctionMapper` is a non-empty string, then function
0236 // `FunctionMapper` is applied to the operand value, and the return value is then
0237 // compared against range [Start, End].
0238 class CheckImmOperandRange<int Index, int Start, int End>
0239 : CheckAll<[CheckImmOperandGE<Index, Start>, CheckImmOperandLE<Index, End>]>;
0240
0241 // Used to expand the body of a function predicate. See the definition of
0242 // TIIPredicate below.
0243 class MCStatement;
0244
0245 // Expands to a return statement. The return expression is a boolean expression
0246 // described by a MCInstPredicate.
0247 class MCReturnStatement<MCInstPredicate predicate> : MCStatement {
0248 MCInstPredicate Pred = predicate;
0249 }
0250
0251 // Used to automatically construct cases of a switch statement where the switch
0252 // variable is an instruction opcode. There is a 'case' for every opcode in the
0253 // `opcodes` list, and each case is associated with MCStatement `caseStmt`.
0254 class MCOpcodeSwitchCase<list<Instruction> opcodes, MCStatement caseStmt> {
0255 list<Instruction> Opcodes = opcodes;
0256 MCStatement CaseStmt = caseStmt;
0257 }
0258
0259 // Expands to a switch statement. The switch variable is an instruction opcode.
0260 // The auto-generated switch is populated by a number of cases based on the
0261 // `cases` list in input. A default case is automatically generated, and it
0262 // evaluates to `default`.
0263 class MCOpcodeSwitchStatement<list<MCOpcodeSwitchCase> cases,
0264 MCStatement default> : MCStatement {
0265 list<MCOpcodeSwitchCase> Cases = cases;
0266 MCStatement DefaultCase = default;
0267 }
0268
0269 // Base class for function predicates.
0270 class FunctionPredicateBase<string name, MCStatement body> {
0271 string FunctionName = name;
0272 MCStatement Body = body;
0273 }
0274
0275 // Check that a call to method `Name` in class "XXXInstrInfo" (where XXX is
0276 // the name of a target) returns true.
0277 //
0278 // TIIPredicate definitions are used to model calls to the target-specific
0279 // InstrInfo. A TIIPredicate is treated specially by the InstrInfoEmitter
0280 // tablegen backend, which will use it to automatically generate a definition in
0281 // the target specific `InstrInfo` class.
0282 //
0283 // There cannot be multiple TIIPredicate definitions with the same name for the
0284 // same target.
0285 class TIIPredicate<string Name, MCStatement body>
0286 : FunctionPredicateBase<Name, body>, MCInstPredicate;
0287
0288 // A function predicate that takes as input a machine instruction, and returns
0289 // a boolean value.
0290 //
0291 // This predicate is expanded into a function call by the PredicateExpander.
0292 // In particular, the PredicateExpander would either expand this predicate into
0293 // a call to `MCInstFn`, or into a call to`MachineInstrFn` depending on whether
0294 // it is lowering predicates for MCInst or MachineInstr.
0295 //
0296 // In this context, `MCInstFn` and `MachineInstrFn` are both function names.
0297 class CheckFunctionPredicate<string MCInstFn, string MachineInstrFn> : MCInstPredicate {
0298 string MCInstFnName = MCInstFn;
0299 string MachineInstrFnName = MachineInstrFn;
0300 }
0301
0302 // Similar to CheckFunctionPredicate. However it assumes that MachineInstrFn is
0303 // a method in TargetInstrInfo, and MCInstrFn takes an extra pointer to
0304 // MCInstrInfo.
0305 //
0306 // It Expands to:
0307 // - TIIPointer->MachineInstrFn(MI)
0308 // - MCInstrFn(MI, MCII);
0309 class CheckFunctionPredicateWithTII<string MCInstFn, string MachineInstrFn, string
0310 TIIPointer = "TII"> : MCInstPredicate {
0311 string MCInstFnName = MCInstFn;
0312 string TIIPtrName = TIIPointer;
0313 string MachineInstrFnName = MachineInstrFn;
0314 }
0315
0316 // Used to classify machine instructions based on a machine instruction
0317 // predicate.
0318 //
0319 // Let IC be an InstructionEquivalenceClass definition, and MI a machine
0320 // instruction. We say that MI belongs to the equivalence class described by IC
0321 // if and only if the following two conditions are met:
0322 // a) MI's opcode is in the `opcodes` set, and
0323 // b) `Predicate` evaluates to true when applied to MI.
0324 //
0325 // Instances of this class can be used by processor scheduling models to
0326 // describe instructions that have a property in common. For example,
0327 // InstructionEquivalenceClass definitions can be used to identify the set of
0328 // dependency breaking instructions for a processor model.
0329 //
0330 // An (optional) list of operand indices can be used to further describe
0331 // properties that apply to instruction operands. For example, it can be used to
0332 // identify register uses of a dependency breaking instructions that are not in
0333 // a RAW dependency.
0334 class InstructionEquivalenceClass<list<Instruction> opcodes,
0335 MCInstPredicate pred,
0336 list<int> operands = []> {
0337 list<Instruction> Opcodes = opcodes;
0338 MCInstPredicate Predicate = pred;
0339 list<int> OperandIndices = operands;
0340 }
0341
0342 // Used by processor models to describe dependency breaking instructions.
0343 //
0344 // This is mainly an alias for InstructionEquivalenceClass. Input operand
0345 // `BrokenDeps` identifies the set of "broken dependencies". There is one bit
0346 // per each implicit and explicit input operand. An empty set of broken
0347 // dependencies means: "explicit input register operands are independent."
0348 class DepBreakingClass<list<Instruction> opcodes, MCInstPredicate pred,
0349 list<int> BrokenDeps = []>
0350 : InstructionEquivalenceClass<opcodes, pred, BrokenDeps>;
0351
0352 // A function descriptor used to describe the signature of a predicate methods
0353 // which will be expanded by the STIPredicateExpander into a tablegen'd
0354 // XXXGenSubtargetInfo class member definition (here, XXX is a target name).
0355 //
0356 // It describes the signature of a TargetSubtarget hook, as well as a few extra
0357 // properties. Examples of extra properties are:
0358 // - The default return value for the auto-generate function hook.
0359 // - A list of subtarget hooks (Delegates) that are called from this function.
0360 //
0361 class STIPredicateDecl<string name, MCInstPredicate default = FalsePred,
0362 bit overrides = true, bit expandForMC = true,
0363 bit updatesOpcodeMask = false,
0364 list<STIPredicateDecl> delegates = []> {
0365 string Name = name;
0366
0367 MCInstPredicate DefaultReturnValue = default;
0368
0369 // True if this method is declared as virtual in class TargetSubtargetInfo.
0370 bit OverridesBaseClassMember = overrides;
0371
0372 // True if we need an equivalent predicate function in the MC layer.
0373 bit ExpandForMC = expandForMC;
0374
0375 // True if the autogenerated method has a extra in/out APInt param used as a
0376 // mask of operands.
0377 bit UpdatesOpcodeMask = updatesOpcodeMask;
0378
0379 // A list of STIPredicates used by this definition to delegate part of the
0380 // computation. For example, STIPredicateFunction `isDependencyBreaking()`
0381 // delegates to `isZeroIdiom()` part of its computation.
0382 list<STIPredicateDecl> Delegates = delegates;
0383 }
0384
0385 // A predicate function definition member of class `XXXGenSubtargetInfo`.
0386 //
0387 // If `Declaration.ExpandForMC` is true, then SubtargetEmitter
0388 // will also expand another definition of this method that accepts a MCInst.
0389 class STIPredicate<STIPredicateDecl declaration,
0390 list<InstructionEquivalenceClass> classes> {
0391 STIPredicateDecl Declaration = declaration;
0392 list<InstructionEquivalenceClass> Classes = classes;
0393 SchedMachineModel SchedModel = ?;
0394 }
0395
0396 // Convenience classes and definitions used by processor scheduling models to
0397 // describe dependency breaking instructions and move elimination candidates.
0398 let UpdatesOpcodeMask = true in {
0399
0400 def IsZeroIdiomDecl : STIPredicateDecl<"isZeroIdiom">;
0401
0402 let Delegates = [IsZeroIdiomDecl] in
0403 def IsDepBreakingDecl : STIPredicateDecl<"isDependencyBreaking">;
0404
0405 } // UpdatesOpcodeMask
0406
0407 def IsOptimizableRegisterMoveDecl
0408 : STIPredicateDecl<"isOptimizableRegisterMove">;
0409
0410 class IsZeroIdiomFunction<list<DepBreakingClass> classes>
0411 : STIPredicate<IsZeroIdiomDecl, classes>;
0412
0413 class IsDepBreakingFunction<list<DepBreakingClass> classes>
0414 : STIPredicate<IsDepBreakingDecl, classes>;
0415
0416 class IsOptimizableRegisterMove<list<InstructionEquivalenceClass> classes>
0417 : STIPredicate<IsOptimizableRegisterMoveDecl, classes>;