blob: cad46247fdd25bda8ac8dd6cc85fbcb573110da9 [file] [log] [blame]
TDYa127d668a062012-04-13 12:36:57 -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_arm.h"
18
19#include "ir_builder.h"
20#include "thread.h"
TDYa127853cd092012-04-21 22:15:31 -070021#include "utils_llvm.h"
TDYa127d668a062012-04-13 12:36:57 -070022
Brian Carlstrom37d48792013-03-22 14:14:45 -070023#include <llvm/IR/DerivedTypes.h>
24#include <llvm/IR/Function.h>
25#include <llvm/IR/InlineAsm.h>
26#include <llvm/IR/Module.h>
27#include <llvm/IR/Type.h>
TDYa127d668a062012-04-13 12:36:57 -070028
29#include <vector>
30
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070031using ::llvm::CallInst;
32using ::llvm::Function;
33using ::llvm::FunctionType;
34using ::llvm::InlineAsm;
35using ::llvm::IntegerType;
36using ::llvm::Type;
37using ::llvm::Value;
TDYa127d668a062012-04-13 12:36:57 -070038
TDYa127823433d2012-09-26 16:03:51 -070039namespace {
40
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070041char LDRSTRSuffixByType(art::llvm::IRBuilder& irb, Type* type) {
TDYa127823433d2012-09-26 16:03:51 -070042 int width = type->isPointerTy() ?
43 irb.getSizeOfPtrEquivInt()*8 :
Ian Rogers4c1c2832013-03-04 18:30:13 -080044 ::llvm::cast<IntegerType>(type)->getBitWidth();
TDYa127823433d2012-09-26 16:03:51 -070045 switch (width) {
46 case 8: return 'b';
47 case 16: return 'h';
48 case 32: return ' ';
49 default:
50 LOG(FATAL) << "Unsupported width: " << width;
51 return ' ';
52 }
53}
54
Brian Carlstrom7934ac22013-07-26 10:54:15 -070055} // namespace
TDYa127823433d2012-09-26 16:03:51 -070056
TDYa127d668a062012-04-13 12:36:57 -070057namespace art {
Ian Rogers4c1c2832013-03-04 18:30:13 -080058namespace llvm {
TDYa127d668a062012-04-13 12:36:57 -070059
TDYa127b08ed122012-06-05 23:51:19 -070060/* Thread */
TDYa127d668a062012-04-13 12:36:57 -070061
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070062Value* RuntimeSupportBuilderARM::EmitGetCurrentThread() {
TDYa127de479be2012-05-31 08:03:26 -070063 Function* ori_func = GetRuntimeSupportFunction(runtime_support::GetCurrentThread);
64 InlineAsm* func = InlineAsm::get(ori_func->getFunctionType(), "mov $0, r9", "=r", false);
65 CallInst* thread = irb_.CreateCall(func);
66 thread->setDoesNotAccessMemory();
67 irb_.SetTBAA(thread, kTBAAConstJObject);
68 return thread;
69}
TDYa127d668a062012-04-13 12:36:57 -070070
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070071Value* RuntimeSupportBuilderARM::EmitLoadFromThreadOffset(int64_t offset, ::llvm::Type* type,
72 TBAASpecialType s_ty) {
TDYa127de479be2012-05-31 08:03:26 -070073 FunctionType* func_ty = FunctionType::get(/*Result=*/type,
74 /*isVarArg=*/false);
TDYa127823433d2012-09-26 16:03:51 -070075 std::string inline_asm(StringPrintf("ldr%c $0, [r9, #%d]",
76 LDRSTRSuffixByType(irb_, type),
77 static_cast<int>(offset)));
TDYa127de479be2012-05-31 08:03:26 -070078 InlineAsm* func = InlineAsm::get(func_ty, inline_asm, "=r", true);
79 CallInst* result = irb_.CreateCall(func);
80 result->setOnlyReadsMemory();
81 irb_.SetTBAA(result, s_ty);
82 return result;
83}
TDYa127d668a062012-04-13 12:36:57 -070084
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070085void RuntimeSupportBuilderARM::EmitStoreToThreadOffset(int64_t offset, Value* value,
TDYa127de479be2012-05-31 08:03:26 -070086 TBAASpecialType s_ty) {
87 FunctionType* func_ty = FunctionType::get(/*Result=*/Type::getVoidTy(context_),
88 /*Params=*/value->getType(),
89 /*isVarArg=*/false);
TDYa127823433d2012-09-26 16:03:51 -070090 std::string inline_asm(StringPrintf("str%c $0, [r9, #%d]",
91 LDRSTRSuffixByType(irb_, value->getType()),
92 static_cast<int>(offset)));
TDYa127de479be2012-05-31 08:03:26 -070093 InlineAsm* func = InlineAsm::get(func_ty, inline_asm, "r", true);
94 CallInst* call_inst = irb_.CreateCall(func, value);
95 irb_.SetTBAA(call_inst, s_ty);
96}
TDYa127853cd092012-04-21 22:15:31 -070097
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070098Value* RuntimeSupportBuilderARM::EmitSetCurrentThread(Value* thread) {
TDYa127c1478262012-06-20 20:22:27 -070099 // Separate to two InlineAsm: The first one produces the return value, while the second,
100 // sets the current thread.
101 // LLVM can delete the first one if the caller in LLVM IR doesn't use the return value.
102 //
103 // Here we don't call EmitGetCurrentThread, because we mark it as DoesNotAccessMemory and
104 // ConstJObject. We denote side effect to "true" below instead, so LLVM won't
105 // reorder these instructions incorrectly.
106 Function* ori_func = GetRuntimeSupportFunction(runtime_support::GetCurrentThread);
107 InlineAsm* func = InlineAsm::get(ori_func->getFunctionType(), "mov $0, r9", "=r", true);
108 CallInst* old_thread_register = irb_.CreateCall(func);
109 old_thread_register->setOnlyReadsMemory();
110
111 FunctionType* func_ty = FunctionType::get(/*Result=*/Type::getVoidTy(context_),
112 /*Params=*/irb_.getJObjectTy(),
113 /*isVarArg=*/false);
114 func = InlineAsm::get(func_ty, "mov r9, $0", "r", true);
TDYa127de479be2012-05-31 08:03:26 -0700115 irb_.CreateCall(func, thread);
TDYa127c1478262012-06-20 20:22:27 -0700116 return old_thread_register;
TDYa127d668a062012-04-13 12:36:57 -0700117}
118
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700119} // namespace llvm
120} // namespace art