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) {
168 static std::string TypeString(const char mod, StringRef typestr,
182 // base type to get the type string for.
183 char type = ClassifyType(typestr, quad, poly, usgn);
185 // Based on the modifying character, change the type and width if necessary.
186 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
198 s += poly ? "poly8" : "int8";
201 s += quad ? "x16" : "x8";
204 s += poly ? "poly16" : "int16";
207 s += quad ? "x8" : "x4";
213 s += quad ? "x4" : "x2";
219 s += quad ? "x2" : "x1";
225 s += quad ? "x8" : "x4";
231 s += quad ? "x4" : "x2";
234 throw "unhandled type!";
245 // Append _t, finishing the type string typedef type.
257 static std::string BuiltinTypeString(const char mod, StringRef typestr,
258 ClassKind ck, bool ret) {
271 // base type to get the type string for.
272 char type = ClassifyType(typestr, quad, poly, usgn);
274 // Based on the modifying character, change the type and width if necessary.
275 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
284 usgn = usgn | poly | ((ck == ClassI || ck == ClassW) && scal && type != 'f');
304 // Since the return value must be one type, return a vector type of the
305 // appropriate width which we will bitcast.
308 return quad ? "V32c" : "V16c";
310 return quad ? "V48c" : "V24c";
312 return quad ? "V64c" : "V32c";
313 if (mod == 'f' || (ck != ClassB && type == 'f'))
314 return quad ? "V4f" : "V2f";
315 if (ck != ClassB && type == 's')
316 return quad ? "V8s" : "V4s";
317 if (ck != ClassB && type == 'i')
318 return quad ? "V4i" : "V2i";
319 if (ck != ClassB && type == 'l')
320 return quad ? "V2LLi" : "V1LLi";
322 return quad ? "V16c" : "V8c";
325 // Non-return array types are passed as individual vectors.
327 return quad ? "V16cV16c" : "V8cV8c";
329 return quad ? "V16cV16cV16c" : "V8cV8cV8c";
331 return quad ? "V16cV16cV16cV16c" : "V8cV8cV8cV8c";
333 if (mod == 'f' || (ck != ClassB && type == 'f'))
334 return quad ? "V4f" : "V2f";
335 if (ck != ClassB && type == 's')
336 return quad ? "V8s" : "V4s";
337 if (ck != ClassB && type == 'i')
338 return quad ? "V4i" : "V2i";
339 if (ck != ClassB && type == 'l')
340 return quad ? "V2LLi" : "V1LLi";
342 return quad ? "V16c" : "V8c";
345 // Turn "vst2_lane" into "vst2q_lane_f32", etc.
346 static std::string MangleName(const std::string &name, StringRef typestr,
348 if (name == "vcvt_f32_f16")
354 char type = ClassifyType(typestr, quad, poly, usgn);
356 std::string s = name;
361 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
362 case ClassI: s += "_i8"; break;
363 case ClassW: s += "_8"; break;
369 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
370 case ClassI: s += "_i16"; break;
371 case ClassW: s += "_16"; break;
377 case ClassS: s += usgn ? "_u32" : "_s32"; break;
378 case ClassI: s += "_i32"; break;
379 case ClassW: s += "_32"; break;
385 case ClassS: s += usgn ? "_u64" : "_s64"; break;
386 case ClassI: s += "_i64"; break;
387 case ClassW: s += "_64"; break;
394 case ClassI: s += "_f16"; break;
395 case ClassW: s += "_16"; break;
402 case ClassI: s += "_f32"; break;
403 case ClassW: s += "_32"; break;
408 throw "unhandled type!";
414 // Insert a 'q' before the first '_' character so that it ends up before
415 // _lane or _n on vector-scalar operations.
417 size_t pos = s.find('_');
418 s = s.insert(pos, "q");
423 // Generate the string "(argtype a, argtype b, ...)"
424 static std::string GenArgs(const std::string &proto, StringRef typestr) {
425 bool define = proto.find('i') != std::string::npos;
431 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
433 s += TypeString(proto[i], typestr);
445 // Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
446 // If structTypes is true, the NEON types are structs of vector types rather
447 // than vector types, and the call becomes "a.val + b.val"
448 static std::string GenOpString(OpKind op, const std::string &proto,
449 StringRef typestr, bool structTypes = true) {
450 std::string ts = TypeString(proto[0], typestr);
451 std::string s = ts + " r; r";
453 bool dummy, quad = false;
454 char type = ClassifyType(typestr, quad, dummy, dummy);
457 case 'c': nElts = 8; break;
458 case 's': nElts = 4; break;
459 case 'i': nElts = 2; break;
460 case 'l': nElts = 1; break;
461 case 'h': nElts = 4; break;
462 case 'f': nElts = 2; break;
472 if (proto.size() > 1)
473 a = (structTypes && proto[1] != 'l' && proto[1] != 's') ? "a.val" : "a";
474 b = structTypes ? "b.val" : "b";
475 c = structTypes ? "c.val" : "c";
488 s += a + " + ( " + b + " * " + c + " )";
491 s += a + " - ( " + b + " * " + c + " )";
494 s += "(__neon_" + ts + ")(" + a + " == " + b + ")";
497 s += "(__neon_" + ts + ")(" + a + " >= " + b + ")";
500 s += "(__neon_" + ts + ")(" + a + " <= " + b + ")";
503 s += "(__neon_" + ts + ")(" + a + " > " + b + ")";
506 s += "(__neon_" + ts + ")(" + a + " < " + b + ")";
530 s += "(__neon_" + ts + ")" + a;
533 s += "__builtin_shufflevector((__neon_int64x1_t)" + a;
534 s += ", (__neon_int64x1_t)" + b + ", 0, 1)";
537 s += "(__neon_int64x1_t)(((__neon_int64x2_t)" + a + ")[1])";
540 s += "(__neon_int64x1_t)(((__neon_int64x2_t)" + a + ")[0])";
543 s += "(__neon_" + ts + "){ ";
544 for (unsigned i = 0; i != nElts; ++i) {
552 throw "unknown OpKind!";
559 static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
560 unsigned mod = proto[0];
563 if (mod == 'v' || mod == 'f')
573 // base type to get the type string for.
574 char type = ClassifyType(typestr, quad, poly, usgn);
576 // Based on the modifying character, change the type and width if necessary.
577 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
604 throw "unhandled type!";
610 // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
611 // If structTypes is true, the NEON types are structs of vector types rather
612 // than vector types, and the call becomes __builtin_neon_cls(a.val)
613 static std::string GenBuiltin(const std::string &name, const std::string &proto,
614 StringRef typestr, ClassKind ck,
615 bool structTypes = true) {
619 bool unioning = (proto[0] == '2' || proto[0] == '3' || proto[0] == '4');
620 bool define = proto.find('i') != std::string::npos;
622 // If all types are the same size, bitcasting the args will take care
623 // of arg checking. The actual signedness etc. will be taken care of with
625 if (proto.find('s') == std::string::npos)
628 if (proto[0] != 'v') {
629 std::string ts = TypeString(proto[0], typestr);
633 s += "(" + ts + "){(__neon_" + ts + ")";
637 s += TypeString(proto[0], typestr, true) + " val; ";
638 s += TypeString(proto[0], typestr, false) + " s; ";
645 if (structTypes && proto[0] != 's' && proto[0] != 'i' && proto[0] != 'l')
652 s += "__builtin_neon_";
653 s += MangleName(name, typestr, ck);
656 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
657 // Handle multiple-vector values specially, emitting each subvector as an
658 // argument to the __builtin.
659 if (structTypes && (proto[i] == '2' || proto[i] == '3' || proto[i] == '4')){
660 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
662 s += ".val[" + utostr(vi) + "]";
672 // Parenthesize the args from the macro.
679 if (structTypes && proto[i] != 's' && proto[i] != 'i' && proto[i] != 'l' &&
680 proto[i] != 'p' && proto[i] != 'c') {
687 // Extra constant integer to hold type class enum for this function, e.g. s8
689 s += ", " + utostr(GetNeonEnum(proto, typestr));
696 if (proto[0] != 'v') {
710 static std::string GenBuiltinDef(const std::string &name,
711 const std::string &proto,
712 StringRef typestr, ClassKind ck) {
713 std::string s("BUILTIN(__builtin_neon_");
715 // If all types are the same size, bitcasting the args will take care
716 // of arg checking. The actual signedness etc. will be taken care of with
718 if (proto.find('s') == std::string::npos)
721 s += MangleName(name, typestr, ck);
724 for (unsigned i = 0, e = proto.size(); i != e; ++i)
725 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
727 // Extra constant integer to hold type class enum for this function, e.g. s8
735 void NeonEmitter::run(raw_ostream &OS) {
736 EmitSourceFileHeader("ARM NEON Header", OS);
738 // FIXME: emit license into file?
740 OS << "#ifndef __ARM_NEON_H\n";
741 OS << "#define __ARM_NEON_H\n\n";
743 OS << "#ifndef __ARM_NEON__\n";
744 OS << "#error \"NEON support not enabled\"\n";
747 OS << "#include <stdint.h>\n\n";
749 // Emit NEON-specific scalar typedefs.
750 // FIXME: probably need to do something better for polynomial types.
751 // FIXME: is this the correct thing to do for float16?
752 OS << "typedef float float32_t;\n";
753 OS << "typedef uint8_t poly8_t;\n";
754 OS << "typedef uint16_t poly16_t;\n";
755 OS << "typedef uint16_t float16_t;\n";
757 // Emit Neon vector typedefs.
758 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
759 SmallVector<StringRef, 24> TDTypeVec;
760 ParseTypes(0, TypedefTypes, TDTypeVec);
762 // Emit vector typedefs.
763 for (unsigned v = 1; v != 5; ++v) {
764 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
765 bool dummy, quad = false;
766 (void) ClassifyType(TDTypeVec[i], quad, dummy, dummy);
767 OS << "typedef __attribute__(( __vector_size__(";
769 OS << utostr(8*v*(quad ? 2 : 1)) << ") )) ";
773 OS << TypeString('s', TDTypeVec[i]);
776 char t = (v == 1) ? 'd' : '0' + v;
777 OS << TypeString(t, TDTypeVec[i]) << ";\n";
782 // Emit struct typedefs.
783 for (unsigned vi = 1; vi != 5; ++vi) {
784 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
785 std::string ts = TypeString('d', TDTypeVec[i]);
786 std::string vs = (vi > 1) ? TypeString('0' + vi, TDTypeVec[i]) : ts;
787 OS << "typedef struct __" << vs << " {\n";
788 OS << " __neon_" << ts << " val";
790 OS << "[" << utostr(vi) << "]";
791 OS << ";\n} " << vs << ";\n\n";
795 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
797 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
799 // Unique the return+pattern types, and assign them.
800 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
802 std::string name = LowercaseString(R->getName());
803 std::string Proto = R->getValueAsString("Prototype");
804 std::string Types = R->getValueAsString("Types");
806 SmallVector<StringRef, 16> TypeVec;
807 ParseTypes(R, Types, TypeVec);
809 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
811 bool define = Proto.find('i') != std::string::npos;
813 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
814 assert(!Proto.empty() && "");
816 // static always inline + return type
820 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
822 // Function name with type suffix
823 OS << " " << MangleName(name, TypeVec[ti], ClassS);
825 // Function arguments
826 OS << GenArgs(Proto, TypeVec[ti]);
835 OS << GenOpString(k, Proto, TypeVec[ti]);
837 if (R->getSuperClasses().size() < 2)
838 throw TGError(R->getLoc(), "Builtin has no class kind");
840 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
843 throw TGError(R->getLoc(), "Builtin has no class kind");
844 OS << GenBuiltin(name, Proto, TypeVec[ti], ck);
852 OS << "#undef __ai\n\n";
853 OS << "#endif /* __ARM_NEON_H */\n";
856 void NeonEmitter::runHeader(raw_ostream &OS) {
857 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
859 StringMap<OpKind> EmittedMap;
861 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
864 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
868 std::string name = LowercaseString(R->getName());
869 std::string Proto = R->getValueAsString("Prototype");
870 std::string Types = R->getValueAsString("Types");
872 SmallVector<StringRef, 16> TypeVec;
873 ParseTypes(R, Types, TypeVec);
875 if (R->getSuperClasses().size() < 2)
876 throw TGError(R->getLoc(), "Builtin has no class kind");
878 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
880 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
881 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
882 if (EmittedMap.count(bd))
885 EmittedMap[bd] = OpNone;