blob: eff29c8b042372be1990b5e5d3cdae14c13043eb [file] [log] [blame]
TDYa127b08ed122012-06-05 23:51:19 -07001/*
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 Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "mirror/object.h"
TDYa127b08ed122012-06-05 23:51:19 -070021#include "monitor.h"
TDYa127b08ed122012-06-05 23:51:19 -070022#include "thread.h"
23#include "utils_llvm.h"
24
Brian Carlstrom37d48792013-03-22 14:14:45 -070025#include <llvm/IR/DerivedTypes.h>
26#include <llvm/IR/Function.h>
27#include <llvm/IR/InlineAsm.h>
28#include <llvm/IR/Module.h>
29#include <llvm/IR/Type.h>
TDYa127b08ed122012-06-05 23:51:19 -070030
31#include <inttypes.h>
32#include <vector>
33
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070034using ::llvm::BasicBlock;
35using ::llvm::Function;
36using ::llvm::FunctionType;
37using ::llvm::InlineAsm;
38using ::llvm::Type;
39using ::llvm::Value;
TDYa127b08ed122012-06-05 23:51:19 -070040
41namespace art {
Ian Rogers4c1c2832013-03-04 18:30:13 -080042namespace llvm {
TDYa127b08ed122012-06-05 23:51:19 -070043
44
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070045void RuntimeSupportBuilderThumb2::EmitLockObject(Value* object) {
TDYa127b08ed122012-06-05 23:51:19 -070046 FunctionType* func_ty = FunctionType::get(/*Result=*/irb_.getInt32Ty(),
47 /*Params=*/irb_.getJObjectTy(),
48 /*isVarArg=*/false);
49 // $0: result
50 // $1: object
51 // $2: temp
52 // $3: temp
53 std::string asms;
Mathieu Chartier9b3c3cd2013-08-12 17:41:54 -070054 StringAppendF(&asms, "add $3, $1, #%" PRId32 "\n", mirror::Object::MonitorOffset().Int32Value());
55 StringAppendF(&asms, "ldr $2, [r9, #%" PRId32 "]\n", Thread::ThinLockIdOffset().Int32Value());
TDYa127b08ed122012-06-05 23:51:19 -070056 StringAppendF(&asms, "ldrex $0, [$3]\n");
57 StringAppendF(&asms, "lsl $2, $2, %d\n", LW_LOCK_OWNER_SHIFT);
58 StringAppendF(&asms, "bfi $2, $0, #0, #%d\n", LW_LOCK_OWNER_SHIFT - 1);
59 StringAppendF(&asms, "bfc $0, #%d, #%d\n", LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1);
60 StringAppendF(&asms, "cmp $0, #0\n");
61 StringAppendF(&asms, "it eq\n");
62 StringAppendF(&asms, "strexeq $0, $2, [$3]\n");
63
64 InlineAsm* func = InlineAsm::get(func_ty, asms, "=&l,l,~l,~l", true);
65
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070066 Value* retry_slow_path = irb_.CreateCall(func, object);
TDYa127b08ed122012-06-05 23:51:19 -070067 retry_slow_path = irb_.CreateICmpNE(retry_slow_path, irb_.getJInt(0));
68
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070069 Function* parent_func = irb_.GetInsertBlock()->getParent();
TDYa127b08ed122012-06-05 23:51:19 -070070 BasicBlock* basic_block_lock = BasicBlock::Create(context_, "lock", parent_func);
71 BasicBlock* basic_block_cont = BasicBlock::Create(context_, "lock_cont", parent_func);
72 irb_.CreateCondBr(retry_slow_path, basic_block_lock, basic_block_cont, kUnlikely);
73
74 irb_.SetInsertPoint(basic_block_lock);
75 Function* slow_func = GetRuntimeSupportFunction(runtime_support::LockObject);
76 irb_.CreateCall2(slow_func, object, EmitGetCurrentThread());
77 irb_.CreateBr(basic_block_cont);
78
79 irb_.SetInsertPoint(basic_block_cont);
Brian Carlstrom7934ac22013-07-26 10:54:15 -070080 { // Memory barrier
TDYa127b08ed122012-06-05 23:51:19 -070081 FunctionType* asm_ty = FunctionType::get(/*Result=*/Type::getVoidTy(context_),
82 /*isVarArg=*/false);
83 InlineAsm* func = InlineAsm::get(asm_ty, "dmb sy", "", true);
84 irb_.CreateCall(func);
85 }
86}
87
88
Brian Carlstrom7934ac22013-07-26 10:54:15 -070089} // namespace llvm
90} // namespace art