2005-09-08 07:44:43 +08:00
|
|
|
//===- DAGISelEmitter.cpp - Generate an instruction selector --------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2005-09-08 07:44:43 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This tablegen backend emits a DAG instruction selector.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-06-11 23:37:55 +08:00
|
|
|
#include "CodeGenDAGPatterns.h"
|
2010-02-15 16:04:42 +08:00
|
|
|
#include "DAGISelMatcher.h"
|
2005-09-08 07:44:43 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2012-06-11 23:37:55 +08:00
|
|
|
#include "llvm/TableGen/Record.h"
|
|
|
|
#include "llvm/TableGen/TableGenBackend.h"
|
2005-09-08 07:44:43 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 06:55:11 +08:00
|
|
|
#define DEBUG_TYPE "dag-isel-emitter"
|
|
|
|
|
2012-06-11 23:37:55 +08:00
|
|
|
namespace {
|
|
|
|
/// DAGISelEmitter - The top-level class which coordinates construction
|
|
|
|
/// and emission of the instruction selector.
|
|
|
|
class DAGISelEmitter {
|
2020-10-22 14:58:49 +08:00
|
|
|
RecordKeeper &Records; // Just so we can get at the timing functions.
|
2012-06-11 23:37:55 +08:00
|
|
|
CodeGenDAGPatterns CGP;
|
|
|
|
public:
|
2020-10-22 14:58:49 +08:00
|
|
|
explicit DAGISelEmitter(RecordKeeper &R) : Records(R), CGP(R) {}
|
2012-06-11 23:37:55 +08:00
|
|
|
void run(raw_ostream &OS);
|
|
|
|
};
|
|
|
|
} // End anonymous namespace
|
|
|
|
|
2005-09-08 07:44:43 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-01-06 06:43:57 +08:00
|
|
|
// DAGISelEmitter Helper methods
|
2005-09-08 07:44:43 +08:00
|
|
|
//
|
|
|
|
|
2005-09-29 01:57:56 +08: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.
|
2018-06-15 04:32:58 +08:00
|
|
|
static unsigned getResultPatternCost(TreePatternNode *P,
|
2008-01-06 09:10:31 +08:00
|
|
|
CodeGenDAGPatterns &CGP) {
|
2018-06-15 04:32:58 +08:00
|
|
|
if (P->isLeaf()) return 0;
|
2011-03-01 09:39:05 +08:00
|
|
|
|
2006-02-18 10:33:09 +08:00
|
|
|
unsigned Cost = 0;
|
2018-06-15 04:32:58 +08:00
|
|
|
Record *Op = P->getOperator();
|
2006-02-18 10:33:09 +08:00
|
|
|
if (Op->isSubClassOf("Instruction")) {
|
|
|
|
Cost++;
|
2010-03-19 08:07:20 +08:00
|
|
|
CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
|
2009-10-30 02:10:34 +08:00
|
|
|
if (II.usesCustomInserter)
|
2006-02-18 10:33:09 +08:00
|
|
|
Cost += 10;
|
|
|
|
}
|
2018-06-15 04:32:58 +08:00
|
|
|
for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
|
|
|
|
Cost += getResultPatternCost(P->getChild(i), CGP);
|
2005-09-29 01:57:56 +08:00
|
|
|
return Cost;
|
|
|
|
}
|
|
|
|
|
2006-07-19 08:24:41 +08:00
|
|
|
/// getResultPatternCodeSize - Compute the code size of instructions for this
|
|
|
|
/// pattern.
|
2018-06-15 04:32:58 +08:00
|
|
|
static unsigned getResultPatternSize(TreePatternNode *P,
|
2008-01-06 09:10:31 +08:00
|
|
|
CodeGenDAGPatterns &CGP) {
|
2018-06-15 04:32:58 +08:00
|
|
|
if (P->isLeaf()) return 0;
|
2006-07-19 08:24:41 +08:00
|
|
|
|
|
|
|
unsigned Cost = 0;
|
2018-06-15 04:32:58 +08:00
|
|
|
Record *Op = P->getOperator();
|
2006-07-19 08:24:41 +08:00
|
|
|
if (Op->isSubClassOf("Instruction")) {
|
|
|
|
Cost += Op->getValueAsInt("CodeSize");
|
|
|
|
}
|
2018-06-15 04:32:58 +08:00
|
|
|
for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
|
|
|
|
Cost += getResultPatternSize(P->getChild(i), CGP);
|
2006-07-19 08:24:41 +08:00
|
|
|
return Cost;
|
|
|
|
}
|
|
|
|
|
2010-02-22 03:22:06 +08: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-02 05:49:54 +08:00
|
|
|
struct PatternSortingPredicate {
|
|
|
|
PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {}
|
2010-02-22 03:22:06 +08:00
|
|
|
CodeGenDAGPatterns &CGP;
|
2011-03-01 09:39:05 +08:00
|
|
|
|
2010-03-29 09:40:38 +08:00
|
|
|
bool operator()(const PatternToMatch *LHS, const PatternToMatch *RHS) {
|
2017-09-15 00:56:21 +08:00
|
|
|
const TreePatternNode *LT = LHS->getSrcPattern();
|
|
|
|
const TreePatternNode *RT = RHS->getSrcPattern();
|
2011-03-01 09:39:05 +08:00
|
|
|
|
2017-09-15 00:56:21 +08:00
|
|
|
MVT LHSVT = LT->getNumTypes() != 0 ? LT->getSimpleType(0) : MVT::Other;
|
|
|
|
MVT RHSVT = RT->getNumTypes() != 0 ? RT->getSimpleType(0) : MVT::Other;
|
2013-10-01 17:49:01 +08:00
|
|
|
if (LHSVT.isVector() != RHSVT.isVector())
|
|
|
|
return RHSVT.isVector();
|
2011-03-01 09:39:05 +08:00
|
|
|
|
2013-10-01 17:49:01 +08:00
|
|
|
if (LHSVT.isFloatingPoint() != RHSVT.isFloatingPoint())
|
|
|
|
return RHSVT.isFloatingPoint();
|
2011-03-01 09:39:05 +08:00
|
|
|
|
2010-03-29 09:40:38 +08:00
|
|
|
// 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.
|
2014-08-01 08:32:36 +08:00
|
|
|
int LHSSize = LHS->getPatternComplexity(CGP);
|
|
|
|
int RHSSize = RHS->getPatternComplexity(CGP);
|
2010-02-22 03:22:06 +08:00
|
|
|
if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost
|
|
|
|
if (LHSSize < RHSSize) return false;
|
2011-03-01 09:39:05 +08:00
|
|
|
|
2010-02-22 03:22:06 +08:00
|
|
|
// If the patterns have equal complexity, compare generated instruction cost
|
2018-06-15 04:32:58 +08:00
|
|
|
unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP);
|
|
|
|
unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP);
|
2010-02-22 03:22:06 +08:00
|
|
|
if (LHSCost < RHSCost) return true;
|
|
|
|
if (LHSCost > RHSCost) return false;
|
2011-03-01 09:39:05 +08:00
|
|
|
|
2018-06-15 04:32:58 +08:00
|
|
|
unsigned LHSPatSize = getResultPatternSize(LHS->getDstPattern(), CGP);
|
|
|
|
unsigned RHSPatSize = getResultPatternSize(RHS->getDstPattern(), CGP);
|
2010-03-02 06:09:11 +08:00
|
|
|
if (LHSPatSize < RHSPatSize) return true;
|
|
|
|
if (LHSPatSize > RHSPatSize) return false;
|
2011-03-01 09:39:05 +08:00
|
|
|
|
[TableGen] Support multi-alternative pattern fragments
A TableGen instruction record usually contains a DAG pattern that will
describe the SelectionDAG operation that can be implemented by this
instruction. However, there will be cases where several different DAG
patterns can all be implemented by the same instruction. The way to
represent this today is to write additional patterns in the Pattern
(or usually Pat) class that map those extra DAG patterns to the
instruction. This usually also works fine.
However, I've noticed cases where the current setup seems to require
quite a bit of extra (and duplicated) text in the target .td files.
For example, in the SystemZ back-end, there are quite a number of
instructions that can implement an "add-with-overflow" operation.
The same instructions also need to be used to implement just plain
addition (simply ignoring the extra overflow output). The current
solution requires creating extra Pat pattern for every instruction,
duplicating the information about which particular add operands
map best to which particular instruction.
This patch enhances TableGen to support a new PatFrags class, which
can be used to encapsulate multiple alternative patterns that may
all match to the same instruction. It operates the same way as the
existing PatFrag class, except that it accepts a list of DAG patterns
to match instead of just a single one. As an example, we can now define
a PatFrags to match either an "add-with-overflow" or a regular add
operation:
def z_sadd : PatFrags<(ops node:$src1, node:$src2),
[(z_saddo node:$src1, node:$src2),
(add node:$src1, node:$src2)]>;
and then use this in the add instruction pattern:
defm AR : BinaryRRAndK<"ar", 0x1A, 0xB9F8, z_sadd, GR32, GR32>;
These SystemZ target changes are implemented here as well.
Note that PatFrag is now defined as a subclass of PatFrags, which
means that some users of internals of PatFrag need to be updated.
(E.g. instead of using PatFrag.Fragment you now need to use
!head(PatFrag.Fragments).)
The implementation is based on the following main ideas:
- InlinePatternFragments may now replace each original pattern
with several result patterns, not just one.
- parseInstructionPattern delays calling InlinePatternFragments
and InferAllTypes. Instead, it extracts a single DAG match
pattern from the main instruction pattern.
- Processing of the DAG match pattern part of the main instruction
pattern now shares most code with processing match patterns from
the Pattern class.
- Direct use of main instruction patterns in InferFromPattern and
EmitResultInstructionAsOperand is removed; everything now operates
solely on DAG match patterns.
Reviewed by: hfinkel
Differential Revision: https://reviews.llvm.org/D48545
llvm-svn: 336999
2018-07-13 21:18:00 +08:00
|
|
|
// Sort based on the UID of the pattern, to reflect source order.
|
|
|
|
// Note that this is not guaranteed to be unique, since a single source
|
|
|
|
// pattern may have been resolved into multiple match patterns due to
|
|
|
|
// alternative fragments. To ensure deterministic output, always use
|
|
|
|
// std::stable_sort with this predicate.
|
2021-02-07 14:31:38 +08:00
|
|
|
return LHS->getID() < RHS->getID();
|
2010-02-22 03:22:06 +08:00
|
|
|
}
|
|
|
|
};
|
2012-06-11 23:37:55 +08:00
|
|
|
} // End anonymous namespace
|
2010-02-22 03:22:06 +08:00
|
|
|
|
|
|
|
|
2009-07-03 08:10:29 +08:00
|
|
|
void DAGISelEmitter::run(raw_ostream &OS) {
|
2012-06-11 23:37:55 +08:00
|
|
|
emitSourceFileHeader("DAG Instruction Selector for the " +
|
2016-12-04 13:48:16 +08:00
|
|
|
CGP.getTargetInfo().getName().str() + " target", OS);
|
2011-03-01 09:39:05 +08:00
|
|
|
|
2005-09-14 08:09:24 +08: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-10 00:44:44 +08:00
|
|
|
|
2017-11-11 02:36:04 +08:00
|
|
|
OS << "// If GET_DAGISEL_DECL is #defined with any value, only function\n"
|
|
|
|
"// declarations will be included when this file is included.\n"
|
|
|
|
"// If GET_DAGISEL_BODY is #defined, its value should be the name of\n"
|
|
|
|
"// the instruction selector class. Function bodies will be emitted\n"
|
|
|
|
"// and each function's name will be qualified with the name of the\n"
|
|
|
|
"// class.\n"
|
|
|
|
"//\n"
|
|
|
|
"// When neither of the GET_DAGISEL* macros is defined, the functions\n"
|
|
|
|
"// are emitted inline.\n\n";
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_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";
|
|
|
|
});
|
2010-03-01 09:54:19 +08:00
|
|
|
|
2010-02-22 03:22:06 +08:00
|
|
|
// Add all the patterns to a temporary list so we can sort them.
|
2020-10-22 14:58:49 +08:00
|
|
|
Records.startTimer("Sort patterns");
|
2010-02-22 03:22:06 +08:00
|
|
|
std::vector<const PatternToMatch*> Patterns;
|
2021-02-02 12:55:08 +08:00
|
|
|
for (const PatternToMatch &PTM : CGP.ptms())
|
|
|
|
Patterns.push_back(&PTM);
|
2010-02-22 03:22:06 +08:00
|
|
|
|
|
|
|
// We want to process the matches in order of minimal cost. Sort the patterns
|
|
|
|
// so the least cost one is at the start.
|
2021-01-14 11:14:42 +08:00
|
|
|
llvm::stable_sort(Patterns, PatternSortingPredicate(CGP));
|
2011-03-01 09:39:05 +08:00
|
|
|
|
2010-03-01 15:17:40 +08:00
|
|
|
// Convert each variant of each pattern into a Matcher.
|
2020-10-22 14:58:49 +08:00
|
|
|
Records.startTimer("Convert to matchers");
|
2010-02-26 03:00:39 +08:00
|
|
|
std::vector<Matcher*> PatternMatchers;
|
2021-04-18 12:49:45 +08:00
|
|
|
for (const PatternToMatch *PTM : Patterns) {
|
2010-03-01 15:17:40 +08:00
|
|
|
for (unsigned Variant = 0; ; ++Variant) {
|
2021-04-18 12:49:45 +08:00
|
|
|
if (Matcher *M = ConvertPatternToMatcher(*PTM, Variant, CGP))
|
2010-03-01 15:17:40 +08:00
|
|
|
PatternMatchers.push_back(M);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-03-01 09:39:05 +08:00
|
|
|
|
2014-12-15 08:40:07 +08:00
|
|
|
std::unique_ptr<Matcher> TheMatcher =
|
2019-08-15 23:54:37 +08:00
|
|
|
std::make_unique<ScopeMatcher>(PatternMatchers);
|
2010-02-17 07:13:59 +08:00
|
|
|
|
2020-10-22 14:58:49 +08:00
|
|
|
Records.startTimer("Optimize matchers");
|
2014-12-15 08:40:07 +08:00
|
|
|
OptimizeMatcher(TheMatcher, CGP);
|
2020-10-22 14:58:49 +08:00
|
|
|
|
2010-02-15 16:04:42 +08:00
|
|
|
//Matcher->dump();
|
2020-10-22 14:58:49 +08:00
|
|
|
|
|
|
|
Records.startTimer("Emit matcher table");
|
2014-12-15 08:40:07 +08:00
|
|
|
EmitMatcherTable(TheMatcher.get(), CGP, OS);
|
2005-09-08 07:44:43 +08:00
|
|
|
}
|
2012-06-11 23:37:55 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
void EmitDAGISel(RecordKeeper &RK, raw_ostream &OS) {
|
2020-11-26 10:00:28 +08:00
|
|
|
RK.startTimer("Parse patterns");
|
2012-06-11 23:37:55 +08:00
|
|
|
DAGISelEmitter(RK).run(OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // End llvm namespace
|