GIMatchDagOperands.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //===- GIMatchDagOperands.h - Represent a shared operand list for nodes ---===//
  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. //
  10. //===----------------------------------------------------------------------===//
  11. #ifndef LLVM_UTILS_TABLEGEN_GIMATCHDAGOPERANDS_H
  12. #define LLVM_UTILS_TABLEGEN_GIMATCHDAGOPERANDS_H
  13. #include "llvm/ADT/FoldingSet.h"
  14. #include "llvm/ADT/StringMap.h"
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. #include <vector>
  18. namespace llvm {
  19. class CodeGenInstruction;
  20. /// Describes an operand of a MachineInstr w.r.t the DAG Matching. This
  21. /// information is derived from CodeGenInstruction::Operands but is more
  22. /// readily available for context-less access as we don't need to know which
  23. /// instruction it's used with or know how many defs that instruction had.
  24. ///
  25. /// There may be multiple GIMatchDagOperand's with the same contents. However,
  26. /// they are uniqued within the set of instructions that have the same overall
  27. /// operand list. For example, given:
  28. /// Inst1 operands ($dst:<def>, $src1, $src2)
  29. /// Inst2 operands ($dst:<def>, $src1, $src2)
  30. /// Inst3 operands ($dst:<def>, $src)
  31. /// $src1 will have a single instance of GIMatchDagOperand shared by Inst1 and
  32. /// Inst2, as will $src2. $dst however, will have two instances one shared
  33. /// between Inst1 and Inst2 and one unique to Inst3. We could potentially
  34. /// fully de-dupe the GIMatchDagOperand instances but the saving is not expected
  35. /// to be worth the overhead.
  36. ///
  37. /// The result of this is that the address of the object can be relied upon to
  38. /// trivially identify commonality between two instructions which will be useful
  39. /// when generating the matcher. When the pointers differ, the contents can be
  40. /// inspected instead.
  41. class GIMatchDagOperand {
  42. unsigned Idx;
  43. StringRef Name;
  44. bool IsDef;
  45. public:
  46. GIMatchDagOperand(unsigned Idx, StringRef Name, bool IsDef)
  47. : Idx(Idx), Name(Name), IsDef(IsDef) {}
  48. unsigned getIdx() const { return Idx; }
  49. StringRef getName() const { return Name; }
  50. bool isDef() const { return IsDef; }
  51. /// This object isn't a FoldingSetNode but it's part of one. See FoldingSet
  52. /// for details on the Profile function.
  53. void Profile(FoldingSetNodeID &ID) const;
  54. /// A helper that behaves like Profile() but is also usable without the object.
  55. /// We use size_t here to match enumerate<...>::index(). If we don't match
  56. /// that the hashes won't be equal.
  57. static void Profile(FoldingSetNodeID &ID, size_t Idx, StringRef Name,
  58. bool IsDef);
  59. };
  60. /// A list of GIMatchDagOperands for an instruction without any association with
  61. /// a particular instruction.
  62. ///
  63. /// An important detail to be aware of with this class is that they are shared
  64. /// with other instructions of a similar 'shape'. For example, all the binary
  65. /// instructions are likely to share a single GIMatchDagOperandList. This is
  66. /// primarily a memory optimization as it's fairly common to have a large number
  67. /// of instructions but only a few 'shapes'.
  68. ///
  69. /// See GIMatchDagOperandList::Profile() for the details on how they are folded.
  70. class GIMatchDagOperandList : public FoldingSetNode {
  71. public:
  72. using value_type = GIMatchDagOperand;
  73. protected:
  74. using vector_type = SmallVector<GIMatchDagOperand, 3>;
  75. public:
  76. using iterator = vector_type::iterator;
  77. using const_iterator = vector_type::const_iterator;
  78. protected:
  79. vector_type Operands;
  80. StringMap<unsigned> OperandsByName;
  81. public:
  82. void add(StringRef Name, unsigned Idx, bool IsDef);
  83. /// See FoldingSet for details.
  84. void Profile(FoldingSetNodeID &ID) const;
  85. iterator begin() { return Operands.begin(); }
  86. const_iterator begin() const { return Operands.begin(); }
  87. iterator end() { return Operands.end(); }
  88. const_iterator end() const { return Operands.end(); }
  89. const value_type &operator[](unsigned I) const { return Operands[I]; }
  90. const value_type &operator[](StringRef K) const;
  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. /// This is the portion of GIMatchDagContext that directly relates to
  97. /// GIMatchDagOperandList and GIMatchDagOperandList.
  98. class GIMatchDagOperandListContext {
  99. FoldingSet<GIMatchDagOperandList> OperandLists;
  100. std::vector<std::unique_ptr<GIMatchDagOperandList>> OperandListsOwner;
  101. public:
  102. const GIMatchDagOperandList &makeEmptyOperandList();
  103. const GIMatchDagOperandList &makeOperandList(const CodeGenInstruction &I);
  104. const GIMatchDagOperandList &makeMIPredicateOperandList();
  105. const GIMatchDagOperandList &makeTwoMOPredicateOperandList();
  106. void print(raw_ostream &OS) const;
  107. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  108. LLVM_DUMP_METHOD void dump() const { print(errs()); }
  109. #endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  110. };
  111. } // end namespace llvm
  112. #endif // ifndef LLVM_UTILS_TABLEGEN_GIMATCHDAGOPERANDS_H