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 s += TypeString(proto[i], typestr);
484 // Generate the local temporaries used to provide type checking for macro
486 static std::string GenMacroLocals(const std::string &proto, StringRef typestr) {
490 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
491 // Do not create a temporary for an immediate argument.
492 // That would defeat the whole point of using a macro!
493 if (proto[i] == 'i') continue;
495 s += TypeString(proto[i], typestr) + " __";
506 static std::string Duplicate(unsigned nElts, StringRef typestr,
507 const std::string &a) {
510 s = "(" + TypeString('d', typestr) + "){ ";
511 for (unsigned i = 0; i != nElts; ++i) {
521 static unsigned GetNumElements(StringRef typestr, bool &quad) {
524 char type = ClassifyType(typestr, quad, dummy, dummy);
527 case 'c': nElts = 8; break;
528 case 's': nElts = 4; break;
529 case 'i': nElts = 2; break;
530 case 'l': nElts = 1; break;
531 case 'h': nElts = 4; break;
532 case 'f': nElts = 2; break;
534 throw "unhandled type!";
537 if (quad) nElts <<= 1;
541 // Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
542 static std::string GenOpString(OpKind op, const std::string &proto,
545 unsigned nElts = GetNumElements(typestr, quad);
547 std::string ts = TypeString(proto[0], typestr);
549 if (op == OpHi || op == OpLo) {
550 s = "union { " + ts + " r; double d; } u; u.d = ";
563 s += "a * " + Duplicate(nElts, typestr, "b") + ";";
569 s += "a + (b * " + Duplicate(nElts, typestr, "c") + ");";
575 s += "a - (b * " + Duplicate(nElts, typestr, "c") + ");";
581 s += "(" + ts + ")(a == b);";
584 s += "(" + ts + ")(a >= b);";
587 s += "(" + ts + ")(a <= b);";
590 s += "(" + ts + ")(a > b);";
593 s += "(" + ts + ")(a < b);";
617 s += "(" + ts + ")a;";
620 s += "(" + ts + ")__builtin_shufflevector((int64x1_t)a";
621 s += ", (int64x1_t)b, 0, 1);";
624 s += "(((float64x2_t)a)[1]);";
627 s += "(((float64x2_t)a)[0]);";
630 s += Duplicate(nElts, typestr, "a") + ";";
633 // ((0 & 1) | (~0 & 2))
635 ts = TypeString(proto[1], typestr);
636 s += "((a & (" + ts + ")b) | ";
637 s += "(~a & (" + ts + ")c));";
640 s += "__builtin_shufflevector(a, a";
641 for (unsigned i = 2; i <= nElts; i += 2)
642 for (unsigned j = 0; j != 2; ++j)
643 s += ", " + utostr(i - j - 1);
647 unsigned WordElts = nElts >> (1 + (int)quad);
648 s += "__builtin_shufflevector(a, a";
649 for (unsigned i = WordElts; i <= nElts; i += WordElts)
650 for (unsigned j = 0; j != WordElts; ++j)
651 s += ", " + utostr(i - j - 1);
656 unsigned DblWordElts = nElts >> (int)quad;
657 s += "__builtin_shufflevector(a, a";
658 for (unsigned i = DblWordElts; i <= nElts; i += DblWordElts)
659 for (unsigned j = 0; j != DblWordElts; ++j)
660 s += ", " + utostr(i - j - 1);
665 throw "unknown OpKind!";
668 if (op == OpHi || op == OpLo)
673 static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
674 unsigned mod = proto[0];
677 if (mod == 'v' || mod == 'f')
687 // Base type to get the type string for.
688 char type = ClassifyType(typestr, quad, poly, usgn);
690 // Based on the modifying character, change the type and width if necessary.
691 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
695 if (quad && proto[1] != 'g')
718 throw "unhandled type!";
724 // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
725 static std::string GenBuiltin(const std::string &name, const std::string &proto,
726 StringRef typestr, ClassKind ck) {
729 // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
730 // sret-like argument.
731 bool sret = (proto[0] >= '2' && proto[0] <= '4');
733 // If this builtin takes an immediate argument, we need to #define it rather
734 // than use a standard declaration, so that SemaChecking can range check
735 // the immediate passed by the user.
736 bool define = proto.find('i') != std::string::npos;
738 // Check if the prototype has a scalar operand with the type of the vector
739 // elements. If not, bitcasting the args will take care of arg checking.
740 // The actual signedness etc. will be taken care of with special enums.
741 if (proto.find('s') == std::string::npos)
744 // Macro arguments are not type-checked like inline function arguments, so
745 // assign them to local temporaries to get the right type checking.
747 s += GenMacroLocals(proto, typestr);
749 if (proto[0] != 'v') {
750 std::string ts = TypeString(proto[0], typestr);
760 s += "return (" + ts + ")";
764 bool splat = proto.find('a') != std::string::npos;
766 s += "__builtin_neon_";
768 // Call the non-splat builtin: chop off the "_n" suffix from the name.
769 std::string vname(name, 0, name.size()-2);
770 s += MangleName(vname, typestr, ck);
772 s += MangleName(name, typestr, ck);
776 // Pass the address of the return variable as the first argument to sret-like
782 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
783 std::string args = std::string(&arg, 1);
785 // For macros, use the local temporaries instead of the macro arguments.
786 if (define && proto[i] != 'i')
789 bool argQuad = false;
790 bool argPoly = false;
791 bool argUsgn = false;
792 bool argScalar = false;
794 char argType = ClassifyType(typestr, argQuad, argPoly, argUsgn);
795 argType = ModType(proto[i], argType, argQuad, argPoly, argUsgn, argScalar,
798 // Handle multiple-vector values specially, emitting each subvector as an
799 // argument to the __builtin.
800 if (proto[i] >= '2' && proto[i] <= '4') {
801 // Check if an explicit cast is needed.
802 if (argType != 'c' || argPoly || argUsgn)
803 args = (argQuad ? "(int8x16_t)" : "(int8x8_t)") + args;
805 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
806 s += args + ".val[" + utostr(vi) + "]";
816 if (splat && (i + 1) == e)
817 args = Duplicate(GetNumElements(typestr, argQuad), typestr, args);
819 // Check if an explicit cast is needed.
820 if ((splat || !argScalar) &&
821 ((ck == ClassB && argType != 'c') || argPoly || argUsgn)) {
822 std::string argTypeStr = "c";
824 argTypeStr = argType;
826 argTypeStr = "Q" + argTypeStr;
827 args = "(" + TypeString('d', argTypeStr) + ")" + args;
835 // Extra constant integer to hold type class enum for this function, e.g. s8
837 s += ", " + utostr(GetNeonEnum(proto, typestr));
841 if (proto[0] != 'v' && sret) {
850 static std::string GenBuiltinDef(const std::string &name,
851 const std::string &proto,
852 StringRef typestr, ClassKind ck) {
853 std::string s("BUILTIN(__builtin_neon_");
855 // If all types are the same size, bitcasting the args will take care
856 // of arg checking. The actual signedness etc. will be taken care of with
858 if (proto.find('s') == std::string::npos)
861 s += MangleName(name, typestr, ck);
864 for (unsigned i = 0, e = proto.size(); i != e; ++i)
865 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
867 // Extra constant integer to hold type class enum for this function, e.g. s8
875 /// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h
876 /// is comprised of type definitions and function declarations.
877 void NeonEmitter::run(raw_ostream &OS) {
878 EmitSourceFileHeader("ARM NEON Header", OS);
880 // FIXME: emit license into file?
882 OS << "#ifndef __ARM_NEON_H\n";
883 OS << "#define __ARM_NEON_H\n\n";
885 OS << "#ifndef __ARM_NEON__\n";
886 OS << "#error \"NEON support not enabled\"\n";
889 OS << "#include <stdint.h>\n\n";
891 // Emit NEON-specific scalar typedefs.
892 OS << "typedef float float32_t;\n";
893 OS << "typedef int8_t poly8_t;\n";
894 OS << "typedef int16_t poly16_t;\n";
895 OS << "typedef uint16_t float16_t;\n";
897 // Emit Neon vector typedefs.
898 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
899 SmallVector<StringRef, 24> TDTypeVec;
900 ParseTypes(0, TypedefTypes, TDTypeVec);
902 // Emit vector typedefs.
903 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
904 bool dummy, quad = false, poly = false;
905 (void) ClassifyType(TDTypeVec[i], quad, poly, dummy);
907 OS << "typedef __attribute__((neon_polyvector_type(";
909 OS << "typedef __attribute__((neon_vector_type(";
911 unsigned nElts = GetNumElements(TDTypeVec[i], quad);
912 OS << utostr(nElts) << "))) ";
916 OS << TypeString('s', TDTypeVec[i]);
917 OS << " " << TypeString('d', TDTypeVec[i]) << ";\n";
920 OS << "typedef __attribute__((__vector_size__(8))) "
921 "double float64x1_t;\n";
922 OS << "typedef __attribute__((__vector_size__(16))) "
923 "double float64x2_t;\n";
926 // Emit struct typedefs.
927 for (unsigned vi = 2; vi != 5; ++vi) {
928 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
929 std::string ts = TypeString('d', TDTypeVec[i]);
930 std::string vs = TypeString('0' + vi, TDTypeVec[i]);
931 OS << "typedef struct " << vs << " {\n";
932 OS << " " << ts << " val";
933 OS << "[" << utostr(vi) << "]";
939 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
941 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
943 // Unique the return+pattern types, and assign them.
944 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
946 std::string name = LowercaseString(R->getName());
947 std::string Proto = R->getValueAsString("Prototype");
948 std::string Types = R->getValueAsString("Types");
950 SmallVector<StringRef, 16> TypeVec;
951 ParseTypes(R, Types, TypeVec);
953 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
955 bool define = Proto.find('i') != std::string::npos;
957 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
958 assert(!Proto.empty() && "");
960 // static always inline + return type
964 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
966 // Function name with type suffix
967 OS << " " << MangleName(name, TypeVec[ti], ClassS);
969 // Function arguments
970 OS << GenArgs(Proto, TypeVec[ti]);
974 OS << " __extension__ ({ \\\n ";
979 OS << GenOpString(k, Proto, TypeVec[ti]);
981 if (R->getSuperClasses().size() < 2)
982 throw TGError(R->getLoc(), "Builtin has no class kind");
984 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
987 throw TGError(R->getLoc(), "Builtin has no class kind");
988 OS << GenBuiltin(name, Proto, TypeVec[ti], ck);
998 OS << "#undef __ai\n\n";
999 OS << "#endif /* __ARM_NEON_H */\n";
1002 static unsigned RangeFromType(StringRef typestr) {
1003 // base type to get the type string for.
1004 bool quad = false, dummy = false;
1005 char type = ClassifyType(typestr, quad, dummy, dummy);
1009 return (8 << (int)quad) - 1;
1012 return (4 << (int)quad) - 1;
1015 return (2 << (int)quad) - 1;
1017 return (1 << (int)quad) - 1;
1019 throw "unhandled type!";
1022 assert(0 && "unreachable");
1026 /// runHeader - Emit a file with sections defining:
1027 /// 1. the NEON section of BuiltinsARM.def.
1028 /// 2. the SemaChecking code for the type overload checking.
1029 /// 3. the SemaChecking code for validation of intrinsic immedate arguments.
1030 void NeonEmitter::runHeader(raw_ostream &OS) {
1031 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1033 StringMap<OpKind> EmittedMap;
1035 // Generate BuiltinsARM.def for NEON
1036 OS << "#ifdef GET_NEON_BUILTINS\n";
1037 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1039 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1043 std::string Proto = R->getValueAsString("Prototype");
1045 // Functions with 'a' (the splat code) in the type prototype should not get
1046 // their own builtin as they use the non-splat variant.
1047 if (Proto.find('a') != std::string::npos)
1050 std::string Types = R->getValueAsString("Types");
1051 SmallVector<StringRef, 16> TypeVec;
1052 ParseTypes(R, Types, TypeVec);
1054 if (R->getSuperClasses().size() < 2)
1055 throw TGError(R->getLoc(), "Builtin has no class kind");
1057 std::string name = LowercaseString(R->getName());
1058 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1060 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1061 // Generate the BuiltinsARM.def declaration for this builtin, ensuring
1062 // that each unique BUILTIN() macro appears only once in the output
1064 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
1065 if (EmittedMap.count(bd))
1068 EmittedMap[bd] = OpNone;
1074 // Generate the overloaded type checking code for SemaChecking.cpp
1075 OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
1076 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1078 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1082 std::string Proto = R->getValueAsString("Prototype");
1083 std::string Types = R->getValueAsString("Types");
1084 std::string name = LowercaseString(R->getName());
1086 // Functions with 'a' (the splat code) in the type prototype should not get
1087 // their own builtin as they use the non-splat variant.
1088 if (Proto.find('a') != std::string::npos)
1091 // Functions which have a scalar argument cannot be overloaded, no need to
1092 // check them if we are emitting the type checking code.
1093 if (Proto.find('s') != std::string::npos)
1096 SmallVector<StringRef, 16> TypeVec;
1097 ParseTypes(R, Types, TypeVec);
1099 if (R->getSuperClasses().size() < 2)
1100 throw TGError(R->getLoc(), "Builtin has no class kind");
1102 int si = -1, qi = -1;
1103 unsigned mask = 0, qmask = 0;
1104 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1105 // Generate the switch case(s) for this builtin for the type validation.
1106 bool quad = false, poly = false, usgn = false;
1107 (void) ClassifyType(TypeVec[ti], quad, poly, usgn);
1111 qmask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1114 mask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1118 OS << "case ARM::BI__builtin_neon_"
1119 << MangleName(name, TypeVec[si], ClassB)
1120 << ": mask = " << "0x" << utohexstr(mask) << "; break;\n";
1122 OS << "case ARM::BI__builtin_neon_"
1123 << MangleName(name, TypeVec[qi], ClassB)
1124 << ": mask = " << "0x" << utohexstr(qmask) << "; break;\n";
1128 // Generate the intrinsic range checking code for shift/lane immediates.
1129 OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
1130 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1133 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1137 std::string name = LowercaseString(R->getName());
1138 std::string Proto = R->getValueAsString("Prototype");
1139 std::string Types = R->getValueAsString("Types");
1141 // Functions with 'a' (the splat code) in the type prototype should not get
1142 // their own builtin as they use the non-splat variant.
1143 if (Proto.find('a') != std::string::npos)
1146 // Functions which do not have an immediate do not need to have range
1147 // checking code emitted.
1148 if (Proto.find('i') == std::string::npos)
1151 SmallVector<StringRef, 16> TypeVec;
1152 ParseTypes(R, Types, TypeVec);
1154 if (R->getSuperClasses().size() < 2)
1155 throw TGError(R->getLoc(), "Builtin has no class kind");
1157 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1159 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1160 std::string namestr, shiftstr, rangestr;
1162 // Builtins which are overloaded by type will need to have their upper
1163 // bound computed at Sema time based on the type constant.
1164 if (Proto.find('s') == std::string::npos) {
1166 if (R->getValueAsBit("isShift")) {
1167 shiftstr = ", true";
1169 // Right shifts have an 'r' in the name, left shifts do not.
1170 if (name.find('r') != std::string::npos)
1171 rangestr = "l = 1; ";
1173 rangestr += "u = RFT(TV" + shiftstr + ")";
1175 rangestr = "u = " + utostr(RangeFromType(TypeVec[ti]));
1177 // Make sure cases appear only once by uniquing them in a string map.
1178 namestr = MangleName(name, TypeVec[ti], ck);
1179 if (EmittedMap.count(namestr))
1181 EmittedMap[namestr] = OpNone;
1183 // Calculate the index of the immediate that should be range checked.
1184 unsigned immidx = 0;
1186 // Builtins that return a struct of multiple vectors have an extra
1187 // leading arg for the struct return.
1188 if (Proto[0] >= '2' && Proto[0] <= '4')
1191 // Add one to the index for each argument until we reach the immediate
1192 // to be checked. Structs of vectors are passed as multiple arguments.
1193 for (unsigned ii = 1, ie = Proto.size(); ii != ie; ++ii) {
1194 switch (Proto[ii]) {
1195 default: immidx += 1; break;
1196 case '2': immidx += 2; break;
1197 case '3': immidx += 3; break;
1198 case '4': immidx += 4; break;
1199 case 'i': ie = ii + 1; break;
1202 OS << "case ARM::BI__builtin_neon_" << MangleName(name, TypeVec[ti], ck)
1203 << ": i = " << immidx << "; " << rangestr << "; break;\n";