blob: 64224d9e51d0b79e78944b5144dfc7050899ab38 [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
Bob Wilson333f5192010-12-15 16:58:45 +000021// called, rather than the normal run() entry point. A complete set of tests
22// for Neon intrinsics can be generated by calling the runTests() entry point.
Nate Begemand72c9002010-06-13 04:47:03 +000023//
Nate Begeman5ddb0872010-05-28 01:08:32 +000024//===----------------------------------------------------------------------===//
25
26#include "NeonEmitter.h"
Nate Begeman22237772010-06-02 00:34:55 +000027#include "llvm/ADT/SmallString.h"
28#include "llvm/ADT/SmallVector.h"
Nate Begeman5ddb0872010-05-28 01:08:32 +000029#include "llvm/ADT/StringExtras.h"
Nate Begeman5ddb0872010-05-28 01:08:32 +000030#include <string>
31
32using namespace llvm;
33
Nate Begemand72c9002010-06-13 04:47:03 +000034/// ParseTypes - break down a string such as "fQf" into a vector of StringRefs,
35/// which each StringRef representing a single type declared in the string.
36/// for "fQf" we would end up with 2 StringRefs, "f", and "Qf", representing
37/// 2xfloat and 4xfloat respectively.
Nate Begeman22237772010-06-02 00:34:55 +000038static void ParseTypes(Record *r, std::string &s,
39 SmallVectorImpl<StringRef> &TV) {
40 const char *data = s.data();
41 int len = 0;
Bob Wilson7f762182010-12-04 04:40:15 +000042
Nate Begeman22237772010-06-02 00:34:55 +000043 for (unsigned i = 0, e = s.size(); i != e; ++i, ++len) {
44 if (data[len] == 'P' || data[len] == 'Q' || data[len] == 'U')
45 continue;
Bob Wilson7f762182010-12-04 04:40:15 +000046
Nate Begeman22237772010-06-02 00:34:55 +000047 switch (data[len]) {
48 case 'c':
49 case 's':
50 case 'i':
51 case 'l':
52 case 'h':
53 case 'f':
54 break;
55 default:
56 throw TGError(r->getLoc(),
57 "Unexpected letter: " + std::string(data + len, 1));
58 break;
59 }
60 TV.push_back(StringRef(data, len + 1));
61 data += len + 1;
62 len = -1;
63 }
64}
65
Nate Begemand72c9002010-06-13 04:47:03 +000066/// Widen - Convert a type code into the next wider type. char -> short,
67/// short -> int, etc.
Duncan Sands8dbbace2010-06-02 08:37:30 +000068static char Widen(const char t) {
Nate Begeman22237772010-06-02 00:34:55 +000069 switch (t) {
70 case 'c':
71 return 's';
72 case 's':
73 return 'i';
74 case 'i':
75 return 'l';
Bob Wilsonae6be762010-12-15 23:16:25 +000076 case 'h':
77 return 'f';
Nate Begeman22237772010-06-02 00:34:55 +000078 default: throw "unhandled type in widen!";
79 }
80 return '\0';
81}
82
Nate Begemand72c9002010-06-13 04:47:03 +000083/// Narrow - Convert a type code into the next smaller type. short -> char,
84/// float -> half float, etc.
Nate Begeman3861e742010-06-03 21:35:22 +000085static char Narrow(const char t) {
86 switch (t) {
87 case 's':
88 return 'c';
89 case 'i':
90 return 's';
91 case 'l':
92 return 'i';
Nate Begeman900f4672010-06-08 00:14:42 +000093 case 'f':
94 return 'h';
Bob Wilsonb055f742010-11-23 19:38:34 +000095 default: throw "unhandled type in narrow!";
Nate Begeman3861e742010-06-03 21:35:22 +000096 }
97 return '\0';
98}
99
Nate Begemand72c9002010-06-13 04:47:03 +0000100/// For a particular StringRef, return the base type code, and whether it has
101/// the quad-vector, polynomial, or unsigned modifiers set.
Nate Begemanaf905ef2010-06-02 06:17:19 +0000102static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) {
Nate Begeman22237772010-06-02 00:34:55 +0000103 unsigned off = 0;
Bob Wilson7f762182010-12-04 04:40:15 +0000104
Nate Begemanaf905ef2010-06-02 06:17:19 +0000105 // remember quad.
106 if (ty[off] == 'Q') {
107 quad = true;
108 ++off;
109 }
Bob Wilson7f762182010-12-04 04:40:15 +0000110
Nate Begemanaf905ef2010-06-02 06:17:19 +0000111 // remember poly.
112 if (ty[off] == 'P') {
113 poly = true;
114 ++off;
115 }
Bob Wilson7f762182010-12-04 04:40:15 +0000116
Nate Begemanaf905ef2010-06-02 06:17:19 +0000117 // remember unsigned.
118 if (ty[off] == 'U') {
119 usgn = true;
120 ++off;
121 }
Bob Wilson7f762182010-12-04 04:40:15 +0000122
Nate Begemanaf905ef2010-06-02 06:17:19 +0000123 // base type to get the type string for.
124 return ty[off];
125}
126
Nate Begemand72c9002010-06-13 04:47:03 +0000127/// ModType - Transform a type code and its modifiers based on a mod code. The
128/// mod code definitions may be found at the top of arm_neon.td.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000129static char ModType(const char mod, char type, bool &quad, bool &poly,
130 bool &usgn, bool &scal, bool &cnst, bool &pntr) {
Nate Begeman22237772010-06-02 00:34:55 +0000131 switch (mod) {
Nate Begeman22237772010-06-02 00:34:55 +0000132 case 't':
133 if (poly) {
134 poly = false;
135 usgn = true;
136 }
137 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000138 case 'u':
Nate Begeman22237772010-06-02 00:34:55 +0000139 usgn = true;
Bob Wilson181b76d2010-11-18 21:43:22 +0000140 poly = false;
141 if (type == 'f')
142 type = 'i';
143 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000144 case 'x':
Bob Wilson181b76d2010-11-18 21:43:22 +0000145 usgn = false;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000146 poly = false;
Nate Begeman22237772010-06-02 00:34:55 +0000147 if (type == 'f')
148 type = 'i';
149 break;
150 case 'f':
Nate Begeman900f4672010-06-08 00:14:42 +0000151 if (type == 'h')
152 quad = true;
Nate Begeman22237772010-06-02 00:34:55 +0000153 type = 'f';
Nate Begeman162d3ba2010-06-03 04:04:09 +0000154 usgn = false;
Nate Begeman22237772010-06-02 00:34:55 +0000155 break;
Nate Begeman59d70cb2010-08-06 01:24:11 +0000156 case 'g':
157 quad = false;
158 break;
Nate Begeman22237772010-06-02 00:34:55 +0000159 case 'w':
160 type = Widen(type);
161 quad = true;
162 break;
163 case 'n':
164 type = Widen(type);
165 break;
Bob Wilsonf00140c2010-12-01 19:49:58 +0000166 case 'i':
167 type = 'i';
168 scal = true;
169 break;
Nate Begeman22237772010-06-02 00:34:55 +0000170 case 'l':
171 type = 'l';
172 scal = true;
173 usgn = true;
174 break;
175 case 's':
Nate Begeman4b425a82010-06-10 00:16:56 +0000176 case 'a':
Nate Begeman22237772010-06-02 00:34:55 +0000177 scal = true;
178 break;
179 case 'k':
180 quad = true;
181 break;
182 case 'c':
183 cnst = true;
184 case 'p':
185 pntr = true;
186 scal = true;
187 break;
Nate Begeman3861e742010-06-03 21:35:22 +0000188 case 'h':
189 type = Narrow(type);
Nate Begeman900f4672010-06-08 00:14:42 +0000190 if (type == 'h')
191 quad = false;
Nate Begeman3861e742010-06-03 21:35:22 +0000192 break;
193 case 'e':
194 type = Narrow(type);
195 usgn = true;
196 break;
Nate Begeman22237772010-06-02 00:34:55 +0000197 default:
198 break;
199 }
Nate Begemanb0a4e452010-06-07 16:00:37 +0000200 return type;
201}
202
Nate Begemand72c9002010-06-13 04:47:03 +0000203/// TypeString - for a modifier and type, generate the name of the typedef for
Bob Wilson6904d7a2010-11-16 19:39:14 +0000204/// that type. QUc -> uint8x8_t.
205static std::string TypeString(const char mod, StringRef typestr) {
Nate Begemanb0a4e452010-06-07 16:00:37 +0000206 bool quad = false;
207 bool poly = false;
208 bool usgn = false;
209 bool scal = false;
210 bool cnst = false;
211 bool pntr = false;
Bob Wilson7f762182010-12-04 04:40:15 +0000212
Nate Begemanb0a4e452010-06-07 16:00:37 +0000213 if (mod == 'v')
214 return "void";
215 if (mod == 'i')
216 return "int";
Bob Wilson7f762182010-12-04 04:40:15 +0000217
Nate Begemanb0a4e452010-06-07 16:00:37 +0000218 // base type to get the type string for.
219 char type = ClassifyType(typestr, quad, poly, usgn);
Bob Wilson7f762182010-12-04 04:40:15 +0000220
Nate Begemanb0a4e452010-06-07 16:00:37 +0000221 // Based on the modifying character, change the type and width if necessary.
222 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
Bob Wilson7f762182010-12-04 04:40:15 +0000223
Nate Begeman22237772010-06-02 00:34:55 +0000224 SmallString<128> s;
Bob Wilson7f762182010-12-04 04:40:15 +0000225
Nate Begeman22237772010-06-02 00:34:55 +0000226 if (usgn)
227 s.push_back('u');
Bob Wilson7f762182010-12-04 04:40:15 +0000228
Nate Begeman22237772010-06-02 00:34:55 +0000229 switch (type) {
230 case 'c':
231 s += poly ? "poly8" : "int8";
232 if (scal)
233 break;
234 s += quad ? "x16" : "x8";
235 break;
236 case 's':
237 s += poly ? "poly16" : "int16";
238 if (scal)
239 break;
240 s += quad ? "x8" : "x4";
241 break;
242 case 'i':
243 s += "int32";
244 if (scal)
245 break;
246 s += quad ? "x4" : "x2";
247 break;
248 case 'l':
249 s += "int64";
250 if (scal)
251 break;
252 s += quad ? "x2" : "x1";
253 break;
254 case 'h':
255 s += "float16";
256 if (scal)
257 break;
258 s += quad ? "x8" : "x4";
259 break;
260 case 'f':
261 s += "float32";
262 if (scal)
263 break;
264 s += quad ? "x4" : "x2";
265 break;
Nate Begeman22237772010-06-02 00:34:55 +0000266 default:
267 throw "unhandled type!";
268 break;
269 }
270
271 if (mod == '2')
272 s += "x2";
273 if (mod == '3')
274 s += "x3";
275 if (mod == '4')
276 s += "x4";
Bob Wilson7f762182010-12-04 04:40:15 +0000277
Nate Begeman22237772010-06-02 00:34:55 +0000278 // Append _t, finishing the type string typedef type.
279 s += "_t";
Bob Wilson7f762182010-12-04 04:40:15 +0000280
Nate Begeman22237772010-06-02 00:34:55 +0000281 if (cnst)
282 s += " const";
Bob Wilson7f762182010-12-04 04:40:15 +0000283
Nate Begeman22237772010-06-02 00:34:55 +0000284 if (pntr)
285 s += " *";
Bob Wilson7f762182010-12-04 04:40:15 +0000286
Nate Begeman22237772010-06-02 00:34:55 +0000287 return s.str();
288}
289
Bob Wilson1ac27cf2010-06-24 22:04:30 +0000290/// BuiltinTypeString - for a modifier and type, generate the clang
291/// BuiltinsARM.def prototype code for the function. See the top of clang's
292/// Builtins.def for a description of the type strings.
Nate Begeman7c21f742010-06-04 21:36:00 +0000293static std::string BuiltinTypeString(const char mod, StringRef typestr,
294 ClassKind ck, bool ret) {
Nate Begeman92f98af2010-06-04 07:11:25 +0000295 bool quad = false;
296 bool poly = false;
297 bool usgn = false;
298 bool scal = false;
299 bool cnst = false;
300 bool pntr = false;
Bob Wilson7f762182010-12-04 04:40:15 +0000301
Nate Begeman92f98af2010-06-04 07:11:25 +0000302 if (mod == 'v')
Bob Wilson95148ad2010-12-01 19:49:56 +0000303 return "v"; // void
Nate Begeman92f98af2010-06-04 07:11:25 +0000304 if (mod == 'i')
Bob Wilson95148ad2010-12-01 19:49:56 +0000305 return "i"; // int
Bob Wilson7f762182010-12-04 04:40:15 +0000306
Nate Begeman92f98af2010-06-04 07:11:25 +0000307 // base type to get the type string for.
308 char type = ClassifyType(typestr, quad, poly, usgn);
Bob Wilson7f762182010-12-04 04:40:15 +0000309
Nate Begeman92f98af2010-06-04 07:11:25 +0000310 // Based on the modifying character, change the type and width if necessary.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000311 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
312
Bob Wilson95148ad2010-12-01 19:49:56 +0000313 // All pointers are void* pointers. Change type to 'v' now.
Nate Begemanc4a1b652010-06-20 21:09:52 +0000314 if (pntr) {
315 usgn = false;
316 poly = false;
Nate Begemanb0a4e452010-06-07 16:00:37 +0000317 type = 'v';
Nate Begemanc4a1b652010-06-20 21:09:52 +0000318 }
Bob Wilson95148ad2010-12-01 19:49:56 +0000319 // Treat half-float ('h') types as unsigned short ('s') types.
Nate Begeman92f98af2010-06-04 07:11:25 +0000320 if (type == 'h') {
321 type = 's';
322 usgn = true;
323 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000324 usgn = usgn | poly | ((ck == ClassI || ck == ClassW) && scal && type != 'f');
Nate Begeman92f98af2010-06-04 07:11:25 +0000325
326 if (scal) {
327 SmallString<128> s;
328
329 if (usgn)
330 s.push_back('U');
Bob Wilson317bafb2010-12-02 00:24:59 +0000331 else if (type == 'c')
332 s.push_back('S'); // make chars explicitly signed
Bob Wilson7f762182010-12-04 04:40:15 +0000333
Bob Wilson95148ad2010-12-01 19:49:56 +0000334 if (type == 'l') // 64-bit long
Nate Begeman7c21f742010-06-04 21:36:00 +0000335 s += "LLi";
336 else
337 s.push_back(type);
Bob Wilson7f762182010-12-04 04:40:15 +0000338
Nate Begeman92f98af2010-06-04 07:11:25 +0000339 if (cnst)
340 s.push_back('C');
341 if (pntr)
342 s.push_back('*');
343 return s.str();
344 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000345
346 // Since the return value must be one type, return a vector type of the
Nate Begemanc4a1b652010-06-20 21:09:52 +0000347 // appropriate width which we will bitcast. An exception is made for
348 // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
349 // fashion, storing them to a pointer arg.
Nate Begeman7c21f742010-06-04 21:36:00 +0000350 if (ret) {
Bob Wilson5b7fe592010-12-01 19:49:51 +0000351 if (mod >= '2' && mod <= '4')
Bob Wilson95148ad2010-12-01 19:49:56 +0000352 return "vv*"; // void result with void* first argument
Nate Begemanf50551e2010-06-09 18:02:26 +0000353 if (mod == 'f' || (ck != ClassB && type == 'f'))
Nate Begeman56387832010-06-08 06:01:16 +0000354 return quad ? "V4f" : "V2f";
Nate Begemanf50551e2010-06-09 18:02:26 +0000355 if (ck != ClassB && type == 's')
Nate Begeman007afe42010-06-09 05:11:55 +0000356 return quad ? "V8s" : "V4s";
Nate Begemanf50551e2010-06-09 18:02:26 +0000357 if (ck != ClassB && type == 'i')
Nate Begeman56387832010-06-08 06:01:16 +0000358 return quad ? "V4i" : "V2i";
Nate Begemanf50551e2010-06-09 18:02:26 +0000359 if (ck != ClassB && type == 'l')
Nate Begeman007afe42010-06-09 05:11:55 +0000360 return quad ? "V2LLi" : "V1LLi";
Bob Wilson7f762182010-12-04 04:40:15 +0000361
Bob Wilson317bafb2010-12-02 00:24:59 +0000362 return quad ? "V16Sc" : "V8Sc";
Bob Wilson7f762182010-12-04 04:40:15 +0000363 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000364
365 // Non-return array types are passed as individual vectors.
Nate Begeman92f98af2010-06-04 07:11:25 +0000366 if (mod == '2')
Bob Wilson317bafb2010-12-02 00:24:59 +0000367 return quad ? "V16ScV16Sc" : "V8ScV8Sc";
Nate Begeman92f98af2010-06-04 07:11:25 +0000368 if (mod == '3')
Bob Wilson317bafb2010-12-02 00:24:59 +0000369 return quad ? "V16ScV16ScV16Sc" : "V8ScV8ScV8Sc";
Nate Begeman92f98af2010-06-04 07:11:25 +0000370 if (mod == '4')
Bob Wilson317bafb2010-12-02 00:24:59 +0000371 return quad ? "V16ScV16ScV16ScV16Sc" : "V8ScV8ScV8ScV8Sc";
Nate Begeman92f98af2010-06-04 07:11:25 +0000372
Nate Begemanf50551e2010-06-09 18:02:26 +0000373 if (mod == 'f' || (ck != ClassB && type == 'f'))
Nate Begeman007afe42010-06-09 05:11:55 +0000374 return quad ? "V4f" : "V2f";
Nate Begemanf50551e2010-06-09 18:02:26 +0000375 if (ck != ClassB && type == 's')
Nate Begeman007afe42010-06-09 05:11:55 +0000376 return quad ? "V8s" : "V4s";
Nate Begemanf50551e2010-06-09 18:02:26 +0000377 if (ck != ClassB && type == 'i')
Nate Begeman007afe42010-06-09 05:11:55 +0000378 return quad ? "V4i" : "V2i";
Nate Begemanf50551e2010-06-09 18:02:26 +0000379 if (ck != ClassB && type == 'l')
Nate Begeman007afe42010-06-09 05:11:55 +0000380 return quad ? "V2LLi" : "V1LLi";
Bob Wilson7f762182010-12-04 04:40:15 +0000381
Bob Wilson317bafb2010-12-02 00:24:59 +0000382 return quad ? "V16Sc" : "V8Sc";
Nate Begeman92f98af2010-06-04 07:11:25 +0000383}
384
Bob Wilson7f762182010-12-04 04:40:15 +0000385/// MangleName - Append a type or width suffix to a base neon function name,
Nate Begemand72c9002010-06-13 04:47:03 +0000386/// and insert a 'q' in the appropriate location if the operation works on
387/// 128b rather than 64b. E.g. turn "vst2_lane" into "vst2q_lane_f32", etc.
Nate Begemana8979a02010-06-04 00:21:41 +0000388static std::string MangleName(const std::string &name, StringRef typestr,
389 ClassKind ck) {
Nate Begeman900f4672010-06-08 00:14:42 +0000390 if (name == "vcvt_f32_f16")
391 return name;
Bob Wilson7f762182010-12-04 04:40:15 +0000392
Nate Begemanaf905ef2010-06-02 06:17:19 +0000393 bool quad = false;
394 bool poly = false;
395 bool usgn = false;
396 char type = ClassifyType(typestr, quad, poly, usgn);
397
398 std::string s = name;
Bob Wilson7f762182010-12-04 04:40:15 +0000399
Nate Begemanaf905ef2010-06-02 06:17:19 +0000400 switch (type) {
Nate Begemana8979a02010-06-04 00:21:41 +0000401 case 'c':
402 switch (ck) {
403 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
404 case ClassI: s += "_i8"; break;
405 case ClassW: s += "_8"; break;
406 default: break;
407 }
408 break;
409 case 's':
410 switch (ck) {
411 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
412 case ClassI: s += "_i16"; break;
413 case ClassW: s += "_16"; break;
414 default: break;
415 }
416 break;
417 case 'i':
418 switch (ck) {
419 case ClassS: s += usgn ? "_u32" : "_s32"; break;
420 case ClassI: s += "_i32"; break;
421 case ClassW: s += "_32"; break;
422 default: break;
423 }
424 break;
425 case 'l':
426 switch (ck) {
427 case ClassS: s += usgn ? "_u64" : "_s64"; break;
428 case ClassI: s += "_i64"; break;
429 case ClassW: s += "_64"; break;
430 default: break;
431 }
432 break;
433 case 'h':
434 switch (ck) {
435 case ClassS:
436 case ClassI: s += "_f16"; break;
437 case ClassW: s += "_16"; break;
438 default: break;
439 }
440 break;
441 case 'f':
442 switch (ck) {
443 case ClassS:
444 case ClassI: s += "_f32"; break;
445 case ClassW: s += "_32"; break;
446 default: break;
447 }
448 break;
449 default:
450 throw "unhandled type!";
451 break;
Nate Begemanaf905ef2010-06-02 06:17:19 +0000452 }
Nate Begemana8979a02010-06-04 00:21:41 +0000453 if (ck == ClassB)
Nate Begeman92f98af2010-06-04 07:11:25 +0000454 s += "_v";
Bob Wilson7f762182010-12-04 04:40:15 +0000455
456 // Insert a 'q' before the first '_' character so that it ends up before
Nate Begemanaf905ef2010-06-02 06:17:19 +0000457 // _lane or _n on vector-scalar operations.
458 if (quad) {
459 size_t pos = s.find('_');
460 s = s.insert(pos, "q");
461 }
462 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000463}
464
Nate Begemanaf905ef2010-06-02 06:17:19 +0000465// Generate the string "(argtype a, argtype b, ...)"
Nate Begeman22237772010-06-02 00:34:55 +0000466static std::string GenArgs(const std::string &proto, StringRef typestr) {
Nate Begeman6c060db2010-06-09 01:09:00 +0000467 bool define = proto.find('i') != std::string::npos;
Nate Begemanaf905ef2010-06-02 06:17:19 +0000468 char arg = 'a';
Bob Wilson7f762182010-12-04 04:40:15 +0000469
Nate Begemanaf905ef2010-06-02 06:17:19 +0000470 std::string s;
471 s += "(";
Bob Wilson7f762182010-12-04 04:40:15 +0000472
Nate Begemanaf905ef2010-06-02 06:17:19 +0000473 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
Bob Wilson194aa582010-12-03 00:34:09 +0000474 if (define) {
475 // Immediate macro arguments are used directly instead of being assigned
476 // to local temporaries; prepend an underscore prefix to make their
477 // names consistent with the local temporaries.
478 if (proto[i] == 'i')
479 s += "__";
480 } else {
481 s += TypeString(proto[i], typestr) + " __";
Nate Begeman6c060db2010-06-09 01:09:00 +0000482 }
Nate Begemanaf905ef2010-06-02 06:17:19 +0000483 s.push_back(arg);
484 if ((i + 1) < e)
485 s += ", ";
486 }
Bob Wilson7f762182010-12-04 04:40:15 +0000487
Nate Begemanaf905ef2010-06-02 06:17:19 +0000488 s += ")";
489 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000490}
491
Bob Wilson194aa582010-12-03 00:34:09 +0000492// Macro arguments are not type-checked like inline function arguments, so
493// assign them to local temporaries to get the right type checking.
Bob Wilson377296e2010-12-02 07:10:39 +0000494static std::string GenMacroLocals(const std::string &proto, StringRef typestr) {
495 char arg = 'a';
496 std::string s;
Bob Wilson7f762182010-12-04 04:40:15 +0000497
Bob Wilson377296e2010-12-02 07:10:39 +0000498 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
499 // Do not create a temporary for an immediate argument.
500 // That would defeat the whole point of using a macro!
501 if (proto[i] == 'i') continue;
502
503 s += TypeString(proto[i], typestr) + " __";
504 s.push_back(arg);
505 s += " = (";
506 s.push_back(arg);
507 s += "); ";
508 }
Bob Wilson7f762182010-12-04 04:40:15 +0000509
Bob Wilson377296e2010-12-02 07:10:39 +0000510 s += "\\\n ";
511 return s;
512}
513
Bob Wilsonc4ba09d2010-12-07 20:02:45 +0000514// Use the vmovl builtin to sign-extend or zero-extend a vector.
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000515static std::string Extend(StringRef typestr, const std::string &a) {
Bob Wilsonc4ba09d2010-12-07 20:02:45 +0000516 std::string s;
517 s = MangleName("vmovl", typestr, ClassS);
518 s += "(" + a + ")";
519 return s;
520}
521
Bob Wilson7f762182010-12-04 04:40:15 +0000522static std::string Duplicate(unsigned nElts, StringRef typestr,
Nate Begemancc3c41a2010-06-12 03:09:49 +0000523 const std::string &a) {
Nate Begeman4b425a82010-06-10 00:16:56 +0000524 std::string s;
Bob Wilson7f762182010-12-04 04:40:15 +0000525
Bob Wilson6904d7a2010-11-16 19:39:14 +0000526 s = "(" + TypeString('d', typestr) + "){ ";
Nate Begeman4b425a82010-06-10 00:16:56 +0000527 for (unsigned i = 0; i != nElts; ++i) {
528 s += a;
529 if ((i + 1) < nElts)
530 s += ", ";
531 }
532 s += " }";
Bob Wilson7f762182010-12-04 04:40:15 +0000533
Nate Begeman4b425a82010-06-10 00:16:56 +0000534 return s;
535}
536
Bob Wilsonb0d98692010-12-03 00:34:12 +0000537static std::string SplatLane(unsigned nElts, const std::string &vec,
538 const std::string &lane) {
539 std::string s = "__builtin_shufflevector(" + vec + ", " + vec;
540 for (unsigned i = 0; i < nElts; ++i)
541 s += ", " + lane;
542 s += ")";
543 return s;
544}
545
Bob Wilsonb308b622010-11-16 23:57:01 +0000546static unsigned GetNumElements(StringRef typestr, bool &quad) {
547 quad = false;
548 bool dummy = false;
Nate Begemancc3c41a2010-06-12 03:09:49 +0000549 char type = ClassifyType(typestr, quad, dummy, dummy);
550 unsigned nElts = 0;
551 switch (type) {
Bob Wilsonb308b622010-11-16 23:57:01 +0000552 case 'c': nElts = 8; break;
553 case 's': nElts = 4; break;
554 case 'i': nElts = 2; break;
555 case 'l': nElts = 1; break;
556 case 'h': nElts = 4; break;
557 case 'f': nElts = 2; break;
558 default:
559 throw "unhandled type!";
560 break;
Nate Begemancc3c41a2010-06-12 03:09:49 +0000561 }
Bob Wilsonb308b622010-11-16 23:57:01 +0000562 if (quad) nElts <<= 1;
563 return nElts;
564}
565
566// Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
567static std::string GenOpString(OpKind op, const std::string &proto,
568 StringRef typestr) {
569 bool quad;
570 unsigned nElts = GetNumElements(typestr, quad);
Bob Wilson7f762182010-12-04 04:40:15 +0000571
Bob Wilson194aa582010-12-03 00:34:09 +0000572 // If this builtin takes an immediate argument, we need to #define it rather
573 // than use a standard declaration, so that SemaChecking can range check
574 // the immediate passed by the user.
575 bool define = proto.find('i') != std::string::npos;
576
Nate Begeman4b425a82010-06-10 00:16:56 +0000577 std::string ts = TypeString(proto[0], typestr);
Bob Wilsonee9ca072010-09-15 01:52:33 +0000578 std::string s;
Bob Wilson067a16c2011-01-07 23:40:49 +0000579 if (!define) {
Bob Wilson37a0b542010-12-02 07:44:23 +0000580 s = "return ";
Bob Wilsonee9ca072010-09-15 01:52:33 +0000581 }
Bob Wilson7f762182010-12-04 04:40:15 +0000582
Nate Begeman162d3ba2010-06-03 04:04:09 +0000583 switch(op) {
584 case OpAdd:
Bob Wilson194aa582010-12-03 00:34:09 +0000585 s += "__a + __b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000586 break;
Bob Wilsone113ae52010-12-08 00:14:04 +0000587 case OpAddl:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000588 s += Extend(typestr, "__a") + " + " + Extend(typestr, "__b") + ";";
Bob Wilsone113ae52010-12-08 00:14:04 +0000589 break;
590 case OpAddw:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000591 s += "__a + " + Extend(typestr, "__b") + ";";
Bob Wilsone113ae52010-12-08 00:14:04 +0000592 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000593 case OpSub:
Bob Wilson194aa582010-12-03 00:34:09 +0000594 s += "__a - __b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000595 break;
Bob Wilsone113ae52010-12-08 00:14:04 +0000596 case OpSubl:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000597 s += Extend(typestr, "__a") + " - " + Extend(typestr, "__b") + ";";
Bob Wilsone113ae52010-12-08 00:14:04 +0000598 break;
599 case OpSubw:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000600 s += "__a - " + Extend(typestr, "__b") + ";";
Bob Wilsone113ae52010-12-08 00:14:04 +0000601 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000602 case OpMulN:
Bob Wilson194aa582010-12-03 00:34:09 +0000603 s += "__a * " + Duplicate(nElts, typestr, "__b") + ";";
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000604 break;
Bob Wilsonb0d98692010-12-03 00:34:12 +0000605 case OpMulLane:
606 s += "__a * " + SplatLane(nElts, "__b", "__c") + ";";
607 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000608 case OpMul:
Bob Wilson194aa582010-12-03 00:34:09 +0000609 s += "__a * __b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000610 break;
Bob Wilsonc4ba09d2010-12-07 20:02:45 +0000611 case OpMullN:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000612 s += Extend(typestr, "__a") + " * " +
613 Extend(typestr, Duplicate(nElts << (int)quad, typestr, "__b")) + ";";
Bob Wilsonc4ba09d2010-12-07 20:02:45 +0000614 break;
Bob Wilson3467cd02010-12-07 22:02:48 +0000615 case OpMullLane:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000616 s += Extend(typestr, "__a") + " * " +
617 Extend(typestr, SplatLane(nElts, "__b", "__c")) + ";";
Bob Wilson3467cd02010-12-07 22:02:48 +0000618 break;
Bob Wilsonc4ba09d2010-12-07 20:02:45 +0000619 case OpMull:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000620 s += Extend(typestr, "__a") + " * " + Extend(typestr, "__b") + ";";
Bob Wilsonc4ba09d2010-12-07 20:02:45 +0000621 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000622 case OpMlaN:
Bob Wilson194aa582010-12-03 00:34:09 +0000623 s += "__a + (__b * " + Duplicate(nElts, typestr, "__c") + ");";
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000624 break;
Bob Wilsonb0d98692010-12-03 00:34:12 +0000625 case OpMlaLane:
626 s += "__a + (__b * " + SplatLane(nElts, "__c", "__d") + ");";
627 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000628 case OpMla:
Bob Wilson194aa582010-12-03 00:34:09 +0000629 s += "__a + (__b * __c);";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000630 break;
Bob Wilson05843162010-12-07 23:53:37 +0000631 case OpMlalN:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000632 s += "__a + (" + Extend(typestr, "__b") + " * " +
633 Extend(typestr, Duplicate(nElts, typestr, "__c")) + ");";
Bob Wilson05843162010-12-07 23:53:37 +0000634 break;
635 case OpMlalLane:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000636 s += "__a + (" + Extend(typestr, "__b") + " * " +
637 Extend(typestr, SplatLane(nElts, "__c", "__d")) + ");";
Bob Wilson05843162010-12-07 23:53:37 +0000638 break;
639 case OpMlal:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000640 s += "__a + (" + Extend(typestr, "__b") + " * " +
641 Extend(typestr, "__c") + ");";
Bob Wilson05843162010-12-07 23:53:37 +0000642 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000643 case OpMlsN:
Bob Wilson194aa582010-12-03 00:34:09 +0000644 s += "__a - (__b * " + Duplicate(nElts, typestr, "__c") + ");";
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000645 break;
Bob Wilsonb0d98692010-12-03 00:34:12 +0000646 case OpMlsLane:
647 s += "__a - (__b * " + SplatLane(nElts, "__c", "__d") + ");";
648 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000649 case OpMls:
Bob Wilson194aa582010-12-03 00:34:09 +0000650 s += "__a - (__b * __c);";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000651 break;
Bob Wilson05843162010-12-07 23:53:37 +0000652 case OpMlslN:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000653 s += "__a - (" + Extend(typestr, "__b") + " * " +
654 Extend(typestr, Duplicate(nElts, typestr, "__c")) + ");";
Bob Wilson05843162010-12-07 23:53:37 +0000655 break;
656 case OpMlslLane:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000657 s += "__a - (" + Extend(typestr, "__b") + " * " +
658 Extend(typestr, SplatLane(nElts, "__c", "__d")) + ");";
Bob Wilson05843162010-12-07 23:53:37 +0000659 break;
660 case OpMlsl:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000661 s += "__a - (" + Extend(typestr, "__b") + " * " +
662 Extend(typestr, "__c") + ");";
Bob Wilson05843162010-12-07 23:53:37 +0000663 break;
Bob Wilson74410892010-12-08 22:36:08 +0000664 case OpQDMullLane:
665 s += MangleName("vqdmull", typestr, ClassS) + "(__a, " +
666 SplatLane(nElts, "__b", "__c") + ");";
667 break;
668 case OpQDMlalLane:
Bob Wilsonc2ef8282010-12-10 06:37:53 +0000669 s += MangleName("vqdmlal", typestr, ClassS) + "(__a, __b, " +
670 SplatLane(nElts, "__c", "__d") + ");";
Bob Wilson74410892010-12-08 22:36:08 +0000671 break;
672 case OpQDMlslLane:
Bob Wilsonc2ef8282010-12-10 06:37:53 +0000673 s += MangleName("vqdmlsl", typestr, ClassS) + "(__a, __b, " +
674 SplatLane(nElts, "__c", "__d") + ");";
Bob Wilson74410892010-12-08 22:36:08 +0000675 break;
676 case OpQDMulhLane:
677 s += MangleName("vqdmulh", typestr, ClassS) + "(__a, " +
678 SplatLane(nElts, "__b", "__c") + ");";
679 break;
680 case OpQRDMulhLane:
681 s += MangleName("vqrdmulh", typestr, ClassS) + "(__a, " +
682 SplatLane(nElts, "__b", "__c") + ");";
683 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000684 case OpEq:
Bob Wilson194aa582010-12-03 00:34:09 +0000685 s += "(" + ts + ")(__a == __b);";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000686 break;
687 case OpGe:
Bob Wilson194aa582010-12-03 00:34:09 +0000688 s += "(" + ts + ")(__a >= __b);";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000689 break;
690 case OpLe:
Bob Wilson194aa582010-12-03 00:34:09 +0000691 s += "(" + ts + ")(__a <= __b);";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000692 break;
693 case OpGt:
Bob Wilson194aa582010-12-03 00:34:09 +0000694 s += "(" + ts + ")(__a > __b);";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000695 break;
696 case OpLt:
Bob Wilson194aa582010-12-03 00:34:09 +0000697 s += "(" + ts + ")(__a < __b);";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000698 break;
699 case OpNeg:
Bob Wilson194aa582010-12-03 00:34:09 +0000700 s += " -__a;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000701 break;
702 case OpNot:
Bob Wilson194aa582010-12-03 00:34:09 +0000703 s += " ~__a;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000704 break;
705 case OpAnd:
Bob Wilson194aa582010-12-03 00:34:09 +0000706 s += "__a & __b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000707 break;
708 case OpOr:
Bob Wilson194aa582010-12-03 00:34:09 +0000709 s += "__a | __b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000710 break;
711 case OpXor:
Bob Wilson194aa582010-12-03 00:34:09 +0000712 s += "__a ^ __b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000713 break;
714 case OpAndNot:
Bob Wilson194aa582010-12-03 00:34:09 +0000715 s += "__a & ~__b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000716 break;
717 case OpOrNot:
Bob Wilson194aa582010-12-03 00:34:09 +0000718 s += "__a | ~__b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000719 break;
Nate Begeman3861e742010-06-03 21:35:22 +0000720 case OpCast:
Bob Wilson194aa582010-12-03 00:34:09 +0000721 s += "(" + ts + ")__a;";
Nate Begeman3861e742010-06-03 21:35:22 +0000722 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000723 case OpConcat:
Bob Wilson194aa582010-12-03 00:34:09 +0000724 s += "(" + ts + ")__builtin_shufflevector((int64x1_t)__a";
725 s += ", (int64x1_t)__b, 0, 1);";
Nate Begeman900f4672010-06-08 00:14:42 +0000726 break;
Nate Begeman6c060db2010-06-09 01:09:00 +0000727 case OpHi:
Bob Wilson067a16c2011-01-07 23:40:49 +0000728 s += "(" + ts +
729 ")__builtin_shufflevector((int64x2_t)__a, (int64x2_t)__a, 1);";
Nate Begeman6c060db2010-06-09 01:09:00 +0000730 break;
731 case OpLo:
Bob Wilson067a16c2011-01-07 23:40:49 +0000732 s += "(" + ts +
733 ")__builtin_shufflevector((int64x2_t)__a, (int64x2_t)__a, 0);";
Nate Begeman6c060db2010-06-09 01:09:00 +0000734 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000735 case OpDup:
Bob Wilson194aa582010-12-03 00:34:09 +0000736 s += Duplicate(nElts, typestr, "__a") + ";";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000737 break;
Bob Wilson2196caa2010-12-07 22:39:24 +0000738 case OpDupLane:
739 s += SplatLane(nElts, "__a", "__b") + ";";
740 break;
Nate Begemancc3c41a2010-06-12 03:09:49 +0000741 case OpSelect:
742 // ((0 & 1) | (~0 & 2))
Bob Wilson1dbfa912010-12-02 01:18:20 +0000743 s += "(" + ts + ")";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000744 ts = TypeString(proto[1], typestr);
Bob Wilson194aa582010-12-03 00:34:09 +0000745 s += "((__a & (" + ts + ")__b) | ";
746 s += "(~__a & (" + ts + ")__c));";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000747 break;
748 case OpRev16:
Bob Wilson194aa582010-12-03 00:34:09 +0000749 s += "__builtin_shufflevector(__a, __a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000750 for (unsigned i = 2; i <= nElts; i += 2)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000751 for (unsigned j = 0; j != 2; ++j)
752 s += ", " + utostr(i - j - 1);
Bob Wilson37a0b542010-12-02 07:44:23 +0000753 s += ");";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000754 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000755 case OpRev32: {
756 unsigned WordElts = nElts >> (1 + (int)quad);
Bob Wilson194aa582010-12-03 00:34:09 +0000757 s += "__builtin_shufflevector(__a, __a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000758 for (unsigned i = WordElts; i <= nElts; i += WordElts)
759 for (unsigned j = 0; j != WordElts; ++j)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000760 s += ", " + utostr(i - j - 1);
Bob Wilson37a0b542010-12-02 07:44:23 +0000761 s += ");";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000762 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000763 }
764 case OpRev64: {
765 unsigned DblWordElts = nElts >> (int)quad;
Bob Wilson194aa582010-12-03 00:34:09 +0000766 s += "__builtin_shufflevector(__a, __a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000767 for (unsigned i = DblWordElts; i <= nElts; i += DblWordElts)
768 for (unsigned j = 0; j != DblWordElts; ++j)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000769 s += ", " + utostr(i - j - 1);
Bob Wilson37a0b542010-12-02 07:44:23 +0000770 s += ");";
Nate Begeman900f4672010-06-08 00:14:42 +0000771 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000772 }
Bob Wilsonb4504302010-12-08 21:39:04 +0000773 case OpAbdl: {
774 std::string abd = MangleName("vabd", typestr, ClassS) + "(__a, __b)";
775 if (typestr[0] != 'U') {
776 // vabd results are always unsigned and must be zero-extended.
777 std::string utype = "U" + typestr.str();
778 s += "(" + TypeString(proto[0], typestr) + ")";
779 abd = "(" + TypeString('d', utype) + ")" + abd;
780 s += Extend(utype, abd) + ";";
781 } else {
782 s += Extend(typestr, abd) + ";";
783 }
784 break;
785 }
Bob Wilsonf4f39d32010-12-08 20:09:10 +0000786 case OpAba:
787 s += "__a + " + MangleName("vabd", typestr, ClassS) + "(__b, __c);";
788 break;
Bob Wilsonb4504302010-12-08 21:39:04 +0000789 case OpAbal: {
790 s += "__a + ";
791 std::string abd = MangleName("vabd", typestr, ClassS) + "(__b, __c)";
792 if (typestr[0] != 'U') {
793 // vabd results are always unsigned and must be zero-extended.
794 std::string utype = "U" + typestr.str();
795 s += "(" + TypeString(proto[0], typestr) + ")";
796 abd = "(" + TypeString('d', utype) + ")" + abd;
797 s += Extend(utype, abd) + ";";
798 } else {
799 s += Extend(typestr, abd) + ";";
800 }
801 break;
802 }
Nate Begeman162d3ba2010-06-03 04:04:09 +0000803 default:
804 throw "unknown OpKind!";
805 break;
806 }
Nate Begeman162d3ba2010-06-03 04:04:09 +0000807 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000808}
809
Nate Begemanb0a4e452010-06-07 16:00:37 +0000810static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
811 unsigned mod = proto[0];
812 unsigned ret = 0;
813
Nate Begeman900f4672010-06-08 00:14:42 +0000814 if (mod == 'v' || mod == 'f')
Nate Begemanb0a4e452010-06-07 16:00:37 +0000815 mod = proto[1];
816
817 bool quad = false;
818 bool poly = false;
819 bool usgn = false;
820 bool scal = false;
821 bool cnst = false;
822 bool pntr = false;
Bob Wilson7f762182010-12-04 04:40:15 +0000823
Nate Begeman59d70cb2010-08-06 01:24:11 +0000824 // Base type to get the type string for.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000825 char type = ClassifyType(typestr, quad, poly, usgn);
Bob Wilson7f762182010-12-04 04:40:15 +0000826
Nate Begemanb0a4e452010-06-07 16:00:37 +0000827 // Based on the modifying character, change the type and width if necessary.
828 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
Nate Begeman59d70cb2010-08-06 01:24:11 +0000829
Nate Begemanb0a4e452010-06-07 16:00:37 +0000830 if (usgn)
831 ret |= 0x08;
Nate Begeman59d70cb2010-08-06 01:24:11 +0000832 if (quad && proto[1] != 'g')
Nate Begemanb0a4e452010-06-07 16:00:37 +0000833 ret |= 0x10;
Bob Wilson7f762182010-12-04 04:40:15 +0000834
Nate Begemanb0a4e452010-06-07 16:00:37 +0000835 switch (type) {
Bob Wilson7f762182010-12-04 04:40:15 +0000836 case 'c':
Nate Begemanb0a4e452010-06-07 16:00:37 +0000837 ret |= poly ? 5 : 0;
838 break;
839 case 's':
840 ret |= poly ? 6 : 1;
841 break;
842 case 'i':
843 ret |= 2;
844 break;
845 case 'l':
846 ret |= 3;
847 break;
848 case 'h':
849 ret |= 7;
850 break;
851 case 'f':
852 ret |= 4;
853 break;
854 default:
855 throw "unhandled type!";
856 break;
857 }
858 return ret;
859}
860
Nate Begeman7c8c8832010-06-02 21:53:00 +0000861// Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
Nate Begeman7c8c8832010-06-02 21:53:00 +0000862static std::string GenBuiltin(const std::string &name, const std::string &proto,
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000863 StringRef typestr, ClassKind ck) {
Nate Begeman162d3ba2010-06-03 04:04:09 +0000864 std::string s;
Nate Begeman9e584b32010-06-04 22:53:30 +0000865
Nate Begemanc4a1b652010-06-20 21:09:52 +0000866 // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
867 // sret-like argument.
Bob Wilson5b7fe592010-12-01 19:49:51 +0000868 bool sret = (proto[0] >= '2' && proto[0] <= '4');
Nate Begemanc4a1b652010-06-20 21:09:52 +0000869
870 // If this builtin takes an immediate argument, we need to #define it rather
871 // than use a standard declaration, so that SemaChecking can range check
872 // the immediate passed by the user.
Nate Begeman6c060db2010-06-09 01:09:00 +0000873 bool define = proto.find('i') != std::string::npos;
Nate Begeman9e584b32010-06-04 22:53:30 +0000874
Bob Wilson95148ad2010-12-01 19:49:56 +0000875 // Check if the prototype has a scalar operand with the type of the vector
876 // elements. If not, bitcasting the args will take care of arg checking.
877 // The actual signedness etc. will be taken care of with special enums.
Nate Begeman9e584b32010-06-04 22:53:30 +0000878 if (proto.find('s') == std::string::npos)
879 ck = ClassB;
Nate Begeman7c21f742010-06-04 21:36:00 +0000880
Nate Begeman162d3ba2010-06-03 04:04:09 +0000881 if (proto[0] != 'v') {
Nate Begeman6c060db2010-06-09 01:09:00 +0000882 std::string ts = TypeString(proto[0], typestr);
Bob Wilson7f762182010-12-04 04:40:15 +0000883
Nate Begeman6c060db2010-06-09 01:09:00 +0000884 if (define) {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000885 if (sret)
Bob Wilson052008b2010-12-02 02:42:51 +0000886 s += ts + " r; ";
Bob Wilsone106bc12010-12-02 00:24:56 +0000887 else
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000888 s += "(" + ts + ")";
Nate Begemanc4a1b652010-06-20 21:09:52 +0000889 } else if (sret) {
890 s += ts + " r; ";
Nate Begeman9e584b32010-06-04 22:53:30 +0000891 } else {
Bob Wilson37a0b542010-12-02 07:44:23 +0000892 s += "return (" + ts + ")";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000893 }
Nate Begeman4b425a82010-06-10 00:16:56 +0000894 }
Bob Wilson7f762182010-12-04 04:40:15 +0000895
Nate Begeman4b425a82010-06-10 00:16:56 +0000896 bool splat = proto.find('a') != std::string::npos;
Bob Wilson7f762182010-12-04 04:40:15 +0000897
Nate Begeman7c8c8832010-06-02 21:53:00 +0000898 s += "__builtin_neon_";
Nate Begeman4b425a82010-06-10 00:16:56 +0000899 if (splat) {
Bob Wilson95148ad2010-12-01 19:49:56 +0000900 // Call the non-splat builtin: chop off the "_n" suffix from the name.
Nate Begeman4b425a82010-06-10 00:16:56 +0000901 std::string vname(name, 0, name.size()-2);
902 s += MangleName(vname, typestr, ck);
903 } else {
904 s += MangleName(name, typestr, ck);
905 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000906 s += "(";
Nate Begemanc4a1b652010-06-20 21:09:52 +0000907
908 // Pass the address of the return variable as the first argument to sret-like
909 // builtins.
910 if (sret)
911 s += "&r, ";
Bob Wilson7f762182010-12-04 04:40:15 +0000912
Bob Wilson377296e2010-12-02 07:10:39 +0000913 char arg = 'a';
Nate Begeman7c8c8832010-06-02 21:53:00 +0000914 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
Nate Begemancc3c41a2010-06-12 03:09:49 +0000915 std::string args = std::string(&arg, 1);
Bob Wilson95148ad2010-12-01 19:49:56 +0000916
Bob Wilson194aa582010-12-03 00:34:09 +0000917 // Use the local temporaries instead of the macro arguments.
918 args = "__" + args;
Bob Wilsonf00140c2010-12-01 19:49:58 +0000919
920 bool argQuad = false;
921 bool argPoly = false;
922 bool argUsgn = false;
923 bool argScalar = false;
924 bool dummy = false;
925 char argType = ClassifyType(typestr, argQuad, argPoly, argUsgn);
926 argType = ModType(proto[i], argType, argQuad, argPoly, argUsgn, argScalar,
927 dummy, dummy);
928
Nate Begeman9e584b32010-06-04 22:53:30 +0000929 // Handle multiple-vector values specially, emitting each subvector as an
930 // argument to the __builtin.
Bob Wilson5b7fe592010-12-01 19:49:51 +0000931 if (proto[i] >= '2' && proto[i] <= '4') {
Bob Wilsonf00140c2010-12-01 19:49:58 +0000932 // Check if an explicit cast is needed.
933 if (argType != 'c' || argPoly || argUsgn)
934 args = (argQuad ? "(int8x16_t)" : "(int8x8_t)") + args;
935
Nate Begeman9e584b32010-06-04 22:53:30 +0000936 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000937 s += args + ".val[" + utostr(vi) + "]";
Nate Begeman9e584b32010-06-04 22:53:30 +0000938 if ((vi + 1) < ve)
939 s += ", ";
940 }
941 if ((i + 1) < e)
942 s += ", ";
943
944 continue;
945 }
Bob Wilson7f762182010-12-04 04:40:15 +0000946
Bob Wilson4a6c7fc2010-12-02 01:18:23 +0000947 if (splat && (i + 1) == e)
948 args = Duplicate(GetNumElements(typestr, argQuad), typestr, args);
949
Bob Wilsonf00140c2010-12-01 19:49:58 +0000950 // Check if an explicit cast is needed.
Bob Wilson4a6c7fc2010-12-02 01:18:23 +0000951 if ((splat || !argScalar) &&
Bob Wilsonf00140c2010-12-01 19:49:58 +0000952 ((ck == ClassB && argType != 'c') || argPoly || argUsgn)) {
953 std::string argTypeStr = "c";
954 if (ck != ClassB)
955 argTypeStr = argType;
956 if (argQuad)
957 argTypeStr = "Q" + argTypeStr;
958 args = "(" + TypeString('d', argTypeStr) + ")" + args;
959 }
Bob Wilson7f762182010-12-04 04:40:15 +0000960
Bob Wilson4a6c7fc2010-12-02 01:18:23 +0000961 s += args;
Nate Begeman7c8c8832010-06-02 21:53:00 +0000962 if ((i + 1) < e)
963 s += ", ";
964 }
Bob Wilson7f762182010-12-04 04:40:15 +0000965
Nate Begeman9e584b32010-06-04 22:53:30 +0000966 // Extra constant integer to hold type class enum for this function, e.g. s8
Nate Begeman9e584b32010-06-04 22:53:30 +0000967 if (ck == ClassB)
Nate Begemanb0a4e452010-06-07 16:00:37 +0000968 s += ", " + utostr(GetNeonEnum(proto, typestr));
Bob Wilson7f762182010-12-04 04:40:15 +0000969
Bob Wilson052008b2010-12-02 02:42:51 +0000970 s += ");";
Nate Begeman9e584b32010-06-04 22:53:30 +0000971
Bob Wilson37a0b542010-12-02 07:44:23 +0000972 if (proto[0] != 'v' && sret) {
973 if (define)
974 s += " r;";
975 else
Nate Begemanc4a1b652010-06-20 21:09:52 +0000976 s += " return r;";
Nate Begeman9e584b32010-06-04 22:53:30 +0000977 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000978 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000979}
980
Bob Wilson7f762182010-12-04 04:40:15 +0000981static std::string GenBuiltinDef(const std::string &name,
Nate Begeman73cef3e2010-06-04 01:26:15 +0000982 const std::string &proto,
983 StringRef typestr, ClassKind ck) {
984 std::string s("BUILTIN(__builtin_neon_");
Nate Begeman92f98af2010-06-04 07:11:25 +0000985
Bob Wilson7f762182010-12-04 04:40:15 +0000986 // If all types are the same size, bitcasting the args will take care
Nate Begeman92f98af2010-06-04 07:11:25 +0000987 // of arg checking. The actual signedness etc. will be taken care of with
988 // special enums.
989 if (proto.find('s') == std::string::npos)
990 ck = ClassB;
Bob Wilson7f762182010-12-04 04:40:15 +0000991
Nate Begeman73cef3e2010-06-04 01:26:15 +0000992 s += MangleName(name, typestr, ck);
993 s += ", \"";
Bob Wilson7f762182010-12-04 04:40:15 +0000994
Nate Begeman92f98af2010-06-04 07:11:25 +0000995 for (unsigned i = 0, e = proto.size(); i != e; ++i)
Nate Begeman7c21f742010-06-04 21:36:00 +0000996 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
997
998 // Extra constant integer to hold type class enum for this function, e.g. s8
999 if (ck == ClassB)
1000 s += "i";
Bob Wilson7f762182010-12-04 04:40:15 +00001001
Nate Begeman73cef3e2010-06-04 01:26:15 +00001002 s += "\", \"n\")";
1003 return s;
1004}
1005
Bob Wilson3890e392010-12-07 01:12:23 +00001006static std::string GenIntrinsic(const std::string &name,
1007 const std::string &proto,
1008 StringRef outTypeStr, StringRef inTypeStr,
1009 OpKind kind, ClassKind classKind) {
1010 assert(!proto.empty() && "");
1011 bool define = proto.find('i') != std::string::npos;
1012 std::string s;
1013
1014 // static always inline + return type
1015 if (define)
1016 s += "#define ";
1017 else
1018 s += "__ai " + TypeString(proto[0], outTypeStr) + " ";
1019
1020 // Function name with type suffix
1021 std::string mangledName = MangleName(name, outTypeStr, ClassS);
1022 if (outTypeStr != inTypeStr) {
1023 // If the input type is different (e.g., for vreinterpret), append a suffix
1024 // for the input type. String off a "Q" (quad) prefix so that MangleName
1025 // does not insert another "q" in the name.
1026 unsigned typeStrOff = (inTypeStr[0] == 'Q' ? 1 : 0);
1027 StringRef inTypeNoQuad = inTypeStr.substr(typeStrOff);
1028 mangledName = MangleName(mangledName, inTypeNoQuad, ClassS);
1029 }
1030 s += mangledName;
1031
1032 // Function arguments
1033 s += GenArgs(proto, inTypeStr);
1034
1035 // Definition.
1036 if (define) {
1037 s += " __extension__ ({ \\\n ";
1038 s += GenMacroLocals(proto, inTypeStr);
1039 } else {
1040 s += " { \\\n ";
1041 }
1042
1043 if (kind != OpNone)
1044 s += GenOpString(kind, proto, outTypeStr);
1045 else
1046 s += GenBuiltin(name, proto, outTypeStr, classKind);
1047 if (define)
1048 s += " })";
1049 else
1050 s += " }";
1051 s += "\n";
1052 return s;
1053}
1054
Nate Begemand72c9002010-06-13 04:47:03 +00001055/// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h
1056/// is comprised of type definitions and function declarations.
Nate Begeman5ddb0872010-05-28 01:08:32 +00001057void NeonEmitter::run(raw_ostream &OS) {
Bob Wilson0e1fb7a2010-12-09 18:31:01 +00001058 OS <<
Bob Wilsonb78558c2010-12-09 18:43:35 +00001059 "/*===---- arm_neon.h - ARM Neon intrinsics ------------------------------"
1060 "---===\n"
Bob Wilson0e1fb7a2010-12-09 18:31:01 +00001061 " *\n"
Bob Wilsonb78558c2010-12-09 18:43:35 +00001062 " * Permission is hereby granted, free of charge, to any person obtaining "
1063 "a copy\n"
1064 " * of this software and associated documentation files (the \"Software\"),"
1065 " to deal\n"
1066 " * in the Software without restriction, including without limitation the "
1067 "rights\n"
1068 " * to use, copy, modify, merge, publish, distribute, sublicense, "
1069 "and/or sell\n"
Bob Wilson0e1fb7a2010-12-09 18:31:01 +00001070 " * copies of the Software, and to permit persons to whom the Software is\n"
1071 " * furnished to do so, subject to the following conditions:\n"
1072 " *\n"
Bob Wilsonb78558c2010-12-09 18:43:35 +00001073 " * The above copyright notice and this permission notice shall be "
1074 "included in\n"
Bob Wilson0e1fb7a2010-12-09 18:31:01 +00001075 " * all copies or substantial portions of the Software.\n"
1076 " *\n"
Bob Wilsonb78558c2010-12-09 18:43:35 +00001077 " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, "
1078 "EXPRESS OR\n"
1079 " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
1080 "MERCHANTABILITY,\n"
1081 " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT "
1082 "SHALL THE\n"
1083 " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR "
1084 "OTHER\n"
1085 " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, "
1086 "ARISING FROM,\n"
1087 " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER "
1088 "DEALINGS IN\n"
Bob Wilson0e1fb7a2010-12-09 18:31:01 +00001089 " * THE SOFTWARE.\n"
1090 " *\n"
Bob Wilsonb78558c2010-12-09 18:43:35 +00001091 " *===--------------------------------------------------------------------"
1092 "---===\n"
Bob Wilson0e1fb7a2010-12-09 18:31:01 +00001093 " */\n\n";
Bob Wilson7f762182010-12-04 04:40:15 +00001094
Nate Begeman5ddb0872010-05-28 01:08:32 +00001095 OS << "#ifndef __ARM_NEON_H\n";
1096 OS << "#define __ARM_NEON_H\n\n";
Bob Wilson7f762182010-12-04 04:40:15 +00001097
Nate Begeman5ddb0872010-05-28 01:08:32 +00001098 OS << "#ifndef __ARM_NEON__\n";
1099 OS << "#error \"NEON support not enabled\"\n";
1100 OS << "#endif\n\n";
1101
1102 OS << "#include <stdint.h>\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +00001103
1104 // Emit NEON-specific scalar typedefs.
Nate Begeman7c8c8832010-06-02 21:53:00 +00001105 OS << "typedef float float32_t;\n";
Bob Wilson4fbf6382010-11-16 23:57:03 +00001106 OS << "typedef int8_t poly8_t;\n";
1107 OS << "typedef int16_t poly16_t;\n";
Nate Begeman162d3ba2010-06-03 04:04:09 +00001108 OS << "typedef uint16_t float16_t;\n";
Nate Begeman9e584b32010-06-04 22:53:30 +00001109
Nate Begeman7c8c8832010-06-02 21:53:00 +00001110 // Emit Neon vector typedefs.
1111 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
1112 SmallVector<StringRef, 24> TDTypeVec;
1113 ParseTypes(0, TypedefTypes, TDTypeVec);
1114
1115 // Emit vector typedefs.
Bob Wilson6904d7a2010-11-16 19:39:14 +00001116 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
Bob Wilson6a8aceb2010-11-16 23:57:06 +00001117 bool dummy, quad = false, poly = false;
1118 (void) ClassifyType(TDTypeVec[i], quad, poly, dummy);
1119 if (poly)
1120 OS << "typedef __attribute__((neon_polyvector_type(";
1121 else
1122 OS << "typedef __attribute__((neon_vector_type(";
Bob Wilson7f762182010-12-04 04:40:15 +00001123
Bob Wilson6a8aceb2010-11-16 23:57:06 +00001124 unsigned nElts = GetNumElements(TDTypeVec[i], quad);
1125 OS << utostr(nElts) << "))) ";
1126 if (nElts < 10)
Bob Wilson6904d7a2010-11-16 19:39:14 +00001127 OS << " ";
Bob Wilson7f762182010-12-04 04:40:15 +00001128
Bob Wilson6904d7a2010-11-16 19:39:14 +00001129 OS << TypeString('s', TDTypeVec[i]);
1130 OS << " " << TypeString('d', TDTypeVec[i]) << ";\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +00001131 }
1132 OS << "\n";
1133
1134 // Emit struct typedefs.
Bob Wilson6904d7a2010-11-16 19:39:14 +00001135 for (unsigned vi = 2; vi != 5; ++vi) {
Nate Begeman7c8c8832010-06-02 21:53:00 +00001136 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
Bob Wilson6904d7a2010-11-16 19:39:14 +00001137 std::string ts = TypeString('d', TDTypeVec[i]);
1138 std::string vs = TypeString('0' + vi, TDTypeVec[i]);
1139 OS << "typedef struct " << vs << " {\n";
1140 OS << " " << ts << " val";
1141 OS << "[" << utostr(vi) << "]";
1142 OS << ";\n} ";
Bob Wilsonf18dfec2010-11-16 19:16:06 +00001143 OS << vs << ";\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +00001144 }
1145 }
Bob Wilson7f762182010-12-04 04:40:15 +00001146
Nate Begeman7c8c8832010-06-02 21:53:00 +00001147 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
1148
Nate Begeman5ddb0872010-05-28 01:08:32 +00001149 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
Bob Wilson7f762182010-12-04 04:40:15 +00001150
Bob Wilsonf4f39d32010-12-08 20:09:10 +00001151 // Emit vmovl and vabd intrinsics first so they can be used by other
Bob Wilson74410892010-12-08 22:36:08 +00001152 // intrinsics. (Some of the saturating multiply instructions are also
1153 // used to implement the corresponding "_lane" variants, but tablegen
1154 // sorts the records into alphabetical order so that the "_lane" variants
1155 // come after the intrinsics they use.)
Bob Wilsonda1d3dc2010-12-07 23:53:32 +00001156 emitIntrinsic(OS, Records.getDef("VMOVL"));
Bob Wilsonf4f39d32010-12-08 20:09:10 +00001157 emitIntrinsic(OS, Records.getDef("VABD"));
Bob Wilsonda1d3dc2010-12-07 23:53:32 +00001158
Nate Begeman5ddb0872010-05-28 01:08:32 +00001159 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1160 Record *R = RV[i];
Bob Wilsonf4f39d32010-12-08 20:09:10 +00001161 if (R->getName() != "VMOVL" && R->getName() != "VABD")
Bob Wilsonda1d3dc2010-12-07 23:53:32 +00001162 emitIntrinsic(OS, R);
Nate Begeman5ddb0872010-05-28 01:08:32 +00001163 }
Bob Wilsonda1d3dc2010-12-07 23:53:32 +00001164
Nate Begeman73cef3e2010-06-04 01:26:15 +00001165 OS << "#undef __ai\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +00001166 OS << "#endif /* __ARM_NEON_H */\n";
Nate Begeman5ddb0872010-05-28 01:08:32 +00001167}
Nate Begemana8979a02010-06-04 00:21:41 +00001168
Bob Wilsonda1d3dc2010-12-07 23:53:32 +00001169/// emitIntrinsic - Write out the arm_neon.h header file definitions for the
1170/// intrinsics specified by record R.
1171void NeonEmitter::emitIntrinsic(raw_ostream &OS, Record *R) {
1172 std::string name = R->getValueAsString("Name");
1173 std::string Proto = R->getValueAsString("Prototype");
1174 std::string Types = R->getValueAsString("Types");
1175
1176 SmallVector<StringRef, 16> TypeVec;
1177 ParseTypes(R, Types, TypeVec);
1178
1179 OpKind kind = OpMap[R->getValueAsDef("Operand")->getName()];
1180
1181 ClassKind classKind = ClassNone;
1182 if (R->getSuperClasses().size() >= 2)
1183 classKind = ClassMap[R->getSuperClasses()[1]];
1184 if (classKind == ClassNone && kind == OpNone)
1185 throw TGError(R->getLoc(), "Builtin has no class kind");
1186
1187 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1188 if (kind == OpReinterpret) {
1189 bool outQuad = false;
1190 bool dummy = false;
1191 (void)ClassifyType(TypeVec[ti], outQuad, dummy, dummy);
1192 for (unsigned srcti = 0, srcte = TypeVec.size();
1193 srcti != srcte; ++srcti) {
1194 bool inQuad = false;
1195 (void)ClassifyType(TypeVec[srcti], inQuad, dummy, dummy);
1196 if (srcti == ti || inQuad != outQuad)
1197 continue;
1198 OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[srcti],
1199 OpCast, ClassS);
1200 }
1201 } else {
1202 OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[ti],
1203 kind, classKind);
1204 }
1205 }
1206 OS << "\n";
1207}
1208
Bob Wilson06b04aa2010-12-15 16:58:42 +00001209static unsigned RangeFromType(const char mod, StringRef typestr) {
Nate Begeman918f8e42010-06-14 05:17:23 +00001210 // base type to get the type string for.
1211 bool quad = false, dummy = false;
1212 char type = ClassifyType(typestr, quad, dummy, dummy);
Bob Wilson06b04aa2010-12-15 16:58:42 +00001213 type = ModType(mod, type, quad, dummy, dummy, dummy, dummy, dummy);
Bob Wilson7f762182010-12-04 04:40:15 +00001214
Nate Begeman918f8e42010-06-14 05:17:23 +00001215 switch (type) {
1216 case 'c':
Nate Begeman4da883a2010-06-15 22:10:31 +00001217 return (8 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001218 case 'h':
1219 case 's':
Nate Begeman4da883a2010-06-15 22:10:31 +00001220 return (4 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001221 case 'f':
1222 case 'i':
Nate Begeman4da883a2010-06-15 22:10:31 +00001223 return (2 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001224 case 'l':
Nate Begeman4da883a2010-06-15 22:10:31 +00001225 return (1 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001226 default:
1227 throw "unhandled type!";
1228 break;
1229 }
Bob Wilsonfdb530d2010-07-28 18:21:10 +00001230 assert(0 && "unreachable");
1231 return 0;
Nate Begeman918f8e42010-06-14 05:17:23 +00001232}
1233
Nate Begemanf8c4c272010-06-17 04:15:13 +00001234/// runHeader - Emit a file with sections defining:
1235/// 1. the NEON section of BuiltinsARM.def.
1236/// 2. the SemaChecking code for the type overload checking.
1237/// 3. the SemaChecking code for validation of intrinsic immedate arguments.
Nate Begemana8979a02010-06-04 00:21:41 +00001238void NeonEmitter::runHeader(raw_ostream &OS) {
Nate Begeman73cef3e2010-06-04 01:26:15 +00001239 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1240
1241 StringMap<OpKind> EmittedMap;
Bob Wilson7f762182010-12-04 04:40:15 +00001242
Nate Begemanf8c4c272010-06-17 04:15:13 +00001243 // Generate BuiltinsARM.def for NEON
1244 OS << "#ifdef GET_NEON_BUILTINS\n";
Nate Begeman73cef3e2010-06-04 01:26:15 +00001245 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1246 Record *R = RV[i];
Nate Begeman73cef3e2010-06-04 01:26:15 +00001247 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1248 if (k != OpNone)
1249 continue;
Nate Begeman73cef3e2010-06-04 01:26:15 +00001250
Nate Begemanf8c4c272010-06-17 04:15:13 +00001251 std::string Proto = R->getValueAsString("Prototype");
Bob Wilson7f762182010-12-04 04:40:15 +00001252
Nate Begemand72c9002010-06-13 04:47:03 +00001253 // Functions with 'a' (the splat code) in the type prototype should not get
1254 // their own builtin as they use the non-splat variant.
Nate Begeman4b425a82010-06-10 00:16:56 +00001255 if (Proto.find('a') != std::string::npos)
1256 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001257
Nate Begemanf8c4c272010-06-17 04:15:13 +00001258 std::string Types = R->getValueAsString("Types");
Nate Begeman73cef3e2010-06-04 01:26:15 +00001259 SmallVector<StringRef, 16> TypeVec;
1260 ParseTypes(R, Types, TypeVec);
Bob Wilson7f762182010-12-04 04:40:15 +00001261
Nate Begeman73cef3e2010-06-04 01:26:15 +00001262 if (R->getSuperClasses().size() < 2)
1263 throw TGError(R->getLoc(), "Builtin has no class kind");
Bob Wilson7f762182010-12-04 04:40:15 +00001264
Bob Wilsonce0bb542010-12-03 17:19:39 +00001265 std::string name = R->getValueAsString("Name");
Nate Begeman73cef3e2010-06-04 01:26:15 +00001266 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
Bob Wilson7f762182010-12-04 04:40:15 +00001267
Nate Begeman73cef3e2010-06-04 01:26:15 +00001268 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
Nate Begemand72c9002010-06-13 04:47:03 +00001269 // Generate the BuiltinsARM.def declaration for this builtin, ensuring
1270 // that each unique BUILTIN() macro appears only once in the output
1271 // stream.
Nate Begeman73cef3e2010-06-04 01:26:15 +00001272 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
1273 if (EmittedMap.count(bd))
1274 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001275
Nate Begeman73cef3e2010-06-04 01:26:15 +00001276 EmittedMap[bd] = OpNone;
1277 OS << bd << "\n";
1278 }
Nate Begemanf8c4c272010-06-17 04:15:13 +00001279 }
1280 OS << "#endif\n\n";
Bob Wilson7f762182010-12-04 04:40:15 +00001281
Nate Begemanf8c4c272010-06-17 04:15:13 +00001282 // Generate the overloaded type checking code for SemaChecking.cpp
1283 OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
1284 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1285 Record *R = RV[i];
1286 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1287 if (k != OpNone)
Nate Begemand72c9002010-06-13 04:47:03 +00001288 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001289
Nate Begemanf8c4c272010-06-17 04:15:13 +00001290 std::string Proto = R->getValueAsString("Prototype");
1291 std::string Types = R->getValueAsString("Types");
Bob Wilsonce0bb542010-12-03 17:19:39 +00001292 std::string name = R->getValueAsString("Name");
Bob Wilson7f762182010-12-04 04:40:15 +00001293
Nate Begemanf8c4c272010-06-17 04:15:13 +00001294 // Functions with 'a' (the splat code) in the type prototype should not get
1295 // their own builtin as they use the non-splat variant.
1296 if (Proto.find('a') != std::string::npos)
1297 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001298
Nate Begemanf8c4c272010-06-17 04:15:13 +00001299 // Functions which have a scalar argument cannot be overloaded, no need to
1300 // check them if we are emitting the type checking code.
1301 if (Proto.find('s') != std::string::npos)
1302 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001303
Nate Begemanf8c4c272010-06-17 04:15:13 +00001304 SmallVector<StringRef, 16> TypeVec;
1305 ParseTypes(R, Types, TypeVec);
Bob Wilson7f762182010-12-04 04:40:15 +00001306
Nate Begemanf8c4c272010-06-17 04:15:13 +00001307 if (R->getSuperClasses().size() < 2)
1308 throw TGError(R->getLoc(), "Builtin has no class kind");
Bob Wilson7f762182010-12-04 04:40:15 +00001309
Nate Begemanf8c4c272010-06-17 04:15:13 +00001310 int si = -1, qi = -1;
1311 unsigned mask = 0, qmask = 0;
1312 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1313 // Generate the switch case(s) for this builtin for the type validation.
1314 bool quad = false, poly = false, usgn = false;
1315 (void) ClassifyType(TypeVec[ti], quad, poly, usgn);
Bob Wilson7f762182010-12-04 04:40:15 +00001316
Nate Begemanf8c4c272010-06-17 04:15:13 +00001317 if (quad) {
1318 qi = ti;
1319 qmask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1320 } else {
1321 si = ti;
1322 mask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1323 }
1324 }
1325 if (mask)
Bob Wilson7f762182010-12-04 04:40:15 +00001326 OS << "case ARM::BI__builtin_neon_"
Bob Wilsond8b84702010-12-07 01:12:19 +00001327 << MangleName(name, TypeVec[si], ClassB)
1328 << ": mask = " << "0x" << utohexstr(mask) << "; break;\n";
Nate Begemanf8c4c272010-06-17 04:15:13 +00001329 if (qmask)
Bob Wilson7f762182010-12-04 04:40:15 +00001330 OS << "case ARM::BI__builtin_neon_"
Bob Wilsond8b84702010-12-07 01:12:19 +00001331 << MangleName(name, TypeVec[qi], ClassB)
1332 << ": mask = " << "0x" << utohexstr(qmask) << "; break;\n";
Nate Begemanf8c4c272010-06-17 04:15:13 +00001333 }
1334 OS << "#endif\n\n";
Bob Wilson7f762182010-12-04 04:40:15 +00001335
Nate Begemanf8c4c272010-06-17 04:15:13 +00001336 // Generate the intrinsic range checking code for shift/lane immediates.
1337 OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
1338 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1339 Record *R = RV[i];
Bob Wilson7f762182010-12-04 04:40:15 +00001340
Nate Begemanf8c4c272010-06-17 04:15:13 +00001341 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1342 if (k != OpNone)
1343 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001344
Bob Wilsonce0bb542010-12-03 17:19:39 +00001345 std::string name = R->getValueAsString("Name");
Nate Begemanf8c4c272010-06-17 04:15:13 +00001346 std::string Proto = R->getValueAsString("Prototype");
1347 std::string Types = R->getValueAsString("Types");
Bob Wilson7f762182010-12-04 04:40:15 +00001348
Nate Begemanf8c4c272010-06-17 04:15:13 +00001349 // Functions with 'a' (the splat code) in the type prototype should not get
1350 // their own builtin as they use the non-splat variant.
1351 if (Proto.find('a') != std::string::npos)
1352 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001353
Nate Begemanf8c4c272010-06-17 04:15:13 +00001354 // Functions which do not have an immediate do not need to have range
1355 // checking code emitted.
Bob Wilson06b04aa2010-12-15 16:58:42 +00001356 size_t immPos = Proto.find('i');
1357 if (immPos == std::string::npos)
Nate Begemanf8c4c272010-06-17 04:15:13 +00001358 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001359
Nate Begemanf8c4c272010-06-17 04:15:13 +00001360 SmallVector<StringRef, 16> TypeVec;
1361 ParseTypes(R, Types, TypeVec);
Bob Wilson7f762182010-12-04 04:40:15 +00001362
Nate Begemanf8c4c272010-06-17 04:15:13 +00001363 if (R->getSuperClasses().size() < 2)
1364 throw TGError(R->getLoc(), "Builtin has no class kind");
Bob Wilson7f762182010-12-04 04:40:15 +00001365
Nate Begemanf8c4c272010-06-17 04:15:13 +00001366 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
Bob Wilson7f762182010-12-04 04:40:15 +00001367
Nate Begemanf8c4c272010-06-17 04:15:13 +00001368 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1369 std::string namestr, shiftstr, rangestr;
Bob Wilson7f762182010-12-04 04:40:15 +00001370
Nate Begemanf8c4c272010-06-17 04:15:13 +00001371 // Builtins which are overloaded by type will need to have their upper
1372 // bound computed at Sema time based on the type constant.
1373 if (Proto.find('s') == std::string::npos) {
1374 ck = ClassB;
1375 if (R->getValueAsBit("isShift")) {
1376 shiftstr = ", true";
Bob Wilson7f762182010-12-04 04:40:15 +00001377
Nate Begemanf8c4c272010-06-17 04:15:13 +00001378 // Right shifts have an 'r' in the name, left shifts do not.
1379 if (name.find('r') != std::string::npos)
1380 rangestr = "l = 1; ";
1381 }
1382 rangestr += "u = RFT(TV" + shiftstr + ")";
1383 } else {
Bob Wilson06b04aa2010-12-15 16:58:42 +00001384 // The immediate generally refers to a lane in the preceding argument.
1385 assert(immPos > 0 && "unexpected immediate operand");
1386 rangestr = "u = " + utostr(RangeFromType(Proto[immPos-1], TypeVec[ti]));
Nate Begemanf8c4c272010-06-17 04:15:13 +00001387 }
Nate Begemanc4a1b652010-06-20 21:09:52 +00001388 // Make sure cases appear only once by uniquing them in a string map.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001389 namestr = MangleName(name, TypeVec[ti], ck);
1390 if (EmittedMap.count(namestr))
1391 continue;
1392 EmittedMap[namestr] = OpNone;
Nate Begemanc4a1b652010-06-20 21:09:52 +00001393
1394 // Calculate the index of the immediate that should be range checked.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001395 unsigned immidx = 0;
Bob Wilson7f762182010-12-04 04:40:15 +00001396
Nate Begemanc4a1b652010-06-20 21:09:52 +00001397 // Builtins that return a struct of multiple vectors have an extra
1398 // leading arg for the struct return.
Bob Wilson5b7fe592010-12-01 19:49:51 +00001399 if (Proto[0] >= '2' && Proto[0] <= '4')
Nate Begemanc4a1b652010-06-20 21:09:52 +00001400 ++immidx;
Bob Wilson7f762182010-12-04 04:40:15 +00001401
1402 // Add one to the index for each argument until we reach the immediate
Nate Begemanc4a1b652010-06-20 21:09:52 +00001403 // to be checked. Structs of vectors are passed as multiple arguments.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001404 for (unsigned ii = 1, ie = Proto.size(); ii != ie; ++ii) {
1405 switch (Proto[ii]) {
1406 default: immidx += 1; break;
1407 case '2': immidx += 2; break;
1408 case '3': immidx += 3; break;
1409 case '4': immidx += 4; break;
1410 case 'i': ie = ii + 1; break;
1411 }
1412 }
Bob Wilsond8b84702010-12-07 01:12:19 +00001413 OS << "case ARM::BI__builtin_neon_" << MangleName(name, TypeVec[ti], ck)
Nate Begemanf8c4c272010-06-17 04:15:13 +00001414 << ": i = " << immidx << "; " << rangestr << "; break;\n";
Nate Begemand72c9002010-06-13 04:47:03 +00001415 }
Nate Begeman73cef3e2010-06-04 01:26:15 +00001416 }
Nate Begemanf8c4c272010-06-17 04:15:13 +00001417 OS << "#endif\n\n";
Nate Begemana8979a02010-06-04 00:21:41 +00001418}
Bob Wilson333f5192010-12-15 16:58:45 +00001419
1420/// GenTest - Write out a test for the intrinsic specified by the name and
1421/// type strings, including the embedded patterns for FileCheck to match.
1422static std::string GenTest(const std::string &name,
1423 const std::string &proto,
1424 StringRef outTypeStr, StringRef inTypeStr,
1425 bool isShift) {
1426 assert(!proto.empty() && "");
1427 std::string s;
1428
1429 // Function name with type suffix
1430 std::string mangledName = MangleName(name, outTypeStr, ClassS);
1431 if (outTypeStr != inTypeStr) {
1432 // If the input type is different (e.g., for vreinterpret), append a suffix
1433 // for the input type. String off a "Q" (quad) prefix so that MangleName
1434 // does not insert another "q" in the name.
1435 unsigned typeStrOff = (inTypeStr[0] == 'Q' ? 1 : 0);
1436 StringRef inTypeNoQuad = inTypeStr.substr(typeStrOff);
1437 mangledName = MangleName(mangledName, inTypeNoQuad, ClassS);
1438 }
1439
1440 // Emit the FileCheck patterns.
1441 s += "// CHECK: test_" + mangledName + "\n";
1442 // s += "// CHECK: \n"; // FIXME: + expected instruction opcode.
1443
1444 // Emit the start of the test function.
1445 s += TypeString(proto[0], outTypeStr) + " test_" + mangledName + "(";
1446 char arg = 'a';
1447 std::string comma;
1448 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
1449 // Do not create arguments for values that must be immediate constants.
1450 if (proto[i] == 'i')
1451 continue;
1452 s += comma + TypeString(proto[i], inTypeStr) + " ";
1453 s.push_back(arg);
1454 comma = ", ";
1455 }
1456 s += ") { \\\n ";
1457
1458 if (proto[0] != 'v')
1459 s += "return ";
1460 s += mangledName + "(";
1461 arg = 'a';
1462 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
1463 if (proto[i] == 'i') {
1464 // For immediate operands, test the maximum value.
1465 if (isShift)
1466 s += "1"; // FIXME
1467 else
1468 // The immediate generally refers to a lane in the preceding argument.
1469 s += utostr(RangeFromType(proto[i-1], inTypeStr));
1470 } else {
1471 s.push_back(arg);
1472 }
1473 if ((i + 1) < e)
1474 s += ", ";
1475 }
1476 s += ");\n}\n\n";
1477 return s;
1478}
1479
1480/// runTests - Write out a complete set of tests for all of the Neon
1481/// intrinsics.
1482void NeonEmitter::runTests(raw_ostream &OS) {
1483 OS <<
1484 "// RUN: %clang_cc1 -triple thumbv7-apple-darwin \\\n"
Bob Wilson14595d92010-12-17 01:21:03 +00001485 "// RUN: -target-cpu cortex-a9 -ffreestanding -S -o - %s | FileCheck %s\n"
Bob Wilson333f5192010-12-15 16:58:45 +00001486 "\n"
1487 "#include <arm_neon.h>\n"
1488 "\n";
1489
1490 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1491 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1492 Record *R = RV[i];
1493 std::string name = R->getValueAsString("Name");
1494 std::string Proto = R->getValueAsString("Prototype");
1495 std::string Types = R->getValueAsString("Types");
1496 bool isShift = R->getValueAsBit("isShift");
1497
1498 SmallVector<StringRef, 16> TypeVec;
1499 ParseTypes(R, Types, TypeVec);
1500
1501 OpKind kind = OpMap[R->getValueAsDef("Operand")->getName()];
1502 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1503 if (kind == OpReinterpret) {
1504 bool outQuad = false;
1505 bool dummy = false;
1506 (void)ClassifyType(TypeVec[ti], outQuad, dummy, dummy);
1507 for (unsigned srcti = 0, srcte = TypeVec.size();
1508 srcti != srcte; ++srcti) {
1509 bool inQuad = false;
1510 (void)ClassifyType(TypeVec[srcti], inQuad, dummy, dummy);
1511 if (srcti == ti || inQuad != outQuad)
1512 continue;
1513 OS << GenTest(name, Proto, TypeVec[ti], TypeVec[srcti], isShift);
1514 }
1515 } else {
1516 OS << GenTest(name, Proto, TypeVec[ti], TypeVec[ti], isShift);
1517 }
1518 }
1519 OS << "\n";
1520 }
1521}
1522