blob: 07836bb27a182350e8cca870a2d2cb1a77a84d83 [file] [log] [blame]
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -07001/*
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
21#include <llvm/DerivedTypes.h>
22#include <llvm/Function.h>
23#include <llvm/Intrinsics.h>
24#include <llvm/Module.h>
25#include <llvm/Support/IRBuilder.h>
26
27using namespace art;
28using namespace greenland;
29
30namespace {
31
32inline llvm::Type*
33GetLLVMTypeOfIntrinsicValType(IRBuilder& irb,
34 IntrinsicHelper::IntrinsicValType type) {
35 switch (type) {
36 case IntrinsicHelper::kVoidTy: {
37 return irb.getVoidTy();
38 }
39 case IntrinsicHelper::kJavaObjectTy: {
40 return irb.GetJObjectTy();
41 }
42 case IntrinsicHelper::kJavaMethodTy: {
43 return irb.GetJMethodTy();
44 }
45 case IntrinsicHelper::kJavaThreadTy: {
46 return irb.GetJThreadTy();
47 }
48 case IntrinsicHelper::kInt1Ty:
49 case IntrinsicHelper::kInt1ConstantTy: {
50 return irb.getInt1Ty();
51 }
52 case IntrinsicHelper::kInt8Ty:
53 case IntrinsicHelper::kInt8ConstantTy: {
54 return irb.getInt8Ty();
55 }
56 case IntrinsicHelper::kInt16Ty:
57 case IntrinsicHelper::kInt16ConstantTy: {
58 return irb.getInt16Ty();
59 }
60 case IntrinsicHelper::kInt32Ty:
61 case IntrinsicHelper::kInt32ConstantTy: {
62 return irb.getInt32Ty();
63 }
64 case IntrinsicHelper::kInt64Ty:
65 case IntrinsicHelper::kInt64ConstantTy: {
66 return irb.getInt64Ty();
67 }
68 case IntrinsicHelper::kNone:
69 case IntrinsicHelper::kVarArgTy:
70 default: {
71 LOG(FATAL) << "Invalid intrinsic type " << type << "to get LLVM type!";
72 return NULL;
73 }
74 }
75 // unreachable
76}
77
78} // anonymous namespace
79
80namespace art {
81namespace greenland {
82
83const IntrinsicHelper::IntrinsicInfo IntrinsicHelper::Info[MaxIntrinsicId] = {
84#define DEF_INTRINSICS_FUNC(_, NAME, ATTR, RET_TYPE, ARG1_TYPE, ARG2_TYPE, \
85 ARG3_TYPE, ARG4_TYPE, \
86 ARG5_TYPE) \
87 { #NAME, ATTR, RET_TYPE, { ARG1_TYPE, ARG2_TYPE, \
88 ARG3_TYPE, ARG4_TYPE, \
89 ARG5_TYPE} },
90#include "intrinsic_func_list.def"
91};
92
93IntrinsicHelper::IntrinsicHelper(llvm::LLVMContext& context,
94 llvm::Module& module) {
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
109 llvm::Type* arg_type[kIntrinsicMaxArgc];
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
127 llvm::Type* ret_type =
128 GetLLVMTypeOfIntrinsicValType(irb, info.ret_val_type_);
129
130 llvm::FunctionType* type =
131 llvm::FunctionType::get(ret_type,
132 llvm::ArrayRef<llvm::Type*>(arg_type, num_args),
133 is_var_arg);
134
135 // Declare the function
136 llvm::Function *fn = llvm::Function::Create(type,
137 llvm::Function::ExternalLinkage,
138 info.name_, &module);
139
140 fn->setOnlyReadsMemory(info.attr_ & kAttrReadOnly);
141 // None of the intrinsics throws exception
142 fn->setDoesNotThrow(true);
143
144 intrinsic_funcs_[id] = fn;
145
146 DCHECK_NE(fn, static_cast<llvm::Function*>(NULL)) << "Intrinsic `"
147 << GetName(id) << "' was not defined!";
148
149 // Add "noalias" and "nocapture" attribute to all arguments of pointer type
150 for (llvm::Function::arg_iterator arg_iter = fn->arg_begin(),
151 arg_end = fn->arg_end(); arg_iter != arg_end; arg_iter++) {
152 if (arg_iter->getType()->isPointerTy()) {
153 arg_iter->addAttr(llvm::Attribute::NoCapture);
154 arg_iter->addAttr(llvm::Attribute::NoAlias);
155 }
156 }
157
158 // Insert the newly created intrinsic to intrinsic_funcs_map_
159 if (!intrinsic_funcs_map_.insert(std::make_pair(fn, id)).second) {
160 LOG(FATAL) << "Duplicate entry in intrinsic functions map?";
161 }
162 }
163
164 return;
165}
166
167} // namespace greenland
168} // namespace art