DAGISelEmitter.cpp 7.1 KB

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