blob: ebdc970e3fbe7824067a47d7f7abc9e6a624b015 [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
Bob Wilson7f762182010-12-04 04:40:15 +000011// a declaration and definition of each function specified by the ARM NEON
Nate Begeman5ddb0872010-05-28 01:08:32 +000012// 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
Bob Wilson7f762182010-12-04 04:40:15 +000015// are suffixed with the element type of the input vectors. Functions may be
Nate Begemand72c9002010-06-13 04:47:03 +000016// 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;
Bob Wilson7f762182010-12-04 04:40:15 +000041
Nate Begeman22237772010-06-02 00:34:55 +000042 for (unsigned i = 0, e = s.size(); i != e; ++i, ++len) {
43 if (data[len] == 'P' || data[len] == 'Q' || data[len] == 'U')
44 continue;
Bob Wilson7f762182010-12-04 04:40:15 +000045
Nate Begeman22237772010-06-02 00:34:55 +000046 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;
Bob Wilson7f762182010-12-04 04:40:15 +0000101
Nate Begemanaf905ef2010-06-02 06:17:19 +0000102 // remember quad.
103 if (ty[off] == 'Q') {
104 quad = true;
105 ++off;
106 }
Bob Wilson7f762182010-12-04 04:40:15 +0000107
Nate Begemanaf905ef2010-06-02 06:17:19 +0000108 // remember poly.
109 if (ty[off] == 'P') {
110 poly = true;
111 ++off;
112 }
Bob Wilson7f762182010-12-04 04:40:15 +0000113
Nate Begemanaf905ef2010-06-02 06:17:19 +0000114 // remember unsigned.
115 if (ty[off] == 'U') {
116 usgn = true;
117 ++off;
118 }
Bob Wilson7f762182010-12-04 04:40:15 +0000119
Nate Begemanaf905ef2010-06-02 06:17:19 +0000120 // 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;
Bob Wilson7f762182010-12-04 04:40:15 +0000209
Nate Begemanb0a4e452010-06-07 16:00:37 +0000210 if (mod == 'v')
211 return "void";
212 if (mod == 'i')
213 return "int";
Bob Wilson7f762182010-12-04 04:40:15 +0000214
Nate Begemanb0a4e452010-06-07 16:00:37 +0000215 // base type to get the type string for.
216 char type = ClassifyType(typestr, quad, poly, usgn);
Bob Wilson7f762182010-12-04 04:40:15 +0000217
Nate Begemanb0a4e452010-06-07 16:00:37 +0000218 // Based on the modifying character, change the type and width if necessary.
219 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
Bob Wilson7f762182010-12-04 04:40:15 +0000220
Nate Begeman22237772010-06-02 00:34:55 +0000221 SmallString<128> s;
Bob Wilson7f762182010-12-04 04:40:15 +0000222
Nate Begeman22237772010-06-02 00:34:55 +0000223 if (usgn)
224 s.push_back('u');
Bob Wilson7f762182010-12-04 04:40:15 +0000225
Nate Begeman22237772010-06-02 00:34:55 +0000226 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";
Bob Wilson7f762182010-12-04 04:40:15 +0000274
Nate Begeman22237772010-06-02 00:34:55 +0000275 // Append _t, finishing the type string typedef type.
276 s += "_t";
Bob Wilson7f762182010-12-04 04:40:15 +0000277
Nate Begeman22237772010-06-02 00:34:55 +0000278 if (cnst)
279 s += " const";
Bob Wilson7f762182010-12-04 04:40:15 +0000280
Nate Begeman22237772010-06-02 00:34:55 +0000281 if (pntr)
282 s += " *";
Bob Wilson7f762182010-12-04 04:40:15 +0000283
Nate Begeman22237772010-06-02 00:34:55 +0000284 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;
Bob Wilson7f762182010-12-04 04:40:15 +0000298
Nate Begeman92f98af2010-06-04 07:11:25 +0000299 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
Bob Wilson7f762182010-12-04 04:40:15 +0000303
Nate Begeman92f98af2010-06-04 07:11:25 +0000304 // base type to get the type string for.
305 char type = ClassifyType(typestr, quad, poly, usgn);
Bob Wilson7f762182010-12-04 04:40:15 +0000306
Nate Begeman92f98af2010-06-04 07:11:25 +0000307 // 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');
Bob Wilson317bafb2010-12-02 00:24:59 +0000328 else if (type == 'c')
329 s.push_back('S'); // make chars explicitly signed
Bob Wilson7f762182010-12-04 04:40:15 +0000330
Bob Wilson95148ad2010-12-01 19:49:56 +0000331 if (type == 'l') // 64-bit long
Nate Begeman7c21f742010-06-04 21:36:00 +0000332 s += "LLi";
333 else
334 s.push_back(type);
Bob Wilson7f762182010-12-04 04:40:15 +0000335
Nate Begeman92f98af2010-06-04 07:11:25 +0000336 if (cnst)
337 s.push_back('C');
338 if (pntr)
339 s.push_back('*');
340 return s.str();
341 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000342
343 // Since the return value must be one type, return a vector type of the
Nate Begemanc4a1b652010-06-20 21:09:52 +0000344 // appropriate width which we will bitcast. An exception is made for
345 // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
346 // fashion, storing them to a pointer arg.
Nate Begeman7c21f742010-06-04 21:36:00 +0000347 if (ret) {
Bob Wilson5b7fe592010-12-01 19:49:51 +0000348 if (mod >= '2' && mod <= '4')
Bob Wilson95148ad2010-12-01 19:49:56 +0000349 return "vv*"; // void result with void* first argument
Nate Begemanf50551e2010-06-09 18:02:26 +0000350 if (mod == 'f' || (ck != ClassB && type == 'f'))
Nate Begeman56387832010-06-08 06:01:16 +0000351 return quad ? "V4f" : "V2f";
Nate Begemanf50551e2010-06-09 18:02:26 +0000352 if (ck != ClassB && type == 's')
Nate Begeman007afe42010-06-09 05:11:55 +0000353 return quad ? "V8s" : "V4s";
Nate Begemanf50551e2010-06-09 18:02:26 +0000354 if (ck != ClassB && type == 'i')
Nate Begeman56387832010-06-08 06:01:16 +0000355 return quad ? "V4i" : "V2i";
Nate Begemanf50551e2010-06-09 18:02:26 +0000356 if (ck != ClassB && type == 'l')
Nate Begeman007afe42010-06-09 05:11:55 +0000357 return quad ? "V2LLi" : "V1LLi";
Bob Wilson7f762182010-12-04 04:40:15 +0000358
Bob Wilson317bafb2010-12-02 00:24:59 +0000359 return quad ? "V16Sc" : "V8Sc";
Bob Wilson7f762182010-12-04 04:40:15 +0000360 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000361
362 // Non-return array types are passed as individual vectors.
Nate Begeman92f98af2010-06-04 07:11:25 +0000363 if (mod == '2')
Bob Wilson317bafb2010-12-02 00:24:59 +0000364 return quad ? "V16ScV16Sc" : "V8ScV8Sc";
Nate Begeman92f98af2010-06-04 07:11:25 +0000365 if (mod == '3')
Bob Wilson317bafb2010-12-02 00:24:59 +0000366 return quad ? "V16ScV16ScV16Sc" : "V8ScV8ScV8Sc";
Nate Begeman92f98af2010-06-04 07:11:25 +0000367 if (mod == '4')
Bob Wilson317bafb2010-12-02 00:24:59 +0000368 return quad ? "V16ScV16ScV16ScV16Sc" : "V8ScV8ScV8ScV8Sc";
Nate Begeman92f98af2010-06-04 07:11:25 +0000369
Nate Begemanf50551e2010-06-09 18:02:26 +0000370 if (mod == 'f' || (ck != ClassB && type == 'f'))
Nate Begeman007afe42010-06-09 05:11:55 +0000371 return quad ? "V4f" : "V2f";
Nate Begemanf50551e2010-06-09 18:02:26 +0000372 if (ck != ClassB && type == 's')
Nate Begeman007afe42010-06-09 05:11:55 +0000373 return quad ? "V8s" : "V4s";
Nate Begemanf50551e2010-06-09 18:02:26 +0000374 if (ck != ClassB && type == 'i')
Nate Begeman007afe42010-06-09 05:11:55 +0000375 return quad ? "V4i" : "V2i";
Nate Begemanf50551e2010-06-09 18:02:26 +0000376 if (ck != ClassB && type == 'l')
Nate Begeman007afe42010-06-09 05:11:55 +0000377 return quad ? "V2LLi" : "V1LLi";
Bob Wilson7f762182010-12-04 04:40:15 +0000378
Bob Wilson317bafb2010-12-02 00:24:59 +0000379 return quad ? "V16Sc" : "V8Sc";
Nate Begeman92f98af2010-06-04 07:11:25 +0000380}
381
Bob Wilson7f762182010-12-04 04:40:15 +0000382/// MangleName - Append a type or width suffix to a base neon function name,
Nate Begemand72c9002010-06-13 04:47:03 +0000383/// and insert a 'q' in the appropriate location if the operation works on
384/// 128b rather than 64b. E.g. turn "vst2_lane" into "vst2q_lane_f32", etc.
Nate Begemana8979a02010-06-04 00:21:41 +0000385static std::string MangleName(const std::string &name, StringRef typestr,
386 ClassKind ck) {
Nate Begeman900f4672010-06-08 00:14:42 +0000387 if (name == "vcvt_f32_f16")
388 return name;
Bob Wilson7f762182010-12-04 04:40:15 +0000389
Nate Begemanaf905ef2010-06-02 06:17:19 +0000390 bool quad = false;
391 bool poly = false;
392 bool usgn = false;
393 char type = ClassifyType(typestr, quad, poly, usgn);
394
395 std::string s = name;
Bob Wilson7f762182010-12-04 04:40:15 +0000396
Nate Begemanaf905ef2010-06-02 06:17:19 +0000397 switch (type) {
Nate Begemana8979a02010-06-04 00:21:41 +0000398 case 'c':
399 switch (ck) {
400 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
401 case ClassI: s += "_i8"; break;
402 case ClassW: s += "_8"; break;
403 default: break;
404 }
405 break;
406 case 's':
407 switch (ck) {
408 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
409 case ClassI: s += "_i16"; break;
410 case ClassW: s += "_16"; break;
411 default: break;
412 }
413 break;
414 case 'i':
415 switch (ck) {
416 case ClassS: s += usgn ? "_u32" : "_s32"; break;
417 case ClassI: s += "_i32"; break;
418 case ClassW: s += "_32"; break;
419 default: break;
420 }
421 break;
422 case 'l':
423 switch (ck) {
424 case ClassS: s += usgn ? "_u64" : "_s64"; break;
425 case ClassI: s += "_i64"; break;
426 case ClassW: s += "_64"; break;
427 default: break;
428 }
429 break;
430 case 'h':
431 switch (ck) {
432 case ClassS:
433 case ClassI: s += "_f16"; break;
434 case ClassW: s += "_16"; break;
435 default: break;
436 }
437 break;
438 case 'f':
439 switch (ck) {
440 case ClassS:
441 case ClassI: s += "_f32"; break;
442 case ClassW: s += "_32"; break;
443 default: break;
444 }
445 break;
446 default:
447 throw "unhandled type!";
448 break;
Nate Begemanaf905ef2010-06-02 06:17:19 +0000449 }
Nate Begemana8979a02010-06-04 00:21:41 +0000450 if (ck == ClassB)
Nate Begeman92f98af2010-06-04 07:11:25 +0000451 s += "_v";
Bob Wilson7f762182010-12-04 04:40:15 +0000452
453 // Insert a 'q' before the first '_' character so that it ends up before
Nate Begemanaf905ef2010-06-02 06:17:19 +0000454 // _lane or _n on vector-scalar operations.
455 if (quad) {
456 size_t pos = s.find('_');
457 s = s.insert(pos, "q");
458 }
459 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000460}
461
Nate Begemanaf905ef2010-06-02 06:17:19 +0000462// Generate the string "(argtype a, argtype b, ...)"
Nate Begeman22237772010-06-02 00:34:55 +0000463static std::string GenArgs(const std::string &proto, StringRef typestr) {
Nate Begeman6c060db2010-06-09 01:09:00 +0000464 bool define = proto.find('i') != std::string::npos;
Nate Begemanaf905ef2010-06-02 06:17:19 +0000465 char arg = 'a';
Bob Wilson7f762182010-12-04 04:40:15 +0000466
Nate Begemanaf905ef2010-06-02 06:17:19 +0000467 std::string s;
468 s += "(";
Bob Wilson7f762182010-12-04 04:40:15 +0000469
Nate Begemanaf905ef2010-06-02 06:17:19 +0000470 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
Bob Wilson194aa582010-12-03 00:34:09 +0000471 if (define) {
472 // Immediate macro arguments are used directly instead of being assigned
473 // to local temporaries; prepend an underscore prefix to make their
474 // names consistent with the local temporaries.
475 if (proto[i] == 'i')
476 s += "__";
477 } else {
478 s += TypeString(proto[i], typestr) + " __";
Nate Begeman6c060db2010-06-09 01:09:00 +0000479 }
Nate Begemanaf905ef2010-06-02 06:17:19 +0000480 s.push_back(arg);
481 if ((i + 1) < e)
482 s += ", ";
483 }
Bob Wilson7f762182010-12-04 04:40:15 +0000484
Nate Begemanaf905ef2010-06-02 06:17:19 +0000485 s += ")";
486 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000487}
488
Bob Wilson194aa582010-12-03 00:34:09 +0000489// Macro arguments are not type-checked like inline function arguments, so
490// assign them to local temporaries to get the right type checking.
Bob Wilson377296e2010-12-02 07:10:39 +0000491static std::string GenMacroLocals(const std::string &proto, StringRef typestr) {
492 char arg = 'a';
493 std::string s;
Bob Wilson7f762182010-12-04 04:40:15 +0000494
Bob Wilson377296e2010-12-02 07:10:39 +0000495 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
496 // Do not create a temporary for an immediate argument.
497 // That would defeat the whole point of using a macro!
498 if (proto[i] == 'i') continue;
499
500 s += TypeString(proto[i], typestr) + " __";
501 s.push_back(arg);
502 s += " = (";
503 s.push_back(arg);
504 s += "); ";
505 }
Bob Wilson7f762182010-12-04 04:40:15 +0000506
Bob Wilson377296e2010-12-02 07:10:39 +0000507 s += "\\\n ";
508 return s;
509}
510
Bob Wilsonc4ba09d2010-12-07 20:02:45 +0000511// Use the vmovl builtin to sign-extend or zero-extend a vector.
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000512static std::string Extend(StringRef typestr, const std::string &a) {
Bob Wilsonc4ba09d2010-12-07 20:02:45 +0000513 std::string s;
514 s = MangleName("vmovl", typestr, ClassS);
515 s += "(" + a + ")";
516 return s;
517}
518
Bob Wilson7f762182010-12-04 04:40:15 +0000519static std::string Duplicate(unsigned nElts, StringRef typestr,
Nate Begemancc3c41a2010-06-12 03:09:49 +0000520 const std::string &a) {
Nate Begeman4b425a82010-06-10 00:16:56 +0000521 std::string s;
Bob Wilson7f762182010-12-04 04:40:15 +0000522
Bob Wilson6904d7a2010-11-16 19:39:14 +0000523 s = "(" + TypeString('d', typestr) + "){ ";
Nate Begeman4b425a82010-06-10 00:16:56 +0000524 for (unsigned i = 0; i != nElts; ++i) {
525 s += a;
526 if ((i + 1) < nElts)
527 s += ", ";
528 }
529 s += " }";
Bob Wilson7f762182010-12-04 04:40:15 +0000530
Nate Begeman4b425a82010-06-10 00:16:56 +0000531 return s;
532}
533
Bob Wilsonb0d98692010-12-03 00:34:12 +0000534static std::string SplatLane(unsigned nElts, const std::string &vec,
535 const std::string &lane) {
536 std::string s = "__builtin_shufflevector(" + vec + ", " + vec;
537 for (unsigned i = 0; i < nElts; ++i)
538 s += ", " + lane;
539 s += ")";
540 return s;
541}
542
Bob Wilsonb308b622010-11-16 23:57:01 +0000543static unsigned GetNumElements(StringRef typestr, bool &quad) {
544 quad = false;
545 bool dummy = false;
Nate Begemancc3c41a2010-06-12 03:09:49 +0000546 char type = ClassifyType(typestr, quad, dummy, dummy);
547 unsigned nElts = 0;
548 switch (type) {
Bob Wilsonb308b622010-11-16 23:57:01 +0000549 case 'c': nElts = 8; break;
550 case 's': nElts = 4; break;
551 case 'i': nElts = 2; break;
552 case 'l': nElts = 1; break;
553 case 'h': nElts = 4; break;
554 case 'f': nElts = 2; break;
555 default:
556 throw "unhandled type!";
557 break;
Nate Begemancc3c41a2010-06-12 03:09:49 +0000558 }
Bob Wilsonb308b622010-11-16 23:57:01 +0000559 if (quad) nElts <<= 1;
560 return nElts;
561}
562
563// Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
564static std::string GenOpString(OpKind op, const std::string &proto,
565 StringRef typestr) {
566 bool quad;
567 unsigned nElts = GetNumElements(typestr, quad);
Bob Wilson7f762182010-12-04 04:40:15 +0000568
Bob Wilson194aa582010-12-03 00:34:09 +0000569 // If this builtin takes an immediate argument, we need to #define it rather
570 // than use a standard declaration, so that SemaChecking can range check
571 // the immediate passed by the user.
572 bool define = proto.find('i') != std::string::npos;
573
Nate Begeman4b425a82010-06-10 00:16:56 +0000574 std::string ts = TypeString(proto[0], typestr);
Bob Wilsonee9ca072010-09-15 01:52:33 +0000575 std::string s;
576 if (op == OpHi || op == OpLo) {
Bob Wilson37a0b542010-12-02 07:44:23 +0000577 s = "union { " + ts + " r; double d; } u; u.d = ";
Bob Wilson194aa582010-12-03 00:34:09 +0000578 } else if (!define) {
Bob Wilson37a0b542010-12-02 07:44:23 +0000579 s = "return ";
Bob Wilsonee9ca072010-09-15 01:52:33 +0000580 }
Bob Wilson7f762182010-12-04 04:40:15 +0000581
Nate Begeman162d3ba2010-06-03 04:04:09 +0000582 switch(op) {
583 case OpAdd:
Bob Wilson194aa582010-12-03 00:34:09 +0000584 s += "__a + __b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000585 break;
Bob Wilsone113ae52010-12-08 00:14:04 +0000586 case OpAddl:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000587 s += Extend(typestr, "__a") + " + " + Extend(typestr, "__b") + ";";
Bob Wilsone113ae52010-12-08 00:14:04 +0000588 break;
589 case OpAddw:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000590 s += "__a + " + Extend(typestr, "__b") + ";";
Bob Wilsone113ae52010-12-08 00:14:04 +0000591 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000592 case OpSub:
Bob Wilson194aa582010-12-03 00:34:09 +0000593 s += "__a - __b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000594 break;
Bob Wilsone113ae52010-12-08 00:14:04 +0000595 case OpSubl:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000596 s += Extend(typestr, "__a") + " - " + Extend(typestr, "__b") + ";";
Bob Wilsone113ae52010-12-08 00:14:04 +0000597 break;
598 case OpSubw:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000599 s += "__a - " + Extend(typestr, "__b") + ";";
Bob Wilsone113ae52010-12-08 00:14:04 +0000600 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000601 case OpMulN:
Bob Wilson194aa582010-12-03 00:34:09 +0000602 s += "__a * " + Duplicate(nElts, typestr, "__b") + ";";
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000603 break;
Bob Wilsonb0d98692010-12-03 00:34:12 +0000604 case OpMulLane:
605 s += "__a * " + SplatLane(nElts, "__b", "__c") + ";";
606 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000607 case OpMul:
Bob Wilson194aa582010-12-03 00:34:09 +0000608 s += "__a * __b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000609 break;
Bob Wilsonc4ba09d2010-12-07 20:02:45 +0000610 case OpMullN:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000611 s += Extend(typestr, "__a") + " * " +
612 Extend(typestr, Duplicate(nElts << (int)quad, typestr, "__b")) + ";";
Bob Wilsonc4ba09d2010-12-07 20:02:45 +0000613 break;
Bob Wilson3467cd02010-12-07 22:02:48 +0000614 case OpMullLane:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000615 s += Extend(typestr, "__a") + " * " +
616 Extend(typestr, SplatLane(nElts, "__b", "__c")) + ";";
Bob Wilson3467cd02010-12-07 22:02:48 +0000617 break;
Bob Wilsonc4ba09d2010-12-07 20:02:45 +0000618 case OpMull:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000619 s += Extend(typestr, "__a") + " * " + Extend(typestr, "__b") + ";";
Bob Wilsonc4ba09d2010-12-07 20:02:45 +0000620 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000621 case OpMlaN:
Bob Wilson194aa582010-12-03 00:34:09 +0000622 s += "__a + (__b * " + Duplicate(nElts, typestr, "__c") + ");";
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000623 break;
Bob Wilsonb0d98692010-12-03 00:34:12 +0000624 case OpMlaLane:
625 s += "__a + (__b * " + SplatLane(nElts, "__c", "__d") + ");";
626 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000627 case OpMla:
Bob Wilson194aa582010-12-03 00:34:09 +0000628 s += "__a + (__b * __c);";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000629 break;
Bob Wilson05843162010-12-07 23:53:37 +0000630 case OpMlalN:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000631 s += "__a + (" + Extend(typestr, "__b") + " * " +
632 Extend(typestr, Duplicate(nElts, typestr, "__c")) + ");";
Bob Wilson05843162010-12-07 23:53:37 +0000633 break;
634 case OpMlalLane:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000635 s += "__a + (" + Extend(typestr, "__b") + " * " +
636 Extend(typestr, SplatLane(nElts, "__c", "__d")) + ");";
Bob Wilson05843162010-12-07 23:53:37 +0000637 break;
638 case OpMlal:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000639 s += "__a + (" + Extend(typestr, "__b") + " * " +
640 Extend(typestr, "__c") + ");";
Bob Wilson05843162010-12-07 23:53:37 +0000641 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000642 case OpMlsN:
Bob Wilson194aa582010-12-03 00:34:09 +0000643 s += "__a - (__b * " + Duplicate(nElts, typestr, "__c") + ");";
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000644 break;
Bob Wilsonb0d98692010-12-03 00:34:12 +0000645 case OpMlsLane:
646 s += "__a - (__b * " + SplatLane(nElts, "__c", "__d") + ");";
647 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000648 case OpMls:
Bob Wilson194aa582010-12-03 00:34:09 +0000649 s += "__a - (__b * __c);";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000650 break;
Bob Wilson05843162010-12-07 23:53:37 +0000651 case OpMlslN:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000652 s += "__a - (" + Extend(typestr, "__b") + " * " +
653 Extend(typestr, Duplicate(nElts, typestr, "__c")) + ");";
Bob Wilson05843162010-12-07 23:53:37 +0000654 break;
655 case OpMlslLane:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000656 s += "__a - (" + Extend(typestr, "__b") + " * " +
657 Extend(typestr, SplatLane(nElts, "__c", "__d")) + ");";
Bob Wilson05843162010-12-07 23:53:37 +0000658 break;
659 case OpMlsl:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000660 s += "__a - (" + Extend(typestr, "__b") + " * " +
661 Extend(typestr, "__c") + ");";
Bob Wilson05843162010-12-07 23:53:37 +0000662 break;
Bob Wilson74410892010-12-08 22:36:08 +0000663 case OpQDMullLane:
664 s += MangleName("vqdmull", typestr, ClassS) + "(__a, " +
665 SplatLane(nElts, "__b", "__c") + ");";
666 break;
667 case OpQDMlalLane:
668 s += MangleName("vqdmlal", typestr, ClassS) + "(__a, " +
669 SplatLane(nElts, "__b", "__c") + ");";
670 break;
671 case OpQDMlslLane:
672 s += MangleName("vqdmlsl", typestr, ClassS) + "(__a, " +
673 SplatLane(nElts, "__b", "__c") + ");";
674 break;
675 case OpQDMulhLane:
676 s += MangleName("vqdmulh", typestr, ClassS) + "(__a, " +
677 SplatLane(nElts, "__b", "__c") + ");";
678 break;
679 case OpQRDMulhLane:
680 s += MangleName("vqrdmulh", typestr, ClassS) + "(__a, " +
681 SplatLane(nElts, "__b", "__c") + ");";
682 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000683 case OpEq:
Bob Wilson194aa582010-12-03 00:34:09 +0000684 s += "(" + ts + ")(__a == __b);";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000685 break;
686 case OpGe:
Bob Wilson194aa582010-12-03 00:34:09 +0000687 s += "(" + ts + ")(__a >= __b);";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000688 break;
689 case OpLe:
Bob Wilson194aa582010-12-03 00:34:09 +0000690 s += "(" + ts + ")(__a <= __b);";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000691 break;
692 case OpGt:
Bob Wilson194aa582010-12-03 00:34:09 +0000693 s += "(" + ts + ")(__a > __b);";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000694 break;
695 case OpLt:
Bob Wilson194aa582010-12-03 00:34:09 +0000696 s += "(" + ts + ")(__a < __b);";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000697 break;
698 case OpNeg:
Bob Wilson194aa582010-12-03 00:34:09 +0000699 s += " -__a;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000700 break;
701 case OpNot:
Bob Wilson194aa582010-12-03 00:34:09 +0000702 s += " ~__a;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000703 break;
704 case OpAnd:
Bob Wilson194aa582010-12-03 00:34:09 +0000705 s += "__a & __b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000706 break;
707 case OpOr:
Bob Wilson194aa582010-12-03 00:34:09 +0000708 s += "__a | __b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000709 break;
710 case OpXor:
Bob Wilson194aa582010-12-03 00:34:09 +0000711 s += "__a ^ __b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000712 break;
713 case OpAndNot:
Bob Wilson194aa582010-12-03 00:34:09 +0000714 s += "__a & ~__b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000715 break;
716 case OpOrNot:
Bob Wilson194aa582010-12-03 00:34:09 +0000717 s += "__a | ~__b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000718 break;
Nate Begeman3861e742010-06-03 21:35:22 +0000719 case OpCast:
Bob Wilson194aa582010-12-03 00:34:09 +0000720 s += "(" + ts + ")__a;";
Nate Begeman3861e742010-06-03 21:35:22 +0000721 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000722 case OpConcat:
Bob Wilson194aa582010-12-03 00:34:09 +0000723 s += "(" + ts + ")__builtin_shufflevector((int64x1_t)__a";
724 s += ", (int64x1_t)__b, 0, 1);";
Nate Begeman900f4672010-06-08 00:14:42 +0000725 break;
Nate Begeman6c060db2010-06-09 01:09:00 +0000726 case OpHi:
Bob Wilson194aa582010-12-03 00:34:09 +0000727 s += "(((float64x2_t)__a)[1]);";
Nate Begeman6c060db2010-06-09 01:09:00 +0000728 break;
729 case OpLo:
Bob Wilson194aa582010-12-03 00:34:09 +0000730 s += "(((float64x2_t)__a)[0]);";
Nate Begeman6c060db2010-06-09 01:09:00 +0000731 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000732 case OpDup:
Bob Wilson194aa582010-12-03 00:34:09 +0000733 s += Duplicate(nElts, typestr, "__a") + ";";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000734 break;
Bob Wilson2196caa2010-12-07 22:39:24 +0000735 case OpDupLane:
736 s += SplatLane(nElts, "__a", "__b") + ";";
737 break;
Nate Begemancc3c41a2010-06-12 03:09:49 +0000738 case OpSelect:
739 // ((0 & 1) | (~0 & 2))
Bob Wilson1dbfa912010-12-02 01:18:20 +0000740 s += "(" + ts + ")";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000741 ts = TypeString(proto[1], typestr);
Bob Wilson194aa582010-12-03 00:34:09 +0000742 s += "((__a & (" + ts + ")__b) | ";
743 s += "(~__a & (" + ts + ")__c));";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000744 break;
745 case OpRev16:
Bob Wilson194aa582010-12-03 00:34:09 +0000746 s += "__builtin_shufflevector(__a, __a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000747 for (unsigned i = 2; i <= nElts; i += 2)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000748 for (unsigned j = 0; j != 2; ++j)
749 s += ", " + utostr(i - j - 1);
Bob Wilson37a0b542010-12-02 07:44:23 +0000750 s += ");";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000751 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000752 case OpRev32: {
753 unsigned WordElts = nElts >> (1 + (int)quad);
Bob Wilson194aa582010-12-03 00:34:09 +0000754 s += "__builtin_shufflevector(__a, __a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000755 for (unsigned i = WordElts; i <= nElts; i += WordElts)
756 for (unsigned j = 0; j != WordElts; ++j)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000757 s += ", " + utostr(i - j - 1);
Bob Wilson37a0b542010-12-02 07:44:23 +0000758 s += ");";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000759 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000760 }
761 case OpRev64: {
762 unsigned DblWordElts = nElts >> (int)quad;
Bob Wilson194aa582010-12-03 00:34:09 +0000763 s += "__builtin_shufflevector(__a, __a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000764 for (unsigned i = DblWordElts; i <= nElts; i += DblWordElts)
765 for (unsigned j = 0; j != DblWordElts; ++j)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000766 s += ", " + utostr(i - j - 1);
Bob Wilson37a0b542010-12-02 07:44:23 +0000767 s += ");";
Nate Begeman900f4672010-06-08 00:14:42 +0000768 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000769 }
Bob Wilsonb4504302010-12-08 21:39:04 +0000770 case OpAbdl: {
771 std::string abd = MangleName("vabd", typestr, ClassS) + "(__a, __b)";
772 if (typestr[0] != 'U') {
773 // vabd results are always unsigned and must be zero-extended.
774 std::string utype = "U" + typestr.str();
775 s += "(" + TypeString(proto[0], typestr) + ")";
776 abd = "(" + TypeString('d', utype) + ")" + abd;
777 s += Extend(utype, abd) + ";";
778 } else {
779 s += Extend(typestr, abd) + ";";
780 }
781 break;
782 }
Bob Wilsonf4f39d32010-12-08 20:09:10 +0000783 case OpAba:
784 s += "__a + " + MangleName("vabd", typestr, ClassS) + "(__b, __c);";
785 break;
Bob Wilsonb4504302010-12-08 21:39:04 +0000786 case OpAbal: {
787 s += "__a + ";
788 std::string abd = MangleName("vabd", typestr, ClassS) + "(__b, __c)";
789 if (typestr[0] != 'U') {
790 // vabd results are always unsigned and must be zero-extended.
791 std::string utype = "U" + typestr.str();
792 s += "(" + TypeString(proto[0], typestr) + ")";
793 abd = "(" + TypeString('d', utype) + ")" + abd;
794 s += Extend(utype, abd) + ";";
795 } else {
796 s += Extend(typestr, abd) + ";";
797 }
798 break;
799 }
Nate Begeman162d3ba2010-06-03 04:04:09 +0000800 default:
801 throw "unknown OpKind!";
802 break;
803 }
Bob Wilson194aa582010-12-03 00:34:09 +0000804 if (op == OpHi || op == OpLo) {
805 if (!define)
806 s += " return";
807 s += " u.r;";
808 }
Nate Begeman162d3ba2010-06-03 04:04:09 +0000809 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000810}
811
Nate Begemanb0a4e452010-06-07 16:00:37 +0000812static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
813 unsigned mod = proto[0];
814 unsigned ret = 0;
815
Nate Begeman900f4672010-06-08 00:14:42 +0000816 if (mod == 'v' || mod == 'f')
Nate Begemanb0a4e452010-06-07 16:00:37 +0000817 mod = proto[1];
818
819 bool quad = false;
820 bool poly = false;
821 bool usgn = false;
822 bool scal = false;
823 bool cnst = false;
824 bool pntr = false;
Bob Wilson7f762182010-12-04 04:40:15 +0000825
Nate Begeman59d70cb2010-08-06 01:24:11 +0000826 // Base type to get the type string for.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000827 char type = ClassifyType(typestr, quad, poly, usgn);
Bob Wilson7f762182010-12-04 04:40:15 +0000828
Nate Begemanb0a4e452010-06-07 16:00:37 +0000829 // Based on the modifying character, change the type and width if necessary.
830 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
Nate Begeman59d70cb2010-08-06 01:24:11 +0000831
Nate Begemanb0a4e452010-06-07 16:00:37 +0000832 if (usgn)
833 ret |= 0x08;
Nate Begeman59d70cb2010-08-06 01:24:11 +0000834 if (quad && proto[1] != 'g')
Nate Begemanb0a4e452010-06-07 16:00:37 +0000835 ret |= 0x10;
Bob Wilson7f762182010-12-04 04:40:15 +0000836
Nate Begemanb0a4e452010-06-07 16:00:37 +0000837 switch (type) {
Bob Wilson7f762182010-12-04 04:40:15 +0000838 case 'c':
Nate Begemanb0a4e452010-06-07 16:00:37 +0000839 ret |= poly ? 5 : 0;
840 break;
841 case 's':
842 ret |= poly ? 6 : 1;
843 break;
844 case 'i':
845 ret |= 2;
846 break;
847 case 'l':
848 ret |= 3;
849 break;
850 case 'h':
851 ret |= 7;
852 break;
853 case 'f':
854 ret |= 4;
855 break;
856 default:
857 throw "unhandled type!";
858 break;
859 }
860 return ret;
861}
862
Nate Begeman7c8c8832010-06-02 21:53:00 +0000863// Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
Nate Begeman7c8c8832010-06-02 21:53:00 +0000864static std::string GenBuiltin(const std::string &name, const std::string &proto,
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000865 StringRef typestr, ClassKind ck) {
Nate Begeman162d3ba2010-06-03 04:04:09 +0000866 std::string s;
Nate Begeman9e584b32010-06-04 22:53:30 +0000867
Nate Begemanc4a1b652010-06-20 21:09:52 +0000868 // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
869 // sret-like argument.
Bob Wilson5b7fe592010-12-01 19:49:51 +0000870 bool sret = (proto[0] >= '2' && proto[0] <= '4');
Nate Begemanc4a1b652010-06-20 21:09:52 +0000871
872 // If this builtin takes an immediate argument, we need to #define it rather
873 // than use a standard declaration, so that SemaChecking can range check
874 // the immediate passed by the user.
Nate Begeman6c060db2010-06-09 01:09:00 +0000875 bool define = proto.find('i') != std::string::npos;
Nate Begeman9e584b32010-06-04 22:53:30 +0000876
Bob Wilson95148ad2010-12-01 19:49:56 +0000877 // Check if the prototype has a scalar operand with the type of the vector
878 // elements. If not, bitcasting the args will take care of arg checking.
879 // The actual signedness etc. will be taken care of with special enums.
Nate Begeman9e584b32010-06-04 22:53:30 +0000880 if (proto.find('s') == std::string::npos)
881 ck = ClassB;
Nate Begeman7c21f742010-06-04 21:36:00 +0000882
Nate Begeman162d3ba2010-06-03 04:04:09 +0000883 if (proto[0] != 'v') {
Nate Begeman6c060db2010-06-09 01:09:00 +0000884 std::string ts = TypeString(proto[0], typestr);
Bob Wilson7f762182010-12-04 04:40:15 +0000885
Nate Begeman6c060db2010-06-09 01:09:00 +0000886 if (define) {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000887 if (sret)
Bob Wilson052008b2010-12-02 02:42:51 +0000888 s += ts + " r; ";
Bob Wilsone106bc12010-12-02 00:24:56 +0000889 else
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000890 s += "(" + ts + ")";
Nate Begemanc4a1b652010-06-20 21:09:52 +0000891 } else if (sret) {
892 s += ts + " r; ";
Nate Begeman9e584b32010-06-04 22:53:30 +0000893 } else {
Bob Wilson37a0b542010-12-02 07:44:23 +0000894 s += "return (" + ts + ")";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000895 }
Nate Begeman4b425a82010-06-10 00:16:56 +0000896 }
Bob Wilson7f762182010-12-04 04:40:15 +0000897
Nate Begeman4b425a82010-06-10 00:16:56 +0000898 bool splat = proto.find('a') != std::string::npos;
Bob Wilson7f762182010-12-04 04:40:15 +0000899
Nate Begeman7c8c8832010-06-02 21:53:00 +0000900 s += "__builtin_neon_";
Nate Begeman4b425a82010-06-10 00:16:56 +0000901 if (splat) {
Bob Wilson95148ad2010-12-01 19:49:56 +0000902 // Call the non-splat builtin: chop off the "_n" suffix from the name.
Nate Begeman4b425a82010-06-10 00:16:56 +0000903 std::string vname(name, 0, name.size()-2);
904 s += MangleName(vname, typestr, ck);
905 } else {
906 s += MangleName(name, typestr, ck);
907 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000908 s += "(";
Nate Begemanc4a1b652010-06-20 21:09:52 +0000909
910 // Pass the address of the return variable as the first argument to sret-like
911 // builtins.
912 if (sret)
913 s += "&r, ";
Bob Wilson7f762182010-12-04 04:40:15 +0000914
Bob Wilson377296e2010-12-02 07:10:39 +0000915 char arg = 'a';
Nate Begeman7c8c8832010-06-02 21:53:00 +0000916 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
Nate Begemancc3c41a2010-06-12 03:09:49 +0000917 std::string args = std::string(&arg, 1);
Bob Wilson95148ad2010-12-01 19:49:56 +0000918
Bob Wilson194aa582010-12-03 00:34:09 +0000919 // Use the local temporaries instead of the macro arguments.
920 args = "__" + args;
Bob Wilsonf00140c2010-12-01 19:49:58 +0000921
922 bool argQuad = false;
923 bool argPoly = false;
924 bool argUsgn = false;
925 bool argScalar = false;
926 bool dummy = false;
927 char argType = ClassifyType(typestr, argQuad, argPoly, argUsgn);
928 argType = ModType(proto[i], argType, argQuad, argPoly, argUsgn, argScalar,
929 dummy, dummy);
930
Nate Begeman9e584b32010-06-04 22:53:30 +0000931 // Handle multiple-vector values specially, emitting each subvector as an
932 // argument to the __builtin.
Bob Wilson5b7fe592010-12-01 19:49:51 +0000933 if (proto[i] >= '2' && proto[i] <= '4') {
Bob Wilsonf00140c2010-12-01 19:49:58 +0000934 // Check if an explicit cast is needed.
935 if (argType != 'c' || argPoly || argUsgn)
936 args = (argQuad ? "(int8x16_t)" : "(int8x8_t)") + args;
937
Nate Begeman9e584b32010-06-04 22:53:30 +0000938 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000939 s += args + ".val[" + utostr(vi) + "]";
Nate Begeman9e584b32010-06-04 22:53:30 +0000940 if ((vi + 1) < ve)
941 s += ", ";
942 }
943 if ((i + 1) < e)
944 s += ", ";
945
946 continue;
947 }
Bob Wilson7f762182010-12-04 04:40:15 +0000948
Bob Wilson4a6c7fc2010-12-02 01:18:23 +0000949 if (splat && (i + 1) == e)
950 args = Duplicate(GetNumElements(typestr, argQuad), typestr, args);
951
Bob Wilsonf00140c2010-12-01 19:49:58 +0000952 // Check if an explicit cast is needed.
Bob Wilson4a6c7fc2010-12-02 01:18:23 +0000953 if ((splat || !argScalar) &&
Bob Wilsonf00140c2010-12-01 19:49:58 +0000954 ((ck == ClassB && argType != 'c') || argPoly || argUsgn)) {
955 std::string argTypeStr = "c";
956 if (ck != ClassB)
957 argTypeStr = argType;
958 if (argQuad)
959 argTypeStr = "Q" + argTypeStr;
960 args = "(" + TypeString('d', argTypeStr) + ")" + args;
961 }
Bob Wilson7f762182010-12-04 04:40:15 +0000962
Bob Wilson4a6c7fc2010-12-02 01:18:23 +0000963 s += args;
Nate Begeman7c8c8832010-06-02 21:53:00 +0000964 if ((i + 1) < e)
965 s += ", ";
966 }
Bob Wilson7f762182010-12-04 04:40:15 +0000967
Nate Begeman9e584b32010-06-04 22:53:30 +0000968 // Extra constant integer to hold type class enum for this function, e.g. s8
Nate Begeman9e584b32010-06-04 22:53:30 +0000969 if (ck == ClassB)
Nate Begemanb0a4e452010-06-07 16:00:37 +0000970 s += ", " + utostr(GetNeonEnum(proto, typestr));
Bob Wilson7f762182010-12-04 04:40:15 +0000971
Bob Wilson052008b2010-12-02 02:42:51 +0000972 s += ");";
Nate Begeman9e584b32010-06-04 22:53:30 +0000973
Bob Wilson37a0b542010-12-02 07:44:23 +0000974 if (proto[0] != 'v' && sret) {
975 if (define)
976 s += " r;";
977 else
Nate Begemanc4a1b652010-06-20 21:09:52 +0000978 s += " return r;";
Nate Begeman9e584b32010-06-04 22:53:30 +0000979 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000980 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000981}
982
Bob Wilson7f762182010-12-04 04:40:15 +0000983static std::string GenBuiltinDef(const std::string &name,
Nate Begeman73cef3e2010-06-04 01:26:15 +0000984 const std::string &proto,
985 StringRef typestr, ClassKind ck) {
986 std::string s("BUILTIN(__builtin_neon_");
Nate Begeman92f98af2010-06-04 07:11:25 +0000987
Bob Wilson7f762182010-12-04 04:40:15 +0000988 // If all types are the same size, bitcasting the args will take care
Nate Begeman92f98af2010-06-04 07:11:25 +0000989 // of arg checking. The actual signedness etc. will be taken care of with
990 // special enums.
991 if (proto.find('s') == std::string::npos)
992 ck = ClassB;
Bob Wilson7f762182010-12-04 04:40:15 +0000993
Nate Begeman73cef3e2010-06-04 01:26:15 +0000994 s += MangleName(name, typestr, ck);
995 s += ", \"";
Bob Wilson7f762182010-12-04 04:40:15 +0000996
Nate Begeman92f98af2010-06-04 07:11:25 +0000997 for (unsigned i = 0, e = proto.size(); i != e; ++i)
Nate Begeman7c21f742010-06-04 21:36:00 +0000998 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
999
1000 // Extra constant integer to hold type class enum for this function, e.g. s8
1001 if (ck == ClassB)
1002 s += "i";
Bob Wilson7f762182010-12-04 04:40:15 +00001003
Nate Begeman73cef3e2010-06-04 01:26:15 +00001004 s += "\", \"n\")";
1005 return s;
1006}
1007
Bob Wilson3890e392010-12-07 01:12:23 +00001008static std::string GenIntrinsic(const std::string &name,
1009 const std::string &proto,
1010 StringRef outTypeStr, StringRef inTypeStr,
1011 OpKind kind, ClassKind classKind) {
1012 assert(!proto.empty() && "");
1013 bool define = proto.find('i') != std::string::npos;
1014 std::string s;
1015
1016 // static always inline + return type
1017 if (define)
1018 s += "#define ";
1019 else
1020 s += "__ai " + TypeString(proto[0], outTypeStr) + " ";
1021
1022 // Function name with type suffix
1023 std::string mangledName = MangleName(name, outTypeStr, ClassS);
1024 if (outTypeStr != inTypeStr) {
1025 // If the input type is different (e.g., for vreinterpret), append a suffix
1026 // for the input type. String off a "Q" (quad) prefix so that MangleName
1027 // does not insert another "q" in the name.
1028 unsigned typeStrOff = (inTypeStr[0] == 'Q' ? 1 : 0);
1029 StringRef inTypeNoQuad = inTypeStr.substr(typeStrOff);
1030 mangledName = MangleName(mangledName, inTypeNoQuad, ClassS);
1031 }
1032 s += mangledName;
1033
1034 // Function arguments
1035 s += GenArgs(proto, inTypeStr);
1036
1037 // Definition.
1038 if (define) {
1039 s += " __extension__ ({ \\\n ";
1040 s += GenMacroLocals(proto, inTypeStr);
1041 } else {
1042 s += " { \\\n ";
1043 }
1044
1045 if (kind != OpNone)
1046 s += GenOpString(kind, proto, outTypeStr);
1047 else
1048 s += GenBuiltin(name, proto, outTypeStr, classKind);
1049 if (define)
1050 s += " })";
1051 else
1052 s += " }";
1053 s += "\n";
1054 return s;
1055}
1056
Nate Begemand72c9002010-06-13 04:47:03 +00001057/// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h
1058/// is comprised of type definitions and function declarations.
Nate Begeman5ddb0872010-05-28 01:08:32 +00001059void NeonEmitter::run(raw_ostream &OS) {
Bob Wilson0e1fb7a2010-12-09 18:31:01 +00001060 OS <<
1061 "/*===---- arm_neon.h - ARM Neon intrinsics ---------------------------------===\n"
1062 " *\n"
1063 " * Permission is hereby granted, free of charge, to any person obtaining a copy\n"
1064 " * of this software and associated documentation files (the \"Software\"), to deal\n"
1065 " * in the Software without restriction, including without limitation the rights\n"
1066 " * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n"
1067 " * copies of the Software, and to permit persons to whom the Software is\n"
1068 " * furnished to do so, subject to the following conditions:\n"
1069 " *\n"
1070 " * The above copyright notice and this permission notice shall be included in\n"
1071 " * all copies or substantial portions of the Software.\n"
1072 " *\n"
1073 " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n"
1074 " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n"
1075 " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n"
1076 " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n"
1077 " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n"
1078 " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n"
1079 " * THE SOFTWARE.\n"
1080 " *\n"
1081 " *===-----------------------------------------------------------------------===\n"
1082 " */\n\n";
Bob Wilson7f762182010-12-04 04:40:15 +00001083
Nate Begeman5ddb0872010-05-28 01:08:32 +00001084 OS << "#ifndef __ARM_NEON_H\n";
1085 OS << "#define __ARM_NEON_H\n\n";
Bob Wilson7f762182010-12-04 04:40:15 +00001086
Nate Begeman5ddb0872010-05-28 01:08:32 +00001087 OS << "#ifndef __ARM_NEON__\n";
1088 OS << "#error \"NEON support not enabled\"\n";
1089 OS << "#endif\n\n";
1090
1091 OS << "#include <stdint.h>\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +00001092
1093 // Emit NEON-specific scalar typedefs.
Nate Begeman7c8c8832010-06-02 21:53:00 +00001094 OS << "typedef float float32_t;\n";
Bob Wilson4fbf6382010-11-16 23:57:03 +00001095 OS << "typedef int8_t poly8_t;\n";
1096 OS << "typedef int16_t poly16_t;\n";
Nate Begeman162d3ba2010-06-03 04:04:09 +00001097 OS << "typedef uint16_t float16_t;\n";
Nate Begeman9e584b32010-06-04 22:53:30 +00001098
Nate Begeman7c8c8832010-06-02 21:53:00 +00001099 // Emit Neon vector typedefs.
1100 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
1101 SmallVector<StringRef, 24> TDTypeVec;
1102 ParseTypes(0, TypedefTypes, TDTypeVec);
1103
1104 // Emit vector typedefs.
Bob Wilson6904d7a2010-11-16 19:39:14 +00001105 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
Bob Wilson6a8aceb2010-11-16 23:57:06 +00001106 bool dummy, quad = false, poly = false;
1107 (void) ClassifyType(TDTypeVec[i], quad, poly, dummy);
1108 if (poly)
1109 OS << "typedef __attribute__((neon_polyvector_type(";
1110 else
1111 OS << "typedef __attribute__((neon_vector_type(";
Bob Wilson7f762182010-12-04 04:40:15 +00001112
Bob Wilson6a8aceb2010-11-16 23:57:06 +00001113 unsigned nElts = GetNumElements(TDTypeVec[i], quad);
1114 OS << utostr(nElts) << "))) ";
1115 if (nElts < 10)
Bob Wilson6904d7a2010-11-16 19:39:14 +00001116 OS << " ";
Bob Wilson7f762182010-12-04 04:40:15 +00001117
Bob Wilson6904d7a2010-11-16 19:39:14 +00001118 OS << TypeString('s', TDTypeVec[i]);
1119 OS << " " << TypeString('d', TDTypeVec[i]) << ";\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +00001120 }
1121 OS << "\n";
Bob Wilson6a8aceb2010-11-16 23:57:06 +00001122 OS << "typedef __attribute__((__vector_size__(8))) "
Bob Wilson6904d7a2010-11-16 19:39:14 +00001123 "double float64x1_t;\n";
Bob Wilson6a8aceb2010-11-16 23:57:06 +00001124 OS << "typedef __attribute__((__vector_size__(16))) "
Bob Wilson6904d7a2010-11-16 19:39:14 +00001125 "double float64x2_t;\n";
Bob Wilsonee9ca072010-09-15 01:52:33 +00001126 OS << "\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +00001127
1128 // Emit struct typedefs.
Bob Wilson6904d7a2010-11-16 19:39:14 +00001129 for (unsigned vi = 2; vi != 5; ++vi) {
Nate Begeman7c8c8832010-06-02 21:53:00 +00001130 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
Bob Wilson6904d7a2010-11-16 19:39:14 +00001131 std::string ts = TypeString('d', TDTypeVec[i]);
1132 std::string vs = TypeString('0' + vi, TDTypeVec[i]);
1133 OS << "typedef struct " << vs << " {\n";
1134 OS << " " << ts << " val";
1135 OS << "[" << utostr(vi) << "]";
1136 OS << ";\n} ";
Bob Wilsonf18dfec2010-11-16 19:16:06 +00001137 OS << vs << ";\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +00001138 }
1139 }
Bob Wilson7f762182010-12-04 04:40:15 +00001140
Nate Begeman7c8c8832010-06-02 21:53:00 +00001141 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
1142
Nate Begeman5ddb0872010-05-28 01:08:32 +00001143 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
Bob Wilson7f762182010-12-04 04:40:15 +00001144
Bob Wilsonf4f39d32010-12-08 20:09:10 +00001145 // Emit vmovl and vabd intrinsics first so they can be used by other
Bob Wilson74410892010-12-08 22:36:08 +00001146 // intrinsics. (Some of the saturating multiply instructions are also
1147 // used to implement the corresponding "_lane" variants, but tablegen
1148 // sorts the records into alphabetical order so that the "_lane" variants
1149 // come after the intrinsics they use.)
Bob Wilsonda1d3dc2010-12-07 23:53:32 +00001150 emitIntrinsic(OS, Records.getDef("VMOVL"));
Bob Wilsonf4f39d32010-12-08 20:09:10 +00001151 emitIntrinsic(OS, Records.getDef("VABD"));
Bob Wilsonda1d3dc2010-12-07 23:53:32 +00001152
Nate Begeman5ddb0872010-05-28 01:08:32 +00001153 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1154 Record *R = RV[i];
Bob Wilsonf4f39d32010-12-08 20:09:10 +00001155 if (R->getName() != "VMOVL" && R->getName() != "VABD")
Bob Wilsonda1d3dc2010-12-07 23:53:32 +00001156 emitIntrinsic(OS, R);
Nate Begeman5ddb0872010-05-28 01:08:32 +00001157 }
Bob Wilsonda1d3dc2010-12-07 23:53:32 +00001158
Nate Begeman73cef3e2010-06-04 01:26:15 +00001159 OS << "#undef __ai\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +00001160 OS << "#endif /* __ARM_NEON_H */\n";
Nate Begeman5ddb0872010-05-28 01:08:32 +00001161}
Nate Begemana8979a02010-06-04 00:21:41 +00001162
Bob Wilsonda1d3dc2010-12-07 23:53:32 +00001163/// emitIntrinsic - Write out the arm_neon.h header file definitions for the
1164/// intrinsics specified by record R.
1165void NeonEmitter::emitIntrinsic(raw_ostream &OS, Record *R) {
1166 std::string name = R->getValueAsString("Name");
1167 std::string Proto = R->getValueAsString("Prototype");
1168 std::string Types = R->getValueAsString("Types");
1169
1170 SmallVector<StringRef, 16> TypeVec;
1171 ParseTypes(R, Types, TypeVec);
1172
1173 OpKind kind = OpMap[R->getValueAsDef("Operand")->getName()];
1174
1175 ClassKind classKind = ClassNone;
1176 if (R->getSuperClasses().size() >= 2)
1177 classKind = ClassMap[R->getSuperClasses()[1]];
1178 if (classKind == ClassNone && kind == OpNone)
1179 throw TGError(R->getLoc(), "Builtin has no class kind");
1180
1181 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1182 if (kind == OpReinterpret) {
1183 bool outQuad = false;
1184 bool dummy = false;
1185 (void)ClassifyType(TypeVec[ti], outQuad, dummy, dummy);
1186 for (unsigned srcti = 0, srcte = TypeVec.size();
1187 srcti != srcte; ++srcti) {
1188 bool inQuad = false;
1189 (void)ClassifyType(TypeVec[srcti], inQuad, dummy, dummy);
1190 if (srcti == ti || inQuad != outQuad)
1191 continue;
1192 OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[srcti],
1193 OpCast, ClassS);
1194 }
1195 } else {
1196 OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[ti],
1197 kind, classKind);
1198 }
1199 }
1200 OS << "\n";
1201}
1202
Nate Begeman918f8e42010-06-14 05:17:23 +00001203static unsigned RangeFromType(StringRef typestr) {
1204 // base type to get the type string for.
1205 bool quad = false, dummy = false;
1206 char type = ClassifyType(typestr, quad, dummy, dummy);
Bob Wilson7f762182010-12-04 04:40:15 +00001207
Nate Begeman918f8e42010-06-14 05:17:23 +00001208 switch (type) {
1209 case 'c':
Nate Begeman4da883a2010-06-15 22:10:31 +00001210 return (8 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001211 case 'h':
1212 case 's':
Nate Begeman4da883a2010-06-15 22:10:31 +00001213 return (4 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001214 case 'f':
1215 case 'i':
Nate Begeman4da883a2010-06-15 22:10:31 +00001216 return (2 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001217 case 'l':
Nate Begeman4da883a2010-06-15 22:10:31 +00001218 return (1 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001219 default:
1220 throw "unhandled type!";
1221 break;
1222 }
Bob Wilsonfdb530d2010-07-28 18:21:10 +00001223 assert(0 && "unreachable");
1224 return 0;
Nate Begeman918f8e42010-06-14 05:17:23 +00001225}
1226
Nate Begemanf8c4c272010-06-17 04:15:13 +00001227/// runHeader - Emit a file with sections defining:
1228/// 1. the NEON section of BuiltinsARM.def.
1229/// 2. the SemaChecking code for the type overload checking.
1230/// 3. the SemaChecking code for validation of intrinsic immedate arguments.
Nate Begemana8979a02010-06-04 00:21:41 +00001231void NeonEmitter::runHeader(raw_ostream &OS) {
Nate Begeman73cef3e2010-06-04 01:26:15 +00001232 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1233
1234 StringMap<OpKind> EmittedMap;
Bob Wilson7f762182010-12-04 04:40:15 +00001235
Nate Begemanf8c4c272010-06-17 04:15:13 +00001236 // Generate BuiltinsARM.def for NEON
1237 OS << "#ifdef GET_NEON_BUILTINS\n";
Nate Begeman73cef3e2010-06-04 01:26:15 +00001238 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1239 Record *R = RV[i];
Nate Begeman73cef3e2010-06-04 01:26:15 +00001240 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1241 if (k != OpNone)
1242 continue;
Nate Begeman73cef3e2010-06-04 01:26:15 +00001243
Nate Begemanf8c4c272010-06-17 04:15:13 +00001244 std::string Proto = R->getValueAsString("Prototype");
Bob Wilson7f762182010-12-04 04:40:15 +00001245
Nate Begemand72c9002010-06-13 04:47:03 +00001246 // Functions with 'a' (the splat code) in the type prototype should not get
1247 // their own builtin as they use the non-splat variant.
Nate Begeman4b425a82010-06-10 00:16:56 +00001248 if (Proto.find('a') != std::string::npos)
1249 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001250
Nate Begemanf8c4c272010-06-17 04:15:13 +00001251 std::string Types = R->getValueAsString("Types");
Nate Begeman73cef3e2010-06-04 01:26:15 +00001252 SmallVector<StringRef, 16> TypeVec;
1253 ParseTypes(R, Types, TypeVec);
Bob Wilson7f762182010-12-04 04:40:15 +00001254
Nate Begeman73cef3e2010-06-04 01:26:15 +00001255 if (R->getSuperClasses().size() < 2)
1256 throw TGError(R->getLoc(), "Builtin has no class kind");
Bob Wilson7f762182010-12-04 04:40:15 +00001257
Bob Wilsonce0bb542010-12-03 17:19:39 +00001258 std::string name = R->getValueAsString("Name");
Nate Begeman73cef3e2010-06-04 01:26:15 +00001259 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
Bob Wilson7f762182010-12-04 04:40:15 +00001260
Nate Begeman73cef3e2010-06-04 01:26:15 +00001261 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
Nate Begemand72c9002010-06-13 04:47:03 +00001262 // Generate the BuiltinsARM.def declaration for this builtin, ensuring
1263 // that each unique BUILTIN() macro appears only once in the output
1264 // stream.
Nate Begeman73cef3e2010-06-04 01:26:15 +00001265 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
1266 if (EmittedMap.count(bd))
1267 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001268
Nate Begeman73cef3e2010-06-04 01:26:15 +00001269 EmittedMap[bd] = OpNone;
1270 OS << bd << "\n";
1271 }
Nate Begemanf8c4c272010-06-17 04:15:13 +00001272 }
1273 OS << "#endif\n\n";
Bob Wilson7f762182010-12-04 04:40:15 +00001274
Nate Begemanf8c4c272010-06-17 04:15:13 +00001275 // Generate the overloaded type checking code for SemaChecking.cpp
1276 OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
1277 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1278 Record *R = RV[i];
1279 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1280 if (k != OpNone)
Nate Begemand72c9002010-06-13 04:47:03 +00001281 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001282
Nate Begemanf8c4c272010-06-17 04:15:13 +00001283 std::string Proto = R->getValueAsString("Prototype");
1284 std::string Types = R->getValueAsString("Types");
Bob Wilsonce0bb542010-12-03 17:19:39 +00001285 std::string name = R->getValueAsString("Name");
Bob Wilson7f762182010-12-04 04:40:15 +00001286
Nate Begemanf8c4c272010-06-17 04:15:13 +00001287 // Functions with 'a' (the splat code) in the type prototype should not get
1288 // their own builtin as they use the non-splat variant.
1289 if (Proto.find('a') != std::string::npos)
1290 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001291
Nate Begemanf8c4c272010-06-17 04:15:13 +00001292 // Functions which have a scalar argument cannot be overloaded, no need to
1293 // check them if we are emitting the type checking code.
1294 if (Proto.find('s') != std::string::npos)
1295 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001296
Nate Begemanf8c4c272010-06-17 04:15:13 +00001297 SmallVector<StringRef, 16> TypeVec;
1298 ParseTypes(R, Types, TypeVec);
Bob Wilson7f762182010-12-04 04:40:15 +00001299
Nate Begemanf8c4c272010-06-17 04:15:13 +00001300 if (R->getSuperClasses().size() < 2)
1301 throw TGError(R->getLoc(), "Builtin has no class kind");
Bob Wilson7f762182010-12-04 04:40:15 +00001302
Nate Begemanf8c4c272010-06-17 04:15:13 +00001303 int si = -1, qi = -1;
1304 unsigned mask = 0, qmask = 0;
1305 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1306 // Generate the switch case(s) for this builtin for the type validation.
1307 bool quad = false, poly = false, usgn = false;
1308 (void) ClassifyType(TypeVec[ti], quad, poly, usgn);
Bob Wilson7f762182010-12-04 04:40:15 +00001309
Nate Begemanf8c4c272010-06-17 04:15:13 +00001310 if (quad) {
1311 qi = ti;
1312 qmask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1313 } else {
1314 si = ti;
1315 mask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1316 }
1317 }
1318 if (mask)
Bob Wilson7f762182010-12-04 04:40:15 +00001319 OS << "case ARM::BI__builtin_neon_"
Bob Wilsond8b84702010-12-07 01:12:19 +00001320 << MangleName(name, TypeVec[si], ClassB)
1321 << ": mask = " << "0x" << utohexstr(mask) << "; break;\n";
Nate Begemanf8c4c272010-06-17 04:15:13 +00001322 if (qmask)
Bob Wilson7f762182010-12-04 04:40:15 +00001323 OS << "case ARM::BI__builtin_neon_"
Bob Wilsond8b84702010-12-07 01:12:19 +00001324 << MangleName(name, TypeVec[qi], ClassB)
1325 << ": mask = " << "0x" << utohexstr(qmask) << "; break;\n";
Nate Begemanf8c4c272010-06-17 04:15:13 +00001326 }
1327 OS << "#endif\n\n";
Bob Wilson7f762182010-12-04 04:40:15 +00001328
Nate Begemanf8c4c272010-06-17 04:15:13 +00001329 // Generate the intrinsic range checking code for shift/lane immediates.
1330 OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
1331 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1332 Record *R = RV[i];
Bob Wilson7f762182010-12-04 04:40:15 +00001333
Nate Begemanf8c4c272010-06-17 04:15:13 +00001334 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1335 if (k != OpNone)
1336 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001337
Bob Wilsonce0bb542010-12-03 17:19:39 +00001338 std::string name = R->getValueAsString("Name");
Nate Begemanf8c4c272010-06-17 04:15:13 +00001339 std::string Proto = R->getValueAsString("Prototype");
1340 std::string Types = R->getValueAsString("Types");
Bob Wilson7f762182010-12-04 04:40:15 +00001341
Nate Begemanf8c4c272010-06-17 04:15:13 +00001342 // Functions with 'a' (the splat code) in the type prototype should not get
1343 // their own builtin as they use the non-splat variant.
1344 if (Proto.find('a') != std::string::npos)
1345 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001346
Nate Begemanf8c4c272010-06-17 04:15:13 +00001347 // Functions which do not have an immediate do not need to have range
1348 // checking code emitted.
1349 if (Proto.find('i') == std::string::npos)
1350 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001351
Nate Begemanf8c4c272010-06-17 04:15:13 +00001352 SmallVector<StringRef, 16> TypeVec;
1353 ParseTypes(R, Types, TypeVec);
Bob Wilson7f762182010-12-04 04:40:15 +00001354
Nate Begemanf8c4c272010-06-17 04:15:13 +00001355 if (R->getSuperClasses().size() < 2)
1356 throw TGError(R->getLoc(), "Builtin has no class kind");
Bob Wilson7f762182010-12-04 04:40:15 +00001357
Nate Begemanf8c4c272010-06-17 04:15:13 +00001358 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
Bob Wilson7f762182010-12-04 04:40:15 +00001359
Nate Begemanf8c4c272010-06-17 04:15:13 +00001360 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1361 std::string namestr, shiftstr, rangestr;
Bob Wilson7f762182010-12-04 04:40:15 +00001362
Nate Begemanf8c4c272010-06-17 04:15:13 +00001363 // Builtins which are overloaded by type will need to have their upper
1364 // bound computed at Sema time based on the type constant.
1365 if (Proto.find('s') == std::string::npos) {
1366 ck = ClassB;
1367 if (R->getValueAsBit("isShift")) {
1368 shiftstr = ", true";
Bob Wilson7f762182010-12-04 04:40:15 +00001369
Nate Begemanf8c4c272010-06-17 04:15:13 +00001370 // Right shifts have an 'r' in the name, left shifts do not.
1371 if (name.find('r') != std::string::npos)
1372 rangestr = "l = 1; ";
1373 }
1374 rangestr += "u = RFT(TV" + shiftstr + ")";
1375 } else {
1376 rangestr = "u = " + utostr(RangeFromType(TypeVec[ti]));
1377 }
Nate Begemanc4a1b652010-06-20 21:09:52 +00001378 // Make sure cases appear only once by uniquing them in a string map.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001379 namestr = MangleName(name, TypeVec[ti], ck);
1380 if (EmittedMap.count(namestr))
1381 continue;
1382 EmittedMap[namestr] = OpNone;
Nate Begemanc4a1b652010-06-20 21:09:52 +00001383
1384 // Calculate the index of the immediate that should be range checked.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001385 unsigned immidx = 0;
Bob Wilson7f762182010-12-04 04:40:15 +00001386
Nate Begemanc4a1b652010-06-20 21:09:52 +00001387 // Builtins that return a struct of multiple vectors have an extra
1388 // leading arg for the struct return.
Bob Wilson5b7fe592010-12-01 19:49:51 +00001389 if (Proto[0] >= '2' && Proto[0] <= '4')
Nate Begemanc4a1b652010-06-20 21:09:52 +00001390 ++immidx;
Bob Wilson7f762182010-12-04 04:40:15 +00001391
1392 // Add one to the index for each argument until we reach the immediate
Nate Begemanc4a1b652010-06-20 21:09:52 +00001393 // to be checked. Structs of vectors are passed as multiple arguments.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001394 for (unsigned ii = 1, ie = Proto.size(); ii != ie; ++ii) {
1395 switch (Proto[ii]) {
1396 default: immidx += 1; break;
1397 case '2': immidx += 2; break;
1398 case '3': immidx += 3; break;
1399 case '4': immidx += 4; break;
1400 case 'i': ie = ii + 1; break;
1401 }
1402 }
Bob Wilsond8b84702010-12-07 01:12:19 +00001403 OS << "case ARM::BI__builtin_neon_" << MangleName(name, TypeVec[ti], ck)
Nate Begemanf8c4c272010-06-17 04:15:13 +00001404 << ": i = " << immidx << "; " << rangestr << "; break;\n";
Nate Begemand72c9002010-06-13 04:47:03 +00001405 }
Nate Begeman73cef3e2010-06-04 01:26:15 +00001406 }
Nate Begemanf8c4c272010-06-17 04:15:13 +00001407 OS << "#endif\n\n";
Nate Begemana8979a02010-06-04 00:21:41 +00001408}