Fix typo pointed out in pr9339.
[llvm/stm8.git] / utils / TableGen / DAGISelEmitter.cpp
blob8a73404dad95ca9a0fedd3be422aedff35aeed41
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.
12 //===----------------------------------------------------------------------===//
14 #include "DAGISelEmitter.h"
15 #include "DAGISelMatcher.h"
16 #include "Record.h"
17 #include "llvm/Support/Debug.h"
18 using namespace llvm;
20 //===----------------------------------------------------------------------===//
21 // DAGISelEmitter Helper methods
24 /// getResultPatternCost - Compute the number of instructions for this pattern.
25 /// This is a temporary hack. We should really include the instruction
26 /// latencies in this calculation.
27 static unsigned getResultPatternCost(TreePatternNode *P,
28 CodeGenDAGPatterns &CGP) {
29 if (P->isLeaf()) return 0;
31 unsigned Cost = 0;
32 Record *Op = P->getOperator();
33 if (Op->isSubClassOf("Instruction")) {
34 Cost++;
35 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
36 if (II.usesCustomInserter)
37 Cost += 10;
39 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
40 Cost += getResultPatternCost(P->getChild(i), CGP);
41 return Cost;
44 /// getResultPatternCodeSize - Compute the code size of instructions for this
45 /// pattern.
46 static unsigned getResultPatternSize(TreePatternNode *P,
47 CodeGenDAGPatterns &CGP) {
48 if (P->isLeaf()) return 0;
50 unsigned Cost = 0;
51 Record *Op = P->getOperator();
52 if (Op->isSubClassOf("Instruction")) {
53 Cost += Op->getValueAsInt("CodeSize");
55 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
56 Cost += getResultPatternSize(P->getChild(i), CGP);
57 return Cost;
60 namespace {
61 // PatternSortingPredicate - return true if we prefer to match LHS before RHS.
62 // In particular, we want to match maximal patterns first and lowest cost within
63 // a particular complexity first.
64 struct PatternSortingPredicate {
65 PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {}
66 CodeGenDAGPatterns &CGP;
68 bool operator()(const PatternToMatch *LHS, const PatternToMatch *RHS) {
69 const TreePatternNode *LHSSrc = LHS->getSrcPattern();
70 const TreePatternNode *RHSSrc = RHS->getSrcPattern();
72 if (LHSSrc->getNumTypes() != 0 && RHSSrc->getNumTypes() != 0 &&
73 LHSSrc->getType(0) != RHSSrc->getType(0)) {
74 MVT::SimpleValueType V1 = LHSSrc->getType(0), V2 = RHSSrc->getType(0);
75 if (MVT(V1).isVector() != MVT(V2).isVector())
76 return MVT(V2).isVector();
78 if (MVT(V1).isFloatingPoint() != MVT(V2).isFloatingPoint())
79 return MVT(V2).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 unsigned LHSSize = LHS->getPatternComplexity(CGP);
86 unsigned RHSSize = RHS->getPatternComplexity(CGP);
87 if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost
88 if (LHSSize < RHSSize) return false;
90 // If the patterns have equal complexity, compare generated instruction cost
91 unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP);
92 unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP);
93 if (LHSCost < RHSCost) return true;
94 if (LHSCost > RHSCost) return false;
96 unsigned LHSPatSize = getResultPatternSize(LHS->getDstPattern(), CGP);
97 unsigned RHSPatSize = getResultPatternSize(RHS->getDstPattern(), CGP);
98 if (LHSPatSize < RHSPatSize) return true;
99 if (LHSPatSize > RHSPatSize) return false;
101 // Sort based on the UID of the pattern, giving us a deterministic ordering
102 // if all other sorting conditions fail.
103 assert(LHS == RHS || LHS->ID != RHS->ID);
104 return LHS->ID < RHS->ID;
110 void DAGISelEmitter::run(raw_ostream &OS) {
111 EmitSourceFileHeader("DAG Instruction Selector for the " +
112 CGP.getTargetInfo().getName() + " target", OS);
114 OS << "// *** NOTE: This file is #included into the middle of the target\n"
115 << "// *** instruction selector class. These functions are really "
116 << "methods.\n\n";
118 DEBUG(errs() << "\n\nALL PATTERNS TO MATCH:\n\n";
119 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
120 E = CGP.ptm_end(); I != E; ++I) {
121 errs() << "PATTERN: "; I->getSrcPattern()->dump();
122 errs() << "\nRESULT: "; I->getDstPattern()->dump();
123 errs() << "\n";
126 // Add all the patterns to a temporary list so we can sort them.
127 std::vector<const PatternToMatch*> Patterns;
128 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end();
129 I != E; ++I)
130 Patterns.push_back(&*I);
132 // We want to process the matches in order of minimal cost. Sort the patterns
133 // so the least cost one is at the start.
134 std::sort(Patterns.begin(), Patterns.end(), PatternSortingPredicate(CGP));
137 // Convert each variant of each pattern into a Matcher.
138 std::vector<Matcher*> PatternMatchers;
139 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
140 for (unsigned Variant = 0; ; ++Variant) {
141 if (Matcher *M = ConvertPatternToMatcher(*Patterns[i], Variant, CGP))
142 PatternMatchers.push_back(M);
143 else
144 break;
148 Matcher *TheMatcher = new ScopeMatcher(&PatternMatchers[0],
149 PatternMatchers.size());
151 TheMatcher = OptimizeMatcher(TheMatcher, CGP);
152 //Matcher->dump();
153 EmitMatcherTable(TheMatcher, CGP, OS);
154 delete TheMatcher;