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