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"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/ADT/StringMap.h"
57 static void ParseTypes(Record *r, std::string &s,
58 SmallVectorImpl<StringRef> &TV) {
59 const char *data = s.data();
62 for (unsigned i = 0, e = s.size(); i != e; ++i, ++len) {
63 if (data[len] == 'P' || data[len] == 'Q' || data[len] == 'U')
75 throw TGError(r->getLoc(),
76 "Unexpected letter: " + std::string(data + len, 1));
79 TV.push_back(StringRef(data, len + 1));
85 static char Widen(const char t) {
93 default: throw "unhandled type in widen!";
98 static char Narrow(const char t) {
106 default: throw "unhandled type in widen!";
111 static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) {
115 if (ty[off] == 'Q') {
121 if (ty[off] == 'P') {
126 // remember unsigned.
127 if (ty[off] == 'U') {
132 // base type to get the type string for.
136 static std::string TypeString(const char mod, StringRef typestr) {
144 // base type to get the type string for.
145 char type = ClassifyType(typestr, quad, poly, usgn);
147 // Based on the modifying character, change the type and width if necessary.
211 s += poly ? "poly8" : "int8";
214 s += quad ? "x16" : "x8";
217 s += poly ? "poly16" : "int16";
220 s += quad ? "x8" : "x4";
226 s += quad ? "x4" : "x2";
232 s += quad ? "x2" : "x1";
238 s += quad ? "x8" : "x4";
244 s += quad ? "x4" : "x2";
247 throw "unhandled type!";
258 // Append _t, finishing the type string typedef type.
270 // Turn "vst2_lane" into "vst2q_lane_f32", etc.
271 static std::string MangleName(const std::string &name, StringRef typestr,
276 char type = ClassifyType(typestr, quad, poly, usgn);
278 std::string s = name;
283 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
284 case ClassI: s += "_i8"; break;
285 case ClassW: s += "_8"; break;
291 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
292 case ClassI: s += "_i16"; break;
293 case ClassW: s += "_16"; break;
299 case ClassS: s += usgn ? "_u32" : "_s32"; break;
300 case ClassI: s += "_i32"; break;
301 case ClassW: s += "_32"; break;
307 case ClassS: s += usgn ? "_u64" : "_s64"; break;
308 case ClassI: s += "_i64"; break;
309 case ClassW: s += "_64"; break;
316 case ClassI: s += "_f16"; break;
317 case ClassW: s += "_16"; break;
324 case ClassI: s += "_f32"; break;
325 case ClassW: s += "_32"; break;
330 throw "unhandled type!";
336 // Insert a 'q' before the first '_' character so that it ends up before
337 // _lane or _n on vector-scalar operations.
339 size_t pos = s.find('_');
340 s = s.insert(pos, "q");
345 // Generate the string "(argtype a, argtype b, ...)"
346 static std::string GenArgs(const std::string &proto, StringRef typestr) {
352 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
353 s += TypeString(proto[i], typestr);
364 // Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
365 // If structTypes is true, the NEON types are structs of vector types rather
366 // than vector types, and the call becomes "a.val + b.val"
367 static std::string GenOpString(OpKind op, const std::string &proto,
368 StringRef typestr, bool structTypes = true) {
369 std::string s("return ");
370 std::string ts = TypeString(proto[0], typestr);
372 s += "(" + ts + "){";
375 if (proto.size() > 1)
376 a = (structTypes && proto[1] != 'l') ? "a.val" : "a";
377 b = structTypes ? "b.val" : "b";
378 c = structTypes ? "c.val" : "c";
391 s += a + " + ( " + b + " * " + c + " )";
394 s += a + " - ( " + b + " * " + c + " )";
397 s += "(__neon_" + ts + ")(" + a + " == " + b + ")";
400 s += "(__neon_" + ts + ")(" + a + " >= " + b + ")";
403 s += "(__neon_" + ts + ")(" + a + " <= " + b + ")";
406 s += "(__neon_" + ts + ")(" + a + " > " + b + ")";
409 s += "(__neon_" + ts + ")(" + a + " < " + b + ")";
433 s += "(__neon_" + ts + ")" + a;
436 throw "unknown OpKind!";
446 // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
447 // If structTypes is true, the NEON types are structs of vector types rather
448 // than vector types, and the call becomes __builtin_neon_cls(a.val)
449 static std::string GenBuiltin(const std::string &name, const std::string &proto,
450 StringRef typestr, ClassKind ck,
451 bool structTypes = true) {
455 if (proto[0] != 'v') {
456 // FIXME: if return type is 2/3/4, emit unioning code.
460 s += TypeString(proto[0], typestr);
465 s += "__builtin_neon_";
466 s += MangleName(name, typestr, ck);
469 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
471 if (structTypes && proto[i] != 's' && proto[i] != 'i' && proto[i] != 'l' &&
472 proto[i] != 'p' && proto[i] != 'c') {
480 if (proto[0] != 'v' && structTypes)
486 void NeonEmitter::run(raw_ostream &OS) {
487 EmitSourceFileHeader("ARM NEON Header", OS);
489 // FIXME: emit license into file?
491 OS << "#ifndef __ARM_NEON_H\n";
492 OS << "#define __ARM_NEON_H\n\n";
494 OS << "#ifndef __ARM_NEON__\n";
495 OS << "#error \"NEON support not enabled\"\n";
498 OS << "#include <stdint.h>\n\n";
500 // Emit NEON-specific scalar typedefs.
501 // FIXME: probably need to do something better for polynomial types.
502 // FIXME: is this the correct thing to do for float16?
503 OS << "typedef float float32_t;\n";
504 OS << "typedef uint8_t poly8_t;\n";
505 OS << "typedef uint16_t poly16_t;\n";
506 OS << "typedef uint16_t float16_t;\n";
508 // Emit Neon vector typedefs.
509 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
510 SmallVector<StringRef, 24> TDTypeVec;
511 ParseTypes(0, TypedefTypes, TDTypeVec);
513 // Emit vector typedefs.
514 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
515 bool dummy, quad = false;
516 (void) ClassifyType(TDTypeVec[i], quad, dummy, dummy);
517 OS << "typedef __attribute__(( __vector_size__(";
518 OS << (quad ? "16) )) " : "8) )) ");
519 OS << TypeString('s', TDTypeVec[i]);
521 OS << TypeString('d', TDTypeVec[i]) << ";\n";
525 // Emit struct typedefs.
526 for (unsigned vi = 1; vi != 5; ++vi) {
527 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
528 std::string ts = TypeString('d', TDTypeVec[i]);
529 std::string vs = (vi > 1) ? TypeString('0' + vi, TDTypeVec[i]) : ts;
530 OS << "typedef struct __" << vs << " {\n";
531 OS << " __neon_" << ts << " val";
533 OS << "[" << utostr(vi) << "]";
534 OS << ";\n} " << vs << ";\n\n";
538 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
540 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
542 StringMap<OpKind> OpMap;
543 OpMap["OP_NONE"] = OpNone;
544 OpMap["OP_ADD"] = OpAdd;
545 OpMap["OP_SUB"] = OpSub;
546 OpMap["OP_MUL"] = OpMul;
547 OpMap["OP_MLA"] = OpMla;
548 OpMap["OP_MLS"] = OpMls;
549 OpMap["OP_EQ"] = OpEq;
550 OpMap["OP_GE"] = OpGe;
551 OpMap["OP_LE"] = OpLe;
552 OpMap["OP_GT"] = OpGt;
553 OpMap["OP_LT"] = OpLt;
554 OpMap["OP_NEG"] = OpNeg;
555 OpMap["OP_NOT"] = OpNot;
556 OpMap["OP_AND"] = OpAnd;
557 OpMap["OP_OR"] = OpOr;
558 OpMap["OP_XOR"] = OpXor;
559 OpMap["OP_ANDN"] = OpAndNot;
560 OpMap["OP_ORN"] = OpOrNot;
561 OpMap["OP_CAST"] = OpCast;
563 DenseMap<Record*, ClassKind> ClassMap;
564 Record *SI = Records.getClass("SInst");
565 Record *II = Records.getClass("IInst");
566 Record *WI = Records.getClass("WInst");
567 Record *BI = Records.getClass("BInst");
568 ClassMap[SI] = ClassS;
569 ClassMap[II] = ClassI;
570 ClassMap[WI] = ClassW;
571 ClassMap[BI] = ClassB;
573 // Unique the return+pattern types, and assign them.
574 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
576 std::string name = LowercaseString(R->getName());
577 std::string Proto = R->getValueAsString("Prototype");
578 std::string Types = R->getValueAsString("Types");
580 SmallVector<StringRef, 16> TypeVec;
581 ParseTypes(R, Types, TypeVec);
583 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
585 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
586 assert(!Proto.empty() && "");
588 // static always inline + return type
589 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
591 // Function name with type suffix
592 OS << " " << MangleName(name, TypeVec[ti], ClassS);
594 // Function arguments
595 OS << GenArgs(Proto, TypeVec[ti]);
601 OS << GenOpString(k, Proto, TypeVec[ti]);
603 if (R->getSuperClasses().size() < 2)
604 throw TGError(R->getLoc(), "Builtin has no class kind");
606 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
609 throw TGError(R->getLoc(), "Builtin has no class kind");
610 OS << GenBuiltin(name, Proto, TypeVec[ti], ck);
619 // Unique the return+pattern types, and assign them to each record
620 // Emit a #define for each unique "type" of intrinsic declaring all variants.
621 // Emit a #define for each intrinsic mapping it to a particular type.
623 OS << "#endif /* __ARM_NEON_H */\n";
626 void NeonEmitter::runHeader(raw_ostream &OS) {