1 //===- NeonEmitter.cpp - Generate arm_neon.h for use with clang -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tablegen backend is responsible for emitting arm_neon.h, which includes
11 // a declaration and definition of each function specified by the ARM NEON
12 // compiler interface. See ARM document DUI0348B.
14 // Each NEON instruction is implemented in terms of 1 or more functions which
15 // are suffixed with the element type of the input vectors. Functions may be
16 // implemented in terms of generic vector operations such as +, *, -, etc. or
17 // by calling a __builtin_-prefixed function which will be handled by clang's
20 // Additional validation code can be generated by this file when runHeader() is
21 // called, rather than the normal run() entry point. A complete set of tests
22 // for Neon intrinsics can be generated by calling the runTests() entry point.
24 //===----------------------------------------------------------------------===//
26 #include "NeonEmitter.h"
28 #include "llvm/ADT/SmallString.h"
29 #include "llvm/ADT/SmallVector.h"
30 #include "llvm/ADT/StringExtras.h"
35 /// ParseTypes - break down a string such as "fQf" into a vector of StringRefs,
36 /// which each StringRef representing a single type declared in the string.
37 /// for "fQf" we would end up with 2 StringRefs, "f", and "Qf", representing
38 /// 2xfloat and 4xfloat respectively.
39 static void ParseTypes(Record
*r
, std::string
&s
,
40 SmallVectorImpl
<StringRef
> &TV
) {
41 const char *data
= s
.data();
44 for (unsigned i
= 0, e
= s
.size(); i
!= e
; ++i
, ++len
) {
45 if (data
[len
] == 'P' || data
[len
] == 'Q' || data
[len
] == 'U')
57 throw TGError(r
->getLoc(),
58 "Unexpected letter: " + std::string(data
+ len
, 1));
61 TV
.push_back(StringRef(data
, len
+ 1));
67 /// Widen - Convert a type code into the next wider type. char -> short,
68 /// short -> int, etc.
69 static char Widen(const char t
) {
79 default: throw "unhandled type in widen!";
84 /// Narrow - Convert a type code into the next smaller type. short -> char,
85 /// float -> half float, etc.
86 static char Narrow(const char t
) {
96 default: throw "unhandled type in narrow!";
101 /// For a particular StringRef, return the base type code, and whether it has
102 /// the quad-vector, polynomial, or unsigned modifiers set.
103 static char ClassifyType(StringRef ty
, bool &quad
, bool &poly
, bool &usgn
) {
107 if (ty
[off
] == 'Q') {
113 if (ty
[off
] == 'P') {
118 // remember unsigned.
119 if (ty
[off
] == 'U') {
124 // base type to get the type string for.
128 /// ModType - Transform a type code and its modifiers based on a mod code. The
129 /// mod code definitions may be found at the top of arm_neon.td.
130 static char ModType(const char mod
, char type
, bool &quad
, bool &poly
,
131 bool &usgn
, bool &scal
, bool &cnst
, bool &pntr
) {
204 /// TypeString - for a modifier and type, generate the name of the typedef for
205 /// that type. QUc -> uint8x8_t.
206 static std::string
TypeString(const char mod
, StringRef typestr
) {
219 // base type to get the type string for.
220 char type
= ClassifyType(typestr
, quad
, poly
, usgn
);
222 // Based on the modifying character, change the type and width if necessary.
223 type
= ModType(mod
, type
, quad
, poly
, usgn
, scal
, cnst
, pntr
);
232 s
+= poly
? "poly8" : "int8";
235 s
+= quad
? "x16" : "x8";
238 s
+= poly
? "poly16" : "int16";
241 s
+= quad
? "x8" : "x4";
247 s
+= quad
? "x4" : "x2";
253 s
+= quad
? "x2" : "x1";
259 s
+= quad
? "x8" : "x4";
265 s
+= quad
? "x4" : "x2";
268 throw "unhandled type!";
279 // Append _t, finishing the type string typedef type.
291 /// BuiltinTypeString - for a modifier and type, generate the clang
292 /// BuiltinsARM.def prototype code for the function. See the top of clang's
293 /// Builtins.def for a description of the type strings.
294 static std::string
BuiltinTypeString(const char mod
, StringRef typestr
,
295 ClassKind ck
, bool ret
) {
308 // base type to get the type string for.
309 char type
= ClassifyType(typestr
, quad
, poly
, usgn
);
311 // Based on the modifying character, change the type and width if necessary.
312 type
= ModType(mod
, type
, quad
, poly
, usgn
, scal
, cnst
, pntr
);
314 // All pointers are void* pointers. Change type to 'v' now.
320 // Treat half-float ('h') types as unsigned short ('s') types.
325 usgn
= usgn
| poly
| ((ck
== ClassI
|| ck
== ClassW
) && scal
&& type
!= 'f');
332 else if (type
== 'c')
333 s
.push_back('S'); // make chars explicitly signed
335 if (type
== 'l') // 64-bit long
347 // Since the return value must be one type, return a vector type of the
348 // appropriate width which we will bitcast. An exception is made for
349 // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
350 // fashion, storing them to a pointer arg.
352 if (mod
>= '2' && mod
<= '4')
353 return "vv*"; // void result with void* first argument
354 if (mod
== 'f' || (ck
!= ClassB
&& type
== 'f'))
355 return quad
? "V4f" : "V2f";
356 if (ck
!= ClassB
&& type
== 's')
357 return quad
? "V8s" : "V4s";
358 if (ck
!= ClassB
&& type
== 'i')
359 return quad
? "V4i" : "V2i";
360 if (ck
!= ClassB
&& type
== 'l')
361 return quad
? "V2LLi" : "V1LLi";
363 return quad
? "V16Sc" : "V8Sc";
366 // Non-return array types are passed as individual vectors.
368 return quad
? "V16ScV16Sc" : "V8ScV8Sc";
370 return quad
? "V16ScV16ScV16Sc" : "V8ScV8ScV8Sc";
372 return quad
? "V16ScV16ScV16ScV16Sc" : "V8ScV8ScV8ScV8Sc";
374 if (mod
== 'f' || (ck
!= ClassB
&& type
== 'f'))
375 return quad
? "V4f" : "V2f";
376 if (ck
!= ClassB
&& type
== 's')
377 return quad
? "V8s" : "V4s";
378 if (ck
!= ClassB
&& type
== 'i')
379 return quad
? "V4i" : "V2i";
380 if (ck
!= ClassB
&& type
== 'l')
381 return quad
? "V2LLi" : "V1LLi";
383 return quad
? "V16Sc" : "V8Sc";
386 /// MangleName - Append a type or width suffix to a base neon function name,
387 /// and insert a 'q' in the appropriate location if the operation works on
388 /// 128b rather than 64b. E.g. turn "vst2_lane" into "vst2q_lane_f32", etc.
389 static std::string
MangleName(const std::string
&name
, StringRef typestr
,
391 if (name
== "vcvt_f32_f16")
397 char type
= ClassifyType(typestr
, quad
, poly
, usgn
);
399 std::string s
= name
;
404 case ClassS
: s
+= poly
? "_p8" : usgn
? "_u8" : "_s8"; break;
405 case ClassI
: s
+= "_i8"; break;
406 case ClassW
: s
+= "_8"; break;
412 case ClassS
: s
+= poly
? "_p16" : usgn
? "_u16" : "_s16"; break;
413 case ClassI
: s
+= "_i16"; break;
414 case ClassW
: s
+= "_16"; break;
420 case ClassS
: s
+= usgn
? "_u32" : "_s32"; break;
421 case ClassI
: s
+= "_i32"; break;
422 case ClassW
: s
+= "_32"; break;
428 case ClassS
: s
+= usgn
? "_u64" : "_s64"; break;
429 case ClassI
: s
+= "_i64"; break;
430 case ClassW
: s
+= "_64"; break;
437 case ClassI
: s
+= "_f16"; break;
438 case ClassW
: s
+= "_16"; break;
445 case ClassI
: s
+= "_f32"; break;
446 case ClassW
: s
+= "_32"; break;
451 throw "unhandled type!";
457 // Insert a 'q' before the first '_' character so that it ends up before
458 // _lane or _n on vector-scalar operations.
460 size_t pos
= s
.find('_');
461 s
= s
.insert(pos
, "q");
466 /// UseMacro - Examine the prototype string to determine if the intrinsic
467 /// should be defined as a preprocessor macro instead of an inline function.
468 static bool UseMacro(const std::string
&proto
) {
469 // If this builtin takes an immediate argument, we need to #define it rather
470 // than use a standard declaration, so that SemaChecking can range check
471 // the immediate passed by the user.
472 if (proto
.find('i') != std::string::npos
)
475 // Pointer arguments need to use macros to avoid hiding aligned attributes
476 // from the pointer type.
477 if (proto
.find('p') != std::string::npos
||
478 proto
.find('c') != std::string::npos
)
484 /// MacroArgUsedDirectly - Return true if argument i for an intrinsic that is
485 /// defined as a macro should be accessed directly instead of being first
486 /// assigned to a local temporary.
487 static bool MacroArgUsedDirectly(const std::string
&proto
, unsigned i
) {
488 return (proto
[i
] == 'i' || proto
[i
] == 'p' || proto
[i
] == 'c');
491 // Generate the string "(argtype a, argtype b, ...)"
492 static std::string
GenArgs(const std::string
&proto
, StringRef typestr
) {
493 bool define
= UseMacro(proto
);
499 for (unsigned i
= 1, e
= proto
.size(); i
!= e
; ++i
, ++arg
) {
501 // Some macro arguments are used directly instead of being assigned
502 // to local temporaries; prepend an underscore prefix to make their
503 // names consistent with the local temporaries.
504 if (MacroArgUsedDirectly(proto
, i
))
507 s
+= TypeString(proto
[i
], typestr
) + " __";
518 // Macro arguments are not type-checked like inline function arguments, so
519 // assign them to local temporaries to get the right type checking.
520 static std::string
GenMacroLocals(const std::string
&proto
, StringRef typestr
) {
523 bool generatedLocal
= false;
525 for (unsigned i
= 1, e
= proto
.size(); i
!= e
; ++i
, ++arg
) {
526 // Do not create a temporary for an immediate argument.
527 // That would defeat the whole point of using a macro!
530 generatedLocal
= true;
532 // For other (non-immediate) arguments that are used directly, a local
533 // temporary is still needed to get the correct type checking, even though
534 // that temporary is not used for anything.
535 if (MacroArgUsedDirectly(proto
, i
)) {
536 s
+= TypeString(proto
[i
], typestr
) + " __";
546 s
+= TypeString(proto
[i
], typestr
) + " __";
558 // Use the vmovl builtin to sign-extend or zero-extend a vector.
559 static std::string
Extend(StringRef typestr
, const std::string
&a
) {
561 s
= MangleName("vmovl", typestr
, ClassS
);
566 static std::string
Duplicate(unsigned nElts
, StringRef typestr
,
567 const std::string
&a
) {
570 s
= "(" + TypeString('d', typestr
) + "){ ";
571 for (unsigned i
= 0; i
!= nElts
; ++i
) {
581 static std::string
SplatLane(unsigned nElts
, const std::string
&vec
,
582 const std::string
&lane
) {
583 std::string s
= "__builtin_shufflevector(" + vec
+ ", " + vec
;
584 for (unsigned i
= 0; i
< nElts
; ++i
)
590 static unsigned GetNumElements(StringRef typestr
, bool &quad
) {
593 char type
= ClassifyType(typestr
, quad
, dummy
, dummy
);
596 case 'c': nElts
= 8; break;
597 case 's': nElts
= 4; break;
598 case 'i': nElts
= 2; break;
599 case 'l': nElts
= 1; break;
600 case 'h': nElts
= 4; break;
601 case 'f': nElts
= 2; break;
603 throw "unhandled type!";
606 if (quad
) nElts
<<= 1;
610 // Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
611 static std::string
GenOpString(OpKind op
, const std::string
&proto
,
614 unsigned nElts
= GetNumElements(typestr
, quad
);
615 bool define
= UseMacro(proto
);
617 std::string ts
= TypeString(proto
[0], typestr
);
628 s
+= Extend(typestr
, "__a") + " + " + Extend(typestr
, "__b") + ";";
631 s
+= "__a + " + Extend(typestr
, "__b") + ";";
637 s
+= Extend(typestr
, "__a") + " - " + Extend(typestr
, "__b") + ";";
640 s
+= "__a - " + Extend(typestr
, "__b") + ";";
643 s
+= "__a * " + Duplicate(nElts
, typestr
, "__b") + ";";
646 s
+= "__a * " + SplatLane(nElts
, "__b", "__c") + ";";
652 s
+= MangleName("vmull", typestr
, ClassS
) + "(__a, " +
653 SplatLane(nElts
, "__b", "__c") + ");";
656 s
+= "__a + (__b * " + Duplicate(nElts
, typestr
, "__c") + ");";
659 s
+= "__a + (__b * " + SplatLane(nElts
, "__c", "__d") + ");";
662 s
+= "__a + (__b * __c);";
665 s
+= "__a + " + MangleName("vmull", typestr
, ClassS
) + "(__b, " +
666 Duplicate(nElts
, typestr
, "__c") + ");";
669 s
+= "__a + " + MangleName("vmull", typestr
, ClassS
) + "(__b, " +
670 SplatLane(nElts
, "__c", "__d") + ");";
673 s
+= "__a + " + MangleName("vmull", typestr
, ClassS
) + "(__b, __c);";
676 s
+= "__a - (__b * " + Duplicate(nElts
, typestr
, "__c") + ");";
679 s
+= "__a - (__b * " + SplatLane(nElts
, "__c", "__d") + ");";
682 s
+= "__a - (__b * __c);";
685 s
+= "__a - " + MangleName("vmull", typestr
, ClassS
) + "(__b, " +
686 Duplicate(nElts
, typestr
, "__c") + ");";
689 s
+= "__a - " + MangleName("vmull", typestr
, ClassS
) + "(__b, " +
690 SplatLane(nElts
, "__c", "__d") + ");";
693 s
+= "__a - " + MangleName("vmull", typestr
, ClassS
) + "(__b, __c);";
696 s
+= MangleName("vqdmull", typestr
, ClassS
) + "(__a, " +
697 SplatLane(nElts
, "__b", "__c") + ");";
700 s
+= MangleName("vqdmlal", typestr
, ClassS
) + "(__a, __b, " +
701 SplatLane(nElts
, "__c", "__d") + ");";
704 s
+= MangleName("vqdmlsl", typestr
, ClassS
) + "(__a, __b, " +
705 SplatLane(nElts
, "__c", "__d") + ");";
708 s
+= MangleName("vqdmulh", typestr
, ClassS
) + "(__a, " +
709 SplatLane(nElts
, "__b", "__c") + ");";
712 s
+= MangleName("vqrdmulh", typestr
, ClassS
) + "(__a, " +
713 SplatLane(nElts
, "__b", "__c") + ");";
716 s
+= "(" + ts
+ ")(__a == __b);";
719 s
+= "(" + ts
+ ")(__a >= __b);";
722 s
+= "(" + ts
+ ")(__a <= __b);";
725 s
+= "(" + ts
+ ")(__a > __b);";
728 s
+= "(" + ts
+ ")(__a < __b);";
752 s
+= "(" + ts
+ ")__a;";
755 s
+= "(" + ts
+ ")__builtin_shufflevector((int64x1_t)__a";
756 s
+= ", (int64x1_t)__b, 0, 1);";
760 ")__builtin_shufflevector((int64x2_t)__a, (int64x2_t)__a, 1);";
764 ")__builtin_shufflevector((int64x2_t)__a, (int64x2_t)__a, 0);";
767 s
+= Duplicate(nElts
, typestr
, "__a") + ";";
770 s
+= SplatLane(nElts
, "__a", "__b") + ";";
773 // ((0 & 1) | (~0 & 2))
775 ts
= TypeString(proto
[1], typestr
);
776 s
+= "((__a & (" + ts
+ ")__b) | ";
777 s
+= "(~__a & (" + ts
+ ")__c));";
780 s
+= "__builtin_shufflevector(__a, __a";
781 for (unsigned i
= 2; i
<= nElts
; i
+= 2)
782 for (unsigned j
= 0; j
!= 2; ++j
)
783 s
+= ", " + utostr(i
- j
- 1);
787 unsigned WordElts
= nElts
>> (1 + (int)quad
);
788 s
+= "__builtin_shufflevector(__a, __a";
789 for (unsigned i
= WordElts
; i
<= nElts
; i
+= WordElts
)
790 for (unsigned j
= 0; j
!= WordElts
; ++j
)
791 s
+= ", " + utostr(i
- j
- 1);
796 unsigned DblWordElts
= nElts
>> (int)quad
;
797 s
+= "__builtin_shufflevector(__a, __a";
798 for (unsigned i
= DblWordElts
; i
<= nElts
; i
+= DblWordElts
)
799 for (unsigned j
= 0; j
!= DblWordElts
; ++j
)
800 s
+= ", " + utostr(i
- j
- 1);
805 std::string abd
= MangleName("vabd", typestr
, ClassS
) + "(__a, __b)";
806 if (typestr
[0] != 'U') {
807 // vabd results are always unsigned and must be zero-extended.
808 std::string utype
= "U" + typestr
.str();
809 s
+= "(" + TypeString(proto
[0], typestr
) + ")";
810 abd
= "(" + TypeString('d', utype
) + ")" + abd
;
811 s
+= Extend(utype
, abd
) + ";";
813 s
+= Extend(typestr
, abd
) + ";";
818 s
+= "__a + " + MangleName("vabd", typestr
, ClassS
) + "(__b, __c);";
822 std::string abd
= MangleName("vabd", typestr
, ClassS
) + "(__b, __c)";
823 if (typestr
[0] != 'U') {
824 // vabd results are always unsigned and must be zero-extended.
825 std::string utype
= "U" + typestr
.str();
826 s
+= "(" + TypeString(proto
[0], typestr
) + ")";
827 abd
= "(" + TypeString('d', utype
) + ")" + abd
;
828 s
+= Extend(utype
, abd
) + ";";
830 s
+= Extend(typestr
, abd
) + ";";
835 throw "unknown OpKind!";
841 static unsigned GetNeonEnum(const std::string
&proto
, StringRef typestr
) {
842 unsigned mod
= proto
[0];
845 if (mod
== 'v' || mod
== 'f')
855 // Base type to get the type string for.
856 char type
= ClassifyType(typestr
, quad
, poly
, usgn
);
858 // Based on the modifying character, change the type and width if necessary.
859 type
= ModType(mod
, type
, quad
, poly
, usgn
, scal
, cnst
, pntr
);
863 if (quad
&& proto
[1] != 'g')
886 throw "unhandled type!";
892 // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
893 static std::string
GenBuiltin(const std::string
&name
, const std::string
&proto
,
894 StringRef typestr
, ClassKind ck
) {
897 // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
898 // sret-like argument.
899 bool sret
= (proto
[0] >= '2' && proto
[0] <= '4');
901 bool define
= UseMacro(proto
);
903 // Check if the prototype has a scalar operand with the type of the vector
904 // elements. If not, bitcasting the args will take care of arg checking.
905 // The actual signedness etc. will be taken care of with special enums.
906 if (proto
.find('s') == std::string::npos
)
909 if (proto
[0] != 'v') {
910 std::string ts
= TypeString(proto
[0], typestr
);
920 s
+= "return (" + ts
+ ")";
924 bool splat
= proto
.find('a') != std::string::npos
;
926 s
+= "__builtin_neon_";
928 // Call the non-splat builtin: chop off the "_n" suffix from the name.
929 std::string
vname(name
, 0, name
.size()-2);
930 s
+= MangleName(vname
, typestr
, ck
);
932 s
+= MangleName(name
, typestr
, ck
);
936 // Pass the address of the return variable as the first argument to sret-like
942 for (unsigned i
= 1, e
= proto
.size(); i
!= e
; ++i
, ++arg
) {
943 std::string args
= std::string(&arg
, 1);
945 // Use the local temporaries instead of the macro arguments.
948 bool argQuad
= false;
949 bool argPoly
= false;
950 bool argUsgn
= false;
951 bool argScalar
= false;
953 char argType
= ClassifyType(typestr
, argQuad
, argPoly
, argUsgn
);
954 argType
= ModType(proto
[i
], argType
, argQuad
, argPoly
, argUsgn
, argScalar
,
957 // Handle multiple-vector values specially, emitting each subvector as an
958 // argument to the __builtin.
959 if (proto
[i
] >= '2' && proto
[i
] <= '4') {
960 // Check if an explicit cast is needed.
961 if (argType
!= 'c' || argPoly
|| argUsgn
)
962 args
= (argQuad
? "(int8x16_t)" : "(int8x8_t)") + args
;
964 for (unsigned vi
= 0, ve
= proto
[i
] - '0'; vi
!= ve
; ++vi
) {
965 s
+= args
+ ".val[" + utostr(vi
) + "]";
975 if (splat
&& (i
+ 1) == e
)
976 args
= Duplicate(GetNumElements(typestr
, argQuad
), typestr
, args
);
978 // Check if an explicit cast is needed.
979 if ((splat
|| !argScalar
) &&
980 ((ck
== ClassB
&& argType
!= 'c') || argPoly
|| argUsgn
)) {
981 std::string argTypeStr
= "c";
983 argTypeStr
= argType
;
985 argTypeStr
= "Q" + argTypeStr
;
986 args
= "(" + TypeString('d', argTypeStr
) + ")" + args
;
994 // Extra constant integer to hold type class enum for this function, e.g. s8
996 s
+= ", " + utostr(GetNeonEnum(proto
, typestr
));
1000 if (proto
[0] != 'v' && sret
) {
1009 static std::string
GenBuiltinDef(const std::string
&name
,
1010 const std::string
&proto
,
1011 StringRef typestr
, ClassKind ck
) {
1012 std::string
s("BUILTIN(__builtin_neon_");
1014 // If all types are the same size, bitcasting the args will take care
1015 // of arg checking. The actual signedness etc. will be taken care of with
1017 if (proto
.find('s') == std::string::npos
)
1020 s
+= MangleName(name
, typestr
, ck
);
1023 for (unsigned i
= 0, e
= proto
.size(); i
!= e
; ++i
)
1024 s
+= BuiltinTypeString(proto
[i
], typestr
, ck
, i
== 0);
1026 // Extra constant integer to hold type class enum for this function, e.g. s8
1034 static std::string
GenIntrinsic(const std::string
&name
,
1035 const std::string
&proto
,
1036 StringRef outTypeStr
, StringRef inTypeStr
,
1037 OpKind kind
, ClassKind classKind
) {
1038 assert(!proto
.empty() && "");
1039 bool define
= UseMacro(proto
);
1042 // static always inline + return type
1046 s
+= "__ai " + TypeString(proto
[0], outTypeStr
) + " ";
1048 // Function name with type suffix
1049 std::string mangledName
= MangleName(name
, outTypeStr
, ClassS
);
1050 if (outTypeStr
!= inTypeStr
) {
1051 // If the input type is different (e.g., for vreinterpret), append a suffix
1052 // for the input type. String off a "Q" (quad) prefix so that MangleName
1053 // does not insert another "q" in the name.
1054 unsigned typeStrOff
= (inTypeStr
[0] == 'Q' ? 1 : 0);
1055 StringRef inTypeNoQuad
= inTypeStr
.substr(typeStrOff
);
1056 mangledName
= MangleName(mangledName
, inTypeNoQuad
, ClassS
);
1060 // Function arguments
1061 s
+= GenArgs(proto
, inTypeStr
);
1065 s
+= " __extension__ ({ \\\n ";
1066 s
+= GenMacroLocals(proto
, inTypeStr
);
1072 s
+= GenOpString(kind
, proto
, outTypeStr
);
1074 s
+= GenBuiltin(name
, proto
, outTypeStr
, classKind
);
1083 /// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h
1084 /// is comprised of type definitions and function declarations.
1085 void NeonEmitter::run(raw_ostream
&OS
) {
1087 "/*===---- arm_neon.h - ARM Neon intrinsics ------------------------------"
1090 " * Permission is hereby granted, free of charge, to any person obtaining "
1092 " * of this software and associated documentation files (the \"Software\"),"
1094 " * in the Software without restriction, including without limitation the "
1096 " * to use, copy, modify, merge, publish, distribute, sublicense, "
1098 " * copies of the Software, and to permit persons to whom the Software is\n"
1099 " * furnished to do so, subject to the following conditions:\n"
1101 " * The above copyright notice and this permission notice shall be "
1103 " * all copies or substantial portions of the Software.\n"
1105 " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, "
1107 " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
1108 "MERCHANTABILITY,\n"
1109 " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT "
1111 " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR "
1113 " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, "
1115 " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER "
1117 " * THE SOFTWARE.\n"
1119 " *===--------------------------------------------------------------------"
1123 OS
<< "#ifndef __ARM_NEON_H\n";
1124 OS
<< "#define __ARM_NEON_H\n\n";
1126 OS
<< "#ifndef __ARM_NEON__\n";
1127 OS
<< "#error \"NEON support not enabled\"\n";
1130 OS
<< "#include <stdint.h>\n\n";
1132 // Emit NEON-specific scalar typedefs.
1133 OS
<< "typedef float float32_t;\n";
1134 OS
<< "typedef int8_t poly8_t;\n";
1135 OS
<< "typedef int16_t poly16_t;\n";
1136 OS
<< "typedef uint16_t float16_t;\n";
1138 // Emit Neon vector typedefs.
1139 std::string
TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
1140 SmallVector
<StringRef
, 24> TDTypeVec
;
1141 ParseTypes(0, TypedefTypes
, TDTypeVec
);
1143 // Emit vector typedefs.
1144 for (unsigned i
= 0, e
= TDTypeVec
.size(); i
!= e
; ++i
) {
1145 bool dummy
, quad
= false, poly
= false;
1146 (void) ClassifyType(TDTypeVec
[i
], quad
, poly
, dummy
);
1148 OS
<< "typedef __attribute__((neon_polyvector_type(";
1150 OS
<< "typedef __attribute__((neon_vector_type(";
1152 unsigned nElts
= GetNumElements(TDTypeVec
[i
], quad
);
1153 OS
<< utostr(nElts
) << "))) ";
1157 OS
<< TypeString('s', TDTypeVec
[i
]);
1158 OS
<< " " << TypeString('d', TDTypeVec
[i
]) << ";\n";
1162 // Emit struct typedefs.
1163 for (unsigned vi
= 2; vi
!= 5; ++vi
) {
1164 for (unsigned i
= 0, e
= TDTypeVec
.size(); i
!= e
; ++i
) {
1165 std::string ts
= TypeString('d', TDTypeVec
[i
]);
1166 std::string vs
= TypeString('0' + vi
, TDTypeVec
[i
]);
1167 OS
<< "typedef struct " << vs
<< " {\n";
1168 OS
<< " " << ts
<< " val";
1169 OS
<< "[" << utostr(vi
) << "]";
1171 OS
<< vs
<< ";\n\n";
1175 OS
<< "#define __ai static __attribute__((__always_inline__))\n\n";
1177 std::vector
<Record
*> RV
= Records
.getAllDerivedDefinitions("Inst");
1179 // Emit vmovl, vmull and vabd intrinsics first so they can be used by other
1180 // intrinsics. (Some of the saturating multiply instructions are also
1181 // used to implement the corresponding "_lane" variants, but tablegen
1182 // sorts the records into alphabetical order so that the "_lane" variants
1183 // come after the intrinsics they use.)
1184 emitIntrinsic(OS
, Records
.getDef("VMOVL"));
1185 emitIntrinsic(OS
, Records
.getDef("VMULL"));
1186 emitIntrinsic(OS
, Records
.getDef("VABD"));
1188 for (unsigned i
= 0, e
= RV
.size(); i
!= e
; ++i
) {
1190 if (R
->getName() != "VMOVL" &&
1191 R
->getName() != "VMULL" &&
1192 R
->getName() != "VABD")
1193 emitIntrinsic(OS
, R
);
1196 OS
<< "#undef __ai\n\n";
1197 OS
<< "#endif /* __ARM_NEON_H */\n";
1200 /// emitIntrinsic - Write out the arm_neon.h header file definitions for the
1201 /// intrinsics specified by record R.
1202 void NeonEmitter::emitIntrinsic(raw_ostream
&OS
, Record
*R
) {
1203 std::string name
= R
->getValueAsString("Name");
1204 std::string Proto
= R
->getValueAsString("Prototype");
1205 std::string Types
= R
->getValueAsString("Types");
1207 SmallVector
<StringRef
, 16> TypeVec
;
1208 ParseTypes(R
, Types
, TypeVec
);
1210 OpKind kind
= OpMap
[R
->getValueAsDef("Operand")->getName()];
1212 ClassKind classKind
= ClassNone
;
1213 if (R
->getSuperClasses().size() >= 2)
1214 classKind
= ClassMap
[R
->getSuperClasses()[1]];
1215 if (classKind
== ClassNone
&& kind
== OpNone
)
1216 throw TGError(R
->getLoc(), "Builtin has no class kind");
1218 for (unsigned ti
= 0, te
= TypeVec
.size(); ti
!= te
; ++ti
) {
1219 if (kind
== OpReinterpret
) {
1220 bool outQuad
= false;
1222 (void)ClassifyType(TypeVec
[ti
], outQuad
, dummy
, dummy
);
1223 for (unsigned srcti
= 0, srcte
= TypeVec
.size();
1224 srcti
!= srcte
; ++srcti
) {
1225 bool inQuad
= false;
1226 (void)ClassifyType(TypeVec
[srcti
], inQuad
, dummy
, dummy
);
1227 if (srcti
== ti
|| inQuad
!= outQuad
)
1229 OS
<< GenIntrinsic(name
, Proto
, TypeVec
[ti
], TypeVec
[srcti
],
1233 OS
<< GenIntrinsic(name
, Proto
, TypeVec
[ti
], TypeVec
[ti
],
1240 static unsigned RangeFromType(const char mod
, StringRef typestr
) {
1241 // base type to get the type string for.
1242 bool quad
= false, dummy
= false;
1243 char type
= ClassifyType(typestr
, quad
, dummy
, dummy
);
1244 type
= ModType(mod
, type
, quad
, dummy
, dummy
, dummy
, dummy
, dummy
);
1248 return (8 << (int)quad
) - 1;
1251 return (4 << (int)quad
) - 1;
1254 return (2 << (int)quad
) - 1;
1256 return (1 << (int)quad
) - 1;
1258 throw "unhandled type!";
1261 assert(0 && "unreachable");
1265 /// runHeader - Emit a file with sections defining:
1266 /// 1. the NEON section of BuiltinsARM.def.
1267 /// 2. the SemaChecking code for the type overload checking.
1268 /// 3. the SemaChecking code for validation of intrinsic immedate arguments.
1269 void NeonEmitter::runHeader(raw_ostream
&OS
) {
1270 std::vector
<Record
*> RV
= Records
.getAllDerivedDefinitions("Inst");
1272 StringMap
<OpKind
> EmittedMap
;
1274 // Generate BuiltinsARM.def for NEON
1275 OS
<< "#ifdef GET_NEON_BUILTINS\n";
1276 for (unsigned i
= 0, e
= RV
.size(); i
!= e
; ++i
) {
1278 OpKind k
= OpMap
[R
->getValueAsDef("Operand")->getName()];
1282 std::string Proto
= R
->getValueAsString("Prototype");
1284 // Functions with 'a' (the splat code) in the type prototype should not get
1285 // their own builtin as they use the non-splat variant.
1286 if (Proto
.find('a') != std::string::npos
)
1289 std::string Types
= R
->getValueAsString("Types");
1290 SmallVector
<StringRef
, 16> TypeVec
;
1291 ParseTypes(R
, Types
, TypeVec
);
1293 if (R
->getSuperClasses().size() < 2)
1294 throw TGError(R
->getLoc(), "Builtin has no class kind");
1296 std::string name
= R
->getValueAsString("Name");
1297 ClassKind ck
= ClassMap
[R
->getSuperClasses()[1]];
1299 for (unsigned ti
= 0, te
= TypeVec
.size(); ti
!= te
; ++ti
) {
1300 // Generate the BuiltinsARM.def declaration for this builtin, ensuring
1301 // that each unique BUILTIN() macro appears only once in the output
1303 std::string bd
= GenBuiltinDef(name
, Proto
, TypeVec
[ti
], ck
);
1304 if (EmittedMap
.count(bd
))
1307 EmittedMap
[bd
] = OpNone
;
1313 // Generate the overloaded type checking code for SemaChecking.cpp
1314 OS
<< "#ifdef GET_NEON_OVERLOAD_CHECK\n";
1315 for (unsigned i
= 0, e
= RV
.size(); i
!= e
; ++i
) {
1317 OpKind k
= OpMap
[R
->getValueAsDef("Operand")->getName()];
1321 std::string Proto
= R
->getValueAsString("Prototype");
1322 std::string Types
= R
->getValueAsString("Types");
1323 std::string name
= R
->getValueAsString("Name");
1325 // Functions with 'a' (the splat code) in the type prototype should not get
1326 // their own builtin as they use the non-splat variant.
1327 if (Proto
.find('a') != std::string::npos
)
1330 // Functions which have a scalar argument cannot be overloaded, no need to
1331 // check them if we are emitting the type checking code.
1332 if (Proto
.find('s') != std::string::npos
)
1335 SmallVector
<StringRef
, 16> TypeVec
;
1336 ParseTypes(R
, Types
, TypeVec
);
1338 if (R
->getSuperClasses().size() < 2)
1339 throw TGError(R
->getLoc(), "Builtin has no class kind");
1341 int si
= -1, qi
= -1;
1342 unsigned mask
= 0, qmask
= 0;
1343 for (unsigned ti
= 0, te
= TypeVec
.size(); ti
!= te
; ++ti
) {
1344 // Generate the switch case(s) for this builtin for the type validation.
1345 bool quad
= false, poly
= false, usgn
= false;
1346 (void) ClassifyType(TypeVec
[ti
], quad
, poly
, usgn
);
1350 qmask
|= 1 << GetNeonEnum(Proto
, TypeVec
[ti
]);
1353 mask
|= 1 << GetNeonEnum(Proto
, TypeVec
[ti
]);
1357 OS
<< "case ARM::BI__builtin_neon_"
1358 << MangleName(name
, TypeVec
[si
], ClassB
)
1359 << ": mask = " << "0x" << utohexstr(mask
) << "; break;\n";
1361 OS
<< "case ARM::BI__builtin_neon_"
1362 << MangleName(name
, TypeVec
[qi
], ClassB
)
1363 << ": mask = " << "0x" << utohexstr(qmask
) << "; break;\n";
1367 // Generate the intrinsic range checking code for shift/lane immediates.
1368 OS
<< "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
1369 for (unsigned i
= 0, e
= RV
.size(); i
!= e
; ++i
) {
1372 OpKind k
= OpMap
[R
->getValueAsDef("Operand")->getName()];
1376 std::string name
= R
->getValueAsString("Name");
1377 std::string Proto
= R
->getValueAsString("Prototype");
1378 std::string Types
= R
->getValueAsString("Types");
1380 // Functions with 'a' (the splat code) in the type prototype should not get
1381 // their own builtin as they use the non-splat variant.
1382 if (Proto
.find('a') != std::string::npos
)
1385 // Functions which do not have an immediate do not need to have range
1386 // checking code emitted.
1387 size_t immPos
= Proto
.find('i');
1388 if (immPos
== std::string::npos
)
1391 SmallVector
<StringRef
, 16> TypeVec
;
1392 ParseTypes(R
, Types
, TypeVec
);
1394 if (R
->getSuperClasses().size() < 2)
1395 throw TGError(R
->getLoc(), "Builtin has no class kind");
1397 ClassKind ck
= ClassMap
[R
->getSuperClasses()[1]];
1399 for (unsigned ti
= 0, te
= TypeVec
.size(); ti
!= te
; ++ti
) {
1400 std::string namestr
, shiftstr
, rangestr
;
1402 if (R
->getValueAsBit("isVCVT_N")) {
1403 // VCVT between floating- and fixed-point values takes an immediate
1404 // in the range 1 to 32.
1406 rangestr
= "l = 1; u = 31"; // upper bound = l + u
1407 } else if (Proto
.find('s') == std::string::npos
) {
1408 // Builtins which are overloaded by type will need to have their upper
1409 // bound computed at Sema time based on the type constant.
1411 if (R
->getValueAsBit("isShift")) {
1412 shiftstr
= ", true";
1414 // Right shifts have an 'r' in the name, left shifts do not.
1415 if (name
.find('r') != std::string::npos
)
1416 rangestr
= "l = 1; ";
1418 rangestr
+= "u = RFT(TV" + shiftstr
+ ")";
1420 // The immediate generally refers to a lane in the preceding argument.
1421 assert(immPos
> 0 && "unexpected immediate operand");
1422 rangestr
= "u = " + utostr(RangeFromType(Proto
[immPos
-1], TypeVec
[ti
]));
1424 // Make sure cases appear only once by uniquing them in a string map.
1425 namestr
= MangleName(name
, TypeVec
[ti
], ck
);
1426 if (EmittedMap
.count(namestr
))
1428 EmittedMap
[namestr
] = OpNone
;
1430 // Calculate the index of the immediate that should be range checked.
1431 unsigned immidx
= 0;
1433 // Builtins that return a struct of multiple vectors have an extra
1434 // leading arg for the struct return.
1435 if (Proto
[0] >= '2' && Proto
[0] <= '4')
1438 // Add one to the index for each argument until we reach the immediate
1439 // to be checked. Structs of vectors are passed as multiple arguments.
1440 for (unsigned ii
= 1, ie
= Proto
.size(); ii
!= ie
; ++ii
) {
1441 switch (Proto
[ii
]) {
1442 default: immidx
+= 1; break;
1443 case '2': immidx
+= 2; break;
1444 case '3': immidx
+= 3; break;
1445 case '4': immidx
+= 4; break;
1446 case 'i': ie
= ii
+ 1; break;
1449 OS
<< "case ARM::BI__builtin_neon_" << MangleName(name
, TypeVec
[ti
], ck
)
1450 << ": i = " << immidx
<< "; " << rangestr
<< "; break;\n";
1456 /// GenTest - Write out a test for the intrinsic specified by the name and
1457 /// type strings, including the embedded patterns for FileCheck to match.
1458 static std::string
GenTest(const std::string
&name
,
1459 const std::string
&proto
,
1460 StringRef outTypeStr
, StringRef inTypeStr
,
1462 assert(!proto
.empty() && "");
1465 // Function name with type suffix
1466 std::string mangledName
= MangleName(name
, outTypeStr
, ClassS
);
1467 if (outTypeStr
!= inTypeStr
) {
1468 // If the input type is different (e.g., for vreinterpret), append a suffix
1469 // for the input type. String off a "Q" (quad) prefix so that MangleName
1470 // does not insert another "q" in the name.
1471 unsigned typeStrOff
= (inTypeStr
[0] == 'Q' ? 1 : 0);
1472 StringRef inTypeNoQuad
= inTypeStr
.substr(typeStrOff
);
1473 mangledName
= MangleName(mangledName
, inTypeNoQuad
, ClassS
);
1476 // Emit the FileCheck patterns.
1477 s
+= "// CHECK: test_" + mangledName
+ "\n";
1478 // s += "// CHECK: \n"; // FIXME: + expected instruction opcode.
1480 // Emit the start of the test function.
1481 s
+= TypeString(proto
[0], outTypeStr
) + " test_" + mangledName
+ "(";
1484 for (unsigned i
= 1, e
= proto
.size(); i
!= e
; ++i
, ++arg
) {
1485 // Do not create arguments for values that must be immediate constants.
1486 if (proto
[i
] == 'i')
1488 s
+= comma
+ TypeString(proto
[i
], inTypeStr
) + " ";
1494 if (proto
[0] != 'v')
1496 s
+= mangledName
+ "(";
1498 for (unsigned i
= 1, e
= proto
.size(); i
!= e
; ++i
, ++arg
) {
1499 if (proto
[i
] == 'i') {
1500 // For immediate operands, test the maximum value.
1504 // The immediate generally refers to a lane in the preceding argument.
1505 s
+= utostr(RangeFromType(proto
[i
-1], inTypeStr
));
1516 /// runTests - Write out a complete set of tests for all of the Neon
1518 void NeonEmitter::runTests(raw_ostream
&OS
) {
1520 "// RUN: %clang_cc1 -triple thumbv7-apple-darwin \\\n"
1521 "// RUN: -target-cpu cortex-a9 -ffreestanding -S -o - %s | FileCheck %s\n"
1523 "#include <arm_neon.h>\n"
1526 std::vector
<Record
*> RV
= Records
.getAllDerivedDefinitions("Inst");
1527 for (unsigned i
= 0, e
= RV
.size(); i
!= e
; ++i
) {
1529 std::string name
= R
->getValueAsString("Name");
1530 std::string Proto
= R
->getValueAsString("Prototype");
1531 std::string Types
= R
->getValueAsString("Types");
1532 bool isShift
= R
->getValueAsBit("isShift");
1534 SmallVector
<StringRef
, 16> TypeVec
;
1535 ParseTypes(R
, Types
, TypeVec
);
1537 OpKind kind
= OpMap
[R
->getValueAsDef("Operand")->getName()];
1538 for (unsigned ti
= 0, te
= TypeVec
.size(); ti
!= te
; ++ti
) {
1539 if (kind
== OpReinterpret
) {
1540 bool outQuad
= false;
1542 (void)ClassifyType(TypeVec
[ti
], outQuad
, dummy
, dummy
);
1543 for (unsigned srcti
= 0, srcte
= TypeVec
.size();
1544 srcti
!= srcte
; ++srcti
) {
1545 bool inQuad
= false;
1546 (void)ClassifyType(TypeVec
[srcti
], inQuad
, dummy
, dummy
);
1547 if (srcti
== ti
|| inQuad
!= outQuad
)
1549 OS
<< GenTest(name
, Proto
, TypeVec
[ti
], TypeVec
[srcti
], isShift
);
1552 OS
<< GenTest(name
, Proto
, TypeVec
[ti
], TypeVec
[ti
], isShift
);