blob: f3707f2b229035867d5082bde7f43bd76119ff88 [file] [log] [blame]
Nate Begeman5ddb0872010-05-28 01:08:32 +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//
Nate Begemand72c9002010-06-13 04:47:03 +000014// 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.
22//
Nate Begeman5ddb0872010-05-28 01:08:32 +000023//===----------------------------------------------------------------------===//
24
25#include "NeonEmitter.h"
Nate Begeman22237772010-06-02 00:34:55 +000026#include "llvm/ADT/SmallString.h"
27#include "llvm/ADT/SmallVector.h"
Nate Begeman5ddb0872010-05-28 01:08:32 +000028#include "llvm/ADT/StringExtras.h"
Nate Begeman5ddb0872010-05-28 01:08:32 +000029#include <string>
30
31using namespace llvm;
32
Nate Begemand72c9002010-06-13 04:47:03 +000033/// ParseTypes - break down a string such as "fQf" into a vector of StringRefs,
34/// which each StringRef representing a single type declared in the string.
35/// for "fQf" we would end up with 2 StringRefs, "f", and "Qf", representing
36/// 2xfloat and 4xfloat respectively.
Nate Begeman22237772010-06-02 00:34:55 +000037static void ParseTypes(Record *r, std::string &s,
38 SmallVectorImpl<StringRef> &TV) {
39 const char *data = s.data();
40 int len = 0;
41
42 for (unsigned i = 0, e = s.size(); i != e; ++i, ++len) {
43 if (data[len] == 'P' || data[len] == 'Q' || data[len] == 'U')
44 continue;
45
46 switch (data[len]) {
47 case 'c':
48 case 's':
49 case 'i':
50 case 'l':
51 case 'h':
52 case 'f':
53 break;
54 default:
55 throw TGError(r->getLoc(),
56 "Unexpected letter: " + std::string(data + len, 1));
57 break;
58 }
59 TV.push_back(StringRef(data, len + 1));
60 data += len + 1;
61 len = -1;
62 }
63}
64
Nate Begemand72c9002010-06-13 04:47:03 +000065/// Widen - Convert a type code into the next wider type. char -> short,
66/// short -> int, etc.
Duncan Sands8dbbace2010-06-02 08:37:30 +000067static char Widen(const char t) {
Nate Begeman22237772010-06-02 00:34:55 +000068 switch (t) {
69 case 'c':
70 return 's';
71 case 's':
72 return 'i';
73 case 'i':
74 return 'l';
75 default: throw "unhandled type in widen!";
76 }
77 return '\0';
78}
79
Nate Begemand72c9002010-06-13 04:47:03 +000080/// Narrow - Convert a type code into the next smaller type. short -> char,
81/// float -> half float, etc.
Nate Begeman3861e742010-06-03 21:35:22 +000082static char Narrow(const char t) {
83 switch (t) {
84 case 's':
85 return 'c';
86 case 'i':
87 return 's';
88 case 'l':
89 return 'i';
Nate Begeman900f4672010-06-08 00:14:42 +000090 case 'f':
91 return 'h';
Nate Begeman8a6e7e12010-09-23 16:49:17 +000092 default: throw "unhandled type in widen!";
Nate Begeman3861e742010-06-03 21:35:22 +000093 }
94 return '\0';
95}
96
Nate Begemand72c9002010-06-13 04:47:03 +000097/// For a particular StringRef, return the base type code, and whether it has
98/// the quad-vector, polynomial, or unsigned modifiers set.
Nate Begemanaf905ef2010-06-02 06:17:19 +000099static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) {
Nate Begeman22237772010-06-02 00:34:55 +0000100 unsigned off = 0;
101
Nate Begemanaf905ef2010-06-02 06:17:19 +0000102 // remember quad.
103 if (ty[off] == 'Q') {
104 quad = true;
105 ++off;
106 }
107
108 // remember poly.
109 if (ty[off] == 'P') {
110 poly = true;
111 ++off;
112 }
113
114 // remember unsigned.
115 if (ty[off] == 'U') {
116 usgn = true;
117 ++off;
118 }
119
120 // base type to get the type string for.
121 return ty[off];
122}
123
Nate Begemand72c9002010-06-13 04:47:03 +0000124/// ModType - Transform a type code and its modifiers based on a mod code. The
125/// mod code definitions may be found at the top of arm_neon.td.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000126static char ModType(const char mod, char type, bool &quad, bool &poly,
127 bool &usgn, bool &scal, bool &cnst, bool &pntr) {
Nate Begeman22237772010-06-02 00:34:55 +0000128 switch (mod) {
Nate Begeman22237772010-06-02 00:34:55 +0000129 case 't':
130 if (poly) {
131 poly = false;
132 usgn = true;
133 }
134 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000135 case 'u':
Nate Begeman22237772010-06-02 00:34:55 +0000136 usgn = true;
Nate Begeman900f4672010-06-08 00:14:42 +0000137 case 'x':
Nate Begeman162d3ba2010-06-03 04:04:09 +0000138 poly = false;
Nate Begeman22237772010-06-02 00:34:55 +0000139 if (type == 'f')
140 type = 'i';
141 break;
142 case 'f':
Nate Begeman900f4672010-06-08 00:14:42 +0000143 if (type == 'h')
144 quad = true;
Nate Begeman22237772010-06-02 00:34:55 +0000145 type = 'f';
Nate Begeman162d3ba2010-06-03 04:04:09 +0000146 usgn = false;
Nate Begeman22237772010-06-02 00:34:55 +0000147 break;
Nate Begeman59d70cb2010-08-06 01:24:11 +0000148 case 'g':
149 quad = false;
150 break;
Nate Begeman22237772010-06-02 00:34:55 +0000151 case 'w':
152 type = Widen(type);
153 quad = true;
154 break;
155 case 'n':
156 type = Widen(type);
157 break;
Nate Begeman22237772010-06-02 00:34:55 +0000158 case 'l':
159 type = 'l';
160 scal = true;
161 usgn = true;
162 break;
163 case 's':
Nate Begeman4b425a82010-06-10 00:16:56 +0000164 case 'a':
Nate Begeman22237772010-06-02 00:34:55 +0000165 scal = true;
166 break;
167 case 'k':
168 quad = true;
169 break;
170 case 'c':
171 cnst = true;
172 case 'p':
173 pntr = true;
174 scal = true;
175 break;
Nate Begeman3861e742010-06-03 21:35:22 +0000176 case 'h':
177 type = Narrow(type);
Nate Begeman900f4672010-06-08 00:14:42 +0000178 if (type == 'h')
179 quad = false;
Nate Begeman3861e742010-06-03 21:35:22 +0000180 break;
181 case 'e':
182 type = Narrow(type);
183 usgn = true;
184 break;
Nate Begeman22237772010-06-02 00:34:55 +0000185 default:
186 break;
187 }
Nate Begemanb0a4e452010-06-07 16:00:37 +0000188 return type;
189}
190
Nate Begemand72c9002010-06-13 04:47:03 +0000191/// TypeString - for a modifier and type, generate the name of the typedef for
192/// that type. If generic is true, emit the generic vector type rather than
Bob Wilson1ac27cf2010-06-24 22:04:30 +0000193/// the public NEON type. QUc -> uint8x8_t / __neon_uint8x8_t.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000194static std::string TypeString(const char mod, StringRef typestr,
Nate Begemand72c9002010-06-13 04:47:03 +0000195 bool generic = false) {
Nate Begemanb0a4e452010-06-07 16:00:37 +0000196 bool quad = false;
197 bool poly = false;
198 bool usgn = false;
199 bool scal = false;
200 bool cnst = false;
201 bool pntr = false;
202
203 if (mod == 'v')
204 return "void";
205 if (mod == 'i')
206 return "int";
207
208 // base type to get the type string for.
209 char type = ClassifyType(typestr, quad, poly, usgn);
210
211 // Based on the modifying character, change the type and width if necessary.
212 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
Nate Begeman22237772010-06-02 00:34:55 +0000213
214 SmallString<128> s;
215
Nate Begemand72c9002010-06-13 04:47:03 +0000216 if (generic)
Nate Begeman9e584b32010-06-04 22:53:30 +0000217 s += "__neon_";
218
Nate Begeman22237772010-06-02 00:34:55 +0000219 if (usgn)
220 s.push_back('u');
221
222 switch (type) {
223 case 'c':
224 s += poly ? "poly8" : "int8";
225 if (scal)
226 break;
227 s += quad ? "x16" : "x8";
228 break;
229 case 's':
230 s += poly ? "poly16" : "int16";
231 if (scal)
232 break;
233 s += quad ? "x8" : "x4";
234 break;
235 case 'i':
236 s += "int32";
237 if (scal)
238 break;
239 s += quad ? "x4" : "x2";
240 break;
241 case 'l':
242 s += "int64";
243 if (scal)
244 break;
245 s += quad ? "x2" : "x1";
246 break;
247 case 'h':
248 s += "float16";
249 if (scal)
250 break;
251 s += quad ? "x8" : "x4";
252 break;
253 case 'f':
254 s += "float32";
255 if (scal)
256 break;
257 s += quad ? "x4" : "x2";
258 break;
Nate Begeman22237772010-06-02 00:34:55 +0000259 default:
260 throw "unhandled type!";
261 break;
262 }
263
264 if (mod == '2')
265 s += "x2";
266 if (mod == '3')
267 s += "x3";
268 if (mod == '4')
269 s += "x4";
270
271 // Append _t, finishing the type string typedef type.
272 s += "_t";
273
274 if (cnst)
275 s += " const";
276
277 if (pntr)
278 s += " *";
279
280 return s.str();
281}
282
Bob Wilson1ac27cf2010-06-24 22:04:30 +0000283/// BuiltinTypeString - for a modifier and type, generate the clang
284/// BuiltinsARM.def prototype code for the function. See the top of clang's
285/// Builtins.def for a description of the type strings.
Nate Begeman7c21f742010-06-04 21:36:00 +0000286static std::string BuiltinTypeString(const char mod, StringRef typestr,
287 ClassKind ck, bool ret) {
Nate Begeman92f98af2010-06-04 07:11:25 +0000288 bool quad = false;
289 bool poly = false;
290 bool usgn = false;
291 bool scal = false;
292 bool cnst = false;
293 bool pntr = false;
294
295 if (mod == 'v')
296 return "v";
297 if (mod == 'i')
298 return "i";
299
300 // base type to get the type string for.
301 char type = ClassifyType(typestr, quad, poly, usgn);
302
303 // Based on the modifying character, change the type and width if necessary.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000304 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
305
Nate Begemanc4a1b652010-06-20 21:09:52 +0000306 if (pntr) {
307 usgn = false;
308 poly = false;
Nate Begemanb0a4e452010-06-07 16:00:37 +0000309 type = 'v';
Nate Begemanc4a1b652010-06-20 21:09:52 +0000310 }
Nate Begeman92f98af2010-06-04 07:11:25 +0000311 if (type == 'h') {
312 type = 's';
313 usgn = true;
314 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000315 usgn = usgn | poly | ((ck == ClassI || ck == ClassW) && scal && type != 'f');
Nate Begeman92f98af2010-06-04 07:11:25 +0000316
317 if (scal) {
318 SmallString<128> s;
319
320 if (usgn)
321 s.push_back('U');
Nate Begeman7c21f742010-06-04 21:36:00 +0000322
323 if (type == 'l')
324 s += "LLi";
325 else
326 s.push_back(type);
327
Nate Begeman92f98af2010-06-04 07:11:25 +0000328 if (cnst)
329 s.push_back('C');
330 if (pntr)
331 s.push_back('*');
332 return s.str();
333 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000334
335 // Since the return value must be one type, return a vector type of the
Nate Begemanc4a1b652010-06-20 21:09:52 +0000336 // appropriate width which we will bitcast. An exception is made for
337 // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
338 // fashion, storing them to a pointer arg.
Nate Begeman7c21f742010-06-04 21:36:00 +0000339 if (ret) {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000340 if (mod == '2' || mod == '3' || mod == '4')
341 return "vv*";
Nate Begemanf50551e2010-06-09 18:02:26 +0000342 if (mod == 'f' || (ck != ClassB && type == 'f'))
Nate Begeman56387832010-06-08 06:01:16 +0000343 return quad ? "V4f" : "V2f";
Nate Begemanf50551e2010-06-09 18:02:26 +0000344 if (ck != ClassB && type == 's')
Nate Begeman007afe42010-06-09 05:11:55 +0000345 return quad ? "V8s" : "V4s";
Nate Begemanf50551e2010-06-09 18:02:26 +0000346 if (ck != ClassB && type == 'i')
Nate Begeman56387832010-06-08 06:01:16 +0000347 return quad ? "V4i" : "V2i";
Nate Begemanf50551e2010-06-09 18:02:26 +0000348 if (ck != ClassB && type == 'l')
Nate Begeman007afe42010-06-09 05:11:55 +0000349 return quad ? "V2LLi" : "V1LLi";
Nate Begeman900f4672010-06-08 00:14:42 +0000350
Nate Begeman7c21f742010-06-04 21:36:00 +0000351 return quad ? "V16c" : "V8c";
352 }
353
354 // Non-return array types are passed as individual vectors.
Nate Begeman92f98af2010-06-04 07:11:25 +0000355 if (mod == '2')
356 return quad ? "V16cV16c" : "V8cV8c";
357 if (mod == '3')
358 return quad ? "V16cV16cV16c" : "V8cV8cV8c";
359 if (mod == '4')
360 return quad ? "V16cV16cV16cV16c" : "V8cV8cV8cV8c";
361
Nate Begemanf50551e2010-06-09 18:02:26 +0000362 if (mod == 'f' || (ck != ClassB && type == 'f'))
Nate Begeman007afe42010-06-09 05:11:55 +0000363 return quad ? "V4f" : "V2f";
Nate Begemanf50551e2010-06-09 18:02:26 +0000364 if (ck != ClassB && type == 's')
Nate Begeman007afe42010-06-09 05:11:55 +0000365 return quad ? "V8s" : "V4s";
Nate Begemanf50551e2010-06-09 18:02:26 +0000366 if (ck != ClassB && type == 'i')
Nate Begeman007afe42010-06-09 05:11:55 +0000367 return quad ? "V4i" : "V2i";
Nate Begemanf50551e2010-06-09 18:02:26 +0000368 if (ck != ClassB && type == 'l')
Nate Begeman007afe42010-06-09 05:11:55 +0000369 return quad ? "V2LLi" : "V1LLi";
370
Nate Begeman92f98af2010-06-04 07:11:25 +0000371 return quad ? "V16c" : "V8c";
372}
373
Bob Wilson9969bc32010-06-24 22:03:41 +0000374/// StructTag - generate the name of the struct tag for a type.
375/// These names are mandated by ARM's ABI.
376static std::string StructTag(StringRef typestr) {
377 bool quad = false;
378 bool poly = false;
379 bool usgn = false;
380
381 // base type to get the type string for.
382 char type = ClassifyType(typestr, quad, poly, usgn);
383
384 SmallString<128> s;
385 s += "__simd";
386 s += quad ? "128_" : "64_";
387 if (usgn)
388 s.push_back('u');
389
390 switch (type) {
391 case 'c':
392 s += poly ? "poly8" : "int8";
393 break;
394 case 's':
395 s += poly ? "poly16" : "int16";
396 break;
397 case 'i':
398 s += "int32";
399 break;
400 case 'l':
401 s += "int64";
402 break;
403 case 'h':
404 s += "float16";
405 break;
406 case 'f':
407 s += "float32";
408 break;
409 default:
410 throw "unhandled type!";
411 break;
412 }
413
414 // Append _t, finishing the struct tag name.
415 s += "_t";
416
417 return s.str();
418}
419
Nate Begemand72c9002010-06-13 04:47:03 +0000420/// MangleName - Append a type or width suffix to a base neon function name,
421/// and insert a 'q' in the appropriate location if the operation works on
422/// 128b rather than 64b. E.g. turn "vst2_lane" into "vst2q_lane_f32", etc.
Nate Begemana8979a02010-06-04 00:21:41 +0000423static std::string MangleName(const std::string &name, StringRef typestr,
424 ClassKind ck) {
Nate Begeman900f4672010-06-08 00:14:42 +0000425 if (name == "vcvt_f32_f16")
426 return name;
427
Nate Begemanaf905ef2010-06-02 06:17:19 +0000428 bool quad = false;
429 bool poly = false;
430 bool usgn = false;
431 char type = ClassifyType(typestr, quad, poly, usgn);
432
433 std::string s = name;
434
435 switch (type) {
Nate Begemana8979a02010-06-04 00:21:41 +0000436 case 'c':
437 switch (ck) {
438 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
439 case ClassI: s += "_i8"; break;
440 case ClassW: s += "_8"; break;
441 default: break;
442 }
443 break;
444 case 's':
445 switch (ck) {
446 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
447 case ClassI: s += "_i16"; break;
448 case ClassW: s += "_16"; break;
449 default: break;
450 }
451 break;
452 case 'i':
453 switch (ck) {
454 case ClassS: s += usgn ? "_u32" : "_s32"; break;
455 case ClassI: s += "_i32"; break;
456 case ClassW: s += "_32"; break;
457 default: break;
458 }
459 break;
460 case 'l':
461 switch (ck) {
462 case ClassS: s += usgn ? "_u64" : "_s64"; break;
463 case ClassI: s += "_i64"; break;
464 case ClassW: s += "_64"; break;
465 default: break;
466 }
467 break;
468 case 'h':
469 switch (ck) {
470 case ClassS:
471 case ClassI: s += "_f16"; break;
472 case ClassW: s += "_16"; break;
473 default: break;
474 }
475 break;
476 case 'f':
477 switch (ck) {
478 case ClassS:
479 case ClassI: s += "_f32"; break;
480 case ClassW: s += "_32"; break;
481 default: break;
482 }
483 break;
484 default:
485 throw "unhandled type!";
486 break;
Nate Begemanaf905ef2010-06-02 06:17:19 +0000487 }
Nate Begemana8979a02010-06-04 00:21:41 +0000488 if (ck == ClassB)
Nate Begeman92f98af2010-06-04 07:11:25 +0000489 s += "_v";
Nate Begemana8979a02010-06-04 00:21:41 +0000490
Nate Begemanaf905ef2010-06-02 06:17:19 +0000491 // Insert a 'q' before the first '_' character so that it ends up before
492 // _lane or _n on vector-scalar operations.
493 if (quad) {
494 size_t pos = s.find('_');
495 s = s.insert(pos, "q");
496 }
497 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000498}
499
Nate Begemanaf905ef2010-06-02 06:17:19 +0000500// Generate the string "(argtype a, argtype b, ...)"
Nate Begeman22237772010-06-02 00:34:55 +0000501static std::string GenArgs(const std::string &proto, StringRef typestr) {
Nate Begeman6c060db2010-06-09 01:09:00 +0000502 bool define = proto.find('i') != std::string::npos;
Nate Begemanaf905ef2010-06-02 06:17:19 +0000503 char arg = 'a';
504
505 std::string s;
506 s += "(";
507
508 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
Nate Begeman6c060db2010-06-09 01:09:00 +0000509 if (!define) {
510 s += TypeString(proto[i], typestr);
511 s.push_back(' ');
512 }
Nate Begemanaf905ef2010-06-02 06:17:19 +0000513 s.push_back(arg);
514 if ((i + 1) < e)
515 s += ", ";
516 }
517
518 s += ")";
519 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000520}
521
Nate Begemancc3c41a2010-06-12 03:09:49 +0000522static std::string Duplicate(unsigned nElts, StringRef typestr,
523 const std::string &a) {
Nate Begeman4b425a82010-06-10 00:16:56 +0000524 std::string s;
525
526 s = "(__neon_" + TypeString('d', typestr) + "){ ";
527 for (unsigned i = 0; i != nElts; ++i) {
528 s += a;
529 if ((i + 1) < nElts)
530 s += ", ";
531 }
532 s += " }";
533
534 return s;
535}
536
537// Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
Bob Wilson1ce588c2010-11-16 18:43:07 +0000538// If structTypes is true, the NEON types are structs of vector types rather
539// than vector types, and the call becomes "a.val + b.val"
Nate Begeman4b425a82010-06-10 00:16:56 +0000540static std::string GenOpString(OpKind op, const std::string &proto,
Bob Wilson1ce588c2010-11-16 18:43:07 +0000541 StringRef typestr, bool structTypes = true) {
Nate Begemancc3c41a2010-06-12 03:09:49 +0000542 bool dummy, quad = false;
543 char type = ClassifyType(typestr, quad, dummy, dummy);
544 unsigned nElts = 0;
545 switch (type) {
546 case 'c': nElts = 8; break;
547 case 's': nElts = 4; break;
548 case 'i': nElts = 2; break;
549 case 'l': nElts = 1; break;
550 case 'h': nElts = 4; break;
551 case 'f': nElts = 2; break;
552 }
553
Nate Begeman4b425a82010-06-10 00:16:56 +0000554 std::string ts = TypeString(proto[0], typestr);
Bob Wilsonee9ca072010-09-15 01:52:33 +0000555 std::string s;
556 if (op == OpHi || op == OpLo) {
557 s = "union { " + ts + " r; double d; } u; u.d";
558 } else {
559 s = ts + " r; r";
Bob Wilson1ce588c2010-11-16 18:43:07 +0000560 if (structTypes)
561 s += ".val";
Bob Wilsonee9ca072010-09-15 01:52:33 +0000562 }
Nate Begeman900f4672010-06-08 00:14:42 +0000563
564 s += " = ";
565
Bob Wilson1ce588c2010-11-16 18:43:07 +0000566 std::string a, b, c;
567 if (proto.size() > 1)
568 a = (structTypes && proto[1] != 'l' && proto[1] != 's') ? "a.val" : "a";
569 b = structTypes ? "b.val" : "b";
570 c = structTypes ? "c.val" : "c";
571
Nate Begeman162d3ba2010-06-03 04:04:09 +0000572 switch(op) {
573 case OpAdd:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000574 s += a + " + " + b;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000575 break;
576 case OpSub:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000577 s += a + " - " + b;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000578 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000579 case OpMulN:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000580 b = Duplicate(nElts << (int)quad, typestr, "b");
Nate Begeman162d3ba2010-06-03 04:04:09 +0000581 case OpMul:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000582 s += a + " * " + b;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000583 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000584 case OpMlaN:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000585 c = Duplicate(nElts << (int)quad, typestr, "c");
Nate Begeman162d3ba2010-06-03 04:04:09 +0000586 case OpMla:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000587 s += a + " + ( " + b + " * " + c + " )";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000588 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000589 case OpMlsN:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000590 c = Duplicate(nElts << (int)quad, typestr, "c");
Nate Begeman162d3ba2010-06-03 04:04:09 +0000591 case OpMls:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000592 s += a + " - ( " + b + " * " + c + " )";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000593 break;
594 case OpEq:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000595 s += "(__neon_" + ts + ")(" + a + " == " + b + ")";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000596 break;
597 case OpGe:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000598 s += "(__neon_" + ts + ")(" + a + " >= " + b + ")";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000599 break;
600 case OpLe:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000601 s += "(__neon_" + ts + ")(" + a + " <= " + b + ")";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000602 break;
603 case OpGt:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000604 s += "(__neon_" + ts + ")(" + a + " > " + b + ")";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000605 break;
606 case OpLt:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000607 s += "(__neon_" + ts + ")(" + a + " < " + b + ")";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000608 break;
609 case OpNeg:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000610 s += " -" + a;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000611 break;
612 case OpNot:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000613 s += " ~" + a;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000614 break;
615 case OpAnd:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000616 s += a + " & " + b;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000617 break;
618 case OpOr:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000619 s += a + " | " + b;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000620 break;
621 case OpXor:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000622 s += a + " ^ " + b;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000623 break;
624 case OpAndNot:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000625 s += a + " & ~" + b;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000626 break;
627 case OpOrNot:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000628 s += a + " | ~" + b;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000629 break;
Nate Begeman3861e742010-06-03 21:35:22 +0000630 case OpCast:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000631 s += "(__neon_" + ts + ")" + a;
Nate Begeman3861e742010-06-03 21:35:22 +0000632 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000633 case OpConcat:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000634 s += "__builtin_shufflevector((__neon_int64x1_t)" + a;
635 s += ", (__neon_int64x1_t)" + b + ", 0, 1)";
Nate Begeman900f4672010-06-08 00:14:42 +0000636 break;
Nate Begeman6c060db2010-06-09 01:09:00 +0000637 case OpHi:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000638 s += "(((__neon_float64x2_t)" + a + ")[1])";
Nate Begeman6c060db2010-06-09 01:09:00 +0000639 break;
640 case OpLo:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000641 s += "(((__neon_float64x2_t)" + a + ")[0])";
Nate Begeman6c060db2010-06-09 01:09:00 +0000642 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000643 case OpDup:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000644 s += Duplicate(nElts << (int)quad, typestr, a);
Nate Begemancc3c41a2010-06-12 03:09:49 +0000645 break;
646 case OpSelect:
647 // ((0 & 1) | (~0 & 2))
648 ts = TypeString(proto[1], typestr);
Bob Wilson1ce588c2010-11-16 18:43:07 +0000649 s += "( " + a + " & (__neon_" + ts + ")" + b + ") | ";
650 s += "(~" + a + " & (__neon_" + ts + ")" + c + ")";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000651 break;
652 case OpRev16:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000653 s += "__builtin_shufflevector(" + a + ", " + a;
Nate Begeman4da883a2010-06-15 22:10:31 +0000654 for (unsigned i = 2; i <= nElts << (int)quad; i += 2)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000655 for (unsigned j = 0; j != 2; ++j)
656 s += ", " + utostr(i - j - 1);
657 s += ")";
658 break;
659 case OpRev32:
660 nElts >>= 1;
Bob Wilson1ce588c2010-11-16 18:43:07 +0000661 s += "__builtin_shufflevector(" + a + ", " + a;
Nate Begeman4da883a2010-06-15 22:10:31 +0000662 for (unsigned i = nElts; i <= nElts << (1 + (int)quad); i += nElts)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000663 for (unsigned j = 0; j != nElts; ++j)
664 s += ", " + utostr(i - j - 1);
665 s += ")";
666 break;
667 case OpRev64:
Bob Wilson1ce588c2010-11-16 18:43:07 +0000668 s += "__builtin_shufflevector(" + a + ", " + a;
Nate Begeman4da883a2010-06-15 22:10:31 +0000669 for (unsigned i = nElts; i <= nElts << (int)quad; i += nElts)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000670 for (unsigned j = 0; j != nElts; ++j)
671 s += ", " + utostr(i - j - 1);
672 s += ")";
Nate Begeman900f4672010-06-08 00:14:42 +0000673 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000674 default:
675 throw "unknown OpKind!";
676 break;
677 }
Bob Wilsonee9ca072010-09-15 01:52:33 +0000678 if (op == OpHi || op == OpLo)
679 s += "; return u.r;";
680 else
681 s += "; return r;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000682 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000683}
684
Nate Begemanb0a4e452010-06-07 16:00:37 +0000685static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
686 unsigned mod = proto[0];
687 unsigned ret = 0;
688
Nate Begeman900f4672010-06-08 00:14:42 +0000689 if (mod == 'v' || mod == 'f')
Nate Begemanb0a4e452010-06-07 16:00:37 +0000690 mod = proto[1];
691
692 bool quad = false;
693 bool poly = false;
694 bool usgn = false;
695 bool scal = false;
696 bool cnst = false;
697 bool pntr = false;
698
Nate Begeman59d70cb2010-08-06 01:24:11 +0000699 // Base type to get the type string for.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000700 char type = ClassifyType(typestr, quad, poly, usgn);
701
702 // Based on the modifying character, change the type and width if necessary.
703 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
Nate Begeman59d70cb2010-08-06 01:24:11 +0000704
Nate Begemanb0a4e452010-06-07 16:00:37 +0000705 if (usgn)
706 ret |= 0x08;
Nate Begeman59d70cb2010-08-06 01:24:11 +0000707 if (quad && proto[1] != 'g')
Nate Begemanb0a4e452010-06-07 16:00:37 +0000708 ret |= 0x10;
709
710 switch (type) {
711 case 'c':
712 ret |= poly ? 5 : 0;
713 break;
714 case 's':
715 ret |= poly ? 6 : 1;
716 break;
717 case 'i':
718 ret |= 2;
719 break;
720 case 'l':
721 ret |= 3;
722 break;
723 case 'h':
724 ret |= 7;
725 break;
726 case 'f':
727 ret |= 4;
728 break;
729 default:
730 throw "unhandled type!";
731 break;
732 }
733 return ret;
734}
735
Nate Begeman7c8c8832010-06-02 21:53:00 +0000736// Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
Bob Wilson1ce588c2010-11-16 18:43:07 +0000737// If structTypes is true, the NEON types are structs of vector types rather
738// than vector types, and the call becomes __builtin_neon_cls(a.val)
Nate Begeman7c8c8832010-06-02 21:53:00 +0000739static std::string GenBuiltin(const std::string &name, const std::string &proto,
Bob Wilson1ce588c2010-11-16 18:43:07 +0000740 StringRef typestr, ClassKind ck,
741 bool structTypes = true) {
Nate Begemancc3c41a2010-06-12 03:09:49 +0000742 bool dummy, quad = false;
743 char type = ClassifyType(typestr, quad, dummy, dummy);
744 unsigned nElts = 0;
745 switch (type) {
746 case 'c': nElts = 8; break;
747 case 's': nElts = 4; break;
748 case 'i': nElts = 2; break;
749 case 'l': nElts = 1; break;
750 case 'h': nElts = 4; break;
751 case 'f': nElts = 2; break;
752 }
Chris Lattner5ca96982010-06-12 15:46:56 +0000753 if (quad) nElts <<= 1;
Nate Begemancc3c41a2010-06-12 03:09:49 +0000754
Nate Begeman7c8c8832010-06-02 21:53:00 +0000755 char arg = 'a';
Nate Begeman162d3ba2010-06-03 04:04:09 +0000756 std::string s;
Nate Begeman9e584b32010-06-04 22:53:30 +0000757
Nate Begemanc4a1b652010-06-20 21:09:52 +0000758 // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
759 // sret-like argument.
760 bool sret = (proto[0] == '2' || proto[0] == '3' || proto[0] == '4');
761
762 // If this builtin takes an immediate argument, we need to #define it rather
763 // than use a standard declaration, so that SemaChecking can range check
764 // the immediate passed by the user.
Nate Begeman6c060db2010-06-09 01:09:00 +0000765 bool define = proto.find('i') != std::string::npos;
Nate Begeman9e584b32010-06-04 22:53:30 +0000766
767 // If all types are the same size, bitcasting the args will take care
768 // of arg checking. The actual signedness etc. will be taken care of with
769 // special enums.
770 if (proto.find('s') == std::string::npos)
771 ck = ClassB;
Nate Begeman7c21f742010-06-04 21:36:00 +0000772
Nate Begeman162d3ba2010-06-03 04:04:09 +0000773 if (proto[0] != 'v') {
Nate Begeman6c060db2010-06-09 01:09:00 +0000774 std::string ts = TypeString(proto[0], typestr);
775
776 if (define) {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000777 if (sret)
778 s += "({ " + ts + " r; ";
779 else if (proto[0] != 's')
Bob Wilson1ce588c2010-11-16 18:43:07 +0000780 s += "(" + ts + "){(__neon_" + ts + ")";
Nate Begemanc4a1b652010-06-20 21:09:52 +0000781 } else if (sret) {
782 s += ts + " r; ";
Nate Begeman9e584b32010-06-04 22:53:30 +0000783 } else {
Bob Wilson1ce588c2010-11-16 18:43:07 +0000784 s += ts + " r; r";
785 if (structTypes && proto[0] != 's' && proto[0] != 'i' && proto[0] != 'l')
786 s += ".val";
787
788 s += " = ";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000789 }
Nate Begeman4b425a82010-06-10 00:16:56 +0000790 }
791
792 bool splat = proto.find('a') != std::string::npos;
Nate Begeman7c8c8832010-06-02 21:53:00 +0000793
794 s += "__builtin_neon_";
Nate Begeman4b425a82010-06-10 00:16:56 +0000795 if (splat) {
796 std::string vname(name, 0, name.size()-2);
797 s += MangleName(vname, typestr, ck);
798 } else {
799 s += MangleName(name, typestr, ck);
800 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000801 s += "(";
Nate Begemanc4a1b652010-06-20 21:09:52 +0000802
803 // Pass the address of the return variable as the first argument to sret-like
804 // builtins.
805 if (sret)
806 s += "&r, ";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000807
808 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
Nate Begemancc3c41a2010-06-12 03:09:49 +0000809 std::string args = std::string(&arg, 1);
810 if (define)
811 args = "(" + args + ")";
812
Nate Begeman9e584b32010-06-04 22:53:30 +0000813 // Handle multiple-vector values specially, emitting each subvector as an
814 // argument to the __builtin.
Bob Wilson1ce588c2010-11-16 18:43:07 +0000815 if (structTypes && (proto[i] == '2' || proto[i] == '3' || proto[i] == '4')){
Nate Begeman9e584b32010-06-04 22:53:30 +0000816 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
Bob Wilson1ce588c2010-11-16 18:43:07 +0000817 s += args + ".val[" + utostr(vi) + "].val";
Nate Begeman9e584b32010-06-04 22:53:30 +0000818 if ((vi + 1) < ve)
819 s += ", ";
820 }
821 if ((i + 1) < e)
822 s += ", ";
823
824 continue;
825 }
826
Nate Begeman4b425a82010-06-10 00:16:56 +0000827 if (splat && (i + 1) == e)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000828 s += Duplicate(nElts, typestr, args);
Nate Begeman4b425a82010-06-10 00:16:56 +0000829 else
Nate Begemancc3c41a2010-06-12 03:09:49 +0000830 s += args;
Bob Wilson1ce588c2010-11-16 18:43:07 +0000831
832 if (structTypes && proto[i] != 's' && proto[i] != 'i' && proto[i] != 'l' &&
833 proto[i] != 'p' && proto[i] != 'c' && proto[i] != 'a') {
834 s += ".val";
835 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000836 if ((i + 1) < e)
837 s += ", ";
838 }
839
Nate Begeman9e584b32010-06-04 22:53:30 +0000840 // Extra constant integer to hold type class enum for this function, e.g. s8
Nate Begeman9e584b32010-06-04 22:53:30 +0000841 if (ck == ClassB)
Nate Begemanb0a4e452010-06-07 16:00:37 +0000842 s += ", " + utostr(GetNeonEnum(proto, typestr));
Nate Begeman9e584b32010-06-04 22:53:30 +0000843
Nate Begeman6c060db2010-06-09 01:09:00 +0000844 if (define)
845 s += ")";
846 else
847 s += ");";
Nate Begeman9e584b32010-06-04 22:53:30 +0000848
849 if (proto[0] != 'v') {
Nate Begeman6c060db2010-06-09 01:09:00 +0000850 if (define) {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000851 if (sret)
852 s += "; r; })";
Bob Wilson1ce588c2010-11-16 18:43:07 +0000853 else if (proto[0] != 's')
854 s += "}";
Nate Begeman6c060db2010-06-09 01:09:00 +0000855 } else {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000856 s += " return r;";
Nate Begeman6c060db2010-06-09 01:09:00 +0000857 }
Nate Begeman9e584b32010-06-04 22:53:30 +0000858 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000859 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000860}
861
Nate Begeman73cef3e2010-06-04 01:26:15 +0000862static std::string GenBuiltinDef(const std::string &name,
863 const std::string &proto,
864 StringRef typestr, ClassKind ck) {
865 std::string s("BUILTIN(__builtin_neon_");
Nate Begeman92f98af2010-06-04 07:11:25 +0000866
867 // If all types are the same size, bitcasting the args will take care
868 // of arg checking. The actual signedness etc. will be taken care of with
869 // special enums.
870 if (proto.find('s') == std::string::npos)
871 ck = ClassB;
872
Nate Begeman73cef3e2010-06-04 01:26:15 +0000873 s += MangleName(name, typestr, ck);
874 s += ", \"";
875
Nate Begeman92f98af2010-06-04 07:11:25 +0000876 for (unsigned i = 0, e = proto.size(); i != e; ++i)
Nate Begeman7c21f742010-06-04 21:36:00 +0000877 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
878
879 // Extra constant integer to hold type class enum for this function, e.g. s8
880 if (ck == ClassB)
881 s += "i";
Nate Begeman73cef3e2010-06-04 01:26:15 +0000882
883 s += "\", \"n\")";
884 return s;
885}
886
Nate Begemand72c9002010-06-13 04:47:03 +0000887/// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h
888/// is comprised of type definitions and function declarations.
Nate Begeman5ddb0872010-05-28 01:08:32 +0000889void NeonEmitter::run(raw_ostream &OS) {
890 EmitSourceFileHeader("ARM NEON Header", OS);
891
892 // FIXME: emit license into file?
893
894 OS << "#ifndef __ARM_NEON_H\n";
895 OS << "#define __ARM_NEON_H\n\n";
896
897 OS << "#ifndef __ARM_NEON__\n";
898 OS << "#error \"NEON support not enabled\"\n";
899 OS << "#endif\n\n";
900
901 OS << "#include <stdint.h>\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000902
903 // Emit NEON-specific scalar typedefs.
Nate Begeman7c8c8832010-06-02 21:53:00 +0000904 OS << "typedef float float32_t;\n";
905 OS << "typedef uint8_t poly8_t;\n";
906 OS << "typedef uint16_t poly16_t;\n";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000907 OS << "typedef uint16_t float16_t;\n";
Nate Begeman9e584b32010-06-04 22:53:30 +0000908
Nate Begeman7c8c8832010-06-02 21:53:00 +0000909 // Emit Neon vector typedefs.
910 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
911 SmallVector<StringRef, 24> TDTypeVec;
912 ParseTypes(0, TypedefTypes, TDTypeVec);
913
914 // Emit vector typedefs.
Nate Begeman9e584b32010-06-04 22:53:30 +0000915 for (unsigned v = 1; v != 5; ++v) {
916 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
917 bool dummy, quad = false;
918 (void) ClassifyType(TDTypeVec[i], quad, dummy, dummy);
919 OS << "typedef __attribute__(( __vector_size__(";
920
921 OS << utostr(8*v*(quad ? 2 : 1)) << ") )) ";
Bob Wilson72774c92010-09-14 21:52:34 +0000922 if (!quad && v == 1)
Nate Begeman9e584b32010-06-04 22:53:30 +0000923 OS << " ";
924
925 OS << TypeString('s', TDTypeVec[i]);
926 OS << " __neon_";
927
928 char t = (v == 1) ? 'd' : '0' + v;
929 OS << TypeString(t, TDTypeVec[i]) << ";\n";
930 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000931 }
932 OS << "\n";
Bob Wilsonee9ca072010-09-15 01:52:33 +0000933 OS << "typedef __attribute__(( __vector_size__(8) )) "
934 "double __neon_float64x1_t;\n";
935 OS << "typedef __attribute__(( __vector_size__(16) )) "
936 "double __neon_float64x2_t;\n";
937 OS << "\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000938
939 // Emit struct typedefs.
940 for (unsigned vi = 1; vi != 5; ++vi) {
941 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
Bob Wilson9969bc32010-06-24 22:03:41 +0000942 std::string ts = TypeString('d', TDTypeVec[i], vi == 1);
943 std::string vs = TypeString((vi > 1) ? '0' + vi : 'd', TDTypeVec[i]);
944 std::string tag = (vi > 1) ? vs : StructTag(TDTypeVec[i]);
Bob Wilson1ce588c2010-11-16 18:43:07 +0000945 OS << "typedef struct " << tag << " {\n";
946 OS << " " << ts << " val";
947 if (vi > 1)
Nate Begeman7c8c8832010-06-02 21:53:00 +0000948 OS << "[" << utostr(vi) << "]";
Bob Wilson1ce588c2010-11-16 18:43:07 +0000949 OS << ";\n} " << vs << ";\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000950 }
951 }
Nate Begeman5ddb0872010-05-28 01:08:32 +0000952
Nate Begeman7c8c8832010-06-02 21:53:00 +0000953 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
954
Nate Begeman5ddb0872010-05-28 01:08:32 +0000955 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
956
Nate Begeman22237772010-06-02 00:34:55 +0000957 // Unique the return+pattern types, and assign them.
Nate Begeman5ddb0872010-05-28 01:08:32 +0000958 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
959 Record *R = RV[i];
Nate Begeman22237772010-06-02 00:34:55 +0000960 std::string name = LowercaseString(R->getName());
961 std::string Proto = R->getValueAsString("Prototype");
Nate Begeman5ddb0872010-05-28 01:08:32 +0000962 std::string Types = R->getValueAsString("Types");
Nate Begeman22237772010-06-02 00:34:55 +0000963
964 SmallVector<StringRef, 16> TypeVec;
965 ParseTypes(R, Types, TypeVec);
966
Nate Begeman162d3ba2010-06-03 04:04:09 +0000967 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
Nate Begemane66aab52010-06-02 07:14:28 +0000968
Nate Begeman6c060db2010-06-09 01:09:00 +0000969 bool define = Proto.find('i') != std::string::npos;
970
Nate Begeman22237772010-06-02 00:34:55 +0000971 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
972 assert(!Proto.empty() && "");
973
Nate Begeman7c8c8832010-06-02 21:53:00 +0000974 // static always inline + return type
Nate Begeman6c060db2010-06-09 01:09:00 +0000975 if (define)
976 OS << "#define";
977 else
978 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
Nate Begeman22237772010-06-02 00:34:55 +0000979
Nate Begemane66aab52010-06-02 07:14:28 +0000980 // Function name with type suffix
Nate Begemana8979a02010-06-04 00:21:41 +0000981 OS << " " << MangleName(name, TypeVec[ti], ClassS);
Nate Begeman22237772010-06-02 00:34:55 +0000982
Nate Begemane66aab52010-06-02 07:14:28 +0000983 // Function arguments
984 OS << GenArgs(Proto, TypeVec[ti]);
Nate Begeman22237772010-06-02 00:34:55 +0000985
Nate Begemane66aab52010-06-02 07:14:28 +0000986 // Definition.
Nate Begeman6c060db2010-06-09 01:09:00 +0000987 if (define)
988 OS << " ";
989 else
990 OS << " { ";
Nate Begeman22237772010-06-02 00:34:55 +0000991
Nate Begemana8979a02010-06-04 00:21:41 +0000992 if (k != OpNone) {
Nate Begeman162d3ba2010-06-03 04:04:09 +0000993 OS << GenOpString(k, Proto, TypeVec[ti]);
Nate Begemana8979a02010-06-04 00:21:41 +0000994 } else {
995 if (R->getSuperClasses().size() < 2)
996 throw TGError(R->getLoc(), "Builtin has no class kind");
997
998 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
999
1000 if (ck == ClassNone)
1001 throw TGError(R->getLoc(), "Builtin has no class kind");
1002 OS << GenBuiltin(name, Proto, TypeVec[ti], ck);
1003 }
Nate Begeman6c060db2010-06-09 01:09:00 +00001004 if (!define)
1005 OS << " }";
1006 OS << "\n";
Nate Begeman22237772010-06-02 00:34:55 +00001007 }
1008 OS << "\n";
Nate Begeman5ddb0872010-05-28 01:08:32 +00001009 }
Nate Begeman73cef3e2010-06-04 01:26:15 +00001010 OS << "#undef __ai\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +00001011 OS << "#endif /* __ARM_NEON_H */\n";
Nate Begeman5ddb0872010-05-28 01:08:32 +00001012}
Nate Begemana8979a02010-06-04 00:21:41 +00001013
Nate Begeman918f8e42010-06-14 05:17:23 +00001014static unsigned RangeFromType(StringRef typestr) {
1015 // base type to get the type string for.
1016 bool quad = false, dummy = false;
1017 char type = ClassifyType(typestr, quad, dummy, dummy);
1018
1019 switch (type) {
1020 case 'c':
Nate Begeman4da883a2010-06-15 22:10:31 +00001021 return (8 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001022 case 'h':
1023 case 's':
Nate Begeman4da883a2010-06-15 22:10:31 +00001024 return (4 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001025 case 'f':
1026 case 'i':
Nate Begeman4da883a2010-06-15 22:10:31 +00001027 return (2 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001028 case 'l':
Nate Begeman4da883a2010-06-15 22:10:31 +00001029 return (1 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001030 default:
1031 throw "unhandled type!";
1032 break;
1033 }
Bob Wilsonfdb530d2010-07-28 18:21:10 +00001034 assert(0 && "unreachable");
1035 return 0;
Nate Begeman918f8e42010-06-14 05:17:23 +00001036}
1037
Nate Begemanf8c4c272010-06-17 04:15:13 +00001038/// runHeader - Emit a file with sections defining:
1039/// 1. the NEON section of BuiltinsARM.def.
1040/// 2. the SemaChecking code for the type overload checking.
1041/// 3. the SemaChecking code for validation of intrinsic immedate arguments.
Nate Begemana8979a02010-06-04 00:21:41 +00001042void NeonEmitter::runHeader(raw_ostream &OS) {
Nate Begeman73cef3e2010-06-04 01:26:15 +00001043 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1044
1045 StringMap<OpKind> EmittedMap;
1046
Nate Begemanf8c4c272010-06-17 04:15:13 +00001047 // Generate BuiltinsARM.def for NEON
1048 OS << "#ifdef GET_NEON_BUILTINS\n";
Nate Begeman73cef3e2010-06-04 01:26:15 +00001049 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1050 Record *R = RV[i];
Nate Begeman73cef3e2010-06-04 01:26:15 +00001051 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1052 if (k != OpNone)
1053 continue;
Nate Begeman73cef3e2010-06-04 01:26:15 +00001054
Nate Begemanf8c4c272010-06-17 04:15:13 +00001055 std::string Proto = R->getValueAsString("Prototype");
1056
Nate Begemand72c9002010-06-13 04:47:03 +00001057 // Functions with 'a' (the splat code) in the type prototype should not get
1058 // their own builtin as they use the non-splat variant.
Nate Begeman4b425a82010-06-10 00:16:56 +00001059 if (Proto.find('a') != std::string::npos)
1060 continue;
Nate Begemand72c9002010-06-13 04:47:03 +00001061
Nate Begemanf8c4c272010-06-17 04:15:13 +00001062 std::string Types = R->getValueAsString("Types");
Nate Begeman73cef3e2010-06-04 01:26:15 +00001063 SmallVector<StringRef, 16> TypeVec;
1064 ParseTypes(R, Types, TypeVec);
Nate Begemand72c9002010-06-13 04:47:03 +00001065
Nate Begeman73cef3e2010-06-04 01:26:15 +00001066 if (R->getSuperClasses().size() < 2)
1067 throw TGError(R->getLoc(), "Builtin has no class kind");
1068
Nate Begemanf8c4c272010-06-17 04:15:13 +00001069 std::string name = LowercaseString(R->getName());
Nate Begeman73cef3e2010-06-04 01:26:15 +00001070 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1071
1072 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
Nate Begemand72c9002010-06-13 04:47:03 +00001073 // Generate the BuiltinsARM.def declaration for this builtin, ensuring
1074 // that each unique BUILTIN() macro appears only once in the output
1075 // stream.
Nate Begeman73cef3e2010-06-04 01:26:15 +00001076 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
1077 if (EmittedMap.count(bd))
1078 continue;
1079
1080 EmittedMap[bd] = OpNone;
1081 OS << bd << "\n";
1082 }
Nate Begemanf8c4c272010-06-17 04:15:13 +00001083 }
1084 OS << "#endif\n\n";
1085
1086 // Generate the overloaded type checking code for SemaChecking.cpp
1087 OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
1088 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1089 Record *R = RV[i];
1090 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1091 if (k != OpNone)
Nate Begemand72c9002010-06-13 04:47:03 +00001092 continue;
Nate Begemanf8c4c272010-06-17 04:15:13 +00001093
1094 std::string Proto = R->getValueAsString("Prototype");
1095 std::string Types = R->getValueAsString("Types");
1096 std::string name = LowercaseString(R->getName());
1097
1098 // Functions with 'a' (the splat code) in the type prototype should not get
1099 // their own builtin as they use the non-splat variant.
1100 if (Proto.find('a') != std::string::npos)
1101 continue;
1102
1103 // Functions which have a scalar argument cannot be overloaded, no need to
1104 // check them if we are emitting the type checking code.
1105 if (Proto.find('s') != std::string::npos)
1106 continue;
1107
1108 SmallVector<StringRef, 16> TypeVec;
1109 ParseTypes(R, Types, TypeVec);
1110
1111 if (R->getSuperClasses().size() < 2)
1112 throw TGError(R->getLoc(), "Builtin has no class kind");
1113
1114 int si = -1, qi = -1;
1115 unsigned mask = 0, qmask = 0;
1116 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1117 // Generate the switch case(s) for this builtin for the type validation.
1118 bool quad = false, poly = false, usgn = false;
1119 (void) ClassifyType(TypeVec[ti], quad, poly, usgn);
1120
1121 if (quad) {
1122 qi = ti;
1123 qmask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1124 } else {
1125 si = ti;
1126 mask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1127 }
1128 }
1129 if (mask)
1130 OS << "case ARM::BI__builtin_neon_"
1131 << MangleName(name, TypeVec[si], ClassB)
1132 << ": mask = " << "0x" << utohexstr(mask) << "; break;\n";
1133 if (qmask)
1134 OS << "case ARM::BI__builtin_neon_"
1135 << MangleName(name, TypeVec[qi], ClassB)
1136 << ": mask = " << "0x" << utohexstr(qmask) << "; break;\n";
1137 }
1138 OS << "#endif\n\n";
1139
1140 // Generate the intrinsic range checking code for shift/lane immediates.
1141 OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
1142 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1143 Record *R = RV[i];
1144
1145 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1146 if (k != OpNone)
1147 continue;
1148
1149 std::string name = LowercaseString(R->getName());
1150 std::string Proto = R->getValueAsString("Prototype");
1151 std::string Types = R->getValueAsString("Types");
1152
1153 // Functions with 'a' (the splat code) in the type prototype should not get
1154 // their own builtin as they use the non-splat variant.
1155 if (Proto.find('a') != std::string::npos)
1156 continue;
1157
1158 // Functions which do not have an immediate do not need to have range
1159 // checking code emitted.
1160 if (Proto.find('i') == std::string::npos)
1161 continue;
1162
1163 SmallVector<StringRef, 16> TypeVec;
1164 ParseTypes(R, Types, TypeVec);
1165
1166 if (R->getSuperClasses().size() < 2)
1167 throw TGError(R->getLoc(), "Builtin has no class kind");
1168
1169 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1170
1171 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1172 std::string namestr, shiftstr, rangestr;
1173
1174 // Builtins which are overloaded by type will need to have their upper
1175 // bound computed at Sema time based on the type constant.
1176 if (Proto.find('s') == std::string::npos) {
1177 ck = ClassB;
1178 if (R->getValueAsBit("isShift")) {
1179 shiftstr = ", true";
1180
1181 // Right shifts have an 'r' in the name, left shifts do not.
1182 if (name.find('r') != std::string::npos)
1183 rangestr = "l = 1; ";
1184 }
1185 rangestr += "u = RFT(TV" + shiftstr + ")";
1186 } else {
1187 rangestr = "u = " + utostr(RangeFromType(TypeVec[ti]));
1188 }
Nate Begemanc4a1b652010-06-20 21:09:52 +00001189 // Make sure cases appear only once by uniquing them in a string map.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001190 namestr = MangleName(name, TypeVec[ti], ck);
1191 if (EmittedMap.count(namestr))
1192 continue;
1193 EmittedMap[namestr] = OpNone;
Nate Begemanc4a1b652010-06-20 21:09:52 +00001194
1195 // Calculate the index of the immediate that should be range checked.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001196 unsigned immidx = 0;
Nate Begemanc4a1b652010-06-20 21:09:52 +00001197
1198 // Builtins that return a struct of multiple vectors have an extra
1199 // leading arg for the struct return.
1200 if (Proto[0] == '2' || Proto[0] == '3' || Proto[0] == '4')
1201 ++immidx;
1202
1203 // Add one to the index for each argument until we reach the immediate
1204 // to be checked. Structs of vectors are passed as multiple arguments.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001205 for (unsigned ii = 1, ie = Proto.size(); ii != ie; ++ii) {
1206 switch (Proto[ii]) {
1207 default: immidx += 1; break;
1208 case '2': immidx += 2; break;
1209 case '3': immidx += 3; break;
1210 case '4': immidx += 4; break;
1211 case 'i': ie = ii + 1; break;
1212 }
1213 }
1214 OS << "case ARM::BI__builtin_neon_" << MangleName(name, TypeVec[ti], ck)
1215 << ": i = " << immidx << "; " << rangestr << "; break;\n";
Nate Begemand72c9002010-06-13 04:47:03 +00001216 }
Nate Begeman73cef3e2010-06-04 01:26:15 +00001217 }
Nate Begemanf8c4c272010-06-17 04:15:13 +00001218 OS << "#endif\n\n";
Nate Begemana8979a02010-06-04 00:21:41 +00001219}