1 //===- CodeGenDAGPatterns.cpp - Read DAG patterns from .td file -----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the CodeGenDAGPatterns class, which is used to read and
10 // represent the patterns present in a .td file for instructions.
12 //===----------------------------------------------------------------------===//
14 #include "CodeGenDAGPatterns.h"
15 #include "llvm/ADT/BitVector.h"
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/ADT/MapVector.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallSet.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/TypeSize.h"
27 #include "llvm/TableGen/Error.h"
28 #include "llvm/TableGen/Record.h"
35 #define DEBUG_TYPE "dag-patterns"
37 static inline bool isIntegerOrPtr(MVT VT) {
38 return VT.isInteger() || VT == MVT::iPTR;
40 static inline bool isFloatingPoint(MVT VT) {
41 return VT.isFloatingPoint();
43 static inline bool isVector(MVT VT) {
46 static inline bool isScalar(MVT VT) {
47 return !VT.isVector();
50 template <typename Predicate>
51 static bool berase_if(MachineValueTypeSet &S, Predicate P) {
53 // It is ok to iterate over MachineValueTypeSet and remove elements from it
64 // --- TypeSetByHwMode
66 // This is a parameterized type-set class. For each mode there is a list
67 // of types that are currently possible for a given tree node. Type
68 // inference will apply to each mode separately.
70 TypeSetByHwMode::TypeSetByHwMode(ArrayRef<ValueTypeByHwMode> VTList) {
71 for (const ValueTypeByHwMode &VVT : VTList) {
73 AddrSpaces.push_back(VVT.PtrAddrSpace);
77 bool TypeSetByHwMode::isValueTypeByHwMode(bool AllowEmpty) const {
78 for (const auto &I : *this) {
79 if (I.second.size() > 1)
81 if (!AllowEmpty && I.second.empty())
87 ValueTypeByHwMode TypeSetByHwMode::getValueTypeByHwMode() const {
88 assert(isValueTypeByHwMode(true) &&
89 "The type set has multiple types for at least one HW mode");
90 ValueTypeByHwMode VVT;
91 auto ASI = AddrSpaces.begin();
93 for (const auto &I : *this) {
94 MVT T = I.second.empty() ? MVT::Other : *I.second.begin();
95 VVT.getOrCreateTypeForMode(I.first, T);
96 if (ASI != AddrSpaces.end())
97 VVT.PtrAddrSpace = *ASI++;
102 bool TypeSetByHwMode::isPossible() const {
103 for (const auto &I : *this)
104 if (!I.second.empty())
109 bool TypeSetByHwMode::insert(const ValueTypeByHwMode &VVT) {
110 bool Changed = false;
111 bool ContainsDefault = false;
114 SmallDenseSet<unsigned, 4> Modes;
115 for (const auto &P : VVT) {
116 unsigned M = P.first;
118 // Make sure there exists a set for each specific mode from VVT.
119 Changed |= getOrCreate(M).insert(P.second).second;
120 // Cache VVT's default mode.
121 if (DefaultMode == M) {
122 ContainsDefault = true;
127 // If VVT has a default mode, add the corresponding type to all
128 // modes in "this" that do not exist in VVT.
130 for (auto &I : *this)
131 if (!Modes.count(I.first))
132 Changed |= I.second.insert(DT).second;
137 // Constrain the type set to be the intersection with VTS.
138 bool TypeSetByHwMode::constrain(const TypeSetByHwMode &VTS) {
139 bool Changed = false;
141 for (const auto &I : VTS) {
142 unsigned M = I.first;
143 if (M == DefaultMode || hasMode(M))
145 Map.insert({M, Map.at(DefaultMode)});
150 for (auto &I : *this) {
151 unsigned M = I.first;
152 SetType &S = I.second;
153 if (VTS.hasMode(M) || VTS.hasDefault()) {
154 Changed |= intersect(I.second, VTS.get(M));
155 } else if (!S.empty()) {
163 template <typename Predicate>
164 bool TypeSetByHwMode::constrain(Predicate P) {
165 bool Changed = false;
166 for (auto &I : *this)
167 Changed |= berase_if(I.second, [&P](MVT VT) { return !P(VT); });
171 template <typename Predicate>
172 bool TypeSetByHwMode::assign_if(const TypeSetByHwMode &VTS, Predicate P) {
174 for (const auto &I : VTS) {
175 SetType &S = getOrCreate(I.first);
176 for (auto J : I.second)
183 void TypeSetByHwMode::writeToStream(raw_ostream &OS) const {
184 SmallVector<unsigned, 4> Modes;
185 Modes.reserve(Map.size());
187 for (const auto &I : *this)
188 Modes.push_back(I.first);
193 array_pod_sort(Modes.begin(), Modes.end());
196 for (unsigned M : Modes) {
197 OS << ' ' << getModeName(M) << ':';
198 writeToStream(get(M), OS);
203 void TypeSetByHwMode::writeToStream(const SetType &S, raw_ostream &OS) {
204 SmallVector<MVT, 4> Types(S.begin(), S.end());
205 array_pod_sort(Types.begin(), Types.end());
208 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
209 OS << ValueTypeByHwMode::getMVTName(Types[i]);
216 bool TypeSetByHwMode::operator==(const TypeSetByHwMode &VTS) const {
217 // The isSimple call is much quicker than hasDefault - check this first.
218 bool IsSimple = isSimple();
219 bool VTSIsSimple = VTS.isSimple();
220 if (IsSimple && VTSIsSimple)
221 return *begin() == *VTS.begin();
223 // Speedup: We have a default if the set is simple.
224 bool HaveDefault = IsSimple || hasDefault();
225 bool VTSHaveDefault = VTSIsSimple || VTS.hasDefault();
226 if (HaveDefault != VTSHaveDefault)
229 SmallDenseSet<unsigned, 4> Modes;
230 for (auto &I : *this)
231 Modes.insert(I.first);
232 for (const auto &I : VTS)
233 Modes.insert(I.first);
236 // Both sets have default mode.
237 for (unsigned M : Modes) {
238 if (get(M) != VTS.get(M))
242 // Neither set has default mode.
243 for (unsigned M : Modes) {
244 // If there is no default mode, an empty set is equivalent to not having
245 // the corresponding mode.
246 bool NoModeThis = !hasMode(M) || get(M).empty();
247 bool NoModeVTS = !VTS.hasMode(M) || VTS.get(M).empty();
248 if (NoModeThis != NoModeVTS)
251 if (get(M) != VTS.get(M))
260 raw_ostream &operator<<(raw_ostream &OS, const TypeSetByHwMode &T) {
267 void TypeSetByHwMode::dump() const {
268 dbgs() << *this << '\n';
271 bool TypeSetByHwMode::intersect(SetType &Out, const SetType &In) {
272 bool OutP = Out.count(MVT::iPTR), InP = In.count(MVT::iPTR);
273 auto Int = [&In](MVT T) -> bool { return !In.count(T); };
276 return berase_if(Out, Int);
278 // Compute the intersection of scalars separately to account for only
279 // one set containing iPTR.
280 // The itersection of iPTR with a set of integer scalar types that does not
281 // include iPTR will result in the most specific scalar type:
282 // - iPTR is more specific than any set with two elements or more
283 // - iPTR is less specific than any single integer scalar type.
285 // { iPTR } * { i32 } -> { i32 }
286 // { iPTR } * { i32 i64 } -> { iPTR }
288 // { iPTR i32 } * { i32 } -> { i32 }
289 // { iPTR i32 } * { i32 i64 } -> { i32 i64 }
290 // { iPTR i32 } * { i32 i64 i128 } -> { iPTR i32 }
292 // Compute the difference between the two sets in such a way that the
293 // iPTR is in the set that is being subtracted. This is to see if there
294 // are any extra scalars in the set without iPTR that are not in the
295 // set containing iPTR. Then the iPTR could be considered a "wildcard"
296 // matching these scalars. If there is only one such scalar, it would
297 // replace the iPTR, if there are more, the iPTR would be retained.
301 berase_if(Diff, [&In](MVT T) { return In.count(T); });
302 // Pre-remove these elements and rely only on InP/OutP to determine
303 // whether a change has been made.
304 berase_if(Out, [&Diff](MVT T) { return Diff.count(T); });
307 berase_if(Diff, [&Out](MVT T) { return Out.count(T); });
308 Out.erase(MVT::iPTR);
311 // The actual intersection.
312 bool Changed = berase_if(Out, Int);
313 unsigned NumD = Diff.size();
318 Out.insert(*Diff.begin());
319 // This is a change only if Out was the one with iPTR (which is now
323 // Multiple elements from Out are now replaced with iPTR.
324 Out.insert(MVT::iPTR);
330 bool TypeSetByHwMode::validate() const {
334 bool AllEmpty = true;
335 for (const auto &I : *this)
336 AllEmpty &= I.second.empty();
344 bool TypeInfer::MergeInTypeInfo(TypeSetByHwMode &Out,
345 const TypeSetByHwMode &In) {
346 ValidateOnExit _1(Out, *this);
348 if (In.empty() || Out == In || TP.hasError())
355 bool Changed = Out.constrain(In);
356 if (Changed && Out.empty())
357 TP.error("Type contradiction");
362 bool TypeInfer::forceArbitrary(TypeSetByHwMode &Out) {
363 ValidateOnExit _1(Out, *this);
366 assert(!Out.empty() && "cannot pick from an empty set");
368 bool Changed = false;
369 for (auto &I : Out) {
370 TypeSetByHwMode::SetType &S = I.second;
373 MVT T = *S.begin(); // Pick the first element.
381 bool TypeInfer::EnforceInteger(TypeSetByHwMode &Out) {
382 ValidateOnExit _1(Out, *this);
386 return Out.constrain(isIntegerOrPtr);
388 return Out.assign_if(getLegalTypes(), isIntegerOrPtr);
391 bool TypeInfer::EnforceFloatingPoint(TypeSetByHwMode &Out) {
392 ValidateOnExit _1(Out, *this);
396 return Out.constrain(isFloatingPoint);
398 return Out.assign_if(getLegalTypes(), isFloatingPoint);
401 bool TypeInfer::EnforceScalar(TypeSetByHwMode &Out) {
402 ValidateOnExit _1(Out, *this);
406 return Out.constrain(isScalar);
408 return Out.assign_if(getLegalTypes(), isScalar);
411 bool TypeInfer::EnforceVector(TypeSetByHwMode &Out) {
412 ValidateOnExit _1(Out, *this);
416 return Out.constrain(isVector);
418 return Out.assign_if(getLegalTypes(), isVector);
421 bool TypeInfer::EnforceAny(TypeSetByHwMode &Out) {
422 ValidateOnExit _1(Out, *this);
423 if (TP.hasError() || !Out.empty())
426 Out = getLegalTypes();
430 template <typename Iter, typename Pred, typename Less>
431 static Iter min_if(Iter B, Iter E, Pred P, Less L) {
435 for (Iter I = B; I != E; ++I) {
438 if (Min == E || L(*I, *Min))
444 template <typename Iter, typename Pred, typename Less>
445 static Iter max_if(Iter B, Iter E, Pred P, Less L) {
449 for (Iter I = B; I != E; ++I) {
452 if (Max == E || L(*Max, *I))
458 /// Make sure that for each type in Small, there exists a larger type in Big.
459 bool TypeInfer::EnforceSmallerThan(TypeSetByHwMode &Small,
460 TypeSetByHwMode &Big) {
461 ValidateOnExit _1(Small, *this), _2(Big, *this);
464 bool Changed = false;
467 Changed |= EnforceAny(Small);
469 Changed |= EnforceAny(Big);
471 assert(Small.hasDefault() && Big.hasDefault());
473 std::vector<unsigned> Modes = union_modes(Small, Big);
475 // 1. Only allow integer or floating point types and make sure that
476 // both sides are both integer or both floating point.
477 // 2. Make sure that either both sides have vector types, or neither
479 for (unsigned M : Modes) {
480 TypeSetByHwMode::SetType &S = Small.get(M);
481 TypeSetByHwMode::SetType &B = Big.get(M);
483 if (any_of(S, isIntegerOrPtr) && any_of(S, isIntegerOrPtr)) {
484 auto NotInt = [](MVT VT) { return !isIntegerOrPtr(VT); };
485 Changed |= berase_if(S, NotInt);
486 Changed |= berase_if(B, NotInt);
487 } else if (any_of(S, isFloatingPoint) && any_of(B, isFloatingPoint)) {
488 auto NotFP = [](MVT VT) { return !isFloatingPoint(VT); };
489 Changed |= berase_if(S, NotFP);
490 Changed |= berase_if(B, NotFP);
491 } else if (S.empty() || B.empty()) {
492 Changed = !S.empty() || !B.empty();
496 TP.error("Incompatible types");
500 if (none_of(S, isVector) || none_of(B, isVector)) {
501 Changed |= berase_if(S, isVector);
502 Changed |= berase_if(B, isVector);
506 auto LT = [](MVT A, MVT B) -> bool {
507 // Always treat non-scalable MVTs as smaller than scalable MVTs for the
508 // purposes of ordering.
509 auto ASize = std::make_tuple(A.isScalableVector(), A.getScalarSizeInBits(),
511 auto BSize = std::make_tuple(B.isScalableVector(), B.getScalarSizeInBits(),
513 return ASize < BSize;
515 auto SameKindLE = [](MVT A, MVT B) -> bool {
516 // This function is used when removing elements: when a vector is compared
517 // to a non-vector or a scalable vector to any non-scalable MVT, it should
518 // return false (to avoid removal).
519 if (std::make_tuple(A.isVector(), A.isScalableVector()) !=
520 std::make_tuple(B.isVector(), B.isScalableVector()))
523 return std::make_tuple(A.getScalarSizeInBits(), A.getSizeInBits()) <=
524 std::make_tuple(B.getScalarSizeInBits(), B.getSizeInBits());
527 for (unsigned M : Modes) {
528 TypeSetByHwMode::SetType &S = Small.get(M);
529 TypeSetByHwMode::SetType &B = Big.get(M);
530 // MinS = min scalar in Small, remove all scalars from Big that are
531 // smaller-or-equal than MinS.
532 auto MinS = min_if(S.begin(), S.end(), isScalar, LT);
534 Changed |= berase_if(B, std::bind(SameKindLE,
535 std::placeholders::_1, *MinS));
537 // MaxS = max scalar in Big, remove all scalars from Small that are
539 auto MaxS = max_if(B.begin(), B.end(), isScalar, LT);
541 Changed |= berase_if(S, std::bind(SameKindLE,
542 *MaxS, std::placeholders::_1));
544 // MinV = min vector in Small, remove all vectors from Big that are
545 // smaller-or-equal than MinV.
546 auto MinV = min_if(S.begin(), S.end(), isVector, LT);
548 Changed |= berase_if(B, std::bind(SameKindLE,
549 std::placeholders::_1, *MinV));
551 // MaxV = max vector in Big, remove all vectors from Small that are
553 auto MaxV = max_if(B.begin(), B.end(), isVector, LT);
555 Changed |= berase_if(S, std::bind(SameKindLE,
556 *MaxV, std::placeholders::_1));
562 /// 1. Ensure that for each type T in Vec, T is a vector type, and that
563 /// for each type U in Elem, U is a scalar type.
564 /// 2. Ensure that for each (scalar) type U in Elem, there exists a (vector)
565 /// type T in Vec, such that U is the element type of T.
566 bool TypeInfer::EnforceVectorEltTypeIs(TypeSetByHwMode &Vec,
567 TypeSetByHwMode &Elem) {
568 ValidateOnExit _1(Vec, *this), _2(Elem, *this);
571 bool Changed = false;
574 Changed |= EnforceVector(Vec);
576 Changed |= EnforceScalar(Elem);
578 for (unsigned M : union_modes(Vec, Elem)) {
579 TypeSetByHwMode::SetType &V = Vec.get(M);
580 TypeSetByHwMode::SetType &E = Elem.get(M);
582 Changed |= berase_if(V, isScalar); // Scalar = !vector
583 Changed |= berase_if(E, isVector); // Vector = !scalar
584 assert(!V.empty() && !E.empty());
586 SmallSet<MVT,4> VT, ST;
587 // Collect element types from the "vector" set.
589 VT.insert(T.getVectorElementType());
590 // Collect scalar types from the "element" set.
594 // Remove from V all (vector) types whose element type is not in S.
595 Changed |= berase_if(V, [&ST](MVT T) -> bool {
596 return !ST.count(T.getVectorElementType());
598 // Remove from E all (scalar) types, for which there is no corresponding
600 Changed |= berase_if(E, [&VT](MVT T) -> bool { return !VT.count(T); });
606 bool TypeInfer::EnforceVectorEltTypeIs(TypeSetByHwMode &Vec,
607 const ValueTypeByHwMode &VVT) {
608 TypeSetByHwMode Tmp(VVT);
609 ValidateOnExit _1(Vec, *this), _2(Tmp, *this);
610 return EnforceVectorEltTypeIs(Vec, Tmp);
613 /// Ensure that for each type T in Sub, T is a vector type, and there
614 /// exists a type U in Vec such that U is a vector type with the same
615 /// element type as T and at least as many elements as T.
616 bool TypeInfer::EnforceVectorSubVectorTypeIs(TypeSetByHwMode &Vec,
617 TypeSetByHwMode &Sub) {
618 ValidateOnExit _1(Vec, *this), _2(Sub, *this);
622 /// Return true if B is a suB-vector of P, i.e. P is a suPer-vector of B.
623 auto IsSubVec = [](MVT B, MVT P) -> bool {
624 if (!B.isVector() || !P.isVector())
626 // Logically a <4 x i32> is a valid subvector of <n x 4 x i32>
627 // but until there are obvious use-cases for this, keep the
629 if (B.isScalableVector() != P.isScalableVector())
631 if (B.getVectorElementType() != P.getVectorElementType())
633 return B.getVectorNumElements() < P.getVectorNumElements();
636 /// Return true if S has no element (vector type) that T is a sub-vector of,
637 /// i.e. has the same element type as T and more elements.
638 auto NoSubV = [&IsSubVec](const TypeSetByHwMode::SetType &S, MVT T) -> bool {
645 /// Return true if S has no element (vector type) that T is a super-vector
646 /// of, i.e. has the same element type as T and fewer elements.
647 auto NoSupV = [&IsSubVec](const TypeSetByHwMode::SetType &S, MVT T) -> bool {
654 bool Changed = false;
657 Changed |= EnforceVector(Vec);
659 Changed |= EnforceVector(Sub);
661 for (unsigned M : union_modes(Vec, Sub)) {
662 TypeSetByHwMode::SetType &S = Sub.get(M);
663 TypeSetByHwMode::SetType &V = Vec.get(M);
665 Changed |= berase_if(S, isScalar);
667 // Erase all types from S that are not sub-vectors of a type in V.
668 Changed |= berase_if(S, std::bind(NoSubV, V, std::placeholders::_1));
670 // Erase all types from V that are not super-vectors of a type in S.
671 Changed |= berase_if(V, std::bind(NoSupV, S, std::placeholders::_1));
677 /// 1. Ensure that V has a scalar type iff W has a scalar type.
678 /// 2. Ensure that for each vector type T in V, there exists a vector
679 /// type U in W, such that T and U have the same number of elements.
680 /// 3. Ensure that for each vector type U in W, there exists a vector
681 /// type T in V, such that T and U have the same number of elements
683 bool TypeInfer::EnforceSameNumElts(TypeSetByHwMode &V, TypeSetByHwMode &W) {
684 ValidateOnExit _1(V, *this), _2(W, *this);
688 bool Changed = false;
690 Changed |= EnforceAny(V);
692 Changed |= EnforceAny(W);
694 // An actual vector type cannot have 0 elements, so we can treat scalars
695 // as zero-length vectors. This way both vectors and scalars can be
696 // processed identically.
697 auto NoLength = [](const SmallSet<unsigned,2> &Lengths, MVT T) -> bool {
698 return !Lengths.count(T.isVector() ? T.getVectorNumElements() : 0);
701 for (unsigned M : union_modes(V, W)) {
702 TypeSetByHwMode::SetType &VS = V.get(M);
703 TypeSetByHwMode::SetType &WS = W.get(M);
705 SmallSet<unsigned,2> VN, WN;
707 VN.insert(T.isVector() ? T.getVectorNumElements() : 0);
709 WN.insert(T.isVector() ? T.getVectorNumElements() : 0);
711 Changed |= berase_if(VS, std::bind(NoLength, WN, std::placeholders::_1));
712 Changed |= berase_if(WS, std::bind(NoLength, VN, std::placeholders::_1));
717 /// 1. Ensure that for each type T in A, there exists a type U in B,
718 /// such that T and U have equal size in bits.
719 /// 2. Ensure that for each type U in B, there exists a type T in A
720 /// such that T and U have equal size in bits (reverse of 1).
721 bool TypeInfer::EnforceSameSize(TypeSetByHwMode &A, TypeSetByHwMode &B) {
722 ValidateOnExit _1(A, *this), _2(B, *this);
725 bool Changed = false;
727 Changed |= EnforceAny(A);
729 Changed |= EnforceAny(B);
731 auto NoSize = [](const SmallSet<unsigned,2> &Sizes, MVT T) -> bool {
732 return !Sizes.count(T.getSizeInBits());
735 for (unsigned M : union_modes(A, B)) {
736 TypeSetByHwMode::SetType &AS = A.get(M);
737 TypeSetByHwMode::SetType &BS = B.get(M);
738 SmallSet<unsigned,2> AN, BN;
741 AN.insert(T.getSizeInBits());
743 BN.insert(T.getSizeInBits());
745 Changed |= berase_if(AS, std::bind(NoSize, BN, std::placeholders::_1));
746 Changed |= berase_if(BS, std::bind(NoSize, AN, std::placeholders::_1));
752 void TypeInfer::expandOverloads(TypeSetByHwMode &VTS) {
753 ValidateOnExit _1(VTS, *this);
754 const TypeSetByHwMode &Legal = getLegalTypes();
755 assert(Legal.isDefaultOnly() && "Default-mode only expected");
756 const TypeSetByHwMode::SetType &LegalTypes = Legal.get(DefaultMode);
759 expandOverloads(I.second, LegalTypes);
762 void TypeInfer::expandOverloads(TypeSetByHwMode::SetType &Out,
763 const TypeSetByHwMode::SetType &Legal) {
766 if (!T.isOverloaded())
770 // MachineValueTypeSet allows iteration and erasing.
775 switch (Ov.SimpleTy) {
777 Out.insert(MVT::iPTR);
780 for (MVT T : MVT::integer_valuetypes())
783 for (MVT T : MVT::integer_fixedlen_vector_valuetypes())
786 for (MVT T : MVT::integer_scalable_vector_valuetypes())
791 for (MVT T : MVT::fp_valuetypes())
794 for (MVT T : MVT::fp_fixedlen_vector_valuetypes())
797 for (MVT T : MVT::fp_scalable_vector_valuetypes())
802 for (MVT T : MVT::vector_valuetypes())
807 for (MVT T : MVT::all_valuetypes())
817 const TypeSetByHwMode &TypeInfer::getLegalTypes() {
818 if (!LegalTypesCached) {
819 TypeSetByHwMode::SetType &LegalTypes = LegalCache.getOrCreate(DefaultMode);
820 // Stuff all types from all modes into the default mode.
821 const TypeSetByHwMode <S = TP.getDAGPatterns().getLegalTypes();
822 for (const auto &I : LTS)
823 LegalTypes.insert(I.second);
824 LegalTypesCached = true;
826 assert(LegalCache.isDefaultOnly() && "Default-mode only expected");
831 TypeInfer::ValidateOnExit::~ValidateOnExit() {
832 if (Infer.Validate && !VTS.validate()) {
833 dbgs() << "Type set is empty for each HW mode:\n"
834 "possible type contradiction in the pattern below "
835 "(use -print-records with llvm-tblgen to see all "
836 "expanded records).\n";
838 llvm_unreachable(nullptr);
844 //===----------------------------------------------------------------------===//
845 // ScopedName Implementation
846 //===----------------------------------------------------------------------===//
848 bool ScopedName::operator==(const ScopedName &o) const {
849 return Scope == o.Scope && Identifier == o.Identifier;
852 bool ScopedName::operator!=(const ScopedName &o) const {
853 return !(*this == o);
857 //===----------------------------------------------------------------------===//
858 // TreePredicateFn Implementation
859 //===----------------------------------------------------------------------===//
861 /// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag.
862 TreePredicateFn::TreePredicateFn(TreePattern *N) : PatFragRec(N) {
864 (!hasPredCode() || !hasImmCode()) &&
865 ".td file corrupt: can't have a node predicate *and* an imm predicate");
868 bool TreePredicateFn::hasPredCode() const {
869 return isLoad() || isStore() || isAtomic() ||
870 !PatFragRec->getRecord()->getValueAsString("PredicateCode").empty();
873 std::string TreePredicateFn::getPredCode() const {
874 std::string Code = "";
876 if (!isLoad() && !isStore() && !isAtomic()) {
877 Record *MemoryVT = getMemoryVT();
880 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
881 "MemoryVT requires IsLoad or IsStore");
884 if (!isLoad() && !isStore()) {
886 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
887 "IsUnindexed requires IsLoad or IsStore");
889 Record *ScalarMemoryVT = getScalarMemoryVT();
892 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
893 "ScalarMemoryVT requires IsLoad or IsStore");
896 if (isLoad() + isStore() + isAtomic() > 1)
897 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
898 "IsLoad, IsStore, and IsAtomic are mutually exclusive");
901 if (!isUnindexed() && !isNonExtLoad() && !isAnyExtLoad() &&
902 !isSignExtLoad() && !isZeroExtLoad() && getMemoryVT() == nullptr &&
903 getScalarMemoryVT() == nullptr && getAddressSpaces() == nullptr &&
904 getMinAlignment() < 1)
905 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
906 "IsLoad cannot be used by itself");
909 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
910 "IsNonExtLoad requires IsLoad");
912 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
913 "IsAnyExtLoad requires IsLoad");
915 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
916 "IsSignExtLoad requires IsLoad");
918 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
919 "IsZeroExtLoad requires IsLoad");
923 if (!isUnindexed() && !isTruncStore() && !isNonTruncStore() &&
924 getMemoryVT() == nullptr && getScalarMemoryVT() == nullptr &&
925 getAddressSpaces() == nullptr && getMinAlignment() < 1)
926 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
927 "IsStore cannot be used by itself");
929 if (isNonTruncStore())
930 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
931 "IsNonTruncStore requires IsStore");
933 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
934 "IsTruncStore requires IsStore");
938 if (getMemoryVT() == nullptr && !isAtomicOrderingMonotonic() &&
939 getAddressSpaces() == nullptr &&
940 !isAtomicOrderingAcquire() && !isAtomicOrderingRelease() &&
941 !isAtomicOrderingAcquireRelease() &&
942 !isAtomicOrderingSequentiallyConsistent() &&
943 !isAtomicOrderingAcquireOrStronger() &&
944 !isAtomicOrderingReleaseOrStronger() &&
945 !isAtomicOrderingWeakerThanAcquire() &&
946 !isAtomicOrderingWeakerThanRelease())
947 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
948 "IsAtomic cannot be used by itself");
950 if (isAtomicOrderingMonotonic())
951 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
952 "IsAtomicOrderingMonotonic requires IsAtomic");
953 if (isAtomicOrderingAcquire())
954 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
955 "IsAtomicOrderingAcquire requires IsAtomic");
956 if (isAtomicOrderingRelease())
957 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
958 "IsAtomicOrderingRelease requires IsAtomic");
959 if (isAtomicOrderingAcquireRelease())
960 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
961 "IsAtomicOrderingAcquireRelease requires IsAtomic");
962 if (isAtomicOrderingSequentiallyConsistent())
963 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
964 "IsAtomicOrderingSequentiallyConsistent requires IsAtomic");
965 if (isAtomicOrderingAcquireOrStronger())
966 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
967 "IsAtomicOrderingAcquireOrStronger requires IsAtomic");
968 if (isAtomicOrderingReleaseOrStronger())
969 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
970 "IsAtomicOrderingReleaseOrStronger requires IsAtomic");
971 if (isAtomicOrderingWeakerThanAcquire())
972 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
973 "IsAtomicOrderingWeakerThanAcquire requires IsAtomic");
976 if (isLoad() || isStore() || isAtomic()) {
977 if (ListInit *AddressSpaces = getAddressSpaces()) {
978 Code += "unsigned AddrSpace = cast<MemSDNode>(N)->getAddressSpace();\n"
982 for (Init *Val : AddressSpaces->getValues()) {
988 IntInit *IntVal = dyn_cast<IntInit>(Val);
990 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
991 "AddressSpaces element must be integer");
994 Code += "AddrSpace != " + utostr(IntVal->getValue());
997 Code += ")\nreturn false;\n";
1000 int64_t MinAlign = getMinAlignment();
1002 Code += "if (cast<MemSDNode>(N)->getAlignment() < ";
1003 Code += utostr(MinAlign);
1004 Code += ")\nreturn false;\n";
1007 Record *MemoryVT = getMemoryVT();
1010 Code += ("if (cast<MemSDNode>(N)->getMemoryVT() != MVT::" +
1011 MemoryVT->getName() + ") return false;\n")
1015 if (isAtomic() && isAtomicOrderingMonotonic())
1016 Code += "if (cast<AtomicSDNode>(N)->getOrdering() != "
1017 "AtomicOrdering::Monotonic) return false;\n";
1018 if (isAtomic() && isAtomicOrderingAcquire())
1019 Code += "if (cast<AtomicSDNode>(N)->getOrdering() != "
1020 "AtomicOrdering::Acquire) return false;\n";
1021 if (isAtomic() && isAtomicOrderingRelease())
1022 Code += "if (cast<AtomicSDNode>(N)->getOrdering() != "
1023 "AtomicOrdering::Release) return false;\n";
1024 if (isAtomic() && isAtomicOrderingAcquireRelease())
1025 Code += "if (cast<AtomicSDNode>(N)->getOrdering() != "
1026 "AtomicOrdering::AcquireRelease) return false;\n";
1027 if (isAtomic() && isAtomicOrderingSequentiallyConsistent())
1028 Code += "if (cast<AtomicSDNode>(N)->getOrdering() != "
1029 "AtomicOrdering::SequentiallyConsistent) return false;\n";
1031 if (isAtomic() && isAtomicOrderingAcquireOrStronger())
1032 Code += "if (!isAcquireOrStronger(cast<AtomicSDNode>(N)->getOrdering())) "
1034 if (isAtomic() && isAtomicOrderingWeakerThanAcquire())
1035 Code += "if (isAcquireOrStronger(cast<AtomicSDNode>(N)->getOrdering())) "
1038 if (isAtomic() && isAtomicOrderingReleaseOrStronger())
1039 Code += "if (!isReleaseOrStronger(cast<AtomicSDNode>(N)->getOrdering())) "
1041 if (isAtomic() && isAtomicOrderingWeakerThanRelease())
1042 Code += "if (isReleaseOrStronger(cast<AtomicSDNode>(N)->getOrdering())) "
1045 if (isLoad() || isStore()) {
1046 StringRef SDNodeName = isLoad() ? "LoadSDNode" : "StoreSDNode";
1049 Code += ("if (cast<" + SDNodeName +
1050 ">(N)->getAddressingMode() != ISD::UNINDEXED) "
1055 if ((isNonExtLoad() + isAnyExtLoad() + isSignExtLoad() +
1056 isZeroExtLoad()) > 1)
1057 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
1058 "IsNonExtLoad, IsAnyExtLoad, IsSignExtLoad, and "
1059 "IsZeroExtLoad are mutually exclusive");
1061 Code += "if (cast<LoadSDNode>(N)->getExtensionType() != "
1062 "ISD::NON_EXTLOAD) return false;\n";
1064 Code += "if (cast<LoadSDNode>(N)->getExtensionType() != ISD::EXTLOAD) "
1066 if (isSignExtLoad())
1067 Code += "if (cast<LoadSDNode>(N)->getExtensionType() != ISD::SEXTLOAD) "
1069 if (isZeroExtLoad())
1070 Code += "if (cast<LoadSDNode>(N)->getExtensionType() != ISD::ZEXTLOAD) "
1073 if ((isNonTruncStore() + isTruncStore()) > 1)
1075 getOrigPatFragRecord()->getRecord()->getLoc(),
1076 "IsNonTruncStore, and IsTruncStore are mutually exclusive");
1077 if (isNonTruncStore())
1079 " if (cast<StoreSDNode>(N)->isTruncatingStore()) return false;\n";
1082 " if (!cast<StoreSDNode>(N)->isTruncatingStore()) return false;\n";
1085 Record *ScalarMemoryVT = getScalarMemoryVT();
1088 Code += ("if (cast<" + SDNodeName +
1089 ">(N)->getMemoryVT().getScalarType() != MVT::" +
1090 ScalarMemoryVT->getName() + ") return false;\n")
1094 std::string PredicateCode = PatFragRec->getRecord()->getValueAsString("PredicateCode");
1096 Code += PredicateCode;
1098 if (PredicateCode.empty() && !Code.empty())
1099 Code += "return true;\n";
1104 bool TreePredicateFn::hasImmCode() const {
1105 return !PatFragRec->getRecord()->getValueAsString("ImmediateCode").empty();
1108 std::string TreePredicateFn::getImmCode() const {
1109 return PatFragRec->getRecord()->getValueAsString("ImmediateCode");
1112 bool TreePredicateFn::immCodeUsesAPInt() const {
1113 return getOrigPatFragRecord()->getRecord()->getValueAsBit("IsAPInt");
1116 bool TreePredicateFn::immCodeUsesAPFloat() const {
1118 // The return value will be false when IsAPFloat is unset.
1119 return getOrigPatFragRecord()->getRecord()->getValueAsBitOrUnset("IsAPFloat",
1123 bool TreePredicateFn::isPredefinedPredicateEqualTo(StringRef Field,
1127 getOrigPatFragRecord()->getRecord()->getValueAsBitOrUnset(Field, Unset);
1130 return Result == Value;
1132 bool TreePredicateFn::usesOperands() const {
1133 return isPredefinedPredicateEqualTo("PredicateCodeUsesOperands", true);
1135 bool TreePredicateFn::isLoad() const {
1136 return isPredefinedPredicateEqualTo("IsLoad", true);
1138 bool TreePredicateFn::isStore() const {
1139 return isPredefinedPredicateEqualTo("IsStore", true);
1141 bool TreePredicateFn::isAtomic() const {
1142 return isPredefinedPredicateEqualTo("IsAtomic", true);
1144 bool TreePredicateFn::isUnindexed() const {
1145 return isPredefinedPredicateEqualTo("IsUnindexed", true);
1147 bool TreePredicateFn::isNonExtLoad() const {
1148 return isPredefinedPredicateEqualTo("IsNonExtLoad", true);
1150 bool TreePredicateFn::isAnyExtLoad() const {
1151 return isPredefinedPredicateEqualTo("IsAnyExtLoad", true);
1153 bool TreePredicateFn::isSignExtLoad() const {
1154 return isPredefinedPredicateEqualTo("IsSignExtLoad", true);
1156 bool TreePredicateFn::isZeroExtLoad() const {
1157 return isPredefinedPredicateEqualTo("IsZeroExtLoad", true);
1159 bool TreePredicateFn::isNonTruncStore() const {
1160 return isPredefinedPredicateEqualTo("IsTruncStore", false);
1162 bool TreePredicateFn::isTruncStore() const {
1163 return isPredefinedPredicateEqualTo("IsTruncStore", true);
1165 bool TreePredicateFn::isAtomicOrderingMonotonic() const {
1166 return isPredefinedPredicateEqualTo("IsAtomicOrderingMonotonic", true);
1168 bool TreePredicateFn::isAtomicOrderingAcquire() const {
1169 return isPredefinedPredicateEqualTo("IsAtomicOrderingAcquire", true);
1171 bool TreePredicateFn::isAtomicOrderingRelease() const {
1172 return isPredefinedPredicateEqualTo("IsAtomicOrderingRelease", true);
1174 bool TreePredicateFn::isAtomicOrderingAcquireRelease() const {
1175 return isPredefinedPredicateEqualTo("IsAtomicOrderingAcquireRelease", true);
1177 bool TreePredicateFn::isAtomicOrderingSequentiallyConsistent() const {
1178 return isPredefinedPredicateEqualTo("IsAtomicOrderingSequentiallyConsistent",
1181 bool TreePredicateFn::isAtomicOrderingAcquireOrStronger() const {
1182 return isPredefinedPredicateEqualTo("IsAtomicOrderingAcquireOrStronger", true);
1184 bool TreePredicateFn::isAtomicOrderingWeakerThanAcquire() const {
1185 return isPredefinedPredicateEqualTo("IsAtomicOrderingAcquireOrStronger", false);
1187 bool TreePredicateFn::isAtomicOrderingReleaseOrStronger() const {
1188 return isPredefinedPredicateEqualTo("IsAtomicOrderingReleaseOrStronger", true);
1190 bool TreePredicateFn::isAtomicOrderingWeakerThanRelease() const {
1191 return isPredefinedPredicateEqualTo("IsAtomicOrderingReleaseOrStronger", false);
1193 Record *TreePredicateFn::getMemoryVT() const {
1194 Record *R = getOrigPatFragRecord()->getRecord();
1195 if (R->isValueUnset("MemoryVT"))
1197 return R->getValueAsDef("MemoryVT");
1200 ListInit *TreePredicateFn::getAddressSpaces() const {
1201 Record *R = getOrigPatFragRecord()->getRecord();
1202 if (R->isValueUnset("AddressSpaces"))
1204 return R->getValueAsListInit("AddressSpaces");
1207 int64_t TreePredicateFn::getMinAlignment() const {
1208 Record *R = getOrigPatFragRecord()->getRecord();
1209 if (R->isValueUnset("MinAlignment"))
1211 return R->getValueAsInt("MinAlignment");
1214 Record *TreePredicateFn::getScalarMemoryVT() const {
1215 Record *R = getOrigPatFragRecord()->getRecord();
1216 if (R->isValueUnset("ScalarMemoryVT"))
1218 return R->getValueAsDef("ScalarMemoryVT");
1220 bool TreePredicateFn::hasGISelPredicateCode() const {
1221 return !PatFragRec->getRecord()
1222 ->getValueAsString("GISelPredicateCode")
1225 std::string TreePredicateFn::getGISelPredicateCode() const {
1226 return PatFragRec->getRecord()->getValueAsString("GISelPredicateCode");
1229 StringRef TreePredicateFn::getImmType() const {
1230 if (immCodeUsesAPInt())
1231 return "const APInt &";
1232 if (immCodeUsesAPFloat())
1233 return "const APFloat &";
1237 StringRef TreePredicateFn::getImmTypeIdentifier() const {
1238 if (immCodeUsesAPInt())
1240 else if (immCodeUsesAPFloat())
1245 /// isAlwaysTrue - Return true if this is a noop predicate.
1246 bool TreePredicateFn::isAlwaysTrue() const {
1247 return !hasPredCode() && !hasImmCode();
1250 /// Return the name to use in the generated code to reference this, this is
1251 /// "Predicate_foo" if from a pattern fragment "foo".
1252 std::string TreePredicateFn::getFnName() const {
1253 return "Predicate_" + PatFragRec->getRecord()->getName().str();
1256 /// getCodeToRunOnSDNode - Return the code for the function body that
1257 /// evaluates this predicate. The argument is expected to be in "Node",
1258 /// not N. This handles casting and conversion to a concrete node type as
1260 std::string TreePredicateFn::getCodeToRunOnSDNode() const {
1261 // Handle immediate predicates first.
1262 std::string ImmCode = getImmCode();
1263 if (!ImmCode.empty()) {
1265 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
1266 "IsLoad cannot be used with ImmLeaf or its subclasses");
1268 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
1269 "IsStore cannot be used with ImmLeaf or its subclasses");
1272 getOrigPatFragRecord()->getRecord()->getLoc(),
1273 "IsUnindexed cannot be used with ImmLeaf or its subclasses");
1276 getOrigPatFragRecord()->getRecord()->getLoc(),
1277 "IsNonExtLoad cannot be used with ImmLeaf or its subclasses");
1280 getOrigPatFragRecord()->getRecord()->getLoc(),
1281 "IsAnyExtLoad cannot be used with ImmLeaf or its subclasses");
1282 if (isSignExtLoad())
1284 getOrigPatFragRecord()->getRecord()->getLoc(),
1285 "IsSignExtLoad cannot be used with ImmLeaf or its subclasses");
1286 if (isZeroExtLoad())
1288 getOrigPatFragRecord()->getRecord()->getLoc(),
1289 "IsZeroExtLoad cannot be used with ImmLeaf or its subclasses");
1290 if (isNonTruncStore())
1292 getOrigPatFragRecord()->getRecord()->getLoc(),
1293 "IsNonTruncStore cannot be used with ImmLeaf or its subclasses");
1296 getOrigPatFragRecord()->getRecord()->getLoc(),
1297 "IsTruncStore cannot be used with ImmLeaf or its subclasses");
1299 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
1300 "MemoryVT cannot be used with ImmLeaf or its subclasses");
1301 if (getScalarMemoryVT())
1303 getOrigPatFragRecord()->getRecord()->getLoc(),
1304 "ScalarMemoryVT cannot be used with ImmLeaf or its subclasses");
1306 std::string Result = (" " + getImmType() + " Imm = ").str();
1307 if (immCodeUsesAPFloat())
1308 Result += "cast<ConstantFPSDNode>(Node)->getValueAPF();\n";
1309 else if (immCodeUsesAPInt())
1310 Result += "cast<ConstantSDNode>(Node)->getAPIntValue();\n";
1312 Result += "cast<ConstantSDNode>(Node)->getSExtValue();\n";
1313 return Result + ImmCode;
1316 // Handle arbitrary node predicates.
1317 assert(hasPredCode() && "Don't have any predicate code!");
1319 // If this is using PatFrags, there are multiple trees to search. They should
1320 // all have the same class. FIXME: Is there a way to find a common
1322 StringRef ClassName;
1323 for (const auto &Tree : PatFragRec->getTrees()) {
1324 StringRef TreeClassName;
1326 TreeClassName = "SDNode";
1328 Record *Op = Tree->getOperator();
1329 const SDNodeInfo &Info = PatFragRec->getDAGPatterns().getSDNodeInfo(Op);
1330 TreeClassName = Info.getSDClassName();
1333 if (ClassName.empty())
1334 ClassName = TreeClassName;
1335 else if (ClassName != TreeClassName) {
1336 PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
1337 "PatFrags trees do not have consistent class");
1342 if (ClassName == "SDNode")
1343 Result = " SDNode *N = Node;\n";
1345 Result = " auto *N = cast<" + ClassName.str() + ">(Node);\n";
1347 return (Twine(Result) + " (void)N;\n" + getPredCode()).str();
1350 //===----------------------------------------------------------------------===//
1351 // PatternToMatch implementation
1354 static bool isImmAllOnesAllZerosMatch(const TreePatternNode *P) {
1357 DefInit *DI = dyn_cast<DefInit>(P->getLeafValue());
1361 Record *R = DI->getDef();
1362 return R->getName() == "immAllOnesV" || R->getName() == "immAllZerosV";
1365 /// getPatternSize - Return the 'size' of this pattern. We want to match large
1366 /// patterns before small ones. This is used to determine the size of a
1368 static unsigned getPatternSize(const TreePatternNode *P,
1369 const CodeGenDAGPatterns &CGP) {
1370 unsigned Size = 3; // The node itself.
1371 // If the root node is a ConstantSDNode, increases its size.
1372 // e.g. (set R32:$dst, 0).
1373 if (P->isLeaf() && isa<IntInit>(P->getLeafValue()))
1376 if (const ComplexPattern *AM = P->getComplexPatternInfo(CGP)) {
1377 Size += AM->getComplexity();
1378 // We don't want to count any children twice, so return early.
1382 // If this node has some predicate function that must match, it adds to the
1383 // complexity of this node.
1384 if (!P->getPredicateCalls().empty())
1387 // Count children in the count if they are also nodes.
1388 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) {
1389 const TreePatternNode *Child = P->getChild(i);
1390 if (!Child->isLeaf() && Child->getNumTypes()) {
1391 const TypeSetByHwMode &T0 = Child->getExtType(0);
1392 // At this point, all variable type sets should be simple, i.e. only
1393 // have a default mode.
1394 if (T0.getMachineValueType() != MVT::Other) {
1395 Size += getPatternSize(Child, CGP);
1399 if (Child->isLeaf()) {
1400 if (isa<IntInit>(Child->getLeafValue()))
1401 Size += 5; // Matches a ConstantSDNode (+3) and a specific value (+2).
1402 else if (Child->getComplexPatternInfo(CGP))
1403 Size += getPatternSize(Child, CGP);
1404 else if (isImmAllOnesAllZerosMatch(Child))
1405 Size += 4; // Matches a build_vector(+3) and a predicate (+1).
1406 else if (!Child->getPredicateCalls().empty())
1414 /// Compute the complexity metric for the input pattern. This roughly
1415 /// corresponds to the number of nodes that are covered.
1416 int PatternToMatch::
1417 getPatternComplexity(const CodeGenDAGPatterns &CGP) const {
1418 return getPatternSize(getSrcPattern(), CGP) + getAddedComplexity();
1421 /// getPredicateCheck - Return a single string containing all of this
1422 /// pattern's predicates concatenated with "&&" operators.
1424 std::string PatternToMatch::getPredicateCheck() const {
1425 SmallVector<const Predicate*,4> PredList;
1426 for (const Predicate &P : Predicates) {
1427 if (!P.getCondString().empty())
1428 PredList.push_back(&P);
1430 llvm::sort(PredList, deref<std::less<>>());
1433 for (unsigned i = 0, e = PredList.size(); i != e; ++i) {
1436 Check += '(' + PredList[i]->getCondString() + ')';
1441 //===----------------------------------------------------------------------===//
1442 // SDTypeConstraint implementation
1445 SDTypeConstraint::SDTypeConstraint(Record *R, const CodeGenHwModes &CGH) {
1446 OperandNo = R->getValueAsInt("OperandNum");
1448 if (R->isSubClassOf("SDTCisVT")) {
1449 ConstraintType = SDTCisVT;
1450 VVT = getValueTypeByHwMode(R->getValueAsDef("VT"), CGH);
1451 for (const auto &P : VVT)
1452 if (P.second == MVT::isVoid)
1453 PrintFatalError(R->getLoc(), "Cannot use 'Void' as type to SDTCisVT");
1454 } else if (R->isSubClassOf("SDTCisPtrTy")) {
1455 ConstraintType = SDTCisPtrTy;
1456 } else if (R->isSubClassOf("SDTCisInt")) {
1457 ConstraintType = SDTCisInt;
1458 } else if (R->isSubClassOf("SDTCisFP")) {
1459 ConstraintType = SDTCisFP;
1460 } else if (R->isSubClassOf("SDTCisVec")) {
1461 ConstraintType = SDTCisVec;
1462 } else if (R->isSubClassOf("SDTCisSameAs")) {
1463 ConstraintType = SDTCisSameAs;
1464 x.SDTCisSameAs_Info.OtherOperandNum = R->getValueAsInt("OtherOperandNum");
1465 } else if (R->isSubClassOf("SDTCisVTSmallerThanOp")) {
1466 ConstraintType = SDTCisVTSmallerThanOp;
1467 x.SDTCisVTSmallerThanOp_Info.OtherOperandNum =
1468 R->getValueAsInt("OtherOperandNum");
1469 } else if (R->isSubClassOf("SDTCisOpSmallerThanOp")) {
1470 ConstraintType = SDTCisOpSmallerThanOp;
1471 x.SDTCisOpSmallerThanOp_Info.BigOperandNum =
1472 R->getValueAsInt("BigOperandNum");
1473 } else if (R->isSubClassOf("SDTCisEltOfVec")) {
1474 ConstraintType = SDTCisEltOfVec;
1475 x.SDTCisEltOfVec_Info.OtherOperandNum = R->getValueAsInt("OtherOpNum");
1476 } else if (R->isSubClassOf("SDTCisSubVecOfVec")) {
1477 ConstraintType = SDTCisSubVecOfVec;
1478 x.SDTCisSubVecOfVec_Info.OtherOperandNum =
1479 R->getValueAsInt("OtherOpNum");
1480 } else if (R->isSubClassOf("SDTCVecEltisVT")) {
1481 ConstraintType = SDTCVecEltisVT;
1482 VVT = getValueTypeByHwMode(R->getValueAsDef("VT"), CGH);
1483 for (const auto &P : VVT) {
1486 PrintFatalError(R->getLoc(),
1487 "Cannot use vector type as SDTCVecEltisVT");
1488 if (!T.isInteger() && !T.isFloatingPoint())
1489 PrintFatalError(R->getLoc(), "Must use integer or floating point type "
1490 "as SDTCVecEltisVT");
1492 } else if (R->isSubClassOf("SDTCisSameNumEltsAs")) {
1493 ConstraintType = SDTCisSameNumEltsAs;
1494 x.SDTCisSameNumEltsAs_Info.OtherOperandNum =
1495 R->getValueAsInt("OtherOperandNum");
1496 } else if (R->isSubClassOf("SDTCisSameSizeAs")) {
1497 ConstraintType = SDTCisSameSizeAs;
1498 x.SDTCisSameSizeAs_Info.OtherOperandNum =
1499 R->getValueAsInt("OtherOperandNum");
1501 PrintFatalError(R->getLoc(),
1502 "Unrecognized SDTypeConstraint '" + R->getName() + "'!\n");
1506 /// getOperandNum - Return the node corresponding to operand #OpNo in tree
1507 /// N, and the result number in ResNo.
1508 static TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N,
1509 const SDNodeInfo &NodeInfo,
1511 unsigned NumResults = NodeInfo.getNumResults();
1512 if (OpNo < NumResults) {
1519 if (OpNo >= N->getNumChildren()) {
1521 raw_string_ostream OS(S);
1522 OS << "Invalid operand number in type constraint "
1523 << (OpNo+NumResults) << " ";
1525 PrintFatalError(OS.str());
1528 return N->getChild(OpNo);
1531 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
1532 /// constraint to the nodes operands. This returns true if it makes a
1533 /// change, false otherwise. If a type contradiction is found, flag an error.
1534 bool SDTypeConstraint::ApplyTypeConstraint(TreePatternNode *N,
1535 const SDNodeInfo &NodeInfo,
1536 TreePattern &TP) const {
1540 unsigned ResNo = 0; // The result number being referenced.
1541 TreePatternNode *NodeToApply = getOperandNum(OperandNo, N, NodeInfo, ResNo);
1542 TypeInfer &TI = TP.getInfer();
1544 switch (ConstraintType) {
1546 // Operand must be a particular type.
1547 return NodeToApply->UpdateNodeType(ResNo, VVT, TP);
1549 // Operand must be same as target pointer type.
1550 return NodeToApply->UpdateNodeType(ResNo, MVT::iPTR, TP);
1552 // Require it to be one of the legal integer VTs.
1553 return TI.EnforceInteger(NodeToApply->getExtType(ResNo));
1555 // Require it to be one of the legal fp VTs.
1556 return TI.EnforceFloatingPoint(NodeToApply->getExtType(ResNo));
1558 // Require it to be one of the legal vector VTs.
1559 return TI.EnforceVector(NodeToApply->getExtType(ResNo));
1560 case SDTCisSameAs: {
1561 unsigned OResNo = 0;
1562 TreePatternNode *OtherNode =
1563 getOperandNum(x.SDTCisSameAs_Info.OtherOperandNum, N, NodeInfo, OResNo);
1564 return NodeToApply->UpdateNodeType(ResNo, OtherNode->getExtType(OResNo),TP)|
1565 OtherNode->UpdateNodeType(OResNo,NodeToApply->getExtType(ResNo),TP);
1567 case SDTCisVTSmallerThanOp: {
1568 // The NodeToApply must be a leaf node that is a VT. OtherOperandNum must
1569 // have an integer type that is smaller than the VT.
1570 if (!NodeToApply->isLeaf() ||
1571 !isa<DefInit>(NodeToApply->getLeafValue()) ||
1572 !static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef()
1573 ->isSubClassOf("ValueType")) {
1574 TP.error(N->getOperator()->getName() + " expects a VT operand!");
1577 DefInit *DI = static_cast<DefInit*>(NodeToApply->getLeafValue());
1578 const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
1579 auto VVT = getValueTypeByHwMode(DI->getDef(), T.getHwModes());
1580 TypeSetByHwMode TypeListTmp(VVT);
1582 unsigned OResNo = 0;
1583 TreePatternNode *OtherNode =
1584 getOperandNum(x.SDTCisVTSmallerThanOp_Info.OtherOperandNum, N, NodeInfo,
1587 return TI.EnforceSmallerThan(TypeListTmp, OtherNode->getExtType(OResNo));
1589 case SDTCisOpSmallerThanOp: {
1590 unsigned BResNo = 0;
1591 TreePatternNode *BigOperand =
1592 getOperandNum(x.SDTCisOpSmallerThanOp_Info.BigOperandNum, N, NodeInfo,
1594 return TI.EnforceSmallerThan(NodeToApply->getExtType(ResNo),
1595 BigOperand->getExtType(BResNo));
1597 case SDTCisEltOfVec: {
1598 unsigned VResNo = 0;
1599 TreePatternNode *VecOperand =
1600 getOperandNum(x.SDTCisEltOfVec_Info.OtherOperandNum, N, NodeInfo,
1602 // Filter vector types out of VecOperand that don't have the right element
1604 return TI.EnforceVectorEltTypeIs(VecOperand->getExtType(VResNo),
1605 NodeToApply->getExtType(ResNo));
1607 case SDTCisSubVecOfVec: {
1608 unsigned VResNo = 0;
1609 TreePatternNode *BigVecOperand =
1610 getOperandNum(x.SDTCisSubVecOfVec_Info.OtherOperandNum, N, NodeInfo,
1613 // Filter vector types out of BigVecOperand that don't have the
1614 // right subvector type.
1615 return TI.EnforceVectorSubVectorTypeIs(BigVecOperand->getExtType(VResNo),
1616 NodeToApply->getExtType(ResNo));
1618 case SDTCVecEltisVT: {
1619 return TI.EnforceVectorEltTypeIs(NodeToApply->getExtType(ResNo), VVT);
1621 case SDTCisSameNumEltsAs: {
1622 unsigned OResNo = 0;
1623 TreePatternNode *OtherNode =
1624 getOperandNum(x.SDTCisSameNumEltsAs_Info.OtherOperandNum,
1625 N, NodeInfo, OResNo);
1626 return TI.EnforceSameNumElts(OtherNode->getExtType(OResNo),
1627 NodeToApply->getExtType(ResNo));
1629 case SDTCisSameSizeAs: {
1630 unsigned OResNo = 0;
1631 TreePatternNode *OtherNode =
1632 getOperandNum(x.SDTCisSameSizeAs_Info.OtherOperandNum,
1633 N, NodeInfo, OResNo);
1634 return TI.EnforceSameSize(OtherNode->getExtType(OResNo),
1635 NodeToApply->getExtType(ResNo));
1638 llvm_unreachable("Invalid ConstraintType!");
1641 // Update the node type to match an instruction operand or result as specified
1642 // in the ins or outs lists on the instruction definition. Return true if the
1643 // type was actually changed.
1644 bool TreePatternNode::UpdateNodeTypeFromInst(unsigned ResNo,
1647 // The 'unknown' operand indicates that types should be inferred from the
1649 if (Operand->isSubClassOf("unknown_class"))
1652 // The Operand class specifies a type directly.
1653 if (Operand->isSubClassOf("Operand")) {
1654 Record *R = Operand->getValueAsDef("Type");
1655 const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
1656 return UpdateNodeType(ResNo, getValueTypeByHwMode(R, T.getHwModes()), TP);
1659 // PointerLikeRegClass has a type that is determined at runtime.
1660 if (Operand->isSubClassOf("PointerLikeRegClass"))
1661 return UpdateNodeType(ResNo, MVT::iPTR, TP);
1663 // Both RegisterClass and RegisterOperand operands derive their types from a
1664 // register class def.
1665 Record *RC = nullptr;
1666 if (Operand->isSubClassOf("RegisterClass"))
1668 else if (Operand->isSubClassOf("RegisterOperand"))
1669 RC = Operand->getValueAsDef("RegClass");
1671 assert(RC && "Unknown operand type");
1672 CodeGenTarget &Tgt = TP.getDAGPatterns().getTargetInfo();
1673 return UpdateNodeType(ResNo, Tgt.getRegisterClass(RC).getValueTypes(), TP);
1676 bool TreePatternNode::ContainsUnresolvedType(TreePattern &TP) const {
1677 for (unsigned i = 0, e = Types.size(); i != e; ++i)
1678 if (!TP.getInfer().isConcrete(Types[i], true))
1680 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1681 if (getChild(i)->ContainsUnresolvedType(TP))
1686 bool TreePatternNode::hasProperTypeByHwMode() const {
1687 for (const TypeSetByHwMode &S : Types)
1688 if (!S.isDefaultOnly())
1690 for (const TreePatternNodePtr &C : Children)
1691 if (C->hasProperTypeByHwMode())
1696 bool TreePatternNode::hasPossibleType() const {
1697 for (const TypeSetByHwMode &S : Types)
1698 if (!S.isPossible())
1700 for (const TreePatternNodePtr &C : Children)
1701 if (!C->hasPossibleType())
1706 bool TreePatternNode::setDefaultMode(unsigned Mode) {
1707 for (TypeSetByHwMode &S : Types) {
1709 // Check if the selected mode had a type conflict.
1710 if (S.get(DefaultMode).empty())
1713 for (const TreePatternNodePtr &C : Children)
1714 if (!C->setDefaultMode(Mode))
1719 //===----------------------------------------------------------------------===//
1720 // SDNodeInfo implementation
1722 SDNodeInfo::SDNodeInfo(Record *R, const CodeGenHwModes &CGH) : Def(R) {
1723 EnumName = R->getValueAsString("Opcode");
1724 SDClassName = R->getValueAsString("SDClass");
1725 Record *TypeProfile = R->getValueAsDef("TypeProfile");
1726 NumResults = TypeProfile->getValueAsInt("NumResults");
1727 NumOperands = TypeProfile->getValueAsInt("NumOperands");
1729 // Parse the properties.
1730 Properties = parseSDPatternOperatorProperties(R);
1732 // Parse the type constraints.
1733 std::vector<Record*> ConstraintList =
1734 TypeProfile->getValueAsListOfDefs("Constraints");
1735 for (Record *R : ConstraintList)
1736 TypeConstraints.emplace_back(R, CGH);
1739 /// getKnownType - If the type constraints on this node imply a fixed type
1740 /// (e.g. all stores return void, etc), then return it as an
1741 /// MVT::SimpleValueType. Otherwise, return EEVT::Other.
1742 MVT::SimpleValueType SDNodeInfo::getKnownType(unsigned ResNo) const {
1743 unsigned NumResults = getNumResults();
1744 assert(NumResults <= 1 &&
1745 "We only work with nodes with zero or one result so far!");
1746 assert(ResNo == 0 && "Only handles single result nodes so far");
1748 for (const SDTypeConstraint &Constraint : TypeConstraints) {
1749 // Make sure that this applies to the correct node result.
1750 if (Constraint.OperandNo >= NumResults) // FIXME: need value #
1753 switch (Constraint.ConstraintType) {
1755 case SDTypeConstraint::SDTCisVT:
1756 if (Constraint.VVT.isSimple())
1757 return Constraint.VVT.getSimple().SimpleTy;
1759 case SDTypeConstraint::SDTCisPtrTy:
1766 //===----------------------------------------------------------------------===//
1767 // TreePatternNode implementation
1770 static unsigned GetNumNodeResults(Record *Operator, CodeGenDAGPatterns &CDP) {
1771 if (Operator->getName() == "set" ||
1772 Operator->getName() == "implicit")
1773 return 0; // All return nothing.
1775 if (Operator->isSubClassOf("Intrinsic"))
1776 return CDP.getIntrinsic(Operator).IS.RetVTs.size();
1778 if (Operator->isSubClassOf("SDNode"))
1779 return CDP.getSDNodeInfo(Operator).getNumResults();
1781 if (Operator->isSubClassOf("PatFrags")) {
1782 // If we've already parsed this pattern fragment, get it. Otherwise, handle
1783 // the forward reference case where one pattern fragment references another
1784 // before it is processed.
1785 if (TreePattern *PFRec = CDP.getPatternFragmentIfRead(Operator)) {
1786 // The number of results of a fragment with alternative records is the
1787 // maximum number of results across all alternatives.
1788 unsigned NumResults = 0;
1789 for (auto T : PFRec->getTrees())
1790 NumResults = std::max(NumResults, T->getNumTypes());
1794 ListInit *LI = Operator->getValueAsListInit("Fragments");
1795 assert(LI && "Invalid Fragment");
1796 unsigned NumResults = 0;
1797 for (Init *I : LI->getValues()) {
1798 Record *Op = nullptr;
1799 if (DagInit *Dag = dyn_cast<DagInit>(I))
1800 if (DefInit *DI = dyn_cast<DefInit>(Dag->getOperator()))
1802 assert(Op && "Invalid Fragment");
1803 NumResults = std::max(NumResults, GetNumNodeResults(Op, CDP));
1808 if (Operator->isSubClassOf("Instruction")) {
1809 CodeGenInstruction &InstInfo = CDP.getTargetInfo().getInstruction(Operator);
1811 unsigned NumDefsToAdd = InstInfo.Operands.NumDefs;
1813 // Subtract any defaulted outputs.
1814 for (unsigned i = 0; i != InstInfo.Operands.NumDefs; ++i) {
1815 Record *OperandNode = InstInfo.Operands[i].Rec;
1817 if (OperandNode->isSubClassOf("OperandWithDefaultOps") &&
1818 !CDP.getDefaultOperand(OperandNode).DefaultOps.empty())
1822 // Add on one implicit def if it has a resolvable type.
1823 if (InstInfo.HasOneImplicitDefWithKnownVT(CDP.getTargetInfo()) !=MVT::Other)
1825 return NumDefsToAdd;
1828 if (Operator->isSubClassOf("SDNodeXForm"))
1829 return 1; // FIXME: Generalize SDNodeXForm
1831 if (Operator->isSubClassOf("ValueType"))
1832 return 1; // A type-cast of one result.
1834 if (Operator->isSubClassOf("ComplexPattern"))
1837 errs() << *Operator;
1838 PrintFatalError("Unhandled node in GetNumNodeResults");
1841 void TreePatternNode::print(raw_ostream &OS) const {
1843 OS << *getLeafValue();
1845 OS << '(' << getOperator()->getName();
1847 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
1849 getExtType(i).writeToStream(OS);
1853 if (getNumChildren() != 0) {
1855 getChild(0)->print(OS);
1856 for (unsigned i = 1, e = getNumChildren(); i != e; ++i) {
1858 getChild(i)->print(OS);
1864 for (const TreePredicateCall &Pred : PredicateCalls) {
1867 OS << Pred.Scope << ":";
1868 OS << Pred.Fn.getFnName() << ">>";
1871 OS << "<<X:" << TransformFn->getName() << ">>";
1872 if (!getName().empty())
1873 OS << ":$" << getName();
1875 for (const ScopedName &Name : NamesAsPredicateArg)
1876 OS << ":$pred:" << Name.getScope() << ":" << Name.getIdentifier();
1878 void TreePatternNode::dump() const {
1882 /// isIsomorphicTo - Return true if this node is recursively
1883 /// isomorphic to the specified node. For this comparison, the node's
1884 /// entire state is considered. The assigned name is ignored, since
1885 /// nodes with differing names are considered isomorphic. However, if
1886 /// the assigned name is present in the dependent variable set, then
1887 /// the assigned name is considered significant and the node is
1888 /// isomorphic if the names match.
1889 bool TreePatternNode::isIsomorphicTo(const TreePatternNode *N,
1890 const MultipleUseVarSet &DepVars) const {
1891 if (N == this) return true;
1892 if (N->isLeaf() != isLeaf() || getExtTypes() != N->getExtTypes() ||
1893 getPredicateCalls() != N->getPredicateCalls() ||
1894 getTransformFn() != N->getTransformFn())
1898 if (DefInit *DI = dyn_cast<DefInit>(getLeafValue())) {
1899 if (DefInit *NDI = dyn_cast<DefInit>(N->getLeafValue())) {
1900 return ((DI->getDef() == NDI->getDef())
1901 && (DepVars.find(getName()) == DepVars.end()
1902 || getName() == N->getName()));
1905 return getLeafValue() == N->getLeafValue();
1908 if (N->getOperator() != getOperator() ||
1909 N->getNumChildren() != getNumChildren()) return false;
1910 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1911 if (!getChild(i)->isIsomorphicTo(N->getChild(i), DepVars))
1916 /// clone - Make a copy of this tree and all of its children.
1918 TreePatternNodePtr TreePatternNode::clone() const {
1919 TreePatternNodePtr New;
1921 New = std::make_shared<TreePatternNode>(getLeafValue(), getNumTypes());
1923 std::vector<TreePatternNodePtr> CChildren;
1924 CChildren.reserve(Children.size());
1925 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1926 CChildren.push_back(getChild(i)->clone());
1927 New = std::make_shared<TreePatternNode>(getOperator(), std::move(CChildren),
1930 New->setName(getName());
1931 New->setNamesAsPredicateArg(getNamesAsPredicateArg());
1933 New->setPredicateCalls(getPredicateCalls());
1934 New->setTransformFn(getTransformFn());
1938 /// RemoveAllTypes - Recursively strip all the types of this tree.
1939 void TreePatternNode::RemoveAllTypes() {
1940 // Reset to unknown type.
1941 std::fill(Types.begin(), Types.end(), TypeSetByHwMode());
1942 if (isLeaf()) return;
1943 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1944 getChild(i)->RemoveAllTypes();
1948 /// SubstituteFormalArguments - Replace the formal arguments in this tree
1949 /// with actual values specified by ArgMap.
1950 void TreePatternNode::SubstituteFormalArguments(
1951 std::map<std::string, TreePatternNodePtr> &ArgMap) {
1952 if (isLeaf()) return;
1954 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
1955 TreePatternNode *Child = getChild(i);
1956 if (Child->isLeaf()) {
1957 Init *Val = Child->getLeafValue();
1958 // Note that, when substituting into an output pattern, Val might be an
1960 if (isa<UnsetInit>(Val) || (isa<DefInit>(Val) &&
1961 cast<DefInit>(Val)->getDef()->getName() == "node")) {
1962 // We found a use of a formal argument, replace it with its value.
1963 TreePatternNodePtr NewChild = ArgMap[Child->getName()];
1964 assert(NewChild && "Couldn't find formal argument!");
1965 assert((Child->getPredicateCalls().empty() ||
1966 NewChild->getPredicateCalls() == Child->getPredicateCalls()) &&
1967 "Non-empty child predicate clobbered!");
1968 setChild(i, std::move(NewChild));
1971 getChild(i)->SubstituteFormalArguments(ArgMap);
1977 /// InlinePatternFragments - If this pattern refers to any pattern
1978 /// fragments, return the set of inlined versions (this can be more than
1979 /// one if a PatFrags record has multiple alternatives).
1980 void TreePatternNode::InlinePatternFragments(
1981 TreePatternNodePtr T, TreePattern &TP,
1982 std::vector<TreePatternNodePtr> &OutAlternatives) {
1988 OutAlternatives.push_back(T); // nothing to do.
1992 Record *Op = getOperator();
1994 if (!Op->isSubClassOf("PatFrags")) {
1995 if (getNumChildren() == 0) {
1996 OutAlternatives.push_back(T);
2000 // Recursively inline children nodes.
2001 std::vector<std::vector<TreePatternNodePtr> > ChildAlternatives;
2002 ChildAlternatives.resize(getNumChildren());
2003 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
2004 TreePatternNodePtr Child = getChildShared(i);
2005 Child->InlinePatternFragments(Child, TP, ChildAlternatives[i]);
2006 // If there are no alternatives for any child, there are no
2007 // alternatives for this expression as whole.
2008 if (ChildAlternatives[i].empty())
2011 for (auto NewChild : ChildAlternatives[i])
2012 assert((Child->getPredicateCalls().empty() ||
2013 NewChild->getPredicateCalls() == Child->getPredicateCalls()) &&
2014 "Non-empty child predicate clobbered!");
2017 // The end result is an all-pairs construction of the resultant pattern.
2018 std::vector<unsigned> Idxs;
2019 Idxs.resize(ChildAlternatives.size());
2022 // Create the variant and add it to the output list.
2023 std::vector<TreePatternNodePtr> NewChildren;
2024 for (unsigned i = 0, e = ChildAlternatives.size(); i != e; ++i)
2025 NewChildren.push_back(ChildAlternatives[i][Idxs[i]]);
2026 TreePatternNodePtr R = std::make_shared<TreePatternNode>(
2027 getOperator(), std::move(NewChildren), getNumTypes());
2029 // Copy over properties.
2030 R->setName(getName());
2031 R->setNamesAsPredicateArg(getNamesAsPredicateArg());
2032 R->setPredicateCalls(getPredicateCalls());
2033 R->setTransformFn(getTransformFn());
2034 for (unsigned i = 0, e = getNumTypes(); i != e; ++i)
2035 R->setType(i, getExtType(i));
2036 for (unsigned i = 0, e = getNumResults(); i != e; ++i)
2037 R->setResultIndex(i, getResultIndex(i));
2039 // Register alternative.
2040 OutAlternatives.push_back(R);
2042 // Increment indices to the next permutation by incrementing the
2043 // indices from last index backward, e.g., generate the sequence
2044 // [0, 0], [0, 1], [1, 0], [1, 1].
2046 for (IdxsIdx = Idxs.size() - 1; IdxsIdx >= 0; --IdxsIdx) {
2047 if (++Idxs[IdxsIdx] == ChildAlternatives[IdxsIdx].size())
2052 NotDone = (IdxsIdx >= 0);
2058 // Otherwise, we found a reference to a fragment. First, look up its
2059 // TreePattern record.
2060 TreePattern *Frag = TP.getDAGPatterns().getPatternFragment(Op);
2062 // Verify that we are passing the right number of operands.
2063 if (Frag->getNumArgs() != Children.size()) {
2064 TP.error("'" + Op->getName() + "' fragment requires " +
2065 Twine(Frag->getNumArgs()) + " operands!");
2069 TreePredicateFn PredFn(Frag);
2071 if (TreePredicateFn(Frag).usesOperands())
2072 Scope = TP.getDAGPatterns().allocateScope();
2074 // Compute the map of formal to actual arguments.
2075 std::map<std::string, TreePatternNodePtr> ArgMap;
2076 for (unsigned i = 0, e = Frag->getNumArgs(); i != e; ++i) {
2077 TreePatternNodePtr Child = getChildShared(i);
2079 Child = Child->clone();
2080 Child->addNameAsPredicateArg(ScopedName(Scope, Frag->getArgName(i)));
2082 ArgMap[Frag->getArgName(i)] = Child;
2085 // Loop over all fragment alternatives.
2086 for (auto Alternative : Frag->getTrees()) {
2087 TreePatternNodePtr FragTree = Alternative->clone();
2089 if (!PredFn.isAlwaysTrue())
2090 FragTree->addPredicateCall(PredFn, Scope);
2092 // Resolve formal arguments to their actual value.
2093 if (Frag->getNumArgs())
2094 FragTree->SubstituteFormalArguments(ArgMap);
2096 // Transfer types. Note that the resolved alternative may have fewer
2097 // (but not more) results than the PatFrags node.
2098 FragTree->setName(getName());
2099 for (unsigned i = 0, e = FragTree->getNumTypes(); i != e; ++i)
2100 FragTree->UpdateNodeType(i, getExtType(i), TP);
2102 // Transfer in the old predicates.
2103 for (const TreePredicateCall &Pred : getPredicateCalls())
2104 FragTree->addPredicateCall(Pred);
2106 // The fragment we inlined could have recursive inlining that is needed. See
2107 // if there are any pattern fragments in it and inline them as needed.
2108 FragTree->InlinePatternFragments(FragTree, TP, OutAlternatives);
2112 /// getImplicitType - Check to see if the specified record has an implicit
2113 /// type which should be applied to it. This will infer the type of register
2114 /// references from the register file information, for example.
2116 /// When Unnamed is set, return the type of a DAG operand with no name, such as
2117 /// the F8RC register class argument in:
2119 /// (COPY_TO_REGCLASS GPR:$src, F8RC)
2121 /// When Unnamed is false, return the type of a named DAG operand such as the
2122 /// GPR:$src operand above.
2124 static TypeSetByHwMode getImplicitType(Record *R, unsigned ResNo,
2128 CodeGenDAGPatterns &CDP = TP.getDAGPatterns();
2130 // Check to see if this is a register operand.
2131 if (R->isSubClassOf("RegisterOperand")) {
2132 assert(ResNo == 0 && "Regoperand ref only has one result!");
2134 return TypeSetByHwMode(); // Unknown.
2135 Record *RegClass = R->getValueAsDef("RegClass");
2136 const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
2137 return TypeSetByHwMode(T.getRegisterClass(RegClass).getValueTypes());
2140 // Check to see if this is a register or a register class.
2141 if (R->isSubClassOf("RegisterClass")) {
2142 assert(ResNo == 0 && "Regclass ref only has one result!");
2143 // An unnamed register class represents itself as an i32 immediate, for
2144 // example on a COPY_TO_REGCLASS instruction.
2146 return TypeSetByHwMode(MVT::i32);
2148 // In a named operand, the register class provides the possible set of
2151 return TypeSetByHwMode(); // Unknown.
2152 const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
2153 return TypeSetByHwMode(T.getRegisterClass(R).getValueTypes());
2156 if (R->isSubClassOf("PatFrags")) {
2157 assert(ResNo == 0 && "FIXME: PatFrag with multiple results?");
2158 // Pattern fragment types will be resolved when they are inlined.
2159 return TypeSetByHwMode(); // Unknown.
2162 if (R->isSubClassOf("Register")) {
2163 assert(ResNo == 0 && "Registers only produce one result!");
2165 return TypeSetByHwMode(); // Unknown.
2166 const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
2167 return TypeSetByHwMode(T.getRegisterVTs(R));
2170 if (R->isSubClassOf("SubRegIndex")) {
2171 assert(ResNo == 0 && "SubRegisterIndices only produce one result!");
2172 return TypeSetByHwMode(MVT::i32);
2175 if (R->isSubClassOf("ValueType")) {
2176 assert(ResNo == 0 && "This node only has one result!");
2177 // An unnamed VTSDNode represents itself as an MVT::Other immediate.
2179 // (sext_inreg GPR:$src, i16)
2182 return TypeSetByHwMode(MVT::Other);
2183 // With a name, the ValueType simply provides the type of the named
2186 // (sext_inreg i32:$src, i16)
2189 return TypeSetByHwMode(); // Unknown.
2190 const CodeGenHwModes &CGH = CDP.getTargetInfo().getHwModes();
2191 return TypeSetByHwMode(getValueTypeByHwMode(R, CGH));
2194 if (R->isSubClassOf("CondCode")) {
2195 assert(ResNo == 0 && "This node only has one result!");
2196 // Using a CondCodeSDNode.
2197 return TypeSetByHwMode(MVT::Other);
2200 if (R->isSubClassOf("ComplexPattern")) {
2201 assert(ResNo == 0 && "FIXME: ComplexPattern with multiple results?");
2203 return TypeSetByHwMode(); // Unknown.
2204 return TypeSetByHwMode(CDP.getComplexPattern(R).getValueType());
2206 if (R->isSubClassOf("PointerLikeRegClass")) {
2207 assert(ResNo == 0 && "Regclass can only have one result!");
2208 TypeSetByHwMode VTS(MVT::iPTR);
2209 TP.getInfer().expandOverloads(VTS);
2213 if (R->getName() == "node" || R->getName() == "srcvalue" ||
2214 R->getName() == "zero_reg" || R->getName() == "immAllOnesV" ||
2215 R->getName() == "immAllZerosV" || R->getName() == "undef_tied_input") {
2217 return TypeSetByHwMode(); // Unknown.
2220 if (R->isSubClassOf("Operand")) {
2221 const CodeGenHwModes &CGH = CDP.getTargetInfo().getHwModes();
2222 Record *T = R->getValueAsDef("Type");
2223 return TypeSetByHwMode(getValueTypeByHwMode(T, CGH));
2226 TP.error("Unknown node flavor used in pattern: " + R->getName());
2227 return TypeSetByHwMode(MVT::Other);
2231 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
2232 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
2233 const CodeGenIntrinsic *TreePatternNode::
2234 getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const {
2235 if (getOperator() != CDP.get_intrinsic_void_sdnode() &&
2236 getOperator() != CDP.get_intrinsic_w_chain_sdnode() &&
2237 getOperator() != CDP.get_intrinsic_wo_chain_sdnode())
2240 unsigned IID = cast<IntInit>(getChild(0)->getLeafValue())->getValue();
2241 return &CDP.getIntrinsicInfo(IID);
2244 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
2245 /// return the ComplexPattern information, otherwise return null.
2246 const ComplexPattern *
2247 TreePatternNode::getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const {
2250 DefInit *DI = dyn_cast<DefInit>(getLeafValue());
2255 Rec = getOperator();
2257 if (!Rec->isSubClassOf("ComplexPattern"))
2259 return &CGP.getComplexPattern(Rec);
2262 unsigned TreePatternNode::getNumMIResults(const CodeGenDAGPatterns &CGP) const {
2263 // A ComplexPattern specifically declares how many results it fills in.
2264 if (const ComplexPattern *CP = getComplexPatternInfo(CGP))
2265 return CP->getNumOperands();
2267 // If MIOperandInfo is specified, that gives the count.
2269 DefInit *DI = dyn_cast<DefInit>(getLeafValue());
2270 if (DI && DI->getDef()->isSubClassOf("Operand")) {
2271 DagInit *MIOps = DI->getDef()->getValueAsDag("MIOperandInfo");
2272 if (MIOps->getNumArgs())
2273 return MIOps->getNumArgs();
2277 // Otherwise there is just one result.
2281 /// NodeHasProperty - Return true if this node has the specified property.
2282 bool TreePatternNode::NodeHasProperty(SDNP Property,
2283 const CodeGenDAGPatterns &CGP) const {
2285 if (const ComplexPattern *CP = getComplexPatternInfo(CGP))
2286 return CP->hasProperty(Property);
2291 if (Property != SDNPHasChain) {
2292 // The chain proprety is already present on the different intrinsic node
2293 // types (intrinsic_w_chain, intrinsic_void), and is not explicitly listed
2294 // on the intrinsic. Anything else is specific to the individual intrinsic.
2295 if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CGP))
2296 return Int->hasProperty(Property);
2299 if (!Operator->isSubClassOf("SDPatternOperator"))
2302 return CGP.getSDNodeInfo(Operator).hasProperty(Property);
2308 /// TreeHasProperty - Return true if any node in this tree has the specified
2310 bool TreePatternNode::TreeHasProperty(SDNP Property,
2311 const CodeGenDAGPatterns &CGP) const {
2312 if (NodeHasProperty(Property, CGP))
2314 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
2315 if (getChild(i)->TreeHasProperty(Property, CGP))
2320 /// isCommutativeIntrinsic - Return true if the node corresponds to a
2321 /// commutative intrinsic.
2323 TreePatternNode::isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const {
2324 if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP))
2325 return Int->isCommutative;
2329 static bool isOperandClass(const TreePatternNode *N, StringRef Class) {
2331 return N->getOperator()->isSubClassOf(Class);
2333 DefInit *DI = dyn_cast<DefInit>(N->getLeafValue());
2334 if (DI && DI->getDef()->isSubClassOf(Class))
2340 static void emitTooManyOperandsError(TreePattern &TP,
2344 TP.error("Instruction '" + InstName + "' was provided " + Twine(Actual) +
2345 " operands but expected only " + Twine(Expected) + "!");
2348 static void emitTooFewOperandsError(TreePattern &TP,
2351 TP.error("Instruction '" + InstName +
2352 "' expects more than the provided " + Twine(Actual) + " operands!");
2355 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
2356 /// this node and its children in the tree. This returns true if it makes a
2357 /// change, false otherwise. If a type contradiction is found, flag an error.
2358 bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
2362 CodeGenDAGPatterns &CDP = TP.getDAGPatterns();
2364 if (DefInit *DI = dyn_cast<DefInit>(getLeafValue())) {
2365 // If it's a regclass or something else known, include the type.
2366 bool MadeChange = false;
2367 for (unsigned i = 0, e = Types.size(); i != e; ++i)
2368 MadeChange |= UpdateNodeType(i, getImplicitType(DI->getDef(), i,
2370 !hasName(), TP), TP);
2374 if (IntInit *II = dyn_cast<IntInit>(getLeafValue())) {
2375 assert(Types.size() == 1 && "Invalid IntInit");
2377 // Int inits are always integers. :)
2378 bool MadeChange = TP.getInfer().EnforceInteger(Types[0]);
2380 if (!TP.getInfer().isConcrete(Types[0], false))
2383 ValueTypeByHwMode VVT = TP.getInfer().getConcrete(Types[0], false);
2384 for (auto &P : VVT) {
2385 MVT::SimpleValueType VT = P.second.SimpleTy;
2386 if (VT == MVT::iPTR || VT == MVT::iPTRAny)
2388 unsigned Size = MVT(VT).getSizeInBits();
2389 // Make sure that the value is representable for this type.
2392 // Check that the value doesn't use more bits than we have. It must
2393 // either be a sign- or zero-extended equivalent of the original.
2394 int64_t SignBitAndAbove = II->getValue() >> (Size - 1);
2395 if (SignBitAndAbove == -1 || SignBitAndAbove == 0 ||
2396 SignBitAndAbove == 1)
2399 TP.error("Integer value '" + Twine(II->getValue()) +
2400 "' is out of range for type '" + getEnumName(VT) + "'!");
2409 if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP)) {
2410 bool MadeChange = false;
2412 // Apply the result type to the node.
2413 unsigned NumRetVTs = Int->IS.RetVTs.size();
2414 unsigned NumParamVTs = Int->IS.ParamVTs.size();
2416 for (unsigned i = 0, e = NumRetVTs; i != e; ++i)
2417 MadeChange |= UpdateNodeType(i, Int->IS.RetVTs[i], TP);
2419 if (getNumChildren() != NumParamVTs + 1) {
2420 TP.error("Intrinsic '" + Int->Name + "' expects " + Twine(NumParamVTs) +
2421 " operands, not " + Twine(getNumChildren() - 1) + " operands!");
2425 // Apply type info to the intrinsic ID.
2426 MadeChange |= getChild(0)->UpdateNodeType(0, MVT::iPTR, TP);
2428 for (unsigned i = 0, e = getNumChildren()-1; i != e; ++i) {
2429 MadeChange |= getChild(i+1)->ApplyTypeConstraints(TP, NotRegisters);
2431 MVT::SimpleValueType OpVT = Int->IS.ParamVTs[i];
2432 assert(getChild(i+1)->getNumTypes() == 1 && "Unhandled case");
2433 MadeChange |= getChild(i+1)->UpdateNodeType(0, OpVT, TP);
2438 if (getOperator()->isSubClassOf("SDNode")) {
2439 const SDNodeInfo &NI = CDP.getSDNodeInfo(getOperator());
2441 // Check that the number of operands is sane. Negative operands -> varargs.
2442 if (NI.getNumOperands() >= 0 &&
2443 getNumChildren() != (unsigned)NI.getNumOperands()) {
2444 TP.error(getOperator()->getName() + " node requires exactly " +
2445 Twine(NI.getNumOperands()) + " operands!");
2449 bool MadeChange = false;
2450 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
2451 MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters);
2452 MadeChange |= NI.ApplyTypeConstraints(this, TP);
2456 if (getOperator()->isSubClassOf("Instruction")) {
2457 const DAGInstruction &Inst = CDP.getInstruction(getOperator());
2458 CodeGenInstruction &InstInfo =
2459 CDP.getTargetInfo().getInstruction(getOperator());
2461 bool MadeChange = false;
2463 // Apply the result types to the node, these come from the things in the
2464 // (outs) list of the instruction.
2465 unsigned NumResultsToAdd = std::min(InstInfo.Operands.NumDefs,
2466 Inst.getNumResults());
2467 for (unsigned ResNo = 0; ResNo != NumResultsToAdd; ++ResNo)
2468 MadeChange |= UpdateNodeTypeFromInst(ResNo, Inst.getResult(ResNo), TP);
2470 // If the instruction has implicit defs, we apply the first one as a result.
2471 // FIXME: This sucks, it should apply all implicit defs.
2472 if (!InstInfo.ImplicitDefs.empty()) {
2473 unsigned ResNo = NumResultsToAdd;
2475 // FIXME: Generalize to multiple possible types and multiple possible
2477 MVT::SimpleValueType VT =
2478 InstInfo.HasOneImplicitDefWithKnownVT(CDP.getTargetInfo());
2480 if (VT != MVT::Other)
2481 MadeChange |= UpdateNodeType(ResNo, VT, TP);
2484 // If this is an INSERT_SUBREG, constrain the source and destination VTs to
2486 if (getOperator()->getName() == "INSERT_SUBREG") {
2487 assert(getChild(0)->getNumTypes() == 1 && "FIXME: Unhandled");
2488 MadeChange |= UpdateNodeType(0, getChild(0)->getExtType(0), TP);
2489 MadeChange |= getChild(0)->UpdateNodeType(0, getExtType(0), TP);
2490 } else if (getOperator()->getName() == "REG_SEQUENCE") {
2491 // We need to do extra, custom typechecking for REG_SEQUENCE since it is
2494 unsigned NChild = getNumChildren();
2496 TP.error("REG_SEQUENCE requires at least 3 operands!");
2500 if (NChild % 2 == 0) {
2501 TP.error("REG_SEQUENCE requires an odd number of operands!");
2505 if (!isOperandClass(getChild(0), "RegisterClass")) {
2506 TP.error("REG_SEQUENCE requires a RegisterClass for first operand!");
2510 for (unsigned I = 1; I < NChild; I += 2) {
2511 TreePatternNode *SubIdxChild = getChild(I + 1);
2512 if (!isOperandClass(SubIdxChild, "SubRegIndex")) {
2513 TP.error("REG_SEQUENCE requires a SubRegIndex for operand " +
2514 Twine(I + 1) + "!");
2520 // If one or more operands with a default value appear at the end of the
2521 // formal operand list for an instruction, we allow them to be overridden
2522 // by optional operands provided in the pattern.
2524 // But if an operand B without a default appears at any point after an
2525 // operand A with a default, then we don't allow A to be overridden,
2526 // because there would be no way to specify whether the next operand in
2527 // the pattern was intended to override A or skip it.
2528 unsigned NonOverridableOperands = Inst.getNumOperands();
2529 while (NonOverridableOperands > 0 &&
2530 CDP.operandHasDefault(Inst.getOperand(NonOverridableOperands-1)))
2531 --NonOverridableOperands;
2533 unsigned ChildNo = 0;
2534 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
2535 Record *OperandNode = Inst.getOperand(i);
2537 // If the operand has a default value, do we use it? We must use the
2538 // default if we've run out of children of the pattern DAG to consume,
2539 // or if the operand is followed by a non-defaulted one.
2540 if (CDP.operandHasDefault(OperandNode) &&
2541 (i < NonOverridableOperands || ChildNo >= getNumChildren()))
2544 // If we have run out of child nodes and there _isn't_ a default
2545 // value we can use for the next operand, give an error.
2546 if (ChildNo >= getNumChildren()) {
2547 emitTooFewOperandsError(TP, getOperator()->getName(), getNumChildren());
2551 TreePatternNode *Child = getChild(ChildNo++);
2552 unsigned ChildResNo = 0; // Instructions always use res #0 of their op.
2554 // If the operand has sub-operands, they may be provided by distinct
2555 // child patterns, so attempt to match each sub-operand separately.
2556 if (OperandNode->isSubClassOf("Operand")) {
2557 DagInit *MIOpInfo = OperandNode->getValueAsDag("MIOperandInfo");
2558 if (unsigned NumArgs = MIOpInfo->getNumArgs()) {
2559 // But don't do that if the whole operand is being provided by
2560 // a single ComplexPattern-related Operand.
2562 if (Child->getNumMIResults(CDP) < NumArgs) {
2563 // Match first sub-operand against the child we already have.
2564 Record *SubRec = cast<DefInit>(MIOpInfo->getArg(0))->getDef();
2566 Child->UpdateNodeTypeFromInst(ChildResNo, SubRec, TP);
2568 // And the remaining sub-operands against subsequent children.
2569 for (unsigned Arg = 1; Arg < NumArgs; ++Arg) {
2570 if (ChildNo >= getNumChildren()) {
2571 emitTooFewOperandsError(TP, getOperator()->getName(),
2575 Child = getChild(ChildNo++);
2577 SubRec = cast<DefInit>(MIOpInfo->getArg(Arg))->getDef();
2579 Child->UpdateNodeTypeFromInst(ChildResNo, SubRec, TP);
2586 // If we didn't match by pieces above, attempt to match the whole
2588 MadeChange |= Child->UpdateNodeTypeFromInst(ChildResNo, OperandNode, TP);
2591 if (!InstInfo.Operands.isVariadic && ChildNo != getNumChildren()) {
2592 emitTooManyOperandsError(TP, getOperator()->getName(),
2593 ChildNo, getNumChildren());
2597 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
2598 MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters);
2602 if (getOperator()->isSubClassOf("ComplexPattern")) {
2603 bool MadeChange = false;
2605 for (unsigned i = 0; i < getNumChildren(); ++i)
2606 MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters);
2611 assert(getOperator()->isSubClassOf("SDNodeXForm") && "Unknown node type!");
2613 // Node transforms always take one operand.
2614 if (getNumChildren() != 1) {
2615 TP.error("Node transform '" + getOperator()->getName() +
2616 "' requires one operand!");
2620 bool MadeChange = getChild(0)->ApplyTypeConstraints(TP, NotRegisters);
2624 /// OnlyOnRHSOfCommutative - Return true if this value is only allowed on the
2625 /// RHS of a commutative operation, not the on LHS.
2626 static bool OnlyOnRHSOfCommutative(TreePatternNode *N) {
2627 if (!N->isLeaf() && N->getOperator()->getName() == "imm")
2629 if (N->isLeaf() && isa<IntInit>(N->getLeafValue()))
2635 /// canPatternMatch - If it is impossible for this pattern to match on this
2636 /// target, fill in Reason and return false. Otherwise, return true. This is
2637 /// used as a sanity check for .td files (to prevent people from writing stuff
2638 /// that can never possibly work), and to prevent the pattern permuter from
2639 /// generating stuff that is useless.
2640 bool TreePatternNode::canPatternMatch(std::string &Reason,
2641 const CodeGenDAGPatterns &CDP) {
2642 if (isLeaf()) return true;
2644 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
2645 if (!getChild(i)->canPatternMatch(Reason, CDP))
2648 // If this is an intrinsic, handle cases that would make it not match. For
2649 // example, if an operand is required to be an immediate.
2650 if (getOperator()->isSubClassOf("Intrinsic")) {
2655 if (getOperator()->isSubClassOf("ComplexPattern"))
2658 // If this node is a commutative operator, check that the LHS isn't an
2660 const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(getOperator());
2661 bool isCommIntrinsic = isCommutativeIntrinsic(CDP);
2662 if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) {
2663 // Scan all of the operands of the node and make sure that only the last one
2664 // is a constant node, unless the RHS also is.
2665 if (!OnlyOnRHSOfCommutative(getChild(getNumChildren()-1))) {
2666 unsigned Skip = isCommIntrinsic ? 1 : 0; // First operand is intrinsic id.
2667 for (unsigned i = Skip, e = getNumChildren()-1; i != e; ++i)
2668 if (OnlyOnRHSOfCommutative(getChild(i))) {
2669 Reason="Immediate value must be on the RHS of commutative operators!";
2678 //===----------------------------------------------------------------------===//
2679 // TreePattern implementation
2682 TreePattern::TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
2683 CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp),
2684 isInputPattern(isInput), HasError(false),
2686 for (Init *I : RawPat->getValues())
2687 Trees.push_back(ParseTreePattern(I, ""));
2690 TreePattern::TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
2691 CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp),
2692 isInputPattern(isInput), HasError(false),
2694 Trees.push_back(ParseTreePattern(Pat, ""));
2697 TreePattern::TreePattern(Record *TheRec, TreePatternNodePtr Pat, bool isInput,
2698 CodeGenDAGPatterns &cdp)
2699 : TheRecord(TheRec), CDP(cdp), isInputPattern(isInput), HasError(false),
2701 Trees.push_back(Pat);
2704 void TreePattern::error(const Twine &Msg) {
2708 PrintError(TheRecord->getLoc(), "In " + TheRecord->getName() + ": " + Msg);
2712 void TreePattern::ComputeNamedNodes() {
2713 for (TreePatternNodePtr &Tree : Trees)
2714 ComputeNamedNodes(Tree.get());
2717 void TreePattern::ComputeNamedNodes(TreePatternNode *N) {
2718 if (!N->getName().empty())
2719 NamedNodes[N->getName()].push_back(N);
2721 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
2722 ComputeNamedNodes(N->getChild(i));
2725 TreePatternNodePtr TreePattern::ParseTreePattern(Init *TheInit,
2727 if (DefInit *DI = dyn_cast<DefInit>(TheInit)) {
2728 Record *R = DI->getDef();
2730 // Direct reference to a leaf DagNode or PatFrag? Turn it into a
2731 // TreePatternNode of its own. For example:
2732 /// (foo GPR, imm) -> (foo GPR, (imm))
2733 if (R->isSubClassOf("SDNode") || R->isSubClassOf("PatFrags"))
2734 return ParseTreePattern(
2735 DagInit::get(DI, nullptr,
2736 std::vector<std::pair<Init*, StringInit*> >()),
2740 TreePatternNodePtr Res = std::make_shared<TreePatternNode>(DI, 1);
2741 if (R->getName() == "node" && !OpName.empty()) {
2743 error("'node' argument requires a name to match with operand list");
2744 Args.push_back(OpName);
2747 Res->setName(OpName);
2751 // ?:$name or just $name.
2752 if (isa<UnsetInit>(TheInit)) {
2754 error("'?' argument requires a name to match with operand list");
2755 TreePatternNodePtr Res = std::make_shared<TreePatternNode>(TheInit, 1);
2756 Args.push_back(OpName);
2757 Res->setName(OpName);
2761 if (isa<IntInit>(TheInit) || isa<BitInit>(TheInit)) {
2762 if (!OpName.empty())
2763 error("Constant int or bit argument should not have a name!");
2764 if (isa<BitInit>(TheInit))
2765 TheInit = TheInit->convertInitializerTo(IntRecTy::get());
2766 return std::make_shared<TreePatternNode>(TheInit, 1);
2769 if (BitsInit *BI = dyn_cast<BitsInit>(TheInit)) {
2770 // Turn this into an IntInit.
2771 Init *II = BI->convertInitializerTo(IntRecTy::get());
2772 if (!II || !isa<IntInit>(II))
2773 error("Bits value must be constants!");
2774 return ParseTreePattern(II, OpName);
2777 DagInit *Dag = dyn_cast<DagInit>(TheInit);
2779 TheInit->print(errs());
2780 error("Pattern has unexpected init kind!");
2782 DefInit *OpDef = dyn_cast<DefInit>(Dag->getOperator());
2783 if (!OpDef) error("Pattern has unexpected operator type!");
2784 Record *Operator = OpDef->getDef();
2786 if (Operator->isSubClassOf("ValueType")) {
2787 // If the operator is a ValueType, then this must be "type cast" of a leaf
2789 if (Dag->getNumArgs() != 1)
2790 error("Type cast only takes one operand!");
2792 TreePatternNodePtr New =
2793 ParseTreePattern(Dag->getArg(0), Dag->getArgNameStr(0));
2795 // Apply the type cast.
2796 assert(New->getNumTypes() == 1 && "FIXME: Unhandled");
2797 const CodeGenHwModes &CGH = getDAGPatterns().getTargetInfo().getHwModes();
2798 New->UpdateNodeType(0, getValueTypeByHwMode(Operator, CGH), *this);
2800 if (!OpName.empty())
2801 error("ValueType cast should not have a name!");
2805 // Verify that this is something that makes sense for an operator.
2806 if (!Operator->isSubClassOf("PatFrags") &&
2807 !Operator->isSubClassOf("SDNode") &&
2808 !Operator->isSubClassOf("Instruction") &&
2809 !Operator->isSubClassOf("SDNodeXForm") &&
2810 !Operator->isSubClassOf("Intrinsic") &&
2811 !Operator->isSubClassOf("ComplexPattern") &&
2812 Operator->getName() != "set" &&
2813 Operator->getName() != "implicit")
2814 error("Unrecognized node '" + Operator->getName() + "'!");
2816 // Check to see if this is something that is illegal in an input pattern.
2817 if (isInputPattern) {
2818 if (Operator->isSubClassOf("Instruction") ||
2819 Operator->isSubClassOf("SDNodeXForm"))
2820 error("Cannot use '" + Operator->getName() + "' in an input pattern!");
2822 if (Operator->isSubClassOf("Intrinsic"))
2823 error("Cannot use '" + Operator->getName() + "' in an output pattern!");
2825 if (Operator->isSubClassOf("SDNode") &&
2826 Operator->getName() != "imm" &&
2827 Operator->getName() != "timm" &&
2828 Operator->getName() != "fpimm" &&
2829 Operator->getName() != "tglobaltlsaddr" &&
2830 Operator->getName() != "tconstpool" &&
2831 Operator->getName() != "tjumptable" &&
2832 Operator->getName() != "tframeindex" &&
2833 Operator->getName() != "texternalsym" &&
2834 Operator->getName() != "tblockaddress" &&
2835 Operator->getName() != "tglobaladdr" &&
2836 Operator->getName() != "bb" &&
2837 Operator->getName() != "vt" &&
2838 Operator->getName() != "mcsym")
2839 error("Cannot use '" + Operator->getName() + "' in an output pattern!");
2842 std::vector<TreePatternNodePtr> Children;
2844 // Parse all the operands.
2845 for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i)
2846 Children.push_back(ParseTreePattern(Dag->getArg(i), Dag->getArgNameStr(i)));
2848 // Get the actual number of results before Operator is converted to an intrinsic
2849 // node (which is hard-coded to have either zero or one result).
2850 unsigned NumResults = GetNumNodeResults(Operator, CDP);
2852 // If the operator is an intrinsic, then this is just syntactic sugar for
2853 // (intrinsic_* <number>, ..children..). Pick the right intrinsic node, and
2854 // convert the intrinsic name to a number.
2855 if (Operator->isSubClassOf("Intrinsic")) {
2856 const CodeGenIntrinsic &Int = getDAGPatterns().getIntrinsic(Operator);
2857 unsigned IID = getDAGPatterns().getIntrinsicID(Operator)+1;
2859 // If this intrinsic returns void, it must have side-effects and thus a
2861 if (Int.IS.RetVTs.empty())
2862 Operator = getDAGPatterns().get_intrinsic_void_sdnode();
2863 else if (Int.ModRef != CodeGenIntrinsic::NoMem || Int.hasSideEffects)
2864 // Has side-effects, requires chain.
2865 Operator = getDAGPatterns().get_intrinsic_w_chain_sdnode();
2866 else // Otherwise, no chain.
2867 Operator = getDAGPatterns().get_intrinsic_wo_chain_sdnode();
2869 Children.insert(Children.begin(),
2870 std::make_shared<TreePatternNode>(IntInit::get(IID), 1));
2873 if (Operator->isSubClassOf("ComplexPattern")) {
2874 for (unsigned i = 0; i < Children.size(); ++i) {
2875 TreePatternNodePtr Child = Children[i];
2877 if (Child->getName().empty())
2878 error("All arguments to a ComplexPattern must be named");
2880 // Check that the ComplexPattern uses are consistent: "(MY_PAT $a, $b)"
2881 // and "(MY_PAT $b, $a)" should not be allowed in the same pattern;
2882 // neither should "(MY_PAT_1 $a, $b)" and "(MY_PAT_2 $a, $b)".
2883 auto OperandId = std::make_pair(Operator, i);
2884 auto PrevOp = ComplexPatternOperands.find(Child->getName());
2885 if (PrevOp != ComplexPatternOperands.end()) {
2886 if (PrevOp->getValue() != OperandId)
2887 error("All ComplexPattern operands must appear consistently: "
2888 "in the same order in just one ComplexPattern instance.");
2890 ComplexPatternOperands[Child->getName()] = OperandId;
2894 TreePatternNodePtr Result =
2895 std::make_shared<TreePatternNode>(Operator, std::move(Children),
2897 Result->setName(OpName);
2899 if (Dag->getName()) {
2900 assert(Result->getName().empty());
2901 Result->setName(Dag->getNameStr());
2906 /// SimplifyTree - See if we can simplify this tree to eliminate something that
2907 /// will never match in favor of something obvious that will. This is here
2908 /// strictly as a convenience to target authors because it allows them to write
2909 /// more type generic things and have useless type casts fold away.
2911 /// This returns true if any change is made.
2912 static bool SimplifyTree(TreePatternNodePtr &N) {
2916 // If we have a bitconvert with a resolved type and if the source and
2917 // destination types are the same, then the bitconvert is useless, remove it.
2918 if (N->getOperator()->getName() == "bitconvert" &&
2919 N->getExtType(0).isValueTypeByHwMode(false) &&
2920 N->getExtType(0) == N->getChild(0)->getExtType(0) &&
2921 N->getName().empty()) {
2922 N = N->getChildShared(0);
2927 // Walk all children.
2928 bool MadeChange = false;
2929 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
2930 TreePatternNodePtr Child = N->getChildShared(i);
2931 MadeChange |= SimplifyTree(Child);
2932 N->setChild(i, std::move(Child));
2939 /// InferAllTypes - Infer/propagate as many types throughout the expression
2940 /// patterns as possible. Return true if all types are inferred, false
2941 /// otherwise. Flags an error if a type contradiction is found.
2943 InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> > *InNamedTypes) {
2944 if (NamedNodes.empty())
2945 ComputeNamedNodes();
2947 bool MadeChange = true;
2948 while (MadeChange) {
2950 for (TreePatternNodePtr &Tree : Trees) {
2951 MadeChange |= Tree->ApplyTypeConstraints(*this, false);
2952 MadeChange |= SimplifyTree(Tree);
2955 // If there are constraints on our named nodes, apply them.
2956 for (auto &Entry : NamedNodes) {
2957 SmallVectorImpl<TreePatternNode*> &Nodes = Entry.second;
2959 // If we have input named node types, propagate their types to the named
2962 if (!InNamedTypes->count(Entry.getKey())) {
2963 error("Node '" + std::string(Entry.getKey()) +
2964 "' in output pattern but not input pattern");
2968 const SmallVectorImpl<TreePatternNode*> &InNodes =
2969 InNamedTypes->find(Entry.getKey())->second;
2971 // The input types should be fully resolved by now.
2972 for (TreePatternNode *Node : Nodes) {
2973 // If this node is a register class, and it is the root of the pattern
2974 // then we're mapping something onto an input register. We allow
2975 // changing the type of the input register in this case. This allows
2976 // us to match things like:
2977 // def : Pat<(v1i64 (bitconvert(v2i32 DPR:$src))), (v1i64 DPR:$src)>;
2978 if (Node == Trees[0].get() && Node->isLeaf()) {
2979 DefInit *DI = dyn_cast<DefInit>(Node->getLeafValue());
2980 if (DI && (DI->getDef()->isSubClassOf("RegisterClass") ||
2981 DI->getDef()->isSubClassOf("RegisterOperand")))
2985 assert(Node->getNumTypes() == 1 &&
2986 InNodes[0]->getNumTypes() == 1 &&
2987 "FIXME: cannot name multiple result nodes yet");
2988 MadeChange |= Node->UpdateNodeType(0, InNodes[0]->getExtType(0),
2993 // If there are multiple nodes with the same name, they must all have the
2995 if (Entry.second.size() > 1) {
2996 for (unsigned i = 0, e = Nodes.size()-1; i != e; ++i) {
2997 TreePatternNode *N1 = Nodes[i], *N2 = Nodes[i+1];
2998 assert(N1->getNumTypes() == 1 && N2->getNumTypes() == 1 &&
2999 "FIXME: cannot name multiple result nodes yet");
3001 MadeChange |= N1->UpdateNodeType(0, N2->getExtType(0), *this);
3002 MadeChange |= N2->UpdateNodeType(0, N1->getExtType(0), *this);
3008 bool HasUnresolvedTypes = false;
3009 for (const TreePatternNodePtr &Tree : Trees)
3010 HasUnresolvedTypes |= Tree->ContainsUnresolvedType(*this);
3011 return !HasUnresolvedTypes;
3014 void TreePattern::print(raw_ostream &OS) const {
3015 OS << getRecord()->getName();
3016 if (!Args.empty()) {
3017 OS << "(" << Args[0];
3018 for (unsigned i = 1, e = Args.size(); i != e; ++i)
3019 OS << ", " << Args[i];
3024 if (Trees.size() > 1)
3026 for (const TreePatternNodePtr &Tree : Trees) {
3032 if (Trees.size() > 1)
3036 void TreePattern::dump() const { print(errs()); }
3038 //===----------------------------------------------------------------------===//
3039 // CodeGenDAGPatterns implementation
3042 CodeGenDAGPatterns::CodeGenDAGPatterns(RecordKeeper &R,
3043 PatternRewriterFn PatternRewriter)
3044 : Records(R), Target(R), LegalVTS(Target.getLegalValueTypes()),
3045 PatternRewriter(PatternRewriter) {
3047 Intrinsics = CodeGenIntrinsicTable(Records);
3049 ParseNodeTransforms();
3050 ParseComplexPatterns();
3051 ParsePatternFragments();
3052 ParseDefaultOperands();
3053 ParseInstructions();
3054 ParsePatternFragments(/*OutFrags*/true);
3057 // Break patterns with parameterized types into a series of patterns,
3058 // where each one has a fixed type and is predicated on the conditions
3059 // of the associated HW mode.
3060 ExpandHwModeBasedTypes();
3062 // Generate variants. For example, commutative patterns can match
3063 // multiple ways. Add them to PatternsToMatch as well.
3066 // Infer instruction flags. For example, we can detect loads,
3067 // stores, and side effects in many cases by examining an
3068 // instruction's pattern.
3069 InferInstructionFlags();
3071 // Verify that instruction flags match the patterns.
3072 VerifyInstructionFlags();
3075 Record *CodeGenDAGPatterns::getSDNodeNamed(const std::string &Name) const {
3076 Record *N = Records.getDef(Name);
3077 if (!N || !N->isSubClassOf("SDNode"))
3078 PrintFatalError("Error getting SDNode '" + Name + "'!");
3083 // Parse all of the SDNode definitions for the target, populating SDNodes.
3084 void CodeGenDAGPatterns::ParseNodeInfo() {
3085 std::vector<Record*> Nodes = Records.getAllDerivedDefinitions("SDNode");
3086 const CodeGenHwModes &CGH = getTargetInfo().getHwModes();
3088 while (!Nodes.empty()) {
3089 Record *R = Nodes.back();
3090 SDNodes.insert(std::make_pair(R, SDNodeInfo(R, CGH)));
3094 // Get the builtin intrinsic nodes.
3095 intrinsic_void_sdnode = getSDNodeNamed("intrinsic_void");
3096 intrinsic_w_chain_sdnode = getSDNodeNamed("intrinsic_w_chain");
3097 intrinsic_wo_chain_sdnode = getSDNodeNamed("intrinsic_wo_chain");
3100 /// ParseNodeTransforms - Parse all SDNodeXForm instances into the SDNodeXForms
3101 /// map, and emit them to the file as functions.
3102 void CodeGenDAGPatterns::ParseNodeTransforms() {
3103 std::vector<Record*> Xforms = Records.getAllDerivedDefinitions("SDNodeXForm");
3104 while (!Xforms.empty()) {
3105 Record *XFormNode = Xforms.back();
3106 Record *SDNode = XFormNode->getValueAsDef("Opcode");
3107 StringRef Code = XFormNode->getValueAsString("XFormFunction");
3108 SDNodeXForms.insert(std::make_pair(XFormNode, NodeXForm(SDNode, Code)));
3114 void CodeGenDAGPatterns::ParseComplexPatterns() {
3115 std::vector<Record*> AMs = Records.getAllDerivedDefinitions("ComplexPattern");
3116 while (!AMs.empty()) {
3117 ComplexPatterns.insert(std::make_pair(AMs.back(), AMs.back()));
3123 /// ParsePatternFragments - Parse all of the PatFrag definitions in the .td
3124 /// file, building up the PatternFragments map. After we've collected them all,
3125 /// inline fragments together as necessary, so that there are no references left
3126 /// inside a pattern fragment to a pattern fragment.
3128 void CodeGenDAGPatterns::ParsePatternFragments(bool OutFrags) {
3129 std::vector<Record*> Fragments = Records.getAllDerivedDefinitions("PatFrags");
3131 // First step, parse all of the fragments.
3132 for (Record *Frag : Fragments) {
3133 if (OutFrags != Frag->isSubClassOf("OutPatFrag"))
3136 ListInit *LI = Frag->getValueAsListInit("Fragments");
3138 (PatternFragments[Frag] = std::make_unique<TreePattern>(
3139 Frag, LI, !Frag->isSubClassOf("OutPatFrag"),
3142 // Validate the argument list, converting it to set, to discard duplicates.
3143 std::vector<std::string> &Args = P->getArgList();
3144 // Copy the args so we can take StringRefs to them.
3145 auto ArgsCopy = Args;
3146 SmallDenseSet<StringRef, 4> OperandsSet;
3147 OperandsSet.insert(ArgsCopy.begin(), ArgsCopy.end());
3149 if (OperandsSet.count(""))
3150 P->error("Cannot have unnamed 'node' values in pattern fragment!");
3152 // Parse the operands list.
3153 DagInit *OpsList = Frag->getValueAsDag("Operands");
3154 DefInit *OpsOp = dyn_cast<DefInit>(OpsList->getOperator());
3155 // Special cases: ops == outs == ins. Different names are used to
3156 // improve readability.
3158 (OpsOp->getDef()->getName() != "ops" &&
3159 OpsOp->getDef()->getName() != "outs" &&
3160 OpsOp->getDef()->getName() != "ins"))
3161 P->error("Operands list should start with '(ops ... '!");
3163 // Copy over the arguments.
3165 for (unsigned j = 0, e = OpsList->getNumArgs(); j != e; ++j) {
3166 if (!isa<DefInit>(OpsList->getArg(j)) ||
3167 cast<DefInit>(OpsList->getArg(j))->getDef()->getName() != "node")
3168 P->error("Operands list should all be 'node' values.");
3169 if (!OpsList->getArgName(j))
3170 P->error("Operands list should have names for each operand!");
3171 StringRef ArgNameStr = OpsList->getArgNameStr(j);
3172 if (!OperandsSet.count(ArgNameStr))
3173 P->error("'" + ArgNameStr +
3174 "' does not occur in pattern or was multiply specified!");
3175 OperandsSet.erase(ArgNameStr);
3176 Args.push_back(ArgNameStr);
3179 if (!OperandsSet.empty())
3180 P->error("Operands list does not contain an entry for operand '" +
3181 *OperandsSet.begin() + "'!");
3183 // If there is a node transformation corresponding to this, keep track of
3185 Record *Transform = Frag->getValueAsDef("OperandTransform");
3186 if (!getSDNodeTransform(Transform).second.empty()) // not noop xform?
3187 for (auto T : P->getTrees())
3188 T->setTransformFn(Transform);
3191 // Now that we've parsed all of the tree fragments, do a closure on them so
3192 // that there are not references to PatFrags left inside of them.
3193 for (Record *Frag : Fragments) {
3194 if (OutFrags != Frag->isSubClassOf("OutPatFrag"))
3197 TreePattern &ThePat = *PatternFragments[Frag];
3198 ThePat.InlinePatternFragments();
3200 // Infer as many types as possible. Don't worry about it if we don't infer
3201 // all of them, some may depend on the inputs of the pattern. Also, don't
3202 // validate type sets; validation may cause spurious failures e.g. if a
3203 // fragment needs floating-point types but the current target does not have
3204 // any (this is only an error if that fragment is ever used!).
3206 TypeInfer::SuppressValidation SV(ThePat.getInfer());
3207 ThePat.InferAllTypes();
3208 ThePat.resetError();
3211 // If debugging, print out the pattern fragment result.
3212 LLVM_DEBUG(ThePat.dump());
3216 void CodeGenDAGPatterns::ParseDefaultOperands() {
3217 std::vector<Record*> DefaultOps;
3218 DefaultOps = Records.getAllDerivedDefinitions("OperandWithDefaultOps");
3220 // Find some SDNode.
3221 assert(!SDNodes.empty() && "No SDNodes parsed?");
3222 Init *SomeSDNode = DefInit::get(SDNodes.begin()->first);
3224 for (unsigned i = 0, e = DefaultOps.size(); i != e; ++i) {
3225 DagInit *DefaultInfo = DefaultOps[i]->getValueAsDag("DefaultOps");
3227 // Clone the DefaultInfo dag node, changing the operator from 'ops' to
3228 // SomeSDnode so that we can parse this.
3229 std::vector<std::pair<Init*, StringInit*> > Ops;
3230 for (unsigned op = 0, e = DefaultInfo->getNumArgs(); op != e; ++op)
3231 Ops.push_back(std::make_pair(DefaultInfo->getArg(op),
3232 DefaultInfo->getArgName(op)));
3233 DagInit *DI = DagInit::get(SomeSDNode, nullptr, Ops);
3235 // Create a TreePattern to parse this.
3236 TreePattern P(DefaultOps[i], DI, false, *this);
3237 assert(P.getNumTrees() == 1 && "This ctor can only produce one tree!");
3239 // Copy the operands over into a DAGDefaultOperand.
3240 DAGDefaultOperand DefaultOpInfo;
3242 const TreePatternNodePtr &T = P.getTree(0);
3243 for (unsigned op = 0, e = T->getNumChildren(); op != e; ++op) {
3244 TreePatternNodePtr TPN = T->getChildShared(op);
3245 while (TPN->ApplyTypeConstraints(P, false))
3246 /* Resolve all types */;
3248 if (TPN->ContainsUnresolvedType(P)) {
3249 PrintFatalError("Value #" + Twine(i) + " of OperandWithDefaultOps '" +
3250 DefaultOps[i]->getName() +
3251 "' doesn't have a concrete type!");
3253 DefaultOpInfo.DefaultOps.push_back(std::move(TPN));
3256 // Insert it into the DefaultOperands map so we can find it later.
3257 DefaultOperands[DefaultOps[i]] = DefaultOpInfo;
3261 /// HandleUse - Given "Pat" a leaf in the pattern, check to see if it is an
3262 /// instruction input. Return true if this is a real use.
3263 static bool HandleUse(TreePattern &I, TreePatternNodePtr Pat,
3264 std::map<std::string, TreePatternNodePtr> &InstInputs) {
3265 // No name -> not interesting.
3266 if (Pat->getName().empty()) {
3267 if (Pat->isLeaf()) {
3268 DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue());
3269 if (DI && (DI->getDef()->isSubClassOf("RegisterClass") ||
3270 DI->getDef()->isSubClassOf("RegisterOperand")))
3271 I.error("Input " + DI->getDef()->getName() + " must be named!");
3277 if (Pat->isLeaf()) {
3278 DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue());
3280 I.error("Input $" + Pat->getName() + " must be an identifier!");
3283 Rec = Pat->getOperator();
3286 // SRCVALUE nodes are ignored.
3287 if (Rec->getName() == "srcvalue")
3290 TreePatternNodePtr &Slot = InstInputs[Pat->getName()];
3296 if (Slot->isLeaf()) {
3297 SlotRec = cast<DefInit>(Slot->getLeafValue())->getDef();
3299 assert(Slot->getNumChildren() == 0 && "can't be a use with children!");
3300 SlotRec = Slot->getOperator();
3303 // Ensure that the inputs agree if we've already seen this input.
3305 I.error("All $" + Pat->getName() + " inputs must agree with each other");
3306 // Ensure that the types can agree as well.
3307 Slot->UpdateNodeType(0, Pat->getExtType(0), I);
3308 Pat->UpdateNodeType(0, Slot->getExtType(0), I);
3309 if (Slot->getExtTypes() != Pat->getExtTypes())
3310 I.error("All $" + Pat->getName() + " inputs must agree with each other");
3314 /// FindPatternInputsAndOutputs - Scan the specified TreePatternNode (which is
3315 /// part of "I", the instruction), computing the set of inputs and outputs of
3316 /// the pattern. Report errors if we see anything naughty.
3317 void CodeGenDAGPatterns::FindPatternInputsAndOutputs(
3318 TreePattern &I, TreePatternNodePtr Pat,
3319 std::map<std::string, TreePatternNodePtr> &InstInputs,
3320 MapVector<std::string, TreePatternNodePtr, std::map<std::string, unsigned>>
3322 std::vector<Record *> &InstImpResults) {
3324 // The instruction pattern still has unresolved fragments. For *named*
3325 // nodes we must resolve those here. This may not result in multiple
3327 if (!Pat->getName().empty()) {
3328 TreePattern SrcPattern(I.getRecord(), Pat, true, *this);
3329 SrcPattern.InlinePatternFragments();
3330 SrcPattern.InferAllTypes();
3331 Pat = SrcPattern.getOnlyTree();
3334 if (Pat->isLeaf()) {
3335 bool isUse = HandleUse(I, Pat, InstInputs);
3336 if (!isUse && Pat->getTransformFn())
3337 I.error("Cannot specify a transform function for a non-input value!");
3341 if (Pat->getOperator()->getName() == "implicit") {
3342 for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) {
3343 TreePatternNode *Dest = Pat->getChild(i);
3344 if (!Dest->isLeaf())
3345 I.error("implicitly defined value should be a register!");
3347 DefInit *Val = dyn_cast<DefInit>(Dest->getLeafValue());
3348 if (!Val || !Val->getDef()->isSubClassOf("Register"))
3349 I.error("implicitly defined value should be a register!");
3350 InstImpResults.push_back(Val->getDef());
3355 if (Pat->getOperator()->getName() != "set") {
3356 // If this is not a set, verify that the children nodes are not void typed,
3358 for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) {
3359 if (Pat->getChild(i)->getNumTypes() == 0)
3360 I.error("Cannot have void nodes inside of patterns!");
3361 FindPatternInputsAndOutputs(I, Pat->getChildShared(i), InstInputs,
3362 InstResults, InstImpResults);
3365 // If this is a non-leaf node with no children, treat it basically as if
3366 // it were a leaf. This handles nodes like (imm).
3367 bool isUse = HandleUse(I, Pat, InstInputs);
3369 if (!isUse && Pat->getTransformFn())
3370 I.error("Cannot specify a transform function for a non-input value!");
3374 // Otherwise, this is a set, validate and collect instruction results.
3375 if (Pat->getNumChildren() == 0)
3376 I.error("set requires operands!");
3378 if (Pat->getTransformFn())
3379 I.error("Cannot specify a transform function on a set node!");
3381 // Check the set destinations.
3382 unsigned NumDests = Pat->getNumChildren()-1;
3383 for (unsigned i = 0; i != NumDests; ++i) {
3384 TreePatternNodePtr Dest = Pat->getChildShared(i);
3385 // For set destinations we also must resolve fragments here.
3386 TreePattern DestPattern(I.getRecord(), Dest, false, *this);
3387 DestPattern.InlinePatternFragments();
3388 DestPattern.InferAllTypes();
3389 Dest = DestPattern.getOnlyTree();
3391 if (!Dest->isLeaf())
3392 I.error("set destination should be a register!");
3394 DefInit *Val = dyn_cast<DefInit>(Dest->getLeafValue());
3396 I.error("set destination should be a register!");
3400 if (Val->getDef()->isSubClassOf("RegisterClass") ||
3401 Val->getDef()->isSubClassOf("ValueType") ||
3402 Val->getDef()->isSubClassOf("RegisterOperand") ||
3403 Val->getDef()->isSubClassOf("PointerLikeRegClass")) {
3404 if (Dest->getName().empty())
3405 I.error("set destination must have a name!");
3406 if (InstResults.count(Dest->getName()))
3407 I.error("cannot set '" + Dest->getName() + "' multiple times");
3408 InstResults[Dest->getName()] = Dest;
3409 } else if (Val->getDef()->isSubClassOf("Register")) {
3410 InstImpResults.push_back(Val->getDef());
3412 I.error("set destination should be a register!");
3416 // Verify and collect info from the computation.
3417 FindPatternInputsAndOutputs(I, Pat->getChildShared(NumDests), InstInputs,
3418 InstResults, InstImpResults);
3421 //===----------------------------------------------------------------------===//
3422 // Instruction Analysis
3423 //===----------------------------------------------------------------------===//
3425 class InstAnalyzer {
3426 const CodeGenDAGPatterns &CDP;
3428 bool hasSideEffects;
3435 InstAnalyzer(const CodeGenDAGPatterns &cdp)
3436 : CDP(cdp), hasSideEffects(false), mayStore(false), mayLoad(false),
3437 isBitcast(false), isVariadic(false), hasChain(false) {}
3439 void Analyze(const PatternToMatch &Pat) {
3440 const TreePatternNode *N = Pat.getSrcPattern();
3442 // These properties are detected only on the root node.
3443 isBitcast = IsNodeBitcast(N);
3447 bool IsNodeBitcast(const TreePatternNode *N) const {
3448 if (hasSideEffects || mayLoad || mayStore || isVariadic)
3453 if (N->getNumChildren() != 1 || !N->getChild(0)->isLeaf())
3456 const SDNodeInfo &OpInfo = CDP.getSDNodeInfo(N->getOperator());
3457 if (OpInfo.getNumResults() != 1 || OpInfo.getNumOperands() != 1)
3459 return OpInfo.getEnumName() == "ISD::BITCAST";
3463 void AnalyzeNode(const TreePatternNode *N) {
3465 if (DefInit *DI = dyn_cast<DefInit>(N->getLeafValue())) {
3466 Record *LeafRec = DI->getDef();
3467 // Handle ComplexPattern leaves.
3468 if (LeafRec->isSubClassOf("ComplexPattern")) {
3469 const ComplexPattern &CP = CDP.getComplexPattern(LeafRec);
3470 if (CP.hasProperty(SDNPMayStore)) mayStore = true;
3471 if (CP.hasProperty(SDNPMayLoad)) mayLoad = true;
3472 if (CP.hasProperty(SDNPSideEffect)) hasSideEffects = true;
3478 // Analyze children.
3479 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
3480 AnalyzeNode(N->getChild(i));
3482 // Notice properties of the node.
3483 if (N->NodeHasProperty(SDNPMayStore, CDP)) mayStore = true;
3484 if (N->NodeHasProperty(SDNPMayLoad, CDP)) mayLoad = true;
3485 if (N->NodeHasProperty(SDNPSideEffect, CDP)) hasSideEffects = true;
3486 if (N->NodeHasProperty(SDNPVariadic, CDP)) isVariadic = true;
3487 if (N->NodeHasProperty(SDNPHasChain, CDP)) hasChain = true;
3489 if (const CodeGenIntrinsic *IntInfo = N->getIntrinsicInfo(CDP)) {
3490 // If this is an intrinsic, analyze it.
3491 if (IntInfo->ModRef & CodeGenIntrinsic::MR_Ref)
3492 mayLoad = true;// These may load memory.
3494 if (IntInfo->ModRef & CodeGenIntrinsic::MR_Mod)
3495 mayStore = true;// Intrinsics that can write to memory are 'mayStore'.
3497 if (IntInfo->ModRef >= CodeGenIntrinsic::ReadWriteMem ||
3498 IntInfo->hasSideEffects)
3499 // ReadWriteMem intrinsics can have other strange effects.
3500 hasSideEffects = true;
3506 static bool InferFromPattern(CodeGenInstruction &InstInfo,
3507 const InstAnalyzer &PatInfo,
3511 // Remember where InstInfo got its flags.
3512 if (InstInfo.hasUndefFlags())
3513 InstInfo.InferredFrom = PatDef;
3515 // Check explicitly set flags for consistency.
3516 if (InstInfo.hasSideEffects != PatInfo.hasSideEffects &&
3517 !InstInfo.hasSideEffects_Unset) {
3518 // Allow explicitly setting hasSideEffects = 1 on instructions, even when
3519 // the pattern has no side effects. That could be useful for div/rem
3520 // instructions that may trap.
3521 if (!InstInfo.hasSideEffects) {
3523 PrintError(PatDef->getLoc(), "Pattern doesn't match hasSideEffects = " +
3524 Twine(InstInfo.hasSideEffects));
3528 if (InstInfo.mayStore != PatInfo.mayStore && !InstInfo.mayStore_Unset) {
3530 PrintError(PatDef->getLoc(), "Pattern doesn't match mayStore = " +
3531 Twine(InstInfo.mayStore));
3534 if (InstInfo.mayLoad != PatInfo.mayLoad && !InstInfo.mayLoad_Unset) {
3535 // Allow explicitly setting mayLoad = 1, even when the pattern has no loads.
3536 // Some targets translate immediates to loads.
3537 if (!InstInfo.mayLoad) {
3539 PrintError(PatDef->getLoc(), "Pattern doesn't match mayLoad = " +
3540 Twine(InstInfo.mayLoad));
3544 // Transfer inferred flags.
3545 InstInfo.hasSideEffects |= PatInfo.hasSideEffects;
3546 InstInfo.mayStore |= PatInfo.mayStore;
3547 InstInfo.mayLoad |= PatInfo.mayLoad;
3549 // These flags are silently added without any verification.
3550 // FIXME: To match historical behavior of TableGen, for now add those flags
3551 // only when we're inferring from the primary instruction pattern.
3552 if (PatDef->isSubClassOf("Instruction")) {
3553 InstInfo.isBitcast |= PatInfo.isBitcast;
3554 InstInfo.hasChain |= PatInfo.hasChain;
3555 InstInfo.hasChain_Inferred = true;
3558 // Don't infer isVariadic. This flag means something different on SDNodes and
3559 // instructions. For example, a CALL SDNode is variadic because it has the
3560 // call arguments as operands, but a CALL instruction is not variadic - it
3561 // has argument registers as implicit, not explicit uses.
3566 /// hasNullFragReference - Return true if the DAG has any reference to the
3567 /// null_frag operator.
3568 static bool hasNullFragReference(DagInit *DI) {
3569 DefInit *OpDef = dyn_cast<DefInit>(DI->getOperator());
3570 if (!OpDef) return false;
3571 Record *Operator = OpDef->getDef();
3573 // If this is the null fragment, return true.
3574 if (Operator->getName() == "null_frag") return true;
3575 // If any of the arguments reference the null fragment, return true.
3576 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
3577 DagInit *Arg = dyn_cast<DagInit>(DI->getArg(i));
3578 if (Arg && hasNullFragReference(Arg))
3585 /// hasNullFragReference - Return true if any DAG in the list references
3586 /// the null_frag operator.
3587 static bool hasNullFragReference(ListInit *LI) {
3588 for (Init *I : LI->getValues()) {
3589 DagInit *DI = dyn_cast<DagInit>(I);
3590 assert(DI && "non-dag in an instruction Pattern list?!");
3591 if (hasNullFragReference(DI))
3597 /// Get all the instructions in a tree.
3599 getInstructionsInTree(TreePatternNode *Tree, SmallVectorImpl<Record*> &Instrs) {
3602 if (Tree->getOperator()->isSubClassOf("Instruction"))
3603 Instrs.push_back(Tree->getOperator());
3604 for (unsigned i = 0, e = Tree->getNumChildren(); i != e; ++i)
3605 getInstructionsInTree(Tree->getChild(i), Instrs);
3608 /// Check the class of a pattern leaf node against the instruction operand it
3610 static bool checkOperandClass(CGIOperandList::OperandInfo &OI,
3615 // Allow direct value types to be used in instruction set patterns.
3616 // The type will be checked later.
3617 if (Leaf->isSubClassOf("ValueType"))
3620 // Patterns can also be ComplexPattern instances.
3621 if (Leaf->isSubClassOf("ComplexPattern"))
3627 void CodeGenDAGPatterns::parseInstructionPattern(
3628 CodeGenInstruction &CGI, ListInit *Pat, DAGInstMap &DAGInsts) {
3630 assert(!DAGInsts.count(CGI.TheDef) && "Instruction already parsed!");
3632 // Parse the instruction.
3633 TreePattern I(CGI.TheDef, Pat, true, *this);
3635 // InstInputs - Keep track of all of the inputs of the instruction, along
3636 // with the record they are declared as.
3637 std::map<std::string, TreePatternNodePtr> InstInputs;
3639 // InstResults - Keep track of all the virtual registers that are 'set'
3640 // in the instruction, including what reg class they are.
3641 MapVector<std::string, TreePatternNodePtr, std::map<std::string, unsigned>>
3644 std::vector<Record*> InstImpResults;
3646 // Verify that the top-level forms in the instruction are of void type, and
3647 // fill in the InstResults map.
3648 SmallString<32> TypesString;
3649 for (unsigned j = 0, e = I.getNumTrees(); j != e; ++j) {
3650 TypesString.clear();
3651 TreePatternNodePtr Pat = I.getTree(j);
3652 if (Pat->getNumTypes() != 0) {
3653 raw_svector_ostream OS(TypesString);
3654 for (unsigned k = 0, ke = Pat->getNumTypes(); k != ke; ++k) {
3657 Pat->getExtType(k).writeToStream(OS);
3659 I.error("Top-level forms in instruction pattern should have"
3660 " void types, has types " +
3664 // Find inputs and outputs, and verify the structure of the uses/defs.
3665 FindPatternInputsAndOutputs(I, Pat, InstInputs, InstResults,
3669 // Now that we have inputs and outputs of the pattern, inspect the operands
3670 // list for the instruction. This determines the order that operands are
3671 // added to the machine instruction the node corresponds to.
3672 unsigned NumResults = InstResults.size();
3674 // Parse the operands list from the (ops) list, validating it.
3675 assert(I.getArgList().empty() && "Args list should still be empty here!");
3677 // Check that all of the results occur first in the list.
3678 std::vector<Record*> Results;
3679 std::vector<unsigned> ResultIndices;
3680 SmallVector<TreePatternNodePtr, 2> ResNodes;
3681 for (unsigned i = 0; i != NumResults; ++i) {
3682 if (i == CGI.Operands.size()) {
3683 const std::string &OpName =
3684 std::find_if(InstResults.begin(), InstResults.end(),
3685 [](const std::pair<std::string, TreePatternNodePtr> &P) {
3690 I.error("'" + OpName + "' set but does not appear in operand list!");
3693 const std::string &OpName = CGI.Operands[i].Name;
3695 // Check that it exists in InstResults.
3696 auto InstResultIter = InstResults.find(OpName);
3697 if (InstResultIter == InstResults.end() || !InstResultIter->second)
3698 I.error("Operand $" + OpName + " does not exist in operand list!");
3700 TreePatternNodePtr RNode = InstResultIter->second;
3701 Record *R = cast<DefInit>(RNode->getLeafValue())->getDef();
3702 ResNodes.push_back(std::move(RNode));
3704 I.error("Operand $" + OpName + " should be a set destination: all "
3705 "outputs must occur before inputs in operand list!");
3707 if (!checkOperandClass(CGI.Operands[i], R))
3708 I.error("Operand $" + OpName + " class mismatch!");
3710 // Remember the return type.
3711 Results.push_back(CGI.Operands[i].Rec);
3713 // Remember the result index.
3714 ResultIndices.push_back(std::distance(InstResults.begin(), InstResultIter));
3716 // Okay, this one checks out.
3717 InstResultIter->second = nullptr;
3720 // Loop over the inputs next.
3721 std::vector<TreePatternNodePtr> ResultNodeOperands;
3722 std::vector<Record*> Operands;
3723 for (unsigned i = NumResults, e = CGI.Operands.size(); i != e; ++i) {
3724 CGIOperandList::OperandInfo &Op = CGI.Operands[i];
3725 const std::string &OpName = Op.Name;
3727 I.error("Operand #" + Twine(i) + " in operands list has no name!");
3729 if (!InstInputs.count(OpName)) {
3730 // If this is an operand with a DefaultOps set filled in, we can ignore
3731 // this. When we codegen it, we will do so as always executed.
3732 if (Op.Rec->isSubClassOf("OperandWithDefaultOps")) {
3733 // Does it have a non-empty DefaultOps field? If so, ignore this
3735 if (!getDefaultOperand(Op.Rec).DefaultOps.empty())
3738 I.error("Operand $" + OpName +
3739 " does not appear in the instruction pattern");
3741 TreePatternNodePtr InVal = InstInputs[OpName];
3742 InstInputs.erase(OpName); // It occurred, remove from map.
3744 if (InVal->isLeaf() && isa<DefInit>(InVal->getLeafValue())) {
3745 Record *InRec = static_cast<DefInit*>(InVal->getLeafValue())->getDef();
3746 if (!checkOperandClass(Op, InRec))
3747 I.error("Operand $" + OpName + "'s register class disagrees"
3748 " between the operand and pattern");
3750 Operands.push_back(Op.Rec);
3752 // Construct the result for the dest-pattern operand list.
3753 TreePatternNodePtr OpNode = InVal->clone();
3755 // No predicate is useful on the result.
3756 OpNode->clearPredicateCalls();
3758 // Promote the xform function to be an explicit node if set.
3759 if (Record *Xform = OpNode->getTransformFn()) {
3760 OpNode->setTransformFn(nullptr);
3761 std::vector<TreePatternNodePtr> Children;
3762 Children.push_back(OpNode);
3763 OpNode = std::make_shared<TreePatternNode>(Xform, std::move(Children),
3764 OpNode->getNumTypes());
3767 ResultNodeOperands.push_back(std::move(OpNode));
3770 if (!InstInputs.empty())
3771 I.error("Input operand $" + InstInputs.begin()->first +
3772 " occurs in pattern but not in operands list!");
3774 TreePatternNodePtr ResultPattern = std::make_shared<TreePatternNode>(
3775 I.getRecord(), std::move(ResultNodeOperands),
3776 GetNumNodeResults(I.getRecord(), *this));
3777 // Copy fully inferred output node types to instruction result pattern.
3778 for (unsigned i = 0; i != NumResults; ++i) {
3779 assert(ResNodes[i]->getNumTypes() == 1 && "FIXME: Unhandled");
3780 ResultPattern->setType(i, ResNodes[i]->getExtType(0));
3781 ResultPattern->setResultIndex(i, ResultIndices[i]);
3784 // FIXME: Assume only the first tree is the pattern. The others are clobber
3786 TreePatternNodePtr Pattern = I.getTree(0);
3787 TreePatternNodePtr SrcPattern;
3788 if (Pattern->getOperator()->getName() == "set") {
3789 SrcPattern = Pattern->getChild(Pattern->getNumChildren()-1)->clone();
3791 // Not a set (store or something?)
3792 SrcPattern = Pattern;
3795 // Create and insert the instruction.
3796 // FIXME: InstImpResults should not be part of DAGInstruction.
3797 Record *R = I.getRecord();
3798 DAGInsts.emplace(std::piecewise_construct, std::forward_as_tuple(R),
3799 std::forward_as_tuple(Results, Operands, InstImpResults,
3800 SrcPattern, ResultPattern));
3802 LLVM_DEBUG(I.dump());
3805 /// ParseInstructions - Parse all of the instructions, inlining and resolving
3806 /// any fragments involved. This populates the Instructions list with fully
3807 /// resolved instructions.
3808 void CodeGenDAGPatterns::ParseInstructions() {
3809 std::vector<Record*> Instrs = Records.getAllDerivedDefinitions("Instruction");
3811 for (Record *Instr : Instrs) {
3812 ListInit *LI = nullptr;
3814 if (isa<ListInit>(Instr->getValueInit("Pattern")))
3815 LI = Instr->getValueAsListInit("Pattern");
3817 // If there is no pattern, only collect minimal information about the
3818 // instruction for its operand list. We have to assume that there is one
3819 // result, as we have no detailed info. A pattern which references the
3820 // null_frag operator is as-if no pattern were specified. Normally this
3821 // is from a multiclass expansion w/ a SDPatternOperator passed in as
3823 if (!LI || LI->empty() || hasNullFragReference(LI)) {
3824 std::vector<Record*> Results;
3825 std::vector<Record*> Operands;
3827 CodeGenInstruction &InstInfo = Target.getInstruction(Instr);
3829 if (InstInfo.Operands.size() != 0) {
3830 for (unsigned j = 0, e = InstInfo.Operands.NumDefs; j < e; ++j)
3831 Results.push_back(InstInfo.Operands[j].Rec);
3833 // The rest are inputs.
3834 for (unsigned j = InstInfo.Operands.NumDefs,
3835 e = InstInfo.Operands.size(); j < e; ++j)
3836 Operands.push_back(InstInfo.Operands[j].Rec);
3839 // Create and insert the instruction.
3840 std::vector<Record*> ImpResults;
3841 Instructions.insert(std::make_pair(Instr,
3842 DAGInstruction(Results, Operands, ImpResults)));
3843 continue; // no pattern.
3846 CodeGenInstruction &CGI = Target.getInstruction(Instr);
3847 parseInstructionPattern(CGI, LI, Instructions);
3850 // If we can, convert the instructions to be patterns that are matched!
3851 for (auto &Entry : Instructions) {
3852 Record *Instr = Entry.first;
3853 DAGInstruction &TheInst = Entry.second;
3854 TreePatternNodePtr SrcPattern = TheInst.getSrcPattern();
3855 TreePatternNodePtr ResultPattern = TheInst.getResultPattern();
3857 if (SrcPattern && ResultPattern) {
3858 TreePattern Pattern(Instr, SrcPattern, true, *this);
3859 TreePattern Result(Instr, ResultPattern, false, *this);
3860 ParseOnePattern(Instr, Pattern, Result, TheInst.getImpResults());
3865 typedef std::pair<TreePatternNode *, unsigned> NameRecord;
3867 static void FindNames(TreePatternNode *P,
3868 std::map<std::string, NameRecord> &Names,
3869 TreePattern *PatternTop) {
3870 if (!P->getName().empty()) {
3871 NameRecord &Rec = Names[P->getName()];
3872 // If this is the first instance of the name, remember the node.
3873 if (Rec.second++ == 0)
3875 else if (Rec.first->getExtTypes() != P->getExtTypes())
3876 PatternTop->error("repetition of value: $" + P->getName() +
3877 " where different uses have different types!");
3881 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
3882 FindNames(P->getChild(i), Names, PatternTop);
3886 std::vector<Predicate> CodeGenDAGPatterns::makePredList(ListInit *L) {
3887 std::vector<Predicate> Preds;
3888 for (Init *I : L->getValues()) {
3889 if (DefInit *Pred = dyn_cast<DefInit>(I))
3890 Preds.push_back(Pred->getDef());
3892 llvm_unreachable("Non-def on the list");
3895 // Sort so that different orders get canonicalized to the same string.
3900 void CodeGenDAGPatterns::AddPatternToMatch(TreePattern *Pattern,
3901 PatternToMatch &&PTM) {
3902 // Do some sanity checking on the pattern we're about to match.
3904 if (!PTM.getSrcPattern()->canPatternMatch(Reason, *this)) {
3905 PrintWarning(Pattern->getRecord()->getLoc(),
3906 Twine("Pattern can never match: ") + Reason);
3910 // If the source pattern's root is a complex pattern, that complex pattern
3911 // must specify the nodes it can potentially match.
3912 if (const ComplexPattern *CP =
3913 PTM.getSrcPattern()->getComplexPatternInfo(*this))
3914 if (CP->getRootNodes().empty())
3915 Pattern->error("ComplexPattern at root must specify list of opcodes it"
3919 // Find all of the named values in the input and output, ensure they have the
3921 std::map<std::string, NameRecord> SrcNames, DstNames;
3922 FindNames(PTM.getSrcPattern(), SrcNames, Pattern);
3923 FindNames(PTM.getDstPattern(), DstNames, Pattern);
3925 // Scan all of the named values in the destination pattern, rejecting them if
3926 // they don't exist in the input pattern.
3927 for (const auto &Entry : DstNames) {
3928 if (SrcNames[Entry.first].first == nullptr)
3929 Pattern->error("Pattern has input without matching name in output: $" +
3933 // Scan all of the named values in the source pattern, rejecting them if the
3934 // name isn't used in the dest, and isn't used to tie two values together.
3935 for (const auto &Entry : SrcNames)
3936 if (DstNames[Entry.first].first == nullptr &&
3937 SrcNames[Entry.first].second == 1)
3938 Pattern->error("Pattern has dead named input: $" + Entry.first);
3940 PatternsToMatch.push_back(PTM);
3943 void CodeGenDAGPatterns::InferInstructionFlags() {
3944 ArrayRef<const CodeGenInstruction*> Instructions =
3945 Target.getInstructionsByEnumValue();
3947 unsigned Errors = 0;
3949 // Try to infer flags from all patterns in PatternToMatch. These include
3950 // both the primary instruction patterns (which always come first) and
3951 // patterns defined outside the instruction.
3952 for (const PatternToMatch &PTM : ptms()) {
3953 // We can only infer from single-instruction patterns, otherwise we won't
3954 // know which instruction should get the flags.
3955 SmallVector<Record*, 8> PatInstrs;
3956 getInstructionsInTree(PTM.getDstPattern(), PatInstrs);
3957 if (PatInstrs.size() != 1)
3960 // Get the single instruction.
3961 CodeGenInstruction &InstInfo = Target.getInstruction(PatInstrs.front());
3963 // Only infer properties from the first pattern. We'll verify the others.
3964 if (InstInfo.InferredFrom)
3967 InstAnalyzer PatInfo(*this);
3968 PatInfo.Analyze(PTM);
3969 Errors += InferFromPattern(InstInfo, PatInfo, PTM.getSrcRecord());
3973 PrintFatalError("pattern conflicts");
3975 // If requested by the target, guess any undefined properties.
3976 if (Target.guessInstructionProperties()) {
3977 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
3978 CodeGenInstruction *InstInfo =
3979 const_cast<CodeGenInstruction *>(Instructions[i]);
3980 if (InstInfo->InferredFrom)
3982 // The mayLoad and mayStore flags default to false.
3983 // Conservatively assume hasSideEffects if it wasn't explicit.
3984 if (InstInfo->hasSideEffects_Unset)
3985 InstInfo->hasSideEffects = true;
3990 // Complain about any flags that are still undefined.
3991 for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
3992 CodeGenInstruction *InstInfo =
3993 const_cast<CodeGenInstruction *>(Instructions[i]);
3994 if (InstInfo->InferredFrom)
3996 if (InstInfo->hasSideEffects_Unset)
3997 PrintError(InstInfo->TheDef->getLoc(),
3998 "Can't infer hasSideEffects from patterns");
3999 if (InstInfo->mayStore_Unset)
4000 PrintError(InstInfo->TheDef->getLoc(),
4001 "Can't infer mayStore from patterns");
4002 if (InstInfo->mayLoad_Unset)
4003 PrintError(InstInfo->TheDef->getLoc(),
4004 "Can't infer mayLoad from patterns");
4009 /// Verify instruction flags against pattern node properties.
4010 void CodeGenDAGPatterns::VerifyInstructionFlags() {
4011 unsigned Errors = 0;
4012 for (ptm_iterator I = ptm_begin(), E = ptm_end(); I != E; ++I) {
4013 const PatternToMatch &PTM = *I;
4014 SmallVector<Record*, 8> Instrs;
4015 getInstructionsInTree(PTM.getDstPattern(), Instrs);
4019 // Count the number of instructions with each flag set.
4020 unsigned NumSideEffects = 0;
4021 unsigned NumStores = 0;
4022 unsigned NumLoads = 0;
4023 for (const Record *Instr : Instrs) {
4024 const CodeGenInstruction &InstInfo = Target.getInstruction(Instr);
4025 NumSideEffects += InstInfo.hasSideEffects;
4026 NumStores += InstInfo.mayStore;
4027 NumLoads += InstInfo.mayLoad;
4030 // Analyze the source pattern.
4031 InstAnalyzer PatInfo(*this);
4032 PatInfo.Analyze(PTM);
4034 // Collect error messages.
4035 SmallVector<std::string, 4> Msgs;
4037 // Check for missing flags in the output.
4038 // Permit extra flags for now at least.
4039 if (PatInfo.hasSideEffects && !NumSideEffects)
4040 Msgs.push_back("pattern has side effects, but hasSideEffects isn't set");
4042 // Don't verify store flags on instructions with side effects. At least for
4043 // intrinsics, side effects implies mayStore.
4044 if (!PatInfo.hasSideEffects && PatInfo.mayStore && !NumStores)
4045 Msgs.push_back("pattern may store, but mayStore isn't set");
4047 // Similarly, mayStore implies mayLoad on intrinsics.
4048 if (!PatInfo.mayStore && PatInfo.mayLoad && !NumLoads)
4049 Msgs.push_back("pattern may load, but mayLoad isn't set");
4051 // Print error messages.
4056 for (const std::string &Msg : Msgs)
4057 PrintError(PTM.getSrcRecord()->getLoc(), Twine(Msg) + " on the " +
4058 (Instrs.size() == 1 ?
4059 "instruction" : "output instructions"));
4060 // Provide the location of the relevant instruction definitions.
4061 for (const Record *Instr : Instrs) {
4062 if (Instr != PTM.getSrcRecord())
4063 PrintError(Instr->getLoc(), "defined here");
4064 const CodeGenInstruction &InstInfo = Target.getInstruction(Instr);
4065 if (InstInfo.InferredFrom &&
4066 InstInfo.InferredFrom != InstInfo.TheDef &&
4067 InstInfo.InferredFrom != PTM.getSrcRecord())
4068 PrintError(InstInfo.InferredFrom->getLoc(), "inferred from pattern");
4072 PrintFatalError("Errors in DAG patterns");
4075 /// Given a pattern result with an unresolved type, see if we can find one
4076 /// instruction with an unresolved result type. Force this result type to an
4077 /// arbitrary element if it's possible types to converge results.
4078 static bool ForceArbitraryInstResultType(TreePatternNode *N, TreePattern &TP) {
4082 // Analyze children.
4083 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
4084 if (ForceArbitraryInstResultType(N->getChild(i), TP))
4087 if (!N->getOperator()->isSubClassOf("Instruction"))
4090 // If this type is already concrete or completely unknown we can't do
4092 TypeInfer &TI = TP.getInfer();
4093 for (unsigned i = 0, e = N->getNumTypes(); i != e; ++i) {
4094 if (N->getExtType(i).empty() || TI.isConcrete(N->getExtType(i), false))
4097 // Otherwise, force its type to an arbitrary choice.
4098 if (TI.forceArbitrary(N->getExtType(i)))
4105 // Promote xform function to be an explicit node wherever set.
4106 static TreePatternNodePtr PromoteXForms(TreePatternNodePtr N) {
4107 if (Record *Xform = N->getTransformFn()) {
4108 N->setTransformFn(nullptr);
4109 std::vector<TreePatternNodePtr> Children;
4110 Children.push_back(PromoteXForms(N));
4111 return std::make_shared<TreePatternNode>(Xform, std::move(Children),
4116 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
4117 TreePatternNodePtr Child = N->getChildShared(i);
4118 N->setChild(i, PromoteXForms(Child));
4123 void CodeGenDAGPatterns::ParseOnePattern(Record *TheDef,
4124 TreePattern &Pattern, TreePattern &Result,
4125 const std::vector<Record *> &InstImpResults) {
4127 // Inline pattern fragments and expand multiple alternatives.
4128 Pattern.InlinePatternFragments();
4129 Result.InlinePatternFragments();
4131 if (Result.getNumTrees() != 1)
4132 Result.error("Cannot use multi-alternative fragments in result pattern!");
4135 bool IterateInference;
4136 bool InferredAllPatternTypes, InferredAllResultTypes;
4138 // Infer as many types as possible. If we cannot infer all of them, we
4139 // can never do anything with this pattern: report it to the user.
4140 InferredAllPatternTypes =
4141 Pattern.InferAllTypes(&Pattern.getNamedNodesMap());
4143 // Infer as many types as possible. If we cannot infer all of them, we
4144 // can never do anything with this pattern: report it to the user.
4145 InferredAllResultTypes =
4146 Result.InferAllTypes(&Pattern.getNamedNodesMap());
4148 IterateInference = false;
4150 // Apply the type of the result to the source pattern. This helps us
4151 // resolve cases where the input type is known to be a pointer type (which
4152 // is considered resolved), but the result knows it needs to be 32- or
4153 // 64-bits. Infer the other way for good measure.
4154 for (auto T : Pattern.getTrees())
4155 for (unsigned i = 0, e = std::min(Result.getOnlyTree()->getNumTypes(),
4158 IterateInference |= T->UpdateNodeType(
4159 i, Result.getOnlyTree()->getExtType(i), Result);
4160 IterateInference |= Result.getOnlyTree()->UpdateNodeType(
4161 i, T->getExtType(i), Result);
4164 // If our iteration has converged and the input pattern's types are fully
4165 // resolved but the result pattern is not fully resolved, we may have a
4166 // situation where we have two instructions in the result pattern and
4167 // the instructions require a common register class, but don't care about
4168 // what actual MVT is used. This is actually a bug in our modelling:
4169 // output patterns should have register classes, not MVTs.
4171 // In any case, to handle this, we just go through and disambiguate some
4172 // arbitrary types to the result pattern's nodes.
4173 if (!IterateInference && InferredAllPatternTypes &&
4174 !InferredAllResultTypes)
4176 ForceArbitraryInstResultType(Result.getTree(0).get(), Result);
4177 } while (IterateInference);
4179 // Verify that we inferred enough types that we can do something with the
4180 // pattern and result. If these fire the user has to add type casts.
4181 if (!InferredAllPatternTypes)
4182 Pattern.error("Could not infer all types in pattern!");
4183 if (!InferredAllResultTypes) {
4185 Result.error("Could not infer all types in pattern result!");
4188 // Promote xform function to be an explicit node wherever set.
4189 TreePatternNodePtr DstShared = PromoteXForms(Result.getOnlyTree());
4191 TreePattern Temp(Result.getRecord(), DstShared, false, *this);
4192 Temp.InferAllTypes();
4194 ListInit *Preds = TheDef->getValueAsListInit("Predicates");
4195 int Complexity = TheDef->getValueAsInt("AddedComplexity");
4197 if (PatternRewriter)
4198 PatternRewriter(&Pattern);
4200 // A pattern may end up with an "impossible" type, i.e. a situation
4201 // where all types have been eliminated for some node in this pattern.
4202 // This could occur for intrinsics that only make sense for a specific
4203 // value type, and use a specific register class. If, for some mode,
4204 // that register class does not accept that type, the type inference
4205 // will lead to a contradiction, which is not an error however, but
4206 // a sign that this pattern will simply never match.
4207 if (Temp.getOnlyTree()->hasPossibleType())
4208 for (auto T : Pattern.getTrees())
4209 if (T->hasPossibleType())
4210 AddPatternToMatch(&Pattern,
4211 PatternToMatch(TheDef, makePredList(Preds),
4212 T, Temp.getOnlyTree(),
4213 InstImpResults, Complexity,
4217 void CodeGenDAGPatterns::ParsePatterns() {
4218 std::vector<Record*> Patterns = Records.getAllDerivedDefinitions("Pattern");
4220 for (Record *CurPattern : Patterns) {
4221 DagInit *Tree = CurPattern->getValueAsDag("PatternToMatch");
4223 // If the pattern references the null_frag, there's nothing to do.
4224 if (hasNullFragReference(Tree))
4227 TreePattern Pattern(CurPattern, Tree, true, *this);
4229 ListInit *LI = CurPattern->getValueAsListInit("ResultInstrs");
4230 if (LI->empty()) continue; // no pattern.
4232 // Parse the instruction.
4233 TreePattern Result(CurPattern, LI, false, *this);
4235 if (Result.getNumTrees() != 1)
4236 Result.error("Cannot handle instructions producing instructions "
4237 "with temporaries yet!");
4239 // Validate that the input pattern is correct.
4240 std::map<std::string, TreePatternNodePtr> InstInputs;
4241 MapVector<std::string, TreePatternNodePtr, std::map<std::string, unsigned>>
4243 std::vector<Record*> InstImpResults;
4244 for (unsigned j = 0, ee = Pattern.getNumTrees(); j != ee; ++j)
4245 FindPatternInputsAndOutputs(Pattern, Pattern.getTree(j), InstInputs,
4246 InstResults, InstImpResults);
4248 ParseOnePattern(CurPattern, Pattern, Result, InstImpResults);
4252 static void collectModes(std::set<unsigned> &Modes, const TreePatternNode *N) {
4253 for (const TypeSetByHwMode &VTS : N->getExtTypes())
4254 for (const auto &I : VTS)
4255 Modes.insert(I.first);
4257 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
4258 collectModes(Modes, N->getChild(i));
4261 void CodeGenDAGPatterns::ExpandHwModeBasedTypes() {
4262 const CodeGenHwModes &CGH = getTargetInfo().getHwModes();
4263 std::map<unsigned,std::vector<Predicate>> ModeChecks;
4264 std::vector<PatternToMatch> Copy = PatternsToMatch;
4265 PatternsToMatch.clear();
4267 auto AppendPattern = [this, &ModeChecks](PatternToMatch &P, unsigned Mode) {
4268 TreePatternNodePtr NewSrc = P.SrcPattern->clone();
4269 TreePatternNodePtr NewDst = P.DstPattern->clone();
4270 if (!NewSrc->setDefaultMode(Mode) || !NewDst->setDefaultMode(Mode)) {
4274 std::vector<Predicate> Preds = P.Predicates;
4275 const std::vector<Predicate> &MC = ModeChecks[Mode];
4276 Preds.insert(Preds.end(), MC.begin(), MC.end());
4277 PatternsToMatch.emplace_back(P.getSrcRecord(), Preds, std::move(NewSrc),
4278 std::move(NewDst), P.getDstRegs(),
4279 P.getAddedComplexity(), Record::getNewUID(),
4283 for (PatternToMatch &P : Copy) {
4284 TreePatternNodePtr SrcP = nullptr, DstP = nullptr;
4285 if (P.SrcPattern->hasProperTypeByHwMode())
4286 SrcP = P.SrcPattern;
4287 if (P.DstPattern->hasProperTypeByHwMode())
4288 DstP = P.DstPattern;
4289 if (!SrcP && !DstP) {
4290 PatternsToMatch.push_back(P);
4294 std::set<unsigned> Modes;
4296 collectModes(Modes, SrcP.get());
4298 collectModes(Modes, DstP.get());
4300 // The predicate for the default mode needs to be constructed for each
4301 // pattern separately.
4302 // Since not all modes must be present in each pattern, if a mode m is
4303 // absent, then there is no point in constructing a check for m. If such
4304 // a check was created, it would be equivalent to checking the default
4305 // mode, except not all modes' predicates would be a part of the checking
4306 // code. The subsequently generated check for the default mode would then
4307 // have the exact same patterns, but a different predicate code. To avoid
4308 // duplicated patterns with different predicate checks, construct the
4309 // default check as a negation of all predicates that are actually present
4310 // in the source/destination patterns.
4311 std::vector<Predicate> DefaultPred;
4313 for (unsigned M : Modes) {
4314 if (M == DefaultMode)
4316 if (ModeChecks.find(M) != ModeChecks.end())
4319 // Fill the map entry for this mode.
4320 const HwMode &HM = CGH.getMode(M);
4321 ModeChecks[M].emplace_back(Predicate(HM.Features, true));
4323 // Add negations of the HM's predicates to the default predicate.
4324 DefaultPred.emplace_back(Predicate(HM.Features, false));
4327 for (unsigned M : Modes) {
4328 if (M == DefaultMode)
4330 AppendPattern(P, M);
4333 bool HasDefault = Modes.count(DefaultMode);
4335 AppendPattern(P, DefaultMode);
4339 /// Dependent variable map for CodeGenDAGPattern variant generation
4340 typedef StringMap<int> DepVarMap;
4342 static void FindDepVarsOf(TreePatternNode *N, DepVarMap &DepMap) {
4344 if (N->hasName() && isa<DefInit>(N->getLeafValue()))
4345 DepMap[N->getName()]++;
4347 for (size_t i = 0, e = N->getNumChildren(); i != e; ++i)
4348 FindDepVarsOf(N->getChild(i), DepMap);
4352 /// Find dependent variables within child patterns
4353 static void FindDepVars(TreePatternNode *N, MultipleUseVarSet &DepVars) {
4354 DepVarMap depcounts;
4355 FindDepVarsOf(N, depcounts);
4356 for (const auto &Pair : depcounts) {
4357 if (Pair.getValue() > 1)
4358 DepVars.insert(Pair.getKey());
4363 /// Dump the dependent variable set:
4364 static void DumpDepVars(MultipleUseVarSet &DepVars) {
4365 if (DepVars.empty()) {
4366 LLVM_DEBUG(errs() << "<empty set>");
4368 LLVM_DEBUG(errs() << "[ ");
4369 for (const auto &DepVar : DepVars) {
4370 LLVM_DEBUG(errs() << DepVar.getKey() << " ");
4372 LLVM_DEBUG(errs() << "]");
4378 /// CombineChildVariants - Given a bunch of permutations of each child of the
4379 /// 'operator' node, put them together in all possible ways.
4380 static void CombineChildVariants(
4381 TreePatternNodePtr Orig,
4382 const std::vector<std::vector<TreePatternNodePtr>> &ChildVariants,
4383 std::vector<TreePatternNodePtr> &OutVariants, CodeGenDAGPatterns &CDP,
4384 const MultipleUseVarSet &DepVars) {
4385 // Make sure that each operand has at least one variant to choose from.
4386 for (const auto &Variants : ChildVariants)
4387 if (Variants.empty())
4390 // The end result is an all-pairs construction of the resultant pattern.
4391 std::vector<unsigned> Idxs;
4392 Idxs.resize(ChildVariants.size());
4396 LLVM_DEBUG(if (!Idxs.empty()) {
4397 errs() << Orig->getOperator()->getName() << ": Idxs = [ ";
4398 for (unsigned Idx : Idxs) {
4399 errs() << Idx << " ";
4404 // Create the variant and add it to the output list.
4405 std::vector<TreePatternNodePtr> NewChildren;
4406 for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i)
4407 NewChildren.push_back(ChildVariants[i][Idxs[i]]);
4408 TreePatternNodePtr R = std::make_shared<TreePatternNode>(
4409 Orig->getOperator(), std::move(NewChildren), Orig->getNumTypes());
4411 // Copy over properties.
4412 R->setName(Orig->getName());
4413 R->setNamesAsPredicateArg(Orig->getNamesAsPredicateArg());
4414 R->setPredicateCalls(Orig->getPredicateCalls());
4415 R->setTransformFn(Orig->getTransformFn());
4416 for (unsigned i = 0, e = Orig->getNumTypes(); i != e; ++i)
4417 R->setType(i, Orig->getExtType(i));
4419 // If this pattern cannot match, do not include it as a variant.
4420 std::string ErrString;
4421 // Scan to see if this pattern has already been emitted. We can get
4422 // duplication due to things like commuting:
4423 // (and GPRC:$a, GPRC:$b) -> (and GPRC:$b, GPRC:$a)
4424 // which are the same pattern. Ignore the dups.
4425 if (R->canPatternMatch(ErrString, CDP) &&
4426 none_of(OutVariants, [&](TreePatternNodePtr Variant) {
4427 return R->isIsomorphicTo(Variant.get(), DepVars);
4429 OutVariants.push_back(R);
4431 // Increment indices to the next permutation by incrementing the
4432 // indices from last index backward, e.g., generate the sequence
4433 // [0, 0], [0, 1], [1, 0], [1, 1].
4435 for (IdxsIdx = Idxs.size() - 1; IdxsIdx >= 0; --IdxsIdx) {
4436 if (++Idxs[IdxsIdx] == ChildVariants[IdxsIdx].size())
4441 NotDone = (IdxsIdx >= 0);
4445 /// CombineChildVariants - A helper function for binary operators.
4447 static void CombineChildVariants(TreePatternNodePtr Orig,
4448 const std::vector<TreePatternNodePtr> &LHS,
4449 const std::vector<TreePatternNodePtr> &RHS,
4450 std::vector<TreePatternNodePtr> &OutVariants,
4451 CodeGenDAGPatterns &CDP,
4452 const MultipleUseVarSet &DepVars) {
4453 std::vector<std::vector<TreePatternNodePtr>> ChildVariants;
4454 ChildVariants.push_back(LHS);
4455 ChildVariants.push_back(RHS);
4456 CombineChildVariants(Orig, ChildVariants, OutVariants, CDP, DepVars);
4460 GatherChildrenOfAssociativeOpcode(TreePatternNodePtr N,
4461 std::vector<TreePatternNodePtr> &Children) {
4462 assert(N->getNumChildren()==2 &&"Associative but doesn't have 2 children!");
4463 Record *Operator = N->getOperator();
4465 // Only permit raw nodes.
4466 if (!N->getName().empty() || !N->getPredicateCalls().empty() ||
4467 N->getTransformFn()) {
4468 Children.push_back(N);
4472 if (N->getChild(0)->isLeaf() || N->getChild(0)->getOperator() != Operator)
4473 Children.push_back(N->getChildShared(0));
4475 GatherChildrenOfAssociativeOpcode(N->getChildShared(0), Children);
4477 if (N->getChild(1)->isLeaf() || N->getChild(1)->getOperator() != Operator)
4478 Children.push_back(N->getChildShared(1));
4480 GatherChildrenOfAssociativeOpcode(N->getChildShared(1), Children);
4483 /// GenerateVariantsOf - Given a pattern N, generate all permutations we can of
4484 /// the (potentially recursive) pattern by using algebraic laws.
4486 static void GenerateVariantsOf(TreePatternNodePtr N,
4487 std::vector<TreePatternNodePtr> &OutVariants,
4488 CodeGenDAGPatterns &CDP,
4489 const MultipleUseVarSet &DepVars) {
4490 // We cannot permute leaves or ComplexPattern uses.
4491 if (N->isLeaf() || N->getOperator()->isSubClassOf("ComplexPattern")) {
4492 OutVariants.push_back(N);
4496 // Look up interesting info about the node.
4497 const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(N->getOperator());
4499 // If this node is associative, re-associate.
4500 if (NodeInfo.hasProperty(SDNPAssociative)) {
4501 // Re-associate by pulling together all of the linked operators
4502 std::vector<TreePatternNodePtr> MaximalChildren;
4503 GatherChildrenOfAssociativeOpcode(N, MaximalChildren);
4505 // Only handle child sizes of 3. Otherwise we'll end up trying too many
4507 if (MaximalChildren.size() == 3) {
4508 // Find the variants of all of our maximal children.
4509 std::vector<TreePatternNodePtr> AVariants, BVariants, CVariants;
4510 GenerateVariantsOf(MaximalChildren[0], AVariants, CDP, DepVars);
4511 GenerateVariantsOf(MaximalChildren[1], BVariants, CDP, DepVars);
4512 GenerateVariantsOf(MaximalChildren[2], CVariants, CDP, DepVars);
4514 // There are only two ways we can permute the tree:
4515 // (A op B) op C and A op (B op C)
4516 // Within these forms, we can also permute A/B/C.
4518 // Generate legal pair permutations of A/B/C.
4519 std::vector<TreePatternNodePtr> ABVariants;
4520 std::vector<TreePatternNodePtr> BAVariants;
4521 std::vector<TreePatternNodePtr> ACVariants;
4522 std::vector<TreePatternNodePtr> CAVariants;
4523 std::vector<TreePatternNodePtr> BCVariants;
4524 std::vector<TreePatternNodePtr> CBVariants;
4525 CombineChildVariants(N, AVariants, BVariants, ABVariants, CDP, DepVars);
4526 CombineChildVariants(N, BVariants, AVariants, BAVariants, CDP, DepVars);
4527 CombineChildVariants(N, AVariants, CVariants, ACVariants, CDP, DepVars);
4528 CombineChildVariants(N, CVariants, AVariants, CAVariants, CDP, DepVars);
4529 CombineChildVariants(N, BVariants, CVariants, BCVariants, CDP, DepVars);
4530 CombineChildVariants(N, CVariants, BVariants, CBVariants, CDP, DepVars);
4532 // Combine those into the result: (x op x) op x
4533 CombineChildVariants(N, ABVariants, CVariants, OutVariants, CDP, DepVars);
4534 CombineChildVariants(N, BAVariants, CVariants, OutVariants, CDP, DepVars);
4535 CombineChildVariants(N, ACVariants, BVariants, OutVariants, CDP, DepVars);
4536 CombineChildVariants(N, CAVariants, BVariants, OutVariants, CDP, DepVars);
4537 CombineChildVariants(N, BCVariants, AVariants, OutVariants, CDP, DepVars);
4538 CombineChildVariants(N, CBVariants, AVariants, OutVariants, CDP, DepVars);
4540 // Combine those into the result: x op (x op x)
4541 CombineChildVariants(N, CVariants, ABVariants, OutVariants, CDP, DepVars);
4542 CombineChildVariants(N, CVariants, BAVariants, OutVariants, CDP, DepVars);
4543 CombineChildVariants(N, BVariants, ACVariants, OutVariants, CDP, DepVars);
4544 CombineChildVariants(N, BVariants, CAVariants, OutVariants, CDP, DepVars);
4545 CombineChildVariants(N, AVariants, BCVariants, OutVariants, CDP, DepVars);
4546 CombineChildVariants(N, AVariants, CBVariants, OutVariants, CDP, DepVars);
4551 // Compute permutations of all children.
4552 std::vector<std::vector<TreePatternNodePtr>> ChildVariants;
4553 ChildVariants.resize(N->getNumChildren());
4554 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
4555 GenerateVariantsOf(N->getChildShared(i), ChildVariants[i], CDP, DepVars);
4557 // Build all permutations based on how the children were formed.
4558 CombineChildVariants(N, ChildVariants, OutVariants, CDP, DepVars);
4560 // If this node is commutative, consider the commuted order.
4561 bool isCommIntrinsic = N->isCommutativeIntrinsic(CDP);
4562 if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) {
4563 assert((N->getNumChildren()>=2 || isCommIntrinsic) &&
4564 "Commutative but doesn't have 2 children!");
4565 // Don't count children which are actually register references.
4567 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
4568 TreePatternNode *Child = N->getChild(i);
4569 if (Child->isLeaf())
4570 if (DefInit *DI = dyn_cast<DefInit>(Child->getLeafValue())) {
4571 Record *RR = DI->getDef();
4572 if (RR->isSubClassOf("Register"))
4577 // Consider the commuted order.
4578 if (isCommIntrinsic) {
4579 // Commutative intrinsic. First operand is the intrinsic id, 2nd and 3rd
4580 // operands are the commutative operands, and there might be more operands
4583 "Commutative intrinsic should have at least 3 children!");
4584 std::vector<std::vector<TreePatternNodePtr>> Variants;
4585 Variants.push_back(std::move(ChildVariants[0])); // Intrinsic id.
4586 Variants.push_back(std::move(ChildVariants[2]));
4587 Variants.push_back(std::move(ChildVariants[1]));
4588 for (unsigned i = 3; i != NC; ++i)
4589 Variants.push_back(std::move(ChildVariants[i]));
4590 CombineChildVariants(N, Variants, OutVariants, CDP, DepVars);
4591 } else if (NC == N->getNumChildren()) {
4592 std::vector<std::vector<TreePatternNodePtr>> Variants;
4593 Variants.push_back(std::move(ChildVariants[1]));
4594 Variants.push_back(std::move(ChildVariants[0]));
4595 for (unsigned i = 2; i != NC; ++i)
4596 Variants.push_back(std::move(ChildVariants[i]));
4597 CombineChildVariants(N, Variants, OutVariants, CDP, DepVars);
4603 // GenerateVariants - Generate variants. For example, commutative patterns can
4604 // match multiple ways. Add them to PatternsToMatch as well.
4605 void CodeGenDAGPatterns::GenerateVariants() {
4606 LLVM_DEBUG(errs() << "Generating instruction variants.\n");
4608 // Loop over all of the patterns we've collected, checking to see if we can
4609 // generate variants of the instruction, through the exploitation of
4610 // identities. This permits the target to provide aggressive matching without
4611 // the .td file having to contain tons of variants of instructions.
4613 // Note that this loop adds new patterns to the PatternsToMatch list, but we
4614 // intentionally do not reconsider these. Any variants of added patterns have
4615 // already been added.
4617 const unsigned NumOriginalPatterns = PatternsToMatch.size();
4618 BitVector MatchedPatterns(NumOriginalPatterns);
4619 std::vector<BitVector> MatchedPredicates(NumOriginalPatterns,
4620 BitVector(NumOriginalPatterns));
4622 typedef std::pair<MultipleUseVarSet, std::vector<TreePatternNodePtr>>
4624 std::map<unsigned, DepsAndVariants> PatternsWithVariants;
4626 // Collect patterns with more than one variant.
4627 for (unsigned i = 0; i != NumOriginalPatterns; ++i) {
4628 MultipleUseVarSet DepVars;
4629 std::vector<TreePatternNodePtr> Variants;
4630 FindDepVars(PatternsToMatch[i].getSrcPattern(), DepVars);
4631 LLVM_DEBUG(errs() << "Dependent/multiply used variables: ");
4632 LLVM_DEBUG(DumpDepVars(DepVars));
4633 LLVM_DEBUG(errs() << "\n");
4634 GenerateVariantsOf(PatternsToMatch[i].getSrcPatternShared(), Variants,
4637 assert(!Variants.empty() && "Must create at least original variant!");
4638 if (Variants.size() == 1) // No additional variants for this pattern.
4641 LLVM_DEBUG(errs() << "FOUND VARIANTS OF: ";
4642 PatternsToMatch[i].getSrcPattern()->dump(); errs() << "\n");
4644 PatternsWithVariants[i] = std::make_pair(DepVars, Variants);
4646 // Cache matching predicates.
4647 if (MatchedPatterns[i])
4650 const std::vector<Predicate> &Predicates =
4651 PatternsToMatch[i].getPredicates();
4653 BitVector &Matches = MatchedPredicates[i];
4654 MatchedPatterns.set(i);
4657 // Don't test patterns that have already been cached - it won't match.
4658 for (unsigned p = 0; p != NumOriginalPatterns; ++p)
4659 if (!MatchedPatterns[p])
4660 Matches[p] = (Predicates == PatternsToMatch[p].getPredicates());
4662 // Copy this to all the matching patterns.
4663 for (int p = Matches.find_first(); p != -1; p = Matches.find_next(p))
4665 MatchedPatterns.set(p);
4666 MatchedPredicates[p] = Matches;
4670 for (auto it : PatternsWithVariants) {
4671 unsigned i = it.first;
4672 const MultipleUseVarSet &DepVars = it.second.first;
4673 const std::vector<TreePatternNodePtr> &Variants = it.second.second;
4675 for (unsigned v = 0, e = Variants.size(); v != e; ++v) {
4676 TreePatternNodePtr Variant = Variants[v];
4677 BitVector &Matches = MatchedPredicates[i];
4679 LLVM_DEBUG(errs() << " VAR#" << v << ": "; Variant->dump();
4682 // Scan to see if an instruction or explicit pattern already matches this.
4683 bool AlreadyExists = false;
4684 for (unsigned p = 0, e = PatternsToMatch.size(); p != e; ++p) {
4685 // Skip if the top level predicates do not match.
4688 // Check to see if this variant already exists.
4689 if (Variant->isIsomorphicTo(PatternsToMatch[p].getSrcPattern(),
4691 LLVM_DEBUG(errs() << " *** ALREADY EXISTS, ignoring variant.\n");
4692 AlreadyExists = true;
4696 // If we already have it, ignore the variant.
4697 if (AlreadyExists) continue;
4699 // Otherwise, add it to the list of patterns we have.
4700 PatternsToMatch.push_back(PatternToMatch(
4701 PatternsToMatch[i].getSrcRecord(), PatternsToMatch[i].getPredicates(),
4702 Variant, PatternsToMatch[i].getDstPatternShared(),
4703 PatternsToMatch[i].getDstRegs(),
4704 PatternsToMatch[i].getAddedComplexity(), Record::getNewUID()));
4705 MatchedPredicates.push_back(Matches);
4707 // Add a new match the same as this pattern.
4708 for (auto &P : MatchedPredicates)
4712 LLVM_DEBUG(errs() << "\n");