Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1 | //===- NeonEmitter.cpp - Generate arm_neon.h for use with clang -*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This tablegen backend is responsible for emitting arm_neon.h, which includes |
| 11 | // a declaration and definition of each function specified by the ARM NEON |
| 12 | // compiler interface. See ARM document DUI0348B. |
| 13 | // |
| 14 | // Each NEON instruction is implemented in terms of 1 or more functions which |
| 15 | // are suffixed with the element type of the input vectors. Functions may be |
| 16 | // implemented in terms of generic vector operations such as +, *, -, etc. or |
| 17 | // by calling a __builtin_-prefixed function which will be handled by clang's |
| 18 | // CodeGen library. |
| 19 | // |
| 20 | // Additional validation code can be generated by this file when runHeader() is |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 21 | // called, rather than the normal run() entry point. |
| 22 | // |
| 23 | // See also the documentation in include/clang/Basic/arm_neon.td. |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 24 | // |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/ArrayRef.h" |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DenseMap.h" |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/None.h" |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallVector.h" |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/STLExtras.h" |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/StringExtras.h" |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/StringRef.h" |
| 34 | #include "llvm/Support/Casting.h" |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ErrorHandling.h" |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 36 | #include "llvm/Support/raw_ostream.h" |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 37 | #include "llvm/TableGen/Error.h" |
| 38 | #include "llvm/TableGen/Record.h" |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 39 | #include "llvm/TableGen/SetTheory.h" |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 40 | #include <algorithm> |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 41 | #include <cassert> |
| 42 | #include <cctype> |
| 43 | #include <cstddef> |
| 44 | #include <cstdint> |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 45 | #include <deque> |
Chandler Carruth | 575bc3ba | 2015-01-14 11:23:58 +0000 | [diff] [blame] | 46 | #include <map> |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 47 | #include <set> |
Chandler Carruth | 575bc3ba | 2015-01-14 11:23:58 +0000 | [diff] [blame] | 48 | #include <sstream> |
| 49 | #include <string> |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 50 | #include <utility> |
Chandler Carruth | 575bc3ba | 2015-01-14 11:23:58 +0000 | [diff] [blame] | 51 | #include <vector> |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 52 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 53 | using namespace llvm; |
| 54 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 55 | namespace { |
| 56 | |
| 57 | // While globals are generally bad, this one allows us to perform assertions |
| 58 | // liberally and somehow still trace them back to the def they indirectly |
| 59 | // came from. |
| 60 | static Record *CurrentRecord = nullptr; |
| 61 | static void assert_with_loc(bool Assertion, const std::string &Str) { |
| 62 | if (!Assertion) { |
| 63 | if (CurrentRecord) |
| 64 | PrintFatalError(CurrentRecord->getLoc(), Str); |
| 65 | else |
| 66 | PrintFatalError(Str); |
| 67 | } |
| 68 | } |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 69 | |
| 70 | enum ClassKind { |
| 71 | ClassNone, |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 72 | ClassI, // generic integer instruction, e.g., "i8" suffix |
| 73 | ClassS, // signed/unsigned/poly, e.g., "s8", "u8" or "p8" suffix |
| 74 | ClassW, // width-specific instruction, e.g., "8" suffix |
| 75 | ClassB, // bitcast arguments with enum argument to specify type |
| 76 | ClassL, // Logical instructions which are op instructions |
| 77 | // but we need to not emit any suffix for in our |
| 78 | // tests. |
| 79 | ClassNoTest // Instructions which we do not test since they are |
| 80 | // not TRUE instructions. |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | /// NeonTypeFlags - Flags to identify the types for overloaded Neon |
| 84 | /// builtins. These must be kept in sync with the flags in |
| 85 | /// include/clang/Basic/TargetBuiltins.h. |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 86 | namespace NeonTypeFlags { |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 87 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 88 | enum { EltTypeMask = 0xf, UnsignedFlag = 0x10, QuadFlag = 0x20 }; |
| 89 | |
| 90 | enum EltType { |
| 91 | Int8, |
| 92 | Int16, |
| 93 | Int32, |
| 94 | Int64, |
| 95 | Poly8, |
| 96 | Poly16, |
| 97 | Poly64, |
| 98 | Poly128, |
| 99 | Float16, |
| 100 | Float32, |
| 101 | Float64 |
| 102 | }; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 103 | |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 104 | } // end namespace NeonTypeFlags |
| 105 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 106 | class NeonEmitter; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 107 | |
| 108 | //===----------------------------------------------------------------------===// |
| 109 | // TypeSpec |
| 110 | //===----------------------------------------------------------------------===// |
| 111 | |
| 112 | /// A TypeSpec is just a simple wrapper around a string, but gets its own type |
| 113 | /// for strong typing purposes. |
| 114 | /// |
| 115 | /// A TypeSpec can be used to create a type. |
| 116 | class TypeSpec : public std::string { |
| 117 | public: |
| 118 | static std::vector<TypeSpec> fromTypeSpecs(StringRef Str) { |
| 119 | std::vector<TypeSpec> Ret; |
| 120 | TypeSpec Acc; |
| 121 | for (char I : Str.str()) { |
| 122 | if (islower(I)) { |
| 123 | Acc.push_back(I); |
| 124 | Ret.push_back(TypeSpec(Acc)); |
| 125 | Acc.clear(); |
| 126 | } else { |
| 127 | Acc.push_back(I); |
| 128 | } |
| 129 | } |
| 130 | return Ret; |
| 131 | } |
| 132 | }; |
| 133 | |
| 134 | //===----------------------------------------------------------------------===// |
| 135 | // Type |
| 136 | //===----------------------------------------------------------------------===// |
| 137 | |
| 138 | /// A Type. Not much more to say here. |
| 139 | class Type { |
| 140 | private: |
| 141 | TypeSpec TS; |
| 142 | |
Ahmed Bougacha | 94df730 | 2015-06-04 01:43:41 +0000 | [diff] [blame] | 143 | bool Float, Signed, Immediate, Void, Poly, Constant, Pointer; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 144 | // ScalarForMangling and NoManglingQ are really not suited to live here as |
| 145 | // they are not related to the type. But they live in the TypeSpec (not the |
| 146 | // prototype), so this is really the only place to store them. |
| 147 | bool ScalarForMangling, NoManglingQ; |
| 148 | unsigned Bitwidth, ElementBitwidth, NumVectors; |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 149 | |
| 150 | public: |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 151 | Type() |
Ahmed Bougacha | 94df730 | 2015-06-04 01:43:41 +0000 | [diff] [blame] | 152 | : Float(false), Signed(false), Immediate(false), Void(true), Poly(false), |
| 153 | Constant(false), Pointer(false), ScalarForMangling(false), |
| 154 | NoManglingQ(false), Bitwidth(0), ElementBitwidth(0), NumVectors(0) {} |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 155 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 156 | Type(TypeSpec TS, char CharMod) |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 157 | : TS(std::move(TS)), Float(false), Signed(false), Immediate(false), |
| 158 | Void(false), Poly(false), Constant(false), Pointer(false), |
| 159 | ScalarForMangling(false), NoManglingQ(false), Bitwidth(0), |
| 160 | ElementBitwidth(0), NumVectors(0) { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 161 | applyModifier(CharMod); |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 162 | } |
| 163 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 164 | /// Returns a type representing "void". |
| 165 | static Type getVoid() { return Type(); } |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 166 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 167 | bool operator==(const Type &Other) const { return str() == Other.str(); } |
| 168 | bool operator!=(const Type &Other) const { return !operator==(Other); } |
| 169 | |
| 170 | // |
| 171 | // Query functions |
| 172 | // |
| 173 | bool isScalarForMangling() const { return ScalarForMangling; } |
| 174 | bool noManglingQ() const { return NoManglingQ; } |
| 175 | |
| 176 | bool isPointer() const { return Pointer; } |
| 177 | bool isFloating() const { return Float; } |
| 178 | bool isInteger() const { return !Float && !Poly; } |
| 179 | bool isSigned() const { return Signed; } |
Ahmed Bougacha | 94df730 | 2015-06-04 01:43:41 +0000 | [diff] [blame] | 180 | bool isImmediate() const { return Immediate; } |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 181 | bool isScalar() const { return NumVectors == 0; } |
| 182 | bool isVector() const { return NumVectors > 0; } |
| 183 | bool isFloat() const { return Float && ElementBitwidth == 32; } |
| 184 | bool isDouble() const { return Float && ElementBitwidth == 64; } |
| 185 | bool isHalf() const { return Float && ElementBitwidth == 16; } |
| 186 | bool isPoly() const { return Poly; } |
| 187 | bool isChar() const { return ElementBitwidth == 8; } |
| 188 | bool isShort() const { return !Float && ElementBitwidth == 16; } |
| 189 | bool isInt() const { return !Float && ElementBitwidth == 32; } |
| 190 | bool isLong() const { return !Float && ElementBitwidth == 64; } |
| 191 | bool isVoid() const { return Void; } |
| 192 | unsigned getNumElements() const { return Bitwidth / ElementBitwidth; } |
| 193 | unsigned getSizeInBits() const { return Bitwidth; } |
| 194 | unsigned getElementSizeInBits() const { return ElementBitwidth; } |
| 195 | unsigned getNumVectors() const { return NumVectors; } |
| 196 | |
| 197 | // |
| 198 | // Mutator functions |
| 199 | // |
| 200 | void makeUnsigned() { Signed = false; } |
| 201 | void makeSigned() { Signed = true; } |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 202 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 203 | void makeInteger(unsigned ElemWidth, bool Sign) { |
| 204 | Float = false; |
| 205 | Poly = false; |
| 206 | Signed = Sign; |
Ahmed Bougacha | 94df730 | 2015-06-04 01:43:41 +0000 | [diff] [blame] | 207 | Immediate = false; |
| 208 | ElementBitwidth = ElemWidth; |
| 209 | } |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 210 | |
Ahmed Bougacha | 94df730 | 2015-06-04 01:43:41 +0000 | [diff] [blame] | 211 | void makeImmediate(unsigned ElemWidth) { |
| 212 | Float = false; |
| 213 | Poly = false; |
| 214 | Signed = true; |
| 215 | Immediate = true; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 216 | ElementBitwidth = ElemWidth; |
| 217 | } |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 218 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 219 | void makeScalar() { |
| 220 | Bitwidth = ElementBitwidth; |
| 221 | NumVectors = 0; |
| 222 | } |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 223 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 224 | void makeOneVector() { |
| 225 | assert(isVector()); |
| 226 | NumVectors = 1; |
| 227 | } |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 228 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 229 | void doubleLanes() { |
| 230 | assert_with_loc(Bitwidth != 128, "Can't get bigger than 128!"); |
| 231 | Bitwidth = 128; |
| 232 | } |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 233 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 234 | void halveLanes() { |
| 235 | assert_with_loc(Bitwidth != 64, "Can't get smaller than 64!"); |
| 236 | Bitwidth = 64; |
| 237 | } |
| 238 | |
| 239 | /// Return the C string representation of a type, which is the typename |
| 240 | /// defined in stdint.h or arm_neon.h. |
| 241 | std::string str() const; |
| 242 | |
| 243 | /// Return the string representation of a type, which is an encoded |
| 244 | /// string for passing to the BUILTIN() macro in Builtins.def. |
| 245 | std::string builtin_str() const; |
| 246 | |
| 247 | /// Return the value in NeonTypeFlags for this type. |
| 248 | unsigned getNeonEnum() const; |
| 249 | |
| 250 | /// Parse a type from a stdint.h or arm_neon.h typedef name, |
| 251 | /// for example uint32x2_t or int64_t. |
| 252 | static Type fromTypedefName(StringRef Name); |
| 253 | |
| 254 | private: |
| 255 | /// Creates the type based on the typespec string in TS. |
| 256 | /// Sets "Quad" to true if the "Q" or "H" modifiers were |
| 257 | /// seen. This is needed by applyModifier as some modifiers |
| 258 | /// only take effect if the type size was changed by "Q" or "H". |
| 259 | void applyTypespec(bool &Quad); |
| 260 | /// Applies a prototype modifier to the type. |
| 261 | void applyModifier(char Mod); |
| 262 | }; |
| 263 | |
| 264 | //===----------------------------------------------------------------------===// |
| 265 | // Variable |
| 266 | //===----------------------------------------------------------------------===// |
| 267 | |
| 268 | /// A variable is a simple class that just has a type and a name. |
| 269 | class Variable { |
| 270 | Type T; |
| 271 | std::string N; |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 272 | |
| 273 | public: |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 274 | Variable() : T(Type::getVoid()), N("") {} |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 275 | Variable(Type T, std::string N) : T(std::move(T)), N(std::move(N)) {} |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 276 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 277 | Type getType() const { return T; } |
| 278 | std::string getName() const { return "__" + N; } |
| 279 | }; |
| 280 | |
| 281 | //===----------------------------------------------------------------------===// |
| 282 | // Intrinsic |
| 283 | //===----------------------------------------------------------------------===// |
| 284 | |
| 285 | /// The main grunt class. This represents an instantiation of an intrinsic with |
| 286 | /// a particular typespec and prototype. |
| 287 | class Intrinsic { |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 288 | friend class DagEmitter; |
| 289 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 290 | /// The Record this intrinsic was created from. |
| 291 | Record *R; |
| 292 | /// The unmangled name and prototype. |
| 293 | std::string Name, Proto; |
| 294 | /// The input and output typespecs. InTS == OutTS except when |
| 295 | /// CartesianProductOfTypes is 1 - this is the case for vreinterpret. |
| 296 | TypeSpec OutTS, InTS; |
| 297 | /// The base class kind. Most intrinsics use ClassS, which has full type |
| 298 | /// info for integers (s32/u32). Some use ClassI, which doesn't care about |
| 299 | /// signedness (i32), while some (ClassB) have no type at all, only a width |
| 300 | /// (32). |
| 301 | ClassKind CK; |
| 302 | /// The list of DAGs for the body. May be empty, in which case we should |
| 303 | /// emit a builtin call. |
| 304 | ListInit *Body; |
| 305 | /// The architectural #ifdef guard. |
| 306 | std::string Guard; |
| 307 | /// Set if the Unvailable bit is 1. This means we don't generate a body, |
| 308 | /// just an "unavailable" attribute on a declaration. |
| 309 | bool IsUnavailable; |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 310 | /// Is this intrinsic safe for big-endian? or does it need its arguments |
| 311 | /// reversing? |
| 312 | bool BigEndianSafe; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 313 | |
| 314 | /// The types of return value [0] and parameters [1..]. |
| 315 | std::vector<Type> Types; |
| 316 | /// The local variables defined. |
| 317 | std::map<std::string, Variable> Variables; |
| 318 | /// NeededEarly - set if any other intrinsic depends on this intrinsic. |
| 319 | bool NeededEarly; |
| 320 | /// UseMacro - set if we should implement using a macro or unset for a |
| 321 | /// function. |
| 322 | bool UseMacro; |
| 323 | /// The set of intrinsics that this intrinsic uses/requires. |
| 324 | std::set<Intrinsic *> Dependencies; |
| 325 | /// The "base type", which is Type('d', OutTS). InBaseType is only |
| 326 | /// different if CartesianProductOfTypes = 1 (for vreinterpret). |
| 327 | Type BaseType, InBaseType; |
| 328 | /// The return variable. |
| 329 | Variable RetVar; |
| 330 | /// A postfix to apply to every variable. Defaults to "". |
| 331 | std::string VariablePostfix; |
| 332 | |
| 333 | NeonEmitter &Emitter; |
| 334 | std::stringstream OS; |
| 335 | |
| 336 | public: |
| 337 | Intrinsic(Record *R, StringRef Name, StringRef Proto, TypeSpec OutTS, |
| 338 | TypeSpec InTS, ClassKind CK, ListInit *Body, NeonEmitter &Emitter, |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 339 | StringRef Guard, bool IsUnavailable, bool BigEndianSafe) |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 340 | : R(R), Name(Name.str()), Proto(Proto.str()), OutTS(OutTS), InTS(InTS), |
| 341 | CK(CK), Body(Body), Guard(Guard.str()), IsUnavailable(IsUnavailable), |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 342 | BigEndianSafe(BigEndianSafe), NeededEarly(false), UseMacro(false), |
| 343 | BaseType(OutTS, 'd'), InBaseType(InTS, 'd'), Emitter(Emitter) { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 344 | // If this builtin takes an immediate argument, we need to #define it rather |
| 345 | // than use a standard declaration, so that SemaChecking can range check |
| 346 | // the immediate passed by the user. |
| 347 | if (Proto.find('i') != std::string::npos) |
| 348 | UseMacro = true; |
| 349 | |
| 350 | // Pointer arguments need to use macros to avoid hiding aligned attributes |
| 351 | // from the pointer type. |
| 352 | if (Proto.find('p') != std::string::npos || |
| 353 | Proto.find('c') != std::string::npos) |
| 354 | UseMacro = true; |
| 355 | |
| 356 | // It is not permitted to pass or return an __fp16 by value, so intrinsics |
| 357 | // taking a scalar float16_t must be implemented as macros. |
| 358 | if (OutTS.find('h') != std::string::npos && |
| 359 | Proto.find('s') != std::string::npos) |
| 360 | UseMacro = true; |
| 361 | |
| 362 | // Modify the TypeSpec per-argument to get a concrete Type, and create |
| 363 | // known variables for each. |
| 364 | // Types[0] is the return value. |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 365 | Types.emplace_back(OutTS, Proto[0]); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 366 | for (unsigned I = 1; I < Proto.size(); ++I) |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 367 | Types.emplace_back(InTS, Proto[I]); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /// Get the Record that this intrinsic is based off. |
| 371 | Record *getRecord() const { return R; } |
| 372 | /// Get the set of Intrinsics that this intrinsic calls. |
| 373 | /// this is the set of immediate dependencies, NOT the |
| 374 | /// transitive closure. |
| 375 | const std::set<Intrinsic *> &getDependencies() const { return Dependencies; } |
| 376 | /// Get the architectural guard string (#ifdef). |
| 377 | std::string getGuard() const { return Guard; } |
| 378 | /// Get the non-mangled name. |
| 379 | std::string getName() const { return Name; } |
| 380 | |
| 381 | /// Return true if the intrinsic takes an immediate operand. |
| 382 | bool hasImmediate() const { |
| 383 | return Proto.find('i') != std::string::npos; |
| 384 | } |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 385 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 386 | /// Return the parameter index of the immediate operand. |
| 387 | unsigned getImmediateIdx() const { |
| 388 | assert(hasImmediate()); |
| 389 | unsigned Idx = Proto.find('i'); |
| 390 | assert(Idx > 0 && "Can't return an immediate!"); |
| 391 | return Idx - 1; |
| 392 | } |
| 393 | |
| 394 | /// Return true if the intrinsic takes an splat operand. |
| 395 | bool hasSplat() const { return Proto.find('a') != std::string::npos; } |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 396 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 397 | /// Return the parameter index of the splat operand. |
| 398 | unsigned getSplatIdx() const { |
| 399 | assert(hasSplat()); |
| 400 | unsigned Idx = Proto.find('a'); |
| 401 | assert(Idx > 0 && "Can't return a splat!"); |
| 402 | return Idx - 1; |
| 403 | } |
| 404 | |
| 405 | unsigned getNumParams() const { return Proto.size() - 1; } |
| 406 | Type getReturnType() const { return Types[0]; } |
| 407 | Type getParamType(unsigned I) const { return Types[I + 1]; } |
| 408 | Type getBaseType() const { return BaseType; } |
| 409 | /// Return the raw prototype string. |
| 410 | std::string getProto() const { return Proto; } |
| 411 | |
| 412 | /// Return true if the prototype has a scalar argument. |
| 413 | /// This does not return true for the "splat" code ('a'). |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 414 | bool protoHasScalar() const; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 415 | |
| 416 | /// Return the index that parameter PIndex will sit at |
| 417 | /// in a generated function call. This is often just PIndex, |
| 418 | /// but may not be as things such as multiple-vector operands |
| 419 | /// and sret parameters need to be taken into accont. |
| 420 | unsigned getGeneratedParamIdx(unsigned PIndex) { |
| 421 | unsigned Idx = 0; |
| 422 | if (getReturnType().getNumVectors() > 1) |
| 423 | // Multiple vectors are passed as sret. |
| 424 | ++Idx; |
| 425 | |
| 426 | for (unsigned I = 0; I < PIndex; ++I) |
| 427 | Idx += std::max(1U, getParamType(I).getNumVectors()); |
| 428 | |
| 429 | return Idx; |
| 430 | } |
| 431 | |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 432 | bool hasBody() const { return Body && !Body->getValues().empty(); } |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 433 | |
| 434 | void setNeededEarly() { NeededEarly = true; } |
| 435 | |
| 436 | bool operator<(const Intrinsic &Other) const { |
| 437 | // Sort lexicographically on a two-tuple (Guard, Name) |
| 438 | if (Guard != Other.Guard) |
| 439 | return Guard < Other.Guard; |
| 440 | return Name < Other.Name; |
| 441 | } |
| 442 | |
| 443 | ClassKind getClassKind(bool UseClassBIfScalar = false) { |
| 444 | if (UseClassBIfScalar && !protoHasScalar()) |
| 445 | return ClassB; |
| 446 | return CK; |
| 447 | } |
| 448 | |
| 449 | /// Return the name, mangled with type information. |
| 450 | /// If ForceClassS is true, use ClassS (u32/s32) instead |
| 451 | /// of the intrinsic's own type class. |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 452 | std::string getMangledName(bool ForceClassS = false) const; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 453 | /// Return the type code for a builtin function call. |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 454 | std::string getInstTypeCode(Type T, ClassKind CK) const; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 455 | /// Return the type string for a BUILTIN() macro in Builtins.def. |
| 456 | std::string getBuiltinTypeStr(); |
| 457 | |
| 458 | /// Generate the intrinsic, returning code. |
| 459 | std::string generate(); |
| 460 | /// Perform type checking and populate the dependency graph, but |
| 461 | /// don't generate code yet. |
| 462 | void indexBody(); |
| 463 | |
| 464 | private: |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 465 | std::string mangleName(std::string Name, ClassKind CK) const; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 466 | |
| 467 | void initVariables(); |
| 468 | std::string replaceParamsIn(std::string S); |
| 469 | |
| 470 | void emitBodyAsBuiltinCall(); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 471 | |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 472 | void generateImpl(bool ReverseArguments, |
| 473 | StringRef NamePrefix, StringRef CallPrefix); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 474 | void emitReturn(); |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 475 | void emitBody(StringRef CallPrefix); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 476 | void emitShadowedArgs(); |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 477 | void emitArgumentReversal(); |
| 478 | void emitReturnReversal(); |
| 479 | void emitReverseVariable(Variable &Dest, Variable &Src); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 480 | void emitNewLine(); |
| 481 | void emitClosingBrace(); |
| 482 | void emitOpeningBrace(); |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 483 | void emitPrototype(StringRef NamePrefix); |
| 484 | |
| 485 | class DagEmitter { |
| 486 | Intrinsic &Intr; |
| 487 | StringRef CallPrefix; |
| 488 | |
| 489 | public: |
| 490 | DagEmitter(Intrinsic &Intr, StringRef CallPrefix) : |
| 491 | Intr(Intr), CallPrefix(CallPrefix) { |
| 492 | } |
| 493 | std::pair<Type, std::string> emitDagArg(Init *Arg, std::string ArgName); |
| 494 | std::pair<Type, std::string> emitDagSaveTemp(DagInit *DI); |
| 495 | std::pair<Type, std::string> emitDagSplat(DagInit *DI); |
| 496 | std::pair<Type, std::string> emitDagDup(DagInit *DI); |
| 497 | std::pair<Type, std::string> emitDagShuffle(DagInit *DI); |
| 498 | std::pair<Type, std::string> emitDagCast(DagInit *DI, bool IsBitCast); |
| 499 | std::pair<Type, std::string> emitDagCall(DagInit *DI); |
| 500 | std::pair<Type, std::string> emitDagNameReplace(DagInit *DI); |
| 501 | std::pair<Type, std::string> emitDagLiteral(DagInit *DI); |
| 502 | std::pair<Type, std::string> emitDagOp(DagInit *DI); |
| 503 | std::pair<Type, std::string> emitDag(DagInit *DI); |
| 504 | }; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 505 | }; |
| 506 | |
| 507 | //===----------------------------------------------------------------------===// |
| 508 | // NeonEmitter |
| 509 | //===----------------------------------------------------------------------===// |
| 510 | |
| 511 | class NeonEmitter { |
| 512 | RecordKeeper &Records; |
| 513 | DenseMap<Record *, ClassKind> ClassMap; |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 514 | std::map<std::string, std::deque<Intrinsic>> IntrinsicMap; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 515 | unsigned UniqueNumber; |
| 516 | |
| 517 | void createIntrinsic(Record *R, SmallVectorImpl<Intrinsic *> &Out); |
| 518 | void genBuiltinsDef(raw_ostream &OS, SmallVectorImpl<Intrinsic *> &Defs); |
| 519 | void genOverloadTypeCheckCode(raw_ostream &OS, |
| 520 | SmallVectorImpl<Intrinsic *> &Defs); |
| 521 | void genIntrinsicRangeCheckCode(raw_ostream &OS, |
| 522 | SmallVectorImpl<Intrinsic *> &Defs); |
| 523 | |
| 524 | public: |
| 525 | /// Called by Intrinsic - this attempts to get an intrinsic that takes |
| 526 | /// the given types as arguments. |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 527 | Intrinsic &getIntrinsic(StringRef Name, ArrayRef<Type> Types); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 528 | |
| 529 | /// Called by Intrinsic - returns a globally-unique number. |
| 530 | unsigned getUniqueNumber() { return UniqueNumber++; } |
| 531 | |
| 532 | NeonEmitter(RecordKeeper &R) : Records(R), UniqueNumber(0) { |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 533 | Record *SI = R.getClass("SInst"); |
| 534 | Record *II = R.getClass("IInst"); |
| 535 | Record *WI = R.getClass("WInst"); |
Michael Gottesman | fc89cc2 | 2013-04-16 21:18:42 +0000 | [diff] [blame] | 536 | Record *SOpI = R.getClass("SOpInst"); |
| 537 | Record *IOpI = R.getClass("IOpInst"); |
| 538 | Record *WOpI = R.getClass("WOpInst"); |
| 539 | Record *LOpI = R.getClass("LOpInst"); |
| 540 | Record *NoTestOpI = R.getClass("NoTestOpInst"); |
| 541 | |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 542 | ClassMap[SI] = ClassS; |
| 543 | ClassMap[II] = ClassI; |
| 544 | ClassMap[WI] = ClassW; |
Michael Gottesman | fc89cc2 | 2013-04-16 21:18:42 +0000 | [diff] [blame] | 545 | ClassMap[SOpI] = ClassS; |
| 546 | ClassMap[IOpI] = ClassI; |
| 547 | ClassMap[WOpI] = ClassW; |
| 548 | ClassMap[LOpI] = ClassL; |
| 549 | ClassMap[NoTestOpI] = ClassNoTest; |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | // run - Emit arm_neon.h.inc |
| 553 | void run(raw_ostream &o); |
| 554 | |
| 555 | // runHeader - Emit all the __builtin prototypes used in arm_neon.h |
| 556 | void runHeader(raw_ostream &o); |
| 557 | |
| 558 | // runTests - Emit tests for all the Neon intrinsics. |
| 559 | void runTests(raw_ostream &o); |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 560 | }; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 561 | |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 562 | } // end anonymous namespace |
| 563 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 564 | //===----------------------------------------------------------------------===// |
| 565 | // Type implementation |
| 566 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 567 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 568 | std::string Type::str() const { |
| 569 | if (Void) |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 570 | return "void"; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 571 | std::string S; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 572 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 573 | if (!Signed && isInteger()) |
| 574 | S += "u"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 575 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 576 | if (Poly) |
| 577 | S += "poly"; |
| 578 | else if (Float) |
| 579 | S += "float"; |
| 580 | else |
| 581 | S += "int"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 582 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 583 | S += utostr(ElementBitwidth); |
| 584 | if (isVector()) |
| 585 | S += "x" + utostr(getNumElements()); |
| 586 | if (NumVectors > 1) |
| 587 | S += "x" + utostr(NumVectors); |
| 588 | S += "_t"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 589 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 590 | if (Constant) |
| 591 | S += " const"; |
| 592 | if (Pointer) |
| 593 | S += " *"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 594 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 595 | return S; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 596 | } |
| 597 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 598 | std::string Type::builtin_str() const { |
| 599 | std::string S; |
| 600 | if (isVoid()) |
| 601 | return "v"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 602 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 603 | if (Pointer) |
| 604 | // All pointers are void pointers. |
| 605 | S += "v"; |
| 606 | else if (isInteger()) |
| 607 | switch (ElementBitwidth) { |
| 608 | case 8: S += "c"; break; |
| 609 | case 16: S += "s"; break; |
| 610 | case 32: S += "i"; break; |
| 611 | case 64: S += "Wi"; break; |
| 612 | case 128: S += "LLLi"; break; |
Craig Topper | 0039f3f | 2014-06-18 03:57:25 +0000 | [diff] [blame] | 613 | default: llvm_unreachable("Unhandled case!"); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 614 | } |
| 615 | else |
| 616 | switch (ElementBitwidth) { |
| 617 | case 16: S += "h"; break; |
| 618 | case 32: S += "f"; break; |
| 619 | case 64: S += "d"; break; |
Craig Topper | 0039f3f | 2014-06-18 03:57:25 +0000 | [diff] [blame] | 620 | default: llvm_unreachable("Unhandled case!"); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 621 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 622 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 623 | if (isChar() && !Pointer) |
| 624 | // Make chars explicitly signed. |
| 625 | S = "S" + S; |
| 626 | else if (isInteger() && !Pointer && !Signed) |
| 627 | S = "U" + S; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 628 | |
Ahmed Bougacha | 94df730 | 2015-06-04 01:43:41 +0000 | [diff] [blame] | 629 | // Constant indices are "int", but have the "constant expression" modifier. |
| 630 | if (isImmediate()) { |
| 631 | assert(isInteger() && isSigned()); |
| 632 | S = "I" + S; |
| 633 | } |
| 634 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 635 | if (isScalar()) { |
| 636 | if (Constant) S += "C"; |
| 637 | if (Pointer) S += "*"; |
| 638 | return S; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 639 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 640 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 641 | std::string Ret; |
| 642 | for (unsigned I = 0; I < NumVectors; ++I) |
| 643 | Ret += "V" + utostr(getNumElements()) + S; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 644 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 645 | return Ret; |
| 646 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 647 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 648 | unsigned Type::getNeonEnum() const { |
| 649 | unsigned Addend; |
| 650 | switch (ElementBitwidth) { |
| 651 | case 8: Addend = 0; break; |
| 652 | case 16: Addend = 1; break; |
| 653 | case 32: Addend = 2; break; |
| 654 | case 64: Addend = 3; break; |
| 655 | case 128: Addend = 4; break; |
Craig Topper | c7193c4 | 2014-06-18 03:13:41 +0000 | [diff] [blame] | 656 | default: llvm_unreachable("Unhandled element bitwidth!"); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 657 | } |
| 658 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 659 | unsigned Base = (unsigned)NeonTypeFlags::Int8 + Addend; |
| 660 | if (Poly) { |
| 661 | // Adjustment needed because Poly32 doesn't exist. |
| 662 | if (Addend >= 2) |
| 663 | --Addend; |
| 664 | Base = (unsigned)NeonTypeFlags::Poly8 + Addend; |
| 665 | } |
| 666 | if (Float) { |
| 667 | assert(Addend != 0 && "Float8 doesn't exist!"); |
| 668 | Base = (unsigned)NeonTypeFlags::Float16 + (Addend - 1); |
| 669 | } |
| 670 | |
| 671 | if (Bitwidth == 128) |
| 672 | Base |= (unsigned)NeonTypeFlags::QuadFlag; |
| 673 | if (isInteger() && !Signed) |
| 674 | Base |= (unsigned)NeonTypeFlags::UnsignedFlag; |
| 675 | |
| 676 | return Base; |
| 677 | } |
| 678 | |
| 679 | Type Type::fromTypedefName(StringRef Name) { |
| 680 | Type T; |
| 681 | T.Void = false; |
| 682 | T.Float = false; |
| 683 | T.Poly = false; |
| 684 | |
| 685 | if (Name.front() == 'u') { |
| 686 | T.Signed = false; |
| 687 | Name = Name.drop_front(); |
| 688 | } else { |
| 689 | T.Signed = true; |
| 690 | } |
| 691 | |
| 692 | if (Name.startswith("float")) { |
| 693 | T.Float = true; |
| 694 | Name = Name.drop_front(5); |
| 695 | } else if (Name.startswith("poly")) { |
| 696 | T.Poly = true; |
| 697 | Name = Name.drop_front(4); |
| 698 | } else { |
| 699 | assert(Name.startswith("int")); |
| 700 | Name = Name.drop_front(3); |
| 701 | } |
| 702 | |
| 703 | unsigned I = 0; |
| 704 | for (I = 0; I < Name.size(); ++I) { |
| 705 | if (!isdigit(Name[I])) |
| 706 | break; |
| 707 | } |
| 708 | Name.substr(0, I).getAsInteger(10, T.ElementBitwidth); |
| 709 | Name = Name.drop_front(I); |
| 710 | |
| 711 | T.Bitwidth = T.ElementBitwidth; |
| 712 | T.NumVectors = 1; |
| 713 | |
| 714 | if (Name.front() == 'x') { |
| 715 | Name = Name.drop_front(); |
| 716 | unsigned I = 0; |
| 717 | for (I = 0; I < Name.size(); ++I) { |
| 718 | if (!isdigit(Name[I])) |
| 719 | break; |
| 720 | } |
| 721 | unsigned NumLanes; |
| 722 | Name.substr(0, I).getAsInteger(10, NumLanes); |
| 723 | Name = Name.drop_front(I); |
| 724 | T.Bitwidth = T.ElementBitwidth * NumLanes; |
| 725 | } else { |
| 726 | // Was scalar. |
| 727 | T.NumVectors = 0; |
| 728 | } |
| 729 | if (Name.front() == 'x') { |
| 730 | Name = Name.drop_front(); |
| 731 | unsigned I = 0; |
| 732 | for (I = 0; I < Name.size(); ++I) { |
| 733 | if (!isdigit(Name[I])) |
| 734 | break; |
| 735 | } |
| 736 | Name.substr(0, I).getAsInteger(10, T.NumVectors); |
| 737 | Name = Name.drop_front(I); |
| 738 | } |
| 739 | |
| 740 | assert(Name.startswith("_t") && "Malformed typedef!"); |
| 741 | return T; |
| 742 | } |
| 743 | |
| 744 | void Type::applyTypespec(bool &Quad) { |
| 745 | std::string S = TS; |
| 746 | ScalarForMangling = false; |
| 747 | Void = false; |
| 748 | Poly = Float = false; |
| 749 | ElementBitwidth = ~0U; |
| 750 | Signed = true; |
| 751 | NumVectors = 1; |
| 752 | |
| 753 | for (char I : S) { |
| 754 | switch (I) { |
| 755 | case 'S': |
| 756 | ScalarForMangling = true; |
| 757 | break; |
| 758 | case 'H': |
| 759 | NoManglingQ = true; |
| 760 | Quad = true; |
| 761 | break; |
| 762 | case 'Q': |
| 763 | Quad = true; |
| 764 | break; |
| 765 | case 'P': |
| 766 | Poly = true; |
| 767 | break; |
| 768 | case 'U': |
| 769 | Signed = false; |
| 770 | break; |
| 771 | case 'c': |
| 772 | ElementBitwidth = 8; |
| 773 | break; |
| 774 | case 'h': |
| 775 | Float = true; |
| 776 | // Fall through |
| 777 | case 's': |
| 778 | ElementBitwidth = 16; |
| 779 | break; |
| 780 | case 'f': |
| 781 | Float = true; |
| 782 | // Fall through |
| 783 | case 'i': |
| 784 | ElementBitwidth = 32; |
| 785 | break; |
| 786 | case 'd': |
| 787 | Float = true; |
| 788 | // Fall through |
| 789 | case 'l': |
| 790 | ElementBitwidth = 64; |
| 791 | break; |
| 792 | case 'k': |
| 793 | ElementBitwidth = 128; |
| 794 | // Poly doesn't have a 128x1 type. |
| 795 | if (Poly) |
| 796 | NumVectors = 0; |
| 797 | break; |
| 798 | default: |
Craig Topper | 0039f3f | 2014-06-18 03:57:25 +0000 | [diff] [blame] | 799 | llvm_unreachable("Unhandled type code!"); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | assert(ElementBitwidth != ~0U && "Bad element bitwidth!"); |
| 803 | |
| 804 | Bitwidth = Quad ? 128 : 64; |
| 805 | } |
| 806 | |
| 807 | void Type::applyModifier(char Mod) { |
| 808 | bool AppliedQuad = false; |
| 809 | applyTypespec(AppliedQuad); |
| 810 | |
| 811 | switch (Mod) { |
| 812 | case 'v': |
| 813 | Void = true; |
| 814 | break; |
| 815 | case 't': |
| 816 | if (Poly) { |
| 817 | Poly = false; |
| 818 | Signed = false; |
| 819 | } |
| 820 | break; |
| 821 | case 'b': |
| 822 | Signed = false; |
| 823 | Float = false; |
| 824 | Poly = false; |
| 825 | NumVectors = 0; |
| 826 | Bitwidth = ElementBitwidth; |
| 827 | break; |
| 828 | case '$': |
| 829 | Signed = true; |
| 830 | Float = false; |
| 831 | Poly = false; |
| 832 | NumVectors = 0; |
| 833 | Bitwidth = ElementBitwidth; |
| 834 | break; |
| 835 | case 'u': |
| 836 | Signed = false; |
| 837 | Poly = false; |
| 838 | Float = false; |
| 839 | break; |
| 840 | case 'x': |
| 841 | Signed = true; |
| 842 | assert(!Poly && "'u' can't be used with poly types!"); |
| 843 | Float = false; |
| 844 | break; |
| 845 | case 'o': |
| 846 | Bitwidth = ElementBitwidth = 64; |
| 847 | NumVectors = 0; |
| 848 | Float = true; |
| 849 | break; |
| 850 | case 'y': |
| 851 | Bitwidth = ElementBitwidth = 32; |
| 852 | NumVectors = 0; |
| 853 | Float = true; |
| 854 | break; |
| 855 | case 'f': |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 856 | Float = true; |
| 857 | ElementBitwidth = 32; |
| 858 | break; |
| 859 | case 'F': |
| 860 | Float = true; |
| 861 | ElementBitwidth = 64; |
| 862 | break; |
| 863 | case 'g': |
| 864 | if (AppliedQuad) |
| 865 | Bitwidth /= 2; |
| 866 | break; |
| 867 | case 'j': |
| 868 | if (!AppliedQuad) |
| 869 | Bitwidth *= 2; |
| 870 | break; |
| 871 | case 'w': |
| 872 | ElementBitwidth *= 2; |
| 873 | Bitwidth *= 2; |
| 874 | break; |
| 875 | case 'n': |
| 876 | ElementBitwidth *= 2; |
| 877 | break; |
| 878 | case 'i': |
| 879 | Float = false; |
| 880 | Poly = false; |
| 881 | ElementBitwidth = Bitwidth = 32; |
| 882 | NumVectors = 0; |
| 883 | Signed = true; |
Ahmed Bougacha | 94df730 | 2015-06-04 01:43:41 +0000 | [diff] [blame] | 884 | Immediate = true; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 885 | break; |
| 886 | case 'l': |
| 887 | Float = false; |
| 888 | Poly = false; |
| 889 | ElementBitwidth = Bitwidth = 64; |
| 890 | NumVectors = 0; |
| 891 | Signed = false; |
Ahmed Bougacha | 94df730 | 2015-06-04 01:43:41 +0000 | [diff] [blame] | 892 | Immediate = true; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 893 | break; |
| 894 | case 'z': |
| 895 | ElementBitwidth /= 2; |
| 896 | Bitwidth = ElementBitwidth; |
| 897 | NumVectors = 0; |
| 898 | break; |
| 899 | case 'r': |
| 900 | ElementBitwidth *= 2; |
| 901 | Bitwidth = ElementBitwidth; |
| 902 | NumVectors = 0; |
| 903 | break; |
| 904 | case 's': |
| 905 | case 'a': |
| 906 | Bitwidth = ElementBitwidth; |
| 907 | NumVectors = 0; |
| 908 | break; |
| 909 | case 'k': |
| 910 | Bitwidth *= 2; |
| 911 | break; |
| 912 | case 'c': |
| 913 | Constant = true; |
| 914 | // Fall through |
| 915 | case 'p': |
| 916 | Pointer = true; |
| 917 | Bitwidth = ElementBitwidth; |
| 918 | NumVectors = 0; |
| 919 | break; |
| 920 | case 'h': |
| 921 | ElementBitwidth /= 2; |
| 922 | break; |
| 923 | case 'q': |
| 924 | ElementBitwidth /= 2; |
| 925 | Bitwidth *= 2; |
| 926 | break; |
| 927 | case 'e': |
| 928 | ElementBitwidth /= 2; |
| 929 | Signed = false; |
| 930 | break; |
| 931 | case 'm': |
| 932 | ElementBitwidth /= 2; |
| 933 | Bitwidth /= 2; |
| 934 | break; |
| 935 | case 'd': |
| 936 | break; |
| 937 | case '2': |
| 938 | NumVectors = 2; |
| 939 | break; |
| 940 | case '3': |
| 941 | NumVectors = 3; |
| 942 | break; |
| 943 | case '4': |
| 944 | NumVectors = 4; |
| 945 | break; |
| 946 | case 'B': |
| 947 | NumVectors = 2; |
| 948 | if (!AppliedQuad) |
| 949 | Bitwidth *= 2; |
| 950 | break; |
| 951 | case 'C': |
| 952 | NumVectors = 3; |
| 953 | if (!AppliedQuad) |
| 954 | Bitwidth *= 2; |
| 955 | break; |
| 956 | case 'D': |
| 957 | NumVectors = 4; |
| 958 | if (!AppliedQuad) |
| 959 | Bitwidth *= 2; |
| 960 | break; |
| 961 | default: |
Craig Topper | 0039f3f | 2014-06-18 03:57:25 +0000 | [diff] [blame] | 962 | llvm_unreachable("Unhandled character!"); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 963 | } |
| 964 | } |
| 965 | |
| 966 | //===----------------------------------------------------------------------===// |
| 967 | // Intrinsic implementation |
| 968 | //===----------------------------------------------------------------------===// |
| 969 | |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 970 | std::string Intrinsic::getInstTypeCode(Type T, ClassKind CK) const { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 971 | char typeCode = '\0'; |
| 972 | bool printNumber = true; |
| 973 | |
| 974 | if (CK == ClassB) |
| 975 | return ""; |
| 976 | |
| 977 | if (T.isPoly()) |
| 978 | typeCode = 'p'; |
| 979 | else if (T.isInteger()) |
| 980 | typeCode = T.isSigned() ? 's' : 'u'; |
| 981 | else |
| 982 | typeCode = 'f'; |
| 983 | |
| 984 | if (CK == ClassI) { |
| 985 | switch (typeCode) { |
| 986 | default: |
| 987 | break; |
| 988 | case 's': |
| 989 | case 'u': |
| 990 | case 'p': |
| 991 | typeCode = 'i'; |
| 992 | break; |
| 993 | } |
| 994 | } |
| 995 | if (CK == ClassB) { |
| 996 | typeCode = '\0'; |
| 997 | } |
| 998 | |
| 999 | std::string S; |
| 1000 | if (typeCode != '\0') |
| 1001 | S.push_back(typeCode); |
| 1002 | if (printNumber) |
| 1003 | S += utostr(T.getElementSizeInBits()); |
| 1004 | |
| 1005 | return S; |
| 1006 | } |
| 1007 | |
Ahmed Bougacha | 22a1696 | 2015-08-21 23:24:18 +0000 | [diff] [blame] | 1008 | static bool isFloatingPointProtoModifier(char Mod) { |
Vedant Kumar | a44a6ac | 2017-06-02 01:22:14 +0000 | [diff] [blame^] | 1009 | return Mod == 'F' || Mod == 'f'; |
Ahmed Bougacha | 22a1696 | 2015-08-21 23:24:18 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1012 | std::string Intrinsic::getBuiltinTypeStr() { |
| 1013 | ClassKind LocalCK = getClassKind(true); |
| 1014 | std::string S; |
| 1015 | |
| 1016 | Type RetT = getReturnType(); |
| 1017 | if ((LocalCK == ClassI || LocalCK == ClassW) && RetT.isScalar() && |
| 1018 | !RetT.isFloating()) |
| 1019 | RetT.makeInteger(RetT.getElementSizeInBits(), false); |
| 1020 | |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1021 | // Since the return value must be one type, return a vector type of the |
| 1022 | // appropriate width which we will bitcast. An exception is made for |
| 1023 | // returning structs of 2, 3, or 4 vectors which are returned in a sret-like |
| 1024 | // fashion, storing them to a pointer arg. |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1025 | if (RetT.getNumVectors() > 1) { |
| 1026 | S += "vv*"; // void result with void* first argument |
| 1027 | } else { |
| 1028 | if (RetT.isPoly()) |
| 1029 | RetT.makeInteger(RetT.getElementSizeInBits(), false); |
| 1030 | if (!RetT.isScalar() && !RetT.isSigned()) |
| 1031 | RetT.makeSigned(); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1032 | |
Ahmed Bougacha | 22a1696 | 2015-08-21 23:24:18 +0000 | [diff] [blame] | 1033 | bool ForcedVectorFloatingType = isFloatingPointProtoModifier(Proto[0]); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1034 | if (LocalCK == ClassB && !RetT.isScalar() && !ForcedVectorFloatingType) |
| 1035 | // Cast to vector of 8-bit elements. |
| 1036 | RetT.makeInteger(8, true); |
| 1037 | |
| 1038 | S += RetT.builtin_str(); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1041 | for (unsigned I = 0; I < getNumParams(); ++I) { |
| 1042 | Type T = getParamType(I); |
| 1043 | if (T.isPoly()) |
| 1044 | T.makeInteger(T.getElementSizeInBits(), false); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1045 | |
Ahmed Bougacha | 22a1696 | 2015-08-21 23:24:18 +0000 | [diff] [blame] | 1046 | bool ForcedFloatingType = isFloatingPointProtoModifier(Proto[I + 1]); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1047 | if (LocalCK == ClassB && !T.isScalar() && !ForcedFloatingType) |
| 1048 | T.makeInteger(8, true); |
| 1049 | // Halves always get converted to 8-bit elements. |
| 1050 | if (T.isHalf() && T.isVector() && !T.isScalarForMangling()) |
| 1051 | T.makeInteger(8, true); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1052 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1053 | if (LocalCK == ClassI) |
| 1054 | T.makeSigned(); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1055 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1056 | if (hasImmediate() && getImmediateIdx() == I) |
Ahmed Bougacha | 94df730 | 2015-06-04 01:43:41 +0000 | [diff] [blame] | 1057 | T.makeImmediate(32); |
Michael Gottesman | 095c58f | 2013-04-16 22:07:30 +0000 | [diff] [blame] | 1058 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1059 | S += T.builtin_str(); |
Michael Gottesman | 095c58f | 2013-04-16 22:07:30 +0000 | [diff] [blame] | 1060 | } |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1061 | |
| 1062 | // Extra constant integer to hold type class enum for this function, e.g. s8 |
| 1063 | if (LocalCK == ClassB) |
| 1064 | S += "i"; |
| 1065 | |
| 1066 | return S; |
Michael Gottesman | 095c58f | 2013-04-16 22:07:30 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 1069 | std::string Intrinsic::getMangledName(bool ForceClassS) const { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1070 | // Check if the prototype has a scalar operand with the type of the vector |
| 1071 | // elements. If not, bitcasting the args will take care of arg checking. |
| 1072 | // The actual signedness etc. will be taken care of with special enums. |
| 1073 | ClassKind LocalCK = CK; |
| 1074 | if (!protoHasScalar()) |
| 1075 | LocalCK = ClassB; |
| 1076 | |
| 1077 | return mangleName(Name, ForceClassS ? ClassS : LocalCK); |
Kevin Qin | c076d06 | 2013-08-29 07:55:15 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 1080 | std::string Intrinsic::mangleName(std::string Name, ClassKind LocalCK) const { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1081 | std::string typeCode = getInstTypeCode(BaseType, LocalCK); |
| 1082 | std::string S = Name; |
Hao Liu | 5e4ce1a | 2013-11-18 06:33:43 +0000 | [diff] [blame] | 1083 | |
Ahmed Bougacha | cd5b8a0 | 2015-08-21 23:34:20 +0000 | [diff] [blame] | 1084 | if (Name == "vcvt_f16_f32" || Name == "vcvt_f32_f16" || |
| 1085 | Name == "vcvt_f32_f64" || Name == "vcvt_f64_f32") |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1086 | return Name; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1087 | |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 1088 | if (!typeCode.empty()) { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1089 | // If the name ends with _xN (N = 2,3,4), insert the typeCode before _xN. |
| 1090 | if (Name.size() >= 3 && isdigit(Name.back()) && |
| 1091 | Name[Name.length() - 2] == 'x' && Name[Name.length() - 3] == '_') |
| 1092 | S.insert(S.length() - 3, "_" + typeCode); |
Hao Liu | 5e4ce1a | 2013-11-18 06:33:43 +0000 | [diff] [blame] | 1093 | else |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1094 | S += "_" + typeCode; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1095 | } |
Michael Gottesman | 095c58f | 2013-04-16 22:07:30 +0000 | [diff] [blame] | 1096 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1097 | if (BaseType != InBaseType) { |
| 1098 | // A reinterpret - out the input base type at the end. |
| 1099 | S += "_" + getInstTypeCode(InBaseType, LocalCK); |
| 1100 | } |
| 1101 | |
| 1102 | if (LocalCK == ClassB) |
| 1103 | S += "_v"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1104 | |
| 1105 | // Insert a 'q' before the first '_' character so that it ends up before |
| 1106 | // _lane or _n on vector-scalar operations. |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1107 | if (BaseType.getSizeInBits() == 128 && !BaseType.noManglingQ()) { |
| 1108 | size_t Pos = S.find('_'); |
| 1109 | S.insert(Pos, "q"); |
Kevin Qin | c076d06 | 2013-08-29 07:55:15 +0000 | [diff] [blame] | 1110 | } |
Michael Gottesman | 6cd3e56 | 2013-04-16 23:00:26 +0000 | [diff] [blame] | 1111 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1112 | char Suffix = '\0'; |
| 1113 | if (BaseType.isScalarForMangling()) { |
| 1114 | switch (BaseType.getElementSizeInBits()) { |
| 1115 | case 8: Suffix = 'b'; break; |
| 1116 | case 16: Suffix = 'h'; break; |
| 1117 | case 32: Suffix = 's'; break; |
| 1118 | case 64: Suffix = 'd'; break; |
Craig Topper | 0039f3f | 2014-06-18 03:57:25 +0000 | [diff] [blame] | 1119 | default: llvm_unreachable("Bad suffix!"); |
Michael Gottesman | 6cd3e56 | 2013-04-16 23:00:26 +0000 | [diff] [blame] | 1120 | } |
| 1121 | } |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1122 | if (Suffix != '\0') { |
| 1123 | size_t Pos = S.find('_'); |
| 1124 | S.insert(Pos, &Suffix, 1); |
Michael Gottesman | 6cd3e56 | 2013-04-16 23:00:26 +0000 | [diff] [blame] | 1125 | } |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1126 | |
| 1127 | return S; |
Michael Gottesman | 6cd3e56 | 2013-04-16 23:00:26 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1130 | std::string Intrinsic::replaceParamsIn(std::string S) { |
| 1131 | while (S.find('$') != std::string::npos) { |
| 1132 | size_t Pos = S.find('$'); |
| 1133 | size_t End = Pos + 1; |
| 1134 | while (isalpha(S[End])) |
| 1135 | ++End; |
Michael Gottesman | 6cd3e56 | 2013-04-16 23:00:26 +0000 | [diff] [blame] | 1136 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1137 | std::string VarName = S.substr(Pos + 1, End - Pos - 1); |
| 1138 | assert_with_loc(Variables.find(VarName) != Variables.end(), |
| 1139 | "Variable not defined!"); |
| 1140 | S.replace(Pos, End - Pos, Variables.find(VarName)->second.getName()); |
Michael Gottesman | 6cd3e56 | 2013-04-16 23:00:26 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1143 | return S; |
Michael Gottesman | 6cd3e56 | 2013-04-16 23:00:26 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1146 | void Intrinsic::initVariables() { |
| 1147 | Variables.clear(); |
Michael Gottesman | 6cd3e56 | 2013-04-16 23:00:26 +0000 | [diff] [blame] | 1148 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1149 | // Modify the TypeSpec per-argument to get a concrete Type, and create |
| 1150 | // known variables for each. |
| 1151 | for (unsigned I = 1; I < Proto.size(); ++I) { |
| 1152 | char NameC = '0' + (I - 1); |
| 1153 | std::string Name = "p"; |
| 1154 | Name.push_back(NameC); |
| 1155 | |
| 1156 | Variables[Name] = Variable(Types[I], Name + VariablePostfix); |
| 1157 | } |
| 1158 | RetVar = Variable(Types[0], "ret" + VariablePostfix); |
| 1159 | } |
| 1160 | |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1161 | void Intrinsic::emitPrototype(StringRef NamePrefix) { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1162 | if (UseMacro) |
| 1163 | OS << "#define "; |
| 1164 | else |
| 1165 | OS << "__ai " << Types[0].str() << " "; |
| 1166 | |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1167 | OS << NamePrefix.str() << mangleName(Name, ClassS) << "("; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1168 | |
| 1169 | for (unsigned I = 0; I < getNumParams(); ++I) { |
| 1170 | if (I != 0) |
| 1171 | OS << ", "; |
| 1172 | |
| 1173 | char NameC = '0' + I; |
| 1174 | std::string Name = "p"; |
| 1175 | Name.push_back(NameC); |
| 1176 | assert(Variables.find(Name) != Variables.end()); |
| 1177 | Variable &V = Variables[Name]; |
| 1178 | |
| 1179 | if (!UseMacro) |
| 1180 | OS << V.getType().str() << " "; |
| 1181 | OS << V.getName(); |
| 1182 | } |
| 1183 | |
| 1184 | OS << ")"; |
| 1185 | } |
| 1186 | |
| 1187 | void Intrinsic::emitOpeningBrace() { |
| 1188 | if (UseMacro) |
| 1189 | OS << " __extension__ ({"; |
| 1190 | else |
| 1191 | OS << " {"; |
| 1192 | emitNewLine(); |
| 1193 | } |
| 1194 | |
| 1195 | void Intrinsic::emitClosingBrace() { |
| 1196 | if (UseMacro) |
| 1197 | OS << "})"; |
| 1198 | else |
| 1199 | OS << "}"; |
| 1200 | } |
| 1201 | |
| 1202 | void Intrinsic::emitNewLine() { |
| 1203 | if (UseMacro) |
| 1204 | OS << " \\\n"; |
| 1205 | else |
| 1206 | OS << "\n"; |
| 1207 | } |
| 1208 | |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1209 | void Intrinsic::emitReverseVariable(Variable &Dest, Variable &Src) { |
| 1210 | if (Dest.getType().getNumVectors() > 1) { |
| 1211 | emitNewLine(); |
| 1212 | |
| 1213 | for (unsigned K = 0; K < Dest.getType().getNumVectors(); ++K) { |
Craig Topper | 054b391 | 2016-01-31 00:20:26 +0000 | [diff] [blame] | 1214 | OS << " " << Dest.getName() << ".val[" << K << "] = " |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1215 | << "__builtin_shufflevector(" |
Craig Topper | 054b391 | 2016-01-31 00:20:26 +0000 | [diff] [blame] | 1216 | << Src.getName() << ".val[" << K << "], " |
| 1217 | << Src.getName() << ".val[" << K << "]"; |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1218 | for (int J = Dest.getType().getNumElements() - 1; J >= 0; --J) |
Craig Topper | 054b391 | 2016-01-31 00:20:26 +0000 | [diff] [blame] | 1219 | OS << ", " << J; |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1220 | OS << ");"; |
| 1221 | emitNewLine(); |
| 1222 | } |
| 1223 | } else { |
| 1224 | OS << " " << Dest.getName() |
| 1225 | << " = __builtin_shufflevector(" << Src.getName() << ", " << Src.getName(); |
| 1226 | for (int J = Dest.getType().getNumElements() - 1; J >= 0; --J) |
Craig Topper | 054b391 | 2016-01-31 00:20:26 +0000 | [diff] [blame] | 1227 | OS << ", " << J; |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1228 | OS << ");"; |
| 1229 | emitNewLine(); |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | void Intrinsic::emitArgumentReversal() { |
| 1234 | if (BigEndianSafe) |
| 1235 | return; |
| 1236 | |
| 1237 | // Reverse all vector arguments. |
| 1238 | for (unsigned I = 0; I < getNumParams(); ++I) { |
| 1239 | std::string Name = "p" + utostr(I); |
| 1240 | std::string NewName = "rev" + utostr(I); |
| 1241 | |
| 1242 | Variable &V = Variables[Name]; |
| 1243 | Variable NewV(V.getType(), NewName + VariablePostfix); |
| 1244 | |
| 1245 | if (!NewV.getType().isVector() || NewV.getType().getNumElements() == 1) |
| 1246 | continue; |
| 1247 | |
| 1248 | OS << " " << NewV.getType().str() << " " << NewV.getName() << ";"; |
| 1249 | emitReverseVariable(NewV, V); |
| 1250 | V = NewV; |
| 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | void Intrinsic::emitReturnReversal() { |
| 1255 | if (BigEndianSafe) |
| 1256 | return; |
| 1257 | if (!getReturnType().isVector() || getReturnType().isVoid() || |
| 1258 | getReturnType().getNumElements() == 1) |
| 1259 | return; |
| 1260 | emitReverseVariable(RetVar, RetVar); |
| 1261 | } |
| 1262 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1263 | void Intrinsic::emitShadowedArgs() { |
| 1264 | // Macro arguments are not type-checked like inline function arguments, |
| 1265 | // so assign them to local temporaries to get the right type checking. |
| 1266 | if (!UseMacro) |
Michael Gottesman | 6cd3e56 | 2013-04-16 23:00:26 +0000 | [diff] [blame] | 1267 | return; |
| 1268 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1269 | for (unsigned I = 0; I < getNumParams(); ++I) { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1270 | // Do not create a temporary for an immediate argument. |
| 1271 | // That would defeat the whole point of using a macro! |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1272 | if (hasImmediate() && Proto[I+1] == 'i') |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1273 | continue; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1274 | // Do not create a temporary for pointer arguments. The input |
| 1275 | // pointer may have an alignment hint. |
| 1276 | if (getParamType(I).isPointer()) |
| 1277 | continue; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1278 | |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1279 | std::string Name = "p" + utostr(I); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1280 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1281 | assert(Variables.find(Name) != Variables.end()); |
| 1282 | Variable &V = Variables[Name]; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1283 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1284 | std::string NewName = "s" + utostr(I); |
| 1285 | Variable V2(V.getType(), NewName + VariablePostfix); |
Jiangning Liu | 1bda93a | 2013-09-09 02:21:08 +0000 | [diff] [blame] | 1286 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1287 | OS << " " << V2.getType().str() << " " << V2.getName() << " = " |
| 1288 | << V.getName() << ";"; |
| 1289 | emitNewLine(); |
Jiangning Liu | 1bda93a | 2013-09-09 02:21:08 +0000 | [diff] [blame] | 1290 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1291 | V = V2; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1292 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
Jiangning Liu | ee3e087 | 2013-11-27 14:02:55 +0000 | [diff] [blame] | 1295 | // We don't check 'a' in this function, because for builtin function the |
| 1296 | // argument matching to 'a' uses a vector type splatted from a scalar type. |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 1297 | bool Intrinsic::protoHasScalar() const { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1298 | return (Proto.find('s') != std::string::npos || |
| 1299 | Proto.find('z') != std::string::npos || |
| 1300 | Proto.find('r') != std::string::npos || |
| 1301 | Proto.find('b') != std::string::npos || |
| 1302 | Proto.find('$') != std::string::npos || |
| 1303 | Proto.find('y') != std::string::npos || |
| 1304 | Proto.find('o') != std::string::npos); |
Jiangning Liu | b96ebac | 2013-10-05 08:22:55 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1307 | void Intrinsic::emitBodyAsBuiltinCall() { |
| 1308 | std::string S; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1309 | |
| 1310 | // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit |
| 1311 | // sret-like argument. |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1312 | bool SRet = getReturnType().getNumVectors() >= 2; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1313 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1314 | StringRef N = Name; |
| 1315 | if (hasSplat()) { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1316 | // Call the non-splat builtin: chop off the "_n" suffix from the name. |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1317 | assert(N.endswith("_n")); |
| 1318 | N = N.drop_back(2); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1319 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1320 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1321 | ClassKind LocalCK = CK; |
| 1322 | if (!protoHasScalar()) |
| 1323 | LocalCK = ClassB; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1324 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1325 | if (!getReturnType().isVoid() && !SRet) |
| 1326 | S += "(" + RetVar.getType().str() + ") "; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1327 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1328 | S += "__builtin_neon_" + mangleName(N, LocalCK) + "("; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1329 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1330 | if (SRet) |
| 1331 | S += "&" + RetVar.getName() + ", "; |
| 1332 | |
| 1333 | for (unsigned I = 0; I < getNumParams(); ++I) { |
| 1334 | Variable &V = Variables["p" + utostr(I)]; |
| 1335 | Type T = V.getType(); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1336 | |
| 1337 | // Handle multiple-vector values specially, emitting each subvector as an |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1338 | // argument to the builtin. |
| 1339 | if (T.getNumVectors() > 1) { |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1340 | // Check if an explicit cast is needed. |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1341 | std::string Cast; |
| 1342 | if (T.isChar() || T.isPoly() || !T.isSigned()) { |
| 1343 | Type T2 = T; |
| 1344 | T2.makeOneVector(); |
| 1345 | T2.makeInteger(8, /*Signed=*/true); |
| 1346 | Cast = "(" + T2.str() + ")"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1347 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1348 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1349 | for (unsigned J = 0; J < T.getNumVectors(); ++J) |
| 1350 | S += Cast + V.getName() + ".val[" + utostr(J) + "], "; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1351 | continue; |
| 1352 | } |
| 1353 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1354 | std::string Arg; |
| 1355 | Type CastToType = T; |
| 1356 | if (hasSplat() && I == getSplatIdx()) { |
| 1357 | Arg = "(" + BaseType.str() + ") {"; |
| 1358 | for (unsigned J = 0; J < BaseType.getNumElements(); ++J) { |
| 1359 | if (J != 0) |
| 1360 | Arg += ", "; |
| 1361 | Arg += V.getName(); |
| 1362 | } |
| 1363 | Arg += "}"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1364 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1365 | CastToType = BaseType; |
| 1366 | } else { |
| 1367 | Arg = V.getName(); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1370 | // Check if an explicit cast is needed. |
| 1371 | if (CastToType.isVector()) { |
| 1372 | CastToType.makeInteger(8, true); |
| 1373 | Arg = "(" + CastToType.str() + ")" + Arg; |
| 1374 | } |
| 1375 | |
| 1376 | S += Arg + ", "; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | // Extra constant integer to hold type class enum for this function, e.g. s8 |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1380 | if (getClassKind(true) == ClassB) { |
| 1381 | Type ThisTy = getReturnType(); |
Ahmed Bougacha | 22a1696 | 2015-08-21 23:24:18 +0000 | [diff] [blame] | 1382 | if (Proto[0] == 'v' || isFloatingPointProtoModifier(Proto[0])) |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1383 | ThisTy = getParamType(0); |
| 1384 | if (ThisTy.isPointer()) |
| 1385 | ThisTy = getParamType(1); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1386 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1387 | S += utostr(ThisTy.getNeonEnum()); |
| 1388 | } else { |
| 1389 | // Remove extraneous ", ". |
| 1390 | S.pop_back(); |
| 1391 | S.pop_back(); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1392 | } |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1393 | S += ");"; |
| 1394 | |
| 1395 | std::string RetExpr; |
| 1396 | if (!SRet && !RetVar.getType().isVoid()) |
| 1397 | RetExpr = RetVar.getName() + " = "; |
| 1398 | |
| 1399 | OS << " " << RetExpr << S; |
| 1400 | emitNewLine(); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1403 | void Intrinsic::emitBody(StringRef CallPrefix) { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1404 | std::vector<std::string> Lines; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1405 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1406 | assert(RetVar.getType() == Types[0]); |
| 1407 | // Create a return variable, if we're not void. |
| 1408 | if (!RetVar.getType().isVoid()) { |
| 1409 | OS << " " << RetVar.getType().str() << " " << RetVar.getName() << ";"; |
| 1410 | emitNewLine(); |
| 1411 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1412 | |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 1413 | if (!Body || Body->getValues().empty()) { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1414 | // Nothing specific to output - must output a builtin. |
| 1415 | emitBodyAsBuiltinCall(); |
| 1416 | return; |
| 1417 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1418 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1419 | // We have a list of "things to output". The last should be returned. |
| 1420 | for (auto *I : Body->getValues()) { |
| 1421 | if (StringInit *SI = dyn_cast<StringInit>(I)) { |
| 1422 | Lines.push_back(replaceParamsIn(SI->getAsString())); |
| 1423 | } else if (DagInit *DI = dyn_cast<DagInit>(I)) { |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1424 | DagEmitter DE(*this, CallPrefix); |
| 1425 | Lines.push_back(DE.emitDag(DI).second + ";"); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1426 | } |
| 1427 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1428 | |
Alexander Kornienko | 6ee521c | 2015-01-23 15:36:10 +0000 | [diff] [blame] | 1429 | assert(!Lines.empty() && "Empty def?"); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1430 | if (!RetVar.getType().isVoid()) |
| 1431 | Lines.back().insert(0, RetVar.getName() + " = "); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1432 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1433 | for (auto &L : Lines) { |
| 1434 | OS << " " << L; |
| 1435 | emitNewLine(); |
| 1436 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1439 | void Intrinsic::emitReturn() { |
| 1440 | if (RetVar.getType().isVoid()) |
| 1441 | return; |
| 1442 | if (UseMacro) |
| 1443 | OS << " " << RetVar.getName() << ";"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1444 | else |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1445 | OS << " return " << RetVar.getName() << ";"; |
| 1446 | emitNewLine(); |
| 1447 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1448 | |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1449 | std::pair<Type, std::string> Intrinsic::DagEmitter::emitDag(DagInit *DI) { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1450 | // At this point we should only be seeing a def. |
| 1451 | DefInit *DefI = cast<DefInit>(DI->getOperator()); |
| 1452 | std::string Op = DefI->getAsString(); |
| 1453 | |
| 1454 | if (Op == "cast" || Op == "bitcast") |
| 1455 | return emitDagCast(DI, Op == "bitcast"); |
| 1456 | if (Op == "shuffle") |
| 1457 | return emitDagShuffle(DI); |
| 1458 | if (Op == "dup") |
| 1459 | return emitDagDup(DI); |
| 1460 | if (Op == "splat") |
| 1461 | return emitDagSplat(DI); |
| 1462 | if (Op == "save_temp") |
| 1463 | return emitDagSaveTemp(DI); |
| 1464 | if (Op == "op") |
| 1465 | return emitDagOp(DI); |
| 1466 | if (Op == "call") |
| 1467 | return emitDagCall(DI); |
| 1468 | if (Op == "name_replace") |
| 1469 | return emitDagNameReplace(DI); |
| 1470 | if (Op == "literal") |
| 1471 | return emitDagLiteral(DI); |
| 1472 | assert_with_loc(false, "Unknown operation!"); |
| 1473 | return std::make_pair(Type::getVoid(), ""); |
| 1474 | } |
| 1475 | |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1476 | std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagOp(DagInit *DI) { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1477 | std::string Op = cast<StringInit>(DI->getArg(0))->getAsUnquotedString(); |
| 1478 | if (DI->getNumArgs() == 2) { |
| 1479 | // Unary op. |
| 1480 | std::pair<Type, std::string> R = |
Matthias Braun | f1b0199 | 2016-12-05 06:00:51 +0000 | [diff] [blame] | 1481 | emitDagArg(DI->getArg(1), DI->getArgNameStr(1)); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1482 | return std::make_pair(R.first, Op + R.second); |
| 1483 | } else { |
| 1484 | assert(DI->getNumArgs() == 3 && "Can only handle unary and binary ops!"); |
| 1485 | std::pair<Type, std::string> R1 = |
Matthias Braun | f1b0199 | 2016-12-05 06:00:51 +0000 | [diff] [blame] | 1486 | emitDagArg(DI->getArg(1), DI->getArgNameStr(1)); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1487 | std::pair<Type, std::string> R2 = |
Matthias Braun | f1b0199 | 2016-12-05 06:00:51 +0000 | [diff] [blame] | 1488 | emitDagArg(DI->getArg(2), DI->getArgNameStr(2)); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1489 | assert_with_loc(R1.first == R2.first, "Argument type mismatch!"); |
| 1490 | return std::make_pair(R1.first, R1.second + " " + Op + " " + R2.second); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1491 | } |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1492 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1493 | |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1494 | std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagCall(DagInit *DI) { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1495 | std::vector<Type> Types; |
| 1496 | std::vector<std::string> Values; |
| 1497 | for (unsigned I = 0; I < DI->getNumArgs() - 1; ++I) { |
| 1498 | std::pair<Type, std::string> R = |
Matthias Braun | f1b0199 | 2016-12-05 06:00:51 +0000 | [diff] [blame] | 1499 | emitDagArg(DI->getArg(I + 1), DI->getArgNameStr(I + 1)); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1500 | Types.push_back(R.first); |
| 1501 | Values.push_back(R.second); |
| 1502 | } |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1503 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1504 | // Look up the called intrinsic. |
| 1505 | std::string N; |
| 1506 | if (StringInit *SI = dyn_cast<StringInit>(DI->getArg(0))) |
| 1507 | N = SI->getAsUnquotedString(); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 1508 | else |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1509 | N = emitDagArg(DI->getArg(0), "").second; |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 1510 | Intrinsic &Callee = Intr.Emitter.getIntrinsic(N, Types); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1511 | |
| 1512 | // Make sure the callee is known as an early def. |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 1513 | Callee.setNeededEarly(); |
| 1514 | Intr.Dependencies.insert(&Callee); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1515 | |
| 1516 | // Now create the call itself. |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 1517 | std::string S = CallPrefix.str() + Callee.getMangledName(true) + "("; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1518 | for (unsigned I = 0; I < DI->getNumArgs() - 1; ++I) { |
| 1519 | if (I != 0) |
| 1520 | S += ", "; |
| 1521 | S += Values[I]; |
| 1522 | } |
| 1523 | S += ")"; |
| 1524 | |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 1525 | return std::make_pair(Callee.getReturnType(), S); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1528 | std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagCast(DagInit *DI, |
| 1529 | bool IsBitCast){ |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1530 | // (cast MOD* VAL) -> cast VAL to type given by MOD. |
| 1531 | std::pair<Type, std::string> R = emitDagArg( |
Matthias Braun | f1b0199 | 2016-12-05 06:00:51 +0000 | [diff] [blame] | 1532 | DI->getArg(DI->getNumArgs() - 1), |
| 1533 | DI->getArgNameStr(DI->getNumArgs() - 1)); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1534 | Type castToType = R.first; |
| 1535 | for (unsigned ArgIdx = 0; ArgIdx < DI->getNumArgs() - 1; ++ArgIdx) { |
| 1536 | |
| 1537 | // MOD can take several forms: |
| 1538 | // 1. $X - take the type of parameter / variable X. |
| 1539 | // 2. The value "R" - take the type of the return type. |
| 1540 | // 3. a type string |
| 1541 | // 4. The value "U" or "S" to switch the signedness. |
| 1542 | // 5. The value "H" or "D" to half or double the bitwidth. |
| 1543 | // 6. The value "8" to convert to 8-bit (signed) integer lanes. |
Matthias Braun | f1b0199 | 2016-12-05 06:00:51 +0000 | [diff] [blame] | 1544 | if (!DI->getArgNameStr(ArgIdx).empty()) { |
| 1545 | assert_with_loc(Intr.Variables.find(DI->getArgNameStr(ArgIdx)) != |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1546 | Intr.Variables.end(), |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1547 | "Variable not found"); |
Matthias Braun | f1b0199 | 2016-12-05 06:00:51 +0000 | [diff] [blame] | 1548 | castToType = Intr.Variables[DI->getArgNameStr(ArgIdx)].getType(); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1549 | } else { |
| 1550 | StringInit *SI = dyn_cast<StringInit>(DI->getArg(ArgIdx)); |
| 1551 | assert_with_loc(SI, "Expected string type or $Name for cast type"); |
| 1552 | |
| 1553 | if (SI->getAsUnquotedString() == "R") { |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1554 | castToType = Intr.getReturnType(); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1555 | } else if (SI->getAsUnquotedString() == "U") { |
| 1556 | castToType.makeUnsigned(); |
| 1557 | } else if (SI->getAsUnquotedString() == "S") { |
| 1558 | castToType.makeSigned(); |
| 1559 | } else if (SI->getAsUnquotedString() == "H") { |
| 1560 | castToType.halveLanes(); |
| 1561 | } else if (SI->getAsUnquotedString() == "D") { |
| 1562 | castToType.doubleLanes(); |
| 1563 | } else if (SI->getAsUnquotedString() == "8") { |
| 1564 | castToType.makeInteger(8, true); |
| 1565 | } else { |
| 1566 | castToType = Type::fromTypedefName(SI->getAsUnquotedString()); |
| 1567 | assert_with_loc(!castToType.isVoid(), "Unknown typedef"); |
| 1568 | } |
| 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | std::string S; |
| 1573 | if (IsBitCast) { |
| 1574 | // Emit a reinterpret cast. The second operand must be an lvalue, so create |
| 1575 | // a temporary. |
| 1576 | std::string N = "reint"; |
| 1577 | unsigned I = 0; |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1578 | while (Intr.Variables.find(N) != Intr.Variables.end()) |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1579 | N = "reint" + utostr(++I); |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1580 | Intr.Variables[N] = Variable(R.first, N + Intr.VariablePostfix); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1581 | |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1582 | Intr.OS << R.first.str() << " " << Intr.Variables[N].getName() << " = " |
| 1583 | << R.second << ";"; |
| 1584 | Intr.emitNewLine(); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1585 | |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1586 | S = "*(" + castToType.str() + " *) &" + Intr.Variables[N].getName() + ""; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1587 | } else { |
| 1588 | // Emit a normal (static) cast. |
| 1589 | S = "(" + castToType.str() + ")(" + R.second + ")"; |
| 1590 | } |
| 1591 | |
| 1592 | return std::make_pair(castToType, S); |
| 1593 | } |
| 1594 | |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1595 | std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagShuffle(DagInit *DI){ |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1596 | // See the documentation in arm_neon.td for a description of these operators. |
| 1597 | class LowHalf : public SetTheory::Operator { |
| 1598 | public: |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1599 | void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts, |
| 1600 | ArrayRef<SMLoc> Loc) override { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1601 | SetTheory::RecSet Elts2; |
| 1602 | ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts2, Loc); |
| 1603 | Elts.insert(Elts2.begin(), Elts2.begin() + (Elts2.size() / 2)); |
| 1604 | } |
| 1605 | }; |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 1606 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1607 | class HighHalf : public SetTheory::Operator { |
| 1608 | public: |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1609 | void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts, |
| 1610 | ArrayRef<SMLoc> Loc) override { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1611 | SetTheory::RecSet Elts2; |
| 1612 | ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts2, Loc); |
| 1613 | Elts.insert(Elts2.begin() + (Elts2.size() / 2), Elts2.end()); |
| 1614 | } |
| 1615 | }; |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 1616 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1617 | class Rev : public SetTheory::Operator { |
| 1618 | unsigned ElementSize; |
| 1619 | |
| 1620 | public: |
| 1621 | Rev(unsigned ElementSize) : ElementSize(ElementSize) {} |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 1622 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1623 | void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts, |
| 1624 | ArrayRef<SMLoc> Loc) override { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1625 | SetTheory::RecSet Elts2; |
| 1626 | ST.evaluate(Expr->arg_begin() + 1, Expr->arg_end(), Elts2, Loc); |
| 1627 | |
| 1628 | int64_t VectorSize = cast<IntInit>(Expr->getArg(0))->getValue(); |
| 1629 | VectorSize /= ElementSize; |
| 1630 | |
| 1631 | std::vector<Record *> Revved; |
| 1632 | for (unsigned VI = 0; VI < Elts2.size(); VI += VectorSize) { |
| 1633 | for (int LI = VectorSize - 1; LI >= 0; --LI) { |
| 1634 | Revved.push_back(Elts2[VI + LI]); |
| 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | Elts.insert(Revved.begin(), Revved.end()); |
| 1639 | } |
| 1640 | }; |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 1641 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1642 | class MaskExpander : public SetTheory::Expander { |
| 1643 | unsigned N; |
| 1644 | |
| 1645 | public: |
| 1646 | MaskExpander(unsigned N) : N(N) {} |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 1647 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1648 | void expand(SetTheory &ST, Record *R, SetTheory::RecSet &Elts) override { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1649 | unsigned Addend = 0; |
| 1650 | if (R->getName() == "mask0") |
| 1651 | Addend = 0; |
| 1652 | else if (R->getName() == "mask1") |
| 1653 | Addend = N; |
| 1654 | else |
| 1655 | return; |
| 1656 | for (unsigned I = 0; I < N; ++I) |
| 1657 | Elts.insert(R->getRecords().getDef("sv" + utostr(I + Addend))); |
| 1658 | } |
| 1659 | }; |
| 1660 | |
| 1661 | // (shuffle arg1, arg2, sequence) |
| 1662 | std::pair<Type, std::string> Arg1 = |
Matthias Braun | f1b0199 | 2016-12-05 06:00:51 +0000 | [diff] [blame] | 1663 | emitDagArg(DI->getArg(0), DI->getArgNameStr(0)); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1664 | std::pair<Type, std::string> Arg2 = |
Matthias Braun | f1b0199 | 2016-12-05 06:00:51 +0000 | [diff] [blame] | 1665 | emitDagArg(DI->getArg(1), DI->getArgNameStr(1)); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1666 | assert_with_loc(Arg1.first == Arg2.first, |
| 1667 | "Different types in arguments to shuffle!"); |
| 1668 | |
| 1669 | SetTheory ST; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1670 | SetTheory::RecSet Elts; |
Craig Topper | bccb773 | 2015-04-24 06:53:50 +0000 | [diff] [blame] | 1671 | ST.addOperator("lowhalf", llvm::make_unique<LowHalf>()); |
| 1672 | ST.addOperator("highhalf", llvm::make_unique<HighHalf>()); |
| 1673 | ST.addOperator("rev", |
| 1674 | llvm::make_unique<Rev>(Arg1.first.getElementSizeInBits())); |
| 1675 | ST.addExpander("MaskExpand", |
| 1676 | llvm::make_unique<MaskExpander>(Arg1.first.getNumElements())); |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 1677 | ST.evaluate(DI->getArg(2), Elts, None); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1678 | |
| 1679 | std::string S = "__builtin_shufflevector(" + Arg1.second + ", " + Arg2.second; |
| 1680 | for (auto &E : Elts) { |
| 1681 | StringRef Name = E->getName(); |
| 1682 | assert_with_loc(Name.startswith("sv"), |
| 1683 | "Incorrect element kind in shuffle mask!"); |
| 1684 | S += ", " + Name.drop_front(2).str(); |
| 1685 | } |
| 1686 | S += ")"; |
| 1687 | |
| 1688 | // Recalculate the return type - the shuffle may have halved or doubled it. |
| 1689 | Type T(Arg1.first); |
| 1690 | if (Elts.size() > T.getNumElements()) { |
| 1691 | assert_with_loc( |
| 1692 | Elts.size() == T.getNumElements() * 2, |
| 1693 | "Can only double or half the number of elements in a shuffle!"); |
| 1694 | T.doubleLanes(); |
| 1695 | } else if (Elts.size() < T.getNumElements()) { |
| 1696 | assert_with_loc( |
| 1697 | Elts.size() == T.getNumElements() / 2, |
| 1698 | "Can only double or half the number of elements in a shuffle!"); |
| 1699 | T.halveLanes(); |
| 1700 | } |
| 1701 | |
| 1702 | return std::make_pair(T, S); |
| 1703 | } |
| 1704 | |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1705 | std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagDup(DagInit *DI) { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1706 | assert_with_loc(DI->getNumArgs() == 1, "dup() expects one argument"); |
Matthias Braun | f1b0199 | 2016-12-05 06:00:51 +0000 | [diff] [blame] | 1707 | std::pair<Type, std::string> A = emitDagArg(DI->getArg(0), |
| 1708 | DI->getArgNameStr(0)); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1709 | assert_with_loc(A.first.isScalar(), "dup() expects a scalar argument"); |
| 1710 | |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1711 | Type T = Intr.getBaseType(); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1712 | assert_with_loc(T.isVector(), "dup() used but default type is scalar!"); |
| 1713 | std::string S = "(" + T.str() + ") {"; |
| 1714 | for (unsigned I = 0; I < T.getNumElements(); ++I) { |
| 1715 | if (I != 0) |
| 1716 | S += ", "; |
| 1717 | S += A.second; |
| 1718 | } |
| 1719 | S += "}"; |
| 1720 | |
| 1721 | return std::make_pair(T, S); |
| 1722 | } |
| 1723 | |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1724 | std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagSplat(DagInit *DI) { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1725 | assert_with_loc(DI->getNumArgs() == 2, "splat() expects two arguments"); |
Matthias Braun | f1b0199 | 2016-12-05 06:00:51 +0000 | [diff] [blame] | 1726 | std::pair<Type, std::string> A = emitDagArg(DI->getArg(0), |
| 1727 | DI->getArgNameStr(0)); |
| 1728 | std::pair<Type, std::string> B = emitDagArg(DI->getArg(1), |
| 1729 | DI->getArgNameStr(1)); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1730 | |
| 1731 | assert_with_loc(B.first.isScalar(), |
| 1732 | "splat() requires a scalar int as the second argument"); |
| 1733 | |
| 1734 | std::string S = "__builtin_shufflevector(" + A.second + ", " + A.second; |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1735 | for (unsigned I = 0; I < Intr.getBaseType().getNumElements(); ++I) { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1736 | S += ", " + B.second; |
| 1737 | } |
| 1738 | S += ")"; |
| 1739 | |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1740 | return std::make_pair(Intr.getBaseType(), S); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1743 | std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagSaveTemp(DagInit *DI) { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1744 | assert_with_loc(DI->getNumArgs() == 2, "save_temp() expects two arguments"); |
Matthias Braun | f1b0199 | 2016-12-05 06:00:51 +0000 | [diff] [blame] | 1745 | std::pair<Type, std::string> A = emitDagArg(DI->getArg(1), |
| 1746 | DI->getArgNameStr(1)); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1747 | |
| 1748 | assert_with_loc(!A.first.isVoid(), |
| 1749 | "Argument to save_temp() must have non-void type!"); |
| 1750 | |
Matthias Braun | f1b0199 | 2016-12-05 06:00:51 +0000 | [diff] [blame] | 1751 | std::string N = DI->getArgNameStr(0); |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 1752 | assert_with_loc(!N.empty(), |
| 1753 | "save_temp() expects a name as the first argument"); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1754 | |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1755 | assert_with_loc(Intr.Variables.find(N) == Intr.Variables.end(), |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1756 | "Variable already defined!"); |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1757 | Intr.Variables[N] = Variable(A.first, N + Intr.VariablePostfix); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1758 | |
| 1759 | std::string S = |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1760 | A.first.str() + " " + Intr.Variables[N].getName() + " = " + A.second; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1761 | |
| 1762 | return std::make_pair(Type::getVoid(), S); |
| 1763 | } |
| 1764 | |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1765 | std::pair<Type, std::string> |
| 1766 | Intrinsic::DagEmitter::emitDagNameReplace(DagInit *DI) { |
| 1767 | std::string S = Intr.Name; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1768 | |
| 1769 | assert_with_loc(DI->getNumArgs() == 2, "name_replace requires 2 arguments!"); |
| 1770 | std::string ToReplace = cast<StringInit>(DI->getArg(0))->getAsUnquotedString(); |
| 1771 | std::string ReplaceWith = cast<StringInit>(DI->getArg(1))->getAsUnquotedString(); |
| 1772 | |
| 1773 | size_t Idx = S.find(ToReplace); |
| 1774 | |
| 1775 | assert_with_loc(Idx != std::string::npos, "name should contain '" + ToReplace + "'!"); |
| 1776 | S.replace(Idx, ToReplace.size(), ReplaceWith); |
| 1777 | |
| 1778 | return std::make_pair(Type::getVoid(), S); |
| 1779 | } |
| 1780 | |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1781 | std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagLiteral(DagInit *DI){ |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1782 | std::string Ty = cast<StringInit>(DI->getArg(0))->getAsUnquotedString(); |
| 1783 | std::string Value = cast<StringInit>(DI->getArg(1))->getAsUnquotedString(); |
| 1784 | return std::make_pair(Type::fromTypedefName(Ty), Value); |
| 1785 | } |
| 1786 | |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1787 | std::pair<Type, std::string> |
| 1788 | Intrinsic::DagEmitter::emitDagArg(Init *Arg, std::string ArgName) { |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 1789 | if (!ArgName.empty()) { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1790 | assert_with_loc(!Arg->isComplete(), |
| 1791 | "Arguments must either be DAGs or names, not both!"); |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1792 | assert_with_loc(Intr.Variables.find(ArgName) != Intr.Variables.end(), |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1793 | "Variable not defined!"); |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1794 | Variable &V = Intr.Variables[ArgName]; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1795 | return std::make_pair(V.getType(), V.getName()); |
| 1796 | } |
| 1797 | |
| 1798 | assert(Arg && "Neither ArgName nor Arg?!"); |
| 1799 | DagInit *DI = dyn_cast<DagInit>(Arg); |
| 1800 | assert_with_loc(DI, "Arguments must either be DAGs or names!"); |
| 1801 | |
| 1802 | return emitDag(DI); |
| 1803 | } |
| 1804 | |
| 1805 | std::string Intrinsic::generate() { |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1806 | // Little endian intrinsics are simple and don't require any argument |
| 1807 | // swapping. |
| 1808 | OS << "#ifdef __LITTLE_ENDIAN__\n"; |
| 1809 | |
| 1810 | generateImpl(false, "", ""); |
| 1811 | |
| 1812 | OS << "#else\n"; |
| 1813 | |
| 1814 | // Big endian intrinsics are more complex. The user intended these |
| 1815 | // intrinsics to operate on a vector "as-if" loaded by (V)LDR, |
| 1816 | // but we load as-if (V)LD1. So we should swap all arguments and |
| 1817 | // swap the return value too. |
| 1818 | // |
| 1819 | // If we call sub-intrinsics, we should call a version that does |
| 1820 | // not re-swap the arguments! |
| 1821 | generateImpl(true, "", "__noswap_"); |
| 1822 | |
| 1823 | // If we're needed early, create a non-swapping variant for |
| 1824 | // big-endian. |
| 1825 | if (NeededEarly) { |
| 1826 | generateImpl(false, "__noswap_", "__noswap_"); |
| 1827 | } |
| 1828 | OS << "#endif\n\n"; |
| 1829 | |
| 1830 | return OS.str(); |
| 1831 | } |
| 1832 | |
| 1833 | void Intrinsic::generateImpl(bool ReverseArguments, |
| 1834 | StringRef NamePrefix, StringRef CallPrefix) { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1835 | CurrentRecord = R; |
| 1836 | |
| 1837 | // If we call a macro, our local variables may be corrupted due to |
| 1838 | // lack of proper lexical scoping. So, add a globally unique postfix |
| 1839 | // to every variable. |
| 1840 | // |
| 1841 | // indexBody() should have set up the Dependencies set by now. |
| 1842 | for (auto *I : Dependencies) |
| 1843 | if (I->UseMacro) { |
| 1844 | VariablePostfix = "_" + utostr(Emitter.getUniqueNumber()); |
| 1845 | break; |
| 1846 | } |
| 1847 | |
| 1848 | initVariables(); |
| 1849 | |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1850 | emitPrototype(NamePrefix); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1851 | |
| 1852 | if (IsUnavailable) { |
| 1853 | OS << " __attribute__((unavailable));"; |
| 1854 | } else { |
| 1855 | emitOpeningBrace(); |
| 1856 | emitShadowedArgs(); |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1857 | if (ReverseArguments) |
| 1858 | emitArgumentReversal(); |
| 1859 | emitBody(CallPrefix); |
| 1860 | if (ReverseArguments) |
| 1861 | emitReturnReversal(); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1862 | emitReturn(); |
| 1863 | emitClosingBrace(); |
| 1864 | } |
| 1865 | OS << "\n"; |
| 1866 | |
| 1867 | CurrentRecord = nullptr; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | void Intrinsic::indexBody() { |
| 1871 | CurrentRecord = R; |
| 1872 | |
| 1873 | initVariables(); |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1874 | emitBody(""); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1875 | OS.str(""); |
| 1876 | |
| 1877 | CurrentRecord = nullptr; |
| 1878 | } |
| 1879 | |
| 1880 | //===----------------------------------------------------------------------===// |
| 1881 | // NeonEmitter implementation |
| 1882 | //===----------------------------------------------------------------------===// |
| 1883 | |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 1884 | Intrinsic &NeonEmitter::getIntrinsic(StringRef Name, ArrayRef<Type> Types) { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1885 | // First, look up the name in the intrinsic map. |
| 1886 | assert_with_loc(IntrinsicMap.find(Name.str()) != IntrinsicMap.end(), |
| 1887 | ("Intrinsic '" + Name + "' not found!").str()); |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 1888 | auto &V = IntrinsicMap.find(Name.str())->second; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1889 | std::vector<Intrinsic *> GoodVec; |
| 1890 | |
| 1891 | // Create a string to print if we end up failing. |
| 1892 | std::string ErrMsg = "looking up intrinsic '" + Name.str() + "("; |
| 1893 | for (unsigned I = 0; I < Types.size(); ++I) { |
| 1894 | if (I != 0) |
| 1895 | ErrMsg += ", "; |
| 1896 | ErrMsg += Types[I].str(); |
| 1897 | } |
| 1898 | ErrMsg += ")'\n"; |
| 1899 | ErrMsg += "Available overloads:\n"; |
| 1900 | |
| 1901 | // Now, look through each intrinsic implementation and see if the types are |
| 1902 | // compatible. |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 1903 | for (auto &I : V) { |
| 1904 | ErrMsg += " - " + I.getReturnType().str() + " " + I.getMangledName(); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1905 | ErrMsg += "("; |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 1906 | for (unsigned A = 0; A < I.getNumParams(); ++A) { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1907 | if (A != 0) |
| 1908 | ErrMsg += ", "; |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 1909 | ErrMsg += I.getParamType(A).str(); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1910 | } |
| 1911 | ErrMsg += ")\n"; |
| 1912 | |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 1913 | if (I.getNumParams() != Types.size()) |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1914 | continue; |
| 1915 | |
| 1916 | bool Good = true; |
| 1917 | for (unsigned Arg = 0; Arg < Types.size(); ++Arg) { |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 1918 | if (I.getParamType(Arg) != Types[Arg]) { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1919 | Good = false; |
| 1920 | break; |
| 1921 | } |
| 1922 | } |
| 1923 | if (Good) |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 1924 | GoodVec.push_back(&I); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 1927 | assert_with_loc(!GoodVec.empty(), |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1928 | "No compatible intrinsic found - " + ErrMsg); |
| 1929 | assert_with_loc(GoodVec.size() == 1, "Multiple overloads found - " + ErrMsg); |
| 1930 | |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 1931 | return *GoodVec.front(); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | void NeonEmitter::createIntrinsic(Record *R, |
| 1935 | SmallVectorImpl<Intrinsic *> &Out) { |
| 1936 | std::string Name = R->getValueAsString("Name"); |
| 1937 | std::string Proto = R->getValueAsString("Prototype"); |
| 1938 | std::string Types = R->getValueAsString("Types"); |
| 1939 | Record *OperationRec = R->getValueAsDef("Operation"); |
| 1940 | bool CartesianProductOfTypes = R->getValueAsBit("CartesianProductOfTypes"); |
James Molloy | b452f78 | 2014-06-27 11:53:35 +0000 | [diff] [blame] | 1941 | bool BigEndianSafe = R->getValueAsBit("BigEndianSafe"); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1942 | std::string Guard = R->getValueAsString("ArchGuard"); |
| 1943 | bool IsUnavailable = OperationRec->getValueAsBit("Unavailable"); |
| 1944 | |
| 1945 | // Set the global current record. This allows assert_with_loc to produce |
| 1946 | // decent location information even when highly nested. |
| 1947 | CurrentRecord = R; |
| 1948 | |
| 1949 | ListInit *Body = OperationRec->getValueAsListInit("Ops"); |
| 1950 | |
| 1951 | std::vector<TypeSpec> TypeSpecs = TypeSpec::fromTypeSpecs(Types); |
| 1952 | |
| 1953 | ClassKind CK = ClassNone; |
| 1954 | if (R->getSuperClasses().size() >= 2) |
Craig Topper | 2576124 | 2016-01-18 19:52:54 +0000 | [diff] [blame] | 1955 | CK = ClassMap[R->getSuperClasses()[1].first]; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1956 | |
| 1957 | std::vector<std::pair<TypeSpec, TypeSpec>> NewTypeSpecs; |
| 1958 | for (auto TS : TypeSpecs) { |
| 1959 | if (CartesianProductOfTypes) { |
| 1960 | Type DefaultT(TS, 'd'); |
| 1961 | for (auto SrcTS : TypeSpecs) { |
| 1962 | Type DefaultSrcT(SrcTS, 'd'); |
| 1963 | if (TS == SrcTS || |
| 1964 | DefaultSrcT.getSizeInBits() != DefaultT.getSizeInBits()) |
| 1965 | continue; |
| 1966 | NewTypeSpecs.push_back(std::make_pair(TS, SrcTS)); |
| 1967 | } |
| 1968 | } else { |
| 1969 | NewTypeSpecs.push_back(std::make_pair(TS, TS)); |
| 1970 | } |
| 1971 | } |
| 1972 | |
| 1973 | std::sort(NewTypeSpecs.begin(), NewTypeSpecs.end()); |
James Dennett | fa24549 | 2015-04-06 21:09:24 +0000 | [diff] [blame] | 1974 | NewTypeSpecs.erase(std::unique(NewTypeSpecs.begin(), NewTypeSpecs.end()), |
| 1975 | NewTypeSpecs.end()); |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 1976 | auto &Entry = IntrinsicMap[Name]; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1977 | |
| 1978 | for (auto &I : NewTypeSpecs) { |
David Blaikie | 4c96a5e | 2015-08-06 18:29:32 +0000 | [diff] [blame] | 1979 | Entry.emplace_back(R, Name, Proto, I.first, I.second, CK, Body, *this, |
| 1980 | Guard, IsUnavailable, BigEndianSafe); |
| 1981 | Out.push_back(&Entry.back()); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | CurrentRecord = nullptr; |
| 1985 | } |
| 1986 | |
| 1987 | /// genBuiltinsDef: Generate the BuiltinsARM.def and BuiltinsAArch64.def |
| 1988 | /// declaration of builtins, checking for unique builtin declarations. |
| 1989 | void NeonEmitter::genBuiltinsDef(raw_ostream &OS, |
| 1990 | SmallVectorImpl<Intrinsic *> &Defs) { |
| 1991 | OS << "#ifdef GET_NEON_BUILTINS\n"; |
| 1992 | |
| 1993 | // We only want to emit a builtin once, and we want to emit them in |
| 1994 | // alphabetical order, so use a std::set. |
| 1995 | std::set<std::string> Builtins; |
| 1996 | |
| 1997 | for (auto *Def : Defs) { |
| 1998 | if (Def->hasBody()) |
| 1999 | continue; |
| 2000 | // Functions with 'a' (the splat code) in the type prototype should not get |
| 2001 | // their own builtin as they use the non-splat variant. |
| 2002 | if (Def->hasSplat()) |
| 2003 | continue; |
| 2004 | |
| 2005 | std::string S = "BUILTIN(__builtin_neon_" + Def->getMangledName() + ", \""; |
| 2006 | |
| 2007 | S += Def->getBuiltinTypeStr(); |
| 2008 | S += "\", \"n\")"; |
| 2009 | |
| 2010 | Builtins.insert(S); |
| 2011 | } |
| 2012 | |
| 2013 | for (auto &S : Builtins) |
| 2014 | OS << S << "\n"; |
| 2015 | OS << "#endif\n\n"; |
| 2016 | } |
| 2017 | |
| 2018 | /// Generate the ARM and AArch64 overloaded type checking code for |
| 2019 | /// SemaChecking.cpp, checking for unique builtin declarations. |
| 2020 | void NeonEmitter::genOverloadTypeCheckCode(raw_ostream &OS, |
| 2021 | SmallVectorImpl<Intrinsic *> &Defs) { |
| 2022 | OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n"; |
| 2023 | |
| 2024 | // We record each overload check line before emitting because subsequent Inst |
| 2025 | // definitions may extend the number of permitted types (i.e. augment the |
| 2026 | // Mask). Use std::map to avoid sorting the table by hash number. |
| 2027 | struct OverloadInfo { |
| 2028 | uint64_t Mask; |
| 2029 | int PtrArgNum; |
| 2030 | bool HasConstPtr; |
| 2031 | OverloadInfo() : Mask(0ULL), PtrArgNum(0), HasConstPtr(false) {} |
| 2032 | }; |
| 2033 | std::map<std::string, OverloadInfo> OverloadMap; |
| 2034 | |
| 2035 | for (auto *Def : Defs) { |
| 2036 | // If the def has a body (that is, it has Operation DAGs), it won't call |
| 2037 | // __builtin_neon_* so we don't need to generate a definition for it. |
| 2038 | if (Def->hasBody()) |
| 2039 | continue; |
| 2040 | // Functions with 'a' (the splat code) in the type prototype should not get |
| 2041 | // their own builtin as they use the non-splat variant. |
| 2042 | if (Def->hasSplat()) |
| 2043 | continue; |
| 2044 | // Functions which have a scalar argument cannot be overloaded, no need to |
| 2045 | // check them if we are emitting the type checking code. |
| 2046 | if (Def->protoHasScalar()) |
| 2047 | continue; |
| 2048 | |
| 2049 | uint64_t Mask = 0ULL; |
| 2050 | Type Ty = Def->getReturnType(); |
Ahmed Bougacha | 22a1696 | 2015-08-21 23:24:18 +0000 | [diff] [blame] | 2051 | if (Def->getProto()[0] == 'v' || |
| 2052 | isFloatingPointProtoModifier(Def->getProto()[0])) |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2053 | Ty = Def->getParamType(0); |
| 2054 | if (Ty.isPointer()) |
| 2055 | Ty = Def->getParamType(1); |
| 2056 | |
| 2057 | Mask |= 1ULL << Ty.getNeonEnum(); |
| 2058 | |
| 2059 | // Check if the function has a pointer or const pointer argument. |
| 2060 | std::string Proto = Def->getProto(); |
| 2061 | int PtrArgNum = -1; |
| 2062 | bool HasConstPtr = false; |
| 2063 | for (unsigned I = 0; I < Def->getNumParams(); ++I) { |
| 2064 | char ArgType = Proto[I + 1]; |
| 2065 | if (ArgType == 'c') { |
| 2066 | HasConstPtr = true; |
| 2067 | PtrArgNum = I; |
| 2068 | break; |
| 2069 | } |
| 2070 | if (ArgType == 'p') { |
| 2071 | PtrArgNum = I; |
| 2072 | break; |
| 2073 | } |
| 2074 | } |
| 2075 | // For sret builtins, adjust the pointer argument index. |
| 2076 | if (PtrArgNum >= 0 && Def->getReturnType().getNumVectors() > 1) |
| 2077 | PtrArgNum += 1; |
| 2078 | |
| 2079 | std::string Name = Def->getName(); |
| 2080 | // Omit type checking for the pointer arguments of vld1_lane, vld1_dup, |
| 2081 | // and vst1_lane intrinsics. Using a pointer to the vector element |
| 2082 | // type with one of those operations causes codegen to select an aligned |
| 2083 | // load/store instruction. If you want an unaligned operation, |
| 2084 | // the pointer argument needs to have less alignment than element type, |
| 2085 | // so just accept any pointer type. |
| 2086 | if (Name == "vld1_lane" || Name == "vld1_dup" || Name == "vst1_lane") { |
| 2087 | PtrArgNum = -1; |
| 2088 | HasConstPtr = false; |
| 2089 | } |
| 2090 | |
| 2091 | if (Mask) { |
| 2092 | std::string Name = Def->getMangledName(); |
| 2093 | OverloadMap.insert(std::make_pair(Name, OverloadInfo())); |
| 2094 | OverloadInfo &OI = OverloadMap[Name]; |
| 2095 | OI.Mask |= Mask; |
| 2096 | OI.PtrArgNum |= PtrArgNum; |
| 2097 | OI.HasConstPtr = HasConstPtr; |
| 2098 | } |
| 2099 | } |
| 2100 | |
| 2101 | for (auto &I : OverloadMap) { |
| 2102 | OverloadInfo &OI = I.second; |
| 2103 | |
| 2104 | OS << "case NEON::BI__builtin_neon_" << I.first << ": "; |
| 2105 | OS << "mask = 0x" << utohexstr(OI.Mask) << "ULL"; |
| 2106 | if (OI.PtrArgNum >= 0) |
| 2107 | OS << "; PtrArgNum = " << OI.PtrArgNum; |
| 2108 | if (OI.HasConstPtr) |
| 2109 | OS << "; HasConstPtr = true"; |
| 2110 | OS << "; break;\n"; |
| 2111 | } |
| 2112 | OS << "#endif\n\n"; |
| 2113 | } |
| 2114 | |
| 2115 | void |
| 2116 | NeonEmitter::genIntrinsicRangeCheckCode(raw_ostream &OS, |
| 2117 | SmallVectorImpl<Intrinsic *> &Defs) { |
| 2118 | OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n"; |
| 2119 | |
| 2120 | std::set<std::string> Emitted; |
| 2121 | |
| 2122 | for (auto *Def : Defs) { |
| 2123 | if (Def->hasBody()) |
| 2124 | continue; |
| 2125 | // Functions with 'a' (the splat code) in the type prototype should not get |
| 2126 | // their own builtin as they use the non-splat variant. |
| 2127 | if (Def->hasSplat()) |
| 2128 | continue; |
Alp Toker | 958027b | 2014-07-14 19:42:55 +0000 | [diff] [blame] | 2129 | // Functions which do not have an immediate do not need to have range |
| 2130 | // checking code emitted. |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2131 | if (!Def->hasImmediate()) |
| 2132 | continue; |
| 2133 | if (Emitted.find(Def->getMangledName()) != Emitted.end()) |
| 2134 | continue; |
| 2135 | |
| 2136 | std::string LowerBound, UpperBound; |
| 2137 | |
| 2138 | Record *R = Def->getRecord(); |
| 2139 | if (R->getValueAsBit("isVCVT_N")) { |
| 2140 | // VCVT between floating- and fixed-point values takes an immediate |
| 2141 | // in the range [1, 32) for f32 or [1, 64) for f64. |
| 2142 | LowerBound = "1"; |
| 2143 | if (Def->getBaseType().getElementSizeInBits() == 32) |
| 2144 | UpperBound = "31"; |
| 2145 | else |
| 2146 | UpperBound = "63"; |
| 2147 | } else if (R->getValueAsBit("isScalarShift")) { |
| 2148 | // Right shifts have an 'r' in the name, left shifts do not. Convert |
| 2149 | // instructions have the same bounds and right shifts. |
| 2150 | if (Def->getName().find('r') != std::string::npos || |
| 2151 | Def->getName().find("cvt") != std::string::npos) |
| 2152 | LowerBound = "1"; |
| 2153 | |
| 2154 | UpperBound = utostr(Def->getReturnType().getElementSizeInBits() - 1); |
| 2155 | } else if (R->getValueAsBit("isShift")) { |
Alp Toker | 958027b | 2014-07-14 19:42:55 +0000 | [diff] [blame] | 2156 | // Builtins which are overloaded by type will need to have their upper |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2157 | // bound computed at Sema time based on the type constant. |
| 2158 | |
| 2159 | // Right shifts have an 'r' in the name, left shifts do not. |
| 2160 | if (Def->getName().find('r') != std::string::npos) |
| 2161 | LowerBound = "1"; |
| 2162 | UpperBound = "RFT(TV, true)"; |
| 2163 | } else if (Def->getClassKind(true) == ClassB) { |
| 2164 | // ClassB intrinsics have a type (and hence lane number) that is only |
| 2165 | // known at runtime. |
| 2166 | if (R->getValueAsBit("isLaneQ")) |
| 2167 | UpperBound = "RFT(TV, false, true)"; |
| 2168 | else |
| 2169 | UpperBound = "RFT(TV, false, false)"; |
| 2170 | } else { |
| 2171 | // The immediate generally refers to a lane in the preceding argument. |
| 2172 | assert(Def->getImmediateIdx() > 0); |
| 2173 | Type T = Def->getParamType(Def->getImmediateIdx() - 1); |
| 2174 | UpperBound = utostr(T.getNumElements() - 1); |
| 2175 | } |
| 2176 | |
| 2177 | // Calculate the index of the immediate that should be range checked. |
| 2178 | unsigned Idx = Def->getNumParams(); |
| 2179 | if (Def->hasImmediate()) |
| 2180 | Idx = Def->getGeneratedParamIdx(Def->getImmediateIdx()); |
| 2181 | |
| 2182 | OS << "case NEON::BI__builtin_neon_" << Def->getMangledName() << ": " |
| 2183 | << "i = " << Idx << ";"; |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 2184 | if (!LowerBound.empty()) |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2185 | OS << " l = " << LowerBound << ";"; |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 2186 | if (!UpperBound.empty()) |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2187 | OS << " u = " << UpperBound << ";"; |
| 2188 | OS << " break;\n"; |
| 2189 | |
| 2190 | Emitted.insert(Def->getMangledName()); |
| 2191 | } |
| 2192 | |
| 2193 | OS << "#endif\n\n"; |
| 2194 | } |
| 2195 | |
| 2196 | /// runHeader - Emit a file with sections defining: |
| 2197 | /// 1. the NEON section of BuiltinsARM.def and BuiltinsAArch64.def. |
| 2198 | /// 2. the SemaChecking code for the type overload checking. |
| 2199 | /// 3. the SemaChecking code for validation of intrinsic immediate arguments. |
| 2200 | void NeonEmitter::runHeader(raw_ostream &OS) { |
| 2201 | std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst"); |
| 2202 | |
| 2203 | SmallVector<Intrinsic *, 128> Defs; |
| 2204 | for (auto *R : RV) |
| 2205 | createIntrinsic(R, Defs); |
| 2206 | |
| 2207 | // Generate shared BuiltinsXXX.def |
| 2208 | genBuiltinsDef(OS, Defs); |
| 2209 | |
| 2210 | // Generate ARM overloaded type checking code for SemaChecking.cpp |
| 2211 | genOverloadTypeCheckCode(OS, Defs); |
| 2212 | |
| 2213 | // Generate ARM range checking code for shift/lane immediates. |
| 2214 | genIntrinsicRangeCheckCode(OS, Defs); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | /// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h |
| 2218 | /// is comprised of type definitions and function declarations. |
| 2219 | void NeonEmitter::run(raw_ostream &OS) { |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2220 | OS << "/*===---- arm_neon.h - ARM Neon intrinsics " |
| 2221 | "------------------------------" |
| 2222 | "---===\n" |
| 2223 | " *\n" |
| 2224 | " * Permission is hereby granted, free of charge, to any person " |
| 2225 | "obtaining " |
| 2226 | "a copy\n" |
| 2227 | " * of this software and associated documentation files (the " |
| 2228 | "\"Software\")," |
| 2229 | " to deal\n" |
| 2230 | " * in the Software without restriction, including without limitation " |
| 2231 | "the " |
| 2232 | "rights\n" |
| 2233 | " * to use, copy, modify, merge, publish, distribute, sublicense, " |
| 2234 | "and/or sell\n" |
| 2235 | " * copies of the Software, and to permit persons to whom the Software " |
| 2236 | "is\n" |
| 2237 | " * furnished to do so, subject to the following conditions:\n" |
| 2238 | " *\n" |
| 2239 | " * The above copyright notice and this permission notice shall be " |
| 2240 | "included in\n" |
| 2241 | " * all copies or substantial portions of the Software.\n" |
| 2242 | " *\n" |
| 2243 | " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, " |
| 2244 | "EXPRESS OR\n" |
| 2245 | " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF " |
| 2246 | "MERCHANTABILITY,\n" |
| 2247 | " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT " |
| 2248 | "SHALL THE\n" |
| 2249 | " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR " |
| 2250 | "OTHER\n" |
| 2251 | " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, " |
| 2252 | "ARISING FROM,\n" |
| 2253 | " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER " |
| 2254 | "DEALINGS IN\n" |
| 2255 | " * THE SOFTWARE.\n" |
| 2256 | " *\n" |
| 2257 | " *===-----------------------------------------------------------------" |
| 2258 | "---" |
| 2259 | "---===\n" |
| 2260 | " */\n\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2261 | |
| 2262 | OS << "#ifndef __ARM_NEON_H\n"; |
| 2263 | OS << "#define __ARM_NEON_H\n\n"; |
| 2264 | |
Tim Northover | 5bb34ca | 2013-11-21 12:36:34 +0000 | [diff] [blame] | 2265 | OS << "#if !defined(__ARM_NEON)\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2266 | OS << "#error \"NEON support not enabled\"\n"; |
| 2267 | OS << "#endif\n\n"; |
| 2268 | |
| 2269 | OS << "#include <stdint.h>\n\n"; |
| 2270 | |
| 2271 | // Emit NEON-specific scalar typedefs. |
| 2272 | OS << "typedef float float32_t;\n"; |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 2273 | OS << "typedef __fp16 float16_t;\n"; |
| 2274 | |
| 2275 | OS << "#ifdef __aarch64__\n"; |
| 2276 | OS << "typedef double float64_t;\n"; |
| 2277 | OS << "#endif\n\n"; |
| 2278 | |
| 2279 | // For now, signedness of polynomial types depends on target |
| 2280 | OS << "#ifdef __aarch64__\n"; |
| 2281 | OS << "typedef uint8_t poly8_t;\n"; |
| 2282 | OS << "typedef uint16_t poly16_t;\n"; |
Kevin Qin | caac85e | 2013-11-14 03:29:16 +0000 | [diff] [blame] | 2283 | OS << "typedef uint64_t poly64_t;\n"; |
Kevin Qin | fb79d7f | 2013-12-10 06:49:01 +0000 | [diff] [blame] | 2284 | OS << "typedef __uint128_t poly128_t;\n"; |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 2285 | OS << "#else\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2286 | OS << "typedef int8_t poly8_t;\n"; |
| 2287 | OS << "typedef int16_t poly16_t;\n"; |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 2288 | OS << "#endif\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2289 | |
| 2290 | // Emit Neon vector typedefs. |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 2291 | std::string TypedefTypes( |
Kevin Qin | caac85e | 2013-11-14 03:29:16 +0000 | [diff] [blame] | 2292 | "cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfdQdPcQPcPsQPsPlQPl"); |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2293 | std::vector<TypeSpec> TDTypeVec = TypeSpec::fromTypeSpecs(TypedefTypes); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2294 | |
| 2295 | // Emit vector typedefs. |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2296 | bool InIfdef = false; |
| 2297 | for (auto &TS : TDTypeVec) { |
| 2298 | bool IsA64 = false; |
| 2299 | Type T(TS, 'd'); |
| 2300 | if (T.isDouble() || (T.isPoly() && T.isLong())) |
| 2301 | IsA64 = true; |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 2302 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2303 | if (InIfdef && !IsA64) { |
Jiangning Liu | 4617e9d | 2013-10-04 09:21:17 +0000 | [diff] [blame] | 2304 | OS << "#endif\n"; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2305 | InIfdef = false; |
| 2306 | } |
| 2307 | if (!InIfdef && IsA64) { |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 2308 | OS << "#ifdef __aarch64__\n"; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2309 | InIfdef = true; |
| 2310 | } |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 2311 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2312 | if (T.isPoly()) |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2313 | OS << "typedef __attribute__((neon_polyvector_type("; |
| 2314 | else |
| 2315 | OS << "typedef __attribute__((neon_vector_type("; |
| 2316 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2317 | Type T2 = T; |
| 2318 | T2.makeScalar(); |
| 2319 | OS << utostr(T.getNumElements()) << "))) "; |
| 2320 | OS << T2.str(); |
| 2321 | OS << " " << T.str() << ";\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2322 | } |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2323 | if (InIfdef) |
Kevin Qin | caac85e | 2013-11-14 03:29:16 +0000 | [diff] [blame] | 2324 | OS << "#endif\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2325 | OS << "\n"; |
| 2326 | |
| 2327 | // Emit struct typedefs. |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2328 | InIfdef = false; |
| 2329 | for (unsigned NumMembers = 2; NumMembers <= 4; ++NumMembers) { |
| 2330 | for (auto &TS : TDTypeVec) { |
| 2331 | bool IsA64 = false; |
| 2332 | Type T(TS, 'd'); |
| 2333 | if (T.isDouble() || (T.isPoly() && T.isLong())) |
| 2334 | IsA64 = true; |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 2335 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2336 | if (InIfdef && !IsA64) { |
Jiangning Liu | 4617e9d | 2013-10-04 09:21:17 +0000 | [diff] [blame] | 2337 | OS << "#endif\n"; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2338 | InIfdef = false; |
| 2339 | } |
| 2340 | if (!InIfdef && IsA64) { |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 2341 | OS << "#ifdef __aarch64__\n"; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2342 | InIfdef = true; |
| 2343 | } |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 2344 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2345 | char M = '2' + (NumMembers - 2); |
| 2346 | Type VT(TS, M); |
| 2347 | OS << "typedef struct " << VT.str() << " {\n"; |
| 2348 | OS << " " << T.str() << " val"; |
| 2349 | OS << "[" << utostr(NumMembers) << "]"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2350 | OS << ";\n} "; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2351 | OS << VT.str() << ";\n"; |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 2352 | OS << "\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2353 | } |
| 2354 | } |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2355 | if (InIfdef) |
Kevin Qin | caac85e | 2013-11-14 03:29:16 +0000 | [diff] [blame] | 2356 | OS << "#endif\n"; |
| 2357 | OS << "\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2358 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2359 | OS << "#define __ai static inline __attribute__((__always_inline__, " |
| 2360 | "__nodebug__))\n\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2361 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2362 | SmallVector<Intrinsic *, 128> Defs; |
| 2363 | std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst"); |
| 2364 | for (auto *R : RV) |
| 2365 | createIntrinsic(R, Defs); |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2366 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2367 | for (auto *I : Defs) |
| 2368 | I->indexBody(); |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 2369 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2370 | std::stable_sort( |
| 2371 | Defs.begin(), Defs.end(), |
| 2372 | [](const Intrinsic *A, const Intrinsic *B) { return *A < *B; }); |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 2373 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2374 | // Only emit a def when its requirements have been met. |
| 2375 | // FIXME: This loop could be made faster, but it's fast enough for now. |
| 2376 | bool MadeProgress = true; |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 2377 | std::string InGuard; |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2378 | while (!Defs.empty() && MadeProgress) { |
| 2379 | MadeProgress = false; |
| 2380 | |
| 2381 | for (SmallVector<Intrinsic *, 128>::iterator I = Defs.begin(); |
| 2382 | I != Defs.end(); /*No step*/) { |
| 2383 | bool DependenciesSatisfied = true; |
| 2384 | for (auto *II : (*I)->getDependencies()) { |
| 2385 | if (std::find(Defs.begin(), Defs.end(), II) != Defs.end()) |
| 2386 | DependenciesSatisfied = false; |
| 2387 | } |
| 2388 | if (!DependenciesSatisfied) { |
| 2389 | // Try the next one. |
| 2390 | ++I; |
| 2391 | continue; |
| 2392 | } |
| 2393 | |
| 2394 | // Emit #endif/#if pair if needed. |
| 2395 | if ((*I)->getGuard() != InGuard) { |
| 2396 | if (!InGuard.empty()) |
| 2397 | OS << "#endif\n"; |
| 2398 | InGuard = (*I)->getGuard(); |
| 2399 | if (!InGuard.empty()) |
| 2400 | OS << "#if " << InGuard << "\n"; |
| 2401 | } |
| 2402 | |
| 2403 | // Actually generate the intrinsic code. |
| 2404 | OS << (*I)->generate(); |
| 2405 | |
| 2406 | MadeProgress = true; |
| 2407 | I = Defs.erase(I); |
| 2408 | } |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 2409 | } |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2410 | assert(Defs.empty() && "Some requirements were not satisfied!"); |
| 2411 | if (!InGuard.empty()) |
| 2412 | OS << "#endif\n"; |
Tim Northover | 2fe823a | 2013-08-01 09:23:19 +0000 | [diff] [blame] | 2413 | |
James Molloy | dee4ab0 | 2014-06-17 13:11:27 +0000 | [diff] [blame] | 2414 | OS << "\n"; |
Peter Collingbourne | bee583f | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 2415 | OS << "#undef __ai\n\n"; |
| 2416 | OS << "#endif /* __ARM_NEON_H */\n"; |
| 2417 | } |
| 2418 | |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 2419 | namespace clang { |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 2420 | |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 2421 | void EmitNeon(RecordKeeper &Records, raw_ostream &OS) { |
| 2422 | NeonEmitter(Records).run(OS); |
| 2423 | } |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 2424 | |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 2425 | void EmitNeonSema(RecordKeeper &Records, raw_ostream &OS) { |
| 2426 | NeonEmitter(Records).runHeader(OS); |
| 2427 | } |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 2428 | |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 2429 | void EmitNeonTest(RecordKeeper &Records, raw_ostream &OS) { |
Craig Topper | 0039f3f | 2014-06-18 03:57:25 +0000 | [diff] [blame] | 2430 | llvm_unreachable("Neon test generation no longer implemented!"); |
Jakob Stoklund Olesen | 995e0e1 | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 2431 | } |
Eugene Zelenko | 58ab22f | 2016-11-29 22:44:24 +0000 | [diff] [blame] | 2432 | |
| 2433 | } // end namespace clang |