blob: f581c8892341a4c7e0c438747e321c6aa5e81933 [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) {
1014 OS << "{";
1015 for (const auto &Pair : InsnVariableNames)
1016 OS << "&" << Pair.second << ", ";
1017 OS << "}";
1018}
1019
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001020/// Emit C++ statements to check the shape of the match and capture
1021/// instructions into local variables.
1022void RuleMatcher::emitCxxCaptureStmts(raw_ostream &OS, StringRef Expr) {
1023 assert(Matchers.size() == 1 && "Cannot handle multi-root matchers yet");
1024 std::string InsnVarName = defineInsnVar(OS, *Matchers.front(), Expr);
1025 Matchers.front()->emitCxxCaptureStmts(OS, *this, InsnVarName);
1026}
1027
1028void RuleMatcher::emit(raw_ostream &OS) {
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001029 if (Matchers.empty())
1030 llvm_unreachable("Unexpected empty matcher!");
Daniel Sandersdc662ff2017-01-26 11:10:14 +00001031
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001032 // The representation supports rules that require multiple roots such as:
1033 // %ptr(p0) = ...
1034 // %elt0(s32) = G_LOAD %ptr
1035 // %1(p0) = G_ADD %ptr, 4
1036 // %elt1(s32) = G_LOAD p0 %1
1037 // which could be usefully folded into:
1038 // %ptr(p0) = ...
1039 // %elt0(s32), %elt1(s32) = TGT_LOAD_PAIR %ptr
1040 // on some targets but we don't need to make use of that yet.
1041 assert(Matchers.size() == 1 && "Cannot handle multi-root matchers yet");
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001042 OS << "if ([&]() {\n";
1043
1044 emitCxxCaptureStmts(OS, "I");
1045
1046 OS << " if (";
1047 Matchers.front()->emitCxxPredicateExpr(OS, *this,
1048 getInsnVarName(*Matchers.front()));
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001049 OS << ") {\n";
1050
Daniel Sandersbee57392017-04-04 13:25:23 +00001051 // We must also check if it's safe to fold the matched instructions.
1052 if (InsnVariableNames.size() >= 2) {
1053 for (const auto &Pair : InsnVariableNames) {
1054 // Skip the root node since it isn't moving anywhere. Everything else is
1055 // sinking to meet it.
1056 if (Pair.first == Matchers.front().get())
1057 continue;
1058
1059 // Reject the difficult cases until we have a more accurate check.
1060 OS << " if (!isObviouslySafeToFold(" << Pair.second
1061 << ")) return false;\n";
1062
1063 // FIXME: Emit checks to determine it's _actually_ safe to fold and/or
1064 // account for unsafe cases.
1065 //
1066 // Example:
1067 // MI1--> %0 = ...
1068 // %1 = ... %0
1069 // MI0--> %2 = ... %0
1070 // It's not safe to erase MI1. We currently handle this by not
1071 // erasing %0 (even when it's dead).
1072 //
1073 // Example:
1074 // MI1--> %0 = load volatile @a
1075 // %1 = load volatile @a
1076 // MI0--> %2 = ... %0
1077 // It's not safe to sink %0's def past %1. We currently handle
1078 // this by rejecting all loads.
1079 //
1080 // Example:
1081 // MI1--> %0 = load @a
1082 // %1 = store @a
1083 // MI0--> %2 = ... %0
1084 // It's not safe to sink %0's def past %1. We currently handle
1085 // this by rejecting all loads.
1086 //
1087 // Example:
1088 // G_CONDBR %cond, @BB1
1089 // BB0:
1090 // MI1--> %0 = load @a
1091 // G_BR @BB1
1092 // BB1:
1093 // MI0--> %2 = ... %0
1094 // It's not always safe to sink %0 across control flow. In this
1095 // case it may introduce a memory fault. We currentl handle this
1096 // by rejecting all loads.
1097 }
1098 }
1099
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001100 for (const auto &MA : Actions) {
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001101 MA->emitCxxActionStmts(OS, *this, "I");
Daniel Sandersdc662ff2017-01-26 11:10:14 +00001102 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001103
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001104 OS << " constrainSelectedInstRegOperands(NewI, TII, TRI, RBI);\n";
1105 OS << " return true;\n";
1106 OS << " }\n";
1107 OS << " return false;\n";
1108 OS << " }()) { return true; }\n\n";
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001109}
Daniel Sanders43c882c2017-02-01 10:53:10 +00001110
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001111bool RuleMatcher::isHigherPriorityThan(const RuleMatcher &B) const {
1112 // Rules involving more match roots have higher priority.
1113 if (Matchers.size() > B.Matchers.size())
1114 return true;
1115 if (Matchers.size() < B.Matchers.size())
Daniel Sanders759ff412017-02-24 13:58:11 +00001116 return false;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001117
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001118 for (const auto &Matcher : zip(Matchers, B.Matchers)) {
1119 if (std::get<0>(Matcher)->isHigherPriorityThan(*std::get<1>(Matcher)))
1120 return true;
1121 if (std::get<1>(Matcher)->isHigherPriorityThan(*std::get<0>(Matcher)))
1122 return false;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001123 }
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001124
1125 return false;
Simon Pilgrima7d1da82017-03-15 22:50:47 +00001126}
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001127
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001128unsigned RuleMatcher::countTemporaryOperands() const {
1129 return std::accumulate(
1130 Matchers.begin(), Matchers.end(), 0,
1131 [](unsigned A, const std::unique_ptr<InstructionMatcher> &Matcher) {
1132 return A + Matcher->countTemporaryOperands();
1133 });
1134}
1135
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001136//===- GlobalISelEmitter class --------------------------------------------===//
1137
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001138class GlobalISelEmitter {
1139public:
1140 explicit GlobalISelEmitter(RecordKeeper &RK);
1141 void run(raw_ostream &OS);
1142
1143private:
1144 const RecordKeeper &RK;
1145 const CodeGenDAGPatterns CGP;
1146 const CodeGenTarget &Target;
1147
1148 /// Keep track of the equivalence between SDNodes and Instruction.
1149 /// This is defined using 'GINodeEquiv' in the target description.
1150 DenseMap<Record *, const CodeGenInstruction *> NodeEquivs;
1151
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001152 /// Keep track of the equivalence between ComplexPattern's and
1153 /// GIComplexOperandMatcher. Map entries are specified by subclassing
1154 /// GIComplexPatternEquiv.
1155 DenseMap<const Record *, const Record *> ComplexPatternEquivs;
1156
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001157 void gatherNodeEquivs();
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001158 const CodeGenInstruction *findNodeEquiv(Record *N) const;
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001159
Daniel Sandersc270c502017-03-30 09:36:33 +00001160 Error importRulePredicates(RuleMatcher &M, ArrayRef<Init *> Predicates) const;
Daniel Sandersffc7d582017-03-29 15:37:18 +00001161 Expected<InstructionMatcher &>
Daniel Sandersc270c502017-03-30 09:36:33 +00001162 createAndImportSelDAGMatcher(InstructionMatcher &InsnMatcher,
1163 const TreePatternNode *Src) const;
1164 Error importChildMatcher(InstructionMatcher &InsnMatcher,
1165 TreePatternNode *SrcChild, unsigned OpIdx,
1166 unsigned &TempOpIdx) const;
1167 Expected<BuildMIAction &> createAndImportInstructionRenderer(
1168 RuleMatcher &M, const TreePatternNode *Dst,
1169 const InstructionMatcher &InsnMatcher) const;
1170 Error importExplicitUseRenderer(BuildMIAction &DstMIBuilder,
1171 TreePatternNode *DstChild,
1172 const InstructionMatcher &InsnMatcher,
1173 unsigned &TempOpIdx) const;
1174 Error
Daniel Sandersffc7d582017-03-29 15:37:18 +00001175 importImplicitDefRenderers(BuildMIAction &DstMIBuilder,
1176 const std::vector<Record *> &ImplicitDefs) const;
1177
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001178 /// Analyze pattern \p P, returning a matcher for it if possible.
1179 /// Otherwise, return an Error explaining why we don't support it.
1180 Expected<RuleMatcher> runOnPattern(const PatternToMatch &P);
1181};
1182
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001183void GlobalISelEmitter::gatherNodeEquivs() {
1184 assert(NodeEquivs.empty());
1185 for (Record *Equiv : RK.getAllDerivedDefinitions("GINodeEquiv"))
1186 NodeEquivs[Equiv->getValueAsDef("Node")] =
1187 &Target.getInstruction(Equiv->getValueAsDef("I"));
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001188
1189 assert(ComplexPatternEquivs.empty());
1190 for (Record *Equiv : RK.getAllDerivedDefinitions("GIComplexPatternEquiv")) {
1191 Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent");
1192 if (!SelDAGEquiv)
1193 continue;
1194 ComplexPatternEquivs[SelDAGEquiv] = Equiv;
1195 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001196}
1197
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001198const CodeGenInstruction *GlobalISelEmitter::findNodeEquiv(Record *N) const {
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001199 return NodeEquivs.lookup(N);
1200}
1201
1202GlobalISelEmitter::GlobalISelEmitter(RecordKeeper &RK)
1203 : RK(RK), CGP(RK), Target(CGP.getTargetInfo()) {}
1204
1205//===- Emitter ------------------------------------------------------------===//
1206
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001207/// Helper function to let the emitter report skip reason error messages.
1208static Error failedImport(const Twine &Reason) {
1209 return make_error<StringError>(Reason, inconvertibleErrorCode());
1210}
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001211
Daniel Sandersc270c502017-03-30 09:36:33 +00001212Error
Daniel Sandersffc7d582017-03-29 15:37:18 +00001213GlobalISelEmitter::importRulePredicates(RuleMatcher &M,
1214 ArrayRef<Init *> Predicates) const {
1215 if (!Predicates.empty())
1216 return failedImport("Pattern has a predicate");
Daniel Sandersc270c502017-03-30 09:36:33 +00001217 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001218}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001219
Daniel Sandersc270c502017-03-30 09:36:33 +00001220Expected<InstructionMatcher &> GlobalISelEmitter::createAndImportSelDAGMatcher(
1221 InstructionMatcher &InsnMatcher, const TreePatternNode *Src) const {
Daniel Sandersffc7d582017-03-29 15:37:18 +00001222 // Start with the defined operands (i.e., the results of the root operator).
1223 if (Src->getExtTypes().size() > 1)
1224 return failedImport("Src pattern has multiple results");
1225
1226 auto SrcGIOrNull = findNodeEquiv(Src->getOperator());
1227 if (!SrcGIOrNull)
1228 return failedImport("Pattern operator lacks an equivalent Instruction");
1229 auto &SrcGI = *SrcGIOrNull;
1230
1231 // The operators look good: match the opcode and mutate it to the new one.
1232 InsnMatcher.addPredicate<InstructionOpcodeMatcher>(&SrcGI);
1233
1234 unsigned OpIdx = 0;
1235 for (const EEVT::TypeSet &Ty : Src->getExtTypes()) {
1236 auto OpTyOrNone = MVTToLLT(Ty.getConcrete());
1237
1238 if (!OpTyOrNone)
1239 return failedImport(
1240 "Result of Src pattern operator has an unsupported type");
1241
1242 // Results don't have a name unless they are the root node. The caller will
1243 // set the name if appropriate.
1244 OperandMatcher &OM = InsnMatcher.addOperand(OpIdx++, "");
1245 OM.addPredicate<LLTOperandMatcher>(*OpTyOrNone);
1246 }
1247
1248 unsigned TempOpIdx = 0;
1249 // Match the used operands (i.e. the children of the operator).
1250 for (unsigned i = 0, e = Src->getNumChildren(); i != e; ++i) {
1251 if (auto Error = importChildMatcher(InsnMatcher, Src->getChild(i), OpIdx++,
Daniel Sandersc270c502017-03-30 09:36:33 +00001252 TempOpIdx))
Daniel Sandersffc7d582017-03-29 15:37:18 +00001253 return std::move(Error);
1254 }
1255
1256 return InsnMatcher;
1257}
1258
Daniel Sandersc270c502017-03-30 09:36:33 +00001259Error GlobalISelEmitter::importChildMatcher(InstructionMatcher &InsnMatcher,
1260 TreePatternNode *SrcChild,
1261 unsigned OpIdx,
1262 unsigned &TempOpIdx) const {
Daniel Sandersffc7d582017-03-29 15:37:18 +00001263 OperandMatcher &OM = InsnMatcher.addOperand(OpIdx, SrcChild->getName());
1264
1265 if (SrcChild->hasAnyPredicate())
1266 return failedImport("Src pattern child has predicate");
1267
1268 ArrayRef<EEVT::TypeSet> ChildTypes = SrcChild->getExtTypes();
1269 if (ChildTypes.size() != 1)
1270 return failedImport("Src pattern child has multiple results");
1271
1272 // Check MBB's before the type check since they are not a known type.
1273 if (!SrcChild->isLeaf()) {
1274 if (SrcChild->getOperator()->isSubClassOf("SDNode")) {
1275 auto &ChildSDNI = CGP.getSDNodeInfo(SrcChild->getOperator());
1276 if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") {
1277 OM.addPredicate<MBBOperandMatcher>();
Daniel Sandersc270c502017-03-30 09:36:33 +00001278 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001279 }
1280 }
Daniel Sandersffc7d582017-03-29 15:37:18 +00001281 }
1282
1283 auto OpTyOrNone = MVTToLLT(ChildTypes.front().getConcrete());
1284 if (!OpTyOrNone)
1285 return failedImport("Src operand has an unsupported type");
1286 OM.addPredicate<LLTOperandMatcher>(*OpTyOrNone);
1287
Daniel Sandersbee57392017-04-04 13:25:23 +00001288 // Check for nested instructions.
1289 if (!SrcChild->isLeaf()) {
1290 // Map the node to a gMIR instruction.
1291 InstructionOperandMatcher &InsnOperand =
1292 OM.addPredicate<InstructionOperandMatcher>();
1293 auto InsnMatcherOrError =
1294 createAndImportSelDAGMatcher(InsnOperand.getInsnMatcher(), SrcChild);
1295 if (auto Error = InsnMatcherOrError.takeError())
1296 return Error;
1297
1298 return Error::success();
1299 }
1300
Daniel Sandersffc7d582017-03-29 15:37:18 +00001301 // Check for constant immediates.
1302 if (auto *ChildInt = dyn_cast<IntInit>(SrcChild->getLeafValue())) {
1303 OM.addPredicate<IntOperandMatcher>(ChildInt->getValue());
Daniel Sandersc270c502017-03-30 09:36:33 +00001304 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001305 }
1306
1307 // Check for def's like register classes or ComplexPattern's.
1308 if (auto *ChildDefInit = dyn_cast<DefInit>(SrcChild->getLeafValue())) {
1309 auto *ChildRec = ChildDefInit->getDef();
1310
1311 // Check for register classes.
1312 if (ChildRec->isSubClassOf("RegisterClass")) {
1313 OM.addPredicate<RegisterBankOperandMatcher>(
1314 Target.getRegisterClass(ChildRec));
Daniel Sandersc270c502017-03-30 09:36:33 +00001315 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001316 }
1317
1318 // Check for ComplexPattern's.
1319 if (ChildRec->isSubClassOf("ComplexPattern")) {
1320 const auto &ComplexPattern = ComplexPatternEquivs.find(ChildRec);
1321 if (ComplexPattern == ComplexPatternEquivs.end())
1322 return failedImport(
1323 "SelectionDAG ComplexPattern not mapped to GlobalISel");
1324
1325 const auto &Predicate = OM.addPredicate<ComplexPatternOperandMatcher>(
1326 *ComplexPattern->second, TempOpIdx);
1327 TempOpIdx += Predicate.countTemporaryOperands();
Daniel Sandersc270c502017-03-30 09:36:33 +00001328 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001329 }
1330
1331 return failedImport(
1332 "Src pattern child def is an unsupported tablegen class");
1333 }
1334
1335 return failedImport("Src pattern child is an unsupported kind");
1336}
1337
Daniel Sandersc270c502017-03-30 09:36:33 +00001338Error GlobalISelEmitter::importExplicitUseRenderer(
Daniel Sandersffc7d582017-03-29 15:37:18 +00001339 BuildMIAction &DstMIBuilder, TreePatternNode *DstChild,
1340 const InstructionMatcher &InsnMatcher, unsigned &TempOpIdx) const {
1341 // The only non-leaf child we accept is 'bb': it's an operator because
1342 // BasicBlockSDNode isn't inline, but in MI it's just another operand.
1343 if (!DstChild->isLeaf()) {
1344 if (DstChild->getOperator()->isSubClassOf("SDNode")) {
1345 auto &ChildSDNI = CGP.getSDNodeInfo(DstChild->getOperator());
1346 if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") {
1347 DstMIBuilder.addRenderer<CopyRenderer>(InsnMatcher,
1348 DstChild->getName());
Daniel Sandersc270c502017-03-30 09:36:33 +00001349 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001350 }
1351 }
1352 return failedImport("Dst pattern child isn't a leaf node or an MBB");
1353 }
1354
1355 // Otherwise, we're looking for a bog-standard RegisterClass operand.
1356 if (DstChild->hasAnyPredicate())
1357 return failedImport("Dst pattern child has predicate");
1358
1359 if (auto *ChildDefInit = dyn_cast<DefInit>(DstChild->getLeafValue())) {
1360 auto *ChildRec = ChildDefInit->getDef();
1361
1362 ArrayRef<EEVT::TypeSet> ChildTypes = DstChild->getExtTypes();
1363 if (ChildTypes.size() != 1)
1364 return failedImport("Dst pattern child has multiple results");
1365
1366 auto OpTyOrNone = MVTToLLT(ChildTypes.front().getConcrete());
1367 if (!OpTyOrNone)
1368 return failedImport("Dst operand has an unsupported type");
1369
1370 if (ChildRec->isSubClassOf("Register")) {
1371 DstMIBuilder.addRenderer<AddRegisterRenderer>(ChildRec);
Daniel Sandersc270c502017-03-30 09:36:33 +00001372 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001373 }
1374
1375 if (ChildRec->isSubClassOf("RegisterClass")) {
1376 DstMIBuilder.addRenderer<CopyRenderer>(InsnMatcher, DstChild->getName());
Daniel Sandersc270c502017-03-30 09:36:33 +00001377 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001378 }
1379
1380 if (ChildRec->isSubClassOf("ComplexPattern")) {
1381 const auto &ComplexPattern = ComplexPatternEquivs.find(ChildRec);
1382 if (ComplexPattern == ComplexPatternEquivs.end())
1383 return failedImport(
1384 "SelectionDAG ComplexPattern not mapped to GlobalISel");
1385
1386 SmallVector<OperandPlaceholder, 2> RenderedOperands;
1387 for (unsigned I = 0;
1388 I <
1389 InsnMatcher.getOperand(DstChild->getName()).countTemporaryOperands();
1390 ++I) {
1391 RenderedOperands.push_back(OperandPlaceholder::CreateTemporary(I));
1392 TempOpIdx++;
1393 }
1394 DstMIBuilder.addRenderer<RenderComplexPatternOperand>(
1395 *ComplexPattern->second, RenderedOperands);
Daniel Sandersc270c502017-03-30 09:36:33 +00001396 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001397 }
1398
1399 return failedImport(
1400 "Dst pattern child def is an unsupported tablegen class");
1401 }
1402
1403 return failedImport("Dst pattern child is an unsupported kind");
1404}
1405
Daniel Sandersc270c502017-03-30 09:36:33 +00001406Expected<BuildMIAction &> GlobalISelEmitter::createAndImportInstructionRenderer(
Daniel Sandersffc7d582017-03-29 15:37:18 +00001407 RuleMatcher &M, const TreePatternNode *Dst,
1408 const InstructionMatcher &InsnMatcher) const {
1409 Record *DstOp = Dst->getOperator();
1410 if (!DstOp->isSubClassOf("Instruction"))
1411 return failedImport("Pattern operator isn't an instruction");
1412 auto &DstI = Target.getInstruction(DstOp);
1413
1414 auto &DstMIBuilder = M.addAction<BuildMIAction>(&DstI, InsnMatcher);
1415
1416 // Render the explicit defs.
1417 for (unsigned I = 0; I < DstI.Operands.NumDefs; ++I) {
1418 const auto &DstIOperand = DstI.Operands[I];
1419 DstMIBuilder.addRenderer<CopyRenderer>(InsnMatcher, DstIOperand.Name);
1420 }
1421
1422 // Render the explicit uses.
1423 unsigned TempOpIdx = 0;
1424 for (unsigned i = 0, e = Dst->getNumChildren(); i != e; ++i) {
1425 if (auto Error = importExplicitUseRenderer(DstMIBuilder, Dst->getChild(i),
Daniel Sandersc270c502017-03-30 09:36:33 +00001426 InsnMatcher, TempOpIdx))
Daniel Sandersffc7d582017-03-29 15:37:18 +00001427 return std::move(Error);
1428 }
1429
1430 return DstMIBuilder;
1431}
1432
Daniel Sandersc270c502017-03-30 09:36:33 +00001433Error GlobalISelEmitter::importImplicitDefRenderers(
Daniel Sandersffc7d582017-03-29 15:37:18 +00001434 BuildMIAction &DstMIBuilder,
1435 const std::vector<Record *> &ImplicitDefs) const {
1436 if (!ImplicitDefs.empty())
1437 return failedImport("Pattern defines a physical register");
Daniel Sandersc270c502017-03-30 09:36:33 +00001438 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001439}
1440
1441Expected<RuleMatcher> GlobalISelEmitter::runOnPattern(const PatternToMatch &P) {
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001442 // Keep track of the matchers and actions to emit.
Ahmed Bougacha9aa4c102017-02-04 00:47:08 +00001443 RuleMatcher M;
1444 M.addAction<DebugCommentAction>(P);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001445
Daniel Sandersc270c502017-03-30 09:36:33 +00001446 if (auto Error = importRulePredicates(M, P.getPredicates()->getValues()))
Daniel Sandersffc7d582017-03-29 15:37:18 +00001447 return std::move(Error);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001448
1449 // Next, analyze the pattern operators.
1450 TreePatternNode *Src = P.getSrcPattern();
1451 TreePatternNode *Dst = P.getDstPattern();
1452
1453 // If the root of either pattern isn't a simple operator, ignore it.
1454 if (!isTrivialOperatorNode(Dst))
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001455 return failedImport("Dst pattern root isn't a trivial operator");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001456 if (!isTrivialOperatorNode(Src))
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001457 return failedImport("Src pattern root isn't a trivial operator");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001458
Daniel Sandersbee57392017-04-04 13:25:23 +00001459 // Start with the defined operands (i.e., the results of the root operator).
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001460 Record *DstOp = Dst->getOperator();
1461 if (!DstOp->isSubClassOf("Instruction"))
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001462 return failedImport("Pattern operator isn't an instruction");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001463
1464 auto &DstI = Target.getInstruction(DstOp);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001465 if (DstI.Operands.NumDefs != Src->getExtTypes().size())
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001466 return failedImport("Src pattern results and dst MI defs are different");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001467
Daniel Sandersffc7d582017-03-29 15:37:18 +00001468 InstructionMatcher &InsnMatcherTemp = M.addInstructionMatcher();
Daniel Sandersc270c502017-03-30 09:36:33 +00001469 auto InsnMatcherOrError = createAndImportSelDAGMatcher(InsnMatcherTemp, Src);
Daniel Sandersffc7d582017-03-29 15:37:18 +00001470 if (auto Error = InsnMatcherOrError.takeError())
1471 return std::move(Error);
1472 InstructionMatcher &InsnMatcher = InsnMatcherOrError.get();
1473
1474 // The root of the match also has constraints on the register bank so that it
1475 // matches the result instruction.
1476 unsigned OpIdx = 0;
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001477 for (const EEVT::TypeSet &Ty : Src->getExtTypes()) {
Daniel Sandersffc7d582017-03-29 15:37:18 +00001478 (void)Ty;
1479
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001480 const auto &DstIOperand = DstI.Operands[OpIdx];
1481 Record *DstIOpRec = DstIOperand.Rec;
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001482 if (!DstIOpRec->isSubClassOf("RegisterClass"))
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001483 return failedImport("Dst MI def isn't a register class");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001484
Daniel Sandersffc7d582017-03-29 15:37:18 +00001485 OperandMatcher &OM = InsnMatcher.getOperand(OpIdx);
1486 OM.setSymbolicName(DstIOperand.Name);
Daniel Sandersdc662ff2017-01-26 11:10:14 +00001487 OM.addPredicate<RegisterBankOperandMatcher>(
1488 Target.getRegisterClass(DstIOpRec));
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001489 ++OpIdx;
1490 }
1491
Daniel Sandersc270c502017-03-30 09:36:33 +00001492 auto DstMIBuilderOrError =
1493 createAndImportInstructionRenderer(M, Dst, InsnMatcher);
Daniel Sandersffc7d582017-03-29 15:37:18 +00001494 if (auto Error = DstMIBuilderOrError.takeError())
1495 return std::move(Error);
1496 BuildMIAction &DstMIBuilder = DstMIBuilderOrError.get();
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001497
Daniel Sandersffc7d582017-03-29 15:37:18 +00001498 // Render the implicit defs.
1499 // These are only added to the root of the result.
Daniel Sandersc270c502017-03-30 09:36:33 +00001500 if (auto Error = importImplicitDefRenderers(DstMIBuilder, P.getDstRegs()))
Daniel Sandersffc7d582017-03-29 15:37:18 +00001501 return std::move(Error);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001502
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001503 // We're done with this pattern! It's eligible for GISel emission; return it.
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00001504 ++NumPatternImported;
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001505 return std::move(M);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001506}
1507
1508void GlobalISelEmitter::run(raw_ostream &OS) {
1509 // Track the GINodeEquiv definitions.
1510 gatherNodeEquivs();
1511
1512 emitSourceFileHeader(("Global Instruction Selector for the " +
1513 Target.getName() + " target").str(), OS);
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00001514 std::vector<RuleMatcher> Rules;
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001515 // Look through the SelectionDAG patterns we found, possibly emitting some.
1516 for (const PatternToMatch &Pat : CGP.ptms()) {
1517 ++NumPatternTotal;
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001518 auto MatcherOrErr = runOnPattern(Pat);
1519
1520 // The pattern analysis can fail, indicating an unsupported pattern.
1521 // Report that if we've been asked to do so.
1522 if (auto Err = MatcherOrErr.takeError()) {
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001523 if (WarnOnSkippedPatterns) {
1524 PrintWarning(Pat.getSrcRecord()->getLoc(),
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001525 "Skipped pattern: " + toString(std::move(Err)));
1526 } else {
1527 consumeError(std::move(Err));
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001528 }
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00001529 ++NumPatternImportsSkipped;
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001530 continue;
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001531 }
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001532
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00001533 Rules.push_back(std::move(MatcherOrErr.get()));
1534 }
1535
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001536 std::stable_sort(Rules.begin(), Rules.end(),
1537 [&](const RuleMatcher &A, const RuleMatcher &B) {
Daniel Sanders759ff412017-02-24 13:58:11 +00001538 if (A.isHigherPriorityThan(B)) {
1539 assert(!B.isHigherPriorityThan(A) && "Cannot be more important "
1540 "and less important at "
1541 "the same time");
1542 return true;
1543 }
1544 return false;
1545 });
1546
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001547 unsigned MaxTemporaries = 0;
1548 for (const auto &Rule : Rules)
1549 MaxTemporaries = std::max(MaxTemporaries, Rule.countTemporaryOperands());
1550
1551 OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n";
1552 for (unsigned I = 0; I < MaxTemporaries; ++I)
1553 OS << " mutable MachineOperand TempOp" << I << ";\n";
1554 OS << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n\n";
1555
1556 OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n";
1557 for (unsigned I = 0; I < MaxTemporaries; ++I)
1558 OS << ", TempOp" << I << "(MachineOperand::CreatePlaceholder())\n";
1559 OS << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n\n";
1560
1561 OS << "#ifdef GET_GLOBALISEL_IMPL\n"
1562 << "bool " << Target.getName()
1563 << "InstructionSelector::selectImpl(MachineInstr &I) const {\n"
1564 << " MachineFunction &MF = *I.getParent()->getParent();\n"
1565 << " const MachineRegisterInfo &MRI = MF.getRegInfo();\n";
1566
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001567 for (auto &Rule : Rules) {
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00001568 Rule.emit(OS);
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001569 ++NumPatternEmitted;
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001570 }
1571
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001572 OS << " return false;\n"
1573 << "}\n"
1574 << "#endif // ifdef GET_GLOBALISEL_IMPL\n";
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001575}
1576
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001577} // end anonymous namespace
1578
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001579//===----------------------------------------------------------------------===//
1580
1581namespace llvm {
1582void EmitGlobalISel(RecordKeeper &RK, raw_ostream &OS) {
1583 GlobalISelEmitter(RK).run(OS);
1584}
1585} // End llvm namespace