GIMatchDagOperands.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //===- GIMatchDagOperands.cpp - 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. #include "GIMatchDagOperands.h"
  9. #include "../CodeGenInstruction.h"
  10. using namespace llvm;
  11. void GIMatchDagOperand::Profile(FoldingSetNodeID &ID) const {
  12. Profile(ID, Idx, Name, IsDef);
  13. }
  14. void GIMatchDagOperand::Profile(FoldingSetNodeID &ID, size_t Idx,
  15. StringRef Name, bool IsDef) {
  16. ID.AddInteger(Idx);
  17. ID.AddString(Name);
  18. ID.AddBoolean(IsDef);
  19. }
  20. void GIMatchDagOperandList::add(StringRef Name, unsigned Idx, bool IsDef) {
  21. assert(Idx == Operands.size() && "Operands added in wrong order");
  22. Operands.emplace_back(Operands.size(), Name, IsDef);
  23. OperandsByName.try_emplace(Operands.back().getName(), Operands.size() - 1);
  24. }
  25. void GIMatchDagOperandList::Profile(FoldingSetNodeID &ID) const {
  26. for (const auto &I : enumerate(Operands))
  27. GIMatchDagOperand::Profile(ID, I.index(), I.value().getName(),
  28. I.value().isDef());
  29. }
  30. void GIMatchDagOperandList::print(raw_ostream &OS) const {
  31. if (Operands.empty()) {
  32. OS << "<empty>";
  33. return;
  34. }
  35. StringRef Separator = "";
  36. for (const auto &I : Operands) {
  37. OS << Separator << I.getIdx() << ":" << I.getName();
  38. if (I.isDef())
  39. OS << "<def>";
  40. Separator = ", ";
  41. }
  42. }
  43. const GIMatchDagOperandList::value_type &GIMatchDagOperandList::
  44. operator[](StringRef K) const {
  45. const auto &I = OperandsByName.find(K);
  46. assert(I != OperandsByName.end() && "Operand not found by name");
  47. return Operands[I->second];
  48. }
  49. const GIMatchDagOperandList &
  50. GIMatchDagOperandListContext::makeEmptyOperandList() {
  51. FoldingSetNodeID ID;
  52. void *InsertPoint;
  53. GIMatchDagOperandList *Value =
  54. OperandLists.FindNodeOrInsertPos(ID, InsertPoint);
  55. if (Value)
  56. return *Value;
  57. std::unique_ptr<GIMatchDagOperandList> NewValue =
  58. std::make_unique<GIMatchDagOperandList>();
  59. OperandLists.InsertNode(NewValue.get(), InsertPoint);
  60. OperandListsOwner.push_back(std::move(NewValue));
  61. return *OperandListsOwner.back().get();
  62. }
  63. const GIMatchDagOperandList &
  64. GIMatchDagOperandListContext::makeOperandList(const CodeGenInstruction &I) {
  65. FoldingSetNodeID ID;
  66. for (unsigned i = 0; i < I.Operands.size(); ++i)
  67. GIMatchDagOperand::Profile(ID, i, I.Operands[i].Name,
  68. i < I.Operands.NumDefs);
  69. void *InsertPoint;
  70. GIMatchDagOperandList *Value =
  71. OperandLists.FindNodeOrInsertPos(ID, InsertPoint);
  72. if (Value)
  73. return *Value;
  74. std::unique_ptr<GIMatchDagOperandList> NewValue =
  75. std::make_unique<GIMatchDagOperandList>();
  76. for (unsigned i = 0; i < I.Operands.size(); ++i)
  77. NewValue->add(I.Operands[i].Name, i, i < I.Operands.NumDefs);
  78. OperandLists.InsertNode(NewValue.get(), InsertPoint);
  79. OperandListsOwner.push_back(std::move(NewValue));
  80. return *OperandListsOwner.back().get();
  81. }
  82. const GIMatchDagOperandList &
  83. GIMatchDagOperandListContext::makeMIPredicateOperandList() {
  84. FoldingSetNodeID ID;
  85. GIMatchDagOperand::Profile(ID, 0, "$", true);
  86. GIMatchDagOperand::Profile(ID, 1, "mi", false);
  87. void *InsertPoint;
  88. GIMatchDagOperandList *Value =
  89. OperandLists.FindNodeOrInsertPos(ID, InsertPoint);
  90. if (Value)
  91. return *Value;
  92. std::unique_ptr<GIMatchDagOperandList> NewValue =
  93. std::make_unique<GIMatchDagOperandList>();
  94. NewValue->add("$", 0, true);
  95. NewValue->add("mi", 1, false);
  96. OperandLists.InsertNode(NewValue.get(), InsertPoint);
  97. OperandListsOwner.push_back(std::move(NewValue));
  98. return *OperandListsOwner.back().get();
  99. }
  100. const GIMatchDagOperandList &
  101. GIMatchDagOperandListContext::makeTwoMOPredicateOperandList() {
  102. FoldingSetNodeID ID;
  103. GIMatchDagOperand::Profile(ID, 0, "$", true);
  104. GIMatchDagOperand::Profile(ID, 1, "mi0", false);
  105. GIMatchDagOperand::Profile(ID, 2, "mi1", false);
  106. void *InsertPoint;
  107. GIMatchDagOperandList *Value =
  108. OperandLists.FindNodeOrInsertPos(ID, InsertPoint);
  109. if (Value)
  110. return *Value;
  111. std::unique_ptr<GIMatchDagOperandList> NewValue =
  112. std::make_unique<GIMatchDagOperandList>();
  113. NewValue->add("$", 0, true);
  114. NewValue->add("mi0", 1, false);
  115. NewValue->add("mi1", 2, false);
  116. OperandLists.InsertNode(NewValue.get(), InsertPoint);
  117. OperandListsOwner.push_back(std::move(NewValue));
  118. return *OperandListsOwner.back().get();
  119. }
  120. void GIMatchDagOperandListContext::print(raw_ostream &OS) const {
  121. OS << "GIMatchDagOperandListContext {\n"
  122. << " OperandLists {\n";
  123. for (const auto &I : OperandListsOwner) {
  124. OS << " ";
  125. I->print(OS);
  126. OS << "\n";
  127. }
  128. OS << " }\n"
  129. << "}\n";
  130. }