1
//===- DAGISelEmitter.cpp - Generate an instruction selector --------------===//
3
// The LLVM Compiler Infrastructure
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
8
//===----------------------------------------------------------------------===//
10
// This tablegen backend emits a DAG instruction selector.
12
//===----------------------------------------------------------------------===//
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"
21
#define DEBUG_TYPE "dag-isel-emitter"
24
/// DAGISelEmitter - The top-level class which coordinates construction
25
/// and emission of the instruction selector.
26
class DAGISelEmitter {
27
CodeGenDAGPatterns CGP;
29
explicit DAGISelEmitter(RecordKeeper &R) : CGP(R) {}
30
void run(raw_ostream &OS);
32
} // End anonymous namespace
34
//===----------------------------------------------------------------------===//
35
// DAGISelEmitter Helper methods
38
/// getResultPatternCost - Compute the number of instructions for this pattern.
39
/// This is a temporary hack. We should really include the instruction
40
/// latencies in this calculation.
41
static unsigned getResultPatternCost(TreePatternNode *P,
42
CodeGenDAGPatterns &CGP) {
43
if (P->isLeaf()) return 0;
46
Record *Op = P->getOperator();
47
if (Op->isSubClassOf("Instruction")) {
49
CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
50
if (II.usesCustomInserter)
53
for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
54
Cost += getResultPatternCost(P->getChild(i), CGP);
58
/// getResultPatternCodeSize - Compute the code size of instructions for this
60
static unsigned getResultPatternSize(TreePatternNode *P,
61
CodeGenDAGPatterns &CGP) {
62
if (P->isLeaf()) return 0;
65
Record *Op = P->getOperator();
66
if (Op->isSubClassOf("Instruction")) {
67
Cost += Op->getValueAsInt("CodeSize");
69
for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
70
Cost += getResultPatternSize(P->getChild(i), CGP);
75
// PatternSortingPredicate - return true if we prefer to match LHS before RHS.
76
// In particular, we want to match maximal patterns first and lowest cost within
77
// a particular complexity first.
78
struct PatternSortingPredicate {
79
PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {}
80
CodeGenDAGPatterns &CGP;
82
bool operator()(const PatternToMatch *LHS, const PatternToMatch *RHS) {
83
const TreePatternNode *LHSSrc = LHS->getSrcPattern();
84
const TreePatternNode *RHSSrc = RHS->getSrcPattern();
86
MVT LHSVT = (LHSSrc->getNumTypes() != 0 ? LHSSrc->getType(0) : MVT::Other);
87
MVT RHSVT = (RHSSrc->getNumTypes() != 0 ? RHSSrc->getType(0) : MVT::Other);
88
if (LHSVT.isVector() != RHSVT.isVector())
89
return RHSVT.isVector();
91
if (LHSVT.isFloatingPoint() != RHSVT.isFloatingPoint())
92
return RHSVT.isFloatingPoint();
94
// Otherwise, if the patterns might both match, sort based on complexity,
95
// which means that we prefer to match patterns that cover more nodes in the
96
// input over nodes that cover fewer.
97
int LHSSize = LHS->getPatternComplexity(CGP);
98
int RHSSize = RHS->getPatternComplexity(CGP);
99
if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost
100
if (LHSSize < RHSSize) return false;
102
// If the patterns have equal complexity, compare generated instruction cost
103
unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP);
104
unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP);
105
if (LHSCost < RHSCost) return true;
106
if (LHSCost > RHSCost) return false;
108
unsigned LHSPatSize = getResultPatternSize(LHS->getDstPattern(), CGP);
109
unsigned RHSPatSize = getResultPatternSize(RHS->getDstPattern(), CGP);
110
if (LHSPatSize < RHSPatSize) return true;
111
if (LHSPatSize > RHSPatSize) return false;
113
// Sort based on the UID of the pattern, giving us a deterministic ordering
114
// if all other sorting conditions fail.
115
assert(LHS == RHS || LHS->ID != RHS->ID);
116
return LHS->ID < RHS->ID;
119
} // End anonymous namespace
122
void DAGISelEmitter::run(raw_ostream &OS) {
123
emitSourceFileHeader("DAG Instruction Selector for the " +
124
CGP.getTargetInfo().getName() + " target", OS);
126
OS << "// *** NOTE: This file is #included into the middle of the target\n"
127
<< "// *** instruction selector class. These functions are really "
130
DEBUG(errs() << "\n\nALL PATTERNS TO MATCH:\n\n";
131
for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
132
E = CGP.ptm_end(); I != E; ++I) {
133
errs() << "PATTERN: "; I->getSrcPattern()->dump();
134
errs() << "\nRESULT: "; I->getDstPattern()->dump();
138
// Add all the patterns to a temporary list so we can sort them.
139
std::vector<const PatternToMatch*> Patterns;
140
for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end();
142
Patterns.push_back(&*I);
144
// We want to process the matches in order of minimal cost. Sort the patterns
145
// so the least cost one is at the start.
146
std::sort(Patterns.begin(), Patterns.end(), PatternSortingPredicate(CGP));
149
// Convert each variant of each pattern into a Matcher.
150
std::vector<Matcher*> PatternMatchers;
151
for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
152
for (unsigned Variant = 0; ; ++Variant) {
153
if (Matcher *M = ConvertPatternToMatcher(*Patterns[i], Variant, CGP))
154
PatternMatchers.push_back(M);
160
std::unique_ptr<Matcher> TheMatcher =
161
llvm::make_unique<ScopeMatcher>(PatternMatchers);
163
OptimizeMatcher(TheMatcher, CGP);
165
EmitMatcherTable(TheMatcher.get(), CGP, OS);
170
void EmitDAGISel(RecordKeeper &RK, raw_ostream &OS) {
171
DAGISelEmitter(RK).run(OS);
174
} // End llvm namespace