xref: /freebsd/contrib/llvm-project/clang/utils/TableGen/NeonEmitter.cpp (revision 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623)
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/None.h"
305ffd83dbSDimitry Andric #include "llvm/ADT/Optional.h"
310b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
325ffd83dbSDimitry Andric #include "llvm/ADT/SmallVector.h"
330b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h"
340b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
350b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
360b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
370b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
380b57cec5SDimitry Andric #include "llvm/TableGen/Error.h"
390b57cec5SDimitry Andric #include "llvm/TableGen/Record.h"
400b57cec5SDimitry Andric #include "llvm/TableGen/SetTheory.h"
410b57cec5SDimitry Andric #include <algorithm>
420b57cec5SDimitry Andric #include <cassert>
430b57cec5SDimitry Andric #include <cctype>
440b57cec5SDimitry Andric #include <cstddef>
450b57cec5SDimitry Andric #include <cstdint>
460b57cec5SDimitry Andric #include <deque>
470b57cec5SDimitry Andric #include <map>
480b57cec5SDimitry Andric #include <set>
490b57cec5SDimitry Andric #include <sstream>
500b57cec5SDimitry Andric #include <string>
510b57cec5SDimitry Andric #include <utility>
520b57cec5SDimitry Andric #include <vector>
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric using namespace llvm;
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric namespace {
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric // While globals are generally bad, this one allows us to perform assertions
590b57cec5SDimitry Andric // liberally and somehow still trace them back to the def they indirectly
600b57cec5SDimitry Andric // came from.
610b57cec5SDimitry Andric static Record *CurrentRecord = nullptr;
620b57cec5SDimitry Andric static void assert_with_loc(bool Assertion, const std::string &Str) {
630b57cec5SDimitry Andric   if (!Assertion) {
640b57cec5SDimitry Andric     if (CurrentRecord)
650b57cec5SDimitry Andric       PrintFatalError(CurrentRecord->getLoc(), Str);
660b57cec5SDimitry Andric     else
670b57cec5SDimitry Andric       PrintFatalError(Str);
680b57cec5SDimitry Andric   }
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric enum ClassKind {
720b57cec5SDimitry Andric   ClassNone,
730b57cec5SDimitry Andric   ClassI,     // generic integer instruction, e.g., "i8" suffix
740b57cec5SDimitry Andric   ClassS,     // signed/unsigned/poly, e.g., "s8", "u8" or "p8" suffix
750b57cec5SDimitry Andric   ClassW,     // width-specific instruction, e.g., "8" suffix
760b57cec5SDimitry Andric   ClassB,     // bitcast arguments with enum argument to specify type
770b57cec5SDimitry Andric   ClassL,     // Logical instructions which are op instructions
780b57cec5SDimitry Andric               // but we need to not emit any suffix for in our
790b57cec5SDimitry Andric               // tests.
800b57cec5SDimitry Andric   ClassNoTest // Instructions which we do not test since they are
810b57cec5SDimitry Andric               // not TRUE instructions.
820b57cec5SDimitry Andric };
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric /// NeonTypeFlags - Flags to identify the types for overloaded Neon
850b57cec5SDimitry Andric /// builtins.  These must be kept in sync with the flags in
860b57cec5SDimitry Andric /// include/clang/Basic/TargetBuiltins.h.
870b57cec5SDimitry Andric namespace NeonTypeFlags {
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric enum { EltTypeMask = 0xf, UnsignedFlag = 0x10, QuadFlag = 0x20 };
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric enum EltType {
920b57cec5SDimitry Andric   Int8,
930b57cec5SDimitry Andric   Int16,
940b57cec5SDimitry Andric   Int32,
950b57cec5SDimitry Andric   Int64,
960b57cec5SDimitry Andric   Poly8,
970b57cec5SDimitry Andric   Poly16,
980b57cec5SDimitry Andric   Poly64,
990b57cec5SDimitry Andric   Poly128,
1000b57cec5SDimitry Andric   Float16,
1010b57cec5SDimitry Andric   Float32,
1025ffd83dbSDimitry Andric   Float64,
1035ffd83dbSDimitry Andric   BFloat16
1040b57cec5SDimitry Andric };
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric } // end namespace NeonTypeFlags
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric class NeonEmitter;
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1110b57cec5SDimitry Andric // TypeSpec
1120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric /// A TypeSpec is just a simple wrapper around a string, but gets its own type
1150b57cec5SDimitry Andric /// for strong typing purposes.
1160b57cec5SDimitry Andric ///
1170b57cec5SDimitry Andric /// A TypeSpec can be used to create a type.
1180b57cec5SDimitry Andric class TypeSpec : public std::string {
1190b57cec5SDimitry Andric public:
1200b57cec5SDimitry Andric   static std::vector<TypeSpec> fromTypeSpecs(StringRef Str) {
1210b57cec5SDimitry Andric     std::vector<TypeSpec> Ret;
1220b57cec5SDimitry Andric     TypeSpec Acc;
1230b57cec5SDimitry Andric     for (char I : Str.str()) {
1240b57cec5SDimitry Andric       if (islower(I)) {
1250b57cec5SDimitry Andric         Acc.push_back(I);
1260b57cec5SDimitry Andric         Ret.push_back(TypeSpec(Acc));
1270b57cec5SDimitry Andric         Acc.clear();
1280b57cec5SDimitry Andric       } else {
1290b57cec5SDimitry Andric         Acc.push_back(I);
1300b57cec5SDimitry Andric       }
1310b57cec5SDimitry Andric     }
1320b57cec5SDimitry Andric     return Ret;
1330b57cec5SDimitry Andric   }
1340b57cec5SDimitry Andric };
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1370b57cec5SDimitry Andric // Type
1380b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric /// A Type. Not much more to say here.
1410b57cec5SDimitry Andric class Type {
1420b57cec5SDimitry Andric private:
1430b57cec5SDimitry Andric   TypeSpec TS;
1440b57cec5SDimitry Andric 
145480093f4SDimitry Andric   enum TypeKind {
146480093f4SDimitry Andric     Void,
147480093f4SDimitry Andric     Float,
148480093f4SDimitry Andric     SInt,
149480093f4SDimitry Andric     UInt,
150480093f4SDimitry Andric     Poly,
1515ffd83dbSDimitry Andric     BFloat16,
152480093f4SDimitry Andric   };
153480093f4SDimitry Andric   TypeKind Kind;
154480093f4SDimitry Andric   bool Immediate, Constant, Pointer;
1550b57cec5SDimitry Andric   // ScalarForMangling and NoManglingQ are really not suited to live here as
1560b57cec5SDimitry Andric   // they are not related to the type. But they live in the TypeSpec (not the
1570b57cec5SDimitry Andric   // prototype), so this is really the only place to store them.
1580b57cec5SDimitry Andric   bool ScalarForMangling, NoManglingQ;
1590b57cec5SDimitry Andric   unsigned Bitwidth, ElementBitwidth, NumVectors;
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric public:
1620b57cec5SDimitry Andric   Type()
163480093f4SDimitry Andric       : Kind(Void), Immediate(false), Constant(false),
164480093f4SDimitry Andric         Pointer(false), ScalarForMangling(false), NoManglingQ(false),
165480093f4SDimitry Andric         Bitwidth(0), ElementBitwidth(0), NumVectors(0) {}
1660b57cec5SDimitry Andric 
167480093f4SDimitry Andric   Type(TypeSpec TS, StringRef CharMods)
168480093f4SDimitry Andric       : TS(std::move(TS)), Kind(Void), Immediate(false),
169480093f4SDimitry Andric         Constant(false), Pointer(false), ScalarForMangling(false),
170480093f4SDimitry Andric         NoManglingQ(false), Bitwidth(0), ElementBitwidth(0), NumVectors(0) {
171480093f4SDimitry Andric     applyModifiers(CharMods);
1720b57cec5SDimitry Andric   }
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric   /// Returns a type representing "void".
1750b57cec5SDimitry Andric   static Type getVoid() { return Type(); }
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric   bool operator==(const Type &Other) const { return str() == Other.str(); }
1780b57cec5SDimitry Andric   bool operator!=(const Type &Other) const { return !operator==(Other); }
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric   //
1810b57cec5SDimitry Andric   // Query functions
1820b57cec5SDimitry Andric   //
1830b57cec5SDimitry Andric   bool isScalarForMangling() const { return ScalarForMangling; }
1840b57cec5SDimitry Andric   bool noManglingQ() const { return NoManglingQ; }
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric   bool isPointer() const { return Pointer; }
187480093f4SDimitry Andric   bool isValue() const { return !isVoid() && !isPointer(); }
188480093f4SDimitry Andric   bool isScalar() const { return isValue() && NumVectors == 0; }
189480093f4SDimitry Andric   bool isVector() const { return isValue() && NumVectors > 0; }
190480093f4SDimitry Andric   bool isConstPointer() const { return Constant; }
191480093f4SDimitry Andric   bool isFloating() const { return Kind == Float; }
192480093f4SDimitry Andric   bool isInteger() const { return Kind == SInt || Kind == UInt; }
193480093f4SDimitry Andric   bool isPoly() const { return Kind == Poly; }
194480093f4SDimitry Andric   bool isSigned() const { return Kind == SInt; }
1950b57cec5SDimitry Andric   bool isImmediate() const { return Immediate; }
196480093f4SDimitry Andric   bool isFloat() const { return isFloating() && ElementBitwidth == 32; }
197480093f4SDimitry Andric   bool isDouble() const { return isFloating() && ElementBitwidth == 64; }
198480093f4SDimitry Andric   bool isHalf() const { return isFloating() && ElementBitwidth == 16; }
1990b57cec5SDimitry Andric   bool isChar() const { return ElementBitwidth == 8; }
200480093f4SDimitry Andric   bool isShort() const { return isInteger() && ElementBitwidth == 16; }
201480093f4SDimitry Andric   bool isInt() const { return isInteger() && ElementBitwidth == 32; }
202480093f4SDimitry Andric   bool isLong() const { return isInteger() && ElementBitwidth == 64; }
203480093f4SDimitry Andric   bool isVoid() const { return Kind == Void; }
2045ffd83dbSDimitry Andric   bool isBFloat16() const { return Kind == BFloat16; }
2050b57cec5SDimitry Andric   unsigned getNumElements() const { return Bitwidth / ElementBitwidth; }
2060b57cec5SDimitry Andric   unsigned getSizeInBits() const { return Bitwidth; }
2070b57cec5SDimitry Andric   unsigned getElementSizeInBits() const { return ElementBitwidth; }
2080b57cec5SDimitry Andric   unsigned getNumVectors() const { return NumVectors; }
2090b57cec5SDimitry Andric 
2100b57cec5SDimitry Andric   //
2110b57cec5SDimitry Andric   // Mutator functions
2120b57cec5SDimitry Andric   //
213480093f4SDimitry Andric   void makeUnsigned() {
214480093f4SDimitry Andric     assert(!isVoid() && "not a potentially signed type");
215480093f4SDimitry Andric     Kind = UInt;
216480093f4SDimitry Andric   }
217480093f4SDimitry Andric   void makeSigned() {
218480093f4SDimitry Andric     assert(!isVoid() && "not a potentially signed type");
219480093f4SDimitry Andric     Kind = SInt;
220480093f4SDimitry Andric   }
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric   void makeInteger(unsigned ElemWidth, bool Sign) {
223480093f4SDimitry Andric     assert(!isVoid() && "converting void to int probably not useful");
224480093f4SDimitry Andric     Kind = Sign ? SInt : UInt;
2250b57cec5SDimitry Andric     Immediate = false;
2260b57cec5SDimitry Andric     ElementBitwidth = ElemWidth;
2270b57cec5SDimitry Andric   }
2280b57cec5SDimitry Andric 
2290b57cec5SDimitry Andric   void makeImmediate(unsigned ElemWidth) {
230480093f4SDimitry Andric     Kind = SInt;
2310b57cec5SDimitry Andric     Immediate = true;
2320b57cec5SDimitry Andric     ElementBitwidth = ElemWidth;
2330b57cec5SDimitry Andric   }
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric   void makeScalar() {
2360b57cec5SDimitry Andric     Bitwidth = ElementBitwidth;
2370b57cec5SDimitry Andric     NumVectors = 0;
2380b57cec5SDimitry Andric   }
2390b57cec5SDimitry Andric 
2400b57cec5SDimitry Andric   void makeOneVector() {
2410b57cec5SDimitry Andric     assert(isVector());
2420b57cec5SDimitry Andric     NumVectors = 1;
2430b57cec5SDimitry Andric   }
2440b57cec5SDimitry Andric 
2455ffd83dbSDimitry Andric   void make32BitElement() {
2465ffd83dbSDimitry Andric     assert_with_loc(Bitwidth > 32, "Not enough bits to make it 32!");
2475ffd83dbSDimitry Andric     ElementBitwidth = 32;
2485ffd83dbSDimitry Andric   }
2495ffd83dbSDimitry Andric 
2500b57cec5SDimitry Andric   void doubleLanes() {
2510b57cec5SDimitry Andric     assert_with_loc(Bitwidth != 128, "Can't get bigger than 128!");
2520b57cec5SDimitry Andric     Bitwidth = 128;
2530b57cec5SDimitry Andric   }
2540b57cec5SDimitry Andric 
2550b57cec5SDimitry Andric   void halveLanes() {
2560b57cec5SDimitry Andric     assert_with_loc(Bitwidth != 64, "Can't get smaller than 64!");
2570b57cec5SDimitry Andric     Bitwidth = 64;
2580b57cec5SDimitry Andric   }
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric   /// Return the C string representation of a type, which is the typename
2610b57cec5SDimitry Andric   /// defined in stdint.h or arm_neon.h.
2620b57cec5SDimitry Andric   std::string str() const;
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric   /// Return the string representation of a type, which is an encoded
2650b57cec5SDimitry Andric   /// string for passing to the BUILTIN() macro in Builtins.def.
2660b57cec5SDimitry Andric   std::string builtin_str() const;
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric   /// Return the value in NeonTypeFlags for this type.
2690b57cec5SDimitry Andric   unsigned getNeonEnum() const;
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric   /// Parse a type from a stdint.h or arm_neon.h typedef name,
2720b57cec5SDimitry Andric   /// for example uint32x2_t or int64_t.
2730b57cec5SDimitry Andric   static Type fromTypedefName(StringRef Name);
2740b57cec5SDimitry Andric 
2750b57cec5SDimitry Andric private:
2760b57cec5SDimitry Andric   /// Creates the type based on the typespec string in TS.
2770b57cec5SDimitry Andric   /// Sets "Quad" to true if the "Q" or "H" modifiers were
2780b57cec5SDimitry Andric   /// seen. This is needed by applyModifier as some modifiers
2790b57cec5SDimitry Andric   /// only take effect if the type size was changed by "Q" or "H".
2800b57cec5SDimitry Andric   void applyTypespec(bool &Quad);
281480093f4SDimitry Andric   /// Applies prototype modifiers to the type.
282480093f4SDimitry Andric   void applyModifiers(StringRef Mods);
2830b57cec5SDimitry Andric };
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2860b57cec5SDimitry Andric // Variable
2870b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2880b57cec5SDimitry Andric 
2890b57cec5SDimitry Andric /// A variable is a simple class that just has a type and a name.
2900b57cec5SDimitry Andric class Variable {
2910b57cec5SDimitry Andric   Type T;
2920b57cec5SDimitry Andric   std::string N;
2930b57cec5SDimitry Andric 
2940b57cec5SDimitry Andric public:
295*04eeddc0SDimitry Andric   Variable() : T(Type::getVoid()) {}
2960b57cec5SDimitry Andric   Variable(Type T, std::string N) : T(std::move(T)), N(std::move(N)) {}
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric   Type getType() const { return T; }
2990b57cec5SDimitry Andric   std::string getName() const { return "__" + N; }
3000b57cec5SDimitry Andric };
3010b57cec5SDimitry Andric 
3020b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3030b57cec5SDimitry Andric // Intrinsic
3040b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3050b57cec5SDimitry Andric 
3060b57cec5SDimitry Andric /// The main grunt class. This represents an instantiation of an intrinsic with
3070b57cec5SDimitry Andric /// a particular typespec and prototype.
3080b57cec5SDimitry Andric class Intrinsic {
3090b57cec5SDimitry Andric   /// The Record this intrinsic was created from.
3100b57cec5SDimitry Andric   Record *R;
311480093f4SDimitry Andric   /// The unmangled name.
312480093f4SDimitry Andric   std::string Name;
3130b57cec5SDimitry Andric   /// The input and output typespecs. InTS == OutTS except when
3145ffd83dbSDimitry Andric   /// CartesianProductWith is non-empty - this is the case for vreinterpret.
3150b57cec5SDimitry Andric   TypeSpec OutTS, InTS;
3160b57cec5SDimitry Andric   /// The base class kind. Most intrinsics use ClassS, which has full type
3170b57cec5SDimitry Andric   /// info for integers (s32/u32). Some use ClassI, which doesn't care about
3180b57cec5SDimitry Andric   /// signedness (i32), while some (ClassB) have no type at all, only a width
3190b57cec5SDimitry Andric   /// (32).
3200b57cec5SDimitry Andric   ClassKind CK;
3210b57cec5SDimitry Andric   /// The list of DAGs for the body. May be empty, in which case we should
3220b57cec5SDimitry Andric   /// emit a builtin call.
3230b57cec5SDimitry Andric   ListInit *Body;
3240b57cec5SDimitry Andric   /// The architectural #ifdef guard.
3250b57cec5SDimitry Andric   std::string Guard;
3260b57cec5SDimitry Andric   /// Set if the Unavailable bit is 1. This means we don't generate a body,
3270b57cec5SDimitry Andric   /// just an "unavailable" attribute on a declaration.
3280b57cec5SDimitry Andric   bool IsUnavailable;
3290b57cec5SDimitry Andric   /// Is this intrinsic safe for big-endian? or does it need its arguments
3300b57cec5SDimitry Andric   /// reversing?
3310b57cec5SDimitry Andric   bool BigEndianSafe;
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric   /// The types of return value [0] and parameters [1..].
3340b57cec5SDimitry Andric   std::vector<Type> Types;
335480093f4SDimitry Andric   /// The index of the key type passed to CGBuiltin.cpp for polymorphic calls.
336480093f4SDimitry Andric   int PolymorphicKeyType;
3370b57cec5SDimitry Andric   /// The local variables defined.
3380b57cec5SDimitry Andric   std::map<std::string, Variable> Variables;
3390b57cec5SDimitry Andric   /// NeededEarly - set if any other intrinsic depends on this intrinsic.
3400b57cec5SDimitry Andric   bool NeededEarly;
3410b57cec5SDimitry Andric   /// UseMacro - set if we should implement using a macro or unset for a
3420b57cec5SDimitry Andric   ///            function.
3430b57cec5SDimitry Andric   bool UseMacro;
3440b57cec5SDimitry Andric   /// The set of intrinsics that this intrinsic uses/requires.
3450b57cec5SDimitry Andric   std::set<Intrinsic *> Dependencies;
3460b57cec5SDimitry Andric   /// The "base type", which is Type('d', OutTS). InBaseType is only
3475ffd83dbSDimitry Andric   /// different if CartesianProductWith is non-empty (for vreinterpret).
3480b57cec5SDimitry Andric   Type BaseType, InBaseType;
3490b57cec5SDimitry Andric   /// The return variable.
3500b57cec5SDimitry Andric   Variable RetVar;
3510b57cec5SDimitry Andric   /// A postfix to apply to every variable. Defaults to "".
3520b57cec5SDimitry Andric   std::string VariablePostfix;
3530b57cec5SDimitry Andric 
3540b57cec5SDimitry Andric   NeonEmitter &Emitter;
3550b57cec5SDimitry Andric   std::stringstream OS;
3560b57cec5SDimitry Andric 
357a7dea167SDimitry Andric   bool isBigEndianSafe() const {
358a7dea167SDimitry Andric     if (BigEndianSafe)
359a7dea167SDimitry Andric       return true;
360a7dea167SDimitry Andric 
361a7dea167SDimitry Andric     for (const auto &T : Types){
362a7dea167SDimitry Andric       if (T.isVector() && T.getNumElements() > 1)
363a7dea167SDimitry Andric         return false;
364a7dea167SDimitry Andric     }
365a7dea167SDimitry Andric     return true;
366a7dea167SDimitry Andric   }
367a7dea167SDimitry Andric 
3680b57cec5SDimitry Andric public:
3690b57cec5SDimitry Andric   Intrinsic(Record *R, StringRef Name, StringRef Proto, TypeSpec OutTS,
3700b57cec5SDimitry Andric             TypeSpec InTS, ClassKind CK, ListInit *Body, NeonEmitter &Emitter,
3710b57cec5SDimitry Andric             StringRef Guard, bool IsUnavailable, bool BigEndianSafe)
372480093f4SDimitry Andric       : R(R), Name(Name.str()), OutTS(OutTS), InTS(InTS), CK(CK), Body(Body),
373480093f4SDimitry Andric         Guard(Guard.str()), IsUnavailable(IsUnavailable),
374480093f4SDimitry Andric         BigEndianSafe(BigEndianSafe), PolymorphicKeyType(0), NeededEarly(false),
375480093f4SDimitry Andric         UseMacro(false), BaseType(OutTS, "."), InBaseType(InTS, "."),
376480093f4SDimitry Andric         Emitter(Emitter) {
3770b57cec5SDimitry Andric     // Modify the TypeSpec per-argument to get a concrete Type, and create
3780b57cec5SDimitry Andric     // known variables for each.
3790b57cec5SDimitry Andric     // Types[0] is the return value.
380480093f4SDimitry Andric     unsigned Pos = 0;
381480093f4SDimitry Andric     Types.emplace_back(OutTS, getNextModifiers(Proto, Pos));
382480093f4SDimitry Andric     StringRef Mods = getNextModifiers(Proto, Pos);
383480093f4SDimitry Andric     while (!Mods.empty()) {
384480093f4SDimitry Andric       Types.emplace_back(InTS, Mods);
385349cc55cSDimitry Andric       if (Mods.contains('!'))
386480093f4SDimitry Andric         PolymorphicKeyType = Types.size() - 1;
387480093f4SDimitry Andric 
388480093f4SDimitry Andric       Mods = getNextModifiers(Proto, Pos);
389480093f4SDimitry Andric     }
390480093f4SDimitry Andric 
391480093f4SDimitry Andric     for (auto Type : Types) {
392480093f4SDimitry Andric       // If this builtin takes an immediate argument, we need to #define it rather
393480093f4SDimitry Andric       // than use a standard declaration, so that SemaChecking can range check
394480093f4SDimitry Andric       // the immediate passed by the user.
395480093f4SDimitry Andric 
396480093f4SDimitry Andric       // Pointer arguments need to use macros to avoid hiding aligned attributes
397480093f4SDimitry Andric       // from the pointer type.
398480093f4SDimitry Andric 
399480093f4SDimitry Andric       // It is not permitted to pass or return an __fp16 by value, so intrinsics
400480093f4SDimitry Andric       // taking a scalar float16_t must be implemented as macros.
401480093f4SDimitry Andric       if (Type.isImmediate() || Type.isPointer() ||
402480093f4SDimitry Andric           (Type.isScalar() && Type.isHalf()))
403480093f4SDimitry Andric         UseMacro = true;
404480093f4SDimitry Andric     }
4050b57cec5SDimitry Andric   }
4060b57cec5SDimitry Andric 
4070b57cec5SDimitry Andric   /// Get the Record that this intrinsic is based off.
4080b57cec5SDimitry Andric   Record *getRecord() const { return R; }
4090b57cec5SDimitry Andric   /// Get the set of Intrinsics that this intrinsic calls.
4100b57cec5SDimitry Andric   /// this is the set of immediate dependencies, NOT the
4110b57cec5SDimitry Andric   /// transitive closure.
4120b57cec5SDimitry Andric   const std::set<Intrinsic *> &getDependencies() const { return Dependencies; }
4130b57cec5SDimitry Andric   /// Get the architectural guard string (#ifdef).
4140b57cec5SDimitry Andric   std::string getGuard() const { return Guard; }
4150b57cec5SDimitry Andric   /// Get the non-mangled name.
4160b57cec5SDimitry Andric   std::string getName() const { return Name; }
4170b57cec5SDimitry Andric 
4180b57cec5SDimitry Andric   /// Return true if the intrinsic takes an immediate operand.
4190b57cec5SDimitry Andric   bool hasImmediate() const {
420349cc55cSDimitry Andric     return llvm::any_of(Types, [](const Type &T) { return T.isImmediate(); });
4210b57cec5SDimitry Andric   }
4220b57cec5SDimitry Andric 
4230b57cec5SDimitry Andric   /// Return the parameter index of the immediate operand.
4240b57cec5SDimitry Andric   unsigned getImmediateIdx() const {
425480093f4SDimitry Andric     for (unsigned Idx = 0; Idx < Types.size(); ++Idx)
426480093f4SDimitry Andric       if (Types[Idx].isImmediate())
4270b57cec5SDimitry Andric         return Idx - 1;
428480093f4SDimitry Andric     llvm_unreachable("Intrinsic has no immediate");
4290b57cec5SDimitry Andric   }
4300b57cec5SDimitry Andric 
4310b57cec5SDimitry Andric 
432480093f4SDimitry Andric   unsigned getNumParams() const { return Types.size() - 1; }
4330b57cec5SDimitry Andric   Type getReturnType() const { return Types[0]; }
4340b57cec5SDimitry Andric   Type getParamType(unsigned I) const { return Types[I + 1]; }
4350b57cec5SDimitry Andric   Type getBaseType() const { return BaseType; }
436480093f4SDimitry Andric   Type getPolymorphicKeyType() const { return Types[PolymorphicKeyType]; }
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric   /// Return true if the prototype has a scalar argument.
4390b57cec5SDimitry Andric   bool protoHasScalar() const;
4400b57cec5SDimitry Andric 
4410b57cec5SDimitry Andric   /// Return the index that parameter PIndex will sit at
4420b57cec5SDimitry Andric   /// in a generated function call. This is often just PIndex,
4430b57cec5SDimitry Andric   /// but may not be as things such as multiple-vector operands
4440b57cec5SDimitry Andric   /// and sret parameters need to be taken into accont.
4450b57cec5SDimitry Andric   unsigned getGeneratedParamIdx(unsigned PIndex) {
4460b57cec5SDimitry Andric     unsigned Idx = 0;
4470b57cec5SDimitry Andric     if (getReturnType().getNumVectors() > 1)
4480b57cec5SDimitry Andric       // Multiple vectors are passed as sret.
4490b57cec5SDimitry Andric       ++Idx;
4500b57cec5SDimitry Andric 
4510b57cec5SDimitry Andric     for (unsigned I = 0; I < PIndex; ++I)
4520b57cec5SDimitry Andric       Idx += std::max(1U, getParamType(I).getNumVectors());
4530b57cec5SDimitry Andric 
4540b57cec5SDimitry Andric     return Idx;
4550b57cec5SDimitry Andric   }
4560b57cec5SDimitry Andric 
4570b57cec5SDimitry Andric   bool hasBody() const { return Body && !Body->getValues().empty(); }
4580b57cec5SDimitry Andric 
4590b57cec5SDimitry Andric   void setNeededEarly() { NeededEarly = true; }
4600b57cec5SDimitry Andric 
4610b57cec5SDimitry Andric   bool operator<(const Intrinsic &Other) const {
4620b57cec5SDimitry Andric     // Sort lexicographically on a two-tuple (Guard, Name)
4630b57cec5SDimitry Andric     if (Guard != Other.Guard)
4640b57cec5SDimitry Andric       return Guard < Other.Guard;
4650b57cec5SDimitry Andric     return Name < Other.Name;
4660b57cec5SDimitry Andric   }
4670b57cec5SDimitry Andric 
4680b57cec5SDimitry Andric   ClassKind getClassKind(bool UseClassBIfScalar = false) {
4690b57cec5SDimitry Andric     if (UseClassBIfScalar && !protoHasScalar())
4700b57cec5SDimitry Andric       return ClassB;
4710b57cec5SDimitry Andric     return CK;
4720b57cec5SDimitry Andric   }
4730b57cec5SDimitry Andric 
4740b57cec5SDimitry Andric   /// Return the name, mangled with type information.
4750b57cec5SDimitry Andric   /// If ForceClassS is true, use ClassS (u32/s32) instead
4760b57cec5SDimitry Andric   /// of the intrinsic's own type class.
4770b57cec5SDimitry Andric   std::string getMangledName(bool ForceClassS = false) const;
4780b57cec5SDimitry Andric   /// Return the type code for a builtin function call.
4790b57cec5SDimitry Andric   std::string getInstTypeCode(Type T, ClassKind CK) const;
4800b57cec5SDimitry Andric   /// Return the type string for a BUILTIN() macro in Builtins.def.
4810b57cec5SDimitry Andric   std::string getBuiltinTypeStr();
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric   /// Generate the intrinsic, returning code.
4840b57cec5SDimitry Andric   std::string generate();
4850b57cec5SDimitry Andric   /// Perform type checking and populate the dependency graph, but
4860b57cec5SDimitry Andric   /// don't generate code yet.
4870b57cec5SDimitry Andric   void indexBody();
4880b57cec5SDimitry Andric 
4890b57cec5SDimitry Andric private:
490480093f4SDimitry Andric   StringRef getNextModifiers(StringRef Proto, unsigned &Pos) const;
491480093f4SDimitry Andric 
4920b57cec5SDimitry Andric   std::string mangleName(std::string Name, ClassKind CK) const;
4930b57cec5SDimitry Andric 
4940b57cec5SDimitry Andric   void initVariables();
4950b57cec5SDimitry Andric   std::string replaceParamsIn(std::string S);
4960b57cec5SDimitry Andric 
4970b57cec5SDimitry Andric   void emitBodyAsBuiltinCall();
4980b57cec5SDimitry Andric 
4990b57cec5SDimitry Andric   void generateImpl(bool ReverseArguments,
5000b57cec5SDimitry Andric                     StringRef NamePrefix, StringRef CallPrefix);
5010b57cec5SDimitry Andric   void emitReturn();
5020b57cec5SDimitry Andric   void emitBody(StringRef CallPrefix);
5030b57cec5SDimitry Andric   void emitShadowedArgs();
5040b57cec5SDimitry Andric   void emitArgumentReversal();
5050b57cec5SDimitry Andric   void emitReturnReversal();
5060b57cec5SDimitry Andric   void emitReverseVariable(Variable &Dest, Variable &Src);
5070b57cec5SDimitry Andric   void emitNewLine();
5080b57cec5SDimitry Andric   void emitClosingBrace();
5090b57cec5SDimitry Andric   void emitOpeningBrace();
5100b57cec5SDimitry Andric   void emitPrototype(StringRef NamePrefix);
5110b57cec5SDimitry Andric 
5120b57cec5SDimitry Andric   class DagEmitter {
5130b57cec5SDimitry Andric     Intrinsic &Intr;
5140b57cec5SDimitry Andric     StringRef CallPrefix;
5150b57cec5SDimitry Andric 
5160b57cec5SDimitry Andric   public:
5170b57cec5SDimitry Andric     DagEmitter(Intrinsic &Intr, StringRef CallPrefix) :
5180b57cec5SDimitry Andric       Intr(Intr), CallPrefix(CallPrefix) {
5190b57cec5SDimitry Andric     }
5200b57cec5SDimitry Andric     std::pair<Type, std::string> emitDagArg(Init *Arg, std::string ArgName);
5210b57cec5SDimitry Andric     std::pair<Type, std::string> emitDagSaveTemp(DagInit *DI);
5220b57cec5SDimitry Andric     std::pair<Type, std::string> emitDagSplat(DagInit *DI);
5230b57cec5SDimitry Andric     std::pair<Type, std::string> emitDagDup(DagInit *DI);
5240b57cec5SDimitry Andric     std::pair<Type, std::string> emitDagDupTyped(DagInit *DI);
5250b57cec5SDimitry Andric     std::pair<Type, std::string> emitDagShuffle(DagInit *DI);
5260b57cec5SDimitry Andric     std::pair<Type, std::string> emitDagCast(DagInit *DI, bool IsBitCast);
5275ffd83dbSDimitry Andric     std::pair<Type, std::string> emitDagCall(DagInit *DI,
5285ffd83dbSDimitry Andric                                              bool MatchMangledName);
5290b57cec5SDimitry Andric     std::pair<Type, std::string> emitDagNameReplace(DagInit *DI);
5300b57cec5SDimitry Andric     std::pair<Type, std::string> emitDagLiteral(DagInit *DI);
5310b57cec5SDimitry Andric     std::pair<Type, std::string> emitDagOp(DagInit *DI);
5320b57cec5SDimitry Andric     std::pair<Type, std::string> emitDag(DagInit *DI);
5330b57cec5SDimitry Andric   };
5340b57cec5SDimitry Andric };
5350b57cec5SDimitry Andric 
5360b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
5370b57cec5SDimitry Andric // NeonEmitter
5380b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
5390b57cec5SDimitry Andric 
5400b57cec5SDimitry Andric class NeonEmitter {
5410b57cec5SDimitry Andric   RecordKeeper &Records;
5420b57cec5SDimitry Andric   DenseMap<Record *, ClassKind> ClassMap;
5430b57cec5SDimitry Andric   std::map<std::string, std::deque<Intrinsic>> IntrinsicMap;
5440b57cec5SDimitry Andric   unsigned UniqueNumber;
5450b57cec5SDimitry Andric 
5460b57cec5SDimitry Andric   void createIntrinsic(Record *R, SmallVectorImpl<Intrinsic *> &Out);
5470b57cec5SDimitry Andric   void genBuiltinsDef(raw_ostream &OS, SmallVectorImpl<Intrinsic *> &Defs);
5480b57cec5SDimitry Andric   void genOverloadTypeCheckCode(raw_ostream &OS,
5490b57cec5SDimitry Andric                                 SmallVectorImpl<Intrinsic *> &Defs);
5500b57cec5SDimitry Andric   void genIntrinsicRangeCheckCode(raw_ostream &OS,
5510b57cec5SDimitry Andric                                   SmallVectorImpl<Intrinsic *> &Defs);
5520b57cec5SDimitry Andric 
5530b57cec5SDimitry Andric public:
5540b57cec5SDimitry Andric   /// Called by Intrinsic - this attempts to get an intrinsic that takes
5550b57cec5SDimitry Andric   /// the given types as arguments.
5565ffd83dbSDimitry Andric   Intrinsic &getIntrinsic(StringRef Name, ArrayRef<Type> Types,
5575ffd83dbSDimitry Andric                           Optional<std::string> MangledName);
5580b57cec5SDimitry Andric 
5590b57cec5SDimitry Andric   /// Called by Intrinsic - returns a globally-unique number.
5600b57cec5SDimitry Andric   unsigned getUniqueNumber() { return UniqueNumber++; }
5610b57cec5SDimitry Andric 
5620b57cec5SDimitry Andric   NeonEmitter(RecordKeeper &R) : Records(R), UniqueNumber(0) {
5630b57cec5SDimitry Andric     Record *SI = R.getClass("SInst");
5640b57cec5SDimitry Andric     Record *II = R.getClass("IInst");
5650b57cec5SDimitry Andric     Record *WI = R.getClass("WInst");
5660b57cec5SDimitry Andric     Record *SOpI = R.getClass("SOpInst");
5670b57cec5SDimitry Andric     Record *IOpI = R.getClass("IOpInst");
5680b57cec5SDimitry Andric     Record *WOpI = R.getClass("WOpInst");
5690b57cec5SDimitry Andric     Record *LOpI = R.getClass("LOpInst");
5700b57cec5SDimitry Andric     Record *NoTestOpI = R.getClass("NoTestOpInst");
5710b57cec5SDimitry Andric 
5720b57cec5SDimitry Andric     ClassMap[SI] = ClassS;
5730b57cec5SDimitry Andric     ClassMap[II] = ClassI;
5740b57cec5SDimitry Andric     ClassMap[WI] = ClassW;
5750b57cec5SDimitry Andric     ClassMap[SOpI] = ClassS;
5760b57cec5SDimitry Andric     ClassMap[IOpI] = ClassI;
5770b57cec5SDimitry Andric     ClassMap[WOpI] = ClassW;
5780b57cec5SDimitry Andric     ClassMap[LOpI] = ClassL;
5790b57cec5SDimitry Andric     ClassMap[NoTestOpI] = ClassNoTest;
5800b57cec5SDimitry Andric   }
5810b57cec5SDimitry Andric 
582e8d8bef9SDimitry Andric   // Emit arm_neon.h.inc
5830b57cec5SDimitry Andric   void run(raw_ostream &o);
5840b57cec5SDimitry Andric 
585e8d8bef9SDimitry Andric   // Emit arm_fp16.h.inc
5860b57cec5SDimitry Andric   void runFP16(raw_ostream &o);
5870b57cec5SDimitry Andric 
588e8d8bef9SDimitry Andric   // Emit arm_bf16.h.inc
5895ffd83dbSDimitry Andric   void runBF16(raw_ostream &o);
5905ffd83dbSDimitry Andric 
591e8d8bef9SDimitry Andric   // Emit all the __builtin prototypes used in arm_neon.h, arm_fp16.h and
592e8d8bef9SDimitry Andric   // arm_bf16.h
5930b57cec5SDimitry Andric   void runHeader(raw_ostream &o);
5940b57cec5SDimitry Andric };
5950b57cec5SDimitry Andric 
5960b57cec5SDimitry Andric } // end anonymous namespace
5970b57cec5SDimitry Andric 
5980b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
5990b57cec5SDimitry Andric // Type implementation
6000b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
6010b57cec5SDimitry Andric 
6020b57cec5SDimitry Andric std::string Type::str() const {
603480093f4SDimitry Andric   if (isVoid())
6040b57cec5SDimitry Andric     return "void";
6050b57cec5SDimitry Andric   std::string S;
6060b57cec5SDimitry Andric 
607480093f4SDimitry Andric   if (isInteger() && !isSigned())
6080b57cec5SDimitry Andric     S += "u";
6090b57cec5SDimitry Andric 
610480093f4SDimitry Andric   if (isPoly())
6110b57cec5SDimitry Andric     S += "poly";
612480093f4SDimitry Andric   else if (isFloating())
6130b57cec5SDimitry Andric     S += "float";
6145ffd83dbSDimitry Andric   else if (isBFloat16())
6155ffd83dbSDimitry Andric     S += "bfloat";
6160b57cec5SDimitry Andric   else
6170b57cec5SDimitry Andric     S += "int";
6180b57cec5SDimitry Andric 
6190b57cec5SDimitry Andric   S += utostr(ElementBitwidth);
6200b57cec5SDimitry Andric   if (isVector())
6210b57cec5SDimitry Andric     S += "x" + utostr(getNumElements());
6220b57cec5SDimitry Andric   if (NumVectors > 1)
6230b57cec5SDimitry Andric     S += "x" + utostr(NumVectors);
6240b57cec5SDimitry Andric   S += "_t";
6250b57cec5SDimitry Andric 
6260b57cec5SDimitry Andric   if (Constant)
6270b57cec5SDimitry Andric     S += " const";
6280b57cec5SDimitry Andric   if (Pointer)
6290b57cec5SDimitry Andric     S += " *";
6300b57cec5SDimitry Andric 
6310b57cec5SDimitry Andric   return S;
6320b57cec5SDimitry Andric }
6330b57cec5SDimitry Andric 
6340b57cec5SDimitry Andric std::string Type::builtin_str() const {
6350b57cec5SDimitry Andric   std::string S;
6360b57cec5SDimitry Andric   if (isVoid())
6370b57cec5SDimitry Andric     return "v";
6380b57cec5SDimitry Andric 
639480093f4SDimitry Andric   if (isPointer()) {
6400b57cec5SDimitry Andric     // All pointers are void pointers.
641480093f4SDimitry Andric     S = "v";
642480093f4SDimitry Andric     if (isConstPointer())
643480093f4SDimitry Andric       S += "C";
644480093f4SDimitry Andric     S += "*";
645480093f4SDimitry Andric     return S;
646480093f4SDimitry Andric   } else if (isInteger())
6470b57cec5SDimitry Andric     switch (ElementBitwidth) {
6480b57cec5SDimitry Andric     case 8: S += "c"; break;
6490b57cec5SDimitry Andric     case 16: S += "s"; break;
6500b57cec5SDimitry Andric     case 32: S += "i"; break;
6510b57cec5SDimitry Andric     case 64: S += "Wi"; break;
6520b57cec5SDimitry Andric     case 128: S += "LLLi"; break;
6530b57cec5SDimitry Andric     default: llvm_unreachable("Unhandled case!");
6540b57cec5SDimitry Andric     }
6555ffd83dbSDimitry Andric   else if (isBFloat16()) {
6565ffd83dbSDimitry Andric     assert(ElementBitwidth == 16 && "BFloat16 can only be 16 bits");
6575ffd83dbSDimitry Andric     S += "y";
6585ffd83dbSDimitry Andric   } else
6590b57cec5SDimitry Andric     switch (ElementBitwidth) {
6600b57cec5SDimitry Andric     case 16: S += "h"; break;
6610b57cec5SDimitry Andric     case 32: S += "f"; break;
6620b57cec5SDimitry Andric     case 64: S += "d"; break;
6630b57cec5SDimitry Andric     default: llvm_unreachable("Unhandled case!");
6640b57cec5SDimitry Andric     }
6650b57cec5SDimitry Andric 
666480093f4SDimitry Andric   // FIXME: NECESSARY???????????????????????????????????????????????????????????????????????
667480093f4SDimitry Andric   if (isChar() && !isPointer() && isSigned())
6680b57cec5SDimitry Andric     // Make chars explicitly signed.
6690b57cec5SDimitry Andric     S = "S" + S;
670480093f4SDimitry Andric   else if (isInteger() && !isSigned())
6710b57cec5SDimitry Andric     S = "U" + S;
6720b57cec5SDimitry Andric 
6730b57cec5SDimitry Andric   // Constant indices are "int", but have the "constant expression" modifier.
6740b57cec5SDimitry Andric   if (isImmediate()) {
6750b57cec5SDimitry Andric     assert(isInteger() && isSigned());
6760b57cec5SDimitry Andric     S = "I" + S;
6770b57cec5SDimitry Andric   }
6780b57cec5SDimitry Andric 
679480093f4SDimitry Andric   if (isScalar())
6800b57cec5SDimitry Andric     return S;
6810b57cec5SDimitry Andric 
6820b57cec5SDimitry Andric   std::string Ret;
6830b57cec5SDimitry Andric   for (unsigned I = 0; I < NumVectors; ++I)
6840b57cec5SDimitry Andric     Ret += "V" + utostr(getNumElements()) + S;
6850b57cec5SDimitry Andric 
6860b57cec5SDimitry Andric   return Ret;
6870b57cec5SDimitry Andric }
6880b57cec5SDimitry Andric 
6890b57cec5SDimitry Andric unsigned Type::getNeonEnum() const {
6900b57cec5SDimitry Andric   unsigned Addend;
6910b57cec5SDimitry Andric   switch (ElementBitwidth) {
6920b57cec5SDimitry Andric   case 8: Addend = 0; break;
6930b57cec5SDimitry Andric   case 16: Addend = 1; break;
6940b57cec5SDimitry Andric   case 32: Addend = 2; break;
6950b57cec5SDimitry Andric   case 64: Addend = 3; break;
6960b57cec5SDimitry Andric   case 128: Addend = 4; break;
6970b57cec5SDimitry Andric   default: llvm_unreachable("Unhandled element bitwidth!");
6980b57cec5SDimitry Andric   }
6990b57cec5SDimitry Andric 
7000b57cec5SDimitry Andric   unsigned Base = (unsigned)NeonTypeFlags::Int8 + Addend;
701480093f4SDimitry Andric   if (isPoly()) {
7020b57cec5SDimitry Andric     // Adjustment needed because Poly32 doesn't exist.
7030b57cec5SDimitry Andric     if (Addend >= 2)
7040b57cec5SDimitry Andric       --Addend;
7050b57cec5SDimitry Andric     Base = (unsigned)NeonTypeFlags::Poly8 + Addend;
7060b57cec5SDimitry Andric   }
707480093f4SDimitry Andric   if (isFloating()) {
7080b57cec5SDimitry Andric     assert(Addend != 0 && "Float8 doesn't exist!");
7090b57cec5SDimitry Andric     Base = (unsigned)NeonTypeFlags::Float16 + (Addend - 1);
7100b57cec5SDimitry Andric   }
7110b57cec5SDimitry Andric 
7125ffd83dbSDimitry Andric   if (isBFloat16()) {
7135ffd83dbSDimitry Andric     assert(Addend == 1 && "BFloat16 is only 16 bit");
7145ffd83dbSDimitry Andric     Base = (unsigned)NeonTypeFlags::BFloat16;
7155ffd83dbSDimitry Andric   }
7165ffd83dbSDimitry Andric 
7170b57cec5SDimitry Andric   if (Bitwidth == 128)
7180b57cec5SDimitry Andric     Base |= (unsigned)NeonTypeFlags::QuadFlag;
719480093f4SDimitry Andric   if (isInteger() && !isSigned())
7200b57cec5SDimitry Andric     Base |= (unsigned)NeonTypeFlags::UnsignedFlag;
7210b57cec5SDimitry Andric 
7220b57cec5SDimitry Andric   return Base;
7230b57cec5SDimitry Andric }
7240b57cec5SDimitry Andric 
7250b57cec5SDimitry Andric Type Type::fromTypedefName(StringRef Name) {
7260b57cec5SDimitry Andric   Type T;
727480093f4SDimitry Andric   T.Kind = SInt;
7280b57cec5SDimitry Andric 
7290b57cec5SDimitry Andric   if (Name.front() == 'u') {
730480093f4SDimitry Andric     T.Kind = UInt;
7310b57cec5SDimitry Andric     Name = Name.drop_front();
7320b57cec5SDimitry Andric   }
7330b57cec5SDimitry Andric 
7340b57cec5SDimitry Andric   if (Name.startswith("float")) {
735480093f4SDimitry Andric     T.Kind = Float;
7360b57cec5SDimitry Andric     Name = Name.drop_front(5);
7370b57cec5SDimitry Andric   } else if (Name.startswith("poly")) {
738480093f4SDimitry Andric     T.Kind = Poly;
7390b57cec5SDimitry Andric     Name = Name.drop_front(4);
7405ffd83dbSDimitry Andric   } else if (Name.startswith("bfloat")) {
7415ffd83dbSDimitry Andric     T.Kind = BFloat16;
7425ffd83dbSDimitry Andric     Name = Name.drop_front(6);
7430b57cec5SDimitry Andric   } else {
7440b57cec5SDimitry Andric     assert(Name.startswith("int"));
7450b57cec5SDimitry Andric     Name = Name.drop_front(3);
7460b57cec5SDimitry Andric   }
7470b57cec5SDimitry Andric 
7480b57cec5SDimitry Andric   unsigned I = 0;
7490b57cec5SDimitry Andric   for (I = 0; I < Name.size(); ++I) {
7500b57cec5SDimitry Andric     if (!isdigit(Name[I]))
7510b57cec5SDimitry Andric       break;
7520b57cec5SDimitry Andric   }
7530b57cec5SDimitry Andric   Name.substr(0, I).getAsInteger(10, T.ElementBitwidth);
7540b57cec5SDimitry Andric   Name = Name.drop_front(I);
7550b57cec5SDimitry Andric 
7560b57cec5SDimitry Andric   T.Bitwidth = T.ElementBitwidth;
7570b57cec5SDimitry Andric   T.NumVectors = 1;
7580b57cec5SDimitry Andric 
7590b57cec5SDimitry Andric   if (Name.front() == 'x') {
7600b57cec5SDimitry Andric     Name = Name.drop_front();
7610b57cec5SDimitry Andric     unsigned I = 0;
7620b57cec5SDimitry Andric     for (I = 0; I < Name.size(); ++I) {
7630b57cec5SDimitry Andric       if (!isdigit(Name[I]))
7640b57cec5SDimitry Andric         break;
7650b57cec5SDimitry Andric     }
7660b57cec5SDimitry Andric     unsigned NumLanes;
7670b57cec5SDimitry Andric     Name.substr(0, I).getAsInteger(10, NumLanes);
7680b57cec5SDimitry Andric     Name = Name.drop_front(I);
7690b57cec5SDimitry Andric     T.Bitwidth = T.ElementBitwidth * NumLanes;
7700b57cec5SDimitry Andric   } else {
7710b57cec5SDimitry Andric     // Was scalar.
7720b57cec5SDimitry Andric     T.NumVectors = 0;
7730b57cec5SDimitry Andric   }
7740b57cec5SDimitry Andric   if (Name.front() == 'x') {
7750b57cec5SDimitry Andric     Name = Name.drop_front();
7760b57cec5SDimitry Andric     unsigned I = 0;
7770b57cec5SDimitry Andric     for (I = 0; I < Name.size(); ++I) {
7780b57cec5SDimitry Andric       if (!isdigit(Name[I]))
7790b57cec5SDimitry Andric         break;
7800b57cec5SDimitry Andric     }
7810b57cec5SDimitry Andric     Name.substr(0, I).getAsInteger(10, T.NumVectors);
7820b57cec5SDimitry Andric     Name = Name.drop_front(I);
7830b57cec5SDimitry Andric   }
7840b57cec5SDimitry Andric 
7850b57cec5SDimitry Andric   assert(Name.startswith("_t") && "Malformed typedef!");
7860b57cec5SDimitry Andric   return T;
7870b57cec5SDimitry Andric }
7880b57cec5SDimitry Andric 
7890b57cec5SDimitry Andric void Type::applyTypespec(bool &Quad) {
7900b57cec5SDimitry Andric   std::string S = TS;
7910b57cec5SDimitry Andric   ScalarForMangling = false;
792480093f4SDimitry Andric   Kind = SInt;
7930b57cec5SDimitry Andric   ElementBitwidth = ~0U;
7940b57cec5SDimitry Andric   NumVectors = 1;
7950b57cec5SDimitry Andric 
7960b57cec5SDimitry Andric   for (char I : S) {
7970b57cec5SDimitry Andric     switch (I) {
7980b57cec5SDimitry Andric     case 'S':
7990b57cec5SDimitry Andric       ScalarForMangling = true;
8000b57cec5SDimitry Andric       break;
8010b57cec5SDimitry Andric     case 'H':
8020b57cec5SDimitry Andric       NoManglingQ = true;
8030b57cec5SDimitry Andric       Quad = true;
8040b57cec5SDimitry Andric       break;
8050b57cec5SDimitry Andric     case 'Q':
8060b57cec5SDimitry Andric       Quad = true;
8070b57cec5SDimitry Andric       break;
8080b57cec5SDimitry Andric     case 'P':
809480093f4SDimitry Andric       Kind = Poly;
8100b57cec5SDimitry Andric       break;
8110b57cec5SDimitry Andric     case 'U':
812480093f4SDimitry Andric       Kind = UInt;
8130b57cec5SDimitry Andric       break;
8140b57cec5SDimitry Andric     case 'c':
8150b57cec5SDimitry Andric       ElementBitwidth = 8;
8160b57cec5SDimitry Andric       break;
8170b57cec5SDimitry Andric     case 'h':
818480093f4SDimitry Andric       Kind = Float;
8190b57cec5SDimitry Andric       LLVM_FALLTHROUGH;
8200b57cec5SDimitry Andric     case 's':
8210b57cec5SDimitry Andric       ElementBitwidth = 16;
8220b57cec5SDimitry Andric       break;
8230b57cec5SDimitry Andric     case 'f':
824480093f4SDimitry Andric       Kind = Float;
8250b57cec5SDimitry Andric       LLVM_FALLTHROUGH;
8260b57cec5SDimitry Andric     case 'i':
8270b57cec5SDimitry Andric       ElementBitwidth = 32;
8280b57cec5SDimitry Andric       break;
8290b57cec5SDimitry Andric     case 'd':
830480093f4SDimitry Andric       Kind = Float;
8310b57cec5SDimitry Andric       LLVM_FALLTHROUGH;
8320b57cec5SDimitry Andric     case 'l':
8330b57cec5SDimitry Andric       ElementBitwidth = 64;
8340b57cec5SDimitry Andric       break;
8350b57cec5SDimitry Andric     case 'k':
8360b57cec5SDimitry Andric       ElementBitwidth = 128;
8370b57cec5SDimitry Andric       // Poly doesn't have a 128x1 type.
838480093f4SDimitry Andric       if (isPoly())
8390b57cec5SDimitry Andric         NumVectors = 0;
8400b57cec5SDimitry Andric       break;
8415ffd83dbSDimitry Andric     case 'b':
8425ffd83dbSDimitry Andric       Kind = BFloat16;
8435ffd83dbSDimitry Andric       ElementBitwidth = 16;
8445ffd83dbSDimitry Andric       break;
8450b57cec5SDimitry Andric     default:
8460b57cec5SDimitry Andric       llvm_unreachable("Unhandled type code!");
8470b57cec5SDimitry Andric     }
8480b57cec5SDimitry Andric   }
8490b57cec5SDimitry Andric   assert(ElementBitwidth != ~0U && "Bad element bitwidth!");
8500b57cec5SDimitry Andric 
8510b57cec5SDimitry Andric   Bitwidth = Quad ? 128 : 64;
8520b57cec5SDimitry Andric }
8530b57cec5SDimitry Andric 
854480093f4SDimitry Andric void Type::applyModifiers(StringRef Mods) {
8550b57cec5SDimitry Andric   bool AppliedQuad = false;
8560b57cec5SDimitry Andric   applyTypespec(AppliedQuad);
8570b57cec5SDimitry Andric 
858480093f4SDimitry Andric   for (char Mod : Mods) {
8590b57cec5SDimitry Andric     switch (Mod) {
860480093f4SDimitry Andric     case '.':
861480093f4SDimitry Andric       break;
8620b57cec5SDimitry Andric     case 'v':
863480093f4SDimitry Andric       Kind = Void;
8640b57cec5SDimitry Andric       break;
865480093f4SDimitry Andric     case 'S':
866480093f4SDimitry Andric       Kind = SInt;
8670b57cec5SDimitry Andric       break;
8680b57cec5SDimitry Andric     case 'U':
869480093f4SDimitry Andric       Kind = UInt;
8700b57cec5SDimitry Andric       break;
8715ffd83dbSDimitry Andric     case 'B':
8725ffd83dbSDimitry Andric       Kind = BFloat16;
8735ffd83dbSDimitry Andric       ElementBitwidth = 16;
8745ffd83dbSDimitry Andric       break;
8750b57cec5SDimitry Andric     case 'F':
876480093f4SDimitry Andric       Kind = Float;
8770b57cec5SDimitry Andric       break;
878480093f4SDimitry Andric     case 'P':
879480093f4SDimitry Andric       Kind = Poly;
8800b57cec5SDimitry Andric       break;
881480093f4SDimitry Andric     case '>':
882480093f4SDimitry Andric       assert(ElementBitwidth < 128);
883480093f4SDimitry Andric       ElementBitwidth *= 2;
884480093f4SDimitry Andric       break;
885480093f4SDimitry Andric     case '<':
886480093f4SDimitry Andric       assert(ElementBitwidth > 8);
887480093f4SDimitry Andric       ElementBitwidth /= 2;
8880b57cec5SDimitry Andric       break;
8890b57cec5SDimitry Andric     case '1':
8900b57cec5SDimitry Andric       NumVectors = 0;
8910b57cec5SDimitry Andric       break;
8920b57cec5SDimitry Andric     case '2':
8930b57cec5SDimitry Andric       NumVectors = 2;
8940b57cec5SDimitry Andric       break;
8950b57cec5SDimitry Andric     case '3':
8960b57cec5SDimitry Andric       NumVectors = 3;
8970b57cec5SDimitry Andric       break;
8980b57cec5SDimitry Andric     case '4':
8990b57cec5SDimitry Andric       NumVectors = 4;
9000b57cec5SDimitry Andric       break;
901480093f4SDimitry Andric     case '*':
902480093f4SDimitry Andric       Pointer = true;
9030b57cec5SDimitry Andric       break;
904480093f4SDimitry Andric     case 'c':
905480093f4SDimitry Andric       Constant = true;
9060b57cec5SDimitry Andric       break;
907480093f4SDimitry Andric     case 'Q':
908480093f4SDimitry Andric       Bitwidth = 128;
9090b57cec5SDimitry Andric       break;
910480093f4SDimitry Andric     case 'q':
911480093f4SDimitry Andric       Bitwidth = 64;
9120b57cec5SDimitry Andric       break;
913480093f4SDimitry Andric     case 'I':
914480093f4SDimitry Andric       Kind = SInt;
915480093f4SDimitry Andric       ElementBitwidth = Bitwidth = 32;
916480093f4SDimitry Andric       NumVectors = 0;
917480093f4SDimitry Andric       Immediate = true;
9180b57cec5SDimitry Andric       break;
919480093f4SDimitry Andric     case 'p':
920480093f4SDimitry Andric       if (isPoly())
921480093f4SDimitry Andric         Kind = UInt;
922480093f4SDimitry Andric       break;
923480093f4SDimitry Andric     case '!':
924480093f4SDimitry Andric       // Key type, handled elsewhere.
9250b57cec5SDimitry Andric       break;
9260b57cec5SDimitry Andric     default:
9270b57cec5SDimitry Andric       llvm_unreachable("Unhandled character!");
9280b57cec5SDimitry Andric     }
9290b57cec5SDimitry Andric   }
930480093f4SDimitry Andric }
9310b57cec5SDimitry Andric 
9320b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
9330b57cec5SDimitry Andric // Intrinsic implementation
9340b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
9350b57cec5SDimitry Andric 
936480093f4SDimitry Andric StringRef Intrinsic::getNextModifiers(StringRef Proto, unsigned &Pos) const {
937480093f4SDimitry Andric   if (Proto.size() == Pos)
938480093f4SDimitry Andric     return StringRef();
939480093f4SDimitry Andric   else if (Proto[Pos] != '(')
940480093f4SDimitry Andric     return Proto.substr(Pos++, 1);
941480093f4SDimitry Andric 
942480093f4SDimitry Andric   size_t Start = Pos + 1;
943480093f4SDimitry Andric   size_t End = Proto.find(')', Start);
944480093f4SDimitry Andric   assert_with_loc(End != StringRef::npos, "unmatched modifier group paren");
945480093f4SDimitry Andric   Pos = End + 1;
946480093f4SDimitry Andric   return Proto.slice(Start, End);
947480093f4SDimitry Andric }
948480093f4SDimitry Andric 
9490b57cec5SDimitry Andric std::string Intrinsic::getInstTypeCode(Type T, ClassKind CK) const {
9500b57cec5SDimitry Andric   char typeCode = '\0';
9510b57cec5SDimitry Andric   bool printNumber = true;
9520b57cec5SDimitry Andric 
9530b57cec5SDimitry Andric   if (CK == ClassB)
9540b57cec5SDimitry Andric     return "";
9550b57cec5SDimitry Andric 
9565ffd83dbSDimitry Andric   if (T.isBFloat16())
9575ffd83dbSDimitry Andric     return "bf16";
9585ffd83dbSDimitry Andric 
9590b57cec5SDimitry Andric   if (T.isPoly())
9600b57cec5SDimitry Andric     typeCode = 'p';
9610b57cec5SDimitry Andric   else if (T.isInteger())
9620b57cec5SDimitry Andric     typeCode = T.isSigned() ? 's' : 'u';
9630b57cec5SDimitry Andric   else
9640b57cec5SDimitry Andric     typeCode = 'f';
9650b57cec5SDimitry Andric 
9660b57cec5SDimitry Andric   if (CK == ClassI) {
9670b57cec5SDimitry Andric     switch (typeCode) {
9680b57cec5SDimitry Andric     default:
9690b57cec5SDimitry Andric       break;
9700b57cec5SDimitry Andric     case 's':
9710b57cec5SDimitry Andric     case 'u':
9720b57cec5SDimitry Andric     case 'p':
9730b57cec5SDimitry Andric       typeCode = 'i';
9740b57cec5SDimitry Andric       break;
9750b57cec5SDimitry Andric     }
9760b57cec5SDimitry Andric   }
9770b57cec5SDimitry Andric   if (CK == ClassB) {
9780b57cec5SDimitry Andric     typeCode = '\0';
9790b57cec5SDimitry Andric   }
9800b57cec5SDimitry Andric 
9810b57cec5SDimitry Andric   std::string S;
9820b57cec5SDimitry Andric   if (typeCode != '\0')
9830b57cec5SDimitry Andric     S.push_back(typeCode);
9840b57cec5SDimitry Andric   if (printNumber)
9850b57cec5SDimitry Andric     S += utostr(T.getElementSizeInBits());
9860b57cec5SDimitry Andric 
9870b57cec5SDimitry Andric   return S;
9880b57cec5SDimitry Andric }
9890b57cec5SDimitry Andric 
9900b57cec5SDimitry Andric std::string Intrinsic::getBuiltinTypeStr() {
9910b57cec5SDimitry Andric   ClassKind LocalCK = getClassKind(true);
9920b57cec5SDimitry Andric   std::string S;
9930b57cec5SDimitry Andric 
9940b57cec5SDimitry Andric   Type RetT = getReturnType();
9950b57cec5SDimitry Andric   if ((LocalCK == ClassI || LocalCK == ClassW) && RetT.isScalar() &&
9965ffd83dbSDimitry Andric       !RetT.isFloating() && !RetT.isBFloat16())
9970b57cec5SDimitry Andric     RetT.makeInteger(RetT.getElementSizeInBits(), false);
9980b57cec5SDimitry Andric 
9990b57cec5SDimitry Andric   // Since the return value must be one type, return a vector type of the
10000b57cec5SDimitry Andric   // appropriate width which we will bitcast.  An exception is made for
10010b57cec5SDimitry Andric   // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
10020b57cec5SDimitry Andric   // fashion, storing them to a pointer arg.
10030b57cec5SDimitry Andric   if (RetT.getNumVectors() > 1) {
10040b57cec5SDimitry Andric     S += "vv*"; // void result with void* first argument
10050b57cec5SDimitry Andric   } else {
10060b57cec5SDimitry Andric     if (RetT.isPoly())
10070b57cec5SDimitry Andric       RetT.makeInteger(RetT.getElementSizeInBits(), false);
1008480093f4SDimitry Andric     if (!RetT.isScalar() && RetT.isInteger() && !RetT.isSigned())
10090b57cec5SDimitry Andric       RetT.makeSigned();
10100b57cec5SDimitry Andric 
1011480093f4SDimitry Andric     if (LocalCK == ClassB && RetT.isValue() && !RetT.isScalar())
10120b57cec5SDimitry Andric       // Cast to vector of 8-bit elements.
10130b57cec5SDimitry Andric       RetT.makeInteger(8, true);
10140b57cec5SDimitry Andric 
10150b57cec5SDimitry Andric     S += RetT.builtin_str();
10160b57cec5SDimitry Andric   }
10170b57cec5SDimitry Andric 
10180b57cec5SDimitry Andric   for (unsigned I = 0; I < getNumParams(); ++I) {
10190b57cec5SDimitry Andric     Type T = getParamType(I);
10200b57cec5SDimitry Andric     if (T.isPoly())
10210b57cec5SDimitry Andric       T.makeInteger(T.getElementSizeInBits(), false);
10220b57cec5SDimitry Andric 
1023480093f4SDimitry Andric     if (LocalCK == ClassB && !T.isScalar())
10240b57cec5SDimitry Andric       T.makeInteger(8, true);
10250b57cec5SDimitry Andric     // Halves always get converted to 8-bit elements.
10260b57cec5SDimitry Andric     if (T.isHalf() && T.isVector() && !T.isScalarForMangling())
10270b57cec5SDimitry Andric       T.makeInteger(8, true);
10280b57cec5SDimitry Andric 
1029480093f4SDimitry Andric     if (LocalCK == ClassI && T.isInteger())
10300b57cec5SDimitry Andric       T.makeSigned();
10310b57cec5SDimitry Andric 
10320b57cec5SDimitry Andric     if (hasImmediate() && getImmediateIdx() == I)
10330b57cec5SDimitry Andric       T.makeImmediate(32);
10340b57cec5SDimitry Andric 
10350b57cec5SDimitry Andric     S += T.builtin_str();
10360b57cec5SDimitry Andric   }
10370b57cec5SDimitry Andric 
10380b57cec5SDimitry Andric   // Extra constant integer to hold type class enum for this function, e.g. s8
10390b57cec5SDimitry Andric   if (LocalCK == ClassB)
10400b57cec5SDimitry Andric     S += "i";
10410b57cec5SDimitry Andric 
10420b57cec5SDimitry Andric   return S;
10430b57cec5SDimitry Andric }
10440b57cec5SDimitry Andric 
10450b57cec5SDimitry Andric std::string Intrinsic::getMangledName(bool ForceClassS) const {
10460b57cec5SDimitry Andric   // Check if the prototype has a scalar operand with the type of the vector
10470b57cec5SDimitry Andric   // elements.  If not, bitcasting the args will take care of arg checking.
10480b57cec5SDimitry Andric   // The actual signedness etc. will be taken care of with special enums.
10490b57cec5SDimitry Andric   ClassKind LocalCK = CK;
10500b57cec5SDimitry Andric   if (!protoHasScalar())
10510b57cec5SDimitry Andric     LocalCK = ClassB;
10520b57cec5SDimitry Andric 
10530b57cec5SDimitry Andric   return mangleName(Name, ForceClassS ? ClassS : LocalCK);
10540b57cec5SDimitry Andric }
10550b57cec5SDimitry Andric 
10560b57cec5SDimitry Andric std::string Intrinsic::mangleName(std::string Name, ClassKind LocalCK) const {
10570b57cec5SDimitry Andric   std::string typeCode = getInstTypeCode(BaseType, LocalCK);
10580b57cec5SDimitry Andric   std::string S = Name;
10590b57cec5SDimitry Andric 
10600b57cec5SDimitry Andric   if (Name == "vcvt_f16_f32" || Name == "vcvt_f32_f16" ||
10615ffd83dbSDimitry Andric       Name == "vcvt_f32_f64" || Name == "vcvt_f64_f32" ||
10625ffd83dbSDimitry Andric       Name == "vcvt_f32_bf16")
10630b57cec5SDimitry Andric     return Name;
10640b57cec5SDimitry Andric 
10650b57cec5SDimitry Andric   if (!typeCode.empty()) {
10660b57cec5SDimitry Andric     // If the name ends with _xN (N = 2,3,4), insert the typeCode before _xN.
10670b57cec5SDimitry Andric     if (Name.size() >= 3 && isdigit(Name.back()) &&
10680b57cec5SDimitry Andric         Name[Name.length() - 2] == 'x' && Name[Name.length() - 3] == '_')
10690b57cec5SDimitry Andric       S.insert(S.length() - 3, "_" + typeCode);
10700b57cec5SDimitry Andric     else
10710b57cec5SDimitry Andric       S += "_" + typeCode;
10720b57cec5SDimitry Andric   }
10730b57cec5SDimitry Andric 
10740b57cec5SDimitry Andric   if (BaseType != InBaseType) {
10750b57cec5SDimitry Andric     // A reinterpret - out the input base type at the end.
10760b57cec5SDimitry Andric     S += "_" + getInstTypeCode(InBaseType, LocalCK);
10770b57cec5SDimitry Andric   }
10780b57cec5SDimitry Andric 
10790b57cec5SDimitry Andric   if (LocalCK == ClassB)
10800b57cec5SDimitry Andric     S += "_v";
10810b57cec5SDimitry Andric 
10820b57cec5SDimitry Andric   // Insert a 'q' before the first '_' character so that it ends up before
10830b57cec5SDimitry Andric   // _lane or _n on vector-scalar operations.
10840b57cec5SDimitry Andric   if (BaseType.getSizeInBits() == 128 && !BaseType.noManglingQ()) {
10850b57cec5SDimitry Andric     size_t Pos = S.find('_');
10860b57cec5SDimitry Andric     S.insert(Pos, "q");
10870b57cec5SDimitry Andric   }
10880b57cec5SDimitry Andric 
10890b57cec5SDimitry Andric   char Suffix = '\0';
10900b57cec5SDimitry Andric   if (BaseType.isScalarForMangling()) {
10910b57cec5SDimitry Andric     switch (BaseType.getElementSizeInBits()) {
10920b57cec5SDimitry Andric     case 8: Suffix = 'b'; break;
10930b57cec5SDimitry Andric     case 16: Suffix = 'h'; break;
10940b57cec5SDimitry Andric     case 32: Suffix = 's'; break;
10950b57cec5SDimitry Andric     case 64: Suffix = 'd'; break;
10960b57cec5SDimitry Andric     default: llvm_unreachable("Bad suffix!");
10970b57cec5SDimitry Andric     }
10980b57cec5SDimitry Andric   }
10990b57cec5SDimitry Andric   if (Suffix != '\0') {
11000b57cec5SDimitry Andric     size_t Pos = S.find('_');
11010b57cec5SDimitry Andric     S.insert(Pos, &Suffix, 1);
11020b57cec5SDimitry Andric   }
11030b57cec5SDimitry Andric 
11040b57cec5SDimitry Andric   return S;
11050b57cec5SDimitry Andric }
11060b57cec5SDimitry Andric 
11070b57cec5SDimitry Andric std::string Intrinsic::replaceParamsIn(std::string S) {
11080b57cec5SDimitry Andric   while (S.find('$') != std::string::npos) {
11090b57cec5SDimitry Andric     size_t Pos = S.find('$');
11100b57cec5SDimitry Andric     size_t End = Pos + 1;
11110b57cec5SDimitry Andric     while (isalpha(S[End]))
11120b57cec5SDimitry Andric       ++End;
11130b57cec5SDimitry Andric 
11140b57cec5SDimitry Andric     std::string VarName = S.substr(Pos + 1, End - Pos - 1);
11150b57cec5SDimitry Andric     assert_with_loc(Variables.find(VarName) != Variables.end(),
11160b57cec5SDimitry Andric                     "Variable not defined!");
11170b57cec5SDimitry Andric     S.replace(Pos, End - Pos, Variables.find(VarName)->second.getName());
11180b57cec5SDimitry Andric   }
11190b57cec5SDimitry Andric 
11200b57cec5SDimitry Andric   return S;
11210b57cec5SDimitry Andric }
11220b57cec5SDimitry Andric 
11230b57cec5SDimitry Andric void Intrinsic::initVariables() {
11240b57cec5SDimitry Andric   Variables.clear();
11250b57cec5SDimitry Andric 
11260b57cec5SDimitry Andric   // Modify the TypeSpec per-argument to get a concrete Type, and create
11270b57cec5SDimitry Andric   // known variables for each.
1128480093f4SDimitry Andric   for (unsigned I = 1; I < Types.size(); ++I) {
11290b57cec5SDimitry Andric     char NameC = '0' + (I - 1);
11300b57cec5SDimitry Andric     std::string Name = "p";
11310b57cec5SDimitry Andric     Name.push_back(NameC);
11320b57cec5SDimitry Andric 
11330b57cec5SDimitry Andric     Variables[Name] = Variable(Types[I], Name + VariablePostfix);
11340b57cec5SDimitry Andric   }
11350b57cec5SDimitry Andric   RetVar = Variable(Types[0], "ret" + VariablePostfix);
11360b57cec5SDimitry Andric }
11370b57cec5SDimitry Andric 
11380b57cec5SDimitry Andric void Intrinsic::emitPrototype(StringRef NamePrefix) {
11390b57cec5SDimitry Andric   if (UseMacro)
11400b57cec5SDimitry Andric     OS << "#define ";
11410b57cec5SDimitry Andric   else
11420b57cec5SDimitry Andric     OS << "__ai " << Types[0].str() << " ";
11430b57cec5SDimitry Andric 
11440b57cec5SDimitry Andric   OS << NamePrefix.str() << mangleName(Name, ClassS) << "(";
11450b57cec5SDimitry Andric 
11460b57cec5SDimitry Andric   for (unsigned I = 0; I < getNumParams(); ++I) {
11470b57cec5SDimitry Andric     if (I != 0)
11480b57cec5SDimitry Andric       OS << ", ";
11490b57cec5SDimitry Andric 
11500b57cec5SDimitry Andric     char NameC = '0' + I;
11510b57cec5SDimitry Andric     std::string Name = "p";
11520b57cec5SDimitry Andric     Name.push_back(NameC);
11530b57cec5SDimitry Andric     assert(Variables.find(Name) != Variables.end());
11540b57cec5SDimitry Andric     Variable &V = Variables[Name];
11550b57cec5SDimitry Andric 
11560b57cec5SDimitry Andric     if (!UseMacro)
11570b57cec5SDimitry Andric       OS << V.getType().str() << " ";
11580b57cec5SDimitry Andric     OS << V.getName();
11590b57cec5SDimitry Andric   }
11600b57cec5SDimitry Andric 
11610b57cec5SDimitry Andric   OS << ")";
11620b57cec5SDimitry Andric }
11630b57cec5SDimitry Andric 
11640b57cec5SDimitry Andric void Intrinsic::emitOpeningBrace() {
11650b57cec5SDimitry Andric   if (UseMacro)
11660b57cec5SDimitry Andric     OS << " __extension__ ({";
11670b57cec5SDimitry Andric   else
11680b57cec5SDimitry Andric     OS << " {";
11690b57cec5SDimitry Andric   emitNewLine();
11700b57cec5SDimitry Andric }
11710b57cec5SDimitry Andric 
11720b57cec5SDimitry Andric void Intrinsic::emitClosingBrace() {
11730b57cec5SDimitry Andric   if (UseMacro)
11740b57cec5SDimitry Andric     OS << "})";
11750b57cec5SDimitry Andric   else
11760b57cec5SDimitry Andric     OS << "}";
11770b57cec5SDimitry Andric }
11780b57cec5SDimitry Andric 
11790b57cec5SDimitry Andric void Intrinsic::emitNewLine() {
11800b57cec5SDimitry Andric   if (UseMacro)
11810b57cec5SDimitry Andric     OS << " \\\n";
11820b57cec5SDimitry Andric   else
11830b57cec5SDimitry Andric     OS << "\n";
11840b57cec5SDimitry Andric }
11850b57cec5SDimitry Andric 
11860b57cec5SDimitry Andric void Intrinsic::emitReverseVariable(Variable &Dest, Variable &Src) {
11870b57cec5SDimitry Andric   if (Dest.getType().getNumVectors() > 1) {
11880b57cec5SDimitry Andric     emitNewLine();
11890b57cec5SDimitry Andric 
11900b57cec5SDimitry Andric     for (unsigned K = 0; K < Dest.getType().getNumVectors(); ++K) {
11910b57cec5SDimitry Andric       OS << "  " << Dest.getName() << ".val[" << K << "] = "
11920b57cec5SDimitry Andric          << "__builtin_shufflevector("
11930b57cec5SDimitry Andric          << Src.getName() << ".val[" << K << "], "
11940b57cec5SDimitry Andric          << Src.getName() << ".val[" << K << "]";
11950b57cec5SDimitry Andric       for (int J = Dest.getType().getNumElements() - 1; J >= 0; --J)
11960b57cec5SDimitry Andric         OS << ", " << J;
11970b57cec5SDimitry Andric       OS << ");";
11980b57cec5SDimitry Andric       emitNewLine();
11990b57cec5SDimitry Andric     }
12000b57cec5SDimitry Andric   } else {
12010b57cec5SDimitry Andric     OS << "  " << Dest.getName()
12020b57cec5SDimitry Andric        << " = __builtin_shufflevector(" << Src.getName() << ", " << Src.getName();
12030b57cec5SDimitry Andric     for (int J = Dest.getType().getNumElements() - 1; J >= 0; --J)
12040b57cec5SDimitry Andric       OS << ", " << J;
12050b57cec5SDimitry Andric     OS << ");";
12060b57cec5SDimitry Andric     emitNewLine();
12070b57cec5SDimitry Andric   }
12080b57cec5SDimitry Andric }
12090b57cec5SDimitry Andric 
12100b57cec5SDimitry Andric void Intrinsic::emitArgumentReversal() {
1211a7dea167SDimitry Andric   if (isBigEndianSafe())
12120b57cec5SDimitry Andric     return;
12130b57cec5SDimitry Andric 
12140b57cec5SDimitry Andric   // Reverse all vector arguments.
12150b57cec5SDimitry Andric   for (unsigned I = 0; I < getNumParams(); ++I) {
12160b57cec5SDimitry Andric     std::string Name = "p" + utostr(I);
12170b57cec5SDimitry Andric     std::string NewName = "rev" + utostr(I);
12180b57cec5SDimitry Andric 
12190b57cec5SDimitry Andric     Variable &V = Variables[Name];
12200b57cec5SDimitry Andric     Variable NewV(V.getType(), NewName + VariablePostfix);
12210b57cec5SDimitry Andric 
12220b57cec5SDimitry Andric     if (!NewV.getType().isVector() || NewV.getType().getNumElements() == 1)
12230b57cec5SDimitry Andric       continue;
12240b57cec5SDimitry Andric 
12250b57cec5SDimitry Andric     OS << "  " << NewV.getType().str() << " " << NewV.getName() << ";";
12260b57cec5SDimitry Andric     emitReverseVariable(NewV, V);
12270b57cec5SDimitry Andric     V = NewV;
12280b57cec5SDimitry Andric   }
12290b57cec5SDimitry Andric }
12300b57cec5SDimitry Andric 
12310b57cec5SDimitry Andric void Intrinsic::emitReturnReversal() {
1232a7dea167SDimitry Andric   if (isBigEndianSafe())
12330b57cec5SDimitry Andric     return;
12340b57cec5SDimitry Andric   if (!getReturnType().isVector() || getReturnType().isVoid() ||
12350b57cec5SDimitry Andric       getReturnType().getNumElements() == 1)
12360b57cec5SDimitry Andric     return;
12370b57cec5SDimitry Andric   emitReverseVariable(RetVar, RetVar);
12380b57cec5SDimitry Andric }
12390b57cec5SDimitry Andric 
12400b57cec5SDimitry Andric void Intrinsic::emitShadowedArgs() {
12410b57cec5SDimitry Andric   // Macro arguments are not type-checked like inline function arguments,
12420b57cec5SDimitry Andric   // so assign them to local temporaries to get the right type checking.
12430b57cec5SDimitry Andric   if (!UseMacro)
12440b57cec5SDimitry Andric     return;
12450b57cec5SDimitry Andric 
12460b57cec5SDimitry Andric   for (unsigned I = 0; I < getNumParams(); ++I) {
12470b57cec5SDimitry Andric     // Do not create a temporary for an immediate argument.
12480b57cec5SDimitry Andric     // That would defeat the whole point of using a macro!
1249480093f4SDimitry Andric     if (getParamType(I).isImmediate())
12500b57cec5SDimitry Andric       continue;
12510b57cec5SDimitry Andric     // Do not create a temporary for pointer arguments. The input
12520b57cec5SDimitry Andric     // pointer may have an alignment hint.
12530b57cec5SDimitry Andric     if (getParamType(I).isPointer())
12540b57cec5SDimitry Andric       continue;
12550b57cec5SDimitry Andric 
12560b57cec5SDimitry Andric     std::string Name = "p" + utostr(I);
12570b57cec5SDimitry Andric 
12580b57cec5SDimitry Andric     assert(Variables.find(Name) != Variables.end());
12590b57cec5SDimitry Andric     Variable &V = Variables[Name];
12600b57cec5SDimitry Andric 
12610b57cec5SDimitry Andric     std::string NewName = "s" + utostr(I);
12620b57cec5SDimitry Andric     Variable V2(V.getType(), NewName + VariablePostfix);
12630b57cec5SDimitry Andric 
12640b57cec5SDimitry Andric     OS << "  " << V2.getType().str() << " " << V2.getName() << " = "
12650b57cec5SDimitry Andric        << V.getName() << ";";
12660b57cec5SDimitry Andric     emitNewLine();
12670b57cec5SDimitry Andric 
12680b57cec5SDimitry Andric     V = V2;
12690b57cec5SDimitry Andric   }
12700b57cec5SDimitry Andric }
12710b57cec5SDimitry Andric 
12720b57cec5SDimitry Andric bool Intrinsic::protoHasScalar() const {
1273349cc55cSDimitry Andric   return llvm::any_of(
1274349cc55cSDimitry Andric       Types, [](const Type &T) { return T.isScalar() && !T.isImmediate(); });
12750b57cec5SDimitry Andric }
12760b57cec5SDimitry Andric 
12770b57cec5SDimitry Andric void Intrinsic::emitBodyAsBuiltinCall() {
12780b57cec5SDimitry Andric   std::string S;
12790b57cec5SDimitry Andric 
12800b57cec5SDimitry Andric   // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
12810b57cec5SDimitry Andric   // sret-like argument.
12820b57cec5SDimitry Andric   bool SRet = getReturnType().getNumVectors() >= 2;
12830b57cec5SDimitry Andric 
12840b57cec5SDimitry Andric   StringRef N = Name;
12850b57cec5SDimitry Andric   ClassKind LocalCK = CK;
12860b57cec5SDimitry Andric   if (!protoHasScalar())
12870b57cec5SDimitry Andric     LocalCK = ClassB;
12880b57cec5SDimitry Andric 
12890b57cec5SDimitry Andric   if (!getReturnType().isVoid() && !SRet)
12900b57cec5SDimitry Andric     S += "(" + RetVar.getType().str() + ") ";
12910b57cec5SDimitry Andric 
12925ffd83dbSDimitry Andric   S += "__builtin_neon_" + mangleName(std::string(N), LocalCK) + "(";
12930b57cec5SDimitry Andric 
12940b57cec5SDimitry Andric   if (SRet)
12950b57cec5SDimitry Andric     S += "&" + RetVar.getName() + ", ";
12960b57cec5SDimitry Andric 
12970b57cec5SDimitry Andric   for (unsigned I = 0; I < getNumParams(); ++I) {
12980b57cec5SDimitry Andric     Variable &V = Variables["p" + utostr(I)];
12990b57cec5SDimitry Andric     Type T = V.getType();
13000b57cec5SDimitry Andric 
13010b57cec5SDimitry Andric     // Handle multiple-vector values specially, emitting each subvector as an
13020b57cec5SDimitry Andric     // argument to the builtin.
13030b57cec5SDimitry Andric     if (T.getNumVectors() > 1) {
13040b57cec5SDimitry Andric       // Check if an explicit cast is needed.
13050b57cec5SDimitry Andric       std::string Cast;
1306a7dea167SDimitry Andric       if (LocalCK == ClassB) {
13070b57cec5SDimitry Andric         Type T2 = T;
13080b57cec5SDimitry Andric         T2.makeOneVector();
1309*04eeddc0SDimitry Andric         T2.makeInteger(8, /*Sign=*/true);
13100b57cec5SDimitry Andric         Cast = "(" + T2.str() + ")";
13110b57cec5SDimitry Andric       }
13120b57cec5SDimitry Andric 
13130b57cec5SDimitry Andric       for (unsigned J = 0; J < T.getNumVectors(); ++J)
13140b57cec5SDimitry Andric         S += Cast + V.getName() + ".val[" + utostr(J) + "], ";
13150b57cec5SDimitry Andric       continue;
13160b57cec5SDimitry Andric     }
13170b57cec5SDimitry Andric 
1318480093f4SDimitry Andric     std::string Arg = V.getName();
13190b57cec5SDimitry Andric     Type CastToType = T;
13200b57cec5SDimitry Andric 
13210b57cec5SDimitry Andric     // Check if an explicit cast is needed.
1322a7dea167SDimitry Andric     if (CastToType.isVector() &&
1323a7dea167SDimitry Andric         (LocalCK == ClassB || (T.isHalf() && !T.isScalarForMangling()))) {
13240b57cec5SDimitry Andric       CastToType.makeInteger(8, true);
13250b57cec5SDimitry Andric       Arg = "(" + CastToType.str() + ")" + Arg;
1326a7dea167SDimitry Andric     } else if (CastToType.isVector() && LocalCK == ClassI) {
1327480093f4SDimitry Andric       if (CastToType.isInteger())
1328a7dea167SDimitry Andric         CastToType.makeSigned();
1329a7dea167SDimitry Andric       Arg = "(" + CastToType.str() + ")" + Arg;
13300b57cec5SDimitry Andric     }
13310b57cec5SDimitry Andric 
13320b57cec5SDimitry Andric     S += Arg + ", ";
13330b57cec5SDimitry Andric   }
13340b57cec5SDimitry Andric 
13350b57cec5SDimitry Andric   // Extra constant integer to hold type class enum for this function, e.g. s8
13360b57cec5SDimitry Andric   if (getClassKind(true) == ClassB) {
1337480093f4SDimitry Andric     S += utostr(getPolymorphicKeyType().getNeonEnum());
13380b57cec5SDimitry Andric   } else {
13390b57cec5SDimitry Andric     // Remove extraneous ", ".
13400b57cec5SDimitry Andric     S.pop_back();
13410b57cec5SDimitry Andric     S.pop_back();
13420b57cec5SDimitry Andric   }
13430b57cec5SDimitry Andric   S += ");";
13440b57cec5SDimitry Andric 
13450b57cec5SDimitry Andric   std::string RetExpr;
13460b57cec5SDimitry Andric   if (!SRet && !RetVar.getType().isVoid())
13470b57cec5SDimitry Andric     RetExpr = RetVar.getName() + " = ";
13480b57cec5SDimitry Andric 
13490b57cec5SDimitry Andric   OS << "  " << RetExpr << S;
13500b57cec5SDimitry Andric   emitNewLine();
13510b57cec5SDimitry Andric }
13520b57cec5SDimitry Andric 
13530b57cec5SDimitry Andric void Intrinsic::emitBody(StringRef CallPrefix) {
13540b57cec5SDimitry Andric   std::vector<std::string> Lines;
13550b57cec5SDimitry Andric 
13560b57cec5SDimitry Andric   assert(RetVar.getType() == Types[0]);
13570b57cec5SDimitry Andric   // Create a return variable, if we're not void.
13580b57cec5SDimitry Andric   if (!RetVar.getType().isVoid()) {
13590b57cec5SDimitry Andric     OS << "  " << RetVar.getType().str() << " " << RetVar.getName() << ";";
13600b57cec5SDimitry Andric     emitNewLine();
13610b57cec5SDimitry Andric   }
13620b57cec5SDimitry Andric 
13630b57cec5SDimitry Andric   if (!Body || Body->getValues().empty()) {
13640b57cec5SDimitry Andric     // Nothing specific to output - must output a builtin.
13650b57cec5SDimitry Andric     emitBodyAsBuiltinCall();
13660b57cec5SDimitry Andric     return;
13670b57cec5SDimitry Andric   }
13680b57cec5SDimitry Andric 
13690b57cec5SDimitry Andric   // We have a list of "things to output". The last should be returned.
13700b57cec5SDimitry Andric   for (auto *I : Body->getValues()) {
13710b57cec5SDimitry Andric     if (StringInit *SI = dyn_cast<StringInit>(I)) {
13720b57cec5SDimitry Andric       Lines.push_back(replaceParamsIn(SI->getAsString()));
13730b57cec5SDimitry Andric     } else if (DagInit *DI = dyn_cast<DagInit>(I)) {
13740b57cec5SDimitry Andric       DagEmitter DE(*this, CallPrefix);
13750b57cec5SDimitry Andric       Lines.push_back(DE.emitDag(DI).second + ";");
13760b57cec5SDimitry Andric     }
13770b57cec5SDimitry Andric   }
13780b57cec5SDimitry Andric 
13790b57cec5SDimitry Andric   assert(!Lines.empty() && "Empty def?");
13800b57cec5SDimitry Andric   if (!RetVar.getType().isVoid())
13810b57cec5SDimitry Andric     Lines.back().insert(0, RetVar.getName() + " = ");
13820b57cec5SDimitry Andric 
13830b57cec5SDimitry Andric   for (auto &L : Lines) {
13840b57cec5SDimitry Andric     OS << "  " << L;
13850b57cec5SDimitry Andric     emitNewLine();
13860b57cec5SDimitry Andric   }
13870b57cec5SDimitry Andric }
13880b57cec5SDimitry Andric 
13890b57cec5SDimitry Andric void Intrinsic::emitReturn() {
13900b57cec5SDimitry Andric   if (RetVar.getType().isVoid())
13910b57cec5SDimitry Andric     return;
13920b57cec5SDimitry Andric   if (UseMacro)
13930b57cec5SDimitry Andric     OS << "  " << RetVar.getName() << ";";
13940b57cec5SDimitry Andric   else
13950b57cec5SDimitry Andric     OS << "  return " << RetVar.getName() << ";";
13960b57cec5SDimitry Andric   emitNewLine();
13970b57cec5SDimitry Andric }
13980b57cec5SDimitry Andric 
13990b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDag(DagInit *DI) {
14000b57cec5SDimitry Andric   // At this point we should only be seeing a def.
14010b57cec5SDimitry Andric   DefInit *DefI = cast<DefInit>(DI->getOperator());
14020b57cec5SDimitry Andric   std::string Op = DefI->getAsString();
14030b57cec5SDimitry Andric 
14040b57cec5SDimitry Andric   if (Op == "cast" || Op == "bitcast")
14050b57cec5SDimitry Andric     return emitDagCast(DI, Op == "bitcast");
14060b57cec5SDimitry Andric   if (Op == "shuffle")
14070b57cec5SDimitry Andric     return emitDagShuffle(DI);
14080b57cec5SDimitry Andric   if (Op == "dup")
14090b57cec5SDimitry Andric     return emitDagDup(DI);
14100b57cec5SDimitry Andric   if (Op == "dup_typed")
14110b57cec5SDimitry Andric     return emitDagDupTyped(DI);
14120b57cec5SDimitry Andric   if (Op == "splat")
14130b57cec5SDimitry Andric     return emitDagSplat(DI);
14140b57cec5SDimitry Andric   if (Op == "save_temp")
14150b57cec5SDimitry Andric     return emitDagSaveTemp(DI);
14160b57cec5SDimitry Andric   if (Op == "op")
14170b57cec5SDimitry Andric     return emitDagOp(DI);
14185ffd83dbSDimitry Andric   if (Op == "call" || Op == "call_mangled")
14195ffd83dbSDimitry Andric     return emitDagCall(DI, Op == "call_mangled");
14200b57cec5SDimitry Andric   if (Op == "name_replace")
14210b57cec5SDimitry Andric     return emitDagNameReplace(DI);
14220b57cec5SDimitry Andric   if (Op == "literal")
14230b57cec5SDimitry Andric     return emitDagLiteral(DI);
14240b57cec5SDimitry Andric   assert_with_loc(false, "Unknown operation!");
14250b57cec5SDimitry Andric   return std::make_pair(Type::getVoid(), "");
14260b57cec5SDimitry Andric }
14270b57cec5SDimitry Andric 
14280b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagOp(DagInit *DI) {
14290b57cec5SDimitry Andric   std::string Op = cast<StringInit>(DI->getArg(0))->getAsUnquotedString();
14300b57cec5SDimitry Andric   if (DI->getNumArgs() == 2) {
14310b57cec5SDimitry Andric     // Unary op.
14320b57cec5SDimitry Andric     std::pair<Type, std::string> R =
14335ffd83dbSDimitry Andric         emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1)));
14340b57cec5SDimitry Andric     return std::make_pair(R.first, Op + R.second);
14350b57cec5SDimitry Andric   } else {
14360b57cec5SDimitry Andric     assert(DI->getNumArgs() == 3 && "Can only handle unary and binary ops!");
14370b57cec5SDimitry Andric     std::pair<Type, std::string> R1 =
14385ffd83dbSDimitry Andric         emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1)));
14390b57cec5SDimitry Andric     std::pair<Type, std::string> R2 =
14405ffd83dbSDimitry Andric         emitDagArg(DI->getArg(2), std::string(DI->getArgNameStr(2)));
14410b57cec5SDimitry Andric     assert_with_loc(R1.first == R2.first, "Argument type mismatch!");
14420b57cec5SDimitry Andric     return std::make_pair(R1.first, R1.second + " " + Op + " " + R2.second);
14430b57cec5SDimitry Andric   }
14440b57cec5SDimitry Andric }
14450b57cec5SDimitry Andric 
14465ffd83dbSDimitry Andric std::pair<Type, std::string>
14475ffd83dbSDimitry Andric Intrinsic::DagEmitter::emitDagCall(DagInit *DI, bool MatchMangledName) {
14480b57cec5SDimitry Andric   std::vector<Type> Types;
14490b57cec5SDimitry Andric   std::vector<std::string> Values;
14500b57cec5SDimitry Andric   for (unsigned I = 0; I < DI->getNumArgs() - 1; ++I) {
14510b57cec5SDimitry Andric     std::pair<Type, std::string> R =
14525ffd83dbSDimitry Andric         emitDagArg(DI->getArg(I + 1), std::string(DI->getArgNameStr(I + 1)));
14530b57cec5SDimitry Andric     Types.push_back(R.first);
14540b57cec5SDimitry Andric     Values.push_back(R.second);
14550b57cec5SDimitry Andric   }
14560b57cec5SDimitry Andric 
14570b57cec5SDimitry Andric   // Look up the called intrinsic.
14580b57cec5SDimitry Andric   std::string N;
14590b57cec5SDimitry Andric   if (StringInit *SI = dyn_cast<StringInit>(DI->getArg(0)))
14600b57cec5SDimitry Andric     N = SI->getAsUnquotedString();
14610b57cec5SDimitry Andric   else
14620b57cec5SDimitry Andric     N = emitDagArg(DI->getArg(0), "").second;
14635ffd83dbSDimitry Andric   Optional<std::string> MangledName;
14645ffd83dbSDimitry Andric   if (MatchMangledName) {
14655ffd83dbSDimitry Andric     if (Intr.getRecord()->getValueAsBit("isLaneQ"))
14665ffd83dbSDimitry Andric       N += "q";
14675ffd83dbSDimitry Andric     MangledName = Intr.mangleName(N, ClassS);
14685ffd83dbSDimitry Andric   }
14695ffd83dbSDimitry Andric   Intrinsic &Callee = Intr.Emitter.getIntrinsic(N, Types, MangledName);
14700b57cec5SDimitry Andric 
14710b57cec5SDimitry Andric   // Make sure the callee is known as an early def.
14720b57cec5SDimitry Andric   Callee.setNeededEarly();
14730b57cec5SDimitry Andric   Intr.Dependencies.insert(&Callee);
14740b57cec5SDimitry Andric 
14750b57cec5SDimitry Andric   // Now create the call itself.
1476*04eeddc0SDimitry Andric   std::string S;
1477a7dea167SDimitry Andric   if (!Callee.isBigEndianSafe())
1478a7dea167SDimitry Andric     S += CallPrefix.str();
1479a7dea167SDimitry Andric   S += Callee.getMangledName(true) + "(";
14800b57cec5SDimitry Andric   for (unsigned I = 0; I < DI->getNumArgs() - 1; ++I) {
14810b57cec5SDimitry Andric     if (I != 0)
14820b57cec5SDimitry Andric       S += ", ";
14830b57cec5SDimitry Andric     S += Values[I];
14840b57cec5SDimitry Andric   }
14850b57cec5SDimitry Andric   S += ")";
14860b57cec5SDimitry Andric 
14870b57cec5SDimitry Andric   return std::make_pair(Callee.getReturnType(), S);
14880b57cec5SDimitry Andric }
14890b57cec5SDimitry Andric 
14900b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagCast(DagInit *DI,
14910b57cec5SDimitry Andric                                                                 bool IsBitCast){
14920b57cec5SDimitry Andric   // (cast MOD* VAL) -> cast VAL to type given by MOD.
14935ffd83dbSDimitry Andric   std::pair<Type, std::string> R =
14945ffd83dbSDimitry Andric       emitDagArg(DI->getArg(DI->getNumArgs() - 1),
14955ffd83dbSDimitry Andric                  std::string(DI->getArgNameStr(DI->getNumArgs() - 1)));
14960b57cec5SDimitry Andric   Type castToType = R.first;
14970b57cec5SDimitry Andric   for (unsigned ArgIdx = 0; ArgIdx < DI->getNumArgs() - 1; ++ArgIdx) {
14980b57cec5SDimitry Andric 
14990b57cec5SDimitry Andric     // MOD can take several forms:
15000b57cec5SDimitry Andric     //   1. $X - take the type of parameter / variable X.
15010b57cec5SDimitry Andric     //   2. The value "R" - take the type of the return type.
15020b57cec5SDimitry Andric     //   3. a type string
15030b57cec5SDimitry Andric     //   4. The value "U" or "S" to switch the signedness.
15040b57cec5SDimitry Andric     //   5. The value "H" or "D" to half or double the bitwidth.
15050b57cec5SDimitry Andric     //   6. The value "8" to convert to 8-bit (signed) integer lanes.
15060b57cec5SDimitry Andric     if (!DI->getArgNameStr(ArgIdx).empty()) {
15075ffd83dbSDimitry Andric       assert_with_loc(Intr.Variables.find(std::string(
15085ffd83dbSDimitry Andric                           DI->getArgNameStr(ArgIdx))) != Intr.Variables.end(),
15090b57cec5SDimitry Andric                       "Variable not found");
15105ffd83dbSDimitry Andric       castToType =
15115ffd83dbSDimitry Andric           Intr.Variables[std::string(DI->getArgNameStr(ArgIdx))].getType();
15120b57cec5SDimitry Andric     } else {
15130b57cec5SDimitry Andric       StringInit *SI = dyn_cast<StringInit>(DI->getArg(ArgIdx));
15140b57cec5SDimitry Andric       assert_with_loc(SI, "Expected string type or $Name for cast type");
15150b57cec5SDimitry Andric 
15160b57cec5SDimitry Andric       if (SI->getAsUnquotedString() == "R") {
15170b57cec5SDimitry Andric         castToType = Intr.getReturnType();
15180b57cec5SDimitry Andric       } else if (SI->getAsUnquotedString() == "U") {
15190b57cec5SDimitry Andric         castToType.makeUnsigned();
15200b57cec5SDimitry Andric       } else if (SI->getAsUnquotedString() == "S") {
15210b57cec5SDimitry Andric         castToType.makeSigned();
15220b57cec5SDimitry Andric       } else if (SI->getAsUnquotedString() == "H") {
15230b57cec5SDimitry Andric         castToType.halveLanes();
15240b57cec5SDimitry Andric       } else if (SI->getAsUnquotedString() == "D") {
15250b57cec5SDimitry Andric         castToType.doubleLanes();
15260b57cec5SDimitry Andric       } else if (SI->getAsUnquotedString() == "8") {
15270b57cec5SDimitry Andric         castToType.makeInteger(8, true);
15285ffd83dbSDimitry Andric       } else if (SI->getAsUnquotedString() == "32") {
15295ffd83dbSDimitry Andric         castToType.make32BitElement();
15300b57cec5SDimitry Andric       } else {
15310b57cec5SDimitry Andric         castToType = Type::fromTypedefName(SI->getAsUnquotedString());
15320b57cec5SDimitry Andric         assert_with_loc(!castToType.isVoid(), "Unknown typedef");
15330b57cec5SDimitry Andric       }
15340b57cec5SDimitry Andric     }
15350b57cec5SDimitry Andric   }
15360b57cec5SDimitry Andric 
15370b57cec5SDimitry Andric   std::string S;
15380b57cec5SDimitry Andric   if (IsBitCast) {
15390b57cec5SDimitry Andric     // Emit a reinterpret cast. The second operand must be an lvalue, so create
15400b57cec5SDimitry Andric     // a temporary.
15410b57cec5SDimitry Andric     std::string N = "reint";
15420b57cec5SDimitry Andric     unsigned I = 0;
15430b57cec5SDimitry Andric     while (Intr.Variables.find(N) != Intr.Variables.end())
15440b57cec5SDimitry Andric       N = "reint" + utostr(++I);
15450b57cec5SDimitry Andric     Intr.Variables[N] = Variable(R.first, N + Intr.VariablePostfix);
15460b57cec5SDimitry Andric 
15470b57cec5SDimitry Andric     Intr.OS << R.first.str() << " " << Intr.Variables[N].getName() << " = "
15480b57cec5SDimitry Andric             << R.second << ";";
15490b57cec5SDimitry Andric     Intr.emitNewLine();
15500b57cec5SDimitry Andric 
15510b57cec5SDimitry Andric     S = "*(" + castToType.str() + " *) &" + Intr.Variables[N].getName() + "";
15520b57cec5SDimitry Andric   } else {
15530b57cec5SDimitry Andric     // Emit a normal (static) cast.
15540b57cec5SDimitry Andric     S = "(" + castToType.str() + ")(" + R.second + ")";
15550b57cec5SDimitry Andric   }
15560b57cec5SDimitry Andric 
15570b57cec5SDimitry Andric   return std::make_pair(castToType, S);
15580b57cec5SDimitry Andric }
15590b57cec5SDimitry Andric 
15600b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagShuffle(DagInit *DI){
15610b57cec5SDimitry Andric   // See the documentation in arm_neon.td for a description of these operators.
15620b57cec5SDimitry Andric   class LowHalf : public SetTheory::Operator {
15630b57cec5SDimitry Andric   public:
15640b57cec5SDimitry Andric     void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
15650b57cec5SDimitry Andric                ArrayRef<SMLoc> Loc) override {
15660b57cec5SDimitry Andric       SetTheory::RecSet Elts2;
15670b57cec5SDimitry Andric       ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts2, Loc);
15680b57cec5SDimitry Andric       Elts.insert(Elts2.begin(), Elts2.begin() + (Elts2.size() / 2));
15690b57cec5SDimitry Andric     }
15700b57cec5SDimitry Andric   };
15710b57cec5SDimitry Andric 
15720b57cec5SDimitry Andric   class HighHalf : public SetTheory::Operator {
15730b57cec5SDimitry Andric   public:
15740b57cec5SDimitry Andric     void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
15750b57cec5SDimitry Andric                ArrayRef<SMLoc> Loc) override {
15760b57cec5SDimitry Andric       SetTheory::RecSet Elts2;
15770b57cec5SDimitry Andric       ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts2, Loc);
15780b57cec5SDimitry Andric       Elts.insert(Elts2.begin() + (Elts2.size() / 2), Elts2.end());
15790b57cec5SDimitry Andric     }
15800b57cec5SDimitry Andric   };
15810b57cec5SDimitry Andric 
15820b57cec5SDimitry Andric   class Rev : public SetTheory::Operator {
15830b57cec5SDimitry Andric     unsigned ElementSize;
15840b57cec5SDimitry Andric 
15850b57cec5SDimitry Andric   public:
15860b57cec5SDimitry Andric     Rev(unsigned ElementSize) : ElementSize(ElementSize) {}
15870b57cec5SDimitry Andric 
15880b57cec5SDimitry Andric     void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
15890b57cec5SDimitry Andric                ArrayRef<SMLoc> Loc) override {
15900b57cec5SDimitry Andric       SetTheory::RecSet Elts2;
15910b57cec5SDimitry Andric       ST.evaluate(Expr->arg_begin() + 1, Expr->arg_end(), Elts2, Loc);
15920b57cec5SDimitry Andric 
15930b57cec5SDimitry Andric       int64_t VectorSize = cast<IntInit>(Expr->getArg(0))->getValue();
15940b57cec5SDimitry Andric       VectorSize /= ElementSize;
15950b57cec5SDimitry Andric 
15960b57cec5SDimitry Andric       std::vector<Record *> Revved;
15970b57cec5SDimitry Andric       for (unsigned VI = 0; VI < Elts2.size(); VI += VectorSize) {
15980b57cec5SDimitry Andric         for (int LI = VectorSize - 1; LI >= 0; --LI) {
15990b57cec5SDimitry Andric           Revved.push_back(Elts2[VI + LI]);
16000b57cec5SDimitry Andric         }
16010b57cec5SDimitry Andric       }
16020b57cec5SDimitry Andric 
16030b57cec5SDimitry Andric       Elts.insert(Revved.begin(), Revved.end());
16040b57cec5SDimitry Andric     }
16050b57cec5SDimitry Andric   };
16060b57cec5SDimitry Andric 
16070b57cec5SDimitry Andric   class MaskExpander : public SetTheory::Expander {
16080b57cec5SDimitry Andric     unsigned N;
16090b57cec5SDimitry Andric 
16100b57cec5SDimitry Andric   public:
16110b57cec5SDimitry Andric     MaskExpander(unsigned N) : N(N) {}
16120b57cec5SDimitry Andric 
16130b57cec5SDimitry Andric     void expand(SetTheory &ST, Record *R, SetTheory::RecSet &Elts) override {
16140b57cec5SDimitry Andric       unsigned Addend = 0;
16150b57cec5SDimitry Andric       if (R->getName() == "mask0")
16160b57cec5SDimitry Andric         Addend = 0;
16170b57cec5SDimitry Andric       else if (R->getName() == "mask1")
16180b57cec5SDimitry Andric         Addend = N;
16190b57cec5SDimitry Andric       else
16200b57cec5SDimitry Andric         return;
16210b57cec5SDimitry Andric       for (unsigned I = 0; I < N; ++I)
16220b57cec5SDimitry Andric         Elts.insert(R->getRecords().getDef("sv" + utostr(I + Addend)));
16230b57cec5SDimitry Andric     }
16240b57cec5SDimitry Andric   };
16250b57cec5SDimitry Andric 
16260b57cec5SDimitry Andric   // (shuffle arg1, arg2, sequence)
16270b57cec5SDimitry Andric   std::pair<Type, std::string> Arg1 =
16285ffd83dbSDimitry Andric       emitDagArg(DI->getArg(0), std::string(DI->getArgNameStr(0)));
16290b57cec5SDimitry Andric   std::pair<Type, std::string> Arg2 =
16305ffd83dbSDimitry Andric       emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1)));
16310b57cec5SDimitry Andric   assert_with_loc(Arg1.first == Arg2.first,
16320b57cec5SDimitry Andric                   "Different types in arguments to shuffle!");
16330b57cec5SDimitry Andric 
16340b57cec5SDimitry Andric   SetTheory ST;
16350b57cec5SDimitry Andric   SetTheory::RecSet Elts;
1636a7dea167SDimitry Andric   ST.addOperator("lowhalf", std::make_unique<LowHalf>());
1637a7dea167SDimitry Andric   ST.addOperator("highhalf", std::make_unique<HighHalf>());
16380b57cec5SDimitry Andric   ST.addOperator("rev",
1639a7dea167SDimitry Andric                  std::make_unique<Rev>(Arg1.first.getElementSizeInBits()));
16400b57cec5SDimitry Andric   ST.addExpander("MaskExpand",
1641a7dea167SDimitry Andric                  std::make_unique<MaskExpander>(Arg1.first.getNumElements()));
16420b57cec5SDimitry Andric   ST.evaluate(DI->getArg(2), Elts, None);
16430b57cec5SDimitry Andric 
16440b57cec5SDimitry Andric   std::string S = "__builtin_shufflevector(" + Arg1.second + ", " + Arg2.second;
16450b57cec5SDimitry Andric   for (auto &E : Elts) {
16460b57cec5SDimitry Andric     StringRef Name = E->getName();
16470b57cec5SDimitry Andric     assert_with_loc(Name.startswith("sv"),
16480b57cec5SDimitry Andric                     "Incorrect element kind in shuffle mask!");
16490b57cec5SDimitry Andric     S += ", " + Name.drop_front(2).str();
16500b57cec5SDimitry Andric   }
16510b57cec5SDimitry Andric   S += ")";
16520b57cec5SDimitry Andric 
16530b57cec5SDimitry Andric   // Recalculate the return type - the shuffle may have halved or doubled it.
16540b57cec5SDimitry Andric   Type T(Arg1.first);
16550b57cec5SDimitry Andric   if (Elts.size() > T.getNumElements()) {
16560b57cec5SDimitry Andric     assert_with_loc(
16570b57cec5SDimitry Andric         Elts.size() == T.getNumElements() * 2,
16580b57cec5SDimitry Andric         "Can only double or half the number of elements in a shuffle!");
16590b57cec5SDimitry Andric     T.doubleLanes();
16600b57cec5SDimitry Andric   } else if (Elts.size() < T.getNumElements()) {
16610b57cec5SDimitry Andric     assert_with_loc(
16620b57cec5SDimitry Andric         Elts.size() == T.getNumElements() / 2,
16630b57cec5SDimitry Andric         "Can only double or half the number of elements in a shuffle!");
16640b57cec5SDimitry Andric     T.halveLanes();
16650b57cec5SDimitry Andric   }
16660b57cec5SDimitry Andric 
16670b57cec5SDimitry Andric   return std::make_pair(T, S);
16680b57cec5SDimitry Andric }
16690b57cec5SDimitry Andric 
16700b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagDup(DagInit *DI) {
16710b57cec5SDimitry Andric   assert_with_loc(DI->getNumArgs() == 1, "dup() expects one argument");
16725ffd83dbSDimitry Andric   std::pair<Type, std::string> A =
16735ffd83dbSDimitry Andric       emitDagArg(DI->getArg(0), std::string(DI->getArgNameStr(0)));
16740b57cec5SDimitry Andric   assert_with_loc(A.first.isScalar(), "dup() expects a scalar argument");
16750b57cec5SDimitry Andric 
16760b57cec5SDimitry Andric   Type T = Intr.getBaseType();
16770b57cec5SDimitry Andric   assert_with_loc(T.isVector(), "dup() used but default type is scalar!");
16780b57cec5SDimitry Andric   std::string S = "(" + T.str() + ") {";
16790b57cec5SDimitry Andric   for (unsigned I = 0; I < T.getNumElements(); ++I) {
16800b57cec5SDimitry Andric     if (I != 0)
16810b57cec5SDimitry Andric       S += ", ";
16820b57cec5SDimitry Andric     S += A.second;
16830b57cec5SDimitry Andric   }
16840b57cec5SDimitry Andric   S += "}";
16850b57cec5SDimitry Andric 
16860b57cec5SDimitry Andric   return std::make_pair(T, S);
16870b57cec5SDimitry Andric }
16880b57cec5SDimitry Andric 
16890b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagDupTyped(DagInit *DI) {
16900b57cec5SDimitry Andric   assert_with_loc(DI->getNumArgs() == 2, "dup_typed() expects two arguments");
16915ffd83dbSDimitry Andric   std::pair<Type, std::string> B =
16925ffd83dbSDimitry Andric       emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1)));
16930b57cec5SDimitry Andric   assert_with_loc(B.first.isScalar(),
16940b57cec5SDimitry Andric                   "dup_typed() requires a scalar as the second argument");
1695e8d8bef9SDimitry Andric   Type T;
1696e8d8bef9SDimitry Andric   // If the type argument is a constant string, construct the type directly.
1697e8d8bef9SDimitry Andric   if (StringInit *SI = dyn_cast<StringInit>(DI->getArg(0))) {
1698e8d8bef9SDimitry Andric     T = Type::fromTypedefName(SI->getAsUnquotedString());
1699e8d8bef9SDimitry Andric     assert_with_loc(!T.isVoid(), "Unknown typedef");
1700e8d8bef9SDimitry Andric   } else
1701e8d8bef9SDimitry Andric     T = emitDagArg(DI->getArg(0), std::string(DI->getArgNameStr(0))).first;
17020b57cec5SDimitry Andric 
17030b57cec5SDimitry Andric   assert_with_loc(T.isVector(), "dup_typed() used but target type is scalar!");
17040b57cec5SDimitry Andric   std::string S = "(" + T.str() + ") {";
17050b57cec5SDimitry Andric   for (unsigned I = 0; I < T.getNumElements(); ++I) {
17060b57cec5SDimitry Andric     if (I != 0)
17070b57cec5SDimitry Andric       S += ", ";
17080b57cec5SDimitry Andric     S += B.second;
17090b57cec5SDimitry Andric   }
17100b57cec5SDimitry Andric   S += "}";
17110b57cec5SDimitry Andric 
17120b57cec5SDimitry Andric   return std::make_pair(T, S);
17130b57cec5SDimitry Andric }
17140b57cec5SDimitry Andric 
17150b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagSplat(DagInit *DI) {
17160b57cec5SDimitry Andric   assert_with_loc(DI->getNumArgs() == 2, "splat() expects two arguments");
17175ffd83dbSDimitry Andric   std::pair<Type, std::string> A =
17185ffd83dbSDimitry Andric       emitDagArg(DI->getArg(0), std::string(DI->getArgNameStr(0)));
17195ffd83dbSDimitry Andric   std::pair<Type, std::string> B =
17205ffd83dbSDimitry Andric       emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1)));
17210b57cec5SDimitry Andric 
17220b57cec5SDimitry Andric   assert_with_loc(B.first.isScalar(),
17230b57cec5SDimitry Andric                   "splat() requires a scalar int as the second argument");
17240b57cec5SDimitry Andric 
17250b57cec5SDimitry Andric   std::string S = "__builtin_shufflevector(" + A.second + ", " + A.second;
17260b57cec5SDimitry Andric   for (unsigned I = 0; I < Intr.getBaseType().getNumElements(); ++I) {
17270b57cec5SDimitry Andric     S += ", " + B.second;
17280b57cec5SDimitry Andric   }
17290b57cec5SDimitry Andric   S += ")";
17300b57cec5SDimitry Andric 
17310b57cec5SDimitry Andric   return std::make_pair(Intr.getBaseType(), S);
17320b57cec5SDimitry Andric }
17330b57cec5SDimitry Andric 
17340b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagSaveTemp(DagInit *DI) {
17350b57cec5SDimitry Andric   assert_with_loc(DI->getNumArgs() == 2, "save_temp() expects two arguments");
17365ffd83dbSDimitry Andric   std::pair<Type, std::string> A =
17375ffd83dbSDimitry Andric       emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1)));
17380b57cec5SDimitry Andric 
17390b57cec5SDimitry Andric   assert_with_loc(!A.first.isVoid(),
17400b57cec5SDimitry Andric                   "Argument to save_temp() must have non-void type!");
17410b57cec5SDimitry Andric 
17425ffd83dbSDimitry Andric   std::string N = std::string(DI->getArgNameStr(0));
17430b57cec5SDimitry Andric   assert_with_loc(!N.empty(),
17440b57cec5SDimitry Andric                   "save_temp() expects a name as the first argument");
17450b57cec5SDimitry Andric 
17460b57cec5SDimitry Andric   assert_with_loc(Intr.Variables.find(N) == Intr.Variables.end(),
17470b57cec5SDimitry Andric                   "Variable already defined!");
17480b57cec5SDimitry Andric   Intr.Variables[N] = Variable(A.first, N + Intr.VariablePostfix);
17490b57cec5SDimitry Andric 
17500b57cec5SDimitry Andric   std::string S =
17510b57cec5SDimitry Andric       A.first.str() + " " + Intr.Variables[N].getName() + " = " + A.second;
17520b57cec5SDimitry Andric 
17530b57cec5SDimitry Andric   return std::make_pair(Type::getVoid(), S);
17540b57cec5SDimitry Andric }
17550b57cec5SDimitry Andric 
17560b57cec5SDimitry Andric std::pair<Type, std::string>
17570b57cec5SDimitry Andric Intrinsic::DagEmitter::emitDagNameReplace(DagInit *DI) {
17580b57cec5SDimitry Andric   std::string S = Intr.Name;
17590b57cec5SDimitry Andric 
17600b57cec5SDimitry Andric   assert_with_loc(DI->getNumArgs() == 2, "name_replace requires 2 arguments!");
17610b57cec5SDimitry Andric   std::string ToReplace = cast<StringInit>(DI->getArg(0))->getAsUnquotedString();
17620b57cec5SDimitry Andric   std::string ReplaceWith = cast<StringInit>(DI->getArg(1))->getAsUnquotedString();
17630b57cec5SDimitry Andric 
17640b57cec5SDimitry Andric   size_t Idx = S.find(ToReplace);
17650b57cec5SDimitry Andric 
17660b57cec5SDimitry Andric   assert_with_loc(Idx != std::string::npos, "name should contain '" + ToReplace + "'!");
17670b57cec5SDimitry Andric   S.replace(Idx, ToReplace.size(), ReplaceWith);
17680b57cec5SDimitry Andric 
17690b57cec5SDimitry Andric   return std::make_pair(Type::getVoid(), S);
17700b57cec5SDimitry Andric }
17710b57cec5SDimitry Andric 
17720b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagLiteral(DagInit *DI){
17730b57cec5SDimitry Andric   std::string Ty = cast<StringInit>(DI->getArg(0))->getAsUnquotedString();
17740b57cec5SDimitry Andric   std::string Value = cast<StringInit>(DI->getArg(1))->getAsUnquotedString();
17750b57cec5SDimitry Andric   return std::make_pair(Type::fromTypedefName(Ty), Value);
17760b57cec5SDimitry Andric }
17770b57cec5SDimitry Andric 
17780b57cec5SDimitry Andric std::pair<Type, std::string>
17790b57cec5SDimitry Andric Intrinsic::DagEmitter::emitDagArg(Init *Arg, std::string ArgName) {
17800b57cec5SDimitry Andric   if (!ArgName.empty()) {
17810b57cec5SDimitry Andric     assert_with_loc(!Arg->isComplete(),
17820b57cec5SDimitry Andric                     "Arguments must either be DAGs or names, not both!");
17830b57cec5SDimitry Andric     assert_with_loc(Intr.Variables.find(ArgName) != Intr.Variables.end(),
17840b57cec5SDimitry Andric                     "Variable not defined!");
17850b57cec5SDimitry Andric     Variable &V = Intr.Variables[ArgName];
17860b57cec5SDimitry Andric     return std::make_pair(V.getType(), V.getName());
17870b57cec5SDimitry Andric   }
17880b57cec5SDimitry Andric 
17890b57cec5SDimitry Andric   assert(Arg && "Neither ArgName nor Arg?!");
17900b57cec5SDimitry Andric   DagInit *DI = dyn_cast<DagInit>(Arg);
17910b57cec5SDimitry Andric   assert_with_loc(DI, "Arguments must either be DAGs or names!");
17920b57cec5SDimitry Andric 
17930b57cec5SDimitry Andric   return emitDag(DI);
17940b57cec5SDimitry Andric }
17950b57cec5SDimitry Andric 
17960b57cec5SDimitry Andric std::string Intrinsic::generate() {
1797a7dea167SDimitry Andric   // Avoid duplicated code for big and little endian
1798a7dea167SDimitry Andric   if (isBigEndianSafe()) {
1799a7dea167SDimitry Andric     generateImpl(false, "", "");
1800a7dea167SDimitry Andric     return OS.str();
1801a7dea167SDimitry Andric   }
18020b57cec5SDimitry Andric   // Little endian intrinsics are simple and don't require any argument
18030b57cec5SDimitry Andric   // swapping.
18040b57cec5SDimitry Andric   OS << "#ifdef __LITTLE_ENDIAN__\n";
18050b57cec5SDimitry Andric 
18060b57cec5SDimitry Andric   generateImpl(false, "", "");
18070b57cec5SDimitry Andric 
18080b57cec5SDimitry Andric   OS << "#else\n";
18090b57cec5SDimitry Andric 
18100b57cec5SDimitry Andric   // Big endian intrinsics are more complex. The user intended these
18110b57cec5SDimitry Andric   // intrinsics to operate on a vector "as-if" loaded by (V)LDR,
18120b57cec5SDimitry Andric   // but we load as-if (V)LD1. So we should swap all arguments and
18130b57cec5SDimitry Andric   // swap the return value too.
18140b57cec5SDimitry Andric   //
18150b57cec5SDimitry Andric   // If we call sub-intrinsics, we should call a version that does
18160b57cec5SDimitry Andric   // not re-swap the arguments!
18170b57cec5SDimitry Andric   generateImpl(true, "", "__noswap_");
18180b57cec5SDimitry Andric 
18190b57cec5SDimitry Andric   // If we're needed early, create a non-swapping variant for
18200b57cec5SDimitry Andric   // big-endian.
18210b57cec5SDimitry Andric   if (NeededEarly) {
18220b57cec5SDimitry Andric     generateImpl(false, "__noswap_", "__noswap_");
18230b57cec5SDimitry Andric   }
18240b57cec5SDimitry Andric   OS << "#endif\n\n";
18250b57cec5SDimitry Andric 
18260b57cec5SDimitry Andric   return OS.str();
18270b57cec5SDimitry Andric }
18280b57cec5SDimitry Andric 
18290b57cec5SDimitry Andric void Intrinsic::generateImpl(bool ReverseArguments,
18300b57cec5SDimitry Andric                              StringRef NamePrefix, StringRef CallPrefix) {
18310b57cec5SDimitry Andric   CurrentRecord = R;
18320b57cec5SDimitry Andric 
18330b57cec5SDimitry Andric   // If we call a macro, our local variables may be corrupted due to
18340b57cec5SDimitry Andric   // lack of proper lexical scoping. So, add a globally unique postfix
18350b57cec5SDimitry Andric   // to every variable.
18360b57cec5SDimitry Andric   //
18370b57cec5SDimitry Andric   // indexBody() should have set up the Dependencies set by now.
18380b57cec5SDimitry Andric   for (auto *I : Dependencies)
18390b57cec5SDimitry Andric     if (I->UseMacro) {
18400b57cec5SDimitry Andric       VariablePostfix = "_" + utostr(Emitter.getUniqueNumber());
18410b57cec5SDimitry Andric       break;
18420b57cec5SDimitry Andric     }
18430b57cec5SDimitry Andric 
18440b57cec5SDimitry Andric   initVariables();
18450b57cec5SDimitry Andric 
18460b57cec5SDimitry Andric   emitPrototype(NamePrefix);
18470b57cec5SDimitry Andric 
18480b57cec5SDimitry Andric   if (IsUnavailable) {
18490b57cec5SDimitry Andric     OS << " __attribute__((unavailable));";
18500b57cec5SDimitry Andric   } else {
18510b57cec5SDimitry Andric     emitOpeningBrace();
18520b57cec5SDimitry Andric     emitShadowedArgs();
18530b57cec5SDimitry Andric     if (ReverseArguments)
18540b57cec5SDimitry Andric       emitArgumentReversal();
18550b57cec5SDimitry Andric     emitBody(CallPrefix);
18560b57cec5SDimitry Andric     if (ReverseArguments)
18570b57cec5SDimitry Andric       emitReturnReversal();
18580b57cec5SDimitry Andric     emitReturn();
18590b57cec5SDimitry Andric     emitClosingBrace();
18600b57cec5SDimitry Andric   }
18610b57cec5SDimitry Andric   OS << "\n";
18620b57cec5SDimitry Andric 
18630b57cec5SDimitry Andric   CurrentRecord = nullptr;
18640b57cec5SDimitry Andric }
18650b57cec5SDimitry Andric 
18660b57cec5SDimitry Andric void Intrinsic::indexBody() {
18670b57cec5SDimitry Andric   CurrentRecord = R;
18680b57cec5SDimitry Andric 
18690b57cec5SDimitry Andric   initVariables();
18700b57cec5SDimitry Andric   emitBody("");
18710b57cec5SDimitry Andric   OS.str("");
18720b57cec5SDimitry Andric 
18730b57cec5SDimitry Andric   CurrentRecord = nullptr;
18740b57cec5SDimitry Andric }
18750b57cec5SDimitry Andric 
18760b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
18770b57cec5SDimitry Andric // NeonEmitter implementation
18780b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
18790b57cec5SDimitry Andric 
18805ffd83dbSDimitry Andric Intrinsic &NeonEmitter::getIntrinsic(StringRef Name, ArrayRef<Type> Types,
18815ffd83dbSDimitry Andric                                      Optional<std::string> MangledName) {
18820b57cec5SDimitry Andric   // First, look up the name in the intrinsic map.
18830b57cec5SDimitry Andric   assert_with_loc(IntrinsicMap.find(Name.str()) != IntrinsicMap.end(),
18840b57cec5SDimitry Andric                   ("Intrinsic '" + Name + "' not found!").str());
18850b57cec5SDimitry Andric   auto &V = IntrinsicMap.find(Name.str())->second;
18860b57cec5SDimitry Andric   std::vector<Intrinsic *> GoodVec;
18870b57cec5SDimitry Andric 
18880b57cec5SDimitry Andric   // Create a string to print if we end up failing.
18890b57cec5SDimitry Andric   std::string ErrMsg = "looking up intrinsic '" + Name.str() + "(";
18900b57cec5SDimitry Andric   for (unsigned I = 0; I < Types.size(); ++I) {
18910b57cec5SDimitry Andric     if (I != 0)
18920b57cec5SDimitry Andric       ErrMsg += ", ";
18930b57cec5SDimitry Andric     ErrMsg += Types[I].str();
18940b57cec5SDimitry Andric   }
18950b57cec5SDimitry Andric   ErrMsg += ")'\n";
18960b57cec5SDimitry Andric   ErrMsg += "Available overloads:\n";
18970b57cec5SDimitry Andric 
18980b57cec5SDimitry Andric   // Now, look through each intrinsic implementation and see if the types are
18990b57cec5SDimitry Andric   // compatible.
19000b57cec5SDimitry Andric   for (auto &I : V) {
19010b57cec5SDimitry Andric     ErrMsg += "  - " + I.getReturnType().str() + " " + I.getMangledName();
19020b57cec5SDimitry Andric     ErrMsg += "(";
19030b57cec5SDimitry Andric     for (unsigned A = 0; A < I.getNumParams(); ++A) {
19040b57cec5SDimitry Andric       if (A != 0)
19050b57cec5SDimitry Andric         ErrMsg += ", ";
19060b57cec5SDimitry Andric       ErrMsg += I.getParamType(A).str();
19070b57cec5SDimitry Andric     }
19080b57cec5SDimitry Andric     ErrMsg += ")\n";
19090b57cec5SDimitry Andric 
19105ffd83dbSDimitry Andric     if (MangledName && MangledName != I.getMangledName(true))
19115ffd83dbSDimitry Andric       continue;
19125ffd83dbSDimitry Andric 
19130b57cec5SDimitry Andric     if (I.getNumParams() != Types.size())
19140b57cec5SDimitry Andric       continue;
19150b57cec5SDimitry Andric 
19165ffd83dbSDimitry Andric     unsigned ArgNum = 0;
1917349cc55cSDimitry Andric     bool MatchingArgumentTypes = llvm::all_of(Types, [&](const auto &Type) {
19185ffd83dbSDimitry Andric       return Type == I.getParamType(ArgNum++);
19195ffd83dbSDimitry Andric     });
19205ffd83dbSDimitry Andric 
19215ffd83dbSDimitry Andric     if (MatchingArgumentTypes)
19220b57cec5SDimitry Andric       GoodVec.push_back(&I);
19230b57cec5SDimitry Andric   }
19240b57cec5SDimitry Andric 
19250b57cec5SDimitry Andric   assert_with_loc(!GoodVec.empty(),
19260b57cec5SDimitry Andric                   "No compatible intrinsic found - " + ErrMsg);
19270b57cec5SDimitry Andric   assert_with_loc(GoodVec.size() == 1, "Multiple overloads found - " + ErrMsg);
19280b57cec5SDimitry Andric 
19290b57cec5SDimitry Andric   return *GoodVec.front();
19300b57cec5SDimitry Andric }
19310b57cec5SDimitry Andric 
19320b57cec5SDimitry Andric void NeonEmitter::createIntrinsic(Record *R,
19330b57cec5SDimitry Andric                                   SmallVectorImpl<Intrinsic *> &Out) {
19345ffd83dbSDimitry Andric   std::string Name = std::string(R->getValueAsString("Name"));
19355ffd83dbSDimitry Andric   std::string Proto = std::string(R->getValueAsString("Prototype"));
19365ffd83dbSDimitry Andric   std::string Types = std::string(R->getValueAsString("Types"));
19370b57cec5SDimitry Andric   Record *OperationRec = R->getValueAsDef("Operation");
19380b57cec5SDimitry Andric   bool BigEndianSafe  = R->getValueAsBit("BigEndianSafe");
19395ffd83dbSDimitry Andric   std::string Guard = std::string(R->getValueAsString("ArchGuard"));
19400b57cec5SDimitry Andric   bool IsUnavailable = OperationRec->getValueAsBit("Unavailable");
19415ffd83dbSDimitry Andric   std::string CartesianProductWith = std::string(R->getValueAsString("CartesianProductWith"));
19420b57cec5SDimitry Andric 
19430b57cec5SDimitry Andric   // Set the global current record. This allows assert_with_loc to produce
19440b57cec5SDimitry Andric   // decent location information even when highly nested.
19450b57cec5SDimitry Andric   CurrentRecord = R;
19460b57cec5SDimitry Andric 
19470b57cec5SDimitry Andric   ListInit *Body = OperationRec->getValueAsListInit("Ops");
19480b57cec5SDimitry Andric 
19490b57cec5SDimitry Andric   std::vector<TypeSpec> TypeSpecs = TypeSpec::fromTypeSpecs(Types);
19500b57cec5SDimitry Andric 
19510b57cec5SDimitry Andric   ClassKind CK = ClassNone;
19520b57cec5SDimitry Andric   if (R->getSuperClasses().size() >= 2)
19530b57cec5SDimitry Andric     CK = ClassMap[R->getSuperClasses()[1].first];
19540b57cec5SDimitry Andric 
19550b57cec5SDimitry Andric   std::vector<std::pair<TypeSpec, TypeSpec>> NewTypeSpecs;
19565ffd83dbSDimitry Andric   if (!CartesianProductWith.empty()) {
19575ffd83dbSDimitry Andric     std::vector<TypeSpec> ProductTypeSpecs = TypeSpec::fromTypeSpecs(CartesianProductWith);
19580b57cec5SDimitry Andric     for (auto TS : TypeSpecs) {
1959480093f4SDimitry Andric       Type DefaultT(TS, ".");
19605ffd83dbSDimitry Andric       for (auto SrcTS : ProductTypeSpecs) {
1961480093f4SDimitry Andric         Type DefaultSrcT(SrcTS, ".");
19620b57cec5SDimitry Andric         if (TS == SrcTS ||
19630b57cec5SDimitry Andric             DefaultSrcT.getSizeInBits() != DefaultT.getSizeInBits())
19640b57cec5SDimitry Andric           continue;
19650b57cec5SDimitry Andric         NewTypeSpecs.push_back(std::make_pair(TS, SrcTS));
19660b57cec5SDimitry Andric       }
19675ffd83dbSDimitry Andric     }
19680b57cec5SDimitry Andric   } else {
19695ffd83dbSDimitry Andric     for (auto TS : TypeSpecs) {
19700b57cec5SDimitry Andric       NewTypeSpecs.push_back(std::make_pair(TS, TS));
19710b57cec5SDimitry Andric     }
19720b57cec5SDimitry Andric   }
19730b57cec5SDimitry Andric 
19740b57cec5SDimitry Andric   llvm::sort(NewTypeSpecs);
19750b57cec5SDimitry Andric   NewTypeSpecs.erase(std::unique(NewTypeSpecs.begin(), NewTypeSpecs.end()),
19760b57cec5SDimitry Andric 		     NewTypeSpecs.end());
19770b57cec5SDimitry Andric   auto &Entry = IntrinsicMap[Name];
19780b57cec5SDimitry Andric 
19790b57cec5SDimitry Andric   for (auto &I : NewTypeSpecs) {
19800b57cec5SDimitry Andric     Entry.emplace_back(R, Name, Proto, I.first, I.second, CK, Body, *this,
19810b57cec5SDimitry Andric                        Guard, IsUnavailable, BigEndianSafe);
19820b57cec5SDimitry Andric     Out.push_back(&Entry.back());
19830b57cec5SDimitry Andric   }
19840b57cec5SDimitry Andric 
19850b57cec5SDimitry Andric   CurrentRecord = nullptr;
19860b57cec5SDimitry Andric }
19870b57cec5SDimitry Andric 
19880b57cec5SDimitry Andric /// genBuiltinsDef: Generate the BuiltinsARM.def and  BuiltinsAArch64.def
19890b57cec5SDimitry Andric /// declaration of builtins, checking for unique builtin declarations.
19900b57cec5SDimitry Andric void NeonEmitter::genBuiltinsDef(raw_ostream &OS,
19910b57cec5SDimitry Andric                                  SmallVectorImpl<Intrinsic *> &Defs) {
19920b57cec5SDimitry Andric   OS << "#ifdef GET_NEON_BUILTINS\n";
19930b57cec5SDimitry Andric 
19940b57cec5SDimitry Andric   // We only want to emit a builtin once, and we want to emit them in
19950b57cec5SDimitry Andric   // alphabetical order, so use a std::set.
19960b57cec5SDimitry Andric   std::set<std::string> Builtins;
19970b57cec5SDimitry Andric 
19980b57cec5SDimitry Andric   for (auto *Def : Defs) {
19990b57cec5SDimitry Andric     if (Def->hasBody())
20000b57cec5SDimitry Andric       continue;
20010b57cec5SDimitry Andric 
20020b57cec5SDimitry Andric     std::string S = "BUILTIN(__builtin_neon_" + Def->getMangledName() + ", \"";
20030b57cec5SDimitry Andric 
20040b57cec5SDimitry Andric     S += Def->getBuiltinTypeStr();
20050b57cec5SDimitry Andric     S += "\", \"n\")";
20060b57cec5SDimitry Andric 
20070b57cec5SDimitry Andric     Builtins.insert(S);
20080b57cec5SDimitry Andric   }
20090b57cec5SDimitry Andric 
20100b57cec5SDimitry Andric   for (auto &S : Builtins)
20110b57cec5SDimitry Andric     OS << S << "\n";
20120b57cec5SDimitry Andric   OS << "#endif\n\n";
20130b57cec5SDimitry Andric }
20140b57cec5SDimitry Andric 
20150b57cec5SDimitry Andric /// Generate the ARM and AArch64 overloaded type checking code for
20160b57cec5SDimitry Andric /// SemaChecking.cpp, checking for unique builtin declarations.
20170b57cec5SDimitry Andric void NeonEmitter::genOverloadTypeCheckCode(raw_ostream &OS,
20180b57cec5SDimitry Andric                                            SmallVectorImpl<Intrinsic *> &Defs) {
20190b57cec5SDimitry Andric   OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
20200b57cec5SDimitry Andric 
20210b57cec5SDimitry Andric   // We record each overload check line before emitting because subsequent Inst
20220b57cec5SDimitry Andric   // definitions may extend the number of permitted types (i.e. augment the
20230b57cec5SDimitry Andric   // Mask). Use std::map to avoid sorting the table by hash number.
20240b57cec5SDimitry Andric   struct OverloadInfo {
20250b57cec5SDimitry Andric     uint64_t Mask;
20260b57cec5SDimitry Andric     int PtrArgNum;
20270b57cec5SDimitry Andric     bool HasConstPtr;
20280b57cec5SDimitry Andric     OverloadInfo() : Mask(0ULL), PtrArgNum(0), HasConstPtr(false) {}
20290b57cec5SDimitry Andric   };
20300b57cec5SDimitry Andric   std::map<std::string, OverloadInfo> OverloadMap;
20310b57cec5SDimitry Andric 
20320b57cec5SDimitry Andric   for (auto *Def : Defs) {
20330b57cec5SDimitry Andric     // If the def has a body (that is, it has Operation DAGs), it won't call
20340b57cec5SDimitry Andric     // __builtin_neon_* so we don't need to generate a definition for it.
20350b57cec5SDimitry Andric     if (Def->hasBody())
20360b57cec5SDimitry Andric       continue;
20370b57cec5SDimitry Andric     // Functions which have a scalar argument cannot be overloaded, no need to
20380b57cec5SDimitry Andric     // check them if we are emitting the type checking code.
20390b57cec5SDimitry Andric     if (Def->protoHasScalar())
20400b57cec5SDimitry Andric       continue;
20410b57cec5SDimitry Andric 
20420b57cec5SDimitry Andric     uint64_t Mask = 0ULL;
2043480093f4SDimitry Andric     Mask |= 1ULL << Def->getPolymorphicKeyType().getNeonEnum();
20440b57cec5SDimitry Andric 
20450b57cec5SDimitry Andric     // Check if the function has a pointer or const pointer argument.
20460b57cec5SDimitry Andric     int PtrArgNum = -1;
20470b57cec5SDimitry Andric     bool HasConstPtr = false;
20480b57cec5SDimitry Andric     for (unsigned I = 0; I < Def->getNumParams(); ++I) {
2049480093f4SDimitry Andric       const auto &Type = Def->getParamType(I);
2050480093f4SDimitry Andric       if (Type.isPointer()) {
20510b57cec5SDimitry Andric         PtrArgNum = I;
2052480093f4SDimitry Andric         HasConstPtr = Type.isConstPointer();
20530b57cec5SDimitry Andric       }
20540b57cec5SDimitry Andric     }
2055480093f4SDimitry Andric 
20560b57cec5SDimitry Andric     // For sret builtins, adjust the pointer argument index.
20570b57cec5SDimitry Andric     if (PtrArgNum >= 0 && Def->getReturnType().getNumVectors() > 1)
20580b57cec5SDimitry Andric       PtrArgNum += 1;
20590b57cec5SDimitry Andric 
20600b57cec5SDimitry Andric     std::string Name = Def->getName();
20610b57cec5SDimitry Andric     // Omit type checking for the pointer arguments of vld1_lane, vld1_dup,
20620b57cec5SDimitry Andric     // and vst1_lane intrinsics.  Using a pointer to the vector element
20630b57cec5SDimitry Andric     // type with one of those operations causes codegen to select an aligned
20640b57cec5SDimitry Andric     // load/store instruction.  If you want an unaligned operation,
20650b57cec5SDimitry Andric     // the pointer argument needs to have less alignment than element type,
20660b57cec5SDimitry Andric     // so just accept any pointer type.
20670b57cec5SDimitry Andric     if (Name == "vld1_lane" || Name == "vld1_dup" || Name == "vst1_lane") {
20680b57cec5SDimitry Andric       PtrArgNum = -1;
20690b57cec5SDimitry Andric       HasConstPtr = false;
20700b57cec5SDimitry Andric     }
20710b57cec5SDimitry Andric 
20720b57cec5SDimitry Andric     if (Mask) {
20730b57cec5SDimitry Andric       std::string Name = Def->getMangledName();
20740b57cec5SDimitry Andric       OverloadMap.insert(std::make_pair(Name, OverloadInfo()));
20750b57cec5SDimitry Andric       OverloadInfo &OI = OverloadMap[Name];
20760b57cec5SDimitry Andric       OI.Mask |= Mask;
20770b57cec5SDimitry Andric       OI.PtrArgNum |= PtrArgNum;
20780b57cec5SDimitry Andric       OI.HasConstPtr = HasConstPtr;
20790b57cec5SDimitry Andric     }
20800b57cec5SDimitry Andric   }
20810b57cec5SDimitry Andric 
20820b57cec5SDimitry Andric   for (auto &I : OverloadMap) {
20830b57cec5SDimitry Andric     OverloadInfo &OI = I.second;
20840b57cec5SDimitry Andric 
20850b57cec5SDimitry Andric     OS << "case NEON::BI__builtin_neon_" << I.first << ": ";
20860b57cec5SDimitry Andric     OS << "mask = 0x" << Twine::utohexstr(OI.Mask) << "ULL";
20870b57cec5SDimitry Andric     if (OI.PtrArgNum >= 0)
20880b57cec5SDimitry Andric       OS << "; PtrArgNum = " << OI.PtrArgNum;
20890b57cec5SDimitry Andric     if (OI.HasConstPtr)
20900b57cec5SDimitry Andric       OS << "; HasConstPtr = true";
20910b57cec5SDimitry Andric     OS << "; break;\n";
20920b57cec5SDimitry Andric   }
20930b57cec5SDimitry Andric   OS << "#endif\n\n";
20940b57cec5SDimitry Andric }
20950b57cec5SDimitry Andric 
20960b57cec5SDimitry Andric void NeonEmitter::genIntrinsicRangeCheckCode(raw_ostream &OS,
20970b57cec5SDimitry Andric                                         SmallVectorImpl<Intrinsic *> &Defs) {
20980b57cec5SDimitry Andric   OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
20990b57cec5SDimitry Andric 
21000b57cec5SDimitry Andric   std::set<std::string> Emitted;
21010b57cec5SDimitry Andric 
21020b57cec5SDimitry Andric   for (auto *Def : Defs) {
21030b57cec5SDimitry Andric     if (Def->hasBody())
21040b57cec5SDimitry Andric       continue;
21050b57cec5SDimitry Andric     // Functions which do not have an immediate do not need to have range
21060b57cec5SDimitry Andric     // checking code emitted.
21070b57cec5SDimitry Andric     if (!Def->hasImmediate())
21080b57cec5SDimitry Andric       continue;
21090b57cec5SDimitry Andric     if (Emitted.find(Def->getMangledName()) != Emitted.end())
21100b57cec5SDimitry Andric       continue;
21110b57cec5SDimitry Andric 
21120b57cec5SDimitry Andric     std::string LowerBound, UpperBound;
21130b57cec5SDimitry Andric 
21140b57cec5SDimitry Andric     Record *R = Def->getRecord();
2115fe6060f1SDimitry Andric     if (R->getValueAsBit("isVXAR")) {
2116fe6060f1SDimitry Andric       //VXAR takes an immediate in the range [0, 63]
2117fe6060f1SDimitry Andric       LowerBound = "0";
2118fe6060f1SDimitry Andric       UpperBound = "63";
2119fe6060f1SDimitry Andric     } else if (R->getValueAsBit("isVCVT_N")) {
21200b57cec5SDimitry Andric       // VCVT between floating- and fixed-point values takes an immediate
21210b57cec5SDimitry Andric       // in the range [1, 32) for f32 or [1, 64) for f64 or [1, 16) for f16.
21220b57cec5SDimitry Andric       LowerBound = "1";
21230b57cec5SDimitry Andric 	  if (Def->getBaseType().getElementSizeInBits() == 16 ||
21240b57cec5SDimitry Andric 		  Def->getName().find('h') != std::string::npos)
21250b57cec5SDimitry Andric 		// VCVTh operating on FP16 intrinsics in range [1, 16)
21260b57cec5SDimitry Andric 		UpperBound = "15";
21270b57cec5SDimitry Andric 	  else if (Def->getBaseType().getElementSizeInBits() == 32)
21280b57cec5SDimitry Andric         UpperBound = "31";
21290b57cec5SDimitry Andric 	  else
21300b57cec5SDimitry Andric         UpperBound = "63";
21310b57cec5SDimitry Andric     } else if (R->getValueAsBit("isScalarShift")) {
21320b57cec5SDimitry Andric       // Right shifts have an 'r' in the name, left shifts do not. Convert
21330b57cec5SDimitry Andric       // instructions have the same bounds and right shifts.
21340b57cec5SDimitry Andric       if (Def->getName().find('r') != std::string::npos ||
21350b57cec5SDimitry Andric           Def->getName().find("cvt") != std::string::npos)
21360b57cec5SDimitry Andric         LowerBound = "1";
21370b57cec5SDimitry Andric 
21380b57cec5SDimitry Andric       UpperBound = utostr(Def->getReturnType().getElementSizeInBits() - 1);
21390b57cec5SDimitry Andric     } else if (R->getValueAsBit("isShift")) {
21400b57cec5SDimitry Andric       // Builtins which are overloaded by type will need to have their upper
21410b57cec5SDimitry Andric       // bound computed at Sema time based on the type constant.
21420b57cec5SDimitry Andric 
21430b57cec5SDimitry Andric       // Right shifts have an 'r' in the name, left shifts do not.
21440b57cec5SDimitry Andric       if (Def->getName().find('r') != std::string::npos)
21450b57cec5SDimitry Andric         LowerBound = "1";
21460b57cec5SDimitry Andric       UpperBound = "RFT(TV, true)";
21470b57cec5SDimitry Andric     } else if (Def->getClassKind(true) == ClassB) {
21480b57cec5SDimitry Andric       // ClassB intrinsics have a type (and hence lane number) that is only
21490b57cec5SDimitry Andric       // known at runtime.
21500b57cec5SDimitry Andric       if (R->getValueAsBit("isLaneQ"))
21510b57cec5SDimitry Andric         UpperBound = "RFT(TV, false, true)";
21520b57cec5SDimitry Andric       else
21530b57cec5SDimitry Andric         UpperBound = "RFT(TV, false, false)";
21540b57cec5SDimitry Andric     } else {
21550b57cec5SDimitry Andric       // The immediate generally refers to a lane in the preceding argument.
21560b57cec5SDimitry Andric       assert(Def->getImmediateIdx() > 0);
21570b57cec5SDimitry Andric       Type T = Def->getParamType(Def->getImmediateIdx() - 1);
21580b57cec5SDimitry Andric       UpperBound = utostr(T.getNumElements() - 1);
21590b57cec5SDimitry Andric     }
21600b57cec5SDimitry Andric 
21610b57cec5SDimitry Andric     // Calculate the index of the immediate that should be range checked.
21620b57cec5SDimitry Andric     unsigned Idx = Def->getNumParams();
21630b57cec5SDimitry Andric     if (Def->hasImmediate())
21640b57cec5SDimitry Andric       Idx = Def->getGeneratedParamIdx(Def->getImmediateIdx());
21650b57cec5SDimitry Andric 
21660b57cec5SDimitry Andric     OS << "case NEON::BI__builtin_neon_" << Def->getMangledName() << ": "
21670b57cec5SDimitry Andric        << "i = " << Idx << ";";
21680b57cec5SDimitry Andric     if (!LowerBound.empty())
21690b57cec5SDimitry Andric       OS << " l = " << LowerBound << ";";
21700b57cec5SDimitry Andric     if (!UpperBound.empty())
21710b57cec5SDimitry Andric       OS << " u = " << UpperBound << ";";
21720b57cec5SDimitry Andric     OS << " break;\n";
21730b57cec5SDimitry Andric 
21740b57cec5SDimitry Andric     Emitted.insert(Def->getMangledName());
21750b57cec5SDimitry Andric   }
21760b57cec5SDimitry Andric 
21770b57cec5SDimitry Andric   OS << "#endif\n\n";
21780b57cec5SDimitry Andric }
21790b57cec5SDimitry Andric 
21800b57cec5SDimitry Andric /// runHeader - Emit a file with sections defining:
21810b57cec5SDimitry Andric /// 1. the NEON section of BuiltinsARM.def and BuiltinsAArch64.def.
21820b57cec5SDimitry Andric /// 2. the SemaChecking code for the type overload checking.
21830b57cec5SDimitry Andric /// 3. the SemaChecking code for validation of intrinsic immediate arguments.
21840b57cec5SDimitry Andric void NeonEmitter::runHeader(raw_ostream &OS) {
21850b57cec5SDimitry Andric   std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
21860b57cec5SDimitry Andric 
21870b57cec5SDimitry Andric   SmallVector<Intrinsic *, 128> Defs;
21880b57cec5SDimitry Andric   for (auto *R : RV)
21890b57cec5SDimitry Andric     createIntrinsic(R, Defs);
21900b57cec5SDimitry Andric 
21910b57cec5SDimitry Andric   // Generate shared BuiltinsXXX.def
21920b57cec5SDimitry Andric   genBuiltinsDef(OS, Defs);
21930b57cec5SDimitry Andric 
21940b57cec5SDimitry Andric   // Generate ARM overloaded type checking code for SemaChecking.cpp
21950b57cec5SDimitry Andric   genOverloadTypeCheckCode(OS, Defs);
21960b57cec5SDimitry Andric 
21970b57cec5SDimitry Andric   // Generate ARM range checking code for shift/lane immediates.
21980b57cec5SDimitry Andric   genIntrinsicRangeCheckCode(OS, Defs);
21990b57cec5SDimitry Andric }
22000b57cec5SDimitry Andric 
22015ffd83dbSDimitry Andric static void emitNeonTypeDefs(const std::string& types, raw_ostream &OS) {
22025ffd83dbSDimitry Andric   std::string TypedefTypes(types);
22035ffd83dbSDimitry Andric   std::vector<TypeSpec> TDTypeVec = TypeSpec::fromTypeSpecs(TypedefTypes);
22045ffd83dbSDimitry Andric 
22055ffd83dbSDimitry Andric   // Emit vector typedefs.
22065ffd83dbSDimitry Andric   bool InIfdef = false;
22075ffd83dbSDimitry Andric   for (auto &TS : TDTypeVec) {
22085ffd83dbSDimitry Andric     bool IsA64 = false;
22095ffd83dbSDimitry Andric     Type T(TS, ".");
22105ffd83dbSDimitry Andric     if (T.isDouble())
22115ffd83dbSDimitry Andric       IsA64 = true;
22125ffd83dbSDimitry Andric 
22135ffd83dbSDimitry Andric     if (InIfdef && !IsA64) {
22145ffd83dbSDimitry Andric       OS << "#endif\n";
22155ffd83dbSDimitry Andric       InIfdef = false;
22165ffd83dbSDimitry Andric     }
22175ffd83dbSDimitry Andric     if (!InIfdef && IsA64) {
22185ffd83dbSDimitry Andric       OS << "#ifdef __aarch64__\n";
22195ffd83dbSDimitry Andric       InIfdef = true;
22205ffd83dbSDimitry Andric     }
22215ffd83dbSDimitry Andric 
22225ffd83dbSDimitry Andric     if (T.isPoly())
22235ffd83dbSDimitry Andric       OS << "typedef __attribute__((neon_polyvector_type(";
22245ffd83dbSDimitry Andric     else
22255ffd83dbSDimitry Andric       OS << "typedef __attribute__((neon_vector_type(";
22265ffd83dbSDimitry Andric 
22275ffd83dbSDimitry Andric     Type T2 = T;
22285ffd83dbSDimitry Andric     T2.makeScalar();
22295ffd83dbSDimitry Andric     OS << T.getNumElements() << "))) ";
22305ffd83dbSDimitry Andric     OS << T2.str();
22315ffd83dbSDimitry Andric     OS << " " << T.str() << ";\n";
22325ffd83dbSDimitry Andric   }
22335ffd83dbSDimitry Andric   if (InIfdef)
22345ffd83dbSDimitry Andric     OS << "#endif\n";
22355ffd83dbSDimitry Andric   OS << "\n";
22365ffd83dbSDimitry Andric 
22375ffd83dbSDimitry Andric   // Emit struct typedefs.
22385ffd83dbSDimitry Andric   InIfdef = false;
22395ffd83dbSDimitry Andric   for (unsigned NumMembers = 2; NumMembers <= 4; ++NumMembers) {
22405ffd83dbSDimitry Andric     for (auto &TS : TDTypeVec) {
22415ffd83dbSDimitry Andric       bool IsA64 = false;
22425ffd83dbSDimitry Andric       Type T(TS, ".");
22435ffd83dbSDimitry Andric       if (T.isDouble())
22445ffd83dbSDimitry Andric         IsA64 = true;
22455ffd83dbSDimitry Andric 
22465ffd83dbSDimitry Andric       if (InIfdef && !IsA64) {
22475ffd83dbSDimitry Andric         OS << "#endif\n";
22485ffd83dbSDimitry Andric         InIfdef = false;
22495ffd83dbSDimitry Andric       }
22505ffd83dbSDimitry Andric       if (!InIfdef && IsA64) {
22515ffd83dbSDimitry Andric         OS << "#ifdef __aarch64__\n";
22525ffd83dbSDimitry Andric         InIfdef = true;
22535ffd83dbSDimitry Andric       }
22545ffd83dbSDimitry Andric 
22555ffd83dbSDimitry Andric       const char Mods[] = { static_cast<char>('2' + (NumMembers - 2)), 0};
22565ffd83dbSDimitry Andric       Type VT(TS, Mods);
22575ffd83dbSDimitry Andric       OS << "typedef struct " << VT.str() << " {\n";
22585ffd83dbSDimitry Andric       OS << "  " << T.str() << " val";
22595ffd83dbSDimitry Andric       OS << "[" << NumMembers << "]";
22605ffd83dbSDimitry Andric       OS << ";\n} ";
22615ffd83dbSDimitry Andric       OS << VT.str() << ";\n";
22625ffd83dbSDimitry Andric       OS << "\n";
22635ffd83dbSDimitry Andric     }
22645ffd83dbSDimitry Andric   }
22655ffd83dbSDimitry Andric   if (InIfdef)
22665ffd83dbSDimitry Andric     OS << "#endif\n";
22675ffd83dbSDimitry Andric }
22685ffd83dbSDimitry Andric 
22690b57cec5SDimitry Andric /// run - Read the records in arm_neon.td and output arm_neon.h.  arm_neon.h
22700b57cec5SDimitry Andric /// is comprised of type definitions and function declarations.
22710b57cec5SDimitry Andric void NeonEmitter::run(raw_ostream &OS) {
22720b57cec5SDimitry Andric   OS << "/*===---- arm_neon.h - ARM Neon intrinsics "
22730b57cec5SDimitry Andric         "------------------------------"
22740b57cec5SDimitry Andric         "---===\n"
22750b57cec5SDimitry Andric         " *\n"
22760b57cec5SDimitry Andric         " * Permission is hereby granted, free of charge, to any person "
22770b57cec5SDimitry Andric         "obtaining "
22780b57cec5SDimitry Andric         "a copy\n"
22790b57cec5SDimitry Andric         " * of this software and associated documentation files (the "
22800b57cec5SDimitry Andric         "\"Software\"),"
22810b57cec5SDimitry Andric         " to deal\n"
22820b57cec5SDimitry Andric         " * in the Software without restriction, including without limitation "
22830b57cec5SDimitry Andric         "the "
22840b57cec5SDimitry Andric         "rights\n"
22850b57cec5SDimitry Andric         " * to use, copy, modify, merge, publish, distribute, sublicense, "
22860b57cec5SDimitry Andric         "and/or sell\n"
22870b57cec5SDimitry Andric         " * copies of the Software, and to permit persons to whom the Software "
22880b57cec5SDimitry Andric         "is\n"
22890b57cec5SDimitry Andric         " * furnished to do so, subject to the following conditions:\n"
22900b57cec5SDimitry Andric         " *\n"
22910b57cec5SDimitry Andric         " * The above copyright notice and this permission notice shall be "
22920b57cec5SDimitry Andric         "included in\n"
22930b57cec5SDimitry Andric         " * all copies or substantial portions of the Software.\n"
22940b57cec5SDimitry Andric         " *\n"
22950b57cec5SDimitry Andric         " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, "
22960b57cec5SDimitry Andric         "EXPRESS OR\n"
22970b57cec5SDimitry Andric         " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
22980b57cec5SDimitry Andric         "MERCHANTABILITY,\n"
22990b57cec5SDimitry Andric         " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT "
23000b57cec5SDimitry Andric         "SHALL THE\n"
23010b57cec5SDimitry Andric         " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR "
23020b57cec5SDimitry Andric         "OTHER\n"
23030b57cec5SDimitry Andric         " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, "
23040b57cec5SDimitry Andric         "ARISING FROM,\n"
23050b57cec5SDimitry Andric         " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER "
23060b57cec5SDimitry Andric         "DEALINGS IN\n"
23070b57cec5SDimitry Andric         " * THE SOFTWARE.\n"
23080b57cec5SDimitry Andric         " *\n"
23090b57cec5SDimitry Andric         " *===-----------------------------------------------------------------"
23100b57cec5SDimitry Andric         "---"
23110b57cec5SDimitry Andric         "---===\n"
23120b57cec5SDimitry Andric         " */\n\n";
23130b57cec5SDimitry Andric 
23140b57cec5SDimitry Andric   OS << "#ifndef __ARM_NEON_H\n";
23150b57cec5SDimitry Andric   OS << "#define __ARM_NEON_H\n\n";
23160b57cec5SDimitry Andric 
23175ffd83dbSDimitry Andric   OS << "#ifndef __ARM_FP\n";
23185ffd83dbSDimitry Andric   OS << "#error \"NEON intrinsics not available with the soft-float ABI. "
23195ffd83dbSDimitry Andric         "Please use -mfloat-abi=softfp or -mfloat-abi=hard\"\n";
23205ffd83dbSDimitry Andric   OS << "#else\n\n";
23215ffd83dbSDimitry Andric 
23220b57cec5SDimitry Andric   OS << "#if !defined(__ARM_NEON)\n";
23230b57cec5SDimitry Andric   OS << "#error \"NEON support not enabled\"\n";
23245ffd83dbSDimitry Andric   OS << "#else\n\n";
23250b57cec5SDimitry Andric 
23260b57cec5SDimitry Andric   OS << "#include <stdint.h>\n\n";
23270b57cec5SDimitry Andric 
23285ffd83dbSDimitry Andric   OS << "#ifdef __ARM_FEATURE_BF16\n";
23295ffd83dbSDimitry Andric   OS << "#include <arm_bf16.h>\n";
23305ffd83dbSDimitry Andric   OS << "typedef __bf16 bfloat16_t;\n";
23315ffd83dbSDimitry Andric   OS << "#endif\n\n";
23325ffd83dbSDimitry Andric 
23330b57cec5SDimitry Andric   // Emit NEON-specific scalar typedefs.
23340b57cec5SDimitry Andric   OS << "typedef float float32_t;\n";
23350b57cec5SDimitry Andric   OS << "typedef __fp16 float16_t;\n";
23360b57cec5SDimitry Andric 
23370b57cec5SDimitry Andric   OS << "#ifdef __aarch64__\n";
23380b57cec5SDimitry Andric   OS << "typedef double float64_t;\n";
23390b57cec5SDimitry Andric   OS << "#endif\n\n";
23400b57cec5SDimitry Andric 
23410b57cec5SDimitry Andric   // For now, signedness of polynomial types depends on target
23420b57cec5SDimitry Andric   OS << "#ifdef __aarch64__\n";
23430b57cec5SDimitry Andric   OS << "typedef uint8_t poly8_t;\n";
23440b57cec5SDimitry Andric   OS << "typedef uint16_t poly16_t;\n";
23450b57cec5SDimitry Andric   OS << "typedef uint64_t poly64_t;\n";
23460b57cec5SDimitry Andric   OS << "typedef __uint128_t poly128_t;\n";
23470b57cec5SDimitry Andric   OS << "#else\n";
23480b57cec5SDimitry Andric   OS << "typedef int8_t poly8_t;\n";
23490b57cec5SDimitry Andric   OS << "typedef int16_t poly16_t;\n";
23505ffd83dbSDimitry Andric   OS << "typedef int64_t poly64_t;\n";
23510b57cec5SDimitry Andric   OS << "#endif\n";
23520b57cec5SDimitry Andric 
23535ffd83dbSDimitry Andric   emitNeonTypeDefs("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfdQdPcQPcPsQPsPlQPl", OS);
23540b57cec5SDimitry Andric 
23555ffd83dbSDimitry Andric   OS << "#ifdef __ARM_FEATURE_BF16\n";
23565ffd83dbSDimitry Andric   emitNeonTypeDefs("bQb", OS);
23575ffd83dbSDimitry Andric   OS << "#endif\n\n";
23580b57cec5SDimitry Andric 
23590b57cec5SDimitry Andric   OS << "#define __ai static __inline__ __attribute__((__always_inline__, "
23600b57cec5SDimitry Andric         "__nodebug__))\n\n";
23610b57cec5SDimitry Andric 
23620b57cec5SDimitry Andric   SmallVector<Intrinsic *, 128> Defs;
23630b57cec5SDimitry Andric   std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
23640b57cec5SDimitry Andric   for (auto *R : RV)
23650b57cec5SDimitry Andric     createIntrinsic(R, Defs);
23660b57cec5SDimitry Andric 
23670b57cec5SDimitry Andric   for (auto *I : Defs)
23680b57cec5SDimitry Andric     I->indexBody();
23690b57cec5SDimitry Andric 
2370a7dea167SDimitry Andric   llvm::stable_sort(Defs, llvm::deref<std::less<>>());
23710b57cec5SDimitry Andric 
23720b57cec5SDimitry Andric   // Only emit a def when its requirements have been met.
23730b57cec5SDimitry Andric   // FIXME: This loop could be made faster, but it's fast enough for now.
23740b57cec5SDimitry Andric   bool MadeProgress = true;
23750b57cec5SDimitry Andric   std::string InGuard;
23760b57cec5SDimitry Andric   while (!Defs.empty() && MadeProgress) {
23770b57cec5SDimitry Andric     MadeProgress = false;
23780b57cec5SDimitry Andric 
23790b57cec5SDimitry Andric     for (SmallVector<Intrinsic *, 128>::iterator I = Defs.begin();
23800b57cec5SDimitry Andric          I != Defs.end(); /*No step*/) {
23810b57cec5SDimitry Andric       bool DependenciesSatisfied = true;
23820b57cec5SDimitry Andric       for (auto *II : (*I)->getDependencies()) {
23830b57cec5SDimitry Andric         if (llvm::is_contained(Defs, II))
23840b57cec5SDimitry Andric           DependenciesSatisfied = false;
23850b57cec5SDimitry Andric       }
23860b57cec5SDimitry Andric       if (!DependenciesSatisfied) {
23870b57cec5SDimitry Andric         // Try the next one.
23880b57cec5SDimitry Andric         ++I;
23890b57cec5SDimitry Andric         continue;
23900b57cec5SDimitry Andric       }
23910b57cec5SDimitry Andric 
23920b57cec5SDimitry Andric       // Emit #endif/#if pair if needed.
23930b57cec5SDimitry Andric       if ((*I)->getGuard() != InGuard) {
23940b57cec5SDimitry Andric         if (!InGuard.empty())
23950b57cec5SDimitry Andric           OS << "#endif\n";
23960b57cec5SDimitry Andric         InGuard = (*I)->getGuard();
23970b57cec5SDimitry Andric         if (!InGuard.empty())
23980b57cec5SDimitry Andric           OS << "#if " << InGuard << "\n";
23990b57cec5SDimitry Andric       }
24000b57cec5SDimitry Andric 
24010b57cec5SDimitry Andric       // Actually generate the intrinsic code.
24020b57cec5SDimitry Andric       OS << (*I)->generate();
24030b57cec5SDimitry Andric 
24040b57cec5SDimitry Andric       MadeProgress = true;
24050b57cec5SDimitry Andric       I = Defs.erase(I);
24060b57cec5SDimitry Andric     }
24070b57cec5SDimitry Andric   }
24080b57cec5SDimitry Andric   assert(Defs.empty() && "Some requirements were not satisfied!");
24090b57cec5SDimitry Andric   if (!InGuard.empty())
24100b57cec5SDimitry Andric     OS << "#endif\n";
24110b57cec5SDimitry Andric 
24120b57cec5SDimitry Andric   OS << "\n";
24130b57cec5SDimitry Andric   OS << "#undef __ai\n\n";
24145ffd83dbSDimitry Andric   OS << "#endif /* if !defined(__ARM_NEON) */\n";
24155ffd83dbSDimitry Andric   OS << "#endif /* ifndef __ARM_FP */\n";
24160b57cec5SDimitry Andric   OS << "#endif /* __ARM_NEON_H */\n";
24170b57cec5SDimitry Andric }
24180b57cec5SDimitry Andric 
24190b57cec5SDimitry Andric /// run - Read the records in arm_fp16.td and output arm_fp16.h.  arm_fp16.h
24200b57cec5SDimitry Andric /// is comprised of type definitions and function declarations.
24210b57cec5SDimitry Andric void NeonEmitter::runFP16(raw_ostream &OS) {
24220b57cec5SDimitry Andric   OS << "/*===---- arm_fp16.h - ARM FP16 intrinsics "
24230b57cec5SDimitry Andric         "------------------------------"
24240b57cec5SDimitry Andric         "---===\n"
24250b57cec5SDimitry Andric         " *\n"
24260b57cec5SDimitry Andric         " * Permission is hereby granted, free of charge, to any person "
24270b57cec5SDimitry Andric         "obtaining a copy\n"
24280b57cec5SDimitry Andric         " * of this software and associated documentation files (the "
24290b57cec5SDimitry Andric 				"\"Software\"), to deal\n"
24300b57cec5SDimitry Andric         " * in the Software without restriction, including without limitation "
24310b57cec5SDimitry Andric 				"the rights\n"
24320b57cec5SDimitry Andric         " * to use, copy, modify, merge, publish, distribute, sublicense, "
24330b57cec5SDimitry Andric 				"and/or sell\n"
24340b57cec5SDimitry Andric         " * copies of the Software, and to permit persons to whom the Software "
24350b57cec5SDimitry Andric 				"is\n"
24360b57cec5SDimitry Andric         " * furnished to do so, subject to the following conditions:\n"
24370b57cec5SDimitry Andric         " *\n"
24380b57cec5SDimitry Andric         " * The above copyright notice and this permission notice shall be "
24390b57cec5SDimitry Andric         "included in\n"
24400b57cec5SDimitry Andric         " * all copies or substantial portions of the Software.\n"
24410b57cec5SDimitry Andric         " *\n"
24420b57cec5SDimitry Andric         " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, "
24430b57cec5SDimitry Andric         "EXPRESS OR\n"
24440b57cec5SDimitry Andric         " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
24450b57cec5SDimitry Andric         "MERCHANTABILITY,\n"
24460b57cec5SDimitry Andric         " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT "
24470b57cec5SDimitry Andric         "SHALL THE\n"
24480b57cec5SDimitry Andric         " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR "
24490b57cec5SDimitry Andric         "OTHER\n"
24500b57cec5SDimitry Andric         " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, "
24510b57cec5SDimitry Andric         "ARISING FROM,\n"
24520b57cec5SDimitry Andric         " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER "
24530b57cec5SDimitry Andric         "DEALINGS IN\n"
24540b57cec5SDimitry Andric         " * THE SOFTWARE.\n"
24550b57cec5SDimitry Andric         " *\n"
24560b57cec5SDimitry Andric         " *===-----------------------------------------------------------------"
24570b57cec5SDimitry Andric         "---"
24580b57cec5SDimitry Andric         "---===\n"
24590b57cec5SDimitry Andric         " */\n\n";
24600b57cec5SDimitry Andric 
24610b57cec5SDimitry Andric   OS << "#ifndef __ARM_FP16_H\n";
24620b57cec5SDimitry Andric   OS << "#define __ARM_FP16_H\n\n";
24630b57cec5SDimitry Andric 
24640b57cec5SDimitry Andric   OS << "#include <stdint.h>\n\n";
24650b57cec5SDimitry Andric 
24660b57cec5SDimitry Andric   OS << "typedef __fp16 float16_t;\n";
24670b57cec5SDimitry Andric 
24680b57cec5SDimitry Andric   OS << "#define __ai static __inline__ __attribute__((__always_inline__, "
24690b57cec5SDimitry Andric         "__nodebug__))\n\n";
24700b57cec5SDimitry Andric 
24710b57cec5SDimitry Andric   SmallVector<Intrinsic *, 128> Defs;
24720b57cec5SDimitry Andric   std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
24730b57cec5SDimitry Andric   for (auto *R : RV)
24740b57cec5SDimitry Andric     createIntrinsic(R, Defs);
24750b57cec5SDimitry Andric 
24760b57cec5SDimitry Andric   for (auto *I : Defs)
24770b57cec5SDimitry Andric     I->indexBody();
24780b57cec5SDimitry Andric 
2479a7dea167SDimitry Andric   llvm::stable_sort(Defs, llvm::deref<std::less<>>());
24800b57cec5SDimitry Andric 
24810b57cec5SDimitry Andric   // Only emit a def when its requirements have been met.
24820b57cec5SDimitry Andric   // FIXME: This loop could be made faster, but it's fast enough for now.
24830b57cec5SDimitry Andric   bool MadeProgress = true;
24840b57cec5SDimitry Andric   std::string InGuard;
24850b57cec5SDimitry Andric   while (!Defs.empty() && MadeProgress) {
24860b57cec5SDimitry Andric     MadeProgress = false;
24870b57cec5SDimitry Andric 
24880b57cec5SDimitry Andric     for (SmallVector<Intrinsic *, 128>::iterator I = Defs.begin();
24890b57cec5SDimitry Andric          I != Defs.end(); /*No step*/) {
24900b57cec5SDimitry Andric       bool DependenciesSatisfied = true;
24910b57cec5SDimitry Andric       for (auto *II : (*I)->getDependencies()) {
24920b57cec5SDimitry Andric         if (llvm::is_contained(Defs, II))
24930b57cec5SDimitry Andric           DependenciesSatisfied = false;
24940b57cec5SDimitry Andric       }
24950b57cec5SDimitry Andric       if (!DependenciesSatisfied) {
24960b57cec5SDimitry Andric         // Try the next one.
24970b57cec5SDimitry Andric         ++I;
24980b57cec5SDimitry Andric         continue;
24990b57cec5SDimitry Andric       }
25000b57cec5SDimitry Andric 
25010b57cec5SDimitry Andric       // Emit #endif/#if pair if needed.
25020b57cec5SDimitry Andric       if ((*I)->getGuard() != InGuard) {
25030b57cec5SDimitry Andric         if (!InGuard.empty())
25040b57cec5SDimitry Andric           OS << "#endif\n";
25050b57cec5SDimitry Andric         InGuard = (*I)->getGuard();
25060b57cec5SDimitry Andric         if (!InGuard.empty())
25070b57cec5SDimitry Andric           OS << "#if " << InGuard << "\n";
25080b57cec5SDimitry Andric       }
25090b57cec5SDimitry Andric 
25100b57cec5SDimitry Andric       // Actually generate the intrinsic code.
25110b57cec5SDimitry Andric       OS << (*I)->generate();
25120b57cec5SDimitry Andric 
25130b57cec5SDimitry Andric       MadeProgress = true;
25140b57cec5SDimitry Andric       I = Defs.erase(I);
25150b57cec5SDimitry Andric     }
25160b57cec5SDimitry Andric   }
25170b57cec5SDimitry Andric   assert(Defs.empty() && "Some requirements were not satisfied!");
25180b57cec5SDimitry Andric   if (!InGuard.empty())
25190b57cec5SDimitry Andric     OS << "#endif\n";
25200b57cec5SDimitry Andric 
25210b57cec5SDimitry Andric   OS << "\n";
25220b57cec5SDimitry Andric   OS << "#undef __ai\n\n";
25230b57cec5SDimitry Andric   OS << "#endif /* __ARM_FP16_H */\n";
25240b57cec5SDimitry Andric }
25250b57cec5SDimitry Andric 
25265ffd83dbSDimitry Andric void NeonEmitter::runBF16(raw_ostream &OS) {
25275ffd83dbSDimitry Andric   OS << "/*===---- arm_bf16.h - ARM BF16 intrinsics "
25285ffd83dbSDimitry Andric         "-----------------------------------===\n"
25295ffd83dbSDimitry Andric         " *\n"
25305ffd83dbSDimitry Andric         " *\n"
25315ffd83dbSDimitry Andric         " * Part of the LLVM Project, under the Apache License v2.0 with LLVM "
25325ffd83dbSDimitry Andric         "Exceptions.\n"
25335ffd83dbSDimitry Andric         " * See https://llvm.org/LICENSE.txt for license information.\n"
25345ffd83dbSDimitry Andric         " * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n"
25355ffd83dbSDimitry Andric         " *\n"
25365ffd83dbSDimitry Andric         " *===-----------------------------------------------------------------"
25375ffd83dbSDimitry Andric         "------===\n"
25385ffd83dbSDimitry Andric         " */\n\n";
25395ffd83dbSDimitry Andric 
25405ffd83dbSDimitry Andric   OS << "#ifndef __ARM_BF16_H\n";
25415ffd83dbSDimitry Andric   OS << "#define __ARM_BF16_H\n\n";
25425ffd83dbSDimitry Andric 
25435ffd83dbSDimitry Andric   OS << "typedef __bf16 bfloat16_t;\n";
25445ffd83dbSDimitry Andric 
25455ffd83dbSDimitry Andric   OS << "#define __ai static __inline__ __attribute__((__always_inline__, "
25465ffd83dbSDimitry Andric         "__nodebug__))\n\n";
25475ffd83dbSDimitry Andric 
25485ffd83dbSDimitry Andric   SmallVector<Intrinsic *, 128> Defs;
25495ffd83dbSDimitry Andric   std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
25505ffd83dbSDimitry Andric   for (auto *R : RV)
25515ffd83dbSDimitry Andric     createIntrinsic(R, Defs);
25525ffd83dbSDimitry Andric 
25535ffd83dbSDimitry Andric   for (auto *I : Defs)
25545ffd83dbSDimitry Andric     I->indexBody();
25555ffd83dbSDimitry Andric 
25565ffd83dbSDimitry Andric   llvm::stable_sort(Defs, llvm::deref<std::less<>>());
25575ffd83dbSDimitry Andric 
25585ffd83dbSDimitry Andric   // Only emit a def when its requirements have been met.
25595ffd83dbSDimitry Andric   // FIXME: This loop could be made faster, but it's fast enough for now.
25605ffd83dbSDimitry Andric   bool MadeProgress = true;
25615ffd83dbSDimitry Andric   std::string InGuard;
25625ffd83dbSDimitry Andric   while (!Defs.empty() && MadeProgress) {
25635ffd83dbSDimitry Andric     MadeProgress = false;
25645ffd83dbSDimitry Andric 
25655ffd83dbSDimitry Andric     for (SmallVector<Intrinsic *, 128>::iterator I = Defs.begin();
25665ffd83dbSDimitry Andric          I != Defs.end(); /*No step*/) {
25675ffd83dbSDimitry Andric       bool DependenciesSatisfied = true;
25685ffd83dbSDimitry Andric       for (auto *II : (*I)->getDependencies()) {
25695ffd83dbSDimitry Andric         if (llvm::is_contained(Defs, II))
25705ffd83dbSDimitry Andric           DependenciesSatisfied = false;
25715ffd83dbSDimitry Andric       }
25725ffd83dbSDimitry Andric       if (!DependenciesSatisfied) {
25735ffd83dbSDimitry Andric         // Try the next one.
25745ffd83dbSDimitry Andric         ++I;
25755ffd83dbSDimitry Andric         continue;
25765ffd83dbSDimitry Andric       }
25775ffd83dbSDimitry Andric 
25785ffd83dbSDimitry Andric       // Emit #endif/#if pair if needed.
25795ffd83dbSDimitry Andric       if ((*I)->getGuard() != InGuard) {
25805ffd83dbSDimitry Andric         if (!InGuard.empty())
25815ffd83dbSDimitry Andric           OS << "#endif\n";
25825ffd83dbSDimitry Andric         InGuard = (*I)->getGuard();
25835ffd83dbSDimitry Andric         if (!InGuard.empty())
25845ffd83dbSDimitry Andric           OS << "#if " << InGuard << "\n";
25855ffd83dbSDimitry Andric       }
25865ffd83dbSDimitry Andric 
25875ffd83dbSDimitry Andric       // Actually generate the intrinsic code.
25885ffd83dbSDimitry Andric       OS << (*I)->generate();
25895ffd83dbSDimitry Andric 
25905ffd83dbSDimitry Andric       MadeProgress = true;
25915ffd83dbSDimitry Andric       I = Defs.erase(I);
25925ffd83dbSDimitry Andric     }
25935ffd83dbSDimitry Andric   }
25945ffd83dbSDimitry Andric   assert(Defs.empty() && "Some requirements were not satisfied!");
25955ffd83dbSDimitry Andric   if (!InGuard.empty())
25965ffd83dbSDimitry Andric     OS << "#endif\n";
25975ffd83dbSDimitry Andric 
25985ffd83dbSDimitry Andric   OS << "\n";
25995ffd83dbSDimitry Andric   OS << "#undef __ai\n\n";
26005ffd83dbSDimitry Andric 
26015ffd83dbSDimitry Andric   OS << "#endif\n";
26025ffd83dbSDimitry Andric }
26035ffd83dbSDimitry Andric 
2604a7dea167SDimitry Andric void clang::EmitNeon(RecordKeeper &Records, raw_ostream &OS) {
26050b57cec5SDimitry Andric   NeonEmitter(Records).run(OS);
26060b57cec5SDimitry Andric }
26070b57cec5SDimitry Andric 
2608a7dea167SDimitry Andric void clang::EmitFP16(RecordKeeper &Records, raw_ostream &OS) {
26090b57cec5SDimitry Andric   NeonEmitter(Records).runFP16(OS);
26100b57cec5SDimitry Andric }
26110b57cec5SDimitry Andric 
26125ffd83dbSDimitry Andric void clang::EmitBF16(RecordKeeper &Records, raw_ostream &OS) {
26135ffd83dbSDimitry Andric   NeonEmitter(Records).runBF16(OS);
26145ffd83dbSDimitry Andric }
26155ffd83dbSDimitry Andric 
2616a7dea167SDimitry Andric void clang::EmitNeonSema(RecordKeeper &Records, raw_ostream &OS) {
26170b57cec5SDimitry Andric   NeonEmitter(Records).runHeader(OS);
26180b57cec5SDimitry Andric }
26190b57cec5SDimitry Andric 
2620a7dea167SDimitry Andric void clang::EmitNeonTest(RecordKeeper &Records, raw_ostream &OS) {
26210b57cec5SDimitry Andric   llvm_unreachable("Neon test generation no longer implemented!");
26220b57cec5SDimitry Andric }
2623