Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 1 | //===- NeonEmitter.cpp - Generate arm_neon.h for use with clang -*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This tablegen backend is responsible for emitting arm_neon.h, which includes |
| 11 | // a declaration and definition of each function specified by the ARM NEON |
| 12 | // compiler interface. See ARM document DUI0348B. |
| 13 | // |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 14 | // Each NEON instruction is implemented in terms of 1 or more functions which |
| 15 | // are suffixed with the element type of the input vectors. Functions may be |
| 16 | // implemented in terms of generic vector operations such as +, *, -, etc. or |
| 17 | // by calling a __builtin_-prefixed function which will be handled by clang's |
| 18 | // CodeGen library. |
| 19 | // |
| 20 | // Additional validation code can be generated by this file when runHeader() is |
| 21 | // called, rather than the normal run() entry point. |
| 22 | // |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | #include "NeonEmitter.h" |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallString.h" |
| 27 | #include "llvm/ADT/SmallVector.h" |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/StringExtras.h" |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 29 | #include <string> |
| 30 | |
| 31 | using namespace llvm; |
| 32 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 33 | /// ParseTypes - break down a string such as "fQf" into a vector of StringRefs, |
| 34 | /// which each StringRef representing a single type declared in the string. |
| 35 | /// for "fQf" we would end up with 2 StringRefs, "f", and "Qf", representing |
| 36 | /// 2xfloat and 4xfloat respectively. |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 37 | static void ParseTypes(Record *r, std::string &s, |
| 38 | SmallVectorImpl<StringRef> &TV) { |
| 39 | const char *data = s.data(); |
| 40 | int len = 0; |
| 41 | |
| 42 | for (unsigned i = 0, e = s.size(); i != e; ++i, ++len) { |
| 43 | if (data[len] == 'P' || data[len] == 'Q' || data[len] == 'U') |
| 44 | continue; |
| 45 | |
| 46 | switch (data[len]) { |
| 47 | case 'c': |
| 48 | case 's': |
| 49 | case 'i': |
| 50 | case 'l': |
| 51 | case 'h': |
| 52 | case 'f': |
| 53 | break; |
| 54 | default: |
| 55 | throw TGError(r->getLoc(), |
| 56 | "Unexpected letter: " + std::string(data + len, 1)); |
| 57 | break; |
| 58 | } |
| 59 | TV.push_back(StringRef(data, len + 1)); |
| 60 | data += len + 1; |
| 61 | len = -1; |
| 62 | } |
| 63 | } |
| 64 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 65 | /// Widen - Convert a type code into the next wider type. char -> short, |
| 66 | /// short -> int, etc. |
Duncan Sands | 8dbbace | 2010-06-02 08:37:30 +0000 | [diff] [blame] | 67 | static char Widen(const char t) { |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 68 | switch (t) { |
| 69 | case 'c': |
| 70 | return 's'; |
| 71 | case 's': |
| 72 | return 'i'; |
| 73 | case 'i': |
| 74 | return 'l'; |
| 75 | default: throw "unhandled type in widen!"; |
| 76 | } |
| 77 | return '\0'; |
| 78 | } |
| 79 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 80 | /// Narrow - Convert a type code into the next smaller type. short -> char, |
| 81 | /// float -> half float, etc. |
Nate Begeman | 3861e74 | 2010-06-03 21:35:22 +0000 | [diff] [blame] | 82 | static char Narrow(const char t) { |
| 83 | switch (t) { |
| 84 | case 's': |
| 85 | return 'c'; |
| 86 | case 'i': |
| 87 | return 's'; |
| 88 | case 'l': |
| 89 | return 'i'; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 90 | case 'f': |
| 91 | return 'h'; |
Bob Wilson | b055f74 | 2010-11-23 19:38:34 +0000 | [diff] [blame] | 92 | default: throw "unhandled type in narrow!"; |
Nate Begeman | 3861e74 | 2010-06-03 21:35:22 +0000 | [diff] [blame] | 93 | } |
| 94 | return '\0'; |
| 95 | } |
| 96 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 97 | /// For a particular StringRef, return the base type code, and whether it has |
| 98 | /// the quad-vector, polynomial, or unsigned modifiers set. |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 99 | static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) { |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 100 | unsigned off = 0; |
| 101 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 102 | // remember quad. |
| 103 | if (ty[off] == 'Q') { |
| 104 | quad = true; |
| 105 | ++off; |
| 106 | } |
| 107 | |
| 108 | // remember poly. |
| 109 | if (ty[off] == 'P') { |
| 110 | poly = true; |
| 111 | ++off; |
| 112 | } |
| 113 | |
| 114 | // remember unsigned. |
| 115 | if (ty[off] == 'U') { |
| 116 | usgn = true; |
| 117 | ++off; |
| 118 | } |
| 119 | |
| 120 | // base type to get the type string for. |
| 121 | return ty[off]; |
| 122 | } |
| 123 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 124 | /// ModType - Transform a type code and its modifiers based on a mod code. The |
| 125 | /// mod code definitions may be found at the top of arm_neon.td. |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 126 | static char ModType(const char mod, char type, bool &quad, bool &poly, |
| 127 | bool &usgn, bool &scal, bool &cnst, bool &pntr) { |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 128 | switch (mod) { |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 129 | case 't': |
| 130 | if (poly) { |
| 131 | poly = false; |
| 132 | usgn = true; |
| 133 | } |
| 134 | break; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 135 | case 'u': |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 136 | usgn = true; |
Bob Wilson | 181b76d | 2010-11-18 21:43:22 +0000 | [diff] [blame] | 137 | poly = false; |
| 138 | if (type == 'f') |
| 139 | type = 'i'; |
| 140 | break; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 141 | case 'x': |
Bob Wilson | 181b76d | 2010-11-18 21:43:22 +0000 | [diff] [blame] | 142 | usgn = false; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 143 | poly = false; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 144 | if (type == 'f') |
| 145 | type = 'i'; |
| 146 | break; |
| 147 | case 'f': |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 148 | if (type == 'h') |
| 149 | quad = true; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 150 | type = 'f'; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 151 | usgn = false; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 152 | break; |
Nate Begeman | 59d70cb | 2010-08-06 01:24:11 +0000 | [diff] [blame] | 153 | case 'g': |
| 154 | quad = false; |
| 155 | break; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 156 | case 'w': |
| 157 | type = Widen(type); |
| 158 | quad = true; |
| 159 | break; |
| 160 | case 'n': |
| 161 | type = Widen(type); |
| 162 | break; |
Bob Wilson | f00140c | 2010-12-01 19:49:58 +0000 | [diff] [blame] | 163 | case 'i': |
| 164 | type = 'i'; |
| 165 | scal = true; |
| 166 | break; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 167 | case 'l': |
| 168 | type = 'l'; |
| 169 | scal = true; |
| 170 | usgn = true; |
| 171 | break; |
| 172 | case 's': |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 173 | case 'a': |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 174 | scal = true; |
| 175 | break; |
| 176 | case 'k': |
| 177 | quad = true; |
| 178 | break; |
| 179 | case 'c': |
| 180 | cnst = true; |
| 181 | case 'p': |
| 182 | pntr = true; |
| 183 | scal = true; |
| 184 | break; |
Nate Begeman | 3861e74 | 2010-06-03 21:35:22 +0000 | [diff] [blame] | 185 | case 'h': |
| 186 | type = Narrow(type); |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 187 | if (type == 'h') |
| 188 | quad = false; |
Nate Begeman | 3861e74 | 2010-06-03 21:35:22 +0000 | [diff] [blame] | 189 | break; |
| 190 | case 'e': |
| 191 | type = Narrow(type); |
| 192 | usgn = true; |
| 193 | break; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 194 | default: |
| 195 | break; |
| 196 | } |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 197 | return type; |
| 198 | } |
| 199 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 200 | /// TypeString - for a modifier and type, generate the name of the typedef for |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 201 | /// that type. QUc -> uint8x8_t. |
| 202 | static std::string TypeString(const char mod, StringRef typestr) { |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 203 | bool quad = false; |
| 204 | bool poly = false; |
| 205 | bool usgn = false; |
| 206 | bool scal = false; |
| 207 | bool cnst = false; |
| 208 | bool pntr = false; |
| 209 | |
| 210 | if (mod == 'v') |
| 211 | return "void"; |
| 212 | if (mod == 'i') |
| 213 | return "int"; |
| 214 | |
| 215 | // base type to get the type string for. |
| 216 | char type = ClassifyType(typestr, quad, poly, usgn); |
| 217 | |
| 218 | // Based on the modifying character, change the type and width if necessary. |
| 219 | type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr); |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 220 | |
| 221 | SmallString<128> s; |
| 222 | |
| 223 | if (usgn) |
| 224 | s.push_back('u'); |
| 225 | |
| 226 | switch (type) { |
| 227 | case 'c': |
| 228 | s += poly ? "poly8" : "int8"; |
| 229 | if (scal) |
| 230 | break; |
| 231 | s += quad ? "x16" : "x8"; |
| 232 | break; |
| 233 | case 's': |
| 234 | s += poly ? "poly16" : "int16"; |
| 235 | if (scal) |
| 236 | break; |
| 237 | s += quad ? "x8" : "x4"; |
| 238 | break; |
| 239 | case 'i': |
| 240 | s += "int32"; |
| 241 | if (scal) |
| 242 | break; |
| 243 | s += quad ? "x4" : "x2"; |
| 244 | break; |
| 245 | case 'l': |
| 246 | s += "int64"; |
| 247 | if (scal) |
| 248 | break; |
| 249 | s += quad ? "x2" : "x1"; |
| 250 | break; |
| 251 | case 'h': |
| 252 | s += "float16"; |
| 253 | if (scal) |
| 254 | break; |
| 255 | s += quad ? "x8" : "x4"; |
| 256 | break; |
| 257 | case 'f': |
| 258 | s += "float32"; |
| 259 | if (scal) |
| 260 | break; |
| 261 | s += quad ? "x4" : "x2"; |
| 262 | break; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 263 | default: |
| 264 | throw "unhandled type!"; |
| 265 | break; |
| 266 | } |
| 267 | |
| 268 | if (mod == '2') |
| 269 | s += "x2"; |
| 270 | if (mod == '3') |
| 271 | s += "x3"; |
| 272 | if (mod == '4') |
| 273 | s += "x4"; |
| 274 | |
| 275 | // Append _t, finishing the type string typedef type. |
| 276 | s += "_t"; |
| 277 | |
| 278 | if (cnst) |
| 279 | s += " const"; |
| 280 | |
| 281 | if (pntr) |
| 282 | s += " *"; |
| 283 | |
| 284 | return s.str(); |
| 285 | } |
| 286 | |
Bob Wilson | 1ac27cf | 2010-06-24 22:04:30 +0000 | [diff] [blame] | 287 | /// BuiltinTypeString - for a modifier and type, generate the clang |
| 288 | /// BuiltinsARM.def prototype code for the function. See the top of clang's |
| 289 | /// Builtins.def for a description of the type strings. |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 290 | static std::string BuiltinTypeString(const char mod, StringRef typestr, |
| 291 | ClassKind ck, bool ret) { |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 292 | bool quad = false; |
| 293 | bool poly = false; |
| 294 | bool usgn = false; |
| 295 | bool scal = false; |
| 296 | bool cnst = false; |
| 297 | bool pntr = false; |
| 298 | |
| 299 | if (mod == 'v') |
Bob Wilson | 95148ad | 2010-12-01 19:49:56 +0000 | [diff] [blame] | 300 | return "v"; // void |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 301 | if (mod == 'i') |
Bob Wilson | 95148ad | 2010-12-01 19:49:56 +0000 | [diff] [blame] | 302 | return "i"; // int |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 303 | |
| 304 | // base type to get the type string for. |
| 305 | char type = ClassifyType(typestr, quad, poly, usgn); |
| 306 | |
| 307 | // Based on the modifying character, change the type and width if necessary. |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 308 | type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr); |
| 309 | |
Bob Wilson | 95148ad | 2010-12-01 19:49:56 +0000 | [diff] [blame] | 310 | // All pointers are void* pointers. Change type to 'v' now. |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 311 | if (pntr) { |
| 312 | usgn = false; |
| 313 | poly = false; |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 314 | type = 'v'; |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 315 | } |
Bob Wilson | 95148ad | 2010-12-01 19:49:56 +0000 | [diff] [blame] | 316 | // Treat half-float ('h') types as unsigned short ('s') types. |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 317 | if (type == 'h') { |
| 318 | type = 's'; |
| 319 | usgn = true; |
| 320 | } |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 321 | usgn = usgn | poly | ((ck == ClassI || ck == ClassW) && scal && type != 'f'); |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 322 | |
| 323 | if (scal) { |
| 324 | SmallString<128> s; |
| 325 | |
| 326 | if (usgn) |
| 327 | s.push_back('U'); |
Bob Wilson | 317bafb | 2010-12-02 00:24:59 +0000 | [diff] [blame] | 328 | else if (type == 'c') |
| 329 | s.push_back('S'); // make chars explicitly signed |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 330 | |
Bob Wilson | 95148ad | 2010-12-01 19:49:56 +0000 | [diff] [blame] | 331 | if (type == 'l') // 64-bit long |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 332 | s += "LLi"; |
| 333 | else |
| 334 | s.push_back(type); |
| 335 | |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 336 | if (cnst) |
| 337 | s.push_back('C'); |
| 338 | if (pntr) |
| 339 | s.push_back('*'); |
| 340 | return s.str(); |
| 341 | } |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 342 | |
| 343 | // Since the return value must be one type, return a vector type of the |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 344 | // appropriate width which we will bitcast. An exception is made for |
| 345 | // returning structs of 2, 3, or 4 vectors which are returned in a sret-like |
| 346 | // fashion, storing them to a pointer arg. |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 347 | if (ret) { |
Bob Wilson | 5b7fe59 | 2010-12-01 19:49:51 +0000 | [diff] [blame] | 348 | if (mod >= '2' && mod <= '4') |
Bob Wilson | 95148ad | 2010-12-01 19:49:56 +0000 | [diff] [blame] | 349 | return "vv*"; // void result with void* first argument |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 350 | if (mod == 'f' || (ck != ClassB && type == 'f')) |
Nate Begeman | 5638783 | 2010-06-08 06:01:16 +0000 | [diff] [blame] | 351 | return quad ? "V4f" : "V2f"; |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 352 | if (ck != ClassB && type == 's') |
Nate Begeman | 007afe4 | 2010-06-09 05:11:55 +0000 | [diff] [blame] | 353 | return quad ? "V8s" : "V4s"; |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 354 | if (ck != ClassB && type == 'i') |
Nate Begeman | 5638783 | 2010-06-08 06:01:16 +0000 | [diff] [blame] | 355 | return quad ? "V4i" : "V2i"; |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 356 | if (ck != ClassB && type == 'l') |
Nate Begeman | 007afe4 | 2010-06-09 05:11:55 +0000 | [diff] [blame] | 357 | return quad ? "V2LLi" : "V1LLi"; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 358 | |
Bob Wilson | 317bafb | 2010-12-02 00:24:59 +0000 | [diff] [blame] | 359 | return quad ? "V16Sc" : "V8Sc"; |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | // Non-return array types are passed as individual vectors. |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 363 | if (mod == '2') |
Bob Wilson | 317bafb | 2010-12-02 00:24:59 +0000 | [diff] [blame] | 364 | return quad ? "V16ScV16Sc" : "V8ScV8Sc"; |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 365 | if (mod == '3') |
Bob Wilson | 317bafb | 2010-12-02 00:24:59 +0000 | [diff] [blame] | 366 | return quad ? "V16ScV16ScV16Sc" : "V8ScV8ScV8Sc"; |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 367 | if (mod == '4') |
Bob Wilson | 317bafb | 2010-12-02 00:24:59 +0000 | [diff] [blame] | 368 | return quad ? "V16ScV16ScV16ScV16Sc" : "V8ScV8ScV8ScV8Sc"; |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 369 | |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 370 | if (mod == 'f' || (ck != ClassB && type == 'f')) |
Nate Begeman | 007afe4 | 2010-06-09 05:11:55 +0000 | [diff] [blame] | 371 | return quad ? "V4f" : "V2f"; |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 372 | if (ck != ClassB && type == 's') |
Nate Begeman | 007afe4 | 2010-06-09 05:11:55 +0000 | [diff] [blame] | 373 | return quad ? "V8s" : "V4s"; |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 374 | if (ck != ClassB && type == 'i') |
Nate Begeman | 007afe4 | 2010-06-09 05:11:55 +0000 | [diff] [blame] | 375 | return quad ? "V4i" : "V2i"; |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 376 | if (ck != ClassB && type == 'l') |
Nate Begeman | 007afe4 | 2010-06-09 05:11:55 +0000 | [diff] [blame] | 377 | return quad ? "V2LLi" : "V1LLi"; |
| 378 | |
Bob Wilson | 317bafb | 2010-12-02 00:24:59 +0000 | [diff] [blame] | 379 | return quad ? "V16Sc" : "V8Sc"; |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 382 | /// MangleName - Append a type or width suffix to a base neon function name, |
| 383 | /// and insert a 'q' in the appropriate location if the operation works on |
| 384 | /// 128b rather than 64b. E.g. turn "vst2_lane" into "vst2q_lane_f32", etc. |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 385 | static std::string MangleName(const std::string &name, StringRef typestr, |
| 386 | ClassKind ck) { |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 387 | if (name == "vcvt_f32_f16") |
| 388 | return name; |
| 389 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 390 | bool quad = false; |
| 391 | bool poly = false; |
| 392 | bool usgn = false; |
| 393 | char type = ClassifyType(typestr, quad, poly, usgn); |
| 394 | |
| 395 | std::string s = name; |
| 396 | |
| 397 | switch (type) { |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 398 | case 'c': |
| 399 | switch (ck) { |
| 400 | case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break; |
| 401 | case ClassI: s += "_i8"; break; |
| 402 | case ClassW: s += "_8"; break; |
| 403 | default: break; |
| 404 | } |
| 405 | break; |
| 406 | case 's': |
| 407 | switch (ck) { |
| 408 | case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break; |
| 409 | case ClassI: s += "_i16"; break; |
| 410 | case ClassW: s += "_16"; break; |
| 411 | default: break; |
| 412 | } |
| 413 | break; |
| 414 | case 'i': |
| 415 | switch (ck) { |
| 416 | case ClassS: s += usgn ? "_u32" : "_s32"; break; |
| 417 | case ClassI: s += "_i32"; break; |
| 418 | case ClassW: s += "_32"; break; |
| 419 | default: break; |
| 420 | } |
| 421 | break; |
| 422 | case 'l': |
| 423 | switch (ck) { |
| 424 | case ClassS: s += usgn ? "_u64" : "_s64"; break; |
| 425 | case ClassI: s += "_i64"; break; |
| 426 | case ClassW: s += "_64"; break; |
| 427 | default: break; |
| 428 | } |
| 429 | break; |
| 430 | case 'h': |
| 431 | switch (ck) { |
| 432 | case ClassS: |
| 433 | case ClassI: s += "_f16"; break; |
| 434 | case ClassW: s += "_16"; break; |
| 435 | default: break; |
| 436 | } |
| 437 | break; |
| 438 | case 'f': |
| 439 | switch (ck) { |
| 440 | case ClassS: |
| 441 | case ClassI: s += "_f32"; break; |
| 442 | case ClassW: s += "_32"; break; |
| 443 | default: break; |
| 444 | } |
| 445 | break; |
| 446 | default: |
| 447 | throw "unhandled type!"; |
| 448 | break; |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 449 | } |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 450 | if (ck == ClassB) |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 451 | s += "_v"; |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 452 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 453 | // Insert a 'q' before the first '_' character so that it ends up before |
| 454 | // _lane or _n on vector-scalar operations. |
| 455 | if (quad) { |
| 456 | size_t pos = s.find('_'); |
| 457 | s = s.insert(pos, "q"); |
| 458 | } |
| 459 | return s; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 462 | // Generate the string "(argtype a, argtype b, ...)" |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 463 | static std::string GenArgs(const std::string &proto, StringRef typestr) { |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 464 | bool define = proto.find('i') != std::string::npos; |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 465 | char arg = 'a'; |
| 466 | |
| 467 | std::string s; |
| 468 | s += "("; |
| 469 | |
| 470 | for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) { |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 471 | if (define) { |
| 472 | // Immediate macro arguments are used directly instead of being assigned |
| 473 | // to local temporaries; prepend an underscore prefix to make their |
| 474 | // names consistent with the local temporaries. |
| 475 | if (proto[i] == 'i') |
| 476 | s += "__"; |
| 477 | } else { |
| 478 | s += TypeString(proto[i], typestr) + " __"; |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 479 | } |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 480 | s.push_back(arg); |
| 481 | if ((i + 1) < e) |
| 482 | s += ", "; |
| 483 | } |
| 484 | |
| 485 | s += ")"; |
| 486 | return s; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 489 | // Macro arguments are not type-checked like inline function arguments, so |
| 490 | // assign them to local temporaries to get the right type checking. |
Bob Wilson | 377296e | 2010-12-02 07:10:39 +0000 | [diff] [blame] | 491 | static std::string GenMacroLocals(const std::string &proto, StringRef typestr) { |
| 492 | char arg = 'a'; |
| 493 | std::string s; |
| 494 | |
| 495 | for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) { |
| 496 | // Do not create a temporary for an immediate argument. |
| 497 | // That would defeat the whole point of using a macro! |
| 498 | if (proto[i] == 'i') continue; |
| 499 | |
| 500 | s += TypeString(proto[i], typestr) + " __"; |
| 501 | s.push_back(arg); |
| 502 | s += " = ("; |
| 503 | s.push_back(arg); |
| 504 | s += "); "; |
| 505 | } |
| 506 | |
| 507 | s += "\\\n "; |
| 508 | return s; |
| 509 | } |
| 510 | |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 511 | static std::string Duplicate(unsigned nElts, StringRef typestr, |
| 512 | const std::string &a) { |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 513 | std::string s; |
| 514 | |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 515 | s = "(" + TypeString('d', typestr) + "){ "; |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 516 | for (unsigned i = 0; i != nElts; ++i) { |
| 517 | s += a; |
| 518 | if ((i + 1) < nElts) |
| 519 | s += ", "; |
| 520 | } |
| 521 | s += " }"; |
| 522 | |
| 523 | return s; |
| 524 | } |
| 525 | |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 526 | static unsigned GetNumElements(StringRef typestr, bool &quad) { |
| 527 | quad = false; |
| 528 | bool dummy = false; |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 529 | char type = ClassifyType(typestr, quad, dummy, dummy); |
| 530 | unsigned nElts = 0; |
| 531 | switch (type) { |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 532 | case 'c': nElts = 8; break; |
| 533 | case 's': nElts = 4; break; |
| 534 | case 'i': nElts = 2; break; |
| 535 | case 'l': nElts = 1; break; |
| 536 | case 'h': nElts = 4; break; |
| 537 | case 'f': nElts = 2; break; |
| 538 | default: |
| 539 | throw "unhandled type!"; |
| 540 | break; |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 541 | } |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 542 | if (quad) nElts <<= 1; |
| 543 | return nElts; |
| 544 | } |
| 545 | |
| 546 | // Generate the definition for this intrinsic, e.g. "a + b" for OpAdd. |
| 547 | static std::string GenOpString(OpKind op, const std::string &proto, |
| 548 | StringRef typestr) { |
| 549 | bool quad; |
| 550 | unsigned nElts = GetNumElements(typestr, quad); |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 551 | |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 552 | // If this builtin takes an immediate argument, we need to #define it rather |
| 553 | // than use a standard declaration, so that SemaChecking can range check |
| 554 | // the immediate passed by the user. |
| 555 | bool define = proto.find('i') != std::string::npos; |
| 556 | |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 557 | std::string ts = TypeString(proto[0], typestr); |
Bob Wilson | ee9ca07 | 2010-09-15 01:52:33 +0000 | [diff] [blame] | 558 | std::string s; |
| 559 | if (op == OpHi || op == OpLo) { |
Bob Wilson | 37a0b54 | 2010-12-02 07:44:23 +0000 | [diff] [blame] | 560 | s = "union { " + ts + " r; double d; } u; u.d = "; |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 561 | } else if (!define) { |
Bob Wilson | 37a0b54 | 2010-12-02 07:44:23 +0000 | [diff] [blame] | 562 | s = "return "; |
Bob Wilson | ee9ca07 | 2010-09-15 01:52:33 +0000 | [diff] [blame] | 563 | } |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 564 | |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 565 | switch(op) { |
| 566 | case OpAdd: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 567 | s += "__a + __b;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 568 | break; |
| 569 | case OpSub: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 570 | s += "__a - __b;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 571 | break; |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 572 | case OpMulN: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 573 | s += "__a * " + Duplicate(nElts, typestr, "__b") + ";"; |
Bob Wilson | f18dfec | 2010-11-16 19:16:06 +0000 | [diff] [blame] | 574 | break; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 575 | case OpMul: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 576 | s += "__a * __b;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 577 | break; |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 578 | case OpMlaN: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 579 | s += "__a + (__b * " + Duplicate(nElts, typestr, "__c") + ");"; |
Bob Wilson | f18dfec | 2010-11-16 19:16:06 +0000 | [diff] [blame] | 580 | break; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 581 | case OpMla: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 582 | s += "__a + (__b * __c);"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 583 | break; |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 584 | case OpMlsN: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 585 | s += "__a - (__b * " + Duplicate(nElts, typestr, "__c") + ");"; |
Bob Wilson | f18dfec | 2010-11-16 19:16:06 +0000 | [diff] [blame] | 586 | break; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 587 | case OpMls: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 588 | s += "__a - (__b * __c);"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 589 | break; |
| 590 | case OpEq: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 591 | s += "(" + ts + ")(__a == __b);"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 592 | break; |
| 593 | case OpGe: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 594 | s += "(" + ts + ")(__a >= __b);"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 595 | break; |
| 596 | case OpLe: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 597 | s += "(" + ts + ")(__a <= __b);"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 598 | break; |
| 599 | case OpGt: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 600 | s += "(" + ts + ")(__a > __b);"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 601 | break; |
| 602 | case OpLt: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 603 | s += "(" + ts + ")(__a < __b);"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 604 | break; |
| 605 | case OpNeg: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 606 | s += " -__a;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 607 | break; |
| 608 | case OpNot: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 609 | s += " ~__a;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 610 | break; |
| 611 | case OpAnd: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 612 | s += "__a & __b;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 613 | break; |
| 614 | case OpOr: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 615 | s += "__a | __b;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 616 | break; |
| 617 | case OpXor: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 618 | s += "__a ^ __b;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 619 | break; |
| 620 | case OpAndNot: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 621 | s += "__a & ~__b;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 622 | break; |
| 623 | case OpOrNot: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 624 | s += "__a | ~__b;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 625 | break; |
Nate Begeman | 3861e74 | 2010-06-03 21:35:22 +0000 | [diff] [blame] | 626 | case OpCast: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 627 | s += "(" + ts + ")__a;"; |
Nate Begeman | 3861e74 | 2010-06-03 21:35:22 +0000 | [diff] [blame] | 628 | break; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 629 | case OpConcat: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 630 | s += "(" + ts + ")__builtin_shufflevector((int64x1_t)__a"; |
| 631 | s += ", (int64x1_t)__b, 0, 1);"; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 632 | break; |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 633 | case OpHi: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 634 | s += "(((float64x2_t)__a)[1]);"; |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 635 | break; |
| 636 | case OpLo: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 637 | s += "(((float64x2_t)__a)[0]);"; |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 638 | break; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 639 | case OpDup: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 640 | s += Duplicate(nElts, typestr, "__a") + ";"; |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 641 | break; |
| 642 | case OpSelect: |
| 643 | // ((0 & 1) | (~0 & 2)) |
Bob Wilson | 1dbfa91 | 2010-12-02 01:18:20 +0000 | [diff] [blame] | 644 | s += "(" + ts + ")"; |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 645 | ts = TypeString(proto[1], typestr); |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 646 | s += "((__a & (" + ts + ")__b) | "; |
| 647 | s += "(~__a & (" + ts + ")__c));"; |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 648 | break; |
| 649 | case OpRev16: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 650 | s += "__builtin_shufflevector(__a, __a"; |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 651 | for (unsigned i = 2; i <= nElts; i += 2) |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 652 | for (unsigned j = 0; j != 2; ++j) |
| 653 | s += ", " + utostr(i - j - 1); |
Bob Wilson | 37a0b54 | 2010-12-02 07:44:23 +0000 | [diff] [blame] | 654 | s += ");"; |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 655 | break; |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 656 | case OpRev32: { |
| 657 | unsigned WordElts = nElts >> (1 + (int)quad); |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 658 | s += "__builtin_shufflevector(__a, __a"; |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 659 | for (unsigned i = WordElts; i <= nElts; i += WordElts) |
| 660 | for (unsigned j = 0; j != WordElts; ++j) |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 661 | s += ", " + utostr(i - j - 1); |
Bob Wilson | 37a0b54 | 2010-12-02 07:44:23 +0000 | [diff] [blame] | 662 | s += ");"; |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 663 | break; |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 664 | } |
| 665 | case OpRev64: { |
| 666 | unsigned DblWordElts = nElts >> (int)quad; |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 667 | s += "__builtin_shufflevector(__a, __a"; |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 668 | for (unsigned i = DblWordElts; i <= nElts; i += DblWordElts) |
| 669 | for (unsigned j = 0; j != DblWordElts; ++j) |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 670 | s += ", " + utostr(i - j - 1); |
Bob Wilson | 37a0b54 | 2010-12-02 07:44:23 +0000 | [diff] [blame] | 671 | s += ");"; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 672 | break; |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 673 | } |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 674 | default: |
| 675 | throw "unknown OpKind!"; |
| 676 | break; |
| 677 | } |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 678 | if (op == OpHi || op == OpLo) { |
| 679 | if (!define) |
| 680 | s += " return"; |
| 681 | s += " u.r;"; |
| 682 | } |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 683 | return s; |
Nate Begeman | e66aab5 | 2010-06-02 07:14:28 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 686 | static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) { |
| 687 | unsigned mod = proto[0]; |
| 688 | unsigned ret = 0; |
| 689 | |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 690 | if (mod == 'v' || mod == 'f') |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 691 | mod = proto[1]; |
| 692 | |
| 693 | bool quad = false; |
| 694 | bool poly = false; |
| 695 | bool usgn = false; |
| 696 | bool scal = false; |
| 697 | bool cnst = false; |
| 698 | bool pntr = false; |
| 699 | |
Nate Begeman | 59d70cb | 2010-08-06 01:24:11 +0000 | [diff] [blame] | 700 | // Base type to get the type string for. |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 701 | char type = ClassifyType(typestr, quad, poly, usgn); |
| 702 | |
| 703 | // Based on the modifying character, change the type and width if necessary. |
| 704 | type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr); |
Nate Begeman | 59d70cb | 2010-08-06 01:24:11 +0000 | [diff] [blame] | 705 | |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 706 | if (usgn) |
| 707 | ret |= 0x08; |
Nate Begeman | 59d70cb | 2010-08-06 01:24:11 +0000 | [diff] [blame] | 708 | if (quad && proto[1] != 'g') |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 709 | ret |= 0x10; |
| 710 | |
| 711 | switch (type) { |
| 712 | case 'c': |
| 713 | ret |= poly ? 5 : 0; |
| 714 | break; |
| 715 | case 's': |
| 716 | ret |= poly ? 6 : 1; |
| 717 | break; |
| 718 | case 'i': |
| 719 | ret |= 2; |
| 720 | break; |
| 721 | case 'l': |
| 722 | ret |= 3; |
| 723 | break; |
| 724 | case 'h': |
| 725 | ret |= 7; |
| 726 | break; |
| 727 | case 'f': |
| 728 | ret |= 4; |
| 729 | break; |
| 730 | default: |
| 731 | throw "unhandled type!"; |
| 732 | break; |
| 733 | } |
| 734 | return ret; |
| 735 | } |
| 736 | |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 737 | // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a) |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 738 | static std::string GenBuiltin(const std::string &name, const std::string &proto, |
Bob Wilson | f18dfec | 2010-11-16 19:16:06 +0000 | [diff] [blame] | 739 | StringRef typestr, ClassKind ck) { |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 740 | std::string s; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 741 | |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 742 | // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit |
| 743 | // sret-like argument. |
Bob Wilson | 5b7fe59 | 2010-12-01 19:49:51 +0000 | [diff] [blame] | 744 | bool sret = (proto[0] >= '2' && proto[0] <= '4'); |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 745 | |
| 746 | // If this builtin takes an immediate argument, we need to #define it rather |
| 747 | // than use a standard declaration, so that SemaChecking can range check |
| 748 | // the immediate passed by the user. |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 749 | bool define = proto.find('i') != std::string::npos; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 750 | |
Bob Wilson | 95148ad | 2010-12-01 19:49:56 +0000 | [diff] [blame] | 751 | // Check if the prototype has a scalar operand with the type of the vector |
| 752 | // elements. If not, bitcasting the args will take care of arg checking. |
| 753 | // The actual signedness etc. will be taken care of with special enums. |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 754 | if (proto.find('s') == std::string::npos) |
| 755 | ck = ClassB; |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 756 | |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 757 | if (proto[0] != 'v') { |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 758 | std::string ts = TypeString(proto[0], typestr); |
| 759 | |
| 760 | if (define) { |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 761 | if (sret) |
Bob Wilson | 052008b | 2010-12-02 02:42:51 +0000 | [diff] [blame] | 762 | s += ts + " r; "; |
Bob Wilson | e106bc1 | 2010-12-02 00:24:56 +0000 | [diff] [blame] | 763 | else |
Bob Wilson | f18dfec | 2010-11-16 19:16:06 +0000 | [diff] [blame] | 764 | s += "(" + ts + ")"; |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 765 | } else if (sret) { |
| 766 | s += ts + " r; "; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 767 | } else { |
Bob Wilson | 37a0b54 | 2010-12-02 07:44:23 +0000 | [diff] [blame] | 768 | s += "return (" + ts + ")"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 769 | } |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | bool splat = proto.find('a') != std::string::npos; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 773 | |
| 774 | s += "__builtin_neon_"; |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 775 | if (splat) { |
Bob Wilson | 95148ad | 2010-12-01 19:49:56 +0000 | [diff] [blame] | 776 | // Call the non-splat builtin: chop off the "_n" suffix from the name. |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 777 | std::string vname(name, 0, name.size()-2); |
| 778 | s += MangleName(vname, typestr, ck); |
| 779 | } else { |
| 780 | s += MangleName(name, typestr, ck); |
| 781 | } |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 782 | s += "("; |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 783 | |
| 784 | // Pass the address of the return variable as the first argument to sret-like |
| 785 | // builtins. |
| 786 | if (sret) |
| 787 | s += "&r, "; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 788 | |
Bob Wilson | 377296e | 2010-12-02 07:10:39 +0000 | [diff] [blame] | 789 | char arg = 'a'; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 790 | for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) { |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 791 | std::string args = std::string(&arg, 1); |
Bob Wilson | 95148ad | 2010-12-01 19:49:56 +0000 | [diff] [blame] | 792 | |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 793 | // Use the local temporaries instead of the macro arguments. |
| 794 | args = "__" + args; |
Bob Wilson | f00140c | 2010-12-01 19:49:58 +0000 | [diff] [blame] | 795 | |
| 796 | bool argQuad = false; |
| 797 | bool argPoly = false; |
| 798 | bool argUsgn = false; |
| 799 | bool argScalar = false; |
| 800 | bool dummy = false; |
| 801 | char argType = ClassifyType(typestr, argQuad, argPoly, argUsgn); |
| 802 | argType = ModType(proto[i], argType, argQuad, argPoly, argUsgn, argScalar, |
| 803 | dummy, dummy); |
| 804 | |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 805 | // Handle multiple-vector values specially, emitting each subvector as an |
| 806 | // argument to the __builtin. |
Bob Wilson | 5b7fe59 | 2010-12-01 19:49:51 +0000 | [diff] [blame] | 807 | if (proto[i] >= '2' && proto[i] <= '4') { |
Bob Wilson | f00140c | 2010-12-01 19:49:58 +0000 | [diff] [blame] | 808 | // Check if an explicit cast is needed. |
| 809 | if (argType != 'c' || argPoly || argUsgn) |
| 810 | args = (argQuad ? "(int8x16_t)" : "(int8x8_t)") + args; |
| 811 | |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 812 | for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) { |
Bob Wilson | f18dfec | 2010-11-16 19:16:06 +0000 | [diff] [blame] | 813 | s += args + ".val[" + utostr(vi) + "]"; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 814 | if ((vi + 1) < ve) |
| 815 | s += ", "; |
| 816 | } |
| 817 | if ((i + 1) < e) |
| 818 | s += ", "; |
| 819 | |
| 820 | continue; |
| 821 | } |
| 822 | |
Bob Wilson | 4a6c7fc | 2010-12-02 01:18:23 +0000 | [diff] [blame] | 823 | if (splat && (i + 1) == e) |
| 824 | args = Duplicate(GetNumElements(typestr, argQuad), typestr, args); |
| 825 | |
Bob Wilson | f00140c | 2010-12-01 19:49:58 +0000 | [diff] [blame] | 826 | // Check if an explicit cast is needed. |
Bob Wilson | 4a6c7fc | 2010-12-02 01:18:23 +0000 | [diff] [blame] | 827 | if ((splat || !argScalar) && |
Bob Wilson | f00140c | 2010-12-01 19:49:58 +0000 | [diff] [blame] | 828 | ((ck == ClassB && argType != 'c') || argPoly || argUsgn)) { |
| 829 | std::string argTypeStr = "c"; |
| 830 | if (ck != ClassB) |
| 831 | argTypeStr = argType; |
| 832 | if (argQuad) |
| 833 | argTypeStr = "Q" + argTypeStr; |
| 834 | args = "(" + TypeString('d', argTypeStr) + ")" + args; |
| 835 | } |
| 836 | |
Bob Wilson | 4a6c7fc | 2010-12-02 01:18:23 +0000 | [diff] [blame] | 837 | s += args; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 838 | if ((i + 1) < e) |
| 839 | s += ", "; |
| 840 | } |
| 841 | |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 842 | // Extra constant integer to hold type class enum for this function, e.g. s8 |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 843 | if (ck == ClassB) |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 844 | s += ", " + utostr(GetNeonEnum(proto, typestr)); |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 845 | |
Bob Wilson | 052008b | 2010-12-02 02:42:51 +0000 | [diff] [blame] | 846 | s += ");"; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 847 | |
Bob Wilson | 37a0b54 | 2010-12-02 07:44:23 +0000 | [diff] [blame] | 848 | if (proto[0] != 'v' && sret) { |
| 849 | if (define) |
| 850 | s += " r;"; |
| 851 | else |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 852 | s += " return r;"; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 853 | } |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 854 | return s; |
Nate Begeman | e66aab5 | 2010-06-02 07:14:28 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 857 | static std::string GenBuiltinDef(const std::string &name, |
| 858 | const std::string &proto, |
| 859 | StringRef typestr, ClassKind ck) { |
| 860 | std::string s("BUILTIN(__builtin_neon_"); |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 861 | |
| 862 | // If all types are the same size, bitcasting the args will take care |
| 863 | // of arg checking. The actual signedness etc. will be taken care of with |
| 864 | // special enums. |
| 865 | if (proto.find('s') == std::string::npos) |
| 866 | ck = ClassB; |
| 867 | |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 868 | s += MangleName(name, typestr, ck); |
| 869 | s += ", \""; |
| 870 | |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 871 | for (unsigned i = 0, e = proto.size(); i != e; ++i) |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 872 | s += BuiltinTypeString(proto[i], typestr, ck, i == 0); |
| 873 | |
| 874 | // Extra constant integer to hold type class enum for this function, e.g. s8 |
| 875 | if (ck == ClassB) |
| 876 | s += "i"; |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 877 | |
| 878 | s += "\", \"n\")"; |
| 879 | return s; |
| 880 | } |
| 881 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 882 | /// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h |
| 883 | /// is comprised of type definitions and function declarations. |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 884 | void NeonEmitter::run(raw_ostream &OS) { |
| 885 | EmitSourceFileHeader("ARM NEON Header", OS); |
| 886 | |
| 887 | // FIXME: emit license into file? |
| 888 | |
| 889 | OS << "#ifndef __ARM_NEON_H\n"; |
| 890 | OS << "#define __ARM_NEON_H\n\n"; |
| 891 | |
| 892 | OS << "#ifndef __ARM_NEON__\n"; |
| 893 | OS << "#error \"NEON support not enabled\"\n"; |
| 894 | OS << "#endif\n\n"; |
| 895 | |
| 896 | OS << "#include <stdint.h>\n\n"; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 897 | |
| 898 | // Emit NEON-specific scalar typedefs. |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 899 | OS << "typedef float float32_t;\n"; |
Bob Wilson | 4fbf638 | 2010-11-16 23:57:03 +0000 | [diff] [blame] | 900 | OS << "typedef int8_t poly8_t;\n"; |
| 901 | OS << "typedef int16_t poly16_t;\n"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 902 | OS << "typedef uint16_t float16_t;\n"; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 903 | |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 904 | // Emit Neon vector typedefs. |
| 905 | std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs"); |
| 906 | SmallVector<StringRef, 24> TDTypeVec; |
| 907 | ParseTypes(0, TypedefTypes, TDTypeVec); |
| 908 | |
| 909 | // Emit vector typedefs. |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 910 | for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) { |
Bob Wilson | 6a8aceb | 2010-11-16 23:57:06 +0000 | [diff] [blame] | 911 | bool dummy, quad = false, poly = false; |
| 912 | (void) ClassifyType(TDTypeVec[i], quad, poly, dummy); |
| 913 | if (poly) |
| 914 | OS << "typedef __attribute__((neon_polyvector_type("; |
| 915 | else |
| 916 | OS << "typedef __attribute__((neon_vector_type("; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 917 | |
Bob Wilson | 6a8aceb | 2010-11-16 23:57:06 +0000 | [diff] [blame] | 918 | unsigned nElts = GetNumElements(TDTypeVec[i], quad); |
| 919 | OS << utostr(nElts) << "))) "; |
| 920 | if (nElts < 10) |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 921 | OS << " "; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 922 | |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 923 | OS << TypeString('s', TDTypeVec[i]); |
| 924 | OS << " " << TypeString('d', TDTypeVec[i]) << ";\n"; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 925 | } |
| 926 | OS << "\n"; |
Bob Wilson | 6a8aceb | 2010-11-16 23:57:06 +0000 | [diff] [blame] | 927 | OS << "typedef __attribute__((__vector_size__(8))) " |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 928 | "double float64x1_t;\n"; |
Bob Wilson | 6a8aceb | 2010-11-16 23:57:06 +0000 | [diff] [blame] | 929 | OS << "typedef __attribute__((__vector_size__(16))) " |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 930 | "double float64x2_t;\n"; |
Bob Wilson | ee9ca07 | 2010-09-15 01:52:33 +0000 | [diff] [blame] | 931 | OS << "\n"; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 932 | |
| 933 | // Emit struct typedefs. |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 934 | for (unsigned vi = 2; vi != 5; ++vi) { |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 935 | for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) { |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 936 | std::string ts = TypeString('d', TDTypeVec[i]); |
| 937 | std::string vs = TypeString('0' + vi, TDTypeVec[i]); |
| 938 | OS << "typedef struct " << vs << " {\n"; |
| 939 | OS << " " << ts << " val"; |
| 940 | OS << "[" << utostr(vi) << "]"; |
| 941 | OS << ";\n} "; |
Bob Wilson | f18dfec | 2010-11-16 19:16:06 +0000 | [diff] [blame] | 942 | OS << vs << ";\n\n"; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 943 | } |
| 944 | } |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 945 | |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 946 | OS << "#define __ai static __attribute__((__always_inline__))\n\n"; |
| 947 | |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 948 | std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst"); |
| 949 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 950 | // Unique the return+pattern types, and assign them. |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 951 | for (unsigned i = 0, e = RV.size(); i != e; ++i) { |
| 952 | Record *R = RV[i]; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 953 | std::string name = LowercaseString(R->getName()); |
| 954 | std::string Proto = R->getValueAsString("Prototype"); |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 955 | std::string Types = R->getValueAsString("Types"); |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 956 | |
| 957 | SmallVector<StringRef, 16> TypeVec; |
| 958 | ParseTypes(R, Types, TypeVec); |
| 959 | |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 960 | OpKind k = OpMap[R->getValueAsDef("Operand")->getName()]; |
Nate Begeman | e66aab5 | 2010-06-02 07:14:28 +0000 | [diff] [blame] | 961 | |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 962 | bool define = Proto.find('i') != std::string::npos; |
| 963 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 964 | for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) { |
| 965 | assert(!Proto.empty() && ""); |
| 966 | |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 967 | // static always inline + return type |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 968 | if (define) |
| 969 | OS << "#define"; |
| 970 | else |
| 971 | OS << "__ai " << TypeString(Proto[0], TypeVec[ti]); |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 972 | |
Nate Begeman | e66aab5 | 2010-06-02 07:14:28 +0000 | [diff] [blame] | 973 | // Function name with type suffix |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 974 | OS << " " << MangleName(name, TypeVec[ti], ClassS); |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 975 | |
Nate Begeman | e66aab5 | 2010-06-02 07:14:28 +0000 | [diff] [blame] | 976 | // Function arguments |
| 977 | OS << GenArgs(Proto, TypeVec[ti]); |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 978 | |
Nate Begeman | e66aab5 | 2010-06-02 07:14:28 +0000 | [diff] [blame] | 979 | // Definition. |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 980 | if (define) { |
Bob Wilson | 052008b | 2010-12-02 02:42:51 +0000 | [diff] [blame] | 981 | OS << " __extension__ ({ \\\n "; |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 982 | OS << GenMacroLocals(Proto, TypeVec[ti]); |
| 983 | } else { |
Bob Wilson | 052008b | 2010-12-02 02:42:51 +0000 | [diff] [blame] | 984 | OS << " { \\\n "; |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame^] | 985 | } |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 986 | |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 987 | if (k != OpNone) { |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 988 | OS << GenOpString(k, Proto, TypeVec[ti]); |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 989 | } else { |
| 990 | if (R->getSuperClasses().size() < 2) |
| 991 | throw TGError(R->getLoc(), "Builtin has no class kind"); |
| 992 | |
| 993 | ClassKind ck = ClassMap[R->getSuperClasses()[1]]; |
| 994 | |
| 995 | if (ck == ClassNone) |
| 996 | throw TGError(R->getLoc(), "Builtin has no class kind"); |
| 997 | OS << GenBuiltin(name, Proto, TypeVec[ti], ck); |
| 998 | } |
Bob Wilson | 052008b | 2010-12-02 02:42:51 +0000 | [diff] [blame] | 999 | if (define) |
| 1000 | OS << " })"; |
| 1001 | else |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 1002 | OS << " }"; |
| 1003 | OS << "\n"; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 1004 | } |
| 1005 | OS << "\n"; |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 1006 | } |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1007 | OS << "#undef __ai\n\n"; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 1008 | OS << "#endif /* __ARM_NEON_H */\n"; |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 1009 | } |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 1010 | |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1011 | static unsigned RangeFromType(StringRef typestr) { |
| 1012 | // base type to get the type string for. |
| 1013 | bool quad = false, dummy = false; |
| 1014 | char type = ClassifyType(typestr, quad, dummy, dummy); |
| 1015 | |
| 1016 | switch (type) { |
| 1017 | case 'c': |
Nate Begeman | 4da883a | 2010-06-15 22:10:31 +0000 | [diff] [blame] | 1018 | return (8 << (int)quad) - 1; |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1019 | case 'h': |
| 1020 | case 's': |
Nate Begeman | 4da883a | 2010-06-15 22:10:31 +0000 | [diff] [blame] | 1021 | return (4 << (int)quad) - 1; |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1022 | case 'f': |
| 1023 | case 'i': |
Nate Begeman | 4da883a | 2010-06-15 22:10:31 +0000 | [diff] [blame] | 1024 | return (2 << (int)quad) - 1; |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1025 | case 'l': |
Nate Begeman | 4da883a | 2010-06-15 22:10:31 +0000 | [diff] [blame] | 1026 | return (1 << (int)quad) - 1; |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1027 | default: |
| 1028 | throw "unhandled type!"; |
| 1029 | break; |
| 1030 | } |
Bob Wilson | fdb530d | 2010-07-28 18:21:10 +0000 | [diff] [blame] | 1031 | assert(0 && "unreachable"); |
| 1032 | return 0; |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1035 | /// runHeader - Emit a file with sections defining: |
| 1036 | /// 1. the NEON section of BuiltinsARM.def. |
| 1037 | /// 2. the SemaChecking code for the type overload checking. |
| 1038 | /// 3. the SemaChecking code for validation of intrinsic immedate arguments. |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 1039 | void NeonEmitter::runHeader(raw_ostream &OS) { |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1040 | std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst"); |
| 1041 | |
| 1042 | StringMap<OpKind> EmittedMap; |
| 1043 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1044 | // Generate BuiltinsARM.def for NEON |
| 1045 | OS << "#ifdef GET_NEON_BUILTINS\n"; |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1046 | for (unsigned i = 0, e = RV.size(); i != e; ++i) { |
| 1047 | Record *R = RV[i]; |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1048 | OpKind k = OpMap[R->getValueAsDef("Operand")->getName()]; |
| 1049 | if (k != OpNone) |
| 1050 | continue; |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1051 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1052 | std::string Proto = R->getValueAsString("Prototype"); |
| 1053 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 1054 | // Functions with 'a' (the splat code) in the type prototype should not get |
| 1055 | // their own builtin as they use the non-splat variant. |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 1056 | if (Proto.find('a') != std::string::npos) |
| 1057 | continue; |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 1058 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1059 | std::string Types = R->getValueAsString("Types"); |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1060 | SmallVector<StringRef, 16> TypeVec; |
| 1061 | ParseTypes(R, Types, TypeVec); |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 1062 | |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1063 | if (R->getSuperClasses().size() < 2) |
| 1064 | throw TGError(R->getLoc(), "Builtin has no class kind"); |
| 1065 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1066 | std::string name = LowercaseString(R->getName()); |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1067 | ClassKind ck = ClassMap[R->getSuperClasses()[1]]; |
| 1068 | |
| 1069 | for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) { |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 1070 | // Generate the BuiltinsARM.def declaration for this builtin, ensuring |
| 1071 | // that each unique BUILTIN() macro appears only once in the output |
| 1072 | // stream. |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1073 | std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck); |
| 1074 | if (EmittedMap.count(bd)) |
| 1075 | continue; |
| 1076 | |
| 1077 | EmittedMap[bd] = OpNone; |
| 1078 | OS << bd << "\n"; |
| 1079 | } |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1080 | } |
| 1081 | OS << "#endif\n\n"; |
| 1082 | |
| 1083 | // Generate the overloaded type checking code for SemaChecking.cpp |
| 1084 | OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n"; |
| 1085 | for (unsigned i = 0, e = RV.size(); i != e; ++i) { |
| 1086 | Record *R = RV[i]; |
| 1087 | OpKind k = OpMap[R->getValueAsDef("Operand")->getName()]; |
| 1088 | if (k != OpNone) |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 1089 | continue; |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1090 | |
| 1091 | std::string Proto = R->getValueAsString("Prototype"); |
| 1092 | std::string Types = R->getValueAsString("Types"); |
| 1093 | std::string name = LowercaseString(R->getName()); |
| 1094 | |
| 1095 | // Functions with 'a' (the splat code) in the type prototype should not get |
| 1096 | // their own builtin as they use the non-splat variant. |
| 1097 | if (Proto.find('a') != std::string::npos) |
| 1098 | continue; |
| 1099 | |
| 1100 | // Functions which have a scalar argument cannot be overloaded, no need to |
| 1101 | // check them if we are emitting the type checking code. |
| 1102 | if (Proto.find('s') != std::string::npos) |
| 1103 | continue; |
| 1104 | |
| 1105 | SmallVector<StringRef, 16> TypeVec; |
| 1106 | ParseTypes(R, Types, TypeVec); |
| 1107 | |
| 1108 | if (R->getSuperClasses().size() < 2) |
| 1109 | throw TGError(R->getLoc(), "Builtin has no class kind"); |
| 1110 | |
| 1111 | int si = -1, qi = -1; |
| 1112 | unsigned mask = 0, qmask = 0; |
| 1113 | for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) { |
| 1114 | // Generate the switch case(s) for this builtin for the type validation. |
| 1115 | bool quad = false, poly = false, usgn = false; |
| 1116 | (void) ClassifyType(TypeVec[ti], quad, poly, usgn); |
| 1117 | |
| 1118 | if (quad) { |
| 1119 | qi = ti; |
| 1120 | qmask |= 1 << GetNeonEnum(Proto, TypeVec[ti]); |
| 1121 | } else { |
| 1122 | si = ti; |
| 1123 | mask |= 1 << GetNeonEnum(Proto, TypeVec[ti]); |
| 1124 | } |
| 1125 | } |
| 1126 | if (mask) |
| 1127 | OS << "case ARM::BI__builtin_neon_" |
| 1128 | << MangleName(name, TypeVec[si], ClassB) |
| 1129 | << ": mask = " << "0x" << utohexstr(mask) << "; break;\n"; |
| 1130 | if (qmask) |
| 1131 | OS << "case ARM::BI__builtin_neon_" |
| 1132 | << MangleName(name, TypeVec[qi], ClassB) |
| 1133 | << ": mask = " << "0x" << utohexstr(qmask) << "; break;\n"; |
| 1134 | } |
| 1135 | OS << "#endif\n\n"; |
| 1136 | |
| 1137 | // Generate the intrinsic range checking code for shift/lane immediates. |
| 1138 | OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n"; |
| 1139 | for (unsigned i = 0, e = RV.size(); i != e; ++i) { |
| 1140 | Record *R = RV[i]; |
| 1141 | |
| 1142 | OpKind k = OpMap[R->getValueAsDef("Operand")->getName()]; |
| 1143 | if (k != OpNone) |
| 1144 | continue; |
| 1145 | |
| 1146 | std::string name = LowercaseString(R->getName()); |
| 1147 | std::string Proto = R->getValueAsString("Prototype"); |
| 1148 | std::string Types = R->getValueAsString("Types"); |
| 1149 | |
| 1150 | // Functions with 'a' (the splat code) in the type prototype should not get |
| 1151 | // their own builtin as they use the non-splat variant. |
| 1152 | if (Proto.find('a') != std::string::npos) |
| 1153 | continue; |
| 1154 | |
| 1155 | // Functions which do not have an immediate do not need to have range |
| 1156 | // checking code emitted. |
| 1157 | if (Proto.find('i') == std::string::npos) |
| 1158 | continue; |
| 1159 | |
| 1160 | SmallVector<StringRef, 16> TypeVec; |
| 1161 | ParseTypes(R, Types, TypeVec); |
| 1162 | |
| 1163 | if (R->getSuperClasses().size() < 2) |
| 1164 | throw TGError(R->getLoc(), "Builtin has no class kind"); |
| 1165 | |
| 1166 | ClassKind ck = ClassMap[R->getSuperClasses()[1]]; |
| 1167 | |
| 1168 | for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) { |
| 1169 | std::string namestr, shiftstr, rangestr; |
| 1170 | |
| 1171 | // Builtins which are overloaded by type will need to have their upper |
| 1172 | // bound computed at Sema time based on the type constant. |
| 1173 | if (Proto.find('s') == std::string::npos) { |
| 1174 | ck = ClassB; |
| 1175 | if (R->getValueAsBit("isShift")) { |
| 1176 | shiftstr = ", true"; |
| 1177 | |
| 1178 | // Right shifts have an 'r' in the name, left shifts do not. |
| 1179 | if (name.find('r') != std::string::npos) |
| 1180 | rangestr = "l = 1; "; |
| 1181 | } |
| 1182 | rangestr += "u = RFT(TV" + shiftstr + ")"; |
| 1183 | } else { |
| 1184 | rangestr = "u = " + utostr(RangeFromType(TypeVec[ti])); |
| 1185 | } |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 1186 | // Make sure cases appear only once by uniquing them in a string map. |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1187 | namestr = MangleName(name, TypeVec[ti], ck); |
| 1188 | if (EmittedMap.count(namestr)) |
| 1189 | continue; |
| 1190 | EmittedMap[namestr] = OpNone; |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 1191 | |
| 1192 | // Calculate the index of the immediate that should be range checked. |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1193 | unsigned immidx = 0; |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 1194 | |
| 1195 | // Builtins that return a struct of multiple vectors have an extra |
| 1196 | // leading arg for the struct return. |
Bob Wilson | 5b7fe59 | 2010-12-01 19:49:51 +0000 | [diff] [blame] | 1197 | if (Proto[0] >= '2' && Proto[0] <= '4') |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 1198 | ++immidx; |
| 1199 | |
| 1200 | // Add one to the index for each argument until we reach the immediate |
| 1201 | // to be checked. Structs of vectors are passed as multiple arguments. |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1202 | for (unsigned ii = 1, ie = Proto.size(); ii != ie; ++ii) { |
| 1203 | switch (Proto[ii]) { |
| 1204 | default: immidx += 1; break; |
| 1205 | case '2': immidx += 2; break; |
| 1206 | case '3': immidx += 3; break; |
| 1207 | case '4': immidx += 4; break; |
| 1208 | case 'i': ie = ii + 1; break; |
| 1209 | } |
| 1210 | } |
| 1211 | OS << "case ARM::BI__builtin_neon_" << MangleName(name, TypeVec[ti], ck) |
| 1212 | << ": i = " << immidx << "; " << rangestr << "; break;\n"; |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 1213 | } |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1214 | } |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1215 | OS << "#endif\n\n"; |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 1216 | } |