DAGISelEmitter.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //===- DAGISelEmitter.cpp - Generate an instruction selector --------------===//
  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. // This tablegen backend emits a DAG instruction selector.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "CodeGenDAGPatterns.h"
  13. #include "DAGISelMatcher.h"
  14. #include "llvm/Support/Debug.h"
  15. #include "llvm/TableGen/Record.h"
  16. #include "llvm/TableGen/TableGenBackend.h"
  17. using namespace llvm;
  18. #define DEBUG_TYPE "dag-isel-emitter"
  19. namespace {
  20. /// DAGISelEmitter - The top-level class which coordinates construction
  21. /// and emission of the instruction selector.
  22. class DAGISelEmitter {
  23. RecordKeeper &Records; // Just so we can get at the timing functions.
  24. CodeGenDAGPatterns CGP;
  25. public:
  26. explicit DAGISelEmitter(RecordKeeper &R) : Records(R), CGP(R) {}
  27. void run(raw_ostream &OS);
  28. };
  29. } // End anonymous namespace
  30. //===----------------------------------------------------------------------===//
  31. // DAGISelEmitter Helper methods
  32. //
  33. /// getResultPatternCost - Compute the number of instructions for this pattern.
  34. /// This is a temporary hack. We should really include the instruction
  35. /// latencies in this calculation.
  36. static unsigned getResultPatternCost(TreePatternNode *P,
  37. CodeGenDAGPatterns &CGP) {
  38. if (P->isLeaf()) return 0;
  39. unsigned Cost = 0;
  40. Record *Op = P->getOperator();
  41. if (Op->isSubClassOf("Instruction")) {
  42. Cost++;
  43. CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
  44. if (II.usesCustomInserter)
  45. Cost += 10;
  46. }
  47. for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
  48. Cost += getResultPatternCost(P->getChild(i), CGP);
  49. return Cost;
  50. }
  51. /// getResultPatternCodeSize - Compute the code size of instructions for this
  52. /// pattern.
  53. static unsigned getResultPatternSize(TreePatternNode *P,
  54. CodeGenDAGPatterns &CGP) {
  55. if (P->isLeaf()) return 0;
  56. unsigned Cost = 0;
  57. Record *Op = P->getOperator();
  58. if (Op->isSubClassOf("Instruction")) {
  59. Cost += Op->getValueAsInt("CodeSize");
  60. }
  61. for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
  62. Cost += getResultPatternSize(P->getChild(i), CGP);
  63. return Cost;
  64. }
  65. namespace {
  66. // PatternSortingPredicate - return true if we prefer to match LHS before RHS.
  67. // In particular, we want to match maximal patterns first and lowest cost within
  68. // a particular complexity first.
  69. struct PatternSortingPredicate {
  70. PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {}
  71. CodeGenDAGPatterns &CGP;
  72. bool operator()(const PatternToMatch *LHS, const PatternToMatch *RHS) {
  73. const TreePatternNode *LT = LHS->getSrcPattern();
  74. const TreePatternNode *RT = RHS->getSrcPattern();
  75. MVT LHSVT = LT->getNumTypes() != 0 ? LT->getSimpleType(0) : MVT::Other;
  76. MVT RHSVT = RT->getNumTypes() != 0 ? RT->getSimpleType(0) : MVT::Other;
  77. if (LHSVT.isVector() != RHSVT.isVector())
  78. return RHSVT.isVector();
  79. if (LHSVT.isFloatingPoint() != RHSVT.isFloatingPoint())
  80. return RHSVT.isFloatingPoint();
  81. // Otherwise, if the patterns might both match, sort based on complexity,
  82. // which means that we prefer to match patterns that cover more nodes in the
  83. // input over nodes that cover fewer.
  84. int LHSSize = LHS->getPatternComplexity(CGP);
  85. int RHSSize = RHS->getPatternComplexity(CGP);
  86. if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost
  87. if (LHSSize < RHSSize) return false;
  88. // If the patterns have equal complexity, compare generated instruction cost
  89. unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP);
  90. unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP);
  91. if (LHSCost < RHSCost) return true;
  92. if (LHSCost > RHSCost) return false;
  93. unsigned LHSPatSize = getResultPatternSize(LHS->getDstPattern(), CGP);
  94. unsigned RHSPatSize = getResultPatternSize(RHS->getDstPattern(), CGP);
  95. if (LHSPatSize < RHSPatSize) return true;
  96. if (LHSPatSize > RHSPatSize) return false;
  97. // Sort based on the UID of the pattern, to reflect source order.
  98. // Note that this is not guaranteed to be unique, since a single source
  99. // pattern may have been resolved into multiple match patterns due to
  100. // alternative fragments. To ensure deterministic output, always use
  101. // std::stable_sort with this predicate.
  102. return LHS->getID() < RHS->getID();
  103. }
  104. };
  105. } // End anonymous namespace
  106. void DAGISelEmitter::run(raw_ostream &OS) {
  107. emitSourceFileHeader("DAG Instruction Selector for the " +
  108. CGP.getTargetInfo().getName().str() + " target", OS);
  109. OS << "// *** NOTE: This file is #included into the middle of the target\n"
  110. << "// *** instruction selector class. These functions are really "
  111. << "methods.\n\n";
  112. OS << "// If GET_DAGISEL_DECL is #defined with any value, only function\n"
  113. "// declarations will be included when this file is included.\n"
  114. "// If GET_DAGISEL_BODY is #defined, its value should be the name of\n"
  115. "// the instruction selector class. Function bodies will be emitted\n"
  116. "// and each function's name will be qualified with the name of the\n"
  117. "// class.\n"
  118. "//\n"
  119. "// When neither of the GET_DAGISEL* macros is defined, the functions\n"
  120. "// are emitted inline.\n\n";
  121. LLVM_DEBUG(errs() << "\n\nALL PATTERNS TO MATCH:\n\n";
  122. for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
  123. E = CGP.ptm_end();
  124. I != E; ++I) {
  125. errs() << "PATTERN: ";
  126. I->getSrcPattern()->dump();
  127. errs() << "\nRESULT: ";
  128. I->getDstPattern()->dump();
  129. errs() << "\n";
  130. });
  131. // Add all the patterns to a temporary list so we can sort them.
  132. Records.startTimer("Sort patterns");
  133. std::vector<const PatternToMatch*> Patterns;
  134. for (const PatternToMatch &PTM : CGP.ptms())
  135. Patterns.push_back(&PTM);
  136. // We want to process the matches in order of minimal cost. Sort the patterns
  137. // so the least cost one is at the start.
  138. llvm::stable_sort(Patterns, PatternSortingPredicate(CGP));
  139. // Convert each variant of each pattern into a Matcher.
  140. Records.startTimer("Convert to matchers");
  141. std::vector<Matcher*> PatternMatchers;
  142. for (const PatternToMatch *PTM : Patterns) {
  143. for (unsigned Variant = 0; ; ++Variant) {
  144. if (Matcher *M = ConvertPatternToMatcher(*PTM, Variant, CGP))
  145. PatternMatchers.push_back(M);
  146. else
  147. break;
  148. }
  149. }
  150. std::unique_ptr<Matcher> TheMatcher =
  151. std::make_unique<ScopeMatcher>(PatternMatchers);
  152. Records.startTimer("Optimize matchers");
  153. OptimizeMatcher(TheMatcher, CGP);
  154. //Matcher->dump();
  155. Records.startTimer("Emit matcher table");
  156. EmitMatcherTable(TheMatcher.get(), CGP, OS);
  157. }
  158. namespace llvm {
  159. void EmitDAGISel(RecordKeeper &RK, raw_ostream &OS) {
  160. RK.startTimer("Parse patterns");
  161. DAGISelEmitter(RK).run(OS);
  162. }
  163. } // End llvm namespace