blob: ae5953dd1a6cbd8929b211e8ca2f4c2671698b22 [file] [log] [blame]
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001//===- GlobalISelEmitter.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/// \file
11/// This tablegen backend emits code for use by the GlobalISel instruction
12/// selector. See include/llvm/CodeGen/TargetGlobalISel.td.
13///
14/// This file analyzes the patterns recognized by the SelectionDAGISel tablegen
15/// backend, filters out the ones that are unsupported, maps
16/// SelectionDAG-specific constructs to their GlobalISel counterpart
17/// (when applicable: MVT to LLT; SDNode to generic Instruction).
18///
19/// Not all patterns are supported: pass the tablegen invocation
20/// "-warn-on-skipped-patterns" to emit a warning when a pattern is skipped,
21/// as well as why.
22///
23/// The generated file defines a single method:
24/// bool <Target>InstructionSelector::selectImpl(MachineInstr &I) const;
25/// intended to be used in InstructionSelector::select as the first-step
26/// selector for the patterns that don't require complex C++.
27///
28/// FIXME: We'll probably want to eventually define a base
29/// "TargetGenInstructionSelector" class.
30///
31//===----------------------------------------------------------------------===//
32
33#include "CodeGenDAGPatterns.h"
34#include "llvm/ADT/Optional.h"
35#include "llvm/ADT/Statistic.h"
36#include "llvm/CodeGen/MachineValueType.h"
37#include "llvm/Support/CommandLine.h"
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +000038#include "llvm/Support/Error.h"
Daniel Sanders52b4ce72017-03-07 23:20:35 +000039#include "llvm/Support/LowLevelTypeImpl.h"
Pavel Labath52a82e22017-02-21 09:19:41 +000040#include "llvm/Support/ScopedPrinter.h"
Ahmed Bougacha36f70352016-12-21 23:26:20 +000041#include "llvm/TableGen/Error.h"
42#include "llvm/TableGen/Record.h"
43#include "llvm/TableGen/TableGenBackend.h"
44#include <string>
Daniel Sanders8a4bae92017-03-14 21:32:08 +000045#include <numeric>
Ahmed Bougacha36f70352016-12-21 23:26:20 +000046using namespace llvm;
47
48#define DEBUG_TYPE "gisel-emitter"
49
50STATISTIC(NumPatternTotal, "Total number of patterns");
Daniel Sandersb41ce2b2017-02-20 14:31:27 +000051STATISTIC(NumPatternImported, "Number of patterns imported from SelectionDAG");
52STATISTIC(NumPatternImportsSkipped, "Number of SelectionDAG imports skipped");
Ahmed Bougacha36f70352016-12-21 23:26:20 +000053STATISTIC(NumPatternEmitted, "Number of patterns emitted");
54
Daniel Sanders0848b232017-03-27 13:15:13 +000055cl::OptionCategory GlobalISelEmitterCat("Options for -gen-global-isel");
56
Ahmed Bougacha36f70352016-12-21 23:26:20 +000057static cl::opt<bool> WarnOnSkippedPatterns(
58 "warn-on-skipped-patterns",
59 cl::desc("Explain why a pattern was skipped for inclusion "
60 "in the GlobalISel selector"),
Daniel Sanders0848b232017-03-27 13:15:13 +000061 cl::init(false), cl::cat(GlobalISelEmitterCat));
Ahmed Bougacha36f70352016-12-21 23:26:20 +000062
Daniel Sandersbdfebb82017-03-15 20:18:38 +000063namespace {
Ahmed Bougacha36f70352016-12-21 23:26:20 +000064//===- Helper functions ---------------------------------------------------===//
65
Daniel Sanders52b4ce72017-03-07 23:20:35 +000066/// This class stands in for LLT wherever we want to tablegen-erate an
67/// equivalent at compiler run-time.
68class LLTCodeGen {
69private:
70 LLT Ty;
71
72public:
73 LLTCodeGen(const LLT &Ty) : Ty(Ty) {}
74
75 void emitCxxConstructorCall(raw_ostream &OS) const {
76 if (Ty.isScalar()) {
77 OS << "LLT::scalar(" << Ty.getSizeInBits() << ")";
78 return;
79 }
80 if (Ty.isVector()) {
81 OS << "LLT::vector(" << Ty.getNumElements() << ", " << Ty.getSizeInBits()
82 << ")";
83 return;
84 }
85 llvm_unreachable("Unhandled LLT");
86 }
Daniel Sanders8a4bae92017-03-14 21:32:08 +000087
88 const LLT &get() const { return Ty; }
89};
90
91class InstructionMatcher;
92class OperandPlaceholder {
93private:
94 enum PlaceholderKind {
95 OP_MatchReference,
96 OP_Temporary,
97 } Kind;
98
99 struct MatchReferenceData {
100 InstructionMatcher *InsnMatcher;
101 StringRef InsnVarName;
102 StringRef SymbolicName;
103 };
104
105 struct TemporaryData {
106 unsigned OpIdx;
107 };
108
109 union {
110 struct MatchReferenceData MatchReference;
111 struct TemporaryData Temporary;
112 };
113
114 OperandPlaceholder(PlaceholderKind Kind) : Kind(Kind) {}
115
116public:
117 ~OperandPlaceholder() {}
118
119 static OperandPlaceholder
120 CreateMatchReference(InstructionMatcher *InsnMatcher,
121 const StringRef InsnVarName, const StringRef SymbolicName) {
122 OperandPlaceholder Result(OP_MatchReference);
123 Result.MatchReference.InsnMatcher = InsnMatcher;
124 Result.MatchReference.InsnVarName = InsnVarName;
125 Result.MatchReference.SymbolicName = SymbolicName;
126 return Result;
127 }
128
129 static OperandPlaceholder CreateTemporary(unsigned OpIdx) {
130 OperandPlaceholder Result(OP_Temporary);
131 Result.Temporary.OpIdx = OpIdx;
132 return Result;
133 }
134
135 void emitCxxValueExpr(raw_ostream &OS) const;
Daniel Sanders52b4ce72017-03-07 23:20:35 +0000136};
137
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000138/// Convert an MVT to an equivalent LLT if possible, or the invalid LLT() for
139/// MVTs that don't map cleanly to an LLT (e.g., iPTR, *any, ...).
Daniel Sanders52b4ce72017-03-07 23:20:35 +0000140static Optional<LLTCodeGen> MVTToLLT(MVT::SimpleValueType SVT) {
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000141 MVT VT(SVT);
Daniel Sanders52b4ce72017-03-07 23:20:35 +0000142 if (VT.isVector() && VT.getVectorNumElements() != 1)
143 return LLTCodeGen(LLT::vector(VT.getVectorNumElements(), VT.getScalarSizeInBits()));
144 if (VT.isInteger() || VT.isFloatingPoint())
145 return LLTCodeGen(LLT::scalar(VT.getSizeInBits()));
146 return None;
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000147}
148
149static bool isTrivialOperatorNode(const TreePatternNode *N) {
150 return !N->isLeaf() && !N->hasAnyPredicate() && !N->getTransformFn();
151}
152
153//===- Matchers -----------------------------------------------------------===//
154
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000155class MatchAction;
156
157/// Generates code to check that a match rule matches.
158class RuleMatcher {
159 /// A list of matchers that all need to succeed for the current rule to match.
160 /// FIXME: This currently supports a single match position but could be
161 /// extended to support multiple positions to support div/rem fusion or
162 /// load-multiple instructions.
163 std::vector<std::unique_ptr<InstructionMatcher>> Matchers;
164
165 /// A list of actions that need to be taken when all predicates in this rule
166 /// have succeeded.
167 std::vector<std::unique_ptr<MatchAction>> Actions;
168
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000169 /// A map of instruction matchers to the local variables created by
170 /// emitCxxCaptureStmts().
171 std::map<const InstructionMatcher *, std::string> InsnVariableNames;
172
173 /// ID for the next instruction variable defined with defineInsnVar()
174 unsigned NextInsnVarID;
175
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000176public:
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000177 RuleMatcher()
178 : Matchers(), Actions(), InsnVariableNames(), NextInsnVarID(0) {}
Zachary Turnerb7dbd872017-03-20 19:56:52 +0000179 RuleMatcher(RuleMatcher &&Other) = default;
180 RuleMatcher &operator=(RuleMatcher &&Other) = default;
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000181
182 InstructionMatcher &addInstructionMatcher();
183
184 template <class Kind, class... Args> Kind &addAction(Args &&... args);
185
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000186 std::string defineInsnVar(raw_ostream &OS, const InstructionMatcher &Matcher,
187 StringRef Value);
188 StringRef getInsnVarName(const InstructionMatcher &InsnMatcher) const;
189
190 void emitCxxCaptureStmts(raw_ostream &OS, StringRef Expr);
191
192 void emit(raw_ostream &OS);
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000193
194 /// Compare the priority of this object and B.
195 ///
196 /// Returns true if this object is more important than B.
197 bool isHigherPriorityThan(const RuleMatcher &B) const;
198
199 /// Report the maximum number of temporary operands needed by the rule
200 /// matcher.
201 unsigned countTemporaryOperands() const;
202};
203
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000204template <class PredicateTy> class PredicateListMatcher {
205private:
206 typedef std::vector<std::unique_ptr<PredicateTy>> PredicateVec;
207 PredicateVec Predicates;
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000208
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000209public:
210 /// Construct a new operand predicate and add it to the matcher.
211 template <class Kind, class... Args>
212 Kind &addPredicate(Args&&... args) {
Ahmed Bougachab67a3ce2017-01-26 22:07:37 +0000213 Predicates.emplace_back(
214 llvm::make_unique<Kind>(std::forward<Args>(args)...));
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000215 return *static_cast<Kind *>(Predicates.back().get());
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000216 }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000217
218 typename PredicateVec::const_iterator predicates_begin() const { return Predicates.begin(); }
219 typename PredicateVec::const_iterator predicates_end() const { return Predicates.end(); }
220 iterator_range<typename PredicateVec::const_iterator> predicates() const {
221 return make_range(predicates_begin(), predicates_end());
222 }
Daniel Sanders759ff412017-02-24 13:58:11 +0000223 typename PredicateVec::size_type predicates_size() const { return Predicates.size(); }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000224
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000225 /// Emit a C++ expression that tests whether all the predicates are met.
Ahmed Bougachab67a3ce2017-01-26 22:07:37 +0000226 template <class... Args>
Daniel Sandersf8c804f2017-01-28 11:10:42 +0000227 void emitCxxPredicateListExpr(raw_ostream &OS, Args &&... args) const {
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000228 if (Predicates.empty()) {
229 OS << "true";
230 return;
231 }
232
233 StringRef Separator = "";
234 for (const auto &Predicate : predicates()) {
235 OS << Separator << "(";
Ahmed Bougachab67a3ce2017-01-26 22:07:37 +0000236 Predicate->emitCxxPredicateExpr(OS, std::forward<Args>(args)...);
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000237 OS << ")";
Ahmed Bougacha905af9f2017-02-04 00:47:02 +0000238 Separator = " &&\n";
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000239 }
240 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000241};
242
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000243/// Generates code to check a predicate of an operand.
244///
245/// Typical predicates include:
246/// * Operand is a particular register.
247/// * Operand is assigned a particular register bank.
248/// * Operand is an MBB.
249class OperandPredicateMatcher {
250public:
Daniel Sanders759ff412017-02-24 13:58:11 +0000251 /// This enum is used for RTTI and also defines the priority that is given to
252 /// the predicate when generating the matcher code. Kinds with higher priority
253 /// must be tested first.
254 ///
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000255 /// The relative priority of OPM_LLT, OPM_RegBank, and OPM_MBB do not matter
256 /// but OPM_Int must have priority over OPM_RegBank since constant integers
257 /// are represented by a virtual register defined by a G_CONSTANT instruction.
Daniel Sanders759ff412017-02-24 13:58:11 +0000258 enum PredicateKind {
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000259 OPM_ComplexPattern,
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000260 OPM_Int,
Daniel Sanders759ff412017-02-24 13:58:11 +0000261 OPM_LLT,
262 OPM_RegBank,
263 OPM_MBB,
264 };
265
266protected:
267 PredicateKind Kind;
268
269public:
270 OperandPredicateMatcher(PredicateKind Kind) : Kind(Kind) {}
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000271 virtual ~OperandPredicateMatcher() {}
272
Daniel Sanders759ff412017-02-24 13:58:11 +0000273 PredicateKind getKind() const { return Kind; }
274
Daniel Sanderse604ef52017-02-20 15:30:43 +0000275 /// Emit a C++ expression that checks the predicate for the given operand.
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000276 virtual void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanderse604ef52017-02-20 15:30:43 +0000277 StringRef OperandExpr) const = 0;
Daniel Sanders759ff412017-02-24 13:58:11 +0000278
279 /// Compare the priority of this object and B.
280 ///
281 /// Returns true if this object is more important than B.
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000282 virtual bool isHigherPriorityThan(const OperandPredicateMatcher &B) const {
283 return Kind < B.Kind;
Daniel Sanders759ff412017-02-24 13:58:11 +0000284 };
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000285
286 /// Report the maximum number of temporary operands needed by the predicate
287 /// matcher.
288 virtual unsigned countTemporaryOperands() const { return 0; }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000289};
290
291/// Generates code to check that an operand is a particular LLT.
292class LLTOperandMatcher : public OperandPredicateMatcher {
293protected:
Daniel Sanders52b4ce72017-03-07 23:20:35 +0000294 LLTCodeGen Ty;
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000295
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000296public:
Daniel Sanders52b4ce72017-03-07 23:20:35 +0000297 LLTOperandMatcher(const LLTCodeGen &Ty)
Daniel Sanders759ff412017-02-24 13:58:11 +0000298 : OperandPredicateMatcher(OPM_LLT), Ty(Ty) {}
299
300 static bool classof(const OperandPredicateMatcher *P) {
301 return P->getKind() == OPM_LLT;
302 }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000303
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000304 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanderse604ef52017-02-20 15:30:43 +0000305 StringRef OperandExpr) const override {
Daniel Sanders52b4ce72017-03-07 23:20:35 +0000306 OS << "MRI.getType(" << OperandExpr << ".getReg()) == (";
307 Ty.emitCxxConstructorCall(OS);
308 OS << ")";
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000309 }
310};
311
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000312/// Generates code to check that an operand is a particular target constant.
313class ComplexPatternOperandMatcher : public OperandPredicateMatcher {
314protected:
315 const Record &TheDef;
316 /// The index of the first temporary operand to allocate to this
317 /// ComplexPattern.
318 unsigned BaseTemporaryID;
319
320 unsigned getNumOperands() const {
321 return TheDef.getValueAsDag("Operands")->getNumArgs();
322 }
323
324public:
325 ComplexPatternOperandMatcher(const Record &TheDef, unsigned BaseTemporaryID)
326 : OperandPredicateMatcher(OPM_ComplexPattern), TheDef(TheDef),
327 BaseTemporaryID(BaseTemporaryID) {}
328
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000329 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000330 StringRef OperandExpr) const override {
331 OS << TheDef.getValueAsString("MatcherFn") << "(" << OperandExpr;
332 for (unsigned I = 0; I < getNumOperands(); ++I) {
333 OS << ", ";
334 OperandPlaceholder::CreateTemporary(BaseTemporaryID + I)
335 .emitCxxValueExpr(OS);
336 }
337 OS << ")";
338 }
339
340 unsigned countTemporaryOperands() const override {
341 return getNumOperands();
342 }
343};
344
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000345/// Generates code to check that an operand is in a particular register bank.
346class RegisterBankOperandMatcher : public OperandPredicateMatcher {
347protected:
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000348 const CodeGenRegisterClass &RC;
349
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000350public:
Daniel Sanders759ff412017-02-24 13:58:11 +0000351 RegisterBankOperandMatcher(const CodeGenRegisterClass &RC)
352 : OperandPredicateMatcher(OPM_RegBank), RC(RC) {}
353
354 static bool classof(const OperandPredicateMatcher *P) {
355 return P->getKind() == OPM_RegBank;
356 }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000357
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000358 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanderse604ef52017-02-20 15:30:43 +0000359 StringRef OperandExpr) const override {
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000360 OS << "(&RBI.getRegBankFromRegClass(" << RC.getQualifiedName()
Daniel Sanderse604ef52017-02-20 15:30:43 +0000361 << "RegClass) == RBI.getRegBank(" << OperandExpr
362 << ".getReg(), MRI, TRI))";
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000363 }
364};
365
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000366/// Generates code to check that an operand is a basic block.
367class MBBOperandMatcher : public OperandPredicateMatcher {
368public:
Daniel Sanders759ff412017-02-24 13:58:11 +0000369 MBBOperandMatcher() : OperandPredicateMatcher(OPM_MBB) {}
370
371 static bool classof(const OperandPredicateMatcher *P) {
372 return P->getKind() == OPM_MBB;
373 }
374
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000375 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanderse604ef52017-02-20 15:30:43 +0000376 StringRef OperandExpr) const override {
377 OS << OperandExpr << ".isMBB()";
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000378 }
379};
380
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000381/// Generates code to check that an operand is a particular int.
382class IntOperandMatcher : public OperandPredicateMatcher {
383protected:
384 int64_t Value;
385
386public:
387 IntOperandMatcher(int64_t Value)
388 : OperandPredicateMatcher(OPM_Int), Value(Value) {}
389
390 static bool classof(const OperandPredicateMatcher *P) {
391 return P->getKind() == OPM_Int;
392 }
393
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000394 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Simon Pilgrimd0302912017-02-24 17:20:27 +0000395 StringRef OperandExpr) const override {
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000396 OS << "isOperandImmEqual(" << OperandExpr << ", " << Value << ", MRI)";
397 }
398};
399
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000400/// Generates code to check that a set of predicates match for a particular
401/// operand.
402class OperandMatcher : public PredicateListMatcher<OperandPredicateMatcher> {
403protected:
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000404 InstructionMatcher &Insn;
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000405 unsigned OpIdx;
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000406 std::string SymbolicName;
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000407
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000408public:
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000409 OperandMatcher(InstructionMatcher &Insn, unsigned OpIdx,
410 const std::string &SymbolicName)
411 : Insn(Insn), OpIdx(OpIdx), SymbolicName(SymbolicName) {}
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000412
413 bool hasSymbolicName() const { return !SymbolicName.empty(); }
414 const StringRef getSymbolicName() const { return SymbolicName; }
Daniel Sandersffc7d582017-03-29 15:37:18 +0000415 void setSymbolicName(StringRef Name) {
416 assert(SymbolicName.empty() && "Operand already has a symbolic name");
417 SymbolicName = Name;
418 }
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000419 unsigned getOperandIndex() const { return OpIdx; }
420
421 std::string getOperandExpr(const StringRef InsnVarName) const {
Pavel Labath52a82e22017-02-21 09:19:41 +0000422 return (InsnVarName + ".getOperand(" + llvm::to_string(OpIdx) + ")").str();
Daniel Sanderse604ef52017-02-20 15:30:43 +0000423 }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000424
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000425 InstructionMatcher &getInstructionMatcher() const { return Insn; }
426
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000427 /// Emit a C++ expression that tests whether the instruction named in
428 /// InsnVarName matches all the predicate and all the operands.
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000429 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
430 const StringRef InsnVarName) const {
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000431 OS << "(/* ";
432 if (SymbolicName.empty())
433 OS << "Operand " << OpIdx;
434 else
435 OS << SymbolicName;
436 OS << " */ ";
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000437 emitCxxPredicateListExpr(OS, Rule, getOperandExpr(InsnVarName));
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000438 OS << ")";
439 }
Daniel Sanders759ff412017-02-24 13:58:11 +0000440
441 /// Compare the priority of this object and B.
442 ///
443 /// Returns true if this object is more important than B.
444 bool isHigherPriorityThan(const OperandMatcher &B) const {
445 // Operand matchers involving more predicates have higher priority.
446 if (predicates_size() > B.predicates_size())
447 return true;
448 if (predicates_size() < B.predicates_size())
449 return false;
450
451 // This assumes that predicates are added in a consistent order.
452 for (const auto &Predicate : zip(predicates(), B.predicates())) {
453 if (std::get<0>(Predicate)->isHigherPriorityThan(*std::get<1>(Predicate)))
454 return true;
455 if (std::get<1>(Predicate)->isHigherPriorityThan(*std::get<0>(Predicate)))
456 return false;
457 }
458
459 return false;
460 };
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000461
462 /// Report the maximum number of temporary operands needed by the operand
463 /// matcher.
464 unsigned countTemporaryOperands() const {
465 return std::accumulate(
466 predicates().begin(), predicates().end(), 0,
467 [](unsigned A,
468 const std::unique_ptr<OperandPredicateMatcher> &Predicate) {
469 return A + Predicate->countTemporaryOperands();
470 });
471 }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000472};
473
474/// Generates code to check a predicate on an instruction.
475///
476/// Typical predicates include:
477/// * The opcode of the instruction is a particular value.
478/// * The nsw/nuw flag is/isn't set.
479class InstructionPredicateMatcher {
Daniel Sanders759ff412017-02-24 13:58:11 +0000480protected:
481 /// This enum is used for RTTI and also defines the priority that is given to
482 /// the predicate when generating the matcher code. Kinds with higher priority
483 /// must be tested first.
484 enum PredicateKind {
485 IPM_Opcode,
486 };
487
488 PredicateKind Kind;
489
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000490public:
Daniel Sanders8d4d72f2017-02-24 14:53:35 +0000491 InstructionPredicateMatcher(PredicateKind Kind) : Kind(Kind) {}
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000492 virtual ~InstructionPredicateMatcher() {}
493
Daniel Sanders759ff412017-02-24 13:58:11 +0000494 PredicateKind getKind() const { return Kind; }
495
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000496 /// Emit a C++ expression that tests whether the instruction named in
497 /// InsnVarName matches the predicate.
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000498 virtual void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Ahmed Bougacha6a1ac5a2017-02-09 02:50:01 +0000499 StringRef InsnVarName) const = 0;
Daniel Sanders759ff412017-02-24 13:58:11 +0000500
501 /// Compare the priority of this object and B.
502 ///
503 /// Returns true if this object is more important than B.
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000504 virtual bool isHigherPriorityThan(const InstructionPredicateMatcher &B) const {
Daniel Sanders759ff412017-02-24 13:58:11 +0000505 return Kind < B.Kind;
506 };
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000507
508 /// Report the maximum number of temporary operands needed by the predicate
509 /// matcher.
510 virtual unsigned countTemporaryOperands() const { return 0; }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000511};
512
513/// Generates code to check the opcode of an instruction.
514class InstructionOpcodeMatcher : public InstructionPredicateMatcher {
515protected:
516 const CodeGenInstruction *I;
517
518public:
Daniel Sanders8d4d72f2017-02-24 14:53:35 +0000519 InstructionOpcodeMatcher(const CodeGenInstruction *I)
520 : InstructionPredicateMatcher(IPM_Opcode), I(I) {}
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000521
Daniel Sanders759ff412017-02-24 13:58:11 +0000522 static bool classof(const InstructionPredicateMatcher *P) {
523 return P->getKind() == IPM_Opcode;
524 }
525
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000526 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Ahmed Bougacha6a1ac5a2017-02-09 02:50:01 +0000527 StringRef InsnVarName) const override {
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000528 OS << InsnVarName << ".getOpcode() == " << I->Namespace
529 << "::" << I->TheDef->getName();
530 }
Daniel Sanders759ff412017-02-24 13:58:11 +0000531
532 /// Compare the priority of this object and B.
533 ///
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000534 /// Returns true if this object is more important than B.
535 bool isHigherPriorityThan(const InstructionPredicateMatcher &B) const override {
Daniel Sanders759ff412017-02-24 13:58:11 +0000536 if (InstructionPredicateMatcher::isHigherPriorityThan(B))
537 return true;
538 if (B.InstructionPredicateMatcher::isHigherPriorityThan(*this))
539 return false;
540
541 // Prioritize opcodes for cosmetic reasons in the generated source. Although
542 // this is cosmetic at the moment, we may want to drive a similar ordering
543 // using instruction frequency information to improve compile time.
544 if (const InstructionOpcodeMatcher *BO =
545 dyn_cast<InstructionOpcodeMatcher>(&B))
546 return I->TheDef->getName() < BO->I->TheDef->getName();
547
548 return false;
549 };
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000550};
551
552/// Generates code to check that a set of predicates and operands match for a
553/// particular instruction.
554///
555/// Typical predicates include:
556/// * Has a specific opcode.
557/// * Has an nsw/nuw flag or doesn't.
558class InstructionMatcher
559 : public PredicateListMatcher<InstructionPredicateMatcher> {
560protected:
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000561 typedef std::vector<OperandMatcher> OperandVec;
562
563 /// The operands to match. All rendered operands must be present even if the
564 /// condition is always true.
565 OperandVec Operands;
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000566
567public:
568 /// Add an operand to the matcher.
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000569 OperandMatcher &addOperand(unsigned OpIdx, const std::string &SymbolicName) {
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000570 Operands.emplace_back(*this, OpIdx, SymbolicName);
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000571 return Operands.back();
572 }
573
Daniel Sandersffc7d582017-03-29 15:37:18 +0000574 OperandMatcher &getOperand(unsigned OpIdx) {
575 auto I = std::find_if(Operands.begin(), Operands.end(),
576 [&OpIdx](const OperandMatcher &X) {
577 return X.getOperandIndex() == OpIdx;
578 });
579 if (I != Operands.end())
580 return *I;
581 llvm_unreachable("Failed to lookup operand");
582 }
583
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000584 Optional<const OperandMatcher *> getOptionalOperand(StringRef SymbolicName) const {
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000585 assert(!SymbolicName.empty() && "Cannot lookup unnamed operand");
586 const auto &I = std::find_if(Operands.begin(), Operands.end(),
587 [&SymbolicName](const OperandMatcher &X) {
588 return X.getSymbolicName() == SymbolicName;
589 });
590 if (I != Operands.end())
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000591 return &*I;
592 return None;
593 }
594
595 const OperandMatcher &getOperand(const StringRef SymbolicName) const {
596 Optional<const OperandMatcher *>OM = getOptionalOperand(SymbolicName);
597 if (OM.hasValue())
598 return *OM.getValue();
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000599 llvm_unreachable("Failed to lookup operand");
600 }
601
602 unsigned getNumOperands() const { return Operands.size(); }
Daniel Sandersffc7d582017-03-29 15:37:18 +0000603 OperandVec::const_iterator operands_begin() const { return Operands.begin(); }
604 OperandVec::const_iterator operands_end() const { return Operands.end(); }
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000605 iterator_range<OperandVec::const_iterator> operands() const {
606 return make_range(operands_begin(), operands_end());
607 }
608
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000609 /// Emit C++ statements to check the shape of the match and capture
610 /// instructions into local variables.
611 ///
612 /// TODO: When nested instruction matching is implemented, this function will
613 /// descend into the operands and capture variables.
614 void emitCxxCaptureStmts(raw_ostream &OS, RuleMatcher &Rule, StringRef Expr) {
615 OS << "if (" << Expr << ".getNumOperands() < " << getNumOperands() << ")\n"
616 << " return false;\n";
617 }
618
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000619 /// Emit a C++ expression that tests whether the instruction named in
620 /// InsnVarName matches all the predicates and all the operands.
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000621 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
622 StringRef InsnVarName) const {
623 emitCxxPredicateListExpr(OS, Rule, InsnVarName);
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000624 for (const auto &Operand : Operands) {
Ahmed Bougacha905af9f2017-02-04 00:47:02 +0000625 OS << " &&\n(";
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000626 Operand.emitCxxPredicateExpr(OS, Rule, InsnVarName);
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000627 OS << ")";
628 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000629 }
Daniel Sanders759ff412017-02-24 13:58:11 +0000630
631 /// Compare the priority of this object and B.
632 ///
633 /// Returns true if this object is more important than B.
634 bool isHigherPriorityThan(const InstructionMatcher &B) const {
635 // Instruction matchers involving more operands have higher priority.
636 if (Operands.size() > B.Operands.size())
637 return true;
638 if (Operands.size() < B.Operands.size())
639 return false;
640
641 for (const auto &Predicate : zip(predicates(), B.predicates())) {
642 if (std::get<0>(Predicate)->isHigherPriorityThan(*std::get<1>(Predicate)))
643 return true;
644 if (std::get<1>(Predicate)->isHigherPriorityThan(*std::get<0>(Predicate)))
645 return false;
646 }
647
648 for (const auto &Operand : zip(Operands, B.Operands)) {
649 if (std::get<0>(Operand).isHigherPriorityThan(std::get<1>(Operand)))
650 return true;
651 if (std::get<1>(Operand).isHigherPriorityThan(std::get<0>(Operand)))
652 return false;
653 }
654
655 return false;
656 };
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000657
658 /// Report the maximum number of temporary operands needed by the instruction
659 /// matcher.
660 unsigned countTemporaryOperands() const {
661 return std::accumulate(predicates().begin(), predicates().end(), 0,
662 [](unsigned A,
663 const std::unique_ptr<InstructionPredicateMatcher>
664 &Predicate) {
665 return A + Predicate->countTemporaryOperands();
666 }) +
667 std::accumulate(Operands.begin(), Operands.end(), 0,
668 [](unsigned A, const OperandMatcher &Operand) {
669 return A + Operand.countTemporaryOperands();
670 });
671 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000672};
673
Daniel Sanders43c882c2017-02-01 10:53:10 +0000674//===- Actions ------------------------------------------------------------===//
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000675void OperandPlaceholder::emitCxxValueExpr(raw_ostream &OS) const {
676 switch (Kind) {
677 case OP_MatchReference:
678 OS << MatchReference.InsnMatcher->getOperand(MatchReference.SymbolicName)
679 .getOperandExpr(MatchReference.InsnVarName);
680 break;
681 case OP_Temporary:
682 OS << "TempOp" << Temporary.OpIdx;
683 break;
684 }
685}
Daniel Sanders43c882c2017-02-01 10:53:10 +0000686
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000687class OperandRenderer {
688public:
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000689 enum RendererKind { OR_Copy, OR_Register, OR_ComplexPattern };
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000690
691protected:
692 RendererKind Kind;
693
694public:
695 OperandRenderer(RendererKind Kind) : Kind(Kind) {}
696 virtual ~OperandRenderer() {}
697
698 RendererKind getKind() const { return Kind; }
699
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000700 virtual void emitCxxRenderStmts(raw_ostream &OS, RuleMatcher &Rule) const = 0;
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000701};
702
703/// A CopyRenderer emits code to copy a single operand from an existing
704/// instruction to the one being built.
705class CopyRenderer : public OperandRenderer {
706protected:
707 /// The matcher for the instruction that this operand is copied from.
708 /// This provides the facility for looking up an a operand by it's name so
709 /// that it can be used as a source for the instruction being built.
710 const InstructionMatcher &Matched;
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000711 /// The name of the operand.
712 const StringRef SymbolicName;
713
714public:
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000715 CopyRenderer(const InstructionMatcher &Matched, StringRef SymbolicName)
716 : OperandRenderer(OR_Copy), Matched(Matched), SymbolicName(SymbolicName) {
717 }
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000718
719 static bool classof(const OperandRenderer *R) {
720 return R->getKind() == OR_Copy;
721 }
722
723 const StringRef getSymbolicName() const { return SymbolicName; }
724
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000725 void emitCxxRenderStmts(raw_ostream &OS, RuleMatcher &Rule) const override {
726 const OperandMatcher &Operand = Matched.getOperand(SymbolicName);
727 StringRef InsnVarName =
728 Rule.getInsnVarName(Operand.getInstructionMatcher());
729 std::string OperandExpr = Operand.getOperandExpr(InsnVarName);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000730 OS << " MIB.add(" << OperandExpr << "/*" << SymbolicName << "*/);\n";
731 }
732};
733
734/// Adds a specific physical register to the instruction being built.
735/// This is typically useful for WZR/XZR on AArch64.
736class AddRegisterRenderer : public OperandRenderer {
737protected:
738 const Record *RegisterDef;
739
740public:
741 AddRegisterRenderer(const Record *RegisterDef)
742 : OperandRenderer(OR_Register), RegisterDef(RegisterDef) {}
743
744 static bool classof(const OperandRenderer *R) {
745 return R->getKind() == OR_Register;
746 }
747
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000748 void emitCxxRenderStmts(raw_ostream &OS, RuleMatcher &Rule) const override {
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000749 OS << " MIB.addReg(" << RegisterDef->getValueAsString("Namespace")
750 << "::" << RegisterDef->getName() << ");\n";
751 }
752};
753
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000754class RenderComplexPatternOperand : public OperandRenderer {
755private:
756 const Record &TheDef;
757 std::vector<OperandPlaceholder> Sources;
758
759 unsigned getNumOperands() const {
760 return TheDef.getValueAsDag("Operands")->getNumArgs();
761 }
762
763public:
764 RenderComplexPatternOperand(const Record &TheDef,
765 const ArrayRef<OperandPlaceholder> Sources)
766 : OperandRenderer(OR_ComplexPattern), TheDef(TheDef), Sources(Sources) {}
767
768 static bool classof(const OperandRenderer *R) {
769 return R->getKind() == OR_ComplexPattern;
770 }
771
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000772 void emitCxxRenderStmts(raw_ostream &OS, RuleMatcher &Rule) const override {
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000773 assert(Sources.size() == getNumOperands() && "Inconsistent number of operands");
774 for (const auto &Source : Sources) {
775 OS << "MIB.add(";
776 Source.emitCxxValueExpr(OS);
777 OS << ");\n";
778 }
779 }
780};
781
Ahmed Bougacha56ca3a92017-02-04 00:47:10 +0000782/// An action taken when all Matcher predicates succeeded for a parent rule.
783///
784/// Typical actions include:
785/// * Changing the opcode of an instruction.
786/// * Adding an operand to an instruction.
Daniel Sanders43c882c2017-02-01 10:53:10 +0000787class MatchAction {
788public:
789 virtual ~MatchAction() {}
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000790
791 /// Emit the C++ statements to implement the action.
792 ///
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000793 /// \param RecycleVarName If given, it's an instruction to recycle. The
794 /// requirements on the instruction vary from action to
795 /// action.
796 virtual void emitCxxActionStmts(raw_ostream &OS, RuleMatcher &Rule,
797 StringRef RecycleVarName) const = 0;
Daniel Sanders43c882c2017-02-01 10:53:10 +0000798};
799
Ahmed Bougacha9aa4c102017-02-04 00:47:08 +0000800/// Generates a comment describing the matched rule being acted upon.
801class DebugCommentAction : public MatchAction {
802private:
803 const PatternToMatch &P;
804
805public:
806 DebugCommentAction(const PatternToMatch &P) : P(P) {}
807
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000808 void emitCxxActionStmts(raw_ostream &OS, RuleMatcher &Rule,
809 StringRef RecycleVarName) const override {
810 OS << "// " << *P.getSrcPattern() << " => " << *P.getDstPattern() << "\n";
Ahmed Bougacha9aa4c102017-02-04 00:47:08 +0000811 }
812};
813
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000814/// Generates code to build an instruction or mutate an existing instruction
815/// into the desired instruction when this is possible.
816class BuildMIAction : public MatchAction {
Daniel Sanders43c882c2017-02-01 10:53:10 +0000817private:
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000818 const CodeGenInstruction *I;
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000819 const InstructionMatcher &Matched;
820 std::vector<std::unique_ptr<OperandRenderer>> OperandRenderers;
821
822 /// True if the instruction can be built solely by mutating the opcode.
823 bool canMutate() const {
824 for (const auto &Renderer : enumerate(OperandRenderers)) {
Zachary Turner309a0882017-03-13 16:24:10 +0000825 if (const auto *Copy = dyn_cast<CopyRenderer>(&*Renderer.value())) {
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000826 if (Matched.getOperand(Copy->getSymbolicName()).getOperandIndex() !=
Zachary Turner309a0882017-03-13 16:24:10 +0000827 Renderer.index())
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000828 return false;
829 } else
830 return false;
831 }
832
833 return true;
834 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000835
Daniel Sanders43c882c2017-02-01 10:53:10 +0000836public:
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000837 BuildMIAction(const CodeGenInstruction *I, const InstructionMatcher &Matched)
838 : I(I), Matched(Matched) {}
Daniel Sanders43c882c2017-02-01 10:53:10 +0000839
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000840 template <class Kind, class... Args>
841 Kind &addRenderer(Args&&... args) {
842 OperandRenderers.emplace_back(
843 llvm::make_unique<Kind>(std::forward<Args>(args)...));
844 return *static_cast<Kind *>(OperandRenderers.back().get());
845 }
846
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000847 void emitCxxActionStmts(raw_ostream &OS, RuleMatcher &Rule,
848 StringRef RecycleVarName) const override {
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000849 if (canMutate()) {
Tim Northover4340d642017-03-20 21:58:23 +0000850 OS << " " << RecycleVarName << ".setDesc(TII.get(" << I->Namespace
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000851 << "::" << I->TheDef->getName() << "));\n";
Tim Northover4340d642017-03-20 21:58:23 +0000852
853 if (!I->ImplicitDefs.empty() || !I->ImplicitUses.empty()) {
854 OS << " auto MIB = MachineInstrBuilder(MF, &" << RecycleVarName
855 << ");\n";
856
857 for (auto Def : I->ImplicitDefs) {
858 auto Namespace = Def->getValueAsString("Namespace");
859 OS << " MIB.addDef(" << Namespace << "::" << Def->getName()
860 << ", RegState::Implicit);\n";
861 }
862 for (auto Use : I->ImplicitUses) {
863 auto Namespace = Use->getValueAsString("Namespace");
864 OS << " MIB.addUse(" << Namespace << "::" << Use->getName()
865 << ", RegState::Implicit);\n";
866 }
867 }
868
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000869 OS << " MachineInstr &NewI = " << RecycleVarName << ";\n";
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000870 return;
871 }
872
873 // TODO: Simple permutation looks like it could be almost as common as
874 // mutation due to commutative operations.
875
876 OS << "MachineInstrBuilder MIB = BuildMI(*I.getParent(), I, "
877 "I.getDebugLoc(), TII.get("
878 << I->Namespace << "::" << I->TheDef->getName() << "));\n";
879 for (const auto &Renderer : OperandRenderers)
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000880 Renderer->emitCxxRenderStmts(OS, Rule);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000881 OS << " MIB.setMemRefs(I.memoperands_begin(), I.memoperands_end());\n";
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000882 OS << " " << RecycleVarName << ".eraseFromParent();\n";
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000883 OS << " MachineInstr &NewI = *MIB;\n";
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000884 }
885};
886
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000887InstructionMatcher &RuleMatcher::addInstructionMatcher() {
888 Matchers.emplace_back(new InstructionMatcher());
889 return *Matchers.back();
890}
Ahmed Bougacha56ca3a92017-02-04 00:47:10 +0000891
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000892template <class Kind, class... Args>
893Kind &RuleMatcher::addAction(Args &&... args) {
894 Actions.emplace_back(llvm::make_unique<Kind>(std::forward<Args>(args)...));
895 return *static_cast<Kind *>(Actions.back().get());
896}
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000897
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000898std::string RuleMatcher::defineInsnVar(raw_ostream &OS,
899 const InstructionMatcher &Matcher,
900 StringRef Value) {
901 std::string InsnVarName = "MI" + llvm::to_string(NextInsnVarID++);
902 OS << "MachineInstr &" << InsnVarName << " = " << Value << ";\n";
903 InsnVariableNames[&Matcher] = InsnVarName;
904 return InsnVarName;
905}
906
907StringRef RuleMatcher::getInsnVarName(const InstructionMatcher &InsnMatcher) const {
908 const auto &I = InsnVariableNames.find(&InsnMatcher);
909 if (I != InsnVariableNames.end())
910 return I->second;
911 llvm_unreachable("Matched Insn was not captured in a local variable");
912}
913
914/// Emit C++ statements to check the shape of the match and capture
915/// instructions into local variables.
916void RuleMatcher::emitCxxCaptureStmts(raw_ostream &OS, StringRef Expr) {
917 assert(Matchers.size() == 1 && "Cannot handle multi-root matchers yet");
918 std::string InsnVarName = defineInsnVar(OS, *Matchers.front(), Expr);
919 Matchers.front()->emitCxxCaptureStmts(OS, *this, InsnVarName);
920}
921
922void RuleMatcher::emit(raw_ostream &OS) {
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000923 if (Matchers.empty())
924 llvm_unreachable("Unexpected empty matcher!");
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000925
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000926 // The representation supports rules that require multiple roots such as:
927 // %ptr(p0) = ...
928 // %elt0(s32) = G_LOAD %ptr
929 // %1(p0) = G_ADD %ptr, 4
930 // %elt1(s32) = G_LOAD p0 %1
931 // which could be usefully folded into:
932 // %ptr(p0) = ...
933 // %elt0(s32), %elt1(s32) = TGT_LOAD_PAIR %ptr
934 // on some targets but we don't need to make use of that yet.
935 assert(Matchers.size() == 1 && "Cannot handle multi-root matchers yet");
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000936 OS << "if ([&]() {\n";
937
938 emitCxxCaptureStmts(OS, "I");
939
940 OS << " if (";
941 Matchers.front()->emitCxxPredicateExpr(OS, *this,
942 getInsnVarName(*Matchers.front()));
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000943 OS << ") {\n";
944
945 for (const auto &MA : Actions) {
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000946 MA->emitCxxActionStmts(OS, *this, "I");
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000947 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000948
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000949 OS << " constrainSelectedInstRegOperands(NewI, TII, TRI, RBI);\n";
950 OS << " return true;\n";
951 OS << " }\n";
952 OS << " return false;\n";
953 OS << " }()) { return true; }\n\n";
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000954}
Daniel Sanders43c882c2017-02-01 10:53:10 +0000955
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000956bool RuleMatcher::isHigherPriorityThan(const RuleMatcher &B) const {
957 // Rules involving more match roots have higher priority.
958 if (Matchers.size() > B.Matchers.size())
959 return true;
960 if (Matchers.size() < B.Matchers.size())
Daniel Sanders759ff412017-02-24 13:58:11 +0000961 return false;
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000962
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000963 for (const auto &Matcher : zip(Matchers, B.Matchers)) {
964 if (std::get<0>(Matcher)->isHigherPriorityThan(*std::get<1>(Matcher)))
965 return true;
966 if (std::get<1>(Matcher)->isHigherPriorityThan(*std::get<0>(Matcher)))
967 return false;
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000968 }
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000969
970 return false;
Simon Pilgrima7d1da82017-03-15 22:50:47 +0000971}
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000972
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000973unsigned RuleMatcher::countTemporaryOperands() const {
974 return std::accumulate(
975 Matchers.begin(), Matchers.end(), 0,
976 [](unsigned A, const std::unique_ptr<InstructionMatcher> &Matcher) {
977 return A + Matcher->countTemporaryOperands();
978 });
979}
980
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000981//===- GlobalISelEmitter class --------------------------------------------===//
982
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +0000983class GlobalISelEmitter {
984public:
985 explicit GlobalISelEmitter(RecordKeeper &RK);
986 void run(raw_ostream &OS);
987
988private:
989 const RecordKeeper &RK;
990 const CodeGenDAGPatterns CGP;
991 const CodeGenTarget &Target;
992
993 /// Keep track of the equivalence between SDNodes and Instruction.
994 /// This is defined using 'GINodeEquiv' in the target description.
995 DenseMap<Record *, const CodeGenInstruction *> NodeEquivs;
996
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000997 /// Keep track of the equivalence between ComplexPattern's and
998 /// GIComplexOperandMatcher. Map entries are specified by subclassing
999 /// GIComplexPatternEquiv.
1000 DenseMap<const Record *, const Record *> ComplexPatternEquivs;
1001
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001002 void gatherNodeEquivs();
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001003 const CodeGenInstruction *findNodeEquiv(Record *N) const;
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001004
Daniel Sandersffc7d582017-03-29 15:37:18 +00001005 Expected<bool> importRulePredicates(RuleMatcher &M,
1006 ArrayRef<Init *> Predicates) const;
1007 Expected<InstructionMatcher &>
1008 importSelDAGMatcher(InstructionMatcher &InsnMatcher,
1009 const TreePatternNode *Src) const;
1010 Expected<bool> importChildMatcher(InstructionMatcher &InsnMatcher,
1011 TreePatternNode *SrcChild, unsigned OpIdx,
1012 unsigned &TempOpIdx) const;
1013 Expected<BuildMIAction &>
1014 importInstructionRenderer(RuleMatcher &M, const TreePatternNode *Dst,
1015 const InstructionMatcher &InsnMatcher) const;
1016 Expected<bool> importExplicitUseRenderer(
1017 BuildMIAction &DstMIBuilder, TreePatternNode *DstChild,
1018 const InstructionMatcher &InsnMatcher, unsigned &TempOpIdx) const;
1019 Expected<bool>
1020 importImplicitDefRenderers(BuildMIAction &DstMIBuilder,
1021 const std::vector<Record *> &ImplicitDefs) const;
1022
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001023 /// Analyze pattern \p P, returning a matcher for it if possible.
1024 /// Otherwise, return an Error explaining why we don't support it.
1025 Expected<RuleMatcher> runOnPattern(const PatternToMatch &P);
1026};
1027
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001028void GlobalISelEmitter::gatherNodeEquivs() {
1029 assert(NodeEquivs.empty());
1030 for (Record *Equiv : RK.getAllDerivedDefinitions("GINodeEquiv"))
1031 NodeEquivs[Equiv->getValueAsDef("Node")] =
1032 &Target.getInstruction(Equiv->getValueAsDef("I"));
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001033
1034 assert(ComplexPatternEquivs.empty());
1035 for (Record *Equiv : RK.getAllDerivedDefinitions("GIComplexPatternEquiv")) {
1036 Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent");
1037 if (!SelDAGEquiv)
1038 continue;
1039 ComplexPatternEquivs[SelDAGEquiv] = Equiv;
1040 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001041}
1042
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001043const CodeGenInstruction *GlobalISelEmitter::findNodeEquiv(Record *N) const {
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001044 return NodeEquivs.lookup(N);
1045}
1046
1047GlobalISelEmitter::GlobalISelEmitter(RecordKeeper &RK)
1048 : RK(RK), CGP(RK), Target(CGP.getTargetInfo()) {}
1049
1050//===- Emitter ------------------------------------------------------------===//
1051
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001052/// Helper function to let the emitter report skip reason error messages.
1053static Error failedImport(const Twine &Reason) {
1054 return make_error<StringError>(Reason, inconvertibleErrorCode());
1055}
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001056
Daniel Sandersffc7d582017-03-29 15:37:18 +00001057Expected<bool>
1058GlobalISelEmitter::importRulePredicates(RuleMatcher &M,
1059 ArrayRef<Init *> Predicates) const {
1060 if (!Predicates.empty())
1061 return failedImport("Pattern has a predicate");
1062 return true;
1063}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001064
Daniel Sandersffc7d582017-03-29 15:37:18 +00001065Expected<InstructionMatcher &>
1066GlobalISelEmitter::importSelDAGMatcher(InstructionMatcher &InsnMatcher,
1067 const TreePatternNode *Src) const {
1068 // Start with the defined operands (i.e., the results of the root operator).
1069 if (Src->getExtTypes().size() > 1)
1070 return failedImport("Src pattern has multiple results");
1071
1072 auto SrcGIOrNull = findNodeEquiv(Src->getOperator());
1073 if (!SrcGIOrNull)
1074 return failedImport("Pattern operator lacks an equivalent Instruction");
1075 auto &SrcGI = *SrcGIOrNull;
1076
1077 // The operators look good: match the opcode and mutate it to the new one.
1078 InsnMatcher.addPredicate<InstructionOpcodeMatcher>(&SrcGI);
1079
1080 unsigned OpIdx = 0;
1081 for (const EEVT::TypeSet &Ty : Src->getExtTypes()) {
1082 auto OpTyOrNone = MVTToLLT(Ty.getConcrete());
1083
1084 if (!OpTyOrNone)
1085 return failedImport(
1086 "Result of Src pattern operator has an unsupported type");
1087
1088 // Results don't have a name unless they are the root node. The caller will
1089 // set the name if appropriate.
1090 OperandMatcher &OM = InsnMatcher.addOperand(OpIdx++, "");
1091 OM.addPredicate<LLTOperandMatcher>(*OpTyOrNone);
1092 }
1093
1094 unsigned TempOpIdx = 0;
1095 // Match the used operands (i.e. the children of the operator).
1096 for (unsigned i = 0, e = Src->getNumChildren(); i != e; ++i) {
1097 if (auto Error = importChildMatcher(InsnMatcher, Src->getChild(i), OpIdx++,
1098 TempOpIdx)
1099 .takeError())
1100 return std::move(Error);
1101 }
1102
1103 return InsnMatcher;
1104}
1105
1106Expected<bool>
1107GlobalISelEmitter::importChildMatcher(InstructionMatcher &InsnMatcher,
1108 TreePatternNode *SrcChild, unsigned OpIdx,
1109 unsigned &TempOpIdx) const {
1110 OperandMatcher &OM = InsnMatcher.addOperand(OpIdx, SrcChild->getName());
1111
1112 if (SrcChild->hasAnyPredicate())
1113 return failedImport("Src pattern child has predicate");
1114
1115 ArrayRef<EEVT::TypeSet> ChildTypes = SrcChild->getExtTypes();
1116 if (ChildTypes.size() != 1)
1117 return failedImport("Src pattern child has multiple results");
1118
1119 // Check MBB's before the type check since they are not a known type.
1120 if (!SrcChild->isLeaf()) {
1121 if (SrcChild->getOperator()->isSubClassOf("SDNode")) {
1122 auto &ChildSDNI = CGP.getSDNodeInfo(SrcChild->getOperator());
1123 if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") {
1124 OM.addPredicate<MBBOperandMatcher>();
1125 return true;
1126 }
1127 }
1128
1129 return failedImport("Src child operand is an unsupported type");
1130 }
1131
1132 auto OpTyOrNone = MVTToLLT(ChildTypes.front().getConcrete());
1133 if (!OpTyOrNone)
1134 return failedImport("Src operand has an unsupported type");
1135 OM.addPredicate<LLTOperandMatcher>(*OpTyOrNone);
1136
1137 // Check for constant immediates.
1138 if (auto *ChildInt = dyn_cast<IntInit>(SrcChild->getLeafValue())) {
1139 OM.addPredicate<IntOperandMatcher>(ChildInt->getValue());
1140 return true;
1141 }
1142
1143 // Check for def's like register classes or ComplexPattern's.
1144 if (auto *ChildDefInit = dyn_cast<DefInit>(SrcChild->getLeafValue())) {
1145 auto *ChildRec = ChildDefInit->getDef();
1146
1147 // Check for register classes.
1148 if (ChildRec->isSubClassOf("RegisterClass")) {
1149 OM.addPredicate<RegisterBankOperandMatcher>(
1150 Target.getRegisterClass(ChildRec));
1151 return true;
1152 }
1153
1154 // Check for ComplexPattern's.
1155 if (ChildRec->isSubClassOf("ComplexPattern")) {
1156 const auto &ComplexPattern = ComplexPatternEquivs.find(ChildRec);
1157 if (ComplexPattern == ComplexPatternEquivs.end())
1158 return failedImport(
1159 "SelectionDAG ComplexPattern not mapped to GlobalISel");
1160
1161 const auto &Predicate = OM.addPredicate<ComplexPatternOperandMatcher>(
1162 *ComplexPattern->second, TempOpIdx);
1163 TempOpIdx += Predicate.countTemporaryOperands();
1164 return true;
1165 }
1166
1167 return failedImport(
1168 "Src pattern child def is an unsupported tablegen class");
1169 }
1170
1171 return failedImport("Src pattern child is an unsupported kind");
1172}
1173
1174Expected<bool> GlobalISelEmitter::importExplicitUseRenderer(
1175 BuildMIAction &DstMIBuilder, TreePatternNode *DstChild,
1176 const InstructionMatcher &InsnMatcher, unsigned &TempOpIdx) const {
1177 // The only non-leaf child we accept is 'bb': it's an operator because
1178 // BasicBlockSDNode isn't inline, but in MI it's just another operand.
1179 if (!DstChild->isLeaf()) {
1180 if (DstChild->getOperator()->isSubClassOf("SDNode")) {
1181 auto &ChildSDNI = CGP.getSDNodeInfo(DstChild->getOperator());
1182 if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") {
1183 DstMIBuilder.addRenderer<CopyRenderer>(InsnMatcher,
1184 DstChild->getName());
1185 return true;
1186 }
1187 }
1188 return failedImport("Dst pattern child isn't a leaf node or an MBB");
1189 }
1190
1191 // Otherwise, we're looking for a bog-standard RegisterClass operand.
1192 if (DstChild->hasAnyPredicate())
1193 return failedImport("Dst pattern child has predicate");
1194
1195 if (auto *ChildDefInit = dyn_cast<DefInit>(DstChild->getLeafValue())) {
1196 auto *ChildRec = ChildDefInit->getDef();
1197
1198 ArrayRef<EEVT::TypeSet> ChildTypes = DstChild->getExtTypes();
1199 if (ChildTypes.size() != 1)
1200 return failedImport("Dst pattern child has multiple results");
1201
1202 auto OpTyOrNone = MVTToLLT(ChildTypes.front().getConcrete());
1203 if (!OpTyOrNone)
1204 return failedImport("Dst operand has an unsupported type");
1205
1206 if (ChildRec->isSubClassOf("Register")) {
1207 DstMIBuilder.addRenderer<AddRegisterRenderer>(ChildRec);
1208 return true;
1209 }
1210
1211 if (ChildRec->isSubClassOf("RegisterClass")) {
1212 DstMIBuilder.addRenderer<CopyRenderer>(InsnMatcher, DstChild->getName());
1213 return true;
1214 }
1215
1216 if (ChildRec->isSubClassOf("ComplexPattern")) {
1217 const auto &ComplexPattern = ComplexPatternEquivs.find(ChildRec);
1218 if (ComplexPattern == ComplexPatternEquivs.end())
1219 return failedImport(
1220 "SelectionDAG ComplexPattern not mapped to GlobalISel");
1221
1222 SmallVector<OperandPlaceholder, 2> RenderedOperands;
1223 for (unsigned I = 0;
1224 I <
1225 InsnMatcher.getOperand(DstChild->getName()).countTemporaryOperands();
1226 ++I) {
1227 RenderedOperands.push_back(OperandPlaceholder::CreateTemporary(I));
1228 TempOpIdx++;
1229 }
1230 DstMIBuilder.addRenderer<RenderComplexPatternOperand>(
1231 *ComplexPattern->second, RenderedOperands);
1232 return true;
1233 }
1234
1235 return failedImport(
1236 "Dst pattern child def is an unsupported tablegen class");
1237 }
1238
1239 return failedImport("Dst pattern child is an unsupported kind");
1240}
1241
1242Expected<BuildMIAction &> GlobalISelEmitter::importInstructionRenderer(
1243 RuleMatcher &M, const TreePatternNode *Dst,
1244 const InstructionMatcher &InsnMatcher) const {
1245 Record *DstOp = Dst->getOperator();
1246 if (!DstOp->isSubClassOf("Instruction"))
1247 return failedImport("Pattern operator isn't an instruction");
1248 auto &DstI = Target.getInstruction(DstOp);
1249
1250 auto &DstMIBuilder = M.addAction<BuildMIAction>(&DstI, InsnMatcher);
1251
1252 // Render the explicit defs.
1253 for (unsigned I = 0; I < DstI.Operands.NumDefs; ++I) {
1254 const auto &DstIOperand = DstI.Operands[I];
1255 DstMIBuilder.addRenderer<CopyRenderer>(InsnMatcher, DstIOperand.Name);
1256 }
1257
1258 // Render the explicit uses.
1259 unsigned TempOpIdx = 0;
1260 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
1261 if (auto Error = importExplicitUseRenderer(DstMIBuilder, Dst->getChild(i),
1262 InsnMatcher, TempOpIdx)
1263 .takeError())
1264 return std::move(Error);
1265 }
1266
1267 return DstMIBuilder;
1268}
1269
1270Expected<bool> GlobalISelEmitter::importImplicitDefRenderers(
1271 BuildMIAction &DstMIBuilder,
1272 const std::vector<Record *> &ImplicitDefs) const {
1273 if (!ImplicitDefs.empty())
1274 return failedImport("Pattern defines a physical register");
1275 return true;
1276}
1277
1278Expected<RuleMatcher> GlobalISelEmitter::runOnPattern(const PatternToMatch &P) {
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001279 // Keep track of the matchers and actions to emit.
Ahmed Bougacha9aa4c102017-02-04 00:47:08 +00001280 RuleMatcher M;
1281 M.addAction<DebugCommentAction>(P);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001282
Daniel Sandersffc7d582017-03-29 15:37:18 +00001283 if (auto Error =
1284 importRulePredicates(M, P.getPredicates()->getValues()).takeError())
1285 return std::move(Error);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001286
1287 // Next, analyze the pattern operators.
1288 TreePatternNode *Src = P.getSrcPattern();
1289 TreePatternNode *Dst = P.getDstPattern();
1290
1291 // If the root of either pattern isn't a simple operator, ignore it.
1292 if (!isTrivialOperatorNode(Dst))
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001293 return failedImport("Dst pattern root isn't a trivial operator");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001294 if (!isTrivialOperatorNode(Src))
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001295 return failedImport("Src pattern root isn't a trivial operator");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001296
1297 Record *DstOp = Dst->getOperator();
1298 if (!DstOp->isSubClassOf("Instruction"))
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001299 return failedImport("Pattern operator isn't an instruction");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001300
1301 auto &DstI = Target.getInstruction(DstOp);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001302 if (DstI.Operands.NumDefs != Src->getExtTypes().size())
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001303 return failedImport("Src pattern results and dst MI defs are different");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001304
Daniel Sandersffc7d582017-03-29 15:37:18 +00001305 InstructionMatcher &InsnMatcherTemp = M.addInstructionMatcher();
1306 auto InsnMatcherOrError = importSelDAGMatcher(InsnMatcherTemp, Src);
1307 if (auto Error = InsnMatcherOrError.takeError())
1308 return std::move(Error);
1309 InstructionMatcher &InsnMatcher = InsnMatcherOrError.get();
1310
1311 // The root of the match also has constraints on the register bank so that it
1312 // matches the result instruction.
1313 unsigned OpIdx = 0;
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001314 for (const EEVT::TypeSet &Ty : Src->getExtTypes()) {
Daniel Sandersffc7d582017-03-29 15:37:18 +00001315 (void)Ty;
1316
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001317 const auto &DstIOperand = DstI.Operands[OpIdx];
1318 Record *DstIOpRec = DstIOperand.Rec;
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001319 if (!DstIOpRec->isSubClassOf("RegisterClass"))
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001320 return failedImport("Dst MI def isn't a register class");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001321
Daniel Sandersffc7d582017-03-29 15:37:18 +00001322 OperandMatcher &OM = InsnMatcher.getOperand(OpIdx);
1323 OM.setSymbolicName(DstIOperand.Name);
Daniel Sandersdc662ff2017-01-26 11:10:14 +00001324 OM.addPredicate<RegisterBankOperandMatcher>(
1325 Target.getRegisterClass(DstIOpRec));
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001326 ++OpIdx;
1327 }
1328
Daniel Sandersffc7d582017-03-29 15:37:18 +00001329 auto DstMIBuilderOrError = importInstructionRenderer(M, Dst, InsnMatcher);
1330 if (auto Error = DstMIBuilderOrError.takeError())
1331 return std::move(Error);
1332 BuildMIAction &DstMIBuilder = DstMIBuilderOrError.get();
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001333
Daniel Sandersffc7d582017-03-29 15:37:18 +00001334 // Render the implicit defs.
1335 // These are only added to the root of the result.
1336 if (auto Error =
1337 importImplicitDefRenderers(DstMIBuilder, P.getDstRegs()).takeError())
1338 return std::move(Error);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001339
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001340 // We're done with this pattern! It's eligible for GISel emission; return it.
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00001341 ++NumPatternImported;
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001342 return std::move(M);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001343}
1344
1345void GlobalISelEmitter::run(raw_ostream &OS) {
1346 // Track the GINodeEquiv definitions.
1347 gatherNodeEquivs();
1348
1349 emitSourceFileHeader(("Global Instruction Selector for the " +
1350 Target.getName() + " target").str(), OS);
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00001351 std::vector<RuleMatcher> Rules;
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001352 // Look through the SelectionDAG patterns we found, possibly emitting some.
1353 for (const PatternToMatch &Pat : CGP.ptms()) {
1354 ++NumPatternTotal;
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001355 auto MatcherOrErr = runOnPattern(Pat);
1356
1357 // The pattern analysis can fail, indicating an unsupported pattern.
1358 // Report that if we've been asked to do so.
1359 if (auto Err = MatcherOrErr.takeError()) {
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001360 if (WarnOnSkippedPatterns) {
1361 PrintWarning(Pat.getSrcRecord()->getLoc(),
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001362 "Skipped pattern: " + toString(std::move(Err)));
1363 } else {
1364 consumeError(std::move(Err));
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001365 }
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00001366 ++NumPatternImportsSkipped;
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001367 continue;
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001368 }
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001369
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00001370 Rules.push_back(std::move(MatcherOrErr.get()));
1371 }
1372
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001373 std::stable_sort(Rules.begin(), Rules.end(),
1374 [&](const RuleMatcher &A, const RuleMatcher &B) {
Daniel Sanders759ff412017-02-24 13:58:11 +00001375 if (A.isHigherPriorityThan(B)) {
1376 assert(!B.isHigherPriorityThan(A) && "Cannot be more important "
1377 "and less important at "
1378 "the same time");
1379 return true;
1380 }
1381 return false;
1382 });
1383
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001384 unsigned MaxTemporaries = 0;
1385 for (const auto &Rule : Rules)
1386 MaxTemporaries = std::max(MaxTemporaries, Rule.countTemporaryOperands());
1387
1388 OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n";
1389 for (unsigned I = 0; I < MaxTemporaries; ++I)
1390 OS << " mutable MachineOperand TempOp" << I << ";\n";
1391 OS << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n\n";
1392
1393 OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n";
1394 for (unsigned I = 0; I < MaxTemporaries; ++I)
1395 OS << ", TempOp" << I << "(MachineOperand::CreatePlaceholder())\n";
1396 OS << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n\n";
1397
1398 OS << "#ifdef GET_GLOBALISEL_IMPL\n"
1399 << "bool " << Target.getName()
1400 << "InstructionSelector::selectImpl(MachineInstr &I) const {\n"
1401 << " MachineFunction &MF = *I.getParent()->getParent();\n"
1402 << " const MachineRegisterInfo &MRI = MF.getRegInfo();\n";
1403
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001404 for (auto &Rule : Rules) {
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00001405 Rule.emit(OS);
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001406 ++NumPatternEmitted;
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001407 }
1408
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001409 OS << " return false;\n"
1410 << "}\n"
1411 << "#endif // ifdef GET_GLOBALISEL_IMPL\n";
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001412}
1413
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001414} // end anonymous namespace
1415
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001416//===----------------------------------------------------------------------===//
1417
1418namespace llvm {
1419void EmitGlobalISel(RecordKeeper &RK, raw_ostream &OS) {
1420 GlobalISelEmitter(RK).run(OS);
1421}
1422} // End llvm namespace