Home | History | Annotate | Download | only in llvm
      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"
     21 #include "utils_llvm.h"
     22 
     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>
     28 
     29 #include <vector>
     30 
     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;
     38 
     39 namespace {
     40 
     41 char LDRSTRSuffixByType(art::llvm::IRBuilder& irb, Type* type) {
     42   int width = type->isPointerTy() ?
     43               irb.getSizeOfPtrEquivInt()*8 :
     44               ::llvm::cast<IntegerType>(type)->getBitWidth();
     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 
     55 }  // namespace
     56 
     57 namespace art {
     58 namespace llvm {
     59 
     60 /* Thread */
     61 
     62 Value* RuntimeSupportBuilderARM::EmitGetCurrentThread() {
     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 }
     70 
     71 Value* RuntimeSupportBuilderARM::EmitLoadFromThreadOffset(int64_t offset, ::llvm::Type* type,
     72                                                           TBAASpecialType s_ty) {
     73   FunctionType* func_ty = FunctionType::get(/*Result=*/type,
     74                                             /*isVarArg=*/false);
     75   std::string inline_asm(StringPrintf("ldr%c $0, [r9, #%d]",
     76                                       LDRSTRSuffixByType(irb_, type),
     77                                       static_cast<int>(offset)));
     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 }
     84 
     85 void RuntimeSupportBuilderARM::EmitStoreToThreadOffset(int64_t offset, Value* value,
     86                                                        TBAASpecialType s_ty) {
     87   FunctionType* func_ty = FunctionType::get(/*Result=*/Type::getVoidTy(context_),
     88                                             /*Params=*/value->getType(),
     89                                             /*isVarArg=*/false);
     90   std::string inline_asm(StringPrintf("str%c $0, [r9, #%d]",
     91                                       LDRSTRSuffixByType(irb_, value->getType()),
     92                                       static_cast<int>(offset)));
     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 }
     97 
     98 Value* RuntimeSupportBuilderARM::EmitSetCurrentThread(Value* thread) {
     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);
    115   irb_.CreateCall(func, thread);
    116   return old_thread_register;
    117 }
    118 
    119 }  // namespace llvm
    120 }  // namespace art
    121