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 // Turn "vst2_lane" into "vst2q_lane_f32", etc.
238 static std::string MangleName(const std::string &name, StringRef typestr,
243 char type = ClassifyType(typestr, quad, poly, usgn);
245 std::string s = name;
250 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
251 case ClassI: s += "_i8"; break;
252 case ClassW: s += "_8"; break;
258 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
259 case ClassI: s += "_i16"; break;
260 case ClassW: s += "_16"; break;
266 case ClassS: s += usgn ? "_u32" : "_s32"; break;
267 case ClassI: s += "_i32"; break;
268 case ClassW: s += "_32"; break;
274 case ClassS: s += usgn ? "_u64" : "_s64"; break;
275 case ClassI: s += "_i64"; break;
276 case ClassW: s += "_64"; break;
283 case ClassI: s += "_f16"; break;
284 case ClassW: s += "_16"; break;
291 case ClassI: s += "_f32"; break;
292 case ClassW: s += "_32"; break;
297 throw "unhandled type!";
303 // Insert a 'q' before the first '_' character so that it ends up before
304 // _lane or _n on vector-scalar operations.
306 size_t pos = s.find('_');
307 s = s.insert(pos, "q");
312 // Generate the string "(argtype a, argtype b, ...)"
313 static std::string GenArgs(const std::string &proto, StringRef typestr) {
319 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
320 s += TypeString(proto[i], typestr);
331 // Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
332 // If structTypes is true, the NEON types are structs of vector types rather
333 // than vector types, and the call becomes "a.val + b.val"
334 static std::string GenOpString(OpKind op, const std::string &proto,
335 StringRef typestr, bool structTypes = true) {
336 std::string s("return ");
337 std::string ts = TypeString(proto[0], typestr);
339 s += "(" + ts + "){";
342 if (proto.size() > 1)
343 a = (structTypes && proto[1] != 'l') ? "a.val" : "a";
344 b = structTypes ? "b.val" : "b";
345 c = structTypes ? "c.val" : "c";
358 s += a + " + ( " + b + " * " + c + " )";
361 s += a + " - ( " + b + " * " + c + " )";
364 s += "(__neon_" + ts + ")(" + a + " == " + b + ")";
367 s += "(__neon_" + ts + ")(" + a + " >= " + b + ")";
370 s += "(__neon_" + ts + ")(" + a + " <= " + b + ")";
373 s += "(__neon_" + ts + ")(" + a + " > " + b + ")";
376 s += "(__neon_" + ts + ")(" + a + " < " + b + ")";
400 s += "(__neon_" + ts + ")" + a;
403 throw "unknown OpKind!";
413 // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
414 // If structTypes is true, the NEON types are structs of vector types rather
415 // than vector types, and the call becomes __builtin_neon_cls(a.val)
416 static std::string GenBuiltin(const std::string &name, const std::string &proto,
417 StringRef typestr, ClassKind ck,
418 bool structTypes = true) {
422 if (proto[0] != 'v') {
423 // FIXME: if return type is 2/3/4, emit unioning code.
427 s += TypeString(proto[0], typestr);
432 s += "__builtin_neon_";
433 s += MangleName(name, typestr, ck);
436 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
438 if (structTypes && proto[i] != 's' && proto[i] != 'i' && proto[i] != 'l' &&
439 proto[i] != 'p' && proto[i] != 'c') {
447 if (proto[0] != 'v' && structTypes)
453 static std::string GenBuiltinDef(const std::string &name,
454 const std::string &proto,
455 StringRef typestr, ClassKind ck) {
456 std::string s("BUILTIN(__builtin_neon_");
457 s += MangleName(name, typestr, ck);
460 for (unsigned i = 0, e = proto.size(); i != e; ++i) {
467 void NeonEmitter::run(raw_ostream &OS) {
468 EmitSourceFileHeader("ARM NEON Header", OS);
470 // FIXME: emit license into file?
472 OS << "#ifndef __ARM_NEON_H\n";
473 OS << "#define __ARM_NEON_H\n\n";
475 OS << "#ifndef __ARM_NEON__\n";
476 OS << "#error \"NEON support not enabled\"\n";
479 OS << "#include <stdint.h>\n\n";
481 // Emit NEON-specific scalar typedefs.
482 // FIXME: probably need to do something better for polynomial types.
483 // FIXME: is this the correct thing to do for float16?
484 OS << "typedef float float32_t;\n";
485 OS << "typedef uint8_t poly8_t;\n";
486 OS << "typedef uint16_t poly16_t;\n";
487 OS << "typedef uint16_t float16_t;\n";
489 // Emit Neon vector typedefs.
490 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
491 SmallVector<StringRef, 24> TDTypeVec;
492 ParseTypes(0, TypedefTypes, TDTypeVec);
494 // Emit vector typedefs.
495 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
496 bool dummy, quad = false;
497 (void) ClassifyType(TDTypeVec[i], quad, dummy, dummy);
498 OS << "typedef __attribute__(( __vector_size__(";
499 OS << (quad ? "16) )) " : "8) )) ");
500 OS << TypeString('s', TDTypeVec[i]);
502 OS << TypeString('d', TDTypeVec[i]) << ";\n";
506 // Emit struct typedefs.
507 for (unsigned vi = 1; vi != 5; ++vi) {
508 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
509 std::string ts = TypeString('d', TDTypeVec[i]);
510 std::string vs = (vi > 1) ? TypeString('0' + vi, TDTypeVec[i]) : ts;
511 OS << "typedef struct __" << vs << " {\n";
512 OS << " __neon_" << ts << " val";
514 OS << "[" << utostr(vi) << "]";
515 OS << ";\n} " << vs << ";\n\n";
519 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
521 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
523 // Unique the return+pattern types, and assign them.
524 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
526 std::string name = LowercaseString(R->getName());
527 std::string Proto = R->getValueAsString("Prototype");
528 std::string Types = R->getValueAsString("Types");
530 SmallVector<StringRef, 16> TypeVec;
531 ParseTypes(R, Types, TypeVec);
533 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
535 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
536 assert(!Proto.empty() && "");
538 // static always inline + return type
539 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
541 // Function name with type suffix
542 OS << " " << MangleName(name, TypeVec[ti], ClassS);
544 // Function arguments
545 OS << GenArgs(Proto, TypeVec[ti]);
551 OS << GenOpString(k, Proto, TypeVec[ti]);
553 if (R->getSuperClasses().size() < 2)
554 throw TGError(R->getLoc(), "Builtin has no class kind");
556 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
559 throw TGError(R->getLoc(), "Builtin has no class kind");
560 OS << GenBuiltin(name, Proto, TypeVec[ti], ck);
567 OS << "#undef __ai\n\n";
568 OS << "#endif /* __ARM_NEON_H */\n";
571 void NeonEmitter::runHeader(raw_ostream &OS) {
572 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
574 StringMap<OpKind> EmittedMap;
576 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
579 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
583 std::string name = LowercaseString(R->getName());
584 std::string Proto = R->getValueAsString("Prototype");
585 std::string Types = R->getValueAsString("Types");
587 SmallVector<StringRef, 16> TypeVec;
588 ParseTypes(R, Types, TypeVec);
590 if (R->getSuperClasses().size() < 2)
591 throw TGError(R->getLoc(), "Builtin has no class kind");
593 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
595 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
596 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
597 if (EmittedMap.count(bd))
600 EmittedMap[bd] = OpNone;