GIMatchDagPredicate.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //===- GIMatchDagPredicate - Represent a predicate to check ---------------===//
  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. #ifndef LLVM_UTILS_TABLEGEN_GIMATCHDAGPREDICATE_H
  9. #define LLVM_UTILS_TABLEGEN_GIMATCHDAGPREDICATE_H
  10. #include "llvm/ADT/StringRef.h"
  11. #include "GIMatchDag.h"
  12. namespace llvm {
  13. class CodeExpansions;
  14. class CodeGenInstruction;
  15. class GIMatchDagOperandList;
  16. class GIMatchDagContext;
  17. class raw_ostream;
  18. /// Represents a predicate on the match DAG. This records the details of the
  19. /// predicate. The dependencies are stored in the GIMatchDag as edges.
  20. ///
  21. /// Instances of this class objects are owned by the GIMatchDag and are not
  22. /// shareable between instances of GIMatchDag.
  23. class GIMatchDagPredicate {
  24. public:
  25. enum GIMatchDagPredicateKind {
  26. GIMatchDagPredicateKind_Opcode,
  27. GIMatchDagPredicateKind_OneOfOpcodes,
  28. GIMatchDagPredicateKind_SameMO,
  29. };
  30. protected:
  31. const GIMatchDagPredicateKind Kind;
  32. /// The name of the predicate. For example:
  33. /// (FOO $a:s32, $b, $c)
  34. /// will cause 's32' to be assigned to this member for the $a predicate.
  35. /// Similarly, the opcode predicate will cause 'FOO' to be assigned to this
  36. /// member. Anonymous instructions will have a name assigned for debugging
  37. /// purposes.
  38. StringRef Name;
  39. /// The operand list for this predicate. This object may be shared with
  40. /// other predicates of a similar 'shape'.
  41. const GIMatchDagOperandList &OperandInfo;
  42. public:
  43. GIMatchDagPredicate(GIMatchDagPredicateKind Kind, StringRef Name,
  44. const GIMatchDagOperandList &OperandInfo)
  45. : Kind(Kind), Name(Name), OperandInfo(OperandInfo) {}
  46. virtual ~GIMatchDagPredicate() {}
  47. GIMatchDagPredicateKind getKind() const { return Kind; }
  48. StringRef getName() const { return Name; }
  49. const GIMatchDagOperandList &getOperandInfo() const { return OperandInfo; }
  50. // Generate C++ code to check this predicate. If a partitioner has already
  51. // tested this predicate then this function won't be called. If this function
  52. // is called, it must emit code and return true to indicate that it did so. If
  53. // it ever returns false, then the caller will abort due to an untested
  54. // predicate.
  55. virtual bool generateCheckCode(raw_ostream &OS, StringRef Indent,
  56. const CodeExpansions &Expansions) const {
  57. return false;
  58. }
  59. virtual void print(raw_ostream &OS) const;
  60. virtual void printDescription(raw_ostream &OS) const;
  61. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  62. virtual LLVM_DUMP_METHOD void dump() const { print(errs()); }
  63. #endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  64. };
  65. class GIMatchDagOpcodePredicate : public GIMatchDagPredicate {
  66. const CodeGenInstruction &Instr;
  67. public:
  68. GIMatchDagOpcodePredicate(GIMatchDagContext &Ctx, StringRef Name,
  69. const CodeGenInstruction &Instr);
  70. static bool classof(const GIMatchDagPredicate *P) {
  71. return P->getKind() == GIMatchDagPredicateKind_Opcode;
  72. }
  73. const CodeGenInstruction *getInstr() const { return &Instr; }
  74. void printDescription(raw_ostream &OS) const override;
  75. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  76. virtual LLVM_DUMP_METHOD void dump() const override { print(errs()); }
  77. #endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  78. };
  79. class GIMatchDagOneOfOpcodesPredicate : public GIMatchDagPredicate {
  80. SmallVector<const CodeGenInstruction *, 4> Instrs;
  81. public:
  82. GIMatchDagOneOfOpcodesPredicate(GIMatchDagContext &Ctx, StringRef Name);
  83. void addOpcode(const CodeGenInstruction *Instr) { Instrs.push_back(Instr); }
  84. static bool classof(const GIMatchDagPredicate *P) {
  85. return P->getKind() == GIMatchDagPredicateKind_OneOfOpcodes;
  86. }
  87. const SmallVectorImpl<const CodeGenInstruction *> &getInstrs() const {
  88. return Instrs;
  89. }
  90. void printDescription(raw_ostream &OS) const override;
  91. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  92. virtual LLVM_DUMP_METHOD void dump() const override { print(errs()); }
  93. #endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  94. };
  95. class GIMatchDagSameMOPredicate : public GIMatchDagPredicate {
  96. public:
  97. GIMatchDagSameMOPredicate(GIMatchDagContext &Ctx, StringRef Name);
  98. static bool classof(const GIMatchDagPredicate *P) {
  99. return P->getKind() == GIMatchDagPredicateKind_SameMO;
  100. }
  101. void printDescription(raw_ostream &OS) const override;
  102. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  103. virtual LLVM_DUMP_METHOD void dump() const override { print(errs()); }
  104. #endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  105. };
  106. raw_ostream &operator<<(raw_ostream &OS, const GIMatchDagPredicate &N);
  107. raw_ostream &operator<<(raw_ostream &OS, const GIMatchDagOpcodePredicate &N);
  108. } // end namespace llvm
  109. #endif // ifndef LLVM_UTILS_TABLEGEN_GIMATCHDAGPREDICATE_H