blob: 169f8e8b42ef4f2d12cb2a27d0eff0836ff4a7d5 [file] [log] [blame]
TDYa127d668a062012-04-13 12:36:57 -07001/*
2 * Copyright (C) 2011 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 "runtime_support_builder.h"
18
Mathieu Chartier8ed6d612012-10-09 14:39:28 -070019#include "gc/card_table.h"
TDYa127d668a062012-04-13 12:36:57 -070020#include "ir_builder.h"
TDYa127b08ed122012-06-05 23:51:19 -070021#include "monitor.h"
22#include "object.h"
TDYa127d668a062012-04-13 12:36:57 -070023#include "thread.h"
24
25#include <llvm/DerivedTypes.h>
26#include <llvm/Function.h>
27#include <llvm/Module.h>
28#include <llvm/Type.h>
29
30using namespace llvm;
31
32namespace art {
33namespace compiler_llvm {
34
35using namespace runtime_support;
36
37
38RuntimeSupportBuilder::RuntimeSupportBuilder(llvm::LLVMContext& context,
39 llvm::Module& module,
40 IRBuilder& irb)
41 : context_(context), module_(module), irb_(irb)
42{
TDYa12783bb6622012-04-17 02:20:34 -070043 memset(target_runtime_support_func_, 0, sizeof(target_runtime_support_func_));
TDYa127d668a062012-04-13 12:36:57 -070044#define GET_RUNTIME_SUPPORT_FUNC_DECL(ID, NAME) \
45 do { \
46 llvm::Function* fn = module_.getFunction(#NAME); \
47 DCHECK_NE(fn, (void*)NULL) << "Function not found: " << #NAME; \
TDYa127de479be2012-05-31 08:03:26 -070048 runtime_support_func_decls_[runtime_support::ID] = fn; \
TDYa127d668a062012-04-13 12:36:57 -070049 } while (0);
50
51#include "runtime_support_func_list.h"
52 RUNTIME_SUPPORT_FUNC_LIST(GET_RUNTIME_SUPPORT_FUNC_DECL)
53#undef RUNTIME_SUPPORT_FUNC_LIST
54#undef GET_RUNTIME_SUPPORT_FUNC_DECL
55}
56
TDYa127de479be2012-05-31 08:03:26 -070057
58/* Thread */
59
60llvm::Value* RuntimeSupportBuilder::EmitGetCurrentThread() {
61 Function* func = GetRuntimeSupportFunction(runtime_support::GetCurrentThread);
TDYa1279a129452012-07-19 03:10:08 -070062 CallInst* call_inst = irb_.CreateCall(func);
TDYa127de479be2012-05-31 08:03:26 -070063 call_inst->setOnlyReadsMemory();
64 irb_.SetTBAA(call_inst, kTBAAConstJObject);
65 return call_inst;
66}
67
68llvm::Value* RuntimeSupportBuilder::EmitLoadFromThreadOffset(int64_t offset, llvm::Type* type,
69 TBAASpecialType s_ty) {
TDYa1279a129452012-07-19 03:10:08 -070070 Value* thread = EmitGetCurrentThread();
TDYa127de479be2012-05-31 08:03:26 -070071 return irb_.LoadFromObjectOffset(thread, offset, type, s_ty);
72}
73
74void RuntimeSupportBuilder::EmitStoreToThreadOffset(int64_t offset, llvm::Value* value,
75 TBAASpecialType s_ty) {
TDYa1279a129452012-07-19 03:10:08 -070076 Value* thread = EmitGetCurrentThread();
TDYa127de479be2012-05-31 08:03:26 -070077 irb_.StoreToObjectOffset(thread, offset, value, s_ty);
78}
79
TDYa127c1478262012-06-20 20:22:27 -070080llvm::Value* RuntimeSupportBuilder::EmitSetCurrentThread(llvm::Value* thread) {
TDYa127de479be2012-05-31 08:03:26 -070081 Function* func = GetRuntimeSupportFunction(runtime_support::SetCurrentThread);
TDYa127c1478262012-06-20 20:22:27 -070082 return irb_.CreateCall(func, thread);
TDYa127de479be2012-05-31 08:03:26 -070083}
84
85
86/* ShadowFrame */
87
88llvm::Value* RuntimeSupportBuilder::EmitPushShadowFrame(llvm::Value* new_shadow_frame,
TDYa127ce4cc0d2012-11-18 16:59:53 -080089 llvm::Value* method,
90 uint32_t num_vregs) {
TDYa127de479be2012-05-31 08:03:26 -070091 Value* old_shadow_frame = EmitLoadFromThreadOffset(Thread::TopShadowFrameOffset().Int32Value(),
92 irb_.getArtFrameTy()->getPointerTo(),
93 kTBAARuntimeInfo);
94 EmitStoreToThreadOffset(Thread::TopShadowFrameOffset().Int32Value(),
95 new_shadow_frame,
96 kTBAARuntimeInfo);
97
98 // Store the method pointer
99 irb_.StoreToObjectOffset(new_shadow_frame,
100 ShadowFrame::MethodOffset(),
101 method,
102 kTBAAShadowFrame);
103
Ian Rogers5438ad82012-10-15 17:22:44 -0700104 // Store the number of vregs
105 irb_.StoreToObjectOffset(new_shadow_frame,
106 ShadowFrame::NumberOfVRegsOffset(),
TDYa127ce4cc0d2012-11-18 16:59:53 -0800107 irb_.getInt32(num_vregs),
TDYa127de479be2012-05-31 08:03:26 -0700108 kTBAAShadowFrame);
109
110 // Store the link to previous shadow frame
111 irb_.StoreToObjectOffset(new_shadow_frame,
112 ShadowFrame::LinkOffset(),
113 old_shadow_frame,
114 kTBAAShadowFrame);
115
116 return old_shadow_frame;
117}
118
119llvm::Value*
120RuntimeSupportBuilder::EmitPushShadowFrameNoInline(llvm::Value* new_shadow_frame,
TDYa127ce4cc0d2012-11-18 16:59:53 -0800121 llvm::Value* method,
122 uint32_t num_vregs) {
TDYa127de479be2012-05-31 08:03:26 -0700123 Function* func = GetRuntimeSupportFunction(runtime_support::PushShadowFrame);
124 llvm::CallInst* call_inst =
TDYa127ce4cc0d2012-11-18 16:59:53 -0800125 irb_.CreateCall4(func,
TDYa127f54f3ac2012-10-16 22:39:00 -0700126 EmitGetCurrentThread(),
127 new_shadow_frame,
128 method,
TDYa127ce4cc0d2012-11-18 16:59:53 -0800129 irb_.getInt32(num_vregs));
TDYa127de479be2012-05-31 08:03:26 -0700130 irb_.SetTBAA(call_inst, kTBAARuntimeInfo);
131 return call_inst;
132}
133
134void RuntimeSupportBuilder::EmitPopShadowFrame(llvm::Value* old_shadow_frame) {
135 // Store old shadow frame to TopShadowFrame
136 EmitStoreToThreadOffset(Thread::TopShadowFrameOffset().Int32Value(),
137 old_shadow_frame,
138 kTBAARuntimeInfo);
139}
140
141
TDYa127823433d2012-09-26 16:03:51 -0700142/* Exception */
143
144llvm::Value* RuntimeSupportBuilder::EmitGetAndClearException() {
145 Function* slow_func = GetRuntimeSupportFunction(runtime_support::GetAndClearException);
146 return irb_.CreateCall(slow_func, EmitGetCurrentThread());
147}
TDYa127de479be2012-05-31 08:03:26 -0700148
149llvm::Value* RuntimeSupportBuilder::EmitIsExceptionPending() {
Jeff Haoa8fd2d52013-01-17 23:10:27 +0000150 Value* exception = EmitLoadFromThreadOffset(Thread::ExceptionOffset().Int32Value(),
151 irb_.getJObjectTy(),
152 kTBAARuntimeInfo);
153 // If exception not null
154 return irb_.CreateIsNotNull(exception);
TDYa127de479be2012-05-31 08:03:26 -0700155}
156
TDYa127823433d2012-09-26 16:03:51 -0700157
158/* Suspend */
159
TDYa127de479be2012-05-31 08:03:26 -0700160void RuntimeSupportBuilder::EmitTestSuspend() {
161 Function* slow_func = GetRuntimeSupportFunction(runtime_support::TestSuspend);
TDYa127de479be2012-05-31 08:03:26 -0700162 CallInst* call_inst = irb_.CreateCall(slow_func, EmitGetCurrentThread());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800163 irb_.SetTBAA(call_inst, kTBAAJRuntime);
TDYa127de479be2012-05-31 08:03:26 -0700164}
165
166
TDYa127b08ed122012-06-05 23:51:19 -0700167/* Monitor */
168
169void RuntimeSupportBuilder::EmitLockObject(llvm::Value* object) {
170 // TODO: Implement a fast path.
171 Function* slow_func = GetRuntimeSupportFunction(runtime_support::LockObject);
172 irb_.CreateCall2(slow_func, object, EmitGetCurrentThread());
173}
174
175void RuntimeSupportBuilder::EmitUnlockObject(llvm::Value* object) {
TDYa1279a129452012-07-19 03:10:08 -0700176 Value* lock_id =
TDYa127b08ed122012-06-05 23:51:19 -0700177 EmitLoadFromThreadOffset(Thread::ThinLockIdOffset().Int32Value(),
178 irb_.getJIntTy(),
179 kTBAARuntimeInfo);
TDYa1279a129452012-07-19 03:10:08 -0700180 Value* monitor =
TDYa127b08ed122012-06-05 23:51:19 -0700181 irb_.LoadFromObjectOffset(object,
182 Object::MonitorOffset().Int32Value(),
183 irb_.getJIntTy(),
184 kTBAARuntimeInfo);
185
TDYa1279a129452012-07-19 03:10:08 -0700186 Value* my_monitor = irb_.CreateShl(lock_id, LW_LOCK_OWNER_SHIFT);
187 Value* hash_state = irb_.CreateAnd(monitor, (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT));
188 Value* real_monitor = irb_.CreateAnd(monitor, ~(LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT));
TDYa127b08ed122012-06-05 23:51:19 -0700189
190 // Is thin lock, held by us and not recursively acquired
TDYa1279a129452012-07-19 03:10:08 -0700191 Value* is_fast_path = irb_.CreateICmpEQ(real_monitor, my_monitor);
TDYa127b08ed122012-06-05 23:51:19 -0700192
TDYa1279a129452012-07-19 03:10:08 -0700193 Function* parent_func = irb_.GetInsertBlock()->getParent();
TDYa127b08ed122012-06-05 23:51:19 -0700194 BasicBlock* bb_fast = BasicBlock::Create(context_, "unlock_fast", parent_func);
195 BasicBlock* bb_slow = BasicBlock::Create(context_, "unlock_slow", parent_func);
196 BasicBlock* bb_cont = BasicBlock::Create(context_, "unlock_cont", parent_func);
197 irb_.CreateCondBr(is_fast_path, bb_fast, bb_slow, kLikely);
198
199 irb_.SetInsertPoint(bb_fast);
200 // Set all bits to zero (except hash state)
201 irb_.StoreToObjectOffset(object,
202 Object::MonitorOffset().Int32Value(),
203 hash_state,
204 kTBAARuntimeInfo);
205 irb_.CreateBr(bb_cont);
206
207 irb_.SetInsertPoint(bb_slow);
208 Function* slow_func = GetRuntimeSupportFunction(runtime_support::UnlockObject);
209 irb_.CreateCall2(slow_func, object, EmitGetCurrentThread());
210 irb_.CreateBr(bb_cont);
211
212 irb_.SetInsertPoint(bb_cont);
213}
214
215
TDYa1279a129452012-07-19 03:10:08 -0700216void RuntimeSupportBuilder::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
217 Function* parent_func = irb_.GetInsertBlock()->getParent();
218 BasicBlock* bb_mark_gc_card = BasicBlock::Create(context_, "mark_gc_card", parent_func);
219 BasicBlock* bb_cont = BasicBlock::Create(context_, "mark_gc_card_cont", parent_func);
TDYa127d668a062012-04-13 12:36:57 -0700220
TDYa1279a129452012-07-19 03:10:08 -0700221 llvm::Value* not_null = irb_.CreateIsNotNull(value);
222 irb_.CreateCondBr(not_null, bb_mark_gc_card, bb_cont);
TDYa127d668a062012-04-13 12:36:57 -0700223
TDYa1279a129452012-07-19 03:10:08 -0700224 irb_.SetInsertPoint(bb_mark_gc_card);
225 Value* card_table = EmitLoadFromThreadOffset(Thread::CardTableOffset().Int32Value(),
226 irb_.getInt8Ty()->getPointerTo(),
227 kTBAAConstJObject);
228 Value* target_addr_int = irb_.CreatePtrToInt(target_addr, irb_.getPtrEquivIntTy());
Mathieu Chartier8ed6d612012-10-09 14:39:28 -0700229 Value* card_no = irb_.CreateLShr(target_addr_int, irb_.getPtrEquivInt(CardTable::kCardShift));
TDYa1279a129452012-07-19 03:10:08 -0700230 Value* card_table_entry = irb_.CreateGEP(card_table, card_no);
Mathieu Chartier8ed6d612012-10-09 14:39:28 -0700231 irb_.CreateStore(irb_.getInt8(CardTable::kCardDirty), card_table_entry, kTBAARuntimeInfo);
TDYa1279a129452012-07-19 03:10:08 -0700232 irb_.CreateBr(bb_cont);
TDYa127d668a062012-04-13 12:36:57 -0700233
TDYa1279a129452012-07-19 03:10:08 -0700234 irb_.SetInsertPoint(bb_cont);
TDYa127d668a062012-04-13 12:36:57 -0700235}
236
TDYa1279a129452012-07-19 03:10:08 -0700237
TDYa127d668a062012-04-13 12:36:57 -0700238} // namespace compiler_llvm
239} // namespace art