Home | History | Annotate | Download | only in TableGen
      1 //===- NeonEmitter.cpp - Generate arm_neon.h for use with clang -*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     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.
     13 //
     14 // Each NEON instruction is implemented in terms of 1 or more functions which
     15 // are suffixed with the element type of the input vectors.  Functions may be
     16 // implemented in terms of generic vector operations such as +, *, -, etc. or
     17 // by calling a __builtin_-prefixed function which will be handled by clang's
     18 // CodeGen library.
     19 //
     20 // Additional validation code can be generated by this file when runHeader() is
     21 // called, rather than the normal run() entry point.  A complete set of tests
     22 // for Neon intrinsics can be generated by calling the runTests() entry point.
     23 //
     24 //===----------------------------------------------------------------------===//
     25 
     26 #include "NeonEmitter.h"
     27 #include "llvm/TableGen/Error.h"
     28 #include "llvm/ADT/SmallString.h"
     29 #include "llvm/ADT/SmallVector.h"
     30 #include "llvm/ADT/StringExtras.h"
     31 #include "llvm/Support/ErrorHandling.h"
     32 #include <string>
     33 
     34 using namespace llvm;
     35 
     36 /// ParseTypes - break down a string such as "fQf" into a vector of StringRefs,
     37 /// which each StringRef representing a single type declared in the string.
     38 /// for "fQf" we would end up with 2 StringRefs, "f", and "Qf", representing
     39 /// 2xfloat and 4xfloat respectively.
     40 static void ParseTypes(Record *r, std::string &s,
     41                        SmallVectorImpl<StringRef> &TV) {
     42   const char *data = s.data();
     43   int len = 0;
     44 
     45   for (unsigned i = 0, e = s.size(); i != e; ++i, ++len) {
     46     if (data[len] == 'P' || data[len] == 'Q' || data[len] == 'U')
     47       continue;
     48 
     49     switch (data[len]) {
     50       case 'c':
     51       case 's':
     52       case 'i':
     53       case 'l':
     54       case 'h':
     55       case 'f':
     56         break;
     57       default:
     58         throw TGError(r->getLoc(),
     59                       "Unexpected letter: " + std::string(data + len, 1));
     60     }
     61     TV.push_back(StringRef(data, len + 1));
     62     data += len + 1;
     63     len = -1;
     64   }
     65 }
     66 
     67 /// Widen - Convert a type code into the next wider type.  char -> short,
     68 /// short -> int, etc.
     69 static char Widen(const char t) {
     70   switch (t) {
     71     case 'c':
     72       return 's';
     73     case 's':
     74       return 'i';
     75     case 'i':
     76       return 'l';
     77     case 'h':
     78       return 'f';
     79     default: throw "unhandled type in widen!";
     80   }
     81 }
     82 
     83 /// Narrow - Convert a type code into the next smaller type.  short -> char,
     84 /// float -> half float, etc.
     85 static char Narrow(const char t) {
     86   switch (t) {
     87     case 's':
     88       return 'c';
     89     case 'i':
     90       return 's';
     91     case 'l':
     92       return 'i';
     93     case 'f':
     94       return 'h';
     95     default: throw "unhandled type in narrow!";
     96   }
     97 }
     98 
     99 /// For a particular StringRef, return the base type code, and whether it has
    100 /// the quad-vector, polynomial, or unsigned modifiers set.
    101 static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) {
    102   unsigned off = 0;
    103 
    104   // remember quad.
    105   if (ty[off] == 'Q') {
    106     quad = true;
    107     ++off;
    108   }
    109 
    110   // remember poly.
    111   if (ty[off] == 'P') {
    112     poly = true;
    113     ++off;
    114   }
    115 
    116   // remember unsigned.
    117   if (ty[off] == 'U') {
    118     usgn = true;
    119     ++off;
    120   }
    121 
    122   // base type to get the type string for.
    123   return ty[off];
    124 }
    125 
    126 /// ModType - Transform a type code and its modifiers based on a mod code. The
    127 /// mod code definitions may be found at the top of arm_neon.td.
    128 static char ModType(const char mod, char type, bool &quad, bool &poly,
    129                     bool &usgn, bool &scal, bool &cnst, bool &pntr) {
    130   switch (mod) {
    131     case 't':
    132       if (poly) {
    133         poly = false;
    134         usgn = true;
    135       }
    136       break;
    137     case 'u':
    138       usgn = true;
    139       poly = false;
    140       if (type == 'f')
    141         type = 'i';
    142       break;
    143     case 'x':
    144       usgn = false;
    145       poly = false;
    146       if (type == 'f')
    147         type = 'i';
    148       break;
    149     case 'f':
    150       if (type == 'h')
    151         quad = true;
    152       type = 'f';
    153       usgn = false;
    154       break;
    155     case 'g':
    156       quad = false;
    157       break;
    158     case 'w':
    159       type = Widen(type);
    160       quad = true;
    161       break;
    162     case 'n':
    163       type = Widen(type);
    164       break;
    165     case 'i':
    166       type = 'i';
    167       scal = true;
    168       break;
    169     case 'l':
    170       type = 'l';
    171       scal = true;
    172       usgn = true;
    173       break;
    174     case 's':
    175     case 'a':
    176       scal = true;
    177       break;
    178     case 'k':
    179       quad = true;
    180       break;
    181     case 'c':
    182       cnst = true;
    183     case 'p':
    184       pntr = true;
    185       scal = true;
    186       break;
    187     case 'h':
    188       type = Narrow(type);
    189       if (type == 'h')
    190         quad = false;
    191       break;
    192     case 'e':
    193       type = Narrow(type);
    194       usgn = true;
    195       break;
    196     default:
    197       break;
    198   }
    199   return type;
    200 }
    201 
    202 /// TypeString - for a modifier and type, generate the name of the typedef for
    203 /// that type.  QUc -> uint8x8_t.
    204 static std::string TypeString(const char mod, StringRef typestr) {
    205   bool quad = false;
    206   bool poly = false;
    207   bool usgn = false;
    208   bool scal = false;
    209   bool cnst = false;
    210   bool pntr = false;
    211 
    212   if (mod == 'v')
    213     return "void";
    214   if (mod == 'i')
    215     return "int";
    216 
    217   // base type to get the type string for.
    218   char type = ClassifyType(typestr, quad, poly, usgn);
    219 
    220   // Based on the modifying character, change the type and width if necessary.
    221   type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
    222 
    223   SmallString<128> s;
    224 
    225   if (usgn)
    226     s.push_back('u');
    227 
    228   switch (type) {
    229     case 'c':
    230       s += poly ? "poly8" : "int8";
    231       if (scal)
    232         break;
    233       s += quad ? "x16" : "x8";
    234       break;
    235     case 's':
    236       s += poly ? "poly16" : "int16";
    237       if (scal)
    238         break;
    239       s += quad ? "x8" : "x4";
    240       break;
    241     case 'i':
    242       s += "int32";
    243       if (scal)
    244         break;
    245       s += quad ? "x4" : "x2";
    246       break;
    247     case 'l':
    248       s += "int64";
    249       if (scal)
    250         break;
    251       s += quad ? "x2" : "x1";
    252       break;
    253     case 'h':
    254       s += "float16";
    255       if (scal)
    256         break;
    257       s += quad ? "x8" : "x4";
    258       break;
    259     case 'f':
    260       s += "float32";
    261       if (scal)
    262         break;
    263       s += quad ? "x4" : "x2";
    264       break;
    265     default:
    266       throw "unhandled type!";
    267   }
    268 
    269   if (mod == '2')
    270     s += "x2";
    271   if (mod == '3')
    272     s += "x3";
    273   if (mod == '4')
    274     s += "x4";
    275 
    276   // Append _t, finishing the type string typedef type.
    277   s += "_t";
    278 
    279   if (cnst)
    280     s += " const";
    281 
    282   if (pntr)
    283     s += " *";
    284 
    285   return s.str();
    286 }
    287 
    288 /// BuiltinTypeString - for a modifier and type, generate the clang
    289 /// BuiltinsARM.def prototype code for the function.  See the top of clang's
    290 /// Builtins.def for a description of the type strings.
    291 static std::string BuiltinTypeString(const char mod, StringRef typestr,
    292                                      ClassKind ck, bool ret) {
    293   bool quad = false;
    294   bool poly = false;
    295   bool usgn = false;
    296   bool scal = false;
    297   bool cnst = false;
    298   bool pntr = false;
    299 
    300   if (mod == 'v')
    301     return "v"; // void
    302   if (mod == 'i')
    303     return "i"; // int
    304 
    305   // base type to get the type string for.
    306   char type = ClassifyType(typestr, quad, poly, usgn);
    307 
    308   // Based on the modifying character, change the type and width if necessary.
    309   type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
    310 
    311   // All pointers are void* pointers.  Change type to 'v' now.
    312   if (pntr) {
    313     usgn = false;
    314     poly = false;
    315     type = 'v';
    316   }
    317   // Treat half-float ('h') types as unsigned short ('s') types.
    318   if (type == 'h') {
    319     type = 's';
    320     usgn = true;
    321   }
    322   usgn = usgn | poly | ((ck == ClassI || ck == ClassW) && scal && type != 'f');
    323 
    324   if (scal) {
    325     SmallString<128> s;
    326 
    327     if (usgn)
    328       s.push_back('U');
    329     else if (type == 'c')
    330       s.push_back('S'); // make chars explicitly signed
    331 
    332     if (type == 'l') // 64-bit long
    333       s += "LLi";
    334     else
    335       s.push_back(type);
    336 
    337     if (cnst)
    338       s.push_back('C');
    339     if (pntr)
    340       s.push_back('*');
    341     return s.str();
    342   }
    343 
    344   // Since the return value must be one type, return a vector type of the
    345   // appropriate width which we will bitcast.  An exception is made for
    346   // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
    347   // fashion, storing them to a pointer arg.
    348   if (ret) {
    349     if (mod >= '2' && mod <= '4')
    350       return "vv*"; // void result with void* first argument
    351     if (mod == 'f' || (ck != ClassB && type == 'f'))
    352       return quad ? "V4f" : "V2f";
    353     if (ck != ClassB && type == 's')
    354       return quad ? "V8s" : "V4s";
    355     if (ck != ClassB && type == 'i')
    356       return quad ? "V4i" : "V2i";
    357     if (ck != ClassB && type == 'l')
    358       return quad ? "V2LLi" : "V1LLi";
    359 
    360     return quad ? "V16Sc" : "V8Sc";
    361   }
    362 
    363   // Non-return array types are passed as individual vectors.
    364   if (mod == '2')
    365     return quad ? "V16ScV16Sc" : "V8ScV8Sc";
    366   if (mod == '3')
    367     return quad ? "V16ScV16ScV16Sc" : "V8ScV8ScV8Sc";
    368   if (mod == '4')
    369     return quad ? "V16ScV16ScV16ScV16Sc" : "V8ScV8ScV8ScV8Sc";
    370 
    371   if (mod == 'f' || (ck != ClassB && type == 'f'))
    372     return quad ? "V4f" : "V2f";
    373   if (ck != ClassB && type == 's')
    374     return quad ? "V8s" : "V4s";
    375   if (ck != ClassB && type == 'i')
    376     return quad ? "V4i" : "V2i";
    377   if (ck != ClassB && type == 'l')
    378     return quad ? "V2LLi" : "V1LLi";
    379 
    380   return quad ? "V16Sc" : "V8Sc";
    381 }
    382 
    383 /// MangleName - Append a type or width suffix to a base neon function name,
    384 /// and insert a 'q' in the appropriate location if the operation works on
    385 /// 128b rather than 64b.   E.g. turn "vst2_lane" into "vst2q_lane_f32", etc.
    386 static std::string MangleName(const std::string &name, StringRef typestr,
    387                               ClassKind ck) {
    388   if (name == "vcvt_f32_f16")
    389     return name;
    390 
    391   bool quad = false;
    392   bool poly = false;
    393   bool usgn = false;
    394   char type = ClassifyType(typestr, quad, poly, usgn);
    395 
    396   std::string s = name;
    397 
    398   switch (type) {
    399   case 'c':
    400     switch (ck) {
    401     case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
    402     case ClassI: s += "_i8"; break;
    403     case ClassW: s += "_8"; break;
    404     default: break;
    405     }
    406     break;
    407   case 's':
    408     switch (ck) {
    409     case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
    410     case ClassI: s += "_i16"; break;
    411     case ClassW: s += "_16"; break;
    412     default: break;
    413     }
    414     break;
    415   case 'i':
    416     switch (ck) {
    417     case ClassS: s += usgn ? "_u32" : "_s32"; break;
    418     case ClassI: s += "_i32"; break;
    419     case ClassW: s += "_32"; break;
    420     default: break;
    421     }
    422     break;
    423   case 'l':
    424     switch (ck) {
    425     case ClassS: s += usgn ? "_u64" : "_s64"; break;
    426     case ClassI: s += "_i64"; break;
    427     case ClassW: s += "_64"; break;
    428     default: break;
    429     }
    430     break;
    431   case 'h':
    432     switch (ck) {
    433     case ClassS:
    434     case ClassI: s += "_f16"; break;
    435     case ClassW: s += "_16"; break;
    436     default: break;
    437     }
    438     break;
    439   case 'f':
    440     switch (ck) {
    441     case ClassS:
    442     case ClassI: s += "_f32"; break;
    443     case ClassW: s += "_32"; break;
    444     default: break;
    445     }
    446     break;
    447   default:
    448     throw "unhandled type!";
    449   }
    450   if (ck == ClassB)
    451     s += "_v";
    452 
    453   // Insert a 'q' before the first '_' character so that it ends up before
    454   // _lane or _n on vector-scalar operations.
    455   if (quad) {
    456     size_t pos = s.find('_');
    457     s = s.insert(pos, "q");
    458   }
    459   return s;
    460 }
    461 
    462 /// UseMacro - Examine the prototype string to determine if the intrinsic
    463 /// should be defined as a preprocessor macro instead of an inline function.
    464 static bool UseMacro(const std::string &proto) {
    465   // If this builtin takes an immediate argument, we need to #define it rather
    466   // than use a standard declaration, so that SemaChecking can range check
    467   // the immediate passed by the user.
    468   if (proto.find('i') != std::string::npos)
    469     return true;
    470 
    471   // Pointer arguments need to use macros to avoid hiding aligned attributes
    472   // from the pointer type.
    473   if (proto.find('p') != std::string::npos ||
    474       proto.find('c') != std::string::npos)
    475     return true;
    476 
    477   return false;
    478 }
    479 
    480 /// MacroArgUsedDirectly - Return true if argument i for an intrinsic that is
    481 /// defined as a macro should be accessed directly instead of being first
    482 /// assigned to a local temporary.
    483 static bool MacroArgUsedDirectly(const std::string &proto, unsigned i) {
    484   // True for constant ints (i), pointers (p) and const pointers (c).
    485   return (proto[i] == 'i' || proto[i] == 'p' || proto[i] == 'c');
    486 }
    487 
    488 // Generate the string "(argtype a, argtype b, ...)"
    489 static std::string GenArgs(const std::string &proto, StringRef typestr) {
    490   bool define = UseMacro(proto);
    491   char arg = 'a';
    492 
    493   std::string s;
    494   s += "(";
    495 
    496   for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
    497     if (define) {
    498       // Some macro arguments are used directly instead of being assigned
    499       // to local temporaries; prepend an underscore prefix to make their
    500       // names consistent with the local temporaries.
    501       if (MacroArgUsedDirectly(proto, i))
    502         s += "__";
    503     } else {
    504       s += TypeString(proto[i], typestr) + " __";
    505     }
    506     s.push_back(arg);
    507     if ((i + 1) < e)
    508       s += ", ";
    509   }
    510 
    511   s += ")";
    512   return s;
    513 }
    514 
    515 // Macro arguments are not type-checked like inline function arguments, so
    516 // assign them to local temporaries to get the right type checking.
    517 static std::string GenMacroLocals(const std::string &proto, StringRef typestr) {
    518   char arg = 'a';
    519   std::string s;
    520   bool generatedLocal = false;
    521 
    522   for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
    523     // Do not create a temporary for an immediate argument.
    524     // That would defeat the whole point of using a macro!
    525     if (MacroArgUsedDirectly(proto, i))
    526       continue;
    527     generatedLocal = true;
    528 
    529     s += TypeString(proto[i], typestr) + " __";
    530     s.push_back(arg);
    531     s += " = (";
    532     s.push_back(arg);
    533     s += "); ";
    534   }
    535 
    536   if (generatedLocal)
    537     s += "\\\n  ";
    538   return s;
    539 }
    540 
    541 // Use the vmovl builtin to sign-extend or zero-extend a vector.
    542 static std::string Extend(StringRef typestr, const std::string &a) {
    543   std::string s;
    544   s = MangleName("vmovl", typestr, ClassS);
    545   s += "(" + a + ")";
    546   return s;
    547 }
    548 
    549 static std::string Duplicate(unsigned nElts, StringRef typestr,
    550                              const std::string &a) {
    551   std::string s;
    552 
    553   s = "(" + TypeString('d', typestr) + "){ ";
    554   for (unsigned i = 0; i != nElts; ++i) {
    555     s += a;
    556     if ((i + 1) < nElts)
    557       s += ", ";
    558   }
    559   s += " }";
    560 
    561   return s;
    562 }
    563 
    564 static std::string SplatLane(unsigned nElts, const std::string &vec,
    565                              const std::string &lane) {
    566   std::string s = "__builtin_shufflevector(" + vec + ", " + vec;
    567   for (unsigned i = 0; i < nElts; ++i)
    568     s += ", " + lane;
    569   s += ")";
    570   return s;
    571 }
    572 
    573 static unsigned GetNumElements(StringRef typestr, bool &quad) {
    574   quad = false;
    575   bool dummy = false;
    576   char type = ClassifyType(typestr, quad, dummy, dummy);
    577   unsigned nElts = 0;
    578   switch (type) {
    579   case 'c': nElts = 8; break;
    580   case 's': nElts = 4; break;
    581   case 'i': nElts = 2; break;
    582   case 'l': nElts = 1; break;
    583   case 'h': nElts = 4; break;
    584   case 'f': nElts = 2; break;
    585   default:
    586     throw "unhandled type!";
    587   }
    588   if (quad) nElts <<= 1;
    589   return nElts;
    590 }
    591 
    592 // Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
    593 static std::string GenOpString(OpKind op, const std::string &proto,
    594                                StringRef typestr) {
    595   bool quad;
    596   unsigned nElts = GetNumElements(typestr, quad);
    597   bool define = UseMacro(proto);
    598 
    599   std::string ts = TypeString(proto[0], typestr);
    600   std::string s;
    601   if (!define) {
    602     s = "return ";
    603   }
    604 
    605   switch(op) {
    606   case OpAdd:
    607     s += "__a + __b;";
    608     break;
    609   case OpAddl:
    610     s += Extend(typestr, "__a") + " + " + Extend(typestr, "__b") + ";";
    611     break;
    612   case OpAddw:
    613     s += "__a + " + Extend(typestr, "__b") + ";";
    614     break;
    615   case OpSub:
    616     s += "__a - __b;";
    617     break;
    618   case OpSubl:
    619     s += Extend(typestr, "__a") + " - " + Extend(typestr, "__b") + ";";
    620     break;
    621   case OpSubw:
    622     s += "__a - " + Extend(typestr, "__b") + ";";
    623     break;
    624   case OpMulN:
    625     s += "__a * " + Duplicate(nElts, typestr, "__b") + ";";
    626     break;
    627   case OpMulLane:
    628     s += "__a * " + SplatLane(nElts, "__b", "__c") + ";";
    629     break;
    630   case OpMul:
    631     s += "__a * __b;";
    632     break;
    633   case OpMullLane:
    634     s += MangleName("vmull", typestr, ClassS) + "(__a, " +
    635       SplatLane(nElts, "__b", "__c") + ");";
    636     break;
    637   case OpMlaN:
    638     s += "__a + (__b * " + Duplicate(nElts, typestr, "__c") + ");";
    639     break;
    640   case OpMlaLane:
    641     s += "__a + (__b * " + SplatLane(nElts, "__c", "__d") + ");";
    642     break;
    643   case OpMla:
    644     s += "__a + (__b * __c);";
    645     break;
    646   case OpMlalN:
    647     s += "__a + " + MangleName("vmull", typestr, ClassS) + "(__b, " +
    648       Duplicate(nElts, typestr, "__c") + ");";
    649     break;
    650   case OpMlalLane:
    651     s += "__a + " + MangleName("vmull", typestr, ClassS) + "(__b, " +
    652       SplatLane(nElts, "__c", "__d") + ");";
    653     break;
    654   case OpMlal:
    655     s += "__a + " + MangleName("vmull", typestr, ClassS) + "(__b, __c);";
    656     break;
    657   case OpMlsN:
    658     s += "__a - (__b * " + Duplicate(nElts, typestr, "__c") + ");";
    659     break;
    660   case OpMlsLane:
    661     s += "__a - (__b * " + SplatLane(nElts, "__c", "__d") + ");";
    662     break;
    663   case OpMls:
    664     s += "__a - (__b * __c);";
    665     break;
    666   case OpMlslN:
    667     s += "__a - " + MangleName("vmull", typestr, ClassS) + "(__b, " +
    668       Duplicate(nElts, typestr, "__c") + ");";
    669     break;
    670   case OpMlslLane:
    671     s += "__a - " + MangleName("vmull", typestr, ClassS) + "(__b, " +
    672       SplatLane(nElts, "__c", "__d") + ");";
    673     break;
    674   case OpMlsl:
    675     s += "__a - " + MangleName("vmull", typestr, ClassS) + "(__b, __c);";
    676     break;
    677   case OpQDMullLane:
    678     s += MangleName("vqdmull", typestr, ClassS) + "(__a, " +
    679       SplatLane(nElts, "__b", "__c") + ");";
    680     break;
    681   case OpQDMlalLane:
    682     s += MangleName("vqdmlal", typestr, ClassS) + "(__a, __b, " +
    683       SplatLane(nElts, "__c", "__d") + ");";
    684     break;
    685   case OpQDMlslLane:
    686     s += MangleName("vqdmlsl", typestr, ClassS) + "(__a, __b, " +
    687       SplatLane(nElts, "__c", "__d") + ");";
    688     break;
    689   case OpQDMulhLane:
    690     s += MangleName("vqdmulh", typestr, ClassS) + "(__a, " +
    691       SplatLane(nElts, "__b", "__c") + ");";
    692     break;
    693   case OpQRDMulhLane:
    694     s += MangleName("vqrdmulh", typestr, ClassS) + "(__a, " +
    695       SplatLane(nElts, "__b", "__c") + ");";
    696     break;
    697   case OpEq:
    698     s += "(" + ts + ")(__a == __b);";
    699     break;
    700   case OpGe:
    701     s += "(" + ts + ")(__a >= __b);";
    702     break;
    703   case OpLe:
    704     s += "(" + ts + ")(__a <= __b);";
    705     break;
    706   case OpGt:
    707     s += "(" + ts + ")(__a > __b);";
    708     break;
    709   case OpLt:
    710     s += "(" + ts + ")(__a < __b);";
    711     break;
    712   case OpNeg:
    713     s += " -__a;";
    714     break;
    715   case OpNot:
    716     s += " ~__a;";
    717     break;
    718   case OpAnd:
    719     s += "__a & __b;";
    720     break;
    721   case OpOr:
    722     s += "__a | __b;";
    723     break;
    724   case OpXor:
    725     s += "__a ^ __b;";
    726     break;
    727   case OpAndNot:
    728     s += "__a & ~__b;";
    729     break;
    730   case OpOrNot:
    731     s += "__a | ~__b;";
    732     break;
    733   case OpCast:
    734     s += "(" + ts + ")__a;";
    735     break;
    736   case OpConcat:
    737     s += "(" + ts + ")__builtin_shufflevector((int64x1_t)__a";
    738     s += ", (int64x1_t)__b, 0, 1);";
    739     break;
    740   case OpHi:
    741     s += "(" + ts +
    742       ")__builtin_shufflevector((int64x2_t)__a, (int64x2_t)__a, 1);";
    743     break;
    744   case OpLo:
    745     s += "(" + ts +
    746       ")__builtin_shufflevector((int64x2_t)__a, (int64x2_t)__a, 0);";
    747     break;
    748   case OpDup:
    749     s += Duplicate(nElts, typestr, "__a") + ";";
    750     break;
    751   case OpDupLane:
    752     s += SplatLane(nElts, "__a", "__b") + ";";
    753     break;
    754   case OpSelect:
    755     // ((0 & 1) | (~0 & 2))
    756     s += "(" + ts + ")";
    757     ts = TypeString(proto[1], typestr);
    758     s += "((__a & (" + ts + ")__b) | ";
    759     s += "(~__a & (" + ts + ")__c));";
    760     break;
    761   case OpRev16:
    762     s += "__builtin_shufflevector(__a, __a";
    763     for (unsigned i = 2; i <= nElts; i += 2)
    764       for (unsigned j = 0; j != 2; ++j)
    765         s += ", " + utostr(i - j - 1);
    766     s += ");";
    767     break;
    768   case OpRev32: {
    769     unsigned WordElts = nElts >> (1 + (int)quad);
    770     s += "__builtin_shufflevector(__a, __a";
    771     for (unsigned i = WordElts; i <= nElts; i += WordElts)
    772       for (unsigned j = 0; j != WordElts; ++j)
    773         s += ", " + utostr(i - j - 1);
    774     s += ");";
    775     break;
    776   }
    777   case OpRev64: {
    778     unsigned DblWordElts = nElts >> (int)quad;
    779     s += "__builtin_shufflevector(__a, __a";
    780     for (unsigned i = DblWordElts; i <= nElts; i += DblWordElts)
    781       for (unsigned j = 0; j != DblWordElts; ++j)
    782         s += ", " + utostr(i - j - 1);
    783     s += ");";
    784     break;
    785   }
    786   case OpAbdl: {
    787     std::string abd = MangleName("vabd", typestr, ClassS) + "(__a, __b)";
    788     if (typestr[0] != 'U') {
    789       // vabd results are always unsigned and must be zero-extended.
    790       std::string utype = "U" + typestr.str();
    791       s += "(" + TypeString(proto[0], typestr) + ")";
    792       abd = "(" + TypeString('d', utype) + ")" + abd;
    793       s += Extend(utype, abd) + ";";
    794     } else {
    795       s += Extend(typestr, abd) + ";";
    796     }
    797     break;
    798   }
    799   case OpAba:
    800     s += "__a + " + MangleName("vabd", typestr, ClassS) + "(__b, __c);";
    801     break;
    802   case OpAbal: {
    803     s += "__a + ";
    804     std::string abd = MangleName("vabd", typestr, ClassS) + "(__b, __c)";
    805     if (typestr[0] != 'U') {
    806       // vabd results are always unsigned and must be zero-extended.
    807       std::string utype = "U" + typestr.str();
    808       s += "(" + TypeString(proto[0], typestr) + ")";
    809       abd = "(" + TypeString('d', utype) + ")" + abd;
    810       s += Extend(utype, abd) + ";";
    811     } else {
    812       s += Extend(typestr, abd) + ";";
    813     }
    814     break;
    815   }
    816   default:
    817     throw "unknown OpKind!";
    818   }
    819   return s;
    820 }
    821 
    822 static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
    823   unsigned mod = proto[0];
    824 
    825   if (mod == 'v' || mod == 'f')
    826     mod = proto[1];
    827 
    828   bool quad = false;
    829   bool poly = false;
    830   bool usgn = false;
    831   bool scal = false;
    832   bool cnst = false;
    833   bool pntr = false;
    834 
    835   // Base type to get the type string for.
    836   char type = ClassifyType(typestr, quad, poly, usgn);
    837 
    838   // Based on the modifying character, change the type and width if necessary.
    839   type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
    840 
    841   NeonTypeFlags::EltType ET;
    842   switch (type) {
    843     case 'c':
    844       ET = poly ? NeonTypeFlags::Poly8 : NeonTypeFlags::Int8;
    845       break;
    846     case 's':
    847       ET = poly ? NeonTypeFlags::Poly16 : NeonTypeFlags::Int16;
    848       break;
    849     case 'i':
    850       ET = NeonTypeFlags::Int32;
    851       break;
    852     case 'l':
    853       ET = NeonTypeFlags::Int64;
    854       break;
    855     case 'h':
    856       ET = NeonTypeFlags::Float16;
    857       break;
    858     case 'f':
    859       ET = NeonTypeFlags::Float32;
    860       break;
    861     default:
    862       throw "unhandled type!";
    863   }
    864   NeonTypeFlags Flags(ET, usgn, quad && proto[1] != 'g');
    865   return Flags.getFlags();
    866 }
    867 
    868 // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
    869 static std::string GenBuiltin(const std::string &name, const std::string &proto,
    870                               StringRef typestr, ClassKind ck) {
    871   std::string s;
    872 
    873   // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
    874   // sret-like argument.
    875   bool sret = (proto[0] >= '2' && proto[0] <= '4');
    876 
    877   bool define = UseMacro(proto);
    878 
    879   // Check if the prototype has a scalar operand with the type of the vector
    880   // elements.  If not, bitcasting the args will take care of arg checking.
    881   // The actual signedness etc. will be taken care of with special enums.
    882   if (proto.find('s') == std::string::npos)
    883     ck = ClassB;
    884 
    885   if (proto[0] != 'v') {
    886     std::string ts = TypeString(proto[0], typestr);
    887 
    888     if (define) {
    889       if (sret)
    890         s += ts + " r; ";
    891       else
    892         s += "(" + ts + ")";
    893     } else if (sret) {
    894       s += ts + " r; ";
    895     } else {
    896       s += "return (" + ts + ")";
    897     }
    898   }
    899 
    900   bool splat = proto.find('a') != std::string::npos;
    901 
    902   s += "__builtin_neon_";
    903   if (splat) {
    904     // Call the non-splat builtin: chop off the "_n" suffix from the name.
    905     std::string vname(name, 0, name.size()-2);
    906     s += MangleName(vname, typestr, ck);
    907   } else {
    908     s += MangleName(name, typestr, ck);
    909   }
    910   s += "(";
    911 
    912   // Pass the address of the return variable as the first argument to sret-like
    913   // builtins.
    914   if (sret)
    915     s += "&r, ";
    916 
    917   char arg = 'a';
    918   for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
    919     std::string args = std::string(&arg, 1);
    920 
    921     // Use the local temporaries instead of the macro arguments.
    922     args = "__" + args;
    923 
    924     bool argQuad = false;
    925     bool argPoly = false;
    926     bool argUsgn = false;
    927     bool argScalar = false;
    928     bool dummy = false;
    929     char argType = ClassifyType(typestr, argQuad, argPoly, argUsgn);
    930     argType = ModType(proto[i], argType, argQuad, argPoly, argUsgn, argScalar,
    931                       dummy, dummy);
    932 
    933     // Handle multiple-vector values specially, emitting each subvector as an
    934     // argument to the __builtin.
    935     if (proto[i] >= '2' && proto[i] <= '4') {
    936       // Check if an explicit cast is needed.
    937       if (argType != 'c' || argPoly || argUsgn)
    938         args = (argQuad ? "(int8x16_t)" : "(int8x8_t)") + args;
    939 
    940       for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
    941         s += args + ".val[" + utostr(vi) + "]";
    942         if ((vi + 1) < ve)
    943           s += ", ";
    944       }
    945       if ((i + 1) < e)
    946         s += ", ";
    947 
    948       continue;
    949     }
    950 
    951     if (splat && (i + 1) == e)
    952       args = Duplicate(GetNumElements(typestr, argQuad), typestr, args);
    953 
    954     // Check if an explicit cast is needed.
    955     if ((splat || !argScalar) &&
    956         ((ck == ClassB && argType != 'c') || argPoly || argUsgn)) {
    957       std::string argTypeStr = "c";
    958       if (ck != ClassB)
    959         argTypeStr = argType;
    960       if (argQuad)
    961         argTypeStr = "Q" + argTypeStr;
    962       args = "(" + TypeString('d', argTypeStr) + ")" + args;
    963     }
    964 
    965     s += args;
    966     if ((i + 1) < e)
    967       s += ", ";
    968   }
    969 
    970   // Extra constant integer to hold type class enum for this function, e.g. s8
    971   if (ck == ClassB)
    972     s += ", " + utostr(GetNeonEnum(proto, typestr));
    973 
    974   s += ");";
    975 
    976   if (proto[0] != 'v' && sret) {
    977     if (define)
    978       s += " r;";
    979     else
    980       s += " return r;";
    981   }
    982   return s;
    983 }
    984 
    985 static std::string GenBuiltinDef(const std::string &name,
    986                                  const std::string &proto,
    987                                  StringRef typestr, ClassKind ck) {
    988   std::string s("BUILTIN(__builtin_neon_");
    989 
    990   // If all types are the same size, bitcasting the args will take care
    991   // of arg checking.  The actual signedness etc. will be taken care of with
    992   // special enums.
    993   if (proto.find('s') == std::string::npos)
    994     ck = ClassB;
    995 
    996   s += MangleName(name, typestr, ck);
    997   s += ", \"";
    998 
    999   for (unsigned i = 0, e = proto.size(); i != e; ++i)
   1000     s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
   1001 
   1002   // Extra constant integer to hold type class enum for this function, e.g. s8
   1003   if (ck == ClassB)
   1004     s += "i";
   1005 
   1006   s += "\", \"n\")";
   1007   return s;
   1008 }
   1009 
   1010 static std::string GenIntrinsic(const std::string &name,
   1011                                 const std::string &proto,
   1012                                 StringRef outTypeStr, StringRef inTypeStr,
   1013                                 OpKind kind, ClassKind classKind) {
   1014   assert(!proto.empty() && "");
   1015   bool define = UseMacro(proto);
   1016   std::string s;
   1017 
   1018   // static always inline + return type
   1019   if (define)
   1020     s += "#define ";
   1021   else
   1022     s += "__ai " + TypeString(proto[0], outTypeStr) + " ";
   1023 
   1024   // Function name with type suffix
   1025   std::string mangledName = MangleName(name, outTypeStr, ClassS);
   1026   if (outTypeStr != inTypeStr) {
   1027     // If the input type is different (e.g., for vreinterpret), append a suffix
   1028     // for the input type.  String off a "Q" (quad) prefix so that MangleName
   1029     // does not insert another "q" in the name.
   1030     unsigned typeStrOff = (inTypeStr[0] == 'Q' ? 1 : 0);
   1031     StringRef inTypeNoQuad = inTypeStr.substr(typeStrOff);
   1032     mangledName = MangleName(mangledName, inTypeNoQuad, ClassS);
   1033   }
   1034   s += mangledName;
   1035 
   1036   // Function arguments
   1037   s += GenArgs(proto, inTypeStr);
   1038 
   1039   // Definition.
   1040   if (define) {
   1041     s += " __extension__ ({ \\\n  ";
   1042     s += GenMacroLocals(proto, inTypeStr);
   1043   } else {
   1044     s += " { \\\n  ";
   1045   }
   1046 
   1047   if (kind != OpNone)
   1048     s += GenOpString(kind, proto, outTypeStr);
   1049   else
   1050     s += GenBuiltin(name, proto, outTypeStr, classKind);
   1051   if (define)
   1052     s += " })";
   1053   else
   1054     s += " }";
   1055   s += "\n";
   1056   return s;
   1057 }
   1058 
   1059 /// run - Read the records in arm_neon.td and output arm_neon.h.  arm_neon.h
   1060 /// is comprised of type definitions and function declarations.
   1061 void NeonEmitter::run(raw_ostream &OS) {
   1062   OS <<
   1063     "/*===---- arm_neon.h - ARM Neon intrinsics ------------------------------"
   1064     "---===\n"
   1065     " *\n"
   1066     " * Permission is hereby granted, free of charge, to any person obtaining "
   1067     "a copy\n"
   1068     " * of this software and associated documentation files (the \"Software\"),"
   1069     " to deal\n"
   1070     " * in the Software without restriction, including without limitation the "
   1071     "rights\n"
   1072     " * to use, copy, modify, merge, publish, distribute, sublicense, "
   1073     "and/or sell\n"
   1074     " * copies of the Software, and to permit persons to whom the Software is\n"
   1075     " * furnished to do so, subject to the following conditions:\n"
   1076     " *\n"
   1077     " * The above copyright notice and this permission notice shall be "
   1078     "included in\n"
   1079     " * all copies or substantial portions of the Software.\n"
   1080     " *\n"
   1081     " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, "
   1082     "EXPRESS OR\n"
   1083     " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
   1084     "MERCHANTABILITY,\n"
   1085     " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT "
   1086     "SHALL THE\n"
   1087     " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR "
   1088     "OTHER\n"
   1089     " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, "
   1090     "ARISING FROM,\n"
   1091     " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER "
   1092     "DEALINGS IN\n"
   1093     " * THE SOFTWARE.\n"
   1094     " *\n"
   1095     " *===--------------------------------------------------------------------"
   1096     "---===\n"
   1097     " */\n\n";
   1098 
   1099   OS << "#ifndef __ARM_NEON_H\n";
   1100   OS << "#define __ARM_NEON_H\n\n";
   1101 
   1102   OS << "#ifndef __ARM_NEON__\n";
   1103   OS << "#error \"NEON support not enabled\"\n";
   1104   OS << "#endif\n\n";
   1105 
   1106   OS << "#include <stdint.h>\n\n";
   1107 
   1108   // Emit NEON-specific scalar typedefs.
   1109   OS << "typedef float float32_t;\n";
   1110   OS << "typedef int8_t poly8_t;\n";
   1111   OS << "typedef int16_t poly16_t;\n";
   1112   OS << "typedef uint16_t float16_t;\n";
   1113 
   1114   // Emit Neon vector typedefs.
   1115   std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
   1116   SmallVector<StringRef, 24> TDTypeVec;
   1117   ParseTypes(0, TypedefTypes, TDTypeVec);
   1118 
   1119   // Emit vector typedefs.
   1120   for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
   1121     bool dummy, quad = false, poly = false;
   1122     (void) ClassifyType(TDTypeVec[i], quad, poly, dummy);
   1123     if (poly)
   1124       OS << "typedef __attribute__((neon_polyvector_type(";
   1125     else
   1126       OS << "typedef __attribute__((neon_vector_type(";
   1127 
   1128     unsigned nElts = GetNumElements(TDTypeVec[i], quad);
   1129     OS << utostr(nElts) << "))) ";
   1130     if (nElts < 10)
   1131       OS << " ";
   1132 
   1133     OS << TypeString('s', TDTypeVec[i]);
   1134     OS << " " << TypeString('d', TDTypeVec[i]) << ";\n";
   1135   }
   1136   OS << "\n";
   1137 
   1138   // Emit struct typedefs.
   1139   for (unsigned vi = 2; vi != 5; ++vi) {
   1140     for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
   1141       std::string ts = TypeString('d', TDTypeVec[i]);
   1142       std::string vs = TypeString('0' + vi, TDTypeVec[i]);
   1143       OS << "typedef struct " << vs << " {\n";
   1144       OS << "  " << ts << " val";
   1145       OS << "[" << utostr(vi) << "]";
   1146       OS << ";\n} ";
   1147       OS << vs << ";\n\n";
   1148     }
   1149   }
   1150 
   1151   OS<<"#define __ai static __attribute__((__always_inline__, __nodebug__))\n\n";
   1152 
   1153   std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
   1154 
   1155   // Emit vmovl, vmull and vabd intrinsics first so they can be used by other
   1156   // intrinsics.  (Some of the saturating multiply instructions are also
   1157   // used to implement the corresponding "_lane" variants, but tablegen
   1158   // sorts the records into alphabetical order so that the "_lane" variants
   1159   // come after the intrinsics they use.)
   1160   emitIntrinsic(OS, Records.getDef("VMOVL"));
   1161   emitIntrinsic(OS, Records.getDef("VMULL"));
   1162   emitIntrinsic(OS, Records.getDef("VABD"));
   1163 
   1164   for (unsigned i = 0, e = RV.size(); i != e; ++i) {
   1165     Record *R = RV[i];
   1166     if (R->getName() != "VMOVL" &&
   1167         R->getName() != "VMULL" &&
   1168         R->getName() != "VABD")
   1169       emitIntrinsic(OS, R);
   1170   }
   1171 
   1172   OS << "#undef __ai\n\n";
   1173   OS << "#endif /* __ARM_NEON_H */\n";
   1174 }
   1175 
   1176 /// emitIntrinsic - Write out the arm_neon.h header file definitions for the
   1177 /// intrinsics specified by record R.
   1178 void NeonEmitter::emitIntrinsic(raw_ostream &OS, Record *R) {
   1179   std::string name = R->getValueAsString("Name");
   1180   std::string Proto = R->getValueAsString("Prototype");
   1181   std::string Types = R->getValueAsString("Types");
   1182 
   1183   SmallVector<StringRef, 16> TypeVec;
   1184   ParseTypes(R, Types, TypeVec);
   1185 
   1186   OpKind kind = OpMap[R->getValueAsDef("Operand")->getName()];
   1187 
   1188   ClassKind classKind = ClassNone;
   1189   if (R->getSuperClasses().size() >= 2)
   1190     classKind = ClassMap[R->getSuperClasses()[1]];
   1191   if (classKind == ClassNone && kind == OpNone)
   1192     throw TGError(R->getLoc(), "Builtin has no class kind");
   1193 
   1194   for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
   1195     if (kind == OpReinterpret) {
   1196       bool outQuad = false;
   1197       bool dummy = false;
   1198       (void)ClassifyType(TypeVec[ti], outQuad, dummy, dummy);
   1199       for (unsigned srcti = 0, srcte = TypeVec.size();
   1200            srcti != srcte; ++srcti) {
   1201         bool inQuad = false;
   1202         (void)ClassifyType(TypeVec[srcti], inQuad, dummy, dummy);
   1203         if (srcti == ti || inQuad != outQuad)
   1204           continue;
   1205         OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[srcti],
   1206                            OpCast, ClassS);
   1207       }
   1208     } else {
   1209       OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[ti],
   1210                          kind, classKind);
   1211     }
   1212   }
   1213   OS << "\n";
   1214 }
   1215 
   1216 static unsigned RangeFromType(const char mod, StringRef typestr) {
   1217   // base type to get the type string for.
   1218   bool quad = false, dummy = false;
   1219   char type = ClassifyType(typestr, quad, dummy, dummy);
   1220   type = ModType(mod, type, quad, dummy, dummy, dummy, dummy, dummy);
   1221 
   1222   switch (type) {
   1223     case 'c':
   1224       return (8 << (int)quad) - 1;
   1225     case 'h':
   1226     case 's':
   1227       return (4 << (int)quad) - 1;
   1228     case 'f':
   1229     case 'i':
   1230       return (2 << (int)quad) - 1;
   1231     case 'l':
   1232       return (1 << (int)quad) - 1;
   1233     default:
   1234       throw "unhandled type!";
   1235   }
   1236 }
   1237 
   1238 /// runHeader - Emit a file with sections defining:
   1239 /// 1. the NEON section of BuiltinsARM.def.
   1240 /// 2. the SemaChecking code for the type overload checking.
   1241 /// 3. the SemaChecking code for validation of intrinsic immedate arguments.
   1242 void NeonEmitter::runHeader(raw_ostream &OS) {
   1243   std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
   1244 
   1245   StringMap<OpKind> EmittedMap;
   1246 
   1247   // Generate BuiltinsARM.def for NEON
   1248   OS << "#ifdef GET_NEON_BUILTINS\n";
   1249   for (unsigned i = 0, e = RV.size(); i != e; ++i) {
   1250     Record *R = RV[i];
   1251     OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
   1252     if (k != OpNone)
   1253       continue;
   1254 
   1255     std::string Proto = R->getValueAsString("Prototype");
   1256 
   1257     // Functions with 'a' (the splat code) in the type prototype should not get
   1258     // their own builtin as they use the non-splat variant.
   1259     if (Proto.find('a') != std::string::npos)
   1260       continue;
   1261 
   1262     std::string Types = R->getValueAsString("Types");
   1263     SmallVector<StringRef, 16> TypeVec;
   1264     ParseTypes(R, Types, TypeVec);
   1265 
   1266     if (R->getSuperClasses().size() < 2)
   1267       throw TGError(R->getLoc(), "Builtin has no class kind");
   1268 
   1269     std::string name = R->getValueAsString("Name");
   1270     ClassKind ck = ClassMap[R->getSuperClasses()[1]];
   1271 
   1272     for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
   1273       // Generate the BuiltinsARM.def declaration for this builtin, ensuring
   1274       // that each unique BUILTIN() macro appears only once in the output
   1275       // stream.
   1276       std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
   1277       if (EmittedMap.count(bd))
   1278         continue;
   1279 
   1280       EmittedMap[bd] = OpNone;
   1281       OS << bd << "\n";
   1282     }
   1283   }
   1284   OS << "#endif\n\n";
   1285 
   1286   // Generate the overloaded type checking code for SemaChecking.cpp
   1287   OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
   1288   for (unsigned i = 0, e = RV.size(); i != e; ++i) {
   1289     Record *R = RV[i];
   1290     OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
   1291     if (k != OpNone)
   1292       continue;
   1293 
   1294     std::string Proto = R->getValueAsString("Prototype");
   1295     std::string Types = R->getValueAsString("Types");
   1296     std::string name = R->getValueAsString("Name");
   1297 
   1298     // Functions with 'a' (the splat code) in the type prototype should not get
   1299     // their own builtin as they use the non-splat variant.
   1300     if (Proto.find('a') != std::string::npos)
   1301       continue;
   1302 
   1303     // Functions which have a scalar argument cannot be overloaded, no need to
   1304     // check them if we are emitting the type checking code.
   1305     if (Proto.find('s') != std::string::npos)
   1306       continue;
   1307 
   1308     SmallVector<StringRef, 16> TypeVec;
   1309     ParseTypes(R, Types, TypeVec);
   1310 
   1311     if (R->getSuperClasses().size() < 2)
   1312       throw TGError(R->getLoc(), "Builtin has no class kind");
   1313 
   1314     int si = -1, qi = -1;
   1315     unsigned mask = 0, qmask = 0;
   1316     for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
   1317       // Generate the switch case(s) for this builtin for the type validation.
   1318       bool quad = false, poly = false, usgn = false;
   1319       (void) ClassifyType(TypeVec[ti], quad, poly, usgn);
   1320 
   1321       if (quad) {
   1322         qi = ti;
   1323         qmask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
   1324       } else {
   1325         si = ti;
   1326         mask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
   1327       }
   1328     }
   1329 
   1330     // Check if the builtin function has a pointer or const pointer argument.
   1331     int PtrArgNum = -1;
   1332     bool HasConstPtr = false;
   1333     for (unsigned arg = 1, arge = Proto.size(); arg != arge; ++arg) {
   1334       char ArgType = Proto[arg];
   1335       if (ArgType == 'c') {
   1336         HasConstPtr = true;
   1337         PtrArgNum = arg - 1;
   1338         break;
   1339       }
   1340       if (ArgType == 'p') {
   1341         PtrArgNum = arg - 1;
   1342         break;
   1343       }
   1344     }
   1345     // For sret builtins, adjust the pointer argument index.
   1346     if (PtrArgNum >= 0 && (Proto[0] >= '2' && Proto[0] <= '4'))
   1347       PtrArgNum += 1;
   1348 
   1349     // Omit type checking for the pointer arguments of vld1_lane, vld1_dup,
   1350     // and vst1_lane intrinsics.  Using a pointer to the vector element
   1351     // type with one of those operations causes codegen to select an aligned
   1352     // load/store instruction.  If you want an unaligned operation,
   1353     // the pointer argument needs to have less alignment than element type,
   1354     // so just accept any pointer type.
   1355     if (name == "vld1_lane" || name == "vld1_dup" || name == "vst1_lane") {
   1356       PtrArgNum = -1;
   1357       HasConstPtr = false;
   1358     }
   1359 
   1360     if (mask) {
   1361       OS << "case ARM::BI__builtin_neon_"
   1362          << MangleName(name, TypeVec[si], ClassB)
   1363          << ": mask = " << "0x" << utohexstr(mask);
   1364       if (PtrArgNum >= 0)
   1365         OS << "; PtrArgNum = " << PtrArgNum;
   1366       if (HasConstPtr)
   1367         OS << "; HasConstPtr = true";
   1368       OS << "; break;\n";
   1369     }
   1370     if (qmask) {
   1371       OS << "case ARM::BI__builtin_neon_"
   1372          << MangleName(name, TypeVec[qi], ClassB)
   1373          << ": mask = " << "0x" << utohexstr(qmask);
   1374       if (PtrArgNum >= 0)
   1375         OS << "; PtrArgNum = " << PtrArgNum;
   1376       if (HasConstPtr)
   1377         OS << "; HasConstPtr = true";
   1378       OS << "; break;\n";
   1379     }
   1380   }
   1381   OS << "#endif\n\n";
   1382 
   1383   // Generate the intrinsic range checking code for shift/lane immediates.
   1384   OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
   1385   for (unsigned i = 0, e = RV.size(); i != e; ++i) {
   1386     Record *R = RV[i];
   1387 
   1388     OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
   1389     if (k != OpNone)
   1390       continue;
   1391 
   1392     std::string name = R->getValueAsString("Name");
   1393     std::string Proto = R->getValueAsString("Prototype");
   1394     std::string Types = R->getValueAsString("Types");
   1395 
   1396     // Functions with 'a' (the splat code) in the type prototype should not get
   1397     // their own builtin as they use the non-splat variant.
   1398     if (Proto.find('a') != std::string::npos)
   1399       continue;
   1400 
   1401     // Functions which do not have an immediate do not need to have range
   1402     // checking code emitted.
   1403     size_t immPos = Proto.find('i');
   1404     if (immPos == std::string::npos)
   1405       continue;
   1406 
   1407     SmallVector<StringRef, 16> TypeVec;
   1408     ParseTypes(R, Types, TypeVec);
   1409 
   1410     if (R->getSuperClasses().size() < 2)
   1411       throw TGError(R->getLoc(), "Builtin has no class kind");
   1412 
   1413     ClassKind ck = ClassMap[R->getSuperClasses()[1]];
   1414 
   1415     for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
   1416       std::string namestr, shiftstr, rangestr;
   1417 
   1418       if (R->getValueAsBit("isVCVT_N")) {
   1419         // VCVT between floating- and fixed-point values takes an immediate
   1420         // in the range 1 to 32.
   1421         ck = ClassB;
   1422         rangestr = "l = 1; u = 31"; // upper bound = l + u
   1423       } else if (Proto.find('s') == std::string::npos) {
   1424         // Builtins which are overloaded by type will need to have their upper
   1425         // bound computed at Sema time based on the type constant.
   1426         ck = ClassB;
   1427         if (R->getValueAsBit("isShift")) {
   1428           shiftstr = ", true";
   1429 
   1430           // Right shifts have an 'r' in the name, left shifts do not.
   1431           if (name.find('r') != std::string::npos)
   1432             rangestr = "l = 1; ";
   1433         }
   1434         rangestr += "u = RFT(TV" + shiftstr + ")";
   1435       } else {
   1436         // The immediate generally refers to a lane in the preceding argument.
   1437         assert(immPos > 0 && "unexpected immediate operand");
   1438         rangestr = "u = " + utostr(RangeFromType(Proto[immPos-1], TypeVec[ti]));
   1439       }
   1440       // Make sure cases appear only once by uniquing them in a string map.
   1441       namestr = MangleName(name, TypeVec[ti], ck);
   1442       if (EmittedMap.count(namestr))
   1443         continue;
   1444       EmittedMap[namestr] = OpNone;
   1445 
   1446       // Calculate the index of the immediate that should be range checked.
   1447       unsigned immidx = 0;
   1448 
   1449       // Builtins that return a struct of multiple vectors have an extra
   1450       // leading arg for the struct return.
   1451       if (Proto[0] >= '2' && Proto[0] <= '4')
   1452         ++immidx;
   1453 
   1454       // Add one to the index for each argument until we reach the immediate
   1455       // to be checked.  Structs of vectors are passed as multiple arguments.
   1456       for (unsigned ii = 1, ie = Proto.size(); ii != ie; ++ii) {
   1457         switch (Proto[ii]) {
   1458           default:  immidx += 1; break;
   1459           case '2': immidx += 2; break;
   1460           case '3': immidx += 3; break;
   1461           case '4': immidx += 4; break;
   1462           case 'i': ie = ii + 1; break;
   1463         }
   1464       }
   1465       OS << "case ARM::BI__builtin_neon_" << MangleName(name, TypeVec[ti], ck)
   1466          << ": i = " << immidx << "; " << rangestr << "; break;\n";
   1467     }
   1468   }
   1469   OS << "#endif\n\n";
   1470 }
   1471 
   1472 /// GenTest - Write out a test for the intrinsic specified by the name and
   1473 /// type strings, including the embedded patterns for FileCheck to match.
   1474 static std::string GenTest(const std::string &name,
   1475                            const std::string &proto,
   1476                            StringRef outTypeStr, StringRef inTypeStr,
   1477                            bool isShift) {
   1478   assert(!proto.empty() && "");
   1479   std::string s;
   1480 
   1481   // Function name with type suffix
   1482   std::string mangledName = MangleName(name, outTypeStr, ClassS);
   1483   if (outTypeStr != inTypeStr) {
   1484     // If the input type is different (e.g., for vreinterpret), append a suffix
   1485     // for the input type.  String off a "Q" (quad) prefix so that MangleName
   1486     // does not insert another "q" in the name.
   1487     unsigned typeStrOff = (inTypeStr[0] == 'Q' ? 1 : 0);
   1488     StringRef inTypeNoQuad = inTypeStr.substr(typeStrOff);
   1489     mangledName = MangleName(mangledName, inTypeNoQuad, ClassS);
   1490   }
   1491 
   1492   // Emit the FileCheck patterns.
   1493   s += "// CHECK: test_" + mangledName + "\n";
   1494   // s += "// CHECK: \n"; // FIXME: + expected instruction opcode.
   1495 
   1496   // Emit the start of the test function.
   1497   s += TypeString(proto[0], outTypeStr) + " test_" + mangledName + "(";
   1498   char arg = 'a';
   1499   std::string comma;
   1500   for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
   1501     // Do not create arguments for values that must be immediate constants.
   1502     if (proto[i] == 'i')
   1503       continue;
   1504     s += comma + TypeString(proto[i], inTypeStr) + " ";
   1505     s.push_back(arg);
   1506     comma = ", ";
   1507   }
   1508   s += ") { \\\n  ";
   1509 
   1510   if (proto[0] != 'v')
   1511     s += "return ";
   1512   s += mangledName + "(";
   1513   arg = 'a';
   1514   for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
   1515     if (proto[i] == 'i') {
   1516       // For immediate operands, test the maximum value.
   1517       if (isShift)
   1518         s += "1"; // FIXME
   1519       else
   1520         // The immediate generally refers to a lane in the preceding argument.
   1521         s += utostr(RangeFromType(proto[i-1], inTypeStr));
   1522     } else {
   1523       s.push_back(arg);
   1524     }
   1525     if ((i + 1) < e)
   1526       s += ", ";
   1527   }
   1528   s += ");\n}\n\n";
   1529   return s;
   1530 }
   1531 
   1532 /// runTests - Write out a complete set of tests for all of the Neon
   1533 /// intrinsics.
   1534 void NeonEmitter::runTests(raw_ostream &OS) {
   1535   OS <<
   1536     "// RUN: %clang_cc1 -triple thumbv7-apple-darwin \\\n"
   1537     "// RUN:  -target-cpu cortex-a9 -ffreestanding -S -o - %s | FileCheck %s\n"
   1538     "\n"
   1539     "#include <arm_neon.h>\n"
   1540     "\n";
   1541 
   1542   std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
   1543   for (unsigned i = 0, e = RV.size(); i != e; ++i) {
   1544     Record *R = RV[i];
   1545     std::string name = R->getValueAsString("Name");
   1546     std::string Proto = R->getValueAsString("Prototype");
   1547     std::string Types = R->getValueAsString("Types");
   1548     bool isShift = R->getValueAsBit("isShift");
   1549 
   1550     SmallVector<StringRef, 16> TypeVec;
   1551     ParseTypes(R, Types, TypeVec);
   1552 
   1553     OpKind kind = OpMap[R->getValueAsDef("Operand")->getName()];
   1554     for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
   1555       if (kind == OpReinterpret) {
   1556         bool outQuad = false;
   1557         bool dummy = false;
   1558         (void)ClassifyType(TypeVec[ti], outQuad, dummy, dummy);
   1559         for (unsigned srcti = 0, srcte = TypeVec.size();
   1560              srcti != srcte; ++srcti) {
   1561           bool inQuad = false;
   1562           (void)ClassifyType(TypeVec[srcti], inQuad, dummy, dummy);
   1563           if (srcti == ti || inQuad != outQuad)
   1564             continue;
   1565           OS << GenTest(name, Proto, TypeVec[ti], TypeVec[srcti], isShift);
   1566         }
   1567       } else {
   1568         OS << GenTest(name, Proto, TypeVec[ti], TypeVec[ti], isShift);
   1569       }
   1570     }
   1571     OS << "\n";
   1572   }
   1573 }
   1574 
   1575