1 //===- CodeGenDAGPatterns.h - Read DAG patterns from .td file ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the CodeGenDAGPatterns class, which is used to read and
11 // represent the patterns present in a .td file for instructions.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_UTILS_TABLEGEN_CODEGENDAGPATTERNS_H
16 #define LLVM_UTILS_TABLEGEN_CODEGENDAGPATTERNS_H
18 #include "CodeGenHwModes.h"
19 #include "CodeGenIntrinsics.h"
20 #include "CodeGenTarget.h"
21 #include "SDNodeProperties.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringMap.h"
24 #include "llvm/ADT/StringSet.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/MathExtras.h"
42 class TreePatternNode;
43 class CodeGenDAGPatterns;
46 /// Shared pointer for TreePatternNode.
47 using TreePatternNodePtr = std::shared_ptr<TreePatternNode>;
49 /// This represents a set of MVTs. Since the underlying type for the MVT
50 /// is uint8_t, there are at most 256 values. To reduce the number of memory
51 /// allocations and deallocations, represent the set as a sequence of bits.
52 /// To reduce the allocations even further, make MachineValueTypeSet own
53 /// the storage and use std::array as the bit container.
54 struct MachineValueTypeSet {
55 static_assert(std::is_same<std::underlying_type<MVT::SimpleValueType>::type,
57 "Change uint8_t here to the SimpleValueType's type");
58 static unsigned constexpr Capacity = std::numeric_limits<uint8_t>::max()+1;
59 using WordType = uint64_t;
60 static unsigned constexpr WordWidth = CHAR_BIT*sizeof(WordType);
61 static unsigned constexpr NumWords = Capacity/WordWidth;
62 static_assert(NumWords*WordWidth == Capacity,
63 "Capacity should be a multiple of WordWidth");
65 LLVM_ATTRIBUTE_ALWAYS_INLINE
66 MachineValueTypeSet() {
70 LLVM_ATTRIBUTE_ALWAYS_INLINE
71 unsigned size() const {
73 for (WordType W : Words)
74 Count += countPopulation(W);
77 LLVM_ATTRIBUTE_ALWAYS_INLINE
79 std::memset(Words.data(), 0, NumWords*sizeof(WordType));
81 LLVM_ATTRIBUTE_ALWAYS_INLINE
83 for (WordType W : Words)
88 LLVM_ATTRIBUTE_ALWAYS_INLINE
89 unsigned count(MVT T) const {
90 return (Words[T.SimpleTy / WordWidth] >> (T.SimpleTy % WordWidth)) & 1;
92 std::pair<MachineValueTypeSet&,bool> insert(MVT T) {
93 bool V = count(T.SimpleTy);
94 Words[T.SimpleTy / WordWidth] |= WordType(1) << (T.SimpleTy % WordWidth);
97 MachineValueTypeSet &insert(const MachineValueTypeSet &S) {
98 for (unsigned i = 0; i != NumWords; ++i)
99 Words[i] |= S.Words[i];
102 LLVM_ATTRIBUTE_ALWAYS_INLINE
104 Words[T.SimpleTy / WordWidth] &= ~(WordType(1) << (T.SimpleTy % WordWidth));
107 struct const_iterator {
108 // Some implementations of the C++ library require these traits to be
110 using iterator_category = std::forward_iterator_tag;
111 using value_type = MVT;
112 using difference_type = ptrdiff_t;
113 using pointer = const MVT*;
114 using reference = const MVT&;
116 LLVM_ATTRIBUTE_ALWAYS_INLINE
117 MVT operator*() const {
118 assert(Pos != Capacity);
119 return MVT::SimpleValueType(Pos);
121 LLVM_ATTRIBUTE_ALWAYS_INLINE
122 const_iterator(const MachineValueTypeSet *S, bool End) : Set(S) {
123 Pos = End ? Capacity : find_from_pos(0);
125 LLVM_ATTRIBUTE_ALWAYS_INLINE
126 const_iterator &operator++() {
127 assert(Pos != Capacity);
128 Pos = find_from_pos(Pos+1);
132 LLVM_ATTRIBUTE_ALWAYS_INLINE
133 bool operator==(const const_iterator &It) const {
134 return Set == It.Set && Pos == It.Pos;
136 LLVM_ATTRIBUTE_ALWAYS_INLINE
137 bool operator!=(const const_iterator &It) const {
138 return !operator==(It);
142 unsigned find_from_pos(unsigned P) const {
143 unsigned SkipWords = P / WordWidth;
144 unsigned SkipBits = P % WordWidth;
145 unsigned Count = SkipWords * WordWidth;
147 // If P is in the middle of a word, process it manually here, because
148 // the trailing bits need to be masked off to use findFirstSet.
150 WordType W = Set->Words[SkipWords];
151 W &= maskLeadingOnes<WordType>(WordWidth-SkipBits);
153 return Count + findFirstSet(W);
158 for (unsigned i = SkipWords; i != NumWords; ++i) {
159 WordType W = Set->Words[i];
161 return Count + findFirstSet(W);
167 const MachineValueTypeSet *Set;
171 LLVM_ATTRIBUTE_ALWAYS_INLINE
172 const_iterator begin() const { return const_iterator(this, false); }
173 LLVM_ATTRIBUTE_ALWAYS_INLINE
174 const_iterator end() const { return const_iterator(this, true); }
176 LLVM_ATTRIBUTE_ALWAYS_INLINE
177 bool operator==(const MachineValueTypeSet &S) const {
178 return Words == S.Words;
180 LLVM_ATTRIBUTE_ALWAYS_INLINE
181 bool operator!=(const MachineValueTypeSet &S) const {
182 return !operator==(S);
186 friend struct const_iterator;
187 std::array<WordType,NumWords> Words;
190 struct TypeSetByHwMode : public InfoByHwMode<MachineValueTypeSet> {
191 using SetType = MachineValueTypeSet;
193 TypeSetByHwMode() = default;
194 TypeSetByHwMode(const TypeSetByHwMode &VTS) = default;
195 TypeSetByHwMode(MVT::SimpleValueType VT)
196 : TypeSetByHwMode(ValueTypeByHwMode(VT)) {}
197 TypeSetByHwMode(ValueTypeByHwMode VT)
198 : TypeSetByHwMode(ArrayRef<ValueTypeByHwMode>(&VT, 1)) {}
199 TypeSetByHwMode(ArrayRef<ValueTypeByHwMode> VTList);
201 SetType &getOrCreate(unsigned Mode) {
204 return Map.insert({Mode,SetType()}).first->second;
207 bool isValueTypeByHwMode(bool AllowEmpty) const;
208 ValueTypeByHwMode getValueTypeByHwMode() const;
210 LLVM_ATTRIBUTE_ALWAYS_INLINE
211 bool isMachineValueType() const {
212 return isDefaultOnly() && Map.begin()->second.size() == 1;
215 LLVM_ATTRIBUTE_ALWAYS_INLINE
216 MVT getMachineValueType() const {
217 assert(isMachineValueType());
218 return *Map.begin()->second.begin();
221 bool isPossible() const;
223 LLVM_ATTRIBUTE_ALWAYS_INLINE
224 bool isDefaultOnly() const {
225 return Map.size() == 1 && Map.begin()->first == DefaultMode;
228 bool insert(const ValueTypeByHwMode &VVT);
229 bool constrain(const TypeSetByHwMode &VTS);
230 template <typename Predicate> bool constrain(Predicate P);
231 template <typename Predicate>
232 bool assign_if(const TypeSetByHwMode &VTS, Predicate P);
234 void writeToStream(raw_ostream &OS) const;
235 static void writeToStream(const SetType &S, raw_ostream &OS);
237 bool operator==(const TypeSetByHwMode &VTS) const;
238 bool operator!=(const TypeSetByHwMode &VTS) const { return !(*this == VTS); }
241 bool validate() const;
244 /// Intersect two sets. Return true if anything has changed.
245 bool intersect(SetType &Out, const SetType &In);
248 raw_ostream &operator<<(raw_ostream &OS, const TypeSetByHwMode &T);
251 TypeInfer(TreePattern &T) : TP(T), ForceMode(0) {}
253 bool isConcrete(const TypeSetByHwMode &VTS, bool AllowEmpty) const {
254 return VTS.isValueTypeByHwMode(AllowEmpty);
256 ValueTypeByHwMode getConcrete(const TypeSetByHwMode &VTS,
257 bool AllowEmpty) const {
258 assert(VTS.isValueTypeByHwMode(AllowEmpty));
259 return VTS.getValueTypeByHwMode();
262 /// The protocol in the following functions (Merge*, force*, Enforce*,
263 /// expand*) is to return "true" if a change has been made, "false"
266 bool MergeInTypeInfo(TypeSetByHwMode &Out, const TypeSetByHwMode &In);
267 bool MergeInTypeInfo(TypeSetByHwMode &Out, MVT::SimpleValueType InVT) {
268 return MergeInTypeInfo(Out, TypeSetByHwMode(InVT));
270 bool MergeInTypeInfo(TypeSetByHwMode &Out, ValueTypeByHwMode InVT) {
271 return MergeInTypeInfo(Out, TypeSetByHwMode(InVT));
274 /// Reduce the set \p Out to have at most one element for each mode.
275 bool forceArbitrary(TypeSetByHwMode &Out);
277 /// The following four functions ensure that upon return the set \p Out
278 /// will only contain types of the specified kind: integer, floating-point,
279 /// scalar, or vector.
280 /// If \p Out is empty, all legal types of the specified kind will be added
281 /// to it. Otherwise, all types that are not of the specified kind will be
282 /// removed from \p Out.
283 bool EnforceInteger(TypeSetByHwMode &Out);
284 bool EnforceFloatingPoint(TypeSetByHwMode &Out);
285 bool EnforceScalar(TypeSetByHwMode &Out);
286 bool EnforceVector(TypeSetByHwMode &Out);
288 /// If \p Out is empty, fill it with all legal types. Otherwise, leave it
290 bool EnforceAny(TypeSetByHwMode &Out);
291 /// Make sure that for each type in \p Small, there exists a larger type
293 bool EnforceSmallerThan(TypeSetByHwMode &Small, TypeSetByHwMode &Big);
294 /// 1. Ensure that for each type T in \p Vec, T is a vector type, and that
295 /// for each type U in \p Elem, U is a scalar type.
296 /// 2. Ensure that for each (scalar) type U in \p Elem, there exists a
297 /// (vector) type T in \p Vec, such that U is the element type of T.
298 bool EnforceVectorEltTypeIs(TypeSetByHwMode &Vec, TypeSetByHwMode &Elem);
299 bool EnforceVectorEltTypeIs(TypeSetByHwMode &Vec,
300 const ValueTypeByHwMode &VVT);
301 /// Ensure that for each type T in \p Sub, T is a vector type, and there
302 /// exists a type U in \p Vec such that U is a vector type with the same
303 /// element type as T and at least as many elements as T.
304 bool EnforceVectorSubVectorTypeIs(TypeSetByHwMode &Vec,
305 TypeSetByHwMode &Sub);
306 /// 1. Ensure that \p V has a scalar type iff \p W has a scalar type.
307 /// 2. Ensure that for each vector type T in \p V, there exists a vector
308 /// type U in \p W, such that T and U have the same number of elements.
309 /// 3. Ensure that for each vector type U in \p W, there exists a vector
310 /// type T in \p V, such that T and U have the same number of elements
312 bool EnforceSameNumElts(TypeSetByHwMode &V, TypeSetByHwMode &W);
313 /// 1. Ensure that for each type T in \p A, there exists a type U in \p B,
314 /// such that T and U have equal size in bits.
315 /// 2. Ensure that for each type U in \p B, there exists a type T in \p A
316 /// such that T and U have equal size in bits (reverse of 1).
317 bool EnforceSameSize(TypeSetByHwMode &A, TypeSetByHwMode &B);
319 /// For each overloaded type (i.e. of form *Any), replace it with the
320 /// corresponding subset of legal, specific types.
321 void expandOverloads(TypeSetByHwMode &VTS);
322 void expandOverloads(TypeSetByHwMode::SetType &Out,
323 const TypeSetByHwMode::SetType &Legal);
325 struct ValidateOnExit {
326 ValidateOnExit(TypeSetByHwMode &T, TypeInfer &TI) : Infer(TI), VTS(T) {}
330 ~ValidateOnExit() {} // Empty destructor with NDEBUG.
333 TypeSetByHwMode &VTS;
336 struct SuppressValidation {
337 SuppressValidation(TypeInfer &TI) : Infer(TI), SavedValidate(TI.Validate) {
338 Infer.Validate = false;
340 ~SuppressValidation() {
341 Infer.Validate = SavedValidate;
348 unsigned ForceMode; // Mode to use when set.
349 bool CodeGen = false; // Set during generation of matcher code.
350 bool Validate = true; // Indicate whether to validate types.
353 TypeSetByHwMode getLegalTypes();
355 /// Cached legal types.
356 bool LegalTypesCached = false;
357 TypeSetByHwMode::SetType LegalCache = {};
360 /// Set type used to track multiply used variables in patterns
361 typedef StringSet<> MultipleUseVarSet;
363 /// SDTypeConstraint - This is a discriminated union of constraints,
364 /// corresponding to the SDTypeConstraint tablegen class in Target.td.
365 struct SDTypeConstraint {
366 SDTypeConstraint(Record *R, const CodeGenHwModes &CGH);
368 unsigned OperandNo; // The operand # this constraint applies to.
370 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs,
371 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec,
372 SDTCisSubVecOfVec, SDTCVecEltisVT, SDTCisSameNumEltsAs, SDTCisSameSizeAs
375 union { // The discriminated union.
377 unsigned OtherOperandNum;
380 unsigned OtherOperandNum;
381 } SDTCisVTSmallerThanOp_Info;
383 unsigned BigOperandNum;
384 } SDTCisOpSmallerThanOp_Info;
386 unsigned OtherOperandNum;
387 } SDTCisEltOfVec_Info;
389 unsigned OtherOperandNum;
390 } SDTCisSubVecOfVec_Info;
392 unsigned OtherOperandNum;
393 } SDTCisSameNumEltsAs_Info;
395 unsigned OtherOperandNum;
396 } SDTCisSameSizeAs_Info;
399 // The VT for SDTCisVT and SDTCVecEltisVT.
400 // Must not be in the union because it has a non-trivial destructor.
401 ValueTypeByHwMode VVT;
403 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
404 /// constraint to the nodes operands. This returns true if it makes a
405 /// change, false otherwise. If a type contradiction is found, an error
407 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
408 TreePattern &TP) const;
411 /// SDNodeInfo - One of these records is created for each SDNode instance in
412 /// the target .td file. This represents the various dag nodes we will be
417 StringRef SDClassName;
421 std::vector<SDTypeConstraint> TypeConstraints;
423 // Parse the specified record.
424 SDNodeInfo(Record *R, const CodeGenHwModes &CGH);
426 unsigned getNumResults() const { return NumResults; }
428 /// getNumOperands - This is the number of operands required or -1 if
430 int getNumOperands() const { return NumOperands; }
431 Record *getRecord() const { return Def; }
432 StringRef getEnumName() const { return EnumName; }
433 StringRef getSDClassName() const { return SDClassName; }
435 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
436 return TypeConstraints;
439 /// getKnownType - If the type constraints on this node imply a fixed type
440 /// (e.g. all stores return void, etc), then return it as an
441 /// MVT::SimpleValueType. Otherwise, return MVT::Other.
442 MVT::SimpleValueType getKnownType(unsigned ResNo) const;
444 /// hasProperty - Return true if this node has the specified property.
446 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
448 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
449 /// constraints for this node to the operands of the node. This returns
450 /// true if it makes a change, false otherwise. If a type contradiction is
451 /// found, an error is flagged.
452 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const;
455 /// TreePredicateFn - This is an abstraction that represents the predicates on
456 /// a PatFrag node. This is a simple one-word wrapper around a pointer to
457 /// provide nice accessors.
458 class TreePredicateFn {
459 /// PatFragRec - This is the TreePattern for the PatFrag that we
460 /// originally came from.
461 TreePattern *PatFragRec;
463 /// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag.
464 TreePredicateFn(TreePattern *N);
467 TreePattern *getOrigPatFragRecord() const { return PatFragRec; }
469 /// isAlwaysTrue - Return true if this is a noop predicate.
470 bool isAlwaysTrue() const;
472 bool isImmediatePattern() const { return hasImmCode(); }
474 /// getImmediatePredicateCode - Return the code that evaluates this pattern if
475 /// this is an immediate predicate. It is an error to call this on a
476 /// non-immediate pattern.
477 std::string getImmediatePredicateCode() const {
478 std::string Result = getImmCode();
479 assert(!Result.empty() && "Isn't an immediate pattern!");
483 bool operator==(const TreePredicateFn &RHS) const {
484 return PatFragRec == RHS.PatFragRec;
487 bool operator!=(const TreePredicateFn &RHS) const { return !(*this == RHS); }
489 /// Return the name to use in the generated code to reference this, this is
490 /// "Predicate_foo" if from a pattern fragment "foo".
491 std::string getFnName() const;
493 /// getCodeToRunOnSDNode - Return the code for the function body that
494 /// evaluates this predicate. The argument is expected to be in "Node",
495 /// not N. This handles casting and conversion to a concrete node type as
497 std::string getCodeToRunOnSDNode() const;
499 /// Get the data type of the argument to getImmediatePredicateCode().
500 StringRef getImmType() const;
502 /// Get a string that describes the type returned by getImmType() but is
503 /// usable as part of an identifier.
504 StringRef getImmTypeIdentifier() const;
506 // Is the desired predefined predicate for a load?
508 // Is the desired predefined predicate for a store?
509 bool isStore() const;
510 // Is the desired predefined predicate for an atomic?
511 bool isAtomic() const;
513 /// Is this predicate the predefined unindexed load predicate?
514 /// Is this predicate the predefined unindexed store predicate?
515 bool isUnindexed() const;
516 /// Is this predicate the predefined non-extending load predicate?
517 bool isNonExtLoad() const;
518 /// Is this predicate the predefined any-extend load predicate?
519 bool isAnyExtLoad() const;
520 /// Is this predicate the predefined sign-extend load predicate?
521 bool isSignExtLoad() const;
522 /// Is this predicate the predefined zero-extend load predicate?
523 bool isZeroExtLoad() const;
524 /// Is this predicate the predefined non-truncating store predicate?
525 bool isNonTruncStore() const;
526 /// Is this predicate the predefined truncating store predicate?
527 bool isTruncStore() const;
529 /// Is this predicate the predefined monotonic atomic predicate?
530 bool isAtomicOrderingMonotonic() const;
531 /// Is this predicate the predefined acquire atomic predicate?
532 bool isAtomicOrderingAcquire() const;
533 /// Is this predicate the predefined release atomic predicate?
534 bool isAtomicOrderingRelease() const;
535 /// Is this predicate the predefined acquire-release atomic predicate?
536 bool isAtomicOrderingAcquireRelease() const;
537 /// Is this predicate the predefined sequentially consistent atomic predicate?
538 bool isAtomicOrderingSequentiallyConsistent() const;
540 /// Is this predicate the predefined acquire-or-stronger atomic predicate?
541 bool isAtomicOrderingAcquireOrStronger() const;
542 /// Is this predicate the predefined weaker-than-acquire atomic predicate?
543 bool isAtomicOrderingWeakerThanAcquire() const;
545 /// Is this predicate the predefined release-or-stronger atomic predicate?
546 bool isAtomicOrderingReleaseOrStronger() const;
547 /// Is this predicate the predefined weaker-than-release atomic predicate?
548 bool isAtomicOrderingWeakerThanRelease() const;
550 /// If non-null, indicates that this predicate is a predefined memory VT
551 /// predicate for a load/store and returns the ValueType record for the memory VT.
552 Record *getMemoryVT() const;
553 /// If non-null, indicates that this predicate is a predefined memory VT
554 /// predicate (checking only the scalar type) for load/store and returns the
555 /// ValueType record for the memory VT.
556 Record *getScalarMemoryVT() const;
558 // If true, indicates that GlobalISel-based C++ code was supplied.
559 bool hasGISelPredicateCode() const;
560 std::string getGISelPredicateCode() const;
563 bool hasPredCode() const;
564 bool hasImmCode() const;
565 std::string getPredCode() const;
566 std::string getImmCode() const;
567 bool immCodeUsesAPInt() const;
568 bool immCodeUsesAPFloat() const;
570 bool isPredefinedPredicateEqualTo(StringRef Field, bool Value) const;
574 class TreePatternNode {
575 /// The type of each node result. Before and during type inference, each
576 /// result may be a set of possible types. After (successful) type inference,
577 /// each is a single concrete type.
578 std::vector<TypeSetByHwMode> Types;
580 /// Operator - The Record for the operator if this is an interior node (not
584 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
588 /// Name - The name given to this node with the :$foo notation.
592 /// PredicateFns - The predicate functions to execute on this node to check
593 /// for a match. If this list is empty, no predicate is involved.
594 std::vector<TreePredicateFn> PredicateFns;
596 /// TransformFn - The transformation function to execute on this node before
597 /// it can be substituted into the resulting instruction on a pattern match.
600 std::vector<TreePatternNodePtr> Children;
603 TreePatternNode(Record *Op, std::vector<TreePatternNodePtr> Ch,
605 : Operator(Op), Val(nullptr), TransformFn(nullptr),
606 Children(std::move(Ch)) {
607 Types.resize(NumResults);
609 TreePatternNode(Init *val, unsigned NumResults) // leaf ctor
610 : Operator(nullptr), Val(val), TransformFn(nullptr) {
611 Types.resize(NumResults);
614 bool hasName() const { return !Name.empty(); }
615 const std::string &getName() const { return Name; }
616 void setName(StringRef N) { Name.assign(N.begin(), N.end()); }
618 bool isLeaf() const { return Val != nullptr; }
621 unsigned getNumTypes() const { return Types.size(); }
622 ValueTypeByHwMode getType(unsigned ResNo) const {
623 return Types[ResNo].getValueTypeByHwMode();
625 const std::vector<TypeSetByHwMode> &getExtTypes() const { return Types; }
626 const TypeSetByHwMode &getExtType(unsigned ResNo) const {
629 TypeSetByHwMode &getExtType(unsigned ResNo) { return Types[ResNo]; }
630 void setType(unsigned ResNo, const TypeSetByHwMode &T) { Types[ResNo] = T; }
631 MVT::SimpleValueType getSimpleType(unsigned ResNo) const {
632 return Types[ResNo].getMachineValueType().SimpleTy;
635 bool hasConcreteType(unsigned ResNo) const {
636 return Types[ResNo].isValueTypeByHwMode(false);
638 bool isTypeCompletelyUnknown(unsigned ResNo, TreePattern &TP) const {
639 return Types[ResNo].empty();
642 Init *getLeafValue() const { assert(isLeaf()); return Val; }
643 Record *getOperator() const { assert(!isLeaf()); return Operator; }
645 unsigned getNumChildren() const { return Children.size(); }
646 TreePatternNode *getChild(unsigned N) const { return Children[N].get(); }
647 const TreePatternNodePtr &getChildShared(unsigned N) const {
650 void setChild(unsigned i, TreePatternNodePtr N) { Children[i] = N; }
652 /// hasChild - Return true if N is any of our children.
653 bool hasChild(const TreePatternNode *N) const {
654 for (unsigned i = 0, e = Children.size(); i != e; ++i)
655 if (Children[i].get() == N)
660 bool hasProperTypeByHwMode() const;
661 bool hasPossibleType() const;
662 bool setDefaultMode(unsigned Mode);
664 bool hasAnyPredicate() const { return !PredicateFns.empty(); }
666 const std::vector<TreePredicateFn> &getPredicateFns() const {
669 void clearPredicateFns() { PredicateFns.clear(); }
670 void setPredicateFns(const std::vector<TreePredicateFn> &Fns) {
671 assert(PredicateFns.empty() && "Overwriting non-empty predicate list!");
674 void addPredicateFn(const TreePredicateFn &Fn) {
675 assert(!Fn.isAlwaysTrue() && "Empty predicate string!");
676 if (!is_contained(PredicateFns, Fn))
677 PredicateFns.push_back(Fn);
680 Record *getTransformFn() const { return TransformFn; }
681 void setTransformFn(Record *Fn) { TransformFn = Fn; }
683 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
684 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
685 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
687 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
688 /// return the ComplexPattern information, otherwise return null.
689 const ComplexPattern *
690 getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
692 /// Returns the number of MachineInstr operands that would be produced by this
693 /// node if it mapped directly to an output Instruction's
694 /// operand. ComplexPattern specifies this explicitly; MIOperandInfo gives it
695 /// for Operands; otherwise 1.
696 unsigned getNumMIResults(const CodeGenDAGPatterns &CGP) const;
698 /// NodeHasProperty - Return true if this node has the specified property.
699 bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
701 /// TreeHasProperty - Return true if any node in this tree has the specified
703 bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
705 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
706 /// marked isCommutative.
707 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
709 void print(raw_ostream &OS) const;
712 public: // Higher level manipulation routines.
714 /// clone - Return a new copy of this tree.
716 TreePatternNodePtr clone() const;
718 /// RemoveAllTypes - Recursively strip all the types of this tree.
719 void RemoveAllTypes();
721 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
722 /// the specified node. For this comparison, all of the state of the node
723 /// is considered, except for the assigned name. Nodes with differing names
724 /// that are otherwise identical are considered isomorphic.
725 bool isIsomorphicTo(const TreePatternNode *N,
726 const MultipleUseVarSet &DepVars) const;
728 /// SubstituteFormalArguments - Replace the formal arguments in this tree
729 /// with actual values specified by ArgMap.
731 SubstituteFormalArguments(std::map<std::string, TreePatternNodePtr> &ArgMap);
733 /// InlinePatternFragments - If this pattern refers to any pattern
734 /// fragments, return the set of inlined versions (this can be more than
735 /// one if a PatFrags record has multiple alternatives).
736 void InlinePatternFragments(TreePatternNodePtr T,
738 std::vector<TreePatternNodePtr> &OutAlternatives);
740 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
741 /// this node and its children in the tree. This returns true if it makes a
742 /// change, false otherwise. If a type contradiction is found, flag an error.
743 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
745 /// UpdateNodeType - Set the node type of N to VT if VT contains
746 /// information. If N already contains a conflicting type, then flag an
747 /// error. This returns true if any information was updated.
749 bool UpdateNodeType(unsigned ResNo, const TypeSetByHwMode &InTy,
751 bool UpdateNodeType(unsigned ResNo, MVT::SimpleValueType InTy,
753 bool UpdateNodeType(unsigned ResNo, ValueTypeByHwMode InTy,
756 // Update node type with types inferred from an instruction operand or result
757 // def from the ins/outs lists.
758 // Return true if the type changed.
759 bool UpdateNodeTypeFromInst(unsigned ResNo, Record *Operand, TreePattern &TP);
761 /// ContainsUnresolvedType - Return true if this tree contains any
762 /// unresolved types.
763 bool ContainsUnresolvedType(TreePattern &TP) const;
765 /// canPatternMatch - If it is impossible for this pattern to match on this
766 /// target, fill in Reason and return false. Otherwise, return true.
767 bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
770 inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
776 /// TreePattern - Represent a pattern, used for instructions, pattern
780 /// Trees - The list of pattern trees which corresponds to this pattern.
781 /// Note that PatFrag's only have a single tree.
783 std::vector<TreePatternNodePtr> Trees;
785 /// NamedNodes - This is all of the nodes that have names in the trees in this
787 StringMap<SmallVector<TreePatternNode *, 1>> NamedNodes;
789 /// TheRecord - The actual TableGen record corresponding to this pattern.
793 /// Args - This is a list of all of the arguments to this pattern (for
794 /// PatFrag patterns), which are the 'node' markers in this pattern.
795 std::vector<std::string> Args;
797 /// CDP - the top-level object coordinating this madness.
799 CodeGenDAGPatterns &CDP;
801 /// isInputPattern - True if this is an input pattern, something to match.
802 /// False if this is an output pattern, something to emit.
805 /// hasError - True if the currently processed nodes have unresolvable types
806 /// or other non-fatal errors
809 /// It's important that the usage of operands in ComplexPatterns is
810 /// consistent: each named operand can be defined by at most one
811 /// ComplexPattern. This records the ComplexPattern instance and the operand
812 /// number for each operand encountered in a ComplexPattern to aid in that
814 StringMap<std::pair<Record *, unsigned>> ComplexPatternOperands;
820 /// TreePattern constructor - Parse the specified DagInits into the
822 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
823 CodeGenDAGPatterns &ise);
824 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
825 CodeGenDAGPatterns &ise);
826 TreePattern(Record *TheRec, TreePatternNodePtr Pat, bool isInput,
827 CodeGenDAGPatterns &ise);
829 /// getTrees - Return the tree patterns which corresponds to this pattern.
831 const std::vector<TreePatternNodePtr> &getTrees() const { return Trees; }
832 unsigned getNumTrees() const { return Trees.size(); }
833 const TreePatternNodePtr &getTree(unsigned i) const { return Trees[i]; }
834 void setTree(unsigned i, TreePatternNodePtr Tree) { Trees[i] = Tree; }
835 const TreePatternNodePtr &getOnlyTree() const {
836 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
840 const StringMap<SmallVector<TreePatternNode *, 1>> &getNamedNodesMap() {
841 if (NamedNodes.empty())
846 /// getRecord - Return the actual TableGen record corresponding to this
849 Record *getRecord() const { return TheRecord; }
851 unsigned getNumArgs() const { return Args.size(); }
852 const std::string &getArgName(unsigned i) const {
853 assert(i < Args.size() && "Argument reference out of range!");
856 std::vector<std::string> &getArgList() { return Args; }
858 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
860 /// InlinePatternFragments - If this pattern refers to any pattern
861 /// fragments, inline them into place, giving us a pattern without any
862 /// PatFrags references. This may increase the number of trees in the
863 /// pattern if a PatFrags has multiple alternatives.
864 void InlinePatternFragments() {
865 std::vector<TreePatternNodePtr> Copy = Trees;
867 for (unsigned i = 0, e = Copy.size(); i != e; ++i)
868 Copy[i]->InlinePatternFragments(Copy[i], *this, Trees);
871 /// InferAllTypes - Infer/propagate as many types throughout the expression
872 /// patterns as possible. Return true if all types are inferred, false
873 /// otherwise. Bail out if a type contradiction is found.
875 const StringMap<SmallVector<TreePatternNode *, 1>> *NamedTypes = nullptr);
877 /// error - If this is the first error in the current resolution step,
878 /// print it and set the error flag. Otherwise, continue silently.
879 void error(const Twine &Msg);
880 bool hasError() const {
887 TypeInfer &getInfer() { return Infer; }
889 void print(raw_ostream &OS) const;
893 TreePatternNodePtr ParseTreePattern(Init *DI, StringRef OpName);
894 void ComputeNamedNodes();
895 void ComputeNamedNodes(TreePatternNode *N);
899 inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
900 const TypeSetByHwMode &InTy,
902 TypeSetByHwMode VTS(InTy);
903 TP.getInfer().expandOverloads(VTS);
904 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
907 inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
908 MVT::SimpleValueType InTy,
910 TypeSetByHwMode VTS(InTy);
911 TP.getInfer().expandOverloads(VTS);
912 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
915 inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
916 ValueTypeByHwMode InTy,
918 TypeSetByHwMode VTS(InTy);
919 TP.getInfer().expandOverloads(VTS);
920 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
924 /// DAGDefaultOperand - One of these is created for each OperandWithDefaultOps
925 /// that has a set ExecuteAlways / DefaultOps field.
926 struct DAGDefaultOperand {
927 std::vector<TreePatternNodePtr> DefaultOps;
930 class DAGInstruction {
931 std::vector<Record*> Results;
932 std::vector<Record*> Operands;
933 std::vector<Record*> ImpResults;
934 TreePatternNodePtr SrcPattern;
935 TreePatternNodePtr ResultPattern;
938 DAGInstruction(const std::vector<Record*> &results,
939 const std::vector<Record*> &operands,
940 const std::vector<Record*> &impresults,
941 TreePatternNodePtr srcpattern = nullptr,
942 TreePatternNodePtr resultpattern = nullptr)
943 : Results(results), Operands(operands), ImpResults(impresults),
944 SrcPattern(srcpattern), ResultPattern(resultpattern) {}
946 unsigned getNumResults() const { return Results.size(); }
947 unsigned getNumOperands() const { return Operands.size(); }
948 unsigned getNumImpResults() const { return ImpResults.size(); }
949 const std::vector<Record*>& getImpResults() const { return ImpResults; }
951 Record *getResult(unsigned RN) const {
952 assert(RN < Results.size());
956 Record *getOperand(unsigned ON) const {
957 assert(ON < Operands.size());
961 Record *getImpResult(unsigned RN) const {
962 assert(RN < ImpResults.size());
963 return ImpResults[RN];
966 TreePatternNodePtr getSrcPattern() const { return SrcPattern; }
967 TreePatternNodePtr getResultPattern() const { return ResultPattern; }
970 /// This class represents a condition that has to be satisfied for a pattern
971 /// to be tried. It is a generalization of a class "Pattern" from Target.td:
972 /// in addition to the Target.td's predicates, this class can also represent
973 /// conditions associated with HW modes. Both types will eventually become
974 /// strings containing C++ code to be executed, the difference is in how
975 /// these strings are generated.
978 Predicate(Record *R, bool C = true) : Def(R), IfCond(C), IsHwMode(false) {
979 assert(R->isSubClassOf("Predicate") &&
980 "Predicate objects should only be created for records derived"
981 "from Predicate class");
983 Predicate(StringRef FS, bool C = true) : Def(nullptr), Features(FS.str()),
984 IfCond(C), IsHwMode(true) {}
986 /// Return a string which contains the C++ condition code that will serve
987 /// as a predicate during instruction selection.
988 std::string getCondString() const {
989 // The string will excute in a subclass of SelectionDAGISel.
990 // Cast to std::string explicitly to avoid ambiguity with StringRef.
991 std::string C = IsHwMode
992 ? std::string("MF->getSubtarget().checkFeatures(\"" + Features + "\")")
993 : std::string(Def->getValueAsString("CondString"));
994 return IfCond ? C : "!("+C+')';
996 bool operator==(const Predicate &P) const {
997 return IfCond == P.IfCond && IsHwMode == P.IsHwMode && Def == P.Def;
999 bool operator<(const Predicate &P) const {
1000 if (IsHwMode != P.IsHwMode)
1001 return IsHwMode < P.IsHwMode;
1002 assert(!Def == !P.Def && "Inconsistency between Def and IsHwMode");
1003 if (IfCond != P.IfCond)
1004 return IfCond < P.IfCond;
1006 return LessRecord()(Def, P.Def);
1007 return Features < P.Features;
1009 Record *Def; ///< Predicate definition from .td file, null for
1011 std::string Features; ///< Feature string for HW mode.
1012 bool IfCond; ///< The boolean value that the condition has to
1013 ///< evaluate to for this predicate to be true.
1014 bool IsHwMode; ///< Does this predicate correspond to a HW mode?
1017 /// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
1018 /// processed to produce isel.
1019 class PatternToMatch {
1021 PatternToMatch(Record *srcrecord, std::vector<Predicate> preds,
1022 TreePatternNodePtr src, TreePatternNodePtr dst,
1023 std::vector<Record *> dstregs, int complexity,
1024 unsigned uid, unsigned setmode = 0)
1025 : SrcRecord(srcrecord), SrcPattern(src), DstPattern(dst),
1026 Predicates(std::move(preds)), Dstregs(std::move(dstregs)),
1027 AddedComplexity(complexity), ID(uid), ForceMode(setmode) {}
1029 Record *SrcRecord; // Originating Record for the pattern.
1030 TreePatternNodePtr SrcPattern; // Source pattern to match.
1031 TreePatternNodePtr DstPattern; // Resulting pattern.
1032 std::vector<Predicate> Predicates; // Top level predicate conditions
1034 std::vector<Record*> Dstregs; // Physical register defs being matched.
1035 int AddedComplexity; // Add to matching pattern complexity.
1036 unsigned ID; // Unique ID for the record.
1037 unsigned ForceMode; // Force this mode in type inference when set.
1039 Record *getSrcRecord() const { return SrcRecord; }
1040 TreePatternNode *getSrcPattern() const { return SrcPattern.get(); }
1041 TreePatternNodePtr getSrcPatternShared() const { return SrcPattern; }
1042 TreePatternNode *getDstPattern() const { return DstPattern.get(); }
1043 TreePatternNodePtr getDstPatternShared() const { return DstPattern; }
1044 const std::vector<Record*> &getDstRegs() const { return Dstregs; }
1045 int getAddedComplexity() const { return AddedComplexity; }
1046 const std::vector<Predicate> &getPredicates() const { return Predicates; }
1048 std::string getPredicateCheck() const;
1050 /// Compute the complexity metric for the input pattern. This roughly
1051 /// corresponds to the number of nodes that are covered.
1052 int getPatternComplexity(const CodeGenDAGPatterns &CGP) const;
1055 class CodeGenDAGPatterns {
1056 RecordKeeper &Records;
1057 CodeGenTarget Target;
1058 CodeGenIntrinsicTable Intrinsics;
1059 CodeGenIntrinsicTable TgtIntrinsics;
1061 std::map<Record*, SDNodeInfo, LessRecordByID> SDNodes;
1062 std::map<Record*, std::pair<Record*, std::string>, LessRecordByID>
1064 std::map<Record*, ComplexPattern, LessRecordByID> ComplexPatterns;
1065 std::map<Record *, std::unique_ptr<TreePattern>, LessRecordByID>
1067 std::map<Record*, DAGDefaultOperand, LessRecordByID> DefaultOperands;
1068 std::map<Record*, DAGInstruction, LessRecordByID> Instructions;
1070 // Specific SDNode definitions:
1071 Record *intrinsic_void_sdnode;
1072 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
1074 /// PatternsToMatch - All of the things we are matching on the DAG. The first
1075 /// value is the pattern to match, the second pattern is the result to
1077 std::vector<PatternToMatch> PatternsToMatch;
1079 TypeSetByHwMode LegalVTS;
1081 using PatternRewriterFn = std::function<void (TreePattern *)>;
1082 PatternRewriterFn PatternRewriter;
1085 CodeGenDAGPatterns(RecordKeeper &R,
1086 PatternRewriterFn PatternRewriter = nullptr);
1088 CodeGenTarget &getTargetInfo() { return Target; }
1089 const CodeGenTarget &getTargetInfo() const { return Target; }
1090 const TypeSetByHwMode &getLegalTypes() const { return LegalVTS; }
1092 Record *getSDNodeNamed(const std::string &Name) const;
1094 const SDNodeInfo &getSDNodeInfo(Record *R) const {
1095 auto F = SDNodes.find(R);
1096 assert(F != SDNodes.end() && "Unknown node!");
1100 // Node transformation lookups.
1101 typedef std::pair<Record*, std::string> NodeXForm;
1102 const NodeXForm &getSDNodeTransform(Record *R) const {
1103 auto F = SDNodeXForms.find(R);
1104 assert(F != SDNodeXForms.end() && "Invalid transform!");
1108 typedef std::map<Record*, NodeXForm, LessRecordByID>::const_iterator
1110 nx_iterator nx_begin() const { return SDNodeXForms.begin(); }
1111 nx_iterator nx_end() const { return SDNodeXForms.end(); }
1114 const ComplexPattern &getComplexPattern(Record *R) const {
1115 auto F = ComplexPatterns.find(R);
1116 assert(F != ComplexPatterns.end() && "Unknown addressing mode!");
1120 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
1121 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
1122 if (Intrinsics[i].TheDef == R) return Intrinsics[i];
1123 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
1124 if (TgtIntrinsics[i].TheDef == R) return TgtIntrinsics[i];
1125 llvm_unreachable("Unknown intrinsic!");
1128 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
1129 if (IID-1 < Intrinsics.size())
1130 return Intrinsics[IID-1];
1131 if (IID-Intrinsics.size()-1 < TgtIntrinsics.size())
1132 return TgtIntrinsics[IID-Intrinsics.size()-1];
1133 llvm_unreachable("Bad intrinsic ID!");
1136 unsigned getIntrinsicID(Record *R) const {
1137 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
1138 if (Intrinsics[i].TheDef == R) return i;
1139 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
1140 if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size();
1141 llvm_unreachable("Unknown intrinsic!");
1144 const DAGDefaultOperand &getDefaultOperand(Record *R) const {
1145 auto F = DefaultOperands.find(R);
1146 assert(F != DefaultOperands.end() &&"Isn't an analyzed default operand!");
1150 // Pattern Fragment information.
1151 TreePattern *getPatternFragment(Record *R) const {
1152 auto F = PatternFragments.find(R);
1153 assert(F != PatternFragments.end() && "Invalid pattern fragment request!");
1154 return F->second.get();
1156 TreePattern *getPatternFragmentIfRead(Record *R) const {
1157 auto F = PatternFragments.find(R);
1158 if (F == PatternFragments.end())
1160 return F->second.get();
1163 typedef std::map<Record *, std::unique_ptr<TreePattern>,
1164 LessRecordByID>::const_iterator pf_iterator;
1165 pf_iterator pf_begin() const { return PatternFragments.begin(); }
1166 pf_iterator pf_end() const { return PatternFragments.end(); }
1167 iterator_range<pf_iterator> ptfs() const { return PatternFragments; }
1169 // Patterns to match information.
1170 typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
1171 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
1172 ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
1173 iterator_range<ptm_iterator> ptms() const { return PatternsToMatch; }
1175 /// Parse the Pattern for an instruction, and insert the result in DAGInsts.
1176 typedef std::map<Record*, DAGInstruction, LessRecordByID> DAGInstMap;
1177 void parseInstructionPattern(
1178 CodeGenInstruction &CGI, ListInit *Pattern,
1179 DAGInstMap &DAGInsts);
1181 const DAGInstruction &getInstruction(Record *R) const {
1182 auto F = Instructions.find(R);
1183 assert(F != Instructions.end() && "Unknown instruction!");
1187 Record *get_intrinsic_void_sdnode() const {
1188 return intrinsic_void_sdnode;
1190 Record *get_intrinsic_w_chain_sdnode() const {
1191 return intrinsic_w_chain_sdnode;
1193 Record *get_intrinsic_wo_chain_sdnode() const {
1194 return intrinsic_wo_chain_sdnode;
1197 bool hasTargetIntrinsics() { return !TgtIntrinsics.empty(); }
1200 void ParseNodeInfo();
1201 void ParseNodeTransforms();
1202 void ParseComplexPatterns();
1203 void ParsePatternFragments(bool OutFrags = false);
1204 void ParseDefaultOperands();
1205 void ParseInstructions();
1206 void ParsePatterns();
1207 void ExpandHwModeBasedTypes();
1208 void InferInstructionFlags();
1209 void GenerateVariants();
1210 void VerifyInstructionFlags();
1212 std::vector<Predicate> makePredList(ListInit *L);
1214 void ParseOnePattern(Record *TheDef,
1215 TreePattern &Pattern, TreePattern &Result,
1216 const std::vector<Record *> &InstImpResults);
1217 void AddPatternToMatch(TreePattern *Pattern, PatternToMatch &&PTM);
1218 void FindPatternInputsAndOutputs(
1219 TreePattern &I, TreePatternNodePtr Pat,
1220 std::map<std::string, TreePatternNodePtr> &InstInputs,
1221 std::map<std::string, TreePatternNodePtr> &InstResults,
1222 std::vector<Record *> &InstImpResults);
1226 inline bool SDNodeInfo::ApplyTypeConstraints(TreePatternNode *N,
1227 TreePattern &TP) const {
1228 bool MadeChange = false;
1229 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
1230 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
1234 } // end namespace llvm