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 //===----------------------------------------------------------------------===//
16 #include "NeonEmitter.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringExtras.h"
24 static void ParseTypes(Record *r, std::string &s,
25 SmallVectorImpl<StringRef> &TV) {
26 const char *data = s.data();
29 for (unsigned i = 0, e = s.size(); i != e; ++i, ++len) {
30 if (data[len] == 'P' || data[len] == 'Q' || data[len] == 'U')
42 throw TGError(r->getLoc(),
43 "Unexpected letter: " + std::string(data + len, 1));
46 TV.push_back(StringRef(data, len + 1));
52 static char Widen(const char t) {
60 default: throw "unhandled type in widen!";
65 static char Narrow(const char t) {
75 default: throw "unhandled type in widen!";
80 static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) {
101 // base type to get the type string for.
105 static char ModType(const char mod, char type, bool &quad, bool &poly,
106 bool &usgn, bool &scal, bool &cnst, bool &pntr) {
169 static std::string TypeString(const char mod, StringRef typestr,
183 // base type to get the type string for.
184 char type = ClassifyType(typestr, quad, poly, usgn);
186 // Based on the modifying character, change the type and width if necessary.
187 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
199 s += poly ? "poly8" : "int8";
202 s += quad ? "x16" : "x8";
205 s += poly ? "poly16" : "int16";
208 s += quad ? "x8" : "x4";
214 s += quad ? "x4" : "x2";
220 s += quad ? "x2" : "x1";
226 s += quad ? "x8" : "x4";
232 s += quad ? "x4" : "x2";
235 throw "unhandled type!";
246 // Append _t, finishing the type string typedef type.
258 static std::string BuiltinTypeString(const char mod, StringRef typestr,
259 ClassKind ck, bool ret) {
272 // base type to get the type string for.
273 char type = ClassifyType(typestr, quad, poly, usgn);
275 // Based on the modifying character, change the type and width if necessary.
276 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
285 usgn = usgn | poly | ((ck == ClassI || ck == ClassW) && scal && type != 'f');
305 // Since the return value must be one type, return a vector type of the
306 // appropriate width which we will bitcast.
309 return quad ? "V32c" : "V16c";
311 return quad ? "V48c" : "V24c";
313 return quad ? "V64c" : "V32c";
314 if (mod == 'f' || (ck != ClassB && type == 'f'))
315 return quad ? "V4f" : "V2f";
316 if (ck != ClassB && type == 's')
317 return quad ? "V8s" : "V4s";
318 if (ck != ClassB && type == 'i')
319 return quad ? "V4i" : "V2i";
320 if (ck != ClassB && type == 'l')
321 return quad ? "V2LLi" : "V1LLi";
323 return quad ? "V16c" : "V8c";
326 // Non-return array types are passed as individual vectors.
328 return quad ? "V16cV16c" : "V8cV8c";
330 return quad ? "V16cV16cV16c" : "V8cV8cV8c";
332 return quad ? "V16cV16cV16cV16c" : "V8cV8cV8cV8c";
334 if (mod == 'f' || (ck != ClassB && type == 'f'))
335 return quad ? "V4f" : "V2f";
336 if (ck != ClassB && type == 's')
337 return quad ? "V8s" : "V4s";
338 if (ck != ClassB && type == 'i')
339 return quad ? "V4i" : "V2i";
340 if (ck != ClassB && type == 'l')
341 return quad ? "V2LLi" : "V1LLi";
343 return quad ? "V16c" : "V8c";
346 // Turn "vst2_lane" into "vst2q_lane_f32", etc.
347 static std::string MangleName(const std::string &name, StringRef typestr,
349 if (name == "vcvt_f32_f16")
355 char type = ClassifyType(typestr, quad, poly, usgn);
357 std::string s = name;
362 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
363 case ClassI: s += "_i8"; break;
364 case ClassW: s += "_8"; break;
370 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
371 case ClassI: s += "_i16"; break;
372 case ClassW: s += "_16"; break;
378 case ClassS: s += usgn ? "_u32" : "_s32"; break;
379 case ClassI: s += "_i32"; break;
380 case ClassW: s += "_32"; break;
386 case ClassS: s += usgn ? "_u64" : "_s64"; break;
387 case ClassI: s += "_i64"; break;
388 case ClassW: s += "_64"; break;
395 case ClassI: s += "_f16"; break;
396 case ClassW: s += "_16"; break;
403 case ClassI: s += "_f32"; break;
404 case ClassW: s += "_32"; break;
409 throw "unhandled type!";
415 // Insert a 'q' before the first '_' character so that it ends up before
416 // _lane or _n on vector-scalar operations.
418 size_t pos = s.find('_');
419 s = s.insert(pos, "q");
424 // Generate the string "(argtype a, argtype b, ...)"
425 static std::string GenArgs(const std::string &proto, StringRef typestr) {
426 bool define = proto.find('i') != std::string::npos;
432 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
434 s += TypeString(proto[i], typestr);
446 static std::string Duplicate(unsigned nElts, StringRef typestr,
447 const std::string &a) {
450 s = "(__neon_" + TypeString('d', typestr) + "){ ";
451 for (unsigned i = 0; i != nElts; ++i) {
461 // Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
462 // If structTypes is true, the NEON types are structs of vector types rather
463 // than vector types, and the call becomes "a.val + b.val"
464 static std::string GenOpString(OpKind op, const std::string &proto,
465 StringRef typestr, bool structTypes = true) {
466 bool dummy, quad = false;
467 char type = ClassifyType(typestr, quad, dummy, dummy);
470 case 'c': nElts = 8; break;
471 case 's': nElts = 4; break;
472 case 'i': nElts = 2; break;
473 case 'l': nElts = 1; break;
474 case 'h': nElts = 4; break;
475 case 'f': nElts = 2; break;
478 std::string ts = TypeString(proto[0], typestr);
479 std::string s = ts + " r; r";
487 if (proto.size() > 1)
488 a = (structTypes && proto[1] != 'l' && proto[1] != 's') ? "a.val" : "a";
489 b = structTypes ? "b.val" : "b";
490 c = structTypes ? "c.val" : "c";
500 b = Duplicate(nElts << quad, typestr, "b");
505 c = Duplicate(nElts << quad, typestr, "c");
507 s += a + " + ( " + b + " * " + c + " )";
510 c = Duplicate(nElts << quad, typestr, "c");
512 s += a + " - ( " + b + " * " + c + " )";
515 s += "(__neon_" + ts + ")(" + a + " == " + b + ")";
518 s += "(__neon_" + ts + ")(" + a + " >= " + b + ")";
521 s += "(__neon_" + ts + ")(" + a + " <= " + b + ")";
524 s += "(__neon_" + ts + ")(" + a + " > " + b + ")";
527 s += "(__neon_" + ts + ")(" + a + " < " + b + ")";
551 s += "(__neon_" + ts + ")" + a;
554 s += "__builtin_shufflevector((__neon_int64x1_t)" + a;
555 s += ", (__neon_int64x1_t)" + b + ", 0, 1)";
558 s += "(__neon_int64x1_t)(((__neon_int64x2_t)" + a + ")[1])";
561 s += "(__neon_int64x1_t)(((__neon_int64x2_t)" + a + ")[0])";
564 s += Duplicate(nElts << quad, typestr, a);
567 // ((0 & 1) | (~0 & 2))
568 ts = TypeString(proto[1], typestr);
569 s += "( " + a + " & (__neon_" + ts + ")" + b + ") | ";
570 s += "(~" + a + " & (__neon_" + ts + ")" + c + ")";
573 s += "__builtin_shufflevector(" + a + ", " + a;
574 for (unsigned i = 2; i <= nElts << quad; i += 2)
575 for (unsigned j = 0; j != 2; ++j)
576 s += ", " + utostr(i - j - 1);
581 s += "__builtin_shufflevector(" + a + ", " + a;
582 for (unsigned i = nElts; i <= nElts << (1 + quad); i += nElts)
583 for (unsigned j = 0; j != nElts; ++j)
584 s += ", " + utostr(i - j - 1);
588 s += "__builtin_shufflevector(" + a + ", " + a;
589 for (unsigned i = nElts; i <= nElts << quad; i += nElts)
590 for (unsigned j = 0; j != nElts; ++j)
591 s += ", " + utostr(i - j - 1);
595 throw "unknown OpKind!";
602 static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
603 unsigned mod = proto[0];
606 if (mod == 'v' || mod == 'f')
616 // base type to get the type string for.
617 char type = ClassifyType(typestr, quad, poly, usgn);
619 // Based on the modifying character, change the type and width if necessary.
620 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
649 throw "unhandled type!";
655 // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
656 // If structTypes is true, the NEON types are structs of vector types rather
657 // than vector types, and the call becomes __builtin_neon_cls(a.val)
658 static std::string GenBuiltin(const std::string &name, const std::string &proto,
659 StringRef typestr, ClassKind ck,
660 bool structTypes = true) {
661 bool dummy, quad = false;
662 char type = ClassifyType(typestr, quad, dummy, dummy);
665 case 'c': nElts = 8; break;
666 case 's': nElts = 4; break;
667 case 'i': nElts = 2; break;
668 case 'l': nElts = 1; break;
669 case 'h': nElts = 4; break;
670 case 'f': nElts = 2; break;
672 if (quad) nElts <<= 1;
677 bool unioning = (proto[0] == '2' || proto[0] == '3' || proto[0] == '4');
678 bool define = proto.find('i') != std::string::npos;
680 // If all types are the same size, bitcasting the args will take care
681 // of arg checking. The actual signedness etc. will be taken care of with
683 if (proto.find('s') == std::string::npos)
686 if (proto[0] != 'v') {
687 std::string ts = TypeString(proto[0], typestr);
691 s += "(" + ts + "){(__neon_" + ts + ")";
695 s += TypeString(proto[0], typestr, true) + " val; ";
696 s += TypeString(proto[0], typestr, false) + " s; ";
703 if (structTypes && proto[0] != 's' && proto[0] != 'i' && proto[0] != 'l')
710 bool splat = proto.find('a') != std::string::npos;
712 s += "__builtin_neon_";
714 std::string vname(name, 0, name.size()-2);
715 s += MangleName(vname, typestr, ck);
717 s += MangleName(name, typestr, ck);
721 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
722 std::string args = std::string(&arg, 1);
724 args = "(" + args + ")";
726 // Handle multiple-vector values specially, emitting each subvector as an
727 // argument to the __builtin.
728 if (structTypes && (proto[i] == '2' || proto[i] == '3' || proto[i] == '4')){
729 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
730 s += args + ".val[" + utostr(vi) + "]";
740 if (splat && (i + 1) == e)
741 s += Duplicate(nElts, typestr, args);
745 if (structTypes && proto[i] != 's' && proto[i] != 'i' && proto[i] != 'l' &&
746 proto[i] != 'p' && proto[i] != 'c' && proto[i] != 'a') {
753 // Extra constant integer to hold type class enum for this function, e.g. s8
755 s += ", " + utostr(GetNeonEnum(proto, typestr));
762 if (proto[0] != 'v') {
776 static std::string GenBuiltinDef(const std::string &name,
777 const std::string &proto,
778 StringRef typestr, ClassKind ck) {
779 std::string s("BUILTIN(__builtin_neon_");
781 // If all types are the same size, bitcasting the args will take care
782 // of arg checking. The actual signedness etc. will be taken care of with
784 if (proto.find('s') == std::string::npos)
787 s += MangleName(name, typestr, ck);
790 for (unsigned i = 0, e = proto.size(); i != e; ++i)
791 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
793 // Extra constant integer to hold type class enum for this function, e.g. s8
801 void NeonEmitter::run(raw_ostream &OS) {
802 EmitSourceFileHeader("ARM NEON Header", OS);
804 // FIXME: emit license into file?
806 OS << "#ifndef __ARM_NEON_H\n";
807 OS << "#define __ARM_NEON_H\n\n";
809 OS << "#ifndef __ARM_NEON__\n";
810 OS << "#error \"NEON support not enabled\"\n";
813 OS << "#include <stdint.h>\n\n";
815 // Emit NEON-specific scalar typedefs.
816 // FIXME: probably need to do something better for polynomial types.
817 OS << "typedef float float32_t;\n";
818 OS << "typedef uint8_t poly8_t;\n";
819 OS << "typedef uint16_t poly16_t;\n";
820 OS << "typedef uint16_t float16_t;\n";
822 // Emit Neon vector typedefs.
823 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
824 SmallVector<StringRef, 24> TDTypeVec;
825 ParseTypes(0, TypedefTypes, TDTypeVec);
827 // Emit vector typedefs.
828 for (unsigned v = 1; v != 5; ++v) {
829 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
830 bool dummy, quad = false;
831 (void) ClassifyType(TDTypeVec[i], quad, dummy, dummy);
832 OS << "typedef __attribute__(( __vector_size__(";
834 OS << utostr(8*v*(quad ? 2 : 1)) << ") )) ";
838 OS << TypeString('s', TDTypeVec[i]);
841 char t = (v == 1) ? 'd' : '0' + v;
842 OS << TypeString(t, TDTypeVec[i]) << ";\n";
847 // Emit struct typedefs.
848 for (unsigned vi = 1; vi != 5; ++vi) {
849 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
850 std::string ts = TypeString('d', TDTypeVec[i]);
851 std::string vs = (vi > 1) ? TypeString('0' + vi, TDTypeVec[i]) : ts;
852 OS << "typedef struct __" << vs << " {\n";
853 OS << " __neon_" << ts << " val";
855 OS << "[" << utostr(vi) << "]";
856 OS << ";\n} " << vs << ";\n\n";
860 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
862 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
864 // Unique the return+pattern types, and assign them.
865 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
867 std::string name = LowercaseString(R->getName());
868 std::string Proto = R->getValueAsString("Prototype");
869 std::string Types = R->getValueAsString("Types");
871 SmallVector<StringRef, 16> TypeVec;
872 ParseTypes(R, Types, TypeVec);
874 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
876 bool define = Proto.find('i') != std::string::npos;
878 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
879 assert(!Proto.empty() && "");
881 // static always inline + return type
885 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
887 // Function name with type suffix
888 OS << " " << MangleName(name, TypeVec[ti], ClassS);
890 // Function arguments
891 OS << GenArgs(Proto, TypeVec[ti]);
900 OS << GenOpString(k, Proto, TypeVec[ti]);
902 if (R->getSuperClasses().size() < 2)
903 throw TGError(R->getLoc(), "Builtin has no class kind");
905 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
908 throw TGError(R->getLoc(), "Builtin has no class kind");
909 OS << GenBuiltin(name, Proto, TypeVec[ti], ck);
917 OS << "#undef __ai\n\n";
918 OS << "#endif /* __ARM_NEON_H */\n";
921 void NeonEmitter::runHeader(raw_ostream &OS) {
922 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
924 StringMap<OpKind> EmittedMap;
926 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
929 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
933 std::string name = LowercaseString(R->getName());
934 std::string Proto = R->getValueAsString("Prototype");
935 std::string Types = R->getValueAsString("Types");
937 if (Proto.find('a') != std::string::npos)
940 SmallVector<StringRef, 16> TypeVec;
941 ParseTypes(R, Types, TypeVec);
943 if (R->getSuperClasses().size() < 2)
944 throw TGError(R->getLoc(), "Builtin has no class kind");
946 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
948 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
949 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
950 if (EmittedMap.count(bd))
953 EmittedMap[bd] = OpNone;