1 //===- CodeGenDAGPatterns.cpp - Read DAG patterns from .td file -----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the CodeGenDAGPatterns class, which is used to read and
10 // represent the patterns present in a .td file for instructions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CodeGenDAGPatterns.h"
15 #include "llvm/ADT/DenseSet.h"
16 #include "llvm/ADT/MapVector.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallSet.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/TypeSize.h"
26 #include "llvm/TableGen/Error.h"
27 #include "llvm/TableGen/Record.h"
28 #include <algorithm>
29 #include <cstdio>
30 #include <iterator>
31 #include <set>
32 using namespace llvm;
33 
34 #define DEBUG_TYPE "dag-patterns"
35 
36 static inline bool isIntegerOrPtr(MVT VT) {
37   return VT.isInteger() || VT == MVT::iPTR;
38 }
39 static inline bool isFloatingPoint(MVT VT) {
40   return VT.isFloatingPoint();
41 }
42 static inline bool isVector(MVT VT) {
43   return VT.isVector();
44 }
45 static inline bool isScalar(MVT VT) {
46   return !VT.isVector();
47 }
48 
49 template <typename Predicate>
50 static bool berase_if(MachineValueTypeSet &S, Predicate P) {
51   bool Erased = false;
52   // It is ok to iterate over MachineValueTypeSet and remove elements from it
53   // at the same time.
54   for (MVT T : S) {
55     if (!P(T))
56       continue;
57     Erased = true;
58     S.erase(T);
59   }
60   return Erased;
61 }
62 
63 // --- TypeSetByHwMode
64 
65 // This is a parameterized type-set class. For each mode there is a list
66 // of types that are currently possible for a given tree node. Type
67 // inference will apply to each mode separately.
68 
69 TypeSetByHwMode::TypeSetByHwMode(ArrayRef<ValueTypeByHwMode> VTList) {
70   for (const ValueTypeByHwMode &VVT : VTList) {
71     insert(VVT);
72     AddrSpaces.push_back(VVT.PtrAddrSpace);
73   }
74 }
75 
76 bool TypeSetByHwMode::isValueTypeByHwMode(bool AllowEmpty) const {
77   for (const auto &I : *this) {
78     if (I.second.size() > 1)
79       return false;
80     if (!AllowEmpty && I.second.empty())
81       return false;
82   }
83   return true;
84 }
85 
86 ValueTypeByHwMode TypeSetByHwMode::getValueTypeByHwMode() const {
87   assert(isValueTypeByHwMode(true) &&
88          "The type set has multiple types for at least one HW mode");
89   ValueTypeByHwMode VVT;
90   auto ASI = AddrSpaces.begin();
91 
92   for (const auto &I : *this) {
93     MVT T = I.second.empty() ? MVT::Other : *I.second.begin();
94     VVT.getOrCreateTypeForMode(I.first, T);
95     if (ASI != AddrSpaces.end())
96       VVT.PtrAddrSpace = *ASI++;
97   }
98   return VVT;
99 }
100 
101 bool TypeSetByHwMode::isPossible() const {
102   for (const auto &I : *this)
103     if (!I.second.empty())
104       return true;
105   return false;
106 }
107 
108 bool TypeSetByHwMode::insert(const ValueTypeByHwMode &VVT) {
109   bool Changed = false;
110   bool ContainsDefault = false;
111   MVT DT = MVT::Other;
112 
113   for (const auto &P : VVT) {
114     unsigned M = P.first;
115     // Make sure there exists a set for each specific mode from VVT.
116     Changed |= getOrCreate(M).insert(P.second).second;
117     // Cache VVT's default mode.
118     if (DefaultMode == M) {
119       ContainsDefault = true;
120       DT = P.second;
121     }
122   }
123 
124   // If VVT has a default mode, add the corresponding type to all
125   // modes in "this" that do not exist in VVT.
126   if (ContainsDefault)
127     for (auto &I : *this)
128       if (!VVT.hasMode(I.first))
129         Changed |= I.second.insert(DT).second;
130 
131   return Changed;
132 }
133 
134 // Constrain the type set to be the intersection with VTS.
135 bool TypeSetByHwMode::constrain(const TypeSetByHwMode &VTS) {
136   bool Changed = false;
137   if (hasDefault()) {
138     for (const auto &I : VTS) {
139       unsigned M = I.first;
140       if (M == DefaultMode || hasMode(M))
141         continue;
142       Map.insert({M, Map.at(DefaultMode)});
143       Changed = true;
144     }
145   }
146 
147   for (auto &I : *this) {
148     unsigned M = I.first;
149     SetType &S = I.second;
150     if (VTS.hasMode(M) || VTS.hasDefault()) {
151       Changed |= intersect(I.second, VTS.get(M));
152     } else if (!S.empty()) {
153       S.clear();
154       Changed = true;
155     }
156   }
157   return Changed;
158 }
159 
160 template <typename Predicate>
161 bool TypeSetByHwMode::constrain(Predicate P) {
162   bool Changed = false;
163   for (auto &I : *this)
164     Changed |= berase_if(I.second, [&P](MVT VT) { return !P(VT); });
165   return Changed;
166 }
167 
168 template <typename Predicate>
169 bool TypeSetByHwMode::assign_if(const TypeSetByHwMode &VTS, Predicate P) {
170   assert(empty());
171   for (const auto &I : VTS) {
172     SetType &S = getOrCreate(I.first);
173     for (auto J : I.second)
174       if (P(J))
175         S.insert(J);
176   }
177   return !empty();
178 }
179 
180 void TypeSetByHwMode::writeToStream(raw_ostream &OS) const {
181   SmallVector<unsigned, 4> Modes;
182   Modes.reserve(Map.size());
183 
184   for (const auto &I : *this)
185     Modes.push_back(I.first);
186   if (Modes.empty()) {
187     OS << "{}";
188     return;
189   }
190   array_pod_sort(Modes.begin(), Modes.end());
191 
192   OS << '{';
193   for (unsigned M : Modes) {
194     OS << ' ' << getModeName(M) << ':';
195     writeToStream(get(M), OS);
196   }
197   OS << " }";
198 }
199 
200 void TypeSetByHwMode::writeToStream(const SetType &S, raw_ostream &OS) {
201   SmallVector<MVT, 4> Types(S.begin(), S.end());
202   array_pod_sort(Types.begin(), Types.end());
203 
204   OS << '[';
205   ListSeparator LS(" ");
206   for (const MVT &T : Types)
207     OS << LS << ValueTypeByHwMode::getMVTName(T);
208   OS << ']';
209 }
210 
211 bool TypeSetByHwMode::operator==(const TypeSetByHwMode &VTS) const {
212   // The isSimple call is much quicker than hasDefault - check this first.
213   bool IsSimple = isSimple();
214   bool VTSIsSimple = VTS.isSimple();
215   if (IsSimple && VTSIsSimple)
216     return *begin() == *VTS.begin();
217 
218   // Speedup: We have a default if the set is simple.
219   bool HaveDefault = IsSimple || hasDefault();
220   bool VTSHaveDefault = VTSIsSimple || VTS.hasDefault();
221   if (HaveDefault != VTSHaveDefault)
222     return false;
223 
224   SmallSet<unsigned, 4> Modes;
225   for (auto &I : *this)
226     Modes.insert(I.first);
227   for (const auto &I : VTS)
228     Modes.insert(I.first);
229 
230   if (HaveDefault) {
231     // Both sets have default mode.
232     for (unsigned M : Modes) {
233       if (get(M) != VTS.get(M))
234         return false;
235     }
236   } else {
237     // Neither set has default mode.
238     for (unsigned M : Modes) {
239       // If there is no default mode, an empty set is equivalent to not having
240       // the corresponding mode.
241       bool NoModeThis = !hasMode(M) || get(M).empty();
242       bool NoModeVTS = !VTS.hasMode(M) || VTS.get(M).empty();
243       if (NoModeThis != NoModeVTS)
244         return false;
245       if (!NoModeThis)
246         if (get(M) != VTS.get(M))
247           return false;
248     }
249   }
250 
251   return true;
252 }
253 
254 namespace llvm {
255   raw_ostream &operator<<(raw_ostream &OS, const TypeSetByHwMode &T) {
256     T.writeToStream(OS);
257     return OS;
258   }
259 }
260 
261 LLVM_DUMP_METHOD
262 void TypeSetByHwMode::dump() const {
263   dbgs() << *this << '\n';
264 }
265 
266 bool TypeSetByHwMode::intersect(SetType &Out, const SetType &In) {
267   bool OutP = Out.count(MVT::iPTR), InP = In.count(MVT::iPTR);
268   auto Int = [&In](MVT T) -> bool { return !In.count(T); };
269 
270   if (OutP == InP)
271     return berase_if(Out, Int);
272 
273   // Compute the intersection of scalars separately to account for only
274   // one set containing iPTR.
275   // The intersection of iPTR with a set of integer scalar types that does not
276   // include iPTR will result in the most specific scalar type:
277   // - iPTR is more specific than any set with two elements or more
278   // - iPTR is less specific than any single integer scalar type.
279   // For example
280   // { iPTR } * { i32 }     -> { i32 }
281   // { iPTR } * { i32 i64 } -> { iPTR }
282   // and
283   // { iPTR i32 } * { i32 }          -> { i32 }
284   // { iPTR i32 } * { i32 i64 }      -> { i32 i64 }
285   // { iPTR i32 } * { i32 i64 i128 } -> { iPTR i32 }
286 
287   // Compute the difference between the two sets in such a way that the
288   // iPTR is in the set that is being subtracted. This is to see if there
289   // are any extra scalars in the set without iPTR that are not in the
290   // set containing iPTR. Then the iPTR could be considered a "wildcard"
291   // matching these scalars. If there is only one such scalar, it would
292   // replace the iPTR, if there are more, the iPTR would be retained.
293   SetType Diff;
294   if (InP) {
295     Diff = Out;
296     berase_if(Diff, [&In](MVT T) { return In.count(T); });
297     // Pre-remove these elements and rely only on InP/OutP to determine
298     // whether a change has been made.
299     berase_if(Out, [&Diff](MVT T) { return Diff.count(T); });
300   } else {
301     Diff = In;
302     berase_if(Diff, [&Out](MVT T) { return Out.count(T); });
303     Out.erase(MVT::iPTR);
304   }
305 
306   // The actual intersection.
307   bool Changed = berase_if(Out, Int);
308   unsigned NumD = Diff.size();
309   if (NumD == 0)
310     return Changed;
311 
312   if (NumD == 1) {
313     Out.insert(*Diff.begin());
314     // This is a change only if Out was the one with iPTR (which is now
315     // being replaced).
316     Changed |= OutP;
317   } else {
318     // Multiple elements from Out are now replaced with iPTR.
319     Out.insert(MVT::iPTR);
320     Changed |= !OutP;
321   }
322   return Changed;
323 }
324 
325 bool TypeSetByHwMode::validate() const {
326 #ifndef NDEBUG
327   if (empty())
328     return true;
329   bool AllEmpty = true;
330   for (const auto &I : *this)
331     AllEmpty &= I.second.empty();
332   return !AllEmpty;
333 #endif
334   return true;
335 }
336 
337 // --- TypeInfer
338 
339 bool TypeInfer::MergeInTypeInfo(TypeSetByHwMode &Out,
340                                 const TypeSetByHwMode &In) {
341   ValidateOnExit _1(Out, *this);
342   In.validate();
343   if (In.empty() || Out == In || TP.hasError())
344     return false;
345   if (Out.empty()) {
346     Out = In;
347     return true;
348   }
349 
350   bool Changed = Out.constrain(In);
351   if (Changed && Out.empty())
352     TP.error("Type contradiction");
353 
354   return Changed;
355 }
356 
357 bool TypeInfer::forceArbitrary(TypeSetByHwMode &Out) {
358   ValidateOnExit _1(Out, *this);
359   if (TP.hasError())
360     return false;
361   assert(!Out.empty() && "cannot pick from an empty set");
362 
363   bool Changed = false;
364   for (auto &I : Out) {
365     TypeSetByHwMode::SetType &S = I.second;
366     if (S.size() <= 1)
367       continue;
368     MVT T = *S.begin(); // Pick the first element.
369     S.clear();
370     S.insert(T);
371     Changed = true;
372   }
373   return Changed;
374 }
375 
376 bool TypeInfer::EnforceInteger(TypeSetByHwMode &Out) {
377   ValidateOnExit _1(Out, *this);
378   if (TP.hasError())
379     return false;
380   if (!Out.empty())
381     return Out.constrain(isIntegerOrPtr);
382 
383   return Out.assign_if(getLegalTypes(), isIntegerOrPtr);
384 }
385 
386 bool TypeInfer::EnforceFloatingPoint(TypeSetByHwMode &Out) {
387   ValidateOnExit _1(Out, *this);
388   if (TP.hasError())
389     return false;
390   if (!Out.empty())
391     return Out.constrain(isFloatingPoint);
392 
393   return Out.assign_if(getLegalTypes(), isFloatingPoint);
394 }
395 
396 bool TypeInfer::EnforceScalar(TypeSetByHwMode &Out) {
397   ValidateOnExit _1(Out, *this);
398   if (TP.hasError())
399     return false;
400   if (!Out.empty())
401     return Out.constrain(isScalar);
402 
403   return Out.assign_if(getLegalTypes(), isScalar);
404 }
405 
406 bool TypeInfer::EnforceVector(TypeSetByHwMode &Out) {
407   ValidateOnExit _1(Out, *this);
408   if (TP.hasError())
409     return false;
410   if (!Out.empty())
411     return Out.constrain(isVector);
412 
413   return Out.assign_if(getLegalTypes(), isVector);
414 }
415 
416 bool TypeInfer::EnforceAny(TypeSetByHwMode &Out) {
417   ValidateOnExit _1(Out, *this);
418   if (TP.hasError() || !Out.empty())
419     return false;
420 
421   Out = getLegalTypes();
422   return true;
423 }
424 
425 template <typename Iter, typename Pred, typename Less>
426 static Iter min_if(Iter B, Iter E, Pred P, Less L) {
427   if (B == E)
428     return E;
429   Iter Min = E;
430   for (Iter I = B; I != E; ++I) {
431     if (!P(*I))
432       continue;
433     if (Min == E || L(*I, *Min))
434       Min = I;
435   }
436   return Min;
437 }
438 
439 template <typename Iter, typename Pred, typename Less>
440 static Iter max_if(Iter B, Iter E, Pred P, Less L) {
441   if (B == E)
442     return E;
443   Iter Max = E;
444   for (Iter I = B; I != E; ++I) {
445     if (!P(*I))
446       continue;
447     if (Max == E || L(*Max, *I))
448       Max = I;
449   }
450   return Max;
451 }
452 
453 /// Make sure that for each type in Small, there exists a larger type in Big.
454 bool TypeInfer::EnforceSmallerThan(TypeSetByHwMode &Small,
455                                    TypeSetByHwMode &Big) {
456   ValidateOnExit _1(Small, *this), _2(Big, *this);
457   if (TP.hasError())
458     return false;
459   bool Changed = false;
460 
461   if (Small.empty())
462     Changed |= EnforceAny(Small);
463   if (Big.empty())
464     Changed |= EnforceAny(Big);
465 
466   assert(Small.hasDefault() && Big.hasDefault());
467 
468   SmallVector<unsigned, 4> Modes;
469   union_modes(Small, Big, Modes);
470 
471   // 1. Only allow integer or floating point types and make sure that
472   //    both sides are both integer or both floating point.
473   // 2. Make sure that either both sides have vector types, or neither
474   //    of them does.
475   for (unsigned M : Modes) {
476     TypeSetByHwMode::SetType &S = Small.get(M);
477     TypeSetByHwMode::SetType &B = Big.get(M);
478 
479     if (any_of(S, isIntegerOrPtr) && any_of(S, isIntegerOrPtr)) {
480       auto NotInt = [](MVT VT) { return !isIntegerOrPtr(VT); };
481       Changed |= berase_if(S, NotInt);
482       Changed |= berase_if(B, NotInt);
483     } else if (any_of(S, isFloatingPoint) && any_of(B, isFloatingPoint)) {
484       auto NotFP = [](MVT VT) { return !isFloatingPoint(VT); };
485       Changed |= berase_if(S, NotFP);
486       Changed |= berase_if(B, NotFP);
487     } else if (S.empty() || B.empty()) {
488       Changed = !S.empty() || !B.empty();
489       S.clear();
490       B.clear();
491     } else {
492       TP.error("Incompatible types");
493       return Changed;
494     }
495 
496     if (none_of(S, isVector) || none_of(B, isVector)) {
497       Changed |= berase_if(S, isVector);
498       Changed |= berase_if(B, isVector);
499     }
500   }
501 
502   auto LT = [](MVT A, MVT B) -> bool {
503     // Always treat non-scalable MVTs as smaller than scalable MVTs for the
504     // purposes of ordering.
505     auto ASize = std::make_tuple(A.isScalableVector(), A.getScalarSizeInBits(),
506                                  A.getSizeInBits().getKnownMinSize());
507     auto BSize = std::make_tuple(B.isScalableVector(), B.getScalarSizeInBits(),
508                                  B.getSizeInBits().getKnownMinSize());
509     return ASize < BSize;
510   };
511   auto SameKindLE = [](MVT A, MVT B) -> bool {
512     // This function is used when removing elements: when a vector is compared
513     // to a non-vector or a scalable vector to any non-scalable MVT, it should
514     // return false (to avoid removal).
515     if (std::make_tuple(A.isVector(), A.isScalableVector()) !=
516         std::make_tuple(B.isVector(), B.isScalableVector()))
517       return false;
518 
519     return std::make_tuple(A.getScalarSizeInBits(),
520                            A.getSizeInBits().getKnownMinSize()) <=
521            std::make_tuple(B.getScalarSizeInBits(),
522                            B.getSizeInBits().getKnownMinSize());
523   };
524 
525   for (unsigned M : Modes) {
526     TypeSetByHwMode::SetType &S = Small.get(M);
527     TypeSetByHwMode::SetType &B = Big.get(M);
528     // MinS = min scalar in Small, remove all scalars from Big that are
529     // smaller-or-equal than MinS.
530     auto MinS = min_if(S.begin(), S.end(), isScalar, LT);
531     if (MinS != S.end())
532       Changed |= berase_if(B, std::bind(SameKindLE,
533                                         std::placeholders::_1, *MinS));
534 
535     // MaxS = max scalar in Big, remove all scalars from Small that are
536     // larger than MaxS.
537     auto MaxS = max_if(B.begin(), B.end(), isScalar, LT);
538     if (MaxS != B.end())
539       Changed |= berase_if(S, std::bind(SameKindLE,
540                                         *MaxS, std::placeholders::_1));
541 
542     // MinV = min vector in Small, remove all vectors from Big that are
543     // smaller-or-equal than MinV.
544     auto MinV = min_if(S.begin(), S.end(), isVector, LT);
545     if (MinV != S.end())
546       Changed |= berase_if(B, std::bind(SameKindLE,
547                                         std::placeholders::_1, *MinV));
548 
549     // MaxV = max vector in Big, remove all vectors from Small that are
550     // larger than MaxV.
551     auto MaxV = max_if(B.begin(), B.end(), isVector, LT);
552     if (MaxV != B.end())
553       Changed |= berase_if(S, std::bind(SameKindLE,
554                                         *MaxV, std::placeholders::_1));
555   }
556 
557   return Changed;
558 }
559 
560 /// 1. Ensure that for each type T in Vec, T is a vector type, and that
561 ///    for each type U in Elem, U is a scalar type.
562 /// 2. Ensure that for each (scalar) type U in Elem, there exists a (vector)
563 ///    type T in Vec, such that U is the element type of T.
564 bool TypeInfer::EnforceVectorEltTypeIs(TypeSetByHwMode &Vec,
565                                        TypeSetByHwMode &Elem) {
566   ValidateOnExit _1(Vec, *this), _2(Elem, *this);
567   if (TP.hasError())
568     return false;
569   bool Changed = false;
570 
571   if (Vec.empty())
572     Changed |= EnforceVector(Vec);
573   if (Elem.empty())
574     Changed |= EnforceScalar(Elem);
575 
576   SmallVector<unsigned, 4> Modes;
577   union_modes(Vec, Elem, Modes);
578   for (unsigned M : Modes) {
579     TypeSetByHwMode::SetType &V = Vec.get(M);
580     TypeSetByHwMode::SetType &E = Elem.get(M);
581 
582     Changed |= berase_if(V, isScalar);  // Scalar = !vector
583     Changed |= berase_if(E, isVector);  // Vector = !scalar
584     assert(!V.empty() && !E.empty());
585 
586     MachineValueTypeSet VT, ST;
587     // Collect element types from the "vector" set.
588     for (MVT T : V)
589       VT.insert(T.getVectorElementType());
590     // Collect scalar types from the "element" set.
591     for (MVT T : E)
592       ST.insert(T);
593 
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());
597                             });
598     // Remove from E all (scalar) types, for which there is no corresponding
599     // type in V.
600     Changed |= berase_if(E, [&VT](MVT T) -> bool { return !VT.count(T); });
601   }
602 
603   return Changed;
604 }
605 
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);
611 }
612 
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);
619   if (TP.hasError())
620     return false;
621 
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())
625       return false;
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
628     // types separate.
629     if (B.isScalableVector() != P.isScalableVector())
630       return false;
631     if (B.getVectorElementType() != P.getVectorElementType())
632       return false;
633     return B.getVectorNumElements() < P.getVectorNumElements();
634   };
635 
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 {
639     for (auto I : S)
640       if (IsSubVec(T, I))
641         return false;
642     return true;
643   };
644 
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 {
648     for (auto I : S)
649       if (IsSubVec(I, T))
650         return false;
651     return true;
652   };
653 
654   bool Changed = false;
655 
656   if (Vec.empty())
657     Changed |= EnforceVector(Vec);
658   if (Sub.empty())
659     Changed |= EnforceVector(Sub);
660 
661   SmallVector<unsigned, 4> Modes;
662   union_modes(Vec, Sub, Modes);
663   for (unsigned M : Modes) {
664     TypeSetByHwMode::SetType &S = Sub.get(M);
665     TypeSetByHwMode::SetType &V = Vec.get(M);
666 
667     Changed |= berase_if(S, isScalar);
668 
669     // Erase all types from S that are not sub-vectors of a type in V.
670     Changed |= berase_if(S, std::bind(NoSubV, V, std::placeholders::_1));
671 
672     // Erase all types from V that are not super-vectors of a type in S.
673     Changed |= berase_if(V, std::bind(NoSupV, S, std::placeholders::_1));
674   }
675 
676   return Changed;
677 }
678 
679 /// 1. Ensure that V has a scalar type iff W has a scalar type.
680 /// 2. Ensure that for each vector type T in V, there exists a vector
681 ///    type U in W, such that T and U have the same number of elements.
682 /// 3. Ensure that for each vector type U in W, there exists a vector
683 ///    type T in V, such that T and U have the same number of elements
684 ///    (reverse of 2).
685 bool TypeInfer::EnforceSameNumElts(TypeSetByHwMode &V, TypeSetByHwMode &W) {
686   ValidateOnExit _1(V, *this), _2(W, *this);
687   if (TP.hasError())
688     return false;
689 
690   bool Changed = false;
691   if (V.empty())
692     Changed |= EnforceAny(V);
693   if (W.empty())
694     Changed |= EnforceAny(W);
695 
696   // An actual vector type cannot have 0 elements, so we can treat scalars
697   // as zero-length vectors. This way both vectors and scalars can be
698   // processed identically.
699   auto NoLength = [](const SmallSet<unsigned,2> &Lengths, MVT T) -> bool {
700     return !Lengths.count(T.isVector() ? T.getVectorNumElements() : 0);
701   };
702 
703   SmallVector<unsigned, 4> Modes;
704   union_modes(V, W, Modes);
705   for (unsigned M : Modes) {
706     TypeSetByHwMode::SetType &VS = V.get(M);
707     TypeSetByHwMode::SetType &WS = W.get(M);
708 
709     SmallSet<unsigned,2> VN, WN;
710     for (MVT T : VS)
711       VN.insert(T.isVector() ? T.getVectorNumElements() : 0);
712     for (MVT T : WS)
713       WN.insert(T.isVector() ? T.getVectorNumElements() : 0);
714 
715     Changed |= berase_if(VS, std::bind(NoLength, WN, std::placeholders::_1));
716     Changed |= berase_if(WS, std::bind(NoLength, VN, std::placeholders::_1));
717   }
718   return Changed;
719 }
720 
721 namespace {
722 struct TypeSizeComparator {
723   bool operator()(const TypeSize &LHS, const TypeSize &RHS) const {
724     return std::make_tuple(LHS.isScalable(), LHS.getKnownMinValue()) <
725            std::make_tuple(RHS.isScalable(), RHS.getKnownMinValue());
726   }
727 };
728 } // end anonymous namespace
729 
730 /// 1. Ensure that for each type T in A, there exists a type U in B,
731 ///    such that T and U have equal size in bits.
732 /// 2. Ensure that for each type U in B, there exists a type T in A
733 ///    such that T and U have equal size in bits (reverse of 1).
734 bool TypeInfer::EnforceSameSize(TypeSetByHwMode &A, TypeSetByHwMode &B) {
735   ValidateOnExit _1(A, *this), _2(B, *this);
736   if (TP.hasError())
737     return false;
738   bool Changed = false;
739   if (A.empty())
740     Changed |= EnforceAny(A);
741   if (B.empty())
742     Changed |= EnforceAny(B);
743 
744   typedef SmallSet<TypeSize, 2, TypeSizeComparator> TypeSizeSet;
745 
746   auto NoSize = [](const TypeSizeSet &Sizes, MVT T) -> bool {
747     return !Sizes.count(T.getSizeInBits());
748   };
749 
750   SmallVector<unsigned, 4> Modes;
751   union_modes(A, B, Modes);
752   for (unsigned M : Modes) {
753     TypeSetByHwMode::SetType &AS = A.get(M);
754     TypeSetByHwMode::SetType &BS = B.get(M);
755     TypeSizeSet AN, BN;
756 
757     for (MVT T : AS)
758       AN.insert(T.getSizeInBits());
759     for (MVT T : BS)
760       BN.insert(T.getSizeInBits());
761 
762     Changed |= berase_if(AS, std::bind(NoSize, BN, std::placeholders::_1));
763     Changed |= berase_if(BS, std::bind(NoSize, AN, std::placeholders::_1));
764   }
765 
766   return Changed;
767 }
768 
769 void TypeInfer::expandOverloads(TypeSetByHwMode &VTS) {
770   ValidateOnExit _1(VTS, *this);
771   const TypeSetByHwMode &Legal = getLegalTypes();
772   assert(Legal.isDefaultOnly() && "Default-mode only expected");
773   const TypeSetByHwMode::SetType &LegalTypes = Legal.get(DefaultMode);
774 
775   for (auto &I : VTS)
776     expandOverloads(I.second, LegalTypes);
777 }
778 
779 void TypeInfer::expandOverloads(TypeSetByHwMode::SetType &Out,
780                                 const TypeSetByHwMode::SetType &Legal) {
781   std::set<MVT> Ovs;
782   for (MVT T : Out) {
783     if (!T.isOverloaded())
784       continue;
785 
786     Ovs.insert(T);
787     // MachineValueTypeSet allows iteration and erasing.
788     Out.erase(T);
789   }
790 
791   for (MVT Ov : Ovs) {
792     switch (Ov.SimpleTy) {
793       case MVT::iPTRAny:
794         Out.insert(MVT::iPTR);
795         return;
796       case MVT::iAny:
797         for (MVT T : MVT::integer_valuetypes())
798           if (Legal.count(T))
799             Out.insert(T);
800         for (MVT T : MVT::integer_fixedlen_vector_valuetypes())
801           if (Legal.count(T))
802             Out.insert(T);
803         for (MVT T : MVT::integer_scalable_vector_valuetypes())
804           if (Legal.count(T))
805             Out.insert(T);
806         return;
807       case MVT::fAny:
808         for (MVT T : MVT::fp_valuetypes())
809           if (Legal.count(T))
810             Out.insert(T);
811         for (MVT T : MVT::fp_fixedlen_vector_valuetypes())
812           if (Legal.count(T))
813             Out.insert(T);
814         for (MVT T : MVT::fp_scalable_vector_valuetypes())
815           if (Legal.count(T))
816             Out.insert(T);
817         return;
818       case MVT::vAny:
819         for (MVT T : MVT::vector_valuetypes())
820           if (Legal.count(T))
821             Out.insert(T);
822         return;
823       case MVT::Any:
824         for (MVT T : MVT::all_valuetypes())
825           if (Legal.count(T))
826             Out.insert(T);
827         return;
828       default:
829         break;
830     }
831   }
832 }
833 
834 const TypeSetByHwMode &TypeInfer::getLegalTypes() {
835   if (!LegalTypesCached) {
836     TypeSetByHwMode::SetType &LegalTypes = LegalCache.getOrCreate(DefaultMode);
837     // Stuff all types from all modes into the default mode.
838     const TypeSetByHwMode &LTS = TP.getDAGPatterns().getLegalTypes();
839     for (const auto &I : LTS)
840       LegalTypes.insert(I.second);
841     LegalTypesCached = true;
842   }
843   assert(LegalCache.isDefaultOnly() && "Default-mode only expected");
844   return LegalCache;
845 }
846 
847 #ifndef NDEBUG
848 TypeInfer::ValidateOnExit::~ValidateOnExit() {
849   if (Infer.Validate && !VTS.validate()) {
850     dbgs() << "Type set is empty for each HW mode:\n"
851               "possible type contradiction in the pattern below "
852               "(use -print-records with llvm-tblgen to see all "
853               "expanded records).\n";
854     Infer.TP.dump();
855     dbgs() << "Generated from record:\n";
856     Infer.TP.getRecord()->dump();
857     PrintFatalError(Infer.TP.getRecord()->getLoc(),
858                     "Type set is empty for each HW mode in '" +
859                         Infer.TP.getRecord()->getName() + "'");
860   }
861 }
862 #endif
863 
864 
865 //===----------------------------------------------------------------------===//
866 // ScopedName Implementation
867 //===----------------------------------------------------------------------===//
868 
869 bool ScopedName::operator==(const ScopedName &o) const {
870   return Scope == o.Scope && Identifier == o.Identifier;
871 }
872 
873 bool ScopedName::operator!=(const ScopedName &o) const {
874   return !(*this == o);
875 }
876 
877 
878 //===----------------------------------------------------------------------===//
879 // TreePredicateFn Implementation
880 //===----------------------------------------------------------------------===//
881 
882 /// TreePredicateFn constructor.  Here 'N' is a subclass of PatFrag.
883 TreePredicateFn::TreePredicateFn(TreePattern *N) : PatFragRec(N) {
884   assert(
885       (!hasPredCode() || !hasImmCode()) &&
886       ".td file corrupt: can't have a node predicate *and* an imm predicate");
887 }
888 
889 bool TreePredicateFn::hasPredCode() const {
890   return isLoad() || isStore() || isAtomic() ||
891          !PatFragRec->getRecord()->getValueAsString("PredicateCode").empty();
892 }
893 
894 std::string TreePredicateFn::getPredCode() const {
895   std::string Code;
896 
897   if (!isLoad() && !isStore() && !isAtomic()) {
898     Record *MemoryVT = getMemoryVT();
899 
900     if (MemoryVT)
901       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
902                       "MemoryVT requires IsLoad or IsStore");
903   }
904 
905   if (!isLoad() && !isStore()) {
906     if (isUnindexed())
907       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
908                       "IsUnindexed requires IsLoad or IsStore");
909 
910     Record *ScalarMemoryVT = getScalarMemoryVT();
911 
912     if (ScalarMemoryVT)
913       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
914                       "ScalarMemoryVT requires IsLoad or IsStore");
915   }
916 
917   if (isLoad() + isStore() + isAtomic() > 1)
918     PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
919                     "IsLoad, IsStore, and IsAtomic are mutually exclusive");
920 
921   if (isLoad()) {
922     if (!isUnindexed() && !isNonExtLoad() && !isAnyExtLoad() &&
923         !isSignExtLoad() && !isZeroExtLoad() && getMemoryVT() == nullptr &&
924         getScalarMemoryVT() == nullptr && getAddressSpaces() == nullptr &&
925         getMinAlignment() < 1)
926       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
927                       "IsLoad cannot be used by itself");
928   } else {
929     if (isNonExtLoad())
930       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
931                       "IsNonExtLoad requires IsLoad");
932     if (isAnyExtLoad())
933       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
934                       "IsAnyExtLoad requires IsLoad");
935     if (isSignExtLoad())
936       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
937                       "IsSignExtLoad requires IsLoad");
938     if (isZeroExtLoad())
939       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
940                       "IsZeroExtLoad requires IsLoad");
941   }
942 
943   if (isStore()) {
944     if (!isUnindexed() && !isTruncStore() && !isNonTruncStore() &&
945         getMemoryVT() == nullptr && getScalarMemoryVT() == nullptr &&
946         getAddressSpaces() == nullptr && getMinAlignment() < 1)
947       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
948                       "IsStore cannot be used by itself");
949   } else {
950     if (isNonTruncStore())
951       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
952                       "IsNonTruncStore requires IsStore");
953     if (isTruncStore())
954       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
955                       "IsTruncStore requires IsStore");
956   }
957 
958   if (isAtomic()) {
959     if (getMemoryVT() == nullptr && !isAtomicOrderingMonotonic() &&
960         getAddressSpaces() == nullptr &&
961         !isAtomicOrderingAcquire() && !isAtomicOrderingRelease() &&
962         !isAtomicOrderingAcquireRelease() &&
963         !isAtomicOrderingSequentiallyConsistent() &&
964         !isAtomicOrderingAcquireOrStronger() &&
965         !isAtomicOrderingReleaseOrStronger() &&
966         !isAtomicOrderingWeakerThanAcquire() &&
967         !isAtomicOrderingWeakerThanRelease())
968       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
969                       "IsAtomic cannot be used by itself");
970   } else {
971     if (isAtomicOrderingMonotonic())
972       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
973                       "IsAtomicOrderingMonotonic requires IsAtomic");
974     if (isAtomicOrderingAcquire())
975       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
976                       "IsAtomicOrderingAcquire requires IsAtomic");
977     if (isAtomicOrderingRelease())
978       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
979                       "IsAtomicOrderingRelease requires IsAtomic");
980     if (isAtomicOrderingAcquireRelease())
981       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
982                       "IsAtomicOrderingAcquireRelease requires IsAtomic");
983     if (isAtomicOrderingSequentiallyConsistent())
984       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
985                       "IsAtomicOrderingSequentiallyConsistent requires IsAtomic");
986     if (isAtomicOrderingAcquireOrStronger())
987       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
988                       "IsAtomicOrderingAcquireOrStronger requires IsAtomic");
989     if (isAtomicOrderingReleaseOrStronger())
990       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
991                       "IsAtomicOrderingReleaseOrStronger requires IsAtomic");
992     if (isAtomicOrderingWeakerThanAcquire())
993       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
994                       "IsAtomicOrderingWeakerThanAcquire requires IsAtomic");
995   }
996 
997   if (isLoad() || isStore() || isAtomic()) {
998     if (ListInit *AddressSpaces = getAddressSpaces()) {
999       Code += "unsigned AddrSpace = cast<MemSDNode>(N)->getAddressSpace();\n"
1000         " if (";
1001 
1002       ListSeparator LS(" && ");
1003       for (Init *Val : AddressSpaces->getValues()) {
1004         Code += LS;
1005 
1006         IntInit *IntVal = dyn_cast<IntInit>(Val);
1007         if (!IntVal) {
1008           PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
1009                           "AddressSpaces element must be integer");
1010         }
1011 
1012         Code += "AddrSpace != " + utostr(IntVal->getValue());
1013       }
1014 
1015       Code += ")\nreturn false;\n";
1016     }
1017 
1018     int64_t MinAlign = getMinAlignment();
1019     if (MinAlign > 0) {
1020       Code += "if (cast<MemSDNode>(N)->getAlign() < Align(";
1021       Code += utostr(MinAlign);
1022       Code += "))\nreturn false;\n";
1023     }
1024 
1025     Record *MemoryVT = getMemoryVT();
1026 
1027     if (MemoryVT)
1028       Code += ("if (cast<MemSDNode>(N)->getMemoryVT() != MVT::" +
1029                MemoryVT->getName() + ") return false;\n")
1030                   .str();
1031   }
1032 
1033   if (isAtomic() && isAtomicOrderingMonotonic())
1034     Code += "if (cast<AtomicSDNode>(N)->getOrdering() != "
1035             "AtomicOrdering::Monotonic) return false;\n";
1036   if (isAtomic() && isAtomicOrderingAcquire())
1037     Code += "if (cast<AtomicSDNode>(N)->getOrdering() != "
1038             "AtomicOrdering::Acquire) return false;\n";
1039   if (isAtomic() && isAtomicOrderingRelease())
1040     Code += "if (cast<AtomicSDNode>(N)->getOrdering() != "
1041             "AtomicOrdering::Release) return false;\n";
1042   if (isAtomic() && isAtomicOrderingAcquireRelease())
1043     Code += "if (cast<AtomicSDNode>(N)->getOrdering() != "
1044             "AtomicOrdering::AcquireRelease) return false;\n";
1045   if (isAtomic() && isAtomicOrderingSequentiallyConsistent())
1046     Code += "if (cast<AtomicSDNode>(N)->getOrdering() != "
1047             "AtomicOrdering::SequentiallyConsistent) return false;\n";
1048 
1049   if (isAtomic() && isAtomicOrderingAcquireOrStronger())
1050     Code += "if (!isAcquireOrStronger(cast<AtomicSDNode>(N)->getOrdering())) "
1051             "return false;\n";
1052   if (isAtomic() && isAtomicOrderingWeakerThanAcquire())
1053     Code += "if (isAcquireOrStronger(cast<AtomicSDNode>(N)->getOrdering())) "
1054             "return false;\n";
1055 
1056   if (isAtomic() && isAtomicOrderingReleaseOrStronger())
1057     Code += "if (!isReleaseOrStronger(cast<AtomicSDNode>(N)->getOrdering())) "
1058             "return false;\n";
1059   if (isAtomic() && isAtomicOrderingWeakerThanRelease())
1060     Code += "if (isReleaseOrStronger(cast<AtomicSDNode>(N)->getOrdering())) "
1061             "return false;\n";
1062 
1063   if (isLoad() || isStore()) {
1064     StringRef SDNodeName = isLoad() ? "LoadSDNode" : "StoreSDNode";
1065 
1066     if (isUnindexed())
1067       Code += ("if (cast<" + SDNodeName +
1068                ">(N)->getAddressingMode() != ISD::UNINDEXED) "
1069                "return false;\n")
1070                   .str();
1071 
1072     if (isLoad()) {
1073       if ((isNonExtLoad() + isAnyExtLoad() + isSignExtLoad() +
1074            isZeroExtLoad()) > 1)
1075         PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
1076                         "IsNonExtLoad, IsAnyExtLoad, IsSignExtLoad, and "
1077                         "IsZeroExtLoad are mutually exclusive");
1078       if (isNonExtLoad())
1079         Code += "if (cast<LoadSDNode>(N)->getExtensionType() != "
1080                 "ISD::NON_EXTLOAD) return false;\n";
1081       if (isAnyExtLoad())
1082         Code += "if (cast<LoadSDNode>(N)->getExtensionType() != ISD::EXTLOAD) "
1083                 "return false;\n";
1084       if (isSignExtLoad())
1085         Code += "if (cast<LoadSDNode>(N)->getExtensionType() != ISD::SEXTLOAD) "
1086                 "return false;\n";
1087       if (isZeroExtLoad())
1088         Code += "if (cast<LoadSDNode>(N)->getExtensionType() != ISD::ZEXTLOAD) "
1089                 "return false;\n";
1090     } else {
1091       if ((isNonTruncStore() + isTruncStore()) > 1)
1092         PrintFatalError(
1093             getOrigPatFragRecord()->getRecord()->getLoc(),
1094             "IsNonTruncStore, and IsTruncStore are mutually exclusive");
1095       if (isNonTruncStore())
1096         Code +=
1097             " if (cast<StoreSDNode>(N)->isTruncatingStore()) return false;\n";
1098       if (isTruncStore())
1099         Code +=
1100             " if (!cast<StoreSDNode>(N)->isTruncatingStore()) return false;\n";
1101     }
1102 
1103     Record *ScalarMemoryVT = getScalarMemoryVT();
1104 
1105     if (ScalarMemoryVT)
1106       Code += ("if (cast<" + SDNodeName +
1107                ">(N)->getMemoryVT().getScalarType() != MVT::" +
1108                ScalarMemoryVT->getName() + ") return false;\n")
1109                   .str();
1110   }
1111 
1112   std::string PredicateCode =
1113       std::string(PatFragRec->getRecord()->getValueAsString("PredicateCode"));
1114 
1115   Code += PredicateCode;
1116 
1117   if (PredicateCode.empty() && !Code.empty())
1118     Code += "return true;\n";
1119 
1120   return Code;
1121 }
1122 
1123 bool TreePredicateFn::hasImmCode() const {
1124   return !PatFragRec->getRecord()->getValueAsString("ImmediateCode").empty();
1125 }
1126 
1127 std::string TreePredicateFn::getImmCode() const {
1128   return std::string(
1129       PatFragRec->getRecord()->getValueAsString("ImmediateCode"));
1130 }
1131 
1132 bool TreePredicateFn::immCodeUsesAPInt() const {
1133   return getOrigPatFragRecord()->getRecord()->getValueAsBit("IsAPInt");
1134 }
1135 
1136 bool TreePredicateFn::immCodeUsesAPFloat() const {
1137   bool Unset;
1138   // The return value will be false when IsAPFloat is unset.
1139   return getOrigPatFragRecord()->getRecord()->getValueAsBitOrUnset("IsAPFloat",
1140                                                                    Unset);
1141 }
1142 
1143 bool TreePredicateFn::isPredefinedPredicateEqualTo(StringRef Field,
1144                                                    bool Value) const {
1145   bool Unset;
1146   bool Result =
1147       getOrigPatFragRecord()->getRecord()->getValueAsBitOrUnset(Field, Unset);
1148   if (Unset)
1149     return false;
1150   return Result == Value;
1151 }
1152 bool TreePredicateFn::usesOperands() const {
1153   return isPredefinedPredicateEqualTo("PredicateCodeUsesOperands", true);
1154 }
1155 bool TreePredicateFn::isLoad() const {
1156   return isPredefinedPredicateEqualTo("IsLoad", true);
1157 }
1158 bool TreePredicateFn::isStore() const {
1159   return isPredefinedPredicateEqualTo("IsStore", true);
1160 }
1161 bool TreePredicateFn::isAtomic() const {
1162   return isPredefinedPredicateEqualTo("IsAtomic", true);
1163 }
1164 bool TreePredicateFn::isUnindexed() const {
1165   return isPredefinedPredicateEqualTo("IsUnindexed", true);
1166 }
1167 bool TreePredicateFn::isNonExtLoad() const {
1168   return isPredefinedPredicateEqualTo("IsNonExtLoad", true);
1169 }
1170 bool TreePredicateFn::isAnyExtLoad() const {
1171   return isPredefinedPredicateEqualTo("IsAnyExtLoad", true);
1172 }
1173 bool TreePredicateFn::isSignExtLoad() const {
1174   return isPredefinedPredicateEqualTo("IsSignExtLoad", true);
1175 }
1176 bool TreePredicateFn::isZeroExtLoad() const {
1177   return isPredefinedPredicateEqualTo("IsZeroExtLoad", true);
1178 }
1179 bool TreePredicateFn::isNonTruncStore() const {
1180   return isPredefinedPredicateEqualTo("IsTruncStore", false);
1181 }
1182 bool TreePredicateFn::isTruncStore() const {
1183   return isPredefinedPredicateEqualTo("IsTruncStore", true);
1184 }
1185 bool TreePredicateFn::isAtomicOrderingMonotonic() const {
1186   return isPredefinedPredicateEqualTo("IsAtomicOrderingMonotonic", true);
1187 }
1188 bool TreePredicateFn::isAtomicOrderingAcquire() const {
1189   return isPredefinedPredicateEqualTo("IsAtomicOrderingAcquire", true);
1190 }
1191 bool TreePredicateFn::isAtomicOrderingRelease() const {
1192   return isPredefinedPredicateEqualTo("IsAtomicOrderingRelease", true);
1193 }
1194 bool TreePredicateFn::isAtomicOrderingAcquireRelease() const {
1195   return isPredefinedPredicateEqualTo("IsAtomicOrderingAcquireRelease", true);
1196 }
1197 bool TreePredicateFn::isAtomicOrderingSequentiallyConsistent() const {
1198   return isPredefinedPredicateEqualTo("IsAtomicOrderingSequentiallyConsistent",
1199                                       true);
1200 }
1201 bool TreePredicateFn::isAtomicOrderingAcquireOrStronger() const {
1202   return isPredefinedPredicateEqualTo("IsAtomicOrderingAcquireOrStronger", true);
1203 }
1204 bool TreePredicateFn::isAtomicOrderingWeakerThanAcquire() const {
1205   return isPredefinedPredicateEqualTo("IsAtomicOrderingAcquireOrStronger", false);
1206 }
1207 bool TreePredicateFn::isAtomicOrderingReleaseOrStronger() const {
1208   return isPredefinedPredicateEqualTo("IsAtomicOrderingReleaseOrStronger", true);
1209 }
1210 bool TreePredicateFn::isAtomicOrderingWeakerThanRelease() const {
1211   return isPredefinedPredicateEqualTo("IsAtomicOrderingReleaseOrStronger", false);
1212 }
1213 Record *TreePredicateFn::getMemoryVT() const {
1214   Record *R = getOrigPatFragRecord()->getRecord();
1215   if (R->isValueUnset("MemoryVT"))
1216     return nullptr;
1217   return R->getValueAsDef("MemoryVT");
1218 }
1219 
1220 ListInit *TreePredicateFn::getAddressSpaces() const {
1221   Record *R = getOrigPatFragRecord()->getRecord();
1222   if (R->isValueUnset("AddressSpaces"))
1223     return nullptr;
1224   return R->getValueAsListInit("AddressSpaces");
1225 }
1226 
1227 int64_t TreePredicateFn::getMinAlignment() const {
1228   Record *R = getOrigPatFragRecord()->getRecord();
1229   if (R->isValueUnset("MinAlignment"))
1230     return 0;
1231   return R->getValueAsInt("MinAlignment");
1232 }
1233 
1234 Record *TreePredicateFn::getScalarMemoryVT() const {
1235   Record *R = getOrigPatFragRecord()->getRecord();
1236   if (R->isValueUnset("ScalarMemoryVT"))
1237     return nullptr;
1238   return R->getValueAsDef("ScalarMemoryVT");
1239 }
1240 bool TreePredicateFn::hasGISelPredicateCode() const {
1241   return !PatFragRec->getRecord()
1242               ->getValueAsString("GISelPredicateCode")
1243               .empty();
1244 }
1245 std::string TreePredicateFn::getGISelPredicateCode() const {
1246   return std::string(
1247       PatFragRec->getRecord()->getValueAsString("GISelPredicateCode"));
1248 }
1249 
1250 StringRef TreePredicateFn::getImmType() const {
1251   if (immCodeUsesAPInt())
1252     return "const APInt &";
1253   if (immCodeUsesAPFloat())
1254     return "const APFloat &";
1255   return "int64_t";
1256 }
1257 
1258 StringRef TreePredicateFn::getImmTypeIdentifier() const {
1259   if (immCodeUsesAPInt())
1260     return "APInt";
1261   if (immCodeUsesAPFloat())
1262     return "APFloat";
1263   return "I64";
1264 }
1265 
1266 /// isAlwaysTrue - Return true if this is a noop predicate.
1267 bool TreePredicateFn::isAlwaysTrue() const {
1268   return !hasPredCode() && !hasImmCode();
1269 }
1270 
1271 /// Return the name to use in the generated code to reference this, this is
1272 /// "Predicate_foo" if from a pattern fragment "foo".
1273 std::string TreePredicateFn::getFnName() const {
1274   return "Predicate_" + PatFragRec->getRecord()->getName().str();
1275 }
1276 
1277 /// getCodeToRunOnSDNode - Return the code for the function body that
1278 /// evaluates this predicate.  The argument is expected to be in "Node",
1279 /// not N.  This handles casting and conversion to a concrete node type as
1280 /// appropriate.
1281 std::string TreePredicateFn::getCodeToRunOnSDNode() const {
1282   // Handle immediate predicates first.
1283   std::string ImmCode = getImmCode();
1284   if (!ImmCode.empty()) {
1285     if (isLoad())
1286       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
1287                       "IsLoad cannot be used with ImmLeaf or its subclasses");
1288     if (isStore())
1289       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
1290                       "IsStore cannot be used with ImmLeaf or its subclasses");
1291     if (isUnindexed())
1292       PrintFatalError(
1293           getOrigPatFragRecord()->getRecord()->getLoc(),
1294           "IsUnindexed cannot be used with ImmLeaf or its subclasses");
1295     if (isNonExtLoad())
1296       PrintFatalError(
1297           getOrigPatFragRecord()->getRecord()->getLoc(),
1298           "IsNonExtLoad cannot be used with ImmLeaf or its subclasses");
1299     if (isAnyExtLoad())
1300       PrintFatalError(
1301           getOrigPatFragRecord()->getRecord()->getLoc(),
1302           "IsAnyExtLoad cannot be used with ImmLeaf or its subclasses");
1303     if (isSignExtLoad())
1304       PrintFatalError(
1305           getOrigPatFragRecord()->getRecord()->getLoc(),
1306           "IsSignExtLoad cannot be used with ImmLeaf or its subclasses");
1307     if (isZeroExtLoad())
1308       PrintFatalError(
1309           getOrigPatFragRecord()->getRecord()->getLoc(),
1310           "IsZeroExtLoad cannot be used with ImmLeaf or its subclasses");
1311     if (isNonTruncStore())
1312       PrintFatalError(
1313           getOrigPatFragRecord()->getRecord()->getLoc(),
1314           "IsNonTruncStore cannot be used with ImmLeaf or its subclasses");
1315     if (isTruncStore())
1316       PrintFatalError(
1317           getOrigPatFragRecord()->getRecord()->getLoc(),
1318           "IsTruncStore cannot be used with ImmLeaf or its subclasses");
1319     if (getMemoryVT())
1320       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
1321                       "MemoryVT cannot be used with ImmLeaf or its subclasses");
1322     if (getScalarMemoryVT())
1323       PrintFatalError(
1324           getOrigPatFragRecord()->getRecord()->getLoc(),
1325           "ScalarMemoryVT cannot be used with ImmLeaf or its subclasses");
1326 
1327     std::string Result = ("    " + getImmType() + " Imm = ").str();
1328     if (immCodeUsesAPFloat())
1329       Result += "cast<ConstantFPSDNode>(Node)->getValueAPF();\n";
1330     else if (immCodeUsesAPInt())
1331       Result += "cast<ConstantSDNode>(Node)->getAPIntValue();\n";
1332     else
1333       Result += "cast<ConstantSDNode>(Node)->getSExtValue();\n";
1334     return Result + ImmCode;
1335   }
1336 
1337   // Handle arbitrary node predicates.
1338   assert(hasPredCode() && "Don't have any predicate code!");
1339 
1340   // If this is using PatFrags, there are multiple trees to search. They should
1341   // all have the same class.  FIXME: Is there a way to find a common
1342   // superclass?
1343   StringRef ClassName;
1344   for (const auto &Tree : PatFragRec->getTrees()) {
1345     StringRef TreeClassName;
1346     if (Tree->isLeaf())
1347       TreeClassName = "SDNode";
1348     else {
1349       Record *Op = Tree->getOperator();
1350       const SDNodeInfo &Info = PatFragRec->getDAGPatterns().getSDNodeInfo(Op);
1351       TreeClassName = Info.getSDClassName();
1352     }
1353 
1354     if (ClassName.empty())
1355       ClassName = TreeClassName;
1356     else if (ClassName != TreeClassName) {
1357       PrintFatalError(getOrigPatFragRecord()->getRecord()->getLoc(),
1358                       "PatFrags trees do not have consistent class");
1359     }
1360   }
1361 
1362   std::string Result;
1363   if (ClassName == "SDNode")
1364     Result = "    SDNode *N = Node;\n";
1365   else
1366     Result = "    auto *N = cast<" + ClassName.str() + ">(Node);\n";
1367 
1368   return (Twine(Result) + "    (void)N;\n" + getPredCode()).str();
1369 }
1370 
1371 //===----------------------------------------------------------------------===//
1372 // PatternToMatch implementation
1373 //
1374 
1375 static bool isImmAllOnesAllZerosMatch(const TreePatternNode *P) {
1376   if (!P->isLeaf())
1377     return false;
1378   DefInit *DI = dyn_cast<DefInit>(P->getLeafValue());
1379   if (!DI)
1380     return false;
1381 
1382   Record *R = DI->getDef();
1383   return R->getName() == "immAllOnesV" || R->getName() == "immAllZerosV";
1384 }
1385 
1386 /// getPatternSize - Return the 'size' of this pattern.  We want to match large
1387 /// patterns before small ones.  This is used to determine the size of a
1388 /// pattern.
1389 static unsigned getPatternSize(const TreePatternNode *P,
1390                                const CodeGenDAGPatterns &CGP) {
1391   unsigned Size = 3;  // The node itself.
1392   // If the root node is a ConstantSDNode, increases its size.
1393   // e.g. (set R32:$dst, 0).
1394   if (P->isLeaf() && isa<IntInit>(P->getLeafValue()))
1395     Size += 2;
1396 
1397   if (const ComplexPattern *AM = P->getComplexPatternInfo(CGP)) {
1398     Size += AM->getComplexity();
1399     // We don't want to count any children twice, so return early.
1400     return Size;
1401   }
1402 
1403   // If this node has some predicate function that must match, it adds to the
1404   // complexity of this node.
1405   if (!P->getPredicateCalls().empty())
1406     ++Size;
1407 
1408   // Count children in the count if they are also nodes.
1409   for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) {
1410     const TreePatternNode *Child = P->getChild(i);
1411     if (!Child->isLeaf() && Child->getNumTypes()) {
1412       const TypeSetByHwMode &T0 = Child->getExtType(0);
1413       // At this point, all variable type sets should be simple, i.e. only
1414       // have a default mode.
1415       if (T0.getMachineValueType() != MVT::Other) {
1416         Size += getPatternSize(Child, CGP);
1417         continue;
1418       }
1419     }
1420     if (Child->isLeaf()) {
1421       if (isa<IntInit>(Child->getLeafValue()))
1422         Size += 5;  // Matches a ConstantSDNode (+3) and a specific value (+2).
1423       else if (Child->getComplexPatternInfo(CGP))
1424         Size += getPatternSize(Child, CGP);
1425       else if (isImmAllOnesAllZerosMatch(Child))
1426         Size += 4; // Matches a build_vector(+3) and a predicate (+1).
1427       else if (!Child->getPredicateCalls().empty())
1428         ++Size;
1429     }
1430   }
1431 
1432   return Size;
1433 }
1434 
1435 /// Compute the complexity metric for the input pattern.  This roughly
1436 /// corresponds to the number of nodes that are covered.
1437 int PatternToMatch::
1438 getPatternComplexity(const CodeGenDAGPatterns &CGP) const {
1439   return getPatternSize(getSrcPattern(), CGP) + getAddedComplexity();
1440 }
1441 
1442 void PatternToMatch::getPredicateRecords(
1443     SmallVectorImpl<Record *> &PredicateRecs) const {
1444   for (Init *I : Predicates->getValues()) {
1445     if (DefInit *Pred = dyn_cast<DefInit>(I)) {
1446       Record *Def = Pred->getDef();
1447       if (!Def->isSubClassOf("Predicate")) {
1448 #ifndef NDEBUG
1449         Def->dump();
1450 #endif
1451         llvm_unreachable("Unknown predicate type!");
1452       }
1453       PredicateRecs.push_back(Def);
1454     }
1455   }
1456   // Sort so that different orders get canonicalized to the same string.
1457   llvm::sort(PredicateRecs, LessRecord());
1458 }
1459 
1460 /// getPredicateCheck - Return a single string containing all of this
1461 /// pattern's predicates concatenated with "&&" operators.
1462 ///
1463 std::string PatternToMatch::getPredicateCheck() const {
1464   SmallVector<Record *, 4> PredicateRecs;
1465   getPredicateRecords(PredicateRecs);
1466 
1467   SmallString<128> PredicateCheck;
1468   for (Record *Pred : PredicateRecs) {
1469     StringRef CondString = Pred->getValueAsString("CondString");
1470     if (CondString.empty())
1471       continue;
1472     if (!PredicateCheck.empty())
1473       PredicateCheck += " && ";
1474     PredicateCheck += "(";
1475     PredicateCheck += CondString;
1476     PredicateCheck += ")";
1477   }
1478 
1479   if (!HwModeFeatures.empty()) {
1480     if (!PredicateCheck.empty())
1481       PredicateCheck += " && ";
1482     PredicateCheck += HwModeFeatures;
1483   }
1484 
1485   return std::string(PredicateCheck);
1486 }
1487 
1488 //===----------------------------------------------------------------------===//
1489 // SDTypeConstraint implementation
1490 //
1491 
1492 SDTypeConstraint::SDTypeConstraint(Record *R, const CodeGenHwModes &CGH) {
1493   OperandNo = R->getValueAsInt("OperandNum");
1494 
1495   if (R->isSubClassOf("SDTCisVT")) {
1496     ConstraintType = SDTCisVT;
1497     VVT = getValueTypeByHwMode(R->getValueAsDef("VT"), CGH);
1498     for (const auto &P : VVT)
1499       if (P.second == MVT::isVoid)
1500         PrintFatalError(R->getLoc(), "Cannot use 'Void' as type to SDTCisVT");
1501   } else if (R->isSubClassOf("SDTCisPtrTy")) {
1502     ConstraintType = SDTCisPtrTy;
1503   } else if (R->isSubClassOf("SDTCisInt")) {
1504     ConstraintType = SDTCisInt;
1505   } else if (R->isSubClassOf("SDTCisFP")) {
1506     ConstraintType = SDTCisFP;
1507   } else if (R->isSubClassOf("SDTCisVec")) {
1508     ConstraintType = SDTCisVec;
1509   } else if (R->isSubClassOf("SDTCisSameAs")) {
1510     ConstraintType = SDTCisSameAs;
1511     x.SDTCisSameAs_Info.OtherOperandNum = R->getValueAsInt("OtherOperandNum");
1512   } else if (R->isSubClassOf("SDTCisVTSmallerThanOp")) {
1513     ConstraintType = SDTCisVTSmallerThanOp;
1514     x.SDTCisVTSmallerThanOp_Info.OtherOperandNum =
1515       R->getValueAsInt("OtherOperandNum");
1516   } else if (R->isSubClassOf("SDTCisOpSmallerThanOp")) {
1517     ConstraintType = SDTCisOpSmallerThanOp;
1518     x.SDTCisOpSmallerThanOp_Info.BigOperandNum =
1519       R->getValueAsInt("BigOperandNum");
1520   } else if (R->isSubClassOf("SDTCisEltOfVec")) {
1521     ConstraintType = SDTCisEltOfVec;
1522     x.SDTCisEltOfVec_Info.OtherOperandNum = R->getValueAsInt("OtherOpNum");
1523   } else if (R->isSubClassOf("SDTCisSubVecOfVec")) {
1524     ConstraintType = SDTCisSubVecOfVec;
1525     x.SDTCisSubVecOfVec_Info.OtherOperandNum =
1526       R->getValueAsInt("OtherOpNum");
1527   } else if (R->isSubClassOf("SDTCVecEltisVT")) {
1528     ConstraintType = SDTCVecEltisVT;
1529     VVT = getValueTypeByHwMode(R->getValueAsDef("VT"), CGH);
1530     for (const auto &P : VVT) {
1531       MVT T = P.second;
1532       if (T.isVector())
1533         PrintFatalError(R->getLoc(),
1534                         "Cannot use vector type as SDTCVecEltisVT");
1535       if (!T.isInteger() && !T.isFloatingPoint())
1536         PrintFatalError(R->getLoc(), "Must use integer or floating point type "
1537                                      "as SDTCVecEltisVT");
1538     }
1539   } else if (R->isSubClassOf("SDTCisSameNumEltsAs")) {
1540     ConstraintType = SDTCisSameNumEltsAs;
1541     x.SDTCisSameNumEltsAs_Info.OtherOperandNum =
1542       R->getValueAsInt("OtherOperandNum");
1543   } else if (R->isSubClassOf("SDTCisSameSizeAs")) {
1544     ConstraintType = SDTCisSameSizeAs;
1545     x.SDTCisSameSizeAs_Info.OtherOperandNum =
1546       R->getValueAsInt("OtherOperandNum");
1547   } else {
1548     PrintFatalError(R->getLoc(),
1549                     "Unrecognized SDTypeConstraint '" + R->getName() + "'!\n");
1550   }
1551 }
1552 
1553 /// getOperandNum - Return the node corresponding to operand #OpNo in tree
1554 /// N, and the result number in ResNo.
1555 static TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N,
1556                                       const SDNodeInfo &NodeInfo,
1557                                       unsigned &ResNo) {
1558   unsigned NumResults = NodeInfo.getNumResults();
1559   if (OpNo < NumResults) {
1560     ResNo = OpNo;
1561     return N;
1562   }
1563 
1564   OpNo -= NumResults;
1565 
1566   if (OpNo >= N->getNumChildren()) {
1567     std::string S;
1568     raw_string_ostream OS(S);
1569     OS << "Invalid operand number in type constraint "
1570            << (OpNo+NumResults) << " ";
1571     N->print(OS);
1572     PrintFatalError(OS.str());
1573   }
1574 
1575   return N->getChild(OpNo);
1576 }
1577 
1578 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
1579 /// constraint to the nodes operands.  This returns true if it makes a
1580 /// change, false otherwise.  If a type contradiction is found, flag an error.
1581 bool SDTypeConstraint::ApplyTypeConstraint(TreePatternNode *N,
1582                                            const SDNodeInfo &NodeInfo,
1583                                            TreePattern &TP) const {
1584   if (TP.hasError())
1585     return false;
1586 
1587   unsigned ResNo = 0; // The result number being referenced.
1588   TreePatternNode *NodeToApply = getOperandNum(OperandNo, N, NodeInfo, ResNo);
1589   TypeInfer &TI = TP.getInfer();
1590 
1591   switch (ConstraintType) {
1592   case SDTCisVT:
1593     // Operand must be a particular type.
1594     return NodeToApply->UpdateNodeType(ResNo, VVT, TP);
1595   case SDTCisPtrTy:
1596     // Operand must be same as target pointer type.
1597     return NodeToApply->UpdateNodeType(ResNo, MVT::iPTR, TP);
1598   case SDTCisInt:
1599     // Require it to be one of the legal integer VTs.
1600      return TI.EnforceInteger(NodeToApply->getExtType(ResNo));
1601   case SDTCisFP:
1602     // Require it to be one of the legal fp VTs.
1603     return TI.EnforceFloatingPoint(NodeToApply->getExtType(ResNo));
1604   case SDTCisVec:
1605     // Require it to be one of the legal vector VTs.
1606     return TI.EnforceVector(NodeToApply->getExtType(ResNo));
1607   case SDTCisSameAs: {
1608     unsigned OResNo = 0;
1609     TreePatternNode *OtherNode =
1610       getOperandNum(x.SDTCisSameAs_Info.OtherOperandNum, N, NodeInfo, OResNo);
1611     return NodeToApply->UpdateNodeType(ResNo, OtherNode->getExtType(OResNo),TP)|
1612            OtherNode->UpdateNodeType(OResNo,NodeToApply->getExtType(ResNo),TP);
1613   }
1614   case SDTCisVTSmallerThanOp: {
1615     // The NodeToApply must be a leaf node that is a VT.  OtherOperandNum must
1616     // have an integer type that is smaller than the VT.
1617     if (!NodeToApply->isLeaf() ||
1618         !isa<DefInit>(NodeToApply->getLeafValue()) ||
1619         !static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef()
1620                ->isSubClassOf("ValueType")) {
1621       TP.error(N->getOperator()->getName() + " expects a VT operand!");
1622       return false;
1623     }
1624     DefInit *DI = static_cast<DefInit*>(NodeToApply->getLeafValue());
1625     const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
1626     auto VVT = getValueTypeByHwMode(DI->getDef(), T.getHwModes());
1627     TypeSetByHwMode TypeListTmp(VVT);
1628 
1629     unsigned OResNo = 0;
1630     TreePatternNode *OtherNode =
1631       getOperandNum(x.SDTCisVTSmallerThanOp_Info.OtherOperandNum, N, NodeInfo,
1632                     OResNo);
1633 
1634     return TI.EnforceSmallerThan(TypeListTmp, OtherNode->getExtType(OResNo));
1635   }
1636   case SDTCisOpSmallerThanOp: {
1637     unsigned BResNo = 0;
1638     TreePatternNode *BigOperand =
1639       getOperandNum(x.SDTCisOpSmallerThanOp_Info.BigOperandNum, N, NodeInfo,
1640                     BResNo);
1641     return TI.EnforceSmallerThan(NodeToApply->getExtType(ResNo),
1642                                  BigOperand->getExtType(BResNo));
1643   }
1644   case SDTCisEltOfVec: {
1645     unsigned VResNo = 0;
1646     TreePatternNode *VecOperand =
1647       getOperandNum(x.SDTCisEltOfVec_Info.OtherOperandNum, N, NodeInfo,
1648                     VResNo);
1649     // Filter vector types out of VecOperand that don't have the right element
1650     // type.
1651     return TI.EnforceVectorEltTypeIs(VecOperand->getExtType(VResNo),
1652                                      NodeToApply->getExtType(ResNo));
1653   }
1654   case SDTCisSubVecOfVec: {
1655     unsigned VResNo = 0;
1656     TreePatternNode *BigVecOperand =
1657       getOperandNum(x.SDTCisSubVecOfVec_Info.OtherOperandNum, N, NodeInfo,
1658                     VResNo);
1659 
1660     // Filter vector types out of BigVecOperand that don't have the
1661     // right subvector type.
1662     return TI.EnforceVectorSubVectorTypeIs(BigVecOperand->getExtType(VResNo),
1663                                            NodeToApply->getExtType(ResNo));
1664   }
1665   case SDTCVecEltisVT: {
1666     return TI.EnforceVectorEltTypeIs(NodeToApply->getExtType(ResNo), VVT);
1667   }
1668   case SDTCisSameNumEltsAs: {
1669     unsigned OResNo = 0;
1670     TreePatternNode *OtherNode =
1671       getOperandNum(x.SDTCisSameNumEltsAs_Info.OtherOperandNum,
1672                     N, NodeInfo, OResNo);
1673     return TI.EnforceSameNumElts(OtherNode->getExtType(OResNo),
1674                                  NodeToApply->getExtType(ResNo));
1675   }
1676   case SDTCisSameSizeAs: {
1677     unsigned OResNo = 0;
1678     TreePatternNode *OtherNode =
1679       getOperandNum(x.SDTCisSameSizeAs_Info.OtherOperandNum,
1680                     N, NodeInfo, OResNo);
1681     return TI.EnforceSameSize(OtherNode->getExtType(OResNo),
1682                               NodeToApply->getExtType(ResNo));
1683   }
1684   }
1685   llvm_unreachable("Invalid ConstraintType!");
1686 }
1687 
1688 // Update the node type to match an instruction operand or result as specified
1689 // in the ins or outs lists on the instruction definition. Return true if the
1690 // type was actually changed.
1691 bool TreePatternNode::UpdateNodeTypeFromInst(unsigned ResNo,
1692                                              Record *Operand,
1693                                              TreePattern &TP) {
1694   // The 'unknown' operand indicates that types should be inferred from the
1695   // context.
1696   if (Operand->isSubClassOf("unknown_class"))
1697     return false;
1698 
1699   // The Operand class specifies a type directly.
1700   if (Operand->isSubClassOf("Operand")) {
1701     Record *R = Operand->getValueAsDef("Type");
1702     const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
1703     return UpdateNodeType(ResNo, getValueTypeByHwMode(R, T.getHwModes()), TP);
1704   }
1705 
1706   // PointerLikeRegClass has a type that is determined at runtime.
1707   if (Operand->isSubClassOf("PointerLikeRegClass"))
1708     return UpdateNodeType(ResNo, MVT::iPTR, TP);
1709 
1710   // Both RegisterClass and RegisterOperand operands derive their types from a
1711   // register class def.
1712   Record *RC = nullptr;
1713   if (Operand->isSubClassOf("RegisterClass"))
1714     RC = Operand;
1715   else if (Operand->isSubClassOf("RegisterOperand"))
1716     RC = Operand->getValueAsDef("RegClass");
1717 
1718   assert(RC && "Unknown operand type");
1719   CodeGenTarget &Tgt = TP.getDAGPatterns().getTargetInfo();
1720   return UpdateNodeType(ResNo, Tgt.getRegisterClass(RC).getValueTypes(), TP);
1721 }
1722 
1723 bool TreePatternNode::ContainsUnresolvedType(TreePattern &TP) const {
1724   for (unsigned i = 0, e = Types.size(); i != e; ++i)
1725     if (!TP.getInfer().isConcrete(Types[i], true))
1726       return true;
1727   for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1728     if (getChild(i)->ContainsUnresolvedType(TP))
1729       return true;
1730   return false;
1731 }
1732 
1733 bool TreePatternNode::hasProperTypeByHwMode() const {
1734   for (const TypeSetByHwMode &S : Types)
1735     if (!S.isDefaultOnly())
1736       return true;
1737   for (const TreePatternNodePtr &C : Children)
1738     if (C->hasProperTypeByHwMode())
1739       return true;
1740   return false;
1741 }
1742 
1743 bool TreePatternNode::hasPossibleType() const {
1744   for (const TypeSetByHwMode &S : Types)
1745     if (!S.isPossible())
1746       return false;
1747   for (const TreePatternNodePtr &C : Children)
1748     if (!C->hasPossibleType())
1749       return false;
1750   return true;
1751 }
1752 
1753 bool TreePatternNode::setDefaultMode(unsigned Mode) {
1754   for (TypeSetByHwMode &S : Types) {
1755     S.makeSimple(Mode);
1756     // Check if the selected mode had a type conflict.
1757     if (S.get(DefaultMode).empty())
1758       return false;
1759   }
1760   for (const TreePatternNodePtr &C : Children)
1761     if (!C->setDefaultMode(Mode))
1762       return false;
1763   return true;
1764 }
1765 
1766 //===----------------------------------------------------------------------===//
1767 // SDNodeInfo implementation
1768 //
1769 SDNodeInfo::SDNodeInfo(Record *R, const CodeGenHwModes &CGH) : Def(R) {
1770   EnumName    = R->getValueAsString("Opcode");
1771   SDClassName = R->getValueAsString("SDClass");
1772   Record *TypeProfile = R->getValueAsDef("TypeProfile");
1773   NumResults = TypeProfile->getValueAsInt("NumResults");
1774   NumOperands = TypeProfile->getValueAsInt("NumOperands");
1775 
1776   // Parse the properties.
1777   Properties = parseSDPatternOperatorProperties(R);
1778 
1779   // Parse the type constraints.
1780   std::vector<Record*> ConstraintList =
1781     TypeProfile->getValueAsListOfDefs("Constraints");
1782   for (Record *R : ConstraintList)
1783     TypeConstraints.emplace_back(R, CGH);
1784 }
1785 
1786 /// getKnownType - If the type constraints on this node imply a fixed type
1787 /// (e.g. all stores return void, etc), then return it as an
1788 /// MVT::SimpleValueType.  Otherwise, return EEVT::Other.
1789 MVT::SimpleValueType SDNodeInfo::getKnownType(unsigned ResNo) const {
1790   unsigned NumResults = getNumResults();
1791   assert(NumResults <= 1 &&
1792          "We only work with nodes with zero or one result so far!");
1793   assert(ResNo == 0 && "Only handles single result nodes so far");
1794 
1795   for (const SDTypeConstraint &Constraint : TypeConstraints) {
1796     // Make sure that this applies to the correct node result.
1797     if (Constraint.OperandNo >= NumResults)  // FIXME: need value #
1798       continue;
1799 
1800     switch (Constraint.ConstraintType) {
1801     default: break;
1802     case SDTypeConstraint::SDTCisVT:
1803       if (Constraint.VVT.isSimple())
1804         return Constraint.VVT.getSimple().SimpleTy;
1805       break;
1806     case SDTypeConstraint::SDTCisPtrTy:
1807       return MVT::iPTR;
1808     }
1809   }
1810   return MVT::Other;
1811 }
1812 
1813 //===----------------------------------------------------------------------===//
1814 // TreePatternNode implementation
1815 //
1816 
1817 static unsigned GetNumNodeResults(Record *Operator, CodeGenDAGPatterns &CDP) {
1818   if (Operator->getName() == "set" ||
1819       Operator->getName() == "implicit")
1820     return 0;  // All return nothing.
1821 
1822   if (Operator->isSubClassOf("Intrinsic"))
1823     return CDP.getIntrinsic(Operator).IS.RetVTs.size();
1824 
1825   if (Operator->isSubClassOf("SDNode"))
1826     return CDP.getSDNodeInfo(Operator).getNumResults();
1827 
1828   if (Operator->isSubClassOf("PatFrags")) {
1829     // If we've already parsed this pattern fragment, get it.  Otherwise, handle
1830     // the forward reference case where one pattern fragment references another
1831     // before it is processed.
1832     if (TreePattern *PFRec = CDP.getPatternFragmentIfRead(Operator)) {
1833       // The number of results of a fragment with alternative records is the
1834       // maximum number of results across all alternatives.
1835       unsigned NumResults = 0;
1836       for (const auto &T : PFRec->getTrees())
1837         NumResults = std::max(NumResults, T->getNumTypes());
1838       return NumResults;
1839     }
1840 
1841     ListInit *LI = Operator->getValueAsListInit("Fragments");
1842     assert(LI && "Invalid Fragment");
1843     unsigned NumResults = 0;
1844     for (Init *I : LI->getValues()) {
1845       Record *Op = nullptr;
1846       if (DagInit *Dag = dyn_cast<DagInit>(I))
1847         if (DefInit *DI = dyn_cast<DefInit>(Dag->getOperator()))
1848           Op = DI->getDef();
1849       assert(Op && "Invalid Fragment");
1850       NumResults = std::max(NumResults, GetNumNodeResults(Op, CDP));
1851     }
1852     return NumResults;
1853   }
1854 
1855   if (Operator->isSubClassOf("Instruction")) {
1856     CodeGenInstruction &InstInfo = CDP.getTargetInfo().getInstruction(Operator);
1857 
1858     unsigned NumDefsToAdd = InstInfo.Operands.NumDefs;
1859 
1860     // Subtract any defaulted outputs.
1861     for (unsigned i = 0; i != InstInfo.Operands.NumDefs; ++i) {
1862       Record *OperandNode = InstInfo.Operands[i].Rec;
1863 
1864       if (OperandNode->isSubClassOf("OperandWithDefaultOps") &&
1865           !CDP.getDefaultOperand(OperandNode).DefaultOps.empty())
1866         --NumDefsToAdd;
1867     }
1868 
1869     // Add on one implicit def if it has a resolvable type.
1870     if (InstInfo.HasOneImplicitDefWithKnownVT(CDP.getTargetInfo()) !=MVT::Other)
1871       ++NumDefsToAdd;
1872     return NumDefsToAdd;
1873   }
1874 
1875   if (Operator->isSubClassOf("SDNodeXForm"))
1876     return 1;  // FIXME: Generalize SDNodeXForm
1877 
1878   if (Operator->isSubClassOf("ValueType"))
1879     return 1;  // A type-cast of one result.
1880 
1881   if (Operator->isSubClassOf("ComplexPattern"))
1882     return 1;
1883 
1884   errs() << *Operator;
1885   PrintFatalError("Unhandled node in GetNumNodeResults");
1886 }
1887 
1888 void TreePatternNode::print(raw_ostream &OS) const {
1889   if (isLeaf())
1890     OS << *getLeafValue();
1891   else
1892     OS << '(' << getOperator()->getName();
1893 
1894   for (unsigned i = 0, e = Types.size(); i != e; ++i) {
1895     OS << ':';
1896     getExtType(i).writeToStream(OS);
1897   }
1898 
1899   if (!isLeaf()) {
1900     if (getNumChildren() != 0) {
1901       OS << " ";
1902       ListSeparator LS;
1903       for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
1904         OS << LS;
1905         getChild(i)->print(OS);
1906       }
1907     }
1908     OS << ")";
1909   }
1910 
1911   for (const TreePredicateCall &Pred : PredicateCalls) {
1912     OS << "<<P:";
1913     if (Pred.Scope)
1914       OS << Pred.Scope << ":";
1915     OS << Pred.Fn.getFnName() << ">>";
1916   }
1917   if (TransformFn)
1918     OS << "<<X:" << TransformFn->getName() << ">>";
1919   if (!getName().empty())
1920     OS << ":$" << getName();
1921 
1922   for (const ScopedName &Name : NamesAsPredicateArg)
1923     OS << ":$pred:" << Name.getScope() << ":" << Name.getIdentifier();
1924 }
1925 void TreePatternNode::dump() const {
1926   print(errs());
1927 }
1928 
1929 /// isIsomorphicTo - Return true if this node is recursively
1930 /// isomorphic to the specified node.  For this comparison, the node's
1931 /// entire state is considered. The assigned name is ignored, since
1932 /// nodes with differing names are considered isomorphic. However, if
1933 /// the assigned name is present in the dependent variable set, then
1934 /// the assigned name is considered significant and the node is
1935 /// isomorphic if the names match.
1936 bool TreePatternNode::isIsomorphicTo(const TreePatternNode *N,
1937                                      const MultipleUseVarSet &DepVars) const {
1938   if (N == this) return true;
1939   if (N->isLeaf() != isLeaf() || getExtTypes() != N->getExtTypes() ||
1940       getPredicateCalls() != N->getPredicateCalls() ||
1941       getTransformFn() != N->getTransformFn())
1942     return false;
1943 
1944   if (isLeaf()) {
1945     if (DefInit *DI = dyn_cast<DefInit>(getLeafValue())) {
1946       if (DefInit *NDI = dyn_cast<DefInit>(N->getLeafValue())) {
1947         return ((DI->getDef() == NDI->getDef())
1948                 && (DepVars.find(getName()) == DepVars.end()
1949                     || getName() == N->getName()));
1950       }
1951     }
1952     return getLeafValue() == N->getLeafValue();
1953   }
1954 
1955   if (N->getOperator() != getOperator() ||
1956       N->getNumChildren() != getNumChildren()) return false;
1957   for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1958     if (!getChild(i)->isIsomorphicTo(N->getChild(i), DepVars))
1959       return false;
1960   return true;
1961 }
1962 
1963 /// clone - Make a copy of this tree and all of its children.
1964 ///
1965 TreePatternNodePtr TreePatternNode::clone() const {
1966   TreePatternNodePtr New;
1967   if (isLeaf()) {
1968     New = std::make_shared<TreePatternNode>(getLeafValue(), getNumTypes());
1969   } else {
1970     std::vector<TreePatternNodePtr> CChildren;
1971     CChildren.reserve(Children.size());
1972     for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1973       CChildren.push_back(getChild(i)->clone());
1974     New = std::make_shared<TreePatternNode>(getOperator(), std::move(CChildren),
1975                                             getNumTypes());
1976   }
1977   New->setName(getName());
1978   New->setNamesAsPredicateArg(getNamesAsPredicateArg());
1979   New->Types = Types;
1980   New->setPredicateCalls(getPredicateCalls());
1981   New->setTransformFn(getTransformFn());
1982   return New;
1983 }
1984 
1985 /// RemoveAllTypes - Recursively strip all the types of this tree.
1986 void TreePatternNode::RemoveAllTypes() {
1987   // Reset to unknown type.
1988   std::fill(Types.begin(), Types.end(), TypeSetByHwMode());
1989   if (isLeaf()) return;
1990   for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
1991     getChild(i)->RemoveAllTypes();
1992 }
1993 
1994 
1995 /// SubstituteFormalArguments - Replace the formal arguments in this tree
1996 /// with actual values specified by ArgMap.
1997 void TreePatternNode::SubstituteFormalArguments(
1998     std::map<std::string, TreePatternNodePtr> &ArgMap) {
1999   if (isLeaf()) return;
2000 
2001   for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
2002     TreePatternNode *Child = getChild(i);
2003     if (Child->isLeaf()) {
2004       Init *Val = Child->getLeafValue();
2005       // Note that, when substituting into an output pattern, Val might be an
2006       // UnsetInit.
2007       if (isa<UnsetInit>(Val) || (isa<DefInit>(Val) &&
2008           cast<DefInit>(Val)->getDef()->getName() == "node")) {
2009         // We found a use of a formal argument, replace it with its value.
2010         TreePatternNodePtr NewChild = ArgMap[Child->getName()];
2011         assert(NewChild && "Couldn't find formal argument!");
2012         assert((Child->getPredicateCalls().empty() ||
2013                 NewChild->getPredicateCalls() == Child->getPredicateCalls()) &&
2014                "Non-empty child predicate clobbered!");
2015         setChild(i, std::move(NewChild));
2016       }
2017     } else {
2018       getChild(i)->SubstituteFormalArguments(ArgMap);
2019     }
2020   }
2021 }
2022 
2023 
2024 /// InlinePatternFragments - If this pattern refers to any pattern
2025 /// fragments, return the set of inlined versions (this can be more than
2026 /// one if a PatFrags record has multiple alternatives).
2027 void TreePatternNode::InlinePatternFragments(
2028   TreePatternNodePtr T, TreePattern &TP,
2029   std::vector<TreePatternNodePtr> &OutAlternatives) {
2030 
2031   if (TP.hasError())
2032     return;
2033 
2034   if (isLeaf()) {
2035     OutAlternatives.push_back(T);  // nothing to do.
2036     return;
2037   }
2038 
2039   Record *Op = getOperator();
2040 
2041   if (!Op->isSubClassOf("PatFrags")) {
2042     if (getNumChildren() == 0) {
2043       OutAlternatives.push_back(T);
2044       return;
2045     }
2046 
2047     // Recursively inline children nodes.
2048     std::vector<std::vector<TreePatternNodePtr> > ChildAlternatives;
2049     ChildAlternatives.resize(getNumChildren());
2050     for (unsigned i = 0, e = getNumChildren(); i != e; ++i) {
2051       TreePatternNodePtr Child = getChildShared(i);
2052       Child->InlinePatternFragments(Child, TP, ChildAlternatives[i]);
2053       // If there are no alternatives for any child, there are no
2054       // alternatives for this expression as whole.
2055       if (ChildAlternatives[i].empty())
2056         return;
2057 
2058       assert((Child->getPredicateCalls().empty() ||
2059               llvm::all_of(ChildAlternatives[i],
2060                            [&](const TreePatternNodePtr &NewChild) {
2061                              return NewChild->getPredicateCalls() ==
2062                                     Child->getPredicateCalls();
2063                            })) &&
2064              "Non-empty child predicate clobbered!");
2065     }
2066 
2067     // The end result is an all-pairs construction of the resultant pattern.
2068     std::vector<unsigned> Idxs;
2069     Idxs.resize(ChildAlternatives.size());
2070     bool NotDone;
2071     do {
2072       // Create the variant and add it to the output list.
2073       std::vector<TreePatternNodePtr> NewChildren;
2074       for (unsigned i = 0, e = ChildAlternatives.size(); i != e; ++i)
2075         NewChildren.push_back(ChildAlternatives[i][Idxs[i]]);
2076       TreePatternNodePtr R = std::make_shared<TreePatternNode>(
2077           getOperator(), std::move(NewChildren), getNumTypes());
2078 
2079       // Copy over properties.
2080       R->setName(getName());
2081       R->setNamesAsPredicateArg(getNamesAsPredicateArg());
2082       R->setPredicateCalls(getPredicateCalls());
2083       R->setTransformFn(getTransformFn());
2084       for (unsigned i = 0, e = getNumTypes(); i != e; ++i)
2085         R->setType(i, getExtType(i));
2086       for (unsigned i = 0, e = getNumResults(); i != e; ++i)
2087         R->setResultIndex(i, getResultIndex(i));
2088 
2089       // Register alternative.
2090       OutAlternatives.push_back(R);
2091 
2092       // Increment indices to the next permutation by incrementing the
2093       // indices from last index backward, e.g., generate the sequence
2094       // [0, 0], [0, 1], [1, 0], [1, 1].
2095       int IdxsIdx;
2096       for (IdxsIdx = Idxs.size() - 1; IdxsIdx >= 0; --IdxsIdx) {
2097         if (++Idxs[IdxsIdx] == ChildAlternatives[IdxsIdx].size())
2098           Idxs[IdxsIdx] = 0;
2099         else
2100           break;
2101       }
2102       NotDone = (IdxsIdx >= 0);
2103     } while (NotDone);
2104 
2105     return;
2106   }
2107 
2108   // Otherwise, we found a reference to a fragment.  First, look up its
2109   // TreePattern record.
2110   TreePattern *Frag = TP.getDAGPatterns().getPatternFragment(Op);
2111 
2112   // Verify that we are passing the right number of operands.
2113   if (Frag->getNumArgs() != Children.size()) {
2114     TP.error("'" + Op->getName() + "' fragment requires " +
2115              Twine(Frag->getNumArgs()) + " operands!");
2116     return;
2117   }
2118 
2119   TreePredicateFn PredFn(Frag);
2120   unsigned Scope = 0;
2121   if (TreePredicateFn(Frag).usesOperands())
2122     Scope = TP.getDAGPatterns().allocateScope();
2123 
2124   // Compute the map of formal to actual arguments.
2125   std::map<std::string, TreePatternNodePtr> ArgMap;
2126   for (unsigned i = 0, e = Frag->getNumArgs(); i != e; ++i) {
2127     TreePatternNodePtr Child = getChildShared(i);
2128     if (Scope != 0) {
2129       Child = Child->clone();
2130       Child->addNameAsPredicateArg(ScopedName(Scope, Frag->getArgName(i)));
2131     }
2132     ArgMap[Frag->getArgName(i)] = Child;
2133   }
2134 
2135   // Loop over all fragment alternatives.
2136   for (const auto &Alternative : Frag->getTrees()) {
2137     TreePatternNodePtr FragTree = Alternative->clone();
2138 
2139     if (!PredFn.isAlwaysTrue())
2140       FragTree->addPredicateCall(PredFn, Scope);
2141 
2142     // Resolve formal arguments to their actual value.
2143     if (Frag->getNumArgs())
2144       FragTree->SubstituteFormalArguments(ArgMap);
2145 
2146     // Transfer types.  Note that the resolved alternative may have fewer
2147     // (but not more) results than the PatFrags node.
2148     FragTree->setName(getName());
2149     for (unsigned i = 0, e = FragTree->getNumTypes(); i != e; ++i)
2150       FragTree->UpdateNodeType(i, getExtType(i), TP);
2151 
2152     // Transfer in the old predicates.
2153     for (const TreePredicateCall &Pred : getPredicateCalls())
2154       FragTree->addPredicateCall(Pred);
2155 
2156     // The fragment we inlined could have recursive inlining that is needed.  See
2157     // if there are any pattern fragments in it and inline them as needed.
2158     FragTree->InlinePatternFragments(FragTree, TP, OutAlternatives);
2159   }
2160 }
2161 
2162 /// getImplicitType - Check to see if the specified record has an implicit
2163 /// type which should be applied to it.  This will infer the type of register
2164 /// references from the register file information, for example.
2165 ///
2166 /// When Unnamed is set, return the type of a DAG operand with no name, such as
2167 /// the F8RC register class argument in:
2168 ///
2169 ///   (COPY_TO_REGCLASS GPR:$src, F8RC)
2170 ///
2171 /// When Unnamed is false, return the type of a named DAG operand such as the
2172 /// GPR:$src operand above.
2173 ///
2174 static TypeSetByHwMode getImplicitType(Record *R, unsigned ResNo,
2175                                        bool NotRegisters,
2176                                        bool Unnamed,
2177                                        TreePattern &TP) {
2178   CodeGenDAGPatterns &CDP = TP.getDAGPatterns();
2179 
2180   // Check to see if this is a register operand.
2181   if (R->isSubClassOf("RegisterOperand")) {
2182     assert(ResNo == 0 && "Regoperand ref only has one result!");
2183     if (NotRegisters)
2184       return TypeSetByHwMode(); // Unknown.
2185     Record *RegClass = R->getValueAsDef("RegClass");
2186     const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
2187     return TypeSetByHwMode(T.getRegisterClass(RegClass).getValueTypes());
2188   }
2189 
2190   // Check to see if this is a register or a register class.
2191   if (R->isSubClassOf("RegisterClass")) {
2192     assert(ResNo == 0 && "Regclass ref only has one result!");
2193     // An unnamed register class represents itself as an i32 immediate, for
2194     // example on a COPY_TO_REGCLASS instruction.
2195     if (Unnamed)
2196       return TypeSetByHwMode(MVT::i32);
2197 
2198     // In a named operand, the register class provides the possible set of
2199     // types.
2200     if (NotRegisters)
2201       return TypeSetByHwMode(); // Unknown.
2202     const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
2203     return TypeSetByHwMode(T.getRegisterClass(R).getValueTypes());
2204   }
2205 
2206   if (R->isSubClassOf("PatFrags")) {
2207     assert(ResNo == 0 && "FIXME: PatFrag with multiple results?");
2208     // Pattern fragment types will be resolved when they are inlined.
2209     return TypeSetByHwMode(); // Unknown.
2210   }
2211 
2212   if (R->isSubClassOf("Register")) {
2213     assert(ResNo == 0 && "Registers only produce one result!");
2214     if (NotRegisters)
2215       return TypeSetByHwMode(); // Unknown.
2216     const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo();
2217     return TypeSetByHwMode(T.getRegisterVTs(R));
2218   }
2219 
2220   if (R->isSubClassOf("SubRegIndex")) {
2221     assert(ResNo == 0 && "SubRegisterIndices only produce one result!");
2222     return TypeSetByHwMode(MVT::i32);
2223   }
2224 
2225   if (R->isSubClassOf("ValueType")) {
2226     assert(ResNo == 0 && "This node only has one result!");
2227     // An unnamed VTSDNode represents itself as an MVT::Other immediate.
2228     //
2229     //   (sext_inreg GPR:$src, i16)
2230     //                         ~~~
2231     if (Unnamed)
2232       return TypeSetByHwMode(MVT::Other);
2233     // With a name, the ValueType simply provides the type of the named
2234     // variable.
2235     //
2236     //   (sext_inreg i32:$src, i16)
2237     //               ~~~~~~~~
2238     if (NotRegisters)
2239       return TypeSetByHwMode(); // Unknown.
2240     const CodeGenHwModes &CGH = CDP.getTargetInfo().getHwModes();
2241     return TypeSetByHwMode(getValueTypeByHwMode(R, CGH));
2242   }
2243 
2244   if (R->isSubClassOf("CondCode")) {
2245     assert(ResNo == 0 && "This node only has one result!");
2246     // Using a CondCodeSDNode.
2247     return TypeSetByHwMode(MVT::Other);
2248   }
2249 
2250   if (R->isSubClassOf("ComplexPattern")) {
2251     assert(ResNo == 0 && "FIXME: ComplexPattern with multiple results?");
2252     if (NotRegisters)
2253       return TypeSetByHwMode(); // Unknown.
2254     return TypeSetByHwMode(CDP.getComplexPattern(R).getValueType());
2255   }
2256   if (R->isSubClassOf("PointerLikeRegClass")) {
2257     assert(ResNo == 0 && "Regclass can only have one result!");
2258     TypeSetByHwMode VTS(MVT::iPTR);
2259     TP.getInfer().expandOverloads(VTS);
2260     return VTS;
2261   }
2262 
2263   if (R->getName() == "node" || R->getName() == "srcvalue" ||
2264       R->getName() == "zero_reg" || R->getName() == "immAllOnesV" ||
2265       R->getName() == "immAllZerosV" || R->getName() == "undef_tied_input") {
2266     // Placeholder.
2267     return TypeSetByHwMode(); // Unknown.
2268   }
2269 
2270   if (R->isSubClassOf("Operand")) {
2271     const CodeGenHwModes &CGH = CDP.getTargetInfo().getHwModes();
2272     Record *T = R->getValueAsDef("Type");
2273     return TypeSetByHwMode(getValueTypeByHwMode(T, CGH));
2274   }
2275 
2276   TP.error("Unknown node flavor used in pattern: " + R->getName());
2277   return TypeSetByHwMode(MVT::Other);
2278 }
2279 
2280 
2281 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
2282 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
2283 const CodeGenIntrinsic *TreePatternNode::
2284 getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const {
2285   if (getOperator() != CDP.get_intrinsic_void_sdnode() &&
2286       getOperator() != CDP.get_intrinsic_w_chain_sdnode() &&
2287       getOperator() != CDP.get_intrinsic_wo_chain_sdnode())
2288     return nullptr;
2289 
2290   unsigned IID = cast<IntInit>(getChild(0)->getLeafValue())->getValue();
2291   return &CDP.getIntrinsicInfo(IID);
2292 }
2293 
2294 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
2295 /// return the ComplexPattern information, otherwise return null.
2296 const ComplexPattern *
2297 TreePatternNode::getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const {
2298   Record *Rec;
2299   if (isLeaf()) {
2300     DefInit *DI = dyn_cast<DefInit>(getLeafValue());
2301     if (!DI)
2302       return nullptr;
2303     Rec = DI->getDef();
2304   } else
2305     Rec = getOperator();
2306 
2307   if (!Rec->isSubClassOf("ComplexPattern"))
2308     return nullptr;
2309   return &CGP.getComplexPattern(Rec);
2310 }
2311 
2312 unsigned TreePatternNode::getNumMIResults(const CodeGenDAGPatterns &CGP) const {
2313   // A ComplexPattern specifically declares how many results it fills in.
2314   if (const ComplexPattern *CP = getComplexPatternInfo(CGP))
2315     return CP->getNumOperands();
2316 
2317   // If MIOperandInfo is specified, that gives the count.
2318   if (isLeaf()) {
2319     DefInit *DI = dyn_cast<DefInit>(getLeafValue());
2320     if (DI && DI->getDef()->isSubClassOf("Operand")) {
2321       DagInit *MIOps = DI->getDef()->getValueAsDag("MIOperandInfo");
2322       if (MIOps->getNumArgs())
2323         return MIOps->getNumArgs();
2324     }
2325   }
2326 
2327   // Otherwise there is just one result.
2328   return 1;
2329 }
2330 
2331 /// NodeHasProperty - Return true if this node has the specified property.
2332 bool TreePatternNode::NodeHasProperty(SDNP Property,
2333                                       const CodeGenDAGPatterns &CGP) const {
2334   if (isLeaf()) {
2335     if (const ComplexPattern *CP = getComplexPatternInfo(CGP))
2336       return CP->hasProperty(Property);
2337 
2338     return false;
2339   }
2340 
2341   if (Property != SDNPHasChain) {
2342     // The chain proprety is already present on the different intrinsic node
2343     // types (intrinsic_w_chain, intrinsic_void), and is not explicitly listed
2344     // on the intrinsic. Anything else is specific to the individual intrinsic.
2345     if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CGP))
2346       return Int->hasProperty(Property);
2347   }
2348 
2349   if (!Operator->isSubClassOf("SDPatternOperator"))
2350     return false;
2351 
2352   return CGP.getSDNodeInfo(Operator).hasProperty(Property);
2353 }
2354 
2355 
2356 
2357 
2358 /// TreeHasProperty - Return true if any node in this tree has the specified
2359 /// property.
2360 bool TreePatternNode::TreeHasProperty(SDNP Property,
2361                                       const CodeGenDAGPatterns &CGP) const {
2362   if (NodeHasProperty(Property, CGP))
2363     return true;
2364   for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
2365     if (getChild(i)->TreeHasProperty(Property, CGP))
2366       return true;
2367   return false;
2368 }
2369 
2370 /// isCommutativeIntrinsic - Return true if the node corresponds to a
2371 /// commutative intrinsic.
2372 bool
2373 TreePatternNode::isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const {
2374   if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP))
2375     return Int->isCommutative;
2376   return false;
2377 }
2378 
2379 static bool isOperandClass(const TreePatternNode *N, StringRef Class) {
2380   if (!N->isLeaf())
2381     return N->getOperator()->isSubClassOf(Class);
2382 
2383   DefInit *DI = dyn_cast<DefInit>(N->getLeafValue());
2384   if (DI && DI->getDef()->isSubClassOf(Class))
2385     return true;
2386 
2387   return false;
2388 }
2389 
2390 static void emitTooManyOperandsError(TreePattern &TP,
2391                                      StringRef InstName,
2392                                      unsigned Expected,
2393                                      unsigned Actual) {
2394   TP.error("Instruction '" + InstName + "' was provided " + Twine(Actual) +
2395            " operands but expected only " + Twine(Expected) + "!");
2396 }
2397 
2398 static void emitTooFewOperandsError(TreePattern &TP,
2399                                     StringRef InstName,
2400                                     unsigned Actual) {
2401   TP.error("Instruction '" + InstName +
2402            "' expects more than the provided " + Twine(Actual) + " operands!");
2403 }
2404 
2405 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
2406 /// this node and its children in the tree.  This returns true if it makes a
2407 /// change, false otherwise.  If a type contradiction is found, flag an error.
2408 bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) {
2409   if (TP.hasError())
2410     return false;
2411 
2412   CodeGenDAGPatterns &CDP = TP.getDAGPatterns();
2413   if (isLeaf()) {
2414     if (DefInit *DI = dyn_cast<DefInit>(getLeafValue())) {
2415       // If it's a regclass or something else known, include the type.
2416       bool MadeChange = false;
2417       for (unsigned i = 0, e = Types.size(); i != e; ++i)
2418         MadeChange |= UpdateNodeType(i, getImplicitType(DI->getDef(), i,
2419                                                         NotRegisters,
2420                                                         !hasName(), TP), TP);
2421       return MadeChange;
2422     }
2423 
2424     if (IntInit *II = dyn_cast<IntInit>(getLeafValue())) {
2425       assert(Types.size() == 1 && "Invalid IntInit");
2426 
2427       // Int inits are always integers. :)
2428       bool MadeChange = TP.getInfer().EnforceInteger(Types[0]);
2429 
2430       if (!TP.getInfer().isConcrete(Types[0], false))
2431         return MadeChange;
2432 
2433       ValueTypeByHwMode VVT = TP.getInfer().getConcrete(Types[0], false);
2434       for (auto &P : VVT) {
2435         MVT::SimpleValueType VT = P.second.SimpleTy;
2436         if (VT == MVT::iPTR || VT == MVT::iPTRAny)
2437           continue;
2438         unsigned Size = MVT(VT).getFixedSizeInBits();
2439         // Make sure that the value is representable for this type.
2440         if (Size >= 32)
2441           continue;
2442         // Check that the value doesn't use more bits than we have. It must
2443         // either be a sign- or zero-extended equivalent of the original.
2444         int64_t SignBitAndAbove = II->getValue() >> (Size - 1);
2445         if (SignBitAndAbove == -1 || SignBitAndAbove == 0 ||
2446             SignBitAndAbove == 1)
2447           continue;
2448 
2449         TP.error("Integer value '" + Twine(II->getValue()) +
2450                  "' is out of range for type '" + getEnumName(VT) + "'!");
2451         break;
2452       }
2453       return MadeChange;
2454     }
2455 
2456     return false;
2457   }
2458 
2459   if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP)) {
2460     bool MadeChange = false;
2461 
2462     // Apply the result type to the node.
2463     unsigned NumRetVTs = Int->IS.RetVTs.size();
2464     unsigned NumParamVTs = Int->IS.ParamVTs.size();
2465 
2466     for (unsigned i = 0, e = NumRetVTs; i != e; ++i)
2467       MadeChange |= UpdateNodeType(i, Int->IS.RetVTs[i], TP);
2468 
2469     if (getNumChildren() != NumParamVTs + 1) {
2470       TP.error("Intrinsic '" + Int->Name + "' expects " + Twine(NumParamVTs) +
2471                " operands, not " + Twine(getNumChildren() - 1) + " operands!");
2472       return false;
2473     }
2474 
2475     // Apply type info to the intrinsic ID.
2476     MadeChange |= getChild(0)->UpdateNodeType(0, MVT::iPTR, TP);
2477 
2478     for (unsigned i = 0, e = getNumChildren()-1; i != e; ++i) {
2479       MadeChange |= getChild(i+1)->ApplyTypeConstraints(TP, NotRegisters);
2480 
2481       MVT::SimpleValueType OpVT = Int->IS.ParamVTs[i];
2482       assert(getChild(i+1)->getNumTypes() == 1 && "Unhandled case");
2483       MadeChange |= getChild(i+1)->UpdateNodeType(0, OpVT, TP);
2484     }
2485     return MadeChange;
2486   }
2487 
2488   if (getOperator()->isSubClassOf("SDNode")) {
2489     const SDNodeInfo &NI = CDP.getSDNodeInfo(getOperator());
2490 
2491     // Check that the number of operands is sane.  Negative operands -> varargs.
2492     if (NI.getNumOperands() >= 0 &&
2493         getNumChildren() != (unsigned)NI.getNumOperands()) {
2494       TP.error(getOperator()->getName() + " node requires exactly " +
2495                Twine(NI.getNumOperands()) + " operands!");
2496       return false;
2497     }
2498 
2499     bool MadeChange = false;
2500     for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
2501       MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters);
2502     MadeChange |= NI.ApplyTypeConstraints(this, TP);
2503     return MadeChange;
2504   }
2505 
2506   if (getOperator()->isSubClassOf("Instruction")) {
2507     const DAGInstruction &Inst = CDP.getInstruction(getOperator());
2508     CodeGenInstruction &InstInfo =
2509       CDP.getTargetInfo().getInstruction(getOperator());
2510 
2511     bool MadeChange = false;
2512 
2513     // Apply the result types to the node, these come from the things in the
2514     // (outs) list of the instruction.
2515     unsigned NumResultsToAdd = std::min(InstInfo.Operands.NumDefs,
2516                                         Inst.getNumResults());
2517     for (unsigned ResNo = 0; ResNo != NumResultsToAdd; ++ResNo)
2518       MadeChange |= UpdateNodeTypeFromInst(ResNo, Inst.getResult(ResNo), TP);
2519 
2520     // If the instruction has implicit defs, we apply the first one as a result.
2521     // FIXME: This sucks, it should apply all implicit defs.
2522     if (!InstInfo.ImplicitDefs.empty()) {
2523       unsigned ResNo = NumResultsToAdd;
2524 
2525       // FIXME: Generalize to multiple possible types and multiple possible
2526       // ImplicitDefs.
2527       MVT::SimpleValueType VT =
2528         InstInfo.HasOneImplicitDefWithKnownVT(CDP.getTargetInfo());
2529 
2530       if (VT != MVT::Other)
2531         MadeChange |= UpdateNodeType(ResNo, VT, TP);
2532     }
2533 
2534     // If this is an INSERT_SUBREG, constrain the source and destination VTs to
2535     // be the same.
2536     if (getOperator()->getName() == "INSERT_SUBREG") {
2537       assert(getChild(0)->getNumTypes() == 1 && "FIXME: Unhandled");
2538       MadeChange |= UpdateNodeType(0, getChild(0)->getExtType(0), TP);
2539       MadeChange |= getChild(0)->UpdateNodeType(0, getExtType(0), TP);
2540     } else if (getOperator()->getName() == "REG_SEQUENCE") {
2541       // We need to do extra, custom typechecking for REG_SEQUENCE since it is
2542       // variadic.
2543 
2544       unsigned NChild = getNumChildren();
2545       if (NChild < 3) {
2546         TP.error("REG_SEQUENCE requires at least 3 operands!");
2547         return false;
2548       }
2549 
2550       if (NChild % 2 == 0) {
2551         TP.error("REG_SEQUENCE requires an odd number of operands!");
2552         return false;
2553       }
2554 
2555       if (!isOperandClass(getChild(0), "RegisterClass")) {
2556         TP.error("REG_SEQUENCE requires a RegisterClass for first operand!");
2557         return false;
2558       }
2559 
2560       for (unsigned I = 1; I < NChild; I += 2) {
2561         TreePatternNode *SubIdxChild = getChild(I + 1);
2562         if (!isOperandClass(SubIdxChild, "SubRegIndex")) {
2563           TP.error("REG_SEQUENCE requires a SubRegIndex for operand " +
2564                    Twine(I + 1) + "!");
2565           return false;
2566         }
2567       }
2568     }
2569 
2570     unsigned NumResults = Inst.getNumResults();
2571     unsigned NumFixedOperands = InstInfo.Operands.size();
2572 
2573     // If one or more operands with a default value appear at the end of the
2574     // formal operand list for an instruction, we allow them to be overridden
2575     // by optional operands provided in the pattern.
2576     //
2577     // But if an operand B without a default appears at any point after an
2578     // operand A with a default, then we don't allow A to be overridden,
2579     // because there would be no way to specify whether the next operand in
2580     // the pattern was intended to override A or skip it.
2581     unsigned NonOverridableOperands = NumFixedOperands;
2582     while (NonOverridableOperands > NumResults &&
2583            CDP.operandHasDefault(InstInfo.Operands[NonOverridableOperands-1].Rec))
2584       --NonOverridableOperands;
2585 
2586     unsigned ChildNo = 0;
2587     assert(NumResults <= NumFixedOperands);
2588     for (unsigned i = NumResults, e = NumFixedOperands; i != e; ++i) {
2589       Record *OperandNode = InstInfo.Operands[i].Rec;
2590 
2591       // If the operand has a default value, do we use it? We must use the
2592       // default if we've run out of children of the pattern DAG to consume,
2593       // or if the operand is followed by a non-defaulted one.
2594       if (CDP.operandHasDefault(OperandNode) &&
2595           (i < NonOverridableOperands || ChildNo >= getNumChildren()))
2596         continue;
2597 
2598       // If we have run out of child nodes and there _isn't_ a default
2599       // value we can use for the next operand, give an error.
2600       if (ChildNo >= getNumChildren()) {
2601         emitTooFewOperandsError(TP, getOperator()->getName(), getNumChildren());
2602         return false;
2603       }
2604 
2605       TreePatternNode *Child = getChild(ChildNo++);
2606       unsigned ChildResNo = 0;  // Instructions always use res #0 of their op.
2607 
2608       // If the operand has sub-operands, they may be provided by distinct
2609       // child patterns, so attempt to match each sub-operand separately.
2610       if (OperandNode->isSubClassOf("Operand")) {
2611         DagInit *MIOpInfo = OperandNode->getValueAsDag("MIOperandInfo");
2612         if (unsigned NumArgs = MIOpInfo->getNumArgs()) {
2613           // But don't do that if the whole operand is being provided by
2614           // a single ComplexPattern-related Operand.
2615 
2616           if (Child->getNumMIResults(CDP) < NumArgs) {
2617             // Match first sub-operand against the child we already have.
2618             Record *SubRec = cast<DefInit>(MIOpInfo->getArg(0))->getDef();
2619             MadeChange |=
2620               Child->UpdateNodeTypeFromInst(ChildResNo, SubRec, TP);
2621 
2622             // And the remaining sub-operands against subsequent children.
2623             for (unsigned Arg = 1; Arg < NumArgs; ++Arg) {
2624               if (ChildNo >= getNumChildren()) {
2625                 emitTooFewOperandsError(TP, getOperator()->getName(),
2626                                         getNumChildren());
2627                 return false;
2628               }
2629               Child = getChild(ChildNo++);
2630 
2631               SubRec = cast<DefInit>(MIOpInfo->getArg(Arg))->getDef();
2632               MadeChange |=
2633                 Child->UpdateNodeTypeFromInst(ChildResNo, SubRec, TP);
2634             }
2635             continue;
2636           }
2637         }
2638       }
2639 
2640       // If we didn't match by pieces above, attempt to match the whole
2641       // operand now.
2642       MadeChange |= Child->UpdateNodeTypeFromInst(ChildResNo, OperandNode, TP);
2643     }
2644 
2645     if (!InstInfo.Operands.isVariadic && ChildNo != getNumChildren()) {
2646       emitTooManyOperandsError(TP, getOperator()->getName(),
2647                                ChildNo, getNumChildren());
2648       return false;
2649     }
2650 
2651     for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
2652       MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters);
2653     return MadeChange;
2654   }
2655 
2656   if (getOperator()->isSubClassOf("ComplexPattern")) {
2657     bool MadeChange = false;
2658 
2659     for (unsigned i = 0; i < getNumChildren(); ++i)
2660       MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters);
2661 
2662     return MadeChange;
2663   }
2664 
2665   assert(getOperator()->isSubClassOf("SDNodeXForm") && "Unknown node type!");
2666 
2667   // Node transforms always take one operand.
2668   if (getNumChildren() != 1) {
2669     TP.error("Node transform '" + getOperator()->getName() +
2670              "' requires one operand!");
2671     return false;
2672   }
2673 
2674   bool MadeChange = getChild(0)->ApplyTypeConstraints(TP, NotRegisters);
2675   return MadeChange;
2676 }
2677 
2678 /// OnlyOnRHSOfCommutative - Return true if this value is only allowed on the
2679 /// RHS of a commutative operation, not the on LHS.
2680 static bool OnlyOnRHSOfCommutative(TreePatternNode *N) {
2681   if (!N->isLeaf() && N->getOperator()->getName() == "imm")
2682     return true;
2683   if (N->isLeaf() && isa<IntInit>(N->getLeafValue()))
2684     return true;
2685   if (isImmAllOnesAllZerosMatch(N))
2686     return true;
2687   return false;
2688 }
2689 
2690 
2691 /// canPatternMatch - If it is impossible for this pattern to match on this
2692 /// target, fill in Reason and return false.  Otherwise, return true.  This is
2693 /// used as a sanity check for .td files (to prevent people from writing stuff
2694 /// that can never possibly work), and to prevent the pattern permuter from
2695 /// generating stuff that is useless.
2696 bool TreePatternNode::canPatternMatch(std::string &Reason,
2697                                       const CodeGenDAGPatterns &CDP) {
2698   if (isLeaf()) return true;
2699 
2700   for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
2701     if (!getChild(i)->canPatternMatch(Reason, CDP))
2702       return false;
2703 
2704   // If this is an intrinsic, handle cases that would make it not match.  For
2705   // example, if an operand is required to be an immediate.
2706   if (getOperator()->isSubClassOf("Intrinsic")) {
2707     // TODO:
2708     return true;
2709   }
2710 
2711   if (getOperator()->isSubClassOf("ComplexPattern"))
2712     return true;
2713 
2714   // If this node is a commutative operator, check that the LHS isn't an
2715   // immediate.
2716   const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(getOperator());
2717   bool isCommIntrinsic = isCommutativeIntrinsic(CDP);
2718   if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) {
2719     // Scan all of the operands of the node and make sure that only the last one
2720     // is a constant node, unless the RHS also is.
2721     if (!OnlyOnRHSOfCommutative(getChild(getNumChildren()-1))) {
2722       unsigned Skip = isCommIntrinsic ? 1 : 0; // First operand is intrinsic id.
2723       for (unsigned i = Skip, e = getNumChildren()-1; i != e; ++i)
2724         if (OnlyOnRHSOfCommutative(getChild(i))) {
2725           Reason="Immediate value must be on the RHS of commutative operators!";
2726           return false;
2727         }
2728     }
2729   }
2730 
2731   return true;
2732 }
2733 
2734 //===----------------------------------------------------------------------===//
2735 // TreePattern implementation
2736 //
2737 
2738 TreePattern::TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
2739                          CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp),
2740                          isInputPattern(isInput), HasError(false),
2741                          Infer(*this) {
2742   for (Init *I : RawPat->getValues())
2743     Trees.push_back(ParseTreePattern(I, ""));
2744 }
2745 
2746 TreePattern::TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
2747                          CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp),
2748                          isInputPattern(isInput), HasError(false),
2749                          Infer(*this) {
2750   Trees.push_back(ParseTreePattern(Pat, ""));
2751 }
2752 
2753 TreePattern::TreePattern(Record *TheRec, TreePatternNodePtr Pat, bool isInput,
2754                          CodeGenDAGPatterns &cdp)
2755     : TheRecord(TheRec), CDP(cdp), isInputPattern(isInput), HasError(false),
2756       Infer(*this) {
2757   Trees.push_back(Pat);
2758 }
2759 
2760 void TreePattern::error(const Twine &Msg) {
2761   if (HasError)
2762     return;
2763   dump();
2764   PrintError(TheRecord->getLoc(), "In " + TheRecord->getName() + ": " + Msg);
2765   HasError = true;
2766 }
2767 
2768 void TreePattern::ComputeNamedNodes() {
2769   for (TreePatternNodePtr &Tree : Trees)
2770     ComputeNamedNodes(Tree.get());
2771 }
2772 
2773 void TreePattern::ComputeNamedNodes(TreePatternNode *N) {
2774   if (!N->getName().empty())
2775     NamedNodes[N->getName()].push_back(N);
2776 
2777   for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
2778     ComputeNamedNodes(N->getChild(i));
2779 }
2780 
2781 TreePatternNodePtr TreePattern::ParseTreePattern(Init *TheInit,
2782                                                  StringRef OpName) {
2783   if (DefInit *DI = dyn_cast<DefInit>(TheInit)) {
2784     Record *R = DI->getDef();
2785 
2786     // Direct reference to a leaf DagNode or PatFrag?  Turn it into a
2787     // TreePatternNode of its own.  For example:
2788     ///   (foo GPR, imm) -> (foo GPR, (imm))
2789     if (R->isSubClassOf("SDNode") || R->isSubClassOf("PatFrags"))
2790       return ParseTreePattern(
2791         DagInit::get(DI, nullptr,
2792                      std::vector<std::pair<Init*, StringInit*> >()),
2793         OpName);
2794 
2795     // Input argument?
2796     TreePatternNodePtr Res = std::make_shared<TreePatternNode>(DI, 1);
2797     if (R->getName() == "node" && !OpName.empty()) {
2798       if (OpName.empty())
2799         error("'node' argument requires a name to match with operand list");
2800       Args.push_back(std::string(OpName));
2801     }
2802 
2803     Res->setName(OpName);
2804     return Res;
2805   }
2806 
2807   // ?:$name or just $name.
2808   if (isa<UnsetInit>(TheInit)) {
2809     if (OpName.empty())
2810       error("'?' argument requires a name to match with operand list");
2811     TreePatternNodePtr Res = std::make_shared<TreePatternNode>(TheInit, 1);
2812     Args.push_back(std::string(OpName));
2813     Res->setName(OpName);
2814     return Res;
2815   }
2816 
2817   if (isa<IntInit>(TheInit) || isa<BitInit>(TheInit)) {
2818     if (!OpName.empty())
2819       error("Constant int or bit argument should not have a name!");
2820     if (isa<BitInit>(TheInit))
2821       TheInit = TheInit->convertInitializerTo(IntRecTy::get());
2822     return std::make_shared<TreePatternNode>(TheInit, 1);
2823   }
2824 
2825   if (BitsInit *BI = dyn_cast<BitsInit>(TheInit)) {
2826     // Turn this into an IntInit.
2827     Init *II = BI->convertInitializerTo(IntRecTy::get());
2828     if (!II || !isa<IntInit>(II))
2829       error("Bits value must be constants!");
2830     return ParseTreePattern(II, OpName);
2831   }
2832 
2833   DagInit *Dag = dyn_cast<DagInit>(TheInit);
2834   if (!Dag) {
2835     TheInit->print(errs());
2836     error("Pattern has unexpected init kind!");
2837   }
2838   DefInit *OpDef = dyn_cast<DefInit>(Dag->getOperator());
2839   if (!OpDef) error("Pattern has unexpected operator type!");
2840   Record *Operator = OpDef->getDef();
2841 
2842   if (Operator->isSubClassOf("ValueType")) {
2843     // If the operator is a ValueType, then this must be "type cast" of a leaf
2844     // node.
2845     if (Dag->getNumArgs() != 1)
2846       error("Type cast only takes one operand!");
2847 
2848     TreePatternNodePtr New =
2849         ParseTreePattern(Dag->getArg(0), Dag->getArgNameStr(0));
2850 
2851     // Apply the type cast.
2852     assert(New->getNumTypes() == 1 && "FIXME: Unhandled");
2853     const CodeGenHwModes &CGH = getDAGPatterns().getTargetInfo().getHwModes();
2854     New->UpdateNodeType(0, getValueTypeByHwMode(Operator, CGH), *this);
2855 
2856     if (!OpName.empty())
2857       error("ValueType cast should not have a name!");
2858     return New;
2859   }
2860 
2861   // Verify that this is something that makes sense for an operator.
2862   if (!Operator->isSubClassOf("PatFrags") &&
2863       !Operator->isSubClassOf("SDNode") &&
2864       !Operator->isSubClassOf("Instruction") &&
2865       !Operator->isSubClassOf("SDNodeXForm") &&
2866       !Operator->isSubClassOf("Intrinsic") &&
2867       !Operator->isSubClassOf("ComplexPattern") &&
2868       Operator->getName() != "set" &&
2869       Operator->getName() != "implicit")
2870     error("Unrecognized node '" + Operator->getName() + "'!");
2871 
2872   //  Check to see if this is something that is illegal in an input pattern.
2873   if (isInputPattern) {
2874     if (Operator->isSubClassOf("Instruction") ||
2875         Operator->isSubClassOf("SDNodeXForm"))
2876       error("Cannot use '" + Operator->getName() + "' in an input pattern!");
2877   } else {
2878     if (Operator->isSubClassOf("Intrinsic"))
2879       error("Cannot use '" + Operator->getName() + "' in an output pattern!");
2880 
2881     if (Operator->isSubClassOf("SDNode") &&
2882         Operator->getName() != "imm" &&
2883         Operator->getName() != "timm" &&
2884         Operator->getName() != "fpimm" &&
2885         Operator->getName() != "tglobaltlsaddr" &&
2886         Operator->getName() != "tconstpool" &&
2887         Operator->getName() != "tjumptable" &&
2888         Operator->getName() != "tframeindex" &&
2889         Operator->getName() != "texternalsym" &&
2890         Operator->getName() != "tblockaddress" &&
2891         Operator->getName() != "tglobaladdr" &&
2892         Operator->getName() != "bb" &&
2893         Operator->getName() != "vt" &&
2894         Operator->getName() != "mcsym")
2895       error("Cannot use '" + Operator->getName() + "' in an output pattern!");
2896   }
2897 
2898   std::vector<TreePatternNodePtr> Children;
2899 
2900   // Parse all the operands.
2901   for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i)
2902     Children.push_back(ParseTreePattern(Dag->getArg(i), Dag->getArgNameStr(i)));
2903 
2904   // Get the actual number of results before Operator is converted to an intrinsic
2905   // node (which is hard-coded to have either zero or one result).
2906   unsigned NumResults = GetNumNodeResults(Operator, CDP);
2907 
2908   // If the operator is an intrinsic, then this is just syntactic sugar for
2909   // (intrinsic_* <number>, ..children..).  Pick the right intrinsic node, and
2910   // convert the intrinsic name to a number.
2911   if (Operator->isSubClassOf("Intrinsic")) {
2912     const CodeGenIntrinsic &Int = getDAGPatterns().getIntrinsic(Operator);
2913     unsigned IID = getDAGPatterns().getIntrinsicID(Operator)+1;
2914 
2915     // If this intrinsic returns void, it must have side-effects and thus a
2916     // chain.
2917     if (Int.IS.RetVTs.empty())
2918       Operator = getDAGPatterns().get_intrinsic_void_sdnode();
2919     else if (Int.ModRef != CodeGenIntrinsic::NoMem || Int.hasSideEffects)
2920       // Has side-effects, requires chain.
2921       Operator = getDAGPatterns().get_intrinsic_w_chain_sdnode();
2922     else // Otherwise, no chain.
2923       Operator = getDAGPatterns().get_intrinsic_wo_chain_sdnode();
2924 
2925     Children.insert(Children.begin(),
2926                     std::make_shared<TreePatternNode>(IntInit::get(IID), 1));
2927   }
2928 
2929   if (Operator->isSubClassOf("ComplexPattern")) {
2930     for (unsigned i = 0; i < Children.size(); ++i) {
2931       TreePatternNodePtr Child = Children[i];
2932 
2933       if (Child->getName().empty())
2934         error("All arguments to a ComplexPattern must be named");
2935 
2936       // Check that the ComplexPattern uses are consistent: "(MY_PAT $a, $b)"
2937       // and "(MY_PAT $b, $a)" should not be allowed in the same pattern;
2938       // neither should "(MY_PAT_1 $a, $b)" and "(MY_PAT_2 $a, $b)".
2939       auto OperandId = std::make_pair(Operator, i);
2940       auto PrevOp = ComplexPatternOperands.find(Child->getName());
2941       if (PrevOp != ComplexPatternOperands.end()) {
2942         if (PrevOp->getValue() != OperandId)
2943           error("All ComplexPattern operands must appear consistently: "
2944                 "in the same order in just one ComplexPattern instance.");
2945       } else
2946         ComplexPatternOperands[Child->getName()] = OperandId;
2947     }
2948   }
2949 
2950   TreePatternNodePtr Result =
2951       std::make_shared<TreePatternNode>(Operator, std::move(Children),
2952                                         NumResults);
2953   Result->setName(OpName);
2954 
2955   if (Dag->getName()) {
2956     assert(Result->getName().empty());
2957     Result->setName(Dag->getNameStr());
2958   }
2959   return Result;
2960 }
2961 
2962 /// SimplifyTree - See if we can simplify this tree to eliminate something that
2963 /// will never match in favor of something obvious that will.  This is here
2964 /// strictly as a convenience to target authors because it allows them to write
2965 /// more type generic things and have useless type casts fold away.
2966 ///
2967 /// This returns true if any change is made.
2968 static bool SimplifyTree(TreePatternNodePtr &N) {
2969   if (N->isLeaf())
2970     return false;
2971 
2972   // If we have a bitconvert with a resolved type and if the source and
2973   // destination types are the same, then the bitconvert is useless, remove it.
2974   //
2975   // We make an exception if the types are completely empty. This can come up
2976   // when the pattern being simplified is in the Fragments list of a PatFrags,
2977   // so that the operand is just an untyped "node". In that situation we leave
2978   // bitconverts unsimplified, and simplify them later once the fragment is
2979   // expanded into its true context.
2980   if (N->getOperator()->getName() == "bitconvert" &&
2981       N->getExtType(0).isValueTypeByHwMode(false) &&
2982       !N->getExtType(0).empty() &&
2983       N->getExtType(0) == N->getChild(0)->getExtType(0) &&
2984       N->getName().empty()) {
2985     N = N->getChildShared(0);
2986     SimplifyTree(N);
2987     return true;
2988   }
2989 
2990   // Walk all children.
2991   bool MadeChange = false;
2992   for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
2993     TreePatternNodePtr Child = N->getChildShared(i);
2994     MadeChange |= SimplifyTree(Child);
2995     N->setChild(i, std::move(Child));
2996   }
2997   return MadeChange;
2998 }
2999 
3000 
3001 
3002 /// InferAllTypes - Infer/propagate as many types throughout the expression
3003 /// patterns as possible.  Return true if all types are inferred, false
3004 /// otherwise.  Flags an error if a type contradiction is found.
3005 bool TreePattern::
3006 InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> > *InNamedTypes) {
3007   if (NamedNodes.empty())
3008     ComputeNamedNodes();
3009 
3010   bool MadeChange = true;
3011   while (MadeChange) {
3012     MadeChange = false;
3013     for (TreePatternNodePtr &Tree : Trees) {
3014       MadeChange |= Tree->ApplyTypeConstraints(*this, false);
3015       MadeChange |= SimplifyTree(Tree);
3016     }
3017 
3018     // If there are constraints on our named nodes, apply them.
3019     for (auto &Entry : NamedNodes) {
3020       SmallVectorImpl<TreePatternNode*> &Nodes = Entry.second;
3021 
3022       // If we have input named node types, propagate their types to the named
3023       // values here.
3024       if (InNamedTypes) {
3025         if (!InNamedTypes->count(Entry.getKey())) {
3026           error("Node '" + std::string(Entry.getKey()) +
3027                 "' in output pattern but not input pattern");
3028           return true;
3029         }
3030 
3031         const SmallVectorImpl<TreePatternNode*> &InNodes =
3032           InNamedTypes->find(Entry.getKey())->second;
3033 
3034         // The input types should be fully resolved by now.
3035         for (TreePatternNode *Node : Nodes) {
3036           // If this node is a register class, and it is the root of the pattern
3037           // then we're mapping something onto an input register.  We allow
3038           // changing the type of the input register in this case.  This allows
3039           // us to match things like:
3040           //  def : Pat<(v1i64 (bitconvert(v2i32 DPR:$src))), (v1i64 DPR:$src)>;
3041           if (Node == Trees[0].get() && Node->isLeaf()) {
3042             DefInit *DI = dyn_cast<DefInit>(Node->getLeafValue());
3043             if (DI && (DI->getDef()->isSubClassOf("RegisterClass") ||
3044                        DI->getDef()->isSubClassOf("RegisterOperand")))
3045               continue;
3046           }
3047 
3048           assert(Node->getNumTypes() == 1 &&
3049                  InNodes[0]->getNumTypes() == 1 &&
3050                  "FIXME: cannot name multiple result nodes yet");
3051           MadeChange |= Node->UpdateNodeType(0, InNodes[0]->getExtType(0),
3052                                              *this);
3053         }
3054       }
3055 
3056       // If there are multiple nodes with the same name, they must all have the
3057       // same type.
3058       if (Entry.second.size() > 1) {
3059         for (unsigned i = 0, e = Nodes.size()-1; i != e; ++i) {
3060           TreePatternNode *N1 = Nodes[i], *N2 = Nodes[i+1];
3061           assert(N1->getNumTypes() == 1 && N2->getNumTypes() == 1 &&
3062                  "FIXME: cannot name multiple result nodes yet");
3063 
3064           MadeChange |= N1->UpdateNodeType(0, N2->getExtType(0), *this);
3065           MadeChange |= N2->UpdateNodeType(0, N1->getExtType(0), *this);
3066         }
3067       }
3068     }
3069   }
3070 
3071   bool HasUnresolvedTypes = false;
3072   for (const TreePatternNodePtr &Tree : Trees)
3073     HasUnresolvedTypes |= Tree->ContainsUnresolvedType(*this);
3074   return !HasUnresolvedTypes;
3075 }
3076 
3077 void TreePattern::print(raw_ostream &OS) const {
3078   OS << getRecord()->getName();
3079   if (!Args.empty()) {
3080     OS << "(";
3081     ListSeparator LS;
3082     for (const std::string &Arg : Args)
3083       OS << LS << Arg;
3084     OS << ")";
3085   }
3086   OS << ": ";
3087 
3088   if (Trees.size() > 1)
3089     OS << "[\n";
3090   for (const TreePatternNodePtr &Tree : Trees) {
3091     OS << "\t";
3092     Tree->print(OS);
3093     OS << "\n";
3094   }
3095 
3096   if (Trees.size() > 1)
3097     OS << "]\n";
3098 }
3099 
3100 void TreePattern::dump() const { print(errs()); }
3101 
3102 //===----------------------------------------------------------------------===//
3103 // CodeGenDAGPatterns implementation
3104 //
3105 
3106 CodeGenDAGPatterns::CodeGenDAGPatterns(RecordKeeper &R,
3107                                        PatternRewriterFn PatternRewriter)
3108     : Records(R), Target(R), LegalVTS(Target.getLegalValueTypes()),
3109       PatternRewriter(PatternRewriter) {
3110 
3111   Intrinsics = CodeGenIntrinsicTable(Records);
3112   ParseNodeInfo();
3113   ParseNodeTransforms();
3114   ParseComplexPatterns();
3115   ParsePatternFragments();
3116   ParseDefaultOperands();
3117   ParseInstructions();
3118   ParsePatternFragments(/*OutFrags*/true);
3119   ParsePatterns();
3120 
3121   // Generate variants.  For example, commutative patterns can match
3122   // multiple ways.  Add them to PatternsToMatch as well.
3123   GenerateVariants();
3124 
3125   // Break patterns with parameterized types into a series of patterns,
3126   // where each one has a fixed type and is predicated on the conditions
3127   // of the associated HW mode.
3128   ExpandHwModeBasedTypes();
3129 
3130   // Infer instruction flags.  For example, we can detect loads,
3131   // stores, and side effects in many cases by examining an
3132   // instruction's pattern.
3133   InferInstructionFlags();
3134 
3135   // Verify that instruction flags match the patterns.
3136   VerifyInstructionFlags();
3137 }
3138 
3139 Record *CodeGenDAGPatterns::getSDNodeNamed(StringRef Name) const {
3140   Record *N = Records.getDef(Name);
3141   if (!N || !N->isSubClassOf("SDNode"))
3142     PrintFatalError("Error getting SDNode '" + Name + "'!");
3143 
3144   return N;
3145 }
3146 
3147 // Parse all of the SDNode definitions for the target, populating SDNodes.
3148 void CodeGenDAGPatterns::ParseNodeInfo() {
3149   std::vector<Record*> Nodes = Records.getAllDerivedDefinitions("SDNode");
3150   const CodeGenHwModes &CGH = getTargetInfo().getHwModes();
3151 
3152   while (!Nodes.empty()) {
3153     Record *R = Nodes.back();
3154     SDNodes.insert(std::make_pair(R, SDNodeInfo(R, CGH)));
3155     Nodes.pop_back();
3156   }
3157 
3158   // Get the builtin intrinsic nodes.
3159   intrinsic_void_sdnode     = getSDNodeNamed("intrinsic_void");
3160   intrinsic_w_chain_sdnode  = getSDNodeNamed("intrinsic_w_chain");
3161   intrinsic_wo_chain_sdnode = getSDNodeNamed("intrinsic_wo_chain");
3162 }
3163 
3164 /// ParseNodeTransforms - Parse all SDNodeXForm instances into the SDNodeXForms
3165 /// map, and emit them to the file as functions.
3166 void CodeGenDAGPatterns::ParseNodeTransforms() {
3167   std::vector<Record*> Xforms = Records.getAllDerivedDefinitions("SDNodeXForm");
3168   while (!Xforms.empty()) {
3169     Record *XFormNode = Xforms.back();
3170     Record *SDNode = XFormNode->getValueAsDef("Opcode");
3171     StringRef Code = XFormNode->getValueAsString("XFormFunction");
3172     SDNodeXForms.insert(
3173         std::make_pair(XFormNode, NodeXForm(SDNode, std::string(Code))));
3174 
3175     Xforms.pop_back();
3176   }
3177 }
3178 
3179 void CodeGenDAGPatterns::ParseComplexPatterns() {
3180   std::vector<Record*> AMs = Records.getAllDerivedDefinitions("ComplexPattern");
3181   while (!AMs.empty()) {
3182     ComplexPatterns.insert(std::make_pair(AMs.back(), AMs.back()));
3183     AMs.pop_back();
3184   }
3185 }
3186 
3187 
3188 /// ParsePatternFragments - Parse all of the PatFrag definitions in the .td
3189 /// file, building up the PatternFragments map.  After we've collected them all,
3190 /// inline fragments together as necessary, so that there are no references left
3191 /// inside a pattern fragment to a pattern fragment.
3192 ///
3193 void CodeGenDAGPatterns::ParsePatternFragments(bool OutFrags) {
3194   std::vector<Record*> Fragments = Records.getAllDerivedDefinitions("PatFrags");
3195 
3196   // First step, parse all of the fragments.
3197   for (Record *Frag : Fragments) {
3198     if (OutFrags != Frag->isSubClassOf("OutPatFrag"))
3199       continue;
3200 
3201     ListInit *LI = Frag->getValueAsListInit("Fragments");
3202     TreePattern *P =
3203         (PatternFragments[Frag] = std::make_unique<TreePattern>(
3204              Frag, LI, !Frag->isSubClassOf("OutPatFrag"),
3205              *this)).get();
3206 
3207     // Validate the argument list, converting it to set, to discard duplicates.
3208     std::vector<std::string> &Args = P->getArgList();
3209     // Copy the args so we can take StringRefs to them.
3210     auto ArgsCopy = Args;
3211     SmallDenseSet<StringRef, 4> OperandsSet;
3212     OperandsSet.insert(ArgsCopy.begin(), ArgsCopy.end());
3213 
3214     if (OperandsSet.count(""))
3215       P->error("Cannot have unnamed 'node' values in pattern fragment!");
3216 
3217     // Parse the operands list.
3218     DagInit *OpsList = Frag->getValueAsDag("Operands");
3219     DefInit *OpsOp = dyn_cast<DefInit>(OpsList->getOperator());
3220     // Special cases: ops == outs == ins. Different names are used to
3221     // improve readability.
3222     if (!OpsOp ||
3223         (OpsOp->getDef()->getName() != "ops" &&
3224          OpsOp->getDef()->getName() != "outs" &&
3225          OpsOp->getDef()->getName() != "ins"))
3226       P->error("Operands list should start with '(ops ... '!");
3227 
3228     // Copy over the arguments.
3229     Args.clear();
3230     for (unsigned j = 0, e = OpsList->getNumArgs(); j != e; ++j) {
3231       if (!isa<DefInit>(OpsList->getArg(j)) ||
3232           cast<DefInit>(OpsList->getArg(j))->getDef()->getName() != "node")
3233         P->error("Operands list should all be 'node' values.");
3234       if (!OpsList->getArgName(j))
3235         P->error("Operands list should have names for each operand!");
3236       StringRef ArgNameStr = OpsList->getArgNameStr(j);
3237       if (!OperandsSet.count(ArgNameStr))
3238         P->error("'" + ArgNameStr +
3239                  "' does not occur in pattern or was multiply specified!");
3240       OperandsSet.erase(ArgNameStr);
3241       Args.push_back(std::string(ArgNameStr));
3242     }
3243 
3244     if (!OperandsSet.empty())
3245       P->error("Operands list does not contain an entry for operand '" +
3246                *OperandsSet.begin() + "'!");
3247 
3248     // If there is a node transformation corresponding to this, keep track of
3249     // it.
3250     Record *Transform = Frag->getValueAsDef("OperandTransform");
3251     if (!getSDNodeTransform(Transform).second.empty())    // not noop xform?
3252       for (const auto &T : P->getTrees())
3253         T->setTransformFn(Transform);
3254   }
3255 
3256   // Now that we've parsed all of the tree fragments, do a closure on them so
3257   // that there are not references to PatFrags left inside of them.
3258   for (Record *Frag : Fragments) {
3259     if (OutFrags != Frag->isSubClassOf("OutPatFrag"))
3260       continue;
3261 
3262     TreePattern &ThePat = *PatternFragments[Frag];
3263     ThePat.InlinePatternFragments();
3264 
3265     // Infer as many types as possible.  Don't worry about it if we don't infer
3266     // all of them, some may depend on the inputs of the pattern.  Also, don't
3267     // validate type sets; validation may cause spurious failures e.g. if a
3268     // fragment needs floating-point types but the current target does not have
3269     // any (this is only an error if that fragment is ever used!).
3270     {
3271       TypeInfer::SuppressValidation SV(ThePat.getInfer());
3272       ThePat.InferAllTypes();
3273       ThePat.resetError();
3274     }
3275 
3276     // If debugging, print out the pattern fragment result.
3277     LLVM_DEBUG(ThePat.dump());
3278   }
3279 }
3280 
3281 void CodeGenDAGPatterns::ParseDefaultOperands() {
3282   std::vector<Record*> DefaultOps;
3283   DefaultOps = Records.getAllDerivedDefinitions("OperandWithDefaultOps");
3284 
3285   // Find some SDNode.
3286   assert(!SDNodes.empty() && "No SDNodes parsed?");
3287   Init *SomeSDNode = DefInit::get(SDNodes.begin()->first);
3288 
3289   for (unsigned i = 0, e = DefaultOps.size(); i != e; ++i) {
3290     DagInit *DefaultInfo = DefaultOps[i]->getValueAsDag("DefaultOps");
3291 
3292     // Clone the DefaultInfo dag node, changing the operator from 'ops' to
3293     // SomeSDnode so that we can parse this.
3294     std::vector<std::pair<Init*, StringInit*> > Ops;
3295     for (unsigned op = 0, e = DefaultInfo->getNumArgs(); op != e; ++op)
3296       Ops.push_back(std::make_pair(DefaultInfo->getArg(op),
3297                                    DefaultInfo->getArgName(op)));
3298     DagInit *DI = DagInit::get(SomeSDNode, nullptr, Ops);
3299 
3300     // Create a TreePattern to parse this.
3301     TreePattern P(DefaultOps[i], DI, false, *this);
3302     assert(P.getNumTrees() == 1 && "This ctor can only produce one tree!");
3303 
3304     // Copy the operands over into a DAGDefaultOperand.
3305     DAGDefaultOperand DefaultOpInfo;
3306 
3307     const TreePatternNodePtr &T = P.getTree(0);
3308     for (unsigned op = 0, e = T->getNumChildren(); op != e; ++op) {
3309       TreePatternNodePtr TPN = T->getChildShared(op);
3310       while (TPN->ApplyTypeConstraints(P, false))
3311         /* Resolve all types */;
3312 
3313       if (TPN->ContainsUnresolvedType(P)) {
3314         PrintFatalError("Value #" + Twine(i) + " of OperandWithDefaultOps '" +
3315                         DefaultOps[i]->getName() +
3316                         "' doesn't have a concrete type!");
3317       }
3318       DefaultOpInfo.DefaultOps.push_back(std::move(TPN));
3319     }
3320 
3321     // Insert it into the DefaultOperands map so we can find it later.
3322     DefaultOperands[DefaultOps[i]] = DefaultOpInfo;
3323   }
3324 }
3325 
3326 /// HandleUse - Given "Pat" a leaf in the pattern, check to see if it is an
3327 /// instruction input.  Return true if this is a real use.
3328 static bool HandleUse(TreePattern &I, TreePatternNodePtr Pat,
3329                       std::map<std::string, TreePatternNodePtr> &InstInputs) {
3330   // No name -> not interesting.
3331   if (Pat->getName().empty()) {
3332     if (Pat->isLeaf()) {
3333       DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue());
3334       if (DI && (DI->getDef()->isSubClassOf("RegisterClass") ||
3335                  DI->getDef()->isSubClassOf("RegisterOperand")))
3336         I.error("Input " + DI->getDef()->getName() + " must be named!");
3337     }
3338     return false;
3339   }
3340 
3341   Record *Rec;
3342   if (Pat->isLeaf()) {
3343     DefInit *DI = dyn_cast<DefInit>(Pat->getLeafValue());
3344     if (!DI)
3345       I.error("Input $" + Pat->getName() + " must be an identifier!");
3346     Rec = DI->getDef();
3347   } else {
3348     Rec = Pat->getOperator();
3349   }
3350 
3351   // SRCVALUE nodes are ignored.
3352   if (Rec->getName() == "srcvalue")
3353     return false;
3354 
3355   TreePatternNodePtr &Slot = InstInputs[Pat->getName()];
3356   if (!Slot) {
3357     Slot = Pat;
3358     return true;
3359   }
3360   Record *SlotRec;
3361   if (Slot->isLeaf()) {
3362     SlotRec = cast<DefInit>(Slot->getLeafValue())->getDef();
3363   } else {
3364     assert(Slot->getNumChildren() == 0 && "can't be a use with children!");
3365     SlotRec = Slot->getOperator();
3366   }
3367 
3368   // Ensure that the inputs agree if we've already seen this input.
3369   if (Rec != SlotRec)
3370     I.error("All $" + Pat->getName() + " inputs must agree with each other");
3371   // Ensure that the types can agree as well.
3372   Slot->UpdateNodeType(0, Pat->getExtType(0), I);
3373   Pat->UpdateNodeType(0, Slot->getExtType(0), I);
3374   if (Slot->getExtTypes() != Pat->getExtTypes())
3375     I.error("All $" + Pat->getName() + " inputs must agree with each other");
3376   return true;
3377 }
3378 
3379 /// FindPatternInputsAndOutputs - Scan the specified TreePatternNode (which is
3380 /// part of "I", the instruction), computing the set of inputs and outputs of
3381 /// the pattern.  Report errors if we see anything naughty.
3382 void CodeGenDAGPatterns::FindPatternInputsAndOutputs(
3383     TreePattern &I, TreePatternNodePtr Pat,
3384     std::map<std::string, TreePatternNodePtr> &InstInputs,
3385     MapVector<std::string, TreePatternNodePtr, std::map<std::string, unsigned>>
3386         &InstResults,
3387     std::vector<Record *> &InstImpResults) {
3388 
3389   // The instruction pattern still has unresolved fragments.  For *named*
3390   // nodes we must resolve those here.  This may not result in multiple
3391   // alternatives.
3392   if (!Pat->getName().empty()) {
3393     TreePattern SrcPattern(I.getRecord(), Pat, true, *this);
3394     SrcPattern.InlinePatternFragments();
3395     SrcPattern.InferAllTypes();
3396     Pat = SrcPattern.getOnlyTree();
3397   }
3398 
3399   if (Pat->isLeaf()) {
3400     bool isUse = HandleUse(I, Pat, InstInputs);
3401     if (!isUse && Pat->getTransformFn())
3402       I.error("Cannot specify a transform function for a non-input value!");
3403     return;
3404   }
3405 
3406   if (Pat->getOperator()->getName() == "implicit") {
3407     for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) {
3408       TreePatternNode *Dest = Pat->getChild(i);
3409       if (!Dest->isLeaf())
3410         I.error("implicitly defined value should be a register!");
3411 
3412       DefInit *Val = dyn_cast<DefInit>(Dest->getLeafValue());
3413       if (!Val || !Val->getDef()->isSubClassOf("Register"))
3414         I.error("implicitly defined value should be a register!");
3415       InstImpResults.push_back(Val->getDef());
3416     }
3417     return;
3418   }
3419 
3420   if (Pat->getOperator()->getName() != "set") {
3421     // If this is not a set, verify that the children nodes are not void typed,
3422     // and recurse.
3423     for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) {
3424       if (Pat->getChild(i)->getNumTypes() == 0)
3425         I.error("Cannot have void nodes inside of patterns!");
3426       FindPatternInputsAndOutputs(I, Pat->getChildShared(i), InstInputs,
3427                                   InstResults, InstImpResults);
3428     }
3429 
3430     // If this is a non-leaf node with no children, treat it basically as if
3431     // it were a leaf.  This handles nodes like (imm).
3432     bool isUse = HandleUse(I, Pat, InstInputs);
3433 
3434     if (!isUse && Pat->getTransformFn())
3435       I.error("Cannot specify a transform function for a non-input value!");
3436     return;
3437   }
3438 
3439   // Otherwise, this is a set, validate and collect instruction results.
3440   if (Pat->getNumChildren() == 0)
3441     I.error("set requires operands!");
3442 
3443   if (Pat->getTransformFn())
3444     I.error("Cannot specify a transform function on a set node!");
3445 
3446   // Check the set destinations.
3447   unsigned NumDests = Pat->getNumChildren()-1;
3448   for (unsigned i = 0; i != NumDests; ++i) {
3449     TreePatternNodePtr Dest = Pat->getChildShared(i);
3450     // For set destinations we also must resolve fragments here.
3451     TreePattern DestPattern(I.getRecord(), Dest, false, *this);
3452     DestPattern.InlinePatternFragments();
3453     DestPattern.InferAllTypes();
3454     Dest = DestPattern.getOnlyTree();
3455 
3456     if (!Dest->isLeaf())
3457       I.error("set destination should be a register!");
3458 
3459     DefInit *Val = dyn_cast<DefInit>(Dest->getLeafValue());
3460     if (!Val) {
3461       I.error("set destination should be a register!");
3462       continue;
3463     }
3464 
3465     if (Val->getDef()->isSubClassOf("RegisterClass") ||
3466         Val->getDef()->isSubClassOf("ValueType") ||
3467         Val->getDef()->isSubClassOf("RegisterOperand") ||
3468         Val->getDef()->isSubClassOf("PointerLikeRegClass")) {
3469       if (Dest->getName().empty())
3470         I.error("set destination must have a name!");
3471       if (InstResults.count(Dest->getName()))
3472         I.error("cannot set '" + Dest->getName() + "' multiple times");
3473       InstResults[Dest->getName()] = Dest;
3474     } else if (Val->getDef()->isSubClassOf("Register")) {
3475       InstImpResults.push_back(Val->getDef());
3476     } else {
3477       I.error("set destination should be a register!");
3478     }
3479   }
3480 
3481   // Verify and collect info from the computation.
3482   FindPatternInputsAndOutputs(I, Pat->getChildShared(NumDests), InstInputs,
3483                               InstResults, InstImpResults);
3484 }
3485 
3486 //===----------------------------------------------------------------------===//
3487 // Instruction Analysis
3488 //===----------------------------------------------------------------------===//
3489 
3490 class InstAnalyzer {
3491   const CodeGenDAGPatterns &CDP;
3492 public:
3493   bool hasSideEffects;
3494   bool mayStore;
3495   bool mayLoad;
3496   bool isBitcast;
3497   bool isVariadic;
3498   bool hasChain;
3499 
3500   InstAnalyzer(const CodeGenDAGPatterns &cdp)
3501     : CDP(cdp), hasSideEffects(false), mayStore(false), mayLoad(false),
3502       isBitcast(false), isVariadic(false), hasChain(false) {}
3503 
3504   void Analyze(const PatternToMatch &Pat) {
3505     const TreePatternNode *N = Pat.getSrcPattern();
3506     AnalyzeNode(N);
3507     // These properties are detected only on the root node.
3508     isBitcast = IsNodeBitcast(N);
3509   }
3510 
3511 private:
3512   bool IsNodeBitcast(const TreePatternNode *N) const {
3513     if (hasSideEffects || mayLoad || mayStore || isVariadic)
3514       return false;
3515 
3516     if (N->isLeaf())
3517       return false;
3518     if (N->getNumChildren() != 1 || !N->getChild(0)->isLeaf())
3519       return false;
3520 
3521     if (N->getOperator()->isSubClassOf("ComplexPattern"))
3522       return false;
3523 
3524     const SDNodeInfo &OpInfo = CDP.getSDNodeInfo(N->getOperator());
3525     if (OpInfo.getNumResults() != 1 || OpInfo.getNumOperands() != 1)
3526       return false;
3527     return OpInfo.getEnumName() == "ISD::BITCAST";
3528   }
3529 
3530 public:
3531   void AnalyzeNode(const TreePatternNode *N) {
3532     if (N->isLeaf()) {
3533       if (DefInit *DI = dyn_cast<DefInit>(N->getLeafValue())) {
3534         Record *LeafRec = DI->getDef();
3535         // Handle ComplexPattern leaves.
3536         if (LeafRec->isSubClassOf("ComplexPattern")) {
3537           const ComplexPattern &CP = CDP.getComplexPattern(LeafRec);
3538           if (CP.hasProperty(SDNPMayStore)) mayStore = true;
3539           if (CP.hasProperty(SDNPMayLoad)) mayLoad = true;
3540           if (CP.hasProperty(SDNPSideEffect)) hasSideEffects = true;
3541         }
3542       }
3543       return;
3544     }
3545 
3546     // Analyze children.
3547     for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
3548       AnalyzeNode(N->getChild(i));
3549 
3550     // Notice properties of the node.
3551     if (N->NodeHasProperty(SDNPMayStore, CDP)) mayStore = true;
3552     if (N->NodeHasProperty(SDNPMayLoad, CDP)) mayLoad = true;
3553     if (N->NodeHasProperty(SDNPSideEffect, CDP)) hasSideEffects = true;
3554     if (N->NodeHasProperty(SDNPVariadic, CDP)) isVariadic = true;
3555     if (N->NodeHasProperty(SDNPHasChain, CDP)) hasChain = true;
3556 
3557     if (const CodeGenIntrinsic *IntInfo = N->getIntrinsicInfo(CDP)) {
3558       // If this is an intrinsic, analyze it.
3559       if (IntInfo->ModRef & CodeGenIntrinsic::MR_Ref)
3560         mayLoad = true;// These may load memory.
3561 
3562       if (IntInfo->ModRef & CodeGenIntrinsic::MR_Mod)
3563         mayStore = true;// Intrinsics that can write to memory are 'mayStore'.
3564 
3565       if (IntInfo->ModRef >= CodeGenIntrinsic::ReadWriteMem ||
3566           IntInfo->hasSideEffects)
3567         // ReadWriteMem intrinsics can have other strange effects.
3568         hasSideEffects = true;
3569     }
3570   }
3571 
3572 };
3573 
3574 static bool InferFromPattern(CodeGenInstruction &InstInfo,
3575                              const InstAnalyzer &PatInfo,
3576                              Record *PatDef) {
3577   bool Error = false;
3578 
3579   // Remember where InstInfo got its flags.
3580   if (InstInfo.hasUndefFlags())
3581       InstInfo.InferredFrom = PatDef;
3582 
3583   // Check explicitly set flags for consistency.
3584   if (InstInfo.hasSideEffects != PatInfo.hasSideEffects &&
3585       !InstInfo.hasSideEffects_Unset) {
3586     // Allow explicitly setting hasSideEffects = 1 on instructions, even when
3587     // the pattern has no side effects. That could be useful for div/rem
3588     // instructions that may trap.
3589     if (!InstInfo.hasSideEffects) {
3590       Error = true;
3591       PrintError(PatDef->getLoc(), "Pattern doesn't match hasSideEffects = " +
3592                  Twine(InstInfo.hasSideEffects));
3593     }
3594   }
3595 
3596   if (InstInfo.mayStore != PatInfo.mayStore && !InstInfo.mayStore_Unset) {
3597     Error = true;
3598     PrintError(PatDef->getLoc(), "Pattern doesn't match mayStore = " +
3599                Twine(InstInfo.mayStore));
3600   }
3601 
3602   if (InstInfo.mayLoad != PatInfo.mayLoad && !InstInfo.mayLoad_Unset) {
3603     // Allow explicitly setting mayLoad = 1, even when the pattern has no loads.
3604     // Some targets translate immediates to loads.
3605     if (!InstInfo.mayLoad) {
3606       Error = true;
3607       PrintError(PatDef->getLoc(), "Pattern doesn't match mayLoad = " +
3608                  Twine(InstInfo.mayLoad));
3609     }
3610   }
3611 
3612   // Transfer inferred flags.
3613   InstInfo.hasSideEffects |= PatInfo.hasSideEffects;
3614   InstInfo.mayStore |= PatInfo.mayStore;
3615   InstInfo.mayLoad |= PatInfo.mayLoad;
3616 
3617   // These flags are silently added without any verification.
3618   // FIXME: To match historical behavior of TableGen, for now add those flags
3619   // only when we're inferring from the primary instruction pattern.
3620   if (PatDef->isSubClassOf("Instruction")) {
3621     InstInfo.isBitcast |= PatInfo.isBitcast;
3622     InstInfo.hasChain |= PatInfo.hasChain;
3623     InstInfo.hasChain_Inferred = true;
3624   }
3625 
3626   // Don't infer isVariadic. This flag means something different on SDNodes and
3627   // instructions. For example, a CALL SDNode is variadic because it has the
3628   // call arguments as operands, but a CALL instruction is not variadic - it
3629   // has argument registers as implicit, not explicit uses.
3630 
3631   return Error;
3632 }
3633 
3634 /// hasNullFragReference - Return true if the DAG has any reference to the
3635 /// null_frag operator.
3636 static bool hasNullFragReference(DagInit *DI) {
3637   DefInit *OpDef = dyn_cast<DefInit>(DI->getOperator());
3638   if (!OpDef) return false;
3639   Record *Operator = OpDef->getDef();
3640 
3641   // If this is the null fragment, return true.
3642   if (Operator->getName() == "null_frag") return true;
3643   // If any of the arguments reference the null fragment, return true.
3644   for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i) {
3645     if (auto Arg = dyn_cast<DefInit>(DI->getArg(i)))
3646       if (Arg->getDef()->getName() == "null_frag")
3647         return true;
3648     DagInit *Arg = dyn_cast<DagInit>(DI->getArg(i));
3649     if (Arg && hasNullFragReference(Arg))
3650       return true;
3651   }
3652 
3653   return false;
3654 }
3655 
3656 /// hasNullFragReference - Return true if any DAG in the list references
3657 /// the null_frag operator.
3658 static bool hasNullFragReference(ListInit *LI) {
3659   for (Init *I : LI->getValues()) {
3660     DagInit *DI = dyn_cast<DagInit>(I);
3661     assert(DI && "non-dag in an instruction Pattern list?!");
3662     if (hasNullFragReference(DI))
3663       return true;
3664   }
3665   return false;
3666 }
3667 
3668 /// Get all the instructions in a tree.
3669 static void
3670 getInstructionsInTree(TreePatternNode *Tree, SmallVectorImpl<Record*> &Instrs) {
3671   if (Tree->isLeaf())
3672     return;
3673   if (Tree->getOperator()->isSubClassOf("Instruction"))
3674     Instrs.push_back(Tree->getOperator());
3675   for (unsigned i = 0, e = Tree->getNumChildren(); i != e; ++i)
3676     getInstructionsInTree(Tree->getChild(i), Instrs);
3677 }
3678 
3679 /// Check the class of a pattern leaf node against the instruction operand it
3680 /// represents.
3681 static bool checkOperandClass(CGIOperandList::OperandInfo &OI,
3682                               Record *Leaf) {
3683   if (OI.Rec == Leaf)
3684     return true;
3685 
3686   // Allow direct value types to be used in instruction set patterns.
3687   // The type will be checked later.
3688   if (Leaf->isSubClassOf("ValueType"))
3689     return true;
3690 
3691   // Patterns can also be ComplexPattern instances.
3692   if (Leaf->isSubClassOf("ComplexPattern"))
3693     return true;
3694 
3695   return false;
3696 }
3697 
3698 void CodeGenDAGPatterns::parseInstructionPattern(
3699     CodeGenInstruction &CGI, ListInit *Pat, DAGInstMap &DAGInsts) {
3700 
3701   assert(!DAGInsts.count(CGI.TheDef) && "Instruction already parsed!");
3702 
3703   // Parse the instruction.
3704   TreePattern I(CGI.TheDef, Pat, true, *this);
3705 
3706   // InstInputs - Keep track of all of the inputs of the instruction, along
3707   // with the record they are declared as.
3708   std::map<std::string, TreePatternNodePtr> InstInputs;
3709 
3710   // InstResults - Keep track of all the virtual registers that are 'set'
3711   // in the instruction, including what reg class they are.
3712   MapVector<std::string, TreePatternNodePtr, std::map<std::string, unsigned>>
3713       InstResults;
3714 
3715   std::vector<Record*> InstImpResults;
3716 
3717   // Verify that the top-level forms in the instruction are of void type, and
3718   // fill in the InstResults map.
3719   SmallString<32> TypesString;
3720   for (unsigned j = 0, e = I.getNumTrees(); j != e; ++j) {
3721     TypesString.clear();
3722     TreePatternNodePtr Pat = I.getTree(j);
3723     if (Pat->getNumTypes() != 0) {
3724       raw_svector_ostream OS(TypesString);
3725       ListSeparator LS;
3726       for (unsigned k = 0, ke = Pat->getNumTypes(); k != ke; ++k) {
3727         OS << LS;
3728         Pat->getExtType(k).writeToStream(OS);
3729       }
3730       I.error("Top-level forms in instruction pattern should have"
3731                " void types, has types " +
3732                OS.str());
3733     }
3734 
3735     // Find inputs and outputs, and verify the structure of the uses/defs.
3736     FindPatternInputsAndOutputs(I, Pat, InstInputs, InstResults,
3737                                 InstImpResults);
3738   }
3739 
3740   // Now that we have inputs and outputs of the pattern, inspect the operands
3741   // list for the instruction.  This determines the order that operands are
3742   // added to the machine instruction the node corresponds to.
3743   unsigned NumResults = InstResults.size();
3744 
3745   // Parse the operands list from the (ops) list, validating it.
3746   assert(I.getArgList().empty() && "Args list should still be empty here!");
3747 
3748   // Check that all of the results occur first in the list.
3749   std::vector<Record*> Results;
3750   std::vector<unsigned> ResultIndices;
3751   SmallVector<TreePatternNodePtr, 2> ResNodes;
3752   for (unsigned i = 0; i != NumResults; ++i) {
3753     if (i == CGI.Operands.size()) {
3754       const std::string &OpName =
3755           llvm::find_if(
3756               InstResults,
3757               [](const std::pair<std::string, TreePatternNodePtr> &P) {
3758                 return P.second;
3759               })
3760               ->first;
3761 
3762       I.error("'" + OpName + "' set but does not appear in operand list!");
3763     }
3764 
3765     const std::string &OpName = CGI.Operands[i].Name;
3766 
3767     // Check that it exists in InstResults.
3768     auto InstResultIter = InstResults.find(OpName);
3769     if (InstResultIter == InstResults.end() || !InstResultIter->second)
3770       I.error("Operand $" + OpName + " does not exist in operand list!");
3771 
3772     TreePatternNodePtr RNode = InstResultIter->second;
3773     Record *R = cast<DefInit>(RNode->getLeafValue())->getDef();
3774     ResNodes.push_back(std::move(RNode));
3775     if (!R)
3776       I.error("Operand $" + OpName + " should be a set destination: all "
3777                "outputs must occur before inputs in operand list!");
3778 
3779     if (!checkOperandClass(CGI.Operands[i], R))
3780       I.error("Operand $" + OpName + " class mismatch!");
3781 
3782     // Remember the return type.
3783     Results.push_back(CGI.Operands[i].Rec);
3784 
3785     // Remember the result index.
3786     ResultIndices.push_back(std::distance(InstResults.begin(), InstResultIter));
3787 
3788     // Okay, this one checks out.
3789     InstResultIter->second = nullptr;
3790   }
3791 
3792   // Loop over the inputs next.
3793   std::vector<TreePatternNodePtr> ResultNodeOperands;
3794   std::vector<Record*> Operands;
3795   for (unsigned i = NumResults, e = CGI.Operands.size(); i != e; ++i) {
3796     CGIOperandList::OperandInfo &Op = CGI.Operands[i];
3797     const std::string &OpName = Op.Name;
3798     if (OpName.empty())
3799       I.error("Operand #" + Twine(i) + " in operands list has no name!");
3800 
3801     if (!InstInputs.count(OpName)) {
3802       // If this is an operand with a DefaultOps set filled in, we can ignore
3803       // this.  When we codegen it, we will do so as always executed.
3804       if (Op.Rec->isSubClassOf("OperandWithDefaultOps")) {
3805         // Does it have a non-empty DefaultOps field?  If so, ignore this
3806         // operand.
3807         if (!getDefaultOperand(Op.Rec).DefaultOps.empty())
3808           continue;
3809       }
3810       I.error("Operand $" + OpName +
3811                " does not appear in the instruction pattern");
3812     }
3813     TreePatternNodePtr InVal = InstInputs[OpName];
3814     InstInputs.erase(OpName);   // It occurred, remove from map.
3815 
3816     if (InVal->isLeaf() && isa<DefInit>(InVal->getLeafValue())) {
3817       Record *InRec = static_cast<DefInit*>(InVal->getLeafValue())->getDef();
3818       if (!checkOperandClass(Op, InRec))
3819         I.error("Operand $" + OpName + "'s register class disagrees"
3820                  " between the operand and pattern");
3821     }
3822     Operands.push_back(Op.Rec);
3823 
3824     // Construct the result for the dest-pattern operand list.
3825     TreePatternNodePtr OpNode = InVal->clone();
3826 
3827     // No predicate is useful on the result.
3828     OpNode->clearPredicateCalls();
3829 
3830     // Promote the xform function to be an explicit node if set.
3831     if (Record *Xform = OpNode->getTransformFn()) {
3832       OpNode->setTransformFn(nullptr);
3833       std::vector<TreePatternNodePtr> Children;
3834       Children.push_back(OpNode);
3835       OpNode = std::make_shared<TreePatternNode>(Xform, std::move(Children),
3836                                                  OpNode->getNumTypes());
3837     }
3838 
3839     ResultNodeOperands.push_back(std::move(OpNode));
3840   }
3841 
3842   if (!InstInputs.empty())
3843     I.error("Input operand $" + InstInputs.begin()->first +
3844             " occurs in pattern but not in operands list!");
3845 
3846   TreePatternNodePtr ResultPattern = std::make_shared<TreePatternNode>(
3847       I.getRecord(), std::move(ResultNodeOperands),
3848       GetNumNodeResults(I.getRecord(), *this));
3849   // Copy fully inferred output node types to instruction result pattern.
3850   for (unsigned i = 0; i != NumResults; ++i) {
3851     assert(ResNodes[i]->getNumTypes() == 1 && "FIXME: Unhandled");
3852     ResultPattern->setType(i, ResNodes[i]->getExtType(0));
3853     ResultPattern->setResultIndex(i, ResultIndices[i]);
3854   }
3855 
3856   // FIXME: Assume only the first tree is the pattern. The others are clobber
3857   // nodes.
3858   TreePatternNodePtr Pattern = I.getTree(0);
3859   TreePatternNodePtr SrcPattern;
3860   if (Pattern->getOperator()->getName() == "set") {
3861     SrcPattern = Pattern->getChild(Pattern->getNumChildren()-1)->clone();
3862   } else{
3863     // Not a set (store or something?)
3864     SrcPattern = Pattern;
3865   }
3866 
3867   // Create and insert the instruction.
3868   // FIXME: InstImpResults should not be part of DAGInstruction.
3869   Record *R = I.getRecord();
3870   DAGInsts.emplace(std::piecewise_construct, std::forward_as_tuple(R),
3871                    std::forward_as_tuple(Results, Operands, InstImpResults,
3872                                          SrcPattern, ResultPattern));
3873 
3874   LLVM_DEBUG(I.dump());
3875 }
3876 
3877 /// ParseInstructions - Parse all of the instructions, inlining and resolving
3878 /// any fragments involved.  This populates the Instructions list with fully
3879 /// resolved instructions.
3880 void CodeGenDAGPatterns::ParseInstructions() {
3881   std::vector<Record*> Instrs = Records.getAllDerivedDefinitions("Instruction");
3882 
3883   for (Record *Instr : Instrs) {
3884     ListInit *LI = nullptr;
3885 
3886     if (isa<ListInit>(Instr->getValueInit("Pattern")))
3887       LI = Instr->getValueAsListInit("Pattern");
3888 
3889     // If there is no pattern, only collect minimal information about the
3890     // instruction for its operand list.  We have to assume that there is one
3891     // result, as we have no detailed info. A pattern which references the
3892     // null_frag operator is as-if no pattern were specified. Normally this
3893     // is from a multiclass expansion w/ a SDPatternOperator passed in as
3894     // null_frag.
3895     if (!LI || LI->empty() || hasNullFragReference(LI)) {
3896       std::vector<Record*> Results;
3897       std::vector<Record*> Operands;
3898 
3899       CodeGenInstruction &InstInfo = Target.getInstruction(Instr);
3900 
3901       if (InstInfo.Operands.size() != 0) {
3902         for (unsigned j = 0, e = InstInfo.Operands.NumDefs; j < e; ++j)
3903           Results.push_back(InstInfo.Operands[j].Rec);
3904 
3905         // The rest are inputs.
3906         for (unsigned j = InstInfo.Operands.NumDefs,
3907                e = InstInfo.Operands.size(); j < e; ++j)
3908           Operands.push_back(InstInfo.Operands[j].Rec);
3909       }
3910 
3911       // Create and insert the instruction.
3912       std::vector<Record*> ImpResults;
3913       Instructions.insert(std::make_pair(Instr,
3914                             DAGInstruction(Results, Operands, ImpResults)));
3915       continue;  // no pattern.
3916     }
3917 
3918     CodeGenInstruction &CGI = Target.getInstruction(Instr);
3919     parseInstructionPattern(CGI, LI, Instructions);
3920   }
3921 
3922   // If we can, convert the instructions to be patterns that are matched!
3923   for (auto &Entry : Instructions) {
3924     Record *Instr = Entry.first;
3925     DAGInstruction &TheInst = Entry.second;
3926     TreePatternNodePtr SrcPattern = TheInst.getSrcPattern();
3927     TreePatternNodePtr ResultPattern = TheInst.getResultPattern();
3928 
3929     if (SrcPattern && ResultPattern) {
3930       TreePattern Pattern(Instr, SrcPattern, true, *this);
3931       TreePattern Result(Instr, ResultPattern, false, *this);
3932       ParseOnePattern(Instr, Pattern, Result, TheInst.getImpResults());
3933     }
3934   }
3935 }
3936 
3937 typedef std::pair<TreePatternNode *, unsigned> NameRecord;
3938 
3939 static void FindNames(TreePatternNode *P,
3940                       std::map<std::string, NameRecord> &Names,
3941                       TreePattern *PatternTop) {
3942   if (!P->getName().empty()) {
3943     NameRecord &Rec = Names[P->getName()];
3944     // If this is the first instance of the name, remember the node.
3945     if (Rec.second++ == 0)
3946       Rec.first = P;
3947     else if (Rec.first->getExtTypes() != P->getExtTypes())
3948       PatternTop->error("repetition of value: $" + P->getName() +
3949                         " where different uses have different types!");
3950   }
3951 
3952   if (!P->isLeaf()) {
3953     for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
3954       FindNames(P->getChild(i), Names, PatternTop);
3955   }
3956 }
3957 
3958 void CodeGenDAGPatterns::AddPatternToMatch(TreePattern *Pattern,
3959                                            PatternToMatch &&PTM) {
3960   // Do some sanity checking on the pattern we're about to match.
3961   std::string Reason;
3962   if (!PTM.getSrcPattern()->canPatternMatch(Reason, *this)) {
3963     PrintWarning(Pattern->getRecord()->getLoc(),
3964       Twine("Pattern can never match: ") + Reason);
3965     return;
3966   }
3967 
3968   // If the source pattern's root is a complex pattern, that complex pattern
3969   // must specify the nodes it can potentially match.
3970   if (const ComplexPattern *CP =
3971         PTM.getSrcPattern()->getComplexPatternInfo(*this))
3972     if (CP->getRootNodes().empty())
3973       Pattern->error("ComplexPattern at root must specify list of opcodes it"
3974                      " could match");
3975 
3976 
3977   // Find all of the named values in the input and output, ensure they have the
3978   // same type.
3979   std::map<std::string, NameRecord> SrcNames, DstNames;
3980   FindNames(PTM.getSrcPattern(), SrcNames, Pattern);
3981   FindNames(PTM.getDstPattern(), DstNames, Pattern);
3982 
3983   // Scan all of the named values in the destination pattern, rejecting them if
3984   // they don't exist in the input pattern.
3985   for (const auto &Entry : DstNames) {
3986     if (SrcNames[Entry.first].first == nullptr)
3987       Pattern->error("Pattern has input without matching name in output: $" +
3988                      Entry.first);
3989   }
3990 
3991   // Scan all of the named values in the source pattern, rejecting them if the
3992   // name isn't used in the dest, and isn't used to tie two values together.
3993   for (const auto &Entry : SrcNames)
3994     if (DstNames[Entry.first].first == nullptr &&
3995         SrcNames[Entry.first].second == 1)
3996       Pattern->error("Pattern has dead named input: $" + Entry.first);
3997 
3998   PatternsToMatch.push_back(std::move(PTM));
3999 }
4000 
4001 void CodeGenDAGPatterns::InferInstructionFlags() {
4002   ArrayRef<const CodeGenInstruction*> Instructions =
4003     Target.getInstructionsByEnumValue();
4004 
4005   unsigned Errors = 0;
4006 
4007   // Try to infer flags from all patterns in PatternToMatch.  These include
4008   // both the primary instruction patterns (which always come first) and
4009   // patterns defined outside the instruction.
4010   for (const PatternToMatch &PTM : ptms()) {
4011     // We can only infer from single-instruction patterns, otherwise we won't
4012     // know which instruction should get the flags.
4013     SmallVector<Record*, 8> PatInstrs;
4014     getInstructionsInTree(PTM.getDstPattern(), PatInstrs);
4015     if (PatInstrs.size() != 1)
4016       continue;
4017 
4018     // Get the single instruction.
4019     CodeGenInstruction &InstInfo = Target.getInstruction(PatInstrs.front());
4020 
4021     // Only infer properties from the first pattern. We'll verify the others.
4022     if (InstInfo.InferredFrom)
4023       continue;
4024 
4025     InstAnalyzer PatInfo(*this);
4026     PatInfo.Analyze(PTM);
4027     Errors += InferFromPattern(InstInfo, PatInfo, PTM.getSrcRecord());
4028   }
4029 
4030   if (Errors)
4031     PrintFatalError("pattern conflicts");
4032 
4033   // If requested by the target, guess any undefined properties.
4034   if (Target.guessInstructionProperties()) {
4035     for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
4036       CodeGenInstruction *InstInfo =
4037         const_cast<CodeGenInstruction *>(Instructions[i]);
4038       if (InstInfo->InferredFrom)
4039         continue;
4040       // The mayLoad and mayStore flags default to false.
4041       // Conservatively assume hasSideEffects if it wasn't explicit.
4042       if (InstInfo->hasSideEffects_Unset)
4043         InstInfo->hasSideEffects = true;
4044     }
4045     return;
4046   }
4047 
4048   // Complain about any flags that are still undefined.
4049   for (unsigned i = 0, e = Instructions.size(); i != e; ++i) {
4050     CodeGenInstruction *InstInfo =
4051       const_cast<CodeGenInstruction *>(Instructions[i]);
4052     if (InstInfo->InferredFrom)
4053       continue;
4054     if (InstInfo->hasSideEffects_Unset)
4055       PrintError(InstInfo->TheDef->getLoc(),
4056                  "Can't infer hasSideEffects from patterns");
4057     if (InstInfo->mayStore_Unset)
4058       PrintError(InstInfo->TheDef->getLoc(),
4059                  "Can't infer mayStore from patterns");
4060     if (InstInfo->mayLoad_Unset)
4061       PrintError(InstInfo->TheDef->getLoc(),
4062                  "Can't infer mayLoad from patterns");
4063   }
4064 }
4065 
4066 
4067 /// Verify instruction flags against pattern node properties.
4068 void CodeGenDAGPatterns::VerifyInstructionFlags() {
4069   unsigned Errors = 0;
4070   for (const PatternToMatch &PTM : ptms()) {
4071     SmallVector<Record*, 8> Instrs;
4072     getInstructionsInTree(PTM.getDstPattern(), Instrs);
4073     if (Instrs.empty())
4074       continue;
4075 
4076     // Count the number of instructions with each flag set.
4077     unsigned NumSideEffects = 0;
4078     unsigned NumStores = 0;
4079     unsigned NumLoads = 0;
4080     for (const Record *Instr : Instrs) {
4081       const CodeGenInstruction &InstInfo = Target.getInstruction(Instr);
4082       NumSideEffects += InstInfo.hasSideEffects;
4083       NumStores += InstInfo.mayStore;
4084       NumLoads += InstInfo.mayLoad;
4085     }
4086 
4087     // Analyze the source pattern.
4088     InstAnalyzer PatInfo(*this);
4089     PatInfo.Analyze(PTM);
4090 
4091     // Collect error messages.
4092     SmallVector<std::string, 4> Msgs;
4093 
4094     // Check for missing flags in the output.
4095     // Permit extra flags for now at least.
4096     if (PatInfo.hasSideEffects && !NumSideEffects)
4097       Msgs.push_back("pattern has side effects, but hasSideEffects isn't set");
4098 
4099     // Don't verify store flags on instructions with side effects. At least for
4100     // intrinsics, side effects implies mayStore.
4101     if (!PatInfo.hasSideEffects && PatInfo.mayStore && !NumStores)
4102       Msgs.push_back("pattern may store, but mayStore isn't set");
4103 
4104     // Similarly, mayStore implies mayLoad on intrinsics.
4105     if (!PatInfo.mayStore && PatInfo.mayLoad && !NumLoads)
4106       Msgs.push_back("pattern may load, but mayLoad isn't set");
4107 
4108     // Print error messages.
4109     if (Msgs.empty())
4110       continue;
4111     ++Errors;
4112 
4113     for (const std::string &Msg : Msgs)
4114       PrintError(PTM.getSrcRecord()->getLoc(), Twine(Msg) + " on the " +
4115                  (Instrs.size() == 1 ?
4116                   "instruction" : "output instructions"));
4117     // Provide the location of the relevant instruction definitions.
4118     for (const Record *Instr : Instrs) {
4119       if (Instr != PTM.getSrcRecord())
4120         PrintError(Instr->getLoc(), "defined here");
4121       const CodeGenInstruction &InstInfo = Target.getInstruction(Instr);
4122       if (InstInfo.InferredFrom &&
4123           InstInfo.InferredFrom != InstInfo.TheDef &&
4124           InstInfo.InferredFrom != PTM.getSrcRecord())
4125         PrintError(InstInfo.InferredFrom->getLoc(), "inferred from pattern");
4126     }
4127   }
4128   if (Errors)
4129     PrintFatalError("Errors in DAG patterns");
4130 }
4131 
4132 /// Given a pattern result with an unresolved type, see if we can find one
4133 /// instruction with an unresolved result type.  Force this result type to an
4134 /// arbitrary element if it's possible types to converge results.
4135 static bool ForceArbitraryInstResultType(TreePatternNode *N, TreePattern &TP) {
4136   if (N->isLeaf())
4137     return false;
4138 
4139   // Analyze children.
4140   for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
4141     if (ForceArbitraryInstResultType(N->getChild(i), TP))
4142       return true;
4143 
4144   if (!N->getOperator()->isSubClassOf("Instruction"))
4145     return false;
4146 
4147   // If this type is already concrete or completely unknown we can't do
4148   // anything.
4149   TypeInfer &TI = TP.getInfer();
4150   for (unsigned i = 0, e = N->getNumTypes(); i != e; ++i) {
4151     if (N->getExtType(i).empty() || TI.isConcrete(N->getExtType(i), false))
4152       continue;
4153 
4154     // Otherwise, force its type to an arbitrary choice.
4155     if (TI.forceArbitrary(N->getExtType(i)))
4156       return true;
4157   }
4158 
4159   return false;
4160 }
4161 
4162 // Promote xform function to be an explicit node wherever set.
4163 static TreePatternNodePtr PromoteXForms(TreePatternNodePtr N) {
4164   if (Record *Xform = N->getTransformFn()) {
4165       N->setTransformFn(nullptr);
4166       std::vector<TreePatternNodePtr> Children;
4167       Children.push_back(PromoteXForms(N));
4168       return std::make_shared<TreePatternNode>(Xform, std::move(Children),
4169                                                N->getNumTypes());
4170   }
4171 
4172   if (!N->isLeaf())
4173     for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
4174       TreePatternNodePtr Child = N->getChildShared(i);
4175       N->setChild(i, PromoteXForms(Child));
4176     }
4177   return N;
4178 }
4179 
4180 void CodeGenDAGPatterns::ParseOnePattern(Record *TheDef,
4181        TreePattern &Pattern, TreePattern &Result,
4182        const std::vector<Record *> &InstImpResults) {
4183 
4184   // Inline pattern fragments and expand multiple alternatives.
4185   Pattern.InlinePatternFragments();
4186   Result.InlinePatternFragments();
4187 
4188   if (Result.getNumTrees() != 1)
4189     Result.error("Cannot use multi-alternative fragments in result pattern!");
4190 
4191   // Infer types.
4192   bool IterateInference;
4193   bool InferredAllPatternTypes, InferredAllResultTypes;
4194   do {
4195     // Infer as many types as possible.  If we cannot infer all of them, we
4196     // can never do anything with this pattern: report it to the user.
4197     InferredAllPatternTypes =
4198         Pattern.InferAllTypes(&Pattern.getNamedNodesMap());
4199 
4200     // Infer as many types as possible.  If we cannot infer all of them, we
4201     // can never do anything with this pattern: report it to the user.
4202     InferredAllResultTypes =
4203         Result.InferAllTypes(&Pattern.getNamedNodesMap());
4204 
4205     IterateInference = false;
4206 
4207     // Apply the type of the result to the source pattern.  This helps us
4208     // resolve cases where the input type is known to be a pointer type (which
4209     // is considered resolved), but the result knows it needs to be 32- or
4210     // 64-bits.  Infer the other way for good measure.
4211     for (const auto &T : Pattern.getTrees())
4212       for (unsigned i = 0, e = std::min(Result.getOnlyTree()->getNumTypes(),
4213                                         T->getNumTypes());
4214          i != e; ++i) {
4215         IterateInference |= T->UpdateNodeType(
4216             i, Result.getOnlyTree()->getExtType(i), Result);
4217         IterateInference |= Result.getOnlyTree()->UpdateNodeType(
4218             i, T->getExtType(i), Result);
4219       }
4220 
4221     // If our iteration has converged and the input pattern's types are fully
4222     // resolved but the result pattern is not fully resolved, we may have a
4223     // situation where we have two instructions in the result pattern and
4224     // the instructions require a common register class, but don't care about
4225     // what actual MVT is used.  This is actually a bug in our modelling:
4226     // output patterns should have register classes, not MVTs.
4227     //
4228     // In any case, to handle this, we just go through and disambiguate some
4229     // arbitrary types to the result pattern's nodes.
4230     if (!IterateInference && InferredAllPatternTypes &&
4231         !InferredAllResultTypes)
4232       IterateInference =
4233           ForceArbitraryInstResultType(Result.getTree(0).get(), Result);
4234   } while (IterateInference);
4235 
4236   // Verify that we inferred enough types that we can do something with the
4237   // pattern and result.  If these fire the user has to add type casts.
4238   if (!InferredAllPatternTypes)
4239     Pattern.error("Could not infer all types in pattern!");
4240   if (!InferredAllResultTypes) {
4241     Pattern.dump();
4242     Result.error("Could not infer all types in pattern result!");
4243   }
4244 
4245   // Promote xform function to be an explicit node wherever set.
4246   TreePatternNodePtr DstShared = PromoteXForms(Result.getOnlyTree());
4247 
4248   TreePattern Temp(Result.getRecord(), DstShared, false, *this);
4249   Temp.InferAllTypes();
4250 
4251   ListInit *Preds = TheDef->getValueAsListInit("Predicates");
4252   int Complexity = TheDef->getValueAsInt("AddedComplexity");
4253 
4254   if (PatternRewriter)
4255     PatternRewriter(&Pattern);
4256 
4257   // A pattern may end up with an "impossible" type, i.e. a situation
4258   // where all types have been eliminated for some node in this pattern.
4259   // This could occur for intrinsics that only make sense for a specific
4260   // value type, and use a specific register class. If, for some mode,
4261   // that register class does not accept that type, the type inference
4262   // will lead to a contradiction, which is not an error however, but
4263   // a sign that this pattern will simply never match.
4264   if (Temp.getOnlyTree()->hasPossibleType())
4265     for (const auto &T : Pattern.getTrees())
4266       if (T->hasPossibleType())
4267         AddPatternToMatch(&Pattern,
4268                           PatternToMatch(TheDef, Preds, T, Temp.getOnlyTree(),
4269                                          InstImpResults, Complexity,
4270                                          TheDef->getID()));
4271 }
4272 
4273 void CodeGenDAGPatterns::ParsePatterns() {
4274   std::vector<Record*> Patterns = Records.getAllDerivedDefinitions("Pattern");
4275 
4276   for (Record *CurPattern : Patterns) {
4277     DagInit *Tree = CurPattern->getValueAsDag("PatternToMatch");
4278 
4279     // If the pattern references the null_frag, there's nothing to do.
4280     if (hasNullFragReference(Tree))
4281       continue;
4282 
4283     TreePattern Pattern(CurPattern, Tree, true, *this);
4284 
4285     ListInit *LI = CurPattern->getValueAsListInit("ResultInstrs");
4286     if (LI->empty()) continue;  // no pattern.
4287 
4288     // Parse the instruction.
4289     TreePattern Result(CurPattern, LI, false, *this);
4290 
4291     if (Result.getNumTrees() != 1)
4292       Result.error("Cannot handle instructions producing instructions "
4293                    "with temporaries yet!");
4294 
4295     // Validate that the input pattern is correct.
4296     std::map<std::string, TreePatternNodePtr> InstInputs;
4297     MapVector<std::string, TreePatternNodePtr, std::map<std::string, unsigned>>
4298         InstResults;
4299     std::vector<Record*> InstImpResults;
4300     for (unsigned j = 0, ee = Pattern.getNumTrees(); j != ee; ++j)
4301       FindPatternInputsAndOutputs(Pattern, Pattern.getTree(j), InstInputs,
4302                                   InstResults, InstImpResults);
4303 
4304     ParseOnePattern(CurPattern, Pattern, Result, InstImpResults);
4305   }
4306 }
4307 
4308 static void collectModes(std::set<unsigned> &Modes, const TreePatternNode *N) {
4309   for (const TypeSetByHwMode &VTS : N->getExtTypes())
4310     for (const auto &I : VTS)
4311       Modes.insert(I.first);
4312 
4313   for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
4314     collectModes(Modes, N->getChild(i));
4315 }
4316 
4317 void CodeGenDAGPatterns::ExpandHwModeBasedTypes() {
4318   const CodeGenHwModes &CGH = getTargetInfo().getHwModes();
4319   std::vector<PatternToMatch> Copy;
4320   PatternsToMatch.swap(Copy);
4321 
4322   auto AppendPattern = [this](PatternToMatch &P, unsigned Mode,
4323                               StringRef Check) {
4324     TreePatternNodePtr NewSrc = P.getSrcPattern()->clone();
4325     TreePatternNodePtr NewDst = P.getDstPattern()->clone();
4326     if (!NewSrc->setDefaultMode(Mode) || !NewDst->setDefaultMode(Mode)) {
4327       return;
4328     }
4329 
4330     PatternsToMatch.emplace_back(P.getSrcRecord(), P.getPredicates(),
4331                                  std::move(NewSrc), std::move(NewDst),
4332                                  P.getDstRegs(), P.getAddedComplexity(),
4333                                  Record::getNewUID(), Mode, Check);
4334   };
4335 
4336   for (PatternToMatch &P : Copy) {
4337     TreePatternNodePtr SrcP = nullptr, DstP = nullptr;
4338     if (P.getSrcPattern()->hasProperTypeByHwMode())
4339       SrcP = P.getSrcPatternShared();
4340     if (P.getDstPattern()->hasProperTypeByHwMode())
4341       DstP = P.getDstPatternShared();
4342     if (!SrcP && !DstP) {
4343       PatternsToMatch.push_back(P);
4344       continue;
4345     }
4346 
4347     std::set<unsigned> Modes;
4348     if (SrcP)
4349       collectModes(Modes, SrcP.get());
4350     if (DstP)
4351       collectModes(Modes, DstP.get());
4352 
4353     // The predicate for the default mode needs to be constructed for each
4354     // pattern separately.
4355     // Since not all modes must be present in each pattern, if a mode m is
4356     // absent, then there is no point in constructing a check for m. If such
4357     // a check was created, it would be equivalent to checking the default
4358     // mode, except not all modes' predicates would be a part of the checking
4359     // code. The subsequently generated check for the default mode would then
4360     // have the exact same patterns, but a different predicate code. To avoid
4361     // duplicated patterns with different predicate checks, construct the
4362     // default check as a negation of all predicates that are actually present
4363     // in the source/destination patterns.
4364     SmallString<128> DefaultCheck;
4365 
4366     for (unsigned M : Modes) {
4367       if (M == DefaultMode)
4368         continue;
4369 
4370       // Fill the map entry for this mode.
4371       const HwMode &HM = CGH.getMode(M);
4372       AppendPattern(P, M, "(MF->getSubtarget().checkFeatures(\"" + HM.Features + "\"))");
4373 
4374       // Add negations of the HM's predicates to the default predicate.
4375       if (!DefaultCheck.empty())
4376         DefaultCheck += " && ";
4377       DefaultCheck += "(!(MF->getSubtarget().checkFeatures(\"";
4378       DefaultCheck += HM.Features;
4379       DefaultCheck += "\")))";
4380     }
4381 
4382     bool HasDefault = Modes.count(DefaultMode);
4383     if (HasDefault)
4384       AppendPattern(P, DefaultMode, DefaultCheck);
4385   }
4386 }
4387 
4388 /// Dependent variable map for CodeGenDAGPattern variant generation
4389 typedef StringMap<int> DepVarMap;
4390 
4391 static void FindDepVarsOf(TreePatternNode *N, DepVarMap &DepMap) {
4392   if (N->isLeaf()) {
4393     if (N->hasName() && isa<DefInit>(N->getLeafValue()))
4394       DepMap[N->getName()]++;
4395   } else {
4396     for (size_t i = 0, e = N->getNumChildren(); i != e; ++i)
4397       FindDepVarsOf(N->getChild(i), DepMap);
4398   }
4399 }
4400 
4401 /// Find dependent variables within child patterns
4402 static void FindDepVars(TreePatternNode *N, MultipleUseVarSet &DepVars) {
4403   DepVarMap depcounts;
4404   FindDepVarsOf(N, depcounts);
4405   for (const auto &Pair : depcounts) {
4406     if (Pair.getValue() > 1)
4407       DepVars.insert(Pair.getKey());
4408   }
4409 }
4410 
4411 #ifndef NDEBUG
4412 /// Dump the dependent variable set:
4413 static void DumpDepVars(MultipleUseVarSet &DepVars) {
4414   if (DepVars.empty()) {
4415     LLVM_DEBUG(errs() << "<empty set>");
4416   } else {
4417     LLVM_DEBUG(errs() << "[ ");
4418     for (const auto &DepVar : DepVars) {
4419       LLVM_DEBUG(errs() << DepVar.getKey() << " ");
4420     }
4421     LLVM_DEBUG(errs() << "]");
4422   }
4423 }
4424 #endif
4425 
4426 
4427 /// CombineChildVariants - Given a bunch of permutations of each child of the
4428 /// 'operator' node, put them together in all possible ways.
4429 static void CombineChildVariants(
4430     TreePatternNodePtr Orig,
4431     const std::vector<std::vector<TreePatternNodePtr>> &ChildVariants,
4432     std::vector<TreePatternNodePtr> &OutVariants, CodeGenDAGPatterns &CDP,
4433     const MultipleUseVarSet &DepVars) {
4434   // Make sure that each operand has at least one variant to choose from.
4435   for (const auto &Variants : ChildVariants)
4436     if (Variants.empty())
4437       return;
4438 
4439   // The end result is an all-pairs construction of the resultant pattern.
4440   std::vector<unsigned> Idxs;
4441   Idxs.resize(ChildVariants.size());
4442   bool NotDone;
4443   do {
4444 #ifndef NDEBUG
4445     LLVM_DEBUG(if (!Idxs.empty()) {
4446       errs() << Orig->getOperator()->getName() << ": Idxs = [ ";
4447       for (unsigned Idx : Idxs) {
4448         errs() << Idx << " ";
4449       }
4450       errs() << "]\n";
4451     });
4452 #endif
4453     // Create the variant and add it to the output list.
4454     std::vector<TreePatternNodePtr> NewChildren;
4455     for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i)
4456       NewChildren.push_back(ChildVariants[i][Idxs[i]]);
4457     TreePatternNodePtr R = std::make_shared<TreePatternNode>(
4458         Orig->getOperator(), std::move(NewChildren), Orig->getNumTypes());
4459 
4460     // Copy over properties.
4461     R->setName(Orig->getName());
4462     R->setNamesAsPredicateArg(Orig->getNamesAsPredicateArg());
4463     R->setPredicateCalls(Orig->getPredicateCalls());
4464     R->setTransformFn(Orig->getTransformFn());
4465     for (unsigned i = 0, e = Orig->getNumTypes(); i != e; ++i)
4466       R->setType(i, Orig->getExtType(i));
4467 
4468     // If this pattern cannot match, do not include it as a variant.
4469     std::string ErrString;
4470     // Scan to see if this pattern has already been emitted.  We can get
4471     // duplication due to things like commuting:
4472     //   (and GPRC:$a, GPRC:$b) -> (and GPRC:$b, GPRC:$a)
4473     // which are the same pattern.  Ignore the dups.
4474     if (R->canPatternMatch(ErrString, CDP) &&
4475         none_of(OutVariants, [&](TreePatternNodePtr Variant) {
4476           return R->isIsomorphicTo(Variant.get(), DepVars);
4477         }))
4478       OutVariants.push_back(R);
4479 
4480     // Increment indices to the next permutation by incrementing the
4481     // indices from last index backward, e.g., generate the sequence
4482     // [0, 0], [0, 1], [1, 0], [1, 1].
4483     int IdxsIdx;
4484     for (IdxsIdx = Idxs.size() - 1; IdxsIdx >= 0; --IdxsIdx) {
4485       if (++Idxs[IdxsIdx] == ChildVariants[IdxsIdx].size())
4486         Idxs[IdxsIdx] = 0;
4487       else
4488         break;
4489     }
4490     NotDone = (IdxsIdx >= 0);
4491   } while (NotDone);
4492 }
4493 
4494 /// CombineChildVariants - A helper function for binary operators.
4495 ///
4496 static void CombineChildVariants(TreePatternNodePtr Orig,
4497                                  const std::vector<TreePatternNodePtr> &LHS,
4498                                  const std::vector<TreePatternNodePtr> &RHS,
4499                                  std::vector<TreePatternNodePtr> &OutVariants,
4500                                  CodeGenDAGPatterns &CDP,
4501                                  const MultipleUseVarSet &DepVars) {
4502   std::vector<std::vector<TreePatternNodePtr>> ChildVariants;
4503   ChildVariants.push_back(LHS);
4504   ChildVariants.push_back(RHS);
4505   CombineChildVariants(Orig, ChildVariants, OutVariants, CDP, DepVars);
4506 }
4507 
4508 static void
4509 GatherChildrenOfAssociativeOpcode(TreePatternNodePtr N,
4510                                   std::vector<TreePatternNodePtr> &Children) {
4511   assert(N->getNumChildren()==2 &&"Associative but doesn't have 2 children!");
4512   Record *Operator = N->getOperator();
4513 
4514   // Only permit raw nodes.
4515   if (!N->getName().empty() || !N->getPredicateCalls().empty() ||
4516       N->getTransformFn()) {
4517     Children.push_back(N);
4518     return;
4519   }
4520 
4521   if (N->getChild(0)->isLeaf() || N->getChild(0)->getOperator() != Operator)
4522     Children.push_back(N->getChildShared(0));
4523   else
4524     GatherChildrenOfAssociativeOpcode(N->getChildShared(0), Children);
4525 
4526   if (N->getChild(1)->isLeaf() || N->getChild(1)->getOperator() != Operator)
4527     Children.push_back(N->getChildShared(1));
4528   else
4529     GatherChildrenOfAssociativeOpcode(N->getChildShared(1), Children);
4530 }
4531 
4532 /// GenerateVariantsOf - Given a pattern N, generate all permutations we can of
4533 /// the (potentially recursive) pattern by using algebraic laws.
4534 ///
4535 static void GenerateVariantsOf(TreePatternNodePtr N,
4536                                std::vector<TreePatternNodePtr> &OutVariants,
4537                                CodeGenDAGPatterns &CDP,
4538                                const MultipleUseVarSet &DepVars) {
4539   // We cannot permute leaves or ComplexPattern uses.
4540   if (N->isLeaf() || N->getOperator()->isSubClassOf("ComplexPattern")) {
4541     OutVariants.push_back(N);
4542     return;
4543   }
4544 
4545   // Look up interesting info about the node.
4546   const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(N->getOperator());
4547 
4548   // If this node is associative, re-associate.
4549   if (NodeInfo.hasProperty(SDNPAssociative)) {
4550     // Re-associate by pulling together all of the linked operators
4551     std::vector<TreePatternNodePtr> MaximalChildren;
4552     GatherChildrenOfAssociativeOpcode(N, MaximalChildren);
4553 
4554     // Only handle child sizes of 3.  Otherwise we'll end up trying too many
4555     // permutations.
4556     if (MaximalChildren.size() == 3) {
4557       // Find the variants of all of our maximal children.
4558       std::vector<TreePatternNodePtr> AVariants, BVariants, CVariants;
4559       GenerateVariantsOf(MaximalChildren[0], AVariants, CDP, DepVars);
4560       GenerateVariantsOf(MaximalChildren[1], BVariants, CDP, DepVars);
4561       GenerateVariantsOf(MaximalChildren[2], CVariants, CDP, DepVars);
4562 
4563       // There are only two ways we can permute the tree:
4564       //   (A op B) op C    and    A op (B op C)
4565       // Within these forms, we can also permute A/B/C.
4566 
4567       // Generate legal pair permutations of A/B/C.
4568       std::vector<TreePatternNodePtr> ABVariants;
4569       std::vector<TreePatternNodePtr> BAVariants;
4570       std::vector<TreePatternNodePtr> ACVariants;
4571       std::vector<TreePatternNodePtr> CAVariants;
4572       std::vector<TreePatternNodePtr> BCVariants;
4573       std::vector<TreePatternNodePtr> CBVariants;
4574       CombineChildVariants(N, AVariants, BVariants, ABVariants, CDP, DepVars);
4575       CombineChildVariants(N, BVariants, AVariants, BAVariants, CDP, DepVars);
4576       CombineChildVariants(N, AVariants, CVariants, ACVariants, CDP, DepVars);
4577       CombineChildVariants(N, CVariants, AVariants, CAVariants, CDP, DepVars);
4578       CombineChildVariants(N, BVariants, CVariants, BCVariants, CDP, DepVars);
4579       CombineChildVariants(N, CVariants, BVariants, CBVariants, CDP, DepVars);
4580 
4581       // Combine those into the result: (x op x) op x
4582       CombineChildVariants(N, ABVariants, CVariants, OutVariants, CDP, DepVars);
4583       CombineChildVariants(N, BAVariants, CVariants, OutVariants, CDP, DepVars);
4584       CombineChildVariants(N, ACVariants, BVariants, OutVariants, CDP, DepVars);
4585       CombineChildVariants(N, CAVariants, BVariants, OutVariants, CDP, DepVars);
4586       CombineChildVariants(N, BCVariants, AVariants, OutVariants, CDP, DepVars);
4587       CombineChildVariants(N, CBVariants, AVariants, OutVariants, CDP, DepVars);
4588 
4589       // Combine those into the result: x op (x op x)
4590       CombineChildVariants(N, CVariants, ABVariants, OutVariants, CDP, DepVars);
4591       CombineChildVariants(N, CVariants, BAVariants, OutVariants, CDP, DepVars);
4592       CombineChildVariants(N, BVariants, ACVariants, OutVariants, CDP, DepVars);
4593       CombineChildVariants(N, BVariants, CAVariants, OutVariants, CDP, DepVars);
4594       CombineChildVariants(N, AVariants, BCVariants, OutVariants, CDP, DepVars);
4595       CombineChildVariants(N, AVariants, CBVariants, OutVariants, CDP, DepVars);
4596       return;
4597     }
4598   }
4599 
4600   // Compute permutations of all children.
4601   std::vector<std::vector<TreePatternNodePtr>> ChildVariants;
4602   ChildVariants.resize(N->getNumChildren());
4603   for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
4604     GenerateVariantsOf(N->getChildShared(i), ChildVariants[i], CDP, DepVars);
4605 
4606   // Build all permutations based on how the children were formed.
4607   CombineChildVariants(N, ChildVariants, OutVariants, CDP, DepVars);
4608 
4609   // If this node is commutative, consider the commuted order.
4610   bool isCommIntrinsic = N->isCommutativeIntrinsic(CDP);
4611   if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) {
4612     assert((N->getNumChildren()>=2 || isCommIntrinsic) &&
4613            "Commutative but doesn't have 2 children!");
4614     // Don't count children which are actually register references.
4615     unsigned NC = 0;
4616     for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
4617       TreePatternNode *Child = N->getChild(i);
4618       if (Child->isLeaf())
4619         if (DefInit *DI = dyn_cast<DefInit>(Child->getLeafValue())) {
4620           Record *RR = DI->getDef();
4621           if (RR->isSubClassOf("Register"))
4622             continue;
4623         }
4624       NC++;
4625     }
4626     // Consider the commuted order.
4627     if (isCommIntrinsic) {
4628       // Commutative intrinsic. First operand is the intrinsic id, 2nd and 3rd
4629       // operands are the commutative operands, and there might be more operands
4630       // after those.
4631       assert(NC >= 3 &&
4632              "Commutative intrinsic should have at least 3 children!");
4633       std::vector<std::vector<TreePatternNodePtr>> Variants;
4634       Variants.push_back(std::move(ChildVariants[0])); // Intrinsic id.
4635       Variants.push_back(std::move(ChildVariants[2]));
4636       Variants.push_back(std::move(ChildVariants[1]));
4637       for (unsigned i = 3; i != NC; ++i)
4638         Variants.push_back(std::move(ChildVariants[i]));
4639       CombineChildVariants(N, Variants, OutVariants, CDP, DepVars);
4640     } else if (NC == N->getNumChildren()) {
4641       std::vector<std::vector<TreePatternNodePtr>> Variants;
4642       Variants.push_back(std::move(ChildVariants[1]));
4643       Variants.push_back(std::move(ChildVariants[0]));
4644       for (unsigned i = 2; i != NC; ++i)
4645         Variants.push_back(std::move(ChildVariants[i]));
4646       CombineChildVariants(N, Variants, OutVariants, CDP, DepVars);
4647     }
4648   }
4649 }
4650 
4651 
4652 // GenerateVariants - Generate variants.  For example, commutative patterns can
4653 // match multiple ways.  Add them to PatternsToMatch as well.
4654 void CodeGenDAGPatterns::GenerateVariants() {
4655   LLVM_DEBUG(errs() << "Generating instruction variants.\n");
4656 
4657   // Loop over all of the patterns we've collected, checking to see if we can
4658   // generate variants of the instruction, through the exploitation of
4659   // identities.  This permits the target to provide aggressive matching without
4660   // the .td file having to contain tons of variants of instructions.
4661   //
4662   // Note that this loop adds new patterns to the PatternsToMatch list, but we
4663   // intentionally do not reconsider these.  Any variants of added patterns have
4664   // already been added.
4665   //
4666   for (unsigned i = 0, e = PatternsToMatch.size(); i != e; ++i) {
4667     MultipleUseVarSet DepVars;
4668     std::vector<TreePatternNodePtr> Variants;
4669     FindDepVars(PatternsToMatch[i].getSrcPattern(), DepVars);
4670     LLVM_DEBUG(errs() << "Dependent/multiply used variables: ");
4671     LLVM_DEBUG(DumpDepVars(DepVars));
4672     LLVM_DEBUG(errs() << "\n");
4673     GenerateVariantsOf(PatternsToMatch[i].getSrcPatternShared(), Variants,
4674                        *this, DepVars);
4675 
4676     assert(PatternsToMatch[i].getHwModeFeatures().empty() &&
4677            "HwModes should not have been expanded yet!");
4678 
4679     assert(!Variants.empty() && "Must create at least original variant!");
4680     if (Variants.size() == 1) // No additional variants for this pattern.
4681       continue;
4682 
4683     LLVM_DEBUG(errs() << "FOUND VARIANTS OF: ";
4684                PatternsToMatch[i].getSrcPattern()->dump(); errs() << "\n");
4685 
4686     for (unsigned v = 0, e = Variants.size(); v != e; ++v) {
4687       TreePatternNodePtr Variant = Variants[v];
4688 
4689       LLVM_DEBUG(errs() << "  VAR#" << v << ": "; Variant->dump();
4690                  errs() << "\n");
4691 
4692       // Scan to see if an instruction or explicit pattern already matches this.
4693       bool AlreadyExists = false;
4694       for (unsigned p = 0, e = PatternsToMatch.size(); p != e; ++p) {
4695         // Skip if the top level predicates do not match.
4696         if ((i != p) && (PatternsToMatch[i].getPredicates() !=
4697                          PatternsToMatch[p].getPredicates()))
4698           continue;
4699         // Check to see if this variant already exists.
4700         if (Variant->isIsomorphicTo(PatternsToMatch[p].getSrcPattern(),
4701                                     DepVars)) {
4702           LLVM_DEBUG(errs() << "  *** ALREADY EXISTS, ignoring variant.\n");
4703           AlreadyExists = true;
4704           break;
4705         }
4706       }
4707       // If we already have it, ignore the variant.
4708       if (AlreadyExists) continue;
4709 
4710       // Otherwise, add it to the list of patterns we have.
4711       PatternsToMatch.emplace_back(
4712           PatternsToMatch[i].getSrcRecord(), PatternsToMatch[i].getPredicates(),
4713           Variant, PatternsToMatch[i].getDstPatternShared(),
4714           PatternsToMatch[i].getDstRegs(),
4715           PatternsToMatch[i].getAddedComplexity(), Record::getNewUID(),
4716           PatternsToMatch[i].getForceMode(),
4717           PatternsToMatch[i].getHwModeFeatures());
4718     }
4719 
4720     LLVM_DEBUG(errs() << "\n");
4721   }
4722 }
4723