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";
314 return quad ? "V4f" : "V2f";
315 if (mod == 'x' || mod == 'u')
316 return quad ? "V4i" : "V2i";
318 return quad ? "V16c" : "V8c";
321 // Non-return array types are passed as individual vectors.
323 return quad ? "V16cV16c" : "V8cV8c";
325 return quad ? "V16cV16cV16c" : "V8cV8cV8c";
327 return quad ? "V16cV16cV16cV16c" : "V8cV8cV8cV8c";
329 return quad ? "V4f" : "V2f";
331 return quad ? "V16c" : "V8c";
334 // Turn "vst2_lane" into "vst2q_lane_f32", etc.
335 static std::string MangleName(const std::string &name, StringRef typestr,
337 if (name == "vcvt_f32_f16")
343 char type = ClassifyType(typestr, quad, poly, usgn);
345 std::string s = name;
350 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
351 case ClassI: s += "_i8"; break;
352 case ClassW: s += "_8"; break;
358 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
359 case ClassI: s += "_i16"; break;
360 case ClassW: s += "_16"; break;
366 case ClassS: s += usgn ? "_u32" : "_s32"; break;
367 case ClassI: s += "_i32"; break;
368 case ClassW: s += "_32"; break;
374 case ClassS: s += usgn ? "_u64" : "_s64"; break;
375 case ClassI: s += "_i64"; break;
376 case ClassW: s += "_64"; break;
383 case ClassI: s += "_f16"; break;
384 case ClassW: s += "_16"; break;
391 case ClassI: s += "_f32"; break;
392 case ClassW: s += "_32"; break;
397 throw "unhandled type!";
403 // Insert a 'q' before the first '_' character so that it ends up before
404 // _lane or _n on vector-scalar operations.
406 size_t pos = s.find('_');
407 s = s.insert(pos, "q");
412 // Generate the string "(argtype a, argtype b, ...)"
413 static std::string GenArgs(const std::string &proto, StringRef typestr) {
419 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
420 s += TypeString(proto[i], typestr);
431 // Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
432 // If structTypes is true, the NEON types are structs of vector types rather
433 // than vector types, and the call becomes "a.val + b.val"
434 static std::string GenOpString(OpKind op, const std::string &proto,
435 StringRef typestr, bool structTypes = true) {
436 std::string ts = TypeString(proto[0], typestr);
437 std::string s = ts + " r; r";
440 char type = ClassifyType(typestr, quad, dummy, dummy);
443 case 'c': nElts = 8; break;
444 case 's': nElts = 4; break;
445 case 'i': nElts = 2; break;
446 case 'l': nElts = 1; break;
447 case 'h': nElts = 4; break;
448 case 'f': nElts = 2; break;
458 if (proto.size() > 1)
459 a = (structTypes && proto[1] != 'l' && proto[1] != 's') ? "a.val" : "a";
460 b = structTypes ? "b.val" : "b";
461 c = structTypes ? "c.val" : "c";
474 s += a + " + ( " + b + " * " + c + " )";
477 s += a + " - ( " + b + " * " + c + " )";
480 s += "(__neon_" + ts + ")(" + a + " == " + b + ")";
483 s += "(__neon_" + ts + ")(" + a + " >= " + b + ")";
486 s += "(__neon_" + ts + ")(" + a + " <= " + b + ")";
489 s += "(__neon_" + ts + ")(" + a + " > " + b + ")";
492 s += "(__neon_" + ts + ")(" + a + " < " + b + ")";
516 s += "(__neon_" + ts + ")" + a;
519 s += "__builtin_shufflevector((__neon_int64x1_t)" + a;
520 s += ", (__neon_int64x1_t)" + b + ", 0, 1)";
523 s += "(__neon_" + ts + "){ ";
524 for (unsigned i = 0; i != nElts; ++i) {
532 throw "unknown OpKind!";
539 static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
540 unsigned mod = proto[0];
543 if (mod == 'v' || mod == 'f')
553 // base type to get the type string for.
554 char type = ClassifyType(typestr, quad, poly, usgn);
556 // Based on the modifying character, change the type and width if necessary.
557 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
584 throw "unhandled type!";
590 // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
591 // If structTypes is true, the NEON types are structs of vector types rather
592 // than vector types, and the call becomes __builtin_neon_cls(a.val)
593 static std::string GenBuiltin(const std::string &name, const std::string &proto,
594 StringRef typestr, ClassKind ck,
595 bool structTypes = true) {
599 bool unioning = (proto[0] == '2' || proto[0] == '3' || proto[0] == '4');
601 // If all types are the same size, bitcasting the args will take care
602 // of arg checking. The actual signedness etc. will be taken care of with
604 if (proto.find('s') == std::string::npos)
607 if (proto[0] != 'v') {
610 s += TypeString(proto[0], typestr, true) + " val; ";
611 s += TypeString(proto[0], typestr, false) + " s; ";
614 s += TypeString(proto[0], typestr);
618 if (structTypes && proto[0] != 's' && proto[0] != 'i' && proto[0] != 'l')
624 s += "__builtin_neon_";
625 s += MangleName(name, typestr, ck);
628 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
629 // Handle multiple-vector values specially, emitting each subvector as an
630 // argument to the __builtin.
631 if (structTypes && (proto[i] == '2' || proto[i] == '3' || proto[i] == '4')){
632 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
634 s += ".val[" + utostr(vi) + "]";
646 if (structTypes && proto[i] != 's' && proto[i] != 'i' && proto[i] != 'l' &&
647 proto[i] != 'p' && proto[i] != 'c') {
654 // Extra constant integer to hold type class enum for this function, e.g. s8
656 s += ", " + utostr(GetNeonEnum(proto, typestr));
660 if (proto[0] != 'v') {
669 static std::string GenBuiltinDef(const std::string &name,
670 const std::string &proto,
671 StringRef typestr, ClassKind ck) {
672 std::string s("BUILTIN(__builtin_neon_");
674 // If all types are the same size, bitcasting the args will take care
675 // of arg checking. The actual signedness etc. will be taken care of with
677 if (proto.find('s') == std::string::npos)
680 s += MangleName(name, typestr, ck);
683 for (unsigned i = 0, e = proto.size(); i != e; ++i)
684 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
686 // Extra constant integer to hold type class enum for this function, e.g. s8
694 void NeonEmitter::run(raw_ostream &OS) {
695 EmitSourceFileHeader("ARM NEON Header", OS);
697 // FIXME: emit license into file?
699 OS << "#ifndef __ARM_NEON_H\n";
700 OS << "#define __ARM_NEON_H\n\n";
702 OS << "#ifndef __ARM_NEON__\n";
703 OS << "#error \"NEON support not enabled\"\n";
706 OS << "#include <stdint.h>\n\n";
708 // Emit NEON-specific scalar typedefs.
709 // FIXME: probably need to do something better for polynomial types.
710 // FIXME: is this the correct thing to do for float16?
711 OS << "typedef float float32_t;\n";
712 OS << "typedef uint8_t poly8_t;\n";
713 OS << "typedef uint16_t poly16_t;\n";
714 OS << "typedef uint16_t float16_t;\n";
716 // Emit Neon vector typedefs.
717 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
718 SmallVector<StringRef, 24> TDTypeVec;
719 ParseTypes(0, TypedefTypes, TDTypeVec);
721 // Emit vector typedefs.
722 for (unsigned v = 1; v != 5; ++v) {
723 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
724 bool dummy, quad = false;
725 (void) ClassifyType(TDTypeVec[i], quad, dummy, dummy);
726 OS << "typedef __attribute__(( __vector_size__(";
728 OS << utostr(8*v*(quad ? 2 : 1)) << ") )) ";
732 OS << TypeString('s', TDTypeVec[i]);
735 char t = (v == 1) ? 'd' : '0' + v;
736 OS << TypeString(t, TDTypeVec[i]) << ";\n";
741 // Emit struct typedefs.
742 for (unsigned vi = 1; vi != 5; ++vi) {
743 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
744 std::string ts = TypeString('d', TDTypeVec[i]);
745 std::string vs = (vi > 1) ? TypeString('0' + vi, TDTypeVec[i]) : ts;
746 OS << "typedef struct __" << vs << " {\n";
747 OS << " __neon_" << ts << " val";
749 OS << "[" << utostr(vi) << "]";
750 OS << ";\n} " << vs << ";\n\n";
754 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
756 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
758 // Unique the return+pattern types, and assign them.
759 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
761 std::string name = LowercaseString(R->getName());
762 std::string Proto = R->getValueAsString("Prototype");
763 std::string Types = R->getValueAsString("Types");
765 SmallVector<StringRef, 16> TypeVec;
766 ParseTypes(R, Types, TypeVec);
768 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
770 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
771 assert(!Proto.empty() && "");
773 // static always inline + return type
774 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
776 // Function name with type suffix
777 OS << " " << MangleName(name, TypeVec[ti], ClassS);
779 // Function arguments
780 OS << GenArgs(Proto, TypeVec[ti]);
786 OS << GenOpString(k, Proto, TypeVec[ti]);
788 if (R->getSuperClasses().size() < 2)
789 throw TGError(R->getLoc(), "Builtin has no class kind");
791 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
794 throw TGError(R->getLoc(), "Builtin has no class kind");
795 OS << GenBuiltin(name, Proto, TypeVec[ti], ck);
802 OS << "#undef __ai\n\n";
803 OS << "#endif /* __ARM_NEON_H */\n";
806 void NeonEmitter::runHeader(raw_ostream &OS) {
807 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
809 StringMap<OpKind> EmittedMap;
811 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
814 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
818 std::string name = LowercaseString(R->getName());
819 std::string Proto = R->getValueAsString("Prototype");
820 std::string Types = R->getValueAsString("Types");
822 SmallVector<StringRef, 16> TypeVec;
823 ParseTypes(R, Types, TypeVec);
825 if (R->getSuperClasses().size() < 2)
826 throw TGError(R->getLoc(), "Builtin has no class kind");
828 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
830 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
831 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
832 if (EmittedMap.count(bd))
835 EmittedMap[bd] = OpNone;