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, |
| 63 | Trap |
| 64 | }; |
| 65 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 66 | /// Operations that can be represented by the AtomicRMW intrinsic. |
Jan Voung | 5cd240d | 2014-06-25 10:36:46 -0700 | [diff] [blame] | 67 | /// |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 68 | /// Do not reorder these values: their order offers forward compatibility of |
| 69 | /// bitcode targeted to PNaCl. |
Jan Voung | 5cd240d | 2014-06-25 10:36:46 -0700 | [diff] [blame] | 70 | enum AtomicRMWOperation { |
| 71 | AtomicInvalid = 0, // Invalid, keep first. |
| 72 | AtomicAdd, |
| 73 | AtomicSub, |
| 74 | AtomicOr, |
| 75 | AtomicAnd, |
| 76 | AtomicXor, |
| 77 | AtomicExchange, |
| 78 | AtomicNum // Invalid, keep last. |
| 79 | }; |
| 80 | |
| 81 | /// Memory orderings supported by PNaCl IR. |
| 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 MemoryOrder { |
| 86 | MemoryOrderInvalid = 0, // Invalid, keep first. |
| 87 | MemoryOrderRelaxed, |
| 88 | MemoryOrderConsume, |
| 89 | MemoryOrderAcquire, |
| 90 | MemoryOrderRelease, |
| 91 | MemoryOrderAcquireRelease, |
| 92 | MemoryOrderSequentiallyConsistent, |
| 93 | MemoryOrderNum // Invalid, keep last. |
| 94 | }; |
| 95 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 96 | /// Verify memory ordering rules for atomic intrinsics. For AtomicCmpxchg, |
| 97 | /// Order is the "success" ordering and OrderOther is the "failure" ordering. |
| 98 | /// Returns true if valid, false if invalid. |
| 99 | // TODO(stichnot,kschimpf): Perform memory order validation in the bitcode |
| 100 | // reader/parser, allowing LLVM and Subzero to share. See |
Jim Stichnoth | 1c335ef | 2015-03-18 09:01:52 -0700 | [diff] [blame] | 101 | // https://code.google.com/p/nativeclient/issues/detail?id=4126 . |
| 102 | static bool isMemoryOrderValid(IntrinsicID ID, uint64_t Order, |
| 103 | uint64_t OrderOther = MemoryOrderInvalid); |
Jan Voung | 5cd240d | 2014-06-25 10:36:46 -0700 | [diff] [blame] | 104 | |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 105 | enum SideEffects { SideEffects_F = 0, SideEffects_T = 1 }; |
Jan Voung | 44d53e1 | 2014-09-11 19:18:03 -0700 | [diff] [blame] | 106 | |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 107 | enum ReturnsTwice { ReturnsTwice_F = 0, ReturnsTwice_T = 1 }; |
Jan Voung | 44d53e1 | 2014-09-11 19:18:03 -0700 | [diff] [blame] | 108 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 109 | /// Basic attributes related to each intrinsic, that are relevant to code |
| 110 | /// generation. Perhaps the attributes representation can be shared with |
| 111 | /// general function calls, but PNaCl currently strips all attributes from |
| 112 | /// functions. |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 113 | struct IntrinsicInfo { |
Jan Voung | 44d53e1 | 2014-09-11 19:18:03 -0700 | [diff] [blame] | 114 | enum IntrinsicID ID : 30; |
| 115 | enum SideEffects HasSideEffects : 1; |
| 116 | enum ReturnsTwice ReturnsTwice : 1; |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 119 | /// The types of validation values for FullIntrinsicInfo.validateCall. |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 120 | enum ValidateCallValue { |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 121 | IsValidCall, /// Valid use of instrinsic call. |
| 122 | BadReturnType, /// Return type invalid for intrinsic. |
| 123 | WrongNumOfArgs, /// Wrong number of arguments for intrinsic. |
| 124 | WrongCallArgType, /// Argument of wrong type. |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 127 | /// The complete set of information about an intrinsic. |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 128 | struct FullIntrinsicInfo { |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 129 | struct IntrinsicInfo Info; /// Information that CodeGen would care about. |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 130 | |
| 131 | // Sanity check during parsing. |
| 132 | Type Signature[kMaxIntrinsicParameters]; |
| 133 | uint8_t NumTypes; |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 134 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 135 | /// Validates that type signature of call matches intrinsic. If |
| 136 | /// WrongArgumentType is returned, ArgIndex is set to corresponding argument |
| 137 | /// index. |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 138 | ValidateCallValue validateCall(const Ice::InstCall *Call, |
| 139 | SizeT &ArgIndex) const; |
| 140 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 141 | /// Returns the return type of the intrinsic. |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 142 | Type getReturnType() const { |
Jim Stichnoth | 92b3144 | 2015-09-15 10:10:50 -0700 | [diff] [blame] | 143 | assert(NumTypes > 0); |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 144 | return Signature[0]; |
| 145 | } |
| 146 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 147 | /// Returns number of arguments expected. |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 148 | SizeT getNumArgs() const { |
Jim Stichnoth | 92b3144 | 2015-09-15 10:10:50 -0700 | [diff] [blame] | 149 | assert(NumTypes > 0); |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 150 | return NumTypes - 1; |
| 151 | } |
| 152 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 153 | /// Returns type of Index-th argument. |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 154 | Type getArgType(SizeT Index) const; |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 157 | /// Find the information about a given intrinsic, based on function name. If |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 158 | /// the function name does not have the common "llvm." prefix, nullptr is |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 159 | /// returned and Error is set to false. Otherwise, tries to find a reference |
| 160 | /// to a FullIntrinsicInfo entry (valid for the lifetime of the map). If |
| 161 | /// 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] | 162 | /// Error to true and returns nullptr (indicating an unknown "llvm.foo" |
| 163 | /// intrinsic). |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame^] | 164 | const FullIntrinsicInfo *find(GlobalString Name, bool &Error) const; |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 165 | |
| 166 | private: |
| 167 | // TODO(jvoung): May want to switch to something like LLVM's StringMap. |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame^] | 168 | using IntrinsicMap = std::unordered_map<GlobalString, FullIntrinsicInfo>; |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 169 | IntrinsicMap Map; |
Jan Voung | 3bd9f1a | 2014-06-18 10:50:57 -0700 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | } // end of namespace Ice |
| 173 | |
| 174 | #endif // SUBZERO_SRC_ICEINTRINSICS_H |