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