106f32e7eSjoerg //===- NeonEmitter.cpp - Generate arm_neon.h for use with clang -*- C++ -*-===//
206f32e7eSjoerg //
306f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information.
506f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606f32e7eSjoerg //
706f32e7eSjoerg //===----------------------------------------------------------------------===//
806f32e7eSjoerg //
906f32e7eSjoerg // This tablegen backend is responsible for emitting arm_neon.h, which includes
1006f32e7eSjoerg // a declaration and definition of each function specified by the ARM NEON
1106f32e7eSjoerg // compiler interface.  See ARM document DUI0348B.
1206f32e7eSjoerg //
1306f32e7eSjoerg // Each NEON instruction is implemented in terms of 1 or more functions which
1406f32e7eSjoerg // are suffixed with the element type of the input vectors.  Functions may be
1506f32e7eSjoerg // implemented in terms of generic vector operations such as +, *, -, etc. or
1606f32e7eSjoerg // by calling a __builtin_-prefixed function which will be handled by clang's
1706f32e7eSjoerg // CodeGen library.
1806f32e7eSjoerg //
1906f32e7eSjoerg // Additional validation code can be generated by this file when runHeader() is
2006f32e7eSjoerg // called, rather than the normal run() entry point.
2106f32e7eSjoerg //
2206f32e7eSjoerg // See also the documentation in include/clang/Basic/arm_neon.td.
2306f32e7eSjoerg //
2406f32e7eSjoerg //===----------------------------------------------------------------------===//
2506f32e7eSjoerg 
2606f32e7eSjoerg #include "TableGenBackends.h"
2706f32e7eSjoerg #include "llvm/ADT/ArrayRef.h"
2806f32e7eSjoerg #include "llvm/ADT/DenseMap.h"
2906f32e7eSjoerg #include "llvm/ADT/None.h"
30*13fbcb42Sjoerg #include "llvm/ADT/Optional.h"
3106f32e7eSjoerg #include "llvm/ADT/STLExtras.h"
32*13fbcb42Sjoerg #include "llvm/ADT/SmallVector.h"
3306f32e7eSjoerg #include "llvm/ADT/StringExtras.h"
3406f32e7eSjoerg #include "llvm/ADT/StringRef.h"
3506f32e7eSjoerg #include "llvm/Support/Casting.h"
3606f32e7eSjoerg #include "llvm/Support/ErrorHandling.h"
3706f32e7eSjoerg #include "llvm/Support/raw_ostream.h"
3806f32e7eSjoerg #include "llvm/TableGen/Error.h"
3906f32e7eSjoerg #include "llvm/TableGen/Record.h"
4006f32e7eSjoerg #include "llvm/TableGen/SetTheory.h"
4106f32e7eSjoerg #include <algorithm>
4206f32e7eSjoerg #include <cassert>
4306f32e7eSjoerg #include <cctype>
4406f32e7eSjoerg #include <cstddef>
4506f32e7eSjoerg #include <cstdint>
4606f32e7eSjoerg #include <deque>
4706f32e7eSjoerg #include <map>
4806f32e7eSjoerg #include <set>
4906f32e7eSjoerg #include <sstream>
5006f32e7eSjoerg #include <string>
5106f32e7eSjoerg #include <utility>
5206f32e7eSjoerg #include <vector>
5306f32e7eSjoerg 
5406f32e7eSjoerg using namespace llvm;
5506f32e7eSjoerg 
5606f32e7eSjoerg namespace {
5706f32e7eSjoerg 
5806f32e7eSjoerg // While globals are generally bad, this one allows us to perform assertions
5906f32e7eSjoerg // liberally and somehow still trace them back to the def they indirectly
6006f32e7eSjoerg // came from.
6106f32e7eSjoerg static Record *CurrentRecord = nullptr;
assert_with_loc(bool Assertion,const std::string & Str)6206f32e7eSjoerg static void assert_with_loc(bool Assertion, const std::string &Str) {
6306f32e7eSjoerg   if (!Assertion) {
6406f32e7eSjoerg     if (CurrentRecord)
6506f32e7eSjoerg       PrintFatalError(CurrentRecord->getLoc(), Str);
6606f32e7eSjoerg     else
6706f32e7eSjoerg       PrintFatalError(Str);
6806f32e7eSjoerg   }
6906f32e7eSjoerg }
7006f32e7eSjoerg 
7106f32e7eSjoerg enum ClassKind {
7206f32e7eSjoerg   ClassNone,
7306f32e7eSjoerg   ClassI,     // generic integer instruction, e.g., "i8" suffix
7406f32e7eSjoerg   ClassS,     // signed/unsigned/poly, e.g., "s8", "u8" or "p8" suffix
7506f32e7eSjoerg   ClassW,     // width-specific instruction, e.g., "8" suffix
7606f32e7eSjoerg   ClassB,     // bitcast arguments with enum argument to specify type
7706f32e7eSjoerg   ClassL,     // Logical instructions which are op instructions
7806f32e7eSjoerg               // but we need to not emit any suffix for in our
7906f32e7eSjoerg               // tests.
8006f32e7eSjoerg   ClassNoTest // Instructions which we do not test since they are
8106f32e7eSjoerg               // not TRUE instructions.
8206f32e7eSjoerg };
8306f32e7eSjoerg 
8406f32e7eSjoerg /// NeonTypeFlags - Flags to identify the types for overloaded Neon
8506f32e7eSjoerg /// builtins.  These must be kept in sync with the flags in
8606f32e7eSjoerg /// include/clang/Basic/TargetBuiltins.h.
8706f32e7eSjoerg namespace NeonTypeFlags {
8806f32e7eSjoerg 
8906f32e7eSjoerg enum { EltTypeMask = 0xf, UnsignedFlag = 0x10, QuadFlag = 0x20 };
9006f32e7eSjoerg 
9106f32e7eSjoerg enum EltType {
9206f32e7eSjoerg   Int8,
9306f32e7eSjoerg   Int16,
9406f32e7eSjoerg   Int32,
9506f32e7eSjoerg   Int64,
9606f32e7eSjoerg   Poly8,
9706f32e7eSjoerg   Poly16,
9806f32e7eSjoerg   Poly64,
9906f32e7eSjoerg   Poly128,
10006f32e7eSjoerg   Float16,
10106f32e7eSjoerg   Float32,
102*13fbcb42Sjoerg   Float64,
103*13fbcb42Sjoerg   BFloat16
10406f32e7eSjoerg };
10506f32e7eSjoerg 
10606f32e7eSjoerg } // end namespace NeonTypeFlags
10706f32e7eSjoerg 
10806f32e7eSjoerg class NeonEmitter;
10906f32e7eSjoerg 
11006f32e7eSjoerg //===----------------------------------------------------------------------===//
11106f32e7eSjoerg // TypeSpec
11206f32e7eSjoerg //===----------------------------------------------------------------------===//
11306f32e7eSjoerg 
11406f32e7eSjoerg /// A TypeSpec is just a simple wrapper around a string, but gets its own type
11506f32e7eSjoerg /// for strong typing purposes.
11606f32e7eSjoerg ///
11706f32e7eSjoerg /// A TypeSpec can be used to create a type.
11806f32e7eSjoerg class TypeSpec : public std::string {
11906f32e7eSjoerg public:
fromTypeSpecs(StringRef Str)12006f32e7eSjoerg   static std::vector<TypeSpec> fromTypeSpecs(StringRef Str) {
12106f32e7eSjoerg     std::vector<TypeSpec> Ret;
12206f32e7eSjoerg     TypeSpec Acc;
12306f32e7eSjoerg     for (char I : Str.str()) {
12406f32e7eSjoerg       if (islower(I)) {
12506f32e7eSjoerg         Acc.push_back(I);
12606f32e7eSjoerg         Ret.push_back(TypeSpec(Acc));
12706f32e7eSjoerg         Acc.clear();
12806f32e7eSjoerg       } else {
12906f32e7eSjoerg         Acc.push_back(I);
13006f32e7eSjoerg       }
13106f32e7eSjoerg     }
13206f32e7eSjoerg     return Ret;
13306f32e7eSjoerg   }
13406f32e7eSjoerg };
13506f32e7eSjoerg 
13606f32e7eSjoerg //===----------------------------------------------------------------------===//
13706f32e7eSjoerg // Type
13806f32e7eSjoerg //===----------------------------------------------------------------------===//
13906f32e7eSjoerg 
14006f32e7eSjoerg /// A Type. Not much more to say here.
14106f32e7eSjoerg class Type {
14206f32e7eSjoerg private:
14306f32e7eSjoerg   TypeSpec TS;
14406f32e7eSjoerg 
145*13fbcb42Sjoerg   enum TypeKind {
146*13fbcb42Sjoerg     Void,
147*13fbcb42Sjoerg     Float,
148*13fbcb42Sjoerg     SInt,
149*13fbcb42Sjoerg     UInt,
150*13fbcb42Sjoerg     Poly,
151*13fbcb42Sjoerg     BFloat16,
152*13fbcb42Sjoerg   };
153*13fbcb42Sjoerg   TypeKind Kind;
154*13fbcb42Sjoerg   bool Immediate, Constant, Pointer;
15506f32e7eSjoerg   // ScalarForMangling and NoManglingQ are really not suited to live here as
15606f32e7eSjoerg   // they are not related to the type. But they live in the TypeSpec (not the
15706f32e7eSjoerg   // prototype), so this is really the only place to store them.
15806f32e7eSjoerg   bool ScalarForMangling, NoManglingQ;
15906f32e7eSjoerg   unsigned Bitwidth, ElementBitwidth, NumVectors;
16006f32e7eSjoerg 
16106f32e7eSjoerg public:
Type()16206f32e7eSjoerg   Type()
163*13fbcb42Sjoerg       : Kind(Void), Immediate(false), Constant(false),
164*13fbcb42Sjoerg         Pointer(false), ScalarForMangling(false), NoManglingQ(false),
165*13fbcb42Sjoerg         Bitwidth(0), ElementBitwidth(0), NumVectors(0) {}
16606f32e7eSjoerg 
Type(TypeSpec TS,StringRef CharMods)167*13fbcb42Sjoerg   Type(TypeSpec TS, StringRef CharMods)
168*13fbcb42Sjoerg       : TS(std::move(TS)), Kind(Void), Immediate(false),
169*13fbcb42Sjoerg         Constant(false), Pointer(false), ScalarForMangling(false),
170*13fbcb42Sjoerg         NoManglingQ(false), Bitwidth(0), ElementBitwidth(0), NumVectors(0) {
171*13fbcb42Sjoerg     applyModifiers(CharMods);
17206f32e7eSjoerg   }
17306f32e7eSjoerg 
17406f32e7eSjoerg   /// Returns a type representing "void".
getVoid()17506f32e7eSjoerg   static Type getVoid() { return Type(); }
17606f32e7eSjoerg 
operator ==(const Type & Other) const17706f32e7eSjoerg   bool operator==(const Type &Other) const { return str() == Other.str(); }
operator !=(const Type & Other) const17806f32e7eSjoerg   bool operator!=(const Type &Other) const { return !operator==(Other); }
17906f32e7eSjoerg 
18006f32e7eSjoerg   //
18106f32e7eSjoerg   // Query functions
18206f32e7eSjoerg   //
isScalarForMangling() const18306f32e7eSjoerg   bool isScalarForMangling() const { return ScalarForMangling; }
noManglingQ() const18406f32e7eSjoerg   bool noManglingQ() const { return NoManglingQ; }
18506f32e7eSjoerg 
isPointer() const18606f32e7eSjoerg   bool isPointer() const { return Pointer; }
isValue() const187*13fbcb42Sjoerg   bool isValue() const { return !isVoid() && !isPointer(); }
isScalar() const188*13fbcb42Sjoerg   bool isScalar() const { return isValue() && NumVectors == 0; }
isVector() const189*13fbcb42Sjoerg   bool isVector() const { return isValue() && NumVectors > 0; }
isConstPointer() const190*13fbcb42Sjoerg   bool isConstPointer() const { return Constant; }
isFloating() const191*13fbcb42Sjoerg   bool isFloating() const { return Kind == Float; }
isInteger() const192*13fbcb42Sjoerg   bool isInteger() const { return Kind == SInt || Kind == UInt; }
isPoly() const193*13fbcb42Sjoerg   bool isPoly() const { return Kind == Poly; }
isSigned() const194*13fbcb42Sjoerg   bool isSigned() const { return Kind == SInt; }
isImmediate() const19506f32e7eSjoerg   bool isImmediate() const { return Immediate; }
isFloat() const196*13fbcb42Sjoerg   bool isFloat() const { return isFloating() && ElementBitwidth == 32; }
isDouble() const197*13fbcb42Sjoerg   bool isDouble() const { return isFloating() && ElementBitwidth == 64; }
isHalf() const198*13fbcb42Sjoerg   bool isHalf() const { return isFloating() && ElementBitwidth == 16; }
isChar() const19906f32e7eSjoerg   bool isChar() const { return ElementBitwidth == 8; }
isShort() const200*13fbcb42Sjoerg   bool isShort() const { return isInteger() && ElementBitwidth == 16; }
isInt() const201*13fbcb42Sjoerg   bool isInt() const { return isInteger() && ElementBitwidth == 32; }
isLong() const202*13fbcb42Sjoerg   bool isLong() const { return isInteger() && ElementBitwidth == 64; }
isVoid() const203*13fbcb42Sjoerg   bool isVoid() const { return Kind == Void; }
isBFloat16() const204*13fbcb42Sjoerg   bool isBFloat16() const { return Kind == BFloat16; }
getNumElements() const20506f32e7eSjoerg   unsigned getNumElements() const { return Bitwidth / ElementBitwidth; }
getSizeInBits() const20606f32e7eSjoerg   unsigned getSizeInBits() const { return Bitwidth; }
getElementSizeInBits() const20706f32e7eSjoerg   unsigned getElementSizeInBits() const { return ElementBitwidth; }
getNumVectors() const20806f32e7eSjoerg   unsigned getNumVectors() const { return NumVectors; }
20906f32e7eSjoerg 
21006f32e7eSjoerg   //
21106f32e7eSjoerg   // Mutator functions
21206f32e7eSjoerg   //
makeUnsigned()213*13fbcb42Sjoerg   void makeUnsigned() {
214*13fbcb42Sjoerg     assert(!isVoid() && "not a potentially signed type");
215*13fbcb42Sjoerg     Kind = UInt;
216*13fbcb42Sjoerg   }
makeSigned()217*13fbcb42Sjoerg   void makeSigned() {
218*13fbcb42Sjoerg     assert(!isVoid() && "not a potentially signed type");
219*13fbcb42Sjoerg     Kind = SInt;
220*13fbcb42Sjoerg   }
22106f32e7eSjoerg 
makeInteger(unsigned ElemWidth,bool Sign)22206f32e7eSjoerg   void makeInteger(unsigned ElemWidth, bool Sign) {
223*13fbcb42Sjoerg     assert(!isVoid() && "converting void to int probably not useful");
224*13fbcb42Sjoerg     Kind = Sign ? SInt : UInt;
22506f32e7eSjoerg     Immediate = false;
22606f32e7eSjoerg     ElementBitwidth = ElemWidth;
22706f32e7eSjoerg   }
22806f32e7eSjoerg 
makeImmediate(unsigned ElemWidth)22906f32e7eSjoerg   void makeImmediate(unsigned ElemWidth) {
230*13fbcb42Sjoerg     Kind = SInt;
23106f32e7eSjoerg     Immediate = true;
23206f32e7eSjoerg     ElementBitwidth = ElemWidth;
23306f32e7eSjoerg   }
23406f32e7eSjoerg 
makeScalar()23506f32e7eSjoerg   void makeScalar() {
23606f32e7eSjoerg     Bitwidth = ElementBitwidth;
23706f32e7eSjoerg     NumVectors = 0;
23806f32e7eSjoerg   }
23906f32e7eSjoerg 
makeOneVector()24006f32e7eSjoerg   void makeOneVector() {
24106f32e7eSjoerg     assert(isVector());
24206f32e7eSjoerg     NumVectors = 1;
24306f32e7eSjoerg   }
24406f32e7eSjoerg 
make32BitElement()245*13fbcb42Sjoerg   void make32BitElement() {
246*13fbcb42Sjoerg     assert_with_loc(Bitwidth > 32, "Not enough bits to make it 32!");
247*13fbcb42Sjoerg     ElementBitwidth = 32;
248*13fbcb42Sjoerg   }
249*13fbcb42Sjoerg 
doubleLanes()25006f32e7eSjoerg   void doubleLanes() {
25106f32e7eSjoerg     assert_with_loc(Bitwidth != 128, "Can't get bigger than 128!");
25206f32e7eSjoerg     Bitwidth = 128;
25306f32e7eSjoerg   }
25406f32e7eSjoerg 
halveLanes()25506f32e7eSjoerg   void halveLanes() {
25606f32e7eSjoerg     assert_with_loc(Bitwidth != 64, "Can't get smaller than 64!");
25706f32e7eSjoerg     Bitwidth = 64;
25806f32e7eSjoerg   }
25906f32e7eSjoerg 
26006f32e7eSjoerg   /// Return the C string representation of a type, which is the typename
26106f32e7eSjoerg   /// defined in stdint.h or arm_neon.h.
26206f32e7eSjoerg   std::string str() const;
26306f32e7eSjoerg 
26406f32e7eSjoerg   /// Return the string representation of a type, which is an encoded
26506f32e7eSjoerg   /// string for passing to the BUILTIN() macro in Builtins.def.
26606f32e7eSjoerg   std::string builtin_str() const;
26706f32e7eSjoerg 
26806f32e7eSjoerg   /// Return the value in NeonTypeFlags for this type.
26906f32e7eSjoerg   unsigned getNeonEnum() const;
27006f32e7eSjoerg 
27106f32e7eSjoerg   /// Parse a type from a stdint.h or arm_neon.h typedef name,
27206f32e7eSjoerg   /// for example uint32x2_t or int64_t.
27306f32e7eSjoerg   static Type fromTypedefName(StringRef Name);
27406f32e7eSjoerg 
27506f32e7eSjoerg private:
27606f32e7eSjoerg   /// Creates the type based on the typespec string in TS.
27706f32e7eSjoerg   /// Sets "Quad" to true if the "Q" or "H" modifiers were
27806f32e7eSjoerg   /// seen. This is needed by applyModifier as some modifiers
27906f32e7eSjoerg   /// only take effect if the type size was changed by "Q" or "H".
28006f32e7eSjoerg   void applyTypespec(bool &Quad);
281*13fbcb42Sjoerg   /// Applies prototype modifiers to the type.
282*13fbcb42Sjoerg   void applyModifiers(StringRef Mods);
28306f32e7eSjoerg };
28406f32e7eSjoerg 
28506f32e7eSjoerg //===----------------------------------------------------------------------===//
28606f32e7eSjoerg // Variable
28706f32e7eSjoerg //===----------------------------------------------------------------------===//
28806f32e7eSjoerg 
28906f32e7eSjoerg /// A variable is a simple class that just has a type and a name.
29006f32e7eSjoerg class Variable {
29106f32e7eSjoerg   Type T;
29206f32e7eSjoerg   std::string N;
29306f32e7eSjoerg 
29406f32e7eSjoerg public:
Variable()29506f32e7eSjoerg   Variable() : T(Type::getVoid()), N("") {}
Variable(Type T,std::string N)29606f32e7eSjoerg   Variable(Type T, std::string N) : T(std::move(T)), N(std::move(N)) {}
29706f32e7eSjoerg 
getType() const29806f32e7eSjoerg   Type getType() const { return T; }
getName() const29906f32e7eSjoerg   std::string getName() const { return "__" + N; }
30006f32e7eSjoerg };
30106f32e7eSjoerg 
30206f32e7eSjoerg //===----------------------------------------------------------------------===//
30306f32e7eSjoerg // Intrinsic
30406f32e7eSjoerg //===----------------------------------------------------------------------===//
30506f32e7eSjoerg 
30606f32e7eSjoerg /// The main grunt class. This represents an instantiation of an intrinsic with
30706f32e7eSjoerg /// a particular typespec and prototype.
30806f32e7eSjoerg class Intrinsic {
30906f32e7eSjoerg   /// The Record this intrinsic was created from.
31006f32e7eSjoerg   Record *R;
311*13fbcb42Sjoerg   /// The unmangled name.
312*13fbcb42Sjoerg   std::string Name;
31306f32e7eSjoerg   /// The input and output typespecs. InTS == OutTS except when
314*13fbcb42Sjoerg   /// CartesianProductWith is non-empty - this is the case for vreinterpret.
31506f32e7eSjoerg   TypeSpec OutTS, InTS;
31606f32e7eSjoerg   /// The base class kind. Most intrinsics use ClassS, which has full type
31706f32e7eSjoerg   /// info for integers (s32/u32). Some use ClassI, which doesn't care about
31806f32e7eSjoerg   /// signedness (i32), while some (ClassB) have no type at all, only a width
31906f32e7eSjoerg   /// (32).
32006f32e7eSjoerg   ClassKind CK;
32106f32e7eSjoerg   /// The list of DAGs for the body. May be empty, in which case we should
32206f32e7eSjoerg   /// emit a builtin call.
32306f32e7eSjoerg   ListInit *Body;
32406f32e7eSjoerg   /// The architectural #ifdef guard.
32506f32e7eSjoerg   std::string Guard;
32606f32e7eSjoerg   /// Set if the Unavailable bit is 1. This means we don't generate a body,
32706f32e7eSjoerg   /// just an "unavailable" attribute on a declaration.
32806f32e7eSjoerg   bool IsUnavailable;
32906f32e7eSjoerg   /// Is this intrinsic safe for big-endian? or does it need its arguments
33006f32e7eSjoerg   /// reversing?
33106f32e7eSjoerg   bool BigEndianSafe;
33206f32e7eSjoerg 
33306f32e7eSjoerg   /// The types of return value [0] and parameters [1..].
33406f32e7eSjoerg   std::vector<Type> Types;
335*13fbcb42Sjoerg   /// The index of the key type passed to CGBuiltin.cpp for polymorphic calls.
336*13fbcb42Sjoerg   int PolymorphicKeyType;
33706f32e7eSjoerg   /// The local variables defined.
33806f32e7eSjoerg   std::map<std::string, Variable> Variables;
33906f32e7eSjoerg   /// NeededEarly - set if any other intrinsic depends on this intrinsic.
34006f32e7eSjoerg   bool NeededEarly;
34106f32e7eSjoerg   /// UseMacro - set if we should implement using a macro or unset for a
34206f32e7eSjoerg   ///            function.
34306f32e7eSjoerg   bool UseMacro;
34406f32e7eSjoerg   /// The set of intrinsics that this intrinsic uses/requires.
34506f32e7eSjoerg   std::set<Intrinsic *> Dependencies;
34606f32e7eSjoerg   /// The "base type", which is Type('d', OutTS). InBaseType is only
347*13fbcb42Sjoerg   /// different if CartesianProductWith is non-empty (for vreinterpret).
34806f32e7eSjoerg   Type BaseType, InBaseType;
34906f32e7eSjoerg   /// The return variable.
35006f32e7eSjoerg   Variable RetVar;
35106f32e7eSjoerg   /// A postfix to apply to every variable. Defaults to "".
35206f32e7eSjoerg   std::string VariablePostfix;
35306f32e7eSjoerg 
35406f32e7eSjoerg   NeonEmitter &Emitter;
35506f32e7eSjoerg   std::stringstream OS;
35606f32e7eSjoerg 
isBigEndianSafe() const35706f32e7eSjoerg   bool isBigEndianSafe() const {
35806f32e7eSjoerg     if (BigEndianSafe)
35906f32e7eSjoerg       return true;
36006f32e7eSjoerg 
36106f32e7eSjoerg     for (const auto &T : Types){
36206f32e7eSjoerg       if (T.isVector() && T.getNumElements() > 1)
36306f32e7eSjoerg         return false;
36406f32e7eSjoerg     }
36506f32e7eSjoerg     return true;
36606f32e7eSjoerg   }
36706f32e7eSjoerg 
36806f32e7eSjoerg public:
Intrinsic(Record * R,StringRef Name,StringRef Proto,TypeSpec OutTS,TypeSpec InTS,ClassKind CK,ListInit * Body,NeonEmitter & Emitter,StringRef Guard,bool IsUnavailable,bool BigEndianSafe)36906f32e7eSjoerg   Intrinsic(Record *R, StringRef Name, StringRef Proto, TypeSpec OutTS,
37006f32e7eSjoerg             TypeSpec InTS, ClassKind CK, ListInit *Body, NeonEmitter &Emitter,
37106f32e7eSjoerg             StringRef Guard, bool IsUnavailable, bool BigEndianSafe)
372*13fbcb42Sjoerg       : R(R), Name(Name.str()), OutTS(OutTS), InTS(InTS), CK(CK), Body(Body),
373*13fbcb42Sjoerg         Guard(Guard.str()), IsUnavailable(IsUnavailable),
374*13fbcb42Sjoerg         BigEndianSafe(BigEndianSafe), PolymorphicKeyType(0), NeededEarly(false),
375*13fbcb42Sjoerg         UseMacro(false), BaseType(OutTS, "."), InBaseType(InTS, "."),
376*13fbcb42Sjoerg         Emitter(Emitter) {
37706f32e7eSjoerg     // Modify the TypeSpec per-argument to get a concrete Type, and create
37806f32e7eSjoerg     // known variables for each.
37906f32e7eSjoerg     // Types[0] is the return value.
380*13fbcb42Sjoerg     unsigned Pos = 0;
381*13fbcb42Sjoerg     Types.emplace_back(OutTS, getNextModifiers(Proto, Pos));
382*13fbcb42Sjoerg     StringRef Mods = getNextModifiers(Proto, Pos);
383*13fbcb42Sjoerg     while (!Mods.empty()) {
384*13fbcb42Sjoerg       Types.emplace_back(InTS, Mods);
385*13fbcb42Sjoerg       if (Mods.find('!') != StringRef::npos)
386*13fbcb42Sjoerg         PolymorphicKeyType = Types.size() - 1;
387*13fbcb42Sjoerg 
388*13fbcb42Sjoerg       Mods = getNextModifiers(Proto, Pos);
389*13fbcb42Sjoerg     }
390*13fbcb42Sjoerg 
391*13fbcb42Sjoerg     for (auto Type : Types) {
392*13fbcb42Sjoerg       // If this builtin takes an immediate argument, we need to #define it rather
393*13fbcb42Sjoerg       // than use a standard declaration, so that SemaChecking can range check
394*13fbcb42Sjoerg       // the immediate passed by the user.
395*13fbcb42Sjoerg 
396*13fbcb42Sjoerg       // Pointer arguments need to use macros to avoid hiding aligned attributes
397*13fbcb42Sjoerg       // from the pointer type.
398*13fbcb42Sjoerg 
399*13fbcb42Sjoerg       // It is not permitted to pass or return an __fp16 by value, so intrinsics
400*13fbcb42Sjoerg       // taking a scalar float16_t must be implemented as macros.
401*13fbcb42Sjoerg       if (Type.isImmediate() || Type.isPointer() ||
402*13fbcb42Sjoerg           (Type.isScalar() && Type.isHalf()))
403*13fbcb42Sjoerg         UseMacro = true;
404*13fbcb42Sjoerg     }
40506f32e7eSjoerg   }
40606f32e7eSjoerg 
40706f32e7eSjoerg   /// Get the Record that this intrinsic is based off.
getRecord() const40806f32e7eSjoerg   Record *getRecord() const { return R; }
40906f32e7eSjoerg   /// Get the set of Intrinsics that this intrinsic calls.
41006f32e7eSjoerg   /// this is the set of immediate dependencies, NOT the
41106f32e7eSjoerg   /// transitive closure.
getDependencies() const41206f32e7eSjoerg   const std::set<Intrinsic *> &getDependencies() const { return Dependencies; }
41306f32e7eSjoerg   /// Get the architectural guard string (#ifdef).
getGuard() const41406f32e7eSjoerg   std::string getGuard() const { return Guard; }
41506f32e7eSjoerg   /// Get the non-mangled name.
getName() const41606f32e7eSjoerg   std::string getName() const { return Name; }
41706f32e7eSjoerg 
41806f32e7eSjoerg   /// Return true if the intrinsic takes an immediate operand.
hasImmediate() const41906f32e7eSjoerg   bool hasImmediate() const {
420*13fbcb42Sjoerg     return std::any_of(Types.begin(), Types.end(),
421*13fbcb42Sjoerg                        [](const Type &T) { return T.isImmediate(); });
42206f32e7eSjoerg   }
42306f32e7eSjoerg 
42406f32e7eSjoerg   /// Return the parameter index of the immediate operand.
getImmediateIdx() const42506f32e7eSjoerg   unsigned getImmediateIdx() const {
426*13fbcb42Sjoerg     for (unsigned Idx = 0; Idx < Types.size(); ++Idx)
427*13fbcb42Sjoerg       if (Types[Idx].isImmediate())
42806f32e7eSjoerg         return Idx - 1;
429*13fbcb42Sjoerg     llvm_unreachable("Intrinsic has no immediate");
43006f32e7eSjoerg   }
43106f32e7eSjoerg 
43206f32e7eSjoerg 
getNumParams() const433*13fbcb42Sjoerg   unsigned getNumParams() const { return Types.size() - 1; }
getReturnType() const43406f32e7eSjoerg   Type getReturnType() const { return Types[0]; }
getParamType(unsigned I) const43506f32e7eSjoerg   Type getParamType(unsigned I) const { return Types[I + 1]; }
getBaseType() const43606f32e7eSjoerg   Type getBaseType() const { return BaseType; }
getPolymorphicKeyType() const437*13fbcb42Sjoerg   Type getPolymorphicKeyType() const { return Types[PolymorphicKeyType]; }
43806f32e7eSjoerg 
43906f32e7eSjoerg   /// Return true if the prototype has a scalar argument.
44006f32e7eSjoerg   bool protoHasScalar() const;
44106f32e7eSjoerg 
44206f32e7eSjoerg   /// Return the index that parameter PIndex will sit at
44306f32e7eSjoerg   /// in a generated function call. This is often just PIndex,
44406f32e7eSjoerg   /// but may not be as things such as multiple-vector operands
44506f32e7eSjoerg   /// and sret parameters need to be taken into accont.
getGeneratedParamIdx(unsigned PIndex)44606f32e7eSjoerg   unsigned getGeneratedParamIdx(unsigned PIndex) {
44706f32e7eSjoerg     unsigned Idx = 0;
44806f32e7eSjoerg     if (getReturnType().getNumVectors() > 1)
44906f32e7eSjoerg       // Multiple vectors are passed as sret.
45006f32e7eSjoerg       ++Idx;
45106f32e7eSjoerg 
45206f32e7eSjoerg     for (unsigned I = 0; I < PIndex; ++I)
45306f32e7eSjoerg       Idx += std::max(1U, getParamType(I).getNumVectors());
45406f32e7eSjoerg 
45506f32e7eSjoerg     return Idx;
45606f32e7eSjoerg   }
45706f32e7eSjoerg 
hasBody() const45806f32e7eSjoerg   bool hasBody() const { return Body && !Body->getValues().empty(); }
45906f32e7eSjoerg 
setNeededEarly()46006f32e7eSjoerg   void setNeededEarly() { NeededEarly = true; }
46106f32e7eSjoerg 
operator <(const Intrinsic & Other) const46206f32e7eSjoerg   bool operator<(const Intrinsic &Other) const {
46306f32e7eSjoerg     // Sort lexicographically on a two-tuple (Guard, Name)
46406f32e7eSjoerg     if (Guard != Other.Guard)
46506f32e7eSjoerg       return Guard < Other.Guard;
46606f32e7eSjoerg     return Name < Other.Name;
46706f32e7eSjoerg   }
46806f32e7eSjoerg 
getClassKind(bool UseClassBIfScalar=false)46906f32e7eSjoerg   ClassKind getClassKind(bool UseClassBIfScalar = false) {
47006f32e7eSjoerg     if (UseClassBIfScalar && !protoHasScalar())
47106f32e7eSjoerg       return ClassB;
47206f32e7eSjoerg     return CK;
47306f32e7eSjoerg   }
47406f32e7eSjoerg 
47506f32e7eSjoerg   /// Return the name, mangled with type information.
47606f32e7eSjoerg   /// If ForceClassS is true, use ClassS (u32/s32) instead
47706f32e7eSjoerg   /// of the intrinsic's own type class.
47806f32e7eSjoerg   std::string getMangledName(bool ForceClassS = false) const;
47906f32e7eSjoerg   /// Return the type code for a builtin function call.
48006f32e7eSjoerg   std::string getInstTypeCode(Type T, ClassKind CK) const;
48106f32e7eSjoerg   /// Return the type string for a BUILTIN() macro in Builtins.def.
48206f32e7eSjoerg   std::string getBuiltinTypeStr();
48306f32e7eSjoerg 
48406f32e7eSjoerg   /// Generate the intrinsic, returning code.
48506f32e7eSjoerg   std::string generate();
48606f32e7eSjoerg   /// Perform type checking and populate the dependency graph, but
48706f32e7eSjoerg   /// don't generate code yet.
48806f32e7eSjoerg   void indexBody();
48906f32e7eSjoerg 
49006f32e7eSjoerg private:
491*13fbcb42Sjoerg   StringRef getNextModifiers(StringRef Proto, unsigned &Pos) const;
492*13fbcb42Sjoerg 
49306f32e7eSjoerg   std::string mangleName(std::string Name, ClassKind CK) const;
49406f32e7eSjoerg 
49506f32e7eSjoerg   void initVariables();
49606f32e7eSjoerg   std::string replaceParamsIn(std::string S);
49706f32e7eSjoerg 
49806f32e7eSjoerg   void emitBodyAsBuiltinCall();
49906f32e7eSjoerg 
50006f32e7eSjoerg   void generateImpl(bool ReverseArguments,
50106f32e7eSjoerg                     StringRef NamePrefix, StringRef CallPrefix);
50206f32e7eSjoerg   void emitReturn();
50306f32e7eSjoerg   void emitBody(StringRef CallPrefix);
50406f32e7eSjoerg   void emitShadowedArgs();
50506f32e7eSjoerg   void emitArgumentReversal();
50606f32e7eSjoerg   void emitReturnReversal();
50706f32e7eSjoerg   void emitReverseVariable(Variable &Dest, Variable &Src);
50806f32e7eSjoerg   void emitNewLine();
50906f32e7eSjoerg   void emitClosingBrace();
51006f32e7eSjoerg   void emitOpeningBrace();
51106f32e7eSjoerg   void emitPrototype(StringRef NamePrefix);
51206f32e7eSjoerg 
51306f32e7eSjoerg   class DagEmitter {
51406f32e7eSjoerg     Intrinsic &Intr;
51506f32e7eSjoerg     StringRef CallPrefix;
51606f32e7eSjoerg 
51706f32e7eSjoerg   public:
DagEmitter(Intrinsic & Intr,StringRef CallPrefix)51806f32e7eSjoerg     DagEmitter(Intrinsic &Intr, StringRef CallPrefix) :
51906f32e7eSjoerg       Intr(Intr), CallPrefix(CallPrefix) {
52006f32e7eSjoerg     }
52106f32e7eSjoerg     std::pair<Type, std::string> emitDagArg(Init *Arg, std::string ArgName);
52206f32e7eSjoerg     std::pair<Type, std::string> emitDagSaveTemp(DagInit *DI);
52306f32e7eSjoerg     std::pair<Type, std::string> emitDagSplat(DagInit *DI);
52406f32e7eSjoerg     std::pair<Type, std::string> emitDagDup(DagInit *DI);
52506f32e7eSjoerg     std::pair<Type, std::string> emitDagDupTyped(DagInit *DI);
52606f32e7eSjoerg     std::pair<Type, std::string> emitDagShuffle(DagInit *DI);
52706f32e7eSjoerg     std::pair<Type, std::string> emitDagCast(DagInit *DI, bool IsBitCast);
528*13fbcb42Sjoerg     std::pair<Type, std::string> emitDagCall(DagInit *DI,
529*13fbcb42Sjoerg                                              bool MatchMangledName);
53006f32e7eSjoerg     std::pair<Type, std::string> emitDagNameReplace(DagInit *DI);
53106f32e7eSjoerg     std::pair<Type, std::string> emitDagLiteral(DagInit *DI);
53206f32e7eSjoerg     std::pair<Type, std::string> emitDagOp(DagInit *DI);
53306f32e7eSjoerg     std::pair<Type, std::string> emitDag(DagInit *DI);
53406f32e7eSjoerg   };
53506f32e7eSjoerg };
53606f32e7eSjoerg 
53706f32e7eSjoerg //===----------------------------------------------------------------------===//
53806f32e7eSjoerg // NeonEmitter
53906f32e7eSjoerg //===----------------------------------------------------------------------===//
54006f32e7eSjoerg 
54106f32e7eSjoerg class NeonEmitter {
54206f32e7eSjoerg   RecordKeeper &Records;
54306f32e7eSjoerg   DenseMap<Record *, ClassKind> ClassMap;
54406f32e7eSjoerg   std::map<std::string, std::deque<Intrinsic>> IntrinsicMap;
54506f32e7eSjoerg   unsigned UniqueNumber;
54606f32e7eSjoerg 
54706f32e7eSjoerg   void createIntrinsic(Record *R, SmallVectorImpl<Intrinsic *> &Out);
54806f32e7eSjoerg   void genBuiltinsDef(raw_ostream &OS, SmallVectorImpl<Intrinsic *> &Defs);
54906f32e7eSjoerg   void genOverloadTypeCheckCode(raw_ostream &OS,
55006f32e7eSjoerg                                 SmallVectorImpl<Intrinsic *> &Defs);
55106f32e7eSjoerg   void genIntrinsicRangeCheckCode(raw_ostream &OS,
55206f32e7eSjoerg                                   SmallVectorImpl<Intrinsic *> &Defs);
55306f32e7eSjoerg 
55406f32e7eSjoerg public:
55506f32e7eSjoerg   /// Called by Intrinsic - this attempts to get an intrinsic that takes
55606f32e7eSjoerg   /// the given types as arguments.
557*13fbcb42Sjoerg   Intrinsic &getIntrinsic(StringRef Name, ArrayRef<Type> Types,
558*13fbcb42Sjoerg                           Optional<std::string> MangledName);
55906f32e7eSjoerg 
56006f32e7eSjoerg   /// Called by Intrinsic - returns a globally-unique number.
getUniqueNumber()56106f32e7eSjoerg   unsigned getUniqueNumber() { return UniqueNumber++; }
56206f32e7eSjoerg 
NeonEmitter(RecordKeeper & R)56306f32e7eSjoerg   NeonEmitter(RecordKeeper &R) : Records(R), UniqueNumber(0) {
56406f32e7eSjoerg     Record *SI = R.getClass("SInst");
56506f32e7eSjoerg     Record *II = R.getClass("IInst");
56606f32e7eSjoerg     Record *WI = R.getClass("WInst");
56706f32e7eSjoerg     Record *SOpI = R.getClass("SOpInst");
56806f32e7eSjoerg     Record *IOpI = R.getClass("IOpInst");
56906f32e7eSjoerg     Record *WOpI = R.getClass("WOpInst");
57006f32e7eSjoerg     Record *LOpI = R.getClass("LOpInst");
57106f32e7eSjoerg     Record *NoTestOpI = R.getClass("NoTestOpInst");
57206f32e7eSjoerg 
57306f32e7eSjoerg     ClassMap[SI] = ClassS;
57406f32e7eSjoerg     ClassMap[II] = ClassI;
57506f32e7eSjoerg     ClassMap[WI] = ClassW;
57606f32e7eSjoerg     ClassMap[SOpI] = ClassS;
57706f32e7eSjoerg     ClassMap[IOpI] = ClassI;
57806f32e7eSjoerg     ClassMap[WOpI] = ClassW;
57906f32e7eSjoerg     ClassMap[LOpI] = ClassL;
58006f32e7eSjoerg     ClassMap[NoTestOpI] = ClassNoTest;
58106f32e7eSjoerg   }
58206f32e7eSjoerg 
583*13fbcb42Sjoerg   // Emit arm_neon.h.inc
58406f32e7eSjoerg   void run(raw_ostream &o);
58506f32e7eSjoerg 
586*13fbcb42Sjoerg   // Emit arm_fp16.h.inc
58706f32e7eSjoerg   void runFP16(raw_ostream &o);
58806f32e7eSjoerg 
589*13fbcb42Sjoerg   // Emit arm_bf16.h.inc
590*13fbcb42Sjoerg   void runBF16(raw_ostream &o);
59106f32e7eSjoerg 
592*13fbcb42Sjoerg   // Emit all the __builtin prototypes used in arm_neon.h, arm_fp16.h and
593*13fbcb42Sjoerg   // arm_bf16.h
594*13fbcb42Sjoerg   void runHeader(raw_ostream &o);
59506f32e7eSjoerg };
59606f32e7eSjoerg 
59706f32e7eSjoerg } // end anonymous namespace
59806f32e7eSjoerg 
59906f32e7eSjoerg //===----------------------------------------------------------------------===//
60006f32e7eSjoerg // Type implementation
60106f32e7eSjoerg //===----------------------------------------------------------------------===//
60206f32e7eSjoerg 
str() const60306f32e7eSjoerg std::string Type::str() const {
604*13fbcb42Sjoerg   if (isVoid())
60506f32e7eSjoerg     return "void";
60606f32e7eSjoerg   std::string S;
60706f32e7eSjoerg 
608*13fbcb42Sjoerg   if (isInteger() && !isSigned())
60906f32e7eSjoerg     S += "u";
61006f32e7eSjoerg 
611*13fbcb42Sjoerg   if (isPoly())
61206f32e7eSjoerg     S += "poly";
613*13fbcb42Sjoerg   else if (isFloating())
61406f32e7eSjoerg     S += "float";
615*13fbcb42Sjoerg   else if (isBFloat16())
616*13fbcb42Sjoerg     S += "bfloat";
61706f32e7eSjoerg   else
61806f32e7eSjoerg     S += "int";
61906f32e7eSjoerg 
62006f32e7eSjoerg   S += utostr(ElementBitwidth);
62106f32e7eSjoerg   if (isVector())
62206f32e7eSjoerg     S += "x" + utostr(getNumElements());
62306f32e7eSjoerg   if (NumVectors > 1)
62406f32e7eSjoerg     S += "x" + utostr(NumVectors);
62506f32e7eSjoerg   S += "_t";
62606f32e7eSjoerg 
62706f32e7eSjoerg   if (Constant)
62806f32e7eSjoerg     S += " const";
62906f32e7eSjoerg   if (Pointer)
63006f32e7eSjoerg     S += " *";
63106f32e7eSjoerg 
63206f32e7eSjoerg   return S;
63306f32e7eSjoerg }
63406f32e7eSjoerg 
builtin_str() const63506f32e7eSjoerg std::string Type::builtin_str() const {
63606f32e7eSjoerg   std::string S;
63706f32e7eSjoerg   if (isVoid())
63806f32e7eSjoerg     return "v";
63906f32e7eSjoerg 
640*13fbcb42Sjoerg   if (isPointer()) {
64106f32e7eSjoerg     // All pointers are void pointers.
642*13fbcb42Sjoerg     S = "v";
643*13fbcb42Sjoerg     if (isConstPointer())
644*13fbcb42Sjoerg       S += "C";
645*13fbcb42Sjoerg     S += "*";
646*13fbcb42Sjoerg     return S;
647*13fbcb42Sjoerg   } else if (isInteger())
64806f32e7eSjoerg     switch (ElementBitwidth) {
64906f32e7eSjoerg     case 8: S += "c"; break;
65006f32e7eSjoerg     case 16: S += "s"; break;
65106f32e7eSjoerg     case 32: S += "i"; break;
65206f32e7eSjoerg     case 64: S += "Wi"; break;
65306f32e7eSjoerg     case 128: S += "LLLi"; break;
65406f32e7eSjoerg     default: llvm_unreachable("Unhandled case!");
65506f32e7eSjoerg     }
656*13fbcb42Sjoerg   else if (isBFloat16()) {
657*13fbcb42Sjoerg     assert(ElementBitwidth == 16 && "BFloat16 can only be 16 bits");
658*13fbcb42Sjoerg     S += "y";
659*13fbcb42Sjoerg   } else
66006f32e7eSjoerg     switch (ElementBitwidth) {
66106f32e7eSjoerg     case 16: S += "h"; break;
66206f32e7eSjoerg     case 32: S += "f"; break;
66306f32e7eSjoerg     case 64: S += "d"; break;
66406f32e7eSjoerg     default: llvm_unreachable("Unhandled case!");
66506f32e7eSjoerg     }
66606f32e7eSjoerg 
667*13fbcb42Sjoerg   // FIXME: NECESSARY???????????????????????????????????????????????????????????????????????
668*13fbcb42Sjoerg   if (isChar() && !isPointer() && isSigned())
66906f32e7eSjoerg     // Make chars explicitly signed.
67006f32e7eSjoerg     S = "S" + S;
671*13fbcb42Sjoerg   else if (isInteger() && !isSigned())
67206f32e7eSjoerg     S = "U" + S;
67306f32e7eSjoerg 
67406f32e7eSjoerg   // Constant indices are "int", but have the "constant expression" modifier.
67506f32e7eSjoerg   if (isImmediate()) {
67606f32e7eSjoerg     assert(isInteger() && isSigned());
67706f32e7eSjoerg     S = "I" + S;
67806f32e7eSjoerg   }
67906f32e7eSjoerg 
680*13fbcb42Sjoerg   if (isScalar())
68106f32e7eSjoerg     return S;
68206f32e7eSjoerg 
68306f32e7eSjoerg   std::string Ret;
68406f32e7eSjoerg   for (unsigned I = 0; I < NumVectors; ++I)
68506f32e7eSjoerg     Ret += "V" + utostr(getNumElements()) + S;
68606f32e7eSjoerg 
68706f32e7eSjoerg   return Ret;
68806f32e7eSjoerg }
68906f32e7eSjoerg 
getNeonEnum() const69006f32e7eSjoerg unsigned Type::getNeonEnum() const {
69106f32e7eSjoerg   unsigned Addend;
69206f32e7eSjoerg   switch (ElementBitwidth) {
69306f32e7eSjoerg   case 8: Addend = 0; break;
69406f32e7eSjoerg   case 16: Addend = 1; break;
69506f32e7eSjoerg   case 32: Addend = 2; break;
69606f32e7eSjoerg   case 64: Addend = 3; break;
69706f32e7eSjoerg   case 128: Addend = 4; break;
69806f32e7eSjoerg   default: llvm_unreachable("Unhandled element bitwidth!");
69906f32e7eSjoerg   }
70006f32e7eSjoerg 
70106f32e7eSjoerg   unsigned Base = (unsigned)NeonTypeFlags::Int8 + Addend;
702*13fbcb42Sjoerg   if (isPoly()) {
70306f32e7eSjoerg     // Adjustment needed because Poly32 doesn't exist.
70406f32e7eSjoerg     if (Addend >= 2)
70506f32e7eSjoerg       --Addend;
70606f32e7eSjoerg     Base = (unsigned)NeonTypeFlags::Poly8 + Addend;
70706f32e7eSjoerg   }
708*13fbcb42Sjoerg   if (isFloating()) {
70906f32e7eSjoerg     assert(Addend != 0 && "Float8 doesn't exist!");
71006f32e7eSjoerg     Base = (unsigned)NeonTypeFlags::Float16 + (Addend - 1);
71106f32e7eSjoerg   }
71206f32e7eSjoerg 
713*13fbcb42Sjoerg   if (isBFloat16()) {
714*13fbcb42Sjoerg     assert(Addend == 1 && "BFloat16 is only 16 bit");
715*13fbcb42Sjoerg     Base = (unsigned)NeonTypeFlags::BFloat16;
716*13fbcb42Sjoerg   }
717*13fbcb42Sjoerg 
71806f32e7eSjoerg   if (Bitwidth == 128)
71906f32e7eSjoerg     Base |= (unsigned)NeonTypeFlags::QuadFlag;
720*13fbcb42Sjoerg   if (isInteger() && !isSigned())
72106f32e7eSjoerg     Base |= (unsigned)NeonTypeFlags::UnsignedFlag;
72206f32e7eSjoerg 
72306f32e7eSjoerg   return Base;
72406f32e7eSjoerg }
72506f32e7eSjoerg 
fromTypedefName(StringRef Name)72606f32e7eSjoerg Type Type::fromTypedefName(StringRef Name) {
72706f32e7eSjoerg   Type T;
728*13fbcb42Sjoerg   T.Kind = SInt;
72906f32e7eSjoerg 
73006f32e7eSjoerg   if (Name.front() == 'u') {
731*13fbcb42Sjoerg     T.Kind = UInt;
73206f32e7eSjoerg     Name = Name.drop_front();
73306f32e7eSjoerg   }
73406f32e7eSjoerg 
73506f32e7eSjoerg   if (Name.startswith("float")) {
736*13fbcb42Sjoerg     T.Kind = Float;
73706f32e7eSjoerg     Name = Name.drop_front(5);
73806f32e7eSjoerg   } else if (Name.startswith("poly")) {
739*13fbcb42Sjoerg     T.Kind = Poly;
74006f32e7eSjoerg     Name = Name.drop_front(4);
741*13fbcb42Sjoerg   } else if (Name.startswith("bfloat")) {
742*13fbcb42Sjoerg     T.Kind = BFloat16;
743*13fbcb42Sjoerg     Name = Name.drop_front(6);
74406f32e7eSjoerg   } else {
74506f32e7eSjoerg     assert(Name.startswith("int"));
74606f32e7eSjoerg     Name = Name.drop_front(3);
74706f32e7eSjoerg   }
74806f32e7eSjoerg 
74906f32e7eSjoerg   unsigned I = 0;
75006f32e7eSjoerg   for (I = 0; I < Name.size(); ++I) {
75106f32e7eSjoerg     if (!isdigit(Name[I]))
75206f32e7eSjoerg       break;
75306f32e7eSjoerg   }
75406f32e7eSjoerg   Name.substr(0, I).getAsInteger(10, T.ElementBitwidth);
75506f32e7eSjoerg   Name = Name.drop_front(I);
75606f32e7eSjoerg 
75706f32e7eSjoerg   T.Bitwidth = T.ElementBitwidth;
75806f32e7eSjoerg   T.NumVectors = 1;
75906f32e7eSjoerg 
76006f32e7eSjoerg   if (Name.front() == 'x') {
76106f32e7eSjoerg     Name = Name.drop_front();
76206f32e7eSjoerg     unsigned I = 0;
76306f32e7eSjoerg     for (I = 0; I < Name.size(); ++I) {
76406f32e7eSjoerg       if (!isdigit(Name[I]))
76506f32e7eSjoerg         break;
76606f32e7eSjoerg     }
76706f32e7eSjoerg     unsigned NumLanes;
76806f32e7eSjoerg     Name.substr(0, I).getAsInteger(10, NumLanes);
76906f32e7eSjoerg     Name = Name.drop_front(I);
77006f32e7eSjoerg     T.Bitwidth = T.ElementBitwidth * NumLanes;
77106f32e7eSjoerg   } else {
77206f32e7eSjoerg     // Was scalar.
77306f32e7eSjoerg     T.NumVectors = 0;
77406f32e7eSjoerg   }
77506f32e7eSjoerg   if (Name.front() == 'x') {
77606f32e7eSjoerg     Name = Name.drop_front();
77706f32e7eSjoerg     unsigned I = 0;
77806f32e7eSjoerg     for (I = 0; I < Name.size(); ++I) {
77906f32e7eSjoerg       if (!isdigit(Name[I]))
78006f32e7eSjoerg         break;
78106f32e7eSjoerg     }
78206f32e7eSjoerg     Name.substr(0, I).getAsInteger(10, T.NumVectors);
78306f32e7eSjoerg     Name = Name.drop_front(I);
78406f32e7eSjoerg   }
78506f32e7eSjoerg 
78606f32e7eSjoerg   assert(Name.startswith("_t") && "Malformed typedef!");
78706f32e7eSjoerg   return T;
78806f32e7eSjoerg }
78906f32e7eSjoerg 
applyTypespec(bool & Quad)79006f32e7eSjoerg void Type::applyTypespec(bool &Quad) {
79106f32e7eSjoerg   std::string S = TS;
79206f32e7eSjoerg   ScalarForMangling = false;
793*13fbcb42Sjoerg   Kind = SInt;
79406f32e7eSjoerg   ElementBitwidth = ~0U;
79506f32e7eSjoerg   NumVectors = 1;
79606f32e7eSjoerg 
79706f32e7eSjoerg   for (char I : S) {
79806f32e7eSjoerg     switch (I) {
79906f32e7eSjoerg     case 'S':
80006f32e7eSjoerg       ScalarForMangling = true;
80106f32e7eSjoerg       break;
80206f32e7eSjoerg     case 'H':
80306f32e7eSjoerg       NoManglingQ = true;
80406f32e7eSjoerg       Quad = true;
80506f32e7eSjoerg       break;
80606f32e7eSjoerg     case 'Q':
80706f32e7eSjoerg       Quad = true;
80806f32e7eSjoerg       break;
80906f32e7eSjoerg     case 'P':
810*13fbcb42Sjoerg       Kind = Poly;
81106f32e7eSjoerg       break;
81206f32e7eSjoerg     case 'U':
813*13fbcb42Sjoerg       Kind = UInt;
81406f32e7eSjoerg       break;
81506f32e7eSjoerg     case 'c':
81606f32e7eSjoerg       ElementBitwidth = 8;
81706f32e7eSjoerg       break;
81806f32e7eSjoerg     case 'h':
819*13fbcb42Sjoerg       Kind = Float;
82006f32e7eSjoerg       LLVM_FALLTHROUGH;
82106f32e7eSjoerg     case 's':
82206f32e7eSjoerg       ElementBitwidth = 16;
82306f32e7eSjoerg       break;
82406f32e7eSjoerg     case 'f':
825*13fbcb42Sjoerg       Kind = Float;
82606f32e7eSjoerg       LLVM_FALLTHROUGH;
82706f32e7eSjoerg     case 'i':
82806f32e7eSjoerg       ElementBitwidth = 32;
82906f32e7eSjoerg       break;
83006f32e7eSjoerg     case 'd':
831*13fbcb42Sjoerg       Kind = Float;
83206f32e7eSjoerg       LLVM_FALLTHROUGH;
83306f32e7eSjoerg     case 'l':
83406f32e7eSjoerg       ElementBitwidth = 64;
83506f32e7eSjoerg       break;
83606f32e7eSjoerg     case 'k':
83706f32e7eSjoerg       ElementBitwidth = 128;
83806f32e7eSjoerg       // Poly doesn't have a 128x1 type.
839*13fbcb42Sjoerg       if (isPoly())
84006f32e7eSjoerg         NumVectors = 0;
84106f32e7eSjoerg       break;
842*13fbcb42Sjoerg     case 'b':
843*13fbcb42Sjoerg       Kind = BFloat16;
844*13fbcb42Sjoerg       ElementBitwidth = 16;
845*13fbcb42Sjoerg       break;
84606f32e7eSjoerg     default:
84706f32e7eSjoerg       llvm_unreachable("Unhandled type code!");
84806f32e7eSjoerg     }
84906f32e7eSjoerg   }
85006f32e7eSjoerg   assert(ElementBitwidth != ~0U && "Bad element bitwidth!");
85106f32e7eSjoerg 
85206f32e7eSjoerg   Bitwidth = Quad ? 128 : 64;
85306f32e7eSjoerg }
85406f32e7eSjoerg 
applyModifiers(StringRef Mods)855*13fbcb42Sjoerg void Type::applyModifiers(StringRef Mods) {
85606f32e7eSjoerg   bool AppliedQuad = false;
85706f32e7eSjoerg   applyTypespec(AppliedQuad);
85806f32e7eSjoerg 
859*13fbcb42Sjoerg   for (char Mod : Mods) {
86006f32e7eSjoerg     switch (Mod) {
861*13fbcb42Sjoerg     case '.':
862*13fbcb42Sjoerg       break;
86306f32e7eSjoerg     case 'v':
864*13fbcb42Sjoerg       Kind = Void;
86506f32e7eSjoerg       break;
866*13fbcb42Sjoerg     case 'S':
867*13fbcb42Sjoerg       Kind = SInt;
86806f32e7eSjoerg       break;
86906f32e7eSjoerg     case 'U':
870*13fbcb42Sjoerg       Kind = UInt;
87106f32e7eSjoerg       break;
872*13fbcb42Sjoerg     case 'B':
873*13fbcb42Sjoerg       Kind = BFloat16;
874*13fbcb42Sjoerg       ElementBitwidth = 16;
87506f32e7eSjoerg       break;
87606f32e7eSjoerg     case 'F':
877*13fbcb42Sjoerg       Kind = Float;
87806f32e7eSjoerg       break;
879*13fbcb42Sjoerg     case 'P':
880*13fbcb42Sjoerg       Kind = Poly;
88106f32e7eSjoerg       break;
882*13fbcb42Sjoerg     case '>':
883*13fbcb42Sjoerg       assert(ElementBitwidth < 128);
884*13fbcb42Sjoerg       ElementBitwidth *= 2;
885*13fbcb42Sjoerg       break;
886*13fbcb42Sjoerg     case '<':
887*13fbcb42Sjoerg       assert(ElementBitwidth > 8);
888*13fbcb42Sjoerg       ElementBitwidth /= 2;
88906f32e7eSjoerg       break;
89006f32e7eSjoerg     case '1':
89106f32e7eSjoerg       NumVectors = 0;
89206f32e7eSjoerg       break;
89306f32e7eSjoerg     case '2':
89406f32e7eSjoerg       NumVectors = 2;
89506f32e7eSjoerg       break;
89606f32e7eSjoerg     case '3':
89706f32e7eSjoerg       NumVectors = 3;
89806f32e7eSjoerg       break;
89906f32e7eSjoerg     case '4':
90006f32e7eSjoerg       NumVectors = 4;
90106f32e7eSjoerg       break;
902*13fbcb42Sjoerg     case '*':
903*13fbcb42Sjoerg       Pointer = true;
90406f32e7eSjoerg       break;
905*13fbcb42Sjoerg     case 'c':
906*13fbcb42Sjoerg       Constant = true;
90706f32e7eSjoerg       break;
908*13fbcb42Sjoerg     case 'Q':
909*13fbcb42Sjoerg       Bitwidth = 128;
91006f32e7eSjoerg       break;
911*13fbcb42Sjoerg     case 'q':
912*13fbcb42Sjoerg       Bitwidth = 64;
91306f32e7eSjoerg       break;
914*13fbcb42Sjoerg     case 'I':
915*13fbcb42Sjoerg       Kind = SInt;
916*13fbcb42Sjoerg       ElementBitwidth = Bitwidth = 32;
917*13fbcb42Sjoerg       NumVectors = 0;
918*13fbcb42Sjoerg       Immediate = true;
91906f32e7eSjoerg       break;
920*13fbcb42Sjoerg     case 'p':
921*13fbcb42Sjoerg       if (isPoly())
922*13fbcb42Sjoerg         Kind = UInt;
923*13fbcb42Sjoerg       break;
924*13fbcb42Sjoerg     case '!':
925*13fbcb42Sjoerg       // Key type, handled elsewhere.
92606f32e7eSjoerg       break;
92706f32e7eSjoerg     default:
92806f32e7eSjoerg       llvm_unreachable("Unhandled character!");
92906f32e7eSjoerg     }
93006f32e7eSjoerg   }
931*13fbcb42Sjoerg }
93206f32e7eSjoerg 
93306f32e7eSjoerg //===----------------------------------------------------------------------===//
93406f32e7eSjoerg // Intrinsic implementation
93506f32e7eSjoerg //===----------------------------------------------------------------------===//
93606f32e7eSjoerg 
getNextModifiers(StringRef Proto,unsigned & Pos) const937*13fbcb42Sjoerg StringRef Intrinsic::getNextModifiers(StringRef Proto, unsigned &Pos) const {
938*13fbcb42Sjoerg   if (Proto.size() == Pos)
939*13fbcb42Sjoerg     return StringRef();
940*13fbcb42Sjoerg   else if (Proto[Pos] != '(')
941*13fbcb42Sjoerg     return Proto.substr(Pos++, 1);
942*13fbcb42Sjoerg 
943*13fbcb42Sjoerg   size_t Start = Pos + 1;
944*13fbcb42Sjoerg   size_t End = Proto.find(')', Start);
945*13fbcb42Sjoerg   assert_with_loc(End != StringRef::npos, "unmatched modifier group paren");
946*13fbcb42Sjoerg   Pos = End + 1;
947*13fbcb42Sjoerg   return Proto.slice(Start, End);
948*13fbcb42Sjoerg }
949*13fbcb42Sjoerg 
getInstTypeCode(Type T,ClassKind CK) const95006f32e7eSjoerg std::string Intrinsic::getInstTypeCode(Type T, ClassKind CK) const {
95106f32e7eSjoerg   char typeCode = '\0';
95206f32e7eSjoerg   bool printNumber = true;
95306f32e7eSjoerg 
95406f32e7eSjoerg   if (CK == ClassB)
95506f32e7eSjoerg     return "";
95606f32e7eSjoerg 
957*13fbcb42Sjoerg   if (T.isBFloat16())
958*13fbcb42Sjoerg     return "bf16";
959*13fbcb42Sjoerg 
96006f32e7eSjoerg   if (T.isPoly())
96106f32e7eSjoerg     typeCode = 'p';
96206f32e7eSjoerg   else if (T.isInteger())
96306f32e7eSjoerg     typeCode = T.isSigned() ? 's' : 'u';
96406f32e7eSjoerg   else
96506f32e7eSjoerg     typeCode = 'f';
96606f32e7eSjoerg 
96706f32e7eSjoerg   if (CK == ClassI) {
96806f32e7eSjoerg     switch (typeCode) {
96906f32e7eSjoerg     default:
97006f32e7eSjoerg       break;
97106f32e7eSjoerg     case 's':
97206f32e7eSjoerg     case 'u':
97306f32e7eSjoerg     case 'p':
97406f32e7eSjoerg       typeCode = 'i';
97506f32e7eSjoerg       break;
97606f32e7eSjoerg     }
97706f32e7eSjoerg   }
97806f32e7eSjoerg   if (CK == ClassB) {
97906f32e7eSjoerg     typeCode = '\0';
98006f32e7eSjoerg   }
98106f32e7eSjoerg 
98206f32e7eSjoerg   std::string S;
98306f32e7eSjoerg   if (typeCode != '\0')
98406f32e7eSjoerg     S.push_back(typeCode);
98506f32e7eSjoerg   if (printNumber)
98606f32e7eSjoerg     S += utostr(T.getElementSizeInBits());
98706f32e7eSjoerg 
98806f32e7eSjoerg   return S;
98906f32e7eSjoerg }
99006f32e7eSjoerg 
getBuiltinTypeStr()99106f32e7eSjoerg std::string Intrinsic::getBuiltinTypeStr() {
99206f32e7eSjoerg   ClassKind LocalCK = getClassKind(true);
99306f32e7eSjoerg   std::string S;
99406f32e7eSjoerg 
99506f32e7eSjoerg   Type RetT = getReturnType();
99606f32e7eSjoerg   if ((LocalCK == ClassI || LocalCK == ClassW) && RetT.isScalar() &&
997*13fbcb42Sjoerg       !RetT.isFloating() && !RetT.isBFloat16())
99806f32e7eSjoerg     RetT.makeInteger(RetT.getElementSizeInBits(), false);
99906f32e7eSjoerg 
100006f32e7eSjoerg   // Since the return value must be one type, return a vector type of the
100106f32e7eSjoerg   // appropriate width which we will bitcast.  An exception is made for
100206f32e7eSjoerg   // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
100306f32e7eSjoerg   // fashion, storing them to a pointer arg.
100406f32e7eSjoerg   if (RetT.getNumVectors() > 1) {
100506f32e7eSjoerg     S += "vv*"; // void result with void* first argument
100606f32e7eSjoerg   } else {
100706f32e7eSjoerg     if (RetT.isPoly())
100806f32e7eSjoerg       RetT.makeInteger(RetT.getElementSizeInBits(), false);
1009*13fbcb42Sjoerg     if (!RetT.isScalar() && RetT.isInteger() && !RetT.isSigned())
101006f32e7eSjoerg       RetT.makeSigned();
101106f32e7eSjoerg 
1012*13fbcb42Sjoerg     if (LocalCK == ClassB && RetT.isValue() && !RetT.isScalar())
101306f32e7eSjoerg       // Cast to vector of 8-bit elements.
101406f32e7eSjoerg       RetT.makeInteger(8, true);
101506f32e7eSjoerg 
101606f32e7eSjoerg     S += RetT.builtin_str();
101706f32e7eSjoerg   }
101806f32e7eSjoerg 
101906f32e7eSjoerg   for (unsigned I = 0; I < getNumParams(); ++I) {
102006f32e7eSjoerg     Type T = getParamType(I);
102106f32e7eSjoerg     if (T.isPoly())
102206f32e7eSjoerg       T.makeInteger(T.getElementSizeInBits(), false);
102306f32e7eSjoerg 
1024*13fbcb42Sjoerg     if (LocalCK == ClassB && !T.isScalar())
102506f32e7eSjoerg       T.makeInteger(8, true);
102606f32e7eSjoerg     // Halves always get converted to 8-bit elements.
102706f32e7eSjoerg     if (T.isHalf() && T.isVector() && !T.isScalarForMangling())
102806f32e7eSjoerg       T.makeInteger(8, true);
102906f32e7eSjoerg 
1030*13fbcb42Sjoerg     if (LocalCK == ClassI && T.isInteger())
103106f32e7eSjoerg       T.makeSigned();
103206f32e7eSjoerg 
103306f32e7eSjoerg     if (hasImmediate() && getImmediateIdx() == I)
103406f32e7eSjoerg       T.makeImmediate(32);
103506f32e7eSjoerg 
103606f32e7eSjoerg     S += T.builtin_str();
103706f32e7eSjoerg   }
103806f32e7eSjoerg 
103906f32e7eSjoerg   // Extra constant integer to hold type class enum for this function, e.g. s8
104006f32e7eSjoerg   if (LocalCK == ClassB)
104106f32e7eSjoerg     S += "i";
104206f32e7eSjoerg 
104306f32e7eSjoerg   return S;
104406f32e7eSjoerg }
104506f32e7eSjoerg 
getMangledName(bool ForceClassS) const104606f32e7eSjoerg std::string Intrinsic::getMangledName(bool ForceClassS) const {
104706f32e7eSjoerg   // Check if the prototype has a scalar operand with the type of the vector
104806f32e7eSjoerg   // elements.  If not, bitcasting the args will take care of arg checking.
104906f32e7eSjoerg   // The actual signedness etc. will be taken care of with special enums.
105006f32e7eSjoerg   ClassKind LocalCK = CK;
105106f32e7eSjoerg   if (!protoHasScalar())
105206f32e7eSjoerg     LocalCK = ClassB;
105306f32e7eSjoerg 
105406f32e7eSjoerg   return mangleName(Name, ForceClassS ? ClassS : LocalCK);
105506f32e7eSjoerg }
105606f32e7eSjoerg 
mangleName(std::string Name,ClassKind LocalCK) const105706f32e7eSjoerg std::string Intrinsic::mangleName(std::string Name, ClassKind LocalCK) const {
105806f32e7eSjoerg   std::string typeCode = getInstTypeCode(BaseType, LocalCK);
105906f32e7eSjoerg   std::string S = Name;
106006f32e7eSjoerg 
106106f32e7eSjoerg   if (Name == "vcvt_f16_f32" || Name == "vcvt_f32_f16" ||
1062*13fbcb42Sjoerg       Name == "vcvt_f32_f64" || Name == "vcvt_f64_f32" ||
1063*13fbcb42Sjoerg       Name == "vcvt_f32_bf16")
106406f32e7eSjoerg     return Name;
106506f32e7eSjoerg 
106606f32e7eSjoerg   if (!typeCode.empty()) {
106706f32e7eSjoerg     // If the name ends with _xN (N = 2,3,4), insert the typeCode before _xN.
106806f32e7eSjoerg     if (Name.size() >= 3 && isdigit(Name.back()) &&
106906f32e7eSjoerg         Name[Name.length() - 2] == 'x' && Name[Name.length() - 3] == '_')
107006f32e7eSjoerg       S.insert(S.length() - 3, "_" + typeCode);
107106f32e7eSjoerg     else
107206f32e7eSjoerg       S += "_" + typeCode;
107306f32e7eSjoerg   }
107406f32e7eSjoerg 
107506f32e7eSjoerg   if (BaseType != InBaseType) {
107606f32e7eSjoerg     // A reinterpret - out the input base type at the end.
107706f32e7eSjoerg     S += "_" + getInstTypeCode(InBaseType, LocalCK);
107806f32e7eSjoerg   }
107906f32e7eSjoerg 
108006f32e7eSjoerg   if (LocalCK == ClassB)
108106f32e7eSjoerg     S += "_v";
108206f32e7eSjoerg 
108306f32e7eSjoerg   // Insert a 'q' before the first '_' character so that it ends up before
108406f32e7eSjoerg   // _lane or _n on vector-scalar operations.
108506f32e7eSjoerg   if (BaseType.getSizeInBits() == 128 && !BaseType.noManglingQ()) {
108606f32e7eSjoerg     size_t Pos = S.find('_');
108706f32e7eSjoerg     S.insert(Pos, "q");
108806f32e7eSjoerg   }
108906f32e7eSjoerg 
109006f32e7eSjoerg   char Suffix = '\0';
109106f32e7eSjoerg   if (BaseType.isScalarForMangling()) {
109206f32e7eSjoerg     switch (BaseType.getElementSizeInBits()) {
109306f32e7eSjoerg     case 8: Suffix = 'b'; break;
109406f32e7eSjoerg     case 16: Suffix = 'h'; break;
109506f32e7eSjoerg     case 32: Suffix = 's'; break;
109606f32e7eSjoerg     case 64: Suffix = 'd'; break;
109706f32e7eSjoerg     default: llvm_unreachable("Bad suffix!");
109806f32e7eSjoerg     }
109906f32e7eSjoerg   }
110006f32e7eSjoerg   if (Suffix != '\0') {
110106f32e7eSjoerg     size_t Pos = S.find('_');
110206f32e7eSjoerg     S.insert(Pos, &Suffix, 1);
110306f32e7eSjoerg   }
110406f32e7eSjoerg 
110506f32e7eSjoerg   return S;
110606f32e7eSjoerg }
110706f32e7eSjoerg 
replaceParamsIn(std::string S)110806f32e7eSjoerg std::string Intrinsic::replaceParamsIn(std::string S) {
110906f32e7eSjoerg   while (S.find('$') != std::string::npos) {
111006f32e7eSjoerg     size_t Pos = S.find('$');
111106f32e7eSjoerg     size_t End = Pos + 1;
111206f32e7eSjoerg     while (isalpha(S[End]))
111306f32e7eSjoerg       ++End;
111406f32e7eSjoerg 
111506f32e7eSjoerg     std::string VarName = S.substr(Pos + 1, End - Pos - 1);
111606f32e7eSjoerg     assert_with_loc(Variables.find(VarName) != Variables.end(),
111706f32e7eSjoerg                     "Variable not defined!");
111806f32e7eSjoerg     S.replace(Pos, End - Pos, Variables.find(VarName)->second.getName());
111906f32e7eSjoerg   }
112006f32e7eSjoerg 
112106f32e7eSjoerg   return S;
112206f32e7eSjoerg }
112306f32e7eSjoerg 
initVariables()112406f32e7eSjoerg void Intrinsic::initVariables() {
112506f32e7eSjoerg   Variables.clear();
112606f32e7eSjoerg 
112706f32e7eSjoerg   // Modify the TypeSpec per-argument to get a concrete Type, and create
112806f32e7eSjoerg   // known variables for each.
1129*13fbcb42Sjoerg   for (unsigned I = 1; I < Types.size(); ++I) {
113006f32e7eSjoerg     char NameC = '0' + (I - 1);
113106f32e7eSjoerg     std::string Name = "p";
113206f32e7eSjoerg     Name.push_back(NameC);
113306f32e7eSjoerg 
113406f32e7eSjoerg     Variables[Name] = Variable(Types[I], Name + VariablePostfix);
113506f32e7eSjoerg   }
113606f32e7eSjoerg   RetVar = Variable(Types[0], "ret" + VariablePostfix);
113706f32e7eSjoerg }
113806f32e7eSjoerg 
emitPrototype(StringRef NamePrefix)113906f32e7eSjoerg void Intrinsic::emitPrototype(StringRef NamePrefix) {
114006f32e7eSjoerg   if (UseMacro)
114106f32e7eSjoerg     OS << "#define ";
114206f32e7eSjoerg   else
114306f32e7eSjoerg     OS << "__ai " << Types[0].str() << " ";
114406f32e7eSjoerg 
114506f32e7eSjoerg   OS << NamePrefix.str() << mangleName(Name, ClassS) << "(";
114606f32e7eSjoerg 
114706f32e7eSjoerg   for (unsigned I = 0; I < getNumParams(); ++I) {
114806f32e7eSjoerg     if (I != 0)
114906f32e7eSjoerg       OS << ", ";
115006f32e7eSjoerg 
115106f32e7eSjoerg     char NameC = '0' + I;
115206f32e7eSjoerg     std::string Name = "p";
115306f32e7eSjoerg     Name.push_back(NameC);
115406f32e7eSjoerg     assert(Variables.find(Name) != Variables.end());
115506f32e7eSjoerg     Variable &V = Variables[Name];
115606f32e7eSjoerg 
115706f32e7eSjoerg     if (!UseMacro)
115806f32e7eSjoerg       OS << V.getType().str() << " ";
115906f32e7eSjoerg     OS << V.getName();
116006f32e7eSjoerg   }
116106f32e7eSjoerg 
116206f32e7eSjoerg   OS << ")";
116306f32e7eSjoerg }
116406f32e7eSjoerg 
emitOpeningBrace()116506f32e7eSjoerg void Intrinsic::emitOpeningBrace() {
116606f32e7eSjoerg   if (UseMacro)
116706f32e7eSjoerg     OS << " __extension__ ({";
116806f32e7eSjoerg   else
116906f32e7eSjoerg     OS << " {";
117006f32e7eSjoerg   emitNewLine();
117106f32e7eSjoerg }
117206f32e7eSjoerg 
emitClosingBrace()117306f32e7eSjoerg void Intrinsic::emitClosingBrace() {
117406f32e7eSjoerg   if (UseMacro)
117506f32e7eSjoerg     OS << "})";
117606f32e7eSjoerg   else
117706f32e7eSjoerg     OS << "}";
117806f32e7eSjoerg }
117906f32e7eSjoerg 
emitNewLine()118006f32e7eSjoerg void Intrinsic::emitNewLine() {
118106f32e7eSjoerg   if (UseMacro)
118206f32e7eSjoerg     OS << " \\\n";
118306f32e7eSjoerg   else
118406f32e7eSjoerg     OS << "\n";
118506f32e7eSjoerg }
118606f32e7eSjoerg 
emitReverseVariable(Variable & Dest,Variable & Src)118706f32e7eSjoerg void Intrinsic::emitReverseVariable(Variable &Dest, Variable &Src) {
118806f32e7eSjoerg   if (Dest.getType().getNumVectors() > 1) {
118906f32e7eSjoerg     emitNewLine();
119006f32e7eSjoerg 
119106f32e7eSjoerg     for (unsigned K = 0; K < Dest.getType().getNumVectors(); ++K) {
119206f32e7eSjoerg       OS << "  " << Dest.getName() << ".val[" << K << "] = "
119306f32e7eSjoerg          << "__builtin_shufflevector("
119406f32e7eSjoerg          << Src.getName() << ".val[" << K << "], "
119506f32e7eSjoerg          << Src.getName() << ".val[" << K << "]";
119606f32e7eSjoerg       for (int J = Dest.getType().getNumElements() - 1; J >= 0; --J)
119706f32e7eSjoerg         OS << ", " << J;
119806f32e7eSjoerg       OS << ");";
119906f32e7eSjoerg       emitNewLine();
120006f32e7eSjoerg     }
120106f32e7eSjoerg   } else {
120206f32e7eSjoerg     OS << "  " << Dest.getName()
120306f32e7eSjoerg        << " = __builtin_shufflevector(" << Src.getName() << ", " << Src.getName();
120406f32e7eSjoerg     for (int J = Dest.getType().getNumElements() - 1; J >= 0; --J)
120506f32e7eSjoerg       OS << ", " << J;
120606f32e7eSjoerg     OS << ");";
120706f32e7eSjoerg     emitNewLine();
120806f32e7eSjoerg   }
120906f32e7eSjoerg }
121006f32e7eSjoerg 
emitArgumentReversal()121106f32e7eSjoerg void Intrinsic::emitArgumentReversal() {
121206f32e7eSjoerg   if (isBigEndianSafe())
121306f32e7eSjoerg     return;
121406f32e7eSjoerg 
121506f32e7eSjoerg   // Reverse all vector arguments.
121606f32e7eSjoerg   for (unsigned I = 0; I < getNumParams(); ++I) {
121706f32e7eSjoerg     std::string Name = "p" + utostr(I);
121806f32e7eSjoerg     std::string NewName = "rev" + utostr(I);
121906f32e7eSjoerg 
122006f32e7eSjoerg     Variable &V = Variables[Name];
122106f32e7eSjoerg     Variable NewV(V.getType(), NewName + VariablePostfix);
122206f32e7eSjoerg 
122306f32e7eSjoerg     if (!NewV.getType().isVector() || NewV.getType().getNumElements() == 1)
122406f32e7eSjoerg       continue;
122506f32e7eSjoerg 
122606f32e7eSjoerg     OS << "  " << NewV.getType().str() << " " << NewV.getName() << ";";
122706f32e7eSjoerg     emitReverseVariable(NewV, V);
122806f32e7eSjoerg     V = NewV;
122906f32e7eSjoerg   }
123006f32e7eSjoerg }
123106f32e7eSjoerg 
emitReturnReversal()123206f32e7eSjoerg void Intrinsic::emitReturnReversal() {
123306f32e7eSjoerg   if (isBigEndianSafe())
123406f32e7eSjoerg     return;
123506f32e7eSjoerg   if (!getReturnType().isVector() || getReturnType().isVoid() ||
123606f32e7eSjoerg       getReturnType().getNumElements() == 1)
123706f32e7eSjoerg     return;
123806f32e7eSjoerg   emitReverseVariable(RetVar, RetVar);
123906f32e7eSjoerg }
124006f32e7eSjoerg 
emitShadowedArgs()124106f32e7eSjoerg void Intrinsic::emitShadowedArgs() {
124206f32e7eSjoerg   // Macro arguments are not type-checked like inline function arguments,
124306f32e7eSjoerg   // so assign them to local temporaries to get the right type checking.
124406f32e7eSjoerg   if (!UseMacro)
124506f32e7eSjoerg     return;
124606f32e7eSjoerg 
124706f32e7eSjoerg   for (unsigned I = 0; I < getNumParams(); ++I) {
124806f32e7eSjoerg     // Do not create a temporary for an immediate argument.
124906f32e7eSjoerg     // That would defeat the whole point of using a macro!
1250*13fbcb42Sjoerg     if (getParamType(I).isImmediate())
125106f32e7eSjoerg       continue;
125206f32e7eSjoerg     // Do not create a temporary for pointer arguments. The input
125306f32e7eSjoerg     // pointer may have an alignment hint.
125406f32e7eSjoerg     if (getParamType(I).isPointer())
125506f32e7eSjoerg       continue;
125606f32e7eSjoerg 
125706f32e7eSjoerg     std::string Name = "p" + utostr(I);
125806f32e7eSjoerg 
125906f32e7eSjoerg     assert(Variables.find(Name) != Variables.end());
126006f32e7eSjoerg     Variable &V = Variables[Name];
126106f32e7eSjoerg 
126206f32e7eSjoerg     std::string NewName = "s" + utostr(I);
126306f32e7eSjoerg     Variable V2(V.getType(), NewName + VariablePostfix);
126406f32e7eSjoerg 
126506f32e7eSjoerg     OS << "  " << V2.getType().str() << " " << V2.getName() << " = "
126606f32e7eSjoerg        << V.getName() << ";";
126706f32e7eSjoerg     emitNewLine();
126806f32e7eSjoerg 
126906f32e7eSjoerg     V = V2;
127006f32e7eSjoerg   }
127106f32e7eSjoerg }
127206f32e7eSjoerg 
protoHasScalar() const127306f32e7eSjoerg bool Intrinsic::protoHasScalar() const {
1274*13fbcb42Sjoerg   return std::any_of(Types.begin(), Types.end(), [](const Type &T) {
1275*13fbcb42Sjoerg     return T.isScalar() && !T.isImmediate();
1276*13fbcb42Sjoerg   });
127706f32e7eSjoerg }
127806f32e7eSjoerg 
emitBodyAsBuiltinCall()127906f32e7eSjoerg void Intrinsic::emitBodyAsBuiltinCall() {
128006f32e7eSjoerg   std::string S;
128106f32e7eSjoerg 
128206f32e7eSjoerg   // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
128306f32e7eSjoerg   // sret-like argument.
128406f32e7eSjoerg   bool SRet = getReturnType().getNumVectors() >= 2;
128506f32e7eSjoerg 
128606f32e7eSjoerg   StringRef N = Name;
128706f32e7eSjoerg   ClassKind LocalCK = CK;
128806f32e7eSjoerg   if (!protoHasScalar())
128906f32e7eSjoerg     LocalCK = ClassB;
129006f32e7eSjoerg 
129106f32e7eSjoerg   if (!getReturnType().isVoid() && !SRet)
129206f32e7eSjoerg     S += "(" + RetVar.getType().str() + ") ";
129306f32e7eSjoerg 
1294*13fbcb42Sjoerg   S += "__builtin_neon_" + mangleName(std::string(N), LocalCK) + "(";
129506f32e7eSjoerg 
129606f32e7eSjoerg   if (SRet)
129706f32e7eSjoerg     S += "&" + RetVar.getName() + ", ";
129806f32e7eSjoerg 
129906f32e7eSjoerg   for (unsigned I = 0; I < getNumParams(); ++I) {
130006f32e7eSjoerg     Variable &V = Variables["p" + utostr(I)];
130106f32e7eSjoerg     Type T = V.getType();
130206f32e7eSjoerg 
130306f32e7eSjoerg     // Handle multiple-vector values specially, emitting each subvector as an
130406f32e7eSjoerg     // argument to the builtin.
130506f32e7eSjoerg     if (T.getNumVectors() > 1) {
130606f32e7eSjoerg       // Check if an explicit cast is needed.
130706f32e7eSjoerg       std::string Cast;
130806f32e7eSjoerg       if (LocalCK == ClassB) {
130906f32e7eSjoerg         Type T2 = T;
131006f32e7eSjoerg         T2.makeOneVector();
131106f32e7eSjoerg         T2.makeInteger(8, /*Signed=*/true);
131206f32e7eSjoerg         Cast = "(" + T2.str() + ")";
131306f32e7eSjoerg       }
131406f32e7eSjoerg 
131506f32e7eSjoerg       for (unsigned J = 0; J < T.getNumVectors(); ++J)
131606f32e7eSjoerg         S += Cast + V.getName() + ".val[" + utostr(J) + "], ";
131706f32e7eSjoerg       continue;
131806f32e7eSjoerg     }
131906f32e7eSjoerg 
1320*13fbcb42Sjoerg     std::string Arg = V.getName();
132106f32e7eSjoerg     Type CastToType = T;
132206f32e7eSjoerg 
132306f32e7eSjoerg     // Check if an explicit cast is needed.
132406f32e7eSjoerg     if (CastToType.isVector() &&
132506f32e7eSjoerg         (LocalCK == ClassB || (T.isHalf() && !T.isScalarForMangling()))) {
132606f32e7eSjoerg       CastToType.makeInteger(8, true);
132706f32e7eSjoerg       Arg = "(" + CastToType.str() + ")" + Arg;
132806f32e7eSjoerg     } else if (CastToType.isVector() && LocalCK == ClassI) {
1329*13fbcb42Sjoerg       if (CastToType.isInteger())
133006f32e7eSjoerg         CastToType.makeSigned();
133106f32e7eSjoerg       Arg = "(" + CastToType.str() + ")" + Arg;
133206f32e7eSjoerg     }
133306f32e7eSjoerg 
133406f32e7eSjoerg     S += Arg + ", ";
133506f32e7eSjoerg   }
133606f32e7eSjoerg 
133706f32e7eSjoerg   // Extra constant integer to hold type class enum for this function, e.g. s8
133806f32e7eSjoerg   if (getClassKind(true) == ClassB) {
1339*13fbcb42Sjoerg     S += utostr(getPolymorphicKeyType().getNeonEnum());
134006f32e7eSjoerg   } else {
134106f32e7eSjoerg     // Remove extraneous ", ".
134206f32e7eSjoerg     S.pop_back();
134306f32e7eSjoerg     S.pop_back();
134406f32e7eSjoerg   }
134506f32e7eSjoerg   S += ");";
134606f32e7eSjoerg 
134706f32e7eSjoerg   std::string RetExpr;
134806f32e7eSjoerg   if (!SRet && !RetVar.getType().isVoid())
134906f32e7eSjoerg     RetExpr = RetVar.getName() + " = ";
135006f32e7eSjoerg 
135106f32e7eSjoerg   OS << "  " << RetExpr << S;
135206f32e7eSjoerg   emitNewLine();
135306f32e7eSjoerg }
135406f32e7eSjoerg 
emitBody(StringRef CallPrefix)135506f32e7eSjoerg void Intrinsic::emitBody(StringRef CallPrefix) {
135606f32e7eSjoerg   std::vector<std::string> Lines;
135706f32e7eSjoerg 
135806f32e7eSjoerg   assert(RetVar.getType() == Types[0]);
135906f32e7eSjoerg   // Create a return variable, if we're not void.
136006f32e7eSjoerg   if (!RetVar.getType().isVoid()) {
136106f32e7eSjoerg     OS << "  " << RetVar.getType().str() << " " << RetVar.getName() << ";";
136206f32e7eSjoerg     emitNewLine();
136306f32e7eSjoerg   }
136406f32e7eSjoerg 
136506f32e7eSjoerg   if (!Body || Body->getValues().empty()) {
136606f32e7eSjoerg     // Nothing specific to output - must output a builtin.
136706f32e7eSjoerg     emitBodyAsBuiltinCall();
136806f32e7eSjoerg     return;
136906f32e7eSjoerg   }
137006f32e7eSjoerg 
137106f32e7eSjoerg   // We have a list of "things to output". The last should be returned.
137206f32e7eSjoerg   for (auto *I : Body->getValues()) {
137306f32e7eSjoerg     if (StringInit *SI = dyn_cast<StringInit>(I)) {
137406f32e7eSjoerg       Lines.push_back(replaceParamsIn(SI->getAsString()));
137506f32e7eSjoerg     } else if (DagInit *DI = dyn_cast<DagInit>(I)) {
137606f32e7eSjoerg       DagEmitter DE(*this, CallPrefix);
137706f32e7eSjoerg       Lines.push_back(DE.emitDag(DI).second + ";");
137806f32e7eSjoerg     }
137906f32e7eSjoerg   }
138006f32e7eSjoerg 
138106f32e7eSjoerg   assert(!Lines.empty() && "Empty def?");
138206f32e7eSjoerg   if (!RetVar.getType().isVoid())
138306f32e7eSjoerg     Lines.back().insert(0, RetVar.getName() + " = ");
138406f32e7eSjoerg 
138506f32e7eSjoerg   for (auto &L : Lines) {
138606f32e7eSjoerg     OS << "  " << L;
138706f32e7eSjoerg     emitNewLine();
138806f32e7eSjoerg   }
138906f32e7eSjoerg }
139006f32e7eSjoerg 
emitReturn()139106f32e7eSjoerg void Intrinsic::emitReturn() {
139206f32e7eSjoerg   if (RetVar.getType().isVoid())
139306f32e7eSjoerg     return;
139406f32e7eSjoerg   if (UseMacro)
139506f32e7eSjoerg     OS << "  " << RetVar.getName() << ";";
139606f32e7eSjoerg   else
139706f32e7eSjoerg     OS << "  return " << RetVar.getName() << ";";
139806f32e7eSjoerg   emitNewLine();
139906f32e7eSjoerg }
140006f32e7eSjoerg 
emitDag(DagInit * DI)140106f32e7eSjoerg std::pair<Type, std::string> Intrinsic::DagEmitter::emitDag(DagInit *DI) {
140206f32e7eSjoerg   // At this point we should only be seeing a def.
140306f32e7eSjoerg   DefInit *DefI = cast<DefInit>(DI->getOperator());
140406f32e7eSjoerg   std::string Op = DefI->getAsString();
140506f32e7eSjoerg 
140606f32e7eSjoerg   if (Op == "cast" || Op == "bitcast")
140706f32e7eSjoerg     return emitDagCast(DI, Op == "bitcast");
140806f32e7eSjoerg   if (Op == "shuffle")
140906f32e7eSjoerg     return emitDagShuffle(DI);
141006f32e7eSjoerg   if (Op == "dup")
141106f32e7eSjoerg     return emitDagDup(DI);
141206f32e7eSjoerg   if (Op == "dup_typed")
141306f32e7eSjoerg     return emitDagDupTyped(DI);
141406f32e7eSjoerg   if (Op == "splat")
141506f32e7eSjoerg     return emitDagSplat(DI);
141606f32e7eSjoerg   if (Op == "save_temp")
141706f32e7eSjoerg     return emitDagSaveTemp(DI);
141806f32e7eSjoerg   if (Op == "op")
141906f32e7eSjoerg     return emitDagOp(DI);
1420*13fbcb42Sjoerg   if (Op == "call" || Op == "call_mangled")
1421*13fbcb42Sjoerg     return emitDagCall(DI, Op == "call_mangled");
142206f32e7eSjoerg   if (Op == "name_replace")
142306f32e7eSjoerg     return emitDagNameReplace(DI);
142406f32e7eSjoerg   if (Op == "literal")
142506f32e7eSjoerg     return emitDagLiteral(DI);
142606f32e7eSjoerg   assert_with_loc(false, "Unknown operation!");
142706f32e7eSjoerg   return std::make_pair(Type::getVoid(), "");
142806f32e7eSjoerg }
142906f32e7eSjoerg 
emitDagOp(DagInit * DI)143006f32e7eSjoerg std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagOp(DagInit *DI) {
143106f32e7eSjoerg   std::string Op = cast<StringInit>(DI->getArg(0))->getAsUnquotedString();
143206f32e7eSjoerg   if (DI->getNumArgs() == 2) {
143306f32e7eSjoerg     // Unary op.
143406f32e7eSjoerg     std::pair<Type, std::string> R =
1435*13fbcb42Sjoerg         emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1)));
143606f32e7eSjoerg     return std::make_pair(R.first, Op + R.second);
143706f32e7eSjoerg   } else {
143806f32e7eSjoerg     assert(DI->getNumArgs() == 3 && "Can only handle unary and binary ops!");
143906f32e7eSjoerg     std::pair<Type, std::string> R1 =
1440*13fbcb42Sjoerg         emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1)));
144106f32e7eSjoerg     std::pair<Type, std::string> R2 =
1442*13fbcb42Sjoerg         emitDagArg(DI->getArg(2), std::string(DI->getArgNameStr(2)));
144306f32e7eSjoerg     assert_with_loc(R1.first == R2.first, "Argument type mismatch!");
144406f32e7eSjoerg     return std::make_pair(R1.first, R1.second + " " + Op + " " + R2.second);
144506f32e7eSjoerg   }
144606f32e7eSjoerg }
144706f32e7eSjoerg 
1448*13fbcb42Sjoerg std::pair<Type, std::string>
emitDagCall(DagInit * DI,bool MatchMangledName)1449*13fbcb42Sjoerg Intrinsic::DagEmitter::emitDagCall(DagInit *DI, bool MatchMangledName) {
145006f32e7eSjoerg   std::vector<Type> Types;
145106f32e7eSjoerg   std::vector<std::string> Values;
145206f32e7eSjoerg   for (unsigned I = 0; I < DI->getNumArgs() - 1; ++I) {
145306f32e7eSjoerg     std::pair<Type, std::string> R =
1454*13fbcb42Sjoerg         emitDagArg(DI->getArg(I + 1), std::string(DI->getArgNameStr(I + 1)));
145506f32e7eSjoerg     Types.push_back(R.first);
145606f32e7eSjoerg     Values.push_back(R.second);
145706f32e7eSjoerg   }
145806f32e7eSjoerg 
145906f32e7eSjoerg   // Look up the called intrinsic.
146006f32e7eSjoerg   std::string N;
146106f32e7eSjoerg   if (StringInit *SI = dyn_cast<StringInit>(DI->getArg(0)))
146206f32e7eSjoerg     N = SI->getAsUnquotedString();
146306f32e7eSjoerg   else
146406f32e7eSjoerg     N = emitDagArg(DI->getArg(0), "").second;
1465*13fbcb42Sjoerg   Optional<std::string> MangledName;
1466*13fbcb42Sjoerg   if (MatchMangledName) {
1467*13fbcb42Sjoerg     if (Intr.getRecord()->getValueAsBit("isLaneQ"))
1468*13fbcb42Sjoerg       N += "q";
1469*13fbcb42Sjoerg     MangledName = Intr.mangleName(N, ClassS);
1470*13fbcb42Sjoerg   }
1471*13fbcb42Sjoerg   Intrinsic &Callee = Intr.Emitter.getIntrinsic(N, Types, MangledName);
147206f32e7eSjoerg 
147306f32e7eSjoerg   // Make sure the callee is known as an early def.
147406f32e7eSjoerg   Callee.setNeededEarly();
147506f32e7eSjoerg   Intr.Dependencies.insert(&Callee);
147606f32e7eSjoerg 
147706f32e7eSjoerg   // Now create the call itself.
147806f32e7eSjoerg   std::string S = "";
147906f32e7eSjoerg   if (!Callee.isBigEndianSafe())
148006f32e7eSjoerg     S += CallPrefix.str();
148106f32e7eSjoerg   S += Callee.getMangledName(true) + "(";
148206f32e7eSjoerg   for (unsigned I = 0; I < DI->getNumArgs() - 1; ++I) {
148306f32e7eSjoerg     if (I != 0)
148406f32e7eSjoerg       S += ", ";
148506f32e7eSjoerg     S += Values[I];
148606f32e7eSjoerg   }
148706f32e7eSjoerg   S += ")";
148806f32e7eSjoerg 
148906f32e7eSjoerg   return std::make_pair(Callee.getReturnType(), S);
149006f32e7eSjoerg }
149106f32e7eSjoerg 
emitDagCast(DagInit * DI,bool IsBitCast)149206f32e7eSjoerg std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagCast(DagInit *DI,
149306f32e7eSjoerg                                                                 bool IsBitCast){
149406f32e7eSjoerg   // (cast MOD* VAL) -> cast VAL to type given by MOD.
1495*13fbcb42Sjoerg   std::pair<Type, std::string> R =
1496*13fbcb42Sjoerg       emitDagArg(DI->getArg(DI->getNumArgs() - 1),
1497*13fbcb42Sjoerg                  std::string(DI->getArgNameStr(DI->getNumArgs() - 1)));
149806f32e7eSjoerg   Type castToType = R.first;
149906f32e7eSjoerg   for (unsigned ArgIdx = 0; ArgIdx < DI->getNumArgs() - 1; ++ArgIdx) {
150006f32e7eSjoerg 
150106f32e7eSjoerg     // MOD can take several forms:
150206f32e7eSjoerg     //   1. $X - take the type of parameter / variable X.
150306f32e7eSjoerg     //   2. The value "R" - take the type of the return type.
150406f32e7eSjoerg     //   3. a type string
150506f32e7eSjoerg     //   4. The value "U" or "S" to switch the signedness.
150606f32e7eSjoerg     //   5. The value "H" or "D" to half or double the bitwidth.
150706f32e7eSjoerg     //   6. The value "8" to convert to 8-bit (signed) integer lanes.
150806f32e7eSjoerg     if (!DI->getArgNameStr(ArgIdx).empty()) {
1509*13fbcb42Sjoerg       assert_with_loc(Intr.Variables.find(std::string(
1510*13fbcb42Sjoerg                           DI->getArgNameStr(ArgIdx))) != Intr.Variables.end(),
151106f32e7eSjoerg                       "Variable not found");
1512*13fbcb42Sjoerg       castToType =
1513*13fbcb42Sjoerg           Intr.Variables[std::string(DI->getArgNameStr(ArgIdx))].getType();
151406f32e7eSjoerg     } else {
151506f32e7eSjoerg       StringInit *SI = dyn_cast<StringInit>(DI->getArg(ArgIdx));
151606f32e7eSjoerg       assert_with_loc(SI, "Expected string type or $Name for cast type");
151706f32e7eSjoerg 
151806f32e7eSjoerg       if (SI->getAsUnquotedString() == "R") {
151906f32e7eSjoerg         castToType = Intr.getReturnType();
152006f32e7eSjoerg       } else if (SI->getAsUnquotedString() == "U") {
152106f32e7eSjoerg         castToType.makeUnsigned();
152206f32e7eSjoerg       } else if (SI->getAsUnquotedString() == "S") {
152306f32e7eSjoerg         castToType.makeSigned();
152406f32e7eSjoerg       } else if (SI->getAsUnquotedString() == "H") {
152506f32e7eSjoerg         castToType.halveLanes();
152606f32e7eSjoerg       } else if (SI->getAsUnquotedString() == "D") {
152706f32e7eSjoerg         castToType.doubleLanes();
152806f32e7eSjoerg       } else if (SI->getAsUnquotedString() == "8") {
152906f32e7eSjoerg         castToType.makeInteger(8, true);
1530*13fbcb42Sjoerg       } else if (SI->getAsUnquotedString() == "32") {
1531*13fbcb42Sjoerg         castToType.make32BitElement();
153206f32e7eSjoerg       } else {
153306f32e7eSjoerg         castToType = Type::fromTypedefName(SI->getAsUnquotedString());
153406f32e7eSjoerg         assert_with_loc(!castToType.isVoid(), "Unknown typedef");
153506f32e7eSjoerg       }
153606f32e7eSjoerg     }
153706f32e7eSjoerg   }
153806f32e7eSjoerg 
153906f32e7eSjoerg   std::string S;
154006f32e7eSjoerg   if (IsBitCast) {
154106f32e7eSjoerg     // Emit a reinterpret cast. The second operand must be an lvalue, so create
154206f32e7eSjoerg     // a temporary.
154306f32e7eSjoerg     std::string N = "reint";
154406f32e7eSjoerg     unsigned I = 0;
154506f32e7eSjoerg     while (Intr.Variables.find(N) != Intr.Variables.end())
154606f32e7eSjoerg       N = "reint" + utostr(++I);
154706f32e7eSjoerg     Intr.Variables[N] = Variable(R.first, N + Intr.VariablePostfix);
154806f32e7eSjoerg 
154906f32e7eSjoerg     Intr.OS << R.first.str() << " " << Intr.Variables[N].getName() << " = "
155006f32e7eSjoerg             << R.second << ";";
155106f32e7eSjoerg     Intr.emitNewLine();
155206f32e7eSjoerg 
155306f32e7eSjoerg     S = "*(" + castToType.str() + " *) &" + Intr.Variables[N].getName() + "";
155406f32e7eSjoerg   } else {
155506f32e7eSjoerg     // Emit a normal (static) cast.
155606f32e7eSjoerg     S = "(" + castToType.str() + ")(" + R.second + ")";
155706f32e7eSjoerg   }
155806f32e7eSjoerg 
155906f32e7eSjoerg   return std::make_pair(castToType, S);
156006f32e7eSjoerg }
156106f32e7eSjoerg 
emitDagShuffle(DagInit * DI)156206f32e7eSjoerg std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagShuffle(DagInit *DI){
156306f32e7eSjoerg   // See the documentation in arm_neon.td for a description of these operators.
156406f32e7eSjoerg   class LowHalf : public SetTheory::Operator {
156506f32e7eSjoerg   public:
156606f32e7eSjoerg     void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
156706f32e7eSjoerg                ArrayRef<SMLoc> Loc) override {
156806f32e7eSjoerg       SetTheory::RecSet Elts2;
156906f32e7eSjoerg       ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts2, Loc);
157006f32e7eSjoerg       Elts.insert(Elts2.begin(), Elts2.begin() + (Elts2.size() / 2));
157106f32e7eSjoerg     }
157206f32e7eSjoerg   };
157306f32e7eSjoerg 
157406f32e7eSjoerg   class HighHalf : public SetTheory::Operator {
157506f32e7eSjoerg   public:
157606f32e7eSjoerg     void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
157706f32e7eSjoerg                ArrayRef<SMLoc> Loc) override {
157806f32e7eSjoerg       SetTheory::RecSet Elts2;
157906f32e7eSjoerg       ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts2, Loc);
158006f32e7eSjoerg       Elts.insert(Elts2.begin() + (Elts2.size() / 2), Elts2.end());
158106f32e7eSjoerg     }
158206f32e7eSjoerg   };
158306f32e7eSjoerg 
158406f32e7eSjoerg   class Rev : public SetTheory::Operator {
158506f32e7eSjoerg     unsigned ElementSize;
158606f32e7eSjoerg 
158706f32e7eSjoerg   public:
158806f32e7eSjoerg     Rev(unsigned ElementSize) : ElementSize(ElementSize) {}
158906f32e7eSjoerg 
159006f32e7eSjoerg     void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
159106f32e7eSjoerg                ArrayRef<SMLoc> Loc) override {
159206f32e7eSjoerg       SetTheory::RecSet Elts2;
159306f32e7eSjoerg       ST.evaluate(Expr->arg_begin() + 1, Expr->arg_end(), Elts2, Loc);
159406f32e7eSjoerg 
159506f32e7eSjoerg       int64_t VectorSize = cast<IntInit>(Expr->getArg(0))->getValue();
159606f32e7eSjoerg       VectorSize /= ElementSize;
159706f32e7eSjoerg 
159806f32e7eSjoerg       std::vector<Record *> Revved;
159906f32e7eSjoerg       for (unsigned VI = 0; VI < Elts2.size(); VI += VectorSize) {
160006f32e7eSjoerg         for (int LI = VectorSize - 1; LI >= 0; --LI) {
160106f32e7eSjoerg           Revved.push_back(Elts2[VI + LI]);
160206f32e7eSjoerg         }
160306f32e7eSjoerg       }
160406f32e7eSjoerg 
160506f32e7eSjoerg       Elts.insert(Revved.begin(), Revved.end());
160606f32e7eSjoerg     }
160706f32e7eSjoerg   };
160806f32e7eSjoerg 
160906f32e7eSjoerg   class MaskExpander : public SetTheory::Expander {
161006f32e7eSjoerg     unsigned N;
161106f32e7eSjoerg 
161206f32e7eSjoerg   public:
161306f32e7eSjoerg     MaskExpander(unsigned N) : N(N) {}
161406f32e7eSjoerg 
161506f32e7eSjoerg     void expand(SetTheory &ST, Record *R, SetTheory::RecSet &Elts) override {
161606f32e7eSjoerg       unsigned Addend = 0;
161706f32e7eSjoerg       if (R->getName() == "mask0")
161806f32e7eSjoerg         Addend = 0;
161906f32e7eSjoerg       else if (R->getName() == "mask1")
162006f32e7eSjoerg         Addend = N;
162106f32e7eSjoerg       else
162206f32e7eSjoerg         return;
162306f32e7eSjoerg       for (unsigned I = 0; I < N; ++I)
162406f32e7eSjoerg         Elts.insert(R->getRecords().getDef("sv" + utostr(I + Addend)));
162506f32e7eSjoerg     }
162606f32e7eSjoerg   };
162706f32e7eSjoerg 
162806f32e7eSjoerg   // (shuffle arg1, arg2, sequence)
162906f32e7eSjoerg   std::pair<Type, std::string> Arg1 =
1630*13fbcb42Sjoerg       emitDagArg(DI->getArg(0), std::string(DI->getArgNameStr(0)));
163106f32e7eSjoerg   std::pair<Type, std::string> Arg2 =
1632*13fbcb42Sjoerg       emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1)));
163306f32e7eSjoerg   assert_with_loc(Arg1.first == Arg2.first,
163406f32e7eSjoerg                   "Different types in arguments to shuffle!");
163506f32e7eSjoerg 
163606f32e7eSjoerg   SetTheory ST;
163706f32e7eSjoerg   SetTheory::RecSet Elts;
163806f32e7eSjoerg   ST.addOperator("lowhalf", std::make_unique<LowHalf>());
163906f32e7eSjoerg   ST.addOperator("highhalf", std::make_unique<HighHalf>());
164006f32e7eSjoerg   ST.addOperator("rev",
164106f32e7eSjoerg                  std::make_unique<Rev>(Arg1.first.getElementSizeInBits()));
164206f32e7eSjoerg   ST.addExpander("MaskExpand",
164306f32e7eSjoerg                  std::make_unique<MaskExpander>(Arg1.first.getNumElements()));
164406f32e7eSjoerg   ST.evaluate(DI->getArg(2), Elts, None);
164506f32e7eSjoerg 
164606f32e7eSjoerg   std::string S = "__builtin_shufflevector(" + Arg1.second + ", " + Arg2.second;
164706f32e7eSjoerg   for (auto &E : Elts) {
164806f32e7eSjoerg     StringRef Name = E->getName();
164906f32e7eSjoerg     assert_with_loc(Name.startswith("sv"),
165006f32e7eSjoerg                     "Incorrect element kind in shuffle mask!");
165106f32e7eSjoerg     S += ", " + Name.drop_front(2).str();
165206f32e7eSjoerg   }
165306f32e7eSjoerg   S += ")";
165406f32e7eSjoerg 
165506f32e7eSjoerg   // Recalculate the return type - the shuffle may have halved or doubled it.
165606f32e7eSjoerg   Type T(Arg1.first);
165706f32e7eSjoerg   if (Elts.size() > T.getNumElements()) {
165806f32e7eSjoerg     assert_with_loc(
165906f32e7eSjoerg         Elts.size() == T.getNumElements() * 2,
166006f32e7eSjoerg         "Can only double or half the number of elements in a shuffle!");
166106f32e7eSjoerg     T.doubleLanes();
166206f32e7eSjoerg   } else if (Elts.size() < T.getNumElements()) {
166306f32e7eSjoerg     assert_with_loc(
166406f32e7eSjoerg         Elts.size() == T.getNumElements() / 2,
166506f32e7eSjoerg         "Can only double or half the number of elements in a shuffle!");
166606f32e7eSjoerg     T.halveLanes();
166706f32e7eSjoerg   }
166806f32e7eSjoerg 
166906f32e7eSjoerg   return std::make_pair(T, S);
167006f32e7eSjoerg }
167106f32e7eSjoerg 
emitDagDup(DagInit * DI)167206f32e7eSjoerg std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagDup(DagInit *DI) {
167306f32e7eSjoerg   assert_with_loc(DI->getNumArgs() == 1, "dup() expects one argument");
1674*13fbcb42Sjoerg   std::pair<Type, std::string> A =
1675*13fbcb42Sjoerg       emitDagArg(DI->getArg(0), std::string(DI->getArgNameStr(0)));
167606f32e7eSjoerg   assert_with_loc(A.first.isScalar(), "dup() expects a scalar argument");
167706f32e7eSjoerg 
167806f32e7eSjoerg   Type T = Intr.getBaseType();
167906f32e7eSjoerg   assert_with_loc(T.isVector(), "dup() used but default type is scalar!");
168006f32e7eSjoerg   std::string S = "(" + T.str() + ") {";
168106f32e7eSjoerg   for (unsigned I = 0; I < T.getNumElements(); ++I) {
168206f32e7eSjoerg     if (I != 0)
168306f32e7eSjoerg       S += ", ";
168406f32e7eSjoerg     S += A.second;
168506f32e7eSjoerg   }
168606f32e7eSjoerg   S += "}";
168706f32e7eSjoerg 
168806f32e7eSjoerg   return std::make_pair(T, S);
168906f32e7eSjoerg }
169006f32e7eSjoerg 
emitDagDupTyped(DagInit * DI)169106f32e7eSjoerg std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagDupTyped(DagInit *DI) {
169206f32e7eSjoerg   assert_with_loc(DI->getNumArgs() == 2, "dup_typed() expects two arguments");
1693*13fbcb42Sjoerg   std::pair<Type, std::string> B =
1694*13fbcb42Sjoerg       emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1)));
169506f32e7eSjoerg   assert_with_loc(B.first.isScalar(),
169606f32e7eSjoerg                   "dup_typed() requires a scalar as the second argument");
1697*13fbcb42Sjoerg   Type T;
1698*13fbcb42Sjoerg   // If the type argument is a constant string, construct the type directly.
1699*13fbcb42Sjoerg   if (StringInit *SI = dyn_cast<StringInit>(DI->getArg(0))) {
1700*13fbcb42Sjoerg     T = Type::fromTypedefName(SI->getAsUnquotedString());
1701*13fbcb42Sjoerg     assert_with_loc(!T.isVoid(), "Unknown typedef");
1702*13fbcb42Sjoerg   } else
1703*13fbcb42Sjoerg     T = emitDagArg(DI->getArg(0), std::string(DI->getArgNameStr(0))).first;
170406f32e7eSjoerg 
170506f32e7eSjoerg   assert_with_loc(T.isVector(), "dup_typed() used but target type is scalar!");
170606f32e7eSjoerg   std::string S = "(" + T.str() + ") {";
170706f32e7eSjoerg   for (unsigned I = 0; I < T.getNumElements(); ++I) {
170806f32e7eSjoerg     if (I != 0)
170906f32e7eSjoerg       S += ", ";
171006f32e7eSjoerg     S += B.second;
171106f32e7eSjoerg   }
171206f32e7eSjoerg   S += "}";
171306f32e7eSjoerg 
171406f32e7eSjoerg   return std::make_pair(T, S);
171506f32e7eSjoerg }
171606f32e7eSjoerg 
emitDagSplat(DagInit * DI)171706f32e7eSjoerg std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagSplat(DagInit *DI) {
171806f32e7eSjoerg   assert_with_loc(DI->getNumArgs() == 2, "splat() expects two arguments");
1719*13fbcb42Sjoerg   std::pair<Type, std::string> A =
1720*13fbcb42Sjoerg       emitDagArg(DI->getArg(0), std::string(DI->getArgNameStr(0)));
1721*13fbcb42Sjoerg   std::pair<Type, std::string> B =
1722*13fbcb42Sjoerg       emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1)));
172306f32e7eSjoerg 
172406f32e7eSjoerg   assert_with_loc(B.first.isScalar(),
172506f32e7eSjoerg                   "splat() requires a scalar int as the second argument");
172606f32e7eSjoerg 
172706f32e7eSjoerg   std::string S = "__builtin_shufflevector(" + A.second + ", " + A.second;
172806f32e7eSjoerg   for (unsigned I = 0; I < Intr.getBaseType().getNumElements(); ++I) {
172906f32e7eSjoerg     S += ", " + B.second;
173006f32e7eSjoerg   }
173106f32e7eSjoerg   S += ")";
173206f32e7eSjoerg 
173306f32e7eSjoerg   return std::make_pair(Intr.getBaseType(), S);
173406f32e7eSjoerg }
173506f32e7eSjoerg 
emitDagSaveTemp(DagInit * DI)173606f32e7eSjoerg std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagSaveTemp(DagInit *DI) {
173706f32e7eSjoerg   assert_with_loc(DI->getNumArgs() == 2, "save_temp() expects two arguments");
1738*13fbcb42Sjoerg   std::pair<Type, std::string> A =
1739*13fbcb42Sjoerg       emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1)));
174006f32e7eSjoerg 
174106f32e7eSjoerg   assert_with_loc(!A.first.isVoid(),
174206f32e7eSjoerg                   "Argument to save_temp() must have non-void type!");
174306f32e7eSjoerg 
1744*13fbcb42Sjoerg   std::string N = std::string(DI->getArgNameStr(0));
174506f32e7eSjoerg   assert_with_loc(!N.empty(),
174606f32e7eSjoerg                   "save_temp() expects a name as the first argument");
174706f32e7eSjoerg 
174806f32e7eSjoerg   assert_with_loc(Intr.Variables.find(N) == Intr.Variables.end(),
174906f32e7eSjoerg                   "Variable already defined!");
175006f32e7eSjoerg   Intr.Variables[N] = Variable(A.first, N + Intr.VariablePostfix);
175106f32e7eSjoerg 
175206f32e7eSjoerg   std::string S =
175306f32e7eSjoerg       A.first.str() + " " + Intr.Variables[N].getName() + " = " + A.second;
175406f32e7eSjoerg 
175506f32e7eSjoerg   return std::make_pair(Type::getVoid(), S);
175606f32e7eSjoerg }
175706f32e7eSjoerg 
175806f32e7eSjoerg std::pair<Type, std::string>
emitDagNameReplace(DagInit * DI)175906f32e7eSjoerg Intrinsic::DagEmitter::emitDagNameReplace(DagInit *DI) {
176006f32e7eSjoerg   std::string S = Intr.Name;
176106f32e7eSjoerg 
176206f32e7eSjoerg   assert_with_loc(DI->getNumArgs() == 2, "name_replace requires 2 arguments!");
176306f32e7eSjoerg   std::string ToReplace = cast<StringInit>(DI->getArg(0))->getAsUnquotedString();
176406f32e7eSjoerg   std::string ReplaceWith = cast<StringInit>(DI->getArg(1))->getAsUnquotedString();
176506f32e7eSjoerg 
176606f32e7eSjoerg   size_t Idx = S.find(ToReplace);
176706f32e7eSjoerg 
176806f32e7eSjoerg   assert_with_loc(Idx != std::string::npos, "name should contain '" + ToReplace + "'!");
176906f32e7eSjoerg   S.replace(Idx, ToReplace.size(), ReplaceWith);
177006f32e7eSjoerg 
177106f32e7eSjoerg   return std::make_pair(Type::getVoid(), S);
177206f32e7eSjoerg }
177306f32e7eSjoerg 
emitDagLiteral(DagInit * DI)177406f32e7eSjoerg std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagLiteral(DagInit *DI){
177506f32e7eSjoerg   std::string Ty = cast<StringInit>(DI->getArg(0))->getAsUnquotedString();
177606f32e7eSjoerg   std::string Value = cast<StringInit>(DI->getArg(1))->getAsUnquotedString();
177706f32e7eSjoerg   return std::make_pair(Type::fromTypedefName(Ty), Value);
177806f32e7eSjoerg }
177906f32e7eSjoerg 
178006f32e7eSjoerg std::pair<Type, std::string>
emitDagArg(Init * Arg,std::string ArgName)178106f32e7eSjoerg Intrinsic::DagEmitter::emitDagArg(Init *Arg, std::string ArgName) {
178206f32e7eSjoerg   if (!ArgName.empty()) {
178306f32e7eSjoerg     assert_with_loc(!Arg->isComplete(),
178406f32e7eSjoerg                     "Arguments must either be DAGs or names, not both!");
178506f32e7eSjoerg     assert_with_loc(Intr.Variables.find(ArgName) != Intr.Variables.end(),
178606f32e7eSjoerg                     "Variable not defined!");
178706f32e7eSjoerg     Variable &V = Intr.Variables[ArgName];
178806f32e7eSjoerg     return std::make_pair(V.getType(), V.getName());
178906f32e7eSjoerg   }
179006f32e7eSjoerg 
179106f32e7eSjoerg   assert(Arg && "Neither ArgName nor Arg?!");
179206f32e7eSjoerg   DagInit *DI = dyn_cast<DagInit>(Arg);
179306f32e7eSjoerg   assert_with_loc(DI, "Arguments must either be DAGs or names!");
179406f32e7eSjoerg 
179506f32e7eSjoerg   return emitDag(DI);
179606f32e7eSjoerg }
179706f32e7eSjoerg 
generate()179806f32e7eSjoerg std::string Intrinsic::generate() {
179906f32e7eSjoerg   // Avoid duplicated code for big and little endian
180006f32e7eSjoerg   if (isBigEndianSafe()) {
180106f32e7eSjoerg     generateImpl(false, "", "");
180206f32e7eSjoerg     return OS.str();
180306f32e7eSjoerg   }
180406f32e7eSjoerg   // Little endian intrinsics are simple and don't require any argument
180506f32e7eSjoerg   // swapping.
180606f32e7eSjoerg   OS << "#ifdef __LITTLE_ENDIAN__\n";
180706f32e7eSjoerg 
180806f32e7eSjoerg   generateImpl(false, "", "");
180906f32e7eSjoerg 
181006f32e7eSjoerg   OS << "#else\n";
181106f32e7eSjoerg 
181206f32e7eSjoerg   // Big endian intrinsics are more complex. The user intended these
181306f32e7eSjoerg   // intrinsics to operate on a vector "as-if" loaded by (V)LDR,
181406f32e7eSjoerg   // but we load as-if (V)LD1. So we should swap all arguments and
181506f32e7eSjoerg   // swap the return value too.
181606f32e7eSjoerg   //
181706f32e7eSjoerg   // If we call sub-intrinsics, we should call a version that does
181806f32e7eSjoerg   // not re-swap the arguments!
181906f32e7eSjoerg   generateImpl(true, "", "__noswap_");
182006f32e7eSjoerg 
182106f32e7eSjoerg   // If we're needed early, create a non-swapping variant for
182206f32e7eSjoerg   // big-endian.
182306f32e7eSjoerg   if (NeededEarly) {
182406f32e7eSjoerg     generateImpl(false, "__noswap_", "__noswap_");
182506f32e7eSjoerg   }
182606f32e7eSjoerg   OS << "#endif\n\n";
182706f32e7eSjoerg 
182806f32e7eSjoerg   return OS.str();
182906f32e7eSjoerg }
183006f32e7eSjoerg 
generateImpl(bool ReverseArguments,StringRef NamePrefix,StringRef CallPrefix)183106f32e7eSjoerg void Intrinsic::generateImpl(bool ReverseArguments,
183206f32e7eSjoerg                              StringRef NamePrefix, StringRef CallPrefix) {
183306f32e7eSjoerg   CurrentRecord = R;
183406f32e7eSjoerg 
183506f32e7eSjoerg   // If we call a macro, our local variables may be corrupted due to
183606f32e7eSjoerg   // lack of proper lexical scoping. So, add a globally unique postfix
183706f32e7eSjoerg   // to every variable.
183806f32e7eSjoerg   //
183906f32e7eSjoerg   // indexBody() should have set up the Dependencies set by now.
184006f32e7eSjoerg   for (auto *I : Dependencies)
184106f32e7eSjoerg     if (I->UseMacro) {
184206f32e7eSjoerg       VariablePostfix = "_" + utostr(Emitter.getUniqueNumber());
184306f32e7eSjoerg       break;
184406f32e7eSjoerg     }
184506f32e7eSjoerg 
184606f32e7eSjoerg   initVariables();
184706f32e7eSjoerg 
184806f32e7eSjoerg   emitPrototype(NamePrefix);
184906f32e7eSjoerg 
185006f32e7eSjoerg   if (IsUnavailable) {
185106f32e7eSjoerg     OS << " __attribute__((unavailable));";
185206f32e7eSjoerg   } else {
185306f32e7eSjoerg     emitOpeningBrace();
185406f32e7eSjoerg     emitShadowedArgs();
185506f32e7eSjoerg     if (ReverseArguments)
185606f32e7eSjoerg       emitArgumentReversal();
185706f32e7eSjoerg     emitBody(CallPrefix);
185806f32e7eSjoerg     if (ReverseArguments)
185906f32e7eSjoerg       emitReturnReversal();
186006f32e7eSjoerg     emitReturn();
186106f32e7eSjoerg     emitClosingBrace();
186206f32e7eSjoerg   }
186306f32e7eSjoerg   OS << "\n";
186406f32e7eSjoerg 
186506f32e7eSjoerg   CurrentRecord = nullptr;
186606f32e7eSjoerg }
186706f32e7eSjoerg 
indexBody()186806f32e7eSjoerg void Intrinsic::indexBody() {
186906f32e7eSjoerg   CurrentRecord = R;
187006f32e7eSjoerg 
187106f32e7eSjoerg   initVariables();
187206f32e7eSjoerg   emitBody("");
187306f32e7eSjoerg   OS.str("");
187406f32e7eSjoerg 
187506f32e7eSjoerg   CurrentRecord = nullptr;
187606f32e7eSjoerg }
187706f32e7eSjoerg 
187806f32e7eSjoerg //===----------------------------------------------------------------------===//
187906f32e7eSjoerg // NeonEmitter implementation
188006f32e7eSjoerg //===----------------------------------------------------------------------===//
188106f32e7eSjoerg 
getIntrinsic(StringRef Name,ArrayRef<Type> Types,Optional<std::string> MangledName)1882*13fbcb42Sjoerg Intrinsic &NeonEmitter::getIntrinsic(StringRef Name, ArrayRef<Type> Types,
1883*13fbcb42Sjoerg                                      Optional<std::string> MangledName) {
188406f32e7eSjoerg   // First, look up the name in the intrinsic map.
188506f32e7eSjoerg   assert_with_loc(IntrinsicMap.find(Name.str()) != IntrinsicMap.end(),
188606f32e7eSjoerg                   ("Intrinsic '" + Name + "' not found!").str());
188706f32e7eSjoerg   auto &V = IntrinsicMap.find(Name.str())->second;
188806f32e7eSjoerg   std::vector<Intrinsic *> GoodVec;
188906f32e7eSjoerg 
189006f32e7eSjoerg   // Create a string to print if we end up failing.
189106f32e7eSjoerg   std::string ErrMsg = "looking up intrinsic '" + Name.str() + "(";
189206f32e7eSjoerg   for (unsigned I = 0; I < Types.size(); ++I) {
189306f32e7eSjoerg     if (I != 0)
189406f32e7eSjoerg       ErrMsg += ", ";
189506f32e7eSjoerg     ErrMsg += Types[I].str();
189606f32e7eSjoerg   }
189706f32e7eSjoerg   ErrMsg += ")'\n";
189806f32e7eSjoerg   ErrMsg += "Available overloads:\n";
189906f32e7eSjoerg 
190006f32e7eSjoerg   // Now, look through each intrinsic implementation and see if the types are
190106f32e7eSjoerg   // compatible.
190206f32e7eSjoerg   for (auto &I : V) {
190306f32e7eSjoerg     ErrMsg += "  - " + I.getReturnType().str() + " " + I.getMangledName();
190406f32e7eSjoerg     ErrMsg += "(";
190506f32e7eSjoerg     for (unsigned A = 0; A < I.getNumParams(); ++A) {
190606f32e7eSjoerg       if (A != 0)
190706f32e7eSjoerg         ErrMsg += ", ";
190806f32e7eSjoerg       ErrMsg += I.getParamType(A).str();
190906f32e7eSjoerg     }
191006f32e7eSjoerg     ErrMsg += ")\n";
191106f32e7eSjoerg 
1912*13fbcb42Sjoerg     if (MangledName && MangledName != I.getMangledName(true))
1913*13fbcb42Sjoerg       continue;
1914*13fbcb42Sjoerg 
191506f32e7eSjoerg     if (I.getNumParams() != Types.size())
191606f32e7eSjoerg       continue;
191706f32e7eSjoerg 
1918*13fbcb42Sjoerg     unsigned ArgNum = 0;
1919*13fbcb42Sjoerg     bool MatchingArgumentTypes =
1920*13fbcb42Sjoerg         std::all_of(Types.begin(), Types.end(), [&](const auto &Type) {
1921*13fbcb42Sjoerg           return Type == I.getParamType(ArgNum++);
1922*13fbcb42Sjoerg         });
1923*13fbcb42Sjoerg 
1924*13fbcb42Sjoerg     if (MatchingArgumentTypes)
192506f32e7eSjoerg       GoodVec.push_back(&I);
192606f32e7eSjoerg   }
192706f32e7eSjoerg 
192806f32e7eSjoerg   assert_with_loc(!GoodVec.empty(),
192906f32e7eSjoerg                   "No compatible intrinsic found - " + ErrMsg);
193006f32e7eSjoerg   assert_with_loc(GoodVec.size() == 1, "Multiple overloads found - " + ErrMsg);
193106f32e7eSjoerg 
193206f32e7eSjoerg   return *GoodVec.front();
193306f32e7eSjoerg }
193406f32e7eSjoerg 
createIntrinsic(Record * R,SmallVectorImpl<Intrinsic * > & Out)193506f32e7eSjoerg void NeonEmitter::createIntrinsic(Record *R,
193606f32e7eSjoerg                                   SmallVectorImpl<Intrinsic *> &Out) {
1937*13fbcb42Sjoerg   std::string Name = std::string(R->getValueAsString("Name"));
1938*13fbcb42Sjoerg   std::string Proto = std::string(R->getValueAsString("Prototype"));
1939*13fbcb42Sjoerg   std::string Types = std::string(R->getValueAsString("Types"));
194006f32e7eSjoerg   Record *OperationRec = R->getValueAsDef("Operation");
194106f32e7eSjoerg   bool BigEndianSafe  = R->getValueAsBit("BigEndianSafe");
1942*13fbcb42Sjoerg   std::string Guard = std::string(R->getValueAsString("ArchGuard"));
194306f32e7eSjoerg   bool IsUnavailable = OperationRec->getValueAsBit("Unavailable");
1944*13fbcb42Sjoerg   std::string CartesianProductWith = std::string(R->getValueAsString("CartesianProductWith"));
194506f32e7eSjoerg 
194606f32e7eSjoerg   // Set the global current record. This allows assert_with_loc to produce
194706f32e7eSjoerg   // decent location information even when highly nested.
194806f32e7eSjoerg   CurrentRecord = R;
194906f32e7eSjoerg 
195006f32e7eSjoerg   ListInit *Body = OperationRec->getValueAsListInit("Ops");
195106f32e7eSjoerg 
195206f32e7eSjoerg   std::vector<TypeSpec> TypeSpecs = TypeSpec::fromTypeSpecs(Types);
195306f32e7eSjoerg 
195406f32e7eSjoerg   ClassKind CK = ClassNone;
195506f32e7eSjoerg   if (R->getSuperClasses().size() >= 2)
195606f32e7eSjoerg     CK = ClassMap[R->getSuperClasses()[1].first];
195706f32e7eSjoerg 
195806f32e7eSjoerg   std::vector<std::pair<TypeSpec, TypeSpec>> NewTypeSpecs;
1959*13fbcb42Sjoerg   if (!CartesianProductWith.empty()) {
1960*13fbcb42Sjoerg     std::vector<TypeSpec> ProductTypeSpecs = TypeSpec::fromTypeSpecs(CartesianProductWith);
196106f32e7eSjoerg     for (auto TS : TypeSpecs) {
1962*13fbcb42Sjoerg       Type DefaultT(TS, ".");
1963*13fbcb42Sjoerg       for (auto SrcTS : ProductTypeSpecs) {
1964*13fbcb42Sjoerg         Type DefaultSrcT(SrcTS, ".");
196506f32e7eSjoerg         if (TS == SrcTS ||
196606f32e7eSjoerg             DefaultSrcT.getSizeInBits() != DefaultT.getSizeInBits())
196706f32e7eSjoerg           continue;
196806f32e7eSjoerg         NewTypeSpecs.push_back(std::make_pair(TS, SrcTS));
196906f32e7eSjoerg       }
1970*13fbcb42Sjoerg     }
197106f32e7eSjoerg   } else {
1972*13fbcb42Sjoerg     for (auto TS : TypeSpecs) {
197306f32e7eSjoerg       NewTypeSpecs.push_back(std::make_pair(TS, TS));
197406f32e7eSjoerg     }
197506f32e7eSjoerg   }
197606f32e7eSjoerg 
197706f32e7eSjoerg   llvm::sort(NewTypeSpecs);
197806f32e7eSjoerg   NewTypeSpecs.erase(std::unique(NewTypeSpecs.begin(), NewTypeSpecs.end()),
197906f32e7eSjoerg 		     NewTypeSpecs.end());
198006f32e7eSjoerg   auto &Entry = IntrinsicMap[Name];
198106f32e7eSjoerg 
198206f32e7eSjoerg   for (auto &I : NewTypeSpecs) {
198306f32e7eSjoerg     Entry.emplace_back(R, Name, Proto, I.first, I.second, CK, Body, *this,
198406f32e7eSjoerg                        Guard, IsUnavailable, BigEndianSafe);
198506f32e7eSjoerg     Out.push_back(&Entry.back());
198606f32e7eSjoerg   }
198706f32e7eSjoerg 
198806f32e7eSjoerg   CurrentRecord = nullptr;
198906f32e7eSjoerg }
199006f32e7eSjoerg 
199106f32e7eSjoerg /// genBuiltinsDef: Generate the BuiltinsARM.def and  BuiltinsAArch64.def
199206f32e7eSjoerg /// declaration of builtins, checking for unique builtin declarations.
genBuiltinsDef(raw_ostream & OS,SmallVectorImpl<Intrinsic * > & Defs)199306f32e7eSjoerg void NeonEmitter::genBuiltinsDef(raw_ostream &OS,
199406f32e7eSjoerg                                  SmallVectorImpl<Intrinsic *> &Defs) {
199506f32e7eSjoerg   OS << "#ifdef GET_NEON_BUILTINS\n";
199606f32e7eSjoerg 
199706f32e7eSjoerg   // We only want to emit a builtin once, and we want to emit them in
199806f32e7eSjoerg   // alphabetical order, so use a std::set.
199906f32e7eSjoerg   std::set<std::string> Builtins;
200006f32e7eSjoerg 
200106f32e7eSjoerg   for (auto *Def : Defs) {
200206f32e7eSjoerg     if (Def->hasBody())
200306f32e7eSjoerg       continue;
200406f32e7eSjoerg 
200506f32e7eSjoerg     std::string S = "BUILTIN(__builtin_neon_" + Def->getMangledName() + ", \"";
200606f32e7eSjoerg 
200706f32e7eSjoerg     S += Def->getBuiltinTypeStr();
200806f32e7eSjoerg     S += "\", \"n\")";
200906f32e7eSjoerg 
201006f32e7eSjoerg     Builtins.insert(S);
201106f32e7eSjoerg   }
201206f32e7eSjoerg 
201306f32e7eSjoerg   for (auto &S : Builtins)
201406f32e7eSjoerg     OS << S << "\n";
201506f32e7eSjoerg   OS << "#endif\n\n";
201606f32e7eSjoerg }
201706f32e7eSjoerg 
201806f32e7eSjoerg /// Generate the ARM and AArch64 overloaded type checking code for
201906f32e7eSjoerg /// SemaChecking.cpp, checking for unique builtin declarations.
genOverloadTypeCheckCode(raw_ostream & OS,SmallVectorImpl<Intrinsic * > & Defs)202006f32e7eSjoerg void NeonEmitter::genOverloadTypeCheckCode(raw_ostream &OS,
202106f32e7eSjoerg                                            SmallVectorImpl<Intrinsic *> &Defs) {
202206f32e7eSjoerg   OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
202306f32e7eSjoerg 
202406f32e7eSjoerg   // We record each overload check line before emitting because subsequent Inst
202506f32e7eSjoerg   // definitions may extend the number of permitted types (i.e. augment the
202606f32e7eSjoerg   // Mask). Use std::map to avoid sorting the table by hash number.
202706f32e7eSjoerg   struct OverloadInfo {
202806f32e7eSjoerg     uint64_t Mask;
202906f32e7eSjoerg     int PtrArgNum;
203006f32e7eSjoerg     bool HasConstPtr;
203106f32e7eSjoerg     OverloadInfo() : Mask(0ULL), PtrArgNum(0), HasConstPtr(false) {}
203206f32e7eSjoerg   };
203306f32e7eSjoerg   std::map<std::string, OverloadInfo> OverloadMap;
203406f32e7eSjoerg 
203506f32e7eSjoerg   for (auto *Def : Defs) {
203606f32e7eSjoerg     // If the def has a body (that is, it has Operation DAGs), it won't call
203706f32e7eSjoerg     // __builtin_neon_* so we don't need to generate a definition for it.
203806f32e7eSjoerg     if (Def->hasBody())
203906f32e7eSjoerg       continue;
204006f32e7eSjoerg     // Functions which have a scalar argument cannot be overloaded, no need to
204106f32e7eSjoerg     // check them if we are emitting the type checking code.
204206f32e7eSjoerg     if (Def->protoHasScalar())
204306f32e7eSjoerg       continue;
204406f32e7eSjoerg 
204506f32e7eSjoerg     uint64_t Mask = 0ULL;
2046*13fbcb42Sjoerg     Mask |= 1ULL << Def->getPolymorphicKeyType().getNeonEnum();
204706f32e7eSjoerg 
204806f32e7eSjoerg     // Check if the function has a pointer or const pointer argument.
204906f32e7eSjoerg     int PtrArgNum = -1;
205006f32e7eSjoerg     bool HasConstPtr = false;
205106f32e7eSjoerg     for (unsigned I = 0; I < Def->getNumParams(); ++I) {
2052*13fbcb42Sjoerg       const auto &Type = Def->getParamType(I);
2053*13fbcb42Sjoerg       if (Type.isPointer()) {
205406f32e7eSjoerg         PtrArgNum = I;
2055*13fbcb42Sjoerg         HasConstPtr = Type.isConstPointer();
205606f32e7eSjoerg       }
205706f32e7eSjoerg     }
2058*13fbcb42Sjoerg 
205906f32e7eSjoerg     // For sret builtins, adjust the pointer argument index.
206006f32e7eSjoerg     if (PtrArgNum >= 0 && Def->getReturnType().getNumVectors() > 1)
206106f32e7eSjoerg       PtrArgNum += 1;
206206f32e7eSjoerg 
206306f32e7eSjoerg     std::string Name = Def->getName();
206406f32e7eSjoerg     // Omit type checking for the pointer arguments of vld1_lane, vld1_dup,
206506f32e7eSjoerg     // and vst1_lane intrinsics.  Using a pointer to the vector element
206606f32e7eSjoerg     // type with one of those operations causes codegen to select an aligned
206706f32e7eSjoerg     // load/store instruction.  If you want an unaligned operation,
206806f32e7eSjoerg     // the pointer argument needs to have less alignment than element type,
206906f32e7eSjoerg     // so just accept any pointer type.
207006f32e7eSjoerg     if (Name == "vld1_lane" || Name == "vld1_dup" || Name == "vst1_lane") {
207106f32e7eSjoerg       PtrArgNum = -1;
207206f32e7eSjoerg       HasConstPtr = false;
207306f32e7eSjoerg     }
207406f32e7eSjoerg 
207506f32e7eSjoerg     if (Mask) {
207606f32e7eSjoerg       std::string Name = Def->getMangledName();
207706f32e7eSjoerg       OverloadMap.insert(std::make_pair(Name, OverloadInfo()));
207806f32e7eSjoerg       OverloadInfo &OI = OverloadMap[Name];
207906f32e7eSjoerg       OI.Mask |= Mask;
208006f32e7eSjoerg       OI.PtrArgNum |= PtrArgNum;
208106f32e7eSjoerg       OI.HasConstPtr = HasConstPtr;
208206f32e7eSjoerg     }
208306f32e7eSjoerg   }
208406f32e7eSjoerg 
208506f32e7eSjoerg   for (auto &I : OverloadMap) {
208606f32e7eSjoerg     OverloadInfo &OI = I.second;
208706f32e7eSjoerg 
208806f32e7eSjoerg     OS << "case NEON::BI__builtin_neon_" << I.first << ": ";
208906f32e7eSjoerg     OS << "mask = 0x" << Twine::utohexstr(OI.Mask) << "ULL";
209006f32e7eSjoerg     if (OI.PtrArgNum >= 0)
209106f32e7eSjoerg       OS << "; PtrArgNum = " << OI.PtrArgNum;
209206f32e7eSjoerg     if (OI.HasConstPtr)
209306f32e7eSjoerg       OS << "; HasConstPtr = true";
209406f32e7eSjoerg     OS << "; break;\n";
209506f32e7eSjoerg   }
209606f32e7eSjoerg   OS << "#endif\n\n";
209706f32e7eSjoerg }
209806f32e7eSjoerg 
genIntrinsicRangeCheckCode(raw_ostream & OS,SmallVectorImpl<Intrinsic * > & Defs)209906f32e7eSjoerg void NeonEmitter::genIntrinsicRangeCheckCode(raw_ostream &OS,
210006f32e7eSjoerg                                         SmallVectorImpl<Intrinsic *> &Defs) {
210106f32e7eSjoerg   OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
210206f32e7eSjoerg 
210306f32e7eSjoerg   std::set<std::string> Emitted;
210406f32e7eSjoerg 
210506f32e7eSjoerg   for (auto *Def : Defs) {
210606f32e7eSjoerg     if (Def->hasBody())
210706f32e7eSjoerg       continue;
210806f32e7eSjoerg     // Functions which do not have an immediate do not need to have range
210906f32e7eSjoerg     // checking code emitted.
211006f32e7eSjoerg     if (!Def->hasImmediate())
211106f32e7eSjoerg       continue;
211206f32e7eSjoerg     if (Emitted.find(Def->getMangledName()) != Emitted.end())
211306f32e7eSjoerg       continue;
211406f32e7eSjoerg 
211506f32e7eSjoerg     std::string LowerBound, UpperBound;
211606f32e7eSjoerg 
211706f32e7eSjoerg     Record *R = Def->getRecord();
2118*13fbcb42Sjoerg     if (R->getValueAsBit("isVXAR")) {
2119*13fbcb42Sjoerg       //VXAR takes an immediate in the range [0, 63]
2120*13fbcb42Sjoerg       LowerBound = "0";
2121*13fbcb42Sjoerg       UpperBound = "63";
2122*13fbcb42Sjoerg     } else if (R->getValueAsBit("isVCVT_N")) {
212306f32e7eSjoerg       // VCVT between floating- and fixed-point values takes an immediate
212406f32e7eSjoerg       // in the range [1, 32) for f32 or [1, 64) for f64 or [1, 16) for f16.
212506f32e7eSjoerg       LowerBound = "1";
212606f32e7eSjoerg 	  if (Def->getBaseType().getElementSizeInBits() == 16 ||
212706f32e7eSjoerg 		  Def->getName().find('h') != std::string::npos)
212806f32e7eSjoerg 		// VCVTh operating on FP16 intrinsics in range [1, 16)
212906f32e7eSjoerg 		UpperBound = "15";
213006f32e7eSjoerg 	  else if (Def->getBaseType().getElementSizeInBits() == 32)
213106f32e7eSjoerg         UpperBound = "31";
213206f32e7eSjoerg 	  else
213306f32e7eSjoerg         UpperBound = "63";
213406f32e7eSjoerg     } else if (R->getValueAsBit("isScalarShift")) {
213506f32e7eSjoerg       // Right shifts have an 'r' in the name, left shifts do not. Convert
213606f32e7eSjoerg       // instructions have the same bounds and right shifts.
213706f32e7eSjoerg       if (Def->getName().find('r') != std::string::npos ||
213806f32e7eSjoerg           Def->getName().find("cvt") != std::string::npos)
213906f32e7eSjoerg         LowerBound = "1";
214006f32e7eSjoerg 
214106f32e7eSjoerg       UpperBound = utostr(Def->getReturnType().getElementSizeInBits() - 1);
214206f32e7eSjoerg     } else if (R->getValueAsBit("isShift")) {
214306f32e7eSjoerg       // Builtins which are overloaded by type will need to have their upper
214406f32e7eSjoerg       // bound computed at Sema time based on the type constant.
214506f32e7eSjoerg 
214606f32e7eSjoerg       // Right shifts have an 'r' in the name, left shifts do not.
214706f32e7eSjoerg       if (Def->getName().find('r') != std::string::npos)
214806f32e7eSjoerg         LowerBound = "1";
214906f32e7eSjoerg       UpperBound = "RFT(TV, true)";
215006f32e7eSjoerg     } else if (Def->getClassKind(true) == ClassB) {
215106f32e7eSjoerg       // ClassB intrinsics have a type (and hence lane number) that is only
215206f32e7eSjoerg       // known at runtime.
215306f32e7eSjoerg       if (R->getValueAsBit("isLaneQ"))
215406f32e7eSjoerg         UpperBound = "RFT(TV, false, true)";
215506f32e7eSjoerg       else
215606f32e7eSjoerg         UpperBound = "RFT(TV, false, false)";
215706f32e7eSjoerg     } else {
215806f32e7eSjoerg       // The immediate generally refers to a lane in the preceding argument.
215906f32e7eSjoerg       assert(Def->getImmediateIdx() > 0);
216006f32e7eSjoerg       Type T = Def->getParamType(Def->getImmediateIdx() - 1);
216106f32e7eSjoerg       UpperBound = utostr(T.getNumElements() - 1);
216206f32e7eSjoerg     }
216306f32e7eSjoerg 
216406f32e7eSjoerg     // Calculate the index of the immediate that should be range checked.
216506f32e7eSjoerg     unsigned Idx = Def->getNumParams();
216606f32e7eSjoerg     if (Def->hasImmediate())
216706f32e7eSjoerg       Idx = Def->getGeneratedParamIdx(Def->getImmediateIdx());
216806f32e7eSjoerg 
216906f32e7eSjoerg     OS << "case NEON::BI__builtin_neon_" << Def->getMangledName() << ": "
217006f32e7eSjoerg        << "i = " << Idx << ";";
217106f32e7eSjoerg     if (!LowerBound.empty())
217206f32e7eSjoerg       OS << " l = " << LowerBound << ";";
217306f32e7eSjoerg     if (!UpperBound.empty())
217406f32e7eSjoerg       OS << " u = " << UpperBound << ";";
217506f32e7eSjoerg     OS << " break;\n";
217606f32e7eSjoerg 
217706f32e7eSjoerg     Emitted.insert(Def->getMangledName());
217806f32e7eSjoerg   }
217906f32e7eSjoerg 
218006f32e7eSjoerg   OS << "#endif\n\n";
218106f32e7eSjoerg }
218206f32e7eSjoerg 
218306f32e7eSjoerg /// runHeader - Emit a file with sections defining:
218406f32e7eSjoerg /// 1. the NEON section of BuiltinsARM.def and BuiltinsAArch64.def.
218506f32e7eSjoerg /// 2. the SemaChecking code for the type overload checking.
218606f32e7eSjoerg /// 3. the SemaChecking code for validation of intrinsic immediate arguments.
runHeader(raw_ostream & OS)218706f32e7eSjoerg void NeonEmitter::runHeader(raw_ostream &OS) {
218806f32e7eSjoerg   std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
218906f32e7eSjoerg 
219006f32e7eSjoerg   SmallVector<Intrinsic *, 128> Defs;
219106f32e7eSjoerg   for (auto *R : RV)
219206f32e7eSjoerg     createIntrinsic(R, Defs);
219306f32e7eSjoerg 
219406f32e7eSjoerg   // Generate shared BuiltinsXXX.def
219506f32e7eSjoerg   genBuiltinsDef(OS, Defs);
219606f32e7eSjoerg 
219706f32e7eSjoerg   // Generate ARM overloaded type checking code for SemaChecking.cpp
219806f32e7eSjoerg   genOverloadTypeCheckCode(OS, Defs);
219906f32e7eSjoerg 
220006f32e7eSjoerg   // Generate ARM range checking code for shift/lane immediates.
220106f32e7eSjoerg   genIntrinsicRangeCheckCode(OS, Defs);
220206f32e7eSjoerg }
220306f32e7eSjoerg 
emitNeonTypeDefs(const std::string & types,raw_ostream & OS)2204*13fbcb42Sjoerg static void emitNeonTypeDefs(const std::string& types, raw_ostream &OS) {
2205*13fbcb42Sjoerg   std::string TypedefTypes(types);
2206*13fbcb42Sjoerg   std::vector<TypeSpec> TDTypeVec = TypeSpec::fromTypeSpecs(TypedefTypes);
2207*13fbcb42Sjoerg 
2208*13fbcb42Sjoerg   // Emit vector typedefs.
2209*13fbcb42Sjoerg   bool InIfdef = false;
2210*13fbcb42Sjoerg   for (auto &TS : TDTypeVec) {
2211*13fbcb42Sjoerg     bool IsA64 = false;
2212*13fbcb42Sjoerg     Type T(TS, ".");
2213*13fbcb42Sjoerg     if (T.isDouble())
2214*13fbcb42Sjoerg       IsA64 = true;
2215*13fbcb42Sjoerg 
2216*13fbcb42Sjoerg     if (InIfdef && !IsA64) {
2217*13fbcb42Sjoerg       OS << "#endif\n";
2218*13fbcb42Sjoerg       InIfdef = false;
2219*13fbcb42Sjoerg     }
2220*13fbcb42Sjoerg     if (!InIfdef && IsA64) {
2221*13fbcb42Sjoerg       OS << "#ifdef __aarch64__\n";
2222*13fbcb42Sjoerg       InIfdef = true;
2223*13fbcb42Sjoerg     }
2224*13fbcb42Sjoerg 
2225*13fbcb42Sjoerg     if (T.isPoly())
2226*13fbcb42Sjoerg       OS << "typedef __attribute__((neon_polyvector_type(";
2227*13fbcb42Sjoerg     else
2228*13fbcb42Sjoerg       OS << "typedef __attribute__((neon_vector_type(";
2229*13fbcb42Sjoerg 
2230*13fbcb42Sjoerg     Type T2 = T;
2231*13fbcb42Sjoerg     T2.makeScalar();
2232*13fbcb42Sjoerg     OS << T.getNumElements() << "))) ";
2233*13fbcb42Sjoerg     OS << T2.str();
2234*13fbcb42Sjoerg     OS << " " << T.str() << ";\n";
2235*13fbcb42Sjoerg   }
2236*13fbcb42Sjoerg   if (InIfdef)
2237*13fbcb42Sjoerg     OS << "#endif\n";
2238*13fbcb42Sjoerg   OS << "\n";
2239*13fbcb42Sjoerg 
2240*13fbcb42Sjoerg   // Emit struct typedefs.
2241*13fbcb42Sjoerg   InIfdef = false;
2242*13fbcb42Sjoerg   for (unsigned NumMembers = 2; NumMembers <= 4; ++NumMembers) {
2243*13fbcb42Sjoerg     for (auto &TS : TDTypeVec) {
2244*13fbcb42Sjoerg       bool IsA64 = false;
2245*13fbcb42Sjoerg       Type T(TS, ".");
2246*13fbcb42Sjoerg       if (T.isDouble())
2247*13fbcb42Sjoerg         IsA64 = true;
2248*13fbcb42Sjoerg 
2249*13fbcb42Sjoerg       if (InIfdef && !IsA64) {
2250*13fbcb42Sjoerg         OS << "#endif\n";
2251*13fbcb42Sjoerg         InIfdef = false;
2252*13fbcb42Sjoerg       }
2253*13fbcb42Sjoerg       if (!InIfdef && IsA64) {
2254*13fbcb42Sjoerg         OS << "#ifdef __aarch64__\n";
2255*13fbcb42Sjoerg         InIfdef = true;
2256*13fbcb42Sjoerg       }
2257*13fbcb42Sjoerg 
2258*13fbcb42Sjoerg       const char Mods[] = { static_cast<char>('2' + (NumMembers - 2)), 0};
2259*13fbcb42Sjoerg       Type VT(TS, Mods);
2260*13fbcb42Sjoerg       OS << "typedef struct " << VT.str() << " {\n";
2261*13fbcb42Sjoerg       OS << "  " << T.str() << " val";
2262*13fbcb42Sjoerg       OS << "[" << NumMembers << "]";
2263*13fbcb42Sjoerg       OS << ";\n} ";
2264*13fbcb42Sjoerg       OS << VT.str() << ";\n";
2265*13fbcb42Sjoerg       OS << "\n";
2266*13fbcb42Sjoerg     }
2267*13fbcb42Sjoerg   }
2268*13fbcb42Sjoerg   if (InIfdef)
2269*13fbcb42Sjoerg     OS << "#endif\n";
2270*13fbcb42Sjoerg }
2271*13fbcb42Sjoerg 
227206f32e7eSjoerg /// run - Read the records in arm_neon.td and output arm_neon.h.  arm_neon.h
227306f32e7eSjoerg /// is comprised of type definitions and function declarations.
run(raw_ostream & OS)227406f32e7eSjoerg void NeonEmitter::run(raw_ostream &OS) {
227506f32e7eSjoerg   OS << "/*===---- arm_neon.h - ARM Neon intrinsics "
227606f32e7eSjoerg         "------------------------------"
227706f32e7eSjoerg         "---===\n"
227806f32e7eSjoerg         " *\n"
227906f32e7eSjoerg         " * Permission is hereby granted, free of charge, to any person "
228006f32e7eSjoerg         "obtaining "
228106f32e7eSjoerg         "a copy\n"
228206f32e7eSjoerg         " * of this software and associated documentation files (the "
228306f32e7eSjoerg         "\"Software\"),"
228406f32e7eSjoerg         " to deal\n"
228506f32e7eSjoerg         " * in the Software without restriction, including without limitation "
228606f32e7eSjoerg         "the "
228706f32e7eSjoerg         "rights\n"
228806f32e7eSjoerg         " * to use, copy, modify, merge, publish, distribute, sublicense, "
228906f32e7eSjoerg         "and/or sell\n"
229006f32e7eSjoerg         " * copies of the Software, and to permit persons to whom the Software "
229106f32e7eSjoerg         "is\n"
229206f32e7eSjoerg         " * furnished to do so, subject to the following conditions:\n"
229306f32e7eSjoerg         " *\n"
229406f32e7eSjoerg         " * The above copyright notice and this permission notice shall be "
229506f32e7eSjoerg         "included in\n"
229606f32e7eSjoerg         " * all copies or substantial portions of the Software.\n"
229706f32e7eSjoerg         " *\n"
229806f32e7eSjoerg         " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, "
229906f32e7eSjoerg         "EXPRESS OR\n"
230006f32e7eSjoerg         " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
230106f32e7eSjoerg         "MERCHANTABILITY,\n"
230206f32e7eSjoerg         " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT "
230306f32e7eSjoerg         "SHALL THE\n"
230406f32e7eSjoerg         " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR "
230506f32e7eSjoerg         "OTHER\n"
230606f32e7eSjoerg         " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, "
230706f32e7eSjoerg         "ARISING FROM,\n"
230806f32e7eSjoerg         " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER "
230906f32e7eSjoerg         "DEALINGS IN\n"
231006f32e7eSjoerg         " * THE SOFTWARE.\n"
231106f32e7eSjoerg         " *\n"
231206f32e7eSjoerg         " *===-----------------------------------------------------------------"
231306f32e7eSjoerg         "---"
231406f32e7eSjoerg         "---===\n"
231506f32e7eSjoerg         " */\n\n";
231606f32e7eSjoerg 
231706f32e7eSjoerg   OS << "#ifndef __ARM_NEON_H\n";
231806f32e7eSjoerg   OS << "#define __ARM_NEON_H\n\n";
231906f32e7eSjoerg 
2320*13fbcb42Sjoerg   OS << "#ifndef __ARM_FP\n";
2321*13fbcb42Sjoerg   OS << "#error \"NEON intrinsics not available with the soft-float ABI. "
2322*13fbcb42Sjoerg         "Please use -mfloat-abi=softfp or -mfloat-abi=hard\"\n";
2323*13fbcb42Sjoerg   OS << "#else\n\n";
2324*13fbcb42Sjoerg 
232506f32e7eSjoerg   OS << "#if !defined(__ARM_NEON)\n";
232606f32e7eSjoerg   OS << "#error \"NEON support not enabled\"\n";
2327*13fbcb42Sjoerg   OS << "#else\n\n";
232806f32e7eSjoerg 
232906f32e7eSjoerg   OS << "#include <stdint.h>\n\n";
233006f32e7eSjoerg 
2331*13fbcb42Sjoerg   OS << "#ifdef __ARM_FEATURE_BF16\n";
2332*13fbcb42Sjoerg   OS << "#include <arm_bf16.h>\n";
2333*13fbcb42Sjoerg   OS << "typedef __bf16 bfloat16_t;\n";
2334*13fbcb42Sjoerg   OS << "#endif\n\n";
2335*13fbcb42Sjoerg 
233606f32e7eSjoerg   // Emit NEON-specific scalar typedefs.
233706f32e7eSjoerg   OS << "typedef float float32_t;\n";
233806f32e7eSjoerg   OS << "typedef __fp16 float16_t;\n";
233906f32e7eSjoerg 
234006f32e7eSjoerg   OS << "#ifdef __aarch64__\n";
234106f32e7eSjoerg   OS << "typedef double float64_t;\n";
234206f32e7eSjoerg   OS << "#endif\n\n";
234306f32e7eSjoerg 
234406f32e7eSjoerg   // For now, signedness of polynomial types depends on target
234506f32e7eSjoerg   OS << "#ifdef __aarch64__\n";
234606f32e7eSjoerg   OS << "typedef uint8_t poly8_t;\n";
234706f32e7eSjoerg   OS << "typedef uint16_t poly16_t;\n";
234806f32e7eSjoerg   OS << "typedef uint64_t poly64_t;\n";
234906f32e7eSjoerg   OS << "typedef __uint128_t poly128_t;\n";
235006f32e7eSjoerg   OS << "#else\n";
235106f32e7eSjoerg   OS << "typedef int8_t poly8_t;\n";
235206f32e7eSjoerg   OS << "typedef int16_t poly16_t;\n";
2353*13fbcb42Sjoerg   OS << "typedef int64_t poly64_t;\n";
235406f32e7eSjoerg   OS << "#endif\n";
235506f32e7eSjoerg 
2356*13fbcb42Sjoerg   emitNeonTypeDefs("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfdQdPcQPcPsQPsPlQPl", OS);
235706f32e7eSjoerg 
2358*13fbcb42Sjoerg   OS << "#ifdef __ARM_FEATURE_BF16\n";
2359*13fbcb42Sjoerg   emitNeonTypeDefs("bQb", OS);
2360*13fbcb42Sjoerg   OS << "#endif\n\n";
236106f32e7eSjoerg 
236206f32e7eSjoerg   OS << "#define __ai static __inline__ __attribute__((__always_inline__, "
236306f32e7eSjoerg         "__nodebug__))\n\n";
236406f32e7eSjoerg 
236506f32e7eSjoerg   SmallVector<Intrinsic *, 128> Defs;
236606f32e7eSjoerg   std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
236706f32e7eSjoerg   for (auto *R : RV)
236806f32e7eSjoerg     createIntrinsic(R, Defs);
236906f32e7eSjoerg 
237006f32e7eSjoerg   for (auto *I : Defs)
237106f32e7eSjoerg     I->indexBody();
237206f32e7eSjoerg 
237306f32e7eSjoerg   llvm::stable_sort(Defs, llvm::deref<std::less<>>());
237406f32e7eSjoerg 
237506f32e7eSjoerg   // Only emit a def when its requirements have been met.
237606f32e7eSjoerg   // FIXME: This loop could be made faster, but it's fast enough for now.
237706f32e7eSjoerg   bool MadeProgress = true;
237806f32e7eSjoerg   std::string InGuard;
237906f32e7eSjoerg   while (!Defs.empty() && MadeProgress) {
238006f32e7eSjoerg     MadeProgress = false;
238106f32e7eSjoerg 
238206f32e7eSjoerg     for (SmallVector<Intrinsic *, 128>::iterator I = Defs.begin();
238306f32e7eSjoerg          I != Defs.end(); /*No step*/) {
238406f32e7eSjoerg       bool DependenciesSatisfied = true;
238506f32e7eSjoerg       for (auto *II : (*I)->getDependencies()) {
238606f32e7eSjoerg         if (llvm::is_contained(Defs, II))
238706f32e7eSjoerg           DependenciesSatisfied = false;
238806f32e7eSjoerg       }
238906f32e7eSjoerg       if (!DependenciesSatisfied) {
239006f32e7eSjoerg         // Try the next one.
239106f32e7eSjoerg         ++I;
239206f32e7eSjoerg         continue;
239306f32e7eSjoerg       }
239406f32e7eSjoerg 
239506f32e7eSjoerg       // Emit #endif/#if pair if needed.
239606f32e7eSjoerg       if ((*I)->getGuard() != InGuard) {
239706f32e7eSjoerg         if (!InGuard.empty())
239806f32e7eSjoerg           OS << "#endif\n";
239906f32e7eSjoerg         InGuard = (*I)->getGuard();
240006f32e7eSjoerg         if (!InGuard.empty())
240106f32e7eSjoerg           OS << "#if " << InGuard << "\n";
240206f32e7eSjoerg       }
240306f32e7eSjoerg 
240406f32e7eSjoerg       // Actually generate the intrinsic code.
240506f32e7eSjoerg       OS << (*I)->generate();
240606f32e7eSjoerg 
240706f32e7eSjoerg       MadeProgress = true;
240806f32e7eSjoerg       I = Defs.erase(I);
240906f32e7eSjoerg     }
241006f32e7eSjoerg   }
241106f32e7eSjoerg   assert(Defs.empty() && "Some requirements were not satisfied!");
241206f32e7eSjoerg   if (!InGuard.empty())
241306f32e7eSjoerg     OS << "#endif\n";
241406f32e7eSjoerg 
241506f32e7eSjoerg   OS << "\n";
241606f32e7eSjoerg   OS << "#undef __ai\n\n";
2417*13fbcb42Sjoerg   OS << "#endif /* if !defined(__ARM_NEON) */\n";
2418*13fbcb42Sjoerg   OS << "#endif /* ifndef __ARM_FP */\n";
241906f32e7eSjoerg   OS << "#endif /* __ARM_NEON_H */\n";
242006f32e7eSjoerg }
242106f32e7eSjoerg 
242206f32e7eSjoerg /// run - Read the records in arm_fp16.td and output arm_fp16.h.  arm_fp16.h
242306f32e7eSjoerg /// is comprised of type definitions and function declarations.
runFP16(raw_ostream & OS)242406f32e7eSjoerg void NeonEmitter::runFP16(raw_ostream &OS) {
242506f32e7eSjoerg   OS << "/*===---- arm_fp16.h - ARM FP16 intrinsics "
242606f32e7eSjoerg         "------------------------------"
242706f32e7eSjoerg         "---===\n"
242806f32e7eSjoerg         " *\n"
242906f32e7eSjoerg         " * Permission is hereby granted, free of charge, to any person "
243006f32e7eSjoerg         "obtaining a copy\n"
243106f32e7eSjoerg         " * of this software and associated documentation files (the "
243206f32e7eSjoerg 				"\"Software\"), to deal\n"
243306f32e7eSjoerg         " * in the Software without restriction, including without limitation "
243406f32e7eSjoerg 				"the rights\n"
243506f32e7eSjoerg         " * to use, copy, modify, merge, publish, distribute, sublicense, "
243606f32e7eSjoerg 				"and/or sell\n"
243706f32e7eSjoerg         " * copies of the Software, and to permit persons to whom the Software "
243806f32e7eSjoerg 				"is\n"
243906f32e7eSjoerg         " * furnished to do so, subject to the following conditions:\n"
244006f32e7eSjoerg         " *\n"
244106f32e7eSjoerg         " * The above copyright notice and this permission notice shall be "
244206f32e7eSjoerg         "included in\n"
244306f32e7eSjoerg         " * all copies or substantial portions of the Software.\n"
244406f32e7eSjoerg         " *\n"
244506f32e7eSjoerg         " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, "
244606f32e7eSjoerg         "EXPRESS OR\n"
244706f32e7eSjoerg         " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
244806f32e7eSjoerg         "MERCHANTABILITY,\n"
244906f32e7eSjoerg         " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT "
245006f32e7eSjoerg         "SHALL THE\n"
245106f32e7eSjoerg         " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR "
245206f32e7eSjoerg         "OTHER\n"
245306f32e7eSjoerg         " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, "
245406f32e7eSjoerg         "ARISING FROM,\n"
245506f32e7eSjoerg         " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER "
245606f32e7eSjoerg         "DEALINGS IN\n"
245706f32e7eSjoerg         " * THE SOFTWARE.\n"
245806f32e7eSjoerg         " *\n"
245906f32e7eSjoerg         " *===-----------------------------------------------------------------"
246006f32e7eSjoerg         "---"
246106f32e7eSjoerg         "---===\n"
246206f32e7eSjoerg         " */\n\n";
246306f32e7eSjoerg 
246406f32e7eSjoerg   OS << "#ifndef __ARM_FP16_H\n";
246506f32e7eSjoerg   OS << "#define __ARM_FP16_H\n\n";
246606f32e7eSjoerg 
246706f32e7eSjoerg   OS << "#include <stdint.h>\n\n";
246806f32e7eSjoerg 
246906f32e7eSjoerg   OS << "typedef __fp16 float16_t;\n";
247006f32e7eSjoerg 
247106f32e7eSjoerg   OS << "#define __ai static __inline__ __attribute__((__always_inline__, "
247206f32e7eSjoerg         "__nodebug__))\n\n";
247306f32e7eSjoerg 
247406f32e7eSjoerg   SmallVector<Intrinsic *, 128> Defs;
247506f32e7eSjoerg   std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
247606f32e7eSjoerg   for (auto *R : RV)
247706f32e7eSjoerg     createIntrinsic(R, Defs);
247806f32e7eSjoerg 
247906f32e7eSjoerg   for (auto *I : Defs)
248006f32e7eSjoerg     I->indexBody();
248106f32e7eSjoerg 
248206f32e7eSjoerg   llvm::stable_sort(Defs, llvm::deref<std::less<>>());
248306f32e7eSjoerg 
248406f32e7eSjoerg   // Only emit a def when its requirements have been met.
248506f32e7eSjoerg   // FIXME: This loop could be made faster, but it's fast enough for now.
248606f32e7eSjoerg   bool MadeProgress = true;
248706f32e7eSjoerg   std::string InGuard;
248806f32e7eSjoerg   while (!Defs.empty() && MadeProgress) {
248906f32e7eSjoerg     MadeProgress = false;
249006f32e7eSjoerg 
249106f32e7eSjoerg     for (SmallVector<Intrinsic *, 128>::iterator I = Defs.begin();
249206f32e7eSjoerg          I != Defs.end(); /*No step*/) {
249306f32e7eSjoerg       bool DependenciesSatisfied = true;
249406f32e7eSjoerg       for (auto *II : (*I)->getDependencies()) {
249506f32e7eSjoerg         if (llvm::is_contained(Defs, II))
249606f32e7eSjoerg           DependenciesSatisfied = false;
249706f32e7eSjoerg       }
249806f32e7eSjoerg       if (!DependenciesSatisfied) {
249906f32e7eSjoerg         // Try the next one.
250006f32e7eSjoerg         ++I;
250106f32e7eSjoerg         continue;
250206f32e7eSjoerg       }
250306f32e7eSjoerg 
250406f32e7eSjoerg       // Emit #endif/#if pair if needed.
250506f32e7eSjoerg       if ((*I)->getGuard() != InGuard) {
250606f32e7eSjoerg         if (!InGuard.empty())
250706f32e7eSjoerg           OS << "#endif\n";
250806f32e7eSjoerg         InGuard = (*I)->getGuard();
250906f32e7eSjoerg         if (!InGuard.empty())
251006f32e7eSjoerg           OS << "#if " << InGuard << "\n";
251106f32e7eSjoerg       }
251206f32e7eSjoerg 
251306f32e7eSjoerg       // Actually generate the intrinsic code.
251406f32e7eSjoerg       OS << (*I)->generate();
251506f32e7eSjoerg 
251606f32e7eSjoerg       MadeProgress = true;
251706f32e7eSjoerg       I = Defs.erase(I);
251806f32e7eSjoerg     }
251906f32e7eSjoerg   }
252006f32e7eSjoerg   assert(Defs.empty() && "Some requirements were not satisfied!");
252106f32e7eSjoerg   if (!InGuard.empty())
252206f32e7eSjoerg     OS << "#endif\n";
252306f32e7eSjoerg 
252406f32e7eSjoerg   OS << "\n";
252506f32e7eSjoerg   OS << "#undef __ai\n\n";
252606f32e7eSjoerg   OS << "#endif /* __ARM_FP16_H */\n";
252706f32e7eSjoerg }
252806f32e7eSjoerg 
runBF16(raw_ostream & OS)2529*13fbcb42Sjoerg void NeonEmitter::runBF16(raw_ostream &OS) {
2530*13fbcb42Sjoerg   OS << "/*===---- arm_bf16.h - ARM BF16 intrinsics "
2531*13fbcb42Sjoerg         "-----------------------------------===\n"
2532*13fbcb42Sjoerg         " *\n"
2533*13fbcb42Sjoerg         " *\n"
2534*13fbcb42Sjoerg         " * Part of the LLVM Project, under the Apache License v2.0 with LLVM "
2535*13fbcb42Sjoerg         "Exceptions.\n"
2536*13fbcb42Sjoerg         " * See https://llvm.org/LICENSE.txt for license information.\n"
2537*13fbcb42Sjoerg         " * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n"
2538*13fbcb42Sjoerg         " *\n"
2539*13fbcb42Sjoerg         " *===-----------------------------------------------------------------"
2540*13fbcb42Sjoerg         "------===\n"
2541*13fbcb42Sjoerg         " */\n\n";
2542*13fbcb42Sjoerg 
2543*13fbcb42Sjoerg   OS << "#ifndef __ARM_BF16_H\n";
2544*13fbcb42Sjoerg   OS << "#define __ARM_BF16_H\n\n";
2545*13fbcb42Sjoerg 
2546*13fbcb42Sjoerg   OS << "typedef __bf16 bfloat16_t;\n";
2547*13fbcb42Sjoerg 
2548*13fbcb42Sjoerg   OS << "#define __ai static __inline__ __attribute__((__always_inline__, "
2549*13fbcb42Sjoerg         "__nodebug__))\n\n";
2550*13fbcb42Sjoerg 
2551*13fbcb42Sjoerg   SmallVector<Intrinsic *, 128> Defs;
2552*13fbcb42Sjoerg   std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
2553*13fbcb42Sjoerg   for (auto *R : RV)
2554*13fbcb42Sjoerg     createIntrinsic(R, Defs);
2555*13fbcb42Sjoerg 
2556*13fbcb42Sjoerg   for (auto *I : Defs)
2557*13fbcb42Sjoerg     I->indexBody();
2558*13fbcb42Sjoerg 
2559*13fbcb42Sjoerg   llvm::stable_sort(Defs, llvm::deref<std::less<>>());
2560*13fbcb42Sjoerg 
2561*13fbcb42Sjoerg   // Only emit a def when its requirements have been met.
2562*13fbcb42Sjoerg   // FIXME: This loop could be made faster, but it's fast enough for now.
2563*13fbcb42Sjoerg   bool MadeProgress = true;
2564*13fbcb42Sjoerg   std::string InGuard;
2565*13fbcb42Sjoerg   while (!Defs.empty() && MadeProgress) {
2566*13fbcb42Sjoerg     MadeProgress = false;
2567*13fbcb42Sjoerg 
2568*13fbcb42Sjoerg     for (SmallVector<Intrinsic *, 128>::iterator I = Defs.begin();
2569*13fbcb42Sjoerg          I != Defs.end(); /*No step*/) {
2570*13fbcb42Sjoerg       bool DependenciesSatisfied = true;
2571*13fbcb42Sjoerg       for (auto *II : (*I)->getDependencies()) {
2572*13fbcb42Sjoerg         if (llvm::is_contained(Defs, II))
2573*13fbcb42Sjoerg           DependenciesSatisfied = false;
2574*13fbcb42Sjoerg       }
2575*13fbcb42Sjoerg       if (!DependenciesSatisfied) {
2576*13fbcb42Sjoerg         // Try the next one.
2577*13fbcb42Sjoerg         ++I;
2578*13fbcb42Sjoerg         continue;
2579*13fbcb42Sjoerg       }
2580*13fbcb42Sjoerg 
2581*13fbcb42Sjoerg       // Emit #endif/#if pair if needed.
2582*13fbcb42Sjoerg       if ((*I)->getGuard() != InGuard) {
2583*13fbcb42Sjoerg         if (!InGuard.empty())
2584*13fbcb42Sjoerg           OS << "#endif\n";
2585*13fbcb42Sjoerg         InGuard = (*I)->getGuard();
2586*13fbcb42Sjoerg         if (!InGuard.empty())
2587*13fbcb42Sjoerg           OS << "#if " << InGuard << "\n";
2588*13fbcb42Sjoerg       }
2589*13fbcb42Sjoerg 
2590*13fbcb42Sjoerg       // Actually generate the intrinsic code.
2591*13fbcb42Sjoerg       OS << (*I)->generate();
2592*13fbcb42Sjoerg 
2593*13fbcb42Sjoerg       MadeProgress = true;
2594*13fbcb42Sjoerg       I = Defs.erase(I);
2595*13fbcb42Sjoerg     }
2596*13fbcb42Sjoerg   }
2597*13fbcb42Sjoerg   assert(Defs.empty() && "Some requirements were not satisfied!");
2598*13fbcb42Sjoerg   if (!InGuard.empty())
2599*13fbcb42Sjoerg     OS << "#endif\n";
2600*13fbcb42Sjoerg 
2601*13fbcb42Sjoerg   OS << "\n";
2602*13fbcb42Sjoerg   OS << "#undef __ai\n\n";
2603*13fbcb42Sjoerg 
2604*13fbcb42Sjoerg   OS << "#endif\n";
2605*13fbcb42Sjoerg }
2606*13fbcb42Sjoerg 
EmitNeon(RecordKeeper & Records,raw_ostream & OS)260706f32e7eSjoerg void clang::EmitNeon(RecordKeeper &Records, raw_ostream &OS) {
260806f32e7eSjoerg   NeonEmitter(Records).run(OS);
260906f32e7eSjoerg }
261006f32e7eSjoerg 
EmitFP16(RecordKeeper & Records,raw_ostream & OS)261106f32e7eSjoerg void clang::EmitFP16(RecordKeeper &Records, raw_ostream &OS) {
261206f32e7eSjoerg   NeonEmitter(Records).runFP16(OS);
261306f32e7eSjoerg }
261406f32e7eSjoerg 
EmitBF16(RecordKeeper & Records,raw_ostream & OS)2615*13fbcb42Sjoerg void clang::EmitBF16(RecordKeeper &Records, raw_ostream &OS) {
2616*13fbcb42Sjoerg   NeonEmitter(Records).runBF16(OS);
2617*13fbcb42Sjoerg }
2618*13fbcb42Sjoerg 
EmitNeonSema(RecordKeeper & Records,raw_ostream & OS)261906f32e7eSjoerg void clang::EmitNeonSema(RecordKeeper &Records, raw_ostream &OS) {
262006f32e7eSjoerg   NeonEmitter(Records).runHeader(OS);
262106f32e7eSjoerg }
262206f32e7eSjoerg 
EmitNeonTest(RecordKeeper & Records,raw_ostream & OS)262306f32e7eSjoerg void clang::EmitNeonTest(RecordKeeper &Records, raw_ostream &OS) {
262406f32e7eSjoerg   llvm_unreachable("Neon test generation no longer implemented!");
262506f32e7eSjoerg }
2626