Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceIntrinsics.h - List of Ice Intrinsics -----*- C++ -*-===// |
| 2 | // |
| 3 | // The Subzero Code Generator |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
Jim Stichnoth | 92a6e5b | 2015-12-02 16:52:44 -0800 | [diff] [blame] | 11 | /// \brief Declares the kinds of intrinsics supported by PNaCl. |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 12 | /// |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef SUBZERO_SRC_ICEINTRINSICS_H |
| 16 | #define SUBZERO_SRC_ICEINTRINSICS_H |
| 17 | |
| 18 | #include "IceDefs.h" |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 19 | #include "IceStringPool.h" |
Karl Schimpf | e1e013c | 2014-06-27 09:15:29 -0700 | [diff] [blame] | 20 | #include "IceTypes.h" |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 21 | |
| 22 | namespace Ice { |
| 23 | |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 24 | class InstCall; |
| 25 | |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 26 | static constexpr size_t kMaxIntrinsicParameters = 6; |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 27 | |
| 28 | class Intrinsics { |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 29 | Intrinsics(const Intrinsics &) = delete; |
| 30 | Intrinsics &operator=(const Intrinsics &) = delete; |
| 31 | |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 32 | public: |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 33 | explicit Intrinsics(GlobalContext *Ctx); |
| 34 | ~Intrinsics() = default; |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 35 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 36 | /// Some intrinsics allow overloading by type. This enum collapses all |
| 37 | /// overloads into a single ID, but the type can still be recovered by the |
| 38 | /// type of the intrinsic function call's return value and parameters. |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 39 | enum IntrinsicID { |
| 40 | UnknownIntrinsic = 0, |
| 41 | // Arbitrary (alphabetical) order. |
| 42 | AtomicCmpxchg, |
| 43 | AtomicFence, |
| 44 | AtomicFenceAll, |
| 45 | AtomicIsLockFree, |
| 46 | AtomicLoad, |
| 47 | AtomicRMW, |
| 48 | AtomicStore, |
| 49 | Bswap, |
| 50 | Ctlz, |
| 51 | Ctpop, |
| 52 | Cttz, |
Jim Stichnoth | 8c980d0 | 2015-03-19 13:01:50 -0700 | [diff] [blame] | 53 | Fabs, |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 54 | Longjmp, |
| 55 | Memcpy, |
| 56 | Memmove, |
| 57 | Memset, |
| 58 | NaClReadTP, |
| 59 | Setjmp, |
| 60 | Sqrt, |
| 61 | Stacksave, |
| 62 | Stackrestore, |
Nicolas Capens | acfb3df | 2016-10-03 10:46:30 -0400 | [diff] [blame] | 63 | Trap, |
| 64 | // The intrinsics below are not part of the PNaCl specification. |
Nicolas Capens | 67a49b5 | 2016-10-26 13:18:35 -0400 | [diff] [blame] | 65 | AddSaturateSigned, |
| 66 | AddSaturateUnsigned, |
Nicolas Capens | acfb3df | 2016-10-03 10:46:30 -0400 | [diff] [blame] | 67 | LoadSubVector, |
Nicolas Capens | 13cde0f | 2016-10-26 10:36:11 -0400 | [diff] [blame] | 68 | MultiplyAddPairs, |
| 69 | MultiplyHighSigned, |
| 70 | MultiplyHighUnsigned, |
Nicolas Capens | dbf81e0 | 2017-01-14 12:53:55 -0500 | [diff] [blame] | 71 | Nearbyint, |
Nicolas Capens | f0d12c3 | 2016-10-27 15:17:41 -0400 | [diff] [blame] | 72 | Round, |
Nicolas Capens | 13cde0f | 2016-10-26 10:36:11 -0400 | [diff] [blame] | 73 | SignMask, |
Nicolas Capens | ef8210d | 2016-10-17 17:42:29 -0400 | [diff] [blame] | 74 | StoreSubVector, |
Nicolas Capens | 67a49b5 | 2016-10-26 13:18:35 -0400 | [diff] [blame] | 75 | SubtractSaturateSigned, |
| 76 | SubtractSaturateUnsigned, |
Nicolas Capens | ef8210d | 2016-10-17 17:42:29 -0400 | [diff] [blame] | 77 | VectorPackSigned, |
Nicolas Capens | 13cde0f | 2016-10-26 10:36:11 -0400 | [diff] [blame] | 78 | VectorPackUnsigned |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 81 | /// Operations that can be represented by the AtomicRMW intrinsic. |
Jan Voung | 5cd240d | 2014-06-25 10:36:46 -0700 | [diff] [blame] | 82 | /// |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 83 | /// Do not reorder these values: their order offers forward compatibility of |
| 84 | /// bitcode targeted to PNaCl. |
Jan Voung | 5cd240d | 2014-06-25 10:36:46 -0700 | [diff] [blame] | 85 | enum AtomicRMWOperation { |
| 86 | AtomicInvalid = 0, // Invalid, keep first. |
| 87 | AtomicAdd, |
| 88 | AtomicSub, |
| 89 | AtomicOr, |
| 90 | AtomicAnd, |
| 91 | AtomicXor, |
| 92 | AtomicExchange, |
| 93 | AtomicNum // Invalid, keep last. |
| 94 | }; |
| 95 | |
| 96 | /// Memory orderings supported by PNaCl IR. |
| 97 | /// |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 98 | /// Do not reorder these values: their order offers forward compatibility of |
| 99 | /// bitcode targeted to PNaCl. |
Jan Voung | 5cd240d | 2014-06-25 10:36:46 -0700 | [diff] [blame] | 100 | enum MemoryOrder { |
| 101 | MemoryOrderInvalid = 0, // Invalid, keep first. |
| 102 | MemoryOrderRelaxed, |
| 103 | MemoryOrderConsume, |
| 104 | MemoryOrderAcquire, |
| 105 | MemoryOrderRelease, |
| 106 | MemoryOrderAcquireRelease, |
| 107 | MemoryOrderSequentiallyConsistent, |
| 108 | MemoryOrderNum // Invalid, keep last. |
| 109 | }; |
| 110 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 111 | /// Verify memory ordering rules for atomic intrinsics. For AtomicCmpxchg, |
| 112 | /// Order is the "success" ordering and OrderOther is the "failure" ordering. |
| 113 | /// Returns true if valid, false if invalid. |
| 114 | // TODO(stichnot,kschimpf): Perform memory order validation in the bitcode |
| 115 | // reader/parser, allowing LLVM and Subzero to share. See |
Jim Stichnoth | 1c335ef | 2015-03-18 09:01:52 -0700 | [diff] [blame] | 116 | // https://code.google.com/p/nativeclient/issues/detail?id=4126 . |
| 117 | static bool isMemoryOrderValid(IntrinsicID ID, uint64_t Order, |
| 118 | uint64_t OrderOther = MemoryOrderInvalid); |
Jan Voung | 5cd240d | 2014-06-25 10:36:46 -0700 | [diff] [blame] | 119 | |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 120 | enum SideEffects { SideEffects_F = 0, SideEffects_T = 1 }; |
Jan Voung | 44d53e1 | 2014-09-11 19:18:03 -0700 | [diff] [blame] | 121 | |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 122 | enum ReturnsTwice { ReturnsTwice_F = 0, ReturnsTwice_T = 1 }; |
Jan Voung | 44d53e1 | 2014-09-11 19:18:03 -0700 | [diff] [blame] | 123 | |
Jim Stichnoth | f1f773d | 2016-04-21 16:54:33 -0700 | [diff] [blame] | 124 | enum MemoryWrite { MemoryWrite_F = 0, MemoryWrite_T = 1 }; |
| 125 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 126 | /// Basic attributes related to each intrinsic, that are relevant to code |
| 127 | /// generation. Perhaps the attributes representation can be shared with |
| 128 | /// general function calls, but PNaCl currently strips all attributes from |
| 129 | /// functions. |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 130 | struct IntrinsicInfo { |
Jim Stichnoth | f1f773d | 2016-04-21 16:54:33 -0700 | [diff] [blame] | 131 | enum IntrinsicID ID : 29; |
Jan Voung | 44d53e1 | 2014-09-11 19:18:03 -0700 | [diff] [blame] | 132 | enum SideEffects HasSideEffects : 1; |
| 133 | enum ReturnsTwice ReturnsTwice : 1; |
Jim Stichnoth | f1f773d | 2016-04-21 16:54:33 -0700 | [diff] [blame] | 134 | enum MemoryWrite IsMemoryWrite : 1; |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 135 | }; |
Jim Stichnoth | f1f773d | 2016-04-21 16:54:33 -0700 | [diff] [blame] | 136 | static_assert(sizeof(IntrinsicInfo) == 4, "IntrinsicInfo should be 32 bits"); |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 137 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 138 | /// The types of validation values for FullIntrinsicInfo.validateCall. |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 139 | enum ValidateCallValue { |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 140 | IsValidCall, /// Valid use of instrinsic call. |
| 141 | BadReturnType, /// Return type invalid for intrinsic. |
| 142 | WrongNumOfArgs, /// Wrong number of arguments for intrinsic. |
| 143 | WrongCallArgType, /// Argument of wrong type. |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 146 | /// The complete set of information about an intrinsic. |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 147 | struct FullIntrinsicInfo { |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 148 | struct IntrinsicInfo Info; /// Information that CodeGen would care about. |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 149 | |
| 150 | // Sanity check during parsing. |
| 151 | Type Signature[kMaxIntrinsicParameters]; |
| 152 | uint8_t NumTypes; |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 153 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 154 | /// Validates that type signature of call matches intrinsic. If |
| 155 | /// WrongArgumentType is returned, ArgIndex is set to corresponding argument |
| 156 | /// index. |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 157 | ValidateCallValue validateCall(const Ice::InstCall *Call, |
| 158 | SizeT &ArgIndex) const; |
| 159 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 160 | /// Returns the return type of the intrinsic. |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 161 | Type getReturnType() const { |
Jim Stichnoth | 92b3144 | 2015-09-15 10:10:50 -0700 | [diff] [blame] | 162 | assert(NumTypes > 0); |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 163 | return Signature[0]; |
| 164 | } |
| 165 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 166 | /// Returns number of arguments expected. |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 167 | SizeT getNumArgs() const { |
Jim Stichnoth | 92b3144 | 2015-09-15 10:10:50 -0700 | [diff] [blame] | 168 | assert(NumTypes > 0); |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 169 | return NumTypes - 1; |
| 170 | } |
| 171 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 172 | /// Returns type of Index-th argument. |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 173 | Type getArgType(SizeT Index) const; |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 176 | /// Find the information about a given intrinsic, based on function name. If |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 177 | /// the function name does not have the common "llvm." prefix, nullptr is |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 178 | /// returned and Error is set to false. Otherwise, tries to find a reference |
| 179 | /// to a FullIntrinsicInfo entry (valid for the lifetime of the map). If |
| 180 | /// found, sets Error to false and returns the reference. If not found, sets |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 181 | /// Error to true and returns nullptr (indicating an unknown "llvm.foo" |
| 182 | /// intrinsic). |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 183 | const FullIntrinsicInfo *find(GlobalString Name, bool &Error) const; |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 184 | |
| 185 | private: |
| 186 | // TODO(jvoung): May want to switch to something like LLVM's StringMap. |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 187 | using IntrinsicMap = std::unordered_map<GlobalString, FullIntrinsicInfo>; |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 188 | IntrinsicMap Map; |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | } // end of namespace Ice |
| 192 | |
| 193 | #endif // SUBZERO_SRC_ICEINTRINSICS_H |