1//===- Matchers.h ---------------------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10/// This file contains the code related to the GlobalISel Matchers, which
11/// are the data structures used to model amd optimize state machines that
12/// can be emitted as "match tables" (see MatchTable.h/.cpp).
13///
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_UTILS_TABLEGEN_COMMON_GLOBALISEL_MATCHTABLE_MATCHERS_H
17#define LLVM_UTILS_TABLEGEN_COMMON_GLOBALISEL_MATCHTABLE_MATCHERS_H
18
19#include "Common/CodeGenDAGPatterns.h"
20#include "MatchTable.h"
21#include "Types.h"
22#include "llvm/ADT/ArrayRef.h"
23#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/MapVector.h"
25#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/StringMap.h"
27#include "llvm/ADT/StringRef.h"
28#include "llvm/CodeGenTypes/LowLevelType.h"
29#include "llvm/Support/Error.h"
30#include "llvm/Support/SaveAndRestore.h"
31#include <deque>
32#include <list>
33#include <map>
34#include <memory>
35#include <optional>
36#include <set>
37#include <string>
38#include <vector>
39
40namespace llvm {
41
42class raw_ostream;
43class Record;
44class SMLoc;
45class CodeGenRegisterClass;
46
47namespace gi {
48class MatchTable;
49class Matcher;
50class RuleMatcher;
51class OperandMatcher;
52class MatchAction;
53class PredicateMatcher;
54class InstructionMatcher;
55
56enum {
57 GISF_IgnoreCopies = 0x1,
58};
59
60using GISelFlags = std::uint32_t;
61
62//===- Helper functions ---------------------------------------------------===//
63
64/// Takes a sequence of \p Rules and group them based on the predicates
65/// they share. \p MatcherStorage is used as a memory container
66/// for the group that are created as part of this process.
67///
68/// Example of GroupMatcher formation via this function:
69/// \verbatim
70/// # R1
71/// # predicate A
72/// # predicate B
73/// ...
74/// # R2
75/// # predicate A // <-- effectively this is going to be checked twice.
76/// // Once in R1 and once in R2.
77/// # predicate C
78/// \endverbatim
79/// Output with optimization:
80/// \verbatim
81/// # Group1_2
82/// # predicate A // <-- Check is now shared.
83/// # R1
84/// # predicate B
85/// # R2
86/// # predicate C
87/// \endverbatim
88std::vector<Matcher *>
89optimizeRuleset(MutableArrayRef<RuleMatcher> Rules,
90 std::vector<std::unique_ptr<Matcher>> &MatcherStorage);
91
92/// Build a MatchTable for emission from \p Rules
93MatchTable buildMatchTable(ArrayRef<Matcher *> Rules, bool WithCoverage,
94 bool IsCombiner = false);
95
96//===- Matchers -----------------------------------------------------------===//
97class Matcher {
98public:
99 enum MatcherKind {
100 MK_Group,
101 MK_Switch,
102 MK_Rule,
103 };
104
105 Matcher(MatcherKind Kind) : Kind(Kind) {}
106 virtual ~Matcher();
107
108 MatcherKind getKind() const { return Kind; }
109
110 virtual void optimize();
111 virtual void emit(MatchTable &Table) = 0;
112
113 virtual bool hasFirstCondition() const = 0;
114 virtual const PredicateMatcher &getFirstCondition() const = 0;
115 virtual LLTCodeGen getFirstConditionAsRootType() const = 0;
116 virtual std::unique_ptr<PredicateMatcher> popFirstCondition() = 0;
117
118 /// Check recursively if the matcher records named operands for use in C++
119 /// predicates.
120 virtual bool recordsOperand() const = 0;
121
122private:
123 MatcherKind Kind;
124};
125
126class GroupMatcher final : public Matcher {
127 /// Conditions that form a common prefix of all the matchers contained.
128 SmallVector<std::unique_ptr<PredicateMatcher>, 1> Conditions;
129
130 /// All the nested matchers, sharing a common prefix.
131 std::vector<Matcher *> Matchers;
132
133 /// An owning collection for any auxiliary matchers created while optimizing
134 /// nested matchers contained.
135 std::vector<std::unique_ptr<Matcher>> MatcherStorage;
136
137public:
138 GroupMatcher() : Matcher(MK_Group) {}
139
140 static bool classof(const Matcher *M) { return M->getKind() == MK_Group; }
141
142 /// Add a matcher to the collection of nested matchers if it meets the
143 /// requirements, and return true. If it doesn't, do nothing and return false.
144 ///
145 /// Expected to preserve its argument, so it could be moved out later on.
146 bool addMatcher(Matcher &Candidate);
147
148 /// Mark the matcher as fully-built and ensure any invariants expected by both
149 /// optimize() and emit(...) methods. Generally, both sequences of calls
150 /// are expected to lead to a sensible result:
151 ///
152 /// addMatcher(...)*; finalize(); optimize(); emit(...); and
153 /// addMatcher(...)*; finalize(); emit(...);
154 ///
155 /// or generally
156 ///
157 /// addMatcher(...)*; finalize(); { optimize()*; emit(...); }*
158 ///
159 /// Multiple calls to optimize() are expected to be handled gracefully, though
160 /// optimize() is not expected to be idempotent. Multiple calls to finalize()
161 /// aren't generally supported. emit(...) is expected to be non-mutating and
162 /// producing the exact same results upon repeated calls.
163 ///
164 /// addMatcher() calls after the finalize() call are not supported.
165 ///
166 /// finalize() and optimize() are both allowed to mutate the contained
167 /// matchers, so moving them out after finalize() is not supported.
168 void finalize();
169 void optimize() override;
170 void emit(MatchTable &Table) override;
171
172 /// Could be used to move out the matchers added previously, unless finalize()
173 /// has been already called. If any of the matchers are moved out, the group
174 /// becomes safe to destroy, but not safe to re-use for anything else.
175 iterator_range<std::vector<Matcher *>::iterator> matchers() {
176 return Matchers;
177 }
178 size_t size() const { return Matchers.size(); }
179 bool empty() const { return Matchers.empty(); }
180
181 std::unique_ptr<PredicateMatcher> popFirstCondition() override;
182 const PredicateMatcher &getFirstCondition() const override {
183 assert(!Conditions.empty() &&
184 "Trying to get a condition from a condition-less group");
185 return *Conditions.front();
186 }
187 LLTCodeGen getFirstConditionAsRootType() const override;
188 bool hasFirstCondition() const override { return !Conditions.empty(); }
189
190 bool recordsOperand() const override;
191
192private:
193 /// See if a candidate matcher could be added to this group solely by
194 /// analyzing its first condition.
195 bool candidateConditionMatches(const PredicateMatcher &Predicate) const;
196};
197
198/// MatchTableRecord and associated value, for jump table generation.
199struct RecordAndValue {
200 MatchTableRecord Record;
201 int64_t RawValue;
202
203 RecordAndValue(MatchTableRecord Record,
204 int64_t RawValue = std::numeric_limits<int64_t>::min())
205 : Record(std::move(Record)), RawValue(RawValue) {}
206
207 bool operator<(const RecordAndValue &Other) const {
208 return RawValue < Other.RawValue;
209 }
210};
211
212class SwitchMatcher : public Matcher {
213 /// All the nested matchers, representing switch-cases. The first conditions
214 /// (as Matcher::getFirstCondition() reports) of all the nested matchers must
215 /// share the same type and path to a value they check, in other words, be
216 /// isIdenticalDownToValue. Multiple matchers can share the same value and are
217 /// bucketed together.
218 std::vector<Matcher *> Matchers;
219
220 /// The representative condition, with a type and a path (InsnVarID and OpIdx
221 /// in most cases) shared by all the matchers contained.
222 std::unique_ptr<PredicateMatcher> Condition;
223
224 struct Bucket {
225 RecordAndValue Value;
226 std::vector<Matcher *> Matchers;
227
228 explicit Bucket(RecordAndValue Value) : Value(std::move(Value)) {}
229 };
230
231 /// Buckets of matchers keyed by their case value.
232 std::map<int64_t, Bucket> Buckets;
233
234 /// An owning collection for any auxiliary matchers created while optimizing
235 /// nested matchers contained.
236 std::vector<std::unique_ptr<Matcher>> MatcherStorage;
237
238public:
239 SwitchMatcher();
240 ~SwitchMatcher();
241
242 static bool classof(const Matcher *M) { return M->getKind() == MK_Switch; }
243
244 bool addMatcher(Matcher &Candidate);
245
246 void finalize();
247 void emit(MatchTable &Table) override;
248
249 iterator_range<std::vector<Matcher *>::iterator> matchers() {
250 return make_range(x: Matchers.begin(), y: Matchers.end());
251 }
252 size_t size() const { return Matchers.size(); }
253 bool empty() const { return Matchers.empty(); }
254
255 std::unique_ptr<PredicateMatcher> popFirstCondition() override {
256 // SwitchMatcher doesn't have a common first condition for its cases, as all
257 // the cases only share a kind of a value (a type and a path to it) they
258 // match, but deliberately differ in the actual value they match.
259 llvm_unreachable("Trying to pop a condition from a condition-less group");
260 }
261
262 const PredicateMatcher &getFirstCondition() const override {
263 llvm_unreachable("Trying to get a condition from a condition-less group");
264 }
265
266 LLTCodeGen getFirstConditionAsRootType() const override {
267 llvm_unreachable("Trying to get a condition from a condition-less group");
268 }
269
270 bool hasFirstCondition() const override { return false; }
271
272 bool recordsOperand() const override;
273
274private:
275 /// See if the predicate type has a Switch-implementation for it.
276 static bool isSupportedPredicateType(const PredicateMatcher &Predicate);
277
278 bool candidateConditionMatches(const PredicateMatcher &Predicate) const;
279
280 /// emit()-helper
281 static void emitPredicateSpecificOpcodes(const PredicateMatcher &P,
282 MatchTable &Table);
283};
284
285/// Generates code to check that a match rule matches.
286class RuleMatcher : public Matcher {
287public:
288 using ActionList = std::list<std::unique_ptr<MatchAction>>;
289 using action_iterator = ActionList::iterator;
290
291protected:
292 std::vector<std::unique_ptr<InstructionMatcher>> InsnMatchers;
293
294 /// A list of matchers that all need to succeed for the current rule to match.
295 /// FIXME: This currently supports a single match position but could be
296 /// extended to support multiple positions to support div/rem fusion or
297 /// load-multiple instructions.
298 using RootsTy = SmallVector<InstructionMatcher *, 1>;
299 RootsTy Roots;
300
301 /// A list of actions that need to be taken when all predicates in this rule
302 /// have succeeded.
303 ActionList Actions;
304
305 /// Combiners can sometimes just run C++ code to finish matching a rule &
306 /// mutate instructions instead of relying on MatchActions. Empty if unused.
307 std::string CustomCXXAction;
308
309 using MutatableInsnSet = SmallPtrSet<InstructionMatcher *, 4>;
310
311 // The set of instruction matchers that have not yet been claimed for mutation
312 // by a BuildMI.
313 MutatableInsnSet MutatableInsns;
314
315 /// A map of named operands defined by the matchers that may be referenced by
316 /// the renderers.
317 StringMap<OperandMatcher *> DefinedOperands;
318
319 using PhysRegOperandsTy = SmallMapVector<const Record *, OperandMatcher *, 1>;
320
321 /// A map of anonymous physical register operands defined by the matchers that
322 /// may be referenced by the renderers.
323 PhysRegOperandsTy PhysRegOperands;
324
325 /// ID for the next output instruction allocated with allocateOutputInsnID()
326 unsigned NextOutputInsnID = 0;
327
328 /// ID for the next temporary register ID allocated with allocateTempRegID()
329 unsigned NextTempRegID = 0;
330
331 /// ID for the next recorded type. Starts at -1 and counts down.
332 TempTypeIdx NextTempTypeIdx = -1;
333
334 // HwMode predicate index for this rule. -1 if no HwMode.
335 int HwModeIdx = -1;
336
337 /// Current GISelFlags
338 GISelFlags Flags = 0;
339
340 /// Whether the back-end that emitted this RuleMatcher relies on
341 /// RecordNamedOperandMatcher for C++ code to access instruction operands.
342 /// When false, it means the back-end uses other means that we do not know
343 /// about and we thus need to assume ANY operand can be accessed by ANY C++
344 /// code (GenericInstructionPredicateMatcher)
345 bool UsesRecordOperand = true;
346
347 std::vector<std::string> RequiredSimplePredicates;
348 std::vector<const Record *> RequiredFeatures;
349 std::vector<std::unique_ptr<PredicateMatcher>> EpilogueMatchers;
350
351 DenseSet<unsigned> ErasedInsnIDs;
352
353 ArrayRef<SMLoc> SrcLoc;
354
355 using DefinedComplexPatternSubOperand =
356 std::tuple<const Record *, unsigned, unsigned>;
357 using DefinedComplexPatternSubOperandMap =
358 StringMap<DefinedComplexPatternSubOperand>;
359 /// A map of Symbolic Names to ComplexPattern sub-operands.
360 DefinedComplexPatternSubOperandMap ComplexSubOperands;
361 /// A map used to for multiple referenced error check of ComplexSubOperand.
362 /// ComplexSubOperand can't be referenced multiple from different operands,
363 /// however multiple references from same operand are allowed since that is
364 /// how 'same operand checks' are generated.
365 StringMap<std::string> ComplexSubOperandsParentName;
366
367 uint64_t RuleID;
368 static uint64_t NextRuleID;
369
370 GISelFlags updateGISelFlag(GISelFlags CurFlags, const Record *R,
371 StringRef FlagName, GISelFlags FlagBit);
372
373 friend class InstructionOperandMatcher;
374
375 InstructionMatcher &allocateInstructionMatcher(StringRef SymbolicName,
376 bool AllowNumOpsCheck = true);
377
378public:
379 RuleMatcher(ArrayRef<SMLoc> SrcLoc, bool UsesRecordOperand = true);
380 RuleMatcher(RuleMatcher &&Other) = default;
381 RuleMatcher &operator=(RuleMatcher &&Other) = default;
382
383 static bool classof(const Matcher *M) { return M->getKind() == MK_Rule; }
384
385 TempTypeIdx getNextTempTypeIdx() { return NextTempTypeIdx--; }
386
387 uint64_t getRuleID() const { return RuleID; }
388
389 InstructionMatcher &addInstructionMatcher(StringRef SymbolicName);
390 void addRequiredFeature(const Record *Feature) {
391 RequiredFeatures.push_back(x: Feature);
392 }
393 ArrayRef<const Record *> getRequiredFeatures() const {
394 return RequiredFeatures;
395 }
396
397 bool usesRecordOperand() const { return UsesRecordOperand; }
398
399 void addHwModeIdx(unsigned Idx) { HwModeIdx = Idx; }
400 int getHwModeIdx() const { return HwModeIdx; }
401
402 void addRequiredSimplePredicate(StringRef PredName);
403 const std::vector<std::string> &getRequiredSimplePredicates();
404
405 /// Attempts to mark \p ID as erased (GIR_EraseFromParent called on it).
406 /// If \p ID has already been erased, returns false and GIR_EraseFromParent
407 /// should NOT be emitted.
408 bool tryEraseInsnID(unsigned ID) { return ErasedInsnIDs.insert(V: ID).second; }
409
410 void setCustomCXXAction(StringRef FnEnumName) {
411 CustomCXXAction = FnEnumName.str();
412 }
413
414 // Emplaces an action of the specified Kind at the end of the action list.
415 //
416 // Returns a reference to the newly created action.
417 //
418 // Like std::vector::emplace_back(), may invalidate all iterators if the new
419 // size exceeds the capacity. Otherwise, only invalidates the past-the-end
420 // iterator.
421 template <class Kind, class... Args> Kind &addAction(Args &&...args) {
422 Actions.emplace_back(std::make_unique<Kind>(std::forward<Args>(args)...));
423 return *static_cast<Kind *>(Actions.back().get());
424 }
425
426 // Emplaces an action of the specified Kind before the given insertion point.
427 //
428 // Returns an iterator pointing at the newly created instruction.
429 //
430 // Like std::vector::insert(), may invalidate all iterators if the new size
431 // exceeds the capacity. Otherwise, only invalidates the iterators from the
432 // insertion point onwards.
433 template <class Kind, class... Args>
434 action_iterator insertAction(action_iterator InsertPt, Args &&...args) {
435 return Actions.emplace(InsertPt,
436 std::make_unique<Kind>(std::forward<Args>(args)...));
437 }
438
439 void setPermanentGISelFlags(GISelFlags V) { Flags = V; }
440
441 // Update the active GISelFlags based on the GISelFlags Record R.
442 // A SaveAndRestore object is returned so the old GISelFlags are restored
443 // at the end of the scope.
444 SaveAndRestore<GISelFlags> setGISelFlags(const Record *R);
445 GISelFlags getGISelFlags() const { return Flags; }
446
447 MutatableInsnSet::const_iterator mutatable_insns_begin() const {
448 return MutatableInsns.begin();
449 }
450 MutatableInsnSet::const_iterator mutatable_insns_end() const {
451 return MutatableInsns.end();
452 }
453 iterator_range<MutatableInsnSet::const_iterator> mutatable_insns() const {
454 return make_range(x: mutatable_insns_begin(), y: mutatable_insns_end());
455 }
456 void reserveInsnMatcherForMutation(InstructionMatcher *InsnMatcher) {
457 bool R = MutatableInsns.erase(Ptr: InsnMatcher);
458 assert(R && "Reserving a mutatable insn that isn't available");
459 (void)R;
460 }
461
462 auto all_instmatchers() const {
463 return make_range(x: InsnMatchers.begin(), y: InsnMatchers.end());
464 }
465
466 action_iterator actions_begin() { return Actions.begin(); }
467 action_iterator actions_end() { return Actions.end(); }
468 iterator_range<action_iterator> actions() {
469 return make_range(x: actions_begin(), y: actions_end());
470 }
471
472 bool hasOperand(StringRef SymbolicName) const {
473 return DefinedOperands.contains(Key: SymbolicName);
474 }
475
476 void defineOperand(StringRef SymbolicName, OperandMatcher &OM);
477
478 void definePhysRegOperand(const Record *Reg, OperandMatcher &OM);
479
480 Error defineComplexSubOperand(StringRef SymbolicName,
481 const Record *ComplexPattern,
482 unsigned RendererID, unsigned SubOperandID,
483 StringRef ParentSymbolicName);
484
485 std::optional<DefinedComplexPatternSubOperand>
486 getComplexSubOperand(StringRef SymbolicName) const {
487 const auto &I = ComplexSubOperands.find(Key: SymbolicName);
488 if (I == ComplexSubOperands.end())
489 return std::nullopt;
490 return I->second;
491 }
492
493 InstructionMatcher &getInstructionMatcher(StringRef SymbolicName) const;
494 OperandMatcher &getOperandMatcher(StringRef Name);
495 const OperandMatcher &getOperandMatcher(StringRef Name) const;
496 const OperandMatcher &getPhysRegOperandMatcher(const Record *) const;
497
498 void optimize() override;
499 void emit(MatchTable &Table) override;
500
501 bool recordsOperand() const override;
502
503 /// Compare the priority of this object and B.
504 ///
505 /// Returns true if this object is more important than B.
506 bool isHigherPriorityThan(const RuleMatcher &B) const;
507
508 /// Report the maximum number of temporary operands needed by the rule
509 /// matcher.
510 unsigned countRendererFns() const;
511
512 std::unique_ptr<PredicateMatcher> popFirstCondition() override;
513 const PredicateMatcher &getFirstCondition() const override;
514 LLTCodeGen getFirstConditionAsRootType() const override;
515 bool hasFirstCondition() const override;
516 StringRef getOpcode() const;
517
518 // FIXME: Remove this as soon as possible
519 InstructionMatcher &roots_front() const { return *Roots.front(); }
520
521 unsigned allocateOutputInsnID() { return NextOutputInsnID++; }
522 unsigned allocateTempRegID() { return NextTempRegID++; }
523
524 iterator_range<PhysRegOperandsTy::const_iterator> physoperands() const {
525 return make_range(x: PhysRegOperands.begin(), y: PhysRegOperands.end());
526 }
527
528 iterator_range<RootsTy::iterator> roots() { return Roots; }
529 bool roots_empty() const { return Roots.empty(); }
530 void roots_pop_front();
531};
532
533template <class PredicateTy> class PredicateListMatcher {
534private:
535 /// Template instantiations should specialize this to return a string to use
536 /// for the comment emitted when there are no predicates.
537 std::string getNoPredicateComment() const;
538
539protected:
540 using PredicatesTy = std::deque<std::unique_ptr<PredicateTy>>;
541 PredicatesTy Predicates;
542
543 /// Track if the list of predicates was manipulated by one of the optimization
544 /// methods.
545 bool Optimized = false;
546
547public:
548 typename PredicatesTy::iterator predicates_begin() {
549 return Predicates.begin();
550 }
551 typename PredicatesTy::iterator predicates_end() { return Predicates.end(); }
552 iterator_range<typename PredicatesTy::iterator> predicates() {
553 return make_range(predicates_begin(), predicates_end());
554 }
555 typename PredicatesTy::size_type predicates_size() const {
556 return Predicates.size();
557 }
558 bool predicates_empty() const { return Predicates.empty(); }
559
560 template <typename Ty> bool contains() const {
561 return any_of(Predicates, [&](auto &P) { return isa<Ty>(P.get()); });
562 }
563
564 std::unique_ptr<PredicateTy> predicates_pop_front() {
565 std::unique_ptr<PredicateTy> Front = std::move(Predicates.front());
566 Predicates.pop_front();
567 Optimized = true;
568 return Front;
569 }
570
571 void prependPredicate(std::unique_ptr<PredicateTy> &&Predicate) {
572 Predicates.push_front(std::move(Predicate));
573 }
574
575 void eraseNullPredicates() {
576 const auto NewEnd =
577 std::stable_partition(Predicates.begin(), Predicates.end(),
578 std::logical_not<std::unique_ptr<PredicateTy>>());
579 if (NewEnd != Predicates.begin()) {
580 Predicates.erase(Predicates.begin(), NewEnd);
581 Optimized = true;
582 }
583 }
584
585 /// Emit MatchTable opcodes that tests whether all the predicates are met.
586 template <class... Args>
587 void emitPredicateListOpcodes(MatchTable &Table, Args &&...args) {
588 if (Predicates.empty() && !Optimized) {
589 Table << MatchTable::Comment(Comment: getNoPredicateComment())
590 << MatchTable::LineBreak;
591 return;
592 }
593
594 for (const auto &Predicate : predicates())
595 Predicate->emitPredicateOpcodes(Table, std::forward<Args>(args)...);
596 }
597
598 /// Provide a function to avoid emitting certain predicates. This is used to
599 /// defer some predicate checks until after others
600 using PredicateFilterFunc = std::function<bool(const PredicateTy &)>;
601
602 /// Emit MatchTable opcodes for predicates which satisfy \p
603 /// ShouldEmitPredicate. This should be called multiple times to ensure all
604 /// predicates are eventually added to the match table.
605 template <class... Args>
606 void emitFilteredPredicateListOpcodes(PredicateFilterFunc ShouldEmitPredicate,
607 MatchTable &Table, Args &&...args) {
608 if (Predicates.empty() && !Optimized) {
609 Table << MatchTable::Comment(Comment: getNoPredicateComment())
610 << MatchTable::LineBreak;
611 return;
612 }
613
614 for (const auto &Predicate : predicates()) {
615 if (ShouldEmitPredicate(*Predicate))
616 Predicate->emitPredicateOpcodes(Table, std::forward<Args>(args)...);
617 }
618 }
619};
620
621class PredicateMatcher {
622public:
623 /// This enum is used for RTTI and also defines the priority that is given to
624 /// the predicate when generating the matcher code. Kinds with higher priority
625 /// must be tested first.
626 ///
627 /// The relative priority of OPM_LLT, OPM_RegBank, and OPM_MBB do not matter
628 /// but OPM_Int must have priority over OPM_RegBank since constant integers
629 /// are represented by a virtual register defined by a G_CONSTANT instruction.
630 ///
631 /// Note: The relative priority between IPM_ and OPM_ does not matter, they
632 /// are currently not compared between each other.
633 enum PredicateKind {
634 IPM_Opcode,
635 IPM_NumOperands,
636 IPM_ImmPredicate,
637 IPM_AtomicOrderingMMO,
638 IPM_MemoryLLTSize,
639 IPM_MemoryVsLLTSize,
640 IPM_MemoryAddressSpace,
641 IPM_MemoryAlignment,
642 IPM_VectorSplatImm,
643 IPM_NoUse,
644 IPM_OneUse,
645 IPM_GenericPredicate,
646 IPM_MIFlags,
647
648 OPM_LeafPredicate,
649 OPM_ImmPredicate,
650 OPM_Imm,
651 OPM_SameOperand,
652 OPM_ComplexPattern,
653 OPM_IntrinsicID,
654 OPM_CmpPredicate,
655 OPM_Instruction,
656 OPM_Int,
657 OPM_LiteralInt,
658 OPM_LLT,
659 OPM_LLTShape,
660 OPM_PointerToAny,
661 OPM_RegBank,
662 OPM_MBB,
663 OPM_RecordNamedOperand,
664 OPM_RecordRegType,
665 };
666
667protected:
668 PredicateKind Kind;
669 unsigned InsnVarID;
670 unsigned OpIdx;
671
672public:
673 PredicateMatcher(PredicateKind Kind, unsigned InsnVarID, unsigned OpIdx = ~0)
674 : Kind(Kind), InsnVarID(InsnVarID), OpIdx(OpIdx) {}
675 virtual ~PredicateMatcher();
676
677 unsigned getInsnVarID() const { return InsnVarID; }
678 unsigned getOpIdx() const { return OpIdx; }
679
680 /// Emit MatchTable opcodes that check the predicate for the given operand.
681 virtual void emitPredicateOpcodes(MatchTable &Table) const = 0;
682
683 PredicateKind getKind() const { return Kind; }
684
685 bool dependsOnRecordedOperands() const {
686 // Custom predicates really depend on the context pattern of the
687 // instruction, not just the individual instruction. This therefore
688 // implicitly depends on all other pattern constraints.
689 return Kind == IPM_GenericPredicate;
690 }
691
692 /// \param M A Matcher that contains this PredicateMatcher.
693 /// \returns true if this PredicateMatcher can be hoisted outside of \p M.
694 virtual bool canHoistOutsideOf(const Matcher &M) const { return true; }
695
696 bool recordsOperand() const { return Kind == OPM_RecordNamedOperand; }
697
698 virtual bool isIdentical(const PredicateMatcher &B) const {
699 return B.getKind() == getKind() && InsnVarID == B.InsnVarID &&
700 OpIdx == B.OpIdx;
701 }
702
703 virtual bool isIdenticalDownToValue(const PredicateMatcher &B) const {
704 return hasValue() && PredicateMatcher::isIdentical(B);
705 }
706
707 virtual RecordAndValue getValue() const {
708 assert(hasValue() && "Can not get a value of a value-less predicate!");
709 llvm_unreachable("Not implemented yet");
710 }
711 virtual bool hasValue() const { return false; }
712
713 /// Report the maximum number of temporary operands needed by the predicate
714 /// matcher.
715 virtual unsigned countRendererFns() const { return 0; }
716};
717
718/// Generates code to check a predicate of an operand.
719///
720/// Typical predicates include:
721/// * Operand is a particular register.
722/// * Operand is assigned a particular register bank.
723/// * Operand is an MBB.
724class OperandPredicateMatcher : public PredicateMatcher {
725public:
726 OperandPredicateMatcher(PredicateKind Kind, unsigned InsnVarID,
727 unsigned OpIdx)
728 : PredicateMatcher(Kind, InsnVarID, OpIdx) {}
729 ~OperandPredicateMatcher() override;
730
731 /// Compare the priority of this object and B.
732 ///
733 /// Returns true if this object is more important than B.
734 virtual bool isHigherPriorityThan(const OperandPredicateMatcher &B) const;
735};
736
737template <>
738inline std::string
739PredicateListMatcher<OperandPredicateMatcher>::getNoPredicateComment() const {
740 return "No operand predicates";
741}
742
743/// Generates code to check that a register operand is defined by the same exact
744/// one as another.
745class SameOperandMatcher : public OperandPredicateMatcher {
746 unsigned OtherInsnID;
747 unsigned OtherOpIdx;
748
749 GISelFlags Flags;
750
751public:
752 SameOperandMatcher(unsigned InsnVarID, unsigned OpIdx, unsigned OtherInsnID,
753 unsigned OtherOpIdx, GISelFlags Flags)
754 : OperandPredicateMatcher(OPM_SameOperand, InsnVarID, OpIdx),
755 OtherInsnID(OtherInsnID), OtherOpIdx(OtherOpIdx), Flags(Flags) {}
756
757 static bool classof(const PredicateMatcher *P) {
758 return P->getKind() == OPM_SameOperand;
759 }
760
761 void emitPredicateOpcodes(MatchTable &Table) const override;
762
763 bool isIdentical(const PredicateMatcher &B) const override {
764 return OperandPredicateMatcher::isIdentical(B) &&
765 OtherInsnID == cast<SameOperandMatcher>(Val: &B)->OtherInsnID &&
766 OtherOpIdx == cast<SameOperandMatcher>(Val: &B)->OtherOpIdx;
767 }
768
769 virtual bool canHoistOutsideOf(const Matcher &M) const override {
770 // We can only hoist these if they only refer to the root instruction.
771 // We do not support hoisting predicates on non-root instructions.
772 return OtherInsnID == 0 && InsnVarID == 0;
773 }
774};
775
776/// Generates code to check that an operand is a particular LLT.
777class LLTOperandMatcher : public OperandPredicateMatcher {
778protected:
779 LLTCodeGen Ty;
780 /// If set, the type depends on the HwMode and Ty is unused.
781 std::optional<LLTByHwMode> HwModeTy;
782
783 LLTOperandMatcher(PredicateKind Kind, unsigned InsnVarID, unsigned OpIdx,
784 const LLTCodeGen &Ty)
785 : OperandPredicateMatcher(Kind, InsnVarID, OpIdx), Ty(Ty) {
786 KnownTypes.insert(x: Ty);
787 }
788
789 LLTOperandMatcher(PredicateKind Kind, unsigned InsnVarID, unsigned OpIdx,
790 const LLTByHwMode &Ty)
791 : OperandPredicateMatcher(Kind, InsnVarID, OpIdx), HwModeTy(Ty) {}
792
793public:
794 static std::map<LLTCodeGen, unsigned> TypeIDValues;
795
796 static void initTypeIDValuesMap() {
797 TypeIDValues.clear();
798
799 unsigned ID = 0;
800 for (const LLTCodeGen &LLTy : KnownTypes)
801 TypeIDValues[LLTy] = ID++;
802 }
803
804 LLTOperandMatcher(unsigned InsnVarID, unsigned OpIdx, const LLTCodeGen &Ty)
805 : LLTOperandMatcher(OPM_LLT, InsnVarID, OpIdx, Ty) {}
806 LLTOperandMatcher(unsigned InsnVarID, unsigned OpIdx, const LLTByHwMode &Ty)
807 : LLTOperandMatcher(OPM_LLT, InsnVarID, OpIdx, Ty) {}
808
809 static bool classof(const PredicateMatcher *P) {
810 return P->getKind() == OPM_LLT;
811 }
812
813 bool isIdentical(const PredicateMatcher &B) const override {
814 return OperandPredicateMatcher::isIdentical(B) &&
815 Ty == cast<LLTOperandMatcher>(Val: &B)->Ty &&
816 HwModeTy == cast<LLTOperandMatcher>(Val: &B)->HwModeTy;
817 }
818
819 RecordAndValue getValue() const override;
820 bool hasValue() const override;
821
822 /// Returns the type. Returns an invalid LLT if the type depends on HwMode.
823 LLTCodeGen getTy() const { return Ty; }
824 bool isHwModeDependent() const { return HwModeTy.has_value(); }
825
826 void emitPredicateOpcodes(MatchTable &Table) const override;
827};
828
829/// Generates code to check that the element count & element sizes are the same.
830class LLTOperandShapeMatcher : public LLTOperandMatcher {
831 static ElementCount getShapeElementCount(const LLT &Ty) {
832 return Ty.isVector() ? Ty.getElementCount() : ElementCount::getFixed(MinVal: 1);
833 }
834
835 static unsigned getShapeScalarSizeInBits(const LLT &Ty) {
836 return Ty.getScalarSizeInBits();
837 }
838
839public:
840 LLTOperandShapeMatcher(unsigned InsnVarID, unsigned OpIdx,
841 const LLTCodeGen &Ty)
842 : LLTOperandMatcher(OPM_LLTShape, InsnVarID, OpIdx, Ty) {}
843
844 static bool classof(const PredicateMatcher *P) {
845 return P->getKind() == OPM_LLTShape;
846 }
847
848 LLTOperandShapeMatcher(unsigned InsnVarID, unsigned OpIdx,
849 const LLTByHwMode &Ty)
850 : LLTOperandMatcher(OPM_LLTShape, InsnVarID, OpIdx, Ty) {}
851
852 bool isIdentical(const PredicateMatcher &B) const override {
853 if (!OperandPredicateMatcher::isIdentical(B))
854 return false;
855 if (isHwModeDependent() ||
856 cast<LLTOperandShapeMatcher>(Val: &B)->isHwModeDependent())
857 return HwModeTy == cast<LLTOperandShapeMatcher>(Val: &B)->HwModeTy;
858 return getShapeElementCount(Ty: Ty.get()) ==
859 getShapeElementCount(
860 Ty: cast<LLTOperandShapeMatcher>(Val: &B)->Ty.get()) &&
861 getShapeScalarSizeInBits(Ty: Ty.get()) ==
862 getShapeScalarSizeInBits(
863 Ty: cast<LLTOperandShapeMatcher>(Val: &B)->Ty.get());
864 }
865};
866
867/// Generates code to check that an operand is a pointer to any address space.
868///
869/// In SelectionDAG, the types did not describe pointers or address spaces. As a
870/// result, iN is used to describe a pointer of N bits to any address space and
871/// PatFrag predicates are typically used to constrain the address space.
872/// There's no reliable means to derive the missing type information from the
873/// pattern so imported rules must test the components of a pointer separately.
874///
875/// If SizeInBits is zero, then the pointer size will be obtained from the
876/// subtarget.
877class PointerToAnyOperandMatcher : public OperandPredicateMatcher {
878protected:
879 unsigned SizeInBits = 0;
880 /// If set, the pointer size is the size of this type for the HwMode and
881 /// SizeInBits is unused.
882 std::optional<LLTByHwMode> HwModeTy;
883
884public:
885 PointerToAnyOperandMatcher(unsigned InsnVarID, unsigned OpIdx,
886 unsigned SizeInBits)
887 : OperandPredicateMatcher(OPM_PointerToAny, InsnVarID, OpIdx),
888 SizeInBits(SizeInBits) {}
889 PointerToAnyOperandMatcher(unsigned InsnVarID, unsigned OpIdx,
890 const LLTByHwMode &Ty)
891 : OperandPredicateMatcher(OPM_PointerToAny, InsnVarID, OpIdx),
892 HwModeTy(Ty) {}
893
894 static bool classof(const PredicateMatcher *P) {
895 return P->getKind() == OPM_PointerToAny;
896 }
897
898 bool isIdentical(const PredicateMatcher &B) const override {
899 return OperandPredicateMatcher::isIdentical(B) &&
900 SizeInBits == cast<PointerToAnyOperandMatcher>(Val: &B)->SizeInBits &&
901 HwModeTy == cast<PointerToAnyOperandMatcher>(Val: &B)->HwModeTy;
902 }
903
904 void emitPredicateOpcodes(MatchTable &Table) const override;
905};
906
907/// Generates code to record named operand in RecordedOperands list at StoreIdx.
908/// Predicates with 'let PredicateCodeUsesOperands = 1' get RecordedOperands as
909/// an argument to predicate's c++ code once all operands have been matched.
910class RecordNamedOperandMatcher : public OperandPredicateMatcher {
911protected:
912 unsigned StoreIdx;
913 std::string Name;
914
915public:
916 RecordNamedOperandMatcher(unsigned InsnVarID, unsigned OpIdx,
917 unsigned StoreIdx, StringRef Name)
918 : OperandPredicateMatcher(OPM_RecordNamedOperand, InsnVarID, OpIdx),
919 StoreIdx(StoreIdx), Name(Name) {}
920
921 static bool classof(const PredicateMatcher *P) {
922 return P->getKind() == OPM_RecordNamedOperand;
923 }
924
925 bool isIdentical(const PredicateMatcher &B) const override {
926 return OperandPredicateMatcher::isIdentical(B) &&
927 StoreIdx == cast<RecordNamedOperandMatcher>(Val: &B)->StoreIdx &&
928 Name == cast<RecordNamedOperandMatcher>(Val: &B)->Name;
929 }
930
931 void emitPredicateOpcodes(MatchTable &Table) const override;
932};
933
934/// Generates code to store a register operand's type into the set of temporary
935/// LLTs.
936class RecordRegisterType : public OperandPredicateMatcher {
937protected:
938 TempTypeIdx Idx;
939
940public:
941 RecordRegisterType(unsigned InsnVarID, unsigned OpIdx, TempTypeIdx Idx)
942 : OperandPredicateMatcher(OPM_RecordRegType, InsnVarID, OpIdx), Idx(Idx) {
943 }
944
945 static bool classof(const PredicateMatcher *P) {
946 return P->getKind() == OPM_RecordRegType;
947 }
948
949 bool isIdentical(const PredicateMatcher &B) const override {
950 return OperandPredicateMatcher::isIdentical(B) &&
951 Idx == cast<RecordRegisterType>(Val: &B)->Idx;
952 }
953
954 void emitPredicateOpcodes(MatchTable &Table) const override;
955};
956
957/// Generates code to check that an operand is a particular target constant.
958class ComplexPatternOperandMatcher : public OperandPredicateMatcher {
959protected:
960 const OperandMatcher &Operand;
961 const Record &TheDef;
962
963 unsigned getAllocatedTemporariesBaseID() const;
964
965public:
966 bool isIdentical(const PredicateMatcher &B) const override { return false; }
967
968 ComplexPatternOperandMatcher(unsigned InsnVarID, unsigned OpIdx,
969 const OperandMatcher &Operand,
970 const Record &TheDef)
971 : OperandPredicateMatcher(OPM_ComplexPattern, InsnVarID, OpIdx),
972 Operand(Operand), TheDef(TheDef) {}
973
974 static bool classof(const PredicateMatcher *P) {
975 return P->getKind() == OPM_ComplexPattern;
976 }
977
978 void emitPredicateOpcodes(MatchTable &Table) const override;
979 unsigned countRendererFns() const override { return 1; }
980};
981
982/// Generates code to check that an operand is in a particular register bank.
983class RegisterBankOperandMatcher : public OperandPredicateMatcher {
984protected:
985 const CodeGenRegisterClass &RC;
986
987public:
988 RegisterBankOperandMatcher(unsigned InsnVarID, unsigned OpIdx,
989 const CodeGenRegisterClass &RC)
990 : OperandPredicateMatcher(OPM_RegBank, InsnVarID, OpIdx), RC(RC) {}
991
992 bool isIdentical(const PredicateMatcher &B) const override;
993
994 static bool classof(const PredicateMatcher *P) {
995 return P->getKind() == OPM_RegBank;
996 }
997
998 void emitPredicateOpcodes(MatchTable &Table) const override;
999};
1000
1001/// Generates code to check that an operand is a basic block.
1002class MBBOperandMatcher : public OperandPredicateMatcher {
1003public:
1004 MBBOperandMatcher(unsigned InsnVarID, unsigned OpIdx)
1005 : OperandPredicateMatcher(OPM_MBB, InsnVarID, OpIdx) {}
1006
1007 static bool classof(const PredicateMatcher *P) {
1008 return P->getKind() == OPM_MBB;
1009 }
1010
1011 void emitPredicateOpcodes(MatchTable &Table) const override;
1012};
1013
1014class ImmOperandMatcher : public OperandPredicateMatcher {
1015public:
1016 ImmOperandMatcher(unsigned InsnVarID, unsigned OpIdx)
1017 : OperandPredicateMatcher(OPM_Imm, InsnVarID, OpIdx) {}
1018
1019 static bool classof(const PredicateMatcher *P) {
1020 return P->getKind() == OPM_Imm;
1021 }
1022
1023 void emitPredicateOpcodes(MatchTable &Table) const override;
1024};
1025
1026/// Generates code to check that an operand is a G_CONSTANT with a particular
1027/// int.
1028class ConstantIntOperandMatcher : public OperandPredicateMatcher {
1029protected:
1030 int64_t Value;
1031
1032public:
1033 ConstantIntOperandMatcher(unsigned InsnVarID, unsigned OpIdx, int64_t Value)
1034 : OperandPredicateMatcher(OPM_Int, InsnVarID, OpIdx), Value(Value) {}
1035
1036 bool isIdentical(const PredicateMatcher &B) const override {
1037 return OperandPredicateMatcher::isIdentical(B) &&
1038 Value == cast<ConstantIntOperandMatcher>(Val: &B)->Value;
1039 }
1040
1041 static bool classof(const PredicateMatcher *P) {
1042 return P->getKind() == OPM_Int;
1043 }
1044
1045 void emitPredicateOpcodes(MatchTable &Table) const override;
1046};
1047
1048/// Generates code to check that an operand is a raw int (where MO.isImm() or
1049/// MO.isCImm() is true).
1050class LiteralIntOperandMatcher : public OperandPredicateMatcher {
1051protected:
1052 int64_t Value;
1053
1054public:
1055 LiteralIntOperandMatcher(unsigned InsnVarID, unsigned OpIdx, int64_t Value)
1056 : OperandPredicateMatcher(OPM_LiteralInt, InsnVarID, OpIdx),
1057 Value(Value) {}
1058
1059 bool isIdentical(const PredicateMatcher &B) const override {
1060 return OperandPredicateMatcher::isIdentical(B) &&
1061 Value == cast<LiteralIntOperandMatcher>(Val: &B)->Value;
1062 }
1063
1064 static bool classof(const PredicateMatcher *P) {
1065 return P->getKind() == OPM_LiteralInt;
1066 }
1067
1068 void emitPredicateOpcodes(MatchTable &Table) const override;
1069};
1070
1071/// Generates code to check that an operand is an CmpInst predicate
1072class CmpPredicateOperandMatcher : public OperandPredicateMatcher {
1073protected:
1074 std::string PredName;
1075
1076public:
1077 CmpPredicateOperandMatcher(unsigned InsnVarID, unsigned OpIdx, std::string P)
1078 : OperandPredicateMatcher(OPM_CmpPredicate, InsnVarID, OpIdx),
1079 PredName(std::move(P)) {}
1080
1081 bool isIdentical(const PredicateMatcher &B) const override {
1082 return OperandPredicateMatcher::isIdentical(B) &&
1083 PredName == cast<CmpPredicateOperandMatcher>(Val: &B)->PredName;
1084 }
1085
1086 static bool classof(const PredicateMatcher *P) {
1087 return P->getKind() == OPM_CmpPredicate;
1088 }
1089
1090 void emitPredicateOpcodes(MatchTable &Table) const override;
1091};
1092
1093/// Generates code to check that an operand is an intrinsic ID.
1094class IntrinsicIDOperandMatcher : public OperandPredicateMatcher {
1095protected:
1096 const CodeGenIntrinsic *II;
1097
1098public:
1099 IntrinsicIDOperandMatcher(unsigned InsnVarID, unsigned OpIdx,
1100 const CodeGenIntrinsic *II)
1101 : OperandPredicateMatcher(OPM_IntrinsicID, InsnVarID, OpIdx), II(II) {}
1102
1103 bool isIdentical(const PredicateMatcher &B) const override {
1104 return OperandPredicateMatcher::isIdentical(B) &&
1105 II == cast<IntrinsicIDOperandMatcher>(Val: &B)->II;
1106 }
1107
1108 static bool classof(const PredicateMatcher *P) {
1109 return P->getKind() == OPM_IntrinsicID;
1110 }
1111
1112 void emitPredicateOpcodes(MatchTable &Table) const override;
1113};
1114
1115/// Generates code to check that this operand is an immediate whose value meets
1116/// an immediate predicate.
1117class OperandImmPredicateMatcher : public OperandPredicateMatcher {
1118protected:
1119 TreePredicateFn Predicate;
1120
1121public:
1122 OperandImmPredicateMatcher(unsigned InsnVarID, unsigned OpIdx,
1123 const TreePredicateFn &Predicate)
1124 : OperandPredicateMatcher(OPM_ImmPredicate, InsnVarID, OpIdx),
1125 Predicate(Predicate) {}
1126
1127 bool isIdentical(const PredicateMatcher &B) const override {
1128 return OperandPredicateMatcher::isIdentical(B) &&
1129 Predicate.getOrigPatFragRecord() ==
1130 cast<OperandImmPredicateMatcher>(Val: &B)
1131 ->Predicate.getOrigPatFragRecord();
1132 }
1133
1134 static bool classof(const PredicateMatcher *P) {
1135 return P->getKind() == OPM_ImmPredicate;
1136 }
1137
1138 void emitPredicateOpcodes(MatchTable &Table) const override;
1139};
1140
1141/// Generates code to check that this operand is a register whose value meets
1142/// the predicate.
1143class OperandLeafPredicateMatcher : public OperandPredicateMatcher {
1144protected:
1145 TreePredicateFn Predicate;
1146
1147public:
1148 OperandLeafPredicateMatcher(unsigned InsnVarID, unsigned OpIdx,
1149 const TreePredicateFn &Predicate)
1150 : OperandPredicateMatcher(OPM_LeafPredicate, InsnVarID, OpIdx),
1151 Predicate(Predicate) {}
1152
1153 static bool classof(const PredicateMatcher *P) {
1154 return P->getKind() == OPM_LeafPredicate;
1155 }
1156
1157 void emitPredicateOpcodes(MatchTable &Table) const override;
1158};
1159
1160/// Generates code to check that a set of predicates match for a particular
1161/// operand.
1162class OperandMatcher : public PredicateListMatcher<OperandPredicateMatcher> {
1163protected:
1164 InstructionMatcher &Insn;
1165 unsigned OpIdx;
1166 std::string SymbolicName;
1167
1168 /// The index of the first temporary variable allocated to this operand. The
1169 /// number of allocated temporaries can be found with
1170 /// countRendererFns().
1171 unsigned AllocatedTemporariesBaseID;
1172
1173 TempTypeIdx TTIdx = 0;
1174
1175 // TODO: has many implications, figure them all out
1176 bool IsVariadic = false;
1177
1178public:
1179 OperandMatcher(InstructionMatcher &Insn, unsigned OpIdx,
1180 const std::string &SymbolicName,
1181 unsigned AllocatedTemporariesBaseID, bool IsVariadic = false)
1182 : Insn(Insn), OpIdx(OpIdx), SymbolicName(SymbolicName),
1183 AllocatedTemporariesBaseID(AllocatedTemporariesBaseID),
1184 IsVariadic(IsVariadic) {}
1185
1186 bool hasSymbolicName() const { return !SymbolicName.empty(); }
1187 StringRef getSymbolicName() const { return SymbolicName; }
1188 void setSymbolicName(StringRef Name) {
1189 assert(SymbolicName.empty() && "Operand already has a symbolic name");
1190 SymbolicName = Name.str();
1191 }
1192
1193 /// Construct a new operand predicate and add it to the matcher.
1194 template <class Kind, class... Args>
1195 std::optional<Kind *> addPredicate(Args &&...args) {
1196 // TODO: Should variadic ops support predicates?
1197 if (isSameAsAnotherOperand() || IsVariadic)
1198 return std::nullopt;
1199 Predicates.emplace_back(std::make_unique<Kind>(
1200 getInsnVarID(), getOpIdx(), std::forward<Args>(args)...));
1201 return static_cast<Kind *>(Predicates.back().get());
1202 }
1203
1204 unsigned getOpIdx() const { return OpIdx; }
1205 unsigned getInsnVarID() const;
1206
1207 bool isVariadic() const { return IsVariadic; }
1208
1209 /// If this OperandMatcher has not been assigned a TempTypeIdx yet, assigns it
1210 /// one and adds a `RecordRegisterType` predicate to this matcher. If one has
1211 /// already been assigned, simply returns it.
1212 TempTypeIdx getTempTypeIdx(RuleMatcher &Rule);
1213
1214 bool recordsOperand() const;
1215
1216 std::string getOperandExpr(unsigned InsnVarID) const;
1217
1218 InstructionMatcher &getInstructionMatcher() const { return Insn; }
1219
1220 Error addTypeCheckPredicate(const TypeSetByHwMode &VTy,
1221 bool OperandIsAPointer);
1222
1223 /// Emit MatchTable opcodes that test whether the instruction named in
1224 /// InsnVarID matches all the predicates and all the operands.
1225 void emitPredicateOpcodes(MatchTable &Table);
1226
1227 /// Compare the priority of this object and B.
1228 ///
1229 /// Returns true if this object is more important than B.
1230 bool isHigherPriorityThan(OperandMatcher &B);
1231
1232 /// Report the maximum number of temporary operands needed by the operand
1233 /// matcher.
1234 unsigned countRendererFns();
1235
1236 unsigned getAllocatedTemporariesBaseID() const {
1237 return AllocatedTemporariesBaseID;
1238 }
1239
1240 bool isSameAsAnotherOperand() {
1241 for (const auto &Predicate : predicates())
1242 if (isa<SameOperandMatcher>(Val: Predicate))
1243 return true;
1244 return false;
1245 }
1246};
1247
1248/// Generates code to check a predicate on an instruction.
1249///
1250/// Typical predicates include:
1251/// * The opcode of the instruction is a particular value.
1252/// * The nsw/nuw flag is/isn't set.
1253class InstructionPredicateMatcher : public PredicateMatcher {
1254public:
1255 InstructionPredicateMatcher(PredicateKind Kind, unsigned InsnVarID)
1256 : PredicateMatcher(Kind, InsnVarID) {}
1257 ~InstructionPredicateMatcher() override = default;
1258
1259 /// Compare the priority of this object and B.
1260 ///
1261 /// Returns true if this object is more important than B.
1262 virtual bool
1263 isHigherPriorityThan(const InstructionPredicateMatcher &B) const {
1264 return Kind < B.Kind;
1265 };
1266};
1267
1268template <>
1269inline std::string
1270PredicateListMatcher<PredicateMatcher>::getNoPredicateComment() const {
1271 return "No instruction predicates";
1272}
1273
1274/// Generates code to check the opcode of an instruction.
1275class InstructionOpcodeMatcher : public InstructionPredicateMatcher {
1276protected:
1277 // Allow matching one to several, similar opcodes that share properties. This
1278 // is to handle patterns where one SelectionDAG operation maps to multiple
1279 // GlobalISel ones (e.g. G_BUILD_VECTOR and G_BUILD_VECTOR_TRUNC). The first
1280 // is treated as the canonical opcode.
1281 SmallVector<const CodeGenInstruction *, 2> Insts;
1282
1283 static DenseMap<const CodeGenInstruction *, unsigned> OpcodeValues;
1284
1285 RecordAndValue getInstValue(const CodeGenInstruction *I) const;
1286
1287public:
1288 static void initOpcodeValuesMap(const CodeGenTarget &Target);
1289
1290 InstructionOpcodeMatcher(unsigned InsnVarID,
1291 ArrayRef<const CodeGenInstruction *> I)
1292 : InstructionPredicateMatcher(IPM_Opcode, InsnVarID), Insts(I) {
1293 assert((Insts.size() == 1 || Insts.size() == 2) &&
1294 "unexpected number of opcode alternatives");
1295 }
1296
1297 static bool classof(const PredicateMatcher *P) {
1298 return P->getKind() == IPM_Opcode;
1299 }
1300
1301 bool isIdentical(const PredicateMatcher &B) const override {
1302 return InstructionPredicateMatcher::isIdentical(B) &&
1303 Insts == cast<InstructionOpcodeMatcher>(Val: &B)->Insts;
1304 }
1305
1306 bool hasValue() const override {
1307 return Insts.size() == 1 && OpcodeValues.contains(Val: Insts[0]);
1308 }
1309
1310 // TODO: This is used for the SwitchMatcher optimization. We should be able to
1311 // return a list of the opcodes to match.
1312 RecordAndValue getValue() const override;
1313
1314 void emitPredicateOpcodes(MatchTable &Table) const override;
1315
1316 /// Compare the priority of this object and B.
1317 ///
1318 /// Returns true if this object is more important than B.
1319 bool
1320 isHigherPriorityThan(const InstructionPredicateMatcher &B) const override;
1321
1322 bool isConstantInstruction() const;
1323
1324 // The first opcode is the canonical opcode, and later are alternatives.
1325 StringRef getOpcode() const;
1326 ArrayRef<const CodeGenInstruction *> getAlternativeOpcodes() { return Insts; }
1327 bool isVariadicNumOperands() const;
1328 StringRef getOperandType(unsigned OpIdx) const;
1329};
1330
1331class InstructionNumOperandsMatcher final : public InstructionPredicateMatcher {
1332public:
1333 enum class CheckKind { Eq, LE, GE };
1334
1335private:
1336 unsigned NumOperands = 0;
1337 CheckKind CK;
1338
1339public:
1340 InstructionNumOperandsMatcher(unsigned InsnVarID, unsigned NumOperands,
1341 CheckKind CK = CheckKind::Eq)
1342 : InstructionPredicateMatcher(IPM_NumOperands, InsnVarID),
1343 NumOperands(NumOperands), CK(CK) {}
1344
1345 static bool classof(const PredicateMatcher *P) {
1346 return P->getKind() == IPM_NumOperands;
1347 }
1348
1349 bool isIdentical(const PredicateMatcher &B) const override {
1350 if (!InstructionPredicateMatcher::isIdentical(B))
1351 return false;
1352 const auto &Other = *cast<InstructionNumOperandsMatcher>(Val: &B);
1353 return NumOperands == Other.NumOperands && CK == Other.CK;
1354 }
1355
1356 void emitPredicateOpcodes(MatchTable &Table) const override;
1357};
1358
1359/// Generates code to check that this instruction is a constant whose value
1360/// meets an immediate predicate.
1361///
1362/// Immediates are slightly odd since they are typically used like an operand
1363/// but are represented as an operator internally. We typically write simm8:$src
1364/// in a tablegen pattern, but this is just syntactic sugar for
1365/// (imm:i32)<<P:Predicate_simm8>>:$imm which more directly describes the nodes
1366/// that will be matched and the predicate (which is attached to the imm
1367/// operator) that will be tested. In SelectionDAG this describes a
1368/// ConstantSDNode whose internal value will be tested using the simm8
1369/// predicate.
1370///
1371/// The corresponding GlobalISel representation is %1 = G_CONSTANT iN Value. In
1372/// this representation, the immediate could be tested with an
1373/// InstructionMatcher, InstructionOpcodeMatcher, OperandMatcher, and a
1374/// OperandPredicateMatcher-subclass to check the Value meets the predicate but
1375/// there are two implementation issues with producing that matcher
1376/// configuration from the SelectionDAG pattern:
1377/// * ImmLeaf is a PatFrag whose root is an InstructionMatcher. This means that
1378/// were we to sink the immediate predicate to the operand we would have to
1379/// have two partial implementations of PatFrag support, one for immediates
1380/// and one for non-immediates.
1381/// * At the point we handle the predicate, the OperandMatcher hasn't been
1382/// created yet. If we were to sink the predicate to the OperandMatcher we
1383/// would also have to complicate (or duplicate) the code that descends and
1384/// creates matchers for the subtree.
1385/// Overall, it's simpler to handle it in the place it was found.
1386class InstructionImmPredicateMatcher : public InstructionPredicateMatcher {
1387protected:
1388 TreePredicateFn Predicate;
1389
1390public:
1391 InstructionImmPredicateMatcher(unsigned InsnVarID,
1392 const TreePredicateFn &Predicate)
1393 : InstructionPredicateMatcher(IPM_ImmPredicate, InsnVarID),
1394 Predicate(Predicate) {}
1395
1396 bool isIdentical(const PredicateMatcher &B) const override;
1397
1398 static bool classof(const PredicateMatcher *P) {
1399 return P->getKind() == IPM_ImmPredicate;
1400 }
1401
1402 void emitPredicateOpcodes(MatchTable &Table) const override;
1403};
1404
1405/// Generates code to check that a memory instruction has a atomic ordering
1406/// MachineMemoryOperand.
1407class AtomicOrderingMMOPredicateMatcher : public InstructionPredicateMatcher {
1408public:
1409 enum AOComparator {
1410 AO_Exactly,
1411 AO_OrStronger,
1412 AO_WeakerThan,
1413 };
1414
1415protected:
1416 StringRef Order;
1417 AOComparator Comparator;
1418
1419public:
1420 AtomicOrderingMMOPredicateMatcher(unsigned InsnVarID, StringRef Order,
1421 AOComparator Comparator = AO_Exactly)
1422 : InstructionPredicateMatcher(IPM_AtomicOrderingMMO, InsnVarID),
1423 Order(Order), Comparator(Comparator) {}
1424
1425 static bool classof(const PredicateMatcher *P) {
1426 return P->getKind() == IPM_AtomicOrderingMMO;
1427 }
1428
1429 bool isIdentical(const PredicateMatcher &B) const override;
1430
1431 void emitPredicateOpcodes(MatchTable &Table) const override;
1432};
1433
1434/// Generates code to check that the size of an MMO is exactly N bytes.
1435class MemorySizePredicateMatcher : public InstructionPredicateMatcher {
1436protected:
1437 unsigned MMOIdx;
1438 uint64_t Size;
1439
1440public:
1441 MemorySizePredicateMatcher(unsigned InsnVarID, unsigned MMOIdx, unsigned Size)
1442 : InstructionPredicateMatcher(IPM_MemoryLLTSize, InsnVarID),
1443 MMOIdx(MMOIdx), Size(Size) {}
1444
1445 static bool classof(const PredicateMatcher *P) {
1446 return P->getKind() == IPM_MemoryLLTSize;
1447 }
1448 bool isIdentical(const PredicateMatcher &B) const override {
1449 return InstructionPredicateMatcher::isIdentical(B) &&
1450 MMOIdx == cast<MemorySizePredicateMatcher>(Val: &B)->MMOIdx &&
1451 Size == cast<MemorySizePredicateMatcher>(Val: &B)->Size;
1452 }
1453
1454 void emitPredicateOpcodes(MatchTable &Table) const override;
1455};
1456
1457class MemoryAddressSpacePredicateMatcher : public InstructionPredicateMatcher {
1458protected:
1459 unsigned MMOIdx;
1460 SmallVector<unsigned, 4> AddrSpaces;
1461
1462public:
1463 MemoryAddressSpacePredicateMatcher(unsigned InsnVarID, unsigned MMOIdx,
1464 ArrayRef<unsigned> AddrSpaces)
1465 : InstructionPredicateMatcher(IPM_MemoryAddressSpace, InsnVarID),
1466 MMOIdx(MMOIdx), AddrSpaces(AddrSpaces) {}
1467
1468 static bool classof(const PredicateMatcher *P) {
1469 return P->getKind() == IPM_MemoryAddressSpace;
1470 }
1471
1472 bool isIdentical(const PredicateMatcher &B) const override;
1473
1474 void emitPredicateOpcodes(MatchTable &Table) const override;
1475};
1476
1477class MemoryAlignmentPredicateMatcher : public InstructionPredicateMatcher {
1478protected:
1479 unsigned MMOIdx;
1480 int MinAlign;
1481
1482public:
1483 MemoryAlignmentPredicateMatcher(unsigned InsnVarID, unsigned MMOIdx,
1484 int MinAlign)
1485 : InstructionPredicateMatcher(IPM_MemoryAlignment, InsnVarID),
1486 MMOIdx(MMOIdx), MinAlign(MinAlign) {
1487 assert(MinAlign > 0);
1488 }
1489
1490 static bool classof(const PredicateMatcher *P) {
1491 return P->getKind() == IPM_MemoryAlignment;
1492 }
1493
1494 bool isIdentical(const PredicateMatcher &B) const override;
1495
1496 void emitPredicateOpcodes(MatchTable &Table) const override;
1497};
1498
1499/// Generates code to check that the size of an MMO is less-than, equal-to, or
1500/// greater than a given LLT.
1501class MemoryVsLLTSizePredicateMatcher : public InstructionPredicateMatcher {
1502public:
1503 enum RelationKind {
1504 GreaterThan,
1505 EqualTo,
1506 LessThan,
1507 };
1508
1509protected:
1510 unsigned MMOIdx;
1511 RelationKind Relation;
1512 unsigned OpIdx;
1513
1514public:
1515 MemoryVsLLTSizePredicateMatcher(unsigned InsnVarID, unsigned MMOIdx,
1516 enum RelationKind Relation, unsigned OpIdx)
1517 : InstructionPredicateMatcher(IPM_MemoryVsLLTSize, InsnVarID),
1518 MMOIdx(MMOIdx), Relation(Relation), OpIdx(OpIdx) {}
1519
1520 static bool classof(const PredicateMatcher *P) {
1521 return P->getKind() == IPM_MemoryVsLLTSize;
1522 }
1523 bool isIdentical(const PredicateMatcher &B) const override;
1524
1525 void emitPredicateOpcodes(MatchTable &Table) const override;
1526};
1527
1528// Matcher for immAllOnesV/immAllZerosV
1529class VectorSplatImmPredicateMatcher : public InstructionPredicateMatcher {
1530public:
1531 enum SplatKind { AllZeros, AllOnes };
1532
1533private:
1534 SplatKind Kind;
1535
1536public:
1537 VectorSplatImmPredicateMatcher(unsigned InsnVarID, SplatKind K)
1538 : InstructionPredicateMatcher(IPM_VectorSplatImm, InsnVarID), Kind(K) {}
1539
1540 static bool classof(const PredicateMatcher *P) {
1541 return P->getKind() == IPM_VectorSplatImm;
1542 }
1543
1544 bool isIdentical(const PredicateMatcher &B) const override {
1545 return InstructionPredicateMatcher::isIdentical(B) &&
1546 Kind == static_cast<const VectorSplatImmPredicateMatcher &>(B).Kind;
1547 }
1548
1549 void emitPredicateOpcodes(MatchTable &Table) const override;
1550};
1551
1552/// Generates code to check an arbitrary C++ instruction predicate.
1553class GenericInstructionPredicateMatcher : public InstructionPredicateMatcher {
1554protected:
1555 std::string EnumVal;
1556
1557public:
1558 GenericInstructionPredicateMatcher(unsigned InsnVarID,
1559 TreePredicateFn Predicate);
1560
1561 GenericInstructionPredicateMatcher(unsigned InsnVarID,
1562 const std::string &EnumVal)
1563 : InstructionPredicateMatcher(IPM_GenericPredicate, InsnVarID),
1564 EnumVal(EnumVal) {}
1565
1566 static bool classof(const InstructionPredicateMatcher *P) {
1567 return P->getKind() == IPM_GenericPredicate;
1568 }
1569 bool isIdentical(const PredicateMatcher &B) const override;
1570 void emitPredicateOpcodes(MatchTable &Table) const override;
1571
1572 bool canHoistOutsideOf(const Matcher &M) const override {
1573 // We can only hoist C++ code if the parent Matcher does not define any
1574 // symbol that may be used by C++ code.
1575 // TODO?: Could we be more precise, e.g. hoist if the Matcher records
1576 // operands, but the operands aren't used by this bit of C++.
1577 return !M.recordsOperand();
1578 }
1579};
1580
1581class MIFlagsInstructionPredicateMatcher : public InstructionPredicateMatcher {
1582 SmallVector<StringRef, 2> Flags;
1583 bool CheckNot; // false = GIM_MIFlags, true = GIM_MIFlagsNot
1584
1585public:
1586 MIFlagsInstructionPredicateMatcher(unsigned InsnVarID,
1587 ArrayRef<StringRef> FlagsToCheck,
1588 bool CheckNot = false)
1589 : InstructionPredicateMatcher(IPM_MIFlags, InsnVarID),
1590 Flags(FlagsToCheck), CheckNot(CheckNot) {
1591 sort(C&: Flags);
1592 }
1593
1594 static bool classof(const InstructionPredicateMatcher *P) {
1595 return P->getKind() == IPM_MIFlags;
1596 }
1597
1598 bool isIdentical(const PredicateMatcher &B) const override;
1599 void emitPredicateOpcodes(MatchTable &Table) const override;
1600};
1601
1602/// Generates code to check for the absence of use of the result.
1603// TODO? Generalize this to support checking for one use.
1604class NoUsePredicateMatcher : public InstructionPredicateMatcher {
1605public:
1606 NoUsePredicateMatcher(unsigned InsnVarID)
1607 : InstructionPredicateMatcher(IPM_NoUse, InsnVarID) {}
1608
1609 static bool classof(const PredicateMatcher *P) {
1610 return P->getKind() == IPM_NoUse;
1611 }
1612
1613 bool isIdentical(const PredicateMatcher &B) const override {
1614 return InstructionPredicateMatcher::isIdentical(B);
1615 }
1616
1617 void emitPredicateOpcodes(MatchTable &Table) const override {
1618 Table << MatchTable::Opcode(Opcode: "GIM_CheckHasNoUse")
1619 << MatchTable::Comment(Comment: "MI") << MatchTable::ULEB128Value(IntValue: InsnVarID)
1620 << MatchTable::LineBreak;
1621 }
1622};
1623
1624/// Generates code to check that the first result has only one use.
1625class OneUsePredicateMatcher : public InstructionPredicateMatcher {
1626public:
1627 OneUsePredicateMatcher(unsigned InsnVarID)
1628 : InstructionPredicateMatcher(IPM_OneUse, InsnVarID) {}
1629
1630 static bool classof(const PredicateMatcher *P) {
1631 return P->getKind() == IPM_OneUse;
1632 }
1633
1634 bool isIdentical(const PredicateMatcher &B) const override {
1635 return InstructionPredicateMatcher::isIdentical(B);
1636 }
1637
1638 void emitPredicateOpcodes(MatchTable &Table) const override {
1639 Table << MatchTable::Opcode(Opcode: "GIM_CheckHasOneUse")
1640 << MatchTable::Comment(Comment: "MI") << MatchTable::ULEB128Value(IntValue: InsnVarID)
1641 << MatchTable::LineBreak;
1642 }
1643};
1644
1645/// Generates code to check that a set of predicates and operands match for a
1646/// particular instruction.
1647///
1648/// Typical predicates include:
1649/// * Has a specific opcode.
1650/// * Has an nsw/nuw flag or doesn't.
1651class InstructionMatcher final : public PredicateListMatcher<PredicateMatcher> {
1652protected:
1653 using OperandVec = std::vector<std::unique_ptr<OperandMatcher>>;
1654
1655 RuleMatcher &Rule;
1656
1657 /// The operands to match. All rendered operands must be present even if the
1658 /// condition is always true.
1659 OperandVec Operands;
1660
1661 std::string SymbolicName;
1662 unsigned InsnVarID;
1663 bool AllowNumOpsCheck;
1664
1665 bool canAddNumOperandsCheck() const {
1666 // Add if it's allowed, and:
1667 // - We don't have a variadic operand
1668 // - We don't already have such a check.
1669 return AllowNumOpsCheck && !hasVariadicMatcher() &&
1670 none_of(Range: Predicates, P: [&](const auto &P) {
1671 return P->getKind() ==
1672 InstructionPredicateMatcher::IPM_NumOperands;
1673 });
1674 }
1675
1676public:
1677 InstructionMatcher(RuleMatcher &Rule, unsigned InsnVarID,
1678 StringRef SymbolicName, bool AllowNumOpsCheck = true)
1679 : Rule(Rule), SymbolicName(SymbolicName), InsnVarID(InsnVarID),
1680 AllowNumOpsCheck(AllowNumOpsCheck) {}
1681
1682 /// Construct a new instruction predicate and add it to the matcher.
1683 template <class Kind, class... Args>
1684 std::optional<Kind *> addPredicate(Args &&...args) {
1685 Predicates.emplace_back(
1686 std::make_unique<Kind>(getInsnVarID(), std::forward<Args>(args)...));
1687 return static_cast<Kind *>(Predicates.back().get());
1688 }
1689
1690 RuleMatcher &getRuleMatcher() const { return Rule; }
1691
1692 unsigned getInsnVarID() const { return InsnVarID; }
1693
1694 /// Add an operand to the matcher.
1695 OperandMatcher &addOperand(unsigned OpIdx, const std::string &SymbolicName,
1696 unsigned AllocatedTemporariesBaseID,
1697 bool IsVariadic = false);
1698 OperandMatcher &getOperand(unsigned OpIdx);
1699 OperandMatcher &addPhysRegInput(const Record *Reg, unsigned OpIdx,
1700 unsigned TempOpIdx);
1701
1702 StringRef getSymbolicName() const { return SymbolicName; }
1703
1704 unsigned getNumOperandMatchers() const { return Operands.size(); }
1705 bool hasVariadicMatcher() const {
1706 return !Operands.empty() && Operands.back()->isVariadic();
1707 }
1708
1709 OperandVec::iterator operands_begin() { return Operands.begin(); }
1710 OperandVec::iterator operands_end() { return Operands.end(); }
1711 iterator_range<OperandVec::iterator> operands() {
1712 return make_range(x: operands_begin(), y: operands_end());
1713 }
1714 OperandVec::const_iterator operands_begin() const { return Operands.begin(); }
1715 OperandVec::const_iterator operands_end() const { return Operands.end(); }
1716 iterator_range<OperandVec::const_iterator> operands() const {
1717 return make_range(x: operands_begin(), y: operands_end());
1718 }
1719 bool operands_empty() const { return Operands.empty(); }
1720
1721 void pop_front() { Operands.erase(position: Operands.begin()); }
1722
1723 void optimize();
1724
1725 bool recordsOperand() const;
1726
1727 /// Emit MatchTable opcodes that test whether the instruction named in
1728 /// InsnVarName matches all the predicates and all the operands.
1729 void emitPredicateOpcodes(MatchTable &Table);
1730
1731 /// Compare the priority of this object and B.
1732 ///
1733 /// Returns true if this object is more important than B.
1734 bool isHigherPriorityThan(InstructionMatcher &B);
1735
1736 /// Report the maximum number of temporary operands needed by the instruction
1737 /// matcher.
1738 unsigned countRendererFns();
1739
1740 InstructionOpcodeMatcher &getOpcodeMatcher() {
1741 for (auto &P : predicates())
1742 if (auto *OpMatcher = dyn_cast<InstructionOpcodeMatcher>(Val: P.get()))
1743 return *OpMatcher;
1744 llvm_unreachable("Didn't find an opcode matcher");
1745 }
1746
1747 bool isConstantInstruction() {
1748 return getOpcodeMatcher().isConstantInstruction();
1749 }
1750
1751 StringRef getOpcode() { return getOpcodeMatcher().getOpcode(); }
1752};
1753
1754/// Generates code to check that the operand is a register defined by an
1755/// instruction that matches the given instruction matcher.
1756///
1757/// For example, the pattern:
1758/// (set $dst, (G_MUL (G_ADD $src1, $src2), $src3))
1759/// would use an InstructionOperandMatcher for operand 1 of the G_MUL to match
1760/// the:
1761/// (G_ADD $src1, $src2)
1762/// subpattern.
1763class InstructionOperandMatcher : public OperandPredicateMatcher {
1764protected:
1765 InstructionMatcher &InsnMatcher;
1766
1767 GISelFlags Flags;
1768
1769public:
1770 InstructionOperandMatcher(unsigned InsnVarID, unsigned OpIdx,
1771 RuleMatcher &Rule, StringRef SymbolicName,
1772 bool AllowNumOpsCheck = true)
1773 : OperandPredicateMatcher(OPM_Instruction, InsnVarID, OpIdx),
1774 InsnMatcher(
1775 Rule.allocateInstructionMatcher(SymbolicName, AllowNumOpsCheck)),
1776 Flags(Rule.getGISelFlags()) {}
1777
1778 static bool classof(const PredicateMatcher *P) {
1779 return P->getKind() == OPM_Instruction;
1780 }
1781
1782 InstructionMatcher &getInsnMatcher() const { return InsnMatcher; }
1783
1784 void emitCaptureOpcodes(MatchTable &Table) const;
1785 void emitPredicateOpcodes(MatchTable &Table) const override {
1786 emitCaptureOpcodes(Table);
1787 InsnMatcher.emitPredicateOpcodes(Table);
1788 }
1789
1790 bool isHigherPriorityThan(const OperandPredicateMatcher &B) const override;
1791
1792 /// Report the maximum number of temporary operands needed by the predicate
1793 /// matcher.
1794 unsigned countRendererFns() const override {
1795 return InsnMatcher.countRendererFns();
1796 }
1797};
1798
1799//===- Actions ------------------------------------------------------------===//
1800class OperandRenderer {
1801public:
1802 enum RendererKind {
1803 OR_Copy,
1804 OR_CopyOrAddZeroReg,
1805 OR_CopySubReg,
1806 OR_CopyPhysReg,
1807 OR_CopyConstantAsImm,
1808 OR_CopyFConstantAsFPImm,
1809 OR_Imm,
1810 OR_SubRegIndex,
1811 OR_Register,
1812 OR_TempRegister,
1813 OR_ComplexPattern,
1814 OR_Intrinsic,
1815 OR_Custom,
1816 OR_CustomOperand
1817 };
1818
1819protected:
1820 RendererKind Kind;
1821
1822public:
1823 OperandRenderer(RendererKind Kind) : Kind(Kind) {}
1824 virtual ~OperandRenderer();
1825
1826 RendererKind getKind() const { return Kind; }
1827
1828 virtual void emitRenderOpcodes(MatchTable &Table) const = 0;
1829};
1830
1831/// A CopyRenderer emits code to copy a single operand from an existing
1832/// instruction to the one being built.
1833class CopyRenderer : public OperandRenderer {
1834protected:
1835 unsigned NewInsnID;
1836 StringRef SymbolicName;
1837 unsigned OldInsnID;
1838 unsigned OldOpIdx;
1839 bool OldOpIsVariadic = false;
1840
1841public:
1842 CopyRenderer(unsigned NewInsnID, RuleMatcher &RM, StringRef SymbolicName)
1843 : OperandRenderer(OR_Copy), NewInsnID(NewInsnID),
1844 SymbolicName(SymbolicName) {
1845 assert(!SymbolicName.empty() && "Cannot copy from an unspecified source");
1846 const OperandMatcher &Operand = RM.getOperandMatcher(Name: SymbolicName);
1847 OldInsnID = Operand.getInstructionMatcher().getInsnVarID();
1848 OldOpIdx = Operand.getOpIdx();
1849 OldOpIsVariadic = Operand.isVariadic();
1850 }
1851
1852 static bool classof(const OperandRenderer *R) {
1853 return R->getKind() == OR_Copy;
1854 }
1855
1856 StringRef getSymbolicName() const { return SymbolicName; }
1857
1858 static void emitRenderOpcodes(MatchTable &Table, unsigned NewInsnID,
1859 unsigned OldInsnID, unsigned OpIdx,
1860 StringRef Name, bool ForVariadic = false);
1861
1862 void emitRenderOpcodes(MatchTable &Table) const override;
1863};
1864
1865/// A CopyRenderer emits code to copy a virtual register to a specific physical
1866/// register.
1867class CopyPhysRegRenderer : public OperandRenderer {
1868protected:
1869 unsigned NewInsnID;
1870 const Record *PhysReg;
1871 unsigned OldInsnID;
1872 unsigned OldOpIdx;
1873
1874public:
1875 CopyPhysRegRenderer(unsigned NewInsnID, RuleMatcher &RM, const Record *Reg)
1876 : OperandRenderer(OR_CopyPhysReg), NewInsnID(NewInsnID), PhysReg(Reg) {
1877 assert(PhysReg);
1878 const OperandMatcher &Operand = RM.getPhysRegOperandMatcher(PhysReg);
1879 OldInsnID = Operand.getInstructionMatcher().getInsnVarID();
1880 OldOpIdx = Operand.getOpIdx();
1881 }
1882
1883 static bool classof(const OperandRenderer *R) {
1884 return R->getKind() == OR_CopyPhysReg;
1885 }
1886
1887 const Record *getPhysReg() const { return PhysReg; }
1888
1889 void emitRenderOpcodes(MatchTable &Table) const override;
1890};
1891
1892/// A CopyOrAddZeroRegRenderer emits code to copy a single operand from an
1893/// existing instruction to the one being built. If the operand turns out to be
1894/// a 'G_CONSTANT 0' then it replaces the operand with a zero register.
1895class CopyOrAddZeroRegRenderer : public OperandRenderer {
1896protected:
1897 unsigned NewInsnID;
1898 /// The name of the operand.
1899 const StringRef SymbolicName;
1900 const Record *ZeroRegisterDef;
1901 unsigned OldInsnID;
1902 unsigned OldOpIdx;
1903
1904public:
1905 CopyOrAddZeroRegRenderer(unsigned NewInsnID, RuleMatcher &RM,
1906 StringRef SymbolicName,
1907 const Record *ZeroRegisterDef)
1908 : OperandRenderer(OR_CopyOrAddZeroReg), NewInsnID(NewInsnID),
1909 SymbolicName(SymbolicName), ZeroRegisterDef(ZeroRegisterDef) {
1910 assert(!SymbolicName.empty() && "Cannot copy from an unspecified source");
1911 const OperandMatcher &Operand = RM.getOperandMatcher(Name: SymbolicName);
1912 OldInsnID = Operand.getInstructionMatcher().getInsnVarID();
1913 OldOpIdx = Operand.getOpIdx();
1914 }
1915
1916 static bool classof(const OperandRenderer *R) {
1917 return R->getKind() == OR_CopyOrAddZeroReg;
1918 }
1919
1920 StringRef getSymbolicName() const { return SymbolicName; }
1921
1922 void emitRenderOpcodes(MatchTable &Table) const override;
1923};
1924
1925/// A CopyConstantAsImmRenderer emits code to render a G_CONSTANT instruction to
1926/// an extended immediate operand.
1927class CopyConstantAsImmRenderer : public OperandRenderer {
1928protected:
1929 unsigned NewInsnID;
1930 /// The name of the operand.
1931 const std::string SymbolicName;
1932 bool Signed = true;
1933 unsigned OldInsnID;
1934
1935public:
1936 CopyConstantAsImmRenderer(unsigned NewInsnID, RuleMatcher &RM,
1937 StringRef SymbolicName)
1938 : OperandRenderer(OR_CopyConstantAsImm), NewInsnID(NewInsnID),
1939 SymbolicName(SymbolicName) {
1940 InstructionMatcher &InsnMatcher = RM.getInstructionMatcher(SymbolicName);
1941 OldInsnID = InsnMatcher.getInsnVarID();
1942 }
1943
1944 static bool classof(const OperandRenderer *R) {
1945 return R->getKind() == OR_CopyConstantAsImm;
1946 }
1947
1948 StringRef getSymbolicName() const { return SymbolicName; }
1949
1950 void emitRenderOpcodes(MatchTable &Table) const override;
1951};
1952
1953/// A CopyFConstantAsFPImmRenderer emits code to render a G_FCONSTANT
1954/// instruction to an extended immediate operand.
1955class CopyFConstantAsFPImmRenderer : public OperandRenderer {
1956protected:
1957 unsigned NewInsnID;
1958 /// The name of the operand.
1959 const std::string SymbolicName;
1960 unsigned OldInsnID;
1961
1962public:
1963 CopyFConstantAsFPImmRenderer(unsigned NewInsnID, RuleMatcher &RM,
1964 StringRef SymbolicName)
1965 : OperandRenderer(OR_CopyFConstantAsFPImm), NewInsnID(NewInsnID),
1966 SymbolicName(SymbolicName) {
1967 InstructionMatcher &InsnMatcher = RM.getInstructionMatcher(SymbolicName);
1968 OldInsnID = InsnMatcher.getInsnVarID();
1969 }
1970
1971 static bool classof(const OperandRenderer *R) {
1972 return R->getKind() == OR_CopyFConstantAsFPImm;
1973 }
1974
1975 StringRef getSymbolicName() const { return SymbolicName; }
1976
1977 void emitRenderOpcodes(MatchTable &Table) const override;
1978};
1979
1980/// A CopySubRegRenderer emits code to copy a single register operand from an
1981/// existing instruction to the one being built and indicate that only a
1982/// subregister should be copied.
1983class CopySubRegRenderer : public OperandRenderer {
1984protected:
1985 unsigned NewInsnID;
1986 /// The name of the operand.
1987 const StringRef SymbolicName;
1988 /// The subregister to extract.
1989 const CodeGenSubRegIndex *SubReg;
1990 unsigned OldInsnID;
1991 unsigned OldOpIdx;
1992
1993public:
1994 CopySubRegRenderer(unsigned NewInsnID, RuleMatcher &RM,
1995 StringRef SymbolicName, const CodeGenSubRegIndex *SubReg)
1996 : OperandRenderer(OR_CopySubReg), NewInsnID(NewInsnID),
1997 SymbolicName(SymbolicName), SubReg(SubReg) {
1998 const OperandMatcher &Operand = RM.getOperandMatcher(Name: SymbolicName);
1999 OldInsnID = Operand.getInstructionMatcher().getInsnVarID();
2000 OldOpIdx = Operand.getOpIdx();
2001 }
2002
2003 static bool classof(const OperandRenderer *R) {
2004 return R->getKind() == OR_CopySubReg;
2005 }
2006
2007 StringRef getSymbolicName() const { return SymbolicName; }
2008
2009 void emitRenderOpcodes(MatchTable &Table) const override;
2010};
2011
2012/// Adds a specific physical register to the instruction being built.
2013/// This is typically useful for WZR/XZR on AArch64.
2014class AddRegisterRenderer : public OperandRenderer {
2015protected:
2016 unsigned InsnID;
2017 const Record *RegisterDef;
2018 bool IsDef;
2019 bool IsDead;
2020 const CodeGenTarget &Target;
2021
2022public:
2023 AddRegisterRenderer(unsigned InsnID, const CodeGenTarget &Target,
2024 const Record *RegisterDef, bool IsDef = false,
2025 bool IsDead = false)
2026 : OperandRenderer(OR_Register), InsnID(InsnID), RegisterDef(RegisterDef),
2027 IsDef(IsDef), IsDead(IsDead), Target(Target) {}
2028
2029 static bool classof(const OperandRenderer *R) {
2030 return R->getKind() == OR_Register;
2031 }
2032
2033 void emitRenderOpcodes(MatchTable &Table) const override;
2034};
2035
2036/// Adds a specific temporary virtual register to the instruction being built.
2037/// This is used to chain instructions together when emitting multiple
2038/// instructions.
2039class TempRegRenderer : public OperandRenderer {
2040protected:
2041 unsigned InsnID;
2042 unsigned TempRegID;
2043 const CodeGenSubRegIndex *SubRegIdx;
2044 bool IsDef;
2045 bool IsDead;
2046
2047public:
2048 TempRegRenderer(unsigned InsnID, unsigned TempRegID, bool IsDef = false,
2049 const CodeGenSubRegIndex *SubReg = nullptr,
2050 bool IsDead = false)
2051 : OperandRenderer(OR_Register), InsnID(InsnID), TempRegID(TempRegID),
2052 SubRegIdx(SubReg), IsDef(IsDef), IsDead(IsDead) {}
2053
2054 static bool classof(const OperandRenderer *R) {
2055 return R->getKind() == OR_TempRegister;
2056 }
2057
2058 void emitRenderOpcodes(MatchTable &Table) const override;
2059};
2060
2061/// Adds a specific immediate to the instruction being built.
2062/// Without a type this is a raw integer immediate. With ConstantLLT it is a
2063/// typed constant: ConstantInt, or ConstantFP when IsFP is set (Imm is then
2064/// the IEEE bit pattern of that type).
2065class ImmRenderer : public OperandRenderer {
2066protected:
2067 unsigned InsnID;
2068 int64_t Imm;
2069 std::optional<LLTCodeGenOrTempType> ConstantLLT;
2070 bool IsFP = false;
2071
2072public:
2073 ImmRenderer(unsigned InsnID, int64_t Imm)
2074 : OperandRenderer(OR_Imm), InsnID(InsnID), Imm(Imm) {}
2075
2076 ImmRenderer(unsigned InsnID, int64_t Imm,
2077 const LLTCodeGenOrTempType &ConstantLLT, bool IsFP = false)
2078 : OperandRenderer(OR_Imm), InsnID(InsnID), Imm(Imm),
2079 ConstantLLT(ConstantLLT), IsFP(IsFP) {
2080 if (ConstantLLT.isLLTCodeGen())
2081 KnownTypes.insert(x: ConstantLLT.getLLTCodeGen());
2082 }
2083
2084 static bool classof(const OperandRenderer *R) {
2085 return R->getKind() == OR_Imm;
2086 }
2087
2088 static void emitAddImm(MatchTable &Table, unsigned InsnID, int64_t Imm,
2089 StringRef ImmName = "Imm");
2090
2091 void emitRenderOpcodes(MatchTable &Table) const override;
2092};
2093
2094/// Adds an enum value for a subreg index to the instruction being built.
2095class SubRegIndexRenderer : public OperandRenderer {
2096protected:
2097 unsigned InsnID;
2098 const CodeGenSubRegIndex *SubRegIdx;
2099
2100public:
2101 SubRegIndexRenderer(unsigned InsnID, const CodeGenSubRegIndex *SRI)
2102 : OperandRenderer(OR_SubRegIndex), InsnID(InsnID), SubRegIdx(SRI) {}
2103
2104 static bool classof(const OperandRenderer *R) {
2105 return R->getKind() == OR_SubRegIndex;
2106 }
2107
2108 void emitRenderOpcodes(MatchTable &Table) const override;
2109};
2110
2111/// Adds operands by calling a renderer function supplied by the ComplexPattern
2112/// matcher function.
2113class RenderComplexPatternOperand : public OperandRenderer {
2114private:
2115 unsigned InsnID;
2116 const Record &TheDef;
2117 /// The name of the operand.
2118 const StringRef SymbolicName;
2119 /// The renderer number. This must be unique within a rule since it's used to
2120 /// identify a temporary variable to hold the renderer function.
2121 unsigned RendererID;
2122 /// When provided, this is the suboperand of the ComplexPattern operand to
2123 /// render. Otherwise all the suboperands will be rendered.
2124 std::optional<unsigned> SubOperand;
2125 /// The subregister to extract. Render the whole register if not specified.
2126 const CodeGenSubRegIndex *SubReg;
2127
2128 unsigned getNumOperands() const {
2129 return TheDef.getValueAsDag(FieldName: "Operands")->getNumArgs();
2130 }
2131
2132public:
2133 RenderComplexPatternOperand(unsigned InsnID, const Record &TheDef,
2134 StringRef SymbolicName, unsigned RendererID,
2135 std::optional<unsigned> SubOperand = std::nullopt,
2136 const CodeGenSubRegIndex *SubReg = nullptr)
2137 : OperandRenderer(OR_ComplexPattern), InsnID(InsnID), TheDef(TheDef),
2138 SymbolicName(SymbolicName), RendererID(RendererID),
2139 SubOperand(SubOperand), SubReg(SubReg) {}
2140
2141 static bool classof(const OperandRenderer *R) {
2142 return R->getKind() == OR_ComplexPattern;
2143 }
2144
2145 void emitRenderOpcodes(MatchTable &Table) const override;
2146};
2147
2148/// Adds an intrinsic ID operand to the instruction being built.
2149class IntrinsicIDRenderer : public OperandRenderer {
2150protected:
2151 unsigned InsnID;
2152 const CodeGenIntrinsic *II;
2153
2154public:
2155 IntrinsicIDRenderer(unsigned InsnID, const CodeGenIntrinsic *II)
2156 : OperandRenderer(OR_Intrinsic), InsnID(InsnID), II(II) {}
2157
2158 static bool classof(const OperandRenderer *R) {
2159 return R->getKind() == OR_Intrinsic;
2160 }
2161
2162 void emitRenderOpcodes(MatchTable &Table) const override;
2163};
2164
2165class CustomRenderer : public OperandRenderer {
2166protected:
2167 unsigned InsnID;
2168 const Record &Renderer;
2169 /// The name of the operand.
2170 const std::string SymbolicName;
2171 unsigned OldInsnID;
2172
2173public:
2174 CustomRenderer(unsigned InsnID, RuleMatcher &RM, const Record &Renderer,
2175 StringRef SymbolicName)
2176 : OperandRenderer(OR_Custom), InsnID(InsnID), Renderer(Renderer),
2177 SymbolicName(SymbolicName) {
2178 InstructionMatcher &InsnMatcher = RM.getInstructionMatcher(SymbolicName);
2179 OldInsnID = InsnMatcher.getInsnVarID();
2180 }
2181
2182 static bool classof(const OperandRenderer *R) {
2183 return R->getKind() == OR_Custom;
2184 }
2185
2186 void emitRenderOpcodes(MatchTable &Table) const override;
2187};
2188
2189class CustomOperandRenderer : public OperandRenderer {
2190protected:
2191 unsigned InsnID;
2192 const Record &Renderer;
2193 /// The name of the operand.
2194 const std::string SymbolicName;
2195 unsigned OldInsnID;
2196 unsigned OldOpIdx;
2197
2198public:
2199 CustomOperandRenderer(unsigned InsnID, RuleMatcher &RM,
2200 const Record &Renderer, StringRef SymbolicName)
2201 : OperandRenderer(OR_CustomOperand), InsnID(InsnID), Renderer(Renderer),
2202 SymbolicName(SymbolicName) {
2203 const OperandMatcher &OM = RM.getOperandMatcher(Name: SymbolicName);
2204 OldInsnID = OM.getInsnVarID();
2205 OldOpIdx = OM.getOpIdx();
2206 }
2207
2208 static bool classof(const OperandRenderer *R) {
2209 return R->getKind() == OR_CustomOperand;
2210 }
2211
2212 void emitRenderOpcodes(MatchTable &Table) const override;
2213};
2214
2215/// An action taken when all Matcher predicates succeeded for a parent rule.
2216///
2217/// Typical actions include:
2218/// * Changing the opcode of an instruction.
2219/// * Adding an operand to an instruction.
2220class MatchAction {
2221public:
2222 enum ActionKind {
2223 AK_DebugComment,
2224 AK_BuildMI,
2225 AK_BuildConstantMI,
2226 AK_EraseInst,
2227 AK_ReplaceReg,
2228 AK_ConstraintOpsToDef,
2229 AK_ConstraintOpsToRC,
2230 AK_MakeTempReg,
2231 };
2232
2233 MatchAction(ActionKind K) : Kind(K) {}
2234
2235 ActionKind getKind() const { return Kind; }
2236
2237 virtual ~MatchAction() = default;
2238
2239 // Some actions may need to add extra predicates to ensure they can run.
2240 virtual void emitAdditionalPredicates(MatchTable &Table) const {}
2241
2242 /// Emit the MatchTable opcodes to implement the action.
2243 virtual void emitActionOpcodes(MatchTable &Table) const = 0;
2244
2245 /// If this opcode has an overload that can call GIR_Done directly, call \p
2246 /// OnDone, emit the opcode, and return true. Otherwise, emit the normal
2247 /// action opcode and return false.
2248 virtual bool emitActionOpcodesAndDone(MatchTable &Table,
2249 function_ref<void()> OnDone) const {
2250 emitActionOpcodes(Table);
2251 return false;
2252 }
2253
2254private:
2255 ActionKind Kind;
2256};
2257
2258/// Generates a comment describing the matched rule being acted upon.
2259class DebugCommentAction : public MatchAction {
2260private:
2261 std::string S;
2262
2263public:
2264 DebugCommentAction(StringRef S) : MatchAction(AK_DebugComment), S(S.str()) {}
2265
2266 static bool classof(const MatchAction *A) {
2267 return A->getKind() == AK_DebugComment;
2268 }
2269
2270 void emitActionOpcodes(MatchTable &Table) const override {
2271 Table << MatchTable::Comment(Comment: S) << MatchTable::LineBreak;
2272 }
2273};
2274
2275/// Generates code to build an instruction or mutate an existing instruction
2276/// into the desired instruction when this is possible.
2277class BuildMIAction : public MatchAction {
2278private:
2279 unsigned InsnID;
2280 const CodeGenInstruction *I;
2281 InstructionMatcher *Matched = nullptr;
2282 std::vector<std::unique_ptr<OperandRenderer>> OperandRenderers;
2283 SmallPtrSet<const Record *, 4> DeadImplicitDefs;
2284
2285 std::vector<const InstructionMatcher *> CopiedFlags;
2286 std::vector<StringRef> SetFlags;
2287 std::vector<StringRef> UnsetFlags;
2288 std::vector<unsigned> MergeInsnIDs;
2289
2290 /// True if the instruction can be built solely by mutating the opcode.
2291 bool canMutate(RuleMatcher &Rule, const InstructionMatcher *Insn) const;
2292
2293public:
2294 BuildMIAction(unsigned InsnID, RuleMatcher &RM, const CodeGenInstruction *I)
2295 : MatchAction(AK_BuildMI), InsnID(InsnID), I(I) {
2296
2297 // Emit the ID's for all the instructions that are matched by this rule.
2298 // TODO: Limit this to matched instructions that mayLoad/mayStore or have
2299 // some other means of having a memoperand. Also limit this to
2300 // emitted instructions that expect to have a memoperand too. For
2301 // example, (G_SEXT (G_LOAD x)) that results in separate load and
2302 // sign-extend instructions shouldn't put the memoperand on the
2303 // sign-extend since it has no effect there.
2304 if (I->mayLoad || I->mayStore) {
2305 for (const auto &Matcher : RM.all_instmatchers())
2306 MergeInsnIDs.push_back(x: Matcher->getInsnVarID());
2307 llvm::sort(C&: MergeInsnIDs);
2308 }
2309 }
2310
2311 static bool classof(const MatchAction *A) {
2312 return A->getKind() == AK_BuildMI;
2313 }
2314
2315 unsigned getInsnID() const { return InsnID; }
2316 const CodeGenInstruction *getCGI() const { return I; }
2317
2318 void addSetMIFlags(StringRef Flag) { SetFlags.push_back(x: Flag); }
2319 void addUnsetMIFlags(StringRef Flag) { UnsetFlags.push_back(x: Flag); }
2320 void addCopiedMIFlags(const InstructionMatcher &IM) {
2321 CopiedFlags.push_back(x: &IM);
2322 }
2323
2324 void chooseInsnToMutate(RuleMatcher &Rule);
2325
2326 void setDeadImplicitDef(const Record *R) { DeadImplicitDefs.insert(Ptr: R); }
2327
2328 template <class Kind, class... Args> Kind &addRenderer(Args &&...args) {
2329 OperandRenderers.emplace_back(
2330 std::make_unique<Kind>(InsnID, std::forward<Args>(args)...));
2331 return *static_cast<Kind *>(OperandRenderers.back().get());
2332 }
2333
2334 void emitActionOpcodes(MatchTable &Table) const override;
2335};
2336
2337/// Generates code to create a constant that defines a TempReg.
2338/// The instruction created is usually a G_CONSTANT but it could also be a
2339/// G_BUILD_VECTOR for vector types.
2340class BuildConstantAction : public MatchAction {
2341 unsigned TempRegID;
2342 int64_t Val;
2343
2344public:
2345 BuildConstantAction(unsigned TempRegID, int64_t Val)
2346 : MatchAction(AK_BuildConstantMI), TempRegID(TempRegID), Val(Val) {}
2347
2348 static bool classof(const MatchAction *A) {
2349 return A->getKind() == AK_BuildConstantMI;
2350 }
2351
2352 void emitActionOpcodes(MatchTable &Table) const override;
2353};
2354
2355class EraseInstAction : public MatchAction {
2356 unsigned InsnID;
2357
2358public:
2359 EraseInstAction(unsigned InsnID)
2360 : MatchAction(AK_EraseInst), InsnID(InsnID) {}
2361
2362 unsigned getInsnID() const { return InsnID; }
2363
2364 static bool classof(const MatchAction *A) {
2365 return A->getKind() == AK_EraseInst;
2366 }
2367
2368 void emitActionOpcodes(MatchTable &Table) const override;
2369 bool emitActionOpcodesAndDone(MatchTable &Table,
2370 function_ref<void()> OnDone) const override;
2371};
2372
2373class ReplaceRegAction : public MatchAction {
2374 unsigned OldInsnID, OldOpIdx;
2375 unsigned NewInsnId = -1, NewOpIdx;
2376 unsigned TempRegID = -1;
2377
2378public:
2379 ReplaceRegAction(unsigned OldInsnID, unsigned OldOpIdx, unsigned NewInsnId,
2380 unsigned NewOpIdx)
2381 : MatchAction(AK_ReplaceReg), OldInsnID(OldInsnID), OldOpIdx(OldOpIdx),
2382 NewInsnId(NewInsnId), NewOpIdx(NewOpIdx) {}
2383
2384 ReplaceRegAction(unsigned OldInsnID, unsigned OldOpIdx, unsigned TempRegID)
2385 : MatchAction(AK_ReplaceReg), OldInsnID(OldInsnID), OldOpIdx(OldOpIdx),
2386 TempRegID(TempRegID) {}
2387
2388 static bool classof(const MatchAction *A) {
2389 return A->getKind() == AK_ReplaceReg;
2390 }
2391
2392 void emitAdditionalPredicates(MatchTable &Table) const override;
2393 void emitActionOpcodes(MatchTable &Table) const override;
2394};
2395
2396/// Generates code to constrain the operands of an output instruction to the
2397/// register classes specified by the definition of that instruction.
2398class ConstrainOperandsToDefinitionAction : public MatchAction {
2399 unsigned InsnID;
2400
2401public:
2402 ConstrainOperandsToDefinitionAction(unsigned InsnID)
2403 : MatchAction(AK_ConstraintOpsToDef), InsnID(InsnID) {}
2404
2405 static bool classof(const MatchAction *A) {
2406 return A->getKind() == AK_ConstraintOpsToDef;
2407 }
2408
2409 void emitActionOpcodes(MatchTable &Table) const override {
2410 if (InsnID == 0) {
2411 Table << MatchTable::Opcode(Opcode: "GIR_RootConstrainSelectedInstOperands")
2412 << MatchTable::LineBreak;
2413 } else {
2414 Table << MatchTable::Opcode(Opcode: "GIR_ConstrainSelectedInstOperands")
2415 << MatchTable::Comment(Comment: "InsnID") << MatchTable::ULEB128Value(IntValue: InsnID)
2416 << MatchTable::LineBreak;
2417 }
2418 }
2419};
2420
2421/// Generates code to constrain the specified operand of an output instruction
2422/// to the specified register class.
2423class ConstrainOperandToRegClassAction : public MatchAction {
2424 unsigned InsnID;
2425 unsigned OpIdx;
2426 const CodeGenRegisterClass &RC;
2427
2428public:
2429 ConstrainOperandToRegClassAction(unsigned InsnID, unsigned OpIdx,
2430 const CodeGenRegisterClass &RC)
2431 : MatchAction(AK_ConstraintOpsToRC), InsnID(InsnID), OpIdx(OpIdx),
2432 RC(RC) {}
2433
2434 static bool classof(const MatchAction *A) {
2435 return A->getKind() == AK_ConstraintOpsToRC;
2436 }
2437
2438 void emitActionOpcodes(MatchTable &Table) const override;
2439};
2440
2441/// Generates code to create a temporary register which can be used to chain
2442/// instructions together.
2443class MakeTempRegisterAction : public MatchAction {
2444private:
2445 LLTCodeGenOrTempType Ty;
2446 unsigned TempRegID;
2447
2448public:
2449 MakeTempRegisterAction(const LLTCodeGenOrTempType &Ty, unsigned TempRegID)
2450 : MatchAction(AK_MakeTempReg), Ty(Ty), TempRegID(TempRegID) {
2451 if (Ty.isLLTCodeGen())
2452 KnownTypes.insert(x: Ty.getLLTCodeGen());
2453 }
2454
2455 static bool classof(const MatchAction *A) {
2456 return A->getKind() == AK_MakeTempReg;
2457 }
2458
2459 void emitActionOpcodes(MatchTable &Table) const override;
2460};
2461
2462} // namespace gi
2463} // namespace llvm
2464
2465#endif // LLVM_UTILS_TABLEGEN_COMMON_GLOBALISEL_GLOBALISELMATCHERS_H
2466