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