blob: 431f531c307e23f505b5d8e066abce0a00783069 [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"
Jim Grosbach0b6a44a2011-06-21 22:55:50 +000027#include "Error.h"
Nate Begeman22237772010-06-02 00:34:55 +000028#include "llvm/ADT/SmallString.h"
29#include "llvm/ADT/SmallVector.h"
Nate Begeman5ddb0872010-05-28 01:08:32 +000030#include "llvm/ADT/StringExtras.h"
Nate Begeman5ddb0872010-05-28 01:08:32 +000031#include <string>
32
33using namespace llvm;
34
Nate Begemand72c9002010-06-13 04:47:03 +000035/// ParseTypes - break down a string such as "fQf" into a vector of StringRefs,
36/// which each StringRef representing a single type declared in the string.
37/// for "fQf" we would end up with 2 StringRefs, "f", and "Qf", representing
38/// 2xfloat and 4xfloat respectively.
Nate Begeman22237772010-06-02 00:34:55 +000039static void ParseTypes(Record *r, std::string &s,
40 SmallVectorImpl<StringRef> &TV) {
41 const char *data = s.data();
42 int len = 0;
Bob Wilson7f762182010-12-04 04:40:15 +000043
Nate Begeman22237772010-06-02 00:34:55 +000044 for (unsigned i = 0, e = s.size(); i != e; ++i, ++len) {
45 if (data[len] == 'P' || data[len] == 'Q' || data[len] == 'U')
46 continue;
Bob Wilson7f762182010-12-04 04:40:15 +000047
Nate Begeman22237772010-06-02 00:34:55 +000048 switch (data[len]) {
49 case 'c':
50 case 's':
51 case 'i':
52 case 'l':
53 case 'h':
54 case 'f':
55 break;
56 default:
57 throw TGError(r->getLoc(),
58 "Unexpected letter: " + std::string(data + len, 1));
59 break;
60 }
61 TV.push_back(StringRef(data, len + 1));
62 data += len + 1;
63 len = -1;
64 }
65}
66
Nate Begemand72c9002010-06-13 04:47:03 +000067/// Widen - Convert a type code into the next wider type. char -> short,
68/// short -> int, etc.
Duncan Sands8dbbace2010-06-02 08:37:30 +000069static char Widen(const char t) {
Nate Begeman22237772010-06-02 00:34:55 +000070 switch (t) {
71 case 'c':
72 return 's';
73 case 's':
74 return 'i';
75 case 'i':
76 return 'l';
Bob Wilsonae6be762010-12-15 23:16:25 +000077 case 'h':
78 return 'f';
Nate Begeman22237772010-06-02 00:34:55 +000079 default: throw "unhandled type in widen!";
80 }
81 return '\0';
82}
83
Nate Begemand72c9002010-06-13 04:47:03 +000084/// Narrow - Convert a type code into the next smaller type. short -> char,
85/// float -> half float, etc.
Nate Begeman3861e742010-06-03 21:35:22 +000086static char Narrow(const char t) {
87 switch (t) {
88 case 's':
89 return 'c';
90 case 'i':
91 return 's';
92 case 'l':
93 return 'i';
Nate Begeman900f4672010-06-08 00:14:42 +000094 case 'f':
95 return 'h';
Bob Wilsonb055f742010-11-23 19:38:34 +000096 default: throw "unhandled type in narrow!";
Nate Begeman3861e742010-06-03 21:35:22 +000097 }
98 return '\0';
99}
100
Nate Begemand72c9002010-06-13 04:47:03 +0000101/// For a particular StringRef, return the base type code, and whether it has
102/// the quad-vector, polynomial, or unsigned modifiers set.
Nate Begemanaf905ef2010-06-02 06:17:19 +0000103static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) {
Nate Begeman22237772010-06-02 00:34:55 +0000104 unsigned off = 0;
Bob Wilson7f762182010-12-04 04:40:15 +0000105
Nate Begemanaf905ef2010-06-02 06:17:19 +0000106 // remember quad.
107 if (ty[off] == 'Q') {
108 quad = true;
109 ++off;
110 }
Bob Wilson7f762182010-12-04 04:40:15 +0000111
Nate Begemanaf905ef2010-06-02 06:17:19 +0000112 // remember poly.
113 if (ty[off] == 'P') {
114 poly = true;
115 ++off;
116 }
Bob Wilson7f762182010-12-04 04:40:15 +0000117
Nate Begemanaf905ef2010-06-02 06:17:19 +0000118 // remember unsigned.
119 if (ty[off] == 'U') {
120 usgn = true;
121 ++off;
122 }
Bob Wilson7f762182010-12-04 04:40:15 +0000123
Nate Begemanaf905ef2010-06-02 06:17:19 +0000124 // base type to get the type string for.
125 return ty[off];
126}
127
Nate Begemand72c9002010-06-13 04:47:03 +0000128/// ModType - Transform a type code and its modifiers based on a mod code. The
129/// mod code definitions may be found at the top of arm_neon.td.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000130static char ModType(const char mod, char type, bool &quad, bool &poly,
131 bool &usgn, bool &scal, bool &cnst, bool &pntr) {
Nate Begeman22237772010-06-02 00:34:55 +0000132 switch (mod) {
Nate Begeman22237772010-06-02 00:34:55 +0000133 case 't':
134 if (poly) {
135 poly = false;
136 usgn = true;
137 }
138 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000139 case 'u':
Nate Begeman22237772010-06-02 00:34:55 +0000140 usgn = true;
Bob Wilson181b76d2010-11-18 21:43:22 +0000141 poly = false;
142 if (type == 'f')
143 type = 'i';
144 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000145 case 'x':
Bob Wilson181b76d2010-11-18 21:43:22 +0000146 usgn = false;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000147 poly = false;
Nate Begeman22237772010-06-02 00:34:55 +0000148 if (type == 'f')
149 type = 'i';
150 break;
151 case 'f':
Nate Begeman900f4672010-06-08 00:14:42 +0000152 if (type == 'h')
153 quad = true;
Nate Begeman22237772010-06-02 00:34:55 +0000154 type = 'f';
Nate Begeman162d3ba2010-06-03 04:04:09 +0000155 usgn = false;
Nate Begeman22237772010-06-02 00:34:55 +0000156 break;
Nate Begeman59d70cb2010-08-06 01:24:11 +0000157 case 'g':
158 quad = false;
159 break;
Nate Begeman22237772010-06-02 00:34:55 +0000160 case 'w':
161 type = Widen(type);
162 quad = true;
163 break;
164 case 'n':
165 type = Widen(type);
166 break;
Bob Wilsonf00140c2010-12-01 19:49:58 +0000167 case 'i':
168 type = 'i';
169 scal = true;
170 break;
Nate Begeman22237772010-06-02 00:34:55 +0000171 case 'l':
172 type = 'l';
173 scal = true;
174 usgn = true;
175 break;
176 case 's':
Nate Begeman4b425a82010-06-10 00:16:56 +0000177 case 'a':
Nate Begeman22237772010-06-02 00:34:55 +0000178 scal = true;
179 break;
180 case 'k':
181 quad = true;
182 break;
183 case 'c':
184 cnst = true;
185 case 'p':
186 pntr = true;
187 scal = true;
188 break;
Nate Begeman3861e742010-06-03 21:35:22 +0000189 case 'h':
190 type = Narrow(type);
Nate Begeman900f4672010-06-08 00:14:42 +0000191 if (type == 'h')
192 quad = false;
Nate Begeman3861e742010-06-03 21:35:22 +0000193 break;
194 case 'e':
195 type = Narrow(type);
196 usgn = true;
197 break;
Nate Begeman22237772010-06-02 00:34:55 +0000198 default:
199 break;
200 }
Nate Begemanb0a4e452010-06-07 16:00:37 +0000201 return type;
202}
203
Nate Begemand72c9002010-06-13 04:47:03 +0000204/// TypeString - for a modifier and type, generate the name of the typedef for
Bob Wilson6904d7a2010-11-16 19:39:14 +0000205/// that type. QUc -> uint8x8_t.
206static std::string TypeString(const char mod, StringRef typestr) {
Nate Begemanb0a4e452010-06-07 16:00:37 +0000207 bool quad = false;
208 bool poly = false;
209 bool usgn = false;
210 bool scal = false;
211 bool cnst = false;
212 bool pntr = false;
Bob Wilson7f762182010-12-04 04:40:15 +0000213
Nate Begemanb0a4e452010-06-07 16:00:37 +0000214 if (mod == 'v')
215 return "void";
216 if (mod == 'i')
217 return "int";
Bob Wilson7f762182010-12-04 04:40:15 +0000218
Nate Begemanb0a4e452010-06-07 16:00:37 +0000219 // base type to get the type string for.
220 char type = ClassifyType(typestr, quad, poly, usgn);
Bob Wilson7f762182010-12-04 04:40:15 +0000221
Nate Begemanb0a4e452010-06-07 16:00:37 +0000222 // Based on the modifying character, change the type and width if necessary.
223 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
Bob Wilson7f762182010-12-04 04:40:15 +0000224
Nate Begeman22237772010-06-02 00:34:55 +0000225 SmallString<128> s;
Bob Wilson7f762182010-12-04 04:40:15 +0000226
Nate Begeman22237772010-06-02 00:34:55 +0000227 if (usgn)
228 s.push_back('u');
Bob Wilson7f762182010-12-04 04:40:15 +0000229
Nate Begeman22237772010-06-02 00:34:55 +0000230 switch (type) {
231 case 'c':
232 s += poly ? "poly8" : "int8";
233 if (scal)
234 break;
235 s += quad ? "x16" : "x8";
236 break;
237 case 's':
238 s += poly ? "poly16" : "int16";
239 if (scal)
240 break;
241 s += quad ? "x8" : "x4";
242 break;
243 case 'i':
244 s += "int32";
245 if (scal)
246 break;
247 s += quad ? "x4" : "x2";
248 break;
249 case 'l':
250 s += "int64";
251 if (scal)
252 break;
253 s += quad ? "x2" : "x1";
254 break;
255 case 'h':
256 s += "float16";
257 if (scal)
258 break;
259 s += quad ? "x8" : "x4";
260 break;
261 case 'f':
262 s += "float32";
263 if (scal)
264 break;
265 s += quad ? "x4" : "x2";
266 break;
Nate Begeman22237772010-06-02 00:34:55 +0000267 default:
268 throw "unhandled type!";
269 break;
270 }
271
272 if (mod == '2')
273 s += "x2";
274 if (mod == '3')
275 s += "x3";
276 if (mod == '4')
277 s += "x4";
Bob Wilson7f762182010-12-04 04:40:15 +0000278
Nate Begeman22237772010-06-02 00:34:55 +0000279 // Append _t, finishing the type string typedef type.
280 s += "_t";
Bob Wilson7f762182010-12-04 04:40:15 +0000281
Nate Begeman22237772010-06-02 00:34:55 +0000282 if (cnst)
283 s += " const";
Bob Wilson7f762182010-12-04 04:40:15 +0000284
Nate Begeman22237772010-06-02 00:34:55 +0000285 if (pntr)
286 s += " *";
Bob Wilson7f762182010-12-04 04:40:15 +0000287
Nate Begeman22237772010-06-02 00:34:55 +0000288 return s.str();
289}
290
Bob Wilson1ac27cf2010-06-24 22:04:30 +0000291/// BuiltinTypeString - for a modifier and type, generate the clang
292/// BuiltinsARM.def prototype code for the function. See the top of clang's
293/// Builtins.def for a description of the type strings.
Nate Begeman7c21f742010-06-04 21:36:00 +0000294static std::string BuiltinTypeString(const char mod, StringRef typestr,
295 ClassKind ck, bool ret) {
Nate Begeman92f98af2010-06-04 07:11:25 +0000296 bool quad = false;
297 bool poly = false;
298 bool usgn = false;
299 bool scal = false;
300 bool cnst = false;
301 bool pntr = false;
Bob Wilson7f762182010-12-04 04:40:15 +0000302
Nate Begeman92f98af2010-06-04 07:11:25 +0000303 if (mod == 'v')
Bob Wilson95148ad2010-12-01 19:49:56 +0000304 return "v"; // void
Nate Begeman92f98af2010-06-04 07:11:25 +0000305 if (mod == 'i')
Bob Wilson95148ad2010-12-01 19:49:56 +0000306 return "i"; // int
Bob Wilson7f762182010-12-04 04:40:15 +0000307
Nate Begeman92f98af2010-06-04 07:11:25 +0000308 // base type to get the type string for.
309 char type = ClassifyType(typestr, quad, poly, usgn);
Bob Wilson7f762182010-12-04 04:40:15 +0000310
Nate Begeman92f98af2010-06-04 07:11:25 +0000311 // Based on the modifying character, change the type and width if necessary.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000312 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
313
Bob Wilson95148ad2010-12-01 19:49:56 +0000314 // All pointers are void* pointers. Change type to 'v' now.
Nate Begemanc4a1b652010-06-20 21:09:52 +0000315 if (pntr) {
316 usgn = false;
317 poly = false;
Nate Begemanb0a4e452010-06-07 16:00:37 +0000318 type = 'v';
Nate Begemanc4a1b652010-06-20 21:09:52 +0000319 }
Bob Wilson95148ad2010-12-01 19:49:56 +0000320 // Treat half-float ('h') types as unsigned short ('s') types.
Nate Begeman92f98af2010-06-04 07:11:25 +0000321 if (type == 'h') {
322 type = 's';
323 usgn = true;
324 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000325 usgn = usgn | poly | ((ck == ClassI || ck == ClassW) && scal && type != 'f');
Nate Begeman92f98af2010-06-04 07:11:25 +0000326
327 if (scal) {
328 SmallString<128> s;
329
330 if (usgn)
331 s.push_back('U');
Bob Wilson317bafb2010-12-02 00:24:59 +0000332 else if (type == 'c')
333 s.push_back('S'); // make chars explicitly signed
Bob Wilson7f762182010-12-04 04:40:15 +0000334
Bob Wilson95148ad2010-12-01 19:49:56 +0000335 if (type == 'l') // 64-bit long
Nate Begeman7c21f742010-06-04 21:36:00 +0000336 s += "LLi";
337 else
338 s.push_back(type);
Bob Wilson7f762182010-12-04 04:40:15 +0000339
Nate Begeman92f98af2010-06-04 07:11:25 +0000340 if (cnst)
341 s.push_back('C');
342 if (pntr)
343 s.push_back('*');
344 return s.str();
345 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000346
347 // Since the return value must be one type, return a vector type of the
Nate Begemanc4a1b652010-06-20 21:09:52 +0000348 // appropriate width which we will bitcast. An exception is made for
349 // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
350 // fashion, storing them to a pointer arg.
Nate Begeman7c21f742010-06-04 21:36:00 +0000351 if (ret) {
Bob Wilson5b7fe592010-12-01 19:49:51 +0000352 if (mod >= '2' && mod <= '4')
Bob Wilson95148ad2010-12-01 19:49:56 +0000353 return "vv*"; // void result with void* first argument
Nate Begemanf50551e2010-06-09 18:02:26 +0000354 if (mod == 'f' || (ck != ClassB && type == 'f'))
Nate Begeman56387832010-06-08 06:01:16 +0000355 return quad ? "V4f" : "V2f";
Nate Begemanf50551e2010-06-09 18:02:26 +0000356 if (ck != ClassB && type == 's')
Nate Begeman007afe42010-06-09 05:11:55 +0000357 return quad ? "V8s" : "V4s";
Nate Begemanf50551e2010-06-09 18:02:26 +0000358 if (ck != ClassB && type == 'i')
Nate Begeman56387832010-06-08 06:01:16 +0000359 return quad ? "V4i" : "V2i";
Nate Begemanf50551e2010-06-09 18:02:26 +0000360 if (ck != ClassB && type == 'l')
Nate Begeman007afe42010-06-09 05:11:55 +0000361 return quad ? "V2LLi" : "V1LLi";
Bob Wilson7f762182010-12-04 04:40:15 +0000362
Bob Wilson317bafb2010-12-02 00:24:59 +0000363 return quad ? "V16Sc" : "V8Sc";
Bob Wilson7f762182010-12-04 04:40:15 +0000364 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000365
366 // Non-return array types are passed as individual vectors.
Nate Begeman92f98af2010-06-04 07:11:25 +0000367 if (mod == '2')
Bob Wilson317bafb2010-12-02 00:24:59 +0000368 return quad ? "V16ScV16Sc" : "V8ScV8Sc";
Nate Begeman92f98af2010-06-04 07:11:25 +0000369 if (mod == '3')
Bob Wilson317bafb2010-12-02 00:24:59 +0000370 return quad ? "V16ScV16ScV16Sc" : "V8ScV8ScV8Sc";
Nate Begeman92f98af2010-06-04 07:11:25 +0000371 if (mod == '4')
Bob Wilson317bafb2010-12-02 00:24:59 +0000372 return quad ? "V16ScV16ScV16ScV16Sc" : "V8ScV8ScV8ScV8Sc";
Nate Begeman92f98af2010-06-04 07:11:25 +0000373
Nate Begemanf50551e2010-06-09 18:02:26 +0000374 if (mod == 'f' || (ck != ClassB && type == 'f'))
Nate Begeman007afe42010-06-09 05:11:55 +0000375 return quad ? "V4f" : "V2f";
Nate Begemanf50551e2010-06-09 18:02:26 +0000376 if (ck != ClassB && type == 's')
Nate Begeman007afe42010-06-09 05:11:55 +0000377 return quad ? "V8s" : "V4s";
Nate Begemanf50551e2010-06-09 18:02:26 +0000378 if (ck != ClassB && type == 'i')
Nate Begeman007afe42010-06-09 05:11:55 +0000379 return quad ? "V4i" : "V2i";
Nate Begemanf50551e2010-06-09 18:02:26 +0000380 if (ck != ClassB && type == 'l')
Nate Begeman007afe42010-06-09 05:11:55 +0000381 return quad ? "V2LLi" : "V1LLi";
Bob Wilson7f762182010-12-04 04:40:15 +0000382
Bob Wilson317bafb2010-12-02 00:24:59 +0000383 return quad ? "V16Sc" : "V8Sc";
Nate Begeman92f98af2010-06-04 07:11:25 +0000384}
385
Bob Wilson7f762182010-12-04 04:40:15 +0000386/// MangleName - Append a type or width suffix to a base neon function name,
Nate Begemand72c9002010-06-13 04:47:03 +0000387/// and insert a 'q' in the appropriate location if the operation works on
388/// 128b rather than 64b. E.g. turn "vst2_lane" into "vst2q_lane_f32", etc.
Nate Begemana8979a02010-06-04 00:21:41 +0000389static std::string MangleName(const std::string &name, StringRef typestr,
390 ClassKind ck) {
Nate Begeman900f4672010-06-08 00:14:42 +0000391 if (name == "vcvt_f32_f16")
392 return name;
Bob Wilson7f762182010-12-04 04:40:15 +0000393
Nate Begemanaf905ef2010-06-02 06:17:19 +0000394 bool quad = false;
395 bool poly = false;
396 bool usgn = false;
397 char type = ClassifyType(typestr, quad, poly, usgn);
398
399 std::string s = name;
Bob Wilson7f762182010-12-04 04:40:15 +0000400
Nate Begemanaf905ef2010-06-02 06:17:19 +0000401 switch (type) {
Nate Begemana8979a02010-06-04 00:21:41 +0000402 case 'c':
403 switch (ck) {
404 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
405 case ClassI: s += "_i8"; break;
406 case ClassW: s += "_8"; break;
407 default: break;
408 }
409 break;
410 case 's':
411 switch (ck) {
412 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
413 case ClassI: s += "_i16"; break;
414 case ClassW: s += "_16"; break;
415 default: break;
416 }
417 break;
418 case 'i':
419 switch (ck) {
420 case ClassS: s += usgn ? "_u32" : "_s32"; break;
421 case ClassI: s += "_i32"; break;
422 case ClassW: s += "_32"; break;
423 default: break;
424 }
425 break;
426 case 'l':
427 switch (ck) {
428 case ClassS: s += usgn ? "_u64" : "_s64"; break;
429 case ClassI: s += "_i64"; break;
430 case ClassW: s += "_64"; break;
431 default: break;
432 }
433 break;
434 case 'h':
435 switch (ck) {
436 case ClassS:
437 case ClassI: s += "_f16"; break;
438 case ClassW: s += "_16"; break;
439 default: break;
440 }
441 break;
442 case 'f':
443 switch (ck) {
444 case ClassS:
445 case ClassI: s += "_f32"; break;
446 case ClassW: s += "_32"; break;
447 default: break;
448 }
449 break;
450 default:
451 throw "unhandled type!";
452 break;
Nate Begemanaf905ef2010-06-02 06:17:19 +0000453 }
Nate Begemana8979a02010-06-04 00:21:41 +0000454 if (ck == ClassB)
Nate Begeman92f98af2010-06-04 07:11:25 +0000455 s += "_v";
Bob Wilson7f762182010-12-04 04:40:15 +0000456
457 // Insert a 'q' before the first '_' character so that it ends up before
Nate Begemanaf905ef2010-06-02 06:17:19 +0000458 // _lane or _n on vector-scalar operations.
459 if (quad) {
460 size_t pos = s.find('_');
461 s = s.insert(pos, "q");
462 }
463 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000464}
465
Bob Wilsonc1fe1002011-04-22 00:37:01 +0000466/// UseMacro - Examine the prototype string to determine if the intrinsic
467/// should be defined as a preprocessor macro instead of an inline function.
468static bool UseMacro(const std::string &proto) {
469 // If this builtin takes an immediate argument, we need to #define it rather
470 // than use a standard declaration, so that SemaChecking can range check
471 // the immediate passed by the user.
472 if (proto.find('i') != std::string::npos)
473 return true;
474
475 // Pointer arguments need to use macros to avoid hiding aligned attributes
476 // from the pointer type.
477 if (proto.find('p') != std::string::npos ||
478 proto.find('c') != std::string::npos)
479 return true;
480
481 return false;
482}
483
484/// MacroArgUsedDirectly - Return true if argument i for an intrinsic that is
485/// defined as a macro should be accessed directly instead of being first
486/// assigned to a local temporary.
487static bool MacroArgUsedDirectly(const std::string &proto, unsigned i) {
488 return (proto[i] == 'i' || proto[i] == 'p' || proto[i] == 'c');
489}
490
Nate Begemanaf905ef2010-06-02 06:17:19 +0000491// Generate the string "(argtype a, argtype b, ...)"
Nate Begeman22237772010-06-02 00:34:55 +0000492static std::string GenArgs(const std::string &proto, StringRef typestr) {
Bob Wilsonc1fe1002011-04-22 00:37:01 +0000493 bool define = UseMacro(proto);
Nate Begemanaf905ef2010-06-02 06:17:19 +0000494 char arg = 'a';
Bob Wilson7f762182010-12-04 04:40:15 +0000495
Nate Begemanaf905ef2010-06-02 06:17:19 +0000496 std::string s;
497 s += "(";
Bob Wilson7f762182010-12-04 04:40:15 +0000498
Nate Begemanaf905ef2010-06-02 06:17:19 +0000499 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
Bob Wilson194aa582010-12-03 00:34:09 +0000500 if (define) {
Bob Wilsonc1fe1002011-04-22 00:37:01 +0000501 // Some macro arguments are used directly instead of being assigned
Bob Wilson194aa582010-12-03 00:34:09 +0000502 // to local temporaries; prepend an underscore prefix to make their
503 // names consistent with the local temporaries.
Bob Wilsonc1fe1002011-04-22 00:37:01 +0000504 if (MacroArgUsedDirectly(proto, i))
Bob Wilson194aa582010-12-03 00:34:09 +0000505 s += "__";
506 } else {
507 s += TypeString(proto[i], typestr) + " __";
Nate Begeman6c060db2010-06-09 01:09:00 +0000508 }
Nate Begemanaf905ef2010-06-02 06:17:19 +0000509 s.push_back(arg);
510 if ((i + 1) < e)
511 s += ", ";
512 }
Bob Wilson7f762182010-12-04 04:40:15 +0000513
Nate Begemanaf905ef2010-06-02 06:17:19 +0000514 s += ")";
515 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000516}
517
Bob Wilson194aa582010-12-03 00:34:09 +0000518// Macro arguments are not type-checked like inline function arguments, so
519// assign them to local temporaries to get the right type checking.
Bob Wilson377296e2010-12-02 07:10:39 +0000520static std::string GenMacroLocals(const std::string &proto, StringRef typestr) {
521 char arg = 'a';
522 std::string s;
Bob Wilsonc1fe1002011-04-22 00:37:01 +0000523 bool generatedLocal = false;
Bob Wilson7f762182010-12-04 04:40:15 +0000524
Bob Wilson377296e2010-12-02 07:10:39 +0000525 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
526 // Do not create a temporary for an immediate argument.
527 // That would defeat the whole point of using a macro!
Bob Wilsonc1fe1002011-04-22 00:37:01 +0000528 if (proto[i] == 'i')
529 continue;
530 generatedLocal = true;
531
532 // For other (non-immediate) arguments that are used directly, a local
533 // temporary is still needed to get the correct type checking, even though
534 // that temporary is not used for anything.
535 if (MacroArgUsedDirectly(proto, i)) {
536 s += TypeString(proto[i], typestr) + " __";
537 s.push_back(arg);
538 s += "_ = (__";
539 s.push_back(arg);
540 s += "); (void)__";
541 s.push_back(arg);
542 s += "_; ";
543 continue;
544 }
Bob Wilson377296e2010-12-02 07:10:39 +0000545
546 s += TypeString(proto[i], typestr) + " __";
547 s.push_back(arg);
548 s += " = (";
549 s.push_back(arg);
550 s += "); ";
551 }
Bob Wilson7f762182010-12-04 04:40:15 +0000552
Bob Wilsonc1fe1002011-04-22 00:37:01 +0000553 if (generatedLocal)
554 s += "\\\n ";
Bob Wilson377296e2010-12-02 07:10:39 +0000555 return s;
556}
557
Bob Wilsonc4ba09d2010-12-07 20:02:45 +0000558// Use the vmovl builtin to sign-extend or zero-extend a vector.
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000559static std::string Extend(StringRef typestr, const std::string &a) {
Bob Wilsonc4ba09d2010-12-07 20:02:45 +0000560 std::string s;
561 s = MangleName("vmovl", typestr, ClassS);
562 s += "(" + a + ")";
563 return s;
564}
565
Bob Wilson7f762182010-12-04 04:40:15 +0000566static std::string Duplicate(unsigned nElts, StringRef typestr,
Nate Begemancc3c41a2010-06-12 03:09:49 +0000567 const std::string &a) {
Nate Begeman4b425a82010-06-10 00:16:56 +0000568 std::string s;
Bob Wilson7f762182010-12-04 04:40:15 +0000569
Bob Wilson6904d7a2010-11-16 19:39:14 +0000570 s = "(" + TypeString('d', typestr) + "){ ";
Nate Begeman4b425a82010-06-10 00:16:56 +0000571 for (unsigned i = 0; i != nElts; ++i) {
572 s += a;
573 if ((i + 1) < nElts)
574 s += ", ";
575 }
576 s += " }";
Bob Wilson7f762182010-12-04 04:40:15 +0000577
Nate Begeman4b425a82010-06-10 00:16:56 +0000578 return s;
579}
580
Bob Wilsonb0d98692010-12-03 00:34:12 +0000581static std::string SplatLane(unsigned nElts, const std::string &vec,
582 const std::string &lane) {
583 std::string s = "__builtin_shufflevector(" + vec + ", " + vec;
584 for (unsigned i = 0; i < nElts; ++i)
585 s += ", " + lane;
586 s += ")";
587 return s;
588}
589
Bob Wilsonb308b622010-11-16 23:57:01 +0000590static unsigned GetNumElements(StringRef typestr, bool &quad) {
591 quad = false;
592 bool dummy = false;
Nate Begemancc3c41a2010-06-12 03:09:49 +0000593 char type = ClassifyType(typestr, quad, dummy, dummy);
594 unsigned nElts = 0;
595 switch (type) {
Bob Wilsonb308b622010-11-16 23:57:01 +0000596 case 'c': nElts = 8; break;
597 case 's': nElts = 4; break;
598 case 'i': nElts = 2; break;
599 case 'l': nElts = 1; break;
600 case 'h': nElts = 4; break;
601 case 'f': nElts = 2; break;
602 default:
603 throw "unhandled type!";
604 break;
Nate Begemancc3c41a2010-06-12 03:09:49 +0000605 }
Bob Wilsonb308b622010-11-16 23:57:01 +0000606 if (quad) nElts <<= 1;
607 return nElts;
608}
609
610// Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
611static std::string GenOpString(OpKind op, const std::string &proto,
612 StringRef typestr) {
613 bool quad;
614 unsigned nElts = GetNumElements(typestr, quad);
Bob Wilsonc1fe1002011-04-22 00:37:01 +0000615 bool define = UseMacro(proto);
Bob Wilson194aa582010-12-03 00:34:09 +0000616
Nate Begeman4b425a82010-06-10 00:16:56 +0000617 std::string ts = TypeString(proto[0], typestr);
Bob Wilsonee9ca072010-09-15 01:52:33 +0000618 std::string s;
Bob Wilson067a16c2011-01-07 23:40:49 +0000619 if (!define) {
Bob Wilson37a0b542010-12-02 07:44:23 +0000620 s = "return ";
Bob Wilsonee9ca072010-09-15 01:52:33 +0000621 }
Bob Wilson7f762182010-12-04 04:40:15 +0000622
Nate Begeman162d3ba2010-06-03 04:04:09 +0000623 switch(op) {
624 case OpAdd:
Bob Wilson194aa582010-12-03 00:34:09 +0000625 s += "__a + __b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000626 break;
Bob Wilsone113ae52010-12-08 00:14:04 +0000627 case OpAddl:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000628 s += Extend(typestr, "__a") + " + " + Extend(typestr, "__b") + ";";
Bob Wilsone113ae52010-12-08 00:14:04 +0000629 break;
630 case OpAddw:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000631 s += "__a + " + Extend(typestr, "__b") + ";";
Bob Wilsone113ae52010-12-08 00:14:04 +0000632 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000633 case OpSub:
Bob Wilson194aa582010-12-03 00:34:09 +0000634 s += "__a - __b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000635 break;
Bob Wilsone113ae52010-12-08 00:14:04 +0000636 case OpSubl:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000637 s += Extend(typestr, "__a") + " - " + Extend(typestr, "__b") + ";";
Bob Wilsone113ae52010-12-08 00:14:04 +0000638 break;
639 case OpSubw:
Bob Wilsonb1e9df32010-12-08 21:39:00 +0000640 s += "__a - " + Extend(typestr, "__b") + ";";
Bob Wilsone113ae52010-12-08 00:14:04 +0000641 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000642 case OpMulN:
Bob Wilson194aa582010-12-03 00:34:09 +0000643 s += "__a * " + Duplicate(nElts, typestr, "__b") + ";";
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000644 break;
Bob Wilsonb0d98692010-12-03 00:34:12 +0000645 case OpMulLane:
646 s += "__a * " + SplatLane(nElts, "__b", "__c") + ";";
647 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000648 case OpMul:
Bob Wilson194aa582010-12-03 00:34:09 +0000649 s += "__a * __b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000650 break;
Bob Wilson3467cd02010-12-07 22:02:48 +0000651 case OpMullLane:
Bob Wilsonbbe7c652011-03-31 00:09:35 +0000652 s += MangleName("vmull", typestr, ClassS) + "(__a, " +
653 SplatLane(nElts, "__b", "__c") + ");";
Bob Wilsonc4ba09d2010-12-07 20:02:45 +0000654 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000655 case OpMlaN:
Bob Wilson194aa582010-12-03 00:34:09 +0000656 s += "__a + (__b * " + Duplicate(nElts, typestr, "__c") + ");";
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000657 break;
Bob Wilsonb0d98692010-12-03 00:34:12 +0000658 case OpMlaLane:
659 s += "__a + (__b * " + SplatLane(nElts, "__c", "__d") + ");";
660 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000661 case OpMla:
Bob Wilson194aa582010-12-03 00:34:09 +0000662 s += "__a + (__b * __c);";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000663 break;
Bob Wilson05843162010-12-07 23:53:37 +0000664 case OpMlalN:
Bob Wilsonbbe7c652011-03-31 00:09:35 +0000665 s += "__a + " + MangleName("vmull", typestr, ClassS) + "(__b, " +
666 Duplicate(nElts, typestr, "__c") + ");";
Bob Wilson05843162010-12-07 23:53:37 +0000667 break;
668 case OpMlalLane:
Bob Wilsonbbe7c652011-03-31 00:09:35 +0000669 s += "__a + " + MangleName("vmull", typestr, ClassS) + "(__b, " +
670 SplatLane(nElts, "__c", "__d") + ");";
Bob Wilson05843162010-12-07 23:53:37 +0000671 break;
672 case OpMlal:
Bob Wilsonbbe7c652011-03-31 00:09:35 +0000673 s += "__a + " + MangleName("vmull", typestr, ClassS) + "(__b, __c);";
Bob Wilson05843162010-12-07 23:53:37 +0000674 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000675 case OpMlsN:
Bob Wilson194aa582010-12-03 00:34:09 +0000676 s += "__a - (__b * " + Duplicate(nElts, typestr, "__c") + ");";
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000677 break;
Bob Wilsonb0d98692010-12-03 00:34:12 +0000678 case OpMlsLane:
679 s += "__a - (__b * " + SplatLane(nElts, "__c", "__d") + ");";
680 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000681 case OpMls:
Bob Wilson194aa582010-12-03 00:34:09 +0000682 s += "__a - (__b * __c);";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000683 break;
Bob Wilson05843162010-12-07 23:53:37 +0000684 case OpMlslN:
Bob Wilsonbbe7c652011-03-31 00:09:35 +0000685 s += "__a - " + MangleName("vmull", typestr, ClassS) + "(__b, " +
686 Duplicate(nElts, typestr, "__c") + ");";
Bob Wilson05843162010-12-07 23:53:37 +0000687 break;
688 case OpMlslLane:
Bob Wilsonbbe7c652011-03-31 00:09:35 +0000689 s += "__a - " + MangleName("vmull", typestr, ClassS) + "(__b, " +
690 SplatLane(nElts, "__c", "__d") + ");";
Bob Wilson05843162010-12-07 23:53:37 +0000691 break;
692 case OpMlsl:
Bob Wilsonbbe7c652011-03-31 00:09:35 +0000693 s += "__a - " + MangleName("vmull", typestr, ClassS) + "(__b, __c);";
Bob Wilson05843162010-12-07 23:53:37 +0000694 break;
Bob Wilson74410892010-12-08 22:36:08 +0000695 case OpQDMullLane:
696 s += MangleName("vqdmull", typestr, ClassS) + "(__a, " +
697 SplatLane(nElts, "__b", "__c") + ");";
698 break;
699 case OpQDMlalLane:
Bob Wilsonc2ef8282010-12-10 06:37:53 +0000700 s += MangleName("vqdmlal", typestr, ClassS) + "(__a, __b, " +
701 SplatLane(nElts, "__c", "__d") + ");";
Bob Wilson74410892010-12-08 22:36:08 +0000702 break;
703 case OpQDMlslLane:
Bob Wilsonc2ef8282010-12-10 06:37:53 +0000704 s += MangleName("vqdmlsl", typestr, ClassS) + "(__a, __b, " +
705 SplatLane(nElts, "__c", "__d") + ");";
Bob Wilson74410892010-12-08 22:36:08 +0000706 break;
707 case OpQDMulhLane:
708 s += MangleName("vqdmulh", typestr, ClassS) + "(__a, " +
709 SplatLane(nElts, "__b", "__c") + ");";
710 break;
711 case OpQRDMulhLane:
712 s += MangleName("vqrdmulh", typestr, ClassS) + "(__a, " +
713 SplatLane(nElts, "__b", "__c") + ");";
714 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000715 case OpEq:
Bob Wilson194aa582010-12-03 00:34:09 +0000716 s += "(" + ts + ")(__a == __b);";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000717 break;
718 case OpGe:
Bob Wilson194aa582010-12-03 00:34:09 +0000719 s += "(" + ts + ")(__a >= __b);";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000720 break;
721 case OpLe:
Bob Wilson194aa582010-12-03 00:34:09 +0000722 s += "(" + ts + ")(__a <= __b);";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000723 break;
724 case OpGt:
Bob Wilson194aa582010-12-03 00:34:09 +0000725 s += "(" + ts + ")(__a > __b);";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000726 break;
727 case OpLt:
Bob Wilson194aa582010-12-03 00:34:09 +0000728 s += "(" + ts + ")(__a < __b);";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000729 break;
730 case OpNeg:
Bob Wilson194aa582010-12-03 00:34:09 +0000731 s += " -__a;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000732 break;
733 case OpNot:
Bob Wilson194aa582010-12-03 00:34:09 +0000734 s += " ~__a;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000735 break;
736 case OpAnd:
Bob Wilson194aa582010-12-03 00:34:09 +0000737 s += "__a & __b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000738 break;
739 case OpOr:
Bob Wilson194aa582010-12-03 00:34:09 +0000740 s += "__a | __b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000741 break;
742 case OpXor:
Bob Wilson194aa582010-12-03 00:34:09 +0000743 s += "__a ^ __b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000744 break;
745 case OpAndNot:
Bob Wilson194aa582010-12-03 00:34:09 +0000746 s += "__a & ~__b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000747 break;
748 case OpOrNot:
Bob Wilson194aa582010-12-03 00:34:09 +0000749 s += "__a | ~__b;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000750 break;
Nate Begeman3861e742010-06-03 21:35:22 +0000751 case OpCast:
Bob Wilson194aa582010-12-03 00:34:09 +0000752 s += "(" + ts + ")__a;";
Nate Begeman3861e742010-06-03 21:35:22 +0000753 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000754 case OpConcat:
Bob Wilson194aa582010-12-03 00:34:09 +0000755 s += "(" + ts + ")__builtin_shufflevector((int64x1_t)__a";
756 s += ", (int64x1_t)__b, 0, 1);";
Nate Begeman900f4672010-06-08 00:14:42 +0000757 break;
Nate Begeman6c060db2010-06-09 01:09:00 +0000758 case OpHi:
Bob Wilson067a16c2011-01-07 23:40:49 +0000759 s += "(" + ts +
760 ")__builtin_shufflevector((int64x2_t)__a, (int64x2_t)__a, 1);";
Nate Begeman6c060db2010-06-09 01:09:00 +0000761 break;
762 case OpLo:
Bob Wilson067a16c2011-01-07 23:40:49 +0000763 s += "(" + ts +
764 ")__builtin_shufflevector((int64x2_t)__a, (int64x2_t)__a, 0);";
Nate Begeman6c060db2010-06-09 01:09:00 +0000765 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000766 case OpDup:
Bob Wilson194aa582010-12-03 00:34:09 +0000767 s += Duplicate(nElts, typestr, "__a") + ";";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000768 break;
Bob Wilson2196caa2010-12-07 22:39:24 +0000769 case OpDupLane:
770 s += SplatLane(nElts, "__a", "__b") + ";";
771 break;
Nate Begemancc3c41a2010-06-12 03:09:49 +0000772 case OpSelect:
773 // ((0 & 1) | (~0 & 2))
Bob Wilson1dbfa912010-12-02 01:18:20 +0000774 s += "(" + ts + ")";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000775 ts = TypeString(proto[1], typestr);
Bob Wilson194aa582010-12-03 00:34:09 +0000776 s += "((__a & (" + ts + ")__b) | ";
777 s += "(~__a & (" + ts + ")__c));";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000778 break;
779 case OpRev16:
Bob Wilson194aa582010-12-03 00:34:09 +0000780 s += "__builtin_shufflevector(__a, __a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000781 for (unsigned i = 2; i <= nElts; i += 2)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000782 for (unsigned j = 0; j != 2; ++j)
783 s += ", " + utostr(i - j - 1);
Bob Wilson37a0b542010-12-02 07:44:23 +0000784 s += ");";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000785 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000786 case OpRev32: {
787 unsigned WordElts = nElts >> (1 + (int)quad);
Bob Wilson194aa582010-12-03 00:34:09 +0000788 s += "__builtin_shufflevector(__a, __a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000789 for (unsigned i = WordElts; i <= nElts; i += WordElts)
790 for (unsigned j = 0; j != WordElts; ++j)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000791 s += ", " + utostr(i - j - 1);
Bob Wilson37a0b542010-12-02 07:44:23 +0000792 s += ");";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000793 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000794 }
795 case OpRev64: {
796 unsigned DblWordElts = nElts >> (int)quad;
Bob Wilson194aa582010-12-03 00:34:09 +0000797 s += "__builtin_shufflevector(__a, __a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000798 for (unsigned i = DblWordElts; i <= nElts; i += DblWordElts)
799 for (unsigned j = 0; j != DblWordElts; ++j)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000800 s += ", " + utostr(i - j - 1);
Bob Wilson37a0b542010-12-02 07:44:23 +0000801 s += ");";
Nate Begeman900f4672010-06-08 00:14:42 +0000802 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000803 }
Bob Wilsonb4504302010-12-08 21:39:04 +0000804 case OpAbdl: {
805 std::string abd = MangleName("vabd", typestr, ClassS) + "(__a, __b)";
806 if (typestr[0] != 'U') {
807 // vabd results are always unsigned and must be zero-extended.
808 std::string utype = "U" + typestr.str();
809 s += "(" + TypeString(proto[0], typestr) + ")";
810 abd = "(" + TypeString('d', utype) + ")" + abd;
811 s += Extend(utype, abd) + ";";
812 } else {
813 s += Extend(typestr, abd) + ";";
814 }
815 break;
816 }
Bob Wilsonf4f39d32010-12-08 20:09:10 +0000817 case OpAba:
818 s += "__a + " + MangleName("vabd", typestr, ClassS) + "(__b, __c);";
819 break;
Bob Wilsonb4504302010-12-08 21:39:04 +0000820 case OpAbal: {
821 s += "__a + ";
822 std::string abd = MangleName("vabd", typestr, ClassS) + "(__b, __c)";
823 if (typestr[0] != 'U') {
824 // vabd results are always unsigned and must be zero-extended.
825 std::string utype = "U" + typestr.str();
826 s += "(" + TypeString(proto[0], typestr) + ")";
827 abd = "(" + TypeString('d', utype) + ")" + abd;
828 s += Extend(utype, abd) + ";";
829 } else {
830 s += Extend(typestr, abd) + ";";
831 }
832 break;
833 }
Nate Begeman162d3ba2010-06-03 04:04:09 +0000834 default:
835 throw "unknown OpKind!";
836 break;
837 }
Nate Begeman162d3ba2010-06-03 04:04:09 +0000838 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000839}
840
Nate Begemanb0a4e452010-06-07 16:00:37 +0000841static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
842 unsigned mod = proto[0];
843 unsigned ret = 0;
844
Nate Begeman900f4672010-06-08 00:14:42 +0000845 if (mod == 'v' || mod == 'f')
Nate Begemanb0a4e452010-06-07 16:00:37 +0000846 mod = proto[1];
847
848 bool quad = false;
849 bool poly = false;
850 bool usgn = false;
851 bool scal = false;
852 bool cnst = false;
853 bool pntr = false;
Bob Wilson7f762182010-12-04 04:40:15 +0000854
Nate Begeman59d70cb2010-08-06 01:24:11 +0000855 // Base type to get the type string for.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000856 char type = ClassifyType(typestr, quad, poly, usgn);
Bob Wilson7f762182010-12-04 04:40:15 +0000857
Nate Begemanb0a4e452010-06-07 16:00:37 +0000858 // Based on the modifying character, change the type and width if necessary.
859 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
Nate Begeman59d70cb2010-08-06 01:24:11 +0000860
Nate Begemanb0a4e452010-06-07 16:00:37 +0000861 if (usgn)
862 ret |= 0x08;
Nate Begeman59d70cb2010-08-06 01:24:11 +0000863 if (quad && proto[1] != 'g')
Nate Begemanb0a4e452010-06-07 16:00:37 +0000864 ret |= 0x10;
Bob Wilson7f762182010-12-04 04:40:15 +0000865
Nate Begemanb0a4e452010-06-07 16:00:37 +0000866 switch (type) {
Bob Wilson7f762182010-12-04 04:40:15 +0000867 case 'c':
Nate Begemanb0a4e452010-06-07 16:00:37 +0000868 ret |= poly ? 5 : 0;
869 break;
870 case 's':
871 ret |= poly ? 6 : 1;
872 break;
873 case 'i':
874 ret |= 2;
875 break;
876 case 'l':
877 ret |= 3;
878 break;
879 case 'h':
880 ret |= 7;
881 break;
882 case 'f':
883 ret |= 4;
884 break;
885 default:
886 throw "unhandled type!";
887 break;
888 }
889 return ret;
890}
891
Bob Wilsone9e0e3a2011-06-24 21:32:40 +0000892// Generate the definition for this intrinsic.
Nate Begeman7c8c8832010-06-02 21:53:00 +0000893static std::string GenBuiltin(const std::string &name, const std::string &proto,
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000894 StringRef typestr, ClassKind ck) {
Nate Begeman162d3ba2010-06-03 04:04:09 +0000895 std::string s;
Nate Begeman9e584b32010-06-04 22:53:30 +0000896
Nate Begemanc4a1b652010-06-20 21:09:52 +0000897 // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
898 // sret-like argument.
Bob Wilson5b7fe592010-12-01 19:49:51 +0000899 bool sret = (proto[0] >= '2' && proto[0] <= '4');
Nate Begemanc4a1b652010-06-20 21:09:52 +0000900
Bob Wilsonc1fe1002011-04-22 00:37:01 +0000901 bool define = UseMacro(proto);
Nate Begeman9e584b32010-06-04 22:53:30 +0000902
Bob Wilson95148ad2010-12-01 19:49:56 +0000903 // Check if the prototype has a scalar operand with the type of the vector
904 // elements. If not, bitcasting the args will take care of arg checking.
905 // The actual signedness etc. will be taken care of with special enums.
Nate Begeman9e584b32010-06-04 22:53:30 +0000906 if (proto.find('s') == std::string::npos)
907 ck = ClassB;
Nate Begeman7c21f742010-06-04 21:36:00 +0000908
Nate Begeman162d3ba2010-06-03 04:04:09 +0000909 if (proto[0] != 'v') {
Nate Begeman6c060db2010-06-09 01:09:00 +0000910 std::string ts = TypeString(proto[0], typestr);
Bob Wilson7f762182010-12-04 04:40:15 +0000911
Nate Begeman6c060db2010-06-09 01:09:00 +0000912 if (define) {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000913 if (sret)
Bob Wilson052008b2010-12-02 02:42:51 +0000914 s += ts + " r; ";
Bob Wilsone106bc12010-12-02 00:24:56 +0000915 else
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000916 s += "(" + ts + ")";
Nate Begemanc4a1b652010-06-20 21:09:52 +0000917 } else if (sret) {
918 s += ts + " r; ";
Nate Begeman9e584b32010-06-04 22:53:30 +0000919 } else {
Bob Wilson37a0b542010-12-02 07:44:23 +0000920 s += "return (" + ts + ")";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000921 }
Nate Begeman4b425a82010-06-10 00:16:56 +0000922 }
Bob Wilson7f762182010-12-04 04:40:15 +0000923
Nate Begeman4b425a82010-06-10 00:16:56 +0000924 bool splat = proto.find('a') != std::string::npos;
Bob Wilson7f762182010-12-04 04:40:15 +0000925
Bob Wilsone9e0e3a2011-06-24 21:32:40 +0000926 s += "__builtin_";
Nate Begeman4b425a82010-06-10 00:16:56 +0000927 if (splat) {
Bob Wilson95148ad2010-12-01 19:49:56 +0000928 // Call the non-splat builtin: chop off the "_n" suffix from the name.
Nate Begeman4b425a82010-06-10 00:16:56 +0000929 std::string vname(name, 0, name.size()-2);
930 s += MangleName(vname, typestr, ck);
931 } else {
932 s += MangleName(name, typestr, ck);
933 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000934 s += "(";
Nate Begemanc4a1b652010-06-20 21:09:52 +0000935
936 // Pass the address of the return variable as the first argument to sret-like
937 // builtins.
938 if (sret)
939 s += "&r, ";
Bob Wilson7f762182010-12-04 04:40:15 +0000940
Bob Wilson377296e2010-12-02 07:10:39 +0000941 char arg = 'a';
Nate Begeman7c8c8832010-06-02 21:53:00 +0000942 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
Nate Begemancc3c41a2010-06-12 03:09:49 +0000943 std::string args = std::string(&arg, 1);
Bob Wilson95148ad2010-12-01 19:49:56 +0000944
Bob Wilson194aa582010-12-03 00:34:09 +0000945 // Use the local temporaries instead of the macro arguments.
946 args = "__" + args;
Bob Wilsonf00140c2010-12-01 19:49:58 +0000947
948 bool argQuad = false;
949 bool argPoly = false;
950 bool argUsgn = false;
951 bool argScalar = false;
952 bool dummy = false;
953 char argType = ClassifyType(typestr, argQuad, argPoly, argUsgn);
954 argType = ModType(proto[i], argType, argQuad, argPoly, argUsgn, argScalar,
955 dummy, dummy);
956
Nate Begeman9e584b32010-06-04 22:53:30 +0000957 // Handle multiple-vector values specially, emitting each subvector as an
958 // argument to the __builtin.
Bob Wilson5b7fe592010-12-01 19:49:51 +0000959 if (proto[i] >= '2' && proto[i] <= '4') {
Bob Wilsonf00140c2010-12-01 19:49:58 +0000960 // Check if an explicit cast is needed.
961 if (argType != 'c' || argPoly || argUsgn)
962 args = (argQuad ? "(int8x16_t)" : "(int8x8_t)") + args;
963
Nate Begeman9e584b32010-06-04 22:53:30 +0000964 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000965 s += args + ".val[" + utostr(vi) + "]";
Nate Begeman9e584b32010-06-04 22:53:30 +0000966 if ((vi + 1) < ve)
967 s += ", ";
968 }
969 if ((i + 1) < e)
970 s += ", ";
971
972 continue;
973 }
Bob Wilson7f762182010-12-04 04:40:15 +0000974
Bob Wilson4a6c7fc2010-12-02 01:18:23 +0000975 if (splat && (i + 1) == e)
976 args = Duplicate(GetNumElements(typestr, argQuad), typestr, args);
977
Bob Wilsonf00140c2010-12-01 19:49:58 +0000978 // Check if an explicit cast is needed.
Bob Wilson4a6c7fc2010-12-02 01:18:23 +0000979 if ((splat || !argScalar) &&
Bob Wilsonf00140c2010-12-01 19:49:58 +0000980 ((ck == ClassB && argType != 'c') || argPoly || argUsgn)) {
981 std::string argTypeStr = "c";
982 if (ck != ClassB)
983 argTypeStr = argType;
984 if (argQuad)
985 argTypeStr = "Q" + argTypeStr;
986 args = "(" + TypeString('d', argTypeStr) + ")" + args;
987 }
Bob Wilson7f762182010-12-04 04:40:15 +0000988
Bob Wilson4a6c7fc2010-12-02 01:18:23 +0000989 s += args;
Nate Begeman7c8c8832010-06-02 21:53:00 +0000990 if ((i + 1) < e)
991 s += ", ";
992 }
Bob Wilson7f762182010-12-04 04:40:15 +0000993
Nate Begeman9e584b32010-06-04 22:53:30 +0000994 // Extra constant integer to hold type class enum for this function, e.g. s8
Nate Begeman9e584b32010-06-04 22:53:30 +0000995 if (ck == ClassB)
Nate Begemanb0a4e452010-06-07 16:00:37 +0000996 s += ", " + utostr(GetNeonEnum(proto, typestr));
Bob Wilson7f762182010-12-04 04:40:15 +0000997
Bob Wilson052008b2010-12-02 02:42:51 +0000998 s += ");";
Nate Begeman9e584b32010-06-04 22:53:30 +0000999
Bob Wilson37a0b542010-12-02 07:44:23 +00001000 if (proto[0] != 'v' && sret) {
1001 if (define)
1002 s += " r;";
1003 else
Nate Begemanc4a1b652010-06-20 21:09:52 +00001004 s += " return r;";
Nate Begeman9e584b32010-06-04 22:53:30 +00001005 }
Nate Begeman7c8c8832010-06-02 21:53:00 +00001006 return s;
Nate Begemane66aab52010-06-02 07:14:28 +00001007}
1008
Bob Wilson7f762182010-12-04 04:40:15 +00001009static std::string GenBuiltinDef(const std::string &name,
Nate Begeman73cef3e2010-06-04 01:26:15 +00001010 const std::string &proto,
1011 StringRef typestr, ClassKind ck) {
Bob Wilsone9e0e3a2011-06-24 21:32:40 +00001012 std::string s("BUILTIN(__builtin_");
Nate Begeman92f98af2010-06-04 07:11:25 +00001013
Bob Wilson7f762182010-12-04 04:40:15 +00001014 // If all types are the same size, bitcasting the args will take care
Nate Begeman92f98af2010-06-04 07:11:25 +00001015 // of arg checking. The actual signedness etc. will be taken care of with
1016 // special enums.
1017 if (proto.find('s') == std::string::npos)
1018 ck = ClassB;
Bob Wilson7f762182010-12-04 04:40:15 +00001019
Nate Begeman73cef3e2010-06-04 01:26:15 +00001020 s += MangleName(name, typestr, ck);
1021 s += ", \"";
Bob Wilson7f762182010-12-04 04:40:15 +00001022
Nate Begeman92f98af2010-06-04 07:11:25 +00001023 for (unsigned i = 0, e = proto.size(); i != e; ++i)
Nate Begeman7c21f742010-06-04 21:36:00 +00001024 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
1025
1026 // Extra constant integer to hold type class enum for this function, e.g. s8
1027 if (ck == ClassB)
1028 s += "i";
Bob Wilson7f762182010-12-04 04:40:15 +00001029
Nate Begeman73cef3e2010-06-04 01:26:15 +00001030 s += "\", \"n\")";
1031 return s;
1032}
1033
Bob Wilson3890e392010-12-07 01:12:23 +00001034static std::string GenIntrinsic(const std::string &name,
1035 const std::string &proto,
1036 StringRef outTypeStr, StringRef inTypeStr,
1037 OpKind kind, ClassKind classKind) {
1038 assert(!proto.empty() && "");
Bob Wilsonc1fe1002011-04-22 00:37:01 +00001039 bool define = UseMacro(proto);
Bob Wilson3890e392010-12-07 01:12:23 +00001040 std::string s;
1041
1042 // static always inline + return type
1043 if (define)
1044 s += "#define ";
1045 else
1046 s += "__ai " + TypeString(proto[0], outTypeStr) + " ";
1047
1048 // Function name with type suffix
1049 std::string mangledName = MangleName(name, outTypeStr, ClassS);
1050 if (outTypeStr != inTypeStr) {
1051 // If the input type is different (e.g., for vreinterpret), append a suffix
1052 // for the input type. String off a "Q" (quad) prefix so that MangleName
1053 // does not insert another "q" in the name.
1054 unsigned typeStrOff = (inTypeStr[0] == 'Q' ? 1 : 0);
1055 StringRef inTypeNoQuad = inTypeStr.substr(typeStrOff);
1056 mangledName = MangleName(mangledName, inTypeNoQuad, ClassS);
1057 }
1058 s += mangledName;
1059
1060 // Function arguments
1061 s += GenArgs(proto, inTypeStr);
1062
1063 // Definition.
1064 if (define) {
1065 s += " __extension__ ({ \\\n ";
1066 s += GenMacroLocals(proto, inTypeStr);
1067 } else {
1068 s += " { \\\n ";
1069 }
1070
1071 if (kind != OpNone)
1072 s += GenOpString(kind, proto, outTypeStr);
1073 else
1074 s += GenBuiltin(name, proto, outTypeStr, classKind);
1075 if (define)
1076 s += " })";
1077 else
1078 s += " }";
1079 s += "\n";
1080 return s;
1081}
1082
Nate Begemand72c9002010-06-13 04:47:03 +00001083/// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h
1084/// is comprised of type definitions and function declarations.
Nate Begeman5ddb0872010-05-28 01:08:32 +00001085void NeonEmitter::run(raw_ostream &OS) {
Bob Wilson0e1fb7a2010-12-09 18:31:01 +00001086 OS <<
Bob Wilsonb78558c2010-12-09 18:43:35 +00001087 "/*===---- arm_neon.h - ARM Neon intrinsics ------------------------------"
1088 "---===\n"
Bob Wilson0e1fb7a2010-12-09 18:31:01 +00001089 " *\n"
Bob Wilsonb78558c2010-12-09 18:43:35 +00001090 " * Permission is hereby granted, free of charge, to any person obtaining "
1091 "a copy\n"
1092 " * of this software and associated documentation files (the \"Software\"),"
1093 " to deal\n"
1094 " * in the Software without restriction, including without limitation the "
1095 "rights\n"
1096 " * to use, copy, modify, merge, publish, distribute, sublicense, "
1097 "and/or sell\n"
Bob Wilson0e1fb7a2010-12-09 18:31:01 +00001098 " * copies of the Software, and to permit persons to whom the Software is\n"
1099 " * furnished to do so, subject to the following conditions:\n"
1100 " *\n"
Bob Wilsonb78558c2010-12-09 18:43:35 +00001101 " * The above copyright notice and this permission notice shall be "
1102 "included in\n"
Bob Wilson0e1fb7a2010-12-09 18:31:01 +00001103 " * all copies or substantial portions of the Software.\n"
1104 " *\n"
Bob Wilsonb78558c2010-12-09 18:43:35 +00001105 " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, "
1106 "EXPRESS OR\n"
1107 " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
1108 "MERCHANTABILITY,\n"
1109 " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT "
1110 "SHALL THE\n"
1111 " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR "
1112 "OTHER\n"
1113 " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, "
1114 "ARISING FROM,\n"
1115 " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER "
1116 "DEALINGS IN\n"
Bob Wilson0e1fb7a2010-12-09 18:31:01 +00001117 " * THE SOFTWARE.\n"
1118 " *\n"
Bob Wilsonb78558c2010-12-09 18:43:35 +00001119 " *===--------------------------------------------------------------------"
1120 "---===\n"
Bob Wilson0e1fb7a2010-12-09 18:31:01 +00001121 " */\n\n";
Bob Wilson7f762182010-12-04 04:40:15 +00001122
Nate Begeman5ddb0872010-05-28 01:08:32 +00001123 OS << "#ifndef __ARM_NEON_H\n";
1124 OS << "#define __ARM_NEON_H\n\n";
Bob Wilson7f762182010-12-04 04:40:15 +00001125
Nate Begeman5ddb0872010-05-28 01:08:32 +00001126 OS << "#ifndef __ARM_NEON__\n";
1127 OS << "#error \"NEON support not enabled\"\n";
1128 OS << "#endif\n\n";
1129
1130 OS << "#include <stdint.h>\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +00001131
1132 // Emit NEON-specific scalar typedefs.
Nate Begeman7c8c8832010-06-02 21:53:00 +00001133 OS << "typedef float float32_t;\n";
Bob Wilson4fbf6382010-11-16 23:57:03 +00001134 OS << "typedef int8_t poly8_t;\n";
1135 OS << "typedef int16_t poly16_t;\n";
Nate Begeman162d3ba2010-06-03 04:04:09 +00001136 OS << "typedef uint16_t float16_t;\n";
Nate Begeman9e584b32010-06-04 22:53:30 +00001137
Nate Begeman7c8c8832010-06-02 21:53:00 +00001138 // Emit Neon vector typedefs.
1139 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
1140 SmallVector<StringRef, 24> TDTypeVec;
1141 ParseTypes(0, TypedefTypes, TDTypeVec);
1142
1143 // Emit vector typedefs.
Bob Wilson6904d7a2010-11-16 19:39:14 +00001144 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
Bob Wilson6a8aceb2010-11-16 23:57:06 +00001145 bool dummy, quad = false, poly = false;
1146 (void) ClassifyType(TDTypeVec[i], quad, poly, dummy);
1147 if (poly)
1148 OS << "typedef __attribute__((neon_polyvector_type(";
1149 else
1150 OS << "typedef __attribute__((neon_vector_type(";
Bob Wilson7f762182010-12-04 04:40:15 +00001151
Bob Wilson6a8aceb2010-11-16 23:57:06 +00001152 unsigned nElts = GetNumElements(TDTypeVec[i], quad);
1153 OS << utostr(nElts) << "))) ";
1154 if (nElts < 10)
Bob Wilson6904d7a2010-11-16 19:39:14 +00001155 OS << " ";
Bob Wilson7f762182010-12-04 04:40:15 +00001156
Bob Wilson6904d7a2010-11-16 19:39:14 +00001157 OS << TypeString('s', TDTypeVec[i]);
1158 OS << " " << TypeString('d', TDTypeVec[i]) << ";\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +00001159 }
1160 OS << "\n";
1161
1162 // Emit struct typedefs.
Bob Wilson6904d7a2010-11-16 19:39:14 +00001163 for (unsigned vi = 2; vi != 5; ++vi) {
Nate Begeman7c8c8832010-06-02 21:53:00 +00001164 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
Bob Wilson6904d7a2010-11-16 19:39:14 +00001165 std::string ts = TypeString('d', TDTypeVec[i]);
1166 std::string vs = TypeString('0' + vi, TDTypeVec[i]);
1167 OS << "typedef struct " << vs << " {\n";
1168 OS << " " << ts << " val";
1169 OS << "[" << utostr(vi) << "]";
1170 OS << ";\n} ";
Bob Wilsonf18dfec2010-11-16 19:16:06 +00001171 OS << vs << ";\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +00001172 }
1173 }
Bob Wilson7f762182010-12-04 04:40:15 +00001174
Nate Begeman7c8c8832010-06-02 21:53:00 +00001175 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
1176
Nate Begeman5ddb0872010-05-28 01:08:32 +00001177 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
Bob Wilson7f762182010-12-04 04:40:15 +00001178
Bob Wilsonbbe7c652011-03-31 00:09:35 +00001179 // Emit vmovl, vmull and vabd intrinsics first so they can be used by other
Bob Wilson74410892010-12-08 22:36:08 +00001180 // intrinsics. (Some of the saturating multiply instructions are also
1181 // used to implement the corresponding "_lane" variants, but tablegen
1182 // sorts the records into alphabetical order so that the "_lane" variants
1183 // come after the intrinsics they use.)
Bob Wilsonda1d3dc2010-12-07 23:53:32 +00001184 emitIntrinsic(OS, Records.getDef("VMOVL"));
Bob Wilsonbbe7c652011-03-31 00:09:35 +00001185 emitIntrinsic(OS, Records.getDef("VMULL"));
Bob Wilsonf4f39d32010-12-08 20:09:10 +00001186 emitIntrinsic(OS, Records.getDef("VABD"));
Bob Wilsonda1d3dc2010-12-07 23:53:32 +00001187
Nate Begeman5ddb0872010-05-28 01:08:32 +00001188 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1189 Record *R = RV[i];
Bob Wilsonbbe7c652011-03-31 00:09:35 +00001190 if (R->getName() != "VMOVL" &&
1191 R->getName() != "VMULL" &&
1192 R->getName() != "VABD")
Bob Wilsonda1d3dc2010-12-07 23:53:32 +00001193 emitIntrinsic(OS, R);
Nate Begeman5ddb0872010-05-28 01:08:32 +00001194 }
Bob Wilsonda1d3dc2010-12-07 23:53:32 +00001195
Nate Begeman73cef3e2010-06-04 01:26:15 +00001196 OS << "#undef __ai\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +00001197 OS << "#endif /* __ARM_NEON_H */\n";
Nate Begeman5ddb0872010-05-28 01:08:32 +00001198}
Nate Begemana8979a02010-06-04 00:21:41 +00001199
Bob Wilsonda1d3dc2010-12-07 23:53:32 +00001200/// emitIntrinsic - Write out the arm_neon.h header file definitions for the
1201/// intrinsics specified by record R.
1202void NeonEmitter::emitIntrinsic(raw_ostream &OS, Record *R) {
1203 std::string name = R->getValueAsString("Name");
1204 std::string Proto = R->getValueAsString("Prototype");
1205 std::string Types = R->getValueAsString("Types");
1206
1207 SmallVector<StringRef, 16> TypeVec;
1208 ParseTypes(R, Types, TypeVec);
1209
1210 OpKind kind = OpMap[R->getValueAsDef("Operand")->getName()];
1211
1212 ClassKind classKind = ClassNone;
1213 if (R->getSuperClasses().size() >= 2)
1214 classKind = ClassMap[R->getSuperClasses()[1]];
1215 if (classKind == ClassNone && kind == OpNone)
1216 throw TGError(R->getLoc(), "Builtin has no class kind");
1217
1218 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1219 if (kind == OpReinterpret) {
1220 bool outQuad = false;
1221 bool dummy = false;
1222 (void)ClassifyType(TypeVec[ti], outQuad, dummy, dummy);
1223 for (unsigned srcti = 0, srcte = TypeVec.size();
1224 srcti != srcte; ++srcti) {
1225 bool inQuad = false;
1226 (void)ClassifyType(TypeVec[srcti], inQuad, dummy, dummy);
1227 if (srcti == ti || inQuad != outQuad)
1228 continue;
1229 OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[srcti],
1230 OpCast, ClassS);
1231 }
1232 } else {
1233 OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[ti],
1234 kind, classKind);
1235 }
1236 }
1237 OS << "\n";
1238}
1239
Bob Wilson06b04aa2010-12-15 16:58:42 +00001240static unsigned RangeFromType(const char mod, StringRef typestr) {
Nate Begeman918f8e42010-06-14 05:17:23 +00001241 // base type to get the type string for.
1242 bool quad = false, dummy = false;
1243 char type = ClassifyType(typestr, quad, dummy, dummy);
Bob Wilson06b04aa2010-12-15 16:58:42 +00001244 type = ModType(mod, type, quad, dummy, dummy, dummy, dummy, dummy);
Bob Wilson7f762182010-12-04 04:40:15 +00001245
Nate Begeman918f8e42010-06-14 05:17:23 +00001246 switch (type) {
1247 case 'c':
Nate Begeman4da883a2010-06-15 22:10:31 +00001248 return (8 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001249 case 'h':
1250 case 's':
Nate Begeman4da883a2010-06-15 22:10:31 +00001251 return (4 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001252 case 'f':
1253 case 'i':
Nate Begeman4da883a2010-06-15 22:10:31 +00001254 return (2 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001255 case 'l':
Nate Begeman4da883a2010-06-15 22:10:31 +00001256 return (1 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001257 default:
1258 throw "unhandled type!";
1259 break;
1260 }
Bob Wilsonfdb530d2010-07-28 18:21:10 +00001261 assert(0 && "unreachable");
1262 return 0;
Nate Begeman918f8e42010-06-14 05:17:23 +00001263}
1264
Nate Begemanf8c4c272010-06-17 04:15:13 +00001265/// runHeader - Emit a file with sections defining:
1266/// 1. the NEON section of BuiltinsARM.def.
1267/// 2. the SemaChecking code for the type overload checking.
1268/// 3. the SemaChecking code for validation of intrinsic immedate arguments.
Nate Begemana8979a02010-06-04 00:21:41 +00001269void NeonEmitter::runHeader(raw_ostream &OS) {
Nate Begeman73cef3e2010-06-04 01:26:15 +00001270 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1271
1272 StringMap<OpKind> EmittedMap;
Bob Wilson7f762182010-12-04 04:40:15 +00001273
Nate Begemanf8c4c272010-06-17 04:15:13 +00001274 // Generate BuiltinsARM.def for NEON
1275 OS << "#ifdef GET_NEON_BUILTINS\n";
Nate Begeman73cef3e2010-06-04 01:26:15 +00001276 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1277 Record *R = RV[i];
Nate Begeman73cef3e2010-06-04 01:26:15 +00001278 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1279 if (k != OpNone)
1280 continue;
Nate Begeman73cef3e2010-06-04 01:26:15 +00001281
Nate Begemanf8c4c272010-06-17 04:15:13 +00001282 std::string Proto = R->getValueAsString("Prototype");
Bob Wilson7f762182010-12-04 04:40:15 +00001283
Nate Begemand72c9002010-06-13 04:47:03 +00001284 // Functions with 'a' (the splat code) in the type prototype should not get
1285 // their own builtin as they use the non-splat variant.
Nate Begeman4b425a82010-06-10 00:16:56 +00001286 if (Proto.find('a') != std::string::npos)
1287 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001288
Nate Begemanf8c4c272010-06-17 04:15:13 +00001289 std::string Types = R->getValueAsString("Types");
Nate Begeman73cef3e2010-06-04 01:26:15 +00001290 SmallVector<StringRef, 16> TypeVec;
1291 ParseTypes(R, Types, TypeVec);
Bob Wilson7f762182010-12-04 04:40:15 +00001292
Nate Begeman73cef3e2010-06-04 01:26:15 +00001293 if (R->getSuperClasses().size() < 2)
1294 throw TGError(R->getLoc(), "Builtin has no class kind");
Bob Wilson7f762182010-12-04 04:40:15 +00001295
Bob Wilsonce0bb542010-12-03 17:19:39 +00001296 std::string name = R->getValueAsString("Name");
Nate Begeman73cef3e2010-06-04 01:26:15 +00001297 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
Bob Wilson7f762182010-12-04 04:40:15 +00001298
Nate Begeman73cef3e2010-06-04 01:26:15 +00001299 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
Nate Begemand72c9002010-06-13 04:47:03 +00001300 // Generate the BuiltinsARM.def declaration for this builtin, ensuring
1301 // that each unique BUILTIN() macro appears only once in the output
1302 // stream.
Nate Begeman73cef3e2010-06-04 01:26:15 +00001303 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
1304 if (EmittedMap.count(bd))
1305 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001306
Nate Begeman73cef3e2010-06-04 01:26:15 +00001307 EmittedMap[bd] = OpNone;
1308 OS << bd << "\n";
1309 }
Nate Begemanf8c4c272010-06-17 04:15:13 +00001310 }
1311 OS << "#endif\n\n";
Bob Wilson7f762182010-12-04 04:40:15 +00001312
Nate Begemanf8c4c272010-06-17 04:15:13 +00001313 // Generate the overloaded type checking code for SemaChecking.cpp
1314 OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
1315 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1316 Record *R = RV[i];
1317 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1318 if (k != OpNone)
Nate Begemand72c9002010-06-13 04:47:03 +00001319 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001320
Nate Begemanf8c4c272010-06-17 04:15:13 +00001321 std::string Proto = R->getValueAsString("Prototype");
1322 std::string Types = R->getValueAsString("Types");
Bob Wilsonce0bb542010-12-03 17:19:39 +00001323 std::string name = R->getValueAsString("Name");
Bob Wilson7f762182010-12-04 04:40:15 +00001324
Nate Begemanf8c4c272010-06-17 04:15:13 +00001325 // Functions with 'a' (the splat code) in the type prototype should not get
1326 // their own builtin as they use the non-splat variant.
1327 if (Proto.find('a') != std::string::npos)
1328 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001329
Nate Begemanf8c4c272010-06-17 04:15:13 +00001330 // Functions which have a scalar argument cannot be overloaded, no need to
1331 // check them if we are emitting the type checking code.
1332 if (Proto.find('s') != std::string::npos)
1333 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001334
Nate Begemanf8c4c272010-06-17 04:15:13 +00001335 SmallVector<StringRef, 16> TypeVec;
1336 ParseTypes(R, Types, TypeVec);
Bob Wilson7f762182010-12-04 04:40:15 +00001337
Nate Begemanf8c4c272010-06-17 04:15:13 +00001338 if (R->getSuperClasses().size() < 2)
1339 throw TGError(R->getLoc(), "Builtin has no class kind");
Bob Wilson7f762182010-12-04 04:40:15 +00001340
Nate Begemanf8c4c272010-06-17 04:15:13 +00001341 int si = -1, qi = -1;
1342 unsigned mask = 0, qmask = 0;
1343 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1344 // Generate the switch case(s) for this builtin for the type validation.
1345 bool quad = false, poly = false, usgn = false;
1346 (void) ClassifyType(TypeVec[ti], quad, poly, usgn);
Bob Wilson7f762182010-12-04 04:40:15 +00001347
Nate Begemanf8c4c272010-06-17 04:15:13 +00001348 if (quad) {
1349 qi = ti;
1350 qmask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1351 } else {
1352 si = ti;
1353 mask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1354 }
1355 }
1356 if (mask)
Bob Wilsone9e0e3a2011-06-24 21:32:40 +00001357 OS << "case ARM::BI__builtin_"
Bob Wilsond8b84702010-12-07 01:12:19 +00001358 << MangleName(name, TypeVec[si], ClassB)
1359 << ": mask = " << "0x" << utohexstr(mask) << "; break;\n";
Nate Begemanf8c4c272010-06-17 04:15:13 +00001360 if (qmask)
Bob Wilsone9e0e3a2011-06-24 21:32:40 +00001361 OS << "case ARM::BI__builtin_"
Bob Wilsond8b84702010-12-07 01:12:19 +00001362 << MangleName(name, TypeVec[qi], ClassB)
1363 << ": mask = " << "0x" << utohexstr(qmask) << "; break;\n";
Nate Begemanf8c4c272010-06-17 04:15:13 +00001364 }
1365 OS << "#endif\n\n";
Bob Wilson7f762182010-12-04 04:40:15 +00001366
Nate Begemanf8c4c272010-06-17 04:15:13 +00001367 // Generate the intrinsic range checking code for shift/lane immediates.
1368 OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
1369 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1370 Record *R = RV[i];
Bob Wilson7f762182010-12-04 04:40:15 +00001371
Nate Begemanf8c4c272010-06-17 04:15:13 +00001372 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1373 if (k != OpNone)
1374 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001375
Bob Wilsonce0bb542010-12-03 17:19:39 +00001376 std::string name = R->getValueAsString("Name");
Nate Begemanf8c4c272010-06-17 04:15:13 +00001377 std::string Proto = R->getValueAsString("Prototype");
1378 std::string Types = R->getValueAsString("Types");
Bob Wilson7f762182010-12-04 04:40:15 +00001379
Nate Begemanf8c4c272010-06-17 04:15:13 +00001380 // Functions with 'a' (the splat code) in the type prototype should not get
1381 // their own builtin as they use the non-splat variant.
1382 if (Proto.find('a') != std::string::npos)
1383 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001384
Nate Begemanf8c4c272010-06-17 04:15:13 +00001385 // Functions which do not have an immediate do not need to have range
1386 // checking code emitted.
Bob Wilson06b04aa2010-12-15 16:58:42 +00001387 size_t immPos = Proto.find('i');
1388 if (immPos == std::string::npos)
Nate Begemanf8c4c272010-06-17 04:15:13 +00001389 continue;
Bob Wilson7f762182010-12-04 04:40:15 +00001390
Nate Begemanf8c4c272010-06-17 04:15:13 +00001391 SmallVector<StringRef, 16> TypeVec;
1392 ParseTypes(R, Types, TypeVec);
Bob Wilson7f762182010-12-04 04:40:15 +00001393
Nate Begemanf8c4c272010-06-17 04:15:13 +00001394 if (R->getSuperClasses().size() < 2)
1395 throw TGError(R->getLoc(), "Builtin has no class kind");
Bob Wilson7f762182010-12-04 04:40:15 +00001396
Nate Begemanf8c4c272010-06-17 04:15:13 +00001397 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
Bob Wilson7f762182010-12-04 04:40:15 +00001398
Nate Begemanf8c4c272010-06-17 04:15:13 +00001399 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1400 std::string namestr, shiftstr, rangestr;
Bob Wilson7f762182010-12-04 04:40:15 +00001401
Bob Wilsone450a002011-06-09 16:57:29 +00001402 if (R->getValueAsBit("isVCVT_N")) {
1403 // VCVT between floating- and fixed-point values takes an immediate
1404 // in the range 1 to 32.
1405 ck = ClassB;
1406 rangestr = "l = 1; u = 31"; // upper bound = l + u
1407 } else if (Proto.find('s') == std::string::npos) {
1408 // Builtins which are overloaded by type will need to have their upper
1409 // bound computed at Sema time based on the type constant.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001410 ck = ClassB;
1411 if (R->getValueAsBit("isShift")) {
1412 shiftstr = ", true";
Bob Wilson7f762182010-12-04 04:40:15 +00001413
Nate Begemanf8c4c272010-06-17 04:15:13 +00001414 // Right shifts have an 'r' in the name, left shifts do not.
1415 if (name.find('r') != std::string::npos)
1416 rangestr = "l = 1; ";
1417 }
1418 rangestr += "u = RFT(TV" + shiftstr + ")";
1419 } else {
Bob Wilson06b04aa2010-12-15 16:58:42 +00001420 // The immediate generally refers to a lane in the preceding argument.
1421 assert(immPos > 0 && "unexpected immediate operand");
1422 rangestr = "u = " + utostr(RangeFromType(Proto[immPos-1], TypeVec[ti]));
Nate Begemanf8c4c272010-06-17 04:15:13 +00001423 }
Nate Begemanc4a1b652010-06-20 21:09:52 +00001424 // Make sure cases appear only once by uniquing them in a string map.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001425 namestr = MangleName(name, TypeVec[ti], ck);
1426 if (EmittedMap.count(namestr))
1427 continue;
1428 EmittedMap[namestr] = OpNone;
Nate Begemanc4a1b652010-06-20 21:09:52 +00001429
1430 // Calculate the index of the immediate that should be range checked.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001431 unsigned immidx = 0;
Bob Wilson7f762182010-12-04 04:40:15 +00001432
Nate Begemanc4a1b652010-06-20 21:09:52 +00001433 // Builtins that return a struct of multiple vectors have an extra
1434 // leading arg for the struct return.
Bob Wilson5b7fe592010-12-01 19:49:51 +00001435 if (Proto[0] >= '2' && Proto[0] <= '4')
Nate Begemanc4a1b652010-06-20 21:09:52 +00001436 ++immidx;
Bob Wilson7f762182010-12-04 04:40:15 +00001437
1438 // Add one to the index for each argument until we reach the immediate
Nate Begemanc4a1b652010-06-20 21:09:52 +00001439 // to be checked. Structs of vectors are passed as multiple arguments.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001440 for (unsigned ii = 1, ie = Proto.size(); ii != ie; ++ii) {
1441 switch (Proto[ii]) {
1442 default: immidx += 1; break;
1443 case '2': immidx += 2; break;
1444 case '3': immidx += 3; break;
1445 case '4': immidx += 4; break;
1446 case 'i': ie = ii + 1; break;
1447 }
1448 }
Bob Wilsone9e0e3a2011-06-24 21:32:40 +00001449 OS << "case ARM::BI__builtin_" << MangleName(name, TypeVec[ti], ck)
Nate Begemanf8c4c272010-06-17 04:15:13 +00001450 << ": i = " << immidx << "; " << rangestr << "; break;\n";
Nate Begemand72c9002010-06-13 04:47:03 +00001451 }
Nate Begeman73cef3e2010-06-04 01:26:15 +00001452 }
Nate Begemanf8c4c272010-06-17 04:15:13 +00001453 OS << "#endif\n\n";
Nate Begemana8979a02010-06-04 00:21:41 +00001454}
Bob Wilson333f5192010-12-15 16:58:45 +00001455
1456/// GenTest - Write out a test for the intrinsic specified by the name and
1457/// type strings, including the embedded patterns for FileCheck to match.
1458static std::string GenTest(const std::string &name,
1459 const std::string &proto,
1460 StringRef outTypeStr, StringRef inTypeStr,
1461 bool isShift) {
1462 assert(!proto.empty() && "");
1463 std::string s;
1464
1465 // Function name with type suffix
1466 std::string mangledName = MangleName(name, outTypeStr, ClassS);
1467 if (outTypeStr != inTypeStr) {
1468 // If the input type is different (e.g., for vreinterpret), append a suffix
1469 // for the input type. String off a "Q" (quad) prefix so that MangleName
1470 // does not insert another "q" in the name.
1471 unsigned typeStrOff = (inTypeStr[0] == 'Q' ? 1 : 0);
1472 StringRef inTypeNoQuad = inTypeStr.substr(typeStrOff);
1473 mangledName = MangleName(mangledName, inTypeNoQuad, ClassS);
1474 }
1475
1476 // Emit the FileCheck patterns.
1477 s += "// CHECK: test_" + mangledName + "\n";
1478 // s += "// CHECK: \n"; // FIXME: + expected instruction opcode.
1479
1480 // Emit the start of the test function.
1481 s += TypeString(proto[0], outTypeStr) + " test_" + mangledName + "(";
1482 char arg = 'a';
1483 std::string comma;
1484 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
1485 // Do not create arguments for values that must be immediate constants.
1486 if (proto[i] == 'i')
1487 continue;
1488 s += comma + TypeString(proto[i], inTypeStr) + " ";
1489 s.push_back(arg);
1490 comma = ", ";
1491 }
1492 s += ") { \\\n ";
1493
1494 if (proto[0] != 'v')
1495 s += "return ";
1496 s += mangledName + "(";
1497 arg = 'a';
1498 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
1499 if (proto[i] == 'i') {
1500 // For immediate operands, test the maximum value.
1501 if (isShift)
1502 s += "1"; // FIXME
1503 else
1504 // The immediate generally refers to a lane in the preceding argument.
1505 s += utostr(RangeFromType(proto[i-1], inTypeStr));
1506 } else {
1507 s.push_back(arg);
1508 }
1509 if ((i + 1) < e)
1510 s += ", ";
1511 }
1512 s += ");\n}\n\n";
1513 return s;
1514}
1515
1516/// runTests - Write out a complete set of tests for all of the Neon
1517/// intrinsics.
1518void NeonEmitter::runTests(raw_ostream &OS) {
1519 OS <<
1520 "// RUN: %clang_cc1 -triple thumbv7-apple-darwin \\\n"
Bob Wilson14595d92010-12-17 01:21:03 +00001521 "// RUN: -target-cpu cortex-a9 -ffreestanding -S -o - %s | FileCheck %s\n"
Bob Wilson333f5192010-12-15 16:58:45 +00001522 "\n"
1523 "#include <arm_neon.h>\n"
1524 "\n";
1525
1526 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1527 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1528 Record *R = RV[i];
1529 std::string name = R->getValueAsString("Name");
1530 std::string Proto = R->getValueAsString("Prototype");
1531 std::string Types = R->getValueAsString("Types");
1532 bool isShift = R->getValueAsBit("isShift");
1533
1534 SmallVector<StringRef, 16> TypeVec;
1535 ParseTypes(R, Types, TypeVec);
1536
1537 OpKind kind = OpMap[R->getValueAsDef("Operand")->getName()];
1538 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1539 if (kind == OpReinterpret) {
1540 bool outQuad = false;
1541 bool dummy = false;
1542 (void)ClassifyType(TypeVec[ti], outQuad, dummy, dummy);
1543 for (unsigned srcti = 0, srcte = TypeVec.size();
1544 srcti != srcte; ++srcti) {
1545 bool inQuad = false;
1546 (void)ClassifyType(TypeVec[srcti], inQuad, dummy, dummy);
1547 if (srcti == ti || inQuad != outQuad)
1548 continue;
1549 OS << GenTest(name, Proto, TypeVec[ti], TypeVec[srcti], isShift);
1550 }
1551 } else {
1552 OS << GenTest(name, Proto, TypeVec[ti], TypeVec[ti], isShift);
1553 }
1554 }
1555 OS << "\n";
1556 }
1557}
1558