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 | |
| 17 | #include "intrinsic_helper.h" |
| 18 | |
| 19 | #include "ir_builder.h" |
| 20 | |
Brian Carlstrom | 37d4879 | 2013-03-22 14:14:45 -0700 | [diff] [blame] | 21 | #include <llvm/IR/Attributes.h> |
| 22 | #include <llvm/IR/DerivedTypes.h> |
| 23 | #include <llvm/IR/Function.h> |
| 24 | #include <llvm/IR/IRBuilder.h> |
| 25 | #include <llvm/IR/Intrinsics.h> |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 26 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 27 | namespace art { |
| 28 | namespace llvm { |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 29 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 30 | const IntrinsicHelper::IntrinsicInfo IntrinsicHelper::Info[] = { |
| 31 | #define DEF_INTRINSICS_FUNC(_, NAME, ATTR, RET_TYPE, ARG1_TYPE, ARG2_TYPE, \ |
| 32 | ARG3_TYPE, ARG4_TYPE, \ |
| 33 | ARG5_TYPE) \ |
| 34 | { #NAME, ATTR, RET_TYPE, { ARG1_TYPE, ARG2_TYPE, \ |
| 35 | ARG3_TYPE, ARG4_TYPE, \ |
| 36 | ARG5_TYPE} }, |
| 37 | #include "intrinsic_func_list.def" |
| 38 | }; |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 39 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 40 | static ::llvm::Type* GetLLVMTypeOfIntrinsicValType(IRBuilder& irb, |
| 41 | IntrinsicHelper::IntrinsicValType type) { |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 42 | switch (type) { |
| 43 | case IntrinsicHelper::kVoidTy: { |
| 44 | return irb.getVoidTy(); |
| 45 | } |
| 46 | case IntrinsicHelper::kJavaObjectTy: { |
Ian Rogers | 76ae4fe | 2013-02-27 16:03:41 -0800 | [diff] [blame] | 47 | return irb.getJObjectTy(); |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 48 | } |
| 49 | case IntrinsicHelper::kJavaMethodTy: { |
Ian Rogers | 76ae4fe | 2013-02-27 16:03:41 -0800 | [diff] [blame] | 50 | return irb.getJMethodTy(); |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 51 | } |
| 52 | case IntrinsicHelper::kJavaThreadTy: { |
Ian Rogers | 76ae4fe | 2013-02-27 16:03:41 -0800 | [diff] [blame] | 53 | return irb.getJThreadTy(); |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 54 | } |
| 55 | case IntrinsicHelper::kInt1Ty: |
| 56 | case IntrinsicHelper::kInt1ConstantTy: { |
| 57 | return irb.getInt1Ty(); |
| 58 | } |
| 59 | case IntrinsicHelper::kInt8Ty: |
| 60 | case IntrinsicHelper::kInt8ConstantTy: { |
| 61 | return irb.getInt8Ty(); |
| 62 | } |
| 63 | case IntrinsicHelper::kInt16Ty: |
| 64 | case IntrinsicHelper::kInt16ConstantTy: { |
| 65 | return irb.getInt16Ty(); |
| 66 | } |
| 67 | case IntrinsicHelper::kInt32Ty: |
| 68 | case IntrinsicHelper::kInt32ConstantTy: { |
| 69 | return irb.getInt32Ty(); |
| 70 | } |
| 71 | case IntrinsicHelper::kInt64Ty: |
| 72 | case IntrinsicHelper::kInt64ConstantTy: { |
| 73 | return irb.getInt64Ty(); |
| 74 | } |
Shih-wei Liao | 21d28f5 | 2012-06-12 05:55:00 -0700 | [diff] [blame] | 75 | case IntrinsicHelper::kFloatTy: |
| 76 | case IntrinsicHelper::kFloatConstantTy: { |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 77 | return irb.getFloatTy(); |
| 78 | } |
Shih-wei Liao | 21d28f5 | 2012-06-12 05:55:00 -0700 | [diff] [blame] | 79 | case IntrinsicHelper::kDoubleTy: |
| 80 | case IntrinsicHelper::kDoubleConstantTy: { |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 81 | return irb.getDoubleTy(); |
| 82 | } |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 83 | case IntrinsicHelper::kNone: |
| 84 | case IntrinsicHelper::kVarArgTy: |
| 85 | default: { |
| 86 | LOG(FATAL) << "Invalid intrinsic type " << type << "to get LLVM type!"; |
| 87 | return NULL; |
| 88 | } |
| 89 | } |
| 90 | // unreachable |
| 91 | } |
| 92 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 93 | IntrinsicHelper::IntrinsicHelper(::llvm::LLVMContext& context, |
| 94 | ::llvm::Module& module) { |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 95 | IRBuilder irb(context, module, *this); |
| 96 | |
| 97 | ::memset(intrinsic_funcs_, 0, sizeof(intrinsic_funcs_)); |
| 98 | |
| 99 | // This loop does the following things: |
| 100 | // 1. Introduce the intrinsic function into the module |
| 101 | // 2. Add "nocapture" and "noalias" attribute to the arguments in all |
| 102 | // intrinsics functions. |
| 103 | // 3. Initialize intrinsic_funcs_map_. |
| 104 | for (unsigned i = 0; i < MaxIntrinsicId; i++) { |
| 105 | IntrinsicId id = static_cast<IntrinsicId>(i); |
| 106 | const IntrinsicInfo& info = Info[i]; |
| 107 | |
| 108 | // Parse and construct the argument type from IntrinsicInfo |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 109 | ::llvm::Type* arg_type[kIntrinsicMaxArgc]; |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 110 | unsigned num_args = 0; |
| 111 | bool is_var_arg = false; |
| 112 | for (unsigned arg_iter = 0; arg_iter < kIntrinsicMaxArgc; arg_iter++) { |
| 113 | IntrinsicValType type = info.arg_type_[arg_iter]; |
| 114 | |
| 115 | if (type == kNone) { |
| 116 | break; |
| 117 | } else if (type == kVarArgTy) { |
| 118 | // Variable argument type must be the last argument |
| 119 | is_var_arg = true; |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | arg_type[num_args++] = GetLLVMTypeOfIntrinsicValType(irb, type); |
| 124 | } |
| 125 | |
| 126 | // Construct the function type |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 127 | ::llvm::Type* ret_type = |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 128 | GetLLVMTypeOfIntrinsicValType(irb, info.ret_val_type_); |
| 129 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 130 | ::llvm::FunctionType* type = |
| 131 | ::llvm::FunctionType::get(ret_type, |
| 132 | ::llvm::ArrayRef< ::llvm::Type*>(arg_type, num_args), |
| 133 | is_var_arg); |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 134 | |
| 135 | // Declare the function |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 136 | ::llvm::Function *fn = ::llvm::Function::Create(type, |
| 137 | ::llvm::Function::ExternalLinkage, |
| 138 | info.name_, &module); |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 139 | |
Brian Carlstrom | 37d4879 | 2013-03-22 14:14:45 -0700 | [diff] [blame] | 140 | if (info.attr_ & kAttrReadOnly) { |
| 141 | fn->setOnlyReadsMemory(); |
| 142 | } |
| 143 | if (info.attr_ & kAttrReadNone) { |
| 144 | fn->setDoesNotAccessMemory(); |
| 145 | } |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 146 | // None of the intrinsics throws exception |
Brian Carlstrom | 37d4879 | 2013-03-22 14:14:45 -0700 | [diff] [blame] | 147 | fn->setDoesNotThrow(); |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 148 | |
| 149 | intrinsic_funcs_[id] = fn; |
| 150 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 151 | DCHECK_NE(fn, static_cast< ::llvm::Function*>(NULL)) << "Intrinsic `" |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 152 | << GetName(id) << "' was not defined!"; |
| 153 | |
| 154 | // Add "noalias" and "nocapture" attribute to all arguments of pointer type |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 155 | for (::llvm::Function::arg_iterator arg_iter = fn->arg_begin(), |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 156 | arg_end = fn->arg_end(); arg_iter != arg_end; arg_iter++) { |
| 157 | if (arg_iter->getType()->isPointerTy()) { |
Brian Carlstrom | 37d4879 | 2013-03-22 14:14:45 -0700 | [diff] [blame] | 158 | std::vector< ::llvm::Attribute::AttrKind> attributes; |
| 159 | attributes.push_back(::llvm::Attribute::NoCapture); |
| 160 | attributes.push_back(::llvm::Attribute::NoAlias); |
| 161 | ::llvm::AttributeSet attribute_set = ::llvm::AttributeSet::get(fn->getContext(), |
| 162 | arg_iter->getArgNo(), |
| 163 | attributes); |
| 164 | arg_iter->addAttr(attribute_set); |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | // Insert the newly created intrinsic to intrinsic_funcs_map_ |
| 169 | if (!intrinsic_funcs_map_.insert(std::make_pair(fn, id)).second) { |
| 170 | LOG(FATAL) << "Duplicate entry in intrinsic functions map?"; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return; |
| 175 | } |
| 176 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 177 | } // namespace llvm |
| 178 | } // namespace art |