blob: 257bf6572a75d0d87ea96f7e80e59f86f38a44d4 [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,
93 OpAbal
94};
95
96enum ClassKind {
97 ClassNone,
98 ClassI, // generic integer instruction, e.g., "i8" suffix
99 ClassS, // signed/unsigned/poly, e.g., "s8", "u8" or "p8" suffix
100 ClassW, // width-specific instruction, e.g., "8" suffix
Michael Gottesman21e4e942013-04-16 21:18:42 +0000101 ClassB, // bitcast arguments with enum argument to specify type
102 ClassL, // Logical instructions which are op instructions
103 // but we need to not emit any suffix for in our
104 // tests.
105 ClassNoTest // Instructions which we do not test since they are
106 // not TRUE instructions.
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000107};
108
109/// NeonTypeFlags - Flags to identify the types for overloaded Neon
110/// builtins. These must be kept in sync with the flags in
111/// include/clang/Basic/TargetBuiltins.h.
112namespace {
113class NeonTypeFlags {
114 enum {
115 EltTypeMask = 0xf,
116 UnsignedFlag = 0x10,
117 QuadFlag = 0x20
118 };
119 uint32_t Flags;
120
121public:
122 enum EltType {
123 Int8,
124 Int16,
125 Int32,
126 Int64,
127 Poly8,
128 Poly16,
129 Float16,
130 Float32
131 };
132
133 NeonTypeFlags(unsigned F) : Flags(F) {}
134 NeonTypeFlags(EltType ET, bool IsUnsigned, bool IsQuad) : Flags(ET) {
135 if (IsUnsigned)
136 Flags |= UnsignedFlag;
137 if (IsQuad)
138 Flags |= QuadFlag;
139 }
140
141 uint32_t getFlags() const { return Flags; }
142};
143} // end anonymous namespace
144
145namespace {
146class NeonEmitter {
147 RecordKeeper &Records;
148 StringMap<OpKind> OpMap;
149 DenseMap<Record*, ClassKind> ClassMap;
150
151public:
152 NeonEmitter(RecordKeeper &R) : Records(R) {
153 OpMap["OP_NONE"] = OpNone;
154 OpMap["OP_UNAVAILABLE"] = OpUnavailable;
155 OpMap["OP_ADD"] = OpAdd;
156 OpMap["OP_ADDL"] = OpAddl;
157 OpMap["OP_ADDW"] = OpAddw;
158 OpMap["OP_SUB"] = OpSub;
159 OpMap["OP_SUBL"] = OpSubl;
160 OpMap["OP_SUBW"] = OpSubw;
161 OpMap["OP_MUL"] = OpMul;
162 OpMap["OP_MLA"] = OpMla;
163 OpMap["OP_MLAL"] = OpMlal;
164 OpMap["OP_MLS"] = OpMls;
165 OpMap["OP_MLSL"] = OpMlsl;
166 OpMap["OP_MUL_N"] = OpMulN;
167 OpMap["OP_MLA_N"] = OpMlaN;
168 OpMap["OP_MLS_N"] = OpMlsN;
169 OpMap["OP_MLAL_N"] = OpMlalN;
170 OpMap["OP_MLSL_N"] = OpMlslN;
171 OpMap["OP_MUL_LN"]= OpMulLane;
172 OpMap["OP_MULL_LN"] = OpMullLane;
173 OpMap["OP_MLA_LN"]= OpMlaLane;
174 OpMap["OP_MLS_LN"]= OpMlsLane;
175 OpMap["OP_MLAL_LN"] = OpMlalLane;
176 OpMap["OP_MLSL_LN"] = OpMlslLane;
177 OpMap["OP_QDMULL_LN"] = OpQDMullLane;
178 OpMap["OP_QDMLAL_LN"] = OpQDMlalLane;
179 OpMap["OP_QDMLSL_LN"] = OpQDMlslLane;
180 OpMap["OP_QDMULH_LN"] = OpQDMulhLane;
181 OpMap["OP_QRDMULH_LN"] = OpQRDMulhLane;
182 OpMap["OP_EQ"] = OpEq;
183 OpMap["OP_GE"] = OpGe;
184 OpMap["OP_LE"] = OpLe;
185 OpMap["OP_GT"] = OpGt;
186 OpMap["OP_LT"] = OpLt;
187 OpMap["OP_NEG"] = OpNeg;
188 OpMap["OP_NOT"] = OpNot;
189 OpMap["OP_AND"] = OpAnd;
190 OpMap["OP_OR"] = OpOr;
191 OpMap["OP_XOR"] = OpXor;
192 OpMap["OP_ANDN"] = OpAndNot;
193 OpMap["OP_ORN"] = OpOrNot;
194 OpMap["OP_CAST"] = OpCast;
195 OpMap["OP_CONC"] = OpConcat;
196 OpMap["OP_HI"] = OpHi;
197 OpMap["OP_LO"] = OpLo;
198 OpMap["OP_DUP"] = OpDup;
199 OpMap["OP_DUP_LN"] = OpDupLane;
200 OpMap["OP_SEL"] = OpSelect;
201 OpMap["OP_REV16"] = OpRev16;
202 OpMap["OP_REV32"] = OpRev32;
203 OpMap["OP_REV64"] = OpRev64;
204 OpMap["OP_REINT"] = OpReinterpret;
205 OpMap["OP_ABDL"] = OpAbdl;
206 OpMap["OP_ABA"] = OpAba;
207 OpMap["OP_ABAL"] = OpAbal;
208
209 Record *SI = R.getClass("SInst");
210 Record *II = R.getClass("IInst");
211 Record *WI = R.getClass("WInst");
Michael Gottesman21e4e942013-04-16 21:18:42 +0000212 Record *SOpI = R.getClass("SOpInst");
213 Record *IOpI = R.getClass("IOpInst");
214 Record *WOpI = R.getClass("WOpInst");
215 Record *LOpI = R.getClass("LOpInst");
216 Record *NoTestOpI = R.getClass("NoTestOpInst");
217
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000218 ClassMap[SI] = ClassS;
219 ClassMap[II] = ClassI;
220 ClassMap[WI] = ClassW;
Michael Gottesman21e4e942013-04-16 21:18:42 +0000221 ClassMap[SOpI] = ClassS;
222 ClassMap[IOpI] = ClassI;
223 ClassMap[WOpI] = ClassW;
224 ClassMap[LOpI] = ClassL;
225 ClassMap[NoTestOpI] = ClassNoTest;
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000226 }
227
228 // run - Emit arm_neon.h.inc
229 void run(raw_ostream &o);
230
231 // runHeader - Emit all the __builtin prototypes used in arm_neon.h
232 void runHeader(raw_ostream &o);
233
234 // runTests - Emit tests for all the Neon intrinsics.
235 void runTests(raw_ostream &o);
236
237private:
238 void emitIntrinsic(raw_ostream &OS, Record *R);
239};
240} // end anonymous namespace
241
Peter Collingbourne51d77772011-10-06 13:03:08 +0000242/// ParseTypes - break down a string such as "fQf" into a vector of StringRefs,
243/// which each StringRef representing a single type declared in the string.
244/// for "fQf" we would end up with 2 StringRefs, "f", and "Qf", representing
245/// 2xfloat and 4xfloat respectively.
246static void ParseTypes(Record *r, std::string &s,
247 SmallVectorImpl<StringRef> &TV) {
248 const char *data = s.data();
249 int len = 0;
250
251 for (unsigned i = 0, e = s.size(); i != e; ++i, ++len) {
252 if (data[len] == 'P' || data[len] == 'Q' || data[len] == 'U')
253 continue;
254
255 switch (data[len]) {
256 case 'c':
257 case 's':
258 case 'i':
259 case 'l':
260 case 'h':
261 case 'f':
262 break;
263 default:
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +0000264 PrintFatalError(r->getLoc(),
Peter Collingbourne51d77772011-10-06 13:03:08 +0000265 "Unexpected letter: " + std::string(data + len, 1));
Peter Collingbourne51d77772011-10-06 13:03:08 +0000266 }
267 TV.push_back(StringRef(data, len + 1));
268 data += len + 1;
269 len = -1;
270 }
271}
272
273/// Widen - Convert a type code into the next wider type. char -> short,
274/// short -> int, etc.
275static char Widen(const char t) {
276 switch (t) {
277 case 'c':
278 return 's';
279 case 's':
280 return 'i';
281 case 'i':
282 return 'l';
283 case 'h':
284 return 'f';
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +0000285 default:
286 PrintFatalError("unhandled type in widen!");
Peter Collingbourne51d77772011-10-06 13:03:08 +0000287 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000288}
289
290/// Narrow - Convert a type code into the next smaller type. short -> char,
291/// float -> half float, etc.
292static char Narrow(const char t) {
293 switch (t) {
294 case 's':
295 return 'c';
296 case 'i':
297 return 's';
298 case 'l':
299 return 'i';
300 case 'f':
301 return 'h';
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +0000302 default:
303 PrintFatalError("unhandled type in narrow!");
Peter Collingbourne51d77772011-10-06 13:03:08 +0000304 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000305}
306
307/// For a particular StringRef, return the base type code, and whether it has
308/// the quad-vector, polynomial, or unsigned modifiers set.
309static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) {
310 unsigned off = 0;
311
312 // remember quad.
313 if (ty[off] == 'Q') {
314 quad = true;
315 ++off;
316 }
317
318 // remember poly.
319 if (ty[off] == 'P') {
320 poly = true;
321 ++off;
322 }
323
324 // remember unsigned.
325 if (ty[off] == 'U') {
326 usgn = true;
327 ++off;
328 }
329
330 // base type to get the type string for.
331 return ty[off];
332}
333
334/// ModType - Transform a type code and its modifiers based on a mod code. The
335/// mod code definitions may be found at the top of arm_neon.td.
336static char ModType(const char mod, char type, bool &quad, bool &poly,
337 bool &usgn, bool &scal, bool &cnst, bool &pntr) {
338 switch (mod) {
339 case 't':
340 if (poly) {
341 poly = false;
342 usgn = true;
343 }
344 break;
345 case 'u':
346 usgn = true;
347 poly = false;
348 if (type == 'f')
349 type = 'i';
350 break;
351 case 'x':
352 usgn = false;
353 poly = false;
354 if (type == 'f')
355 type = 'i';
356 break;
357 case 'f':
358 if (type == 'h')
359 quad = true;
360 type = 'f';
361 usgn = false;
362 break;
363 case 'g':
364 quad = false;
365 break;
366 case 'w':
367 type = Widen(type);
368 quad = true;
369 break;
370 case 'n':
371 type = Widen(type);
372 break;
373 case 'i':
374 type = 'i';
375 scal = true;
376 break;
377 case 'l':
378 type = 'l';
379 scal = true;
380 usgn = true;
381 break;
382 case 's':
383 case 'a':
384 scal = true;
385 break;
386 case 'k':
387 quad = true;
388 break;
389 case 'c':
390 cnst = true;
391 case 'p':
392 pntr = true;
393 scal = true;
394 break;
395 case 'h':
396 type = Narrow(type);
397 if (type == 'h')
398 quad = false;
399 break;
400 case 'e':
401 type = Narrow(type);
402 usgn = true;
403 break;
404 default:
405 break;
406 }
407 return type;
408}
409
410/// TypeString - for a modifier and type, generate the name of the typedef for
411/// that type. QUc -> uint8x8_t.
412static std::string TypeString(const char mod, StringRef typestr) {
413 bool quad = false;
414 bool poly = false;
415 bool usgn = false;
416 bool scal = false;
417 bool cnst = false;
418 bool pntr = false;
419
420 if (mod == 'v')
421 return "void";
422 if (mod == 'i')
423 return "int";
424
425 // base type to get the type string for.
426 char type = ClassifyType(typestr, quad, poly, usgn);
427
428 // Based on the modifying character, change the type and width if necessary.
429 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
430
431 SmallString<128> s;
432
433 if (usgn)
434 s.push_back('u');
435
436 switch (type) {
437 case 'c':
438 s += poly ? "poly8" : "int8";
439 if (scal)
440 break;
441 s += quad ? "x16" : "x8";
442 break;
443 case 's':
444 s += poly ? "poly16" : "int16";
445 if (scal)
446 break;
447 s += quad ? "x8" : "x4";
448 break;
449 case 'i':
450 s += "int32";
451 if (scal)
452 break;
453 s += quad ? "x4" : "x2";
454 break;
455 case 'l':
456 s += "int64";
457 if (scal)
458 break;
459 s += quad ? "x2" : "x1";
460 break;
461 case 'h':
462 s += "float16";
463 if (scal)
464 break;
465 s += quad ? "x8" : "x4";
466 break;
467 case 'f':
468 s += "float32";
469 if (scal)
470 break;
471 s += quad ? "x4" : "x2";
472 break;
473 default:
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +0000474 PrintFatalError("unhandled type!");
Peter Collingbourne51d77772011-10-06 13:03:08 +0000475 }
476
477 if (mod == '2')
478 s += "x2";
479 if (mod == '3')
480 s += "x3";
481 if (mod == '4')
482 s += "x4";
483
484 // Append _t, finishing the type string typedef type.
485 s += "_t";
486
487 if (cnst)
488 s += " const";
489
490 if (pntr)
491 s += " *";
492
493 return s.str();
494}
495
496/// BuiltinTypeString - for a modifier and type, generate the clang
497/// BuiltinsARM.def prototype code for the function. See the top of clang's
498/// Builtins.def for a description of the type strings.
499static std::string BuiltinTypeString(const char mod, StringRef typestr,
500 ClassKind ck, bool ret) {
501 bool quad = false;
502 bool poly = false;
503 bool usgn = false;
504 bool scal = false;
505 bool cnst = false;
506 bool pntr = false;
507
508 if (mod == 'v')
509 return "v"; // void
510 if (mod == 'i')
511 return "i"; // int
512
513 // base type to get the type string for.
514 char type = ClassifyType(typestr, quad, poly, usgn);
515
516 // Based on the modifying character, change the type and width if necessary.
517 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
518
519 // All pointers are void* pointers. Change type to 'v' now.
520 if (pntr) {
521 usgn = false;
522 poly = false;
523 type = 'v';
524 }
525 // Treat half-float ('h') types as unsigned short ('s') types.
526 if (type == 'h') {
527 type = 's';
528 usgn = true;
529 }
530 usgn = usgn | poly | ((ck == ClassI || ck == ClassW) && scal && type != 'f');
531
532 if (scal) {
533 SmallString<128> s;
534
535 if (usgn)
536 s.push_back('U');
537 else if (type == 'c')
538 s.push_back('S'); // make chars explicitly signed
539
540 if (type == 'l') // 64-bit long
541 s += "LLi";
542 else
543 s.push_back(type);
544
545 if (cnst)
546 s.push_back('C');
547 if (pntr)
548 s.push_back('*');
549 return s.str();
550 }
551
552 // Since the return value must be one type, return a vector type of the
553 // appropriate width which we will bitcast. An exception is made for
554 // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
555 // fashion, storing them to a pointer arg.
556 if (ret) {
557 if (mod >= '2' && mod <= '4')
558 return "vv*"; // void result with void* first argument
559 if (mod == 'f' || (ck != ClassB && type == 'f'))
560 return quad ? "V4f" : "V2f";
561 if (ck != ClassB && type == 's')
562 return quad ? "V8s" : "V4s";
563 if (ck != ClassB && type == 'i')
564 return quad ? "V4i" : "V2i";
565 if (ck != ClassB && type == 'l')
566 return quad ? "V2LLi" : "V1LLi";
567
568 return quad ? "V16Sc" : "V8Sc";
569 }
570
571 // Non-return array types are passed as individual vectors.
572 if (mod == '2')
573 return quad ? "V16ScV16Sc" : "V8ScV8Sc";
574 if (mod == '3')
575 return quad ? "V16ScV16ScV16Sc" : "V8ScV8ScV8Sc";
576 if (mod == '4')
577 return quad ? "V16ScV16ScV16ScV16Sc" : "V8ScV8ScV8ScV8Sc";
578
579 if (mod == 'f' || (ck != ClassB && type == 'f'))
580 return quad ? "V4f" : "V2f";
581 if (ck != ClassB && type == 's')
582 return quad ? "V8s" : "V4s";
583 if (ck != ClassB && type == 'i')
584 return quad ? "V4i" : "V2i";
585 if (ck != ClassB && type == 'l')
586 return quad ? "V2LLi" : "V1LLi";
587
588 return quad ? "V16Sc" : "V8Sc";
589}
590
Michael Gottesmanfb599a42013-04-16 22:07:30 +0000591/// InstructionTypeCode - Computes the ARM argument character code and
592/// quad status for a specific type string and ClassKind.
593static void InstructionTypeCode(const StringRef &typeStr,
594 const ClassKind ck,
595 bool &quad,
596 std::string &typeCode) {
597 bool poly = false;
598 bool usgn = false;
599 char type = ClassifyType(typeStr, quad, poly, usgn);
600
601 switch (type) {
602 case 'c':
603 switch (ck) {
604 case ClassS: typeCode = poly ? "p8" : usgn ? "u8" : "s8"; break;
605 case ClassI: typeCode = "i8"; break;
606 case ClassW: typeCode = "8"; break;
607 default: break;
608 }
609 break;
610 case 's':
611 switch (ck) {
612 case ClassS: typeCode = poly ? "p16" : usgn ? "u16" : "s16"; break;
613 case ClassI: typeCode = "i16"; break;
614 case ClassW: typeCode = "16"; break;
615 default: break;
616 }
617 break;
618 case 'i':
619 switch (ck) {
620 case ClassS: typeCode = usgn ? "u32" : "s32"; break;
621 case ClassI: typeCode = "i32"; break;
622 case ClassW: typeCode = "32"; break;
623 default: break;
624 }
625 break;
626 case 'l':
627 switch (ck) {
628 case ClassS: typeCode = usgn ? "u64" : "s64"; break;
629 case ClassI: typeCode = "i64"; break;
630 case ClassW: typeCode = "64"; break;
631 default: break;
632 }
633 break;
634 case 'h':
635 switch (ck) {
636 case ClassS:
637 case ClassI: typeCode = "f16"; break;
638 case ClassW: typeCode = "16"; break;
639 default: break;
640 }
641 break;
642 case 'f':
643 switch (ck) {
644 case ClassS:
645 case ClassI: typeCode = "f32"; break;
646 case ClassW: typeCode = "32"; break;
647 default: break;
648 }
649 break;
650 default:
651 PrintFatalError("unhandled type!");
652 }
653}
654
Peter Collingbourne51d77772011-10-06 13:03:08 +0000655/// MangleName - Append a type or width suffix to a base neon function name,
656/// and insert a 'q' in the appropriate location if the operation works on
657/// 128b rather than 64b. E.g. turn "vst2_lane" into "vst2q_lane_f32", etc.
658static std::string MangleName(const std::string &name, StringRef typestr,
659 ClassKind ck) {
660 if (name == "vcvt_f32_f16")
661 return name;
662
663 bool quad = false;
Michael Gottesmanfb599a42013-04-16 22:07:30 +0000664 std::string typeCode = "";
665
666 InstructionTypeCode(typestr, ck, quad, typeCode);
Peter Collingbourne51d77772011-10-06 13:03:08 +0000667
668 std::string s = name;
669
Michael Gottesmanfb599a42013-04-16 22:07:30 +0000670 if (typeCode.size() > 0) {
671 s += "_" + typeCode;
Peter Collingbourne51d77772011-10-06 13:03:08 +0000672 }
Michael Gottesmanfb599a42013-04-16 22:07:30 +0000673
Peter Collingbourne51d77772011-10-06 13:03:08 +0000674 if (ck == ClassB)
675 s += "_v";
676
677 // Insert a 'q' before the first '_' character so that it ends up before
678 // _lane or _n on vector-scalar operations.
679 if (quad) {
680 size_t pos = s.find('_');
681 s = s.insert(pos, "q");
682 }
683 return s;
684}
685
686/// UseMacro - Examine the prototype string to determine if the intrinsic
687/// should be defined as a preprocessor macro instead of an inline function.
688static bool UseMacro(const std::string &proto) {
689 // If this builtin takes an immediate argument, we need to #define it rather
690 // than use a standard declaration, so that SemaChecking can range check
691 // the immediate passed by the user.
692 if (proto.find('i') != std::string::npos)
693 return true;
694
695 // Pointer arguments need to use macros to avoid hiding aligned attributes
696 // from the pointer type.
697 if (proto.find('p') != std::string::npos ||
698 proto.find('c') != std::string::npos)
699 return true;
700
701 return false;
702}
703
704/// MacroArgUsedDirectly - Return true if argument i for an intrinsic that is
705/// defined as a macro should be accessed directly instead of being first
706/// assigned to a local temporary.
707static bool MacroArgUsedDirectly(const std::string &proto, unsigned i) {
708 // True for constant ints (i), pointers (p) and const pointers (c).
709 return (proto[i] == 'i' || proto[i] == 'p' || proto[i] == 'c');
710}
711
712// Generate the string "(argtype a, argtype b, ...)"
713static std::string GenArgs(const std::string &proto, StringRef typestr) {
714 bool define = UseMacro(proto);
715 char arg = 'a';
716
717 std::string s;
718 s += "(";
719
720 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
721 if (define) {
722 // Some macro arguments are used directly instead of being assigned
723 // to local temporaries; prepend an underscore prefix to make their
724 // names consistent with the local temporaries.
725 if (MacroArgUsedDirectly(proto, i))
726 s += "__";
727 } else {
728 s += TypeString(proto[i], typestr) + " __";
729 }
730 s.push_back(arg);
731 if ((i + 1) < e)
732 s += ", ";
733 }
734
735 s += ")";
736 return s;
737}
738
739// Macro arguments are not type-checked like inline function arguments, so
740// assign them to local temporaries to get the right type checking.
741static std::string GenMacroLocals(const std::string &proto, StringRef typestr) {
742 char arg = 'a';
743 std::string s;
744 bool generatedLocal = false;
745
746 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
747 // Do not create a temporary for an immediate argument.
748 // That would defeat the whole point of using a macro!
Peter Collingbourne51d77772011-10-06 13:03:08 +0000749 if (MacroArgUsedDirectly(proto, i))
750 continue;
751 generatedLocal = true;
752
753 s += TypeString(proto[i], typestr) + " __";
754 s.push_back(arg);
755 s += " = (";
756 s.push_back(arg);
757 s += "); ";
758 }
759
760 if (generatedLocal)
761 s += "\\\n ";
762 return s;
763}
764
765// Use the vmovl builtin to sign-extend or zero-extend a vector.
766static std::string Extend(StringRef typestr, const std::string &a) {
767 std::string s;
768 s = MangleName("vmovl", typestr, ClassS);
769 s += "(" + a + ")";
770 return s;
771}
772
773static std::string Duplicate(unsigned nElts, StringRef typestr,
774 const std::string &a) {
775 std::string s;
776
777 s = "(" + TypeString('d', typestr) + "){ ";
778 for (unsigned i = 0; i != nElts; ++i) {
779 s += a;
780 if ((i + 1) < nElts)
781 s += ", ";
782 }
783 s += " }";
784
785 return s;
786}
787
788static std::string SplatLane(unsigned nElts, const std::string &vec,
789 const std::string &lane) {
790 std::string s = "__builtin_shufflevector(" + vec + ", " + vec;
791 for (unsigned i = 0; i < nElts; ++i)
792 s += ", " + lane;
793 s += ")";
794 return s;
795}
796
797static unsigned GetNumElements(StringRef typestr, bool &quad) {
798 quad = false;
799 bool dummy = false;
800 char type = ClassifyType(typestr, quad, dummy, dummy);
801 unsigned nElts = 0;
802 switch (type) {
803 case 'c': nElts = 8; break;
804 case 's': nElts = 4; break;
805 case 'i': nElts = 2; break;
806 case 'l': nElts = 1; break;
807 case 'h': nElts = 4; break;
808 case 'f': nElts = 2; break;
809 default:
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +0000810 PrintFatalError("unhandled type!");
Peter Collingbourne51d77772011-10-06 13:03:08 +0000811 }
812 if (quad) nElts <<= 1;
813 return nElts;
814}
815
816// Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
817static std::string GenOpString(OpKind op, const std::string &proto,
818 StringRef typestr) {
819 bool quad;
820 unsigned nElts = GetNumElements(typestr, quad);
821 bool define = UseMacro(proto);
822
823 std::string ts = TypeString(proto[0], typestr);
824 std::string s;
825 if (!define) {
826 s = "return ";
827 }
828
829 switch(op) {
830 case OpAdd:
831 s += "__a + __b;";
832 break;
833 case OpAddl:
834 s += Extend(typestr, "__a") + " + " + Extend(typestr, "__b") + ";";
835 break;
836 case OpAddw:
837 s += "__a + " + Extend(typestr, "__b") + ";";
838 break;
839 case OpSub:
840 s += "__a - __b;";
841 break;
842 case OpSubl:
843 s += Extend(typestr, "__a") + " - " + Extend(typestr, "__b") + ";";
844 break;
845 case OpSubw:
846 s += "__a - " + Extend(typestr, "__b") + ";";
847 break;
848 case OpMulN:
849 s += "__a * " + Duplicate(nElts, typestr, "__b") + ";";
850 break;
851 case OpMulLane:
852 s += "__a * " + SplatLane(nElts, "__b", "__c") + ";";
853 break;
854 case OpMul:
855 s += "__a * __b;";
856 break;
857 case OpMullLane:
858 s += MangleName("vmull", typestr, ClassS) + "(__a, " +
859 SplatLane(nElts, "__b", "__c") + ");";
860 break;
861 case OpMlaN:
862 s += "__a + (__b * " + Duplicate(nElts, typestr, "__c") + ");";
863 break;
864 case OpMlaLane:
865 s += "__a + (__b * " + SplatLane(nElts, "__c", "__d") + ");";
866 break;
867 case OpMla:
868 s += "__a + (__b * __c);";
869 break;
870 case OpMlalN:
871 s += "__a + " + MangleName("vmull", typestr, ClassS) + "(__b, " +
872 Duplicate(nElts, typestr, "__c") + ");";
873 break;
874 case OpMlalLane:
875 s += "__a + " + MangleName("vmull", typestr, ClassS) + "(__b, " +
876 SplatLane(nElts, "__c", "__d") + ");";
877 break;
878 case OpMlal:
879 s += "__a + " + MangleName("vmull", typestr, ClassS) + "(__b, __c);";
880 break;
881 case OpMlsN:
882 s += "__a - (__b * " + Duplicate(nElts, typestr, "__c") + ");";
883 break;
884 case OpMlsLane:
885 s += "__a - (__b * " + SplatLane(nElts, "__c", "__d") + ");";
886 break;
887 case OpMls:
888 s += "__a - (__b * __c);";
889 break;
890 case OpMlslN:
891 s += "__a - " + MangleName("vmull", typestr, ClassS) + "(__b, " +
892 Duplicate(nElts, typestr, "__c") + ");";
893 break;
894 case OpMlslLane:
895 s += "__a - " + MangleName("vmull", typestr, ClassS) + "(__b, " +
896 SplatLane(nElts, "__c", "__d") + ");";
897 break;
898 case OpMlsl:
899 s += "__a - " + MangleName("vmull", typestr, ClassS) + "(__b, __c);";
900 break;
901 case OpQDMullLane:
902 s += MangleName("vqdmull", typestr, ClassS) + "(__a, " +
903 SplatLane(nElts, "__b", "__c") + ");";
904 break;
905 case OpQDMlalLane:
906 s += MangleName("vqdmlal", typestr, ClassS) + "(__a, __b, " +
907 SplatLane(nElts, "__c", "__d") + ");";
908 break;
909 case OpQDMlslLane:
910 s += MangleName("vqdmlsl", typestr, ClassS) + "(__a, __b, " +
911 SplatLane(nElts, "__c", "__d") + ");";
912 break;
913 case OpQDMulhLane:
914 s += MangleName("vqdmulh", typestr, ClassS) + "(__a, " +
915 SplatLane(nElts, "__b", "__c") + ");";
916 break;
917 case OpQRDMulhLane:
918 s += MangleName("vqrdmulh", typestr, ClassS) + "(__a, " +
919 SplatLane(nElts, "__b", "__c") + ");";
920 break;
921 case OpEq:
922 s += "(" + ts + ")(__a == __b);";
923 break;
924 case OpGe:
925 s += "(" + ts + ")(__a >= __b);";
926 break;
927 case OpLe:
928 s += "(" + ts + ")(__a <= __b);";
929 break;
930 case OpGt:
931 s += "(" + ts + ")(__a > __b);";
932 break;
933 case OpLt:
934 s += "(" + ts + ")(__a < __b);";
935 break;
936 case OpNeg:
937 s += " -__a;";
938 break;
939 case OpNot:
940 s += " ~__a;";
941 break;
942 case OpAnd:
943 s += "__a & __b;";
944 break;
945 case OpOr:
946 s += "__a | __b;";
947 break;
948 case OpXor:
949 s += "__a ^ __b;";
950 break;
951 case OpAndNot:
952 s += "__a & ~__b;";
953 break;
954 case OpOrNot:
955 s += "__a | ~__b;";
956 break;
957 case OpCast:
958 s += "(" + ts + ")__a;";
959 break;
960 case OpConcat:
961 s += "(" + ts + ")__builtin_shufflevector((int64x1_t)__a";
962 s += ", (int64x1_t)__b, 0, 1);";
963 break;
964 case OpHi:
965 s += "(" + ts +
966 ")__builtin_shufflevector((int64x2_t)__a, (int64x2_t)__a, 1);";
967 break;
968 case OpLo:
969 s += "(" + ts +
970 ")__builtin_shufflevector((int64x2_t)__a, (int64x2_t)__a, 0);";
971 break;
972 case OpDup:
973 s += Duplicate(nElts, typestr, "__a") + ";";
974 break;
975 case OpDupLane:
976 s += SplatLane(nElts, "__a", "__b") + ";";
977 break;
978 case OpSelect:
979 // ((0 & 1) | (~0 & 2))
980 s += "(" + ts + ")";
981 ts = TypeString(proto[1], typestr);
982 s += "((__a & (" + ts + ")__b) | ";
983 s += "(~__a & (" + ts + ")__c));";
984 break;
985 case OpRev16:
986 s += "__builtin_shufflevector(__a, __a";
987 for (unsigned i = 2; i <= nElts; i += 2)
988 for (unsigned j = 0; j != 2; ++j)
989 s += ", " + utostr(i - j - 1);
990 s += ");";
991 break;
992 case OpRev32: {
993 unsigned WordElts = nElts >> (1 + (int)quad);
994 s += "__builtin_shufflevector(__a, __a";
995 for (unsigned i = WordElts; i <= nElts; i += WordElts)
996 for (unsigned j = 0; j != WordElts; ++j)
997 s += ", " + utostr(i - j - 1);
998 s += ");";
999 break;
1000 }
1001 case OpRev64: {
1002 unsigned DblWordElts = nElts >> (int)quad;
1003 s += "__builtin_shufflevector(__a, __a";
1004 for (unsigned i = DblWordElts; i <= nElts; i += DblWordElts)
1005 for (unsigned j = 0; j != DblWordElts; ++j)
1006 s += ", " + utostr(i - j - 1);
1007 s += ");";
1008 break;
1009 }
1010 case OpAbdl: {
1011 std::string abd = MangleName("vabd", typestr, ClassS) + "(__a, __b)";
1012 if (typestr[0] != 'U') {
1013 // vabd results are always unsigned and must be zero-extended.
1014 std::string utype = "U" + typestr.str();
1015 s += "(" + TypeString(proto[0], typestr) + ")";
1016 abd = "(" + TypeString('d', utype) + ")" + abd;
1017 s += Extend(utype, abd) + ";";
1018 } else {
1019 s += Extend(typestr, abd) + ";";
1020 }
1021 break;
1022 }
1023 case OpAba:
1024 s += "__a + " + MangleName("vabd", typestr, ClassS) + "(__b, __c);";
1025 break;
1026 case OpAbal: {
1027 s += "__a + ";
1028 std::string abd = MangleName("vabd", typestr, ClassS) + "(__b, __c)";
1029 if (typestr[0] != 'U') {
1030 // vabd results are always unsigned and must be zero-extended.
1031 std::string utype = "U" + typestr.str();
1032 s += "(" + TypeString(proto[0], typestr) + ")";
1033 abd = "(" + TypeString('d', utype) + ")" + abd;
1034 s += Extend(utype, abd) + ";";
1035 } else {
1036 s += Extend(typestr, abd) + ";";
1037 }
1038 break;
1039 }
1040 default:
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +00001041 PrintFatalError("unknown OpKind!");
Peter Collingbourne51d77772011-10-06 13:03:08 +00001042 }
1043 return s;
1044}
1045
1046static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
1047 unsigned mod = proto[0];
Peter Collingbourne51d77772011-10-06 13:03:08 +00001048
1049 if (mod == 'v' || mod == 'f')
1050 mod = proto[1];
1051
1052 bool quad = false;
1053 bool poly = false;
1054 bool usgn = false;
1055 bool scal = false;
1056 bool cnst = false;
1057 bool pntr = false;
1058
1059 // Base type to get the type string for.
1060 char type = ClassifyType(typestr, quad, poly, usgn);
1061
1062 // Based on the modifying character, change the type and width if necessary.
1063 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
1064
Bob Wilsonda95f732011-11-08 01:16:11 +00001065 NeonTypeFlags::EltType ET;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001066 switch (type) {
1067 case 'c':
Bob Wilsonda95f732011-11-08 01:16:11 +00001068 ET = poly ? NeonTypeFlags::Poly8 : NeonTypeFlags::Int8;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001069 break;
1070 case 's':
Bob Wilsonda95f732011-11-08 01:16:11 +00001071 ET = poly ? NeonTypeFlags::Poly16 : NeonTypeFlags::Int16;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001072 break;
1073 case 'i':
Bob Wilsonda95f732011-11-08 01:16:11 +00001074 ET = NeonTypeFlags::Int32;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001075 break;
1076 case 'l':
Bob Wilsonda95f732011-11-08 01:16:11 +00001077 ET = NeonTypeFlags::Int64;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001078 break;
1079 case 'h':
Bob Wilsonda95f732011-11-08 01:16:11 +00001080 ET = NeonTypeFlags::Float16;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001081 break;
1082 case 'f':
Bob Wilsonda95f732011-11-08 01:16:11 +00001083 ET = NeonTypeFlags::Float32;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001084 break;
1085 default:
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +00001086 PrintFatalError("unhandled type!");
Peter Collingbourne51d77772011-10-06 13:03:08 +00001087 }
Bob Wilsonda95f732011-11-08 01:16:11 +00001088 NeonTypeFlags Flags(ET, usgn, quad && proto[1] != 'g');
1089 return Flags.getFlags();
Peter Collingbourne51d77772011-10-06 13:03:08 +00001090}
1091
1092// Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
1093static std::string GenBuiltin(const std::string &name, const std::string &proto,
1094 StringRef typestr, ClassKind ck) {
1095 std::string s;
1096
1097 // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
1098 // sret-like argument.
1099 bool sret = (proto[0] >= '2' && proto[0] <= '4');
1100
1101 bool define = UseMacro(proto);
1102
1103 // Check if the prototype has a scalar operand with the type of the vector
1104 // elements. If not, bitcasting the args will take care of arg checking.
1105 // The actual signedness etc. will be taken care of with special enums.
1106 if (proto.find('s') == std::string::npos)
1107 ck = ClassB;
1108
1109 if (proto[0] != 'v') {
1110 std::string ts = TypeString(proto[0], typestr);
1111
1112 if (define) {
1113 if (sret)
1114 s += ts + " r; ";
1115 else
1116 s += "(" + ts + ")";
1117 } else if (sret) {
1118 s += ts + " r; ";
1119 } else {
1120 s += "return (" + ts + ")";
1121 }
1122 }
1123
1124 bool splat = proto.find('a') != std::string::npos;
1125
1126 s += "__builtin_neon_";
1127 if (splat) {
1128 // Call the non-splat builtin: chop off the "_n" suffix from the name.
1129 std::string vname(name, 0, name.size()-2);
1130 s += MangleName(vname, typestr, ck);
1131 } else {
1132 s += MangleName(name, typestr, ck);
1133 }
1134 s += "(";
1135
1136 // Pass the address of the return variable as the first argument to sret-like
1137 // builtins.
1138 if (sret)
1139 s += "&r, ";
1140
1141 char arg = 'a';
1142 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
1143 std::string args = std::string(&arg, 1);
1144
1145 // Use the local temporaries instead of the macro arguments.
1146 args = "__" + args;
1147
1148 bool argQuad = false;
1149 bool argPoly = false;
1150 bool argUsgn = false;
1151 bool argScalar = false;
1152 bool dummy = false;
1153 char argType = ClassifyType(typestr, argQuad, argPoly, argUsgn);
1154 argType = ModType(proto[i], argType, argQuad, argPoly, argUsgn, argScalar,
1155 dummy, dummy);
1156
1157 // Handle multiple-vector values specially, emitting each subvector as an
1158 // argument to the __builtin.
1159 if (proto[i] >= '2' && proto[i] <= '4') {
1160 // Check if an explicit cast is needed.
1161 if (argType != 'c' || argPoly || argUsgn)
1162 args = (argQuad ? "(int8x16_t)" : "(int8x8_t)") + args;
1163
1164 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
1165 s += args + ".val[" + utostr(vi) + "]";
1166 if ((vi + 1) < ve)
1167 s += ", ";
1168 }
1169 if ((i + 1) < e)
1170 s += ", ";
1171
1172 continue;
1173 }
1174
1175 if (splat && (i + 1) == e)
1176 args = Duplicate(GetNumElements(typestr, argQuad), typestr, args);
1177
1178 // Check if an explicit cast is needed.
1179 if ((splat || !argScalar) &&
1180 ((ck == ClassB && argType != 'c') || argPoly || argUsgn)) {
1181 std::string argTypeStr = "c";
1182 if (ck != ClassB)
1183 argTypeStr = argType;
1184 if (argQuad)
1185 argTypeStr = "Q" + argTypeStr;
1186 args = "(" + TypeString('d', argTypeStr) + ")" + args;
1187 }
1188
1189 s += args;
1190 if ((i + 1) < e)
1191 s += ", ";
1192 }
1193
1194 // Extra constant integer to hold type class enum for this function, e.g. s8
1195 if (ck == ClassB)
1196 s += ", " + utostr(GetNeonEnum(proto, typestr));
1197
1198 s += ");";
1199
1200 if (proto[0] != 'v' && sret) {
1201 if (define)
1202 s += " r;";
1203 else
1204 s += " return r;";
1205 }
1206 return s;
1207}
1208
1209static std::string GenBuiltinDef(const std::string &name,
1210 const std::string &proto,
1211 StringRef typestr, ClassKind ck) {
1212 std::string s("BUILTIN(__builtin_neon_");
1213
1214 // If all types are the same size, bitcasting the args will take care
1215 // of arg checking. The actual signedness etc. will be taken care of with
1216 // special enums.
1217 if (proto.find('s') == std::string::npos)
1218 ck = ClassB;
1219
1220 s += MangleName(name, typestr, ck);
1221 s += ", \"";
1222
1223 for (unsigned i = 0, e = proto.size(); i != e; ++i)
1224 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
1225
1226 // Extra constant integer to hold type class enum for this function, e.g. s8
1227 if (ck == ClassB)
1228 s += "i";
1229
1230 s += "\", \"n\")";
1231 return s;
1232}
1233
1234static std::string GenIntrinsic(const std::string &name,
1235 const std::string &proto,
1236 StringRef outTypeStr, StringRef inTypeStr,
1237 OpKind kind, ClassKind classKind) {
1238 assert(!proto.empty() && "");
Jim Grosbach667381b2012-05-09 18:17:30 +00001239 bool define = UseMacro(proto) && kind != OpUnavailable;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001240 std::string s;
1241
1242 // static always inline + return type
1243 if (define)
1244 s += "#define ";
1245 else
1246 s += "__ai " + TypeString(proto[0], outTypeStr) + " ";
1247
1248 // Function name with type suffix
1249 std::string mangledName = MangleName(name, outTypeStr, ClassS);
1250 if (outTypeStr != inTypeStr) {
1251 // If the input type is different (e.g., for vreinterpret), append a suffix
1252 // for the input type. String off a "Q" (quad) prefix so that MangleName
1253 // does not insert another "q" in the name.
1254 unsigned typeStrOff = (inTypeStr[0] == 'Q' ? 1 : 0);
1255 StringRef inTypeNoQuad = inTypeStr.substr(typeStrOff);
1256 mangledName = MangleName(mangledName, inTypeNoQuad, ClassS);
1257 }
1258 s += mangledName;
1259
1260 // Function arguments
1261 s += GenArgs(proto, inTypeStr);
1262
1263 // Definition.
1264 if (define) {
1265 s += " __extension__ ({ \\\n ";
1266 s += GenMacroLocals(proto, inTypeStr);
Jim Grosbach667381b2012-05-09 18:17:30 +00001267 } else if (kind == OpUnavailable) {
1268 s += " __attribute__((unavailable));\n";
1269 return s;
1270 } else
Jim Grosbach66981c72012-08-03 17:30:46 +00001271 s += " {\n ";
Peter Collingbourne51d77772011-10-06 13:03:08 +00001272
1273 if (kind != OpNone)
1274 s += GenOpString(kind, proto, outTypeStr);
1275 else
1276 s += GenBuiltin(name, proto, outTypeStr, classKind);
1277 if (define)
1278 s += " })";
1279 else
1280 s += " }";
1281 s += "\n";
1282 return s;
1283}
1284
1285/// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h
1286/// is comprised of type definitions and function declarations.
1287void NeonEmitter::run(raw_ostream &OS) {
1288 OS <<
1289 "/*===---- arm_neon.h - ARM Neon intrinsics ------------------------------"
1290 "---===\n"
1291 " *\n"
1292 " * Permission is hereby granted, free of charge, to any person obtaining "
1293 "a copy\n"
1294 " * of this software and associated documentation files (the \"Software\"),"
1295 " to deal\n"
1296 " * in the Software without restriction, including without limitation the "
1297 "rights\n"
1298 " * to use, copy, modify, merge, publish, distribute, sublicense, "
1299 "and/or sell\n"
1300 " * copies of the Software, and to permit persons to whom the Software is\n"
1301 " * furnished to do so, subject to the following conditions:\n"
1302 " *\n"
1303 " * The above copyright notice and this permission notice shall be "
1304 "included in\n"
1305 " * all copies or substantial portions of the Software.\n"
1306 " *\n"
1307 " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, "
1308 "EXPRESS OR\n"
1309 " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
1310 "MERCHANTABILITY,\n"
1311 " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT "
1312 "SHALL THE\n"
1313 " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR "
1314 "OTHER\n"
1315 " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, "
1316 "ARISING FROM,\n"
1317 " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER "
1318 "DEALINGS IN\n"
1319 " * THE SOFTWARE.\n"
1320 " *\n"
1321 " *===--------------------------------------------------------------------"
1322 "---===\n"
1323 " */\n\n";
1324
1325 OS << "#ifndef __ARM_NEON_H\n";
1326 OS << "#define __ARM_NEON_H\n\n";
1327
1328 OS << "#ifndef __ARM_NEON__\n";
1329 OS << "#error \"NEON support not enabled\"\n";
1330 OS << "#endif\n\n";
1331
1332 OS << "#include <stdint.h>\n\n";
1333
1334 // Emit NEON-specific scalar typedefs.
1335 OS << "typedef float float32_t;\n";
1336 OS << "typedef int8_t poly8_t;\n";
1337 OS << "typedef int16_t poly16_t;\n";
1338 OS << "typedef uint16_t float16_t;\n";
1339
1340 // Emit Neon vector typedefs.
1341 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
1342 SmallVector<StringRef, 24> TDTypeVec;
1343 ParseTypes(0, TypedefTypes, TDTypeVec);
1344
1345 // Emit vector typedefs.
1346 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
1347 bool dummy, quad = false, poly = false;
1348 (void) ClassifyType(TDTypeVec[i], quad, poly, dummy);
1349 if (poly)
1350 OS << "typedef __attribute__((neon_polyvector_type(";
1351 else
1352 OS << "typedef __attribute__((neon_vector_type(";
1353
1354 unsigned nElts = GetNumElements(TDTypeVec[i], quad);
1355 OS << utostr(nElts) << "))) ";
1356 if (nElts < 10)
1357 OS << " ";
1358
1359 OS << TypeString('s', TDTypeVec[i]);
1360 OS << " " << TypeString('d', TDTypeVec[i]) << ";\n";
1361 }
1362 OS << "\n";
1363
1364 // Emit struct typedefs.
1365 for (unsigned vi = 2; vi != 5; ++vi) {
1366 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
1367 std::string ts = TypeString('d', TDTypeVec[i]);
1368 std::string vs = TypeString('0' + vi, TDTypeVec[i]);
1369 OS << "typedef struct " << vs << " {\n";
1370 OS << " " << ts << " val";
1371 OS << "[" << utostr(vi) << "]";
1372 OS << ";\n} ";
1373 OS << vs << ";\n\n";
1374 }
1375 }
1376
Bob Wilson1e8058f2013-04-12 20:17:20 +00001377 OS<<"#define __ai static inline __attribute__((__always_inline__, __nodebug__))\n\n";
Peter Collingbourne51d77772011-10-06 13:03:08 +00001378
1379 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1380
1381 // Emit vmovl, vmull and vabd intrinsics first so they can be used by other
1382 // intrinsics. (Some of the saturating multiply instructions are also
1383 // used to implement the corresponding "_lane" variants, but tablegen
1384 // sorts the records into alphabetical order so that the "_lane" variants
1385 // come after the intrinsics they use.)
1386 emitIntrinsic(OS, Records.getDef("VMOVL"));
1387 emitIntrinsic(OS, Records.getDef("VMULL"));
1388 emitIntrinsic(OS, Records.getDef("VABD"));
1389
1390 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1391 Record *R = RV[i];
1392 if (R->getName() != "VMOVL" &&
1393 R->getName() != "VMULL" &&
1394 R->getName() != "VABD")
1395 emitIntrinsic(OS, R);
1396 }
1397
1398 OS << "#undef __ai\n\n";
1399 OS << "#endif /* __ARM_NEON_H */\n";
1400}
1401
1402/// emitIntrinsic - Write out the arm_neon.h header file definitions for the
1403/// intrinsics specified by record R.
1404void NeonEmitter::emitIntrinsic(raw_ostream &OS, Record *R) {
1405 std::string name = R->getValueAsString("Name");
1406 std::string Proto = R->getValueAsString("Prototype");
1407 std::string Types = R->getValueAsString("Types");
1408
1409 SmallVector<StringRef, 16> TypeVec;
1410 ParseTypes(R, Types, TypeVec);
1411
1412 OpKind kind = OpMap[R->getValueAsDef("Operand")->getName()];
1413
1414 ClassKind classKind = ClassNone;
1415 if (R->getSuperClasses().size() >= 2)
1416 classKind = ClassMap[R->getSuperClasses()[1]];
1417 if (classKind == ClassNone && kind == OpNone)
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +00001418 PrintFatalError(R->getLoc(), "Builtin has no class kind");
Peter Collingbourne51d77772011-10-06 13:03:08 +00001419
1420 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1421 if (kind == OpReinterpret) {
1422 bool outQuad = false;
1423 bool dummy = false;
1424 (void)ClassifyType(TypeVec[ti], outQuad, dummy, dummy);
1425 for (unsigned srcti = 0, srcte = TypeVec.size();
1426 srcti != srcte; ++srcti) {
1427 bool inQuad = false;
1428 (void)ClassifyType(TypeVec[srcti], inQuad, dummy, dummy);
1429 if (srcti == ti || inQuad != outQuad)
1430 continue;
1431 OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[srcti],
1432 OpCast, ClassS);
1433 }
1434 } else {
1435 OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[ti],
1436 kind, classKind);
1437 }
1438 }
1439 OS << "\n";
1440}
1441
1442static unsigned RangeFromType(const char mod, StringRef typestr) {
1443 // base type to get the type string for.
1444 bool quad = false, dummy = false;
1445 char type = ClassifyType(typestr, quad, dummy, dummy);
1446 type = ModType(mod, type, quad, dummy, dummy, dummy, dummy, dummy);
1447
1448 switch (type) {
1449 case 'c':
1450 return (8 << (int)quad) - 1;
1451 case 'h':
1452 case 's':
1453 return (4 << (int)quad) - 1;
1454 case 'f':
1455 case 'i':
1456 return (2 << (int)quad) - 1;
1457 case 'l':
1458 return (1 << (int)quad) - 1;
1459 default:
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +00001460 PrintFatalError("unhandled type!");
Peter Collingbourne51d77772011-10-06 13:03:08 +00001461 }
Peter Collingbourne51d77772011-10-06 13:03:08 +00001462}
1463
1464/// runHeader - Emit a file with sections defining:
1465/// 1. the NEON section of BuiltinsARM.def.
1466/// 2. the SemaChecking code for the type overload checking.
Jim Grosbach667381b2012-05-09 18:17:30 +00001467/// 3. the SemaChecking code for validation of intrinsic immediate arguments.
Peter Collingbourne51d77772011-10-06 13:03:08 +00001468void NeonEmitter::runHeader(raw_ostream &OS) {
1469 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1470
1471 StringMap<OpKind> EmittedMap;
1472
1473 // Generate BuiltinsARM.def for NEON
1474 OS << "#ifdef GET_NEON_BUILTINS\n";
1475 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1476 Record *R = RV[i];
1477 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1478 if (k != OpNone)
1479 continue;
1480
1481 std::string Proto = R->getValueAsString("Prototype");
1482
1483 // Functions with 'a' (the splat code) in the type prototype should not get
1484 // their own builtin as they use the non-splat variant.
1485 if (Proto.find('a') != std::string::npos)
1486 continue;
1487
1488 std::string Types = R->getValueAsString("Types");
1489 SmallVector<StringRef, 16> TypeVec;
1490 ParseTypes(R, Types, TypeVec);
1491
1492 if (R->getSuperClasses().size() < 2)
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +00001493 PrintFatalError(R->getLoc(), "Builtin has no class kind");
Peter Collingbourne51d77772011-10-06 13:03:08 +00001494
1495 std::string name = R->getValueAsString("Name");
1496 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1497
1498 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1499 // Generate the BuiltinsARM.def declaration for this builtin, ensuring
1500 // that each unique BUILTIN() macro appears only once in the output
1501 // stream.
1502 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
1503 if (EmittedMap.count(bd))
1504 continue;
1505
1506 EmittedMap[bd] = OpNone;
1507 OS << bd << "\n";
1508 }
1509 }
1510 OS << "#endif\n\n";
1511
1512 // Generate the overloaded type checking code for SemaChecking.cpp
1513 OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
1514 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1515 Record *R = RV[i];
1516 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1517 if (k != OpNone)
1518 continue;
1519
1520 std::string Proto = R->getValueAsString("Prototype");
1521 std::string Types = R->getValueAsString("Types");
1522 std::string name = R->getValueAsString("Name");
1523
1524 // Functions with 'a' (the splat code) in the type prototype should not get
1525 // their own builtin as they use the non-splat variant.
1526 if (Proto.find('a') != std::string::npos)
1527 continue;
1528
1529 // Functions which have a scalar argument cannot be overloaded, no need to
1530 // check them if we are emitting the type checking code.
1531 if (Proto.find('s') != std::string::npos)
1532 continue;
1533
1534 SmallVector<StringRef, 16> TypeVec;
1535 ParseTypes(R, Types, TypeVec);
1536
1537 if (R->getSuperClasses().size() < 2)
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +00001538 PrintFatalError(R->getLoc(), "Builtin has no class kind");
Peter Collingbourne51d77772011-10-06 13:03:08 +00001539
1540 int si = -1, qi = -1;
Richard Smithf8ee6bc2012-08-14 01:28:02 +00001541 uint64_t mask = 0, qmask = 0;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001542 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1543 // Generate the switch case(s) for this builtin for the type validation.
1544 bool quad = false, poly = false, usgn = false;
1545 (void) ClassifyType(TypeVec[ti], quad, poly, usgn);
1546
1547 if (quad) {
1548 qi = ti;
Richard Smithf8ee6bc2012-08-14 01:28:02 +00001549 qmask |= 1ULL << GetNeonEnum(Proto, TypeVec[ti]);
Peter Collingbourne51d77772011-10-06 13:03:08 +00001550 } else {
1551 si = ti;
Richard Smithf8ee6bc2012-08-14 01:28:02 +00001552 mask |= 1ULL << GetNeonEnum(Proto, TypeVec[ti]);
Peter Collingbourne51d77772011-10-06 13:03:08 +00001553 }
1554 }
Bob Wilson46482552011-11-16 21:32:23 +00001555
1556 // Check if the builtin function has a pointer or const pointer argument.
1557 int PtrArgNum = -1;
1558 bool HasConstPtr = false;
1559 for (unsigned arg = 1, arge = Proto.size(); arg != arge; ++arg) {
1560 char ArgType = Proto[arg];
1561 if (ArgType == 'c') {
1562 HasConstPtr = true;
1563 PtrArgNum = arg - 1;
1564 break;
1565 }
1566 if (ArgType == 'p') {
1567 PtrArgNum = arg - 1;
1568 break;
1569 }
1570 }
1571 // For sret builtins, adjust the pointer argument index.
1572 if (PtrArgNum >= 0 && (Proto[0] >= '2' && Proto[0] <= '4'))
1573 PtrArgNum += 1;
1574
Bob Wilson9082cdd2011-12-20 06:16:48 +00001575 // Omit type checking for the pointer arguments of vld1_lane, vld1_dup,
1576 // and vst1_lane intrinsics. Using a pointer to the vector element
1577 // type with one of those operations causes codegen to select an aligned
1578 // load/store instruction. If you want an unaligned operation,
1579 // the pointer argument needs to have less alignment than element type,
1580 // so just accept any pointer type.
1581 if (name == "vld1_lane" || name == "vld1_dup" || name == "vst1_lane") {
1582 PtrArgNum = -1;
1583 HasConstPtr = false;
1584 }
1585
Bob Wilson6f9f03e2011-11-08 05:04:11 +00001586 if (mask) {
Peter Collingbourne51d77772011-10-06 13:03:08 +00001587 OS << "case ARM::BI__builtin_neon_"
1588 << MangleName(name, TypeVec[si], ClassB)
Richard Smithb27660a2012-08-14 03:55:16 +00001589 << ": mask = " << "0x" << utohexstr(mask) << "ULL";
Bob Wilson46482552011-11-16 21:32:23 +00001590 if (PtrArgNum >= 0)
1591 OS << "; PtrArgNum = " << PtrArgNum;
Bob Wilson6f9f03e2011-11-08 05:04:11 +00001592 if (HasConstPtr)
1593 OS << "; HasConstPtr = true";
1594 OS << "; break;\n";
1595 }
1596 if (qmask) {
Peter Collingbourne51d77772011-10-06 13:03:08 +00001597 OS << "case ARM::BI__builtin_neon_"
1598 << MangleName(name, TypeVec[qi], ClassB)
Richard Smithb27660a2012-08-14 03:55:16 +00001599 << ": mask = " << "0x" << utohexstr(qmask) << "ULL";
Bob Wilson46482552011-11-16 21:32:23 +00001600 if (PtrArgNum >= 0)
1601 OS << "; PtrArgNum = " << PtrArgNum;
Bob Wilson6f9f03e2011-11-08 05:04:11 +00001602 if (HasConstPtr)
1603 OS << "; HasConstPtr = true";
1604 OS << "; break;\n";
1605 }
Peter Collingbourne51d77772011-10-06 13:03:08 +00001606 }
1607 OS << "#endif\n\n";
1608
1609 // Generate the intrinsic range checking code for shift/lane immediates.
1610 OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
1611 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1612 Record *R = RV[i];
1613
1614 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1615 if (k != OpNone)
1616 continue;
1617
1618 std::string name = R->getValueAsString("Name");
1619 std::string Proto = R->getValueAsString("Prototype");
1620 std::string Types = R->getValueAsString("Types");
1621
1622 // Functions with 'a' (the splat code) in the type prototype should not get
1623 // their own builtin as they use the non-splat variant.
1624 if (Proto.find('a') != std::string::npos)
1625 continue;
1626
1627 // Functions which do not have an immediate do not need to have range
1628 // checking code emitted.
1629 size_t immPos = Proto.find('i');
1630 if (immPos == std::string::npos)
1631 continue;
1632
1633 SmallVector<StringRef, 16> TypeVec;
1634 ParseTypes(R, Types, TypeVec);
1635
1636 if (R->getSuperClasses().size() < 2)
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +00001637 PrintFatalError(R->getLoc(), "Builtin has no class kind");
Peter Collingbourne51d77772011-10-06 13:03:08 +00001638
1639 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1640
1641 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1642 std::string namestr, shiftstr, rangestr;
1643
1644 if (R->getValueAsBit("isVCVT_N")) {
1645 // VCVT between floating- and fixed-point values takes an immediate
1646 // in the range 1 to 32.
1647 ck = ClassB;
1648 rangestr = "l = 1; u = 31"; // upper bound = l + u
1649 } else if (Proto.find('s') == std::string::npos) {
1650 // Builtins which are overloaded by type will need to have their upper
1651 // bound computed at Sema time based on the type constant.
1652 ck = ClassB;
1653 if (R->getValueAsBit("isShift")) {
1654 shiftstr = ", true";
1655
1656 // Right shifts have an 'r' in the name, left shifts do not.
1657 if (name.find('r') != std::string::npos)
1658 rangestr = "l = 1; ";
1659 }
1660 rangestr += "u = RFT(TV" + shiftstr + ")";
1661 } else {
1662 // The immediate generally refers to a lane in the preceding argument.
1663 assert(immPos > 0 && "unexpected immediate operand");
1664 rangestr = "u = " + utostr(RangeFromType(Proto[immPos-1], TypeVec[ti]));
1665 }
1666 // Make sure cases appear only once by uniquing them in a string map.
1667 namestr = MangleName(name, TypeVec[ti], ck);
1668 if (EmittedMap.count(namestr))
1669 continue;
1670 EmittedMap[namestr] = OpNone;
1671
1672 // Calculate the index of the immediate that should be range checked.
1673 unsigned immidx = 0;
1674
1675 // Builtins that return a struct of multiple vectors have an extra
1676 // leading arg for the struct return.
1677 if (Proto[0] >= '2' && Proto[0] <= '4')
1678 ++immidx;
1679
1680 // Add one to the index for each argument until we reach the immediate
1681 // to be checked. Structs of vectors are passed as multiple arguments.
1682 for (unsigned ii = 1, ie = Proto.size(); ii != ie; ++ii) {
1683 switch (Proto[ii]) {
1684 default: immidx += 1; break;
1685 case '2': immidx += 2; break;
1686 case '3': immidx += 3; break;
1687 case '4': immidx += 4; break;
1688 case 'i': ie = ii + 1; break;
1689 }
1690 }
1691 OS << "case ARM::BI__builtin_neon_" << MangleName(name, TypeVec[ti], ck)
1692 << ": i = " << immidx << "; " << rangestr << "; break;\n";
1693 }
1694 }
1695 OS << "#endif\n\n";
1696}
1697
1698/// GenTest - Write out a test for the intrinsic specified by the name and
1699/// type strings, including the embedded patterns for FileCheck to match.
1700static std::string GenTest(const std::string &name,
1701 const std::string &proto,
1702 StringRef outTypeStr, StringRef inTypeStr,
1703 bool isShift) {
1704 assert(!proto.empty() && "");
1705 std::string s;
1706
1707 // Function name with type suffix
1708 std::string mangledName = MangleName(name, outTypeStr, ClassS);
1709 if (outTypeStr != inTypeStr) {
1710 // If the input type is different (e.g., for vreinterpret), append a suffix
1711 // for the input type. String off a "Q" (quad) prefix so that MangleName
1712 // does not insert another "q" in the name.
1713 unsigned typeStrOff = (inTypeStr[0] == 'Q' ? 1 : 0);
1714 StringRef inTypeNoQuad = inTypeStr.substr(typeStrOff);
1715 mangledName = MangleName(mangledName, inTypeNoQuad, ClassS);
1716 }
1717
1718 // Emit the FileCheck patterns.
1719 s += "// CHECK: test_" + mangledName + "\n";
1720 // s += "// CHECK: \n"; // FIXME: + expected instruction opcode.
1721
1722 // Emit the start of the test function.
1723 s += TypeString(proto[0], outTypeStr) + " test_" + mangledName + "(";
1724 char arg = 'a';
1725 std::string comma;
1726 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
1727 // Do not create arguments for values that must be immediate constants.
1728 if (proto[i] == 'i')
1729 continue;
1730 s += comma + TypeString(proto[i], inTypeStr) + " ";
1731 s.push_back(arg);
1732 comma = ", ";
1733 }
Jim Grosbachb4a54252012-05-30 18:18:29 +00001734 s += ") {\n ";
Peter Collingbourne51d77772011-10-06 13:03:08 +00001735
1736 if (proto[0] != 'v')
1737 s += "return ";
1738 s += mangledName + "(";
1739 arg = 'a';
1740 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
1741 if (proto[i] == 'i') {
1742 // For immediate operands, test the maximum value.
1743 if (isShift)
1744 s += "1"; // FIXME
1745 else
1746 // The immediate generally refers to a lane in the preceding argument.
1747 s += utostr(RangeFromType(proto[i-1], inTypeStr));
1748 } else {
1749 s.push_back(arg);
1750 }
1751 if ((i + 1) < e)
1752 s += ", ";
1753 }
1754 s += ");\n}\n\n";
1755 return s;
1756}
1757
1758/// runTests - Write out a complete set of tests for all of the Neon
1759/// intrinsics.
1760void NeonEmitter::runTests(raw_ostream &OS) {
1761 OS <<
1762 "// RUN: %clang_cc1 -triple thumbv7-apple-darwin \\\n"
1763 "// RUN: -target-cpu cortex-a9 -ffreestanding -S -o - %s | FileCheck %s\n"
1764 "\n"
1765 "#include <arm_neon.h>\n"
1766 "\n";
1767
1768 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1769 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1770 Record *R = RV[i];
1771 std::string name = R->getValueAsString("Name");
1772 std::string Proto = R->getValueAsString("Prototype");
1773 std::string Types = R->getValueAsString("Types");
1774 bool isShift = R->getValueAsBit("isShift");
1775
1776 SmallVector<StringRef, 16> TypeVec;
1777 ParseTypes(R, Types, TypeVec);
1778
1779 OpKind kind = OpMap[R->getValueAsDef("Operand")->getName()];
Jim Grosbach667381b2012-05-09 18:17:30 +00001780 if (kind == OpUnavailable)
1781 continue;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001782 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1783 if (kind == OpReinterpret) {
1784 bool outQuad = false;
1785 bool dummy = false;
1786 (void)ClassifyType(TypeVec[ti], outQuad, dummy, dummy);
1787 for (unsigned srcti = 0, srcte = TypeVec.size();
1788 srcti != srcte; ++srcti) {
1789 bool inQuad = false;
1790 (void)ClassifyType(TypeVec[srcti], inQuad, dummy, dummy);
1791 if (srcti == ti || inQuad != outQuad)
1792 continue;
1793 OS << GenTest(name, Proto, TypeVec[ti], TypeVec[srcti], isShift);
1794 }
1795 } else {
1796 OS << GenTest(name, Proto, TypeVec[ti], TypeVec[ti], isShift);
1797 }
1798 }
1799 OS << "\n";
1800 }
1801}
1802
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +00001803namespace clang {
1804void EmitNeon(RecordKeeper &Records, raw_ostream &OS) {
1805 NeonEmitter(Records).run(OS);
1806}
1807void EmitNeonSema(RecordKeeper &Records, raw_ostream &OS) {
1808 NeonEmitter(Records).runHeader(OS);
1809}
1810void EmitNeonTest(RecordKeeper &Records, raw_ostream &OS) {
1811 NeonEmitter(Records).runTests(OS);
1812}
1813} // End namespace clang