1 //===- GlobalISelEmitter.cpp - Generate an instruction selector -----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// This tablegen backend emits code for use by the GlobalISel instruction
11 /// selector. See include/llvm/CodeGen/TargetGlobalISel.td.
13 /// This file analyzes the patterns recognized by the SelectionDAGISel tablegen
14 /// backend, filters out the ones that are unsupported, maps
15 /// SelectionDAG-specific constructs to their GlobalISel counterpart
16 /// (when applicable: MVT to LLT; SDNode to generic Instruction).
18 /// Not all patterns are supported: pass the tablegen invocation
19 /// "-warn-on-skipped-patterns" to emit a warning when a pattern is skipped,
22 /// The generated file defines a single method:
23 /// bool <Target>InstructionSelector::selectImpl(MachineInstr &I) const;
24 /// intended to be used in InstructionSelector::select as the first-step
25 /// selector for the patterns that don't require complex C++.
27 /// FIXME: We'll probably want to eventually define a base
28 /// "TargetGenInstructionSelector" class.
30 //===----------------------------------------------------------------------===//
32 #include "CodeGenDAGPatterns.h"
33 #include "SubtargetFeatureInfo.h"
34 #include "llvm/ADT/Optional.h"
35 #include "llvm/ADT/SmallSet.h"
36 #include "llvm/ADT/Statistic.h"
37 #include "llvm/Support/CodeGenCoverage.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Support/Error.h"
40 #include "llvm/Support/LowLevelTypeImpl.h"
41 #include "llvm/Support/MachineValueType.h"
42 #include "llvm/Support/ScopedPrinter.h"
43 #include "llvm/TableGen/Error.h"
44 #include "llvm/TableGen/Record.h"
45 #include "llvm/TableGen/TableGenBackend.h"
50 #define DEBUG_TYPE "gisel-emitter"
52 STATISTIC(NumPatternTotal
, "Total number of patterns");
53 STATISTIC(NumPatternImported
, "Number of patterns imported from SelectionDAG");
54 STATISTIC(NumPatternImportsSkipped
, "Number of SelectionDAG imports skipped");
55 STATISTIC(NumPatternsTested
, "Number of patterns executed according to coverage information");
56 STATISTIC(NumPatternEmitted
, "Number of patterns emitted");
58 cl::OptionCategory
GlobalISelEmitterCat("Options for -gen-global-isel");
60 static cl::opt
<bool> WarnOnSkippedPatterns(
61 "warn-on-skipped-patterns",
62 cl::desc("Explain why a pattern was skipped for inclusion "
63 "in the GlobalISel selector"),
64 cl::init(false), cl::cat(GlobalISelEmitterCat
));
66 static cl::opt
<bool> GenerateCoverage(
67 "instrument-gisel-coverage",
68 cl::desc("Generate coverage instrumentation for GlobalISel"),
69 cl::init(false), cl::cat(GlobalISelEmitterCat
));
71 static cl::opt
<std::string
> UseCoverageFile(
72 "gisel-coverage-file", cl::init(""),
73 cl::desc("Specify file to retrieve coverage information from"),
74 cl::cat(GlobalISelEmitterCat
));
76 static cl::opt
<bool> OptimizeMatchTable(
77 "optimize-match-table",
78 cl::desc("Generate an optimized version of the match table"),
79 cl::init(true), cl::cat(GlobalISelEmitterCat
));
82 //===- Helper functions ---------------------------------------------------===//
84 /// Get the name of the enum value used to number the predicate function.
85 std::string
getEnumNameForPredicate(const TreePredicateFn
&Predicate
) {
86 if (Predicate
.hasGISelPredicateCode())
87 return "GIPFP_MI_" + Predicate
.getFnName();
88 return "GIPFP_" + Predicate
.getImmTypeIdentifier().str() + "_" +
89 Predicate
.getFnName();
92 /// Get the opcode used to check this predicate.
93 std::string
getMatchOpcodeForPredicate(const TreePredicateFn
&Predicate
) {
94 return "GIM_Check" + Predicate
.getImmTypeIdentifier().str() + "ImmPredicate";
97 /// This class stands in for LLT wherever we want to tablegen-erate an
98 /// equivalent at compiler run-time.
104 LLTCodeGen() = default;
105 LLTCodeGen(const LLT
&Ty
) : Ty(Ty
) {}
107 std::string
getCxxEnumValue() const {
109 raw_string_ostream
OS(Str
);
111 emitCxxEnumValue(OS
);
115 void emitCxxEnumValue(raw_ostream
&OS
) const {
117 OS
<< "GILLT_s" << Ty
.getSizeInBits();
121 OS
<< "GILLT_v" << Ty
.getNumElements() << "s" << Ty
.getScalarSizeInBits();
124 if (Ty
.isPointer()) {
125 OS
<< "GILLT_p" << Ty
.getAddressSpace();
126 if (Ty
.getSizeInBits() > 0)
127 OS
<< "s" << Ty
.getSizeInBits();
130 llvm_unreachable("Unhandled LLT");
133 void emitCxxConstructorCall(raw_ostream
&OS
) const {
135 OS
<< "LLT::scalar(" << Ty
.getSizeInBits() << ")";
139 OS
<< "LLT::vector(" << Ty
.getNumElements() << ", "
140 << Ty
.getScalarSizeInBits() << ")";
143 if (Ty
.isPointer() && Ty
.getSizeInBits() > 0) {
144 OS
<< "LLT::pointer(" << Ty
.getAddressSpace() << ", "
145 << Ty
.getSizeInBits() << ")";
148 llvm_unreachable("Unhandled LLT");
151 const LLT
&get() const { return Ty
; }
153 /// This ordering is used for std::unique() and llvm::sort(). There's no
154 /// particular logic behind the order but either A < B or B < A must be
156 bool operator<(const LLTCodeGen
&Other
) const {
157 if (Ty
.isValid() != Other
.Ty
.isValid())
158 return Ty
.isValid() < Other
.Ty
.isValid();
162 if (Ty
.isVector() != Other
.Ty
.isVector())
163 return Ty
.isVector() < Other
.Ty
.isVector();
164 if (Ty
.isScalar() != Other
.Ty
.isScalar())
165 return Ty
.isScalar() < Other
.Ty
.isScalar();
166 if (Ty
.isPointer() != Other
.Ty
.isPointer())
167 return Ty
.isPointer() < Other
.Ty
.isPointer();
169 if (Ty
.isPointer() && Ty
.getAddressSpace() != Other
.Ty
.getAddressSpace())
170 return Ty
.getAddressSpace() < Other
.Ty
.getAddressSpace();
172 if (Ty
.isVector() && Ty
.getNumElements() != Other
.Ty
.getNumElements())
173 return Ty
.getNumElements() < Other
.Ty
.getNumElements();
175 return Ty
.getSizeInBits() < Other
.Ty
.getSizeInBits();
178 bool operator==(const LLTCodeGen
&B
) const { return Ty
== B
.Ty
; }
181 // Track all types that are used so we can emit the corresponding enum.
182 std::set
<LLTCodeGen
> KnownTypes
;
184 class InstructionMatcher
;
185 /// Convert an MVT to an equivalent LLT if possible, or the invalid LLT() for
186 /// MVTs that don't map cleanly to an LLT (e.g., iPTR, *any, ...).
187 static Optional
<LLTCodeGen
> MVTToLLT(MVT::SimpleValueType SVT
) {
190 if (VT
.isVector() && VT
.getVectorNumElements() != 1)
192 LLT::vector(VT
.getVectorNumElements(), VT
.getScalarSizeInBits()));
194 if (VT
.isInteger() || VT
.isFloatingPoint())
195 return LLTCodeGen(LLT::scalar(VT
.getSizeInBits()));
199 static std::string
explainPredicates(const TreePatternNode
*N
) {
200 std::string Explanation
= "";
201 StringRef Separator
= "";
202 for (const TreePredicateCall
&Call
: N
->getPredicateCalls()) {
203 const TreePredicateFn
&P
= Call
.Fn
;
205 (Separator
+ P
.getOrigPatFragRecord()->getRecord()->getName()).str();
208 if (P
.isAlwaysTrue())
209 Explanation
+= " always-true";
210 if (P
.isImmediatePattern())
211 Explanation
+= " immediate";
214 Explanation
+= " unindexed";
216 if (P
.isNonExtLoad())
217 Explanation
+= " non-extload";
218 if (P
.isAnyExtLoad())
219 Explanation
+= " extload";
220 if (P
.isSignExtLoad())
221 Explanation
+= " sextload";
222 if (P
.isZeroExtLoad())
223 Explanation
+= " zextload";
225 if (P
.isNonTruncStore())
226 Explanation
+= " non-truncstore";
227 if (P
.isTruncStore())
228 Explanation
+= " truncstore";
230 if (Record
*VT
= P
.getMemoryVT())
231 Explanation
+= (" MemVT=" + VT
->getName()).str();
232 if (Record
*VT
= P
.getScalarMemoryVT())
233 Explanation
+= (" ScalarVT(MemVT)=" + VT
->getName()).str();
235 if (ListInit
*AddrSpaces
= P
.getAddressSpaces()) {
236 raw_string_ostream
OS(Explanation
);
237 OS
<< " AddressSpaces=[";
239 StringRef AddrSpaceSeparator
;
240 for (Init
*Val
: AddrSpaces
->getValues()) {
241 IntInit
*IntVal
= dyn_cast
<IntInit
>(Val
);
245 OS
<< AddrSpaceSeparator
<< IntVal
->getValue();
246 AddrSpaceSeparator
= ", ";
252 int64_t MinAlign
= P
.getMinAlignment();
254 Explanation
+= " MinAlign=" + utostr(MinAlign
);
256 if (P
.isAtomicOrderingMonotonic())
257 Explanation
+= " monotonic";
258 if (P
.isAtomicOrderingAcquire())
259 Explanation
+= " acquire";
260 if (P
.isAtomicOrderingRelease())
261 Explanation
+= " release";
262 if (P
.isAtomicOrderingAcquireRelease())
263 Explanation
+= " acq_rel";
264 if (P
.isAtomicOrderingSequentiallyConsistent())
265 Explanation
+= " seq_cst";
266 if (P
.isAtomicOrderingAcquireOrStronger())
267 Explanation
+= " >=acquire";
268 if (P
.isAtomicOrderingWeakerThanAcquire())
269 Explanation
+= " <acquire";
270 if (P
.isAtomicOrderingReleaseOrStronger())
271 Explanation
+= " >=release";
272 if (P
.isAtomicOrderingWeakerThanRelease())
273 Explanation
+= " <release";
278 std::string
explainOperator(Record
*Operator
) {
279 if (Operator
->isSubClassOf("SDNode"))
280 return (" (" + Operator
->getValueAsString("Opcode") + ")").str();
282 if (Operator
->isSubClassOf("Intrinsic"))
283 return (" (Operator is an Intrinsic, " + Operator
->getName() + ")").str();
285 if (Operator
->isSubClassOf("ComplexPattern"))
286 return (" (Operator is an unmapped ComplexPattern, " + Operator
->getName() +
290 if (Operator
->isSubClassOf("SDNodeXForm"))
291 return (" (Operator is an unmapped SDNodeXForm, " + Operator
->getName() +
295 return (" (Operator " + Operator
->getName() + " not understood)").str();
298 /// Helper function to let the emitter report skip reason error messages.
299 static Error
failedImport(const Twine
&Reason
) {
300 return make_error
<StringError
>(Reason
, inconvertibleErrorCode());
303 static Error
isTrivialOperatorNode(const TreePatternNode
*N
) {
304 std::string Explanation
= "";
305 std::string Separator
= "";
307 bool HasUnsupportedPredicate
= false;
308 for (const TreePredicateCall
&Call
: N
->getPredicateCalls()) {
309 const TreePredicateFn
&Predicate
= Call
.Fn
;
311 if (Predicate
.isAlwaysTrue())
314 if (Predicate
.isImmediatePattern())
317 if (Predicate
.isNonExtLoad() || Predicate
.isAnyExtLoad() ||
318 Predicate
.isSignExtLoad() || Predicate
.isZeroExtLoad())
321 if (Predicate
.isNonTruncStore() || Predicate
.isTruncStore())
324 if (Predicate
.isLoad() && Predicate
.getMemoryVT())
327 if (Predicate
.isLoad() || Predicate
.isStore()) {
328 if (Predicate
.isUnindexed())
332 if (Predicate
.isLoad() || Predicate
.isStore() || Predicate
.isAtomic()) {
333 const ListInit
*AddrSpaces
= Predicate
.getAddressSpaces();
334 if (AddrSpaces
&& !AddrSpaces
->empty())
337 if (Predicate
.getMinAlignment() > 0)
341 if (Predicate
.isAtomic() && Predicate
.getMemoryVT())
344 if (Predicate
.isAtomic() &&
345 (Predicate
.isAtomicOrderingMonotonic() ||
346 Predicate
.isAtomicOrderingAcquire() ||
347 Predicate
.isAtomicOrderingRelease() ||
348 Predicate
.isAtomicOrderingAcquireRelease() ||
349 Predicate
.isAtomicOrderingSequentiallyConsistent() ||
350 Predicate
.isAtomicOrderingAcquireOrStronger() ||
351 Predicate
.isAtomicOrderingWeakerThanAcquire() ||
352 Predicate
.isAtomicOrderingReleaseOrStronger() ||
353 Predicate
.isAtomicOrderingWeakerThanRelease()))
356 if (Predicate
.hasGISelPredicateCode())
359 HasUnsupportedPredicate
= true;
360 Explanation
= Separator
+ "Has a predicate (" + explainPredicates(N
) + ")";
362 Explanation
+= (Separator
+ "first-failing:" +
363 Predicate
.getOrigPatFragRecord()->getRecord()->getName())
368 if (!HasUnsupportedPredicate
)
369 return Error::success();
371 return failedImport(Explanation
);
374 static Record
*getInitValueAsRegClass(Init
*V
) {
375 if (DefInit
*VDefInit
= dyn_cast
<DefInit
>(V
)) {
376 if (VDefInit
->getDef()->isSubClassOf("RegisterOperand"))
377 return VDefInit
->getDef()->getValueAsDef("RegClass");
378 if (VDefInit
->getDef()->isSubClassOf("RegisterClass"))
379 return VDefInit
->getDef();
385 getNameForFeatureBitset(const std::vector
<Record
*> &FeatureBitset
) {
386 std::string Name
= "GIFBS";
387 for (const auto &Feature
: FeatureBitset
)
388 Name
+= ("_" + Feature
->getName()).str();
392 //===- MatchTable Helpers -------------------------------------------------===//
396 /// A record to be stored in a MatchTable.
398 /// This class represents any and all output that may be required to emit the
399 /// MatchTable. Instances are most often configured to represent an opcode or
400 /// value that will be emitted to the table with some formatting but it can also
401 /// represent commas, comments, and other formatting instructions.
402 struct MatchTableRecord
{
403 enum RecordFlagsBits
{
405 /// Causes EmitStr to be formatted as comment when emitted.
407 /// Causes the record value to be followed by a comma when emitted.
408 MTRF_CommaFollows
= 0x2,
409 /// Causes the record value to be followed by a line break when emitted.
410 MTRF_LineBreakFollows
= 0x4,
411 /// Indicates that the record defines a label and causes an additional
412 /// comment to be emitted containing the index of the label.
414 /// Causes the record to be emitted as the index of the label specified by
415 /// LabelID along with a comment indicating where that label is.
416 MTRF_JumpTarget
= 0x10,
417 /// Causes the formatter to add a level of indentation before emitting the
420 /// Causes the formatter to remove a level of indentation after emitting the
425 /// When MTRF_Label or MTRF_JumpTarget is used, indicates a label id to
426 /// reference or define.
428 /// The string to emit. Depending on the MTRF_* flags it may be a comment, a
429 /// value, a label name.
433 /// The number of MatchTable elements described by this record. Comments are 0
434 /// while values are typically 1. Values >1 may occur when we need to emit
435 /// values that exceed the size of a MatchTable element.
436 unsigned NumElements
;
439 /// A bitfield of RecordFlagsBits flags.
442 /// The actual run-time value, if known
445 MatchTableRecord(Optional
<unsigned> LabelID_
, StringRef EmitStr
,
446 unsigned NumElements
, unsigned Flags
,
447 int64_t RawValue
= std::numeric_limits
<int64_t>::min())
448 : LabelID(LabelID_
.hasValue() ? LabelID_
.getValue() : ~0u),
449 EmitStr(EmitStr
), NumElements(NumElements
), Flags(Flags
),
452 assert((!LabelID_
.hasValue() || LabelID
!= ~0u) &&
453 "This value is reserved for non-labels");
455 MatchTableRecord(const MatchTableRecord
&Other
) = default;
456 MatchTableRecord(MatchTableRecord
&&Other
) = default;
458 /// Useful if a Match Table Record gets optimized out
459 void turnIntoComment() {
460 Flags
|= MTRF_Comment
;
461 Flags
&= ~MTRF_CommaFollows
;
465 /// For Jump Table generation purposes
466 bool operator<(const MatchTableRecord
&Other
) const {
467 return RawValue
< Other
.RawValue
;
469 int64_t getRawValue() const { return RawValue
; }
471 void emit(raw_ostream
&OS
, bool LineBreakNextAfterThis
,
472 const MatchTable
&Table
) const;
473 unsigned size() const { return NumElements
; }
478 /// Holds the contents of a generated MatchTable to enable formatting and the
479 /// necessary index tracking needed to support GIM_Try.
481 /// An unique identifier for the table. The generated table will be named
484 /// The records that make up the table. Also includes comments describing the
485 /// values being emitted and line breaks to format it.
486 std::vector
<MatchTableRecord
> Contents
;
487 /// The currently defined labels.
488 DenseMap
<unsigned, unsigned> LabelMap
;
489 /// Tracks the sum of MatchTableRecord::NumElements as the table is built.
490 unsigned CurrentSize
= 0;
491 /// A unique identifier for a MatchTable label.
492 unsigned CurrentLabelID
= 0;
493 /// Determines if the table should be instrumented for rule coverage tracking.
497 static MatchTableRecord LineBreak
;
498 static MatchTableRecord
Comment(StringRef Comment
) {
499 return MatchTableRecord(None
, Comment
, 0, MatchTableRecord::MTRF_Comment
);
501 static MatchTableRecord
Opcode(StringRef Opcode
, int IndentAdjust
= 0) {
502 unsigned ExtraFlags
= 0;
503 if (IndentAdjust
> 0)
504 ExtraFlags
|= MatchTableRecord::MTRF_Indent
;
505 if (IndentAdjust
< 0)
506 ExtraFlags
|= MatchTableRecord::MTRF_Outdent
;
508 return MatchTableRecord(None
, Opcode
, 1,
509 MatchTableRecord::MTRF_CommaFollows
| ExtraFlags
);
511 static MatchTableRecord
NamedValue(StringRef NamedValue
) {
512 return MatchTableRecord(None
, NamedValue
, 1,
513 MatchTableRecord::MTRF_CommaFollows
);
515 static MatchTableRecord
NamedValue(StringRef NamedValue
, int64_t RawValue
) {
516 return MatchTableRecord(None
, NamedValue
, 1,
517 MatchTableRecord::MTRF_CommaFollows
, RawValue
);
519 static MatchTableRecord
NamedValue(StringRef Namespace
,
520 StringRef NamedValue
) {
521 return MatchTableRecord(None
, (Namespace
+ "::" + NamedValue
).str(), 1,
522 MatchTableRecord::MTRF_CommaFollows
);
524 static MatchTableRecord
NamedValue(StringRef Namespace
, StringRef NamedValue
,
526 return MatchTableRecord(None
, (Namespace
+ "::" + NamedValue
).str(), 1,
527 MatchTableRecord::MTRF_CommaFollows
, RawValue
);
529 static MatchTableRecord
IntValue(int64_t IntValue
) {
530 return MatchTableRecord(None
, llvm::to_string(IntValue
), 1,
531 MatchTableRecord::MTRF_CommaFollows
);
533 static MatchTableRecord
Label(unsigned LabelID
) {
534 return MatchTableRecord(LabelID
, "Label " + llvm::to_string(LabelID
), 0,
535 MatchTableRecord::MTRF_Label
|
536 MatchTableRecord::MTRF_Comment
|
537 MatchTableRecord::MTRF_LineBreakFollows
);
539 static MatchTableRecord
JumpTarget(unsigned LabelID
) {
540 return MatchTableRecord(LabelID
, "Label " + llvm::to_string(LabelID
), 1,
541 MatchTableRecord::MTRF_JumpTarget
|
542 MatchTableRecord::MTRF_Comment
|
543 MatchTableRecord::MTRF_CommaFollows
);
546 static MatchTable
buildTable(ArrayRef
<Matcher
*> Rules
, bool WithCoverage
);
548 MatchTable(bool WithCoverage
, unsigned ID
= 0)
549 : ID(ID
), IsWithCoverage(WithCoverage
) {}
551 bool isWithCoverage() const { return IsWithCoverage
; }
553 void push_back(const MatchTableRecord
&Value
) {
554 if (Value
.Flags
& MatchTableRecord::MTRF_Label
)
555 defineLabel(Value
.LabelID
);
556 Contents
.push_back(Value
);
557 CurrentSize
+= Value
.size();
560 unsigned allocateLabelID() { return CurrentLabelID
++; }
562 void defineLabel(unsigned LabelID
) {
563 LabelMap
.insert(std::make_pair(LabelID
, CurrentSize
));
566 unsigned getLabelIndex(unsigned LabelID
) const {
567 const auto I
= LabelMap
.find(LabelID
);
568 assert(I
!= LabelMap
.end() && "Use of undeclared label");
572 void emitUse(raw_ostream
&OS
) const { OS
<< "MatchTable" << ID
; }
574 void emitDeclaration(raw_ostream
&OS
) const {
575 unsigned Indentation
= 4;
576 OS
<< " constexpr static int64_t MatchTable" << ID
<< "[] = {";
577 LineBreak
.emit(OS
, true, *this);
578 OS
<< std::string(Indentation
, ' ');
580 for (auto I
= Contents
.begin(), E
= Contents
.end(); I
!= E
;
582 bool LineBreakIsNext
= false;
583 const auto &NextI
= std::next(I
);
586 if (NextI
->EmitStr
== "" &&
587 NextI
->Flags
== MatchTableRecord::MTRF_LineBreakFollows
)
588 LineBreakIsNext
= true;
591 if (I
->Flags
& MatchTableRecord::MTRF_Indent
)
594 I
->emit(OS
, LineBreakIsNext
, *this);
595 if (I
->Flags
& MatchTableRecord::MTRF_LineBreakFollows
)
596 OS
<< std::string(Indentation
, ' ');
598 if (I
->Flags
& MatchTableRecord::MTRF_Outdent
)
605 MatchTableRecord
MatchTable::LineBreak
= {
606 None
, "" /* Emit String */, 0 /* Elements */,
607 MatchTableRecord::MTRF_LineBreakFollows
};
609 void MatchTableRecord::emit(raw_ostream
&OS
, bool LineBreakIsNextAfterThis
,
610 const MatchTable
&Table
) const {
611 bool UseLineComment
=
612 LineBreakIsNextAfterThis
| (Flags
& MTRF_LineBreakFollows
);
613 if (Flags
& (MTRF_JumpTarget
| MTRF_CommaFollows
))
614 UseLineComment
= false;
616 if (Flags
& MTRF_Comment
)
617 OS
<< (UseLineComment
? "// " : "/*");
620 if (Flags
& MTRF_Label
)
621 OS
<< ": @" << Table
.getLabelIndex(LabelID
);
623 if (Flags
& MTRF_Comment
&& !UseLineComment
)
626 if (Flags
& MTRF_JumpTarget
) {
627 if (Flags
& MTRF_Comment
)
629 OS
<< Table
.getLabelIndex(LabelID
);
632 if (Flags
& MTRF_CommaFollows
) {
634 if (!LineBreakIsNextAfterThis
&& !(Flags
& MTRF_LineBreakFollows
))
638 if (Flags
& MTRF_LineBreakFollows
)
642 MatchTable
&operator<<(MatchTable
&Table
, const MatchTableRecord
&Value
) {
643 Table
.push_back(Value
);
647 //===- Matchers -----------------------------------------------------------===//
649 class OperandMatcher
;
651 class PredicateMatcher
;
656 virtual ~Matcher() = default;
657 virtual void optimize() {}
658 virtual void emit(MatchTable
&Table
) = 0;
660 virtual bool hasFirstCondition() const = 0;
661 virtual const PredicateMatcher
&getFirstCondition() const = 0;
662 virtual std::unique_ptr
<PredicateMatcher
> popFirstCondition() = 0;
665 MatchTable
MatchTable::buildTable(ArrayRef
<Matcher
*> Rules
,
667 MatchTable
Table(WithCoverage
);
668 for (Matcher
*Rule
: Rules
)
671 return Table
<< MatchTable::Opcode("GIM_Reject") << MatchTable::LineBreak
;
674 class GroupMatcher final
: public Matcher
{
675 /// Conditions that form a common prefix of all the matchers contained.
676 SmallVector
<std::unique_ptr
<PredicateMatcher
>, 1> Conditions
;
678 /// All the nested matchers, sharing a common prefix.
679 std::vector
<Matcher
*> Matchers
;
681 /// An owning collection for any auxiliary matchers created while optimizing
682 /// nested matchers contained.
683 std::vector
<std::unique_ptr
<Matcher
>> MatcherStorage
;
686 /// Add a matcher to the collection of nested matchers if it meets the
687 /// requirements, and return true. If it doesn't, do nothing and return false.
689 /// Expected to preserve its argument, so it could be moved out later on.
690 bool addMatcher(Matcher
&Candidate
);
692 /// Mark the matcher as fully-built and ensure any invariants expected by both
693 /// optimize() and emit(...) methods. Generally, both sequences of calls
694 /// are expected to lead to a sensible result:
696 /// addMatcher(...)*; finalize(); optimize(); emit(...); and
697 /// addMatcher(...)*; finalize(); emit(...);
701 /// addMatcher(...)*; finalize(); { optimize()*; emit(...); }*
703 /// Multiple calls to optimize() are expected to be handled gracefully, though
704 /// optimize() is not expected to be idempotent. Multiple calls to finalize()
705 /// aren't generally supported. emit(...) is expected to be non-mutating and
706 /// producing the exact same results upon repeated calls.
708 /// addMatcher() calls after the finalize() call are not supported.
710 /// finalize() and optimize() are both allowed to mutate the contained
711 /// matchers, so moving them out after finalize() is not supported.
713 void optimize() override
;
714 void emit(MatchTable
&Table
) override
;
716 /// Could be used to move out the matchers added previously, unless finalize()
717 /// has been already called. If any of the matchers are moved out, the group
718 /// becomes safe to destroy, but not safe to re-use for anything else.
719 iterator_range
<std::vector
<Matcher
*>::iterator
> matchers() {
720 return make_range(Matchers
.begin(), Matchers
.end());
722 size_t size() const { return Matchers
.size(); }
723 bool empty() const { return Matchers
.empty(); }
725 std::unique_ptr
<PredicateMatcher
> popFirstCondition() override
{
726 assert(!Conditions
.empty() &&
727 "Trying to pop a condition from a condition-less group");
728 std::unique_ptr
<PredicateMatcher
> P
= std::move(Conditions
.front());
729 Conditions
.erase(Conditions
.begin());
732 const PredicateMatcher
&getFirstCondition() const override
{
733 assert(!Conditions
.empty() &&
734 "Trying to get a condition from a condition-less group");
735 return *Conditions
.front();
737 bool hasFirstCondition() const override
{ return !Conditions
.empty(); }
740 /// See if a candidate matcher could be added to this group solely by
741 /// analyzing its first condition.
742 bool candidateConditionMatches(const PredicateMatcher
&Predicate
) const;
745 class SwitchMatcher
: public Matcher
{
746 /// All the nested matchers, representing distinct switch-cases. The first
747 /// conditions (as Matcher::getFirstCondition() reports) of all the nested
748 /// matchers must share the same type and path to a value they check, in other
749 /// words, be isIdenticalDownToValue, but have different values they check
751 std::vector
<Matcher
*> Matchers
;
753 /// The representative condition, with a type and a path (InsnVarID and OpIdx
754 /// in most cases) shared by all the matchers contained.
755 std::unique_ptr
<PredicateMatcher
> Condition
= nullptr;
757 /// Temporary set used to check that the case values don't repeat within the
759 std::set
<MatchTableRecord
> Values
;
761 /// An owning collection for any auxiliary matchers created while optimizing
762 /// nested matchers contained.
763 std::vector
<std::unique_ptr
<Matcher
>> MatcherStorage
;
766 bool addMatcher(Matcher
&Candidate
);
769 void emit(MatchTable
&Table
) override
;
771 iterator_range
<std::vector
<Matcher
*>::iterator
> matchers() {
772 return make_range(Matchers
.begin(), Matchers
.end());
774 size_t size() const { return Matchers
.size(); }
775 bool empty() const { return Matchers
.empty(); }
777 std::unique_ptr
<PredicateMatcher
> popFirstCondition() override
{
778 // SwitchMatcher doesn't have a common first condition for its cases, as all
779 // the cases only share a kind of a value (a type and a path to it) they
780 // match, but deliberately differ in the actual value they match.
781 llvm_unreachable("Trying to pop a condition from a condition-less group");
783 const PredicateMatcher
&getFirstCondition() const override
{
784 llvm_unreachable("Trying to pop a condition from a condition-less group");
786 bool hasFirstCondition() const override
{ return false; }
789 /// See if the predicate type has a Switch-implementation for it.
790 static bool isSupportedPredicateType(const PredicateMatcher
&Predicate
);
792 bool candidateConditionMatches(const PredicateMatcher
&Predicate
) const;
795 static void emitPredicateSpecificOpcodes(const PredicateMatcher
&P
,
799 /// Generates code to check that a match rule matches.
800 class RuleMatcher
: public Matcher
{
802 using ActionList
= std::list
<std::unique_ptr
<MatchAction
>>;
803 using action_iterator
= ActionList::iterator
;
806 /// A list of matchers that all need to succeed for the current rule to match.
807 /// FIXME: This currently supports a single match position but could be
808 /// extended to support multiple positions to support div/rem fusion or
809 /// load-multiple instructions.
810 using MatchersTy
= std::vector
<std::unique_ptr
<InstructionMatcher
>> ;
813 /// A list of actions that need to be taken when all predicates in this rule
817 using DefinedInsnVariablesMap
= std::map
<InstructionMatcher
*, unsigned>;
819 /// A map of instruction matchers to the local variables
820 DefinedInsnVariablesMap InsnVariableIDs
;
822 using MutatableInsnSet
= SmallPtrSet
<InstructionMatcher
*, 4>;
824 // The set of instruction matchers that have not yet been claimed for mutation
826 MutatableInsnSet MutatableInsns
;
828 /// A map of named operands defined by the matchers that may be referenced by
830 StringMap
<OperandMatcher
*> DefinedOperands
;
832 /// ID for the next instruction variable defined with implicitlyDefineInsnVar()
833 unsigned NextInsnVarID
;
835 /// ID for the next output instruction allocated with allocateOutputInsnID()
836 unsigned NextOutputInsnID
;
838 /// ID for the next temporary register ID allocated with allocateTempRegID()
839 unsigned NextTempRegID
;
841 std::vector
<Record
*> RequiredFeatures
;
842 std::vector
<std::unique_ptr
<PredicateMatcher
>> EpilogueMatchers
;
844 ArrayRef
<SMLoc
> SrcLoc
;
846 typedef std::tuple
<Record
*, unsigned, unsigned>
847 DefinedComplexPatternSubOperand
;
848 typedef StringMap
<DefinedComplexPatternSubOperand
>
849 DefinedComplexPatternSubOperandMap
;
850 /// A map of Symbolic Names to ComplexPattern sub-operands.
851 DefinedComplexPatternSubOperandMap ComplexSubOperands
;
854 static uint64_t NextRuleID
;
857 RuleMatcher(ArrayRef
<SMLoc
> SrcLoc
)
858 : Matchers(), Actions(), InsnVariableIDs(), MutatableInsns(),
859 DefinedOperands(), NextInsnVarID(0), NextOutputInsnID(0),
860 NextTempRegID(0), SrcLoc(SrcLoc
), ComplexSubOperands(),
861 RuleID(NextRuleID
++) {}
862 RuleMatcher(RuleMatcher
&&Other
) = default;
863 RuleMatcher
&operator=(RuleMatcher
&&Other
) = default;
865 uint64_t getRuleID() const { return RuleID
; }
867 InstructionMatcher
&addInstructionMatcher(StringRef SymbolicName
);
868 void addRequiredFeature(Record
*Feature
);
869 const std::vector
<Record
*> &getRequiredFeatures() const;
871 template <class Kind
, class... Args
> Kind
&addAction(Args
&&... args
);
872 template <class Kind
, class... Args
>
873 action_iterator
insertAction(action_iterator InsertPt
, Args
&&... args
);
875 /// Define an instruction without emitting any code to do so.
876 unsigned implicitlyDefineInsnVar(InstructionMatcher
&Matcher
);
878 unsigned getInsnVarID(InstructionMatcher
&InsnMatcher
) const;
879 DefinedInsnVariablesMap::const_iterator
defined_insn_vars_begin() const {
880 return InsnVariableIDs
.begin();
882 DefinedInsnVariablesMap::const_iterator
defined_insn_vars_end() const {
883 return InsnVariableIDs
.end();
885 iterator_range
<typename
DefinedInsnVariablesMap::const_iterator
>
886 defined_insn_vars() const {
887 return make_range(defined_insn_vars_begin(), defined_insn_vars_end());
890 MutatableInsnSet::const_iterator
mutatable_insns_begin() const {
891 return MutatableInsns
.begin();
893 MutatableInsnSet::const_iterator
mutatable_insns_end() const {
894 return MutatableInsns
.end();
896 iterator_range
<typename
MutatableInsnSet::const_iterator
>
897 mutatable_insns() const {
898 return make_range(mutatable_insns_begin(), mutatable_insns_end());
900 void reserveInsnMatcherForMutation(InstructionMatcher
*InsnMatcher
) {
901 bool R
= MutatableInsns
.erase(InsnMatcher
);
902 assert(R
&& "Reserving a mutatable insn that isn't available");
906 action_iterator
actions_begin() { return Actions
.begin(); }
907 action_iterator
actions_end() { return Actions
.end(); }
908 iterator_range
<action_iterator
> actions() {
909 return make_range(actions_begin(), actions_end());
912 void defineOperand(StringRef SymbolicName
, OperandMatcher
&OM
);
914 Error
defineComplexSubOperand(StringRef SymbolicName
, Record
*ComplexPattern
,
915 unsigned RendererID
, unsigned SubOperandID
) {
916 if (ComplexSubOperands
.count(SymbolicName
))
918 "Complex suboperand referenced more than once (Operand: " +
921 ComplexSubOperands
[SymbolicName
] =
922 std::make_tuple(ComplexPattern
, RendererID
, SubOperandID
);
924 return Error::success();
927 Optional
<DefinedComplexPatternSubOperand
>
928 getComplexSubOperand(StringRef SymbolicName
) const {
929 const auto &I
= ComplexSubOperands
.find(SymbolicName
);
930 if (I
== ComplexSubOperands
.end())
935 InstructionMatcher
&getInstructionMatcher(StringRef SymbolicName
) const;
936 const OperandMatcher
&getOperandMatcher(StringRef Name
) const;
938 void optimize() override
;
939 void emit(MatchTable
&Table
) override
;
941 /// Compare the priority of this object and B.
943 /// Returns true if this object is more important than B.
944 bool isHigherPriorityThan(const RuleMatcher
&B
) const;
946 /// Report the maximum number of temporary operands needed by the rule
948 unsigned countRendererFns() const;
950 std::unique_ptr
<PredicateMatcher
> popFirstCondition() override
;
951 const PredicateMatcher
&getFirstCondition() const override
;
952 LLTCodeGen
getFirstConditionAsRootType();
953 bool hasFirstCondition() const override
;
954 unsigned getNumOperands() const;
955 StringRef
getOpcode() const;
957 // FIXME: Remove this as soon as possible
958 InstructionMatcher
&insnmatchers_front() const { return *Matchers
.front(); }
960 unsigned allocateOutputInsnID() { return NextOutputInsnID
++; }
961 unsigned allocateTempRegID() { return NextTempRegID
++; }
963 iterator_range
<MatchersTy::iterator
> insnmatchers() {
964 return make_range(Matchers
.begin(), Matchers
.end());
966 bool insnmatchers_empty() const { return Matchers
.empty(); }
967 void insnmatchers_pop_front() { Matchers
.erase(Matchers
.begin()); }
970 uint64_t RuleMatcher::NextRuleID
= 0;
972 using action_iterator
= RuleMatcher::action_iterator
;
974 template <class PredicateTy
> class PredicateListMatcher
{
976 /// Template instantiations should specialize this to return a string to use
977 /// for the comment emitted when there are no predicates.
978 std::string
getNoPredicateComment() const;
981 using PredicatesTy
= std::deque
<std::unique_ptr
<PredicateTy
>>;
982 PredicatesTy Predicates
;
984 /// Track if the list of predicates was manipulated by one of the optimization
986 bool Optimized
= false;
989 /// Construct a new predicate and add it to the matcher.
990 template <class Kind
, class... Args
>
991 Optional
<Kind
*> addPredicate(Args
&&... args
);
993 typename
PredicatesTy::iterator
predicates_begin() {
994 return Predicates
.begin();
996 typename
PredicatesTy::iterator
predicates_end() {
997 return Predicates
.end();
999 iterator_range
<typename
PredicatesTy::iterator
> predicates() {
1000 return make_range(predicates_begin(), predicates_end());
1002 typename
PredicatesTy::size_type
predicates_size() const {
1003 return Predicates
.size();
1005 bool predicates_empty() const { return Predicates
.empty(); }
1007 std::unique_ptr
<PredicateTy
> predicates_pop_front() {
1008 std::unique_ptr
<PredicateTy
> Front
= std::move(Predicates
.front());
1009 Predicates
.pop_front();
1014 void prependPredicate(std::unique_ptr
<PredicateTy
> &&Predicate
) {
1015 Predicates
.push_front(std::move(Predicate
));
1018 void eraseNullPredicates() {
1020 std::stable_partition(Predicates
.begin(), Predicates
.end(),
1021 std::logical_not
<std::unique_ptr
<PredicateTy
>>());
1022 if (NewEnd
!= Predicates
.begin()) {
1023 Predicates
.erase(Predicates
.begin(), NewEnd
);
1028 /// Emit MatchTable opcodes that tests whether all the predicates are met.
1029 template <class... Args
>
1030 void emitPredicateListOpcodes(MatchTable
&Table
, Args
&&... args
) {
1031 if (Predicates
.empty() && !Optimized
) {
1032 Table
<< MatchTable::Comment(getNoPredicateComment())
1033 << MatchTable::LineBreak
;
1037 for (const auto &Predicate
: predicates())
1038 Predicate
->emitPredicateOpcodes(Table
, std::forward
<Args
>(args
)...);
1042 class PredicateMatcher
{
1044 /// This enum is used for RTTI and also defines the priority that is given to
1045 /// the predicate when generating the matcher code. Kinds with higher priority
1046 /// must be tested first.
1048 /// The relative priority of OPM_LLT, OPM_RegBank, and OPM_MBB do not matter
1049 /// but OPM_Int must have priority over OPM_RegBank since constant integers
1050 /// are represented by a virtual register defined by a G_CONSTANT instruction.
1052 /// Note: The relative priority between IPM_ and OPM_ does not matter, they
1053 /// are currently not compared between each other.
1054 enum PredicateKind
{
1058 IPM_AtomicOrderingMMO
,
1060 IPM_MemoryVsLLTSize
,
1061 IPM_MemoryAddressSpace
,
1062 IPM_MemoryAlignment
,
1063 IPM_GenericPredicate
,
1083 PredicateMatcher(PredicateKind Kind
, unsigned InsnVarID
, unsigned OpIdx
= ~0)
1084 : Kind(Kind
), InsnVarID(InsnVarID
), OpIdx(OpIdx
) {}
1086 unsigned getInsnVarID() const { return InsnVarID
; }
1087 unsigned getOpIdx() const { return OpIdx
; }
1089 virtual ~PredicateMatcher() = default;
1090 /// Emit MatchTable opcodes that check the predicate for the given operand.
1091 virtual void emitPredicateOpcodes(MatchTable
&Table
,
1092 RuleMatcher
&Rule
) const = 0;
1094 PredicateKind
getKind() const { return Kind
; }
1096 virtual bool isIdentical(const PredicateMatcher
&B
) const {
1097 return B
.getKind() == getKind() && InsnVarID
== B
.InsnVarID
&&
1101 virtual bool isIdenticalDownToValue(const PredicateMatcher
&B
) const {
1102 return hasValue() && PredicateMatcher::isIdentical(B
);
1105 virtual MatchTableRecord
getValue() const {
1106 assert(hasValue() && "Can not get a value of a value-less predicate!");
1107 llvm_unreachable("Not implemented yet");
1109 virtual bool hasValue() const { return false; }
1111 /// Report the maximum number of temporary operands needed by the predicate
1113 virtual unsigned countRendererFns() const { return 0; }
1116 /// Generates code to check a predicate of an operand.
1118 /// Typical predicates include:
1119 /// * Operand is a particular register.
1120 /// * Operand is assigned a particular register bank.
1121 /// * Operand is an MBB.
1122 class OperandPredicateMatcher
: public PredicateMatcher
{
1124 OperandPredicateMatcher(PredicateKind Kind
, unsigned InsnVarID
,
1126 : PredicateMatcher(Kind
, InsnVarID
, OpIdx
) {}
1127 virtual ~OperandPredicateMatcher() {}
1129 /// Compare the priority of this object and B.
1131 /// Returns true if this object is more important than B.
1132 virtual bool isHigherPriorityThan(const OperandPredicateMatcher
&B
) const;
1137 PredicateListMatcher
<OperandPredicateMatcher
>::getNoPredicateComment() const {
1138 return "No operand predicates";
1141 /// Generates code to check that a register operand is defined by the same exact
1143 class SameOperandMatcher
: public OperandPredicateMatcher
{
1144 std::string MatchingName
;
1147 SameOperandMatcher(unsigned InsnVarID
, unsigned OpIdx
, StringRef MatchingName
)
1148 : OperandPredicateMatcher(OPM_SameOperand
, InsnVarID
, OpIdx
),
1149 MatchingName(MatchingName
) {}
1151 static bool classof(const PredicateMatcher
*P
) {
1152 return P
->getKind() == OPM_SameOperand
;
1155 void emitPredicateOpcodes(MatchTable
&Table
,
1156 RuleMatcher
&Rule
) const override
;
1158 bool isIdentical(const PredicateMatcher
&B
) const override
{
1159 return OperandPredicateMatcher::isIdentical(B
) &&
1160 MatchingName
== cast
<SameOperandMatcher
>(&B
)->MatchingName
;
1164 /// Generates code to check that an operand is a particular LLT.
1165 class LLTOperandMatcher
: public OperandPredicateMatcher
{
1170 static std::map
<LLTCodeGen
, unsigned> TypeIDValues
;
1172 static void initTypeIDValuesMap() {
1173 TypeIDValues
.clear();
1176 for (const LLTCodeGen LLTy
: KnownTypes
)
1177 TypeIDValues
[LLTy
] = ID
++;
1180 LLTOperandMatcher(unsigned InsnVarID
, unsigned OpIdx
, const LLTCodeGen
&Ty
)
1181 : OperandPredicateMatcher(OPM_LLT
, InsnVarID
, OpIdx
), Ty(Ty
) {
1182 KnownTypes
.insert(Ty
);
1185 static bool classof(const PredicateMatcher
*P
) {
1186 return P
->getKind() == OPM_LLT
;
1188 bool isIdentical(const PredicateMatcher
&B
) const override
{
1189 return OperandPredicateMatcher::isIdentical(B
) &&
1190 Ty
== cast
<LLTOperandMatcher
>(&B
)->Ty
;
1192 MatchTableRecord
getValue() const override
{
1193 const auto VI
= TypeIDValues
.find(Ty
);
1194 if (VI
== TypeIDValues
.end())
1195 return MatchTable::NamedValue(getTy().getCxxEnumValue());
1196 return MatchTable::NamedValue(getTy().getCxxEnumValue(), VI
->second
);
1198 bool hasValue() const override
{
1199 if (TypeIDValues
.size() != KnownTypes
.size())
1200 initTypeIDValuesMap();
1201 return TypeIDValues
.count(Ty
);
1204 LLTCodeGen
getTy() const { return Ty
; }
1206 void emitPredicateOpcodes(MatchTable
&Table
,
1207 RuleMatcher
&Rule
) const override
{
1208 Table
<< MatchTable::Opcode("GIM_CheckType") << MatchTable::Comment("MI")
1209 << MatchTable::IntValue(InsnVarID
) << MatchTable::Comment("Op")
1210 << MatchTable::IntValue(OpIdx
) << MatchTable::Comment("Type")
1211 << getValue() << MatchTable::LineBreak
;
1215 std::map
<LLTCodeGen
, unsigned> LLTOperandMatcher::TypeIDValues
;
1217 /// Generates code to check that an operand is a pointer to any address space.
1219 /// In SelectionDAG, the types did not describe pointers or address spaces. As a
1220 /// result, iN is used to describe a pointer of N bits to any address space and
1221 /// PatFrag predicates are typically used to constrain the address space. There's
1222 /// no reliable means to derive the missing type information from the pattern so
1223 /// imported rules must test the components of a pointer separately.
1225 /// If SizeInBits is zero, then the pointer size will be obtained from the
1227 class PointerToAnyOperandMatcher
: public OperandPredicateMatcher
{
1229 unsigned SizeInBits
;
1232 PointerToAnyOperandMatcher(unsigned InsnVarID
, unsigned OpIdx
,
1233 unsigned SizeInBits
)
1234 : OperandPredicateMatcher(OPM_PointerToAny
, InsnVarID
, OpIdx
),
1235 SizeInBits(SizeInBits
) {}
1237 static bool classof(const OperandPredicateMatcher
*P
) {
1238 return P
->getKind() == OPM_PointerToAny
;
1241 void emitPredicateOpcodes(MatchTable
&Table
,
1242 RuleMatcher
&Rule
) const override
{
1243 Table
<< MatchTable::Opcode("GIM_CheckPointerToAny")
1244 << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID
)
1245 << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx
)
1246 << MatchTable::Comment("SizeInBits")
1247 << MatchTable::IntValue(SizeInBits
) << MatchTable::LineBreak
;
1251 /// Generates code to check that an operand is a particular target constant.
1252 class ComplexPatternOperandMatcher
: public OperandPredicateMatcher
{
1254 const OperandMatcher
&Operand
;
1255 const Record
&TheDef
;
1257 unsigned getAllocatedTemporariesBaseID() const;
1260 bool isIdentical(const PredicateMatcher
&B
) const override
{ return false; }
1262 ComplexPatternOperandMatcher(unsigned InsnVarID
, unsigned OpIdx
,
1263 const OperandMatcher
&Operand
,
1264 const Record
&TheDef
)
1265 : OperandPredicateMatcher(OPM_ComplexPattern
, InsnVarID
, OpIdx
),
1266 Operand(Operand
), TheDef(TheDef
) {}
1268 static bool classof(const PredicateMatcher
*P
) {
1269 return P
->getKind() == OPM_ComplexPattern
;
1272 void emitPredicateOpcodes(MatchTable
&Table
,
1273 RuleMatcher
&Rule
) const override
{
1274 unsigned ID
= getAllocatedTemporariesBaseID();
1275 Table
<< MatchTable::Opcode("GIM_CheckComplexPattern")
1276 << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID
)
1277 << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx
)
1278 << MatchTable::Comment("Renderer") << MatchTable::IntValue(ID
)
1279 << MatchTable::NamedValue(("GICP_" + TheDef
.getName()).str())
1280 << MatchTable::LineBreak
;
1283 unsigned countRendererFns() const override
{
1288 /// Generates code to check that an operand is in a particular register bank.
1289 class RegisterBankOperandMatcher
: public OperandPredicateMatcher
{
1291 const CodeGenRegisterClass
&RC
;
1294 RegisterBankOperandMatcher(unsigned InsnVarID
, unsigned OpIdx
,
1295 const CodeGenRegisterClass
&RC
)
1296 : OperandPredicateMatcher(OPM_RegBank
, InsnVarID
, OpIdx
), RC(RC
) {}
1298 bool isIdentical(const PredicateMatcher
&B
) const override
{
1299 return OperandPredicateMatcher::isIdentical(B
) &&
1300 RC
.getDef() == cast
<RegisterBankOperandMatcher
>(&B
)->RC
.getDef();
1303 static bool classof(const PredicateMatcher
*P
) {
1304 return P
->getKind() == OPM_RegBank
;
1307 void emitPredicateOpcodes(MatchTable
&Table
,
1308 RuleMatcher
&Rule
) const override
{
1309 Table
<< MatchTable::Opcode("GIM_CheckRegBankForClass")
1310 << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID
)
1311 << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx
)
1312 << MatchTable::Comment("RC")
1313 << MatchTable::NamedValue(RC
.getQualifiedName() + "RegClassID")
1314 << MatchTable::LineBreak
;
1318 /// Generates code to check that an operand is a basic block.
1319 class MBBOperandMatcher
: public OperandPredicateMatcher
{
1321 MBBOperandMatcher(unsigned InsnVarID
, unsigned OpIdx
)
1322 : OperandPredicateMatcher(OPM_MBB
, InsnVarID
, OpIdx
) {}
1324 static bool classof(const PredicateMatcher
*P
) {
1325 return P
->getKind() == OPM_MBB
;
1328 void emitPredicateOpcodes(MatchTable
&Table
,
1329 RuleMatcher
&Rule
) const override
{
1330 Table
<< MatchTable::Opcode("GIM_CheckIsMBB") << MatchTable::Comment("MI")
1331 << MatchTable::IntValue(InsnVarID
) << MatchTable::Comment("Op")
1332 << MatchTable::IntValue(OpIdx
) << MatchTable::LineBreak
;
1336 /// Generates code to check that an operand is a G_CONSTANT with a particular
1338 class ConstantIntOperandMatcher
: public OperandPredicateMatcher
{
1343 ConstantIntOperandMatcher(unsigned InsnVarID
, unsigned OpIdx
, int64_t Value
)
1344 : OperandPredicateMatcher(OPM_Int
, InsnVarID
, OpIdx
), Value(Value
) {}
1346 bool isIdentical(const PredicateMatcher
&B
) const override
{
1347 return OperandPredicateMatcher::isIdentical(B
) &&
1348 Value
== cast
<ConstantIntOperandMatcher
>(&B
)->Value
;
1351 static bool classof(const PredicateMatcher
*P
) {
1352 return P
->getKind() == OPM_Int
;
1355 void emitPredicateOpcodes(MatchTable
&Table
,
1356 RuleMatcher
&Rule
) const override
{
1357 Table
<< MatchTable::Opcode("GIM_CheckConstantInt")
1358 << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID
)
1359 << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx
)
1360 << MatchTable::IntValue(Value
) << MatchTable::LineBreak
;
1364 /// Generates code to check that an operand is a raw int (where MO.isImm() or
1365 /// MO.isCImm() is true).
1366 class LiteralIntOperandMatcher
: public OperandPredicateMatcher
{
1371 LiteralIntOperandMatcher(unsigned InsnVarID
, unsigned OpIdx
, int64_t Value
)
1372 : OperandPredicateMatcher(OPM_LiteralInt
, InsnVarID
, OpIdx
),
1375 bool isIdentical(const PredicateMatcher
&B
) const override
{
1376 return OperandPredicateMatcher::isIdentical(B
) &&
1377 Value
== cast
<LiteralIntOperandMatcher
>(&B
)->Value
;
1380 static bool classof(const PredicateMatcher
*P
) {
1381 return P
->getKind() == OPM_LiteralInt
;
1384 void emitPredicateOpcodes(MatchTable
&Table
,
1385 RuleMatcher
&Rule
) const override
{
1386 Table
<< MatchTable::Opcode("GIM_CheckLiteralInt")
1387 << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID
)
1388 << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx
)
1389 << MatchTable::IntValue(Value
) << MatchTable::LineBreak
;
1393 /// Generates code to check that an operand is an CmpInst predicate
1394 class CmpPredicateOperandMatcher
: public OperandPredicateMatcher
{
1396 std::string PredName
;
1399 CmpPredicateOperandMatcher(unsigned InsnVarID
, unsigned OpIdx
,
1401 : OperandPredicateMatcher(OPM_CmpPredicate
, InsnVarID
, OpIdx
), PredName(P
) {}
1403 bool isIdentical(const PredicateMatcher
&B
) const override
{
1404 return OperandPredicateMatcher::isIdentical(B
) &&
1405 PredName
== cast
<CmpPredicateOperandMatcher
>(&B
)->PredName
;
1408 static bool classof(const PredicateMatcher
*P
) {
1409 return P
->getKind() == OPM_CmpPredicate
;
1412 void emitPredicateOpcodes(MatchTable
&Table
,
1413 RuleMatcher
&Rule
) const override
{
1414 Table
<< MatchTable::Opcode("GIM_CheckCmpPredicate")
1415 << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID
)
1416 << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx
)
1417 << MatchTable::Comment("Predicate")
1418 << MatchTable::NamedValue("CmpInst", PredName
)
1419 << MatchTable::LineBreak
;
1423 /// Generates code to check that an operand is an intrinsic ID.
1424 class IntrinsicIDOperandMatcher
: public OperandPredicateMatcher
{
1426 const CodeGenIntrinsic
*II
;
1429 IntrinsicIDOperandMatcher(unsigned InsnVarID
, unsigned OpIdx
,
1430 const CodeGenIntrinsic
*II
)
1431 : OperandPredicateMatcher(OPM_IntrinsicID
, InsnVarID
, OpIdx
), II(II
) {}
1433 bool isIdentical(const PredicateMatcher
&B
) const override
{
1434 return OperandPredicateMatcher::isIdentical(B
) &&
1435 II
== cast
<IntrinsicIDOperandMatcher
>(&B
)->II
;
1438 static bool classof(const PredicateMatcher
*P
) {
1439 return P
->getKind() == OPM_IntrinsicID
;
1442 void emitPredicateOpcodes(MatchTable
&Table
,
1443 RuleMatcher
&Rule
) const override
{
1444 Table
<< MatchTable::Opcode("GIM_CheckIntrinsicID")
1445 << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID
)
1446 << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx
)
1447 << MatchTable::NamedValue("Intrinsic::" + II
->EnumName
)
1448 << MatchTable::LineBreak
;
1452 /// Generates code to check that a set of predicates match for a particular
1454 class OperandMatcher
: public PredicateListMatcher
<OperandPredicateMatcher
> {
1456 InstructionMatcher
&Insn
;
1458 std::string SymbolicName
;
1460 /// The index of the first temporary variable allocated to this operand. The
1461 /// number of allocated temporaries can be found with
1462 /// countRendererFns().
1463 unsigned AllocatedTemporariesBaseID
;
1466 OperandMatcher(InstructionMatcher
&Insn
, unsigned OpIdx
,
1467 const std::string
&SymbolicName
,
1468 unsigned AllocatedTemporariesBaseID
)
1469 : Insn(Insn
), OpIdx(OpIdx
), SymbolicName(SymbolicName
),
1470 AllocatedTemporariesBaseID(AllocatedTemporariesBaseID
) {}
1472 bool hasSymbolicName() const { return !SymbolicName
.empty(); }
1473 const StringRef
getSymbolicName() const { return SymbolicName
; }
1474 void setSymbolicName(StringRef Name
) {
1475 assert(SymbolicName
.empty() && "Operand already has a symbolic name");
1476 SymbolicName
= Name
;
1479 /// Construct a new operand predicate and add it to the matcher.
1480 template <class Kind
, class... Args
>
1481 Optional
<Kind
*> addPredicate(Args
&&... args
) {
1482 if (isSameAsAnotherOperand())
1484 Predicates
.emplace_back(std::make_unique
<Kind
>(
1485 getInsnVarID(), getOpIdx(), std::forward
<Args
>(args
)...));
1486 return static_cast<Kind
*>(Predicates
.back().get());
1489 unsigned getOpIdx() const { return OpIdx
; }
1490 unsigned getInsnVarID() const;
1492 std::string
getOperandExpr(unsigned InsnVarID
) const {
1493 return "State.MIs[" + llvm::to_string(InsnVarID
) + "]->getOperand(" +
1494 llvm::to_string(OpIdx
) + ")";
1497 InstructionMatcher
&getInstructionMatcher() const { return Insn
; }
1499 Error
addTypeCheckPredicate(const TypeSetByHwMode
&VTy
,
1500 bool OperandIsAPointer
);
1502 /// Emit MatchTable opcodes that test whether the instruction named in
1503 /// InsnVarID matches all the predicates and all the operands.
1504 void emitPredicateOpcodes(MatchTable
&Table
, RuleMatcher
&Rule
) {
1506 std::string Comment
;
1507 raw_string_ostream
CommentOS(Comment
);
1508 CommentOS
<< "MIs[" << getInsnVarID() << "] ";
1509 if (SymbolicName
.empty())
1510 CommentOS
<< "Operand " << OpIdx
;
1512 CommentOS
<< SymbolicName
;
1513 Table
<< MatchTable::Comment(CommentOS
.str()) << MatchTable::LineBreak
;
1516 emitPredicateListOpcodes(Table
, Rule
);
1519 /// Compare the priority of this object and B.
1521 /// Returns true if this object is more important than B.
1522 bool isHigherPriorityThan(OperandMatcher
&B
) {
1523 // Operand matchers involving more predicates have higher priority.
1524 if (predicates_size() > B
.predicates_size())
1526 if (predicates_size() < B
.predicates_size())
1529 // This assumes that predicates are added in a consistent order.
1530 for (auto &&Predicate
: zip(predicates(), B
.predicates())) {
1531 if (std::get
<0>(Predicate
)->isHigherPriorityThan(*std::get
<1>(Predicate
)))
1533 if (std::get
<1>(Predicate
)->isHigherPriorityThan(*std::get
<0>(Predicate
)))
1540 /// Report the maximum number of temporary operands needed by the operand
1542 unsigned countRendererFns() {
1543 return std::accumulate(
1544 predicates().begin(), predicates().end(), 0,
1546 const std::unique_ptr
<OperandPredicateMatcher
> &Predicate
) {
1547 return A
+ Predicate
->countRendererFns();
1551 unsigned getAllocatedTemporariesBaseID() const {
1552 return AllocatedTemporariesBaseID
;
1555 bool isSameAsAnotherOperand() {
1556 for (const auto &Predicate
: predicates())
1557 if (isa
<SameOperandMatcher
>(Predicate
))
1563 Error
OperandMatcher::addTypeCheckPredicate(const TypeSetByHwMode
&VTy
,
1564 bool OperandIsAPointer
) {
1565 if (!VTy
.isMachineValueType())
1566 return failedImport("unsupported typeset");
1568 if (VTy
.getMachineValueType() == MVT::iPTR
&& OperandIsAPointer
) {
1569 addPredicate
<PointerToAnyOperandMatcher
>(0);
1570 return Error::success();
1573 auto OpTyOrNone
= MVTToLLT(VTy
.getMachineValueType().SimpleTy
);
1575 return failedImport("unsupported type");
1577 if (OperandIsAPointer
)
1578 addPredicate
<PointerToAnyOperandMatcher
>(OpTyOrNone
->get().getSizeInBits());
1579 else if (VTy
.isPointer())
1580 addPredicate
<LLTOperandMatcher
>(LLT::pointer(VTy
.getPtrAddrSpace(),
1581 OpTyOrNone
->get().getSizeInBits()));
1583 addPredicate
<LLTOperandMatcher
>(*OpTyOrNone
);
1584 return Error::success();
1587 unsigned ComplexPatternOperandMatcher::getAllocatedTemporariesBaseID() const {
1588 return Operand
.getAllocatedTemporariesBaseID();
1591 /// Generates code to check a predicate on an instruction.
1593 /// Typical predicates include:
1594 /// * The opcode of the instruction is a particular value.
1595 /// * The nsw/nuw flag is/isn't set.
1596 class InstructionPredicateMatcher
: public PredicateMatcher
{
1598 InstructionPredicateMatcher(PredicateKind Kind
, unsigned InsnVarID
)
1599 : PredicateMatcher(Kind
, InsnVarID
) {}
1600 virtual ~InstructionPredicateMatcher() {}
1602 /// Compare the priority of this object and B.
1604 /// Returns true if this object is more important than B.
1606 isHigherPriorityThan(const InstructionPredicateMatcher
&B
) const {
1607 return Kind
< B
.Kind
;
1613 PredicateListMatcher
<PredicateMatcher
>::getNoPredicateComment() const {
1614 return "No instruction predicates";
1617 /// Generates code to check the opcode of an instruction.
1618 class InstructionOpcodeMatcher
: public InstructionPredicateMatcher
{
1620 const CodeGenInstruction
*I
;
1622 static DenseMap
<const CodeGenInstruction
*, unsigned> OpcodeValues
;
1625 static void initOpcodeValuesMap(const CodeGenTarget
&Target
) {
1626 OpcodeValues
.clear();
1628 unsigned OpcodeValue
= 0;
1629 for (const CodeGenInstruction
*I
: Target
.getInstructionsByEnumValue())
1630 OpcodeValues
[I
] = OpcodeValue
++;
1633 InstructionOpcodeMatcher(unsigned InsnVarID
, const CodeGenInstruction
*I
)
1634 : InstructionPredicateMatcher(IPM_Opcode
, InsnVarID
), I(I
) {}
1636 static bool classof(const PredicateMatcher
*P
) {
1637 return P
->getKind() == IPM_Opcode
;
1640 bool isIdentical(const PredicateMatcher
&B
) const override
{
1641 return InstructionPredicateMatcher::isIdentical(B
) &&
1642 I
== cast
<InstructionOpcodeMatcher
>(&B
)->I
;
1644 MatchTableRecord
getValue() const override
{
1645 const auto VI
= OpcodeValues
.find(I
);
1646 if (VI
!= OpcodeValues
.end())
1647 return MatchTable::NamedValue(I
->Namespace
, I
->TheDef
->getName(),
1649 return MatchTable::NamedValue(I
->Namespace
, I
->TheDef
->getName());
1651 bool hasValue() const override
{ return OpcodeValues
.count(I
); }
1653 void emitPredicateOpcodes(MatchTable
&Table
,
1654 RuleMatcher
&Rule
) const override
{
1655 Table
<< MatchTable::Opcode("GIM_CheckOpcode") << MatchTable::Comment("MI")
1656 << MatchTable::IntValue(InsnVarID
) << getValue()
1657 << MatchTable::LineBreak
;
1660 /// Compare the priority of this object and B.
1662 /// Returns true if this object is more important than B.
1664 isHigherPriorityThan(const InstructionPredicateMatcher
&B
) const override
{
1665 if (InstructionPredicateMatcher::isHigherPriorityThan(B
))
1667 if (B
.InstructionPredicateMatcher::isHigherPriorityThan(*this))
1670 // Prioritize opcodes for cosmetic reasons in the generated source. Although
1671 // this is cosmetic at the moment, we may want to drive a similar ordering
1672 // using instruction frequency information to improve compile time.
1673 if (const InstructionOpcodeMatcher
*BO
=
1674 dyn_cast
<InstructionOpcodeMatcher
>(&B
))
1675 return I
->TheDef
->getName() < BO
->I
->TheDef
->getName();
1680 bool isConstantInstruction() const {
1681 return I
->TheDef
->getName() == "G_CONSTANT";
1684 StringRef
getOpcode() const { return I
->TheDef
->getName(); }
1685 unsigned getNumOperands() const { return I
->Operands
.size(); }
1687 StringRef
getOperandType(unsigned OpIdx
) const {
1688 return I
->Operands
[OpIdx
].OperandType
;
1692 DenseMap
<const CodeGenInstruction
*, unsigned>
1693 InstructionOpcodeMatcher::OpcodeValues
;
1695 class InstructionNumOperandsMatcher final
: public InstructionPredicateMatcher
{
1696 unsigned NumOperands
= 0;
1699 InstructionNumOperandsMatcher(unsigned InsnVarID
, unsigned NumOperands
)
1700 : InstructionPredicateMatcher(IPM_NumOperands
, InsnVarID
),
1701 NumOperands(NumOperands
) {}
1703 static bool classof(const PredicateMatcher
*P
) {
1704 return P
->getKind() == IPM_NumOperands
;
1707 bool isIdentical(const PredicateMatcher
&B
) const override
{
1708 return InstructionPredicateMatcher::isIdentical(B
) &&
1709 NumOperands
== cast
<InstructionNumOperandsMatcher
>(&B
)->NumOperands
;
1712 void emitPredicateOpcodes(MatchTable
&Table
,
1713 RuleMatcher
&Rule
) const override
{
1714 Table
<< MatchTable::Opcode("GIM_CheckNumOperands")
1715 << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID
)
1716 << MatchTable::Comment("Expected")
1717 << MatchTable::IntValue(NumOperands
) << MatchTable::LineBreak
;
1721 /// Generates code to check that this instruction is a constant whose value
1722 /// meets an immediate predicate.
1724 /// Immediates are slightly odd since they are typically used like an operand
1725 /// but are represented as an operator internally. We typically write simm8:$src
1726 /// in a tablegen pattern, but this is just syntactic sugar for
1727 /// (imm:i32)<<P:Predicate_simm8>>:$imm which more directly describes the nodes
1728 /// that will be matched and the predicate (which is attached to the imm
1729 /// operator) that will be tested. In SelectionDAG this describes a
1730 /// ConstantSDNode whose internal value will be tested using the simm8 predicate.
1732 /// The corresponding GlobalISel representation is %1 = G_CONSTANT iN Value. In
1733 /// this representation, the immediate could be tested with an
1734 /// InstructionMatcher, InstructionOpcodeMatcher, OperandMatcher, and a
1735 /// OperandPredicateMatcher-subclass to check the Value meets the predicate but
1736 /// there are two implementation issues with producing that matcher
1737 /// configuration from the SelectionDAG pattern:
1738 /// * ImmLeaf is a PatFrag whose root is an InstructionMatcher. This means that
1739 /// were we to sink the immediate predicate to the operand we would have to
1740 /// have two partial implementations of PatFrag support, one for immediates
1741 /// and one for non-immediates.
1742 /// * At the point we handle the predicate, the OperandMatcher hasn't been
1743 /// created yet. If we were to sink the predicate to the OperandMatcher we
1744 /// would also have to complicate (or duplicate) the code that descends and
1745 /// creates matchers for the subtree.
1746 /// Overall, it's simpler to handle it in the place it was found.
1747 class InstructionImmPredicateMatcher
: public InstructionPredicateMatcher
{
1749 TreePredicateFn Predicate
;
1752 InstructionImmPredicateMatcher(unsigned InsnVarID
,
1753 const TreePredicateFn
&Predicate
)
1754 : InstructionPredicateMatcher(IPM_ImmPredicate
, InsnVarID
),
1755 Predicate(Predicate
) {}
1757 bool isIdentical(const PredicateMatcher
&B
) const override
{
1758 return InstructionPredicateMatcher::isIdentical(B
) &&
1759 Predicate
.getOrigPatFragRecord() ==
1760 cast
<InstructionImmPredicateMatcher
>(&B
)
1761 ->Predicate
.getOrigPatFragRecord();
1764 static bool classof(const PredicateMatcher
*P
) {
1765 return P
->getKind() == IPM_ImmPredicate
;
1768 void emitPredicateOpcodes(MatchTable
&Table
,
1769 RuleMatcher
&Rule
) const override
{
1770 Table
<< MatchTable::Opcode(getMatchOpcodeForPredicate(Predicate
))
1771 << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID
)
1772 << MatchTable::Comment("Predicate")
1773 << MatchTable::NamedValue(getEnumNameForPredicate(Predicate
))
1774 << MatchTable::LineBreak
;
1778 /// Generates code to check that a memory instruction has a atomic ordering
1779 /// MachineMemoryOperand.
1780 class AtomicOrderingMMOPredicateMatcher
: public InstructionPredicateMatcher
{
1790 AOComparator Comparator
;
1793 AtomicOrderingMMOPredicateMatcher(unsigned InsnVarID
, StringRef Order
,
1794 AOComparator Comparator
= AO_Exactly
)
1795 : InstructionPredicateMatcher(IPM_AtomicOrderingMMO
, InsnVarID
),
1796 Order(Order
), Comparator(Comparator
) {}
1798 static bool classof(const PredicateMatcher
*P
) {
1799 return P
->getKind() == IPM_AtomicOrderingMMO
;
1802 bool isIdentical(const PredicateMatcher
&B
) const override
{
1803 if (!InstructionPredicateMatcher::isIdentical(B
))
1805 const auto &R
= *cast
<AtomicOrderingMMOPredicateMatcher
>(&B
);
1806 return Order
== R
.Order
&& Comparator
== R
.Comparator
;
1809 void emitPredicateOpcodes(MatchTable
&Table
,
1810 RuleMatcher
&Rule
) const override
{
1811 StringRef Opcode
= "GIM_CheckAtomicOrdering";
1813 if (Comparator
== AO_OrStronger
)
1814 Opcode
= "GIM_CheckAtomicOrderingOrStrongerThan";
1815 if (Comparator
== AO_WeakerThan
)
1816 Opcode
= "GIM_CheckAtomicOrderingWeakerThan";
1818 Table
<< MatchTable::Opcode(Opcode
) << MatchTable::Comment("MI")
1819 << MatchTable::IntValue(InsnVarID
) << MatchTable::Comment("Order")
1820 << MatchTable::NamedValue(("(int64_t)AtomicOrdering::" + Order
).str())
1821 << MatchTable::LineBreak
;
1825 /// Generates code to check that the size of an MMO is exactly N bytes.
1826 class MemorySizePredicateMatcher
: public InstructionPredicateMatcher
{
1832 MemorySizePredicateMatcher(unsigned InsnVarID
, unsigned MMOIdx
, unsigned Size
)
1833 : InstructionPredicateMatcher(IPM_MemoryLLTSize
, InsnVarID
),
1834 MMOIdx(MMOIdx
), Size(Size
) {}
1836 static bool classof(const PredicateMatcher
*P
) {
1837 return P
->getKind() == IPM_MemoryLLTSize
;
1839 bool isIdentical(const PredicateMatcher
&B
) const override
{
1840 return InstructionPredicateMatcher::isIdentical(B
) &&
1841 MMOIdx
== cast
<MemorySizePredicateMatcher
>(&B
)->MMOIdx
&&
1842 Size
== cast
<MemorySizePredicateMatcher
>(&B
)->Size
;
1845 void emitPredicateOpcodes(MatchTable
&Table
,
1846 RuleMatcher
&Rule
) const override
{
1847 Table
<< MatchTable::Opcode("GIM_CheckMemorySizeEqualTo")
1848 << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID
)
1849 << MatchTable::Comment("MMO") << MatchTable::IntValue(MMOIdx
)
1850 << MatchTable::Comment("Size") << MatchTable::IntValue(Size
)
1851 << MatchTable::LineBreak
;
1855 class MemoryAddressSpacePredicateMatcher
: public InstructionPredicateMatcher
{
1858 SmallVector
<unsigned, 4> AddrSpaces
;
1861 MemoryAddressSpacePredicateMatcher(unsigned InsnVarID
, unsigned MMOIdx
,
1862 ArrayRef
<unsigned> AddrSpaces
)
1863 : InstructionPredicateMatcher(IPM_MemoryAddressSpace
, InsnVarID
),
1864 MMOIdx(MMOIdx
), AddrSpaces(AddrSpaces
.begin(), AddrSpaces
.end()) {}
1866 static bool classof(const PredicateMatcher
*P
) {
1867 return P
->getKind() == IPM_MemoryAddressSpace
;
1869 bool isIdentical(const PredicateMatcher
&B
) const override
{
1870 if (!InstructionPredicateMatcher::isIdentical(B
))
1872 auto *Other
= cast
<MemoryAddressSpacePredicateMatcher
>(&B
);
1873 return MMOIdx
== Other
->MMOIdx
&& AddrSpaces
== Other
->AddrSpaces
;
1876 void emitPredicateOpcodes(MatchTable
&Table
,
1877 RuleMatcher
&Rule
) const override
{
1878 Table
<< MatchTable::Opcode("GIM_CheckMemoryAddressSpace")
1879 << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID
)
1880 << MatchTable::Comment("MMO") << MatchTable::IntValue(MMOIdx
)
1881 // Encode number of address spaces to expect.
1882 << MatchTable::Comment("NumAddrSpace")
1883 << MatchTable::IntValue(AddrSpaces
.size());
1884 for (unsigned AS
: AddrSpaces
)
1885 Table
<< MatchTable::Comment("AddrSpace") << MatchTable::IntValue(AS
);
1887 Table
<< MatchTable::LineBreak
;
1891 class MemoryAlignmentPredicateMatcher
: public InstructionPredicateMatcher
{
1897 MemoryAlignmentPredicateMatcher(unsigned InsnVarID
, unsigned MMOIdx
,
1899 : InstructionPredicateMatcher(IPM_MemoryAlignment
, InsnVarID
),
1900 MMOIdx(MMOIdx
), MinAlign(MinAlign
) {
1901 assert(MinAlign
> 0);
1904 static bool classof(const PredicateMatcher
*P
) {
1905 return P
->getKind() == IPM_MemoryAlignment
;
1908 bool isIdentical(const PredicateMatcher
&B
) const override
{
1909 if (!InstructionPredicateMatcher::isIdentical(B
))
1911 auto *Other
= cast
<MemoryAlignmentPredicateMatcher
>(&B
);
1912 return MMOIdx
== Other
->MMOIdx
&& MinAlign
== Other
->MinAlign
;
1915 void emitPredicateOpcodes(MatchTable
&Table
,
1916 RuleMatcher
&Rule
) const override
{
1917 Table
<< MatchTable::Opcode("GIM_CheckMemoryAlignment")
1918 << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID
)
1919 << MatchTable::Comment("MMO") << MatchTable::IntValue(MMOIdx
)
1920 << MatchTable::Comment("MinAlign") << MatchTable::IntValue(MinAlign
)
1921 << MatchTable::LineBreak
;
1925 /// Generates code to check that the size of an MMO is less-than, equal-to, or
1926 /// greater than a given LLT.
1927 class MemoryVsLLTSizePredicateMatcher
: public InstructionPredicateMatcher
{
1937 RelationKind Relation
;
1941 MemoryVsLLTSizePredicateMatcher(unsigned InsnVarID
, unsigned MMOIdx
,
1942 enum RelationKind Relation
,
1944 : InstructionPredicateMatcher(IPM_MemoryVsLLTSize
, InsnVarID
),
1945 MMOIdx(MMOIdx
), Relation(Relation
), OpIdx(OpIdx
) {}
1947 static bool classof(const PredicateMatcher
*P
) {
1948 return P
->getKind() == IPM_MemoryVsLLTSize
;
1950 bool isIdentical(const PredicateMatcher
&B
) const override
{
1951 return InstructionPredicateMatcher::isIdentical(B
) &&
1952 MMOIdx
== cast
<MemoryVsLLTSizePredicateMatcher
>(&B
)->MMOIdx
&&
1953 Relation
== cast
<MemoryVsLLTSizePredicateMatcher
>(&B
)->Relation
&&
1954 OpIdx
== cast
<MemoryVsLLTSizePredicateMatcher
>(&B
)->OpIdx
;
1957 void emitPredicateOpcodes(MatchTable
&Table
,
1958 RuleMatcher
&Rule
) const override
{
1959 Table
<< MatchTable::Opcode(Relation
== EqualTo
1960 ? "GIM_CheckMemorySizeEqualToLLT"
1961 : Relation
== GreaterThan
1962 ? "GIM_CheckMemorySizeGreaterThanLLT"
1963 : "GIM_CheckMemorySizeLessThanLLT")
1964 << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID
)
1965 << MatchTable::Comment("MMO") << MatchTable::IntValue(MMOIdx
)
1966 << MatchTable::Comment("OpIdx") << MatchTable::IntValue(OpIdx
)
1967 << MatchTable::LineBreak
;
1971 /// Generates code to check an arbitrary C++ instruction predicate.
1972 class GenericInstructionPredicateMatcher
: public InstructionPredicateMatcher
{
1974 TreePredicateFn Predicate
;
1977 GenericInstructionPredicateMatcher(unsigned InsnVarID
,
1978 TreePredicateFn Predicate
)
1979 : InstructionPredicateMatcher(IPM_GenericPredicate
, InsnVarID
),
1980 Predicate(Predicate
) {}
1982 static bool classof(const InstructionPredicateMatcher
*P
) {
1983 return P
->getKind() == IPM_GenericPredicate
;
1985 bool isIdentical(const PredicateMatcher
&B
) const override
{
1986 return InstructionPredicateMatcher::isIdentical(B
) &&
1988 static_cast<const GenericInstructionPredicateMatcher
&>(B
)
1991 void emitPredicateOpcodes(MatchTable
&Table
,
1992 RuleMatcher
&Rule
) const override
{
1993 Table
<< MatchTable::Opcode("GIM_CheckCxxInsnPredicate")
1994 << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID
)
1995 << MatchTable::Comment("FnId")
1996 << MatchTable::NamedValue(getEnumNameForPredicate(Predicate
))
1997 << MatchTable::LineBreak
;
2001 /// Generates code to check that a set of predicates and operands match for a
2002 /// particular instruction.
2004 /// Typical predicates include:
2005 /// * Has a specific opcode.
2006 /// * Has an nsw/nuw flag or doesn't.
2007 class InstructionMatcher final
: public PredicateListMatcher
<PredicateMatcher
> {
2009 typedef std::vector
<std::unique_ptr
<OperandMatcher
>> OperandVec
;
2013 /// The operands to match. All rendered operands must be present even if the
2014 /// condition is always true.
2015 OperandVec Operands
;
2016 bool NumOperandsCheck
= true;
2018 std::string SymbolicName
;
2022 InstructionMatcher(RuleMatcher
&Rule
, StringRef SymbolicName
)
2023 : Rule(Rule
), SymbolicName(SymbolicName
) {
2024 // We create a new instruction matcher.
2025 // Get a new ID for that instruction.
2026 InsnVarID
= Rule
.implicitlyDefineInsnVar(*this);
2029 /// Construct a new instruction predicate and add it to the matcher.
2030 template <class Kind
, class... Args
>
2031 Optional
<Kind
*> addPredicate(Args
&&... args
) {
2032 Predicates
.emplace_back(
2033 std::make_unique
<Kind
>(getInsnVarID(), std::forward
<Args
>(args
)...));
2034 return static_cast<Kind
*>(Predicates
.back().get());
2037 RuleMatcher
&getRuleMatcher() const { return Rule
; }
2039 unsigned getInsnVarID() const { return InsnVarID
; }
2041 /// Add an operand to the matcher.
2042 OperandMatcher
&addOperand(unsigned OpIdx
, const std::string
&SymbolicName
,
2043 unsigned AllocatedTemporariesBaseID
) {
2044 Operands
.emplace_back(new OperandMatcher(*this, OpIdx
, SymbolicName
,
2045 AllocatedTemporariesBaseID
));
2046 if (!SymbolicName
.empty())
2047 Rule
.defineOperand(SymbolicName
, *Operands
.back());
2049 return *Operands
.back();
2052 OperandMatcher
&getOperand(unsigned OpIdx
) {
2053 auto I
= std::find_if(Operands
.begin(), Operands
.end(),
2054 [&OpIdx
](const std::unique_ptr
<OperandMatcher
> &X
) {
2055 return X
->getOpIdx() == OpIdx
;
2057 if (I
!= Operands
.end())
2059 llvm_unreachable("Failed to lookup operand");
2062 StringRef
getSymbolicName() const { return SymbolicName
; }
2063 unsigned getNumOperands() const { return Operands
.size(); }
2064 OperandVec::iterator
operands_begin() { return Operands
.begin(); }
2065 OperandVec::iterator
operands_end() { return Operands
.end(); }
2066 iterator_range
<OperandVec::iterator
> operands() {
2067 return make_range(operands_begin(), operands_end());
2069 OperandVec::const_iterator
operands_begin() const { return Operands
.begin(); }
2070 OperandVec::const_iterator
operands_end() const { return Operands
.end(); }
2071 iterator_range
<OperandVec::const_iterator
> operands() const {
2072 return make_range(operands_begin(), operands_end());
2074 bool operands_empty() const { return Operands
.empty(); }
2076 void pop_front() { Operands
.erase(Operands
.begin()); }
2080 /// Emit MatchTable opcodes that test whether the instruction named in
2081 /// InsnVarName matches all the predicates and all the operands.
2082 void emitPredicateOpcodes(MatchTable
&Table
, RuleMatcher
&Rule
) {
2083 if (NumOperandsCheck
)
2084 InstructionNumOperandsMatcher(InsnVarID
, getNumOperands())
2085 .emitPredicateOpcodes(Table
, Rule
);
2087 emitPredicateListOpcodes(Table
, Rule
);
2089 for (const auto &Operand
: Operands
)
2090 Operand
->emitPredicateOpcodes(Table
, Rule
);
2093 /// Compare the priority of this object and B.
2095 /// Returns true if this object is more important than B.
2096 bool isHigherPriorityThan(InstructionMatcher
&B
) {
2097 // Instruction matchers involving more operands have higher priority.
2098 if (Operands
.size() > B
.Operands
.size())
2100 if (Operands
.size() < B
.Operands
.size())
2103 for (auto &&P
: zip(predicates(), B
.predicates())) {
2104 auto L
= static_cast<InstructionPredicateMatcher
*>(std::get
<0>(P
).get());
2105 auto R
= static_cast<InstructionPredicateMatcher
*>(std::get
<1>(P
).get());
2106 if (L
->isHigherPriorityThan(*R
))
2108 if (R
->isHigherPriorityThan(*L
))
2112 for (const auto &Operand
: zip(Operands
, B
.Operands
)) {
2113 if (std::get
<0>(Operand
)->isHigherPriorityThan(*std::get
<1>(Operand
)))
2115 if (std::get
<1>(Operand
)->isHigherPriorityThan(*std::get
<0>(Operand
)))
2122 /// Report the maximum number of temporary operands needed by the instruction
2124 unsigned countRendererFns() {
2125 return std::accumulate(
2126 predicates().begin(), predicates().end(), 0,
2128 const std::unique_ptr
<PredicateMatcher
> &Predicate
) {
2129 return A
+ Predicate
->countRendererFns();
2132 Operands
.begin(), Operands
.end(), 0,
2133 [](unsigned A
, const std::unique_ptr
<OperandMatcher
> &Operand
) {
2134 return A
+ Operand
->countRendererFns();
2138 InstructionOpcodeMatcher
&getOpcodeMatcher() {
2139 for (auto &P
: predicates())
2140 if (auto *OpMatcher
= dyn_cast
<InstructionOpcodeMatcher
>(P
.get()))
2142 llvm_unreachable("Didn't find an opcode matcher");
2145 bool isConstantInstruction() {
2146 return getOpcodeMatcher().isConstantInstruction();
2149 StringRef
getOpcode() { return getOpcodeMatcher().getOpcode(); }
2152 StringRef
RuleMatcher::getOpcode() const {
2153 return Matchers
.front()->getOpcode();
2156 unsigned RuleMatcher::getNumOperands() const {
2157 return Matchers
.front()->getNumOperands();
2160 LLTCodeGen
RuleMatcher::getFirstConditionAsRootType() {
2161 InstructionMatcher
&InsnMatcher
= *Matchers
.front();
2162 if (!InsnMatcher
.predicates_empty())
2163 if (const auto *TM
=
2164 dyn_cast
<LLTOperandMatcher
>(&**InsnMatcher
.predicates_begin()))
2165 if (TM
->getInsnVarID() == 0 && TM
->getOpIdx() == 0)
2170 /// Generates code to check that the operand is a register defined by an
2171 /// instruction that matches the given instruction matcher.
2173 /// For example, the pattern:
2174 /// (set $dst, (G_MUL (G_ADD $src1, $src2), $src3))
2175 /// would use an InstructionOperandMatcher for operand 1 of the G_MUL to match
2177 /// (G_ADD $src1, $src2)
2179 class InstructionOperandMatcher
: public OperandPredicateMatcher
{
2181 std::unique_ptr
<InstructionMatcher
> InsnMatcher
;
2184 InstructionOperandMatcher(unsigned InsnVarID
, unsigned OpIdx
,
2185 RuleMatcher
&Rule
, StringRef SymbolicName
)
2186 : OperandPredicateMatcher(OPM_Instruction
, InsnVarID
, OpIdx
),
2187 InsnMatcher(new InstructionMatcher(Rule
, SymbolicName
)) {}
2189 static bool classof(const PredicateMatcher
*P
) {
2190 return P
->getKind() == OPM_Instruction
;
2193 InstructionMatcher
&getInsnMatcher() const { return *InsnMatcher
; }
2195 void emitCaptureOpcodes(MatchTable
&Table
, RuleMatcher
&Rule
) const {
2196 const unsigned NewInsnVarID
= InsnMatcher
->getInsnVarID();
2197 Table
<< MatchTable::Opcode("GIM_RecordInsn")
2198 << MatchTable::Comment("DefineMI")
2199 << MatchTable::IntValue(NewInsnVarID
) << MatchTable::Comment("MI")
2200 << MatchTable::IntValue(getInsnVarID())
2201 << MatchTable::Comment("OpIdx") << MatchTable::IntValue(getOpIdx())
2202 << MatchTable::Comment("MIs[" + llvm::to_string(NewInsnVarID
) + "]")
2203 << MatchTable::LineBreak
;
2206 void emitPredicateOpcodes(MatchTable
&Table
,
2207 RuleMatcher
&Rule
) const override
{
2208 emitCaptureOpcodes(Table
, Rule
);
2209 InsnMatcher
->emitPredicateOpcodes(Table
, Rule
);
2212 bool isHigherPriorityThan(const OperandPredicateMatcher
&B
) const override
{
2213 if (OperandPredicateMatcher::isHigherPriorityThan(B
))
2215 if (B
.OperandPredicateMatcher::isHigherPriorityThan(*this))
2218 if (const InstructionOperandMatcher
*BP
=
2219 dyn_cast
<InstructionOperandMatcher
>(&B
))
2220 if (InsnMatcher
->isHigherPriorityThan(*BP
->InsnMatcher
))
2226 void InstructionMatcher::optimize() {
2227 SmallVector
<std::unique_ptr
<PredicateMatcher
>, 8> Stash
;
2228 const auto &OpcMatcher
= getOpcodeMatcher();
2230 Stash
.push_back(predicates_pop_front());
2231 if (Stash
.back().get() == &OpcMatcher
) {
2232 if (NumOperandsCheck
&& OpcMatcher
.getNumOperands() < getNumOperands())
2234 new InstructionNumOperandsMatcher(InsnVarID
, getNumOperands()));
2235 NumOperandsCheck
= false;
2237 for (auto &OM
: Operands
)
2238 for (auto &OP
: OM
->predicates())
2239 if (isa
<IntrinsicIDOperandMatcher
>(OP
)) {
2240 Stash
.push_back(std::move(OP
));
2241 OM
->eraseNullPredicates();
2246 if (InsnVarID
> 0) {
2247 assert(!Operands
.empty() && "Nested instruction is expected to def a vreg");
2248 for (auto &OP
: Operands
[0]->predicates())
2250 Operands
[0]->eraseNullPredicates();
2252 for (auto &OM
: Operands
) {
2253 for (auto &OP
: OM
->predicates())
2254 if (isa
<LLTOperandMatcher
>(OP
))
2255 Stash
.push_back(std::move(OP
));
2256 OM
->eraseNullPredicates();
2258 while (!Stash
.empty())
2259 prependPredicate(Stash
.pop_back_val());
2262 //===- Actions ------------------------------------------------------------===//
2263 class OperandRenderer
{
2267 OR_CopyOrAddZeroReg
,
2269 OR_CopyConstantAsImm
,
2270 OR_CopyFConstantAsFPImm
,
2282 OperandRenderer(RendererKind Kind
) : Kind(Kind
) {}
2283 virtual ~OperandRenderer() {}
2285 RendererKind
getKind() const { return Kind
; }
2287 virtual void emitRenderOpcodes(MatchTable
&Table
,
2288 RuleMatcher
&Rule
) const = 0;
2291 /// A CopyRenderer emits code to copy a single operand from an existing
2292 /// instruction to the one being built.
2293 class CopyRenderer
: public OperandRenderer
{
2296 /// The name of the operand.
2297 const StringRef SymbolicName
;
2300 CopyRenderer(unsigned NewInsnID
, StringRef SymbolicName
)
2301 : OperandRenderer(OR_Copy
), NewInsnID(NewInsnID
),
2302 SymbolicName(SymbolicName
) {
2303 assert(!SymbolicName
.empty() && "Cannot copy from an unspecified source");
2306 static bool classof(const OperandRenderer
*R
) {
2307 return R
->getKind() == OR_Copy
;
2310 const StringRef
getSymbolicName() const { return SymbolicName
; }
2312 void emitRenderOpcodes(MatchTable
&Table
, RuleMatcher
&Rule
) const override
{
2313 const OperandMatcher
&Operand
= Rule
.getOperandMatcher(SymbolicName
);
2314 unsigned OldInsnVarID
= Rule
.getInsnVarID(Operand
.getInstructionMatcher());
2315 Table
<< MatchTable::Opcode("GIR_Copy") << MatchTable::Comment("NewInsnID")
2316 << MatchTable::IntValue(NewInsnID
) << MatchTable::Comment("OldInsnID")
2317 << MatchTable::IntValue(OldInsnVarID
) << MatchTable::Comment("OpIdx")
2318 << MatchTable::IntValue(Operand
.getOpIdx())
2319 << MatchTable::Comment(SymbolicName
) << MatchTable::LineBreak
;
2323 /// A CopyOrAddZeroRegRenderer emits code to copy a single operand from an
2324 /// existing instruction to the one being built. If the operand turns out to be
2325 /// a 'G_CONSTANT 0' then it replaces the operand with a zero register.
2326 class CopyOrAddZeroRegRenderer
: public OperandRenderer
{
2329 /// The name of the operand.
2330 const StringRef SymbolicName
;
2331 const Record
*ZeroRegisterDef
;
2334 CopyOrAddZeroRegRenderer(unsigned NewInsnID
,
2335 StringRef SymbolicName
, Record
*ZeroRegisterDef
)
2336 : OperandRenderer(OR_CopyOrAddZeroReg
), NewInsnID(NewInsnID
),
2337 SymbolicName(SymbolicName
), ZeroRegisterDef(ZeroRegisterDef
) {
2338 assert(!SymbolicName
.empty() && "Cannot copy from an unspecified source");
2341 static bool classof(const OperandRenderer
*R
) {
2342 return R
->getKind() == OR_CopyOrAddZeroReg
;
2345 const StringRef
getSymbolicName() const { return SymbolicName
; }
2347 void emitRenderOpcodes(MatchTable
&Table
, RuleMatcher
&Rule
) const override
{
2348 const OperandMatcher
&Operand
= Rule
.getOperandMatcher(SymbolicName
);
2349 unsigned OldInsnVarID
= Rule
.getInsnVarID(Operand
.getInstructionMatcher());
2350 Table
<< MatchTable::Opcode("GIR_CopyOrAddZeroReg")
2351 << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID
)
2352 << MatchTable::Comment("OldInsnID")
2353 << MatchTable::IntValue(OldInsnVarID
) << MatchTable::Comment("OpIdx")
2354 << MatchTable::IntValue(Operand
.getOpIdx())
2355 << MatchTable::NamedValue(
2356 (ZeroRegisterDef
->getValue("Namespace")
2357 ? ZeroRegisterDef
->getValueAsString("Namespace")
2359 ZeroRegisterDef
->getName())
2360 << MatchTable::Comment(SymbolicName
) << MatchTable::LineBreak
;
2364 /// A CopyConstantAsImmRenderer emits code to render a G_CONSTANT instruction to
2365 /// an extended immediate operand.
2366 class CopyConstantAsImmRenderer
: public OperandRenderer
{
2369 /// The name of the operand.
2370 const std::string SymbolicName
;
2374 CopyConstantAsImmRenderer(unsigned NewInsnID
, StringRef SymbolicName
)
2375 : OperandRenderer(OR_CopyConstantAsImm
), NewInsnID(NewInsnID
),
2376 SymbolicName(SymbolicName
), Signed(true) {}
2378 static bool classof(const OperandRenderer
*R
) {
2379 return R
->getKind() == OR_CopyConstantAsImm
;
2382 const StringRef
getSymbolicName() const { return SymbolicName
; }
2384 void emitRenderOpcodes(MatchTable
&Table
, RuleMatcher
&Rule
) const override
{
2385 InstructionMatcher
&InsnMatcher
= Rule
.getInstructionMatcher(SymbolicName
);
2386 unsigned OldInsnVarID
= Rule
.getInsnVarID(InsnMatcher
);
2387 Table
<< MatchTable::Opcode(Signed
? "GIR_CopyConstantAsSImm"
2388 : "GIR_CopyConstantAsUImm")
2389 << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID
)
2390 << MatchTable::Comment("OldInsnID")
2391 << MatchTable::IntValue(OldInsnVarID
)
2392 << MatchTable::Comment(SymbolicName
) << MatchTable::LineBreak
;
2396 /// A CopyFConstantAsFPImmRenderer emits code to render a G_FCONSTANT
2397 /// instruction to an extended immediate operand.
2398 class CopyFConstantAsFPImmRenderer
: public OperandRenderer
{
2401 /// The name of the operand.
2402 const std::string SymbolicName
;
2405 CopyFConstantAsFPImmRenderer(unsigned NewInsnID
, StringRef SymbolicName
)
2406 : OperandRenderer(OR_CopyFConstantAsFPImm
), NewInsnID(NewInsnID
),
2407 SymbolicName(SymbolicName
) {}
2409 static bool classof(const OperandRenderer
*R
) {
2410 return R
->getKind() == OR_CopyFConstantAsFPImm
;
2413 const StringRef
getSymbolicName() const { return SymbolicName
; }
2415 void emitRenderOpcodes(MatchTable
&Table
, RuleMatcher
&Rule
) const override
{
2416 InstructionMatcher
&InsnMatcher
= Rule
.getInstructionMatcher(SymbolicName
);
2417 unsigned OldInsnVarID
= Rule
.getInsnVarID(InsnMatcher
);
2418 Table
<< MatchTable::Opcode("GIR_CopyFConstantAsFPImm")
2419 << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID
)
2420 << MatchTable::Comment("OldInsnID")
2421 << MatchTable::IntValue(OldInsnVarID
)
2422 << MatchTable::Comment(SymbolicName
) << MatchTable::LineBreak
;
2426 /// A CopySubRegRenderer emits code to copy a single register operand from an
2427 /// existing instruction to the one being built and indicate that only a
2428 /// subregister should be copied.
2429 class CopySubRegRenderer
: public OperandRenderer
{
2432 /// The name of the operand.
2433 const StringRef SymbolicName
;
2434 /// The subregister to extract.
2435 const CodeGenSubRegIndex
*SubReg
;
2438 CopySubRegRenderer(unsigned NewInsnID
, StringRef SymbolicName
,
2439 const CodeGenSubRegIndex
*SubReg
)
2440 : OperandRenderer(OR_CopySubReg
), NewInsnID(NewInsnID
),
2441 SymbolicName(SymbolicName
), SubReg(SubReg
) {}
2443 static bool classof(const OperandRenderer
*R
) {
2444 return R
->getKind() == OR_CopySubReg
;
2447 const StringRef
getSymbolicName() const { return SymbolicName
; }
2449 void emitRenderOpcodes(MatchTable
&Table
, RuleMatcher
&Rule
) const override
{
2450 const OperandMatcher
&Operand
= Rule
.getOperandMatcher(SymbolicName
);
2451 unsigned OldInsnVarID
= Rule
.getInsnVarID(Operand
.getInstructionMatcher());
2452 Table
<< MatchTable::Opcode("GIR_CopySubReg")
2453 << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID
)
2454 << MatchTable::Comment("OldInsnID")
2455 << MatchTable::IntValue(OldInsnVarID
) << MatchTable::Comment("OpIdx")
2456 << MatchTable::IntValue(Operand
.getOpIdx())
2457 << MatchTable::Comment("SubRegIdx")
2458 << MatchTable::IntValue(SubReg
->EnumValue
)
2459 << MatchTable::Comment(SymbolicName
) << MatchTable::LineBreak
;
2463 /// Adds a specific physical register to the instruction being built.
2464 /// This is typically useful for WZR/XZR on AArch64.
2465 class AddRegisterRenderer
: public OperandRenderer
{
2468 const Record
*RegisterDef
;
2471 AddRegisterRenderer(unsigned InsnID
, const Record
*RegisterDef
)
2472 : OperandRenderer(OR_Register
), InsnID(InsnID
), RegisterDef(RegisterDef
) {
2475 static bool classof(const OperandRenderer
*R
) {
2476 return R
->getKind() == OR_Register
;
2479 void emitRenderOpcodes(MatchTable
&Table
, RuleMatcher
&Rule
) const override
{
2480 Table
<< MatchTable::Opcode("GIR_AddRegister")
2481 << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID
)
2482 << MatchTable::NamedValue(
2483 (RegisterDef
->getValue("Namespace")
2484 ? RegisterDef
->getValueAsString("Namespace")
2486 RegisterDef
->getName())
2487 << MatchTable::LineBreak
;
2491 /// Adds a specific temporary virtual register to the instruction being built.
2492 /// This is used to chain instructions together when emitting multiple
2494 class TempRegRenderer
: public OperandRenderer
{
2501 TempRegRenderer(unsigned InsnID
, unsigned TempRegID
, bool IsDef
= false)
2502 : OperandRenderer(OR_Register
), InsnID(InsnID
), TempRegID(TempRegID
),
2505 static bool classof(const OperandRenderer
*R
) {
2506 return R
->getKind() == OR_TempRegister
;
2509 void emitRenderOpcodes(MatchTable
&Table
, RuleMatcher
&Rule
) const override
{
2510 Table
<< MatchTable::Opcode("GIR_AddTempRegister")
2511 << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID
)
2512 << MatchTable::Comment("TempRegID") << MatchTable::IntValue(TempRegID
)
2513 << MatchTable::Comment("TempRegFlags");
2515 Table
<< MatchTable::NamedValue("RegState::Define");
2517 Table
<< MatchTable::IntValue(0);
2518 Table
<< MatchTable::LineBreak
;
2522 /// Adds a specific immediate to the instruction being built.
2523 class ImmRenderer
: public OperandRenderer
{
2529 ImmRenderer(unsigned InsnID
, int64_t Imm
)
2530 : OperandRenderer(OR_Imm
), InsnID(InsnID
), Imm(Imm
) {}
2532 static bool classof(const OperandRenderer
*R
) {
2533 return R
->getKind() == OR_Imm
;
2536 void emitRenderOpcodes(MatchTable
&Table
, RuleMatcher
&Rule
) const override
{
2537 Table
<< MatchTable::Opcode("GIR_AddImm") << MatchTable::Comment("InsnID")
2538 << MatchTable::IntValue(InsnID
) << MatchTable::Comment("Imm")
2539 << MatchTable::IntValue(Imm
) << MatchTable::LineBreak
;
2543 /// Adds operands by calling a renderer function supplied by the ComplexPattern
2544 /// matcher function.
2545 class RenderComplexPatternOperand
: public OperandRenderer
{
2548 const Record
&TheDef
;
2549 /// The name of the operand.
2550 const StringRef SymbolicName
;
2551 /// The renderer number. This must be unique within a rule since it's used to
2552 /// identify a temporary variable to hold the renderer function.
2553 unsigned RendererID
;
2554 /// When provided, this is the suboperand of the ComplexPattern operand to
2555 /// render. Otherwise all the suboperands will be rendered.
2556 Optional
<unsigned> SubOperand
;
2558 unsigned getNumOperands() const {
2559 return TheDef
.getValueAsDag("Operands")->getNumArgs();
2563 RenderComplexPatternOperand(unsigned InsnID
, const Record
&TheDef
,
2564 StringRef SymbolicName
, unsigned RendererID
,
2565 Optional
<unsigned> SubOperand
= None
)
2566 : OperandRenderer(OR_ComplexPattern
), InsnID(InsnID
), TheDef(TheDef
),
2567 SymbolicName(SymbolicName
), RendererID(RendererID
),
2568 SubOperand(SubOperand
) {}
2570 static bool classof(const OperandRenderer
*R
) {
2571 return R
->getKind() == OR_ComplexPattern
;
2574 void emitRenderOpcodes(MatchTable
&Table
, RuleMatcher
&Rule
) const override
{
2575 Table
<< MatchTable::Opcode(SubOperand
.hasValue() ? "GIR_ComplexSubOperandRenderer"
2576 : "GIR_ComplexRenderer")
2577 << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID
)
2578 << MatchTable::Comment("RendererID")
2579 << MatchTable::IntValue(RendererID
);
2580 if (SubOperand
.hasValue())
2581 Table
<< MatchTable::Comment("SubOperand")
2582 << MatchTable::IntValue(SubOperand
.getValue());
2583 Table
<< MatchTable::Comment(SymbolicName
) << MatchTable::LineBreak
;
2587 class CustomRenderer
: public OperandRenderer
{
2590 const Record
&Renderer
;
2591 /// The name of the operand.
2592 const std::string SymbolicName
;
2595 CustomRenderer(unsigned InsnID
, const Record
&Renderer
,
2596 StringRef SymbolicName
)
2597 : OperandRenderer(OR_Custom
), InsnID(InsnID
), Renderer(Renderer
),
2598 SymbolicName(SymbolicName
) {}
2600 static bool classof(const OperandRenderer
*R
) {
2601 return R
->getKind() == OR_Custom
;
2604 void emitRenderOpcodes(MatchTable
&Table
, RuleMatcher
&Rule
) const override
{
2605 InstructionMatcher
&InsnMatcher
= Rule
.getInstructionMatcher(SymbolicName
);
2606 unsigned OldInsnVarID
= Rule
.getInsnVarID(InsnMatcher
);
2607 Table
<< MatchTable::Opcode("GIR_CustomRenderer")
2608 << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID
)
2609 << MatchTable::Comment("OldInsnID")
2610 << MatchTable::IntValue(OldInsnVarID
)
2611 << MatchTable::Comment("Renderer")
2612 << MatchTable::NamedValue(
2613 "GICR_" + Renderer
.getValueAsString("RendererFn").str())
2614 << MatchTable::Comment(SymbolicName
) << MatchTable::LineBreak
;
2618 /// An action taken when all Matcher predicates succeeded for a parent rule.
2620 /// Typical actions include:
2621 /// * Changing the opcode of an instruction.
2622 /// * Adding an operand to an instruction.
2625 virtual ~MatchAction() {}
2627 /// Emit the MatchTable opcodes to implement the action.
2628 virtual void emitActionOpcodes(MatchTable
&Table
,
2629 RuleMatcher
&Rule
) const = 0;
2632 /// Generates a comment describing the matched rule being acted upon.
2633 class DebugCommentAction
: public MatchAction
{
2638 DebugCommentAction(StringRef S
) : S(S
) {}
2640 void emitActionOpcodes(MatchTable
&Table
, RuleMatcher
&Rule
) const override
{
2641 Table
<< MatchTable::Comment(S
) << MatchTable::LineBreak
;
2645 /// Generates code to build an instruction or mutate an existing instruction
2646 /// into the desired instruction when this is possible.
2647 class BuildMIAction
: public MatchAction
{
2650 const CodeGenInstruction
*I
;
2651 InstructionMatcher
*Matched
;
2652 std::vector
<std::unique_ptr
<OperandRenderer
>> OperandRenderers
;
2654 /// True if the instruction can be built solely by mutating the opcode.
2655 bool canMutate(RuleMatcher
&Rule
, const InstructionMatcher
*Insn
) const {
2659 if (OperandRenderers
.size() != Insn
->getNumOperands())
2662 for (const auto &Renderer
: enumerate(OperandRenderers
)) {
2663 if (const auto *Copy
= dyn_cast
<CopyRenderer
>(&*Renderer
.value())) {
2664 const OperandMatcher
&OM
= Rule
.getOperandMatcher(Copy
->getSymbolicName());
2665 if (Insn
!= &OM
.getInstructionMatcher() ||
2666 OM
.getOpIdx() != Renderer
.index())
2676 BuildMIAction(unsigned InsnID
, const CodeGenInstruction
*I
)
2677 : InsnID(InsnID
), I(I
), Matched(nullptr) {}
2679 unsigned getInsnID() const { return InsnID
; }
2680 const CodeGenInstruction
*getCGI() const { return I
; }
2682 void chooseInsnToMutate(RuleMatcher
&Rule
) {
2683 for (auto *MutateCandidate
: Rule
.mutatable_insns()) {
2684 if (canMutate(Rule
, MutateCandidate
)) {
2685 // Take the first one we're offered that we're able to mutate.
2686 Rule
.reserveInsnMatcherForMutation(MutateCandidate
);
2687 Matched
= MutateCandidate
;
2693 template <class Kind
, class... Args
>
2694 Kind
&addRenderer(Args
&&... args
) {
2695 OperandRenderers
.emplace_back(
2696 std::make_unique
<Kind
>(InsnID
, std::forward
<Args
>(args
)...));
2697 return *static_cast<Kind
*>(OperandRenderers
.back().get());
2700 void emitActionOpcodes(MatchTable
&Table
, RuleMatcher
&Rule
) const override
{
2702 assert(canMutate(Rule
, Matched
) &&
2703 "Arranged to mutate an insn that isn't mutatable");
2705 unsigned RecycleInsnID
= Rule
.getInsnVarID(*Matched
);
2706 Table
<< MatchTable::Opcode("GIR_MutateOpcode")
2707 << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID
)
2708 << MatchTable::Comment("RecycleInsnID")
2709 << MatchTable::IntValue(RecycleInsnID
)
2710 << MatchTable::Comment("Opcode")
2711 << MatchTable::NamedValue(I
->Namespace
, I
->TheDef
->getName())
2712 << MatchTable::LineBreak
;
2714 if (!I
->ImplicitDefs
.empty() || !I
->ImplicitUses
.empty()) {
2715 for (auto Def
: I
->ImplicitDefs
) {
2716 auto Namespace
= Def
->getValue("Namespace")
2717 ? Def
->getValueAsString("Namespace")
2719 Table
<< MatchTable::Opcode("GIR_AddImplicitDef")
2720 << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID
)
2721 << MatchTable::NamedValue(Namespace
, Def
->getName())
2722 << MatchTable::LineBreak
;
2724 for (auto Use
: I
->ImplicitUses
) {
2725 auto Namespace
= Use
->getValue("Namespace")
2726 ? Use
->getValueAsString("Namespace")
2728 Table
<< MatchTable::Opcode("GIR_AddImplicitUse")
2729 << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID
)
2730 << MatchTable::NamedValue(Namespace
, Use
->getName())
2731 << MatchTable::LineBreak
;
2737 // TODO: Simple permutation looks like it could be almost as common as
2738 // mutation due to commutative operations.
2740 Table
<< MatchTable::Opcode("GIR_BuildMI") << MatchTable::Comment("InsnID")
2741 << MatchTable::IntValue(InsnID
) << MatchTable::Comment("Opcode")
2742 << MatchTable::NamedValue(I
->Namespace
, I
->TheDef
->getName())
2743 << MatchTable::LineBreak
;
2744 for (const auto &Renderer
: OperandRenderers
)
2745 Renderer
->emitRenderOpcodes(Table
, Rule
);
2747 if (I
->mayLoad
|| I
->mayStore
) {
2748 Table
<< MatchTable::Opcode("GIR_MergeMemOperands")
2749 << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID
)
2750 << MatchTable::Comment("MergeInsnID's");
2751 // Emit the ID's for all the instructions that are matched by this rule.
2752 // TODO: Limit this to matched instructions that mayLoad/mayStore or have
2753 // some other means of having a memoperand. Also limit this to
2754 // emitted instructions that expect to have a memoperand too. For
2755 // example, (G_SEXT (G_LOAD x)) that results in separate load and
2756 // sign-extend instructions shouldn't put the memoperand on the
2757 // sign-extend since it has no effect there.
2758 std::vector
<unsigned> MergeInsnIDs
;
2759 for (const auto &IDMatcherPair
: Rule
.defined_insn_vars())
2760 MergeInsnIDs
.push_back(IDMatcherPair
.second
);
2761 llvm::sort(MergeInsnIDs
);
2762 for (const auto &MergeInsnID
: MergeInsnIDs
)
2763 Table
<< MatchTable::IntValue(MergeInsnID
);
2764 Table
<< MatchTable::NamedValue("GIU_MergeMemOperands_EndOfList")
2765 << MatchTable::LineBreak
;
2768 // FIXME: This is a hack but it's sufficient for ISel. We'll need to do
2769 // better for combines. Particularly when there are multiple match
2772 Table
<< MatchTable::Opcode("GIR_EraseFromParent")
2773 << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID
)
2774 << MatchTable::LineBreak
;
2778 /// Generates code to constrain the operands of an output instruction to the
2779 /// register classes specified by the definition of that instruction.
2780 class ConstrainOperandsToDefinitionAction
: public MatchAction
{
2784 ConstrainOperandsToDefinitionAction(unsigned InsnID
) : InsnID(InsnID
) {}
2786 void emitActionOpcodes(MatchTable
&Table
, RuleMatcher
&Rule
) const override
{
2787 Table
<< MatchTable::Opcode("GIR_ConstrainSelectedInstOperands")
2788 << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID
)
2789 << MatchTable::LineBreak
;
2793 /// Generates code to constrain the specified operand of an output instruction
2794 /// to the specified register class.
2795 class ConstrainOperandToRegClassAction
: public MatchAction
{
2798 const CodeGenRegisterClass
&RC
;
2801 ConstrainOperandToRegClassAction(unsigned InsnID
, unsigned OpIdx
,
2802 const CodeGenRegisterClass
&RC
)
2803 : InsnID(InsnID
), OpIdx(OpIdx
), RC(RC
) {}
2805 void emitActionOpcodes(MatchTable
&Table
, RuleMatcher
&Rule
) const override
{
2806 Table
<< MatchTable::Opcode("GIR_ConstrainOperandRC")
2807 << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID
)
2808 << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx
)
2809 << MatchTable::Comment("RC " + RC
.getName())
2810 << MatchTable::IntValue(RC
.EnumValue
) << MatchTable::LineBreak
;
2814 /// Generates code to create a temporary register which can be used to chain
2815 /// instructions together.
2816 class MakeTempRegisterAction
: public MatchAction
{
2822 MakeTempRegisterAction(const LLTCodeGen
&Ty
, unsigned TempRegID
)
2823 : Ty(Ty
), TempRegID(TempRegID
) {}
2825 void emitActionOpcodes(MatchTable
&Table
, RuleMatcher
&Rule
) const override
{
2826 Table
<< MatchTable::Opcode("GIR_MakeTempReg")
2827 << MatchTable::Comment("TempRegID") << MatchTable::IntValue(TempRegID
)
2828 << MatchTable::Comment("TypeID")
2829 << MatchTable::NamedValue(Ty
.getCxxEnumValue())
2830 << MatchTable::LineBreak
;
2834 InstructionMatcher
&RuleMatcher::addInstructionMatcher(StringRef SymbolicName
) {
2835 Matchers
.emplace_back(new InstructionMatcher(*this, SymbolicName
));
2836 MutatableInsns
.insert(Matchers
.back().get());
2837 return *Matchers
.back();
2840 void RuleMatcher::addRequiredFeature(Record
*Feature
) {
2841 RequiredFeatures
.push_back(Feature
);
2844 const std::vector
<Record
*> &RuleMatcher::getRequiredFeatures() const {
2845 return RequiredFeatures
;
2848 // Emplaces an action of the specified Kind at the end of the action list.
2850 // Returns a reference to the newly created action.
2852 // Like std::vector::emplace_back(), may invalidate all iterators if the new
2853 // size exceeds the capacity. Otherwise, only invalidates the past-the-end
2855 template <class Kind
, class... Args
>
2856 Kind
&RuleMatcher::addAction(Args
&&... args
) {
2857 Actions
.emplace_back(std::make_unique
<Kind
>(std::forward
<Args
>(args
)...));
2858 return *static_cast<Kind
*>(Actions
.back().get());
2861 // Emplaces an action of the specified Kind before the given insertion point.
2863 // Returns an iterator pointing at the newly created instruction.
2865 // Like std::vector::insert(), may invalidate all iterators if the new size
2866 // exceeds the capacity. Otherwise, only invalidates the iterators from the
2867 // insertion point onwards.
2868 template <class Kind
, class... Args
>
2869 action_iterator
RuleMatcher::insertAction(action_iterator InsertPt
,
2871 return Actions
.emplace(InsertPt
,
2872 std::make_unique
<Kind
>(std::forward
<Args
>(args
)...));
2875 unsigned RuleMatcher::implicitlyDefineInsnVar(InstructionMatcher
&Matcher
) {
2876 unsigned NewInsnVarID
= NextInsnVarID
++;
2877 InsnVariableIDs
[&Matcher
] = NewInsnVarID
;
2878 return NewInsnVarID
;
2881 unsigned RuleMatcher::getInsnVarID(InstructionMatcher
&InsnMatcher
) const {
2882 const auto &I
= InsnVariableIDs
.find(&InsnMatcher
);
2883 if (I
!= InsnVariableIDs
.end())
2885 llvm_unreachable("Matched Insn was not captured in a local variable");
2888 void RuleMatcher::defineOperand(StringRef SymbolicName
, OperandMatcher
&OM
) {
2889 if (DefinedOperands
.find(SymbolicName
) == DefinedOperands
.end()) {
2890 DefinedOperands
[SymbolicName
] = &OM
;
2894 // If the operand is already defined, then we must ensure both references in
2895 // the matcher have the exact same node.
2896 OM
.addPredicate
<SameOperandMatcher
>(OM
.getSymbolicName());
2899 InstructionMatcher
&
2900 RuleMatcher::getInstructionMatcher(StringRef SymbolicName
) const {
2901 for (const auto &I
: InsnVariableIDs
)
2902 if (I
.first
->getSymbolicName() == SymbolicName
)
2905 ("Failed to lookup instruction " + SymbolicName
).str().c_str());
2908 const OperandMatcher
&
2909 RuleMatcher::getOperandMatcher(StringRef Name
) const {
2910 const auto &I
= DefinedOperands
.find(Name
);
2912 if (I
== DefinedOperands
.end())
2913 PrintFatalError(SrcLoc
, "Operand " + Name
+ " was not declared in matcher");
2918 void RuleMatcher::emit(MatchTable
&Table
) {
2919 if (Matchers
.empty())
2920 llvm_unreachable("Unexpected empty matcher!");
2922 // The representation supports rules that require multiple roots such as:
2924 // %elt0(s32) = G_LOAD %ptr
2925 // %1(p0) = G_ADD %ptr, 4
2926 // %elt1(s32) = G_LOAD p0 %1
2927 // which could be usefully folded into:
2929 // %elt0(s32), %elt1(s32) = TGT_LOAD_PAIR %ptr
2930 // on some targets but we don't need to make use of that yet.
2931 assert(Matchers
.size() == 1 && "Cannot handle multi-root matchers yet");
2933 unsigned LabelID
= Table
.allocateLabelID();
2934 Table
<< MatchTable::Opcode("GIM_Try", +1)
2935 << MatchTable::Comment("On fail goto")
2936 << MatchTable::JumpTarget(LabelID
)
2937 << MatchTable::Comment(("Rule ID " + Twine(RuleID
) + " //").str())
2938 << MatchTable::LineBreak
;
2940 if (!RequiredFeatures
.empty()) {
2941 Table
<< MatchTable::Opcode("GIM_CheckFeatures")
2942 << MatchTable::NamedValue(getNameForFeatureBitset(RequiredFeatures
))
2943 << MatchTable::LineBreak
;
2946 Matchers
.front()->emitPredicateOpcodes(Table
, *this);
2948 // We must also check if it's safe to fold the matched instructions.
2949 if (InsnVariableIDs
.size() >= 2) {
2950 // Invert the map to create stable ordering (by var names)
2951 SmallVector
<unsigned, 2> InsnIDs
;
2952 for (const auto &Pair
: InsnVariableIDs
) {
2953 // Skip the root node since it isn't moving anywhere. Everything else is
2954 // sinking to meet it.
2955 if (Pair
.first
== Matchers
.front().get())
2958 InsnIDs
.push_back(Pair
.second
);
2960 llvm::sort(InsnIDs
);
2962 for (const auto &InsnID
: InsnIDs
) {
2963 // Reject the difficult cases until we have a more accurate check.
2964 Table
<< MatchTable::Opcode("GIM_CheckIsSafeToFold")
2965 << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID
)
2966 << MatchTable::LineBreak
;
2968 // FIXME: Emit checks to determine it's _actually_ safe to fold and/or
2969 // account for unsafe cases.
2974 // MI0--> %2 = ... %0
2975 // It's not safe to erase MI1. We currently handle this by not
2976 // erasing %0 (even when it's dead).
2979 // MI1--> %0 = load volatile @a
2980 // %1 = load volatile @a
2981 // MI0--> %2 = ... %0
2982 // It's not safe to sink %0's def past %1. We currently handle
2983 // this by rejecting all loads.
2986 // MI1--> %0 = load @a
2988 // MI0--> %2 = ... %0
2989 // It's not safe to sink %0's def past %1. We currently handle
2990 // this by rejecting all loads.
2993 // G_CONDBR %cond, @BB1
2995 // MI1--> %0 = load @a
2998 // MI0--> %2 = ... %0
2999 // It's not always safe to sink %0 across control flow. In this
3000 // case it may introduce a memory fault. We currentl handle this
3001 // by rejecting all loads.
3005 for (const auto &PM
: EpilogueMatchers
)
3006 PM
->emitPredicateOpcodes(Table
, *this);
3008 for (const auto &MA
: Actions
)
3009 MA
->emitActionOpcodes(Table
, *this);
3011 if (Table
.isWithCoverage())
3012 Table
<< MatchTable::Opcode("GIR_Coverage") << MatchTable::IntValue(RuleID
)
3013 << MatchTable::LineBreak
;
3015 Table
<< MatchTable::Comment(("GIR_Coverage, " + Twine(RuleID
) + ",").str())
3016 << MatchTable::LineBreak
;
3018 Table
<< MatchTable::Opcode("GIR_Done", -1) << MatchTable::LineBreak
3019 << MatchTable::Label(LabelID
);
3020 ++NumPatternEmitted
;
3023 bool RuleMatcher::isHigherPriorityThan(const RuleMatcher
&B
) const {
3024 // Rules involving more match roots have higher priority.
3025 if (Matchers
.size() > B
.Matchers
.size())
3027 if (Matchers
.size() < B
.Matchers
.size())
3030 for (const auto &Matcher
: zip(Matchers
, B
.Matchers
)) {
3031 if (std::get
<0>(Matcher
)->isHigherPriorityThan(*std::get
<1>(Matcher
)))
3033 if (std::get
<1>(Matcher
)->isHigherPriorityThan(*std::get
<0>(Matcher
)))
3040 unsigned RuleMatcher::countRendererFns() const {
3041 return std::accumulate(
3042 Matchers
.begin(), Matchers
.end(), 0,
3043 [](unsigned A
, const std::unique_ptr
<InstructionMatcher
> &Matcher
) {
3044 return A
+ Matcher
->countRendererFns();
3048 bool OperandPredicateMatcher::isHigherPriorityThan(
3049 const OperandPredicateMatcher
&B
) const {
3050 // Generally speaking, an instruction is more important than an Int or a
3051 // LiteralInt because it can cover more nodes but theres an exception to
3052 // this. G_CONSTANT's are less important than either of those two because they
3053 // are more permissive.
3055 const InstructionOperandMatcher
*AOM
=
3056 dyn_cast
<InstructionOperandMatcher
>(this);
3057 const InstructionOperandMatcher
*BOM
=
3058 dyn_cast
<InstructionOperandMatcher
>(&B
);
3059 bool AIsConstantInsn
= AOM
&& AOM
->getInsnMatcher().isConstantInstruction();
3060 bool BIsConstantInsn
= BOM
&& BOM
->getInsnMatcher().isConstantInstruction();
3063 // The relative priorities between a G_CONSTANT and any other instruction
3064 // don't actually matter but this code is needed to ensure a strict weak
3065 // ordering. This is particularly important on Windows where the rules will
3066 // be incorrectly sorted without it.
3067 if (AIsConstantInsn
!= BIsConstantInsn
)
3068 return AIsConstantInsn
< BIsConstantInsn
;
3072 if (AOM
&& AIsConstantInsn
&& (B
.Kind
== OPM_Int
|| B
.Kind
== OPM_LiteralInt
))
3074 if (BOM
&& BIsConstantInsn
&& (Kind
== OPM_Int
|| Kind
== OPM_LiteralInt
))
3077 return Kind
< B
.Kind
;
3080 void SameOperandMatcher::emitPredicateOpcodes(MatchTable
&Table
,
3081 RuleMatcher
&Rule
) const {
3082 const OperandMatcher
&OtherOM
= Rule
.getOperandMatcher(MatchingName
);
3083 unsigned OtherInsnVarID
= Rule
.getInsnVarID(OtherOM
.getInstructionMatcher());
3084 assert(OtherInsnVarID
== OtherOM
.getInstructionMatcher().getInsnVarID());
3086 Table
<< MatchTable::Opcode("GIM_CheckIsSameOperand")
3087 << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID
)
3088 << MatchTable::Comment("OpIdx") << MatchTable::IntValue(OpIdx
)
3089 << MatchTable::Comment("OtherMI")
3090 << MatchTable::IntValue(OtherInsnVarID
)
3091 << MatchTable::Comment("OtherOpIdx")
3092 << MatchTable::IntValue(OtherOM
.getOpIdx())
3093 << MatchTable::LineBreak
;
3096 //===- GlobalISelEmitter class --------------------------------------------===//
3098 class GlobalISelEmitter
{
3100 explicit GlobalISelEmitter(RecordKeeper
&RK
);
3101 void run(raw_ostream
&OS
);
3104 const RecordKeeper
&RK
;
3105 const CodeGenDAGPatterns CGP
;
3106 const CodeGenTarget
&Target
;
3107 CodeGenRegBank CGRegs
;
3109 /// Keep track of the equivalence between SDNodes and Instruction by mapping
3110 /// SDNodes to the GINodeEquiv mapping. We need to map to the GINodeEquiv to
3111 /// check for attributes on the relation such as CheckMMOIsNonAtomic.
3112 /// This is defined using 'GINodeEquiv' in the target description.
3113 DenseMap
<Record
*, Record
*> NodeEquivs
;
3115 /// Keep track of the equivalence between ComplexPattern's and
3116 /// GIComplexOperandMatcher. Map entries are specified by subclassing
3117 /// GIComplexPatternEquiv.
3118 DenseMap
<const Record
*, const Record
*> ComplexPatternEquivs
;
3120 /// Keep track of the equivalence between SDNodeXForm's and
3121 /// GICustomOperandRenderer. Map entries are specified by subclassing
3122 /// GISDNodeXFormEquiv.
3123 DenseMap
<const Record
*, const Record
*> SDNodeXFormEquivs
;
3125 /// Keep track of Scores of PatternsToMatch similar to how the DAG does.
3126 /// This adds compatibility for RuleMatchers to use this for ordering rules.
3127 DenseMap
<uint64_t, int> RuleMatcherScores
;
3129 // Map of predicates to their subtarget features.
3130 SubtargetFeatureInfoMap SubtargetFeatures
;
3132 // Rule coverage information.
3133 Optional
<CodeGenCoverage
> RuleCoverage
;
3135 void gatherOpcodeValues();
3136 void gatherTypeIDValues();
3137 void gatherNodeEquivs();
3139 Record
*findNodeEquiv(Record
*N
) const;
3140 const CodeGenInstruction
*getEquivNode(Record
&Equiv
,
3141 const TreePatternNode
*N
) const;
3143 Error
importRulePredicates(RuleMatcher
&M
, ArrayRef
<Predicate
> Predicates
);
3144 Expected
<InstructionMatcher
&>
3145 createAndImportSelDAGMatcher(RuleMatcher
&Rule
,
3146 InstructionMatcher
&InsnMatcher
,
3147 const TreePatternNode
*Src
, unsigned &TempOpIdx
);
3148 Error
importComplexPatternOperandMatcher(OperandMatcher
&OM
, Record
*R
,
3149 unsigned &TempOpIdx
) const;
3150 Error
importChildMatcher(RuleMatcher
&Rule
, InstructionMatcher
&InsnMatcher
,
3151 const TreePatternNode
*SrcChild
,
3152 bool OperandIsAPointer
, unsigned OpIdx
,
3153 unsigned &TempOpIdx
);
3155 Expected
<BuildMIAction
&>
3156 createAndImportInstructionRenderer(RuleMatcher
&M
,
3157 const TreePatternNode
*Dst
);
3158 Expected
<action_iterator
> createAndImportSubInstructionRenderer(
3159 action_iterator InsertPt
, RuleMatcher
&M
, const TreePatternNode
*Dst
,
3161 Expected
<action_iterator
>
3162 createInstructionRenderer(action_iterator InsertPt
, RuleMatcher
&M
,
3163 const TreePatternNode
*Dst
);
3164 void importExplicitDefRenderers(BuildMIAction
&DstMIBuilder
);
3165 Expected
<action_iterator
>
3166 importExplicitUseRenderers(action_iterator InsertPt
, RuleMatcher
&M
,
3167 BuildMIAction
&DstMIBuilder
,
3168 const llvm::TreePatternNode
*Dst
);
3169 Expected
<action_iterator
>
3170 importExplicitUseRenderer(action_iterator InsertPt
, RuleMatcher
&Rule
,
3171 BuildMIAction
&DstMIBuilder
,
3172 TreePatternNode
*DstChild
);
3173 Error
importDefaultOperandRenderers(action_iterator InsertPt
, RuleMatcher
&M
,
3174 BuildMIAction
&DstMIBuilder
,
3175 DagInit
*DefaultOps
) const;
3177 importImplicitDefRenderers(BuildMIAction
&DstMIBuilder
,
3178 const std::vector
<Record
*> &ImplicitDefs
) const;
3180 void emitCxxPredicateFns(raw_ostream
&OS
, StringRef CodeFieldName
,
3181 StringRef TypeIdentifier
, StringRef ArgType
,
3182 StringRef ArgName
, StringRef AdditionalDeclarations
,
3183 std::function
<bool(const Record
*R
)> Filter
);
3184 void emitImmPredicateFns(raw_ostream
&OS
, StringRef TypeIdentifier
,
3186 std::function
<bool(const Record
*R
)> Filter
);
3187 void emitMIPredicateFns(raw_ostream
&OS
);
3189 /// Analyze pattern \p P, returning a matcher for it if possible.
3190 /// Otherwise, return an Error explaining why we don't support it.
3191 Expected
<RuleMatcher
> runOnPattern(const PatternToMatch
&P
);
3193 void declareSubtargetFeature(Record
*Predicate
);
3195 MatchTable
buildMatchTable(MutableArrayRef
<RuleMatcher
> Rules
, bool Optimize
,
3198 /// Infer a CodeGenRegisterClass for the type of \p SuperRegNode. The returned
3199 /// CodeGenRegisterClass will support the CodeGenRegisterClass of
3200 /// \p SubRegNode, and the subregister index defined by \p SubRegIdxNode.
3201 /// If no register class is found, return None.
3202 Optional
<const CodeGenRegisterClass
*>
3203 inferSuperRegisterClassForNode(const TypeSetByHwMode
&Ty
,
3204 TreePatternNode
*SuperRegNode
,
3205 TreePatternNode
*SubRegIdxNode
);
3207 /// Infer a CodeGenRegisterClass which suppoorts \p Ty and \p SubRegIdxNode.
3208 /// Return None if no such class exists.
3209 Optional
<const CodeGenRegisterClass
*>
3210 inferSuperRegisterClass(const TypeSetByHwMode
&Ty
,
3211 TreePatternNode
*SubRegIdxNode
);
3213 /// Return the CodeGenRegisterClass associated with \p Leaf if it has one.
3214 Optional
<const CodeGenRegisterClass
*>
3215 getRegClassFromLeaf(TreePatternNode
*Leaf
);
3217 /// Return a CodeGenRegisterClass for \p N if one can be found. Return None
3219 Optional
<const CodeGenRegisterClass
*>
3220 inferRegClassFromPattern(TreePatternNode
*N
);
3223 /// Takes a sequence of \p Rules and group them based on the predicates
3224 /// they share. \p MatcherStorage is used as a memory container
3225 /// for the group that are created as part of this process.
3227 /// What this optimization does looks like if GroupT = GroupMatcher:
3228 /// Output without optimization:
3235 /// # predicate A // <-- effectively this is going to be checked twice.
3236 /// // Once in R1 and once in R2.
3239 /// Output with optimization:
3242 /// # predicate A // <-- Check is now shared.
3248 template <class GroupT
>
3249 static std::vector
<Matcher
*> optimizeRules(
3250 ArrayRef
<Matcher
*> Rules
,
3251 std::vector
<std::unique_ptr
<Matcher
>> &MatcherStorage
);
3254 void GlobalISelEmitter::gatherOpcodeValues() {
3255 InstructionOpcodeMatcher::initOpcodeValuesMap(Target
);
3258 void GlobalISelEmitter::gatherTypeIDValues() {
3259 LLTOperandMatcher::initTypeIDValuesMap();
3262 void GlobalISelEmitter::gatherNodeEquivs() {
3263 assert(NodeEquivs
.empty());
3264 for (Record
*Equiv
: RK
.getAllDerivedDefinitions("GINodeEquiv"))
3265 NodeEquivs
[Equiv
->getValueAsDef("Node")] = Equiv
;
3267 assert(ComplexPatternEquivs
.empty());
3268 for (Record
*Equiv
: RK
.getAllDerivedDefinitions("GIComplexPatternEquiv")) {
3269 Record
*SelDAGEquiv
= Equiv
->getValueAsDef("SelDAGEquivalent");
3272 ComplexPatternEquivs
[SelDAGEquiv
] = Equiv
;
3275 assert(SDNodeXFormEquivs
.empty());
3276 for (Record
*Equiv
: RK
.getAllDerivedDefinitions("GISDNodeXFormEquiv")) {
3277 Record
*SelDAGEquiv
= Equiv
->getValueAsDef("SelDAGEquivalent");
3280 SDNodeXFormEquivs
[SelDAGEquiv
] = Equiv
;
3284 Record
*GlobalISelEmitter::findNodeEquiv(Record
*N
) const {
3285 return NodeEquivs
.lookup(N
);
3288 const CodeGenInstruction
*
3289 GlobalISelEmitter::getEquivNode(Record
&Equiv
, const TreePatternNode
*N
) const {
3290 if (N
->getNumChildren() >= 1) {
3291 // setcc operation maps to two different G_* instructions based on the type.
3292 if (!Equiv
.isValueUnset("IfFloatingPoint") &&
3293 MVT(N
->getChild(0)->getSimpleType(0)).isFloatingPoint())
3294 return &Target
.getInstruction(Equiv
.getValueAsDef("IfFloatingPoint"));
3297 for (const TreePredicateCall
&Call
: N
->getPredicateCalls()) {
3298 const TreePredicateFn
&Predicate
= Call
.Fn
;
3299 if (!Equiv
.isValueUnset("IfSignExtend") && Predicate
.isLoad() &&
3300 Predicate
.isSignExtLoad())
3301 return &Target
.getInstruction(Equiv
.getValueAsDef("IfSignExtend"));
3302 if (!Equiv
.isValueUnset("IfZeroExtend") && Predicate
.isLoad() &&
3303 Predicate
.isZeroExtLoad())
3304 return &Target
.getInstruction(Equiv
.getValueAsDef("IfZeroExtend"));
3307 return &Target
.getInstruction(Equiv
.getValueAsDef("I"));
3310 GlobalISelEmitter::GlobalISelEmitter(RecordKeeper
&RK
)
3311 : RK(RK
), CGP(RK
), Target(CGP
.getTargetInfo()),
3312 CGRegs(RK
, Target
.getHwModes()) {}
3314 //===- Emitter ------------------------------------------------------------===//
3317 GlobalISelEmitter::importRulePredicates(RuleMatcher
&M
,
3318 ArrayRef
<Predicate
> Predicates
) {
3319 for (const Predicate
&P
: Predicates
) {
3320 if (!P
.Def
|| P
.getCondString().empty())
3322 declareSubtargetFeature(P
.Def
);
3323 M
.addRequiredFeature(P
.Def
);
3326 return Error::success();
3329 Expected
<InstructionMatcher
&> GlobalISelEmitter::createAndImportSelDAGMatcher(
3330 RuleMatcher
&Rule
, InstructionMatcher
&InsnMatcher
,
3331 const TreePatternNode
*Src
, unsigned &TempOpIdx
) {
3332 Record
*SrcGIEquivOrNull
= nullptr;
3333 const CodeGenInstruction
*SrcGIOrNull
= nullptr;
3335 // Start with the defined operands (i.e., the results of the root operator).
3336 if (Src
->getExtTypes().size() > 1)
3337 return failedImport("Src pattern has multiple results");
3339 if (Src
->isLeaf()) {
3340 Init
*SrcInit
= Src
->getLeafValue();
3341 if (isa
<IntInit
>(SrcInit
)) {
3342 InsnMatcher
.addPredicate
<InstructionOpcodeMatcher
>(
3343 &Target
.getInstruction(RK
.getDef("G_CONSTANT")));
3345 return failedImport(
3346 "Unable to deduce gMIR opcode to handle Src (which is a leaf)");
3348 SrcGIEquivOrNull
= findNodeEquiv(Src
->getOperator());
3349 if (!SrcGIEquivOrNull
)
3350 return failedImport("Pattern operator lacks an equivalent Instruction" +
3351 explainOperator(Src
->getOperator()));
3352 SrcGIOrNull
= getEquivNode(*SrcGIEquivOrNull
, Src
);
3354 // The operators look good: match the opcode
3355 InsnMatcher
.addPredicate
<InstructionOpcodeMatcher
>(SrcGIOrNull
);
3359 for (const TypeSetByHwMode
&VTy
: Src
->getExtTypes()) {
3360 // Results don't have a name unless they are the root node. The caller will
3361 // set the name if appropriate.
3362 OperandMatcher
&OM
= InsnMatcher
.addOperand(OpIdx
++, "", TempOpIdx
);
3363 if (auto Error
= OM
.addTypeCheckPredicate(VTy
, false /* OperandIsAPointer */))
3364 return failedImport(toString(std::move(Error
)) +
3365 " for result of Src pattern operator");
3368 for (const TreePredicateCall
&Call
: Src
->getPredicateCalls()) {
3369 const TreePredicateFn
&Predicate
= Call
.Fn
;
3370 if (Predicate
.isAlwaysTrue())
3373 if (Predicate
.isImmediatePattern()) {
3374 InsnMatcher
.addPredicate
<InstructionImmPredicateMatcher
>(Predicate
);
3378 // An address space check is needed in all contexts if there is one.
3379 if (Predicate
.isLoad() || Predicate
.isStore() || Predicate
.isAtomic()) {
3380 if (const ListInit
*AddrSpaces
= Predicate
.getAddressSpaces()) {
3381 SmallVector
<unsigned, 4> ParsedAddrSpaces
;
3383 for (Init
*Val
: AddrSpaces
->getValues()) {
3384 IntInit
*IntVal
= dyn_cast
<IntInit
>(Val
);
3386 return failedImport("Address space is not an integer");
3387 ParsedAddrSpaces
.push_back(IntVal
->getValue());
3390 if (!ParsedAddrSpaces
.empty()) {
3391 InsnMatcher
.addPredicate
<MemoryAddressSpacePredicateMatcher
>(
3392 0, ParsedAddrSpaces
);
3396 int64_t MinAlign
= Predicate
.getMinAlignment();
3398 InsnMatcher
.addPredicate
<MemoryAlignmentPredicateMatcher
>(0, MinAlign
);
3401 // G_LOAD is used for both non-extending and any-extending loads.
3402 if (Predicate
.isLoad() && Predicate
.isNonExtLoad()) {
3403 InsnMatcher
.addPredicate
<MemoryVsLLTSizePredicateMatcher
>(
3404 0, MemoryVsLLTSizePredicateMatcher::EqualTo
, 0);
3407 if (Predicate
.isLoad() && Predicate
.isAnyExtLoad()) {
3408 InsnMatcher
.addPredicate
<MemoryVsLLTSizePredicateMatcher
>(
3409 0, MemoryVsLLTSizePredicateMatcher::LessThan
, 0);
3413 if (Predicate
.isStore()) {
3414 if (Predicate
.isTruncStore()) {
3415 // FIXME: If MemoryVT is set, we end up with 2 checks for the MMO size.
3416 InsnMatcher
.addPredicate
<MemoryVsLLTSizePredicateMatcher
>(
3417 0, MemoryVsLLTSizePredicateMatcher::LessThan
, 0);
3420 if (Predicate
.isNonTruncStore()) {
3421 // We need to check the sizes match here otherwise we could incorrectly
3422 // match truncating stores with non-truncating ones.
3423 InsnMatcher
.addPredicate
<MemoryVsLLTSizePredicateMatcher
>(
3424 0, MemoryVsLLTSizePredicateMatcher::EqualTo
, 0);
3428 // No check required. We already did it by swapping the opcode.
3429 if (!SrcGIEquivOrNull
->isValueUnset("IfSignExtend") &&
3430 Predicate
.isSignExtLoad())
3433 // No check required. We already did it by swapping the opcode.
3434 if (!SrcGIEquivOrNull
->isValueUnset("IfZeroExtend") &&
3435 Predicate
.isZeroExtLoad())
3438 // No check required. G_STORE by itself is a non-extending store.
3439 if (Predicate
.isNonTruncStore())
3442 if (Predicate
.isLoad() || Predicate
.isStore() || Predicate
.isAtomic()) {
3443 if (Predicate
.getMemoryVT() != nullptr) {
3444 Optional
<LLTCodeGen
> MemTyOrNone
=
3445 MVTToLLT(getValueType(Predicate
.getMemoryVT()));
3448 return failedImport("MemVT could not be converted to LLT");
3450 // MMO's work in bytes so we must take care of unusual types like i1
3451 // don't round down.
3452 unsigned MemSizeInBits
=
3453 llvm::alignTo(MemTyOrNone
->get().getSizeInBits(), 8);
3455 InsnMatcher
.addPredicate
<MemorySizePredicateMatcher
>(
3456 0, MemSizeInBits
/ 8);
3461 if (Predicate
.isLoad() || Predicate
.isStore()) {
3462 // No check required. A G_LOAD/G_STORE is an unindexed load.
3463 if (Predicate
.isUnindexed())
3467 if (Predicate
.isAtomic()) {
3468 if (Predicate
.isAtomicOrderingMonotonic()) {
3469 InsnMatcher
.addPredicate
<AtomicOrderingMMOPredicateMatcher
>(
3473 if (Predicate
.isAtomicOrderingAcquire()) {
3474 InsnMatcher
.addPredicate
<AtomicOrderingMMOPredicateMatcher
>("Acquire");
3477 if (Predicate
.isAtomicOrderingRelease()) {
3478 InsnMatcher
.addPredicate
<AtomicOrderingMMOPredicateMatcher
>("Release");
3481 if (Predicate
.isAtomicOrderingAcquireRelease()) {
3482 InsnMatcher
.addPredicate
<AtomicOrderingMMOPredicateMatcher
>(
3486 if (Predicate
.isAtomicOrderingSequentiallyConsistent()) {
3487 InsnMatcher
.addPredicate
<AtomicOrderingMMOPredicateMatcher
>(
3488 "SequentiallyConsistent");
3492 if (Predicate
.isAtomicOrderingAcquireOrStronger()) {
3493 InsnMatcher
.addPredicate
<AtomicOrderingMMOPredicateMatcher
>(
3494 "Acquire", AtomicOrderingMMOPredicateMatcher::AO_OrStronger
);
3497 if (Predicate
.isAtomicOrderingWeakerThanAcquire()) {
3498 InsnMatcher
.addPredicate
<AtomicOrderingMMOPredicateMatcher
>(
3499 "Acquire", AtomicOrderingMMOPredicateMatcher::AO_WeakerThan
);
3503 if (Predicate
.isAtomicOrderingReleaseOrStronger()) {
3504 InsnMatcher
.addPredicate
<AtomicOrderingMMOPredicateMatcher
>(
3505 "Release", AtomicOrderingMMOPredicateMatcher::AO_OrStronger
);
3508 if (Predicate
.isAtomicOrderingWeakerThanRelease()) {
3509 InsnMatcher
.addPredicate
<AtomicOrderingMMOPredicateMatcher
>(
3510 "Release", AtomicOrderingMMOPredicateMatcher::AO_WeakerThan
);
3515 if (Predicate
.hasGISelPredicateCode()) {
3516 InsnMatcher
.addPredicate
<GenericInstructionPredicateMatcher
>(Predicate
);
3520 return failedImport("Src pattern child has predicate (" +
3521 explainPredicates(Src
) + ")");
3523 if (SrcGIEquivOrNull
&& SrcGIEquivOrNull
->getValueAsBit("CheckMMOIsNonAtomic"))
3524 InsnMatcher
.addPredicate
<AtomicOrderingMMOPredicateMatcher
>("NotAtomic");
3526 if (Src
->isLeaf()) {
3527 Init
*SrcInit
= Src
->getLeafValue();
3528 if (IntInit
*SrcIntInit
= dyn_cast
<IntInit
>(SrcInit
)) {
3529 OperandMatcher
&OM
=
3530 InsnMatcher
.addOperand(OpIdx
++, Src
->getName(), TempOpIdx
);
3531 OM
.addPredicate
<LiteralIntOperandMatcher
>(SrcIntInit
->getValue());
3533 return failedImport(
3534 "Unable to deduce gMIR opcode to handle Src (which is a leaf)");
3536 assert(SrcGIOrNull
&&
3537 "Expected to have already found an equivalent Instruction");
3538 if (SrcGIOrNull
->TheDef
->getName() == "G_CONSTANT" ||
3539 SrcGIOrNull
->TheDef
->getName() == "G_FCONSTANT") {
3540 // imm/fpimm still have operands but we don't need to do anything with it
3541 // here since we don't support ImmLeaf predicates yet. However, we still
3542 // need to note the hidden operand to get GIM_CheckNumOperands correct.
3543 InsnMatcher
.addOperand(OpIdx
++, "", TempOpIdx
);
3547 // Special case because the operand order is changed from setcc. The
3548 // predicate operand needs to be swapped from the last operand to the first
3551 unsigned NumChildren
= Src
->getNumChildren();
3552 bool IsFCmp
= SrcGIOrNull
->TheDef
->getName() == "G_FCMP";
3554 if (IsFCmp
|| SrcGIOrNull
->TheDef
->getName() == "G_ICMP") {
3555 TreePatternNode
*SrcChild
= Src
->getChild(NumChildren
- 1);
3556 if (SrcChild
->isLeaf()) {
3557 DefInit
*DI
= dyn_cast
<DefInit
>(SrcChild
->getLeafValue());
3558 Record
*CCDef
= DI
? DI
->getDef() : nullptr;
3559 if (!CCDef
|| !CCDef
->isSubClassOf("CondCode"))
3560 return failedImport("Unable to handle CondCode");
3562 OperandMatcher
&OM
=
3563 InsnMatcher
.addOperand(OpIdx
++, SrcChild
->getName(), TempOpIdx
);
3564 StringRef PredType
= IsFCmp
? CCDef
->getValueAsString("FCmpPredicate") :
3565 CCDef
->getValueAsString("ICmpPredicate");
3567 if (!PredType
.empty()) {
3568 OM
.addPredicate
<CmpPredicateOperandMatcher
>(PredType
);
3569 // Process the other 2 operands normally.
3575 // Match the used operands (i.e. the children of the operator).
3577 SrcGIOrNull
->TheDef
->getName() == "G_INTRINSIC" ||
3578 SrcGIOrNull
->TheDef
->getName() == "G_INTRINSIC_W_SIDE_EFFECTS";
3579 const CodeGenIntrinsic
*II
= Src
->getIntrinsicInfo(CGP
);
3580 if (IsIntrinsic
&& !II
)
3581 return failedImport("Expected IntInit containing intrinsic ID)");
3583 for (unsigned i
= 0; i
!= NumChildren
; ++i
) {
3584 TreePatternNode
*SrcChild
= Src
->getChild(i
);
3586 // SelectionDAG allows pointers to be represented with iN since it doesn't
3587 // distinguish between pointers and integers but they are different types in GlobalISel.
3588 // Coerce integers to pointers to address space 0 if the context indicates a pointer.
3589 bool OperandIsAPointer
= SrcGIOrNull
->isOperandAPointer(i
);
3592 // For G_INTRINSIC/G_INTRINSIC_W_SIDE_EFFECTS, the operand immediately
3593 // following the defs is an intrinsic ID.
3595 OperandMatcher
&OM
=
3596 InsnMatcher
.addOperand(OpIdx
++, SrcChild
->getName(), TempOpIdx
);
3597 OM
.addPredicate
<IntrinsicIDOperandMatcher
>(II
);
3601 // We have to check intrinsics for llvm_anyptr_ty parameters.
3603 // Note that we have to look at the i-1th parameter, because we don't
3604 // have the intrinsic ID in the intrinsic's parameter list.
3605 OperandIsAPointer
|= II
->isParamAPointer(i
- 1);
3609 importChildMatcher(Rule
, InsnMatcher
, SrcChild
, OperandIsAPointer
,
3610 OpIdx
++, TempOpIdx
))
3611 return std::move(Error
);
3618 Error
GlobalISelEmitter::importComplexPatternOperandMatcher(
3619 OperandMatcher
&OM
, Record
*R
, unsigned &TempOpIdx
) const {
3620 const auto &ComplexPattern
= ComplexPatternEquivs
.find(R
);
3621 if (ComplexPattern
== ComplexPatternEquivs
.end())
3622 return failedImport("SelectionDAG ComplexPattern (" + R
->getName() +
3623 ") not mapped to GlobalISel");
3625 OM
.addPredicate
<ComplexPatternOperandMatcher
>(OM
, *ComplexPattern
->second
);
3627 return Error::success();
3630 Error
GlobalISelEmitter::importChildMatcher(RuleMatcher
&Rule
,
3631 InstructionMatcher
&InsnMatcher
,
3632 const TreePatternNode
*SrcChild
,
3633 bool OperandIsAPointer
,
3635 unsigned &TempOpIdx
) {
3636 OperandMatcher
&OM
=
3637 InsnMatcher
.addOperand(OpIdx
, SrcChild
->getName(), TempOpIdx
);
3638 if (OM
.isSameAsAnotherOperand())
3639 return Error::success();
3641 ArrayRef
<TypeSetByHwMode
> ChildTypes
= SrcChild
->getExtTypes();
3642 if (ChildTypes
.size() != 1)
3643 return failedImport("Src pattern child has multiple results");
3645 // Check MBB's before the type check since they are not a known type.
3646 if (!SrcChild
->isLeaf()) {
3647 if (SrcChild
->getOperator()->isSubClassOf("SDNode")) {
3648 auto &ChildSDNI
= CGP
.getSDNodeInfo(SrcChild
->getOperator());
3649 if (ChildSDNI
.getSDClassName() == "BasicBlockSDNode") {
3650 OM
.addPredicate
<MBBOperandMatcher
>();
3651 return Error::success();
3657 OM
.addTypeCheckPredicate(ChildTypes
.front(), OperandIsAPointer
))
3658 return failedImport(toString(std::move(Error
)) + " for Src operand (" +
3659 to_string(*SrcChild
) + ")");
3661 // Check for nested instructions.
3662 if (!SrcChild
->isLeaf()) {
3663 if (SrcChild
->getOperator()->isSubClassOf("ComplexPattern")) {
3664 // When a ComplexPattern is used as an operator, it should do the same
3665 // thing as when used as a leaf. However, the children of the operator
3666 // name the sub-operands that make up the complex operand and we must
3667 // prepare to reference them in the renderer too.
3668 unsigned RendererID
= TempOpIdx
;
3669 if (auto Error
= importComplexPatternOperandMatcher(
3670 OM
, SrcChild
->getOperator(), TempOpIdx
))
3673 for (unsigned i
= 0, e
= SrcChild
->getNumChildren(); i
!= e
; ++i
) {
3674 auto *SubOperand
= SrcChild
->getChild(i
);
3675 if (!SubOperand
->getName().empty()) {
3676 if (auto Error
= Rule
.defineComplexSubOperand(SubOperand
->getName(),
3677 SrcChild
->getOperator(),
3683 return Error::success();
3686 auto MaybeInsnOperand
= OM
.addPredicate
<InstructionOperandMatcher
>(
3687 InsnMatcher
.getRuleMatcher(), SrcChild
->getName());
3688 if (!MaybeInsnOperand
.hasValue()) {
3689 // This isn't strictly true. If the user were to provide exactly the same
3690 // matchers as the original operand then we could allow it. However, it's
3691 // simpler to not permit the redundant specification.
3692 return failedImport("Nested instruction cannot be the same as another operand");
3695 // Map the node to a gMIR instruction.
3696 InstructionOperandMatcher
&InsnOperand
= **MaybeInsnOperand
;
3697 auto InsnMatcherOrError
= createAndImportSelDAGMatcher(
3698 Rule
, InsnOperand
.getInsnMatcher(), SrcChild
, TempOpIdx
);
3699 if (auto Error
= InsnMatcherOrError
.takeError())
3702 return Error::success();
3705 if (SrcChild
->hasAnyPredicate())
3706 return failedImport("Src pattern child has unsupported predicate");
3708 // Check for constant immediates.
3709 if (auto *ChildInt
= dyn_cast
<IntInit
>(SrcChild
->getLeafValue())) {
3710 OM
.addPredicate
<ConstantIntOperandMatcher
>(ChildInt
->getValue());
3711 return Error::success();
3714 // Check for def's like register classes or ComplexPattern's.
3715 if (auto *ChildDefInit
= dyn_cast
<DefInit
>(SrcChild
->getLeafValue())) {
3716 auto *ChildRec
= ChildDefInit
->getDef();
3718 // Check for register classes.
3719 if (ChildRec
->isSubClassOf("RegisterClass") ||
3720 ChildRec
->isSubClassOf("RegisterOperand")) {
3721 OM
.addPredicate
<RegisterBankOperandMatcher
>(
3722 Target
.getRegisterClass(getInitValueAsRegClass(ChildDefInit
)));
3723 return Error::success();
3726 // Check for ValueType.
3727 if (ChildRec
->isSubClassOf("ValueType")) {
3728 // We already added a type check as standard practice so this doesn't need
3730 return Error::success();
3733 // Check for ComplexPattern's.
3734 if (ChildRec
->isSubClassOf("ComplexPattern"))
3735 return importComplexPatternOperandMatcher(OM
, ChildRec
, TempOpIdx
);
3737 if (ChildRec
->isSubClassOf("ImmLeaf")) {
3738 return failedImport(
3739 "Src pattern child def is an unsupported tablegen class (ImmLeaf)");
3742 return failedImport(
3743 "Src pattern child def is an unsupported tablegen class");
3746 return failedImport("Src pattern child is an unsupported kind");
3749 Expected
<action_iterator
> GlobalISelEmitter::importExplicitUseRenderer(
3750 action_iterator InsertPt
, RuleMatcher
&Rule
, BuildMIAction
&DstMIBuilder
,
3751 TreePatternNode
*DstChild
) {
3753 const auto &SubOperand
= Rule
.getComplexSubOperand(DstChild
->getName());
3754 if (SubOperand
.hasValue()) {
3755 DstMIBuilder
.addRenderer
<RenderComplexPatternOperand
>(
3756 *std::get
<0>(*SubOperand
), DstChild
->getName(),
3757 std::get
<1>(*SubOperand
), std::get
<2>(*SubOperand
));
3761 if (!DstChild
->isLeaf()) {
3763 if (DstChild
->getOperator()->isSubClassOf("SDNodeXForm")) {
3764 auto Child
= DstChild
->getChild(0);
3765 auto I
= SDNodeXFormEquivs
.find(DstChild
->getOperator());
3766 if (I
!= SDNodeXFormEquivs
.end()) {
3767 DstMIBuilder
.addRenderer
<CustomRenderer
>(*I
->second
, Child
->getName());
3770 return failedImport("SDNodeXForm " + Child
->getName() +
3771 " has no custom renderer");
3774 // We accept 'bb' here. It's an operator because BasicBlockSDNode isn't
3775 // inline, but in MI it's just another operand.
3776 if (DstChild
->getOperator()->isSubClassOf("SDNode")) {
3777 auto &ChildSDNI
= CGP
.getSDNodeInfo(DstChild
->getOperator());
3778 if (ChildSDNI
.getSDClassName() == "BasicBlockSDNode") {
3779 DstMIBuilder
.addRenderer
<CopyRenderer
>(DstChild
->getName());
3784 // Similarly, imm is an operator in TreePatternNode's view but must be
3785 // rendered as operands.
3786 // FIXME: The target should be able to choose sign-extended when appropriate
3788 if (DstChild
->getOperator()->getName() == "imm") {
3789 DstMIBuilder
.addRenderer
<CopyConstantAsImmRenderer
>(DstChild
->getName());
3791 } else if (DstChild
->getOperator()->getName() == "fpimm") {
3792 DstMIBuilder
.addRenderer
<CopyFConstantAsFPImmRenderer
>(
3793 DstChild
->getName());
3797 if (DstChild
->getOperator()->isSubClassOf("Instruction")) {
3798 ArrayRef
<TypeSetByHwMode
> ChildTypes
= DstChild
->getExtTypes();
3799 if (ChildTypes
.size() != 1)
3800 return failedImport("Dst pattern child has multiple results");
3802 Optional
<LLTCodeGen
> OpTyOrNone
= None
;
3803 if (ChildTypes
.front().isMachineValueType())
3805 MVTToLLT(ChildTypes
.front().getMachineValueType().SimpleTy
);
3807 return failedImport("Dst operand has an unsupported type");
3809 unsigned TempRegID
= Rule
.allocateTempRegID();
3810 InsertPt
= Rule
.insertAction
<MakeTempRegisterAction
>(
3811 InsertPt
, OpTyOrNone
.getValue(), TempRegID
);
3812 DstMIBuilder
.addRenderer
<TempRegRenderer
>(TempRegID
);
3814 auto InsertPtOrError
= createAndImportSubInstructionRenderer(
3815 ++InsertPt
, Rule
, DstChild
, TempRegID
);
3816 if (auto Error
= InsertPtOrError
.takeError())
3817 return std::move(Error
);
3818 return InsertPtOrError
.get();
3821 return failedImport("Dst pattern child isn't a leaf node or an MBB" + llvm::to_string(*DstChild
));
3824 // It could be a specific immediate in which case we should just check for
3826 if (const IntInit
*ChildIntInit
=
3827 dyn_cast
<IntInit
>(DstChild
->getLeafValue())) {
3828 DstMIBuilder
.addRenderer
<ImmRenderer
>(ChildIntInit
->getValue());
3832 // Otherwise, we're looking for a bog-standard RegisterClass operand.
3833 if (auto *ChildDefInit
= dyn_cast
<DefInit
>(DstChild
->getLeafValue())) {
3834 auto *ChildRec
= ChildDefInit
->getDef();
3836 ArrayRef
<TypeSetByHwMode
> ChildTypes
= DstChild
->getExtTypes();
3837 if (ChildTypes
.size() != 1)
3838 return failedImport("Dst pattern child has multiple results");
3840 Optional
<LLTCodeGen
> OpTyOrNone
= None
;
3841 if (ChildTypes
.front().isMachineValueType())
3842 OpTyOrNone
= MVTToLLT(ChildTypes
.front().getMachineValueType().SimpleTy
);
3844 return failedImport("Dst operand has an unsupported type");
3846 if (ChildRec
->isSubClassOf("Register")) {
3847 DstMIBuilder
.addRenderer
<AddRegisterRenderer
>(ChildRec
);
3851 if (ChildRec
->isSubClassOf("RegisterClass") ||
3852 ChildRec
->isSubClassOf("RegisterOperand") ||
3853 ChildRec
->isSubClassOf("ValueType")) {
3854 if (ChildRec
->isSubClassOf("RegisterOperand") &&
3855 !ChildRec
->isValueUnset("GIZeroRegister")) {
3856 DstMIBuilder
.addRenderer
<CopyOrAddZeroRegRenderer
>(
3857 DstChild
->getName(), ChildRec
->getValueAsDef("GIZeroRegister"));
3861 DstMIBuilder
.addRenderer
<CopyRenderer
>(DstChild
->getName());
3865 if (ChildRec
->isSubClassOf("SubRegIndex")) {
3866 CodeGenSubRegIndex
*SubIdx
= CGRegs
.getSubRegIdx(ChildRec
);
3867 DstMIBuilder
.addRenderer
<ImmRenderer
>(SubIdx
->EnumValue
);
3871 if (ChildRec
->isSubClassOf("ComplexPattern")) {
3872 const auto &ComplexPattern
= ComplexPatternEquivs
.find(ChildRec
);
3873 if (ComplexPattern
== ComplexPatternEquivs
.end())
3874 return failedImport(
3875 "SelectionDAG ComplexPattern not mapped to GlobalISel");
3877 const OperandMatcher
&OM
= Rule
.getOperandMatcher(DstChild
->getName());
3878 DstMIBuilder
.addRenderer
<RenderComplexPatternOperand
>(
3879 *ComplexPattern
->second
, DstChild
->getName(),
3880 OM
.getAllocatedTemporariesBaseID());
3884 return failedImport(
3885 "Dst pattern child def is an unsupported tablegen class");
3888 return failedImport("Dst pattern child is an unsupported kind");
3891 Expected
<BuildMIAction
&> GlobalISelEmitter::createAndImportInstructionRenderer(
3892 RuleMatcher
&M
, const TreePatternNode
*Dst
) {
3893 auto InsertPtOrError
= createInstructionRenderer(M
.actions_end(), M
, Dst
);
3894 if (auto Error
= InsertPtOrError
.takeError())
3895 return std::move(Error
);
3897 action_iterator InsertPt
= InsertPtOrError
.get();
3898 BuildMIAction
&DstMIBuilder
= *static_cast<BuildMIAction
*>(InsertPt
->get());
3900 importExplicitDefRenderers(DstMIBuilder
);
3902 if (auto Error
= importExplicitUseRenderers(InsertPt
, M
, DstMIBuilder
, Dst
)
3904 return std::move(Error
);
3906 return DstMIBuilder
;
3909 Expected
<action_iterator
>
3910 GlobalISelEmitter::createAndImportSubInstructionRenderer(
3911 const action_iterator InsertPt
, RuleMatcher
&M
, const TreePatternNode
*Dst
,
3912 unsigned TempRegID
) {
3913 auto InsertPtOrError
= createInstructionRenderer(InsertPt
, M
, Dst
);
3915 // TODO: Assert there's exactly one result.
3917 if (auto Error
= InsertPtOrError
.takeError())
3918 return std::move(Error
);
3920 BuildMIAction
&DstMIBuilder
=
3921 *static_cast<BuildMIAction
*>(InsertPtOrError
.get()->get());
3923 // Assign the result to TempReg.
3924 DstMIBuilder
.addRenderer
<TempRegRenderer
>(TempRegID
, true);
3927 importExplicitUseRenderers(InsertPtOrError
.get(), M
, DstMIBuilder
, Dst
);
3928 if (auto Error
= InsertPtOrError
.takeError())
3929 return std::move(Error
);
3931 // We need to make sure that when we import an INSERT_SUBREG as a
3932 // subinstruction that it ends up being constrained to the correct super
3933 // register and subregister classes.
3934 auto OpName
= Target
.getInstruction(Dst
->getOperator()).TheDef
->getName();
3935 if (OpName
== "INSERT_SUBREG") {
3936 auto SubClass
= inferRegClassFromPattern(Dst
->getChild(1));
3938 return failedImport(
3939 "Cannot infer register class from INSERT_SUBREG operand #1");
3940 Optional
<const CodeGenRegisterClass
*> SuperClass
=
3941 inferSuperRegisterClassForNode(Dst
->getExtType(0), Dst
->getChild(0),
3944 return failedImport(
3945 "Cannot infer register class for INSERT_SUBREG operand #0");
3946 // The destination and the super register source of an INSERT_SUBREG must
3947 // be the same register class.
3948 M
.insertAction
<ConstrainOperandToRegClassAction
>(
3949 InsertPt
, DstMIBuilder
.getInsnID(), 0, **SuperClass
);
3950 M
.insertAction
<ConstrainOperandToRegClassAction
>(
3951 InsertPt
, DstMIBuilder
.getInsnID(), 1, **SuperClass
);
3952 M
.insertAction
<ConstrainOperandToRegClassAction
>(
3953 InsertPt
, DstMIBuilder
.getInsnID(), 2, **SubClass
);
3954 return InsertPtOrError
.get();
3957 // Similar to INSERT_SUBREG, we also have to handle SUBREG_TO_REG as a
3959 if (OpName
== "SUBREG_TO_REG") {
3960 auto SubClass
= inferRegClassFromPattern(Dst
->getChild(1));
3962 return failedImport(
3963 "Cannot infer register class from SUBREG_TO_REG child #1");
3964 auto SuperClass
= inferSuperRegisterClass(Dst
->getExtType(0),
3967 return failedImport(
3968 "Cannot infer register class for SUBREG_TO_REG operand #0");
3969 M
.insertAction
<ConstrainOperandToRegClassAction
>(
3970 InsertPt
, DstMIBuilder
.getInsnID(), 0, **SuperClass
);
3971 M
.insertAction
<ConstrainOperandToRegClassAction
>(
3972 InsertPt
, DstMIBuilder
.getInsnID(), 2, **SubClass
);
3973 return InsertPtOrError
.get();
3976 M
.insertAction
<ConstrainOperandsToDefinitionAction
>(InsertPt
,
3977 DstMIBuilder
.getInsnID());
3978 return InsertPtOrError
.get();
3981 Expected
<action_iterator
> GlobalISelEmitter::createInstructionRenderer(
3982 action_iterator InsertPt
, RuleMatcher
&M
, const TreePatternNode
*Dst
) {
3983 Record
*DstOp
= Dst
->getOperator();
3984 if (!DstOp
->isSubClassOf("Instruction")) {
3985 if (DstOp
->isSubClassOf("ValueType"))
3986 return failedImport(
3987 "Pattern operator isn't an instruction (it's a ValueType)");
3988 return failedImport("Pattern operator isn't an instruction");
3990 CodeGenInstruction
*DstI
= &Target
.getInstruction(DstOp
);
3992 // COPY_TO_REGCLASS is just a copy with a ConstrainOperandToRegClassAction
3993 // attached. Similarly for EXTRACT_SUBREG except that's a subregister copy.
3994 if (DstI
->TheDef
->getName() == "COPY_TO_REGCLASS")
3995 DstI
= &Target
.getInstruction(RK
.getDef("COPY"));
3996 else if (DstI
->TheDef
->getName() == "EXTRACT_SUBREG")
3997 DstI
= &Target
.getInstruction(RK
.getDef("COPY"));
3998 else if (DstI
->TheDef
->getName() == "REG_SEQUENCE")
3999 return failedImport("Unable to emit REG_SEQUENCE");
4001 return M
.insertAction
<BuildMIAction
>(InsertPt
, M
.allocateOutputInsnID(),
4005 void GlobalISelEmitter::importExplicitDefRenderers(
4006 BuildMIAction
&DstMIBuilder
) {
4007 const CodeGenInstruction
*DstI
= DstMIBuilder
.getCGI();
4008 for (unsigned I
= 0; I
< DstI
->Operands
.NumDefs
; ++I
) {
4009 const CGIOperandList::OperandInfo
&DstIOperand
= DstI
->Operands
[I
];
4010 DstMIBuilder
.addRenderer
<CopyRenderer
>(DstIOperand
.Name
);
4014 Expected
<action_iterator
> GlobalISelEmitter::importExplicitUseRenderers(
4015 action_iterator InsertPt
, RuleMatcher
&M
, BuildMIAction
&DstMIBuilder
,
4016 const llvm::TreePatternNode
*Dst
) {
4017 const CodeGenInstruction
*DstI
= DstMIBuilder
.getCGI();
4018 CodeGenInstruction
*OrigDstI
= &Target
.getInstruction(Dst
->getOperator());
4020 // EXTRACT_SUBREG needs to use a subregister COPY.
4021 if (OrigDstI
->TheDef
->getName() == "EXTRACT_SUBREG") {
4022 if (!Dst
->getChild(0)->isLeaf())
4023 return failedImport("EXTRACT_SUBREG child #1 is not a leaf");
4025 if (DefInit
*SubRegInit
=
4026 dyn_cast
<DefInit
>(Dst
->getChild(1)->getLeafValue())) {
4027 Record
*RCDef
= getInitValueAsRegClass(Dst
->getChild(0)->getLeafValue());
4029 return failedImport("EXTRACT_SUBREG child #0 could not "
4030 "be coerced to a register class");
4032 CodeGenRegisterClass
*RC
= CGRegs
.getRegClass(RCDef
);
4033 CodeGenSubRegIndex
*SubIdx
= CGRegs
.getSubRegIdx(SubRegInit
->getDef());
4035 const auto &SrcRCDstRCPair
=
4036 RC
->getMatchingSubClassWithSubRegs(CGRegs
, SubIdx
);
4037 if (SrcRCDstRCPair
.hasValue()) {
4038 assert(SrcRCDstRCPair
->second
&& "Couldn't find a matching subclass");
4039 if (SrcRCDstRCPair
->first
!= RC
)
4040 return failedImport("EXTRACT_SUBREG requires an additional COPY");
4043 DstMIBuilder
.addRenderer
<CopySubRegRenderer
>(Dst
->getChild(0)->getName(),
4048 return failedImport("EXTRACT_SUBREG child #1 is not a subreg index");
4051 // Render the explicit uses.
4052 unsigned DstINumUses
= OrigDstI
->Operands
.size() - OrigDstI
->Operands
.NumDefs
;
4053 unsigned ExpectedDstINumUses
= Dst
->getNumChildren();
4054 if (OrigDstI
->TheDef
->getName() == "COPY_TO_REGCLASS") {
4055 DstINumUses
--; // Ignore the class constraint.
4056 ExpectedDstINumUses
--;
4060 unsigned NumDefaultOps
= 0;
4061 for (unsigned I
= 0; I
!= DstINumUses
; ++I
) {
4062 const CGIOperandList::OperandInfo
&DstIOperand
=
4063 DstI
->Operands
[DstI
->Operands
.NumDefs
+ I
];
4065 // If the operand has default values, introduce them now.
4066 // FIXME: Until we have a decent test case that dictates we should do
4067 // otherwise, we're going to assume that operands with default values cannot
4068 // be specified in the patterns. Therefore, adding them will not cause us to
4069 // end up with too many rendered operands.
4070 if (DstIOperand
.Rec
->isSubClassOf("OperandWithDefaultOps")) {
4071 DagInit
*DefaultOps
= DstIOperand
.Rec
->getValueAsDag("DefaultOps");
4072 if (auto Error
= importDefaultOperandRenderers(
4073 InsertPt
, M
, DstMIBuilder
, DefaultOps
))
4074 return std::move(Error
);
4079 auto InsertPtOrError
= importExplicitUseRenderer(InsertPt
, M
, DstMIBuilder
,
4080 Dst
->getChild(Child
));
4081 if (auto Error
= InsertPtOrError
.takeError())
4082 return std::move(Error
);
4083 InsertPt
= InsertPtOrError
.get();
4087 if (NumDefaultOps
+ ExpectedDstINumUses
!= DstINumUses
)
4088 return failedImport("Expected " + llvm::to_string(DstINumUses
) +
4089 " used operands but found " +
4090 llvm::to_string(ExpectedDstINumUses
) +
4091 " explicit ones and " + llvm::to_string(NumDefaultOps
) +
4097 Error
GlobalISelEmitter::importDefaultOperandRenderers(
4098 action_iterator InsertPt
, RuleMatcher
&M
, BuildMIAction
&DstMIBuilder
,
4099 DagInit
*DefaultOps
) const {
4100 for (const auto *DefaultOp
: DefaultOps
->getArgs()) {
4101 Optional
<LLTCodeGen
> OpTyOrNone
= None
;
4103 // Look through ValueType operators.
4104 if (const DagInit
*DefaultDagOp
= dyn_cast
<DagInit
>(DefaultOp
)) {
4105 if (const DefInit
*DefaultDagOperator
=
4106 dyn_cast
<DefInit
>(DefaultDagOp
->getOperator())) {
4107 if (DefaultDagOperator
->getDef()->isSubClassOf("ValueType")) {
4108 OpTyOrNone
= MVTToLLT(getValueType(
4109 DefaultDagOperator
->getDef()));
4110 DefaultOp
= DefaultDagOp
->getArg(0);
4115 if (const DefInit
*DefaultDefOp
= dyn_cast
<DefInit
>(DefaultOp
)) {
4116 auto Def
= DefaultDefOp
->getDef();
4117 if (Def
->getName() == "undef_tied_input") {
4118 unsigned TempRegID
= M
.allocateTempRegID();
4119 M
.insertAction
<MakeTempRegisterAction
>(
4120 InsertPt
, OpTyOrNone
.getValue(), TempRegID
);
4121 InsertPt
= M
.insertAction
<BuildMIAction
>(
4122 InsertPt
, M
.allocateOutputInsnID(),
4123 &Target
.getInstruction(RK
.getDef("IMPLICIT_DEF")));
4124 BuildMIAction
&IDMIBuilder
= *static_cast<BuildMIAction
*>(
4126 IDMIBuilder
.addRenderer
<TempRegRenderer
>(TempRegID
);
4127 DstMIBuilder
.addRenderer
<TempRegRenderer
>(TempRegID
);
4129 DstMIBuilder
.addRenderer
<AddRegisterRenderer
>(Def
);
4134 if (const IntInit
*DefaultIntOp
= dyn_cast
<IntInit
>(DefaultOp
)) {
4135 DstMIBuilder
.addRenderer
<ImmRenderer
>(DefaultIntOp
->getValue());
4139 return failedImport("Could not add default op");
4142 return Error::success();
4145 Error
GlobalISelEmitter::importImplicitDefRenderers(
4146 BuildMIAction
&DstMIBuilder
,
4147 const std::vector
<Record
*> &ImplicitDefs
) const {
4148 if (!ImplicitDefs
.empty())
4149 return failedImport("Pattern defines a physical register");
4150 return Error::success();
4153 Optional
<const CodeGenRegisterClass
*>
4154 GlobalISelEmitter::getRegClassFromLeaf(TreePatternNode
*Leaf
) {
4155 assert(Leaf
&& "Expected node?");
4156 assert(Leaf
->isLeaf() && "Expected leaf?");
4157 Record
*RCRec
= getInitValueAsRegClass(Leaf
->getLeafValue());
4160 CodeGenRegisterClass
*RC
= CGRegs
.getRegClass(RCRec
);
4166 Optional
<const CodeGenRegisterClass
*>
4167 GlobalISelEmitter::inferRegClassFromPattern(TreePatternNode
*N
) {
4172 return getRegClassFromLeaf(N
);
4174 // We don't have a leaf node, so we have to try and infer something. Check
4175 // that we have an instruction that we an infer something from.
4177 // Only handle things that produce a single type.
4178 if (N
->getNumTypes() != 1)
4180 Record
*OpRec
= N
->getOperator();
4182 // We only want instructions.
4183 if (!OpRec
->isSubClassOf("Instruction"))
4186 // Don't want to try and infer things when there could potentially be more
4187 // than one candidate register class.
4188 auto &Inst
= Target
.getInstruction(OpRec
);
4189 if (Inst
.Operands
.NumDefs
> 1)
4192 // Handle any special-case instructions which we can safely infer register
4194 StringRef InstName
= Inst
.TheDef
->getName();
4195 bool IsRegSequence
= InstName
== "REG_SEQUENCE";
4196 if (IsRegSequence
|| InstName
== "COPY_TO_REGCLASS") {
4197 // If we have a COPY_TO_REGCLASS, then we need to handle it specially. It
4198 // has the desired register class as the first child.
4199 TreePatternNode
*RCChild
= N
->getChild(IsRegSequence
? 0 : 1);
4200 if (!RCChild
->isLeaf())
4202 return getRegClassFromLeaf(RCChild
);
4205 // Handle destination record types that we can safely infer a register class
4207 const auto &DstIOperand
= Inst
.Operands
[0];
4208 Record
*DstIOpRec
= DstIOperand
.Rec
;
4209 if (DstIOpRec
->isSubClassOf("RegisterOperand")) {
4210 DstIOpRec
= DstIOpRec
->getValueAsDef("RegClass");
4211 const CodeGenRegisterClass
&RC
= Target
.getRegisterClass(DstIOpRec
);
4215 if (DstIOpRec
->isSubClassOf("RegisterClass")) {
4216 const CodeGenRegisterClass
&RC
= Target
.getRegisterClass(DstIOpRec
);
4223 Optional
<const CodeGenRegisterClass
*>
4224 GlobalISelEmitter::inferSuperRegisterClass(const TypeSetByHwMode
&Ty
,
4225 TreePatternNode
*SubRegIdxNode
) {
4226 assert(SubRegIdxNode
&& "Expected subregister index node!");
4227 // We need a ValueTypeByHwMode for getSuperRegForSubReg.
4228 if (!Ty
.isValueTypeByHwMode(false))
4230 if (!SubRegIdxNode
->isLeaf())
4232 DefInit
*SubRegInit
= dyn_cast
<DefInit
>(SubRegIdxNode
->getLeafValue());
4235 CodeGenSubRegIndex
*SubIdx
= CGRegs
.getSubRegIdx(SubRegInit
->getDef());
4237 // Use the information we found above to find a minimal register class which
4238 // supports the subregister and type we want.
4240 Target
.getSuperRegForSubReg(Ty
.getValueTypeByHwMode(), CGRegs
, SubIdx
);
4246 Optional
<const CodeGenRegisterClass
*>
4247 GlobalISelEmitter::inferSuperRegisterClassForNode(
4248 const TypeSetByHwMode
&Ty
, TreePatternNode
*SuperRegNode
,
4249 TreePatternNode
*SubRegIdxNode
) {
4250 assert(SuperRegNode
&& "Expected super register node!");
4251 // Check if we already have a defined register class for the super register
4252 // node. If we do, then we should preserve that rather than inferring anything
4253 // from the subregister index node. We can assume that whoever wrote the
4254 // pattern in the first place made sure that the super register and
4255 // subregister are compatible.
4256 if (Optional
<const CodeGenRegisterClass
*> SuperRegisterClass
=
4257 inferRegClassFromPattern(SuperRegNode
))
4258 return *SuperRegisterClass
;
4259 return inferSuperRegisterClass(Ty
, SubRegIdxNode
);
4262 Expected
<RuleMatcher
> GlobalISelEmitter::runOnPattern(const PatternToMatch
&P
) {
4263 // Keep track of the matchers and actions to emit.
4264 int Score
= P
.getPatternComplexity(CGP
);
4265 RuleMatcher
M(P
.getSrcRecord()->getLoc());
4266 RuleMatcherScores
[M
.getRuleID()] = Score
;
4267 M
.addAction
<DebugCommentAction
>(llvm::to_string(*P
.getSrcPattern()) +
4269 llvm::to_string(*P
.getDstPattern()));
4271 if (auto Error
= importRulePredicates(M
, P
.getPredicates()))
4272 return std::move(Error
);
4274 // Next, analyze the pattern operators.
4275 TreePatternNode
*Src
= P
.getSrcPattern();
4276 TreePatternNode
*Dst
= P
.getDstPattern();
4278 // If the root of either pattern isn't a simple operator, ignore it.
4279 if (auto Err
= isTrivialOperatorNode(Dst
))
4280 return failedImport("Dst pattern root isn't a trivial operator (" +
4281 toString(std::move(Err
)) + ")");
4282 if (auto Err
= isTrivialOperatorNode(Src
))
4283 return failedImport("Src pattern root isn't a trivial operator (" +
4284 toString(std::move(Err
)) + ")");
4286 // The different predicates and matchers created during
4287 // addInstructionMatcher use the RuleMatcher M to set up their
4288 // instruction ID (InsnVarID) that are going to be used when
4289 // M is going to be emitted.
4290 // However, the code doing the emission still relies on the IDs
4291 // returned during that process by the RuleMatcher when issuing
4292 // the recordInsn opcodes.
4294 // 1. The order in which we created the predicates
4295 // and such must be the same as the order in which we emit them,
4297 // 2. We need to reset the generation of the IDs in M somewhere between
4298 // addInstructionMatcher and emit
4300 // FIXME: Long term, we don't want to have to rely on this implicit
4301 // naming being the same. One possible solution would be to have
4302 // explicit operator for operation capture and reference those.
4303 // The plus side is that it would expose opportunities to share
4304 // the capture accross rules. The downside is that it would
4305 // introduce a dependency between predicates (captures must happen
4306 // before their first use.)
4307 InstructionMatcher
&InsnMatcherTemp
= M
.addInstructionMatcher(Src
->getName());
4308 unsigned TempOpIdx
= 0;
4309 auto InsnMatcherOrError
=
4310 createAndImportSelDAGMatcher(M
, InsnMatcherTemp
, Src
, TempOpIdx
);
4311 if (auto Error
= InsnMatcherOrError
.takeError())
4312 return std::move(Error
);
4313 InstructionMatcher
&InsnMatcher
= InsnMatcherOrError
.get();
4315 if (Dst
->isLeaf()) {
4316 Record
*RCDef
= getInitValueAsRegClass(Dst
->getLeafValue());
4318 const CodeGenRegisterClass
&RC
= Target
.getRegisterClass(RCDef
);
4320 // We need to replace the def and all its uses with the specified
4321 // operand. However, we must also insert COPY's wherever needed.
4322 // For now, emit a copy and let the register allocator clean up.
4323 auto &DstI
= Target
.getInstruction(RK
.getDef("COPY"));
4324 const auto &DstIOperand
= DstI
.Operands
[0];
4326 OperandMatcher
&OM0
= InsnMatcher
.getOperand(0);
4327 OM0
.setSymbolicName(DstIOperand
.Name
);
4328 M
.defineOperand(OM0
.getSymbolicName(), OM0
);
4329 OM0
.addPredicate
<RegisterBankOperandMatcher
>(RC
);
4331 auto &DstMIBuilder
=
4332 M
.addAction
<BuildMIAction
>(M
.allocateOutputInsnID(), &DstI
);
4333 DstMIBuilder
.addRenderer
<CopyRenderer
>(DstIOperand
.Name
);
4334 DstMIBuilder
.addRenderer
<CopyRenderer
>(Dst
->getName());
4335 M
.addAction
<ConstrainOperandToRegClassAction
>(0, 0, RC
);
4337 // We're done with this pattern! It's eligible for GISel emission; return
4339 ++NumPatternImported
;
4340 return std::move(M
);
4343 return failedImport("Dst pattern root isn't a known leaf");
4346 // Start with the defined operands (i.e., the results of the root operator).
4347 Record
*DstOp
= Dst
->getOperator();
4348 if (!DstOp
->isSubClassOf("Instruction"))
4349 return failedImport("Pattern operator isn't an instruction");
4351 auto &DstI
= Target
.getInstruction(DstOp
);
4352 StringRef DstIName
= DstI
.TheDef
->getName();
4354 if (DstI
.Operands
.NumDefs
!= Src
->getExtTypes().size())
4355 return failedImport("Src pattern results and dst MI defs are different (" +
4356 to_string(Src
->getExtTypes().size()) + " def(s) vs " +
4357 to_string(DstI
.Operands
.NumDefs
) + " def(s))");
4359 // The root of the match also has constraints on the register bank so that it
4360 // matches the result instruction.
4362 for (const TypeSetByHwMode
&VTy
: Src
->getExtTypes()) {
4365 const auto &DstIOperand
= DstI
.Operands
[OpIdx
];
4366 Record
*DstIOpRec
= DstIOperand
.Rec
;
4367 if (DstIName
== "COPY_TO_REGCLASS") {
4368 DstIOpRec
= getInitValueAsRegClass(Dst
->getChild(1)->getLeafValue());
4370 if (DstIOpRec
== nullptr)
4371 return failedImport(
4372 "COPY_TO_REGCLASS operand #1 isn't a register class");
4373 } else if (DstIName
== "REG_SEQUENCE") {
4374 DstIOpRec
= getInitValueAsRegClass(Dst
->getChild(0)->getLeafValue());
4375 if (DstIOpRec
== nullptr)
4376 return failedImport("REG_SEQUENCE operand #0 isn't a register class");
4377 } else if (DstIName
== "EXTRACT_SUBREG") {
4378 if (!Dst
->getChild(0)->isLeaf())
4379 return failedImport("EXTRACT_SUBREG operand #0 isn't a leaf");
4381 // We can assume that a subregister is in the same bank as it's super
4383 DstIOpRec
= getInitValueAsRegClass(Dst
->getChild(0)->getLeafValue());
4385 if (DstIOpRec
== nullptr)
4386 return failedImport("EXTRACT_SUBREG operand #0 isn't a register class");
4387 } else if (DstIName
== "INSERT_SUBREG") {
4388 auto MaybeSuperClass
= inferSuperRegisterClassForNode(
4389 VTy
, Dst
->getChild(0), Dst
->getChild(2));
4390 if (!MaybeSuperClass
)
4391 return failedImport(
4392 "Cannot infer register class for INSERT_SUBREG operand #0");
4393 // Move to the next pattern here, because the register class we found
4394 // doesn't necessarily have a record associated with it. So, we can't
4395 // set DstIOpRec using this.
4396 OperandMatcher
&OM
= InsnMatcher
.getOperand(OpIdx
);
4397 OM
.setSymbolicName(DstIOperand
.Name
);
4398 M
.defineOperand(OM
.getSymbolicName(), OM
);
4399 OM
.addPredicate
<RegisterBankOperandMatcher
>(**MaybeSuperClass
);
4402 } else if (DstIName
== "SUBREG_TO_REG") {
4403 auto MaybeRegClass
= inferSuperRegisterClass(VTy
, Dst
->getChild(2));
4405 return failedImport(
4406 "Cannot infer register class for SUBREG_TO_REG operand #0");
4407 OperandMatcher
&OM
= InsnMatcher
.getOperand(OpIdx
);
4408 OM
.setSymbolicName(DstIOperand
.Name
);
4409 M
.defineOperand(OM
.getSymbolicName(), OM
);
4410 OM
.addPredicate
<RegisterBankOperandMatcher
>(**MaybeRegClass
);
4413 } else if (DstIOpRec
->isSubClassOf("RegisterOperand"))
4414 DstIOpRec
= DstIOpRec
->getValueAsDef("RegClass");
4415 else if (!DstIOpRec
->isSubClassOf("RegisterClass"))
4416 return failedImport("Dst MI def isn't a register class" +
4419 OperandMatcher
&OM
= InsnMatcher
.getOperand(OpIdx
);
4420 OM
.setSymbolicName(DstIOperand
.Name
);
4421 M
.defineOperand(OM
.getSymbolicName(), OM
);
4422 OM
.addPredicate
<RegisterBankOperandMatcher
>(
4423 Target
.getRegisterClass(DstIOpRec
));
4427 auto DstMIBuilderOrError
= createAndImportInstructionRenderer(M
, Dst
);
4428 if (auto Error
= DstMIBuilderOrError
.takeError())
4429 return std::move(Error
);
4430 BuildMIAction
&DstMIBuilder
= DstMIBuilderOrError
.get();
4432 // Render the implicit defs.
4433 // These are only added to the root of the result.
4434 if (auto Error
= importImplicitDefRenderers(DstMIBuilder
, P
.getDstRegs()))
4435 return std::move(Error
);
4437 DstMIBuilder
.chooseInsnToMutate(M
);
4439 // Constrain the registers to classes. This is normally derived from the
4440 // emitted instruction but a few instructions require special handling.
4441 if (DstIName
== "COPY_TO_REGCLASS") {
4442 // COPY_TO_REGCLASS does not provide operand constraints itself but the
4443 // result is constrained to the class given by the second child.
4445 getInitValueAsRegClass(Dst
->getChild(1)->getLeafValue());
4447 if (DstIOpRec
== nullptr)
4448 return failedImport("COPY_TO_REGCLASS operand #1 isn't a register class");
4450 M
.addAction
<ConstrainOperandToRegClassAction
>(
4451 0, 0, Target
.getRegisterClass(DstIOpRec
));
4453 // We're done with this pattern! It's eligible for GISel emission; return
4455 ++NumPatternImported
;
4456 return std::move(M
);
4459 if (DstIName
== "EXTRACT_SUBREG") {
4460 // EXTRACT_SUBREG selects into a subregister COPY but unlike most
4461 // instructions, the result register class is controlled by the
4462 // subregisters of the operand. As a result, we must constrain the result
4463 // class rather than check that it's already the right one.
4464 if (!Dst
->getChild(0)->isLeaf())
4465 return failedImport("EXTRACT_SUBREG child #1 is not a leaf");
4467 DefInit
*SubRegInit
= dyn_cast
<DefInit
>(Dst
->getChild(1)->getLeafValue());
4469 return failedImport("EXTRACT_SUBREG child #1 is not a subreg index");
4471 // Constrain the result to the same register bank as the operand.
4473 getInitValueAsRegClass(Dst
->getChild(0)->getLeafValue());
4475 if (DstIOpRec
== nullptr)
4476 return failedImport("EXTRACT_SUBREG operand #1 isn't a register class");
4478 CodeGenSubRegIndex
*SubIdx
= CGRegs
.getSubRegIdx(SubRegInit
->getDef());
4479 CodeGenRegisterClass
*SrcRC
= CGRegs
.getRegClass(DstIOpRec
);
4481 // It would be nice to leave this constraint implicit but we're required
4482 // to pick a register class so constrain the result to a register class
4483 // that can hold the correct MVT.
4485 // FIXME: This may introduce an extra copy if the chosen class doesn't
4486 // actually contain the subregisters.
4487 assert(Src
->getExtTypes().size() == 1 &&
4488 "Expected Src of EXTRACT_SUBREG to have one result type");
4490 const auto &SrcRCDstRCPair
=
4491 SrcRC
->getMatchingSubClassWithSubRegs(CGRegs
, SubIdx
);
4492 assert(SrcRCDstRCPair
->second
&& "Couldn't find a matching subclass");
4493 M
.addAction
<ConstrainOperandToRegClassAction
>(0, 0, *SrcRCDstRCPair
->second
);
4494 M
.addAction
<ConstrainOperandToRegClassAction
>(0, 1, *SrcRCDstRCPair
->first
);
4496 // We're done with this pattern! It's eligible for GISel emission; return
4498 ++NumPatternImported
;
4499 return std::move(M
);
4502 if (DstIName
== "INSERT_SUBREG") {
4503 assert(Src
->getExtTypes().size() == 1 &&
4504 "Expected Src of INSERT_SUBREG to have one result type");
4505 // We need to constrain the destination, a super regsister source, and a
4506 // subregister source.
4507 auto SubClass
= inferRegClassFromPattern(Dst
->getChild(1));
4509 return failedImport(
4510 "Cannot infer register class from INSERT_SUBREG operand #1");
4511 auto SuperClass
= inferSuperRegisterClassForNode(
4512 Src
->getExtType(0), Dst
->getChild(0), Dst
->getChild(2));
4514 return failedImport(
4515 "Cannot infer register class for INSERT_SUBREG operand #0");
4516 M
.addAction
<ConstrainOperandToRegClassAction
>(0, 0, **SuperClass
);
4517 M
.addAction
<ConstrainOperandToRegClassAction
>(0, 1, **SuperClass
);
4518 M
.addAction
<ConstrainOperandToRegClassAction
>(0, 2, **SubClass
);
4519 ++NumPatternImported
;
4520 return std::move(M
);
4523 if (DstIName
== "SUBREG_TO_REG") {
4524 // We need to constrain the destination and subregister source.
4525 assert(Src
->getExtTypes().size() == 1 &&
4526 "Expected Src of SUBREG_TO_REG to have one result type");
4528 // Attempt to infer the subregister source from the first child. If it has
4529 // an explicitly given register class, we'll use that. Otherwise, we will
4531 auto SubClass
= inferRegClassFromPattern(Dst
->getChild(1));
4533 return failedImport(
4534 "Cannot infer register class from SUBREG_TO_REG child #1");
4535 // We don't have a child to look at that might have a super register node.
4537 inferSuperRegisterClass(Src
->getExtType(0), Dst
->getChild(2));
4539 return failedImport(
4540 "Cannot infer register class for SUBREG_TO_REG operand #0");
4541 M
.addAction
<ConstrainOperandToRegClassAction
>(0, 0, **SuperClass
);
4542 M
.addAction
<ConstrainOperandToRegClassAction
>(0, 2, **SubClass
);
4543 ++NumPatternImported
;
4544 return std::move(M
);
4547 M
.addAction
<ConstrainOperandsToDefinitionAction
>(0);
4549 // We're done with this pattern! It's eligible for GISel emission; return it.
4550 ++NumPatternImported
;
4551 return std::move(M
);
4554 // Emit imm predicate table and an enum to reference them with.
4555 // The 'Predicate_' part of the name is redundant but eliminating it is more
4556 // trouble than it's worth.
4557 void GlobalISelEmitter::emitCxxPredicateFns(
4558 raw_ostream
&OS
, StringRef CodeFieldName
, StringRef TypeIdentifier
,
4559 StringRef ArgType
, StringRef ArgName
, StringRef AdditionalDeclarations
,
4560 std::function
<bool(const Record
*R
)> Filter
) {
4561 std::vector
<const Record
*> MatchedRecords
;
4562 const auto &Defs
= RK
.getAllDerivedDefinitions("PatFrag");
4563 std::copy_if(Defs
.begin(), Defs
.end(), std::back_inserter(MatchedRecords
),
4564 [&](Record
*Record
) {
4565 return !Record
->getValueAsString(CodeFieldName
).empty() &&
4569 if (!MatchedRecords
.empty()) {
4570 OS
<< "// PatFrag predicates.\n"
4572 std::string EnumeratorSeparator
=
4573 (" = GIPFP_" + TypeIdentifier
+ "_Invalid + 1,\n").str();
4574 for (const auto *Record
: MatchedRecords
) {
4575 OS
<< " GIPFP_" << TypeIdentifier
<< "_Predicate_" << Record
->getName()
4576 << EnumeratorSeparator
;
4577 EnumeratorSeparator
= ",\n";
4582 OS
<< "bool " << Target
.getName() << "InstructionSelector::test" << ArgName
4583 << "Predicate_" << TypeIdentifier
<< "(unsigned PredicateID, " << ArgType
<< " "
4584 << ArgName
<< ") const {\n"
4585 << AdditionalDeclarations
;
4586 if (!AdditionalDeclarations
.empty())
4588 if (!MatchedRecords
.empty())
4589 OS
<< " switch (PredicateID) {\n";
4590 for (const auto *Record
: MatchedRecords
) {
4591 OS
<< " case GIPFP_" << TypeIdentifier
<< "_Predicate_"
4592 << Record
->getName() << ": {\n"
4593 << " " << Record
->getValueAsString(CodeFieldName
) << "\n"
4594 << " llvm_unreachable(\"" << CodeFieldName
4595 << " should have returned\");\n"
4596 << " return false;\n"
4599 if (!MatchedRecords
.empty())
4601 OS
<< " llvm_unreachable(\"Unknown predicate\");\n"
4602 << " return false;\n"
4606 void GlobalISelEmitter::emitImmPredicateFns(
4607 raw_ostream
&OS
, StringRef TypeIdentifier
, StringRef ArgType
,
4608 std::function
<bool(const Record
*R
)> Filter
) {
4609 return emitCxxPredicateFns(OS
, "ImmediateCode", TypeIdentifier
, ArgType
,
4613 void GlobalISelEmitter::emitMIPredicateFns(raw_ostream
&OS
) {
4614 return emitCxxPredicateFns(
4615 OS
, "GISelPredicateCode", "MI", "const MachineInstr &", "MI",
4616 " const MachineFunction &MF = *MI.getParent()->getParent();\n"
4617 " const MachineRegisterInfo &MRI = MF.getRegInfo();\n"
4619 [](const Record
*R
) { return true; });
4622 template <class GroupT
>
4623 std::vector
<Matcher
*> GlobalISelEmitter::optimizeRules(
4624 ArrayRef
<Matcher
*> Rules
,
4625 std::vector
<std::unique_ptr
<Matcher
>> &MatcherStorage
) {
4627 std::vector
<Matcher
*> OptRules
;
4628 std::unique_ptr
<GroupT
> CurrentGroup
= std::make_unique
<GroupT
>();
4629 assert(CurrentGroup
->empty() && "Newly created group isn't empty!");
4630 unsigned NumGroups
= 0;
4632 auto ProcessCurrentGroup
= [&]() {
4633 if (CurrentGroup
->empty())
4634 // An empty group is good to be reused:
4637 // If the group isn't large enough to provide any benefit, move all the
4638 // added rules out of it and make sure to re-create the group to properly
4639 // re-initialize it:
4640 if (CurrentGroup
->size() < 2)
4641 for (Matcher
*M
: CurrentGroup
->matchers())
4642 OptRules
.push_back(M
);
4644 CurrentGroup
->finalize();
4645 OptRules
.push_back(CurrentGroup
.get());
4646 MatcherStorage
.emplace_back(std::move(CurrentGroup
));
4649 CurrentGroup
= std::make_unique
<GroupT
>();
4651 for (Matcher
*Rule
: Rules
) {
4652 // Greedily add as many matchers as possible to the current group:
4653 if (CurrentGroup
->addMatcher(*Rule
))
4656 ProcessCurrentGroup();
4657 assert(CurrentGroup
->empty() && "A group wasn't properly re-initialized");
4659 // Try to add the pending matcher to a newly created empty group:
4660 if (!CurrentGroup
->addMatcher(*Rule
))
4661 // If we couldn't add the matcher to an empty group, that group type
4662 // doesn't support that kind of matchers at all, so just skip it:
4663 OptRules
.push_back(Rule
);
4665 ProcessCurrentGroup();
4667 LLVM_DEBUG(dbgs() << "NumGroups: " << NumGroups
<< "\n");
4668 assert(CurrentGroup
->empty() && "The last group wasn't properly processed");
4673 GlobalISelEmitter::buildMatchTable(MutableArrayRef
<RuleMatcher
> Rules
,
4674 bool Optimize
, bool WithCoverage
) {
4675 std::vector
<Matcher
*> InputRules
;
4676 for (Matcher
&Rule
: Rules
)
4677 InputRules
.push_back(&Rule
);
4680 return MatchTable::buildTable(InputRules
, WithCoverage
);
4682 unsigned CurrentOrdering
= 0;
4683 StringMap
<unsigned> OpcodeOrder
;
4684 for (RuleMatcher
&Rule
: Rules
) {
4685 const StringRef Opcode
= Rule
.getOpcode();
4686 assert(!Opcode
.empty() && "Didn't expect an undefined opcode");
4687 if (OpcodeOrder
.count(Opcode
) == 0)
4688 OpcodeOrder
[Opcode
] = CurrentOrdering
++;
4691 std::stable_sort(InputRules
.begin(), InputRules
.end(),
4692 [&OpcodeOrder
](const Matcher
*A
, const Matcher
*B
) {
4693 auto *L
= static_cast<const RuleMatcher
*>(A
);
4694 auto *R
= static_cast<const RuleMatcher
*>(B
);
4695 return std::make_tuple(OpcodeOrder
[L
->getOpcode()],
4696 L
->getNumOperands()) <
4697 std::make_tuple(OpcodeOrder
[R
->getOpcode()],
4698 R
->getNumOperands());
4701 for (Matcher
*Rule
: InputRules
)
4704 std::vector
<std::unique_ptr
<Matcher
>> MatcherStorage
;
4705 std::vector
<Matcher
*> OptRules
=
4706 optimizeRules
<GroupMatcher
>(InputRules
, MatcherStorage
);
4708 for (Matcher
*Rule
: OptRules
)
4711 OptRules
= optimizeRules
<SwitchMatcher
>(OptRules
, MatcherStorage
);
4713 return MatchTable::buildTable(OptRules
, WithCoverage
);
4716 void GroupMatcher::optimize() {
4717 // Make sure we only sort by a specific predicate within a range of rules that
4718 // all have that predicate checked against a specific value (not a wildcard):
4719 auto F
= Matchers
.begin();
4721 auto E
= Matchers
.end();
4724 auto *R
= static_cast<RuleMatcher
*>(*T
);
4725 if (!R
->getFirstConditionAsRootType().get().isValid())
4729 std::stable_sort(F
, T
, [](Matcher
*A
, Matcher
*B
) {
4730 auto *L
= static_cast<RuleMatcher
*>(A
);
4731 auto *R
= static_cast<RuleMatcher
*>(B
);
4732 return L
->getFirstConditionAsRootType() <
4733 R
->getFirstConditionAsRootType();
4738 GlobalISelEmitter::optimizeRules
<GroupMatcher
>(Matchers
, MatcherStorage
)
4740 GlobalISelEmitter::optimizeRules
<SwitchMatcher
>(Matchers
, MatcherStorage
)
4744 void GlobalISelEmitter::run(raw_ostream
&OS
) {
4745 if (!UseCoverageFile
.empty()) {
4746 RuleCoverage
= CodeGenCoverage();
4747 auto RuleCoverageBufOrErr
= MemoryBuffer::getFile(UseCoverageFile
);
4748 if (!RuleCoverageBufOrErr
) {
4749 PrintWarning(SMLoc(), "Missing rule coverage data");
4750 RuleCoverage
= None
;
4752 if (!RuleCoverage
->parse(*RuleCoverageBufOrErr
.get(), Target
.getName())) {
4753 PrintWarning(SMLoc(), "Ignoring invalid or missing rule coverage data");
4754 RuleCoverage
= None
;
4759 // Track the run-time opcode values
4760 gatherOpcodeValues();
4761 // Track the run-time LLT ID values
4762 gatherTypeIDValues();
4764 // Track the GINodeEquiv definitions.
4767 emitSourceFileHeader(("Global Instruction Selector for the " +
4768 Target
.getName() + " target").str(), OS
);
4769 std::vector
<RuleMatcher
> Rules
;
4770 // Look through the SelectionDAG patterns we found, possibly emitting some.
4771 for (const PatternToMatch
&Pat
: CGP
.ptms()) {
4774 auto MatcherOrErr
= runOnPattern(Pat
);
4776 // The pattern analysis can fail, indicating an unsupported pattern.
4777 // Report that if we've been asked to do so.
4778 if (auto Err
= MatcherOrErr
.takeError()) {
4779 if (WarnOnSkippedPatterns
) {
4780 PrintWarning(Pat
.getSrcRecord()->getLoc(),
4781 "Skipped pattern: " + toString(std::move(Err
)));
4783 consumeError(std::move(Err
));
4785 ++NumPatternImportsSkipped
;
4790 if (RuleCoverage
->isCovered(MatcherOrErr
->getRuleID()))
4791 ++NumPatternsTested
;
4793 PrintWarning(Pat
.getSrcRecord()->getLoc(),
4794 "Pattern is not covered by a test");
4796 Rules
.push_back(std::move(MatcherOrErr
.get()));
4799 // Comparison function to order records by name.
4800 auto orderByName
= [](const Record
*A
, const Record
*B
) {
4801 return A
->getName() < B
->getName();
4804 std::vector
<Record
*> ComplexPredicates
=
4805 RK
.getAllDerivedDefinitions("GIComplexOperandMatcher");
4806 llvm::sort(ComplexPredicates
, orderByName
);
4808 std::vector
<Record
*> CustomRendererFns
=
4809 RK
.getAllDerivedDefinitions("GICustomOperandRenderer");
4810 llvm::sort(CustomRendererFns
, orderByName
);
4812 unsigned MaxTemporaries
= 0;
4813 for (const auto &Rule
: Rules
)
4814 MaxTemporaries
= std::max(MaxTemporaries
, Rule
.countRendererFns());
4816 OS
<< "#ifdef GET_GLOBALISEL_PREDICATE_BITSET\n"
4817 << "const unsigned MAX_SUBTARGET_PREDICATES = " << SubtargetFeatures
.size()
4819 << "using PredicateBitset = "
4820 "llvm::PredicateBitsetImpl<MAX_SUBTARGET_PREDICATES>;\n"
4821 << "#endif // ifdef GET_GLOBALISEL_PREDICATE_BITSET\n\n";
4823 OS
<< "#ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n"
4824 << " mutable MatcherState State;\n"
4826 "ComplexRendererFns("
4828 << "InstructionSelector::*ComplexMatcherMemFn)(MachineOperand &) const;\n"
4830 << " typedef void(" << Target
.getName()
4831 << "InstructionSelector::*CustomRendererFn)(MachineInstrBuilder &, const "
4834 << " const ISelInfoTy<PredicateBitset, ComplexMatcherMemFn, "
4835 "CustomRendererFn> "
4837 OS
<< " static " << Target
.getName()
4838 << "InstructionSelector::ComplexMatcherMemFn ComplexPredicateFns[];\n"
4839 << " static " << Target
.getName()
4840 << "InstructionSelector::CustomRendererFn CustomRenderers[];\n"
4841 << " bool testImmPredicate_I64(unsigned PredicateID, int64_t Imm) const "
4843 << " bool testImmPredicate_APInt(unsigned PredicateID, const APInt &Imm) "
4845 << " bool testImmPredicate_APFloat(unsigned PredicateID, const APFloat "
4846 "&Imm) const override;\n"
4847 << " const int64_t *getMatchTable() const override;\n"
4848 << " bool testMIPredicate_MI(unsigned PredicateID, const MachineInstr &MI) "
4850 << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n\n";
4852 OS
<< "#ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n"
4853 << ", State(" << MaxTemporaries
<< "),\n"
4854 << "ISelInfo(TypeObjects, NumTypeObjects, FeatureBitsets"
4855 << ", ComplexPredicateFns, CustomRenderers)\n"
4856 << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n\n";
4858 OS
<< "#ifdef GET_GLOBALISEL_IMPL\n";
4859 SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(SubtargetFeatures
,
4862 // Separate subtarget features by how often they must be recomputed.
4863 SubtargetFeatureInfoMap ModuleFeatures
;
4864 std::copy_if(SubtargetFeatures
.begin(), SubtargetFeatures
.end(),
4865 std::inserter(ModuleFeatures
, ModuleFeatures
.end()),
4866 [](const SubtargetFeatureInfoMap::value_type
&X
) {
4867 return !X
.second
.mustRecomputePerFunction();
4869 SubtargetFeatureInfoMap FunctionFeatures
;
4870 std::copy_if(SubtargetFeatures
.begin(), SubtargetFeatures
.end(),
4871 std::inserter(FunctionFeatures
, FunctionFeatures
.end()),
4872 [](const SubtargetFeatureInfoMap::value_type
&X
) {
4873 return X
.second
.mustRecomputePerFunction();
4876 SubtargetFeatureInfo::emitComputeAvailableFeatures(
4877 Target
.getName(), "InstructionSelector", "computeAvailableModuleFeatures",
4878 ModuleFeatures
, OS
);
4879 SubtargetFeatureInfo::emitComputeAvailableFeatures(
4880 Target
.getName(), "InstructionSelector",
4881 "computeAvailableFunctionFeatures", FunctionFeatures
, OS
,
4882 "const MachineFunction *MF");
4884 // Emit a table containing the LLT objects needed by the matcher and an enum
4885 // for the matcher to reference them with.
4886 std::vector
<LLTCodeGen
> TypeObjects
;
4887 for (const auto &Ty
: KnownTypes
)
4888 TypeObjects
.push_back(Ty
);
4889 llvm::sort(TypeObjects
);
4890 OS
<< "// LLT Objects.\n"
4892 for (const auto &TypeObject
: TypeObjects
) {
4894 TypeObject
.emitCxxEnumValue(OS
);
4898 OS
<< "const static size_t NumTypeObjects = " << TypeObjects
.size() << ";\n"
4899 << "const static LLT TypeObjects[] = {\n";
4900 for (const auto &TypeObject
: TypeObjects
) {
4902 TypeObject
.emitCxxConstructorCall(OS
);
4907 // Emit a table containing the PredicateBitsets objects needed by the matcher
4908 // and an enum for the matcher to reference them with.
4909 std::vector
<std::vector
<Record
*>> FeatureBitsets
;
4910 for (auto &Rule
: Rules
)
4911 FeatureBitsets
.push_back(Rule
.getRequiredFeatures());
4912 llvm::sort(FeatureBitsets
, [&](const std::vector
<Record
*> &A
,
4913 const std::vector
<Record
*> &B
) {
4914 if (A
.size() < B
.size())
4916 if (A
.size() > B
.size())
4918 for (const auto &Pair
: zip(A
, B
)) {
4919 if (std::get
<0>(Pair
)->getName() < std::get
<1>(Pair
)->getName())
4921 if (std::get
<0>(Pair
)->getName() > std::get
<1>(Pair
)->getName())
4926 FeatureBitsets
.erase(
4927 std::unique(FeatureBitsets
.begin(), FeatureBitsets
.end()),
4928 FeatureBitsets
.end());
4929 OS
<< "// Feature bitsets.\n"
4931 << " GIFBS_Invalid,\n";
4932 for (const auto &FeatureBitset
: FeatureBitsets
) {
4933 if (FeatureBitset
.empty())
4935 OS
<< " " << getNameForFeatureBitset(FeatureBitset
) << ",\n";
4938 << "const static PredicateBitset FeatureBitsets[] {\n"
4939 << " {}, // GIFBS_Invalid\n";
4940 for (const auto &FeatureBitset
: FeatureBitsets
) {
4941 if (FeatureBitset
.empty())
4944 for (const auto &Feature
: FeatureBitset
) {
4945 const auto &I
= SubtargetFeatures
.find(Feature
);
4946 assert(I
!= SubtargetFeatures
.end() && "Didn't import predicate?");
4947 OS
<< I
->second
.getEnumBitName() << ", ";
4953 // Emit complex predicate table and an enum to reference them with.
4954 OS
<< "// ComplexPattern predicates.\n"
4956 << " GICP_Invalid,\n";
4957 for (const auto &Record
: ComplexPredicates
)
4958 OS
<< " GICP_" << Record
->getName() << ",\n";
4960 << "// See constructor for table contents\n\n";
4962 emitImmPredicateFns(OS
, "I64", "int64_t", [](const Record
*R
) {
4964 return !R
->getValueAsBitOrUnset("IsAPFloat", Unset
) &&
4965 !R
->getValueAsBit("IsAPInt");
4967 emitImmPredicateFns(OS
, "APFloat", "const APFloat &", [](const Record
*R
) {
4969 return R
->getValueAsBitOrUnset("IsAPFloat", Unset
);
4971 emitImmPredicateFns(OS
, "APInt", "const APInt &", [](const Record
*R
) {
4972 return R
->getValueAsBit("IsAPInt");
4974 emitMIPredicateFns(OS
);
4977 OS
<< Target
.getName() << "InstructionSelector::ComplexMatcherMemFn\n"
4978 << Target
.getName() << "InstructionSelector::ComplexPredicateFns[] = {\n"
4979 << " nullptr, // GICP_Invalid\n";
4980 for (const auto &Record
: ComplexPredicates
)
4981 OS
<< " &" << Target
.getName()
4982 << "InstructionSelector::" << Record
->getValueAsString("MatcherFn")
4983 << ", // " << Record
->getName() << "\n";
4986 OS
<< "// Custom renderers.\n"
4988 << " GICR_Invalid,\n";
4989 for (const auto &Record
: CustomRendererFns
)
4990 OS
<< " GICR_" << Record
->getValueAsString("RendererFn") << ", \n";
4993 OS
<< Target
.getName() << "InstructionSelector::CustomRendererFn\n"
4994 << Target
.getName() << "InstructionSelector::CustomRenderers[] = {\n"
4995 << " nullptr, // GICP_Invalid\n";
4996 for (const auto &Record
: CustomRendererFns
)
4997 OS
<< " &" << Target
.getName()
4998 << "InstructionSelector::" << Record
->getValueAsString("RendererFn")
4999 << ", // " << Record
->getName() << "\n";
5002 llvm::stable_sort(Rules
, [&](const RuleMatcher
&A
, const RuleMatcher
&B
) {
5003 int ScoreA
= RuleMatcherScores
[A
.getRuleID()];
5004 int ScoreB
= RuleMatcherScores
[B
.getRuleID()];
5005 if (ScoreA
> ScoreB
)
5007 if (ScoreB
> ScoreA
)
5009 if (A
.isHigherPriorityThan(B
)) {
5010 assert(!B
.isHigherPriorityThan(A
) && "Cannot be more important "
5011 "and less important at "
5018 OS
<< "bool " << Target
.getName()
5019 << "InstructionSelector::selectImpl(MachineInstr &I, CodeGenCoverage "
5020 "&CoverageInfo) const {\n"
5021 << " MachineFunction &MF = *I.getParent()->getParent();\n"
5022 << " MachineRegisterInfo &MRI = MF.getRegInfo();\n"
5023 << " // FIXME: This should be computed on a per-function basis rather "
5025 << " AvailableFunctionFeatures = computeAvailableFunctionFeatures(&STI, "
5027 << " const PredicateBitset AvailableFeatures = getAvailableFeatures();\n"
5028 << " NewMIVector OutMIs;\n"
5029 << " State.MIs.clear();\n"
5030 << " State.MIs.push_back(&I);\n\n"
5031 << " if (executeMatchTable(*this, OutMIs, State, ISelInfo"
5032 << ", getMatchTable(), TII, MRI, TRI, RBI, AvailableFeatures"
5033 << ", CoverageInfo)) {\n"
5034 << " return true;\n"
5036 << " return false;\n"
5039 const MatchTable Table
=
5040 buildMatchTable(Rules
, OptimizeMatchTable
, GenerateCoverage
);
5041 OS
<< "const int64_t *" << Target
.getName()
5042 << "InstructionSelector::getMatchTable() const {\n";
5043 Table
.emitDeclaration(OS
);
5047 OS
<< "#endif // ifdef GET_GLOBALISEL_IMPL\n";
5049 OS
<< "#ifdef GET_GLOBALISEL_PREDICATES_DECL\n"
5050 << "PredicateBitset AvailableModuleFeatures;\n"
5051 << "mutable PredicateBitset AvailableFunctionFeatures;\n"
5052 << "PredicateBitset getAvailableFeatures() const {\n"
5053 << " return AvailableModuleFeatures | AvailableFunctionFeatures;\n"
5055 << "PredicateBitset\n"
5056 << "computeAvailableModuleFeatures(const " << Target
.getName()
5057 << "Subtarget *Subtarget) const;\n"
5058 << "PredicateBitset\n"
5059 << "computeAvailableFunctionFeatures(const " << Target
.getName()
5060 << "Subtarget *Subtarget,\n"
5061 << " const MachineFunction *MF) const;\n"
5062 << "#endif // ifdef GET_GLOBALISEL_PREDICATES_DECL\n";
5064 OS
<< "#ifdef GET_GLOBALISEL_PREDICATES_INIT\n"
5065 << "AvailableModuleFeatures(computeAvailableModuleFeatures(&STI)),\n"
5066 << "AvailableFunctionFeatures()\n"
5067 << "#endif // ifdef GET_GLOBALISEL_PREDICATES_INIT\n";
5070 void GlobalISelEmitter::declareSubtargetFeature(Record
*Predicate
) {
5071 if (SubtargetFeatures
.count(Predicate
) == 0)
5072 SubtargetFeatures
.emplace(
5073 Predicate
, SubtargetFeatureInfo(Predicate
, SubtargetFeatures
.size()));
5076 void RuleMatcher::optimize() {
5077 for (auto &Item
: InsnVariableIDs
) {
5078 InstructionMatcher
&InsnMatcher
= *Item
.first
;
5079 for (auto &OM
: InsnMatcher
.operands()) {
5080 // Complex Patterns are usually expensive and they relatively rarely fail
5081 // on their own: more often we end up throwing away all the work done by a
5082 // matching part of a complex pattern because some other part of the
5083 // enclosing pattern didn't match. All of this makes it beneficial to
5084 // delay complex patterns until the very end of the rule matching,
5085 // especially for targets having lots of complex patterns.
5086 for (auto &OP
: OM
->predicates())
5087 if (isa
<ComplexPatternOperandMatcher
>(OP
))
5088 EpilogueMatchers
.emplace_back(std::move(OP
));
5089 OM
->eraseNullPredicates();
5091 InsnMatcher
.optimize();
5093 llvm::sort(EpilogueMatchers
, [](const std::unique_ptr
<PredicateMatcher
> &L
,
5094 const std::unique_ptr
<PredicateMatcher
> &R
) {
5095 return std::make_tuple(L
->getKind(), L
->getInsnVarID(), L
->getOpIdx()) <
5096 std::make_tuple(R
->getKind(), R
->getInsnVarID(), R
->getOpIdx());
5100 bool RuleMatcher::hasFirstCondition() const {
5101 if (insnmatchers_empty())
5103 InstructionMatcher
&Matcher
= insnmatchers_front();
5104 if (!Matcher
.predicates_empty())
5106 for (auto &OM
: Matcher
.operands())
5107 for (auto &OP
: OM
->predicates())
5108 if (!isa
<InstructionOperandMatcher
>(OP
))
5113 const PredicateMatcher
&RuleMatcher::getFirstCondition() const {
5114 assert(!insnmatchers_empty() &&
5115 "Trying to get a condition from an empty RuleMatcher");
5117 InstructionMatcher
&Matcher
= insnmatchers_front();
5118 if (!Matcher
.predicates_empty())
5119 return **Matcher
.predicates_begin();
5120 // If there is no more predicate on the instruction itself, look at its
5122 for (auto &OM
: Matcher
.operands())
5123 for (auto &OP
: OM
->predicates())
5124 if (!isa
<InstructionOperandMatcher
>(OP
))
5127 llvm_unreachable("Trying to get a condition from an InstructionMatcher with "
5131 std::unique_ptr
<PredicateMatcher
> RuleMatcher::popFirstCondition() {
5132 assert(!insnmatchers_empty() &&
5133 "Trying to pop a condition from an empty RuleMatcher");
5135 InstructionMatcher
&Matcher
= insnmatchers_front();
5136 if (!Matcher
.predicates_empty())
5137 return Matcher
.predicates_pop_front();
5138 // If there is no more predicate on the instruction itself, look at its
5140 for (auto &OM
: Matcher
.operands())
5141 for (auto &OP
: OM
->predicates())
5142 if (!isa
<InstructionOperandMatcher
>(OP
)) {
5143 std::unique_ptr
<PredicateMatcher
> Result
= std::move(OP
);
5144 OM
->eraseNullPredicates();
5148 llvm_unreachable("Trying to pop a condition from an InstructionMatcher with "
5152 bool GroupMatcher::candidateConditionMatches(
5153 const PredicateMatcher
&Predicate
) const {
5156 // Sharing predicates for nested instructions is not supported yet as we
5157 // currently don't hoist the GIM_RecordInsn's properly, therefore we can
5158 // only work on the original root instruction (InsnVarID == 0):
5159 if (Predicate
.getInsnVarID() != 0)
5161 // ... otherwise an empty group can handle any predicate with no specific
5166 const Matcher
&Representative
= **Matchers
.begin();
5167 const auto &RepresentativeCondition
= Representative
.getFirstCondition();
5168 // ... if not empty, the group can only accomodate matchers with the exact
5169 // same first condition:
5170 return Predicate
.isIdentical(RepresentativeCondition
);
5173 bool GroupMatcher::addMatcher(Matcher
&Candidate
) {
5174 if (!Candidate
.hasFirstCondition())
5177 const PredicateMatcher
&Predicate
= Candidate
.getFirstCondition();
5178 if (!candidateConditionMatches(Predicate
))
5181 Matchers
.push_back(&Candidate
);
5185 void GroupMatcher::finalize() {
5186 assert(Conditions
.empty() && "Already finalized?");
5190 Matcher
&FirstRule
= **Matchers
.begin();
5192 // All the checks are expected to succeed during the first iteration:
5193 for (const auto &Rule
: Matchers
)
5194 if (!Rule
->hasFirstCondition())
5196 const auto &FirstCondition
= FirstRule
.getFirstCondition();
5197 for (unsigned I
= 1, E
= Matchers
.size(); I
< E
; ++I
)
5198 if (!Matchers
[I
]->getFirstCondition().isIdentical(FirstCondition
))
5201 Conditions
.push_back(FirstRule
.popFirstCondition());
5202 for (unsigned I
= 1, E
= Matchers
.size(); I
< E
; ++I
)
5203 Matchers
[I
]->popFirstCondition();
5207 void GroupMatcher::emit(MatchTable
&Table
) {
5208 unsigned LabelID
= ~0U;
5209 if (!Conditions
.empty()) {
5210 LabelID
= Table
.allocateLabelID();
5211 Table
<< MatchTable::Opcode("GIM_Try", +1)
5212 << MatchTable::Comment("On fail goto")
5213 << MatchTable::JumpTarget(LabelID
) << MatchTable::LineBreak
;
5215 for (auto &Condition
: Conditions
)
5216 Condition
->emitPredicateOpcodes(
5217 Table
, *static_cast<RuleMatcher
*>(*Matchers
.begin()));
5219 for (const auto &M
: Matchers
)
5223 if (!Conditions
.empty())
5224 Table
<< MatchTable::Opcode("GIM_Reject", -1) << MatchTable::LineBreak
5225 << MatchTable::Label(LabelID
);
5228 bool SwitchMatcher::isSupportedPredicateType(const PredicateMatcher
&P
) {
5229 return isa
<InstructionOpcodeMatcher
>(P
) || isa
<LLTOperandMatcher
>(P
);
5232 bool SwitchMatcher::candidateConditionMatches(
5233 const PredicateMatcher
&Predicate
) const {
5236 // Sharing predicates for nested instructions is not supported yet as we
5237 // currently don't hoist the GIM_RecordInsn's properly, therefore we can
5238 // only work on the original root instruction (InsnVarID == 0):
5239 if (Predicate
.getInsnVarID() != 0)
5241 // ... while an attempt to add even a root matcher to an empty SwitchMatcher
5242 // could fail as not all the types of conditions are supported:
5243 if (!isSupportedPredicateType(Predicate
))
5245 // ... or the condition might not have a proper implementation of
5246 // getValue() / isIdenticalDownToValue() yet:
5247 if (!Predicate
.hasValue())
5249 // ... otherwise an empty Switch can accomodate the condition with no
5250 // further requirements:
5254 const Matcher
&CaseRepresentative
= **Matchers
.begin();
5255 const auto &RepresentativeCondition
= CaseRepresentative
.getFirstCondition();
5256 // Switch-cases must share the same kind of condition and path to the value it
5258 if (!Predicate
.isIdenticalDownToValue(RepresentativeCondition
))
5261 const auto Value
= Predicate
.getValue();
5262 // ... but be unique with respect to the actual value they check:
5263 return Values
.count(Value
) == 0;
5266 bool SwitchMatcher::addMatcher(Matcher
&Candidate
) {
5267 if (!Candidate
.hasFirstCondition())
5270 const PredicateMatcher
&Predicate
= Candidate
.getFirstCondition();
5271 if (!candidateConditionMatches(Predicate
))
5273 const auto Value
= Predicate
.getValue();
5274 Values
.insert(Value
);
5276 Matchers
.push_back(&Candidate
);
5280 void SwitchMatcher::finalize() {
5281 assert(Condition
== nullptr && "Already finalized");
5282 assert(Values
.size() == Matchers
.size() && "Broken SwitchMatcher");
5286 std::stable_sort(Matchers
.begin(), Matchers
.end(),
5287 [](const Matcher
*L
, const Matcher
*R
) {
5288 return L
->getFirstCondition().getValue() <
5289 R
->getFirstCondition().getValue();
5291 Condition
= Matchers
[0]->popFirstCondition();
5292 for (unsigned I
= 1, E
= Values
.size(); I
< E
; ++I
)
5293 Matchers
[I
]->popFirstCondition();
5296 void SwitchMatcher::emitPredicateSpecificOpcodes(const PredicateMatcher
&P
,
5297 MatchTable
&Table
) {
5298 assert(isSupportedPredicateType(P
) && "Predicate type is not supported");
5300 if (const auto *Condition
= dyn_cast
<InstructionOpcodeMatcher
>(&P
)) {
5301 Table
<< MatchTable::Opcode("GIM_SwitchOpcode") << MatchTable::Comment("MI")
5302 << MatchTable::IntValue(Condition
->getInsnVarID());
5305 if (const auto *Condition
= dyn_cast
<LLTOperandMatcher
>(&P
)) {
5306 Table
<< MatchTable::Opcode("GIM_SwitchType") << MatchTable::Comment("MI")
5307 << MatchTable::IntValue(Condition
->getInsnVarID())
5308 << MatchTable::Comment("Op")
5309 << MatchTable::IntValue(Condition
->getOpIdx());
5313 llvm_unreachable("emitPredicateSpecificOpcodes is broken: can not handle a "
5314 "predicate type that is claimed to be supported");
5317 void SwitchMatcher::emit(MatchTable
&Table
) {
5318 assert(Values
.size() == Matchers
.size() && "Broken SwitchMatcher");
5321 assert(Condition
!= nullptr &&
5322 "Broken SwitchMatcher, hasn't been finalized?");
5324 std::vector
<unsigned> LabelIDs(Values
.size());
5325 std::generate(LabelIDs
.begin(), LabelIDs
.end(),
5326 [&Table
]() { return Table
.allocateLabelID(); });
5327 const unsigned Default
= Table
.allocateLabelID();
5329 const int64_t LowerBound
= Values
.begin()->getRawValue();
5330 const int64_t UpperBound
= Values
.rbegin()->getRawValue() + 1;
5332 emitPredicateSpecificOpcodes(*Condition
, Table
);
5334 Table
<< MatchTable::Comment("[") << MatchTable::IntValue(LowerBound
)
5335 << MatchTable::IntValue(UpperBound
) << MatchTable::Comment(")")
5336 << MatchTable::Comment("default:") << MatchTable::JumpTarget(Default
);
5338 int64_t J
= LowerBound
;
5339 auto VI
= Values
.begin();
5340 for (unsigned I
= 0, E
= Values
.size(); I
< E
; ++I
) {
5342 while (J
++ < V
.getRawValue())
5343 Table
<< MatchTable::IntValue(0);
5344 V
.turnIntoComment();
5345 Table
<< MatchTable::LineBreak
<< V
<< MatchTable::JumpTarget(LabelIDs
[I
]);
5347 Table
<< MatchTable::LineBreak
;
5349 for (unsigned I
= 0, E
= Values
.size(); I
< E
; ++I
) {
5350 Table
<< MatchTable::Label(LabelIDs
[I
]);
5351 Matchers
[I
]->emit(Table
);
5352 Table
<< MatchTable::Opcode("GIM_Reject") << MatchTable::LineBreak
;
5354 Table
<< MatchTable::Label(Default
);
5357 unsigned OperandMatcher::getInsnVarID() const { return Insn
.getInsnVarID(); }
5359 } // end anonymous namespace
5361 //===----------------------------------------------------------------------===//
5364 void EmitGlobalISel(RecordKeeper
&RK
, raw_ostream
&OS
) {
5365 GlobalISelEmitter(RK
).run(OS
);
5367 } // End llvm namespace