1//===- CodeGenDAGPatterns.h - Read DAG patterns from .td file ---*- C++ -*-===//
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// This file declares the CodeGenDAGPatterns class, which is used to read and
10// represent the patterns present in a .td file for instructions.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_UTILS_TABLEGEN_COMMON_CODEGENDAGPATTERNS_H
15#define LLVM_UTILS_TABLEGEN_COMMON_CODEGENDAGPATTERNS_H
16
17#include "Basic/CodeGenIntrinsics.h"
18#include "Basic/SDNodeProperties.h"
19#include "CodeGenTarget.h"
20#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/IntrusiveRefCntPtr.h"
22#include "llvm/ADT/MapVector.h"
23#include "llvm/ADT/PointerUnion.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/StringMap.h"
26#include "llvm/ADT/StringSet.h"
27#include "llvm/ADT/Twine.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/MathExtras.h"
30#include "llvm/TableGen/Record.h"
31#include <algorithm>
32#include <array>
33#include <map>
34#include <numeric>
35#include <vector>
36
37namespace llvm {
38
39class Init;
40class ListInit;
41class DagInit;
42class SDNodeInfo;
43class TreePattern;
44class TreePatternNode;
45class CodeGenDAGPatterns;
46
47/// Shared pointer for TreePatternNode.
48using TreePatternNodePtr = IntrusiveRefCntPtr<TreePatternNode>;
49
50/// This represents a set of MVTs. Since the underlying type for the MVT
51/// is uint16_t, there are at most 65536 values. To reduce the number of memory
52/// allocations and deallocations, represent the set as a sequence of bits.
53/// To reduce the allocations even further, make MachineValueTypeSet own
54/// the storage and use std::array as the bit container.
55struct MachineValueTypeSet {
56 static unsigned constexpr Capacity = 512;
57 using WordType = uint64_t;
58 static unsigned constexpr WordWidth = CHAR_BIT * sizeof(WordType);
59 static unsigned constexpr NumWords = Capacity / WordWidth;
60 static_assert(NumWords * WordWidth == Capacity,
61 "Capacity should be a multiple of WordWidth");
62
63 LLVM_ATTRIBUTE_ALWAYS_INLINE
64 MachineValueTypeSet() { clear(); }
65
66 LLVM_ATTRIBUTE_ALWAYS_INLINE
67 unsigned size() const {
68 unsigned Count = 0;
69 for (WordType W : Words)
70 Count += llvm::popcount(Value: W);
71 return Count;
72 }
73 LLVM_ATTRIBUTE_ALWAYS_INLINE
74 void clear() { Words.fill(u: 0); }
75 LLVM_ATTRIBUTE_ALWAYS_INLINE
76 bool empty() const {
77 for (WordType W : Words)
78 if (W != 0)
79 return false;
80 return true;
81 }
82 LLVM_ATTRIBUTE_ALWAYS_INLINE
83 unsigned count(MVT T) const {
84 assert(T.SimpleTy < Capacity && "Capacity needs to be enlarged");
85 return (Words[T.SimpleTy / WordWidth] >> (T.SimpleTy % WordWidth)) & 1;
86 }
87 std::pair<MachineValueTypeSet &, bool> insert(MVT T) {
88 assert(T.SimpleTy < Capacity && "Capacity needs to be enlarged");
89 bool V = count(T);
90 Words[T.SimpleTy / WordWidth] |= WordType(1) << (T.SimpleTy % WordWidth);
91 return {*this, V};
92 }
93 MachineValueTypeSet &insert(const MachineValueTypeSet &S) {
94 for (unsigned i = 0; i != NumWords; ++i)
95 Words[i] |= S.Words[i];
96 return *this;
97 }
98 LLVM_ATTRIBUTE_ALWAYS_INLINE
99 void erase(MVT T) {
100 assert(T.SimpleTy < Capacity && "Capacity needs to be enlarged");
101 Words[T.SimpleTy / WordWidth] &= ~(WordType(1) << (T.SimpleTy % WordWidth));
102 }
103
104 void writeToStream(raw_ostream &OS) const;
105
106 struct const_iterator {
107 // Some implementations of the C++ library require these traits to be
108 // defined.
109 using iterator_category = std::forward_iterator_tag;
110 using value_type = MVT;
111 using difference_type = ptrdiff_t;
112 using pointer = const MVT *;
113 using reference = const MVT &;
114
115 LLVM_ATTRIBUTE_ALWAYS_INLINE
116 MVT operator*() const {
117 assert(Pos != Capacity);
118 return MVT::SimpleValueType(Pos);
119 }
120 LLVM_ATTRIBUTE_ALWAYS_INLINE
121 const_iterator(const MachineValueTypeSet *S, bool End) : Set(S) {
122 Pos = End ? Capacity : find_from_pos(P: 0);
123 }
124 LLVM_ATTRIBUTE_ALWAYS_INLINE
125 const_iterator &operator++() {
126 assert(Pos != Capacity);
127 Pos = find_from_pos(P: Pos + 1);
128 return *this;
129 }
130
131 LLVM_ATTRIBUTE_ALWAYS_INLINE
132 bool operator==(const const_iterator &It) const {
133 return Set == It.Set && Pos == It.Pos;
134 }
135 LLVM_ATTRIBUTE_ALWAYS_INLINE
136 bool operator!=(const const_iterator &It) const { return !operator==(It); }
137
138 private:
139 unsigned find_from_pos(unsigned P) const {
140 unsigned SkipWords = P / WordWidth;
141
142 for (unsigned i = SkipWords; i != NumWords; ++i) {
143 WordType W = Set->Words[i];
144
145 // If P is in the middle of a word, process it manually here, because
146 // the trailing bits need to be masked off to use countr_zero.
147 if (i == SkipWords) {
148 unsigned SkipBits = P % WordWidth;
149 W &= maskTrailingZeros<WordType>(N: SkipBits);
150 }
151
152 if (W != 0)
153 return i * WordWidth + llvm::countr_zero(Val: W);
154 }
155 return Capacity;
156 }
157
158 const MachineValueTypeSet *Set;
159 unsigned Pos;
160 };
161
162 LLVM_ATTRIBUTE_ALWAYS_INLINE
163 const_iterator begin() const { return const_iterator(this, false); }
164 LLVM_ATTRIBUTE_ALWAYS_INLINE
165 const_iterator end() const { return const_iterator(this, true); }
166
167 LLVM_ATTRIBUTE_ALWAYS_INLINE
168 bool operator==(const MachineValueTypeSet &S) const {
169 return Words == S.Words;
170 }
171 LLVM_ATTRIBUTE_ALWAYS_INLINE
172 bool operator!=(const MachineValueTypeSet &S) const { return !operator==(S); }
173
174private:
175 friend struct const_iterator;
176 std::array<WordType, NumWords> Words;
177};
178
179raw_ostream &operator<<(raw_ostream &OS, const MachineValueTypeSet &T);
180
181struct TypeSetByHwMode : public InfoByHwMode<MachineValueTypeSet> {
182 using SetType = MachineValueTypeSet;
183
184 TypeSetByHwMode() = default;
185 TypeSetByHwMode(const TypeSetByHwMode &VTS) = default;
186 TypeSetByHwMode &operator=(const TypeSetByHwMode &) = default;
187 TypeSetByHwMode(MVT VT) : TypeSetByHwMode(ValueTypeByHwMode(VT)) {}
188 TypeSetByHwMode(ArrayRef<ValueTypeByHwMode> VTList);
189
190 SetType &getOrCreate(unsigned Mode) { return Map[Mode]; }
191
192 bool isValueTypeByHwMode(bool AllowEmpty) const;
193 ValueTypeByHwMode getValueTypeByHwMode(bool SkipEmpty = false) const;
194
195 LLVM_ATTRIBUTE_ALWAYS_INLINE
196 bool isMachineValueType() const {
197 return isSimple() && getSimple().size() == 1;
198 }
199
200 LLVM_ATTRIBUTE_ALWAYS_INLINE
201 MVT getMachineValueType() const {
202 assert(isMachineValueType());
203 return *getSimple().begin();
204 }
205
206 bool isPossible() const;
207
208 bool isPointer() const {
209 return PtrAddrSpace != std::numeric_limits<unsigned>::max();
210 }
211
212 unsigned getPtrAddrSpace() const {
213 assert(isPointer());
214 return PtrAddrSpace;
215 }
216
217 bool insert(const ValueTypeByHwMode &VVT);
218 bool constrain(const TypeSetByHwMode &VTS);
219 template <typename Predicate> bool constrain(Predicate P);
220 template <typename Predicate>
221 bool assign_if(const TypeSetByHwMode &VTS, Predicate P);
222
223 void writeToStream(raw_ostream &OS) const;
224
225 bool operator==(const TypeSetByHwMode &VTS) const;
226 bool operator!=(const TypeSetByHwMode &VTS) const { return !(*this == VTS); }
227
228 void dump() const;
229 bool validate() const;
230
231private:
232 unsigned PtrAddrSpace = std::numeric_limits<unsigned>::max();
233 /// Intersect two sets. Return true if anything has changed.
234 bool intersect(SetType &Out, const SetType &In);
235};
236
237raw_ostream &operator<<(raw_ostream &OS, const TypeSetByHwMode &T);
238
239struct TypeInfer {
240 TypeInfer(TreePattern &T) : TP(T) {}
241
242 /// The protocol in the following functions (Merge*, force*, Enforce*,
243 /// expand*) is to return "true" if a change has been made, "false"
244 /// otherwise.
245
246 bool MergeInTypeInfo(TypeSetByHwMode &Out, const TypeSetByHwMode &In) const;
247 bool MergeInTypeInfo(TypeSetByHwMode &Out, MVT InVT) const {
248 return MergeInTypeInfo(Out, In: TypeSetByHwMode(InVT));
249 }
250 bool MergeInTypeInfo(TypeSetByHwMode &Out,
251 const ValueTypeByHwMode &InVT) const {
252 return MergeInTypeInfo(Out, In: TypeSetByHwMode(InVT));
253 }
254
255 /// Reduce the set \p Out to have at most one element for each mode.
256 bool forceArbitrary(TypeSetByHwMode &Out);
257
258 /// The following four functions ensure that upon return the set \p Out
259 /// will only contain types of the specified kind: integer, floating-point,
260 /// scalar, or vector.
261 /// If \p Out is empty, all legal types of the specified kind will be added
262 /// to it. Otherwise, all types that are not of the specified kind will be
263 /// removed from \p Out.
264 bool EnforceInteger(TypeSetByHwMode &Out);
265 bool EnforceFloatingPoint(TypeSetByHwMode &Out);
266 bool EnforceScalar(TypeSetByHwMode &Out);
267 bool EnforceVector(TypeSetByHwMode &Out);
268
269 /// If \p Out is empty, fill it with all legal types. Otherwise, leave it
270 /// unchanged.
271 bool EnforceAny(TypeSetByHwMode &Out);
272 /// Make sure that for each type in \p Small, there exists a larger type
273 /// in \p Big. \p SmallIsVT indicates that this is being called for
274 /// SDTCisVTSmallerThanOp. In that case the TypeSetByHwMode is re-created for
275 /// each call and needs special consideration in how we detect changes.
276 bool EnforceSmallerThan(TypeSetByHwMode &Small, TypeSetByHwMode &Big,
277 bool SmallIsVT = false);
278 /// 1. Ensure that for each type T in \p Vec, T is a vector type, and that
279 /// for each type U in \p Elem, U is a scalar type.
280 /// 2. Ensure that for each (scalar) type U in \p Elem, there exists a
281 /// (vector) type T in \p Vec, such that U is the element type of T.
282 bool EnforceVectorEltTypeIs(TypeSetByHwMode &Vec, TypeSetByHwMode &Elem);
283 bool EnforceVectorEltTypeIs(TypeSetByHwMode &Vec,
284 const ValueTypeByHwMode &VVT);
285 /// Ensure that for each type T in \p Sub, T is a vector type, and there
286 /// exists a type U in \p Vec such that U is a vector type with the same
287 /// element type as T and at least as many elements as T.
288 bool EnforceVectorSubVectorTypeIs(TypeSetByHwMode &Vec, TypeSetByHwMode &Sub);
289 /// 1. Ensure that \p V has a scalar type iff \p W has a scalar type.
290 /// 2. Ensure that for each vector type T in \p V, there exists a vector
291 /// type U in \p W, such that T and U have the same number of elements.
292 /// 3. Ensure that for each vector type U in \p W, there exists a vector
293 /// type T in \p V, such that T and U have the same number of elements
294 /// (reverse of 2).
295 bool EnforceSameNumElts(TypeSetByHwMode &V, TypeSetByHwMode &W);
296 /// 1. Ensure that for each type T in \p A, there exists a type U in \p B,
297 /// such that T and U have equal size in bits.
298 /// 2. Ensure that for each type U in \p B, there exists a type T in \p A
299 /// such that T and U have equal size in bits (reverse of 1).
300 bool EnforceSameSize(TypeSetByHwMode &A, TypeSetByHwMode &B);
301
302 /// For each overloaded type (i.e. of form *Any), replace it with the
303 /// corresponding subset of legal, specific types.
304 void expandOverloads(TypeSetByHwMode &VTS) const;
305 void expandOverloads(TypeSetByHwMode::SetType &Out,
306 const TypeSetByHwMode::SetType &Legal) const;
307
308 struct ValidateOnExit {
309 ValidateOnExit(const TypeSetByHwMode &T, const TypeInfer &TI)
310 : Infer(TI), VTS(T) {}
311 ~ValidateOnExit();
312 const TypeInfer &Infer;
313 const TypeSetByHwMode &VTS;
314 };
315
316 struct SuppressValidation {
317 SuppressValidation(TypeInfer &TI) : Infer(TI), SavedValidate(TI.Validate) {
318 Infer.Validate = false;
319 }
320 ~SuppressValidation() { Infer.Validate = SavedValidate; }
321 TypeInfer &Infer;
322 bool SavedValidate;
323 };
324
325 TreePattern &TP;
326 bool Validate = true; // Indicate whether to validate types.
327
328private:
329 const TypeSetByHwMode &getLegalTypes() const;
330
331 /// Cached legal types (in default mode).
332 mutable bool LegalTypesCached = false;
333 mutable TypeSetByHwMode LegalCache;
334};
335
336/// Set type used to track multiply used variables in patterns
337using MultipleUseVarSet = StringSet<>;
338
339/// SDTypeConstraint - This is a discriminated union of constraints,
340/// corresponding to the SDTypeConstraint tablegen class in Target.td.
341struct SDTypeConstraint {
342 SDTypeConstraint() = default;
343 SDTypeConstraint(const Record *R, const CodeGenHwModes &CGH);
344
345 unsigned OperandNo; // The operand # this constraint applies to.
346 enum KindTy {
347 SDTCisVT,
348 SDTCisPtrTy,
349 SDTCisInt,
350 SDTCisFP,
351 SDTCisVec,
352 SDTCisSameAs,
353 SDTCisVTSmallerThanOp,
354 SDTCisOpSmallerThanOp,
355 SDTCisEltOfVec,
356 SDTCisSubVecOfVec,
357 SDTCVecEltisVT,
358 SDTCisSameNumEltsAs,
359 SDTCisSameSizeAs
360 } ConstraintType;
361
362 unsigned OtherOperandNo;
363
364 // The VT for SDTCisVT and SDTCVecEltisVT.
365 // Must not be in the union because it has a non-trivial destructor.
366 ValueTypeByHwMode VVT;
367
368 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
369 /// constraint to the nodes operands. This returns true if it makes a
370 /// change, false otherwise. If a type contradiction is found, an error
371 /// is flagged.
372 bool ApplyTypeConstraint(TreePatternNode &N, const SDNodeInfo &NodeInfo,
373 TreePattern &TP) const;
374
375 friend bool operator==(const SDTypeConstraint &LHS,
376 const SDTypeConstraint &RHS);
377 friend bool operator<(const SDTypeConstraint &LHS,
378 const SDTypeConstraint &RHS);
379};
380
381bool operator==(const SDTypeConstraint &LHS, const SDTypeConstraint &RHS);
382bool operator<(const SDTypeConstraint &LHS, const SDTypeConstraint &RHS);
383
384/// ScopedName - A name of a node associated with a "scope" that indicates
385/// the context (e.g. instance of Pattern or PatFrag) in which the name was
386/// used. This enables substitution of pattern fragments while keeping track
387/// of what name(s) were originally given to various nodes in the tree.
388class ScopedName {
389 unsigned Scope;
390 std::string Identifier;
391
392public:
393 ScopedName(unsigned Scope, StringRef Identifier)
394 : Scope(Scope), Identifier(Identifier.str()) {
395 assert(Scope != 0 &&
396 "Scope == 0 is used to indicate predicates without arguments");
397 }
398
399 unsigned getScope() const { return Scope; }
400 const std::string &getIdentifier() const { return Identifier; }
401
402 bool operator==(const ScopedName &o) const;
403 bool operator!=(const ScopedName &o) const;
404};
405
406/// SDNodeInfo - One of these records is created for each SDNode instance in
407/// the target .td file. This represents the various dag nodes we will be
408/// processing.
409class SDNodeInfo {
410 const Record *Def;
411 StringRef EnumName;
412 StringRef SDClassName;
413 unsigned NumResults;
414 int NumOperands;
415 unsigned Properties;
416 bool IsStrictFP;
417 uint32_t TSFlags;
418 std::vector<SDTypeConstraint> TypeConstraints;
419
420public:
421 // Parse the specified record.
422 SDNodeInfo(const Record *R, const CodeGenHwModes &CGH);
423
424 unsigned getNumResults() const { return NumResults; }
425
426 /// getNumOperands - This is the number of operands required or -1 if
427 /// variadic.
428 int getNumOperands() const { return NumOperands; }
429 const Record *getRecord() const { return Def; }
430 StringRef getEnumName() const { return EnumName; }
431 StringRef getSDClassName() const { return SDClassName; }
432
433 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
434 return TypeConstraints;
435 }
436
437 /// getKnownType - If the type constraints on this node imply a fixed type
438 /// (e.g. all stores return void, etc), then return it as an
439 /// MVT. Otherwise, return MVT::Other.
440 MVT getKnownType(unsigned ResNo) const;
441
442 unsigned getProperties() const { return Properties; }
443
444 /// hasProperty - Return true if this node has the specified property.
445 ///
446 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
447
448 bool isStrictFP() const { return IsStrictFP; }
449
450 uint32_t getTSFlags() const { return TSFlags; }
451
452 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
453 /// constraints for this node to the operands of the node. This returns
454 /// true if it makes a change, false otherwise. If a type contradiction is
455 /// found, an error is flagged.
456 bool ApplyTypeConstraints(TreePatternNode &N, TreePattern &TP) const;
457};
458
459/// TreePredicateFn - This is an abstraction that represents the predicates on
460/// a PatFrag node. This is a simple one-word wrapper around a pointer to
461/// provide nice accessors.
462class TreePredicateFn {
463 /// PatFragRec - This is the TreePattern for the PatFrag that we
464 /// originally came from.
465 TreePattern *PatFragRec;
466
467public:
468 /// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag.
469 TreePredicateFn(TreePattern *N);
470
471 TreePattern *getOrigPatFragRecord() const { return PatFragRec; }
472
473 /// isAlwaysTrue - Return true if this is a noop predicate.
474 bool isAlwaysTrue() const;
475
476 bool isImmediatePattern() const { return hasImmCode(); }
477
478 /// getImmediatePredicateCode - Return the code that evaluates this pattern if
479 /// this is an immediate predicate. It is an error to call this on a
480 /// non-immediate pattern.
481 std::string getImmediatePredicateCode() const {
482 std::string Result = getImmCode();
483 assert(!Result.empty() && "Isn't an immediate pattern!");
484 return Result;
485 }
486
487 bool operator==(const TreePredicateFn &RHS) const {
488 return PatFragRec == RHS.PatFragRec;
489 }
490
491 bool operator!=(const TreePredicateFn &RHS) const { return !(*this == RHS); }
492
493 /// Return the name to use in the generated code to reference this, this is
494 /// "Predicate_foo" if from a pattern fragment "foo".
495 std::string getFnName() const;
496
497 /// getCodeToRunOnSDNode - Return the code for the function body that
498 /// evaluates this predicate. The argument is expected to be in "Node",
499 /// not N. This handles casting and conversion to a concrete node type as
500 /// appropriate.
501 std::string getCodeToRunOnSDNode() const;
502
503 /// Get the data type of the argument to getImmediatePredicateCode().
504 StringRef getImmType() const;
505
506 /// Get a string that describes the type returned by getImmType() but is
507 /// usable as part of an identifier.
508 StringRef getImmTypeIdentifier() const;
509
510 // Predicate code uses the PatFrag's captured operands.
511 bool usesOperands() const;
512
513 // Check if the HasNoUse predicate is set.
514 bool hasNoUse() const;
515 // Check if the HasOneUse predicate is set.
516 bool hasOneUse() const;
517
518 // Is the desired predefined predicate for a load?
519 bool isLoad() const;
520 // Is the desired predefined predicate for a store?
521 bool isStore() const;
522 // Is the desired predefined predicate for an atomic?
523 bool isAtomic() const;
524
525 /// Is this predicate the predefined unindexed load predicate?
526 /// Is this predicate the predefined unindexed store predicate?
527 bool isUnindexed() const;
528 /// Is this predicate the predefined non-extending load predicate?
529 bool isNonExtLoad() const;
530 /// Is this predicate the predefined any-extend load predicate?
531 bool isAnyExtLoad() const;
532 /// Is this predicate the predefined sign-extend load predicate?
533 bool isSignExtLoad() const;
534 /// Is this predicate the predefined zero-extend load predicate?
535 bool isZeroExtLoad() const;
536 /// Is this predicate the predefined non-truncating store predicate?
537 bool isNonTruncStore() const;
538 /// Is this predicate the predefined truncating store predicate?
539 bool isTruncStore() const;
540
541 /// Is this predicate the predefined monotonic atomic predicate?
542 bool isAtomicOrderingMonotonic() const;
543 /// Is this predicate the predefined acquire atomic predicate?
544 bool isAtomicOrderingAcquire() const;
545 /// Is this predicate the predefined release atomic predicate?
546 bool isAtomicOrderingRelease() const;
547 /// Is this predicate the predefined acquire-release atomic predicate?
548 bool isAtomicOrderingAcquireRelease() const;
549 /// Is this predicate the predefined sequentially consistent atomic predicate?
550 bool isAtomicOrderingSequentiallyConsistent() const;
551
552 /// Is this predicate the predefined acquire-or-stronger atomic predicate?
553 bool isAtomicOrderingAcquireOrStronger() const;
554 /// Is this predicate the predefined weaker-than-acquire atomic predicate?
555 bool isAtomicOrderingWeakerThanAcquire() const;
556
557 /// Is this predicate the predefined release-or-stronger atomic predicate?
558 bool isAtomicOrderingReleaseOrStronger() const;
559 /// Is this predicate the predefined weaker-than-release atomic predicate?
560 bool isAtomicOrderingWeakerThanRelease() const;
561
562 /// If non-null, indicates that this predicate is a predefined memory VT
563 /// predicate for a load/store and returns the ValueType record for the memory
564 /// VT.
565 const Record *getMemoryVT() const;
566 /// If non-null, indicates that this predicate is a predefined memory VT
567 /// predicate (checking only the scalar type) for load/store and returns the
568 /// ValueType record for the memory VT.
569 const Record *getScalarMemoryVT() const;
570
571 const ListInit *getAddressSpaces() const;
572 int64_t getMinAlignment() const;
573
574 // If true, indicates that GlobalISel-based C++ code was supplied.
575 bool hasGISelPredicateCode() const;
576 std::string getGISelPredicateCode() const;
577
578 // If true, indicates that GlobalISel-based C++ code was supplied for checking
579 // register operands.
580 bool hasGISelLeafPredicateCode() const;
581 std::string getGISelLeafPredicateCode() const;
582
583private:
584 bool hasPredCode() const;
585 bool hasImmCode() const;
586 std::string getPredCode() const;
587 std::string getImmCode() const;
588 bool immCodeUsesAPInt() const;
589 bool immCodeUsesAPFloat() const;
590
591 bool isPredefinedPredicateEqualTo(StringRef Field, bool Value) const;
592};
593
594struct TreePredicateCall {
595 TreePredicateFn Fn;
596
597 // Scope -- unique identifier for retrieving named arguments. 0 is used when
598 // the predicate does not use named arguments.
599 unsigned Scope;
600
601 TreePredicateCall(const TreePredicateFn &Fn, unsigned Scope)
602 : Fn(Fn), Scope(Scope) {}
603
604 bool operator==(const TreePredicateCall &o) const {
605 return Fn == o.Fn && Scope == o.Scope;
606 }
607 bool operator!=(const TreePredicateCall &o) const { return !(*this == o); }
608};
609
610class TreePatternNode : public RefCountedBase<TreePatternNode> {
611 /// The type of each node result. Before and during type inference, each
612 /// result may be a set of possible types. After (successful) type inference,
613 /// each is a single concrete type.
614 std::vector<TypeSetByHwMode> Types;
615
616 /// The index of each result in results of the pattern.
617 std::vector<unsigned> ResultPerm;
618
619 /// OperatorOrVal - The Record for the operator if this is an interior node
620 /// (not a leaf) or the init value (e.g. the "GPRC" record, or "7") for a
621 /// leaf.
622 PointerUnion<const Record *, const Init *> OperatorOrVal;
623
624 /// Name - The name given to this node with the :$foo notation.
625 ///
626 StringRef Name;
627
628 std::vector<ScopedName> NamesAsPredicateArg;
629
630 /// PredicateCalls - The predicate functions to execute on this node to check
631 /// for a match. If this list is empty, no predicate is involved.
632 std::vector<TreePredicateCall> PredicateCalls;
633
634 /// TransformFn - The transformation function to execute on this node before
635 /// it can be substituted into the resulting instruction on a pattern match.
636 const Record *TransformFn;
637
638 std::vector<TreePatternNodePtr> Children;
639
640 /// If this was instantiated from a PatFrag node, and the PatFrag was derived
641 /// from "GISelFlags": the original Record derived from GISelFlags.
642 const Record *GISelFlags = nullptr;
643
644public:
645 TreePatternNode(const Record *Op, std::vector<TreePatternNodePtr> Ch,
646 unsigned NumResults)
647 : OperatorOrVal(Op), TransformFn(nullptr), Children(std::move(Ch)) {
648 Types.resize(new_size: NumResults);
649 ResultPerm.resize(new_size: NumResults);
650 std::iota(first: ResultPerm.begin(), last: ResultPerm.end(), value: 0);
651 }
652 TreePatternNode(const Init *val, unsigned NumResults) // leaf ctor
653 : OperatorOrVal(val), TransformFn(nullptr) {
654 Types.resize(new_size: NumResults);
655 ResultPerm.resize(new_size: NumResults);
656 std::iota(first: ResultPerm.begin(), last: ResultPerm.end(), value: 0);
657 }
658
659 bool hasName() const { return !Name.empty(); }
660 StringRef getName() const { return Name; }
661 void setName(StringRef N) { Name = N; }
662
663 const std::vector<ScopedName> &getNamesAsPredicateArg() const {
664 return NamesAsPredicateArg;
665 }
666 void setNamesAsPredicateArg(const std::vector<ScopedName> &Names) {
667 NamesAsPredicateArg = Names;
668 }
669 void addNameAsPredicateArg(const ScopedName &N) {
670 NamesAsPredicateArg.push_back(x: N);
671 }
672
673 bool isLeaf() const { return isa<const Init *>(Val: OperatorOrVal); }
674
675 // Type accessors.
676 unsigned getNumTypes() const { return Types.size(); }
677 ValueTypeByHwMode getType(unsigned ResNo) const {
678 return Types[ResNo].getValueTypeByHwMode(/*SkipEmpty=*/SkipEmpty: true);
679 }
680 const std::vector<TypeSetByHwMode> &getExtTypes() const { return Types; }
681 const TypeSetByHwMode &getExtType(unsigned ResNo) const {
682 return Types[ResNo];
683 }
684 TypeSetByHwMode &getExtType(unsigned ResNo) { return Types[ResNo]; }
685 void setType(unsigned ResNo, const TypeSetByHwMode &T) { Types[ResNo] = T; }
686 MVT getSimpleType(unsigned ResNo) const {
687 return Types[ResNo].getMachineValueType();
688 }
689
690 bool hasConcreteType(unsigned ResNo) const {
691 return Types[ResNo].isValueTypeByHwMode(AllowEmpty: false);
692 }
693 bool isTypeCompletelyUnknown(unsigned ResNo, TreePattern &TP) const {
694 return Types[ResNo].empty();
695 }
696
697 unsigned getNumResults() const { return ResultPerm.size(); }
698 unsigned getResultIndex(unsigned ResNo) const { return ResultPerm[ResNo]; }
699 void setResultIndex(unsigned ResNo, unsigned RI) { ResultPerm[ResNo] = RI; }
700
701 const Init *getLeafValue() const {
702 assert(isLeaf());
703 return cast<const Init *>(Val: OperatorOrVal);
704 }
705 const Record *getOperator() const {
706 assert(!isLeaf());
707 return cast<const Record *>(Val: OperatorOrVal);
708 }
709
710 using child_iterator = pointee_iterator<decltype(Children)::iterator>;
711 using child_const_iterator =
712 pointee_iterator<decltype(Children)::const_iterator>;
713
714 iterator_range<child_iterator> children() {
715 return make_pointee_range(Range&: Children);
716 }
717
718 iterator_range<child_const_iterator> children() const {
719 return make_pointee_range(Range: Children);
720 }
721
722 unsigned getNumChildren() const { return Children.size(); }
723 const TreePatternNode &getChild(unsigned N) const {
724 return *Children[N].get();
725 }
726 TreePatternNode &getChild(unsigned N) { return *Children[N].get(); }
727 const TreePatternNodePtr &getChildShared(unsigned N) const {
728 return Children[N];
729 }
730 TreePatternNodePtr &getChildSharedPtr(unsigned N) { return Children[N]; }
731 void setChild(unsigned i, TreePatternNodePtr N) { Children[i] = N; }
732
733 /// hasChild - Return true if N is any of our children.
734 bool hasChild(const TreePatternNode *N) const {
735 for (const TreePatternNodePtr &Child : Children)
736 if (Child.get() == N)
737 return true;
738 return false;
739 }
740
741 bool hasProperTypeByHwMode() const;
742 bool hasPossibleType() const;
743 bool setDefaultMode(unsigned Mode);
744
745 bool hasAnyPredicate() const { return !PredicateCalls.empty(); }
746
747 const std::vector<TreePredicateCall> &getPredicateCalls() const {
748 return PredicateCalls;
749 }
750 void clearPredicateCalls() { PredicateCalls.clear(); }
751 void setPredicateCalls(const std::vector<TreePredicateCall> &Calls) {
752 assert(PredicateCalls.empty() && "Overwriting non-empty predicate list!");
753 PredicateCalls = Calls;
754 }
755 void addPredicateCall(const TreePredicateCall &Call) {
756 assert(!Call.Fn.isAlwaysTrue() && "Empty predicate string!");
757 assert(!is_contained(PredicateCalls, Call) &&
758 "predicate applied recursively");
759 PredicateCalls.push_back(x: Call);
760 }
761 void addPredicateCall(const TreePredicateFn &Fn, unsigned Scope) {
762 assert((Scope != 0) == Fn.usesOperands());
763 addPredicateCall(Call: TreePredicateCall(Fn, Scope));
764 }
765
766 const Record *getTransformFn() const { return TransformFn; }
767 void setTransformFn(const Record *Fn) { TransformFn = Fn; }
768
769 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
770 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
771 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
772
773 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
774 /// return the ComplexPattern information, otherwise return null.
775 const ComplexPattern *
776 getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
777
778 /// Returns the number of MachineInstr operands that would be produced by this
779 /// node if it mapped directly to an output Instruction's
780 /// operand. ComplexPattern specifies this explicitly; MIOperandInfo gives it
781 /// for Operands; otherwise 1.
782 unsigned getNumMIResults(const CodeGenDAGPatterns &CGP) const;
783
784 /// NodeHasProperty - Return true if this node has the specified property.
785 bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
786
787 /// TreeHasProperty - Return true if any node in this tree has the specified
788 /// property.
789 bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
790
791 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
792 /// marked isCommutative.
793 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
794
795 void setGISelFlagsRecord(const Record *R) { GISelFlags = R; }
796 const Record *getGISelFlagsRecord() const { return GISelFlags; }
797
798 void print(raw_ostream &OS) const;
799 void dump() const;
800
801public: // Higher level manipulation routines.
802 /// clone - Return a new copy of this tree.
803 ///
804 TreePatternNodePtr clone() const;
805
806 /// RemoveAllTypes - Recursively strip all the types of this tree.
807 void RemoveAllTypes();
808
809 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
810 /// the specified node. For this comparison, all of the state of the node
811 /// is considered, except for the assigned name. Nodes with differing names
812 /// that are otherwise identical are considered isomorphic.
813 bool isIsomorphicTo(const TreePatternNode &N,
814 const MultipleUseVarSet &DepVars) const;
815
816 /// SubstituteFormalArguments - Replace the formal arguments in this tree
817 /// with actual values specified by ArgMap.
818 void
819 SubstituteFormalArguments(std::map<StringRef, TreePatternNodePtr> &ArgMap);
820
821 /// InlinePatternFragments - If \p T pattern refers to any pattern
822 /// fragments, return the set of inlined versions (this can be more than
823 /// one if a PatFrags record has multiple alternatives).
824 void InlinePatternFragments(TreePattern &TP,
825 std::vector<TreePatternNodePtr> &OutAlternatives);
826
827 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
828 /// this node and its children in the tree. This returns true if it makes a
829 /// change, false otherwise. If a type contradiction is found, flag an error.
830 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
831
832 /// UpdateNodeType - Set the node type of N to VT if VT contains
833 /// information. If N already contains a conflicting type, then flag an
834 /// error. This returns true if any information was updated.
835 ///
836 bool UpdateNodeType(unsigned ResNo, const TypeSetByHwMode &InTy,
837 TreePattern &TP);
838 bool UpdateNodeType(unsigned ResNo, MVT InTy, TreePattern &TP);
839 bool UpdateNodeType(unsigned ResNo, const ValueTypeByHwMode &InTy,
840 TreePattern &TP);
841
842 // Update node type with types inferred from an instruction operand or result
843 // def from the ins/outs lists.
844 // Return true if the type changed.
845 bool UpdateNodeTypeFromInst(unsigned ResNo, const Record *Operand,
846 TreePattern &TP);
847
848 /// ContainsUnresolvedType - Return true if this tree contains any
849 /// unresolved types.
850 bool ContainsUnresolvedType(TreePattern &TP) const;
851
852 /// canPatternMatch - If it is impossible for this pattern to match on this
853 /// target, fill in Reason and return false. Otherwise, return true.
854 bool canPatternMatch(std::string &Reason,
855 const CodeGenDAGPatterns &CDP) const;
856};
857
858inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
859 TPN.print(OS);
860 return OS;
861}
862
863/// TreePattern - Represent a pattern, used for instructions, pattern
864/// fragments, etc.
865///
866class TreePattern {
867 /// Trees - The list of pattern trees which corresponds to this pattern.
868 /// Note that PatFrag's only have a single tree.
869 ///
870 std::vector<TreePatternNodePtr> Trees;
871
872 /// NamedNodes - This is all of the nodes that have names in the trees in this
873 /// pattern.
874 StringMap<SmallVector<TreePatternNode *, 1>> NamedNodes;
875
876 /// TheRecord - The actual TableGen record corresponding to this pattern.
877 ///
878 const Record *TheRecord;
879
880 /// Args - This is a list of all of the arguments to this pattern (for
881 /// PatFrag patterns), which are the 'node' markers in this pattern.
882 std::vector<std::string> Args;
883
884 /// CDP - the top-level object coordinating this madness.
885 ///
886 CodeGenDAGPatterns &CDP;
887
888 /// isInputPattern - True if this is an input pattern, something to match.
889 /// False if this is an output pattern, something to emit.
890 bool isInputPattern;
891
892 /// hasError - True if the currently processed nodes have unresolvable types
893 /// or other non-fatal errors
894 bool HasError;
895
896 /// It's important that the usage of operands in ComplexPatterns is
897 /// consistent: each named operand can be defined by at most one
898 /// ComplexPattern. This records the ComplexPattern instance and the operand
899 /// number for each operand encountered in a ComplexPattern to aid in that
900 /// check.
901 StringMap<std::pair<const Record *, unsigned>> ComplexPatternOperands;
902
903 TypeInfer Infer;
904
905public:
906 /// TreePattern constructor - Parse the specified DagInits into the
907 /// current record.
908 TreePattern(const Record *TheRec, const ListInit *RawPat, bool isInput,
909 CodeGenDAGPatterns &ise);
910 TreePattern(const Record *TheRec, const DagInit *Pat, bool isInput,
911 CodeGenDAGPatterns &ise);
912 TreePattern(const Record *TheRec, ArrayRef<const Init *> Args,
913 ArrayRef<const StringInit *> ArgNames, bool isInput,
914 CodeGenDAGPatterns &ise);
915 TreePattern(const Record *TheRec, TreePatternNodePtr Pat, bool isInput,
916 CodeGenDAGPatterns &ise);
917
918 /// getTrees - Return the tree patterns which corresponds to this pattern.
919 ///
920 const std::vector<TreePatternNodePtr> &getTrees() const { return Trees; }
921 unsigned getNumTrees() const { return Trees.size(); }
922 const TreePatternNodePtr &getTree(unsigned i) const { return Trees[i]; }
923 const TreePatternNodePtr &getOnlyTree() const {
924 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
925 return Trees[0];
926 }
927
928 const StringMap<SmallVector<TreePatternNode *, 1>> &getNamedNodesMap() {
929 if (NamedNodes.empty())
930 ComputeNamedNodes();
931 return NamedNodes;
932 }
933
934 /// getRecord - Return the actual TableGen record corresponding to this
935 /// pattern.
936 ///
937 const Record *getRecord() const { return TheRecord; }
938
939 unsigned getNumArgs() const { return Args.size(); }
940 const std::string &getArgName(unsigned i) const {
941 assert(i < Args.size() && "Argument reference out of range!");
942 return Args[i];
943 }
944 std::vector<std::string> &getArgList() { return Args; }
945
946 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
947
948 /// InlinePatternFragments - If this pattern refers to any pattern
949 /// fragments, inline them into place, giving us a pattern without any
950 /// PatFrags references. This may increase the number of trees in the
951 /// pattern if a PatFrags has multiple alternatives.
952 void InlinePatternFragments() {
953 std::vector<TreePatternNodePtr> Copy;
954 Trees.swap(x&: Copy);
955 for (const TreePatternNodePtr &C : Copy)
956 C->InlinePatternFragments(TP&: *this, OutAlternatives&: Trees);
957 }
958
959 /// InferAllTypes - Infer/propagate as many types throughout the expression
960 /// patterns as possible. Return true if all types are inferred, false
961 /// otherwise. Bail out if a type contradiction is found.
962 bool InferAllTypes(
963 const StringMap<SmallVector<TreePatternNode *, 1>> *NamedTypes = nullptr);
964
965 /// error - If this is the first error in the current resolution step,
966 /// print it and set the error flag. Otherwise, continue silently.
967 void error(const Twine &Msg);
968 bool hasError() const { return HasError; }
969 void resetError() { HasError = false; }
970
971 TypeInfer &getInfer() { return Infer; }
972
973 void print(raw_ostream &OS) const;
974 void dump() const;
975
976private:
977 TreePatternNodePtr ParseTreePattern(const Init *DI, StringRef OpName);
978 TreePatternNodePtr
979 ParseRootlessTreePattern(ArrayRef<const Init *> Args,
980 ArrayRef<const StringInit *> ArgNames);
981 void ComputeNamedNodes();
982 void ComputeNamedNodes(TreePatternNode &N);
983};
984
985inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
986 const TypeSetByHwMode &InTy,
987 TreePattern &TP) {
988 TypeSetByHwMode VTS(InTy);
989 TP.getInfer().expandOverloads(VTS);
990 return TP.getInfer().MergeInTypeInfo(Out&: Types[ResNo], In: VTS);
991}
992
993inline bool TreePatternNode::UpdateNodeType(unsigned ResNo, MVT InTy,
994 TreePattern &TP) {
995 TypeSetByHwMode VTS(InTy);
996 TP.getInfer().expandOverloads(VTS);
997 return TP.getInfer().MergeInTypeInfo(Out&: Types[ResNo], In: VTS);
998}
999
1000inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
1001 const ValueTypeByHwMode &InTy,
1002 TreePattern &TP) {
1003 TypeSetByHwMode VTS(InTy);
1004 TP.getInfer().expandOverloads(VTS);
1005 return TP.getInfer().MergeInTypeInfo(Out&: Types[ResNo], In: VTS);
1006}
1007
1008/// DAGDefaultOperand - One of these is created for each OperandWithDefaultOps
1009/// that has a set ExecuteAlways / DefaultOps field.
1010struct DAGDefaultOperand {
1011 std::vector<TreePatternNodePtr> DefaultOps;
1012};
1013
1014class DAGInstruction {
1015 std::vector<const Record *> Results;
1016 std::vector<const Record *> Operands;
1017 std::vector<const Record *> ImpResults;
1018 TreePatternNodePtr SrcPattern;
1019 TreePatternNodePtr ResultPattern;
1020
1021public:
1022 DAGInstruction(std::vector<const Record *> &&Results,
1023 std::vector<const Record *> &&Operands,
1024 std::vector<const Record *> &&ImpResults,
1025 TreePatternNodePtr SrcPattern = nullptr,
1026 TreePatternNodePtr ResultPattern = nullptr)
1027 : Results(std::move(Results)), Operands(std::move(Operands)),
1028 ImpResults(std::move(ImpResults)), SrcPattern(SrcPattern),
1029 ResultPattern(ResultPattern) {}
1030
1031 unsigned getNumResults() const { return Results.size(); }
1032 unsigned getNumOperands() const { return Operands.size(); }
1033 unsigned getNumImpResults() const { return ImpResults.size(); }
1034 ArrayRef<const Record *> getImpResults() const { return ImpResults; }
1035
1036 const Record *getResult(unsigned RN) const {
1037 assert(RN < Results.size());
1038 return Results[RN];
1039 }
1040
1041 const Record *getOperand(unsigned ON) const {
1042 assert(ON < Operands.size());
1043 return Operands[ON];
1044 }
1045
1046 const Record *getImpResult(unsigned RN) const {
1047 assert(RN < ImpResults.size());
1048 return ImpResults[RN];
1049 }
1050
1051 TreePatternNodePtr getSrcPattern() const { return SrcPattern; }
1052 TreePatternNodePtr getResultPattern() const { return ResultPattern; }
1053};
1054
1055/// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
1056/// processed to produce isel.
1057class PatternToMatch {
1058 const Record *SrcRecord; // Originating Record for the pattern.
1059 const ListInit *Predicates; // Top level predicate conditions to match.
1060 TreePatternNodePtr SrcPattern; // Source pattern to match.
1061 TreePatternNodePtr DstPattern; // Resulting pattern.
1062 std::vector<const Record *> Dstregs; // Physical register defs being matched.
1063 std::string HwModeFeatures;
1064 int AddedComplexity; // Add to matching pattern complexity.
1065 bool GISelShouldIgnore; // Should GlobalISel ignore importing this pattern.
1066 unsigned ID; // Unique ID for the record.
1067
1068public:
1069 PatternToMatch(const Record *srcrecord, const ListInit *preds,
1070 TreePatternNodePtr src, TreePatternNodePtr dst,
1071 ArrayRef<const Record *> dstregs, int complexity, unsigned uid,
1072 bool ignore, const Twine &hwmodefeatures = "")
1073 : SrcRecord(srcrecord), Predicates(preds), SrcPattern(src),
1074 DstPattern(dst), Dstregs(dstregs), HwModeFeatures(hwmodefeatures.str()),
1075 AddedComplexity(complexity), GISelShouldIgnore(ignore), ID(uid) {}
1076
1077 const Record *getSrcRecord() const { return SrcRecord; }
1078 const ListInit *getPredicates() const { return Predicates; }
1079 TreePatternNode &getSrcPattern() const { return *SrcPattern; }
1080 TreePatternNodePtr getSrcPatternShared() const { return SrcPattern; }
1081 TreePatternNode &getDstPattern() const { return *DstPattern; }
1082 TreePatternNodePtr getDstPatternShared() const { return DstPattern; }
1083 ArrayRef<const Record *> getDstRegs() const { return Dstregs; }
1084 StringRef getHwModeFeatures() const { return HwModeFeatures; }
1085 int getAddedComplexity() const { return AddedComplexity; }
1086 bool getGISelShouldIgnore() const { return GISelShouldIgnore; }
1087 unsigned getID() const { return ID; }
1088
1089 std::string getPredicateCheck() const;
1090 void
1091 getPredicateRecords(SmallVectorImpl<const Record *> &PredicateRecs) const;
1092
1093 /// Compute the complexity metric for the input pattern. This roughly
1094 /// corresponds to the number of nodes that are covered.
1095 int getPatternComplexity(const CodeGenDAGPatterns &CGP) const;
1096};
1097
1098class CodeGenDAGPatterns {
1099public:
1100 using NodeXForm = std::pair<const Record *, std::string>;
1101
1102private:
1103 const RecordKeeper &Records;
1104 CodeGenTarget Target;
1105 CodeGenIntrinsicTable Intrinsics;
1106 DenseMap<const Record *, unsigned> IntrinsicIDs;
1107
1108 std::map<const Record *, SDNodeInfo, LessRecordByID> SDNodes;
1109
1110 std::map<const Record *, NodeXForm, LessRecordByID> SDNodeXForms;
1111 std::map<const Record *, ComplexPattern, LessRecordByID> ComplexPatterns;
1112 std::map<const Record *, std::unique_ptr<TreePattern>, LessRecordByID>
1113 PatternFragments;
1114 std::map<const Record *, DAGDefaultOperand, LessRecordByID> DefaultOperands;
1115 std::map<const Record *, DAGInstruction, LessRecordByID> Instructions;
1116
1117 // Specific SDNode definitions:
1118 const Record *intrinsic_void_sdnode;
1119 const Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
1120
1121 /// PatternsToMatch - All of the things we are matching on the DAG. The first
1122 /// value is the pattern to match, the second pattern is the result to
1123 /// emit.
1124 std::vector<PatternToMatch> PatternsToMatch;
1125
1126 TypeSetByHwMode LegalVTS;
1127 TypeSetByHwMode LegalPtrVTS;
1128
1129 unsigned NumScopes = 0;
1130
1131public:
1132 CodeGenDAGPatterns(const RecordKeeper &R, bool ExpandHwMode = true);
1133
1134 CodeGenTarget &getTargetInfo() { return Target; }
1135 const CodeGenTarget &getTargetInfo() const { return Target; }
1136 const TypeSetByHwMode &getLegalTypes() const { return LegalVTS; }
1137 const TypeSetByHwMode &getLegalPtrTypes() const { return LegalPtrVTS; }
1138
1139 const Record *getSDNodeNamed(StringRef Name) const;
1140
1141 const SDNodeInfo &getSDNodeInfo(const Record *R) const {
1142 auto F = SDNodes.find(x: R);
1143 assert(F != SDNodes.end() && "Unknown node!");
1144 return F->second;
1145 }
1146
1147 // Node transformation lookups.
1148 const NodeXForm &getSDNodeTransform(const Record *R) const {
1149 auto F = SDNodeXForms.find(x: R);
1150 assert(F != SDNodeXForms.end() && "Invalid transform!");
1151 return F->second;
1152 }
1153
1154 const ComplexPattern &getComplexPattern(const Record *R) const {
1155 auto F = ComplexPatterns.find(x: R);
1156 assert(F != ComplexPatterns.end() && "Unknown addressing mode!");
1157 return F->second;
1158 }
1159
1160 const CodeGenIntrinsic &getIntrinsic(const Record *R) const {
1161 return Intrinsics[getIntrinsicID(R)];
1162 }
1163
1164 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
1165 if (IID - 1 < Intrinsics.size())
1166 return Intrinsics[IID - 1];
1167 llvm_unreachable("Bad intrinsic ID!");
1168 }
1169
1170 unsigned getIntrinsicID(const Record *R) const {
1171 auto I = IntrinsicIDs.find(Val: R);
1172 assert(I != IntrinsicIDs.end() && "Unknown intrinsic!");
1173 return I->second;
1174 }
1175
1176 const DAGDefaultOperand &getDefaultOperand(const Record *R) const {
1177 auto F = DefaultOperands.find(x: R);
1178 assert(F != DefaultOperands.end() && "Isn't an analyzed default operand!");
1179 return F->second;
1180 }
1181
1182 // Pattern Fragment information.
1183 TreePattern *getPatternFragment(const Record *R) const {
1184 auto F = PatternFragments.find(x: R);
1185 assert(F != PatternFragments.end() && "Invalid pattern fragment request!");
1186 return F->second.get();
1187 }
1188 TreePattern *getPatternFragmentIfRead(const Record *R) const {
1189 auto F = PatternFragments.find(x: R);
1190 if (F == PatternFragments.end())
1191 return nullptr;
1192 return F->second.get();
1193 }
1194
1195 using pf_iterator = decltype(PatternFragments)::const_iterator;
1196 pf_iterator pf_begin() const { return PatternFragments.begin(); }
1197 pf_iterator pf_end() const { return PatternFragments.end(); }
1198 iterator_range<pf_iterator> ptfs() const { return PatternFragments; }
1199
1200 // Patterns to match information.
1201 using ptm_iterator = std::vector<PatternToMatch>::const_iterator;
1202 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
1203 ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
1204 iterator_range<ptm_iterator> ptms() const { return PatternsToMatch; }
1205
1206 /// Parse the Pattern for an instruction, and insert the result in DAGInsts.
1207 using DAGInstMap = std::map<const Record *, DAGInstruction, LessRecordByID>;
1208 void parseInstructionPattern(const CodeGenInstruction &CGI,
1209 const ListInit *Pattern, DAGInstMap &DAGInsts);
1210
1211 const DAGInstruction &getInstruction(const Record *R) const {
1212 auto F = Instructions.find(x: R);
1213 assert(F != Instructions.end() && "Unknown instruction!");
1214 return F->second;
1215 }
1216
1217 const Record *get_intrinsic_void_sdnode() const {
1218 return intrinsic_void_sdnode;
1219 }
1220 const Record *get_intrinsic_w_chain_sdnode() const {
1221 return intrinsic_w_chain_sdnode;
1222 }
1223 const Record *get_intrinsic_wo_chain_sdnode() const {
1224 return intrinsic_wo_chain_sdnode;
1225 }
1226
1227 unsigned allocateScope() { return ++NumScopes; }
1228
1229 bool operandHasDefault(const Record *Op) const {
1230 return Op->isSubClassOf(Name: "OperandWithDefaultOps") &&
1231 !getDefaultOperand(R: Op).DefaultOps.empty();
1232 }
1233
1234private:
1235 TypeSetByHwMode ComputeLegalPtrTypes() const;
1236 void ParseNodeInfo();
1237 void ParseNodeTransforms();
1238 void ParseComplexPatterns();
1239 void ParsePatternFragments(bool OutFrags = false);
1240 void ParseDefaultOperands();
1241 void ParseInstructions();
1242 void ParsePatterns();
1243 void ExpandHwModeBasedTypes();
1244 void InferInstructionFlags();
1245 void GenerateVariants();
1246 void VerifyInstructionFlags();
1247
1248 void ParseOnePattern(const Record *TheDef, TreePattern &Pattern,
1249 TreePattern &Result,
1250 ArrayRef<const Record *> InstImpResults,
1251 bool ShouldIgnore = false);
1252 void AddPatternToMatch(TreePattern *Pattern, PatternToMatch &&PTM);
1253
1254 using InstInputsTy = std::map<StringRef, TreePatternNodePtr>;
1255 using InstResultsTy =
1256 MapVector<StringRef, TreePatternNodePtr, std::map<StringRef, unsigned>>;
1257 void FindPatternInputsAndOutputs(TreePattern &I, TreePatternNodePtr Pat,
1258 InstInputsTy &InstInputs,
1259 InstResultsTy &InstResults,
1260 std::vector<const Record *> &InstImpResults);
1261 unsigned getNewUID();
1262};
1263
1264inline bool SDNodeInfo::ApplyTypeConstraints(TreePatternNode &N,
1265 TreePattern &TP) const {
1266 bool MadeChange = false;
1267 for (const SDTypeConstraint &TypeConstraint : TypeConstraints)
1268 MadeChange |= TypeConstraint.ApplyTypeConstraint(N, NodeInfo: *this, TP);
1269 return MadeChange;
1270}
1271
1272} // end namespace llvm
1273
1274#endif // LLVM_UTILS_TABLEGEN_COMMON_CODEGENDAGPATTERNS_H
1275