blob: ae386ae98e1c97f3eca2b7538d06d511ef6f0c1f [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
55static cl::opt<bool> WarnOnSkippedPatterns(
56 "warn-on-skipped-patterns",
57 cl::desc("Explain why a pattern was skipped for inclusion "
58 "in the GlobalISel selector"),
59 cl::init(false));
60
Daniel Sandersbdfebb82017-03-15 20:18:38 +000061namespace {
Ahmed Bougacha36f70352016-12-21 23:26:20 +000062//===- Helper functions ---------------------------------------------------===//
63
Daniel Sanders52b4ce72017-03-07 23:20:35 +000064/// This class stands in for LLT wherever we want to tablegen-erate an
65/// equivalent at compiler run-time.
66class LLTCodeGen {
67private:
68 LLT Ty;
69
70public:
71 LLTCodeGen(const LLT &Ty) : Ty(Ty) {}
72
73 void emitCxxConstructorCall(raw_ostream &OS) const {
74 if (Ty.isScalar()) {
75 OS << "LLT::scalar(" << Ty.getSizeInBits() << ")";
76 return;
77 }
78 if (Ty.isVector()) {
79 OS << "LLT::vector(" << Ty.getNumElements() << ", " << Ty.getSizeInBits()
80 << ")";
81 return;
82 }
83 llvm_unreachable("Unhandled LLT");
84 }
Daniel Sanders8a4bae92017-03-14 21:32:08 +000085
86 const LLT &get() const { return Ty; }
87};
88
89class InstructionMatcher;
90class OperandPlaceholder {
91private:
92 enum PlaceholderKind {
93 OP_MatchReference,
94 OP_Temporary,
95 } Kind;
96
97 struct MatchReferenceData {
98 InstructionMatcher *InsnMatcher;
99 StringRef InsnVarName;
100 StringRef SymbolicName;
101 };
102
103 struct TemporaryData {
104 unsigned OpIdx;
105 };
106
107 union {
108 struct MatchReferenceData MatchReference;
109 struct TemporaryData Temporary;
110 };
111
112 OperandPlaceholder(PlaceholderKind Kind) : Kind(Kind) {}
113
114public:
115 ~OperandPlaceholder() {}
116
117 static OperandPlaceholder
118 CreateMatchReference(InstructionMatcher *InsnMatcher,
119 const StringRef InsnVarName, const StringRef SymbolicName) {
120 OperandPlaceholder Result(OP_MatchReference);
121 Result.MatchReference.InsnMatcher = InsnMatcher;
122 Result.MatchReference.InsnVarName = InsnVarName;
123 Result.MatchReference.SymbolicName = SymbolicName;
124 return Result;
125 }
126
127 static OperandPlaceholder CreateTemporary(unsigned OpIdx) {
128 OperandPlaceholder Result(OP_Temporary);
129 Result.Temporary.OpIdx = OpIdx;
130 return Result;
131 }
132
133 void emitCxxValueExpr(raw_ostream &OS) const;
Daniel Sanders52b4ce72017-03-07 23:20:35 +0000134};
135
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000136/// Convert an MVT to an equivalent LLT if possible, or the invalid LLT() for
137/// MVTs that don't map cleanly to an LLT (e.g., iPTR, *any, ...).
Daniel Sanders52b4ce72017-03-07 23:20:35 +0000138static Optional<LLTCodeGen> MVTToLLT(MVT::SimpleValueType SVT) {
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000139 MVT VT(SVT);
Daniel Sanders52b4ce72017-03-07 23:20:35 +0000140 if (VT.isVector() && VT.getVectorNumElements() != 1)
141 return LLTCodeGen(LLT::vector(VT.getVectorNumElements(), VT.getScalarSizeInBits()));
142 if (VT.isInteger() || VT.isFloatingPoint())
143 return LLTCodeGen(LLT::scalar(VT.getSizeInBits()));
144 return None;
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000145}
146
147static bool isTrivialOperatorNode(const TreePatternNode *N) {
148 return !N->isLeaf() && !N->hasAnyPredicate() && !N->getTransformFn();
149}
150
151//===- Matchers -----------------------------------------------------------===//
152
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000153class MatchAction;
154
155/// Generates code to check that a match rule matches.
156class RuleMatcher {
157 /// A list of matchers that all need to succeed for the current rule to match.
158 /// FIXME: This currently supports a single match position but could be
159 /// extended to support multiple positions to support div/rem fusion or
160 /// load-multiple instructions.
161 std::vector<std::unique_ptr<InstructionMatcher>> Matchers;
162
163 /// A list of actions that need to be taken when all predicates in this rule
164 /// have succeeded.
165 std::vector<std::unique_ptr<MatchAction>> Actions;
166
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000167 /// A map of instruction matchers to the local variables created by
168 /// emitCxxCaptureStmts().
169 std::map<const InstructionMatcher *, std::string> InsnVariableNames;
170
171 /// ID for the next instruction variable defined with defineInsnVar()
172 unsigned NextInsnVarID;
173
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000174public:
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000175 RuleMatcher()
176 : Matchers(), Actions(), InsnVariableNames(), NextInsnVarID(0) {}
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000177
178 InstructionMatcher &addInstructionMatcher();
179
180 template <class Kind, class... Args> Kind &addAction(Args &&... args);
181
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000182 std::string defineInsnVar(raw_ostream &OS, const InstructionMatcher &Matcher,
183 StringRef Value);
184 StringRef getInsnVarName(const InstructionMatcher &InsnMatcher) const;
185
186 void emitCxxCaptureStmts(raw_ostream &OS, StringRef Expr);
187
188 void emit(raw_ostream &OS);
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000189
190 /// Compare the priority of this object and B.
191 ///
192 /// Returns true if this object is more important than B.
193 bool isHigherPriorityThan(const RuleMatcher &B) const;
194
195 /// Report the maximum number of temporary operands needed by the rule
196 /// matcher.
197 unsigned countTemporaryOperands() const;
198};
199
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000200template <class PredicateTy> class PredicateListMatcher {
201private:
202 typedef std::vector<std::unique_ptr<PredicateTy>> PredicateVec;
203 PredicateVec Predicates;
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000204
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000205public:
206 /// Construct a new operand predicate and add it to the matcher.
207 template <class Kind, class... Args>
208 Kind &addPredicate(Args&&... args) {
Ahmed Bougachab67a3ce2017-01-26 22:07:37 +0000209 Predicates.emplace_back(
210 llvm::make_unique<Kind>(std::forward<Args>(args)...));
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000211 return *static_cast<Kind *>(Predicates.back().get());
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000212 }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000213
214 typename PredicateVec::const_iterator predicates_begin() const { return Predicates.begin(); }
215 typename PredicateVec::const_iterator predicates_end() const { return Predicates.end(); }
216 iterator_range<typename PredicateVec::const_iterator> predicates() const {
217 return make_range(predicates_begin(), predicates_end());
218 }
Daniel Sanders759ff412017-02-24 13:58:11 +0000219 typename PredicateVec::size_type predicates_size() const { return Predicates.size(); }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000220
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000221 /// Emit a C++ expression that tests whether all the predicates are met.
Ahmed Bougachab67a3ce2017-01-26 22:07:37 +0000222 template <class... Args>
Daniel Sandersf8c804f2017-01-28 11:10:42 +0000223 void emitCxxPredicateListExpr(raw_ostream &OS, Args &&... args) const {
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000224 if (Predicates.empty()) {
225 OS << "true";
226 return;
227 }
228
229 StringRef Separator = "";
230 for (const auto &Predicate : predicates()) {
231 OS << Separator << "(";
Ahmed Bougachab67a3ce2017-01-26 22:07:37 +0000232 Predicate->emitCxxPredicateExpr(OS, std::forward<Args>(args)...);
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000233 OS << ")";
Ahmed Bougacha905af9f2017-02-04 00:47:02 +0000234 Separator = " &&\n";
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000235 }
236 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000237};
238
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000239/// Generates code to check a predicate of an operand.
240///
241/// Typical predicates include:
242/// * Operand is a particular register.
243/// * Operand is assigned a particular register bank.
244/// * Operand is an MBB.
245class OperandPredicateMatcher {
246public:
Daniel Sanders759ff412017-02-24 13:58:11 +0000247 /// This enum is used for RTTI and also defines the priority that is given to
248 /// the predicate when generating the matcher code. Kinds with higher priority
249 /// must be tested first.
250 ///
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000251 /// The relative priority of OPM_LLT, OPM_RegBank, and OPM_MBB do not matter
252 /// but OPM_Int must have priority over OPM_RegBank since constant integers
253 /// are represented by a virtual register defined by a G_CONSTANT instruction.
Daniel Sanders759ff412017-02-24 13:58:11 +0000254 enum PredicateKind {
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000255 OPM_ComplexPattern,
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000256 OPM_Int,
Daniel Sanders759ff412017-02-24 13:58:11 +0000257 OPM_LLT,
258 OPM_RegBank,
259 OPM_MBB,
260 };
261
262protected:
263 PredicateKind Kind;
264
265public:
266 OperandPredicateMatcher(PredicateKind Kind) : Kind(Kind) {}
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000267 virtual ~OperandPredicateMatcher() {}
268
Daniel Sanders759ff412017-02-24 13:58:11 +0000269 PredicateKind getKind() const { return Kind; }
270
Daniel Sanderse604ef52017-02-20 15:30:43 +0000271 /// Emit a C++ expression that checks the predicate for the given operand.
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000272 virtual void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanderse604ef52017-02-20 15:30:43 +0000273 StringRef OperandExpr) const = 0;
Daniel Sanders759ff412017-02-24 13:58:11 +0000274
275 /// Compare the priority of this object and B.
276 ///
277 /// Returns true if this object is more important than B.
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000278 virtual bool isHigherPriorityThan(const OperandPredicateMatcher &B) const {
279 return Kind < B.Kind;
Daniel Sanders759ff412017-02-24 13:58:11 +0000280 };
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000281
282 /// Report the maximum number of temporary operands needed by the predicate
283 /// matcher.
284 virtual unsigned countTemporaryOperands() const { return 0; }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000285};
286
287/// Generates code to check that an operand is a particular LLT.
288class LLTOperandMatcher : public OperandPredicateMatcher {
289protected:
Daniel Sanders52b4ce72017-03-07 23:20:35 +0000290 LLTCodeGen Ty;
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000291
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000292public:
Daniel Sanders52b4ce72017-03-07 23:20:35 +0000293 LLTOperandMatcher(const LLTCodeGen &Ty)
Daniel Sanders759ff412017-02-24 13:58:11 +0000294 : OperandPredicateMatcher(OPM_LLT), Ty(Ty) {}
295
296 static bool classof(const OperandPredicateMatcher *P) {
297 return P->getKind() == OPM_LLT;
298 }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000299
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000300 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanderse604ef52017-02-20 15:30:43 +0000301 StringRef OperandExpr) const override {
Daniel Sanders52b4ce72017-03-07 23:20:35 +0000302 OS << "MRI.getType(" << OperandExpr << ".getReg()) == (";
303 Ty.emitCxxConstructorCall(OS);
304 OS << ")";
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000305 }
306};
307
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000308/// Generates code to check that an operand is a particular target constant.
309class ComplexPatternOperandMatcher : public OperandPredicateMatcher {
310protected:
311 const Record &TheDef;
312 /// The index of the first temporary operand to allocate to this
313 /// ComplexPattern.
314 unsigned BaseTemporaryID;
315
316 unsigned getNumOperands() const {
317 return TheDef.getValueAsDag("Operands")->getNumArgs();
318 }
319
320public:
321 ComplexPatternOperandMatcher(const Record &TheDef, unsigned BaseTemporaryID)
322 : OperandPredicateMatcher(OPM_ComplexPattern), TheDef(TheDef),
323 BaseTemporaryID(BaseTemporaryID) {}
324
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000325 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000326 StringRef OperandExpr) const override {
327 OS << TheDef.getValueAsString("MatcherFn") << "(" << OperandExpr;
328 for (unsigned I = 0; I < getNumOperands(); ++I) {
329 OS << ", ";
330 OperandPlaceholder::CreateTemporary(BaseTemporaryID + I)
331 .emitCxxValueExpr(OS);
332 }
333 OS << ")";
334 }
335
336 unsigned countTemporaryOperands() const override {
337 return getNumOperands();
338 }
339};
340
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000341/// Generates code to check that an operand is in a particular register bank.
342class RegisterBankOperandMatcher : public OperandPredicateMatcher {
343protected:
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000344 const CodeGenRegisterClass &RC;
345
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000346public:
Daniel Sanders759ff412017-02-24 13:58:11 +0000347 RegisterBankOperandMatcher(const CodeGenRegisterClass &RC)
348 : OperandPredicateMatcher(OPM_RegBank), RC(RC) {}
349
350 static bool classof(const OperandPredicateMatcher *P) {
351 return P->getKind() == OPM_RegBank;
352 }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000353
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000354 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanderse604ef52017-02-20 15:30:43 +0000355 StringRef OperandExpr) const override {
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000356 OS << "(&RBI.getRegBankFromRegClass(" << RC.getQualifiedName()
Daniel Sanderse604ef52017-02-20 15:30:43 +0000357 << "RegClass) == RBI.getRegBank(" << OperandExpr
358 << ".getReg(), MRI, TRI))";
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000359 }
360};
361
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000362/// Generates code to check that an operand is a basic block.
363class MBBOperandMatcher : public OperandPredicateMatcher {
364public:
Daniel Sanders759ff412017-02-24 13:58:11 +0000365 MBBOperandMatcher() : OperandPredicateMatcher(OPM_MBB) {}
366
367 static bool classof(const OperandPredicateMatcher *P) {
368 return P->getKind() == OPM_MBB;
369 }
370
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000371 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanderse604ef52017-02-20 15:30:43 +0000372 StringRef OperandExpr) const override {
373 OS << OperandExpr << ".isMBB()";
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000374 }
375};
376
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000377/// Generates code to check that an operand is a particular int.
378class IntOperandMatcher : public OperandPredicateMatcher {
379protected:
380 int64_t Value;
381
382public:
383 IntOperandMatcher(int64_t Value)
384 : OperandPredicateMatcher(OPM_Int), Value(Value) {}
385
386 static bool classof(const OperandPredicateMatcher *P) {
387 return P->getKind() == OPM_Int;
388 }
389
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000390 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Simon Pilgrimd0302912017-02-24 17:20:27 +0000391 StringRef OperandExpr) const override {
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000392 OS << "isOperandImmEqual(" << OperandExpr << ", " << Value << ", MRI)";
393 }
394};
395
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000396/// Generates code to check that a set of predicates match for a particular
397/// operand.
398class OperandMatcher : public PredicateListMatcher<OperandPredicateMatcher> {
399protected:
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000400 InstructionMatcher &Insn;
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000401 unsigned OpIdx;
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000402 std::string SymbolicName;
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000403
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000404public:
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000405 OperandMatcher(InstructionMatcher &Insn, unsigned OpIdx,
406 const std::string &SymbolicName)
407 : Insn(Insn), OpIdx(OpIdx), SymbolicName(SymbolicName) {}
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000408
409 bool hasSymbolicName() const { return !SymbolicName.empty(); }
410 const StringRef getSymbolicName() const { return SymbolicName; }
411 unsigned getOperandIndex() const { return OpIdx; }
412
413 std::string getOperandExpr(const StringRef InsnVarName) const {
Pavel Labath52a82e22017-02-21 09:19:41 +0000414 return (InsnVarName + ".getOperand(" + llvm::to_string(OpIdx) + ")").str();
Daniel Sanderse604ef52017-02-20 15:30:43 +0000415 }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000416
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000417 InstructionMatcher &getInstructionMatcher() const { return Insn; }
418
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000419 /// Emit a C++ expression that tests whether the instruction named in
420 /// InsnVarName matches all the predicate and all the operands.
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000421 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
422 const StringRef InsnVarName) const {
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000423 OS << "(/* ";
424 if (SymbolicName.empty())
425 OS << "Operand " << OpIdx;
426 else
427 OS << SymbolicName;
428 OS << " */ ";
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000429 emitCxxPredicateListExpr(OS, Rule, getOperandExpr(InsnVarName));
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000430 OS << ")";
431 }
Daniel Sanders759ff412017-02-24 13:58:11 +0000432
433 /// Compare the priority of this object and B.
434 ///
435 /// Returns true if this object is more important than B.
436 bool isHigherPriorityThan(const OperandMatcher &B) const {
437 // Operand matchers involving more predicates have higher priority.
438 if (predicates_size() > B.predicates_size())
439 return true;
440 if (predicates_size() < B.predicates_size())
441 return false;
442
443 // This assumes that predicates are added in a consistent order.
444 for (const auto &Predicate : zip(predicates(), B.predicates())) {
445 if (std::get<0>(Predicate)->isHigherPriorityThan(*std::get<1>(Predicate)))
446 return true;
447 if (std::get<1>(Predicate)->isHigherPriorityThan(*std::get<0>(Predicate)))
448 return false;
449 }
450
451 return false;
452 };
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000453
454 /// Report the maximum number of temporary operands needed by the operand
455 /// matcher.
456 unsigned countTemporaryOperands() const {
457 return std::accumulate(
458 predicates().begin(), predicates().end(), 0,
459 [](unsigned A,
460 const std::unique_ptr<OperandPredicateMatcher> &Predicate) {
461 return A + Predicate->countTemporaryOperands();
462 });
463 }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000464};
465
466/// Generates code to check a predicate on an instruction.
467///
468/// Typical predicates include:
469/// * The opcode of the instruction is a particular value.
470/// * The nsw/nuw flag is/isn't set.
471class InstructionPredicateMatcher {
Daniel Sanders759ff412017-02-24 13:58:11 +0000472protected:
473 /// This enum is used for RTTI and also defines the priority that is given to
474 /// the predicate when generating the matcher code. Kinds with higher priority
475 /// must be tested first.
476 enum PredicateKind {
477 IPM_Opcode,
478 };
479
480 PredicateKind Kind;
481
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000482public:
Daniel Sanders8d4d72f2017-02-24 14:53:35 +0000483 InstructionPredicateMatcher(PredicateKind Kind) : Kind(Kind) {}
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000484 virtual ~InstructionPredicateMatcher() {}
485
Daniel Sanders759ff412017-02-24 13:58:11 +0000486 PredicateKind getKind() const { return Kind; }
487
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000488 /// Emit a C++ expression that tests whether the instruction named in
489 /// InsnVarName matches the predicate.
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000490 virtual void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Ahmed Bougacha6a1ac5a2017-02-09 02:50:01 +0000491 StringRef InsnVarName) const = 0;
Daniel Sanders759ff412017-02-24 13:58:11 +0000492
493 /// Compare the priority of this object and B.
494 ///
495 /// Returns true if this object is more important than B.
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000496 virtual bool isHigherPriorityThan(const InstructionPredicateMatcher &B) const {
Daniel Sanders759ff412017-02-24 13:58:11 +0000497 return Kind < B.Kind;
498 };
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000499
500 /// Report the maximum number of temporary operands needed by the predicate
501 /// matcher.
502 virtual unsigned countTemporaryOperands() const { return 0; }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000503};
504
505/// Generates code to check the opcode of an instruction.
506class InstructionOpcodeMatcher : public InstructionPredicateMatcher {
507protected:
508 const CodeGenInstruction *I;
509
510public:
Daniel Sanders8d4d72f2017-02-24 14:53:35 +0000511 InstructionOpcodeMatcher(const CodeGenInstruction *I)
512 : InstructionPredicateMatcher(IPM_Opcode), I(I) {}
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000513
Daniel Sanders759ff412017-02-24 13:58:11 +0000514 static bool classof(const InstructionPredicateMatcher *P) {
515 return P->getKind() == IPM_Opcode;
516 }
517
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000518 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Ahmed Bougacha6a1ac5a2017-02-09 02:50:01 +0000519 StringRef InsnVarName) const override {
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000520 OS << InsnVarName << ".getOpcode() == " << I->Namespace
521 << "::" << I->TheDef->getName();
522 }
Daniel Sanders759ff412017-02-24 13:58:11 +0000523
524 /// Compare the priority of this object and B.
525 ///
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000526 /// Returns true if this object is more important than B.
527 bool isHigherPriorityThan(const InstructionPredicateMatcher &B) const override {
Daniel Sanders759ff412017-02-24 13:58:11 +0000528 if (InstructionPredicateMatcher::isHigherPriorityThan(B))
529 return true;
530 if (B.InstructionPredicateMatcher::isHigherPriorityThan(*this))
531 return false;
532
533 // Prioritize opcodes for cosmetic reasons in the generated source. Although
534 // this is cosmetic at the moment, we may want to drive a similar ordering
535 // using instruction frequency information to improve compile time.
536 if (const InstructionOpcodeMatcher *BO =
537 dyn_cast<InstructionOpcodeMatcher>(&B))
538 return I->TheDef->getName() < BO->I->TheDef->getName();
539
540 return false;
541 };
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000542};
543
544/// Generates code to check that a set of predicates and operands match for a
545/// particular instruction.
546///
547/// Typical predicates include:
548/// * Has a specific opcode.
549/// * Has an nsw/nuw flag or doesn't.
550class InstructionMatcher
551 : public PredicateListMatcher<InstructionPredicateMatcher> {
552protected:
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000553 typedef std::vector<OperandMatcher> OperandVec;
554
555 /// The operands to match. All rendered operands must be present even if the
556 /// condition is always true.
557 OperandVec Operands;
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000558
559public:
560 /// Add an operand to the matcher.
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000561 OperandMatcher &addOperand(unsigned OpIdx, const std::string &SymbolicName) {
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000562 Operands.emplace_back(*this, OpIdx, SymbolicName);
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000563 return Operands.back();
564 }
565
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000566 Optional<const OperandMatcher *> getOptionalOperand(StringRef SymbolicName) const {
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000567 assert(!SymbolicName.empty() && "Cannot lookup unnamed operand");
568 const auto &I = std::find_if(Operands.begin(), Operands.end(),
569 [&SymbolicName](const OperandMatcher &X) {
570 return X.getSymbolicName() == SymbolicName;
571 });
572 if (I != Operands.end())
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000573 return &*I;
574 return None;
575 }
576
577 const OperandMatcher &getOperand(const StringRef SymbolicName) const {
578 Optional<const OperandMatcher *>OM = getOptionalOperand(SymbolicName);
579 if (OM.hasValue())
580 return *OM.getValue();
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000581 llvm_unreachable("Failed to lookup operand");
582 }
583
584 unsigned getNumOperands() const { return Operands.size(); }
585 OperandVec::const_iterator operands_begin() const {
586 return Operands.begin();
587 }
588 OperandVec::const_iterator operands_end() const {
589 return Operands.end();
590 }
591 iterator_range<OperandVec::const_iterator> operands() const {
592 return make_range(operands_begin(), operands_end());
593 }
594
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000595 /// Emit C++ statements to check the shape of the match and capture
596 /// instructions into local variables.
597 ///
598 /// TODO: When nested instruction matching is implemented, this function will
599 /// descend into the operands and capture variables.
600 void emitCxxCaptureStmts(raw_ostream &OS, RuleMatcher &Rule, StringRef Expr) {
601 OS << "if (" << Expr << ".getNumOperands() < " << getNumOperands() << ")\n"
602 << " return false;\n";
603 }
604
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000605 /// Emit a C++ expression that tests whether the instruction named in
606 /// InsnVarName matches all the predicates and all the operands.
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000607 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
608 StringRef InsnVarName) const {
609 emitCxxPredicateListExpr(OS, Rule, InsnVarName);
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000610 for (const auto &Operand : Operands) {
Ahmed Bougacha905af9f2017-02-04 00:47:02 +0000611 OS << " &&\n(";
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000612 Operand.emitCxxPredicateExpr(OS, Rule, InsnVarName);
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000613 OS << ")";
614 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000615 }
Daniel Sanders759ff412017-02-24 13:58:11 +0000616
617 /// Compare the priority of this object and B.
618 ///
619 /// Returns true if this object is more important than B.
620 bool isHigherPriorityThan(const InstructionMatcher &B) const {
621 // Instruction matchers involving more operands have higher priority.
622 if (Operands.size() > B.Operands.size())
623 return true;
624 if (Operands.size() < B.Operands.size())
625 return false;
626
627 for (const auto &Predicate : zip(predicates(), B.predicates())) {
628 if (std::get<0>(Predicate)->isHigherPriorityThan(*std::get<1>(Predicate)))
629 return true;
630 if (std::get<1>(Predicate)->isHigherPriorityThan(*std::get<0>(Predicate)))
631 return false;
632 }
633
634 for (const auto &Operand : zip(Operands, B.Operands)) {
635 if (std::get<0>(Operand).isHigherPriorityThan(std::get<1>(Operand)))
636 return true;
637 if (std::get<1>(Operand).isHigherPriorityThan(std::get<0>(Operand)))
638 return false;
639 }
640
641 return false;
642 };
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000643
644 /// Report the maximum number of temporary operands needed by the instruction
645 /// matcher.
646 unsigned countTemporaryOperands() const {
647 return std::accumulate(predicates().begin(), predicates().end(), 0,
648 [](unsigned A,
649 const std::unique_ptr<InstructionPredicateMatcher>
650 &Predicate) {
651 return A + Predicate->countTemporaryOperands();
652 }) +
653 std::accumulate(Operands.begin(), Operands.end(), 0,
654 [](unsigned A, const OperandMatcher &Operand) {
655 return A + Operand.countTemporaryOperands();
656 });
657 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000658};
659
Daniel Sanders43c882c2017-02-01 10:53:10 +0000660//===- Actions ------------------------------------------------------------===//
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000661void OperandPlaceholder::emitCxxValueExpr(raw_ostream &OS) const {
662 switch (Kind) {
663 case OP_MatchReference:
664 OS << MatchReference.InsnMatcher->getOperand(MatchReference.SymbolicName)
665 .getOperandExpr(MatchReference.InsnVarName);
666 break;
667 case OP_Temporary:
668 OS << "TempOp" << Temporary.OpIdx;
669 break;
670 }
671}
Daniel Sanders43c882c2017-02-01 10:53:10 +0000672
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000673class OperandRenderer {
674public:
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000675 enum RendererKind { OR_Copy, OR_Register, OR_ComplexPattern };
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000676
677protected:
678 RendererKind Kind;
679
680public:
681 OperandRenderer(RendererKind Kind) : Kind(Kind) {}
682 virtual ~OperandRenderer() {}
683
684 RendererKind getKind() const { return Kind; }
685
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000686 virtual void emitCxxRenderStmts(raw_ostream &OS, RuleMatcher &Rule) const = 0;
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000687};
688
689/// A CopyRenderer emits code to copy a single operand from an existing
690/// instruction to the one being built.
691class CopyRenderer : public OperandRenderer {
692protected:
693 /// The matcher for the instruction that this operand is copied from.
694 /// This provides the facility for looking up an a operand by it's name so
695 /// that it can be used as a source for the instruction being built.
696 const InstructionMatcher &Matched;
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000697 /// The name of the operand.
698 const StringRef SymbolicName;
699
700public:
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000701 CopyRenderer(const InstructionMatcher &Matched, StringRef SymbolicName)
702 : OperandRenderer(OR_Copy), Matched(Matched), SymbolicName(SymbolicName) {
703 }
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000704
705 static bool classof(const OperandRenderer *R) {
706 return R->getKind() == OR_Copy;
707 }
708
709 const StringRef getSymbolicName() const { return SymbolicName; }
710
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000711 void emitCxxRenderStmts(raw_ostream &OS, RuleMatcher &Rule) const override {
712 const OperandMatcher &Operand = Matched.getOperand(SymbolicName);
713 StringRef InsnVarName =
714 Rule.getInsnVarName(Operand.getInstructionMatcher());
715 std::string OperandExpr = Operand.getOperandExpr(InsnVarName);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000716 OS << " MIB.add(" << OperandExpr << "/*" << SymbolicName << "*/);\n";
717 }
718};
719
720/// Adds a specific physical register to the instruction being built.
721/// This is typically useful for WZR/XZR on AArch64.
722class AddRegisterRenderer : public OperandRenderer {
723protected:
724 const Record *RegisterDef;
725
726public:
727 AddRegisterRenderer(const Record *RegisterDef)
728 : OperandRenderer(OR_Register), RegisterDef(RegisterDef) {}
729
730 static bool classof(const OperandRenderer *R) {
731 return R->getKind() == OR_Register;
732 }
733
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000734 void emitCxxRenderStmts(raw_ostream &OS, RuleMatcher &Rule) const override {
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000735 OS << " MIB.addReg(" << RegisterDef->getValueAsString("Namespace")
736 << "::" << RegisterDef->getName() << ");\n";
737 }
738};
739
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000740class RenderComplexPatternOperand : public OperandRenderer {
741private:
742 const Record &TheDef;
743 std::vector<OperandPlaceholder> Sources;
744
745 unsigned getNumOperands() const {
746 return TheDef.getValueAsDag("Operands")->getNumArgs();
747 }
748
749public:
750 RenderComplexPatternOperand(const Record &TheDef,
751 const ArrayRef<OperandPlaceholder> Sources)
752 : OperandRenderer(OR_ComplexPattern), TheDef(TheDef), Sources(Sources) {}
753
754 static bool classof(const OperandRenderer *R) {
755 return R->getKind() == OR_ComplexPattern;
756 }
757
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000758 void emitCxxRenderStmts(raw_ostream &OS, RuleMatcher &Rule) const override {
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000759 assert(Sources.size() == getNumOperands() && "Inconsistent number of operands");
760 for (const auto &Source : Sources) {
761 OS << "MIB.add(";
762 Source.emitCxxValueExpr(OS);
763 OS << ");\n";
764 }
765 }
766};
767
Ahmed Bougacha56ca3a92017-02-04 00:47:10 +0000768/// An action taken when all Matcher predicates succeeded for a parent rule.
769///
770/// Typical actions include:
771/// * Changing the opcode of an instruction.
772/// * Adding an operand to an instruction.
Daniel Sanders43c882c2017-02-01 10:53:10 +0000773class MatchAction {
774public:
775 virtual ~MatchAction() {}
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000776
777 /// Emit the C++ statements to implement the action.
778 ///
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000779 /// \param RecycleVarName If given, it's an instruction to recycle. The
780 /// requirements on the instruction vary from action to
781 /// action.
782 virtual void emitCxxActionStmts(raw_ostream &OS, RuleMatcher &Rule,
783 StringRef RecycleVarName) const = 0;
Daniel Sanders43c882c2017-02-01 10:53:10 +0000784};
785
Ahmed Bougacha9aa4c102017-02-04 00:47:08 +0000786/// Generates a comment describing the matched rule being acted upon.
787class DebugCommentAction : public MatchAction {
788private:
789 const PatternToMatch &P;
790
791public:
792 DebugCommentAction(const PatternToMatch &P) : P(P) {}
793
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000794 void emitCxxActionStmts(raw_ostream &OS, RuleMatcher &Rule,
795 StringRef RecycleVarName) const override {
796 OS << "// " << *P.getSrcPattern() << " => " << *P.getDstPattern() << "\n";
Ahmed Bougacha9aa4c102017-02-04 00:47:08 +0000797 }
798};
799
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000800/// Generates code to build an instruction or mutate an existing instruction
801/// into the desired instruction when this is possible.
802class BuildMIAction : public MatchAction {
Daniel Sanders43c882c2017-02-01 10:53:10 +0000803private:
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000804 const CodeGenInstruction *I;
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000805 const InstructionMatcher &Matched;
806 std::vector<std::unique_ptr<OperandRenderer>> OperandRenderers;
807
808 /// True if the instruction can be built solely by mutating the opcode.
809 bool canMutate() const {
810 for (const auto &Renderer : enumerate(OperandRenderers)) {
Zachary Turner309a0882017-03-13 16:24:10 +0000811 if (const auto *Copy = dyn_cast<CopyRenderer>(&*Renderer.value())) {
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000812 if (Matched.getOperand(Copy->getSymbolicName()).getOperandIndex() !=
Zachary Turner309a0882017-03-13 16:24:10 +0000813 Renderer.index())
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000814 return false;
815 } else
816 return false;
817 }
818
819 return true;
820 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000821
Daniel Sanders43c882c2017-02-01 10:53:10 +0000822public:
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000823 BuildMIAction(const CodeGenInstruction *I, const InstructionMatcher &Matched)
824 : I(I), Matched(Matched) {}
Daniel Sanders43c882c2017-02-01 10:53:10 +0000825
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000826 template <class Kind, class... Args>
827 Kind &addRenderer(Args&&... args) {
828 OperandRenderers.emplace_back(
829 llvm::make_unique<Kind>(std::forward<Args>(args)...));
830 return *static_cast<Kind *>(OperandRenderers.back().get());
831 }
832
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000833 void emitCxxActionStmts(raw_ostream &OS, RuleMatcher &Rule,
834 StringRef RecycleVarName) const override {
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000835 if (canMutate()) {
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000836 OS << RecycleVarName << ".setDesc(TII.get(" << I->Namespace
837 << "::" << I->TheDef->getName() << "));\n";
838 OS << " MachineInstr &NewI = " << RecycleVarName << ";\n";
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000839 return;
840 }
841
842 // TODO: Simple permutation looks like it could be almost as common as
843 // mutation due to commutative operations.
844
845 OS << "MachineInstrBuilder MIB = BuildMI(*I.getParent(), I, "
846 "I.getDebugLoc(), TII.get("
847 << I->Namespace << "::" << I->TheDef->getName() << "));\n";
848 for (const auto &Renderer : OperandRenderers)
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000849 Renderer->emitCxxRenderStmts(OS, Rule);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000850 OS << " MIB.setMemRefs(I.memoperands_begin(), I.memoperands_end());\n";
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000851 OS << " " << RecycleVarName << ".eraseFromParent();\n";
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000852 OS << " MachineInstr &NewI = *MIB;\n";
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000853 }
854};
855
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000856InstructionMatcher &RuleMatcher::addInstructionMatcher() {
857 Matchers.emplace_back(new InstructionMatcher());
858 return *Matchers.back();
859}
Ahmed Bougacha56ca3a92017-02-04 00:47:10 +0000860
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000861template <class Kind, class... Args>
862Kind &RuleMatcher::addAction(Args &&... args) {
863 Actions.emplace_back(llvm::make_unique<Kind>(std::forward<Args>(args)...));
864 return *static_cast<Kind *>(Actions.back().get());
865}
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000866
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000867std::string RuleMatcher::defineInsnVar(raw_ostream &OS,
868 const InstructionMatcher &Matcher,
869 StringRef Value) {
870 std::string InsnVarName = "MI" + llvm::to_string(NextInsnVarID++);
871 OS << "MachineInstr &" << InsnVarName << " = " << Value << ";\n";
872 InsnVariableNames[&Matcher] = InsnVarName;
873 return InsnVarName;
874}
875
876StringRef RuleMatcher::getInsnVarName(const InstructionMatcher &InsnMatcher) const {
877 const auto &I = InsnVariableNames.find(&InsnMatcher);
878 if (I != InsnVariableNames.end())
879 return I->second;
880 llvm_unreachable("Matched Insn was not captured in a local variable");
881}
882
883/// Emit C++ statements to check the shape of the match and capture
884/// instructions into local variables.
885void RuleMatcher::emitCxxCaptureStmts(raw_ostream &OS, StringRef Expr) {
886 assert(Matchers.size() == 1 && "Cannot handle multi-root matchers yet");
887 std::string InsnVarName = defineInsnVar(OS, *Matchers.front(), Expr);
888 Matchers.front()->emitCxxCaptureStmts(OS, *this, InsnVarName);
889}
890
891void RuleMatcher::emit(raw_ostream &OS) {
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000892 if (Matchers.empty())
893 llvm_unreachable("Unexpected empty matcher!");
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000894
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000895 // The representation supports rules that require multiple roots such as:
896 // %ptr(p0) = ...
897 // %elt0(s32) = G_LOAD %ptr
898 // %1(p0) = G_ADD %ptr, 4
899 // %elt1(s32) = G_LOAD p0 %1
900 // which could be usefully folded into:
901 // %ptr(p0) = ...
902 // %elt0(s32), %elt1(s32) = TGT_LOAD_PAIR %ptr
903 // on some targets but we don't need to make use of that yet.
904 assert(Matchers.size() == 1 && "Cannot handle multi-root matchers yet");
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000905 OS << "if ([&]() {\n";
906
907 emitCxxCaptureStmts(OS, "I");
908
909 OS << " if (";
910 Matchers.front()->emitCxxPredicateExpr(OS, *this,
911 getInsnVarName(*Matchers.front()));
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000912 OS << ") {\n";
913
914 for (const auto &MA : Actions) {
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000915 MA->emitCxxActionStmts(OS, *this, "I");
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000916 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000917
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000918 OS << " constrainSelectedInstRegOperands(NewI, TII, TRI, RBI);\n";
919 OS << " return true;\n";
920 OS << " }\n";
921 OS << " return false;\n";
922 OS << " }()) { return true; }\n\n";
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000923}
Daniel Sanders43c882c2017-02-01 10:53:10 +0000924
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000925bool RuleMatcher::isHigherPriorityThan(const RuleMatcher &B) const {
926 // Rules involving more match roots have higher priority.
927 if (Matchers.size() > B.Matchers.size())
928 return true;
929 if (Matchers.size() < B.Matchers.size())
Daniel Sanders759ff412017-02-24 13:58:11 +0000930 return false;
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000931
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000932 for (const auto &Matcher : zip(Matchers, B.Matchers)) {
933 if (std::get<0>(Matcher)->isHigherPriorityThan(*std::get<1>(Matcher)))
934 return true;
935 if (std::get<1>(Matcher)->isHigherPriorityThan(*std::get<0>(Matcher)))
936 return false;
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000937 }
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000938
939 return false;
Simon Pilgrima7d1da82017-03-15 22:50:47 +0000940}
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000941
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000942unsigned RuleMatcher::countTemporaryOperands() const {
943 return std::accumulate(
944 Matchers.begin(), Matchers.end(), 0,
945 [](unsigned A, const std::unique_ptr<InstructionMatcher> &Matcher) {
946 return A + Matcher->countTemporaryOperands();
947 });
948}
949
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000950//===- GlobalISelEmitter class --------------------------------------------===//
951
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +0000952class GlobalISelEmitter {
953public:
954 explicit GlobalISelEmitter(RecordKeeper &RK);
955 void run(raw_ostream &OS);
956
957private:
958 const RecordKeeper &RK;
959 const CodeGenDAGPatterns CGP;
960 const CodeGenTarget &Target;
961
962 /// Keep track of the equivalence between SDNodes and Instruction.
963 /// This is defined using 'GINodeEquiv' in the target description.
964 DenseMap<Record *, const CodeGenInstruction *> NodeEquivs;
965
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000966 /// Keep track of the equivalence between ComplexPattern's and
967 /// GIComplexOperandMatcher. Map entries are specified by subclassing
968 /// GIComplexPatternEquiv.
969 DenseMap<const Record *, const Record *> ComplexPatternEquivs;
970
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +0000971 void gatherNodeEquivs();
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000972 const CodeGenInstruction *findNodeEquiv(Record *N) const;
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +0000973
974 /// Analyze pattern \p P, returning a matcher for it if possible.
975 /// Otherwise, return an Error explaining why we don't support it.
976 Expected<RuleMatcher> runOnPattern(const PatternToMatch &P);
977};
978
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000979void GlobalISelEmitter::gatherNodeEquivs() {
980 assert(NodeEquivs.empty());
981 for (Record *Equiv : RK.getAllDerivedDefinitions("GINodeEquiv"))
982 NodeEquivs[Equiv->getValueAsDef("Node")] =
983 &Target.getInstruction(Equiv->getValueAsDef("I"));
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000984
985 assert(ComplexPatternEquivs.empty());
986 for (Record *Equiv : RK.getAllDerivedDefinitions("GIComplexPatternEquiv")) {
987 Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent");
988 if (!SelDAGEquiv)
989 continue;
990 ComplexPatternEquivs[SelDAGEquiv] = Equiv;
991 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000992}
993
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000994const CodeGenInstruction *GlobalISelEmitter::findNodeEquiv(Record *N) const {
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000995 return NodeEquivs.lookup(N);
996}
997
998GlobalISelEmitter::GlobalISelEmitter(RecordKeeper &RK)
999 : RK(RK), CGP(RK), Target(CGP.getTargetInfo()) {}
1000
1001//===- Emitter ------------------------------------------------------------===//
1002
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001003/// Helper function to let the emitter report skip reason error messages.
1004static Error failedImport(const Twine &Reason) {
1005 return make_error<StringError>(Reason, inconvertibleErrorCode());
1006}
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001007
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001008Expected<RuleMatcher> GlobalISelEmitter::runOnPattern(const PatternToMatch &P) {
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001009 unsigned TempOpIdx = 0;
1010
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001011 // Keep track of the matchers and actions to emit.
Ahmed Bougacha9aa4c102017-02-04 00:47:08 +00001012 RuleMatcher M;
1013 M.addAction<DebugCommentAction>(P);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001014
1015 // First, analyze the whole pattern.
1016 // If the entire pattern has a predicate (e.g., target features), ignore it.
1017 if (!P.getPredicates()->getValues().empty())
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001018 return failedImport("Pattern has a predicate");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001019
1020 // Physreg imp-defs require additional logic. Ignore the pattern.
1021 if (!P.getDstRegs().empty())
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001022 return failedImport("Pattern defines a physical register");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001023
1024 // Next, analyze the pattern operators.
1025 TreePatternNode *Src = P.getSrcPattern();
1026 TreePatternNode *Dst = P.getDstPattern();
1027
1028 // If the root of either pattern isn't a simple operator, ignore it.
1029 if (!isTrivialOperatorNode(Dst))
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001030 return failedImport("Dst pattern root isn't a trivial operator");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001031 if (!isTrivialOperatorNode(Src))
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001032 return failedImport("Src pattern root isn't a trivial operator");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001033
1034 Record *DstOp = Dst->getOperator();
1035 if (!DstOp->isSubClassOf("Instruction"))
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001036 return failedImport("Pattern operator isn't an instruction");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001037
1038 auto &DstI = Target.getInstruction(DstOp);
1039
1040 auto SrcGIOrNull = findNodeEquiv(Src->getOperator());
1041 if (!SrcGIOrNull)
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001042 return failedImport("Pattern operator lacks an equivalent Instruction");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001043 auto &SrcGI = *SrcGIOrNull;
1044
1045 // The operators look good: match the opcode and mutate it to the new one.
Daniel Sandersdc662ff2017-01-26 11:10:14 +00001046 InstructionMatcher &InsnMatcher = M.addInstructionMatcher();
1047 InsnMatcher.addPredicate<InstructionOpcodeMatcher>(&SrcGI);
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001048 auto &DstMIBuilder = M.addAction<BuildMIAction>(&DstI, InsnMatcher);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001049
1050 // Next, analyze the children, only accepting patterns that don't require
1051 // any change to operands.
1052 if (Src->getNumChildren() != Dst->getNumChildren())
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001053 return failedImport("Src/dst patterns have a different # of children");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001054
1055 unsigned OpIdx = 0;
1056
1057 // Start with the defined operands (i.e., the results of the root operator).
1058 if (DstI.Operands.NumDefs != Src->getExtTypes().size())
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001059 return failedImport("Src pattern results and dst MI defs are different");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001060
1061 for (const EEVT::TypeSet &Ty : Src->getExtTypes()) {
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001062 const auto &DstIOperand = DstI.Operands[OpIdx];
1063 Record *DstIOpRec = DstIOperand.Rec;
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001064 if (!DstIOpRec->isSubClassOf("RegisterClass"))
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001065 return failedImport("Dst MI def isn't a register class");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001066
1067 auto OpTyOrNone = MVTToLLT(Ty.getConcrete());
1068 if (!OpTyOrNone)
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001069 return failedImport("Dst operand has an unsupported type");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001070
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001071 OperandMatcher &OM = InsnMatcher.addOperand(OpIdx, DstIOperand.Name);
Daniel Sandersdc662ff2017-01-26 11:10:14 +00001072 OM.addPredicate<LLTOperandMatcher>(*OpTyOrNone);
1073 OM.addPredicate<RegisterBankOperandMatcher>(
1074 Target.getRegisterClass(DstIOpRec));
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001075 DstMIBuilder.addRenderer<CopyRenderer>(InsnMatcher, DstIOperand.Name);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001076 ++OpIdx;
1077 }
1078
1079 // Finally match the used operands (i.e., the children of the root operator).
1080 for (unsigned i = 0, e = Src->getNumChildren(); i != e; ++i) {
1081 auto *SrcChild = Src->getChild(i);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001082
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001083 OperandMatcher &OM = InsnMatcher.addOperand(OpIdx++, SrcChild->getName());
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001084
1085 // The only non-leaf child we accept is 'bb': it's an operator because
1086 // BasicBlockSDNode isn't inline, but in MI it's just another operand.
1087 if (!SrcChild->isLeaf()) {
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001088 if (SrcChild->getOperator()->isSubClassOf("SDNode")) {
1089 auto &ChildSDNI = CGP.getSDNodeInfo(SrcChild->getOperator());
1090 if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") {
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001091 OM.addPredicate<MBBOperandMatcher>();
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001092 continue;
1093 }
1094 }
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001095 return failedImport("Src pattern child isn't a leaf node or an MBB");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001096 }
1097
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001098 if (SrcChild->hasAnyPredicate())
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001099 return failedImport("Src pattern child has predicate");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001100
1101 ArrayRef<EEVT::TypeSet> ChildTypes = SrcChild->getExtTypes();
1102 if (ChildTypes.size() != 1)
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001103 return failedImport("Src pattern child has multiple results");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001104
1105 auto OpTyOrNone = MVTToLLT(ChildTypes.front().getConcrete());
1106 if (!OpTyOrNone)
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001107 return failedImport("Src operand has an unsupported type");
Daniel Sandersdc662ff2017-01-26 11:10:14 +00001108 OM.addPredicate<LLTOperandMatcher>(*OpTyOrNone);
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001109
1110 if (auto *ChildInt = dyn_cast<IntInit>(SrcChild->getLeafValue())) {
1111 OM.addPredicate<IntOperandMatcher>(ChildInt->getValue());
1112 continue;
1113 }
1114
1115 if (auto *ChildDefInit = dyn_cast<DefInit>(SrcChild->getLeafValue())) {
1116 auto *ChildRec = ChildDefInit->getDef();
1117
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001118 if (ChildRec->isSubClassOf("RegisterClass")) {
1119 OM.addPredicate<RegisterBankOperandMatcher>(
1120 Target.getRegisterClass(ChildRec));
1121 continue;
1122 }
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001123
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001124 if (ChildRec->isSubClassOf("ComplexPattern")) {
1125 const auto &ComplexPattern = ComplexPatternEquivs.find(ChildRec);
1126 if (ComplexPattern == ComplexPatternEquivs.end())
1127 return failedImport(
1128 "SelectionDAG ComplexPattern not mapped to GlobalISel");
1129
1130 const auto &Predicate = OM.addPredicate<ComplexPatternOperandMatcher>(
1131 *ComplexPattern->second, TempOpIdx);
1132 TempOpIdx += Predicate.countTemporaryOperands();
1133 continue;
1134 }
1135
1136 return failedImport(
1137 "Src pattern child def is an unsupported tablegen class");
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001138 }
1139
1140 return failedImport("Src pattern child is an unsupported kind");
1141 }
1142
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001143 TempOpIdx = 0;
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001144 // Finally render the used operands (i.e., the children of the root operator).
1145 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
1146 auto *DstChild = Dst->getChild(i);
1147
1148 // The only non-leaf child we accept is 'bb': it's an operator because
1149 // BasicBlockSDNode isn't inline, but in MI it's just another operand.
1150 if (!DstChild->isLeaf()) {
1151 if (DstChild->getOperator()->isSubClassOf("SDNode")) {
1152 auto &ChildSDNI = CGP.getSDNodeInfo(DstChild->getOperator());
1153 if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") {
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001154 DstMIBuilder.addRenderer<CopyRenderer>(InsnMatcher,
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001155 DstChild->getName());
1156 continue;
1157 }
1158 }
1159 return failedImport("Dst pattern child isn't a leaf node or an MBB");
1160 }
1161
1162 // Otherwise, we're looking for a bog-standard RegisterClass operand.
1163 if (DstChild->hasAnyPredicate())
1164 return failedImport("Dst pattern child has predicate");
1165
1166 if (auto *ChildDefInit = dyn_cast<DefInit>(DstChild->getLeafValue())) {
1167 auto *ChildRec = ChildDefInit->getDef();
1168
1169 ArrayRef<EEVT::TypeSet> ChildTypes = DstChild->getExtTypes();
1170 if (ChildTypes.size() != 1)
1171 return failedImport("Dst pattern child has multiple results");
1172
1173 auto OpTyOrNone = MVTToLLT(ChildTypes.front().getConcrete());
1174 if (!OpTyOrNone)
1175 return failedImport("Dst operand has an unsupported type");
1176
1177 if (ChildRec->isSubClassOf("Register")) {
1178 DstMIBuilder.addRenderer<AddRegisterRenderer>(ChildRec);
1179 continue;
1180 }
1181
1182 if (ChildRec->isSubClassOf("RegisterClass")) {
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001183 DstMIBuilder.addRenderer<CopyRenderer>(InsnMatcher,
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001184 DstChild->getName());
1185 continue;
1186 }
1187
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001188 if (ChildRec->isSubClassOf("ComplexPattern")) {
1189 const auto &ComplexPattern = ComplexPatternEquivs.find(ChildRec);
1190 if (ComplexPattern == ComplexPatternEquivs.end())
1191 return failedImport(
1192 "SelectionDAG ComplexPattern not mapped to GlobalISel");
1193
1194 SmallVector<OperandPlaceholder, 2> RenderedOperands;
1195 for (unsigned I = 0; I < InsnMatcher.getOperand(DstChild->getName())
1196 .countTemporaryOperands();
1197 ++I) {
1198 RenderedOperands.push_back(OperandPlaceholder::CreateTemporary(I));
1199 TempOpIdx++;
1200 }
1201 DstMIBuilder.addRenderer<RenderComplexPatternOperand>(
1202 *ComplexPattern->second,
1203 RenderedOperands);
1204 continue;
1205 }
1206
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001207 return failedImport(
1208 "Dst pattern child def is an unsupported tablegen class");
1209 }
1210
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001211 return failedImport("Dst pattern child is an unsupported kind");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001212 }
1213
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001214 // We're done with this pattern! It's eligible for GISel emission; return it.
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00001215 ++NumPatternImported;
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001216 return std::move(M);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001217}
1218
1219void GlobalISelEmitter::run(raw_ostream &OS) {
1220 // Track the GINodeEquiv definitions.
1221 gatherNodeEquivs();
1222
1223 emitSourceFileHeader(("Global Instruction Selector for the " +
1224 Target.getName() + " target").str(), OS);
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00001225 std::vector<RuleMatcher> Rules;
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001226 // Look through the SelectionDAG patterns we found, possibly emitting some.
1227 for (const PatternToMatch &Pat : CGP.ptms()) {
1228 ++NumPatternTotal;
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001229 auto MatcherOrErr = runOnPattern(Pat);
1230
1231 // The pattern analysis can fail, indicating an unsupported pattern.
1232 // Report that if we've been asked to do so.
1233 if (auto Err = MatcherOrErr.takeError()) {
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001234 if (WarnOnSkippedPatterns) {
1235 PrintWarning(Pat.getSrcRecord()->getLoc(),
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001236 "Skipped pattern: " + toString(std::move(Err)));
1237 } else {
1238 consumeError(std::move(Err));
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001239 }
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00001240 ++NumPatternImportsSkipped;
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001241 continue;
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001242 }
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001243
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00001244 Rules.push_back(std::move(MatcherOrErr.get()));
1245 }
1246
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001247 std::stable_sort(Rules.begin(), Rules.end(),
1248 [&](const RuleMatcher &A, const RuleMatcher &B) {
Daniel Sanders759ff412017-02-24 13:58:11 +00001249 if (A.isHigherPriorityThan(B)) {
1250 assert(!B.isHigherPriorityThan(A) && "Cannot be more important "
1251 "and less important at "
1252 "the same time");
1253 return true;
1254 }
1255 return false;
1256 });
1257
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001258 unsigned MaxTemporaries = 0;
1259 for (const auto &Rule : Rules)
1260 MaxTemporaries = std::max(MaxTemporaries, Rule.countTemporaryOperands());
1261
1262 OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n";
1263 for (unsigned I = 0; I < MaxTemporaries; ++I)
1264 OS << " mutable MachineOperand TempOp" << I << ";\n";
1265 OS << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n\n";
1266
1267 OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n";
1268 for (unsigned I = 0; I < MaxTemporaries; ++I)
1269 OS << ", TempOp" << I << "(MachineOperand::CreatePlaceholder())\n";
1270 OS << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n\n";
1271
1272 OS << "#ifdef GET_GLOBALISEL_IMPL\n"
1273 << "bool " << Target.getName()
1274 << "InstructionSelector::selectImpl(MachineInstr &I) const {\n"
1275 << " MachineFunction &MF = *I.getParent()->getParent();\n"
1276 << " const MachineRegisterInfo &MRI = MF.getRegInfo();\n";
1277
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001278 for (auto &Rule : Rules) {
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00001279 Rule.emit(OS);
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001280 ++NumPatternEmitted;
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001281 }
1282
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001283 OS << " return false;\n"
1284 << "}\n"
1285 << "#endif // ifdef GET_GLOBALISEL_IMPL\n";
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001286}
1287
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001288} // end anonymous namespace
1289
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001290//===----------------------------------------------------------------------===//
1291
1292namespace llvm {
1293void EmitGlobalISel(RecordKeeper &RK, raw_ostream &OS) {
1294 GlobalISelEmitter(RK).run(OS);
1295}
1296} // End llvm namespace