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 |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 11 | // a declaration and definition of each function specified by the ARM NEON |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 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 |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 15 | // are suffixed with the element type of the input vectors. Functions may be |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 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; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 41 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 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; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 45 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 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; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 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 | } |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 107 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 108 | // remember poly. |
| 109 | if (ty[off] == 'P') { |
| 110 | poly = true; |
| 111 | ++off; |
| 112 | } |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 113 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 114 | // remember unsigned. |
| 115 | if (ty[off] == 'U') { |
| 116 | usgn = true; |
| 117 | ++off; |
| 118 | } |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 119 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 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; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 209 | |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 210 | if (mod == 'v') |
| 211 | return "void"; |
| 212 | if (mod == 'i') |
| 213 | return "int"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 214 | |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 215 | // base type to get the type string for. |
| 216 | char type = ClassifyType(typestr, quad, poly, usgn); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 217 | |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 218 | // Based on the modifying character, change the type and width if necessary. |
| 219 | type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 220 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 221 | SmallString<128> s; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 222 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 223 | if (usgn) |
| 224 | s.push_back('u'); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 225 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 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"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 274 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 275 | // Append _t, finishing the type string typedef type. |
| 276 | s += "_t"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 277 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 278 | if (cnst) |
| 279 | s += " const"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 280 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 281 | if (pntr) |
| 282 | s += " *"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 283 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 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; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 298 | |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 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 |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 303 | |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 304 | // base type to get the type string for. |
| 305 | char type = ClassifyType(typestr, quad, poly, usgn); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 306 | |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 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 |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +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); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 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"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 358 | |
Bob Wilson | 317bafb | 2010-12-02 00:24:59 +0000 | [diff] [blame] | 359 | return quad ? "V16Sc" : "V8Sc"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 360 | } |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 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"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 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 | |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 382 | /// MangleName - Append a type or width suffix to a base neon function name, |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 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; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 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; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 396 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 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"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 452 | |
| 453 | // Insert a 'q' before the first '_' character so that it ends up before |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 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'; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 466 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 467 | std::string s; |
| 468 | s += "("; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 469 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 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 | } |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 484 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 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; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 494 | |
Bob Wilson | 377296e | 2010-12-02 07:10:39 +0000 | [diff] [blame] | 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 | } |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 506 | |
Bob Wilson | 377296e | 2010-12-02 07:10:39 +0000 | [diff] [blame] | 507 | s += "\\\n "; |
| 508 | return s; |
| 509 | } |
| 510 | |
Bob Wilson | c4ba09d | 2010-12-07 20:02:45 +0000 | [diff] [blame^] | 511 | // Use the vmovl builtin to sign-extend or zero-extend a vector. |
| 512 | static std::string Extend(const std::string &proto, StringRef typestr, |
| 513 | const std::string &a) { |
| 514 | std::string s; |
| 515 | s = MangleName("vmovl", typestr, ClassS); |
| 516 | s += "(" + a + ")"; |
| 517 | return s; |
| 518 | } |
| 519 | |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 520 | static std::string Duplicate(unsigned nElts, StringRef typestr, |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 521 | const std::string &a) { |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 522 | std::string s; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 523 | |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 524 | s = "(" + TypeString('d', typestr) + "){ "; |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 525 | for (unsigned i = 0; i != nElts; ++i) { |
| 526 | s += a; |
| 527 | if ((i + 1) < nElts) |
| 528 | s += ", "; |
| 529 | } |
| 530 | s += " }"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 531 | |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 532 | return s; |
| 533 | } |
| 534 | |
Bob Wilson | b0d9869 | 2010-12-03 00:34:12 +0000 | [diff] [blame] | 535 | static std::string SplatLane(unsigned nElts, const std::string &vec, |
| 536 | const std::string &lane) { |
| 537 | std::string s = "__builtin_shufflevector(" + vec + ", " + vec; |
| 538 | for (unsigned i = 0; i < nElts; ++i) |
| 539 | s += ", " + lane; |
| 540 | s += ")"; |
| 541 | return s; |
| 542 | } |
| 543 | |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 544 | static unsigned GetNumElements(StringRef typestr, bool &quad) { |
| 545 | quad = false; |
| 546 | bool dummy = false; |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 547 | char type = ClassifyType(typestr, quad, dummy, dummy); |
| 548 | unsigned nElts = 0; |
| 549 | switch (type) { |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 550 | case 'c': nElts = 8; break; |
| 551 | case 's': nElts = 4; break; |
| 552 | case 'i': nElts = 2; break; |
| 553 | case 'l': nElts = 1; break; |
| 554 | case 'h': nElts = 4; break; |
| 555 | case 'f': nElts = 2; break; |
| 556 | default: |
| 557 | throw "unhandled type!"; |
| 558 | break; |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 559 | } |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 560 | if (quad) nElts <<= 1; |
| 561 | return nElts; |
| 562 | } |
| 563 | |
| 564 | // Generate the definition for this intrinsic, e.g. "a + b" for OpAdd. |
| 565 | static std::string GenOpString(OpKind op, const std::string &proto, |
| 566 | StringRef typestr) { |
| 567 | bool quad; |
| 568 | unsigned nElts = GetNumElements(typestr, quad); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 569 | |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 570 | // If this builtin takes an immediate argument, we need to #define it rather |
| 571 | // than use a standard declaration, so that SemaChecking can range check |
| 572 | // the immediate passed by the user. |
| 573 | bool define = proto.find('i') != std::string::npos; |
| 574 | |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 575 | std::string ts = TypeString(proto[0], typestr); |
Bob Wilson | ee9ca07 | 2010-09-15 01:52:33 +0000 | [diff] [blame] | 576 | std::string s; |
| 577 | if (op == OpHi || op == OpLo) { |
Bob Wilson | 37a0b54 | 2010-12-02 07:44:23 +0000 | [diff] [blame] | 578 | s = "union { " + ts + " r; double d; } u; u.d = "; |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 579 | } else if (!define) { |
Bob Wilson | 37a0b54 | 2010-12-02 07:44:23 +0000 | [diff] [blame] | 580 | s = "return "; |
Bob Wilson | ee9ca07 | 2010-09-15 01:52:33 +0000 | [diff] [blame] | 581 | } |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 582 | |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 583 | switch(op) { |
| 584 | case OpAdd: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 585 | s += "__a + __b;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 586 | break; |
| 587 | case OpSub: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 588 | s += "__a - __b;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 589 | break; |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 590 | case OpMulN: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 591 | s += "__a * " + Duplicate(nElts, typestr, "__b") + ";"; |
Bob Wilson | f18dfec | 2010-11-16 19:16:06 +0000 | [diff] [blame] | 592 | break; |
Bob Wilson | b0d9869 | 2010-12-03 00:34:12 +0000 | [diff] [blame] | 593 | case OpMulLane: |
| 594 | s += "__a * " + SplatLane(nElts, "__b", "__c") + ";"; |
| 595 | break; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 596 | case OpMul: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 597 | s += "__a * __b;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 598 | break; |
Bob Wilson | c4ba09d | 2010-12-07 20:02:45 +0000 | [diff] [blame^] | 599 | case OpMullN: |
| 600 | s += Extend(proto, typestr, "__a") + " * " + |
| 601 | Extend(proto, typestr, |
| 602 | Duplicate(nElts << (int)quad, typestr, "__b")) + ";"; |
| 603 | break; |
| 604 | case OpMull: |
| 605 | s += Extend(proto, typestr, "__a") + " * " + |
| 606 | Extend(proto, typestr, "__b") + ";"; |
| 607 | break; |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 608 | case OpMlaN: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 609 | s += "__a + (__b * " + Duplicate(nElts, typestr, "__c") + ");"; |
Bob Wilson | f18dfec | 2010-11-16 19:16:06 +0000 | [diff] [blame] | 610 | break; |
Bob Wilson | b0d9869 | 2010-12-03 00:34:12 +0000 | [diff] [blame] | 611 | case OpMlaLane: |
| 612 | s += "__a + (__b * " + SplatLane(nElts, "__c", "__d") + ");"; |
| 613 | break; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 614 | case OpMla: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 615 | s += "__a + (__b * __c);"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 616 | break; |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 617 | case OpMlsN: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 618 | s += "__a - (__b * " + Duplicate(nElts, typestr, "__c") + ");"; |
Bob Wilson | f18dfec | 2010-11-16 19:16:06 +0000 | [diff] [blame] | 619 | break; |
Bob Wilson | b0d9869 | 2010-12-03 00:34:12 +0000 | [diff] [blame] | 620 | case OpMlsLane: |
| 621 | s += "__a - (__b * " + SplatLane(nElts, "__c", "__d") + ");"; |
| 622 | break; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 623 | case OpMls: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 624 | s += "__a - (__b * __c);"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 625 | break; |
| 626 | case OpEq: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 627 | s += "(" + ts + ")(__a == __b);"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 628 | break; |
| 629 | case OpGe: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 630 | s += "(" + ts + ")(__a >= __b);"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 631 | break; |
| 632 | case OpLe: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 633 | s += "(" + ts + ")(__a <= __b);"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 634 | break; |
| 635 | case OpGt: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 636 | s += "(" + ts + ")(__a > __b);"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 637 | break; |
| 638 | case OpLt: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 639 | s += "(" + ts + ")(__a < __b);"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 640 | break; |
| 641 | case OpNeg: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 642 | s += " -__a;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 643 | break; |
| 644 | case OpNot: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 645 | s += " ~__a;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 646 | break; |
| 647 | case OpAnd: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 648 | s += "__a & __b;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 649 | break; |
| 650 | case OpOr: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 651 | s += "__a | __b;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 652 | break; |
| 653 | case OpXor: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 654 | s += "__a ^ __b;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 655 | break; |
| 656 | case OpAndNot: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 657 | s += "__a & ~__b;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 658 | break; |
| 659 | case OpOrNot: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 660 | s += "__a | ~__b;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 661 | break; |
Nate Begeman | 3861e74 | 2010-06-03 21:35:22 +0000 | [diff] [blame] | 662 | case OpCast: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 663 | s += "(" + ts + ")__a;"; |
Nate Begeman | 3861e74 | 2010-06-03 21:35:22 +0000 | [diff] [blame] | 664 | break; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 665 | case OpConcat: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 666 | s += "(" + ts + ")__builtin_shufflevector((int64x1_t)__a"; |
| 667 | s += ", (int64x1_t)__b, 0, 1);"; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 668 | break; |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 669 | case OpHi: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 670 | s += "(((float64x2_t)__a)[1]);"; |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 671 | break; |
| 672 | case OpLo: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 673 | s += "(((float64x2_t)__a)[0]);"; |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 674 | break; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 675 | case OpDup: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 676 | s += Duplicate(nElts, typestr, "__a") + ";"; |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 677 | break; |
| 678 | case OpSelect: |
| 679 | // ((0 & 1) | (~0 & 2)) |
Bob Wilson | 1dbfa91 | 2010-12-02 01:18:20 +0000 | [diff] [blame] | 680 | s += "(" + ts + ")"; |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 681 | ts = TypeString(proto[1], typestr); |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 682 | s += "((__a & (" + ts + ")__b) | "; |
| 683 | s += "(~__a & (" + ts + ")__c));"; |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 684 | break; |
| 685 | case OpRev16: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 686 | s += "__builtin_shufflevector(__a, __a"; |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 687 | for (unsigned i = 2; i <= nElts; i += 2) |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 688 | for (unsigned j = 0; j != 2; ++j) |
| 689 | s += ", " + utostr(i - j - 1); |
Bob Wilson | 37a0b54 | 2010-12-02 07:44:23 +0000 | [diff] [blame] | 690 | s += ");"; |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 691 | break; |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 692 | case OpRev32: { |
| 693 | unsigned WordElts = nElts >> (1 + (int)quad); |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 694 | s += "__builtin_shufflevector(__a, __a"; |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 695 | for (unsigned i = WordElts; i <= nElts; i += WordElts) |
| 696 | for (unsigned j = 0; j != WordElts; ++j) |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 697 | s += ", " + utostr(i - j - 1); |
Bob Wilson | 37a0b54 | 2010-12-02 07:44:23 +0000 | [diff] [blame] | 698 | s += ");"; |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 699 | break; |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 700 | } |
| 701 | case OpRev64: { |
| 702 | unsigned DblWordElts = nElts >> (int)quad; |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 703 | s += "__builtin_shufflevector(__a, __a"; |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 704 | for (unsigned i = DblWordElts; i <= nElts; i += DblWordElts) |
| 705 | for (unsigned j = 0; j != DblWordElts; ++j) |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 706 | s += ", " + utostr(i - j - 1); |
Bob Wilson | 37a0b54 | 2010-12-02 07:44:23 +0000 | [diff] [blame] | 707 | s += ");"; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 708 | break; |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 709 | } |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 710 | default: |
| 711 | throw "unknown OpKind!"; |
| 712 | break; |
| 713 | } |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 714 | if (op == OpHi || op == OpLo) { |
| 715 | if (!define) |
| 716 | s += " return"; |
| 717 | s += " u.r;"; |
| 718 | } |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 719 | return s; |
Nate Begeman | e66aab5 | 2010-06-02 07:14:28 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 722 | static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) { |
| 723 | unsigned mod = proto[0]; |
| 724 | unsigned ret = 0; |
| 725 | |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 726 | if (mod == 'v' || mod == 'f') |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 727 | mod = proto[1]; |
| 728 | |
| 729 | bool quad = false; |
| 730 | bool poly = false; |
| 731 | bool usgn = false; |
| 732 | bool scal = false; |
| 733 | bool cnst = false; |
| 734 | bool pntr = false; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 735 | |
Nate Begeman | 59d70cb | 2010-08-06 01:24:11 +0000 | [diff] [blame] | 736 | // Base type to get the type string for. |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 737 | char type = ClassifyType(typestr, quad, poly, usgn); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 738 | |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 739 | // Based on the modifying character, change the type and width if necessary. |
| 740 | type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr); |
Nate Begeman | 59d70cb | 2010-08-06 01:24:11 +0000 | [diff] [blame] | 741 | |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 742 | if (usgn) |
| 743 | ret |= 0x08; |
Nate Begeman | 59d70cb | 2010-08-06 01:24:11 +0000 | [diff] [blame] | 744 | if (quad && proto[1] != 'g') |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 745 | ret |= 0x10; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 746 | |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 747 | switch (type) { |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 748 | case 'c': |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 749 | ret |= poly ? 5 : 0; |
| 750 | break; |
| 751 | case 's': |
| 752 | ret |= poly ? 6 : 1; |
| 753 | break; |
| 754 | case 'i': |
| 755 | ret |= 2; |
| 756 | break; |
| 757 | case 'l': |
| 758 | ret |= 3; |
| 759 | break; |
| 760 | case 'h': |
| 761 | ret |= 7; |
| 762 | break; |
| 763 | case 'f': |
| 764 | ret |= 4; |
| 765 | break; |
| 766 | default: |
| 767 | throw "unhandled type!"; |
| 768 | break; |
| 769 | } |
| 770 | return ret; |
| 771 | } |
| 772 | |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 773 | // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a) |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 774 | static std::string GenBuiltin(const std::string &name, const std::string &proto, |
Bob Wilson | f18dfec | 2010-11-16 19:16:06 +0000 | [diff] [blame] | 775 | StringRef typestr, ClassKind ck) { |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 776 | std::string s; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 777 | |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 778 | // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit |
| 779 | // sret-like argument. |
Bob Wilson | 5b7fe59 | 2010-12-01 19:49:51 +0000 | [diff] [blame] | 780 | bool sret = (proto[0] >= '2' && proto[0] <= '4'); |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 781 | |
| 782 | // If this builtin takes an immediate argument, we need to #define it rather |
| 783 | // than use a standard declaration, so that SemaChecking can range check |
| 784 | // the immediate passed by the user. |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 785 | bool define = proto.find('i') != std::string::npos; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 786 | |
Bob Wilson | 95148ad | 2010-12-01 19:49:56 +0000 | [diff] [blame] | 787 | // Check if the prototype has a scalar operand with the type of the vector |
| 788 | // elements. If not, bitcasting the args will take care of arg checking. |
| 789 | // The actual signedness etc. will be taken care of with special enums. |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 790 | if (proto.find('s') == std::string::npos) |
| 791 | ck = ClassB; |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 792 | |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 793 | if (proto[0] != 'v') { |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 794 | std::string ts = TypeString(proto[0], typestr); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 795 | |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 796 | if (define) { |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 797 | if (sret) |
Bob Wilson | 052008b | 2010-12-02 02:42:51 +0000 | [diff] [blame] | 798 | s += ts + " r; "; |
Bob Wilson | e106bc1 | 2010-12-02 00:24:56 +0000 | [diff] [blame] | 799 | else |
Bob Wilson | f18dfec | 2010-11-16 19:16:06 +0000 | [diff] [blame] | 800 | s += "(" + ts + ")"; |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 801 | } else if (sret) { |
| 802 | s += ts + " r; "; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 803 | } else { |
Bob Wilson | 37a0b54 | 2010-12-02 07:44:23 +0000 | [diff] [blame] | 804 | s += "return (" + ts + ")"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 805 | } |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 806 | } |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 807 | |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 808 | bool splat = proto.find('a') != std::string::npos; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 809 | |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 810 | s += "__builtin_neon_"; |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 811 | if (splat) { |
Bob Wilson | 95148ad | 2010-12-01 19:49:56 +0000 | [diff] [blame] | 812 | // 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] | 813 | std::string vname(name, 0, name.size()-2); |
| 814 | s += MangleName(vname, typestr, ck); |
| 815 | } else { |
| 816 | s += MangleName(name, typestr, ck); |
| 817 | } |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 818 | s += "("; |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 819 | |
| 820 | // Pass the address of the return variable as the first argument to sret-like |
| 821 | // builtins. |
| 822 | if (sret) |
| 823 | s += "&r, "; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 824 | |
Bob Wilson | 377296e | 2010-12-02 07:10:39 +0000 | [diff] [blame] | 825 | char arg = 'a'; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 826 | for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) { |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 827 | std::string args = std::string(&arg, 1); |
Bob Wilson | 95148ad | 2010-12-01 19:49:56 +0000 | [diff] [blame] | 828 | |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 829 | // Use the local temporaries instead of the macro arguments. |
| 830 | args = "__" + args; |
Bob Wilson | f00140c | 2010-12-01 19:49:58 +0000 | [diff] [blame] | 831 | |
| 832 | bool argQuad = false; |
| 833 | bool argPoly = false; |
| 834 | bool argUsgn = false; |
| 835 | bool argScalar = false; |
| 836 | bool dummy = false; |
| 837 | char argType = ClassifyType(typestr, argQuad, argPoly, argUsgn); |
| 838 | argType = ModType(proto[i], argType, argQuad, argPoly, argUsgn, argScalar, |
| 839 | dummy, dummy); |
| 840 | |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 841 | // Handle multiple-vector values specially, emitting each subvector as an |
| 842 | // argument to the __builtin. |
Bob Wilson | 5b7fe59 | 2010-12-01 19:49:51 +0000 | [diff] [blame] | 843 | if (proto[i] >= '2' && proto[i] <= '4') { |
Bob Wilson | f00140c | 2010-12-01 19:49:58 +0000 | [diff] [blame] | 844 | // Check if an explicit cast is needed. |
| 845 | if (argType != 'c' || argPoly || argUsgn) |
| 846 | args = (argQuad ? "(int8x16_t)" : "(int8x8_t)") + args; |
| 847 | |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 848 | for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) { |
Bob Wilson | f18dfec | 2010-11-16 19:16:06 +0000 | [diff] [blame] | 849 | s += args + ".val[" + utostr(vi) + "]"; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 850 | if ((vi + 1) < ve) |
| 851 | s += ", "; |
| 852 | } |
| 853 | if ((i + 1) < e) |
| 854 | s += ", "; |
| 855 | |
| 856 | continue; |
| 857 | } |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 858 | |
Bob Wilson | 4a6c7fc | 2010-12-02 01:18:23 +0000 | [diff] [blame] | 859 | if (splat && (i + 1) == e) |
| 860 | args = Duplicate(GetNumElements(typestr, argQuad), typestr, args); |
| 861 | |
Bob Wilson | f00140c | 2010-12-01 19:49:58 +0000 | [diff] [blame] | 862 | // Check if an explicit cast is needed. |
Bob Wilson | 4a6c7fc | 2010-12-02 01:18:23 +0000 | [diff] [blame] | 863 | if ((splat || !argScalar) && |
Bob Wilson | f00140c | 2010-12-01 19:49:58 +0000 | [diff] [blame] | 864 | ((ck == ClassB && argType != 'c') || argPoly || argUsgn)) { |
| 865 | std::string argTypeStr = "c"; |
| 866 | if (ck != ClassB) |
| 867 | argTypeStr = argType; |
| 868 | if (argQuad) |
| 869 | argTypeStr = "Q" + argTypeStr; |
| 870 | args = "(" + TypeString('d', argTypeStr) + ")" + args; |
| 871 | } |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 872 | |
Bob Wilson | 4a6c7fc | 2010-12-02 01:18:23 +0000 | [diff] [blame] | 873 | s += args; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 874 | if ((i + 1) < e) |
| 875 | s += ", "; |
| 876 | } |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 877 | |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 878 | // 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] | 879 | if (ck == ClassB) |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 880 | s += ", " + utostr(GetNeonEnum(proto, typestr)); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 881 | |
Bob Wilson | 052008b | 2010-12-02 02:42:51 +0000 | [diff] [blame] | 882 | s += ");"; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 883 | |
Bob Wilson | 37a0b54 | 2010-12-02 07:44:23 +0000 | [diff] [blame] | 884 | if (proto[0] != 'v' && sret) { |
| 885 | if (define) |
| 886 | s += " r;"; |
| 887 | else |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 888 | s += " return r;"; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 889 | } |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 890 | return s; |
Nate Begeman | e66aab5 | 2010-06-02 07:14:28 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 893 | static std::string GenBuiltinDef(const std::string &name, |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 894 | const std::string &proto, |
| 895 | StringRef typestr, ClassKind ck) { |
| 896 | std::string s("BUILTIN(__builtin_neon_"); |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 897 | |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 898 | // If all types are the same size, bitcasting the args will take care |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 899 | // of arg checking. The actual signedness etc. will be taken care of with |
| 900 | // special enums. |
| 901 | if (proto.find('s') == std::string::npos) |
| 902 | ck = ClassB; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 903 | |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 904 | s += MangleName(name, typestr, ck); |
| 905 | s += ", \""; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 906 | |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 907 | for (unsigned i = 0, e = proto.size(); i != e; ++i) |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 908 | s += BuiltinTypeString(proto[i], typestr, ck, i == 0); |
| 909 | |
| 910 | // Extra constant integer to hold type class enum for this function, e.g. s8 |
| 911 | if (ck == ClassB) |
| 912 | s += "i"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 913 | |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 914 | s += "\", \"n\")"; |
| 915 | return s; |
| 916 | } |
| 917 | |
Bob Wilson | 3890e39 | 2010-12-07 01:12:23 +0000 | [diff] [blame] | 918 | static std::string GenIntrinsic(const std::string &name, |
| 919 | const std::string &proto, |
| 920 | StringRef outTypeStr, StringRef inTypeStr, |
| 921 | OpKind kind, ClassKind classKind) { |
| 922 | assert(!proto.empty() && ""); |
| 923 | bool define = proto.find('i') != std::string::npos; |
| 924 | std::string s; |
| 925 | |
| 926 | // static always inline + return type |
| 927 | if (define) |
| 928 | s += "#define "; |
| 929 | else |
| 930 | s += "__ai " + TypeString(proto[0], outTypeStr) + " "; |
| 931 | |
| 932 | // Function name with type suffix |
| 933 | std::string mangledName = MangleName(name, outTypeStr, ClassS); |
| 934 | if (outTypeStr != inTypeStr) { |
| 935 | // If the input type is different (e.g., for vreinterpret), append a suffix |
| 936 | // for the input type. String off a "Q" (quad) prefix so that MangleName |
| 937 | // does not insert another "q" in the name. |
| 938 | unsigned typeStrOff = (inTypeStr[0] == 'Q' ? 1 : 0); |
| 939 | StringRef inTypeNoQuad = inTypeStr.substr(typeStrOff); |
| 940 | mangledName = MangleName(mangledName, inTypeNoQuad, ClassS); |
| 941 | } |
| 942 | s += mangledName; |
| 943 | |
| 944 | // Function arguments |
| 945 | s += GenArgs(proto, inTypeStr); |
| 946 | |
| 947 | // Definition. |
| 948 | if (define) { |
| 949 | s += " __extension__ ({ \\\n "; |
| 950 | s += GenMacroLocals(proto, inTypeStr); |
| 951 | } else { |
| 952 | s += " { \\\n "; |
| 953 | } |
| 954 | |
| 955 | if (kind != OpNone) |
| 956 | s += GenOpString(kind, proto, outTypeStr); |
| 957 | else |
| 958 | s += GenBuiltin(name, proto, outTypeStr, classKind); |
| 959 | if (define) |
| 960 | s += " })"; |
| 961 | else |
| 962 | s += " }"; |
| 963 | s += "\n"; |
| 964 | return s; |
| 965 | } |
| 966 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 967 | /// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h |
| 968 | /// is comprised of type definitions and function declarations. |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 969 | void NeonEmitter::run(raw_ostream &OS) { |
| 970 | EmitSourceFileHeader("ARM NEON Header", OS); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 971 | |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 972 | // FIXME: emit license into file? |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 973 | |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 974 | OS << "#ifndef __ARM_NEON_H\n"; |
| 975 | OS << "#define __ARM_NEON_H\n\n"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 976 | |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 977 | OS << "#ifndef __ARM_NEON__\n"; |
| 978 | OS << "#error \"NEON support not enabled\"\n"; |
| 979 | OS << "#endif\n\n"; |
| 980 | |
| 981 | OS << "#include <stdint.h>\n\n"; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 982 | |
| 983 | // Emit NEON-specific scalar typedefs. |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 984 | OS << "typedef float float32_t;\n"; |
Bob Wilson | 4fbf638 | 2010-11-16 23:57:03 +0000 | [diff] [blame] | 985 | OS << "typedef int8_t poly8_t;\n"; |
| 986 | OS << "typedef int16_t poly16_t;\n"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 987 | OS << "typedef uint16_t float16_t;\n"; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 988 | |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 989 | // Emit Neon vector typedefs. |
| 990 | std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs"); |
| 991 | SmallVector<StringRef, 24> TDTypeVec; |
| 992 | ParseTypes(0, TypedefTypes, TDTypeVec); |
| 993 | |
| 994 | // Emit vector typedefs. |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 995 | for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) { |
Bob Wilson | 6a8aceb | 2010-11-16 23:57:06 +0000 | [diff] [blame] | 996 | bool dummy, quad = false, poly = false; |
| 997 | (void) ClassifyType(TDTypeVec[i], quad, poly, dummy); |
| 998 | if (poly) |
| 999 | OS << "typedef __attribute__((neon_polyvector_type("; |
| 1000 | else |
| 1001 | OS << "typedef __attribute__((neon_vector_type("; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1002 | |
Bob Wilson | 6a8aceb | 2010-11-16 23:57:06 +0000 | [diff] [blame] | 1003 | unsigned nElts = GetNumElements(TDTypeVec[i], quad); |
| 1004 | OS << utostr(nElts) << "))) "; |
| 1005 | if (nElts < 10) |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 1006 | OS << " "; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1007 | |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 1008 | OS << TypeString('s', TDTypeVec[i]); |
| 1009 | OS << " " << TypeString('d', TDTypeVec[i]) << ";\n"; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 1010 | } |
| 1011 | OS << "\n"; |
Bob Wilson | 6a8aceb | 2010-11-16 23:57:06 +0000 | [diff] [blame] | 1012 | OS << "typedef __attribute__((__vector_size__(8))) " |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 1013 | "double float64x1_t;\n"; |
Bob Wilson | 6a8aceb | 2010-11-16 23:57:06 +0000 | [diff] [blame] | 1014 | OS << "typedef __attribute__((__vector_size__(16))) " |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 1015 | "double float64x2_t;\n"; |
Bob Wilson | ee9ca07 | 2010-09-15 01:52:33 +0000 | [diff] [blame] | 1016 | OS << "\n"; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 1017 | |
| 1018 | // Emit struct typedefs. |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 1019 | for (unsigned vi = 2; vi != 5; ++vi) { |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 1020 | for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) { |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 1021 | std::string ts = TypeString('d', TDTypeVec[i]); |
| 1022 | std::string vs = TypeString('0' + vi, TDTypeVec[i]); |
| 1023 | OS << "typedef struct " << vs << " {\n"; |
| 1024 | OS << " " << ts << " val"; |
| 1025 | OS << "[" << utostr(vi) << "]"; |
| 1026 | OS << ";\n} "; |
Bob Wilson | f18dfec | 2010-11-16 19:16:06 +0000 | [diff] [blame] | 1027 | OS << vs << ";\n\n"; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 1028 | } |
| 1029 | } |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1030 | |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 1031 | OS << "#define __ai static __attribute__((__always_inline__))\n\n"; |
| 1032 | |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 1033 | std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst"); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1034 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 1035 | // Unique the return+pattern types, and assign them. |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 1036 | for (unsigned i = 0, e = RV.size(); i != e; ++i) { |
| 1037 | Record *R = RV[i]; |
Bob Wilson | ce0bb54 | 2010-12-03 17:19:39 +0000 | [diff] [blame] | 1038 | std::string name = R->getValueAsString("Name"); |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 1039 | std::string Proto = R->getValueAsString("Prototype"); |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 1040 | std::string Types = R->getValueAsString("Types"); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1041 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 1042 | SmallVector<StringRef, 16> TypeVec; |
| 1043 | ParseTypes(R, Types, TypeVec); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1044 | |
Bob Wilson | 3890e39 | 2010-12-07 01:12:23 +0000 | [diff] [blame] | 1045 | OpKind kind = OpMap[R->getValueAsDef("Operand")->getName()]; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1046 | |
Bob Wilson | 3890e39 | 2010-12-07 01:12:23 +0000 | [diff] [blame] | 1047 | ClassKind classKind = ClassNone; |
| 1048 | if (R->getSuperClasses().size() >= 2) |
| 1049 | classKind = ClassMap[R->getSuperClasses()[1]]; |
| 1050 | if (classKind == ClassNone && kind == OpNone) |
| 1051 | throw TGError(R->getLoc(), "Builtin has no class kind"); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1052 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 1053 | for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) { |
Bob Wilson | 3890e39 | 2010-12-07 01:12:23 +0000 | [diff] [blame] | 1054 | if (kind == OpReinterpret) { |
| 1055 | bool outQuad = false; |
| 1056 | bool dummy = false; |
| 1057 | (void)ClassifyType(TypeVec[ti], outQuad, dummy, dummy); |
| 1058 | for (unsigned srcti = 0, srcte = TypeVec.size(); |
| 1059 | srcti != srcte; ++srcti) { |
| 1060 | bool inQuad = false; |
| 1061 | (void)ClassifyType(TypeVec[srcti], inQuad, dummy, dummy); |
| 1062 | if (srcti == ti || inQuad != outQuad) |
| 1063 | continue; |
| 1064 | OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[srcti], |
| 1065 | OpCast, ClassS); |
| 1066 | } |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 1067 | } else { |
Bob Wilson | 3890e39 | 2010-12-07 01:12:23 +0000 | [diff] [blame] | 1068 | OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[ti], |
| 1069 | kind, classKind); |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 1070 | } |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 1071 | } |
| 1072 | OS << "\n"; |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 1073 | } |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1074 | OS << "#undef __ai\n\n"; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 1075 | OS << "#endif /* __ARM_NEON_H */\n"; |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 1076 | } |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 1077 | |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1078 | static unsigned RangeFromType(StringRef typestr) { |
| 1079 | // base type to get the type string for. |
| 1080 | bool quad = false, dummy = false; |
| 1081 | char type = ClassifyType(typestr, quad, dummy, dummy); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1082 | |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1083 | switch (type) { |
| 1084 | case 'c': |
Nate Begeman | 4da883a | 2010-06-15 22:10:31 +0000 | [diff] [blame] | 1085 | return (8 << (int)quad) - 1; |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1086 | case 'h': |
| 1087 | case 's': |
Nate Begeman | 4da883a | 2010-06-15 22:10:31 +0000 | [diff] [blame] | 1088 | return (4 << (int)quad) - 1; |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1089 | case 'f': |
| 1090 | case 'i': |
Nate Begeman | 4da883a | 2010-06-15 22:10:31 +0000 | [diff] [blame] | 1091 | return (2 << (int)quad) - 1; |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1092 | case 'l': |
Nate Begeman | 4da883a | 2010-06-15 22:10:31 +0000 | [diff] [blame] | 1093 | return (1 << (int)quad) - 1; |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1094 | default: |
| 1095 | throw "unhandled type!"; |
| 1096 | break; |
| 1097 | } |
Bob Wilson | fdb530d | 2010-07-28 18:21:10 +0000 | [diff] [blame] | 1098 | assert(0 && "unreachable"); |
| 1099 | return 0; |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1102 | /// runHeader - Emit a file with sections defining: |
| 1103 | /// 1. the NEON section of BuiltinsARM.def. |
| 1104 | /// 2. the SemaChecking code for the type overload checking. |
| 1105 | /// 3. the SemaChecking code for validation of intrinsic immedate arguments. |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 1106 | void NeonEmitter::runHeader(raw_ostream &OS) { |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1107 | std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst"); |
| 1108 | |
| 1109 | StringMap<OpKind> EmittedMap; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1110 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1111 | // Generate BuiltinsARM.def for NEON |
| 1112 | OS << "#ifdef GET_NEON_BUILTINS\n"; |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1113 | for (unsigned i = 0, e = RV.size(); i != e; ++i) { |
| 1114 | Record *R = RV[i]; |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1115 | OpKind k = OpMap[R->getValueAsDef("Operand")->getName()]; |
| 1116 | if (k != OpNone) |
| 1117 | continue; |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1118 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1119 | std::string Proto = R->getValueAsString("Prototype"); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1120 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 1121 | // Functions with 'a' (the splat code) in the type prototype should not get |
| 1122 | // their own builtin as they use the non-splat variant. |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 1123 | if (Proto.find('a') != std::string::npos) |
| 1124 | continue; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1125 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1126 | std::string Types = R->getValueAsString("Types"); |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1127 | SmallVector<StringRef, 16> TypeVec; |
| 1128 | ParseTypes(R, Types, TypeVec); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1129 | |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1130 | if (R->getSuperClasses().size() < 2) |
| 1131 | throw TGError(R->getLoc(), "Builtin has no class kind"); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1132 | |
Bob Wilson | ce0bb54 | 2010-12-03 17:19:39 +0000 | [diff] [blame] | 1133 | std::string name = R->getValueAsString("Name"); |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1134 | ClassKind ck = ClassMap[R->getSuperClasses()[1]]; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1135 | |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1136 | for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) { |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 1137 | // Generate the BuiltinsARM.def declaration for this builtin, ensuring |
| 1138 | // that each unique BUILTIN() macro appears only once in the output |
| 1139 | // stream. |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1140 | std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck); |
| 1141 | if (EmittedMap.count(bd)) |
| 1142 | continue; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1143 | |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1144 | EmittedMap[bd] = OpNone; |
| 1145 | OS << bd << "\n"; |
| 1146 | } |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1147 | } |
| 1148 | OS << "#endif\n\n"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1149 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1150 | // Generate the overloaded type checking code for SemaChecking.cpp |
| 1151 | OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n"; |
| 1152 | for (unsigned i = 0, e = RV.size(); i != e; ++i) { |
| 1153 | Record *R = RV[i]; |
| 1154 | OpKind k = OpMap[R->getValueAsDef("Operand")->getName()]; |
| 1155 | if (k != OpNone) |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 1156 | continue; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1157 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1158 | std::string Proto = R->getValueAsString("Prototype"); |
| 1159 | std::string Types = R->getValueAsString("Types"); |
Bob Wilson | ce0bb54 | 2010-12-03 17:19:39 +0000 | [diff] [blame] | 1160 | std::string name = R->getValueAsString("Name"); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1161 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1162 | // Functions with 'a' (the splat code) in the type prototype should not get |
| 1163 | // their own builtin as they use the non-splat variant. |
| 1164 | if (Proto.find('a') != std::string::npos) |
| 1165 | continue; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1166 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1167 | // Functions which have a scalar argument cannot be overloaded, no need to |
| 1168 | // check them if we are emitting the type checking code. |
| 1169 | if (Proto.find('s') != std::string::npos) |
| 1170 | continue; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1171 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1172 | SmallVector<StringRef, 16> TypeVec; |
| 1173 | ParseTypes(R, Types, TypeVec); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1174 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1175 | if (R->getSuperClasses().size() < 2) |
| 1176 | throw TGError(R->getLoc(), "Builtin has no class kind"); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1177 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1178 | int si = -1, qi = -1; |
| 1179 | unsigned mask = 0, qmask = 0; |
| 1180 | for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) { |
| 1181 | // Generate the switch case(s) for this builtin for the type validation. |
| 1182 | bool quad = false, poly = false, usgn = false; |
| 1183 | (void) ClassifyType(TypeVec[ti], quad, poly, usgn); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1184 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1185 | if (quad) { |
| 1186 | qi = ti; |
| 1187 | qmask |= 1 << GetNeonEnum(Proto, TypeVec[ti]); |
| 1188 | } else { |
| 1189 | si = ti; |
| 1190 | mask |= 1 << GetNeonEnum(Proto, TypeVec[ti]); |
| 1191 | } |
| 1192 | } |
| 1193 | if (mask) |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1194 | OS << "case ARM::BI__builtin_neon_" |
Bob Wilson | d8b8470 | 2010-12-07 01:12:19 +0000 | [diff] [blame] | 1195 | << MangleName(name, TypeVec[si], ClassB) |
| 1196 | << ": mask = " << "0x" << utohexstr(mask) << "; break;\n"; |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1197 | if (qmask) |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1198 | OS << "case ARM::BI__builtin_neon_" |
Bob Wilson | d8b8470 | 2010-12-07 01:12:19 +0000 | [diff] [blame] | 1199 | << MangleName(name, TypeVec[qi], ClassB) |
| 1200 | << ": mask = " << "0x" << utohexstr(qmask) << "; break;\n"; |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1201 | } |
| 1202 | OS << "#endif\n\n"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1203 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1204 | // Generate the intrinsic range checking code for shift/lane immediates. |
| 1205 | OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n"; |
| 1206 | for (unsigned i = 0, e = RV.size(); i != e; ++i) { |
| 1207 | Record *R = RV[i]; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1208 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1209 | OpKind k = OpMap[R->getValueAsDef("Operand")->getName()]; |
| 1210 | if (k != OpNone) |
| 1211 | continue; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1212 | |
Bob Wilson | ce0bb54 | 2010-12-03 17:19:39 +0000 | [diff] [blame] | 1213 | std::string name = R->getValueAsString("Name"); |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1214 | std::string Proto = R->getValueAsString("Prototype"); |
| 1215 | std::string Types = R->getValueAsString("Types"); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1216 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1217 | // Functions with 'a' (the splat code) in the type prototype should not get |
| 1218 | // their own builtin as they use the non-splat variant. |
| 1219 | if (Proto.find('a') != std::string::npos) |
| 1220 | continue; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1221 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1222 | // Functions which do not have an immediate do not need to have range |
| 1223 | // checking code emitted. |
| 1224 | if (Proto.find('i') == std::string::npos) |
| 1225 | continue; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1226 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1227 | SmallVector<StringRef, 16> TypeVec; |
| 1228 | ParseTypes(R, Types, TypeVec); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1229 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1230 | if (R->getSuperClasses().size() < 2) |
| 1231 | throw TGError(R->getLoc(), "Builtin has no class kind"); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1232 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1233 | ClassKind ck = ClassMap[R->getSuperClasses()[1]]; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1234 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1235 | for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) { |
| 1236 | std::string namestr, shiftstr, rangestr; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1237 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1238 | // Builtins which are overloaded by type will need to have their upper |
| 1239 | // bound computed at Sema time based on the type constant. |
| 1240 | if (Proto.find('s') == std::string::npos) { |
| 1241 | ck = ClassB; |
| 1242 | if (R->getValueAsBit("isShift")) { |
| 1243 | shiftstr = ", true"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1244 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1245 | // Right shifts have an 'r' in the name, left shifts do not. |
| 1246 | if (name.find('r') != std::string::npos) |
| 1247 | rangestr = "l = 1; "; |
| 1248 | } |
| 1249 | rangestr += "u = RFT(TV" + shiftstr + ")"; |
| 1250 | } else { |
| 1251 | rangestr = "u = " + utostr(RangeFromType(TypeVec[ti])); |
| 1252 | } |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 1253 | // 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] | 1254 | namestr = MangleName(name, TypeVec[ti], ck); |
| 1255 | if (EmittedMap.count(namestr)) |
| 1256 | continue; |
| 1257 | EmittedMap[namestr] = OpNone; |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 1258 | |
| 1259 | // Calculate the index of the immediate that should be range checked. |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1260 | unsigned immidx = 0; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1261 | |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 1262 | // Builtins that return a struct of multiple vectors have an extra |
| 1263 | // leading arg for the struct return. |
Bob Wilson | 5b7fe59 | 2010-12-01 19:49:51 +0000 | [diff] [blame] | 1264 | if (Proto[0] >= '2' && Proto[0] <= '4') |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 1265 | ++immidx; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1266 | |
| 1267 | // Add one to the index for each argument until we reach the immediate |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 1268 | // to be checked. Structs of vectors are passed as multiple arguments. |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1269 | for (unsigned ii = 1, ie = Proto.size(); ii != ie; ++ii) { |
| 1270 | switch (Proto[ii]) { |
| 1271 | default: immidx += 1; break; |
| 1272 | case '2': immidx += 2; break; |
| 1273 | case '3': immidx += 3; break; |
| 1274 | case '4': immidx += 4; break; |
| 1275 | case 'i': ie = ii + 1; break; |
| 1276 | } |
| 1277 | } |
Bob Wilson | d8b8470 | 2010-12-07 01:12:19 +0000 | [diff] [blame] | 1278 | OS << "case ARM::BI__builtin_neon_" << MangleName(name, TypeVec[ti], ck) |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1279 | << ": i = " << immidx << "; " << rangestr << "; break;\n"; |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 1280 | } |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1281 | } |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1282 | OS << "#endif\n\n"; |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 1283 | } |