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.
179 s += poly ? "poly8" : "int8";
182 s += quad ? "x16" : "x8";
185 s += poly ? "poly16" : "int16";
188 s += quad ? "x8" : "x4";
194 s += quad ? "x4" : "x2";
200 s += quad ? "x2" : "x1";
206 s += quad ? "x8" : "x4";
212 s += quad ? "x4" : "x2";
215 throw "unhandled type!";
226 // Append _t, finishing the type string typedef type.
238 static std::string BuiltinTypeString(const char mod, StringRef typestr,
239 ClassKind ck, bool ret) {
252 // base type to get the type string for.
253 char type = ClassifyType(typestr, quad, poly, usgn);
255 // Based on the modifying character, change the type and width if necessary.
314 usgn = usgn | poly | ((ck == ClassI || ck == ClassW) && scal && type != 'f');
334 // Since the return value must be one type, return a vector type of the
335 // appropriate width which we will bitcast.
338 return quad ? "V32c" : "V16c";
340 return quad ? "V48c" : "V24c";
342 return quad ? "V64c" : "V32c";
344 return quad ? "V16c" : "V8c";
347 // Non-return array types are passed as individual vectors.
349 return quad ? "V16cV16c" : "V8cV8c";
351 return quad ? "V16cV16cV16c" : "V8cV8cV8c";
353 return quad ? "V16cV16cV16cV16c" : "V8cV8cV8cV8c";
355 return quad ? "V16c" : "V8c";
358 // Turn "vst2_lane" into "vst2q_lane_f32", etc.
359 static std::string MangleName(const std::string &name, StringRef typestr,
364 char type = ClassifyType(typestr, quad, poly, usgn);
366 std::string s = name;
371 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
372 case ClassI: s += "_i8"; break;
373 case ClassW: s += "_8"; break;
379 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
380 case ClassI: s += "_i16"; break;
381 case ClassW: s += "_16"; break;
387 case ClassS: s += usgn ? "_u32" : "_s32"; break;
388 case ClassI: s += "_i32"; break;
389 case ClassW: s += "_32"; break;
395 case ClassS: s += usgn ? "_u64" : "_s64"; break;
396 case ClassI: s += "_i64"; break;
397 case ClassW: s += "_64"; break;
404 case ClassI: s += "_f16"; break;
405 case ClassW: s += "_16"; break;
412 case ClassI: s += "_f32"; break;
413 case ClassW: s += "_32"; break;
418 throw "unhandled type!";
424 // Insert a 'q' before the first '_' character so that it ends up before
425 // _lane or _n on vector-scalar operations.
427 size_t pos = s.find('_');
428 s = s.insert(pos, "q");
433 // Generate the string "(argtype a, argtype b, ...)"
434 static std::string GenArgs(const std::string &proto, StringRef typestr) {
440 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
441 s += TypeString(proto[i], typestr);
452 // Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
453 // If structTypes is true, the NEON types are structs of vector types rather
454 // than vector types, and the call becomes "a.val + b.val"
455 static std::string GenOpString(OpKind op, const std::string &proto,
456 StringRef typestr, bool structTypes = true) {
457 std::string s("return ");
458 std::string ts = TypeString(proto[0], typestr, true);
460 s += "(" + ts + "){";
463 if (proto.size() > 1)
464 a = (structTypes && proto[1] != 'l') ? "a.val" : "a";
465 b = structTypes ? "b.val" : "b";
466 c = structTypes ? "c.val" : "c";
479 s += a + " + ( " + b + " * " + c + " )";
482 s += a + " - ( " + b + " * " + c + " )";
485 s += "(__neon_" + ts + ")(" + a + " == " + b + ")";
488 s += "(__neon_" + ts + ")(" + a + " >= " + b + ")";
491 s += "(__neon_" + ts + ")(" + a + " <= " + b + ")";
494 s += "(__neon_" + ts + ")(" + a + " > " + b + ")";
497 s += "(__neon_" + ts + ")(" + a + " < " + b + ")";
521 s += "(__neon_" + ts + ")" + a;
524 throw "unknown OpKind!";
534 // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
535 // If structTypes is true, the NEON types are structs of vector types rather
536 // than vector types, and the call becomes __builtin_neon_cls(a.val)
537 static std::string GenBuiltin(const std::string &name, const std::string &proto,
538 StringRef typestr, ClassKind ck,
539 bool structTypes = true) {
542 //bool unioning = (proto[0] == '2' || proto[0] == '3' || proto[0] == '4');
544 if (proto[0] != 'v') {
545 // FIXME: if return type is 2/3/4, emit unioning code.
552 s += TypeString(proto[0], typestr, true);
557 s += "__builtin_neon_";
558 s += MangleName(name, typestr, ck);
561 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
563 if (structTypes && proto[i] != 's' && proto[i] != 'i' && proto[i] != 'l' &&
564 proto[i] != 'p' && proto[i] != 'c') {
572 if (proto[0] != 'v' && structTypes)
578 static std::string GenBuiltinDef(const std::string &name,
579 const std::string &proto,
580 StringRef typestr, ClassKind ck) {
581 std::string s("BUILTIN(__builtin_neon_");
583 // If all types are the same size, bitcasting the args will take care
584 // of arg checking. The actual signedness etc. will be taken care of with
586 if (proto.find('s') == std::string::npos)
589 s += MangleName(name, typestr, ck);
592 for (unsigned i = 0, e = proto.size(); i != e; ++i)
593 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
595 // Extra constant integer to hold type class enum for this function, e.g. s8
603 void NeonEmitter::run(raw_ostream &OS) {
604 EmitSourceFileHeader("ARM NEON Header", OS);
606 // FIXME: emit license into file?
608 OS << "#ifndef __ARM_NEON_H\n";
609 OS << "#define __ARM_NEON_H\n\n";
611 OS << "#ifndef __ARM_NEON__\n";
612 OS << "#error \"NEON support not enabled\"\n";
615 OS << "#include <stdint.h>\n\n";
617 // Emit NEON-specific scalar typedefs.
618 // FIXME: probably need to do something better for polynomial types.
619 // FIXME: is this the correct thing to do for float16?
620 OS << "typedef float float32_t;\n";
621 OS << "typedef uint8_t poly8_t;\n";
622 OS << "typedef uint16_t poly16_t;\n";
623 OS << "typedef uint16_t float16_t;\n";
625 // Emit Neon vector typedefs.
626 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
627 SmallVector<StringRef, 24> TDTypeVec;
628 ParseTypes(0, TypedefTypes, TDTypeVec);
630 // Emit vector typedefs.
631 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
632 bool dummy, quad = false;
633 (void) ClassifyType(TDTypeVec[i], quad, dummy, dummy);
634 OS << "typedef __attribute__(( __vector_size__(";
635 OS << (quad ? "16) )) " : "8) )) ");
636 OS << TypeString('s', TDTypeVec[i]);
638 OS << TypeString('d', TDTypeVec[i]) << ";\n";
642 // Emit struct typedefs.
643 for (unsigned vi = 1; vi != 5; ++vi) {
644 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
645 std::string ts = TypeString('d', TDTypeVec[i]);
646 std::string vs = (vi > 1) ? TypeString('0' + vi, TDTypeVec[i]) : ts;
647 OS << "typedef struct __" << vs << " {\n";
648 OS << " __neon_" << ts << " val";
650 OS << "[" << utostr(vi) << "]";
651 OS << ";\n} " << vs << ";\n\n";
655 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
657 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
659 // Unique the return+pattern types, and assign them.
660 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
662 std::string name = LowercaseString(R->getName());
663 std::string Proto = R->getValueAsString("Prototype");
664 std::string Types = R->getValueAsString("Types");
666 SmallVector<StringRef, 16> TypeVec;
667 ParseTypes(R, Types, TypeVec);
669 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
671 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
672 assert(!Proto.empty() && "");
674 // static always inline + return type
675 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
677 // Function name with type suffix
678 OS << " " << MangleName(name, TypeVec[ti], ClassS);
680 // Function arguments
681 OS << GenArgs(Proto, TypeVec[ti]);
687 OS << GenOpString(k, Proto, TypeVec[ti]);
689 if (R->getSuperClasses().size() < 2)
690 throw TGError(R->getLoc(), "Builtin has no class kind");
692 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
695 throw TGError(R->getLoc(), "Builtin has no class kind");
696 OS << GenBuiltin(name, Proto, TypeVec[ti], ck);
703 OS << "#undef __ai\n\n";
704 OS << "#endif /* __ARM_NEON_H */\n";
707 void NeonEmitter::runHeader(raw_ostream &OS) {
708 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
710 StringMap<OpKind> EmittedMap;
712 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
715 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
719 std::string name = LowercaseString(R->getName());
720 std::string Proto = R->getValueAsString("Prototype");
721 std::string Types = R->getValueAsString("Types");
723 SmallVector<StringRef, 16> TypeVec;
724 ParseTypes(R, Types, TypeVec);
726 if (R->getSuperClasses().size() < 2)
727 throw TGError(R->getLoc(), "Builtin has no class kind");
729 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
731 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
732 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
733 if (EmittedMap.count(bd))
736 EmittedMap[bd] = OpNone;