blob: 429142b4c4d1f9f7554bf55b3de4acc2da2f41a2 [file] [log] [blame]
Jan Voung3bd9f1a2014-06-18 10:50:57 -07001//===- 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//===----------------------------------------------------------------------===//
9//
10// This file declares the kinds of intrinsics supported by PNaCl.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef SUBZERO_SRC_ICEINTRINSICS_H
15#define SUBZERO_SRC_ICEINTRINSICS_H
16
17#include "IceDefs.h"
Karl Schimpfe1e013c2014-06-27 09:15:29 -070018#include "IceTypes.h"
Jan Voung3bd9f1a2014-06-18 10:50:57 -070019
20namespace Ice {
21
Karl Schimpf8df26f32014-09-19 09:33:26 -070022class InstCall;
23
Jan Voung3bd9f1a2014-06-18 10:50:57 -070024static const size_t kMaxIntrinsicParameters = 6;
25
26class Intrinsics {
27public:
28 Intrinsics();
29 ~Intrinsics();
30
31 // Some intrinsics allow overloading by type. This enum collapses all
32 // overloads into a single ID, but the type can still be recovered by the
33 // type of the intrinsic function call's return value and parameters.
34 enum IntrinsicID {
35 UnknownIntrinsic = 0,
36 // Arbitrary (alphabetical) order.
37 AtomicCmpxchg,
38 AtomicFence,
39 AtomicFenceAll,
40 AtomicIsLockFree,
41 AtomicLoad,
42 AtomicRMW,
43 AtomicStore,
44 Bswap,
45 Ctlz,
46 Ctpop,
47 Cttz,
48 Longjmp,
49 Memcpy,
50 Memmove,
51 Memset,
52 NaClReadTP,
53 Setjmp,
54 Sqrt,
55 Stacksave,
56 Stackrestore,
57 Trap
58 };
59
Jan Voung5cd240d2014-06-25 10:36:46 -070060 /// Operations that can be represented by the AtomicRMW
61 /// intrinsic.
62 ///
63 /// Do not reorder these values: their order offers forward
64 /// compatibility of bitcode targeted to PNaCl.
65 enum AtomicRMWOperation {
66 AtomicInvalid = 0, // Invalid, keep first.
67 AtomicAdd,
68 AtomicSub,
69 AtomicOr,
70 AtomicAnd,
71 AtomicXor,
72 AtomicExchange,
73 AtomicNum // Invalid, keep last.
74 };
75
76 /// Memory orderings supported by PNaCl IR.
77 ///
78 /// Do not reorder these values: their order offers forward
79 /// compatibility of bitcode targeted to PNaCl.
80 enum MemoryOrder {
81 MemoryOrderInvalid = 0, // Invalid, keep first.
82 MemoryOrderRelaxed,
83 MemoryOrderConsume,
84 MemoryOrderAcquire,
85 MemoryOrderRelease,
86 MemoryOrderAcquireRelease,
87 MemoryOrderSequentiallyConsistent,
88 MemoryOrderNum // Invalid, keep last.
89 };
90
91 static bool VerifyMemoryOrder(uint64_t Order);
92
Jan Voung44d53e12014-09-11 19:18:03 -070093 enum SideEffects {
94 SideEffects_F=0,
95 SideEffects_T=1
96 };
97
98 enum ReturnsTwice {
99 ReturnsTwice_F=0,
100 ReturnsTwice_T=1
101 };
102
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700103 // Basic attributes related to each intrinsic, that are relevant to
Jan Voung44d53e12014-09-11 19:18:03 -0700104 // code generation. Perhaps the attributes representation can be shared
105 // with general function calls, but PNaCl currently strips all
106 // attributes from functions.
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700107 struct IntrinsicInfo {
Jan Voung44d53e12014-09-11 19:18:03 -0700108 enum IntrinsicID ID : 30;
109 enum SideEffects HasSideEffects : 1;
110 enum ReturnsTwice ReturnsTwice : 1;
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700111 };
112
Karl Schimpf8df26f32014-09-19 09:33:26 -0700113 // The types of validation values for FullIntrinsicInfo.validateCall.
114 enum ValidateCallValue {
115 IsValidCall, // Valid use of instrinsic call.
116 BadReturnType, // Return type invalid for intrinsic.
117 WrongNumOfArgs, // Wrong number of arguments for intrinsic.
118 WrongCallArgType, // Argument of wrong type.
119 };
120
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700121 // The complete set of information about an intrinsic.
122 struct FullIntrinsicInfo {
123 struct IntrinsicInfo Info; // Information that CodeGen would care about.
124
125 // Sanity check during parsing.
126 Type Signature[kMaxIntrinsicParameters];
127 uint8_t NumTypes;
Karl Schimpf8df26f32014-09-19 09:33:26 -0700128
129 // Validates that type signature of call matches intrinsic.
130 // If WrongArgumentType is returned, ArgIndex is set to corresponding
131 // argument index.
132 ValidateCallValue validateCall(const Ice::InstCall *Call,
133 SizeT &ArgIndex) const;
134
135 // Returns the return type of the intrinsic.
136 Type getReturnType() const {
137 assert(NumTypes > 1);
138 return Signature[0];
139 }
140
141 // Returns number of arguments expected.
142 SizeT getNumArgs() const {
143 assert(NumTypes > 1);
144 return NumTypes - 1;
145 }
146
147 // Returns type of Index-th argument.
148 Type getArgType(SizeT Index) const;
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700149 };
150
151 // Find the information about a given intrinsic, based on function name.
152 // The function name is expected to have the common "llvm." prefix
153 // stripped. If found, returns a reference to a FullIntrinsicInfo entry
154 // (valid for the lifetime of the map). Otherwise returns null.
155 const FullIntrinsicInfo *find(const IceString &Name) const;
156
157private:
158 // TODO(jvoung): May want to switch to something like LLVM's StringMap.
159 typedef std::map<IceString, FullIntrinsicInfo> IntrinsicMap;
Jim Stichnothf44f3712014-10-01 14:05:51 -0700160 IntrinsicMap Map;
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700161
Jim Stichnoth0795ba02014-10-01 14:23:01 -0700162 Intrinsics(const Intrinsics &) = delete;
163 Intrinsics &operator=(const Intrinsics &) = delete;
Jan Voung3bd9f1a2014-06-18 10:50:57 -0700164};
165
166} // end of namespace Ice
167
168#endif // SUBZERO_SRC_ICEINTRINSICS_H