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