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";
316 return quad ? "V16c" : "V8c";
319 // Non-return array types are passed as individual vectors.
321 return quad ? "V16cV16c" : "V8cV8c";
323 return quad ? "V16cV16cV16c" : "V8cV8cV8c";
325 return quad ? "V16cV16cV16cV16c" : "V8cV8cV8cV8c";
327 return quad ? "V4f" : "V2f";
329 return quad ? "V16c" : "V8c";
332 // Turn "vst2_lane" into "vst2q_lane_f32", etc.
333 static std::string MangleName(const std::string &name, StringRef typestr,
335 if (name == "vcvt_f32_f16")
341 char type = ClassifyType(typestr, quad, poly, usgn);
343 std::string s = name;
348 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
349 case ClassI: s += "_i8"; break;
350 case ClassW: s += "_8"; break;
356 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
357 case ClassI: s += "_i16"; break;
358 case ClassW: s += "_16"; break;
364 case ClassS: s += usgn ? "_u32" : "_s32"; break;
365 case ClassI: s += "_i32"; break;
366 case ClassW: s += "_32"; break;
372 case ClassS: s += usgn ? "_u64" : "_s64"; break;
373 case ClassI: s += "_i64"; break;
374 case ClassW: s += "_64"; break;
381 case ClassI: s += "_f16"; break;
382 case ClassW: s += "_16"; break;
389 case ClassI: s += "_f32"; break;
390 case ClassW: s += "_32"; break;
395 throw "unhandled type!";
401 // Insert a 'q' before the first '_' character so that it ends up before
402 // _lane or _n on vector-scalar operations.
404 size_t pos = s.find('_');
405 s = s.insert(pos, "q");
410 // Generate the string "(argtype a, argtype b, ...)"
411 static std::string GenArgs(const std::string &proto, StringRef typestr) {
417 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
418 s += TypeString(proto[i], typestr);
429 // Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
430 // If structTypes is true, the NEON types are structs of vector types rather
431 // than vector types, and the call becomes "a.val + b.val"
432 static std::string GenOpString(OpKind op, const std::string &proto,
433 StringRef typestr, bool structTypes = true) {
434 std::string ts = TypeString(proto[0], typestr);
435 std::string s = ts + " r; r";
438 char type = ClassifyType(typestr, quad, dummy, dummy);
441 case 'c': nElts = 8; break;
442 case 's': nElts = 4; break;
443 case 'i': nElts = 2; break;
444 case 'l': nElts = 1; break;
445 case 'h': nElts = 4; break;
446 case 'f': nElts = 2; break;
456 if (proto.size() > 1)
457 a = (structTypes && proto[1] != 'l' && proto[1] != 's') ? "a.val" : "a";
458 b = structTypes ? "b.val" : "b";
459 c = structTypes ? "c.val" : "c";
472 s += a + " + ( " + b + " * " + c + " )";
475 s += a + " - ( " + b + " * " + c + " )";
478 s += "(__neon_" + ts + ")(" + a + " == " + b + ")";
481 s += "(__neon_" + ts + ")(" + a + " >= " + b + ")";
484 s += "(__neon_" + ts + ")(" + a + " <= " + b + ")";
487 s += "(__neon_" + ts + ")(" + a + " > " + b + ")";
490 s += "(__neon_" + ts + ")(" + a + " < " + b + ")";
514 s += "(__neon_" + ts + ")" + a;
517 s += "__builtin_shufflevector((__neon_int64x1_t)" + a;
518 s += ", (__neon_int64x1_t)" + b + ", 0, 1)";
521 s += "(__neon_" + ts + "){ ";
522 for (unsigned i = 0; i != nElts; ++i) {
530 throw "unknown OpKind!";
537 static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
538 unsigned mod = proto[0];
541 if (mod == 'v' || mod == 'f')
551 // base type to get the type string for.
552 char type = ClassifyType(typestr, quad, poly, usgn);
554 // Based on the modifying character, change the type and width if necessary.
555 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
582 throw "unhandled type!";
588 // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
589 // If structTypes is true, the NEON types are structs of vector types rather
590 // than vector types, and the call becomes __builtin_neon_cls(a.val)
591 static std::string GenBuiltin(const std::string &name, const std::string &proto,
592 StringRef typestr, ClassKind ck,
593 bool structTypes = true) {
597 bool unioning = (proto[0] == '2' || proto[0] == '3' || proto[0] == '4');
599 // If all types are the same size, bitcasting the args will take care
600 // of arg checking. The actual signedness etc. will be taken care of with
602 if (proto.find('s') == std::string::npos)
605 if (proto[0] != 'v') {
608 s += TypeString(proto[0], typestr, true) + " val; ";
609 s += TypeString(proto[0], typestr, false) + " s; ";
612 s += TypeString(proto[0], typestr);
616 if (structTypes && proto[0] != 's' && proto[0] != 'i' && proto[0] != 'l')
622 s += "__builtin_neon_";
623 s += MangleName(name, typestr, ck);
626 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
627 // Handle multiple-vector values specially, emitting each subvector as an
628 // argument to the __builtin.
629 if (structTypes && (proto[i] == '2' || proto[i] == '3' || proto[i] == '4')){
630 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
632 s += ".val[" + utostr(vi) + "]";
644 if (structTypes && proto[i] != 's' && proto[i] != 'i' && proto[i] != 'l' &&
645 proto[i] != 'p' && proto[i] != 'c') {
652 // Extra constant integer to hold type class enum for this function, e.g. s8
654 s += ", " + utostr(GetNeonEnum(proto, typestr));
658 if (proto[0] != 'v') {
667 static std::string GenBuiltinDef(const std::string &name,
668 const std::string &proto,
669 StringRef typestr, ClassKind ck) {
670 std::string s("BUILTIN(__builtin_neon_");
672 // If all types are the same size, bitcasting the args will take care
673 // of arg checking. The actual signedness etc. will be taken care of with
675 if (proto.find('s') == std::string::npos)
678 s += MangleName(name, typestr, ck);
681 for (unsigned i = 0, e = proto.size(); i != e; ++i)
682 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
684 // Extra constant integer to hold type class enum for this function, e.g. s8
692 void NeonEmitter::run(raw_ostream &OS) {
693 EmitSourceFileHeader("ARM NEON Header", OS);
695 // FIXME: emit license into file?
697 OS << "#ifndef __ARM_NEON_H\n";
698 OS << "#define __ARM_NEON_H\n\n";
700 OS << "#ifndef __ARM_NEON__\n";
701 OS << "#error \"NEON support not enabled\"\n";
704 OS << "#include <stdint.h>\n\n";
706 // Emit NEON-specific scalar typedefs.
707 // FIXME: probably need to do something better for polynomial types.
708 // FIXME: is this the correct thing to do for float16?
709 OS << "typedef float float32_t;\n";
710 OS << "typedef uint8_t poly8_t;\n";
711 OS << "typedef uint16_t poly16_t;\n";
712 OS << "typedef uint16_t float16_t;\n";
714 // Emit Neon vector typedefs.
715 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
716 SmallVector<StringRef, 24> TDTypeVec;
717 ParseTypes(0, TypedefTypes, TDTypeVec);
719 // Emit vector typedefs.
720 for (unsigned v = 1; v != 5; ++v) {
721 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
722 bool dummy, quad = false;
723 (void) ClassifyType(TDTypeVec[i], quad, dummy, dummy);
724 OS << "typedef __attribute__(( __vector_size__(";
726 OS << utostr(8*v*(quad ? 2 : 1)) << ") )) ";
730 OS << TypeString('s', TDTypeVec[i]);
733 char t = (v == 1) ? 'd' : '0' + v;
734 OS << TypeString(t, TDTypeVec[i]) << ";\n";
739 // Emit struct typedefs.
740 for (unsigned vi = 1; vi != 5; ++vi) {
741 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
742 std::string ts = TypeString('d', TDTypeVec[i]);
743 std::string vs = (vi > 1) ? TypeString('0' + vi, TDTypeVec[i]) : ts;
744 OS << "typedef struct __" << vs << " {\n";
745 OS << " __neon_" << ts << " val";
747 OS << "[" << utostr(vi) << "]";
748 OS << ";\n} " << vs << ";\n\n";
752 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
754 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
756 // Unique the return+pattern types, and assign them.
757 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
759 std::string name = LowercaseString(R->getName());
760 std::string Proto = R->getValueAsString("Prototype");
761 std::string Types = R->getValueAsString("Types");
763 SmallVector<StringRef, 16> TypeVec;
764 ParseTypes(R, Types, TypeVec);
766 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
768 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
769 assert(!Proto.empty() && "");
771 // static always inline + return type
772 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
774 // Function name with type suffix
775 OS << " " << MangleName(name, TypeVec[ti], ClassS);
777 // Function arguments
778 OS << GenArgs(Proto, TypeVec[ti]);
784 OS << GenOpString(k, Proto, TypeVec[ti]);
786 if (R->getSuperClasses().size() < 2)
787 throw TGError(R->getLoc(), "Builtin has no class kind");
789 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
792 throw TGError(R->getLoc(), "Builtin has no class kind");
793 OS << GenBuiltin(name, Proto, TypeVec[ti], ck);
800 OS << "#undef __ai\n\n";
801 OS << "#endif /* __ARM_NEON_H */\n";
804 void NeonEmitter::runHeader(raw_ostream &OS) {
805 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
807 StringMap<OpKind> EmittedMap;
809 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
812 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
816 std::string name = LowercaseString(R->getName());
817 std::string Proto = R->getValueAsString("Prototype");
818 std::string Types = R->getValueAsString("Types");
820 SmallVector<StringRef, 16> TypeVec;
821 ParseTypes(R, Types, TypeVec);
823 if (R->getSuperClasses().size() < 2)
824 throw TGError(R->getLoc(), "Builtin has no class kind");
826 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
828 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
829 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
830 if (EmittedMap.count(bd))
833 EmittedMap[bd] = OpNone;