10b57cec5SDimitry Andric //===- NeonEmitter.cpp - Generate arm_neon.h for use with clang -*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This tablegen backend is responsible for emitting arm_neon.h, which includes 100b57cec5SDimitry Andric // a declaration and definition of each function specified by the ARM NEON 110b57cec5SDimitry Andric // compiler interface. See ARM document DUI0348B. 120b57cec5SDimitry Andric // 130b57cec5SDimitry Andric // Each NEON instruction is implemented in terms of 1 or more functions which 140b57cec5SDimitry Andric // are suffixed with the element type of the input vectors. Functions may be 150b57cec5SDimitry Andric // implemented in terms of generic vector operations such as +, *, -, etc. or 160b57cec5SDimitry Andric // by calling a __builtin_-prefixed function which will be handled by clang's 170b57cec5SDimitry Andric // CodeGen library. 180b57cec5SDimitry Andric // 190b57cec5SDimitry Andric // Additional validation code can be generated by this file when runHeader() is 200b57cec5SDimitry Andric // called, rather than the normal run() entry point. 210b57cec5SDimitry Andric // 220b57cec5SDimitry Andric // See also the documentation in include/clang/Basic/arm_neon.td. 230b57cec5SDimitry Andric // 240b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 250b57cec5SDimitry Andric 26a7dea167SDimitry Andric #include "TableGenBackends.h" 270b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 280b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 290b57cec5SDimitry Andric #include "llvm/ADT/None.h" 30*5ffd83dbSDimitry Andric #include "llvm/ADT/Optional.h" 310b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 32*5ffd83dbSDimitry Andric #include "llvm/ADT/SmallVector.h" 330b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h" 340b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 350b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 360b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 370b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 380b57cec5SDimitry Andric #include "llvm/TableGen/Error.h" 390b57cec5SDimitry Andric #include "llvm/TableGen/Record.h" 400b57cec5SDimitry Andric #include "llvm/TableGen/SetTheory.h" 410b57cec5SDimitry Andric #include <algorithm> 420b57cec5SDimitry Andric #include <cassert> 430b57cec5SDimitry Andric #include <cctype> 440b57cec5SDimitry Andric #include <cstddef> 450b57cec5SDimitry Andric #include <cstdint> 460b57cec5SDimitry Andric #include <deque> 470b57cec5SDimitry Andric #include <map> 480b57cec5SDimitry Andric #include <set> 490b57cec5SDimitry Andric #include <sstream> 500b57cec5SDimitry Andric #include <string> 510b57cec5SDimitry Andric #include <utility> 520b57cec5SDimitry Andric #include <vector> 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric using namespace llvm; 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric namespace { 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric // While globals are generally bad, this one allows us to perform assertions 590b57cec5SDimitry Andric // liberally and somehow still trace them back to the def they indirectly 600b57cec5SDimitry Andric // came from. 610b57cec5SDimitry Andric static Record *CurrentRecord = nullptr; 620b57cec5SDimitry Andric static void assert_with_loc(bool Assertion, const std::string &Str) { 630b57cec5SDimitry Andric if (!Assertion) { 640b57cec5SDimitry Andric if (CurrentRecord) 650b57cec5SDimitry Andric PrintFatalError(CurrentRecord->getLoc(), Str); 660b57cec5SDimitry Andric else 670b57cec5SDimitry Andric PrintFatalError(Str); 680b57cec5SDimitry Andric } 690b57cec5SDimitry Andric } 700b57cec5SDimitry Andric 710b57cec5SDimitry Andric enum ClassKind { 720b57cec5SDimitry Andric ClassNone, 730b57cec5SDimitry Andric ClassI, // generic integer instruction, e.g., "i8" suffix 740b57cec5SDimitry Andric ClassS, // signed/unsigned/poly, e.g., "s8", "u8" or "p8" suffix 750b57cec5SDimitry Andric ClassW, // width-specific instruction, e.g., "8" suffix 760b57cec5SDimitry Andric ClassB, // bitcast arguments with enum argument to specify type 770b57cec5SDimitry Andric ClassL, // Logical instructions which are op instructions 780b57cec5SDimitry Andric // but we need to not emit any suffix for in our 790b57cec5SDimitry Andric // tests. 800b57cec5SDimitry Andric ClassNoTest // Instructions which we do not test since they are 810b57cec5SDimitry Andric // not TRUE instructions. 820b57cec5SDimitry Andric }; 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric /// NeonTypeFlags - Flags to identify the types for overloaded Neon 850b57cec5SDimitry Andric /// builtins. These must be kept in sync with the flags in 860b57cec5SDimitry Andric /// include/clang/Basic/TargetBuiltins.h. 870b57cec5SDimitry Andric namespace NeonTypeFlags { 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric enum { EltTypeMask = 0xf, UnsignedFlag = 0x10, QuadFlag = 0x20 }; 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric enum EltType { 920b57cec5SDimitry Andric Int8, 930b57cec5SDimitry Andric Int16, 940b57cec5SDimitry Andric Int32, 950b57cec5SDimitry Andric Int64, 960b57cec5SDimitry Andric Poly8, 970b57cec5SDimitry Andric Poly16, 980b57cec5SDimitry Andric Poly64, 990b57cec5SDimitry Andric Poly128, 1000b57cec5SDimitry Andric Float16, 1010b57cec5SDimitry Andric Float32, 102*5ffd83dbSDimitry Andric Float64, 103*5ffd83dbSDimitry Andric BFloat16 1040b57cec5SDimitry Andric }; 1050b57cec5SDimitry Andric 1060b57cec5SDimitry Andric } // end namespace NeonTypeFlags 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric class NeonEmitter; 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1110b57cec5SDimitry Andric // TypeSpec 1120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1130b57cec5SDimitry Andric 1140b57cec5SDimitry Andric /// A TypeSpec is just a simple wrapper around a string, but gets its own type 1150b57cec5SDimitry Andric /// for strong typing purposes. 1160b57cec5SDimitry Andric /// 1170b57cec5SDimitry Andric /// A TypeSpec can be used to create a type. 1180b57cec5SDimitry Andric class TypeSpec : public std::string { 1190b57cec5SDimitry Andric public: 1200b57cec5SDimitry Andric static std::vector<TypeSpec> fromTypeSpecs(StringRef Str) { 1210b57cec5SDimitry Andric std::vector<TypeSpec> Ret; 1220b57cec5SDimitry Andric TypeSpec Acc; 1230b57cec5SDimitry Andric for (char I : Str.str()) { 1240b57cec5SDimitry Andric if (islower(I)) { 1250b57cec5SDimitry Andric Acc.push_back(I); 1260b57cec5SDimitry Andric Ret.push_back(TypeSpec(Acc)); 1270b57cec5SDimitry Andric Acc.clear(); 1280b57cec5SDimitry Andric } else { 1290b57cec5SDimitry Andric Acc.push_back(I); 1300b57cec5SDimitry Andric } 1310b57cec5SDimitry Andric } 1320b57cec5SDimitry Andric return Ret; 1330b57cec5SDimitry Andric } 1340b57cec5SDimitry Andric }; 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1370b57cec5SDimitry Andric // Type 1380b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1390b57cec5SDimitry Andric 1400b57cec5SDimitry Andric /// A Type. Not much more to say here. 1410b57cec5SDimitry Andric class Type { 1420b57cec5SDimitry Andric private: 1430b57cec5SDimitry Andric TypeSpec TS; 1440b57cec5SDimitry Andric 145480093f4SDimitry Andric enum TypeKind { 146480093f4SDimitry Andric Void, 147480093f4SDimitry Andric Float, 148480093f4SDimitry Andric SInt, 149480093f4SDimitry Andric UInt, 150480093f4SDimitry Andric Poly, 151*5ffd83dbSDimitry Andric BFloat16, 152480093f4SDimitry Andric }; 153480093f4SDimitry Andric TypeKind Kind; 154480093f4SDimitry Andric bool Immediate, Constant, Pointer; 1550b57cec5SDimitry Andric // ScalarForMangling and NoManglingQ are really not suited to live here as 1560b57cec5SDimitry Andric // they are not related to the type. But they live in the TypeSpec (not the 1570b57cec5SDimitry Andric // prototype), so this is really the only place to store them. 1580b57cec5SDimitry Andric bool ScalarForMangling, NoManglingQ; 1590b57cec5SDimitry Andric unsigned Bitwidth, ElementBitwidth, NumVectors; 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric public: 1620b57cec5SDimitry Andric Type() 163480093f4SDimitry Andric : Kind(Void), Immediate(false), Constant(false), 164480093f4SDimitry Andric Pointer(false), ScalarForMangling(false), NoManglingQ(false), 165480093f4SDimitry Andric Bitwidth(0), ElementBitwidth(0), NumVectors(0) {} 1660b57cec5SDimitry Andric 167480093f4SDimitry Andric Type(TypeSpec TS, StringRef CharMods) 168480093f4SDimitry Andric : TS(std::move(TS)), Kind(Void), Immediate(false), 169480093f4SDimitry Andric Constant(false), Pointer(false), ScalarForMangling(false), 170480093f4SDimitry Andric NoManglingQ(false), Bitwidth(0), ElementBitwidth(0), NumVectors(0) { 171480093f4SDimitry Andric applyModifiers(CharMods); 1720b57cec5SDimitry Andric } 1730b57cec5SDimitry Andric 1740b57cec5SDimitry Andric /// Returns a type representing "void". 1750b57cec5SDimitry Andric static Type getVoid() { return Type(); } 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric bool operator==(const Type &Other) const { return str() == Other.str(); } 1780b57cec5SDimitry Andric bool operator!=(const Type &Other) const { return !operator==(Other); } 1790b57cec5SDimitry Andric 1800b57cec5SDimitry Andric // 1810b57cec5SDimitry Andric // Query functions 1820b57cec5SDimitry Andric // 1830b57cec5SDimitry Andric bool isScalarForMangling() const { return ScalarForMangling; } 1840b57cec5SDimitry Andric bool noManglingQ() const { return NoManglingQ; } 1850b57cec5SDimitry Andric 1860b57cec5SDimitry Andric bool isPointer() const { return Pointer; } 187480093f4SDimitry Andric bool isValue() const { return !isVoid() && !isPointer(); } 188480093f4SDimitry Andric bool isScalar() const { return isValue() && NumVectors == 0; } 189480093f4SDimitry Andric bool isVector() const { return isValue() && NumVectors > 0; } 190480093f4SDimitry Andric bool isConstPointer() const { return Constant; } 191480093f4SDimitry Andric bool isFloating() const { return Kind == Float; } 192480093f4SDimitry Andric bool isInteger() const { return Kind == SInt || Kind == UInt; } 193480093f4SDimitry Andric bool isPoly() const { return Kind == Poly; } 194480093f4SDimitry Andric bool isSigned() const { return Kind == SInt; } 1950b57cec5SDimitry Andric bool isImmediate() const { return Immediate; } 196480093f4SDimitry Andric bool isFloat() const { return isFloating() && ElementBitwidth == 32; } 197480093f4SDimitry Andric bool isDouble() const { return isFloating() && ElementBitwidth == 64; } 198480093f4SDimitry Andric bool isHalf() const { return isFloating() && ElementBitwidth == 16; } 1990b57cec5SDimitry Andric bool isChar() const { return ElementBitwidth == 8; } 200480093f4SDimitry Andric bool isShort() const { return isInteger() && ElementBitwidth == 16; } 201480093f4SDimitry Andric bool isInt() const { return isInteger() && ElementBitwidth == 32; } 202480093f4SDimitry Andric bool isLong() const { return isInteger() && ElementBitwidth == 64; } 203480093f4SDimitry Andric bool isVoid() const { return Kind == Void; } 204*5ffd83dbSDimitry Andric bool isBFloat16() const { return Kind == BFloat16; } 2050b57cec5SDimitry Andric unsigned getNumElements() const { return Bitwidth / ElementBitwidth; } 2060b57cec5SDimitry Andric unsigned getSizeInBits() const { return Bitwidth; } 2070b57cec5SDimitry Andric unsigned getElementSizeInBits() const { return ElementBitwidth; } 2080b57cec5SDimitry Andric unsigned getNumVectors() const { return NumVectors; } 2090b57cec5SDimitry Andric 2100b57cec5SDimitry Andric // 2110b57cec5SDimitry Andric // Mutator functions 2120b57cec5SDimitry Andric // 213480093f4SDimitry Andric void makeUnsigned() { 214480093f4SDimitry Andric assert(!isVoid() && "not a potentially signed type"); 215480093f4SDimitry Andric Kind = UInt; 216480093f4SDimitry Andric } 217480093f4SDimitry Andric void makeSigned() { 218480093f4SDimitry Andric assert(!isVoid() && "not a potentially signed type"); 219480093f4SDimitry Andric Kind = SInt; 220480093f4SDimitry Andric } 2210b57cec5SDimitry Andric 2220b57cec5SDimitry Andric void makeInteger(unsigned ElemWidth, bool Sign) { 223480093f4SDimitry Andric assert(!isVoid() && "converting void to int probably not useful"); 224480093f4SDimitry Andric Kind = Sign ? SInt : UInt; 2250b57cec5SDimitry Andric Immediate = false; 2260b57cec5SDimitry Andric ElementBitwidth = ElemWidth; 2270b57cec5SDimitry Andric } 2280b57cec5SDimitry Andric 2290b57cec5SDimitry Andric void makeImmediate(unsigned ElemWidth) { 230480093f4SDimitry Andric Kind = SInt; 2310b57cec5SDimitry Andric Immediate = true; 2320b57cec5SDimitry Andric ElementBitwidth = ElemWidth; 2330b57cec5SDimitry Andric } 2340b57cec5SDimitry Andric 2350b57cec5SDimitry Andric void makeScalar() { 2360b57cec5SDimitry Andric Bitwidth = ElementBitwidth; 2370b57cec5SDimitry Andric NumVectors = 0; 2380b57cec5SDimitry Andric } 2390b57cec5SDimitry Andric 2400b57cec5SDimitry Andric void makeOneVector() { 2410b57cec5SDimitry Andric assert(isVector()); 2420b57cec5SDimitry Andric NumVectors = 1; 2430b57cec5SDimitry Andric } 2440b57cec5SDimitry Andric 245*5ffd83dbSDimitry Andric void make32BitElement() { 246*5ffd83dbSDimitry Andric assert_with_loc(Bitwidth > 32, "Not enough bits to make it 32!"); 247*5ffd83dbSDimitry Andric ElementBitwidth = 32; 248*5ffd83dbSDimitry Andric } 249*5ffd83dbSDimitry Andric 2500b57cec5SDimitry Andric void doubleLanes() { 2510b57cec5SDimitry Andric assert_with_loc(Bitwidth != 128, "Can't get bigger than 128!"); 2520b57cec5SDimitry Andric Bitwidth = 128; 2530b57cec5SDimitry Andric } 2540b57cec5SDimitry Andric 2550b57cec5SDimitry Andric void halveLanes() { 2560b57cec5SDimitry Andric assert_with_loc(Bitwidth != 64, "Can't get smaller than 64!"); 2570b57cec5SDimitry Andric Bitwidth = 64; 2580b57cec5SDimitry Andric } 2590b57cec5SDimitry Andric 2600b57cec5SDimitry Andric /// Return the C string representation of a type, which is the typename 2610b57cec5SDimitry Andric /// defined in stdint.h or arm_neon.h. 2620b57cec5SDimitry Andric std::string str() const; 2630b57cec5SDimitry Andric 2640b57cec5SDimitry Andric /// Return the string representation of a type, which is an encoded 2650b57cec5SDimitry Andric /// string for passing to the BUILTIN() macro in Builtins.def. 2660b57cec5SDimitry Andric std::string builtin_str() const; 2670b57cec5SDimitry Andric 2680b57cec5SDimitry Andric /// Return the value in NeonTypeFlags for this type. 2690b57cec5SDimitry Andric unsigned getNeonEnum() const; 2700b57cec5SDimitry Andric 2710b57cec5SDimitry Andric /// Parse a type from a stdint.h or arm_neon.h typedef name, 2720b57cec5SDimitry Andric /// for example uint32x2_t or int64_t. 2730b57cec5SDimitry Andric static Type fromTypedefName(StringRef Name); 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric private: 2760b57cec5SDimitry Andric /// Creates the type based on the typespec string in TS. 2770b57cec5SDimitry Andric /// Sets "Quad" to true if the "Q" or "H" modifiers were 2780b57cec5SDimitry Andric /// seen. This is needed by applyModifier as some modifiers 2790b57cec5SDimitry Andric /// only take effect if the type size was changed by "Q" or "H". 2800b57cec5SDimitry Andric void applyTypespec(bool &Quad); 281480093f4SDimitry Andric /// Applies prototype modifiers to the type. 282480093f4SDimitry Andric void applyModifiers(StringRef Mods); 2830b57cec5SDimitry Andric }; 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 2860b57cec5SDimitry Andric // Variable 2870b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric /// A variable is a simple class that just has a type and a name. 2900b57cec5SDimitry Andric class Variable { 2910b57cec5SDimitry Andric Type T; 2920b57cec5SDimitry Andric std::string N; 2930b57cec5SDimitry Andric 2940b57cec5SDimitry Andric public: 2950b57cec5SDimitry Andric Variable() : T(Type::getVoid()), N("") {} 2960b57cec5SDimitry Andric Variable(Type T, std::string N) : T(std::move(T)), N(std::move(N)) {} 2970b57cec5SDimitry Andric 2980b57cec5SDimitry Andric Type getType() const { return T; } 2990b57cec5SDimitry Andric std::string getName() const { return "__" + N; } 3000b57cec5SDimitry Andric }; 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 3030b57cec5SDimitry Andric // Intrinsic 3040b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 3050b57cec5SDimitry Andric 3060b57cec5SDimitry Andric /// The main grunt class. This represents an instantiation of an intrinsic with 3070b57cec5SDimitry Andric /// a particular typespec and prototype. 3080b57cec5SDimitry Andric class Intrinsic { 3090b57cec5SDimitry Andric /// The Record this intrinsic was created from. 3100b57cec5SDimitry Andric Record *R; 311480093f4SDimitry Andric /// The unmangled name. 312480093f4SDimitry Andric std::string Name; 3130b57cec5SDimitry Andric /// The input and output typespecs. InTS == OutTS except when 314*5ffd83dbSDimitry Andric /// CartesianProductWith is non-empty - this is the case for vreinterpret. 3150b57cec5SDimitry Andric TypeSpec OutTS, InTS; 3160b57cec5SDimitry Andric /// The base class kind. Most intrinsics use ClassS, which has full type 3170b57cec5SDimitry Andric /// info for integers (s32/u32). Some use ClassI, which doesn't care about 3180b57cec5SDimitry Andric /// signedness (i32), while some (ClassB) have no type at all, only a width 3190b57cec5SDimitry Andric /// (32). 3200b57cec5SDimitry Andric ClassKind CK; 3210b57cec5SDimitry Andric /// The list of DAGs for the body. May be empty, in which case we should 3220b57cec5SDimitry Andric /// emit a builtin call. 3230b57cec5SDimitry Andric ListInit *Body; 3240b57cec5SDimitry Andric /// The architectural #ifdef guard. 3250b57cec5SDimitry Andric std::string Guard; 3260b57cec5SDimitry Andric /// Set if the Unavailable bit is 1. This means we don't generate a body, 3270b57cec5SDimitry Andric /// just an "unavailable" attribute on a declaration. 3280b57cec5SDimitry Andric bool IsUnavailable; 3290b57cec5SDimitry Andric /// Is this intrinsic safe for big-endian? or does it need its arguments 3300b57cec5SDimitry Andric /// reversing? 3310b57cec5SDimitry Andric bool BigEndianSafe; 3320b57cec5SDimitry Andric 3330b57cec5SDimitry Andric /// The types of return value [0] and parameters [1..]. 3340b57cec5SDimitry Andric std::vector<Type> Types; 335480093f4SDimitry Andric /// The index of the key type passed to CGBuiltin.cpp for polymorphic calls. 336480093f4SDimitry Andric int PolymorphicKeyType; 3370b57cec5SDimitry Andric /// The local variables defined. 3380b57cec5SDimitry Andric std::map<std::string, Variable> Variables; 3390b57cec5SDimitry Andric /// NeededEarly - set if any other intrinsic depends on this intrinsic. 3400b57cec5SDimitry Andric bool NeededEarly; 3410b57cec5SDimitry Andric /// UseMacro - set if we should implement using a macro or unset for a 3420b57cec5SDimitry Andric /// function. 3430b57cec5SDimitry Andric bool UseMacro; 3440b57cec5SDimitry Andric /// The set of intrinsics that this intrinsic uses/requires. 3450b57cec5SDimitry Andric std::set<Intrinsic *> Dependencies; 3460b57cec5SDimitry Andric /// The "base type", which is Type('d', OutTS). InBaseType is only 347*5ffd83dbSDimitry Andric /// different if CartesianProductWith is non-empty (for vreinterpret). 3480b57cec5SDimitry Andric Type BaseType, InBaseType; 3490b57cec5SDimitry Andric /// The return variable. 3500b57cec5SDimitry Andric Variable RetVar; 3510b57cec5SDimitry Andric /// A postfix to apply to every variable. Defaults to "". 3520b57cec5SDimitry Andric std::string VariablePostfix; 3530b57cec5SDimitry Andric 3540b57cec5SDimitry Andric NeonEmitter &Emitter; 3550b57cec5SDimitry Andric std::stringstream OS; 3560b57cec5SDimitry Andric 357a7dea167SDimitry Andric bool isBigEndianSafe() const { 358a7dea167SDimitry Andric if (BigEndianSafe) 359a7dea167SDimitry Andric return true; 360a7dea167SDimitry Andric 361a7dea167SDimitry Andric for (const auto &T : Types){ 362a7dea167SDimitry Andric if (T.isVector() && T.getNumElements() > 1) 363a7dea167SDimitry Andric return false; 364a7dea167SDimitry Andric } 365a7dea167SDimitry Andric return true; 366a7dea167SDimitry Andric } 367a7dea167SDimitry Andric 3680b57cec5SDimitry Andric public: 3690b57cec5SDimitry Andric Intrinsic(Record *R, StringRef Name, StringRef Proto, TypeSpec OutTS, 3700b57cec5SDimitry Andric TypeSpec InTS, ClassKind CK, ListInit *Body, NeonEmitter &Emitter, 3710b57cec5SDimitry Andric StringRef Guard, bool IsUnavailable, bool BigEndianSafe) 372480093f4SDimitry Andric : R(R), Name(Name.str()), OutTS(OutTS), InTS(InTS), CK(CK), Body(Body), 373480093f4SDimitry Andric Guard(Guard.str()), IsUnavailable(IsUnavailable), 374480093f4SDimitry Andric BigEndianSafe(BigEndianSafe), PolymorphicKeyType(0), NeededEarly(false), 375480093f4SDimitry Andric UseMacro(false), BaseType(OutTS, "."), InBaseType(InTS, "."), 376480093f4SDimitry Andric Emitter(Emitter) { 3770b57cec5SDimitry Andric // Modify the TypeSpec per-argument to get a concrete Type, and create 3780b57cec5SDimitry Andric // known variables for each. 3790b57cec5SDimitry Andric // Types[0] is the return value. 380480093f4SDimitry Andric unsigned Pos = 0; 381480093f4SDimitry Andric Types.emplace_back(OutTS, getNextModifiers(Proto, Pos)); 382480093f4SDimitry Andric StringRef Mods = getNextModifiers(Proto, Pos); 383480093f4SDimitry Andric while (!Mods.empty()) { 384480093f4SDimitry Andric Types.emplace_back(InTS, Mods); 385480093f4SDimitry Andric if (Mods.find("!") != StringRef::npos) 386480093f4SDimitry Andric PolymorphicKeyType = Types.size() - 1; 387480093f4SDimitry Andric 388480093f4SDimitry Andric Mods = getNextModifiers(Proto, Pos); 389480093f4SDimitry Andric } 390480093f4SDimitry Andric 391480093f4SDimitry Andric for (auto Type : Types) { 392480093f4SDimitry Andric // If this builtin takes an immediate argument, we need to #define it rather 393480093f4SDimitry Andric // than use a standard declaration, so that SemaChecking can range check 394480093f4SDimitry Andric // the immediate passed by the user. 395480093f4SDimitry Andric 396480093f4SDimitry Andric // Pointer arguments need to use macros to avoid hiding aligned attributes 397480093f4SDimitry Andric // from the pointer type. 398480093f4SDimitry Andric 399480093f4SDimitry Andric // It is not permitted to pass or return an __fp16 by value, so intrinsics 400480093f4SDimitry Andric // taking a scalar float16_t must be implemented as macros. 401480093f4SDimitry Andric if (Type.isImmediate() || Type.isPointer() || 402480093f4SDimitry Andric (Type.isScalar() && Type.isHalf())) 403480093f4SDimitry Andric UseMacro = true; 404480093f4SDimitry Andric } 4050b57cec5SDimitry Andric } 4060b57cec5SDimitry Andric 4070b57cec5SDimitry Andric /// Get the Record that this intrinsic is based off. 4080b57cec5SDimitry Andric Record *getRecord() const { return R; } 4090b57cec5SDimitry Andric /// Get the set of Intrinsics that this intrinsic calls. 4100b57cec5SDimitry Andric /// this is the set of immediate dependencies, NOT the 4110b57cec5SDimitry Andric /// transitive closure. 4120b57cec5SDimitry Andric const std::set<Intrinsic *> &getDependencies() const { return Dependencies; } 4130b57cec5SDimitry Andric /// Get the architectural guard string (#ifdef). 4140b57cec5SDimitry Andric std::string getGuard() const { return Guard; } 4150b57cec5SDimitry Andric /// Get the non-mangled name. 4160b57cec5SDimitry Andric std::string getName() const { return Name; } 4170b57cec5SDimitry Andric 4180b57cec5SDimitry Andric /// Return true if the intrinsic takes an immediate operand. 4190b57cec5SDimitry Andric bool hasImmediate() const { 420480093f4SDimitry Andric return std::any_of(Types.begin(), Types.end(), 421480093f4SDimitry Andric [](const Type &T) { return T.isImmediate(); }); 4220b57cec5SDimitry Andric } 4230b57cec5SDimitry Andric 4240b57cec5SDimitry Andric /// Return the parameter index of the immediate operand. 4250b57cec5SDimitry Andric unsigned getImmediateIdx() const { 426480093f4SDimitry Andric for (unsigned Idx = 0; Idx < Types.size(); ++Idx) 427480093f4SDimitry Andric if (Types[Idx].isImmediate()) 4280b57cec5SDimitry Andric return Idx - 1; 429480093f4SDimitry Andric llvm_unreachable("Intrinsic has no immediate"); 4300b57cec5SDimitry Andric } 4310b57cec5SDimitry Andric 4320b57cec5SDimitry Andric 433480093f4SDimitry Andric unsigned getNumParams() const { return Types.size() - 1; } 4340b57cec5SDimitry Andric Type getReturnType() const { return Types[0]; } 4350b57cec5SDimitry Andric Type getParamType(unsigned I) const { return Types[I + 1]; } 4360b57cec5SDimitry Andric Type getBaseType() const { return BaseType; } 437480093f4SDimitry Andric Type getPolymorphicKeyType() const { return Types[PolymorphicKeyType]; } 4380b57cec5SDimitry Andric 4390b57cec5SDimitry Andric /// Return true if the prototype has a scalar argument. 4400b57cec5SDimitry Andric bool protoHasScalar() const; 4410b57cec5SDimitry Andric 4420b57cec5SDimitry Andric /// Return the index that parameter PIndex will sit at 4430b57cec5SDimitry Andric /// in a generated function call. This is often just PIndex, 4440b57cec5SDimitry Andric /// but may not be as things such as multiple-vector operands 4450b57cec5SDimitry Andric /// and sret parameters need to be taken into accont. 4460b57cec5SDimitry Andric unsigned getGeneratedParamIdx(unsigned PIndex) { 4470b57cec5SDimitry Andric unsigned Idx = 0; 4480b57cec5SDimitry Andric if (getReturnType().getNumVectors() > 1) 4490b57cec5SDimitry Andric // Multiple vectors are passed as sret. 4500b57cec5SDimitry Andric ++Idx; 4510b57cec5SDimitry Andric 4520b57cec5SDimitry Andric for (unsigned I = 0; I < PIndex; ++I) 4530b57cec5SDimitry Andric Idx += std::max(1U, getParamType(I).getNumVectors()); 4540b57cec5SDimitry Andric 4550b57cec5SDimitry Andric return Idx; 4560b57cec5SDimitry Andric } 4570b57cec5SDimitry Andric 4580b57cec5SDimitry Andric bool hasBody() const { return Body && !Body->getValues().empty(); } 4590b57cec5SDimitry Andric 4600b57cec5SDimitry Andric void setNeededEarly() { NeededEarly = true; } 4610b57cec5SDimitry Andric 4620b57cec5SDimitry Andric bool operator<(const Intrinsic &Other) const { 4630b57cec5SDimitry Andric // Sort lexicographically on a two-tuple (Guard, Name) 4640b57cec5SDimitry Andric if (Guard != Other.Guard) 4650b57cec5SDimitry Andric return Guard < Other.Guard; 4660b57cec5SDimitry Andric return Name < Other.Name; 4670b57cec5SDimitry Andric } 4680b57cec5SDimitry Andric 4690b57cec5SDimitry Andric ClassKind getClassKind(bool UseClassBIfScalar = false) { 4700b57cec5SDimitry Andric if (UseClassBIfScalar && !protoHasScalar()) 4710b57cec5SDimitry Andric return ClassB; 4720b57cec5SDimitry Andric return CK; 4730b57cec5SDimitry Andric } 4740b57cec5SDimitry Andric 4750b57cec5SDimitry Andric /// Return the name, mangled with type information. 4760b57cec5SDimitry Andric /// If ForceClassS is true, use ClassS (u32/s32) instead 4770b57cec5SDimitry Andric /// of the intrinsic's own type class. 4780b57cec5SDimitry Andric std::string getMangledName(bool ForceClassS = false) const; 4790b57cec5SDimitry Andric /// Return the type code for a builtin function call. 4800b57cec5SDimitry Andric std::string getInstTypeCode(Type T, ClassKind CK) const; 4810b57cec5SDimitry Andric /// Return the type string for a BUILTIN() macro in Builtins.def. 4820b57cec5SDimitry Andric std::string getBuiltinTypeStr(); 4830b57cec5SDimitry Andric 4840b57cec5SDimitry Andric /// Generate the intrinsic, returning code. 4850b57cec5SDimitry Andric std::string generate(); 4860b57cec5SDimitry Andric /// Perform type checking and populate the dependency graph, but 4870b57cec5SDimitry Andric /// don't generate code yet. 4880b57cec5SDimitry Andric void indexBody(); 4890b57cec5SDimitry Andric 4900b57cec5SDimitry Andric private: 491480093f4SDimitry Andric StringRef getNextModifiers(StringRef Proto, unsigned &Pos) const; 492480093f4SDimitry Andric 4930b57cec5SDimitry Andric std::string mangleName(std::string Name, ClassKind CK) const; 4940b57cec5SDimitry Andric 4950b57cec5SDimitry Andric void initVariables(); 4960b57cec5SDimitry Andric std::string replaceParamsIn(std::string S); 4970b57cec5SDimitry Andric 4980b57cec5SDimitry Andric void emitBodyAsBuiltinCall(); 4990b57cec5SDimitry Andric 5000b57cec5SDimitry Andric void generateImpl(bool ReverseArguments, 5010b57cec5SDimitry Andric StringRef NamePrefix, StringRef CallPrefix); 5020b57cec5SDimitry Andric void emitReturn(); 5030b57cec5SDimitry Andric void emitBody(StringRef CallPrefix); 5040b57cec5SDimitry Andric void emitShadowedArgs(); 5050b57cec5SDimitry Andric void emitArgumentReversal(); 5060b57cec5SDimitry Andric void emitReturnReversal(); 5070b57cec5SDimitry Andric void emitReverseVariable(Variable &Dest, Variable &Src); 5080b57cec5SDimitry Andric void emitNewLine(); 5090b57cec5SDimitry Andric void emitClosingBrace(); 5100b57cec5SDimitry Andric void emitOpeningBrace(); 5110b57cec5SDimitry Andric void emitPrototype(StringRef NamePrefix); 5120b57cec5SDimitry Andric 5130b57cec5SDimitry Andric class DagEmitter { 5140b57cec5SDimitry Andric Intrinsic &Intr; 5150b57cec5SDimitry Andric StringRef CallPrefix; 5160b57cec5SDimitry Andric 5170b57cec5SDimitry Andric public: 5180b57cec5SDimitry Andric DagEmitter(Intrinsic &Intr, StringRef CallPrefix) : 5190b57cec5SDimitry Andric Intr(Intr), CallPrefix(CallPrefix) { 5200b57cec5SDimitry Andric } 5210b57cec5SDimitry Andric std::pair<Type, std::string> emitDagArg(Init *Arg, std::string ArgName); 5220b57cec5SDimitry Andric std::pair<Type, std::string> emitDagSaveTemp(DagInit *DI); 5230b57cec5SDimitry Andric std::pair<Type, std::string> emitDagSplat(DagInit *DI); 5240b57cec5SDimitry Andric std::pair<Type, std::string> emitDagDup(DagInit *DI); 5250b57cec5SDimitry Andric std::pair<Type, std::string> emitDagDupTyped(DagInit *DI); 5260b57cec5SDimitry Andric std::pair<Type, std::string> emitDagShuffle(DagInit *DI); 5270b57cec5SDimitry Andric std::pair<Type, std::string> emitDagCast(DagInit *DI, bool IsBitCast); 528*5ffd83dbSDimitry Andric std::pair<Type, std::string> emitDagCall(DagInit *DI, 529*5ffd83dbSDimitry Andric bool MatchMangledName); 5300b57cec5SDimitry Andric std::pair<Type, std::string> emitDagNameReplace(DagInit *DI); 5310b57cec5SDimitry Andric std::pair<Type, std::string> emitDagLiteral(DagInit *DI); 5320b57cec5SDimitry Andric std::pair<Type, std::string> emitDagOp(DagInit *DI); 5330b57cec5SDimitry Andric std::pair<Type, std::string> emitDag(DagInit *DI); 5340b57cec5SDimitry Andric }; 5350b57cec5SDimitry Andric }; 5360b57cec5SDimitry Andric 5370b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 5380b57cec5SDimitry Andric // NeonEmitter 5390b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 5400b57cec5SDimitry Andric 5410b57cec5SDimitry Andric class NeonEmitter { 5420b57cec5SDimitry Andric RecordKeeper &Records; 5430b57cec5SDimitry Andric DenseMap<Record *, ClassKind> ClassMap; 5440b57cec5SDimitry Andric std::map<std::string, std::deque<Intrinsic>> IntrinsicMap; 5450b57cec5SDimitry Andric unsigned UniqueNumber; 5460b57cec5SDimitry Andric 5470b57cec5SDimitry Andric void createIntrinsic(Record *R, SmallVectorImpl<Intrinsic *> &Out); 5480b57cec5SDimitry Andric void genBuiltinsDef(raw_ostream &OS, SmallVectorImpl<Intrinsic *> &Defs); 5490b57cec5SDimitry Andric void genOverloadTypeCheckCode(raw_ostream &OS, 5500b57cec5SDimitry Andric SmallVectorImpl<Intrinsic *> &Defs); 5510b57cec5SDimitry Andric void genIntrinsicRangeCheckCode(raw_ostream &OS, 5520b57cec5SDimitry Andric SmallVectorImpl<Intrinsic *> &Defs); 5530b57cec5SDimitry Andric 5540b57cec5SDimitry Andric public: 5550b57cec5SDimitry Andric /// Called by Intrinsic - this attempts to get an intrinsic that takes 5560b57cec5SDimitry Andric /// the given types as arguments. 557*5ffd83dbSDimitry Andric Intrinsic &getIntrinsic(StringRef Name, ArrayRef<Type> Types, 558*5ffd83dbSDimitry Andric Optional<std::string> MangledName); 5590b57cec5SDimitry Andric 5600b57cec5SDimitry Andric /// Called by Intrinsic - returns a globally-unique number. 5610b57cec5SDimitry Andric unsigned getUniqueNumber() { return UniqueNumber++; } 5620b57cec5SDimitry Andric 5630b57cec5SDimitry Andric NeonEmitter(RecordKeeper &R) : Records(R), UniqueNumber(0) { 5640b57cec5SDimitry Andric Record *SI = R.getClass("SInst"); 5650b57cec5SDimitry Andric Record *II = R.getClass("IInst"); 5660b57cec5SDimitry Andric Record *WI = R.getClass("WInst"); 5670b57cec5SDimitry Andric Record *SOpI = R.getClass("SOpInst"); 5680b57cec5SDimitry Andric Record *IOpI = R.getClass("IOpInst"); 5690b57cec5SDimitry Andric Record *WOpI = R.getClass("WOpInst"); 5700b57cec5SDimitry Andric Record *LOpI = R.getClass("LOpInst"); 5710b57cec5SDimitry Andric Record *NoTestOpI = R.getClass("NoTestOpInst"); 5720b57cec5SDimitry Andric 5730b57cec5SDimitry Andric ClassMap[SI] = ClassS; 5740b57cec5SDimitry Andric ClassMap[II] = ClassI; 5750b57cec5SDimitry Andric ClassMap[WI] = ClassW; 5760b57cec5SDimitry Andric ClassMap[SOpI] = ClassS; 5770b57cec5SDimitry Andric ClassMap[IOpI] = ClassI; 5780b57cec5SDimitry Andric ClassMap[WOpI] = ClassW; 5790b57cec5SDimitry Andric ClassMap[LOpI] = ClassL; 5800b57cec5SDimitry Andric ClassMap[NoTestOpI] = ClassNoTest; 5810b57cec5SDimitry Andric } 5820b57cec5SDimitry Andric 5830b57cec5SDimitry Andric // run - Emit arm_neon.h.inc 5840b57cec5SDimitry Andric void run(raw_ostream &o); 5850b57cec5SDimitry Andric 5860b57cec5SDimitry Andric // runFP16 - Emit arm_fp16.h.inc 5870b57cec5SDimitry Andric void runFP16(raw_ostream &o); 5880b57cec5SDimitry Andric 589*5ffd83dbSDimitry Andric // runBF16 - Emit arm_bf16.h.inc 590*5ffd83dbSDimitry Andric void runBF16(raw_ostream &o); 591*5ffd83dbSDimitry Andric 592*5ffd83dbSDimitry Andric // runHeader - Emit all the __builtin prototypes used in arm_neon.h, 593*5ffd83dbSDimitry Andric // arm_fp16.h and arm_bf16.h 5940b57cec5SDimitry Andric void runHeader(raw_ostream &o); 5950b57cec5SDimitry Andric 5960b57cec5SDimitry Andric // runTests - Emit tests for all the Neon intrinsics. 5970b57cec5SDimitry Andric void runTests(raw_ostream &o); 5980b57cec5SDimitry Andric }; 5990b57cec5SDimitry Andric 6000b57cec5SDimitry Andric } // end anonymous namespace 6010b57cec5SDimitry Andric 6020b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 6030b57cec5SDimitry Andric // Type implementation 6040b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 6050b57cec5SDimitry Andric 6060b57cec5SDimitry Andric std::string Type::str() const { 607480093f4SDimitry Andric if (isVoid()) 6080b57cec5SDimitry Andric return "void"; 6090b57cec5SDimitry Andric std::string S; 6100b57cec5SDimitry Andric 611480093f4SDimitry Andric if (isInteger() && !isSigned()) 6120b57cec5SDimitry Andric S += "u"; 6130b57cec5SDimitry Andric 614480093f4SDimitry Andric if (isPoly()) 6150b57cec5SDimitry Andric S += "poly"; 616480093f4SDimitry Andric else if (isFloating()) 6170b57cec5SDimitry Andric S += "float"; 618*5ffd83dbSDimitry Andric else if (isBFloat16()) 619*5ffd83dbSDimitry Andric S += "bfloat"; 6200b57cec5SDimitry Andric else 6210b57cec5SDimitry Andric S += "int"; 6220b57cec5SDimitry Andric 6230b57cec5SDimitry Andric S += utostr(ElementBitwidth); 6240b57cec5SDimitry Andric if (isVector()) 6250b57cec5SDimitry Andric S += "x" + utostr(getNumElements()); 6260b57cec5SDimitry Andric if (NumVectors > 1) 6270b57cec5SDimitry Andric S += "x" + utostr(NumVectors); 6280b57cec5SDimitry Andric S += "_t"; 6290b57cec5SDimitry Andric 6300b57cec5SDimitry Andric if (Constant) 6310b57cec5SDimitry Andric S += " const"; 6320b57cec5SDimitry Andric if (Pointer) 6330b57cec5SDimitry Andric S += " *"; 6340b57cec5SDimitry Andric 6350b57cec5SDimitry Andric return S; 6360b57cec5SDimitry Andric } 6370b57cec5SDimitry Andric 6380b57cec5SDimitry Andric std::string Type::builtin_str() const { 6390b57cec5SDimitry Andric std::string S; 6400b57cec5SDimitry Andric if (isVoid()) 6410b57cec5SDimitry Andric return "v"; 6420b57cec5SDimitry Andric 643480093f4SDimitry Andric if (isPointer()) { 6440b57cec5SDimitry Andric // All pointers are void pointers. 645480093f4SDimitry Andric S = "v"; 646480093f4SDimitry Andric if (isConstPointer()) 647480093f4SDimitry Andric S += "C"; 648480093f4SDimitry Andric S += "*"; 649480093f4SDimitry Andric return S; 650480093f4SDimitry Andric } else if (isInteger()) 6510b57cec5SDimitry Andric switch (ElementBitwidth) { 6520b57cec5SDimitry Andric case 8: S += "c"; break; 6530b57cec5SDimitry Andric case 16: S += "s"; break; 6540b57cec5SDimitry Andric case 32: S += "i"; break; 6550b57cec5SDimitry Andric case 64: S += "Wi"; break; 6560b57cec5SDimitry Andric case 128: S += "LLLi"; break; 6570b57cec5SDimitry Andric default: llvm_unreachable("Unhandled case!"); 6580b57cec5SDimitry Andric } 659*5ffd83dbSDimitry Andric else if (isBFloat16()) { 660*5ffd83dbSDimitry Andric assert(ElementBitwidth == 16 && "BFloat16 can only be 16 bits"); 661*5ffd83dbSDimitry Andric S += "y"; 662*5ffd83dbSDimitry Andric } else 6630b57cec5SDimitry Andric switch (ElementBitwidth) { 6640b57cec5SDimitry Andric case 16: S += "h"; break; 6650b57cec5SDimitry Andric case 32: S += "f"; break; 6660b57cec5SDimitry Andric case 64: S += "d"; break; 6670b57cec5SDimitry Andric default: llvm_unreachable("Unhandled case!"); 6680b57cec5SDimitry Andric } 6690b57cec5SDimitry Andric 670480093f4SDimitry Andric // FIXME: NECESSARY??????????????????????????????????????????????????????????????????????? 671480093f4SDimitry Andric if (isChar() && !isPointer() && isSigned()) 6720b57cec5SDimitry Andric // Make chars explicitly signed. 6730b57cec5SDimitry Andric S = "S" + S; 674480093f4SDimitry Andric else if (isInteger() && !isSigned()) 6750b57cec5SDimitry Andric S = "U" + S; 6760b57cec5SDimitry Andric 6770b57cec5SDimitry Andric // Constant indices are "int", but have the "constant expression" modifier. 6780b57cec5SDimitry Andric if (isImmediate()) { 6790b57cec5SDimitry Andric assert(isInteger() && isSigned()); 6800b57cec5SDimitry Andric S = "I" + S; 6810b57cec5SDimitry Andric } 6820b57cec5SDimitry Andric 683480093f4SDimitry Andric if (isScalar()) 6840b57cec5SDimitry Andric return S; 6850b57cec5SDimitry Andric 6860b57cec5SDimitry Andric std::string Ret; 6870b57cec5SDimitry Andric for (unsigned I = 0; I < NumVectors; ++I) 6880b57cec5SDimitry Andric Ret += "V" + utostr(getNumElements()) + S; 6890b57cec5SDimitry Andric 6900b57cec5SDimitry Andric return Ret; 6910b57cec5SDimitry Andric } 6920b57cec5SDimitry Andric 6930b57cec5SDimitry Andric unsigned Type::getNeonEnum() const { 6940b57cec5SDimitry Andric unsigned Addend; 6950b57cec5SDimitry Andric switch (ElementBitwidth) { 6960b57cec5SDimitry Andric case 8: Addend = 0; break; 6970b57cec5SDimitry Andric case 16: Addend = 1; break; 6980b57cec5SDimitry Andric case 32: Addend = 2; break; 6990b57cec5SDimitry Andric case 64: Addend = 3; break; 7000b57cec5SDimitry Andric case 128: Addend = 4; break; 7010b57cec5SDimitry Andric default: llvm_unreachable("Unhandled element bitwidth!"); 7020b57cec5SDimitry Andric } 7030b57cec5SDimitry Andric 7040b57cec5SDimitry Andric unsigned Base = (unsigned)NeonTypeFlags::Int8 + Addend; 705480093f4SDimitry Andric if (isPoly()) { 7060b57cec5SDimitry Andric // Adjustment needed because Poly32 doesn't exist. 7070b57cec5SDimitry Andric if (Addend >= 2) 7080b57cec5SDimitry Andric --Addend; 7090b57cec5SDimitry Andric Base = (unsigned)NeonTypeFlags::Poly8 + Addend; 7100b57cec5SDimitry Andric } 711480093f4SDimitry Andric if (isFloating()) { 7120b57cec5SDimitry Andric assert(Addend != 0 && "Float8 doesn't exist!"); 7130b57cec5SDimitry Andric Base = (unsigned)NeonTypeFlags::Float16 + (Addend - 1); 7140b57cec5SDimitry Andric } 7150b57cec5SDimitry Andric 716*5ffd83dbSDimitry Andric if (isBFloat16()) { 717*5ffd83dbSDimitry Andric assert(Addend == 1 && "BFloat16 is only 16 bit"); 718*5ffd83dbSDimitry Andric Base = (unsigned)NeonTypeFlags::BFloat16; 719*5ffd83dbSDimitry Andric } 720*5ffd83dbSDimitry Andric 7210b57cec5SDimitry Andric if (Bitwidth == 128) 7220b57cec5SDimitry Andric Base |= (unsigned)NeonTypeFlags::QuadFlag; 723480093f4SDimitry Andric if (isInteger() && !isSigned()) 7240b57cec5SDimitry Andric Base |= (unsigned)NeonTypeFlags::UnsignedFlag; 7250b57cec5SDimitry Andric 7260b57cec5SDimitry Andric return Base; 7270b57cec5SDimitry Andric } 7280b57cec5SDimitry Andric 7290b57cec5SDimitry Andric Type Type::fromTypedefName(StringRef Name) { 7300b57cec5SDimitry Andric Type T; 731480093f4SDimitry Andric T.Kind = SInt; 7320b57cec5SDimitry Andric 7330b57cec5SDimitry Andric if (Name.front() == 'u') { 734480093f4SDimitry Andric T.Kind = UInt; 7350b57cec5SDimitry Andric Name = Name.drop_front(); 7360b57cec5SDimitry Andric } 7370b57cec5SDimitry Andric 7380b57cec5SDimitry Andric if (Name.startswith("float")) { 739480093f4SDimitry Andric T.Kind = Float; 7400b57cec5SDimitry Andric Name = Name.drop_front(5); 7410b57cec5SDimitry Andric } else if (Name.startswith("poly")) { 742480093f4SDimitry Andric T.Kind = Poly; 7430b57cec5SDimitry Andric Name = Name.drop_front(4); 744*5ffd83dbSDimitry Andric } else if (Name.startswith("bfloat")) { 745*5ffd83dbSDimitry Andric T.Kind = BFloat16; 746*5ffd83dbSDimitry Andric Name = Name.drop_front(6); 7470b57cec5SDimitry Andric } else { 7480b57cec5SDimitry Andric assert(Name.startswith("int")); 7490b57cec5SDimitry Andric Name = Name.drop_front(3); 7500b57cec5SDimitry Andric } 7510b57cec5SDimitry Andric 7520b57cec5SDimitry Andric unsigned I = 0; 7530b57cec5SDimitry Andric for (I = 0; I < Name.size(); ++I) { 7540b57cec5SDimitry Andric if (!isdigit(Name[I])) 7550b57cec5SDimitry Andric break; 7560b57cec5SDimitry Andric } 7570b57cec5SDimitry Andric Name.substr(0, I).getAsInteger(10, T.ElementBitwidth); 7580b57cec5SDimitry Andric Name = Name.drop_front(I); 7590b57cec5SDimitry Andric 7600b57cec5SDimitry Andric T.Bitwidth = T.ElementBitwidth; 7610b57cec5SDimitry Andric T.NumVectors = 1; 7620b57cec5SDimitry Andric 7630b57cec5SDimitry Andric if (Name.front() == 'x') { 7640b57cec5SDimitry Andric Name = Name.drop_front(); 7650b57cec5SDimitry Andric unsigned I = 0; 7660b57cec5SDimitry Andric for (I = 0; I < Name.size(); ++I) { 7670b57cec5SDimitry Andric if (!isdigit(Name[I])) 7680b57cec5SDimitry Andric break; 7690b57cec5SDimitry Andric } 7700b57cec5SDimitry Andric unsigned NumLanes; 7710b57cec5SDimitry Andric Name.substr(0, I).getAsInteger(10, NumLanes); 7720b57cec5SDimitry Andric Name = Name.drop_front(I); 7730b57cec5SDimitry Andric T.Bitwidth = T.ElementBitwidth * NumLanes; 7740b57cec5SDimitry Andric } else { 7750b57cec5SDimitry Andric // Was scalar. 7760b57cec5SDimitry Andric T.NumVectors = 0; 7770b57cec5SDimitry Andric } 7780b57cec5SDimitry Andric if (Name.front() == 'x') { 7790b57cec5SDimitry Andric Name = Name.drop_front(); 7800b57cec5SDimitry Andric unsigned I = 0; 7810b57cec5SDimitry Andric for (I = 0; I < Name.size(); ++I) { 7820b57cec5SDimitry Andric if (!isdigit(Name[I])) 7830b57cec5SDimitry Andric break; 7840b57cec5SDimitry Andric } 7850b57cec5SDimitry Andric Name.substr(0, I).getAsInteger(10, T.NumVectors); 7860b57cec5SDimitry Andric Name = Name.drop_front(I); 7870b57cec5SDimitry Andric } 7880b57cec5SDimitry Andric 7890b57cec5SDimitry Andric assert(Name.startswith("_t") && "Malformed typedef!"); 7900b57cec5SDimitry Andric return T; 7910b57cec5SDimitry Andric } 7920b57cec5SDimitry Andric 7930b57cec5SDimitry Andric void Type::applyTypespec(bool &Quad) { 7940b57cec5SDimitry Andric std::string S = TS; 7950b57cec5SDimitry Andric ScalarForMangling = false; 796480093f4SDimitry Andric Kind = SInt; 7970b57cec5SDimitry Andric ElementBitwidth = ~0U; 7980b57cec5SDimitry Andric NumVectors = 1; 7990b57cec5SDimitry Andric 8000b57cec5SDimitry Andric for (char I : S) { 8010b57cec5SDimitry Andric switch (I) { 8020b57cec5SDimitry Andric case 'S': 8030b57cec5SDimitry Andric ScalarForMangling = true; 8040b57cec5SDimitry Andric break; 8050b57cec5SDimitry Andric case 'H': 8060b57cec5SDimitry Andric NoManglingQ = true; 8070b57cec5SDimitry Andric Quad = true; 8080b57cec5SDimitry Andric break; 8090b57cec5SDimitry Andric case 'Q': 8100b57cec5SDimitry Andric Quad = true; 8110b57cec5SDimitry Andric break; 8120b57cec5SDimitry Andric case 'P': 813480093f4SDimitry Andric Kind = Poly; 8140b57cec5SDimitry Andric break; 8150b57cec5SDimitry Andric case 'U': 816480093f4SDimitry Andric Kind = UInt; 8170b57cec5SDimitry Andric break; 8180b57cec5SDimitry Andric case 'c': 8190b57cec5SDimitry Andric ElementBitwidth = 8; 8200b57cec5SDimitry Andric break; 8210b57cec5SDimitry Andric case 'h': 822480093f4SDimitry Andric Kind = Float; 8230b57cec5SDimitry Andric LLVM_FALLTHROUGH; 8240b57cec5SDimitry Andric case 's': 8250b57cec5SDimitry Andric ElementBitwidth = 16; 8260b57cec5SDimitry Andric break; 8270b57cec5SDimitry Andric case 'f': 828480093f4SDimitry Andric Kind = Float; 8290b57cec5SDimitry Andric LLVM_FALLTHROUGH; 8300b57cec5SDimitry Andric case 'i': 8310b57cec5SDimitry Andric ElementBitwidth = 32; 8320b57cec5SDimitry Andric break; 8330b57cec5SDimitry Andric case 'd': 834480093f4SDimitry Andric Kind = Float; 8350b57cec5SDimitry Andric LLVM_FALLTHROUGH; 8360b57cec5SDimitry Andric case 'l': 8370b57cec5SDimitry Andric ElementBitwidth = 64; 8380b57cec5SDimitry Andric break; 8390b57cec5SDimitry Andric case 'k': 8400b57cec5SDimitry Andric ElementBitwidth = 128; 8410b57cec5SDimitry Andric // Poly doesn't have a 128x1 type. 842480093f4SDimitry Andric if (isPoly()) 8430b57cec5SDimitry Andric NumVectors = 0; 8440b57cec5SDimitry Andric break; 845*5ffd83dbSDimitry Andric case 'b': 846*5ffd83dbSDimitry Andric Kind = BFloat16; 847*5ffd83dbSDimitry Andric ElementBitwidth = 16; 848*5ffd83dbSDimitry Andric break; 8490b57cec5SDimitry Andric default: 8500b57cec5SDimitry Andric llvm_unreachable("Unhandled type code!"); 8510b57cec5SDimitry Andric } 8520b57cec5SDimitry Andric } 8530b57cec5SDimitry Andric assert(ElementBitwidth != ~0U && "Bad element bitwidth!"); 8540b57cec5SDimitry Andric 8550b57cec5SDimitry Andric Bitwidth = Quad ? 128 : 64; 8560b57cec5SDimitry Andric } 8570b57cec5SDimitry Andric 858480093f4SDimitry Andric void Type::applyModifiers(StringRef Mods) { 8590b57cec5SDimitry Andric bool AppliedQuad = false; 8600b57cec5SDimitry Andric applyTypespec(AppliedQuad); 8610b57cec5SDimitry Andric 862480093f4SDimitry Andric for (char Mod : Mods) { 8630b57cec5SDimitry Andric switch (Mod) { 864480093f4SDimitry Andric case '.': 865480093f4SDimitry Andric break; 8660b57cec5SDimitry Andric case 'v': 867480093f4SDimitry Andric Kind = Void; 8680b57cec5SDimitry Andric break; 869480093f4SDimitry Andric case 'S': 870480093f4SDimitry Andric Kind = SInt; 8710b57cec5SDimitry Andric break; 8720b57cec5SDimitry Andric case 'U': 873480093f4SDimitry Andric Kind = UInt; 8740b57cec5SDimitry Andric break; 875*5ffd83dbSDimitry Andric case 'B': 876*5ffd83dbSDimitry Andric Kind = BFloat16; 877*5ffd83dbSDimitry Andric ElementBitwidth = 16; 878*5ffd83dbSDimitry Andric break; 8790b57cec5SDimitry Andric case 'F': 880480093f4SDimitry Andric Kind = Float; 8810b57cec5SDimitry Andric break; 882480093f4SDimitry Andric case 'P': 883480093f4SDimitry Andric Kind = Poly; 8840b57cec5SDimitry Andric break; 885480093f4SDimitry Andric case '>': 886480093f4SDimitry Andric assert(ElementBitwidth < 128); 887480093f4SDimitry Andric ElementBitwidth *= 2; 888480093f4SDimitry Andric break; 889480093f4SDimitry Andric case '<': 890480093f4SDimitry Andric assert(ElementBitwidth > 8); 891480093f4SDimitry Andric ElementBitwidth /= 2; 8920b57cec5SDimitry Andric break; 8930b57cec5SDimitry Andric case '1': 8940b57cec5SDimitry Andric NumVectors = 0; 8950b57cec5SDimitry Andric break; 8960b57cec5SDimitry Andric case '2': 8970b57cec5SDimitry Andric NumVectors = 2; 8980b57cec5SDimitry Andric break; 8990b57cec5SDimitry Andric case '3': 9000b57cec5SDimitry Andric NumVectors = 3; 9010b57cec5SDimitry Andric break; 9020b57cec5SDimitry Andric case '4': 9030b57cec5SDimitry Andric NumVectors = 4; 9040b57cec5SDimitry Andric break; 905480093f4SDimitry Andric case '*': 906480093f4SDimitry Andric Pointer = true; 9070b57cec5SDimitry Andric break; 908480093f4SDimitry Andric case 'c': 909480093f4SDimitry Andric Constant = true; 9100b57cec5SDimitry Andric break; 911480093f4SDimitry Andric case 'Q': 912480093f4SDimitry Andric Bitwidth = 128; 9130b57cec5SDimitry Andric break; 914480093f4SDimitry Andric case 'q': 915480093f4SDimitry Andric Bitwidth = 64; 9160b57cec5SDimitry Andric break; 917480093f4SDimitry Andric case 'I': 918480093f4SDimitry Andric Kind = SInt; 919480093f4SDimitry Andric ElementBitwidth = Bitwidth = 32; 920480093f4SDimitry Andric NumVectors = 0; 921480093f4SDimitry Andric Immediate = true; 9220b57cec5SDimitry Andric break; 923480093f4SDimitry Andric case 'p': 924480093f4SDimitry Andric if (isPoly()) 925480093f4SDimitry Andric Kind = UInt; 926480093f4SDimitry Andric break; 927480093f4SDimitry Andric case '!': 928480093f4SDimitry Andric // Key type, handled elsewhere. 9290b57cec5SDimitry Andric break; 9300b57cec5SDimitry Andric default: 9310b57cec5SDimitry Andric llvm_unreachable("Unhandled character!"); 9320b57cec5SDimitry Andric } 9330b57cec5SDimitry Andric } 934480093f4SDimitry Andric } 9350b57cec5SDimitry Andric 9360b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 9370b57cec5SDimitry Andric // Intrinsic implementation 9380b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 9390b57cec5SDimitry Andric 940480093f4SDimitry Andric StringRef Intrinsic::getNextModifiers(StringRef Proto, unsigned &Pos) const { 941480093f4SDimitry Andric if (Proto.size() == Pos) 942480093f4SDimitry Andric return StringRef(); 943480093f4SDimitry Andric else if (Proto[Pos] != '(') 944480093f4SDimitry Andric return Proto.substr(Pos++, 1); 945480093f4SDimitry Andric 946480093f4SDimitry Andric size_t Start = Pos + 1; 947480093f4SDimitry Andric size_t End = Proto.find(')', Start); 948480093f4SDimitry Andric assert_with_loc(End != StringRef::npos, "unmatched modifier group paren"); 949480093f4SDimitry Andric Pos = End + 1; 950480093f4SDimitry Andric return Proto.slice(Start, End); 951480093f4SDimitry Andric } 952480093f4SDimitry Andric 9530b57cec5SDimitry Andric std::string Intrinsic::getInstTypeCode(Type T, ClassKind CK) const { 9540b57cec5SDimitry Andric char typeCode = '\0'; 9550b57cec5SDimitry Andric bool printNumber = true; 9560b57cec5SDimitry Andric 9570b57cec5SDimitry Andric if (CK == ClassB) 9580b57cec5SDimitry Andric return ""; 9590b57cec5SDimitry Andric 960*5ffd83dbSDimitry Andric if (T.isBFloat16()) 961*5ffd83dbSDimitry Andric return "bf16"; 962*5ffd83dbSDimitry Andric 9630b57cec5SDimitry Andric if (T.isPoly()) 9640b57cec5SDimitry Andric typeCode = 'p'; 9650b57cec5SDimitry Andric else if (T.isInteger()) 9660b57cec5SDimitry Andric typeCode = T.isSigned() ? 's' : 'u'; 9670b57cec5SDimitry Andric else 9680b57cec5SDimitry Andric typeCode = 'f'; 9690b57cec5SDimitry Andric 9700b57cec5SDimitry Andric if (CK == ClassI) { 9710b57cec5SDimitry Andric switch (typeCode) { 9720b57cec5SDimitry Andric default: 9730b57cec5SDimitry Andric break; 9740b57cec5SDimitry Andric case 's': 9750b57cec5SDimitry Andric case 'u': 9760b57cec5SDimitry Andric case 'p': 9770b57cec5SDimitry Andric typeCode = 'i'; 9780b57cec5SDimitry Andric break; 9790b57cec5SDimitry Andric } 9800b57cec5SDimitry Andric } 9810b57cec5SDimitry Andric if (CK == ClassB) { 9820b57cec5SDimitry Andric typeCode = '\0'; 9830b57cec5SDimitry Andric } 9840b57cec5SDimitry Andric 9850b57cec5SDimitry Andric std::string S; 9860b57cec5SDimitry Andric if (typeCode != '\0') 9870b57cec5SDimitry Andric S.push_back(typeCode); 9880b57cec5SDimitry Andric if (printNumber) 9890b57cec5SDimitry Andric S += utostr(T.getElementSizeInBits()); 9900b57cec5SDimitry Andric 9910b57cec5SDimitry Andric return S; 9920b57cec5SDimitry Andric } 9930b57cec5SDimitry Andric 9940b57cec5SDimitry Andric std::string Intrinsic::getBuiltinTypeStr() { 9950b57cec5SDimitry Andric ClassKind LocalCK = getClassKind(true); 9960b57cec5SDimitry Andric std::string S; 9970b57cec5SDimitry Andric 9980b57cec5SDimitry Andric Type RetT = getReturnType(); 9990b57cec5SDimitry Andric if ((LocalCK == ClassI || LocalCK == ClassW) && RetT.isScalar() && 1000*5ffd83dbSDimitry Andric !RetT.isFloating() && !RetT.isBFloat16()) 10010b57cec5SDimitry Andric RetT.makeInteger(RetT.getElementSizeInBits(), false); 10020b57cec5SDimitry Andric 10030b57cec5SDimitry Andric // Since the return value must be one type, return a vector type of the 10040b57cec5SDimitry Andric // appropriate width which we will bitcast. An exception is made for 10050b57cec5SDimitry Andric // returning structs of 2, 3, or 4 vectors which are returned in a sret-like 10060b57cec5SDimitry Andric // fashion, storing them to a pointer arg. 10070b57cec5SDimitry Andric if (RetT.getNumVectors() > 1) { 10080b57cec5SDimitry Andric S += "vv*"; // void result with void* first argument 10090b57cec5SDimitry Andric } else { 10100b57cec5SDimitry Andric if (RetT.isPoly()) 10110b57cec5SDimitry Andric RetT.makeInteger(RetT.getElementSizeInBits(), false); 1012480093f4SDimitry Andric if (!RetT.isScalar() && RetT.isInteger() && !RetT.isSigned()) 10130b57cec5SDimitry Andric RetT.makeSigned(); 10140b57cec5SDimitry Andric 1015480093f4SDimitry Andric if (LocalCK == ClassB && RetT.isValue() && !RetT.isScalar()) 10160b57cec5SDimitry Andric // Cast to vector of 8-bit elements. 10170b57cec5SDimitry Andric RetT.makeInteger(8, true); 10180b57cec5SDimitry Andric 10190b57cec5SDimitry Andric S += RetT.builtin_str(); 10200b57cec5SDimitry Andric } 10210b57cec5SDimitry Andric 10220b57cec5SDimitry Andric for (unsigned I = 0; I < getNumParams(); ++I) { 10230b57cec5SDimitry Andric Type T = getParamType(I); 10240b57cec5SDimitry Andric if (T.isPoly()) 10250b57cec5SDimitry Andric T.makeInteger(T.getElementSizeInBits(), false); 10260b57cec5SDimitry Andric 1027480093f4SDimitry Andric if (LocalCK == ClassB && !T.isScalar()) 10280b57cec5SDimitry Andric T.makeInteger(8, true); 10290b57cec5SDimitry Andric // Halves always get converted to 8-bit elements. 10300b57cec5SDimitry Andric if (T.isHalf() && T.isVector() && !T.isScalarForMangling()) 10310b57cec5SDimitry Andric T.makeInteger(8, true); 10320b57cec5SDimitry Andric 1033480093f4SDimitry Andric if (LocalCK == ClassI && T.isInteger()) 10340b57cec5SDimitry Andric T.makeSigned(); 10350b57cec5SDimitry Andric 10360b57cec5SDimitry Andric if (hasImmediate() && getImmediateIdx() == I) 10370b57cec5SDimitry Andric T.makeImmediate(32); 10380b57cec5SDimitry Andric 10390b57cec5SDimitry Andric S += T.builtin_str(); 10400b57cec5SDimitry Andric } 10410b57cec5SDimitry Andric 10420b57cec5SDimitry Andric // Extra constant integer to hold type class enum for this function, e.g. s8 10430b57cec5SDimitry Andric if (LocalCK == ClassB) 10440b57cec5SDimitry Andric S += "i"; 10450b57cec5SDimitry Andric 10460b57cec5SDimitry Andric return S; 10470b57cec5SDimitry Andric } 10480b57cec5SDimitry Andric 10490b57cec5SDimitry Andric std::string Intrinsic::getMangledName(bool ForceClassS) const { 10500b57cec5SDimitry Andric // Check if the prototype has a scalar operand with the type of the vector 10510b57cec5SDimitry Andric // elements. If not, bitcasting the args will take care of arg checking. 10520b57cec5SDimitry Andric // The actual signedness etc. will be taken care of with special enums. 10530b57cec5SDimitry Andric ClassKind LocalCK = CK; 10540b57cec5SDimitry Andric if (!protoHasScalar()) 10550b57cec5SDimitry Andric LocalCK = ClassB; 10560b57cec5SDimitry Andric 10570b57cec5SDimitry Andric return mangleName(Name, ForceClassS ? ClassS : LocalCK); 10580b57cec5SDimitry Andric } 10590b57cec5SDimitry Andric 10600b57cec5SDimitry Andric std::string Intrinsic::mangleName(std::string Name, ClassKind LocalCK) const { 10610b57cec5SDimitry Andric std::string typeCode = getInstTypeCode(BaseType, LocalCK); 10620b57cec5SDimitry Andric std::string S = Name; 10630b57cec5SDimitry Andric 10640b57cec5SDimitry Andric if (Name == "vcvt_f16_f32" || Name == "vcvt_f32_f16" || 1065*5ffd83dbSDimitry Andric Name == "vcvt_f32_f64" || Name == "vcvt_f64_f32" || 1066*5ffd83dbSDimitry Andric Name == "vcvt_f32_bf16") 10670b57cec5SDimitry Andric return Name; 10680b57cec5SDimitry Andric 10690b57cec5SDimitry Andric if (!typeCode.empty()) { 10700b57cec5SDimitry Andric // If the name ends with _xN (N = 2,3,4), insert the typeCode before _xN. 10710b57cec5SDimitry Andric if (Name.size() >= 3 && isdigit(Name.back()) && 10720b57cec5SDimitry Andric Name[Name.length() - 2] == 'x' && Name[Name.length() - 3] == '_') 10730b57cec5SDimitry Andric S.insert(S.length() - 3, "_" + typeCode); 10740b57cec5SDimitry Andric else 10750b57cec5SDimitry Andric S += "_" + typeCode; 10760b57cec5SDimitry Andric } 10770b57cec5SDimitry Andric 10780b57cec5SDimitry Andric if (BaseType != InBaseType) { 10790b57cec5SDimitry Andric // A reinterpret - out the input base type at the end. 10800b57cec5SDimitry Andric S += "_" + getInstTypeCode(InBaseType, LocalCK); 10810b57cec5SDimitry Andric } 10820b57cec5SDimitry Andric 10830b57cec5SDimitry Andric if (LocalCK == ClassB) 10840b57cec5SDimitry Andric S += "_v"; 10850b57cec5SDimitry Andric 10860b57cec5SDimitry Andric // Insert a 'q' before the first '_' character so that it ends up before 10870b57cec5SDimitry Andric // _lane or _n on vector-scalar operations. 10880b57cec5SDimitry Andric if (BaseType.getSizeInBits() == 128 && !BaseType.noManglingQ()) { 10890b57cec5SDimitry Andric size_t Pos = S.find('_'); 10900b57cec5SDimitry Andric S.insert(Pos, "q"); 10910b57cec5SDimitry Andric } 10920b57cec5SDimitry Andric 10930b57cec5SDimitry Andric char Suffix = '\0'; 10940b57cec5SDimitry Andric if (BaseType.isScalarForMangling()) { 10950b57cec5SDimitry Andric switch (BaseType.getElementSizeInBits()) { 10960b57cec5SDimitry Andric case 8: Suffix = 'b'; break; 10970b57cec5SDimitry Andric case 16: Suffix = 'h'; break; 10980b57cec5SDimitry Andric case 32: Suffix = 's'; break; 10990b57cec5SDimitry Andric case 64: Suffix = 'd'; break; 11000b57cec5SDimitry Andric default: llvm_unreachable("Bad suffix!"); 11010b57cec5SDimitry Andric } 11020b57cec5SDimitry Andric } 11030b57cec5SDimitry Andric if (Suffix != '\0') { 11040b57cec5SDimitry Andric size_t Pos = S.find('_'); 11050b57cec5SDimitry Andric S.insert(Pos, &Suffix, 1); 11060b57cec5SDimitry Andric } 11070b57cec5SDimitry Andric 11080b57cec5SDimitry Andric return S; 11090b57cec5SDimitry Andric } 11100b57cec5SDimitry Andric 11110b57cec5SDimitry Andric std::string Intrinsic::replaceParamsIn(std::string S) { 11120b57cec5SDimitry Andric while (S.find('$') != std::string::npos) { 11130b57cec5SDimitry Andric size_t Pos = S.find('$'); 11140b57cec5SDimitry Andric size_t End = Pos + 1; 11150b57cec5SDimitry Andric while (isalpha(S[End])) 11160b57cec5SDimitry Andric ++End; 11170b57cec5SDimitry Andric 11180b57cec5SDimitry Andric std::string VarName = S.substr(Pos + 1, End - Pos - 1); 11190b57cec5SDimitry Andric assert_with_loc(Variables.find(VarName) != Variables.end(), 11200b57cec5SDimitry Andric "Variable not defined!"); 11210b57cec5SDimitry Andric S.replace(Pos, End - Pos, Variables.find(VarName)->second.getName()); 11220b57cec5SDimitry Andric } 11230b57cec5SDimitry Andric 11240b57cec5SDimitry Andric return S; 11250b57cec5SDimitry Andric } 11260b57cec5SDimitry Andric 11270b57cec5SDimitry Andric void Intrinsic::initVariables() { 11280b57cec5SDimitry Andric Variables.clear(); 11290b57cec5SDimitry Andric 11300b57cec5SDimitry Andric // Modify the TypeSpec per-argument to get a concrete Type, and create 11310b57cec5SDimitry Andric // known variables for each. 1132480093f4SDimitry Andric for (unsigned I = 1; I < Types.size(); ++I) { 11330b57cec5SDimitry Andric char NameC = '0' + (I - 1); 11340b57cec5SDimitry Andric std::string Name = "p"; 11350b57cec5SDimitry Andric Name.push_back(NameC); 11360b57cec5SDimitry Andric 11370b57cec5SDimitry Andric Variables[Name] = Variable(Types[I], Name + VariablePostfix); 11380b57cec5SDimitry Andric } 11390b57cec5SDimitry Andric RetVar = Variable(Types[0], "ret" + VariablePostfix); 11400b57cec5SDimitry Andric } 11410b57cec5SDimitry Andric 11420b57cec5SDimitry Andric void Intrinsic::emitPrototype(StringRef NamePrefix) { 11430b57cec5SDimitry Andric if (UseMacro) 11440b57cec5SDimitry Andric OS << "#define "; 11450b57cec5SDimitry Andric else 11460b57cec5SDimitry Andric OS << "__ai " << Types[0].str() << " "; 11470b57cec5SDimitry Andric 11480b57cec5SDimitry Andric OS << NamePrefix.str() << mangleName(Name, ClassS) << "("; 11490b57cec5SDimitry Andric 11500b57cec5SDimitry Andric for (unsigned I = 0; I < getNumParams(); ++I) { 11510b57cec5SDimitry Andric if (I != 0) 11520b57cec5SDimitry Andric OS << ", "; 11530b57cec5SDimitry Andric 11540b57cec5SDimitry Andric char NameC = '0' + I; 11550b57cec5SDimitry Andric std::string Name = "p"; 11560b57cec5SDimitry Andric Name.push_back(NameC); 11570b57cec5SDimitry Andric assert(Variables.find(Name) != Variables.end()); 11580b57cec5SDimitry Andric Variable &V = Variables[Name]; 11590b57cec5SDimitry Andric 11600b57cec5SDimitry Andric if (!UseMacro) 11610b57cec5SDimitry Andric OS << V.getType().str() << " "; 11620b57cec5SDimitry Andric OS << V.getName(); 11630b57cec5SDimitry Andric } 11640b57cec5SDimitry Andric 11650b57cec5SDimitry Andric OS << ")"; 11660b57cec5SDimitry Andric } 11670b57cec5SDimitry Andric 11680b57cec5SDimitry Andric void Intrinsic::emitOpeningBrace() { 11690b57cec5SDimitry Andric if (UseMacro) 11700b57cec5SDimitry Andric OS << " __extension__ ({"; 11710b57cec5SDimitry Andric else 11720b57cec5SDimitry Andric OS << " {"; 11730b57cec5SDimitry Andric emitNewLine(); 11740b57cec5SDimitry Andric } 11750b57cec5SDimitry Andric 11760b57cec5SDimitry Andric void Intrinsic::emitClosingBrace() { 11770b57cec5SDimitry Andric if (UseMacro) 11780b57cec5SDimitry Andric OS << "})"; 11790b57cec5SDimitry Andric else 11800b57cec5SDimitry Andric OS << "}"; 11810b57cec5SDimitry Andric } 11820b57cec5SDimitry Andric 11830b57cec5SDimitry Andric void Intrinsic::emitNewLine() { 11840b57cec5SDimitry Andric if (UseMacro) 11850b57cec5SDimitry Andric OS << " \\\n"; 11860b57cec5SDimitry Andric else 11870b57cec5SDimitry Andric OS << "\n"; 11880b57cec5SDimitry Andric } 11890b57cec5SDimitry Andric 11900b57cec5SDimitry Andric void Intrinsic::emitReverseVariable(Variable &Dest, Variable &Src) { 11910b57cec5SDimitry Andric if (Dest.getType().getNumVectors() > 1) { 11920b57cec5SDimitry Andric emitNewLine(); 11930b57cec5SDimitry Andric 11940b57cec5SDimitry Andric for (unsigned K = 0; K < Dest.getType().getNumVectors(); ++K) { 11950b57cec5SDimitry Andric OS << " " << Dest.getName() << ".val[" << K << "] = " 11960b57cec5SDimitry Andric << "__builtin_shufflevector(" 11970b57cec5SDimitry Andric << Src.getName() << ".val[" << K << "], " 11980b57cec5SDimitry Andric << Src.getName() << ".val[" << K << "]"; 11990b57cec5SDimitry Andric for (int J = Dest.getType().getNumElements() - 1; J >= 0; --J) 12000b57cec5SDimitry Andric OS << ", " << J; 12010b57cec5SDimitry Andric OS << ");"; 12020b57cec5SDimitry Andric emitNewLine(); 12030b57cec5SDimitry Andric } 12040b57cec5SDimitry Andric } else { 12050b57cec5SDimitry Andric OS << " " << Dest.getName() 12060b57cec5SDimitry Andric << " = __builtin_shufflevector(" << Src.getName() << ", " << Src.getName(); 12070b57cec5SDimitry Andric for (int J = Dest.getType().getNumElements() - 1; J >= 0; --J) 12080b57cec5SDimitry Andric OS << ", " << J; 12090b57cec5SDimitry Andric OS << ");"; 12100b57cec5SDimitry Andric emitNewLine(); 12110b57cec5SDimitry Andric } 12120b57cec5SDimitry Andric } 12130b57cec5SDimitry Andric 12140b57cec5SDimitry Andric void Intrinsic::emitArgumentReversal() { 1215a7dea167SDimitry Andric if (isBigEndianSafe()) 12160b57cec5SDimitry Andric return; 12170b57cec5SDimitry Andric 12180b57cec5SDimitry Andric // Reverse all vector arguments. 12190b57cec5SDimitry Andric for (unsigned I = 0; I < getNumParams(); ++I) { 12200b57cec5SDimitry Andric std::string Name = "p" + utostr(I); 12210b57cec5SDimitry Andric std::string NewName = "rev" + utostr(I); 12220b57cec5SDimitry Andric 12230b57cec5SDimitry Andric Variable &V = Variables[Name]; 12240b57cec5SDimitry Andric Variable NewV(V.getType(), NewName + VariablePostfix); 12250b57cec5SDimitry Andric 12260b57cec5SDimitry Andric if (!NewV.getType().isVector() || NewV.getType().getNumElements() == 1) 12270b57cec5SDimitry Andric continue; 12280b57cec5SDimitry Andric 12290b57cec5SDimitry Andric OS << " " << NewV.getType().str() << " " << NewV.getName() << ";"; 12300b57cec5SDimitry Andric emitReverseVariable(NewV, V); 12310b57cec5SDimitry Andric V = NewV; 12320b57cec5SDimitry Andric } 12330b57cec5SDimitry Andric } 12340b57cec5SDimitry Andric 12350b57cec5SDimitry Andric void Intrinsic::emitReturnReversal() { 1236a7dea167SDimitry Andric if (isBigEndianSafe()) 12370b57cec5SDimitry Andric return; 12380b57cec5SDimitry Andric if (!getReturnType().isVector() || getReturnType().isVoid() || 12390b57cec5SDimitry Andric getReturnType().getNumElements() == 1) 12400b57cec5SDimitry Andric return; 12410b57cec5SDimitry Andric emitReverseVariable(RetVar, RetVar); 12420b57cec5SDimitry Andric } 12430b57cec5SDimitry Andric 12440b57cec5SDimitry Andric void Intrinsic::emitShadowedArgs() { 12450b57cec5SDimitry Andric // Macro arguments are not type-checked like inline function arguments, 12460b57cec5SDimitry Andric // so assign them to local temporaries to get the right type checking. 12470b57cec5SDimitry Andric if (!UseMacro) 12480b57cec5SDimitry Andric return; 12490b57cec5SDimitry Andric 12500b57cec5SDimitry Andric for (unsigned I = 0; I < getNumParams(); ++I) { 12510b57cec5SDimitry Andric // Do not create a temporary for an immediate argument. 12520b57cec5SDimitry Andric // That would defeat the whole point of using a macro! 1253480093f4SDimitry Andric if (getParamType(I).isImmediate()) 12540b57cec5SDimitry Andric continue; 12550b57cec5SDimitry Andric // Do not create a temporary for pointer arguments. The input 12560b57cec5SDimitry Andric // pointer may have an alignment hint. 12570b57cec5SDimitry Andric if (getParamType(I).isPointer()) 12580b57cec5SDimitry Andric continue; 12590b57cec5SDimitry Andric 12600b57cec5SDimitry Andric std::string Name = "p" + utostr(I); 12610b57cec5SDimitry Andric 12620b57cec5SDimitry Andric assert(Variables.find(Name) != Variables.end()); 12630b57cec5SDimitry Andric Variable &V = Variables[Name]; 12640b57cec5SDimitry Andric 12650b57cec5SDimitry Andric std::string NewName = "s" + utostr(I); 12660b57cec5SDimitry Andric Variable V2(V.getType(), NewName + VariablePostfix); 12670b57cec5SDimitry Andric 12680b57cec5SDimitry Andric OS << " " << V2.getType().str() << " " << V2.getName() << " = " 12690b57cec5SDimitry Andric << V.getName() << ";"; 12700b57cec5SDimitry Andric emitNewLine(); 12710b57cec5SDimitry Andric 12720b57cec5SDimitry Andric V = V2; 12730b57cec5SDimitry Andric } 12740b57cec5SDimitry Andric } 12750b57cec5SDimitry Andric 12760b57cec5SDimitry Andric bool Intrinsic::protoHasScalar() const { 1277480093f4SDimitry Andric return std::any_of(Types.begin(), Types.end(), [](const Type &T) { 1278480093f4SDimitry Andric return T.isScalar() && !T.isImmediate(); 1279480093f4SDimitry Andric }); 12800b57cec5SDimitry Andric } 12810b57cec5SDimitry Andric 12820b57cec5SDimitry Andric void Intrinsic::emitBodyAsBuiltinCall() { 12830b57cec5SDimitry Andric std::string S; 12840b57cec5SDimitry Andric 12850b57cec5SDimitry Andric // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit 12860b57cec5SDimitry Andric // sret-like argument. 12870b57cec5SDimitry Andric bool SRet = getReturnType().getNumVectors() >= 2; 12880b57cec5SDimitry Andric 12890b57cec5SDimitry Andric StringRef N = Name; 12900b57cec5SDimitry Andric ClassKind LocalCK = CK; 12910b57cec5SDimitry Andric if (!protoHasScalar()) 12920b57cec5SDimitry Andric LocalCK = ClassB; 12930b57cec5SDimitry Andric 12940b57cec5SDimitry Andric if (!getReturnType().isVoid() && !SRet) 12950b57cec5SDimitry Andric S += "(" + RetVar.getType().str() + ") "; 12960b57cec5SDimitry Andric 1297*5ffd83dbSDimitry Andric S += "__builtin_neon_" + mangleName(std::string(N), LocalCK) + "("; 12980b57cec5SDimitry Andric 12990b57cec5SDimitry Andric if (SRet) 13000b57cec5SDimitry Andric S += "&" + RetVar.getName() + ", "; 13010b57cec5SDimitry Andric 13020b57cec5SDimitry Andric for (unsigned I = 0; I < getNumParams(); ++I) { 13030b57cec5SDimitry Andric Variable &V = Variables["p" + utostr(I)]; 13040b57cec5SDimitry Andric Type T = V.getType(); 13050b57cec5SDimitry Andric 13060b57cec5SDimitry Andric // Handle multiple-vector values specially, emitting each subvector as an 13070b57cec5SDimitry Andric // argument to the builtin. 13080b57cec5SDimitry Andric if (T.getNumVectors() > 1) { 13090b57cec5SDimitry Andric // Check if an explicit cast is needed. 13100b57cec5SDimitry Andric std::string Cast; 1311a7dea167SDimitry Andric if (LocalCK == ClassB) { 13120b57cec5SDimitry Andric Type T2 = T; 13130b57cec5SDimitry Andric T2.makeOneVector(); 13140b57cec5SDimitry Andric T2.makeInteger(8, /*Signed=*/true); 13150b57cec5SDimitry Andric Cast = "(" + T2.str() + ")"; 13160b57cec5SDimitry Andric } 13170b57cec5SDimitry Andric 13180b57cec5SDimitry Andric for (unsigned J = 0; J < T.getNumVectors(); ++J) 13190b57cec5SDimitry Andric S += Cast + V.getName() + ".val[" + utostr(J) + "], "; 13200b57cec5SDimitry Andric continue; 13210b57cec5SDimitry Andric } 13220b57cec5SDimitry Andric 1323480093f4SDimitry Andric std::string Arg = V.getName(); 13240b57cec5SDimitry Andric Type CastToType = T; 13250b57cec5SDimitry Andric 13260b57cec5SDimitry Andric // Check if an explicit cast is needed. 1327a7dea167SDimitry Andric if (CastToType.isVector() && 1328a7dea167SDimitry Andric (LocalCK == ClassB || (T.isHalf() && !T.isScalarForMangling()))) { 13290b57cec5SDimitry Andric CastToType.makeInteger(8, true); 13300b57cec5SDimitry Andric Arg = "(" + CastToType.str() + ")" + Arg; 1331a7dea167SDimitry Andric } else if (CastToType.isVector() && LocalCK == ClassI) { 1332480093f4SDimitry Andric if (CastToType.isInteger()) 1333a7dea167SDimitry Andric CastToType.makeSigned(); 1334a7dea167SDimitry Andric Arg = "(" + CastToType.str() + ")" + Arg; 13350b57cec5SDimitry Andric } 13360b57cec5SDimitry Andric 13370b57cec5SDimitry Andric S += Arg + ", "; 13380b57cec5SDimitry Andric } 13390b57cec5SDimitry Andric 13400b57cec5SDimitry Andric // Extra constant integer to hold type class enum for this function, e.g. s8 13410b57cec5SDimitry Andric if (getClassKind(true) == ClassB) { 1342480093f4SDimitry Andric S += utostr(getPolymorphicKeyType().getNeonEnum()); 13430b57cec5SDimitry Andric } else { 13440b57cec5SDimitry Andric // Remove extraneous ", ". 13450b57cec5SDimitry Andric S.pop_back(); 13460b57cec5SDimitry Andric S.pop_back(); 13470b57cec5SDimitry Andric } 13480b57cec5SDimitry Andric S += ");"; 13490b57cec5SDimitry Andric 13500b57cec5SDimitry Andric std::string RetExpr; 13510b57cec5SDimitry Andric if (!SRet && !RetVar.getType().isVoid()) 13520b57cec5SDimitry Andric RetExpr = RetVar.getName() + " = "; 13530b57cec5SDimitry Andric 13540b57cec5SDimitry Andric OS << " " << RetExpr << S; 13550b57cec5SDimitry Andric emitNewLine(); 13560b57cec5SDimitry Andric } 13570b57cec5SDimitry Andric 13580b57cec5SDimitry Andric void Intrinsic::emitBody(StringRef CallPrefix) { 13590b57cec5SDimitry Andric std::vector<std::string> Lines; 13600b57cec5SDimitry Andric 13610b57cec5SDimitry Andric assert(RetVar.getType() == Types[0]); 13620b57cec5SDimitry Andric // Create a return variable, if we're not void. 13630b57cec5SDimitry Andric if (!RetVar.getType().isVoid()) { 13640b57cec5SDimitry Andric OS << " " << RetVar.getType().str() << " " << RetVar.getName() << ";"; 13650b57cec5SDimitry Andric emitNewLine(); 13660b57cec5SDimitry Andric } 13670b57cec5SDimitry Andric 13680b57cec5SDimitry Andric if (!Body || Body->getValues().empty()) { 13690b57cec5SDimitry Andric // Nothing specific to output - must output a builtin. 13700b57cec5SDimitry Andric emitBodyAsBuiltinCall(); 13710b57cec5SDimitry Andric return; 13720b57cec5SDimitry Andric } 13730b57cec5SDimitry Andric 13740b57cec5SDimitry Andric // We have a list of "things to output". The last should be returned. 13750b57cec5SDimitry Andric for (auto *I : Body->getValues()) { 13760b57cec5SDimitry Andric if (StringInit *SI = dyn_cast<StringInit>(I)) { 13770b57cec5SDimitry Andric Lines.push_back(replaceParamsIn(SI->getAsString())); 13780b57cec5SDimitry Andric } else if (DagInit *DI = dyn_cast<DagInit>(I)) { 13790b57cec5SDimitry Andric DagEmitter DE(*this, CallPrefix); 13800b57cec5SDimitry Andric Lines.push_back(DE.emitDag(DI).second + ";"); 13810b57cec5SDimitry Andric } 13820b57cec5SDimitry Andric } 13830b57cec5SDimitry Andric 13840b57cec5SDimitry Andric assert(!Lines.empty() && "Empty def?"); 13850b57cec5SDimitry Andric if (!RetVar.getType().isVoid()) 13860b57cec5SDimitry Andric Lines.back().insert(0, RetVar.getName() + " = "); 13870b57cec5SDimitry Andric 13880b57cec5SDimitry Andric for (auto &L : Lines) { 13890b57cec5SDimitry Andric OS << " " << L; 13900b57cec5SDimitry Andric emitNewLine(); 13910b57cec5SDimitry Andric } 13920b57cec5SDimitry Andric } 13930b57cec5SDimitry Andric 13940b57cec5SDimitry Andric void Intrinsic::emitReturn() { 13950b57cec5SDimitry Andric if (RetVar.getType().isVoid()) 13960b57cec5SDimitry Andric return; 13970b57cec5SDimitry Andric if (UseMacro) 13980b57cec5SDimitry Andric OS << " " << RetVar.getName() << ";"; 13990b57cec5SDimitry Andric else 14000b57cec5SDimitry Andric OS << " return " << RetVar.getName() << ";"; 14010b57cec5SDimitry Andric emitNewLine(); 14020b57cec5SDimitry Andric } 14030b57cec5SDimitry Andric 14040b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDag(DagInit *DI) { 14050b57cec5SDimitry Andric // At this point we should only be seeing a def. 14060b57cec5SDimitry Andric DefInit *DefI = cast<DefInit>(DI->getOperator()); 14070b57cec5SDimitry Andric std::string Op = DefI->getAsString(); 14080b57cec5SDimitry Andric 14090b57cec5SDimitry Andric if (Op == "cast" || Op == "bitcast") 14100b57cec5SDimitry Andric return emitDagCast(DI, Op == "bitcast"); 14110b57cec5SDimitry Andric if (Op == "shuffle") 14120b57cec5SDimitry Andric return emitDagShuffle(DI); 14130b57cec5SDimitry Andric if (Op == "dup") 14140b57cec5SDimitry Andric return emitDagDup(DI); 14150b57cec5SDimitry Andric if (Op == "dup_typed") 14160b57cec5SDimitry Andric return emitDagDupTyped(DI); 14170b57cec5SDimitry Andric if (Op == "splat") 14180b57cec5SDimitry Andric return emitDagSplat(DI); 14190b57cec5SDimitry Andric if (Op == "save_temp") 14200b57cec5SDimitry Andric return emitDagSaveTemp(DI); 14210b57cec5SDimitry Andric if (Op == "op") 14220b57cec5SDimitry Andric return emitDagOp(DI); 1423*5ffd83dbSDimitry Andric if (Op == "call" || Op == "call_mangled") 1424*5ffd83dbSDimitry Andric return emitDagCall(DI, Op == "call_mangled"); 14250b57cec5SDimitry Andric if (Op == "name_replace") 14260b57cec5SDimitry Andric return emitDagNameReplace(DI); 14270b57cec5SDimitry Andric if (Op == "literal") 14280b57cec5SDimitry Andric return emitDagLiteral(DI); 14290b57cec5SDimitry Andric assert_with_loc(false, "Unknown operation!"); 14300b57cec5SDimitry Andric return std::make_pair(Type::getVoid(), ""); 14310b57cec5SDimitry Andric } 14320b57cec5SDimitry Andric 14330b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagOp(DagInit *DI) { 14340b57cec5SDimitry Andric std::string Op = cast<StringInit>(DI->getArg(0))->getAsUnquotedString(); 14350b57cec5SDimitry Andric if (DI->getNumArgs() == 2) { 14360b57cec5SDimitry Andric // Unary op. 14370b57cec5SDimitry Andric std::pair<Type, std::string> R = 1438*5ffd83dbSDimitry Andric emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1))); 14390b57cec5SDimitry Andric return std::make_pair(R.first, Op + R.second); 14400b57cec5SDimitry Andric } else { 14410b57cec5SDimitry Andric assert(DI->getNumArgs() == 3 && "Can only handle unary and binary ops!"); 14420b57cec5SDimitry Andric std::pair<Type, std::string> R1 = 1443*5ffd83dbSDimitry Andric emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1))); 14440b57cec5SDimitry Andric std::pair<Type, std::string> R2 = 1445*5ffd83dbSDimitry Andric emitDagArg(DI->getArg(2), std::string(DI->getArgNameStr(2))); 14460b57cec5SDimitry Andric assert_with_loc(R1.first == R2.first, "Argument type mismatch!"); 14470b57cec5SDimitry Andric return std::make_pair(R1.first, R1.second + " " + Op + " " + R2.second); 14480b57cec5SDimitry Andric } 14490b57cec5SDimitry Andric } 14500b57cec5SDimitry Andric 1451*5ffd83dbSDimitry Andric std::pair<Type, std::string> 1452*5ffd83dbSDimitry Andric Intrinsic::DagEmitter::emitDagCall(DagInit *DI, bool MatchMangledName) { 14530b57cec5SDimitry Andric std::vector<Type> Types; 14540b57cec5SDimitry Andric std::vector<std::string> Values; 14550b57cec5SDimitry Andric for (unsigned I = 0; I < DI->getNumArgs() - 1; ++I) { 14560b57cec5SDimitry Andric std::pair<Type, std::string> R = 1457*5ffd83dbSDimitry Andric emitDagArg(DI->getArg(I + 1), std::string(DI->getArgNameStr(I + 1))); 14580b57cec5SDimitry Andric Types.push_back(R.first); 14590b57cec5SDimitry Andric Values.push_back(R.second); 14600b57cec5SDimitry Andric } 14610b57cec5SDimitry Andric 14620b57cec5SDimitry Andric // Look up the called intrinsic. 14630b57cec5SDimitry Andric std::string N; 14640b57cec5SDimitry Andric if (StringInit *SI = dyn_cast<StringInit>(DI->getArg(0))) 14650b57cec5SDimitry Andric N = SI->getAsUnquotedString(); 14660b57cec5SDimitry Andric else 14670b57cec5SDimitry Andric N = emitDagArg(DI->getArg(0), "").second; 1468*5ffd83dbSDimitry Andric Optional<std::string> MangledName; 1469*5ffd83dbSDimitry Andric if (MatchMangledName) { 1470*5ffd83dbSDimitry Andric if (Intr.getRecord()->getValueAsBit("isLaneQ")) 1471*5ffd83dbSDimitry Andric N += "q"; 1472*5ffd83dbSDimitry Andric MangledName = Intr.mangleName(N, ClassS); 1473*5ffd83dbSDimitry Andric } 1474*5ffd83dbSDimitry Andric Intrinsic &Callee = Intr.Emitter.getIntrinsic(N, Types, MangledName); 14750b57cec5SDimitry Andric 14760b57cec5SDimitry Andric // Make sure the callee is known as an early def. 14770b57cec5SDimitry Andric Callee.setNeededEarly(); 14780b57cec5SDimitry Andric Intr.Dependencies.insert(&Callee); 14790b57cec5SDimitry Andric 14800b57cec5SDimitry Andric // Now create the call itself. 1481a7dea167SDimitry Andric std::string S = ""; 1482a7dea167SDimitry Andric if (!Callee.isBigEndianSafe()) 1483a7dea167SDimitry Andric S += CallPrefix.str(); 1484a7dea167SDimitry Andric S += Callee.getMangledName(true) + "("; 14850b57cec5SDimitry Andric for (unsigned I = 0; I < DI->getNumArgs() - 1; ++I) { 14860b57cec5SDimitry Andric if (I != 0) 14870b57cec5SDimitry Andric S += ", "; 14880b57cec5SDimitry Andric S += Values[I]; 14890b57cec5SDimitry Andric } 14900b57cec5SDimitry Andric S += ")"; 14910b57cec5SDimitry Andric 14920b57cec5SDimitry Andric return std::make_pair(Callee.getReturnType(), S); 14930b57cec5SDimitry Andric } 14940b57cec5SDimitry Andric 14950b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagCast(DagInit *DI, 14960b57cec5SDimitry Andric bool IsBitCast){ 14970b57cec5SDimitry Andric // (cast MOD* VAL) -> cast VAL to type given by MOD. 1498*5ffd83dbSDimitry Andric std::pair<Type, std::string> R = 1499*5ffd83dbSDimitry Andric emitDagArg(DI->getArg(DI->getNumArgs() - 1), 1500*5ffd83dbSDimitry Andric std::string(DI->getArgNameStr(DI->getNumArgs() - 1))); 15010b57cec5SDimitry Andric Type castToType = R.first; 15020b57cec5SDimitry Andric for (unsigned ArgIdx = 0; ArgIdx < DI->getNumArgs() - 1; ++ArgIdx) { 15030b57cec5SDimitry Andric 15040b57cec5SDimitry Andric // MOD can take several forms: 15050b57cec5SDimitry Andric // 1. $X - take the type of parameter / variable X. 15060b57cec5SDimitry Andric // 2. The value "R" - take the type of the return type. 15070b57cec5SDimitry Andric // 3. a type string 15080b57cec5SDimitry Andric // 4. The value "U" or "S" to switch the signedness. 15090b57cec5SDimitry Andric // 5. The value "H" or "D" to half or double the bitwidth. 15100b57cec5SDimitry Andric // 6. The value "8" to convert to 8-bit (signed) integer lanes. 15110b57cec5SDimitry Andric if (!DI->getArgNameStr(ArgIdx).empty()) { 1512*5ffd83dbSDimitry Andric assert_with_loc(Intr.Variables.find(std::string( 1513*5ffd83dbSDimitry Andric DI->getArgNameStr(ArgIdx))) != Intr.Variables.end(), 15140b57cec5SDimitry Andric "Variable not found"); 1515*5ffd83dbSDimitry Andric castToType = 1516*5ffd83dbSDimitry Andric Intr.Variables[std::string(DI->getArgNameStr(ArgIdx))].getType(); 15170b57cec5SDimitry Andric } else { 15180b57cec5SDimitry Andric StringInit *SI = dyn_cast<StringInit>(DI->getArg(ArgIdx)); 15190b57cec5SDimitry Andric assert_with_loc(SI, "Expected string type or $Name for cast type"); 15200b57cec5SDimitry Andric 15210b57cec5SDimitry Andric if (SI->getAsUnquotedString() == "R") { 15220b57cec5SDimitry Andric castToType = Intr.getReturnType(); 15230b57cec5SDimitry Andric } else if (SI->getAsUnquotedString() == "U") { 15240b57cec5SDimitry Andric castToType.makeUnsigned(); 15250b57cec5SDimitry Andric } else if (SI->getAsUnquotedString() == "S") { 15260b57cec5SDimitry Andric castToType.makeSigned(); 15270b57cec5SDimitry Andric } else if (SI->getAsUnquotedString() == "H") { 15280b57cec5SDimitry Andric castToType.halveLanes(); 15290b57cec5SDimitry Andric } else if (SI->getAsUnquotedString() == "D") { 15300b57cec5SDimitry Andric castToType.doubleLanes(); 15310b57cec5SDimitry Andric } else if (SI->getAsUnquotedString() == "8") { 15320b57cec5SDimitry Andric castToType.makeInteger(8, true); 1533*5ffd83dbSDimitry Andric } else if (SI->getAsUnquotedString() == "32") { 1534*5ffd83dbSDimitry Andric castToType.make32BitElement(); 15350b57cec5SDimitry Andric } else { 15360b57cec5SDimitry Andric castToType = Type::fromTypedefName(SI->getAsUnquotedString()); 15370b57cec5SDimitry Andric assert_with_loc(!castToType.isVoid(), "Unknown typedef"); 15380b57cec5SDimitry Andric } 15390b57cec5SDimitry Andric } 15400b57cec5SDimitry Andric } 15410b57cec5SDimitry Andric 15420b57cec5SDimitry Andric std::string S; 15430b57cec5SDimitry Andric if (IsBitCast) { 15440b57cec5SDimitry Andric // Emit a reinterpret cast. The second operand must be an lvalue, so create 15450b57cec5SDimitry Andric // a temporary. 15460b57cec5SDimitry Andric std::string N = "reint"; 15470b57cec5SDimitry Andric unsigned I = 0; 15480b57cec5SDimitry Andric while (Intr.Variables.find(N) != Intr.Variables.end()) 15490b57cec5SDimitry Andric N = "reint" + utostr(++I); 15500b57cec5SDimitry Andric Intr.Variables[N] = Variable(R.first, N + Intr.VariablePostfix); 15510b57cec5SDimitry Andric 15520b57cec5SDimitry Andric Intr.OS << R.first.str() << " " << Intr.Variables[N].getName() << " = " 15530b57cec5SDimitry Andric << R.second << ";"; 15540b57cec5SDimitry Andric Intr.emitNewLine(); 15550b57cec5SDimitry Andric 15560b57cec5SDimitry Andric S = "*(" + castToType.str() + " *) &" + Intr.Variables[N].getName() + ""; 15570b57cec5SDimitry Andric } else { 15580b57cec5SDimitry Andric // Emit a normal (static) cast. 15590b57cec5SDimitry Andric S = "(" + castToType.str() + ")(" + R.second + ")"; 15600b57cec5SDimitry Andric } 15610b57cec5SDimitry Andric 15620b57cec5SDimitry Andric return std::make_pair(castToType, S); 15630b57cec5SDimitry Andric } 15640b57cec5SDimitry Andric 15650b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagShuffle(DagInit *DI){ 15660b57cec5SDimitry Andric // See the documentation in arm_neon.td for a description of these operators. 15670b57cec5SDimitry Andric class LowHalf : public SetTheory::Operator { 15680b57cec5SDimitry Andric public: 15690b57cec5SDimitry Andric void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts, 15700b57cec5SDimitry Andric ArrayRef<SMLoc> Loc) override { 15710b57cec5SDimitry Andric SetTheory::RecSet Elts2; 15720b57cec5SDimitry Andric ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts2, Loc); 15730b57cec5SDimitry Andric Elts.insert(Elts2.begin(), Elts2.begin() + (Elts2.size() / 2)); 15740b57cec5SDimitry Andric } 15750b57cec5SDimitry Andric }; 15760b57cec5SDimitry Andric 15770b57cec5SDimitry Andric class HighHalf : public SetTheory::Operator { 15780b57cec5SDimitry Andric public: 15790b57cec5SDimitry Andric void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts, 15800b57cec5SDimitry Andric ArrayRef<SMLoc> Loc) override { 15810b57cec5SDimitry Andric SetTheory::RecSet Elts2; 15820b57cec5SDimitry Andric ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts2, Loc); 15830b57cec5SDimitry Andric Elts.insert(Elts2.begin() + (Elts2.size() / 2), Elts2.end()); 15840b57cec5SDimitry Andric } 15850b57cec5SDimitry Andric }; 15860b57cec5SDimitry Andric 15870b57cec5SDimitry Andric class Rev : public SetTheory::Operator { 15880b57cec5SDimitry Andric unsigned ElementSize; 15890b57cec5SDimitry Andric 15900b57cec5SDimitry Andric public: 15910b57cec5SDimitry Andric Rev(unsigned ElementSize) : ElementSize(ElementSize) {} 15920b57cec5SDimitry Andric 15930b57cec5SDimitry Andric void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts, 15940b57cec5SDimitry Andric ArrayRef<SMLoc> Loc) override { 15950b57cec5SDimitry Andric SetTheory::RecSet Elts2; 15960b57cec5SDimitry Andric ST.evaluate(Expr->arg_begin() + 1, Expr->arg_end(), Elts2, Loc); 15970b57cec5SDimitry Andric 15980b57cec5SDimitry Andric int64_t VectorSize = cast<IntInit>(Expr->getArg(0))->getValue(); 15990b57cec5SDimitry Andric VectorSize /= ElementSize; 16000b57cec5SDimitry Andric 16010b57cec5SDimitry Andric std::vector<Record *> Revved; 16020b57cec5SDimitry Andric for (unsigned VI = 0; VI < Elts2.size(); VI += VectorSize) { 16030b57cec5SDimitry Andric for (int LI = VectorSize - 1; LI >= 0; --LI) { 16040b57cec5SDimitry Andric Revved.push_back(Elts2[VI + LI]); 16050b57cec5SDimitry Andric } 16060b57cec5SDimitry Andric } 16070b57cec5SDimitry Andric 16080b57cec5SDimitry Andric Elts.insert(Revved.begin(), Revved.end()); 16090b57cec5SDimitry Andric } 16100b57cec5SDimitry Andric }; 16110b57cec5SDimitry Andric 16120b57cec5SDimitry Andric class MaskExpander : public SetTheory::Expander { 16130b57cec5SDimitry Andric unsigned N; 16140b57cec5SDimitry Andric 16150b57cec5SDimitry Andric public: 16160b57cec5SDimitry Andric MaskExpander(unsigned N) : N(N) {} 16170b57cec5SDimitry Andric 16180b57cec5SDimitry Andric void expand(SetTheory &ST, Record *R, SetTheory::RecSet &Elts) override { 16190b57cec5SDimitry Andric unsigned Addend = 0; 16200b57cec5SDimitry Andric if (R->getName() == "mask0") 16210b57cec5SDimitry Andric Addend = 0; 16220b57cec5SDimitry Andric else if (R->getName() == "mask1") 16230b57cec5SDimitry Andric Addend = N; 16240b57cec5SDimitry Andric else 16250b57cec5SDimitry Andric return; 16260b57cec5SDimitry Andric for (unsigned I = 0; I < N; ++I) 16270b57cec5SDimitry Andric Elts.insert(R->getRecords().getDef("sv" + utostr(I + Addend))); 16280b57cec5SDimitry Andric } 16290b57cec5SDimitry Andric }; 16300b57cec5SDimitry Andric 16310b57cec5SDimitry Andric // (shuffle arg1, arg2, sequence) 16320b57cec5SDimitry Andric std::pair<Type, std::string> Arg1 = 1633*5ffd83dbSDimitry Andric emitDagArg(DI->getArg(0), std::string(DI->getArgNameStr(0))); 16340b57cec5SDimitry Andric std::pair<Type, std::string> Arg2 = 1635*5ffd83dbSDimitry Andric emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1))); 16360b57cec5SDimitry Andric assert_with_loc(Arg1.first == Arg2.first, 16370b57cec5SDimitry Andric "Different types in arguments to shuffle!"); 16380b57cec5SDimitry Andric 16390b57cec5SDimitry Andric SetTheory ST; 16400b57cec5SDimitry Andric SetTheory::RecSet Elts; 1641a7dea167SDimitry Andric ST.addOperator("lowhalf", std::make_unique<LowHalf>()); 1642a7dea167SDimitry Andric ST.addOperator("highhalf", std::make_unique<HighHalf>()); 16430b57cec5SDimitry Andric ST.addOperator("rev", 1644a7dea167SDimitry Andric std::make_unique<Rev>(Arg1.first.getElementSizeInBits())); 16450b57cec5SDimitry Andric ST.addExpander("MaskExpand", 1646a7dea167SDimitry Andric std::make_unique<MaskExpander>(Arg1.first.getNumElements())); 16470b57cec5SDimitry Andric ST.evaluate(DI->getArg(2), Elts, None); 16480b57cec5SDimitry Andric 16490b57cec5SDimitry Andric std::string S = "__builtin_shufflevector(" + Arg1.second + ", " + Arg2.second; 16500b57cec5SDimitry Andric for (auto &E : Elts) { 16510b57cec5SDimitry Andric StringRef Name = E->getName(); 16520b57cec5SDimitry Andric assert_with_loc(Name.startswith("sv"), 16530b57cec5SDimitry Andric "Incorrect element kind in shuffle mask!"); 16540b57cec5SDimitry Andric S += ", " + Name.drop_front(2).str(); 16550b57cec5SDimitry Andric } 16560b57cec5SDimitry Andric S += ")"; 16570b57cec5SDimitry Andric 16580b57cec5SDimitry Andric // Recalculate the return type - the shuffle may have halved or doubled it. 16590b57cec5SDimitry Andric Type T(Arg1.first); 16600b57cec5SDimitry Andric if (Elts.size() > T.getNumElements()) { 16610b57cec5SDimitry Andric assert_with_loc( 16620b57cec5SDimitry Andric Elts.size() == T.getNumElements() * 2, 16630b57cec5SDimitry Andric "Can only double or half the number of elements in a shuffle!"); 16640b57cec5SDimitry Andric T.doubleLanes(); 16650b57cec5SDimitry Andric } else if (Elts.size() < T.getNumElements()) { 16660b57cec5SDimitry Andric assert_with_loc( 16670b57cec5SDimitry Andric Elts.size() == T.getNumElements() / 2, 16680b57cec5SDimitry Andric "Can only double or half the number of elements in a shuffle!"); 16690b57cec5SDimitry Andric T.halveLanes(); 16700b57cec5SDimitry Andric } 16710b57cec5SDimitry Andric 16720b57cec5SDimitry Andric return std::make_pair(T, S); 16730b57cec5SDimitry Andric } 16740b57cec5SDimitry Andric 16750b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagDup(DagInit *DI) { 16760b57cec5SDimitry Andric assert_with_loc(DI->getNumArgs() == 1, "dup() expects one argument"); 1677*5ffd83dbSDimitry Andric std::pair<Type, std::string> A = 1678*5ffd83dbSDimitry Andric emitDagArg(DI->getArg(0), std::string(DI->getArgNameStr(0))); 16790b57cec5SDimitry Andric assert_with_loc(A.first.isScalar(), "dup() expects a scalar argument"); 16800b57cec5SDimitry Andric 16810b57cec5SDimitry Andric Type T = Intr.getBaseType(); 16820b57cec5SDimitry Andric assert_with_loc(T.isVector(), "dup() used but default type is scalar!"); 16830b57cec5SDimitry Andric std::string S = "(" + T.str() + ") {"; 16840b57cec5SDimitry Andric for (unsigned I = 0; I < T.getNumElements(); ++I) { 16850b57cec5SDimitry Andric if (I != 0) 16860b57cec5SDimitry Andric S += ", "; 16870b57cec5SDimitry Andric S += A.second; 16880b57cec5SDimitry Andric } 16890b57cec5SDimitry Andric S += "}"; 16900b57cec5SDimitry Andric 16910b57cec5SDimitry Andric return std::make_pair(T, S); 16920b57cec5SDimitry Andric } 16930b57cec5SDimitry Andric 16940b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagDupTyped(DagInit *DI) { 16950b57cec5SDimitry Andric assert_with_loc(DI->getNumArgs() == 2, "dup_typed() expects two arguments"); 1696*5ffd83dbSDimitry Andric std::pair<Type, std::string> A = 1697*5ffd83dbSDimitry Andric emitDagArg(DI->getArg(0), std::string(DI->getArgNameStr(0))); 1698*5ffd83dbSDimitry Andric std::pair<Type, std::string> B = 1699*5ffd83dbSDimitry Andric emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1))); 17000b57cec5SDimitry Andric assert_with_loc(B.first.isScalar(), 17010b57cec5SDimitry Andric "dup_typed() requires a scalar as the second argument"); 17020b57cec5SDimitry Andric 17030b57cec5SDimitry Andric Type T = A.first; 17040b57cec5SDimitry Andric assert_with_loc(T.isVector(), "dup_typed() used but target type is scalar!"); 17050b57cec5SDimitry Andric std::string S = "(" + T.str() + ") {"; 17060b57cec5SDimitry Andric for (unsigned I = 0; I < T.getNumElements(); ++I) { 17070b57cec5SDimitry Andric if (I != 0) 17080b57cec5SDimitry Andric S += ", "; 17090b57cec5SDimitry Andric S += B.second; 17100b57cec5SDimitry Andric } 17110b57cec5SDimitry Andric S += "}"; 17120b57cec5SDimitry Andric 17130b57cec5SDimitry Andric return std::make_pair(T, S); 17140b57cec5SDimitry Andric } 17150b57cec5SDimitry Andric 17160b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagSplat(DagInit *DI) { 17170b57cec5SDimitry Andric assert_with_loc(DI->getNumArgs() == 2, "splat() expects two arguments"); 1718*5ffd83dbSDimitry Andric std::pair<Type, std::string> A = 1719*5ffd83dbSDimitry Andric emitDagArg(DI->getArg(0), std::string(DI->getArgNameStr(0))); 1720*5ffd83dbSDimitry Andric std::pair<Type, std::string> B = 1721*5ffd83dbSDimitry Andric emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1))); 17220b57cec5SDimitry Andric 17230b57cec5SDimitry Andric assert_with_loc(B.first.isScalar(), 17240b57cec5SDimitry Andric "splat() requires a scalar int as the second argument"); 17250b57cec5SDimitry Andric 17260b57cec5SDimitry Andric std::string S = "__builtin_shufflevector(" + A.second + ", " + A.second; 17270b57cec5SDimitry Andric for (unsigned I = 0; I < Intr.getBaseType().getNumElements(); ++I) { 17280b57cec5SDimitry Andric S += ", " + B.second; 17290b57cec5SDimitry Andric } 17300b57cec5SDimitry Andric S += ")"; 17310b57cec5SDimitry Andric 17320b57cec5SDimitry Andric return std::make_pair(Intr.getBaseType(), S); 17330b57cec5SDimitry Andric } 17340b57cec5SDimitry Andric 17350b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagSaveTemp(DagInit *DI) { 17360b57cec5SDimitry Andric assert_with_loc(DI->getNumArgs() == 2, "save_temp() expects two arguments"); 1737*5ffd83dbSDimitry Andric std::pair<Type, std::string> A = 1738*5ffd83dbSDimitry Andric emitDagArg(DI->getArg(1), std::string(DI->getArgNameStr(1))); 17390b57cec5SDimitry Andric 17400b57cec5SDimitry Andric assert_with_loc(!A.first.isVoid(), 17410b57cec5SDimitry Andric "Argument to save_temp() must have non-void type!"); 17420b57cec5SDimitry Andric 1743*5ffd83dbSDimitry Andric std::string N = std::string(DI->getArgNameStr(0)); 17440b57cec5SDimitry Andric assert_with_loc(!N.empty(), 17450b57cec5SDimitry Andric "save_temp() expects a name as the first argument"); 17460b57cec5SDimitry Andric 17470b57cec5SDimitry Andric assert_with_loc(Intr.Variables.find(N) == Intr.Variables.end(), 17480b57cec5SDimitry Andric "Variable already defined!"); 17490b57cec5SDimitry Andric Intr.Variables[N] = Variable(A.first, N + Intr.VariablePostfix); 17500b57cec5SDimitry Andric 17510b57cec5SDimitry Andric std::string S = 17520b57cec5SDimitry Andric A.first.str() + " " + Intr.Variables[N].getName() + " = " + A.second; 17530b57cec5SDimitry Andric 17540b57cec5SDimitry Andric return std::make_pair(Type::getVoid(), S); 17550b57cec5SDimitry Andric } 17560b57cec5SDimitry Andric 17570b57cec5SDimitry Andric std::pair<Type, std::string> 17580b57cec5SDimitry Andric Intrinsic::DagEmitter::emitDagNameReplace(DagInit *DI) { 17590b57cec5SDimitry Andric std::string S = Intr.Name; 17600b57cec5SDimitry Andric 17610b57cec5SDimitry Andric assert_with_loc(DI->getNumArgs() == 2, "name_replace requires 2 arguments!"); 17620b57cec5SDimitry Andric std::string ToReplace = cast<StringInit>(DI->getArg(0))->getAsUnquotedString(); 17630b57cec5SDimitry Andric std::string ReplaceWith = cast<StringInit>(DI->getArg(1))->getAsUnquotedString(); 17640b57cec5SDimitry Andric 17650b57cec5SDimitry Andric size_t Idx = S.find(ToReplace); 17660b57cec5SDimitry Andric 17670b57cec5SDimitry Andric assert_with_loc(Idx != std::string::npos, "name should contain '" + ToReplace + "'!"); 17680b57cec5SDimitry Andric S.replace(Idx, ToReplace.size(), ReplaceWith); 17690b57cec5SDimitry Andric 17700b57cec5SDimitry Andric return std::make_pair(Type::getVoid(), S); 17710b57cec5SDimitry Andric } 17720b57cec5SDimitry Andric 17730b57cec5SDimitry Andric std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagLiteral(DagInit *DI){ 17740b57cec5SDimitry Andric std::string Ty = cast<StringInit>(DI->getArg(0))->getAsUnquotedString(); 17750b57cec5SDimitry Andric std::string Value = cast<StringInit>(DI->getArg(1))->getAsUnquotedString(); 17760b57cec5SDimitry Andric return std::make_pair(Type::fromTypedefName(Ty), Value); 17770b57cec5SDimitry Andric } 17780b57cec5SDimitry Andric 17790b57cec5SDimitry Andric std::pair<Type, std::string> 17800b57cec5SDimitry Andric Intrinsic::DagEmitter::emitDagArg(Init *Arg, std::string ArgName) { 17810b57cec5SDimitry Andric if (!ArgName.empty()) { 17820b57cec5SDimitry Andric assert_with_loc(!Arg->isComplete(), 17830b57cec5SDimitry Andric "Arguments must either be DAGs or names, not both!"); 17840b57cec5SDimitry Andric assert_with_loc(Intr.Variables.find(ArgName) != Intr.Variables.end(), 17850b57cec5SDimitry Andric "Variable not defined!"); 17860b57cec5SDimitry Andric Variable &V = Intr.Variables[ArgName]; 17870b57cec5SDimitry Andric return std::make_pair(V.getType(), V.getName()); 17880b57cec5SDimitry Andric } 17890b57cec5SDimitry Andric 17900b57cec5SDimitry Andric assert(Arg && "Neither ArgName nor Arg?!"); 17910b57cec5SDimitry Andric DagInit *DI = dyn_cast<DagInit>(Arg); 17920b57cec5SDimitry Andric assert_with_loc(DI, "Arguments must either be DAGs or names!"); 17930b57cec5SDimitry Andric 17940b57cec5SDimitry Andric return emitDag(DI); 17950b57cec5SDimitry Andric } 17960b57cec5SDimitry Andric 17970b57cec5SDimitry Andric std::string Intrinsic::generate() { 1798a7dea167SDimitry Andric // Avoid duplicated code for big and little endian 1799a7dea167SDimitry Andric if (isBigEndianSafe()) { 1800a7dea167SDimitry Andric generateImpl(false, "", ""); 1801a7dea167SDimitry Andric return OS.str(); 1802a7dea167SDimitry Andric } 18030b57cec5SDimitry Andric // Little endian intrinsics are simple and don't require any argument 18040b57cec5SDimitry Andric // swapping. 18050b57cec5SDimitry Andric OS << "#ifdef __LITTLE_ENDIAN__\n"; 18060b57cec5SDimitry Andric 18070b57cec5SDimitry Andric generateImpl(false, "", ""); 18080b57cec5SDimitry Andric 18090b57cec5SDimitry Andric OS << "#else\n"; 18100b57cec5SDimitry Andric 18110b57cec5SDimitry Andric // Big endian intrinsics are more complex. The user intended these 18120b57cec5SDimitry Andric // intrinsics to operate on a vector "as-if" loaded by (V)LDR, 18130b57cec5SDimitry Andric // but we load as-if (V)LD1. So we should swap all arguments and 18140b57cec5SDimitry Andric // swap the return value too. 18150b57cec5SDimitry Andric // 18160b57cec5SDimitry Andric // If we call sub-intrinsics, we should call a version that does 18170b57cec5SDimitry Andric // not re-swap the arguments! 18180b57cec5SDimitry Andric generateImpl(true, "", "__noswap_"); 18190b57cec5SDimitry Andric 18200b57cec5SDimitry Andric // If we're needed early, create a non-swapping variant for 18210b57cec5SDimitry Andric // big-endian. 18220b57cec5SDimitry Andric if (NeededEarly) { 18230b57cec5SDimitry Andric generateImpl(false, "__noswap_", "__noswap_"); 18240b57cec5SDimitry Andric } 18250b57cec5SDimitry Andric OS << "#endif\n\n"; 18260b57cec5SDimitry Andric 18270b57cec5SDimitry Andric return OS.str(); 18280b57cec5SDimitry Andric } 18290b57cec5SDimitry Andric 18300b57cec5SDimitry Andric void Intrinsic::generateImpl(bool ReverseArguments, 18310b57cec5SDimitry Andric StringRef NamePrefix, StringRef CallPrefix) { 18320b57cec5SDimitry Andric CurrentRecord = R; 18330b57cec5SDimitry Andric 18340b57cec5SDimitry Andric // If we call a macro, our local variables may be corrupted due to 18350b57cec5SDimitry Andric // lack of proper lexical scoping. So, add a globally unique postfix 18360b57cec5SDimitry Andric // to every variable. 18370b57cec5SDimitry Andric // 18380b57cec5SDimitry Andric // indexBody() should have set up the Dependencies set by now. 18390b57cec5SDimitry Andric for (auto *I : Dependencies) 18400b57cec5SDimitry Andric if (I->UseMacro) { 18410b57cec5SDimitry Andric VariablePostfix = "_" + utostr(Emitter.getUniqueNumber()); 18420b57cec5SDimitry Andric break; 18430b57cec5SDimitry Andric } 18440b57cec5SDimitry Andric 18450b57cec5SDimitry Andric initVariables(); 18460b57cec5SDimitry Andric 18470b57cec5SDimitry Andric emitPrototype(NamePrefix); 18480b57cec5SDimitry Andric 18490b57cec5SDimitry Andric if (IsUnavailable) { 18500b57cec5SDimitry Andric OS << " __attribute__((unavailable));"; 18510b57cec5SDimitry Andric } else { 18520b57cec5SDimitry Andric emitOpeningBrace(); 18530b57cec5SDimitry Andric emitShadowedArgs(); 18540b57cec5SDimitry Andric if (ReverseArguments) 18550b57cec5SDimitry Andric emitArgumentReversal(); 18560b57cec5SDimitry Andric emitBody(CallPrefix); 18570b57cec5SDimitry Andric if (ReverseArguments) 18580b57cec5SDimitry Andric emitReturnReversal(); 18590b57cec5SDimitry Andric emitReturn(); 18600b57cec5SDimitry Andric emitClosingBrace(); 18610b57cec5SDimitry Andric } 18620b57cec5SDimitry Andric OS << "\n"; 18630b57cec5SDimitry Andric 18640b57cec5SDimitry Andric CurrentRecord = nullptr; 18650b57cec5SDimitry Andric } 18660b57cec5SDimitry Andric 18670b57cec5SDimitry Andric void Intrinsic::indexBody() { 18680b57cec5SDimitry Andric CurrentRecord = R; 18690b57cec5SDimitry Andric 18700b57cec5SDimitry Andric initVariables(); 18710b57cec5SDimitry Andric emitBody(""); 18720b57cec5SDimitry Andric OS.str(""); 18730b57cec5SDimitry Andric 18740b57cec5SDimitry Andric CurrentRecord = nullptr; 18750b57cec5SDimitry Andric } 18760b57cec5SDimitry Andric 18770b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 18780b57cec5SDimitry Andric // NeonEmitter implementation 18790b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 18800b57cec5SDimitry Andric 1881*5ffd83dbSDimitry Andric Intrinsic &NeonEmitter::getIntrinsic(StringRef Name, ArrayRef<Type> Types, 1882*5ffd83dbSDimitry Andric Optional<std::string> MangledName) { 18830b57cec5SDimitry Andric // First, look up the name in the intrinsic map. 18840b57cec5SDimitry Andric assert_with_loc(IntrinsicMap.find(Name.str()) != IntrinsicMap.end(), 18850b57cec5SDimitry Andric ("Intrinsic '" + Name + "' not found!").str()); 18860b57cec5SDimitry Andric auto &V = IntrinsicMap.find(Name.str())->second; 18870b57cec5SDimitry Andric std::vector<Intrinsic *> GoodVec; 18880b57cec5SDimitry Andric 18890b57cec5SDimitry Andric // Create a string to print if we end up failing. 18900b57cec5SDimitry Andric std::string ErrMsg = "looking up intrinsic '" + Name.str() + "("; 18910b57cec5SDimitry Andric for (unsigned I = 0; I < Types.size(); ++I) { 18920b57cec5SDimitry Andric if (I != 0) 18930b57cec5SDimitry Andric ErrMsg += ", "; 18940b57cec5SDimitry Andric ErrMsg += Types[I].str(); 18950b57cec5SDimitry Andric } 18960b57cec5SDimitry Andric ErrMsg += ")'\n"; 18970b57cec5SDimitry Andric ErrMsg += "Available overloads:\n"; 18980b57cec5SDimitry Andric 18990b57cec5SDimitry Andric // Now, look through each intrinsic implementation and see if the types are 19000b57cec5SDimitry Andric // compatible. 19010b57cec5SDimitry Andric for (auto &I : V) { 19020b57cec5SDimitry Andric ErrMsg += " - " + I.getReturnType().str() + " " + I.getMangledName(); 19030b57cec5SDimitry Andric ErrMsg += "("; 19040b57cec5SDimitry Andric for (unsigned A = 0; A < I.getNumParams(); ++A) { 19050b57cec5SDimitry Andric if (A != 0) 19060b57cec5SDimitry Andric ErrMsg += ", "; 19070b57cec5SDimitry Andric ErrMsg += I.getParamType(A).str(); 19080b57cec5SDimitry Andric } 19090b57cec5SDimitry Andric ErrMsg += ")\n"; 19100b57cec5SDimitry Andric 1911*5ffd83dbSDimitry Andric if (MangledName && MangledName != I.getMangledName(true)) 1912*5ffd83dbSDimitry Andric continue; 1913*5ffd83dbSDimitry Andric 19140b57cec5SDimitry Andric if (I.getNumParams() != Types.size()) 19150b57cec5SDimitry Andric continue; 19160b57cec5SDimitry Andric 1917*5ffd83dbSDimitry Andric unsigned ArgNum = 0; 1918*5ffd83dbSDimitry Andric bool MatchingArgumentTypes = 1919*5ffd83dbSDimitry Andric std::all_of(Types.begin(), Types.end(), [&](const auto &Type) { 1920*5ffd83dbSDimitry Andric return Type == I.getParamType(ArgNum++); 1921*5ffd83dbSDimitry Andric }); 1922*5ffd83dbSDimitry Andric 1923*5ffd83dbSDimitry Andric if (MatchingArgumentTypes) 19240b57cec5SDimitry Andric GoodVec.push_back(&I); 19250b57cec5SDimitry Andric } 19260b57cec5SDimitry Andric 19270b57cec5SDimitry Andric assert_with_loc(!GoodVec.empty(), 19280b57cec5SDimitry Andric "No compatible intrinsic found - " + ErrMsg); 19290b57cec5SDimitry Andric assert_with_loc(GoodVec.size() == 1, "Multiple overloads found - " + ErrMsg); 19300b57cec5SDimitry Andric 19310b57cec5SDimitry Andric return *GoodVec.front(); 19320b57cec5SDimitry Andric } 19330b57cec5SDimitry Andric 19340b57cec5SDimitry Andric void NeonEmitter::createIntrinsic(Record *R, 19350b57cec5SDimitry Andric SmallVectorImpl<Intrinsic *> &Out) { 1936*5ffd83dbSDimitry Andric std::string Name = std::string(R->getValueAsString("Name")); 1937*5ffd83dbSDimitry Andric std::string Proto = std::string(R->getValueAsString("Prototype")); 1938*5ffd83dbSDimitry Andric std::string Types = std::string(R->getValueAsString("Types")); 19390b57cec5SDimitry Andric Record *OperationRec = R->getValueAsDef("Operation"); 19400b57cec5SDimitry Andric bool BigEndianSafe = R->getValueAsBit("BigEndianSafe"); 1941*5ffd83dbSDimitry Andric std::string Guard = std::string(R->getValueAsString("ArchGuard")); 19420b57cec5SDimitry Andric bool IsUnavailable = OperationRec->getValueAsBit("Unavailable"); 1943*5ffd83dbSDimitry Andric std::string CartesianProductWith = std::string(R->getValueAsString("CartesianProductWith")); 19440b57cec5SDimitry Andric 19450b57cec5SDimitry Andric // Set the global current record. This allows assert_with_loc to produce 19460b57cec5SDimitry Andric // decent location information even when highly nested. 19470b57cec5SDimitry Andric CurrentRecord = R; 19480b57cec5SDimitry Andric 19490b57cec5SDimitry Andric ListInit *Body = OperationRec->getValueAsListInit("Ops"); 19500b57cec5SDimitry Andric 19510b57cec5SDimitry Andric std::vector<TypeSpec> TypeSpecs = TypeSpec::fromTypeSpecs(Types); 19520b57cec5SDimitry Andric 19530b57cec5SDimitry Andric ClassKind CK = ClassNone; 19540b57cec5SDimitry Andric if (R->getSuperClasses().size() >= 2) 19550b57cec5SDimitry Andric CK = ClassMap[R->getSuperClasses()[1].first]; 19560b57cec5SDimitry Andric 19570b57cec5SDimitry Andric std::vector<std::pair<TypeSpec, TypeSpec>> NewTypeSpecs; 1958*5ffd83dbSDimitry Andric if (!CartesianProductWith.empty()) { 1959*5ffd83dbSDimitry Andric std::vector<TypeSpec> ProductTypeSpecs = TypeSpec::fromTypeSpecs(CartesianProductWith); 19600b57cec5SDimitry Andric for (auto TS : TypeSpecs) { 1961480093f4SDimitry Andric Type DefaultT(TS, "."); 1962*5ffd83dbSDimitry Andric for (auto SrcTS : ProductTypeSpecs) { 1963480093f4SDimitry Andric Type DefaultSrcT(SrcTS, "."); 19640b57cec5SDimitry Andric if (TS == SrcTS || 19650b57cec5SDimitry Andric DefaultSrcT.getSizeInBits() != DefaultT.getSizeInBits()) 19660b57cec5SDimitry Andric continue; 19670b57cec5SDimitry Andric NewTypeSpecs.push_back(std::make_pair(TS, SrcTS)); 19680b57cec5SDimitry Andric } 1969*5ffd83dbSDimitry Andric } 19700b57cec5SDimitry Andric } else { 1971*5ffd83dbSDimitry Andric for (auto TS : TypeSpecs) { 19720b57cec5SDimitry Andric NewTypeSpecs.push_back(std::make_pair(TS, TS)); 19730b57cec5SDimitry Andric } 19740b57cec5SDimitry Andric } 19750b57cec5SDimitry Andric 19760b57cec5SDimitry Andric llvm::sort(NewTypeSpecs); 19770b57cec5SDimitry Andric NewTypeSpecs.erase(std::unique(NewTypeSpecs.begin(), NewTypeSpecs.end()), 19780b57cec5SDimitry Andric NewTypeSpecs.end()); 19790b57cec5SDimitry Andric auto &Entry = IntrinsicMap[Name]; 19800b57cec5SDimitry Andric 19810b57cec5SDimitry Andric for (auto &I : NewTypeSpecs) { 19820b57cec5SDimitry Andric Entry.emplace_back(R, Name, Proto, I.first, I.second, CK, Body, *this, 19830b57cec5SDimitry Andric Guard, IsUnavailable, BigEndianSafe); 19840b57cec5SDimitry Andric Out.push_back(&Entry.back()); 19850b57cec5SDimitry Andric } 19860b57cec5SDimitry Andric 19870b57cec5SDimitry Andric CurrentRecord = nullptr; 19880b57cec5SDimitry Andric } 19890b57cec5SDimitry Andric 19900b57cec5SDimitry Andric /// genBuiltinsDef: Generate the BuiltinsARM.def and BuiltinsAArch64.def 19910b57cec5SDimitry Andric /// declaration of builtins, checking for unique builtin declarations. 19920b57cec5SDimitry Andric void NeonEmitter::genBuiltinsDef(raw_ostream &OS, 19930b57cec5SDimitry Andric SmallVectorImpl<Intrinsic *> &Defs) { 19940b57cec5SDimitry Andric OS << "#ifdef GET_NEON_BUILTINS\n"; 19950b57cec5SDimitry Andric 19960b57cec5SDimitry Andric // We only want to emit a builtin once, and we want to emit them in 19970b57cec5SDimitry Andric // alphabetical order, so use a std::set. 19980b57cec5SDimitry Andric std::set<std::string> Builtins; 19990b57cec5SDimitry Andric 20000b57cec5SDimitry Andric for (auto *Def : Defs) { 20010b57cec5SDimitry Andric if (Def->hasBody()) 20020b57cec5SDimitry Andric continue; 20030b57cec5SDimitry Andric 20040b57cec5SDimitry Andric std::string S = "BUILTIN(__builtin_neon_" + Def->getMangledName() + ", \""; 20050b57cec5SDimitry Andric 20060b57cec5SDimitry Andric S += Def->getBuiltinTypeStr(); 20070b57cec5SDimitry Andric S += "\", \"n\")"; 20080b57cec5SDimitry Andric 20090b57cec5SDimitry Andric Builtins.insert(S); 20100b57cec5SDimitry Andric } 20110b57cec5SDimitry Andric 20120b57cec5SDimitry Andric for (auto &S : Builtins) 20130b57cec5SDimitry Andric OS << S << "\n"; 20140b57cec5SDimitry Andric OS << "#endif\n\n"; 20150b57cec5SDimitry Andric } 20160b57cec5SDimitry Andric 20170b57cec5SDimitry Andric /// Generate the ARM and AArch64 overloaded type checking code for 20180b57cec5SDimitry Andric /// SemaChecking.cpp, checking for unique builtin declarations. 20190b57cec5SDimitry Andric void NeonEmitter::genOverloadTypeCheckCode(raw_ostream &OS, 20200b57cec5SDimitry Andric SmallVectorImpl<Intrinsic *> &Defs) { 20210b57cec5SDimitry Andric OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n"; 20220b57cec5SDimitry Andric 20230b57cec5SDimitry Andric // We record each overload check line before emitting because subsequent Inst 20240b57cec5SDimitry Andric // definitions may extend the number of permitted types (i.e. augment the 20250b57cec5SDimitry Andric // Mask). Use std::map to avoid sorting the table by hash number. 20260b57cec5SDimitry Andric struct OverloadInfo { 20270b57cec5SDimitry Andric uint64_t Mask; 20280b57cec5SDimitry Andric int PtrArgNum; 20290b57cec5SDimitry Andric bool HasConstPtr; 20300b57cec5SDimitry Andric OverloadInfo() : Mask(0ULL), PtrArgNum(0), HasConstPtr(false) {} 20310b57cec5SDimitry Andric }; 20320b57cec5SDimitry Andric std::map<std::string, OverloadInfo> OverloadMap; 20330b57cec5SDimitry Andric 20340b57cec5SDimitry Andric for (auto *Def : Defs) { 20350b57cec5SDimitry Andric // If the def has a body (that is, it has Operation DAGs), it won't call 20360b57cec5SDimitry Andric // __builtin_neon_* so we don't need to generate a definition for it. 20370b57cec5SDimitry Andric if (Def->hasBody()) 20380b57cec5SDimitry Andric continue; 20390b57cec5SDimitry Andric // Functions which have a scalar argument cannot be overloaded, no need to 20400b57cec5SDimitry Andric // check them if we are emitting the type checking code. 20410b57cec5SDimitry Andric if (Def->protoHasScalar()) 20420b57cec5SDimitry Andric continue; 20430b57cec5SDimitry Andric 20440b57cec5SDimitry Andric uint64_t Mask = 0ULL; 2045480093f4SDimitry Andric Mask |= 1ULL << Def->getPolymorphicKeyType().getNeonEnum(); 20460b57cec5SDimitry Andric 20470b57cec5SDimitry Andric // Check if the function has a pointer or const pointer argument. 20480b57cec5SDimitry Andric int PtrArgNum = -1; 20490b57cec5SDimitry Andric bool HasConstPtr = false; 20500b57cec5SDimitry Andric for (unsigned I = 0; I < Def->getNumParams(); ++I) { 2051480093f4SDimitry Andric const auto &Type = Def->getParamType(I); 2052480093f4SDimitry Andric if (Type.isPointer()) { 20530b57cec5SDimitry Andric PtrArgNum = I; 2054480093f4SDimitry Andric HasConstPtr = Type.isConstPointer(); 20550b57cec5SDimitry Andric } 20560b57cec5SDimitry Andric } 2057480093f4SDimitry Andric 20580b57cec5SDimitry Andric // For sret builtins, adjust the pointer argument index. 20590b57cec5SDimitry Andric if (PtrArgNum >= 0 && Def->getReturnType().getNumVectors() > 1) 20600b57cec5SDimitry Andric PtrArgNum += 1; 20610b57cec5SDimitry Andric 20620b57cec5SDimitry Andric std::string Name = Def->getName(); 20630b57cec5SDimitry Andric // Omit type checking for the pointer arguments of vld1_lane, vld1_dup, 20640b57cec5SDimitry Andric // and vst1_lane intrinsics. Using a pointer to the vector element 20650b57cec5SDimitry Andric // type with one of those operations causes codegen to select an aligned 20660b57cec5SDimitry Andric // load/store instruction. If you want an unaligned operation, 20670b57cec5SDimitry Andric // the pointer argument needs to have less alignment than element type, 20680b57cec5SDimitry Andric // so just accept any pointer type. 20690b57cec5SDimitry Andric if (Name == "vld1_lane" || Name == "vld1_dup" || Name == "vst1_lane") { 20700b57cec5SDimitry Andric PtrArgNum = -1; 20710b57cec5SDimitry Andric HasConstPtr = false; 20720b57cec5SDimitry Andric } 20730b57cec5SDimitry Andric 20740b57cec5SDimitry Andric if (Mask) { 20750b57cec5SDimitry Andric std::string Name = Def->getMangledName(); 20760b57cec5SDimitry Andric OverloadMap.insert(std::make_pair(Name, OverloadInfo())); 20770b57cec5SDimitry Andric OverloadInfo &OI = OverloadMap[Name]; 20780b57cec5SDimitry Andric OI.Mask |= Mask; 20790b57cec5SDimitry Andric OI.PtrArgNum |= PtrArgNum; 20800b57cec5SDimitry Andric OI.HasConstPtr = HasConstPtr; 20810b57cec5SDimitry Andric } 20820b57cec5SDimitry Andric } 20830b57cec5SDimitry Andric 20840b57cec5SDimitry Andric for (auto &I : OverloadMap) { 20850b57cec5SDimitry Andric OverloadInfo &OI = I.second; 20860b57cec5SDimitry Andric 20870b57cec5SDimitry Andric OS << "case NEON::BI__builtin_neon_" << I.first << ": "; 20880b57cec5SDimitry Andric OS << "mask = 0x" << Twine::utohexstr(OI.Mask) << "ULL"; 20890b57cec5SDimitry Andric if (OI.PtrArgNum >= 0) 20900b57cec5SDimitry Andric OS << "; PtrArgNum = " << OI.PtrArgNum; 20910b57cec5SDimitry Andric if (OI.HasConstPtr) 20920b57cec5SDimitry Andric OS << "; HasConstPtr = true"; 20930b57cec5SDimitry Andric OS << "; break;\n"; 20940b57cec5SDimitry Andric } 20950b57cec5SDimitry Andric OS << "#endif\n\n"; 20960b57cec5SDimitry Andric } 20970b57cec5SDimitry Andric 20980b57cec5SDimitry Andric void NeonEmitter::genIntrinsicRangeCheckCode(raw_ostream &OS, 20990b57cec5SDimitry Andric SmallVectorImpl<Intrinsic *> &Defs) { 21000b57cec5SDimitry Andric OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n"; 21010b57cec5SDimitry Andric 21020b57cec5SDimitry Andric std::set<std::string> Emitted; 21030b57cec5SDimitry Andric 21040b57cec5SDimitry Andric for (auto *Def : Defs) { 21050b57cec5SDimitry Andric if (Def->hasBody()) 21060b57cec5SDimitry Andric continue; 21070b57cec5SDimitry Andric // Functions which do not have an immediate do not need to have range 21080b57cec5SDimitry Andric // checking code emitted. 21090b57cec5SDimitry Andric if (!Def->hasImmediate()) 21100b57cec5SDimitry Andric continue; 21110b57cec5SDimitry Andric if (Emitted.find(Def->getMangledName()) != Emitted.end()) 21120b57cec5SDimitry Andric continue; 21130b57cec5SDimitry Andric 21140b57cec5SDimitry Andric std::string LowerBound, UpperBound; 21150b57cec5SDimitry Andric 21160b57cec5SDimitry Andric Record *R = Def->getRecord(); 21170b57cec5SDimitry Andric if (R->getValueAsBit("isVCVT_N")) { 21180b57cec5SDimitry Andric // VCVT between floating- and fixed-point values takes an immediate 21190b57cec5SDimitry Andric // in the range [1, 32) for f32 or [1, 64) for f64 or [1, 16) for f16. 21200b57cec5SDimitry Andric LowerBound = "1"; 21210b57cec5SDimitry Andric if (Def->getBaseType().getElementSizeInBits() == 16 || 21220b57cec5SDimitry Andric Def->getName().find('h') != std::string::npos) 21230b57cec5SDimitry Andric // VCVTh operating on FP16 intrinsics in range [1, 16) 21240b57cec5SDimitry Andric UpperBound = "15"; 21250b57cec5SDimitry Andric else if (Def->getBaseType().getElementSizeInBits() == 32) 21260b57cec5SDimitry Andric UpperBound = "31"; 21270b57cec5SDimitry Andric else 21280b57cec5SDimitry Andric UpperBound = "63"; 21290b57cec5SDimitry Andric } else if (R->getValueAsBit("isScalarShift")) { 21300b57cec5SDimitry Andric // Right shifts have an 'r' in the name, left shifts do not. Convert 21310b57cec5SDimitry Andric // instructions have the same bounds and right shifts. 21320b57cec5SDimitry Andric if (Def->getName().find('r') != std::string::npos || 21330b57cec5SDimitry Andric Def->getName().find("cvt") != std::string::npos) 21340b57cec5SDimitry Andric LowerBound = "1"; 21350b57cec5SDimitry Andric 21360b57cec5SDimitry Andric UpperBound = utostr(Def->getReturnType().getElementSizeInBits() - 1); 21370b57cec5SDimitry Andric } else if (R->getValueAsBit("isShift")) { 21380b57cec5SDimitry Andric // Builtins which are overloaded by type will need to have their upper 21390b57cec5SDimitry Andric // bound computed at Sema time based on the type constant. 21400b57cec5SDimitry Andric 21410b57cec5SDimitry Andric // Right shifts have an 'r' in the name, left shifts do not. 21420b57cec5SDimitry Andric if (Def->getName().find('r') != std::string::npos) 21430b57cec5SDimitry Andric LowerBound = "1"; 21440b57cec5SDimitry Andric UpperBound = "RFT(TV, true)"; 21450b57cec5SDimitry Andric } else if (Def->getClassKind(true) == ClassB) { 21460b57cec5SDimitry Andric // ClassB intrinsics have a type (and hence lane number) that is only 21470b57cec5SDimitry Andric // known at runtime. 21480b57cec5SDimitry Andric if (R->getValueAsBit("isLaneQ")) 21490b57cec5SDimitry Andric UpperBound = "RFT(TV, false, true)"; 21500b57cec5SDimitry Andric else 21510b57cec5SDimitry Andric UpperBound = "RFT(TV, false, false)"; 21520b57cec5SDimitry Andric } else { 21530b57cec5SDimitry Andric // The immediate generally refers to a lane in the preceding argument. 21540b57cec5SDimitry Andric assert(Def->getImmediateIdx() > 0); 21550b57cec5SDimitry Andric Type T = Def->getParamType(Def->getImmediateIdx() - 1); 21560b57cec5SDimitry Andric UpperBound = utostr(T.getNumElements() - 1); 21570b57cec5SDimitry Andric } 21580b57cec5SDimitry Andric 21590b57cec5SDimitry Andric // Calculate the index of the immediate that should be range checked. 21600b57cec5SDimitry Andric unsigned Idx = Def->getNumParams(); 21610b57cec5SDimitry Andric if (Def->hasImmediate()) 21620b57cec5SDimitry Andric Idx = Def->getGeneratedParamIdx(Def->getImmediateIdx()); 21630b57cec5SDimitry Andric 21640b57cec5SDimitry Andric OS << "case NEON::BI__builtin_neon_" << Def->getMangledName() << ": " 21650b57cec5SDimitry Andric << "i = " << Idx << ";"; 21660b57cec5SDimitry Andric if (!LowerBound.empty()) 21670b57cec5SDimitry Andric OS << " l = " << LowerBound << ";"; 21680b57cec5SDimitry Andric if (!UpperBound.empty()) 21690b57cec5SDimitry Andric OS << " u = " << UpperBound << ";"; 21700b57cec5SDimitry Andric OS << " break;\n"; 21710b57cec5SDimitry Andric 21720b57cec5SDimitry Andric Emitted.insert(Def->getMangledName()); 21730b57cec5SDimitry Andric } 21740b57cec5SDimitry Andric 21750b57cec5SDimitry Andric OS << "#endif\n\n"; 21760b57cec5SDimitry Andric } 21770b57cec5SDimitry Andric 21780b57cec5SDimitry Andric /// runHeader - Emit a file with sections defining: 21790b57cec5SDimitry Andric /// 1. the NEON section of BuiltinsARM.def and BuiltinsAArch64.def. 21800b57cec5SDimitry Andric /// 2. the SemaChecking code for the type overload checking. 21810b57cec5SDimitry Andric /// 3. the SemaChecking code for validation of intrinsic immediate arguments. 21820b57cec5SDimitry Andric void NeonEmitter::runHeader(raw_ostream &OS) { 21830b57cec5SDimitry Andric std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst"); 21840b57cec5SDimitry Andric 21850b57cec5SDimitry Andric SmallVector<Intrinsic *, 128> Defs; 21860b57cec5SDimitry Andric for (auto *R : RV) 21870b57cec5SDimitry Andric createIntrinsic(R, Defs); 21880b57cec5SDimitry Andric 21890b57cec5SDimitry Andric // Generate shared BuiltinsXXX.def 21900b57cec5SDimitry Andric genBuiltinsDef(OS, Defs); 21910b57cec5SDimitry Andric 21920b57cec5SDimitry Andric // Generate ARM overloaded type checking code for SemaChecking.cpp 21930b57cec5SDimitry Andric genOverloadTypeCheckCode(OS, Defs); 21940b57cec5SDimitry Andric 21950b57cec5SDimitry Andric // Generate ARM range checking code for shift/lane immediates. 21960b57cec5SDimitry Andric genIntrinsicRangeCheckCode(OS, Defs); 21970b57cec5SDimitry Andric } 21980b57cec5SDimitry Andric 2199*5ffd83dbSDimitry Andric static void emitNeonTypeDefs(const std::string& types, raw_ostream &OS) { 2200*5ffd83dbSDimitry Andric std::string TypedefTypes(types); 2201*5ffd83dbSDimitry Andric std::vector<TypeSpec> TDTypeVec = TypeSpec::fromTypeSpecs(TypedefTypes); 2202*5ffd83dbSDimitry Andric 2203*5ffd83dbSDimitry Andric // Emit vector typedefs. 2204*5ffd83dbSDimitry Andric bool InIfdef = false; 2205*5ffd83dbSDimitry Andric for (auto &TS : TDTypeVec) { 2206*5ffd83dbSDimitry Andric bool IsA64 = false; 2207*5ffd83dbSDimitry Andric Type T(TS, "."); 2208*5ffd83dbSDimitry Andric if (T.isDouble()) 2209*5ffd83dbSDimitry Andric IsA64 = true; 2210*5ffd83dbSDimitry Andric 2211*5ffd83dbSDimitry Andric if (InIfdef && !IsA64) { 2212*5ffd83dbSDimitry Andric OS << "#endif\n"; 2213*5ffd83dbSDimitry Andric InIfdef = false; 2214*5ffd83dbSDimitry Andric } 2215*5ffd83dbSDimitry Andric if (!InIfdef && IsA64) { 2216*5ffd83dbSDimitry Andric OS << "#ifdef __aarch64__\n"; 2217*5ffd83dbSDimitry Andric InIfdef = true; 2218*5ffd83dbSDimitry Andric } 2219*5ffd83dbSDimitry Andric 2220*5ffd83dbSDimitry Andric if (T.isPoly()) 2221*5ffd83dbSDimitry Andric OS << "typedef __attribute__((neon_polyvector_type("; 2222*5ffd83dbSDimitry Andric else 2223*5ffd83dbSDimitry Andric OS << "typedef __attribute__((neon_vector_type("; 2224*5ffd83dbSDimitry Andric 2225*5ffd83dbSDimitry Andric Type T2 = T; 2226*5ffd83dbSDimitry Andric T2.makeScalar(); 2227*5ffd83dbSDimitry Andric OS << T.getNumElements() << "))) "; 2228*5ffd83dbSDimitry Andric OS << T2.str(); 2229*5ffd83dbSDimitry Andric OS << " " << T.str() << ";\n"; 2230*5ffd83dbSDimitry Andric } 2231*5ffd83dbSDimitry Andric if (InIfdef) 2232*5ffd83dbSDimitry Andric OS << "#endif\n"; 2233*5ffd83dbSDimitry Andric OS << "\n"; 2234*5ffd83dbSDimitry Andric 2235*5ffd83dbSDimitry Andric // Emit struct typedefs. 2236*5ffd83dbSDimitry Andric InIfdef = false; 2237*5ffd83dbSDimitry Andric for (unsigned NumMembers = 2; NumMembers <= 4; ++NumMembers) { 2238*5ffd83dbSDimitry Andric for (auto &TS : TDTypeVec) { 2239*5ffd83dbSDimitry Andric bool IsA64 = false; 2240*5ffd83dbSDimitry Andric Type T(TS, "."); 2241*5ffd83dbSDimitry Andric if (T.isDouble()) 2242*5ffd83dbSDimitry Andric IsA64 = true; 2243*5ffd83dbSDimitry Andric 2244*5ffd83dbSDimitry Andric if (InIfdef && !IsA64) { 2245*5ffd83dbSDimitry Andric OS << "#endif\n"; 2246*5ffd83dbSDimitry Andric InIfdef = false; 2247*5ffd83dbSDimitry Andric } 2248*5ffd83dbSDimitry Andric if (!InIfdef && IsA64) { 2249*5ffd83dbSDimitry Andric OS << "#ifdef __aarch64__\n"; 2250*5ffd83dbSDimitry Andric InIfdef = true; 2251*5ffd83dbSDimitry Andric } 2252*5ffd83dbSDimitry Andric 2253*5ffd83dbSDimitry Andric const char Mods[] = { static_cast<char>('2' + (NumMembers - 2)), 0}; 2254*5ffd83dbSDimitry Andric Type VT(TS, Mods); 2255*5ffd83dbSDimitry Andric OS << "typedef struct " << VT.str() << " {\n"; 2256*5ffd83dbSDimitry Andric OS << " " << T.str() << " val"; 2257*5ffd83dbSDimitry Andric OS << "[" << NumMembers << "]"; 2258*5ffd83dbSDimitry Andric OS << ";\n} "; 2259*5ffd83dbSDimitry Andric OS << VT.str() << ";\n"; 2260*5ffd83dbSDimitry Andric OS << "\n"; 2261*5ffd83dbSDimitry Andric } 2262*5ffd83dbSDimitry Andric } 2263*5ffd83dbSDimitry Andric if (InIfdef) 2264*5ffd83dbSDimitry Andric OS << "#endif\n"; 2265*5ffd83dbSDimitry Andric } 2266*5ffd83dbSDimitry Andric 22670b57cec5SDimitry Andric /// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h 22680b57cec5SDimitry Andric /// is comprised of type definitions and function declarations. 22690b57cec5SDimitry Andric void NeonEmitter::run(raw_ostream &OS) { 22700b57cec5SDimitry Andric OS << "/*===---- arm_neon.h - ARM Neon intrinsics " 22710b57cec5SDimitry Andric "------------------------------" 22720b57cec5SDimitry Andric "---===\n" 22730b57cec5SDimitry Andric " *\n" 22740b57cec5SDimitry Andric " * Permission is hereby granted, free of charge, to any person " 22750b57cec5SDimitry Andric "obtaining " 22760b57cec5SDimitry Andric "a copy\n" 22770b57cec5SDimitry Andric " * of this software and associated documentation files (the " 22780b57cec5SDimitry Andric "\"Software\")," 22790b57cec5SDimitry Andric " to deal\n" 22800b57cec5SDimitry Andric " * in the Software without restriction, including without limitation " 22810b57cec5SDimitry Andric "the " 22820b57cec5SDimitry Andric "rights\n" 22830b57cec5SDimitry Andric " * to use, copy, modify, merge, publish, distribute, sublicense, " 22840b57cec5SDimitry Andric "and/or sell\n" 22850b57cec5SDimitry Andric " * copies of the Software, and to permit persons to whom the Software " 22860b57cec5SDimitry Andric "is\n" 22870b57cec5SDimitry Andric " * furnished to do so, subject to the following conditions:\n" 22880b57cec5SDimitry Andric " *\n" 22890b57cec5SDimitry Andric " * The above copyright notice and this permission notice shall be " 22900b57cec5SDimitry Andric "included in\n" 22910b57cec5SDimitry Andric " * all copies or substantial portions of the Software.\n" 22920b57cec5SDimitry Andric " *\n" 22930b57cec5SDimitry Andric " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, " 22940b57cec5SDimitry Andric "EXPRESS OR\n" 22950b57cec5SDimitry Andric " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF " 22960b57cec5SDimitry Andric "MERCHANTABILITY,\n" 22970b57cec5SDimitry Andric " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT " 22980b57cec5SDimitry Andric "SHALL THE\n" 22990b57cec5SDimitry Andric " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR " 23000b57cec5SDimitry Andric "OTHER\n" 23010b57cec5SDimitry Andric " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, " 23020b57cec5SDimitry Andric "ARISING FROM,\n" 23030b57cec5SDimitry Andric " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER " 23040b57cec5SDimitry Andric "DEALINGS IN\n" 23050b57cec5SDimitry Andric " * THE SOFTWARE.\n" 23060b57cec5SDimitry Andric " *\n" 23070b57cec5SDimitry Andric " *===-----------------------------------------------------------------" 23080b57cec5SDimitry Andric "---" 23090b57cec5SDimitry Andric "---===\n" 23100b57cec5SDimitry Andric " */\n\n"; 23110b57cec5SDimitry Andric 23120b57cec5SDimitry Andric OS << "#ifndef __ARM_NEON_H\n"; 23130b57cec5SDimitry Andric OS << "#define __ARM_NEON_H\n\n"; 23140b57cec5SDimitry Andric 2315*5ffd83dbSDimitry Andric OS << "#ifndef __ARM_FP\n"; 2316*5ffd83dbSDimitry Andric OS << "#error \"NEON intrinsics not available with the soft-float ABI. " 2317*5ffd83dbSDimitry Andric "Please use -mfloat-abi=softfp or -mfloat-abi=hard\"\n"; 2318*5ffd83dbSDimitry Andric OS << "#else\n\n"; 2319*5ffd83dbSDimitry Andric 23200b57cec5SDimitry Andric OS << "#if !defined(__ARM_NEON)\n"; 23210b57cec5SDimitry Andric OS << "#error \"NEON support not enabled\"\n"; 2322*5ffd83dbSDimitry Andric OS << "#else\n\n"; 23230b57cec5SDimitry Andric 23240b57cec5SDimitry Andric OS << "#include <stdint.h>\n\n"; 23250b57cec5SDimitry Andric 2326*5ffd83dbSDimitry Andric OS << "#ifdef __ARM_FEATURE_BF16\n"; 2327*5ffd83dbSDimitry Andric OS << "#include <arm_bf16.h>\n"; 2328*5ffd83dbSDimitry Andric OS << "typedef __bf16 bfloat16_t;\n"; 2329*5ffd83dbSDimitry Andric OS << "#endif\n\n"; 2330*5ffd83dbSDimitry Andric 23310b57cec5SDimitry Andric // Emit NEON-specific scalar typedefs. 23320b57cec5SDimitry Andric OS << "typedef float float32_t;\n"; 23330b57cec5SDimitry Andric OS << "typedef __fp16 float16_t;\n"; 23340b57cec5SDimitry Andric 23350b57cec5SDimitry Andric OS << "#ifdef __aarch64__\n"; 23360b57cec5SDimitry Andric OS << "typedef double float64_t;\n"; 23370b57cec5SDimitry Andric OS << "#endif\n\n"; 23380b57cec5SDimitry Andric 23390b57cec5SDimitry Andric // For now, signedness of polynomial types depends on target 23400b57cec5SDimitry Andric OS << "#ifdef __aarch64__\n"; 23410b57cec5SDimitry Andric OS << "typedef uint8_t poly8_t;\n"; 23420b57cec5SDimitry Andric OS << "typedef uint16_t poly16_t;\n"; 23430b57cec5SDimitry Andric OS << "typedef uint64_t poly64_t;\n"; 23440b57cec5SDimitry Andric OS << "typedef __uint128_t poly128_t;\n"; 23450b57cec5SDimitry Andric OS << "#else\n"; 23460b57cec5SDimitry Andric OS << "typedef int8_t poly8_t;\n"; 23470b57cec5SDimitry Andric OS << "typedef int16_t poly16_t;\n"; 2348*5ffd83dbSDimitry Andric OS << "typedef int64_t poly64_t;\n"; 23490b57cec5SDimitry Andric OS << "#endif\n"; 23500b57cec5SDimitry Andric 2351*5ffd83dbSDimitry Andric emitNeonTypeDefs("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfdQdPcQPcPsQPsPlQPl", OS); 23520b57cec5SDimitry Andric 2353*5ffd83dbSDimitry Andric OS << "#ifdef __ARM_FEATURE_BF16\n"; 2354*5ffd83dbSDimitry Andric emitNeonTypeDefs("bQb", OS); 2355*5ffd83dbSDimitry Andric OS << "#endif\n\n"; 23560b57cec5SDimitry Andric 23570b57cec5SDimitry Andric OS << "#define __ai static __inline__ __attribute__((__always_inline__, " 23580b57cec5SDimitry Andric "__nodebug__))\n\n"; 23590b57cec5SDimitry Andric 23600b57cec5SDimitry Andric SmallVector<Intrinsic *, 128> Defs; 23610b57cec5SDimitry Andric std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst"); 23620b57cec5SDimitry Andric for (auto *R : RV) 23630b57cec5SDimitry Andric createIntrinsic(R, Defs); 23640b57cec5SDimitry Andric 23650b57cec5SDimitry Andric for (auto *I : Defs) 23660b57cec5SDimitry Andric I->indexBody(); 23670b57cec5SDimitry Andric 2368a7dea167SDimitry Andric llvm::stable_sort(Defs, llvm::deref<std::less<>>()); 23690b57cec5SDimitry Andric 23700b57cec5SDimitry Andric // Only emit a def when its requirements have been met. 23710b57cec5SDimitry Andric // FIXME: This loop could be made faster, but it's fast enough for now. 23720b57cec5SDimitry Andric bool MadeProgress = true; 23730b57cec5SDimitry Andric std::string InGuard; 23740b57cec5SDimitry Andric while (!Defs.empty() && MadeProgress) { 23750b57cec5SDimitry Andric MadeProgress = false; 23760b57cec5SDimitry Andric 23770b57cec5SDimitry Andric for (SmallVector<Intrinsic *, 128>::iterator I = Defs.begin(); 23780b57cec5SDimitry Andric I != Defs.end(); /*No step*/) { 23790b57cec5SDimitry Andric bool DependenciesSatisfied = true; 23800b57cec5SDimitry Andric for (auto *II : (*I)->getDependencies()) { 23810b57cec5SDimitry Andric if (llvm::is_contained(Defs, II)) 23820b57cec5SDimitry Andric DependenciesSatisfied = false; 23830b57cec5SDimitry Andric } 23840b57cec5SDimitry Andric if (!DependenciesSatisfied) { 23850b57cec5SDimitry Andric // Try the next one. 23860b57cec5SDimitry Andric ++I; 23870b57cec5SDimitry Andric continue; 23880b57cec5SDimitry Andric } 23890b57cec5SDimitry Andric 23900b57cec5SDimitry Andric // Emit #endif/#if pair if needed. 23910b57cec5SDimitry Andric if ((*I)->getGuard() != InGuard) { 23920b57cec5SDimitry Andric if (!InGuard.empty()) 23930b57cec5SDimitry Andric OS << "#endif\n"; 23940b57cec5SDimitry Andric InGuard = (*I)->getGuard(); 23950b57cec5SDimitry Andric if (!InGuard.empty()) 23960b57cec5SDimitry Andric OS << "#if " << InGuard << "\n"; 23970b57cec5SDimitry Andric } 23980b57cec5SDimitry Andric 23990b57cec5SDimitry Andric // Actually generate the intrinsic code. 24000b57cec5SDimitry Andric OS << (*I)->generate(); 24010b57cec5SDimitry Andric 24020b57cec5SDimitry Andric MadeProgress = true; 24030b57cec5SDimitry Andric I = Defs.erase(I); 24040b57cec5SDimitry Andric } 24050b57cec5SDimitry Andric } 24060b57cec5SDimitry Andric assert(Defs.empty() && "Some requirements were not satisfied!"); 24070b57cec5SDimitry Andric if (!InGuard.empty()) 24080b57cec5SDimitry Andric OS << "#endif\n"; 24090b57cec5SDimitry Andric 24100b57cec5SDimitry Andric OS << "\n"; 24110b57cec5SDimitry Andric OS << "#undef __ai\n\n"; 2412*5ffd83dbSDimitry Andric OS << "#endif /* if !defined(__ARM_NEON) */\n"; 2413*5ffd83dbSDimitry Andric OS << "#endif /* ifndef __ARM_FP */\n"; 24140b57cec5SDimitry Andric OS << "#endif /* __ARM_NEON_H */\n"; 24150b57cec5SDimitry Andric } 24160b57cec5SDimitry Andric 24170b57cec5SDimitry Andric /// run - Read the records in arm_fp16.td and output arm_fp16.h. arm_fp16.h 24180b57cec5SDimitry Andric /// is comprised of type definitions and function declarations. 24190b57cec5SDimitry Andric void NeonEmitter::runFP16(raw_ostream &OS) { 24200b57cec5SDimitry Andric OS << "/*===---- arm_fp16.h - ARM FP16 intrinsics " 24210b57cec5SDimitry Andric "------------------------------" 24220b57cec5SDimitry Andric "---===\n" 24230b57cec5SDimitry Andric " *\n" 24240b57cec5SDimitry Andric " * Permission is hereby granted, free of charge, to any person " 24250b57cec5SDimitry Andric "obtaining a copy\n" 24260b57cec5SDimitry Andric " * of this software and associated documentation files (the " 24270b57cec5SDimitry Andric "\"Software\"), to deal\n" 24280b57cec5SDimitry Andric " * in the Software without restriction, including without limitation " 24290b57cec5SDimitry Andric "the rights\n" 24300b57cec5SDimitry Andric " * to use, copy, modify, merge, publish, distribute, sublicense, " 24310b57cec5SDimitry Andric "and/or sell\n" 24320b57cec5SDimitry Andric " * copies of the Software, and to permit persons to whom the Software " 24330b57cec5SDimitry Andric "is\n" 24340b57cec5SDimitry Andric " * furnished to do so, subject to the following conditions:\n" 24350b57cec5SDimitry Andric " *\n" 24360b57cec5SDimitry Andric " * The above copyright notice and this permission notice shall be " 24370b57cec5SDimitry Andric "included in\n" 24380b57cec5SDimitry Andric " * all copies or substantial portions of the Software.\n" 24390b57cec5SDimitry Andric " *\n" 24400b57cec5SDimitry Andric " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, " 24410b57cec5SDimitry Andric "EXPRESS OR\n" 24420b57cec5SDimitry Andric " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF " 24430b57cec5SDimitry Andric "MERCHANTABILITY,\n" 24440b57cec5SDimitry Andric " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT " 24450b57cec5SDimitry Andric "SHALL THE\n" 24460b57cec5SDimitry Andric " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR " 24470b57cec5SDimitry Andric "OTHER\n" 24480b57cec5SDimitry Andric " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, " 24490b57cec5SDimitry Andric "ARISING FROM,\n" 24500b57cec5SDimitry Andric " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER " 24510b57cec5SDimitry Andric "DEALINGS IN\n" 24520b57cec5SDimitry Andric " * THE SOFTWARE.\n" 24530b57cec5SDimitry Andric " *\n" 24540b57cec5SDimitry Andric " *===-----------------------------------------------------------------" 24550b57cec5SDimitry Andric "---" 24560b57cec5SDimitry Andric "---===\n" 24570b57cec5SDimitry Andric " */\n\n"; 24580b57cec5SDimitry Andric 24590b57cec5SDimitry Andric OS << "#ifndef __ARM_FP16_H\n"; 24600b57cec5SDimitry Andric OS << "#define __ARM_FP16_H\n\n"; 24610b57cec5SDimitry Andric 24620b57cec5SDimitry Andric OS << "#include <stdint.h>\n\n"; 24630b57cec5SDimitry Andric 24640b57cec5SDimitry Andric OS << "typedef __fp16 float16_t;\n"; 24650b57cec5SDimitry Andric 24660b57cec5SDimitry Andric OS << "#define __ai static __inline__ __attribute__((__always_inline__, " 24670b57cec5SDimitry Andric "__nodebug__))\n\n"; 24680b57cec5SDimitry Andric 24690b57cec5SDimitry Andric SmallVector<Intrinsic *, 128> Defs; 24700b57cec5SDimitry Andric std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst"); 24710b57cec5SDimitry Andric for (auto *R : RV) 24720b57cec5SDimitry Andric createIntrinsic(R, Defs); 24730b57cec5SDimitry Andric 24740b57cec5SDimitry Andric for (auto *I : Defs) 24750b57cec5SDimitry Andric I->indexBody(); 24760b57cec5SDimitry Andric 2477a7dea167SDimitry Andric llvm::stable_sort(Defs, llvm::deref<std::less<>>()); 24780b57cec5SDimitry Andric 24790b57cec5SDimitry Andric // Only emit a def when its requirements have been met. 24800b57cec5SDimitry Andric // FIXME: This loop could be made faster, but it's fast enough for now. 24810b57cec5SDimitry Andric bool MadeProgress = true; 24820b57cec5SDimitry Andric std::string InGuard; 24830b57cec5SDimitry Andric while (!Defs.empty() && MadeProgress) { 24840b57cec5SDimitry Andric MadeProgress = false; 24850b57cec5SDimitry Andric 24860b57cec5SDimitry Andric for (SmallVector<Intrinsic *, 128>::iterator I = Defs.begin(); 24870b57cec5SDimitry Andric I != Defs.end(); /*No step*/) { 24880b57cec5SDimitry Andric bool DependenciesSatisfied = true; 24890b57cec5SDimitry Andric for (auto *II : (*I)->getDependencies()) { 24900b57cec5SDimitry Andric if (llvm::is_contained(Defs, II)) 24910b57cec5SDimitry Andric DependenciesSatisfied = false; 24920b57cec5SDimitry Andric } 24930b57cec5SDimitry Andric if (!DependenciesSatisfied) { 24940b57cec5SDimitry Andric // Try the next one. 24950b57cec5SDimitry Andric ++I; 24960b57cec5SDimitry Andric continue; 24970b57cec5SDimitry Andric } 24980b57cec5SDimitry Andric 24990b57cec5SDimitry Andric // Emit #endif/#if pair if needed. 25000b57cec5SDimitry Andric if ((*I)->getGuard() != InGuard) { 25010b57cec5SDimitry Andric if (!InGuard.empty()) 25020b57cec5SDimitry Andric OS << "#endif\n"; 25030b57cec5SDimitry Andric InGuard = (*I)->getGuard(); 25040b57cec5SDimitry Andric if (!InGuard.empty()) 25050b57cec5SDimitry Andric OS << "#if " << InGuard << "\n"; 25060b57cec5SDimitry Andric } 25070b57cec5SDimitry Andric 25080b57cec5SDimitry Andric // Actually generate the intrinsic code. 25090b57cec5SDimitry Andric OS << (*I)->generate(); 25100b57cec5SDimitry Andric 25110b57cec5SDimitry Andric MadeProgress = true; 25120b57cec5SDimitry Andric I = Defs.erase(I); 25130b57cec5SDimitry Andric } 25140b57cec5SDimitry Andric } 25150b57cec5SDimitry Andric assert(Defs.empty() && "Some requirements were not satisfied!"); 25160b57cec5SDimitry Andric if (!InGuard.empty()) 25170b57cec5SDimitry Andric OS << "#endif\n"; 25180b57cec5SDimitry Andric 25190b57cec5SDimitry Andric OS << "\n"; 25200b57cec5SDimitry Andric OS << "#undef __ai\n\n"; 25210b57cec5SDimitry Andric OS << "#endif /* __ARM_FP16_H */\n"; 25220b57cec5SDimitry Andric } 25230b57cec5SDimitry Andric 2524*5ffd83dbSDimitry Andric void NeonEmitter::runBF16(raw_ostream &OS) { 2525*5ffd83dbSDimitry Andric OS << "/*===---- arm_bf16.h - ARM BF16 intrinsics " 2526*5ffd83dbSDimitry Andric "-----------------------------------===\n" 2527*5ffd83dbSDimitry Andric " *\n" 2528*5ffd83dbSDimitry Andric " *\n" 2529*5ffd83dbSDimitry Andric " * Part of the LLVM Project, under the Apache License v2.0 with LLVM " 2530*5ffd83dbSDimitry Andric "Exceptions.\n" 2531*5ffd83dbSDimitry Andric " * See https://llvm.org/LICENSE.txt for license information.\n" 2532*5ffd83dbSDimitry Andric " * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n" 2533*5ffd83dbSDimitry Andric " *\n" 2534*5ffd83dbSDimitry Andric " *===-----------------------------------------------------------------" 2535*5ffd83dbSDimitry Andric "------===\n" 2536*5ffd83dbSDimitry Andric " */\n\n"; 2537*5ffd83dbSDimitry Andric 2538*5ffd83dbSDimitry Andric OS << "#ifndef __ARM_BF16_H\n"; 2539*5ffd83dbSDimitry Andric OS << "#define __ARM_BF16_H\n\n"; 2540*5ffd83dbSDimitry Andric 2541*5ffd83dbSDimitry Andric OS << "typedef __bf16 bfloat16_t;\n"; 2542*5ffd83dbSDimitry Andric 2543*5ffd83dbSDimitry Andric OS << "#define __ai static __inline__ __attribute__((__always_inline__, " 2544*5ffd83dbSDimitry Andric "__nodebug__))\n\n"; 2545*5ffd83dbSDimitry Andric 2546*5ffd83dbSDimitry Andric SmallVector<Intrinsic *, 128> Defs; 2547*5ffd83dbSDimitry Andric std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst"); 2548*5ffd83dbSDimitry Andric for (auto *R : RV) 2549*5ffd83dbSDimitry Andric createIntrinsic(R, Defs); 2550*5ffd83dbSDimitry Andric 2551*5ffd83dbSDimitry Andric for (auto *I : Defs) 2552*5ffd83dbSDimitry Andric I->indexBody(); 2553*5ffd83dbSDimitry Andric 2554*5ffd83dbSDimitry Andric llvm::stable_sort(Defs, llvm::deref<std::less<>>()); 2555*5ffd83dbSDimitry Andric 2556*5ffd83dbSDimitry Andric // Only emit a def when its requirements have been met. 2557*5ffd83dbSDimitry Andric // FIXME: This loop could be made faster, but it's fast enough for now. 2558*5ffd83dbSDimitry Andric bool MadeProgress = true; 2559*5ffd83dbSDimitry Andric std::string InGuard; 2560*5ffd83dbSDimitry Andric while (!Defs.empty() && MadeProgress) { 2561*5ffd83dbSDimitry Andric MadeProgress = false; 2562*5ffd83dbSDimitry Andric 2563*5ffd83dbSDimitry Andric for (SmallVector<Intrinsic *, 128>::iterator I = Defs.begin(); 2564*5ffd83dbSDimitry Andric I != Defs.end(); /*No step*/) { 2565*5ffd83dbSDimitry Andric bool DependenciesSatisfied = true; 2566*5ffd83dbSDimitry Andric for (auto *II : (*I)->getDependencies()) { 2567*5ffd83dbSDimitry Andric if (llvm::is_contained(Defs, II)) 2568*5ffd83dbSDimitry Andric DependenciesSatisfied = false; 2569*5ffd83dbSDimitry Andric } 2570*5ffd83dbSDimitry Andric if (!DependenciesSatisfied) { 2571*5ffd83dbSDimitry Andric // Try the next one. 2572*5ffd83dbSDimitry Andric ++I; 2573*5ffd83dbSDimitry Andric continue; 2574*5ffd83dbSDimitry Andric } 2575*5ffd83dbSDimitry Andric 2576*5ffd83dbSDimitry Andric // Emit #endif/#if pair if needed. 2577*5ffd83dbSDimitry Andric if ((*I)->getGuard() != InGuard) { 2578*5ffd83dbSDimitry Andric if (!InGuard.empty()) 2579*5ffd83dbSDimitry Andric OS << "#endif\n"; 2580*5ffd83dbSDimitry Andric InGuard = (*I)->getGuard(); 2581*5ffd83dbSDimitry Andric if (!InGuard.empty()) 2582*5ffd83dbSDimitry Andric OS << "#if " << InGuard << "\n"; 2583*5ffd83dbSDimitry Andric } 2584*5ffd83dbSDimitry Andric 2585*5ffd83dbSDimitry Andric // Actually generate the intrinsic code. 2586*5ffd83dbSDimitry Andric OS << (*I)->generate(); 2587*5ffd83dbSDimitry Andric 2588*5ffd83dbSDimitry Andric MadeProgress = true; 2589*5ffd83dbSDimitry Andric I = Defs.erase(I); 2590*5ffd83dbSDimitry Andric } 2591*5ffd83dbSDimitry Andric } 2592*5ffd83dbSDimitry Andric assert(Defs.empty() && "Some requirements were not satisfied!"); 2593*5ffd83dbSDimitry Andric if (!InGuard.empty()) 2594*5ffd83dbSDimitry Andric OS << "#endif\n"; 2595*5ffd83dbSDimitry Andric 2596*5ffd83dbSDimitry Andric OS << "\n"; 2597*5ffd83dbSDimitry Andric OS << "#undef __ai\n\n"; 2598*5ffd83dbSDimitry Andric 2599*5ffd83dbSDimitry Andric OS << "#endif\n"; 2600*5ffd83dbSDimitry Andric } 2601*5ffd83dbSDimitry Andric 2602a7dea167SDimitry Andric void clang::EmitNeon(RecordKeeper &Records, raw_ostream &OS) { 26030b57cec5SDimitry Andric NeonEmitter(Records).run(OS); 26040b57cec5SDimitry Andric } 26050b57cec5SDimitry Andric 2606a7dea167SDimitry Andric void clang::EmitFP16(RecordKeeper &Records, raw_ostream &OS) { 26070b57cec5SDimitry Andric NeonEmitter(Records).runFP16(OS); 26080b57cec5SDimitry Andric } 26090b57cec5SDimitry Andric 2610*5ffd83dbSDimitry Andric void clang::EmitBF16(RecordKeeper &Records, raw_ostream &OS) { 2611*5ffd83dbSDimitry Andric NeonEmitter(Records).runBF16(OS); 2612*5ffd83dbSDimitry Andric } 2613*5ffd83dbSDimitry Andric 2614a7dea167SDimitry Andric void clang::EmitNeonSema(RecordKeeper &Records, raw_ostream &OS) { 26150b57cec5SDimitry Andric NeonEmitter(Records).runHeader(OS); 26160b57cec5SDimitry Andric } 26170b57cec5SDimitry Andric 2618a7dea167SDimitry Andric void clang::EmitNeonTest(RecordKeeper &Records, raw_ostream &OS) { 26190b57cec5SDimitry Andric llvm_unreachable("Neon test generation no longer implemented!"); 26200b57cec5SDimitry Andric } 2621