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) {
73 default: throw "unhandled type in widen!";
78 static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) {
99 // base type to get the type string for.
103 static std::string TypeString(const char mod, StringRef typestr,
112 // base type to get the type string for.
113 char type = ClassifyType(typestr, quad, poly, usgn);
115 // Based on the modifying character, change the type and width if necessary.
182 s += poly ? "poly8" : "int8";
185 s += quad ? "x16" : "x8";
188 s += poly ? "poly16" : "int16";
191 s += quad ? "x8" : "x4";
197 s += quad ? "x4" : "x2";
203 s += quad ? "x2" : "x1";
209 s += quad ? "x8" : "x4";
215 s += quad ? "x4" : "x2";
218 throw "unhandled type!";
229 // Append _t, finishing the type string typedef type.
241 static std::string BuiltinTypeString(const char mod, StringRef typestr,
242 ClassKind ck, bool ret) {
255 // base type to get the type string for.
256 char type = ClassifyType(typestr, quad, poly, usgn);
258 // Based on the modifying character, change the type and width if necessary.
317 usgn = usgn | poly | ((ck == ClassI || ck == ClassW) && scal && type != 'f');
337 // Since the return value must be one type, return a vector type of the
338 // appropriate width which we will bitcast.
341 return quad ? "V32c" : "V16c";
343 return quad ? "V48c" : "V24c";
345 return quad ? "V64c" : "V32c";
347 return quad ? "V16c" : "V8c";
350 // Non-return array types are passed as individual vectors.
352 return quad ? "V16cV16c" : "V8cV8c";
354 return quad ? "V16cV16cV16c" : "V8cV8cV8c";
356 return quad ? "V16cV16cV16cV16c" : "V8cV8cV8cV8c";
358 return quad ? "V16c" : "V8c";
361 // Turn "vst2_lane" into "vst2q_lane_f32", etc.
362 static std::string MangleName(const std::string &name, StringRef typestr,
367 char type = ClassifyType(typestr, quad, poly, usgn);
369 std::string s = name;
374 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
375 case ClassI: s += "_i8"; break;
376 case ClassW: s += "_8"; break;
382 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
383 case ClassI: s += "_i16"; break;
384 case ClassW: s += "_16"; break;
390 case ClassS: s += usgn ? "_u32" : "_s32"; break;
391 case ClassI: s += "_i32"; break;
392 case ClassW: s += "_32"; break;
398 case ClassS: s += usgn ? "_u64" : "_s64"; break;
399 case ClassI: s += "_i64"; break;
400 case ClassW: s += "_64"; break;
407 case ClassI: s += "_f16"; break;
408 case ClassW: s += "_16"; break;
415 case ClassI: s += "_f32"; break;
416 case ClassW: s += "_32"; break;
421 throw "unhandled type!";
427 // Insert a 'q' before the first '_' character so that it ends up before
428 // _lane or _n on vector-scalar operations.
430 size_t pos = s.find('_');
431 s = s.insert(pos, "q");
436 // Generate the string "(argtype a, argtype b, ...)"
437 static std::string GenArgs(const std::string &proto, StringRef typestr) {
443 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
444 s += TypeString(proto[i], typestr);
455 // Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
456 // If structTypes is true, the NEON types are structs of vector types rather
457 // than vector types, and the call becomes "a.val + b.val"
458 static std::string GenOpString(OpKind op, const std::string &proto,
459 StringRef typestr, bool structTypes = true) {
460 std::string s("return ");
461 std::string ts = TypeString(proto[0], typestr);
463 s += "(" + ts + "){";
466 if (proto.size() > 1)
467 a = (structTypes && proto[1] != 'l') ? "a.val" : "a";
468 b = structTypes ? "b.val" : "b";
469 c = structTypes ? "c.val" : "c";
482 s += a + " + ( " + b + " * " + c + " )";
485 s += a + " - ( " + b + " * " + c + " )";
488 s += "(__neon_" + ts + ")(" + a + " == " + b + ")";
491 s += "(__neon_" + ts + ")(" + a + " >= " + b + ")";
494 s += "(__neon_" + ts + ")(" + a + " <= " + b + ")";
497 s += "(__neon_" + ts + ")(" + a + " > " + b + ")";
500 s += "(__neon_" + ts + ")(" + a + " < " + b + ")";
524 s += "(__neon_" + ts + ")" + a;
527 throw "unknown OpKind!";
537 // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
538 // If structTypes is true, the NEON types are structs of vector types rather
539 // than vector types, and the call becomes __builtin_neon_cls(a.val)
540 static std::string GenBuiltin(const std::string &name, const std::string &proto,
541 StringRef typestr, ClassKind ck,
542 bool structTypes = true) {
546 bool unioning = (proto[0] == '2' || proto[0] == '3' || proto[0] == '4');
548 // If all types are the same size, bitcasting the args will take care
549 // of arg checking. The actual signedness etc. will be taken care of with
551 if (proto.find('s') == std::string::npos)
554 if (proto[0] != 'v') {
557 s += TypeString(proto[0], typestr, true) + " val; ";
558 s += TypeString(proto[0], typestr, false) + " s; ";
561 s += TypeString(proto[0], typestr);
565 if (structTypes && proto[0] != 's' && proto[0] != 'i' && proto[0] != 'l')
571 s += "__builtin_neon_";
572 s += MangleName(name, typestr, ck);
575 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
576 // Handle multiple-vector values specially, emitting each subvector as an
577 // argument to the __builtin.
578 if (structTypes && (proto[i] == '2' || proto[i] == '3' || proto[i] == '4')){
579 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
581 s += ".val[" + utostr(vi) + "]";
593 if (structTypes && proto[i] != 's' && proto[i] != 'i' && proto[i] != 'l' &&
594 proto[i] != 'p' && proto[i] != 'c') {
601 // Extra constant integer to hold type class enum for this function, e.g. s8
602 // FIXME: emit actual type num.
608 if (proto[0] != 'v') {
617 static std::string GenBuiltinDef(const std::string &name,
618 const std::string &proto,
619 StringRef typestr, ClassKind ck) {
620 std::string s("BUILTIN(__builtin_neon_");
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 s += MangleName(name, typestr, ck);
631 for (unsigned i = 0, e = proto.size(); i != e; ++i)
632 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
634 // Extra constant integer to hold type class enum for this function, e.g. s8
642 void NeonEmitter::run(raw_ostream &OS) {
643 EmitSourceFileHeader("ARM NEON Header", OS);
645 // FIXME: emit license into file?
647 OS << "#ifndef __ARM_NEON_H\n";
648 OS << "#define __ARM_NEON_H\n\n";
650 OS << "#ifndef __ARM_NEON__\n";
651 OS << "#error \"NEON support not enabled\"\n";
654 OS << "#include <stdint.h>\n\n";
656 // Emit NEON-specific scalar typedefs.
657 // FIXME: probably need to do something better for polynomial types.
658 // FIXME: is this the correct thing to do for float16?
659 OS << "typedef float float32_t;\n";
660 OS << "typedef uint8_t poly8_t;\n";
661 OS << "typedef uint16_t poly16_t;\n";
662 OS << "typedef uint16_t float16_t;\n";
664 // Emit Neon vector typedefs.
665 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
666 SmallVector<StringRef, 24> TDTypeVec;
667 ParseTypes(0, TypedefTypes, TDTypeVec);
669 // Emit vector typedefs.
670 for (unsigned v = 1; v != 5; ++v) {
671 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
672 bool dummy, quad = false;
673 (void) ClassifyType(TDTypeVec[i], quad, dummy, dummy);
674 OS << "typedef __attribute__(( __vector_size__(";
676 OS << utostr(8*v*(quad ? 2 : 1)) << ") )) ";
680 OS << TypeString('s', TDTypeVec[i]);
683 char t = (v == 1) ? 'd' : '0' + v;
684 OS << TypeString(t, TDTypeVec[i]) << ";\n";
689 // Emit struct typedefs.
690 for (unsigned vi = 1; vi != 5; ++vi) {
691 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
692 std::string ts = TypeString('d', TDTypeVec[i]);
693 std::string vs = (vi > 1) ? TypeString('0' + vi, TDTypeVec[i]) : ts;
694 OS << "typedef struct __" << vs << " {\n";
695 OS << " __neon_" << ts << " val";
697 OS << "[" << utostr(vi) << "]";
698 OS << ";\n} " << vs << ";\n\n";
702 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
704 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
706 // Unique the return+pattern types, and assign them.
707 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
709 std::string name = LowercaseString(R->getName());
710 std::string Proto = R->getValueAsString("Prototype");
711 std::string Types = R->getValueAsString("Types");
713 SmallVector<StringRef, 16> TypeVec;
714 ParseTypes(R, Types, TypeVec);
716 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
718 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
719 assert(!Proto.empty() && "");
721 // static always inline + return type
722 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
724 // Function name with type suffix
725 OS << " " << MangleName(name, TypeVec[ti], ClassS);
727 // Function arguments
728 OS << GenArgs(Proto, TypeVec[ti]);
734 OS << GenOpString(k, Proto, TypeVec[ti]);
736 if (R->getSuperClasses().size() < 2)
737 throw TGError(R->getLoc(), "Builtin has no class kind");
739 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
742 throw TGError(R->getLoc(), "Builtin has no class kind");
743 OS << GenBuiltin(name, Proto, TypeVec[ti], ck);
750 OS << "#undef __ai\n\n";
751 OS << "#endif /* __ARM_NEON_H */\n";
754 void NeonEmitter::runHeader(raw_ostream &OS) {
755 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
757 StringMap<OpKind> EmittedMap;
759 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
762 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
766 std::string name = LowercaseString(R->getName());
767 std::string Proto = R->getValueAsString("Prototype");
768 std::string Types = R->getValueAsString("Types");
770 SmallVector<StringRef, 16> TypeVec;
771 ParseTypes(R, Types, TypeVec);
773 if (R->getSuperClasses().size() < 2)
774 throw TGError(R->getLoc(), "Builtin has no class kind");
776 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
778 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
779 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
780 if (EmittedMap.count(bd))
783 EmittedMap[bd] = OpNone;