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/SmallString.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/StringMap.h"
48 static void ParseTypes(Record *r, std::string &s,
49 SmallVectorImpl<StringRef> &TV) {
50 const char *data = s.data();
53 for (unsigned i = 0, e = s.size(); i != e; ++i, ++len) {
54 if (data[len] == 'P' || data[len] == 'Q' || data[len] == 'U')
66 throw TGError(r->getLoc(),
67 "Unexpected letter: " + std::string(data + len, 1));
70 TV.push_back(StringRef(data, len + 1));
76 static char Widen(const char t) {
84 default: throw "unhandled type in widen!";
89 static char Narrow(const char t) {
97 default: throw "unhandled type in widen!";
102 static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) {
106 if (ty[off] == 'Q') {
112 if (ty[off] == 'P') {
117 // remember unsigned.
118 if (ty[off] == 'U') {
123 // base type to get the type string for.
127 static std::string TypeString(const char mod, StringRef typestr) {
135 // base type to get the type string for.
136 char type = ClassifyType(typestr, quad, poly, usgn);
138 // Based on the modifying character, change the type and width if necessary.
202 s += poly ? "poly8" : "int8";
205 s += quad ? "x16" : "x8";
208 s += poly ? "poly16" : "int16";
211 s += quad ? "x8" : "x4";
217 s += quad ? "x4" : "x2";
223 s += quad ? "x2" : "x1";
229 s += quad ? "x8" : "x4";
235 s += quad ? "x4" : "x2";
238 throw "unhandled type!";
249 // Append _t, finishing the type string typedef type.
261 // Turn "vst2_lane" into "vst2q_lane_f32", etc.
262 static std::string MangleName(const std::string &name, StringRef typestr) {
266 char type = ClassifyType(typestr, quad, poly, usgn);
268 std::string s = name;
272 s += poly ? "_p8" : usgn ? "_u8" : "_s8";
275 s += poly ? "_p16" : usgn ? "_u16" : "_s16";
278 s += usgn ? "_u32" : "_s32";
281 s += usgn ? "_u64" : "_s64";
290 throw "unhandled type!";
294 // Insert a 'q' before the first '_' character so that it ends up before
295 // _lane or _n on vector-scalar operations.
297 size_t pos = s.find('_');
298 s = s.insert(pos, "q");
303 // Generate the string "(argtype a, argtype b, ...)"
304 static std::string GenArgs(const std::string &proto, StringRef typestr) {
310 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
311 s += TypeString(proto[i], typestr);
322 // Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
323 // If structTypes is true, the NEON types are structs of vector types rather
324 // than vector types, and the call becomes "a.val + b.val"
325 static std::string GenOpString(OpKind op, const std::string &proto,
326 StringRef typestr, bool structTypes = true) {
327 std::string s("return ");
328 std::string ts = TypeString(proto[0], typestr);
330 s += "(" + ts + "){";
333 if (proto.size() > 1)
334 a = (structTypes && proto[1] != 'l') ? "a.val" : "a";
335 b = structTypes ? "b.val" : "b";
336 c = structTypes ? "c.val" : "c";
349 s += a + " + ( " + b + " * " + c + " )";
352 s += a + " - ( " + b + " * " + c + " )";
355 s += "(__neon_" + ts + ")(" + a + " == " + b + ")";
358 s += "(__neon_" + ts + ")(" + a + " >= " + b + ")";
361 s += "(__neon_" + ts + ")(" + a + " <= " + b + ")";
364 s += "(__neon_" + ts + ")(" + a + " > " + b + ")";
367 s += "(__neon_" + ts + ")(" + a + " < " + b + ")";
391 s += "(__neon_" + ts + ")" + a;
394 throw "unknown OpKind!";
404 // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
405 // If structTypes is true, the NEON types are structs of vector types rather
406 // than vector types, and the call becomes __builtin_neon_cls(a.val)
407 static std::string GenBuiltin(const std::string &name, const std::string &proto,
408 StringRef typestr, bool structTypes = true) {
412 if (proto[0] != 'v') {
413 // FIXME: if return type is 2/3/4, emit unioning code.
417 s += TypeString(proto[0], typestr);
422 s += "__builtin_neon_";
426 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
428 if (structTypes && proto[i] != 's' && proto[i] != 'i' && proto[i] != 'l' &&
429 proto[i] != 'p' && proto[i] != 'c') {
437 if (proto[0] != 'v' && structTypes)
443 void NeonEmitter::run(raw_ostream &OS) {
444 EmitSourceFileHeader("ARM NEON Header", OS);
446 // FIXME: emit license into file?
448 OS << "#ifndef __ARM_NEON_H\n";
449 OS << "#define __ARM_NEON_H\n\n";
451 OS << "#ifndef __ARM_NEON__\n";
452 OS << "#error \"NEON support not enabled\"\n";
455 OS << "#include <stdint.h>\n\n";
457 // Emit NEON-specific scalar typedefs.
458 // FIXME: probably need to do something better for polynomial types.
459 // FIXME: is this the correct thing to do for float16?
460 OS << "typedef float float32_t;\n";
461 OS << "typedef uint8_t poly8_t;\n";
462 OS << "typedef uint16_t poly16_t;\n";
463 OS << "typedef uint16_t float16_t;\n";
465 // Emit Neon vector typedefs.
466 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
467 SmallVector<StringRef, 24> TDTypeVec;
468 ParseTypes(0, TypedefTypes, TDTypeVec);
470 // Emit vector typedefs.
471 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
472 bool dummy, quad = false;
473 (void) ClassifyType(TDTypeVec[i], quad, dummy, dummy);
474 OS << "typedef __attribute__(( __vector_size__(";
475 OS << (quad ? "16) )) " : "8) )) ");
476 OS << TypeString('s', TDTypeVec[i]);
478 OS << TypeString('d', TDTypeVec[i]) << ";\n";
482 // Emit struct typedefs.
483 for (unsigned vi = 1; vi != 5; ++vi) {
484 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
485 std::string ts = TypeString('d', TDTypeVec[i]);
486 std::string vs = (vi > 1) ? TypeString('0' + vi, TDTypeVec[i]) : ts;
487 OS << "typedef struct __" << vs << " {\n";
488 OS << " __neon_" << ts << " val";
490 OS << "[" << utostr(vi) << "]";
491 OS << ";\n} " << vs << ";\n\n";
495 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
497 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
499 StringMap<OpKind> OpMap;
500 OpMap["OP_NONE"] = OpNone;
501 OpMap["OP_ADD"] = OpAdd;
502 OpMap["OP_SUB"] = OpSub;
503 OpMap["OP_MUL"] = OpMul;
504 OpMap["OP_MLA"] = OpMla;
505 OpMap["OP_MLS"] = OpMls;
506 OpMap["OP_EQ"] = OpEq;
507 OpMap["OP_GE"] = OpGe;
508 OpMap["OP_LE"] = OpLe;
509 OpMap["OP_GT"] = OpGt;
510 OpMap["OP_LT"] = OpLt;
511 OpMap["OP_NEG"] = OpNeg;
512 OpMap["OP_NOT"] = OpNot;
513 OpMap["OP_AND"] = OpAnd;
514 OpMap["OP_OR"] = OpOr;
515 OpMap["OP_XOR"] = OpXor;
516 OpMap["OP_ANDN"] = OpAndNot;
517 OpMap["OP_ORN"] = OpOrNot;
518 OpMap["OP_CAST"] = OpCast;
520 // Unique the return+pattern types, and assign them.
521 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
523 std::string name = LowercaseString(R->getName());
524 std::string Proto = R->getValueAsString("Prototype");
525 std::string Types = R->getValueAsString("Types");
527 SmallVector<StringRef, 16> TypeVec;
528 ParseTypes(R, Types, TypeVec);
530 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
532 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
533 assert(!Proto.empty() && "");
535 // static always inline + return type
536 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
538 // Function name with type suffix
539 OS << " " << MangleName(name, TypeVec[ti]);
541 // Function arguments
542 OS << GenArgs(Proto, TypeVec[ti]);
548 OS << GenOpString(k, Proto, TypeVec[ti]);
550 OS << GenBuiltin(name, Proto, TypeVec[ti]);
558 // Unique the return+pattern types, and assign them to each record
559 // Emit a #define for each unique "type" of intrinsic declaring all variants.
560 // Emit a #define for each intrinsic mapping it to a particular type.
562 OS << "#endif /* __ARM_NEON_H */\n";