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 |
Bob Wilson | 333f519 | 2010-12-15 16:58:45 +0000 | [diff] [blame] | 21 | // called, rather than the normal run() entry point. A complete set of tests |
| 22 | // for Neon intrinsics can be generated by calling the runTests() entry point. |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 23 | // |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | #include "NeonEmitter.h" |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallString.h" |
| 28 | #include "llvm/ADT/SmallVector.h" |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/StringExtras.h" |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 30 | #include <string> |
| 31 | |
| 32 | using namespace llvm; |
| 33 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 34 | /// ParseTypes - break down a string such as "fQf" into a vector of StringRefs, |
| 35 | /// which each StringRef representing a single type declared in the string. |
| 36 | /// for "fQf" we would end up with 2 StringRefs, "f", and "Qf", representing |
| 37 | /// 2xfloat and 4xfloat respectively. |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 38 | static void ParseTypes(Record *r, std::string &s, |
| 39 | SmallVectorImpl<StringRef> &TV) { |
| 40 | const char *data = s.data(); |
| 41 | int len = 0; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 42 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 43 | for (unsigned i = 0, e = s.size(); i != e; ++i, ++len) { |
| 44 | if (data[len] == 'P' || data[len] == 'Q' || data[len] == 'U') |
| 45 | continue; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 46 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 47 | switch (data[len]) { |
| 48 | case 'c': |
| 49 | case 's': |
| 50 | case 'i': |
| 51 | case 'l': |
| 52 | case 'h': |
| 53 | case 'f': |
| 54 | break; |
| 55 | default: |
| 56 | throw TGError(r->getLoc(), |
| 57 | "Unexpected letter: " + std::string(data + len, 1)); |
| 58 | break; |
| 59 | } |
| 60 | TV.push_back(StringRef(data, len + 1)); |
| 61 | data += len + 1; |
| 62 | len = -1; |
| 63 | } |
| 64 | } |
| 65 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 66 | /// Widen - Convert a type code into the next wider type. char -> short, |
| 67 | /// short -> int, etc. |
Duncan Sands | 8dbbace | 2010-06-02 08:37:30 +0000 | [diff] [blame] | 68 | static char Widen(const char t) { |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 69 | switch (t) { |
| 70 | case 'c': |
| 71 | return 's'; |
| 72 | case 's': |
| 73 | return 'i'; |
| 74 | case 'i': |
| 75 | return 'l'; |
| 76 | default: throw "unhandled type in widen!"; |
| 77 | } |
| 78 | return '\0'; |
| 79 | } |
| 80 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 81 | /// Narrow - Convert a type code into the next smaller type. short -> char, |
| 82 | /// float -> half float, etc. |
Nate Begeman | 3861e74 | 2010-06-03 21:35:22 +0000 | [diff] [blame] | 83 | static char Narrow(const char t) { |
| 84 | switch (t) { |
| 85 | case 's': |
| 86 | return 'c'; |
| 87 | case 'i': |
| 88 | return 's'; |
| 89 | case 'l': |
| 90 | return 'i'; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 91 | case 'f': |
| 92 | return 'h'; |
Bob Wilson | b055f74 | 2010-11-23 19:38:34 +0000 | [diff] [blame] | 93 | default: throw "unhandled type in narrow!"; |
Nate Begeman | 3861e74 | 2010-06-03 21:35:22 +0000 | [diff] [blame] | 94 | } |
| 95 | return '\0'; |
| 96 | } |
| 97 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 98 | /// For a particular StringRef, return the base type code, and whether it has |
| 99 | /// the quad-vector, polynomial, or unsigned modifiers set. |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 100 | static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) { |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 101 | unsigned off = 0; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 102 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 103 | // remember quad. |
| 104 | if (ty[off] == 'Q') { |
| 105 | quad = true; |
| 106 | ++off; |
| 107 | } |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 108 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 109 | // remember poly. |
| 110 | if (ty[off] == 'P') { |
| 111 | poly = true; |
| 112 | ++off; |
| 113 | } |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 114 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 115 | // remember unsigned. |
| 116 | if (ty[off] == 'U') { |
| 117 | usgn = true; |
| 118 | ++off; |
| 119 | } |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 120 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 121 | // base type to get the type string for. |
| 122 | return ty[off]; |
| 123 | } |
| 124 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 125 | /// ModType - Transform a type code and its modifiers based on a mod code. The |
| 126 | /// mod code definitions may be found at the top of arm_neon.td. |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 127 | static char ModType(const char mod, char type, bool &quad, bool &poly, |
| 128 | bool &usgn, bool &scal, bool &cnst, bool &pntr) { |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 129 | switch (mod) { |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 130 | case 't': |
| 131 | if (poly) { |
| 132 | poly = false; |
| 133 | usgn = true; |
| 134 | } |
| 135 | break; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 136 | case 'u': |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 137 | usgn = true; |
Bob Wilson | 181b76d | 2010-11-18 21:43:22 +0000 | [diff] [blame] | 138 | poly = false; |
| 139 | if (type == 'f') |
| 140 | type = 'i'; |
| 141 | break; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 142 | case 'x': |
Bob Wilson | 181b76d | 2010-11-18 21:43:22 +0000 | [diff] [blame] | 143 | usgn = false; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 144 | poly = false; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 145 | if (type == 'f') |
| 146 | type = 'i'; |
| 147 | break; |
| 148 | case 'f': |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 149 | if (type == 'h') |
| 150 | quad = true; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 151 | type = 'f'; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 152 | usgn = false; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 153 | break; |
Nate Begeman | 59d70cb | 2010-08-06 01:24:11 +0000 | [diff] [blame] | 154 | case 'g': |
| 155 | quad = false; |
| 156 | break; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 157 | case 'w': |
| 158 | type = Widen(type); |
| 159 | quad = true; |
| 160 | break; |
| 161 | case 'n': |
| 162 | type = Widen(type); |
| 163 | break; |
Bob Wilson | f00140c | 2010-12-01 19:49:58 +0000 | [diff] [blame] | 164 | case 'i': |
| 165 | type = 'i'; |
| 166 | scal = true; |
| 167 | break; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 168 | case 'l': |
| 169 | type = 'l'; |
| 170 | scal = true; |
| 171 | usgn = true; |
| 172 | break; |
| 173 | case 's': |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 174 | case 'a': |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 175 | scal = true; |
| 176 | break; |
| 177 | case 'k': |
| 178 | quad = true; |
| 179 | break; |
| 180 | case 'c': |
| 181 | cnst = true; |
| 182 | case 'p': |
| 183 | pntr = true; |
| 184 | scal = true; |
| 185 | break; |
Nate Begeman | 3861e74 | 2010-06-03 21:35:22 +0000 | [diff] [blame] | 186 | case 'h': |
| 187 | type = Narrow(type); |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 188 | if (type == 'h') |
| 189 | quad = false; |
Nate Begeman | 3861e74 | 2010-06-03 21:35:22 +0000 | [diff] [blame] | 190 | break; |
| 191 | case 'e': |
| 192 | type = Narrow(type); |
| 193 | usgn = true; |
| 194 | break; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 195 | default: |
| 196 | break; |
| 197 | } |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 198 | return type; |
| 199 | } |
| 200 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 201 | /// 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] | 202 | /// that type. QUc -> uint8x8_t. |
| 203 | static std::string TypeString(const char mod, StringRef typestr) { |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 204 | bool quad = false; |
| 205 | bool poly = false; |
| 206 | bool usgn = false; |
| 207 | bool scal = false; |
| 208 | bool cnst = false; |
| 209 | bool pntr = false; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 210 | |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 211 | if (mod == 'v') |
| 212 | return "void"; |
| 213 | if (mod == 'i') |
| 214 | return "int"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 215 | |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 216 | // base type to get the type string for. |
| 217 | char type = ClassifyType(typestr, quad, poly, usgn); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 218 | |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 219 | // Based on the modifying character, change the type and width if necessary. |
| 220 | type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 221 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 222 | SmallString<128> s; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 223 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 224 | if (usgn) |
| 225 | s.push_back('u'); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 226 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 227 | switch (type) { |
| 228 | case 'c': |
| 229 | s += poly ? "poly8" : "int8"; |
| 230 | if (scal) |
| 231 | break; |
| 232 | s += quad ? "x16" : "x8"; |
| 233 | break; |
| 234 | case 's': |
| 235 | s += poly ? "poly16" : "int16"; |
| 236 | if (scal) |
| 237 | break; |
| 238 | s += quad ? "x8" : "x4"; |
| 239 | break; |
| 240 | case 'i': |
| 241 | s += "int32"; |
| 242 | if (scal) |
| 243 | break; |
| 244 | s += quad ? "x4" : "x2"; |
| 245 | break; |
| 246 | case 'l': |
| 247 | s += "int64"; |
| 248 | if (scal) |
| 249 | break; |
| 250 | s += quad ? "x2" : "x1"; |
| 251 | break; |
| 252 | case 'h': |
| 253 | s += "float16"; |
| 254 | if (scal) |
| 255 | break; |
| 256 | s += quad ? "x8" : "x4"; |
| 257 | break; |
| 258 | case 'f': |
| 259 | s += "float32"; |
| 260 | if (scal) |
| 261 | break; |
| 262 | s += quad ? "x4" : "x2"; |
| 263 | break; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 264 | default: |
| 265 | throw "unhandled type!"; |
| 266 | break; |
| 267 | } |
| 268 | |
| 269 | if (mod == '2') |
| 270 | s += "x2"; |
| 271 | if (mod == '3') |
| 272 | s += "x3"; |
| 273 | if (mod == '4') |
| 274 | s += "x4"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 275 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 276 | // Append _t, finishing the type string typedef type. |
| 277 | s += "_t"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 278 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 279 | if (cnst) |
| 280 | s += " const"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 281 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 282 | if (pntr) |
| 283 | s += " *"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 284 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 285 | return s.str(); |
| 286 | } |
| 287 | |
Bob Wilson | 1ac27cf | 2010-06-24 22:04:30 +0000 | [diff] [blame] | 288 | /// BuiltinTypeString - for a modifier and type, generate the clang |
| 289 | /// BuiltinsARM.def prototype code for the function. See the top of clang's |
| 290 | /// Builtins.def for a description of the type strings. |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 291 | static std::string BuiltinTypeString(const char mod, StringRef typestr, |
| 292 | ClassKind ck, bool ret) { |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 293 | bool quad = false; |
| 294 | bool poly = false; |
| 295 | bool usgn = false; |
| 296 | bool scal = false; |
| 297 | bool cnst = false; |
| 298 | bool pntr = false; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 299 | |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 300 | if (mod == 'v') |
Bob Wilson | 95148ad | 2010-12-01 19:49:56 +0000 | [diff] [blame] | 301 | return "v"; // void |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 302 | if (mod == 'i') |
Bob Wilson | 95148ad | 2010-12-01 19:49:56 +0000 | [diff] [blame] | 303 | return "i"; // int |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 304 | |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 305 | // base type to get the type string for. |
| 306 | char type = ClassifyType(typestr, quad, poly, usgn); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 307 | |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 308 | // Based on the modifying character, change the type and width if necessary. |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 309 | type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr); |
| 310 | |
Bob Wilson | 95148ad | 2010-12-01 19:49:56 +0000 | [diff] [blame] | 311 | // All pointers are void* pointers. Change type to 'v' now. |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 312 | if (pntr) { |
| 313 | usgn = false; |
| 314 | poly = false; |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 315 | type = 'v'; |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 316 | } |
Bob Wilson | 95148ad | 2010-12-01 19:49:56 +0000 | [diff] [blame] | 317 | // Treat half-float ('h') types as unsigned short ('s') types. |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 318 | if (type == 'h') { |
| 319 | type = 's'; |
| 320 | usgn = true; |
| 321 | } |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 322 | usgn = usgn | poly | ((ck == ClassI || ck == ClassW) && scal && type != 'f'); |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 323 | |
| 324 | if (scal) { |
| 325 | SmallString<128> s; |
| 326 | |
| 327 | if (usgn) |
| 328 | s.push_back('U'); |
Bob Wilson | 317bafb | 2010-12-02 00:24:59 +0000 | [diff] [blame] | 329 | else if (type == 'c') |
| 330 | s.push_back('S'); // make chars explicitly signed |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 331 | |
Bob Wilson | 95148ad | 2010-12-01 19:49:56 +0000 | [diff] [blame] | 332 | if (type == 'l') // 64-bit long |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 333 | s += "LLi"; |
| 334 | else |
| 335 | s.push_back(type); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 336 | |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 337 | if (cnst) |
| 338 | s.push_back('C'); |
| 339 | if (pntr) |
| 340 | s.push_back('*'); |
| 341 | return s.str(); |
| 342 | } |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 343 | |
| 344 | // 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] | 345 | // appropriate width which we will bitcast. An exception is made for |
| 346 | // returning structs of 2, 3, or 4 vectors which are returned in a sret-like |
| 347 | // fashion, storing them to a pointer arg. |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 348 | if (ret) { |
Bob Wilson | 5b7fe59 | 2010-12-01 19:49:51 +0000 | [diff] [blame] | 349 | if (mod >= '2' && mod <= '4') |
Bob Wilson | 95148ad | 2010-12-01 19:49:56 +0000 | [diff] [blame] | 350 | return "vv*"; // void result with void* first argument |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 351 | if (mod == 'f' || (ck != ClassB && type == 'f')) |
Nate Begeman | 5638783 | 2010-06-08 06:01:16 +0000 | [diff] [blame] | 352 | return quad ? "V4f" : "V2f"; |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 353 | if (ck != ClassB && type == 's') |
Nate Begeman | 007afe4 | 2010-06-09 05:11:55 +0000 | [diff] [blame] | 354 | return quad ? "V8s" : "V4s"; |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 355 | if (ck != ClassB && type == 'i') |
Nate Begeman | 5638783 | 2010-06-08 06:01:16 +0000 | [diff] [blame] | 356 | return quad ? "V4i" : "V2i"; |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 357 | if (ck != ClassB && type == 'l') |
Nate Begeman | 007afe4 | 2010-06-09 05:11:55 +0000 | [diff] [blame] | 358 | return quad ? "V2LLi" : "V1LLi"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 359 | |
Bob Wilson | 317bafb | 2010-12-02 00:24:59 +0000 | [diff] [blame] | 360 | return quad ? "V16Sc" : "V8Sc"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 361 | } |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 362 | |
| 363 | // Non-return array types are passed as individual vectors. |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 364 | if (mod == '2') |
Bob Wilson | 317bafb | 2010-12-02 00:24:59 +0000 | [diff] [blame] | 365 | return quad ? "V16ScV16Sc" : "V8ScV8Sc"; |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 366 | if (mod == '3') |
Bob Wilson | 317bafb | 2010-12-02 00:24:59 +0000 | [diff] [blame] | 367 | return quad ? "V16ScV16ScV16Sc" : "V8ScV8ScV8Sc"; |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 368 | if (mod == '4') |
Bob Wilson | 317bafb | 2010-12-02 00:24:59 +0000 | [diff] [blame] | 369 | return quad ? "V16ScV16ScV16ScV16Sc" : "V8ScV8ScV8ScV8Sc"; |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 370 | |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 371 | if (mod == 'f' || (ck != ClassB && type == 'f')) |
Nate Begeman | 007afe4 | 2010-06-09 05:11:55 +0000 | [diff] [blame] | 372 | return quad ? "V4f" : "V2f"; |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 373 | if (ck != ClassB && type == 's') |
Nate Begeman | 007afe4 | 2010-06-09 05:11:55 +0000 | [diff] [blame] | 374 | return quad ? "V8s" : "V4s"; |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 375 | if (ck != ClassB && type == 'i') |
Nate Begeman | 007afe4 | 2010-06-09 05:11:55 +0000 | [diff] [blame] | 376 | return quad ? "V4i" : "V2i"; |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 377 | if (ck != ClassB && type == 'l') |
Nate Begeman | 007afe4 | 2010-06-09 05:11:55 +0000 | [diff] [blame] | 378 | return quad ? "V2LLi" : "V1LLi"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 379 | |
Bob Wilson | 317bafb | 2010-12-02 00:24:59 +0000 | [diff] [blame] | 380 | return quad ? "V16Sc" : "V8Sc"; |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 383 | /// 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] | 384 | /// and insert a 'q' in the appropriate location if the operation works on |
| 385 | /// 128b rather than 64b. E.g. turn "vst2_lane" into "vst2q_lane_f32", etc. |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 386 | static std::string MangleName(const std::string &name, StringRef typestr, |
| 387 | ClassKind ck) { |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 388 | if (name == "vcvt_f32_f16") |
| 389 | return name; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 390 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 391 | bool quad = false; |
| 392 | bool poly = false; |
| 393 | bool usgn = false; |
| 394 | char type = ClassifyType(typestr, quad, poly, usgn); |
| 395 | |
| 396 | std::string s = name; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 397 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 398 | switch (type) { |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 399 | case 'c': |
| 400 | switch (ck) { |
| 401 | case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break; |
| 402 | case ClassI: s += "_i8"; break; |
| 403 | case ClassW: s += "_8"; break; |
| 404 | default: break; |
| 405 | } |
| 406 | break; |
| 407 | case 's': |
| 408 | switch (ck) { |
| 409 | case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break; |
| 410 | case ClassI: s += "_i16"; break; |
| 411 | case ClassW: s += "_16"; break; |
| 412 | default: break; |
| 413 | } |
| 414 | break; |
| 415 | case 'i': |
| 416 | switch (ck) { |
| 417 | case ClassS: s += usgn ? "_u32" : "_s32"; break; |
| 418 | case ClassI: s += "_i32"; break; |
| 419 | case ClassW: s += "_32"; break; |
| 420 | default: break; |
| 421 | } |
| 422 | break; |
| 423 | case 'l': |
| 424 | switch (ck) { |
| 425 | case ClassS: s += usgn ? "_u64" : "_s64"; break; |
| 426 | case ClassI: s += "_i64"; break; |
| 427 | case ClassW: s += "_64"; break; |
| 428 | default: break; |
| 429 | } |
| 430 | break; |
| 431 | case 'h': |
| 432 | switch (ck) { |
| 433 | case ClassS: |
| 434 | case ClassI: s += "_f16"; break; |
| 435 | case ClassW: s += "_16"; break; |
| 436 | default: break; |
| 437 | } |
| 438 | break; |
| 439 | case 'f': |
| 440 | switch (ck) { |
| 441 | case ClassS: |
| 442 | case ClassI: s += "_f32"; break; |
| 443 | case ClassW: s += "_32"; break; |
| 444 | default: break; |
| 445 | } |
| 446 | break; |
| 447 | default: |
| 448 | throw "unhandled type!"; |
| 449 | break; |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 450 | } |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 451 | if (ck == ClassB) |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 452 | s += "_v"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 453 | |
| 454 | // 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] | 455 | // _lane or _n on vector-scalar operations. |
| 456 | if (quad) { |
| 457 | size_t pos = s.find('_'); |
| 458 | s = s.insert(pos, "q"); |
| 459 | } |
| 460 | return s; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 463 | // Generate the string "(argtype a, argtype b, ...)" |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 464 | static std::string GenArgs(const std::string &proto, StringRef typestr) { |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 465 | bool define = proto.find('i') != std::string::npos; |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 466 | char arg = 'a'; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 467 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 468 | std::string s; |
| 469 | s += "("; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 470 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 471 | for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) { |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 472 | if (define) { |
| 473 | // Immediate macro arguments are used directly instead of being assigned |
| 474 | // to local temporaries; prepend an underscore prefix to make their |
| 475 | // names consistent with the local temporaries. |
| 476 | if (proto[i] == 'i') |
| 477 | s += "__"; |
| 478 | } else { |
| 479 | s += TypeString(proto[i], typestr) + " __"; |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 480 | } |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 481 | s.push_back(arg); |
| 482 | if ((i + 1) < e) |
| 483 | s += ", "; |
| 484 | } |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 485 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 486 | s += ")"; |
| 487 | return s; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 490 | // Macro arguments are not type-checked like inline function arguments, so |
| 491 | // assign them to local temporaries to get the right type checking. |
Bob Wilson | 377296e | 2010-12-02 07:10:39 +0000 | [diff] [blame] | 492 | static std::string GenMacroLocals(const std::string &proto, StringRef typestr) { |
| 493 | char arg = 'a'; |
| 494 | std::string s; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 495 | |
Bob Wilson | 377296e | 2010-12-02 07:10:39 +0000 | [diff] [blame] | 496 | for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) { |
| 497 | // Do not create a temporary for an immediate argument. |
| 498 | // That would defeat the whole point of using a macro! |
| 499 | if (proto[i] == 'i') continue; |
| 500 | |
| 501 | s += TypeString(proto[i], typestr) + " __"; |
| 502 | s.push_back(arg); |
| 503 | s += " = ("; |
| 504 | s.push_back(arg); |
| 505 | s += "); "; |
| 506 | } |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 507 | |
Bob Wilson | 377296e | 2010-12-02 07:10:39 +0000 | [diff] [blame] | 508 | s += "\\\n "; |
| 509 | return s; |
| 510 | } |
| 511 | |
Bob Wilson | c4ba09d | 2010-12-07 20:02:45 +0000 | [diff] [blame] | 512 | // Use the vmovl builtin to sign-extend or zero-extend a vector. |
Bob Wilson | b1e9df3 | 2010-12-08 21:39:00 +0000 | [diff] [blame] | 513 | static std::string Extend(StringRef typestr, const std::string &a) { |
Bob Wilson | c4ba09d | 2010-12-07 20:02:45 +0000 | [diff] [blame] | 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; |
Bob Wilson | e113ae5 | 2010-12-08 00:14:04 +0000 | [diff] [blame] | 587 | case OpAddl: |
Bob Wilson | b1e9df3 | 2010-12-08 21:39:00 +0000 | [diff] [blame] | 588 | s += Extend(typestr, "__a") + " + " + Extend(typestr, "__b") + ";"; |
Bob Wilson | e113ae5 | 2010-12-08 00:14:04 +0000 | [diff] [blame] | 589 | break; |
| 590 | case OpAddw: |
Bob Wilson | b1e9df3 | 2010-12-08 21:39:00 +0000 | [diff] [blame] | 591 | s += "__a + " + Extend(typestr, "__b") + ";"; |
Bob Wilson | e113ae5 | 2010-12-08 00:14:04 +0000 | [diff] [blame] | 592 | break; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 593 | case OpSub: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 594 | s += "__a - __b;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 595 | break; |
Bob Wilson | e113ae5 | 2010-12-08 00:14:04 +0000 | [diff] [blame] | 596 | case OpSubl: |
Bob Wilson | b1e9df3 | 2010-12-08 21:39:00 +0000 | [diff] [blame] | 597 | s += Extend(typestr, "__a") + " - " + Extend(typestr, "__b") + ";"; |
Bob Wilson | e113ae5 | 2010-12-08 00:14:04 +0000 | [diff] [blame] | 598 | break; |
| 599 | case OpSubw: |
Bob Wilson | b1e9df3 | 2010-12-08 21:39:00 +0000 | [diff] [blame] | 600 | s += "__a - " + Extend(typestr, "__b") + ";"; |
Bob Wilson | e113ae5 | 2010-12-08 00:14:04 +0000 | [diff] [blame] | 601 | break; |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 602 | case OpMulN: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 603 | s += "__a * " + Duplicate(nElts, typestr, "__b") + ";"; |
Bob Wilson | f18dfec | 2010-11-16 19:16:06 +0000 | [diff] [blame] | 604 | break; |
Bob Wilson | b0d9869 | 2010-12-03 00:34:12 +0000 | [diff] [blame] | 605 | case OpMulLane: |
| 606 | s += "__a * " + SplatLane(nElts, "__b", "__c") + ";"; |
| 607 | break; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 608 | case OpMul: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 609 | s += "__a * __b;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 610 | break; |
Bob Wilson | c4ba09d | 2010-12-07 20:02:45 +0000 | [diff] [blame] | 611 | case OpMullN: |
Bob Wilson | b1e9df3 | 2010-12-08 21:39:00 +0000 | [diff] [blame] | 612 | s += Extend(typestr, "__a") + " * " + |
| 613 | Extend(typestr, Duplicate(nElts << (int)quad, typestr, "__b")) + ";"; |
Bob Wilson | c4ba09d | 2010-12-07 20:02:45 +0000 | [diff] [blame] | 614 | break; |
Bob Wilson | 3467cd0 | 2010-12-07 22:02:48 +0000 | [diff] [blame] | 615 | case OpMullLane: |
Bob Wilson | b1e9df3 | 2010-12-08 21:39:00 +0000 | [diff] [blame] | 616 | s += Extend(typestr, "__a") + " * " + |
| 617 | Extend(typestr, SplatLane(nElts, "__b", "__c")) + ";"; |
Bob Wilson | 3467cd0 | 2010-12-07 22:02:48 +0000 | [diff] [blame] | 618 | break; |
Bob Wilson | c4ba09d | 2010-12-07 20:02:45 +0000 | [diff] [blame] | 619 | case OpMull: |
Bob Wilson | b1e9df3 | 2010-12-08 21:39:00 +0000 | [diff] [blame] | 620 | s += Extend(typestr, "__a") + " * " + Extend(typestr, "__b") + ";"; |
Bob Wilson | c4ba09d | 2010-12-07 20:02:45 +0000 | [diff] [blame] | 621 | break; |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 622 | case OpMlaN: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 623 | s += "__a + (__b * " + Duplicate(nElts, typestr, "__c") + ");"; |
Bob Wilson | f18dfec | 2010-11-16 19:16:06 +0000 | [diff] [blame] | 624 | break; |
Bob Wilson | b0d9869 | 2010-12-03 00:34:12 +0000 | [diff] [blame] | 625 | case OpMlaLane: |
| 626 | s += "__a + (__b * " + SplatLane(nElts, "__c", "__d") + ");"; |
| 627 | break; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 628 | case OpMla: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 629 | s += "__a + (__b * __c);"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 630 | break; |
Bob Wilson | 0584316 | 2010-12-07 23:53:37 +0000 | [diff] [blame] | 631 | case OpMlalN: |
Bob Wilson | b1e9df3 | 2010-12-08 21:39:00 +0000 | [diff] [blame] | 632 | s += "__a + (" + Extend(typestr, "__b") + " * " + |
| 633 | Extend(typestr, Duplicate(nElts, typestr, "__c")) + ");"; |
Bob Wilson | 0584316 | 2010-12-07 23:53:37 +0000 | [diff] [blame] | 634 | break; |
| 635 | case OpMlalLane: |
Bob Wilson | b1e9df3 | 2010-12-08 21:39:00 +0000 | [diff] [blame] | 636 | s += "__a + (" + Extend(typestr, "__b") + " * " + |
| 637 | Extend(typestr, SplatLane(nElts, "__c", "__d")) + ");"; |
Bob Wilson | 0584316 | 2010-12-07 23:53:37 +0000 | [diff] [blame] | 638 | break; |
| 639 | case OpMlal: |
Bob Wilson | b1e9df3 | 2010-12-08 21:39:00 +0000 | [diff] [blame] | 640 | s += "__a + (" + Extend(typestr, "__b") + " * " + |
| 641 | Extend(typestr, "__c") + ");"; |
Bob Wilson | 0584316 | 2010-12-07 23:53:37 +0000 | [diff] [blame] | 642 | break; |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 643 | case OpMlsN: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 644 | s += "__a - (__b * " + Duplicate(nElts, typestr, "__c") + ");"; |
Bob Wilson | f18dfec | 2010-11-16 19:16:06 +0000 | [diff] [blame] | 645 | break; |
Bob Wilson | b0d9869 | 2010-12-03 00:34:12 +0000 | [diff] [blame] | 646 | case OpMlsLane: |
| 647 | s += "__a - (__b * " + SplatLane(nElts, "__c", "__d") + ");"; |
| 648 | break; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 649 | case OpMls: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 650 | s += "__a - (__b * __c);"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 651 | break; |
Bob Wilson | 0584316 | 2010-12-07 23:53:37 +0000 | [diff] [blame] | 652 | case OpMlslN: |
Bob Wilson | b1e9df3 | 2010-12-08 21:39:00 +0000 | [diff] [blame] | 653 | s += "__a - (" + Extend(typestr, "__b") + " * " + |
| 654 | Extend(typestr, Duplicate(nElts, typestr, "__c")) + ");"; |
Bob Wilson | 0584316 | 2010-12-07 23:53:37 +0000 | [diff] [blame] | 655 | break; |
| 656 | case OpMlslLane: |
Bob Wilson | b1e9df3 | 2010-12-08 21:39:00 +0000 | [diff] [blame] | 657 | s += "__a - (" + Extend(typestr, "__b") + " * " + |
| 658 | Extend(typestr, SplatLane(nElts, "__c", "__d")) + ");"; |
Bob Wilson | 0584316 | 2010-12-07 23:53:37 +0000 | [diff] [blame] | 659 | break; |
| 660 | case OpMlsl: |
Bob Wilson | b1e9df3 | 2010-12-08 21:39:00 +0000 | [diff] [blame] | 661 | s += "__a - (" + Extend(typestr, "__b") + " * " + |
| 662 | Extend(typestr, "__c") + ");"; |
Bob Wilson | 0584316 | 2010-12-07 23:53:37 +0000 | [diff] [blame] | 663 | break; |
Bob Wilson | 7441089 | 2010-12-08 22:36:08 +0000 | [diff] [blame] | 664 | case OpQDMullLane: |
| 665 | s += MangleName("vqdmull", typestr, ClassS) + "(__a, " + |
| 666 | SplatLane(nElts, "__b", "__c") + ");"; |
| 667 | break; |
| 668 | case OpQDMlalLane: |
Bob Wilson | c2ef828 | 2010-12-10 06:37:53 +0000 | [diff] [blame] | 669 | s += MangleName("vqdmlal", typestr, ClassS) + "(__a, __b, " + |
| 670 | SplatLane(nElts, "__c", "__d") + ");"; |
Bob Wilson | 7441089 | 2010-12-08 22:36:08 +0000 | [diff] [blame] | 671 | break; |
| 672 | case OpQDMlslLane: |
Bob Wilson | c2ef828 | 2010-12-10 06:37:53 +0000 | [diff] [blame] | 673 | s += MangleName("vqdmlsl", typestr, ClassS) + "(__a, __b, " + |
| 674 | SplatLane(nElts, "__c", "__d") + ");"; |
Bob Wilson | 7441089 | 2010-12-08 22:36:08 +0000 | [diff] [blame] | 675 | break; |
| 676 | case OpQDMulhLane: |
| 677 | s += MangleName("vqdmulh", typestr, ClassS) + "(__a, " + |
| 678 | SplatLane(nElts, "__b", "__c") + ");"; |
| 679 | break; |
| 680 | case OpQRDMulhLane: |
| 681 | s += MangleName("vqrdmulh", typestr, ClassS) + "(__a, " + |
| 682 | SplatLane(nElts, "__b", "__c") + ");"; |
| 683 | break; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 684 | case OpEq: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 685 | s += "(" + ts + ")(__a == __b);"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 686 | break; |
| 687 | case OpGe: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 688 | s += "(" + ts + ")(__a >= __b);"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 689 | break; |
| 690 | case OpLe: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 691 | s += "(" + ts + ")(__a <= __b);"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 692 | break; |
| 693 | case OpGt: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 694 | s += "(" + ts + ")(__a > __b);"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 695 | break; |
| 696 | case OpLt: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 697 | s += "(" + ts + ")(__a < __b);"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 698 | break; |
| 699 | case OpNeg: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 700 | s += " -__a;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 701 | break; |
| 702 | case OpNot: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 703 | s += " ~__a;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 704 | break; |
| 705 | case OpAnd: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 706 | s += "__a & __b;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 707 | break; |
| 708 | case OpOr: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 709 | s += "__a | __b;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 710 | break; |
| 711 | case OpXor: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 712 | s += "__a ^ __b;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 713 | break; |
| 714 | case OpAndNot: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 715 | s += "__a & ~__b;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 716 | break; |
| 717 | case OpOrNot: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 718 | s += "__a | ~__b;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 719 | break; |
Nate Begeman | 3861e74 | 2010-06-03 21:35:22 +0000 | [diff] [blame] | 720 | case OpCast: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 721 | s += "(" + ts + ")__a;"; |
Nate Begeman | 3861e74 | 2010-06-03 21:35:22 +0000 | [diff] [blame] | 722 | break; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 723 | case OpConcat: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 724 | s += "(" + ts + ")__builtin_shufflevector((int64x1_t)__a"; |
| 725 | s += ", (int64x1_t)__b, 0, 1);"; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 726 | break; |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 727 | case OpHi: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 728 | s += "(((float64x2_t)__a)[1]);"; |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 729 | break; |
| 730 | case OpLo: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 731 | s += "(((float64x2_t)__a)[0]);"; |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 732 | break; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 733 | case OpDup: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 734 | s += Duplicate(nElts, typestr, "__a") + ";"; |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 735 | break; |
Bob Wilson | 2196caa | 2010-12-07 22:39:24 +0000 | [diff] [blame] | 736 | case OpDupLane: |
| 737 | s += SplatLane(nElts, "__a", "__b") + ";"; |
| 738 | break; |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 739 | case OpSelect: |
| 740 | // ((0 & 1) | (~0 & 2)) |
Bob Wilson | 1dbfa91 | 2010-12-02 01:18:20 +0000 | [diff] [blame] | 741 | s += "(" + ts + ")"; |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 742 | ts = TypeString(proto[1], typestr); |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 743 | s += "((__a & (" + ts + ")__b) | "; |
| 744 | s += "(~__a & (" + ts + ")__c));"; |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 745 | break; |
| 746 | case OpRev16: |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 747 | s += "__builtin_shufflevector(__a, __a"; |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 748 | for (unsigned i = 2; i <= nElts; i += 2) |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 749 | for (unsigned j = 0; j != 2; ++j) |
| 750 | s += ", " + utostr(i - j - 1); |
Bob Wilson | 37a0b54 | 2010-12-02 07:44:23 +0000 | [diff] [blame] | 751 | s += ");"; |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 752 | break; |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 753 | case OpRev32: { |
| 754 | unsigned WordElts = nElts >> (1 + (int)quad); |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 755 | s += "__builtin_shufflevector(__a, __a"; |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 756 | for (unsigned i = WordElts; i <= nElts; i += WordElts) |
| 757 | for (unsigned j = 0; j != WordElts; ++j) |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 758 | s += ", " + utostr(i - j - 1); |
Bob Wilson | 37a0b54 | 2010-12-02 07:44:23 +0000 | [diff] [blame] | 759 | s += ");"; |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 760 | break; |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 761 | } |
| 762 | case OpRev64: { |
| 763 | unsigned DblWordElts = nElts >> (int)quad; |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 764 | s += "__builtin_shufflevector(__a, __a"; |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 765 | for (unsigned i = DblWordElts; i <= nElts; i += DblWordElts) |
| 766 | for (unsigned j = 0; j != DblWordElts; ++j) |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 767 | s += ", " + utostr(i - j - 1); |
Bob Wilson | 37a0b54 | 2010-12-02 07:44:23 +0000 | [diff] [blame] | 768 | s += ");"; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 769 | break; |
Bob Wilson | b308b62 | 2010-11-16 23:57:01 +0000 | [diff] [blame] | 770 | } |
Bob Wilson | b450430 | 2010-12-08 21:39:04 +0000 | [diff] [blame] | 771 | case OpAbdl: { |
| 772 | std::string abd = MangleName("vabd", typestr, ClassS) + "(__a, __b)"; |
| 773 | if (typestr[0] != 'U') { |
| 774 | // vabd results are always unsigned and must be zero-extended. |
| 775 | std::string utype = "U" + typestr.str(); |
| 776 | s += "(" + TypeString(proto[0], typestr) + ")"; |
| 777 | abd = "(" + TypeString('d', utype) + ")" + abd; |
| 778 | s += Extend(utype, abd) + ";"; |
| 779 | } else { |
| 780 | s += Extend(typestr, abd) + ";"; |
| 781 | } |
| 782 | break; |
| 783 | } |
Bob Wilson | f4f39d3 | 2010-12-08 20:09:10 +0000 | [diff] [blame] | 784 | case OpAba: |
| 785 | s += "__a + " + MangleName("vabd", typestr, ClassS) + "(__b, __c);"; |
| 786 | break; |
Bob Wilson | b450430 | 2010-12-08 21:39:04 +0000 | [diff] [blame] | 787 | case OpAbal: { |
| 788 | s += "__a + "; |
| 789 | std::string abd = MangleName("vabd", typestr, ClassS) + "(__b, __c)"; |
| 790 | if (typestr[0] != 'U') { |
| 791 | // vabd results are always unsigned and must be zero-extended. |
| 792 | std::string utype = "U" + typestr.str(); |
| 793 | s += "(" + TypeString(proto[0], typestr) + ")"; |
| 794 | abd = "(" + TypeString('d', utype) + ")" + abd; |
| 795 | s += Extend(utype, abd) + ";"; |
| 796 | } else { |
| 797 | s += Extend(typestr, abd) + ";"; |
| 798 | } |
| 799 | break; |
| 800 | } |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 801 | default: |
| 802 | throw "unknown OpKind!"; |
| 803 | break; |
| 804 | } |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 805 | if (op == OpHi || op == OpLo) { |
| 806 | if (!define) |
| 807 | s += " return"; |
| 808 | s += " u.r;"; |
| 809 | } |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 810 | return s; |
Nate Begeman | e66aab5 | 2010-06-02 07:14:28 +0000 | [diff] [blame] | 811 | } |
| 812 | |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 813 | static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) { |
| 814 | unsigned mod = proto[0]; |
| 815 | unsigned ret = 0; |
| 816 | |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 817 | if (mod == 'v' || mod == 'f') |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 818 | mod = proto[1]; |
| 819 | |
| 820 | bool quad = false; |
| 821 | bool poly = false; |
| 822 | bool usgn = false; |
| 823 | bool scal = false; |
| 824 | bool cnst = false; |
| 825 | bool pntr = false; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 826 | |
Nate Begeman | 59d70cb | 2010-08-06 01:24:11 +0000 | [diff] [blame] | 827 | // Base type to get the type string for. |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 828 | char type = ClassifyType(typestr, quad, poly, usgn); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 829 | |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 830 | // Based on the modifying character, change the type and width if necessary. |
| 831 | type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr); |
Nate Begeman | 59d70cb | 2010-08-06 01:24:11 +0000 | [diff] [blame] | 832 | |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 833 | if (usgn) |
| 834 | ret |= 0x08; |
Nate Begeman | 59d70cb | 2010-08-06 01:24:11 +0000 | [diff] [blame] | 835 | if (quad && proto[1] != 'g') |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 836 | ret |= 0x10; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 837 | |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 838 | switch (type) { |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 839 | case 'c': |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 840 | ret |= poly ? 5 : 0; |
| 841 | break; |
| 842 | case 's': |
| 843 | ret |= poly ? 6 : 1; |
| 844 | break; |
| 845 | case 'i': |
| 846 | ret |= 2; |
| 847 | break; |
| 848 | case 'l': |
| 849 | ret |= 3; |
| 850 | break; |
| 851 | case 'h': |
| 852 | ret |= 7; |
| 853 | break; |
| 854 | case 'f': |
| 855 | ret |= 4; |
| 856 | break; |
| 857 | default: |
| 858 | throw "unhandled type!"; |
| 859 | break; |
| 860 | } |
| 861 | return ret; |
| 862 | } |
| 863 | |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 864 | // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a) |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 865 | static std::string GenBuiltin(const std::string &name, const std::string &proto, |
Bob Wilson | f18dfec | 2010-11-16 19:16:06 +0000 | [diff] [blame] | 866 | StringRef typestr, ClassKind ck) { |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 867 | std::string s; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 868 | |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 869 | // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit |
| 870 | // sret-like argument. |
Bob Wilson | 5b7fe59 | 2010-12-01 19:49:51 +0000 | [diff] [blame] | 871 | bool sret = (proto[0] >= '2' && proto[0] <= '4'); |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 872 | |
| 873 | // If this builtin takes an immediate argument, we need to #define it rather |
| 874 | // than use a standard declaration, so that SemaChecking can range check |
| 875 | // the immediate passed by the user. |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 876 | bool define = proto.find('i') != std::string::npos; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 877 | |
Bob Wilson | 95148ad | 2010-12-01 19:49:56 +0000 | [diff] [blame] | 878 | // Check if the prototype has a scalar operand with the type of the vector |
| 879 | // elements. If not, bitcasting the args will take care of arg checking. |
| 880 | // The actual signedness etc. will be taken care of with special enums. |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 881 | if (proto.find('s') == std::string::npos) |
| 882 | ck = ClassB; |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 883 | |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 884 | if (proto[0] != 'v') { |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 885 | std::string ts = TypeString(proto[0], typestr); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 886 | |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 887 | if (define) { |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 888 | if (sret) |
Bob Wilson | 052008b | 2010-12-02 02:42:51 +0000 | [diff] [blame] | 889 | s += ts + " r; "; |
Bob Wilson | e106bc1 | 2010-12-02 00:24:56 +0000 | [diff] [blame] | 890 | else |
Bob Wilson | f18dfec | 2010-11-16 19:16:06 +0000 | [diff] [blame] | 891 | s += "(" + ts + ")"; |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 892 | } else if (sret) { |
| 893 | s += ts + " r; "; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 894 | } else { |
Bob Wilson | 37a0b54 | 2010-12-02 07:44:23 +0000 | [diff] [blame] | 895 | s += "return (" + ts + ")"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 896 | } |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 897 | } |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 898 | |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 899 | bool splat = proto.find('a') != std::string::npos; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 900 | |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 901 | s += "__builtin_neon_"; |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 902 | if (splat) { |
Bob Wilson | 95148ad | 2010-12-01 19:49:56 +0000 | [diff] [blame] | 903 | // 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] | 904 | std::string vname(name, 0, name.size()-2); |
| 905 | s += MangleName(vname, typestr, ck); |
| 906 | } else { |
| 907 | s += MangleName(name, typestr, ck); |
| 908 | } |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 909 | s += "("; |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 910 | |
| 911 | // Pass the address of the return variable as the first argument to sret-like |
| 912 | // builtins. |
| 913 | if (sret) |
| 914 | s += "&r, "; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 915 | |
Bob Wilson | 377296e | 2010-12-02 07:10:39 +0000 | [diff] [blame] | 916 | char arg = 'a'; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 917 | for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) { |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 918 | std::string args = std::string(&arg, 1); |
Bob Wilson | 95148ad | 2010-12-01 19:49:56 +0000 | [diff] [blame] | 919 | |
Bob Wilson | 194aa58 | 2010-12-03 00:34:09 +0000 | [diff] [blame] | 920 | // Use the local temporaries instead of the macro arguments. |
| 921 | args = "__" + args; |
Bob Wilson | f00140c | 2010-12-01 19:49:58 +0000 | [diff] [blame] | 922 | |
| 923 | bool argQuad = false; |
| 924 | bool argPoly = false; |
| 925 | bool argUsgn = false; |
| 926 | bool argScalar = false; |
| 927 | bool dummy = false; |
| 928 | char argType = ClassifyType(typestr, argQuad, argPoly, argUsgn); |
| 929 | argType = ModType(proto[i], argType, argQuad, argPoly, argUsgn, argScalar, |
| 930 | dummy, dummy); |
| 931 | |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 932 | // Handle multiple-vector values specially, emitting each subvector as an |
| 933 | // argument to the __builtin. |
Bob Wilson | 5b7fe59 | 2010-12-01 19:49:51 +0000 | [diff] [blame] | 934 | if (proto[i] >= '2' && proto[i] <= '4') { |
Bob Wilson | f00140c | 2010-12-01 19:49:58 +0000 | [diff] [blame] | 935 | // Check if an explicit cast is needed. |
| 936 | if (argType != 'c' || argPoly || argUsgn) |
| 937 | args = (argQuad ? "(int8x16_t)" : "(int8x8_t)") + args; |
| 938 | |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 939 | for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) { |
Bob Wilson | f18dfec | 2010-11-16 19:16:06 +0000 | [diff] [blame] | 940 | s += args + ".val[" + utostr(vi) + "]"; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 941 | if ((vi + 1) < ve) |
| 942 | s += ", "; |
| 943 | } |
| 944 | if ((i + 1) < e) |
| 945 | s += ", "; |
| 946 | |
| 947 | continue; |
| 948 | } |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 949 | |
Bob Wilson | 4a6c7fc | 2010-12-02 01:18:23 +0000 | [diff] [blame] | 950 | if (splat && (i + 1) == e) |
| 951 | args = Duplicate(GetNumElements(typestr, argQuad), typestr, args); |
| 952 | |
Bob Wilson | f00140c | 2010-12-01 19:49:58 +0000 | [diff] [blame] | 953 | // Check if an explicit cast is needed. |
Bob Wilson | 4a6c7fc | 2010-12-02 01:18:23 +0000 | [diff] [blame] | 954 | if ((splat || !argScalar) && |
Bob Wilson | f00140c | 2010-12-01 19:49:58 +0000 | [diff] [blame] | 955 | ((ck == ClassB && argType != 'c') || argPoly || argUsgn)) { |
| 956 | std::string argTypeStr = "c"; |
| 957 | if (ck != ClassB) |
| 958 | argTypeStr = argType; |
| 959 | if (argQuad) |
| 960 | argTypeStr = "Q" + argTypeStr; |
| 961 | args = "(" + TypeString('d', argTypeStr) + ")" + args; |
| 962 | } |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 963 | |
Bob Wilson | 4a6c7fc | 2010-12-02 01:18:23 +0000 | [diff] [blame] | 964 | s += args; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 965 | if ((i + 1) < e) |
| 966 | s += ", "; |
| 967 | } |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 968 | |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 969 | // 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] | 970 | if (ck == ClassB) |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 971 | s += ", " + utostr(GetNeonEnum(proto, typestr)); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 972 | |
Bob Wilson | 052008b | 2010-12-02 02:42:51 +0000 | [diff] [blame] | 973 | s += ");"; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 974 | |
Bob Wilson | 37a0b54 | 2010-12-02 07:44:23 +0000 | [diff] [blame] | 975 | if (proto[0] != 'v' && sret) { |
| 976 | if (define) |
| 977 | s += " r;"; |
| 978 | else |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 979 | s += " return r;"; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 980 | } |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 981 | return s; |
Nate Begeman | e66aab5 | 2010-06-02 07:14:28 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 984 | static std::string GenBuiltinDef(const std::string &name, |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 985 | const std::string &proto, |
| 986 | StringRef typestr, ClassKind ck) { |
| 987 | std::string s("BUILTIN(__builtin_neon_"); |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 988 | |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 989 | // 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] | 990 | // of arg checking. The actual signedness etc. will be taken care of with |
| 991 | // special enums. |
| 992 | if (proto.find('s') == std::string::npos) |
| 993 | ck = ClassB; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 994 | |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 995 | s += MangleName(name, typestr, ck); |
| 996 | s += ", \""; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 997 | |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 998 | for (unsigned i = 0, e = proto.size(); i != e; ++i) |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 999 | s += BuiltinTypeString(proto[i], typestr, ck, i == 0); |
| 1000 | |
| 1001 | // Extra constant integer to hold type class enum for this function, e.g. s8 |
| 1002 | if (ck == ClassB) |
| 1003 | s += "i"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1004 | |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1005 | s += "\", \"n\")"; |
| 1006 | return s; |
| 1007 | } |
| 1008 | |
Bob Wilson | 3890e39 | 2010-12-07 01:12:23 +0000 | [diff] [blame] | 1009 | static std::string GenIntrinsic(const std::string &name, |
| 1010 | const std::string &proto, |
| 1011 | StringRef outTypeStr, StringRef inTypeStr, |
| 1012 | OpKind kind, ClassKind classKind) { |
| 1013 | assert(!proto.empty() && ""); |
| 1014 | bool define = proto.find('i') != std::string::npos; |
| 1015 | std::string s; |
| 1016 | |
| 1017 | // static always inline + return type |
| 1018 | if (define) |
| 1019 | s += "#define "; |
| 1020 | else |
| 1021 | s += "__ai " + TypeString(proto[0], outTypeStr) + " "; |
| 1022 | |
| 1023 | // Function name with type suffix |
| 1024 | std::string mangledName = MangleName(name, outTypeStr, ClassS); |
| 1025 | if (outTypeStr != inTypeStr) { |
| 1026 | // If the input type is different (e.g., for vreinterpret), append a suffix |
| 1027 | // for the input type. String off a "Q" (quad) prefix so that MangleName |
| 1028 | // does not insert another "q" in the name. |
| 1029 | unsigned typeStrOff = (inTypeStr[0] == 'Q' ? 1 : 0); |
| 1030 | StringRef inTypeNoQuad = inTypeStr.substr(typeStrOff); |
| 1031 | mangledName = MangleName(mangledName, inTypeNoQuad, ClassS); |
| 1032 | } |
| 1033 | s += mangledName; |
| 1034 | |
| 1035 | // Function arguments |
| 1036 | s += GenArgs(proto, inTypeStr); |
| 1037 | |
| 1038 | // Definition. |
| 1039 | if (define) { |
| 1040 | s += " __extension__ ({ \\\n "; |
| 1041 | s += GenMacroLocals(proto, inTypeStr); |
| 1042 | } else { |
| 1043 | s += " { \\\n "; |
| 1044 | } |
| 1045 | |
| 1046 | if (kind != OpNone) |
| 1047 | s += GenOpString(kind, proto, outTypeStr); |
| 1048 | else |
| 1049 | s += GenBuiltin(name, proto, outTypeStr, classKind); |
| 1050 | if (define) |
| 1051 | s += " })"; |
| 1052 | else |
| 1053 | s += " }"; |
| 1054 | s += "\n"; |
| 1055 | return s; |
| 1056 | } |
| 1057 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 1058 | /// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h |
| 1059 | /// is comprised of type definitions and function declarations. |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 1060 | void NeonEmitter::run(raw_ostream &OS) { |
Bob Wilson | 0e1fb7a | 2010-12-09 18:31:01 +0000 | [diff] [blame] | 1061 | OS << |
Bob Wilson | b78558c | 2010-12-09 18:43:35 +0000 | [diff] [blame] | 1062 | "/*===---- arm_neon.h - ARM Neon intrinsics ------------------------------" |
| 1063 | "---===\n" |
Bob Wilson | 0e1fb7a | 2010-12-09 18:31:01 +0000 | [diff] [blame] | 1064 | " *\n" |
Bob Wilson | b78558c | 2010-12-09 18:43:35 +0000 | [diff] [blame] | 1065 | " * Permission is hereby granted, free of charge, to any person obtaining " |
| 1066 | "a copy\n" |
| 1067 | " * of this software and associated documentation files (the \"Software\")," |
| 1068 | " to deal\n" |
| 1069 | " * in the Software without restriction, including without limitation the " |
| 1070 | "rights\n" |
| 1071 | " * to use, copy, modify, merge, publish, distribute, sublicense, " |
| 1072 | "and/or sell\n" |
Bob Wilson | 0e1fb7a | 2010-12-09 18:31:01 +0000 | [diff] [blame] | 1073 | " * copies of the Software, and to permit persons to whom the Software is\n" |
| 1074 | " * furnished to do so, subject to the following conditions:\n" |
| 1075 | " *\n" |
Bob Wilson | b78558c | 2010-12-09 18:43:35 +0000 | [diff] [blame] | 1076 | " * The above copyright notice and this permission notice shall be " |
| 1077 | "included in\n" |
Bob Wilson | 0e1fb7a | 2010-12-09 18:31:01 +0000 | [diff] [blame] | 1078 | " * all copies or substantial portions of the Software.\n" |
| 1079 | " *\n" |
Bob Wilson | b78558c | 2010-12-09 18:43:35 +0000 | [diff] [blame] | 1080 | " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, " |
| 1081 | "EXPRESS OR\n" |
| 1082 | " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF " |
| 1083 | "MERCHANTABILITY,\n" |
| 1084 | " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT " |
| 1085 | "SHALL THE\n" |
| 1086 | " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR " |
| 1087 | "OTHER\n" |
| 1088 | " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, " |
| 1089 | "ARISING FROM,\n" |
| 1090 | " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER " |
| 1091 | "DEALINGS IN\n" |
Bob Wilson | 0e1fb7a | 2010-12-09 18:31:01 +0000 | [diff] [blame] | 1092 | " * THE SOFTWARE.\n" |
| 1093 | " *\n" |
Bob Wilson | b78558c | 2010-12-09 18:43:35 +0000 | [diff] [blame] | 1094 | " *===--------------------------------------------------------------------" |
| 1095 | "---===\n" |
Bob Wilson | 0e1fb7a | 2010-12-09 18:31:01 +0000 | [diff] [blame] | 1096 | " */\n\n"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1097 | |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 1098 | OS << "#ifndef __ARM_NEON_H\n"; |
| 1099 | OS << "#define __ARM_NEON_H\n\n"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1100 | |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 1101 | OS << "#ifndef __ARM_NEON__\n"; |
| 1102 | OS << "#error \"NEON support not enabled\"\n"; |
| 1103 | OS << "#endif\n\n"; |
| 1104 | |
| 1105 | OS << "#include <stdint.h>\n\n"; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 1106 | |
| 1107 | // Emit NEON-specific scalar typedefs. |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 1108 | OS << "typedef float float32_t;\n"; |
Bob Wilson | 4fbf638 | 2010-11-16 23:57:03 +0000 | [diff] [blame] | 1109 | OS << "typedef int8_t poly8_t;\n"; |
| 1110 | OS << "typedef int16_t poly16_t;\n"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 1111 | OS << "typedef uint16_t float16_t;\n"; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 1112 | |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 1113 | // Emit Neon vector typedefs. |
| 1114 | std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs"); |
| 1115 | SmallVector<StringRef, 24> TDTypeVec; |
| 1116 | ParseTypes(0, TypedefTypes, TDTypeVec); |
| 1117 | |
| 1118 | // Emit vector typedefs. |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 1119 | for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) { |
Bob Wilson | 6a8aceb | 2010-11-16 23:57:06 +0000 | [diff] [blame] | 1120 | bool dummy, quad = false, poly = false; |
| 1121 | (void) ClassifyType(TDTypeVec[i], quad, poly, dummy); |
| 1122 | if (poly) |
| 1123 | OS << "typedef __attribute__((neon_polyvector_type("; |
| 1124 | else |
| 1125 | OS << "typedef __attribute__((neon_vector_type("; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1126 | |
Bob Wilson | 6a8aceb | 2010-11-16 23:57:06 +0000 | [diff] [blame] | 1127 | unsigned nElts = GetNumElements(TDTypeVec[i], quad); |
| 1128 | OS << utostr(nElts) << "))) "; |
| 1129 | if (nElts < 10) |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 1130 | OS << " "; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1131 | |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 1132 | OS << TypeString('s', TDTypeVec[i]); |
| 1133 | OS << " " << TypeString('d', TDTypeVec[i]) << ";\n"; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 1134 | } |
| 1135 | OS << "\n"; |
Bob Wilson | 6a8aceb | 2010-11-16 23:57:06 +0000 | [diff] [blame] | 1136 | OS << "typedef __attribute__((__vector_size__(8))) " |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 1137 | "double float64x1_t;\n"; |
Bob Wilson | 6a8aceb | 2010-11-16 23:57:06 +0000 | [diff] [blame] | 1138 | OS << "typedef __attribute__((__vector_size__(16))) " |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 1139 | "double float64x2_t;\n"; |
Bob Wilson | ee9ca07 | 2010-09-15 01:52:33 +0000 | [diff] [blame] | 1140 | OS << "\n"; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 1141 | |
| 1142 | // Emit struct typedefs. |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 1143 | for (unsigned vi = 2; vi != 5; ++vi) { |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 1144 | for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) { |
Bob Wilson | 6904d7a | 2010-11-16 19:39:14 +0000 | [diff] [blame] | 1145 | std::string ts = TypeString('d', TDTypeVec[i]); |
| 1146 | std::string vs = TypeString('0' + vi, TDTypeVec[i]); |
| 1147 | OS << "typedef struct " << vs << " {\n"; |
| 1148 | OS << " " << ts << " val"; |
| 1149 | OS << "[" << utostr(vi) << "]"; |
| 1150 | OS << ";\n} "; |
Bob Wilson | f18dfec | 2010-11-16 19:16:06 +0000 | [diff] [blame] | 1151 | OS << vs << ";\n\n"; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 1152 | } |
| 1153 | } |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1154 | |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 1155 | OS << "#define __ai static __attribute__((__always_inline__))\n\n"; |
| 1156 | |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 1157 | std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst"); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1158 | |
Bob Wilson | f4f39d3 | 2010-12-08 20:09:10 +0000 | [diff] [blame] | 1159 | // Emit vmovl and vabd intrinsics first so they can be used by other |
Bob Wilson | 7441089 | 2010-12-08 22:36:08 +0000 | [diff] [blame] | 1160 | // intrinsics. (Some of the saturating multiply instructions are also |
| 1161 | // used to implement the corresponding "_lane" variants, but tablegen |
| 1162 | // sorts the records into alphabetical order so that the "_lane" variants |
| 1163 | // come after the intrinsics they use.) |
Bob Wilson | da1d3dc | 2010-12-07 23:53:32 +0000 | [diff] [blame] | 1164 | emitIntrinsic(OS, Records.getDef("VMOVL")); |
Bob Wilson | f4f39d3 | 2010-12-08 20:09:10 +0000 | [diff] [blame] | 1165 | emitIntrinsic(OS, Records.getDef("VABD")); |
Bob Wilson | da1d3dc | 2010-12-07 23:53:32 +0000 | [diff] [blame] | 1166 | |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 1167 | for (unsigned i = 0, e = RV.size(); i != e; ++i) { |
| 1168 | Record *R = RV[i]; |
Bob Wilson | f4f39d3 | 2010-12-08 20:09:10 +0000 | [diff] [blame] | 1169 | if (R->getName() != "VMOVL" && R->getName() != "VABD") |
Bob Wilson | da1d3dc | 2010-12-07 23:53:32 +0000 | [diff] [blame] | 1170 | emitIntrinsic(OS, R); |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 1171 | } |
Bob Wilson | da1d3dc | 2010-12-07 23:53:32 +0000 | [diff] [blame] | 1172 | |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1173 | OS << "#undef __ai\n\n"; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 1174 | OS << "#endif /* __ARM_NEON_H */\n"; |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 1175 | } |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 1176 | |
Bob Wilson | da1d3dc | 2010-12-07 23:53:32 +0000 | [diff] [blame] | 1177 | /// emitIntrinsic - Write out the arm_neon.h header file definitions for the |
| 1178 | /// intrinsics specified by record R. |
| 1179 | void NeonEmitter::emitIntrinsic(raw_ostream &OS, Record *R) { |
| 1180 | std::string name = R->getValueAsString("Name"); |
| 1181 | std::string Proto = R->getValueAsString("Prototype"); |
| 1182 | std::string Types = R->getValueAsString("Types"); |
| 1183 | |
| 1184 | SmallVector<StringRef, 16> TypeVec; |
| 1185 | ParseTypes(R, Types, TypeVec); |
| 1186 | |
| 1187 | OpKind kind = OpMap[R->getValueAsDef("Operand")->getName()]; |
| 1188 | |
| 1189 | ClassKind classKind = ClassNone; |
| 1190 | if (R->getSuperClasses().size() >= 2) |
| 1191 | classKind = ClassMap[R->getSuperClasses()[1]]; |
| 1192 | if (classKind == ClassNone && kind == OpNone) |
| 1193 | throw TGError(R->getLoc(), "Builtin has no class kind"); |
| 1194 | |
| 1195 | for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) { |
| 1196 | if (kind == OpReinterpret) { |
| 1197 | bool outQuad = false; |
| 1198 | bool dummy = false; |
| 1199 | (void)ClassifyType(TypeVec[ti], outQuad, dummy, dummy); |
| 1200 | for (unsigned srcti = 0, srcte = TypeVec.size(); |
| 1201 | srcti != srcte; ++srcti) { |
| 1202 | bool inQuad = false; |
| 1203 | (void)ClassifyType(TypeVec[srcti], inQuad, dummy, dummy); |
| 1204 | if (srcti == ti || inQuad != outQuad) |
| 1205 | continue; |
| 1206 | OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[srcti], |
| 1207 | OpCast, ClassS); |
| 1208 | } |
| 1209 | } else { |
| 1210 | OS << GenIntrinsic(name, Proto, TypeVec[ti], TypeVec[ti], |
| 1211 | kind, classKind); |
| 1212 | } |
| 1213 | } |
| 1214 | OS << "\n"; |
| 1215 | } |
| 1216 | |
Bob Wilson | 06b04aa | 2010-12-15 16:58:42 +0000 | [diff] [blame] | 1217 | static unsigned RangeFromType(const char mod, StringRef typestr) { |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1218 | // base type to get the type string for. |
| 1219 | bool quad = false, dummy = false; |
| 1220 | char type = ClassifyType(typestr, quad, dummy, dummy); |
Bob Wilson | 06b04aa | 2010-12-15 16:58:42 +0000 | [diff] [blame] | 1221 | type = ModType(mod, type, quad, dummy, dummy, dummy, dummy, dummy); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1222 | |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1223 | switch (type) { |
| 1224 | case 'c': |
Nate Begeman | 4da883a | 2010-06-15 22:10:31 +0000 | [diff] [blame] | 1225 | return (8 << (int)quad) - 1; |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1226 | case 'h': |
| 1227 | case 's': |
Nate Begeman | 4da883a | 2010-06-15 22:10:31 +0000 | [diff] [blame] | 1228 | return (4 << (int)quad) - 1; |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1229 | case 'f': |
| 1230 | case 'i': |
Nate Begeman | 4da883a | 2010-06-15 22:10:31 +0000 | [diff] [blame] | 1231 | return (2 << (int)quad) - 1; |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1232 | case 'l': |
Nate Begeman | 4da883a | 2010-06-15 22:10:31 +0000 | [diff] [blame] | 1233 | return (1 << (int)quad) - 1; |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1234 | default: |
| 1235 | throw "unhandled type!"; |
| 1236 | break; |
| 1237 | } |
Bob Wilson | fdb530d | 2010-07-28 18:21:10 +0000 | [diff] [blame] | 1238 | assert(0 && "unreachable"); |
| 1239 | return 0; |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1242 | /// runHeader - Emit a file with sections defining: |
| 1243 | /// 1. the NEON section of BuiltinsARM.def. |
| 1244 | /// 2. the SemaChecking code for the type overload checking. |
| 1245 | /// 3. the SemaChecking code for validation of intrinsic immedate arguments. |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 1246 | void NeonEmitter::runHeader(raw_ostream &OS) { |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1247 | std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst"); |
| 1248 | |
| 1249 | StringMap<OpKind> EmittedMap; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1250 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1251 | // Generate BuiltinsARM.def for NEON |
| 1252 | OS << "#ifdef GET_NEON_BUILTINS\n"; |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1253 | for (unsigned i = 0, e = RV.size(); i != e; ++i) { |
| 1254 | Record *R = RV[i]; |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1255 | OpKind k = OpMap[R->getValueAsDef("Operand")->getName()]; |
| 1256 | if (k != OpNone) |
| 1257 | continue; |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1258 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1259 | std::string Proto = R->getValueAsString("Prototype"); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1260 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 1261 | // Functions with 'a' (the splat code) in the type prototype should not get |
| 1262 | // their own builtin as they use the non-splat variant. |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 1263 | if (Proto.find('a') != std::string::npos) |
| 1264 | continue; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1265 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1266 | std::string Types = R->getValueAsString("Types"); |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1267 | SmallVector<StringRef, 16> TypeVec; |
| 1268 | ParseTypes(R, Types, TypeVec); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1269 | |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1270 | if (R->getSuperClasses().size() < 2) |
| 1271 | throw TGError(R->getLoc(), "Builtin has no class kind"); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1272 | |
Bob Wilson | ce0bb54 | 2010-12-03 17:19:39 +0000 | [diff] [blame] | 1273 | std::string name = R->getValueAsString("Name"); |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1274 | ClassKind ck = ClassMap[R->getSuperClasses()[1]]; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1275 | |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1276 | for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) { |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 1277 | // Generate the BuiltinsARM.def declaration for this builtin, ensuring |
| 1278 | // that each unique BUILTIN() macro appears only once in the output |
| 1279 | // stream. |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1280 | std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck); |
| 1281 | if (EmittedMap.count(bd)) |
| 1282 | continue; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1283 | |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1284 | EmittedMap[bd] = OpNone; |
| 1285 | OS << bd << "\n"; |
| 1286 | } |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1287 | } |
| 1288 | OS << "#endif\n\n"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1289 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1290 | // Generate the overloaded type checking code for SemaChecking.cpp |
| 1291 | OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n"; |
| 1292 | for (unsigned i = 0, e = RV.size(); i != e; ++i) { |
| 1293 | Record *R = RV[i]; |
| 1294 | OpKind k = OpMap[R->getValueAsDef("Operand")->getName()]; |
| 1295 | if (k != OpNone) |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 1296 | continue; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1297 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1298 | std::string Proto = R->getValueAsString("Prototype"); |
| 1299 | std::string Types = R->getValueAsString("Types"); |
Bob Wilson | ce0bb54 | 2010-12-03 17:19:39 +0000 | [diff] [blame] | 1300 | std::string name = R->getValueAsString("Name"); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1301 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1302 | // Functions with 'a' (the splat code) in the type prototype should not get |
| 1303 | // their own builtin as they use the non-splat variant. |
| 1304 | if (Proto.find('a') != std::string::npos) |
| 1305 | continue; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1306 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1307 | // Functions which have a scalar argument cannot be overloaded, no need to |
| 1308 | // check them if we are emitting the type checking code. |
| 1309 | if (Proto.find('s') != std::string::npos) |
| 1310 | continue; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1311 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1312 | SmallVector<StringRef, 16> TypeVec; |
| 1313 | ParseTypes(R, Types, TypeVec); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1314 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1315 | if (R->getSuperClasses().size() < 2) |
| 1316 | throw TGError(R->getLoc(), "Builtin has no class kind"); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1317 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1318 | int si = -1, qi = -1; |
| 1319 | unsigned mask = 0, qmask = 0; |
| 1320 | for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) { |
| 1321 | // Generate the switch case(s) for this builtin for the type validation. |
| 1322 | bool quad = false, poly = false, usgn = false; |
| 1323 | (void) ClassifyType(TypeVec[ti], quad, poly, usgn); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1324 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1325 | if (quad) { |
| 1326 | qi = ti; |
| 1327 | qmask |= 1 << GetNeonEnum(Proto, TypeVec[ti]); |
| 1328 | } else { |
| 1329 | si = ti; |
| 1330 | mask |= 1 << GetNeonEnum(Proto, TypeVec[ti]); |
| 1331 | } |
| 1332 | } |
| 1333 | if (mask) |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1334 | OS << "case ARM::BI__builtin_neon_" |
Bob Wilson | d8b8470 | 2010-12-07 01:12:19 +0000 | [diff] [blame] | 1335 | << MangleName(name, TypeVec[si], ClassB) |
| 1336 | << ": mask = " << "0x" << utohexstr(mask) << "; break;\n"; |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1337 | if (qmask) |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1338 | OS << "case ARM::BI__builtin_neon_" |
Bob Wilson | d8b8470 | 2010-12-07 01:12:19 +0000 | [diff] [blame] | 1339 | << MangleName(name, TypeVec[qi], ClassB) |
| 1340 | << ": mask = " << "0x" << utohexstr(qmask) << "; break;\n"; |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1341 | } |
| 1342 | OS << "#endif\n\n"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1343 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1344 | // Generate the intrinsic range checking code for shift/lane immediates. |
| 1345 | OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n"; |
| 1346 | for (unsigned i = 0, e = RV.size(); i != e; ++i) { |
| 1347 | Record *R = RV[i]; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1348 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1349 | OpKind k = OpMap[R->getValueAsDef("Operand")->getName()]; |
| 1350 | if (k != OpNone) |
| 1351 | continue; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1352 | |
Bob Wilson | ce0bb54 | 2010-12-03 17:19:39 +0000 | [diff] [blame] | 1353 | std::string name = R->getValueAsString("Name"); |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1354 | std::string Proto = R->getValueAsString("Prototype"); |
| 1355 | std::string Types = R->getValueAsString("Types"); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1356 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1357 | // Functions with 'a' (the splat code) in the type prototype should not get |
| 1358 | // their own builtin as they use the non-splat variant. |
| 1359 | if (Proto.find('a') != std::string::npos) |
| 1360 | continue; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1361 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1362 | // Functions which do not have an immediate do not need to have range |
| 1363 | // checking code emitted. |
Bob Wilson | 06b04aa | 2010-12-15 16:58:42 +0000 | [diff] [blame] | 1364 | size_t immPos = Proto.find('i'); |
| 1365 | if (immPos == std::string::npos) |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1366 | continue; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1367 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1368 | SmallVector<StringRef, 16> TypeVec; |
| 1369 | ParseTypes(R, Types, TypeVec); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1370 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1371 | if (R->getSuperClasses().size() < 2) |
| 1372 | throw TGError(R->getLoc(), "Builtin has no class kind"); |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1373 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1374 | ClassKind ck = ClassMap[R->getSuperClasses()[1]]; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1375 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1376 | for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) { |
| 1377 | std::string namestr, shiftstr, rangestr; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1378 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1379 | // Builtins which are overloaded by type will need to have their upper |
| 1380 | // bound computed at Sema time based on the type constant. |
| 1381 | if (Proto.find('s') == std::string::npos) { |
| 1382 | ck = ClassB; |
| 1383 | if (R->getValueAsBit("isShift")) { |
| 1384 | shiftstr = ", true"; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1385 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1386 | // Right shifts have an 'r' in the name, left shifts do not. |
| 1387 | if (name.find('r') != std::string::npos) |
| 1388 | rangestr = "l = 1; "; |
| 1389 | } |
| 1390 | rangestr += "u = RFT(TV" + shiftstr + ")"; |
| 1391 | } else { |
Bob Wilson | 06b04aa | 2010-12-15 16:58:42 +0000 | [diff] [blame] | 1392 | // The immediate generally refers to a lane in the preceding argument. |
| 1393 | assert(immPos > 0 && "unexpected immediate operand"); |
| 1394 | rangestr = "u = " + utostr(RangeFromType(Proto[immPos-1], TypeVec[ti])); |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1395 | } |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 1396 | // 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] | 1397 | namestr = MangleName(name, TypeVec[ti], ck); |
| 1398 | if (EmittedMap.count(namestr)) |
| 1399 | continue; |
| 1400 | EmittedMap[namestr] = OpNone; |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 1401 | |
| 1402 | // Calculate the index of the immediate that should be range checked. |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1403 | unsigned immidx = 0; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1404 | |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 1405 | // Builtins that return a struct of multiple vectors have an extra |
| 1406 | // leading arg for the struct return. |
Bob Wilson | 5b7fe59 | 2010-12-01 19:49:51 +0000 | [diff] [blame] | 1407 | if (Proto[0] >= '2' && Proto[0] <= '4') |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 1408 | ++immidx; |
Bob Wilson | 7f76218 | 2010-12-04 04:40:15 +0000 | [diff] [blame] | 1409 | |
| 1410 | // 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] | 1411 | // to be checked. Structs of vectors are passed as multiple arguments. |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1412 | for (unsigned ii = 1, ie = Proto.size(); ii != ie; ++ii) { |
| 1413 | switch (Proto[ii]) { |
| 1414 | default: immidx += 1; break; |
| 1415 | case '2': immidx += 2; break; |
| 1416 | case '3': immidx += 3; break; |
| 1417 | case '4': immidx += 4; break; |
| 1418 | case 'i': ie = ii + 1; break; |
| 1419 | } |
| 1420 | } |
Bob Wilson | d8b8470 | 2010-12-07 01:12:19 +0000 | [diff] [blame] | 1421 | OS << "case ARM::BI__builtin_neon_" << MangleName(name, TypeVec[ti], ck) |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1422 | << ": i = " << immidx << "; " << rangestr << "; break;\n"; |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 1423 | } |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1424 | } |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1425 | OS << "#endif\n\n"; |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 1426 | } |
Bob Wilson | 333f519 | 2010-12-15 16:58:45 +0000 | [diff] [blame] | 1427 | |
| 1428 | /// GenTest - Write out a test for the intrinsic specified by the name and |
| 1429 | /// type strings, including the embedded patterns for FileCheck to match. |
| 1430 | static std::string GenTest(const std::string &name, |
| 1431 | const std::string &proto, |
| 1432 | StringRef outTypeStr, StringRef inTypeStr, |
| 1433 | bool isShift) { |
| 1434 | assert(!proto.empty() && ""); |
| 1435 | std::string s; |
| 1436 | |
| 1437 | // Function name with type suffix |
| 1438 | std::string mangledName = MangleName(name, outTypeStr, ClassS); |
| 1439 | if (outTypeStr != inTypeStr) { |
| 1440 | // If the input type is different (e.g., for vreinterpret), append a suffix |
| 1441 | // for the input type. String off a "Q" (quad) prefix so that MangleName |
| 1442 | // does not insert another "q" in the name. |
| 1443 | unsigned typeStrOff = (inTypeStr[0] == 'Q' ? 1 : 0); |
| 1444 | StringRef inTypeNoQuad = inTypeStr.substr(typeStrOff); |
| 1445 | mangledName = MangleName(mangledName, inTypeNoQuad, ClassS); |
| 1446 | } |
| 1447 | |
| 1448 | // Emit the FileCheck patterns. |
| 1449 | s += "// CHECK: test_" + mangledName + "\n"; |
| 1450 | // s += "// CHECK: \n"; // FIXME: + expected instruction opcode. |
| 1451 | |
| 1452 | // Emit the start of the test function. |
| 1453 | s += TypeString(proto[0], outTypeStr) + " test_" + mangledName + "("; |
| 1454 | char arg = 'a'; |
| 1455 | std::string comma; |
| 1456 | for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) { |
| 1457 | // Do not create arguments for values that must be immediate constants. |
| 1458 | if (proto[i] == 'i') |
| 1459 | continue; |
| 1460 | s += comma + TypeString(proto[i], inTypeStr) + " "; |
| 1461 | s.push_back(arg); |
| 1462 | comma = ", "; |
| 1463 | } |
| 1464 | s += ") { \\\n "; |
| 1465 | |
| 1466 | if (proto[0] != 'v') |
| 1467 | s += "return "; |
| 1468 | s += mangledName + "("; |
| 1469 | arg = 'a'; |
| 1470 | for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) { |
| 1471 | if (proto[i] == 'i') { |
| 1472 | // For immediate operands, test the maximum value. |
| 1473 | if (isShift) |
| 1474 | s += "1"; // FIXME |
| 1475 | else |
| 1476 | // The immediate generally refers to a lane in the preceding argument. |
| 1477 | s += utostr(RangeFromType(proto[i-1], inTypeStr)); |
| 1478 | } else { |
| 1479 | s.push_back(arg); |
| 1480 | } |
| 1481 | if ((i + 1) < e) |
| 1482 | s += ", "; |
| 1483 | } |
| 1484 | s += ");\n}\n\n"; |
| 1485 | return s; |
| 1486 | } |
| 1487 | |
| 1488 | /// runTests - Write out a complete set of tests for all of the Neon |
| 1489 | /// intrinsics. |
| 1490 | void NeonEmitter::runTests(raw_ostream &OS) { |
| 1491 | OS << |
| 1492 | "// RUN: %clang_cc1 -triple thumbv7-apple-darwin \\\n" |
| 1493 | "// RUN: -target-cpu cortex-a8 -ffreestanding -S -o - %s | FileCheck %s\n" |
| 1494 | "\n" |
| 1495 | "#include <arm_neon.h>\n" |
| 1496 | "\n"; |
| 1497 | |
| 1498 | std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst"); |
| 1499 | for (unsigned i = 0, e = RV.size(); i != e; ++i) { |
| 1500 | Record *R = RV[i]; |
| 1501 | std::string name = R->getValueAsString("Name"); |
| 1502 | std::string Proto = R->getValueAsString("Prototype"); |
| 1503 | std::string Types = R->getValueAsString("Types"); |
| 1504 | bool isShift = R->getValueAsBit("isShift"); |
| 1505 | |
| 1506 | SmallVector<StringRef, 16> TypeVec; |
| 1507 | ParseTypes(R, Types, TypeVec); |
| 1508 | |
| 1509 | OpKind kind = OpMap[R->getValueAsDef("Operand")->getName()]; |
| 1510 | for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) { |
| 1511 | if (kind == OpReinterpret) { |
| 1512 | bool outQuad = false; |
| 1513 | bool dummy = false; |
| 1514 | (void)ClassifyType(TypeVec[ti], outQuad, dummy, dummy); |
| 1515 | for (unsigned srcti = 0, srcte = TypeVec.size(); |
| 1516 | srcti != srcte; ++srcti) { |
| 1517 | bool inQuad = false; |
| 1518 | (void)ClassifyType(TypeVec[srcti], inQuad, dummy, dummy); |
| 1519 | if (srcti == ti || inQuad != outQuad) |
| 1520 | continue; |
| 1521 | OS << GenTest(name, Proto, TypeVec[ti], TypeVec[srcti], isShift); |
| 1522 | } |
| 1523 | } else { |
| 1524 | OS << GenTest(name, Proto, TypeVec[ti], TypeVec[ti], isShift); |
| 1525 | } |
| 1526 | } |
| 1527 | OS << "\n"; |
| 1528 | } |
| 1529 | } |
| 1530 | |