]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/llvm/tools/clang/utils/TableGen/NeonEmitter.cpp
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / llvm / tools / clang / utils / TableGen / NeonEmitter.cpp
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 "llvm/ADT/DenseMap.h"
27 #include "llvm/ADT/SmallString.h"
28 #include "llvm/ADT/SmallVector.h"
29 #include "llvm/ADT/StringExtras.h"
30 #include "llvm/ADT/StringMap.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/TableGen/Error.h"
33 #include "llvm/TableGen/Record.h"
34 #include "llvm/TableGen/TableGenBackend.h"
35 #include <string>
36 using namespace llvm;
37
38 enum OpKind {
39   OpNone,
40   OpUnavailable,
41   OpAdd,
42   OpAddl,
43   OpAddw,
44   OpSub,
45   OpSubl,
46   OpSubw,
47   OpMul,
48   OpMla,
49   OpMlal,
50   OpMls,
51   OpMlsl,
52   OpMulN,
53   OpMlaN,
54   OpMlsN,
55   OpMlalN,
56   OpMlslN,
57   OpMulLane,
58   OpMullLane,
59   OpMlaLane,
60   OpMlsLane,
61   OpMlalLane,
62   OpMlslLane,
63   OpQDMullLane,
64   OpQDMlalLane,
65   OpQDMlslLane,
66   OpQDMulhLane,
67   OpQRDMulhLane,
68   OpEq,
69   OpGe,
70   OpLe,
71   OpGt,
72   OpLt,
73   OpNeg,
74   OpNot,
75   OpAnd,
76   OpOr,
77   OpXor,
78   OpAndNot,
79   OpOrNot,
80   OpCast,
81   OpConcat,
82   OpDup,
83   OpDupLane,
84   OpHi,
85   OpLo,
86   OpSelect,
87   OpRev16,
88   OpRev32,
89   OpRev64,
90   OpReinterpret,
91   OpAbdl,
92   OpAba,
93   OpAbal
94 };
95
96 enum ClassKind {
97   ClassNone,
98   ClassI,           // generic integer instruction, e.g., "i8" suffix
99   ClassS,           // signed/unsigned/poly, e.g., "s8", "u8" or "p8" suffix
100   ClassW,           // width-specific instruction, e.g., "8" suffix
101   ClassB,           // bitcast arguments with enum argument to specify type
102   ClassL,           // Logical instructions which are op instructions
103                     // but we need to not emit any suffix for in our
104                     // tests.
105   ClassNoTest       // Instructions which we do not test since they are
106                     // not TRUE instructions.
107 };
108
109 /// NeonTypeFlags - Flags to identify the types for overloaded Neon
110 /// builtins.  These must be kept in sync with the flags in
111 /// include/clang/Basic/TargetBuiltins.h.
112 namespace {
113 class NeonTypeFlags {
114   enum {
115     EltTypeMask = 0xf,
116     UnsignedFlag = 0x10,
117     QuadFlag = 0x20
118   };
119   uint32_t Flags;
120
121 public:
122   enum EltType {
123     Int8,
124     Int16,
125     Int32,
126     Int64,
127     Poly8,
128     Poly16,
129     Float16,
130     Float32
131   };
132
133   NeonTypeFlags(unsigned F) : Flags(F) {}
134   NeonTypeFlags(EltType ET, bool IsUnsigned, bool IsQuad) : Flags(ET) {
135     if (IsUnsigned)
136       Flags |= UnsignedFlag;
137     if (IsQuad)
138       Flags |= QuadFlag;
139   }
140
141   uint32_t getFlags() const { return Flags; }
142 };
143 } // end anonymous namespace
144
145 namespace {
146 class NeonEmitter {
147   RecordKeeper &Records;
148   StringMap<OpKind> OpMap;
149   DenseMap<Record*, ClassKind> ClassMap;
150
151 public:
152   NeonEmitter(RecordKeeper &R) : Records(R) {
153     OpMap["OP_NONE"]  = OpNone;
154     OpMap["OP_UNAVAILABLE"] = OpUnavailable;
155     OpMap["OP_ADD"]   = OpAdd;
156     OpMap["OP_ADDL"]  = OpAddl;
157     OpMap["OP_ADDW"]  = OpAddw;
158     OpMap["OP_SUB"]   = OpSub;
159     OpMap["OP_SUBL"]  = OpSubl;
160     OpMap["OP_SUBW"]  = OpSubw;
161     OpMap["OP_MUL"]   = OpMul;
162     OpMap["OP_MLA"]   = OpMla;
163     OpMap["OP_MLAL"]  = OpMlal;
164     OpMap["OP_MLS"]   = OpMls;
165     OpMap["OP_MLSL"]  = OpMlsl;
166     OpMap["OP_MUL_N"] = OpMulN;
167     OpMap["OP_MLA_N"] = OpMlaN;
168     OpMap["OP_MLS_N"] = OpMlsN;
169     OpMap["OP_MLAL_N"] = OpMlalN;
170     OpMap["OP_MLSL_N"] = OpMlslN;
171     OpMap["OP_MUL_LN"]= OpMulLane;
172     OpMap["OP_MULL_LN"] = OpMullLane;
173     OpMap["OP_MLA_LN"]= OpMlaLane;
174     OpMap["OP_MLS_LN"]= OpMlsLane;
175     OpMap["OP_MLAL_LN"] = OpMlalLane;
176     OpMap["OP_MLSL_LN"] = OpMlslLane;
177     OpMap["OP_QDMULL_LN"] = OpQDMullLane;
178     OpMap["OP_QDMLAL_LN"] = OpQDMlalLane;
179     OpMap["OP_QDMLSL_LN"] = OpQDMlslLane;
180     OpMap["OP_QDMULH_LN"] = OpQDMulhLane;
181     OpMap["OP_QRDMULH_LN"] = OpQRDMulhLane;
182     OpMap["OP_EQ"]    = OpEq;
183     OpMap["OP_GE"]    = OpGe;
184     OpMap["OP_LE"]    = OpLe;
185     OpMap["OP_GT"]    = OpGt;
186     OpMap["OP_LT"]    = OpLt;
187     OpMap["OP_NEG"]   = OpNeg;
188     OpMap["OP_NOT"]   = OpNot;
189     OpMap["OP_AND"]   = OpAnd;
190     OpMap["OP_OR"]    = OpOr;
191     OpMap["OP_XOR"]   = OpXor;
192     OpMap["OP_ANDN"]  = OpAndNot;
193     OpMap["OP_ORN"]   = OpOrNot;
194     OpMap["OP_CAST"]  = OpCast;
195     OpMap["OP_CONC"]  = OpConcat;
196     OpMap["OP_HI"]    = OpHi;
197     OpMap["OP_LO"]    = OpLo;
198     OpMap["OP_DUP"]   = OpDup;
199     OpMap["OP_DUP_LN"] = OpDupLane;
200     OpMap["OP_SEL"]   = OpSelect;
201     OpMap["OP_REV16"] = OpRev16;
202     OpMap["OP_REV32"] = OpRev32;
203     OpMap["OP_REV64"] = OpRev64;
204     OpMap["OP_REINT"] = OpReinterpret;
205     OpMap["OP_ABDL"]  = OpAbdl;
206     OpMap["OP_ABA"]   = OpAba;
207     OpMap["OP_ABAL"]  = OpAbal;
208
209     Record *SI = R.getClass("SInst");
210     Record *II = R.getClass("IInst");
211     Record *WI = R.getClass("WInst");
212     Record *SOpI = R.getClass("SOpInst");
213     Record *IOpI = R.getClass("IOpInst");
214     Record *WOpI = R.getClass("WOpInst");
215     Record *LOpI = R.getClass("LOpInst");
216     Record *NoTestOpI = R.getClass("NoTestOpInst");
217
218     ClassMap[SI] = ClassS;
219     ClassMap[II] = ClassI;
220     ClassMap[WI] = ClassW;
221     ClassMap[SOpI] = ClassS;
222     ClassMap[IOpI] = ClassI;
223     ClassMap[WOpI] = ClassW;
224     ClassMap[LOpI] = ClassL;
225     ClassMap[NoTestOpI] = ClassNoTest;
226   }
227
228   // run - Emit arm_neon.h.inc
229   void run(raw_ostream &o);
230
231   // runHeader - Emit all the __builtin prototypes used in arm_neon.h
232   void runHeader(raw_ostream &o);
233
234   // runTests - Emit tests for all the Neon intrinsics.
235   void runTests(raw_ostream &o);
236
237 private:
238   void emitIntrinsic(raw_ostream &OS, Record *R);
239 };
240 } // end anonymous namespace
241
242 /// ParseTypes - break down a string such as "fQf" into a vector of StringRefs,
243 /// which each StringRef representing a single type declared in the string.
244 /// for "fQf" we would end up with 2 StringRefs, "f", and "Qf", representing
245 /// 2xfloat and 4xfloat respectively.
246 static void ParseTypes(Record *r, std::string &s,
247                        SmallVectorImpl<StringRef> &TV) {
248   const char *data = s.data();
249   int len = 0;
250
251   for (unsigned i = 0, e = s.size(); i != e; ++i, ++len) {
252     if (data[len] == 'P' || data[len] == 'Q' || data[len] == 'U')
253       continue;
254
255     switch (data[len]) {
256       case 'c':
257       case 's':
258       case 'i':
259       case 'l':
260       case 'h':
261       case 'f':
262         break;
263       default:
264         PrintFatalError(r->getLoc(),
265                       "Unexpected letter: " + std::string(data + len, 1));
266     }
267     TV.push_back(StringRef(data, len + 1));
268     data += len + 1;
269     len = -1;
270   }
271 }
272
273 /// Widen - Convert a type code into the next wider type.  char -> short,
274 /// short -> int, etc.
275 static char Widen(const char t) {
276   switch (t) {
277     case 'c':
278       return 's';
279     case 's':
280       return 'i';
281     case 'i':
282       return 'l';
283     case 'h':
284       return 'f';
285     default:
286       PrintFatalError("unhandled type in widen!");
287   }
288 }
289
290 /// Narrow - Convert a type code into the next smaller type.  short -> char,
291 /// float -> half float, etc.
292 static char Narrow(const char t) {
293   switch (t) {
294     case 's':
295       return 'c';
296     case 'i':
297       return 's';
298     case 'l':
299       return 'i';
300     case 'f':
301       return 'h';
302     default:
303       PrintFatalError("unhandled type in narrow!");
304   }
305 }
306
307 /// For a particular StringRef, return the base type code, and whether it has
308 /// the quad-vector, polynomial, or unsigned modifiers set.
309 static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) {
310   unsigned off = 0;
311
312   // remember quad.
313   if (ty[off] == 'Q') {
314     quad = true;
315     ++off;
316   }
317
318   // remember poly.
319   if (ty[off] == 'P') {
320     poly = true;
321     ++off;
322   }
323
324   // remember unsigned.
325   if (ty[off] == 'U') {
326     usgn = true;
327     ++off;
328   }
329
330   // base type to get the type string for.
331   return ty[off];
332 }
333
334 /// ModType - Transform a type code and its modifiers based on a mod code. The
335 /// mod code definitions may be found at the top of arm_neon.td.
336 static char ModType(const char mod, char type, bool &quad, bool &poly,
337                     bool &usgn, bool &scal, bool &cnst, bool &pntr) {
338   switch (mod) {
339     case 't':
340       if (poly) {
341         poly = false;
342         usgn = true;
343       }
344       break;
345     case 'u':
346       usgn = true;
347       poly = false;
348       if (type == 'f')
349         type = 'i';
350       break;
351     case 'x':
352       usgn = false;
353       poly = false;
354       if (type == 'f')
355         type = 'i';
356       break;
357     case 'f':
358       if (type == 'h')
359         quad = true;
360       type = 'f';
361       usgn = false;
362       break;
363     case 'g':
364       quad = false;
365       break;
366     case 'w':
367       type = Widen(type);
368       quad = true;
369       break;
370     case 'n':
371       type = Widen(type);
372       break;
373     case 'i':
374       type = 'i';
375       scal = true;
376       break;
377     case 'l':
378       type = 'l';
379       scal = true;
380       usgn = true;
381       break;
382     case 's':
383     case 'a':
384       scal = true;
385       break;
386     case 'k':
387       quad = true;
388       break;
389     case 'c':
390       cnst = true;
391     case 'p':
392       pntr = true;
393       scal = true;
394       break;
395     case 'h':
396       type = Narrow(type);
397       if (type == 'h')
398         quad = false;
399       break;
400     case 'e':
401       type = Narrow(type);
402       usgn = true;
403       break;
404     default:
405       break;
406   }
407   return type;
408 }
409
410 /// TypeString - for a modifier and type, generate the name of the typedef for
411 /// that type.  QUc -> uint8x8_t.
412 static std::string TypeString(const char mod, StringRef typestr) {
413   bool quad = false;
414   bool poly = false;
415   bool usgn = false;
416   bool scal = false;
417   bool cnst = false;
418   bool pntr = false;
419
420   if (mod == 'v')
421     return "void";
422   if (mod == 'i')
423     return "int";
424
425   // base type to get the type string for.
426   char type = ClassifyType(typestr, quad, poly, usgn);
427
428   // Based on the modifying character, change the type and width if necessary.
429   type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
430
431   SmallString<128> s;
432
433   if (usgn)
434     s.push_back('u');
435
436   switch (type) {
437     case 'c':
438       s += poly ? "poly8" : "int8";
439       if (scal)
440         break;
441       s += quad ? "x16" : "x8";
442       break;
443     case 's':
444       s += poly ? "poly16" : "int16";
445       if (scal)
446         break;
447       s += quad ? "x8" : "x4";
448       break;
449     case 'i':
450       s += "int32";
451       if (scal)
452         break;
453       s += quad ? "x4" : "x2";
454       break;
455     case 'l':
456       s += "int64";
457       if (scal)
458         break;
459       s += quad ? "x2" : "x1";
460       break;
461     case 'h':
462       s += "float16";
463       if (scal)
464         break;
465       s += quad ? "x8" : "x4";
466       break;
467     case 'f':
468       s += "float32";
469       if (scal)
470         break;
471       s += quad ? "x4" : "x2";
472       break;
473     default:
474       PrintFatalError("unhandled type!");
475   }
476
477   if (mod == '2')
478     s += "x2";
479   if (mod == '3')
480     s += "x3";
481   if (mod == '4')
482     s += "x4";
483
484   // Append _t, finishing the type string typedef type.
485   s += "_t";
486
487   if (cnst)
488     s += " const";
489
490   if (pntr)
491     s += " *";
492
493   return s.str();
494 }
495
496 /// BuiltinTypeString - for a modifier and type, generate the clang
497 /// BuiltinsARM.def prototype code for the function.  See the top of clang's
498 /// Builtins.def for a description of the type strings.
499 static std::string BuiltinTypeString(const char mod, StringRef typestr,
500                                      ClassKind ck, bool ret) {
501   bool quad = false;
502   bool poly = false;
503   bool usgn = false;
504   bool scal = false;
505   bool cnst = false;
506   bool pntr = false;
507
508   if (mod == 'v')
509     return "v"; // void
510   if (mod == 'i')
511     return "i"; // int
512
513   // base type to get the type string for.
514   char type = ClassifyType(typestr, quad, poly, usgn);
515
516   // Based on the modifying character, change the type and width if necessary.
517   type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
518
519   // All pointers are void* pointers.  Change type to 'v' now.
520   if (pntr) {
521     usgn = false;
522     poly = false;
523     type = 'v';
524   }
525   // Treat half-float ('h') types as unsigned short ('s') types.
526   if (type == 'h') {
527     type = 's';
528     usgn = true;
529   }
530   usgn = usgn | poly | ((ck == ClassI || ck == ClassW) && scal && type != 'f');
531
532   if (scal) {
533     SmallString<128> s;
534
535     if (usgn)
536       s.push_back('U');
537     else if (type == 'c')
538       s.push_back('S'); // make chars explicitly signed
539
540     if (type == 'l') // 64-bit long
541       s += "LLi";
542     else
543       s.push_back(type);
544
545     if (cnst)
546       s.push_back('C');
547     if (pntr)
548       s.push_back('*');
549     return s.str();
550   }
551
552   // Since the return value must be one type, return a vector type of the
553   // appropriate width which we will bitcast.  An exception is made for
554   // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
555   // fashion, storing them to a pointer arg.
556   if (ret) {
557     if (mod >= '2' && mod <= '4')
558       return "vv*"; // void result with void* first argument
559     if (mod == 'f' || (ck != ClassB && type == 'f'))
560       return quad ? "V4f" : "V2f";
561     if (ck != ClassB && type == 's')
562       return quad ? "V8s" : "V4s";
563     if (ck != ClassB && type == 'i')
564       return quad ? "V4i" : "V2i";
565     if (ck != ClassB && type == 'l')
566       return quad ? "V2LLi" : "V1LLi";
567
568     return quad ? "V16Sc" : "V8Sc";
569   }
570
571   // Non-return array types are passed as individual vectors.
572   if (mod == '2')
573     return quad ? "V16ScV16Sc" : "V8ScV8Sc";
574   if (mod == '3')
575     return quad ? "V16ScV16ScV16Sc" : "V8ScV8ScV8Sc";
576   if (mod == '4')
577     return quad ? "V16ScV16ScV16ScV16Sc" : "V8ScV8ScV8ScV8Sc";
578
579   if (mod == 'f' || (ck != ClassB && type == 'f'))
580     return quad ? "V4f" : "V2f";
581   if (ck != ClassB && type == 's')
582     return quad ? "V8s" : "V4s";
583   if (ck != ClassB && type == 'i')
584     return quad ? "V4i" : "V2i";
585   if (ck != ClassB && type == 'l')
586     return quad ? "V2LLi" : "V1LLi";
587
588   return quad ? "V16Sc" : "V8Sc";
589 }
590
591 /// InstructionTypeCode - Computes the ARM argument character code and
592 /// quad status for a specific type string and ClassKind.
593 static void InstructionTypeCode(const StringRef &typeStr,
594                                 const ClassKind ck,
595                                 bool &quad,
596                                 std::string &typeCode) {
597   bool poly = false;
598   bool usgn = false;
599   char type = ClassifyType(typeStr, quad, poly, usgn);
600
601   switch (type) {
602   case 'c':
603     switch (ck) {
604     case ClassS: typeCode = poly ? "p8" : usgn ? "u8" : "s8"; break;
605     case ClassI: typeCode = "i8"; break;
606     case ClassW: typeCode = "8"; break;
607     default: break;
608     }
609     break;
610   case 's':
611     switch (ck) {
612     case ClassS: typeCode = poly ? "p16" : usgn ? "u16" : "s16"; break;
613     case ClassI: typeCode = "i16"; break;
614     case ClassW: typeCode = "16"; break;
615     default: break;
616     }
617     break;
618   case 'i':
619     switch (ck) {
620     case ClassS: typeCode = usgn ? "u32" : "s32"; break;
621     case ClassI: typeCode = "i32"; break;
622     case ClassW: typeCode = "32"; break;
623     default: break;
624     }
625     break;
626   case 'l':
627     switch (ck) {
628     case ClassS: typeCode = usgn ? "u64" : "s64"; break;
629     case ClassI: typeCode = "i64"; break;
630     case ClassW: typeCode = "64"; break;
631     default: break;
632     }
633     break;
634   case 'h':
635     switch (ck) {
636     case ClassS:
637     case ClassI: typeCode = "f16"; break;
638     case ClassW: typeCode = "16"; break;
639     default: break;
640     }
641     break;
642   case 'f':
643     switch (ck) {
644     case ClassS:
645     case ClassI: typeCode = "f32"; break;
646     case ClassW: typeCode = "32"; break;
647     default: break;
648     }
649     break;
650   default:
651     PrintFatalError("unhandled type!");
652   }
653 }
654
655 /// MangleName - Append a type or width suffix to a base neon function name,
656 /// and insert a 'q' in the appropriate location if the operation works on
657 /// 128b rather than 64b.   E.g. turn "vst2_lane" into "vst2q_lane_f32", etc.
658 static std::string MangleName(const std::string &name, StringRef typestr,
659                               ClassKind ck) {
660   if (name == "vcvt_f32_f16")
661     return name;
662
663   bool quad = false;
664   std::string typeCode = "";
665
666   InstructionTypeCode(typestr, ck, quad, typeCode);
667
668   std::string s = name;
669
670   if (typeCode.size() > 0) {
671     s += "_" + typeCode;
672   }
673
674   if (ck == ClassB)
675     s += "_v";
676
677   // Insert a 'q' before the first '_' character so that it ends up before
678   // _lane or _n on vector-scalar operations.
679   if (quad) {
680     size_t pos = s.find('_');
681     s = s.insert(pos, "q");
682   }
683
684   return s;
685 }
686
687 static void PreprocessInstruction(const StringRef &Name,
688                                   const std::string &InstName,
689                                   std::string &Prefix,
690                                   bool &HasNPostfix,
691                                   bool &HasLanePostfix,
692                                   bool &HasDupPostfix,
693                                   bool &IsSpecialVCvt,
694                                   size_t &TBNumber) {
695   // All of our instruction name fields from arm_neon.td are of the form
696   //   <instructionname>_...
697   // Thus we grab our instruction name via computation of said Prefix.
698   const size_t PrefixEnd = Name.find_first_of('_');
699   // If InstName is passed in, we use that instead of our name Prefix.
700   Prefix = InstName.size() == 0? Name.slice(0, PrefixEnd).str() : InstName;
701
702   const StringRef Postfix = Name.slice(PrefixEnd, Name.size());
703
704   HasNPostfix = Postfix.count("_n");
705   HasLanePostfix = Postfix.count("_lane");
706   HasDupPostfix = Postfix.count("_dup");
707   IsSpecialVCvt = Postfix.size() != 0 && Name.count("vcvt");
708
709   if (InstName.compare("vtbl") == 0 ||
710       InstName.compare("vtbx") == 0) {
711     // If we have a vtblN/vtbxN instruction, use the instruction's ASCII
712     // encoding to get its true value.
713     TBNumber = Name[Name.size()-1] - 48;
714   }
715 }
716
717 /// GenerateRegisterCheckPatternsForLoadStores - Given a bunch of data we have
718 /// extracted, generate a FileCheck pattern for a Load Or Store
719 static void
720 GenerateRegisterCheckPatternForLoadStores(const StringRef &NameRef,
721                                           const std::string& OutTypeCode,
722                                           const bool &IsQuad,
723                                           const bool &HasDupPostfix,
724                                           const bool &HasLanePostfix,
725                                           const size_t Count,
726                                           std::string &RegisterSuffix) {
727   const bool IsLDSTOne = NameRef.count("vld1") || NameRef.count("vst1");
728   // If N == 3 || N == 4 and we are dealing with a quad instruction, Clang
729   // will output a series of v{ld,st}1s, so we have to handle it specially.
730   if ((Count == 3 || Count == 4) && IsQuad) {
731     RegisterSuffix += "{";
732     for (size_t i = 0; i < Count; i++) {
733       RegisterSuffix += "d{{[0-9]+}}";
734       if (HasDupPostfix) {
735         RegisterSuffix += "[]";
736       }
737       if (HasLanePostfix) {
738         RegisterSuffix += "[{{[0-9]+}}]";
739       }
740       if (i < Count-1) {
741         RegisterSuffix += ", ";
742       }
743     }
744     RegisterSuffix += "}";
745   } else {
746
747     // Handle normal loads and stores.
748     RegisterSuffix += "{";
749     for (size_t i = 0; i < Count; i++) {
750       RegisterSuffix += "d{{[0-9]+}}";
751       if (HasDupPostfix) {
752         RegisterSuffix += "[]";
753       }
754       if (HasLanePostfix) {
755         RegisterSuffix += "[{{[0-9]+}}]";
756       }
757       if (IsQuad && !HasLanePostfix) {
758         RegisterSuffix += ", d{{[0-9]+}}";
759         if (HasDupPostfix) {
760           RegisterSuffix += "[]";
761         }
762       }
763       if (i < Count-1) {
764         RegisterSuffix += ", ";
765       }
766     }
767     RegisterSuffix += "}, [r{{[0-9]+}}";
768
769     // We only include the alignment hint if we have a vld1.*64 or
770     // a dup/lane instruction.
771     if (IsLDSTOne) {
772       if ((HasLanePostfix || HasDupPostfix) && OutTypeCode != "8") {
773         RegisterSuffix += ", :" + OutTypeCode;
774       } else if (OutTypeCode == "64") {
775         RegisterSuffix += ", :64";
776       }
777     }
778
779     RegisterSuffix += "]";
780   }
781 }
782
783 static bool HasNPostfixAndScalarArgs(const StringRef &NameRef,
784                                      const bool &HasNPostfix) {
785   return (NameRef.count("vmla") ||
786           NameRef.count("vmlal") ||
787           NameRef.count("vmlsl") ||
788           NameRef.count("vmull") ||
789           NameRef.count("vqdmlal") ||
790           NameRef.count("vqdmlsl") ||
791           NameRef.count("vqdmulh") ||
792           NameRef.count("vqdmull") ||
793           NameRef.count("vqrdmulh")) && HasNPostfix;
794 }
795
796 static bool IsFiveOperandLaneAccumulator(const StringRef &NameRef,
797                                          const bool &HasLanePostfix) {
798   return (NameRef.count("vmla") ||
799           NameRef.count("vmls") ||
800           NameRef.count("vmlal") ||
801           NameRef.count("vmlsl") ||
802           (NameRef.count("vmul") && NameRef.size() == 3)||
803           NameRef.count("vqdmlal") ||
804           NameRef.count("vqdmlsl") ||
805           NameRef.count("vqdmulh") ||
806           NameRef.count("vqrdmulh")) && HasLanePostfix;
807 }
808
809 static bool IsSpecialLaneMultiply(const StringRef &NameRef,
810                                   const bool &HasLanePostfix,
811                                   const bool &IsQuad) {
812   const bool IsVMulOrMulh = (NameRef.count("vmul") || NameRef.count("mulh"))
813                                && IsQuad;
814   const bool IsVMull = NameRef.count("mull") && !IsQuad;
815   return (IsVMulOrMulh || IsVMull) && HasLanePostfix;
816 }
817
818 static void NormalizeProtoForRegisterPatternCreation(const std::string &Name,
819                                                      const std::string &Proto,
820                                                      const bool &HasNPostfix,
821                                                      const bool &IsQuad,
822                                                      const bool &HasLanePostfix,
823                                                      const bool &HasDupPostfix,
824                                                      std::string &NormedProto) {
825   // Handle generic case.
826   const StringRef NameRef(Name);
827   for (size_t i = 0, end = Proto.size(); i < end; i++) {
828     switch (Proto[i]) {
829     case 'u':
830     case 'f':
831     case 'd':
832     case 's':
833     case 'x':
834     case 't':
835     case 'n':
836       NormedProto += IsQuad? 'q' : 'd';
837       break;
838     case 'w':
839     case 'k':
840       NormedProto += 'q';
841       break;
842     case 'g':
843     case 'h':
844     case 'e':
845       NormedProto += 'd';
846       break;
847     case 'i':
848       NormedProto += HasLanePostfix? 'a' : 'i';
849       break;
850     case 'a':
851       if (HasLanePostfix) {
852         NormedProto += 'a';
853       } else if (HasNPostfixAndScalarArgs(NameRef, HasNPostfix)) {
854         NormedProto += IsQuad? 'q' : 'd';
855       } else {
856         NormedProto += 'i';
857       }
858       break;
859     }
860   }
861
862   // Handle Special Cases.
863   const bool IsNotVExt = !NameRef.count("vext");
864   const bool IsVPADAL = NameRef.count("vpadal");
865   const bool Is5OpLaneAccum = IsFiveOperandLaneAccumulator(NameRef,
866                                                            HasLanePostfix);
867   const bool IsSpecialLaneMul = IsSpecialLaneMultiply(NameRef, HasLanePostfix,
868                                                       IsQuad);
869
870   if (IsSpecialLaneMul) {
871     // If
872     NormedProto[2] = NormedProto[3];
873     NormedProto.erase(3);
874   } else if (NormedProto.size() == 4 &&
875              NormedProto[0] == NormedProto[1] &&
876              IsNotVExt) {
877     // If NormedProto.size() == 4 and the first two proto characters are the
878     // same, ignore the first.
879     NormedProto = NormedProto.substr(1, 3);
880   } else if (Is5OpLaneAccum) {
881     // If we have a 5 op lane accumulator operation, we take characters 1,2,4
882     std::string tmp = NormedProto.substr(1,2);
883     tmp += NormedProto[4];
884     NormedProto = tmp;
885   } else if (IsVPADAL) {
886     // If we have VPADAL, ignore the first character.
887     NormedProto = NormedProto.substr(0, 2);
888   } else if (NameRef.count("vdup") && NormedProto.size() > 2) {
889     // If our instruction is a dup instruction, keep only the first and
890     // last characters.
891     std::string tmp = "";
892     tmp += NormedProto[0];
893     tmp += NormedProto[NormedProto.size()-1];
894     NormedProto = tmp;
895   }
896 }
897
898 /// GenerateRegisterCheckPatterns - Given a bunch of data we have
899 /// extracted, generate a FileCheck pattern to check that an
900 /// instruction's arguments are correct.
901 static void GenerateRegisterCheckPattern(const std::string &Name,
902                                          const std::string &Proto,
903                                          const std::string &OutTypeCode,
904                                          const bool &HasNPostfix,
905                                          const bool &IsQuad,
906                                          const bool &HasLanePostfix,
907                                          const bool &HasDupPostfix,
908                                          const size_t &TBNumber,
909                                          std::string &RegisterSuffix) {
910
911   RegisterSuffix = "";
912
913   const StringRef NameRef(Name);
914   const StringRef ProtoRef(Proto);
915
916   if ((NameRef.count("vdup") || NameRef.count("vmov")) && HasNPostfix) {
917     return;
918   }
919
920   const bool IsLoadStore = NameRef.count("vld") || NameRef.count("vst");
921   const bool IsTBXOrTBL = NameRef.count("vtbl") || NameRef.count("vtbx");
922
923   if (IsLoadStore) {
924     // Grab N value from  v{ld,st}N using its ascii representation.
925     const size_t Count = NameRef[3] - 48;
926
927     GenerateRegisterCheckPatternForLoadStores(NameRef, OutTypeCode, IsQuad,
928                                               HasDupPostfix, HasLanePostfix,
929                                               Count, RegisterSuffix);
930   } else if (IsTBXOrTBL) {
931     RegisterSuffix += "d{{[0-9]+}}, {";
932     for (size_t i = 0; i < TBNumber-1; i++) {
933       RegisterSuffix += "d{{[0-9]+}}, ";
934     }
935     RegisterSuffix += "d{{[0-9]+}}}, d{{[0-9]+}}";
936   } else {
937     // Handle a normal instruction.
938     if (NameRef.count("vget") || NameRef.count("vset"))
939       return;
940
941     // We first normalize our proto, since we only need to emit 4
942     // different types of checks, yet have more than 4 proto types
943     // that map onto those 4 patterns.
944     std::string NormalizedProto("");
945     NormalizeProtoForRegisterPatternCreation(Name, Proto, HasNPostfix, IsQuad,
946                                              HasLanePostfix, HasDupPostfix,
947                                              NormalizedProto);
948
949     for (size_t i = 0, end = NormalizedProto.size(); i < end; i++) {
950       const char &c = NormalizedProto[i];
951       switch (c) {
952       case 'q':
953         RegisterSuffix += "q{{[0-9]+}}, ";
954         break;
955
956       case 'd':
957         RegisterSuffix += "d{{[0-9]+}}, ";
958         break;
959
960       case 'i':
961         RegisterSuffix += "#{{[0-9]+}}, ";
962         break;
963
964       case 'a':
965         RegisterSuffix += "d{{[0-9]+}}[{{[0-9]}}], ";
966         break;
967       }
968     }
969
970     // Remove extra ", ".
971     RegisterSuffix = RegisterSuffix.substr(0, RegisterSuffix.size()-2);
972   }
973 }
974
975 /// GenerateChecksForIntrinsic - Given a specific instruction name +
976 /// typestr + class kind, generate the proper set of FileCheck
977 /// Patterns to check for. We could just return a string, but instead
978 /// use a vector since it provides us with the extra flexibility of
979 /// emitting multiple checks, which comes in handy for certain cases
980 /// like mla where we want to check for 2 different instructions.
981 static void GenerateChecksForIntrinsic(const std::string &Name,
982                                        const std::string &Proto,
983                                        StringRef &OutTypeStr,
984                                        StringRef &InTypeStr,
985                                        ClassKind Ck,
986                                        const std::string &InstName,
987                                        bool IsHiddenLOp,
988                                        std::vector<std::string>& Result) {
989
990   // If Ck is a ClassNoTest instruction, just return so no test is
991   // emitted.
992   if(Ck == ClassNoTest)
993     return;
994
995   if (Name == "vcvt_f32_f16") {
996     Result.push_back("vcvt.f32.f16");
997     return;
998   }
999
1000
1001   // Now we preprocess our instruction given the data we have to get the
1002   // data that we need.
1003   // Create a StringRef for String Manipulation of our Name.
1004   const StringRef NameRef(Name);
1005   // Instruction Prefix.
1006   std::string Prefix;
1007   // The type code for our out type string.
1008   std::string OutTypeCode;
1009   // To handle our different cases, we need to check for different postfixes.
1010   // Is our instruction a quad instruction.
1011   bool IsQuad = false;
1012   // Our instruction is of the form <instructionname>_n.
1013   bool HasNPostfix = false;
1014   // Our instruction is of the form <instructionname>_lane.
1015   bool HasLanePostfix = false;
1016   // Our instruction is of the form <instructionname>_dup.
1017   bool HasDupPostfix  = false;
1018   // Our instruction is a vcvt instruction which requires special handling.
1019   bool IsSpecialVCvt = false;
1020   // If we have a vtbxN or vtblN instruction, this is set to N.
1021   size_t TBNumber = -1;
1022   // Register Suffix
1023   std::string RegisterSuffix;
1024
1025   PreprocessInstruction(NameRef, InstName, Prefix,
1026                         HasNPostfix, HasLanePostfix, HasDupPostfix,
1027                         IsSpecialVCvt, TBNumber);
1028
1029   InstructionTypeCode(OutTypeStr, Ck, IsQuad, OutTypeCode);
1030   GenerateRegisterCheckPattern(Name, Proto, OutTypeCode, HasNPostfix, IsQuad,
1031                                HasLanePostfix, HasDupPostfix, TBNumber,
1032                                RegisterSuffix);
1033
1034   // In the following section, we handle a bunch of special cases. You can tell
1035   // a special case by the fact we are returning early.
1036
1037   // If our instruction is a logical instruction without postfix or a
1038   // hidden LOp just return the current Prefix.
1039   if (Ck == ClassL || IsHiddenLOp) {
1040     Result.push_back(Prefix + " " + RegisterSuffix);
1041     return;
1042   }
1043
1044   // If we have a vmov, due to the many different cases, some of which
1045   // vary within the different intrinsics generated for a single
1046   // instruction type, just output a vmov. (e.g. given an instruction
1047   // A, A.u32 might be vmov and A.u8 might be vmov.8).
1048   //
1049   // FIXME: Maybe something can be done about this. The two cases that we care
1050   // about are vmov as an LType and vmov as a WType.
1051   if (Prefix == "vmov") {
1052     Result.push_back(Prefix + " " + RegisterSuffix);
1053     return;
1054   }
1055
1056   // In the following section, we handle special cases.
1057
1058   if (OutTypeCode == "64") {
1059     // If we have a 64 bit vdup/vext and are handling an uint64x1_t
1060     // type, the intrinsic will be optimized away, so just return
1061     // nothing.  On the other hand if we are handling an uint64x2_t
1062     // (i.e. quad instruction), vdup/vmov instructions should be
1063     // emitted.
1064     if (Prefix == "vdup" || Prefix == "vext") {
1065       if (IsQuad) {
1066         Result.push_back("{{vmov|vdup}}");
1067       }
1068       return;
1069     }
1070
1071     // v{st,ld}{2,3,4}_{u,s}64 emit v{st,ld}1.64 instructions with
1072     // multiple register operands.
1073     bool MultiLoadPrefix = Prefix == "vld2" || Prefix == "vld3"
1074                             || Prefix == "vld4";
1075     bool MultiStorePrefix = Prefix == "vst2" || Prefix == "vst3"
1076                             || Prefix == "vst4";
1077     if (MultiLoadPrefix || MultiStorePrefix) {
1078       Result.push_back(NameRef.slice(0, 3).str() + "1.64");
1079       return;
1080     }
1081
1082     // v{st,ld}1_{lane,dup}_{u64,s64} use vldr/vstr/vmov/str instead of
1083     // emitting said instructions. So return a check for
1084     // vldr/vstr/vmov/str instead.
1085     if (HasLanePostfix || HasDupPostfix) {
1086       if (Prefix == "vst1") {
1087         Result.push_back("{{str|vstr|vmov}}");
1088         return;
1089       } else if (Prefix == "vld1") {
1090         Result.push_back("{{ldr|vldr|vmov}}");
1091         return;
1092       }
1093     }
1094   }
1095
1096   // vzip.32/vuzp.32 are the same instruction as vtrn.32 and are
1097   // sometimes disassembled as vtrn.32. We use a regex to handle both
1098   // cases.
1099   if ((Prefix == "vzip" || Prefix == "vuzp") && OutTypeCode == "32") {
1100     Result.push_back("{{vtrn|" + Prefix + "}}.32 " + RegisterSuffix);
1101     return;
1102   }
1103
1104   // Currently on most ARM processors, we do not use vmla/vmls for
1105   // quad floating point operations. Instead we output vmul + vadd. So
1106   // check if we have one of those instructions and just output a
1107   // check for vmul.
1108   if (OutTypeCode == "f32") {
1109     if (Prefix == "vmls") {
1110       Result.push_back("vmul." + OutTypeCode + " " + RegisterSuffix);
1111       Result.push_back("vsub." + OutTypeCode);
1112       return;
1113     } else if (Prefix == "vmla") {
1114       Result.push_back("vmul." + OutTypeCode + " " + RegisterSuffix);
1115       Result.push_back("vadd." + OutTypeCode);
1116       return;
1117     }
1118   }
1119
1120   // If we have vcvt, get the input type from the instruction name
1121   // (which should be of the form instname_inputtype) and append it
1122   // before the output type.
1123   if (Prefix == "vcvt") {
1124     const std::string inTypeCode = NameRef.substr(NameRef.find_last_of("_")+1);
1125     Prefix += "." + inTypeCode;
1126   }
1127
1128   // Append output type code to get our final mangled instruction.
1129   Prefix += "." + OutTypeCode;
1130
1131   Result.push_back(Prefix + " " + RegisterSuffix);
1132 }
1133
1134 /// UseMacro - Examine the prototype string to determine if the intrinsic
1135 /// should be defined as a preprocessor macro instead of an inline function.
1136 static bool UseMacro(const std::string &proto) {
1137   // If this builtin takes an immediate argument, we need to #define it rather
1138   // than use a standard declaration, so that SemaChecking can range check
1139   // the immediate passed by the user.
1140   if (proto.find('i') != std::string::npos)
1141     return true;
1142
1143   // Pointer arguments need to use macros to avoid hiding aligned attributes
1144   // from the pointer type.
1145   if (proto.find('p') != std::string::npos ||
1146       proto.find('c') != std::string::npos)
1147     return true;
1148
1149   return false;
1150 }
1151
1152 /// MacroArgUsedDirectly - Return true if argument i for an intrinsic that is
1153 /// defined as a macro should be accessed directly instead of being first
1154 /// assigned to a local temporary.
1155 static bool MacroArgUsedDirectly(const std::string &proto, unsigned i) {
1156   // True for constant ints (i), pointers (p) and const pointers (c).
1157   return (proto[i] == 'i' || proto[i] == 'p' || proto[i] == 'c');
1158 }
1159
1160 // Generate the string "(argtype a, argtype b, ...)"
1161 static std::string GenArgs(const std::string &proto, StringRef typestr) {
1162   bool define = UseMacro(proto);
1163   char arg = 'a';
1164
1165   std::string s;
1166   s += "(";
1167
1168   for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
1169     if (define) {
1170       // Some macro arguments are used directly instead of being assigned
1171       // to local temporaries; prepend an underscore prefix to make their
1172       // names consistent with the local temporaries.
1173       if (MacroArgUsedDirectly(proto, i))
1174         s += "__";
1175     } else {
1176       s += TypeString(proto[i], typestr) + " __";
1177     }
1178     s.push_back(arg);
1179     if ((i + 1) < e)
1180       s += ", ";
1181   }
1182
1183   s += ")";
1184   return s;
1185 }
1186
1187 // Macro arguments are not type-checked like inline function arguments, so
1188 // assign them to local temporaries to get the right type checking.
1189 static std::string GenMacroLocals(const std::string &proto, StringRef typestr) {
1190   char arg = 'a';
1191   std::string s;
1192   bool generatedLocal = false;
1193
1194   for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
1195     // Do not create a temporary for an immediate argument.
1196     // That would defeat the whole point of using a macro!
1197     if (MacroArgUsedDirectly(proto, i))
1198       continue;
1199     generatedLocal = true;
1200
1201     s += TypeString(proto[i], typestr) + " __";
1202     s.push_back(arg);
1203     s += " = (";
1204     s.push_back(arg);
1205     s += "); ";
1206   }
1207
1208   if (generatedLocal)
1209     s += "\\\n  ";
1210   return s;
1211 }
1212
1213 // Use the vmovl builtin to sign-extend or zero-extend a vector.
1214 static std::string Extend(StringRef typestr, const std::string &a) {
1215   std::string s;
1216   s = MangleName("vmovl", typestr, ClassS);
1217   s += "(" + a + ")";
1218   return s;
1219 }
1220
1221 static std::string Duplicate(unsigned nElts, StringRef typestr,
1222                              const std::string &a) {
1223   std::string s;
1224
1225   s = "(" + TypeString('d', typestr) + "){ ";
1226   for (unsigned i = 0; i != nElts; ++i) {
1227     s += a;
1228     if ((i + 1) < nElts)
1229       s += ", ";
1230   }
1231   s += " }";
1232
1233   return s;
1234 }
1235
1236 static std::string SplatLane(unsigned nElts, const std::string &vec,
1237                              const std::string &lane) {
1238   std::string s = "__builtin_shufflevector(" + vec + ", " + vec;
1239   for (unsigned i = 0; i < nElts; ++i)
1240     s += ", " + lane;
1241   s += ")";
1242   return s;
1243 }
1244
1245 static unsigned GetNumElements(StringRef typestr, bool &quad) {
1246   quad = false;
1247   bool dummy = false;
1248   char type = ClassifyType(typestr, quad, dummy, dummy);
1249   unsigned nElts = 0;
1250   switch (type) {
1251   case 'c': nElts = 8; break;
1252   case 's': nElts = 4; break;
1253   case 'i': nElts = 2; break;
1254   case 'l': nElts = 1; break;
1255   case 'h': nElts = 4; break;
1256   case 'f': nElts = 2; break;
1257   default:
1258     PrintFatalError("unhandled type!");
1259   }
1260   if (quad) nElts <<= 1;
1261   return nElts;
1262 }
1263
1264 // Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
1265 static std::string GenOpString(OpKind op, const std::string &proto,
1266                                StringRef typestr) {
1267   bool quad;
1268   unsigned nElts = GetNumElements(typestr, quad);
1269   bool define = UseMacro(proto);
1270
1271   std::string ts = TypeString(proto[0], typestr);
1272   std::string s;
1273   if (!define) {
1274     s = "return ";
1275   }
1276
1277   switch(op) {
1278   case OpAdd:
1279     s += "__a + __b;";
1280     break;
1281   case OpAddl:
1282     s += Extend(typestr, "__a") + " + " + Extend(typestr, "__b") + ";";
1283     break;
1284   case OpAddw:
1285     s += "__a + " + Extend(typestr, "__b") + ";";
1286     break;
1287   case OpSub:
1288     s += "__a - __b;";
1289     break;
1290   case OpSubl:
1291     s += Extend(typestr, "__a") + " - " + Extend(typestr, "__b") + ";";
1292     break;
1293   case OpSubw:
1294     s += "__a - " + Extend(typestr, "__b") + ";";
1295     break;
1296   case OpMulN:
1297     s += "__a * " + Duplicate(nElts, typestr, "__b") + ";";
1298     break;
1299   case OpMulLane:
1300     s += "__a * " + SplatLane(nElts, "__b", "__c") + ";";
1301     break;
1302   case OpMul:
1303     s += "__a * __b;";
1304     break;
1305   case OpMullLane:
1306     s += MangleName("vmull", typestr, ClassS) + "(__a, " +
1307       SplatLane(nElts, "__b", "__c") + ");";
1308     break;
1309   case OpMlaN:
1310     s += "__a + (__b * " + Duplicate(nElts, typestr, "__c") + ");";
1311     break;
1312   case OpMlaLane:
1313     s += "__a + (__b * " + SplatLane(nElts, "__c", "__d") + ");";
1314     break;
1315   case OpMla:
1316     s += "__a + (__b * __c);";
1317     break;
1318   case OpMlalN:
1319     s += "__a + " + MangleName("vmull", typestr, ClassS) + "(__b, " +
1320       Duplicate(nElts, typestr, "__c") + ");";
1321     break;
1322   case OpMlalLane:
1323     s += "__a + " + MangleName("vmull", typestr, ClassS) + "(__b, " +
1324       SplatLane(nElts, "__c", "__d") + ");";
1325     break;
1326   case OpMlal:
1327     s += "__a + " + MangleName("vmull", typestr, ClassS) + "(__b, __c);";
1328     break;
1329   case OpMlsN:
1330     s += "__a - (__b * " + Duplicate(nElts, typestr, "__c") + ");";
1331     break;
1332   case OpMlsLane:
1333     s += "__a - (__b * " + SplatLane(nElts, "__c", "__d") + ");";
1334     break;
1335   case OpMls:
1336     s += "__a - (__b * __c);";
1337     break;
1338   case OpMlslN:
1339     s += "__a - " + MangleName("vmull", typestr, ClassS) + "(__b, " +
1340       Duplicate(nElts, typestr, "__c") + ");";
1341     break;
1342   case OpMlslLane:
1343     s += "__a - " + MangleName("vmull", typestr, ClassS) + "(__b, " +
1344       SplatLane(nElts, "__c", "__d") + ");";
1345     break;
1346   case OpMlsl:
1347     s += "__a - " + MangleName("vmull", typestr, ClassS) + "(__b, __c);";
1348     break;
1349   case OpQDMullLane:
1350     s += MangleName("vqdmull", typestr, ClassS) + "(__a, " +
1351       SplatLane(nElts, "__b", "__c") + ");";
1352     break;
1353   case OpQDMlalLane:
1354     s += MangleName("vqdmlal", typestr, ClassS) + "(__a, __b, " +
1355       SplatLane(nElts, "__c", "__d") + ");";
1356     break;
1357   case OpQDMlslLane:
1358     s += MangleName("vqdmlsl", typestr, ClassS) + "(__a, __b, " +
1359       SplatLane(nElts, "__c", "__d") + ");";
1360     break;
1361   case OpQDMulhLane:
1362     s += MangleName("vqdmulh", typestr, ClassS) + "(__a, " +
1363       SplatLane(nElts, "__b", "__c") + ");";
1364     break;
1365   case OpQRDMulhLane:
1366     s += MangleName("vqrdmulh", typestr, ClassS) + "(__a, " +
1367       SplatLane(nElts, "__b", "__c") + ");";
1368     break;
1369   case OpEq:
1370     s += "(" + ts + ")(__a == __b);";
1371     break;
1372   case OpGe:
1373     s += "(" + ts + ")(__a >= __b);";
1374     break;
1375   case OpLe:
1376     s += "(" + ts + ")(__a <= __b);";
1377     break;
1378   case OpGt:
1379     s += "(" + ts + ")(__a > __b);";
1380     break;
1381   case OpLt:
1382     s += "(" + ts + ")(__a < __b);";
1383     break;
1384   case OpNeg:
1385     s += " -__a;";
1386     break;
1387   case OpNot:
1388     s += " ~__a;";
1389     break;
1390   case OpAnd:
1391     s += "__a & __b;";
1392     break;
1393   case OpOr:
1394     s += "__a | __b;";
1395     break;
1396   case OpXor:
1397     s += "__a ^ __b;";
1398     break;
1399   case OpAndNot:
1400     s += "__a & ~__b;";
1401     break;
1402   case OpOrNot:
1403     s += "__a | ~__b;";
1404     break;
1405   case OpCast:
1406     s += "(" + ts + ")__a;";
1407     break;
1408   case OpConcat:
1409     s += "(" + ts + ")__builtin_shufflevector((int64x1_t)__a";
1410     s += ", (int64x1_t)__b, 0, 1);";
1411     break;
1412   case OpHi:
1413     s += "(" + ts +
1414       ")__builtin_shufflevector((int64x2_t)__a, (int64x2_t)__a, 1);";
1415     break;
1416   case OpLo:
1417     s += "(" + ts +
1418       ")__builtin_shufflevector((int64x2_t)__a, (int64x2_t)__a, 0);";
1419     break;
1420   case OpDup:
1421     s += Duplicate(nElts, typestr, "__a") + ";";
1422     break;
1423   case OpDupLane:
1424     s += SplatLane(nElts, "__a", "__b") + ";";
1425     break;
1426   case OpSelect:
1427     // ((0 & 1) | (~0 & 2))
1428     s += "(" + ts + ")";
1429     ts = TypeString(proto[1], typestr);
1430     s += "((__a & (" + ts + ")__b) | ";
1431     s += "(~__a & (" + ts + ")__c));";
1432     break;
1433   case OpRev16:
1434     s += "__builtin_shufflevector(__a, __a";
1435     for (unsigned i = 2; i <= nElts; i += 2)
1436       for (unsigned j = 0; j != 2; ++j)
1437         s += ", " + utostr(i - j - 1);
1438     s += ");";
1439     break;
1440   case OpRev32: {
1441     unsigned WordElts = nElts >> (1 + (int)quad);
1442     s += "__builtin_shufflevector(__a, __a";
1443     for (unsigned i = WordElts; i <= nElts; i += WordElts)
1444       for (unsigned j = 0; j != WordElts; ++j)
1445         s += ", " + utostr(i - j - 1);
1446     s += ");";
1447     break;
1448   }
1449   case OpRev64: {
1450     unsigned DblWordElts = nElts >> (int)quad;
1451     s += "__builtin_shufflevector(__a, __a";
1452     for (unsigned i = DblWordElts; i <= nElts; i += DblWordElts)
1453       for (unsigned j = 0; j != DblWordElts; ++j)
1454         s += ", " + utostr(i - j - 1);
1455     s += ");";
1456     break;
1457   }
1458   case OpAbdl: {
1459     std::string abd = MangleName("vabd", typestr, ClassS) + "(__a, __b)";
1460     if (typestr[0] != 'U') {
1461       // vabd results are always unsigned and must be zero-extended.
1462       std::string utype = "U" + typestr.str();
1463       s += "(" + TypeString(proto[0], typestr) + ")";
1464       abd = "(" + TypeString('d', utype) + ")" + abd;
1465       s += Extend(utype, abd) + ";";
1466     } else {
1467       s += Extend(typestr, abd) + ";";
1468     }
1469     break;
1470   }
1471   case OpAba:
1472     s += "__a + " + MangleName("vabd", typestr, ClassS) + "(__b, __c);";
1473     break;
1474   case OpAbal: {
1475     s += "__a + ";
1476     std::string abd = MangleName("vabd", typestr, ClassS) + "(__b, __c)";
1477     if (typestr[0] != 'U') {
1478       // vabd results are always unsigned and must be zero-extended.
1479       std::string utype = "U" + typestr.str();
1480       s += "(" + TypeString(proto[0], typestr) + ")";
1481       abd = "(" + TypeString('d', utype) + ")" + abd;
1482       s += Extend(utype, abd) + ";";
1483     } else {
1484       s += Extend(typestr, abd) + ";";
1485     }
1486     break;
1487   }
1488   default:
1489     PrintFatalError("unknown OpKind!");
1490   }
1491   return s;
1492 }
1493
1494 static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
1495   unsigned mod = proto[0];
1496
1497   if (mod == 'v' || mod == 'f')
1498     mod = proto[1];
1499
1500   bool quad = false;
1501   bool poly = false;
1502   bool usgn = false;
1503   bool scal = false;
1504   bool cnst = false;
1505   bool pntr = false;
1506
1507   // Base type to get the type string for.
1508   char type = ClassifyType(typestr, quad, poly, usgn);
1509
1510   // Based on the modifying character, change the type and width if necessary.
1511   type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
1512
1513   NeonTypeFlags::EltType ET;
1514   switch (type) {
1515     case 'c':
1516       ET = poly ? NeonTypeFlags::Poly8 : NeonTypeFlags::Int8;
1517       break;
1518     case 's':
1519       ET = poly ? NeonTypeFlags::Poly16 : NeonTypeFlags::Int16;
1520       break;
1521     case 'i':
1522       ET = NeonTypeFlags::Int32;
1523       break;
1524     case 'l':
1525       ET = NeonTypeFlags::Int64;
1526       break;
1527     case 'h':
1528       ET = NeonTypeFlags::Float16;
1529       break;
1530     case 'f':
1531       ET = NeonTypeFlags::Float32;
1532       break;
1533     default:
1534       PrintFatalError("unhandled type!");
1535   }
1536   NeonTypeFlags Flags(ET, usgn, quad && proto[1] != 'g');
1537   return Flags.getFlags();
1538 }
1539
1540 // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
1541 static std::string GenBuiltin(const std::string &name, const std::string &proto,
1542                               StringRef typestr, ClassKind ck) {
1543   std::string s;
1544
1545   // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
1546   // sret-like argument.
1547   bool sret = (proto[0] >= '2' && proto[0] <= '4');
1548
1549   bool define = UseMacro(proto);
1550
1551   // Check if the prototype has a scalar operand with the type of the vector
1552   // elements.  If not, bitcasting the args will take care of arg checking.
1553   // The actual signedness etc. will be taken care of with special enums.
1554   if (proto.find('s') == std::string::npos)
1555     ck = ClassB;
1556
1557   if (proto[0] != 'v') {
1558     std::string ts = TypeString(proto[0], typestr);
1559
1560     if (define) {
1561       if (sret)
1562         s += ts + " r; ";
1563       else
1564         s += "(" + ts + ")";
1565     } else if (sret) {
1566       s += ts + " r; ";
1567     } else {
1568       s += "return (" + ts + ")";
1569     }
1570   }
1571
1572   bool splat = proto.find('a') != std::string::npos;
1573
1574   s += "__builtin_neon_";
1575   if (splat) {
1576     // Call the non-splat builtin: chop off the "_n" suffix from the name.
1577     std::string vname(name, 0, name.size()-2);
1578     s += MangleName(vname, typestr, ck);
1579   } else {
1580     s += MangleName(name, typestr, ck);
1581   }
1582   s += "(";
1583
1584   // Pass the address of the return variable as the first argument to sret-like
1585   // builtins.
1586   if (sret)
1587     s += "&r, ";
1588
1589   char arg = 'a';
1590   for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
1591     std::string args = std::string(&arg, 1);
1592
1593     // Use the local temporaries instead of the macro arguments.
1594     args = "__" + args;
1595
1596     bool argQuad = false;
1597     bool argPoly = false;
1598     bool argUsgn = false;
1599     bool argScalar = false;
1600     bool dummy = false;
1601     char argType = ClassifyType(typestr, argQuad, argPoly, argUsgn);
1602     argType = ModType(proto[i], argType, argQuad, argPoly, argUsgn, argScalar,
1603                       dummy, dummy);
1604
1605     // Handle multiple-vector values specially, emitting each subvector as an
1606     // argument to the __builtin.
1607     if (proto[i] >= '2' && proto[i] <= '4') {
1608       // Check if an explicit cast is needed.
1609       if (argType != 'c' || argPoly || argUsgn)
1610         args = (argQuad ? "(int8x16_t)" : "(int8x8_t)") + args;
1611
1612       for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
1613         s += args + ".val[" + utostr(vi) + "]";
1614         if ((vi + 1) < ve)
1615           s += ", ";
1616       }
1617       if ((i + 1) < e)
1618         s += ", ";
1619
1620       continue;
1621     }
1622
1623     if (splat && (i + 1) == e)
1624       args = Duplicate(GetNumElements(typestr, argQuad), typestr, args);
1625
1626     // Check if an explicit cast is needed.
1627     if ((splat || !argScalar) &&
1628         ((ck == ClassB && argType != 'c') || argPoly || argUsgn)) {
1629       std::string argTypeStr = "c";
1630       if (ck != ClassB)
1631         argTypeStr = argType;
1632       if (argQuad)
1633         argTypeStr = "Q" + argTypeStr;
1634       args = "(" + TypeString('d', argTypeStr) + ")" + args;
1635     }
1636
1637     s += args;
1638     if ((i + 1) < e)
1639       s += ", ";
1640   }
1641
1642   // Extra constant integer to hold type class enum for this function, e.g. s8
1643   if (ck == ClassB)
1644     s += ", " + utostr(GetNeonEnum(proto, typestr));
1645
1646   s += ");";
1647
1648   if (proto[0] != 'v' && sret) {
1649     if (define)
1650       s += " r;";
1651     else
1652       s += " return r;";
1653   }
1654   return s;
1655 }
1656
1657 static std::string GenBuiltinDef(const std::string &name,
1658                                  const std::string &proto,
1659                                  StringRef typestr, ClassKind ck) {
1660   std::string s("BUILTIN(__builtin_neon_");
1661
1662   // If all types are the same size, bitcasting the args will take care
1663   // of arg checking.  The actual signedness etc. will be taken care of with
1664   // special enums.
1665   if (proto.find('s') == std::string::npos)
1666     ck = ClassB;
1667
1668   s += MangleName(name, typestr, ck);
1669   s += ", \"";
1670
1671   for (unsigned i = 0, e = proto.size(); i != e; ++i)
1672     s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
1673
1674   // Extra constant integer to hold type class enum for this function, e.g. s8
1675   if (ck == ClassB)
1676     s += "i";
1677
1678   s += "\", \"n\")";
1679   return s;
1680 }
1681
1682 static std::string GenIntrinsic(const std::string &name,
1683                                 const std::string &proto,
1684                                 StringRef outTypeStr, StringRef inTypeStr,
1685                                 OpKind kind, ClassKind classKind) {
1686   assert(!proto.empty() && "");
1687   bool define = UseMacro(proto) && kind != OpUnavailable;
1688   std::string s;
1689
1690   // static always inline + return type
1691   if (define)
1692     s += "#define ";
1693   else
1694     s += "__ai " + TypeString(proto[0], outTypeStr) + " ";
1695
1696   // Function name with type suffix
1697   std::string mangledName = MangleName(name, outTypeStr, ClassS);
1698   if (outTypeStr != inTypeStr) {
1699     // If the input type is different (e.g., for vreinterpret), append a suffix
1700     // for the input type.  String off a "Q" (quad) prefix so that MangleName
1701     // does not insert another "q" in the name.
1702     unsigned typeStrOff = (inTypeStr[0] == 'Q' ? 1 : 0);
1703     StringRef inTypeNoQuad = inTypeStr.substr(typeStrOff);
1704     mangledName = MangleName(mangledName, inTypeNoQuad, ClassS);
1705   }
1706   s += mangledName;
1707
1708   // Function arguments
1709   s += GenArgs(proto, inTypeStr);
1710
1711   // Definition.
1712   if (define) {
1713     s += " __extension__ ({ \\\n  ";
1714     s += GenMacroLocals(proto, inTypeStr);
1715   } else if (kind == OpUnavailable) {
1716     s += " __attribute__((unavailable));\n";
1717     return s;
1718   } else
1719     s += " {\n  ";
1720
1721   if (kind != OpNone)
1722     s += GenOpString(kind, proto, outTypeStr);
1723   else
1724     s += GenBuiltin(name, proto, outTypeStr, classKind);
1725   if (define)
1726     s += " })";
1727   else
1728     s += " }";
1729   s += "\n";
1730   return s;
1731 }
1732
1733 /// run - Read the records in arm_neon.td and output arm_neon.h.  arm_neon.h
1734 /// is comprised of type definitions and function declarations.
1735 void NeonEmitter::run(raw_ostream &OS) {
1736   OS << 
1737     "/*===---- arm_neon.h - ARM Neon intrinsics ------------------------------"
1738     "---===\n"
1739     " *\n"
1740     " * Permission is hereby granted, free of charge, to any person obtaining "
1741     "a copy\n"
1742     " * of this software and associated documentation files (the \"Software\"),"
1743     " to deal\n"
1744     " * in the Software without restriction, including without limitation the "
1745     "rights\n"
1746     " * to use, copy, modify, merge, publish, distribute, sublicense, "
1747     "and/or sell\n"
1748     " * copies of the Software, and to permit persons to whom the Software is\n"
1749     " * furnished to do so, subject to the following conditions:\n"
1750     " *\n"
1751     " * The above copyright notice and this permission notice shall be "
1752     "included in\n"
1753     " * all copies or substantial portions of the Software.\n"
1754     " *\n"
1755     " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, "
1756     "EXPRESS OR\n"
1757     " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
1758     "MERCHANTABILITY,\n"
1759     " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT "
1760     "SHALL THE\n"
1761     " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR "
1762     "OTHER\n"
1763     " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, "
1764     "ARISING FROM,\n"
1765     " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER "
1766     "DEALINGS IN\n"
1767     " * THE SOFTWARE.\n"
1768     " *\n"
1769     " *===--------------------------------------------------------------------"
1770     "---===\n"
1771     " */\n\n";
1772
1773   OS << "#ifndef __ARM_NEON_H\n";
1774   OS << "#define __ARM_NEON_H\n\n";
1775
1776   OS << "#ifndef __ARM_NEON__\n";
1777   OS << "#error \"NEON support not enabled\"\n";
1778   OS << "#endif\n\n";
1779
1780   OS << "#include <stdint.h>\n\n";
1781
1782   // Emit NEON-specific scalar typedefs.
1783   OS << "typedef float float32_t;\n";
1784   OS << "typedef int8_t poly8_t;\n";
1785   OS << "typedef int16_t poly16_t;\n";
1786   OS << "typedef uint16_t float16_t;\n";
1787
1788   // Emit Neon vector typedefs.
1789   std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
1790   SmallVector<StringRef, 24> TDTypeVec;
1791   ParseTypes(0, TypedefTypes, TDTypeVec);
1792
1793   // Emit vector typedefs.
1794   for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
1795     bool dummy, quad = false, poly = false;
1796     (void) ClassifyType(TDTypeVec[i], quad, poly, dummy);
1797     if (poly)
1798       OS << "typedef __attribute__((neon_polyvector_type(";
1799     else
1800       OS << "typedef __attribute__((neon_vector_type(";
1801
1802     unsigned nElts = GetNumElements(TDTypeVec[i], quad);
1803     OS << utostr(nElts) << "))) ";
1804     if (nElts < 10)
1805       OS << " ";
1806
1807     OS << TypeString('s', TDTypeVec[i]);
1808     OS << " " << TypeString('d', TDTypeVec[i]) << ";\n";
1809   }
1810   OS << "\n";
1811
1812   // Emit struct typedefs.
1813   for (unsigned vi = 2; vi != 5; ++vi) {
1814     for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
1815       std::string ts = TypeString('d', TDTypeVec[i]);
1816       std::string vs = TypeString('0' + vi, TDTypeVec[i]);
1817       OS << "typedef struct " << vs << " {\n";
1818       OS << "  " << ts << " val";
1819       OS << "[" << utostr(vi) << "]";
1820       OS << ";\n} ";
1821       OS << vs << ";\n\n";
1822     }
1823   }
1824
1825   OS<<"#define __ai static inline __attribute__((__always_inline__, __nodebug__))\n\n";
1826
1827   std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1828
1829   // Emit vmovl, vmull and vabd intrinsics first so they can be used by other
1830   // intrinsics.  (Some of the saturating multiply instructions are also
1831   // used to implement the corresponding "_lane" variants, but tablegen
1832   // sorts the records into alphabetical order so that the "_lane" variants
1833   // come after the intrinsics they use.)
1834   emitIntrinsic(OS, Records.getDef("VMOVL"));
1835   emitIntrinsic(OS, Records.getDef("VMULL"));
1836   emitIntrinsic(OS, Records.getDef("VABD"));
1837
1838   for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1839     Record *R = RV[i];
1840     if (R->getName() != "VMOVL" &&
1841         R->getName() != "VMULL" &&
1842         R->getName() != "VABD")
1843       emitIntrinsic(OS, R);
1844   }
1845
1846   OS << "#undef __ai\n\n";
1847   OS << "#endif /* __ARM_NEON_H */\n";
1848 }
1849
1850 /// emitIntrinsic - Write out the arm_neon.h header file definitions for the
1851 /// intrinsics specified by record R.
1852 void NeonEmitter::emitIntrinsic(raw_ostream &OS, Record *R) {
1853   std::string name = R->getValueAsString("Name");
1854   std::string Proto = R->getValueAsString("Prototype");
1855   std::string Types = R->getValueAsString("Types");
1856
1857   SmallVector<StringRef, 16> TypeVec;
1858   ParseTypes(R, Types, TypeVec);
1859
1860   OpKind kind = OpMap[R->getValueAsDef("Operand")->getName()];
1861
1862   ClassKind classKind = ClassNone;
1863   if (R->getSuperClasses().size() >= 2)
1864     classKind = ClassMap[R->getSuperClasses()[1]];
1865   if (classKind == ClassNone && kind == OpNone)
1866     PrintFatalError(R->getLoc(), "Builtin has no class kind");
1867
1868   for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1869     if (kind == OpReinterpret) {
1870       bool outQuad = false;
1871       bool dummy = false;
1872       (void)ClassifyType(TypeVec[ti], outQuad, dummy, dummy);
1873       for (unsigned srcti = 0, srcte = TypeVec.size();
1874            srcti != srcte; ++srcti) {
1875         bool inQuad = false;
1876         (void)ClassifyType(TypeVec[srcti], inQuad, dummy, dummy);
1877         if (srcti == ti || inQuad != outQuad)
1878           continue;
1879         OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[srcti],
1880                            OpCast, ClassS);
1881       }
1882     } else {
1883       OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[ti],
1884                          kind, classKind);
1885     }
1886   }
1887   OS << "\n";
1888 }
1889
1890 static unsigned RangeFromType(const char mod, StringRef typestr) {
1891   // base type to get the type string for.
1892   bool quad = false, dummy = false;
1893   char type = ClassifyType(typestr, quad, dummy, dummy);
1894   type = ModType(mod, type, quad, dummy, dummy, dummy, dummy, dummy);
1895
1896   switch (type) {
1897     case 'c':
1898       return (8 << (int)quad) - 1;
1899     case 'h':
1900     case 's':
1901       return (4 << (int)quad) - 1;
1902     case 'f':
1903     case 'i':
1904       return (2 << (int)quad) - 1;
1905     case 'l':
1906       return (1 << (int)quad) - 1;
1907     default:
1908       PrintFatalError("unhandled type!");
1909   }
1910 }
1911
1912 /// runHeader - Emit a file with sections defining:
1913 /// 1. the NEON section of BuiltinsARM.def.
1914 /// 2. the SemaChecking code for the type overload checking.
1915 /// 3. the SemaChecking code for validation of intrinsic immediate arguments.
1916 void NeonEmitter::runHeader(raw_ostream &OS) {
1917   std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1918
1919   StringMap<OpKind> EmittedMap;
1920
1921   // Generate BuiltinsARM.def for NEON
1922   OS << "#ifdef GET_NEON_BUILTINS\n";
1923   for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1924     Record *R = RV[i];
1925     OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1926     if (k != OpNone)
1927       continue;
1928
1929     std::string Proto = R->getValueAsString("Prototype");
1930
1931     // Functions with 'a' (the splat code) in the type prototype should not get
1932     // their own builtin as they use the non-splat variant.
1933     if (Proto.find('a') != std::string::npos)
1934       continue;
1935
1936     std::string Types = R->getValueAsString("Types");
1937     SmallVector<StringRef, 16> TypeVec;
1938     ParseTypes(R, Types, TypeVec);
1939
1940     if (R->getSuperClasses().size() < 2)
1941       PrintFatalError(R->getLoc(), "Builtin has no class kind");
1942
1943     std::string name = R->getValueAsString("Name");
1944     ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1945
1946     for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1947       // Generate the BuiltinsARM.def declaration for this builtin, ensuring
1948       // that each unique BUILTIN() macro appears only once in the output
1949       // stream.
1950       std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
1951       if (EmittedMap.count(bd))
1952         continue;
1953
1954       EmittedMap[bd] = OpNone;
1955       OS << bd << "\n";
1956     }
1957   }
1958   OS << "#endif\n\n";
1959
1960   // Generate the overloaded type checking code for SemaChecking.cpp
1961   OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
1962   for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1963     Record *R = RV[i];
1964     OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1965     if (k != OpNone)
1966       continue;
1967
1968     std::string Proto = R->getValueAsString("Prototype");
1969     std::string Types = R->getValueAsString("Types");
1970     std::string name = R->getValueAsString("Name");
1971
1972     // Functions with 'a' (the splat code) in the type prototype should not get
1973     // their own builtin as they use the non-splat variant.
1974     if (Proto.find('a') != std::string::npos)
1975       continue;
1976
1977     // Functions which have a scalar argument cannot be overloaded, no need to
1978     // check them if we are emitting the type checking code.
1979     if (Proto.find('s') != std::string::npos)
1980       continue;
1981
1982     SmallVector<StringRef, 16> TypeVec;
1983     ParseTypes(R, Types, TypeVec);
1984
1985     if (R->getSuperClasses().size() < 2)
1986       PrintFatalError(R->getLoc(), "Builtin has no class kind");
1987
1988     int si = -1, qi = -1;
1989     uint64_t mask = 0, qmask = 0;
1990     for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1991       // Generate the switch case(s) for this builtin for the type validation.
1992       bool quad = false, poly = false, usgn = false;
1993       (void) ClassifyType(TypeVec[ti], quad, poly, usgn);
1994
1995       if (quad) {
1996         qi = ti;
1997         qmask |= 1ULL << GetNeonEnum(Proto, TypeVec[ti]);
1998       } else {
1999         si = ti;
2000         mask |= 1ULL << GetNeonEnum(Proto, TypeVec[ti]);
2001       }
2002     }
2003
2004     // Check if the builtin function has a pointer or const pointer argument.
2005     int PtrArgNum = -1;
2006     bool HasConstPtr = false;
2007     for (unsigned arg = 1, arge = Proto.size(); arg != arge; ++arg) {
2008       char ArgType = Proto[arg];
2009       if (ArgType == 'c') {
2010         HasConstPtr = true;
2011         PtrArgNum = arg - 1;
2012         break;
2013       }
2014       if (ArgType == 'p') {
2015         PtrArgNum = arg - 1;
2016         break;
2017       }
2018     }
2019     // For sret builtins, adjust the pointer argument index.
2020     if (PtrArgNum >= 0 && (Proto[0] >= '2' && Proto[0] <= '4'))
2021       PtrArgNum += 1;
2022
2023     // Omit type checking for the pointer arguments of vld1_lane, vld1_dup,
2024     // and vst1_lane intrinsics.  Using a pointer to the vector element
2025     // type with one of those operations causes codegen to select an aligned
2026     // load/store instruction.  If you want an unaligned operation,
2027     // the pointer argument needs to have less alignment than element type,
2028     // so just accept any pointer type.
2029     if (name == "vld1_lane" || name == "vld1_dup" || name == "vst1_lane") {
2030       PtrArgNum = -1;
2031       HasConstPtr = false;
2032     }
2033
2034     if (mask) {
2035       OS << "case ARM::BI__builtin_neon_"
2036          << MangleName(name, TypeVec[si], ClassB)
2037          << ": mask = " << "0x" << utohexstr(mask) << "ULL";
2038       if (PtrArgNum >= 0)
2039         OS << "; PtrArgNum = " << PtrArgNum;
2040       if (HasConstPtr)
2041         OS << "; HasConstPtr = true";
2042       OS << "; break;\n";
2043     }
2044     if (qmask) {
2045       OS << "case ARM::BI__builtin_neon_"
2046          << MangleName(name, TypeVec[qi], ClassB)
2047          << ": mask = " << "0x" << utohexstr(qmask) << "ULL";
2048       if (PtrArgNum >= 0)
2049         OS << "; PtrArgNum = " << PtrArgNum;
2050       if (HasConstPtr)
2051         OS << "; HasConstPtr = true";
2052       OS << "; break;\n";
2053     }
2054   }
2055   OS << "#endif\n\n";
2056
2057   // Generate the intrinsic range checking code for shift/lane immediates.
2058   OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
2059   for (unsigned i = 0, e = RV.size(); i != e; ++i) {
2060     Record *R = RV[i];
2061
2062     OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
2063     if (k != OpNone)
2064       continue;
2065
2066     std::string name = R->getValueAsString("Name");
2067     std::string Proto = R->getValueAsString("Prototype");
2068     std::string Types = R->getValueAsString("Types");
2069
2070     // Functions with 'a' (the splat code) in the type prototype should not get
2071     // their own builtin as they use the non-splat variant.
2072     if (Proto.find('a') != std::string::npos)
2073       continue;
2074
2075     // Functions which do not have an immediate do not need to have range
2076     // checking code emitted.
2077     size_t immPos = Proto.find('i');
2078     if (immPos == std::string::npos)
2079       continue;
2080
2081     SmallVector<StringRef, 16> TypeVec;
2082     ParseTypes(R, Types, TypeVec);
2083
2084     if (R->getSuperClasses().size() < 2)
2085       PrintFatalError(R->getLoc(), "Builtin has no class kind");
2086
2087     ClassKind ck = ClassMap[R->getSuperClasses()[1]];
2088
2089     for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
2090       std::string namestr, shiftstr, rangestr;
2091
2092       if (R->getValueAsBit("isVCVT_N")) {
2093         // VCVT between floating- and fixed-point values takes an immediate
2094         // in the range 1 to 32.
2095         ck = ClassB;
2096         rangestr = "l = 1; u = 31"; // upper bound = l + u
2097       } else if (Proto.find('s') == std::string::npos) {
2098         // Builtins which are overloaded by type will need to have their upper
2099         // bound computed at Sema time based on the type constant.
2100         ck = ClassB;
2101         if (R->getValueAsBit("isShift")) {
2102           shiftstr = ", true";
2103
2104           // Right shifts have an 'r' in the name, left shifts do not.
2105           if (name.find('r') != std::string::npos)
2106             rangestr = "l = 1; ";
2107         }
2108         rangestr += "u = RFT(TV" + shiftstr + ")";
2109       } else {
2110         // The immediate generally refers to a lane in the preceding argument.
2111         assert(immPos > 0 && "unexpected immediate operand");
2112         rangestr = "u = " + utostr(RangeFromType(Proto[immPos-1], TypeVec[ti]));
2113       }
2114       // Make sure cases appear only once by uniquing them in a string map.
2115       namestr = MangleName(name, TypeVec[ti], ck);
2116       if (EmittedMap.count(namestr))
2117         continue;
2118       EmittedMap[namestr] = OpNone;
2119
2120       // Calculate the index of the immediate that should be range checked.
2121       unsigned immidx = 0;
2122
2123       // Builtins that return a struct of multiple vectors have an extra
2124       // leading arg for the struct return.
2125       if (Proto[0] >= '2' && Proto[0] <= '4')
2126         ++immidx;
2127
2128       // Add one to the index for each argument until we reach the immediate
2129       // to be checked.  Structs of vectors are passed as multiple arguments.
2130       for (unsigned ii = 1, ie = Proto.size(); ii != ie; ++ii) {
2131         switch (Proto[ii]) {
2132           default:  immidx += 1; break;
2133           case '2': immidx += 2; break;
2134           case '3': immidx += 3; break;
2135           case '4': immidx += 4; break;
2136           case 'i': ie = ii + 1; break;
2137         }
2138       }
2139       OS << "case ARM::BI__builtin_neon_" << MangleName(name, TypeVec[ti], ck)
2140          << ": i = " << immidx << "; " << rangestr << "; break;\n";
2141     }
2142   }
2143   OS << "#endif\n\n";
2144 }
2145
2146 /// GenTest - Write out a test for the intrinsic specified by the name and
2147 /// type strings, including the embedded patterns for FileCheck to match.
2148 static std::string GenTest(const std::string &name,
2149                            const std::string &proto,
2150                            StringRef outTypeStr, StringRef inTypeStr,
2151                            bool isShift, bool isHiddenLOp,
2152                            ClassKind ck, const std::string &InstName) {
2153   assert(!proto.empty() && "");
2154   std::string s;
2155
2156   // Function name with type suffix
2157   std::string mangledName = MangleName(name, outTypeStr, ClassS);
2158   if (outTypeStr != inTypeStr) {
2159     // If the input type is different (e.g., for vreinterpret), append a suffix
2160     // for the input type.  String off a "Q" (quad) prefix so that MangleName
2161     // does not insert another "q" in the name.
2162     unsigned typeStrOff = (inTypeStr[0] == 'Q' ? 1 : 0);
2163     StringRef inTypeNoQuad = inTypeStr.substr(typeStrOff);
2164     mangledName = MangleName(mangledName, inTypeNoQuad, ClassS);
2165   }
2166
2167   std::vector<std::string> FileCheckPatterns;
2168   GenerateChecksForIntrinsic(name, proto, outTypeStr, inTypeStr, ck, InstName,
2169                              isHiddenLOp, FileCheckPatterns);
2170
2171   // Emit the FileCheck patterns.
2172   s += "// CHECK: test_" + mangledName + "\n";
2173   // If for any reason we do not want to emit a check, mangledInst
2174   // will be the empty string.
2175   if (FileCheckPatterns.size()) {
2176     for (std::vector<std::string>::const_iterator i = FileCheckPatterns.begin(),
2177                                                   e = FileCheckPatterns.end();
2178          i != e;
2179          ++i) {
2180       s += "// CHECK: " + *i + "\n";
2181     }
2182   }
2183
2184   // Emit the start of the test function.
2185   s += TypeString(proto[0], outTypeStr) + " test_" + mangledName + "(";
2186   char arg = 'a';
2187   std::string comma;
2188   for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
2189     // Do not create arguments for values that must be immediate constants.
2190     if (proto[i] == 'i')
2191       continue;
2192     s += comma + TypeString(proto[i], inTypeStr) + " ";
2193     s.push_back(arg);
2194     comma = ", ";
2195   }
2196   s += ") {\n  ";
2197
2198   if (proto[0] != 'v')
2199     s += "return ";
2200   s += mangledName + "(";
2201   arg = 'a';
2202   for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
2203     if (proto[i] == 'i') {
2204       // For immediate operands, test the maximum value.
2205       if (isShift)
2206         s += "1"; // FIXME
2207       else
2208         // The immediate generally refers to a lane in the preceding argument.
2209         s += utostr(RangeFromType(proto[i-1], inTypeStr));
2210     } else {
2211       s.push_back(arg);
2212     }
2213     if ((i + 1) < e)
2214       s += ", ";
2215   }
2216   s += ");\n}\n\n";
2217   return s;
2218 }
2219
2220 /// runTests - Write out a complete set of tests for all of the Neon
2221 /// intrinsics.
2222 void NeonEmitter::runTests(raw_ostream &OS) {
2223   OS <<
2224     "// RUN: %clang_cc1 -triple thumbv7s-apple-darwin -target-abi apcs-gnu\\\n"
2225     "// RUN:  -target-cpu swift -ffreestanding -Os -S -o - %s\\\n"
2226     "// RUN:  | FileCheck %s\n"
2227     "\n"
2228     "#include <arm_neon.h>\n"
2229     "\n";
2230
2231   std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
2232   for (unsigned i = 0, e = RV.size(); i != e; ++i) {
2233     Record *R = RV[i];
2234     std::string name = R->getValueAsString("Name");
2235     std::string Proto = R->getValueAsString("Prototype");
2236     std::string Types = R->getValueAsString("Types");
2237     bool isShift = R->getValueAsBit("isShift");
2238     std::string InstName = R->getValueAsString("InstName");
2239     bool isHiddenLOp = R->getValueAsBit("isHiddenLInst");
2240
2241     SmallVector<StringRef, 16> TypeVec;
2242     ParseTypes(R, Types, TypeVec);
2243
2244     ClassKind ck = ClassMap[R->getSuperClasses()[1]];
2245     OpKind kind = OpMap[R->getValueAsDef("Operand")->getName()];
2246     if (kind == OpUnavailable)
2247       continue;
2248     for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
2249       if (kind == OpReinterpret) {
2250         bool outQuad = false;
2251         bool dummy = false;
2252         (void)ClassifyType(TypeVec[ti], outQuad, dummy, dummy);
2253         for (unsigned srcti = 0, srcte = TypeVec.size();
2254              srcti != srcte; ++srcti) {
2255           bool inQuad = false;
2256           (void)ClassifyType(TypeVec[srcti], inQuad, dummy, dummy);
2257           if (srcti == ti || inQuad != outQuad)
2258             continue;
2259           OS << GenTest(name, Proto, TypeVec[ti], TypeVec[srcti],
2260                         isShift, isHiddenLOp, ck, InstName);
2261         }
2262       } else {
2263         OS << GenTest(name, Proto, TypeVec[ti], TypeVec[ti],
2264                       isShift, isHiddenLOp, ck, InstName);
2265       }
2266     }
2267     OS << "\n";
2268   }
2269 }
2270
2271 namespace clang {
2272 void EmitNeon(RecordKeeper &Records, raw_ostream &OS) {
2273   NeonEmitter(Records).run(OS);
2274 }
2275 void EmitNeonSema(RecordKeeper &Records, raw_ostream &OS) {
2276   NeonEmitter(Records).runHeader(OS);
2277 }
2278 void EmitNeonTest(RecordKeeper &Records, raw_ostream &OS) {
2279   NeonEmitter(Records).runTests(OS);
2280 }
2281 } // End namespace clang