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(const std::string &proto, StringRef typestr,
513 const std::string &a) {
515 s = MangleName("vmovl", typestr, ClassS);
520 static std::string Duplicate(unsigned nElts, StringRef typestr,
521 const std::string &a) {
524 s = "(" + TypeString('d', typestr) + "){ ";
525 for (unsigned i = 0; i != nElts; ++i) {
535 static std::string SplatLane(unsigned nElts, const std::string &vec,
536 const std::string &lane) {
537 std::string s = "__builtin_shufflevector(" + vec + ", " + vec;
538 for (unsigned i = 0; i < nElts; ++i)
544 static unsigned GetNumElements(StringRef typestr, bool &quad) {
547 char type = ClassifyType(typestr, quad, dummy, dummy);
550 case 'c': nElts = 8; break;
551 case 's': nElts = 4; break;
552 case 'i': nElts = 2; break;
553 case 'l': nElts = 1; break;
554 case 'h': nElts = 4; break;
555 case 'f': nElts = 2; break;
557 throw "unhandled type!";
560 if (quad) nElts <<= 1;
564 // Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
565 static std::string GenOpString(OpKind op, const std::string &proto,
568 unsigned nElts = GetNumElements(typestr, quad);
570 // If this builtin takes an immediate argument, we need to #define it rather
571 // than use a standard declaration, so that SemaChecking can range check
572 // the immediate passed by the user.
573 bool define = proto.find('i') != std::string::npos;
575 std::string ts = TypeString(proto[0], typestr);
577 if (op == OpHi || op == OpLo) {
578 s = "union { " + ts + " r; double d; } u; u.d = ";
579 } else if (!define) {
591 s += "__a * " + Duplicate(nElts, typestr, "__b") + ";";
594 s += "__a * " + SplatLane(nElts, "__b", "__c") + ";";
600 s += Extend(proto, typestr, "__a") + " * " +
601 Extend(proto, typestr,
602 Duplicate(nElts << (int)quad, typestr, "__b")) + ";";
605 s += Extend(proto, typestr, "__a") + " * " +
606 Extend(proto, typestr, "__b") + ";";
609 s += "__a + (__b * " + Duplicate(nElts, typestr, "__c") + ");";
612 s += "__a + (__b * " + SplatLane(nElts, "__c", "__d") + ");";
615 s += "__a + (__b * __c);";
618 s += "__a - (__b * " + Duplicate(nElts, typestr, "__c") + ");";
621 s += "__a - (__b * " + SplatLane(nElts, "__c", "__d") + ");";
624 s += "__a - (__b * __c);";
627 s += "(" + ts + ")(__a == __b);";
630 s += "(" + ts + ")(__a >= __b);";
633 s += "(" + ts + ")(__a <= __b);";
636 s += "(" + ts + ")(__a > __b);";
639 s += "(" + ts + ")(__a < __b);";
663 s += "(" + ts + ")__a;";
666 s += "(" + ts + ")__builtin_shufflevector((int64x1_t)__a";
667 s += ", (int64x1_t)__b, 0, 1);";
670 s += "(((float64x2_t)__a)[1]);";
673 s += "(((float64x2_t)__a)[0]);";
676 s += Duplicate(nElts, typestr, "__a") + ";";
679 // ((0 & 1) | (~0 & 2))
681 ts = TypeString(proto[1], typestr);
682 s += "((__a & (" + ts + ")__b) | ";
683 s += "(~__a & (" + ts + ")__c));";
686 s += "__builtin_shufflevector(__a, __a";
687 for (unsigned i = 2; i <= nElts; i += 2)
688 for (unsigned j = 0; j != 2; ++j)
689 s += ", " + utostr(i - j - 1);
693 unsigned WordElts = nElts >> (1 + (int)quad);
694 s += "__builtin_shufflevector(__a, __a";
695 for (unsigned i = WordElts; i <= nElts; i += WordElts)
696 for (unsigned j = 0; j != WordElts; ++j)
697 s += ", " + utostr(i - j - 1);
702 unsigned DblWordElts = nElts >> (int)quad;
703 s += "__builtin_shufflevector(__a, __a";
704 for (unsigned i = DblWordElts; i <= nElts; i += DblWordElts)
705 for (unsigned j = 0; j != DblWordElts; ++j)
706 s += ", " + utostr(i - j - 1);
711 throw "unknown OpKind!";
714 if (op == OpHi || op == OpLo) {
722 static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
723 unsigned mod = proto[0];
726 if (mod == 'v' || mod == 'f')
736 // Base type to get the type string for.
737 char type = ClassifyType(typestr, quad, poly, usgn);
739 // Based on the modifying character, change the type and width if necessary.
740 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
744 if (quad && proto[1] != 'g')
767 throw "unhandled type!";
773 // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
774 static std::string GenBuiltin(const std::string &name, const std::string &proto,
775 StringRef typestr, ClassKind ck) {
778 // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
779 // sret-like argument.
780 bool sret = (proto[0] >= '2' && proto[0] <= '4');
782 // If this builtin takes an immediate argument, we need to #define it rather
783 // than use a standard declaration, so that SemaChecking can range check
784 // the immediate passed by the user.
785 bool define = proto.find('i') != std::string::npos;
787 // Check if the prototype has a scalar operand with the type of the vector
788 // elements. If not, bitcasting the args will take care of arg checking.
789 // The actual signedness etc. will be taken care of with special enums.
790 if (proto.find('s') == std::string::npos)
793 if (proto[0] != 'v') {
794 std::string ts = TypeString(proto[0], typestr);
804 s += "return (" + ts + ")";
808 bool splat = proto.find('a') != std::string::npos;
810 s += "__builtin_neon_";
812 // Call the non-splat builtin: chop off the "_n" suffix from the name.
813 std::string vname(name, 0, name.size()-2);
814 s += MangleName(vname, typestr, ck);
816 s += MangleName(name, typestr, ck);
820 // Pass the address of the return variable as the first argument to sret-like
826 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
827 std::string args = std::string(&arg, 1);
829 // Use the local temporaries instead of the macro arguments.
832 bool argQuad = false;
833 bool argPoly = false;
834 bool argUsgn = false;
835 bool argScalar = false;
837 char argType = ClassifyType(typestr, argQuad, argPoly, argUsgn);
838 argType = ModType(proto[i], argType, argQuad, argPoly, argUsgn, argScalar,
841 // Handle multiple-vector values specially, emitting each subvector as an
842 // argument to the __builtin.
843 if (proto[i] >= '2' && proto[i] <= '4') {
844 // Check if an explicit cast is needed.
845 if (argType != 'c' || argPoly || argUsgn)
846 args = (argQuad ? "(int8x16_t)" : "(int8x8_t)") + args;
848 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
849 s += args + ".val[" + utostr(vi) + "]";
859 if (splat && (i + 1) == e)
860 args = Duplicate(GetNumElements(typestr, argQuad), typestr, args);
862 // Check if an explicit cast is needed.
863 if ((splat || !argScalar) &&
864 ((ck == ClassB && argType != 'c') || argPoly || argUsgn)) {
865 std::string argTypeStr = "c";
867 argTypeStr = argType;
869 argTypeStr = "Q" + argTypeStr;
870 args = "(" + TypeString('d', argTypeStr) + ")" + args;
878 // Extra constant integer to hold type class enum for this function, e.g. s8
880 s += ", " + utostr(GetNeonEnum(proto, typestr));
884 if (proto[0] != 'v' && sret) {
893 static std::string GenBuiltinDef(const std::string &name,
894 const std::string &proto,
895 StringRef typestr, ClassKind ck) {
896 std::string s("BUILTIN(__builtin_neon_");
898 // If all types are the same size, bitcasting the args will take care
899 // of arg checking. The actual signedness etc. will be taken care of with
901 if (proto.find('s') == std::string::npos)
904 s += MangleName(name, typestr, ck);
907 for (unsigned i = 0, e = proto.size(); i != e; ++i)
908 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
910 // Extra constant integer to hold type class enum for this function, e.g. s8
918 static std::string GenIntrinsic(const std::string &name,
919 const std::string &proto,
920 StringRef outTypeStr, StringRef inTypeStr,
921 OpKind kind, ClassKind classKind) {
922 assert(!proto.empty() && "");
923 bool define = proto.find('i') != std::string::npos;
926 // static always inline + return type
930 s += "__ai " + TypeString(proto[0], outTypeStr) + " ";
932 // Function name with type suffix
933 std::string mangledName = MangleName(name, outTypeStr, ClassS);
934 if (outTypeStr != inTypeStr) {
935 // If the input type is different (e.g., for vreinterpret), append a suffix
936 // for the input type. String off a "Q" (quad) prefix so that MangleName
937 // does not insert another "q" in the name.
938 unsigned typeStrOff = (inTypeStr[0] == 'Q' ? 1 : 0);
939 StringRef inTypeNoQuad = inTypeStr.substr(typeStrOff);
940 mangledName = MangleName(mangledName, inTypeNoQuad, ClassS);
944 // Function arguments
945 s += GenArgs(proto, inTypeStr);
949 s += " __extension__ ({ \\\n ";
950 s += GenMacroLocals(proto, inTypeStr);
956 s += GenOpString(kind, proto, outTypeStr);
958 s += GenBuiltin(name, proto, outTypeStr, classKind);
967 /// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h
968 /// is comprised of type definitions and function declarations.
969 void NeonEmitter::run(raw_ostream &OS) {
970 EmitSourceFileHeader("ARM NEON Header", OS);
972 // FIXME: emit license into file?
974 OS << "#ifndef __ARM_NEON_H\n";
975 OS << "#define __ARM_NEON_H\n\n";
977 OS << "#ifndef __ARM_NEON__\n";
978 OS << "#error \"NEON support not enabled\"\n";
981 OS << "#include <stdint.h>\n\n";
983 // Emit NEON-specific scalar typedefs.
984 OS << "typedef float float32_t;\n";
985 OS << "typedef int8_t poly8_t;\n";
986 OS << "typedef int16_t poly16_t;\n";
987 OS << "typedef uint16_t float16_t;\n";
989 // Emit Neon vector typedefs.
990 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
991 SmallVector<StringRef, 24> TDTypeVec;
992 ParseTypes(0, TypedefTypes, TDTypeVec);
994 // Emit vector typedefs.
995 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
996 bool dummy, quad = false, poly = false;
997 (void) ClassifyType(TDTypeVec[i], quad, poly, dummy);
999 OS << "typedef __attribute__((neon_polyvector_type(";
1001 OS << "typedef __attribute__((neon_vector_type(";
1003 unsigned nElts = GetNumElements(TDTypeVec[i], quad);
1004 OS << utostr(nElts) << "))) ";
1008 OS << TypeString('s', TDTypeVec[i]);
1009 OS << " " << TypeString('d', TDTypeVec[i]) << ";\n";
1012 OS << "typedef __attribute__((__vector_size__(8))) "
1013 "double float64x1_t;\n";
1014 OS << "typedef __attribute__((__vector_size__(16))) "
1015 "double float64x2_t;\n";
1018 // Emit struct typedefs.
1019 for (unsigned vi = 2; vi != 5; ++vi) {
1020 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
1021 std::string ts = TypeString('d', TDTypeVec[i]);
1022 std::string vs = TypeString('0' + vi, TDTypeVec[i]);
1023 OS << "typedef struct " << vs << " {\n";
1024 OS << " " << ts << " val";
1025 OS << "[" << utostr(vi) << "]";
1027 OS << vs << ";\n\n";
1031 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
1033 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1035 // Unique the return+pattern types, and assign them.
1036 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1038 std::string name = R->getValueAsString("Name");
1039 std::string Proto = R->getValueAsString("Prototype");
1040 std::string Types = R->getValueAsString("Types");
1042 SmallVector<StringRef, 16> TypeVec;
1043 ParseTypes(R, Types, TypeVec);
1045 OpKind kind = OpMap[R->getValueAsDef("Operand")->getName()];
1047 ClassKind classKind = ClassNone;
1048 if (R->getSuperClasses().size() >= 2)
1049 classKind = ClassMap[R->getSuperClasses()[1]];
1050 if (classKind == ClassNone && kind == OpNone)
1051 throw TGError(R->getLoc(), "Builtin has no class kind");
1053 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1054 if (kind == OpReinterpret) {
1055 bool outQuad = false;
1057 (void)ClassifyType(TypeVec[ti], outQuad, dummy, dummy);
1058 for (unsigned srcti = 0, srcte = TypeVec.size();
1059 srcti != srcte; ++srcti) {
1060 bool inQuad = false;
1061 (void)ClassifyType(TypeVec[srcti], inQuad, dummy, dummy);
1062 if (srcti == ti || inQuad != outQuad)
1064 OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[srcti],
1068 OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[ti],
1074 OS << "#undef __ai\n\n";
1075 OS << "#endif /* __ARM_NEON_H */\n";
1078 static unsigned RangeFromType(StringRef typestr) {
1079 // base type to get the type string for.
1080 bool quad = false, dummy = false;
1081 char type = ClassifyType(typestr, quad, dummy, dummy);
1085 return (8 << (int)quad) - 1;
1088 return (4 << (int)quad) - 1;
1091 return (2 << (int)quad) - 1;
1093 return (1 << (int)quad) - 1;
1095 throw "unhandled type!";
1098 assert(0 && "unreachable");
1102 /// runHeader - Emit a file with sections defining:
1103 /// 1. the NEON section of BuiltinsARM.def.
1104 /// 2. the SemaChecking code for the type overload checking.
1105 /// 3. the SemaChecking code for validation of intrinsic immedate arguments.
1106 void NeonEmitter::runHeader(raw_ostream &OS) {
1107 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1109 StringMap<OpKind> EmittedMap;
1111 // Generate BuiltinsARM.def for NEON
1112 OS << "#ifdef GET_NEON_BUILTINS\n";
1113 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1115 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1119 std::string Proto = R->getValueAsString("Prototype");
1121 // Functions with 'a' (the splat code) in the type prototype should not get
1122 // their own builtin as they use the non-splat variant.
1123 if (Proto.find('a') != std::string::npos)
1126 std::string Types = R->getValueAsString("Types");
1127 SmallVector<StringRef, 16> TypeVec;
1128 ParseTypes(R, Types, TypeVec);
1130 if (R->getSuperClasses().size() < 2)
1131 throw TGError(R->getLoc(), "Builtin has no class kind");
1133 std::string name = R->getValueAsString("Name");
1134 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1136 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1137 // Generate the BuiltinsARM.def declaration for this builtin, ensuring
1138 // that each unique BUILTIN() macro appears only once in the output
1140 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
1141 if (EmittedMap.count(bd))
1144 EmittedMap[bd] = OpNone;
1150 // Generate the overloaded type checking code for SemaChecking.cpp
1151 OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
1152 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1154 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1158 std::string Proto = R->getValueAsString("Prototype");
1159 std::string Types = R->getValueAsString("Types");
1160 std::string name = R->getValueAsString("Name");
1162 // Functions with 'a' (the splat code) in the type prototype should not get
1163 // their own builtin as they use the non-splat variant.
1164 if (Proto.find('a') != std::string::npos)
1167 // Functions which have a scalar argument cannot be overloaded, no need to
1168 // check them if we are emitting the type checking code.
1169 if (Proto.find('s') != std::string::npos)
1172 SmallVector<StringRef, 16> TypeVec;
1173 ParseTypes(R, Types, TypeVec);
1175 if (R->getSuperClasses().size() < 2)
1176 throw TGError(R->getLoc(), "Builtin has no class kind");
1178 int si = -1, qi = -1;
1179 unsigned mask = 0, qmask = 0;
1180 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1181 // Generate the switch case(s) for this builtin for the type validation.
1182 bool quad = false, poly = false, usgn = false;
1183 (void) ClassifyType(TypeVec[ti], quad, poly, usgn);
1187 qmask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1190 mask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1194 OS << "case ARM::BI__builtin_neon_"
1195 << MangleName(name, TypeVec[si], ClassB)
1196 << ": mask = " << "0x" << utohexstr(mask) << "; break;\n";
1198 OS << "case ARM::BI__builtin_neon_"
1199 << MangleName(name, TypeVec[qi], ClassB)
1200 << ": mask = " << "0x" << utohexstr(qmask) << "; break;\n";
1204 // Generate the intrinsic range checking code for shift/lane immediates.
1205 OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
1206 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1209 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1213 std::string name = R->getValueAsString("Name");
1214 std::string Proto = R->getValueAsString("Prototype");
1215 std::string Types = R->getValueAsString("Types");
1217 // Functions with 'a' (the splat code) in the type prototype should not get
1218 // their own builtin as they use the non-splat variant.
1219 if (Proto.find('a') != std::string::npos)
1222 // Functions which do not have an immediate do not need to have range
1223 // checking code emitted.
1224 if (Proto.find('i') == std::string::npos)
1227 SmallVector<StringRef, 16> TypeVec;
1228 ParseTypes(R, Types, TypeVec);
1230 if (R->getSuperClasses().size() < 2)
1231 throw TGError(R->getLoc(), "Builtin has no class kind");
1233 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1235 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1236 std::string namestr, shiftstr, rangestr;
1238 // Builtins which are overloaded by type will need to have their upper
1239 // bound computed at Sema time based on the type constant.
1240 if (Proto.find('s') == std::string::npos) {
1242 if (R->getValueAsBit("isShift")) {
1243 shiftstr = ", true";
1245 // Right shifts have an 'r' in the name, left shifts do not.
1246 if (name.find('r') != std::string::npos)
1247 rangestr = "l = 1; ";
1249 rangestr += "u = RFT(TV" + shiftstr + ")";
1251 rangestr = "u = " + utostr(RangeFromType(TypeVec[ti]));
1253 // Make sure cases appear only once by uniquing them in a string map.
1254 namestr = MangleName(name, TypeVec[ti], ck);
1255 if (EmittedMap.count(namestr))
1257 EmittedMap[namestr] = OpNone;
1259 // Calculate the index of the immediate that should be range checked.
1260 unsigned immidx = 0;
1262 // Builtins that return a struct of multiple vectors have an extra
1263 // leading arg for the struct return.
1264 if (Proto[0] >= '2' && Proto[0] <= '4')
1267 // Add one to the index for each argument until we reach the immediate
1268 // to be checked. Structs of vectors are passed as multiple arguments.
1269 for (unsigned ii = 1, ie = Proto.size(); ii != ie; ++ii) {
1270 switch (Proto[ii]) {
1271 default: immidx += 1; break;
1272 case '2': immidx += 2; break;
1273 case '3': immidx += 3; break;
1274 case '4': immidx += 4; break;
1275 case 'i': ie = ii + 1; break;
1278 OS << "case ARM::BI__builtin_neon_" << MangleName(name, TypeVec[ti], ck)
1279 << ": i = " << immidx << "; " << rangestr << "; break;\n";