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