2005-09-07 23:44:43 +00:00
|
|
|
//===- DAGISelEmitter.cpp - Generate an instruction selector --------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:37:13 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-09-07 23:44:43 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This tablegen backend emits a DAG instruction selector.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "DAGISelEmitter.h"
|
2010-02-15 08:04:42 +00:00
|
|
|
#include "DAGISelMatcher.h"
|
2005-09-07 23:44:43 +00:00
|
|
|
#include "Record.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-01-05 22:43:57 +00:00
|
|
|
// DAGISelEmitter Helper methods
|
2005-09-07 23:44:43 +00:00
|
|
|
//
|
|
|
|
|
2005-09-28 17:57:56 +00:00
|
|
|
/// getResultPatternCost - Compute the number of instructions for this pattern.
|
|
|
|
/// This is a temporary hack. We should really include the instruction
|
|
|
|
/// latencies in this calculation.
|
2008-01-05 22:25:12 +00:00
|
|
|
static unsigned getResultPatternCost(TreePatternNode *P,
|
2008-01-06 01:10:31 +00:00
|
|
|
CodeGenDAGPatterns &CGP) {
|
2005-09-28 17:57:56 +00:00
|
|
|
if (P->isLeaf()) return 0;
|
|
|
|
|
2006-02-18 02:33:09 +00:00
|
|
|
unsigned Cost = 0;
|
|
|
|
Record *Op = P->getOperator();
|
|
|
|
if (Op->isSubClassOf("Instruction")) {
|
|
|
|
Cost++;
|
2010-03-19 00:07:20 +00:00
|
|
|
CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
|
2009-10-29 18:10:34 +00:00
|
|
|
if (II.usesCustomInserter)
|
2006-02-18 02:33:09 +00:00
|
|
|
Cost += 10;
|
|
|
|
}
|
2005-09-28 17:57:56 +00:00
|
|
|
for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
|
2008-01-05 22:25:12 +00:00
|
|
|
Cost += getResultPatternCost(P->getChild(i), CGP);
|
2005-09-28 17:57:56 +00:00
|
|
|
return Cost;
|
|
|
|
}
|
|
|
|
|
2006-07-19 00:24:41 +00:00
|
|
|
/// getResultPatternCodeSize - Compute the code size of instructions for this
|
|
|
|
/// pattern.
|
2008-01-05 22:25:12 +00:00
|
|
|
static unsigned getResultPatternSize(TreePatternNode *P,
|
2008-01-06 01:10:31 +00:00
|
|
|
CodeGenDAGPatterns &CGP) {
|
2006-07-19 00:24:41 +00:00
|
|
|
if (P->isLeaf()) return 0;
|
|
|
|
|
|
|
|
unsigned Cost = 0;
|
|
|
|
Record *Op = P->getOperator();
|
|
|
|
if (Op->isSubClassOf("Instruction")) {
|
|
|
|
Cost += Op->getValueAsInt("CodeSize");
|
|
|
|
}
|
|
|
|
for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
|
2008-01-05 22:25:12 +00:00
|
|
|
Cost += getResultPatternSize(P->getChild(i), CGP);
|
2006-07-19 00:24:41 +00:00
|
|
|
return Cost;
|
|
|
|
}
|
|
|
|
|
2008-01-05 22:43:57 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Predicate emitter implementation.
|
|
|
|
//
|
|
|
|
|
2009-07-03 00:10:29 +00:00
|
|
|
void DAGISelEmitter::EmitPredicateFunctions(raw_ostream &OS) {
|
2008-01-05 22:43:57 +00:00
|
|
|
OS << "\n// Predicate functions.\n";
|
|
|
|
|
|
|
|
// Walk the pattern fragments, adding them to a map, which sorts them by
|
|
|
|
// name.
|
|
|
|
typedef std::map<std::string, std::pair<Record*, TreePattern*> > PFsByNameTy;
|
|
|
|
PFsByNameTy PFsByName;
|
|
|
|
|
2008-01-06 01:10:31 +00:00
|
|
|
for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end();
|
2008-01-05 22:43:57 +00:00
|
|
|
I != E; ++I)
|
|
|
|
PFsByName.insert(std::make_pair(I->first->getName(), *I));
|
|
|
|
|
|
|
|
|
|
|
|
for (PFsByNameTy::iterator I = PFsByName.begin(), E = PFsByName.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
Record *PatFragRecord = I->second.first;// Record that derives from PatFrag.
|
|
|
|
TreePattern *P = I->second.second;
|
|
|
|
|
|
|
|
// If there is a code init for this fragment, emit the predicate code.
|
|
|
|
std::string Code = PatFragRecord->getValueAsCode("Predicate");
|
|
|
|
if (Code.empty()) continue;
|
|
|
|
|
|
|
|
if (P->getOnlyTree()->isLeaf())
|
|
|
|
OS << "inline bool Predicate_" << PatFragRecord->getName()
|
2010-02-16 07:26:36 +00:00
|
|
|
<< "(SDNode *N) const {\n";
|
2008-01-05 22:43:57 +00:00
|
|
|
else {
|
|
|
|
std::string ClassName =
|
2008-01-05 22:58:54 +00:00
|
|
|
CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName();
|
2008-01-05 22:43:57 +00:00
|
|
|
const char *C2 = ClassName == "SDNode" ? "N" : "inN";
|
|
|
|
|
|
|
|
OS << "inline bool Predicate_" << PatFragRecord->getName()
|
2010-02-16 07:26:36 +00:00
|
|
|
<< "(SDNode *" << C2 << ") const {\n";
|
2008-01-05 22:43:57 +00:00
|
|
|
if (ClassName != "SDNode")
|
|
|
|
OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
|
|
|
|
}
|
|
|
|
OS << Code << "\n}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << "\n\n";
|
|
|
|
}
|
|
|
|
|
2010-03-29 01:40:38 +00:00
|
|
|
|
2010-02-21 19:22:06 +00:00
|
|
|
namespace {
|
|
|
|
// PatternSortingPredicate - return true if we prefer to match LHS before RHS.
|
|
|
|
// In particular, we want to match maximal patterns first and lowest cost within
|
|
|
|
// a particular complexity first.
|
2010-03-01 21:49:54 +00:00
|
|
|
struct PatternSortingPredicate {
|
|
|
|
PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {}
|
2010-02-21 19:22:06 +00:00
|
|
|
CodeGenDAGPatterns &CGP;
|
|
|
|
|
2010-03-29 01:40:38 +00:00
|
|
|
bool operator()(const PatternToMatch *LHS, const PatternToMatch *RHS) {
|
|
|
|
// Otherwise, if the patterns might both match, sort based on complexity,
|
|
|
|
// which means that we prefer to match patterns that cover more nodes in the
|
|
|
|
// input over nodes that cover fewer.
|
|
|
|
unsigned LHSSize = LHS->getPatternComplexity(CGP);
|
|
|
|
unsigned RHSSize = RHS->getPatternComplexity(CGP);
|
2010-02-21 19:22:06 +00:00
|
|
|
if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost
|
|
|
|
if (LHSSize < RHSSize) return false;
|
|
|
|
|
|
|
|
// If the patterns have equal complexity, compare generated instruction cost
|
|
|
|
unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP);
|
|
|
|
unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP);
|
|
|
|
if (LHSCost < RHSCost) return true;
|
|
|
|
if (LHSCost > RHSCost) return false;
|
|
|
|
|
2010-03-01 22:09:11 +00:00
|
|
|
unsigned LHSPatSize = getResultPatternSize(LHS->getDstPattern(), CGP);
|
|
|
|
unsigned RHSPatSize = getResultPatternSize(RHS->getDstPattern(), CGP);
|
|
|
|
if (LHSPatSize < RHSPatSize) return true;
|
|
|
|
if (LHSPatSize > RHSPatSize) return false;
|
|
|
|
|
2010-03-29 01:40:38 +00:00
|
|
|
// Sort based on the UID of the pattern, giving us a deterministic ordering
|
|
|
|
// if all other sorting conditions fail.
|
2010-03-02 18:15:02 +00:00
|
|
|
assert(LHS == RHS || LHS->ID != RHS->ID);
|
2010-03-01 22:09:11 +00:00
|
|
|
return LHS->ID < RHS->ID;
|
2010-02-21 19:22:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-03 00:10:29 +00:00
|
|
|
void DAGISelEmitter::run(raw_ostream &OS) {
|
2008-01-05 22:58:54 +00:00
|
|
|
EmitSourceFileHeader("DAG Instruction Selector for the " +
|
|
|
|
CGP.getTargetInfo().getName() + " target", OS);
|
2005-09-07 23:44:43 +00:00
|
|
|
|
2005-09-14 00:09:24 +00:00
|
|
|
OS << "// *** NOTE: This file is #included into the middle of the target\n"
|
|
|
|
<< "// *** instruction selector class. These functions are really "
|
|
|
|
<< "methods.\n\n";
|
2006-08-09 16:44:44 +00:00
|
|
|
|
2010-03-01 01:54:19 +00:00
|
|
|
DEBUG(errs() << "\n\nALL PATTERNS TO MATCH:\n\n";
|
|
|
|
for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
|
|
|
|
E = CGP.ptm_end(); I != E; ++I) {
|
|
|
|
errs() << "PATTERN: "; I->getSrcPattern()->dump();
|
|
|
|
errs() << "\nRESULT: "; I->getDstPattern()->dump();
|
|
|
|
errs() << "\n";
|
|
|
|
});
|
|
|
|
|
|
|
|
// FIXME: These are being used by hand written code, gross.
|
2008-01-05 22:43:57 +00:00
|
|
|
EmitPredicateFunctions(OS);
|
2010-03-01 01:54:19 +00:00
|
|
|
|
2010-02-21 19:22:06 +00:00
|
|
|
// Add all the patterns to a temporary list so we can sort them.
|
|
|
|
std::vector<const PatternToMatch*> Patterns;
|
|
|
|
for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end();
|
|
|
|
I != E; ++I)
|
|
|
|
Patterns.push_back(&*I);
|
|
|
|
|
|
|
|
// We want to process the matches in order of minimal cost. Sort the patterns
|
|
|
|
// so the least cost one is at the start.
|
2010-03-29 01:58:15 +00:00
|
|
|
std::stable_sort(Patterns.begin(), Patterns.end(),
|
|
|
|
PatternSortingPredicate(CGP));
|
2010-02-21 19:22:06 +00:00
|
|
|
|
|
|
|
|
2010-03-01 07:17:40 +00:00
|
|
|
// Convert each variant of each pattern into a Matcher.
|
2010-02-25 19:00:39 +00:00
|
|
|
std::vector<Matcher*> PatternMatchers;
|
2010-03-01 07:17:40 +00:00
|
|
|
for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
|
|
|
|
for (unsigned Variant = 0; ; ++Variant) {
|
|
|
|
if (Matcher *M = ConvertPatternToMatcher(*Patterns[i], Variant, CGP))
|
|
|
|
PatternMatchers.push_back(M);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-25 19:00:39 +00:00
|
|
|
Matcher *TheMatcher = new ScopeMatcher(&PatternMatchers[0],
|
|
|
|
PatternMatchers.size());
|
2010-02-16 23:13:59 +00:00
|
|
|
|
2010-02-28 20:49:53 +00:00
|
|
|
TheMatcher = OptimizeMatcher(TheMatcher, CGP);
|
2010-02-15 08:04:42 +00:00
|
|
|
//Matcher->dump();
|
2010-03-01 01:54:19 +00:00
|
|
|
EmitMatcherTable(TheMatcher, CGP, OS);
|
2010-02-25 02:04:40 +00:00
|
|
|
delete TheMatcher;
|
2005-09-07 23:44:43 +00:00
|
|
|
}
|