1*67e74705SXin Li //===- NeonEmitter.cpp - Generate arm_neon.h for use with clang -*- C++ -*-===//
2*67e74705SXin Li //
3*67e74705SXin Li // The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // This tablegen backend is responsible for emitting arm_neon.h, which includes
11*67e74705SXin Li // a declaration and definition of each function specified by the ARM NEON
12*67e74705SXin Li // compiler interface. See ARM document DUI0348B.
13*67e74705SXin Li //
14*67e74705SXin Li // Each NEON instruction is implemented in terms of 1 or more functions which
15*67e74705SXin Li // are suffixed with the element type of the input vectors. Functions may be
16*67e74705SXin Li // implemented in terms of generic vector operations such as +, *, -, etc. or
17*67e74705SXin Li // by calling a __builtin_-prefixed function which will be handled by clang's
18*67e74705SXin Li // CodeGen library.
19*67e74705SXin Li //
20*67e74705SXin Li // Additional validation code can be generated by this file when runHeader() is
21*67e74705SXin Li // called, rather than the normal run() entry point.
22*67e74705SXin Li //
23*67e74705SXin Li // See also the documentation in include/clang/Basic/arm_neon.td.
24*67e74705SXin Li //
25*67e74705SXin Li //===----------------------------------------------------------------------===//
26*67e74705SXin Li
27*67e74705SXin Li #include "llvm/ADT/DenseMap.h"
28*67e74705SXin Li #include "llvm/ADT/STLExtras.h"
29*67e74705SXin Li #include "llvm/ADT/SmallString.h"
30*67e74705SXin Li #include "llvm/ADT/SmallVector.h"
31*67e74705SXin Li #include "llvm/ADT/StringExtras.h"
32*67e74705SXin Li #include "llvm/ADT/StringMap.h"
33*67e74705SXin Li #include "llvm/Support/ErrorHandling.h"
34*67e74705SXin Li #include "llvm/TableGen/Error.h"
35*67e74705SXin Li #include "llvm/TableGen/Record.h"
36*67e74705SXin Li #include "llvm/TableGen/SetTheory.h"
37*67e74705SXin Li #include "llvm/TableGen/TableGenBackend.h"
38*67e74705SXin Li #include <algorithm>
39*67e74705SXin Li #include <deque>
40*67e74705SXin Li #include <map>
41*67e74705SXin Li #include <sstream>
42*67e74705SXin Li #include <string>
43*67e74705SXin Li #include <utility>
44*67e74705SXin Li #include <vector>
45*67e74705SXin Li using namespace llvm;
46*67e74705SXin Li
47*67e74705SXin Li namespace {
48*67e74705SXin Li
49*67e74705SXin Li // While globals are generally bad, this one allows us to perform assertions
50*67e74705SXin Li // liberally and somehow still trace them back to the def they indirectly
51*67e74705SXin Li // came from.
52*67e74705SXin Li static Record *CurrentRecord = nullptr;
assert_with_loc(bool Assertion,const std::string & Str)53*67e74705SXin Li static void assert_with_loc(bool Assertion, const std::string &Str) {
54*67e74705SXin Li if (!Assertion) {
55*67e74705SXin Li if (CurrentRecord)
56*67e74705SXin Li PrintFatalError(CurrentRecord->getLoc(), Str);
57*67e74705SXin Li else
58*67e74705SXin Li PrintFatalError(Str);
59*67e74705SXin Li }
60*67e74705SXin Li }
61*67e74705SXin Li
62*67e74705SXin Li enum ClassKind {
63*67e74705SXin Li ClassNone,
64*67e74705SXin Li ClassI, // generic integer instruction, e.g., "i8" suffix
65*67e74705SXin Li ClassS, // signed/unsigned/poly, e.g., "s8", "u8" or "p8" suffix
66*67e74705SXin Li ClassW, // width-specific instruction, e.g., "8" suffix
67*67e74705SXin Li ClassB, // bitcast arguments with enum argument to specify type
68*67e74705SXin Li ClassL, // Logical instructions which are op instructions
69*67e74705SXin Li // but we need to not emit any suffix for in our
70*67e74705SXin Li // tests.
71*67e74705SXin Li ClassNoTest // Instructions which we do not test since they are
72*67e74705SXin Li // not TRUE instructions.
73*67e74705SXin Li };
74*67e74705SXin Li
75*67e74705SXin Li /// NeonTypeFlags - Flags to identify the types for overloaded Neon
76*67e74705SXin Li /// builtins. These must be kept in sync with the flags in
77*67e74705SXin Li /// include/clang/Basic/TargetBuiltins.h.
78*67e74705SXin Li namespace NeonTypeFlags {
79*67e74705SXin Li enum { EltTypeMask = 0xf, UnsignedFlag = 0x10, QuadFlag = 0x20 };
80*67e74705SXin Li
81*67e74705SXin Li enum EltType {
82*67e74705SXin Li Int8,
83*67e74705SXin Li Int16,
84*67e74705SXin Li Int32,
85*67e74705SXin Li Int64,
86*67e74705SXin Li Poly8,
87*67e74705SXin Li Poly16,
88*67e74705SXin Li Poly64,
89*67e74705SXin Li Poly128,
90*67e74705SXin Li Float16,
91*67e74705SXin Li Float32,
92*67e74705SXin Li Float64
93*67e74705SXin Li };
94*67e74705SXin Li }
95*67e74705SXin Li
96*67e74705SXin Li class Intrinsic;
97*67e74705SXin Li class NeonEmitter;
98*67e74705SXin Li class Type;
99*67e74705SXin Li class Variable;
100*67e74705SXin Li
101*67e74705SXin Li //===----------------------------------------------------------------------===//
102*67e74705SXin Li // TypeSpec
103*67e74705SXin Li //===----------------------------------------------------------------------===//
104*67e74705SXin Li
105*67e74705SXin Li /// A TypeSpec is just a simple wrapper around a string, but gets its own type
106*67e74705SXin Li /// for strong typing purposes.
107*67e74705SXin Li ///
108*67e74705SXin Li /// A TypeSpec can be used to create a type.
109*67e74705SXin Li class TypeSpec : public std::string {
110*67e74705SXin Li public:
fromTypeSpecs(StringRef Str)111*67e74705SXin Li static std::vector<TypeSpec> fromTypeSpecs(StringRef Str) {
112*67e74705SXin Li std::vector<TypeSpec> Ret;
113*67e74705SXin Li TypeSpec Acc;
114*67e74705SXin Li for (char I : Str.str()) {
115*67e74705SXin Li if (islower(I)) {
116*67e74705SXin Li Acc.push_back(I);
117*67e74705SXin Li Ret.push_back(TypeSpec(Acc));
118*67e74705SXin Li Acc.clear();
119*67e74705SXin Li } else {
120*67e74705SXin Li Acc.push_back(I);
121*67e74705SXin Li }
122*67e74705SXin Li }
123*67e74705SXin Li return Ret;
124*67e74705SXin Li }
125*67e74705SXin Li };
126*67e74705SXin Li
127*67e74705SXin Li //===----------------------------------------------------------------------===//
128*67e74705SXin Li // Type
129*67e74705SXin Li //===----------------------------------------------------------------------===//
130*67e74705SXin Li
131*67e74705SXin Li /// A Type. Not much more to say here.
132*67e74705SXin Li class Type {
133*67e74705SXin Li private:
134*67e74705SXin Li TypeSpec TS;
135*67e74705SXin Li
136*67e74705SXin Li bool Float, Signed, Immediate, Void, Poly, Constant, Pointer;
137*67e74705SXin Li // ScalarForMangling and NoManglingQ are really not suited to live here as
138*67e74705SXin Li // they are not related to the type. But they live in the TypeSpec (not the
139*67e74705SXin Li // prototype), so this is really the only place to store them.
140*67e74705SXin Li bool ScalarForMangling, NoManglingQ;
141*67e74705SXin Li unsigned Bitwidth, ElementBitwidth, NumVectors;
142*67e74705SXin Li
143*67e74705SXin Li public:
Type()144*67e74705SXin Li Type()
145*67e74705SXin Li : Float(false), Signed(false), Immediate(false), Void(true), Poly(false),
146*67e74705SXin Li Constant(false), Pointer(false), ScalarForMangling(false),
147*67e74705SXin Li NoManglingQ(false), Bitwidth(0), ElementBitwidth(0), NumVectors(0) {}
148*67e74705SXin Li
Type(TypeSpec TS,char CharMod)149*67e74705SXin Li Type(TypeSpec TS, char CharMod)
150*67e74705SXin Li : TS(std::move(TS)), Float(false), Signed(false), Immediate(false),
151*67e74705SXin Li Void(false), Poly(false), Constant(false), Pointer(false),
152*67e74705SXin Li ScalarForMangling(false), NoManglingQ(false), Bitwidth(0),
153*67e74705SXin Li ElementBitwidth(0), NumVectors(0) {
154*67e74705SXin Li applyModifier(CharMod);
155*67e74705SXin Li }
156*67e74705SXin Li
157*67e74705SXin Li /// Returns a type representing "void".
getVoid()158*67e74705SXin Li static Type getVoid() { return Type(); }
159*67e74705SXin Li
operator ==(const Type & Other) const160*67e74705SXin Li bool operator==(const Type &Other) const { return str() == Other.str(); }
operator !=(const Type & Other) const161*67e74705SXin Li bool operator!=(const Type &Other) const { return !operator==(Other); }
162*67e74705SXin Li
163*67e74705SXin Li //
164*67e74705SXin Li // Query functions
165*67e74705SXin Li //
isScalarForMangling() const166*67e74705SXin Li bool isScalarForMangling() const { return ScalarForMangling; }
noManglingQ() const167*67e74705SXin Li bool noManglingQ() const { return NoManglingQ; }
168*67e74705SXin Li
isPointer() const169*67e74705SXin Li bool isPointer() const { return Pointer; }
isFloating() const170*67e74705SXin Li bool isFloating() const { return Float; }
isInteger() const171*67e74705SXin Li bool isInteger() const { return !Float && !Poly; }
isSigned() const172*67e74705SXin Li bool isSigned() const { return Signed; }
isImmediate() const173*67e74705SXin Li bool isImmediate() const { return Immediate; }
isScalar() const174*67e74705SXin Li bool isScalar() const { return NumVectors == 0; }
isVector() const175*67e74705SXin Li bool isVector() const { return NumVectors > 0; }
isFloat() const176*67e74705SXin Li bool isFloat() const { return Float && ElementBitwidth == 32; }
isDouble() const177*67e74705SXin Li bool isDouble() const { return Float && ElementBitwidth == 64; }
isHalf() const178*67e74705SXin Li bool isHalf() const { return Float && ElementBitwidth == 16; }
isPoly() const179*67e74705SXin Li bool isPoly() const { return Poly; }
isChar() const180*67e74705SXin Li bool isChar() const { return ElementBitwidth == 8; }
isShort() const181*67e74705SXin Li bool isShort() const { return !Float && ElementBitwidth == 16; }
isInt() const182*67e74705SXin Li bool isInt() const { return !Float && ElementBitwidth == 32; }
isLong() const183*67e74705SXin Li bool isLong() const { return !Float && ElementBitwidth == 64; }
isVoid() const184*67e74705SXin Li bool isVoid() const { return Void; }
getNumElements() const185*67e74705SXin Li unsigned getNumElements() const { return Bitwidth / ElementBitwidth; }
getSizeInBits() const186*67e74705SXin Li unsigned getSizeInBits() const { return Bitwidth; }
getElementSizeInBits() const187*67e74705SXin Li unsigned getElementSizeInBits() const { return ElementBitwidth; }
getNumVectors() const188*67e74705SXin Li unsigned getNumVectors() const { return NumVectors; }
189*67e74705SXin Li
190*67e74705SXin Li //
191*67e74705SXin Li // Mutator functions
192*67e74705SXin Li //
makeUnsigned()193*67e74705SXin Li void makeUnsigned() { Signed = false; }
makeSigned()194*67e74705SXin Li void makeSigned() { Signed = true; }
makeInteger(unsigned ElemWidth,bool Sign)195*67e74705SXin Li void makeInteger(unsigned ElemWidth, bool Sign) {
196*67e74705SXin Li Float = false;
197*67e74705SXin Li Poly = false;
198*67e74705SXin Li Signed = Sign;
199*67e74705SXin Li Immediate = false;
200*67e74705SXin Li ElementBitwidth = ElemWidth;
201*67e74705SXin Li }
makeImmediate(unsigned ElemWidth)202*67e74705SXin Li void makeImmediate(unsigned ElemWidth) {
203*67e74705SXin Li Float = false;
204*67e74705SXin Li Poly = false;
205*67e74705SXin Li Signed = true;
206*67e74705SXin Li Immediate = true;
207*67e74705SXin Li ElementBitwidth = ElemWidth;
208*67e74705SXin Li }
makeScalar()209*67e74705SXin Li void makeScalar() {
210*67e74705SXin Li Bitwidth = ElementBitwidth;
211*67e74705SXin Li NumVectors = 0;
212*67e74705SXin Li }
makeOneVector()213*67e74705SXin Li void makeOneVector() {
214*67e74705SXin Li assert(isVector());
215*67e74705SXin Li NumVectors = 1;
216*67e74705SXin Li }
doubleLanes()217*67e74705SXin Li void doubleLanes() {
218*67e74705SXin Li assert_with_loc(Bitwidth != 128, "Can't get bigger than 128!");
219*67e74705SXin Li Bitwidth = 128;
220*67e74705SXin Li }
halveLanes()221*67e74705SXin Li void halveLanes() {
222*67e74705SXin Li assert_with_loc(Bitwidth != 64, "Can't get smaller than 64!");
223*67e74705SXin Li Bitwidth = 64;
224*67e74705SXin Li }
225*67e74705SXin Li
226*67e74705SXin Li /// Return the C string representation of a type, which is the typename
227*67e74705SXin Li /// defined in stdint.h or arm_neon.h.
228*67e74705SXin Li std::string str() const;
229*67e74705SXin Li
230*67e74705SXin Li /// Return the string representation of a type, which is an encoded
231*67e74705SXin Li /// string for passing to the BUILTIN() macro in Builtins.def.
232*67e74705SXin Li std::string builtin_str() const;
233*67e74705SXin Li
234*67e74705SXin Li /// Return the value in NeonTypeFlags for this type.
235*67e74705SXin Li unsigned getNeonEnum() const;
236*67e74705SXin Li
237*67e74705SXin Li /// Parse a type from a stdint.h or arm_neon.h typedef name,
238*67e74705SXin Li /// for example uint32x2_t or int64_t.
239*67e74705SXin Li static Type fromTypedefName(StringRef Name);
240*67e74705SXin Li
241*67e74705SXin Li private:
242*67e74705SXin Li /// Creates the type based on the typespec string in TS.
243*67e74705SXin Li /// Sets "Quad" to true if the "Q" or "H" modifiers were
244*67e74705SXin Li /// seen. This is needed by applyModifier as some modifiers
245*67e74705SXin Li /// only take effect if the type size was changed by "Q" or "H".
246*67e74705SXin Li void applyTypespec(bool &Quad);
247*67e74705SXin Li /// Applies a prototype modifier to the type.
248*67e74705SXin Li void applyModifier(char Mod);
249*67e74705SXin Li };
250*67e74705SXin Li
251*67e74705SXin Li //===----------------------------------------------------------------------===//
252*67e74705SXin Li // Variable
253*67e74705SXin Li //===----------------------------------------------------------------------===//
254*67e74705SXin Li
255*67e74705SXin Li /// A variable is a simple class that just has a type and a name.
256*67e74705SXin Li class Variable {
257*67e74705SXin Li Type T;
258*67e74705SXin Li std::string N;
259*67e74705SXin Li
260*67e74705SXin Li public:
Variable()261*67e74705SXin Li Variable() : T(Type::getVoid()), N("") {}
Variable(Type T,std::string N)262*67e74705SXin Li Variable(Type T, std::string N) : T(std::move(T)), N(std::move(N)) {}
263*67e74705SXin Li
getType() const264*67e74705SXin Li Type getType() const { return T; }
getName() const265*67e74705SXin Li std::string getName() const { return "__" + N; }
266*67e74705SXin Li };
267*67e74705SXin Li
268*67e74705SXin Li //===----------------------------------------------------------------------===//
269*67e74705SXin Li // Intrinsic
270*67e74705SXin Li //===----------------------------------------------------------------------===//
271*67e74705SXin Li
272*67e74705SXin Li /// The main grunt class. This represents an instantiation of an intrinsic with
273*67e74705SXin Li /// a particular typespec and prototype.
274*67e74705SXin Li class Intrinsic {
275*67e74705SXin Li friend class DagEmitter;
276*67e74705SXin Li
277*67e74705SXin Li /// The Record this intrinsic was created from.
278*67e74705SXin Li Record *R;
279*67e74705SXin Li /// The unmangled name and prototype.
280*67e74705SXin Li std::string Name, Proto;
281*67e74705SXin Li /// The input and output typespecs. InTS == OutTS except when
282*67e74705SXin Li /// CartesianProductOfTypes is 1 - this is the case for vreinterpret.
283*67e74705SXin Li TypeSpec OutTS, InTS;
284*67e74705SXin Li /// The base class kind. Most intrinsics use ClassS, which has full type
285*67e74705SXin Li /// info for integers (s32/u32). Some use ClassI, which doesn't care about
286*67e74705SXin Li /// signedness (i32), while some (ClassB) have no type at all, only a width
287*67e74705SXin Li /// (32).
288*67e74705SXin Li ClassKind CK;
289*67e74705SXin Li /// The list of DAGs for the body. May be empty, in which case we should
290*67e74705SXin Li /// emit a builtin call.
291*67e74705SXin Li ListInit *Body;
292*67e74705SXin Li /// The architectural #ifdef guard.
293*67e74705SXin Li std::string Guard;
294*67e74705SXin Li /// Set if the Unvailable bit is 1. This means we don't generate a body,
295*67e74705SXin Li /// just an "unavailable" attribute on a declaration.
296*67e74705SXin Li bool IsUnavailable;
297*67e74705SXin Li /// Is this intrinsic safe for big-endian? or does it need its arguments
298*67e74705SXin Li /// reversing?
299*67e74705SXin Li bool BigEndianSafe;
300*67e74705SXin Li
301*67e74705SXin Li /// The types of return value [0] and parameters [1..].
302*67e74705SXin Li std::vector<Type> Types;
303*67e74705SXin Li /// The local variables defined.
304*67e74705SXin Li std::map<std::string, Variable> Variables;
305*67e74705SXin Li /// NeededEarly - set if any other intrinsic depends on this intrinsic.
306*67e74705SXin Li bool NeededEarly;
307*67e74705SXin Li /// UseMacro - set if we should implement using a macro or unset for a
308*67e74705SXin Li /// function.
309*67e74705SXin Li bool UseMacro;
310*67e74705SXin Li /// The set of intrinsics that this intrinsic uses/requires.
311*67e74705SXin Li std::set<Intrinsic *> Dependencies;
312*67e74705SXin Li /// The "base type", which is Type('d', OutTS). InBaseType is only
313*67e74705SXin Li /// different if CartesianProductOfTypes = 1 (for vreinterpret).
314*67e74705SXin Li Type BaseType, InBaseType;
315*67e74705SXin Li /// The return variable.
316*67e74705SXin Li Variable RetVar;
317*67e74705SXin Li /// A postfix to apply to every variable. Defaults to "".
318*67e74705SXin Li std::string VariablePostfix;
319*67e74705SXin Li
320*67e74705SXin Li NeonEmitter &Emitter;
321*67e74705SXin Li std::stringstream OS;
322*67e74705SXin Li
323*67e74705SXin Li public:
Intrinsic(Record * R,StringRef Name,StringRef Proto,TypeSpec OutTS,TypeSpec InTS,ClassKind CK,ListInit * Body,NeonEmitter & Emitter,StringRef Guard,bool IsUnavailable,bool BigEndianSafe)324*67e74705SXin Li Intrinsic(Record *R, StringRef Name, StringRef Proto, TypeSpec OutTS,
325*67e74705SXin Li TypeSpec InTS, ClassKind CK, ListInit *Body, NeonEmitter &Emitter,
326*67e74705SXin Li StringRef Guard, bool IsUnavailable, bool BigEndianSafe)
327*67e74705SXin Li : R(R), Name(Name.str()), Proto(Proto.str()), OutTS(OutTS), InTS(InTS),
328*67e74705SXin Li CK(CK), Body(Body), Guard(Guard.str()), IsUnavailable(IsUnavailable),
329*67e74705SXin Li BigEndianSafe(BigEndianSafe), NeededEarly(false), UseMacro(false),
330*67e74705SXin Li BaseType(OutTS, 'd'), InBaseType(InTS, 'd'), Emitter(Emitter) {
331*67e74705SXin Li // If this builtin takes an immediate argument, we need to #define it rather
332*67e74705SXin Li // than use a standard declaration, so that SemaChecking can range check
333*67e74705SXin Li // the immediate passed by the user.
334*67e74705SXin Li if (Proto.find('i') != std::string::npos)
335*67e74705SXin Li UseMacro = true;
336*67e74705SXin Li
337*67e74705SXin Li // Pointer arguments need to use macros to avoid hiding aligned attributes
338*67e74705SXin Li // from the pointer type.
339*67e74705SXin Li if (Proto.find('p') != std::string::npos ||
340*67e74705SXin Li Proto.find('c') != std::string::npos)
341*67e74705SXin Li UseMacro = true;
342*67e74705SXin Li
343*67e74705SXin Li // It is not permitted to pass or return an __fp16 by value, so intrinsics
344*67e74705SXin Li // taking a scalar float16_t must be implemented as macros.
345*67e74705SXin Li if (OutTS.find('h') != std::string::npos &&
346*67e74705SXin Li Proto.find('s') != std::string::npos)
347*67e74705SXin Li UseMacro = true;
348*67e74705SXin Li
349*67e74705SXin Li // Modify the TypeSpec per-argument to get a concrete Type, and create
350*67e74705SXin Li // known variables for each.
351*67e74705SXin Li // Types[0] is the return value.
352*67e74705SXin Li Types.emplace_back(OutTS, Proto[0]);
353*67e74705SXin Li for (unsigned I = 1; I < Proto.size(); ++I)
354*67e74705SXin Li Types.emplace_back(InTS, Proto[I]);
355*67e74705SXin Li }
356*67e74705SXin Li
357*67e74705SXin Li /// Get the Record that this intrinsic is based off.
getRecord() const358*67e74705SXin Li Record *getRecord() const { return R; }
359*67e74705SXin Li /// Get the set of Intrinsics that this intrinsic calls.
360*67e74705SXin Li /// this is the set of immediate dependencies, NOT the
361*67e74705SXin Li /// transitive closure.
getDependencies() const362*67e74705SXin Li const std::set<Intrinsic *> &getDependencies() const { return Dependencies; }
363*67e74705SXin Li /// Get the architectural guard string (#ifdef).
getGuard() const364*67e74705SXin Li std::string getGuard() const { return Guard; }
365*67e74705SXin Li /// Get the non-mangled name.
getName() const366*67e74705SXin Li std::string getName() const { return Name; }
367*67e74705SXin Li
368*67e74705SXin Li /// Return true if the intrinsic takes an immediate operand.
hasImmediate() const369*67e74705SXin Li bool hasImmediate() const {
370*67e74705SXin Li return Proto.find('i') != std::string::npos;
371*67e74705SXin Li }
372*67e74705SXin Li /// Return the parameter index of the immediate operand.
getImmediateIdx() const373*67e74705SXin Li unsigned getImmediateIdx() const {
374*67e74705SXin Li assert(hasImmediate());
375*67e74705SXin Li unsigned Idx = Proto.find('i');
376*67e74705SXin Li assert(Idx > 0 && "Can't return an immediate!");
377*67e74705SXin Li return Idx - 1;
378*67e74705SXin Li }
379*67e74705SXin Li
380*67e74705SXin Li /// Return true if the intrinsic takes an splat operand.
hasSplat() const381*67e74705SXin Li bool hasSplat() const { return Proto.find('a') != std::string::npos; }
382*67e74705SXin Li /// Return the parameter index of the splat operand.
getSplatIdx() const383*67e74705SXin Li unsigned getSplatIdx() const {
384*67e74705SXin Li assert(hasSplat());
385*67e74705SXin Li unsigned Idx = Proto.find('a');
386*67e74705SXin Li assert(Idx > 0 && "Can't return a splat!");
387*67e74705SXin Li return Idx - 1;
388*67e74705SXin Li }
389*67e74705SXin Li
getNumParams() const390*67e74705SXin Li unsigned getNumParams() const { return Proto.size() - 1; }
getReturnType() const391*67e74705SXin Li Type getReturnType() const { return Types[0]; }
getParamType(unsigned I) const392*67e74705SXin Li Type getParamType(unsigned I) const { return Types[I + 1]; }
getBaseType() const393*67e74705SXin Li Type getBaseType() const { return BaseType; }
394*67e74705SXin Li /// Return the raw prototype string.
getProto() const395*67e74705SXin Li std::string getProto() const { return Proto; }
396*67e74705SXin Li
397*67e74705SXin Li /// Return true if the prototype has a scalar argument.
398*67e74705SXin Li /// This does not return true for the "splat" code ('a').
399*67e74705SXin Li bool protoHasScalar() const;
400*67e74705SXin Li
401*67e74705SXin Li /// Return the index that parameter PIndex will sit at
402*67e74705SXin Li /// in a generated function call. This is often just PIndex,
403*67e74705SXin Li /// but may not be as things such as multiple-vector operands
404*67e74705SXin Li /// and sret parameters need to be taken into accont.
getGeneratedParamIdx(unsigned PIndex)405*67e74705SXin Li unsigned getGeneratedParamIdx(unsigned PIndex) {
406*67e74705SXin Li unsigned Idx = 0;
407*67e74705SXin Li if (getReturnType().getNumVectors() > 1)
408*67e74705SXin Li // Multiple vectors are passed as sret.
409*67e74705SXin Li ++Idx;
410*67e74705SXin Li
411*67e74705SXin Li for (unsigned I = 0; I < PIndex; ++I)
412*67e74705SXin Li Idx += std::max(1U, getParamType(I).getNumVectors());
413*67e74705SXin Li
414*67e74705SXin Li return Idx;
415*67e74705SXin Li }
416*67e74705SXin Li
hasBody() const417*67e74705SXin Li bool hasBody() const { return Body && Body->getValues().size() > 0; }
418*67e74705SXin Li
setNeededEarly()419*67e74705SXin Li void setNeededEarly() { NeededEarly = true; }
420*67e74705SXin Li
operator <(const Intrinsic & Other) const421*67e74705SXin Li bool operator<(const Intrinsic &Other) const {
422*67e74705SXin Li // Sort lexicographically on a two-tuple (Guard, Name)
423*67e74705SXin Li if (Guard != Other.Guard)
424*67e74705SXin Li return Guard < Other.Guard;
425*67e74705SXin Li return Name < Other.Name;
426*67e74705SXin Li }
427*67e74705SXin Li
getClassKind(bool UseClassBIfScalar=false)428*67e74705SXin Li ClassKind getClassKind(bool UseClassBIfScalar = false) {
429*67e74705SXin Li if (UseClassBIfScalar && !protoHasScalar())
430*67e74705SXin Li return ClassB;
431*67e74705SXin Li return CK;
432*67e74705SXin Li }
433*67e74705SXin Li
434*67e74705SXin Li /// Return the name, mangled with type information.
435*67e74705SXin Li /// If ForceClassS is true, use ClassS (u32/s32) instead
436*67e74705SXin Li /// of the intrinsic's own type class.
437*67e74705SXin Li std::string getMangledName(bool ForceClassS = false) const;
438*67e74705SXin Li /// Return the type code for a builtin function call.
439*67e74705SXin Li std::string getInstTypeCode(Type T, ClassKind CK) const;
440*67e74705SXin Li /// Return the type string for a BUILTIN() macro in Builtins.def.
441*67e74705SXin Li std::string getBuiltinTypeStr();
442*67e74705SXin Li
443*67e74705SXin Li /// Generate the intrinsic, returning code.
444*67e74705SXin Li std::string generate();
445*67e74705SXin Li /// Perform type checking and populate the dependency graph, but
446*67e74705SXin Li /// don't generate code yet.
447*67e74705SXin Li void indexBody();
448*67e74705SXin Li
449*67e74705SXin Li private:
450*67e74705SXin Li std::string mangleName(std::string Name, ClassKind CK) const;
451*67e74705SXin Li
452*67e74705SXin Li void initVariables();
453*67e74705SXin Li std::string replaceParamsIn(std::string S);
454*67e74705SXin Li
455*67e74705SXin Li void emitBodyAsBuiltinCall();
456*67e74705SXin Li
457*67e74705SXin Li void generateImpl(bool ReverseArguments,
458*67e74705SXin Li StringRef NamePrefix, StringRef CallPrefix);
459*67e74705SXin Li void emitReturn();
460*67e74705SXin Li void emitBody(StringRef CallPrefix);
461*67e74705SXin Li void emitShadowedArgs();
462*67e74705SXin Li void emitArgumentReversal();
463*67e74705SXin Li void emitReturnReversal();
464*67e74705SXin Li void emitReverseVariable(Variable &Dest, Variable &Src);
465*67e74705SXin Li void emitNewLine();
466*67e74705SXin Li void emitClosingBrace();
467*67e74705SXin Li void emitOpeningBrace();
468*67e74705SXin Li void emitPrototype(StringRef NamePrefix);
469*67e74705SXin Li
470*67e74705SXin Li class DagEmitter {
471*67e74705SXin Li Intrinsic &Intr;
472*67e74705SXin Li StringRef CallPrefix;
473*67e74705SXin Li
474*67e74705SXin Li public:
DagEmitter(Intrinsic & Intr,StringRef CallPrefix)475*67e74705SXin Li DagEmitter(Intrinsic &Intr, StringRef CallPrefix) :
476*67e74705SXin Li Intr(Intr), CallPrefix(CallPrefix) {
477*67e74705SXin Li }
478*67e74705SXin Li std::pair<Type, std::string> emitDagArg(Init *Arg, std::string ArgName);
479*67e74705SXin Li std::pair<Type, std::string> emitDagSaveTemp(DagInit *DI);
480*67e74705SXin Li std::pair<Type, std::string> emitDagSplat(DagInit *DI);
481*67e74705SXin Li std::pair<Type, std::string> emitDagDup(DagInit *DI);
482*67e74705SXin Li std::pair<Type, std::string> emitDagShuffle(DagInit *DI);
483*67e74705SXin Li std::pair<Type, std::string> emitDagCast(DagInit *DI, bool IsBitCast);
484*67e74705SXin Li std::pair<Type, std::string> emitDagCall(DagInit *DI);
485*67e74705SXin Li std::pair<Type, std::string> emitDagNameReplace(DagInit *DI);
486*67e74705SXin Li std::pair<Type, std::string> emitDagLiteral(DagInit *DI);
487*67e74705SXin Li std::pair<Type, std::string> emitDagOp(DagInit *DI);
488*67e74705SXin Li std::pair<Type, std::string> emitDag(DagInit *DI);
489*67e74705SXin Li };
490*67e74705SXin Li
491*67e74705SXin Li };
492*67e74705SXin Li
493*67e74705SXin Li //===----------------------------------------------------------------------===//
494*67e74705SXin Li // NeonEmitter
495*67e74705SXin Li //===----------------------------------------------------------------------===//
496*67e74705SXin Li
497*67e74705SXin Li class NeonEmitter {
498*67e74705SXin Li RecordKeeper &Records;
499*67e74705SXin Li DenseMap<Record *, ClassKind> ClassMap;
500*67e74705SXin Li std::map<std::string, std::deque<Intrinsic>> IntrinsicMap;
501*67e74705SXin Li unsigned UniqueNumber;
502*67e74705SXin Li
503*67e74705SXin Li void createIntrinsic(Record *R, SmallVectorImpl<Intrinsic *> &Out);
504*67e74705SXin Li void genBuiltinsDef(raw_ostream &OS, SmallVectorImpl<Intrinsic *> &Defs);
505*67e74705SXin Li void genOverloadTypeCheckCode(raw_ostream &OS,
506*67e74705SXin Li SmallVectorImpl<Intrinsic *> &Defs);
507*67e74705SXin Li void genIntrinsicRangeCheckCode(raw_ostream &OS,
508*67e74705SXin Li SmallVectorImpl<Intrinsic *> &Defs);
509*67e74705SXin Li
510*67e74705SXin Li public:
511*67e74705SXin Li /// Called by Intrinsic - this attempts to get an intrinsic that takes
512*67e74705SXin Li /// the given types as arguments.
513*67e74705SXin Li Intrinsic &getIntrinsic(StringRef Name, ArrayRef<Type> Types);
514*67e74705SXin Li
515*67e74705SXin Li /// Called by Intrinsic - returns a globally-unique number.
getUniqueNumber()516*67e74705SXin Li unsigned getUniqueNumber() { return UniqueNumber++; }
517*67e74705SXin Li
NeonEmitter(RecordKeeper & R)518*67e74705SXin Li NeonEmitter(RecordKeeper &R) : Records(R), UniqueNumber(0) {
519*67e74705SXin Li Record *SI = R.getClass("SInst");
520*67e74705SXin Li Record *II = R.getClass("IInst");
521*67e74705SXin Li Record *WI = R.getClass("WInst");
522*67e74705SXin Li Record *SOpI = R.getClass("SOpInst");
523*67e74705SXin Li Record *IOpI = R.getClass("IOpInst");
524*67e74705SXin Li Record *WOpI = R.getClass("WOpInst");
525*67e74705SXin Li Record *LOpI = R.getClass("LOpInst");
526*67e74705SXin Li Record *NoTestOpI = R.getClass("NoTestOpInst");
527*67e74705SXin Li
528*67e74705SXin Li ClassMap[SI] = ClassS;
529*67e74705SXin Li ClassMap[II] = ClassI;
530*67e74705SXin Li ClassMap[WI] = ClassW;
531*67e74705SXin Li ClassMap[SOpI] = ClassS;
532*67e74705SXin Li ClassMap[IOpI] = ClassI;
533*67e74705SXin Li ClassMap[WOpI] = ClassW;
534*67e74705SXin Li ClassMap[LOpI] = ClassL;
535*67e74705SXin Li ClassMap[NoTestOpI] = ClassNoTest;
536*67e74705SXin Li }
537*67e74705SXin Li
538*67e74705SXin Li // run - Emit arm_neon.h.inc
539*67e74705SXin Li void run(raw_ostream &o);
540*67e74705SXin Li
541*67e74705SXin Li // runHeader - Emit all the __builtin prototypes used in arm_neon.h
542*67e74705SXin Li void runHeader(raw_ostream &o);
543*67e74705SXin Li
544*67e74705SXin Li // runTests - Emit tests for all the Neon intrinsics.
545*67e74705SXin Li void runTests(raw_ostream &o);
546*67e74705SXin Li };
547*67e74705SXin Li
548*67e74705SXin Li } // end anonymous namespace
549*67e74705SXin Li
550*67e74705SXin Li //===----------------------------------------------------------------------===//
551*67e74705SXin Li // Type implementation
552*67e74705SXin Li //===----------------------------------------------------------------------===//
553*67e74705SXin Li
str() const554*67e74705SXin Li std::string Type::str() const {
555*67e74705SXin Li if (Void)
556*67e74705SXin Li return "void";
557*67e74705SXin Li std::string S;
558*67e74705SXin Li
559*67e74705SXin Li if (!Signed && isInteger())
560*67e74705SXin Li S += "u";
561*67e74705SXin Li
562*67e74705SXin Li if (Poly)
563*67e74705SXin Li S += "poly";
564*67e74705SXin Li else if (Float)
565*67e74705SXin Li S += "float";
566*67e74705SXin Li else
567*67e74705SXin Li S += "int";
568*67e74705SXin Li
569*67e74705SXin Li S += utostr(ElementBitwidth);
570*67e74705SXin Li if (isVector())
571*67e74705SXin Li S += "x" + utostr(getNumElements());
572*67e74705SXin Li if (NumVectors > 1)
573*67e74705SXin Li S += "x" + utostr(NumVectors);
574*67e74705SXin Li S += "_t";
575*67e74705SXin Li
576*67e74705SXin Li if (Constant)
577*67e74705SXin Li S += " const";
578*67e74705SXin Li if (Pointer)
579*67e74705SXin Li S += " *";
580*67e74705SXin Li
581*67e74705SXin Li return S;
582*67e74705SXin Li }
583*67e74705SXin Li
builtin_str() const584*67e74705SXin Li std::string Type::builtin_str() const {
585*67e74705SXin Li std::string S;
586*67e74705SXin Li if (isVoid())
587*67e74705SXin Li return "v";
588*67e74705SXin Li
589*67e74705SXin Li if (Pointer)
590*67e74705SXin Li // All pointers are void pointers.
591*67e74705SXin Li S += "v";
592*67e74705SXin Li else if (isInteger())
593*67e74705SXin Li switch (ElementBitwidth) {
594*67e74705SXin Li case 8: S += "c"; break;
595*67e74705SXin Li case 16: S += "s"; break;
596*67e74705SXin Li case 32: S += "i"; break;
597*67e74705SXin Li case 64: S += "Wi"; break;
598*67e74705SXin Li case 128: S += "LLLi"; break;
599*67e74705SXin Li default: llvm_unreachable("Unhandled case!");
600*67e74705SXin Li }
601*67e74705SXin Li else
602*67e74705SXin Li switch (ElementBitwidth) {
603*67e74705SXin Li case 16: S += "h"; break;
604*67e74705SXin Li case 32: S += "f"; break;
605*67e74705SXin Li case 64: S += "d"; break;
606*67e74705SXin Li default: llvm_unreachable("Unhandled case!");
607*67e74705SXin Li }
608*67e74705SXin Li
609*67e74705SXin Li if (isChar() && !Pointer)
610*67e74705SXin Li // Make chars explicitly signed.
611*67e74705SXin Li S = "S" + S;
612*67e74705SXin Li else if (isInteger() && !Pointer && !Signed)
613*67e74705SXin Li S = "U" + S;
614*67e74705SXin Li
615*67e74705SXin Li // Constant indices are "int", but have the "constant expression" modifier.
616*67e74705SXin Li if (isImmediate()) {
617*67e74705SXin Li assert(isInteger() && isSigned());
618*67e74705SXin Li S = "I" + S;
619*67e74705SXin Li }
620*67e74705SXin Li
621*67e74705SXin Li if (isScalar()) {
622*67e74705SXin Li if (Constant) S += "C";
623*67e74705SXin Li if (Pointer) S += "*";
624*67e74705SXin Li return S;
625*67e74705SXin Li }
626*67e74705SXin Li
627*67e74705SXin Li std::string Ret;
628*67e74705SXin Li for (unsigned I = 0; I < NumVectors; ++I)
629*67e74705SXin Li Ret += "V" + utostr(getNumElements()) + S;
630*67e74705SXin Li
631*67e74705SXin Li return Ret;
632*67e74705SXin Li }
633*67e74705SXin Li
getNeonEnum() const634*67e74705SXin Li unsigned Type::getNeonEnum() const {
635*67e74705SXin Li unsigned Addend;
636*67e74705SXin Li switch (ElementBitwidth) {
637*67e74705SXin Li case 8: Addend = 0; break;
638*67e74705SXin Li case 16: Addend = 1; break;
639*67e74705SXin Li case 32: Addend = 2; break;
640*67e74705SXin Li case 64: Addend = 3; break;
641*67e74705SXin Li case 128: Addend = 4; break;
642*67e74705SXin Li default: llvm_unreachable("Unhandled element bitwidth!");
643*67e74705SXin Li }
644*67e74705SXin Li
645*67e74705SXin Li unsigned Base = (unsigned)NeonTypeFlags::Int8 + Addend;
646*67e74705SXin Li if (Poly) {
647*67e74705SXin Li // Adjustment needed because Poly32 doesn't exist.
648*67e74705SXin Li if (Addend >= 2)
649*67e74705SXin Li --Addend;
650*67e74705SXin Li Base = (unsigned)NeonTypeFlags::Poly8 + Addend;
651*67e74705SXin Li }
652*67e74705SXin Li if (Float) {
653*67e74705SXin Li assert(Addend != 0 && "Float8 doesn't exist!");
654*67e74705SXin Li Base = (unsigned)NeonTypeFlags::Float16 + (Addend - 1);
655*67e74705SXin Li }
656*67e74705SXin Li
657*67e74705SXin Li if (Bitwidth == 128)
658*67e74705SXin Li Base |= (unsigned)NeonTypeFlags::QuadFlag;
659*67e74705SXin Li if (isInteger() && !Signed)
660*67e74705SXin Li Base |= (unsigned)NeonTypeFlags::UnsignedFlag;
661*67e74705SXin Li
662*67e74705SXin Li return Base;
663*67e74705SXin Li }
664*67e74705SXin Li
fromTypedefName(StringRef Name)665*67e74705SXin Li Type Type::fromTypedefName(StringRef Name) {
666*67e74705SXin Li Type T;
667*67e74705SXin Li T.Void = false;
668*67e74705SXin Li T.Float = false;
669*67e74705SXin Li T.Poly = false;
670*67e74705SXin Li
671*67e74705SXin Li if (Name.front() == 'u') {
672*67e74705SXin Li T.Signed = false;
673*67e74705SXin Li Name = Name.drop_front();
674*67e74705SXin Li } else {
675*67e74705SXin Li T.Signed = true;
676*67e74705SXin Li }
677*67e74705SXin Li
678*67e74705SXin Li if (Name.startswith("float")) {
679*67e74705SXin Li T.Float = true;
680*67e74705SXin Li Name = Name.drop_front(5);
681*67e74705SXin Li } else if (Name.startswith("poly")) {
682*67e74705SXin Li T.Poly = true;
683*67e74705SXin Li Name = Name.drop_front(4);
684*67e74705SXin Li } else {
685*67e74705SXin Li assert(Name.startswith("int"));
686*67e74705SXin Li Name = Name.drop_front(3);
687*67e74705SXin Li }
688*67e74705SXin Li
689*67e74705SXin Li unsigned I = 0;
690*67e74705SXin Li for (I = 0; I < Name.size(); ++I) {
691*67e74705SXin Li if (!isdigit(Name[I]))
692*67e74705SXin Li break;
693*67e74705SXin Li }
694*67e74705SXin Li Name.substr(0, I).getAsInteger(10, T.ElementBitwidth);
695*67e74705SXin Li Name = Name.drop_front(I);
696*67e74705SXin Li
697*67e74705SXin Li T.Bitwidth = T.ElementBitwidth;
698*67e74705SXin Li T.NumVectors = 1;
699*67e74705SXin Li
700*67e74705SXin Li if (Name.front() == 'x') {
701*67e74705SXin Li Name = Name.drop_front();
702*67e74705SXin Li unsigned I = 0;
703*67e74705SXin Li for (I = 0; I < Name.size(); ++I) {
704*67e74705SXin Li if (!isdigit(Name[I]))
705*67e74705SXin Li break;
706*67e74705SXin Li }
707*67e74705SXin Li unsigned NumLanes;
708*67e74705SXin Li Name.substr(0, I).getAsInteger(10, NumLanes);
709*67e74705SXin Li Name = Name.drop_front(I);
710*67e74705SXin Li T.Bitwidth = T.ElementBitwidth * NumLanes;
711*67e74705SXin Li } else {
712*67e74705SXin Li // Was scalar.
713*67e74705SXin Li T.NumVectors = 0;
714*67e74705SXin Li }
715*67e74705SXin Li if (Name.front() == 'x') {
716*67e74705SXin Li Name = Name.drop_front();
717*67e74705SXin Li unsigned I = 0;
718*67e74705SXin Li for (I = 0; I < Name.size(); ++I) {
719*67e74705SXin Li if (!isdigit(Name[I]))
720*67e74705SXin Li break;
721*67e74705SXin Li }
722*67e74705SXin Li Name.substr(0, I).getAsInteger(10, T.NumVectors);
723*67e74705SXin Li Name = Name.drop_front(I);
724*67e74705SXin Li }
725*67e74705SXin Li
726*67e74705SXin Li assert(Name.startswith("_t") && "Malformed typedef!");
727*67e74705SXin Li return T;
728*67e74705SXin Li }
729*67e74705SXin Li
applyTypespec(bool & Quad)730*67e74705SXin Li void Type::applyTypespec(bool &Quad) {
731*67e74705SXin Li std::string S = TS;
732*67e74705SXin Li ScalarForMangling = false;
733*67e74705SXin Li Void = false;
734*67e74705SXin Li Poly = Float = false;
735*67e74705SXin Li ElementBitwidth = ~0U;
736*67e74705SXin Li Signed = true;
737*67e74705SXin Li NumVectors = 1;
738*67e74705SXin Li
739*67e74705SXin Li for (char I : S) {
740*67e74705SXin Li switch (I) {
741*67e74705SXin Li case 'S':
742*67e74705SXin Li ScalarForMangling = true;
743*67e74705SXin Li break;
744*67e74705SXin Li case 'H':
745*67e74705SXin Li NoManglingQ = true;
746*67e74705SXin Li Quad = true;
747*67e74705SXin Li break;
748*67e74705SXin Li case 'Q':
749*67e74705SXin Li Quad = true;
750*67e74705SXin Li break;
751*67e74705SXin Li case 'P':
752*67e74705SXin Li Poly = true;
753*67e74705SXin Li break;
754*67e74705SXin Li case 'U':
755*67e74705SXin Li Signed = false;
756*67e74705SXin Li break;
757*67e74705SXin Li case 'c':
758*67e74705SXin Li ElementBitwidth = 8;
759*67e74705SXin Li break;
760*67e74705SXin Li case 'h':
761*67e74705SXin Li Float = true;
762*67e74705SXin Li // Fall through
763*67e74705SXin Li case 's':
764*67e74705SXin Li ElementBitwidth = 16;
765*67e74705SXin Li break;
766*67e74705SXin Li case 'f':
767*67e74705SXin Li Float = true;
768*67e74705SXin Li // Fall through
769*67e74705SXin Li case 'i':
770*67e74705SXin Li ElementBitwidth = 32;
771*67e74705SXin Li break;
772*67e74705SXin Li case 'd':
773*67e74705SXin Li Float = true;
774*67e74705SXin Li // Fall through
775*67e74705SXin Li case 'l':
776*67e74705SXin Li ElementBitwidth = 64;
777*67e74705SXin Li break;
778*67e74705SXin Li case 'k':
779*67e74705SXin Li ElementBitwidth = 128;
780*67e74705SXin Li // Poly doesn't have a 128x1 type.
781*67e74705SXin Li if (Poly)
782*67e74705SXin Li NumVectors = 0;
783*67e74705SXin Li break;
784*67e74705SXin Li default:
785*67e74705SXin Li llvm_unreachable("Unhandled type code!");
786*67e74705SXin Li }
787*67e74705SXin Li }
788*67e74705SXin Li assert(ElementBitwidth != ~0U && "Bad element bitwidth!");
789*67e74705SXin Li
790*67e74705SXin Li Bitwidth = Quad ? 128 : 64;
791*67e74705SXin Li }
792*67e74705SXin Li
applyModifier(char Mod)793*67e74705SXin Li void Type::applyModifier(char Mod) {
794*67e74705SXin Li bool AppliedQuad = false;
795*67e74705SXin Li applyTypespec(AppliedQuad);
796*67e74705SXin Li
797*67e74705SXin Li switch (Mod) {
798*67e74705SXin Li case 'v':
799*67e74705SXin Li Void = true;
800*67e74705SXin Li break;
801*67e74705SXin Li case 't':
802*67e74705SXin Li if (Poly) {
803*67e74705SXin Li Poly = false;
804*67e74705SXin Li Signed = false;
805*67e74705SXin Li }
806*67e74705SXin Li break;
807*67e74705SXin Li case 'b':
808*67e74705SXin Li Signed = false;
809*67e74705SXin Li Float = false;
810*67e74705SXin Li Poly = false;
811*67e74705SXin Li NumVectors = 0;
812*67e74705SXin Li Bitwidth = ElementBitwidth;
813*67e74705SXin Li break;
814*67e74705SXin Li case '$':
815*67e74705SXin Li Signed = true;
816*67e74705SXin Li Float = false;
817*67e74705SXin Li Poly = false;
818*67e74705SXin Li NumVectors = 0;
819*67e74705SXin Li Bitwidth = ElementBitwidth;
820*67e74705SXin Li break;
821*67e74705SXin Li case 'u':
822*67e74705SXin Li Signed = false;
823*67e74705SXin Li Poly = false;
824*67e74705SXin Li Float = false;
825*67e74705SXin Li break;
826*67e74705SXin Li case 'x':
827*67e74705SXin Li Signed = true;
828*67e74705SXin Li assert(!Poly && "'u' can't be used with poly types!");
829*67e74705SXin Li Float = false;
830*67e74705SXin Li break;
831*67e74705SXin Li case 'o':
832*67e74705SXin Li Bitwidth = ElementBitwidth = 64;
833*67e74705SXin Li NumVectors = 0;
834*67e74705SXin Li Float = true;
835*67e74705SXin Li break;
836*67e74705SXin Li case 'y':
837*67e74705SXin Li Bitwidth = ElementBitwidth = 32;
838*67e74705SXin Li NumVectors = 0;
839*67e74705SXin Li Float = true;
840*67e74705SXin Li break;
841*67e74705SXin Li case 'f':
842*67e74705SXin Li Float = true;
843*67e74705SXin Li ElementBitwidth = 32;
844*67e74705SXin Li break;
845*67e74705SXin Li case 'F':
846*67e74705SXin Li Float = true;
847*67e74705SXin Li ElementBitwidth = 64;
848*67e74705SXin Li break;
849*67e74705SXin Li case 'g':
850*67e74705SXin Li if (AppliedQuad)
851*67e74705SXin Li Bitwidth /= 2;
852*67e74705SXin Li break;
853*67e74705SXin Li case 'j':
854*67e74705SXin Li if (!AppliedQuad)
855*67e74705SXin Li Bitwidth *= 2;
856*67e74705SXin Li break;
857*67e74705SXin Li case 'w':
858*67e74705SXin Li ElementBitwidth *= 2;
859*67e74705SXin Li Bitwidth *= 2;
860*67e74705SXin Li break;
861*67e74705SXin Li case 'n':
862*67e74705SXin Li ElementBitwidth *= 2;
863*67e74705SXin Li break;
864*67e74705SXin Li case 'i':
865*67e74705SXin Li Float = false;
866*67e74705SXin Li Poly = false;
867*67e74705SXin Li ElementBitwidth = Bitwidth = 32;
868*67e74705SXin Li NumVectors = 0;
869*67e74705SXin Li Signed = true;
870*67e74705SXin Li Immediate = true;
871*67e74705SXin Li break;
872*67e74705SXin Li case 'l':
873*67e74705SXin Li Float = false;
874*67e74705SXin Li Poly = false;
875*67e74705SXin Li ElementBitwidth = Bitwidth = 64;
876*67e74705SXin Li NumVectors = 0;
877*67e74705SXin Li Signed = false;
878*67e74705SXin Li Immediate = true;
879*67e74705SXin Li break;
880*67e74705SXin Li case 'z':
881*67e74705SXin Li ElementBitwidth /= 2;
882*67e74705SXin Li Bitwidth = ElementBitwidth;
883*67e74705SXin Li NumVectors = 0;
884*67e74705SXin Li break;
885*67e74705SXin Li case 'r':
886*67e74705SXin Li ElementBitwidth *= 2;
887*67e74705SXin Li Bitwidth = ElementBitwidth;
888*67e74705SXin Li NumVectors = 0;
889*67e74705SXin Li break;
890*67e74705SXin Li case 's':
891*67e74705SXin Li case 'a':
892*67e74705SXin Li Bitwidth = ElementBitwidth;
893*67e74705SXin Li NumVectors = 0;
894*67e74705SXin Li break;
895*67e74705SXin Li case 'k':
896*67e74705SXin Li Bitwidth *= 2;
897*67e74705SXin Li break;
898*67e74705SXin Li case 'c':
899*67e74705SXin Li Constant = true;
900*67e74705SXin Li // Fall through
901*67e74705SXin Li case 'p':
902*67e74705SXin Li Pointer = true;
903*67e74705SXin Li Bitwidth = ElementBitwidth;
904*67e74705SXin Li NumVectors = 0;
905*67e74705SXin Li break;
906*67e74705SXin Li case 'h':
907*67e74705SXin Li ElementBitwidth /= 2;
908*67e74705SXin Li break;
909*67e74705SXin Li case 'q':
910*67e74705SXin Li ElementBitwidth /= 2;
911*67e74705SXin Li Bitwidth *= 2;
912*67e74705SXin Li break;
913*67e74705SXin Li case 'e':
914*67e74705SXin Li ElementBitwidth /= 2;
915*67e74705SXin Li Signed = false;
916*67e74705SXin Li break;
917*67e74705SXin Li case 'm':
918*67e74705SXin Li ElementBitwidth /= 2;
919*67e74705SXin Li Bitwidth /= 2;
920*67e74705SXin Li break;
921*67e74705SXin Li case 'd':
922*67e74705SXin Li break;
923*67e74705SXin Li case '2':
924*67e74705SXin Li NumVectors = 2;
925*67e74705SXin Li break;
926*67e74705SXin Li case '3':
927*67e74705SXin Li NumVectors = 3;
928*67e74705SXin Li break;
929*67e74705SXin Li case '4':
930*67e74705SXin Li NumVectors = 4;
931*67e74705SXin Li break;
932*67e74705SXin Li case 'B':
933*67e74705SXin Li NumVectors = 2;
934*67e74705SXin Li if (!AppliedQuad)
935*67e74705SXin Li Bitwidth *= 2;
936*67e74705SXin Li break;
937*67e74705SXin Li case 'C':
938*67e74705SXin Li NumVectors = 3;
939*67e74705SXin Li if (!AppliedQuad)
940*67e74705SXin Li Bitwidth *= 2;
941*67e74705SXin Li break;
942*67e74705SXin Li case 'D':
943*67e74705SXin Li NumVectors = 4;
944*67e74705SXin Li if (!AppliedQuad)
945*67e74705SXin Li Bitwidth *= 2;
946*67e74705SXin Li break;
947*67e74705SXin Li default:
948*67e74705SXin Li llvm_unreachable("Unhandled character!");
949*67e74705SXin Li }
950*67e74705SXin Li }
951*67e74705SXin Li
952*67e74705SXin Li //===----------------------------------------------------------------------===//
953*67e74705SXin Li // Intrinsic implementation
954*67e74705SXin Li //===----------------------------------------------------------------------===//
955*67e74705SXin Li
getInstTypeCode(Type T,ClassKind CK) const956*67e74705SXin Li std::string Intrinsic::getInstTypeCode(Type T, ClassKind CK) const {
957*67e74705SXin Li char typeCode = '\0';
958*67e74705SXin Li bool printNumber = true;
959*67e74705SXin Li
960*67e74705SXin Li if (CK == ClassB)
961*67e74705SXin Li return "";
962*67e74705SXin Li
963*67e74705SXin Li if (T.isPoly())
964*67e74705SXin Li typeCode = 'p';
965*67e74705SXin Li else if (T.isInteger())
966*67e74705SXin Li typeCode = T.isSigned() ? 's' : 'u';
967*67e74705SXin Li else
968*67e74705SXin Li typeCode = 'f';
969*67e74705SXin Li
970*67e74705SXin Li if (CK == ClassI) {
971*67e74705SXin Li switch (typeCode) {
972*67e74705SXin Li default:
973*67e74705SXin Li break;
974*67e74705SXin Li case 's':
975*67e74705SXin Li case 'u':
976*67e74705SXin Li case 'p':
977*67e74705SXin Li typeCode = 'i';
978*67e74705SXin Li break;
979*67e74705SXin Li }
980*67e74705SXin Li }
981*67e74705SXin Li if (CK == ClassB) {
982*67e74705SXin Li typeCode = '\0';
983*67e74705SXin Li }
984*67e74705SXin Li
985*67e74705SXin Li std::string S;
986*67e74705SXin Li if (typeCode != '\0')
987*67e74705SXin Li S.push_back(typeCode);
988*67e74705SXin Li if (printNumber)
989*67e74705SXin Li S += utostr(T.getElementSizeInBits());
990*67e74705SXin Li
991*67e74705SXin Li return S;
992*67e74705SXin Li }
993*67e74705SXin Li
isFloatingPointProtoModifier(char Mod)994*67e74705SXin Li static bool isFloatingPointProtoModifier(char Mod) {
995*67e74705SXin Li return Mod == 'F' || Mod == 'f';
996*67e74705SXin Li }
997*67e74705SXin Li
getBuiltinTypeStr()998*67e74705SXin Li std::string Intrinsic::getBuiltinTypeStr() {
999*67e74705SXin Li ClassKind LocalCK = getClassKind(true);
1000*67e74705SXin Li std::string S;
1001*67e74705SXin Li
1002*67e74705SXin Li Type RetT = getReturnType();
1003*67e74705SXin Li if ((LocalCK == ClassI || LocalCK == ClassW) && RetT.isScalar() &&
1004*67e74705SXin Li !RetT.isFloating())
1005*67e74705SXin Li RetT.makeInteger(RetT.getElementSizeInBits(), false);
1006*67e74705SXin Li
1007*67e74705SXin Li // Since the return value must be one type, return a vector type of the
1008*67e74705SXin Li // appropriate width which we will bitcast. An exception is made for
1009*67e74705SXin Li // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
1010*67e74705SXin Li // fashion, storing them to a pointer arg.
1011*67e74705SXin Li if (RetT.getNumVectors() > 1) {
1012*67e74705SXin Li S += "vv*"; // void result with void* first argument
1013*67e74705SXin Li } else {
1014*67e74705SXin Li if (RetT.isPoly())
1015*67e74705SXin Li RetT.makeInteger(RetT.getElementSizeInBits(), false);
1016*67e74705SXin Li if (!RetT.isScalar() && !RetT.isSigned())
1017*67e74705SXin Li RetT.makeSigned();
1018*67e74705SXin Li
1019*67e74705SXin Li bool ForcedVectorFloatingType = isFloatingPointProtoModifier(Proto[0]);
1020*67e74705SXin Li if (LocalCK == ClassB && !RetT.isScalar() && !ForcedVectorFloatingType)
1021*67e74705SXin Li // Cast to vector of 8-bit elements.
1022*67e74705SXin Li RetT.makeInteger(8, true);
1023*67e74705SXin Li
1024*67e74705SXin Li S += RetT.builtin_str();
1025*67e74705SXin Li }
1026*67e74705SXin Li
1027*67e74705SXin Li for (unsigned I = 0; I < getNumParams(); ++I) {
1028*67e74705SXin Li Type T = getParamType(I);
1029*67e74705SXin Li if (T.isPoly())
1030*67e74705SXin Li T.makeInteger(T.getElementSizeInBits(), false);
1031*67e74705SXin Li
1032*67e74705SXin Li bool ForcedFloatingType = isFloatingPointProtoModifier(Proto[I + 1]);
1033*67e74705SXin Li if (LocalCK == ClassB && !T.isScalar() && !ForcedFloatingType)
1034*67e74705SXin Li T.makeInteger(8, true);
1035*67e74705SXin Li // Halves always get converted to 8-bit elements.
1036*67e74705SXin Li if (T.isHalf() && T.isVector() && !T.isScalarForMangling())
1037*67e74705SXin Li T.makeInteger(8, true);
1038*67e74705SXin Li
1039*67e74705SXin Li if (LocalCK == ClassI)
1040*67e74705SXin Li T.makeSigned();
1041*67e74705SXin Li
1042*67e74705SXin Li if (hasImmediate() && getImmediateIdx() == I)
1043*67e74705SXin Li T.makeImmediate(32);
1044*67e74705SXin Li
1045*67e74705SXin Li S += T.builtin_str();
1046*67e74705SXin Li }
1047*67e74705SXin Li
1048*67e74705SXin Li // Extra constant integer to hold type class enum for this function, e.g. s8
1049*67e74705SXin Li if (LocalCK == ClassB)
1050*67e74705SXin Li S += "i";
1051*67e74705SXin Li
1052*67e74705SXin Li return S;
1053*67e74705SXin Li }
1054*67e74705SXin Li
getMangledName(bool ForceClassS) const1055*67e74705SXin Li std::string Intrinsic::getMangledName(bool ForceClassS) const {
1056*67e74705SXin Li // Check if the prototype has a scalar operand with the type of the vector
1057*67e74705SXin Li // elements. If not, bitcasting the args will take care of arg checking.
1058*67e74705SXin Li // The actual signedness etc. will be taken care of with special enums.
1059*67e74705SXin Li ClassKind LocalCK = CK;
1060*67e74705SXin Li if (!protoHasScalar())
1061*67e74705SXin Li LocalCK = ClassB;
1062*67e74705SXin Li
1063*67e74705SXin Li return mangleName(Name, ForceClassS ? ClassS : LocalCK);
1064*67e74705SXin Li }
1065*67e74705SXin Li
mangleName(std::string Name,ClassKind LocalCK) const1066*67e74705SXin Li std::string Intrinsic::mangleName(std::string Name, ClassKind LocalCK) const {
1067*67e74705SXin Li std::string typeCode = getInstTypeCode(BaseType, LocalCK);
1068*67e74705SXin Li std::string S = Name;
1069*67e74705SXin Li
1070*67e74705SXin Li if (Name == "vcvt_f16_f32" || Name == "vcvt_f32_f16" ||
1071*67e74705SXin Li Name == "vcvt_f32_f64" || Name == "vcvt_f64_f32")
1072*67e74705SXin Li return Name;
1073*67e74705SXin Li
1074*67e74705SXin Li if (typeCode.size() > 0) {
1075*67e74705SXin Li // If the name ends with _xN (N = 2,3,4), insert the typeCode before _xN.
1076*67e74705SXin Li if (Name.size() >= 3 && isdigit(Name.back()) &&
1077*67e74705SXin Li Name[Name.length() - 2] == 'x' && Name[Name.length() - 3] == '_')
1078*67e74705SXin Li S.insert(S.length() - 3, "_" + typeCode);
1079*67e74705SXin Li else
1080*67e74705SXin Li S += "_" + typeCode;
1081*67e74705SXin Li }
1082*67e74705SXin Li
1083*67e74705SXin Li if (BaseType != InBaseType) {
1084*67e74705SXin Li // A reinterpret - out the input base type at the end.
1085*67e74705SXin Li S += "_" + getInstTypeCode(InBaseType, LocalCK);
1086*67e74705SXin Li }
1087*67e74705SXin Li
1088*67e74705SXin Li if (LocalCK == ClassB)
1089*67e74705SXin Li S += "_v";
1090*67e74705SXin Li
1091*67e74705SXin Li // Insert a 'q' before the first '_' character so that it ends up before
1092*67e74705SXin Li // _lane or _n on vector-scalar operations.
1093*67e74705SXin Li if (BaseType.getSizeInBits() == 128 && !BaseType.noManglingQ()) {
1094*67e74705SXin Li size_t Pos = S.find('_');
1095*67e74705SXin Li S.insert(Pos, "q");
1096*67e74705SXin Li }
1097*67e74705SXin Li
1098*67e74705SXin Li char Suffix = '\0';
1099*67e74705SXin Li if (BaseType.isScalarForMangling()) {
1100*67e74705SXin Li switch (BaseType.getElementSizeInBits()) {
1101*67e74705SXin Li case 8: Suffix = 'b'; break;
1102*67e74705SXin Li case 16: Suffix = 'h'; break;
1103*67e74705SXin Li case 32: Suffix = 's'; break;
1104*67e74705SXin Li case 64: Suffix = 'd'; break;
1105*67e74705SXin Li default: llvm_unreachable("Bad suffix!");
1106*67e74705SXin Li }
1107*67e74705SXin Li }
1108*67e74705SXin Li if (Suffix != '\0') {
1109*67e74705SXin Li size_t Pos = S.find('_');
1110*67e74705SXin Li S.insert(Pos, &Suffix, 1);
1111*67e74705SXin Li }
1112*67e74705SXin Li
1113*67e74705SXin Li return S;
1114*67e74705SXin Li }
1115*67e74705SXin Li
replaceParamsIn(std::string S)1116*67e74705SXin Li std::string Intrinsic::replaceParamsIn(std::string S) {
1117*67e74705SXin Li while (S.find('$') != std::string::npos) {
1118*67e74705SXin Li size_t Pos = S.find('$');
1119*67e74705SXin Li size_t End = Pos + 1;
1120*67e74705SXin Li while (isalpha(S[End]))
1121*67e74705SXin Li ++End;
1122*67e74705SXin Li
1123*67e74705SXin Li std::string VarName = S.substr(Pos + 1, End - Pos - 1);
1124*67e74705SXin Li assert_with_loc(Variables.find(VarName) != Variables.end(),
1125*67e74705SXin Li "Variable not defined!");
1126*67e74705SXin Li S.replace(Pos, End - Pos, Variables.find(VarName)->second.getName());
1127*67e74705SXin Li }
1128*67e74705SXin Li
1129*67e74705SXin Li return S;
1130*67e74705SXin Li }
1131*67e74705SXin Li
initVariables()1132*67e74705SXin Li void Intrinsic::initVariables() {
1133*67e74705SXin Li Variables.clear();
1134*67e74705SXin Li
1135*67e74705SXin Li // Modify the TypeSpec per-argument to get a concrete Type, and create
1136*67e74705SXin Li // known variables for each.
1137*67e74705SXin Li for (unsigned I = 1; I < Proto.size(); ++I) {
1138*67e74705SXin Li char NameC = '0' + (I - 1);
1139*67e74705SXin Li std::string Name = "p";
1140*67e74705SXin Li Name.push_back(NameC);
1141*67e74705SXin Li
1142*67e74705SXin Li Variables[Name] = Variable(Types[I], Name + VariablePostfix);
1143*67e74705SXin Li }
1144*67e74705SXin Li RetVar = Variable(Types[0], "ret" + VariablePostfix);
1145*67e74705SXin Li }
1146*67e74705SXin Li
emitPrototype(StringRef NamePrefix)1147*67e74705SXin Li void Intrinsic::emitPrototype(StringRef NamePrefix) {
1148*67e74705SXin Li if (UseMacro)
1149*67e74705SXin Li OS << "#define ";
1150*67e74705SXin Li else
1151*67e74705SXin Li OS << "__ai " << Types[0].str() << " ";
1152*67e74705SXin Li
1153*67e74705SXin Li OS << NamePrefix.str() << mangleName(Name, ClassS) << "(";
1154*67e74705SXin Li
1155*67e74705SXin Li for (unsigned I = 0; I < getNumParams(); ++I) {
1156*67e74705SXin Li if (I != 0)
1157*67e74705SXin Li OS << ", ";
1158*67e74705SXin Li
1159*67e74705SXin Li char NameC = '0' + I;
1160*67e74705SXin Li std::string Name = "p";
1161*67e74705SXin Li Name.push_back(NameC);
1162*67e74705SXin Li assert(Variables.find(Name) != Variables.end());
1163*67e74705SXin Li Variable &V = Variables[Name];
1164*67e74705SXin Li
1165*67e74705SXin Li if (!UseMacro)
1166*67e74705SXin Li OS << V.getType().str() << " ";
1167*67e74705SXin Li OS << V.getName();
1168*67e74705SXin Li }
1169*67e74705SXin Li
1170*67e74705SXin Li OS << ")";
1171*67e74705SXin Li }
1172*67e74705SXin Li
emitOpeningBrace()1173*67e74705SXin Li void Intrinsic::emitOpeningBrace() {
1174*67e74705SXin Li if (UseMacro)
1175*67e74705SXin Li OS << " __extension__ ({";
1176*67e74705SXin Li else
1177*67e74705SXin Li OS << " {";
1178*67e74705SXin Li emitNewLine();
1179*67e74705SXin Li }
1180*67e74705SXin Li
emitClosingBrace()1181*67e74705SXin Li void Intrinsic::emitClosingBrace() {
1182*67e74705SXin Li if (UseMacro)
1183*67e74705SXin Li OS << "})";
1184*67e74705SXin Li else
1185*67e74705SXin Li OS << "}";
1186*67e74705SXin Li }
1187*67e74705SXin Li
emitNewLine()1188*67e74705SXin Li void Intrinsic::emitNewLine() {
1189*67e74705SXin Li if (UseMacro)
1190*67e74705SXin Li OS << " \\\n";
1191*67e74705SXin Li else
1192*67e74705SXin Li OS << "\n";
1193*67e74705SXin Li }
1194*67e74705SXin Li
emitReverseVariable(Variable & Dest,Variable & Src)1195*67e74705SXin Li void Intrinsic::emitReverseVariable(Variable &Dest, Variable &Src) {
1196*67e74705SXin Li if (Dest.getType().getNumVectors() > 1) {
1197*67e74705SXin Li emitNewLine();
1198*67e74705SXin Li
1199*67e74705SXin Li for (unsigned K = 0; K < Dest.getType().getNumVectors(); ++K) {
1200*67e74705SXin Li OS << " " << Dest.getName() << ".val[" << K << "] = "
1201*67e74705SXin Li << "__builtin_shufflevector("
1202*67e74705SXin Li << Src.getName() << ".val[" << K << "], "
1203*67e74705SXin Li << Src.getName() << ".val[" << K << "]";
1204*67e74705SXin Li for (int J = Dest.getType().getNumElements() - 1; J >= 0; --J)
1205*67e74705SXin Li OS << ", " << J;
1206*67e74705SXin Li OS << ");";
1207*67e74705SXin Li emitNewLine();
1208*67e74705SXin Li }
1209*67e74705SXin Li } else {
1210*67e74705SXin Li OS << " " << Dest.getName()
1211*67e74705SXin Li << " = __builtin_shufflevector(" << Src.getName() << ", " << Src.getName();
1212*67e74705SXin Li for (int J = Dest.getType().getNumElements() - 1; J >= 0; --J)
1213*67e74705SXin Li OS << ", " << J;
1214*67e74705SXin Li OS << ");";
1215*67e74705SXin Li emitNewLine();
1216*67e74705SXin Li }
1217*67e74705SXin Li }
1218*67e74705SXin Li
emitArgumentReversal()1219*67e74705SXin Li void Intrinsic::emitArgumentReversal() {
1220*67e74705SXin Li if (BigEndianSafe)
1221*67e74705SXin Li return;
1222*67e74705SXin Li
1223*67e74705SXin Li // Reverse all vector arguments.
1224*67e74705SXin Li for (unsigned I = 0; I < getNumParams(); ++I) {
1225*67e74705SXin Li std::string Name = "p" + utostr(I);
1226*67e74705SXin Li std::string NewName = "rev" + utostr(I);
1227*67e74705SXin Li
1228*67e74705SXin Li Variable &V = Variables[Name];
1229*67e74705SXin Li Variable NewV(V.getType(), NewName + VariablePostfix);
1230*67e74705SXin Li
1231*67e74705SXin Li if (!NewV.getType().isVector() || NewV.getType().getNumElements() == 1)
1232*67e74705SXin Li continue;
1233*67e74705SXin Li
1234*67e74705SXin Li OS << " " << NewV.getType().str() << " " << NewV.getName() << ";";
1235*67e74705SXin Li emitReverseVariable(NewV, V);
1236*67e74705SXin Li V = NewV;
1237*67e74705SXin Li }
1238*67e74705SXin Li }
1239*67e74705SXin Li
emitReturnReversal()1240*67e74705SXin Li void Intrinsic::emitReturnReversal() {
1241*67e74705SXin Li if (BigEndianSafe)
1242*67e74705SXin Li return;
1243*67e74705SXin Li if (!getReturnType().isVector() || getReturnType().isVoid() ||
1244*67e74705SXin Li getReturnType().getNumElements() == 1)
1245*67e74705SXin Li return;
1246*67e74705SXin Li emitReverseVariable(RetVar, RetVar);
1247*67e74705SXin Li }
1248*67e74705SXin Li
1249*67e74705SXin Li
emitShadowedArgs()1250*67e74705SXin Li void Intrinsic::emitShadowedArgs() {
1251*67e74705SXin Li // Macro arguments are not type-checked like inline function arguments,
1252*67e74705SXin Li // so assign them to local temporaries to get the right type checking.
1253*67e74705SXin Li if (!UseMacro)
1254*67e74705SXin Li return;
1255*67e74705SXin Li
1256*67e74705SXin Li for (unsigned I = 0; I < getNumParams(); ++I) {
1257*67e74705SXin Li // Do not create a temporary for an immediate argument.
1258*67e74705SXin Li // That would defeat the whole point of using a macro!
1259*67e74705SXin Li if (hasImmediate() && Proto[I+1] == 'i')
1260*67e74705SXin Li continue;
1261*67e74705SXin Li // Do not create a temporary for pointer arguments. The input
1262*67e74705SXin Li // pointer may have an alignment hint.
1263*67e74705SXin Li if (getParamType(I).isPointer())
1264*67e74705SXin Li continue;
1265*67e74705SXin Li
1266*67e74705SXin Li std::string Name = "p" + utostr(I);
1267*67e74705SXin Li
1268*67e74705SXin Li assert(Variables.find(Name) != Variables.end());
1269*67e74705SXin Li Variable &V = Variables[Name];
1270*67e74705SXin Li
1271*67e74705SXin Li std::string NewName = "s" + utostr(I);
1272*67e74705SXin Li Variable V2(V.getType(), NewName + VariablePostfix);
1273*67e74705SXin Li
1274*67e74705SXin Li OS << " " << V2.getType().str() << " " << V2.getName() << " = "
1275*67e74705SXin Li << V.getName() << ";";
1276*67e74705SXin Li emitNewLine();
1277*67e74705SXin Li
1278*67e74705SXin Li V = V2;
1279*67e74705SXin Li }
1280*67e74705SXin Li }
1281*67e74705SXin Li
1282*67e74705SXin Li // We don't check 'a' in this function, because for builtin function the
1283*67e74705SXin Li // argument matching to 'a' uses a vector type splatted from a scalar type.
protoHasScalar() const1284*67e74705SXin Li bool Intrinsic::protoHasScalar() const {
1285*67e74705SXin Li return (Proto.find('s') != std::string::npos ||
1286*67e74705SXin Li Proto.find('z') != std::string::npos ||
1287*67e74705SXin Li Proto.find('r') != std::string::npos ||
1288*67e74705SXin Li Proto.find('b') != std::string::npos ||
1289*67e74705SXin Li Proto.find('$') != std::string::npos ||
1290*67e74705SXin Li Proto.find('y') != std::string::npos ||
1291*67e74705SXin Li Proto.find('o') != std::string::npos);
1292*67e74705SXin Li }
1293*67e74705SXin Li
emitBodyAsBuiltinCall()1294*67e74705SXin Li void Intrinsic::emitBodyAsBuiltinCall() {
1295*67e74705SXin Li std::string S;
1296*67e74705SXin Li
1297*67e74705SXin Li // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
1298*67e74705SXin Li // sret-like argument.
1299*67e74705SXin Li bool SRet = getReturnType().getNumVectors() >= 2;
1300*67e74705SXin Li
1301*67e74705SXin Li StringRef N = Name;
1302*67e74705SXin Li if (hasSplat()) {
1303*67e74705SXin Li // Call the non-splat builtin: chop off the "_n" suffix from the name.
1304*67e74705SXin Li assert(N.endswith("_n"));
1305*67e74705SXin Li N = N.drop_back(2);
1306*67e74705SXin Li }
1307*67e74705SXin Li
1308*67e74705SXin Li ClassKind LocalCK = CK;
1309*67e74705SXin Li if (!protoHasScalar())
1310*67e74705SXin Li LocalCK = ClassB;
1311*67e74705SXin Li
1312*67e74705SXin Li if (!getReturnType().isVoid() && !SRet)
1313*67e74705SXin Li S += "(" + RetVar.getType().str() + ") ";
1314*67e74705SXin Li
1315*67e74705SXin Li S += "__builtin_neon_" + mangleName(N, LocalCK) + "(";
1316*67e74705SXin Li
1317*67e74705SXin Li if (SRet)
1318*67e74705SXin Li S += "&" + RetVar.getName() + ", ";
1319*67e74705SXin Li
1320*67e74705SXin Li for (unsigned I = 0; I < getNumParams(); ++I) {
1321*67e74705SXin Li Variable &V = Variables["p" + utostr(I)];
1322*67e74705SXin Li Type T = V.getType();
1323*67e74705SXin Li
1324*67e74705SXin Li // Handle multiple-vector values specially, emitting each subvector as an
1325*67e74705SXin Li // argument to the builtin.
1326*67e74705SXin Li if (T.getNumVectors() > 1) {
1327*67e74705SXin Li // Check if an explicit cast is needed.
1328*67e74705SXin Li std::string Cast;
1329*67e74705SXin Li if (T.isChar() || T.isPoly() || !T.isSigned()) {
1330*67e74705SXin Li Type T2 = T;
1331*67e74705SXin Li T2.makeOneVector();
1332*67e74705SXin Li T2.makeInteger(8, /*Signed=*/true);
1333*67e74705SXin Li Cast = "(" + T2.str() + ")";
1334*67e74705SXin Li }
1335*67e74705SXin Li
1336*67e74705SXin Li for (unsigned J = 0; J < T.getNumVectors(); ++J)
1337*67e74705SXin Li S += Cast + V.getName() + ".val[" + utostr(J) + "], ";
1338*67e74705SXin Li continue;
1339*67e74705SXin Li }
1340*67e74705SXin Li
1341*67e74705SXin Li std::string Arg;
1342*67e74705SXin Li Type CastToType = T;
1343*67e74705SXin Li if (hasSplat() && I == getSplatIdx()) {
1344*67e74705SXin Li Arg = "(" + BaseType.str() + ") {";
1345*67e74705SXin Li for (unsigned J = 0; J < BaseType.getNumElements(); ++J) {
1346*67e74705SXin Li if (J != 0)
1347*67e74705SXin Li Arg += ", ";
1348*67e74705SXin Li Arg += V.getName();
1349*67e74705SXin Li }
1350*67e74705SXin Li Arg += "}";
1351*67e74705SXin Li
1352*67e74705SXin Li CastToType = BaseType;
1353*67e74705SXin Li } else {
1354*67e74705SXin Li Arg = V.getName();
1355*67e74705SXin Li }
1356*67e74705SXin Li
1357*67e74705SXin Li // Check if an explicit cast is needed.
1358*67e74705SXin Li if (CastToType.isVector()) {
1359*67e74705SXin Li CastToType.makeInteger(8, true);
1360*67e74705SXin Li Arg = "(" + CastToType.str() + ")" + Arg;
1361*67e74705SXin Li }
1362*67e74705SXin Li
1363*67e74705SXin Li S += Arg + ", ";
1364*67e74705SXin Li }
1365*67e74705SXin Li
1366*67e74705SXin Li // Extra constant integer to hold type class enum for this function, e.g. s8
1367*67e74705SXin Li if (getClassKind(true) == ClassB) {
1368*67e74705SXin Li Type ThisTy = getReturnType();
1369*67e74705SXin Li if (Proto[0] == 'v' || isFloatingPointProtoModifier(Proto[0]))
1370*67e74705SXin Li ThisTy = getParamType(0);
1371*67e74705SXin Li if (ThisTy.isPointer())
1372*67e74705SXin Li ThisTy = getParamType(1);
1373*67e74705SXin Li
1374*67e74705SXin Li S += utostr(ThisTy.getNeonEnum());
1375*67e74705SXin Li } else {
1376*67e74705SXin Li // Remove extraneous ", ".
1377*67e74705SXin Li S.pop_back();
1378*67e74705SXin Li S.pop_back();
1379*67e74705SXin Li }
1380*67e74705SXin Li S += ");";
1381*67e74705SXin Li
1382*67e74705SXin Li std::string RetExpr;
1383*67e74705SXin Li if (!SRet && !RetVar.getType().isVoid())
1384*67e74705SXin Li RetExpr = RetVar.getName() + " = ";
1385*67e74705SXin Li
1386*67e74705SXin Li OS << " " << RetExpr << S;
1387*67e74705SXin Li emitNewLine();
1388*67e74705SXin Li }
1389*67e74705SXin Li
emitBody(StringRef CallPrefix)1390*67e74705SXin Li void Intrinsic::emitBody(StringRef CallPrefix) {
1391*67e74705SXin Li std::vector<std::string> Lines;
1392*67e74705SXin Li
1393*67e74705SXin Li assert(RetVar.getType() == Types[0]);
1394*67e74705SXin Li // Create a return variable, if we're not void.
1395*67e74705SXin Li if (!RetVar.getType().isVoid()) {
1396*67e74705SXin Li OS << " " << RetVar.getType().str() << " " << RetVar.getName() << ";";
1397*67e74705SXin Li emitNewLine();
1398*67e74705SXin Li }
1399*67e74705SXin Li
1400*67e74705SXin Li if (!Body || Body->getValues().size() == 0) {
1401*67e74705SXin Li // Nothing specific to output - must output a builtin.
1402*67e74705SXin Li emitBodyAsBuiltinCall();
1403*67e74705SXin Li return;
1404*67e74705SXin Li }
1405*67e74705SXin Li
1406*67e74705SXin Li // We have a list of "things to output". The last should be returned.
1407*67e74705SXin Li for (auto *I : Body->getValues()) {
1408*67e74705SXin Li if (StringInit *SI = dyn_cast<StringInit>(I)) {
1409*67e74705SXin Li Lines.push_back(replaceParamsIn(SI->getAsString()));
1410*67e74705SXin Li } else if (DagInit *DI = dyn_cast<DagInit>(I)) {
1411*67e74705SXin Li DagEmitter DE(*this, CallPrefix);
1412*67e74705SXin Li Lines.push_back(DE.emitDag(DI).second + ";");
1413*67e74705SXin Li }
1414*67e74705SXin Li }
1415*67e74705SXin Li
1416*67e74705SXin Li assert(!Lines.empty() && "Empty def?");
1417*67e74705SXin Li if (!RetVar.getType().isVoid())
1418*67e74705SXin Li Lines.back().insert(0, RetVar.getName() + " = ");
1419*67e74705SXin Li
1420*67e74705SXin Li for (auto &L : Lines) {
1421*67e74705SXin Li OS << " " << L;
1422*67e74705SXin Li emitNewLine();
1423*67e74705SXin Li }
1424*67e74705SXin Li }
1425*67e74705SXin Li
emitReturn()1426*67e74705SXin Li void Intrinsic::emitReturn() {
1427*67e74705SXin Li if (RetVar.getType().isVoid())
1428*67e74705SXin Li return;
1429*67e74705SXin Li if (UseMacro)
1430*67e74705SXin Li OS << " " << RetVar.getName() << ";";
1431*67e74705SXin Li else
1432*67e74705SXin Li OS << " return " << RetVar.getName() << ";";
1433*67e74705SXin Li emitNewLine();
1434*67e74705SXin Li }
1435*67e74705SXin Li
emitDag(DagInit * DI)1436*67e74705SXin Li std::pair<Type, std::string> Intrinsic::DagEmitter::emitDag(DagInit *DI) {
1437*67e74705SXin Li // At this point we should only be seeing a def.
1438*67e74705SXin Li DefInit *DefI = cast<DefInit>(DI->getOperator());
1439*67e74705SXin Li std::string Op = DefI->getAsString();
1440*67e74705SXin Li
1441*67e74705SXin Li if (Op == "cast" || Op == "bitcast")
1442*67e74705SXin Li return emitDagCast(DI, Op == "bitcast");
1443*67e74705SXin Li if (Op == "shuffle")
1444*67e74705SXin Li return emitDagShuffle(DI);
1445*67e74705SXin Li if (Op == "dup")
1446*67e74705SXin Li return emitDagDup(DI);
1447*67e74705SXin Li if (Op == "splat")
1448*67e74705SXin Li return emitDagSplat(DI);
1449*67e74705SXin Li if (Op == "save_temp")
1450*67e74705SXin Li return emitDagSaveTemp(DI);
1451*67e74705SXin Li if (Op == "op")
1452*67e74705SXin Li return emitDagOp(DI);
1453*67e74705SXin Li if (Op == "call")
1454*67e74705SXin Li return emitDagCall(DI);
1455*67e74705SXin Li if (Op == "name_replace")
1456*67e74705SXin Li return emitDagNameReplace(DI);
1457*67e74705SXin Li if (Op == "literal")
1458*67e74705SXin Li return emitDagLiteral(DI);
1459*67e74705SXin Li assert_with_loc(false, "Unknown operation!");
1460*67e74705SXin Li return std::make_pair(Type::getVoid(), "");
1461*67e74705SXin Li }
1462*67e74705SXin Li
emitDagOp(DagInit * DI)1463*67e74705SXin Li std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagOp(DagInit *DI) {
1464*67e74705SXin Li std::string Op = cast<StringInit>(DI->getArg(0))->getAsUnquotedString();
1465*67e74705SXin Li if (DI->getNumArgs() == 2) {
1466*67e74705SXin Li // Unary op.
1467*67e74705SXin Li std::pair<Type, std::string> R =
1468*67e74705SXin Li emitDagArg(DI->getArg(1), DI->getArgName(1));
1469*67e74705SXin Li return std::make_pair(R.first, Op + R.second);
1470*67e74705SXin Li } else {
1471*67e74705SXin Li assert(DI->getNumArgs() == 3 && "Can only handle unary and binary ops!");
1472*67e74705SXin Li std::pair<Type, std::string> R1 =
1473*67e74705SXin Li emitDagArg(DI->getArg(1), DI->getArgName(1));
1474*67e74705SXin Li std::pair<Type, std::string> R2 =
1475*67e74705SXin Li emitDagArg(DI->getArg(2), DI->getArgName(2));
1476*67e74705SXin Li assert_with_loc(R1.first == R2.first, "Argument type mismatch!");
1477*67e74705SXin Li return std::make_pair(R1.first, R1.second + " " + Op + " " + R2.second);
1478*67e74705SXin Li }
1479*67e74705SXin Li }
1480*67e74705SXin Li
emitDagCall(DagInit * DI)1481*67e74705SXin Li std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagCall(DagInit *DI) {
1482*67e74705SXin Li std::vector<Type> Types;
1483*67e74705SXin Li std::vector<std::string> Values;
1484*67e74705SXin Li for (unsigned I = 0; I < DI->getNumArgs() - 1; ++I) {
1485*67e74705SXin Li std::pair<Type, std::string> R =
1486*67e74705SXin Li emitDagArg(DI->getArg(I + 1), DI->getArgName(I + 1));
1487*67e74705SXin Li Types.push_back(R.first);
1488*67e74705SXin Li Values.push_back(R.second);
1489*67e74705SXin Li }
1490*67e74705SXin Li
1491*67e74705SXin Li // Look up the called intrinsic.
1492*67e74705SXin Li std::string N;
1493*67e74705SXin Li if (StringInit *SI = dyn_cast<StringInit>(DI->getArg(0)))
1494*67e74705SXin Li N = SI->getAsUnquotedString();
1495*67e74705SXin Li else
1496*67e74705SXin Li N = emitDagArg(DI->getArg(0), "").second;
1497*67e74705SXin Li Intrinsic &Callee = Intr.Emitter.getIntrinsic(N, Types);
1498*67e74705SXin Li
1499*67e74705SXin Li // Make sure the callee is known as an early def.
1500*67e74705SXin Li Callee.setNeededEarly();
1501*67e74705SXin Li Intr.Dependencies.insert(&Callee);
1502*67e74705SXin Li
1503*67e74705SXin Li // Now create the call itself.
1504*67e74705SXin Li std::string S = CallPrefix.str() + Callee.getMangledName(true) + "(";
1505*67e74705SXin Li for (unsigned I = 0; I < DI->getNumArgs() - 1; ++I) {
1506*67e74705SXin Li if (I != 0)
1507*67e74705SXin Li S += ", ";
1508*67e74705SXin Li S += Values[I];
1509*67e74705SXin Li }
1510*67e74705SXin Li S += ")";
1511*67e74705SXin Li
1512*67e74705SXin Li return std::make_pair(Callee.getReturnType(), S);
1513*67e74705SXin Li }
1514*67e74705SXin Li
emitDagCast(DagInit * DI,bool IsBitCast)1515*67e74705SXin Li std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagCast(DagInit *DI,
1516*67e74705SXin Li bool IsBitCast){
1517*67e74705SXin Li // (cast MOD* VAL) -> cast VAL to type given by MOD.
1518*67e74705SXin Li std::pair<Type, std::string> R = emitDagArg(
1519*67e74705SXin Li DI->getArg(DI->getNumArgs() - 1), DI->getArgName(DI->getNumArgs() - 1));
1520*67e74705SXin Li Type castToType = R.first;
1521*67e74705SXin Li for (unsigned ArgIdx = 0; ArgIdx < DI->getNumArgs() - 1; ++ArgIdx) {
1522*67e74705SXin Li
1523*67e74705SXin Li // MOD can take several forms:
1524*67e74705SXin Li // 1. $X - take the type of parameter / variable X.
1525*67e74705SXin Li // 2. The value "R" - take the type of the return type.
1526*67e74705SXin Li // 3. a type string
1527*67e74705SXin Li // 4. The value "U" or "S" to switch the signedness.
1528*67e74705SXin Li // 5. The value "H" or "D" to half or double the bitwidth.
1529*67e74705SXin Li // 6. The value "8" to convert to 8-bit (signed) integer lanes.
1530*67e74705SXin Li if (DI->getArgName(ArgIdx).size()) {
1531*67e74705SXin Li assert_with_loc(Intr.Variables.find(DI->getArgName(ArgIdx)) !=
1532*67e74705SXin Li Intr.Variables.end(),
1533*67e74705SXin Li "Variable not found");
1534*67e74705SXin Li castToType = Intr.Variables[DI->getArgName(ArgIdx)].getType();
1535*67e74705SXin Li } else {
1536*67e74705SXin Li StringInit *SI = dyn_cast<StringInit>(DI->getArg(ArgIdx));
1537*67e74705SXin Li assert_with_loc(SI, "Expected string type or $Name for cast type");
1538*67e74705SXin Li
1539*67e74705SXin Li if (SI->getAsUnquotedString() == "R") {
1540*67e74705SXin Li castToType = Intr.getReturnType();
1541*67e74705SXin Li } else if (SI->getAsUnquotedString() == "U") {
1542*67e74705SXin Li castToType.makeUnsigned();
1543*67e74705SXin Li } else if (SI->getAsUnquotedString() == "S") {
1544*67e74705SXin Li castToType.makeSigned();
1545*67e74705SXin Li } else if (SI->getAsUnquotedString() == "H") {
1546*67e74705SXin Li castToType.halveLanes();
1547*67e74705SXin Li } else if (SI->getAsUnquotedString() == "D") {
1548*67e74705SXin Li castToType.doubleLanes();
1549*67e74705SXin Li } else if (SI->getAsUnquotedString() == "8") {
1550*67e74705SXin Li castToType.makeInteger(8, true);
1551*67e74705SXin Li } else {
1552*67e74705SXin Li castToType = Type::fromTypedefName(SI->getAsUnquotedString());
1553*67e74705SXin Li assert_with_loc(!castToType.isVoid(), "Unknown typedef");
1554*67e74705SXin Li }
1555*67e74705SXin Li }
1556*67e74705SXin Li }
1557*67e74705SXin Li
1558*67e74705SXin Li std::string S;
1559*67e74705SXin Li if (IsBitCast) {
1560*67e74705SXin Li // Emit a reinterpret cast. The second operand must be an lvalue, so create
1561*67e74705SXin Li // a temporary.
1562*67e74705SXin Li std::string N = "reint";
1563*67e74705SXin Li unsigned I = 0;
1564*67e74705SXin Li while (Intr.Variables.find(N) != Intr.Variables.end())
1565*67e74705SXin Li N = "reint" + utostr(++I);
1566*67e74705SXin Li Intr.Variables[N] = Variable(R.first, N + Intr.VariablePostfix);
1567*67e74705SXin Li
1568*67e74705SXin Li Intr.OS << R.first.str() << " " << Intr.Variables[N].getName() << " = "
1569*67e74705SXin Li << R.second << ";";
1570*67e74705SXin Li Intr.emitNewLine();
1571*67e74705SXin Li
1572*67e74705SXin Li S = "*(" + castToType.str() + " *) &" + Intr.Variables[N].getName() + "";
1573*67e74705SXin Li } else {
1574*67e74705SXin Li // Emit a normal (static) cast.
1575*67e74705SXin Li S = "(" + castToType.str() + ")(" + R.second + ")";
1576*67e74705SXin Li }
1577*67e74705SXin Li
1578*67e74705SXin Li return std::make_pair(castToType, S);
1579*67e74705SXin Li }
1580*67e74705SXin Li
emitDagShuffle(DagInit * DI)1581*67e74705SXin Li std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagShuffle(DagInit *DI){
1582*67e74705SXin Li // See the documentation in arm_neon.td for a description of these operators.
1583*67e74705SXin Li class LowHalf : public SetTheory::Operator {
1584*67e74705SXin Li public:
1585*67e74705SXin Li void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
1586*67e74705SXin Li ArrayRef<SMLoc> Loc) override {
1587*67e74705SXin Li SetTheory::RecSet Elts2;
1588*67e74705SXin Li ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts2, Loc);
1589*67e74705SXin Li Elts.insert(Elts2.begin(), Elts2.begin() + (Elts2.size() / 2));
1590*67e74705SXin Li }
1591*67e74705SXin Li };
1592*67e74705SXin Li class HighHalf : public SetTheory::Operator {
1593*67e74705SXin Li public:
1594*67e74705SXin Li void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
1595*67e74705SXin Li ArrayRef<SMLoc> Loc) override {
1596*67e74705SXin Li SetTheory::RecSet Elts2;
1597*67e74705SXin Li ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts2, Loc);
1598*67e74705SXin Li Elts.insert(Elts2.begin() + (Elts2.size() / 2), Elts2.end());
1599*67e74705SXin Li }
1600*67e74705SXin Li };
1601*67e74705SXin Li class Rev : public SetTheory::Operator {
1602*67e74705SXin Li unsigned ElementSize;
1603*67e74705SXin Li
1604*67e74705SXin Li public:
1605*67e74705SXin Li Rev(unsigned ElementSize) : ElementSize(ElementSize) {}
1606*67e74705SXin Li void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
1607*67e74705SXin Li ArrayRef<SMLoc> Loc) override {
1608*67e74705SXin Li SetTheory::RecSet Elts2;
1609*67e74705SXin Li ST.evaluate(Expr->arg_begin() + 1, Expr->arg_end(), Elts2, Loc);
1610*67e74705SXin Li
1611*67e74705SXin Li int64_t VectorSize = cast<IntInit>(Expr->getArg(0))->getValue();
1612*67e74705SXin Li VectorSize /= ElementSize;
1613*67e74705SXin Li
1614*67e74705SXin Li std::vector<Record *> Revved;
1615*67e74705SXin Li for (unsigned VI = 0; VI < Elts2.size(); VI += VectorSize) {
1616*67e74705SXin Li for (int LI = VectorSize - 1; LI >= 0; --LI) {
1617*67e74705SXin Li Revved.push_back(Elts2[VI + LI]);
1618*67e74705SXin Li }
1619*67e74705SXin Li }
1620*67e74705SXin Li
1621*67e74705SXin Li Elts.insert(Revved.begin(), Revved.end());
1622*67e74705SXin Li }
1623*67e74705SXin Li };
1624*67e74705SXin Li class MaskExpander : public SetTheory::Expander {
1625*67e74705SXin Li unsigned N;
1626*67e74705SXin Li
1627*67e74705SXin Li public:
1628*67e74705SXin Li MaskExpander(unsigned N) : N(N) {}
1629*67e74705SXin Li void expand(SetTheory &ST, Record *R, SetTheory::RecSet &Elts) override {
1630*67e74705SXin Li unsigned Addend = 0;
1631*67e74705SXin Li if (R->getName() == "mask0")
1632*67e74705SXin Li Addend = 0;
1633*67e74705SXin Li else if (R->getName() == "mask1")
1634*67e74705SXin Li Addend = N;
1635*67e74705SXin Li else
1636*67e74705SXin Li return;
1637*67e74705SXin Li for (unsigned I = 0; I < N; ++I)
1638*67e74705SXin Li Elts.insert(R->getRecords().getDef("sv" + utostr(I + Addend)));
1639*67e74705SXin Li }
1640*67e74705SXin Li };
1641*67e74705SXin Li
1642*67e74705SXin Li // (shuffle arg1, arg2, sequence)
1643*67e74705SXin Li std::pair<Type, std::string> Arg1 =
1644*67e74705SXin Li emitDagArg(DI->getArg(0), DI->getArgName(0));
1645*67e74705SXin Li std::pair<Type, std::string> Arg2 =
1646*67e74705SXin Li emitDagArg(DI->getArg(1), DI->getArgName(1));
1647*67e74705SXin Li assert_with_loc(Arg1.first == Arg2.first,
1648*67e74705SXin Li "Different types in arguments to shuffle!");
1649*67e74705SXin Li
1650*67e74705SXin Li SetTheory ST;
1651*67e74705SXin Li SetTheory::RecSet Elts;
1652*67e74705SXin Li ST.addOperator("lowhalf", llvm::make_unique<LowHalf>());
1653*67e74705SXin Li ST.addOperator("highhalf", llvm::make_unique<HighHalf>());
1654*67e74705SXin Li ST.addOperator("rev",
1655*67e74705SXin Li llvm::make_unique<Rev>(Arg1.first.getElementSizeInBits()));
1656*67e74705SXin Li ST.addExpander("MaskExpand",
1657*67e74705SXin Li llvm::make_unique<MaskExpander>(Arg1.first.getNumElements()));
1658*67e74705SXin Li ST.evaluate(DI->getArg(2), Elts, None);
1659*67e74705SXin Li
1660*67e74705SXin Li std::string S = "__builtin_shufflevector(" + Arg1.second + ", " + Arg2.second;
1661*67e74705SXin Li for (auto &E : Elts) {
1662*67e74705SXin Li StringRef Name = E->getName();
1663*67e74705SXin Li assert_with_loc(Name.startswith("sv"),
1664*67e74705SXin Li "Incorrect element kind in shuffle mask!");
1665*67e74705SXin Li S += ", " + Name.drop_front(2).str();
1666*67e74705SXin Li }
1667*67e74705SXin Li S += ")";
1668*67e74705SXin Li
1669*67e74705SXin Li // Recalculate the return type - the shuffle may have halved or doubled it.
1670*67e74705SXin Li Type T(Arg1.first);
1671*67e74705SXin Li if (Elts.size() > T.getNumElements()) {
1672*67e74705SXin Li assert_with_loc(
1673*67e74705SXin Li Elts.size() == T.getNumElements() * 2,
1674*67e74705SXin Li "Can only double or half the number of elements in a shuffle!");
1675*67e74705SXin Li T.doubleLanes();
1676*67e74705SXin Li } else if (Elts.size() < T.getNumElements()) {
1677*67e74705SXin Li assert_with_loc(
1678*67e74705SXin Li Elts.size() == T.getNumElements() / 2,
1679*67e74705SXin Li "Can only double or half the number of elements in a shuffle!");
1680*67e74705SXin Li T.halveLanes();
1681*67e74705SXin Li }
1682*67e74705SXin Li
1683*67e74705SXin Li return std::make_pair(T, S);
1684*67e74705SXin Li }
1685*67e74705SXin Li
emitDagDup(DagInit * DI)1686*67e74705SXin Li std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagDup(DagInit *DI) {
1687*67e74705SXin Li assert_with_loc(DI->getNumArgs() == 1, "dup() expects one argument");
1688*67e74705SXin Li std::pair<Type, std::string> A = emitDagArg(DI->getArg(0), DI->getArgName(0));
1689*67e74705SXin Li assert_with_loc(A.first.isScalar(), "dup() expects a scalar argument");
1690*67e74705SXin Li
1691*67e74705SXin Li Type T = Intr.getBaseType();
1692*67e74705SXin Li assert_with_loc(T.isVector(), "dup() used but default type is scalar!");
1693*67e74705SXin Li std::string S = "(" + T.str() + ") {";
1694*67e74705SXin Li for (unsigned I = 0; I < T.getNumElements(); ++I) {
1695*67e74705SXin Li if (I != 0)
1696*67e74705SXin Li S += ", ";
1697*67e74705SXin Li S += A.second;
1698*67e74705SXin Li }
1699*67e74705SXin Li S += "}";
1700*67e74705SXin Li
1701*67e74705SXin Li return std::make_pair(T, S);
1702*67e74705SXin Li }
1703*67e74705SXin Li
emitDagSplat(DagInit * DI)1704*67e74705SXin Li std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagSplat(DagInit *DI) {
1705*67e74705SXin Li assert_with_loc(DI->getNumArgs() == 2, "splat() expects two arguments");
1706*67e74705SXin Li std::pair<Type, std::string> A = emitDagArg(DI->getArg(0), DI->getArgName(0));
1707*67e74705SXin Li std::pair<Type, std::string> B = emitDagArg(DI->getArg(1), DI->getArgName(1));
1708*67e74705SXin Li
1709*67e74705SXin Li assert_with_loc(B.first.isScalar(),
1710*67e74705SXin Li "splat() requires a scalar int as the second argument");
1711*67e74705SXin Li
1712*67e74705SXin Li std::string S = "__builtin_shufflevector(" + A.second + ", " + A.second;
1713*67e74705SXin Li for (unsigned I = 0; I < Intr.getBaseType().getNumElements(); ++I) {
1714*67e74705SXin Li S += ", " + B.second;
1715*67e74705SXin Li }
1716*67e74705SXin Li S += ")";
1717*67e74705SXin Li
1718*67e74705SXin Li return std::make_pair(Intr.getBaseType(), S);
1719*67e74705SXin Li }
1720*67e74705SXin Li
emitDagSaveTemp(DagInit * DI)1721*67e74705SXin Li std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagSaveTemp(DagInit *DI) {
1722*67e74705SXin Li assert_with_loc(DI->getNumArgs() == 2, "save_temp() expects two arguments");
1723*67e74705SXin Li std::pair<Type, std::string> A = emitDagArg(DI->getArg(1), DI->getArgName(1));
1724*67e74705SXin Li
1725*67e74705SXin Li assert_with_loc(!A.first.isVoid(),
1726*67e74705SXin Li "Argument to save_temp() must have non-void type!");
1727*67e74705SXin Li
1728*67e74705SXin Li std::string N = DI->getArgName(0);
1729*67e74705SXin Li assert_with_loc(N.size(), "save_temp() expects a name as the first argument");
1730*67e74705SXin Li
1731*67e74705SXin Li assert_with_loc(Intr.Variables.find(N) == Intr.Variables.end(),
1732*67e74705SXin Li "Variable already defined!");
1733*67e74705SXin Li Intr.Variables[N] = Variable(A.first, N + Intr.VariablePostfix);
1734*67e74705SXin Li
1735*67e74705SXin Li std::string S =
1736*67e74705SXin Li A.first.str() + " " + Intr.Variables[N].getName() + " = " + A.second;
1737*67e74705SXin Li
1738*67e74705SXin Li return std::make_pair(Type::getVoid(), S);
1739*67e74705SXin Li }
1740*67e74705SXin Li
1741*67e74705SXin Li std::pair<Type, std::string>
emitDagNameReplace(DagInit * DI)1742*67e74705SXin Li Intrinsic::DagEmitter::emitDagNameReplace(DagInit *DI) {
1743*67e74705SXin Li std::string S = Intr.Name;
1744*67e74705SXin Li
1745*67e74705SXin Li assert_with_loc(DI->getNumArgs() == 2, "name_replace requires 2 arguments!");
1746*67e74705SXin Li std::string ToReplace = cast<StringInit>(DI->getArg(0))->getAsUnquotedString();
1747*67e74705SXin Li std::string ReplaceWith = cast<StringInit>(DI->getArg(1))->getAsUnquotedString();
1748*67e74705SXin Li
1749*67e74705SXin Li size_t Idx = S.find(ToReplace);
1750*67e74705SXin Li
1751*67e74705SXin Li assert_with_loc(Idx != std::string::npos, "name should contain '" + ToReplace + "'!");
1752*67e74705SXin Li S.replace(Idx, ToReplace.size(), ReplaceWith);
1753*67e74705SXin Li
1754*67e74705SXin Li return std::make_pair(Type::getVoid(), S);
1755*67e74705SXin Li }
1756*67e74705SXin Li
emitDagLiteral(DagInit * DI)1757*67e74705SXin Li std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagLiteral(DagInit *DI){
1758*67e74705SXin Li std::string Ty = cast<StringInit>(DI->getArg(0))->getAsUnquotedString();
1759*67e74705SXin Li std::string Value = cast<StringInit>(DI->getArg(1))->getAsUnquotedString();
1760*67e74705SXin Li return std::make_pair(Type::fromTypedefName(Ty), Value);
1761*67e74705SXin Li }
1762*67e74705SXin Li
1763*67e74705SXin Li std::pair<Type, std::string>
emitDagArg(Init * Arg,std::string ArgName)1764*67e74705SXin Li Intrinsic::DagEmitter::emitDagArg(Init *Arg, std::string ArgName) {
1765*67e74705SXin Li if (ArgName.size()) {
1766*67e74705SXin Li assert_with_loc(!Arg->isComplete(),
1767*67e74705SXin Li "Arguments must either be DAGs or names, not both!");
1768*67e74705SXin Li assert_with_loc(Intr.Variables.find(ArgName) != Intr.Variables.end(),
1769*67e74705SXin Li "Variable not defined!");
1770*67e74705SXin Li Variable &V = Intr.Variables[ArgName];
1771*67e74705SXin Li return std::make_pair(V.getType(), V.getName());
1772*67e74705SXin Li }
1773*67e74705SXin Li
1774*67e74705SXin Li assert(Arg && "Neither ArgName nor Arg?!");
1775*67e74705SXin Li DagInit *DI = dyn_cast<DagInit>(Arg);
1776*67e74705SXin Li assert_with_loc(DI, "Arguments must either be DAGs or names!");
1777*67e74705SXin Li
1778*67e74705SXin Li return emitDag(DI);
1779*67e74705SXin Li }
1780*67e74705SXin Li
generate()1781*67e74705SXin Li std::string Intrinsic::generate() {
1782*67e74705SXin Li // Little endian intrinsics are simple and don't require any argument
1783*67e74705SXin Li // swapping.
1784*67e74705SXin Li OS << "#ifdef __LITTLE_ENDIAN__\n";
1785*67e74705SXin Li
1786*67e74705SXin Li generateImpl(false, "", "");
1787*67e74705SXin Li
1788*67e74705SXin Li OS << "#else\n";
1789*67e74705SXin Li
1790*67e74705SXin Li // Big endian intrinsics are more complex. The user intended these
1791*67e74705SXin Li // intrinsics to operate on a vector "as-if" loaded by (V)LDR,
1792*67e74705SXin Li // but we load as-if (V)LD1. So we should swap all arguments and
1793*67e74705SXin Li // swap the return value too.
1794*67e74705SXin Li //
1795*67e74705SXin Li // If we call sub-intrinsics, we should call a version that does
1796*67e74705SXin Li // not re-swap the arguments!
1797*67e74705SXin Li generateImpl(true, "", "__noswap_");
1798*67e74705SXin Li
1799*67e74705SXin Li // If we're needed early, create a non-swapping variant for
1800*67e74705SXin Li // big-endian.
1801*67e74705SXin Li if (NeededEarly) {
1802*67e74705SXin Li generateImpl(false, "__noswap_", "__noswap_");
1803*67e74705SXin Li }
1804*67e74705SXin Li OS << "#endif\n\n";
1805*67e74705SXin Li
1806*67e74705SXin Li return OS.str();
1807*67e74705SXin Li }
1808*67e74705SXin Li
generateImpl(bool ReverseArguments,StringRef NamePrefix,StringRef CallPrefix)1809*67e74705SXin Li void Intrinsic::generateImpl(bool ReverseArguments,
1810*67e74705SXin Li StringRef NamePrefix, StringRef CallPrefix) {
1811*67e74705SXin Li CurrentRecord = R;
1812*67e74705SXin Li
1813*67e74705SXin Li // If we call a macro, our local variables may be corrupted due to
1814*67e74705SXin Li // lack of proper lexical scoping. So, add a globally unique postfix
1815*67e74705SXin Li // to every variable.
1816*67e74705SXin Li //
1817*67e74705SXin Li // indexBody() should have set up the Dependencies set by now.
1818*67e74705SXin Li for (auto *I : Dependencies)
1819*67e74705SXin Li if (I->UseMacro) {
1820*67e74705SXin Li VariablePostfix = "_" + utostr(Emitter.getUniqueNumber());
1821*67e74705SXin Li break;
1822*67e74705SXin Li }
1823*67e74705SXin Li
1824*67e74705SXin Li initVariables();
1825*67e74705SXin Li
1826*67e74705SXin Li emitPrototype(NamePrefix);
1827*67e74705SXin Li
1828*67e74705SXin Li if (IsUnavailable) {
1829*67e74705SXin Li OS << " __attribute__((unavailable));";
1830*67e74705SXin Li } else {
1831*67e74705SXin Li emitOpeningBrace();
1832*67e74705SXin Li emitShadowedArgs();
1833*67e74705SXin Li if (ReverseArguments)
1834*67e74705SXin Li emitArgumentReversal();
1835*67e74705SXin Li emitBody(CallPrefix);
1836*67e74705SXin Li if (ReverseArguments)
1837*67e74705SXin Li emitReturnReversal();
1838*67e74705SXin Li emitReturn();
1839*67e74705SXin Li emitClosingBrace();
1840*67e74705SXin Li }
1841*67e74705SXin Li OS << "\n";
1842*67e74705SXin Li
1843*67e74705SXin Li CurrentRecord = nullptr;
1844*67e74705SXin Li }
1845*67e74705SXin Li
indexBody()1846*67e74705SXin Li void Intrinsic::indexBody() {
1847*67e74705SXin Li CurrentRecord = R;
1848*67e74705SXin Li
1849*67e74705SXin Li initVariables();
1850*67e74705SXin Li emitBody("");
1851*67e74705SXin Li OS.str("");
1852*67e74705SXin Li
1853*67e74705SXin Li CurrentRecord = nullptr;
1854*67e74705SXin Li }
1855*67e74705SXin Li
1856*67e74705SXin Li //===----------------------------------------------------------------------===//
1857*67e74705SXin Li // NeonEmitter implementation
1858*67e74705SXin Li //===----------------------------------------------------------------------===//
1859*67e74705SXin Li
getIntrinsic(StringRef Name,ArrayRef<Type> Types)1860*67e74705SXin Li Intrinsic &NeonEmitter::getIntrinsic(StringRef Name, ArrayRef<Type> Types) {
1861*67e74705SXin Li // First, look up the name in the intrinsic map.
1862*67e74705SXin Li assert_with_loc(IntrinsicMap.find(Name.str()) != IntrinsicMap.end(),
1863*67e74705SXin Li ("Intrinsic '" + Name + "' not found!").str());
1864*67e74705SXin Li auto &V = IntrinsicMap.find(Name.str())->second;
1865*67e74705SXin Li std::vector<Intrinsic *> GoodVec;
1866*67e74705SXin Li
1867*67e74705SXin Li // Create a string to print if we end up failing.
1868*67e74705SXin Li std::string ErrMsg = "looking up intrinsic '" + Name.str() + "(";
1869*67e74705SXin Li for (unsigned I = 0; I < Types.size(); ++I) {
1870*67e74705SXin Li if (I != 0)
1871*67e74705SXin Li ErrMsg += ", ";
1872*67e74705SXin Li ErrMsg += Types[I].str();
1873*67e74705SXin Li }
1874*67e74705SXin Li ErrMsg += ")'\n";
1875*67e74705SXin Li ErrMsg += "Available overloads:\n";
1876*67e74705SXin Li
1877*67e74705SXin Li // Now, look through each intrinsic implementation and see if the types are
1878*67e74705SXin Li // compatible.
1879*67e74705SXin Li for (auto &I : V) {
1880*67e74705SXin Li ErrMsg += " - " + I.getReturnType().str() + " " + I.getMangledName();
1881*67e74705SXin Li ErrMsg += "(";
1882*67e74705SXin Li for (unsigned A = 0; A < I.getNumParams(); ++A) {
1883*67e74705SXin Li if (A != 0)
1884*67e74705SXin Li ErrMsg += ", ";
1885*67e74705SXin Li ErrMsg += I.getParamType(A).str();
1886*67e74705SXin Li }
1887*67e74705SXin Li ErrMsg += ")\n";
1888*67e74705SXin Li
1889*67e74705SXin Li if (I.getNumParams() != Types.size())
1890*67e74705SXin Li continue;
1891*67e74705SXin Li
1892*67e74705SXin Li bool Good = true;
1893*67e74705SXin Li for (unsigned Arg = 0; Arg < Types.size(); ++Arg) {
1894*67e74705SXin Li if (I.getParamType(Arg) != Types[Arg]) {
1895*67e74705SXin Li Good = false;
1896*67e74705SXin Li break;
1897*67e74705SXin Li }
1898*67e74705SXin Li }
1899*67e74705SXin Li if (Good)
1900*67e74705SXin Li GoodVec.push_back(&I);
1901*67e74705SXin Li }
1902*67e74705SXin Li
1903*67e74705SXin Li assert_with_loc(GoodVec.size() > 0,
1904*67e74705SXin Li "No compatible intrinsic found - " + ErrMsg);
1905*67e74705SXin Li assert_with_loc(GoodVec.size() == 1, "Multiple overloads found - " + ErrMsg);
1906*67e74705SXin Li
1907*67e74705SXin Li return *GoodVec.front();
1908*67e74705SXin Li }
1909*67e74705SXin Li
createIntrinsic(Record * R,SmallVectorImpl<Intrinsic * > & Out)1910*67e74705SXin Li void NeonEmitter::createIntrinsic(Record *R,
1911*67e74705SXin Li SmallVectorImpl<Intrinsic *> &Out) {
1912*67e74705SXin Li std::string Name = R->getValueAsString("Name");
1913*67e74705SXin Li std::string Proto = R->getValueAsString("Prototype");
1914*67e74705SXin Li std::string Types = R->getValueAsString("Types");
1915*67e74705SXin Li Record *OperationRec = R->getValueAsDef("Operation");
1916*67e74705SXin Li bool CartesianProductOfTypes = R->getValueAsBit("CartesianProductOfTypes");
1917*67e74705SXin Li bool BigEndianSafe = R->getValueAsBit("BigEndianSafe");
1918*67e74705SXin Li std::string Guard = R->getValueAsString("ArchGuard");
1919*67e74705SXin Li bool IsUnavailable = OperationRec->getValueAsBit("Unavailable");
1920*67e74705SXin Li
1921*67e74705SXin Li // Set the global current record. This allows assert_with_loc to produce
1922*67e74705SXin Li // decent location information even when highly nested.
1923*67e74705SXin Li CurrentRecord = R;
1924*67e74705SXin Li
1925*67e74705SXin Li ListInit *Body = OperationRec->getValueAsListInit("Ops");
1926*67e74705SXin Li
1927*67e74705SXin Li std::vector<TypeSpec> TypeSpecs = TypeSpec::fromTypeSpecs(Types);
1928*67e74705SXin Li
1929*67e74705SXin Li ClassKind CK = ClassNone;
1930*67e74705SXin Li if (R->getSuperClasses().size() >= 2)
1931*67e74705SXin Li CK = ClassMap[R->getSuperClasses()[1].first];
1932*67e74705SXin Li
1933*67e74705SXin Li std::vector<std::pair<TypeSpec, TypeSpec>> NewTypeSpecs;
1934*67e74705SXin Li for (auto TS : TypeSpecs) {
1935*67e74705SXin Li if (CartesianProductOfTypes) {
1936*67e74705SXin Li Type DefaultT(TS, 'd');
1937*67e74705SXin Li for (auto SrcTS : TypeSpecs) {
1938*67e74705SXin Li Type DefaultSrcT(SrcTS, 'd');
1939*67e74705SXin Li if (TS == SrcTS ||
1940*67e74705SXin Li DefaultSrcT.getSizeInBits() != DefaultT.getSizeInBits())
1941*67e74705SXin Li continue;
1942*67e74705SXin Li NewTypeSpecs.push_back(std::make_pair(TS, SrcTS));
1943*67e74705SXin Li }
1944*67e74705SXin Li } else {
1945*67e74705SXin Li NewTypeSpecs.push_back(std::make_pair(TS, TS));
1946*67e74705SXin Li }
1947*67e74705SXin Li }
1948*67e74705SXin Li
1949*67e74705SXin Li std::sort(NewTypeSpecs.begin(), NewTypeSpecs.end());
1950*67e74705SXin Li NewTypeSpecs.erase(std::unique(NewTypeSpecs.begin(), NewTypeSpecs.end()),
1951*67e74705SXin Li NewTypeSpecs.end());
1952*67e74705SXin Li auto &Entry = IntrinsicMap[Name];
1953*67e74705SXin Li
1954*67e74705SXin Li for (auto &I : NewTypeSpecs) {
1955*67e74705SXin Li Entry.emplace_back(R, Name, Proto, I.first, I.second, CK, Body, *this,
1956*67e74705SXin Li Guard, IsUnavailable, BigEndianSafe);
1957*67e74705SXin Li Out.push_back(&Entry.back());
1958*67e74705SXin Li }
1959*67e74705SXin Li
1960*67e74705SXin Li CurrentRecord = nullptr;
1961*67e74705SXin Li }
1962*67e74705SXin Li
1963*67e74705SXin Li /// genBuiltinsDef: Generate the BuiltinsARM.def and BuiltinsAArch64.def
1964*67e74705SXin Li /// declaration of builtins, checking for unique builtin declarations.
genBuiltinsDef(raw_ostream & OS,SmallVectorImpl<Intrinsic * > & Defs)1965*67e74705SXin Li void NeonEmitter::genBuiltinsDef(raw_ostream &OS,
1966*67e74705SXin Li SmallVectorImpl<Intrinsic *> &Defs) {
1967*67e74705SXin Li OS << "#ifdef GET_NEON_BUILTINS\n";
1968*67e74705SXin Li
1969*67e74705SXin Li // We only want to emit a builtin once, and we want to emit them in
1970*67e74705SXin Li // alphabetical order, so use a std::set.
1971*67e74705SXin Li std::set<std::string> Builtins;
1972*67e74705SXin Li
1973*67e74705SXin Li for (auto *Def : Defs) {
1974*67e74705SXin Li if (Def->hasBody())
1975*67e74705SXin Li continue;
1976*67e74705SXin Li // Functions with 'a' (the splat code) in the type prototype should not get
1977*67e74705SXin Li // their own builtin as they use the non-splat variant.
1978*67e74705SXin Li if (Def->hasSplat())
1979*67e74705SXin Li continue;
1980*67e74705SXin Li
1981*67e74705SXin Li std::string S = "BUILTIN(__builtin_neon_" + Def->getMangledName() + ", \"";
1982*67e74705SXin Li
1983*67e74705SXin Li S += Def->getBuiltinTypeStr();
1984*67e74705SXin Li S += "\", \"n\")";
1985*67e74705SXin Li
1986*67e74705SXin Li Builtins.insert(S);
1987*67e74705SXin Li }
1988*67e74705SXin Li
1989*67e74705SXin Li for (auto &S : Builtins)
1990*67e74705SXin Li OS << S << "\n";
1991*67e74705SXin Li OS << "#endif\n\n";
1992*67e74705SXin Li }
1993*67e74705SXin Li
1994*67e74705SXin Li /// Generate the ARM and AArch64 overloaded type checking code for
1995*67e74705SXin Li /// SemaChecking.cpp, checking for unique builtin declarations.
genOverloadTypeCheckCode(raw_ostream & OS,SmallVectorImpl<Intrinsic * > & Defs)1996*67e74705SXin Li void NeonEmitter::genOverloadTypeCheckCode(raw_ostream &OS,
1997*67e74705SXin Li SmallVectorImpl<Intrinsic *> &Defs) {
1998*67e74705SXin Li OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
1999*67e74705SXin Li
2000*67e74705SXin Li // We record each overload check line before emitting because subsequent Inst
2001*67e74705SXin Li // definitions may extend the number of permitted types (i.e. augment the
2002*67e74705SXin Li // Mask). Use std::map to avoid sorting the table by hash number.
2003*67e74705SXin Li struct OverloadInfo {
2004*67e74705SXin Li uint64_t Mask;
2005*67e74705SXin Li int PtrArgNum;
2006*67e74705SXin Li bool HasConstPtr;
2007*67e74705SXin Li OverloadInfo() : Mask(0ULL), PtrArgNum(0), HasConstPtr(false) {}
2008*67e74705SXin Li };
2009*67e74705SXin Li std::map<std::string, OverloadInfo> OverloadMap;
2010*67e74705SXin Li
2011*67e74705SXin Li for (auto *Def : Defs) {
2012*67e74705SXin Li // If the def has a body (that is, it has Operation DAGs), it won't call
2013*67e74705SXin Li // __builtin_neon_* so we don't need to generate a definition for it.
2014*67e74705SXin Li if (Def->hasBody())
2015*67e74705SXin Li continue;
2016*67e74705SXin Li // Functions with 'a' (the splat code) in the type prototype should not get
2017*67e74705SXin Li // their own builtin as they use the non-splat variant.
2018*67e74705SXin Li if (Def->hasSplat())
2019*67e74705SXin Li continue;
2020*67e74705SXin Li // Functions which have a scalar argument cannot be overloaded, no need to
2021*67e74705SXin Li // check them if we are emitting the type checking code.
2022*67e74705SXin Li if (Def->protoHasScalar())
2023*67e74705SXin Li continue;
2024*67e74705SXin Li
2025*67e74705SXin Li uint64_t Mask = 0ULL;
2026*67e74705SXin Li Type Ty = Def->getReturnType();
2027*67e74705SXin Li if (Def->getProto()[0] == 'v' ||
2028*67e74705SXin Li isFloatingPointProtoModifier(Def->getProto()[0]))
2029*67e74705SXin Li Ty = Def->getParamType(0);
2030*67e74705SXin Li if (Ty.isPointer())
2031*67e74705SXin Li Ty = Def->getParamType(1);
2032*67e74705SXin Li
2033*67e74705SXin Li Mask |= 1ULL << Ty.getNeonEnum();
2034*67e74705SXin Li
2035*67e74705SXin Li // Check if the function has a pointer or const pointer argument.
2036*67e74705SXin Li std::string Proto = Def->getProto();
2037*67e74705SXin Li int PtrArgNum = -1;
2038*67e74705SXin Li bool HasConstPtr = false;
2039*67e74705SXin Li for (unsigned I = 0; I < Def->getNumParams(); ++I) {
2040*67e74705SXin Li char ArgType = Proto[I + 1];
2041*67e74705SXin Li if (ArgType == 'c') {
2042*67e74705SXin Li HasConstPtr = true;
2043*67e74705SXin Li PtrArgNum = I;
2044*67e74705SXin Li break;
2045*67e74705SXin Li }
2046*67e74705SXin Li if (ArgType == 'p') {
2047*67e74705SXin Li PtrArgNum = I;
2048*67e74705SXin Li break;
2049*67e74705SXin Li }
2050*67e74705SXin Li }
2051*67e74705SXin Li // For sret builtins, adjust the pointer argument index.
2052*67e74705SXin Li if (PtrArgNum >= 0 && Def->getReturnType().getNumVectors() > 1)
2053*67e74705SXin Li PtrArgNum += 1;
2054*67e74705SXin Li
2055*67e74705SXin Li std::string Name = Def->getName();
2056*67e74705SXin Li // Omit type checking for the pointer arguments of vld1_lane, vld1_dup,
2057*67e74705SXin Li // and vst1_lane intrinsics. Using a pointer to the vector element
2058*67e74705SXin Li // type with one of those operations causes codegen to select an aligned
2059*67e74705SXin Li // load/store instruction. If you want an unaligned operation,
2060*67e74705SXin Li // the pointer argument needs to have less alignment than element type,
2061*67e74705SXin Li // so just accept any pointer type.
2062*67e74705SXin Li if (Name == "vld1_lane" || Name == "vld1_dup" || Name == "vst1_lane") {
2063*67e74705SXin Li PtrArgNum = -1;
2064*67e74705SXin Li HasConstPtr = false;
2065*67e74705SXin Li }
2066*67e74705SXin Li
2067*67e74705SXin Li if (Mask) {
2068*67e74705SXin Li std::string Name = Def->getMangledName();
2069*67e74705SXin Li OverloadMap.insert(std::make_pair(Name, OverloadInfo()));
2070*67e74705SXin Li OverloadInfo &OI = OverloadMap[Name];
2071*67e74705SXin Li OI.Mask |= Mask;
2072*67e74705SXin Li OI.PtrArgNum |= PtrArgNum;
2073*67e74705SXin Li OI.HasConstPtr = HasConstPtr;
2074*67e74705SXin Li }
2075*67e74705SXin Li }
2076*67e74705SXin Li
2077*67e74705SXin Li for (auto &I : OverloadMap) {
2078*67e74705SXin Li OverloadInfo &OI = I.second;
2079*67e74705SXin Li
2080*67e74705SXin Li OS << "case NEON::BI__builtin_neon_" << I.first << ": ";
2081*67e74705SXin Li OS << "mask = 0x" << utohexstr(OI.Mask) << "ULL";
2082*67e74705SXin Li if (OI.PtrArgNum >= 0)
2083*67e74705SXin Li OS << "; PtrArgNum = " << OI.PtrArgNum;
2084*67e74705SXin Li if (OI.HasConstPtr)
2085*67e74705SXin Li OS << "; HasConstPtr = true";
2086*67e74705SXin Li OS << "; break;\n";
2087*67e74705SXin Li }
2088*67e74705SXin Li OS << "#endif\n\n";
2089*67e74705SXin Li }
2090*67e74705SXin Li
2091*67e74705SXin Li void
genIntrinsicRangeCheckCode(raw_ostream & OS,SmallVectorImpl<Intrinsic * > & Defs)2092*67e74705SXin Li NeonEmitter::genIntrinsicRangeCheckCode(raw_ostream &OS,
2093*67e74705SXin Li SmallVectorImpl<Intrinsic *> &Defs) {
2094*67e74705SXin Li OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
2095*67e74705SXin Li
2096*67e74705SXin Li std::set<std::string> Emitted;
2097*67e74705SXin Li
2098*67e74705SXin Li for (auto *Def : Defs) {
2099*67e74705SXin Li if (Def->hasBody())
2100*67e74705SXin Li continue;
2101*67e74705SXin Li // Functions with 'a' (the splat code) in the type prototype should not get
2102*67e74705SXin Li // their own builtin as they use the non-splat variant.
2103*67e74705SXin Li if (Def->hasSplat())
2104*67e74705SXin Li continue;
2105*67e74705SXin Li // Functions which do not have an immediate do not need to have range
2106*67e74705SXin Li // checking code emitted.
2107*67e74705SXin Li if (!Def->hasImmediate())
2108*67e74705SXin Li continue;
2109*67e74705SXin Li if (Emitted.find(Def->getMangledName()) != Emitted.end())
2110*67e74705SXin Li continue;
2111*67e74705SXin Li
2112*67e74705SXin Li std::string LowerBound, UpperBound;
2113*67e74705SXin Li
2114*67e74705SXin Li Record *R = Def->getRecord();
2115*67e74705SXin Li if (R->getValueAsBit("isVCVT_N")) {
2116*67e74705SXin Li // VCVT between floating- and fixed-point values takes an immediate
2117*67e74705SXin Li // in the range [1, 32) for f32 or [1, 64) for f64.
2118*67e74705SXin Li LowerBound = "1";
2119*67e74705SXin Li if (Def->getBaseType().getElementSizeInBits() == 32)
2120*67e74705SXin Li UpperBound = "31";
2121*67e74705SXin Li else
2122*67e74705SXin Li UpperBound = "63";
2123*67e74705SXin Li } else if (R->getValueAsBit("isScalarShift")) {
2124*67e74705SXin Li // Right shifts have an 'r' in the name, left shifts do not. Convert
2125*67e74705SXin Li // instructions have the same bounds and right shifts.
2126*67e74705SXin Li if (Def->getName().find('r') != std::string::npos ||
2127*67e74705SXin Li Def->getName().find("cvt") != std::string::npos)
2128*67e74705SXin Li LowerBound = "1";
2129*67e74705SXin Li
2130*67e74705SXin Li UpperBound = utostr(Def->getReturnType().getElementSizeInBits() - 1);
2131*67e74705SXin Li } else if (R->getValueAsBit("isShift")) {
2132*67e74705SXin Li // Builtins which are overloaded by type will need to have their upper
2133*67e74705SXin Li // bound computed at Sema time based on the type constant.
2134*67e74705SXin Li
2135*67e74705SXin Li // Right shifts have an 'r' in the name, left shifts do not.
2136*67e74705SXin Li if (Def->getName().find('r') != std::string::npos)
2137*67e74705SXin Li LowerBound = "1";
2138*67e74705SXin Li UpperBound = "RFT(TV, true)";
2139*67e74705SXin Li } else if (Def->getClassKind(true) == ClassB) {
2140*67e74705SXin Li // ClassB intrinsics have a type (and hence lane number) that is only
2141*67e74705SXin Li // known at runtime.
2142*67e74705SXin Li if (R->getValueAsBit("isLaneQ"))
2143*67e74705SXin Li UpperBound = "RFT(TV, false, true)";
2144*67e74705SXin Li else
2145*67e74705SXin Li UpperBound = "RFT(TV, false, false)";
2146*67e74705SXin Li } else {
2147*67e74705SXin Li // The immediate generally refers to a lane in the preceding argument.
2148*67e74705SXin Li assert(Def->getImmediateIdx() > 0);
2149*67e74705SXin Li Type T = Def->getParamType(Def->getImmediateIdx() - 1);
2150*67e74705SXin Li UpperBound = utostr(T.getNumElements() - 1);
2151*67e74705SXin Li }
2152*67e74705SXin Li
2153*67e74705SXin Li // Calculate the index of the immediate that should be range checked.
2154*67e74705SXin Li unsigned Idx = Def->getNumParams();
2155*67e74705SXin Li if (Def->hasImmediate())
2156*67e74705SXin Li Idx = Def->getGeneratedParamIdx(Def->getImmediateIdx());
2157*67e74705SXin Li
2158*67e74705SXin Li OS << "case NEON::BI__builtin_neon_" << Def->getMangledName() << ": "
2159*67e74705SXin Li << "i = " << Idx << ";";
2160*67e74705SXin Li if (LowerBound.size())
2161*67e74705SXin Li OS << " l = " << LowerBound << ";";
2162*67e74705SXin Li if (UpperBound.size())
2163*67e74705SXin Li OS << " u = " << UpperBound << ";";
2164*67e74705SXin Li OS << " break;\n";
2165*67e74705SXin Li
2166*67e74705SXin Li Emitted.insert(Def->getMangledName());
2167*67e74705SXin Li }
2168*67e74705SXin Li
2169*67e74705SXin Li OS << "#endif\n\n";
2170*67e74705SXin Li }
2171*67e74705SXin Li
2172*67e74705SXin Li /// runHeader - Emit a file with sections defining:
2173*67e74705SXin Li /// 1. the NEON section of BuiltinsARM.def and BuiltinsAArch64.def.
2174*67e74705SXin Li /// 2. the SemaChecking code for the type overload checking.
2175*67e74705SXin Li /// 3. the SemaChecking code for validation of intrinsic immediate arguments.
runHeader(raw_ostream & OS)2176*67e74705SXin Li void NeonEmitter::runHeader(raw_ostream &OS) {
2177*67e74705SXin Li std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
2178*67e74705SXin Li
2179*67e74705SXin Li SmallVector<Intrinsic *, 128> Defs;
2180*67e74705SXin Li for (auto *R : RV)
2181*67e74705SXin Li createIntrinsic(R, Defs);
2182*67e74705SXin Li
2183*67e74705SXin Li // Generate shared BuiltinsXXX.def
2184*67e74705SXin Li genBuiltinsDef(OS, Defs);
2185*67e74705SXin Li
2186*67e74705SXin Li // Generate ARM overloaded type checking code for SemaChecking.cpp
2187*67e74705SXin Li genOverloadTypeCheckCode(OS, Defs);
2188*67e74705SXin Li
2189*67e74705SXin Li // Generate ARM range checking code for shift/lane immediates.
2190*67e74705SXin Li genIntrinsicRangeCheckCode(OS, Defs);
2191*67e74705SXin Li }
2192*67e74705SXin Li
2193*67e74705SXin Li /// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h
2194*67e74705SXin Li /// is comprised of type definitions and function declarations.
run(raw_ostream & OS)2195*67e74705SXin Li void NeonEmitter::run(raw_ostream &OS) {
2196*67e74705SXin Li OS << "/*===---- arm_neon.h - ARM Neon intrinsics "
2197*67e74705SXin Li "------------------------------"
2198*67e74705SXin Li "---===\n"
2199*67e74705SXin Li " *\n"
2200*67e74705SXin Li " * Permission is hereby granted, free of charge, to any person "
2201*67e74705SXin Li "obtaining "
2202*67e74705SXin Li "a copy\n"
2203*67e74705SXin Li " * of this software and associated documentation files (the "
2204*67e74705SXin Li "\"Software\"),"
2205*67e74705SXin Li " to deal\n"
2206*67e74705SXin Li " * in the Software without restriction, including without limitation "
2207*67e74705SXin Li "the "
2208*67e74705SXin Li "rights\n"
2209*67e74705SXin Li " * to use, copy, modify, merge, publish, distribute, sublicense, "
2210*67e74705SXin Li "and/or sell\n"
2211*67e74705SXin Li " * copies of the Software, and to permit persons to whom the Software "
2212*67e74705SXin Li "is\n"
2213*67e74705SXin Li " * furnished to do so, subject to the following conditions:\n"
2214*67e74705SXin Li " *\n"
2215*67e74705SXin Li " * The above copyright notice and this permission notice shall be "
2216*67e74705SXin Li "included in\n"
2217*67e74705SXin Li " * all copies or substantial portions of the Software.\n"
2218*67e74705SXin Li " *\n"
2219*67e74705SXin Li " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, "
2220*67e74705SXin Li "EXPRESS OR\n"
2221*67e74705SXin Li " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
2222*67e74705SXin Li "MERCHANTABILITY,\n"
2223*67e74705SXin Li " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT "
2224*67e74705SXin Li "SHALL THE\n"
2225*67e74705SXin Li " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR "
2226*67e74705SXin Li "OTHER\n"
2227*67e74705SXin Li " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, "
2228*67e74705SXin Li "ARISING FROM,\n"
2229*67e74705SXin Li " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER "
2230*67e74705SXin Li "DEALINGS IN\n"
2231*67e74705SXin Li " * THE SOFTWARE.\n"
2232*67e74705SXin Li " *\n"
2233*67e74705SXin Li " *===-----------------------------------------------------------------"
2234*67e74705SXin Li "---"
2235*67e74705SXin Li "---===\n"
2236*67e74705SXin Li " */\n\n";
2237*67e74705SXin Li
2238*67e74705SXin Li OS << "#ifndef __ARM_NEON_H\n";
2239*67e74705SXin Li OS << "#define __ARM_NEON_H\n\n";
2240*67e74705SXin Li
2241*67e74705SXin Li OS << "#if !defined(__ARM_NEON)\n";
2242*67e74705SXin Li OS << "#error \"NEON support not enabled\"\n";
2243*67e74705SXin Li OS << "#endif\n\n";
2244*67e74705SXin Li
2245*67e74705SXin Li OS << "#include <stdint.h>\n\n";
2246*67e74705SXin Li
2247*67e74705SXin Li // Emit NEON-specific scalar typedefs.
2248*67e74705SXin Li OS << "typedef float float32_t;\n";
2249*67e74705SXin Li OS << "typedef __fp16 float16_t;\n";
2250*67e74705SXin Li
2251*67e74705SXin Li OS << "#ifdef __aarch64__\n";
2252*67e74705SXin Li OS << "typedef double float64_t;\n";
2253*67e74705SXin Li OS << "#endif\n\n";
2254*67e74705SXin Li
2255*67e74705SXin Li // For now, signedness of polynomial types depends on target
2256*67e74705SXin Li OS << "#ifdef __aarch64__\n";
2257*67e74705SXin Li OS << "typedef uint8_t poly8_t;\n";
2258*67e74705SXin Li OS << "typedef uint16_t poly16_t;\n";
2259*67e74705SXin Li OS << "typedef uint64_t poly64_t;\n";
2260*67e74705SXin Li OS << "typedef __uint128_t poly128_t;\n";
2261*67e74705SXin Li OS << "#else\n";
2262*67e74705SXin Li OS << "typedef int8_t poly8_t;\n";
2263*67e74705SXin Li OS << "typedef int16_t poly16_t;\n";
2264*67e74705SXin Li OS << "#endif\n";
2265*67e74705SXin Li
2266*67e74705SXin Li // Emit Neon vector typedefs.
2267*67e74705SXin Li std::string TypedefTypes(
2268*67e74705SXin Li "cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfdQdPcQPcPsQPsPlQPl");
2269*67e74705SXin Li std::vector<TypeSpec> TDTypeVec = TypeSpec::fromTypeSpecs(TypedefTypes);
2270*67e74705SXin Li
2271*67e74705SXin Li // Emit vector typedefs.
2272*67e74705SXin Li bool InIfdef = false;
2273*67e74705SXin Li for (auto &TS : TDTypeVec) {
2274*67e74705SXin Li bool IsA64 = false;
2275*67e74705SXin Li Type T(TS, 'd');
2276*67e74705SXin Li if (T.isDouble() || (T.isPoly() && T.isLong()))
2277*67e74705SXin Li IsA64 = true;
2278*67e74705SXin Li
2279*67e74705SXin Li if (InIfdef && !IsA64) {
2280*67e74705SXin Li OS << "#endif\n";
2281*67e74705SXin Li InIfdef = false;
2282*67e74705SXin Li }
2283*67e74705SXin Li if (!InIfdef && IsA64) {
2284*67e74705SXin Li OS << "#ifdef __aarch64__\n";
2285*67e74705SXin Li InIfdef = true;
2286*67e74705SXin Li }
2287*67e74705SXin Li
2288*67e74705SXin Li if (T.isPoly())
2289*67e74705SXin Li OS << "typedef __attribute__((neon_polyvector_type(";
2290*67e74705SXin Li else
2291*67e74705SXin Li OS << "typedef __attribute__((neon_vector_type(";
2292*67e74705SXin Li
2293*67e74705SXin Li Type T2 = T;
2294*67e74705SXin Li T2.makeScalar();
2295*67e74705SXin Li OS << utostr(T.getNumElements()) << "))) ";
2296*67e74705SXin Li OS << T2.str();
2297*67e74705SXin Li OS << " " << T.str() << ";\n";
2298*67e74705SXin Li }
2299*67e74705SXin Li if (InIfdef)
2300*67e74705SXin Li OS << "#endif\n";
2301*67e74705SXin Li OS << "\n";
2302*67e74705SXin Li
2303*67e74705SXin Li // Emit struct typedefs.
2304*67e74705SXin Li InIfdef = false;
2305*67e74705SXin Li for (unsigned NumMembers = 2; NumMembers <= 4; ++NumMembers) {
2306*67e74705SXin Li for (auto &TS : TDTypeVec) {
2307*67e74705SXin Li bool IsA64 = false;
2308*67e74705SXin Li Type T(TS, 'd');
2309*67e74705SXin Li if (T.isDouble() || (T.isPoly() && T.isLong()))
2310*67e74705SXin Li IsA64 = true;
2311*67e74705SXin Li
2312*67e74705SXin Li if (InIfdef && !IsA64) {
2313*67e74705SXin Li OS << "#endif\n";
2314*67e74705SXin Li InIfdef = false;
2315*67e74705SXin Li }
2316*67e74705SXin Li if (!InIfdef && IsA64) {
2317*67e74705SXin Li OS << "#ifdef __aarch64__\n";
2318*67e74705SXin Li InIfdef = true;
2319*67e74705SXin Li }
2320*67e74705SXin Li
2321*67e74705SXin Li char M = '2' + (NumMembers - 2);
2322*67e74705SXin Li Type VT(TS, M);
2323*67e74705SXin Li OS << "typedef struct " << VT.str() << " {\n";
2324*67e74705SXin Li OS << " " << T.str() << " val";
2325*67e74705SXin Li OS << "[" << utostr(NumMembers) << "]";
2326*67e74705SXin Li OS << ";\n} ";
2327*67e74705SXin Li OS << VT.str() << ";\n";
2328*67e74705SXin Li OS << "\n";
2329*67e74705SXin Li }
2330*67e74705SXin Li }
2331*67e74705SXin Li if (InIfdef)
2332*67e74705SXin Li OS << "#endif\n";
2333*67e74705SXin Li OS << "\n";
2334*67e74705SXin Li
2335*67e74705SXin Li OS << "#define __ai static inline __attribute__((__always_inline__, "
2336*67e74705SXin Li "__nodebug__))\n\n";
2337*67e74705SXin Li
2338*67e74705SXin Li SmallVector<Intrinsic *, 128> Defs;
2339*67e74705SXin Li std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
2340*67e74705SXin Li for (auto *R : RV)
2341*67e74705SXin Li createIntrinsic(R, Defs);
2342*67e74705SXin Li
2343*67e74705SXin Li for (auto *I : Defs)
2344*67e74705SXin Li I->indexBody();
2345*67e74705SXin Li
2346*67e74705SXin Li std::stable_sort(
2347*67e74705SXin Li Defs.begin(), Defs.end(),
2348*67e74705SXin Li [](const Intrinsic *A, const Intrinsic *B) { return *A < *B; });
2349*67e74705SXin Li
2350*67e74705SXin Li // Only emit a def when its requirements have been met.
2351*67e74705SXin Li // FIXME: This loop could be made faster, but it's fast enough for now.
2352*67e74705SXin Li bool MadeProgress = true;
2353*67e74705SXin Li std::string InGuard = "";
2354*67e74705SXin Li while (!Defs.empty() && MadeProgress) {
2355*67e74705SXin Li MadeProgress = false;
2356*67e74705SXin Li
2357*67e74705SXin Li for (SmallVector<Intrinsic *, 128>::iterator I = Defs.begin();
2358*67e74705SXin Li I != Defs.end(); /*No step*/) {
2359*67e74705SXin Li bool DependenciesSatisfied = true;
2360*67e74705SXin Li for (auto *II : (*I)->getDependencies()) {
2361*67e74705SXin Li if (std::find(Defs.begin(), Defs.end(), II) != Defs.end())
2362*67e74705SXin Li DependenciesSatisfied = false;
2363*67e74705SXin Li }
2364*67e74705SXin Li if (!DependenciesSatisfied) {
2365*67e74705SXin Li // Try the next one.
2366*67e74705SXin Li ++I;
2367*67e74705SXin Li continue;
2368*67e74705SXin Li }
2369*67e74705SXin Li
2370*67e74705SXin Li // Emit #endif/#if pair if needed.
2371*67e74705SXin Li if ((*I)->getGuard() != InGuard) {
2372*67e74705SXin Li if (!InGuard.empty())
2373*67e74705SXin Li OS << "#endif\n";
2374*67e74705SXin Li InGuard = (*I)->getGuard();
2375*67e74705SXin Li if (!InGuard.empty())
2376*67e74705SXin Li OS << "#if " << InGuard << "\n";
2377*67e74705SXin Li }
2378*67e74705SXin Li
2379*67e74705SXin Li // Actually generate the intrinsic code.
2380*67e74705SXin Li OS << (*I)->generate();
2381*67e74705SXin Li
2382*67e74705SXin Li MadeProgress = true;
2383*67e74705SXin Li I = Defs.erase(I);
2384*67e74705SXin Li }
2385*67e74705SXin Li }
2386*67e74705SXin Li assert(Defs.empty() && "Some requirements were not satisfied!");
2387*67e74705SXin Li if (!InGuard.empty())
2388*67e74705SXin Li OS << "#endif\n";
2389*67e74705SXin Li
2390*67e74705SXin Li OS << "\n";
2391*67e74705SXin Li OS << "#undef __ai\n\n";
2392*67e74705SXin Li OS << "#endif /* __ARM_NEON_H */\n";
2393*67e74705SXin Li }
2394*67e74705SXin Li
2395*67e74705SXin Li namespace clang {
EmitNeon(RecordKeeper & Records,raw_ostream & OS)2396*67e74705SXin Li void EmitNeon(RecordKeeper &Records, raw_ostream &OS) {
2397*67e74705SXin Li NeonEmitter(Records).run(OS);
2398*67e74705SXin Li }
EmitNeonSema(RecordKeeper & Records,raw_ostream & OS)2399*67e74705SXin Li void EmitNeonSema(RecordKeeper &Records, raw_ostream &OS) {
2400*67e74705SXin Li NeonEmitter(Records).runHeader(OS);
2401*67e74705SXin Li }
EmitNeonTest(RecordKeeper & Records,raw_ostream & OS)2402*67e74705SXin Li void EmitNeonTest(RecordKeeper &Records, raw_ostream &OS) {
2403*67e74705SXin Li llvm_unreachable("Neon test generation no longer implemented!");
2404*67e74705SXin Li }
2405*67e74705SXin Li } // End namespace clang
2406