TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
TDYa127 | 83bb662 | 2012-04-17 02:20:34 -0700 | [diff] [blame] | 19 | #include "card_table.h" |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 20 | #include "ir_builder.h" |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 21 | #include "monitor.h" |
| 22 | #include "object.h" |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 23 | #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 | |
| 30 | using namespace llvm; |
| 31 | |
| 32 | namespace art { |
| 33 | namespace compiler_llvm { |
| 34 | |
| 35 | using namespace runtime_support; |
| 36 | |
| 37 | |
| 38 | RuntimeSupportBuilder::RuntimeSupportBuilder(llvm::LLVMContext& context, |
| 39 | llvm::Module& module, |
| 40 | IRBuilder& irb) |
| 41 | : context_(context), module_(module), irb_(irb) |
| 42 | { |
TDYa127 | 83bb662 | 2012-04-17 02:20:34 -0700 | [diff] [blame] | 43 | memset(target_runtime_support_func_, 0, sizeof(target_runtime_support_func_)); |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 44 | #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; \ |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 48 | runtime_support_func_decls_[runtime_support::ID] = fn; \ |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 49 | } 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 | |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 57 | |
| 58 | /* Thread */ |
| 59 | |
| 60 | llvm::Value* RuntimeSupportBuilder::EmitGetCurrentThread() { |
| 61 | Function* func = GetRuntimeSupportFunction(runtime_support::GetCurrentThread); |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 62 | CallInst* call_inst = irb_.CreateCall(func); |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 63 | call_inst->setOnlyReadsMemory(); |
| 64 | irb_.SetTBAA(call_inst, kTBAAConstJObject); |
| 65 | return call_inst; |
| 66 | } |
| 67 | |
| 68 | llvm::Value* RuntimeSupportBuilder::EmitLoadFromThreadOffset(int64_t offset, llvm::Type* type, |
| 69 | TBAASpecialType s_ty) { |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 70 | Value* thread = EmitGetCurrentThread(); |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 71 | return irb_.LoadFromObjectOffset(thread, offset, type, s_ty); |
| 72 | } |
| 73 | |
| 74 | void RuntimeSupportBuilder::EmitStoreToThreadOffset(int64_t offset, llvm::Value* value, |
| 75 | TBAASpecialType s_ty) { |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 76 | Value* thread = EmitGetCurrentThread(); |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 77 | irb_.StoreToObjectOffset(thread, offset, value, s_ty); |
| 78 | } |
| 79 | |
TDYa127 | c147826 | 2012-06-20 20:22:27 -0700 | [diff] [blame] | 80 | llvm::Value* RuntimeSupportBuilder::EmitSetCurrentThread(llvm::Value* thread) { |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 81 | Function* func = GetRuntimeSupportFunction(runtime_support::SetCurrentThread); |
TDYa127 | c147826 | 2012-06-20 20:22:27 -0700 | [diff] [blame] | 82 | return irb_.CreateCall(func, thread); |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
| 86 | /* ShadowFrame */ |
| 87 | |
| 88 | llvm::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 | |
| 118 | llvm::Value* |
| 119 | RuntimeSupportBuilder::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 | |
| 128 | void 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 | |
TDYa127 | 823433d | 2012-09-26 16:03:51 -0700 | [diff] [blame] | 136 | /* Exception */ |
| 137 | |
| 138 | llvm::Value* RuntimeSupportBuilder::EmitGetAndClearException() { |
| 139 | Function* slow_func = GetRuntimeSupportFunction(runtime_support::GetAndClearException); |
| 140 | return irb_.CreateCall(slow_func, EmitGetCurrentThread()); |
| 141 | } |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 142 | |
| 143 | llvm::Value* RuntimeSupportBuilder::EmitIsExceptionPending() { |
| 144 | Value* exception = EmitLoadFromThreadOffset(Thread::ExceptionOffset().Int32Value(), |
| 145 | irb_.getJObjectTy(), |
| 146 | kTBAAJRuntime); |
| 147 | // If exception not null |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 148 | return irb_.CreateIsNotNull(exception); |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 149 | } |
| 150 | |
TDYa127 | 823433d | 2012-09-26 16:03:51 -0700 | [diff] [blame] | 151 | |
| 152 | /* Suspend */ |
| 153 | |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 154 | void RuntimeSupportBuilder::EmitTestSuspend() { |
| 155 | Function* slow_func = GetRuntimeSupportFunction(runtime_support::TestSuspend); |
TDYa127 | 823433d | 2012-09-26 16:03:51 -0700 | [diff] [blame] | 156 | Value* suspend_count = EmitLoadFromThreadOffset(Thread::ThreadFlagsOffset().Int32Value(), |
| 157 | irb_.getInt16Ty(), |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 158 | kTBAARuntimeInfo); |
TDYa127 | 823433d | 2012-09-26 16:03:51 -0700 | [diff] [blame] | 159 | Value* is_suspend = irb_.CreateICmpNE(suspend_count, irb_.getInt16(0)); |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 160 | |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 161 | Function* parent_func = irb_.GetInsertBlock()->getParent(); |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 162 | 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 | |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 175 | /* Monitor */ |
| 176 | |
| 177 | void 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 | |
| 183 | void RuntimeSupportBuilder::EmitUnlockObject(llvm::Value* object) { |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 184 | Value* lock_id = |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 185 | EmitLoadFromThreadOffset(Thread::ThinLockIdOffset().Int32Value(), |
| 186 | irb_.getJIntTy(), |
| 187 | kTBAARuntimeInfo); |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 188 | Value* monitor = |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 189 | irb_.LoadFromObjectOffset(object, |
| 190 | Object::MonitorOffset().Int32Value(), |
| 191 | irb_.getJIntTy(), |
| 192 | kTBAARuntimeInfo); |
| 193 | |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 194 | 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)); |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 197 | |
| 198 | // Is thin lock, held by us and not recursively acquired |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 199 | Value* is_fast_path = irb_.CreateICmpEQ(real_monitor, my_monitor); |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 200 | |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 201 | Function* parent_func = irb_.GetInsertBlock()->getParent(); |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 202 | 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 | |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 224 | void 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); |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 228 | |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 229 | llvm::Value* not_null = irb_.CreateIsNotNull(value); |
| 230 | irb_.CreateCondBr(not_null, bb_mark_gc_card, bb_cont); |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 231 | |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 232 | 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); |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 241 | |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 242 | irb_.SetInsertPoint(bb_cont); |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 243 | } |
| 244 | |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 245 | |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 246 | } // namespace compiler_llvm |
| 247 | } // namespace art |