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 widen!";
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) {
191 /// TypeString - for a modifier and type, generate the name of the typedef for
192 /// that type. If generic is true, emit the generic vector type rather than
193 /// the public NEON type. QUc -> uint8x8_t / __neon_uint8x8_t.
194 static std::string TypeString(const char mod, StringRef typestr,
195 bool generic = false) {
208 // base type to get the type string for.
209 char type = ClassifyType(typestr, quad, poly, usgn);
211 // Based on the modifying character, change the type and width if necessary.
212 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
224 s += poly ? "poly8" : "int8";
227 s += quad ? "x16" : "x8";
230 s += poly ? "poly16" : "int16";
233 s += quad ? "x8" : "x4";
239 s += quad ? "x4" : "x2";
245 s += quad ? "x2" : "x1";
251 s += quad ? "x8" : "x4";
257 s += quad ? "x4" : "x2";
260 throw "unhandled type!";
271 // Append _t, finishing the type string typedef type.
283 /// BuiltinTypeString - for a modifier and type, generate the clang
284 /// BuiltinsARM.def prototype code for the function. See the top of clang's
285 /// Builtins.def for a description of the type strings.
286 static std::string BuiltinTypeString(const char mod, StringRef typestr,
287 ClassKind ck, bool ret) {
300 // base type to get the type string for.
301 char type = ClassifyType(typestr, quad, poly, usgn);
303 // Based on the modifying character, change the type and width if necessary.
304 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
315 usgn = usgn | poly | ((ck == ClassI || ck == ClassW) && scal && type != 'f');
335 // Since the return value must be one type, return a vector type of the
336 // appropriate width which we will bitcast. An exception is made for
337 // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
338 // fashion, storing them to a pointer arg.
340 if (mod == '2' || mod == '3' || mod == '4')
342 if (mod == 'f' || (ck != ClassB && type == 'f'))
343 return quad ? "V4f" : "V2f";
344 if (ck != ClassB && type == 's')
345 return quad ? "V8s" : "V4s";
346 if (ck != ClassB && type == 'i')
347 return quad ? "V4i" : "V2i";
348 if (ck != ClassB && type == 'l')
349 return quad ? "V2LLi" : "V1LLi";
351 return quad ? "V16c" : "V8c";
354 // Non-return array types are passed as individual vectors.
356 return quad ? "V16cV16c" : "V8cV8c";
358 return quad ? "V16cV16cV16c" : "V8cV8cV8c";
360 return quad ? "V16cV16cV16cV16c" : "V8cV8cV8cV8c";
362 if (mod == 'f' || (ck != ClassB && type == 'f'))
363 return quad ? "V4f" : "V2f";
364 if (ck != ClassB && type == 's')
365 return quad ? "V8s" : "V4s";
366 if (ck != ClassB && type == 'i')
367 return quad ? "V4i" : "V2i";
368 if (ck != ClassB && type == 'l')
369 return quad ? "V2LLi" : "V1LLi";
371 return quad ? "V16c" : "V8c";
374 /// StructTag - generate the name of the struct tag for a type.
375 /// These names are mandated by ARM's ABI.
376 static std::string StructTag(StringRef typestr) {
381 // base type to get the type string for.
382 char type = ClassifyType(typestr, quad, poly, usgn);
386 s += quad ? "128_" : "64_";
392 s += poly ? "poly8" : "int8";
395 s += poly ? "poly16" : "int16";
410 throw "unhandled type!";
414 // Append _t, finishing the struct tag name.
420 /// MangleName - Append a type or width suffix to a base neon function name,
421 /// and insert a 'q' in the appropriate location if the operation works on
422 /// 128b rather than 64b. E.g. turn "vst2_lane" into "vst2q_lane_f32", etc.
423 static std::string MangleName(const std::string &name, StringRef typestr,
425 if (name == "vcvt_f32_f16")
431 char type = ClassifyType(typestr, quad, poly, usgn);
433 std::string s = name;
438 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
439 case ClassI: s += "_i8"; break;
440 case ClassW: s += "_8"; break;
446 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
447 case ClassI: s += "_i16"; break;
448 case ClassW: s += "_16"; break;
454 case ClassS: s += usgn ? "_u32" : "_s32"; break;
455 case ClassI: s += "_i32"; break;
456 case ClassW: s += "_32"; break;
462 case ClassS: s += usgn ? "_u64" : "_s64"; break;
463 case ClassI: s += "_i64"; break;
464 case ClassW: s += "_64"; break;
471 case ClassI: s += "_f16"; break;
472 case ClassW: s += "_16"; break;
479 case ClassI: s += "_f32"; break;
480 case ClassW: s += "_32"; break;
485 throw "unhandled type!";
491 // Insert a 'q' before the first '_' character so that it ends up before
492 // _lane or _n on vector-scalar operations.
494 size_t pos = s.find('_');
495 s = s.insert(pos, "q");
500 // Generate the string "(argtype a, argtype b, ...)"
501 static std::string GenArgs(const std::string &proto, StringRef typestr) {
502 bool define = proto.find('i') != std::string::npos;
508 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
510 s += TypeString(proto[i], typestr);
522 static std::string Duplicate(unsigned nElts, StringRef typestr,
523 const std::string &a) {
526 s = "(__neon_" + TypeString('d', typestr) + "){ ";
527 for (unsigned i = 0; i != nElts; ++i) {
537 // Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
538 // If structTypes is true, the NEON types are structs of vector types rather
539 // than vector types, and the call becomes "a.val + b.val"
540 static std::string GenOpString(OpKind op, const std::string &proto,
541 StringRef typestr, bool structTypes = true) {
542 bool dummy, quad = false;
543 char type = ClassifyType(typestr, quad, dummy, dummy);
546 case 'c': nElts = 8; break;
547 case 's': nElts = 4; break;
548 case 'i': nElts = 2; break;
549 case 'l': nElts = 1; break;
550 case 'h': nElts = 4; break;
551 case 'f': nElts = 2; break;
554 std::string ts = TypeString(proto[0], typestr);
556 if (op == OpHi || op == OpLo) {
557 s = "union { " + ts + " r; double d; } u; u.d";
567 if (proto.size() > 1)
568 a = (structTypes && proto[1] != 'l' && proto[1] != 's') ? "a.val" : "a";
569 b = structTypes ? "b.val" : "b";
570 c = structTypes ? "c.val" : "c";
580 b = Duplicate(nElts << (int)quad, typestr, "b");
585 c = Duplicate(nElts << (int)quad, typestr, "c");
587 s += a + " + ( " + b + " * " + c + " )";
590 c = Duplicate(nElts << (int)quad, typestr, "c");
592 s += a + " - ( " + b + " * " + c + " )";
595 s += "(__neon_" + ts + ")(" + a + " == " + b + ")";
598 s += "(__neon_" + ts + ")(" + a + " >= " + b + ")";
601 s += "(__neon_" + ts + ")(" + a + " <= " + b + ")";
604 s += "(__neon_" + ts + ")(" + a + " > " + b + ")";
607 s += "(__neon_" + ts + ")(" + a + " < " + b + ")";
631 s += "(__neon_" + ts + ")" + a;
634 s += "__builtin_shufflevector((__neon_int64x1_t)" + a;
635 s += ", (__neon_int64x1_t)" + b + ", 0, 1)";
638 s += "(((__neon_float64x2_t)" + a + ")[1])";
641 s += "(((__neon_float64x2_t)" + a + ")[0])";
644 s += Duplicate(nElts << (int)quad, typestr, a);
647 // ((0 & 1) | (~0 & 2))
648 ts = TypeString(proto[1], typestr);
649 s += "( " + a + " & (__neon_" + ts + ")" + b + ") | ";
650 s += "(~" + a + " & (__neon_" + ts + ")" + c + ")";
653 s += "__builtin_shufflevector(" + a + ", " + a;
654 for (unsigned i = 2; i <= nElts << (int)quad; i += 2)
655 for (unsigned j = 0; j != 2; ++j)
656 s += ", " + utostr(i - j - 1);
661 s += "__builtin_shufflevector(" + a + ", " + a;
662 for (unsigned i = nElts; i <= nElts << (1 + (int)quad); i += nElts)
663 for (unsigned j = 0; j != nElts; ++j)
664 s += ", " + utostr(i - j - 1);
668 s += "__builtin_shufflevector(" + a + ", " + a;
669 for (unsigned i = nElts; i <= nElts << (int)quad; i += nElts)
670 for (unsigned j = 0; j != nElts; ++j)
671 s += ", " + utostr(i - j - 1);
675 throw "unknown OpKind!";
678 if (op == OpHi || op == OpLo)
679 s += "; return u.r;";
685 static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
686 unsigned mod = proto[0];
689 if (mod == 'v' || mod == 'f')
699 // Base type to get the type string for.
700 char type = ClassifyType(typestr, quad, poly, usgn);
702 // Based on the modifying character, change the type and width if necessary.
703 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
707 if (quad && proto[1] != 'g')
730 throw "unhandled type!";
736 // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
737 // If structTypes is true, the NEON types are structs of vector types rather
738 // than vector types, and the call becomes __builtin_neon_cls(a.val)
739 static std::string GenBuiltin(const std::string &name, const std::string &proto,
740 StringRef typestr, ClassKind ck,
741 bool structTypes = true) {
742 bool dummy, quad = false;
743 char type = ClassifyType(typestr, quad, dummy, dummy);
746 case 'c': nElts = 8; break;
747 case 's': nElts = 4; break;
748 case 'i': nElts = 2; break;
749 case 'l': nElts = 1; break;
750 case 'h': nElts = 4; break;
751 case 'f': nElts = 2; break;
753 if (quad) nElts <<= 1;
758 // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
759 // sret-like argument.
760 bool sret = (proto[0] == '2' || proto[0] == '3' || proto[0] == '4');
762 // If this builtin takes an immediate argument, we need to #define it rather
763 // than use a standard declaration, so that SemaChecking can range check
764 // the immediate passed by the user.
765 bool define = proto.find('i') != std::string::npos;
767 // If all types are the same size, bitcasting the args will take care
768 // of arg checking. The actual signedness etc. will be taken care of with
770 if (proto.find('s') == std::string::npos)
773 if (proto[0] != 'v') {
774 std::string ts = TypeString(proto[0], typestr);
778 s += "({ " + ts + " r; ";
779 else if (proto[0] != 's')
780 s += "(" + ts + "){(__neon_" + ts + ")";
785 if (structTypes && proto[0] != 's' && proto[0] != 'i' && proto[0] != 'l')
792 bool splat = proto.find('a') != std::string::npos;
794 s += "__builtin_neon_";
796 std::string vname(name, 0, name.size()-2);
797 s += MangleName(vname, typestr, ck);
799 s += MangleName(name, typestr, ck);
803 // Pass the address of the return variable as the first argument to sret-like
808 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
809 std::string args = std::string(&arg, 1);
811 args = "(" + args + ")";
813 // Handle multiple-vector values specially, emitting each subvector as an
814 // argument to the __builtin.
815 if (structTypes && (proto[i] == '2' || proto[i] == '3' || proto[i] == '4')){
816 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
817 s += args + ".val[" + utostr(vi) + "].val";
827 if (splat && (i + 1) == e)
828 s += Duplicate(nElts, typestr, args);
832 if (structTypes && proto[i] != 's' && proto[i] != 'i' && proto[i] != 'l' &&
833 proto[i] != 'p' && proto[i] != 'c' && proto[i] != 'a') {
840 // Extra constant integer to hold type class enum for this function, e.g. s8
842 s += ", " + utostr(GetNeonEnum(proto, typestr));
849 if (proto[0] != 'v') {
853 else if (proto[0] != 's')
862 static std::string GenBuiltinDef(const std::string &name,
863 const std::string &proto,
864 StringRef typestr, ClassKind ck) {
865 std::string s("BUILTIN(__builtin_neon_");
867 // If all types are the same size, bitcasting the args will take care
868 // of arg checking. The actual signedness etc. will be taken care of with
870 if (proto.find('s') == std::string::npos)
873 s += MangleName(name, typestr, ck);
876 for (unsigned i = 0, e = proto.size(); i != e; ++i)
877 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
879 // Extra constant integer to hold type class enum for this function, e.g. s8
887 /// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h
888 /// is comprised of type definitions and function declarations.
889 void NeonEmitter::run(raw_ostream &OS) {
890 EmitSourceFileHeader("ARM NEON Header", OS);
892 // FIXME: emit license into file?
894 OS << "#ifndef __ARM_NEON_H\n";
895 OS << "#define __ARM_NEON_H\n\n";
897 OS << "#ifndef __ARM_NEON__\n";
898 OS << "#error \"NEON support not enabled\"\n";
901 OS << "#include <stdint.h>\n\n";
903 // Emit NEON-specific scalar typedefs.
904 OS << "typedef float float32_t;\n";
905 OS << "typedef uint8_t poly8_t;\n";
906 OS << "typedef uint16_t poly16_t;\n";
907 OS << "typedef uint16_t float16_t;\n";
909 // Emit Neon vector typedefs.
910 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
911 SmallVector<StringRef, 24> TDTypeVec;
912 ParseTypes(0, TypedefTypes, TDTypeVec);
914 // Emit vector typedefs.
915 for (unsigned v = 1; v != 5; ++v) {
916 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
917 bool dummy, quad = false;
918 (void) ClassifyType(TDTypeVec[i], quad, dummy, dummy);
919 OS << "typedef __attribute__(( __vector_size__(";
921 OS << utostr(8*v*(quad ? 2 : 1)) << ") )) ";
925 OS << TypeString('s', TDTypeVec[i]);
928 char t = (v == 1) ? 'd' : '0' + v;
929 OS << TypeString(t, TDTypeVec[i]) << ";\n";
933 OS << "typedef __attribute__(( __vector_size__(8) )) "
934 "double __neon_float64x1_t;\n";
935 OS << "typedef __attribute__(( __vector_size__(16) )) "
936 "double __neon_float64x2_t;\n";
939 // Emit struct typedefs.
940 for (unsigned vi = 1; vi != 5; ++vi) {
941 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
942 std::string ts = TypeString('d', TDTypeVec[i], vi == 1);
943 std::string vs = TypeString((vi > 1) ? '0' + vi : 'd', TDTypeVec[i]);
944 std::string tag = (vi > 1) ? vs : StructTag(TDTypeVec[i]);
945 OS << "typedef struct " << tag << " {\n";
946 OS << " " << ts << " val";
948 OS << "[" << utostr(vi) << "]";
949 OS << ";\n} " << vs << ";\n\n";
953 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
955 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
957 // Unique the return+pattern types, and assign them.
958 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
960 std::string name = LowercaseString(R->getName());
961 std::string Proto = R->getValueAsString("Prototype");
962 std::string Types = R->getValueAsString("Types");
964 SmallVector<StringRef, 16> TypeVec;
965 ParseTypes(R, Types, TypeVec);
967 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
969 bool define = Proto.find('i') != std::string::npos;
971 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
972 assert(!Proto.empty() && "");
974 // static always inline + return type
978 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
980 // Function name with type suffix
981 OS << " " << MangleName(name, TypeVec[ti], ClassS);
983 // Function arguments
984 OS << GenArgs(Proto, TypeVec[ti]);
993 OS << GenOpString(k, Proto, TypeVec[ti]);
995 if (R->getSuperClasses().size() < 2)
996 throw TGError(R->getLoc(), "Builtin has no class kind");
998 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1000 if (ck == ClassNone)
1001 throw TGError(R->getLoc(), "Builtin has no class kind");
1002 OS << GenBuiltin(name, Proto, TypeVec[ti], ck);
1010 OS << "#undef __ai\n\n";
1011 OS << "#endif /* __ARM_NEON_H */\n";
1014 static unsigned RangeFromType(StringRef typestr) {
1015 // base type to get the type string for.
1016 bool quad = false, dummy = false;
1017 char type = ClassifyType(typestr, quad, dummy, dummy);
1021 return (8 << (int)quad) - 1;
1024 return (4 << (int)quad) - 1;
1027 return (2 << (int)quad) - 1;
1029 return (1 << (int)quad) - 1;
1031 throw "unhandled type!";
1034 assert(0 && "unreachable");
1038 /// runHeader - Emit a file with sections defining:
1039 /// 1. the NEON section of BuiltinsARM.def.
1040 /// 2. the SemaChecking code for the type overload checking.
1041 /// 3. the SemaChecking code for validation of intrinsic immedate arguments.
1042 void NeonEmitter::runHeader(raw_ostream &OS) {
1043 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1045 StringMap<OpKind> EmittedMap;
1047 // Generate BuiltinsARM.def for NEON
1048 OS << "#ifdef GET_NEON_BUILTINS\n";
1049 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1051 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1055 std::string Proto = R->getValueAsString("Prototype");
1057 // Functions with 'a' (the splat code) in the type prototype should not get
1058 // their own builtin as they use the non-splat variant.
1059 if (Proto.find('a') != std::string::npos)
1062 std::string Types = R->getValueAsString("Types");
1063 SmallVector<StringRef, 16> TypeVec;
1064 ParseTypes(R, Types, TypeVec);
1066 if (R->getSuperClasses().size() < 2)
1067 throw TGError(R->getLoc(), "Builtin has no class kind");
1069 std::string name = LowercaseString(R->getName());
1070 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1072 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1073 // Generate the BuiltinsARM.def declaration for this builtin, ensuring
1074 // that each unique BUILTIN() macro appears only once in the output
1076 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
1077 if (EmittedMap.count(bd))
1080 EmittedMap[bd] = OpNone;
1086 // Generate the overloaded type checking code for SemaChecking.cpp
1087 OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
1088 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1090 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1094 std::string Proto = R->getValueAsString("Prototype");
1095 std::string Types = R->getValueAsString("Types");
1096 std::string name = LowercaseString(R->getName());
1098 // Functions with 'a' (the splat code) in the type prototype should not get
1099 // their own builtin as they use the non-splat variant.
1100 if (Proto.find('a') != std::string::npos)
1103 // Functions which have a scalar argument cannot be overloaded, no need to
1104 // check them if we are emitting the type checking code.
1105 if (Proto.find('s') != std::string::npos)
1108 SmallVector<StringRef, 16> TypeVec;
1109 ParseTypes(R, Types, TypeVec);
1111 if (R->getSuperClasses().size() < 2)
1112 throw TGError(R->getLoc(), "Builtin has no class kind");
1114 int si = -1, qi = -1;
1115 unsigned mask = 0, qmask = 0;
1116 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1117 // Generate the switch case(s) for this builtin for the type validation.
1118 bool quad = false, poly = false, usgn = false;
1119 (void) ClassifyType(TypeVec[ti], quad, poly, usgn);
1123 qmask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1126 mask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1130 OS << "case ARM::BI__builtin_neon_"
1131 << MangleName(name, TypeVec[si], ClassB)
1132 << ": mask = " << "0x" << utohexstr(mask) << "; break;\n";
1134 OS << "case ARM::BI__builtin_neon_"
1135 << MangleName(name, TypeVec[qi], ClassB)
1136 << ": mask = " << "0x" << utohexstr(qmask) << "; break;\n";
1140 // Generate the intrinsic range checking code for shift/lane immediates.
1141 OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
1142 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1145 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1149 std::string name = LowercaseString(R->getName());
1150 std::string Proto = R->getValueAsString("Prototype");
1151 std::string Types = R->getValueAsString("Types");
1153 // Functions with 'a' (the splat code) in the type prototype should not get
1154 // their own builtin as they use the non-splat variant.
1155 if (Proto.find('a') != std::string::npos)
1158 // Functions which do not have an immediate do not need to have range
1159 // checking code emitted.
1160 if (Proto.find('i') == std::string::npos)
1163 SmallVector<StringRef, 16> TypeVec;
1164 ParseTypes(R, Types, TypeVec);
1166 if (R->getSuperClasses().size() < 2)
1167 throw TGError(R->getLoc(), "Builtin has no class kind");
1169 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1171 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1172 std::string namestr, shiftstr, rangestr;
1174 // Builtins which are overloaded by type will need to have their upper
1175 // bound computed at Sema time based on the type constant.
1176 if (Proto.find('s') == std::string::npos) {
1178 if (R->getValueAsBit("isShift")) {
1179 shiftstr = ", true";
1181 // Right shifts have an 'r' in the name, left shifts do not.
1182 if (name.find('r') != std::string::npos)
1183 rangestr = "l = 1; ";
1185 rangestr += "u = RFT(TV" + shiftstr + ")";
1187 rangestr = "u = " + utostr(RangeFromType(TypeVec[ti]));
1189 // Make sure cases appear only once by uniquing them in a string map.
1190 namestr = MangleName(name, TypeVec[ti], ck);
1191 if (EmittedMap.count(namestr))
1193 EmittedMap[namestr] = OpNone;
1195 // Calculate the index of the immediate that should be range checked.
1196 unsigned immidx = 0;
1198 // Builtins that return a struct of multiple vectors have an extra
1199 // leading arg for the struct return.
1200 if (Proto[0] == '2' || Proto[0] == '3' || Proto[0] == '4')
1203 // Add one to the index for each argument until we reach the immediate
1204 // to be checked. Structs of vectors are passed as multiple arguments.
1205 for (unsigned ii = 1, ie = Proto.size(); ii != ie; ++ii) {
1206 switch (Proto[ii]) {
1207 default: immidx += 1; break;
1208 case '2': immidx += 2; break;
1209 case '3': immidx += 3; break;
1210 case '4': immidx += 4; break;
1211 case 'i': ie = ii + 1; break;
1214 OS << "case ARM::BI__builtin_neon_" << MangleName(name, TypeVec[ti], ck)
1215 << ": i = " << immidx << "; " << rangestr << "; break;\n";