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) {
111 // base type to get the type string for.
112 char type = ClassifyType(typestr, quad, poly, usgn);
114 // Based on the modifying character, change the type and width if necessary.
178 s += poly ? "poly8" : "int8";
181 s += quad ? "x16" : "x8";
184 s += poly ? "poly16" : "int16";
187 s += quad ? "x8" : "x4";
193 s += quad ? "x4" : "x2";
199 s += quad ? "x2" : "x1";
205 s += quad ? "x8" : "x4";
211 s += quad ? "x4" : "x2";
214 throw "unhandled type!";
225 // Append _t, finishing the type string typedef type.
237 static std::string BuiltinTypeString(const char mod, StringRef typestr) {
250 // base type to get the type string for.
251 char type = ClassifyType(typestr, quad, poly, usgn);
253 // Based on the modifying character, change the type and width if necessary.
328 return quad ? "V16cV16c" : "V8cV8c";
330 return quad ? "V16cV16cV16c" : "V8cV8cV8c";
332 return quad ? "V16cV16cV16cV16c" : "V8cV8cV8cV8c";
334 return quad ? "V16c" : "V8c";
337 // Turn "vst2_lane" into "vst2q_lane_f32", etc.
338 static std::string MangleName(const std::string &name, StringRef typestr,
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 s("return ");
437 std::string ts = TypeString(proto[0], typestr);
439 s += "(" + ts + "){";
442 if (proto.size() > 1)
443 a = (structTypes && proto[1] != 'l') ? "a.val" : "a";
444 b = structTypes ? "b.val" : "b";
445 c = structTypes ? "c.val" : "c";
458 s += a + " + ( " + b + " * " + c + " )";
461 s += a + " - ( " + b + " * " + c + " )";
464 s += "(__neon_" + ts + ")(" + a + " == " + b + ")";
467 s += "(__neon_" + ts + ")(" + a + " >= " + b + ")";
470 s += "(__neon_" + ts + ")(" + a + " <= " + b + ")";
473 s += "(__neon_" + ts + ")(" + a + " > " + b + ")";
476 s += "(__neon_" + ts + ")(" + a + " < " + b + ")";
500 s += "(__neon_" + ts + ")" + a;
503 throw "unknown OpKind!";
513 // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
514 // If structTypes is true, the NEON types are structs of vector types rather
515 // than vector types, and the call becomes __builtin_neon_cls(a.val)
516 static std::string GenBuiltin(const std::string &name, const std::string &proto,
517 StringRef typestr, ClassKind ck,
518 bool structTypes = true) {
522 if (proto[0] != 'v') {
523 // FIXME: if return type is 2/3/4, emit unioning code.
527 s += TypeString(proto[0], typestr);
532 s += "__builtin_neon_";
533 s += MangleName(name, typestr, ck);
536 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
538 if (structTypes && proto[i] != 's' && proto[i] != 'i' && proto[i] != 'l' &&
539 proto[i] != 'p' && proto[i] != 'c') {
547 if (proto[0] != 'v' && structTypes)
553 static std::string GenBuiltinDef(const std::string &name,
554 const std::string &proto,
555 StringRef typestr, ClassKind ck) {
556 std::string s("BUILTIN(__builtin_neon_");
558 // If all types are the same size, bitcasting the args will take care
559 // of arg checking. The actual signedness etc. will be taken care of with
561 if (proto.find('s') == std::string::npos)
564 s += MangleName(name, typestr, ck);
567 for (unsigned i = 0, e = proto.size(); i != e; ++i)
568 s += BuiltinTypeString(proto[i], typestr);
574 void NeonEmitter::run(raw_ostream &OS) {
575 EmitSourceFileHeader("ARM NEON Header", OS);
577 // FIXME: emit license into file?
579 OS << "#ifndef __ARM_NEON_H\n";
580 OS << "#define __ARM_NEON_H\n\n";
582 OS << "#ifndef __ARM_NEON__\n";
583 OS << "#error \"NEON support not enabled\"\n";
586 OS << "#include <stdint.h>\n\n";
588 // Emit NEON-specific scalar typedefs.
589 // FIXME: probably need to do something better for polynomial types.
590 // FIXME: is this the correct thing to do for float16?
591 OS << "typedef float float32_t;\n";
592 OS << "typedef uint8_t poly8_t;\n";
593 OS << "typedef uint16_t poly16_t;\n";
594 OS << "typedef uint16_t float16_t;\n";
596 // Emit Neon vector typedefs.
597 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
598 SmallVector<StringRef, 24> TDTypeVec;
599 ParseTypes(0, TypedefTypes, TDTypeVec);
601 // Emit vector typedefs.
602 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
603 bool dummy, quad = false;
604 (void) ClassifyType(TDTypeVec[i], quad, dummy, dummy);
605 OS << "typedef __attribute__(( __vector_size__(";
606 OS << (quad ? "16) )) " : "8) )) ");
607 OS << TypeString('s', TDTypeVec[i]);
609 OS << TypeString('d', TDTypeVec[i]) << ";\n";
613 // Emit struct typedefs.
614 for (unsigned vi = 1; vi != 5; ++vi) {
615 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
616 std::string ts = TypeString('d', TDTypeVec[i]);
617 std::string vs = (vi > 1) ? TypeString('0' + vi, TDTypeVec[i]) : ts;
618 OS << "typedef struct __" << vs << " {\n";
619 OS << " __neon_" << ts << " val";
621 OS << "[" << utostr(vi) << "]";
622 OS << ";\n} " << vs << ";\n\n";
626 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
628 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
630 // Unique the return+pattern types, and assign them.
631 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
633 std::string name = LowercaseString(R->getName());
634 std::string Proto = R->getValueAsString("Prototype");
635 std::string Types = R->getValueAsString("Types");
637 SmallVector<StringRef, 16> TypeVec;
638 ParseTypes(R, Types, TypeVec);
640 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
642 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
643 assert(!Proto.empty() && "");
645 // static always inline + return type
646 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
648 // Function name with type suffix
649 OS << " " << MangleName(name, TypeVec[ti], ClassS);
651 // Function arguments
652 OS << GenArgs(Proto, TypeVec[ti]);
658 OS << GenOpString(k, Proto, TypeVec[ti]);
660 if (R->getSuperClasses().size() < 2)
661 throw TGError(R->getLoc(), "Builtin has no class kind");
663 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
666 throw TGError(R->getLoc(), "Builtin has no class kind");
667 OS << GenBuiltin(name, Proto, TypeVec[ti], ck);
674 OS << "#undef __ai\n\n";
675 OS << "#endif /* __ARM_NEON_H */\n";
678 void NeonEmitter::runHeader(raw_ostream &OS) {
679 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
681 StringMap<OpKind> EmittedMap;
683 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
686 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
690 std::string name = LowercaseString(R->getName());
691 std::string Proto = R->getValueAsString("Prototype");
692 std::string Types = R->getValueAsString("Types");
694 SmallVector<StringRef, 16> TypeVec;
695 ParseTypes(R, Types, TypeVec);
697 if (R->getSuperClasses().size() < 2)
698 throw TGError(R->getLoc(), "Builtin has no class kind");
700 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
702 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
703 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
704 if (EmittedMap.count(bd))
707 EmittedMap[bd] = OpNone;