blob: 5fa60ae1c7d14f15be9bcd90e11b809aabcfc3ea [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;
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')
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
Nate Begemand72c9002010-06-13 04:47:03 +0000374/// MangleName - Append a type or width suffix to a base neon function name,
375/// and insert a 'q' in the appropriate location if the operation works on
376/// 128b rather than 64b. E.g. turn "vst2_lane" into "vst2q_lane_f32", etc.
Nate Begemana8979a02010-06-04 00:21:41 +0000377static std::string MangleName(const std::string &name, StringRef typestr,
378 ClassKind ck) {
Nate Begeman900f4672010-06-08 00:14:42 +0000379 if (name == "vcvt_f32_f16")
380 return name;
381
Nate Begemanaf905ef2010-06-02 06:17:19 +0000382 bool quad = false;
383 bool poly = false;
384 bool usgn = false;
385 char type = ClassifyType(typestr, quad, poly, usgn);
386
387 std::string s = name;
388
389 switch (type) {
Nate Begemana8979a02010-06-04 00:21:41 +0000390 case 'c':
391 switch (ck) {
392 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
393 case ClassI: s += "_i8"; break;
394 case ClassW: s += "_8"; break;
395 default: break;
396 }
397 break;
398 case 's':
399 switch (ck) {
400 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
401 case ClassI: s += "_i16"; break;
402 case ClassW: s += "_16"; break;
403 default: break;
404 }
405 break;
406 case 'i':
407 switch (ck) {
408 case ClassS: s += usgn ? "_u32" : "_s32"; break;
409 case ClassI: s += "_i32"; break;
410 case ClassW: s += "_32"; break;
411 default: break;
412 }
413 break;
414 case 'l':
415 switch (ck) {
416 case ClassS: s += usgn ? "_u64" : "_s64"; break;
417 case ClassI: s += "_i64"; break;
418 case ClassW: s += "_64"; break;
419 default: break;
420 }
421 break;
422 case 'h':
423 switch (ck) {
424 case ClassS:
425 case ClassI: s += "_f16"; break;
426 case ClassW: s += "_16"; break;
427 default: break;
428 }
429 break;
430 case 'f':
431 switch (ck) {
432 case ClassS:
433 case ClassI: s += "_f32"; break;
434 case ClassW: s += "_32"; break;
435 default: break;
436 }
437 break;
438 default:
439 throw "unhandled type!";
440 break;
Nate Begemanaf905ef2010-06-02 06:17:19 +0000441 }
Nate Begemana8979a02010-06-04 00:21:41 +0000442 if (ck == ClassB)
Nate Begeman92f98af2010-06-04 07:11:25 +0000443 s += "_v";
Nate Begemana8979a02010-06-04 00:21:41 +0000444
Nate Begemanaf905ef2010-06-02 06:17:19 +0000445 // Insert a 'q' before the first '_' character so that it ends up before
446 // _lane or _n on vector-scalar operations.
447 if (quad) {
448 size_t pos = s.find('_');
449 s = s.insert(pos, "q");
450 }
451 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000452}
453
Nate Begemanaf905ef2010-06-02 06:17:19 +0000454// Generate the string "(argtype a, argtype b, ...)"
Nate Begeman22237772010-06-02 00:34:55 +0000455static std::string GenArgs(const std::string &proto, StringRef typestr) {
Nate Begeman6c060db2010-06-09 01:09:00 +0000456 bool define = proto.find('i') != std::string::npos;
Nate Begemanaf905ef2010-06-02 06:17:19 +0000457 char arg = 'a';
458
459 std::string s;
460 s += "(";
461
462 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
Nate Begeman6c060db2010-06-09 01:09:00 +0000463 if (!define) {
464 s += TypeString(proto[i], typestr);
465 s.push_back(' ');
466 }
Nate Begemanaf905ef2010-06-02 06:17:19 +0000467 s.push_back(arg);
468 if ((i + 1) < e)
469 s += ", ";
470 }
471
472 s += ")";
473 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000474}
475
Nate Begemancc3c41a2010-06-12 03:09:49 +0000476static std::string Duplicate(unsigned nElts, StringRef typestr,
477 const std::string &a) {
Nate Begeman4b425a82010-06-10 00:16:56 +0000478 std::string s;
479
Bob Wilson6904d7a2010-11-16 19:39:14 +0000480 s = "(" + TypeString('d', typestr) + "){ ";
Nate Begeman4b425a82010-06-10 00:16:56 +0000481 for (unsigned i = 0; i != nElts; ++i) {
482 s += a;
483 if ((i + 1) < nElts)
484 s += ", ";
485 }
486 s += " }";
487
488 return s;
489}
490
Bob Wilsonb308b622010-11-16 23:57:01 +0000491static unsigned GetNumElements(StringRef typestr, bool &quad) {
492 quad = false;
493 bool dummy = false;
Nate Begemancc3c41a2010-06-12 03:09:49 +0000494 char type = ClassifyType(typestr, quad, dummy, dummy);
495 unsigned nElts = 0;
496 switch (type) {
Bob Wilsonb308b622010-11-16 23:57:01 +0000497 case 'c': nElts = 8; break;
498 case 's': nElts = 4; break;
499 case 'i': nElts = 2; break;
500 case 'l': nElts = 1; break;
501 case 'h': nElts = 4; break;
502 case 'f': nElts = 2; break;
503 default:
504 throw "unhandled type!";
505 break;
Nate Begemancc3c41a2010-06-12 03:09:49 +0000506 }
Bob Wilsonb308b622010-11-16 23:57:01 +0000507 if (quad) nElts <<= 1;
508 return nElts;
509}
510
511// Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
512static std::string GenOpString(OpKind op, const std::string &proto,
513 StringRef typestr) {
514 bool quad;
515 unsigned nElts = GetNumElements(typestr, quad);
Nate Begemancc3c41a2010-06-12 03:09:49 +0000516
Nate Begeman4b425a82010-06-10 00:16:56 +0000517 std::string ts = TypeString(proto[0], typestr);
Bob Wilsonee9ca072010-09-15 01:52:33 +0000518 std::string s;
519 if (op == OpHi || op == OpLo) {
520 s = "union { " + ts + " r; double d; } u; u.d";
521 } else {
522 s = ts + " r; r";
Bob Wilsonee9ca072010-09-15 01:52:33 +0000523 }
Nate Begeman900f4672010-06-08 00:14:42 +0000524
525 s += " = ";
526
Nate Begeman162d3ba2010-06-03 04:04:09 +0000527 switch(op) {
528 case OpAdd:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000529 s += "a + b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000530 break;
531 case OpSub:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000532 s += "a - b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000533 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000534 case OpMulN:
Bob Wilsonb308b622010-11-16 23:57:01 +0000535 s += "a * " + Duplicate(nElts, typestr, "b");
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000536 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000537 case OpMul:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000538 s += "a * b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000539 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000540 case OpMlaN:
Bob Wilsonb308b622010-11-16 23:57:01 +0000541 s += "a + (b * " + Duplicate(nElts, typestr, "c") + ")";
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000542 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000543 case OpMla:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000544 s += "a + (b * c)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000545 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000546 case OpMlsN:
Bob Wilsonb308b622010-11-16 23:57:01 +0000547 s += "a - (b * " + Duplicate(nElts, typestr, "c") + ")";
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000548 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000549 case OpMls:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000550 s += "a - (b * c)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000551 break;
552 case OpEq:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000553 s += "(" + ts + ")(a == b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000554 break;
555 case OpGe:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000556 s += "(" + ts + ")(a >= b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000557 break;
558 case OpLe:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000559 s += "(" + ts + ")(a <= b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000560 break;
561 case OpGt:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000562 s += "(" + ts + ")(a > b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000563 break;
564 case OpLt:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000565 s += "(" + ts + ")(a < b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000566 break;
567 case OpNeg:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000568 s += " -a";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000569 break;
570 case OpNot:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000571 s += " ~a";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000572 break;
573 case OpAnd:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000574 s += "a & b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000575 break;
576 case OpOr:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000577 s += "a | b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000578 break;
579 case OpXor:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000580 s += "a ^ b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000581 break;
582 case OpAndNot:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000583 s += "a & ~b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000584 break;
585 case OpOrNot:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000586 s += "a | ~b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000587 break;
Nate Begeman3861e742010-06-03 21:35:22 +0000588 case OpCast:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000589 s += "(" + ts + ")a";
Nate Begeman3861e742010-06-03 21:35:22 +0000590 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000591 case OpConcat:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000592 s += "__builtin_shufflevector((int64x1_t)a";
593 s += ", (int64x1_t)b, 0, 1)";
Nate Begeman900f4672010-06-08 00:14:42 +0000594 break;
Nate Begeman6c060db2010-06-09 01:09:00 +0000595 case OpHi:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000596 s += "(((float64x2_t)a)[1])";
Nate Begeman6c060db2010-06-09 01:09:00 +0000597 break;
598 case OpLo:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000599 s += "(((float64x2_t)a)[0])";
Nate Begeman6c060db2010-06-09 01:09:00 +0000600 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000601 case OpDup:
Bob Wilsonb308b622010-11-16 23:57:01 +0000602 s += Duplicate(nElts, typestr, "a");
Nate Begemancc3c41a2010-06-12 03:09:49 +0000603 break;
604 case OpSelect:
605 // ((0 & 1) | (~0 & 2))
606 ts = TypeString(proto[1], typestr);
Bob Wilson6904d7a2010-11-16 19:39:14 +0000607 s += "(a & (" + ts + ")b) | ";
608 s += "(~a & (" + ts + ")c)";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000609 break;
610 case OpRev16:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000611 s += "__builtin_shufflevector(a, a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000612 for (unsigned i = 2; i <= nElts; i += 2)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000613 for (unsigned j = 0; j != 2; ++j)
614 s += ", " + utostr(i - j - 1);
615 s += ")";
616 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000617 case OpRev32: {
618 unsigned WordElts = nElts >> (1 + (int)quad);
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000619 s += "__builtin_shufflevector(a, a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000620 for (unsigned i = WordElts; i <= nElts; i += WordElts)
621 for (unsigned j = 0; j != WordElts; ++j)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000622 s += ", " + utostr(i - j - 1);
623 s += ")";
624 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000625 }
626 case OpRev64: {
627 unsigned DblWordElts = nElts >> (int)quad;
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000628 s += "__builtin_shufflevector(a, a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000629 for (unsigned i = DblWordElts; i <= nElts; i += DblWordElts)
630 for (unsigned j = 0; j != DblWordElts; ++j)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000631 s += ", " + utostr(i - j - 1);
632 s += ")";
Nate Begeman900f4672010-06-08 00:14:42 +0000633 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000634 }
Nate Begeman162d3ba2010-06-03 04:04:09 +0000635 default:
636 throw "unknown OpKind!";
637 break;
638 }
Bob Wilsonee9ca072010-09-15 01:52:33 +0000639 if (op == OpHi || op == OpLo)
640 s += "; return u.r;";
641 else
642 s += "; return r;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000643 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000644}
645
Nate Begemanb0a4e452010-06-07 16:00:37 +0000646static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
647 unsigned mod = proto[0];
648 unsigned ret = 0;
649
Nate Begeman900f4672010-06-08 00:14:42 +0000650 if (mod == 'v' || mod == 'f')
Nate Begemanb0a4e452010-06-07 16:00:37 +0000651 mod = proto[1];
652
653 bool quad = false;
654 bool poly = false;
655 bool usgn = false;
656 bool scal = false;
657 bool cnst = false;
658 bool pntr = false;
659
Nate Begeman59d70cb2010-08-06 01:24:11 +0000660 // Base type to get the type string for.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000661 char type = ClassifyType(typestr, quad, poly, usgn);
662
663 // Based on the modifying character, change the type and width if necessary.
664 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
Nate Begeman59d70cb2010-08-06 01:24:11 +0000665
Nate Begemanb0a4e452010-06-07 16:00:37 +0000666 if (usgn)
667 ret |= 0x08;
Nate Begeman59d70cb2010-08-06 01:24:11 +0000668 if (quad && proto[1] != 'g')
Nate Begemanb0a4e452010-06-07 16:00:37 +0000669 ret |= 0x10;
670
671 switch (type) {
672 case 'c':
673 ret |= poly ? 5 : 0;
674 break;
675 case 's':
676 ret |= poly ? 6 : 1;
677 break;
678 case 'i':
679 ret |= 2;
680 break;
681 case 'l':
682 ret |= 3;
683 break;
684 case 'h':
685 ret |= 7;
686 break;
687 case 'f':
688 ret |= 4;
689 break;
690 default:
691 throw "unhandled type!";
692 break;
693 }
694 return ret;
695}
696
Nate Begeman7c8c8832010-06-02 21:53:00 +0000697// Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
Nate Begeman7c8c8832010-06-02 21:53:00 +0000698static std::string GenBuiltin(const std::string &name, const std::string &proto,
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000699 StringRef typestr, ClassKind ck) {
Bob Wilsonb308b622010-11-16 23:57:01 +0000700 bool quad;
701 unsigned nElts = GetNumElements(typestr, quad);
Nate Begeman7c8c8832010-06-02 21:53:00 +0000702 char arg = 'a';
Nate Begeman162d3ba2010-06-03 04:04:09 +0000703 std::string s;
Nate Begeman9e584b32010-06-04 22:53:30 +0000704
Nate Begemanc4a1b652010-06-20 21:09:52 +0000705 // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
706 // sret-like argument.
707 bool sret = (proto[0] == '2' || proto[0] == '3' || proto[0] == '4');
708
709 // If this builtin takes an immediate argument, we need to #define it rather
710 // than use a standard declaration, so that SemaChecking can range check
711 // the immediate passed by the user.
Nate Begeman6c060db2010-06-09 01:09:00 +0000712 bool define = proto.find('i') != std::string::npos;
Nate Begeman9e584b32010-06-04 22:53:30 +0000713
714 // If all types are the same size, bitcasting the args will take care
715 // of arg checking. The actual signedness etc. will be taken care of with
716 // special enums.
717 if (proto.find('s') == std::string::npos)
718 ck = ClassB;
Nate Begeman7c21f742010-06-04 21:36:00 +0000719
Nate Begeman162d3ba2010-06-03 04:04:09 +0000720 if (proto[0] != 'v') {
Nate Begeman6c060db2010-06-09 01:09:00 +0000721 std::string ts = TypeString(proto[0], typestr);
722
723 if (define) {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000724 if (sret)
725 s += "({ " + ts + " r; ";
726 else if (proto[0] != 's')
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000727 s += "(" + ts + ")";
Nate Begemanc4a1b652010-06-20 21:09:52 +0000728 } else if (sret) {
729 s += ts + " r; ";
Nate Begeman9e584b32010-06-04 22:53:30 +0000730 } else {
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000731 s += ts + " r; r = ";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000732 }
Nate Begeman4b425a82010-06-10 00:16:56 +0000733 }
734
735 bool splat = proto.find('a') != std::string::npos;
Nate Begeman7c8c8832010-06-02 21:53:00 +0000736
737 s += "__builtin_neon_";
Nate Begeman4b425a82010-06-10 00:16:56 +0000738 if (splat) {
739 std::string vname(name, 0, name.size()-2);
740 s += MangleName(vname, typestr, ck);
741 } else {
742 s += MangleName(name, typestr, ck);
743 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000744 s += "(";
Nate Begemanc4a1b652010-06-20 21:09:52 +0000745
746 // Pass the address of the return variable as the first argument to sret-like
747 // builtins.
748 if (sret)
749 s += "&r, ";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000750
751 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
Nate Begemancc3c41a2010-06-12 03:09:49 +0000752 std::string args = std::string(&arg, 1);
753 if (define)
754 args = "(" + args + ")";
755
Nate Begeman9e584b32010-06-04 22:53:30 +0000756 // Handle multiple-vector values specially, emitting each subvector as an
757 // argument to the __builtin.
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000758 if (proto[i] == '2' || proto[i] == '3' || proto[i] == '4') {
Nate Begeman9e584b32010-06-04 22:53:30 +0000759 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000760 s += args + ".val[" + utostr(vi) + "]";
Nate Begeman9e584b32010-06-04 22:53:30 +0000761 if ((vi + 1) < ve)
762 s += ", ";
763 }
764 if ((i + 1) < e)
765 s += ", ";
766
767 continue;
768 }
769
Nate Begeman4b425a82010-06-10 00:16:56 +0000770 if (splat && (i + 1) == e)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000771 s += Duplicate(nElts, typestr, args);
Nate Begeman4b425a82010-06-10 00:16:56 +0000772 else
Nate Begemancc3c41a2010-06-12 03:09:49 +0000773 s += args;
Nate Begeman7c8c8832010-06-02 21:53:00 +0000774 if ((i + 1) < e)
775 s += ", ";
776 }
777
Nate Begeman9e584b32010-06-04 22:53:30 +0000778 // Extra constant integer to hold type class enum for this function, e.g. s8
Nate Begeman9e584b32010-06-04 22:53:30 +0000779 if (ck == ClassB)
Nate Begemanb0a4e452010-06-07 16:00:37 +0000780 s += ", " + utostr(GetNeonEnum(proto, typestr));
Nate Begeman9e584b32010-06-04 22:53:30 +0000781
Nate Begeman6c060db2010-06-09 01:09:00 +0000782 if (define)
783 s += ")";
784 else
785 s += ");";
Nate Begeman9e584b32010-06-04 22:53:30 +0000786
787 if (proto[0] != 'v') {
Nate Begeman6c060db2010-06-09 01:09:00 +0000788 if (define) {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000789 if (sret)
790 s += "; r; })";
Nate Begeman6c060db2010-06-09 01:09:00 +0000791 } else {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000792 s += " return r;";
Nate Begeman6c060db2010-06-09 01:09:00 +0000793 }
Nate Begeman9e584b32010-06-04 22:53:30 +0000794 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000795 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000796}
797
Nate Begeman73cef3e2010-06-04 01:26:15 +0000798static std::string GenBuiltinDef(const std::string &name,
799 const std::string &proto,
800 StringRef typestr, ClassKind ck) {
801 std::string s("BUILTIN(__builtin_neon_");
Nate Begeman92f98af2010-06-04 07:11:25 +0000802
803 // If all types are the same size, bitcasting the args will take care
804 // of arg checking. The actual signedness etc. will be taken care of with
805 // special enums.
806 if (proto.find('s') == std::string::npos)
807 ck = ClassB;
808
Nate Begeman73cef3e2010-06-04 01:26:15 +0000809 s += MangleName(name, typestr, ck);
810 s += ", \"";
811
Nate Begeman92f98af2010-06-04 07:11:25 +0000812 for (unsigned i = 0, e = proto.size(); i != e; ++i)
Nate Begeman7c21f742010-06-04 21:36:00 +0000813 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
814
815 // Extra constant integer to hold type class enum for this function, e.g. s8
816 if (ck == ClassB)
817 s += "i";
Nate Begeman73cef3e2010-06-04 01:26:15 +0000818
819 s += "\", \"n\")";
820 return s;
821}
822
Nate Begemand72c9002010-06-13 04:47:03 +0000823/// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h
824/// is comprised of type definitions and function declarations.
Nate Begeman5ddb0872010-05-28 01:08:32 +0000825void NeonEmitter::run(raw_ostream &OS) {
826 EmitSourceFileHeader("ARM NEON Header", OS);
827
828 // FIXME: emit license into file?
829
830 OS << "#ifndef __ARM_NEON_H\n";
831 OS << "#define __ARM_NEON_H\n\n";
832
833 OS << "#ifndef __ARM_NEON__\n";
834 OS << "#error \"NEON support not enabled\"\n";
835 OS << "#endif\n\n";
836
837 OS << "#include <stdint.h>\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000838
839 // Emit NEON-specific scalar typedefs.
Nate Begeman7c8c8832010-06-02 21:53:00 +0000840 OS << "typedef float float32_t;\n";
Bob Wilson4fbf6382010-11-16 23:57:03 +0000841 OS << "typedef int8_t poly8_t;\n";
842 OS << "typedef int16_t poly16_t;\n";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000843 OS << "typedef uint16_t float16_t;\n";
Nate Begeman9e584b32010-06-04 22:53:30 +0000844
Nate Begeman7c8c8832010-06-02 21:53:00 +0000845 // Emit Neon vector typedefs.
846 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
847 SmallVector<StringRef, 24> TDTypeVec;
848 ParseTypes(0, TypedefTypes, TDTypeVec);
849
850 // Emit vector typedefs.
Bob Wilson6904d7a2010-11-16 19:39:14 +0000851 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
Bob Wilson6a8aceb2010-11-16 23:57:06 +0000852 bool dummy, quad = false, poly = false;
853 (void) ClassifyType(TDTypeVec[i], quad, poly, dummy);
854 if (poly)
855 OS << "typedef __attribute__((neon_polyvector_type(";
856 else
857 OS << "typedef __attribute__((neon_vector_type(";
Nate Begeman9e584b32010-06-04 22:53:30 +0000858
Bob Wilson6a8aceb2010-11-16 23:57:06 +0000859 unsigned nElts = GetNumElements(TDTypeVec[i], quad);
860 OS << utostr(nElts) << "))) ";
861 if (nElts < 10)
Bob Wilson6904d7a2010-11-16 19:39:14 +0000862 OS << " ";
Nate Begeman9e584b32010-06-04 22:53:30 +0000863
Bob Wilson6904d7a2010-11-16 19:39:14 +0000864 OS << TypeString('s', TDTypeVec[i]);
865 OS << " " << TypeString('d', TDTypeVec[i]) << ";\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000866 }
867 OS << "\n";
Bob Wilson6a8aceb2010-11-16 23:57:06 +0000868 OS << "typedef __attribute__((__vector_size__(8))) "
Bob Wilson6904d7a2010-11-16 19:39:14 +0000869 "double float64x1_t;\n";
Bob Wilson6a8aceb2010-11-16 23:57:06 +0000870 OS << "typedef __attribute__((__vector_size__(16))) "
Bob Wilson6904d7a2010-11-16 19:39:14 +0000871 "double float64x2_t;\n";
Bob Wilsonee9ca072010-09-15 01:52:33 +0000872 OS << "\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000873
874 // Emit struct typedefs.
Bob Wilson6904d7a2010-11-16 19:39:14 +0000875 for (unsigned vi = 2; vi != 5; ++vi) {
Nate Begeman7c8c8832010-06-02 21:53:00 +0000876 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
Bob Wilson6904d7a2010-11-16 19:39:14 +0000877 std::string ts = TypeString('d', TDTypeVec[i]);
878 std::string vs = TypeString('0' + vi, TDTypeVec[i]);
879 OS << "typedef struct " << vs << " {\n";
880 OS << " " << ts << " val";
881 OS << "[" << utostr(vi) << "]";
882 OS << ";\n} ";
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000883 OS << vs << ";\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000884 }
885 }
Nate Begeman5ddb0872010-05-28 01:08:32 +0000886
Nate Begeman7c8c8832010-06-02 21:53:00 +0000887 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
888
Nate Begeman5ddb0872010-05-28 01:08:32 +0000889 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
890
Nate Begeman22237772010-06-02 00:34:55 +0000891 // Unique the return+pattern types, and assign them.
Nate Begeman5ddb0872010-05-28 01:08:32 +0000892 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
893 Record *R = RV[i];
Nate Begeman22237772010-06-02 00:34:55 +0000894 std::string name = LowercaseString(R->getName());
895 std::string Proto = R->getValueAsString("Prototype");
Nate Begeman5ddb0872010-05-28 01:08:32 +0000896 std::string Types = R->getValueAsString("Types");
Nate Begeman22237772010-06-02 00:34:55 +0000897
898 SmallVector<StringRef, 16> TypeVec;
899 ParseTypes(R, Types, TypeVec);
900
Nate Begeman162d3ba2010-06-03 04:04:09 +0000901 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
Nate Begemane66aab52010-06-02 07:14:28 +0000902
Nate Begeman6c060db2010-06-09 01:09:00 +0000903 bool define = Proto.find('i') != std::string::npos;
904
Nate Begeman22237772010-06-02 00:34:55 +0000905 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
906 assert(!Proto.empty() && "");
907
Nate Begeman7c8c8832010-06-02 21:53:00 +0000908 // static always inline + return type
Nate Begeman6c060db2010-06-09 01:09:00 +0000909 if (define)
910 OS << "#define";
911 else
912 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
Nate Begeman22237772010-06-02 00:34:55 +0000913
Nate Begemane66aab52010-06-02 07:14:28 +0000914 // Function name with type suffix
Nate Begemana8979a02010-06-04 00:21:41 +0000915 OS << " " << MangleName(name, TypeVec[ti], ClassS);
Nate Begeman22237772010-06-02 00:34:55 +0000916
Nate Begemane66aab52010-06-02 07:14:28 +0000917 // Function arguments
918 OS << GenArgs(Proto, TypeVec[ti]);
Nate Begeman22237772010-06-02 00:34:55 +0000919
Nate Begemane66aab52010-06-02 07:14:28 +0000920 // Definition.
Nate Begeman6c060db2010-06-09 01:09:00 +0000921 if (define)
922 OS << " ";
923 else
924 OS << " { ";
Nate Begeman22237772010-06-02 00:34:55 +0000925
Nate Begemana8979a02010-06-04 00:21:41 +0000926 if (k != OpNone) {
Nate Begeman162d3ba2010-06-03 04:04:09 +0000927 OS << GenOpString(k, Proto, TypeVec[ti]);
Nate Begemana8979a02010-06-04 00:21:41 +0000928 } else {
929 if (R->getSuperClasses().size() < 2)
930 throw TGError(R->getLoc(), "Builtin has no class kind");
931
932 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
933
934 if (ck == ClassNone)
935 throw TGError(R->getLoc(), "Builtin has no class kind");
936 OS << GenBuiltin(name, Proto, TypeVec[ti], ck);
937 }
Nate Begeman6c060db2010-06-09 01:09:00 +0000938 if (!define)
939 OS << " }";
940 OS << "\n";
Nate Begeman22237772010-06-02 00:34:55 +0000941 }
942 OS << "\n";
Nate Begeman5ddb0872010-05-28 01:08:32 +0000943 }
Nate Begeman73cef3e2010-06-04 01:26:15 +0000944 OS << "#undef __ai\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000945 OS << "#endif /* __ARM_NEON_H */\n";
Nate Begeman5ddb0872010-05-28 01:08:32 +0000946}
Nate Begemana8979a02010-06-04 00:21:41 +0000947
Nate Begeman918f8e42010-06-14 05:17:23 +0000948static unsigned RangeFromType(StringRef typestr) {
949 // base type to get the type string for.
950 bool quad = false, dummy = false;
951 char type = ClassifyType(typestr, quad, dummy, dummy);
952
953 switch (type) {
954 case 'c':
Nate Begeman4da883a2010-06-15 22:10:31 +0000955 return (8 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +0000956 case 'h':
957 case 's':
Nate Begeman4da883a2010-06-15 22:10:31 +0000958 return (4 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +0000959 case 'f':
960 case 'i':
Nate Begeman4da883a2010-06-15 22:10:31 +0000961 return (2 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +0000962 case 'l':
Nate Begeman4da883a2010-06-15 22:10:31 +0000963 return (1 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +0000964 default:
965 throw "unhandled type!";
966 break;
967 }
Bob Wilsonfdb530d2010-07-28 18:21:10 +0000968 assert(0 && "unreachable");
969 return 0;
Nate Begeman918f8e42010-06-14 05:17:23 +0000970}
971
Nate Begemanf8c4c272010-06-17 04:15:13 +0000972/// runHeader - Emit a file with sections defining:
973/// 1. the NEON section of BuiltinsARM.def.
974/// 2. the SemaChecking code for the type overload checking.
975/// 3. the SemaChecking code for validation of intrinsic immedate arguments.
Nate Begemana8979a02010-06-04 00:21:41 +0000976void NeonEmitter::runHeader(raw_ostream &OS) {
Nate Begeman73cef3e2010-06-04 01:26:15 +0000977 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
978
979 StringMap<OpKind> EmittedMap;
980
Nate Begemanf8c4c272010-06-17 04:15:13 +0000981 // Generate BuiltinsARM.def for NEON
982 OS << "#ifdef GET_NEON_BUILTINS\n";
Nate Begeman73cef3e2010-06-04 01:26:15 +0000983 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
984 Record *R = RV[i];
Nate Begeman73cef3e2010-06-04 01:26:15 +0000985 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
986 if (k != OpNone)
987 continue;
Nate Begeman73cef3e2010-06-04 01:26:15 +0000988
Nate Begemanf8c4c272010-06-17 04:15:13 +0000989 std::string Proto = R->getValueAsString("Prototype");
990
Nate Begemand72c9002010-06-13 04:47:03 +0000991 // Functions with 'a' (the splat code) in the type prototype should not get
992 // their own builtin as they use the non-splat variant.
Nate Begeman4b425a82010-06-10 00:16:56 +0000993 if (Proto.find('a') != std::string::npos)
994 continue;
Nate Begemand72c9002010-06-13 04:47:03 +0000995
Nate Begemanf8c4c272010-06-17 04:15:13 +0000996 std::string Types = R->getValueAsString("Types");
Nate Begeman73cef3e2010-06-04 01:26:15 +0000997 SmallVector<StringRef, 16> TypeVec;
998 ParseTypes(R, Types, TypeVec);
Nate Begemand72c9002010-06-13 04:47:03 +0000999
Nate Begeman73cef3e2010-06-04 01:26:15 +00001000 if (R->getSuperClasses().size() < 2)
1001 throw TGError(R->getLoc(), "Builtin has no class kind");
1002
Nate Begemanf8c4c272010-06-17 04:15:13 +00001003 std::string name = LowercaseString(R->getName());
Nate Begeman73cef3e2010-06-04 01:26:15 +00001004 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1005
1006 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
Nate Begemand72c9002010-06-13 04:47:03 +00001007 // Generate the BuiltinsARM.def declaration for this builtin, ensuring
1008 // that each unique BUILTIN() macro appears only once in the output
1009 // stream.
Nate Begeman73cef3e2010-06-04 01:26:15 +00001010 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
1011 if (EmittedMap.count(bd))
1012 continue;
1013
1014 EmittedMap[bd] = OpNone;
1015 OS << bd << "\n";
1016 }
Nate Begemanf8c4c272010-06-17 04:15:13 +00001017 }
1018 OS << "#endif\n\n";
1019
1020 // Generate the overloaded type checking code for SemaChecking.cpp
1021 OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
1022 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1023 Record *R = RV[i];
1024 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1025 if (k != OpNone)
Nate Begemand72c9002010-06-13 04:47:03 +00001026 continue;
Nate Begemanf8c4c272010-06-17 04:15:13 +00001027
1028 std::string Proto = R->getValueAsString("Prototype");
1029 std::string Types = R->getValueAsString("Types");
1030 std::string name = LowercaseString(R->getName());
1031
1032 // Functions with 'a' (the splat code) in the type prototype should not get
1033 // their own builtin as they use the non-splat variant.
1034 if (Proto.find('a') != std::string::npos)
1035 continue;
1036
1037 // Functions which have a scalar argument cannot be overloaded, no need to
1038 // check them if we are emitting the type checking code.
1039 if (Proto.find('s') != std::string::npos)
1040 continue;
1041
1042 SmallVector<StringRef, 16> TypeVec;
1043 ParseTypes(R, Types, TypeVec);
1044
1045 if (R->getSuperClasses().size() < 2)
1046 throw TGError(R->getLoc(), "Builtin has no class kind");
1047
1048 int si = -1, qi = -1;
1049 unsigned mask = 0, qmask = 0;
1050 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1051 // Generate the switch case(s) for this builtin for the type validation.
1052 bool quad = false, poly = false, usgn = false;
1053 (void) ClassifyType(TypeVec[ti], quad, poly, usgn);
1054
1055 if (quad) {
1056 qi = ti;
1057 qmask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1058 } else {
1059 si = ti;
1060 mask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1061 }
1062 }
1063 if (mask)
1064 OS << "case ARM::BI__builtin_neon_"
1065 << MangleName(name, TypeVec[si], ClassB)
1066 << ": mask = " << "0x" << utohexstr(mask) << "; break;\n";
1067 if (qmask)
1068 OS << "case ARM::BI__builtin_neon_"
1069 << MangleName(name, TypeVec[qi], ClassB)
1070 << ": mask = " << "0x" << utohexstr(qmask) << "; break;\n";
1071 }
1072 OS << "#endif\n\n";
1073
1074 // Generate the intrinsic range checking code for shift/lane immediates.
1075 OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
1076 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1077 Record *R = RV[i];
1078
1079 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1080 if (k != OpNone)
1081 continue;
1082
1083 std::string name = LowercaseString(R->getName());
1084 std::string Proto = R->getValueAsString("Prototype");
1085 std::string Types = R->getValueAsString("Types");
1086
1087 // Functions with 'a' (the splat code) in the type prototype should not get
1088 // their own builtin as they use the non-splat variant.
1089 if (Proto.find('a') != std::string::npos)
1090 continue;
1091
1092 // Functions which do not have an immediate do not need to have range
1093 // checking code emitted.
1094 if (Proto.find('i') == std::string::npos)
1095 continue;
1096
1097 SmallVector<StringRef, 16> TypeVec;
1098 ParseTypes(R, Types, TypeVec);
1099
1100 if (R->getSuperClasses().size() < 2)
1101 throw TGError(R->getLoc(), "Builtin has no class kind");
1102
1103 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1104
1105 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1106 std::string namestr, shiftstr, rangestr;
1107
1108 // Builtins which are overloaded by type will need to have their upper
1109 // bound computed at Sema time based on the type constant.
1110 if (Proto.find('s') == std::string::npos) {
1111 ck = ClassB;
1112 if (R->getValueAsBit("isShift")) {
1113 shiftstr = ", true";
1114
1115 // Right shifts have an 'r' in the name, left shifts do not.
1116 if (name.find('r') != std::string::npos)
1117 rangestr = "l = 1; ";
1118 }
1119 rangestr += "u = RFT(TV" + shiftstr + ")";
1120 } else {
1121 rangestr = "u = " + utostr(RangeFromType(TypeVec[ti]));
1122 }
Nate Begemanc4a1b652010-06-20 21:09:52 +00001123 // Make sure cases appear only once by uniquing them in a string map.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001124 namestr = MangleName(name, TypeVec[ti], ck);
1125 if (EmittedMap.count(namestr))
1126 continue;
1127 EmittedMap[namestr] = OpNone;
Nate Begemanc4a1b652010-06-20 21:09:52 +00001128
1129 // Calculate the index of the immediate that should be range checked.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001130 unsigned immidx = 0;
Nate Begemanc4a1b652010-06-20 21:09:52 +00001131
1132 // Builtins that return a struct of multiple vectors have an extra
1133 // leading arg for the struct return.
1134 if (Proto[0] == '2' || Proto[0] == '3' || Proto[0] == '4')
1135 ++immidx;
1136
1137 // Add one to the index for each argument until we reach the immediate
1138 // to be checked. Structs of vectors are passed as multiple arguments.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001139 for (unsigned ii = 1, ie = Proto.size(); ii != ie; ++ii) {
1140 switch (Proto[ii]) {
1141 default: immidx += 1; break;
1142 case '2': immidx += 2; break;
1143 case '3': immidx += 3; break;
1144 case '4': immidx += 4; break;
1145 case 'i': ie = ii + 1; break;
1146 }
1147 }
1148 OS << "case ARM::BI__builtin_neon_" << MangleName(name, TypeVec[ti], ck)
1149 << ": i = " << immidx << "; " << rangestr << "; break;\n";
Nate Begemand72c9002010-06-13 04:47:03 +00001150 }
Nate Begeman73cef3e2010-06-04 01:26:15 +00001151 }
Nate Begemanf8c4c272010-06-17 04:15:13 +00001152 OS << "#endif\n\n";
Nate Begemana8979a02010-06-04 00:21:41 +00001153}