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