blob: a2d1b1cbc919fdf7b07b83fa9395a0d10b40ac42 [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,
Daniel Sandersdb7ed372017-04-04 13:52:00 +0000121 StringRef InsnVarName, StringRef SymbolicName) {
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000122 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 Sandersbee57392017-04-04 13:25:23 +0000155class OperandMatcher;
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000156class MatchAction;
157
158/// Generates code to check that a match rule matches.
159class RuleMatcher {
160 /// A list of matchers that all need to succeed for the current rule to match.
161 /// FIXME: This currently supports a single match position but could be
162 /// extended to support multiple positions to support div/rem fusion or
163 /// load-multiple instructions.
164 std::vector<std::unique_ptr<InstructionMatcher>> Matchers;
165
166 /// A list of actions that need to be taken when all predicates in this rule
167 /// have succeeded.
168 std::vector<std::unique_ptr<MatchAction>> Actions;
169
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000170 /// A map of instruction matchers to the local variables created by
171 /// emitCxxCaptureStmts().
172 std::map<const InstructionMatcher *, std::string> InsnVariableNames;
173
174 /// ID for the next instruction variable defined with defineInsnVar()
175 unsigned NextInsnVarID;
176
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000177public:
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000178 RuleMatcher()
179 : Matchers(), Actions(), InsnVariableNames(), NextInsnVarID(0) {}
Zachary Turnerb7dbd872017-03-20 19:56:52 +0000180 RuleMatcher(RuleMatcher &&Other) = default;
181 RuleMatcher &operator=(RuleMatcher &&Other) = default;
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000182
183 InstructionMatcher &addInstructionMatcher();
184
185 template <class Kind, class... Args> Kind &addAction(Args &&... args);
186
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000187 std::string defineInsnVar(raw_ostream &OS, const InstructionMatcher &Matcher,
188 StringRef Value);
189 StringRef getInsnVarName(const InstructionMatcher &InsnMatcher) const;
190
Daniel Sandersbee57392017-04-04 13:25:23 +0000191 void emitCxxCapturedInsnList(raw_ostream &OS);
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000192 void emitCxxCaptureStmts(raw_ostream &OS, StringRef Expr);
193
194 void emit(raw_ostream &OS);
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000195
196 /// Compare the priority of this object and B.
197 ///
198 /// Returns true if this object is more important than B.
199 bool isHigherPriorityThan(const RuleMatcher &B) const;
200
201 /// Report the maximum number of temporary operands needed by the rule
202 /// matcher.
203 unsigned countTemporaryOperands() const;
204};
205
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000206template <class PredicateTy> class PredicateListMatcher {
207private:
208 typedef std::vector<std::unique_ptr<PredicateTy>> PredicateVec;
209 PredicateVec Predicates;
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000210
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000211public:
212 /// Construct a new operand predicate and add it to the matcher.
213 template <class Kind, class... Args>
214 Kind &addPredicate(Args&&... args) {
Ahmed Bougachab67a3ce2017-01-26 22:07:37 +0000215 Predicates.emplace_back(
216 llvm::make_unique<Kind>(std::forward<Args>(args)...));
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000217 return *static_cast<Kind *>(Predicates.back().get());
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000218 }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000219
220 typename PredicateVec::const_iterator predicates_begin() const { return Predicates.begin(); }
221 typename PredicateVec::const_iterator predicates_end() const { return Predicates.end(); }
222 iterator_range<typename PredicateVec::const_iterator> predicates() const {
223 return make_range(predicates_begin(), predicates_end());
224 }
Daniel Sanders759ff412017-02-24 13:58:11 +0000225 typename PredicateVec::size_type predicates_size() const { return Predicates.size(); }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000226
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000227 /// Emit a C++ expression that tests whether all the predicates are met.
Ahmed Bougachab67a3ce2017-01-26 22:07:37 +0000228 template <class... Args>
Daniel Sandersf8c804f2017-01-28 11:10:42 +0000229 void emitCxxPredicateListExpr(raw_ostream &OS, Args &&... args) const {
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000230 if (Predicates.empty()) {
231 OS << "true";
232 return;
233 }
234
235 StringRef Separator = "";
236 for (const auto &Predicate : predicates()) {
237 OS << Separator << "(";
Ahmed Bougachab67a3ce2017-01-26 22:07:37 +0000238 Predicate->emitCxxPredicateExpr(OS, std::forward<Args>(args)...);
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000239 OS << ")";
Ahmed Bougacha905af9f2017-02-04 00:47:02 +0000240 Separator = " &&\n";
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000241 }
242 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000243};
244
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000245/// Generates code to check a predicate of an operand.
246///
247/// Typical predicates include:
248/// * Operand is a particular register.
249/// * Operand is assigned a particular register bank.
250/// * Operand is an MBB.
251class OperandPredicateMatcher {
252public:
Daniel Sanders759ff412017-02-24 13:58:11 +0000253 /// This enum is used for RTTI and also defines the priority that is given to
254 /// the predicate when generating the matcher code. Kinds with higher priority
255 /// must be tested first.
256 ///
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000257 /// The relative priority of OPM_LLT, OPM_RegBank, and OPM_MBB do not matter
258 /// but OPM_Int must have priority over OPM_RegBank since constant integers
259 /// are represented by a virtual register defined by a G_CONSTANT instruction.
Daniel Sanders759ff412017-02-24 13:58:11 +0000260 enum PredicateKind {
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000261 OPM_ComplexPattern,
Daniel Sandersbee57392017-04-04 13:25:23 +0000262 OPM_Instruction,
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000263 OPM_Int,
Daniel Sanders759ff412017-02-24 13:58:11 +0000264 OPM_LLT,
265 OPM_RegBank,
266 OPM_MBB,
267 };
268
269protected:
270 PredicateKind Kind;
271
272public:
273 OperandPredicateMatcher(PredicateKind Kind) : Kind(Kind) {}
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000274 virtual ~OperandPredicateMatcher() {}
275
Daniel Sanders759ff412017-02-24 13:58:11 +0000276 PredicateKind getKind() const { return Kind; }
277
Daniel Sandersbee57392017-04-04 13:25:23 +0000278 /// Return the OperandMatcher for the specified operand or nullptr if there
279 /// isn't one by that name in this operand predicate matcher.
280 ///
281 /// InstructionOperandMatcher is the only subclass that can return non-null
282 /// for this.
283 virtual Optional<const OperandMatcher *>
Daniel Sandersdb7ed372017-04-04 13:52:00 +0000284 getOptionalOperand(StringRef SymbolicName) const {
Daniel Sandersbee57392017-04-04 13:25:23 +0000285 assert(!SymbolicName.empty() && "Cannot lookup unnamed operand");
286 return None;
287 }
288
289 /// Emit C++ statements to capture instructions into local variables.
290 ///
291 /// Only InstructionOperandMatcher needs to do anything for this method.
292 virtual void emitCxxCaptureStmts(raw_ostream &OS, RuleMatcher &Rule,
293 StringRef Expr) const {}
294
Daniel Sanderse604ef52017-02-20 15:30:43 +0000295 /// Emit a C++ expression that checks the predicate for the given operand.
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000296 virtual void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanderse604ef52017-02-20 15:30:43 +0000297 StringRef OperandExpr) const = 0;
Daniel Sanders759ff412017-02-24 13:58:11 +0000298
299 /// Compare the priority of this object and B.
300 ///
301 /// Returns true if this object is more important than B.
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000302 virtual bool isHigherPriorityThan(const OperandPredicateMatcher &B) const {
303 return Kind < B.Kind;
Daniel Sanders759ff412017-02-24 13:58:11 +0000304 };
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000305
306 /// Report the maximum number of temporary operands needed by the predicate
307 /// matcher.
308 virtual unsigned countTemporaryOperands() const { return 0; }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000309};
310
311/// Generates code to check that an operand is a particular LLT.
312class LLTOperandMatcher : public OperandPredicateMatcher {
313protected:
Daniel Sanders52b4ce72017-03-07 23:20:35 +0000314 LLTCodeGen Ty;
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000315
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000316public:
Daniel Sanders52b4ce72017-03-07 23:20:35 +0000317 LLTOperandMatcher(const LLTCodeGen &Ty)
Daniel Sanders759ff412017-02-24 13:58:11 +0000318 : OperandPredicateMatcher(OPM_LLT), Ty(Ty) {}
319
320 static bool classof(const OperandPredicateMatcher *P) {
321 return P->getKind() == OPM_LLT;
322 }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000323
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000324 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanderse604ef52017-02-20 15:30:43 +0000325 StringRef OperandExpr) const override {
Daniel Sanders52b4ce72017-03-07 23:20:35 +0000326 OS << "MRI.getType(" << OperandExpr << ".getReg()) == (";
327 Ty.emitCxxConstructorCall(OS);
328 OS << ")";
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000329 }
330};
331
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000332/// Generates code to check that an operand is a particular target constant.
333class ComplexPatternOperandMatcher : public OperandPredicateMatcher {
334protected:
335 const Record &TheDef;
336 /// The index of the first temporary operand to allocate to this
337 /// ComplexPattern.
338 unsigned BaseTemporaryID;
339
340 unsigned getNumOperands() const {
341 return TheDef.getValueAsDag("Operands")->getNumArgs();
342 }
343
344public:
345 ComplexPatternOperandMatcher(const Record &TheDef, unsigned BaseTemporaryID)
346 : OperandPredicateMatcher(OPM_ComplexPattern), TheDef(TheDef),
347 BaseTemporaryID(BaseTemporaryID) {}
348
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000349 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000350 StringRef OperandExpr) const override {
351 OS << TheDef.getValueAsString("MatcherFn") << "(" << OperandExpr;
352 for (unsigned I = 0; I < getNumOperands(); ++I) {
353 OS << ", ";
354 OperandPlaceholder::CreateTemporary(BaseTemporaryID + I)
355 .emitCxxValueExpr(OS);
356 }
357 OS << ")";
358 }
359
360 unsigned countTemporaryOperands() const override {
361 return getNumOperands();
362 }
363};
364
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000365/// Generates code to check that an operand is in a particular register bank.
366class RegisterBankOperandMatcher : public OperandPredicateMatcher {
367protected:
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000368 const CodeGenRegisterClass &RC;
369
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000370public:
Daniel Sanders759ff412017-02-24 13:58:11 +0000371 RegisterBankOperandMatcher(const CodeGenRegisterClass &RC)
372 : OperandPredicateMatcher(OPM_RegBank), RC(RC) {}
373
374 static bool classof(const OperandPredicateMatcher *P) {
375 return P->getKind() == OPM_RegBank;
376 }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000377
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000378 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanderse604ef52017-02-20 15:30:43 +0000379 StringRef OperandExpr) const override {
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000380 OS << "(&RBI.getRegBankFromRegClass(" << RC.getQualifiedName()
Daniel Sanderse604ef52017-02-20 15:30:43 +0000381 << "RegClass) == RBI.getRegBank(" << OperandExpr
382 << ".getReg(), MRI, TRI))";
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000383 }
384};
385
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000386/// Generates code to check that an operand is a basic block.
387class MBBOperandMatcher : public OperandPredicateMatcher {
388public:
Daniel Sanders759ff412017-02-24 13:58:11 +0000389 MBBOperandMatcher() : OperandPredicateMatcher(OPM_MBB) {}
390
391 static bool classof(const OperandPredicateMatcher *P) {
392 return P->getKind() == OPM_MBB;
393 }
394
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000395 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanderse604ef52017-02-20 15:30:43 +0000396 StringRef OperandExpr) const override {
397 OS << OperandExpr << ".isMBB()";
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000398 }
399};
400
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000401/// Generates code to check that an operand is a particular int.
402class IntOperandMatcher : public OperandPredicateMatcher {
403protected:
404 int64_t Value;
405
406public:
407 IntOperandMatcher(int64_t Value)
408 : OperandPredicateMatcher(OPM_Int), Value(Value) {}
409
410 static bool classof(const OperandPredicateMatcher *P) {
411 return P->getKind() == OPM_Int;
412 }
413
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000414 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Simon Pilgrimd0302912017-02-24 17:20:27 +0000415 StringRef OperandExpr) const override {
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000416 OS << "isOperandImmEqual(" << OperandExpr << ", " << Value << ", MRI)";
417 }
418};
419
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000420/// Generates code to check that a set of predicates match for a particular
421/// operand.
422class OperandMatcher : public PredicateListMatcher<OperandPredicateMatcher> {
423protected:
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000424 InstructionMatcher &Insn;
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000425 unsigned OpIdx;
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000426 std::string SymbolicName;
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000427
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000428public:
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000429 OperandMatcher(InstructionMatcher &Insn, unsigned OpIdx,
430 const std::string &SymbolicName)
431 : Insn(Insn), OpIdx(OpIdx), SymbolicName(SymbolicName) {}
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000432
433 bool hasSymbolicName() const { return !SymbolicName.empty(); }
434 const StringRef getSymbolicName() const { return SymbolicName; }
Daniel Sandersffc7d582017-03-29 15:37:18 +0000435 void setSymbolicName(StringRef Name) {
436 assert(SymbolicName.empty() && "Operand already has a symbolic name");
437 SymbolicName = Name;
438 }
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000439 unsigned getOperandIndex() const { return OpIdx; }
440
Daniel Sandersdb7ed372017-04-04 13:52:00 +0000441 std::string getOperandExpr(StringRef InsnVarName) const {
Pavel Labath52a82e22017-02-21 09:19:41 +0000442 return (InsnVarName + ".getOperand(" + llvm::to_string(OpIdx) + ")").str();
Daniel Sanderse604ef52017-02-20 15:30:43 +0000443 }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000444
Daniel Sandersbee57392017-04-04 13:25:23 +0000445 Optional<const OperandMatcher *>
446 getOptionalOperand(StringRef DesiredSymbolicName) const {
447 assert(!DesiredSymbolicName.empty() && "Cannot lookup unnamed operand");
448 if (DesiredSymbolicName == SymbolicName)
449 return this;
450 for (const auto &OP : predicates()) {
451 const auto &MaybeOperand = OP->getOptionalOperand(DesiredSymbolicName);
452 if (MaybeOperand.hasValue())
453 return MaybeOperand.getValue();
454 }
455 return None;
456 }
457
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000458 InstructionMatcher &getInstructionMatcher() const { return Insn; }
459
Daniel Sandersbee57392017-04-04 13:25:23 +0000460 /// Emit C++ statements to capture instructions into local variables.
461 void emitCxxCaptureStmts(raw_ostream &OS, RuleMatcher &Rule,
462 StringRef OperandExpr) const {
463 for (const auto &Predicate : predicates())
464 Predicate->emitCxxCaptureStmts(OS, Rule, OperandExpr);
465 }
466
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000467 /// Emit a C++ expression that tests whether the instruction named in
468 /// InsnVarName matches all the predicate and all the operands.
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000469 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sandersdb7ed372017-04-04 13:52:00 +0000470 StringRef InsnVarName) const {
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000471 OS << "(/* ";
472 if (SymbolicName.empty())
473 OS << "Operand " << OpIdx;
474 else
475 OS << SymbolicName;
476 OS << " */ ";
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000477 emitCxxPredicateListExpr(OS, Rule, getOperandExpr(InsnVarName));
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000478 OS << ")";
479 }
Daniel Sanders759ff412017-02-24 13:58:11 +0000480
481 /// Compare the priority of this object and B.
482 ///
483 /// Returns true if this object is more important than B.
484 bool isHigherPriorityThan(const OperandMatcher &B) const {
485 // Operand matchers involving more predicates have higher priority.
486 if (predicates_size() > B.predicates_size())
487 return true;
488 if (predicates_size() < B.predicates_size())
489 return false;
490
491 // This assumes that predicates are added in a consistent order.
492 for (const auto &Predicate : zip(predicates(), B.predicates())) {
493 if (std::get<0>(Predicate)->isHigherPriorityThan(*std::get<1>(Predicate)))
494 return true;
495 if (std::get<1>(Predicate)->isHigherPriorityThan(*std::get<0>(Predicate)))
496 return false;
497 }
498
499 return false;
500 };
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000501
502 /// Report the maximum number of temporary operands needed by the operand
503 /// matcher.
504 unsigned countTemporaryOperands() const {
505 return std::accumulate(
506 predicates().begin(), predicates().end(), 0,
507 [](unsigned A,
508 const std::unique_ptr<OperandPredicateMatcher> &Predicate) {
509 return A + Predicate->countTemporaryOperands();
510 });
511 }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000512};
513
514/// Generates code to check a predicate on an instruction.
515///
516/// Typical predicates include:
517/// * The opcode of the instruction is a particular value.
518/// * The nsw/nuw flag is/isn't set.
519class InstructionPredicateMatcher {
Daniel Sanders759ff412017-02-24 13:58:11 +0000520protected:
521 /// This enum is used for RTTI and also defines the priority that is given to
522 /// the predicate when generating the matcher code. Kinds with higher priority
523 /// must be tested first.
524 enum PredicateKind {
525 IPM_Opcode,
526 };
527
528 PredicateKind Kind;
529
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000530public:
Daniel Sanders8d4d72f2017-02-24 14:53:35 +0000531 InstructionPredicateMatcher(PredicateKind Kind) : Kind(Kind) {}
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000532 virtual ~InstructionPredicateMatcher() {}
533
Daniel Sanders759ff412017-02-24 13:58:11 +0000534 PredicateKind getKind() const { return Kind; }
535
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000536 /// Emit a C++ expression that tests whether the instruction named in
537 /// InsnVarName matches the predicate.
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000538 virtual void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Ahmed Bougacha6a1ac5a2017-02-09 02:50:01 +0000539 StringRef InsnVarName) const = 0;
Daniel Sanders759ff412017-02-24 13:58:11 +0000540
541 /// Compare the priority of this object and B.
542 ///
543 /// Returns true if this object is more important than B.
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000544 virtual bool isHigherPriorityThan(const InstructionPredicateMatcher &B) const {
Daniel Sanders759ff412017-02-24 13:58:11 +0000545 return Kind < B.Kind;
546 };
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000547
548 /// Report the maximum number of temporary operands needed by the predicate
549 /// matcher.
550 virtual unsigned countTemporaryOperands() const { return 0; }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000551};
552
553/// Generates code to check the opcode of an instruction.
554class InstructionOpcodeMatcher : public InstructionPredicateMatcher {
555protected:
556 const CodeGenInstruction *I;
557
558public:
Daniel Sanders8d4d72f2017-02-24 14:53:35 +0000559 InstructionOpcodeMatcher(const CodeGenInstruction *I)
560 : InstructionPredicateMatcher(IPM_Opcode), I(I) {}
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000561
Daniel Sanders759ff412017-02-24 13:58:11 +0000562 static bool classof(const InstructionPredicateMatcher *P) {
563 return P->getKind() == IPM_Opcode;
564 }
565
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000566 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
Ahmed Bougacha6a1ac5a2017-02-09 02:50:01 +0000567 StringRef InsnVarName) const override {
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000568 OS << InsnVarName << ".getOpcode() == " << I->Namespace
569 << "::" << I->TheDef->getName();
570 }
Daniel Sanders759ff412017-02-24 13:58:11 +0000571
572 /// Compare the priority of this object and B.
573 ///
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000574 /// Returns true if this object is more important than B.
575 bool isHigherPriorityThan(const InstructionPredicateMatcher &B) const override {
Daniel Sanders759ff412017-02-24 13:58:11 +0000576 if (InstructionPredicateMatcher::isHigherPriorityThan(B))
577 return true;
578 if (B.InstructionPredicateMatcher::isHigherPriorityThan(*this))
579 return false;
580
581 // Prioritize opcodes for cosmetic reasons in the generated source. Although
582 // this is cosmetic at the moment, we may want to drive a similar ordering
583 // using instruction frequency information to improve compile time.
584 if (const InstructionOpcodeMatcher *BO =
585 dyn_cast<InstructionOpcodeMatcher>(&B))
586 return I->TheDef->getName() < BO->I->TheDef->getName();
587
588 return false;
589 };
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000590};
591
592/// Generates code to check that a set of predicates and operands match for a
593/// particular instruction.
594///
595/// Typical predicates include:
596/// * Has a specific opcode.
597/// * Has an nsw/nuw flag or doesn't.
598class InstructionMatcher
599 : public PredicateListMatcher<InstructionPredicateMatcher> {
600protected:
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000601 typedef std::vector<OperandMatcher> OperandVec;
602
603 /// The operands to match. All rendered operands must be present even if the
604 /// condition is always true.
605 OperandVec Operands;
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000606
607public:
608 /// Add an operand to the matcher.
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000609 OperandMatcher &addOperand(unsigned OpIdx, const std::string &SymbolicName) {
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000610 Operands.emplace_back(*this, OpIdx, SymbolicName);
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000611 return Operands.back();
612 }
613
Daniel Sandersffc7d582017-03-29 15:37:18 +0000614 OperandMatcher &getOperand(unsigned OpIdx) {
615 auto I = std::find_if(Operands.begin(), Operands.end(),
616 [&OpIdx](const OperandMatcher &X) {
617 return X.getOperandIndex() == OpIdx;
618 });
619 if (I != Operands.end())
620 return *I;
621 llvm_unreachable("Failed to lookup operand");
622 }
623
Daniel Sandersbee57392017-04-04 13:25:23 +0000624 Optional<const OperandMatcher *>
625 getOptionalOperand(StringRef SymbolicName) const {
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000626 assert(!SymbolicName.empty() && "Cannot lookup unnamed operand");
Daniel Sandersbee57392017-04-04 13:25:23 +0000627 for (const auto &Operand : Operands) {
628 const auto &OM = Operand.getOptionalOperand(SymbolicName);
629 if (OM.hasValue())
630 return OM.getValue();
631 }
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000632 return None;
633 }
634
Daniel Sandersdb7ed372017-04-04 13:52:00 +0000635 const OperandMatcher &getOperand(StringRef SymbolicName) const {
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000636 Optional<const OperandMatcher *>OM = getOptionalOperand(SymbolicName);
637 if (OM.hasValue())
638 return *OM.getValue();
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000639 llvm_unreachable("Failed to lookup operand");
640 }
641
642 unsigned getNumOperands() const { return Operands.size(); }
Daniel Sandersbee57392017-04-04 13:25:23 +0000643 OperandVec::iterator operands_begin() { return Operands.begin(); }
644 OperandVec::iterator operands_end() { return Operands.end(); }
645 iterator_range<OperandVec::iterator> operands() {
646 return make_range(operands_begin(), operands_end());
647 }
Daniel Sandersffc7d582017-03-29 15:37:18 +0000648 OperandVec::const_iterator operands_begin() const { return Operands.begin(); }
649 OperandVec::const_iterator operands_end() const { return Operands.end(); }
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000650 iterator_range<OperandVec::const_iterator> operands() const {
651 return make_range(operands_begin(), operands_end());
652 }
653
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000654 /// Emit C++ statements to check the shape of the match and capture
655 /// instructions into local variables.
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000656 void emitCxxCaptureStmts(raw_ostream &OS, RuleMatcher &Rule, StringRef Expr) {
657 OS << "if (" << Expr << ".getNumOperands() < " << getNumOperands() << ")\n"
658 << " return false;\n";
Daniel Sandersbee57392017-04-04 13:25:23 +0000659 for (const auto &Operand : Operands) {
660 Operand.emitCxxCaptureStmts(OS, Rule, Operand.getOperandExpr(Expr));
661 }
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000662 }
663
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000664 /// Emit a C++ expression that tests whether the instruction named in
665 /// InsnVarName matches all the predicates and all the operands.
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000666 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
667 StringRef InsnVarName) const {
668 emitCxxPredicateListExpr(OS, Rule, InsnVarName);
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000669 for (const auto &Operand : Operands) {
Ahmed Bougacha905af9f2017-02-04 00:47:02 +0000670 OS << " &&\n(";
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000671 Operand.emitCxxPredicateExpr(OS, Rule, InsnVarName);
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000672 OS << ")";
673 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000674 }
Daniel Sanders759ff412017-02-24 13:58:11 +0000675
676 /// Compare the priority of this object and B.
677 ///
678 /// Returns true if this object is more important than B.
679 bool isHigherPriorityThan(const InstructionMatcher &B) const {
680 // Instruction matchers involving more operands have higher priority.
681 if (Operands.size() > B.Operands.size())
682 return true;
683 if (Operands.size() < B.Operands.size())
684 return false;
685
686 for (const auto &Predicate : zip(predicates(), B.predicates())) {
687 if (std::get<0>(Predicate)->isHigherPriorityThan(*std::get<1>(Predicate)))
688 return true;
689 if (std::get<1>(Predicate)->isHigherPriorityThan(*std::get<0>(Predicate)))
690 return false;
691 }
692
693 for (const auto &Operand : zip(Operands, B.Operands)) {
694 if (std::get<0>(Operand).isHigherPriorityThan(std::get<1>(Operand)))
695 return true;
696 if (std::get<1>(Operand).isHigherPriorityThan(std::get<0>(Operand)))
697 return false;
698 }
699
700 return false;
701 };
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000702
703 /// Report the maximum number of temporary operands needed by the instruction
704 /// matcher.
705 unsigned countTemporaryOperands() const {
706 return std::accumulate(predicates().begin(), predicates().end(), 0,
707 [](unsigned A,
708 const std::unique_ptr<InstructionPredicateMatcher>
709 &Predicate) {
710 return A + Predicate->countTemporaryOperands();
711 }) +
712 std::accumulate(Operands.begin(), Operands.end(), 0,
713 [](unsigned A, const OperandMatcher &Operand) {
714 return A + Operand.countTemporaryOperands();
715 });
716 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000717};
718
Daniel Sandersbee57392017-04-04 13:25:23 +0000719/// Generates code to check that the operand is a register defined by an
720/// instruction that matches the given instruction matcher.
721///
722/// For example, the pattern:
723/// (set $dst, (G_MUL (G_ADD $src1, $src2), $src3))
724/// would use an InstructionOperandMatcher for operand 1 of the G_MUL to match
725/// the:
726/// (G_ADD $src1, $src2)
727/// subpattern.
728class InstructionOperandMatcher : public OperandPredicateMatcher {
729protected:
730 std::unique_ptr<InstructionMatcher> InsnMatcher;
731
732public:
733 InstructionOperandMatcher()
734 : OperandPredicateMatcher(OPM_Instruction),
735 InsnMatcher(new InstructionMatcher()) {}
736
737 static bool classof(const OperandPredicateMatcher *P) {
738 return P->getKind() == OPM_Instruction;
739 }
740
741 InstructionMatcher &getInsnMatcher() const { return *InsnMatcher; }
742
743 Optional<const OperandMatcher *>
744 getOptionalOperand(StringRef SymbolicName) const override {
745 assert(!SymbolicName.empty() && "Cannot lookup unnamed operand");
746 return InsnMatcher->getOptionalOperand(SymbolicName);
747 }
748
749 void emitCxxCaptureStmts(raw_ostream &OS, RuleMatcher &Rule,
750 StringRef OperandExpr) const override {
751 OS << "if (!" << OperandExpr + ".isReg())\n"
752 << " return false;\n";
753 std::string InsnVarName = Rule.defineInsnVar(
754 OS, *InsnMatcher,
755 ("*MRI.getVRegDef(" + OperandExpr + ".getReg())").str());
756 InsnMatcher->emitCxxCaptureStmts(OS, Rule, InsnVarName);
757 }
758
759 void emitCxxPredicateExpr(raw_ostream &OS, RuleMatcher &Rule,
760 StringRef OperandExpr) const override {
761 OperandExpr = Rule.getInsnVarName(*InsnMatcher);
762 OS << "(";
763 InsnMatcher->emitCxxPredicateExpr(OS, Rule, OperandExpr);
764 OS << ")\n";
765 }
766};
767
Daniel Sanders43c882c2017-02-01 10:53:10 +0000768//===- Actions ------------------------------------------------------------===//
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000769void OperandPlaceholder::emitCxxValueExpr(raw_ostream &OS) const {
770 switch (Kind) {
771 case OP_MatchReference:
772 OS << MatchReference.InsnMatcher->getOperand(MatchReference.SymbolicName)
773 .getOperandExpr(MatchReference.InsnVarName);
774 break;
775 case OP_Temporary:
776 OS << "TempOp" << Temporary.OpIdx;
777 break;
778 }
779}
Daniel Sanders43c882c2017-02-01 10:53:10 +0000780
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000781class OperandRenderer {
782public:
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000783 enum RendererKind { OR_Copy, OR_Register, OR_ComplexPattern };
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000784
785protected:
786 RendererKind Kind;
787
788public:
789 OperandRenderer(RendererKind Kind) : Kind(Kind) {}
790 virtual ~OperandRenderer() {}
791
792 RendererKind getKind() const { return Kind; }
793
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000794 virtual void emitCxxRenderStmts(raw_ostream &OS, RuleMatcher &Rule) const = 0;
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000795};
796
797/// A CopyRenderer emits code to copy a single operand from an existing
798/// instruction to the one being built.
799class CopyRenderer : public OperandRenderer {
800protected:
801 /// The matcher for the instruction that this operand is copied from.
802 /// This provides the facility for looking up an a operand by it's name so
803 /// that it can be used as a source for the instruction being built.
804 const InstructionMatcher &Matched;
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000805 /// The name of the operand.
806 const StringRef SymbolicName;
807
808public:
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000809 CopyRenderer(const InstructionMatcher &Matched, StringRef SymbolicName)
810 : OperandRenderer(OR_Copy), Matched(Matched), SymbolicName(SymbolicName) {
811 }
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000812
813 static bool classof(const OperandRenderer *R) {
814 return R->getKind() == OR_Copy;
815 }
816
817 const StringRef getSymbolicName() const { return SymbolicName; }
818
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000819 void emitCxxRenderStmts(raw_ostream &OS, RuleMatcher &Rule) const override {
820 const OperandMatcher &Operand = Matched.getOperand(SymbolicName);
821 StringRef InsnVarName =
822 Rule.getInsnVarName(Operand.getInstructionMatcher());
823 std::string OperandExpr = Operand.getOperandExpr(InsnVarName);
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000824 OS << " MIB.add(" << OperandExpr << "/*" << SymbolicName << "*/);\n";
825 }
826};
827
828/// Adds a specific physical register to the instruction being built.
829/// This is typically useful for WZR/XZR on AArch64.
830class AddRegisterRenderer : public OperandRenderer {
831protected:
832 const Record *RegisterDef;
833
834public:
835 AddRegisterRenderer(const Record *RegisterDef)
836 : OperandRenderer(OR_Register), RegisterDef(RegisterDef) {}
837
838 static bool classof(const OperandRenderer *R) {
839 return R->getKind() == OR_Register;
840 }
841
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000842 void emitCxxRenderStmts(raw_ostream &OS, RuleMatcher &Rule) const override {
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000843 OS << " MIB.addReg(" << RegisterDef->getValueAsString("Namespace")
844 << "::" << RegisterDef->getName() << ");\n";
845 }
846};
847
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000848class RenderComplexPatternOperand : public OperandRenderer {
849private:
850 const Record &TheDef;
851 std::vector<OperandPlaceholder> Sources;
852
853 unsigned getNumOperands() const {
854 return TheDef.getValueAsDag("Operands")->getNumArgs();
855 }
856
857public:
858 RenderComplexPatternOperand(const Record &TheDef,
859 const ArrayRef<OperandPlaceholder> Sources)
860 : OperandRenderer(OR_ComplexPattern), TheDef(TheDef), Sources(Sources) {}
861
862 static bool classof(const OperandRenderer *R) {
863 return R->getKind() == OR_ComplexPattern;
864 }
865
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000866 void emitCxxRenderStmts(raw_ostream &OS, RuleMatcher &Rule) const override {
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000867 assert(Sources.size() == getNumOperands() && "Inconsistent number of operands");
868 for (const auto &Source : Sources) {
869 OS << "MIB.add(";
870 Source.emitCxxValueExpr(OS);
871 OS << ");\n";
872 }
873 }
874};
875
Ahmed Bougacha56ca3a92017-02-04 00:47:10 +0000876/// An action taken when all Matcher predicates succeeded for a parent rule.
877///
878/// Typical actions include:
879/// * Changing the opcode of an instruction.
880/// * Adding an operand to an instruction.
Daniel Sanders43c882c2017-02-01 10:53:10 +0000881class MatchAction {
882public:
883 virtual ~MatchAction() {}
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000884
885 /// Emit the C++ statements to implement the action.
886 ///
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000887 /// \param RecycleVarName If given, it's an instruction to recycle. The
888 /// requirements on the instruction vary from action to
889 /// action.
890 virtual void emitCxxActionStmts(raw_ostream &OS, RuleMatcher &Rule,
891 StringRef RecycleVarName) const = 0;
Daniel Sanders43c882c2017-02-01 10:53:10 +0000892};
893
Ahmed Bougacha9aa4c102017-02-04 00:47:08 +0000894/// Generates a comment describing the matched rule being acted upon.
895class DebugCommentAction : public MatchAction {
896private:
897 const PatternToMatch &P;
898
899public:
900 DebugCommentAction(const PatternToMatch &P) : P(P) {}
901
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000902 void emitCxxActionStmts(raw_ostream &OS, RuleMatcher &Rule,
903 StringRef RecycleVarName) const override {
904 OS << "// " << *P.getSrcPattern() << " => " << *P.getDstPattern() << "\n";
Ahmed Bougacha9aa4c102017-02-04 00:47:08 +0000905 }
906};
907
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000908/// Generates code to build an instruction or mutate an existing instruction
909/// into the desired instruction when this is possible.
910class BuildMIAction : public MatchAction {
Daniel Sanders43c882c2017-02-01 10:53:10 +0000911private:
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000912 const CodeGenInstruction *I;
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000913 const InstructionMatcher &Matched;
914 std::vector<std::unique_ptr<OperandRenderer>> OperandRenderers;
915
916 /// True if the instruction can be built solely by mutating the opcode.
917 bool canMutate() const {
918 for (const auto &Renderer : enumerate(OperandRenderers)) {
Zachary Turner309a0882017-03-13 16:24:10 +0000919 if (const auto *Copy = dyn_cast<CopyRenderer>(&*Renderer.value())) {
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000920 if (Matched.getOperand(Copy->getSymbolicName()).getOperandIndex() !=
Zachary Turner309a0882017-03-13 16:24:10 +0000921 Renderer.index())
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000922 return false;
923 } else
924 return false;
925 }
926
927 return true;
928 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000929
Daniel Sanders43c882c2017-02-01 10:53:10 +0000930public:
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000931 BuildMIAction(const CodeGenInstruction *I, const InstructionMatcher &Matched)
932 : I(I), Matched(Matched) {}
Daniel Sanders43c882c2017-02-01 10:53:10 +0000933
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000934 template <class Kind, class... Args>
935 Kind &addRenderer(Args&&... args) {
936 OperandRenderers.emplace_back(
937 llvm::make_unique<Kind>(std::forward<Args>(args)...));
938 return *static_cast<Kind *>(OperandRenderers.back().get());
939 }
940
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000941 void emitCxxActionStmts(raw_ostream &OS, RuleMatcher &Rule,
942 StringRef RecycleVarName) const override {
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000943 if (canMutate()) {
Tim Northover4340d642017-03-20 21:58:23 +0000944 OS << " " << RecycleVarName << ".setDesc(TII.get(" << I->Namespace
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000945 << "::" << I->TheDef->getName() << "));\n";
Tim Northover4340d642017-03-20 21:58:23 +0000946
947 if (!I->ImplicitDefs.empty() || !I->ImplicitUses.empty()) {
948 OS << " auto MIB = MachineInstrBuilder(MF, &" << RecycleVarName
949 << ");\n";
950
951 for (auto Def : I->ImplicitDefs) {
952 auto Namespace = Def->getValueAsString("Namespace");
953 OS << " MIB.addDef(" << Namespace << "::" << Def->getName()
954 << ", RegState::Implicit);\n";
955 }
956 for (auto Use : I->ImplicitUses) {
957 auto Namespace = Use->getValueAsString("Namespace");
958 OS << " MIB.addUse(" << Namespace << "::" << Use->getName()
959 << ", RegState::Implicit);\n";
960 }
961 }
962
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000963 OS << " MachineInstr &NewI = " << RecycleVarName << ";\n";
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000964 return;
965 }
966
967 // TODO: Simple permutation looks like it could be almost as common as
968 // mutation due to commutative operations.
969
970 OS << "MachineInstrBuilder MIB = BuildMI(*I.getParent(), I, "
971 "I.getDebugLoc(), TII.get("
972 << I->Namespace << "::" << I->TheDef->getName() << "));\n";
973 for (const auto &Renderer : OperandRenderers)
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000974 Renderer->emitCxxRenderStmts(OS, Rule);
Daniel Sandersbee57392017-04-04 13:25:23 +0000975 OS << " for (const auto *FromMI : ";
976 Rule.emitCxxCapturedInsnList(OS);
977 OS << ")\n";
978 OS << " for (const auto &MMO : FromMI->memoperands())\n";
979 OS << " MIB.addMemOperand(MMO);\n";
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000980 OS << " " << RecycleVarName << ".eraseFromParent();\n";
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000981 OS << " MachineInstr &NewI = *MIB;\n";
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000982 }
983};
984
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000985InstructionMatcher &RuleMatcher::addInstructionMatcher() {
986 Matchers.emplace_back(new InstructionMatcher());
987 return *Matchers.back();
988}
Ahmed Bougacha56ca3a92017-02-04 00:47:10 +0000989
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000990template <class Kind, class... Args>
991Kind &RuleMatcher::addAction(Args &&... args) {
992 Actions.emplace_back(llvm::make_unique<Kind>(std::forward<Args>(args)...));
993 return *static_cast<Kind *>(Actions.back().get());
994}
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000995
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000996std::string RuleMatcher::defineInsnVar(raw_ostream &OS,
997 const InstructionMatcher &Matcher,
998 StringRef Value) {
999 std::string InsnVarName = "MI" + llvm::to_string(NextInsnVarID++);
1000 OS << "MachineInstr &" << InsnVarName << " = " << Value << ";\n";
1001 InsnVariableNames[&Matcher] = InsnVarName;
1002 return InsnVarName;
1003}
1004
1005StringRef RuleMatcher::getInsnVarName(const InstructionMatcher &InsnMatcher) const {
1006 const auto &I = InsnVariableNames.find(&InsnMatcher);
1007 if (I != InsnVariableNames.end())
1008 return I->second;
1009 llvm_unreachable("Matched Insn was not captured in a local variable");
1010}
1011
Daniel Sandersbee57392017-04-04 13:25:23 +00001012/// Emit a C++ initializer_list containing references to every matched instruction.
1013void RuleMatcher::emitCxxCapturedInsnList(raw_ostream &OS) {
Daniel Sanders9e4817d2017-04-04 14:27:06 +00001014 SmallVector<StringRef, 2> Names;
Daniel Sandersbee57392017-04-04 13:25:23 +00001015 for (const auto &Pair : InsnVariableNames)
Daniel Sanders9e4817d2017-04-04 14:27:06 +00001016 Names.push_back(Pair.second);
1017 std::sort(Names.begin(), Names.end());
1018
1019 OS << "{";
1020 for (const auto &Name : Names)
1021 OS << "&" << Name << ", ";
Daniel Sandersbee57392017-04-04 13:25:23 +00001022 OS << "}";
1023}
1024
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001025/// Emit C++ statements to check the shape of the match and capture
1026/// instructions into local variables.
1027void RuleMatcher::emitCxxCaptureStmts(raw_ostream &OS, StringRef Expr) {
1028 assert(Matchers.size() == 1 && "Cannot handle multi-root matchers yet");
1029 std::string InsnVarName = defineInsnVar(OS, *Matchers.front(), Expr);
1030 Matchers.front()->emitCxxCaptureStmts(OS, *this, InsnVarName);
1031}
1032
1033void RuleMatcher::emit(raw_ostream &OS) {
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001034 if (Matchers.empty())
1035 llvm_unreachable("Unexpected empty matcher!");
Daniel Sandersdc662ff2017-01-26 11:10:14 +00001036
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001037 // The representation supports rules that require multiple roots such as:
1038 // %ptr(p0) = ...
1039 // %elt0(s32) = G_LOAD %ptr
1040 // %1(p0) = G_ADD %ptr, 4
1041 // %elt1(s32) = G_LOAD p0 %1
1042 // which could be usefully folded into:
1043 // %ptr(p0) = ...
1044 // %elt0(s32), %elt1(s32) = TGT_LOAD_PAIR %ptr
1045 // on some targets but we don't need to make use of that yet.
1046 assert(Matchers.size() == 1 && "Cannot handle multi-root matchers yet");
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001047 OS << "if ([&]() {\n";
1048
1049 emitCxxCaptureStmts(OS, "I");
1050
1051 OS << " if (";
1052 Matchers.front()->emitCxxPredicateExpr(OS, *this,
1053 getInsnVarName(*Matchers.front()));
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001054 OS << ") {\n";
1055
Daniel Sandersbee57392017-04-04 13:25:23 +00001056 // We must also check if it's safe to fold the matched instructions.
1057 if (InsnVariableNames.size() >= 2) {
1058 for (const auto &Pair : InsnVariableNames) {
1059 // Skip the root node since it isn't moving anywhere. Everything else is
1060 // sinking to meet it.
1061 if (Pair.first == Matchers.front().get())
1062 continue;
1063
1064 // Reject the difficult cases until we have a more accurate check.
1065 OS << " if (!isObviouslySafeToFold(" << Pair.second
1066 << ")) return false;\n";
1067
1068 // FIXME: Emit checks to determine it's _actually_ safe to fold and/or
1069 // account for unsafe cases.
1070 //
1071 // Example:
1072 // MI1--> %0 = ...
1073 // %1 = ... %0
1074 // MI0--> %2 = ... %0
1075 // It's not safe to erase MI1. We currently handle this by not
1076 // erasing %0 (even when it's dead).
1077 //
1078 // Example:
1079 // MI1--> %0 = load volatile @a
1080 // %1 = load volatile @a
1081 // MI0--> %2 = ... %0
1082 // It's not safe to sink %0's def past %1. We currently handle
1083 // this by rejecting all loads.
1084 //
1085 // Example:
1086 // MI1--> %0 = load @a
1087 // %1 = store @a
1088 // MI0--> %2 = ... %0
1089 // It's not safe to sink %0's def past %1. We currently handle
1090 // this by rejecting all loads.
1091 //
1092 // Example:
1093 // G_CONDBR %cond, @BB1
1094 // BB0:
1095 // MI1--> %0 = load @a
1096 // G_BR @BB1
1097 // BB1:
1098 // MI0--> %2 = ... %0
1099 // It's not always safe to sink %0 across control flow. In this
1100 // case it may introduce a memory fault. We currentl handle this
1101 // by rejecting all loads.
1102 }
1103 }
1104
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001105 for (const auto &MA : Actions) {
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001106 MA->emitCxxActionStmts(OS, *this, "I");
Daniel Sandersdc662ff2017-01-26 11:10:14 +00001107 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001108
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001109 OS << " constrainSelectedInstRegOperands(NewI, TII, TRI, RBI);\n";
1110 OS << " return true;\n";
1111 OS << " }\n";
1112 OS << " return false;\n";
1113 OS << " }()) { return true; }\n\n";
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001114}
Daniel Sanders43c882c2017-02-01 10:53:10 +00001115
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001116bool RuleMatcher::isHigherPriorityThan(const RuleMatcher &B) const {
1117 // Rules involving more match roots have higher priority.
1118 if (Matchers.size() > B.Matchers.size())
1119 return true;
1120 if (Matchers.size() < B.Matchers.size())
Daniel Sanders759ff412017-02-24 13:58:11 +00001121 return false;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001122
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001123 for (const auto &Matcher : zip(Matchers, B.Matchers)) {
1124 if (std::get<0>(Matcher)->isHigherPriorityThan(*std::get<1>(Matcher)))
1125 return true;
1126 if (std::get<1>(Matcher)->isHigherPriorityThan(*std::get<0>(Matcher)))
1127 return false;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001128 }
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001129
1130 return false;
Simon Pilgrima7d1da82017-03-15 22:50:47 +00001131}
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001132
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001133unsigned RuleMatcher::countTemporaryOperands() const {
1134 return std::accumulate(
1135 Matchers.begin(), Matchers.end(), 0,
1136 [](unsigned A, const std::unique_ptr<InstructionMatcher> &Matcher) {
1137 return A + Matcher->countTemporaryOperands();
1138 });
1139}
1140
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001141//===- GlobalISelEmitter class --------------------------------------------===//
1142
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001143class GlobalISelEmitter {
1144public:
1145 explicit GlobalISelEmitter(RecordKeeper &RK);
1146 void run(raw_ostream &OS);
1147
1148private:
1149 const RecordKeeper &RK;
1150 const CodeGenDAGPatterns CGP;
1151 const CodeGenTarget &Target;
1152
1153 /// Keep track of the equivalence between SDNodes and Instruction.
1154 /// This is defined using 'GINodeEquiv' in the target description.
1155 DenseMap<Record *, const CodeGenInstruction *> NodeEquivs;
1156
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001157 /// Keep track of the equivalence between ComplexPattern's and
1158 /// GIComplexOperandMatcher. Map entries are specified by subclassing
1159 /// GIComplexPatternEquiv.
1160 DenseMap<const Record *, const Record *> ComplexPatternEquivs;
1161
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001162 void gatherNodeEquivs();
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001163 const CodeGenInstruction *findNodeEquiv(Record *N) const;
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001164
Daniel Sandersc270c502017-03-30 09:36:33 +00001165 Error importRulePredicates(RuleMatcher &M, ArrayRef<Init *> Predicates) const;
Daniel Sandersffc7d582017-03-29 15:37:18 +00001166 Expected<InstructionMatcher &>
Daniel Sandersc270c502017-03-30 09:36:33 +00001167 createAndImportSelDAGMatcher(InstructionMatcher &InsnMatcher,
1168 const TreePatternNode *Src) const;
1169 Error importChildMatcher(InstructionMatcher &InsnMatcher,
1170 TreePatternNode *SrcChild, unsigned OpIdx,
1171 unsigned &TempOpIdx) const;
1172 Expected<BuildMIAction &> createAndImportInstructionRenderer(
1173 RuleMatcher &M, const TreePatternNode *Dst,
1174 const InstructionMatcher &InsnMatcher) const;
1175 Error importExplicitUseRenderer(BuildMIAction &DstMIBuilder,
1176 TreePatternNode *DstChild,
1177 const InstructionMatcher &InsnMatcher,
1178 unsigned &TempOpIdx) const;
1179 Error
Daniel Sandersffc7d582017-03-29 15:37:18 +00001180 importImplicitDefRenderers(BuildMIAction &DstMIBuilder,
1181 const std::vector<Record *> &ImplicitDefs) const;
1182
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001183 /// Analyze pattern \p P, returning a matcher for it if possible.
1184 /// Otherwise, return an Error explaining why we don't support it.
1185 Expected<RuleMatcher> runOnPattern(const PatternToMatch &P);
1186};
1187
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001188void GlobalISelEmitter::gatherNodeEquivs() {
1189 assert(NodeEquivs.empty());
1190 for (Record *Equiv : RK.getAllDerivedDefinitions("GINodeEquiv"))
1191 NodeEquivs[Equiv->getValueAsDef("Node")] =
1192 &Target.getInstruction(Equiv->getValueAsDef("I"));
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001193
1194 assert(ComplexPatternEquivs.empty());
1195 for (Record *Equiv : RK.getAllDerivedDefinitions("GIComplexPatternEquiv")) {
1196 Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent");
1197 if (!SelDAGEquiv)
1198 continue;
1199 ComplexPatternEquivs[SelDAGEquiv] = Equiv;
1200 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001201}
1202
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001203const CodeGenInstruction *GlobalISelEmitter::findNodeEquiv(Record *N) const {
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001204 return NodeEquivs.lookup(N);
1205}
1206
1207GlobalISelEmitter::GlobalISelEmitter(RecordKeeper &RK)
1208 : RK(RK), CGP(RK), Target(CGP.getTargetInfo()) {}
1209
1210//===- Emitter ------------------------------------------------------------===//
1211
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001212/// Helper function to let the emitter report skip reason error messages.
1213static Error failedImport(const Twine &Reason) {
1214 return make_error<StringError>(Reason, inconvertibleErrorCode());
1215}
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001216
Daniel Sandersc270c502017-03-30 09:36:33 +00001217Error
Daniel Sandersffc7d582017-03-29 15:37:18 +00001218GlobalISelEmitter::importRulePredicates(RuleMatcher &M,
1219 ArrayRef<Init *> Predicates) const {
1220 if (!Predicates.empty())
1221 return failedImport("Pattern has a predicate");
Daniel Sandersc270c502017-03-30 09:36:33 +00001222 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001223}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001224
Daniel Sandersc270c502017-03-30 09:36:33 +00001225Expected<InstructionMatcher &> GlobalISelEmitter::createAndImportSelDAGMatcher(
1226 InstructionMatcher &InsnMatcher, const TreePatternNode *Src) const {
Daniel Sandersffc7d582017-03-29 15:37:18 +00001227 // Start with the defined operands (i.e., the results of the root operator).
1228 if (Src->getExtTypes().size() > 1)
1229 return failedImport("Src pattern has multiple results");
1230
1231 auto SrcGIOrNull = findNodeEquiv(Src->getOperator());
1232 if (!SrcGIOrNull)
1233 return failedImport("Pattern operator lacks an equivalent Instruction");
1234 auto &SrcGI = *SrcGIOrNull;
1235
1236 // The operators look good: match the opcode and mutate it to the new one.
1237 InsnMatcher.addPredicate<InstructionOpcodeMatcher>(&SrcGI);
1238
1239 unsigned OpIdx = 0;
1240 for (const EEVT::TypeSet &Ty : Src->getExtTypes()) {
1241 auto OpTyOrNone = MVTToLLT(Ty.getConcrete());
1242
1243 if (!OpTyOrNone)
1244 return failedImport(
1245 "Result of Src pattern operator has an unsupported type");
1246
1247 // Results don't have a name unless they are the root node. The caller will
1248 // set the name if appropriate.
1249 OperandMatcher &OM = InsnMatcher.addOperand(OpIdx++, "");
1250 OM.addPredicate<LLTOperandMatcher>(*OpTyOrNone);
1251 }
1252
1253 unsigned TempOpIdx = 0;
1254 // Match the used operands (i.e. the children of the operator).
1255 for (unsigned i = 0, e = Src->getNumChildren(); i != e; ++i) {
1256 if (auto Error = importChildMatcher(InsnMatcher, Src->getChild(i), OpIdx++,
Daniel Sandersc270c502017-03-30 09:36:33 +00001257 TempOpIdx))
Daniel Sandersffc7d582017-03-29 15:37:18 +00001258 return std::move(Error);
1259 }
1260
1261 return InsnMatcher;
1262}
1263
Daniel Sandersc270c502017-03-30 09:36:33 +00001264Error GlobalISelEmitter::importChildMatcher(InstructionMatcher &InsnMatcher,
1265 TreePatternNode *SrcChild,
1266 unsigned OpIdx,
1267 unsigned &TempOpIdx) const {
Daniel Sandersffc7d582017-03-29 15:37:18 +00001268 OperandMatcher &OM = InsnMatcher.addOperand(OpIdx, SrcChild->getName());
1269
1270 if (SrcChild->hasAnyPredicate())
1271 return failedImport("Src pattern child has predicate");
1272
1273 ArrayRef<EEVT::TypeSet> ChildTypes = SrcChild->getExtTypes();
1274 if (ChildTypes.size() != 1)
1275 return failedImport("Src pattern child has multiple results");
1276
1277 // Check MBB's before the type check since they are not a known type.
1278 if (!SrcChild->isLeaf()) {
1279 if (SrcChild->getOperator()->isSubClassOf("SDNode")) {
1280 auto &ChildSDNI = CGP.getSDNodeInfo(SrcChild->getOperator());
1281 if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") {
1282 OM.addPredicate<MBBOperandMatcher>();
Daniel Sandersc270c502017-03-30 09:36:33 +00001283 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001284 }
1285 }
Daniel Sandersffc7d582017-03-29 15:37:18 +00001286 }
1287
1288 auto OpTyOrNone = MVTToLLT(ChildTypes.front().getConcrete());
1289 if (!OpTyOrNone)
1290 return failedImport("Src operand has an unsupported type");
1291 OM.addPredicate<LLTOperandMatcher>(*OpTyOrNone);
1292
Daniel Sandersbee57392017-04-04 13:25:23 +00001293 // Check for nested instructions.
1294 if (!SrcChild->isLeaf()) {
1295 // Map the node to a gMIR instruction.
1296 InstructionOperandMatcher &InsnOperand =
1297 OM.addPredicate<InstructionOperandMatcher>();
1298 auto InsnMatcherOrError =
1299 createAndImportSelDAGMatcher(InsnOperand.getInsnMatcher(), SrcChild);
1300 if (auto Error = InsnMatcherOrError.takeError())
1301 return Error;
1302
1303 return Error::success();
1304 }
1305
Daniel Sandersffc7d582017-03-29 15:37:18 +00001306 // Check for constant immediates.
1307 if (auto *ChildInt = dyn_cast<IntInit>(SrcChild->getLeafValue())) {
1308 OM.addPredicate<IntOperandMatcher>(ChildInt->getValue());
Daniel Sandersc270c502017-03-30 09:36:33 +00001309 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001310 }
1311
1312 // Check for def's like register classes or ComplexPattern's.
1313 if (auto *ChildDefInit = dyn_cast<DefInit>(SrcChild->getLeafValue())) {
1314 auto *ChildRec = ChildDefInit->getDef();
1315
1316 // Check for register classes.
1317 if (ChildRec->isSubClassOf("RegisterClass")) {
1318 OM.addPredicate<RegisterBankOperandMatcher>(
1319 Target.getRegisterClass(ChildRec));
Daniel Sandersc270c502017-03-30 09:36:33 +00001320 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001321 }
1322
1323 // Check for ComplexPattern's.
1324 if (ChildRec->isSubClassOf("ComplexPattern")) {
1325 const auto &ComplexPattern = ComplexPatternEquivs.find(ChildRec);
1326 if (ComplexPattern == ComplexPatternEquivs.end())
1327 return failedImport(
1328 "SelectionDAG ComplexPattern not mapped to GlobalISel");
1329
1330 const auto &Predicate = OM.addPredicate<ComplexPatternOperandMatcher>(
1331 *ComplexPattern->second, TempOpIdx);
1332 TempOpIdx += Predicate.countTemporaryOperands();
Daniel Sandersc270c502017-03-30 09:36:33 +00001333 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001334 }
1335
1336 return failedImport(
1337 "Src pattern child def is an unsupported tablegen class");
1338 }
1339
1340 return failedImport("Src pattern child is an unsupported kind");
1341}
1342
Daniel Sandersc270c502017-03-30 09:36:33 +00001343Error GlobalISelEmitter::importExplicitUseRenderer(
Daniel Sandersffc7d582017-03-29 15:37:18 +00001344 BuildMIAction &DstMIBuilder, TreePatternNode *DstChild,
1345 const InstructionMatcher &InsnMatcher, unsigned &TempOpIdx) const {
1346 // The only non-leaf child we accept is 'bb': it's an operator because
1347 // BasicBlockSDNode isn't inline, but in MI it's just another operand.
1348 if (!DstChild->isLeaf()) {
1349 if (DstChild->getOperator()->isSubClassOf("SDNode")) {
1350 auto &ChildSDNI = CGP.getSDNodeInfo(DstChild->getOperator());
1351 if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") {
1352 DstMIBuilder.addRenderer<CopyRenderer>(InsnMatcher,
1353 DstChild->getName());
Daniel Sandersc270c502017-03-30 09:36:33 +00001354 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001355 }
1356 }
1357 return failedImport("Dst pattern child isn't a leaf node or an MBB");
1358 }
1359
1360 // Otherwise, we're looking for a bog-standard RegisterClass operand.
1361 if (DstChild->hasAnyPredicate())
1362 return failedImport("Dst pattern child has predicate");
1363
1364 if (auto *ChildDefInit = dyn_cast<DefInit>(DstChild->getLeafValue())) {
1365 auto *ChildRec = ChildDefInit->getDef();
1366
1367 ArrayRef<EEVT::TypeSet> ChildTypes = DstChild->getExtTypes();
1368 if (ChildTypes.size() != 1)
1369 return failedImport("Dst pattern child has multiple results");
1370
1371 auto OpTyOrNone = MVTToLLT(ChildTypes.front().getConcrete());
1372 if (!OpTyOrNone)
1373 return failedImport("Dst operand has an unsupported type");
1374
1375 if (ChildRec->isSubClassOf("Register")) {
1376 DstMIBuilder.addRenderer<AddRegisterRenderer>(ChildRec);
Daniel Sandersc270c502017-03-30 09:36:33 +00001377 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001378 }
1379
1380 if (ChildRec->isSubClassOf("RegisterClass")) {
1381 DstMIBuilder.addRenderer<CopyRenderer>(InsnMatcher, DstChild->getName());
Daniel Sandersc270c502017-03-30 09:36:33 +00001382 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001383 }
1384
1385 if (ChildRec->isSubClassOf("ComplexPattern")) {
1386 const auto &ComplexPattern = ComplexPatternEquivs.find(ChildRec);
1387 if (ComplexPattern == ComplexPatternEquivs.end())
1388 return failedImport(
1389 "SelectionDAG ComplexPattern not mapped to GlobalISel");
1390
1391 SmallVector<OperandPlaceholder, 2> RenderedOperands;
1392 for (unsigned I = 0;
1393 I <
1394 InsnMatcher.getOperand(DstChild->getName()).countTemporaryOperands();
1395 ++I) {
1396 RenderedOperands.push_back(OperandPlaceholder::CreateTemporary(I));
1397 TempOpIdx++;
1398 }
1399 DstMIBuilder.addRenderer<RenderComplexPatternOperand>(
1400 *ComplexPattern->second, RenderedOperands);
Daniel Sandersc270c502017-03-30 09:36:33 +00001401 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001402 }
1403
1404 return failedImport(
1405 "Dst pattern child def is an unsupported tablegen class");
1406 }
1407
1408 return failedImport("Dst pattern child is an unsupported kind");
1409}
1410
Daniel Sandersc270c502017-03-30 09:36:33 +00001411Expected<BuildMIAction &> GlobalISelEmitter::createAndImportInstructionRenderer(
Daniel Sandersffc7d582017-03-29 15:37:18 +00001412 RuleMatcher &M, const TreePatternNode *Dst,
1413 const InstructionMatcher &InsnMatcher) const {
1414 Record *DstOp = Dst->getOperator();
1415 if (!DstOp->isSubClassOf("Instruction"))
1416 return failedImport("Pattern operator isn't an instruction");
1417 auto &DstI = Target.getInstruction(DstOp);
1418
1419 auto &DstMIBuilder = M.addAction<BuildMIAction>(&DstI, InsnMatcher);
1420
1421 // Render the explicit defs.
1422 for (unsigned I = 0; I < DstI.Operands.NumDefs; ++I) {
1423 const auto &DstIOperand = DstI.Operands[I];
1424 DstMIBuilder.addRenderer<CopyRenderer>(InsnMatcher, DstIOperand.Name);
1425 }
1426
1427 // Render the explicit uses.
1428 unsigned TempOpIdx = 0;
1429 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
1430 if (auto Error = importExplicitUseRenderer(DstMIBuilder, Dst->getChild(i),
Daniel Sandersc270c502017-03-30 09:36:33 +00001431 InsnMatcher, TempOpIdx))
Daniel Sandersffc7d582017-03-29 15:37:18 +00001432 return std::move(Error);
1433 }
1434
1435 return DstMIBuilder;
1436}
1437
Daniel Sandersc270c502017-03-30 09:36:33 +00001438Error GlobalISelEmitter::importImplicitDefRenderers(
Daniel Sandersffc7d582017-03-29 15:37:18 +00001439 BuildMIAction &DstMIBuilder,
1440 const std::vector<Record *> &ImplicitDefs) const {
1441 if (!ImplicitDefs.empty())
1442 return failedImport("Pattern defines a physical register");
Daniel Sandersc270c502017-03-30 09:36:33 +00001443 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001444}
1445
1446Expected<RuleMatcher> GlobalISelEmitter::runOnPattern(const PatternToMatch &P) {
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001447 // Keep track of the matchers and actions to emit.
Ahmed Bougacha9aa4c102017-02-04 00:47:08 +00001448 RuleMatcher M;
1449 M.addAction<DebugCommentAction>(P);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001450
Daniel Sandersc270c502017-03-30 09:36:33 +00001451 if (auto Error = importRulePredicates(M, P.getPredicates()->getValues()))
Daniel Sandersffc7d582017-03-29 15:37:18 +00001452 return std::move(Error);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001453
1454 // Next, analyze the pattern operators.
1455 TreePatternNode *Src = P.getSrcPattern();
1456 TreePatternNode *Dst = P.getDstPattern();
1457
1458 // If the root of either pattern isn't a simple operator, ignore it.
1459 if (!isTrivialOperatorNode(Dst))
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001460 return failedImport("Dst pattern root isn't a trivial operator");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001461 if (!isTrivialOperatorNode(Src))
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001462 return failedImport("Src pattern root isn't a trivial operator");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001463
Daniel Sandersbee57392017-04-04 13:25:23 +00001464 // Start with the defined operands (i.e., the results of the root operator).
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001465 Record *DstOp = Dst->getOperator();
1466 if (!DstOp->isSubClassOf("Instruction"))
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001467 return failedImport("Pattern operator isn't an instruction");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001468
1469 auto &DstI = Target.getInstruction(DstOp);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001470 if (DstI.Operands.NumDefs != Src->getExtTypes().size())
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001471 return failedImport("Src pattern results and dst MI defs are different");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001472
Daniel Sandersffc7d582017-03-29 15:37:18 +00001473 InstructionMatcher &InsnMatcherTemp = M.addInstructionMatcher();
Daniel Sandersc270c502017-03-30 09:36:33 +00001474 auto InsnMatcherOrError = createAndImportSelDAGMatcher(InsnMatcherTemp, Src);
Daniel Sandersffc7d582017-03-29 15:37:18 +00001475 if (auto Error = InsnMatcherOrError.takeError())
1476 return std::move(Error);
1477 InstructionMatcher &InsnMatcher = InsnMatcherOrError.get();
1478
1479 // The root of the match also has constraints on the register bank so that it
1480 // matches the result instruction.
1481 unsigned OpIdx = 0;
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001482 for (const EEVT::TypeSet &Ty : Src->getExtTypes()) {
Daniel Sandersffc7d582017-03-29 15:37:18 +00001483 (void)Ty;
1484
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001485 const auto &DstIOperand = DstI.Operands[OpIdx];
1486 Record *DstIOpRec = DstIOperand.Rec;
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001487 if (!DstIOpRec->isSubClassOf("RegisterClass"))
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001488 return failedImport("Dst MI def isn't a register class");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001489
Daniel Sandersffc7d582017-03-29 15:37:18 +00001490 OperandMatcher &OM = InsnMatcher.getOperand(OpIdx);
1491 OM.setSymbolicName(DstIOperand.Name);
Daniel Sandersdc662ff2017-01-26 11:10:14 +00001492 OM.addPredicate<RegisterBankOperandMatcher>(
1493 Target.getRegisterClass(DstIOpRec));
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001494 ++OpIdx;
1495 }
1496
Daniel Sandersc270c502017-03-30 09:36:33 +00001497 auto DstMIBuilderOrError =
1498 createAndImportInstructionRenderer(M, Dst, InsnMatcher);
Daniel Sandersffc7d582017-03-29 15:37:18 +00001499 if (auto Error = DstMIBuilderOrError.takeError())
1500 return std::move(Error);
1501 BuildMIAction &DstMIBuilder = DstMIBuilderOrError.get();
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001502
Daniel Sandersffc7d582017-03-29 15:37:18 +00001503 // Render the implicit defs.
1504 // These are only added to the root of the result.
Daniel Sandersc270c502017-03-30 09:36:33 +00001505 if (auto Error = importImplicitDefRenderers(DstMIBuilder, P.getDstRegs()))
Daniel Sandersffc7d582017-03-29 15:37:18 +00001506 return std::move(Error);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001507
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001508 // We're done with this pattern! It's eligible for GISel emission; return it.
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00001509 ++NumPatternImported;
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001510 return std::move(M);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001511}
1512
1513void GlobalISelEmitter::run(raw_ostream &OS) {
1514 // Track the GINodeEquiv definitions.
1515 gatherNodeEquivs();
1516
1517 emitSourceFileHeader(("Global Instruction Selector for the " +
1518 Target.getName() + " target").str(), OS);
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00001519 std::vector<RuleMatcher> Rules;
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001520 // Look through the SelectionDAG patterns we found, possibly emitting some.
1521 for (const PatternToMatch &Pat : CGP.ptms()) {
1522 ++NumPatternTotal;
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001523 auto MatcherOrErr = runOnPattern(Pat);
1524
1525 // The pattern analysis can fail, indicating an unsupported pattern.
1526 // Report that if we've been asked to do so.
1527 if (auto Err = MatcherOrErr.takeError()) {
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001528 if (WarnOnSkippedPatterns) {
1529 PrintWarning(Pat.getSrcRecord()->getLoc(),
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001530 "Skipped pattern: " + toString(std::move(Err)));
1531 } else {
1532 consumeError(std::move(Err));
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001533 }
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00001534 ++NumPatternImportsSkipped;
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001535 continue;
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001536 }
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001537
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00001538 Rules.push_back(std::move(MatcherOrErr.get()));
1539 }
1540
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001541 std::stable_sort(Rules.begin(), Rules.end(),
1542 [&](const RuleMatcher &A, const RuleMatcher &B) {
Daniel Sanders759ff412017-02-24 13:58:11 +00001543 if (A.isHigherPriorityThan(B)) {
1544 assert(!B.isHigherPriorityThan(A) && "Cannot be more important "
1545 "and less important at "
1546 "the same time");
1547 return true;
1548 }
1549 return false;
1550 });
1551
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001552 unsigned MaxTemporaries = 0;
1553 for (const auto &Rule : Rules)
1554 MaxTemporaries = std::max(MaxTemporaries, Rule.countTemporaryOperands());
1555
1556 OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n";
1557 for (unsigned I = 0; I < MaxTemporaries; ++I)
1558 OS << " mutable MachineOperand TempOp" << I << ";\n";
1559 OS << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n\n";
1560
1561 OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n";
1562 for (unsigned I = 0; I < MaxTemporaries; ++I)
1563 OS << ", TempOp" << I << "(MachineOperand::CreatePlaceholder())\n";
1564 OS << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n\n";
1565
1566 OS << "#ifdef GET_GLOBALISEL_IMPL\n"
1567 << "bool " << Target.getName()
1568 << "InstructionSelector::selectImpl(MachineInstr &I) const {\n"
1569 << " MachineFunction &MF = *I.getParent()->getParent();\n"
1570 << " const MachineRegisterInfo &MRI = MF.getRegInfo();\n";
1571
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001572 for (auto &Rule : Rules) {
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00001573 Rule.emit(OS);
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001574 ++NumPatternEmitted;
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001575 }
1576
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001577 OS << " return false;\n"
1578 << "}\n"
1579 << "#endif // ifdef GET_GLOBALISEL_IMPL\n";
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001580}
1581
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001582} // end anonymous namespace
1583
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001584//===----------------------------------------------------------------------===//
1585
1586namespace llvm {
1587void EmitGlobalISel(RecordKeeper &RK, raw_ostream &OS) {
1588 GlobalISelEmitter(RK).run(OS);
1589}
1590} // End llvm namespace