blob: 13bed47b4a5eb9a1ed1dee06ecbbd82c863e4aba [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';
76 default: throw "unhandled type in widen!";
77 }
78 return '\0';
79}
80
Nate Begemand72c9002010-06-13 04:47:03 +000081/// Narrow - Convert a type code into the next smaller type. short -> char,
82/// float -> half float, etc.
Nate Begeman3861e742010-06-03 21:35:22 +000083static char Narrow(const char t) {
84 switch (t) {
85 case 's':
86 return 'c';
87 case 'i':
88 return 's';
89 case 'l':
90 return 'i';
Nate Begeman900f4672010-06-08 00:14:42 +000091 case 'f':
92 return 'h';
Bob Wilsonb055f742010-11-23 19:38:34 +000093 default: throw "unhandled type in narrow!";
Nate Begeman3861e742010-06-03 21:35:22 +000094 }
95 return '\0';
96}
97
Nate Begemand72c9002010-06-13 04:47:03 +000098/// For a particular StringRef, return the base type code, and whether it has
99/// the quad-vector, polynomial, or unsigned modifiers set.
Nate Begemanaf905ef2010-06-02 06:17:19 +0000100static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) {
Nate Begeman22237772010-06-02 00:34:55 +0000101 unsigned off = 0;
Bob Wilson7f762182010-12-04 04:40:15 +0000102
Nate Begemanaf905ef2010-06-02 06:17:19 +0000103 // remember quad.
104 if (ty[off] == 'Q') {
105 quad = true;
106 ++off;
107 }
Bob Wilson7f762182010-12-04 04:40:15 +0000108
Nate Begemanaf905ef2010-06-02 06:17:19 +0000109 // remember poly.
110 if (ty[off] == 'P') {
111 poly = true;
112 ++off;
113 }
Bob Wilson7f762182010-12-04 04:40:15 +0000114
Nate Begemanaf905ef2010-06-02 06:17:19 +0000115 // remember unsigned.
116 if (ty[off] == 'U') {
117 usgn = true;
118 ++off;
119 }
Bob Wilson7f762182010-12-04 04:40:15 +0000120
Nate Begemanaf905ef2010-06-02 06:17:19 +0000121 // base type to get the type string for.
122 return ty[off];
123}
124
Nate Begemand72c9002010-06-13 04:47:03 +0000125/// ModType - Transform a type code and its modifiers based on a mod code. The
126/// mod code definitions may be found at the top of arm_neon.td.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000127static char ModType(const char mod, char type, bool &quad, bool &poly,
128 bool &usgn, bool &scal, bool &cnst, bool &pntr) {
Nate Begeman22237772010-06-02 00:34:55 +0000129 switch (mod) {
Nate Begeman22237772010-06-02 00:34:55 +0000130 case 't':
131 if (poly) {
132 poly = false;
133 usgn = true;
134 }
135 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000136 case 'u':
Nate Begeman22237772010-06-02 00:34:55 +0000137 usgn = true;
Bob Wilson181b76d2010-11-18 21:43:22 +0000138 poly = false;
139 if (type == 'f')
140 type = 'i';
141 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000142 case 'x':
Bob Wilson181b76d2010-11-18 21:43:22 +0000143 usgn = false;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000144 poly = false;
Nate Begeman22237772010-06-02 00:34:55 +0000145 if (type == 'f')
146 type = 'i';
147 break;
148 case 'f':
Nate Begeman900f4672010-06-08 00:14:42 +0000149 if (type == 'h')
150 quad = true;
Nate Begeman22237772010-06-02 00:34:55 +0000151 type = 'f';
Nate Begeman162d3ba2010-06-03 04:04:09 +0000152 usgn = false;
Nate Begeman22237772010-06-02 00:34:55 +0000153 break;
Nate Begeman59d70cb2010-08-06 01:24:11 +0000154 case 'g':
155 quad = false;
156 break;
Nate Begeman22237772010-06-02 00:34:55 +0000157 case 'w':
158 type = Widen(type);
159 quad = true;
160 break;
161 case 'n':
162 type = Widen(type);
163 break;
Bob Wilsonf00140c2010-12-01 19:49:58 +0000164 case 'i':
165 type = 'i';
166 scal = true;
167 break;
Nate Begeman22237772010-06-02 00:34:55 +0000168 case 'l':
169 type = 'l';
170 scal = true;
171 usgn = true;
172 break;
173 case 's':
Nate Begeman4b425a82010-06-10 00:16:56 +0000174 case 'a':
Nate Begeman22237772010-06-02 00:34:55 +0000175 scal = true;
176 break;
177 case 'k':
178 quad = true;
179 break;
180 case 'c':
181 cnst = true;
182 case 'p':
183 pntr = true;
184 scal = true;
185 break;
Nate Begeman3861e742010-06-03 21:35:22 +0000186 case 'h':
187 type = Narrow(type);
Nate Begeman900f4672010-06-08 00:14:42 +0000188 if (type == 'h')
189 quad = false;
Nate Begeman3861e742010-06-03 21:35:22 +0000190 break;
191 case 'e':
192 type = Narrow(type);
193 usgn = true;
194 break;
Nate Begeman22237772010-06-02 00:34:55 +0000195 default:
196 break;
197 }
Nate Begemanb0a4e452010-06-07 16:00:37 +0000198 return type;
199}
200
Nate Begemand72c9002010-06-13 04:47:03 +0000201/// TypeString - for a modifier and type, generate the name of the typedef for
Bob Wilson6904d7a2010-11-16 19:39:14 +0000202/// that type. QUc -> uint8x8_t.
203static std::string TypeString(const char mod, StringRef typestr) {
Nate Begemanb0a4e452010-06-07 16:00:37 +0000204 bool quad = false;
205 bool poly = false;
206 bool usgn = false;
207 bool scal = false;
208 bool cnst = false;
209 bool pntr = false;
Bob Wilson7f762182010-12-04 04:40:15 +0000210
Nate Begemanb0a4e452010-06-07 16:00:37 +0000211 if (mod == 'v')
212 return "void";
213 if (mod == 'i')
214 return "int";
Bob Wilson7f762182010-12-04 04:40:15 +0000215
Nate Begemanb0a4e452010-06-07 16:00:37 +0000216 // base type to get the type string for.
217 char type = ClassifyType(typestr, quad, poly, usgn);
Bob Wilson7f762182010-12-04 04:40:15 +0000218
Nate Begemanb0a4e452010-06-07 16:00:37 +0000219 // Based on the modifying character, change the type and width if necessary.
220 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
Bob Wilson7f762182010-12-04 04:40:15 +0000221
Nate Begeman22237772010-06-02 00:34:55 +0000222 SmallString<128> s;
Bob Wilson7f762182010-12-04 04:40:15 +0000223
Nate Begeman22237772010-06-02 00:34:55 +0000224 if (usgn)
225 s.push_back('u');
Bob Wilson7f762182010-12-04 04:40:15 +0000226
Nate Begeman22237772010-06-02 00:34:55 +0000227 switch (type) {
228 case 'c':
229 s += poly ? "poly8" : "int8";
230 if (scal)
231 break;
232 s += quad ? "x16" : "x8";
233 break;
234 case 's':
235 s += poly ? "poly16" : "int16";
236 if (scal)
237 break;
238 s += quad ? "x8" : "x4";
239 break;
240 case 'i':
241 s += "int32";
242 if (scal)
243 break;
244 s += quad ? "x4" : "x2";
245 break;
246 case 'l':
247 s += "int64";
248 if (scal)
249 break;
250 s += quad ? "x2" : "x1";
251 break;
252 case 'h':
253 s += "float16";
254 if (scal)
255 break;
256 s += quad ? "x8" : "x4";
257 break;
258 case 'f':
259 s += "float32";
260 if (scal)
261 break;
262 s += quad ? "x4" : "x2";
263 break;
Nate Begeman22237772010-06-02 00:34:55 +0000264 default:
265 throw "unhandled type!";
266 break;
267 }
268
269 if (mod == '2')
270 s += "x2";
271 if (mod == '3')
272 s += "x3";
273 if (mod == '4')
274 s += "x4";
Bob Wilson7f762182010-12-04 04:40:15 +0000275
Nate Begeman22237772010-06-02 00:34:55 +0000276 // Append _t, finishing the type string typedef type.
277 s += "_t";
Bob Wilson7f762182010-12-04 04:40:15 +0000278
Nate Begeman22237772010-06-02 00:34:55 +0000279 if (cnst)
280 s += " const";
Bob Wilson7f762182010-12-04 04:40:15 +0000281
Nate Begeman22237772010-06-02 00:34:55 +0000282 if (pntr)
283 s += " *";
Bob Wilson7f762182010-12-04 04:40:15 +0000284
Nate Begeman22237772010-06-02 00:34:55 +0000285 return s.str();
286}
287
Bob Wilson1ac27cf2010-06-24 22:04:30 +0000288/// BuiltinTypeString - for a modifier and type, generate the clang
289/// BuiltinsARM.def prototype code for the function. See the top of clang's
290/// Builtins.def for a description of the type strings.
Nate Begeman7c21f742010-06-04 21:36:00 +0000291static std::string BuiltinTypeString(const char mod, StringRef typestr,
292 ClassKind ck, bool ret) {
Nate Begeman92f98af2010-06-04 07:11:25 +0000293 bool quad = false;
294 bool poly = false;
295 bool usgn = false;
296 bool scal = false;
297 bool cnst = false;
298 bool pntr = false;
Bob Wilson7f762182010-12-04 04:40:15 +0000299
Nate Begeman92f98af2010-06-04 07:11:25 +0000300 if (mod == 'v')
Bob Wilson95148ad2010-12-01 19:49:56 +0000301 return "v"; // void
Nate Begeman92f98af2010-06-04 07:11:25 +0000302 if (mod == 'i')
Bob Wilson95148ad2010-12-01 19:49:56 +0000303 return "i"; // int
Bob Wilson7f762182010-12-04 04:40:15 +0000304
Nate Begeman92f98af2010-06-04 07:11:25 +0000305 // base type to get the type string for.
306 char type = ClassifyType(typestr, quad, poly, usgn);
Bob Wilson7f762182010-12-04 04:40:15 +0000307
Nate Begeman92f98af2010-06-04 07:11:25 +0000308 // Based on the modifying character, change the type and width if necessary.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000309 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
310
Bob Wilson95148ad2010-12-01 19:49:56 +0000311 // All pointers are void* pointers. Change type to 'v' now.
Nate Begemanc4a1b652010-06-20 21:09:52 +0000312 if (pntr) {
313 usgn = false;
314 poly = false;
Nate Begemanb0a4e452010-06-07 16:00:37 +0000315 type = 'v';
Nate Begemanc4a1b652010-06-20 21:09:52 +0000316 }
Bob Wilson95148ad2010-12-01 19:49:56 +0000317 // Treat half-float ('h') types as unsigned short ('s') types.
Nate Begeman92f98af2010-06-04 07:11:25 +0000318 if (type == 'h') {
319 type = 's';
320 usgn = true;
321 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000322 usgn = usgn | poly | ((ck == ClassI || ck == ClassW) && scal && type != 'f');
Nate Begeman92f98af2010-06-04 07:11:25 +0000323
324 if (scal) {
325 SmallString<128> s;
326
327 if (usgn)
328 s.push_back('U');
Bob Wilson317bafb2010-12-02 00:24:59 +0000329 else if (type == 'c')
330 s.push_back('S'); // make chars explicitly signed
Bob Wilson7f762182010-12-04 04:40:15 +0000331
Bob Wilson95148ad2010-12-01 19:49:56 +0000332 if (type == 'l') // 64-bit long
Nate Begeman7c21f742010-06-04 21:36:00 +0000333 s += "LLi";
334 else
335 s.push_back(type);
Bob Wilson7f762182010-12-04 04:40:15 +0000336
Nate Begeman92f98af2010-06-04 07:11:25 +0000337 if (cnst)
338 s.push_back('C');
339 if (pntr)
340 s.push_back('*');
341 return s.str();
342 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000343
344 // Since the return value must be one type, return a vector type of the
Nate Begemanc4a1b652010-06-20 21:09:52 +0000345 // appropriate width which we will bitcast. An exception is made for
346 // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
347 // fashion, storing them to a pointer arg.
Nate Begeman7c21f742010-06-04 21:36:00 +0000348 if (ret) {
Bob Wilson5b7fe592010-12-01 19:49:51 +0000349 if (mod >= '2' && mod <= '4')
Bob Wilson95148ad2010-12-01 19:49:56 +0000350 return "vv*"; // void result with void* first argument
Nate Begemanf50551e2010-06-09 18:02:26 +0000351 if (mod == 'f' || (ck != ClassB && type == 'f'))
Nate Begeman56387832010-06-08 06:01:16 +0000352 return quad ? "V4f" : "V2f";
Nate Begemanf50551e2010-06-09 18:02:26 +0000353 if (ck != ClassB && type == 's')
Nate Begeman007afe42010-06-09 05:11:55 +0000354 return quad ? "V8s" : "V4s";
Nate Begemanf50551e2010-06-09 18:02:26 +0000355 if (ck != ClassB && type == 'i')
Nate Begeman56387832010-06-08 06:01:16 +0000356 return quad ? "V4i" : "V2i";
Nate Begemanf50551e2010-06-09 18:02:26 +0000357 if (ck != ClassB && type == 'l')
Nate Begeman007afe42010-06-09 05:11:55 +0000358 return quad ? "V2LLi" : "V1LLi";
Bob Wilson7f762182010-12-04 04:40:15 +0000359
Bob Wilson317bafb2010-12-02 00:24:59 +0000360 return quad ? "V16Sc" : "V8Sc";
Bob Wilson7f762182010-12-04 04:40:15 +0000361 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000362
363 // Non-return array types are passed as individual vectors.
Nate Begeman92f98af2010-06-04 07:11:25 +0000364 if (mod == '2')
Bob Wilson317bafb2010-12-02 00:24:59 +0000365 return quad ? "V16ScV16Sc" : "V8ScV8Sc";
Nate Begeman92f98af2010-06-04 07:11:25 +0000366 if (mod == '3')
Bob Wilson317bafb2010-12-02 00:24:59 +0000367 return quad ? "V16ScV16ScV16Sc" : "V8ScV8ScV8Sc";
Nate Begeman92f98af2010-06-04 07:11:25 +0000368 if (mod == '4')
Bob Wilson317bafb2010-12-02 00:24:59 +0000369 return quad ? "V16ScV16ScV16ScV16Sc" : "V8ScV8ScV8ScV8Sc";
Nate Begeman92f98af2010-06-04 07:11:25 +0000370
Nate Begemanf50551e2010-06-09 18:02:26 +0000371 if (mod == 'f' || (ck != ClassB && type == 'f'))
Nate Begeman007afe42010-06-09 05:11:55 +0000372 return quad ? "V4f" : "V2f";
Nate Begemanf50551e2010-06-09 18:02:26 +0000373 if (ck != ClassB && type == 's')
Nate Begeman007afe42010-06-09 05:11:55 +0000374 return quad ? "V8s" : "V4s";
Nate Begemanf50551e2010-06-09 18:02:26 +0000375 if (ck != ClassB && type == 'i')
Nate Begeman007afe42010-06-09 05:11:55 +0000376 return quad ? "V4i" : "V2i";
Nate Begemanf50551e2010-06-09 18:02:26 +0000377 if (ck != ClassB && type == 'l')
Nate Begeman007afe42010-06-09 05:11:55 +0000378 return quad ? "V2LLi" : "V1LLi";
Bob Wilson7f762182010-12-04 04:40:15 +0000379
Bob Wilson317bafb2010-12-02 00:24:59 +0000380 return quad ? "V16Sc" : "V8Sc";
Nate Begeman92f98af2010-06-04 07:11:25 +0000381}
382
Bob Wilson7f762182010-12-04 04:40:15 +0000383/// MangleName - Append a type or width suffix to a base neon function name,
Nate Begemand72c9002010-06-13 04:47:03 +0000384/// and insert a 'q' in the appropriate location if the operation works on
385/// 128b rather than 64b. E.g. turn "vst2_lane" into "vst2q_lane_f32", etc.
Nate Begemana8979a02010-06-04 00:21:41 +0000386static std::string MangleName(const std::string &name, StringRef typestr,
387 ClassKind ck) {
Nate Begeman900f4672010-06-08 00:14:42 +0000388 if (name == "vcvt_f32_f16")
389 return name;
Bob Wilson7f762182010-12-04 04:40:15 +0000390
Nate Begemanaf905ef2010-06-02 06:17:19 +0000391 bool quad = false;
392 bool poly = false;
393 bool usgn = false;
394 char type = ClassifyType(typestr, quad, poly, usgn);
395
396 std::string s = name;
Bob Wilson7f762182010-12-04 04:40:15 +0000397
Nate Begemanaf905ef2010-06-02 06:17:19 +0000398 switch (type) {
Nate Begemana8979a02010-06-04 00:21:41 +0000399 case 'c':
400 switch (ck) {
401 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
402 case ClassI: s += "_i8"; break;
403 case ClassW: s += "_8"; break;
404 default: break;
405 }
406 break;
407 case 's':
408 switch (ck) {
409 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
410 case ClassI: s += "_i16"; break;
411 case ClassW: s += "_16"; break;
412 default: break;
413 }
414 break;
415 case 'i':
416 switch (ck) {
417 case ClassS: s += usgn ? "_u32" : "_s32"; break;
418 case ClassI: s += "_i32"; break;
419 case ClassW: s += "_32"; break;
420 default: break;
421 }
422 break;
423 case 'l':
424 switch (ck) {
425 case ClassS: s += usgn ? "_u64" : "_s64"; break;
426 case ClassI: s += "_i64"; break;
427 case ClassW: s += "_64"; break;
428 default: break;
429 }
430 break;
431 case 'h':
432 switch (ck) {
433 case ClassS:
434 case ClassI: s += "_f16"; break;
435 case ClassW: s += "_16"; break;
436 default: break;
437 }
438 break;
439 case 'f':
440 switch (ck) {
441 case ClassS:
442 case ClassI: s += "_f32"; break;
443 case ClassW: s += "_32"; break;
444 default: break;
445 }
446 break;
447 default:
448 throw "unhandled type!";
449 break;
Nate Begemanaf905ef2010-06-02 06:17:19 +0000450 }
Nate Begemana8979a02010-06-04 00:21:41 +0000451 if (ck == ClassB)
Nate Begeman92f98af2010-06-04 07:11:25 +0000452 s += "_v";
Bob Wilson7f762182010-12-04 04:40:15 +0000453
454 // Insert a 'q' before the first '_' character so that it ends up before
Nate Begemanaf905ef2010-06-02 06:17:19 +0000455 // _lane or _n on vector-scalar operations.
456 if (quad) {
457 size_t pos = s.find('_');
458 s = s.insert(pos, "q");
459 }
460 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000461}
462
Nate Begemanaf905ef2010-06-02 06:17:19 +0000463// Generate the string "(argtype a, argtype b, ...)"
Nate Begeman22237772010-06-02 00:34:55 +0000464static std::string GenArgs(const std::string &proto, StringRef typestr) {
Nate Begeman6c060db2010-06-09 01:09:00 +0000465 bool define = proto.find('i') != std::string::npos;
Nate Begemanaf905ef2010-06-02 06:17:19 +0000466 char arg = 'a';
Bob Wilson7f762182010-12-04 04:40:15 +0000467
Nate Begemanaf905ef2010-06-02 06:17:19 +0000468 std::string s;
469 s += "(";
Bob Wilson7f762182010-12-04 04:40:15 +0000470
Nate Begemanaf905ef2010-06-02 06:17:19 +0000471 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
Bob Wilson194aa582010-12-03 00:34:09 +0000472 if (define) {
473 // Immediate macro arguments are used directly instead of being assigned
474 // to local temporaries; prepend an underscore prefix to make their
475 // names consistent with the local temporaries.
476 if (proto[i] == 'i')
477 s += "__";
478 } else {
479 s += TypeString(proto[i], typestr) + " __";
Nate Begeman6c060db2010-06-09 01:09:00 +0000480 }
Nate Begemanaf905ef2010-06-02 06:17:19 +0000481 s.push_back(arg);
482 if ((i + 1) < e)
483 s += ", ";
484 }
Bob Wilson7f762182010-12-04 04:40:15 +0000485
Nate Begemanaf905ef2010-06-02 06:17:19 +0000486 s += ")";
487 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000488}
489
Bob Wilson194aa582010-12-03 00:34:09 +0000490// Macro arguments are not type-checked like inline function arguments, so
491// assign them to local temporaries to get the right type checking.
Bob Wilson377296e2010-12-02 07:10:39 +0000492static std::string GenMacroLocals(const std::string &proto, StringRef typestr) {
493 char arg = 'a';
494 std::string s;
Bob Wilson7f762182010-12-04 04:40:15 +0000495
Bob Wilson377296e2010-12-02 07:10:39 +0000496 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
497 // Do not create a temporary for an immediate argument.
498 // That would defeat the whole point of using a macro!
499 if (proto[i] == 'i') continue;
500
501 s += TypeString(proto[i], typestr) + " __";
502 s.push_back(arg);
503 s += " = (";
504 s.push_back(arg);
505 s += "); ";
506 }
Bob Wilson7f762182010-12-04 04:40:15 +0000507
Bob Wilson377296e2010-12-02 07:10:39 +0000508 s += "\\\n ";
509 return s;
510}
511
Bob Wilsonc4ba09d2010-12-07 20:02:45 +0000512// Use the vmovl builtin to sign-extend or zero-extend a vector.
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000513static std::string Extend(StringRef typestr, const std::string &a) {
Bob Wilsonc4ba09d2010-12-07 20:02:45 +0000514 std::string s;
515 s = MangleName("vmovl", typestr, ClassS);
516 s += "(" + a + ")";
517 return s;
518}
519
Bob Wilson7f762182010-12-04 04:40:15 +0000520static std::string Duplicate(unsigned nElts, StringRef typestr,
Nate Begemancc3c41a2010-06-12 03:09:49 +0000521 const std::string &a) {
Nate Begeman4b425a82010-06-10 00:16:56 +0000522 std::string s;
Bob Wilson7f762182010-12-04 04:40:15 +0000523
Bob Wilson6904d7a2010-11-16 19:39:14 +0000524 s = "(" + TypeString('d', typestr) + "){ ";
Nate Begeman4b425a82010-06-10 00:16:56 +0000525 for (unsigned i = 0; i != nElts; ++i) {
526 s += a;
527 if ((i + 1) < nElts)
528 s += ", ";
529 }
530 s += " }";
Bob Wilson7f762182010-12-04 04:40:15 +0000531
Nate Begeman4b425a82010-06-10 00:16:56 +0000532 return s;
533}
534
Bob Wilsonb0d98692010-12-03 00:34:12 +0000535static std::string SplatLane(unsigned nElts, const std::string &vec,
536 const std::string &lane) {
537 std::string s = "__builtin_shufflevector(" + vec + ", " + vec;
538 for (unsigned i = 0; i < nElts; ++i)
539 s += ", " + lane;
540 s += ")";
541 return s;
542}
543
Bob Wilsonb308b622010-11-16 23:57:01 +0000544static unsigned GetNumElements(StringRef typestr, bool &quad) {
545 quad = false;
546 bool dummy = false;
Nate Begemancc3c41a2010-06-12 03:09:49 +0000547 char type = ClassifyType(typestr, quad, dummy, dummy);
548 unsigned nElts = 0;
549 switch (type) {
Bob Wilsonb308b622010-11-16 23:57:01 +0000550 case 'c': nElts = 8; break;
551 case 's': nElts = 4; break;
552 case 'i': nElts = 2; break;
553 case 'l': nElts = 1; break;
554 case 'h': nElts = 4; break;
555 case 'f': nElts = 2; break;
556 default:
557 throw "unhandled type!";
558 break;
Nate Begemancc3c41a2010-06-12 03:09:49 +0000559 }
Bob Wilsonb308b622010-11-16 23:57:01 +0000560 if (quad) nElts <<= 1;
561 return nElts;
562}
563
564// Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
565static std::string GenOpString(OpKind op, const std::string &proto,
566 StringRef typestr) {
567 bool quad;
568 unsigned nElts = GetNumElements(typestr, quad);
Bob Wilson7f762182010-12-04 04:40:15 +0000569
Bob Wilson194aa582010-12-03 00:34:09 +0000570 // If this builtin takes an immediate argument, we need to #define it rather
571 // than use a standard declaration, so that SemaChecking can range check
572 // the immediate passed by the user.
573 bool define = proto.find('i') != std::string::npos;
574
Nate Begeman4b425a82010-06-10 00:16:56 +0000575 std::string ts = TypeString(proto[0], typestr);
Bob Wilsonee9ca072010-09-15 01:52:33 +0000576 std::string s;
577 if (op == OpHi || op == OpLo) {
Bob Wilson37a0b542010-12-02 07:44:23 +0000578 s = "union { " + ts + " r; double d; } u; u.d = ";
Bob Wilson194aa582010-12-03 00:34:09 +0000579 } else 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 Wilson194aa582010-12-03 00:34:09 +0000728 s += "(((float64x2_t)__a)[1]);";
Nate Begeman6c060db2010-06-09 01:09:00 +0000729 break;
730 case OpLo:
Bob Wilson194aa582010-12-03 00:34:09 +0000731 s += "(((float64x2_t)__a)[0]);";
Nate Begeman6c060db2010-06-09 01:09:00 +0000732 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000733 case OpDup:
Bob Wilson194aa582010-12-03 00:34:09 +0000734 s += Duplicate(nElts, typestr, "__a") + ";";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000735 break;
Bob Wilson2196caa2010-12-07 22:39:24 +0000736 case OpDupLane:
737 s += SplatLane(nElts, "__a", "__b") + ";";
738 break;
Nate Begemancc3c41a2010-06-12 03:09:49 +0000739 case OpSelect:
740 // ((0 & 1) | (~0 & 2))
Bob Wilson1dbfa912010-12-02 01:18:20 +0000741 s += "(" + ts + ")";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000742 ts = TypeString(proto[1], typestr);
Bob Wilson194aa582010-12-03 00:34:09 +0000743 s += "((__a & (" + ts + ")__b) | ";
744 s += "(~__a & (" + ts + ")__c));";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000745 break;
746 case OpRev16:
Bob Wilson194aa582010-12-03 00:34:09 +0000747 s += "__builtin_shufflevector(__a, __a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000748 for (unsigned i = 2; i <= nElts; i += 2)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000749 for (unsigned j = 0; j != 2; ++j)
750 s += ", " + utostr(i - j - 1);
Bob Wilson37a0b542010-12-02 07:44:23 +0000751 s += ");";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000752 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000753 case OpRev32: {
754 unsigned WordElts = nElts >> (1 + (int)quad);
Bob Wilson194aa582010-12-03 00:34:09 +0000755 s += "__builtin_shufflevector(__a, __a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000756 for (unsigned i = WordElts; i <= nElts; i += WordElts)
757 for (unsigned j = 0; j != WordElts; ++j)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000758 s += ", " + utostr(i - j - 1);
Bob Wilson37a0b542010-12-02 07:44:23 +0000759 s += ");";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000760 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000761 }
762 case OpRev64: {
763 unsigned DblWordElts = nElts >> (int)quad;
Bob Wilson194aa582010-12-03 00:34:09 +0000764 s += "__builtin_shufflevector(__a, __a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000765 for (unsigned i = DblWordElts; i <= nElts; i += DblWordElts)
766 for (unsigned j = 0; j != DblWordElts; ++j)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000767 s += ", " + utostr(i - j - 1);
Bob Wilson37a0b542010-12-02 07:44:23 +0000768 s += ");";
Nate Begeman900f4672010-06-08 00:14:42 +0000769 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000770 }
Bob Wilsonb4504302010-12-08 21:39:04 +0000771 case OpAbdl: {
772 std::string abd = MangleName("vabd", typestr, ClassS) + "(__a, __b)";
773 if (typestr[0] != 'U') {
774 // vabd results are always unsigned and must be zero-extended.
775 std::string utype = "U" + typestr.str();
776 s += "(" + TypeString(proto[0], typestr) + ")";
777 abd = "(" + TypeString('d', utype) + ")" + abd;
778 s += Extend(utype, abd) + ";";
779 } else {
780 s += Extend(typestr, abd) + ";";
781 }
782 break;
783 }
Bob Wilsonf4f39d32010-12-08 20:09:10 +0000784 case OpAba:
785 s += "__a + " + MangleName("vabd", typestr, ClassS) + "(__b, __c);";
786 break;
Bob Wilsonb4504302010-12-08 21:39:04 +0000787 case OpAbal: {
788 s += "__a + ";
789 std::string abd = MangleName("vabd", typestr, ClassS) + "(__b, __c)";
790 if (typestr[0] != 'U') {
791 // vabd results are always unsigned and must be zero-extended.
792 std::string utype = "U" + typestr.str();
793 s += "(" + TypeString(proto[0], typestr) + ")";
794 abd = "(" + TypeString('d', utype) + ")" + abd;
795 s += Extend(utype, abd) + ";";
796 } else {
797 s += Extend(typestr, abd) + ";";
798 }
799 break;
800 }
Nate Begeman162d3ba2010-06-03 04:04:09 +0000801 default:
802 throw "unknown OpKind!";
803 break;
804 }
Bob Wilson194aa582010-12-03 00:34:09 +0000805 if (op == OpHi || op == OpLo) {
806 if (!define)
807 s += " return";
808 s += " u.r;";
809 }
Nate Begeman162d3ba2010-06-03 04:04:09 +0000810 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000811}
812
Nate Begemanb0a4e452010-06-07 16:00:37 +0000813static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
814 unsigned mod = proto[0];
815 unsigned ret = 0;
816
Nate Begeman900f4672010-06-08 00:14:42 +0000817 if (mod == 'v' || mod == 'f')
Nate Begemanb0a4e452010-06-07 16:00:37 +0000818 mod = proto[1];
819
820 bool quad = false;
821 bool poly = false;
822 bool usgn = false;
823 bool scal = false;
824 bool cnst = false;
825 bool pntr = false;
Bob Wilson7f762182010-12-04 04:40:15 +0000826
Nate Begeman59d70cb2010-08-06 01:24:11 +0000827 // Base type to get the type string for.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000828 char type = ClassifyType(typestr, quad, poly, usgn);
Bob Wilson7f762182010-12-04 04:40:15 +0000829
Nate Begemanb0a4e452010-06-07 16:00:37 +0000830 // Based on the modifying character, change the type and width if necessary.
831 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
Nate Begeman59d70cb2010-08-06 01:24:11 +0000832
Nate Begemanb0a4e452010-06-07 16:00:37 +0000833 if (usgn)
834 ret |= 0x08;
Nate Begeman59d70cb2010-08-06 01:24:11 +0000835 if (quad && proto[1] != 'g')
Nate Begemanb0a4e452010-06-07 16:00:37 +0000836 ret |= 0x10;
Bob Wilson7f762182010-12-04 04:40:15 +0000837
Nate Begemanb0a4e452010-06-07 16:00:37 +0000838 switch (type) {
Bob Wilson7f762182010-12-04 04:40:15 +0000839 case 'c':
Nate Begemanb0a4e452010-06-07 16:00:37 +0000840 ret |= poly ? 5 : 0;
841 break;
842 case 's':
843 ret |= poly ? 6 : 1;
844 break;
845 case 'i':
846 ret |= 2;
847 break;
848 case 'l':
849 ret |= 3;
850 break;
851 case 'h':
852 ret |= 7;
853 break;
854 case 'f':
855 ret |= 4;
856 break;
857 default:
858 throw "unhandled type!";
859 break;
860 }
861 return ret;
862}
863
Nate Begeman7c8c8832010-06-02 21:53:00 +0000864// Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
Nate Begeman7c8c8832010-06-02 21:53:00 +0000865static std::string GenBuiltin(const std::string &name, const std::string &proto,
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000866 StringRef typestr, ClassKind ck) {
Nate Begeman162d3ba2010-06-03 04:04:09 +0000867 std::string s;
Nate Begeman9e584b32010-06-04 22:53:30 +0000868
Nate Begemanc4a1b652010-06-20 21:09:52 +0000869 // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
870 // sret-like argument.
Bob Wilson5b7fe592010-12-01 19:49:51 +0000871 bool sret = (proto[0] >= '2' && proto[0] <= '4');
Nate Begemanc4a1b652010-06-20 21:09:52 +0000872
873 // If this builtin takes an immediate argument, we need to #define it rather
874 // than use a standard declaration, so that SemaChecking can range check
875 // the immediate passed by the user.
Nate Begeman6c060db2010-06-09 01:09:00 +0000876 bool define = proto.find('i') != std::string::npos;
Nate Begeman9e584b32010-06-04 22:53:30 +0000877
Bob Wilson95148ad2010-12-01 19:49:56 +0000878 // Check if the prototype has a scalar operand with the type of the vector
879 // elements. If not, bitcasting the args will take care of arg checking.
880 // The actual signedness etc. will be taken care of with special enums.
Nate Begeman9e584b32010-06-04 22:53:30 +0000881 if (proto.find('s') == std::string::npos)
882 ck = ClassB;
Nate Begeman7c21f742010-06-04 21:36:00 +0000883
Nate Begeman162d3ba2010-06-03 04:04:09 +0000884 if (proto[0] != 'v') {
Nate Begeman6c060db2010-06-09 01:09:00 +0000885 std::string ts = TypeString(proto[0], typestr);
Bob Wilson7f762182010-12-04 04:40:15 +0000886
Nate Begeman6c060db2010-06-09 01:09:00 +0000887 if (define) {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000888 if (sret)
Bob Wilson052008b2010-12-02 02:42:51 +0000889 s += ts + " r; ";
Bob Wilsone106bc12010-12-02 00:24:56 +0000890 else
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000891 s += "(" + ts + ")";
Nate Begemanc4a1b652010-06-20 21:09:52 +0000892 } else if (sret) {
893 s += ts + " r; ";
Nate Begeman9e584b32010-06-04 22:53:30 +0000894 } else {
Bob Wilson37a0b542010-12-02 07:44:23 +0000895 s += "return (" + ts + ")";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000896 }
Nate Begeman4b425a82010-06-10 00:16:56 +0000897 }
Bob Wilson7f762182010-12-04 04:40:15 +0000898
Nate Begeman4b425a82010-06-10 00:16:56 +0000899 bool splat = proto.find('a') != std::string::npos;
Bob Wilson7f762182010-12-04 04:40:15 +0000900
Nate Begeman7c8c8832010-06-02 21:53:00 +0000901 s += "__builtin_neon_";
Nate Begeman4b425a82010-06-10 00:16:56 +0000902 if (splat) {
Bob Wilson95148ad2010-12-01 19:49:56 +0000903 // Call the non-splat builtin: chop off the "_n" suffix from the name.
Nate Begeman4b425a82010-06-10 00:16:56 +0000904 std::string vname(name, 0, name.size()-2);
905 s += MangleName(vname, typestr, ck);
906 } else {
907 s += MangleName(name, typestr, ck);
908 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000909 s += "(";
Nate Begemanc4a1b652010-06-20 21:09:52 +0000910
911 // Pass the address of the return variable as the first argument to sret-like
912 // builtins.
913 if (sret)
914 s += "&r, ";
Bob Wilson7f762182010-12-04 04:40:15 +0000915
Bob Wilson377296e2010-12-02 07:10:39 +0000916 char arg = 'a';
Nate Begeman7c8c8832010-06-02 21:53:00 +0000917 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
Nate Begemancc3c41a2010-06-12 03:09:49 +0000918 std::string args = std::string(&arg, 1);
Bob Wilson95148ad2010-12-01 19:49:56 +0000919
Bob Wilson194aa582010-12-03 00:34:09 +0000920 // Use the local temporaries instead of the macro arguments.
921 args = "__" + args;
Bob Wilsonf00140c2010-12-01 19:49:58 +0000922
923 bool argQuad = false;
924 bool argPoly = false;
925 bool argUsgn = false;
926 bool argScalar = false;
927 bool dummy = false;
928 char argType = ClassifyType(typestr, argQuad, argPoly, argUsgn);
929 argType = ModType(proto[i], argType, argQuad, argPoly, argUsgn, argScalar,
930 dummy, dummy);
931
Nate Begeman9e584b32010-06-04 22:53:30 +0000932 // Handle multiple-vector values specially, emitting each subvector as an
933 // argument to the __builtin.
Bob Wilson5b7fe592010-12-01 19:49:51 +0000934 if (proto[i] >= '2' && proto[i] <= '4') {
Bob Wilsonf00140c2010-12-01 19:49:58 +0000935 // Check if an explicit cast is needed.
936 if (argType != 'c' || argPoly || argUsgn)
937 args = (argQuad ? "(int8x16_t)" : "(int8x8_t)") + args;
938
Nate Begeman9e584b32010-06-04 22:53:30 +0000939 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000940 s += args + ".val[" + utostr(vi) + "]";
Nate Begeman9e584b32010-06-04 22:53:30 +0000941 if ((vi + 1) < ve)
942 s += ", ";
943 }
944 if ((i + 1) < e)
945 s += ", ";
946
947 continue;
948 }
Bob Wilson7f762182010-12-04 04:40:15 +0000949
Bob Wilson4a6c7fc2010-12-02 01:18:23 +0000950 if (splat && (i + 1) == e)
951 args = Duplicate(GetNumElements(typestr, argQuad), typestr, args);
952
Bob Wilsonf00140c2010-12-01 19:49:58 +0000953 // Check if an explicit cast is needed.
Bob Wilson4a6c7fc2010-12-02 01:18:23 +0000954 if ((splat || !argScalar) &&
Bob Wilsonf00140c2010-12-01 19:49:58 +0000955 ((ck == ClassB && argType != 'c') || argPoly || argUsgn)) {
956 std::string argTypeStr = "c";
957 if (ck != ClassB)
958 argTypeStr = argType;
959 if (argQuad)
960 argTypeStr = "Q" + argTypeStr;
961 args = "(" + TypeString('d', argTypeStr) + ")" + args;
962 }
Bob Wilson7f762182010-12-04 04:40:15 +0000963
Bob Wilson4a6c7fc2010-12-02 01:18:23 +0000964 s += args;
Nate Begeman7c8c8832010-06-02 21:53:00 +0000965 if ((i + 1) < e)
966 s += ", ";
967 }
Bob Wilson7f762182010-12-04 04:40:15 +0000968
Nate Begeman9e584b32010-06-04 22:53:30 +0000969 // Extra constant integer to hold type class enum for this function, e.g. s8
Nate Begeman9e584b32010-06-04 22:53:30 +0000970 if (ck == ClassB)
Nate Begemanb0a4e452010-06-07 16:00:37 +0000971 s += ", " + utostr(GetNeonEnum(proto, typestr));
Bob Wilson7f762182010-12-04 04:40:15 +0000972
Bob Wilson052008b2010-12-02 02:42:51 +0000973 s += ");";
Nate Begeman9e584b32010-06-04 22:53:30 +0000974
Bob Wilson37a0b542010-12-02 07:44:23 +0000975 if (proto[0] != 'v' && sret) {
976 if (define)
977 s += " r;";
978 else
Nate Begemanc4a1b652010-06-20 21:09:52 +0000979 s += " return r;";
Nate Begeman9e584b32010-06-04 22:53:30 +0000980 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000981 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000982}
983
Bob Wilson7f762182010-12-04 04:40:15 +0000984static std::string GenBuiltinDef(const std::string &name,
Nate Begeman73cef3e2010-06-04 01:26:15 +0000985 const std::string &proto,
986 StringRef typestr, ClassKind ck) {
987 std::string s("BUILTIN(__builtin_neon_");
Nate Begeman92f98af2010-06-04 07:11:25 +0000988
Bob Wilson7f762182010-12-04 04:40:15 +0000989 // If all types are the same size, bitcasting the args will take care
Nate Begeman92f98af2010-06-04 07:11:25 +0000990 // of arg checking. The actual signedness etc. will be taken care of with
991 // special enums.
992 if (proto.find('s') == std::string::npos)
993 ck = ClassB;
Bob Wilson7f762182010-12-04 04:40:15 +0000994
Nate Begeman73cef3e2010-06-04 01:26:15 +0000995 s += MangleName(name, typestr, ck);
996 s += ", \"";
Bob Wilson7f762182010-12-04 04:40:15 +0000997
Nate Begeman92f98af2010-06-04 07:11:25 +0000998 for (unsigned i = 0, e = proto.size(); i != e; ++i)
Nate Begeman7c21f742010-06-04 21:36:00 +0000999 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
1000
1001 // Extra constant integer to hold type class enum for this function, e.g. s8
1002 if (ck == ClassB)
1003 s += "i";
Bob Wilson7f762182010-12-04 04:40:15 +00001004
Nate Begeman73cef3e2010-06-04 01:26:15 +00001005 s += "\", \"n\")";
1006 return s;
1007}
1008
Bob Wilson3890e392010-12-07 01:12:23 +00001009static std::string GenIntrinsic(const std::string &name,
1010 const std::string &proto,
1011 StringRef outTypeStr, StringRef inTypeStr,
1012 OpKind kind, ClassKind classKind) {
1013 assert(!proto.empty() && "");
1014 bool define = proto.find('i') != std::string::npos;
1015 std::string s;
1016
1017 // static always inline + return type
1018 if (define)
1019 s += "#define ";
1020 else
1021 s += "__ai " + TypeString(proto[0], outTypeStr) + " ";
1022
1023 // Function name with type suffix
1024 std::string mangledName = MangleName(name, outTypeStr, ClassS);
1025 if (outTypeStr != inTypeStr) {
1026 // If the input type is different (e.g., for vreinterpret), append a suffix
1027 // for the input type. String off a "Q" (quad) prefix so that MangleName
1028 // does not insert another "q" in the name.
1029 unsigned typeStrOff = (inTypeStr[0] == 'Q' ? 1 : 0);
1030 StringRef inTypeNoQuad = inTypeStr.substr(typeStrOff);
1031 mangledName = MangleName(mangledName, inTypeNoQuad, ClassS);
1032 }
1033 s += mangledName;
1034
1035 // Function arguments
1036 s += GenArgs(proto, inTypeStr);
1037
1038 // Definition.
1039 if (define) {
1040 s += " __extension__ ({ \\\n ";
1041 s += GenMacroLocals(proto, inTypeStr);
1042 } else {
1043 s += " { \\\n ";
1044 }
1045
1046 if (kind != OpNone)
1047 s += GenOpString(kind, proto, outTypeStr);
1048 else
1049 s += GenBuiltin(name, proto, outTypeStr, classKind);
1050 if (define)
1051 s += " })";
1052 else
1053 s += " }";
1054 s += "\n";
1055 return s;
1056}
1057
Nate Begemand72c9002010-06-13 04:47:03 +00001058/// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h
1059/// is comprised of type definitions and function declarations.
Nate Begeman5ddb0872010-05-28 01:08:32 +00001060void NeonEmitter::run(raw_ostream &OS) {
Bob Wilson0e1fb7a2010-12-09 18:31:01 +00001061 OS <<
Bob Wilsonb78558c2010-12-09 18:43:35 +00001062 "/*===---- arm_neon.h - ARM Neon intrinsics ------------------------------"
1063 "---===\n"
Bob Wilson0e1fb7a2010-12-09 18:31:01 +00001064 " *\n"
Bob Wilsonb78558c2010-12-09 18:43:35 +00001065 " * Permission is hereby granted, free of charge, to any person obtaining "
1066 "a copy\n"
1067 " * of this software and associated documentation files (the \"Software\"),"
1068 " to deal\n"
1069 " * in the Software without restriction, including without limitation the "
1070 "rights\n"
1071 " * to use, copy, modify, merge, publish, distribute, sublicense, "
1072 "and/or sell\n"
Bob Wilson0e1fb7a2010-12-09 18:31:01 +00001073 " * copies of the Software, and to permit persons to whom the Software is\n"
1074 " * furnished to do so, subject to the following conditions:\n"
1075 " *\n"
Bob Wilsonb78558c2010-12-09 18:43:35 +00001076 " * The above copyright notice and this permission notice shall be "
1077 "included in\n"
Bob Wilson0e1fb7a2010-12-09 18:31:01 +00001078 " * all copies or substantial portions of the Software.\n"
1079 " *\n"
Bob Wilsonb78558c2010-12-09 18:43:35 +00001080 " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, "
1081 "EXPRESS OR\n"
1082 " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
1083 "MERCHANTABILITY,\n"
1084 " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT "
1085 "SHALL THE\n"
1086 " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR "
1087 "OTHER\n"
1088 " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, "
1089 "ARISING FROM,\n"
1090 " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER "
1091 "DEALINGS IN\n"
Bob Wilson0e1fb7a2010-12-09 18:31:01 +00001092 " * THE SOFTWARE.\n"
1093 " *\n"
Bob Wilsonb78558c2010-12-09 18:43:35 +00001094 " *===--------------------------------------------------------------------"
1095 "---===\n"
Bob Wilson0e1fb7a2010-12-09 18:31:01 +00001096 " */\n\n";
Bob Wilson7f762182010-12-04 04:40:15 +00001097
Nate Begeman5ddb0872010-05-28 01:08:32 +00001098 OS << "#ifndef __ARM_NEON_H\n";
1099 OS << "#define __ARM_NEON_H\n\n";
Bob Wilson7f762182010-12-04 04:40:15 +00001100
Nate Begeman5ddb0872010-05-28 01:08:32 +00001101 OS << "#ifndef __ARM_NEON__\n";
1102 OS << "#error \"NEON support not enabled\"\n";
1103 OS << "#endif\n\n";
1104
1105 OS << "#include <stdint.h>\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +00001106
1107 // Emit NEON-specific scalar typedefs.
Nate Begeman7c8c8832010-06-02 21:53:00 +00001108 OS << "typedef float float32_t;\n";
Bob Wilson4fbf6382010-11-16 23:57:03 +00001109 OS << "typedef int8_t poly8_t;\n";
1110 OS << "typedef int16_t poly16_t;\n";
Nate Begeman162d3ba2010-06-03 04:04:09 +00001111 OS << "typedef uint16_t float16_t;\n";
Nate Begeman9e584b32010-06-04 22:53:30 +00001112
Nate Begeman7c8c8832010-06-02 21:53:00 +00001113 // Emit Neon vector typedefs.
1114 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
1115 SmallVector<StringRef, 24> TDTypeVec;
1116 ParseTypes(0, TypedefTypes, TDTypeVec);
1117
1118 // Emit vector typedefs.
Bob Wilson6904d7a2010-11-16 19:39:14 +00001119 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
Bob Wilson6a8aceb2010-11-16 23:57:06 +00001120 bool dummy, quad = false, poly = false;
1121 (void) ClassifyType(TDTypeVec[i], quad, poly, dummy);
1122 if (poly)
1123 OS << "typedef __attribute__((neon_polyvector_type(";
1124 else
1125 OS << "typedef __attribute__((neon_vector_type(";
Bob Wilson7f762182010-12-04 04:40:15 +00001126
Bob Wilson6a8aceb2010-11-16 23:57:06 +00001127 unsigned nElts = GetNumElements(TDTypeVec[i], quad);
1128 OS << utostr(nElts) << "))) ";
1129 if (nElts < 10)
Bob Wilson6904d7a2010-11-16 19:39:14 +00001130 OS << " ";
Bob Wilson7f762182010-12-04 04:40:15 +00001131
Bob Wilson6904d7a2010-11-16 19:39:14 +00001132 OS << TypeString('s', TDTypeVec[i]);
1133 OS << " " << TypeString('d', TDTypeVec[i]) << ";\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +00001134 }
1135 OS << "\n";
Bob Wilson6a8aceb2010-11-16 23:57:06 +00001136 OS << "typedef __attribute__((__vector_size__(8))) "
Bob Wilson6904d7a2010-11-16 19:39:14 +00001137 "double float64x1_t;\n";
Bob Wilson6a8aceb2010-11-16 23:57:06 +00001138 OS << "typedef __attribute__((__vector_size__(16))) "
Bob Wilson6904d7a2010-11-16 19:39:14 +00001139 "double float64x2_t;\n";
Bob Wilsonee9ca072010-09-15 01:52:33 +00001140 OS << "\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +00001141
1142 // Emit struct typedefs.
Bob Wilson6904d7a2010-11-16 19:39:14 +00001143 for (unsigned vi = 2; vi != 5; ++vi) {
Nate Begeman7c8c8832010-06-02 21:53:00 +00001144 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
Bob Wilson6904d7a2010-11-16 19:39:14 +00001145 std::string ts = TypeString('d', TDTypeVec[i]);
1146 std::string vs = TypeString('0' + vi, TDTypeVec[i]);
1147 OS << "typedef struct " << vs << " {\n";
1148 OS << " " << ts << " val";
1149 OS << "[" << utostr(vi) << "]";
1150 OS << ";\n} ";
Bob Wilsonf18dfec2010-11-16 19:16:06 +00001151 OS << vs << ";\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +00001152 }
1153 }
Bob Wilson7f762182010-12-04 04:40:15 +00001154
Nate Begeman7c8c8832010-06-02 21:53:00 +00001155 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
1156
Nate Begeman5ddb0872010-05-28 01:08:32 +00001157 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
Bob Wilson7f762182010-12-04 04:40:15 +00001158
Bob Wilsonf4f39d32010-12-08 20:09:10 +00001159 // Emit vmovl and vabd intrinsics first so they can be used by other
Bob Wilson74410892010-12-08 22:36:08 +00001160 // intrinsics. (Some of the saturating multiply instructions are also
1161 // used to implement the corresponding "_lane" variants, but tablegen
1162 // sorts the records into alphabetical order so that the "_lane" variants
1163 // come after the intrinsics they use.)
Bob Wilsonda1d3dc2010-12-07 23:53:32 +00001164 emitIntrinsic(OS, Records.getDef("VMOVL"));
Bob Wilsonf4f39d32010-12-08 20:09:10 +00001165 emitIntrinsic(OS, Records.getDef("VABD"));
Bob Wilsonda1d3dc2010-12-07 23:53:32 +00001166
Nate Begeman5ddb0872010-05-28 01:08:32 +00001167 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1168 Record *R = RV[i];
Bob Wilsonf4f39d32010-12-08 20:09:10 +00001169 if (R->getName() != "VMOVL" && R->getName() != "VABD")
Bob Wilsonda1d3dc2010-12-07 23:53:32 +00001170 emitIntrinsic(OS, R);
Nate Begeman5ddb0872010-05-28 01:08:32 +00001171 }
Bob Wilsonda1d3dc2010-12-07 23:53:32 +00001172
Nate Begeman73cef3e2010-06-04 01:26:15 +00001173 OS << "#undef __ai\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +00001174 OS << "#endif /* __ARM_NEON_H */\n";
Nate Begeman5ddb0872010-05-28 01:08:32 +00001175}
Nate Begemana8979a02010-06-04 00:21:41 +00001176
Bob Wilsonda1d3dc2010-12-07 23:53:32 +00001177/// emitIntrinsic - Write out the arm_neon.h header file definitions for the
1178/// intrinsics specified by record R.
1179void NeonEmitter::emitIntrinsic(raw_ostream &OS, Record *R) {
1180 std::string name = R->getValueAsString("Name");
1181 std::string Proto = R->getValueAsString("Prototype");
1182 std::string Types = R->getValueAsString("Types");
1183
1184 SmallVector<StringRef, 16> TypeVec;
1185 ParseTypes(R, Types, TypeVec);
1186
1187 OpKind kind = OpMap[R->getValueAsDef("Operand")->getName()];
1188
1189 ClassKind classKind = ClassNone;
1190 if (R->getSuperClasses().size() >= 2)
1191 classKind = ClassMap[R->getSuperClasses()[1]];
1192 if (classKind == ClassNone && kind == OpNone)
1193 throw TGError(R->getLoc(), "Builtin has no class kind");
1194
1195 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1196 if (kind == OpReinterpret) {
1197 bool outQuad = false;
1198 bool dummy = false;
1199 (void)ClassifyType(TypeVec[ti], outQuad, dummy, dummy);
1200 for (unsigned srcti = 0, srcte = TypeVec.size();
1201 srcti != srcte; ++srcti) {
1202 bool inQuad = false;
1203 (void)ClassifyType(TypeVec[srcti], inQuad, dummy, dummy);
1204 if (srcti == ti || inQuad != outQuad)
1205 continue;
1206 OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[srcti],
1207 OpCast, ClassS);
1208 }
1209 } else {
1210 OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[ti],
1211 kind, classKind);
1212 }
1213 }
1214 OS << "\n";
1215}
1216
Bob Wilson06b04aa2010-12-15 16:58:42 +00001217static unsigned RangeFromType(const char mod, StringRef typestr) {
Nate Begeman918f8e42010-06-14 05:17:23 +00001218 // base type to get the type string for.
1219 bool quad = false, dummy = false;
1220 char type = ClassifyType(typestr, quad, dummy, dummy);
Bob Wilson06b04aa2010-12-15 16:58:42 +00001221 type = ModType(mod, type, quad, dummy, dummy, dummy, dummy, dummy);
Bob Wilson7f762182010-12-04 04:40:15 +00001222
Nate Begeman918f8e42010-06-14 05:17:23 +00001223 switch (type) {
1224 case 'c':
Nate Begeman4da883a2010-06-15 22:10:31 +00001225 return (8 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001226 case 'h':
1227 case 's':
Nate Begeman4da883a2010-06-15 22:10:31 +00001228 return (4 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001229 case 'f':
1230 case 'i':
Nate Begeman4da883a2010-06-15 22:10:31 +00001231 return (2 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001232 case 'l':
Nate Begeman4da883a2010-06-15 22:10:31 +00001233 return (1 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001234 default:
1235 throw "unhandled type!";
1236 break;
1237 }
Bob Wilsonfdb530d2010-07-28 18:21:10 +00001238 assert(0 && "unreachable");
1239 return 0;
Nate Begeman918f8e42010-06-14 05:17:23 +00001240}
1241
Nate Begemanf8c4c272010-06-17 04:15:13 +00001242/// runHeader - Emit a file with sections defining:
1243/// 1. the NEON section of BuiltinsARM.def.
1244/// 2. the SemaChecking code for the type overload checking.
1245/// 3. the SemaChecking code for validation of intrinsic immedate arguments.
Nate Begemana8979a02010-06-04 00:21:41 +00001246void NeonEmitter::runHeader(raw_ostream &OS) {
Nate Begeman73cef3e2010-06-04 01:26:15 +00001247 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1248
1249 StringMap<OpKind> EmittedMap;
Bob Wilson7f762182010-12-04 04:40:15 +00001250
Nate Begemanf8c4c272010-06-17 04:15:13 +00001251 // Generate BuiltinsARM.def for NEON
1252 OS << "#ifdef GET_NEON_BUILTINS\n";
Nate Begeman73cef3e2010-06-04 01:26:15 +00001253 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1254 Record *R = RV[i];
Nate Begeman73cef3e2010-06-04 01:26:15 +00001255 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1256 if (k != OpNone)
1257 continue;
Nate Begeman73cef3e2010-06-04 01:26:15 +00001258
Nate Begemanf8c4c272010-06-17 04:15:13 +00001259 std::string Proto = R->getValueAsString("Prototype");
Bob Wilson7f762182010-12-04 04:40:15 +00001260
Nate Begemand72c9002010-06-13 04:47:03 +00001261 // Functions with 'a' (the splat code) in the type prototype should not get
1262 // their own builtin as they use the non-splat variant.
Nate Begeman4b425a82010-06-10 00:16:56 +00001263 if (Proto.find('a') != std::string::npos)
1264 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001265
Nate Begemanf8c4c272010-06-17 04:15:13 +00001266 std::string Types = R->getValueAsString("Types");
Nate Begeman73cef3e2010-06-04 01:26:15 +00001267 SmallVector<StringRef, 16> TypeVec;
1268 ParseTypes(R, Types, TypeVec);
Bob Wilson7f762182010-12-04 04:40:15 +00001269
Nate Begeman73cef3e2010-06-04 01:26:15 +00001270 if (R->getSuperClasses().size() < 2)
1271 throw TGError(R->getLoc(), "Builtin has no class kind");
Bob Wilson7f762182010-12-04 04:40:15 +00001272
Bob Wilsonce0bb542010-12-03 17:19:39 +00001273 std::string name = R->getValueAsString("Name");
Nate Begeman73cef3e2010-06-04 01:26:15 +00001274 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
Bob Wilson7f762182010-12-04 04:40:15 +00001275
Nate Begeman73cef3e2010-06-04 01:26:15 +00001276 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
Nate Begemand72c9002010-06-13 04:47:03 +00001277 // Generate the BuiltinsARM.def declaration for this builtin, ensuring
1278 // that each unique BUILTIN() macro appears only once in the output
1279 // stream.
Nate Begeman73cef3e2010-06-04 01:26:15 +00001280 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
1281 if (EmittedMap.count(bd))
1282 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001283
Nate Begeman73cef3e2010-06-04 01:26:15 +00001284 EmittedMap[bd] = OpNone;
1285 OS << bd << "\n";
1286 }
Nate Begemanf8c4c272010-06-17 04:15:13 +00001287 }
1288 OS << "#endif\n\n";
Bob Wilson7f762182010-12-04 04:40:15 +00001289
Nate Begemanf8c4c272010-06-17 04:15:13 +00001290 // Generate the overloaded type checking code for SemaChecking.cpp
1291 OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
1292 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1293 Record *R = RV[i];
1294 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1295 if (k != OpNone)
Nate Begemand72c9002010-06-13 04:47:03 +00001296 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001297
Nate Begemanf8c4c272010-06-17 04:15:13 +00001298 std::string Proto = R->getValueAsString("Prototype");
1299 std::string Types = R->getValueAsString("Types");
Bob Wilsonce0bb542010-12-03 17:19:39 +00001300 std::string name = R->getValueAsString("Name");
Bob Wilson7f762182010-12-04 04:40:15 +00001301
Nate Begemanf8c4c272010-06-17 04:15:13 +00001302 // Functions with 'a' (the splat code) in the type prototype should not get
1303 // their own builtin as they use the non-splat variant.
1304 if (Proto.find('a') != std::string::npos)
1305 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001306
Nate Begemanf8c4c272010-06-17 04:15:13 +00001307 // Functions which have a scalar argument cannot be overloaded, no need to
1308 // check them if we are emitting the type checking code.
1309 if (Proto.find('s') != std::string::npos)
1310 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001311
Nate Begemanf8c4c272010-06-17 04:15:13 +00001312 SmallVector<StringRef, 16> TypeVec;
1313 ParseTypes(R, Types, TypeVec);
Bob Wilson7f762182010-12-04 04:40:15 +00001314
Nate Begemanf8c4c272010-06-17 04:15:13 +00001315 if (R->getSuperClasses().size() < 2)
1316 throw TGError(R->getLoc(), "Builtin has no class kind");
Bob Wilson7f762182010-12-04 04:40:15 +00001317
Nate Begemanf8c4c272010-06-17 04:15:13 +00001318 int si = -1, qi = -1;
1319 unsigned mask = 0, qmask = 0;
1320 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1321 // Generate the switch case(s) for this builtin for the type validation.
1322 bool quad = false, poly = false, usgn = false;
1323 (void) ClassifyType(TypeVec[ti], quad, poly, usgn);
Bob Wilson7f762182010-12-04 04:40:15 +00001324
Nate Begemanf8c4c272010-06-17 04:15:13 +00001325 if (quad) {
1326 qi = ti;
1327 qmask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1328 } else {
1329 si = ti;
1330 mask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1331 }
1332 }
1333 if (mask)
Bob Wilson7f762182010-12-04 04:40:15 +00001334 OS << "case ARM::BI__builtin_neon_"
Bob Wilsond8b84702010-12-07 01:12:19 +00001335 << MangleName(name, TypeVec[si], ClassB)
1336 << ": mask = " << "0x" << utohexstr(mask) << "; break;\n";
Nate Begemanf8c4c272010-06-17 04:15:13 +00001337 if (qmask)
Bob Wilson7f762182010-12-04 04:40:15 +00001338 OS << "case ARM::BI__builtin_neon_"
Bob Wilsond8b84702010-12-07 01:12:19 +00001339 << MangleName(name, TypeVec[qi], ClassB)
1340 << ": mask = " << "0x" << utohexstr(qmask) << "; break;\n";
Nate Begemanf8c4c272010-06-17 04:15:13 +00001341 }
1342 OS << "#endif\n\n";
Bob Wilson7f762182010-12-04 04:40:15 +00001343
Nate Begemanf8c4c272010-06-17 04:15:13 +00001344 // Generate the intrinsic range checking code for shift/lane immediates.
1345 OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
1346 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1347 Record *R = RV[i];
Bob Wilson7f762182010-12-04 04:40:15 +00001348
Nate Begemanf8c4c272010-06-17 04:15:13 +00001349 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1350 if (k != OpNone)
1351 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001352
Bob Wilsonce0bb542010-12-03 17:19:39 +00001353 std::string name = R->getValueAsString("Name");
Nate Begemanf8c4c272010-06-17 04:15:13 +00001354 std::string Proto = R->getValueAsString("Prototype");
1355 std::string Types = R->getValueAsString("Types");
Bob Wilson7f762182010-12-04 04:40:15 +00001356
Nate Begemanf8c4c272010-06-17 04:15:13 +00001357 // Functions with 'a' (the splat code) in the type prototype should not get
1358 // their own builtin as they use the non-splat variant.
1359 if (Proto.find('a') != std::string::npos)
1360 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001361
Nate Begemanf8c4c272010-06-17 04:15:13 +00001362 // Functions which do not have an immediate do not need to have range
1363 // checking code emitted.
Bob Wilson06b04aa2010-12-15 16:58:42 +00001364 size_t immPos = Proto.find('i');
1365 if (immPos == std::string::npos)
Nate Begemanf8c4c272010-06-17 04:15:13 +00001366 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001367
Nate Begemanf8c4c272010-06-17 04:15:13 +00001368 SmallVector<StringRef, 16> TypeVec;
1369 ParseTypes(R, Types, TypeVec);
Bob Wilson7f762182010-12-04 04:40:15 +00001370
Nate Begemanf8c4c272010-06-17 04:15:13 +00001371 if (R->getSuperClasses().size() < 2)
1372 throw TGError(R->getLoc(), "Builtin has no class kind");
Bob Wilson7f762182010-12-04 04:40:15 +00001373
Nate Begemanf8c4c272010-06-17 04:15:13 +00001374 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
Bob Wilson7f762182010-12-04 04:40:15 +00001375
Nate Begemanf8c4c272010-06-17 04:15:13 +00001376 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1377 std::string namestr, shiftstr, rangestr;
Bob Wilson7f762182010-12-04 04:40:15 +00001378
Nate Begemanf8c4c272010-06-17 04:15:13 +00001379 // Builtins which are overloaded by type will need to have their upper
1380 // bound computed at Sema time based on the type constant.
1381 if (Proto.find('s') == std::string::npos) {
1382 ck = ClassB;
1383 if (R->getValueAsBit("isShift")) {
1384 shiftstr = ", true";
Bob Wilson7f762182010-12-04 04:40:15 +00001385
Nate Begemanf8c4c272010-06-17 04:15:13 +00001386 // Right shifts have an 'r' in the name, left shifts do not.
1387 if (name.find('r') != std::string::npos)
1388 rangestr = "l = 1; ";
1389 }
1390 rangestr += "u = RFT(TV" + shiftstr + ")";
1391 } else {
Bob Wilson06b04aa2010-12-15 16:58:42 +00001392 // The immediate generally refers to a lane in the preceding argument.
1393 assert(immPos > 0 && "unexpected immediate operand");
1394 rangestr = "u = " + utostr(RangeFromType(Proto[immPos-1], TypeVec[ti]));
Nate Begemanf8c4c272010-06-17 04:15:13 +00001395 }
Nate Begemanc4a1b652010-06-20 21:09:52 +00001396 // Make sure cases appear only once by uniquing them in a string map.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001397 namestr = MangleName(name, TypeVec[ti], ck);
1398 if (EmittedMap.count(namestr))
1399 continue;
1400 EmittedMap[namestr] = OpNone;
Nate Begemanc4a1b652010-06-20 21:09:52 +00001401
1402 // Calculate the index of the immediate that should be range checked.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001403 unsigned immidx = 0;
Bob Wilson7f762182010-12-04 04:40:15 +00001404
Nate Begemanc4a1b652010-06-20 21:09:52 +00001405 // Builtins that return a struct of multiple vectors have an extra
1406 // leading arg for the struct return.
Bob Wilson5b7fe592010-12-01 19:49:51 +00001407 if (Proto[0] >= '2' && Proto[0] <= '4')
Nate Begemanc4a1b652010-06-20 21:09:52 +00001408 ++immidx;
Bob Wilson7f762182010-12-04 04:40:15 +00001409
1410 // Add one to the index for each argument until we reach the immediate
Nate Begemanc4a1b652010-06-20 21:09:52 +00001411 // to be checked. Structs of vectors are passed as multiple arguments.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001412 for (unsigned ii = 1, ie = Proto.size(); ii != ie; ++ii) {
1413 switch (Proto[ii]) {
1414 default: immidx += 1; break;
1415 case '2': immidx += 2; break;
1416 case '3': immidx += 3; break;
1417 case '4': immidx += 4; break;
1418 case 'i': ie = ii + 1; break;
1419 }
1420 }
Bob Wilsond8b84702010-12-07 01:12:19 +00001421 OS << "case ARM::BI__builtin_neon_" << MangleName(name, TypeVec[ti], ck)
Nate Begemanf8c4c272010-06-17 04:15:13 +00001422 << ": i = " << immidx << "; " << rangestr << "; break;\n";
Nate Begemand72c9002010-06-13 04:47:03 +00001423 }
Nate Begeman73cef3e2010-06-04 01:26:15 +00001424 }
Nate Begemanf8c4c272010-06-17 04:15:13 +00001425 OS << "#endif\n\n";
Nate Begemana8979a02010-06-04 00:21:41 +00001426}
Bob Wilson333f5192010-12-15 16:58:45 +00001427
1428/// GenTest - Write out a test for the intrinsic specified by the name and
1429/// type strings, including the embedded patterns for FileCheck to match.
1430static std::string GenTest(const std::string &name,
1431 const std::string &proto,
1432 StringRef outTypeStr, StringRef inTypeStr,
1433 bool isShift) {
1434 assert(!proto.empty() && "");
1435 std::string s;
1436
1437 // Function name with type suffix
1438 std::string mangledName = MangleName(name, outTypeStr, ClassS);
1439 if (outTypeStr != inTypeStr) {
1440 // If the input type is different (e.g., for vreinterpret), append a suffix
1441 // for the input type. String off a "Q" (quad) prefix so that MangleName
1442 // does not insert another "q" in the name.
1443 unsigned typeStrOff = (inTypeStr[0] == 'Q' ? 1 : 0);
1444 StringRef inTypeNoQuad = inTypeStr.substr(typeStrOff);
1445 mangledName = MangleName(mangledName, inTypeNoQuad, ClassS);
1446 }
1447
1448 // Emit the FileCheck patterns.
1449 s += "// CHECK: test_" + mangledName + "\n";
1450 // s += "// CHECK: \n"; // FIXME: + expected instruction opcode.
1451
1452 // Emit the start of the test function.
1453 s += TypeString(proto[0], outTypeStr) + " test_" + mangledName + "(";
1454 char arg = 'a';
1455 std::string comma;
1456 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
1457 // Do not create arguments for values that must be immediate constants.
1458 if (proto[i] == 'i')
1459 continue;
1460 s += comma + TypeString(proto[i], inTypeStr) + " ";
1461 s.push_back(arg);
1462 comma = ", ";
1463 }
1464 s += ") { \\\n ";
1465
1466 if (proto[0] != 'v')
1467 s += "return ";
1468 s += mangledName + "(";
1469 arg = 'a';
1470 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
1471 if (proto[i] == 'i') {
1472 // For immediate operands, test the maximum value.
1473 if (isShift)
1474 s += "1"; // FIXME
1475 else
1476 // The immediate generally refers to a lane in the preceding argument.
1477 s += utostr(RangeFromType(proto[i-1], inTypeStr));
1478 } else {
1479 s.push_back(arg);
1480 }
1481 if ((i + 1) < e)
1482 s += ", ";
1483 }
1484 s += ");\n}\n\n";
1485 return s;
1486}
1487
1488/// runTests - Write out a complete set of tests for all of the Neon
1489/// intrinsics.
1490void NeonEmitter::runTests(raw_ostream &OS) {
1491 OS <<
1492 "// RUN: %clang_cc1 -triple thumbv7-apple-darwin \\\n"
1493 "// RUN: -target-cpu cortex-a8 -ffreestanding -S -o - %s | FileCheck %s\n"
1494 "\n"
1495 "#include <arm_neon.h>\n"
1496 "\n";
1497
1498 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1499 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1500 Record *R = RV[i];
1501 std::string name = R->getValueAsString("Name");
1502 std::string Proto = R->getValueAsString("Prototype");
1503 std::string Types = R->getValueAsString("Types");
1504 bool isShift = R->getValueAsBit("isShift");
1505
1506 SmallVector<StringRef, 16> TypeVec;
1507 ParseTypes(R, Types, TypeVec);
1508
1509 OpKind kind = OpMap[R->getValueAsDef("Operand")->getName()];
1510 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1511 if (kind == OpReinterpret) {
1512 bool outQuad = false;
1513 bool dummy = false;
1514 (void)ClassifyType(TypeVec[ti], outQuad, dummy, dummy);
1515 for (unsigned srcti = 0, srcte = TypeVec.size();
1516 srcti != srcte; ++srcti) {
1517 bool inQuad = false;
1518 (void)ClassifyType(TypeVec[srcti], inQuad, dummy, dummy);
1519 if (srcti == ti || inQuad != outQuad)
1520 continue;
1521 OS << GenTest(name, Proto, TypeVec[ti], TypeVec[srcti], isShift);
1522 }
1523 } else {
1524 OS << GenTest(name, Proto, TypeVec[ti], TypeVec[ti], isShift);
1525 }
1526 }
1527 OS << "\n";
1528 }
1529}
1530