blob: 9feec48e6f0455d83576f18ee971a4f6b246a80f [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;
Bob Wilsonf00140c2010-12-01 19:49:58 +0000163 case 'i':
164 type = 'i';
165 scal = true;
166 break;
Nate Begeman22237772010-06-02 00:34:55 +0000167 case 'l':
168 type = 'l';
169 scal = true;
170 usgn = true;
171 break;
172 case 's':
Nate Begeman4b425a82010-06-10 00:16:56 +0000173 case 'a':
Nate Begeman22237772010-06-02 00:34:55 +0000174 scal = true;
175 break;
176 case 'k':
177 quad = true;
178 break;
179 case 'c':
180 cnst = true;
181 case 'p':
182 pntr = true;
183 scal = true;
184 break;
Nate Begeman3861e742010-06-03 21:35:22 +0000185 case 'h':
186 type = Narrow(type);
Nate Begeman900f4672010-06-08 00:14:42 +0000187 if (type == 'h')
188 quad = false;
Nate Begeman3861e742010-06-03 21:35:22 +0000189 break;
190 case 'e':
191 type = Narrow(type);
192 usgn = true;
193 break;
Nate Begeman22237772010-06-02 00:34:55 +0000194 default:
195 break;
196 }
Nate Begemanb0a4e452010-06-07 16:00:37 +0000197 return type;
198}
199
Nate Begemand72c9002010-06-13 04:47:03 +0000200/// TypeString - for a modifier and type, generate the name of the typedef for
Bob Wilson6904d7a2010-11-16 19:39:14 +0000201/// that type. QUc -> uint8x8_t.
202static std::string TypeString(const char mod, StringRef typestr) {
Nate Begemanb0a4e452010-06-07 16:00:37 +0000203 bool quad = false;
204 bool poly = false;
205 bool usgn = false;
206 bool scal = false;
207 bool cnst = false;
208 bool pntr = false;
209
210 if (mod == 'v')
211 return "void";
212 if (mod == 'i')
213 return "int";
214
215 // base type to get the type string for.
216 char type = ClassifyType(typestr, quad, poly, usgn);
217
218 // Based on the modifying character, change the type and width if necessary.
219 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
Nate Begeman22237772010-06-02 00:34:55 +0000220
221 SmallString<128> s;
222
223 if (usgn)
224 s.push_back('u');
225
226 switch (type) {
227 case 'c':
228 s += poly ? "poly8" : "int8";
229 if (scal)
230 break;
231 s += quad ? "x16" : "x8";
232 break;
233 case 's':
234 s += poly ? "poly16" : "int16";
235 if (scal)
236 break;
237 s += quad ? "x8" : "x4";
238 break;
239 case 'i':
240 s += "int32";
241 if (scal)
242 break;
243 s += quad ? "x4" : "x2";
244 break;
245 case 'l':
246 s += "int64";
247 if (scal)
248 break;
249 s += quad ? "x2" : "x1";
250 break;
251 case 'h':
252 s += "float16";
253 if (scal)
254 break;
255 s += quad ? "x8" : "x4";
256 break;
257 case 'f':
258 s += "float32";
259 if (scal)
260 break;
261 s += quad ? "x4" : "x2";
262 break;
Nate Begeman22237772010-06-02 00:34:55 +0000263 default:
264 throw "unhandled type!";
265 break;
266 }
267
268 if (mod == '2')
269 s += "x2";
270 if (mod == '3')
271 s += "x3";
272 if (mod == '4')
273 s += "x4";
274
275 // Append _t, finishing the type string typedef type.
276 s += "_t";
277
278 if (cnst)
279 s += " const";
280
281 if (pntr)
282 s += " *";
283
284 return s.str();
285}
286
Bob Wilson1ac27cf2010-06-24 22:04:30 +0000287/// BuiltinTypeString - for a modifier and type, generate the clang
288/// BuiltinsARM.def prototype code for the function. See the top of clang's
289/// Builtins.def for a description of the type strings.
Nate Begeman7c21f742010-06-04 21:36:00 +0000290static std::string BuiltinTypeString(const char mod, StringRef typestr,
291 ClassKind ck, bool ret) {
Nate Begeman92f98af2010-06-04 07:11:25 +0000292 bool quad = false;
293 bool poly = false;
294 bool usgn = false;
295 bool scal = false;
296 bool cnst = false;
297 bool pntr = false;
298
299 if (mod == 'v')
Bob Wilson95148ad2010-12-01 19:49:56 +0000300 return "v"; // void
Nate Begeman92f98af2010-06-04 07:11:25 +0000301 if (mod == 'i')
Bob Wilson95148ad2010-12-01 19:49:56 +0000302 return "i"; // int
Nate Begeman92f98af2010-06-04 07:11:25 +0000303
304 // base type to get the type string for.
305 char type = ClassifyType(typestr, quad, poly, usgn);
306
307 // Based on the modifying character, change the type and width if necessary.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000308 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
309
Bob Wilson95148ad2010-12-01 19:49:56 +0000310 // All pointers are void* pointers. Change type to 'v' now.
Nate Begemanc4a1b652010-06-20 21:09:52 +0000311 if (pntr) {
312 usgn = false;
313 poly = false;
Nate Begemanb0a4e452010-06-07 16:00:37 +0000314 type = 'v';
Nate Begemanc4a1b652010-06-20 21:09:52 +0000315 }
Bob Wilson95148ad2010-12-01 19:49:56 +0000316 // Treat half-float ('h') types as unsigned short ('s') types.
Nate Begeman92f98af2010-06-04 07:11:25 +0000317 if (type == 'h') {
318 type = 's';
319 usgn = true;
320 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000321 usgn = usgn | poly | ((ck == ClassI || ck == ClassW) && scal && type != 'f');
Nate Begeman92f98af2010-06-04 07:11:25 +0000322
323 if (scal) {
324 SmallString<128> s;
325
326 if (usgn)
327 s.push_back('U');
Nate Begeman7c21f742010-06-04 21:36:00 +0000328
Bob Wilson95148ad2010-12-01 19:49:56 +0000329 if (type == 'l') // 64-bit long
Nate Begeman7c21f742010-06-04 21:36:00 +0000330 s += "LLi";
331 else
332 s.push_back(type);
333
Nate Begeman92f98af2010-06-04 07:11:25 +0000334 if (cnst)
335 s.push_back('C');
336 if (pntr)
337 s.push_back('*');
338 return s.str();
339 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000340
341 // Since the return value must be one type, return a vector type of the
Nate Begemanc4a1b652010-06-20 21:09:52 +0000342 // appropriate width which we will bitcast. An exception is made for
343 // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
344 // fashion, storing them to a pointer arg.
Nate Begeman7c21f742010-06-04 21:36:00 +0000345 if (ret) {
Bob Wilson5b7fe592010-12-01 19:49:51 +0000346 if (mod >= '2' && mod <= '4')
Bob Wilson95148ad2010-12-01 19:49:56 +0000347 return "vv*"; // void result with void* first argument
Nate Begemanf50551e2010-06-09 18:02:26 +0000348 if (mod == 'f' || (ck != ClassB && type == 'f'))
Nate Begeman56387832010-06-08 06:01:16 +0000349 return quad ? "V4f" : "V2f";
Nate Begemanf50551e2010-06-09 18:02:26 +0000350 if (ck != ClassB && type == 's')
Nate Begeman007afe42010-06-09 05:11:55 +0000351 return quad ? "V8s" : "V4s";
Nate Begemanf50551e2010-06-09 18:02:26 +0000352 if (ck != ClassB && type == 'i')
Nate Begeman56387832010-06-08 06:01:16 +0000353 return quad ? "V4i" : "V2i";
Nate Begemanf50551e2010-06-09 18:02:26 +0000354 if (ck != ClassB && type == 'l')
Nate Begeman007afe42010-06-09 05:11:55 +0000355 return quad ? "V2LLi" : "V1LLi";
Nate Begeman900f4672010-06-08 00:14:42 +0000356
Nate Begeman7c21f742010-06-04 21:36:00 +0000357 return quad ? "V16c" : "V8c";
358 }
359
360 // Non-return array types are passed as individual vectors.
Nate Begeman92f98af2010-06-04 07:11:25 +0000361 if (mod == '2')
362 return quad ? "V16cV16c" : "V8cV8c";
363 if (mod == '3')
364 return quad ? "V16cV16cV16c" : "V8cV8cV8c";
365 if (mod == '4')
366 return quad ? "V16cV16cV16cV16c" : "V8cV8cV8cV8c";
367
Nate Begemanf50551e2010-06-09 18:02:26 +0000368 if (mod == 'f' || (ck != ClassB && type == 'f'))
Nate Begeman007afe42010-06-09 05:11:55 +0000369 return quad ? "V4f" : "V2f";
Nate Begemanf50551e2010-06-09 18:02:26 +0000370 if (ck != ClassB && type == 's')
Nate Begeman007afe42010-06-09 05:11:55 +0000371 return quad ? "V8s" : "V4s";
Nate Begemanf50551e2010-06-09 18:02:26 +0000372 if (ck != ClassB && type == 'i')
Nate Begeman007afe42010-06-09 05:11:55 +0000373 return quad ? "V4i" : "V2i";
Nate Begemanf50551e2010-06-09 18:02:26 +0000374 if (ck != ClassB && type == 'l')
Nate Begeman007afe42010-06-09 05:11:55 +0000375 return quad ? "V2LLi" : "V1LLi";
376
Nate Begeman92f98af2010-06-04 07:11:25 +0000377 return quad ? "V16c" : "V8c";
378}
379
Nate Begemand72c9002010-06-13 04:47:03 +0000380/// MangleName - Append a type or width suffix to a base neon function name,
381/// and insert a 'q' in the appropriate location if the operation works on
382/// 128b rather than 64b. E.g. turn "vst2_lane" into "vst2q_lane_f32", etc.
Nate Begemana8979a02010-06-04 00:21:41 +0000383static std::string MangleName(const std::string &name, StringRef typestr,
384 ClassKind ck) {
Nate Begeman900f4672010-06-08 00:14:42 +0000385 if (name == "vcvt_f32_f16")
386 return name;
387
Nate Begemanaf905ef2010-06-02 06:17:19 +0000388 bool quad = false;
389 bool poly = false;
390 bool usgn = false;
391 char type = ClassifyType(typestr, quad, poly, usgn);
392
393 std::string s = name;
394
395 switch (type) {
Nate Begemana8979a02010-06-04 00:21:41 +0000396 case 'c':
397 switch (ck) {
398 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
399 case ClassI: s += "_i8"; break;
400 case ClassW: s += "_8"; break;
401 default: break;
402 }
403 break;
404 case 's':
405 switch (ck) {
406 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
407 case ClassI: s += "_i16"; break;
408 case ClassW: s += "_16"; break;
409 default: break;
410 }
411 break;
412 case 'i':
413 switch (ck) {
414 case ClassS: s += usgn ? "_u32" : "_s32"; break;
415 case ClassI: s += "_i32"; break;
416 case ClassW: s += "_32"; break;
417 default: break;
418 }
419 break;
420 case 'l':
421 switch (ck) {
422 case ClassS: s += usgn ? "_u64" : "_s64"; break;
423 case ClassI: s += "_i64"; break;
424 case ClassW: s += "_64"; break;
425 default: break;
426 }
427 break;
428 case 'h':
429 switch (ck) {
430 case ClassS:
431 case ClassI: s += "_f16"; break;
432 case ClassW: s += "_16"; break;
433 default: break;
434 }
435 break;
436 case 'f':
437 switch (ck) {
438 case ClassS:
439 case ClassI: s += "_f32"; break;
440 case ClassW: s += "_32"; break;
441 default: break;
442 }
443 break;
444 default:
445 throw "unhandled type!";
446 break;
Nate Begemanaf905ef2010-06-02 06:17:19 +0000447 }
Nate Begemana8979a02010-06-04 00:21:41 +0000448 if (ck == ClassB)
Nate Begeman92f98af2010-06-04 07:11:25 +0000449 s += "_v";
Nate Begemana8979a02010-06-04 00:21:41 +0000450
Nate Begemanaf905ef2010-06-02 06:17:19 +0000451 // Insert a 'q' before the first '_' character so that it ends up before
452 // _lane or _n on vector-scalar operations.
453 if (quad) {
454 size_t pos = s.find('_');
455 s = s.insert(pos, "q");
456 }
457 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000458}
459
Nate Begemanaf905ef2010-06-02 06:17:19 +0000460// Generate the string "(argtype a, argtype b, ...)"
Nate Begeman22237772010-06-02 00:34:55 +0000461static std::string GenArgs(const std::string &proto, StringRef typestr) {
Nate Begeman6c060db2010-06-09 01:09:00 +0000462 bool define = proto.find('i') != std::string::npos;
Nate Begemanaf905ef2010-06-02 06:17:19 +0000463 char arg = 'a';
464
465 std::string s;
466 s += "(";
467
468 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
Nate Begeman6c060db2010-06-09 01:09:00 +0000469 if (!define) {
470 s += TypeString(proto[i], typestr);
471 s.push_back(' ');
472 }
Nate Begemanaf905ef2010-06-02 06:17:19 +0000473 s.push_back(arg);
474 if ((i + 1) < e)
475 s += ", ";
476 }
477
478 s += ")";
479 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000480}
481
Nate Begemancc3c41a2010-06-12 03:09:49 +0000482static std::string Duplicate(unsigned nElts, StringRef typestr,
483 const std::string &a) {
Nate Begeman4b425a82010-06-10 00:16:56 +0000484 std::string s;
485
Bob Wilson6904d7a2010-11-16 19:39:14 +0000486 s = "(" + TypeString('d', typestr) + "){ ";
Nate Begeman4b425a82010-06-10 00:16:56 +0000487 for (unsigned i = 0; i != nElts; ++i) {
488 s += a;
489 if ((i + 1) < nElts)
490 s += ", ";
491 }
492 s += " }";
493
494 return s;
495}
496
Bob Wilsonb308b622010-11-16 23:57:01 +0000497static unsigned GetNumElements(StringRef typestr, bool &quad) {
498 quad = false;
499 bool dummy = false;
Nate Begemancc3c41a2010-06-12 03:09:49 +0000500 char type = ClassifyType(typestr, quad, dummy, dummy);
501 unsigned nElts = 0;
502 switch (type) {
Bob Wilsonb308b622010-11-16 23:57:01 +0000503 case 'c': nElts = 8; break;
504 case 's': nElts = 4; break;
505 case 'i': nElts = 2; break;
506 case 'l': nElts = 1; break;
507 case 'h': nElts = 4; break;
508 case 'f': nElts = 2; break;
509 default:
510 throw "unhandled type!";
511 break;
Nate Begemancc3c41a2010-06-12 03:09:49 +0000512 }
Bob Wilsonb308b622010-11-16 23:57:01 +0000513 if (quad) nElts <<= 1;
514 return nElts;
515}
516
517// Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
518static std::string GenOpString(OpKind op, const std::string &proto,
519 StringRef typestr) {
520 bool quad;
521 unsigned nElts = GetNumElements(typestr, quad);
Nate Begemancc3c41a2010-06-12 03:09:49 +0000522
Nate Begeman4b425a82010-06-10 00:16:56 +0000523 std::string ts = TypeString(proto[0], typestr);
Bob Wilsonee9ca072010-09-15 01:52:33 +0000524 std::string s;
525 if (op == OpHi || op == OpLo) {
526 s = "union { " + ts + " r; double d; } u; u.d";
527 } else {
528 s = ts + " r; r";
Bob Wilsonee9ca072010-09-15 01:52:33 +0000529 }
Nate Begeman900f4672010-06-08 00:14:42 +0000530
531 s += " = ";
532
Nate Begeman162d3ba2010-06-03 04:04:09 +0000533 switch(op) {
534 case OpAdd:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000535 s += "a + b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000536 break;
537 case OpSub:
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 OpMulN:
Bob Wilsonb308b622010-11-16 23:57:01 +0000541 s += "a * " + Duplicate(nElts, typestr, "b");
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000542 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000543 case OpMul:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000544 s += "a * b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000545 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000546 case OpMlaN:
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 OpMla:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000550 s += "a + (b * c)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000551 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000552 case OpMlsN:
Bob Wilsonb308b622010-11-16 23:57:01 +0000553 s += "a - (b * " + Duplicate(nElts, typestr, "c") + ")";
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000554 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000555 case OpMls:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000556 s += "a - (b * c)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000557 break;
558 case OpEq:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000559 s += "(" + ts + ")(a == b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000560 break;
561 case OpGe:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000562 s += "(" + ts + ")(a >= b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000563 break;
564 case OpLe:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000565 s += "(" + ts + ")(a <= b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000566 break;
567 case OpGt:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000568 s += "(" + ts + ")(a > b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000569 break;
570 case OpLt:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000571 s += "(" + ts + ")(a < b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000572 break;
573 case OpNeg:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000574 s += " -a";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000575 break;
576 case OpNot:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000577 s += " ~a";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000578 break;
579 case OpAnd:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000580 s += "a & b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000581 break;
582 case OpOr:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000583 s += "a | b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000584 break;
585 case OpXor:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000586 s += "a ^ b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000587 break;
588 case OpAndNot:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000589 s += "a & ~b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000590 break;
591 case OpOrNot:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000592 s += "a | ~b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000593 break;
Nate Begeman3861e742010-06-03 21:35:22 +0000594 case OpCast:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000595 s += "(" + ts + ")a";
Nate Begeman3861e742010-06-03 21:35:22 +0000596 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000597 case OpConcat:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000598 s += "__builtin_shufflevector((int64x1_t)a";
599 s += ", (int64x1_t)b, 0, 1)";
Nate Begeman900f4672010-06-08 00:14:42 +0000600 break;
Nate Begeman6c060db2010-06-09 01:09:00 +0000601 case OpHi:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000602 s += "(((float64x2_t)a)[1])";
Nate Begeman6c060db2010-06-09 01:09:00 +0000603 break;
604 case OpLo:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000605 s += "(((float64x2_t)a)[0])";
Nate Begeman6c060db2010-06-09 01:09:00 +0000606 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000607 case OpDup:
Bob Wilsonb308b622010-11-16 23:57:01 +0000608 s += Duplicate(nElts, typestr, "a");
Nate Begemancc3c41a2010-06-12 03:09:49 +0000609 break;
610 case OpSelect:
611 // ((0 & 1) | (~0 & 2))
612 ts = TypeString(proto[1], typestr);
Bob Wilson6904d7a2010-11-16 19:39:14 +0000613 s += "(a & (" + ts + ")b) | ";
614 s += "(~a & (" + ts + ")c)";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000615 break;
616 case OpRev16:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000617 s += "__builtin_shufflevector(a, a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000618 for (unsigned i = 2; i <= nElts; i += 2)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000619 for (unsigned j = 0; j != 2; ++j)
620 s += ", " + utostr(i - j - 1);
621 s += ")";
622 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000623 case OpRev32: {
624 unsigned WordElts = nElts >> (1 + (int)quad);
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000625 s += "__builtin_shufflevector(a, a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000626 for (unsigned i = WordElts; i <= nElts; i += WordElts)
627 for (unsigned j = 0; j != WordElts; ++j)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000628 s += ", " + utostr(i - j - 1);
629 s += ")";
630 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000631 }
632 case OpRev64: {
633 unsigned DblWordElts = nElts >> (int)quad;
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000634 s += "__builtin_shufflevector(a, a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000635 for (unsigned i = DblWordElts; i <= nElts; i += DblWordElts)
636 for (unsigned j = 0; j != DblWordElts; ++j)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000637 s += ", " + utostr(i - j - 1);
638 s += ")";
Nate Begeman900f4672010-06-08 00:14:42 +0000639 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000640 }
Nate Begeman162d3ba2010-06-03 04:04:09 +0000641 default:
642 throw "unknown OpKind!";
643 break;
644 }
Bob Wilsonee9ca072010-09-15 01:52:33 +0000645 if (op == OpHi || op == OpLo)
646 s += "; return u.r;";
647 else
648 s += "; return r;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000649 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000650}
651
Nate Begemanb0a4e452010-06-07 16:00:37 +0000652static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
653 unsigned mod = proto[0];
654 unsigned ret = 0;
655
Nate Begeman900f4672010-06-08 00:14:42 +0000656 if (mod == 'v' || mod == 'f')
Nate Begemanb0a4e452010-06-07 16:00:37 +0000657 mod = proto[1];
658
659 bool quad = false;
660 bool poly = false;
661 bool usgn = false;
662 bool scal = false;
663 bool cnst = false;
664 bool pntr = false;
665
Nate Begeman59d70cb2010-08-06 01:24:11 +0000666 // Base type to get the type string for.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000667 char type = ClassifyType(typestr, quad, poly, usgn);
668
669 // Based on the modifying character, change the type and width if necessary.
670 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
Nate Begeman59d70cb2010-08-06 01:24:11 +0000671
Nate Begemanb0a4e452010-06-07 16:00:37 +0000672 if (usgn)
673 ret |= 0x08;
Nate Begeman59d70cb2010-08-06 01:24:11 +0000674 if (quad && proto[1] != 'g')
Nate Begemanb0a4e452010-06-07 16:00:37 +0000675 ret |= 0x10;
676
677 switch (type) {
678 case 'c':
679 ret |= poly ? 5 : 0;
680 break;
681 case 's':
682 ret |= poly ? 6 : 1;
683 break;
684 case 'i':
685 ret |= 2;
686 break;
687 case 'l':
688 ret |= 3;
689 break;
690 case 'h':
691 ret |= 7;
692 break;
693 case 'f':
694 ret |= 4;
695 break;
696 default:
697 throw "unhandled type!";
698 break;
699 }
700 return ret;
701}
702
Nate Begeman7c8c8832010-06-02 21:53:00 +0000703// Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
Nate Begeman7c8c8832010-06-02 21:53:00 +0000704static std::string GenBuiltin(const std::string &name, const std::string &proto,
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000705 StringRef typestr, ClassKind ck) {
Nate Begeman7c8c8832010-06-02 21:53:00 +0000706 char arg = 'a';
Nate Begeman162d3ba2010-06-03 04:04:09 +0000707 std::string s;
Nate Begeman9e584b32010-06-04 22:53:30 +0000708
Nate Begemanc4a1b652010-06-20 21:09:52 +0000709 // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
710 // sret-like argument.
Bob Wilson5b7fe592010-12-01 19:49:51 +0000711 bool sret = (proto[0] >= '2' && proto[0] <= '4');
Nate Begemanc4a1b652010-06-20 21:09:52 +0000712
713 // If this builtin takes an immediate argument, we need to #define it rather
714 // than use a standard declaration, so that SemaChecking can range check
715 // the immediate passed by the user.
Nate Begeman6c060db2010-06-09 01:09:00 +0000716 bool define = proto.find('i') != std::string::npos;
Nate Begeman9e584b32010-06-04 22:53:30 +0000717
Bob Wilson95148ad2010-12-01 19:49:56 +0000718 // Check if the prototype has a scalar operand with the type of the vector
719 // elements. If not, bitcasting the args will take care of arg checking.
720 // The actual signedness etc. will be taken care of with special enums.
Nate Begeman9e584b32010-06-04 22:53:30 +0000721 if (proto.find('s') == std::string::npos)
722 ck = ClassB;
Nate Begeman7c21f742010-06-04 21:36:00 +0000723
Nate Begeman162d3ba2010-06-03 04:04:09 +0000724 if (proto[0] != 'v') {
Nate Begeman6c060db2010-06-09 01:09:00 +0000725 std::string ts = TypeString(proto[0], typestr);
726
727 if (define) {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000728 if (sret)
729 s += "({ " + ts + " r; ";
Bob Wilsone106bc12010-12-02 00:24:56 +0000730 else
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000731 s += "(" + ts + ")";
Nate Begemanc4a1b652010-06-20 21:09:52 +0000732 } else if (sret) {
733 s += ts + " r; ";
Nate Begeman9e584b32010-06-04 22:53:30 +0000734 } else {
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000735 s += ts + " r; r = ";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000736 }
Nate Begeman4b425a82010-06-10 00:16:56 +0000737 }
738
739 bool splat = proto.find('a') != std::string::npos;
Nate Begeman7c8c8832010-06-02 21:53:00 +0000740
741 s += "__builtin_neon_";
Nate Begeman4b425a82010-06-10 00:16:56 +0000742 if (splat) {
Bob Wilson95148ad2010-12-01 19:49:56 +0000743 // Call the non-splat builtin: chop off the "_n" suffix from the name.
Nate Begeman4b425a82010-06-10 00:16:56 +0000744 std::string vname(name, 0, name.size()-2);
745 s += MangleName(vname, typestr, ck);
746 } else {
747 s += MangleName(name, typestr, ck);
748 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000749 s += "(";
Nate Begemanc4a1b652010-06-20 21:09:52 +0000750
751 // Pass the address of the return variable as the first argument to sret-like
752 // builtins.
753 if (sret)
754 s += "&r, ";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000755
756 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
Nate Begemancc3c41a2010-06-12 03:09:49 +0000757 std::string args = std::string(&arg, 1);
Bob Wilson95148ad2010-12-01 19:49:56 +0000758
759 // Wrap macro arguments in parenthesis.
Nate Begemancc3c41a2010-06-12 03:09:49 +0000760 if (define)
761 args = "(" + args + ")";
Bob Wilsonf00140c2010-12-01 19:49:58 +0000762
763 bool argQuad = false;
764 bool argPoly = false;
765 bool argUsgn = false;
766 bool argScalar = false;
767 bool dummy = false;
768 char argType = ClassifyType(typestr, argQuad, argPoly, argUsgn);
769 argType = ModType(proto[i], argType, argQuad, argPoly, argUsgn, argScalar,
770 dummy, dummy);
771
Nate Begeman9e584b32010-06-04 22:53:30 +0000772 // Handle multiple-vector values specially, emitting each subvector as an
773 // argument to the __builtin.
Bob Wilson5b7fe592010-12-01 19:49:51 +0000774 if (proto[i] >= '2' && proto[i] <= '4') {
Bob Wilsonf00140c2010-12-01 19:49:58 +0000775 // Check if an explicit cast is needed.
776 if (argType != 'c' || argPoly || argUsgn)
777 args = (argQuad ? "(int8x16_t)" : "(int8x8_t)") + args;
778
Nate Begeman9e584b32010-06-04 22:53:30 +0000779 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000780 s += args + ".val[" + utostr(vi) + "]";
Nate Begeman9e584b32010-06-04 22:53:30 +0000781 if ((vi + 1) < ve)
782 s += ", ";
783 }
784 if ((i + 1) < e)
785 s += ", ";
786
787 continue;
788 }
789
Bob Wilsonf00140c2010-12-01 19:49:58 +0000790 // Check if an explicit cast is needed.
791 if (!argScalar &&
792 ((ck == ClassB && argType != 'c') || argPoly || argUsgn)) {
793 std::string argTypeStr = "c";
794 if (ck != ClassB)
795 argTypeStr = argType;
796 if (argQuad)
797 argTypeStr = "Q" + argTypeStr;
798 args = "(" + TypeString('d', argTypeStr) + ")" + args;
799 }
800
801 if (splat && (i + 1) == e)
802 s += Duplicate(GetNumElements(typestr, argQuad), typestr, args);
Nate Begeman4b425a82010-06-10 00:16:56 +0000803 else
Nate Begemancc3c41a2010-06-12 03:09:49 +0000804 s += args;
Nate Begeman7c8c8832010-06-02 21:53:00 +0000805 if ((i + 1) < e)
806 s += ", ";
807 }
808
Nate Begeman9e584b32010-06-04 22:53:30 +0000809 // Extra constant integer to hold type class enum for this function, e.g. s8
Nate Begeman9e584b32010-06-04 22:53:30 +0000810 if (ck == ClassB)
Nate Begemanb0a4e452010-06-07 16:00:37 +0000811 s += ", " + utostr(GetNeonEnum(proto, typestr));
Nate Begeman9e584b32010-06-04 22:53:30 +0000812
Nate Begeman6c060db2010-06-09 01:09:00 +0000813 if (define)
814 s += ")";
815 else
816 s += ");";
Nate Begeman9e584b32010-06-04 22:53:30 +0000817
818 if (proto[0] != 'v') {
Nate Begeman6c060db2010-06-09 01:09:00 +0000819 if (define) {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000820 if (sret)
821 s += "; r; })";
Nate Begeman6c060db2010-06-09 01:09:00 +0000822 } else {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000823 s += " return r;";
Nate Begeman6c060db2010-06-09 01:09:00 +0000824 }
Nate Begeman9e584b32010-06-04 22:53:30 +0000825 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000826 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000827}
828
Nate Begeman73cef3e2010-06-04 01:26:15 +0000829static std::string GenBuiltinDef(const std::string &name,
830 const std::string &proto,
831 StringRef typestr, ClassKind ck) {
832 std::string s("BUILTIN(__builtin_neon_");
Nate Begeman92f98af2010-06-04 07:11:25 +0000833
834 // If all types are the same size, bitcasting the args will take care
835 // of arg checking. The actual signedness etc. will be taken care of with
836 // special enums.
837 if (proto.find('s') == std::string::npos)
838 ck = ClassB;
839
Nate Begeman73cef3e2010-06-04 01:26:15 +0000840 s += MangleName(name, typestr, ck);
841 s += ", \"";
842
Nate Begeman92f98af2010-06-04 07:11:25 +0000843 for (unsigned i = 0, e = proto.size(); i != e; ++i)
Nate Begeman7c21f742010-06-04 21:36:00 +0000844 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
845
846 // Extra constant integer to hold type class enum for this function, e.g. s8
847 if (ck == ClassB)
848 s += "i";
Nate Begeman73cef3e2010-06-04 01:26:15 +0000849
850 s += "\", \"n\")";
851 return s;
852}
853
Nate Begemand72c9002010-06-13 04:47:03 +0000854/// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h
855/// is comprised of type definitions and function declarations.
Nate Begeman5ddb0872010-05-28 01:08:32 +0000856void NeonEmitter::run(raw_ostream &OS) {
857 EmitSourceFileHeader("ARM NEON Header", OS);
858
859 // FIXME: emit license into file?
860
861 OS << "#ifndef __ARM_NEON_H\n";
862 OS << "#define __ARM_NEON_H\n\n";
863
864 OS << "#ifndef __ARM_NEON__\n";
865 OS << "#error \"NEON support not enabled\"\n";
866 OS << "#endif\n\n";
867
868 OS << "#include <stdint.h>\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000869
870 // Emit NEON-specific scalar typedefs.
Nate Begeman7c8c8832010-06-02 21:53:00 +0000871 OS << "typedef float float32_t;\n";
Bob Wilson4fbf6382010-11-16 23:57:03 +0000872 OS << "typedef int8_t poly8_t;\n";
873 OS << "typedef int16_t poly16_t;\n";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000874 OS << "typedef uint16_t float16_t;\n";
Nate Begeman9e584b32010-06-04 22:53:30 +0000875
Nate Begeman7c8c8832010-06-02 21:53:00 +0000876 // Emit Neon vector typedefs.
877 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
878 SmallVector<StringRef, 24> TDTypeVec;
879 ParseTypes(0, TypedefTypes, TDTypeVec);
880
881 // Emit vector typedefs.
Bob Wilson6904d7a2010-11-16 19:39:14 +0000882 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
Bob Wilson6a8aceb2010-11-16 23:57:06 +0000883 bool dummy, quad = false, poly = false;
884 (void) ClassifyType(TDTypeVec[i], quad, poly, dummy);
885 if (poly)
886 OS << "typedef __attribute__((neon_polyvector_type(";
887 else
888 OS << "typedef __attribute__((neon_vector_type(";
Nate Begeman9e584b32010-06-04 22:53:30 +0000889
Bob Wilson6a8aceb2010-11-16 23:57:06 +0000890 unsigned nElts = GetNumElements(TDTypeVec[i], quad);
891 OS << utostr(nElts) << "))) ";
892 if (nElts < 10)
Bob Wilson6904d7a2010-11-16 19:39:14 +0000893 OS << " ";
Nate Begeman9e584b32010-06-04 22:53:30 +0000894
Bob Wilson6904d7a2010-11-16 19:39:14 +0000895 OS << TypeString('s', TDTypeVec[i]);
896 OS << " " << TypeString('d', TDTypeVec[i]) << ";\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000897 }
898 OS << "\n";
Bob Wilson6a8aceb2010-11-16 23:57:06 +0000899 OS << "typedef __attribute__((__vector_size__(8))) "
Bob Wilson6904d7a2010-11-16 19:39:14 +0000900 "double float64x1_t;\n";
Bob Wilson6a8aceb2010-11-16 23:57:06 +0000901 OS << "typedef __attribute__((__vector_size__(16))) "
Bob Wilson6904d7a2010-11-16 19:39:14 +0000902 "double float64x2_t;\n";
Bob Wilsonee9ca072010-09-15 01:52:33 +0000903 OS << "\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000904
905 // Emit struct typedefs.
Bob Wilson6904d7a2010-11-16 19:39:14 +0000906 for (unsigned vi = 2; vi != 5; ++vi) {
Nate Begeman7c8c8832010-06-02 21:53:00 +0000907 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
Bob Wilson6904d7a2010-11-16 19:39:14 +0000908 std::string ts = TypeString('d', TDTypeVec[i]);
909 std::string vs = TypeString('0' + vi, TDTypeVec[i]);
910 OS << "typedef struct " << vs << " {\n";
911 OS << " " << ts << " val";
912 OS << "[" << utostr(vi) << "]";
913 OS << ";\n} ";
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000914 OS << vs << ";\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000915 }
916 }
Nate Begeman5ddb0872010-05-28 01:08:32 +0000917
Nate Begeman7c8c8832010-06-02 21:53:00 +0000918 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
919
Nate Begeman5ddb0872010-05-28 01:08:32 +0000920 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
921
Nate Begeman22237772010-06-02 00:34:55 +0000922 // Unique the return+pattern types, and assign them.
Nate Begeman5ddb0872010-05-28 01:08:32 +0000923 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
924 Record *R = RV[i];
Nate Begeman22237772010-06-02 00:34:55 +0000925 std::string name = LowercaseString(R->getName());
926 std::string Proto = R->getValueAsString("Prototype");
Nate Begeman5ddb0872010-05-28 01:08:32 +0000927 std::string Types = R->getValueAsString("Types");
Nate Begeman22237772010-06-02 00:34:55 +0000928
929 SmallVector<StringRef, 16> TypeVec;
930 ParseTypes(R, Types, TypeVec);
931
Nate Begeman162d3ba2010-06-03 04:04:09 +0000932 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
Nate Begemane66aab52010-06-02 07:14:28 +0000933
Nate Begeman6c060db2010-06-09 01:09:00 +0000934 bool define = Proto.find('i') != std::string::npos;
935
Nate Begeman22237772010-06-02 00:34:55 +0000936 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
937 assert(!Proto.empty() && "");
938
Nate Begeman7c8c8832010-06-02 21:53:00 +0000939 // static always inline + return type
Nate Begeman6c060db2010-06-09 01:09:00 +0000940 if (define)
941 OS << "#define";
942 else
943 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
Nate Begeman22237772010-06-02 00:34:55 +0000944
Nate Begemane66aab52010-06-02 07:14:28 +0000945 // Function name with type suffix
Nate Begemana8979a02010-06-04 00:21:41 +0000946 OS << " " << MangleName(name, TypeVec[ti], ClassS);
Nate Begeman22237772010-06-02 00:34:55 +0000947
Nate Begemane66aab52010-06-02 07:14:28 +0000948 // Function arguments
949 OS << GenArgs(Proto, TypeVec[ti]);
Nate Begeman22237772010-06-02 00:34:55 +0000950
Nate Begemane66aab52010-06-02 07:14:28 +0000951 // Definition.
Nate Begeman6c060db2010-06-09 01:09:00 +0000952 if (define)
953 OS << " ";
954 else
955 OS << " { ";
Nate Begeman22237772010-06-02 00:34:55 +0000956
Nate Begemana8979a02010-06-04 00:21:41 +0000957 if (k != OpNone) {
Nate Begeman162d3ba2010-06-03 04:04:09 +0000958 OS << GenOpString(k, Proto, TypeVec[ti]);
Nate Begemana8979a02010-06-04 00:21:41 +0000959 } else {
960 if (R->getSuperClasses().size() < 2)
961 throw TGError(R->getLoc(), "Builtin has no class kind");
962
963 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
964
965 if (ck == ClassNone)
966 throw TGError(R->getLoc(), "Builtin has no class kind");
967 OS << GenBuiltin(name, Proto, TypeVec[ti], ck);
968 }
Nate Begeman6c060db2010-06-09 01:09:00 +0000969 if (!define)
970 OS << " }";
971 OS << "\n";
Nate Begeman22237772010-06-02 00:34:55 +0000972 }
973 OS << "\n";
Nate Begeman5ddb0872010-05-28 01:08:32 +0000974 }
Nate Begeman73cef3e2010-06-04 01:26:15 +0000975 OS << "#undef __ai\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000976 OS << "#endif /* __ARM_NEON_H */\n";
Nate Begeman5ddb0872010-05-28 01:08:32 +0000977}
Nate Begemana8979a02010-06-04 00:21:41 +0000978
Nate Begeman918f8e42010-06-14 05:17:23 +0000979static unsigned RangeFromType(StringRef typestr) {
980 // base type to get the type string for.
981 bool quad = false, dummy = false;
982 char type = ClassifyType(typestr, quad, dummy, dummy);
983
984 switch (type) {
985 case 'c':
Nate Begeman4da883a2010-06-15 22:10:31 +0000986 return (8 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +0000987 case 'h':
988 case 's':
Nate Begeman4da883a2010-06-15 22:10:31 +0000989 return (4 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +0000990 case 'f':
991 case 'i':
Nate Begeman4da883a2010-06-15 22:10:31 +0000992 return (2 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +0000993 case 'l':
Nate Begeman4da883a2010-06-15 22:10:31 +0000994 return (1 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +0000995 default:
996 throw "unhandled type!";
997 break;
998 }
Bob Wilsonfdb530d2010-07-28 18:21:10 +0000999 assert(0 && "unreachable");
1000 return 0;
Nate Begeman918f8e42010-06-14 05:17:23 +00001001}
1002
Nate Begemanf8c4c272010-06-17 04:15:13 +00001003/// runHeader - Emit a file with sections defining:
1004/// 1. the NEON section of BuiltinsARM.def.
1005/// 2. the SemaChecking code for the type overload checking.
1006/// 3. the SemaChecking code for validation of intrinsic immedate arguments.
Nate Begemana8979a02010-06-04 00:21:41 +00001007void NeonEmitter::runHeader(raw_ostream &OS) {
Nate Begeman73cef3e2010-06-04 01:26:15 +00001008 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1009
1010 StringMap<OpKind> EmittedMap;
1011
Nate Begemanf8c4c272010-06-17 04:15:13 +00001012 // Generate BuiltinsARM.def for NEON
1013 OS << "#ifdef GET_NEON_BUILTINS\n";
Nate Begeman73cef3e2010-06-04 01:26:15 +00001014 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1015 Record *R = RV[i];
Nate Begeman73cef3e2010-06-04 01:26:15 +00001016 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1017 if (k != OpNone)
1018 continue;
Nate Begeman73cef3e2010-06-04 01:26:15 +00001019
Nate Begemanf8c4c272010-06-17 04:15:13 +00001020 std::string Proto = R->getValueAsString("Prototype");
1021
Nate Begemand72c9002010-06-13 04:47:03 +00001022 // Functions with 'a' (the splat code) in the type prototype should not get
1023 // their own builtin as they use the non-splat variant.
Nate Begeman4b425a82010-06-10 00:16:56 +00001024 if (Proto.find('a') != std::string::npos)
1025 continue;
Nate Begemand72c9002010-06-13 04:47:03 +00001026
Nate Begemanf8c4c272010-06-17 04:15:13 +00001027 std::string Types = R->getValueAsString("Types");
Nate Begeman73cef3e2010-06-04 01:26:15 +00001028 SmallVector<StringRef, 16> TypeVec;
1029 ParseTypes(R, Types, TypeVec);
Nate Begemand72c9002010-06-13 04:47:03 +00001030
Nate Begeman73cef3e2010-06-04 01:26:15 +00001031 if (R->getSuperClasses().size() < 2)
1032 throw TGError(R->getLoc(), "Builtin has no class kind");
1033
Nate Begemanf8c4c272010-06-17 04:15:13 +00001034 std::string name = LowercaseString(R->getName());
Nate Begeman73cef3e2010-06-04 01:26:15 +00001035 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1036
1037 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
Nate Begemand72c9002010-06-13 04:47:03 +00001038 // Generate the BuiltinsARM.def declaration for this builtin, ensuring
1039 // that each unique BUILTIN() macro appears only once in the output
1040 // stream.
Nate Begeman73cef3e2010-06-04 01:26:15 +00001041 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
1042 if (EmittedMap.count(bd))
1043 continue;
1044
1045 EmittedMap[bd] = OpNone;
1046 OS << bd << "\n";
1047 }
Nate Begemanf8c4c272010-06-17 04:15:13 +00001048 }
1049 OS << "#endif\n\n";
1050
1051 // Generate the overloaded type checking code for SemaChecking.cpp
1052 OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
1053 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1054 Record *R = RV[i];
1055 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1056 if (k != OpNone)
Nate Begemand72c9002010-06-13 04:47:03 +00001057 continue;
Nate Begemanf8c4c272010-06-17 04:15:13 +00001058
1059 std::string Proto = R->getValueAsString("Prototype");
1060 std::string Types = R->getValueAsString("Types");
1061 std::string name = LowercaseString(R->getName());
1062
1063 // Functions with 'a' (the splat code) in the type prototype should not get
1064 // their own builtin as they use the non-splat variant.
1065 if (Proto.find('a') != std::string::npos)
1066 continue;
1067
1068 // Functions which have a scalar argument cannot be overloaded, no need to
1069 // check them if we are emitting the type checking code.
1070 if (Proto.find('s') != std::string::npos)
1071 continue;
1072
1073 SmallVector<StringRef, 16> TypeVec;
1074 ParseTypes(R, Types, TypeVec);
1075
1076 if (R->getSuperClasses().size() < 2)
1077 throw TGError(R->getLoc(), "Builtin has no class kind");
1078
1079 int si = -1, qi = -1;
1080 unsigned mask = 0, qmask = 0;
1081 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1082 // Generate the switch case(s) for this builtin for the type validation.
1083 bool quad = false, poly = false, usgn = false;
1084 (void) ClassifyType(TypeVec[ti], quad, poly, usgn);
1085
1086 if (quad) {
1087 qi = ti;
1088 qmask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1089 } else {
1090 si = ti;
1091 mask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1092 }
1093 }
1094 if (mask)
1095 OS << "case ARM::BI__builtin_neon_"
1096 << MangleName(name, TypeVec[si], ClassB)
1097 << ": mask = " << "0x" << utohexstr(mask) << "; break;\n";
1098 if (qmask)
1099 OS << "case ARM::BI__builtin_neon_"
1100 << MangleName(name, TypeVec[qi], ClassB)
1101 << ": mask = " << "0x" << utohexstr(qmask) << "; break;\n";
1102 }
1103 OS << "#endif\n\n";
1104
1105 // Generate the intrinsic range checking code for shift/lane immediates.
1106 OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
1107 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1108 Record *R = RV[i];
1109
1110 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1111 if (k != OpNone)
1112 continue;
1113
1114 std::string name = LowercaseString(R->getName());
1115 std::string Proto = R->getValueAsString("Prototype");
1116 std::string Types = R->getValueAsString("Types");
1117
1118 // Functions with 'a' (the splat code) in the type prototype should not get
1119 // their own builtin as they use the non-splat variant.
1120 if (Proto.find('a') != std::string::npos)
1121 continue;
1122
1123 // Functions which do not have an immediate do not need to have range
1124 // checking code emitted.
1125 if (Proto.find('i') == std::string::npos)
1126 continue;
1127
1128 SmallVector<StringRef, 16> TypeVec;
1129 ParseTypes(R, Types, TypeVec);
1130
1131 if (R->getSuperClasses().size() < 2)
1132 throw TGError(R->getLoc(), "Builtin has no class kind");
1133
1134 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1135
1136 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1137 std::string namestr, shiftstr, rangestr;
1138
1139 // Builtins which are overloaded by type will need to have their upper
1140 // bound computed at Sema time based on the type constant.
1141 if (Proto.find('s') == std::string::npos) {
1142 ck = ClassB;
1143 if (R->getValueAsBit("isShift")) {
1144 shiftstr = ", true";
1145
1146 // Right shifts have an 'r' in the name, left shifts do not.
1147 if (name.find('r') != std::string::npos)
1148 rangestr = "l = 1; ";
1149 }
1150 rangestr += "u = RFT(TV" + shiftstr + ")";
1151 } else {
1152 rangestr = "u = " + utostr(RangeFromType(TypeVec[ti]));
1153 }
Nate Begemanc4a1b652010-06-20 21:09:52 +00001154 // Make sure cases appear only once by uniquing them in a string map.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001155 namestr = MangleName(name, TypeVec[ti], ck);
1156 if (EmittedMap.count(namestr))
1157 continue;
1158 EmittedMap[namestr] = OpNone;
Nate Begemanc4a1b652010-06-20 21:09:52 +00001159
1160 // Calculate the index of the immediate that should be range checked.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001161 unsigned immidx = 0;
Nate Begemanc4a1b652010-06-20 21:09:52 +00001162
1163 // Builtins that return a struct of multiple vectors have an extra
1164 // leading arg for the struct return.
Bob Wilson5b7fe592010-12-01 19:49:51 +00001165 if (Proto[0] >= '2' && Proto[0] <= '4')
Nate Begemanc4a1b652010-06-20 21:09:52 +00001166 ++immidx;
1167
1168 // Add one to the index for each argument until we reach the immediate
1169 // to be checked. Structs of vectors are passed as multiple arguments.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001170 for (unsigned ii = 1, ie = Proto.size(); ii != ie; ++ii) {
1171 switch (Proto[ii]) {
1172 default: immidx += 1; break;
1173 case '2': immidx += 2; break;
1174 case '3': immidx += 3; break;
1175 case '4': immidx += 4; break;
1176 case 'i': ie = ii + 1; break;
1177 }
1178 }
1179 OS << "case ARM::BI__builtin_neon_" << MangleName(name, TypeVec[ti], ck)
1180 << ": i = " << immidx << "; " << rangestr << "; break;\n";
Nate Begemand72c9002010-06-13 04:47:03 +00001181 }
Nate Begeman73cef3e2010-06-04 01:26:15 +00001182 }
Nate Begemanf8c4c272010-06-17 04:15:13 +00001183 OS << "#endif\n\n";
Nate Begemana8979a02010-06-04 00:21:41 +00001184}