blob: bf5c1753356c6553a33299502e9d0ab4270adacb [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 Begeman3861e742010-06-03 21:35:22 +000092 default: throw "unhandled type in widen!";
93 }
94 return '\0';
95}
96
Nate Begemand72c9002010-06-13 04:47:03 +000097/// For a particular StringRef, return the base type code, and whether it has
98/// the quad-vector, polynomial, or unsigned modifiers set.
Nate Begemanaf905ef2010-06-02 06:17:19 +000099static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) {
Nate Begeman22237772010-06-02 00:34:55 +0000100 unsigned off = 0;
101
Nate Begemanaf905ef2010-06-02 06:17:19 +0000102 // remember quad.
103 if (ty[off] == 'Q') {
104 quad = true;
105 ++off;
106 }
107
108 // remember poly.
109 if (ty[off] == 'P') {
110 poly = true;
111 ++off;
112 }
113
114 // remember unsigned.
115 if (ty[off] == 'U') {
116 usgn = true;
117 ++off;
118 }
119
120 // base type to get the type string for.
121 return ty[off];
122}
123
Nate Begemand72c9002010-06-13 04:47:03 +0000124/// ModType - Transform a type code and its modifiers based on a mod code. The
125/// mod code definitions may be found at the top of arm_neon.td.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000126static char ModType(const char mod, char type, bool &quad, bool &poly,
127 bool &usgn, bool &scal, bool &cnst, bool &pntr) {
Nate Begeman22237772010-06-02 00:34:55 +0000128 switch (mod) {
Nate Begeman22237772010-06-02 00:34:55 +0000129 case 't':
130 if (poly) {
131 poly = false;
132 usgn = true;
133 }
134 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000135 case 'u':
Nate Begeman22237772010-06-02 00:34:55 +0000136 usgn = true;
Nate Begeman900f4672010-06-08 00:14:42 +0000137 case 'x':
Nate Begeman162d3ba2010-06-03 04:04:09 +0000138 poly = false;
Nate Begeman22237772010-06-02 00:34:55 +0000139 if (type == 'f')
140 type = 'i';
141 break;
142 case 'f':
Nate Begeman900f4672010-06-08 00:14:42 +0000143 if (type == 'h')
144 quad = true;
Nate Begeman22237772010-06-02 00:34:55 +0000145 type = 'f';
Nate Begeman162d3ba2010-06-03 04:04:09 +0000146 usgn = false;
Nate Begeman22237772010-06-02 00:34:55 +0000147 break;
148 case 'w':
149 type = Widen(type);
150 quad = true;
151 break;
152 case 'n':
153 type = Widen(type);
154 break;
Nate Begeman22237772010-06-02 00:34:55 +0000155 case 'l':
156 type = 'l';
157 scal = true;
158 usgn = true;
159 break;
160 case 's':
Nate Begeman4b425a82010-06-10 00:16:56 +0000161 case 'a':
Nate Begeman22237772010-06-02 00:34:55 +0000162 scal = true;
163 break;
164 case 'k':
165 quad = true;
166 break;
167 case 'c':
168 cnst = true;
169 case 'p':
Nate Begemanb0a4e452010-06-07 16:00:37 +0000170 usgn = false;
171 poly = false;
Nate Begeman22237772010-06-02 00:34:55 +0000172 pntr = true;
173 scal = true;
174 break;
Nate Begeman3861e742010-06-03 21:35:22 +0000175 case 'h':
176 type = Narrow(type);
Nate Begeman900f4672010-06-08 00:14:42 +0000177 if (type == 'h')
178 quad = false;
Nate Begeman3861e742010-06-03 21:35:22 +0000179 break;
180 case 'e':
181 type = Narrow(type);
182 usgn = true;
183 break;
Nate Begeman22237772010-06-02 00:34:55 +0000184 default:
185 break;
186 }
Nate Begemanb0a4e452010-06-07 16:00:37 +0000187 return type;
188}
189
Nate Begemand72c9002010-06-13 04:47:03 +0000190/// TypeString - for a modifier and type, generate the name of the typedef for
191/// that type. If generic is true, emit the generic vector type rather than
192/// the public NEON type. QUc -> uint8x8t_t / __neon_uint8x8_t.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000193static std::string TypeString(const char mod, StringRef typestr,
Nate Begemand72c9002010-06-13 04:47:03 +0000194 bool generic = false) {
Nate Begemanb0a4e452010-06-07 16:00:37 +0000195 bool quad = false;
196 bool poly = false;
197 bool usgn = false;
198 bool scal = false;
199 bool cnst = false;
200 bool pntr = false;
201
202 if (mod == 'v')
203 return "void";
204 if (mod == 'i')
205 return "int";
206
207 // base type to get the type string for.
208 char type = ClassifyType(typestr, quad, poly, usgn);
209
210 // Based on the modifying character, change the type and width if necessary.
211 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
Nate Begeman22237772010-06-02 00:34:55 +0000212
213 SmallString<128> s;
214
Nate Begemand72c9002010-06-13 04:47:03 +0000215 if (generic)
Nate Begeman9e584b32010-06-04 22:53:30 +0000216 s += "__neon_";
217
Nate Begeman22237772010-06-02 00:34:55 +0000218 if (usgn)
219 s.push_back('u');
220
221 switch (type) {
222 case 'c':
223 s += poly ? "poly8" : "int8";
224 if (scal)
225 break;
226 s += quad ? "x16" : "x8";
227 break;
228 case 's':
229 s += poly ? "poly16" : "int16";
230 if (scal)
231 break;
232 s += quad ? "x8" : "x4";
233 break;
234 case 'i':
235 s += "int32";
236 if (scal)
237 break;
238 s += quad ? "x4" : "x2";
239 break;
240 case 'l':
241 s += "int64";
242 if (scal)
243 break;
244 s += quad ? "x2" : "x1";
245 break;
246 case 'h':
247 s += "float16";
248 if (scal)
249 break;
250 s += quad ? "x8" : "x4";
251 break;
252 case 'f':
253 s += "float32";
254 if (scal)
255 break;
256 s += quad ? "x4" : "x2";
257 break;
Nate Begeman22237772010-06-02 00:34:55 +0000258 default:
259 throw "unhandled type!";
260 break;
261 }
262
263 if (mod == '2')
264 s += "x2";
265 if (mod == '3')
266 s += "x3";
267 if (mod == '4')
268 s += "x4";
269
270 // Append _t, finishing the type string typedef type.
271 s += "_t";
272
273 if (cnst)
274 s += " const";
275
276 if (pntr)
277 s += " *";
278
279 return s.str();
280}
281
Nate Begemand72c9002010-06-13 04:47:03 +0000282/// TypeString - for a modifier and type, generate the clang BuiltinsARM.def
283/// prototype code for the function. See the top of clang's Builtins.def for
284/// a description of the type strings.
Nate Begeman7c21f742010-06-04 21:36:00 +0000285static std::string BuiltinTypeString(const char mod, StringRef typestr,
286 ClassKind ck, bool ret) {
Nate Begeman92f98af2010-06-04 07:11:25 +0000287 bool quad = false;
288 bool poly = false;
289 bool usgn = false;
290 bool scal = false;
291 bool cnst = false;
292 bool pntr = false;
293
294 if (mod == 'v')
295 return "v";
296 if (mod == 'i')
297 return "i";
298
299 // base type to get the type string for.
300 char type = ClassifyType(typestr, quad, poly, usgn);
301
302 // Based on the modifying character, change the type and width if necessary.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000303 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
304
305 if (pntr)
306 type = 'v';
307
Nate Begeman92f98af2010-06-04 07:11:25 +0000308 if (type == 'h') {
309 type = 's';
310 usgn = true;
311 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000312 usgn = usgn | poly | ((ck == ClassI || ck == ClassW) && scal && type != 'f');
Nate Begeman92f98af2010-06-04 07:11:25 +0000313
314 if (scal) {
315 SmallString<128> s;
316
317 if (usgn)
318 s.push_back('U');
Nate Begeman7c21f742010-06-04 21:36:00 +0000319
320 if (type == 'l')
321 s += "LLi";
322 else
323 s.push_back(type);
324
Nate Begeman92f98af2010-06-04 07:11:25 +0000325 if (cnst)
326 s.push_back('C');
327 if (pntr)
328 s.push_back('*');
329 return s.str();
330 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000331
332 // Since the return value must be one type, return a vector type of the
333 // appropriate width which we will bitcast.
334 if (ret) {
335 if (mod == '2')
336 return quad ? "V32c" : "V16c";
337 if (mod == '3')
338 return quad ? "V48c" : "V24c";
339 if (mod == '4')
340 return quad ? "V64c" : "V32c";
Nate Begemanf50551e2010-06-09 18:02:26 +0000341 if (mod == 'f' || (ck != ClassB && type == 'f'))
Nate Begeman56387832010-06-08 06:01:16 +0000342 return quad ? "V4f" : "V2f";
Nate Begemanf50551e2010-06-09 18:02:26 +0000343 if (ck != ClassB && type == 's')
Nate Begeman007afe42010-06-09 05:11:55 +0000344 return quad ? "V8s" : "V4s";
Nate Begemanf50551e2010-06-09 18:02:26 +0000345 if (ck != ClassB && type == 'i')
Nate Begeman56387832010-06-08 06:01:16 +0000346 return quad ? "V4i" : "V2i";
Nate Begemanf50551e2010-06-09 18:02:26 +0000347 if (ck != ClassB && type == 'l')
Nate Begeman007afe42010-06-09 05:11:55 +0000348 return quad ? "V2LLi" : "V1LLi";
Nate Begeman900f4672010-06-08 00:14:42 +0000349
Nate Begeman7c21f742010-06-04 21:36:00 +0000350 return quad ? "V16c" : "V8c";
351 }
352
353 // Non-return array types are passed as individual vectors.
Nate Begeman92f98af2010-06-04 07:11:25 +0000354 if (mod == '2')
355 return quad ? "V16cV16c" : "V8cV8c";
356 if (mod == '3')
357 return quad ? "V16cV16cV16c" : "V8cV8cV8c";
358 if (mod == '4')
359 return quad ? "V16cV16cV16cV16c" : "V8cV8cV8cV8c";
360
Nate Begemanf50551e2010-06-09 18:02:26 +0000361 if (mod == 'f' || (ck != ClassB && type == 'f'))
Nate Begeman007afe42010-06-09 05:11:55 +0000362 return quad ? "V4f" : "V2f";
Nate Begemanf50551e2010-06-09 18:02:26 +0000363 if (ck != ClassB && type == 's')
Nate Begeman007afe42010-06-09 05:11:55 +0000364 return quad ? "V8s" : "V4s";
Nate Begemanf50551e2010-06-09 18:02:26 +0000365 if (ck != ClassB && type == 'i')
Nate Begeman007afe42010-06-09 05:11:55 +0000366 return quad ? "V4i" : "V2i";
Nate Begemanf50551e2010-06-09 18:02:26 +0000367 if (ck != ClassB && type == 'l')
Nate Begeman007afe42010-06-09 05:11:55 +0000368 return quad ? "V2LLi" : "V1LLi";
369
Nate Begeman92f98af2010-06-04 07:11:25 +0000370 return quad ? "V16c" : "V8c";
371}
372
Nate Begemand72c9002010-06-13 04:47:03 +0000373/// MangleName - Append a type or width suffix to a base neon function name,
374/// and insert a 'q' in the appropriate location if the operation works on
375/// 128b rather than 64b. E.g. turn "vst2_lane" into "vst2q_lane_f32", etc.
Nate Begemana8979a02010-06-04 00:21:41 +0000376static std::string MangleName(const std::string &name, StringRef typestr,
377 ClassKind ck) {
Nate Begeman900f4672010-06-08 00:14:42 +0000378 if (name == "vcvt_f32_f16")
379 return name;
380
Nate Begemanaf905ef2010-06-02 06:17:19 +0000381 bool quad = false;
382 bool poly = false;
383 bool usgn = false;
384 char type = ClassifyType(typestr, quad, poly, usgn);
385
386 std::string s = name;
387
388 switch (type) {
Nate Begemana8979a02010-06-04 00:21:41 +0000389 case 'c':
390 switch (ck) {
391 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
392 case ClassI: s += "_i8"; break;
393 case ClassW: s += "_8"; break;
394 default: break;
395 }
396 break;
397 case 's':
398 switch (ck) {
399 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
400 case ClassI: s += "_i16"; break;
401 case ClassW: s += "_16"; break;
402 default: break;
403 }
404 break;
405 case 'i':
406 switch (ck) {
407 case ClassS: s += usgn ? "_u32" : "_s32"; break;
408 case ClassI: s += "_i32"; break;
409 case ClassW: s += "_32"; break;
410 default: break;
411 }
412 break;
413 case 'l':
414 switch (ck) {
415 case ClassS: s += usgn ? "_u64" : "_s64"; break;
416 case ClassI: s += "_i64"; break;
417 case ClassW: s += "_64"; break;
418 default: break;
419 }
420 break;
421 case 'h':
422 switch (ck) {
423 case ClassS:
424 case ClassI: s += "_f16"; break;
425 case ClassW: s += "_16"; break;
426 default: break;
427 }
428 break;
429 case 'f':
430 switch (ck) {
431 case ClassS:
432 case ClassI: s += "_f32"; break;
433 case ClassW: s += "_32"; break;
434 default: break;
435 }
436 break;
437 default:
438 throw "unhandled type!";
439 break;
Nate Begemanaf905ef2010-06-02 06:17:19 +0000440 }
Nate Begemana8979a02010-06-04 00:21:41 +0000441 if (ck == ClassB)
Nate Begeman92f98af2010-06-04 07:11:25 +0000442 s += "_v";
Nate Begemana8979a02010-06-04 00:21:41 +0000443
Nate Begemanaf905ef2010-06-02 06:17:19 +0000444 // Insert a 'q' before the first '_' character so that it ends up before
445 // _lane or _n on vector-scalar operations.
446 if (quad) {
447 size_t pos = s.find('_');
448 s = s.insert(pos, "q");
449 }
450 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000451}
452
Nate Begemanaf905ef2010-06-02 06:17:19 +0000453// Generate the string "(argtype a, argtype b, ...)"
Nate Begeman22237772010-06-02 00:34:55 +0000454static std::string GenArgs(const std::string &proto, StringRef typestr) {
Nate Begeman6c060db2010-06-09 01:09:00 +0000455 bool define = proto.find('i') != std::string::npos;
Nate Begemanaf905ef2010-06-02 06:17:19 +0000456 char arg = 'a';
457
458 std::string s;
459 s += "(";
460
461 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
Nate Begeman6c060db2010-06-09 01:09:00 +0000462 if (!define) {
463 s += TypeString(proto[i], typestr);
464 s.push_back(' ');
465 }
Nate Begemanaf905ef2010-06-02 06:17:19 +0000466 s.push_back(arg);
467 if ((i + 1) < e)
468 s += ", ";
469 }
470
471 s += ")";
472 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000473}
474
Nate Begemancc3c41a2010-06-12 03:09:49 +0000475static std::string Duplicate(unsigned nElts, StringRef typestr,
476 const std::string &a) {
Nate Begeman4b425a82010-06-10 00:16:56 +0000477 std::string s;
478
479 s = "(__neon_" + TypeString('d', typestr) + "){ ";
480 for (unsigned i = 0; i != nElts; ++i) {
481 s += a;
482 if ((i + 1) < nElts)
483 s += ", ";
484 }
485 s += " }";
486
487 return s;
488}
489
490// Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
491// If structTypes is true, the NEON types are structs of vector types rather
492// than vector types, and the call becomes "a.val + b.val"
493static std::string GenOpString(OpKind op, const std::string &proto,
494 StringRef typestr, bool structTypes = true) {
Nate Begemancc3c41a2010-06-12 03:09:49 +0000495 bool dummy, quad = false;
496 char type = ClassifyType(typestr, quad, dummy, dummy);
497 unsigned nElts = 0;
498 switch (type) {
499 case 'c': nElts = 8; break;
500 case 's': nElts = 4; break;
501 case 'i': nElts = 2; break;
502 case 'l': nElts = 1; break;
503 case 'h': nElts = 4; break;
504 case 'f': nElts = 2; break;
505 }
506
Nate Begeman4b425a82010-06-10 00:16:56 +0000507 std::string ts = TypeString(proto[0], typestr);
508 std::string s = ts + " r; r";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000509
Nate Begeman900f4672010-06-08 00:14:42 +0000510 if (structTypes)
511 s += ".val";
512
513 s += " = ";
514
Nate Begeman3861e742010-06-03 21:35:22 +0000515 std::string a, b, c;
516 if (proto.size() > 1)
Nate Begeman900f4672010-06-08 00:14:42 +0000517 a = (structTypes && proto[1] != 'l' && proto[1] != 's') ? "a.val" : "a";
Nate Begeman3861e742010-06-03 21:35:22 +0000518 b = structTypes ? "b.val" : "b";
519 c = structTypes ? "c.val" : "c";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000520
521 switch(op) {
522 case OpAdd:
523 s += a + " + " + b;
524 break;
525 case OpSub:
526 s += a + " - " + b;
527 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000528 case OpMulN:
Nate Begemancc3c41a2010-06-12 03:09:49 +0000529 b = Duplicate(nElts << quad, typestr, "b");
Nate Begeman162d3ba2010-06-03 04:04:09 +0000530 case OpMul:
531 s += a + " * " + b;
532 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000533 case OpMlaN:
Nate Begemancc3c41a2010-06-12 03:09:49 +0000534 c = Duplicate(nElts << quad, typestr, "c");
Nate Begeman162d3ba2010-06-03 04:04:09 +0000535 case OpMla:
536 s += a + " + ( " + b + " * " + c + " )";
537 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000538 case OpMlsN:
Nate Begemancc3c41a2010-06-12 03:09:49 +0000539 c = Duplicate(nElts << quad, typestr, "c");
Nate Begeman162d3ba2010-06-03 04:04:09 +0000540 case OpMls:
541 s += a + " - ( " + b + " * " + c + " )";
542 break;
543 case OpEq:
544 s += "(__neon_" + ts + ")(" + a + " == " + b + ")";
545 break;
546 case OpGe:
547 s += "(__neon_" + ts + ")(" + a + " >= " + b + ")";
548 break;
549 case OpLe:
550 s += "(__neon_" + ts + ")(" + a + " <= " + b + ")";
551 break;
552 case OpGt:
553 s += "(__neon_" + ts + ")(" + a + " > " + b + ")";
554 break;
555 case OpLt:
556 s += "(__neon_" + ts + ")(" + a + " < " + b + ")";
557 break;
558 case OpNeg:
559 s += " -" + a;
560 break;
561 case OpNot:
562 s += " ~" + a;
563 break;
564 case OpAnd:
565 s += a + " & " + b;
566 break;
567 case OpOr:
568 s += a + " | " + b;
569 break;
570 case OpXor:
571 s += a + " ^ " + b;
572 break;
573 case OpAndNot:
574 s += a + " & ~" + b;
575 break;
576 case OpOrNot:
577 s += a + " | ~" + b;
578 break;
Nate Begeman3861e742010-06-03 21:35:22 +0000579 case OpCast:
580 s += "(__neon_" + ts + ")" + a;
581 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000582 case OpConcat:
583 s += "__builtin_shufflevector((__neon_int64x1_t)" + a;
584 s += ", (__neon_int64x1_t)" + b + ", 0, 1)";
585 break;
Nate Begeman6c060db2010-06-09 01:09:00 +0000586 case OpHi:
587 s += "(__neon_int64x1_t)(((__neon_int64x2_t)" + a + ")[1])";
588 break;
589 case OpLo:
590 s += "(__neon_int64x1_t)(((__neon_int64x2_t)" + a + ")[0])";
591 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000592 case OpDup:
Nate Begemancc3c41a2010-06-12 03:09:49 +0000593 s += Duplicate(nElts << quad, typestr, a);
594 break;
595 case OpSelect:
596 // ((0 & 1) | (~0 & 2))
597 ts = TypeString(proto[1], typestr);
598 s += "( " + a + " & (__neon_" + ts + ")" + b + ") | ";
599 s += "(~" + a + " & (__neon_" + ts + ")" + c + ")";
600 break;
601 case OpRev16:
602 s += "__builtin_shufflevector(" + a + ", " + a;
603 for (unsigned i = 2; i <= nElts << quad; i += 2)
604 for (unsigned j = 0; j != 2; ++j)
605 s += ", " + utostr(i - j - 1);
606 s += ")";
607 break;
608 case OpRev32:
609 nElts >>= 1;
610 s += "__builtin_shufflevector(" + a + ", " + a;
611 for (unsigned i = nElts; i <= nElts << (1 + quad); i += nElts)
612 for (unsigned j = 0; j != nElts; ++j)
613 s += ", " + utostr(i - j - 1);
614 s += ")";
615 break;
616 case OpRev64:
617 s += "__builtin_shufflevector(" + a + ", " + a;
618 for (unsigned i = nElts; i <= nElts << quad; i += nElts)
619 for (unsigned j = 0; j != nElts; ++j)
620 s += ", " + utostr(i - j - 1);
621 s += ")";
Nate Begeman900f4672010-06-08 00:14:42 +0000622 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000623 default:
624 throw "unknown OpKind!";
625 break;
626 }
Nate Begeman900f4672010-06-08 00:14:42 +0000627 s += "; return r;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000628 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000629}
630
Nate Begemanb0a4e452010-06-07 16:00:37 +0000631static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
632 unsigned mod = proto[0];
633 unsigned ret = 0;
634
Nate Begeman900f4672010-06-08 00:14:42 +0000635 if (mod == 'v' || mod == 'f')
Nate Begemanb0a4e452010-06-07 16:00:37 +0000636 mod = proto[1];
637
638 bool quad = false;
639 bool poly = false;
640 bool usgn = false;
641 bool scal = false;
642 bool cnst = false;
643 bool pntr = false;
644
645 // base type to get the type string for.
646 char type = ClassifyType(typestr, quad, poly, usgn);
647
648 // Based on the modifying character, change the type and width if necessary.
649 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
650
651 if (usgn)
652 ret |= 0x08;
653 if (quad)
654 ret |= 0x10;
655
656 switch (type) {
657 case 'c':
658 ret |= poly ? 5 : 0;
659 break;
660 case 's':
661 ret |= poly ? 6 : 1;
662 break;
663 case 'i':
664 ret |= 2;
665 break;
666 case 'l':
667 ret |= 3;
668 break;
669 case 'h':
670 ret |= 7;
671 break;
672 case 'f':
673 ret |= 4;
674 break;
675 default:
676 throw "unhandled type!";
677 break;
678 }
679 return ret;
680}
681
Nate Begeman7c8c8832010-06-02 21:53:00 +0000682// Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
683// If structTypes is true, the NEON types are structs of vector types rather
684// than vector types, and the call becomes __builtin_neon_cls(a.val)
685static std::string GenBuiltin(const std::string &name, const std::string &proto,
Nate Begemana8979a02010-06-04 00:21:41 +0000686 StringRef typestr, ClassKind ck,
687 bool structTypes = true) {
Nate Begemancc3c41a2010-06-12 03:09:49 +0000688 bool dummy, quad = false;
689 char type = ClassifyType(typestr, quad, dummy, dummy);
690 unsigned nElts = 0;
691 switch (type) {
692 case 'c': nElts = 8; break;
693 case 's': nElts = 4; break;
694 case 'i': nElts = 2; break;
695 case 'l': nElts = 1; break;
696 case 'h': nElts = 4; break;
697 case 'f': nElts = 2; break;
698 }
Chris Lattner5ca96982010-06-12 15:46:56 +0000699 if (quad) nElts <<= 1;
Nate Begemancc3c41a2010-06-12 03:09:49 +0000700
Nate Begeman7c8c8832010-06-02 21:53:00 +0000701 char arg = 'a';
Nate Begeman162d3ba2010-06-03 04:04:09 +0000702 std::string s;
Nate Begeman9e584b32010-06-04 22:53:30 +0000703
704 bool unioning = (proto[0] == '2' || proto[0] == '3' || proto[0] == '4');
Nate Begeman6c060db2010-06-09 01:09:00 +0000705 bool define = proto.find('i') != std::string::npos;
Nate Begeman9e584b32010-06-04 22:53:30 +0000706
707 // If all types are the same size, bitcasting the args will take care
708 // of arg checking. The actual signedness etc. will be taken care of with
709 // special enums.
710 if (proto.find('s') == std::string::npos)
711 ck = ClassB;
Nate Begeman7c21f742010-06-04 21:36:00 +0000712
Nate Begeman162d3ba2010-06-03 04:04:09 +0000713 if (proto[0] != 'v') {
Nate Begeman6c060db2010-06-09 01:09:00 +0000714 std::string ts = TypeString(proto[0], typestr);
715
716 if (define) {
717 if (proto[0] != 's')
718 s += "(" + ts + "){(__neon_" + ts + ")";
Nate Begeman9e584b32010-06-04 22:53:30 +0000719 } else {
Nate Begeman6c060db2010-06-09 01:09:00 +0000720 if (unioning) {
721 s += "union { ";
722 s += TypeString(proto[0], typestr, true) + " val; ";
723 s += TypeString(proto[0], typestr, false) + " s; ";
724 s += "} r;";
725 } else {
726 s += ts;
727 }
728
729 s += " r; r";
730 if (structTypes && proto[0] != 's' && proto[0] != 'i' && proto[0] != 'l')
731 s += ".val";
732
733 s += " = ";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000734 }
Nate Begeman4b425a82010-06-10 00:16:56 +0000735 }
736
737 bool splat = proto.find('a') != std::string::npos;
Nate Begeman7c8c8832010-06-02 21:53:00 +0000738
739 s += "__builtin_neon_";
Nate Begeman4b425a82010-06-10 00:16:56 +0000740 if (splat) {
741 std::string vname(name, 0, name.size()-2);
742 s += MangleName(vname, typestr, ck);
743 } else {
744 s += MangleName(name, typestr, ck);
745 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000746 s += "(";
747
748 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
Nate Begemancc3c41a2010-06-12 03:09:49 +0000749 std::string args = std::string(&arg, 1);
750 if (define)
751 args = "(" + args + ")";
752
Nate Begeman9e584b32010-06-04 22:53:30 +0000753 // Handle multiple-vector values specially, emitting each subvector as an
754 // argument to the __builtin.
755 if (structTypes && (proto[i] == '2' || proto[i] == '3' || proto[i] == '4')){
756 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
Nate Begemancc3c41a2010-06-12 03:09:49 +0000757 s += args + ".val[" + utostr(vi) + "]";
Nate Begeman9e584b32010-06-04 22:53:30 +0000758 if ((vi + 1) < ve)
759 s += ", ";
760 }
761 if ((i + 1) < e)
762 s += ", ";
763
764 continue;
765 }
766
Nate Begeman4b425a82010-06-10 00:16:56 +0000767 if (splat && (i + 1) == e)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000768 s += Duplicate(nElts, typestr, args);
Nate Begeman4b425a82010-06-10 00:16:56 +0000769 else
Nate Begemancc3c41a2010-06-12 03:09:49 +0000770 s += args;
Nate Begeman9e584b32010-06-04 22:53:30 +0000771
Nate Begeman162d3ba2010-06-03 04:04:09 +0000772 if (structTypes && proto[i] != 's' && proto[i] != 'i' && proto[i] != 'l' &&
Nate Begeman4b425a82010-06-10 00:16:56 +0000773 proto[i] != 'p' && proto[i] != 'c' && proto[i] != 'a') {
Nate Begeman7c8c8832010-06-02 21:53:00 +0000774 s += ".val";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000775 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000776 if ((i + 1) < e)
777 s += ", ";
778 }
779
Nate Begeman9e584b32010-06-04 22:53:30 +0000780 // Extra constant integer to hold type class enum for this function, e.g. s8
Nate Begeman9e584b32010-06-04 22:53:30 +0000781 if (ck == ClassB)
Nate Begemanb0a4e452010-06-07 16:00:37 +0000782 s += ", " + utostr(GetNeonEnum(proto, typestr));
Nate Begeman9e584b32010-06-04 22:53:30 +0000783
Nate Begeman6c060db2010-06-09 01:09:00 +0000784 if (define)
785 s += ")";
786 else
787 s += ");";
Nate Begeman9e584b32010-06-04 22:53:30 +0000788
789 if (proto[0] != 'v') {
Nate Begeman6c060db2010-06-09 01:09:00 +0000790 if (define) {
791 if (proto[0] != 's')
792 s += "}";
793 } else {
794 if (unioning)
795 s += " return r.s;";
796 else
797 s += " return r;";
798 }
Nate Begeman9e584b32010-06-04 22:53:30 +0000799 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000800 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000801}
802
Nate Begeman73cef3e2010-06-04 01:26:15 +0000803static std::string GenBuiltinDef(const std::string &name,
804 const std::string &proto,
805 StringRef typestr, ClassKind ck) {
806 std::string s("BUILTIN(__builtin_neon_");
Nate Begeman92f98af2010-06-04 07:11:25 +0000807
808 // If all types are the same size, bitcasting the args will take care
809 // of arg checking. The actual signedness etc. will be taken care of with
810 // special enums.
811 if (proto.find('s') == std::string::npos)
812 ck = ClassB;
813
Nate Begeman73cef3e2010-06-04 01:26:15 +0000814 s += MangleName(name, typestr, ck);
815 s += ", \"";
816
Nate Begeman92f98af2010-06-04 07:11:25 +0000817 for (unsigned i = 0, e = proto.size(); i != e; ++i)
Nate Begeman7c21f742010-06-04 21:36:00 +0000818 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
819
820 // Extra constant integer to hold type class enum for this function, e.g. s8
821 if (ck == ClassB)
822 s += "i";
Nate Begeman73cef3e2010-06-04 01:26:15 +0000823
824 s += "\", \"n\")";
825 return s;
826}
827
Nate Begemand72c9002010-06-13 04:47:03 +0000828/// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h
829/// is comprised of type definitions and function declarations.
Nate Begeman5ddb0872010-05-28 01:08:32 +0000830void NeonEmitter::run(raw_ostream &OS) {
831 EmitSourceFileHeader("ARM NEON Header", OS);
832
833 // FIXME: emit license into file?
834
835 OS << "#ifndef __ARM_NEON_H\n";
836 OS << "#define __ARM_NEON_H\n\n";
837
838 OS << "#ifndef __ARM_NEON__\n";
839 OS << "#error \"NEON support not enabled\"\n";
840 OS << "#endif\n\n";
841
842 OS << "#include <stdint.h>\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000843
844 // Emit NEON-specific scalar typedefs.
Nate Begeman7c8c8832010-06-02 21:53:00 +0000845 OS << "typedef float float32_t;\n";
846 OS << "typedef uint8_t poly8_t;\n";
847 OS << "typedef uint16_t poly16_t;\n";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000848 OS << "typedef uint16_t float16_t;\n";
Nate Begeman9e584b32010-06-04 22:53:30 +0000849
Nate Begeman7c8c8832010-06-02 21:53:00 +0000850 // Emit Neon vector typedefs.
851 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
852 SmallVector<StringRef, 24> TDTypeVec;
853 ParseTypes(0, TypedefTypes, TDTypeVec);
854
855 // Emit vector typedefs.
Nate Begeman9e584b32010-06-04 22:53:30 +0000856 for (unsigned v = 1; v != 5; ++v) {
857 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
858 bool dummy, quad = false;
859 (void) ClassifyType(TDTypeVec[i], quad, dummy, dummy);
860 OS << "typedef __attribute__(( __vector_size__(";
861
862 OS << utostr(8*v*(quad ? 2 : 1)) << ") )) ";
863 if (!quad)
864 OS << " ";
865
866 OS << TypeString('s', TDTypeVec[i]);
867 OS << " __neon_";
868
869 char t = (v == 1) ? 'd' : '0' + v;
870 OS << TypeString(t, TDTypeVec[i]) << ";\n";
871 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000872 }
873 OS << "\n";
874
875 // Emit struct typedefs.
876 for (unsigned vi = 1; vi != 5; ++vi) {
877 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
878 std::string ts = TypeString('d', TDTypeVec[i]);
879 std::string vs = (vi > 1) ? TypeString('0' + vi, TDTypeVec[i]) : ts;
880 OS << "typedef struct __" << vs << " {\n";
881 OS << " __neon_" << ts << " val";
882 if (vi > 1)
883 OS << "[" << utostr(vi) << "]";
884 OS << ";\n} " << vs << ";\n\n";
885 }
886 }
Nate Begeman5ddb0872010-05-28 01:08:32 +0000887
Nate Begeman7c8c8832010-06-02 21:53:00 +0000888 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
889
Nate Begeman5ddb0872010-05-28 01:08:32 +0000890 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
891
Nate Begeman22237772010-06-02 00:34:55 +0000892 // Unique the return+pattern types, and assign them.
Nate Begeman5ddb0872010-05-28 01:08:32 +0000893 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
894 Record *R = RV[i];
Nate Begeman22237772010-06-02 00:34:55 +0000895 std::string name = LowercaseString(R->getName());
896 std::string Proto = R->getValueAsString("Prototype");
Nate Begeman5ddb0872010-05-28 01:08:32 +0000897 std::string Types = R->getValueAsString("Types");
Nate Begeman22237772010-06-02 00:34:55 +0000898
899 SmallVector<StringRef, 16> TypeVec;
900 ParseTypes(R, Types, TypeVec);
901
Nate Begeman162d3ba2010-06-03 04:04:09 +0000902 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
Nate Begemane66aab52010-06-02 07:14:28 +0000903
Nate Begeman6c060db2010-06-09 01:09:00 +0000904 bool define = Proto.find('i') != std::string::npos;
905
Nate Begeman22237772010-06-02 00:34:55 +0000906 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
907 assert(!Proto.empty() && "");
908
Nate Begeman7c8c8832010-06-02 21:53:00 +0000909 // static always inline + return type
Nate Begeman6c060db2010-06-09 01:09:00 +0000910 if (define)
911 OS << "#define";
912 else
913 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
Nate Begeman22237772010-06-02 00:34:55 +0000914
Nate Begemane66aab52010-06-02 07:14:28 +0000915 // Function name with type suffix
Nate Begemana8979a02010-06-04 00:21:41 +0000916 OS << " " << MangleName(name, TypeVec[ti], ClassS);
Nate Begeman22237772010-06-02 00:34:55 +0000917
Nate Begemane66aab52010-06-02 07:14:28 +0000918 // Function arguments
919 OS << GenArgs(Proto, TypeVec[ti]);
Nate Begeman22237772010-06-02 00:34:55 +0000920
Nate Begemane66aab52010-06-02 07:14:28 +0000921 // Definition.
Nate Begeman6c060db2010-06-09 01:09:00 +0000922 if (define)
923 OS << " ";
924 else
925 OS << " { ";
Nate Begeman22237772010-06-02 00:34:55 +0000926
Nate Begemana8979a02010-06-04 00:21:41 +0000927 if (k != OpNone) {
Nate Begeman162d3ba2010-06-03 04:04:09 +0000928 OS << GenOpString(k, Proto, TypeVec[ti]);
Nate Begemana8979a02010-06-04 00:21:41 +0000929 } else {
930 if (R->getSuperClasses().size() < 2)
931 throw TGError(R->getLoc(), "Builtin has no class kind");
932
933 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
934
935 if (ck == ClassNone)
936 throw TGError(R->getLoc(), "Builtin has no class kind");
937 OS << GenBuiltin(name, Proto, TypeVec[ti], ck);
938 }
Nate Begeman6c060db2010-06-09 01:09:00 +0000939 if (!define)
940 OS << " }";
941 OS << "\n";
Nate Begeman22237772010-06-02 00:34:55 +0000942 }
943 OS << "\n";
Nate Begeman5ddb0872010-05-28 01:08:32 +0000944 }
Nate Begeman73cef3e2010-06-04 01:26:15 +0000945 OS << "#undef __ai\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000946 OS << "#endif /* __ARM_NEON_H */\n";
Nate Begeman5ddb0872010-05-28 01:08:32 +0000947}
Nate Begemana8979a02010-06-04 00:21:41 +0000948
Nate Begemand72c9002010-06-13 04:47:03 +0000949/// runHeader - generate one of three different tables which are used by clang
950/// to support ARM NEON codegen. By default, this will produce the contents of
951/// BuiltinsARM.def's NEON section. You may also enable the genSemaTypes or
952/// getSemaRange variables below to generate code that SemaChecking will use to
953/// validate the builtin function calls.
954///
955/// This is not used as part of the build system currently, but is run manually
956/// and the output placed in the appropriate file.
Nate Begemana8979a02010-06-04 00:21:41 +0000957void NeonEmitter::runHeader(raw_ostream &OS) {
Nate Begeman73cef3e2010-06-04 01:26:15 +0000958 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
959
960 StringMap<OpKind> EmittedMap;
961
Nate Begemand72c9002010-06-13 04:47:03 +0000962 // Set true to generate the overloaded type checking code for SemaChecking.cpp
963 bool genSemaTypes = false;
964
965 // Set true to generate the intrinsic range checking code for shift/lane
966 // immediates for SemaChecking.cpp
967 bool genSemaRange = true;
968
Nate Begeman73cef3e2010-06-04 01:26:15 +0000969 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
970 Record *R = RV[i];
971
972 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
973 if (k != OpNone)
974 continue;
975
976 std::string name = LowercaseString(R->getName());
977 std::string Proto = R->getValueAsString("Prototype");
978 std::string Types = R->getValueAsString("Types");
979
Nate Begemand72c9002010-06-13 04:47:03 +0000980 // Functions with 'a' (the splat code) in the type prototype should not get
981 // their own builtin as they use the non-splat variant.
Nate Begeman4b425a82010-06-10 00:16:56 +0000982 if (Proto.find('a') != std::string::npos)
983 continue;
Nate Begemand72c9002010-06-13 04:47:03 +0000984
985 // Functions which have a scalar argument cannot be overloaded, no need to
986 // check them if we are emitting the type checking code.
987 if (genSemaTypes && Proto.find('s') != std::string::npos)
988 continue;
989
990 // Functions which do not have an immediate do not need to have range
991 // checking code emitted.
992 if (genSemaRange && Proto.find('i') == std::string::npos)
993 continue;
994
Nate Begeman73cef3e2010-06-04 01:26:15 +0000995 SmallVector<StringRef, 16> TypeVec;
996 ParseTypes(R, Types, TypeVec);
Nate Begemand72c9002010-06-13 04:47:03 +0000997
Nate Begeman73cef3e2010-06-04 01:26:15 +0000998 if (R->getSuperClasses().size() < 2)
999 throw TGError(R->getLoc(), "Builtin has no class kind");
1000
1001 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1002
Nate Begemand72c9002010-06-13 04:47:03 +00001003 int si = -1, qi = -1;
1004 unsigned mask = 0, qmask = 0;
Nate Begeman73cef3e2010-06-04 01:26:15 +00001005 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
Nate Begemand72c9002010-06-13 04:47:03 +00001006
1007 // Generate the switch case(s) for this builtin for the type validation.
1008 if (genSemaTypes) {
1009 bool quad = false, poly = false, usgn = false;
1010 (void) ClassifyType(TypeVec[ti], quad, poly, usgn);
1011
1012 if (quad) {
1013 qi = ti;
1014 qmask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1015 } else {
1016 si = ti;
1017 mask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1018 }
1019 continue;
1020 }
1021
1022 if (genSemaRange) {
1023 if (Proto.find('s') == std::string::npos)
1024 ck = ClassB;
1025
1026 OS << "case ARM::BI__builtin_neon_"
1027 << MangleName(name, TypeVec[ti], ck) << "\n";
1028 continue;
1029 }
1030
1031 // Generate the BuiltinsARM.def declaration for this builtin, ensuring
1032 // that each unique BUILTIN() macro appears only once in the output
1033 // stream.
Nate Begeman73cef3e2010-06-04 01:26:15 +00001034 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
1035 if (EmittedMap.count(bd))
1036 continue;
1037
1038 EmittedMap[bd] = OpNone;
1039 OS << bd << "\n";
1040 }
Nate Begemand72c9002010-06-13 04:47:03 +00001041
1042 if (genSemaTypes) {
1043 if (mask)
1044 OS << "case ARM::BI__builtin_neon_"
1045 << MangleName(name, TypeVec[si], ClassB)
1046 << ": mask = " << "0x" << utohexstr(mask) << "; break;\n";
1047 if (qmask)
1048 OS << "case ARM::BI__builtin_neon_"
1049 << MangleName(name, TypeVec[qi], ClassB)
1050 << ": mask = " << "0x" << utohexstr(qmask) << "; break;\n";
1051 continue;
1052 }
Nate Begeman73cef3e2010-06-04 01:26:15 +00001053 }
Nate Begemana8979a02010-06-04 00:21:41 +00001054}