blob: e471f0709f47c4eea6100ed392faca48d85199e8 [file] [log] [blame]
Peter Collingbourne51d77772011-10-06 13:03:08 +00001//===- 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
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +000026#include "llvm/ADT/DenseMap.h"
Peter Collingbourne51d77772011-10-06 13:03:08 +000027#include "llvm/ADT/SmallString.h"
28#include "llvm/ADT/SmallVector.h"
29#include "llvm/ADT/StringExtras.h"
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +000030#include "llvm/ADT/StringMap.h"
David Blaikie7530c032012-01-17 06:56:22 +000031#include "llvm/Support/ErrorHandling.h"
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +000032#include "llvm/TableGen/Error.h"
33#include "llvm/TableGen/Record.h"
34#include "llvm/TableGen/TableGenBackend.h"
Peter Collingbourne51d77772011-10-06 13:03:08 +000035#include <string>
Peter Collingbourne51d77772011-10-06 13:03:08 +000036using namespace llvm;
37
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +000038enum 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,
Tim Northoverb793f0d2013-08-01 09:23:19 +000093 OpAbal,
94 OpDiv
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +000095};
96
97enum 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
Michael Gottesman21e4e942013-04-16 21:18:42 +0000102 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.
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000108};
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.
113namespace {
114class NeonTypeFlags {
115 enum {
116 EltTypeMask = 0xf,
117 UnsignedFlag = 0x10,
118 QuadFlag = 0x20
119 };
120 uint32_t Flags;
121
122public:
123 enum EltType {
124 Int8,
125 Int16,
126 Int32,
127 Int64,
128 Poly8,
129 Poly16,
130 Float16,
Tim Northoverb793f0d2013-08-01 09:23:19 +0000131 Float32,
132 Float64
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000133 };
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
147namespace {
148class NeonEmitter {
149 RecordKeeper &Records;
150 StringMap<OpKind> OpMap;
151 DenseMap<Record*, ClassKind> ClassMap;
152
153public:
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;
Tim Northoverb793f0d2013-08-01 09:23:19 +0000210 OpMap["OP_DIV"] = OpDiv;
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000211
212 Record *SI = R.getClass("SInst");
213 Record *II = R.getClass("IInst");
214 Record *WI = R.getClass("WInst");
Michael Gottesman21e4e942013-04-16 21:18:42 +0000215 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
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000221 ClassMap[SI] = ClassS;
222 ClassMap[II] = ClassI;
223 ClassMap[WI] = ClassW;
Michael Gottesman21e4e942013-04-16 21:18:42 +0000224 ClassMap[SOpI] = ClassS;
225 ClassMap[IOpI] = ClassI;
226 ClassMap[WOpI] = ClassW;
227 ClassMap[LOpI] = ClassL;
228 ClassMap[NoTestOpI] = ClassNoTest;
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000229 }
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
240private:
Tim Northoverb793f0d2013-08-01 09:23:19 +0000241 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);
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000253};
254} // end anonymous namespace
255
Peter Collingbourne51d77772011-10-06 13:03:08 +0000256/// 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.
260static 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) {
Hao Liu12cd6a82013-08-15 08:26:30 +0000266 if (data[len] == 'P' || data[len] == 'Q' || data[len] == 'U'
267 || data[len] == 'H')
Peter Collingbourne51d77772011-10-06 13:03:08 +0000268 continue;
269
270 switch (data[len]) {
271 case 'c':
272 case 's':
273 case 'i':
274 case 'l':
275 case 'h':
276 case 'f':
Tim Northoverb793f0d2013-08-01 09:23:19 +0000277 case 'd':
Peter Collingbourne51d77772011-10-06 13:03:08 +0000278 break;
279 default:
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +0000280 PrintFatalError(r->getLoc(),
Peter Collingbourne51d77772011-10-06 13:03:08 +0000281 "Unexpected letter: " + std::string(data + len, 1));
Peter Collingbourne51d77772011-10-06 13:03:08 +0000282 }
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.
291static 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';
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +0000301 default:
302 PrintFatalError("unhandled type in widen!");
Peter Collingbourne51d77772011-10-06 13:03:08 +0000303 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000304}
305
306/// Narrow - Convert a type code into the next smaller type. short -> char,
307/// float -> half float, etc.
308static 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';
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +0000318 default:
319 PrintFatalError("unhandled type in narrow!");
Peter Collingbourne51d77772011-10-06 13:03:08 +0000320 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000321}
322
323/// For a particular StringRef, return the base type code, and whether it has
324/// the quad-vector, polynomial, or unsigned modifiers set.
325static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) {
326 unsigned off = 0;
327
328 // remember quad.
Hao Liu12cd6a82013-08-15 08:26:30 +0000329 if (ty[off] == 'Q' || ty[off] == 'H') {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000330 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.
352static 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';
Tim Northoverb793f0d2013-08-01 09:23:19 +0000366 if (type == 'd')
367 type = 'l';
Peter Collingbourne51d77772011-10-06 13:03:08 +0000368 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.
430static 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;
Tim Northoverb793f0d2013-08-01 09:23:19 +0000491 case 'd':
492 s += "float64";
493 if (scal)
494 break;
495 s += quad ? "x2" : "x1";
496 break;
497
Peter Collingbourne51d77772011-10-06 13:03:08 +0000498 default:
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +0000499 PrintFatalError("unhandled type!");
Peter Collingbourne51d77772011-10-06 13:03:08 +0000500 }
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.
524static 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
Michael Gottesmanfb599a42013-04-16 22:07:30 +0000616/// InstructionTypeCode - Computes the ARM argument character code and
617/// quad status for a specific type string and ClassKind.
618static 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;
Tim Northoverb793f0d2013-08-01 09:23:19 +0000675 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;
Michael Gottesmanfb599a42013-04-16 22:07:30 +0000687 default:
688 PrintFatalError("unhandled type!");
689 }
690}
691
Peter Collingbourne51d77772011-10-06 13:03:08 +0000692/// MangleName - Append a type or width suffix to a base neon function name,
Hao Liu12cd6a82013-08-15 08:26:30 +0000693/// 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.
Peter Collingbourne51d77772011-10-06 13:03:08 +0000695static 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;
Michael Gottesmanfb599a42013-04-16 22:07:30 +0000701 std::string typeCode = "";
702
703 InstructionTypeCode(typestr, ck, quad, typeCode);
Peter Collingbourne51d77772011-10-06 13:03:08 +0000704
705 std::string s = name;
706
Michael Gottesmanfb599a42013-04-16 22:07:30 +0000707 if (typeCode.size() > 0) {
708 s += "_" + typeCode;
Peter Collingbourne51d77772011-10-06 13:03:08 +0000709 }
Michael Gottesmanfb599a42013-04-16 22:07:30 +0000710
Peter Collingbourne51d77772011-10-06 13:03:08 +0000711 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.
Hao Liu12cd6a82013-08-15 08:26:30 +0000716 if (typestr.startswith("Q")) {
717 size_t pos = s.find('_');
718 s = s.insert(pos, "q");
Peter Collingbourne51d77772011-10-06 13:03:08 +0000719 }
Michael Gottesmanc327f872013-04-16 23:00:26 +0000720
Peter Collingbourne51d77772011-10-06 13:03:08 +0000721 return s;
722}
723
Michael Gottesmanc327f872013-04-16 23:00:26 +0000724static 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
756static void
757GenerateRegisterCheckPatternForLoadStores(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") {
Michael Gottesman410c3f72013-06-24 21:25:37 +0000810 RegisterSuffix += ":" + OutTypeCode;
Michael Gottesmanc327f872013-04-16 23:00:26 +0000811 }
812 }
813
814 RegisterSuffix += "]";
815 }
816}
817
818static 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
831static 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
844static 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
853static 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.
936static 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.
1016static 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
Peter Collingbourne51d77772011-10-06 13:03:08 +00001169/// UseMacro - Examine the prototype string to determine if the intrinsic
1170/// should be defined as a preprocessor macro instead of an inline function.
1171static 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.
1190static 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, ...)"
1196static 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.
1224static 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!
Peter Collingbourne51d77772011-10-06 13:03:08 +00001232 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.
1249static 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
1256static 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
1271static 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
1280static 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;
Tim Northoverb793f0d2013-08-01 09:23:19 +00001292 case 'd':
1293 nElts = 1;
1294 break;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001295 default:
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +00001296 PrintFatalError("unhandled type!");
Peter Collingbourne51d77772011-10-06 13:03:08 +00001297 }
1298 if (quad) nElts <<= 1;
1299 return nElts;
1300}
1301
1302// Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
1303static 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:
Jim Grosbachcd765392013-05-15 02:40:04 +00001451 // 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+= ");";
Peter Collingbourne51d77772011-10-06 13:03:08 +00001456 break;
1457 case OpLo:
Jim Grosbachcd765392013-05-15 02:40:04 +00001458 s += "__builtin_shufflevector(__a, __a";
1459 for (unsigned i = 0; i < nElts; ++i)
1460 s += ", " + utostr(i);
1461 s+= ");";
Peter Collingbourne51d77772011-10-06 13:03:08 +00001462 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 }
Tim Northoverb793f0d2013-08-01 09:23:19 +00001531 case OpDiv:
1532 s += "__a / __b;";
1533 break;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001534 default:
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +00001535 PrintFatalError("unknown OpKind!");
Peter Collingbourne51d77772011-10-06 13:03:08 +00001536 }
1537 return s;
1538}
1539
1540static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
1541 unsigned mod = proto[0];
Peter Collingbourne51d77772011-10-06 13:03:08 +00001542
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
Bob Wilsonda95f732011-11-08 01:16:11 +00001559 NeonTypeFlags::EltType ET;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001560 switch (type) {
1561 case 'c':
Bob Wilsonda95f732011-11-08 01:16:11 +00001562 ET = poly ? NeonTypeFlags::Poly8 : NeonTypeFlags::Int8;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001563 break;
1564 case 's':
Bob Wilsonda95f732011-11-08 01:16:11 +00001565 ET = poly ? NeonTypeFlags::Poly16 : NeonTypeFlags::Int16;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001566 break;
1567 case 'i':
Bob Wilsonda95f732011-11-08 01:16:11 +00001568 ET = NeonTypeFlags::Int32;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001569 break;
1570 case 'l':
Bob Wilsonda95f732011-11-08 01:16:11 +00001571 ET = NeonTypeFlags::Int64;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001572 break;
1573 case 'h':
Bob Wilsonda95f732011-11-08 01:16:11 +00001574 ET = NeonTypeFlags::Float16;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001575 break;
1576 case 'f':
Bob Wilsonda95f732011-11-08 01:16:11 +00001577 ET = NeonTypeFlags::Float32;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001578 break;
Tim Northoverb793f0d2013-08-01 09:23:19 +00001579 case 'd':
1580 ET = NeonTypeFlags::Float64;
1581 break;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001582 default:
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +00001583 PrintFatalError("unhandled type!");
Peter Collingbourne51d77772011-10-06 13:03:08 +00001584 }
Bob Wilsonda95f732011-11-08 01:16:11 +00001585 NeonTypeFlags Flags(ET, usgn, quad && proto[1] != 'g');
1586 return Flags.getFlags();
Peter Collingbourne51d77772011-10-06 13:03:08 +00001587}
1588
1589// Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
1590static 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
1706static 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
1731static 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() && "");
Jim Grosbach667381b2012-05-09 18:17:30 +00001736 bool define = UseMacro(proto) && kind != OpUnavailable;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001737 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);
Jim Grosbach667381b2012-05-09 18:17:30 +00001764 } else if (kind == OpUnavailable) {
1765 s += " __attribute__((unavailable));\n";
1766 return s;
1767 } else
Jim Grosbach66981c72012-08-03 17:30:46 +00001768 s += " {\n ";
Peter Collingbourne51d77772011-10-06 13:03:08 +00001769
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.
1784void 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
Tim Northoverb793f0d2013-08-01 09:23:19 +00001825 OS << "#if !defined(__ARM_NEON__) && !defined(__AARCH_FEATURE_ADVSIMD)\n";
Peter Collingbourne51d77772011-10-06 13:03:08 +00001826 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";
Tim Northoverb793f0d2013-08-01 09:23:19 +00001833 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";
Peter Collingbourne51d77772011-10-06 13:03:08 +00001844 OS << "typedef int8_t poly8_t;\n";
1845 OS << "typedef int16_t poly16_t;\n";
Tim Northoverb793f0d2013-08-01 09:23:19 +00001846 OS << "#endif\n";
Peter Collingbourne51d77772011-10-06 13:03:08 +00001847
1848 // Emit Neon vector typedefs.
Tim Northoverb793f0d2013-08-01 09:23:19 +00001849 std::string TypedefTypes(
1850 "cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfQdPcQPcPsQPs");
Peter Collingbourne51d77772011-10-06 13:03:08 +00001851 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;
Tim Northoverb793f0d2013-08-01 09:23:19 +00001857 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
Peter Collingbourne51d77772011-10-06 13:03:08 +00001866 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";
Tim Northoverb793f0d2013-08-01 09:23:19 +00001878
1879 if (isA64)
1880 OS << "#endif\n";
Peter Collingbourne51d77772011-10-06 13:03:08 +00001881 }
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) {
Tim Northoverb793f0d2013-08-01 09:23:19 +00001887 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
Peter Collingbourne51d77772011-10-06 13:03:08 +00001897 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} ";
Tim Northoverb793f0d2013-08-01 09:23:19 +00001903 OS << vs << ";\n";
1904
1905 if (isA64)
1906 OS << "#endif\n";
1907
1908 OS << "\n";
Peter Collingbourne51d77772011-10-06 13:03:08 +00001909 }
1910 }
1911
Bob Wilson1e8058f2013-04-12 20:17:20 +00001912 OS<<"#define __ai static inline __attribute__((__always_inline__, __nodebug__))\n\n";
Peter Collingbourne51d77772011-10-06 13:03:08 +00001913
1914 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1915
Tim Northoverb793f0d2013-08-01 09:23:19 +00001916 StringMap<ClassKind> EmittedMap;
1917
Peter Collingbourne51d77772011-10-06 13:03:08 +00001918 // 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.)
Tim Northoverb793f0d2013-08-01 09:23:19 +00001923 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";
Peter Collingbourne51d77772011-10-06 13:03:08 +00001946
1947 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1948 Record *R = RV[i];
Tim Northoverb793f0d2013-08-01 09:23:19 +00001949
1950 // Skip ARM intrinsics already included above.
1951 bool isA64 = R->getValueAsBit("isA64");
1952 if (!isA64)
1953 continue;
1954
1955 emitIntrinsic(OS, R, EmittedMap);
Peter Collingbourne51d77772011-10-06 13:03:08 +00001956 }
1957
Tim Northoverb793f0d2013-08-01 09:23:19 +00001958 OS << "#endif\n\n";
1959
Peter Collingbourne51d77772011-10-06 13:03:08 +00001960 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
Tim Northoverb793f0d2013-08-01 09:23:19 +00001965/// intrinsics specified by record R checking for intrinsic uniqueness.
1966void NeonEmitter::emitIntrinsic(raw_ostream &OS, Record *R,
1967 StringMap<ClassKind> &EmittedMap) {
Peter Collingbourne51d77772011-10-06 13:03:08 +00001968 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)
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +00001981 PrintFatalError(R->getLoc(), "Builtin has no class kind");
Peter Collingbourne51d77772011-10-06 13:03:08 +00001982
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;
Tim Northoverb793f0d2013-08-01 09:23:19 +00001994 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;
Peter Collingbourne51d77772011-10-06 13:03:08 +00002000 }
2001 } else {
Tim Northoverb793f0d2013-08-01 09:23:19 +00002002 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;
Peter Collingbourne51d77772011-10-06 13:03:08 +00002008 }
2009 }
2010 OS << "\n";
2011}
2012
2013static 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:
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +00002031 PrintFatalError("unhandled type!");
Peter Collingbourne51d77772011-10-06 13:03:08 +00002032 }
Peter Collingbourne51d77772011-10-06 13:03:08 +00002033}
2034
Tim Northoverb793f0d2013-08-01 09:23:19 +00002035/// Generate the ARM and AArch64 intrinsic range checking code for
2036/// shift/lane immediates, checking for unique declarations.
2037void
2038NeonEmitter::genIntrinsicRangeCheckCode(raw_ostream &OS,
2039 StringMap<ClassKind> &A64IntrinsicMap,
2040 bool isA64RangeCheck) {
2041 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
Peter Collingbourne51d77772011-10-06 13:03:08 +00002042 StringMap<OpKind> EmittedMap;
2043
Tim Northoverb793f0d2013-08-01 09:23:19 +00002044 // 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
Peter Collingbourne51d77772011-10-06 13:03:08 +00002050 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
2051 Record *R = RV[i];
Tim Northoverb793f0d2013-08-01 09:23:19 +00002052
Peter Collingbourne51d77772011-10-06 13:03:08 +00002053 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
2054 if (k != OpNone)
2055 continue;
2056
Tim Northoverb793f0d2013-08-01 09:23:19 +00002057 std::string name = R->getValueAsString("Name");
Peter Collingbourne51d77772011-10-06 13:03:08 +00002058 std::string Proto = R->getValueAsString("Prototype");
Tim Northoverb793f0d2013-08-01 09:23:19 +00002059 std::string Types = R->getValueAsString("Types");
Peter Collingbourne51d77772011-10-06 13:03:08 +00002060
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
Tim Northoverb793f0d2013-08-01 09:23:19 +00002066 // 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
Peter Collingbourne51d77772011-10-06 13:03:08 +00002072 SmallVector<StringRef, 16> TypeVec;
2073 ParseTypes(R, Types, TypeVec);
2074
2075 if (R->getSuperClasses().size() < 2)
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +00002076 PrintFatalError(R->getLoc(), "Builtin has no class kind");
Peter Collingbourne51d77772011-10-06 13:03:08 +00002077
Peter Collingbourne51d77772011-10-06 13:03:08 +00002078 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
2079
Tim Northoverb793f0d2013-08-01 09:23:19 +00002080 // Do not include AArch64 range checks if not generating code for AArch64.
2081 bool isA64 = R->getValueAsBit("isA64");
2082 if (!isA64RangeCheck && isA64)
2083 continue;
Peter Collingbourne51d77772011-10-06 13:03:08 +00002084
Tim Northoverb793f0d2013-08-01 09:23:19 +00002085 // 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";
Peter Collingbourne51d77772011-10-06 13:03:08 +00002160 }
2161 }
2162 OS << "#endif\n\n";
Tim Northoverb793f0d2013-08-01 09:23:19 +00002163}
2164
2165/// Generate the ARM and AArch64 overloaded type checking code for
2166/// SemaChecking.cpp, checking for unique builtin declarations.
2167void
2168NeonEmitter::genOverloadTypeCheckCode(raw_ostream &OS,
2169 StringMap<ClassKind> &A64IntrinsicMap,
2170 bool isA64TypeCheck) {
2171 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
2172 StringMap<OpKind> EmittedMap;
Peter Collingbourne51d77772011-10-06 13:03:08 +00002173
2174 // Generate the overloaded type checking code for SemaChecking.cpp
Tim Northoverb793f0d2013-08-01 09:23:19 +00002175 if (isA64TypeCheck)
2176 OS << "#ifdef GET_NEON_AARCH64_OVERLOAD_CHECK\n";
2177 else
2178 OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
2179
Peter Collingbourne51d77772011-10-06 13:03:08 +00002180 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)
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +00002204 PrintFatalError(R->getLoc(), "Builtin has no class kind");
Peter Collingbourne51d77772011-10-06 13:03:08 +00002205
Tim Northoverb793f0d2013-08-01 09:23:19 +00002206 // 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
Peter Collingbourne51d77772011-10-06 13:03:08 +00002221 int si = -1, qi = -1;
Richard Smithf8ee6bc2012-08-14 01:28:02 +00002222 uint64_t mask = 0, qmask = 0;
Peter Collingbourne51d77772011-10-06 13:03:08 +00002223 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;
Richard Smithf8ee6bc2012-08-14 01:28:02 +00002230 qmask |= 1ULL << GetNeonEnum(Proto, TypeVec[ti]);
Peter Collingbourne51d77772011-10-06 13:03:08 +00002231 } else {
2232 si = ti;
Richard Smithf8ee6bc2012-08-14 01:28:02 +00002233 mask |= 1ULL << GetNeonEnum(Proto, TypeVec[ti]);
Peter Collingbourne51d77772011-10-06 13:03:08 +00002234 }
2235 }
Bob Wilson46482552011-11-16 21:32:23 +00002236
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
Bob Wilson9082cdd2011-12-20 06:16:48 +00002256 // 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
Bob Wilson6f9f03e2011-11-08 05:04:11 +00002267 if (mask) {
Tim Northoverb793f0d2013-08-01 09:23:19 +00002268 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";
Bob Wilson46482552011-11-16 21:32:23 +00002274 if (PtrArgNum >= 0)
2275 OS << "; PtrArgNum = " << PtrArgNum;
Bob Wilson6f9f03e2011-11-08 05:04:11 +00002276 if (HasConstPtr)
2277 OS << "; HasConstPtr = true";
2278 OS << "; break;\n";
2279 }
2280 if (qmask) {
Tim Northoverb793f0d2013-08-01 09:23:19 +00002281 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";
Bob Wilson46482552011-11-16 21:32:23 +00002287 if (PtrArgNum >= 0)
2288 OS << "; PtrArgNum = " << PtrArgNum;
Bob Wilson6f9f03e2011-11-08 05:04:11 +00002289 if (HasConstPtr)
2290 OS << "; HasConstPtr = true";
2291 OS << "; break;\n";
2292 }
Peter Collingbourne51d77772011-10-06 13:03:08 +00002293 }
2294 OS << "#endif\n\n";
Tim Northoverb793f0d2013-08-01 09:23:19 +00002295}
Peter Collingbourne51d77772011-10-06 13:03:08 +00002296
Tim Northoverb793f0d2013-08-01 09:23:19 +00002297/// genBuiltinsDef: Generate the BuiltinsARM.def and BuiltinsAArch64.def
2298/// declaration of builtins, checking for unique builtin declarations.
2299void 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
Peter Collingbourne51d77772011-10-06 13:03:08 +00002311 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
2312 Record *R = RV[i];
Peter Collingbourne51d77772011-10-06 13:03:08 +00002313 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
2314 if (k != OpNone)
2315 continue;
2316
Peter Collingbourne51d77772011-10-06 13:03:08 +00002317 std::string Proto = R->getValueAsString("Prototype");
Tim Northoverb793f0d2013-08-01 09:23:19 +00002318 std::string name = R->getValueAsString("Name");
Peter Collingbourne51d77772011-10-06 13:03:08 +00002319
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
Tim Northoverb793f0d2013-08-01 09:23:19 +00002325 std::string Types = R->getValueAsString("Types");
Peter Collingbourne51d77772011-10-06 13:03:08 +00002326 SmallVector<StringRef, 16> TypeVec;
2327 ParseTypes(R, Types, TypeVec);
2328
2329 if (R->getSuperClasses().size() < 2)
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +00002330 PrintFatalError(R->getLoc(), "Builtin has no class kind");
Peter Collingbourne51d77772011-10-06 13:03:08 +00002331
2332 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
2333
Tim Northoverb793f0d2013-08-01 09:23:19 +00002334 // 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;
Peter Collingbourne51d77772011-10-06 13:03:08 +00002339
Tim Northoverb793f0d2013-08-01 09:23:19 +00002340 // 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)
Peter Collingbourne51d77772011-10-06 13:03:08 +00002346 continue;
Tim Northoverb793f0d2013-08-01 09:23:19 +00002347 }
Peter Collingbourne51d77772011-10-06 13:03:08 +00002348
Tim Northoverb793f0d2013-08-01 09:23:19 +00002349 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;
Peter Collingbourne51d77772011-10-06 13:03:08 +00002356
Tim Northoverb793f0d2013-08-01 09:23:19 +00002357 EmittedMap[bd] = OpNone;
2358 OS << bd << "\n";
Peter Collingbourne51d77772011-10-06 13:03:08 +00002359 }
2360 }
2361 OS << "#endif\n\n";
2362}
2363
Tim Northoverb793f0d2013-08-01 09:23:19 +00002364/// 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.
2368void 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
Peter Collingbourne51d77772011-10-06 13:03:08 +00002409/// GenTest - Write out a test for the intrinsic specified by the name and
2410/// type strings, including the embedded patterns for FileCheck to match.
2411static std::string GenTest(const std::string &name,
2412 const std::string &proto,
2413 StringRef outTypeStr, StringRef inTypeStr,
Michael Gottesman7200bd62013-04-16 22:48:52 +00002414 bool isShift, bool isHiddenLOp,
Tim Northoverb793f0d2013-08-01 09:23:19 +00002415 ClassKind ck, const std::string &InstName,
2416 bool isA64,
2417 std::string & testFuncProto) {
Peter Collingbourne51d77772011-10-06 13:03:08 +00002418 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
Tim Northoverb793f0d2013-08-01 09:23:19 +00002432 // todo: GenerateChecksForIntrinsic does not generate CHECK
2433 // for aarch64 instructions yet
Michael Gottesmanc327f872013-04-16 23:00:26 +00002434 std::vector<std::string> FileCheckPatterns;
Tim Northoverb793f0d2013-08-01 09:23:19 +00002435 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";
Michael Gottesmanc327f872013-04-16 23:00:26 +00002441
Peter Collingbourne51d77772011-10-06 13:03:08 +00002442 // Emit the FileCheck patterns.
Michael Gottesmanc327f872013-04-16 23:00:26 +00002443 // 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) {
Tim Northoverb793f0d2013-08-01 09:23:19 +00002450 s += "// CHECK_ARM: " + *i + "\n";
Michael Gottesmanc327f872013-04-16 23:00:26 +00002451 }
2452 }
Peter Collingbourne51d77772011-10-06 13:03:08 +00002453
2454 // Emit the start of the test function.
Tim Northoverb793f0d2013-08-01 09:23:19 +00002455
2456 testFuncProto = TypeString(proto[0], outTypeStr) + " test_" + mangledName + "(";
Peter Collingbourne51d77772011-10-06 13:03:08 +00002457 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;
Tim Northoverb793f0d2013-08-01 09:23:19 +00002463 testFuncProto += comma + TypeString(proto[i], inTypeStr) + " ";
2464 testFuncProto.push_back(arg);
Peter Collingbourne51d77772011-10-06 13:03:08 +00002465 comma = ", ";
2466 }
Tim Northoverb793f0d2013-08-01 09:23:19 +00002467 testFuncProto += ")";
2468
2469 s+= testFuncProto;
2470 s+= " {\n ";
Peter Collingbourne51d77772011-10-06 13:03:08 +00002471
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
Tim Northoverb793f0d2013-08-01 09:23:19 +00002494/// Write out all intrinsic tests for the specified target, checking
2495/// for intrinsic test uniqueness.
2496void NeonEmitter::genTargetTest(raw_ostream &OS, StringMap<OpKind> &EmittedMap,
2497 bool isA64GenTest) {
2498 if (isA64GenTest)
2499 OS << "#ifdef __aarch64__\n";
Peter Collingbourne51d77772011-10-06 13:03:08 +00002500
Tim Northoverb793f0d2013-08-01 09:23:19 +00002501 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
Peter Collingbourne51d77772011-10-06 13:03:08 +00002502 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");
Michael Gottesman7200bd62013-04-16 22:48:52 +00002508 std::string InstName = R->getValueAsString("InstName");
2509 bool isHiddenLOp = R->getValueAsBit("isHiddenLInst");
Tim Northoverb793f0d2013-08-01 09:23:19 +00002510 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;
Peter Collingbourne51d77772011-10-06 13:03:08 +00002516
2517 SmallVector<StringRef, 16> TypeVec;
2518 ParseTypes(R, Types, TypeVec);
2519
Michael Gottesman7200bd62013-04-16 22:48:52 +00002520 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
Peter Collingbourne51d77772011-10-06 13:03:08 +00002521 OpKind kind = OpMap[R->getValueAsDef("Operand")->getName()];
Jim Grosbach667381b2012-05-09 18:17:30 +00002522 if (kind == OpUnavailable)
2523 continue;
Peter Collingbourne51d77772011-10-06 13:03:08 +00002524 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;
Tim Northoverb793f0d2013-08-01 09:23:19 +00002535 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";
Peter Collingbourne51d77772011-10-06 13:03:08 +00002543 }
2544 } else {
Tim Northoverb793f0d2013-08-01 09:23:19 +00002545 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";
Peter Collingbourne51d77772011-10-06 13:03:08 +00002552 }
2553 }
Peter Collingbourne51d77772011-10-06 13:03:08 +00002554 }
Tim Northoverb793f0d2013-08-01 09:23:19 +00002555
2556 if (isA64GenTest)
2557 OS << "#endif\n";
2558}
2559/// runTests - Write out a complete set of tests for all of the Neon
2560/// intrinsics.
2561void 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);
Peter Collingbourne51d77772011-10-06 13:03:08 +00002585}
2586
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +00002587namespace clang {
2588void EmitNeon(RecordKeeper &Records, raw_ostream &OS) {
2589 NeonEmitter(Records).run(OS);
2590}
2591void EmitNeonSema(RecordKeeper &Records, raw_ostream &OS) {
2592 NeonEmitter(Records).runHeader(OS);
2593}
2594void EmitNeonTest(RecordKeeper &Records, raw_ostream &OS) {
2595 NeonEmitter(Records).runTests(OS);
2596}
2597} // End namespace clang