Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_COMPILER_LLVM_INTRINSIC_HELPER_H_ |
| 18 | #define ART_COMPILER_LLVM_INTRINSIC_HELPER_H_ |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 19 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 20 | #include "base/logging.h" |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 21 | |
| 22 | #include <llvm/ADT/DenseMap.h> |
| 23 | |
| 24 | namespace llvm { |
| 25 | class Function; |
| 26 | class FunctionType; |
| 27 | class LLVMContext; |
| 28 | class Module; |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 29 | } // namespace llvm |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 30 | |
| 31 | namespace art { |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 32 | namespace llvm { |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 33 | |
| 34 | class IRBuilder; |
| 35 | |
| 36 | class IntrinsicHelper { |
| 37 | public: |
| 38 | enum IntrinsicId { |
| 39 | #define DEF_INTRINSICS_FUNC(ID, ...) ID, |
| 40 | #include "intrinsic_func_list.def" |
| 41 | MaxIntrinsicId, |
| 42 | |
| 43 | // Pseudo-intrinsics Id |
| 44 | UnknownId |
| 45 | }; |
| 46 | |
| 47 | enum IntrinsicAttribute { |
| 48 | kAttrNone = 0, |
| 49 | |
Shih-wei Liao | 21d28f5 | 2012-06-12 05:55:00 -0700 | [diff] [blame] | 50 | // Intrinsic that neither modified the memory state nor refer to the global |
| 51 | // state |
| 52 | kAttrReadNone = 1 << 0, |
| 53 | |
| 54 | // Intrinsic that doesn't modify the memory state. Note that one should set |
| 55 | // this flag carefully when the intrinsic may throw exception. Since the |
| 56 | // thread state is implicitly modified when an exception is thrown. |
| 57 | kAttrReadOnly = 1 << 1, |
| 58 | |
| 59 | // Note that intrinsic without kAttrNoThrow and kAttrDoThrow set means that |
| 60 | // intrinsic generates exception in some cases |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 61 | |
| 62 | // Intrinsic that never generates exception |
Shih-wei Liao | 21d28f5 | 2012-06-12 05:55:00 -0700 | [diff] [blame] | 63 | kAttrNoThrow = 1 << 2, |
| 64 | // Intrinsic that always generate exception |
| 65 | kAttrDoThrow = 1 << 3, |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | enum IntrinsicValType { |
| 69 | kNone, |
| 70 | |
| 71 | kVoidTy, |
| 72 | |
| 73 | kJavaObjectTy, |
| 74 | kJavaMethodTy, |
| 75 | kJavaThreadTy, |
| 76 | |
| 77 | kInt1Ty, |
| 78 | kInt8Ty, |
| 79 | kInt16Ty, |
| 80 | kInt32Ty, |
| 81 | kInt64Ty, |
Shih-wei Liao | 21d28f5 | 2012-06-12 05:55:00 -0700 | [diff] [blame] | 82 | kFloatTy, |
| 83 | kDoubleTy, |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 84 | |
| 85 | kInt1ConstantTy, |
| 86 | kInt8ConstantTy, |
| 87 | kInt16ConstantTy, |
| 88 | kInt32ConstantTy, |
| 89 | kInt64ConstantTy, |
Shih-wei Liao | 21d28f5 | 2012-06-12 05:55:00 -0700 | [diff] [blame] | 90 | kFloatConstantTy, |
| 91 | kDoubleConstantTy, |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 92 | |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 93 | kVarArgTy, |
| 94 | }; |
| 95 | |
| 96 | enum { |
| 97 | kIntrinsicMaxArgc = 5 |
| 98 | }; |
| 99 | |
| 100 | typedef struct IntrinsicInfo { |
| 101 | const char* name_; |
| 102 | unsigned attr_; |
| 103 | IntrinsicValType ret_val_type_; |
| 104 | IntrinsicValType arg_type_[kIntrinsicMaxArgc]; |
| 105 | } IntrinsicInfo; |
| 106 | |
| 107 | private: |
Shih-wei Liao | 21d28f5 | 2012-06-12 05:55:00 -0700 | [diff] [blame] | 108 | static const IntrinsicInfo Info[]; |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 109 | |
| 110 | public: |
| 111 | static const IntrinsicInfo& GetInfo(IntrinsicId id) { |
Ian Rogers | 76ae4fe | 2013-02-27 16:03:41 -0800 | [diff] [blame] | 112 | DCHECK(id >= 0 && id < MaxIntrinsicId) << "Unknown ART intrinsics ID: " |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 113 | << id; |
| 114 | return Info[id]; |
| 115 | } |
| 116 | |
| 117 | static const char* GetName(IntrinsicId id) { |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 118 | return (id <= MaxIntrinsicId) ? GetInfo(id).name_ : "InvalidIntrinsic"; |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static unsigned GetAttr(IntrinsicId id) { |
| 122 | return GetInfo(id).attr_; |
| 123 | } |
| 124 | |
| 125 | public: |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 126 | IntrinsicHelper(::llvm::LLVMContext& context, ::llvm::Module& module); |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 127 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 128 | ::llvm::Function* GetIntrinsicFunction(IntrinsicId id) { |
Ian Rogers | 76ae4fe | 2013-02-27 16:03:41 -0800 | [diff] [blame] | 129 | DCHECK(id >= 0 && id < MaxIntrinsicId) << "Unknown ART intrinsics ID: " |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 130 | << id; |
| 131 | return intrinsic_funcs_[id]; |
| 132 | } |
| 133 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 134 | IntrinsicId GetIntrinsicId(const ::llvm::Function* func) const { |
| 135 | ::llvm::DenseMap<const ::llvm::Function*, IntrinsicId>::const_iterator |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 136 | i = intrinsic_funcs_map_.find(func); |
| 137 | if (i == intrinsic_funcs_map_.end()) { |
| 138 | return UnknownId; |
| 139 | } else { |
| 140 | return i->second; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | private: |
Shih-wei Liao | 21d28f5 | 2012-06-12 05:55:00 -0700 | [diff] [blame] | 145 | // FIXME: "+1" is to workaround the GCC bugs: |
| 146 | // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43949 |
| 147 | // Remove this when uses newer GCC (> 4.4.3) |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 148 | ::llvm::Function* intrinsic_funcs_[MaxIntrinsicId + 1]; |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 149 | |
| 150 | // Map a llvm::Function to its intrinsic id |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 151 | ::llvm::DenseMap<const ::llvm::Function*, IntrinsicId> intrinsic_funcs_map_; |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 152 | }; |
| 153 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 154 | } // namespace llvm |
| 155 | } // namespace art |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 156 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 157 | #endif // ART_COMPILER_LLVM_INTRINSIC_HELPER_H_ |