blob: ef65317c73979006adb88db76936c62a37e3b91e [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"
Daniel Sanderse7b0d662017-04-21 15:59:56 +000034#include "SubtargetFeatureInfo.h"
Ahmed Bougacha36f70352016-12-21 23:26:20 +000035#include "llvm/ADT/Optional.h"
Daniel Sanders0ed28822017-04-12 08:23:08 +000036#include "llvm/ADT/SmallSet.h"
Ahmed Bougacha36f70352016-12-21 23:26:20 +000037#include "llvm/ADT/Statistic.h"
38#include "llvm/CodeGen/MachineValueType.h"
39#include "llvm/Support/CommandLine.h"
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +000040#include "llvm/Support/Error.h"
Daniel Sanders52b4ce72017-03-07 23:20:35 +000041#include "llvm/Support/LowLevelTypeImpl.h"
Pavel Labath52a82e22017-02-21 09:19:41 +000042#include "llvm/Support/ScopedPrinter.h"
Ahmed Bougacha36f70352016-12-21 23:26:20 +000043#include "llvm/TableGen/Error.h"
44#include "llvm/TableGen/Record.h"
45#include "llvm/TableGen/TableGenBackend.h"
46#include <string>
Daniel Sanders8a4bae92017-03-14 21:32:08 +000047#include <numeric>
Ahmed Bougacha36f70352016-12-21 23:26:20 +000048using namespace llvm;
49
50#define DEBUG_TYPE "gisel-emitter"
51
52STATISTIC(NumPatternTotal, "Total number of patterns");
Daniel Sandersb41ce2b2017-02-20 14:31:27 +000053STATISTIC(NumPatternImported, "Number of patterns imported from SelectionDAG");
54STATISTIC(NumPatternImportsSkipped, "Number of SelectionDAG imports skipped");
Ahmed Bougacha36f70352016-12-21 23:26:20 +000055STATISTIC(NumPatternEmitted, "Number of patterns emitted");
Daniel Sandersc60abe32017-07-04 15:31:50 +000056/// A unique identifier for a MatchTable.
57static unsigned CurrentMatchTableID = 0;
Ahmed Bougacha36f70352016-12-21 23:26:20 +000058
Daniel Sanders0848b232017-03-27 13:15:13 +000059cl::OptionCategory GlobalISelEmitterCat("Options for -gen-global-isel");
60
Ahmed Bougacha36f70352016-12-21 23:26:20 +000061static cl::opt<bool> WarnOnSkippedPatterns(
62 "warn-on-skipped-patterns",
63 cl::desc("Explain why a pattern was skipped for inclusion "
64 "in the GlobalISel selector"),
Daniel Sanders0848b232017-03-27 13:15:13 +000065 cl::init(false), cl::cat(GlobalISelEmitterCat));
Ahmed Bougacha36f70352016-12-21 23:26:20 +000066
Daniel Sandersbdfebb82017-03-15 20:18:38 +000067namespace {
Ahmed Bougacha36f70352016-12-21 23:26:20 +000068//===- Helper functions ---------------------------------------------------===//
69
Daniel Sanders52b4ce72017-03-07 23:20:35 +000070/// This class stands in for LLT wherever we want to tablegen-erate an
71/// equivalent at compiler run-time.
72class LLTCodeGen {
73private:
74 LLT Ty;
75
76public:
77 LLTCodeGen(const LLT &Ty) : Ty(Ty) {}
78
Daniel Sanders6ab0daa2017-07-04 14:35:06 +000079 void emitCxxEnumValue(raw_ostream &OS) const {
80 if (Ty.isScalar()) {
81 OS << "GILLT_s" << Ty.getSizeInBits();
82 return;
83 }
84 if (Ty.isVector()) {
85 OS << "GILLT_v" << Ty.getNumElements() << "s" << Ty.getScalarSizeInBits();
86 return;
87 }
88 llvm_unreachable("Unhandled LLT");
89 }
90
Daniel Sanders52b4ce72017-03-07 23:20:35 +000091 void emitCxxConstructorCall(raw_ostream &OS) const {
92 if (Ty.isScalar()) {
93 OS << "LLT::scalar(" << Ty.getSizeInBits() << ")";
94 return;
95 }
96 if (Ty.isVector()) {
Daniel Sanders32291982017-06-28 13:50:04 +000097 OS << "LLT::vector(" << Ty.getNumElements() << ", "
98 << Ty.getScalarSizeInBits() << ")";
Daniel Sanders52b4ce72017-03-07 23:20:35 +000099 return;
100 }
101 llvm_unreachable("Unhandled LLT");
102 }
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000103
104 const LLT &get() const { return Ty; }
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000105
106 /// This ordering is used for std::unique() and std::sort(). There's no
107 /// particular logic behind the order.
108 bool operator<(const LLTCodeGen &Other) const {
109 if (!Ty.isValid())
110 return Other.Ty.isValid();
111 if (Ty.isScalar()) {
112 if (!Other.Ty.isValid())
113 return false;
114 if (Other.Ty.isScalar())
115 return Ty.getSizeInBits() < Other.Ty.getSizeInBits();
116 return false;
117 }
118 if (Ty.isVector()) {
119 if (!Other.Ty.isValid() || Other.Ty.isScalar())
120 return false;
121 if (Other.Ty.isVector()) {
122 if (Ty.getNumElements() < Other.Ty.getNumElements())
123 return true;
124 if (Ty.getNumElements() > Other.Ty.getNumElements())
125 return false;
126 return Ty.getSizeInBits() < Other.Ty.getSizeInBits();
127 }
128 return false;
129 }
130 llvm_unreachable("Unhandled LLT");
131 }
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000132};
133
134class InstructionMatcher;
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000135/// Convert an MVT to an equivalent LLT if possible, or the invalid LLT() for
136/// MVTs that don't map cleanly to an LLT (e.g., iPTR, *any, ...).
Daniel Sanders52b4ce72017-03-07 23:20:35 +0000137static Optional<LLTCodeGen> MVTToLLT(MVT::SimpleValueType SVT) {
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000138 MVT VT(SVT);
Daniel Sanders52b4ce72017-03-07 23:20:35 +0000139 if (VT.isVector() && VT.getVectorNumElements() != 1)
Daniel Sanders32291982017-06-28 13:50:04 +0000140 return LLTCodeGen(
141 LLT::vector(VT.getVectorNumElements(), VT.getScalarSizeInBits()));
Daniel Sanders52b4ce72017-03-07 23:20:35 +0000142 if (VT.isInteger() || VT.isFloatingPoint())
143 return LLTCodeGen(LLT::scalar(VT.getSizeInBits()));
144 return None;
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000145}
146
Daniel Sandersd0656a32017-04-13 09:45:37 +0000147static std::string explainPredicates(const TreePatternNode *N) {
148 std::string Explanation = "";
149 StringRef Separator = "";
150 for (const auto &P : N->getPredicateFns()) {
151 Explanation +=
152 (Separator + P.getOrigPatFragRecord()->getRecord()->getName()).str();
153 if (P.isAlwaysTrue())
154 Explanation += " always-true";
155 if (P.isImmediatePattern())
156 Explanation += " immediate";
157 }
158 return Explanation;
159}
160
Daniel Sandersd0656a32017-04-13 09:45:37 +0000161std::string explainOperator(Record *Operator) {
162 if (Operator->isSubClassOf("SDNode"))
Craig Topper2b8419a2017-05-31 19:01:11 +0000163 return (" (" + Operator->getValueAsString("Opcode") + ")").str();
Daniel Sandersd0656a32017-04-13 09:45:37 +0000164
165 if (Operator->isSubClassOf("Intrinsic"))
166 return (" (Operator is an Intrinsic, " + Operator->getName() + ")").str();
167
168 return " (Operator not understood)";
169}
170
171/// Helper function to let the emitter report skip reason error messages.
172static Error failedImport(const Twine &Reason) {
173 return make_error<StringError>(Reason, inconvertibleErrorCode());
174}
175
176static Error isTrivialOperatorNode(const TreePatternNode *N) {
177 std::string Explanation = "";
178 std::string Separator = "";
179 if (N->isLeaf()) {
Daniel Sanders3334cc02017-05-23 20:02:48 +0000180 if (isa<IntInit>(N->getLeafValue()))
Daniel Sanders452c8ae2017-05-23 19:33:16 +0000181 return Error::success();
182
Daniel Sandersd0656a32017-04-13 09:45:37 +0000183 Explanation = "Is a leaf";
184 Separator = ", ";
185 }
186
187 if (N->hasAnyPredicate()) {
188 Explanation = Separator + "Has a predicate (" + explainPredicates(N) + ")";
189 Separator = ", ";
190 }
191
192 if (N->getTransformFn()) {
193 Explanation += Separator + "Has a transform function";
194 Separator = ", ";
195 }
196
197 if (!N->isLeaf() && !N->hasAnyPredicate() && !N->getTransformFn())
198 return Error::success();
199
200 return failedImport(Explanation);
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000201}
202
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +0000203static Record *getInitValueAsRegClass(Init *V) {
204 if (DefInit *VDefInit = dyn_cast<DefInit>(V)) {
205 if (VDefInit->getDef()->isSubClassOf("RegisterOperand"))
206 return VDefInit->getDef()->getValueAsDef("RegClass");
207 if (VDefInit->getDef()->isSubClassOf("RegisterClass"))
208 return VDefInit->getDef();
209 }
210 return nullptr;
211}
212
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000213std::string
214getNameForFeatureBitset(const std::vector<Record *> &FeatureBitset) {
215 std::string Name = "GIFBS";
216 for (const auto &Feature : FeatureBitset)
217 Name += ("_" + Feature->getName()).str();
218 return Name;
219}
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000220//===- Matchers -----------------------------------------------------------===//
221
Daniel Sandersbee57392017-04-04 13:25:23 +0000222class OperandMatcher;
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000223class MatchAction;
224
225/// Generates code to check that a match rule matches.
226class RuleMatcher {
227 /// A list of matchers that all need to succeed for the current rule to match.
228 /// FIXME: This currently supports a single match position but could be
229 /// extended to support multiple positions to support div/rem fusion or
230 /// load-multiple instructions.
231 std::vector<std::unique_ptr<InstructionMatcher>> Matchers;
232
233 /// A list of actions that need to be taken when all predicates in this rule
234 /// have succeeded.
235 std::vector<std::unique_ptr<MatchAction>> Actions;
236
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000237 /// A map of instruction matchers to the local variables created by
Daniel Sanders9d662d22017-07-06 10:06:12 +0000238 /// emitCaptureOpcodes().
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000239 std::map<const InstructionMatcher *, unsigned> InsnVariableIDs;
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000240
241 /// ID for the next instruction variable defined with defineInsnVar()
242 unsigned NextInsnVarID;
243
Daniel Sanderse7b0d662017-04-21 15:59:56 +0000244 std::vector<Record *> RequiredFeatures;
245
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000246public:
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000247 RuleMatcher()
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000248 : Matchers(), Actions(), InsnVariableIDs(), NextInsnVarID(0) {}
Zachary Turnerb7dbd872017-03-20 19:56:52 +0000249 RuleMatcher(RuleMatcher &&Other) = default;
250 RuleMatcher &operator=(RuleMatcher &&Other) = default;
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000251
252 InstructionMatcher &addInstructionMatcher();
Daniel Sanderse7b0d662017-04-21 15:59:56 +0000253 void addRequiredFeature(Record *Feature);
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000254 const std::vector<Record *> &getRequiredFeatures() const;
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000255
256 template <class Kind, class... Args> Kind &addAction(Args &&... args);
257
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000258 /// Define an instruction without emitting any code to do so.
259 /// This is used for the root of the match.
260 unsigned implicitlyDefineInsnVar(const InstructionMatcher &Matcher);
261 /// Define an instruction and emit corresponding state-machine opcodes.
262 unsigned defineInsnVar(raw_ostream &OS, const InstructionMatcher &Matcher,
263 unsigned InsnVarID, unsigned OpIdx);
264 unsigned getInsnVarID(const InstructionMatcher &InsnMatcher) const;
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000265
Daniel Sanders9d662d22017-07-06 10:06:12 +0000266 void emitCaptureOpcodes(raw_ostream &OS);
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000267
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000268 void emit(raw_ostream &OS);
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000269
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000270 /// Compare the priority of this object and B.
271 ///
272 /// Returns true if this object is more important than B.
273 bool isHigherPriorityThan(const RuleMatcher &B) const;
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000274
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000275 /// Report the maximum number of temporary operands needed by the rule
276 /// matcher.
277 unsigned countRendererFns() const;
Daniel Sanders2deea182017-04-22 15:11:04 +0000278
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000279 // FIXME: Remove this as soon as possible
280 InstructionMatcher &insnmatcher_front() const { return *Matchers.front(); }
Daniel Sandersbdfebb82017-03-15 20:18:38 +0000281};
282
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000283template <class PredicateTy> class PredicateListMatcher {
284private:
285 typedef std::vector<std::unique_ptr<PredicateTy>> PredicateVec;
286 PredicateVec Predicates;
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000287
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000288public:
289 /// Construct a new operand predicate and add it to the matcher.
290 template <class Kind, class... Args>
291 Kind &addPredicate(Args&&... args) {
Ahmed Bougachab67a3ce2017-01-26 22:07:37 +0000292 Predicates.emplace_back(
293 llvm::make_unique<Kind>(std::forward<Args>(args)...));
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000294 return *static_cast<Kind *>(Predicates.back().get());
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000295 }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000296
Daniel Sanders32291982017-06-28 13:50:04 +0000297 typename PredicateVec::const_iterator predicates_begin() const {
298 return Predicates.begin();
299 }
300 typename PredicateVec::const_iterator predicates_end() const {
301 return Predicates.end();
302 }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000303 iterator_range<typename PredicateVec::const_iterator> predicates() const {
304 return make_range(predicates_begin(), predicates_end());
305 }
Daniel Sanders32291982017-06-28 13:50:04 +0000306 typename PredicateVec::size_type predicates_size() const {
307 return Predicates.size();
308 }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000309
Daniel Sanders9d662d22017-07-06 10:06:12 +0000310 /// Emit MatchTable opcodes that tests whether all the predicates are met.
Ahmed Bougachab67a3ce2017-01-26 22:07:37 +0000311 template <class... Args>
Daniel Sanders9d662d22017-07-06 10:06:12 +0000312 void emitPredicateListOpcodes(raw_ostream &OS, Args &&... args) const {
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000313 if (Predicates.empty()) {
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000314 OS << "// No predicates\n";
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000315 return;
316 }
317
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000318 for (const auto &Predicate : predicates())
Daniel Sanders9d662d22017-07-06 10:06:12 +0000319 Predicate->emitPredicateOpcodes(OS, std::forward<Args>(args)...);
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000320 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000321};
322
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000323/// Generates code to check a predicate of an operand.
324///
325/// Typical predicates include:
326/// * Operand is a particular register.
327/// * Operand is assigned a particular register bank.
328/// * Operand is an MBB.
329class OperandPredicateMatcher {
330public:
Daniel Sanders759ff412017-02-24 13:58:11 +0000331 /// This enum is used for RTTI and also defines the priority that is given to
332 /// the predicate when generating the matcher code. Kinds with higher priority
333 /// must be tested first.
334 ///
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000335 /// The relative priority of OPM_LLT, OPM_RegBank, and OPM_MBB do not matter
336 /// but OPM_Int must have priority over OPM_RegBank since constant integers
337 /// are represented by a virtual register defined by a G_CONSTANT instruction.
Daniel Sanders759ff412017-02-24 13:58:11 +0000338 enum PredicateKind {
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000339 OPM_ComplexPattern,
Daniel Sandersbee57392017-04-04 13:25:23 +0000340 OPM_Instruction,
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000341 OPM_Int,
Daniel Sanders452c8ae2017-05-23 19:33:16 +0000342 OPM_LiteralInt,
Daniel Sanders759ff412017-02-24 13:58:11 +0000343 OPM_LLT,
344 OPM_RegBank,
345 OPM_MBB,
346 };
347
348protected:
349 PredicateKind Kind;
350
351public:
352 OperandPredicateMatcher(PredicateKind Kind) : Kind(Kind) {}
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000353 virtual ~OperandPredicateMatcher() {}
354
Daniel Sanders759ff412017-02-24 13:58:11 +0000355 PredicateKind getKind() const { return Kind; }
356
Daniel Sandersbee57392017-04-04 13:25:23 +0000357 /// Return the OperandMatcher for the specified operand or nullptr if there
358 /// isn't one by that name in this operand predicate matcher.
359 ///
360 /// InstructionOperandMatcher is the only subclass that can return non-null
361 /// for this.
362 virtual Optional<const OperandMatcher *>
Daniel Sandersdb7ed372017-04-04 13:52:00 +0000363 getOptionalOperand(StringRef SymbolicName) const {
Daniel Sandersbee57392017-04-04 13:25:23 +0000364 assert(!SymbolicName.empty() && "Cannot lookup unnamed operand");
365 return None;
366 }
367
Daniel Sanders9d662d22017-07-06 10:06:12 +0000368 /// Emit MatchTable opcodes to capture instructions into the MIs table.
Daniel Sandersbee57392017-04-04 13:25:23 +0000369 ///
Daniel Sanders9d662d22017-07-06 10:06:12 +0000370 /// Only InstructionOperandMatcher needs to do anything for this method the
371 /// rest just walk the tree.
372 virtual void emitCaptureOpcodes(raw_ostream &OS, RuleMatcher &Rule,
373 unsigned InsnVarID, unsigned OpIdx) const {}
Daniel Sandersbee57392017-04-04 13:25:23 +0000374
Daniel Sanders9d662d22017-07-06 10:06:12 +0000375 /// Emit MatchTable opcodes that check the predicate for the given operand.
376 virtual void emitPredicateOpcodes(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000377 unsigned InsnVarID,
378 unsigned OpIdx) const = 0;
Daniel Sanders759ff412017-02-24 13:58:11 +0000379
380 /// Compare the priority of this object and B.
381 ///
382 /// Returns true if this object is more important than B.
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000383 virtual bool isHigherPriorityThan(const OperandPredicateMatcher &B) const {
384 return Kind < B.Kind;
Daniel Sanders759ff412017-02-24 13:58:11 +0000385 };
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000386
387 /// Report the maximum number of temporary operands needed by the predicate
388 /// matcher.
Daniel Sanders2deea182017-04-22 15:11:04 +0000389 virtual unsigned countRendererFns() const { return 0; }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000390};
391
392/// Generates code to check that an operand is a particular LLT.
393class LLTOperandMatcher : public OperandPredicateMatcher {
394protected:
Daniel Sanders52b4ce72017-03-07 23:20:35 +0000395 LLTCodeGen Ty;
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000396
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000397public:
Daniel Sanders52b4ce72017-03-07 23:20:35 +0000398 LLTOperandMatcher(const LLTCodeGen &Ty)
Daniel Sanders759ff412017-02-24 13:58:11 +0000399 : OperandPredicateMatcher(OPM_LLT), Ty(Ty) {}
400
401 static bool classof(const OperandPredicateMatcher *P) {
402 return P->getKind() == OPM_LLT;
403 }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000404
Daniel Sanders9d662d22017-07-06 10:06:12 +0000405 void emitPredicateOpcodes(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000406 unsigned InsnVarID, unsigned OpIdx) const override {
407 OS << " GIM_CheckType, /*MI*/" << InsnVarID << ", /*Op*/" << OpIdx
408 << ", /*Type*/";
409 Ty.emitCxxEnumValue(OS);
410 OS << ", \n";
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000411 }
412};
413
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000414/// Generates code to check that an operand is a particular target constant.
415class ComplexPatternOperandMatcher : public OperandPredicateMatcher {
416protected:
Daniel Sanders4f3eb242017-04-05 13:14:03 +0000417 const OperandMatcher &Operand;
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000418 const Record &TheDef;
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000419
Daniel Sanders4f3eb242017-04-05 13:14:03 +0000420 unsigned getAllocatedTemporariesBaseID() const;
421
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000422public:
Daniel Sanders4f3eb242017-04-05 13:14:03 +0000423 ComplexPatternOperandMatcher(const OperandMatcher &Operand,
424 const Record &TheDef)
425 : OperandPredicateMatcher(OPM_ComplexPattern), Operand(Operand),
426 TheDef(TheDef) {}
427
428 static bool classof(const OperandPredicateMatcher *P) {
429 return P->getKind() == OPM_ComplexPattern;
430 }
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000431
Daniel Sanders9d662d22017-07-06 10:06:12 +0000432 void emitPredicateOpcodes(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000433 unsigned InsnVarID, unsigned OpIdx) const override {
Daniel Sanders2deea182017-04-22 15:11:04 +0000434 unsigned ID = getAllocatedTemporariesBaseID();
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000435 OS << " GIM_CheckComplexPattern, /*MI*/" << InsnVarID << ", /*Op*/"
436 << OpIdx << ", /*Renderer*/" << ID << ", GICP_"
437 << TheDef.getName() << ",\n";
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000438 }
439
Daniel Sanders2deea182017-04-22 15:11:04 +0000440 unsigned countRendererFns() const override {
441 return 1;
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000442 }
443};
444
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000445/// Generates code to check that an operand is in a particular register bank.
446class RegisterBankOperandMatcher : public OperandPredicateMatcher {
447protected:
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000448 const CodeGenRegisterClass &RC;
449
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000450public:
Daniel Sanders759ff412017-02-24 13:58:11 +0000451 RegisterBankOperandMatcher(const CodeGenRegisterClass &RC)
452 : OperandPredicateMatcher(OPM_RegBank), RC(RC) {}
453
454 static bool classof(const OperandPredicateMatcher *P) {
455 return P->getKind() == OPM_RegBank;
456 }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000457
Daniel Sanders9d662d22017-07-06 10:06:12 +0000458 void emitPredicateOpcodes(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000459 unsigned InsnVarID, unsigned OpIdx) const override {
460 OS << " GIM_CheckRegBankForClass, /*MI*/" << InsnVarID << ", /*Op*/"
461 << OpIdx << ", /*RC*/" << RC.getQualifiedName() << "RegClassID,\n";
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000462 }
463};
464
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000465/// Generates code to check that an operand is a basic block.
466class MBBOperandMatcher : public OperandPredicateMatcher {
467public:
Daniel Sanders759ff412017-02-24 13:58:11 +0000468 MBBOperandMatcher() : OperandPredicateMatcher(OPM_MBB) {}
469
470 static bool classof(const OperandPredicateMatcher *P) {
471 return P->getKind() == OPM_MBB;
472 }
473
Daniel Sanders9d662d22017-07-06 10:06:12 +0000474 void emitPredicateOpcodes(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000475 unsigned InsnVarID, unsigned OpIdx) const override {
476 OS << " GIM_CheckIsMBB, /*MI*/" << InsnVarID << ", /*Op*/" << OpIdx << ",\n";
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000477 }
478};
479
Daniel Sanders452c8ae2017-05-23 19:33:16 +0000480/// Generates code to check that an operand is a G_CONSTANT with a particular
481/// int.
482class ConstantIntOperandMatcher : public OperandPredicateMatcher {
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000483protected:
484 int64_t Value;
485
486public:
Daniel Sanders452c8ae2017-05-23 19:33:16 +0000487 ConstantIntOperandMatcher(int64_t Value)
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000488 : OperandPredicateMatcher(OPM_Int), Value(Value) {}
489
490 static bool classof(const OperandPredicateMatcher *P) {
491 return P->getKind() == OPM_Int;
492 }
493
Daniel Sanders9d662d22017-07-06 10:06:12 +0000494 void emitPredicateOpcodes(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000495 unsigned InsnVarID, unsigned OpIdx) const override {
496 OS << " GIM_CheckConstantInt, /*MI*/" << InsnVarID << ", /*Op*/"
497 << OpIdx << ", " << Value << ",\n";
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000498 }
499};
500
Daniel Sanders452c8ae2017-05-23 19:33:16 +0000501/// Generates code to check that an operand is a raw int (where MO.isImm() or
502/// MO.isCImm() is true).
503class LiteralIntOperandMatcher : public OperandPredicateMatcher {
504protected:
505 int64_t Value;
506
507public:
508 LiteralIntOperandMatcher(int64_t Value)
509 : OperandPredicateMatcher(OPM_LiteralInt), Value(Value) {}
510
511 static bool classof(const OperandPredicateMatcher *P) {
512 return P->getKind() == OPM_LiteralInt;
513 }
514
Daniel Sanders9d662d22017-07-06 10:06:12 +0000515 void emitPredicateOpcodes(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000516 unsigned InsnVarID, unsigned OpIdx) const override {
517 OS << " GIM_CheckLiteralInt, /*MI*/" << InsnVarID << ", /*Op*/"
518 << OpIdx << ", " << Value << ",\n";
Daniel Sanders452c8ae2017-05-23 19:33:16 +0000519 }
520};
521
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000522/// Generates code to check that a set of predicates match for a particular
523/// operand.
524class OperandMatcher : public PredicateListMatcher<OperandPredicateMatcher> {
525protected:
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000526 InstructionMatcher &Insn;
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000527 unsigned OpIdx;
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000528 std::string SymbolicName;
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000529
Daniel Sanders4f3eb242017-04-05 13:14:03 +0000530 /// The index of the first temporary variable allocated to this operand. The
531 /// number of allocated temporaries can be found with
Daniel Sanders2deea182017-04-22 15:11:04 +0000532 /// countRendererFns().
Daniel Sanders4f3eb242017-04-05 13:14:03 +0000533 unsigned AllocatedTemporariesBaseID;
534
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000535public:
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000536 OperandMatcher(InstructionMatcher &Insn, unsigned OpIdx,
Daniel Sanders4f3eb242017-04-05 13:14:03 +0000537 const std::string &SymbolicName,
538 unsigned AllocatedTemporariesBaseID)
539 : Insn(Insn), OpIdx(OpIdx), SymbolicName(SymbolicName),
540 AllocatedTemporariesBaseID(AllocatedTemporariesBaseID) {}
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000541
542 bool hasSymbolicName() const { return !SymbolicName.empty(); }
543 const StringRef getSymbolicName() const { return SymbolicName; }
Daniel Sandersffc7d582017-03-29 15:37:18 +0000544 void setSymbolicName(StringRef Name) {
545 assert(SymbolicName.empty() && "Operand already has a symbolic name");
546 SymbolicName = Name;
547 }
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000548 unsigned getOperandIndex() const { return OpIdx; }
549
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000550 std::string getOperandExpr(unsigned InsnVarID) const {
551 return "State.MIs[" + llvm::to_string(InsnVarID) + "]->getOperand(" +
552 llvm::to_string(OpIdx) + ")";
Daniel Sanderse604ef52017-02-20 15:30:43 +0000553 }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000554
Daniel Sandersbee57392017-04-04 13:25:23 +0000555 Optional<const OperandMatcher *>
556 getOptionalOperand(StringRef DesiredSymbolicName) const {
557 assert(!DesiredSymbolicName.empty() && "Cannot lookup unnamed operand");
558 if (DesiredSymbolicName == SymbolicName)
559 return this;
560 for (const auto &OP : predicates()) {
561 const auto &MaybeOperand = OP->getOptionalOperand(DesiredSymbolicName);
562 if (MaybeOperand.hasValue())
563 return MaybeOperand.getValue();
564 }
565 return None;
566 }
567
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000568 InstructionMatcher &getInstructionMatcher() const { return Insn; }
569
Daniel Sanders9d662d22017-07-06 10:06:12 +0000570 /// Emit MatchTable opcodes to capture instructions into the MIs table.
571 void emitCaptureOpcodes(raw_ostream &OS, RuleMatcher &Rule,
572 unsigned InsnVarID) const {
Daniel Sandersbee57392017-04-04 13:25:23 +0000573 for (const auto &Predicate : predicates())
Daniel Sanders9d662d22017-07-06 10:06:12 +0000574 Predicate->emitCaptureOpcodes(OS, Rule, InsnVarID, OpIdx);
Daniel Sandersbee57392017-04-04 13:25:23 +0000575 }
576
Daniel Sanders9d662d22017-07-06 10:06:12 +0000577 /// Emit MatchTable opcodes that test whether the instruction named in
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000578 /// InsnVarID matches all the predicates and all the operands.
Daniel Sanders9d662d22017-07-06 10:06:12 +0000579 void emitPredicateOpcodes(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000580 unsigned InsnVarID) const {
581 OS << " // MIs[" << InsnVarID << "] ";
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000582 if (SymbolicName.empty())
583 OS << "Operand " << OpIdx;
584 else
585 OS << SymbolicName;
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000586 OS << "\n";
Daniel Sanders9d662d22017-07-06 10:06:12 +0000587 emitPredicateListOpcodes(OS, Rule, InsnVarID, OpIdx);
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000588 }
Daniel Sanders759ff412017-02-24 13:58:11 +0000589
590 /// Compare the priority of this object and B.
591 ///
592 /// Returns true if this object is more important than B.
593 bool isHigherPriorityThan(const OperandMatcher &B) const {
594 // Operand matchers involving more predicates have higher priority.
595 if (predicates_size() > B.predicates_size())
596 return true;
597 if (predicates_size() < B.predicates_size())
598 return false;
599
600 // This assumes that predicates are added in a consistent order.
601 for (const auto &Predicate : zip(predicates(), B.predicates())) {
602 if (std::get<0>(Predicate)->isHigherPriorityThan(*std::get<1>(Predicate)))
603 return true;
604 if (std::get<1>(Predicate)->isHigherPriorityThan(*std::get<0>(Predicate)))
605 return false;
606 }
607
608 return false;
609 };
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000610
611 /// Report the maximum number of temporary operands needed by the operand
612 /// matcher.
Daniel Sanders2deea182017-04-22 15:11:04 +0000613 unsigned countRendererFns() const {
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000614 return std::accumulate(
615 predicates().begin(), predicates().end(), 0,
616 [](unsigned A,
617 const std::unique_ptr<OperandPredicateMatcher> &Predicate) {
Daniel Sanders2deea182017-04-22 15:11:04 +0000618 return A + Predicate->countRendererFns();
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000619 });
620 }
Daniel Sanders4f3eb242017-04-05 13:14:03 +0000621
622 unsigned getAllocatedTemporariesBaseID() const {
623 return AllocatedTemporariesBaseID;
624 }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000625};
626
Daniel Sanders4f3eb242017-04-05 13:14:03 +0000627unsigned ComplexPatternOperandMatcher::getAllocatedTemporariesBaseID() const {
628 return Operand.getAllocatedTemporariesBaseID();
629}
630
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000631/// Generates code to check a predicate on an instruction.
632///
633/// Typical predicates include:
634/// * The opcode of the instruction is a particular value.
635/// * The nsw/nuw flag is/isn't set.
636class InstructionPredicateMatcher {
Daniel Sanders759ff412017-02-24 13:58:11 +0000637protected:
638 /// This enum is used for RTTI and also defines the priority that is given to
639 /// the predicate when generating the matcher code. Kinds with higher priority
640 /// must be tested first.
641 enum PredicateKind {
642 IPM_Opcode,
643 };
644
645 PredicateKind Kind;
646
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000647public:
Daniel Sanders8d4d72f2017-02-24 14:53:35 +0000648 InstructionPredicateMatcher(PredicateKind Kind) : Kind(Kind) {}
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000649 virtual ~InstructionPredicateMatcher() {}
650
Daniel Sanders759ff412017-02-24 13:58:11 +0000651 PredicateKind getKind() const { return Kind; }
652
Daniel Sanders9d662d22017-07-06 10:06:12 +0000653 /// Emit MatchTable opcodes that test whether the instruction named in
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000654 /// InsnVarID matches the predicate.
Daniel Sanders9d662d22017-07-06 10:06:12 +0000655 virtual void emitPredicateOpcodes(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000656 unsigned InsnVarID) const = 0;
Daniel Sanders759ff412017-02-24 13:58:11 +0000657
658 /// Compare the priority of this object and B.
659 ///
660 /// Returns true if this object is more important than B.
Daniel Sanders32291982017-06-28 13:50:04 +0000661 virtual bool
662 isHigherPriorityThan(const InstructionPredicateMatcher &B) const {
Daniel Sanders759ff412017-02-24 13:58:11 +0000663 return Kind < B.Kind;
664 };
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000665
666 /// Report the maximum number of temporary operands needed by the predicate
667 /// matcher.
Daniel Sanders2deea182017-04-22 15:11:04 +0000668 virtual unsigned countRendererFns() const { return 0; }
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000669};
670
671/// Generates code to check the opcode of an instruction.
672class InstructionOpcodeMatcher : public InstructionPredicateMatcher {
673protected:
674 const CodeGenInstruction *I;
675
676public:
Daniel Sanders8d4d72f2017-02-24 14:53:35 +0000677 InstructionOpcodeMatcher(const CodeGenInstruction *I)
678 : InstructionPredicateMatcher(IPM_Opcode), I(I) {}
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000679
Daniel Sanders759ff412017-02-24 13:58:11 +0000680 static bool classof(const InstructionPredicateMatcher *P) {
681 return P->getKind() == IPM_Opcode;
682 }
683
Daniel Sanders9d662d22017-07-06 10:06:12 +0000684 void emitPredicateOpcodes(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000685 unsigned InsnVarID) const override {
686 OS << " GIM_CheckOpcode, /*MI*/" << InsnVarID << ", " << I->Namespace
687 << "::" << I->TheDef->getName() << ",\n";
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000688 }
Daniel Sanders759ff412017-02-24 13:58:11 +0000689
690 /// Compare the priority of this object and B.
691 ///
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000692 /// Returns true if this object is more important than B.
Daniel Sanders32291982017-06-28 13:50:04 +0000693 bool
694 isHigherPriorityThan(const InstructionPredicateMatcher &B) const override {
Daniel Sanders759ff412017-02-24 13:58:11 +0000695 if (InstructionPredicateMatcher::isHigherPriorityThan(B))
696 return true;
697 if (B.InstructionPredicateMatcher::isHigherPriorityThan(*this))
698 return false;
699
700 // Prioritize opcodes for cosmetic reasons in the generated source. Although
701 // this is cosmetic at the moment, we may want to drive a similar ordering
702 // using instruction frequency information to improve compile time.
703 if (const InstructionOpcodeMatcher *BO =
704 dyn_cast<InstructionOpcodeMatcher>(&B))
705 return I->TheDef->getName() < BO->I->TheDef->getName();
706
707 return false;
708 };
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000709};
710
711/// Generates code to check that a set of predicates and operands match for a
712/// particular instruction.
713///
714/// Typical predicates include:
715/// * Has a specific opcode.
716/// * Has an nsw/nuw flag or doesn't.
717class InstructionMatcher
718 : public PredicateListMatcher<InstructionPredicateMatcher> {
719protected:
Daniel Sanders4f3eb242017-04-05 13:14:03 +0000720 typedef std::vector<std::unique_ptr<OperandMatcher>> OperandVec;
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000721
722 /// The operands to match. All rendered operands must be present even if the
723 /// condition is always true.
724 OperandVec Operands;
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000725
726public:
727 /// Add an operand to the matcher.
Daniel Sanders4f3eb242017-04-05 13:14:03 +0000728 OperandMatcher &addOperand(unsigned OpIdx, const std::string &SymbolicName,
729 unsigned AllocatedTemporariesBaseID) {
730 Operands.emplace_back(new OperandMatcher(*this, OpIdx, SymbolicName,
731 AllocatedTemporariesBaseID));
732 return *Operands.back();
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000733 }
734
Daniel Sandersffc7d582017-03-29 15:37:18 +0000735 OperandMatcher &getOperand(unsigned OpIdx) {
736 auto I = std::find_if(Operands.begin(), Operands.end(),
Daniel Sanders4f3eb242017-04-05 13:14:03 +0000737 [&OpIdx](const std::unique_ptr<OperandMatcher> &X) {
738 return X->getOperandIndex() == OpIdx;
Daniel Sandersffc7d582017-03-29 15:37:18 +0000739 });
740 if (I != Operands.end())
Daniel Sanders4f3eb242017-04-05 13:14:03 +0000741 return **I;
Daniel Sandersffc7d582017-03-29 15:37:18 +0000742 llvm_unreachable("Failed to lookup operand");
743 }
744
Daniel Sandersbee57392017-04-04 13:25:23 +0000745 Optional<const OperandMatcher *>
746 getOptionalOperand(StringRef SymbolicName) const {
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000747 assert(!SymbolicName.empty() && "Cannot lookup unnamed operand");
Daniel Sandersbee57392017-04-04 13:25:23 +0000748 for (const auto &Operand : Operands) {
Daniel Sanders4f3eb242017-04-05 13:14:03 +0000749 const auto &OM = Operand->getOptionalOperand(SymbolicName);
Daniel Sandersbee57392017-04-04 13:25:23 +0000750 if (OM.hasValue())
751 return OM.getValue();
752 }
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000753 return None;
754 }
755
Daniel Sandersdb7ed372017-04-04 13:52:00 +0000756 const OperandMatcher &getOperand(StringRef SymbolicName) const {
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000757 Optional<const OperandMatcher *>OM = getOptionalOperand(SymbolicName);
758 if (OM.hasValue())
759 return *OM.getValue();
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000760 llvm_unreachable("Failed to lookup operand");
761 }
762
763 unsigned getNumOperands() const { return Operands.size(); }
Daniel Sandersbee57392017-04-04 13:25:23 +0000764 OperandVec::iterator operands_begin() { return Operands.begin(); }
765 OperandVec::iterator operands_end() { return Operands.end(); }
766 iterator_range<OperandVec::iterator> operands() {
767 return make_range(operands_begin(), operands_end());
768 }
Daniel Sandersffc7d582017-03-29 15:37:18 +0000769 OperandVec::const_iterator operands_begin() const { return Operands.begin(); }
770 OperandVec::const_iterator operands_end() const { return Operands.end(); }
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000771 iterator_range<OperandVec::const_iterator> operands() const {
772 return make_range(operands_begin(), operands_end());
773 }
774
Daniel Sanders9d662d22017-07-06 10:06:12 +0000775 /// Emit MatchTable opcodes to check the shape of the match and capture
776 /// instructions into the MIs table.
777 void emitCaptureOpcodes(raw_ostream &OS, RuleMatcher &Rule,
778 unsigned InsnID) {
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000779 OS << " GIM_CheckNumOperands, /*MI*/" << InsnID << ", /*Expected*/"
780 << getNumOperands() << ",\n";
781 for (const auto &Operand : Operands)
Daniel Sanders9d662d22017-07-06 10:06:12 +0000782 Operand->emitCaptureOpcodes(OS, Rule, InsnID);
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000783 }
784
Daniel Sanders9d662d22017-07-06 10:06:12 +0000785 /// Emit MatchTable opcodes that test whether the instruction named in
Daniel Sandersdc662ff2017-01-26 11:10:14 +0000786 /// InsnVarName matches all the predicates and all the operands.
Daniel Sanders9d662d22017-07-06 10:06:12 +0000787 void emitPredicateOpcodes(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000788 unsigned InsnVarID) const {
Daniel Sanders9d662d22017-07-06 10:06:12 +0000789 emitPredicateListOpcodes(OS, Rule, InsnVarID);
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000790 for (const auto &Operand : Operands)
Daniel Sanders9d662d22017-07-06 10:06:12 +0000791 Operand->emitPredicateOpcodes(OS, Rule, InsnVarID);
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000792 }
Daniel Sanders759ff412017-02-24 13:58:11 +0000793
794 /// Compare the priority of this object and B.
795 ///
796 /// Returns true if this object is more important than B.
797 bool isHigherPriorityThan(const InstructionMatcher &B) const {
798 // Instruction matchers involving more operands have higher priority.
799 if (Operands.size() > B.Operands.size())
800 return true;
801 if (Operands.size() < B.Operands.size())
802 return false;
803
804 for (const auto &Predicate : zip(predicates(), B.predicates())) {
805 if (std::get<0>(Predicate)->isHigherPriorityThan(*std::get<1>(Predicate)))
806 return true;
807 if (std::get<1>(Predicate)->isHigherPriorityThan(*std::get<0>(Predicate)))
808 return false;
809 }
810
811 for (const auto &Operand : zip(Operands, B.Operands)) {
Daniel Sanders4f3eb242017-04-05 13:14:03 +0000812 if (std::get<0>(Operand)->isHigherPriorityThan(*std::get<1>(Operand)))
Daniel Sanders759ff412017-02-24 13:58:11 +0000813 return true;
Daniel Sanders4f3eb242017-04-05 13:14:03 +0000814 if (std::get<1>(Operand)->isHigherPriorityThan(*std::get<0>(Operand)))
Daniel Sanders759ff412017-02-24 13:58:11 +0000815 return false;
816 }
817
818 return false;
819 };
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000820
821 /// Report the maximum number of temporary operands needed by the instruction
822 /// matcher.
Daniel Sanders2deea182017-04-22 15:11:04 +0000823 unsigned countRendererFns() const {
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000824 return std::accumulate(predicates().begin(), predicates().end(), 0,
825 [](unsigned A,
826 const std::unique_ptr<InstructionPredicateMatcher>
827 &Predicate) {
Daniel Sanders2deea182017-04-22 15:11:04 +0000828 return A + Predicate->countRendererFns();
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000829 }) +
Daniel Sanders4f3eb242017-04-05 13:14:03 +0000830 std::accumulate(
831 Operands.begin(), Operands.end(), 0,
832 [](unsigned A, const std::unique_ptr<OperandMatcher> &Operand) {
Daniel Sanders2deea182017-04-22 15:11:04 +0000833 return A + Operand->countRendererFns();
Daniel Sanders4f3eb242017-04-05 13:14:03 +0000834 });
Daniel Sanders8a4bae92017-03-14 21:32:08 +0000835 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +0000836};
837
Daniel Sandersbee57392017-04-04 13:25:23 +0000838/// Generates code to check that the operand is a register defined by an
839/// instruction that matches the given instruction matcher.
840///
841/// For example, the pattern:
842/// (set $dst, (G_MUL (G_ADD $src1, $src2), $src3))
843/// would use an InstructionOperandMatcher for operand 1 of the G_MUL to match
844/// the:
845/// (G_ADD $src1, $src2)
846/// subpattern.
847class InstructionOperandMatcher : public OperandPredicateMatcher {
848protected:
849 std::unique_ptr<InstructionMatcher> InsnMatcher;
850
851public:
852 InstructionOperandMatcher()
853 : OperandPredicateMatcher(OPM_Instruction),
854 InsnMatcher(new InstructionMatcher()) {}
855
856 static bool classof(const OperandPredicateMatcher *P) {
857 return P->getKind() == OPM_Instruction;
858 }
859
860 InstructionMatcher &getInsnMatcher() const { return *InsnMatcher; }
861
862 Optional<const OperandMatcher *>
863 getOptionalOperand(StringRef SymbolicName) const override {
864 assert(!SymbolicName.empty() && "Cannot lookup unnamed operand");
865 return InsnMatcher->getOptionalOperand(SymbolicName);
866 }
867
Daniel Sanders9d662d22017-07-06 10:06:12 +0000868 void emitCaptureOpcodes(raw_ostream &OS, RuleMatcher &Rule,
869 unsigned InsnID, unsigned OpIdx) const override {
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000870 unsigned InsnVarID = Rule.defineInsnVar(OS, *InsnMatcher, InsnID, OpIdx);
Daniel Sanders9d662d22017-07-06 10:06:12 +0000871 InsnMatcher->emitCaptureOpcodes(OS, Rule, InsnVarID);
Daniel Sandersbee57392017-04-04 13:25:23 +0000872 }
873
Daniel Sanders9d662d22017-07-06 10:06:12 +0000874 void emitPredicateOpcodes(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sanders6ab0daa2017-07-04 14:35:06 +0000875 unsigned InsnVarID_,
876 unsigned OpIdx_) const override {
877 unsigned InsnVarID = Rule.getInsnVarID(*InsnMatcher);
Daniel Sanders9d662d22017-07-06 10:06:12 +0000878 InsnMatcher->emitPredicateOpcodes(OS, Rule, InsnVarID);
Daniel Sandersbee57392017-04-04 13:25:23 +0000879 }
880};
881
Daniel Sanders43c882c2017-02-01 10:53:10 +0000882//===- Actions ------------------------------------------------------------===//
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000883class OperandRenderer {
884public:
Daniel Sanderscc36dbf2017-06-27 10:11:39 +0000885 enum RendererKind {
886 OR_Copy,
887 OR_CopySubReg,
888 OR_Imm,
889 OR_Register,
890 OR_ComplexPattern
891 };
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000892
893protected:
894 RendererKind Kind;
895
896public:
897 OperandRenderer(RendererKind Kind) : Kind(Kind) {}
898 virtual ~OperandRenderer() {}
899
900 RendererKind getKind() const { return Kind; }
901
Daniel Sanders3ae33202017-07-06 10:37:17 +0000902 virtual void emitRenderOpcodes(raw_ostream &OS, RuleMatcher &Rule) const = 0;
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000903};
904
905/// A CopyRenderer emits code to copy a single operand from an existing
906/// instruction to the one being built.
907class CopyRenderer : public OperandRenderer {
908protected:
Daniel Sandersd93a35a2017-07-05 09:39:33 +0000909 unsigned NewInsnID;
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000910 /// The matcher for the instruction that this operand is copied from.
911 /// This provides the facility for looking up an a operand by it's name so
912 /// that it can be used as a source for the instruction being built.
913 const InstructionMatcher &Matched;
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000914 /// The name of the operand.
915 const StringRef SymbolicName;
916
917public:
Daniel Sandersd93a35a2017-07-05 09:39:33 +0000918 CopyRenderer(unsigned NewInsnID, const InstructionMatcher &Matched,
919 StringRef SymbolicName)
920 : OperandRenderer(OR_Copy), NewInsnID(NewInsnID), Matched(Matched),
921 SymbolicName(SymbolicName) {}
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000922
923 static bool classof(const OperandRenderer *R) {
924 return R->getKind() == OR_Copy;
925 }
926
927 const StringRef getSymbolicName() const { return SymbolicName; }
928
Daniel Sanders3ae33202017-07-06 10:37:17 +0000929 void emitRenderOpcodes(raw_ostream &OS, RuleMatcher &Rule) const override {
Daniel Sandersb96f40d2017-03-20 15:20:42 +0000930 const OperandMatcher &Operand = Matched.getOperand(SymbolicName);
Daniel Sandersd93a35a2017-07-05 09:39:33 +0000931 unsigned OldInsnVarID = Rule.getInsnVarID(Operand.getInstructionMatcher());
Daniel Sandersa6cfce62017-07-05 14:50:18 +0000932 OS << " GIR_Copy, /*NewInsnID*/" << NewInsnID << ", /*OldInsnID*/"
Daniel Sandersd93a35a2017-07-05 09:39:33 +0000933 << OldInsnVarID << ", /*OpIdx*/" << Operand.getOperandIndex() << ", // "
934 << SymbolicName << "\n";
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000935 }
936};
937
Daniel Sanderscc36dbf2017-06-27 10:11:39 +0000938/// A CopySubRegRenderer emits code to copy a single register operand from an
939/// existing instruction to the one being built and indicate that only a
940/// subregister should be copied.
941class CopySubRegRenderer : public OperandRenderer {
942protected:
Daniel Sandersd93a35a2017-07-05 09:39:33 +0000943 unsigned NewInsnID;
Daniel Sanderscc36dbf2017-06-27 10:11:39 +0000944 /// The matcher for the instruction that this operand is copied from.
945 /// This provides the facility for looking up an a operand by it's name so
946 /// that it can be used as a source for the instruction being built.
947 const InstructionMatcher &Matched;
948 /// The name of the operand.
949 const StringRef SymbolicName;
950 /// The subregister to extract.
951 const CodeGenSubRegIndex *SubReg;
952
953public:
Daniel Sandersd93a35a2017-07-05 09:39:33 +0000954 CopySubRegRenderer(unsigned NewInsnID, const InstructionMatcher &Matched,
955 StringRef SymbolicName, const CodeGenSubRegIndex *SubReg)
956 : OperandRenderer(OR_CopySubReg), NewInsnID(NewInsnID), Matched(Matched),
Daniel Sanderscc36dbf2017-06-27 10:11:39 +0000957 SymbolicName(SymbolicName), SubReg(SubReg) {}
958
959 static bool classof(const OperandRenderer *R) {
960 return R->getKind() == OR_CopySubReg;
961 }
962
963 const StringRef getSymbolicName() const { return SymbolicName; }
964
Daniel Sanders3ae33202017-07-06 10:37:17 +0000965 void emitRenderOpcodes(raw_ostream &OS, RuleMatcher &Rule) const override {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +0000966 const OperandMatcher &Operand = Matched.getOperand(SymbolicName);
Daniel Sandersd93a35a2017-07-05 09:39:33 +0000967 unsigned OldInsnVarID = Rule.getInsnVarID(Operand.getInstructionMatcher());
Daniel Sandersa6cfce62017-07-05 14:50:18 +0000968 OS << " GIR_CopySubReg, /*NewInsnID*/" << NewInsnID
Daniel Sandersd93a35a2017-07-05 09:39:33 +0000969 << ", /*OldInsnID*/" << OldInsnVarID << ", /*OpIdx*/"
970 << Operand.getOperandIndex() << ", /*SubRegIdx*/" << SubReg->EnumValue
971 << ", // " << SymbolicName << "\n";
Daniel Sanderscc36dbf2017-06-27 10:11:39 +0000972 }
973};
974
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000975/// Adds a specific physical register to the instruction being built.
976/// This is typically useful for WZR/XZR on AArch64.
977class AddRegisterRenderer : public OperandRenderer {
978protected:
Daniel Sandersd93a35a2017-07-05 09:39:33 +0000979 unsigned InsnID;
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000980 const Record *RegisterDef;
981
982public:
Daniel Sandersd93a35a2017-07-05 09:39:33 +0000983 AddRegisterRenderer(unsigned InsnID, const Record *RegisterDef)
984 : OperandRenderer(OR_Register), InsnID(InsnID), RegisterDef(RegisterDef) {
985 }
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000986
987 static bool classof(const OperandRenderer *R) {
988 return R->getKind() == OR_Register;
989 }
990
Daniel Sanders3ae33202017-07-06 10:37:17 +0000991 void emitRenderOpcodes(raw_ostream &OS, RuleMatcher &Rule) const override {
Daniel Sandersd93a35a2017-07-05 09:39:33 +0000992 OS << " GIR_AddRegister, /*InsnID*/" << InsnID << ", "
993 << (RegisterDef->getValue("Namespace")
994 ? RegisterDef->getValueAsString("Namespace")
995 : "")
996 << "::" << RegisterDef->getName() << ",\n";
Daniel Sanders066ebbf2017-02-24 15:43:30 +0000997 }
998};
999
Daniel Sanders0ed28822017-04-12 08:23:08 +00001000/// Adds a specific immediate to the instruction being built.
1001class ImmRenderer : public OperandRenderer {
1002protected:
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001003 unsigned InsnID;
Daniel Sanders0ed28822017-04-12 08:23:08 +00001004 int64_t Imm;
1005
1006public:
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001007 ImmRenderer(unsigned InsnID, int64_t Imm)
1008 : OperandRenderer(OR_Imm), InsnID(InsnID), Imm(Imm) {}
Daniel Sanders0ed28822017-04-12 08:23:08 +00001009
1010 static bool classof(const OperandRenderer *R) {
1011 return R->getKind() == OR_Imm;
1012 }
1013
Daniel Sanders3ae33202017-07-06 10:37:17 +00001014 void emitRenderOpcodes(raw_ostream &OS, RuleMatcher &Rule) const override {
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001015 OS << " GIR_AddImm, /*InsnID*/" << InsnID << ", /*Imm*/" << Imm
1016 << ",\n";
Daniel Sanders0ed28822017-04-12 08:23:08 +00001017 }
1018};
1019
Daniel Sanders2deea182017-04-22 15:11:04 +00001020/// Adds operands by calling a renderer function supplied by the ComplexPattern
1021/// matcher function.
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001022class RenderComplexPatternOperand : public OperandRenderer {
1023private:
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001024 unsigned InsnID;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001025 const Record &TheDef;
Daniel Sanders2deea182017-04-22 15:11:04 +00001026 /// The name of the operand.
1027 const StringRef SymbolicName;
1028 /// The renderer number. This must be unique within a rule since it's used to
1029 /// identify a temporary variable to hold the renderer function.
1030 unsigned RendererID;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001031
1032 unsigned getNumOperands() const {
1033 return TheDef.getValueAsDag("Operands")->getNumArgs();
1034 }
1035
1036public:
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001037 RenderComplexPatternOperand(unsigned InsnID, const Record &TheDef,
1038 StringRef SymbolicName, unsigned RendererID)
1039 : OperandRenderer(OR_ComplexPattern), InsnID(InsnID), TheDef(TheDef),
Daniel Sanders2deea182017-04-22 15:11:04 +00001040 SymbolicName(SymbolicName), RendererID(RendererID) {}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001041
1042 static bool classof(const OperandRenderer *R) {
1043 return R->getKind() == OR_ComplexPattern;
1044 }
1045
Daniel Sanders3ae33202017-07-06 10:37:17 +00001046 void emitRenderOpcodes(raw_ostream &OS, RuleMatcher &Rule) const override {
Daniel Sandersa6cfce62017-07-05 14:50:18 +00001047 OS << " GIR_ComplexRenderer, /*InsnID*/" << InsnID << ", /*RendererID*/"
1048 << RendererID << ",\n";
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001049 }
1050};
1051
Ahmed Bougacha56ca3a92017-02-04 00:47:10 +00001052/// An action taken when all Matcher predicates succeeded for a parent rule.
1053///
1054/// Typical actions include:
1055/// * Changing the opcode of an instruction.
1056/// * Adding an operand to an instruction.
Daniel Sanders43c882c2017-02-01 10:53:10 +00001057class MatchAction {
1058public:
1059 virtual ~MatchAction() {}
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001060
1061 /// Emit the C++ statements to implement the action.
1062 ///
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001063 /// \param RecycleInsnID If given, it's an instruction to recycle. The
1064 /// requirements on the instruction vary from action to
1065 /// action.
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001066 virtual void emitCxxActionStmts(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001067 unsigned RecycleInsnID) const = 0;
Daniel Sanders43c882c2017-02-01 10:53:10 +00001068};
1069
Ahmed Bougacha9aa4c102017-02-04 00:47:08 +00001070/// Generates a comment describing the matched rule being acted upon.
1071class DebugCommentAction : public MatchAction {
1072private:
1073 const PatternToMatch &P;
1074
1075public:
1076 DebugCommentAction(const PatternToMatch &P) : P(P) {}
1077
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001078 void emitCxxActionStmts(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001079 unsigned RecycleInsnID) const override {
Daniel Sandersa6cfce62017-07-05 14:50:18 +00001080 OS << " // " << *P.getSrcPattern() << " => " << *P.getDstPattern()
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001081 << "\n";
Ahmed Bougacha9aa4c102017-02-04 00:47:08 +00001082 }
1083};
1084
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001085/// Generates code to build an instruction or mutate an existing instruction
1086/// into the desired instruction when this is possible.
1087class BuildMIAction : public MatchAction {
Daniel Sanders43c882c2017-02-01 10:53:10 +00001088private:
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001089 unsigned InsnID;
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001090 const CodeGenInstruction *I;
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001091 const InstructionMatcher &Matched;
1092 std::vector<std::unique_ptr<OperandRenderer>> OperandRenderers;
1093
1094 /// True if the instruction can be built solely by mutating the opcode.
1095 bool canMutate() const {
Daniel Sanderse9fdba32017-04-29 17:30:09 +00001096 if (OperandRenderers.size() != Matched.getNumOperands())
1097 return false;
1098
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001099 for (const auto &Renderer : enumerate(OperandRenderers)) {
Zachary Turner309a0882017-03-13 16:24:10 +00001100 if (const auto *Copy = dyn_cast<CopyRenderer>(&*Renderer.value())) {
Daniel Sanders3016d3c2017-04-22 14:31:28 +00001101 const OperandMatcher &OM = Matched.getOperand(Copy->getSymbolicName());
1102 if (&Matched != &OM.getInstructionMatcher() ||
1103 OM.getOperandIndex() != Renderer.index())
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001104 return false;
1105 } else
1106 return false;
1107 }
1108
1109 return true;
1110 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001111
Daniel Sanders43c882c2017-02-01 10:53:10 +00001112public:
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001113 BuildMIAction(unsigned InsnID, const CodeGenInstruction *I,
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001114 const InstructionMatcher &Matched)
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001115 : InsnID(InsnID), I(I), Matched(Matched) {}
Daniel Sanders43c882c2017-02-01 10:53:10 +00001116
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001117 template <class Kind, class... Args>
1118 Kind &addRenderer(Args&&... args) {
1119 OperandRenderers.emplace_back(
1120 llvm::make_unique<Kind>(std::forward<Args>(args)...));
1121 return *static_cast<Kind *>(OperandRenderers.back().get());
1122 }
1123
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001124 void emitCxxActionStmts(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001125 unsigned RecycleInsnID) const override {
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001126 if (canMutate()) {
Daniel Sandersa6cfce62017-07-05 14:50:18 +00001127 OS << " GIR_MutateOpcode, /*InsnID*/" << InsnID
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001128 << ", /*RecycleInsnID*/ " << RecycleInsnID << ", /*Opcode*/"
1129 << I->Namespace << "::" << I->TheDef->getName() << ",\n";
Tim Northover4340d642017-03-20 21:58:23 +00001130
1131 if (!I->ImplicitDefs.empty() || !I->ImplicitUses.empty()) {
Tim Northover4340d642017-03-20 21:58:23 +00001132 for (auto Def : I->ImplicitDefs) {
Diana Picus8abcbbb2017-05-02 09:40:49 +00001133 auto Namespace = Def->getValue("Namespace")
1134 ? Def->getValueAsString("Namespace")
1135 : "";
Daniel Sandersa6cfce62017-07-05 14:50:18 +00001136 OS << " GIR_AddImplicitDef, " << InsnID << ", " << Namespace
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001137 << "::" << Def->getName() << ",\n";
Tim Northover4340d642017-03-20 21:58:23 +00001138 }
1139 for (auto Use : I->ImplicitUses) {
Diana Picus8abcbbb2017-05-02 09:40:49 +00001140 auto Namespace = Use->getValue("Namespace")
1141 ? Use->getValueAsString("Namespace")
1142 : "";
Daniel Sandersa6cfce62017-07-05 14:50:18 +00001143 OS << " GIR_AddImplicitUse, " << InsnID << ", " << Namespace
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001144 << "::" << Use->getName() << ",\n";
Tim Northover4340d642017-03-20 21:58:23 +00001145 }
1146 }
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001147 return;
1148 }
1149
1150 // TODO: Simple permutation looks like it could be almost as common as
1151 // mutation due to commutative operations.
1152
Daniel Sandersa6cfce62017-07-05 14:50:18 +00001153 OS << " GIR_BuildMI, /*InsnID*/" << InsnID << ", /*Opcode*/"
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001154 << I->Namespace << "::" << I->TheDef->getName() << ",\n";
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001155 for (const auto &Renderer : OperandRenderers)
Daniel Sanders3ae33202017-07-06 10:37:17 +00001156 Renderer->emitRenderOpcodes(OS, Rule);
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001157
Daniel Sandersa6cfce62017-07-05 14:50:18 +00001158 OS << " GIR_MergeMemOperands, /*InsnID*/" << InsnID << ",\n"
1159 << " GIR_EraseFromParent, /*InsnID*/" << RecycleInsnID << ",\n";
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001160 }
1161};
1162
1163/// Generates code to constrain the operands of an output instruction to the
1164/// register classes specified by the definition of that instruction.
1165class ConstrainOperandsToDefinitionAction : public MatchAction {
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001166 unsigned InsnID;
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001167
1168public:
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001169 ConstrainOperandsToDefinitionAction(unsigned InsnID) : InsnID(InsnID) {}
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001170
1171 void emitCxxActionStmts(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001172 unsigned RecycleInsnID) const override {
Daniel Sandersa6cfce62017-07-05 14:50:18 +00001173 OS << " GIR_ConstrainSelectedInstOperands, /*InsnID*/" << InsnID << ",\n";
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001174 }
1175};
1176
1177/// Generates code to constrain the specified operand of an output instruction
1178/// to the specified register class.
1179class ConstrainOperandToRegClassAction : public MatchAction {
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001180 unsigned InsnID;
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001181 unsigned OpIdx;
1182 const CodeGenRegisterClass &RC;
1183
1184public:
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001185 ConstrainOperandToRegClassAction(unsigned InsnID, unsigned OpIdx,
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001186 const CodeGenRegisterClass &RC)
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001187 : InsnID(InsnID), OpIdx(OpIdx), RC(RC) {}
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001188
1189 void emitCxxActionStmts(raw_ostream &OS, RuleMatcher &Rule,
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001190 unsigned RecycleInsnID) const override {
Daniel Sandersa6cfce62017-07-05 14:50:18 +00001191 OS << " GIR_ConstrainOperandRC, /*InsnID*/" << InsnID << ", /*Op*/"
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001192 << OpIdx << ", /*RC " << RC.getName() << "*/ " << RC.EnumValue << ",\n";
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001193 }
1194};
1195
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001196InstructionMatcher &RuleMatcher::addInstructionMatcher() {
1197 Matchers.emplace_back(new InstructionMatcher());
1198 return *Matchers.back();
1199}
Ahmed Bougacha56ca3a92017-02-04 00:47:10 +00001200
Daniel Sanderse7b0d662017-04-21 15:59:56 +00001201void RuleMatcher::addRequiredFeature(Record *Feature) {
1202 RequiredFeatures.push_back(Feature);
1203}
1204
Daniel Sanders6ab0daa2017-07-04 14:35:06 +00001205const std::vector<Record *> &RuleMatcher::getRequiredFeatures() const {
1206 return RequiredFeatures;
1207}
1208
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001209template <class Kind, class... Args>
1210Kind &RuleMatcher::addAction(Args &&... args) {
1211 Actions.emplace_back(llvm::make_unique<Kind>(std::forward<Args>(args)...));
1212 return *static_cast<Kind *>(Actions.back().get());
1213}
Daniel Sandersdc662ff2017-01-26 11:10:14 +00001214
Daniel Sanders6ab0daa2017-07-04 14:35:06 +00001215unsigned
1216RuleMatcher::implicitlyDefineInsnVar(const InstructionMatcher &Matcher) {
1217 unsigned NewInsnVarID = NextInsnVarID++;
1218 InsnVariableIDs[&Matcher] = NewInsnVarID;
1219 return NewInsnVarID;
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001220}
1221
Daniel Sanders6ab0daa2017-07-04 14:35:06 +00001222unsigned RuleMatcher::defineInsnVar(raw_ostream &OS,
1223 const InstructionMatcher &Matcher,
1224 unsigned InsnID, unsigned OpIdx) {
1225 unsigned NewInsnVarID = implicitlyDefineInsnVar(Matcher);
1226 OS << " GIM_RecordInsn, /*DefineMI*/" << NewInsnVarID << ", /*MI*/"
1227 << InsnID << ", /*OpIdx*/" << OpIdx << ", // MIs[" << NewInsnVarID
1228 << "]\n";
1229 return NewInsnVarID;
1230}
1231
1232unsigned RuleMatcher::getInsnVarID(const InstructionMatcher &InsnMatcher) const {
1233 const auto &I = InsnVariableIDs.find(&InsnMatcher);
1234 if (I != InsnVariableIDs.end())
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001235 return I->second;
1236 llvm_unreachable("Matched Insn was not captured in a local variable");
1237}
1238
Daniel Sanders9d662d22017-07-06 10:06:12 +00001239/// Emit MatchTable opcodes to check the shape of the match and capture
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001240/// instructions into local variables.
Daniel Sanders9d662d22017-07-06 10:06:12 +00001241void RuleMatcher::emitCaptureOpcodes(raw_ostream &OS) {
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001242 assert(Matchers.size() == 1 && "Cannot handle multi-root matchers yet");
Daniel Sanders6ab0daa2017-07-04 14:35:06 +00001243 unsigned InsnVarID = implicitlyDefineInsnVar(*Matchers.front());
Daniel Sanders9d662d22017-07-06 10:06:12 +00001244 Matchers.front()->emitCaptureOpcodes(OS, *this, InsnVarID);
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001245}
1246
Daniel Sanders6ab0daa2017-07-04 14:35:06 +00001247void RuleMatcher::emit(raw_ostream &OS) {
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001248 if (Matchers.empty())
1249 llvm_unreachable("Unexpected empty matcher!");
Daniel Sandersdc662ff2017-01-26 11:10:14 +00001250
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001251 // The representation supports rules that require multiple roots such as:
1252 // %ptr(p0) = ...
1253 // %elt0(s32) = G_LOAD %ptr
1254 // %1(p0) = G_ADD %ptr, 4
1255 // %elt1(s32) = G_LOAD p0 %1
1256 // which could be usefully folded into:
1257 // %ptr(p0) = ...
1258 // %elt0(s32), %elt1(s32) = TGT_LOAD_PAIR %ptr
1259 // on some targets but we don't need to make use of that yet.
1260 assert(Matchers.size() == 1 && "Cannot handle multi-root matchers yet");
Daniel Sanderse7b0d662017-04-21 15:59:56 +00001261
Daniel Sandersc60abe32017-07-04 15:31:50 +00001262 OS << " const static int64_t MatchTable" << CurrentMatchTableID << "[] = {\n";
Daniel Sanderse7b0d662017-04-21 15:59:56 +00001263 if (!RequiredFeatures.empty()) {
Daniel Sanders6ab0daa2017-07-04 14:35:06 +00001264 OS << " GIM_CheckFeatures, " << getNameForFeatureBitset(RequiredFeatures)
1265 << ",\n";
Daniel Sanderse7b0d662017-04-21 15:59:56 +00001266 }
Daniel Sandersb96f40d2017-03-20 15:20:42 +00001267
Daniel Sanders9d662d22017-07-06 10:06:12 +00001268 emitCaptureOpcodes(OS);
Daniel Sanders6ab0daa2017-07-04 14:35:06 +00001269
Daniel Sanders9d662d22017-07-06 10:06:12 +00001270 Matchers.front()->emitPredicateOpcodes(OS, *this,
Daniel Sanders6ab0daa2017-07-04 14:35:06 +00001271 getInsnVarID(*Matchers.front()));
1272
Daniel Sandersbee57392017-04-04 13:25:23 +00001273 // We must also check if it's safe to fold the matched instructions.
Daniel Sanders6ab0daa2017-07-04 14:35:06 +00001274 if (InsnVariableIDs.size() >= 2) {
Galina Kistanova1754fee2017-05-25 01:51:53 +00001275 // Invert the map to create stable ordering (by var names)
Daniel Sanders6ab0daa2017-07-04 14:35:06 +00001276 SmallVector<unsigned, 2> InsnIDs;
1277 for (const auto &Pair : InsnVariableIDs) {
Daniel Sandersbee57392017-04-04 13:25:23 +00001278 // Skip the root node since it isn't moving anywhere. Everything else is
1279 // sinking to meet it.
1280 if (Pair.first == Matchers.front().get())
1281 continue;
1282
Daniel Sanders6ab0daa2017-07-04 14:35:06 +00001283 InsnIDs.push_back(Pair.second);
Galina Kistanova1754fee2017-05-25 01:51:53 +00001284 }
Daniel Sanders6ab0daa2017-07-04 14:35:06 +00001285 std::sort(InsnIDs.begin(), InsnIDs.end());
Galina Kistanova1754fee2017-05-25 01:51:53 +00001286
Daniel Sanders6ab0daa2017-07-04 14:35:06 +00001287 for (const auto &InsnID : InsnIDs) {
Daniel Sandersbee57392017-04-04 13:25:23 +00001288 // Reject the difficult cases until we have a more accurate check.
Daniel Sandersa6cfce62017-07-05 14:50:18 +00001289 OS << " GIM_CheckIsSafeToFold, /*InsnID*/" << InsnID << ",\n";
Daniel Sandersbee57392017-04-04 13:25:23 +00001290
1291 // FIXME: Emit checks to determine it's _actually_ safe to fold and/or
1292 // account for unsafe cases.
1293 //
1294 // Example:
1295 // MI1--> %0 = ...
1296 // %1 = ... %0
1297 // MI0--> %2 = ... %0
1298 // It's not safe to erase MI1. We currently handle this by not
1299 // erasing %0 (even when it's dead).
1300 //
1301 // Example:
1302 // MI1--> %0 = load volatile @a
1303 // %1 = load volatile @a
1304 // MI0--> %2 = ... %0
1305 // It's not safe to sink %0's def past %1. We currently handle
1306 // this by rejecting all loads.
1307 //
1308 // Example:
1309 // MI1--> %0 = load @a
1310 // %1 = store @a
1311 // MI0--> %2 = ... %0
1312 // It's not safe to sink %0's def past %1. We currently handle
1313 // this by rejecting all loads.
1314 //
1315 // Example:
1316 // G_CONDBR %cond, @BB1
1317 // BB0:
1318 // MI1--> %0 = load @a
1319 // G_BR @BB1
1320 // BB1:
1321 // MI0--> %2 = ... %0
1322 // It's not always safe to sink %0 across control flow. In this
1323 // case it may introduce a memory fault. We currentl handle this
1324 // by rejecting all loads.
1325 }
1326 }
1327
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001328 for (const auto &MA : Actions)
1329 MA->emitCxxActionStmts(OS, *this, 0);
Daniel Sandersa6cfce62017-07-05 14:50:18 +00001330 OS << " GIR_Done,\n"
1331 << " };\n"
1332 << " State.MIs.resize(1);\n"
1333 << " DEBUG(dbgs() << \"Processing MatchTable" << CurrentMatchTableID
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001334 << "\\n\");\n"
Daniel Sandersa6cfce62017-07-05 14:50:18 +00001335 << " if (executeMatchTable(*this, OutMIs, State, MatcherInfo, MatchTable"
1336 << CurrentMatchTableID << ", TII, MRI, TRI, RBI, AvailableFeatures)) {\n"
1337 << " return true;\n"
1338 << " }\n\n";
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001339}
Daniel Sanders43c882c2017-02-01 10:53:10 +00001340
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001341bool RuleMatcher::isHigherPriorityThan(const RuleMatcher &B) const {
1342 // Rules involving more match roots have higher priority.
1343 if (Matchers.size() > B.Matchers.size())
1344 return true;
1345 if (Matchers.size() < B.Matchers.size())
Daniel Sanders759ff412017-02-24 13:58:11 +00001346 return false;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001347
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001348 for (const auto &Matcher : zip(Matchers, B.Matchers)) {
1349 if (std::get<0>(Matcher)->isHigherPriorityThan(*std::get<1>(Matcher)))
1350 return true;
1351 if (std::get<1>(Matcher)->isHigherPriorityThan(*std::get<0>(Matcher)))
1352 return false;
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001353 }
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001354
1355 return false;
Simon Pilgrima7d1da82017-03-15 22:50:47 +00001356}
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001357
Daniel Sanders2deea182017-04-22 15:11:04 +00001358unsigned RuleMatcher::countRendererFns() const {
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001359 return std::accumulate(
1360 Matchers.begin(), Matchers.end(), 0,
1361 [](unsigned A, const std::unique_ptr<InstructionMatcher> &Matcher) {
Daniel Sanders2deea182017-04-22 15:11:04 +00001362 return A + Matcher->countRendererFns();
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001363 });
1364}
1365
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001366//===- GlobalISelEmitter class --------------------------------------------===//
1367
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001368class GlobalISelEmitter {
1369public:
1370 explicit GlobalISelEmitter(RecordKeeper &RK);
1371 void run(raw_ostream &OS);
1372
1373private:
1374 const RecordKeeper &RK;
1375 const CodeGenDAGPatterns CGP;
1376 const CodeGenTarget &Target;
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001377 CodeGenRegBank CGRegs;
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001378
1379 /// Keep track of the equivalence between SDNodes and Instruction.
1380 /// This is defined using 'GINodeEquiv' in the target description.
1381 DenseMap<Record *, const CodeGenInstruction *> NodeEquivs;
1382
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001383 /// Keep track of the equivalence between ComplexPattern's and
1384 /// GIComplexOperandMatcher. Map entries are specified by subclassing
1385 /// GIComplexPatternEquiv.
1386 DenseMap<const Record *, const Record *> ComplexPatternEquivs;
1387
Daniel Sanderse7b0d662017-04-21 15:59:56 +00001388 // Map of predicates to their subtarget features.
Daniel Sanderse9fdba32017-04-29 17:30:09 +00001389 SubtargetFeatureInfoMap SubtargetFeatures;
Daniel Sanderse7b0d662017-04-21 15:59:56 +00001390
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001391 void gatherNodeEquivs();
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001392 const CodeGenInstruction *findNodeEquiv(Record *N) const;
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001393
Daniel Sanderse7b0d662017-04-21 15:59:56 +00001394 Error importRulePredicates(RuleMatcher &M, ArrayRef<Init *> Predicates);
Daniel Sandersffc7d582017-03-29 15:37:18 +00001395 Expected<InstructionMatcher &>
Daniel Sandersc270c502017-03-30 09:36:33 +00001396 createAndImportSelDAGMatcher(InstructionMatcher &InsnMatcher,
1397 const TreePatternNode *Src) const;
1398 Error importChildMatcher(InstructionMatcher &InsnMatcher,
Daniel Sanders452c8ae2017-05-23 19:33:16 +00001399 const TreePatternNode *SrcChild, unsigned OpIdx,
Daniel Sandersc270c502017-03-30 09:36:33 +00001400 unsigned &TempOpIdx) const;
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001401 Expected<BuildMIAction &>
1402 createAndImportInstructionRenderer(RuleMatcher &M, const TreePatternNode *Dst,
1403 const InstructionMatcher &InsnMatcher);
Daniel Sandersc270c502017-03-30 09:36:33 +00001404 Error importExplicitUseRenderer(BuildMIAction &DstMIBuilder,
1405 TreePatternNode *DstChild,
Daniel Sanders4f3eb242017-04-05 13:14:03 +00001406 const InstructionMatcher &InsnMatcher) const;
Diana Picus382602f2017-05-17 08:57:28 +00001407 Error importDefaultOperandRenderers(BuildMIAction &DstMIBuilder,
1408 DagInit *DefaultOps) const;
Daniel Sandersc270c502017-03-30 09:36:33 +00001409 Error
Daniel Sandersffc7d582017-03-29 15:37:18 +00001410 importImplicitDefRenderers(BuildMIAction &DstMIBuilder,
1411 const std::vector<Record *> &ImplicitDefs) const;
1412
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001413 /// Analyze pattern \p P, returning a matcher for it if possible.
1414 /// Otherwise, return an Error explaining why we don't support it.
1415 Expected<RuleMatcher> runOnPattern(const PatternToMatch &P);
Daniel Sanderse7b0d662017-04-21 15:59:56 +00001416
1417 void declareSubtargetFeature(Record *Predicate);
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001418};
1419
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001420void GlobalISelEmitter::gatherNodeEquivs() {
1421 assert(NodeEquivs.empty());
1422 for (Record *Equiv : RK.getAllDerivedDefinitions("GINodeEquiv"))
1423 NodeEquivs[Equiv->getValueAsDef("Node")] =
1424 &Target.getInstruction(Equiv->getValueAsDef("I"));
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001425
1426 assert(ComplexPatternEquivs.empty());
1427 for (Record *Equiv : RK.getAllDerivedDefinitions("GIComplexPatternEquiv")) {
1428 Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent");
1429 if (!SelDAGEquiv)
1430 continue;
1431 ComplexPatternEquivs[SelDAGEquiv] = Equiv;
1432 }
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001433}
1434
Daniel Sandersbdfebb82017-03-15 20:18:38 +00001435const CodeGenInstruction *GlobalISelEmitter::findNodeEquiv(Record *N) const {
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001436 return NodeEquivs.lookup(N);
1437}
1438
1439GlobalISelEmitter::GlobalISelEmitter(RecordKeeper &RK)
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001440 : RK(RK), CGP(RK), Target(CGP.getTargetInfo()), CGRegs(RK) {}
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001441
1442//===- Emitter ------------------------------------------------------------===//
1443
Daniel Sandersc270c502017-03-30 09:36:33 +00001444Error
Daniel Sandersffc7d582017-03-29 15:37:18 +00001445GlobalISelEmitter::importRulePredicates(RuleMatcher &M,
Daniel Sanderse7b0d662017-04-21 15:59:56 +00001446 ArrayRef<Init *> Predicates) {
1447 for (const Init *Predicate : Predicates) {
1448 const DefInit *PredicateDef = static_cast<const DefInit *>(Predicate);
1449 declareSubtargetFeature(PredicateDef->getDef());
1450 M.addRequiredFeature(PredicateDef->getDef());
1451 }
1452
Daniel Sandersc270c502017-03-30 09:36:33 +00001453 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001454}
Daniel Sanders8a4bae92017-03-14 21:32:08 +00001455
Daniel Sandersc270c502017-03-30 09:36:33 +00001456Expected<InstructionMatcher &> GlobalISelEmitter::createAndImportSelDAGMatcher(
1457 InstructionMatcher &InsnMatcher, const TreePatternNode *Src) const {
Daniel Sanders85ffd362017-07-06 08:12:20 +00001458 const CodeGenInstruction *SrcGIOrNull = nullptr;
1459
Daniel Sandersffc7d582017-03-29 15:37:18 +00001460 // Start with the defined operands (i.e., the results of the root operator).
1461 if (Src->getExtTypes().size() > 1)
1462 return failedImport("Src pattern has multiple results");
1463
Daniel Sanders452c8ae2017-05-23 19:33:16 +00001464 if (Src->isLeaf()) {
1465 Init *SrcInit = Src->getLeafValue();
Daniel Sanders3334cc02017-05-23 20:02:48 +00001466 if (isa<IntInit>(SrcInit)) {
Daniel Sanders452c8ae2017-05-23 19:33:16 +00001467 InsnMatcher.addPredicate<InstructionOpcodeMatcher>(
1468 &Target.getInstruction(RK.getDef("G_CONSTANT")));
1469 } else
Daniel Sanders32291982017-06-28 13:50:04 +00001470 return failedImport(
1471 "Unable to deduce gMIR opcode to handle Src (which is a leaf)");
Daniel Sanders452c8ae2017-05-23 19:33:16 +00001472 } else {
Daniel Sanders85ffd362017-07-06 08:12:20 +00001473 SrcGIOrNull = findNodeEquiv(Src->getOperator());
Daniel Sanders452c8ae2017-05-23 19:33:16 +00001474 if (!SrcGIOrNull)
1475 return failedImport("Pattern operator lacks an equivalent Instruction" +
1476 explainOperator(Src->getOperator()));
1477 auto &SrcGI = *SrcGIOrNull;
Daniel Sandersffc7d582017-03-29 15:37:18 +00001478
Daniel Sanders452c8ae2017-05-23 19:33:16 +00001479 // The operators look good: match the opcode
1480 InsnMatcher.addPredicate<InstructionOpcodeMatcher>(&SrcGI);
1481 }
Daniel Sandersffc7d582017-03-29 15:37:18 +00001482
1483 unsigned OpIdx = 0;
Daniel Sanders4f3eb242017-04-05 13:14:03 +00001484 unsigned TempOpIdx = 0;
Daniel Sandersffc7d582017-03-29 15:37:18 +00001485 for (const EEVT::TypeSet &Ty : Src->getExtTypes()) {
1486 auto OpTyOrNone = MVTToLLT(Ty.getConcrete());
1487
1488 if (!OpTyOrNone)
1489 return failedImport(
1490 "Result of Src pattern operator has an unsupported type");
1491
1492 // Results don't have a name unless they are the root node. The caller will
1493 // set the name if appropriate.
Daniel Sanders4f3eb242017-04-05 13:14:03 +00001494 OperandMatcher &OM = InsnMatcher.addOperand(OpIdx++, "", TempOpIdx);
Daniel Sandersffc7d582017-03-29 15:37:18 +00001495 OM.addPredicate<LLTOperandMatcher>(*OpTyOrNone);
1496 }
1497
Daniel Sanders452c8ae2017-05-23 19:33:16 +00001498 if (Src->isLeaf()) {
1499 Init *SrcInit = Src->getLeafValue();
1500 if (IntInit *SrcIntInit = dyn_cast<IntInit>(SrcInit)) {
1501 OperandMatcher &OM = InsnMatcher.addOperand(OpIdx++, "", TempOpIdx);
1502 OM.addPredicate<LiteralIntOperandMatcher>(SrcIntInit->getValue());
1503 } else
Daniel Sanders32291982017-06-28 13:50:04 +00001504 return failedImport(
1505 "Unable to deduce gMIR opcode to handle Src (which is a leaf)");
Daniel Sanders452c8ae2017-05-23 19:33:16 +00001506 } else {
Daniel Sanders85ffd362017-07-06 08:12:20 +00001507 assert(SrcGIOrNull &&
1508 "Expected to have already found an equivalent Instruction");
Daniel Sanders452c8ae2017-05-23 19:33:16 +00001509 // Match the used operands (i.e. the children of the operator).
1510 for (unsigned i = 0, e = Src->getNumChildren(); i != e; ++i) {
Daniel Sanders85ffd362017-07-06 08:12:20 +00001511 TreePatternNode *SrcChild = Src->getChild(i);
1512
1513 // For G_INTRINSIC, the operand immediately following the defs is an
1514 // intrinsic ID.
1515 if (SrcGIOrNull->TheDef->getName() == "G_INTRINSIC" && i == 0) {
1516 if (!SrcChild->isLeaf())
1517 return failedImport("Expected IntInit containing intrinsic ID");
1518
1519 if (IntInit *SrcChildIntInit =
1520 dyn_cast<IntInit>(SrcChild->getLeafValue())) {
1521 OperandMatcher &OM =
1522 InsnMatcher.addOperand(OpIdx++, SrcChild->getName(), TempOpIdx);
1523 OM.addPredicate<LiteralIntOperandMatcher>(SrcChildIntInit->getValue());
1524 continue;
1525 }
1526
1527 return failedImport("Expected IntInit containing instrinsic ID)");
1528 }
1529
1530 if (auto Error =
1531 importChildMatcher(InsnMatcher, SrcChild, OpIdx++, TempOpIdx))
Daniel Sanders452c8ae2017-05-23 19:33:16 +00001532 return std::move(Error);
1533 }
Daniel Sandersffc7d582017-03-29 15:37:18 +00001534 }
1535
1536 return InsnMatcher;
1537}
1538
Daniel Sandersc270c502017-03-30 09:36:33 +00001539Error GlobalISelEmitter::importChildMatcher(InstructionMatcher &InsnMatcher,
Daniel Sanders452c8ae2017-05-23 19:33:16 +00001540 const TreePatternNode *SrcChild,
Daniel Sandersc270c502017-03-30 09:36:33 +00001541 unsigned OpIdx,
1542 unsigned &TempOpIdx) const {
Daniel Sanders4f3eb242017-04-05 13:14:03 +00001543 OperandMatcher &OM =
1544 InsnMatcher.addOperand(OpIdx, SrcChild->getName(), TempOpIdx);
Daniel Sandersffc7d582017-03-29 15:37:18 +00001545
1546 if (SrcChild->hasAnyPredicate())
Daniel Sandersd0656a32017-04-13 09:45:37 +00001547 return failedImport("Src pattern child has predicate (" +
1548 explainPredicates(SrcChild) + ")");
Daniel Sandersffc7d582017-03-29 15:37:18 +00001549
1550 ArrayRef<EEVT::TypeSet> ChildTypes = SrcChild->getExtTypes();
1551 if (ChildTypes.size() != 1)
1552 return failedImport("Src pattern child has multiple results");
1553
1554 // Check MBB's before the type check since they are not a known type.
1555 if (!SrcChild->isLeaf()) {
1556 if (SrcChild->getOperator()->isSubClassOf("SDNode")) {
1557 auto &ChildSDNI = CGP.getSDNodeInfo(SrcChild->getOperator());
1558 if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") {
1559 OM.addPredicate<MBBOperandMatcher>();
Daniel Sandersc270c502017-03-30 09:36:33 +00001560 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001561 }
1562 }
Daniel Sandersffc7d582017-03-29 15:37:18 +00001563 }
1564
1565 auto OpTyOrNone = MVTToLLT(ChildTypes.front().getConcrete());
1566 if (!OpTyOrNone)
Daniel Sanders85ffd362017-07-06 08:12:20 +00001567 return failedImport("Src operand has an unsupported type (" + to_string(*SrcChild) + ")");
Daniel Sandersffc7d582017-03-29 15:37:18 +00001568 OM.addPredicate<LLTOperandMatcher>(*OpTyOrNone);
1569
Daniel Sandersbee57392017-04-04 13:25:23 +00001570 // Check for nested instructions.
1571 if (!SrcChild->isLeaf()) {
1572 // Map the node to a gMIR instruction.
1573 InstructionOperandMatcher &InsnOperand =
1574 OM.addPredicate<InstructionOperandMatcher>();
1575 auto InsnMatcherOrError =
1576 createAndImportSelDAGMatcher(InsnOperand.getInsnMatcher(), SrcChild);
1577 if (auto Error = InsnMatcherOrError.takeError())
1578 return Error;
1579
1580 return Error::success();
1581 }
1582
Daniel Sandersffc7d582017-03-29 15:37:18 +00001583 // Check for constant immediates.
1584 if (auto *ChildInt = dyn_cast<IntInit>(SrcChild->getLeafValue())) {
Daniel Sanders452c8ae2017-05-23 19:33:16 +00001585 OM.addPredicate<ConstantIntOperandMatcher>(ChildInt->getValue());
Daniel Sandersc270c502017-03-30 09:36:33 +00001586 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001587 }
1588
1589 // Check for def's like register classes or ComplexPattern's.
1590 if (auto *ChildDefInit = dyn_cast<DefInit>(SrcChild->getLeafValue())) {
1591 auto *ChildRec = ChildDefInit->getDef();
1592
1593 // Check for register classes.
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001594 if (ChildRec->isSubClassOf("RegisterClass") ||
1595 ChildRec->isSubClassOf("RegisterOperand")) {
Daniel Sandersffc7d582017-03-29 15:37:18 +00001596 OM.addPredicate<RegisterBankOperandMatcher>(
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001597 Target.getRegisterClass(getInitValueAsRegClass(ChildDefInit)));
Daniel Sanders658541f2017-04-22 15:53:21 +00001598 return Error::success();
1599 }
1600
Daniel Sandersffc7d582017-03-29 15:37:18 +00001601 // Check for ComplexPattern's.
1602 if (ChildRec->isSubClassOf("ComplexPattern")) {
1603 const auto &ComplexPattern = ComplexPatternEquivs.find(ChildRec);
1604 if (ComplexPattern == ComplexPatternEquivs.end())
Daniel Sandersd0656a32017-04-13 09:45:37 +00001605 return failedImport("SelectionDAG ComplexPattern (" +
1606 ChildRec->getName() + ") not mapped to GlobalISel");
Daniel Sandersffc7d582017-03-29 15:37:18 +00001607
Daniel Sanders2deea182017-04-22 15:11:04 +00001608 OM.addPredicate<ComplexPatternOperandMatcher>(OM,
1609 *ComplexPattern->second);
1610 TempOpIdx++;
Daniel Sandersc270c502017-03-30 09:36:33 +00001611 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001612 }
1613
Daniel Sandersd0656a32017-04-13 09:45:37 +00001614 if (ChildRec->isSubClassOf("ImmLeaf")) {
1615 return failedImport(
1616 "Src pattern child def is an unsupported tablegen class (ImmLeaf)");
1617 }
1618
Daniel Sandersffc7d582017-03-29 15:37:18 +00001619 return failedImport(
1620 "Src pattern child def is an unsupported tablegen class");
1621 }
1622
1623 return failedImport("Src pattern child is an unsupported kind");
1624}
1625
Daniel Sandersc270c502017-03-30 09:36:33 +00001626Error GlobalISelEmitter::importExplicitUseRenderer(
Daniel Sandersffc7d582017-03-29 15:37:18 +00001627 BuildMIAction &DstMIBuilder, TreePatternNode *DstChild,
Daniel Sanders4f3eb242017-04-05 13:14:03 +00001628 const InstructionMatcher &InsnMatcher) const {
Daniel Sandersffc7d582017-03-29 15:37:18 +00001629 // The only non-leaf child we accept is 'bb': it's an operator because
1630 // BasicBlockSDNode isn't inline, but in MI it's just another operand.
1631 if (!DstChild->isLeaf()) {
1632 if (DstChild->getOperator()->isSubClassOf("SDNode")) {
1633 auto &ChildSDNI = CGP.getSDNodeInfo(DstChild->getOperator());
1634 if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") {
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001635 DstMIBuilder.addRenderer<CopyRenderer>(0, InsnMatcher,
Daniel Sandersffc7d582017-03-29 15:37:18 +00001636 DstChild->getName());
Daniel Sandersc270c502017-03-30 09:36:33 +00001637 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001638 }
1639 }
1640 return failedImport("Dst pattern child isn't a leaf node or an MBB");
1641 }
1642
1643 // Otherwise, we're looking for a bog-standard RegisterClass operand.
1644 if (DstChild->hasAnyPredicate())
Daniel Sandersd0656a32017-04-13 09:45:37 +00001645 return failedImport("Dst pattern child has predicate (" +
1646 explainPredicates(DstChild) + ")");
Daniel Sandersffc7d582017-03-29 15:37:18 +00001647
1648 if (auto *ChildDefInit = dyn_cast<DefInit>(DstChild->getLeafValue())) {
1649 auto *ChildRec = ChildDefInit->getDef();
1650
1651 ArrayRef<EEVT::TypeSet> ChildTypes = DstChild->getExtTypes();
1652 if (ChildTypes.size() != 1)
1653 return failedImport("Dst pattern child has multiple results");
1654
1655 auto OpTyOrNone = MVTToLLT(ChildTypes.front().getConcrete());
1656 if (!OpTyOrNone)
1657 return failedImport("Dst operand has an unsupported type");
1658
1659 if (ChildRec->isSubClassOf("Register")) {
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001660 DstMIBuilder.addRenderer<AddRegisterRenderer>(0, ChildRec);
Daniel Sandersc270c502017-03-30 09:36:33 +00001661 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001662 }
1663
Daniel Sanders658541f2017-04-22 15:53:21 +00001664 if (ChildRec->isSubClassOf("RegisterClass") ||
1665 ChildRec->isSubClassOf("RegisterOperand")) {
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001666 DstMIBuilder.addRenderer<CopyRenderer>(0, InsnMatcher,
1667 DstChild->getName());
Daniel Sandersc270c502017-03-30 09:36:33 +00001668 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001669 }
1670
1671 if (ChildRec->isSubClassOf("ComplexPattern")) {
1672 const auto &ComplexPattern = ComplexPatternEquivs.find(ChildRec);
1673 if (ComplexPattern == ComplexPatternEquivs.end())
1674 return failedImport(
1675 "SelectionDAG ComplexPattern not mapped to GlobalISel");
1676
Daniel Sanders4f3eb242017-04-05 13:14:03 +00001677 const OperandMatcher &OM = InsnMatcher.getOperand(DstChild->getName());
Daniel Sandersffc7d582017-03-29 15:37:18 +00001678 DstMIBuilder.addRenderer<RenderComplexPatternOperand>(
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001679 0, *ComplexPattern->second, DstChild->getName(),
Daniel Sanders2deea182017-04-22 15:11:04 +00001680 OM.getAllocatedTemporariesBaseID());
Daniel Sandersc270c502017-03-30 09:36:33 +00001681 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001682 }
1683
Daniel Sandersd0656a32017-04-13 09:45:37 +00001684 if (ChildRec->isSubClassOf("SDNodeXForm"))
1685 return failedImport("Dst pattern child def is an unsupported tablegen "
1686 "class (SDNodeXForm)");
1687
Daniel Sandersffc7d582017-03-29 15:37:18 +00001688 return failedImport(
1689 "Dst pattern child def is an unsupported tablegen class");
1690 }
1691
1692 return failedImport("Dst pattern child is an unsupported kind");
1693}
1694
Daniel Sandersc270c502017-03-30 09:36:33 +00001695Expected<BuildMIAction &> GlobalISelEmitter::createAndImportInstructionRenderer(
Daniel Sandersffc7d582017-03-29 15:37:18 +00001696 RuleMatcher &M, const TreePatternNode *Dst,
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001697 const InstructionMatcher &InsnMatcher) {
Daniel Sandersffc7d582017-03-29 15:37:18 +00001698 Record *DstOp = Dst->getOperator();
Daniel Sandersd0656a32017-04-13 09:45:37 +00001699 if (!DstOp->isSubClassOf("Instruction")) {
1700 if (DstOp->isSubClassOf("ValueType"))
1701 return failedImport(
1702 "Pattern operator isn't an instruction (it's a ValueType)");
Daniel Sandersffc7d582017-03-29 15:37:18 +00001703 return failedImport("Pattern operator isn't an instruction");
Daniel Sandersd0656a32017-04-13 09:45:37 +00001704 }
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001705 CodeGenInstruction *DstI = &Target.getInstruction(DstOp);
Daniel Sandersffc7d582017-03-29 15:37:18 +00001706
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001707 unsigned DstINumUses = DstI->Operands.size() - DstI->Operands.NumDefs;
1708 unsigned ExpectedDstINumUses = Dst->getNumChildren();
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001709 bool IsExtractSubReg = false;
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001710
1711 // COPY_TO_REGCLASS is just a copy with a ConstrainOperandToRegClassAction
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001712 // attached. Similarly for EXTRACT_SUBREG except that's a subregister copy.
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001713 if (DstI->TheDef->getName() == "COPY_TO_REGCLASS") {
1714 DstI = &Target.getInstruction(RK.getDef("COPY"));
1715 DstINumUses--; // Ignore the class constraint.
1716 ExpectedDstINumUses--;
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001717 } else if (DstI->TheDef->getName() == "EXTRACT_SUBREG") {
1718 DstI = &Target.getInstruction(RK.getDef("COPY"));
1719 IsExtractSubReg = true;
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001720 }
1721
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001722 auto &DstMIBuilder = M.addAction<BuildMIAction>(0, DstI, InsnMatcher);
Daniel Sandersffc7d582017-03-29 15:37:18 +00001723
1724 // Render the explicit defs.
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001725 for (unsigned I = 0; I < DstI->Operands.NumDefs; ++I) {
1726 const CGIOperandList::OperandInfo &DstIOperand = DstI->Operands[I];
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001727 DstMIBuilder.addRenderer<CopyRenderer>(0, InsnMatcher, DstIOperand.Name);
Daniel Sandersffc7d582017-03-29 15:37:18 +00001728 }
1729
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001730 // EXTRACT_SUBREG needs to use a subregister COPY.
1731 if (IsExtractSubReg) {
1732 if (!Dst->getChild(0)->isLeaf())
1733 return failedImport("EXTRACT_SUBREG child #1 is not a leaf");
1734
Daniel Sanders32291982017-06-28 13:50:04 +00001735 if (DefInit *SubRegInit =
1736 dyn_cast<DefInit>(Dst->getChild(1)->getLeafValue())) {
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001737 CodeGenRegisterClass *RC = CGRegs.getRegClass(
1738 getInitValueAsRegClass(Dst->getChild(0)->getLeafValue()));
1739 CodeGenSubRegIndex *SubIdx = CGRegs.getSubRegIdx(SubRegInit->getDef());
1740
1741 const auto &SrcRCDstRCPair =
1742 RC->getMatchingSubClassWithSubRegs(CGRegs, SubIdx);
1743 if (SrcRCDstRCPair.hasValue()) {
1744 assert(SrcRCDstRCPair->second && "Couldn't find a matching subclass");
1745 if (SrcRCDstRCPair->first != RC)
1746 return failedImport("EXTRACT_SUBREG requires an additional COPY");
1747 }
1748
1749 DstMIBuilder.addRenderer<CopySubRegRenderer>(
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001750 0, InsnMatcher, Dst->getChild(0)->getName(), SubIdx);
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001751 return DstMIBuilder;
1752 }
1753
1754 return failedImport("EXTRACT_SUBREG child #1 is not a subreg index");
1755 }
1756
Daniel Sandersffc7d582017-03-29 15:37:18 +00001757 // Render the explicit uses.
Daniel Sanders0ed28822017-04-12 08:23:08 +00001758 unsigned Child = 0;
Diana Picus382602f2017-05-17 08:57:28 +00001759 unsigned NumDefaultOps = 0;
Daniel Sanders0ed28822017-04-12 08:23:08 +00001760 for (unsigned I = 0; I != DstINumUses; ++I) {
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001761 const CGIOperandList::OperandInfo &DstIOperand =
1762 DstI->Operands[DstI->Operands.NumDefs + I];
Daniel Sanders0ed28822017-04-12 08:23:08 +00001763
Diana Picus382602f2017-05-17 08:57:28 +00001764 // If the operand has default values, introduce them now.
1765 // FIXME: Until we have a decent test case that dictates we should do
1766 // otherwise, we're going to assume that operands with default values cannot
1767 // be specified in the patterns. Therefore, adding them will not cause us to
1768 // end up with too many rendered operands.
1769 if (DstIOperand.Rec->isSubClassOf("OperandWithDefaultOps")) {
Daniel Sanders0ed28822017-04-12 08:23:08 +00001770 DagInit *DefaultOps = DstIOperand.Rec->getValueAsDag("DefaultOps");
Diana Picus382602f2017-05-17 08:57:28 +00001771 if (auto Error = importDefaultOperandRenderers(DstMIBuilder, DefaultOps))
1772 return std::move(Error);
1773 ++NumDefaultOps;
Daniel Sanders0ed28822017-04-12 08:23:08 +00001774 continue;
1775 }
1776
1777 if (auto Error = importExplicitUseRenderer(
1778 DstMIBuilder, Dst->getChild(Child), InsnMatcher))
Daniel Sandersffc7d582017-03-29 15:37:18 +00001779 return std::move(Error);
Daniel Sanders0ed28822017-04-12 08:23:08 +00001780 ++Child;
Daniel Sandersffc7d582017-03-29 15:37:18 +00001781 }
1782
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001783 if (NumDefaultOps + ExpectedDstINumUses != DstINumUses)
Diana Picuseb2057c2017-05-17 09:25:08 +00001784 return failedImport("Expected " + llvm::to_string(DstINumUses) +
Diana Picus382602f2017-05-17 08:57:28 +00001785 " used operands but found " +
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001786 llvm::to_string(ExpectedDstINumUses) +
Diana Picuseb2057c2017-05-17 09:25:08 +00001787 " explicit ones and " + llvm::to_string(NumDefaultOps) +
Diana Picus382602f2017-05-17 08:57:28 +00001788 " default ones");
1789
Daniel Sandersffc7d582017-03-29 15:37:18 +00001790 return DstMIBuilder;
1791}
1792
Diana Picus382602f2017-05-17 08:57:28 +00001793Error GlobalISelEmitter::importDefaultOperandRenderers(
1794 BuildMIAction &DstMIBuilder, DagInit *DefaultOps) const {
Craig Topper481ff702017-05-29 21:49:34 +00001795 for (const auto *DefaultOp : DefaultOps->getArgs()) {
Diana Picus382602f2017-05-17 08:57:28 +00001796 // Look through ValueType operators.
1797 if (const DagInit *DefaultDagOp = dyn_cast<DagInit>(DefaultOp)) {
1798 if (const DefInit *DefaultDagOperator =
1799 dyn_cast<DefInit>(DefaultDagOp->getOperator())) {
1800 if (DefaultDagOperator->getDef()->isSubClassOf("ValueType"))
1801 DefaultOp = DefaultDagOp->getArg(0);
1802 }
1803 }
1804
1805 if (const DefInit *DefaultDefOp = dyn_cast<DefInit>(DefaultOp)) {
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001806 DstMIBuilder.addRenderer<AddRegisterRenderer>(0, DefaultDefOp->getDef());
Diana Picus382602f2017-05-17 08:57:28 +00001807 continue;
1808 }
1809
1810 if (const IntInit *DefaultIntOp = dyn_cast<IntInit>(DefaultOp)) {
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001811 DstMIBuilder.addRenderer<ImmRenderer>(0, DefaultIntOp->getValue());
Diana Picus382602f2017-05-17 08:57:28 +00001812 continue;
1813 }
1814
1815 return failedImport("Could not add default op");
1816 }
1817
1818 return Error::success();
1819}
1820
Daniel Sandersc270c502017-03-30 09:36:33 +00001821Error GlobalISelEmitter::importImplicitDefRenderers(
Daniel Sandersffc7d582017-03-29 15:37:18 +00001822 BuildMIAction &DstMIBuilder,
1823 const std::vector<Record *> &ImplicitDefs) const {
1824 if (!ImplicitDefs.empty())
1825 return failedImport("Pattern defines a physical register");
Daniel Sandersc270c502017-03-30 09:36:33 +00001826 return Error::success();
Daniel Sandersffc7d582017-03-29 15:37:18 +00001827}
1828
1829Expected<RuleMatcher> GlobalISelEmitter::runOnPattern(const PatternToMatch &P) {
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001830 // Keep track of the matchers and actions to emit.
Ahmed Bougacha9aa4c102017-02-04 00:47:08 +00001831 RuleMatcher M;
1832 M.addAction<DebugCommentAction>(P);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001833
Daniel Sandersc270c502017-03-30 09:36:33 +00001834 if (auto Error = importRulePredicates(M, P.getPredicates()->getValues()))
Daniel Sandersffc7d582017-03-29 15:37:18 +00001835 return std::move(Error);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001836
1837 // Next, analyze the pattern operators.
1838 TreePatternNode *Src = P.getSrcPattern();
1839 TreePatternNode *Dst = P.getDstPattern();
1840
1841 // If the root of either pattern isn't a simple operator, ignore it.
Daniel Sandersd0656a32017-04-13 09:45:37 +00001842 if (auto Err = isTrivialOperatorNode(Dst))
1843 return failedImport("Dst pattern root isn't a trivial operator (" +
1844 toString(std::move(Err)) + ")");
1845 if (auto Err = isTrivialOperatorNode(Src))
1846 return failedImport("Src pattern root isn't a trivial operator (" +
1847 toString(std::move(Err)) + ")");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001848
Daniel Sanders452c8ae2017-05-23 19:33:16 +00001849 if (Dst->isLeaf())
1850 return failedImport("Dst pattern root isn't a known leaf");
1851
Daniel Sandersbee57392017-04-04 13:25:23 +00001852 // Start with the defined operands (i.e., the results of the root operator).
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001853 Record *DstOp = Dst->getOperator();
1854 if (!DstOp->isSubClassOf("Instruction"))
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001855 return failedImport("Pattern operator isn't an instruction");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001856
1857 auto &DstI = Target.getInstruction(DstOp);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001858 if (DstI.Operands.NumDefs != Src->getExtTypes().size())
Daniel Sandersd0656a32017-04-13 09:45:37 +00001859 return failedImport("Src pattern results and dst MI defs are different (" +
1860 to_string(Src->getExtTypes().size()) + " def(s) vs " +
1861 to_string(DstI.Operands.NumDefs) + " def(s))");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001862
Daniel Sandersffc7d582017-03-29 15:37:18 +00001863 InstructionMatcher &InsnMatcherTemp = M.addInstructionMatcher();
Daniel Sandersc270c502017-03-30 09:36:33 +00001864 auto InsnMatcherOrError = createAndImportSelDAGMatcher(InsnMatcherTemp, Src);
Daniel Sandersffc7d582017-03-29 15:37:18 +00001865 if (auto Error = InsnMatcherOrError.takeError())
1866 return std::move(Error);
1867 InstructionMatcher &InsnMatcher = InsnMatcherOrError.get();
1868
1869 // The root of the match also has constraints on the register bank so that it
1870 // matches the result instruction.
1871 unsigned OpIdx = 0;
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001872 for (const EEVT::TypeSet &Ty : Src->getExtTypes()) {
Daniel Sandersffc7d582017-03-29 15:37:18 +00001873 (void)Ty;
1874
Daniel Sanders066ebbf2017-02-24 15:43:30 +00001875 const auto &DstIOperand = DstI.Operands[OpIdx];
1876 Record *DstIOpRec = DstIOperand.Rec;
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001877 if (DstI.TheDef->getName() == "COPY_TO_REGCLASS") {
1878 DstIOpRec = getInitValueAsRegClass(Dst->getChild(1)->getLeafValue());
1879
1880 if (DstIOpRec == nullptr)
1881 return failedImport(
1882 "COPY_TO_REGCLASS operand #1 isn't a register class");
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001883 } else if (DstI.TheDef->getName() == "EXTRACT_SUBREG") {
1884 if (!Dst->getChild(0)->isLeaf())
1885 return failedImport("EXTRACT_SUBREG operand #0 isn't a leaf");
1886
Daniel Sanders32291982017-06-28 13:50:04 +00001887 // We can assume that a subregister is in the same bank as it's super
1888 // register.
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001889 DstIOpRec = getInitValueAsRegClass(Dst->getChild(0)->getLeafValue());
1890
1891 if (DstIOpRec == nullptr)
1892 return failedImport(
1893 "EXTRACT_SUBREG operand #0 isn't a register class");
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001894 } else if (DstIOpRec->isSubClassOf("RegisterOperand"))
Daniel Sanders658541f2017-04-22 15:53:21 +00001895 DstIOpRec = DstIOpRec->getValueAsDef("RegClass");
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001896 else if (!DstIOpRec->isSubClassOf("RegisterClass"))
Daniel Sanders32291982017-06-28 13:50:04 +00001897 return failedImport("Dst MI def isn't a register class" +
1898 to_string(*Dst));
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001899
Daniel Sandersffc7d582017-03-29 15:37:18 +00001900 OperandMatcher &OM = InsnMatcher.getOperand(OpIdx);
1901 OM.setSymbolicName(DstIOperand.Name);
Daniel Sandersdc662ff2017-01-26 11:10:14 +00001902 OM.addPredicate<RegisterBankOperandMatcher>(
1903 Target.getRegisterClass(DstIOpRec));
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001904 ++OpIdx;
1905 }
1906
Daniel Sandersc270c502017-03-30 09:36:33 +00001907 auto DstMIBuilderOrError =
1908 createAndImportInstructionRenderer(M, Dst, InsnMatcher);
Daniel Sandersffc7d582017-03-29 15:37:18 +00001909 if (auto Error = DstMIBuilderOrError.takeError())
1910 return std::move(Error);
1911 BuildMIAction &DstMIBuilder = DstMIBuilderOrError.get();
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001912
Daniel Sandersffc7d582017-03-29 15:37:18 +00001913 // Render the implicit defs.
1914 // These are only added to the root of the result.
Daniel Sandersc270c502017-03-30 09:36:33 +00001915 if (auto Error = importImplicitDefRenderers(DstMIBuilder, P.getDstRegs()))
Daniel Sandersffc7d582017-03-29 15:37:18 +00001916 return std::move(Error);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001917
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001918 // Constrain the registers to classes. This is normally derived from the
1919 // emitted instruction but a few instructions require special handling.
1920 if (DstI.TheDef->getName() == "COPY_TO_REGCLASS") {
1921 // COPY_TO_REGCLASS does not provide operand constraints itself but the
1922 // result is constrained to the class given by the second child.
1923 Record *DstIOpRec =
1924 getInitValueAsRegClass(Dst->getChild(1)->getLeafValue());
1925
1926 if (DstIOpRec == nullptr)
1927 return failedImport("COPY_TO_REGCLASS operand #1 isn't a register class");
1928
1929 M.addAction<ConstrainOperandToRegClassAction>(
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001930 0, 0, Target.getRegisterClass(DstIOpRec));
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001931
1932 // We're done with this pattern! It's eligible for GISel emission; return
1933 // it.
1934 ++NumPatternImported;
1935 return std::move(M);
1936 }
1937
1938 if (DstI.TheDef->getName() == "EXTRACT_SUBREG") {
1939 // EXTRACT_SUBREG selects into a subregister COPY but unlike most
1940 // instructions, the result register class is controlled by the
1941 // subregisters of the operand. As a result, we must constrain the result
1942 // class rather than check that it's already the right one.
1943 if (!Dst->getChild(0)->isLeaf())
1944 return failedImport("EXTRACT_SUBREG child #1 is not a leaf");
1945
Daniel Sanders320390b2017-06-28 15:16:03 +00001946 DefInit *SubRegInit = dyn_cast<DefInit>(Dst->getChild(1)->getLeafValue());
1947 if (!SubRegInit)
1948 return failedImport("EXTRACT_SUBREG child #1 is not a subreg index");
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001949
Daniel Sanders320390b2017-06-28 15:16:03 +00001950 // Constrain the result to the same register bank as the operand.
1951 Record *DstIOpRec =
1952 getInitValueAsRegClass(Dst->getChild(0)->getLeafValue());
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001953
Daniel Sanders320390b2017-06-28 15:16:03 +00001954 if (DstIOpRec == nullptr)
1955 return failedImport("EXTRACT_SUBREG operand #1 isn't a register class");
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001956
Daniel Sanders320390b2017-06-28 15:16:03 +00001957 CodeGenSubRegIndex *SubIdx = CGRegs.getSubRegIdx(SubRegInit->getDef());
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001958 CodeGenRegisterClass *SrcRC = CGRegs.getRegClass(DstIOpRec);
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001959
Daniel Sanders320390b2017-06-28 15:16:03 +00001960 // It would be nice to leave this constraint implicit but we're required
1961 // to pick a register class so constrain the result to a register class
1962 // that can hold the correct MVT.
1963 //
1964 // FIXME: This may introduce an extra copy if the chosen class doesn't
1965 // actually contain the subregisters.
1966 assert(Src->getExtTypes().size() == 1 &&
1967 "Expected Src of EXTRACT_SUBREG to have one result type");
Daniel Sanderscc36dbf2017-06-27 10:11:39 +00001968
Daniel Sanders320390b2017-06-28 15:16:03 +00001969 const auto &SrcRCDstRCPair =
1970 SrcRC->getMatchingSubClassWithSubRegs(CGRegs, SubIdx);
1971 assert(SrcRCDstRCPair->second && "Couldn't find a matching subclass");
Daniel Sandersd93a35a2017-07-05 09:39:33 +00001972 M.addAction<ConstrainOperandToRegClassAction>(0, 0, *SrcRCDstRCPair->second);
1973 M.addAction<ConstrainOperandToRegClassAction>(0, 1, *SrcRCDstRCPair->first);
1974
1975 // We're done with this pattern! It's eligible for GISel emission; return
1976 // it.
1977 ++NumPatternImported;
1978 return std::move(M);
1979 }
1980
1981 M.addAction<ConstrainOperandsToDefinitionAction>(0);
Daniel Sandersa6e2ceb2017-06-20 12:36:34 +00001982
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001983 // We're done with this pattern! It's eligible for GISel emission; return it.
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00001984 ++NumPatternImported;
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001985 return std::move(M);
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001986}
1987
1988void GlobalISelEmitter::run(raw_ostream &OS) {
1989 // Track the GINodeEquiv definitions.
1990 gatherNodeEquivs();
1991
1992 emitSourceFileHeader(("Global Instruction Selector for the " +
1993 Target.getName() + " target").str(), OS);
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00001994 std::vector<RuleMatcher> Rules;
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001995 // Look through the SelectionDAG patterns we found, possibly emitting some.
1996 for (const PatternToMatch &Pat : CGP.ptms()) {
1997 ++NumPatternTotal;
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00001998 auto MatcherOrErr = runOnPattern(Pat);
1999
2000 // The pattern analysis can fail, indicating an unsupported pattern.
2001 // Report that if we've been asked to do so.
2002 if (auto Err = MatcherOrErr.takeError()) {
Ahmed Bougacha36f70352016-12-21 23:26:20 +00002003 if (WarnOnSkippedPatterns) {
2004 PrintWarning(Pat.getSrcRecord()->getLoc(),
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00002005 "Skipped pattern: " + toString(std::move(Err)));
2006 } else {
2007 consumeError(std::move(Err));
Ahmed Bougacha36f70352016-12-21 23:26:20 +00002008 }
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00002009 ++NumPatternImportsSkipped;
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00002010 continue;
Ahmed Bougacha36f70352016-12-21 23:26:20 +00002011 }
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00002012
Daniel Sandersb41ce2b2017-02-20 14:31:27 +00002013 Rules.push_back(std::move(MatcherOrErr.get()));
2014 }
2015
Daniel Sanders066ebbf2017-02-24 15:43:30 +00002016 std::stable_sort(Rules.begin(), Rules.end(),
2017 [&](const RuleMatcher &A, const RuleMatcher &B) {
Daniel Sanders759ff412017-02-24 13:58:11 +00002018 if (A.isHigherPriorityThan(B)) {
2019 assert(!B.isHigherPriorityThan(A) && "Cannot be more important "
2020 "and less important at "
2021 "the same time");
2022 return true;
2023 }
2024 return false;
2025 });
2026
Daniel Sanders6ab0daa2017-07-04 14:35:06 +00002027 std::vector<Record *> ComplexPredicates =
2028 RK.getAllDerivedDefinitions("GIComplexOperandMatcher");
2029 std::sort(ComplexPredicates.begin(), ComplexPredicates.end(),
2030 [](const Record *A, const Record *B) {
2031 if (A->getName() < B->getName())
2032 return true;
2033 return false;
2034 });
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002035 unsigned MaxTemporaries = 0;
2036 for (const auto &Rule : Rules)
Daniel Sanders2deea182017-04-22 15:11:04 +00002037 MaxTemporaries = std::max(MaxTemporaries, Rule.countRendererFns());
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002038
Daniel Sanderse7b0d662017-04-21 15:59:56 +00002039 OS << "#ifdef GET_GLOBALISEL_PREDICATE_BITSET\n"
2040 << "const unsigned MAX_SUBTARGET_PREDICATES = " << SubtargetFeatures.size()
2041 << ";\n"
2042 << "using PredicateBitset = "
2043 "llvm::PredicateBitsetImpl<MAX_SUBTARGET_PREDICATES>;\n"
2044 << "#endif // ifdef GET_GLOBALISEL_PREDICATE_BITSET\n\n";
2045
Daniel Sanders6ab0daa2017-07-04 14:35:06 +00002046 OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n"
2047 << " mutable MatcherState State;\n"
2048 << " typedef "
2049 "ComplexRendererFn("
2050 << Target.getName()
2051 << "InstructionSelector::*ComplexMatcherMemFn)(MachineOperand &) const;\n"
2052 << "const MatcherInfoTy<PredicateBitset, ComplexMatcherMemFn> "
2053 "MatcherInfo;\n"
2054 << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n\n";
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002055
Daniel Sanders6ab0daa2017-07-04 14:35:06 +00002056 OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n"
2057 << ", State(" << MaxTemporaries << "),\n"
2058 << "MatcherInfo({TypeObjects, FeatureBitsets, {\n"
2059 << " nullptr, // GICP_Invalid\n";
2060 for (const auto &Record : ComplexPredicates)
2061 OS << " &" << Target.getName()
2062 << "InstructionSelector::" << Record->getValueAsString("MatcherFn")
2063 << ", // " << Record->getName() << "\n";
2064 OS << "}})\n"
2065 << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n\n";
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002066
Daniel Sanderse7b0d662017-04-21 15:59:56 +00002067 OS << "#ifdef GET_GLOBALISEL_IMPL\n";
2068 SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(SubtargetFeatures,
2069 OS);
Daniel Sanderse9fdba32017-04-29 17:30:09 +00002070
2071 // Separate subtarget features by how often they must be recomputed.
2072 SubtargetFeatureInfoMap ModuleFeatures;
2073 std::copy_if(SubtargetFeatures.begin(), SubtargetFeatures.end(),
2074 std::inserter(ModuleFeatures, ModuleFeatures.end()),
2075 [](const SubtargetFeatureInfoMap::value_type &X) {
2076 return !X.second.mustRecomputePerFunction();
2077 });
2078 SubtargetFeatureInfoMap FunctionFeatures;
2079 std::copy_if(SubtargetFeatures.begin(), SubtargetFeatures.end(),
2080 std::inserter(FunctionFeatures, FunctionFeatures.end()),
2081 [](const SubtargetFeatureInfoMap::value_type &X) {
2082 return X.second.mustRecomputePerFunction();
2083 });
2084
Daniel Sanderse7b0d662017-04-21 15:59:56 +00002085 SubtargetFeatureInfo::emitComputeAvailableFeatures(
Daniel Sanderse9fdba32017-04-29 17:30:09 +00002086 Target.getName(), "InstructionSelector", "computeAvailableModuleFeatures",
2087 ModuleFeatures, OS);
2088 SubtargetFeatureInfo::emitComputeAvailableFeatures(
2089 Target.getName(), "InstructionSelector",
2090 "computeAvailableFunctionFeatures", FunctionFeatures, OS,
2091 "const MachineFunction *MF");
Daniel Sanderse7b0d662017-04-21 15:59:56 +00002092
Daniel Sanders6ab0daa2017-07-04 14:35:06 +00002093 // Emit a table containing the LLT objects needed by the matcher and an enum
2094 // for the matcher to reference them with.
2095 std::vector<LLTCodeGen> TypeObjects = {
2096 LLT::scalar(8), LLT::scalar(16), LLT::scalar(32),
2097 LLT::scalar(64), LLT::scalar(80), LLT::vector(8, 1),
2098 LLT::vector(16, 1), LLT::vector(32, 1), LLT::vector(64, 1),
2099 LLT::vector(8, 8), LLT::vector(16, 8), LLT::vector(32, 8),
2100 LLT::vector(64, 8), LLT::vector(4, 16), LLT::vector(8, 16),
2101 LLT::vector(16, 16), LLT::vector(32, 16), LLT::vector(2, 32),
2102 LLT::vector(4, 32), LLT::vector(8, 32), LLT::vector(16, 32),
2103 LLT::vector(2, 64), LLT::vector(4, 64), LLT::vector(8, 64),
2104 };
2105 std::sort(TypeObjects.begin(), TypeObjects.end());
2106 OS << "enum {\n";
2107 for (const auto &TypeObject : TypeObjects) {
2108 OS << " ";
2109 TypeObject.emitCxxEnumValue(OS);
2110 OS << ",\n";
2111 }
2112 OS << "};\n"
2113 << "const static LLT TypeObjects[] = {\n";
2114 for (const auto &TypeObject : TypeObjects) {
2115 OS << " ";
2116 TypeObject.emitCxxConstructorCall(OS);
2117 OS << ",\n";
2118 }
2119 OS << "};\n\n";
2120
2121 // Emit a table containing the PredicateBitsets objects needed by the matcher
2122 // and an enum for the matcher to reference them with.
2123 std::vector<std::vector<Record *>> FeatureBitsets;
2124 for (auto &Rule : Rules)
2125 FeatureBitsets.push_back(Rule.getRequiredFeatures());
2126 std::sort(
2127 FeatureBitsets.begin(), FeatureBitsets.end(),
2128 [&](const std::vector<Record *> &A, const std::vector<Record *> &B) {
2129 if (A.size() < B.size())
2130 return true;
2131 if (A.size() > B.size())
2132 return false;
2133 for (const auto &Pair : zip(A, B)) {
2134 if (std::get<0>(Pair)->getName() < std::get<1>(Pair)->getName())
2135 return true;
2136 if (std::get<0>(Pair)->getName() > std::get<1>(Pair)->getName())
2137 return false;
2138 }
2139 return false;
2140 });
2141 FeatureBitsets.erase(
2142 std::unique(FeatureBitsets.begin(), FeatureBitsets.end()),
2143 FeatureBitsets.end());
2144 OS << "enum {\n"
2145 << " GIFBS_Invalid,\n";
2146 for (const auto &FeatureBitset : FeatureBitsets) {
2147 if (FeatureBitset.empty())
2148 continue;
2149 OS << " " << getNameForFeatureBitset(FeatureBitset) << ",\n";
2150 }
2151 OS << "};\n"
2152 << "const static PredicateBitset FeatureBitsets[] {\n"
2153 << " {}, // GIFBS_Invalid\n";
2154 for (const auto &FeatureBitset : FeatureBitsets) {
2155 if (FeatureBitset.empty())
2156 continue;
2157 OS << " {";
2158 for (const auto &Feature : FeatureBitset) {
2159 const auto &I = SubtargetFeatures.find(Feature);
2160 assert(I != SubtargetFeatures.end() && "Didn't import predicate?");
2161 OS << I->second.getEnumBitName() << ", ";
2162 }
2163 OS << "},\n";
2164 }
2165 OS << "};\n\n";
2166
2167 // Emit complex predicate table and an enum to reference them with.
2168 OS << "enum {\n"
2169 << " GICP_Invalid,\n";
2170 for (const auto &Record : ComplexPredicates)
2171 OS << " GICP_" << Record->getName() << ",\n";
2172 OS << "};\n"
2173 << "// See constructor for table contents\n\n";
2174
Daniel Sanderse7b0d662017-04-21 15:59:56 +00002175 OS << "bool " << Target.getName()
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002176 << "InstructionSelector::selectImpl(MachineInstr &I) const {\n"
2177 << " MachineFunction &MF = *I.getParent()->getParent();\n"
Daniel Sanders6ab0daa2017-07-04 14:35:06 +00002178 << " MachineRegisterInfo &MRI = MF.getRegInfo();\n"
Daniel Sanders32291982017-06-28 13:50:04 +00002179 << " // FIXME: This should be computed on a per-function basis rather "
2180 "than per-insn.\n"
2181 << " AvailableFunctionFeatures = computeAvailableFunctionFeatures(&STI, "
2182 "&MF);\n"
Daniel Sandersa6cfce62017-07-05 14:50:18 +00002183 << " const PredicateBitset AvailableFeatures = getAvailableFeatures();\n"
2184 << " NewMIVector OutMIs;\n"
2185 << " State.MIs.clear();\n"
2186 << " State.MIs.push_back(&I);\n\n";
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002187
Daniel Sandersb96f40d2017-03-20 15:20:42 +00002188 for (auto &Rule : Rules) {
Daniel Sanders6ab0daa2017-07-04 14:35:06 +00002189 Rule.emit(OS);
Daniel Sandersc60abe32017-07-04 15:31:50 +00002190 ++CurrentMatchTableID;
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00002191 ++NumPatternEmitted;
Daniel Sandersc60abe32017-07-04 15:31:50 +00002192 assert(CurrentMatchTableID == NumPatternEmitted &&
2193 "Statistic deviates from number of emitted tables");
Ahmed Bougacha36f70352016-12-21 23:26:20 +00002194 }
2195
Daniel Sanders8a4bae92017-03-14 21:32:08 +00002196 OS << " return false;\n"
2197 << "}\n"
2198 << "#endif // ifdef GET_GLOBALISEL_IMPL\n";
Daniel Sanderse9fdba32017-04-29 17:30:09 +00002199
2200 OS << "#ifdef GET_GLOBALISEL_PREDICATES_DECL\n"
2201 << "PredicateBitset AvailableModuleFeatures;\n"
2202 << "mutable PredicateBitset AvailableFunctionFeatures;\n"
2203 << "PredicateBitset getAvailableFeatures() const {\n"
2204 << " return AvailableModuleFeatures | AvailableFunctionFeatures;\n"
2205 << "}\n"
2206 << "PredicateBitset\n"
2207 << "computeAvailableModuleFeatures(const " << Target.getName()
2208 << "Subtarget *Subtarget) const;\n"
2209 << "PredicateBitset\n"
2210 << "computeAvailableFunctionFeatures(const " << Target.getName()
2211 << "Subtarget *Subtarget,\n"
2212 << " const MachineFunction *MF) const;\n"
2213 << "#endif // ifdef GET_GLOBALISEL_PREDICATES_DECL\n";
2214
2215 OS << "#ifdef GET_GLOBALISEL_PREDICATES_INIT\n"
2216 << "AvailableModuleFeatures(computeAvailableModuleFeatures(&STI)),\n"
2217 << "AvailableFunctionFeatures()\n"
2218 << "#endif // ifdef GET_GLOBALISEL_PREDICATES_INIT\n";
Ahmed Bougacha36f70352016-12-21 23:26:20 +00002219}
2220
Daniel Sanderse7b0d662017-04-21 15:59:56 +00002221void GlobalISelEmitter::declareSubtargetFeature(Record *Predicate) {
2222 if (SubtargetFeatures.count(Predicate) == 0)
2223 SubtargetFeatures.emplace(
2224 Predicate, SubtargetFeatureInfo(Predicate, SubtargetFeatures.size()));
2225}
2226
Ahmed Bougacha982c5eb2017-02-10 04:00:17 +00002227} // end anonymous namespace
2228
Ahmed Bougacha36f70352016-12-21 23:26:20 +00002229//===----------------------------------------------------------------------===//
2230
2231namespace llvm {
2232void EmitGlobalISel(RecordKeeper &RK, raw_ostream &OS) {
2233 GlobalISelEmitter(RK).run(OS);
2234}
2235} // End llvm namespace