1696bd635SAlexander Shaposhnikov //===-- RenderScriptx86ABIFixups.cpp ----------------------------*- C++ -*-===// 219459580SLuke Drummond // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 619459580SLuke Drummond // 719459580SLuke Drummond //===----------------------------------------------------------------------===// 819459580SLuke Drummond 919459580SLuke Drummond #include <set> 1019459580SLuke Drummond 1119459580SLuke Drummond #include "llvm/ADT/StringRef.h" 1219459580SLuke Drummond #include "llvm/IR/BasicBlock.h" 1319459580SLuke Drummond #include "llvm/IR/CallSite.h" 1419459580SLuke Drummond #include "llvm/IR/Constants.h" 1519459580SLuke Drummond #include "llvm/IR/Function.h" 1619459580SLuke Drummond #include "llvm/IR/Instruction.h" 1719459580SLuke Drummond #include "llvm/IR/Instructions.h" 1819459580SLuke Drummond #include "llvm/IR/Module.h" 1919459580SLuke Drummond #include "llvm/IRReader/IRReader.h" 2019459580SLuke Drummond #include "llvm/Pass.h" 2119459580SLuke Drummond 2219459580SLuke Drummond #include "lldb/Target/Process.h" 236f9e6901SZachary Turner #include "lldb/Utility/Log.h" 2419459580SLuke Drummond 2519459580SLuke Drummond using namespace lldb_private; 26b9c1b51eSKate Stone namespace { 2719459580SLuke Drummond 28b9c1b51eSKate Stone bool isRSAPICall(llvm::Module &module, llvm::CallInst *call_inst) { 2919459580SLuke Drummond // TODO get the list of renderscript modules from lldb and check if 3019459580SLuke Drummond // this llvm::Module calls into any of them. 3119459580SLuke Drummond (void)module; 3219459580SLuke Drummond const auto func_name = call_inst->getCalledFunction()->getName(); 3319459580SLuke Drummond if (func_name.startswith("llvm") || func_name.startswith("lldb")) 3419459580SLuke Drummond return false; 3519459580SLuke Drummond 3619459580SLuke Drummond if (call_inst->getCalledFunction()->isIntrinsic()) 3719459580SLuke Drummond return false; 3819459580SLuke Drummond 3919459580SLuke Drummond return true; 4019459580SLuke Drummond } 4119459580SLuke Drummond 42b9c1b51eSKate Stone bool isRSLargeReturnCall(llvm::Module &module, llvm::CallInst *call_inst) { 43b9c1b51eSKate Stone // i686 and x86_64 returns for large vectors in the RenderScript API are not 4405097246SAdrian Prantl // handled as normal register pairs, but as a hidden sret type. This is not 4505097246SAdrian Prantl // reflected in the debug info or mangled symbol name, and the android ABI 4605097246SAdrian Prantl // for x86 and x86_64, (as well as the emulators) specifies there is no AVX, 4705097246SAdrian Prantl // so bcc generates an sret function because we cannot natively return 48b9c1b51eSKate Stone // 256 bit vectors. 49b9c1b51eSKate Stone // This function simply checks whether a function has a > 128bit return type. 5005097246SAdrian Prantl // It is perhaps an unreliable heuristic, and relies on bcc not generating 5105097246SAdrian Prantl // AVX code, so if the android ABI one day provides for AVX, this function 5205097246SAdrian Prantl // may go out of fashion. 5319459580SLuke Drummond (void)module; 5419459580SLuke Drummond if (!call_inst || !call_inst->getCalledFunction()) 5519459580SLuke Drummond return false; 5619459580SLuke Drummond 57b9c1b51eSKate Stone return call_inst->getCalledFunction() 58b9c1b51eSKate Stone ->getReturnType() 59b9c1b51eSKate Stone ->getPrimitiveSizeInBits() > 128; 6019459580SLuke Drummond } 6119459580SLuke Drummond 62b9c1b51eSKate Stone bool isRSAllocationPtrTy(const llvm::Type *type) { 6319459580SLuke Drummond if (!type->isPointerTy()) 6419459580SLuke Drummond return false; 6519459580SLuke Drummond auto ptr_type = type->getPointerElementType(); 6619459580SLuke Drummond 67b9c1b51eSKate Stone return ptr_type->isStructTy() && 68b9c1b51eSKate Stone ptr_type->getStructName().startswith("struct.rs_allocation"); 6919459580SLuke Drummond } 7019459580SLuke Drummond 71b9c1b51eSKate Stone bool isRSAllocationTyCallSite(llvm::Module &module, llvm::CallInst *call_inst) { 7219459580SLuke Drummond (void)module; 7319459580SLuke Drummond if (!call_inst->hasByValArgument()) 7419459580SLuke Drummond return false; 7519459580SLuke Drummond for (const auto ¶m : call_inst->operand_values()) 7619459580SLuke Drummond if (isRSAllocationPtrTy(param->getType())) 7719459580SLuke Drummond return true; 7819459580SLuke Drummond return false; 7919459580SLuke Drummond } 8019459580SLuke Drummond 81b9c1b51eSKate Stone llvm::FunctionType *cloneToStructRetFnTy(llvm::CallInst *call_inst) { 82b9c1b51eSKate Stone // on x86 StructReturn functions return a pointer to the return value, rather 8305097246SAdrian Prantl // than the return value itself 8405097246SAdrian Prantl // [ref](http://www.agner.org/optimize/calling_conventions.pdf section 6). We 8505097246SAdrian Prantl // create a return type by getting the pointer type of the old return type, 8605097246SAdrian Prantl // and inserting a new initial argument of pointer type of the original 8705097246SAdrian Prantl // return type. 88b9c1b51eSKate Stone Log *log( 89b9c1b51eSKate Stone GetLogIfAnyCategoriesSet(LIBLLDB_LOG_LANGUAGE | LIBLLDB_LOG_EXPRESSIONS)); 9019459580SLuke Drummond 9119459580SLuke Drummond assert(call_inst && "no CallInst"); 9219459580SLuke Drummond llvm::Function *orig = call_inst->getCalledFunction(); 9319459580SLuke Drummond assert(orig && "CallInst has no called function"); 9419459580SLuke Drummond llvm::FunctionType *orig_type = orig->getFunctionType(); 9519459580SLuke Drummond auto name = orig->getName(); 9619459580SLuke Drummond if (log) 97b9c1b51eSKate Stone log->Printf("%s - cloning to StructRet function for '%s'", __FUNCTION__, 98b9c1b51eSKate Stone name.str().c_str()); 9919459580SLuke Drummond 10019459580SLuke Drummond unsigned num_params = orig_type->getNumParams(); 1019d83de42SLuke Drummond std::vector<llvm::Type *> new_params{num_params + 1, nullptr}; 102b9c1b51eSKate Stone std::vector<llvm::Type *> params{orig_type->param_begin(), 103b9c1b51eSKate Stone orig_type->param_end()}; 10419459580SLuke Drummond 105b9c1b51eSKate Stone // This may not work if the function is somehow declared void as llvm is 10605097246SAdrian Prantl // strongly typed and represents void* with i8* 107b9c1b51eSKate Stone assert(!orig_type->getReturnType()->isVoidTy() && 108b9c1b51eSKate Stone "Cannot add StructRet attribute to void function"); 109b9c1b51eSKate Stone llvm::PointerType *return_type_ptr_type = 110b9c1b51eSKate Stone llvm::PointerType::getUnqual(orig->getReturnType()); 111b9c1b51eSKate Stone assert(return_type_ptr_type && 112b9c1b51eSKate Stone "failed to get function return type PointerType"); 11319459580SLuke Drummond if (!return_type_ptr_type) 11419459580SLuke Drummond return nullptr; 11519459580SLuke Drummond 11619459580SLuke Drummond if (log) 117b9c1b51eSKate Stone log->Printf("%s - return type pointer type for StructRet clone @ '0x%p':\n", 118b9c1b51eSKate Stone __FUNCTION__, (void *)return_type_ptr_type); 1194ebdee0aSBruce Mitchener // put the sret pointer argument in place at the beginning of the 12005097246SAdrian Prantl // argument list. 12119459580SLuke Drummond params.emplace(params.begin(), return_type_ptr_type); 12219459580SLuke Drummond assert(params.size() == num_params + 1); 123b9c1b51eSKate Stone return llvm::FunctionType::get(return_type_ptr_type, params, 124b9c1b51eSKate Stone orig->isVarArg()); 12519459580SLuke Drummond } 12619459580SLuke Drummond 127b9c1b51eSKate Stone bool findRSCallSites(llvm::Module &module, 128b9c1b51eSKate Stone std::set<llvm::CallInst *> &rs_callsites, 129b9c1b51eSKate Stone bool (*predicate)(llvm::Module &, llvm::CallInst *)) { 13019459580SLuke Drummond bool found = false; 13119459580SLuke Drummond 13219459580SLuke Drummond for (auto &func : module.getFunctionList()) 13319459580SLuke Drummond for (auto &block : func.getBasicBlockList()) 134b9c1b51eSKate Stone for (auto &inst : block) { 135b9c1b51eSKate Stone llvm::CallInst *call_inst = 136b9c1b51eSKate Stone llvm::dyn_cast_or_null<llvm::CallInst>(&inst); 13719459580SLuke Drummond if (!call_inst || !call_inst->getCalledFunction()) 13819459580SLuke Drummond // This is not the call-site you are looking for... 13919459580SLuke Drummond continue; 140b9c1b51eSKate Stone if (isRSAPICall(module, call_inst) && predicate(module, call_inst)) { 14119459580SLuke Drummond rs_callsites.insert(call_inst); 14219459580SLuke Drummond found = true; 14319459580SLuke Drummond } 14419459580SLuke Drummond } 14519459580SLuke Drummond return found; 14619459580SLuke Drummond } 14719459580SLuke Drummond 148b9c1b51eSKate Stone bool fixupX86StructRetCalls(llvm::Module &module) { 14919459580SLuke Drummond bool changed = false; 15005097246SAdrian Prantl // changing a basic block while iterating over it seems to have some 15105097246SAdrian Prantl // undefined behaviour going on so we find all RS callsites first, then fix 15205097246SAdrian Prantl // them up after consuming the iterator. 15319459580SLuke Drummond std::set<llvm::CallInst *> rs_callsites; 15419459580SLuke Drummond if (!findRSCallSites(module, rs_callsites, isRSLargeReturnCall)) 15519459580SLuke Drummond return false; 15619459580SLuke Drummond 157b9c1b51eSKate Stone for (auto call_inst : rs_callsites) { 15819459580SLuke Drummond llvm::FunctionType *new_func_type = cloneToStructRetFnTy(call_inst); 159b9c1b51eSKate Stone assert(new_func_type && 160b9c1b51eSKate Stone "failed to clone functionType for Renderscript ABI fixup"); 16119459580SLuke Drummond 16219459580SLuke Drummond llvm::CallSite call_site(call_inst); 16319459580SLuke Drummond llvm::Function *func = call_inst->getCalledFunction(); 16419459580SLuke Drummond assert(func && "cannot resolve function in RenderScriptRuntime"); 16519459580SLuke Drummond // Copy the original call arguments 166b9c1b51eSKate Stone std::vector<llvm::Value *> new_call_args(call_site.arg_begin(), 167b9c1b51eSKate Stone call_site.arg_end()); 16819459580SLuke Drummond 16919459580SLuke Drummond // Allocate enough space to store the return value of the original function 170b9c1b51eSKate Stone // we pass a pointer to this allocation as the StructRet param, and then 17105097246SAdrian Prantl // copy its value into the lldb return value 17275696ffaSMatt Arsenault const llvm::DataLayout &DL = module.getDataLayout(); 173b9c1b51eSKate Stone llvm::AllocaInst *return_value_alloc = new llvm::AllocaInst( 17475696ffaSMatt Arsenault func->getReturnType(), DL.getAllocaAddrSpace(), "var_vector_return_alloc", 17575696ffaSMatt Arsenault call_inst); 17619459580SLuke Drummond // use the new allocation as the new first argument 177b9c1b51eSKate Stone new_call_args.emplace(new_call_args.begin(), 178b9c1b51eSKate Stone llvm::cast<llvm::Value>(return_value_alloc)); 179b9c1b51eSKate Stone llvm::PointerType *new_func_ptr_type = 180b9c1b51eSKate Stone llvm::PointerType::get(new_func_type, 0); 18119459580SLuke Drummond // Create the type cast from the old function type to the new one 182b9c1b51eSKate Stone llvm::Constant *new_func_cast = llvm::ConstantExpr::getCast( 183b9c1b51eSKate Stone llvm::Instruction::BitCast, func, new_func_ptr_type); 18419459580SLuke Drummond // create an allocation for a new function pointer 185b9c1b51eSKate Stone llvm::AllocaInst *new_func_ptr = 18675696ffaSMatt Arsenault new llvm::AllocaInst(new_func_ptr_type, DL.getAllocaAddrSpace(), 18775696ffaSMatt Arsenault "new_func_ptr", call_inst); 18819459580SLuke Drummond // store the new_func_cast to the newly allocated space 189a322f36cSDavid Blaikie (new llvm::StoreInst(new_func_cast, new_func_ptr, call_inst)) 190a322f36cSDavid Blaikie ->setName("new_func_ptr_load_cast"); 19119459580SLuke Drummond // load the new function address ready for a jump 192b9c1b51eSKate Stone llvm::LoadInst *new_func_addr_load = 193b9c1b51eSKate Stone new llvm::LoadInst(new_func_ptr, "load_func_pointer", call_inst); 19419459580SLuke Drummond // and create a callinstruction from it 195*ae2f9512SJames Y Knight llvm::CallInst *new_call_inst = 196*ae2f9512SJames Y Knight llvm::CallInst::Create(new_func_type, new_func_addr_load, new_call_args, 197*ae2f9512SJames Y Knight "new_func_call", call_inst); 19819459580SLuke Drummond new_call_inst->setCallingConv(call_inst->getCallingConv()); 19919459580SLuke Drummond new_call_inst->setTailCall(call_inst->isTailCall()); 200b9c1b51eSKate Stone llvm::LoadInst *lldb_save_result_address = 201b9c1b51eSKate Stone new llvm::LoadInst(return_value_alloc, "save_return_val", call_inst); 20219459580SLuke Drummond 20319459580SLuke Drummond // Now remove the old broken call 20419459580SLuke Drummond call_inst->replaceAllUsesWith(lldb_save_result_address); 20519459580SLuke Drummond call_inst->eraseFromParent(); 20619459580SLuke Drummond changed = true; 20719459580SLuke Drummond } 20819459580SLuke Drummond return changed; 20919459580SLuke Drummond } 21019459580SLuke Drummond 211b9c1b51eSKate Stone bool fixupRSAllocationStructByValCalls(llvm::Module &module) { 212b9c1b51eSKate Stone // On x86_64, calls to functions in the RS runtime that take an 21305097246SAdrian Prantl // `rs_allocation` type argument are actually handled as by-ref params by 21405097246SAdrian Prantl // bcc, but appear to be passed by value by lldb (the callsite all use 21505097246SAdrian Prantl // `struct byval`). On x86_64 Linux, struct arguments are transferred in 21605097246SAdrian Prantl // registers if the struct size is no bigger than 128bits 21705097246SAdrian Prantl // [ref](http://www.agner.org/optimize/calling_conventions.pdf) section 7.1 21805097246SAdrian Prantl // "Passing and returning objects" otherwise passed on the stack. an object 21905097246SAdrian Prantl // of type `rs_allocation` is actually 256bits, so should be passed on the 22005097246SAdrian Prantl // stack. However, code generated by bcc actually treats formal params of 22105097246SAdrian Prantl // type `rs_allocation` as `rs_allocation *` so we need to convert the 222b9c1b51eSKate Stone // calling convention to pass by reference, and remove any hint of byval from 223b9c1b51eSKate Stone // formal parameters. 22419459580SLuke Drummond bool changed = false; 22519459580SLuke Drummond std::set<llvm::CallInst *> rs_callsites; 22619459580SLuke Drummond if (!findRSCallSites(module, rs_callsites, isRSAllocationTyCallSite)) 22719459580SLuke Drummond return false; 22819459580SLuke Drummond 22919459580SLuke Drummond std::set<llvm::Function *> rs_functions; 23019459580SLuke Drummond 23119459580SLuke Drummond // for all call instructions 232b9c1b51eSKate Stone for (auto call_inst : rs_callsites) { 233b9c1b51eSKate Stone // add the called function to a set so that we can strip its byval 234b9c1b51eSKate Stone // attributes in another pass 23519459580SLuke Drummond rs_functions.insert(call_inst->getCalledFunction()); 23619459580SLuke Drummond 23719459580SLuke Drummond // get the function attributes 238a3e3715cSReid Kleckner llvm::AttributeList call_attribs = call_inst->getAttributes(); 23919459580SLuke Drummond 24019459580SLuke Drummond // iterate over the argument attributes 24102295000SStephane Sezer for (unsigned I = call_attribs.index_begin(); I != call_attribs.index_end(); 24202295000SStephane Sezer I++) { 24319459580SLuke Drummond // if this argument is passed by val 244e3bb52bbSStephane Sezer if (call_attribs.hasAttribute(I, llvm::Attribute::ByVal)) { 24519459580SLuke Drummond // strip away the byval attribute 246e3bb52bbSStephane Sezer call_inst->removeAttribute(I, llvm::Attribute::ByVal); 24719459580SLuke Drummond changed = true; 24819459580SLuke Drummond } 24919459580SLuke Drummond } 25019459580SLuke Drummond } 25119459580SLuke Drummond 25219459580SLuke Drummond // for all called function decls 253b9c1b51eSKate Stone for (auto func : rs_functions) { 25419459580SLuke Drummond // inspect all of the arguments in the call 25520670ba5SReid Kleckner for (auto &arg : func->args()) { 256b9c1b51eSKate Stone if (arg.hasByValAttr()) { 257f76a8ac5SReid Kleckner arg.removeAttr(llvm::Attribute::ByVal); 25819459580SLuke Drummond changed = true; 25919459580SLuke Drummond } 26019459580SLuke Drummond } 26119459580SLuke Drummond } 26219459580SLuke Drummond return changed; 26319459580SLuke Drummond } 26419459580SLuke Drummond } // end anonymous namespace 26519459580SLuke Drummond 266b9c1b51eSKate Stone namespace lldb_private { 267b9c1b51eSKate Stone namespace lldb_renderscript { 26819459580SLuke Drummond 269b9c1b51eSKate Stone bool fixupX86FunctionCalls(llvm::Module &module) { 27019459580SLuke Drummond return fixupX86StructRetCalls(module); 27119459580SLuke Drummond } 27219459580SLuke Drummond 273b9c1b51eSKate Stone bool fixupX86_64FunctionCalls(llvm::Module &module) { 27419459580SLuke Drummond bool changed = false; 27519459580SLuke Drummond changed |= fixupX86StructRetCalls(module); 27619459580SLuke Drummond changed |= fixupRSAllocationStructByValCalls(module); 27719459580SLuke Drummond return changed; 27819459580SLuke Drummond } 27919459580SLuke Drummond 28019459580SLuke Drummond } // end namespace lldb_renderscript 28119459580SLuke Drummond } // end namespace lldb_private 282