blob: bb505de95daa6959e33b704868f63a710b510845 [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 }
Michael Gottesmanc327f872013-04-16 23:00:26 +0000683
Peter Collingbourne51d77772011-10-06 13:03:08 +0000684 return s;
685}
686
Michael Gottesmanc327f872013-04-16 23:00:26 +0000687static void PreprocessInstruction(const StringRef &Name,
688 const std::string &InstName,
689 std::string &Prefix,
690 bool &HasNPostfix,
691 bool &HasLanePostfix,
692 bool &HasDupPostfix,
693 bool &IsSpecialVCvt,
694 size_t &TBNumber) {
695 // All of our instruction name fields from arm_neon.td are of the form
696 // <instructionname>_...
697 // Thus we grab our instruction name via computation of said Prefix.
698 const size_t PrefixEnd = Name.find_first_of('_');
699 // If InstName is passed in, we use that instead of our name Prefix.
700 Prefix = InstName.size() == 0? Name.slice(0, PrefixEnd).str() : InstName;
701
702 const StringRef Postfix = Name.slice(PrefixEnd, Name.size());
703
704 HasNPostfix = Postfix.count("_n");
705 HasLanePostfix = Postfix.count("_lane");
706 HasDupPostfix = Postfix.count("_dup");
707 IsSpecialVCvt = Postfix.size() != 0 && Name.count("vcvt");
708
709 if (InstName.compare("vtbl") == 0 ||
710 InstName.compare("vtbx") == 0) {
711 // If we have a vtblN/vtbxN instruction, use the instruction's ASCII
712 // encoding to get its true value.
713 TBNumber = Name[Name.size()-1] - 48;
714 }
715}
716
717/// GenerateRegisterCheckPatternsForLoadStores - Given a bunch of data we have
718/// extracted, generate a FileCheck pattern for a Load Or Store
719static void
720GenerateRegisterCheckPatternForLoadStores(const StringRef &NameRef,
721 const std::string& OutTypeCode,
722 const bool &IsQuad,
723 const bool &HasDupPostfix,
724 const bool &HasLanePostfix,
725 const size_t Count,
726 std::string &RegisterSuffix) {
727 const bool IsLDSTOne = NameRef.count("vld1") || NameRef.count("vst1");
728 // If N == 3 || N == 4 and we are dealing with a quad instruction, Clang
729 // will output a series of v{ld,st}1s, so we have to handle it specially.
730 if ((Count == 3 || Count == 4) && IsQuad) {
731 RegisterSuffix += "{";
732 for (size_t i = 0; i < Count; i++) {
733 RegisterSuffix += "d{{[0-9]+}}";
734 if (HasDupPostfix) {
735 RegisterSuffix += "[]";
736 }
737 if (HasLanePostfix) {
738 RegisterSuffix += "[{{[0-9]+}}]";
739 }
740 if (i < Count-1) {
741 RegisterSuffix += ", ";
742 }
743 }
744 RegisterSuffix += "}";
745 } else {
746
747 // Handle normal loads and stores.
748 RegisterSuffix += "{";
749 for (size_t i = 0; i < Count; i++) {
750 RegisterSuffix += "d{{[0-9]+}}";
751 if (HasDupPostfix) {
752 RegisterSuffix += "[]";
753 }
754 if (HasLanePostfix) {
755 RegisterSuffix += "[{{[0-9]+}}]";
756 }
757 if (IsQuad && !HasLanePostfix) {
758 RegisterSuffix += ", d{{[0-9]+}}";
759 if (HasDupPostfix) {
760 RegisterSuffix += "[]";
761 }
762 }
763 if (i < Count-1) {
764 RegisterSuffix += ", ";
765 }
766 }
767 RegisterSuffix += "}, [r{{[0-9]+}}";
768
769 // We only include the alignment hint if we have a vld1.*64 or
770 // a dup/lane instruction.
771 if (IsLDSTOne) {
772 if ((HasLanePostfix || HasDupPostfix) && OutTypeCode != "8") {
Michael Gottesman410c3f72013-06-24 21:25:37 +0000773 RegisterSuffix += ":" + OutTypeCode;
Michael Gottesmanc327f872013-04-16 23:00:26 +0000774 }
775 }
776
777 RegisterSuffix += "]";
778 }
779}
780
781static bool HasNPostfixAndScalarArgs(const StringRef &NameRef,
782 const bool &HasNPostfix) {
783 return (NameRef.count("vmla") ||
784 NameRef.count("vmlal") ||
785 NameRef.count("vmlsl") ||
786 NameRef.count("vmull") ||
787 NameRef.count("vqdmlal") ||
788 NameRef.count("vqdmlsl") ||
789 NameRef.count("vqdmulh") ||
790 NameRef.count("vqdmull") ||
791 NameRef.count("vqrdmulh")) && HasNPostfix;
792}
793
794static bool IsFiveOperandLaneAccumulator(const StringRef &NameRef,
795 const bool &HasLanePostfix) {
796 return (NameRef.count("vmla") ||
797 NameRef.count("vmls") ||
798 NameRef.count("vmlal") ||
799 NameRef.count("vmlsl") ||
800 (NameRef.count("vmul") && NameRef.size() == 3)||
801 NameRef.count("vqdmlal") ||
802 NameRef.count("vqdmlsl") ||
803 NameRef.count("vqdmulh") ||
804 NameRef.count("vqrdmulh")) && HasLanePostfix;
805}
806
807static bool IsSpecialLaneMultiply(const StringRef &NameRef,
808 const bool &HasLanePostfix,
809 const bool &IsQuad) {
810 const bool IsVMulOrMulh = (NameRef.count("vmul") || NameRef.count("mulh"))
811 && IsQuad;
812 const bool IsVMull = NameRef.count("mull") && !IsQuad;
813 return (IsVMulOrMulh || IsVMull) && HasLanePostfix;
814}
815
816static void NormalizeProtoForRegisterPatternCreation(const std::string &Name,
817 const std::string &Proto,
818 const bool &HasNPostfix,
819 const bool &IsQuad,
820 const bool &HasLanePostfix,
821 const bool &HasDupPostfix,
822 std::string &NormedProto) {
823 // Handle generic case.
824 const StringRef NameRef(Name);
825 for (size_t i = 0, end = Proto.size(); i < end; i++) {
826 switch (Proto[i]) {
827 case 'u':
828 case 'f':
829 case 'd':
830 case 's':
831 case 'x':
832 case 't':
833 case 'n':
834 NormedProto += IsQuad? 'q' : 'd';
835 break;
836 case 'w':
837 case 'k':
838 NormedProto += 'q';
839 break;
840 case 'g':
841 case 'h':
842 case 'e':
843 NormedProto += 'd';
844 break;
845 case 'i':
846 NormedProto += HasLanePostfix? 'a' : 'i';
847 break;
848 case 'a':
849 if (HasLanePostfix) {
850 NormedProto += 'a';
851 } else if (HasNPostfixAndScalarArgs(NameRef, HasNPostfix)) {
852 NormedProto += IsQuad? 'q' : 'd';
853 } else {
854 NormedProto += 'i';
855 }
856 break;
857 }
858 }
859
860 // Handle Special Cases.
861 const bool IsNotVExt = !NameRef.count("vext");
862 const bool IsVPADAL = NameRef.count("vpadal");
863 const bool Is5OpLaneAccum = IsFiveOperandLaneAccumulator(NameRef,
864 HasLanePostfix);
865 const bool IsSpecialLaneMul = IsSpecialLaneMultiply(NameRef, HasLanePostfix,
866 IsQuad);
867
868 if (IsSpecialLaneMul) {
869 // If
870 NormedProto[2] = NormedProto[3];
871 NormedProto.erase(3);
872 } else if (NormedProto.size() == 4 &&
873 NormedProto[0] == NormedProto[1] &&
874 IsNotVExt) {
875 // If NormedProto.size() == 4 and the first two proto characters are the
876 // same, ignore the first.
877 NormedProto = NormedProto.substr(1, 3);
878 } else if (Is5OpLaneAccum) {
879 // If we have a 5 op lane accumulator operation, we take characters 1,2,4
880 std::string tmp = NormedProto.substr(1,2);
881 tmp += NormedProto[4];
882 NormedProto = tmp;
883 } else if (IsVPADAL) {
884 // If we have VPADAL, ignore the first character.
885 NormedProto = NormedProto.substr(0, 2);
886 } else if (NameRef.count("vdup") && NormedProto.size() > 2) {
887 // If our instruction is a dup instruction, keep only the first and
888 // last characters.
889 std::string tmp = "";
890 tmp += NormedProto[0];
891 tmp += NormedProto[NormedProto.size()-1];
892 NormedProto = tmp;
893 }
894}
895
896/// GenerateRegisterCheckPatterns - Given a bunch of data we have
897/// extracted, generate a FileCheck pattern to check that an
898/// instruction's arguments are correct.
899static void GenerateRegisterCheckPattern(const std::string &Name,
900 const std::string &Proto,
901 const std::string &OutTypeCode,
902 const bool &HasNPostfix,
903 const bool &IsQuad,
904 const bool &HasLanePostfix,
905 const bool &HasDupPostfix,
906 const size_t &TBNumber,
907 std::string &RegisterSuffix) {
908
909 RegisterSuffix = "";
910
911 const StringRef NameRef(Name);
912 const StringRef ProtoRef(Proto);
913
914 if ((NameRef.count("vdup") || NameRef.count("vmov")) && HasNPostfix) {
915 return;
916 }
917
918 const bool IsLoadStore = NameRef.count("vld") || NameRef.count("vst");
919 const bool IsTBXOrTBL = NameRef.count("vtbl") || NameRef.count("vtbx");
920
921 if (IsLoadStore) {
922 // Grab N value from v{ld,st}N using its ascii representation.
923 const size_t Count = NameRef[3] - 48;
924
925 GenerateRegisterCheckPatternForLoadStores(NameRef, OutTypeCode, IsQuad,
926 HasDupPostfix, HasLanePostfix,
927 Count, RegisterSuffix);
928 } else if (IsTBXOrTBL) {
929 RegisterSuffix += "d{{[0-9]+}}, {";
930 for (size_t i = 0; i < TBNumber-1; i++) {
931 RegisterSuffix += "d{{[0-9]+}}, ";
932 }
933 RegisterSuffix += "d{{[0-9]+}}}, d{{[0-9]+}}";
934 } else {
935 // Handle a normal instruction.
936 if (NameRef.count("vget") || NameRef.count("vset"))
937 return;
938
939 // We first normalize our proto, since we only need to emit 4
940 // different types of checks, yet have more than 4 proto types
941 // that map onto those 4 patterns.
942 std::string NormalizedProto("");
943 NormalizeProtoForRegisterPatternCreation(Name, Proto, HasNPostfix, IsQuad,
944 HasLanePostfix, HasDupPostfix,
945 NormalizedProto);
946
947 for (size_t i = 0, end = NormalizedProto.size(); i < end; i++) {
948 const char &c = NormalizedProto[i];
949 switch (c) {
950 case 'q':
951 RegisterSuffix += "q{{[0-9]+}}, ";
952 break;
953
954 case 'd':
955 RegisterSuffix += "d{{[0-9]+}}, ";
956 break;
957
958 case 'i':
959 RegisterSuffix += "#{{[0-9]+}}, ";
960 break;
961
962 case 'a':
963 RegisterSuffix += "d{{[0-9]+}}[{{[0-9]}}], ";
964 break;
965 }
966 }
967
968 // Remove extra ", ".
969 RegisterSuffix = RegisterSuffix.substr(0, RegisterSuffix.size()-2);
970 }
971}
972
973/// GenerateChecksForIntrinsic - Given a specific instruction name +
974/// typestr + class kind, generate the proper set of FileCheck
975/// Patterns to check for. We could just return a string, but instead
976/// use a vector since it provides us with the extra flexibility of
977/// emitting multiple checks, which comes in handy for certain cases
978/// like mla where we want to check for 2 different instructions.
979static void GenerateChecksForIntrinsic(const std::string &Name,
980 const std::string &Proto,
981 StringRef &OutTypeStr,
982 StringRef &InTypeStr,
983 ClassKind Ck,
984 const std::string &InstName,
985 bool IsHiddenLOp,
986 std::vector<std::string>& Result) {
987
988 // If Ck is a ClassNoTest instruction, just return so no test is
989 // emitted.
990 if(Ck == ClassNoTest)
991 return;
992
993 if (Name == "vcvt_f32_f16") {
994 Result.push_back("vcvt.f32.f16");
995 return;
996 }
997
998
999 // Now we preprocess our instruction given the data we have to get the
1000 // data that we need.
1001 // Create a StringRef for String Manipulation of our Name.
1002 const StringRef NameRef(Name);
1003 // Instruction Prefix.
1004 std::string Prefix;
1005 // The type code for our out type string.
1006 std::string OutTypeCode;
1007 // To handle our different cases, we need to check for different postfixes.
1008 // Is our instruction a quad instruction.
1009 bool IsQuad = false;
1010 // Our instruction is of the form <instructionname>_n.
1011 bool HasNPostfix = false;
1012 // Our instruction is of the form <instructionname>_lane.
1013 bool HasLanePostfix = false;
1014 // Our instruction is of the form <instructionname>_dup.
1015 bool HasDupPostfix = false;
1016 // Our instruction is a vcvt instruction which requires special handling.
1017 bool IsSpecialVCvt = false;
1018 // If we have a vtbxN or vtblN instruction, this is set to N.
1019 size_t TBNumber = -1;
1020 // Register Suffix
1021 std::string RegisterSuffix;
1022
1023 PreprocessInstruction(NameRef, InstName, Prefix,
1024 HasNPostfix, HasLanePostfix, HasDupPostfix,
1025 IsSpecialVCvt, TBNumber);
1026
1027 InstructionTypeCode(OutTypeStr, Ck, IsQuad, OutTypeCode);
1028 GenerateRegisterCheckPattern(Name, Proto, OutTypeCode, HasNPostfix, IsQuad,
1029 HasLanePostfix, HasDupPostfix, TBNumber,
1030 RegisterSuffix);
1031
1032 // In the following section, we handle a bunch of special cases. You can tell
1033 // a special case by the fact we are returning early.
1034
1035 // If our instruction is a logical instruction without postfix or a
1036 // hidden LOp just return the current Prefix.
1037 if (Ck == ClassL || IsHiddenLOp) {
1038 Result.push_back(Prefix + " " + RegisterSuffix);
1039 return;
1040 }
1041
1042 // If we have a vmov, due to the many different cases, some of which
1043 // vary within the different intrinsics generated for a single
1044 // instruction type, just output a vmov. (e.g. given an instruction
1045 // A, A.u32 might be vmov and A.u8 might be vmov.8).
1046 //
1047 // FIXME: Maybe something can be done about this. The two cases that we care
1048 // about are vmov as an LType and vmov as a WType.
1049 if (Prefix == "vmov") {
1050 Result.push_back(Prefix + " " + RegisterSuffix);
1051 return;
1052 }
1053
1054 // In the following section, we handle special cases.
1055
1056 if (OutTypeCode == "64") {
1057 // If we have a 64 bit vdup/vext and are handling an uint64x1_t
1058 // type, the intrinsic will be optimized away, so just return
1059 // nothing. On the other hand if we are handling an uint64x2_t
1060 // (i.e. quad instruction), vdup/vmov instructions should be
1061 // emitted.
1062 if (Prefix == "vdup" || Prefix == "vext") {
1063 if (IsQuad) {
1064 Result.push_back("{{vmov|vdup}}");
1065 }
1066 return;
1067 }
1068
1069 // v{st,ld}{2,3,4}_{u,s}64 emit v{st,ld}1.64 instructions with
1070 // multiple register operands.
1071 bool MultiLoadPrefix = Prefix == "vld2" || Prefix == "vld3"
1072 || Prefix == "vld4";
1073 bool MultiStorePrefix = Prefix == "vst2" || Prefix == "vst3"
1074 || Prefix == "vst4";
1075 if (MultiLoadPrefix || MultiStorePrefix) {
1076 Result.push_back(NameRef.slice(0, 3).str() + "1.64");
1077 return;
1078 }
1079
1080 // v{st,ld}1_{lane,dup}_{u64,s64} use vldr/vstr/vmov/str instead of
1081 // emitting said instructions. So return a check for
1082 // vldr/vstr/vmov/str instead.
1083 if (HasLanePostfix || HasDupPostfix) {
1084 if (Prefix == "vst1") {
1085 Result.push_back("{{str|vstr|vmov}}");
1086 return;
1087 } else if (Prefix == "vld1") {
1088 Result.push_back("{{ldr|vldr|vmov}}");
1089 return;
1090 }
1091 }
1092 }
1093
1094 // vzip.32/vuzp.32 are the same instruction as vtrn.32 and are
1095 // sometimes disassembled as vtrn.32. We use a regex to handle both
1096 // cases.
1097 if ((Prefix == "vzip" || Prefix == "vuzp") && OutTypeCode == "32") {
1098 Result.push_back("{{vtrn|" + Prefix + "}}.32 " + RegisterSuffix);
1099 return;
1100 }
1101
1102 // Currently on most ARM processors, we do not use vmla/vmls for
1103 // quad floating point operations. Instead we output vmul + vadd. So
1104 // check if we have one of those instructions and just output a
1105 // check for vmul.
1106 if (OutTypeCode == "f32") {
1107 if (Prefix == "vmls") {
1108 Result.push_back("vmul." + OutTypeCode + " " + RegisterSuffix);
1109 Result.push_back("vsub." + OutTypeCode);
1110 return;
1111 } else if (Prefix == "vmla") {
1112 Result.push_back("vmul." + OutTypeCode + " " + RegisterSuffix);
1113 Result.push_back("vadd." + OutTypeCode);
1114 return;
1115 }
1116 }
1117
1118 // If we have vcvt, get the input type from the instruction name
1119 // (which should be of the form instname_inputtype) and append it
1120 // before the output type.
1121 if (Prefix == "vcvt") {
1122 const std::string inTypeCode = NameRef.substr(NameRef.find_last_of("_")+1);
1123 Prefix += "." + inTypeCode;
1124 }
1125
1126 // Append output type code to get our final mangled instruction.
1127 Prefix += "." + OutTypeCode;
1128
1129 Result.push_back(Prefix + " " + RegisterSuffix);
1130}
1131
Peter Collingbourne51d77772011-10-06 13:03:08 +00001132/// UseMacro - Examine the prototype string to determine if the intrinsic
1133/// should be defined as a preprocessor macro instead of an inline function.
1134static bool UseMacro(const std::string &proto) {
1135 // If this builtin takes an immediate argument, we need to #define it rather
1136 // than use a standard declaration, so that SemaChecking can range check
1137 // the immediate passed by the user.
1138 if (proto.find('i') != std::string::npos)
1139 return true;
1140
1141 // Pointer arguments need to use macros to avoid hiding aligned attributes
1142 // from the pointer type.
1143 if (proto.find('p') != std::string::npos ||
1144 proto.find('c') != std::string::npos)
1145 return true;
1146
1147 return false;
1148}
1149
1150/// MacroArgUsedDirectly - Return true if argument i for an intrinsic that is
1151/// defined as a macro should be accessed directly instead of being first
1152/// assigned to a local temporary.
1153static bool MacroArgUsedDirectly(const std::string &proto, unsigned i) {
1154 // True for constant ints (i), pointers (p) and const pointers (c).
1155 return (proto[i] == 'i' || proto[i] == 'p' || proto[i] == 'c');
1156}
1157
1158// Generate the string "(argtype a, argtype b, ...)"
1159static std::string GenArgs(const std::string &proto, StringRef typestr) {
1160 bool define = UseMacro(proto);
1161 char arg = 'a';
1162
1163 std::string s;
1164 s += "(";
1165
1166 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
1167 if (define) {
1168 // Some macro arguments are used directly instead of being assigned
1169 // to local temporaries; prepend an underscore prefix to make their
1170 // names consistent with the local temporaries.
1171 if (MacroArgUsedDirectly(proto, i))
1172 s += "__";
1173 } else {
1174 s += TypeString(proto[i], typestr) + " __";
1175 }
1176 s.push_back(arg);
1177 if ((i + 1) < e)
1178 s += ", ";
1179 }
1180
1181 s += ")";
1182 return s;
1183}
1184
1185// Macro arguments are not type-checked like inline function arguments, so
1186// assign them to local temporaries to get the right type checking.
1187static std::string GenMacroLocals(const std::string &proto, StringRef typestr) {
1188 char arg = 'a';
1189 std::string s;
1190 bool generatedLocal = false;
1191
1192 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
1193 // Do not create a temporary for an immediate argument.
1194 // That would defeat the whole point of using a macro!
Peter Collingbourne51d77772011-10-06 13:03:08 +00001195 if (MacroArgUsedDirectly(proto, i))
1196 continue;
1197 generatedLocal = true;
1198
1199 s += TypeString(proto[i], typestr) + " __";
1200 s.push_back(arg);
1201 s += " = (";
1202 s.push_back(arg);
1203 s += "); ";
1204 }
1205
1206 if (generatedLocal)
1207 s += "\\\n ";
1208 return s;
1209}
1210
1211// Use the vmovl builtin to sign-extend or zero-extend a vector.
1212static std::string Extend(StringRef typestr, const std::string &a) {
1213 std::string s;
1214 s = MangleName("vmovl", typestr, ClassS);
1215 s += "(" + a + ")";
1216 return s;
1217}
1218
1219static std::string Duplicate(unsigned nElts, StringRef typestr,
1220 const std::string &a) {
1221 std::string s;
1222
1223 s = "(" + TypeString('d', typestr) + "){ ";
1224 for (unsigned i = 0; i != nElts; ++i) {
1225 s += a;
1226 if ((i + 1) < nElts)
1227 s += ", ";
1228 }
1229 s += " }";
1230
1231 return s;
1232}
1233
1234static std::string SplatLane(unsigned nElts, const std::string &vec,
1235 const std::string &lane) {
1236 std::string s = "__builtin_shufflevector(" + vec + ", " + vec;
1237 for (unsigned i = 0; i < nElts; ++i)
1238 s += ", " + lane;
1239 s += ")";
1240 return s;
1241}
1242
1243static unsigned GetNumElements(StringRef typestr, bool &quad) {
1244 quad = false;
1245 bool dummy = false;
1246 char type = ClassifyType(typestr, quad, dummy, dummy);
1247 unsigned nElts = 0;
1248 switch (type) {
1249 case 'c': nElts = 8; break;
1250 case 's': nElts = 4; break;
1251 case 'i': nElts = 2; break;
1252 case 'l': nElts = 1; break;
1253 case 'h': nElts = 4; break;
1254 case 'f': nElts = 2; break;
1255 default:
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +00001256 PrintFatalError("unhandled type!");
Peter Collingbourne51d77772011-10-06 13:03:08 +00001257 }
1258 if (quad) nElts <<= 1;
1259 return nElts;
1260}
1261
1262// Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
1263static std::string GenOpString(OpKind op, const std::string &proto,
1264 StringRef typestr) {
1265 bool quad;
1266 unsigned nElts = GetNumElements(typestr, quad);
1267 bool define = UseMacro(proto);
1268
1269 std::string ts = TypeString(proto[0], typestr);
1270 std::string s;
1271 if (!define) {
1272 s = "return ";
1273 }
1274
1275 switch(op) {
1276 case OpAdd:
1277 s += "__a + __b;";
1278 break;
1279 case OpAddl:
1280 s += Extend(typestr, "__a") + " + " + Extend(typestr, "__b") + ";";
1281 break;
1282 case OpAddw:
1283 s += "__a + " + Extend(typestr, "__b") + ";";
1284 break;
1285 case OpSub:
1286 s += "__a - __b;";
1287 break;
1288 case OpSubl:
1289 s += Extend(typestr, "__a") + " - " + Extend(typestr, "__b") + ";";
1290 break;
1291 case OpSubw:
1292 s += "__a - " + Extend(typestr, "__b") + ";";
1293 break;
1294 case OpMulN:
1295 s += "__a * " + Duplicate(nElts, typestr, "__b") + ";";
1296 break;
1297 case OpMulLane:
1298 s += "__a * " + SplatLane(nElts, "__b", "__c") + ";";
1299 break;
1300 case OpMul:
1301 s += "__a * __b;";
1302 break;
1303 case OpMullLane:
1304 s += MangleName("vmull", typestr, ClassS) + "(__a, " +
1305 SplatLane(nElts, "__b", "__c") + ");";
1306 break;
1307 case OpMlaN:
1308 s += "__a + (__b * " + Duplicate(nElts, typestr, "__c") + ");";
1309 break;
1310 case OpMlaLane:
1311 s += "__a + (__b * " + SplatLane(nElts, "__c", "__d") + ");";
1312 break;
1313 case OpMla:
1314 s += "__a + (__b * __c);";
1315 break;
1316 case OpMlalN:
1317 s += "__a + " + MangleName("vmull", typestr, ClassS) + "(__b, " +
1318 Duplicate(nElts, typestr, "__c") + ");";
1319 break;
1320 case OpMlalLane:
1321 s += "__a + " + MangleName("vmull", typestr, ClassS) + "(__b, " +
1322 SplatLane(nElts, "__c", "__d") + ");";
1323 break;
1324 case OpMlal:
1325 s += "__a + " + MangleName("vmull", typestr, ClassS) + "(__b, __c);";
1326 break;
1327 case OpMlsN:
1328 s += "__a - (__b * " + Duplicate(nElts, typestr, "__c") + ");";
1329 break;
1330 case OpMlsLane:
1331 s += "__a - (__b * " + SplatLane(nElts, "__c", "__d") + ");";
1332 break;
1333 case OpMls:
1334 s += "__a - (__b * __c);";
1335 break;
1336 case OpMlslN:
1337 s += "__a - " + MangleName("vmull", typestr, ClassS) + "(__b, " +
1338 Duplicate(nElts, typestr, "__c") + ");";
1339 break;
1340 case OpMlslLane:
1341 s += "__a - " + MangleName("vmull", typestr, ClassS) + "(__b, " +
1342 SplatLane(nElts, "__c", "__d") + ");";
1343 break;
1344 case OpMlsl:
1345 s += "__a - " + MangleName("vmull", typestr, ClassS) + "(__b, __c);";
1346 break;
1347 case OpQDMullLane:
1348 s += MangleName("vqdmull", typestr, ClassS) + "(__a, " +
1349 SplatLane(nElts, "__b", "__c") + ");";
1350 break;
1351 case OpQDMlalLane:
1352 s += MangleName("vqdmlal", typestr, ClassS) + "(__a, __b, " +
1353 SplatLane(nElts, "__c", "__d") + ");";
1354 break;
1355 case OpQDMlslLane:
1356 s += MangleName("vqdmlsl", typestr, ClassS) + "(__a, __b, " +
1357 SplatLane(nElts, "__c", "__d") + ");";
1358 break;
1359 case OpQDMulhLane:
1360 s += MangleName("vqdmulh", typestr, ClassS) + "(__a, " +
1361 SplatLane(nElts, "__b", "__c") + ");";
1362 break;
1363 case OpQRDMulhLane:
1364 s += MangleName("vqrdmulh", typestr, ClassS) + "(__a, " +
1365 SplatLane(nElts, "__b", "__c") + ");";
1366 break;
1367 case OpEq:
1368 s += "(" + ts + ")(__a == __b);";
1369 break;
1370 case OpGe:
1371 s += "(" + ts + ")(__a >= __b);";
1372 break;
1373 case OpLe:
1374 s += "(" + ts + ")(__a <= __b);";
1375 break;
1376 case OpGt:
1377 s += "(" + ts + ")(__a > __b);";
1378 break;
1379 case OpLt:
1380 s += "(" + ts + ")(__a < __b);";
1381 break;
1382 case OpNeg:
1383 s += " -__a;";
1384 break;
1385 case OpNot:
1386 s += " ~__a;";
1387 break;
1388 case OpAnd:
1389 s += "__a & __b;";
1390 break;
1391 case OpOr:
1392 s += "__a | __b;";
1393 break;
1394 case OpXor:
1395 s += "__a ^ __b;";
1396 break;
1397 case OpAndNot:
1398 s += "__a & ~__b;";
1399 break;
1400 case OpOrNot:
1401 s += "__a | ~__b;";
1402 break;
1403 case OpCast:
1404 s += "(" + ts + ")__a;";
1405 break;
1406 case OpConcat:
1407 s += "(" + ts + ")__builtin_shufflevector((int64x1_t)__a";
1408 s += ", (int64x1_t)__b, 0, 1);";
1409 break;
1410 case OpHi:
Jim Grosbachcd765392013-05-15 02:40:04 +00001411 // nElts is for the result vector, so the source is twice that number.
1412 s += "__builtin_shufflevector(__a, __a";
1413 for (unsigned i = nElts; i < nElts * 2; ++i)
1414 s += ", " + utostr(i);
1415 s+= ");";
Peter Collingbourne51d77772011-10-06 13:03:08 +00001416 break;
1417 case OpLo:
Jim Grosbachcd765392013-05-15 02:40:04 +00001418 s += "__builtin_shufflevector(__a, __a";
1419 for (unsigned i = 0; i < nElts; ++i)
1420 s += ", " + utostr(i);
1421 s+= ");";
Peter Collingbourne51d77772011-10-06 13:03:08 +00001422 break;
1423 case OpDup:
1424 s += Duplicate(nElts, typestr, "__a") + ";";
1425 break;
1426 case OpDupLane:
1427 s += SplatLane(nElts, "__a", "__b") + ";";
1428 break;
1429 case OpSelect:
1430 // ((0 & 1) | (~0 & 2))
1431 s += "(" + ts + ")";
1432 ts = TypeString(proto[1], typestr);
1433 s += "((__a & (" + ts + ")__b) | ";
1434 s += "(~__a & (" + ts + ")__c));";
1435 break;
1436 case OpRev16:
1437 s += "__builtin_shufflevector(__a, __a";
1438 for (unsigned i = 2; i <= nElts; i += 2)
1439 for (unsigned j = 0; j != 2; ++j)
1440 s += ", " + utostr(i - j - 1);
1441 s += ");";
1442 break;
1443 case OpRev32: {
1444 unsigned WordElts = nElts >> (1 + (int)quad);
1445 s += "__builtin_shufflevector(__a, __a";
1446 for (unsigned i = WordElts; i <= nElts; i += WordElts)
1447 for (unsigned j = 0; j != WordElts; ++j)
1448 s += ", " + utostr(i - j - 1);
1449 s += ");";
1450 break;
1451 }
1452 case OpRev64: {
1453 unsigned DblWordElts = nElts >> (int)quad;
1454 s += "__builtin_shufflevector(__a, __a";
1455 for (unsigned i = DblWordElts; i <= nElts; i += DblWordElts)
1456 for (unsigned j = 0; j != DblWordElts; ++j)
1457 s += ", " + utostr(i - j - 1);
1458 s += ");";
1459 break;
1460 }
1461 case OpAbdl: {
1462 std::string abd = MangleName("vabd", typestr, ClassS) + "(__a, __b)";
1463 if (typestr[0] != 'U') {
1464 // vabd results are always unsigned and must be zero-extended.
1465 std::string utype = "U" + typestr.str();
1466 s += "(" + TypeString(proto[0], typestr) + ")";
1467 abd = "(" + TypeString('d', utype) + ")" + abd;
1468 s += Extend(utype, abd) + ";";
1469 } else {
1470 s += Extend(typestr, abd) + ";";
1471 }
1472 break;
1473 }
1474 case OpAba:
1475 s += "__a + " + MangleName("vabd", typestr, ClassS) + "(__b, __c);";
1476 break;
1477 case OpAbal: {
1478 s += "__a + ";
1479 std::string abd = MangleName("vabd", typestr, ClassS) + "(__b, __c)";
1480 if (typestr[0] != 'U') {
1481 // vabd results are always unsigned and must be zero-extended.
1482 std::string utype = "U" + typestr.str();
1483 s += "(" + TypeString(proto[0], typestr) + ")";
1484 abd = "(" + TypeString('d', utype) + ")" + abd;
1485 s += Extend(utype, abd) + ";";
1486 } else {
1487 s += Extend(typestr, abd) + ";";
1488 }
1489 break;
1490 }
1491 default:
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +00001492 PrintFatalError("unknown OpKind!");
Peter Collingbourne51d77772011-10-06 13:03:08 +00001493 }
1494 return s;
1495}
1496
1497static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
1498 unsigned mod = proto[0];
Peter Collingbourne51d77772011-10-06 13:03:08 +00001499
1500 if (mod == 'v' || mod == 'f')
1501 mod = proto[1];
1502
1503 bool quad = false;
1504 bool poly = false;
1505 bool usgn = false;
1506 bool scal = false;
1507 bool cnst = false;
1508 bool pntr = false;
1509
1510 // Base type to get the type string for.
1511 char type = ClassifyType(typestr, quad, poly, usgn);
1512
1513 // Based on the modifying character, change the type and width if necessary.
1514 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
1515
Bob Wilsonda95f732011-11-08 01:16:11 +00001516 NeonTypeFlags::EltType ET;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001517 switch (type) {
1518 case 'c':
Bob Wilsonda95f732011-11-08 01:16:11 +00001519 ET = poly ? NeonTypeFlags::Poly8 : NeonTypeFlags::Int8;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001520 break;
1521 case 's':
Bob Wilsonda95f732011-11-08 01:16:11 +00001522 ET = poly ? NeonTypeFlags::Poly16 : NeonTypeFlags::Int16;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001523 break;
1524 case 'i':
Bob Wilsonda95f732011-11-08 01:16:11 +00001525 ET = NeonTypeFlags::Int32;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001526 break;
1527 case 'l':
Bob Wilsonda95f732011-11-08 01:16:11 +00001528 ET = NeonTypeFlags::Int64;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001529 break;
1530 case 'h':
Bob Wilsonda95f732011-11-08 01:16:11 +00001531 ET = NeonTypeFlags::Float16;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001532 break;
1533 case 'f':
Bob Wilsonda95f732011-11-08 01:16:11 +00001534 ET = NeonTypeFlags::Float32;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001535 break;
1536 default:
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +00001537 PrintFatalError("unhandled type!");
Peter Collingbourne51d77772011-10-06 13:03:08 +00001538 }
Bob Wilsonda95f732011-11-08 01:16:11 +00001539 NeonTypeFlags Flags(ET, usgn, quad && proto[1] != 'g');
1540 return Flags.getFlags();
Peter Collingbourne51d77772011-10-06 13:03:08 +00001541}
1542
1543// Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
1544static std::string GenBuiltin(const std::string &name, const std::string &proto,
1545 StringRef typestr, ClassKind ck) {
1546 std::string s;
1547
1548 // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
1549 // sret-like argument.
1550 bool sret = (proto[0] >= '2' && proto[0] <= '4');
1551
1552 bool define = UseMacro(proto);
1553
1554 // Check if the prototype has a scalar operand with the type of the vector
1555 // elements. If not, bitcasting the args will take care of arg checking.
1556 // The actual signedness etc. will be taken care of with special enums.
1557 if (proto.find('s') == std::string::npos)
1558 ck = ClassB;
1559
1560 if (proto[0] != 'v') {
1561 std::string ts = TypeString(proto[0], typestr);
1562
1563 if (define) {
1564 if (sret)
1565 s += ts + " r; ";
1566 else
1567 s += "(" + ts + ")";
1568 } else if (sret) {
1569 s += ts + " r; ";
1570 } else {
1571 s += "return (" + ts + ")";
1572 }
1573 }
1574
1575 bool splat = proto.find('a') != std::string::npos;
1576
1577 s += "__builtin_neon_";
1578 if (splat) {
1579 // Call the non-splat builtin: chop off the "_n" suffix from the name.
1580 std::string vname(name, 0, name.size()-2);
1581 s += MangleName(vname, typestr, ck);
1582 } else {
1583 s += MangleName(name, typestr, ck);
1584 }
1585 s += "(";
1586
1587 // Pass the address of the return variable as the first argument to sret-like
1588 // builtins.
1589 if (sret)
1590 s += "&r, ";
1591
1592 char arg = 'a';
1593 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
1594 std::string args = std::string(&arg, 1);
1595
1596 // Use the local temporaries instead of the macro arguments.
1597 args = "__" + args;
1598
1599 bool argQuad = false;
1600 bool argPoly = false;
1601 bool argUsgn = false;
1602 bool argScalar = false;
1603 bool dummy = false;
1604 char argType = ClassifyType(typestr, argQuad, argPoly, argUsgn);
1605 argType = ModType(proto[i], argType, argQuad, argPoly, argUsgn, argScalar,
1606 dummy, dummy);
1607
1608 // Handle multiple-vector values specially, emitting each subvector as an
1609 // argument to the __builtin.
1610 if (proto[i] >= '2' && proto[i] <= '4') {
1611 // Check if an explicit cast is needed.
1612 if (argType != 'c' || argPoly || argUsgn)
1613 args = (argQuad ? "(int8x16_t)" : "(int8x8_t)") + args;
1614
1615 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
1616 s += args + ".val[" + utostr(vi) + "]";
1617 if ((vi + 1) < ve)
1618 s += ", ";
1619 }
1620 if ((i + 1) < e)
1621 s += ", ";
1622
1623 continue;
1624 }
1625
1626 if (splat && (i + 1) == e)
1627 args = Duplicate(GetNumElements(typestr, argQuad), typestr, args);
1628
1629 // Check if an explicit cast is needed.
1630 if ((splat || !argScalar) &&
1631 ((ck == ClassB && argType != 'c') || argPoly || argUsgn)) {
1632 std::string argTypeStr = "c";
1633 if (ck != ClassB)
1634 argTypeStr = argType;
1635 if (argQuad)
1636 argTypeStr = "Q" + argTypeStr;
1637 args = "(" + TypeString('d', argTypeStr) + ")" + args;
1638 }
1639
1640 s += args;
1641 if ((i + 1) < e)
1642 s += ", ";
1643 }
1644
1645 // Extra constant integer to hold type class enum for this function, e.g. s8
1646 if (ck == ClassB)
1647 s += ", " + utostr(GetNeonEnum(proto, typestr));
1648
1649 s += ");";
1650
1651 if (proto[0] != 'v' && sret) {
1652 if (define)
1653 s += " r;";
1654 else
1655 s += " return r;";
1656 }
1657 return s;
1658}
1659
1660static std::string GenBuiltinDef(const std::string &name,
1661 const std::string &proto,
1662 StringRef typestr, ClassKind ck) {
1663 std::string s("BUILTIN(__builtin_neon_");
1664
1665 // If all types are the same size, bitcasting the args will take care
1666 // of arg checking. The actual signedness etc. will be taken care of with
1667 // special enums.
1668 if (proto.find('s') == std::string::npos)
1669 ck = ClassB;
1670
1671 s += MangleName(name, typestr, ck);
1672 s += ", \"";
1673
1674 for (unsigned i = 0, e = proto.size(); i != e; ++i)
1675 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
1676
1677 // Extra constant integer to hold type class enum for this function, e.g. s8
1678 if (ck == ClassB)
1679 s += "i";
1680
1681 s += "\", \"n\")";
1682 return s;
1683}
1684
1685static std::string GenIntrinsic(const std::string &name,
1686 const std::string &proto,
1687 StringRef outTypeStr, StringRef inTypeStr,
1688 OpKind kind, ClassKind classKind) {
1689 assert(!proto.empty() && "");
Jim Grosbach667381b2012-05-09 18:17:30 +00001690 bool define = UseMacro(proto) && kind != OpUnavailable;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001691 std::string s;
1692
1693 // static always inline + return type
1694 if (define)
1695 s += "#define ";
1696 else
1697 s += "__ai " + TypeString(proto[0], outTypeStr) + " ";
1698
1699 // Function name with type suffix
1700 std::string mangledName = MangleName(name, outTypeStr, ClassS);
1701 if (outTypeStr != inTypeStr) {
1702 // If the input type is different (e.g., for vreinterpret), append a suffix
1703 // for the input type. String off a "Q" (quad) prefix so that MangleName
1704 // does not insert another "q" in the name.
1705 unsigned typeStrOff = (inTypeStr[0] == 'Q' ? 1 : 0);
1706 StringRef inTypeNoQuad = inTypeStr.substr(typeStrOff);
1707 mangledName = MangleName(mangledName, inTypeNoQuad, ClassS);
1708 }
1709 s += mangledName;
1710
1711 // Function arguments
1712 s += GenArgs(proto, inTypeStr);
1713
1714 // Definition.
1715 if (define) {
1716 s += " __extension__ ({ \\\n ";
1717 s += GenMacroLocals(proto, inTypeStr);
Jim Grosbach667381b2012-05-09 18:17:30 +00001718 } else if (kind == OpUnavailable) {
1719 s += " __attribute__((unavailable));\n";
1720 return s;
1721 } else
Jim Grosbach66981c72012-08-03 17:30:46 +00001722 s += " {\n ";
Peter Collingbourne51d77772011-10-06 13:03:08 +00001723
1724 if (kind != OpNone)
1725 s += GenOpString(kind, proto, outTypeStr);
1726 else
1727 s += GenBuiltin(name, proto, outTypeStr, classKind);
1728 if (define)
1729 s += " })";
1730 else
1731 s += " }";
1732 s += "\n";
1733 return s;
1734}
1735
1736/// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h
1737/// is comprised of type definitions and function declarations.
1738void NeonEmitter::run(raw_ostream &OS) {
1739 OS <<
1740 "/*===---- arm_neon.h - ARM Neon intrinsics ------------------------------"
1741 "---===\n"
1742 " *\n"
1743 " * Permission is hereby granted, free of charge, to any person obtaining "
1744 "a copy\n"
1745 " * of this software and associated documentation files (the \"Software\"),"
1746 " to deal\n"
1747 " * in the Software without restriction, including without limitation the "
1748 "rights\n"
1749 " * to use, copy, modify, merge, publish, distribute, sublicense, "
1750 "and/or sell\n"
1751 " * copies of the Software, and to permit persons to whom the Software is\n"
1752 " * furnished to do so, subject to the following conditions:\n"
1753 " *\n"
1754 " * The above copyright notice and this permission notice shall be "
1755 "included in\n"
1756 " * all copies or substantial portions of the Software.\n"
1757 " *\n"
1758 " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, "
1759 "EXPRESS OR\n"
1760 " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
1761 "MERCHANTABILITY,\n"
1762 " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT "
1763 "SHALL THE\n"
1764 " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR "
1765 "OTHER\n"
1766 " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, "
1767 "ARISING FROM,\n"
1768 " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER "
1769 "DEALINGS IN\n"
1770 " * THE SOFTWARE.\n"
1771 " *\n"
1772 " *===--------------------------------------------------------------------"
1773 "---===\n"
1774 " */\n\n";
1775
1776 OS << "#ifndef __ARM_NEON_H\n";
1777 OS << "#define __ARM_NEON_H\n\n";
1778
1779 OS << "#ifndef __ARM_NEON__\n";
1780 OS << "#error \"NEON support not enabled\"\n";
1781 OS << "#endif\n\n";
1782
1783 OS << "#include <stdint.h>\n\n";
1784
1785 // Emit NEON-specific scalar typedefs.
1786 OS << "typedef float float32_t;\n";
1787 OS << "typedef int8_t poly8_t;\n";
1788 OS << "typedef int16_t poly16_t;\n";
1789 OS << "typedef uint16_t float16_t;\n";
1790
1791 // Emit Neon vector typedefs.
1792 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
1793 SmallVector<StringRef, 24> TDTypeVec;
1794 ParseTypes(0, TypedefTypes, TDTypeVec);
1795
1796 // Emit vector typedefs.
1797 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
1798 bool dummy, quad = false, poly = false;
1799 (void) ClassifyType(TDTypeVec[i], quad, poly, dummy);
1800 if (poly)
1801 OS << "typedef __attribute__((neon_polyvector_type(";
1802 else
1803 OS << "typedef __attribute__((neon_vector_type(";
1804
1805 unsigned nElts = GetNumElements(TDTypeVec[i], quad);
1806 OS << utostr(nElts) << "))) ";
1807 if (nElts < 10)
1808 OS << " ";
1809
1810 OS << TypeString('s', TDTypeVec[i]);
1811 OS << " " << TypeString('d', TDTypeVec[i]) << ";\n";
1812 }
1813 OS << "\n";
1814
1815 // Emit struct typedefs.
1816 for (unsigned vi = 2; vi != 5; ++vi) {
1817 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
1818 std::string ts = TypeString('d', TDTypeVec[i]);
1819 std::string vs = TypeString('0' + vi, TDTypeVec[i]);
1820 OS << "typedef struct " << vs << " {\n";
1821 OS << " " << ts << " val";
1822 OS << "[" << utostr(vi) << "]";
1823 OS << ";\n} ";
1824 OS << vs << ";\n\n";
1825 }
1826 }
1827
Bob Wilson1e8058f2013-04-12 20:17:20 +00001828 OS<<"#define __ai static inline __attribute__((__always_inline__, __nodebug__))\n\n";
Peter Collingbourne51d77772011-10-06 13:03:08 +00001829
1830 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1831
1832 // Emit vmovl, vmull and vabd intrinsics first so they can be used by other
1833 // intrinsics. (Some of the saturating multiply instructions are also
1834 // used to implement the corresponding "_lane" variants, but tablegen
1835 // sorts the records into alphabetical order so that the "_lane" variants
1836 // come after the intrinsics they use.)
1837 emitIntrinsic(OS, Records.getDef("VMOVL"));
1838 emitIntrinsic(OS, Records.getDef("VMULL"));
1839 emitIntrinsic(OS, Records.getDef("VABD"));
1840
1841 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1842 Record *R = RV[i];
1843 if (R->getName() != "VMOVL" &&
1844 R->getName() != "VMULL" &&
1845 R->getName() != "VABD")
1846 emitIntrinsic(OS, R);
1847 }
1848
1849 OS << "#undef __ai\n\n";
1850 OS << "#endif /* __ARM_NEON_H */\n";
1851}
1852
1853/// emitIntrinsic - Write out the arm_neon.h header file definitions for the
1854/// intrinsics specified by record R.
1855void NeonEmitter::emitIntrinsic(raw_ostream &OS, Record *R) {
1856 std::string name = R->getValueAsString("Name");
1857 std::string Proto = R->getValueAsString("Prototype");
1858 std::string Types = R->getValueAsString("Types");
1859
1860 SmallVector<StringRef, 16> TypeVec;
1861 ParseTypes(R, Types, TypeVec);
1862
1863 OpKind kind = OpMap[R->getValueAsDef("Operand")->getName()];
1864
1865 ClassKind classKind = ClassNone;
1866 if (R->getSuperClasses().size() >= 2)
1867 classKind = ClassMap[R->getSuperClasses()[1]];
1868 if (classKind == ClassNone && kind == OpNone)
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +00001869 PrintFatalError(R->getLoc(), "Builtin has no class kind");
Peter Collingbourne51d77772011-10-06 13:03:08 +00001870
1871 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1872 if (kind == OpReinterpret) {
1873 bool outQuad = false;
1874 bool dummy = false;
1875 (void)ClassifyType(TypeVec[ti], outQuad, dummy, dummy);
1876 for (unsigned srcti = 0, srcte = TypeVec.size();
1877 srcti != srcte; ++srcti) {
1878 bool inQuad = false;
1879 (void)ClassifyType(TypeVec[srcti], inQuad, dummy, dummy);
1880 if (srcti == ti || inQuad != outQuad)
1881 continue;
1882 OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[srcti],
1883 OpCast, ClassS);
1884 }
1885 } else {
1886 OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[ti],
1887 kind, classKind);
1888 }
1889 }
1890 OS << "\n";
1891}
1892
1893static unsigned RangeFromType(const char mod, StringRef typestr) {
1894 // base type to get the type string for.
1895 bool quad = false, dummy = false;
1896 char type = ClassifyType(typestr, quad, dummy, dummy);
1897 type = ModType(mod, type, quad, dummy, dummy, dummy, dummy, dummy);
1898
1899 switch (type) {
1900 case 'c':
1901 return (8 << (int)quad) - 1;
1902 case 'h':
1903 case 's':
1904 return (4 << (int)quad) - 1;
1905 case 'f':
1906 case 'i':
1907 return (2 << (int)quad) - 1;
1908 case 'l':
1909 return (1 << (int)quad) - 1;
1910 default:
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +00001911 PrintFatalError("unhandled type!");
Peter Collingbourne51d77772011-10-06 13:03:08 +00001912 }
Peter Collingbourne51d77772011-10-06 13:03:08 +00001913}
1914
1915/// runHeader - Emit a file with sections defining:
1916/// 1. the NEON section of BuiltinsARM.def.
1917/// 2. the SemaChecking code for the type overload checking.
Jim Grosbach667381b2012-05-09 18:17:30 +00001918/// 3. the SemaChecking code for validation of intrinsic immediate arguments.
Peter Collingbourne51d77772011-10-06 13:03:08 +00001919void NeonEmitter::runHeader(raw_ostream &OS) {
1920 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1921
1922 StringMap<OpKind> EmittedMap;
1923
1924 // Generate BuiltinsARM.def for NEON
1925 OS << "#ifdef GET_NEON_BUILTINS\n";
1926 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1927 Record *R = RV[i];
1928 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1929 if (k != OpNone)
1930 continue;
1931
1932 std::string Proto = R->getValueAsString("Prototype");
1933
1934 // Functions with 'a' (the splat code) in the type prototype should not get
1935 // their own builtin as they use the non-splat variant.
1936 if (Proto.find('a') != std::string::npos)
1937 continue;
1938
1939 std::string Types = R->getValueAsString("Types");
1940 SmallVector<StringRef, 16> TypeVec;
1941 ParseTypes(R, Types, TypeVec);
1942
1943 if (R->getSuperClasses().size() < 2)
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +00001944 PrintFatalError(R->getLoc(), "Builtin has no class kind");
Peter Collingbourne51d77772011-10-06 13:03:08 +00001945
1946 std::string name = R->getValueAsString("Name");
1947 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1948
1949 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1950 // Generate the BuiltinsARM.def declaration for this builtin, ensuring
1951 // that each unique BUILTIN() macro appears only once in the output
1952 // stream.
1953 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
1954 if (EmittedMap.count(bd))
1955 continue;
1956
1957 EmittedMap[bd] = OpNone;
1958 OS << bd << "\n";
1959 }
1960 }
1961 OS << "#endif\n\n";
1962
1963 // Generate the overloaded type checking code for SemaChecking.cpp
1964 OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
1965 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1966 Record *R = RV[i];
1967 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1968 if (k != OpNone)
1969 continue;
1970
1971 std::string Proto = R->getValueAsString("Prototype");
1972 std::string Types = R->getValueAsString("Types");
1973 std::string name = R->getValueAsString("Name");
1974
1975 // Functions with 'a' (the splat code) in the type prototype should not get
1976 // their own builtin as they use the non-splat variant.
1977 if (Proto.find('a') != std::string::npos)
1978 continue;
1979
1980 // Functions which have a scalar argument cannot be overloaded, no need to
1981 // check them if we are emitting the type checking code.
1982 if (Proto.find('s') != std::string::npos)
1983 continue;
1984
1985 SmallVector<StringRef, 16> TypeVec;
1986 ParseTypes(R, Types, TypeVec);
1987
1988 if (R->getSuperClasses().size() < 2)
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +00001989 PrintFatalError(R->getLoc(), "Builtin has no class kind");
Peter Collingbourne51d77772011-10-06 13:03:08 +00001990
1991 int si = -1, qi = -1;
Richard Smithf8ee6bc2012-08-14 01:28:02 +00001992 uint64_t mask = 0, qmask = 0;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001993 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1994 // Generate the switch case(s) for this builtin for the type validation.
1995 bool quad = false, poly = false, usgn = false;
1996 (void) ClassifyType(TypeVec[ti], quad, poly, usgn);
1997
1998 if (quad) {
1999 qi = ti;
Richard Smithf8ee6bc2012-08-14 01:28:02 +00002000 qmask |= 1ULL << GetNeonEnum(Proto, TypeVec[ti]);
Peter Collingbourne51d77772011-10-06 13:03:08 +00002001 } else {
2002 si = ti;
Richard Smithf8ee6bc2012-08-14 01:28:02 +00002003 mask |= 1ULL << GetNeonEnum(Proto, TypeVec[ti]);
Peter Collingbourne51d77772011-10-06 13:03:08 +00002004 }
2005 }
Bob Wilson46482552011-11-16 21:32:23 +00002006
2007 // Check if the builtin function has a pointer or const pointer argument.
2008 int PtrArgNum = -1;
2009 bool HasConstPtr = false;
2010 for (unsigned arg = 1, arge = Proto.size(); arg != arge; ++arg) {
2011 char ArgType = Proto[arg];
2012 if (ArgType == 'c') {
2013 HasConstPtr = true;
2014 PtrArgNum = arg - 1;
2015 break;
2016 }
2017 if (ArgType == 'p') {
2018 PtrArgNum = arg - 1;
2019 break;
2020 }
2021 }
2022 // For sret builtins, adjust the pointer argument index.
2023 if (PtrArgNum >= 0 && (Proto[0] >= '2' && Proto[0] <= '4'))
2024 PtrArgNum += 1;
2025
Bob Wilson9082cdd2011-12-20 06:16:48 +00002026 // Omit type checking for the pointer arguments of vld1_lane, vld1_dup,
2027 // and vst1_lane intrinsics. Using a pointer to the vector element
2028 // type with one of those operations causes codegen to select an aligned
2029 // load/store instruction. If you want an unaligned operation,
2030 // the pointer argument needs to have less alignment than element type,
2031 // so just accept any pointer type.
2032 if (name == "vld1_lane" || name == "vld1_dup" || name == "vst1_lane") {
2033 PtrArgNum = -1;
2034 HasConstPtr = false;
2035 }
2036
Bob Wilson6f9f03e2011-11-08 05:04:11 +00002037 if (mask) {
Peter Collingbourne51d77772011-10-06 13:03:08 +00002038 OS << "case ARM::BI__builtin_neon_"
2039 << MangleName(name, TypeVec[si], ClassB)
Richard Smithb27660a2012-08-14 03:55:16 +00002040 << ": mask = " << "0x" << utohexstr(mask) << "ULL";
Bob Wilson46482552011-11-16 21:32:23 +00002041 if (PtrArgNum >= 0)
2042 OS << "; PtrArgNum = " << PtrArgNum;
Bob Wilson6f9f03e2011-11-08 05:04:11 +00002043 if (HasConstPtr)
2044 OS << "; HasConstPtr = true";
2045 OS << "; break;\n";
2046 }
2047 if (qmask) {
Peter Collingbourne51d77772011-10-06 13:03:08 +00002048 OS << "case ARM::BI__builtin_neon_"
2049 << MangleName(name, TypeVec[qi], ClassB)
Richard Smithb27660a2012-08-14 03:55:16 +00002050 << ": mask = " << "0x" << utohexstr(qmask) << "ULL";
Bob Wilson46482552011-11-16 21:32:23 +00002051 if (PtrArgNum >= 0)
2052 OS << "; PtrArgNum = " << PtrArgNum;
Bob Wilson6f9f03e2011-11-08 05:04:11 +00002053 if (HasConstPtr)
2054 OS << "; HasConstPtr = true";
2055 OS << "; break;\n";
2056 }
Peter Collingbourne51d77772011-10-06 13:03:08 +00002057 }
2058 OS << "#endif\n\n";
2059
2060 // Generate the intrinsic range checking code for shift/lane immediates.
2061 OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
2062 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
2063 Record *R = RV[i];
2064
2065 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
2066 if (k != OpNone)
2067 continue;
2068
2069 std::string name = R->getValueAsString("Name");
2070 std::string Proto = R->getValueAsString("Prototype");
2071 std::string Types = R->getValueAsString("Types");
2072
2073 // Functions with 'a' (the splat code) in the type prototype should not get
2074 // their own builtin as they use the non-splat variant.
2075 if (Proto.find('a') != std::string::npos)
2076 continue;
2077
2078 // Functions which do not have an immediate do not need to have range
2079 // checking code emitted.
2080 size_t immPos = Proto.find('i');
2081 if (immPos == std::string::npos)
2082 continue;
2083
2084 SmallVector<StringRef, 16> TypeVec;
2085 ParseTypes(R, Types, TypeVec);
2086
2087 if (R->getSuperClasses().size() < 2)
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +00002088 PrintFatalError(R->getLoc(), "Builtin has no class kind");
Peter Collingbourne51d77772011-10-06 13:03:08 +00002089
2090 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
2091
2092 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
2093 std::string namestr, shiftstr, rangestr;
2094
2095 if (R->getValueAsBit("isVCVT_N")) {
2096 // VCVT between floating- and fixed-point values takes an immediate
2097 // in the range 1 to 32.
2098 ck = ClassB;
2099 rangestr = "l = 1; u = 31"; // upper bound = l + u
2100 } else if (Proto.find('s') == std::string::npos) {
2101 // Builtins which are overloaded by type will need to have their upper
2102 // bound computed at Sema time based on the type constant.
2103 ck = ClassB;
2104 if (R->getValueAsBit("isShift")) {
2105 shiftstr = ", true";
2106
2107 // Right shifts have an 'r' in the name, left shifts do not.
2108 if (name.find('r') != std::string::npos)
2109 rangestr = "l = 1; ";
2110 }
2111 rangestr += "u = RFT(TV" + shiftstr + ")";
2112 } else {
2113 // The immediate generally refers to a lane in the preceding argument.
2114 assert(immPos > 0 && "unexpected immediate operand");
2115 rangestr = "u = " + utostr(RangeFromType(Proto[immPos-1], TypeVec[ti]));
2116 }
2117 // Make sure cases appear only once by uniquing them in a string map.
2118 namestr = MangleName(name, TypeVec[ti], ck);
2119 if (EmittedMap.count(namestr))
2120 continue;
2121 EmittedMap[namestr] = OpNone;
2122
2123 // Calculate the index of the immediate that should be range checked.
2124 unsigned immidx = 0;
2125
2126 // Builtins that return a struct of multiple vectors have an extra
2127 // leading arg for the struct return.
2128 if (Proto[0] >= '2' && Proto[0] <= '4')
2129 ++immidx;
2130
2131 // Add one to the index for each argument until we reach the immediate
2132 // to be checked. Structs of vectors are passed as multiple arguments.
2133 for (unsigned ii = 1, ie = Proto.size(); ii != ie; ++ii) {
2134 switch (Proto[ii]) {
2135 default: immidx += 1; break;
2136 case '2': immidx += 2; break;
2137 case '3': immidx += 3; break;
2138 case '4': immidx += 4; break;
2139 case 'i': ie = ii + 1; break;
2140 }
2141 }
2142 OS << "case ARM::BI__builtin_neon_" << MangleName(name, TypeVec[ti], ck)
2143 << ": i = " << immidx << "; " << rangestr << "; break;\n";
2144 }
2145 }
2146 OS << "#endif\n\n";
2147}
2148
2149/// GenTest - Write out a test for the intrinsic specified by the name and
2150/// type strings, including the embedded patterns for FileCheck to match.
2151static std::string GenTest(const std::string &name,
2152 const std::string &proto,
2153 StringRef outTypeStr, StringRef inTypeStr,
Michael Gottesman7200bd62013-04-16 22:48:52 +00002154 bool isShift, bool isHiddenLOp,
2155 ClassKind ck, const std::string &InstName) {
Peter Collingbourne51d77772011-10-06 13:03:08 +00002156 assert(!proto.empty() && "");
2157 std::string s;
2158
2159 // Function name with type suffix
2160 std::string mangledName = MangleName(name, outTypeStr, ClassS);
2161 if (outTypeStr != inTypeStr) {
2162 // If the input type is different (e.g., for vreinterpret), append a suffix
2163 // for the input type. String off a "Q" (quad) prefix so that MangleName
2164 // does not insert another "q" in the name.
2165 unsigned typeStrOff = (inTypeStr[0] == 'Q' ? 1 : 0);
2166 StringRef inTypeNoQuad = inTypeStr.substr(typeStrOff);
2167 mangledName = MangleName(mangledName, inTypeNoQuad, ClassS);
2168 }
2169
Michael Gottesmanc327f872013-04-16 23:00:26 +00002170 std::vector<std::string> FileCheckPatterns;
2171 GenerateChecksForIntrinsic(name, proto, outTypeStr, inTypeStr, ck, InstName,
2172 isHiddenLOp, FileCheckPatterns);
2173
Peter Collingbourne51d77772011-10-06 13:03:08 +00002174 // Emit the FileCheck patterns.
2175 s += "// CHECK: test_" + mangledName + "\n";
Michael Gottesmanc327f872013-04-16 23:00:26 +00002176 // If for any reason we do not want to emit a check, mangledInst
2177 // will be the empty string.
2178 if (FileCheckPatterns.size()) {
2179 for (std::vector<std::string>::const_iterator i = FileCheckPatterns.begin(),
2180 e = FileCheckPatterns.end();
2181 i != e;
2182 ++i) {
2183 s += "// CHECK: " + *i + "\n";
2184 }
2185 }
Peter Collingbourne51d77772011-10-06 13:03:08 +00002186
2187 // Emit the start of the test function.
2188 s += TypeString(proto[0], outTypeStr) + " test_" + mangledName + "(";
2189 char arg = 'a';
2190 std::string comma;
2191 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
2192 // Do not create arguments for values that must be immediate constants.
2193 if (proto[i] == 'i')
2194 continue;
2195 s += comma + TypeString(proto[i], inTypeStr) + " ";
2196 s.push_back(arg);
2197 comma = ", ";
2198 }
Jim Grosbachb4a54252012-05-30 18:18:29 +00002199 s += ") {\n ";
Peter Collingbourne51d77772011-10-06 13:03:08 +00002200
2201 if (proto[0] != 'v')
2202 s += "return ";
2203 s += mangledName + "(";
2204 arg = 'a';
2205 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
2206 if (proto[i] == 'i') {
2207 // For immediate operands, test the maximum value.
2208 if (isShift)
2209 s += "1"; // FIXME
2210 else
2211 // The immediate generally refers to a lane in the preceding argument.
2212 s += utostr(RangeFromType(proto[i-1], inTypeStr));
2213 } else {
2214 s.push_back(arg);
2215 }
2216 if ((i + 1) < e)
2217 s += ", ";
2218 }
2219 s += ");\n}\n\n";
2220 return s;
2221}
2222
2223/// runTests - Write out a complete set of tests for all of the Neon
2224/// intrinsics.
2225void NeonEmitter::runTests(raw_ostream &OS) {
2226 OS <<
Michael Gottesmanc873b512013-04-25 00:10:14 +00002227 "// RUN: %clang_cc1 -triple thumbv7s-apple-darwin -target-abi apcs-gnu\\\n"
Michael Gottesmanfb9929e2013-04-16 22:55:01 +00002228 "// RUN: -target-cpu swift -ffreestanding -Os -S -o - %s\\\n"
2229 "// RUN: | FileCheck %s\n"
Peter Collingbourne51d77772011-10-06 13:03:08 +00002230 "\n"
Michael Gottesmanbb5595d2013-06-24 21:25:34 +00002231 "// REQUIRES: long_tests\n"
2232 "\n"
Peter Collingbourne51d77772011-10-06 13:03:08 +00002233 "#include <arm_neon.h>\n"
2234 "\n";
2235
2236 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
2237 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
2238 Record *R = RV[i];
2239 std::string name = R->getValueAsString("Name");
2240 std::string Proto = R->getValueAsString("Prototype");
2241 std::string Types = R->getValueAsString("Types");
2242 bool isShift = R->getValueAsBit("isShift");
Michael Gottesman7200bd62013-04-16 22:48:52 +00002243 std::string InstName = R->getValueAsString("InstName");
2244 bool isHiddenLOp = R->getValueAsBit("isHiddenLInst");
Peter Collingbourne51d77772011-10-06 13:03:08 +00002245
2246 SmallVector<StringRef, 16> TypeVec;
2247 ParseTypes(R, Types, TypeVec);
2248
Michael Gottesman7200bd62013-04-16 22:48:52 +00002249 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
Peter Collingbourne51d77772011-10-06 13:03:08 +00002250 OpKind kind = OpMap[R->getValueAsDef("Operand")->getName()];
Jim Grosbach667381b2012-05-09 18:17:30 +00002251 if (kind == OpUnavailable)
2252 continue;
Peter Collingbourne51d77772011-10-06 13:03:08 +00002253 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
2254 if (kind == OpReinterpret) {
2255 bool outQuad = false;
2256 bool dummy = false;
2257 (void)ClassifyType(TypeVec[ti], outQuad, dummy, dummy);
2258 for (unsigned srcti = 0, srcte = TypeVec.size();
2259 srcti != srcte; ++srcti) {
2260 bool inQuad = false;
2261 (void)ClassifyType(TypeVec[srcti], inQuad, dummy, dummy);
2262 if (srcti == ti || inQuad != outQuad)
2263 continue;
Michael Gottesman7200bd62013-04-16 22:48:52 +00002264 OS << GenTest(name, Proto, TypeVec[ti], TypeVec[srcti],
2265 isShift, isHiddenLOp, ck, InstName);
Peter Collingbourne51d77772011-10-06 13:03:08 +00002266 }
2267 } else {
Michael Gottesman7200bd62013-04-16 22:48:52 +00002268 OS << GenTest(name, Proto, TypeVec[ti], TypeVec[ti],
2269 isShift, isHiddenLOp, ck, InstName);
Peter Collingbourne51d77772011-10-06 13:03:08 +00002270 }
2271 }
2272 OS << "\n";
2273 }
2274}
2275
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +00002276namespace clang {
2277void EmitNeon(RecordKeeper &Records, raw_ostream &OS) {
2278 NeonEmitter(Records).run(OS);
2279}
2280void EmitNeonSema(RecordKeeper &Records, raw_ostream &OS) {
2281 NeonEmitter(Records).runHeader(OS);
2282}
2283void EmitNeonTest(RecordKeeper &Records, raw_ostream &OS) {
2284 NeonEmitter(Records).runTests(OS);
2285}
2286} // End namespace clang