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 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 19 | #include "gc/accounting/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" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 22 | #include "mirror/object.h" |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 23 | #include "runtime_support_llvm_func_list.h" |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 24 | #include "thread.h" |
| 25 | |
Brian Carlstrom | 37d4879 | 2013-03-22 14:14:45 -0700 | [diff] [blame] | 26 | #include <llvm/IR/DerivedTypes.h> |
| 27 | #include <llvm/IR/Function.h> |
| 28 | #include <llvm/IR/Module.h> |
| 29 | #include <llvm/IR/Type.h> |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 30 | |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 31 | using ::llvm::BasicBlock; |
| 32 | using ::llvm::CallInst; |
| 33 | using ::llvm::Function; |
| 34 | using ::llvm::Value; |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 35 | |
| 36 | namespace art { |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 37 | namespace llvm { |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 38 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 39 | RuntimeSupportBuilder::RuntimeSupportBuilder(::llvm::LLVMContext& context, |
| 40 | ::llvm::Module& module, |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 41 | IRBuilder& irb) |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 42 | : context_(context), module_(module), irb_(irb) { |
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 { \ |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 46 | ::llvm::Function* fn = module_.getFunction(#NAME); \ |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame] | 47 | DCHECK(fn != 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 | |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 51 | RUNTIME_SUPPORT_FUNC_LIST(GET_RUNTIME_SUPPORT_FUNC_DECL) |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 52 | } |
| 53 | |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 54 | |
| 55 | /* Thread */ |
| 56 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 57 | ::llvm::Value* RuntimeSupportBuilder::EmitGetCurrentThread() { |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 58 | Function* func = GetRuntimeSupportFunction(runtime_support::GetCurrentThread); |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 59 | CallInst* call_inst = irb_.CreateCall(func); |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 60 | call_inst->setOnlyReadsMemory(); |
| 61 | irb_.SetTBAA(call_inst, kTBAAConstJObject); |
| 62 | return call_inst; |
| 63 | } |
| 64 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 65 | ::llvm::Value* RuntimeSupportBuilder::EmitLoadFromThreadOffset(int64_t offset, ::llvm::Type* type, |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 66 | TBAASpecialType s_ty) { |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 67 | Value* thread = EmitGetCurrentThread(); |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 68 | return irb_.LoadFromObjectOffset(thread, offset, type, s_ty); |
| 69 | } |
| 70 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 71 | void RuntimeSupportBuilder::EmitStoreToThreadOffset(int64_t offset, ::llvm::Value* value, |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 72 | TBAASpecialType s_ty) { |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 73 | Value* thread = EmitGetCurrentThread(); |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 74 | irb_.StoreToObjectOffset(thread, offset, value, s_ty); |
| 75 | } |
| 76 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 77 | ::llvm::Value* RuntimeSupportBuilder::EmitSetCurrentThread(::llvm::Value* thread) { |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 78 | Function* func = GetRuntimeSupportFunction(runtime_support::SetCurrentThread); |
TDYa127 | c147826 | 2012-06-20 20:22:27 -0700 | [diff] [blame] | 79 | return irb_.CreateCall(func, thread); |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | |
| 83 | /* ShadowFrame */ |
| 84 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 85 | ::llvm::Value* RuntimeSupportBuilder::EmitPushShadowFrame(::llvm::Value* new_shadow_frame, |
| 86 | ::llvm::Value* method, |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 87 | uint32_t num_vregs) { |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 88 | Value* old_shadow_frame = EmitLoadFromThreadOffset(Thread::TopShadowFrameOffset().Int32Value(), |
| 89 | irb_.getArtFrameTy()->getPointerTo(), |
| 90 | kTBAARuntimeInfo); |
| 91 | EmitStoreToThreadOffset(Thread::TopShadowFrameOffset().Int32Value(), |
| 92 | new_shadow_frame, |
| 93 | kTBAARuntimeInfo); |
| 94 | |
| 95 | // Store the method pointer |
| 96 | irb_.StoreToObjectOffset(new_shadow_frame, |
| 97 | ShadowFrame::MethodOffset(), |
| 98 | method, |
| 99 | kTBAAShadowFrame); |
| 100 | |
Ian Rogers | 5438ad8 | 2012-10-15 17:22:44 -0700 | [diff] [blame] | 101 | // Store the number of vregs |
| 102 | irb_.StoreToObjectOffset(new_shadow_frame, |
| 103 | ShadowFrame::NumberOfVRegsOffset(), |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 104 | irb_.getInt32(num_vregs), |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 105 | kTBAAShadowFrame); |
| 106 | |
| 107 | // Store the link to previous shadow frame |
| 108 | irb_.StoreToObjectOffset(new_shadow_frame, |
| 109 | ShadowFrame::LinkOffset(), |
| 110 | old_shadow_frame, |
| 111 | kTBAAShadowFrame); |
| 112 | |
| 113 | return old_shadow_frame; |
| 114 | } |
| 115 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 116 | ::llvm::Value* |
| 117 | RuntimeSupportBuilder::EmitPushShadowFrameNoInline(::llvm::Value* new_shadow_frame, |
| 118 | ::llvm::Value* method, |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 119 | uint32_t num_vregs) { |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 120 | Function* func = GetRuntimeSupportFunction(runtime_support::PushShadowFrame); |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 121 | ::llvm::CallInst* call_inst = |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 122 | irb_.CreateCall4(func, |
TDYa127 | f54f3ac | 2012-10-16 22:39:00 -0700 | [diff] [blame] | 123 | EmitGetCurrentThread(), |
| 124 | new_shadow_frame, |
| 125 | method, |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 126 | irb_.getInt32(num_vregs)); |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 127 | irb_.SetTBAA(call_inst, kTBAARuntimeInfo); |
| 128 | return call_inst; |
| 129 | } |
| 130 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 131 | void RuntimeSupportBuilder::EmitPopShadowFrame(::llvm::Value* old_shadow_frame) { |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 132 | // Store old shadow frame to TopShadowFrame |
| 133 | EmitStoreToThreadOffset(Thread::TopShadowFrameOffset().Int32Value(), |
| 134 | old_shadow_frame, |
| 135 | kTBAARuntimeInfo); |
| 136 | } |
| 137 | |
| 138 | |
TDYa127 | 823433d | 2012-09-26 16:03:51 -0700 | [diff] [blame] | 139 | /* Exception */ |
| 140 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 141 | ::llvm::Value* RuntimeSupportBuilder::EmitGetAndClearException() { |
TDYa127 | 823433d | 2012-09-26 16:03:51 -0700 | [diff] [blame] | 142 | Function* slow_func = GetRuntimeSupportFunction(runtime_support::GetAndClearException); |
| 143 | return irb_.CreateCall(slow_func, EmitGetCurrentThread()); |
| 144 | } |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 145 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 146 | ::llvm::Value* RuntimeSupportBuilder::EmitIsExceptionPending() { |
Jeff Hao | a8fd2d5 | 2013-01-17 23:10:27 +0000 | [diff] [blame] | 147 | Value* exception = EmitLoadFromThreadOffset(Thread::ExceptionOffset().Int32Value(), |
| 148 | irb_.getJObjectTy(), |
| 149 | kTBAARuntimeInfo); |
| 150 | // If exception not null |
| 151 | return irb_.CreateIsNotNull(exception); |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 152 | } |
| 153 | |
TDYa127 | 823433d | 2012-09-26 16:03:51 -0700 | [diff] [blame] | 154 | |
| 155 | /* Suspend */ |
| 156 | |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 157 | void RuntimeSupportBuilder::EmitTestSuspend() { |
| 158 | Function* slow_func = GetRuntimeSupportFunction(runtime_support::TestSuspend); |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 159 | CallInst* call_inst = irb_.CreateCall(slow_func, EmitGetCurrentThread()); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 160 | irb_.SetTBAA(call_inst, kTBAAJRuntime); |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 164 | /* Monitor */ |
| 165 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 166 | void RuntimeSupportBuilder::EmitLockObject(::llvm::Value* object) { |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 167 | Function* slow_func = GetRuntimeSupportFunction(runtime_support::LockObject); |
| 168 | irb_.CreateCall2(slow_func, object, EmitGetCurrentThread()); |
| 169 | } |
| 170 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 171 | void RuntimeSupportBuilder::EmitUnlockObject(::llvm::Value* object) { |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 172 | Function* slow_func = GetRuntimeSupportFunction(runtime_support::UnlockObject); |
| 173 | irb_.CreateCall2(slow_func, object, EmitGetCurrentThread()); |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 177 | void RuntimeSupportBuilder::EmitMarkGCCard(::llvm::Value* value, ::llvm::Value* target_addr) { |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 178 | Function* parent_func = irb_.GetInsertBlock()->getParent(); |
| 179 | BasicBlock* bb_mark_gc_card = BasicBlock::Create(context_, "mark_gc_card", parent_func); |
| 180 | BasicBlock* bb_cont = BasicBlock::Create(context_, "mark_gc_card_cont", parent_func); |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 181 | |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 182 | ::llvm::Value* not_null = irb_.CreateIsNotNull(value); |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 183 | irb_.CreateCondBr(not_null, bb_mark_gc_card, bb_cont); |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 184 | |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 185 | irb_.SetInsertPoint(bb_mark_gc_card); |
| 186 | Value* card_table = EmitLoadFromThreadOffset(Thread::CardTableOffset().Int32Value(), |
| 187 | irb_.getInt8Ty()->getPointerTo(), |
| 188 | kTBAAConstJObject); |
| 189 | Value* target_addr_int = irb_.CreatePtrToInt(target_addr, irb_.getPtrEquivIntTy()); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 190 | Value* card_no = irb_.CreateLShr(target_addr_int, |
| 191 | irb_.getPtrEquivInt(gc::accounting::CardTable::kCardShift)); |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 192 | Value* card_table_entry = irb_.CreateGEP(card_table, card_no); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 193 | irb_.CreateStore(irb_.getInt8(gc::accounting::CardTable::kCardDirty), card_table_entry, |
| 194 | kTBAARuntimeInfo); |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 195 | irb_.CreateBr(bb_cont); |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 196 | |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 197 | irb_.SetInsertPoint(bb_cont); |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 198 | } |
| 199 | |
TDYa127 | 9a12945 | 2012-07-19 03:10:08 -0700 | [diff] [blame] | 200 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 201 | } // namespace llvm |
| 202 | } // namespace art |