GIMatchDagInstr.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //===- GIMatchDagInstr.h - Represent a instruction to be matched ----------===//
  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_GIMATCHDAGINSTR_H
  9. #define LLVM_UTILS_TABLEGEN_GIMATCHDAGINSTR_H
  10. #include "llvm/ADT/DenseMap.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/Support/raw_ostream.h"
  13. namespace llvm {
  14. class CodeGenInstruction;
  15. class GIMatchDag;
  16. class GIMatchDagOperandList;
  17. /// Represents an instruction in the match DAG. This object knows very little
  18. /// about the actual instruction to be matched as the bulk of that is in
  19. /// predicates that are associated with the match DAG. It merely knows the names
  20. /// and indices of any operands that need to be matched in order to allow edges
  21. /// to link to them.
  22. ///
  23. /// Instances of this class objects are owned by the GIMatchDag and are not
  24. /// shareable between instances of GIMatchDag. This is because the Name,
  25. /// IsMatchRoot, and OpcodeAnnotation are likely to differ between GIMatchDag
  26. /// instances.
  27. class GIMatchDagInstr {
  28. public:
  29. using const_user_assigned_operand_names_iterator =
  30. DenseMap<unsigned, StringRef>::const_iterator;
  31. protected:
  32. /// The match DAG this instruction belongs to.
  33. GIMatchDag &Dag;
  34. /// The name of the instruction in the pattern. For example:
  35. /// (FOO $a, $b, $c):$name
  36. /// will cause name to be assigned to this member. Anonymous instructions will
  37. /// have a name assigned for debugging purposes.
  38. StringRef Name;
  39. /// The name of the instruction in the pattern as assigned by the user. For
  40. /// example:
  41. /// (FOO $a, $b, $c):$name
  42. /// will cause name to be assigned to this member. If a name is not provided,
  43. /// this will be empty. This name is used to bind variables from rules to the
  44. /// matched instruction.
  45. StringRef UserAssignedName;
  46. /// The name of each operand (if any) that was assigned by the user. For
  47. /// example:
  48. /// (FOO $a, $b, $c):$name
  49. /// will cause {0, "a"}, {1, "b"}, {2, "c} to be inserted into this map.
  50. DenseMap<unsigned, StringRef> UserAssignedNamesForOperands;
  51. /// The operand list for this instruction. This object may be shared with
  52. /// other instructions of a similar 'shape'.
  53. const GIMatchDagOperandList &OperandInfo;
  54. /// For debugging purposes, it's helpful to have access to a description of
  55. /// the Opcode. However, this object shouldn't use it for more than debugging
  56. /// output since predicates are expected to be handled outside the DAG.
  57. CodeGenInstruction *OpcodeAnnotation = nullptr;
  58. /// When true, this instruction will be a starting point for a match attempt.
  59. bool IsMatchRoot = false;
  60. public:
  61. GIMatchDagInstr(GIMatchDag &Dag, StringRef Name, StringRef UserAssignedName,
  62. const GIMatchDagOperandList &OperandInfo)
  63. : Dag(Dag), Name(Name), UserAssignedName(UserAssignedName),
  64. OperandInfo(OperandInfo) {}
  65. const GIMatchDagOperandList &getOperandInfo() const { return OperandInfo; }
  66. StringRef getName() const { return Name; }
  67. StringRef getUserAssignedName() const { return UserAssignedName; }
  68. void assignNameToOperand(unsigned Idx, StringRef Name) {
  69. assert(UserAssignedNamesForOperands[Idx].empty() && "Cannot assign twice");
  70. UserAssignedNamesForOperands[Idx] = Name;
  71. }
  72. const_user_assigned_operand_names_iterator
  73. user_assigned_operand_names_begin() const {
  74. return UserAssignedNamesForOperands.begin();
  75. }
  76. const_user_assigned_operand_names_iterator
  77. user_assigned_operand_names_end() const {
  78. return UserAssignedNamesForOperands.end();
  79. }
  80. iterator_range<const_user_assigned_operand_names_iterator>
  81. user_assigned_operand_names() const {
  82. return make_range(user_assigned_operand_names_begin(),
  83. user_assigned_operand_names_end());
  84. }
  85. /// Mark this instruction as being a root of the match. This means that the
  86. /// matcher will start from this node when attempting to match MIR.
  87. void setMatchRoot();
  88. bool isMatchRoot() const { return IsMatchRoot; }
  89. void setOpcodeAnnotation(CodeGenInstruction *I) { OpcodeAnnotation = I; }
  90. CodeGenInstruction *getOpcodeAnnotation() const { return OpcodeAnnotation; }
  91. void print(raw_ostream &OS) const;
  92. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  93. LLVM_DUMP_METHOD void dump() const { print(errs()); }
  94. #endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  95. };
  96. raw_ostream &operator<<(raw_ostream &OS, const GIMatchDagInstr &N);
  97. } // end namespace llvm
  98. #endif // ifndef LLVM_UTILS_TABLEGEN_GIMATCHDAGINSTR_H