blob: 5de289e39f3a036dc53adb5683e125ef8336ec6b [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';
Bob Wilsonb055f742010-11-23 19:38:34 +000092 default: throw "unhandled type in narrow!";
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;
Bob Wilson181b76d2010-11-18 21:43:22 +0000137 poly = false;
138 if (type == 'f')
139 type = 'i';
140 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000141 case 'x':
Bob Wilson181b76d2010-11-18 21:43:22 +0000142 usgn = false;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000143 poly = false;
Nate Begeman22237772010-06-02 00:34:55 +0000144 if (type == 'f')
145 type = 'i';
146 break;
147 case 'f':
Nate Begeman900f4672010-06-08 00:14:42 +0000148 if (type == 'h')
149 quad = true;
Nate Begeman22237772010-06-02 00:34:55 +0000150 type = 'f';
Nate Begeman162d3ba2010-06-03 04:04:09 +0000151 usgn = false;
Nate Begeman22237772010-06-02 00:34:55 +0000152 break;
Nate Begeman59d70cb2010-08-06 01:24:11 +0000153 case 'g':
154 quad = false;
155 break;
Nate Begeman22237772010-06-02 00:34:55 +0000156 case 'w':
157 type = Widen(type);
158 quad = true;
159 break;
160 case 'n':
161 type = Widen(type);
162 break;
Nate Begeman22237772010-06-02 00:34:55 +0000163 case 'l':
164 type = 'l';
165 scal = true;
166 usgn = true;
167 break;
168 case 's':
Nate Begeman4b425a82010-06-10 00:16:56 +0000169 case 'a':
Nate Begeman22237772010-06-02 00:34:55 +0000170 scal = true;
171 break;
172 case 'k':
173 quad = true;
174 break;
175 case 'c':
176 cnst = true;
177 case 'p':
178 pntr = true;
179 scal = true;
180 break;
Nate Begeman3861e742010-06-03 21:35:22 +0000181 case 'h':
182 type = Narrow(type);
Nate Begeman900f4672010-06-08 00:14:42 +0000183 if (type == 'h')
184 quad = false;
Nate Begeman3861e742010-06-03 21:35:22 +0000185 break;
186 case 'e':
187 type = Narrow(type);
188 usgn = true;
189 break;
Nate Begeman22237772010-06-02 00:34:55 +0000190 default:
191 break;
192 }
Nate Begemanb0a4e452010-06-07 16:00:37 +0000193 return type;
194}
195
Nate Begemand72c9002010-06-13 04:47:03 +0000196/// TypeString - for a modifier and type, generate the name of the typedef for
Bob Wilson6904d7a2010-11-16 19:39:14 +0000197/// that type. QUc -> uint8x8_t.
198static std::string TypeString(const char mod, StringRef typestr) {
Nate Begemanb0a4e452010-06-07 16:00:37 +0000199 bool quad = false;
200 bool poly = false;
201 bool usgn = false;
202 bool scal = false;
203 bool cnst = false;
204 bool pntr = false;
205
206 if (mod == 'v')
207 return "void";
208 if (mod == 'i')
209 return "int";
210
211 // base type to get the type string for.
212 char type = ClassifyType(typestr, quad, poly, usgn);
213
214 // Based on the modifying character, change the type and width if necessary.
215 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
Nate Begeman22237772010-06-02 00:34:55 +0000216
217 SmallString<128> s;
218
219 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')
Bob Wilson95148ad2010-12-01 19:49:56 +0000296 return "v"; // void
Nate Begeman92f98af2010-06-04 07:11:25 +0000297 if (mod == 'i')
Bob Wilson95148ad2010-12-01 19:49:56 +0000298 return "i"; // int
Nate Begeman92f98af2010-06-04 07:11:25 +0000299
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
Bob Wilson95148ad2010-12-01 19:49:56 +0000306 // All pointers are void* pointers. Change type to 'v' now.
Nate Begemanc4a1b652010-06-20 21:09:52 +0000307 if (pntr) {
308 usgn = false;
309 poly = false;
Nate Begemanb0a4e452010-06-07 16:00:37 +0000310 type = 'v';
Nate Begemanc4a1b652010-06-20 21:09:52 +0000311 }
Bob Wilson95148ad2010-12-01 19:49:56 +0000312 // Treat half-float ('h') types as unsigned short ('s') types.
Nate Begeman92f98af2010-06-04 07:11:25 +0000313 if (type == 'h') {
314 type = 's';
315 usgn = true;
316 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000317 usgn = usgn | poly | ((ck == ClassI || ck == ClassW) && scal && type != 'f');
Nate Begeman92f98af2010-06-04 07:11:25 +0000318
319 if (scal) {
320 SmallString<128> s;
321
322 if (usgn)
323 s.push_back('U');
Nate Begeman7c21f742010-06-04 21:36:00 +0000324
Bob Wilson95148ad2010-12-01 19:49:56 +0000325 if (type == 'l') // 64-bit long
Nate Begeman7c21f742010-06-04 21:36:00 +0000326 s += "LLi";
327 else
328 s.push_back(type);
329
Nate Begeman92f98af2010-06-04 07:11:25 +0000330 if (cnst)
331 s.push_back('C');
332 if (pntr)
333 s.push_back('*');
334 return s.str();
335 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000336
337 // Since the return value must be one type, return a vector type of the
Nate Begemanc4a1b652010-06-20 21:09:52 +0000338 // appropriate width which we will bitcast. An exception is made for
339 // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
340 // fashion, storing them to a pointer arg.
Nate Begeman7c21f742010-06-04 21:36:00 +0000341 if (ret) {
Bob Wilson5b7fe592010-12-01 19:49:51 +0000342 if (mod >= '2' && mod <= '4')
Bob Wilson95148ad2010-12-01 19:49:56 +0000343 return "vv*"; // void result with void* first argument
Nate Begemanf50551e2010-06-09 18:02:26 +0000344 if (mod == 'f' || (ck != ClassB && type == 'f'))
Nate Begeman56387832010-06-08 06:01:16 +0000345 return quad ? "V4f" : "V2f";
Nate Begemanf50551e2010-06-09 18:02:26 +0000346 if (ck != ClassB && type == 's')
Nate Begeman007afe42010-06-09 05:11:55 +0000347 return quad ? "V8s" : "V4s";
Nate Begemanf50551e2010-06-09 18:02:26 +0000348 if (ck != ClassB && type == 'i')
Nate Begeman56387832010-06-08 06:01:16 +0000349 return quad ? "V4i" : "V2i";
Nate Begemanf50551e2010-06-09 18:02:26 +0000350 if (ck != ClassB && type == 'l')
Nate Begeman007afe42010-06-09 05:11:55 +0000351 return quad ? "V2LLi" : "V1LLi";
Nate Begeman900f4672010-06-08 00:14:42 +0000352
Nate Begeman7c21f742010-06-04 21:36:00 +0000353 return quad ? "V16c" : "V8c";
354 }
355
356 // Non-return array types are passed as individual vectors.
Nate Begeman92f98af2010-06-04 07:11:25 +0000357 if (mod == '2')
358 return quad ? "V16cV16c" : "V8cV8c";
359 if (mod == '3')
360 return quad ? "V16cV16cV16c" : "V8cV8cV8c";
361 if (mod == '4')
362 return quad ? "V16cV16cV16cV16c" : "V8cV8cV8cV8c";
363
Nate Begemanf50551e2010-06-09 18:02:26 +0000364 if (mod == 'f' || (ck != ClassB && type == 'f'))
Nate Begeman007afe42010-06-09 05:11:55 +0000365 return quad ? "V4f" : "V2f";
Nate Begemanf50551e2010-06-09 18:02:26 +0000366 if (ck != ClassB && type == 's')
Nate Begeman007afe42010-06-09 05:11:55 +0000367 return quad ? "V8s" : "V4s";
Nate Begemanf50551e2010-06-09 18:02:26 +0000368 if (ck != ClassB && type == 'i')
Nate Begeman007afe42010-06-09 05:11:55 +0000369 return quad ? "V4i" : "V2i";
Nate Begemanf50551e2010-06-09 18:02:26 +0000370 if (ck != ClassB && type == 'l')
Nate Begeman007afe42010-06-09 05:11:55 +0000371 return quad ? "V2LLi" : "V1LLi";
372
Nate Begeman92f98af2010-06-04 07:11:25 +0000373 return quad ? "V16c" : "V8c";
374}
375
Nate Begemand72c9002010-06-13 04:47:03 +0000376/// MangleName - Append a type or width suffix to a base neon function name,
377/// and insert a 'q' in the appropriate location if the operation works on
378/// 128b rather than 64b. E.g. turn "vst2_lane" into "vst2q_lane_f32", etc.
Nate Begemana8979a02010-06-04 00:21:41 +0000379static std::string MangleName(const std::string &name, StringRef typestr,
380 ClassKind ck) {
Nate Begeman900f4672010-06-08 00:14:42 +0000381 if (name == "vcvt_f32_f16")
382 return name;
383
Nate Begemanaf905ef2010-06-02 06:17:19 +0000384 bool quad = false;
385 bool poly = false;
386 bool usgn = false;
387 char type = ClassifyType(typestr, quad, poly, usgn);
388
389 std::string s = name;
390
391 switch (type) {
Nate Begemana8979a02010-06-04 00:21:41 +0000392 case 'c':
393 switch (ck) {
394 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
395 case ClassI: s += "_i8"; break;
396 case ClassW: s += "_8"; break;
397 default: break;
398 }
399 break;
400 case 's':
401 switch (ck) {
402 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
403 case ClassI: s += "_i16"; break;
404 case ClassW: s += "_16"; break;
405 default: break;
406 }
407 break;
408 case 'i':
409 switch (ck) {
410 case ClassS: s += usgn ? "_u32" : "_s32"; break;
411 case ClassI: s += "_i32"; break;
412 case ClassW: s += "_32"; break;
413 default: break;
414 }
415 break;
416 case 'l':
417 switch (ck) {
418 case ClassS: s += usgn ? "_u64" : "_s64"; break;
419 case ClassI: s += "_i64"; break;
420 case ClassW: s += "_64"; break;
421 default: break;
422 }
423 break;
424 case 'h':
425 switch (ck) {
426 case ClassS:
427 case ClassI: s += "_f16"; break;
428 case ClassW: s += "_16"; break;
429 default: break;
430 }
431 break;
432 case 'f':
433 switch (ck) {
434 case ClassS:
435 case ClassI: s += "_f32"; break;
436 case ClassW: s += "_32"; break;
437 default: break;
438 }
439 break;
440 default:
441 throw "unhandled type!";
442 break;
Nate Begemanaf905ef2010-06-02 06:17:19 +0000443 }
Nate Begemana8979a02010-06-04 00:21:41 +0000444 if (ck == ClassB)
Nate Begeman92f98af2010-06-04 07:11:25 +0000445 s += "_v";
Nate Begemana8979a02010-06-04 00:21:41 +0000446
Nate Begemanaf905ef2010-06-02 06:17:19 +0000447 // Insert a 'q' before the first '_' character so that it ends up before
448 // _lane or _n on vector-scalar operations.
449 if (quad) {
450 size_t pos = s.find('_');
451 s = s.insert(pos, "q");
452 }
453 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000454}
455
Nate Begemanaf905ef2010-06-02 06:17:19 +0000456// Generate the string "(argtype a, argtype b, ...)"
Nate Begeman22237772010-06-02 00:34:55 +0000457static std::string GenArgs(const std::string &proto, StringRef typestr) {
Nate Begeman6c060db2010-06-09 01:09:00 +0000458 bool define = proto.find('i') != std::string::npos;
Nate Begemanaf905ef2010-06-02 06:17:19 +0000459 char arg = 'a';
460
461 std::string s;
462 s += "(";
463
464 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
Nate Begeman6c060db2010-06-09 01:09:00 +0000465 if (!define) {
466 s += TypeString(proto[i], typestr);
467 s.push_back(' ');
468 }
Nate Begemanaf905ef2010-06-02 06:17:19 +0000469 s.push_back(arg);
470 if ((i + 1) < e)
471 s += ", ";
472 }
473
474 s += ")";
475 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000476}
477
Nate Begemancc3c41a2010-06-12 03:09:49 +0000478static std::string Duplicate(unsigned nElts, StringRef typestr,
479 const std::string &a) {
Nate Begeman4b425a82010-06-10 00:16:56 +0000480 std::string s;
481
Bob Wilson6904d7a2010-11-16 19:39:14 +0000482 s = "(" + TypeString('d', typestr) + "){ ";
Nate Begeman4b425a82010-06-10 00:16:56 +0000483 for (unsigned i = 0; i != nElts; ++i) {
484 s += a;
485 if ((i + 1) < nElts)
486 s += ", ";
487 }
488 s += " }";
489
490 return s;
491}
492
Bob Wilsonb308b622010-11-16 23:57:01 +0000493static unsigned GetNumElements(StringRef typestr, bool &quad) {
494 quad = false;
495 bool dummy = false;
Nate Begemancc3c41a2010-06-12 03:09:49 +0000496 char type = ClassifyType(typestr, quad, dummy, dummy);
497 unsigned nElts = 0;
498 switch (type) {
Bob Wilsonb308b622010-11-16 23:57:01 +0000499 case 'c': nElts = 8; break;
500 case 's': nElts = 4; break;
501 case 'i': nElts = 2; break;
502 case 'l': nElts = 1; break;
503 case 'h': nElts = 4; break;
504 case 'f': nElts = 2; break;
505 default:
506 throw "unhandled type!";
507 break;
Nate Begemancc3c41a2010-06-12 03:09:49 +0000508 }
Bob Wilsonb308b622010-11-16 23:57:01 +0000509 if (quad) nElts <<= 1;
510 return nElts;
511}
512
513// Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
514static std::string GenOpString(OpKind op, const std::string &proto,
515 StringRef typestr) {
516 bool quad;
517 unsigned nElts = GetNumElements(typestr, quad);
Nate Begemancc3c41a2010-06-12 03:09:49 +0000518
Nate Begeman4b425a82010-06-10 00:16:56 +0000519 std::string ts = TypeString(proto[0], typestr);
Bob Wilsonee9ca072010-09-15 01:52:33 +0000520 std::string s;
521 if (op == OpHi || op == OpLo) {
522 s = "union { " + ts + " r; double d; } u; u.d";
523 } else {
524 s = ts + " r; r";
Bob Wilsonee9ca072010-09-15 01:52:33 +0000525 }
Nate Begeman900f4672010-06-08 00:14:42 +0000526
527 s += " = ";
528
Nate Begeman162d3ba2010-06-03 04:04:09 +0000529 switch(op) {
530 case OpAdd:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000531 s += "a + b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000532 break;
533 case OpSub:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000534 s += "a - b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000535 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000536 case OpMulN:
Bob Wilsonb308b622010-11-16 23:57:01 +0000537 s += "a * " + Duplicate(nElts, typestr, "b");
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000538 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000539 case OpMul:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000540 s += "a * b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000541 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000542 case OpMlaN:
Bob Wilsonb308b622010-11-16 23:57:01 +0000543 s += "a + (b * " + Duplicate(nElts, typestr, "c") + ")";
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000544 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000545 case OpMla:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000546 s += "a + (b * c)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000547 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000548 case OpMlsN:
Bob Wilsonb308b622010-11-16 23:57:01 +0000549 s += "a - (b * " + Duplicate(nElts, typestr, "c") + ")";
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000550 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000551 case OpMls:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000552 s += "a - (b * c)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000553 break;
554 case OpEq:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000555 s += "(" + ts + ")(a == b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000556 break;
557 case OpGe:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000558 s += "(" + ts + ")(a >= b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000559 break;
560 case OpLe:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000561 s += "(" + ts + ")(a <= b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000562 break;
563 case OpGt:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000564 s += "(" + ts + ")(a > b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000565 break;
566 case OpLt:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000567 s += "(" + ts + ")(a < b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000568 break;
569 case OpNeg:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000570 s += " -a";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000571 break;
572 case OpNot:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000573 s += " ~a";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000574 break;
575 case OpAnd:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000576 s += "a & b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000577 break;
578 case OpOr:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000579 s += "a | b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000580 break;
581 case OpXor:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000582 s += "a ^ b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000583 break;
584 case OpAndNot:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000585 s += "a & ~b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000586 break;
587 case OpOrNot:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000588 s += "a | ~b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000589 break;
Nate Begeman3861e742010-06-03 21:35:22 +0000590 case OpCast:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000591 s += "(" + ts + ")a";
Nate Begeman3861e742010-06-03 21:35:22 +0000592 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000593 case OpConcat:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000594 s += "__builtin_shufflevector((int64x1_t)a";
595 s += ", (int64x1_t)b, 0, 1)";
Nate Begeman900f4672010-06-08 00:14:42 +0000596 break;
Nate Begeman6c060db2010-06-09 01:09:00 +0000597 case OpHi:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000598 s += "(((float64x2_t)a)[1])";
Nate Begeman6c060db2010-06-09 01:09:00 +0000599 break;
600 case OpLo:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000601 s += "(((float64x2_t)a)[0])";
Nate Begeman6c060db2010-06-09 01:09:00 +0000602 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000603 case OpDup:
Bob Wilsonb308b622010-11-16 23:57:01 +0000604 s += Duplicate(nElts, typestr, "a");
Nate Begemancc3c41a2010-06-12 03:09:49 +0000605 break;
606 case OpSelect:
607 // ((0 & 1) | (~0 & 2))
608 ts = TypeString(proto[1], typestr);
Bob Wilson6904d7a2010-11-16 19:39:14 +0000609 s += "(a & (" + ts + ")b) | ";
610 s += "(~a & (" + ts + ")c)";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000611 break;
612 case OpRev16:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000613 s += "__builtin_shufflevector(a, a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000614 for (unsigned i = 2; i <= nElts; i += 2)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000615 for (unsigned j = 0; j != 2; ++j)
616 s += ", " + utostr(i - j - 1);
617 s += ")";
618 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000619 case OpRev32: {
620 unsigned WordElts = nElts >> (1 + (int)quad);
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000621 s += "__builtin_shufflevector(a, a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000622 for (unsigned i = WordElts; i <= nElts; i += WordElts)
623 for (unsigned j = 0; j != WordElts; ++j)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000624 s += ", " + utostr(i - j - 1);
625 s += ")";
626 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000627 }
628 case OpRev64: {
629 unsigned DblWordElts = nElts >> (int)quad;
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000630 s += "__builtin_shufflevector(a, a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000631 for (unsigned i = DblWordElts; i <= nElts; i += DblWordElts)
632 for (unsigned j = 0; j != DblWordElts; ++j)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000633 s += ", " + utostr(i - j - 1);
634 s += ")";
Nate Begeman900f4672010-06-08 00:14:42 +0000635 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000636 }
Nate Begeman162d3ba2010-06-03 04:04:09 +0000637 default:
638 throw "unknown OpKind!";
639 break;
640 }
Bob Wilsonee9ca072010-09-15 01:52:33 +0000641 if (op == OpHi || op == OpLo)
642 s += "; return u.r;";
643 else
644 s += "; return r;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000645 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000646}
647
Nate Begemanb0a4e452010-06-07 16:00:37 +0000648static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
649 unsigned mod = proto[0];
650 unsigned ret = 0;
651
Nate Begeman900f4672010-06-08 00:14:42 +0000652 if (mod == 'v' || mod == 'f')
Nate Begemanb0a4e452010-06-07 16:00:37 +0000653 mod = proto[1];
654
655 bool quad = false;
656 bool poly = false;
657 bool usgn = false;
658 bool scal = false;
659 bool cnst = false;
660 bool pntr = false;
661
Nate Begeman59d70cb2010-08-06 01:24:11 +0000662 // Base type to get the type string for.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000663 char type = ClassifyType(typestr, quad, poly, usgn);
664
665 // Based on the modifying character, change the type and width if necessary.
666 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
Nate Begeman59d70cb2010-08-06 01:24:11 +0000667
Nate Begemanb0a4e452010-06-07 16:00:37 +0000668 if (usgn)
669 ret |= 0x08;
Nate Begeman59d70cb2010-08-06 01:24:11 +0000670 if (quad && proto[1] != 'g')
Nate Begemanb0a4e452010-06-07 16:00:37 +0000671 ret |= 0x10;
672
673 switch (type) {
674 case 'c':
675 ret |= poly ? 5 : 0;
676 break;
677 case 's':
678 ret |= poly ? 6 : 1;
679 break;
680 case 'i':
681 ret |= 2;
682 break;
683 case 'l':
684 ret |= 3;
685 break;
686 case 'h':
687 ret |= 7;
688 break;
689 case 'f':
690 ret |= 4;
691 break;
692 default:
693 throw "unhandled type!";
694 break;
695 }
696 return ret;
697}
698
Nate Begeman7c8c8832010-06-02 21:53:00 +0000699// Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
Nate Begeman7c8c8832010-06-02 21:53:00 +0000700static std::string GenBuiltin(const std::string &name, const std::string &proto,
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000701 StringRef typestr, ClassKind ck) {
Bob Wilsonb308b622010-11-16 23:57:01 +0000702 bool quad;
703 unsigned nElts = GetNumElements(typestr, quad);
Nate Begeman7c8c8832010-06-02 21:53:00 +0000704 char arg = 'a';
Nate Begeman162d3ba2010-06-03 04:04:09 +0000705 std::string s;
Nate Begeman9e584b32010-06-04 22:53:30 +0000706
Nate Begemanc4a1b652010-06-20 21:09:52 +0000707 // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
708 // sret-like argument.
Bob Wilson5b7fe592010-12-01 19:49:51 +0000709 bool sret = (proto[0] >= '2' && proto[0] <= '4');
Nate Begemanc4a1b652010-06-20 21:09:52 +0000710
711 // If this builtin takes an immediate argument, we need to #define it rather
712 // than use a standard declaration, so that SemaChecking can range check
713 // the immediate passed by the user.
Nate Begeman6c060db2010-06-09 01:09:00 +0000714 bool define = proto.find('i') != std::string::npos;
Nate Begeman9e584b32010-06-04 22:53:30 +0000715
Bob Wilson95148ad2010-12-01 19:49:56 +0000716 // Check if the prototype has a scalar operand with the type of the vector
717 // elements. If not, bitcasting the args will take care of arg checking.
718 // The actual signedness etc. will be taken care of with special enums.
Nate Begeman9e584b32010-06-04 22:53:30 +0000719 if (proto.find('s') == std::string::npos)
720 ck = ClassB;
Nate Begeman7c21f742010-06-04 21:36:00 +0000721
Nate Begeman162d3ba2010-06-03 04:04:09 +0000722 if (proto[0] != 'v') {
Nate Begeman6c060db2010-06-09 01:09:00 +0000723 std::string ts = TypeString(proto[0], typestr);
724
725 if (define) {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000726 if (sret)
727 s += "({ " + ts + " r; ";
728 else if (proto[0] != 's')
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000729 s += "(" + ts + ")";
Nate Begemanc4a1b652010-06-20 21:09:52 +0000730 } else if (sret) {
731 s += ts + " r; ";
Nate Begeman9e584b32010-06-04 22:53:30 +0000732 } else {
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000733 s += ts + " r; r = ";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000734 }
Nate Begeman4b425a82010-06-10 00:16:56 +0000735 }
736
737 bool splat = proto.find('a') != std::string::npos;
Nate Begeman7c8c8832010-06-02 21:53:00 +0000738
739 s += "__builtin_neon_";
Nate Begeman4b425a82010-06-10 00:16:56 +0000740 if (splat) {
Bob Wilson95148ad2010-12-01 19:49:56 +0000741 // Call the non-splat builtin: chop off the "_n" suffix from the name.
Nate Begeman4b425a82010-06-10 00:16:56 +0000742 std::string vname(name, 0, name.size()-2);
743 s += MangleName(vname, typestr, ck);
744 } else {
745 s += MangleName(name, typestr, ck);
746 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000747 s += "(";
Nate Begemanc4a1b652010-06-20 21:09:52 +0000748
749 // Pass the address of the return variable as the first argument to sret-like
750 // builtins.
751 if (sret)
752 s += "&r, ";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000753
754 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
Nate Begemancc3c41a2010-06-12 03:09:49 +0000755 std::string args = std::string(&arg, 1);
Bob Wilson95148ad2010-12-01 19:49:56 +0000756
757 // Wrap macro arguments in parenthesis.
Nate Begemancc3c41a2010-06-12 03:09:49 +0000758 if (define)
759 args = "(" + args + ")";
760
Nate Begeman9e584b32010-06-04 22:53:30 +0000761 // Handle multiple-vector values specially, emitting each subvector as an
762 // argument to the __builtin.
Bob Wilson5b7fe592010-12-01 19:49:51 +0000763 if (proto[i] >= '2' && proto[i] <= '4') {
Nate Begeman9e584b32010-06-04 22:53:30 +0000764 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000765 s += args + ".val[" + utostr(vi) + "]";
Nate Begeman9e584b32010-06-04 22:53:30 +0000766 if ((vi + 1) < ve)
767 s += ", ";
768 }
769 if ((i + 1) < e)
770 s += ", ";
771
772 continue;
773 }
774
Nate Begeman4b425a82010-06-10 00:16:56 +0000775 if (splat && (i + 1) == e)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000776 s += Duplicate(nElts, typestr, args);
Nate Begeman4b425a82010-06-10 00:16:56 +0000777 else
Nate Begemancc3c41a2010-06-12 03:09:49 +0000778 s += args;
Nate Begeman7c8c8832010-06-02 21:53:00 +0000779 if ((i + 1) < e)
780 s += ", ";
781 }
782
Nate Begeman9e584b32010-06-04 22:53:30 +0000783 // Extra constant integer to hold type class enum for this function, e.g. s8
Nate Begeman9e584b32010-06-04 22:53:30 +0000784 if (ck == ClassB)
Nate Begemanb0a4e452010-06-07 16:00:37 +0000785 s += ", " + utostr(GetNeonEnum(proto, typestr));
Nate Begeman9e584b32010-06-04 22:53:30 +0000786
Nate Begeman6c060db2010-06-09 01:09:00 +0000787 if (define)
788 s += ")";
789 else
790 s += ");";
Nate Begeman9e584b32010-06-04 22:53:30 +0000791
792 if (proto[0] != 'v') {
Nate Begeman6c060db2010-06-09 01:09:00 +0000793 if (define) {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000794 if (sret)
795 s += "; r; })";
Nate Begeman6c060db2010-06-09 01:09:00 +0000796 } else {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000797 s += " return r;";
Nate Begeman6c060db2010-06-09 01:09:00 +0000798 }
Nate Begeman9e584b32010-06-04 22:53:30 +0000799 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000800 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000801}
802
Nate Begeman73cef3e2010-06-04 01:26:15 +0000803static std::string GenBuiltinDef(const std::string &name,
804 const std::string &proto,
805 StringRef typestr, ClassKind ck) {
806 std::string s("BUILTIN(__builtin_neon_");
Nate Begeman92f98af2010-06-04 07:11:25 +0000807
808 // If all types are the same size, bitcasting the args will take care
809 // of arg checking. The actual signedness etc. will be taken care of with
810 // special enums.
811 if (proto.find('s') == std::string::npos)
812 ck = ClassB;
813
Nate Begeman73cef3e2010-06-04 01:26:15 +0000814 s += MangleName(name, typestr, ck);
815 s += ", \"";
816
Nate Begeman92f98af2010-06-04 07:11:25 +0000817 for (unsigned i = 0, e = proto.size(); i != e; ++i)
Nate Begeman7c21f742010-06-04 21:36:00 +0000818 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
819
820 // Extra constant integer to hold type class enum for this function, e.g. s8
821 if (ck == ClassB)
822 s += "i";
Nate Begeman73cef3e2010-06-04 01:26:15 +0000823
824 s += "\", \"n\")";
825 return s;
826}
827
Nate Begemand72c9002010-06-13 04:47:03 +0000828/// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h
829/// is comprised of type definitions and function declarations.
Nate Begeman5ddb0872010-05-28 01:08:32 +0000830void NeonEmitter::run(raw_ostream &OS) {
831 EmitSourceFileHeader("ARM NEON Header", OS);
832
833 // FIXME: emit license into file?
834
835 OS << "#ifndef __ARM_NEON_H\n";
836 OS << "#define __ARM_NEON_H\n\n";
837
838 OS << "#ifndef __ARM_NEON__\n";
839 OS << "#error \"NEON support not enabled\"\n";
840 OS << "#endif\n\n";
841
842 OS << "#include <stdint.h>\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000843
844 // Emit NEON-specific scalar typedefs.
Nate Begeman7c8c8832010-06-02 21:53:00 +0000845 OS << "typedef float float32_t;\n";
Bob Wilson4fbf6382010-11-16 23:57:03 +0000846 OS << "typedef int8_t poly8_t;\n";
847 OS << "typedef int16_t poly16_t;\n";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000848 OS << "typedef uint16_t float16_t;\n";
Nate Begeman9e584b32010-06-04 22:53:30 +0000849
Nate Begeman7c8c8832010-06-02 21:53:00 +0000850 // Emit Neon vector typedefs.
851 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
852 SmallVector<StringRef, 24> TDTypeVec;
853 ParseTypes(0, TypedefTypes, TDTypeVec);
854
855 // Emit vector typedefs.
Bob Wilson6904d7a2010-11-16 19:39:14 +0000856 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
Bob Wilson6a8aceb2010-11-16 23:57:06 +0000857 bool dummy, quad = false, poly = false;
858 (void) ClassifyType(TDTypeVec[i], quad, poly, dummy);
859 if (poly)
860 OS << "typedef __attribute__((neon_polyvector_type(";
861 else
862 OS << "typedef __attribute__((neon_vector_type(";
Nate Begeman9e584b32010-06-04 22:53:30 +0000863
Bob Wilson6a8aceb2010-11-16 23:57:06 +0000864 unsigned nElts = GetNumElements(TDTypeVec[i], quad);
865 OS << utostr(nElts) << "))) ";
866 if (nElts < 10)
Bob Wilson6904d7a2010-11-16 19:39:14 +0000867 OS << " ";
Nate Begeman9e584b32010-06-04 22:53:30 +0000868
Bob Wilson6904d7a2010-11-16 19:39:14 +0000869 OS << TypeString('s', TDTypeVec[i]);
870 OS << " " << TypeString('d', TDTypeVec[i]) << ";\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000871 }
872 OS << "\n";
Bob Wilson6a8aceb2010-11-16 23:57:06 +0000873 OS << "typedef __attribute__((__vector_size__(8))) "
Bob Wilson6904d7a2010-11-16 19:39:14 +0000874 "double float64x1_t;\n";
Bob Wilson6a8aceb2010-11-16 23:57:06 +0000875 OS << "typedef __attribute__((__vector_size__(16))) "
Bob Wilson6904d7a2010-11-16 19:39:14 +0000876 "double float64x2_t;\n";
Bob Wilsonee9ca072010-09-15 01:52:33 +0000877 OS << "\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000878
879 // Emit struct typedefs.
Bob Wilson6904d7a2010-11-16 19:39:14 +0000880 for (unsigned vi = 2; vi != 5; ++vi) {
Nate Begeman7c8c8832010-06-02 21:53:00 +0000881 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
Bob Wilson6904d7a2010-11-16 19:39:14 +0000882 std::string ts = TypeString('d', TDTypeVec[i]);
883 std::string vs = TypeString('0' + vi, TDTypeVec[i]);
884 OS << "typedef struct " << vs << " {\n";
885 OS << " " << ts << " val";
886 OS << "[" << utostr(vi) << "]";
887 OS << ";\n} ";
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000888 OS << vs << ";\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000889 }
890 }
Nate Begeman5ddb0872010-05-28 01:08:32 +0000891
Nate Begeman7c8c8832010-06-02 21:53:00 +0000892 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
893
Nate Begeman5ddb0872010-05-28 01:08:32 +0000894 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
895
Nate Begeman22237772010-06-02 00:34:55 +0000896 // Unique the return+pattern types, and assign them.
Nate Begeman5ddb0872010-05-28 01:08:32 +0000897 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
898 Record *R = RV[i];
Nate Begeman22237772010-06-02 00:34:55 +0000899 std::string name = LowercaseString(R->getName());
900 std::string Proto = R->getValueAsString("Prototype");
Nate Begeman5ddb0872010-05-28 01:08:32 +0000901 std::string Types = R->getValueAsString("Types");
Nate Begeman22237772010-06-02 00:34:55 +0000902
903 SmallVector<StringRef, 16> TypeVec;
904 ParseTypes(R, Types, TypeVec);
905
Nate Begeman162d3ba2010-06-03 04:04:09 +0000906 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
Nate Begemane66aab52010-06-02 07:14:28 +0000907
Nate Begeman6c060db2010-06-09 01:09:00 +0000908 bool define = Proto.find('i') != std::string::npos;
909
Nate Begeman22237772010-06-02 00:34:55 +0000910 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
911 assert(!Proto.empty() && "");
912
Nate Begeman7c8c8832010-06-02 21:53:00 +0000913 // static always inline + return type
Nate Begeman6c060db2010-06-09 01:09:00 +0000914 if (define)
915 OS << "#define";
916 else
917 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
Nate Begeman22237772010-06-02 00:34:55 +0000918
Nate Begemane66aab52010-06-02 07:14:28 +0000919 // Function name with type suffix
Nate Begemana8979a02010-06-04 00:21:41 +0000920 OS << " " << MangleName(name, TypeVec[ti], ClassS);
Nate Begeman22237772010-06-02 00:34:55 +0000921
Nate Begemane66aab52010-06-02 07:14:28 +0000922 // Function arguments
923 OS << GenArgs(Proto, TypeVec[ti]);
Nate Begeman22237772010-06-02 00:34:55 +0000924
Nate Begemane66aab52010-06-02 07:14:28 +0000925 // Definition.
Nate Begeman6c060db2010-06-09 01:09:00 +0000926 if (define)
927 OS << " ";
928 else
929 OS << " { ";
Nate Begeman22237772010-06-02 00:34:55 +0000930
Nate Begemana8979a02010-06-04 00:21:41 +0000931 if (k != OpNone) {
Nate Begeman162d3ba2010-06-03 04:04:09 +0000932 OS << GenOpString(k, Proto, TypeVec[ti]);
Nate Begemana8979a02010-06-04 00:21:41 +0000933 } else {
934 if (R->getSuperClasses().size() < 2)
935 throw TGError(R->getLoc(), "Builtin has no class kind");
936
937 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
938
939 if (ck == ClassNone)
940 throw TGError(R->getLoc(), "Builtin has no class kind");
941 OS << GenBuiltin(name, Proto, TypeVec[ti], ck);
942 }
Nate Begeman6c060db2010-06-09 01:09:00 +0000943 if (!define)
944 OS << " }";
945 OS << "\n";
Nate Begeman22237772010-06-02 00:34:55 +0000946 }
947 OS << "\n";
Nate Begeman5ddb0872010-05-28 01:08:32 +0000948 }
Nate Begeman73cef3e2010-06-04 01:26:15 +0000949 OS << "#undef __ai\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000950 OS << "#endif /* __ARM_NEON_H */\n";
Nate Begeman5ddb0872010-05-28 01:08:32 +0000951}
Nate Begemana8979a02010-06-04 00:21:41 +0000952
Nate Begeman918f8e42010-06-14 05:17:23 +0000953static unsigned RangeFromType(StringRef typestr) {
954 // base type to get the type string for.
955 bool quad = false, dummy = false;
956 char type = ClassifyType(typestr, quad, dummy, dummy);
957
958 switch (type) {
959 case 'c':
Nate Begeman4da883a2010-06-15 22:10:31 +0000960 return (8 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +0000961 case 'h':
962 case 's':
Nate Begeman4da883a2010-06-15 22:10:31 +0000963 return (4 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +0000964 case 'f':
965 case 'i':
Nate Begeman4da883a2010-06-15 22:10:31 +0000966 return (2 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +0000967 case 'l':
Nate Begeman4da883a2010-06-15 22:10:31 +0000968 return (1 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +0000969 default:
970 throw "unhandled type!";
971 break;
972 }
Bob Wilsonfdb530d2010-07-28 18:21:10 +0000973 assert(0 && "unreachable");
974 return 0;
Nate Begeman918f8e42010-06-14 05:17:23 +0000975}
976
Nate Begemanf8c4c272010-06-17 04:15:13 +0000977/// runHeader - Emit a file with sections defining:
978/// 1. the NEON section of BuiltinsARM.def.
979/// 2. the SemaChecking code for the type overload checking.
980/// 3. the SemaChecking code for validation of intrinsic immedate arguments.
Nate Begemana8979a02010-06-04 00:21:41 +0000981void NeonEmitter::runHeader(raw_ostream &OS) {
Nate Begeman73cef3e2010-06-04 01:26:15 +0000982 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
983
984 StringMap<OpKind> EmittedMap;
985
Nate Begemanf8c4c272010-06-17 04:15:13 +0000986 // Generate BuiltinsARM.def for NEON
987 OS << "#ifdef GET_NEON_BUILTINS\n";
Nate Begeman73cef3e2010-06-04 01:26:15 +0000988 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
989 Record *R = RV[i];
Nate Begeman73cef3e2010-06-04 01:26:15 +0000990 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
991 if (k != OpNone)
992 continue;
Nate Begeman73cef3e2010-06-04 01:26:15 +0000993
Nate Begemanf8c4c272010-06-17 04:15:13 +0000994 std::string Proto = R->getValueAsString("Prototype");
995
Nate Begemand72c9002010-06-13 04:47:03 +0000996 // Functions with 'a' (the splat code) in the type prototype should not get
997 // their own builtin as they use the non-splat variant.
Nate Begeman4b425a82010-06-10 00:16:56 +0000998 if (Proto.find('a') != std::string::npos)
999 continue;
Nate Begemand72c9002010-06-13 04:47:03 +00001000
Nate Begemanf8c4c272010-06-17 04:15:13 +00001001 std::string Types = R->getValueAsString("Types");
Nate Begeman73cef3e2010-06-04 01:26:15 +00001002 SmallVector<StringRef, 16> TypeVec;
1003 ParseTypes(R, Types, TypeVec);
Nate Begemand72c9002010-06-13 04:47:03 +00001004
Nate Begeman73cef3e2010-06-04 01:26:15 +00001005 if (R->getSuperClasses().size() < 2)
1006 throw TGError(R->getLoc(), "Builtin has no class kind");
1007
Nate Begemanf8c4c272010-06-17 04:15:13 +00001008 std::string name = LowercaseString(R->getName());
Nate Begeman73cef3e2010-06-04 01:26:15 +00001009 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1010
1011 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
Nate Begemand72c9002010-06-13 04:47:03 +00001012 // Generate the BuiltinsARM.def declaration for this builtin, ensuring
1013 // that each unique BUILTIN() macro appears only once in the output
1014 // stream.
Nate Begeman73cef3e2010-06-04 01:26:15 +00001015 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
1016 if (EmittedMap.count(bd))
1017 continue;
1018
1019 EmittedMap[bd] = OpNone;
1020 OS << bd << "\n";
1021 }
Nate Begemanf8c4c272010-06-17 04:15:13 +00001022 }
1023 OS << "#endif\n\n";
1024
1025 // Generate the overloaded type checking code for SemaChecking.cpp
1026 OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
1027 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1028 Record *R = RV[i];
1029 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1030 if (k != OpNone)
Nate Begemand72c9002010-06-13 04:47:03 +00001031 continue;
Nate Begemanf8c4c272010-06-17 04:15:13 +00001032
1033 std::string Proto = R->getValueAsString("Prototype");
1034 std::string Types = R->getValueAsString("Types");
1035 std::string name = LowercaseString(R->getName());
1036
1037 // Functions with 'a' (the splat code) in the type prototype should not get
1038 // their own builtin as they use the non-splat variant.
1039 if (Proto.find('a') != std::string::npos)
1040 continue;
1041
1042 // Functions which have a scalar argument cannot be overloaded, no need to
1043 // check them if we are emitting the type checking code.
1044 if (Proto.find('s') != std::string::npos)
1045 continue;
1046
1047 SmallVector<StringRef, 16> TypeVec;
1048 ParseTypes(R, Types, TypeVec);
1049
1050 if (R->getSuperClasses().size() < 2)
1051 throw TGError(R->getLoc(), "Builtin has no class kind");
1052
1053 int si = -1, qi = -1;
1054 unsigned mask = 0, qmask = 0;
1055 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1056 // Generate the switch case(s) for this builtin for the type validation.
1057 bool quad = false, poly = false, usgn = false;
1058 (void) ClassifyType(TypeVec[ti], quad, poly, usgn);
1059
1060 if (quad) {
1061 qi = ti;
1062 qmask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1063 } else {
1064 si = ti;
1065 mask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1066 }
1067 }
1068 if (mask)
1069 OS << "case ARM::BI__builtin_neon_"
1070 << MangleName(name, TypeVec[si], ClassB)
1071 << ": mask = " << "0x" << utohexstr(mask) << "; break;\n";
1072 if (qmask)
1073 OS << "case ARM::BI__builtin_neon_"
1074 << MangleName(name, TypeVec[qi], ClassB)
1075 << ": mask = " << "0x" << utohexstr(qmask) << "; break;\n";
1076 }
1077 OS << "#endif\n\n";
1078
1079 // Generate the intrinsic range checking code for shift/lane immediates.
1080 OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
1081 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1082 Record *R = RV[i];
1083
1084 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1085 if (k != OpNone)
1086 continue;
1087
1088 std::string name = LowercaseString(R->getName());
1089 std::string Proto = R->getValueAsString("Prototype");
1090 std::string Types = R->getValueAsString("Types");
1091
1092 // Functions with 'a' (the splat code) in the type prototype should not get
1093 // their own builtin as they use the non-splat variant.
1094 if (Proto.find('a') != std::string::npos)
1095 continue;
1096
1097 // Functions which do not have an immediate do not need to have range
1098 // checking code emitted.
1099 if (Proto.find('i') == std::string::npos)
1100 continue;
1101
1102 SmallVector<StringRef, 16> TypeVec;
1103 ParseTypes(R, Types, TypeVec);
1104
1105 if (R->getSuperClasses().size() < 2)
1106 throw TGError(R->getLoc(), "Builtin has no class kind");
1107
1108 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1109
1110 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1111 std::string namestr, shiftstr, rangestr;
1112
1113 // Builtins which are overloaded by type will need to have their upper
1114 // bound computed at Sema time based on the type constant.
1115 if (Proto.find('s') == std::string::npos) {
1116 ck = ClassB;
1117 if (R->getValueAsBit("isShift")) {
1118 shiftstr = ", true";
1119
1120 // Right shifts have an 'r' in the name, left shifts do not.
1121 if (name.find('r') != std::string::npos)
1122 rangestr = "l = 1; ";
1123 }
1124 rangestr += "u = RFT(TV" + shiftstr + ")";
1125 } else {
1126 rangestr = "u = " + utostr(RangeFromType(TypeVec[ti]));
1127 }
Nate Begemanc4a1b652010-06-20 21:09:52 +00001128 // Make sure cases appear only once by uniquing them in a string map.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001129 namestr = MangleName(name, TypeVec[ti], ck);
1130 if (EmittedMap.count(namestr))
1131 continue;
1132 EmittedMap[namestr] = OpNone;
Nate Begemanc4a1b652010-06-20 21:09:52 +00001133
1134 // Calculate the index of the immediate that should be range checked.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001135 unsigned immidx = 0;
Nate Begemanc4a1b652010-06-20 21:09:52 +00001136
1137 // Builtins that return a struct of multiple vectors have an extra
1138 // leading arg for the struct return.
Bob Wilson5b7fe592010-12-01 19:49:51 +00001139 if (Proto[0] >= '2' && Proto[0] <= '4')
Nate Begemanc4a1b652010-06-20 21:09:52 +00001140 ++immidx;
1141
1142 // Add one to the index for each argument until we reach the immediate
1143 // to be checked. Structs of vectors are passed as multiple arguments.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001144 for (unsigned ii = 1, ie = Proto.size(); ii != ie; ++ii) {
1145 switch (Proto[ii]) {
1146 default: immidx += 1; break;
1147 case '2': immidx += 2; break;
1148 case '3': immidx += 3; break;
1149 case '4': immidx += 4; break;
1150 case 'i': ie = ii + 1; break;
1151 }
1152 }
1153 OS << "case ARM::BI__builtin_neon_" << MangleName(name, TypeVec[ti], ck)
1154 << ": i = " << immidx << "; " << rangestr << "; break;\n";
Nate Begemand72c9002010-06-13 04:47:03 +00001155 }
Nate Begeman73cef3e2010-06-04 01:26:15 +00001156 }
Nate Begemanf8c4c272010-06-17 04:15:13 +00001157 OS << "#endif\n\n";
Nate Begemana8979a02010-06-04 00:21:41 +00001158}