TargetInstrPredicate.td 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. //===- TargetInstrPredicate.td - ---------------------------*- tablegen -*-===//
  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. //
  9. // This file defines class MCInstPredicate and its subclasses.
  10. //
  11. // MCInstPredicate definitions are used by target scheduling models to describe
  12. // constraints on instructions.
  13. //
  14. // Here is an example of an MCInstPredicate definition in TableGen:
  15. //
  16. // def MCInstPredicateExample : CheckAll<[
  17. // CheckOpcode<[BLR]>,
  18. // CheckIsRegOperand<0>,
  19. // CheckNot<CheckRegOperand<0, LR>>]>;
  20. //
  21. // The syntax for MCInstPredicate is declarative, and predicate definitions can
  22. // be composed together in order to generate more complex constraints.
  23. //
  24. // The `CheckAll` from the example defines a composition of three different
  25. // predicates. Definition `MCInstPredicateExample` identifies instructions
  26. // whose opcode is BLR, and whose first operand is a register different from
  27. // register `LR`.
  28. //
  29. // Every MCInstPredicate class has a well-known semantic in tablegen. For
  30. // example, `CheckOpcode` is a special type of predicate used to describe a
  31. // constraint on the value of an instruction opcode.
  32. //
  33. // MCInstPredicate definitions are typically used by scheduling models to
  34. // construct MCSchedPredicate definitions (see the definition of class
  35. // MCSchedPredicate in llvm/Target/TargetSchedule.td).
  36. // In particular, an MCSchedPredicate can be used instead of a SchedPredicate
  37. // when defining the set of SchedReadVariant and SchedWriteVariant of a
  38. // processor scheduling model.
  39. //
  40. // The `MCInstPredicateExample` definition above is equivalent (and therefore
  41. // could replace) the following definition from a previous ExynosM3 model (see
  42. // AArch64SchedExynosM3.td):
  43. //
  44. // def M3BranchLinkFastPred : SchedPredicate<[{
  45. // MI->getOpcode() == AArch64::BLR &&
  46. // MI->getOperand(0).isReg() &&
  47. // MI->getOperand(0).getReg() != AArch64::LR}]>;
  48. //
  49. // The main advantage of using MCInstPredicate instead of SchedPredicate is
  50. // portability: users don't need to specify predicates in C++. As a consequence
  51. // of this, MCInstPredicate definitions are not bound to a particular
  52. // representation (i.e. MachineInstr vs MCInst).
  53. //
  54. // Tablegen backends know how to expand MCInstPredicate definitions into actual
  55. // C++ code that works on MachineInstr (and/or MCInst).
  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. // Forward declarations.
  69. class Instruction;
  70. class SchedMachineModel;
  71. // A generic machine instruction predicate.
  72. class MCInstPredicate;
  73. class MCTrue : MCInstPredicate; // A predicate that always evaluates to True.
  74. class MCFalse : MCInstPredicate; // A predicate that always evaluates to False.
  75. def TruePred : MCTrue;
  76. def FalsePred : MCFalse;
  77. // A predicate used to negate the outcome of another predicate.
  78. // It allows to easily express "set difference" operations. For example, it
  79. // makes it easy to describe a check that tests if an opcode is not part of a
  80. // set of opcodes.
  81. class CheckNot<MCInstPredicate P> : MCInstPredicate {
  82. MCInstPredicate Pred = P;
  83. }
  84. // This class is used as a building block to define predicates on instruction
  85. // operands. It is used to reference a specific machine operand.
  86. class MCOperandPredicate<int Index> : MCInstPredicate {
  87. int OpIndex = Index;
  88. }
  89. // Return true if machine operand at position `Index` is a register operand.
  90. class CheckIsRegOperand<int Index> : MCOperandPredicate<Index>;
  91. // Return true if machine operand at position `Index` is an immediate operand.
  92. class CheckIsImmOperand<int Index> : MCOperandPredicate<Index>;
  93. // Check if machine operands at index `First` and index `Second` both reference
  94. // the same register.
  95. class CheckSameRegOperand<int First, int Second> : MCInstPredicate {
  96. int FirstIndex = First;
  97. int SecondIndex = Second;
  98. }
  99. // Base class for checks on register/immediate operands.
  100. // It allows users to define checks like:
  101. // MyFunction(MI->getOperand(Index).getImm()) == Val;
  102. //
  103. // In the example above, `MyFunction` is a function that takes as input an
  104. // immediate operand value, and returns another value. Field `FunctionMapper` is
  105. // the name of the function to call on the operand value.
  106. class CheckOperandBase<int Index, string Fn = ""> : MCOperandPredicate<Index> {
  107. string FunctionMapper = Fn;
  108. }
  109. // Check that the machine register operand at position `Index` references
  110. // register R. This predicate assumes that we already checked that the machine
  111. // operand at position `Index` is a register operand.
  112. class CheckRegOperand<int Index, Register R> : CheckOperandBase<Index> {
  113. Register Reg = R;
  114. }
  115. // Check if register operand at index `Index` is the invalid register.
  116. class CheckInvalidRegOperand<int Index> : CheckOperandBase<Index>;
  117. // Return true if machine operand at position `Index` is a valid
  118. // register operand.
  119. class CheckValidRegOperand<int Index> :
  120. CheckNot<CheckInvalidRegOperand<Index>>;
  121. // Check that the operand at position `Index` is immediate `Imm`.
  122. // If field `FunctionMapper` is a non-empty string, then function
  123. // `FunctionMapper` is applied to the operand value, and the return value is then
  124. // compared against `Imm`.
  125. class CheckImmOperand<int Index, int Imm> : CheckOperandBase<Index> {
  126. int ImmVal = Imm;
  127. }
  128. // Similar to CheckImmOperand, however the immediate is not a literal number.
  129. // This is useful when we want to compare the value of an operand against an
  130. // enum value, and we know the actual integer value of that enum.
  131. class CheckImmOperand_s<int Index, string Value> : CheckOperandBase<Index> {
  132. string ImmVal = Value;
  133. }
  134. // Expands to a call to `FunctionMapper` if field `FunctionMapper` is set.
  135. // Otherwise, it expands to a CheckNot<CheckInvalidRegOperand<Index>>.
  136. class CheckRegOperandSimple<int Index> : CheckOperandBase<Index>;
  137. // Expands to a call to `FunctionMapper` if field `FunctionMapper` is set.
  138. // Otherwise, it simply evaluates to TruePred.
  139. class CheckImmOperandSimple<int Index> : CheckOperandBase<Index>;
  140. // Check that the operand at position `Index` is immediate value zero.
  141. class CheckZeroOperand<int Index> : CheckImmOperand<Index, 0>;
  142. // Check that the instruction has exactly `Num` operands.
  143. class CheckNumOperands<int Num> : MCInstPredicate {
  144. int NumOps = Num;
  145. }
  146. // Check that the instruction opcode is one of the opcodes in set `Opcodes`.
  147. // This is a simple set membership query. The easier way to check if an opcode
  148. // is not a member of the set is by using a `CheckNot<CheckOpcode<[...]>>`
  149. // sequence.
  150. class CheckOpcode<list<Instruction> Opcodes> : MCInstPredicate {
  151. list<Instruction> ValidOpcodes = Opcodes;
  152. }
  153. // Check that the instruction opcode is a pseudo opcode member of the set
  154. // `Opcodes`. This check is always expanded to "false" if we are generating
  155. // code for MCInst.
  156. class CheckPseudo<list<Instruction> Opcodes> : CheckOpcode<Opcodes>;
  157. // A non-portable predicate. Only to use as a last resort when a block of code
  158. // cannot possibly be converted in a declarative way using other MCInstPredicate
  159. // classes. This check is always expanded to "false" when generating code for
  160. // MCInst.
  161. class CheckNonPortable<string Code> : MCInstPredicate {
  162. string CodeBlock = Code;
  163. }
  164. // A sequence of predicates. It is used as the base class for CheckAll, and
  165. // CheckAny. It allows to describe compositions of predicates.
  166. class CheckPredicateSequence<list<MCInstPredicate> Preds> : MCInstPredicate {
  167. list<MCInstPredicate> Predicates = Preds;
  168. }
  169. // Check that all of the predicates in `Preds` evaluate to true.
  170. class CheckAll<list<MCInstPredicate> Sequence>
  171. : CheckPredicateSequence<Sequence>;
  172. // Check that at least one of the predicates in `Preds` evaluates to true.
  173. class CheckAny<list<MCInstPredicate> Sequence>
  174. : CheckPredicateSequence<Sequence>;
  175. // Used to expand the body of a function predicate. See the definition of
  176. // TIIPredicate below.
  177. class MCStatement;
  178. // Expands to a return statement. The return expression is a boolean expression
  179. // described by a MCInstPredicate.
  180. class MCReturnStatement<MCInstPredicate predicate> : MCStatement {
  181. MCInstPredicate Pred = predicate;
  182. }
  183. // Used to automatically construct cases of a switch statement where the switch
  184. // variable is an instruction opcode. There is a 'case' for every opcode in the
  185. // `opcodes` list, and each case is associated with MCStatement `caseStmt`.
  186. class MCOpcodeSwitchCase<list<Instruction> opcodes, MCStatement caseStmt> {
  187. list<Instruction> Opcodes = opcodes;
  188. MCStatement CaseStmt = caseStmt;
  189. }
  190. // Expands to a switch statement. The switch variable is an instruction opcode.
  191. // The auto-generated switch is populated by a number of cases based on the
  192. // `cases` list in input. A default case is automatically generated, and it
  193. // evaluates to `default`.
  194. class MCOpcodeSwitchStatement<list<MCOpcodeSwitchCase> cases,
  195. MCStatement default> : MCStatement {
  196. list<MCOpcodeSwitchCase> Cases = cases;
  197. MCStatement DefaultCase = default;
  198. }
  199. // Base class for function predicates.
  200. class FunctionPredicateBase<string name, MCStatement body> {
  201. string FunctionName = name;
  202. MCStatement Body = body;
  203. }
  204. // Check that a call to method `Name` in class "XXXInstrInfo" (where XXX is
  205. // the name of a target) returns true.
  206. //
  207. // TIIPredicate definitions are used to model calls to the target-specific
  208. // InstrInfo. A TIIPredicate is treated specially by the InstrInfoEmitter
  209. // tablegen backend, which will use it to automatically generate a definition in
  210. // the target specific `InstrInfo` class.
  211. //
  212. // There cannot be multiple TIIPredicate definitions with the same name for the
  213. // same target.
  214. class TIIPredicate<string Name, MCStatement body>
  215. : FunctionPredicateBase<Name, body>, MCInstPredicate;
  216. // A function predicate that takes as input a machine instruction, and returns
  217. // a boolean value.
  218. //
  219. // This predicate is expanded into a function call by the PredicateExpander.
  220. // In particular, the PredicateExpander would either expand this predicate into
  221. // a call to `MCInstFn`, or into a call to`MachineInstrFn` depending on whether
  222. // it is lowering predicates for MCInst or MachineInstr.
  223. //
  224. // In this context, `MCInstFn` and `MachineInstrFn` are both function names.
  225. class CheckFunctionPredicate<string MCInstFn, string MachineInstrFn> : MCInstPredicate {
  226. string MCInstFnName = MCInstFn;
  227. string MachineInstrFnName = MachineInstrFn;
  228. }
  229. // Similar to CheckFunctionPredicate. However it assumes that MachineInstrFn is
  230. // a method in TargetInstrInfo, and MCInstrFn takes an extra pointer to
  231. // MCInstrInfo.
  232. //
  233. // It Expands to:
  234. // - TIIPointer->MachineInstrFn(MI)
  235. // - MCInstrFn(MI, MCII);
  236. class CheckFunctionPredicateWithTII<string MCInstFn, string MachineInstrFn, string
  237. TIIPointer = "TII"> : MCInstPredicate {
  238. string MCInstFnName = MCInstFn;
  239. string TIIPtrName = TIIPointer;
  240. string MachineInstrFnName = MachineInstrFn;
  241. }
  242. // Used to classify machine instructions based on a machine instruction
  243. // predicate.
  244. //
  245. // Let IC be an InstructionEquivalenceClass definition, and MI a machine
  246. // instruction. We say that MI belongs to the equivalence class described by IC
  247. // if and only if the following two conditions are met:
  248. // a) MI's opcode is in the `opcodes` set, and
  249. // b) `Predicate` evaluates to true when applied to MI.
  250. //
  251. // Instances of this class can be used by processor scheduling models to
  252. // describe instructions that have a property in common. For example,
  253. // InstructionEquivalenceClass definitions can be used to identify the set of
  254. // dependency breaking instructions for a processor model.
  255. //
  256. // An (optional) list of operand indices can be used to further describe
  257. // properties that apply to instruction operands. For example, it can be used to
  258. // identify register uses of a dependency breaking instructions that are not in
  259. // a RAW dependency.
  260. class InstructionEquivalenceClass<list<Instruction> opcodes,
  261. MCInstPredicate pred,
  262. list<int> operands = []> {
  263. list<Instruction> Opcodes = opcodes;
  264. MCInstPredicate Predicate = pred;
  265. list<int> OperandIndices = operands;
  266. }
  267. // Used by processor models to describe dependency breaking instructions.
  268. //
  269. // This is mainly an alias for InstructionEquivalenceClass. Input operand
  270. // `BrokenDeps` identifies the set of "broken dependencies". There is one bit
  271. // per each implicit and explicit input operand. An empty set of broken
  272. // dependencies means: "explicit input register operands are independent."
  273. class DepBreakingClass<list<Instruction> opcodes, MCInstPredicate pred,
  274. list<int> BrokenDeps = []>
  275. : InstructionEquivalenceClass<opcodes, pred, BrokenDeps>;
  276. // A function descriptor used to describe the signature of a predicate methods
  277. // which will be expanded by the STIPredicateExpander into a tablegen'd
  278. // XXXGenSubtargetInfo class member definition (here, XXX is a target name).
  279. //
  280. // It describes the signature of a TargetSubtarget hook, as well as a few extra
  281. // properties. Examples of extra properties are:
  282. // - The default return value for the auto-generate function hook.
  283. // - A list of subtarget hooks (Delegates) that are called from this function.
  284. //
  285. class STIPredicateDecl<string name, MCInstPredicate default = FalsePred,
  286. bit overrides = true, bit expandForMC = true,
  287. bit updatesOpcodeMask = false,
  288. list<STIPredicateDecl> delegates = []> {
  289. string Name = name;
  290. MCInstPredicate DefaultReturnValue = default;
  291. // True if this method is declared as virtual in class TargetSubtargetInfo.
  292. bit OverridesBaseClassMember = overrides;
  293. // True if we need an equivalent predicate function in the MC layer.
  294. bit ExpandForMC = expandForMC;
  295. // True if the autogenerated method has a extra in/out APInt param used as a
  296. // mask of operands.
  297. bit UpdatesOpcodeMask = updatesOpcodeMask;
  298. // A list of STIPredicates used by this definition to delegate part of the
  299. // computation. For example, STIPredicateFunction `isDependencyBreaking()`
  300. // delegates to `isZeroIdiom()` part of its computation.
  301. list<STIPredicateDecl> Delegates = delegates;
  302. }
  303. // A predicate function definition member of class `XXXGenSubtargetInfo`.
  304. //
  305. // If `Declaration.ExpandForMC` is true, then SubtargetEmitter
  306. // will also expand another definition of this method that accepts a MCInst.
  307. class STIPredicate<STIPredicateDecl declaration,
  308. list<InstructionEquivalenceClass> classes> {
  309. STIPredicateDecl Declaration = declaration;
  310. list<InstructionEquivalenceClass> Classes = classes;
  311. SchedMachineModel SchedModel = ?;
  312. }
  313. // Convenience classes and definitions used by processor scheduling models to
  314. // describe dependency breaking instructions and move elimination candidates.
  315. let UpdatesOpcodeMask = true in {
  316. def IsZeroIdiomDecl : STIPredicateDecl<"isZeroIdiom">;
  317. let Delegates = [IsZeroIdiomDecl] in
  318. def IsDepBreakingDecl : STIPredicateDecl<"isDependencyBreaking">;
  319. } // UpdatesOpcodeMask
  320. def IsOptimizableRegisterMoveDecl
  321. : STIPredicateDecl<"isOptimizableRegisterMove">;
  322. class IsZeroIdiomFunction<list<DepBreakingClass> classes>
  323. : STIPredicate<IsZeroIdiomDecl, classes>;
  324. class IsDepBreakingFunction<list<DepBreakingClass> classes>
  325. : STIPredicate<IsDepBreakingDecl, classes>;
  326. class IsOptimizableRegisterMove<list<InstructionEquivalenceClass> classes>
  327. : STIPredicate<IsOptimizableRegisterMoveDecl, classes>;