blob: 76665f7fa79c972818b8fb94cfd959fdc9378f7c [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
TDYa12783bb6622012-04-17 02:20:34 -070019#include "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,
89 llvm::Value* method, uint32_t size) {
90 Value* old_shadow_frame = EmitLoadFromThreadOffset(Thread::TopShadowFrameOffset().Int32Value(),
91 irb_.getArtFrameTy()->getPointerTo(),
92 kTBAARuntimeInfo);
93 EmitStoreToThreadOffset(Thread::TopShadowFrameOffset().Int32Value(),
94 new_shadow_frame,
95 kTBAARuntimeInfo);
96
97 // Store the method pointer
98 irb_.StoreToObjectOffset(new_shadow_frame,
99 ShadowFrame::MethodOffset(),
100 method,
101 kTBAAShadowFrame);
102
103 // Store the number of the pointer slots
104 irb_.StoreToObjectOffset(new_shadow_frame,
105 ShadowFrame::NumberOfReferencesOffset(),
106 irb_.getInt32(size),
107 kTBAAShadowFrame);
108
109 // Store the link to previous shadow frame
110 irb_.StoreToObjectOffset(new_shadow_frame,
111 ShadowFrame::LinkOffset(),
112 old_shadow_frame,
113 kTBAAShadowFrame);
114
115 return old_shadow_frame;
116}
117
118llvm::Value*
119RuntimeSupportBuilder::EmitPushShadowFrameNoInline(llvm::Value* new_shadow_frame,
120 llvm::Value* method, uint32_t size) {
121 Function* func = GetRuntimeSupportFunction(runtime_support::PushShadowFrame);
122 llvm::CallInst* call_inst =
123 irb_.CreateCall4(func, EmitGetCurrentThread(), new_shadow_frame, method, irb_.getInt32(size));
124 irb_.SetTBAA(call_inst, kTBAARuntimeInfo);
125 return call_inst;
126}
127
128void RuntimeSupportBuilder::EmitPopShadowFrame(llvm::Value* old_shadow_frame) {
129 // Store old shadow frame to TopShadowFrame
130 EmitStoreToThreadOffset(Thread::TopShadowFrameOffset().Int32Value(),
131 old_shadow_frame,
132 kTBAARuntimeInfo);
133}
134
135
TDYa127823433d2012-09-26 16:03:51 -0700136/* Exception */
137
138llvm::Value* RuntimeSupportBuilder::EmitGetAndClearException() {
139 Function* slow_func = GetRuntimeSupportFunction(runtime_support::GetAndClearException);
140 return irb_.CreateCall(slow_func, EmitGetCurrentThread());
141}
TDYa127de479be2012-05-31 08:03:26 -0700142
143llvm::Value* RuntimeSupportBuilder::EmitIsExceptionPending() {
144 Value* exception = EmitLoadFromThreadOffset(Thread::ExceptionOffset().Int32Value(),
145 irb_.getJObjectTy(),
146 kTBAAJRuntime);
147 // If exception not null
TDYa1279a129452012-07-19 03:10:08 -0700148 return irb_.CreateIsNotNull(exception);
TDYa127de479be2012-05-31 08:03:26 -0700149}
150
TDYa127823433d2012-09-26 16:03:51 -0700151
152/* Suspend */
153
TDYa127de479be2012-05-31 08:03:26 -0700154void RuntimeSupportBuilder::EmitTestSuspend() {
155 Function* slow_func = GetRuntimeSupportFunction(runtime_support::TestSuspend);
TDYa127823433d2012-09-26 16:03:51 -0700156 Value* suspend_count = EmitLoadFromThreadOffset(Thread::ThreadFlagsOffset().Int32Value(),
157 irb_.getInt16Ty(),
TDYa127de479be2012-05-31 08:03:26 -0700158 kTBAARuntimeInfo);
TDYa127823433d2012-09-26 16:03:51 -0700159 Value* is_suspend = irb_.CreateICmpNE(suspend_count, irb_.getInt16(0));
TDYa127de479be2012-05-31 08:03:26 -0700160
TDYa1279a129452012-07-19 03:10:08 -0700161 Function* parent_func = irb_.GetInsertBlock()->getParent();
TDYa127de479be2012-05-31 08:03:26 -0700162 BasicBlock* basic_block_suspend = BasicBlock::Create(context_, "suspend", parent_func);
163 BasicBlock* basic_block_cont = BasicBlock::Create(context_, "suspend_cont", parent_func);
164 irb_.CreateCondBr(is_suspend, basic_block_suspend, basic_block_cont, kUnlikely);
165
166 irb_.SetInsertPoint(basic_block_suspend);
167 CallInst* call_inst = irb_.CreateCall(slow_func, EmitGetCurrentThread());
168 irb_.SetTBAA(call_inst, kTBAARuntimeInfo);
169 irb_.CreateBr(basic_block_cont);
170
171 irb_.SetInsertPoint(basic_block_cont);
172}
173
174
TDYa127b08ed122012-06-05 23:51:19 -0700175/* Monitor */
176
177void RuntimeSupportBuilder::EmitLockObject(llvm::Value* object) {
178 // TODO: Implement a fast path.
179 Function* slow_func = GetRuntimeSupportFunction(runtime_support::LockObject);
180 irb_.CreateCall2(slow_func, object, EmitGetCurrentThread());
181}
182
183void RuntimeSupportBuilder::EmitUnlockObject(llvm::Value* object) {
TDYa1279a129452012-07-19 03:10:08 -0700184 Value* lock_id =
TDYa127b08ed122012-06-05 23:51:19 -0700185 EmitLoadFromThreadOffset(Thread::ThinLockIdOffset().Int32Value(),
186 irb_.getJIntTy(),
187 kTBAARuntimeInfo);
TDYa1279a129452012-07-19 03:10:08 -0700188 Value* monitor =
TDYa127b08ed122012-06-05 23:51:19 -0700189 irb_.LoadFromObjectOffset(object,
190 Object::MonitorOffset().Int32Value(),
191 irb_.getJIntTy(),
192 kTBAARuntimeInfo);
193
TDYa1279a129452012-07-19 03:10:08 -0700194 Value* my_monitor = irb_.CreateShl(lock_id, LW_LOCK_OWNER_SHIFT);
195 Value* hash_state = irb_.CreateAnd(monitor, (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT));
196 Value* real_monitor = irb_.CreateAnd(monitor, ~(LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT));
TDYa127b08ed122012-06-05 23:51:19 -0700197
198 // Is thin lock, held by us and not recursively acquired
TDYa1279a129452012-07-19 03:10:08 -0700199 Value* is_fast_path = irb_.CreateICmpEQ(real_monitor, my_monitor);
TDYa127b08ed122012-06-05 23:51:19 -0700200
TDYa1279a129452012-07-19 03:10:08 -0700201 Function* parent_func = irb_.GetInsertBlock()->getParent();
TDYa127b08ed122012-06-05 23:51:19 -0700202 BasicBlock* bb_fast = BasicBlock::Create(context_, "unlock_fast", parent_func);
203 BasicBlock* bb_slow = BasicBlock::Create(context_, "unlock_slow", parent_func);
204 BasicBlock* bb_cont = BasicBlock::Create(context_, "unlock_cont", parent_func);
205 irb_.CreateCondBr(is_fast_path, bb_fast, bb_slow, kLikely);
206
207 irb_.SetInsertPoint(bb_fast);
208 // Set all bits to zero (except hash state)
209 irb_.StoreToObjectOffset(object,
210 Object::MonitorOffset().Int32Value(),
211 hash_state,
212 kTBAARuntimeInfo);
213 irb_.CreateBr(bb_cont);
214
215 irb_.SetInsertPoint(bb_slow);
216 Function* slow_func = GetRuntimeSupportFunction(runtime_support::UnlockObject);
217 irb_.CreateCall2(slow_func, object, EmitGetCurrentThread());
218 irb_.CreateBr(bb_cont);
219
220 irb_.SetInsertPoint(bb_cont);
221}
222
223
TDYa1279a129452012-07-19 03:10:08 -0700224void RuntimeSupportBuilder::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
225 Function* parent_func = irb_.GetInsertBlock()->getParent();
226 BasicBlock* bb_mark_gc_card = BasicBlock::Create(context_, "mark_gc_card", parent_func);
227 BasicBlock* bb_cont = BasicBlock::Create(context_, "mark_gc_card_cont", parent_func);
TDYa127d668a062012-04-13 12:36:57 -0700228
TDYa1279a129452012-07-19 03:10:08 -0700229 llvm::Value* not_null = irb_.CreateIsNotNull(value);
230 irb_.CreateCondBr(not_null, bb_mark_gc_card, bb_cont);
TDYa127d668a062012-04-13 12:36:57 -0700231
TDYa1279a129452012-07-19 03:10:08 -0700232 irb_.SetInsertPoint(bb_mark_gc_card);
233 Value* card_table = EmitLoadFromThreadOffset(Thread::CardTableOffset().Int32Value(),
234 irb_.getInt8Ty()->getPointerTo(),
235 kTBAAConstJObject);
236 Value* target_addr_int = irb_.CreatePtrToInt(target_addr, irb_.getPtrEquivIntTy());
237 Value* card_no = irb_.CreateLShr(target_addr_int, irb_.getPtrEquivInt(GC_CARD_SHIFT));
238 Value* card_table_entry = irb_.CreateGEP(card_table, card_no);
239 irb_.CreateStore(irb_.getInt8(GC_CARD_DIRTY), card_table_entry, kTBAARuntimeInfo);
240 irb_.CreateBr(bb_cont);
TDYa127d668a062012-04-13 12:36:57 -0700241
TDYa1279a129452012-07-19 03:10:08 -0700242 irb_.SetInsertPoint(bb_cont);
TDYa127d668a062012-04-13 12:36:57 -0700243}
244
TDYa1279a129452012-07-19 03:10:08 -0700245
TDYa127d668a062012-04-13 12:36:57 -0700246} // namespace compiler_llvm
247} // namespace art