GIMatchDagPredicate.h 5.0 KB

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