TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 1 | /* |
| 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 "runtime_support_builder_thumb2.h" |
| 18 | |
| 19 | #include "ir_builder.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 20 | #include "mirror/object.h" |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 21 | #include "monitor.h" |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 22 | #include "thread.h" |
| 23 | #include "utils_llvm.h" |
| 24 | |
| 25 | #include <llvm/DerivedTypes.h> |
| 26 | #include <llvm/Function.h> |
| 27 | #include <llvm/InlineAsm.h> |
| 28 | #include <llvm/Module.h> |
| 29 | #include <llvm/Type.h> |
| 30 | |
| 31 | #include <inttypes.h> |
| 32 | #include <vector> |
| 33 | |
| 34 | using namespace llvm; |
| 35 | |
| 36 | namespace art { |
| 37 | namespace compiler_llvm { |
| 38 | |
| 39 | |
| 40 | void RuntimeSupportBuilderThumb2::EmitLockObject(llvm::Value* object) { |
| 41 | FunctionType* func_ty = FunctionType::get(/*Result=*/irb_.getInt32Ty(), |
| 42 | /*Params=*/irb_.getJObjectTy(), |
| 43 | /*isVarArg=*/false); |
| 44 | // $0: result |
| 45 | // $1: object |
| 46 | // $2: temp |
| 47 | // $3: temp |
| 48 | std::string asms; |
| 49 | StringAppendF(&asms, "add $3, $1, #%"PRId32"\n", Object::MonitorOffset().Int32Value()); |
| 50 | StringAppendF(&asms, "ldr $2, [r9, #%"PRId32"]\n", Thread::ThinLockIdOffset().Int32Value()); |
| 51 | StringAppendF(&asms, "ldrex $0, [$3]\n"); |
| 52 | StringAppendF(&asms, "lsl $2, $2, %d\n", LW_LOCK_OWNER_SHIFT); |
| 53 | StringAppendF(&asms, "bfi $2, $0, #0, #%d\n", LW_LOCK_OWNER_SHIFT - 1); |
| 54 | StringAppendF(&asms, "bfc $0, #%d, #%d\n", LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1); |
| 55 | StringAppendF(&asms, "cmp $0, #0\n"); |
| 56 | StringAppendF(&asms, "it eq\n"); |
| 57 | StringAppendF(&asms, "strexeq $0, $2, [$3]\n"); |
| 58 | |
| 59 | InlineAsm* func = InlineAsm::get(func_ty, asms, "=&l,l,~l,~l", true); |
| 60 | |
| 61 | llvm::Value* retry_slow_path = irb_.CreateCall(func, object); |
| 62 | retry_slow_path = irb_.CreateICmpNE(retry_slow_path, irb_.getJInt(0)); |
| 63 | |
| 64 | llvm::Function* parent_func = irb_.GetInsertBlock()->getParent(); |
| 65 | BasicBlock* basic_block_lock = BasicBlock::Create(context_, "lock", parent_func); |
| 66 | BasicBlock* basic_block_cont = BasicBlock::Create(context_, "lock_cont", parent_func); |
| 67 | irb_.CreateCondBr(retry_slow_path, basic_block_lock, basic_block_cont, kUnlikely); |
| 68 | |
| 69 | irb_.SetInsertPoint(basic_block_lock); |
| 70 | Function* slow_func = GetRuntimeSupportFunction(runtime_support::LockObject); |
| 71 | irb_.CreateCall2(slow_func, object, EmitGetCurrentThread()); |
| 72 | irb_.CreateBr(basic_block_cont); |
| 73 | |
| 74 | irb_.SetInsertPoint(basic_block_cont); |
| 75 | { // Memory barrier |
| 76 | FunctionType* asm_ty = FunctionType::get(/*Result=*/Type::getVoidTy(context_), |
| 77 | /*isVarArg=*/false); |
| 78 | InlineAsm* func = InlineAsm::get(asm_ty, "dmb sy", "", true); |
| 79 | irb_.CreateCall(func); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | |
| 84 | } // namespace compiler_llvm |
| 85 | } // namespace art |