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 char ModType(const char mod, char type, bool &quad, bool &poly,
104 bool &usgn, bool &scal, bool &cnst, bool &pntr) {
161 static std::string TypeString(const char mod, StringRef typestr,
175 // base type to get the type string for.
176 char type = ClassifyType(typestr, quad, poly, usgn);
178 // Based on the modifying character, change the type and width if necessary.
179 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
191 s += poly ? "poly8" : "int8";
194 s += quad ? "x16" : "x8";
197 s += poly ? "poly16" : "int16";
200 s += quad ? "x8" : "x4";
206 s += quad ? "x4" : "x2";
212 s += quad ? "x2" : "x1";
218 s += quad ? "x8" : "x4";
224 s += quad ? "x4" : "x2";
227 throw "unhandled type!";
238 // Append _t, finishing the type string typedef type.
250 static std::string BuiltinTypeString(const char mod, StringRef typestr,
251 ClassKind ck, bool ret) {
264 // base type to get the type string for.
265 char type = ClassifyType(typestr, quad, poly, usgn);
267 // Based on the modifying character, change the type and width if necessary.
268 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
277 usgn = usgn | poly | ((ck == ClassI || ck == ClassW) && scal && type != 'f');
297 // Since the return value must be one type, return a vector type of the
298 // appropriate width which we will bitcast.
301 return quad ? "V32c" : "V16c";
303 return quad ? "V48c" : "V24c";
305 return quad ? "V64c" : "V32c";
307 return quad ? "V16c" : "V8c";
310 // Non-return array types are passed as individual vectors.
312 return quad ? "V16cV16c" : "V8cV8c";
314 return quad ? "V16cV16cV16c" : "V8cV8cV8c";
316 return quad ? "V16cV16cV16cV16c" : "V8cV8cV8cV8c";
318 return quad ? "V16c" : "V8c";
321 // Turn "vst2_lane" into "vst2q_lane_f32", etc.
322 static std::string MangleName(const std::string &name, StringRef typestr,
327 char type = ClassifyType(typestr, quad, poly, usgn);
329 std::string s = name;
334 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
335 case ClassI: s += "_i8"; break;
336 case ClassW: s += "_8"; break;
342 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
343 case ClassI: s += "_i16"; break;
344 case ClassW: s += "_16"; break;
350 case ClassS: s += usgn ? "_u32" : "_s32"; break;
351 case ClassI: s += "_i32"; break;
352 case ClassW: s += "_32"; break;
358 case ClassS: s += usgn ? "_u64" : "_s64"; break;
359 case ClassI: s += "_i64"; break;
360 case ClassW: s += "_64"; break;
367 case ClassI: s += "_f16"; break;
368 case ClassW: s += "_16"; break;
375 case ClassI: s += "_f32"; break;
376 case ClassW: s += "_32"; break;
381 throw "unhandled type!";
387 // Insert a 'q' before the first '_' character so that it ends up before
388 // _lane or _n on vector-scalar operations.
390 size_t pos = s.find('_');
391 s = s.insert(pos, "q");
396 // Generate the string "(argtype a, argtype b, ...)"
397 static std::string GenArgs(const std::string &proto, StringRef typestr) {
403 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
404 s += TypeString(proto[i], typestr);
415 // Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
416 // If structTypes is true, the NEON types are structs of vector types rather
417 // than vector types, and the call becomes "a.val + b.val"
418 static std::string GenOpString(OpKind op, const std::string &proto,
419 StringRef typestr, bool structTypes = true) {
420 std::string s("return ");
421 std::string ts = TypeString(proto[0], typestr);
423 s += "(" + ts + "){";
426 if (proto.size() > 1)
427 a = (structTypes && proto[1] != 'l') ? "a.val" : "a";
428 b = structTypes ? "b.val" : "b";
429 c = structTypes ? "c.val" : "c";
442 s += a + " + ( " + b + " * " + c + " )";
445 s += a + " - ( " + b + " * " + c + " )";
448 s += "(__neon_" + ts + ")(" + a + " == " + b + ")";
451 s += "(__neon_" + ts + ")(" + a + " >= " + b + ")";
454 s += "(__neon_" + ts + ")(" + a + " <= " + b + ")";
457 s += "(__neon_" + ts + ")(" + a + " > " + b + ")";
460 s += "(__neon_" + ts + ")(" + a + " < " + b + ")";
484 s += "(__neon_" + ts + ")" + a;
487 throw "unknown OpKind!";
497 static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
498 unsigned mod = proto[0];
511 // base type to get the type string for.
512 char type = ClassifyType(typestr, quad, poly, usgn);
514 // Based on the modifying character, change the type and width if necessary.
515 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
542 throw "unhandled type!";
548 // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
549 // If structTypes is true, the NEON types are structs of vector types rather
550 // than vector types, and the call becomes __builtin_neon_cls(a.val)
551 static std::string GenBuiltin(const std::string &name, const std::string &proto,
552 StringRef typestr, ClassKind ck,
553 bool structTypes = true) {
557 bool unioning = (proto[0] == '2' || proto[0] == '3' || proto[0] == '4');
559 // If all types are the same size, bitcasting the args will take care
560 // of arg checking. The actual signedness etc. will be taken care of with
562 if (proto.find('s') == std::string::npos)
565 if (proto[0] != 'v') {
568 s += TypeString(proto[0], typestr, true) + " val; ";
569 s += TypeString(proto[0], typestr, false) + " s; ";
572 s += TypeString(proto[0], typestr);
576 if (structTypes && proto[0] != 's' && proto[0] != 'i' && proto[0] != 'l')
582 s += "__builtin_neon_";
583 s += MangleName(name, typestr, ck);
586 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
587 // Handle multiple-vector values specially, emitting each subvector as an
588 // argument to the __builtin.
589 if (structTypes && (proto[i] == '2' || proto[i] == '3' || proto[i] == '4')){
590 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
592 s += ".val[" + utostr(vi) + "]";
604 if (structTypes && proto[i] != 's' && proto[i] != 'i' && proto[i] != 'l' &&
605 proto[i] != 'p' && proto[i] != 'c') {
612 // Extra constant integer to hold type class enum for this function, e.g. s8
614 s += ", " + utostr(GetNeonEnum(proto, typestr));
618 if (proto[0] != 'v') {
627 static std::string GenBuiltinDef(const std::string &name,
628 const std::string &proto,
629 StringRef typestr, ClassKind ck) {
630 std::string s("BUILTIN(__builtin_neon_");
632 // If all types are the same size, bitcasting the args will take care
633 // of arg checking. The actual signedness etc. will be taken care of with
635 if (proto.find('s') == std::string::npos)
638 s += MangleName(name, typestr, ck);
641 for (unsigned i = 0, e = proto.size(); i != e; ++i)
642 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
644 // Extra constant integer to hold type class enum for this function, e.g. s8
652 void NeonEmitter::run(raw_ostream &OS) {
653 EmitSourceFileHeader("ARM NEON Header", OS);
655 // FIXME: emit license into file?
657 OS << "#ifndef __ARM_NEON_H\n";
658 OS << "#define __ARM_NEON_H\n\n";
660 OS << "#ifndef __ARM_NEON__\n";
661 OS << "#error \"NEON support not enabled\"\n";
664 OS << "#include <stdint.h>\n\n";
666 // Emit NEON-specific scalar typedefs.
667 // FIXME: probably need to do something better for polynomial types.
668 // FIXME: is this the correct thing to do for float16?
669 OS << "typedef float float32_t;\n";
670 OS << "typedef uint8_t poly8_t;\n";
671 OS << "typedef uint16_t poly16_t;\n";
672 OS << "typedef uint16_t float16_t;\n";
674 // Emit Neon vector typedefs.
675 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
676 SmallVector<StringRef, 24> TDTypeVec;
677 ParseTypes(0, TypedefTypes, TDTypeVec);
679 // Emit vector typedefs.
680 for (unsigned v = 1; v != 5; ++v) {
681 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
682 bool dummy, quad = false;
683 (void) ClassifyType(TDTypeVec[i], quad, dummy, dummy);
684 OS << "typedef __attribute__(( __vector_size__(";
686 OS << utostr(8*v*(quad ? 2 : 1)) << ") )) ";
690 OS << TypeString('s', TDTypeVec[i]);
693 char t = (v == 1) ? 'd' : '0' + v;
694 OS << TypeString(t, TDTypeVec[i]) << ";\n";
699 // Emit struct typedefs.
700 for (unsigned vi = 1; vi != 5; ++vi) {
701 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
702 std::string ts = TypeString('d', TDTypeVec[i]);
703 std::string vs = (vi > 1) ? TypeString('0' + vi, TDTypeVec[i]) : ts;
704 OS << "typedef struct __" << vs << " {\n";
705 OS << " __neon_" << ts << " val";
707 OS << "[" << utostr(vi) << "]";
708 OS << ";\n} " << vs << ";\n\n";
712 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
714 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
716 // Unique the return+pattern types, and assign them.
717 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
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 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
728 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
729 assert(!Proto.empty() && "");
731 // static always inline + return type
732 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
734 // Function name with type suffix
735 OS << " " << MangleName(name, TypeVec[ti], ClassS);
737 // Function arguments
738 OS << GenArgs(Proto, TypeVec[ti]);
744 OS << GenOpString(k, Proto, TypeVec[ti]);
746 if (R->getSuperClasses().size() < 2)
747 throw TGError(R->getLoc(), "Builtin has no class kind");
749 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
752 throw TGError(R->getLoc(), "Builtin has no class kind");
753 OS << GenBuiltin(name, Proto, TypeVec[ti], ck);
760 OS << "#undef __ai\n\n";
761 OS << "#endif /* __ARM_NEON_H */\n";
764 void NeonEmitter::runHeader(raw_ostream &OS) {
765 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
767 StringMap<OpKind> EmittedMap;
769 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
772 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
776 std::string name = LowercaseString(R->getName());
777 std::string Proto = R->getValueAsString("Prototype");
778 std::string Types = R->getValueAsString("Types");
780 SmallVector<StringRef, 16> TypeVec;
781 ParseTypes(R, Types, TypeVec);
783 if (R->getSuperClasses().size() < 2)
784 throw TGError(R->getLoc(), "Builtin has no class kind");
786 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
788 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
789 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
790 if (EmittedMap.count(bd))
793 EmittedMap[bd] = OpNone;