DAGISelEmitter.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //===- DAGISelEmitter.cpp - Generate an instruction selector --------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This tablegen backend emits a DAG instruction selector.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "CodeGenDAGPatterns.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. CodeGenDAGPatterns CGP;
  25. public:
  26. explicit DAGISelEmitter(RecordKeeper &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 *LHSSrc = LHS->getSrcPattern();
  74. const TreePatternNode *RHSSrc = RHS->getSrcPattern();
  75. MVT LHSVT = (LHSSrc->getNumTypes() != 0 ? LHSSrc->getType(0) : MVT::Other);
  76. MVT RHSVT = (RHSSrc->getNumTypes() != 0 ? RHSSrc->getType(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, giving us a deterministic ordering
  98. // if all other sorting conditions fail.
  99. assert(LHS == RHS || LHS->ID != RHS->ID);
  100. return LHS->ID < RHS->ID;
  101. }
  102. };
  103. } // End anonymous namespace
  104. void DAGISelEmitter::run(raw_ostream &OS) {
  105. emitSourceFileHeader("DAG Instruction Selector for the " +
  106. CGP.getTargetInfo().getName() + " target", OS);
  107. OS << "// *** NOTE: This file is #included into the middle of the target\n"
  108. << "// *** instruction selector class. These functions are really "
  109. << "methods.\n\n";
  110. DEBUG(errs() << "\n\nALL PATTERNS TO MATCH:\n\n";
  111. for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
  112. E = CGP.ptm_end(); I != E; ++I) {
  113. errs() << "PATTERN: "; I->getSrcPattern()->dump();
  114. errs() << "\nRESULT: "; I->getDstPattern()->dump();
  115. errs() << "\n";
  116. });
  117. // Add all the patterns to a temporary list so we can sort them.
  118. std::vector<const PatternToMatch*> Patterns;
  119. for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end();
  120. I != E; ++I)
  121. Patterns.push_back(&*I);
  122. // We want to process the matches in order of minimal cost. Sort the patterns
  123. // so the least cost one is at the start.
  124. std::sort(Patterns.begin(), Patterns.end(), PatternSortingPredicate(CGP));
  125. // Convert each variant of each pattern into a Matcher.
  126. std::vector<Matcher*> PatternMatchers;
  127. for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
  128. for (unsigned Variant = 0; ; ++Variant) {
  129. if (Matcher *M = ConvertPatternToMatcher(*Patterns[i], Variant, CGP))
  130. PatternMatchers.push_back(M);
  131. else
  132. break;
  133. }
  134. }
  135. std::unique_ptr<Matcher> TheMatcher =
  136. llvm::make_unique<ScopeMatcher>(PatternMatchers);
  137. OptimizeMatcher(TheMatcher, CGP);
  138. //Matcher->dump();
  139. EmitMatcherTable(TheMatcher.get(), CGP, OS);
  140. }
  141. namespace llvm {
  142. void EmitDAGISel(RecordKeeper &RK, raw_ostream &OS) {
  143. DAGISelEmitter(RK).run(OS);
  144. }
  145. } // End llvm namespace