xref: /freebsd/contrib/llvm-project/clang/utils/TableGen/NeonEmitter.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
10b57cec5SDimitry Andric //===- NeonEmitter.cpp - Generate arm_neon.h for use with clang -*- 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 tablegen backend is responsible for emitting arm_neon.h, which includes
100b57cec5SDimitry Andric // a declaration and definition of each function specified by the ARM NEON
110b57cec5SDimitry Andric // compiler interface.  See ARM document DUI0348B.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric // Each NEON instruction is implemented in terms of 1 or more functions which
140b57cec5SDimitry Andric // are suffixed with the element type of the input vectors.  Functions may be
150b57cec5SDimitry Andric // implemented in terms of generic vector operations such as +, *, -, etc. or
160b57cec5SDimitry Andric // by calling a __builtin_-prefixed function which will be handled by clang's
170b57cec5SDimitry Andric // CodeGen library.
180b57cec5SDimitry Andric //
190b57cec5SDimitry Andric // Additional validation code can be generated by this file when runHeader() is
200b57cec5SDimitry Andric // called, rather than the normal run() entry point.
210b57cec5SDimitry Andric //
220b57cec5SDimitry Andric // See also the documentation in include/clang/Basic/arm_neon.td.
230b57cec5SDimitry Andric //
240b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
250b57cec5SDimitry Andric 
26a7dea167SDimitry Andric #include "TableGenBackends.h"
270b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
280b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
290b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
305ffd83dbSDimitry Andric #include "llvm/ADT/SmallVector.h"
310b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h"
320b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
330b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
340b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
350b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
360b57cec5SDimitry Andric #include "llvm/TableGen/Error.h"
370b57cec5SDimitry Andric #include "llvm/TableGen/Record.h"
380b57cec5SDimitry Andric #include "llvm/TableGen/SetTheory.h"
390b57cec5SDimitry Andric #include <algorithm>
400b57cec5SDimitry Andric #include <cassert>
410b57cec5SDimitry Andric #include <cctype>
420b57cec5SDimitry Andric #include <cstddef>
430b57cec5SDimitry Andric #include <cstdint>
440b57cec5SDimitry Andric #include <deque>
450b57cec5SDimitry Andric #include <map>
46*bdd1243dSDimitry Andric #include <optional>
470b57cec5SDimitry Andric #include <set>
480b57cec5SDimitry Andric #include <sstream>
490b57cec5SDimitry Andric #include <string>
500b57cec5SDimitry Andric #include <utility>
510b57cec5SDimitry Andric #include <vector>
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric using namespace llvm;
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric namespace {
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric // While globals are generally bad, this one allows us to perform assertions
580b57cec5SDimitry Andric // liberally and somehow still trace them back to the def they indirectly
590b57cec5SDimitry Andric // came from.
600b57cec5SDimitry Andric static Record *CurrentRecord = nullptr;
610b57cec5SDimitry Andric static void assert_with_loc(bool Assertion, const std::string &Str) {
620b57cec5SDimitry Andric   if (!Assertion) {
630b57cec5SDimitry Andric     if (CurrentRecord)
640b57cec5SDimitry Andric       PrintFatalError(CurrentRecord->getLoc(), Str);
650b57cec5SDimitry Andric     else
660b57cec5SDimitry Andric       PrintFatalError(Str);
670b57cec5SDimitry Andric   }
680b57cec5SDimitry Andric }
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric enum ClassKind {
710b57cec5SDimitry Andric   ClassNone,
720b57cec5SDimitry Andric   ClassI,     // generic integer instruction, e.g., "i8" suffix
730b57cec5SDimitry Andric   ClassS,     // signed/unsigned/poly, e.g., "s8", "u8" or "p8" suffix
740b57cec5SDimitry Andric   ClassW,     // width-specific instruction, e.g., "8" suffix
750b57cec5SDimitry Andric   ClassB,     // bitcast arguments with enum argument to specify type
760b57cec5SDimitry Andric   ClassL,     // Logical instructions which are op instructions
770b57cec5SDimitry Andric               // but we need to not emit any suffix for in our
780b57cec5SDimitry Andric               // tests.
790b57cec5SDimitry Andric   ClassNoTest // Instructions which we do not test since they are
800b57cec5SDimitry Andric               // not TRUE instructions.
810b57cec5SDimitry Andric };
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric /// NeonTypeFlags - Flags to identify the types for overloaded Neon
840b57cec5SDimitry Andric /// builtins.  These must be kept in sync with the flags in
850b57cec5SDimitry Andric /// include/clang/Basic/TargetBuiltins.h.
860b57cec5SDimitry Andric namespace NeonTypeFlags {
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric enum { EltTypeMask = 0xf, UnsignedFlag = 0x10, QuadFlag = 0x20 };
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric enum EltType {
910b57cec5SDimitry Andric   Int8,
920b57cec5SDimitry Andric   Int16,
930b57cec5SDimitry Andric   Int32,
940b57cec5SDimitry Andric   Int64,
950b57cec5SDimitry Andric   Poly8,
960b57cec5SDimitry Andric   Poly16,
970b57cec5SDimitry Andric   Poly64,
980b57cec5SDimitry Andric   Poly128,
990b57cec5SDimitry Andric   Float16,
1000b57cec5SDimitry Andric   Float32,
1015ffd83dbSDimitry Andric   Float64,
1025ffd83dbSDimitry Andric   BFloat16
1030b57cec5SDimitry Andric };
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric } // end namespace NeonTypeFlags
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric class NeonEmitter;
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1100b57cec5SDimitry Andric // TypeSpec
1110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric /// A TypeSpec is just a simple wrapper around a string, but gets its own type
1140b57cec5SDimitry Andric /// for strong typing purposes.
1150b57cec5SDimitry Andric ///
1160b57cec5SDimitry Andric /// A TypeSpec can be used to create a type.
1170b57cec5SDimitry Andric class TypeSpec : public std::string {
1180b57cec5SDimitry Andric public:
1190b57cec5SDimitry Andric   static std::vector<TypeSpec> fromTypeSpecs(StringRef Str) {
1200b57cec5SDimitry Andric     std::vector<TypeSpec> Ret;
1210b57cec5SDimitry Andric     TypeSpec Acc;
1220b57cec5SDimitry Andric     for (char I : Str.str()) {
1230b57cec5SDimitry Andric       if (islower(I)) {
1240b57cec5SDimitry Andric         Acc.push_back(I);
1250b57cec5SDimitry Andric         Ret.push_back(TypeSpec(Acc));
1260b57cec5SDimitry Andric         Acc.clear();
1270b57cec5SDimitry Andric       } else {
1280b57cec5SDimitry Andric         Acc.push_back(I);
1290b57cec5SDimitry Andric       }
1300b57cec5SDimitry Andric     }
1310b57cec5SDimitry Andric     return Ret;
1320b57cec5SDimitry Andric   }
1330b57cec5SDimitry Andric };
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1360b57cec5SDimitry Andric // Type
1370b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric /// A Type. Not much more to say here.
1400b57cec5SDimitry Andric class Type {
1410b57cec5SDimitry Andric private:
1420b57cec5SDimitry Andric   TypeSpec TS;
1430b57cec5SDimitry Andric 
144480093f4SDimitry Andric   enum TypeKind {
145480093f4SDimitry Andric     Void,
146480093f4SDimitry Andric     Float,
147480093f4SDimitry Andric     SInt,
148480093f4SDimitry Andric     UInt,
149480093f4SDimitry Andric     Poly,
1505ffd83dbSDimitry Andric     BFloat16,
151480093f4SDimitry Andric   };
152480093f4SDimitry Andric   TypeKind Kind;
153480093f4SDimitry Andric   bool Immediate, Constant, Pointer;
1540b57cec5SDimitry Andric   // ScalarForMangling and NoManglingQ are really not suited to live here as
1550b57cec5SDimitry Andric   // they are not related to the type. But they live in the TypeSpec (not the
1560b57cec5SDimitry Andric   // prototype), so this is really the only place to store them.
1570b57cec5SDimitry Andric   bool ScalarForMangling, NoManglingQ;
1580b57cec5SDimitry Andric   unsigned Bitwidth, ElementBitwidth, NumVectors;
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric public:
1610b57cec5SDimitry Andric   Type()
162480093f4SDimitry Andric       : Kind(Void), Immediate(false), Constant(false),
163480093f4SDimitry Andric         Pointer(false), ScalarForMangling(false), NoManglingQ(false),
164480093f4SDimitry Andric         Bitwidth(0), ElementBitwidth(0), NumVectors(0) {}
1650b57cec5SDimitry Andric 
166480093f4SDimitry Andric   Type(TypeSpec TS, StringRef CharMods)
167480093f4SDimitry Andric       : TS(std::move(TS)), Kind(Void), Immediate(false),
168480093f4SDimitry Andric         Constant(false), Pointer(false), ScalarForMangling(false),
169480093f4SDimitry Andric         NoManglingQ(false), Bitwidth(0), ElementBitwidth(0), NumVectors(0) {
170480093f4SDimitry Andric     applyModifiers(CharMods);
1710b57cec5SDimitry Andric   }
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric   /// Returns a type representing "void".
1740b57cec5SDimitry Andric   static Type getVoid() { return Type(); }
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric   bool operator==(const Type &Other) const { return str() == Other.str(); }
1770b57cec5SDimitry Andric   bool operator!=(const Type &Other) const { return !operator==(Other); }
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric   //
1800b57cec5SDimitry Andric   // Query functions
1810b57cec5SDimitry Andric   //
1820b57cec5SDimitry Andric   bool isScalarForMangling() const { return ScalarForMangling; }
1830b57cec5SDimitry Andric   bool noManglingQ() const { return NoManglingQ; }
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric   bool isPointer() const { return Pointer; }
186480093f4SDimitry Andric   bool isValue() const { return !isVoid() && !isPointer(); }
187480093f4SDimitry Andric   bool isScalar() const { return isValue() && NumVectors == 0; }
188480093f4SDimitry Andric   bool isVector() const { return isValue() && NumVectors > 0; }
189480093f4SDimitry Andric   bool isConstPointer() const { return Constant; }
190480093f4SDimitry Andric   bool isFloating() const { return Kind == Float; }
191480093f4SDimitry Andric   bool isInteger() const { return Kind == SInt || Kind == UInt; }
192480093f4SDimitry Andric   bool isPoly() const { return Kind == Poly; }
193480093f4SDimitry Andric   bool isSigned() const { return Kind == SInt; }
1940b57cec5SDimitry Andric   bool isImmediate() const { return Immediate; }
195480093f4SDimitry Andric   bool isFloat() const { return isFloating() && ElementBitwidth == 32; }
196480093f4SDimitry Andric   bool isDouble() const { return isFloating() && ElementBitwidth == 64; }
197480093f4SDimitry Andric   bool isHalf() const { return isFloating() && ElementBitwidth == 16; }
1980b57cec5SDimitry Andric   bool isChar() const { return ElementBitwidth == 8; }
199480093f4SDimitry Andric   bool isShort() const { return isInteger() && ElementBitwidth == 16; }
200480093f4SDimitry Andric   bool isInt() const { return isInteger() && ElementBitwidth == 32; }
201480093f4SDimitry Andric   bool isLong() const { return isInteger() && ElementBitwidth == 64; }
202480093f4SDimitry Andric   bool isVoid() const { return Kind == Void; }
2035ffd83dbSDimitry Andric   bool isBFloat16() const { return Kind == BFloat16; }
2040b57cec5SDimitry Andric   unsigned getNumElements() const { return Bitwidth / ElementBitwidth; }
2050b57cec5SDimitry Andric   unsigned getSizeInBits() const { return Bitwidth; }
2060b57cec5SDimitry Andric   unsigned getElementSizeInBits() const { return ElementBitwidth; }
2070b57cec5SDimitry Andric   unsigned getNumVectors() const { return NumVectors; }
2080b57cec5SDimitry Andric 
2090b57cec5SDimitry Andric   //
2100b57cec5SDimitry Andric   // Mutator functions
2110b57cec5SDimitry Andric   //
212480093f4SDimitry Andric   void makeUnsigned() {
213480093f4SDimitry Andric     assert(!isVoid() && "not a potentially signed type");
214480093f4SDimitry Andric     Kind = UInt;
215480093f4SDimitry Andric   }
216480093f4SDimitry Andric   void makeSigned() {
217480093f4SDimitry Andric     assert(!isVoid() && "not a potentially signed type");
218480093f4SDimitry Andric     Kind = SInt;
219480093f4SDimitry Andric   }
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric   void makeInteger(unsigned ElemWidth, bool Sign) {
222480093f4SDimitry Andric     assert(!isVoid() && "converting void to int probably not useful");
223480093f4SDimitry Andric     Kind = Sign ? SInt : UInt;
2240b57cec5SDimitry Andric     Immediate = false;
2250b57cec5SDimitry Andric     ElementBitwidth = ElemWidth;
2260b57cec5SDimitry Andric   }
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric   void makeImmediate(unsigned ElemWidth) {
229480093f4SDimitry Andric     Kind = SInt;
2300b57cec5SDimitry Andric     Immediate = true;
2310b57cec5SDimitry Andric     ElementBitwidth = ElemWidth;
2320b57cec5SDimitry Andric   }
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric   void makeScalar() {
2350b57cec5SDimitry Andric     Bitwidth = ElementBitwidth;
2360b57cec5SDimitry Andric     NumVectors = 0;
2370b57cec5SDimitry Andric   }
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric   void makeOneVector() {
2400b57cec5SDimitry Andric     assert(isVector());
2410b57cec5SDimitry Andric     NumVectors = 1;
2420b57cec5SDimitry Andric   }
2430b57cec5SDimitry Andric 
2445ffd83dbSDimitry Andric   void make32BitElement() {
2455ffd83dbSDimitry Andric     assert_with_loc(Bitwidth > 32, "Not enough bits to make it 32!");
2465ffd83dbSDimitry Andric     ElementBitwidth = 32;
2475ffd83dbSDimitry Andric   }
2485ffd83dbSDimitry Andric 
2490b57cec5SDimitry Andric   void doubleLanes() {
2500b57cec5SDimitry Andric     assert_with_loc(Bitwidth != 128, "Can't get bigger than 128!");
2510b57cec5SDimitry Andric     Bitwidth = 128;
2520b57cec5SDimitry Andric   }
2530b57cec5SDimitry Andric 
2540b57cec5SDimitry Andric   void halveLanes() {
2550b57cec5SDimitry Andric     assert_with_loc(Bitwidth != 64, "Can't get smaller than 64!");
2560b57cec5SDimitry Andric     Bitwidth = 64;
2570b57cec5SDimitry Andric   }
2580b57cec5SDimitry Andric 
2590b57cec5SDimitry Andric   /// Return the C string representation of a type, which is the typename
2600b57cec5SDimitry Andric   /// defined in stdint.h or arm_neon.h.
2610b57cec5SDimitry Andric   std::string str() const;
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric   /// Return the string representation of a type, which is an encoded
2640b57cec5SDimitry Andric   /// string for passing to the BUILTIN() macro in Builtins.def.
2650b57cec5SDimitry Andric   std::string builtin_str() const;
2660b57cec5SDimitry Andric 
2670b57cec5SDimitry Andric   /// Return the value in NeonTypeFlags for this type.
2680b57cec5SDimitry Andric   unsigned getNeonEnum() const;
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric   /// Parse a type from a stdint.h or arm_neon.h typedef name,
2710b57cec5SDimitry Andric   /// for example uint32x2_t or int64_t.
2720b57cec5SDimitry Andric   static Type fromTypedefName(StringRef Name);
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric private:
2750b57cec5SDimitry Andric   /// Creates the type based on the typespec string in TS.
2760b57cec5SDimitry Andric   /// Sets "Quad" to true if the "Q" or "H" modifiers were
2770b57cec5SDimitry Andric   /// seen. This is needed by applyModifier as some modifiers
2780b57cec5SDimitry Andric   /// only take effect if the type size was changed by "Q" or "H".
2790b57cec5SDimitry Andric   void applyTypespec(bool &Quad);
280480093f4SDimitry Andric   /// Applies prototype modifiers to the type.
281480093f4SDimitry Andric   void applyModifiers(StringRef Mods);
2820b57cec5SDimitry Andric };
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2850b57cec5SDimitry Andric // Variable
2860b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2870b57cec5SDimitry Andric 
2880b57cec5SDimitry Andric /// A variable is a simple class that just has a type and a name.
2890b57cec5SDimitry Andric class Variable {
2900b57cec5SDimitry Andric   Type T;
2910b57cec5SDimitry Andric   std::string N;
2920b57cec5SDimitry Andric 
2930b57cec5SDimitry Andric public:
29404eeddc0SDimitry Andric   Variable() : T(Type::getVoid()) {}
2950b57cec5SDimitry Andric   Variable(Type T, std::string N) : T(std::move(T)), N(std::move(N)) {}
2960b57cec5SDimitry Andric 
2970b57cec5SDimitry Andric   Type getType() const { return T; }
2980b57cec5SDimitry Andric   std::string getName() const { return "__" + N; }
2990b57cec5SDimitry Andric };
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3020b57cec5SDimitry Andric // Intrinsic
3030b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3040b57cec5SDimitry Andric 
3050b57cec5SDimitry Andric /// The main grunt class. This represents an instantiation of an intrinsic with
3060b57cec5SDimitry Andric /// a particular typespec and prototype.
3070b57cec5SDimitry Andric class Intrinsic {
3080b57cec5SDimitry Andric   /// The Record this intrinsic was created from.
3090b57cec5SDimitry Andric   Record *R;
310480093f4SDimitry Andric   /// The unmangled name.
311480093f4SDimitry Andric   std::string Name;
3120b57cec5SDimitry Andric   /// The input and output typespecs. InTS == OutTS except when
3135ffd83dbSDimitry Andric   /// CartesianProductWith is non-empty - this is the case for vreinterpret.
3140b57cec5SDimitry Andric   TypeSpec OutTS, InTS;
3150b57cec5SDimitry Andric   /// The base class kind. Most intrinsics use ClassS, which has full type
3160b57cec5SDimitry Andric   /// info for integers (s32/u32). Some use ClassI, which doesn't care about
3170b57cec5SDimitry Andric   /// signedness (i32), while some (ClassB) have no type at all, only a width
3180b57cec5SDimitry Andric   /// (32).
3190b57cec5SDimitry Andric   ClassKind CK;
3200b57cec5SDimitry Andric   /// The list of DAGs for the body. May be empty, in which case we should
3210b57cec5SDimitry Andric   /// emit a builtin call.
3220b57cec5SDimitry Andric   ListInit *Body;
323*bdd1243dSDimitry Andric   /// The architectural ifdef guard.
324*bdd1243dSDimitry Andric   std::string ArchGuard;
325*bdd1243dSDimitry Andric   /// The architectural target() guard.
326*bdd1243dSDimitry Andric   std::string TargetGuard;
3270b57cec5SDimitry Andric   /// Set if the Unavailable bit is 1. This means we don't generate a body,
3280b57cec5SDimitry Andric   /// just an "unavailable" attribute on a declaration.
3290b57cec5SDimitry Andric   bool IsUnavailable;
3300b57cec5SDimitry Andric   /// Is this intrinsic safe for big-endian? or does it need its arguments
3310b57cec5SDimitry Andric   /// reversing?
3320b57cec5SDimitry Andric   bool BigEndianSafe;
3330b57cec5SDimitry Andric 
3340b57cec5SDimitry Andric   /// The types of return value [0] and parameters [1..].
3350b57cec5SDimitry Andric   std::vector<Type> Types;
336480093f4SDimitry Andric   /// The index of the key type passed to CGBuiltin.cpp for polymorphic calls.
337480093f4SDimitry Andric   int PolymorphicKeyType;
3380b57cec5SDimitry Andric   /// The local variables defined.
3390b57cec5SDimitry Andric   std::map<std::string, Variable> Variables;
3400b57cec5SDimitry Andric   /// NeededEarly - set if any other intrinsic depends on this intrinsic.
3410b57cec5SDimitry Andric   bool NeededEarly;
3420b57cec5SDimitry Andric   /// UseMacro - set if we should implement using a macro or unset for a
3430b57cec5SDimitry Andric   ///            function.
3440b57cec5SDimitry Andric   bool UseMacro;
3450b57cec5SDimitry Andric   /// The set of intrinsics that this intrinsic uses/requires.
3460b57cec5SDimitry Andric   std::set<Intrinsic *> Dependencies;
3470b57cec5SDimitry Andric   /// The "base type", which is Type('d', OutTS). InBaseType is only
3485ffd83dbSDimitry Andric   /// different if CartesianProductWith is non-empty (for vreinterpret).
3490b57cec5SDimitry Andric   Type BaseType, InBaseType;
3500b57cec5SDimitry Andric   /// The return variable.
3510b57cec5SDimitry Andric   Variable RetVar;
3520b57cec5SDimitry Andric   /// A postfix to apply to every variable. Defaults to "".
3530b57cec5SDimitry Andric   std::string VariablePostfix;
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric   NeonEmitter &Emitter;
3560b57cec5SDimitry Andric   std::stringstream OS;
3570b57cec5SDimitry Andric 
358a7dea167SDimitry Andric   bool isBigEndianSafe() const {
359a7dea167SDimitry Andric     if (BigEndianSafe)
360a7dea167SDimitry Andric       return true;
361a7dea167SDimitry Andric 
362a7dea167SDimitry Andric     for (const auto &T : Types){
363a7dea167SDimitry Andric       if (T.isVector() && T.getNumElements() > 1)
364a7dea167SDimitry Andric         return false;
365a7dea167SDimitry Andric     }
366a7dea167SDimitry Andric     return true;
367a7dea167SDimitry Andric   }
368a7dea167SDimitry Andric 
3690b57cec5SDimitry Andric public:
3700b57cec5SDimitry Andric   Intrinsic(Record *R, StringRef Name, StringRef Proto, TypeSpec OutTS,
3710b57cec5SDimitry Andric             TypeSpec InTS, ClassKind CK, ListInit *Body, NeonEmitter &Emitter,
372*bdd1243dSDimitry Andric             StringRef ArchGuard, StringRef TargetGuard, bool IsUnavailable, bool BigEndianSafe)
373480093f4SDimitry Andric       : R(R), Name(Name.str()), OutTS(OutTS), InTS(InTS), CK(CK), Body(Body),
374*bdd1243dSDimitry Andric         ArchGuard(ArchGuard.str()), TargetGuard(TargetGuard.str()), IsUnavailable(IsUnavailable),
375480093f4SDimitry Andric         BigEndianSafe(BigEndianSafe), PolymorphicKeyType(0), NeededEarly(false),
376480093f4SDimitry Andric         UseMacro(false), BaseType(OutTS, "."), InBaseType(InTS, "."),
377480093f4SDimitry Andric         Emitter(Emitter) {
3780b57cec5SDimitry Andric     // Modify the TypeSpec per-argument to get a concrete Type, and create
3790b57cec5SDimitry Andric     // known variables for each.
3800b57cec5SDimitry Andric     // Types[0] is the return value.
381480093f4SDimitry Andric     unsigned Pos = 0;
382480093f4SDimitry Andric     Types.emplace_back(OutTS, getNextModifiers(Proto, Pos));
383480093f4SDimitry Andric     StringRef Mods = getNextModifiers(Proto, Pos);
384480093f4SDimitry Andric     while (!Mods.empty()) {
385480093f4SDimitry Andric       Types.emplace_back(InTS, Mods);
386349cc55cSDimitry Andric       if (Mods.contains('!'))
387480093f4SDimitry Andric         PolymorphicKeyType = Types.size() - 1;
388480093f4SDimitry Andric 
389480093f4SDimitry Andric       Mods = getNextModifiers(Proto, Pos);
390480093f4SDimitry Andric     }
391480093f4SDimitry Andric 
392480093f4SDimitry Andric     for (auto Type : Types) {
393480093f4SDimitry Andric       // If this builtin takes an immediate argument, we need to #define it rather
394480093f4SDimitry Andric       // than use a standard declaration, so that SemaChecking can range check
395480093f4SDimitry Andric       // the immediate passed by the user.
396480093f4SDimitry Andric 
397480093f4SDimitry Andric       // Pointer arguments need to use macros to avoid hiding aligned attributes
398480093f4SDimitry Andric       // from the pointer type.
399480093f4SDimitry Andric 
400480093f4SDimitry Andric       // It is not permitted to pass or return an __fp16 by value, so intrinsics
401480093f4SDimitry Andric       // taking a scalar float16_t must be implemented as macros.
402480093f4SDimitry Andric       if (Type.isImmediate() || Type.isPointer() ||
403480093f4SDimitry Andric           (Type.isScalar() && Type.isHalf()))
404480093f4SDimitry Andric         UseMacro = true;
405480093f4SDimitry Andric     }
4060b57cec5SDimitry Andric   }
4070b57cec5SDimitry Andric 
4080b57cec5SDimitry Andric   /// Get the Record that this intrinsic is based off.
4090b57cec5SDimitry Andric   Record *getRecord() const { return R; }
4100b57cec5SDimitry Andric   /// Get the set of Intrinsics that this intrinsic calls.
4110b57cec5SDimitry Andric   /// this is the set of immediate dependencies, NOT the
4120b57cec5SDimitry Andric   /// transitive closure.
4130b57cec5SDimitry Andric   const std::set<Intrinsic *> &getDependencies() const { return Dependencies; }
4140b57cec5SDimitry Andric   /// Get the architectural guard string (#ifdef).
415*bdd1243dSDimitry Andric   std::string getArchGuard() const { return ArchGuard; }
416*bdd1243dSDimitry Andric   std::string getTargetGuard() const { return TargetGuard; }
4170b57cec5SDimitry Andric   /// Get the non-mangled name.
4180b57cec5SDimitry Andric   std::string getName() const { return Name; }
4190b57cec5SDimitry Andric 
4200b57cec5SDimitry Andric   /// Return true if the intrinsic takes an immediate operand.
4210b57cec5SDimitry Andric   bool hasImmediate() const {
422349cc55cSDimitry Andric     return llvm::any_of(Types, [](const Type &T) { return T.isImmediate(); });
4230b57cec5SDimitry Andric   }
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric   /// Return the parameter index of the immediate operand.
4260b57cec5SDimitry Andric   unsigned getImmediateIdx() const {
427480093f4SDimitry Andric     for (unsigned Idx = 0; Idx < Types.size(); ++Idx)
428480093f4SDimitry Andric       if (Types[Idx].isImmediate())
4290b57cec5SDimitry Andric         return Idx - 1;
430480093f4SDimitry Andric     llvm_unreachable("Intrinsic has no immediate");
4310b57cec5SDimitry Andric   }
4320b57cec5SDimitry Andric 
4330b57cec5SDimitry Andric 
434480093f4SDimitry Andric   unsigned getNumParams() const { return Types.size() - 1; }
4350b57cec5SDimitry Andric   Type getReturnType() const { return Types[0]; }
4360b57cec5SDimitry Andric   Type getParamType(unsigned I) const { return Types[I + 1]; }
4370b57cec5SDimitry Andric   Type getBaseType() const { return BaseType; }
438480093f4SDimitry Andric   Type getPolymorphicKeyType() const { return Types[PolymorphicKeyType]; }
4390b57cec5SDimitry Andric 
4400b57cec5SDimitry Andric   /// Return true if the prototype has a scalar argument.
4410b57cec5SDimitry Andric   bool protoHasScalar() const;
4420b57cec5SDimitry Andric 
4430b57cec5SDimitry Andric   /// Return the index that parameter PIndex will sit at
4440b57cec5SDimitry Andric   /// in a generated function call. This is often just PIndex,
4450b57cec5SDimitry Andric   /// but may not be as things such as multiple-vector operands
446*bdd1243dSDimitry Andric   /// and sret parameters need to be taken into account.
4470b57cec5SDimitry Andric   unsigned getGeneratedParamIdx(unsigned PIndex) {
4480b57cec5SDimitry Andric     unsigned Idx = 0;
4490b57cec5SDimitry Andric     if (getReturnType().getNumVectors() > 1)
4500b57cec5SDimitry Andric       // Multiple vectors are passed as sret.
4510b57cec5SDimitry Andric       ++Idx;
4520b57cec5SDimitry Andric 
4530b57cec5SDimitry Andric     for (unsigned I = 0; I < PIndex; ++I)
4540b57cec5SDimitry Andric       Idx += std::max(1U, getParamType(I).getNumVectors());
4550b57cec5SDimitry Andric 
4560b57cec5SDimitry Andric     return Idx;
4570b57cec5SDimitry Andric   }
4580b57cec5SDimitry Andric 
4590b57cec5SDimitry Andric   bool hasBody() const { return Body && !Body->getValues().empty(); }
4600b57cec5SDimitry Andric 
4610b57cec5SDimitry Andric   void setNeededEarly() { NeededEarly = true; }
4620b57cec5SDimitry Andric 
4630b57cec5SDimitry Andric   bool operator<(const Intrinsic &Other) const {
464*bdd1243dSDimitry Andric     // Sort lexicographically on a three-tuple (ArchGuard, TargetGuard, Name)
465*bdd1243dSDimitry Andric     if (ArchGuard != Other.ArchGuard)
466*bdd1243dSDimitry Andric       return ArchGuard < Other.ArchGuard;
467*bdd1243dSDimitry Andric     if (TargetGuard != Other.TargetGuard)
468*bdd1243dSDimitry Andric       return TargetGuard < Other.TargetGuard;
4690b57cec5SDimitry Andric     return Name < Other.Name;
4700b57cec5SDimitry Andric   }
4710b57cec5SDimitry Andric 
4720b57cec5SDimitry Andric   ClassKind getClassKind(bool UseClassBIfScalar = false) {
4730b57cec5SDimitry Andric     if (UseClassBIfScalar && !protoHasScalar())
4740b57cec5SDimitry Andric       return ClassB;
4750b57cec5SDimitry Andric     return CK;
4760b57cec5SDimitry Andric   }
4770b57cec5SDimitry Andric 
4780b57cec5SDimitry Andric   /// Return the name, mangled with type information.
4790b57cec5SDimitry Andric   /// If ForceClassS is true, use ClassS (u32/s32) instead
4800b57cec5SDimitry Andric   /// of the intrinsic's own type class.
4810b57cec5SDimitry Andric   std::string getMangledName(bool ForceClassS = false) const;
4820b57cec5SDimitry Andric   /// Return the type code for a builtin function call.
4830b57cec5SDimitry Andric   std::string getInstTypeCode(Type T, ClassKind CK) const;
4840b57cec5SDimitry Andric   /// Return the type string for a BUILTIN() macro in Builtins.def.
4850b57cec5SDimitry Andric   std::string getBuiltinTypeStr();
4860b57cec5SDimitry Andric 
4870b57cec5SDimitry Andric   /// Generate the intrinsic, returning code.
4880b57cec5SDimitry Andric   std::string generate();
4890b57cec5SDimitry Andric   /// Perform type checking and populate the dependency graph, but
4900b57cec5SDimitry Andric   /// don't generate code yet.
4910b57cec5SDimitry Andric   void indexBody();
4920b57cec5SDimitry Andric 
4930b57cec5SDimitry Andric private:
494480093f4SDimitry Andric   StringRef getNextModifiers(StringRef Proto, unsigned &Pos) const;
495480093f4SDimitry Andric 
4960b57cec5SDimitry Andric   std::string mangleName(std::string Name, ClassKind CK) const;
4970b57cec5SDimitry Andric 
4980b57cec5SDimitry Andric   void initVariables();
4990b57cec5SDimitry Andric   std::string replaceParamsIn(std::string S);
5000b57cec5SDimitry Andric 
5010b57cec5SDimitry Andric   void emitBodyAsBuiltinCall();
5020b57cec5SDimitry Andric 
5030b57cec5SDimitry Andric   void generateImpl(bool ReverseArguments,
5040b57cec5SDimitry Andric                     StringRef NamePrefix, StringRef CallPrefix);
5050b57cec5SDimitry Andric   void emitReturn();
5060b57cec5SDimitry Andric   void emitBody(StringRef CallPrefix);
5070b57cec5SDimitry Andric   void emitShadowedArgs();
5080b57cec5SDimitry Andric   void emitArgumentReversal();
5093a9a9c0cSDimitry Andric   void emitReturnVarDecl();
5100b57cec5SDimitry Andric   void emitReturnReversal();
5110b57cec5SDimitry Andric   void emitReverseVariable(Variable &Dest, Variable &Src);
5120b57cec5SDimitry Andric   void emitNewLine();
5130b57cec5SDimitry Andric   void emitClosingBrace();
5140b57cec5SDimitry Andric   void emitOpeningBrace();
5150b57cec5SDimitry Andric   void emitPrototype(StringRef NamePrefix);
5160b57cec5SDimitry Andric 
5170b57cec5SDimitry Andric   class DagEmitter {
5180b57cec5SDimitry Andric     Intrinsic &Intr;
5190b57cec5SDimitry Andric     StringRef CallPrefix;
5200b57cec5SDimitry Andric 
5210b57cec5SDimitry Andric   public:
5220b57cec5SDimitry Andric     DagEmitter(Intrinsic &Intr, StringRef CallPrefix) :
5230b57cec5SDimitry Andric       Intr(Intr), CallPrefix(CallPrefix) {
5240b57cec5SDimitry Andric     }
5250b57cec5SDimitry Andric     std::pair<Type, std::string> emitDagArg(Init *Arg, std::string ArgName);
5260b57cec5SDimitry Andric     std::pair<Type, std::string> emitDagSaveTemp(DagInit *DI);
5270b57cec5SDimitry Andric     std::pair<Type, std::string> emitDagSplat(DagInit *DI);
5280b57cec5SDimitry Andric     std::pair<Type, std::string> emitDagDup(DagInit *DI);
5290b57cec5SDimitry Andric     std::pair<Type, std::string> emitDagDupTyped(DagInit *DI);
5300b57cec5SDimitry Andric     std::pair<Type, std::string> emitDagShuffle(DagInit *DI);
5310b57cec5SDimitry Andric     std::pair<Type, std::string> emitDagCast(DagInit *DI, bool IsBitCast);
5325ffd83dbSDimitry Andric     std::pair<Type, std::string> emitDagCall(DagInit *DI,
5335ffd83dbSDimitry Andric                                              bool MatchMangledName);
5340b57cec5SDimitry Andric     std::pair<Type, std::string> emitDagNameReplace(DagInit *DI);
5350b57cec5SDimitry Andric     std::pair<Type, std::string> emitDagLiteral(DagInit *DI);
5360b57cec5SDimitry Andric     std::pair<Type, std::string> emitDagOp(DagInit *DI);
5370b57cec5SDimitry Andric     std::pair<Type, std::string> emitDag(DagInit *DI);
5380b57cec5SDimitry Andric   };
5390b57cec5SDimitry Andric };
5400b57cec5SDimitry Andric 
5410b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
5420b57cec5SDimitry Andric // NeonEmitter
5430b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
5440b57cec5SDimitry Andric 
5450b57cec5SDimitry Andric class NeonEmitter {
5460b57cec5SDimitry Andric   RecordKeeper &Records;
5470b57cec5SDimitry Andric   DenseMap<Record *, ClassKind> ClassMap;
5480b57cec5SDimitry Andric   std::map<std::string, std::deque<Intrinsic>> IntrinsicMap;
5490b57cec5SDimitry Andric   unsigned UniqueNumber;
5500b57cec5SDimitry Andric 
5510b57cec5SDimitry Andric   void createIntrinsic(Record *R, SmallVectorImpl<Intrinsic *> &Out);
5520b57cec5SDimitry Andric   void genBuiltinsDef(raw_ostream &OS, SmallVectorImpl<Intrinsic *> &Defs);
5530b57cec5SDimitry Andric   void genOverloadTypeCheckCode(raw_ostream &OS,
5540b57cec5SDimitry Andric                                 SmallVectorImpl<Intrinsic *> &Defs);
5550b57cec5SDimitry Andric   void genIntrinsicRangeCheckCode(raw_ostream &OS,
5560b57cec5SDimitry Andric                                   SmallVectorImpl<Intrinsic *> &Defs);
5570b57cec5SDimitry Andric 
5580b57cec5SDimitry Andric public:
5590b57cec5SDimitry Andric   /// Called by Intrinsic - this attempts to get an intrinsic that takes
5600b57cec5SDimitry Andric   /// the given types as arguments.
5615ffd83dbSDimitry Andric   Intrinsic &getIntrinsic(StringRef Name, ArrayRef<Type> Types,
562*bdd1243dSDimitry Andric                           std::optional<std::string> MangledName);
5630b57cec5SDimitry Andric 
5640b57cec5SDimitry Andric   /// Called by Intrinsic - returns a globally-unique number.
5650b57cec5SDimitry Andric   unsigned getUniqueNumber() { return UniqueNumber++; }
5660b57cec5SDimitry Andric 
5670b57cec5SDimitry Andric   NeonEmitter(RecordKeeper &R) : Records(R), UniqueNumber(0) {
5680b57cec5SDimitry Andric     Record *SI = R.getClass("SInst");
5690b57cec5SDimitry Andric     Record *II = R.getClass("IInst");
5700b57cec5SDimitry Andric     Record *WI = R.getClass("WInst");
5710b57cec5SDimitry Andric     Record *SOpI = R.getClass("SOpInst");
5720b57cec5SDimitry Andric     Record *IOpI = R.getClass("IOpInst");
5730b57cec5SDimitry Andric     Record *WOpI = R.getClass("WOpInst");
5740b57cec5SDimitry Andric     Record *LOpI = R.getClass("LOpInst");
5750b57cec5SDimitry Andric     Record *NoTestOpI = R.getClass("NoTestOpInst");
5760b57cec5SDimitry Andric 
5770b57cec5SDimitry Andric     ClassMap[SI] = ClassS;
5780b57cec5SDimitry Andric     ClassMap[II] = ClassI;
5790b57cec5SDimitry Andric     ClassMap[WI] = ClassW;
5800b57cec5SDimitry Andric     ClassMap[SOpI] = ClassS;
5810b57cec5SDimitry Andric     ClassMap[IOpI] = ClassI;
5820b57cec5SDimitry Andric     ClassMap[WOpI] = ClassW;
5830b57cec5SDimitry Andric     ClassMap[LOpI] = ClassL;
5840b57cec5SDimitry Andric     ClassMap[NoTestOpI] = ClassNoTest;
5850b57cec5SDimitry Andric   }
5860b57cec5SDimitry Andric 
587e8d8bef9SDimitry Andric   // Emit arm_neon.h.inc
5880b57cec5SDimitry Andric   void run(raw_ostream &o);
5890b57cec5SDimitry Andric 
590e8d8bef9SDimitry Andric   // Emit arm_fp16.h.inc
5910b57cec5SDimitry Andric   void runFP16(raw_ostream &o);
5920b57cec5SDimitry Andric 
593e8d8bef9SDimitry Andric   // Emit arm_bf16.h.inc
5945ffd83dbSDimitry Andric   void runBF16(raw_ostream &o);
5955ffd83dbSDimitry Andric 
596e8d8bef9SDimitry Andric   // Emit all the __builtin prototypes used in arm_neon.h, arm_fp16.h and
597e8d8bef9SDimitry Andric   // arm_bf16.h
5980b57cec5SDimitry Andric   void runHeader(raw_ostream &o);
5990b57cec5SDimitry Andric };
6000b57cec5SDimitry Andric 
6010b57cec5SDimitry Andric } // end anonymous namespace
6020b57cec5SDimitry Andric 
6030b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
6040b57cec5SDimitry Andric // Type implementation
6050b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
6060b57cec5SDimitry Andric 
6070b57cec5SDimitry Andric std::string Type::str() const {
608480093f4SDimitry Andric   if (isVoid())
6090b57cec5SDimitry Andric     return "void";
6100b57cec5SDimitry Andric   std::string S;
6110b57cec5SDimitry Andric 
612480093f4SDimitry Andric   if (isInteger() && !isSigned())
6130b57cec5SDimitry Andric     S += "u";
6140b57cec5SDimitry Andric 
615480093f4SDimitry Andric   if (isPoly())
6160b57cec5SDimitry Andric     S += "poly";
617480093f4SDimitry Andric   else if (isFloating())
6180b57cec5SDimitry Andric     S += "float";
6195ffd83dbSDimitry Andric   else if (isBFloat16())
6205ffd83dbSDimitry Andric     S += "bfloat";
6210b57cec5SDimitry Andric   else
6220b57cec5SDimitry Andric     S += "int";
6230b57cec5SDimitry Andric 
6240b57cec5SDimitry Andric   S += utostr(ElementBitwidth);
6250b57cec5SDimitry Andric   if (isVector())
6260b57cec5SDimitry Andric     S += "x" + utostr(getNumElements());
6270b57cec5SDimitry Andric   if (NumVectors > 1)
6280b57cec5SDimitry Andric     S += "x" + utostr(NumVectors);
6290b57cec5SDimitry Andric   S += "_t";
6300b57cec5SDimitry Andric 
6310b57cec5SDimitry Andric   if (Constant)
6320b57cec5SDimitry Andric     S += " const";
6330b57cec5SDimitry Andric   if (Pointer)
6340b57cec5SDimitry Andric     S += " *";
6350b57cec5SDimitry Andric 
6360b57cec5SDimitry Andric   return S;
6370b57cec5SDimitry Andric }
6380b57cec5SDimitry Andric 
6390b57cec5SDimitry Andric std::string Type::builtin_str() const {
6400b57cec5SDimitry Andric   std::string S;
6410b57cec5SDimitry Andric   if (isVoid())
6420b57cec5SDimitry Andric     return "v";
6430b57cec5SDimitry Andric 
644480093f4SDimitry Andric   if (isPointer()) {
6450b57cec5SDimitry Andric     // All pointers are void pointers.
646480093f4SDimitry Andric     S = "v";
647480093f4SDimitry Andric     if (isConstPointer())
648480093f4SDimitry Andric       S += "C";
649480093f4SDimitry Andric     S += "*";
650480093f4SDimitry Andric     return S;
651480093f4SDimitry Andric   } else if (isInteger())
6520b57cec5SDimitry Andric     switch (ElementBitwidth) {
6530b57cec5SDimitry Andric     case 8: S += "c"; break;
6540b57cec5SDimitry Andric     case 16: S += "s"; break;
6550b57cec5SDimitry Andric     case 32: S += "i"; break;
6560b57cec5SDimitry Andric     case 64: S += "Wi"; break;
6570b57cec5SDimitry Andric     case 128: S += "LLLi"; break;
6580b57cec5SDimitry Andric     default: llvm_unreachable("Unhandled case!");
6590b57cec5SDimitry Andric     }
6605ffd83dbSDimitry Andric   else if (isBFloat16()) {
6615ffd83dbSDimitry Andric     assert(ElementBitwidth == 16 && "BFloat16 can only be 16 bits");
6625ffd83dbSDimitry Andric     S += "y";
6635ffd83dbSDimitry Andric   } else
6640b57cec5SDimitry Andric     switch (ElementBitwidth) {
6650b57cec5SDimitry Andric     case 16: S += "h"; break;
6660b57cec5SDimitry Andric     case 32: S += "f"; break;
6670b57cec5SDimitry Andric     case 64: S += "d"; break;
6680b57cec5SDimitry Andric     default: llvm_unreachable("Unhandled case!");
6690b57cec5SDimitry Andric     }
6700b57cec5SDimitry Andric 
671480093f4SDimitry Andric   // FIXME: NECESSARY???????????????????????????????????????????????????????????????????????
672480093f4SDimitry Andric   if (isChar() && !isPointer() && isSigned())
6730b57cec5SDimitry Andric     // Make chars explicitly signed.
6740b57cec5SDimitry Andric     S = "S" + S;
675480093f4SDimitry Andric   else if (isInteger() && !isSigned())
6760b57cec5SDimitry Andric     S = "U" + S;
6770b57cec5SDimitry Andric 
6780b57cec5SDimitry Andric   // Constant indices are "int", but have the "constant expression" modifier.
6790b57cec5SDimitry Andric   if (isImmediate()) {
6800b57cec5SDimitry Andric     assert(isInteger() && isSigned());
6810b57cec5SDimitry Andric     S = "I" + S;
6820b57cec5SDimitry Andric   }
6830b57cec5SDimitry Andric 
684480093f4SDimitry Andric   if (isScalar())
6850b57cec5SDimitry Andric     return S;
6860b57cec5SDimitry Andric 
6870b57cec5SDimitry Andric   std::string Ret;
6880b57cec5SDimitry Andric   for (unsigned I = 0; I < NumVectors; ++I)
6890b57cec5SDimitry Andric     Ret += "V" + utostr(getNumElements()) + S;
6900b57cec5SDimitry Andric 
6910b57cec5SDimitry Andric   return Ret;
6920b57cec5SDimitry Andric }
6930b57cec5SDimitry Andric 
6940b57cec5SDimitry Andric unsigned Type::getNeonEnum() const {
6950b57cec5SDimitry Andric   unsigned Addend;
6960b57cec5SDimitry Andric   switch (ElementBitwidth) {
6970b57cec5SDimitry Andric   case 8: Addend = 0; break;
6980b57cec5SDimitry Andric   case 16: Addend = 1; break;
6990b57cec5SDimitry Andric   case 32: Addend = 2; break;
7000b57cec5SDimitry Andric   case 64: Addend = 3; break;
7010b57cec5SDimitry Andric   case 128: Addend = 4; break;
7020b57cec5SDimitry Andric   default: llvm_unreachable("Unhandled element bitwidth!");
7030b57cec5SDimitry Andric   }
7040b57cec5SDimitry Andric 
7050b57cec5SDimitry Andric   unsigned Base = (unsigned)NeonTypeFlags::Int8 + Addend;
706480093f4SDimitry Andric   if (isPoly()) {
7070b57cec5SDimitry Andric     // Adjustment needed because Poly32 doesn't exist.
7080b57cec5SDimitry Andric     if (Addend >= 2)
7090b57cec5SDimitry Andric       --Addend;
7100b57cec5SDimitry Andric     Base = (unsigned)NeonTypeFlags::Poly8 + Addend;
7110b57cec5SDimitry Andric   }
712480093f4SDimitry Andric   if (isFloating()) {
7130b57cec5SDimitry Andric     assert(Addend != 0 && "Float8 doesn't exist!");
7140b57cec5SDimitry Andric     Base = (unsigned)NeonTypeFlags::Float16 + (Addend - 1);
7150b57cec5SDimitry Andric   }
7160b57cec5SDimitry Andric 
7175ffd83dbSDimitry Andric   if (isBFloat16()) {
7185ffd83dbSDimitry Andric     assert(Addend == 1 && "BFloat16 is only 16 bit");
7195ffd83dbSDimitry Andric     Base = (unsigned)NeonTypeFlags::BFloat16;
7205ffd83dbSDimitry Andric   }
7215ffd83dbSDimitry Andric 
7220b57cec5SDimitry Andric   if (Bitwidth == 128)
7230b57cec5SDimitry Andric     Base |= (unsigned)NeonTypeFlags::QuadFlag;
724480093f4SDimitry Andric   if (isInteger() && !isSigned())
7250b57cec5SDimitry Andric     Base |= (unsigned)NeonTypeFlags::UnsignedFlag;
7260b57cec5SDimitry Andric 
7270b57cec5SDimitry Andric   return Base;
7280b57cec5SDimitry Andric }
7290b57cec5SDimitry Andric 
7300b57cec5SDimitry Andric Type Type::fromTypedefName(StringRef Name) {
7310b57cec5SDimitry Andric   Type T;
732480093f4SDimitry Andric   T.Kind = SInt;
7330b57cec5SDimitry Andric 
7340b57cec5SDimitry Andric   if (Name.front() == 'u') {
735480093f4SDimitry Andric     T.Kind = UInt;
7360b57cec5SDimitry Andric     Name = Name.drop_front();
7370b57cec5SDimitry Andric   }
7380b57cec5SDimitry Andric 
7390b57cec5SDimitry Andric   if (Name.startswith("float")) {
740480093f4SDimitry Andric     T.Kind = Float;
7410b57cec5SDimitry Andric     Name = Name.drop_front(5);
7420b57cec5SDimitry Andric   } else if (Name.startswith("poly")) {
743480093f4SDimitry Andric     T.Kind = Poly;
7440b57cec5SDimitry Andric     Name = Name.drop_front(4);
7455ffd83dbSDimitry Andric   } else if (Name.startswith("bfloat")) {
7465ffd83dbSDimitry Andric     T.Kind = BFloat16;
7475ffd83dbSDimitry Andric     Name = Name.drop_front(6);
7480b57cec5SDimitry Andric   } else {
7490b57cec5SDimitry Andric     assert(Name.startswith("int"));
7500b57cec5SDimitry Andric     Name = Name.drop_front(3);
7510b57cec5SDimitry Andric   }
7520b57cec5SDimitry Andric 
7530b57cec5SDimitry Andric   unsigned I = 0;
7540b57cec5SDimitry Andric   for (I = 0; I < Name.size(); ++I) {
7550b57cec5SDimitry Andric     if (!isdigit(Name[I]))
7560b57cec5SDimitry Andric       break;
7570b57cec5SDimitry Andric   }
7580b57cec5SDimitry Andric   Name.substr(0, I).getAsInteger(10, T.ElementBitwidth);
7590b57cec5SDimitry Andric   Name = Name.drop_front(I);
7600b57cec5SDimitry Andric 
7610b57cec5SDimitry Andric   T.Bitwidth = T.ElementBitwidth;
7620b57cec5SDimitry Andric   T.NumVectors = 1;
7630b57cec5SDimitry Andric 
7640b57cec5SDimitry Andric   if (Name.front() == 'x') {
7650b57cec5SDimitry Andric     Name = Name.drop_front();
7660b57cec5SDimitry Andric     unsigned I = 0;
7670b57cec5SDimitry Andric     for (I = 0; I < Name.size(); ++I) {
7680b57cec5SDimitry Andric       if (!isdigit(Name[I]))
7690b57cec5SDimitry Andric         break;
7700b57cec5SDimitry Andric     }
7710b57cec5SDimitry Andric     unsigned NumLanes;
7720b57cec5SDimitry Andric     Name.substr(0, I).getAsInteger(10, NumLanes);
7730b57cec5SDimitry Andric     Name = Name.drop_front(I);
7740b57cec5SDimitry Andric     T.Bitwidth = T.ElementBitwidth * NumLanes;
7750b57cec5SDimitry Andric   } else {
7760b57cec5SDimitry Andric     // Was scalar.
7770b57cec5SDimitry Andric     T.NumVectors = 0;
7780b57cec5SDimitry Andric   }
7790b57cec5SDimitry Andric   if (Name.front() == 'x') {
7800b57cec5SDimitry Andric     Name = Name.drop_front();
7810b57cec5SDimitry Andric     unsigned I = 0;
7820b57cec5SDimitry Andric     for (I = 0; I < Name.size(); ++I) {
7830b57cec5SDimitry Andric       if (!isdigit(Name[I]))
7840b57cec5SDimitry Andric         break;
7850b57cec5SDimitry Andric     }
7860b57cec5SDimitry Andric     Name.substr(0, I).getAsInteger(10, T.NumVectors);
7870b57cec5SDimitry Andric     Name = Name.drop_front(I);
7880b57cec5SDimitry Andric   }
7890b57cec5SDimitry Andric 
7900b57cec5SDimitry Andric   assert(Name.startswith("_t") && "Malformed typedef!");
7910b57cec5SDimitry Andric   return T;
7920b57cec5SDimitry Andric }
7930b57cec5SDimitry Andric 
7940b57cec5SDimitry Andric void Type::applyTypespec(bool &Quad) {
7950b57cec5SDimitry Andric   std::string S = TS;
7960b57cec5SDimitry Andric   ScalarForMangling = false;
797480093f4SDimitry Andric   Kind = SInt;
7980b57cec5SDimitry Andric   ElementBitwidth = ~0U;
7990b57cec5SDimitry Andric   NumVectors = 1;
8000b57cec5SDimitry Andric 
8010b57cec5SDimitry Andric   for (char I : S) {
8020b57cec5SDimitry Andric     switch (I) {
8030b57cec5SDimitry Andric     case 'S':
8040b57cec5SDimitry Andric       ScalarForMangling = true;
8050b57cec5SDimitry Andric       break;
8060b57cec5SDimitry Andric     case 'H':
8070b57cec5SDimitry Andric       NoManglingQ = true;
8080b57cec5SDimitry Andric       Quad = true;
8090b57cec5SDimitry Andric       break;
8100b57cec5SDimitry Andric     case 'Q':
8110b57cec5SDimitry Andric       Quad = true;
8120b57cec5SDimitry Andric       break;
8130b57cec5SDimitry Andric     case 'P':
814480093f4SDimitry Andric       Kind = Poly;
8150b57cec5SDimitry Andric       break;
8160b57cec5SDimitry Andric     case 'U':
817480093f4SDimitry Andric       Kind = UInt;
8180b57cec5SDimitry Andric       break;
8190b57cec5SDimitry Andric     case 'c':
8200b57cec5SDimitry Andric       ElementBitwidth = 8;
8210b57cec5SDimitry Andric       break;
8220b57cec5SDimitry Andric     case 'h':
823480093f4SDimitry Andric       Kind = Float;
824*bdd1243dSDimitry Andric       [[fallthrough]];
8250b57cec5SDimitry Andric     case 's':
8260b57cec5SDimitry Andric       ElementBitwidth = 16;
8270b57cec5SDimitry Andric       break;
8280b57cec5SDimitry Andric     case 'f':
829480093f4SDimitry Andric       Kind = Float;
830*bdd1243dSDimitry Andric       [[fallthrough]];
8310b57cec5SDimitry Andric     case 'i':
8320b57cec5SDimitry Andric       ElementBitwidth = 32;
8330b57cec5SDimitry Andric       break;
8340b57cec5SDimitry Andric     case 'd':
835480093f4SDimitry Andric       Kind = Float;
836*bdd1243dSDimitry Andric       [[fallthrough]];
8370b57cec5SDimitry Andric     case 'l':
8380b57cec5SDimitry Andric       ElementBitwidth = 64;
8390b57cec5SDimitry Andric       break;
8400b57cec5SDimitry Andric     case 'k':
8410b57cec5SDimitry Andric       ElementBitwidth = 128;
8420b57cec5SDimitry Andric       // Poly doesn't have a 128x1 type.
843480093f4SDimitry Andric       if (isPoly())
8440b57cec5SDimitry Andric         NumVectors = 0;
8450b57cec5SDimitry Andric       break;
8465ffd83dbSDimitry Andric     case 'b':
8475ffd83dbSDimitry Andric       Kind = BFloat16;
8485ffd83dbSDimitry Andric       ElementBitwidth = 16;
8495ffd83dbSDimitry Andric       break;
8500b57cec5SDimitry Andric     default:
8510b57cec5SDimitry Andric       llvm_unreachable("Unhandled type code!");
8520b57cec5SDimitry Andric     }
8530b57cec5SDimitry Andric   }
8540b57cec5SDimitry Andric   assert(ElementBitwidth != ~0U && "Bad element bitwidth!");
8550b57cec5SDimitry Andric 
8560b57cec5SDimitry Andric   Bitwidth = Quad ? 128 : 64;
8570b57cec5SDimitry Andric }
8580b57cec5SDimitry Andric 
859480093f4SDimitry Andric void Type::applyModifiers(StringRef Mods) {
8600b57cec5SDimitry Andric   bool AppliedQuad = false;
8610b57cec5SDimitry Andric   applyTypespec(AppliedQuad);
8620b57cec5SDimitry Andric 
863480093f4SDimitry Andric   for (char Mod : Mods) {
8640b57cec5SDimitry Andric     switch (Mod) {
865480093f4SDimitry Andric     case '.':
866480093f4SDimitry Andric       break;
8670b57cec5SDimitry Andric     case 'v':
868480093f4SDimitry Andric       Kind = Void;
8690b57cec5SDimitry Andric       break;
870480093f4SDimitry Andric     case 'S':
871480093f4SDimitry Andric       Kind = SInt;
8720b57cec5SDimitry Andric       break;
8730b57cec5SDimitry Andric     case 'U':
874480093f4SDimitry Andric       Kind = UInt;
8750b57cec5SDimitry Andric       break;
8765ffd83dbSDimitry Andric     case 'B':
8775ffd83dbSDimitry Andric       Kind = BFloat16;
8785ffd83dbSDimitry Andric       ElementBitwidth = 16;
8795ffd83dbSDimitry Andric       break;
8800b57cec5SDimitry Andric     case 'F':
881480093f4SDimitry Andric       Kind = Float;
8820b57cec5SDimitry Andric       break;
883480093f4SDimitry Andric     case 'P':
884480093f4SDimitry Andric       Kind = Poly;
8850b57cec5SDimitry Andric       break;
886480093f4SDimitry Andric     case '>':
887480093f4SDimitry Andric       assert(ElementBitwidth < 128);
888480093f4SDimitry Andric       ElementBitwidth *= 2;
889480093f4SDimitry Andric       break;
890480093f4SDimitry Andric     case '<':
891480093f4SDimitry Andric       assert(ElementBitwidth > 8);
892480093f4SDimitry Andric       ElementBitwidth /= 2;
8930b57cec5SDimitry Andric       break;
8940b57cec5SDimitry Andric     case '1':
8950b57cec5SDimitry Andric       NumVectors = 0;
8960b57cec5SDimitry Andric       break;
8970b57cec5SDimitry Andric     case '2':
8980b57cec5SDimitry Andric       NumVectors = 2;
8990b57cec5SDimitry Andric       break;
9000b57cec5SDimitry Andric     case '3':
9010b57cec5SDimitry Andric       NumVectors = 3;
9020b57cec5SDimitry Andric       break;
9030b57cec5SDimitry Andric     case '4':
9040b57cec5SDimitry Andric       NumVectors = 4;
9050b57cec5SDimitry Andric       break;
906480093f4SDimitry Andric     case '*':
907480093f4SDimitry Andric       Pointer = true;
9080b57cec5SDimitry Andric       break;
909480093f4SDimitry Andric     case 'c':
910480093f4SDimitry Andric       Constant = true;
9110b57cec5SDimitry Andric       break;
912480093f4SDimitry Andric     case 'Q':
913480093f4SDimitry Andric       Bitwidth = 128;
9140b57cec5SDimitry Andric       break;
915480093f4SDimitry Andric     case 'q':
916480093f4SDimitry Andric       Bitwidth = 64;
9170b57cec5SDimitry Andric       break;
918480093f4SDimitry Andric     case 'I':
919480093f4SDimitry Andric       Kind = SInt;
920480093f4SDimitry Andric       ElementBitwidth = Bitwidth = 32;
921480093f4SDimitry Andric       NumVectors = 0;
922480093f4SDimitry Andric       Immediate = true;
9230b57cec5SDimitry Andric       break;
924480093f4SDimitry Andric     case 'p':
925480093f4SDimitry Andric       if (isPoly())
926480093f4SDimitry Andric         Kind = UInt;
927480093f4SDimitry Andric       break;
928480093f4SDimitry Andric     case '!':
929480093f4SDimitry Andric       // Key type, handled elsewhere.
9300b57cec5SDimitry Andric       break;
9310b57cec5SDimitry Andric     default:
9320b57cec5SDimitry Andric       llvm_unreachable("Unhandled character!");
9330b57cec5SDimitry Andric     }
9340b57cec5SDimitry Andric   }
935480093f4SDimitry Andric }
9360b57cec5SDimitry Andric 
9370b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
9380b57cec5SDimitry Andric // Intrinsic implementation
9390b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
9400b57cec5SDimitry Andric 
941480093f4SDimitry Andric StringRef Intrinsic::getNextModifiers(StringRef Proto, unsigned &Pos) const {
942480093f4SDimitry Andric   if (Proto.size() == Pos)
943480093f4SDimitry Andric     return StringRef();
944480093f4SDimitry Andric   else if (Proto[Pos] != '(')
945480093f4SDimitry Andric     return Proto.substr(Pos++, 1);
946480093f4SDimitry Andric 
947480093f4SDimitry Andric   size_t Start = Pos + 1;
948480093f4SDimitry Andric   size_t End = Proto.find(')', Start);
949480093f4SDimitry Andric   assert_with_loc(End != StringRef::npos, "unmatched modifier group paren");
950480093f4SDimitry Andric   Pos = End + 1;
951480093f4SDimitry Andric   return Proto.slice(Start, End);
952480093f4SDimitry Andric }
953480093f4SDimitry Andric 
9540b57cec5SDimitry Andric std::string Intrinsic::getInstTypeCode(Type T, ClassKind CK) const {
9550b57cec5SDimitry Andric   char typeCode = '\0';
9560b57cec5SDimitry Andric   bool printNumber = true;
9570b57cec5SDimitry Andric 
958*bdd1243dSDimitry Andric   if (CK == ClassB && TargetGuard == "")
9590b57cec5SDimitry Andric     return "";
9600b57cec5SDimitry Andric 
9615ffd83dbSDimitry Andric   if (T.isBFloat16())
9625ffd83dbSDimitry Andric     return "bf16";
9635ffd83dbSDimitry Andric 
9640b57cec5SDimitry Andric   if (T.isPoly())
9650b57cec5SDimitry Andric     typeCode = 'p';
9660b57cec5SDimitry Andric   else if (T.isInteger())
9670b57cec5SDimitry Andric     typeCode = T.isSigned() ? 's' : 'u';
9680b57cec5SDimitry Andric   else
9690b57cec5SDimitry Andric     typeCode = 'f';
9700b57cec5SDimitry Andric 
9710b57cec5SDimitry Andric   if (CK == ClassI) {
9720b57cec5SDimitry Andric     switch (typeCode) {
9730b57cec5SDimitry Andric     default:
9740b57cec5SDimitry Andric       break;
9750b57cec5SDimitry Andric     case 's':
9760b57cec5SDimitry Andric     case 'u':
9770b57cec5SDimitry Andric     case 'p':
9780b57cec5SDimitry Andric       typeCode = 'i';
9790b57cec5SDimitry Andric       break;
9800b57cec5SDimitry Andric     }
9810b57cec5SDimitry Andric   }
982*bdd1243dSDimitry Andric   if (CK == ClassB && TargetGuard == "") {
9830b57cec5SDimitry Andric     typeCode = '\0';
9840b57cec5SDimitry Andric   }
9850b57cec5SDimitry Andric 
9860b57cec5SDimitry Andric   std::string S;
9870b57cec5SDimitry Andric   if (typeCode != '\0')
9880b57cec5SDimitry Andric     S.push_back(typeCode);
9890b57cec5SDimitry Andric   if (printNumber)
9900b57cec5SDimitry Andric     S += utostr(T.getElementSizeInBits());
9910b57cec5SDimitry Andric 
9920b57cec5SDimitry Andric   return S;
9930b57cec5SDimitry Andric }
9940b57cec5SDimitry Andric 
9950b57cec5SDimitry Andric std::string Intrinsic::getBuiltinTypeStr() {
9960b57cec5SDimitry Andric   ClassKind LocalCK = getClassKind(true);
9970b57cec5SDimitry Andric   std::string S;
9980b57cec5SDimitry Andric 
9990b57cec5SDimitry Andric   Type RetT = getReturnType();
10000b57cec5SDimitry Andric   if ((LocalCK == ClassI || LocalCK == ClassW) && RetT.isScalar() &&
10015ffd83dbSDimitry Andric       !RetT.isFloating() && !RetT.isBFloat16())
10020b57cec5SDimitry Andric     RetT.makeInteger(RetT.getElementSizeInBits(), false);
10030b57cec5SDimitry Andric 
10040b57cec5SDimitry Andric   // Since the return value must be one type, return a vector type of the
10050b57cec5SDimitry Andric   // appropriate width which we will bitcast.  An exception is made for
10060b57cec5SDimitry Andric   // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
10070b57cec5SDimitry Andric   // fashion, storing them to a pointer arg.
10080b57cec5SDimitry Andric   if (RetT.getNumVectors() > 1) {
10090b57cec5SDimitry Andric     S += "vv*"; // void result with void* first argument
10100b57cec5SDimitry Andric   } else {
10110b57cec5SDimitry Andric     if (RetT.isPoly())
10120b57cec5SDimitry Andric       RetT.makeInteger(RetT.getElementSizeInBits(), false);
1013480093f4SDimitry Andric     if (!RetT.isScalar() && RetT.isInteger() && !RetT.isSigned())
10140b57cec5SDimitry Andric       RetT.makeSigned();
10150b57cec5SDimitry Andric 
1016480093f4SDimitry Andric     if (LocalCK == ClassB && RetT.isValue() && !RetT.isScalar())
10170b57cec5SDimitry Andric       // Cast to vector of 8-bit elements.
10180b57cec5SDimitry Andric       RetT.makeInteger(8, true);
10190b57cec5SDimitry Andric 
10200b57cec5SDimitry Andric     S += RetT.builtin_str();
10210b57cec5SDimitry Andric   }
10220b57cec5SDimitry Andric 
10230b57cec5SDimitry Andric   for (unsigned I = 0; I < getNumParams(); ++I) {
10240b57cec5SDimitry Andric     Type T = getParamType(I);
10250b57cec5SDimitry Andric     if (T.isPoly())
10260b57cec5SDimitry Andric       T.makeInteger(T.getElementSizeInBits(), false);
10270b57cec5SDimitry Andric 
1028480093f4SDimitry Andric     if (LocalCK == ClassB && !T.isScalar())
10290b57cec5SDimitry Andric       T.makeInteger(8, true);
10300b57cec5SDimitry Andric     // Halves always get converted to 8-bit elements.
10310b57cec5SDimitry Andric     if (T.isHalf() && T.isVector() && !T.isScalarForMangling())
10320b57cec5SDimitry Andric       T.makeInteger(8, true);
10330b57cec5SDimitry Andric 
1034480093f4SDimitry Andric     if (LocalCK == ClassI && T.isInteger())
10350b57cec5SDimitry Andric       T.makeSigned();
10360b57cec5SDimitry Andric 
10370b57cec5SDimitry Andric     if (hasImmediate() && getImmediateIdx() == I)
10380b57cec5SDimitry Andric       T.makeImmediate(32);
10390b57cec5SDimitry Andric 
10400b57cec5SDimitry Andric     S += T.builtin_str();
10410b57cec5SDimitry Andric   }
10420b57cec5SDimitry Andric 
10430b57cec5SDimitry Andric   // Extra constant integer to hold type class enum for this function, e.g. s8
10440b57cec5SDimitry Andric   if (LocalCK == ClassB)
10450b57cec5SDimitry Andric     S += "i";
10460b57cec5SDimitry Andric 
10470b57cec5SDimitry Andric   return S;
10480b57cec5SDimitry Andric }
10490b57cec5SDimitry Andric 
10500b57cec5SDimitry Andric std::string Intrinsic::getMangledName(bool ForceClassS) const {
10510b57cec5SDimitry Andric   // Check if the prototype has a scalar operand with the type of the vector
10520b57cec5SDimitry Andric   // elements.  If not, bitcasting the args will take care of arg checking.
10530b57cec5SDimitry Andric   // The actual signedness etc. will be taken care of with special enums.
10540b57cec5SDimitry Andric   ClassKind LocalCK = CK;
10550b57cec5SDimitry Andric   if (!protoHasScalar())
10560b57cec5SDimitry Andric     LocalCK = ClassB;
10570b57cec5SDimitry Andric 
10580b57cec5SDimitry Andric   return mangleName(Name, ForceClassS ? ClassS : LocalCK);
10590b57cec5SDimitry Andric }
10600b57cec5SDimitry Andric 
10610b57cec5SDimitry Andric std::string Intrinsic::mangleName(std::string Name, ClassKind LocalCK) const {
10620b57cec5SDimitry Andric   std::string typeCode = getInstTypeCode(BaseType, LocalCK);
10630b57cec5SDimitry Andric   std::string S = Name;
10640b57cec5SDimitry Andric 
10650b57cec5SDimitry Andric   if (Name == "vcvt_f16_f32" || Name == "vcvt_f32_f16" ||
10665ffd83dbSDimitry Andric       Name == "vcvt_f32_f64" || Name == "vcvt_f64_f32" ||
10675ffd83dbSDimitry Andric       Name == "vcvt_f32_bf16")
10680b57cec5SDimitry Andric     return Name;
10690b57cec5SDimitry Andric 
10700b57cec5SDimitry Andric   if (!typeCode.empty()) {
10710b57cec5SDimitry Andric     // If the name ends with _xN (N = 2,3,4), insert the typeCode before _xN.
10720b57cec5SDimitry Andric     if (Name.size() >= 3 && isdigit(Name.back()) &&
10730b57cec5SDimitry Andric         Name[Name.length() - 2] == 'x' && Name[Name.length() - 3] == '_')
10740b57cec5SDimitry Andric       S.insert(S.length() - 3, "_" + typeCode);
10750b57cec5SDimitry Andric     else
10760b57cec5SDimitry Andric       S += "_" + typeCode;
10770b57cec5SDimitry Andric   }
10780b57cec5SDimitry Andric 
10790b57cec5SDimitry Andric   if (BaseType != InBaseType) {
10800b57cec5SDimitry Andric     // A reinterpret - out the input base type at the end.
10810b57cec5SDimitry Andric     S += "_" + getInstTypeCode(InBaseType, LocalCK);
10820b57cec5SDimitry Andric   }
10830b57cec5SDimitry Andric 
1084*bdd1243dSDimitry Andric   if (LocalCK == ClassB && TargetGuard == "")
10850b57cec5SDimitry Andric     S += "_v";
10860b57cec5SDimitry Andric 
10870b57cec5SDimitry Andric   // Insert a 'q' before the first '_' character so that it ends up before
10880b57cec5SDimitry Andric   // _lane or _n on vector-scalar operations.
10890b57cec5SDimitry Andric   if (BaseType.getSizeInBits() == 128 && !BaseType.noManglingQ()) {
10900b57cec5SDimitry Andric     size_t Pos = S.find('_');
10910b57cec5SDimitry Andric     S.insert(Pos, "q");
10920b57cec5SDimitry Andric   }
10930b57cec5SDimitry Andric 
10940b57cec5SDimitry Andric   char Suffix = '\0';
10950b57cec5SDimitry Andric   if (BaseType.isScalarForMangling()) {
10960b57cec5SDimitry Andric     switch (BaseType.getElementSizeInBits()) {
10970b57cec5SDimitry Andric     case 8: Suffix = 'b'; break;
10980b57cec5SDimitry Andric     case 16: Suffix = 'h'; break;
10990b57cec5SDimitry Andric     case 32: Suffix = 's'; break;
11000b57cec5SDimitry Andric     case 64: Suffix = 'd'; break;
11010b57cec5SDimitry Andric     default: llvm_unreachable("Bad suffix!");
11020b57cec5SDimitry Andric     }
11030b57cec5SDimitry Andric   }
11040b57cec5SDimitry Andric   if (Suffix != '\0') {
11050b57cec5SDimitry Andric     size_t Pos = S.find('_');
11060b57cec5SDimitry Andric     S.insert(Pos, &Suffix, 1);
11070b57cec5SDimitry Andric   }
11080b57cec5SDimitry Andric 
11090b57cec5SDimitry Andric   return S;
11100b57cec5SDimitry Andric }
11110b57cec5SDimitry Andric 
11120b57cec5SDimitry Andric std::string Intrinsic::replaceParamsIn(std::string S) {
11130b57cec5SDimitry Andric   while (S.find('$') != std::string::npos) {
11140b57cec5SDimitry Andric     size_t Pos = S.find('$');
11150b57cec5SDimitry Andric     size_t End = Pos + 1;
11160b57cec5SDimitry Andric     while (isalpha(S[End]))
11170b57cec5SDimitry Andric       ++End;
11180b57cec5SDimitry Andric 
11190b57cec5SDimitry Andric     std::string VarName = S.substr(Pos + 1, End - Pos - 1);
11200b57cec5SDimitry Andric     assert_with_loc(Variables.find(VarName) != Variables.end(),
11210b57cec5SDimitry Andric                     "Variable not defined!");
11220b57cec5SDimitry Andric     S.replace(Pos, End - Pos, Variables.find(VarName)->second.getName());
11230b57cec5SDimitry Andric   }
11240b57cec5SDimitry Andric 
11250b57cec5SDimitry Andric   return S;
11260b57cec5SDimitry Andric }
11270b57cec5SDimitry Andric 
11280b57cec5SDimitry Andric void Intrinsic::initVariables() {
11290b57cec5SDimitry Andric   Variables.clear();
11300b57cec5SDimitry Andric 
11310b57cec5SDimitry Andric   // Modify the TypeSpec per-argument to get a concrete Type, and create
11320b57cec5SDimitry Andric   // known variables for each.
1133480093f4SDimitry Andric   for (unsigned I = 1; I < Types.size(); ++I) {
11340b57cec5SDimitry Andric     char NameC = '0' + (I - 1);
11350b57cec5SDimitry Andric     std::string Name = "p";
11360b57cec5SDimitry Andric     Name.push_back(NameC);
11370b57cec5SDimitry Andric 
11380b57cec5SDimitry Andric     Variables[Name] = Variable(Types[I], Name + VariablePostfix);
11390b57cec5SDimitry Andric   }
11400b57cec5SDimitry Andric   RetVar = Variable(Types[0], "ret" + VariablePostfix);
11410b57cec5SDimitry Andric }
11420b57cec5SDimitry Andric 
11430b57cec5SDimitry Andric void Intrinsic::emitPrototype(StringRef NamePrefix) {
1144*bdd1243dSDimitry Andric   if (UseMacro) {
11450b57cec5SDimitry Andric     OS << "#define ";
1146*bdd1243dSDimitry Andric   } else {
1147*bdd1243dSDimitry Andric     OS << "__ai ";
1148*bdd1243dSDimitry Andric     if (TargetGuard != "")
1149*bdd1243dSDimitry Andric       OS << "__attribute__((target(\"" << TargetGuard << "\"))) ";
1150*bdd1243dSDimitry Andric     OS << Types[0].str() << " ";
1151*bdd1243dSDimitry Andric   }
11520b57cec5SDimitry Andric 
11530b57cec5SDimitry Andric   OS << NamePrefix.str() << mangleName(Name, ClassS) << "(";
11540b57cec5SDimitry Andric 
11550b57cec5SDimitry Andric   for (unsigned I = 0; I < getNumParams(); ++I) {
11560b57cec5SDimitry Andric     if (I != 0)
11570b57cec5SDimitry Andric       OS << ", ";
11580b57cec5SDimitry Andric 
11590b57cec5SDimitry Andric     char NameC = '0' + I;
11600b57cec5SDimitry Andric     std::string Name = "p";
11610b57cec5SDimitry Andric     Name.push_back(NameC);
11620b57cec5SDimitry Andric     assert(Variables.find(Name) != Variables.end());
11630b57cec5SDimitry Andric     Variable &V = Variables[Name];
11640b57cec5SDimitry Andric 
11650b57cec5SDimitry Andric     if (!UseMacro)
11660b57cec5SDimitry Andric       OS << V.getType().str() << " ";
11670b57cec5SDimitry Andric     OS << V.getName();
11680b57cec5SDimitry Andric   }
11690b57cec5SDimitry Andric 
11700b57cec5SDimitry Andric   OS << ")";
11710b57cec5SDimitry Andric }
11720b57cec5SDimitry Andric 
11730b57cec5SDimitry Andric void Intrinsic::emitOpeningBrace() {
11740b57cec5SDimitry Andric   if (UseMacro)
11750b57cec5SDimitry Andric     OS << " __extension__ ({";
11760b57cec5SDimitry Andric   else
11770b57cec5SDimitry Andric     OS << " {";
11780b57cec5SDimitry Andric   emitNewLine();
11790b57cec5SDimitry Andric }
11800b57cec5SDimitry Andric 
11810b57cec5SDimitry Andric void Intrinsic::emitClosingBrace() {
11820b57cec5SDimitry Andric   if (UseMacro)
11830b57cec5SDimitry Andric     OS << "})";
11840b57cec5SDimitry Andric   else
11850b57cec5SDimitry Andric     OS << "}";
11860b57cec5SDimitry Andric }
11870b57cec5SDimitry Andric 
11880b57cec5SDimitry Andric void Intrinsic::emitNewLine() {
11890b57cec5SDimitry Andric   if (UseMacro)
11900b57cec5SDimitry Andric     OS << " \\\n";
11910b57cec5SDimitry Andric   else
11920b57cec5SDimitry Andric     OS << "\n";
11930b57cec5SDimitry Andric }
11940b57cec5SDimitry Andric 
11950b57cec5SDimitry Andric void Intrinsic::emitReverseVariable(Variable &Dest, Variable &Src) {
11960b57cec5SDimitry Andric   if (Dest.getType().getNumVectors() > 1) {
11970b57cec5SDimitry Andric     emitNewLine();
11980b57cec5SDimitry Andric 
11990b57cec5SDimitry Andric     for (unsigned K = 0; K < Dest.getType().getNumVectors(); ++K) {
12000b57cec5SDimitry Andric       OS << "  " << Dest.getName() << ".val[" << K << "] = "
12010b57cec5SDimitry Andric          << "__builtin_shufflevector("
12020b57cec5SDimitry Andric          << Src.getName() << ".val[" << K << "], "
12030b57cec5SDimitry Andric          << Src.getName() << ".val[" << K << "]";
12040b57cec5SDimitry Andric       for (int J = Dest.getType().getNumElements() - 1; J >= 0; --J)
12050b57cec5SDimitry Andric         OS << ", " << J;
12060b57cec5SDimitry Andric       OS << ");";
12070b57cec5SDimitry Andric       emitNewLine();
12080b57cec5SDimitry Andric     }
12090b57cec5SDimitry Andric   } else {
12100b57cec5SDimitry Andric     OS << "  " << Dest.getName()
12110b57cec5SDimitry Andric        << " = __builtin_shufflevector(" << Src.getName() << ", " << Src.getName();
12120b57cec5SDimitry Andric     for (int J = Dest.getType().getNumElements() - 1; J >= 0; --J)
12130b57cec5SDimitry Andric       OS << ", " << J;
12140b57cec5SDimitry Andric     OS << ");";
12150b57cec5SDimitry Andric     emitNewLine();
12160b57cec5SDimitry Andric   }
12170b57cec5SDimitry Andric }
12180b57cec5SDimitry Andric 
12190b57cec5SDimitry Andric void Intrinsic::emitArgumentReversal() {
1220a7dea167SDimitry Andric   if (isBigEndianSafe())
12210b57cec5SDimitry Andric     return;
12220b57cec5SDimitry Andric 
12230b57cec5SDimitry Andric   // Reverse all vector arguments.
12240b57cec5SDimitry Andric   for (unsigned I = 0; I < getNumParams(); ++I) {
12250b57cec5SDimitry Andric     std::string Name = "p" + utostr(I);
12260b57cec5SDimitry Andric     std::string NewName = "rev" + utostr(I);
12270b57cec5SDimitry Andric 
12280b57cec5SDimitry Andric     Variable &V = Variables[Name];
12290b57cec5SDimitry Andric     Variable NewV(V.getType(), NewName + VariablePostfix);
12300b57cec5SDimitry Andric 
12310b57cec5SDimitry Andric     if (!NewV.getType().isVector() || NewV.getType().getNumElements() == 1)
12320b57cec5SDimitry Andric       continue;
12330b57cec5SDimitry Andric 
12340b57cec5SDimitry Andric     OS << "  " << NewV.getType().str() << " " << NewV.getName() << ";";
12350b57cec5SDimitry Andric     emitReverseVariable(NewV, V);
12360b57cec5SDimitry Andric     V = NewV;
12370b57cec5SDimitry Andric   }
12380b57cec5SDimitry Andric }
12390b57cec5SDimitry Andric 
12403a9a9c0cSDimitry Andric void Intrinsic::emitReturnVarDecl() {
12413a9a9c0cSDimitry Andric   assert(RetVar.getType() == Types[0]);
12423a9a9c0cSDimitry Andric   // Create a return variable, if we're not void.
12433a9a9c0cSDimitry Andric   if (!RetVar.getType().isVoid()) {
12443a9a9c0cSDimitry Andric     OS << "  " << RetVar.getType().str() << " " << RetVar.getName() << ";";
12453a9a9c0cSDimitry Andric     emitNewLine();
12463a9a9c0cSDimitry Andric   }
12473a9a9c0cSDimitry Andric }
12483a9a9c0cSDimitry Andric 
12490b57cec5SDimitry Andric void Intrinsic::emitReturnReversal() {
1250a7dea167SDimitry Andric   if (isBigEndianSafe())
12510b57cec5SDimitry Andric     return;
12520b57cec5SDimitry Andric   if (!getReturnType().isVector() || getReturnType().isVoid() ||
12530b57cec5SDimitry Andric       getReturnType().getNumElements() == 1)
12540b57cec5SDimitry Andric     return;
12550b57cec5SDimitry Andric   emitReverseVariable(RetVar, RetVar);
12560b57cec5SDimitry Andric }
12570b57cec5SDimitry Andric 
12580b57cec5SDimitry Andric void Intrinsic::emitShadowedArgs() {
12590b57cec5SDimitry Andric   // Macro arguments are not type-checked like inline function arguments,
12600b57cec5SDimitry Andric   // so assign them to local temporaries to get the right type checking.
12610b57cec5SDimitry Andric   if (!UseMacro)
12620b57cec5SDimitry Andric     return;
12630b57cec5SDimitry Andric 
12640b57cec5SDimitry Andric   for (unsigned I = 0; I < getNumParams(); ++I) {
12650b57cec5SDimitry Andric     // Do not create a temporary for an immediate argument.
12660b57cec5SDimitry Andric     // That would defeat the whole point of using a macro!
1267480093f4SDimitry Andric     if (getParamType(I).isImmediate())
12680b57cec5SDimitry Andric       continue;
12690b57cec5SDimitry Andric     // Do not create a temporary for pointer arguments. The input
12700b57cec5SDimitry Andric     // pointer may have an alignment hint.
12710b57cec5SDimitry Andric     if (getParamType(I).isPointer())
12720b57cec5SDimitry Andric       continue;
12730b57cec5SDimitry Andric 
12740b57cec5SDimitry Andric     std::string Name = "p" + utostr(I);
12750b57cec5SDimitry Andric 
12760b57cec5SDimitry Andric     assert(Variables.find(Name) != Variables.end());
12770b57cec5SDimitry Andric     Variable &V = Variables[Name];
12780b57cec5SDimitry Andric 
12790b57cec5SDimitry Andric     std::string NewName = "s" + utostr(I);
12800b57cec5SDimitry Andric     Variable V2(V.getType(), NewName + VariablePostfix);
12810b57cec5SDimitry Andric 
12820b57cec5SDimitry Andric     OS << "  " << V2.getType().str() << " " << V2.getName() << " = "
12830b57cec5SDimitry Andric        << V.getName() << ";";
12840b57cec5SDimitry Andric     emitNewLine();
12850b57cec5SDimitry Andric 
12860b57cec5SDimitry Andric     V = V2;
12870b57cec5SDimitry Andric   }
12880b57cec5SDimitry Andric }
12890b57cec5SDimitry Andric 
12900b57cec5SDimitry Andric bool Intrinsic::protoHasScalar() const {
1291349cc55cSDimitry Andric   return llvm::any_of(
1292349cc55cSDimitry Andric       Types, [](const Type &T) { return T.isScalar() && !T.isImmediate(); });
12930b57cec5SDimitry Andric }
12940b57cec5SDimitry Andric 
12950b57cec5SDimitry Andric void Intrinsic::emitBodyAsBuiltinCall() {
12960b57cec5SDimitry Andric   std::string S;
12970b57cec5SDimitry Andric 
12980b57cec5SDimitry Andric   // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
12990b57cec5SDimitry Andric   // sret-like argument.
13000b57cec5SDimitry Andric   bool SRet = getReturnType().getNumVectors() >= 2;
13010b57cec5SDimitry Andric 
13020b57cec5SDimitry Andric   StringRef N = Name;
13030b57cec5SDimitry Andric   ClassKind LocalCK = CK;
13040b57cec5SDimitry Andric   if (!protoHasScalar())
13050b57cec5SDimitry Andric     LocalCK = ClassB;
13060b57cec5SDimitry Andric 
13070b57cec5SDimitry Andric   if (!getReturnType().isVoid() && !SRet)
13080b57cec5SDimitry Andric     S += "(" + RetVar.getType().str() + ") ";
13090b57cec5SDimitry Andric 
13105ffd83dbSDimitry Andric   S += "__builtin_neon_" + mangleName(std::string(N), LocalCK) + "(";
13110b57cec5SDimitry Andric 
13120b57cec5SDimitry Andric   if (SRet)
13130b57cec5SDimitry Andric     S += "&" + RetVar.getName() + ", ";
13140b57cec5SDimitry Andric 
13150b57cec5SDimitry Andric   for (unsigned I = 0; I < getNumParams(); ++I) {
13160b57cec5SDimitry Andric     Variable &V = Variables["p" + utostr(I)];
13170b57cec5SDimitry Andric     Type T = V.getType();
13180b57cec5SDimitry Andric 
13190b57cec5SDimitry Andric     // Handle multiple-vector values specially, emitting each subvector as an
13200b57cec5SDimitry Andric     // argument to the builtin.
13210b57cec5SDimitry Andric     if (T.getNumVectors() > 1) {
13220b57cec5SDimitry Andric       // Check if an explicit cast is needed.
13230b57cec5SDimitry Andric       std::string Cast;
1324a7dea167SDimitry Andric       if (LocalCK == ClassB) {
13250b57cec5SDimitry Andric         Type T2 = T;
13260b57cec5SDimitry Andric         T2.makeOneVector();
132704eeddc0SDimitry Andric         T2.makeInteger(8, /*Sign=*/true);
13280b57cec5SDimitry Andric         Cast = "(" + T2.str() + ")";
13290b57cec5SDimitry Andric       }
13300b57cec5SDimitry Andric 
13310b57cec5SDimitry Andric       for (unsigned J = 0; J < T.getNumVectors(); ++J)
13320b57cec5SDimitry Andric         S += Cast + V.getName() + ".val[" + utostr(J) + "], ";
13330b57cec5SDimitry Andric       continue;
13340b57cec5SDimitry Andric     }
13350b57cec5SDimitry Andric 
1336480093f4SDimitry Andric     std::string Arg = V.getName();
13370b57cec5SDimitry Andric     Type CastToType = T;
13380b57cec5SDimitry Andric 
13390b57cec5SDimitry Andric     // Check if an explicit cast is needed.
1340a7dea167SDimitry Andric     if (CastToType.isVector() &&
1341a7dea167SDimitry Andric         (LocalCK == ClassB || (T.isHalf() && !T.isScalarForMangling()))) {
13420b57cec5SDimitry Andric       CastToType.makeInteger(8, true);
13430b57cec5SDimitry Andric       Arg = "(" + CastToType.str() + ")" + Arg;
1344a7dea167SDimitry Andric     } else if (CastToType.isVector() && LocalCK == ClassI) {
1345480093f4SDimitry Andric       if (CastToType.isInteger())
1346a7dea167SDimitry Andric         CastToType.makeSigned();
1347a7dea167SDimitry Andric       Arg = "(" + CastToType.str() + ")" + Arg;
13480b57cec5SDimitry Andric     }
13490b57cec5SDimitry Andric 
13500b57cec5SDimitry Andric     S += Arg + ", ";
13510b57cec5SDimitry Andric   }
13520b57cec5SDimitry Andric 
13530b57cec5SDimitry Andric   // Extra constant integer to hold type class enum for this function, e.g. s8
13540b57cec5SDimitry Andric   if (getClassKind(true) == ClassB) {
1355480093f4SDimitry Andric     S += utostr(getPolymorphicKeyType().getNeonEnum());
13560b57cec5SDimitry Andric   } else {
13570b57cec5SDimitry Andric     // Remove extraneous ", ".
13580b57cec5SDimitry Andric     S.pop_back();
13590b57cec5SDimitry Andric     S.pop_back();
13600b57cec5SDimitry Andric   }
13610b57cec5SDimitry Andric   S += ");";
13620b57cec5SDimitry Andric 
13630b57cec5SDimitry Andric   std::string RetExpr;
13640b57cec5SDimitry Andric   if (!SRet && !RetVar.getType().isVoid())
13650b57cec5SDimitry Andric     RetExpr = RetVar.getName() + " = ";
13660b57cec5SDimitry Andric 
13670b57cec5SDimitry Andric   OS << "  " << RetExpr << S;
13680b57cec5SDimitry Andric   emitNewLine();
13690b57cec5SDimitry Andric }
13700b57cec5SDimitry Andric 
13710b57cec5SDimitry Andric void Intrinsic::emitBody(StringRef CallPrefix) {
13720b57cec5SDimitry Andric   std::vector<std::string> Lines;
13730b57cec5SDimitry Andric 
13740b57cec5SDimitry Andric   if (!Body || Body->getValues().empty()) {
13750b57cec5SDimitry Andric     // Nothing specific to output - must output a builtin.
13760b57cec5SDimitry Andric     emitBodyAsBuiltinCall();
13770b57cec5SDimitry Andric     return;
13780b57cec5SDimitry Andric   }
13790b57cec5SDimitry Andric 
13800b57cec5SDimitry Andric   // We have a list of "things to output". The last should be returned.
13810b57cec5SDimitry Andric   for (auto *I : Body->getValues()) {
13820b57cec5SDimitry Andric     if (StringInit *SI = dyn_cast<StringInit>(I)) {
13830b57cec5SDimitry Andric       Lines.push_back(replaceParamsIn(SI->getAsString()));
13840b57cec5SDimitry Andric     } else if (DagInit *DI = dyn_cast<DagInit>(I)) {
13850b57cec5SDimitry Andric       DagEmitter DE(*this, CallPrefix);
13860b57cec5SDimitry Andric       Lines.push_back(DE.emitDag(DI).second + ";");
13870b57cec5SDimitry Andric     }
13880b57cec5SDimitry Andric   }
13890b57cec5SDimitry Andric 
13900b57cec5SDimitry Andric   assert(!Lines.empty() && "Empty def?");
13910b57cec5SDimitry Andric   if (!RetVar.getType().isVoid())
13920b57cec5SDimitry Andric     Lines.back().insert(0, RetVar.getName() + " = ");
13930b57cec5SDimitry Andric 
13940b57cec5SDimitry Andric   for (auto &L : Lines) {
13950b57cec5SDimitry Andric     OS << "  " << L;
13960b57cec5SDimitry Andric     emitNewLine();
13970b57cec5SDimitry Andric   }
13980b57cec5SDimitry Andric }
13990b57cec5SDimitry Andric 
14000b57cec5SDimitry Andric void Intrinsic::emitReturn() {
14010b57cec5SDimitry Andric   if (RetVar.getType().isVoid())
14020b57cec5SDimitry Andric     return;
14030b57cec5SDimitry Andric   if (UseMacro)
14040b57cec5SDimitry Andric     OS << "  " << RetVar.getName() << ";";
14050b57cec5SDimitry Andric   else
14060b57cec5SDimitry Andric     OS << "  return " << RetVar.getName() << ";";
14070b57cec5SDimitry Andric   emitNewLine();
14080b57cec5SDimitry Andric }
14090b57cec5SDimitry Andric 
14100b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDag(DagInit *DI) {
14110b57cec5SDimitry Andric   // At this point we should only be seeing a def.
14120b57cec5SDimitry Andric   DefInit *DefI = cast<DefInit>(DI->getOperator());
14130b57cec5SDimitry Andric   std::string Op = DefI->getAsString();
14140b57cec5SDimitry Andric 
14150b57cec5SDimitry Andric   if (Op == "cast" || Op == "bitcast")
14160b57cec5SDimitry Andric     return emitDagCast(DI, Op == "bitcast");
14170b57cec5SDimitry Andric   if (Op == "shuffle")
14180b57cec5SDimitry Andric     return emitDagShuffle(DI);
14190b57cec5SDimitry Andric   if (Op == "dup")
14200b57cec5SDimitry Andric     return emitDagDup(DI);
14210b57cec5SDimitry Andric   if (Op == "dup_typed")
14220b57cec5SDimitry Andric     return emitDagDupTyped(DI);
14230b57cec5SDimitry Andric   if (Op == "splat")
14240b57cec5SDimitry Andric     return emitDagSplat(DI);
14250b57cec5SDimitry Andric   if (Op == "save_temp")
14260b57cec5SDimitry Andric     return emitDagSaveTemp(DI);
14270b57cec5SDimitry Andric   if (Op == "op")
14280b57cec5SDimitry Andric     return emitDagOp(DI);
14295ffd83dbSDimitry Andric   if (Op == "call" || Op == "call_mangled")
14305ffd83dbSDimitry Andric     return emitDagCall(DI, Op == "call_mangled");
14310b57cec5SDimitry Andric   if (Op == "name_replace")
14320b57cec5SDimitry Andric     return emitDagNameReplace(DI);
14330b57cec5SDimitry Andric   if (Op == "literal")
14340b57cec5SDimitry Andric     return emitDagLiteral(DI);
14350b57cec5SDimitry Andric   assert_with_loc(false, "Unknown operation!");
14360b57cec5SDimitry Andric   return std::make_pair(Type::getVoid(), "");
14370b57cec5SDimitry Andric }
14380b57cec5SDimitry Andric 
14390b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagOp(DagInit *DI) {
14400b57cec5SDimitry Andric   std::string Op = cast<StringInit>(DI->getArg(0))->getAsUnquotedString();
14410b57cec5SDimitry Andric   if (DI->getNumArgs() == 2) {
14420b57cec5SDimitry Andric     // Unary op.
14430b57cec5SDimitry Andric     std::pair<Type, std::string> R =
14445ffd83dbSDimitry Andric         emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1)));
14450b57cec5SDimitry Andric     return std::make_pair(R.first, Op + R.second);
14460b57cec5SDimitry Andric   } else {
14470b57cec5SDimitry Andric     assert(DI->getNumArgs() == 3 && "Can only handle unary and binary ops!");
14480b57cec5SDimitry Andric     std::pair<Type, std::string> R1 =
14495ffd83dbSDimitry Andric         emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1)));
14500b57cec5SDimitry Andric     std::pair<Type, std::string> R2 =
14515ffd83dbSDimitry Andric         emitDagArg(DI->getArg(2), std::string(DI->getArgNameStr(2)));
14520b57cec5SDimitry Andric     assert_with_loc(R1.first == R2.first, "Argument type mismatch!");
14530b57cec5SDimitry Andric     return std::make_pair(R1.first, R1.second + " " + Op + " " + R2.second);
14540b57cec5SDimitry Andric   }
14550b57cec5SDimitry Andric }
14560b57cec5SDimitry Andric 
14575ffd83dbSDimitry Andric std::pair<Type, std::string>
14585ffd83dbSDimitry Andric Intrinsic::DagEmitter::emitDagCall(DagInit *DI, bool MatchMangledName) {
14590b57cec5SDimitry Andric   std::vector<Type> Types;
14600b57cec5SDimitry Andric   std::vector<std::string> Values;
14610b57cec5SDimitry Andric   for (unsigned I = 0; I < DI->getNumArgs() - 1; ++I) {
14620b57cec5SDimitry Andric     std::pair<Type, std::string> R =
14635ffd83dbSDimitry Andric         emitDagArg(DI->getArg(I + 1), std::string(DI->getArgNameStr(I + 1)));
14640b57cec5SDimitry Andric     Types.push_back(R.first);
14650b57cec5SDimitry Andric     Values.push_back(R.second);
14660b57cec5SDimitry Andric   }
14670b57cec5SDimitry Andric 
14680b57cec5SDimitry Andric   // Look up the called intrinsic.
14690b57cec5SDimitry Andric   std::string N;
14700b57cec5SDimitry Andric   if (StringInit *SI = dyn_cast<StringInit>(DI->getArg(0)))
14710b57cec5SDimitry Andric     N = SI->getAsUnquotedString();
14720b57cec5SDimitry Andric   else
14730b57cec5SDimitry Andric     N = emitDagArg(DI->getArg(0), "").second;
1474*bdd1243dSDimitry Andric   std::optional<std::string> MangledName;
14755ffd83dbSDimitry Andric   if (MatchMangledName) {
14765ffd83dbSDimitry Andric     if (Intr.getRecord()->getValueAsBit("isLaneQ"))
14775ffd83dbSDimitry Andric       N += "q";
14785ffd83dbSDimitry Andric     MangledName = Intr.mangleName(N, ClassS);
14795ffd83dbSDimitry Andric   }
14805ffd83dbSDimitry Andric   Intrinsic &Callee = Intr.Emitter.getIntrinsic(N, Types, MangledName);
14810b57cec5SDimitry Andric 
14820b57cec5SDimitry Andric   // Make sure the callee is known as an early def.
14830b57cec5SDimitry Andric   Callee.setNeededEarly();
14840b57cec5SDimitry Andric   Intr.Dependencies.insert(&Callee);
14850b57cec5SDimitry Andric 
14860b57cec5SDimitry Andric   // Now create the call itself.
148704eeddc0SDimitry Andric   std::string S;
1488a7dea167SDimitry Andric   if (!Callee.isBigEndianSafe())
1489a7dea167SDimitry Andric     S += CallPrefix.str();
1490a7dea167SDimitry Andric   S += Callee.getMangledName(true) + "(";
14910b57cec5SDimitry Andric   for (unsigned I = 0; I < DI->getNumArgs() - 1; ++I) {
14920b57cec5SDimitry Andric     if (I != 0)
14930b57cec5SDimitry Andric       S += ", ";
14940b57cec5SDimitry Andric     S += Values[I];
14950b57cec5SDimitry Andric   }
14960b57cec5SDimitry Andric   S += ")";
14970b57cec5SDimitry Andric 
14980b57cec5SDimitry Andric   return std::make_pair(Callee.getReturnType(), S);
14990b57cec5SDimitry Andric }
15000b57cec5SDimitry Andric 
15010b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagCast(DagInit *DI,
15020b57cec5SDimitry Andric                                                                 bool IsBitCast){
15030b57cec5SDimitry Andric   // (cast MOD* VAL) -> cast VAL to type given by MOD.
15045ffd83dbSDimitry Andric   std::pair<Type, std::string> R =
15055ffd83dbSDimitry Andric       emitDagArg(DI->getArg(DI->getNumArgs() - 1),
15065ffd83dbSDimitry Andric                  std::string(DI->getArgNameStr(DI->getNumArgs() - 1)));
15070b57cec5SDimitry Andric   Type castToType = R.first;
15080b57cec5SDimitry Andric   for (unsigned ArgIdx = 0; ArgIdx < DI->getNumArgs() - 1; ++ArgIdx) {
15090b57cec5SDimitry Andric 
15100b57cec5SDimitry Andric     // MOD can take several forms:
15110b57cec5SDimitry Andric     //   1. $X - take the type of parameter / variable X.
15120b57cec5SDimitry Andric     //   2. The value "R" - take the type of the return type.
15130b57cec5SDimitry Andric     //   3. a type string
15140b57cec5SDimitry Andric     //   4. The value "U" or "S" to switch the signedness.
15150b57cec5SDimitry Andric     //   5. The value "H" or "D" to half or double the bitwidth.
15160b57cec5SDimitry Andric     //   6. The value "8" to convert to 8-bit (signed) integer lanes.
15170b57cec5SDimitry Andric     if (!DI->getArgNameStr(ArgIdx).empty()) {
15185ffd83dbSDimitry Andric       assert_with_loc(Intr.Variables.find(std::string(
15195ffd83dbSDimitry Andric                           DI->getArgNameStr(ArgIdx))) != Intr.Variables.end(),
15200b57cec5SDimitry Andric                       "Variable not found");
15215ffd83dbSDimitry Andric       castToType =
15225ffd83dbSDimitry Andric           Intr.Variables[std::string(DI->getArgNameStr(ArgIdx))].getType();
15230b57cec5SDimitry Andric     } else {
15240b57cec5SDimitry Andric       StringInit *SI = dyn_cast<StringInit>(DI->getArg(ArgIdx));
15250b57cec5SDimitry Andric       assert_with_loc(SI, "Expected string type or $Name for cast type");
15260b57cec5SDimitry Andric 
15270b57cec5SDimitry Andric       if (SI->getAsUnquotedString() == "R") {
15280b57cec5SDimitry Andric         castToType = Intr.getReturnType();
15290b57cec5SDimitry Andric       } else if (SI->getAsUnquotedString() == "U") {
15300b57cec5SDimitry Andric         castToType.makeUnsigned();
15310b57cec5SDimitry Andric       } else if (SI->getAsUnquotedString() == "S") {
15320b57cec5SDimitry Andric         castToType.makeSigned();
15330b57cec5SDimitry Andric       } else if (SI->getAsUnquotedString() == "H") {
15340b57cec5SDimitry Andric         castToType.halveLanes();
15350b57cec5SDimitry Andric       } else if (SI->getAsUnquotedString() == "D") {
15360b57cec5SDimitry Andric         castToType.doubleLanes();
15370b57cec5SDimitry Andric       } else if (SI->getAsUnquotedString() == "8") {
15380b57cec5SDimitry Andric         castToType.makeInteger(8, true);
15395ffd83dbSDimitry Andric       } else if (SI->getAsUnquotedString() == "32") {
15405ffd83dbSDimitry Andric         castToType.make32BitElement();
15410b57cec5SDimitry Andric       } else {
15420b57cec5SDimitry Andric         castToType = Type::fromTypedefName(SI->getAsUnquotedString());
15430b57cec5SDimitry Andric         assert_with_loc(!castToType.isVoid(), "Unknown typedef");
15440b57cec5SDimitry Andric       }
15450b57cec5SDimitry Andric     }
15460b57cec5SDimitry Andric   }
15470b57cec5SDimitry Andric 
15480b57cec5SDimitry Andric   std::string S;
15490b57cec5SDimitry Andric   if (IsBitCast) {
15500b57cec5SDimitry Andric     // Emit a reinterpret cast. The second operand must be an lvalue, so create
15510b57cec5SDimitry Andric     // a temporary.
15520b57cec5SDimitry Andric     std::string N = "reint";
15530b57cec5SDimitry Andric     unsigned I = 0;
15540b57cec5SDimitry Andric     while (Intr.Variables.find(N) != Intr.Variables.end())
15550b57cec5SDimitry Andric       N = "reint" + utostr(++I);
15560b57cec5SDimitry Andric     Intr.Variables[N] = Variable(R.first, N + Intr.VariablePostfix);
15570b57cec5SDimitry Andric 
15580b57cec5SDimitry Andric     Intr.OS << R.first.str() << " " << Intr.Variables[N].getName() << " = "
15590b57cec5SDimitry Andric             << R.second << ";";
15600b57cec5SDimitry Andric     Intr.emitNewLine();
15610b57cec5SDimitry Andric 
15620b57cec5SDimitry Andric     S = "*(" + castToType.str() + " *) &" + Intr.Variables[N].getName() + "";
15630b57cec5SDimitry Andric   } else {
15640b57cec5SDimitry Andric     // Emit a normal (static) cast.
15650b57cec5SDimitry Andric     S = "(" + castToType.str() + ")(" + R.second + ")";
15660b57cec5SDimitry Andric   }
15670b57cec5SDimitry Andric 
15680b57cec5SDimitry Andric   return std::make_pair(castToType, S);
15690b57cec5SDimitry Andric }
15700b57cec5SDimitry Andric 
15710b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagShuffle(DagInit *DI){
15720b57cec5SDimitry Andric   // See the documentation in arm_neon.td for a description of these operators.
15730b57cec5SDimitry Andric   class LowHalf : public SetTheory::Operator {
15740b57cec5SDimitry Andric   public:
15750b57cec5SDimitry Andric     void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
15760b57cec5SDimitry Andric                ArrayRef<SMLoc> Loc) override {
15770b57cec5SDimitry Andric       SetTheory::RecSet Elts2;
15780b57cec5SDimitry Andric       ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts2, Loc);
15790b57cec5SDimitry Andric       Elts.insert(Elts2.begin(), Elts2.begin() + (Elts2.size() / 2));
15800b57cec5SDimitry Andric     }
15810b57cec5SDimitry Andric   };
15820b57cec5SDimitry Andric 
15830b57cec5SDimitry Andric   class HighHalf : public SetTheory::Operator {
15840b57cec5SDimitry Andric   public:
15850b57cec5SDimitry Andric     void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
15860b57cec5SDimitry Andric                ArrayRef<SMLoc> Loc) override {
15870b57cec5SDimitry Andric       SetTheory::RecSet Elts2;
15880b57cec5SDimitry Andric       ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts2, Loc);
15890b57cec5SDimitry Andric       Elts.insert(Elts2.begin() + (Elts2.size() / 2), Elts2.end());
15900b57cec5SDimitry Andric     }
15910b57cec5SDimitry Andric   };
15920b57cec5SDimitry Andric 
15930b57cec5SDimitry Andric   class Rev : public SetTheory::Operator {
15940b57cec5SDimitry Andric     unsigned ElementSize;
15950b57cec5SDimitry Andric 
15960b57cec5SDimitry Andric   public:
15970b57cec5SDimitry Andric     Rev(unsigned ElementSize) : ElementSize(ElementSize) {}
15980b57cec5SDimitry Andric 
15990b57cec5SDimitry Andric     void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
16000b57cec5SDimitry Andric                ArrayRef<SMLoc> Loc) override {
16010b57cec5SDimitry Andric       SetTheory::RecSet Elts2;
16020b57cec5SDimitry Andric       ST.evaluate(Expr->arg_begin() + 1, Expr->arg_end(), Elts2, Loc);
16030b57cec5SDimitry Andric 
16040b57cec5SDimitry Andric       int64_t VectorSize = cast<IntInit>(Expr->getArg(0))->getValue();
16050b57cec5SDimitry Andric       VectorSize /= ElementSize;
16060b57cec5SDimitry Andric 
16070b57cec5SDimitry Andric       std::vector<Record *> Revved;
16080b57cec5SDimitry Andric       for (unsigned VI = 0; VI < Elts2.size(); VI += VectorSize) {
16090b57cec5SDimitry Andric         for (int LI = VectorSize - 1; LI >= 0; --LI) {
16100b57cec5SDimitry Andric           Revved.push_back(Elts2[VI + LI]);
16110b57cec5SDimitry Andric         }
16120b57cec5SDimitry Andric       }
16130b57cec5SDimitry Andric 
16140b57cec5SDimitry Andric       Elts.insert(Revved.begin(), Revved.end());
16150b57cec5SDimitry Andric     }
16160b57cec5SDimitry Andric   };
16170b57cec5SDimitry Andric 
16180b57cec5SDimitry Andric   class MaskExpander : public SetTheory::Expander {
16190b57cec5SDimitry Andric     unsigned N;
16200b57cec5SDimitry Andric 
16210b57cec5SDimitry Andric   public:
16220b57cec5SDimitry Andric     MaskExpander(unsigned N) : N(N) {}
16230b57cec5SDimitry Andric 
16240b57cec5SDimitry Andric     void expand(SetTheory &ST, Record *R, SetTheory::RecSet &Elts) override {
16250b57cec5SDimitry Andric       unsigned Addend = 0;
16260b57cec5SDimitry Andric       if (R->getName() == "mask0")
16270b57cec5SDimitry Andric         Addend = 0;
16280b57cec5SDimitry Andric       else if (R->getName() == "mask1")
16290b57cec5SDimitry Andric         Addend = N;
16300b57cec5SDimitry Andric       else
16310b57cec5SDimitry Andric         return;
16320b57cec5SDimitry Andric       for (unsigned I = 0; I < N; ++I)
16330b57cec5SDimitry Andric         Elts.insert(R->getRecords().getDef("sv" + utostr(I + Addend)));
16340b57cec5SDimitry Andric     }
16350b57cec5SDimitry Andric   };
16360b57cec5SDimitry Andric 
16370b57cec5SDimitry Andric   // (shuffle arg1, arg2, sequence)
16380b57cec5SDimitry Andric   std::pair<Type, std::string> Arg1 =
16395ffd83dbSDimitry Andric       emitDagArg(DI->getArg(0), std::string(DI->getArgNameStr(0)));
16400b57cec5SDimitry Andric   std::pair<Type, std::string> Arg2 =
16415ffd83dbSDimitry Andric       emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1)));
16420b57cec5SDimitry Andric   assert_with_loc(Arg1.first == Arg2.first,
16430b57cec5SDimitry Andric                   "Different types in arguments to shuffle!");
16440b57cec5SDimitry Andric 
16450b57cec5SDimitry Andric   SetTheory ST;
16460b57cec5SDimitry Andric   SetTheory::RecSet Elts;
1647a7dea167SDimitry Andric   ST.addOperator("lowhalf", std::make_unique<LowHalf>());
1648a7dea167SDimitry Andric   ST.addOperator("highhalf", std::make_unique<HighHalf>());
16490b57cec5SDimitry Andric   ST.addOperator("rev",
1650a7dea167SDimitry Andric                  std::make_unique<Rev>(Arg1.first.getElementSizeInBits()));
16510b57cec5SDimitry Andric   ST.addExpander("MaskExpand",
1652a7dea167SDimitry Andric                  std::make_unique<MaskExpander>(Arg1.first.getNumElements()));
1653*bdd1243dSDimitry Andric   ST.evaluate(DI->getArg(2), Elts, std::nullopt);
16540b57cec5SDimitry Andric 
16550b57cec5SDimitry Andric   std::string S = "__builtin_shufflevector(" + Arg1.second + ", " + Arg2.second;
16560b57cec5SDimitry Andric   for (auto &E : Elts) {
16570b57cec5SDimitry Andric     StringRef Name = E->getName();
16580b57cec5SDimitry Andric     assert_with_loc(Name.startswith("sv"),
16590b57cec5SDimitry Andric                     "Incorrect element kind in shuffle mask!");
16600b57cec5SDimitry Andric     S += ", " + Name.drop_front(2).str();
16610b57cec5SDimitry Andric   }
16620b57cec5SDimitry Andric   S += ")";
16630b57cec5SDimitry Andric 
16640b57cec5SDimitry Andric   // Recalculate the return type - the shuffle may have halved or doubled it.
16650b57cec5SDimitry Andric   Type T(Arg1.first);
16660b57cec5SDimitry Andric   if (Elts.size() > T.getNumElements()) {
16670b57cec5SDimitry Andric     assert_with_loc(
16680b57cec5SDimitry Andric         Elts.size() == T.getNumElements() * 2,
16690b57cec5SDimitry Andric         "Can only double or half the number of elements in a shuffle!");
16700b57cec5SDimitry Andric     T.doubleLanes();
16710b57cec5SDimitry Andric   } else if (Elts.size() < T.getNumElements()) {
16720b57cec5SDimitry Andric     assert_with_loc(
16730b57cec5SDimitry Andric         Elts.size() == T.getNumElements() / 2,
16740b57cec5SDimitry Andric         "Can only double or half the number of elements in a shuffle!");
16750b57cec5SDimitry Andric     T.halveLanes();
16760b57cec5SDimitry Andric   }
16770b57cec5SDimitry Andric 
16780b57cec5SDimitry Andric   return std::make_pair(T, S);
16790b57cec5SDimitry Andric }
16800b57cec5SDimitry Andric 
16810b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagDup(DagInit *DI) {
16820b57cec5SDimitry Andric   assert_with_loc(DI->getNumArgs() == 1, "dup() expects one argument");
16835ffd83dbSDimitry Andric   std::pair<Type, std::string> A =
16845ffd83dbSDimitry Andric       emitDagArg(DI->getArg(0), std::string(DI->getArgNameStr(0)));
16850b57cec5SDimitry Andric   assert_with_loc(A.first.isScalar(), "dup() expects a scalar argument");
16860b57cec5SDimitry Andric 
16870b57cec5SDimitry Andric   Type T = Intr.getBaseType();
16880b57cec5SDimitry Andric   assert_with_loc(T.isVector(), "dup() used but default type is scalar!");
16890b57cec5SDimitry Andric   std::string S = "(" + T.str() + ") {";
16900b57cec5SDimitry Andric   for (unsigned I = 0; I < T.getNumElements(); ++I) {
16910b57cec5SDimitry Andric     if (I != 0)
16920b57cec5SDimitry Andric       S += ", ";
16930b57cec5SDimitry Andric     S += A.second;
16940b57cec5SDimitry Andric   }
16950b57cec5SDimitry Andric   S += "}";
16960b57cec5SDimitry Andric 
16970b57cec5SDimitry Andric   return std::make_pair(T, S);
16980b57cec5SDimitry Andric }
16990b57cec5SDimitry Andric 
17000b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagDupTyped(DagInit *DI) {
17010b57cec5SDimitry Andric   assert_with_loc(DI->getNumArgs() == 2, "dup_typed() expects two arguments");
17025ffd83dbSDimitry Andric   std::pair<Type, std::string> B =
17035ffd83dbSDimitry Andric       emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1)));
17040b57cec5SDimitry Andric   assert_with_loc(B.first.isScalar(),
17050b57cec5SDimitry Andric                   "dup_typed() requires a scalar as the second argument");
1706e8d8bef9SDimitry Andric   Type T;
1707e8d8bef9SDimitry Andric   // If the type argument is a constant string, construct the type directly.
1708e8d8bef9SDimitry Andric   if (StringInit *SI = dyn_cast<StringInit>(DI->getArg(0))) {
1709e8d8bef9SDimitry Andric     T = Type::fromTypedefName(SI->getAsUnquotedString());
1710e8d8bef9SDimitry Andric     assert_with_loc(!T.isVoid(), "Unknown typedef");
1711e8d8bef9SDimitry Andric   } else
1712e8d8bef9SDimitry Andric     T = emitDagArg(DI->getArg(0), std::string(DI->getArgNameStr(0))).first;
17130b57cec5SDimitry Andric 
17140b57cec5SDimitry Andric   assert_with_loc(T.isVector(), "dup_typed() used but target type is scalar!");
17150b57cec5SDimitry Andric   std::string S = "(" + T.str() + ") {";
17160b57cec5SDimitry Andric   for (unsigned I = 0; I < T.getNumElements(); ++I) {
17170b57cec5SDimitry Andric     if (I != 0)
17180b57cec5SDimitry Andric       S += ", ";
17190b57cec5SDimitry Andric     S += B.second;
17200b57cec5SDimitry Andric   }
17210b57cec5SDimitry Andric   S += "}";
17220b57cec5SDimitry Andric 
17230b57cec5SDimitry Andric   return std::make_pair(T, S);
17240b57cec5SDimitry Andric }
17250b57cec5SDimitry Andric 
17260b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagSplat(DagInit *DI) {
17270b57cec5SDimitry Andric   assert_with_loc(DI->getNumArgs() == 2, "splat() expects two arguments");
17285ffd83dbSDimitry Andric   std::pair<Type, std::string> A =
17295ffd83dbSDimitry Andric       emitDagArg(DI->getArg(0), std::string(DI->getArgNameStr(0)));
17305ffd83dbSDimitry Andric   std::pair<Type, std::string> B =
17315ffd83dbSDimitry Andric       emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1)));
17320b57cec5SDimitry Andric 
17330b57cec5SDimitry Andric   assert_with_loc(B.first.isScalar(),
17340b57cec5SDimitry Andric                   "splat() requires a scalar int as the second argument");
17350b57cec5SDimitry Andric 
17360b57cec5SDimitry Andric   std::string S = "__builtin_shufflevector(" + A.second + ", " + A.second;
17370b57cec5SDimitry Andric   for (unsigned I = 0; I < Intr.getBaseType().getNumElements(); ++I) {
17380b57cec5SDimitry Andric     S += ", " + B.second;
17390b57cec5SDimitry Andric   }
17400b57cec5SDimitry Andric   S += ")";
17410b57cec5SDimitry Andric 
17420b57cec5SDimitry Andric   return std::make_pair(Intr.getBaseType(), S);
17430b57cec5SDimitry Andric }
17440b57cec5SDimitry Andric 
17450b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagSaveTemp(DagInit *DI) {
17460b57cec5SDimitry Andric   assert_with_loc(DI->getNumArgs() == 2, "save_temp() expects two arguments");
17475ffd83dbSDimitry Andric   std::pair<Type, std::string> A =
17485ffd83dbSDimitry Andric       emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1)));
17490b57cec5SDimitry Andric 
17500b57cec5SDimitry Andric   assert_with_loc(!A.first.isVoid(),
17510b57cec5SDimitry Andric                   "Argument to save_temp() must have non-void type!");
17520b57cec5SDimitry Andric 
17535ffd83dbSDimitry Andric   std::string N = std::string(DI->getArgNameStr(0));
17540b57cec5SDimitry Andric   assert_with_loc(!N.empty(),
17550b57cec5SDimitry Andric                   "save_temp() expects a name as the first argument");
17560b57cec5SDimitry Andric 
17570b57cec5SDimitry Andric   assert_with_loc(Intr.Variables.find(N) == Intr.Variables.end(),
17580b57cec5SDimitry Andric                   "Variable already defined!");
17590b57cec5SDimitry Andric   Intr.Variables[N] = Variable(A.first, N + Intr.VariablePostfix);
17600b57cec5SDimitry Andric 
17610b57cec5SDimitry Andric   std::string S =
17620b57cec5SDimitry Andric       A.first.str() + " " + Intr.Variables[N].getName() + " = " + A.second;
17630b57cec5SDimitry Andric 
17640b57cec5SDimitry Andric   return std::make_pair(Type::getVoid(), S);
17650b57cec5SDimitry Andric }
17660b57cec5SDimitry Andric 
17670b57cec5SDimitry Andric std::pair<Type, std::string>
17680b57cec5SDimitry Andric Intrinsic::DagEmitter::emitDagNameReplace(DagInit *DI) {
17690b57cec5SDimitry Andric   std::string S = Intr.Name;
17700b57cec5SDimitry Andric 
17710b57cec5SDimitry Andric   assert_with_loc(DI->getNumArgs() == 2, "name_replace requires 2 arguments!");
17720b57cec5SDimitry Andric   std::string ToReplace = cast<StringInit>(DI->getArg(0))->getAsUnquotedString();
17730b57cec5SDimitry Andric   std::string ReplaceWith = cast<StringInit>(DI->getArg(1))->getAsUnquotedString();
17740b57cec5SDimitry Andric 
17750b57cec5SDimitry Andric   size_t Idx = S.find(ToReplace);
17760b57cec5SDimitry Andric 
17770b57cec5SDimitry Andric   assert_with_loc(Idx != std::string::npos, "name should contain '" + ToReplace + "'!");
17780b57cec5SDimitry Andric   S.replace(Idx, ToReplace.size(), ReplaceWith);
17790b57cec5SDimitry Andric 
17800b57cec5SDimitry Andric   return std::make_pair(Type::getVoid(), S);
17810b57cec5SDimitry Andric }
17820b57cec5SDimitry Andric 
17830b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagLiteral(DagInit *DI){
17840b57cec5SDimitry Andric   std::string Ty = cast<StringInit>(DI->getArg(0))->getAsUnquotedString();
17850b57cec5SDimitry Andric   std::string Value = cast<StringInit>(DI->getArg(1))->getAsUnquotedString();
17860b57cec5SDimitry Andric   return std::make_pair(Type::fromTypedefName(Ty), Value);
17870b57cec5SDimitry Andric }
17880b57cec5SDimitry Andric 
17890b57cec5SDimitry Andric std::pair<Type, std::string>
17900b57cec5SDimitry Andric Intrinsic::DagEmitter::emitDagArg(Init *Arg, std::string ArgName) {
17910b57cec5SDimitry Andric   if (!ArgName.empty()) {
17920b57cec5SDimitry Andric     assert_with_loc(!Arg->isComplete(),
17930b57cec5SDimitry Andric                     "Arguments must either be DAGs or names, not both!");
17940b57cec5SDimitry Andric     assert_with_loc(Intr.Variables.find(ArgName) != Intr.Variables.end(),
17950b57cec5SDimitry Andric                     "Variable not defined!");
17960b57cec5SDimitry Andric     Variable &V = Intr.Variables[ArgName];
17970b57cec5SDimitry Andric     return std::make_pair(V.getType(), V.getName());
17980b57cec5SDimitry Andric   }
17990b57cec5SDimitry Andric 
18000b57cec5SDimitry Andric   assert(Arg && "Neither ArgName nor Arg?!");
18010b57cec5SDimitry Andric   DagInit *DI = dyn_cast<DagInit>(Arg);
18020b57cec5SDimitry Andric   assert_with_loc(DI, "Arguments must either be DAGs or names!");
18030b57cec5SDimitry Andric 
18040b57cec5SDimitry Andric   return emitDag(DI);
18050b57cec5SDimitry Andric }
18060b57cec5SDimitry Andric 
18070b57cec5SDimitry Andric std::string Intrinsic::generate() {
1808a7dea167SDimitry Andric   // Avoid duplicated code for big and little endian
1809a7dea167SDimitry Andric   if (isBigEndianSafe()) {
1810a7dea167SDimitry Andric     generateImpl(false, "", "");
1811a7dea167SDimitry Andric     return OS.str();
1812a7dea167SDimitry Andric   }
18130b57cec5SDimitry Andric   // Little endian intrinsics are simple and don't require any argument
18140b57cec5SDimitry Andric   // swapping.
18150b57cec5SDimitry Andric   OS << "#ifdef __LITTLE_ENDIAN__\n";
18160b57cec5SDimitry Andric 
18170b57cec5SDimitry Andric   generateImpl(false, "", "");
18180b57cec5SDimitry Andric 
18190b57cec5SDimitry Andric   OS << "#else\n";
18200b57cec5SDimitry Andric 
18210b57cec5SDimitry Andric   // Big endian intrinsics are more complex. The user intended these
18220b57cec5SDimitry Andric   // intrinsics to operate on a vector "as-if" loaded by (V)LDR,
18230b57cec5SDimitry Andric   // but we load as-if (V)LD1. So we should swap all arguments and
18240b57cec5SDimitry Andric   // swap the return value too.
18250b57cec5SDimitry Andric   //
18260b57cec5SDimitry Andric   // If we call sub-intrinsics, we should call a version that does
18270b57cec5SDimitry Andric   // not re-swap the arguments!
18280b57cec5SDimitry Andric   generateImpl(true, "", "__noswap_");
18290b57cec5SDimitry Andric 
18300b57cec5SDimitry Andric   // If we're needed early, create a non-swapping variant for
18310b57cec5SDimitry Andric   // big-endian.
18320b57cec5SDimitry Andric   if (NeededEarly) {
18330b57cec5SDimitry Andric     generateImpl(false, "__noswap_", "__noswap_");
18340b57cec5SDimitry Andric   }
18350b57cec5SDimitry Andric   OS << "#endif\n\n";
18360b57cec5SDimitry Andric 
18370b57cec5SDimitry Andric   return OS.str();
18380b57cec5SDimitry Andric }
18390b57cec5SDimitry Andric 
18400b57cec5SDimitry Andric void Intrinsic::generateImpl(bool ReverseArguments,
18410b57cec5SDimitry Andric                              StringRef NamePrefix, StringRef CallPrefix) {
18420b57cec5SDimitry Andric   CurrentRecord = R;
18430b57cec5SDimitry Andric 
18440b57cec5SDimitry Andric   // If we call a macro, our local variables may be corrupted due to
18450b57cec5SDimitry Andric   // lack of proper lexical scoping. So, add a globally unique postfix
18460b57cec5SDimitry Andric   // to every variable.
18470b57cec5SDimitry Andric   //
18480b57cec5SDimitry Andric   // indexBody() should have set up the Dependencies set by now.
18490b57cec5SDimitry Andric   for (auto *I : Dependencies)
18500b57cec5SDimitry Andric     if (I->UseMacro) {
18510b57cec5SDimitry Andric       VariablePostfix = "_" + utostr(Emitter.getUniqueNumber());
18520b57cec5SDimitry Andric       break;
18530b57cec5SDimitry Andric     }
18540b57cec5SDimitry Andric 
18550b57cec5SDimitry Andric   initVariables();
18560b57cec5SDimitry Andric 
18570b57cec5SDimitry Andric   emitPrototype(NamePrefix);
18580b57cec5SDimitry Andric 
18590b57cec5SDimitry Andric   if (IsUnavailable) {
18600b57cec5SDimitry Andric     OS << " __attribute__((unavailable));";
18610b57cec5SDimitry Andric   } else {
18620b57cec5SDimitry Andric     emitOpeningBrace();
18633a9a9c0cSDimitry Andric     // Emit return variable declaration first as to not trigger
18643a9a9c0cSDimitry Andric     // -Wdeclaration-after-statement.
18653a9a9c0cSDimitry Andric     emitReturnVarDecl();
18660b57cec5SDimitry Andric     emitShadowedArgs();
18670b57cec5SDimitry Andric     if (ReverseArguments)
18680b57cec5SDimitry Andric       emitArgumentReversal();
18690b57cec5SDimitry Andric     emitBody(CallPrefix);
18700b57cec5SDimitry Andric     if (ReverseArguments)
18710b57cec5SDimitry Andric       emitReturnReversal();
18720b57cec5SDimitry Andric     emitReturn();
18730b57cec5SDimitry Andric     emitClosingBrace();
18740b57cec5SDimitry Andric   }
18750b57cec5SDimitry Andric   OS << "\n";
18760b57cec5SDimitry Andric 
18770b57cec5SDimitry Andric   CurrentRecord = nullptr;
18780b57cec5SDimitry Andric }
18790b57cec5SDimitry Andric 
18800b57cec5SDimitry Andric void Intrinsic::indexBody() {
18810b57cec5SDimitry Andric   CurrentRecord = R;
18820b57cec5SDimitry Andric 
18830b57cec5SDimitry Andric   initVariables();
18843a9a9c0cSDimitry Andric   // Emit return variable declaration first as to not trigger
18853a9a9c0cSDimitry Andric   // -Wdeclaration-after-statement.
18863a9a9c0cSDimitry Andric   emitReturnVarDecl();
18870b57cec5SDimitry Andric   emitBody("");
18880b57cec5SDimitry Andric   OS.str("");
18890b57cec5SDimitry Andric 
18900b57cec5SDimitry Andric   CurrentRecord = nullptr;
18910b57cec5SDimitry Andric }
18920b57cec5SDimitry Andric 
18930b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
18940b57cec5SDimitry Andric // NeonEmitter implementation
18950b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
18960b57cec5SDimitry Andric 
18975ffd83dbSDimitry Andric Intrinsic &NeonEmitter::getIntrinsic(StringRef Name, ArrayRef<Type> Types,
1898*bdd1243dSDimitry Andric                                      std::optional<std::string> MangledName) {
18990b57cec5SDimitry Andric   // First, look up the name in the intrinsic map.
19000b57cec5SDimitry Andric   assert_with_loc(IntrinsicMap.find(Name.str()) != IntrinsicMap.end(),
19010b57cec5SDimitry Andric                   ("Intrinsic '" + Name + "' not found!").str());
19020b57cec5SDimitry Andric   auto &V = IntrinsicMap.find(Name.str())->second;
19030b57cec5SDimitry Andric   std::vector<Intrinsic *> GoodVec;
19040b57cec5SDimitry Andric 
19050b57cec5SDimitry Andric   // Create a string to print if we end up failing.
19060b57cec5SDimitry Andric   std::string ErrMsg = "looking up intrinsic '" + Name.str() + "(";
19070b57cec5SDimitry Andric   for (unsigned I = 0; I < Types.size(); ++I) {
19080b57cec5SDimitry Andric     if (I != 0)
19090b57cec5SDimitry Andric       ErrMsg += ", ";
19100b57cec5SDimitry Andric     ErrMsg += Types[I].str();
19110b57cec5SDimitry Andric   }
19120b57cec5SDimitry Andric   ErrMsg += ")'\n";
19130b57cec5SDimitry Andric   ErrMsg += "Available overloads:\n";
19140b57cec5SDimitry Andric 
19150b57cec5SDimitry Andric   // Now, look through each intrinsic implementation and see if the types are
19160b57cec5SDimitry Andric   // compatible.
19170b57cec5SDimitry Andric   for (auto &I : V) {
19180b57cec5SDimitry Andric     ErrMsg += "  - " + I.getReturnType().str() + " " + I.getMangledName();
19190b57cec5SDimitry Andric     ErrMsg += "(";
19200b57cec5SDimitry Andric     for (unsigned A = 0; A < I.getNumParams(); ++A) {
19210b57cec5SDimitry Andric       if (A != 0)
19220b57cec5SDimitry Andric         ErrMsg += ", ";
19230b57cec5SDimitry Andric       ErrMsg += I.getParamType(A).str();
19240b57cec5SDimitry Andric     }
19250b57cec5SDimitry Andric     ErrMsg += ")\n";
19260b57cec5SDimitry Andric 
19275ffd83dbSDimitry Andric     if (MangledName && MangledName != I.getMangledName(true))
19285ffd83dbSDimitry Andric       continue;
19295ffd83dbSDimitry Andric 
19300b57cec5SDimitry Andric     if (I.getNumParams() != Types.size())
19310b57cec5SDimitry Andric       continue;
19320b57cec5SDimitry Andric 
19335ffd83dbSDimitry Andric     unsigned ArgNum = 0;
1934349cc55cSDimitry Andric     bool MatchingArgumentTypes = llvm::all_of(Types, [&](const auto &Type) {
19355ffd83dbSDimitry Andric       return Type == I.getParamType(ArgNum++);
19365ffd83dbSDimitry Andric     });
19375ffd83dbSDimitry Andric 
19385ffd83dbSDimitry Andric     if (MatchingArgumentTypes)
19390b57cec5SDimitry Andric       GoodVec.push_back(&I);
19400b57cec5SDimitry Andric   }
19410b57cec5SDimitry Andric 
19420b57cec5SDimitry Andric   assert_with_loc(!GoodVec.empty(),
19430b57cec5SDimitry Andric                   "No compatible intrinsic found - " + ErrMsg);
19440b57cec5SDimitry Andric   assert_with_loc(GoodVec.size() == 1, "Multiple overloads found - " + ErrMsg);
19450b57cec5SDimitry Andric 
19460b57cec5SDimitry Andric   return *GoodVec.front();
19470b57cec5SDimitry Andric }
19480b57cec5SDimitry Andric 
19490b57cec5SDimitry Andric void NeonEmitter::createIntrinsic(Record *R,
19500b57cec5SDimitry Andric                                   SmallVectorImpl<Intrinsic *> &Out) {
19515ffd83dbSDimitry Andric   std::string Name = std::string(R->getValueAsString("Name"));
19525ffd83dbSDimitry Andric   std::string Proto = std::string(R->getValueAsString("Prototype"));
19535ffd83dbSDimitry Andric   std::string Types = std::string(R->getValueAsString("Types"));
19540b57cec5SDimitry Andric   Record *OperationRec = R->getValueAsDef("Operation");
19550b57cec5SDimitry Andric   bool BigEndianSafe  = R->getValueAsBit("BigEndianSafe");
1956*bdd1243dSDimitry Andric   std::string ArchGuard = std::string(R->getValueAsString("ArchGuard"));
1957*bdd1243dSDimitry Andric   std::string TargetGuard = std::string(R->getValueAsString("TargetGuard"));
19580b57cec5SDimitry Andric   bool IsUnavailable = OperationRec->getValueAsBit("Unavailable");
19595ffd83dbSDimitry Andric   std::string CartesianProductWith = std::string(R->getValueAsString("CartesianProductWith"));
19600b57cec5SDimitry Andric 
19610b57cec5SDimitry Andric   // Set the global current record. This allows assert_with_loc to produce
19620b57cec5SDimitry Andric   // decent location information even when highly nested.
19630b57cec5SDimitry Andric   CurrentRecord = R;
19640b57cec5SDimitry Andric 
19650b57cec5SDimitry Andric   ListInit *Body = OperationRec->getValueAsListInit("Ops");
19660b57cec5SDimitry Andric 
19670b57cec5SDimitry Andric   std::vector<TypeSpec> TypeSpecs = TypeSpec::fromTypeSpecs(Types);
19680b57cec5SDimitry Andric 
19690b57cec5SDimitry Andric   ClassKind CK = ClassNone;
19700b57cec5SDimitry Andric   if (R->getSuperClasses().size() >= 2)
19710b57cec5SDimitry Andric     CK = ClassMap[R->getSuperClasses()[1].first];
19720b57cec5SDimitry Andric 
19730b57cec5SDimitry Andric   std::vector<std::pair<TypeSpec, TypeSpec>> NewTypeSpecs;
19745ffd83dbSDimitry Andric   if (!CartesianProductWith.empty()) {
19755ffd83dbSDimitry Andric     std::vector<TypeSpec> ProductTypeSpecs = TypeSpec::fromTypeSpecs(CartesianProductWith);
19760b57cec5SDimitry Andric     for (auto TS : TypeSpecs) {
1977480093f4SDimitry Andric       Type DefaultT(TS, ".");
19785ffd83dbSDimitry Andric       for (auto SrcTS : ProductTypeSpecs) {
1979480093f4SDimitry Andric         Type DefaultSrcT(SrcTS, ".");
19800b57cec5SDimitry Andric         if (TS == SrcTS ||
19810b57cec5SDimitry Andric             DefaultSrcT.getSizeInBits() != DefaultT.getSizeInBits())
19820b57cec5SDimitry Andric           continue;
19830b57cec5SDimitry Andric         NewTypeSpecs.push_back(std::make_pair(TS, SrcTS));
19840b57cec5SDimitry Andric       }
19855ffd83dbSDimitry Andric     }
19860b57cec5SDimitry Andric   } else {
19875ffd83dbSDimitry Andric     for (auto TS : TypeSpecs) {
19880b57cec5SDimitry Andric       NewTypeSpecs.push_back(std::make_pair(TS, TS));
19890b57cec5SDimitry Andric     }
19900b57cec5SDimitry Andric   }
19910b57cec5SDimitry Andric 
19920b57cec5SDimitry Andric   llvm::sort(NewTypeSpecs);
19930b57cec5SDimitry Andric   NewTypeSpecs.erase(std::unique(NewTypeSpecs.begin(), NewTypeSpecs.end()),
19940b57cec5SDimitry Andric 		     NewTypeSpecs.end());
19950b57cec5SDimitry Andric   auto &Entry = IntrinsicMap[Name];
19960b57cec5SDimitry Andric 
19970b57cec5SDimitry Andric   for (auto &I : NewTypeSpecs) {
19980b57cec5SDimitry Andric     Entry.emplace_back(R, Name, Proto, I.first, I.second, CK, Body, *this,
1999*bdd1243dSDimitry Andric                        ArchGuard, TargetGuard, IsUnavailable, BigEndianSafe);
20000b57cec5SDimitry Andric     Out.push_back(&Entry.back());
20010b57cec5SDimitry Andric   }
20020b57cec5SDimitry Andric 
20030b57cec5SDimitry Andric   CurrentRecord = nullptr;
20040b57cec5SDimitry Andric }
20050b57cec5SDimitry Andric 
20060b57cec5SDimitry Andric /// genBuiltinsDef: Generate the BuiltinsARM.def and  BuiltinsAArch64.def
20070b57cec5SDimitry Andric /// declaration of builtins, checking for unique builtin declarations.
20080b57cec5SDimitry Andric void NeonEmitter::genBuiltinsDef(raw_ostream &OS,
20090b57cec5SDimitry Andric                                  SmallVectorImpl<Intrinsic *> &Defs) {
20100b57cec5SDimitry Andric   OS << "#ifdef GET_NEON_BUILTINS\n";
20110b57cec5SDimitry Andric 
20120b57cec5SDimitry Andric   // We only want to emit a builtin once, and we want to emit them in
20130b57cec5SDimitry Andric   // alphabetical order, so use a std::set.
2014*bdd1243dSDimitry Andric   std::set<std::pair<std::string, std::string>> Builtins;
20150b57cec5SDimitry Andric 
20160b57cec5SDimitry Andric   for (auto *Def : Defs) {
20170b57cec5SDimitry Andric     if (Def->hasBody())
20180b57cec5SDimitry Andric       continue;
20190b57cec5SDimitry Andric 
2020*bdd1243dSDimitry Andric     std::string S = "__builtin_neon_" + Def->getMangledName() + ", \"";
20210b57cec5SDimitry Andric     S += Def->getBuiltinTypeStr();
2022*bdd1243dSDimitry Andric     S += "\", \"n\"";
20230b57cec5SDimitry Andric 
2024*bdd1243dSDimitry Andric     Builtins.emplace(S, Def->getTargetGuard());
20250b57cec5SDimitry Andric   }
20260b57cec5SDimitry Andric 
2027*bdd1243dSDimitry Andric   for (auto &S : Builtins) {
2028*bdd1243dSDimitry Andric     if (S.second == "")
2029*bdd1243dSDimitry Andric       OS << "BUILTIN(";
2030*bdd1243dSDimitry Andric     else
2031*bdd1243dSDimitry Andric       OS << "TARGET_BUILTIN(";
2032*bdd1243dSDimitry Andric     OS << S.first;
2033*bdd1243dSDimitry Andric     if (S.second == "")
2034*bdd1243dSDimitry Andric       OS << ")\n";
2035*bdd1243dSDimitry Andric     else
2036*bdd1243dSDimitry Andric       OS << ", \"" << S.second << "\")\n";
2037*bdd1243dSDimitry Andric   }
2038*bdd1243dSDimitry Andric 
20390b57cec5SDimitry Andric   OS << "#endif\n\n";
20400b57cec5SDimitry Andric }
20410b57cec5SDimitry Andric 
20420b57cec5SDimitry Andric /// Generate the ARM and AArch64 overloaded type checking code for
20430b57cec5SDimitry Andric /// SemaChecking.cpp, checking for unique builtin declarations.
20440b57cec5SDimitry Andric void NeonEmitter::genOverloadTypeCheckCode(raw_ostream &OS,
20450b57cec5SDimitry Andric                                            SmallVectorImpl<Intrinsic *> &Defs) {
20460b57cec5SDimitry Andric   OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
20470b57cec5SDimitry Andric 
20480b57cec5SDimitry Andric   // We record each overload check line before emitting because subsequent Inst
20490b57cec5SDimitry Andric   // definitions may extend the number of permitted types (i.e. augment the
20500b57cec5SDimitry Andric   // Mask). Use std::map to avoid sorting the table by hash number.
20510b57cec5SDimitry Andric   struct OverloadInfo {
20520b57cec5SDimitry Andric     uint64_t Mask;
20530b57cec5SDimitry Andric     int PtrArgNum;
20540b57cec5SDimitry Andric     bool HasConstPtr;
20550b57cec5SDimitry Andric     OverloadInfo() : Mask(0ULL), PtrArgNum(0), HasConstPtr(false) {}
20560b57cec5SDimitry Andric   };
20570b57cec5SDimitry Andric   std::map<std::string, OverloadInfo> OverloadMap;
20580b57cec5SDimitry Andric 
20590b57cec5SDimitry Andric   for (auto *Def : Defs) {
20600b57cec5SDimitry Andric     // If the def has a body (that is, it has Operation DAGs), it won't call
20610b57cec5SDimitry Andric     // __builtin_neon_* so we don't need to generate a definition for it.
20620b57cec5SDimitry Andric     if (Def->hasBody())
20630b57cec5SDimitry Andric       continue;
20640b57cec5SDimitry Andric     // Functions which have a scalar argument cannot be overloaded, no need to
20650b57cec5SDimitry Andric     // check them if we are emitting the type checking code.
20660b57cec5SDimitry Andric     if (Def->protoHasScalar())
20670b57cec5SDimitry Andric       continue;
20680b57cec5SDimitry Andric 
20690b57cec5SDimitry Andric     uint64_t Mask = 0ULL;
2070480093f4SDimitry Andric     Mask |= 1ULL << Def->getPolymorphicKeyType().getNeonEnum();
20710b57cec5SDimitry Andric 
20720b57cec5SDimitry Andric     // Check if the function has a pointer or const pointer argument.
20730b57cec5SDimitry Andric     int PtrArgNum = -1;
20740b57cec5SDimitry Andric     bool HasConstPtr = false;
20750b57cec5SDimitry Andric     for (unsigned I = 0; I < Def->getNumParams(); ++I) {
2076480093f4SDimitry Andric       const auto &Type = Def->getParamType(I);
2077480093f4SDimitry Andric       if (Type.isPointer()) {
20780b57cec5SDimitry Andric         PtrArgNum = I;
2079480093f4SDimitry Andric         HasConstPtr = Type.isConstPointer();
20800b57cec5SDimitry Andric       }
20810b57cec5SDimitry Andric     }
2082480093f4SDimitry Andric 
20830b57cec5SDimitry Andric     // For sret builtins, adjust the pointer argument index.
20840b57cec5SDimitry Andric     if (PtrArgNum >= 0 && Def->getReturnType().getNumVectors() > 1)
20850b57cec5SDimitry Andric       PtrArgNum += 1;
20860b57cec5SDimitry Andric 
20870b57cec5SDimitry Andric     std::string Name = Def->getName();
20880b57cec5SDimitry Andric     // Omit type checking for the pointer arguments of vld1_lane, vld1_dup,
20890b57cec5SDimitry Andric     // and vst1_lane intrinsics.  Using a pointer to the vector element
20900b57cec5SDimitry Andric     // type with one of those operations causes codegen to select an aligned
20910b57cec5SDimitry Andric     // load/store instruction.  If you want an unaligned operation,
20920b57cec5SDimitry Andric     // the pointer argument needs to have less alignment than element type,
20930b57cec5SDimitry Andric     // so just accept any pointer type.
20940b57cec5SDimitry Andric     if (Name == "vld1_lane" || Name == "vld1_dup" || Name == "vst1_lane") {
20950b57cec5SDimitry Andric       PtrArgNum = -1;
20960b57cec5SDimitry Andric       HasConstPtr = false;
20970b57cec5SDimitry Andric     }
20980b57cec5SDimitry Andric 
20990b57cec5SDimitry Andric     if (Mask) {
21000b57cec5SDimitry Andric       std::string Name = Def->getMangledName();
21010b57cec5SDimitry Andric       OverloadMap.insert(std::make_pair(Name, OverloadInfo()));
21020b57cec5SDimitry Andric       OverloadInfo &OI = OverloadMap[Name];
21030b57cec5SDimitry Andric       OI.Mask |= Mask;
21040b57cec5SDimitry Andric       OI.PtrArgNum |= PtrArgNum;
21050b57cec5SDimitry Andric       OI.HasConstPtr = HasConstPtr;
21060b57cec5SDimitry Andric     }
21070b57cec5SDimitry Andric   }
21080b57cec5SDimitry Andric 
21090b57cec5SDimitry Andric   for (auto &I : OverloadMap) {
21100b57cec5SDimitry Andric     OverloadInfo &OI = I.second;
21110b57cec5SDimitry Andric 
21120b57cec5SDimitry Andric     OS << "case NEON::BI__builtin_neon_" << I.first << ": ";
21130b57cec5SDimitry Andric     OS << "mask = 0x" << Twine::utohexstr(OI.Mask) << "ULL";
21140b57cec5SDimitry Andric     if (OI.PtrArgNum >= 0)
21150b57cec5SDimitry Andric       OS << "; PtrArgNum = " << OI.PtrArgNum;
21160b57cec5SDimitry Andric     if (OI.HasConstPtr)
21170b57cec5SDimitry Andric       OS << "; HasConstPtr = true";
21180b57cec5SDimitry Andric     OS << "; break;\n";
21190b57cec5SDimitry Andric   }
21200b57cec5SDimitry Andric   OS << "#endif\n\n";
21210b57cec5SDimitry Andric }
21220b57cec5SDimitry Andric 
21230b57cec5SDimitry Andric void NeonEmitter::genIntrinsicRangeCheckCode(raw_ostream &OS,
21240b57cec5SDimitry Andric                                         SmallVectorImpl<Intrinsic *> &Defs) {
21250b57cec5SDimitry Andric   OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
21260b57cec5SDimitry Andric 
21270b57cec5SDimitry Andric   std::set<std::string> Emitted;
21280b57cec5SDimitry Andric 
21290b57cec5SDimitry Andric   for (auto *Def : Defs) {
21300b57cec5SDimitry Andric     if (Def->hasBody())
21310b57cec5SDimitry Andric       continue;
21320b57cec5SDimitry Andric     // Functions which do not have an immediate do not need to have range
21330b57cec5SDimitry Andric     // checking code emitted.
21340b57cec5SDimitry Andric     if (!Def->hasImmediate())
21350b57cec5SDimitry Andric       continue;
21360b57cec5SDimitry Andric     if (Emitted.find(Def->getMangledName()) != Emitted.end())
21370b57cec5SDimitry Andric       continue;
21380b57cec5SDimitry Andric 
21390b57cec5SDimitry Andric     std::string LowerBound, UpperBound;
21400b57cec5SDimitry Andric 
21410b57cec5SDimitry Andric     Record *R = Def->getRecord();
2142fe6060f1SDimitry Andric     if (R->getValueAsBit("isVXAR")) {
2143fe6060f1SDimitry Andric       //VXAR takes an immediate in the range [0, 63]
2144fe6060f1SDimitry Andric       LowerBound = "0";
2145fe6060f1SDimitry Andric       UpperBound = "63";
2146fe6060f1SDimitry Andric     } else if (R->getValueAsBit("isVCVT_N")) {
21470b57cec5SDimitry Andric       // VCVT between floating- and fixed-point values takes an immediate
21480b57cec5SDimitry Andric       // in the range [1, 32) for f32 or [1, 64) for f64 or [1, 16) for f16.
21490b57cec5SDimitry Andric       LowerBound = "1";
21500b57cec5SDimitry Andric 	  if (Def->getBaseType().getElementSizeInBits() == 16 ||
21510b57cec5SDimitry Andric 		  Def->getName().find('h') != std::string::npos)
21520b57cec5SDimitry Andric 		// VCVTh operating on FP16 intrinsics in range [1, 16)
21530b57cec5SDimitry Andric 		UpperBound = "15";
21540b57cec5SDimitry Andric 	  else if (Def->getBaseType().getElementSizeInBits() == 32)
21550b57cec5SDimitry Andric         UpperBound = "31";
21560b57cec5SDimitry Andric 	  else
21570b57cec5SDimitry Andric         UpperBound = "63";
21580b57cec5SDimitry Andric     } else if (R->getValueAsBit("isScalarShift")) {
21590b57cec5SDimitry Andric       // Right shifts have an 'r' in the name, left shifts do not. Convert
21600b57cec5SDimitry Andric       // instructions have the same bounds and right shifts.
21610b57cec5SDimitry Andric       if (Def->getName().find('r') != std::string::npos ||
21620b57cec5SDimitry Andric           Def->getName().find("cvt") != std::string::npos)
21630b57cec5SDimitry Andric         LowerBound = "1";
21640b57cec5SDimitry Andric 
21650b57cec5SDimitry Andric       UpperBound = utostr(Def->getReturnType().getElementSizeInBits() - 1);
21660b57cec5SDimitry Andric     } else if (R->getValueAsBit("isShift")) {
21670b57cec5SDimitry Andric       // Builtins which are overloaded by type will need to have their upper
21680b57cec5SDimitry Andric       // bound computed at Sema time based on the type constant.
21690b57cec5SDimitry Andric 
21700b57cec5SDimitry Andric       // Right shifts have an 'r' in the name, left shifts do not.
21710b57cec5SDimitry Andric       if (Def->getName().find('r') != std::string::npos)
21720b57cec5SDimitry Andric         LowerBound = "1";
21730b57cec5SDimitry Andric       UpperBound = "RFT(TV, true)";
21740b57cec5SDimitry Andric     } else if (Def->getClassKind(true) == ClassB) {
21750b57cec5SDimitry Andric       // ClassB intrinsics have a type (and hence lane number) that is only
21760b57cec5SDimitry Andric       // known at runtime.
21770b57cec5SDimitry Andric       if (R->getValueAsBit("isLaneQ"))
21780b57cec5SDimitry Andric         UpperBound = "RFT(TV, false, true)";
21790b57cec5SDimitry Andric       else
21800b57cec5SDimitry Andric         UpperBound = "RFT(TV, false, false)";
21810b57cec5SDimitry Andric     } else {
21820b57cec5SDimitry Andric       // The immediate generally refers to a lane in the preceding argument.
21830b57cec5SDimitry Andric       assert(Def->getImmediateIdx() > 0);
21840b57cec5SDimitry Andric       Type T = Def->getParamType(Def->getImmediateIdx() - 1);
21850b57cec5SDimitry Andric       UpperBound = utostr(T.getNumElements() - 1);
21860b57cec5SDimitry Andric     }
21870b57cec5SDimitry Andric 
21880b57cec5SDimitry Andric     // Calculate the index of the immediate that should be range checked.
21890b57cec5SDimitry Andric     unsigned Idx = Def->getNumParams();
21900b57cec5SDimitry Andric     if (Def->hasImmediate())
21910b57cec5SDimitry Andric       Idx = Def->getGeneratedParamIdx(Def->getImmediateIdx());
21920b57cec5SDimitry Andric 
21930b57cec5SDimitry Andric     OS << "case NEON::BI__builtin_neon_" << Def->getMangledName() << ": "
21940b57cec5SDimitry Andric        << "i = " << Idx << ";";
21950b57cec5SDimitry Andric     if (!LowerBound.empty())
21960b57cec5SDimitry Andric       OS << " l = " << LowerBound << ";";
21970b57cec5SDimitry Andric     if (!UpperBound.empty())
21980b57cec5SDimitry Andric       OS << " u = " << UpperBound << ";";
21990b57cec5SDimitry Andric     OS << " break;\n";
22000b57cec5SDimitry Andric 
22010b57cec5SDimitry Andric     Emitted.insert(Def->getMangledName());
22020b57cec5SDimitry Andric   }
22030b57cec5SDimitry Andric 
22040b57cec5SDimitry Andric   OS << "#endif\n\n";
22050b57cec5SDimitry Andric }
22060b57cec5SDimitry Andric 
22070b57cec5SDimitry Andric /// runHeader - Emit a file with sections defining:
22080b57cec5SDimitry Andric /// 1. the NEON section of BuiltinsARM.def and BuiltinsAArch64.def.
22090b57cec5SDimitry Andric /// 2. the SemaChecking code for the type overload checking.
22100b57cec5SDimitry Andric /// 3. the SemaChecking code for validation of intrinsic immediate arguments.
22110b57cec5SDimitry Andric void NeonEmitter::runHeader(raw_ostream &OS) {
22120b57cec5SDimitry Andric   std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
22130b57cec5SDimitry Andric 
22140b57cec5SDimitry Andric   SmallVector<Intrinsic *, 128> Defs;
22150b57cec5SDimitry Andric   for (auto *R : RV)
22160b57cec5SDimitry Andric     createIntrinsic(R, Defs);
22170b57cec5SDimitry Andric 
22180b57cec5SDimitry Andric   // Generate shared BuiltinsXXX.def
22190b57cec5SDimitry Andric   genBuiltinsDef(OS, Defs);
22200b57cec5SDimitry Andric 
22210b57cec5SDimitry Andric   // Generate ARM overloaded type checking code for SemaChecking.cpp
22220b57cec5SDimitry Andric   genOverloadTypeCheckCode(OS, Defs);
22230b57cec5SDimitry Andric 
22240b57cec5SDimitry Andric   // Generate ARM range checking code for shift/lane immediates.
22250b57cec5SDimitry Andric   genIntrinsicRangeCheckCode(OS, Defs);
22260b57cec5SDimitry Andric }
22270b57cec5SDimitry Andric 
22285ffd83dbSDimitry Andric static void emitNeonTypeDefs(const std::string& types, raw_ostream &OS) {
22295ffd83dbSDimitry Andric   std::string TypedefTypes(types);
22305ffd83dbSDimitry Andric   std::vector<TypeSpec> TDTypeVec = TypeSpec::fromTypeSpecs(TypedefTypes);
22315ffd83dbSDimitry Andric 
22325ffd83dbSDimitry Andric   // Emit vector typedefs.
22335ffd83dbSDimitry Andric   bool InIfdef = false;
22345ffd83dbSDimitry Andric   for (auto &TS : TDTypeVec) {
22355ffd83dbSDimitry Andric     bool IsA64 = false;
22365ffd83dbSDimitry Andric     Type T(TS, ".");
22375ffd83dbSDimitry Andric     if (T.isDouble())
22385ffd83dbSDimitry Andric       IsA64 = true;
22395ffd83dbSDimitry Andric 
22405ffd83dbSDimitry Andric     if (InIfdef && !IsA64) {
22415ffd83dbSDimitry Andric       OS << "#endif\n";
22425ffd83dbSDimitry Andric       InIfdef = false;
22435ffd83dbSDimitry Andric     }
22445ffd83dbSDimitry Andric     if (!InIfdef && IsA64) {
22455ffd83dbSDimitry Andric       OS << "#ifdef __aarch64__\n";
22465ffd83dbSDimitry Andric       InIfdef = true;
22475ffd83dbSDimitry Andric     }
22485ffd83dbSDimitry Andric 
22495ffd83dbSDimitry Andric     if (T.isPoly())
22505ffd83dbSDimitry Andric       OS << "typedef __attribute__((neon_polyvector_type(";
22515ffd83dbSDimitry Andric     else
22525ffd83dbSDimitry Andric       OS << "typedef __attribute__((neon_vector_type(";
22535ffd83dbSDimitry Andric 
22545ffd83dbSDimitry Andric     Type T2 = T;
22555ffd83dbSDimitry Andric     T2.makeScalar();
22565ffd83dbSDimitry Andric     OS << T.getNumElements() << "))) ";
22575ffd83dbSDimitry Andric     OS << T2.str();
22585ffd83dbSDimitry Andric     OS << " " << T.str() << ";\n";
22595ffd83dbSDimitry Andric   }
22605ffd83dbSDimitry Andric   if (InIfdef)
22615ffd83dbSDimitry Andric     OS << "#endif\n";
22625ffd83dbSDimitry Andric   OS << "\n";
22635ffd83dbSDimitry Andric 
22645ffd83dbSDimitry Andric   // Emit struct typedefs.
22655ffd83dbSDimitry Andric   InIfdef = false;
22665ffd83dbSDimitry Andric   for (unsigned NumMembers = 2; NumMembers <= 4; ++NumMembers) {
22675ffd83dbSDimitry Andric     for (auto &TS : TDTypeVec) {
22685ffd83dbSDimitry Andric       bool IsA64 = false;
22695ffd83dbSDimitry Andric       Type T(TS, ".");
22705ffd83dbSDimitry Andric       if (T.isDouble())
22715ffd83dbSDimitry Andric         IsA64 = true;
22725ffd83dbSDimitry Andric 
22735ffd83dbSDimitry Andric       if (InIfdef && !IsA64) {
22745ffd83dbSDimitry Andric         OS << "#endif\n";
22755ffd83dbSDimitry Andric         InIfdef = false;
22765ffd83dbSDimitry Andric       }
22775ffd83dbSDimitry Andric       if (!InIfdef && IsA64) {
22785ffd83dbSDimitry Andric         OS << "#ifdef __aarch64__\n";
22795ffd83dbSDimitry Andric         InIfdef = true;
22805ffd83dbSDimitry Andric       }
22815ffd83dbSDimitry Andric 
22825ffd83dbSDimitry Andric       const char Mods[] = { static_cast<char>('2' + (NumMembers - 2)), 0};
22835ffd83dbSDimitry Andric       Type VT(TS, Mods);
22845ffd83dbSDimitry Andric       OS << "typedef struct " << VT.str() << " {\n";
22855ffd83dbSDimitry Andric       OS << "  " << T.str() << " val";
22865ffd83dbSDimitry Andric       OS << "[" << NumMembers << "]";
22875ffd83dbSDimitry Andric       OS << ";\n} ";
22885ffd83dbSDimitry Andric       OS << VT.str() << ";\n";
22895ffd83dbSDimitry Andric       OS << "\n";
22905ffd83dbSDimitry Andric     }
22915ffd83dbSDimitry Andric   }
22925ffd83dbSDimitry Andric   if (InIfdef)
22935ffd83dbSDimitry Andric     OS << "#endif\n";
22945ffd83dbSDimitry Andric }
22955ffd83dbSDimitry Andric 
22960b57cec5SDimitry Andric /// run - Read the records in arm_neon.td and output arm_neon.h.  arm_neon.h
22970b57cec5SDimitry Andric /// is comprised of type definitions and function declarations.
22980b57cec5SDimitry Andric void NeonEmitter::run(raw_ostream &OS) {
22990b57cec5SDimitry Andric   OS << "/*===---- arm_neon.h - ARM Neon intrinsics "
23000b57cec5SDimitry Andric         "------------------------------"
23010b57cec5SDimitry Andric         "---===\n"
23020b57cec5SDimitry Andric         " *\n"
23030b57cec5SDimitry Andric         " * Permission is hereby granted, free of charge, to any person "
23040b57cec5SDimitry Andric         "obtaining "
23050b57cec5SDimitry Andric         "a copy\n"
23060b57cec5SDimitry Andric         " * of this software and associated documentation files (the "
23070b57cec5SDimitry Andric         "\"Software\"),"
23080b57cec5SDimitry Andric         " to deal\n"
23090b57cec5SDimitry Andric         " * in the Software without restriction, including without limitation "
23100b57cec5SDimitry Andric         "the "
23110b57cec5SDimitry Andric         "rights\n"
23120b57cec5SDimitry Andric         " * to use, copy, modify, merge, publish, distribute, sublicense, "
23130b57cec5SDimitry Andric         "and/or sell\n"
23140b57cec5SDimitry Andric         " * copies of the Software, and to permit persons to whom the Software "
23150b57cec5SDimitry Andric         "is\n"
23160b57cec5SDimitry Andric         " * furnished to do so, subject to the following conditions:\n"
23170b57cec5SDimitry Andric         " *\n"
23180b57cec5SDimitry Andric         " * The above copyright notice and this permission notice shall be "
23190b57cec5SDimitry Andric         "included in\n"
23200b57cec5SDimitry Andric         " * all copies or substantial portions of the Software.\n"
23210b57cec5SDimitry Andric         " *\n"
23220b57cec5SDimitry Andric         " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, "
23230b57cec5SDimitry Andric         "EXPRESS OR\n"
23240b57cec5SDimitry Andric         " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
23250b57cec5SDimitry Andric         "MERCHANTABILITY,\n"
23260b57cec5SDimitry Andric         " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT "
23270b57cec5SDimitry Andric         "SHALL THE\n"
23280b57cec5SDimitry Andric         " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR "
23290b57cec5SDimitry Andric         "OTHER\n"
23300b57cec5SDimitry Andric         " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, "
23310b57cec5SDimitry Andric         "ARISING FROM,\n"
23320b57cec5SDimitry Andric         " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER "
23330b57cec5SDimitry Andric         "DEALINGS IN\n"
23340b57cec5SDimitry Andric         " * THE SOFTWARE.\n"
23350b57cec5SDimitry Andric         " *\n"
23360b57cec5SDimitry Andric         " *===-----------------------------------------------------------------"
23370b57cec5SDimitry Andric         "---"
23380b57cec5SDimitry Andric         "---===\n"
23390b57cec5SDimitry Andric         " */\n\n";
23400b57cec5SDimitry Andric 
23410b57cec5SDimitry Andric   OS << "#ifndef __ARM_NEON_H\n";
23420b57cec5SDimitry Andric   OS << "#define __ARM_NEON_H\n\n";
23430b57cec5SDimitry Andric 
23445ffd83dbSDimitry Andric   OS << "#ifndef __ARM_FP\n";
23455ffd83dbSDimitry Andric   OS << "#error \"NEON intrinsics not available with the soft-float ABI. "
23465ffd83dbSDimitry Andric         "Please use -mfloat-abi=softfp or -mfloat-abi=hard\"\n";
23475ffd83dbSDimitry Andric   OS << "#else\n\n";
23485ffd83dbSDimitry Andric 
23490b57cec5SDimitry Andric   OS << "#if !defined(__ARM_NEON)\n";
23500b57cec5SDimitry Andric   OS << "#error \"NEON support not enabled\"\n";
23515ffd83dbSDimitry Andric   OS << "#else\n\n";
23520b57cec5SDimitry Andric 
23530b57cec5SDimitry Andric   OS << "#include <stdint.h>\n\n";
23540b57cec5SDimitry Andric 
23555ffd83dbSDimitry Andric   OS << "#include <arm_bf16.h>\n";
23565ffd83dbSDimitry Andric   OS << "typedef __bf16 bfloat16_t;\n";
23575ffd83dbSDimitry Andric 
23580b57cec5SDimitry Andric   // Emit NEON-specific scalar typedefs.
23590b57cec5SDimitry Andric   OS << "typedef float float32_t;\n";
23600b57cec5SDimitry Andric   OS << "typedef __fp16 float16_t;\n";
23610b57cec5SDimitry Andric 
23620b57cec5SDimitry Andric   OS << "#ifdef __aarch64__\n";
23630b57cec5SDimitry Andric   OS << "typedef double float64_t;\n";
23640b57cec5SDimitry Andric   OS << "#endif\n\n";
23650b57cec5SDimitry Andric 
23660b57cec5SDimitry Andric   // For now, signedness of polynomial types depends on target
23670b57cec5SDimitry Andric   OS << "#ifdef __aarch64__\n";
23680b57cec5SDimitry Andric   OS << "typedef uint8_t poly8_t;\n";
23690b57cec5SDimitry Andric   OS << "typedef uint16_t poly16_t;\n";
23700b57cec5SDimitry Andric   OS << "typedef uint64_t poly64_t;\n";
23710b57cec5SDimitry Andric   OS << "typedef __uint128_t poly128_t;\n";
23720b57cec5SDimitry Andric   OS << "#else\n";
23730b57cec5SDimitry Andric   OS << "typedef int8_t poly8_t;\n";
23740b57cec5SDimitry Andric   OS << "typedef int16_t poly16_t;\n";
23755ffd83dbSDimitry Andric   OS << "typedef int64_t poly64_t;\n";
23760b57cec5SDimitry Andric   OS << "#endif\n";
23770b57cec5SDimitry Andric 
23785ffd83dbSDimitry Andric   emitNeonTypeDefs("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfdQdPcQPcPsQPsPlQPl", OS);
23790b57cec5SDimitry Andric 
23805ffd83dbSDimitry Andric   emitNeonTypeDefs("bQb", OS);
23810b57cec5SDimitry Andric 
23820b57cec5SDimitry Andric   OS << "#define __ai static __inline__ __attribute__((__always_inline__, "
23830b57cec5SDimitry Andric         "__nodebug__))\n\n";
23840b57cec5SDimitry Andric 
23850b57cec5SDimitry Andric   SmallVector<Intrinsic *, 128> Defs;
23860b57cec5SDimitry Andric   std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
23870b57cec5SDimitry Andric   for (auto *R : RV)
23880b57cec5SDimitry Andric     createIntrinsic(R, Defs);
23890b57cec5SDimitry Andric 
23900b57cec5SDimitry Andric   for (auto *I : Defs)
23910b57cec5SDimitry Andric     I->indexBody();
23920b57cec5SDimitry Andric 
2393a7dea167SDimitry Andric   llvm::stable_sort(Defs, llvm::deref<std::less<>>());
23940b57cec5SDimitry Andric 
23950b57cec5SDimitry Andric   // Only emit a def when its requirements have been met.
23960b57cec5SDimitry Andric   // FIXME: This loop could be made faster, but it's fast enough for now.
23970b57cec5SDimitry Andric   bool MadeProgress = true;
23980b57cec5SDimitry Andric   std::string InGuard;
23990b57cec5SDimitry Andric   while (!Defs.empty() && MadeProgress) {
24000b57cec5SDimitry Andric     MadeProgress = false;
24010b57cec5SDimitry Andric 
24020b57cec5SDimitry Andric     for (SmallVector<Intrinsic *, 128>::iterator I = Defs.begin();
24030b57cec5SDimitry Andric          I != Defs.end(); /*No step*/) {
24040b57cec5SDimitry Andric       bool DependenciesSatisfied = true;
24050b57cec5SDimitry Andric       for (auto *II : (*I)->getDependencies()) {
24060b57cec5SDimitry Andric         if (llvm::is_contained(Defs, II))
24070b57cec5SDimitry Andric           DependenciesSatisfied = false;
24080b57cec5SDimitry Andric       }
24090b57cec5SDimitry Andric       if (!DependenciesSatisfied) {
24100b57cec5SDimitry Andric         // Try the next one.
24110b57cec5SDimitry Andric         ++I;
24120b57cec5SDimitry Andric         continue;
24130b57cec5SDimitry Andric       }
24140b57cec5SDimitry Andric 
24150b57cec5SDimitry Andric       // Emit #endif/#if pair if needed.
2416*bdd1243dSDimitry Andric       if ((*I)->getArchGuard() != InGuard) {
24170b57cec5SDimitry Andric         if (!InGuard.empty())
24180b57cec5SDimitry Andric           OS << "#endif\n";
2419*bdd1243dSDimitry Andric         InGuard = (*I)->getArchGuard();
24200b57cec5SDimitry Andric         if (!InGuard.empty())
24210b57cec5SDimitry Andric           OS << "#if " << InGuard << "\n";
24220b57cec5SDimitry Andric       }
24230b57cec5SDimitry Andric 
24240b57cec5SDimitry Andric       // Actually generate the intrinsic code.
24250b57cec5SDimitry Andric       OS << (*I)->generate();
24260b57cec5SDimitry Andric 
24270b57cec5SDimitry Andric       MadeProgress = true;
24280b57cec5SDimitry Andric       I = Defs.erase(I);
24290b57cec5SDimitry Andric     }
24300b57cec5SDimitry Andric   }
24310b57cec5SDimitry Andric   assert(Defs.empty() && "Some requirements were not satisfied!");
24320b57cec5SDimitry Andric   if (!InGuard.empty())
24330b57cec5SDimitry Andric     OS << "#endif\n";
24340b57cec5SDimitry Andric 
24350b57cec5SDimitry Andric   OS << "\n";
24360b57cec5SDimitry Andric   OS << "#undef __ai\n\n";
24375ffd83dbSDimitry Andric   OS << "#endif /* if !defined(__ARM_NEON) */\n";
24385ffd83dbSDimitry Andric   OS << "#endif /* ifndef __ARM_FP */\n";
24390b57cec5SDimitry Andric   OS << "#endif /* __ARM_NEON_H */\n";
24400b57cec5SDimitry Andric }
24410b57cec5SDimitry Andric 
24420b57cec5SDimitry Andric /// run - Read the records in arm_fp16.td and output arm_fp16.h.  arm_fp16.h
24430b57cec5SDimitry Andric /// is comprised of type definitions and function declarations.
24440b57cec5SDimitry Andric void NeonEmitter::runFP16(raw_ostream &OS) {
24450b57cec5SDimitry Andric   OS << "/*===---- arm_fp16.h - ARM FP16 intrinsics "
24460b57cec5SDimitry Andric         "------------------------------"
24470b57cec5SDimitry Andric         "---===\n"
24480b57cec5SDimitry Andric         " *\n"
24490b57cec5SDimitry Andric         " * Permission is hereby granted, free of charge, to any person "
24500b57cec5SDimitry Andric         "obtaining a copy\n"
24510b57cec5SDimitry Andric         " * of this software and associated documentation files (the "
24520b57cec5SDimitry Andric 				"\"Software\"), to deal\n"
24530b57cec5SDimitry Andric         " * in the Software without restriction, including without limitation "
24540b57cec5SDimitry Andric 				"the rights\n"
24550b57cec5SDimitry Andric         " * to use, copy, modify, merge, publish, distribute, sublicense, "
24560b57cec5SDimitry Andric 				"and/or sell\n"
24570b57cec5SDimitry Andric         " * copies of the Software, and to permit persons to whom the Software "
24580b57cec5SDimitry Andric 				"is\n"
24590b57cec5SDimitry Andric         " * furnished to do so, subject to the following conditions:\n"
24600b57cec5SDimitry Andric         " *\n"
24610b57cec5SDimitry Andric         " * The above copyright notice and this permission notice shall be "
24620b57cec5SDimitry Andric         "included in\n"
24630b57cec5SDimitry Andric         " * all copies or substantial portions of the Software.\n"
24640b57cec5SDimitry Andric         " *\n"
24650b57cec5SDimitry Andric         " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, "
24660b57cec5SDimitry Andric         "EXPRESS OR\n"
24670b57cec5SDimitry Andric         " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
24680b57cec5SDimitry Andric         "MERCHANTABILITY,\n"
24690b57cec5SDimitry Andric         " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT "
24700b57cec5SDimitry Andric         "SHALL THE\n"
24710b57cec5SDimitry Andric         " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR "
24720b57cec5SDimitry Andric         "OTHER\n"
24730b57cec5SDimitry Andric         " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, "
24740b57cec5SDimitry Andric         "ARISING FROM,\n"
24750b57cec5SDimitry Andric         " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER "
24760b57cec5SDimitry Andric         "DEALINGS IN\n"
24770b57cec5SDimitry Andric         " * THE SOFTWARE.\n"
24780b57cec5SDimitry Andric         " *\n"
24790b57cec5SDimitry Andric         " *===-----------------------------------------------------------------"
24800b57cec5SDimitry Andric         "---"
24810b57cec5SDimitry Andric         "---===\n"
24820b57cec5SDimitry Andric         " */\n\n";
24830b57cec5SDimitry Andric 
24840b57cec5SDimitry Andric   OS << "#ifndef __ARM_FP16_H\n";
24850b57cec5SDimitry Andric   OS << "#define __ARM_FP16_H\n\n";
24860b57cec5SDimitry Andric 
24870b57cec5SDimitry Andric   OS << "#include <stdint.h>\n\n";
24880b57cec5SDimitry Andric 
24890b57cec5SDimitry Andric   OS << "typedef __fp16 float16_t;\n";
24900b57cec5SDimitry Andric 
24910b57cec5SDimitry Andric   OS << "#define __ai static __inline__ __attribute__((__always_inline__, "
24920b57cec5SDimitry Andric         "__nodebug__))\n\n";
24930b57cec5SDimitry Andric 
24940b57cec5SDimitry Andric   SmallVector<Intrinsic *, 128> Defs;
24950b57cec5SDimitry Andric   std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
24960b57cec5SDimitry Andric   for (auto *R : RV)
24970b57cec5SDimitry Andric     createIntrinsic(R, Defs);
24980b57cec5SDimitry Andric 
24990b57cec5SDimitry Andric   for (auto *I : Defs)
25000b57cec5SDimitry Andric     I->indexBody();
25010b57cec5SDimitry Andric 
2502a7dea167SDimitry Andric   llvm::stable_sort(Defs, llvm::deref<std::less<>>());
25030b57cec5SDimitry Andric 
25040b57cec5SDimitry Andric   // Only emit a def when its requirements have been met.
25050b57cec5SDimitry Andric   // FIXME: This loop could be made faster, but it's fast enough for now.
25060b57cec5SDimitry Andric   bool MadeProgress = true;
25070b57cec5SDimitry Andric   std::string InGuard;
25080b57cec5SDimitry Andric   while (!Defs.empty() && MadeProgress) {
25090b57cec5SDimitry Andric     MadeProgress = false;
25100b57cec5SDimitry Andric 
25110b57cec5SDimitry Andric     for (SmallVector<Intrinsic *, 128>::iterator I = Defs.begin();
25120b57cec5SDimitry Andric          I != Defs.end(); /*No step*/) {
25130b57cec5SDimitry Andric       bool DependenciesSatisfied = true;
25140b57cec5SDimitry Andric       for (auto *II : (*I)->getDependencies()) {
25150b57cec5SDimitry Andric         if (llvm::is_contained(Defs, II))
25160b57cec5SDimitry Andric           DependenciesSatisfied = false;
25170b57cec5SDimitry Andric       }
25180b57cec5SDimitry Andric       if (!DependenciesSatisfied) {
25190b57cec5SDimitry Andric         // Try the next one.
25200b57cec5SDimitry Andric         ++I;
25210b57cec5SDimitry Andric         continue;
25220b57cec5SDimitry Andric       }
25230b57cec5SDimitry Andric 
25240b57cec5SDimitry Andric       // Emit #endif/#if pair if needed.
2525*bdd1243dSDimitry Andric       if ((*I)->getArchGuard() != InGuard) {
25260b57cec5SDimitry Andric         if (!InGuard.empty())
25270b57cec5SDimitry Andric           OS << "#endif\n";
2528*bdd1243dSDimitry Andric         InGuard = (*I)->getArchGuard();
25290b57cec5SDimitry Andric         if (!InGuard.empty())
25300b57cec5SDimitry Andric           OS << "#if " << InGuard << "\n";
25310b57cec5SDimitry Andric       }
25320b57cec5SDimitry Andric 
25330b57cec5SDimitry Andric       // Actually generate the intrinsic code.
25340b57cec5SDimitry Andric       OS << (*I)->generate();
25350b57cec5SDimitry Andric 
25360b57cec5SDimitry Andric       MadeProgress = true;
25370b57cec5SDimitry Andric       I = Defs.erase(I);
25380b57cec5SDimitry Andric     }
25390b57cec5SDimitry Andric   }
25400b57cec5SDimitry Andric   assert(Defs.empty() && "Some requirements were not satisfied!");
25410b57cec5SDimitry Andric   if (!InGuard.empty())
25420b57cec5SDimitry Andric     OS << "#endif\n";
25430b57cec5SDimitry Andric 
25440b57cec5SDimitry Andric   OS << "\n";
25450b57cec5SDimitry Andric   OS << "#undef __ai\n\n";
25460b57cec5SDimitry Andric   OS << "#endif /* __ARM_FP16_H */\n";
25470b57cec5SDimitry Andric }
25480b57cec5SDimitry Andric 
25495ffd83dbSDimitry Andric void NeonEmitter::runBF16(raw_ostream &OS) {
25505ffd83dbSDimitry Andric   OS << "/*===---- arm_bf16.h - ARM BF16 intrinsics "
25515ffd83dbSDimitry Andric         "-----------------------------------===\n"
25525ffd83dbSDimitry Andric         " *\n"
25535ffd83dbSDimitry Andric         " *\n"
25545ffd83dbSDimitry Andric         " * Part of the LLVM Project, under the Apache License v2.0 with LLVM "
25555ffd83dbSDimitry Andric         "Exceptions.\n"
25565ffd83dbSDimitry Andric         " * See https://llvm.org/LICENSE.txt for license information.\n"
25575ffd83dbSDimitry Andric         " * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n"
25585ffd83dbSDimitry Andric         " *\n"
25595ffd83dbSDimitry Andric         " *===-----------------------------------------------------------------"
25605ffd83dbSDimitry Andric         "------===\n"
25615ffd83dbSDimitry Andric         " */\n\n";
25625ffd83dbSDimitry Andric 
25635ffd83dbSDimitry Andric   OS << "#ifndef __ARM_BF16_H\n";
25645ffd83dbSDimitry Andric   OS << "#define __ARM_BF16_H\n\n";
25655ffd83dbSDimitry Andric 
25665ffd83dbSDimitry Andric   OS << "typedef __bf16 bfloat16_t;\n";
25675ffd83dbSDimitry Andric 
25685ffd83dbSDimitry Andric   OS << "#define __ai static __inline__ __attribute__((__always_inline__, "
25695ffd83dbSDimitry Andric         "__nodebug__))\n\n";
25705ffd83dbSDimitry Andric 
25715ffd83dbSDimitry Andric   SmallVector<Intrinsic *, 128> Defs;
25725ffd83dbSDimitry Andric   std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
25735ffd83dbSDimitry Andric   for (auto *R : RV)
25745ffd83dbSDimitry Andric     createIntrinsic(R, Defs);
25755ffd83dbSDimitry Andric 
25765ffd83dbSDimitry Andric   for (auto *I : Defs)
25775ffd83dbSDimitry Andric     I->indexBody();
25785ffd83dbSDimitry Andric 
25795ffd83dbSDimitry Andric   llvm::stable_sort(Defs, llvm::deref<std::less<>>());
25805ffd83dbSDimitry Andric 
25815ffd83dbSDimitry Andric   // Only emit a def when its requirements have been met.
25825ffd83dbSDimitry Andric   // FIXME: This loop could be made faster, but it's fast enough for now.
25835ffd83dbSDimitry Andric   bool MadeProgress = true;
25845ffd83dbSDimitry Andric   std::string InGuard;
25855ffd83dbSDimitry Andric   while (!Defs.empty() && MadeProgress) {
25865ffd83dbSDimitry Andric     MadeProgress = false;
25875ffd83dbSDimitry Andric 
25885ffd83dbSDimitry Andric     for (SmallVector<Intrinsic *, 128>::iterator I = Defs.begin();
25895ffd83dbSDimitry Andric          I != Defs.end(); /*No step*/) {
25905ffd83dbSDimitry Andric       bool DependenciesSatisfied = true;
25915ffd83dbSDimitry Andric       for (auto *II : (*I)->getDependencies()) {
25925ffd83dbSDimitry Andric         if (llvm::is_contained(Defs, II))
25935ffd83dbSDimitry Andric           DependenciesSatisfied = false;
25945ffd83dbSDimitry Andric       }
25955ffd83dbSDimitry Andric       if (!DependenciesSatisfied) {
25965ffd83dbSDimitry Andric         // Try the next one.
25975ffd83dbSDimitry Andric         ++I;
25985ffd83dbSDimitry Andric         continue;
25995ffd83dbSDimitry Andric       }
26005ffd83dbSDimitry Andric 
26015ffd83dbSDimitry Andric       // Emit #endif/#if pair if needed.
2602*bdd1243dSDimitry Andric       if ((*I)->getArchGuard() != InGuard) {
26035ffd83dbSDimitry Andric         if (!InGuard.empty())
26045ffd83dbSDimitry Andric           OS << "#endif\n";
2605*bdd1243dSDimitry Andric         InGuard = (*I)->getArchGuard();
26065ffd83dbSDimitry Andric         if (!InGuard.empty())
26075ffd83dbSDimitry Andric           OS << "#if " << InGuard << "\n";
26085ffd83dbSDimitry Andric       }
26095ffd83dbSDimitry Andric 
26105ffd83dbSDimitry Andric       // Actually generate the intrinsic code.
26115ffd83dbSDimitry Andric       OS << (*I)->generate();
26125ffd83dbSDimitry Andric 
26135ffd83dbSDimitry Andric       MadeProgress = true;
26145ffd83dbSDimitry Andric       I = Defs.erase(I);
26155ffd83dbSDimitry Andric     }
26165ffd83dbSDimitry Andric   }
26175ffd83dbSDimitry Andric   assert(Defs.empty() && "Some requirements were not satisfied!");
26185ffd83dbSDimitry Andric   if (!InGuard.empty())
26195ffd83dbSDimitry Andric     OS << "#endif\n";
26205ffd83dbSDimitry Andric 
26215ffd83dbSDimitry Andric   OS << "\n";
26225ffd83dbSDimitry Andric   OS << "#undef __ai\n\n";
26235ffd83dbSDimitry Andric 
26245ffd83dbSDimitry Andric   OS << "#endif\n";
26255ffd83dbSDimitry Andric }
26265ffd83dbSDimitry Andric 
2627a7dea167SDimitry Andric void clang::EmitNeon(RecordKeeper &Records, raw_ostream &OS) {
26280b57cec5SDimitry Andric   NeonEmitter(Records).run(OS);
26290b57cec5SDimitry Andric }
26300b57cec5SDimitry Andric 
2631a7dea167SDimitry Andric void clang::EmitFP16(RecordKeeper &Records, raw_ostream &OS) {
26320b57cec5SDimitry Andric   NeonEmitter(Records).runFP16(OS);
26330b57cec5SDimitry Andric }
26340b57cec5SDimitry Andric 
26355ffd83dbSDimitry Andric void clang::EmitBF16(RecordKeeper &Records, raw_ostream &OS) {
26365ffd83dbSDimitry Andric   NeonEmitter(Records).runBF16(OS);
26375ffd83dbSDimitry Andric }
26385ffd83dbSDimitry Andric 
2639a7dea167SDimitry Andric void clang::EmitNeonSema(RecordKeeper &Records, raw_ostream &OS) {
26400b57cec5SDimitry Andric   NeonEmitter(Records).runHeader(OS);
26410b57cec5SDimitry Andric }
26420b57cec5SDimitry Andric 
2643a7dea167SDimitry Andric void clang::EmitNeonTest(RecordKeeper &Records, raw_ostream &OS) {
26440b57cec5SDimitry Andric   llvm_unreachable("Neon test generation no longer implemented!");
26450b57cec5SDimitry Andric }
2646