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 14 #include "CodeGenDAGPatterns.h" 15 #include "DAGISelMatcher.h" 16 #include "llvm/Support/Debug.h" 17 #include "llvm/TableGen/Record.h" 18 #include "llvm/TableGen/TableGenBackend.h" 19 using namespace llvm; 20 21 namespace { 22 /// DAGISelEmitter - The top-level class which coordinates construction 23 /// and emission of the instruction selector. 24 class DAGISelEmitter { 25 CodeGenDAGPatterns CGP; 26 public: 27 explicit DAGISelEmitter(RecordKeeper &R) : CGP(R) {} 28 void run(raw_ostream &OS); 29 }; 30 } // End anonymous namespace 31 32 //===----------------------------------------------------------------------===// 33 // DAGISelEmitter Helper methods 34 // 35 36 /// getResultPatternCost - Compute the number of instructions for this pattern. 37 /// This is a temporary hack. We should really include the instruction 38 /// latencies in this calculation. 39 static unsigned getResultPatternCost(TreePatternNode *P, 40 CodeGenDAGPatterns &CGP) { 41 if (P->isLeaf()) return 0; 42 43 unsigned Cost = 0; 44 Record *Op = P->getOperator(); 45 if (Op->isSubClassOf("Instruction")) { 46 Cost++; 47 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op); 48 if (II.usesCustomInserter) 49 Cost += 10; 50 } 51 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) 52 Cost += getResultPatternCost(P->getChild(i), CGP); 53 return Cost; 54 } 55 56 /// getResultPatternCodeSize - Compute the code size of instructions for this 57 /// pattern. 58 static unsigned getResultPatternSize(TreePatternNode *P, 59 CodeGenDAGPatterns &CGP) { 60 if (P->isLeaf()) return 0; 61 62 unsigned Cost = 0; 63 Record *Op = P->getOperator(); 64 if (Op->isSubClassOf("Instruction")) { 65 Cost += Op->getValueAsInt("CodeSize"); 66 } 67 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) 68 Cost += getResultPatternSize(P->getChild(i), CGP); 69 return Cost; 70 } 71 72 namespace { 73 // PatternSortingPredicate - return true if we prefer to match LHS before RHS. 74 // In particular, we want to match maximal patterns first and lowest cost within 75 // a particular complexity first. 76 struct PatternSortingPredicate { 77 PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {} 78 CodeGenDAGPatterns &CGP; 79 80 bool operator()(const PatternToMatch *LHS, const PatternToMatch *RHS) { 81 const TreePatternNode *LHSSrc = LHS->getSrcPattern(); 82 const TreePatternNode *RHSSrc = RHS->getSrcPattern(); 83 84 MVT LHSVT = (LHSSrc->getNumTypes() != 0 ? LHSSrc->getType(0) : MVT::Other); 85 MVT RHSVT = (RHSSrc->getNumTypes() != 0 ? RHSSrc->getType(0) : MVT::Other); 86 if (LHSVT.isVector() != RHSVT.isVector()) 87 return RHSVT.isVector(); 88 89 if (LHSVT.isFloatingPoint() != RHSVT.isFloatingPoint()) 90 return RHSVT.isFloatingPoint(); 91 92 // Otherwise, if the patterns might both match, sort based on complexity, 93 // which means that we prefer to match patterns that cover more nodes in the 94 // input over nodes that cover fewer. 95 unsigned LHSSize = LHS->getPatternComplexity(CGP); 96 unsigned RHSSize = RHS->getPatternComplexity(CGP); 97 if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost 98 if (LHSSize < RHSSize) return false; 99 100 // If the patterns have equal complexity, compare generated instruction cost 101 unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP); 102 unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP); 103 if (LHSCost < RHSCost) return true; 104 if (LHSCost > RHSCost) return false; 105 106 unsigned LHSPatSize = getResultPatternSize(LHS->getDstPattern(), CGP); 107 unsigned RHSPatSize = getResultPatternSize(RHS->getDstPattern(), CGP); 108 if (LHSPatSize < RHSPatSize) return true; 109 if (LHSPatSize > RHSPatSize) return false; 110 111 // Sort based on the UID of the pattern, giving us a deterministic ordering 112 // if all other sorting conditions fail. 113 assert(LHS == RHS || LHS->ID != RHS->ID); 114 return LHS->ID < RHS->ID; 115 } 116 }; 117 } // End anonymous namespace 118 119 120 void DAGISelEmitter::run(raw_ostream &OS) { 121 emitSourceFileHeader("DAG Instruction Selector for the " + 122 CGP.getTargetInfo().getName() + " target", OS); 123 124 OS << "// *** NOTE: This file is #included into the middle of the target\n" 125 << "// *** instruction selector class. These functions are really " 126 << "methods.\n\n"; 127 128 DEBUG(errs() << "\n\nALL PATTERNS TO MATCH:\n\n"; 129 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), 130 E = CGP.ptm_end(); I != E; ++I) { 131 errs() << "PATTERN: "; I->getSrcPattern()->dump(); 132 errs() << "\nRESULT: "; I->getDstPattern()->dump(); 133 errs() << "\n"; 134 }); 135 136 // Add all the patterns to a temporary list so we can sort them. 137 std::vector<const PatternToMatch*> Patterns; 138 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end(); 139 I != E; ++I) 140 Patterns.push_back(&*I); 141 142 // We want to process the matches in order of minimal cost. Sort the patterns 143 // so the least cost one is at the start. 144 std::sort(Patterns.begin(), Patterns.end(), PatternSortingPredicate(CGP)); 145 146 147 // Convert each variant of each pattern into a Matcher. 148 std::vector<Matcher*> PatternMatchers; 149 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) { 150 for (unsigned Variant = 0; ; ++Variant) { 151 if (Matcher *M = ConvertPatternToMatcher(*Patterns[i], Variant, CGP)) 152 PatternMatchers.push_back(M); 153 else 154 break; 155 } 156 } 157 158 Matcher *TheMatcher = new ScopeMatcher(&PatternMatchers[0], 159 PatternMatchers.size()); 160 161 TheMatcher = OptimizeMatcher(TheMatcher, CGP); 162 //Matcher->dump(); 163 EmitMatcherTable(TheMatcher, CGP, OS); 164 delete TheMatcher; 165 } 166 167 namespace llvm { 168 169 void EmitDAGISel(RecordKeeper &RK, raw_ostream &OS) { 170 DAGISelEmitter(RK).run(OS); 171 } 172 173 } // End llvm namespace 174