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