blob: 62a0ff7007256c64f010f10a5af6a1f9f7d85950 [file] [log] [blame]
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001//===- DAGISelEmitter.cpp - Generate an instruction selector --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner54cb8fd2005-09-07 23:44:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend emits a DAG instruction selector.
11//
12//===----------------------------------------------------------------------===//
13
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000014#include "CodeGenDAGPatterns.h"
Chris Lattnerda272d12010-02-15 08:04:42 +000015#include "DAGISelMatcher.h"
Chris Lattner54cb8fd2005-09-07 23:44:43 +000016#include "llvm/Support/Debug.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000017#include "llvm/TableGen/Record.h"
18#include "llvm/TableGen/TableGenBackend.h"
Chris Lattner54cb8fd2005-09-07 23:44:43 +000019using namespace llvm;
20
Chandler Carruth283b3992014-04-21 22:55:11 +000021#define DEBUG_TYPE "dag-isel-emitter"
22
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000023namespace {
24/// DAGISelEmitter - The top-level class which coordinates construction
25/// and emission of the instruction selector.
26class DAGISelEmitter {
27 CodeGenDAGPatterns CGP;
28public:
29 explicit DAGISelEmitter(RecordKeeper &R) : CGP(R) {}
30 void run(raw_ostream &OS);
31};
32} // End anonymous namespace
33
Chris Lattnerca559d02005-09-08 21:03:01 +000034//===----------------------------------------------------------------------===//
Chris Lattnerdc32f982008-01-05 22:43:57 +000035// DAGISelEmitter Helper methods
Chris Lattner54cb8fd2005-09-07 23:44:43 +000036//
37
Chris Lattner05814af2005-09-28 17:57:56 +000038/// 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.
Florian Hahn74dff3b2018-06-14 20:32:58 +000041static unsigned getResultPatternCost(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +000042 CodeGenDAGPatterns &CGP) {
Florian Hahn74dff3b2018-06-14 20:32:58 +000043 if (P->isLeaf()) return 0;
Jim Grosbach9d401932011-03-01 01:39:05 +000044
Evan Chengfbad7082006-02-18 02:33:09 +000045 unsigned Cost = 0;
Florian Hahn74dff3b2018-06-14 20:32:58 +000046 Record *Op = P->getOperator();
Evan Chengfbad7082006-02-18 02:33:09 +000047 if (Op->isSubClassOf("Instruction")) {
48 Cost++;
Chris Lattnerf30187a2010-03-19 00:07:20 +000049 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
Dan Gohman533297b2009-10-29 18:10:34 +000050 if (II.usesCustomInserter)
Evan Chengfbad7082006-02-18 02:33:09 +000051 Cost += 10;
52 }
Florian Hahn74dff3b2018-06-14 20:32:58 +000053 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
54 Cost += getResultPatternCost(P->getChild(i), CGP);
Chris Lattner05814af2005-09-28 17:57:56 +000055 return Cost;
56}
57
Evan Chenge6f32032006-07-19 00:24:41 +000058/// getResultPatternCodeSize - Compute the code size of instructions for this
59/// pattern.
Florian Hahn74dff3b2018-06-14 20:32:58 +000060static unsigned getResultPatternSize(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +000061 CodeGenDAGPatterns &CGP) {
Florian Hahn74dff3b2018-06-14 20:32:58 +000062 if (P->isLeaf()) return 0;
Evan Chenge6f32032006-07-19 00:24:41 +000063
64 unsigned Cost = 0;
Florian Hahn74dff3b2018-06-14 20:32:58 +000065 Record *Op = P->getOperator();
Evan Chenge6f32032006-07-19 00:24:41 +000066 if (Op->isSubClassOf("Instruction")) {
67 Cost += Op->getValueAsInt("CodeSize");
68 }
Florian Hahn74dff3b2018-06-14 20:32:58 +000069 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
70 Cost += getResultPatternSize(P->getChild(i), CGP);
Evan Chenge6f32032006-07-19 00:24:41 +000071 return Cost;
72}
73
Chris Lattner99ce6e82010-02-21 19:22:06 +000074namespace {
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.
Chris Lattneradc53472010-03-01 21:49:54 +000078struct PatternSortingPredicate {
79 PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {}
Chris Lattner99ce6e82010-02-21 19:22:06 +000080 CodeGenDAGPatterns &CGP;
Jim Grosbach9d401932011-03-01 01:39:05 +000081
Chris Lattner48e86db2010-03-29 01:40:38 +000082 bool operator()(const PatternToMatch *LHS, const PatternToMatch *RHS) {
Krzysztof Parzyszekdb815642017-09-14 16:56:21 +000083 const TreePatternNode *LT = LHS->getSrcPattern();
84 const TreePatternNode *RT = RHS->getSrcPattern();
Jim Grosbach9d401932011-03-01 01:39:05 +000085
Krzysztof Parzyszekdb815642017-09-14 16:56:21 +000086 MVT LHSVT = LT->getNumTypes() != 0 ? LT->getSimpleType(0) : MVT::Other;
87 MVT RHSVT = RT->getNumTypes() != 0 ? RT->getSimpleType(0) : MVT::Other;
Richard Sandiford737ca5f2013-10-01 09:49:01 +000088 if (LHSVT.isVector() != RHSVT.isVector())
89 return RHSVT.isVector();
Jim Grosbach9d401932011-03-01 01:39:05 +000090
Richard Sandiford737ca5f2013-10-01 09:49:01 +000091 if (LHSVT.isFloatingPoint() != RHSVT.isFloatingPoint())
92 return RHSVT.isFloatingPoint();
Jim Grosbach9d401932011-03-01 01:39:05 +000093
Chris Lattner48e86db2010-03-29 01:40:38 +000094 // 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.
Tom Stellardf3b62df2014-08-01 00:32:36 +000097 int LHSSize = LHS->getPatternComplexity(CGP);
98 int RHSSize = RHS->getPatternComplexity(CGP);
Chris Lattner99ce6e82010-02-21 19:22:06 +000099 if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost
100 if (LHSSize < RHSSize) return false;
Jim Grosbach9d401932011-03-01 01:39:05 +0000101
Chris Lattner99ce6e82010-02-21 19:22:06 +0000102 // If the patterns have equal complexity, compare generated instruction cost
Florian Hahn74dff3b2018-06-14 20:32:58 +0000103 unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP);
104 unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP);
Chris Lattner99ce6e82010-02-21 19:22:06 +0000105 if (LHSCost < RHSCost) return true;
106 if (LHSCost > RHSCost) return false;
Jim Grosbach9d401932011-03-01 01:39:05 +0000107
Florian Hahn74dff3b2018-06-14 20:32:58 +0000108 unsigned LHSPatSize = getResultPatternSize(LHS->getDstPattern(), CGP);
109 unsigned RHSPatSize = getResultPatternSize(RHS->getDstPattern(), CGP);
Chris Lattner117ccb72010-03-01 22:09:11 +0000110 if (LHSPatSize < RHSPatSize) return true;
111 if (LHSPatSize > RHSPatSize) return false;
Jim Grosbach9d401932011-03-01 01:39:05 +0000112
Ulrich Weigand3a904262018-07-13 13:18:00 +0000113 // Sort based on the UID of the pattern, to reflect source order.
114 // Note that this is not guaranteed to be unique, since a single source
115 // pattern may have been resolved into multiple match patterns due to
116 // alternative fragments. To ensure deterministic output, always use
117 // std::stable_sort with this predicate.
Chris Lattner117ccb72010-03-01 22:09:11 +0000118 return LHS->ID < RHS->ID;
Chris Lattner99ce6e82010-02-21 19:22:06 +0000119 }
120};
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000121} // End anonymous namespace
Chris Lattner99ce6e82010-02-21 19:22:06 +0000122
123
Daniel Dunbar1a551802009-07-03 00:10:29 +0000124void DAGISelEmitter::run(raw_ostream &OS) {
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000125 emitSourceFileHeader("DAG Instruction Selector for the " +
Matthias Braun0c517c82016-12-04 05:48:16 +0000126 CGP.getTargetInfo().getName().str() + " target", OS);
Jim Grosbach9d401932011-03-01 01:39:05 +0000127
Chris Lattner1f39e292005-09-14 00:09:24 +0000128 OS << "// *** NOTE: This file is #included into the middle of the target\n"
129 << "// *** instruction selector class. These functions are really "
130 << "methods.\n\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000131
Krzysztof Parzyszek99119012017-11-10 18:36:04 +0000132 OS << "// If GET_DAGISEL_DECL is #defined with any value, only function\n"
133 "// declarations will be included when this file is included.\n"
134 "// If GET_DAGISEL_BODY is #defined, its value should be the name of\n"
135 "// the instruction selector class. Function bodies will be emitted\n"
136 "// and each function's name will be qualified with the name of the\n"
137 "// class.\n"
138 "//\n"
139 "// When neither of the GET_DAGISEL* macros is defined, the functions\n"
140 "// are emitted inline.\n\n";
141
Nicola Zaghen0818e782018-05-14 12:53:11 +0000142 LLVM_DEBUG(errs() << "\n\nALL PATTERNS TO MATCH:\n\n";
143 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
144 E = CGP.ptm_end();
145 I != E; ++I) {
146 errs() << "PATTERN: ";
147 I->getSrcPattern()->dump();
148 errs() << "\nRESULT: ";
149 I->getDstPattern()->dump();
150 errs() << "\n";
151 });
Chris Lattner4d0c9312010-03-01 01:54:19 +0000152
Chris Lattner99ce6e82010-02-21 19:22:06 +0000153 // Add all the patterns to a temporary list so we can sort them.
154 std::vector<const PatternToMatch*> Patterns;
155 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end();
156 I != E; ++I)
157 Patterns.push_back(&*I);
158
159 // We want to process the matches in order of minimal cost. Sort the patterns
160 // so the least cost one is at the start.
Ulrich Weigand3a904262018-07-13 13:18:00 +0000161 std::stable_sort(Patterns.begin(), Patterns.end(),
162 PatternSortingPredicate(CGP));
Jim Grosbach9d401932011-03-01 01:39:05 +0000163
164
Chris Lattnerfa342fa2010-03-01 07:17:40 +0000165 // Convert each variant of each pattern into a Matcher.
Chris Lattnerd6c84722010-02-25 19:00:39 +0000166 std::vector<Matcher*> PatternMatchers;
Chris Lattnerfa342fa2010-03-01 07:17:40 +0000167 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
168 for (unsigned Variant = 0; ; ++Variant) {
169 if (Matcher *M = ConvertPatternToMatcher(*Patterns[i], Variant, CGP))
170 PatternMatchers.push_back(M);
171 else
172 break;
173 }
174 }
Jim Grosbach9d401932011-03-01 01:39:05 +0000175
Craig Topper55c9dbf2014-12-15 00:40:07 +0000176 std::unique_ptr<Matcher> TheMatcher =
177 llvm::make_unique<ScopeMatcher>(PatternMatchers);
Chris Lattner05446e72010-02-16 23:13:59 +0000178
Craig Topper55c9dbf2014-12-15 00:40:07 +0000179 OptimizeMatcher(TheMatcher, CGP);
Chris Lattnerda272d12010-02-15 08:04:42 +0000180 //Matcher->dump();
Craig Topper55c9dbf2014-12-15 00:40:07 +0000181 EmitMatcherTable(TheMatcher.get(), CGP, OS);
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000182}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000183
184namespace llvm {
185
186void EmitDAGISel(RecordKeeper &RK, raw_ostream &OS) {
187 DAGISelEmitter(RK).run(OS);
188}
189
190} // End llvm namespace