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.
23 //===----------------------------------------------------------------------===//
25 #include "NeonEmitter.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/StringExtras.h"
33 /// ParseTypes - break down a string such as "fQf" into a vector of StringRefs,
34 /// which each StringRef representing a single type declared in the string.
35 /// for "fQf" we would end up with 2 StringRefs, "f", and "Qf", representing
36 /// 2xfloat and 4xfloat respectively.
37 static void ParseTypes(Record *r, std::string &s,
38 SmallVectorImpl<StringRef> &TV) {
39 const char *data = s.data();
42 for (unsigned i = 0, e = s.size(); i != e; ++i, ++len) {
43 if (data[len] == 'P' || data[len] == 'Q' || data[len] == 'U')
55 throw TGError(r->getLoc(),
56 "Unexpected letter: " + std::string(data + len, 1));
59 TV.push_back(StringRef(data, len + 1));
65 /// Widen - Convert a type code into the next wider type. char -> short,
66 /// short -> int, etc.
67 static char Widen(const char t) {
75 default: throw "unhandled type in widen!";
80 /// Narrow - Convert a type code into the next smaller type. short -> char,
81 /// float -> half float, etc.
82 static char Narrow(const char t) {
92 default: throw "unhandled type in narrow!";
97 /// For a particular StringRef, return the base type code, and whether it has
98 /// the quad-vector, polynomial, or unsigned modifiers set.
99 static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) {
103 if (ty[off] == 'Q') {
109 if (ty[off] == 'P') {
114 // remember unsigned.
115 if (ty[off] == 'U') {
120 // base type to get the type string for.
124 /// ModType - Transform a type code and its modifiers based on a mod code. The
125 /// mod code definitions may be found at the top of arm_neon.td.
126 static char ModType(const char mod, char type, bool &quad, bool &poly,
127 bool &usgn, bool &scal, bool &cnst, bool &pntr) {
200 /// TypeString - for a modifier and type, generate the name of the typedef for
201 /// that type. QUc -> uint8x8_t.
202 static std::string TypeString(const char mod, StringRef typestr) {
215 // base type to get the type string for.
216 char type = ClassifyType(typestr, quad, poly, usgn);
218 // Based on the modifying character, change the type and width if necessary.
219 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
228 s += poly ? "poly8" : "int8";
231 s += quad ? "x16" : "x8";
234 s += poly ? "poly16" : "int16";
237 s += quad ? "x8" : "x4";
243 s += quad ? "x4" : "x2";
249 s += quad ? "x2" : "x1";
255 s += quad ? "x8" : "x4";
261 s += quad ? "x4" : "x2";
264 throw "unhandled type!";
275 // Append _t, finishing the type string typedef type.
287 /// BuiltinTypeString - for a modifier and type, generate the clang
288 /// BuiltinsARM.def prototype code for the function. See the top of clang's
289 /// Builtins.def for a description of the type strings.
290 static std::string BuiltinTypeString(const char mod, StringRef typestr,
291 ClassKind ck, bool ret) {
304 // base type to get the type string for.
305 char type = ClassifyType(typestr, quad, poly, usgn);
307 // Based on the modifying character, change the type and width if necessary.
308 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
310 // All pointers are void* pointers. Change type to 'v' now.
316 // Treat half-float ('h') types as unsigned short ('s') types.
321 usgn = usgn | poly | ((ck == ClassI || ck == ClassW) && scal && type != 'f');
328 else if (type == 'c')
329 s.push_back('S'); // make chars explicitly signed
331 if (type == 'l') // 64-bit long
343 // Since the return value must be one type, return a vector type of the
344 // appropriate width which we will bitcast. An exception is made for
345 // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
346 // fashion, storing them to a pointer arg.
348 if (mod >= '2' && mod <= '4')
349 return "vv*"; // void result with void* first argument
350 if (mod == 'f' || (ck != ClassB && type == 'f'))
351 return quad ? "V4f" : "V2f";
352 if (ck != ClassB && type == 's')
353 return quad ? "V8s" : "V4s";
354 if (ck != ClassB && type == 'i')
355 return quad ? "V4i" : "V2i";
356 if (ck != ClassB && type == 'l')
357 return quad ? "V2LLi" : "V1LLi";
359 return quad ? "V16Sc" : "V8Sc";
362 // Non-return array types are passed as individual vectors.
364 return quad ? "V16ScV16Sc" : "V8ScV8Sc";
366 return quad ? "V16ScV16ScV16Sc" : "V8ScV8ScV8Sc";
368 return quad ? "V16ScV16ScV16ScV16Sc" : "V8ScV8ScV8ScV8Sc";
370 if (mod == 'f' || (ck != ClassB && type == 'f'))
371 return quad ? "V4f" : "V2f";
372 if (ck != ClassB && type == 's')
373 return quad ? "V8s" : "V4s";
374 if (ck != ClassB && type == 'i')
375 return quad ? "V4i" : "V2i";
376 if (ck != ClassB && type == 'l')
377 return quad ? "V2LLi" : "V1LLi";
379 return quad ? "V16Sc" : "V8Sc";
382 /// MangleName - Append a type or width suffix to a base neon function name,
383 /// and insert a 'q' in the appropriate location if the operation works on
384 /// 128b rather than 64b. E.g. turn "vst2_lane" into "vst2q_lane_f32", etc.
385 static std::string MangleName(const std::string &name, StringRef typestr,
387 if (name == "vcvt_f32_f16")
393 char type = ClassifyType(typestr, quad, poly, usgn);
395 std::string s = name;
400 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
401 case ClassI: s += "_i8"; break;
402 case ClassW: s += "_8"; break;
408 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
409 case ClassI: s += "_i16"; break;
410 case ClassW: s += "_16"; break;
416 case ClassS: s += usgn ? "_u32" : "_s32"; break;
417 case ClassI: s += "_i32"; break;
418 case ClassW: s += "_32"; break;
424 case ClassS: s += usgn ? "_u64" : "_s64"; break;
425 case ClassI: s += "_i64"; break;
426 case ClassW: s += "_64"; break;
433 case ClassI: s += "_f16"; break;
434 case ClassW: s += "_16"; break;
441 case ClassI: s += "_f32"; break;
442 case ClassW: s += "_32"; break;
447 throw "unhandled type!";
453 // Insert a 'q' before the first '_' character so that it ends up before
454 // _lane or _n on vector-scalar operations.
456 size_t pos = s.find('_');
457 s = s.insert(pos, "q");
462 // Generate the string "(argtype a, argtype b, ...)"
463 static std::string GenArgs(const std::string &proto, StringRef typestr) {
464 bool define = proto.find('i') != std::string::npos;
470 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
472 // Immediate macro arguments are used directly instead of being assigned
473 // to local temporaries; prepend an underscore prefix to make their
474 // names consistent with the local temporaries.
478 s += TypeString(proto[i], typestr) + " __";
489 // Macro arguments are not type-checked like inline function arguments, so
490 // assign them to local temporaries to get the right type checking.
491 static std::string GenMacroLocals(const std::string &proto, StringRef typestr) {
495 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
496 // Do not create a temporary for an immediate argument.
497 // That would defeat the whole point of using a macro!
498 if (proto[i] == 'i') continue;
500 s += TypeString(proto[i], typestr) + " __";
511 // Use the vmovl builtin to sign-extend or zero-extend a vector.
512 static std::string Extend(StringRef typestr, const std::string &a) {
514 s = MangleName("vmovl", typestr, ClassS);
519 static std::string Duplicate(unsigned nElts, StringRef typestr,
520 const std::string &a) {
523 s = "(" + TypeString('d', typestr) + "){ ";
524 for (unsigned i = 0; i != nElts; ++i) {
534 static std::string SplatLane(unsigned nElts, const std::string &vec,
535 const std::string &lane) {
536 std::string s = "__builtin_shufflevector(" + vec + ", " + vec;
537 for (unsigned i = 0; i < nElts; ++i)
543 static unsigned GetNumElements(StringRef typestr, bool &quad) {
546 char type = ClassifyType(typestr, quad, dummy, dummy);
549 case 'c': nElts = 8; break;
550 case 's': nElts = 4; break;
551 case 'i': nElts = 2; break;
552 case 'l': nElts = 1; break;
553 case 'h': nElts = 4; break;
554 case 'f': nElts = 2; break;
556 throw "unhandled type!";
559 if (quad) nElts <<= 1;
563 // Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
564 static std::string GenOpString(OpKind op, const std::string &proto,
567 unsigned nElts = GetNumElements(typestr, quad);
569 // If this builtin takes an immediate argument, we need to #define it rather
570 // than use a standard declaration, so that SemaChecking can range check
571 // the immediate passed by the user.
572 bool define = proto.find('i') != std::string::npos;
574 std::string ts = TypeString(proto[0], typestr);
576 if (op == OpHi || op == OpLo) {
577 s = "union { " + ts + " r; double d; } u; u.d = ";
578 } else if (!define) {
587 s += Extend(typestr, "__a") + " + " + Extend(typestr, "__b") + ";";
590 s += "__a + " + Extend(typestr, "__b") + ";";
596 s += Extend(typestr, "__a") + " - " + Extend(typestr, "__b") + ";";
599 s += "__a - " + Extend(typestr, "__b") + ";";
602 s += "__a * " + Duplicate(nElts, typestr, "__b") + ";";
605 s += "__a * " + SplatLane(nElts, "__b", "__c") + ";";
611 s += Extend(typestr, "__a") + " * " +
612 Extend(typestr, Duplicate(nElts << (int)quad, typestr, "__b")) + ";";
615 s += Extend(typestr, "__a") + " * " +
616 Extend(typestr, SplatLane(nElts, "__b", "__c")) + ";";
619 s += Extend(typestr, "__a") + " * " + Extend(typestr, "__b") + ";";
622 s += "__a + (__b * " + Duplicate(nElts, typestr, "__c") + ");";
625 s += "__a + (__b * " + SplatLane(nElts, "__c", "__d") + ");";
628 s += "__a + (__b * __c);";
631 s += "__a + (" + Extend(typestr, "__b") + " * " +
632 Extend(typestr, Duplicate(nElts, typestr, "__c")) + ");";
635 s += "__a + (" + Extend(typestr, "__b") + " * " +
636 Extend(typestr, SplatLane(nElts, "__c", "__d")) + ");";
639 s += "__a + (" + Extend(typestr, "__b") + " * " +
640 Extend(typestr, "__c") + ");";
643 s += "__a - (__b * " + Duplicate(nElts, typestr, "__c") + ");";
646 s += "__a - (__b * " + SplatLane(nElts, "__c", "__d") + ");";
649 s += "__a - (__b * __c);";
652 s += "__a - (" + Extend(typestr, "__b") + " * " +
653 Extend(typestr, Duplicate(nElts, typestr, "__c")) + ");";
656 s += "__a - (" + Extend(typestr, "__b") + " * " +
657 Extend(typestr, SplatLane(nElts, "__c", "__d")) + ");";
660 s += "__a - (" + Extend(typestr, "__b") + " * " +
661 Extend(typestr, "__c") + ");";
664 s += MangleName("vqdmull", typestr, ClassS) + "(__a, " +
665 SplatLane(nElts, "__b", "__c") + ");";
668 s += MangleName("vqdmlal", typestr, ClassS) + "(__a, " +
669 SplatLane(nElts, "__b", "__c") + ");";
672 s += MangleName("vqdmlsl", typestr, ClassS) + "(__a, " +
673 SplatLane(nElts, "__b", "__c") + ");";
676 s += MangleName("vqdmulh", typestr, ClassS) + "(__a, " +
677 SplatLane(nElts, "__b", "__c") + ");";
680 s += MangleName("vqrdmulh", typestr, ClassS) + "(__a, " +
681 SplatLane(nElts, "__b", "__c") + ");";
684 s += "(" + ts + ")(__a == __b);";
687 s += "(" + ts + ")(__a >= __b);";
690 s += "(" + ts + ")(__a <= __b);";
693 s += "(" + ts + ")(__a > __b);";
696 s += "(" + ts + ")(__a < __b);";
720 s += "(" + ts + ")__a;";
723 s += "(" + ts + ")__builtin_shufflevector((int64x1_t)__a";
724 s += ", (int64x1_t)__b, 0, 1);";
727 s += "(((float64x2_t)__a)[1]);";
730 s += "(((float64x2_t)__a)[0]);";
733 s += Duplicate(nElts, typestr, "__a") + ";";
736 s += SplatLane(nElts, "__a", "__b") + ";";
739 // ((0 & 1) | (~0 & 2))
741 ts = TypeString(proto[1], typestr);
742 s += "((__a & (" + ts + ")__b) | ";
743 s += "(~__a & (" + ts + ")__c));";
746 s += "__builtin_shufflevector(__a, __a";
747 for (unsigned i = 2; i <= nElts; i += 2)
748 for (unsigned j = 0; j != 2; ++j)
749 s += ", " + utostr(i - j - 1);
753 unsigned WordElts = nElts >> (1 + (int)quad);
754 s += "__builtin_shufflevector(__a, __a";
755 for (unsigned i = WordElts; i <= nElts; i += WordElts)
756 for (unsigned j = 0; j != WordElts; ++j)
757 s += ", " + utostr(i - j - 1);
762 unsigned DblWordElts = nElts >> (int)quad;
763 s += "__builtin_shufflevector(__a, __a";
764 for (unsigned i = DblWordElts; i <= nElts; i += DblWordElts)
765 for (unsigned j = 0; j != DblWordElts; ++j)
766 s += ", " + utostr(i - j - 1);
771 std::string abd = MangleName("vabd", typestr, ClassS) + "(__a, __b)";
772 if (typestr[0] != 'U') {
773 // vabd results are always unsigned and must be zero-extended.
774 std::string utype = "U" + typestr.str();
775 s += "(" + TypeString(proto[0], typestr) + ")";
776 abd = "(" + TypeString('d', utype) + ")" + abd;
777 s += Extend(utype, abd) + ";";
779 s += Extend(typestr, abd) + ";";
784 s += "__a + " + MangleName("vabd", typestr, ClassS) + "(__b, __c);";
788 std::string abd = MangleName("vabd", typestr, ClassS) + "(__b, __c)";
789 if (typestr[0] != 'U') {
790 // vabd results are always unsigned and must be zero-extended.
791 std::string utype = "U" + typestr.str();
792 s += "(" + TypeString(proto[0], typestr) + ")";
793 abd = "(" + TypeString('d', utype) + ")" + abd;
794 s += Extend(utype, abd) + ";";
796 s += Extend(typestr, abd) + ";";
801 throw "unknown OpKind!";
804 if (op == OpHi || op == OpLo) {
812 static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
813 unsigned mod = proto[0];
816 if (mod == 'v' || mod == 'f')
826 // Base type to get the type string for.
827 char type = ClassifyType(typestr, quad, poly, usgn);
829 // Based on the modifying character, change the type and width if necessary.
830 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
834 if (quad && proto[1] != 'g')
857 throw "unhandled type!";
863 // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
864 static std::string GenBuiltin(const std::string &name, const std::string &proto,
865 StringRef typestr, ClassKind ck) {
868 // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
869 // sret-like argument.
870 bool sret = (proto[0] >= '2' && proto[0] <= '4');
872 // If this builtin takes an immediate argument, we need to #define it rather
873 // than use a standard declaration, so that SemaChecking can range check
874 // the immediate passed by the user.
875 bool define = proto.find('i') != std::string::npos;
877 // Check if the prototype has a scalar operand with the type of the vector
878 // elements. If not, bitcasting the args will take care of arg checking.
879 // The actual signedness etc. will be taken care of with special enums.
880 if (proto.find('s') == std::string::npos)
883 if (proto[0] != 'v') {
884 std::string ts = TypeString(proto[0], typestr);
894 s += "return (" + ts + ")";
898 bool splat = proto.find('a') != std::string::npos;
900 s += "__builtin_neon_";
902 // Call the non-splat builtin: chop off the "_n" suffix from the name.
903 std::string vname(name, 0, name.size()-2);
904 s += MangleName(vname, typestr, ck);
906 s += MangleName(name, typestr, ck);
910 // Pass the address of the return variable as the first argument to sret-like
916 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
917 std::string args = std::string(&arg, 1);
919 // Use the local temporaries instead of the macro arguments.
922 bool argQuad = false;
923 bool argPoly = false;
924 bool argUsgn = false;
925 bool argScalar = false;
927 char argType = ClassifyType(typestr, argQuad, argPoly, argUsgn);
928 argType = ModType(proto[i], argType, argQuad, argPoly, argUsgn, argScalar,
931 // Handle multiple-vector values specially, emitting each subvector as an
932 // argument to the __builtin.
933 if (proto[i] >= '2' && proto[i] <= '4') {
934 // Check if an explicit cast is needed.
935 if (argType != 'c' || argPoly || argUsgn)
936 args = (argQuad ? "(int8x16_t)" : "(int8x8_t)") + args;
938 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
939 s += args + ".val[" + utostr(vi) + "]";
949 if (splat && (i + 1) == e)
950 args = Duplicate(GetNumElements(typestr, argQuad), typestr, args);
952 // Check if an explicit cast is needed.
953 if ((splat || !argScalar) &&
954 ((ck == ClassB && argType != 'c') || argPoly || argUsgn)) {
955 std::string argTypeStr = "c";
957 argTypeStr = argType;
959 argTypeStr = "Q" + argTypeStr;
960 args = "(" + TypeString('d', argTypeStr) + ")" + args;
968 // Extra constant integer to hold type class enum for this function, e.g. s8
970 s += ", " + utostr(GetNeonEnum(proto, typestr));
974 if (proto[0] != 'v' && sret) {
983 static std::string GenBuiltinDef(const std::string &name,
984 const std::string &proto,
985 StringRef typestr, ClassKind ck) {
986 std::string s("BUILTIN(__builtin_neon_");
988 // If all types are the same size, bitcasting the args will take care
989 // of arg checking. The actual signedness etc. will be taken care of with
991 if (proto.find('s') == std::string::npos)
994 s += MangleName(name, typestr, ck);
997 for (unsigned i = 0, e = proto.size(); i != e; ++i)
998 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
1000 // Extra constant integer to hold type class enum for this function, e.g. s8
1008 static std::string GenIntrinsic(const std::string &name,
1009 const std::string &proto,
1010 StringRef outTypeStr, StringRef inTypeStr,
1011 OpKind kind, ClassKind classKind) {
1012 assert(!proto.empty() && "");
1013 bool define = proto.find('i') != std::string::npos;
1016 // static always inline + return type
1020 s += "__ai " + TypeString(proto[0], outTypeStr) + " ";
1022 // Function name with type suffix
1023 std::string mangledName = MangleName(name, outTypeStr, ClassS);
1024 if (outTypeStr != inTypeStr) {
1025 // If the input type is different (e.g., for vreinterpret), append a suffix
1026 // for the input type. String off a "Q" (quad) prefix so that MangleName
1027 // does not insert another "q" in the name.
1028 unsigned typeStrOff = (inTypeStr[0] == 'Q' ? 1 : 0);
1029 StringRef inTypeNoQuad = inTypeStr.substr(typeStrOff);
1030 mangledName = MangleName(mangledName, inTypeNoQuad, ClassS);
1034 // Function arguments
1035 s += GenArgs(proto, inTypeStr);
1039 s += " __extension__ ({ \\\n ";
1040 s += GenMacroLocals(proto, inTypeStr);
1046 s += GenOpString(kind, proto, outTypeStr);
1048 s += GenBuiltin(name, proto, outTypeStr, classKind);
1057 /// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h
1058 /// is comprised of type definitions and function declarations.
1059 void NeonEmitter::run(raw_ostream &OS) {
1061 "/*===---- arm_neon.h - ARM Neon intrinsics ------------------------------"
1064 " * Permission is hereby granted, free of charge, to any person obtaining "
1066 " * of this software and associated documentation files (the \"Software\"),"
1068 " * in the Software without restriction, including without limitation the "
1070 " * to use, copy, modify, merge, publish, distribute, sublicense, "
1072 " * copies of the Software, and to permit persons to whom the Software is\n"
1073 " * furnished to do so, subject to the following conditions:\n"
1075 " * The above copyright notice and this permission notice shall be "
1077 " * all copies or substantial portions of the Software.\n"
1079 " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, "
1081 " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
1082 "MERCHANTABILITY,\n"
1083 " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT "
1085 " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR "
1087 " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, "
1089 " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER "
1091 " * THE SOFTWARE.\n"
1093 " *===--------------------------------------------------------------------"
1097 OS << "#ifndef __ARM_NEON_H\n";
1098 OS << "#define __ARM_NEON_H\n\n";
1100 OS << "#ifndef __ARM_NEON__\n";
1101 OS << "#error \"NEON support not enabled\"\n";
1104 OS << "#include <stdint.h>\n\n";
1106 // Emit NEON-specific scalar typedefs.
1107 OS << "typedef float float32_t;\n";
1108 OS << "typedef int8_t poly8_t;\n";
1109 OS << "typedef int16_t poly16_t;\n";
1110 OS << "typedef uint16_t float16_t;\n";
1112 // Emit Neon vector typedefs.
1113 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
1114 SmallVector<StringRef, 24> TDTypeVec;
1115 ParseTypes(0, TypedefTypes, TDTypeVec);
1117 // Emit vector typedefs.
1118 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
1119 bool dummy, quad = false, poly = false;
1120 (void) ClassifyType(TDTypeVec[i], quad, poly, dummy);
1122 OS << "typedef __attribute__((neon_polyvector_type(";
1124 OS << "typedef __attribute__((neon_vector_type(";
1126 unsigned nElts = GetNumElements(TDTypeVec[i], quad);
1127 OS << utostr(nElts) << "))) ";
1131 OS << TypeString('s', TDTypeVec[i]);
1132 OS << " " << TypeString('d', TDTypeVec[i]) << ";\n";
1135 OS << "typedef __attribute__((__vector_size__(8))) "
1136 "double float64x1_t;\n";
1137 OS << "typedef __attribute__((__vector_size__(16))) "
1138 "double float64x2_t;\n";
1141 // Emit struct typedefs.
1142 for (unsigned vi = 2; vi != 5; ++vi) {
1143 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
1144 std::string ts = TypeString('d', TDTypeVec[i]);
1145 std::string vs = TypeString('0' + vi, TDTypeVec[i]);
1146 OS << "typedef struct " << vs << " {\n";
1147 OS << " " << ts << " val";
1148 OS << "[" << utostr(vi) << "]";
1150 OS << vs << ";\n\n";
1154 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
1156 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1158 // Emit vmovl and vabd intrinsics first so they can be used by other
1159 // intrinsics. (Some of the saturating multiply instructions are also
1160 // used to implement the corresponding "_lane" variants, but tablegen
1161 // sorts the records into alphabetical order so that the "_lane" variants
1162 // come after the intrinsics they use.)
1163 emitIntrinsic(OS, Records.getDef("VMOVL"));
1164 emitIntrinsic(OS, Records.getDef("VABD"));
1166 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1168 if (R->getName() != "VMOVL" && R->getName() != "VABD")
1169 emitIntrinsic(OS, R);
1172 OS << "#undef __ai\n\n";
1173 OS << "#endif /* __ARM_NEON_H */\n";
1176 /// emitIntrinsic - Write out the arm_neon.h header file definitions for the
1177 /// intrinsics specified by record R.
1178 void NeonEmitter::emitIntrinsic(raw_ostream &OS, Record *R) {
1179 std::string name = R->getValueAsString("Name");
1180 std::string Proto = R->getValueAsString("Prototype");
1181 std::string Types = R->getValueAsString("Types");
1183 SmallVector<StringRef, 16> TypeVec;
1184 ParseTypes(R, Types, TypeVec);
1186 OpKind kind = OpMap[R->getValueAsDef("Operand")->getName()];
1188 ClassKind classKind = ClassNone;
1189 if (R->getSuperClasses().size() >= 2)
1190 classKind = ClassMap[R->getSuperClasses()[1]];
1191 if (classKind == ClassNone && kind == OpNone)
1192 throw TGError(R->getLoc(), "Builtin has no class kind");
1194 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1195 if (kind == OpReinterpret) {
1196 bool outQuad = false;
1198 (void)ClassifyType(TypeVec[ti], outQuad, dummy, dummy);
1199 for (unsigned srcti = 0, srcte = TypeVec.size();
1200 srcti != srcte; ++srcti) {
1201 bool inQuad = false;
1202 (void)ClassifyType(TypeVec[srcti], inQuad, dummy, dummy);
1203 if (srcti == ti || inQuad != outQuad)
1205 OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[srcti],
1209 OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[ti],
1216 static unsigned RangeFromType(StringRef typestr) {
1217 // base type to get the type string for.
1218 bool quad = false, dummy = false;
1219 char type = ClassifyType(typestr, quad, dummy, dummy);
1223 return (8 << (int)quad) - 1;
1226 return (4 << (int)quad) - 1;
1229 return (2 << (int)quad) - 1;
1231 return (1 << (int)quad) - 1;
1233 throw "unhandled type!";
1236 assert(0 && "unreachable");
1240 /// runHeader - Emit a file with sections defining:
1241 /// 1. the NEON section of BuiltinsARM.def.
1242 /// 2. the SemaChecking code for the type overload checking.
1243 /// 3. the SemaChecking code for validation of intrinsic immedate arguments.
1244 void NeonEmitter::runHeader(raw_ostream &OS) {
1245 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1247 StringMap<OpKind> EmittedMap;
1249 // Generate BuiltinsARM.def for NEON
1250 OS << "#ifdef GET_NEON_BUILTINS\n";
1251 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1253 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1257 std::string Proto = R->getValueAsString("Prototype");
1259 // Functions with 'a' (the splat code) in the type prototype should not get
1260 // their own builtin as they use the non-splat variant.
1261 if (Proto.find('a') != std::string::npos)
1264 std::string Types = R->getValueAsString("Types");
1265 SmallVector<StringRef, 16> TypeVec;
1266 ParseTypes(R, Types, TypeVec);
1268 if (R->getSuperClasses().size() < 2)
1269 throw TGError(R->getLoc(), "Builtin has no class kind");
1271 std::string name = R->getValueAsString("Name");
1272 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1274 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1275 // Generate the BuiltinsARM.def declaration for this builtin, ensuring
1276 // that each unique BUILTIN() macro appears only once in the output
1278 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
1279 if (EmittedMap.count(bd))
1282 EmittedMap[bd] = OpNone;
1288 // Generate the overloaded type checking code for SemaChecking.cpp
1289 OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
1290 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1292 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1296 std::string Proto = R->getValueAsString("Prototype");
1297 std::string Types = R->getValueAsString("Types");
1298 std::string name = R->getValueAsString("Name");
1300 // Functions with 'a' (the splat code) in the type prototype should not get
1301 // their own builtin as they use the non-splat variant.
1302 if (Proto.find('a') != std::string::npos)
1305 // Functions which have a scalar argument cannot be overloaded, no need to
1306 // check them if we are emitting the type checking code.
1307 if (Proto.find('s') != std::string::npos)
1310 SmallVector<StringRef, 16> TypeVec;
1311 ParseTypes(R, Types, TypeVec);
1313 if (R->getSuperClasses().size() < 2)
1314 throw TGError(R->getLoc(), "Builtin has no class kind");
1316 int si = -1, qi = -1;
1317 unsigned mask = 0, qmask = 0;
1318 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1319 // Generate the switch case(s) for this builtin for the type validation.
1320 bool quad = false, poly = false, usgn = false;
1321 (void) ClassifyType(TypeVec[ti], quad, poly, usgn);
1325 qmask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1328 mask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1332 OS << "case ARM::BI__builtin_neon_"
1333 << MangleName(name, TypeVec[si], ClassB)
1334 << ": mask = " << "0x" << utohexstr(mask) << "; break;\n";
1336 OS << "case ARM::BI__builtin_neon_"
1337 << MangleName(name, TypeVec[qi], ClassB)
1338 << ": mask = " << "0x" << utohexstr(qmask) << "; break;\n";
1342 // Generate the intrinsic range checking code for shift/lane immediates.
1343 OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
1344 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1347 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1351 std::string name = R->getValueAsString("Name");
1352 std::string Proto = R->getValueAsString("Prototype");
1353 std::string Types = R->getValueAsString("Types");
1355 // Functions with 'a' (the splat code) in the type prototype should not get
1356 // their own builtin as they use the non-splat variant.
1357 if (Proto.find('a') != std::string::npos)
1360 // Functions which do not have an immediate do not need to have range
1361 // checking code emitted.
1362 if (Proto.find('i') == std::string::npos)
1365 SmallVector<StringRef, 16> TypeVec;
1366 ParseTypes(R, Types, TypeVec);
1368 if (R->getSuperClasses().size() < 2)
1369 throw TGError(R->getLoc(), "Builtin has no class kind");
1371 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1373 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1374 std::string namestr, shiftstr, rangestr;
1376 // Builtins which are overloaded by type will need to have their upper
1377 // bound computed at Sema time based on the type constant.
1378 if (Proto.find('s') == std::string::npos) {
1380 if (R->getValueAsBit("isShift")) {
1381 shiftstr = ", true";
1383 // Right shifts have an 'r' in the name, left shifts do not.
1384 if (name.find('r') != std::string::npos)
1385 rangestr = "l = 1; ";
1387 rangestr += "u = RFT(TV" + shiftstr + ")";
1389 rangestr = "u = " + utostr(RangeFromType(TypeVec[ti]));
1391 // Make sure cases appear only once by uniquing them in a string map.
1392 namestr = MangleName(name, TypeVec[ti], ck);
1393 if (EmittedMap.count(namestr))
1395 EmittedMap[namestr] = OpNone;
1397 // Calculate the index of the immediate that should be range checked.
1398 unsigned immidx = 0;
1400 // Builtins that return a struct of multiple vectors have an extra
1401 // leading arg for the struct return.
1402 if (Proto[0] >= '2' && Proto[0] <= '4')
1405 // Add one to the index for each argument until we reach the immediate
1406 // to be checked. Structs of vectors are passed as multiple arguments.
1407 for (unsigned ii = 1, ie = Proto.size(); ii != ie; ++ii) {
1408 switch (Proto[ii]) {
1409 default: immidx += 1; break;
1410 case '2': immidx += 2; break;
1411 case '3': immidx += 3; break;
1412 case '4': immidx += 4; break;
1413 case 'i': ie = ii + 1; break;
1416 OS << "case ARM::BI__builtin_neon_" << MangleName(name, TypeVec[ti], ck)
1417 << ": i = " << immidx << "; " << rangestr << "; break;\n";