TDYa127 | d668a06 | 2012-04-13 12:36:57 -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_arm.h" |
| 18 | |
| 19 | #include "ir_builder.h" |
| 20 | #include "thread.h" |
TDYa127 | 853cd09 | 2012-04-21 22:15:31 -0700 | [diff] [blame] | 21 | #include "utils_llvm.h" |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 22 | |
Brian Carlstrom | 37d4879 | 2013-03-22 14:14:45 -0700 | [diff] [blame] | 23 | #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> |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 28 | |
| 29 | #include <vector> |
| 30 | |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 31 | using ::llvm::CallInst; |
| 32 | using ::llvm::Function; |
| 33 | using ::llvm::FunctionType; |
| 34 | using ::llvm::InlineAsm; |
| 35 | using ::llvm::IntegerType; |
| 36 | using ::llvm::Type; |
| 37 | using ::llvm::Value; |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 38 | |
TDYa127 | 823433d | 2012-09-26 16:03:51 -0700 | [diff] [blame] | 39 | namespace { |
| 40 | |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 41 | char LDRSTRSuffixByType(art::llvm::IRBuilder& irb, Type* type) { |
TDYa127 | 823433d | 2012-09-26 16:03:51 -0700 | [diff] [blame] | 42 | int width = type->isPointerTy() ? |
| 43 | irb.getSizeOfPtrEquivInt()*8 : |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 44 | ::llvm::cast<IntegerType>(type)->getBitWidth(); |
TDYa127 | 823433d | 2012-09-26 16:03:51 -0700 | [diff] [blame] | 45 | 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 Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 55 | } // namespace |
TDYa127 | 823433d | 2012-09-26 16:03:51 -0700 | [diff] [blame] | 56 | |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 57 | namespace art { |
Ian Rogers | 4c1c283 | 2013-03-04 18:30:13 -0800 | [diff] [blame] | 58 | namespace llvm { |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 59 | |
TDYa127 | b08ed12 | 2012-06-05 23:51:19 -0700 | [diff] [blame] | 60 | /* Thread */ |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 61 | |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 62 | Value* RuntimeSupportBuilderARM::EmitGetCurrentThread() { |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 63 | 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 | } |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 70 | |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 71 | Value* RuntimeSupportBuilderARM::EmitLoadFromThreadOffset(int64_t offset, ::llvm::Type* type, |
| 72 | TBAASpecialType s_ty) { |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 73 | FunctionType* func_ty = FunctionType::get(/*Result=*/type, |
| 74 | /*isVarArg=*/false); |
TDYa127 | 823433d | 2012-09-26 16:03:51 -0700 | [diff] [blame] | 75 | std::string inline_asm(StringPrintf("ldr%c $0, [r9, #%d]", |
| 76 | LDRSTRSuffixByType(irb_, type), |
| 77 | static_cast<int>(offset))); |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 78 | 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 | } |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 84 | |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 85 | void RuntimeSupportBuilderARM::EmitStoreToThreadOffset(int64_t offset, Value* value, |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 86 | TBAASpecialType s_ty) { |
| 87 | FunctionType* func_ty = FunctionType::get(/*Result=*/Type::getVoidTy(context_), |
| 88 | /*Params=*/value->getType(), |
| 89 | /*isVarArg=*/false); |
TDYa127 | 823433d | 2012-09-26 16:03:51 -0700 | [diff] [blame] | 90 | std::string inline_asm(StringPrintf("str%c $0, [r9, #%d]", |
| 91 | LDRSTRSuffixByType(irb_, value->getType()), |
| 92 | static_cast<int>(offset))); |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 93 | 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 | } |
TDYa127 | 853cd09 | 2012-04-21 22:15:31 -0700 | [diff] [blame] | 97 | |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 98 | Value* RuntimeSupportBuilderARM::EmitSetCurrentThread(Value* thread) { |
TDYa127 | c147826 | 2012-06-20 20:22:27 -0700 | [diff] [blame] | 99 | // 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); |
TDYa127 | de479be | 2012-05-31 08:03:26 -0700 | [diff] [blame] | 115 | irb_.CreateCall(func, thread); |
TDYa127 | c147826 | 2012-06-20 20:22:27 -0700 | [diff] [blame] | 116 | return old_thread_register; |
TDYa127 | d668a06 | 2012-04-13 12:36:57 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 119 | } // namespace llvm |
| 120 | } // namespace art |