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