10b57cec5SDimitry Andric //===- CodeGenDAGPatterns.h - Read DAG patterns from .td file ---*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file declares the CodeGenDAGPatterns class, which is used to read and
100b57cec5SDimitry Andric // represent the patterns present in a .td file for instructions.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #ifndef LLVM_UTILS_TABLEGEN_CODEGENDAGPATTERNS_H
150b57cec5SDimitry Andric #define LLVM_UTILS_TABLEGEN_CODEGENDAGPATTERNS_H
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric #include "CodeGenIntrinsics.h"
180b57cec5SDimitry Andric #include "CodeGenTarget.h"
190b57cec5SDimitry Andric #include "SDNodeProperties.h"
2006c3fb27SDimitry Andric #include "llvm/ADT/IntrusiveRefCntPtr.h"
210b57cec5SDimitry Andric #include "llvm/ADT/MapVector.h"
2206c3fb27SDimitry Andric #include "llvm/ADT/PointerUnion.h"
230b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
240b57cec5SDimitry Andric #include "llvm/ADT/StringMap.h"
250b57cec5SDimitry Andric #include "llvm/ADT/StringSet.h"
2606c3fb27SDimitry Andric #include "llvm/ADT/Twine.h"
270b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
280b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
2906c3fb27SDimitry Andric #include "llvm/TableGen/Record.h"
300b57cec5SDimitry Andric #include <algorithm>
310b57cec5SDimitry Andric #include <array>
320b57cec5SDimitry Andric #include <functional>
330b57cec5SDimitry Andric #include <map>
340b57cec5SDimitry Andric #include <numeric>
350b57cec5SDimitry Andric #include <vector>
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric namespace llvm {
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric class Init;
400b57cec5SDimitry Andric class ListInit;
410b57cec5SDimitry Andric class DagInit;
420b57cec5SDimitry Andric class SDNodeInfo;
430b57cec5SDimitry Andric class TreePattern;
440b57cec5SDimitry Andric class TreePatternNode;
450b57cec5SDimitry Andric class CodeGenDAGPatterns;
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric /// Shared pointer for TreePatternNode.
4806c3fb27SDimitry Andric using TreePatternNodePtr = IntrusiveRefCntPtr<TreePatternNode>;
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric /// This represents a set of MVTs. Since the underlying type for the MVT
510b57cec5SDimitry Andric /// is uint8_t, there are at most 256 values. To reduce the number of memory
520b57cec5SDimitry Andric /// allocations and deallocations, represent the set as a sequence of bits.
530b57cec5SDimitry Andric /// To reduce the allocations even further, make MachineValueTypeSet own
540b57cec5SDimitry Andric /// the storage and use std::array as the bit container.
550b57cec5SDimitry Andric struct MachineValueTypeSet {
56bdd1243dSDimitry Andric   static_assert(std::is_same<std::underlying_type_t<MVT::SimpleValueType>,
570b57cec5SDimitry Andric                              uint8_t>::value,
580b57cec5SDimitry Andric                 "Change uint8_t here to the SimpleValueType's type");
590b57cec5SDimitry Andric   static unsigned constexpr Capacity = std::numeric_limits<uint8_t>::max()+1;
600b57cec5SDimitry Andric   using WordType = uint64_t;
610b57cec5SDimitry Andric   static unsigned constexpr WordWidth = CHAR_BIT*sizeof(WordType);
620b57cec5SDimitry Andric   static unsigned constexpr NumWords = Capacity/WordWidth;
630b57cec5SDimitry Andric   static_assert(NumWords*WordWidth == Capacity,
640b57cec5SDimitry Andric                 "Capacity should be a multiple of WordWidth");
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric   LLVM_ATTRIBUTE_ALWAYS_INLINE
MachineValueTypeSetMachineValueTypeSet670b57cec5SDimitry Andric   MachineValueTypeSet() {
680b57cec5SDimitry Andric     clear();
690b57cec5SDimitry Andric   }
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric   LLVM_ATTRIBUTE_ALWAYS_INLINE
sizeMachineValueTypeSet720b57cec5SDimitry Andric   unsigned size() const {
730b57cec5SDimitry Andric     unsigned Count = 0;
740b57cec5SDimitry Andric     for (WordType W : Words)
75bdd1243dSDimitry Andric       Count += llvm::popcount(W);
760b57cec5SDimitry Andric     return Count;
770b57cec5SDimitry Andric   }
780b57cec5SDimitry Andric   LLVM_ATTRIBUTE_ALWAYS_INLINE
clearMachineValueTypeSet790b57cec5SDimitry Andric   void clear() {
800b57cec5SDimitry Andric     std::memset(Words.data(), 0, NumWords*sizeof(WordType));
810b57cec5SDimitry Andric   }
820b57cec5SDimitry Andric   LLVM_ATTRIBUTE_ALWAYS_INLINE
emptyMachineValueTypeSet830b57cec5SDimitry Andric   bool empty() const {
840b57cec5SDimitry Andric     for (WordType W : Words)
850b57cec5SDimitry Andric       if (W != 0)
860b57cec5SDimitry Andric         return false;
870b57cec5SDimitry Andric     return true;
880b57cec5SDimitry Andric   }
890b57cec5SDimitry Andric   LLVM_ATTRIBUTE_ALWAYS_INLINE
countMachineValueTypeSet900b57cec5SDimitry Andric   unsigned count(MVT T) const {
910b57cec5SDimitry Andric     return (Words[T.SimpleTy / WordWidth] >> (T.SimpleTy % WordWidth)) & 1;
920b57cec5SDimitry Andric   }
insertMachineValueTypeSet930b57cec5SDimitry Andric   std::pair<MachineValueTypeSet&,bool> insert(MVT T) {
940b57cec5SDimitry Andric     bool V = count(T.SimpleTy);
950b57cec5SDimitry Andric     Words[T.SimpleTy / WordWidth] |= WordType(1) << (T.SimpleTy % WordWidth);
960b57cec5SDimitry Andric     return {*this, V};
970b57cec5SDimitry Andric   }
insertMachineValueTypeSet980b57cec5SDimitry Andric   MachineValueTypeSet &insert(const MachineValueTypeSet &S) {
990b57cec5SDimitry Andric     for (unsigned i = 0; i != NumWords; ++i)
1000b57cec5SDimitry Andric       Words[i] |= S.Words[i];
1010b57cec5SDimitry Andric     return *this;
1020b57cec5SDimitry Andric   }
1030b57cec5SDimitry Andric   LLVM_ATTRIBUTE_ALWAYS_INLINE
eraseMachineValueTypeSet1040b57cec5SDimitry Andric   void erase(MVT T) {
1050b57cec5SDimitry Andric     Words[T.SimpleTy / WordWidth] &= ~(WordType(1) << (T.SimpleTy % WordWidth));
1060b57cec5SDimitry Andric   }
1070b57cec5SDimitry Andric 
108753f127fSDimitry Andric   void writeToStream(raw_ostream &OS) const;
109753f127fSDimitry Andric 
1100b57cec5SDimitry Andric   struct const_iterator {
1110b57cec5SDimitry Andric     // Some implementations of the C++ library require these traits to be
1120b57cec5SDimitry Andric     // defined.
1130b57cec5SDimitry Andric     using iterator_category = std::forward_iterator_tag;
1140b57cec5SDimitry Andric     using value_type = MVT;
1150b57cec5SDimitry Andric     using difference_type = ptrdiff_t;
1160b57cec5SDimitry Andric     using pointer = const MVT*;
1170b57cec5SDimitry Andric     using reference = const MVT&;
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric     LLVM_ATTRIBUTE_ALWAYS_INLINE
1200b57cec5SDimitry Andric     MVT operator*() const {
1210b57cec5SDimitry Andric       assert(Pos != Capacity);
1220b57cec5SDimitry Andric       return MVT::SimpleValueType(Pos);
1230b57cec5SDimitry Andric     }
1240b57cec5SDimitry Andric     LLVM_ATTRIBUTE_ALWAYS_INLINE
const_iteratorMachineValueTypeSet::const_iterator1250b57cec5SDimitry Andric     const_iterator(const MachineValueTypeSet *S, bool End) : Set(S) {
1260b57cec5SDimitry Andric       Pos = End ? Capacity : find_from_pos(0);
1270b57cec5SDimitry Andric     }
1280b57cec5SDimitry Andric     LLVM_ATTRIBUTE_ALWAYS_INLINE
1290b57cec5SDimitry Andric     const_iterator &operator++() {
1300b57cec5SDimitry Andric       assert(Pos != Capacity);
1310b57cec5SDimitry Andric       Pos = find_from_pos(Pos+1);
1320b57cec5SDimitry Andric       return *this;
1330b57cec5SDimitry Andric     }
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric     LLVM_ATTRIBUTE_ALWAYS_INLINE
1360b57cec5SDimitry Andric     bool operator==(const const_iterator &It) const {
1370b57cec5SDimitry Andric       return Set == It.Set && Pos == It.Pos;
1380b57cec5SDimitry Andric     }
1390b57cec5SDimitry Andric     LLVM_ATTRIBUTE_ALWAYS_INLINE
1400b57cec5SDimitry Andric     bool operator!=(const const_iterator &It) const {
1410b57cec5SDimitry Andric       return !operator==(It);
1420b57cec5SDimitry Andric     }
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric   private:
find_from_posMachineValueTypeSet::const_iterator1450b57cec5SDimitry Andric     unsigned find_from_pos(unsigned P) const {
1460b57cec5SDimitry Andric       unsigned SkipWords = P / WordWidth;
1470b57cec5SDimitry Andric       unsigned SkipBits = P % WordWidth;
1480b57cec5SDimitry Andric       unsigned Count = SkipWords * WordWidth;
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric       // If P is in the middle of a word, process it manually here, because
1510b57cec5SDimitry Andric       // the trailing bits need to be masked off to use findFirstSet.
1520b57cec5SDimitry Andric       if (SkipBits != 0) {
1530b57cec5SDimitry Andric         WordType W = Set->Words[SkipWords];
1540b57cec5SDimitry Andric         W &= maskLeadingOnes<WordType>(WordWidth-SkipBits);
1550b57cec5SDimitry Andric         if (W != 0)
156bdd1243dSDimitry Andric           return Count + llvm::countr_zero(W);
1570b57cec5SDimitry Andric         Count += WordWidth;
1580b57cec5SDimitry Andric         SkipWords++;
1590b57cec5SDimitry Andric       }
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric       for (unsigned i = SkipWords; i != NumWords; ++i) {
1620b57cec5SDimitry Andric         WordType W = Set->Words[i];
1630b57cec5SDimitry Andric         if (W != 0)
164bdd1243dSDimitry Andric           return Count + llvm::countr_zero(W);
1650b57cec5SDimitry Andric         Count += WordWidth;
1660b57cec5SDimitry Andric       }
1670b57cec5SDimitry Andric       return Capacity;
1680b57cec5SDimitry Andric     }
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric     const MachineValueTypeSet *Set;
1710b57cec5SDimitry Andric     unsigned Pos;
1720b57cec5SDimitry Andric   };
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric   LLVM_ATTRIBUTE_ALWAYS_INLINE
beginMachineValueTypeSet1750b57cec5SDimitry Andric   const_iterator begin() const { return const_iterator(this, false); }
1760b57cec5SDimitry Andric   LLVM_ATTRIBUTE_ALWAYS_INLINE
endMachineValueTypeSet1770b57cec5SDimitry Andric   const_iterator end()   const { return const_iterator(this, true); }
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric   LLVM_ATTRIBUTE_ALWAYS_INLINE
1800b57cec5SDimitry Andric   bool operator==(const MachineValueTypeSet &S) const {
1810b57cec5SDimitry Andric     return Words == S.Words;
1820b57cec5SDimitry Andric   }
1830b57cec5SDimitry Andric   LLVM_ATTRIBUTE_ALWAYS_INLINE
1840b57cec5SDimitry Andric   bool operator!=(const MachineValueTypeSet &S) const {
1850b57cec5SDimitry Andric     return !operator==(S);
1860b57cec5SDimitry Andric   }
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric private:
1890b57cec5SDimitry Andric   friend struct const_iterator;
1900b57cec5SDimitry Andric   std::array<WordType,NumWords> Words;
1910b57cec5SDimitry Andric };
1920b57cec5SDimitry Andric 
193753f127fSDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const MachineValueTypeSet &T);
194753f127fSDimitry Andric 
1950b57cec5SDimitry Andric struct TypeSetByHwMode : public InfoByHwMode<MachineValueTypeSet> {
1960b57cec5SDimitry Andric   using SetType = MachineValueTypeSet;
19706c3fb27SDimitry Andric   unsigned AddrSpace = std::numeric_limits<unsigned>::max();
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric   TypeSetByHwMode() = default;
2000b57cec5SDimitry Andric   TypeSetByHwMode(const TypeSetByHwMode &VTS) = default;
201480093f4SDimitry Andric   TypeSetByHwMode &operator=(const TypeSetByHwMode &) = default;
TypeSetByHwModeTypeSetByHwMode2020b57cec5SDimitry Andric   TypeSetByHwMode(MVT::SimpleValueType VT)
2030b57cec5SDimitry Andric     : TypeSetByHwMode(ValueTypeByHwMode(VT)) {}
TypeSetByHwModeTypeSetByHwMode2040b57cec5SDimitry Andric   TypeSetByHwMode(ValueTypeByHwMode VT)
2050b57cec5SDimitry Andric     : TypeSetByHwMode(ArrayRef<ValueTypeByHwMode>(&VT, 1)) {}
2060b57cec5SDimitry Andric   TypeSetByHwMode(ArrayRef<ValueTypeByHwMode> VTList);
2070b57cec5SDimitry Andric 
getOrCreateTypeSetByHwMode2080b57cec5SDimitry Andric   SetType &getOrCreate(unsigned Mode) {
209fe6060f1SDimitry Andric     return Map[Mode];
2100b57cec5SDimitry Andric   }
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric   bool isValueTypeByHwMode(bool AllowEmpty) const;
2130b57cec5SDimitry Andric   ValueTypeByHwMode getValueTypeByHwMode() const;
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric   LLVM_ATTRIBUTE_ALWAYS_INLINE
isMachineValueTypeTypeSetByHwMode2160b57cec5SDimitry Andric   bool isMachineValueType() const {
21706c3fb27SDimitry Andric     return isSimple() && getSimple().size() == 1;
2180b57cec5SDimitry Andric   }
2190b57cec5SDimitry Andric 
2200b57cec5SDimitry Andric   LLVM_ATTRIBUTE_ALWAYS_INLINE
getMachineValueTypeTypeSetByHwMode2210b57cec5SDimitry Andric   MVT getMachineValueType() const {
2220b57cec5SDimitry Andric     assert(isMachineValueType());
22306c3fb27SDimitry Andric     return *getSimple().begin();
2240b57cec5SDimitry Andric   }
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric   bool isPossible() const;
2270b57cec5SDimitry Andric 
isPointerTypeSetByHwMode2280b57cec5SDimitry Andric   bool isPointer() const {
2290b57cec5SDimitry Andric     return getValueTypeByHwMode().isPointer();
2300b57cec5SDimitry Andric   }
2310b57cec5SDimitry Andric 
getPtrAddrSpaceTypeSetByHwMode2320b57cec5SDimitry Andric   unsigned getPtrAddrSpace() const {
2330b57cec5SDimitry Andric     assert(isPointer());
2340b57cec5SDimitry Andric     return getValueTypeByHwMode().PtrAddrSpace;
2350b57cec5SDimitry Andric   }
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric   bool insert(const ValueTypeByHwMode &VVT);
2380b57cec5SDimitry Andric   bool constrain(const TypeSetByHwMode &VTS);
2390b57cec5SDimitry Andric   template <typename Predicate> bool constrain(Predicate P);
2400b57cec5SDimitry Andric   template <typename Predicate>
2410b57cec5SDimitry Andric   bool assign_if(const TypeSetByHwMode &VTS, Predicate P);
2420b57cec5SDimitry Andric 
2430b57cec5SDimitry Andric   void writeToStream(raw_ostream &OS) const;
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric   bool operator==(const TypeSetByHwMode &VTS) const;
2460b57cec5SDimitry Andric   bool operator!=(const TypeSetByHwMode &VTS) const { return !(*this == VTS); }
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric   void dump() const;
2490b57cec5SDimitry Andric   bool validate() const;
2500b57cec5SDimitry Andric 
2510b57cec5SDimitry Andric private:
2520b57cec5SDimitry Andric   unsigned PtrAddrSpace = std::numeric_limits<unsigned>::max();
2530b57cec5SDimitry Andric   /// Intersect two sets. Return true if anything has changed.
2540b57cec5SDimitry Andric   bool intersect(SetType &Out, const SetType &In);
2550b57cec5SDimitry Andric };
2560b57cec5SDimitry Andric 
2570b57cec5SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const TypeSetByHwMode &T);
2580b57cec5SDimitry Andric 
2590b57cec5SDimitry Andric struct TypeInfer {
TypeInferTypeInfer26006c3fb27SDimitry Andric   TypeInfer(TreePattern &T) : TP(T) {}
2610b57cec5SDimitry Andric 
isConcreteTypeInfer2620b57cec5SDimitry Andric   bool isConcrete(const TypeSetByHwMode &VTS, bool AllowEmpty) const {
2630b57cec5SDimitry Andric     return VTS.isValueTypeByHwMode(AllowEmpty);
2640b57cec5SDimitry Andric   }
getConcreteTypeInfer2650b57cec5SDimitry Andric   ValueTypeByHwMode getConcrete(const TypeSetByHwMode &VTS,
2660b57cec5SDimitry Andric                                 bool AllowEmpty) const {
2670b57cec5SDimitry Andric     assert(VTS.isValueTypeByHwMode(AllowEmpty));
2680b57cec5SDimitry Andric     return VTS.getValueTypeByHwMode();
2690b57cec5SDimitry Andric   }
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric   /// The protocol in the following functions (Merge*, force*, Enforce*,
2720b57cec5SDimitry Andric   /// expand*) is to return "true" if a change has been made, "false"
2730b57cec5SDimitry Andric   /// otherwise.
2740b57cec5SDimitry Andric 
27506c3fb27SDimitry Andric   bool MergeInTypeInfo(TypeSetByHwMode &Out, const TypeSetByHwMode &In) const;
MergeInTypeInfoTypeInfer27606c3fb27SDimitry Andric   bool MergeInTypeInfo(TypeSetByHwMode &Out, MVT::SimpleValueType InVT) const {
2770b57cec5SDimitry Andric     return MergeInTypeInfo(Out, TypeSetByHwMode(InVT));
2780b57cec5SDimitry Andric   }
MergeInTypeInfoTypeInfer27906c3fb27SDimitry Andric   bool MergeInTypeInfo(TypeSetByHwMode &Out, ValueTypeByHwMode InVT) const {
2800b57cec5SDimitry Andric     return MergeInTypeInfo(Out, TypeSetByHwMode(InVT));
2810b57cec5SDimitry Andric   }
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric   /// Reduce the set \p Out to have at most one element for each mode.
2840b57cec5SDimitry Andric   bool forceArbitrary(TypeSetByHwMode &Out);
2850b57cec5SDimitry Andric 
2860b57cec5SDimitry Andric   /// The following four functions ensure that upon return the set \p Out
2870b57cec5SDimitry Andric   /// will only contain types of the specified kind: integer, floating-point,
2880b57cec5SDimitry Andric   /// scalar, or vector.
2890b57cec5SDimitry Andric   /// If \p Out is empty, all legal types of the specified kind will be added
2900b57cec5SDimitry Andric   /// to it. Otherwise, all types that are not of the specified kind will be
2910b57cec5SDimitry Andric   /// removed from \p Out.
2920b57cec5SDimitry Andric   bool EnforceInteger(TypeSetByHwMode &Out);
2930b57cec5SDimitry Andric   bool EnforceFloatingPoint(TypeSetByHwMode &Out);
2940b57cec5SDimitry Andric   bool EnforceScalar(TypeSetByHwMode &Out);
2950b57cec5SDimitry Andric   bool EnforceVector(TypeSetByHwMode &Out);
2960b57cec5SDimitry Andric 
2970b57cec5SDimitry Andric   /// If \p Out is empty, fill it with all legal types. Otherwise, leave it
2980b57cec5SDimitry Andric   /// unchanged.
2990b57cec5SDimitry Andric   bool EnforceAny(TypeSetByHwMode &Out);
3000b57cec5SDimitry Andric   /// Make sure that for each type in \p Small, there exists a larger type
301349cc55cSDimitry Andric   /// in \p Big. \p SmallIsVT indicates that this is being called for
302349cc55cSDimitry Andric   /// SDTCisVTSmallerThanOp. In that case the TypeSetByHwMode is re-created for
303349cc55cSDimitry Andric   /// each call and needs special consideration in how we detect changes.
304349cc55cSDimitry Andric   bool EnforceSmallerThan(TypeSetByHwMode &Small, TypeSetByHwMode &Big,
305349cc55cSDimitry Andric                           bool SmallIsVT = false);
3060b57cec5SDimitry Andric   /// 1. Ensure that for each type T in \p Vec, T is a vector type, and that
3070b57cec5SDimitry Andric   ///    for each type U in \p Elem, U is a scalar type.
3080b57cec5SDimitry Andric   /// 2. Ensure that for each (scalar) type U in \p Elem, there exists a
3090b57cec5SDimitry Andric   ///    (vector) type T in \p Vec, such that U is the element type of T.
3100b57cec5SDimitry Andric   bool EnforceVectorEltTypeIs(TypeSetByHwMode &Vec, TypeSetByHwMode &Elem);
3110b57cec5SDimitry Andric   bool EnforceVectorEltTypeIs(TypeSetByHwMode &Vec,
3120b57cec5SDimitry Andric                               const ValueTypeByHwMode &VVT);
3130b57cec5SDimitry Andric   /// Ensure that for each type T in \p Sub, T is a vector type, and there
3140b57cec5SDimitry Andric   /// exists a type U in \p Vec such that U is a vector type with the same
3150b57cec5SDimitry Andric   /// element type as T and at least as many elements as T.
3160b57cec5SDimitry Andric   bool EnforceVectorSubVectorTypeIs(TypeSetByHwMode &Vec,
3170b57cec5SDimitry Andric                                     TypeSetByHwMode &Sub);
3180b57cec5SDimitry Andric   /// 1. Ensure that \p V has a scalar type iff \p W has a scalar type.
3190b57cec5SDimitry Andric   /// 2. Ensure that for each vector type T in \p V, there exists a vector
3200b57cec5SDimitry Andric   ///    type U in \p W, such that T and U have the same number of elements.
3210b57cec5SDimitry Andric   /// 3. Ensure that for each vector type U in \p W, there exists a vector
3220b57cec5SDimitry Andric   ///    type T in \p V, such that T and U have the same number of elements
3230b57cec5SDimitry Andric   ///    (reverse of 2).
3240b57cec5SDimitry Andric   bool EnforceSameNumElts(TypeSetByHwMode &V, TypeSetByHwMode &W);
3250b57cec5SDimitry Andric   /// 1. Ensure that for each type T in \p A, there exists a type U in \p B,
3260b57cec5SDimitry Andric   ///    such that T and U have equal size in bits.
3270b57cec5SDimitry Andric   /// 2. Ensure that for each type U in \p B, there exists a type T in \p A
3280b57cec5SDimitry Andric   ///    such that T and U have equal size in bits (reverse of 1).
3290b57cec5SDimitry Andric   bool EnforceSameSize(TypeSetByHwMode &A, TypeSetByHwMode &B);
3300b57cec5SDimitry Andric 
3310b57cec5SDimitry Andric   /// For each overloaded type (i.e. of form *Any), replace it with the
3320b57cec5SDimitry Andric   /// corresponding subset of legal, specific types.
33306c3fb27SDimitry Andric   void expandOverloads(TypeSetByHwMode &VTS) const;
3340b57cec5SDimitry Andric   void expandOverloads(TypeSetByHwMode::SetType &Out,
33506c3fb27SDimitry Andric                        const TypeSetByHwMode::SetType &Legal) const;
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric   struct ValidateOnExit {
ValidateOnExitTypeInfer::ValidateOnExit33806c3fb27SDimitry Andric     ValidateOnExit(const TypeSetByHwMode &T, const TypeInfer &TI)
33906c3fb27SDimitry Andric         : Infer(TI), VTS(T) {}
3400b57cec5SDimitry Andric     ~ValidateOnExit();
34106c3fb27SDimitry Andric     const TypeInfer &Infer;
34206c3fb27SDimitry Andric     const TypeSetByHwMode &VTS;
3430b57cec5SDimitry Andric   };
3440b57cec5SDimitry Andric 
3450b57cec5SDimitry Andric   struct SuppressValidation {
SuppressValidationTypeInfer::SuppressValidation3460b57cec5SDimitry Andric     SuppressValidation(TypeInfer &TI) : Infer(TI), SavedValidate(TI.Validate) {
3470b57cec5SDimitry Andric       Infer.Validate = false;
3480b57cec5SDimitry Andric     }
~SuppressValidationTypeInfer::SuppressValidation3490b57cec5SDimitry Andric     ~SuppressValidation() {
3500b57cec5SDimitry Andric       Infer.Validate = SavedValidate;
3510b57cec5SDimitry Andric     }
3520b57cec5SDimitry Andric     TypeInfer &Infer;
3530b57cec5SDimitry Andric     bool SavedValidate;
3540b57cec5SDimitry Andric   };
3550b57cec5SDimitry Andric 
3560b57cec5SDimitry Andric   TreePattern &TP;
3570b57cec5SDimitry Andric   bool Validate = true;   // Indicate whether to validate types.
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric private:
36006c3fb27SDimitry Andric   const TypeSetByHwMode &getLegalTypes() const;
3610b57cec5SDimitry Andric 
3620b57cec5SDimitry Andric   /// Cached legal types (in default mode).
36306c3fb27SDimitry Andric   mutable bool LegalTypesCached = false;
36406c3fb27SDimitry Andric   mutable TypeSetByHwMode LegalCache;
3650b57cec5SDimitry Andric };
3660b57cec5SDimitry Andric 
3670b57cec5SDimitry Andric /// Set type used to track multiply used variables in patterns
3680b57cec5SDimitry Andric typedef StringSet<> MultipleUseVarSet;
3690b57cec5SDimitry Andric 
3700b57cec5SDimitry Andric /// SDTypeConstraint - This is a discriminated union of constraints,
3710b57cec5SDimitry Andric /// corresponding to the SDTypeConstraint tablegen class in Target.td.
3720b57cec5SDimitry Andric struct SDTypeConstraint {
3730b57cec5SDimitry Andric   SDTypeConstraint(Record *R, const CodeGenHwModes &CGH);
3740b57cec5SDimitry Andric 
3750b57cec5SDimitry Andric   unsigned OperandNo;   // The operand # this constraint applies to.
3760b57cec5SDimitry Andric   enum {
3770b57cec5SDimitry Andric     SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs,
3780b57cec5SDimitry Andric     SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec,
3790b57cec5SDimitry Andric     SDTCisSubVecOfVec, SDTCVecEltisVT, SDTCisSameNumEltsAs, SDTCisSameSizeAs
3800b57cec5SDimitry Andric   } ConstraintType;
3810b57cec5SDimitry Andric 
3820b57cec5SDimitry Andric   union {   // The discriminated union.
3830b57cec5SDimitry Andric     struct {
3840b57cec5SDimitry Andric       unsigned OtherOperandNum;
3850b57cec5SDimitry Andric     } SDTCisSameAs_Info;
3860b57cec5SDimitry Andric     struct {
3870b57cec5SDimitry Andric       unsigned OtherOperandNum;
3880b57cec5SDimitry Andric     } SDTCisVTSmallerThanOp_Info;
3890b57cec5SDimitry Andric     struct {
3900b57cec5SDimitry Andric       unsigned BigOperandNum;
3910b57cec5SDimitry Andric     } SDTCisOpSmallerThanOp_Info;
3920b57cec5SDimitry Andric     struct {
3930b57cec5SDimitry Andric       unsigned OtherOperandNum;
3940b57cec5SDimitry Andric     } SDTCisEltOfVec_Info;
3950b57cec5SDimitry Andric     struct {
3960b57cec5SDimitry Andric       unsigned OtherOperandNum;
3970b57cec5SDimitry Andric     } SDTCisSubVecOfVec_Info;
3980b57cec5SDimitry Andric     struct {
3990b57cec5SDimitry Andric       unsigned OtherOperandNum;
4000b57cec5SDimitry Andric     } SDTCisSameNumEltsAs_Info;
4010b57cec5SDimitry Andric     struct {
4020b57cec5SDimitry Andric       unsigned OtherOperandNum;
4030b57cec5SDimitry Andric     } SDTCisSameSizeAs_Info;
4040b57cec5SDimitry Andric   } x;
4050b57cec5SDimitry Andric 
4060b57cec5SDimitry Andric   // The VT for SDTCisVT and SDTCVecEltisVT.
4070b57cec5SDimitry Andric   // Must not be in the union because it has a non-trivial destructor.
4080b57cec5SDimitry Andric   ValueTypeByHwMode VVT;
4090b57cec5SDimitry Andric 
4100b57cec5SDimitry Andric   /// ApplyTypeConstraint - Given a node in a pattern, apply this type
4110b57cec5SDimitry Andric   /// constraint to the nodes operands.  This returns true if it makes a
4120b57cec5SDimitry Andric   /// change, false otherwise.  If a type contradiction is found, an error
4130b57cec5SDimitry Andric   /// is flagged.
4140b57cec5SDimitry Andric   bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
4150b57cec5SDimitry Andric                            TreePattern &TP) const;
4160b57cec5SDimitry Andric };
4170b57cec5SDimitry Andric 
4180b57cec5SDimitry Andric /// ScopedName - A name of a node associated with a "scope" that indicates
4190b57cec5SDimitry Andric /// the context (e.g. instance of Pattern or PatFrag) in which the name was
4200b57cec5SDimitry Andric /// used. This enables substitution of pattern fragments while keeping track
4210b57cec5SDimitry Andric /// of what name(s) were originally given to various nodes in the tree.
4220b57cec5SDimitry Andric class ScopedName {
4230b57cec5SDimitry Andric   unsigned Scope;
4240b57cec5SDimitry Andric   std::string Identifier;
4250b57cec5SDimitry Andric public:
ScopedName(unsigned Scope,StringRef Identifier)4260b57cec5SDimitry Andric   ScopedName(unsigned Scope, StringRef Identifier)
4275ffd83dbSDimitry Andric       : Scope(Scope), Identifier(std::string(Identifier)) {
4280b57cec5SDimitry Andric     assert(Scope != 0 &&
4290b57cec5SDimitry Andric            "Scope == 0 is used to indicate predicates without arguments");
4300b57cec5SDimitry Andric   }
4310b57cec5SDimitry Andric 
getScope()4320b57cec5SDimitry Andric   unsigned getScope() const { return Scope; }
getIdentifier()4330b57cec5SDimitry Andric   const std::string &getIdentifier() const { return Identifier; }
4340b57cec5SDimitry Andric 
4350b57cec5SDimitry Andric   bool operator==(const ScopedName &o) const;
4360b57cec5SDimitry Andric   bool operator!=(const ScopedName &o) const;
4370b57cec5SDimitry Andric };
4380b57cec5SDimitry Andric 
4390b57cec5SDimitry Andric /// SDNodeInfo - One of these records is created for each SDNode instance in
4400b57cec5SDimitry Andric /// the target .td file.  This represents the various dag nodes we will be
4410b57cec5SDimitry Andric /// processing.
4420b57cec5SDimitry Andric class SDNodeInfo {
4430b57cec5SDimitry Andric   Record *Def;
4440b57cec5SDimitry Andric   StringRef EnumName;
4450b57cec5SDimitry Andric   StringRef SDClassName;
4460b57cec5SDimitry Andric   unsigned Properties;
4470b57cec5SDimitry Andric   unsigned NumResults;
4480b57cec5SDimitry Andric   int NumOperands;
4490b57cec5SDimitry Andric   std::vector<SDTypeConstraint> TypeConstraints;
4500b57cec5SDimitry Andric public:
4510b57cec5SDimitry Andric   // Parse the specified record.
4520b57cec5SDimitry Andric   SDNodeInfo(Record *R, const CodeGenHwModes &CGH);
4530b57cec5SDimitry Andric 
getNumResults()4540b57cec5SDimitry Andric   unsigned getNumResults() const { return NumResults; }
4550b57cec5SDimitry Andric 
4560b57cec5SDimitry Andric   /// getNumOperands - This is the number of operands required or -1 if
4570b57cec5SDimitry Andric   /// variadic.
getNumOperands()4580b57cec5SDimitry Andric   int getNumOperands() const { return NumOperands; }
getRecord()4590b57cec5SDimitry Andric   Record *getRecord() const { return Def; }
getEnumName()4600b57cec5SDimitry Andric   StringRef getEnumName() const { return EnumName; }
getSDClassName()4610b57cec5SDimitry Andric   StringRef getSDClassName() const { return SDClassName; }
4620b57cec5SDimitry Andric 
getTypeConstraints()4630b57cec5SDimitry Andric   const std::vector<SDTypeConstraint> &getTypeConstraints() const {
4640b57cec5SDimitry Andric     return TypeConstraints;
4650b57cec5SDimitry Andric   }
4660b57cec5SDimitry Andric 
4670b57cec5SDimitry Andric   /// getKnownType - If the type constraints on this node imply a fixed type
4680b57cec5SDimitry Andric   /// (e.g. all stores return void, etc), then return it as an
4690b57cec5SDimitry Andric   /// MVT::SimpleValueType.  Otherwise, return MVT::Other.
4700b57cec5SDimitry Andric   MVT::SimpleValueType getKnownType(unsigned ResNo) const;
4710b57cec5SDimitry Andric 
4720b57cec5SDimitry Andric   /// hasProperty - Return true if this node has the specified property.
4730b57cec5SDimitry Andric   ///
hasProperty(enum SDNP Prop)4740b57cec5SDimitry Andric   bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
4750b57cec5SDimitry Andric 
4760b57cec5SDimitry Andric   /// ApplyTypeConstraints - Given a node in a pattern, apply the type
4770b57cec5SDimitry Andric   /// constraints for this node to the operands of the node.  This returns
4780b57cec5SDimitry Andric   /// true if it makes a change, false otherwise.  If a type contradiction is
4790b57cec5SDimitry Andric   /// found, an error is flagged.
4800b57cec5SDimitry Andric   bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const;
4810b57cec5SDimitry Andric };
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric /// TreePredicateFn - This is an abstraction that represents the predicates on
4840b57cec5SDimitry Andric /// a PatFrag node.  This is a simple one-word wrapper around a pointer to
4850b57cec5SDimitry Andric /// provide nice accessors.
4860b57cec5SDimitry Andric class TreePredicateFn {
4870b57cec5SDimitry Andric   /// PatFragRec - This is the TreePattern for the PatFrag that we
4880b57cec5SDimitry Andric   /// originally came from.
4890b57cec5SDimitry Andric   TreePattern *PatFragRec;
4900b57cec5SDimitry Andric public:
4910b57cec5SDimitry Andric   /// TreePredicateFn constructor.  Here 'N' is a subclass of PatFrag.
4920b57cec5SDimitry Andric   TreePredicateFn(TreePattern *N);
4930b57cec5SDimitry Andric 
4940b57cec5SDimitry Andric 
getOrigPatFragRecord()4950b57cec5SDimitry Andric   TreePattern *getOrigPatFragRecord() const { return PatFragRec; }
4960b57cec5SDimitry Andric 
4970b57cec5SDimitry Andric   /// isAlwaysTrue - Return true if this is a noop predicate.
4980b57cec5SDimitry Andric   bool isAlwaysTrue() const;
4990b57cec5SDimitry Andric 
isImmediatePattern()5000b57cec5SDimitry Andric   bool isImmediatePattern() const { return hasImmCode(); }
5010b57cec5SDimitry Andric 
5020b57cec5SDimitry Andric   /// getImmediatePredicateCode - Return the code that evaluates this pattern if
5030b57cec5SDimitry Andric   /// this is an immediate predicate.  It is an error to call this on a
5040b57cec5SDimitry Andric   /// non-immediate pattern.
getImmediatePredicateCode()5050b57cec5SDimitry Andric   std::string getImmediatePredicateCode() const {
5060b57cec5SDimitry Andric     std::string Result = getImmCode();
5070b57cec5SDimitry Andric     assert(!Result.empty() && "Isn't an immediate pattern!");
5080b57cec5SDimitry Andric     return Result;
5090b57cec5SDimitry Andric   }
5100b57cec5SDimitry Andric 
5110b57cec5SDimitry Andric   bool operator==(const TreePredicateFn &RHS) const {
5120b57cec5SDimitry Andric     return PatFragRec == RHS.PatFragRec;
5130b57cec5SDimitry Andric   }
5140b57cec5SDimitry Andric 
5150b57cec5SDimitry Andric   bool operator!=(const TreePredicateFn &RHS) const { return !(*this == RHS); }
5160b57cec5SDimitry Andric 
5170b57cec5SDimitry Andric   /// Return the name to use in the generated code to reference this, this is
5180b57cec5SDimitry Andric   /// "Predicate_foo" if from a pattern fragment "foo".
5190b57cec5SDimitry Andric   std::string getFnName() const;
5200b57cec5SDimitry Andric 
5210b57cec5SDimitry Andric   /// getCodeToRunOnSDNode - Return the code for the function body that
5220b57cec5SDimitry Andric   /// evaluates this predicate.  The argument is expected to be in "Node",
5230b57cec5SDimitry Andric   /// not N.  This handles casting and conversion to a concrete node type as
5240b57cec5SDimitry Andric   /// appropriate.
5250b57cec5SDimitry Andric   std::string getCodeToRunOnSDNode() const;
5260b57cec5SDimitry Andric 
5270b57cec5SDimitry Andric   /// Get the data type of the argument to getImmediatePredicateCode().
5280b57cec5SDimitry Andric   StringRef getImmType() const;
5290b57cec5SDimitry Andric 
5300b57cec5SDimitry Andric   /// Get a string that describes the type returned by getImmType() but is
5310b57cec5SDimitry Andric   /// usable as part of an identifier.
5320b57cec5SDimitry Andric   StringRef getImmTypeIdentifier() const;
5330b57cec5SDimitry Andric 
5340b57cec5SDimitry Andric   // Predicate code uses the PatFrag's captured operands.
5350b57cec5SDimitry Andric   bool usesOperands() const;
5360b57cec5SDimitry Andric 
537753f127fSDimitry Andric   // Check if the HasNoUse predicate is set.
538753f127fSDimitry Andric   bool hasNoUse() const;
539753f127fSDimitry Andric 
5400b57cec5SDimitry Andric   // Is the desired predefined predicate for a load?
5410b57cec5SDimitry Andric   bool isLoad() const;
5420b57cec5SDimitry Andric   // Is the desired predefined predicate for a store?
5430b57cec5SDimitry Andric   bool isStore() const;
5440b57cec5SDimitry Andric   // Is the desired predefined predicate for an atomic?
5450b57cec5SDimitry Andric   bool isAtomic() const;
5460b57cec5SDimitry Andric 
5470b57cec5SDimitry Andric   /// Is this predicate the predefined unindexed load predicate?
5480b57cec5SDimitry Andric   /// Is this predicate the predefined unindexed store predicate?
5490b57cec5SDimitry Andric   bool isUnindexed() const;
5500b57cec5SDimitry Andric   /// Is this predicate the predefined non-extending load predicate?
5510b57cec5SDimitry Andric   bool isNonExtLoad() const;
5520b57cec5SDimitry Andric   /// Is this predicate the predefined any-extend load predicate?
5530b57cec5SDimitry Andric   bool isAnyExtLoad() const;
5540b57cec5SDimitry Andric   /// Is this predicate the predefined sign-extend load predicate?
5550b57cec5SDimitry Andric   bool isSignExtLoad() const;
5560b57cec5SDimitry Andric   /// Is this predicate the predefined zero-extend load predicate?
5570b57cec5SDimitry Andric   bool isZeroExtLoad() const;
5580b57cec5SDimitry Andric   /// Is this predicate the predefined non-truncating store predicate?
5590b57cec5SDimitry Andric   bool isNonTruncStore() const;
5600b57cec5SDimitry Andric   /// Is this predicate the predefined truncating store predicate?
5610b57cec5SDimitry Andric   bool isTruncStore() const;
5620b57cec5SDimitry Andric 
5630b57cec5SDimitry Andric   /// Is this predicate the predefined monotonic atomic predicate?
5640b57cec5SDimitry Andric   bool isAtomicOrderingMonotonic() const;
5650b57cec5SDimitry Andric   /// Is this predicate the predefined acquire atomic predicate?
5660b57cec5SDimitry Andric   bool isAtomicOrderingAcquire() const;
5670b57cec5SDimitry Andric   /// Is this predicate the predefined release atomic predicate?
5680b57cec5SDimitry Andric   bool isAtomicOrderingRelease() const;
5690b57cec5SDimitry Andric   /// Is this predicate the predefined acquire-release atomic predicate?
5700b57cec5SDimitry Andric   bool isAtomicOrderingAcquireRelease() const;
5710b57cec5SDimitry Andric   /// Is this predicate the predefined sequentially consistent atomic predicate?
5720b57cec5SDimitry Andric   bool isAtomicOrderingSequentiallyConsistent() const;
5730b57cec5SDimitry Andric 
5740b57cec5SDimitry Andric   /// Is this predicate the predefined acquire-or-stronger atomic predicate?
5750b57cec5SDimitry Andric   bool isAtomicOrderingAcquireOrStronger() const;
5760b57cec5SDimitry Andric   /// Is this predicate the predefined weaker-than-acquire atomic predicate?
5770b57cec5SDimitry Andric   bool isAtomicOrderingWeakerThanAcquire() const;
5780b57cec5SDimitry Andric 
5790b57cec5SDimitry Andric   /// Is this predicate the predefined release-or-stronger atomic predicate?
5800b57cec5SDimitry Andric   bool isAtomicOrderingReleaseOrStronger() const;
5810b57cec5SDimitry Andric   /// Is this predicate the predefined weaker-than-release atomic predicate?
5820b57cec5SDimitry Andric   bool isAtomicOrderingWeakerThanRelease() const;
5830b57cec5SDimitry Andric 
5840b57cec5SDimitry Andric   /// If non-null, indicates that this predicate is a predefined memory VT
5850b57cec5SDimitry Andric   /// predicate for a load/store and returns the ValueType record for the memory VT.
5860b57cec5SDimitry Andric   Record *getMemoryVT() const;
5870b57cec5SDimitry Andric   /// If non-null, indicates that this predicate is a predefined memory VT
5880b57cec5SDimitry Andric   /// predicate (checking only the scalar type) for load/store and returns the
5890b57cec5SDimitry Andric   /// ValueType record for the memory VT.
5900b57cec5SDimitry Andric   Record *getScalarMemoryVT() const;
5910b57cec5SDimitry Andric 
5920b57cec5SDimitry Andric   ListInit *getAddressSpaces() const;
5938bcb0991SDimitry Andric   int64_t getMinAlignment() const;
5940b57cec5SDimitry Andric 
5950b57cec5SDimitry Andric   // If true, indicates that GlobalISel-based C++ code was supplied.
5960b57cec5SDimitry Andric   bool hasGISelPredicateCode() const;
5970b57cec5SDimitry Andric   std::string getGISelPredicateCode() const;
5980b57cec5SDimitry Andric 
5990b57cec5SDimitry Andric private:
6000b57cec5SDimitry Andric   bool hasPredCode() const;
6010b57cec5SDimitry Andric   bool hasImmCode() const;
6020b57cec5SDimitry Andric   std::string getPredCode() const;
6030b57cec5SDimitry Andric   std::string getImmCode() const;
6040b57cec5SDimitry Andric   bool immCodeUsesAPInt() const;
6050b57cec5SDimitry Andric   bool immCodeUsesAPFloat() const;
6060b57cec5SDimitry Andric 
6070b57cec5SDimitry Andric   bool isPredefinedPredicateEqualTo(StringRef Field, bool Value) const;
6080b57cec5SDimitry Andric };
6090b57cec5SDimitry Andric 
6100b57cec5SDimitry Andric struct TreePredicateCall {
6110b57cec5SDimitry Andric   TreePredicateFn Fn;
6120b57cec5SDimitry Andric 
6130b57cec5SDimitry Andric   // Scope -- unique identifier for retrieving named arguments. 0 is used when
6140b57cec5SDimitry Andric   // the predicate does not use named arguments.
6150b57cec5SDimitry Andric   unsigned Scope;
6160b57cec5SDimitry Andric 
TreePredicateCallTreePredicateCall6170b57cec5SDimitry Andric   TreePredicateCall(const TreePredicateFn &Fn, unsigned Scope)
6180b57cec5SDimitry Andric     : Fn(Fn), Scope(Scope) {}
6190b57cec5SDimitry Andric 
6200b57cec5SDimitry Andric   bool operator==(const TreePredicateCall &o) const {
6210b57cec5SDimitry Andric     return Fn == o.Fn && Scope == o.Scope;
6220b57cec5SDimitry Andric   }
6230b57cec5SDimitry Andric   bool operator!=(const TreePredicateCall &o) const {
6240b57cec5SDimitry Andric     return !(*this == o);
6250b57cec5SDimitry Andric   }
6260b57cec5SDimitry Andric };
6270b57cec5SDimitry Andric 
62806c3fb27SDimitry Andric class TreePatternNode : public RefCountedBase<TreePatternNode> {
6290b57cec5SDimitry Andric   /// The type of each node result.  Before and during type inference, each
6300b57cec5SDimitry Andric   /// result may be a set of possible types.  After (successful) type inference,
6310b57cec5SDimitry Andric   /// each is a single concrete type.
6320b57cec5SDimitry Andric   std::vector<TypeSetByHwMode> Types;
6330b57cec5SDimitry Andric 
6340b57cec5SDimitry Andric   /// The index of each result in results of the pattern.
6350b57cec5SDimitry Andric   std::vector<unsigned> ResultPerm;
6360b57cec5SDimitry Andric 
63706c3fb27SDimitry Andric   /// OperatorOrVal - The Record for the operator if this is an interior node
63806c3fb27SDimitry Andric   /// (not a leaf) or the init value (e.g. the "GPRC" record, or "7") for a
63906c3fb27SDimitry Andric   /// leaf.
64006c3fb27SDimitry Andric   PointerUnion<Record *, Init *> OperatorOrVal;
6410b57cec5SDimitry Andric 
6420b57cec5SDimitry Andric   /// Name - The name given to this node with the :$foo notation.
6430b57cec5SDimitry Andric   ///
6440b57cec5SDimitry Andric   std::string Name;
6450b57cec5SDimitry Andric 
6460b57cec5SDimitry Andric   std::vector<ScopedName> NamesAsPredicateArg;
6470b57cec5SDimitry Andric 
6480b57cec5SDimitry Andric   /// PredicateCalls - The predicate functions to execute on this node to check
6490b57cec5SDimitry Andric   /// for a match.  If this list is empty, no predicate is involved.
6500b57cec5SDimitry Andric   std::vector<TreePredicateCall> PredicateCalls;
6510b57cec5SDimitry Andric 
6520b57cec5SDimitry Andric   /// TransformFn - The transformation function to execute on this node before
6530b57cec5SDimitry Andric   /// it can be substituted into the resulting instruction on a pattern match.
6540b57cec5SDimitry Andric   Record *TransformFn;
6550b57cec5SDimitry Andric 
6560b57cec5SDimitry Andric   std::vector<TreePatternNodePtr> Children;
6570b57cec5SDimitry Andric 
65806c3fb27SDimitry Andric   /// If this was instantiated from a PatFrag node, and the PatFrag was derived
65906c3fb27SDimitry Andric   /// from "GISelFlags": the original Record derived from GISelFlags.
66006c3fb27SDimitry Andric   const Record *GISelFlags = nullptr;
66106c3fb27SDimitry Andric 
6620b57cec5SDimitry Andric public:
TreePatternNode(Record * Op,std::vector<TreePatternNodePtr> Ch,unsigned NumResults)6630b57cec5SDimitry Andric   TreePatternNode(Record *Op, std::vector<TreePatternNodePtr> Ch,
6640b57cec5SDimitry Andric                   unsigned NumResults)
66506c3fb27SDimitry Andric       : OperatorOrVal(Op), TransformFn(nullptr), Children(std::move(Ch)) {
6660b57cec5SDimitry Andric     Types.resize(NumResults);
6670b57cec5SDimitry Andric     ResultPerm.resize(NumResults);
6680b57cec5SDimitry Andric     std::iota(ResultPerm.begin(), ResultPerm.end(), 0);
6690b57cec5SDimitry Andric   }
TreePatternNode(Init * val,unsigned NumResults)6700b57cec5SDimitry Andric   TreePatternNode(Init *val, unsigned NumResults) // leaf ctor
67106c3fb27SDimitry Andric       : OperatorOrVal(val), TransformFn(nullptr) {
6720b57cec5SDimitry Andric     Types.resize(NumResults);
6730b57cec5SDimitry Andric     ResultPerm.resize(NumResults);
6740b57cec5SDimitry Andric     std::iota(ResultPerm.begin(), ResultPerm.end(), 0);
6750b57cec5SDimitry Andric   }
6760b57cec5SDimitry Andric 
hasName()6770b57cec5SDimitry Andric   bool hasName() const { return !Name.empty(); }
getName()6780b57cec5SDimitry Andric   const std::string &getName() const { return Name; }
setName(StringRef N)6790b57cec5SDimitry Andric   void setName(StringRef N) { Name.assign(N.begin(), N.end()); }
6800b57cec5SDimitry Andric 
getNamesAsPredicateArg()6810b57cec5SDimitry Andric   const std::vector<ScopedName> &getNamesAsPredicateArg() const {
6820b57cec5SDimitry Andric     return NamesAsPredicateArg;
6830b57cec5SDimitry Andric   }
setNamesAsPredicateArg(const std::vector<ScopedName> & Names)6840b57cec5SDimitry Andric   void setNamesAsPredicateArg(const std::vector<ScopedName>& Names) {
6850b57cec5SDimitry Andric     NamesAsPredicateArg = Names;
6860b57cec5SDimitry Andric   }
addNameAsPredicateArg(const ScopedName & N)6870b57cec5SDimitry Andric   void addNameAsPredicateArg(const ScopedName &N) {
6880b57cec5SDimitry Andric     NamesAsPredicateArg.push_back(N);
6890b57cec5SDimitry Andric   }
6900b57cec5SDimitry Andric 
isLeaf()69106c3fb27SDimitry Andric   bool isLeaf() const { return isa<Init *>(OperatorOrVal); }
6920b57cec5SDimitry Andric 
6930b57cec5SDimitry Andric   // Type accessors.
getNumTypes()6940b57cec5SDimitry Andric   unsigned getNumTypes() const { return Types.size(); }
getType(unsigned ResNo)6950b57cec5SDimitry Andric   ValueTypeByHwMode getType(unsigned ResNo) const {
6960b57cec5SDimitry Andric     return Types[ResNo].getValueTypeByHwMode();
6970b57cec5SDimitry Andric   }
getExtTypes()6980b57cec5SDimitry Andric   const std::vector<TypeSetByHwMode> &getExtTypes() const { return Types; }
getExtType(unsigned ResNo)6990b57cec5SDimitry Andric   const TypeSetByHwMode &getExtType(unsigned ResNo) const {
7000b57cec5SDimitry Andric     return Types[ResNo];
7010b57cec5SDimitry Andric   }
getExtType(unsigned ResNo)7020b57cec5SDimitry Andric   TypeSetByHwMode &getExtType(unsigned ResNo) { return Types[ResNo]; }
setType(unsigned ResNo,const TypeSetByHwMode & T)7030b57cec5SDimitry Andric   void setType(unsigned ResNo, const TypeSetByHwMode &T) { Types[ResNo] = T; }
getSimpleType(unsigned ResNo)7040b57cec5SDimitry Andric   MVT::SimpleValueType getSimpleType(unsigned ResNo) const {
7050b57cec5SDimitry Andric     return Types[ResNo].getMachineValueType().SimpleTy;
7060b57cec5SDimitry Andric   }
7070b57cec5SDimitry Andric 
hasConcreteType(unsigned ResNo)7080b57cec5SDimitry Andric   bool hasConcreteType(unsigned ResNo) const {
7090b57cec5SDimitry Andric     return Types[ResNo].isValueTypeByHwMode(false);
7100b57cec5SDimitry Andric   }
isTypeCompletelyUnknown(unsigned ResNo,TreePattern & TP)7110b57cec5SDimitry Andric   bool isTypeCompletelyUnknown(unsigned ResNo, TreePattern &TP) const {
7120b57cec5SDimitry Andric     return Types[ResNo].empty();
7130b57cec5SDimitry Andric   }
7140b57cec5SDimitry Andric 
getNumResults()7150b57cec5SDimitry Andric   unsigned getNumResults() const { return ResultPerm.size(); }
getResultIndex(unsigned ResNo)7160b57cec5SDimitry Andric   unsigned getResultIndex(unsigned ResNo) const { return ResultPerm[ResNo]; }
setResultIndex(unsigned ResNo,unsigned RI)7170b57cec5SDimitry Andric   void setResultIndex(unsigned ResNo, unsigned RI) { ResultPerm[ResNo] = RI; }
7180b57cec5SDimitry Andric 
getLeafValue()71906c3fb27SDimitry Andric   Init *getLeafValue() const {
72006c3fb27SDimitry Andric     assert(isLeaf());
72106c3fb27SDimitry Andric     return cast<Init *>(OperatorOrVal);
72206c3fb27SDimitry Andric   }
getOperator()72306c3fb27SDimitry Andric   Record *getOperator() const {
72406c3fb27SDimitry Andric     assert(!isLeaf());
72506c3fb27SDimitry Andric     return cast<Record *>(OperatorOrVal);
72606c3fb27SDimitry Andric   }
7270b57cec5SDimitry Andric 
getNumChildren()7280b57cec5SDimitry Andric   unsigned getNumChildren() const { return Children.size(); }
getChild(unsigned N)72906c3fb27SDimitry Andric   const TreePatternNode *getChild(unsigned N) const {
73006c3fb27SDimitry Andric     return Children[N].get();
73106c3fb27SDimitry Andric   }
getChild(unsigned N)73206c3fb27SDimitry Andric   TreePatternNode *getChild(unsigned N) { return Children[N].get(); }
getChildShared(unsigned N)7330b57cec5SDimitry Andric   const TreePatternNodePtr &getChildShared(unsigned N) const {
7340b57cec5SDimitry Andric     return Children[N];
7350b57cec5SDimitry Andric   }
getChildSharedPtr(unsigned N)73606c3fb27SDimitry Andric   TreePatternNodePtr &getChildSharedPtr(unsigned N) {
73706c3fb27SDimitry Andric     return Children[N];
73806c3fb27SDimitry Andric   }
setChild(unsigned i,TreePatternNodePtr N)7390b57cec5SDimitry Andric   void setChild(unsigned i, TreePatternNodePtr N) { Children[i] = N; }
7400b57cec5SDimitry Andric 
7410b57cec5SDimitry Andric   /// hasChild - Return true if N is any of our children.
hasChild(const TreePatternNode * N)7420b57cec5SDimitry Andric   bool hasChild(const TreePatternNode *N) const {
7430b57cec5SDimitry Andric     for (unsigned i = 0, e = Children.size(); i != e; ++i)
7440b57cec5SDimitry Andric       if (Children[i].get() == N)
7450b57cec5SDimitry Andric         return true;
7460b57cec5SDimitry Andric     return false;
7470b57cec5SDimitry Andric   }
7480b57cec5SDimitry Andric 
7490b57cec5SDimitry Andric   bool hasProperTypeByHwMode() const;
7500b57cec5SDimitry Andric   bool hasPossibleType() const;
7510b57cec5SDimitry Andric   bool setDefaultMode(unsigned Mode);
7520b57cec5SDimitry Andric 
hasAnyPredicate()7530b57cec5SDimitry Andric   bool hasAnyPredicate() const { return !PredicateCalls.empty(); }
7540b57cec5SDimitry Andric 
getPredicateCalls()7550b57cec5SDimitry Andric   const std::vector<TreePredicateCall> &getPredicateCalls() const {
7560b57cec5SDimitry Andric     return PredicateCalls;
7570b57cec5SDimitry Andric   }
clearPredicateCalls()7580b57cec5SDimitry Andric   void clearPredicateCalls() { PredicateCalls.clear(); }
setPredicateCalls(const std::vector<TreePredicateCall> & Calls)7590b57cec5SDimitry Andric   void setPredicateCalls(const std::vector<TreePredicateCall> &Calls) {
7600b57cec5SDimitry Andric     assert(PredicateCalls.empty() && "Overwriting non-empty predicate list!");
7610b57cec5SDimitry Andric     PredicateCalls = Calls;
7620b57cec5SDimitry Andric   }
addPredicateCall(const TreePredicateCall & Call)7630b57cec5SDimitry Andric   void addPredicateCall(const TreePredicateCall &Call) {
7640b57cec5SDimitry Andric     assert(!Call.Fn.isAlwaysTrue() && "Empty predicate string!");
7650b57cec5SDimitry Andric     assert(!is_contained(PredicateCalls, Call) && "predicate applied recursively");
7660b57cec5SDimitry Andric     PredicateCalls.push_back(Call);
7670b57cec5SDimitry Andric   }
addPredicateCall(const TreePredicateFn & Fn,unsigned Scope)7680b57cec5SDimitry Andric   void addPredicateCall(const TreePredicateFn &Fn, unsigned Scope) {
7690b57cec5SDimitry Andric     assert((Scope != 0) == Fn.usesOperands());
7700b57cec5SDimitry Andric     addPredicateCall(TreePredicateCall(Fn, Scope));
7710b57cec5SDimitry Andric   }
7720b57cec5SDimitry Andric 
getTransformFn()7730b57cec5SDimitry Andric   Record *getTransformFn() const { return TransformFn; }
setTransformFn(Record * Fn)7740b57cec5SDimitry Andric   void setTransformFn(Record *Fn) { TransformFn = Fn; }
7750b57cec5SDimitry Andric 
7760b57cec5SDimitry Andric   /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
7770b57cec5SDimitry Andric   /// CodeGenIntrinsic information for it, otherwise return a null pointer.
7780b57cec5SDimitry Andric   const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
7790b57cec5SDimitry Andric 
7800b57cec5SDimitry Andric   /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
7810b57cec5SDimitry Andric   /// return the ComplexPattern information, otherwise return null.
7820b57cec5SDimitry Andric   const ComplexPattern *
7830b57cec5SDimitry Andric   getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
7840b57cec5SDimitry Andric 
7850b57cec5SDimitry Andric   /// Returns the number of MachineInstr operands that would be produced by this
7860b57cec5SDimitry Andric   /// node if it mapped directly to an output Instruction's
7870b57cec5SDimitry Andric   /// operand. ComplexPattern specifies this explicitly; MIOperandInfo gives it
7880b57cec5SDimitry Andric   /// for Operands; otherwise 1.
7890b57cec5SDimitry Andric   unsigned getNumMIResults(const CodeGenDAGPatterns &CGP) const;
7900b57cec5SDimitry Andric 
7910b57cec5SDimitry Andric   /// NodeHasProperty - Return true if this node has the specified property.
7920b57cec5SDimitry Andric   bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
7930b57cec5SDimitry Andric 
7940b57cec5SDimitry Andric   /// TreeHasProperty - Return true if any node in this tree has the specified
7950b57cec5SDimitry Andric   /// property.
7960b57cec5SDimitry Andric   bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
7970b57cec5SDimitry Andric 
7980b57cec5SDimitry Andric   /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
7990b57cec5SDimitry Andric   /// marked isCommutative.
8000b57cec5SDimitry Andric   bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
8010b57cec5SDimitry Andric 
setGISelFlagsRecord(const Record * R)80206c3fb27SDimitry Andric   void setGISelFlagsRecord(const Record *R) { GISelFlags = R; }
getGISelFlagsRecord()80306c3fb27SDimitry Andric   const Record *getGISelFlagsRecord() const { return GISelFlags; }
80406c3fb27SDimitry Andric 
8050b57cec5SDimitry Andric   void print(raw_ostream &OS) const;
8060b57cec5SDimitry Andric   void dump() const;
8070b57cec5SDimitry Andric 
8080b57cec5SDimitry Andric public:   // Higher level manipulation routines.
8090b57cec5SDimitry Andric 
8100b57cec5SDimitry Andric   /// clone - Return a new copy of this tree.
8110b57cec5SDimitry Andric   ///
8120b57cec5SDimitry Andric   TreePatternNodePtr clone() const;
8130b57cec5SDimitry Andric 
8140b57cec5SDimitry Andric   /// RemoveAllTypes - Recursively strip all the types of this tree.
8150b57cec5SDimitry Andric   void RemoveAllTypes();
8160b57cec5SDimitry Andric 
8170b57cec5SDimitry Andric   /// isIsomorphicTo - Return true if this node is recursively isomorphic to
8180b57cec5SDimitry Andric   /// the specified node.  For this comparison, all of the state of the node
8190b57cec5SDimitry Andric   /// is considered, except for the assigned name.  Nodes with differing names
8200b57cec5SDimitry Andric   /// that are otherwise identical are considered isomorphic.
8210b57cec5SDimitry Andric   bool isIsomorphicTo(const TreePatternNode *N,
8220b57cec5SDimitry Andric                       const MultipleUseVarSet &DepVars) const;
8230b57cec5SDimitry Andric 
8240b57cec5SDimitry Andric   /// SubstituteFormalArguments - Replace the formal arguments in this tree
8250b57cec5SDimitry Andric   /// with actual values specified by ArgMap.
8260b57cec5SDimitry Andric   void
8270b57cec5SDimitry Andric   SubstituteFormalArguments(std::map<std::string, TreePatternNodePtr> &ArgMap);
8280b57cec5SDimitry Andric 
82906c3fb27SDimitry Andric   /// InlinePatternFragments - If \p T pattern refers to any pattern
8300b57cec5SDimitry Andric   /// fragments, return the set of inlined versions (this can be more than
8310b57cec5SDimitry Andric   /// one if a PatFrags record has multiple alternatives).
83206c3fb27SDimitry Andric   void InlinePatternFragments(TreePattern &TP,
8330b57cec5SDimitry Andric                               std::vector<TreePatternNodePtr> &OutAlternatives);
8340b57cec5SDimitry Andric 
8350b57cec5SDimitry Andric   /// ApplyTypeConstraints - Apply all of the type constraints relevant to
8360b57cec5SDimitry Andric   /// this node and its children in the tree.  This returns true if it makes a
8370b57cec5SDimitry Andric   /// change, false otherwise.  If a type contradiction is found, flag an error.
8380b57cec5SDimitry Andric   bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
8390b57cec5SDimitry Andric 
8400b57cec5SDimitry Andric   /// UpdateNodeType - Set the node type of N to VT if VT contains
8410b57cec5SDimitry Andric   /// information.  If N already contains a conflicting type, then flag an
8420b57cec5SDimitry Andric   /// error.  This returns true if any information was updated.
8430b57cec5SDimitry Andric   ///
8440b57cec5SDimitry Andric   bool UpdateNodeType(unsigned ResNo, const TypeSetByHwMode &InTy,
8450b57cec5SDimitry Andric                       TreePattern &TP);
8460b57cec5SDimitry Andric   bool UpdateNodeType(unsigned ResNo, MVT::SimpleValueType InTy,
8470b57cec5SDimitry Andric                       TreePattern &TP);
8480b57cec5SDimitry Andric   bool UpdateNodeType(unsigned ResNo, ValueTypeByHwMode InTy,
8490b57cec5SDimitry Andric                       TreePattern &TP);
8500b57cec5SDimitry Andric 
8510b57cec5SDimitry Andric   // Update node type with types inferred from an instruction operand or result
8520b57cec5SDimitry Andric   // def from the ins/outs lists.
8530b57cec5SDimitry Andric   // Return true if the type changed.
8540b57cec5SDimitry Andric   bool UpdateNodeTypeFromInst(unsigned ResNo, Record *Operand, TreePattern &TP);
8550b57cec5SDimitry Andric 
8560b57cec5SDimitry Andric   /// ContainsUnresolvedType - Return true if this tree contains any
8570b57cec5SDimitry Andric   /// unresolved types.
8580b57cec5SDimitry Andric   bool ContainsUnresolvedType(TreePattern &TP) const;
8590b57cec5SDimitry Andric 
8600b57cec5SDimitry Andric   /// canPatternMatch - If it is impossible for this pattern to match on this
8610b57cec5SDimitry Andric   /// target, fill in Reason and return false.  Otherwise, return true.
8620b57cec5SDimitry Andric   bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
8630b57cec5SDimitry Andric };
8640b57cec5SDimitry Andric 
8650b57cec5SDimitry Andric inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
8660b57cec5SDimitry Andric   TPN.print(OS);
8670b57cec5SDimitry Andric   return OS;
8680b57cec5SDimitry Andric }
8690b57cec5SDimitry Andric 
8700b57cec5SDimitry Andric /// TreePattern - Represent a pattern, used for instructions, pattern
8710b57cec5SDimitry Andric /// fragments, etc.
8720b57cec5SDimitry Andric ///
8730b57cec5SDimitry Andric class TreePattern {
8740b57cec5SDimitry Andric   /// Trees - The list of pattern trees which corresponds to this pattern.
8750b57cec5SDimitry Andric   /// Note that PatFrag's only have a single tree.
8760b57cec5SDimitry Andric   ///
8770b57cec5SDimitry Andric   std::vector<TreePatternNodePtr> Trees;
8780b57cec5SDimitry Andric 
8790b57cec5SDimitry Andric   /// NamedNodes - This is all of the nodes that have names in the trees in this
8800b57cec5SDimitry Andric   /// pattern.
8810b57cec5SDimitry Andric   StringMap<SmallVector<TreePatternNode *, 1>> NamedNodes;
8820b57cec5SDimitry Andric 
8830b57cec5SDimitry Andric   /// TheRecord - The actual TableGen record corresponding to this pattern.
8840b57cec5SDimitry Andric   ///
8850b57cec5SDimitry Andric   Record *TheRecord;
8860b57cec5SDimitry Andric 
8870b57cec5SDimitry Andric   /// Args - This is a list of all of the arguments to this pattern (for
8880b57cec5SDimitry Andric   /// PatFrag patterns), which are the 'node' markers in this pattern.
8890b57cec5SDimitry Andric   std::vector<std::string> Args;
8900b57cec5SDimitry Andric 
8910b57cec5SDimitry Andric   /// CDP - the top-level object coordinating this madness.
8920b57cec5SDimitry Andric   ///
8930b57cec5SDimitry Andric   CodeGenDAGPatterns &CDP;
8940b57cec5SDimitry Andric 
8950b57cec5SDimitry Andric   /// isInputPattern - True if this is an input pattern, something to match.
8960b57cec5SDimitry Andric   /// False if this is an output pattern, something to emit.
8970b57cec5SDimitry Andric   bool isInputPattern;
8980b57cec5SDimitry Andric 
8990b57cec5SDimitry Andric   /// hasError - True if the currently processed nodes have unresolvable types
9000b57cec5SDimitry Andric   /// or other non-fatal errors
9010b57cec5SDimitry Andric   bool HasError;
9020b57cec5SDimitry Andric 
9030b57cec5SDimitry Andric   /// It's important that the usage of operands in ComplexPatterns is
9040b57cec5SDimitry Andric   /// consistent: each named operand can be defined by at most one
9050b57cec5SDimitry Andric   /// ComplexPattern. This records the ComplexPattern instance and the operand
9060b57cec5SDimitry Andric   /// number for each operand encountered in a ComplexPattern to aid in that
9070b57cec5SDimitry Andric   /// check.
9080b57cec5SDimitry Andric   StringMap<std::pair<Record *, unsigned>> ComplexPatternOperands;
9090b57cec5SDimitry Andric 
9100b57cec5SDimitry Andric   TypeInfer Infer;
9110b57cec5SDimitry Andric 
9120b57cec5SDimitry Andric public:
9130b57cec5SDimitry Andric 
9140b57cec5SDimitry Andric   /// TreePattern constructor - Parse the specified DagInits into the
9150b57cec5SDimitry Andric   /// current record.
9160b57cec5SDimitry Andric   TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
9170b57cec5SDimitry Andric               CodeGenDAGPatterns &ise);
9180b57cec5SDimitry Andric   TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
9190b57cec5SDimitry Andric               CodeGenDAGPatterns &ise);
9200b57cec5SDimitry Andric   TreePattern(Record *TheRec, TreePatternNodePtr Pat, bool isInput,
9210b57cec5SDimitry Andric               CodeGenDAGPatterns &ise);
9220b57cec5SDimitry Andric 
9230b57cec5SDimitry Andric   /// getTrees - Return the tree patterns which corresponds to this pattern.
9240b57cec5SDimitry Andric   ///
getTrees()9250b57cec5SDimitry Andric   const std::vector<TreePatternNodePtr> &getTrees() const { return Trees; }
getNumTrees()9260b57cec5SDimitry Andric   unsigned getNumTrees() const { return Trees.size(); }
getTree(unsigned i)9270b57cec5SDimitry Andric   const TreePatternNodePtr &getTree(unsigned i) const { return Trees[i]; }
setTree(unsigned i,TreePatternNodePtr Tree)9280b57cec5SDimitry Andric   void setTree(unsigned i, TreePatternNodePtr Tree) { Trees[i] = Tree; }
getOnlyTree()9290b57cec5SDimitry Andric   const TreePatternNodePtr &getOnlyTree() const {
9300b57cec5SDimitry Andric     assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
9310b57cec5SDimitry Andric     return Trees[0];
9320b57cec5SDimitry Andric   }
9330b57cec5SDimitry Andric 
getNamedNodesMap()9340b57cec5SDimitry Andric   const StringMap<SmallVector<TreePatternNode *, 1>> &getNamedNodesMap() {
9350b57cec5SDimitry Andric     if (NamedNodes.empty())
9360b57cec5SDimitry Andric       ComputeNamedNodes();
9370b57cec5SDimitry Andric     return NamedNodes;
9380b57cec5SDimitry Andric   }
9390b57cec5SDimitry Andric 
9400b57cec5SDimitry Andric   /// getRecord - Return the actual TableGen record corresponding to this
9410b57cec5SDimitry Andric   /// pattern.
9420b57cec5SDimitry Andric   ///
getRecord()9430b57cec5SDimitry Andric   Record *getRecord() const { return TheRecord; }
9440b57cec5SDimitry Andric 
getNumArgs()9450b57cec5SDimitry Andric   unsigned getNumArgs() const { return Args.size(); }
getArgName(unsigned i)9460b57cec5SDimitry Andric   const std::string &getArgName(unsigned i) const {
9470b57cec5SDimitry Andric     assert(i < Args.size() && "Argument reference out of range!");
9480b57cec5SDimitry Andric     return Args[i];
9490b57cec5SDimitry Andric   }
getArgList()9500b57cec5SDimitry Andric   std::vector<std::string> &getArgList() { return Args; }
9510b57cec5SDimitry Andric 
getDAGPatterns()9520b57cec5SDimitry Andric   CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
9530b57cec5SDimitry Andric 
9540b57cec5SDimitry Andric   /// InlinePatternFragments - If this pattern refers to any pattern
9550b57cec5SDimitry Andric   /// fragments, inline them into place, giving us a pattern without any
9560b57cec5SDimitry Andric   /// PatFrags references.  This may increase the number of trees in the
9570b57cec5SDimitry Andric   /// pattern if a PatFrags has multiple alternatives.
InlinePatternFragments()9580b57cec5SDimitry Andric   void InlinePatternFragments() {
95906c3fb27SDimitry Andric     std::vector<TreePatternNodePtr> Copy;
96006c3fb27SDimitry Andric     Trees.swap(Copy);
96106c3fb27SDimitry Andric     for (const TreePatternNodePtr &C : Copy)
96206c3fb27SDimitry Andric       C->InlinePatternFragments(*this, Trees);
9630b57cec5SDimitry Andric   }
9640b57cec5SDimitry Andric 
9650b57cec5SDimitry Andric   /// InferAllTypes - Infer/propagate as many types throughout the expression
9660b57cec5SDimitry Andric   /// patterns as possible.  Return true if all types are inferred, false
9670b57cec5SDimitry Andric   /// otherwise.  Bail out if a type contradiction is found.
9680b57cec5SDimitry Andric   bool InferAllTypes(
9690b57cec5SDimitry Andric       const StringMap<SmallVector<TreePatternNode *, 1>> *NamedTypes = nullptr);
9700b57cec5SDimitry Andric 
9710b57cec5SDimitry Andric   /// error - If this is the first error in the current resolution step,
9720b57cec5SDimitry Andric   /// print it and set the error flag.  Otherwise, continue silently.
9730b57cec5SDimitry Andric   void error(const Twine &Msg);
hasError()9740b57cec5SDimitry Andric   bool hasError() const {
9750b57cec5SDimitry Andric     return HasError;
9760b57cec5SDimitry Andric   }
resetError()9770b57cec5SDimitry Andric   void resetError() {
9780b57cec5SDimitry Andric     HasError = false;
9790b57cec5SDimitry Andric   }
9800b57cec5SDimitry Andric 
getInfer()9810b57cec5SDimitry Andric   TypeInfer &getInfer() { return Infer; }
9820b57cec5SDimitry Andric 
9830b57cec5SDimitry Andric   void print(raw_ostream &OS) const;
9840b57cec5SDimitry Andric   void dump() const;
9850b57cec5SDimitry Andric 
9860b57cec5SDimitry Andric private:
9870b57cec5SDimitry Andric   TreePatternNodePtr ParseTreePattern(Init *DI, StringRef OpName);
9880b57cec5SDimitry Andric   void ComputeNamedNodes();
9890b57cec5SDimitry Andric   void ComputeNamedNodes(TreePatternNode *N);
9900b57cec5SDimitry Andric };
9910b57cec5SDimitry Andric 
9920b57cec5SDimitry Andric 
UpdateNodeType(unsigned ResNo,const TypeSetByHwMode & InTy,TreePattern & TP)9930b57cec5SDimitry Andric inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
9940b57cec5SDimitry Andric                                             const TypeSetByHwMode &InTy,
9950b57cec5SDimitry Andric                                             TreePattern &TP) {
9960b57cec5SDimitry Andric   TypeSetByHwMode VTS(InTy);
9970b57cec5SDimitry Andric   TP.getInfer().expandOverloads(VTS);
9980b57cec5SDimitry Andric   return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
9990b57cec5SDimitry Andric }
10000b57cec5SDimitry Andric 
UpdateNodeType(unsigned ResNo,MVT::SimpleValueType InTy,TreePattern & TP)10010b57cec5SDimitry Andric inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
10020b57cec5SDimitry Andric                                             MVT::SimpleValueType InTy,
10030b57cec5SDimitry Andric                                             TreePattern &TP) {
10040b57cec5SDimitry Andric   TypeSetByHwMode VTS(InTy);
10050b57cec5SDimitry Andric   TP.getInfer().expandOverloads(VTS);
10060b57cec5SDimitry Andric   return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
10070b57cec5SDimitry Andric }
10080b57cec5SDimitry Andric 
UpdateNodeType(unsigned ResNo,ValueTypeByHwMode InTy,TreePattern & TP)10090b57cec5SDimitry Andric inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
10100b57cec5SDimitry Andric                                             ValueTypeByHwMode InTy,
10110b57cec5SDimitry Andric                                             TreePattern &TP) {
10120b57cec5SDimitry Andric   TypeSetByHwMode VTS(InTy);
10130b57cec5SDimitry Andric   TP.getInfer().expandOverloads(VTS);
10140b57cec5SDimitry Andric   return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
10150b57cec5SDimitry Andric }
10160b57cec5SDimitry Andric 
10170b57cec5SDimitry Andric 
10180b57cec5SDimitry Andric /// DAGDefaultOperand - One of these is created for each OperandWithDefaultOps
10190b57cec5SDimitry Andric /// that has a set ExecuteAlways / DefaultOps field.
10200b57cec5SDimitry Andric struct DAGDefaultOperand {
10210b57cec5SDimitry Andric   std::vector<TreePatternNodePtr> DefaultOps;
10220b57cec5SDimitry Andric };
10230b57cec5SDimitry Andric 
10240b57cec5SDimitry Andric class DAGInstruction {
10250b57cec5SDimitry Andric   std::vector<Record*> Results;
10260b57cec5SDimitry Andric   std::vector<Record*> Operands;
10270b57cec5SDimitry Andric   std::vector<Record*> ImpResults;
10280b57cec5SDimitry Andric   TreePatternNodePtr SrcPattern;
10290b57cec5SDimitry Andric   TreePatternNodePtr ResultPattern;
10300b57cec5SDimitry Andric 
10310b57cec5SDimitry Andric public:
103206c3fb27SDimitry Andric   DAGInstruction(std::vector<Record *> &&results,
103306c3fb27SDimitry Andric                  std::vector<Record *> &&operands,
103406c3fb27SDimitry Andric                  std::vector<Record *> &&impresults,
10350b57cec5SDimitry Andric                  TreePatternNodePtr srcpattern = nullptr,
10360b57cec5SDimitry Andric                  TreePatternNodePtr resultpattern = nullptr)
Results(std::move (results))103706c3fb27SDimitry Andric       : Results(std::move(results)), Operands(std::move(operands)),
103806c3fb27SDimitry Andric         ImpResults(std::move(impresults)), SrcPattern(srcpattern),
103906c3fb27SDimitry Andric         ResultPattern(resultpattern) {}
10400b57cec5SDimitry Andric 
getNumResults()10410b57cec5SDimitry Andric   unsigned getNumResults() const { return Results.size(); }
getNumOperands()10420b57cec5SDimitry Andric   unsigned getNumOperands() const { return Operands.size(); }
getNumImpResults()10430b57cec5SDimitry Andric   unsigned getNumImpResults() const { return ImpResults.size(); }
getImpResults()10440b57cec5SDimitry Andric   const std::vector<Record*>& getImpResults() const { return ImpResults; }
10450b57cec5SDimitry Andric 
getResult(unsigned RN)10460b57cec5SDimitry Andric   Record *getResult(unsigned RN) const {
10470b57cec5SDimitry Andric     assert(RN < Results.size());
10480b57cec5SDimitry Andric     return Results[RN];
10490b57cec5SDimitry Andric   }
10500b57cec5SDimitry Andric 
getOperand(unsigned ON)10510b57cec5SDimitry Andric   Record *getOperand(unsigned ON) const {
10520b57cec5SDimitry Andric     assert(ON < Operands.size());
10530b57cec5SDimitry Andric     return Operands[ON];
10540b57cec5SDimitry Andric   }
10550b57cec5SDimitry Andric 
getImpResult(unsigned RN)10560b57cec5SDimitry Andric   Record *getImpResult(unsigned RN) const {
10570b57cec5SDimitry Andric     assert(RN < ImpResults.size());
10580b57cec5SDimitry Andric     return ImpResults[RN];
10590b57cec5SDimitry Andric   }
10600b57cec5SDimitry Andric 
getSrcPattern()10610b57cec5SDimitry Andric   TreePatternNodePtr getSrcPattern() const { return SrcPattern; }
getResultPattern()10620b57cec5SDimitry Andric   TreePatternNodePtr getResultPattern() const { return ResultPattern; }
10630b57cec5SDimitry Andric };
10640b57cec5SDimitry Andric 
10650b57cec5SDimitry Andric /// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
10660b57cec5SDimitry Andric /// processed to produce isel.
10670b57cec5SDimitry Andric class PatternToMatch {
10680b57cec5SDimitry Andric   Record          *SrcRecord;   // Originating Record for the pattern.
1069fe6060f1SDimitry Andric   ListInit        *Predicates;  // Top level predicate conditions to match.
10700b57cec5SDimitry Andric   TreePatternNodePtr SrcPattern;      // Source pattern to match.
10710b57cec5SDimitry Andric   TreePatternNodePtr DstPattern;      // Resulting pattern.
10720b57cec5SDimitry Andric   std::vector<Record*> Dstregs; // Physical register defs being matched.
1073fe6060f1SDimitry Andric   std::string      HwModeFeatures;
10740b57cec5SDimitry Andric   int              AddedComplexity; // Add to matching pattern complexity.
10750b57cec5SDimitry Andric   unsigned         ID;          // Unique ID for the record.
10760b57cec5SDimitry Andric 
1077fe6060f1SDimitry Andric public:
1078fe6060f1SDimitry Andric   PatternToMatch(Record *srcrecord, ListInit *preds, TreePatternNodePtr src,
1079fe6060f1SDimitry Andric                  TreePatternNodePtr dst, std::vector<Record *> dstregs,
108006c3fb27SDimitry Andric                  int complexity, unsigned uid,
1081fe6060f1SDimitry Andric                  const Twine &hwmodefeatures = "")
SrcRecord(srcrecord)1082fe6060f1SDimitry Andric       : SrcRecord(srcrecord), Predicates(preds), SrcPattern(src),
1083fe6060f1SDimitry Andric         DstPattern(dst), Dstregs(std::move(dstregs)),
1084fe6060f1SDimitry Andric         HwModeFeatures(hwmodefeatures.str()), AddedComplexity(complexity),
108506c3fb27SDimitry Andric         ID(uid) {}
1086fe6060f1SDimitry Andric 
getSrcRecord()10870b57cec5SDimitry Andric   Record          *getSrcRecord()  const { return SrcRecord; }
getPredicates()1088fe6060f1SDimitry Andric   ListInit        *getPredicates() const { return Predicates; }
getSrcPattern()10890b57cec5SDimitry Andric   TreePatternNode *getSrcPattern() const { return SrcPattern.get(); }
getSrcPatternShared()10900b57cec5SDimitry Andric   TreePatternNodePtr getSrcPatternShared() const { return SrcPattern; }
getDstPattern()10910b57cec5SDimitry Andric   TreePatternNode *getDstPattern() const { return DstPattern.get(); }
getDstPatternShared()10920b57cec5SDimitry Andric   TreePatternNodePtr getDstPatternShared() const { return DstPattern; }
getDstRegs()10930b57cec5SDimitry Andric   const std::vector<Record*> &getDstRegs() const { return Dstregs; }
getHwModeFeatures()1094fe6060f1SDimitry Andric   StringRef   getHwModeFeatures() const { return HwModeFeatures; }
getAddedComplexity()10950b57cec5SDimitry Andric   int         getAddedComplexity() const { return AddedComplexity; }
getID()1096fe6060f1SDimitry Andric   unsigned getID() const { return ID; }
10970b57cec5SDimitry Andric 
10980b57cec5SDimitry Andric   std::string getPredicateCheck() const;
1099fe6060f1SDimitry Andric   void getPredicateRecords(SmallVectorImpl<Record *> &PredicateRecs) const;
11000b57cec5SDimitry Andric 
11010b57cec5SDimitry Andric   /// Compute the complexity metric for the input pattern.  This roughly
11020b57cec5SDimitry Andric   /// corresponds to the number of nodes that are covered.
11030b57cec5SDimitry Andric   int getPatternComplexity(const CodeGenDAGPatterns &CGP) const;
11040b57cec5SDimitry Andric };
11050b57cec5SDimitry Andric 
11060b57cec5SDimitry Andric class CodeGenDAGPatterns {
11070b57cec5SDimitry Andric   RecordKeeper &Records;
11080b57cec5SDimitry Andric   CodeGenTarget Target;
11090b57cec5SDimitry Andric   CodeGenIntrinsicTable Intrinsics;
11100b57cec5SDimitry Andric 
11110b57cec5SDimitry Andric   std::map<Record*, SDNodeInfo, LessRecordByID> SDNodes;
11120b57cec5SDimitry Andric   std::map<Record*, std::pair<Record*, std::string>, LessRecordByID>
11130b57cec5SDimitry Andric       SDNodeXForms;
11140b57cec5SDimitry Andric   std::map<Record*, ComplexPattern, LessRecordByID> ComplexPatterns;
11150b57cec5SDimitry Andric   std::map<Record *, std::unique_ptr<TreePattern>, LessRecordByID>
11160b57cec5SDimitry Andric       PatternFragments;
11170b57cec5SDimitry Andric   std::map<Record*, DAGDefaultOperand, LessRecordByID> DefaultOperands;
11180b57cec5SDimitry Andric   std::map<Record*, DAGInstruction, LessRecordByID> Instructions;
11190b57cec5SDimitry Andric 
11200b57cec5SDimitry Andric   // Specific SDNode definitions:
11210b57cec5SDimitry Andric   Record *intrinsic_void_sdnode;
11220b57cec5SDimitry Andric   Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
11230b57cec5SDimitry Andric 
11240b57cec5SDimitry Andric   /// PatternsToMatch - All of the things we are matching on the DAG.  The first
11250b57cec5SDimitry Andric   /// value is the pattern to match, the second pattern is the result to
11260b57cec5SDimitry Andric   /// emit.
11270b57cec5SDimitry Andric   std::vector<PatternToMatch> PatternsToMatch;
11280b57cec5SDimitry Andric 
11290b57cec5SDimitry Andric   TypeSetByHwMode LegalVTS;
11300b57cec5SDimitry Andric 
11310b57cec5SDimitry Andric   using PatternRewriterFn = std::function<void (TreePattern *)>;
11320b57cec5SDimitry Andric   PatternRewriterFn PatternRewriter;
11330b57cec5SDimitry Andric 
11340b57cec5SDimitry Andric   unsigned NumScopes = 0;
11350b57cec5SDimitry Andric 
11360b57cec5SDimitry Andric public:
11370b57cec5SDimitry Andric   CodeGenDAGPatterns(RecordKeeper &R,
11380b57cec5SDimitry Andric                      PatternRewriterFn PatternRewriter = nullptr);
11390b57cec5SDimitry Andric 
getTargetInfo()11400b57cec5SDimitry Andric   CodeGenTarget &getTargetInfo() { return Target; }
getTargetInfo()11410b57cec5SDimitry Andric   const CodeGenTarget &getTargetInfo() const { return Target; }
getLegalTypes()11420b57cec5SDimitry Andric   const TypeSetByHwMode &getLegalTypes() const { return LegalVTS; }
11430b57cec5SDimitry Andric 
1144e8d8bef9SDimitry Andric   Record *getSDNodeNamed(StringRef Name) const;
11450b57cec5SDimitry Andric 
getSDNodeInfo(Record * R)11460b57cec5SDimitry Andric   const SDNodeInfo &getSDNodeInfo(Record *R) const {
11470b57cec5SDimitry Andric     auto F = SDNodes.find(R);
11480b57cec5SDimitry Andric     assert(F != SDNodes.end() && "Unknown node!");
11490b57cec5SDimitry Andric     return F->second;
11500b57cec5SDimitry Andric   }
11510b57cec5SDimitry Andric 
11520b57cec5SDimitry Andric   // Node transformation lookups.
11530b57cec5SDimitry Andric   typedef std::pair<Record*, std::string> NodeXForm;
getSDNodeTransform(Record * R)11540b57cec5SDimitry Andric   const NodeXForm &getSDNodeTransform(Record *R) const {
11550b57cec5SDimitry Andric     auto F = SDNodeXForms.find(R);
11560b57cec5SDimitry Andric     assert(F != SDNodeXForms.end() && "Invalid transform!");
11570b57cec5SDimitry Andric     return F->second;
11580b57cec5SDimitry Andric   }
11590b57cec5SDimitry Andric 
getComplexPattern(Record * R)11600b57cec5SDimitry Andric   const ComplexPattern &getComplexPattern(Record *R) const {
11610b57cec5SDimitry Andric     auto F = ComplexPatterns.find(R);
11620b57cec5SDimitry Andric     assert(F != ComplexPatterns.end() && "Unknown addressing mode!");
11630b57cec5SDimitry Andric     return F->second;
11640b57cec5SDimitry Andric   }
11650b57cec5SDimitry Andric 
getIntrinsic(Record * R)11660b57cec5SDimitry Andric   const CodeGenIntrinsic &getIntrinsic(Record *R) const {
11670b57cec5SDimitry Andric     for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
11680b57cec5SDimitry Andric       if (Intrinsics[i].TheDef == R) return Intrinsics[i];
11690b57cec5SDimitry Andric     llvm_unreachable("Unknown intrinsic!");
11700b57cec5SDimitry Andric   }
11710b57cec5SDimitry Andric 
getIntrinsicInfo(unsigned IID)11720b57cec5SDimitry Andric   const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
11730b57cec5SDimitry Andric     if (IID-1 < Intrinsics.size())
11740b57cec5SDimitry Andric       return Intrinsics[IID-1];
11750b57cec5SDimitry Andric     llvm_unreachable("Bad intrinsic ID!");
11760b57cec5SDimitry Andric   }
11770b57cec5SDimitry Andric 
getIntrinsicID(Record * R)11780b57cec5SDimitry Andric   unsigned getIntrinsicID(Record *R) const {
11790b57cec5SDimitry Andric     for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
11800b57cec5SDimitry Andric       if (Intrinsics[i].TheDef == R) return i;
11810b57cec5SDimitry Andric     llvm_unreachable("Unknown intrinsic!");
11820b57cec5SDimitry Andric   }
11830b57cec5SDimitry Andric 
getDefaultOperand(Record * R)11840b57cec5SDimitry Andric   const DAGDefaultOperand &getDefaultOperand(Record *R) const {
11850b57cec5SDimitry Andric     auto F = DefaultOperands.find(R);
11860b57cec5SDimitry Andric     assert(F != DefaultOperands.end() &&"Isn't an analyzed default operand!");
11870b57cec5SDimitry Andric     return F->second;
11880b57cec5SDimitry Andric   }
11890b57cec5SDimitry Andric 
11900b57cec5SDimitry Andric   // Pattern Fragment information.
getPatternFragment(Record * R)11910b57cec5SDimitry Andric   TreePattern *getPatternFragment(Record *R) const {
11920b57cec5SDimitry Andric     auto F = PatternFragments.find(R);
11930b57cec5SDimitry Andric     assert(F != PatternFragments.end() && "Invalid pattern fragment request!");
11940b57cec5SDimitry Andric     return F->second.get();
11950b57cec5SDimitry Andric   }
getPatternFragmentIfRead(Record * R)11960b57cec5SDimitry Andric   TreePattern *getPatternFragmentIfRead(Record *R) const {
11970b57cec5SDimitry Andric     auto F = PatternFragments.find(R);
11980b57cec5SDimitry Andric     if (F == PatternFragments.end())
11990b57cec5SDimitry Andric       return nullptr;
12000b57cec5SDimitry Andric     return F->second.get();
12010b57cec5SDimitry Andric   }
12020b57cec5SDimitry Andric 
12030b57cec5SDimitry Andric   typedef std::map<Record *, std::unique_ptr<TreePattern>,
12040b57cec5SDimitry Andric                    LessRecordByID>::const_iterator pf_iterator;
pf_begin()12050b57cec5SDimitry Andric   pf_iterator pf_begin() const { return PatternFragments.begin(); }
pf_end()12060b57cec5SDimitry Andric   pf_iterator pf_end() const { return PatternFragments.end(); }
ptfs()12070b57cec5SDimitry Andric   iterator_range<pf_iterator> ptfs() const { return PatternFragments; }
12080b57cec5SDimitry Andric 
12090b57cec5SDimitry Andric   // Patterns to match information.
12100b57cec5SDimitry Andric   typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
ptm_begin()12110b57cec5SDimitry Andric   ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
ptm_end()12120b57cec5SDimitry Andric   ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
ptms()12130b57cec5SDimitry Andric   iterator_range<ptm_iterator> ptms() const { return PatternsToMatch; }
12140b57cec5SDimitry Andric 
12150b57cec5SDimitry Andric   /// Parse the Pattern for an instruction, and insert the result in DAGInsts.
12160b57cec5SDimitry Andric   typedef std::map<Record*, DAGInstruction, LessRecordByID> DAGInstMap;
12170b57cec5SDimitry Andric   void parseInstructionPattern(
12180b57cec5SDimitry Andric       CodeGenInstruction &CGI, ListInit *Pattern,
12190b57cec5SDimitry Andric       DAGInstMap &DAGInsts);
12200b57cec5SDimitry Andric 
getInstruction(Record * R)12210b57cec5SDimitry Andric   const DAGInstruction &getInstruction(Record *R) const {
12220b57cec5SDimitry Andric     auto F = Instructions.find(R);
12230b57cec5SDimitry Andric     assert(F != Instructions.end() && "Unknown instruction!");
12240b57cec5SDimitry Andric     return F->second;
12250b57cec5SDimitry Andric   }
12260b57cec5SDimitry Andric 
get_intrinsic_void_sdnode()12270b57cec5SDimitry Andric   Record *get_intrinsic_void_sdnode() const {
12280b57cec5SDimitry Andric     return intrinsic_void_sdnode;
12290b57cec5SDimitry Andric   }
get_intrinsic_w_chain_sdnode()12300b57cec5SDimitry Andric   Record *get_intrinsic_w_chain_sdnode() const {
12310b57cec5SDimitry Andric     return intrinsic_w_chain_sdnode;
12320b57cec5SDimitry Andric   }
get_intrinsic_wo_chain_sdnode()12330b57cec5SDimitry Andric   Record *get_intrinsic_wo_chain_sdnode() const {
12340b57cec5SDimitry Andric     return intrinsic_wo_chain_sdnode;
12350b57cec5SDimitry Andric   }
12360b57cec5SDimitry Andric 
allocateScope()12370b57cec5SDimitry Andric   unsigned allocateScope() { return ++NumScopes; }
12380b57cec5SDimitry Andric 
operandHasDefault(Record * Op)12390b57cec5SDimitry Andric   bool operandHasDefault(Record *Op) const {
12400b57cec5SDimitry Andric     return Op->isSubClassOf("OperandWithDefaultOps") &&
12410b57cec5SDimitry Andric       !getDefaultOperand(Op).DefaultOps.empty();
12420b57cec5SDimitry Andric   }
12430b57cec5SDimitry Andric 
12440b57cec5SDimitry Andric private:
12450b57cec5SDimitry Andric   void ParseNodeInfo();
12460b57cec5SDimitry Andric   void ParseNodeTransforms();
12470b57cec5SDimitry Andric   void ParseComplexPatterns();
12480b57cec5SDimitry Andric   void ParsePatternFragments(bool OutFrags = false);
12490b57cec5SDimitry Andric   void ParseDefaultOperands();
12500b57cec5SDimitry Andric   void ParseInstructions();
12510b57cec5SDimitry Andric   void ParsePatterns();
12520b57cec5SDimitry Andric   void ExpandHwModeBasedTypes();
12530b57cec5SDimitry Andric   void InferInstructionFlags();
12540b57cec5SDimitry Andric   void GenerateVariants();
12550b57cec5SDimitry Andric   void VerifyInstructionFlags();
12560b57cec5SDimitry Andric 
12570b57cec5SDimitry Andric   void ParseOnePattern(Record *TheDef,
12580b57cec5SDimitry Andric                        TreePattern &Pattern, TreePattern &Result,
12590b57cec5SDimitry Andric                        const std::vector<Record *> &InstImpResults);
12600b57cec5SDimitry Andric   void AddPatternToMatch(TreePattern *Pattern, PatternToMatch &&PTM);
12610b57cec5SDimitry Andric   void FindPatternInputsAndOutputs(
12620b57cec5SDimitry Andric       TreePattern &I, TreePatternNodePtr Pat,
12630b57cec5SDimitry Andric       std::map<std::string, TreePatternNodePtr> &InstInputs,
12640b57cec5SDimitry Andric       MapVector<std::string, TreePatternNodePtr,
12650b57cec5SDimitry Andric                 std::map<std::string, unsigned>> &InstResults,
12660b57cec5SDimitry Andric       std::vector<Record *> &InstImpResults);
12670b57cec5SDimitry Andric };
12680b57cec5SDimitry Andric 
12690b57cec5SDimitry Andric 
ApplyTypeConstraints(TreePatternNode * N,TreePattern & TP)12700b57cec5SDimitry Andric inline bool SDNodeInfo::ApplyTypeConstraints(TreePatternNode *N,
12710b57cec5SDimitry Andric                                              TreePattern &TP) const {
12720b57cec5SDimitry Andric     bool MadeChange = false;
12730b57cec5SDimitry Andric     for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
12740b57cec5SDimitry Andric       MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
12750b57cec5SDimitry Andric     return MadeChange;
12760b57cec5SDimitry Andric   }
12770b57cec5SDimitry Andric 
12780b57cec5SDimitry Andric } // end namespace llvm
12790b57cec5SDimitry Andric 
12800b57cec5SDimitry Andric #endif
1281