1435933ddSDimitry Andric //===-- RenderScriptx86ABIFixups.cpp ----------------------------*- C++ -*-===//
2435933ddSDimitry Andric //
3435933ddSDimitry Andric // The LLVM Compiler Infrastructure
4435933ddSDimitry Andric //
5435933ddSDimitry Andric // This file is distributed under the University of Illinois Open Source
6435933ddSDimitry Andric // License. See LICENSE.TXT for details.
7435933ddSDimitry Andric //
8435933ddSDimitry Andric //===----------------------------------------------------------------------===//
9435933ddSDimitry Andric
10435933ddSDimitry Andric #include <set>
11435933ddSDimitry Andric
12435933ddSDimitry Andric #include "llvm/ADT/StringRef.h"
13435933ddSDimitry Andric #include "llvm/IR/BasicBlock.h"
14435933ddSDimitry Andric #include "llvm/IR/CallSite.h"
15435933ddSDimitry Andric #include "llvm/IR/Constants.h"
16435933ddSDimitry Andric #include "llvm/IR/Function.h"
17435933ddSDimitry Andric #include "llvm/IR/Instruction.h"
18435933ddSDimitry Andric #include "llvm/IR/Instructions.h"
19435933ddSDimitry Andric #include "llvm/IR/Module.h"
20435933ddSDimitry Andric #include "llvm/IRReader/IRReader.h"
21435933ddSDimitry Andric #include "llvm/Pass.h"
22435933ddSDimitry Andric
23435933ddSDimitry Andric #include "lldb/Target/Process.h"
24f678e45dSDimitry Andric #include "lldb/Utility/Log.h"
25435933ddSDimitry Andric
26435933ddSDimitry Andric using namespace lldb_private;
27435933ddSDimitry Andric namespace {
28435933ddSDimitry Andric
isRSAPICall(llvm::Module & module,llvm::CallInst * call_inst)29435933ddSDimitry Andric bool isRSAPICall(llvm::Module &module, llvm::CallInst *call_inst) {
30435933ddSDimitry Andric // TODO get the list of renderscript modules from lldb and check if
31435933ddSDimitry Andric // this llvm::Module calls into any of them.
32435933ddSDimitry Andric (void)module;
33435933ddSDimitry Andric const auto func_name = call_inst->getCalledFunction()->getName();
34435933ddSDimitry Andric if (func_name.startswith("llvm") || func_name.startswith("lldb"))
35435933ddSDimitry Andric return false;
36435933ddSDimitry Andric
37435933ddSDimitry Andric if (call_inst->getCalledFunction()->isIntrinsic())
38435933ddSDimitry Andric return false;
39435933ddSDimitry Andric
40435933ddSDimitry Andric return true;
41435933ddSDimitry Andric }
42435933ddSDimitry Andric
isRSLargeReturnCall(llvm::Module & module,llvm::CallInst * call_inst)43435933ddSDimitry Andric bool isRSLargeReturnCall(llvm::Module &module, llvm::CallInst *call_inst) {
44435933ddSDimitry Andric // i686 and x86_64 returns for large vectors in the RenderScript API are not
45*4ba319b5SDimitry Andric // handled as normal register pairs, but as a hidden sret type. This is not
46*4ba319b5SDimitry Andric // reflected in the debug info or mangled symbol name, and the android ABI
47*4ba319b5SDimitry Andric // for x86 and x86_64, (as well as the emulators) specifies there is no AVX,
48*4ba319b5SDimitry Andric // so bcc generates an sret function because we cannot natively return
49435933ddSDimitry Andric // 256 bit vectors.
50435933ddSDimitry Andric // This function simply checks whether a function has a > 128bit return type.
51*4ba319b5SDimitry Andric // It is perhaps an unreliable heuristic, and relies on bcc not generating
52*4ba319b5SDimitry Andric // AVX code, so if the android ABI one day provides for AVX, this function
53*4ba319b5SDimitry Andric // may go out of fashion.
54435933ddSDimitry Andric (void)module;
55435933ddSDimitry Andric if (!call_inst || !call_inst->getCalledFunction())
56435933ddSDimitry Andric return false;
57435933ddSDimitry Andric
58435933ddSDimitry Andric return call_inst->getCalledFunction()
59435933ddSDimitry Andric ->getReturnType()
60435933ddSDimitry Andric ->getPrimitiveSizeInBits() > 128;
61435933ddSDimitry Andric }
62435933ddSDimitry Andric
isRSAllocationPtrTy(const llvm::Type * type)63435933ddSDimitry Andric bool isRSAllocationPtrTy(const llvm::Type *type) {
64435933ddSDimitry Andric if (!type->isPointerTy())
65435933ddSDimitry Andric return false;
66435933ddSDimitry Andric auto ptr_type = type->getPointerElementType();
67435933ddSDimitry Andric
68435933ddSDimitry Andric return ptr_type->isStructTy() &&
69435933ddSDimitry Andric ptr_type->getStructName().startswith("struct.rs_allocation");
70435933ddSDimitry Andric }
71435933ddSDimitry Andric
isRSAllocationTyCallSite(llvm::Module & module,llvm::CallInst * call_inst)72435933ddSDimitry Andric bool isRSAllocationTyCallSite(llvm::Module &module, llvm::CallInst *call_inst) {
73435933ddSDimitry Andric (void)module;
74435933ddSDimitry Andric if (!call_inst->hasByValArgument())
75435933ddSDimitry Andric return false;
76435933ddSDimitry Andric for (const auto ¶m : call_inst->operand_values())
77435933ddSDimitry Andric if (isRSAllocationPtrTy(param->getType()))
78435933ddSDimitry Andric return true;
79435933ddSDimitry Andric return false;
80435933ddSDimitry Andric }
81435933ddSDimitry Andric
cloneToStructRetFnTy(llvm::CallInst * call_inst)82435933ddSDimitry Andric llvm::FunctionType *cloneToStructRetFnTy(llvm::CallInst *call_inst) {
83435933ddSDimitry Andric // on x86 StructReturn functions return a pointer to the return value, rather
84*4ba319b5SDimitry Andric // than the return value itself
85*4ba319b5SDimitry Andric // [ref](http://www.agner.org/optimize/calling_conventions.pdf section 6). We
86*4ba319b5SDimitry Andric // create a return type by getting the pointer type of the old return type,
87*4ba319b5SDimitry Andric // and inserting a new initial argument of pointer type of the original
88*4ba319b5SDimitry Andric // return type.
89435933ddSDimitry Andric Log *log(
90435933ddSDimitry Andric GetLogIfAnyCategoriesSet(LIBLLDB_LOG_LANGUAGE | LIBLLDB_LOG_EXPRESSIONS));
91435933ddSDimitry Andric
92435933ddSDimitry Andric assert(call_inst && "no CallInst");
93435933ddSDimitry Andric llvm::Function *orig = call_inst->getCalledFunction();
94435933ddSDimitry Andric assert(orig && "CallInst has no called function");
95435933ddSDimitry Andric llvm::FunctionType *orig_type = orig->getFunctionType();
96435933ddSDimitry Andric auto name = orig->getName();
97435933ddSDimitry Andric if (log)
98435933ddSDimitry Andric log->Printf("%s - cloning to StructRet function for '%s'", __FUNCTION__,
99435933ddSDimitry Andric name.str().c_str());
100435933ddSDimitry Andric
101435933ddSDimitry Andric unsigned num_params = orig_type->getNumParams();
102435933ddSDimitry Andric std::vector<llvm::Type *> new_params{num_params + 1, nullptr};
103435933ddSDimitry Andric std::vector<llvm::Type *> params{orig_type->param_begin(),
104435933ddSDimitry Andric orig_type->param_end()};
105435933ddSDimitry Andric
106435933ddSDimitry Andric // This may not work if the function is somehow declared void as llvm is
107*4ba319b5SDimitry Andric // strongly typed and represents void* with i8*
108435933ddSDimitry Andric assert(!orig_type->getReturnType()->isVoidTy() &&
109435933ddSDimitry Andric "Cannot add StructRet attribute to void function");
110435933ddSDimitry Andric llvm::PointerType *return_type_ptr_type =
111435933ddSDimitry Andric llvm::PointerType::getUnqual(orig->getReturnType());
112435933ddSDimitry Andric assert(return_type_ptr_type &&
113435933ddSDimitry Andric "failed to get function return type PointerType");
114435933ddSDimitry Andric if (!return_type_ptr_type)
115435933ddSDimitry Andric return nullptr;
116435933ddSDimitry Andric
117435933ddSDimitry Andric if (log)
118435933ddSDimitry Andric log->Printf("%s - return type pointer type for StructRet clone @ '0x%p':\n",
119435933ddSDimitry Andric __FUNCTION__, (void *)return_type_ptr_type);
120*4ba319b5SDimitry Andric // put the sret pointer argument in place at the beginning of the
121*4ba319b5SDimitry Andric // argument list.
122435933ddSDimitry Andric params.emplace(params.begin(), return_type_ptr_type);
123435933ddSDimitry Andric assert(params.size() == num_params + 1);
124435933ddSDimitry Andric return llvm::FunctionType::get(return_type_ptr_type, params,
125435933ddSDimitry Andric orig->isVarArg());
126435933ddSDimitry Andric }
127435933ddSDimitry Andric
findRSCallSites(llvm::Module & module,std::set<llvm::CallInst * > & rs_callsites,bool (* predicate)(llvm::Module &,llvm::CallInst *))128435933ddSDimitry Andric bool findRSCallSites(llvm::Module &module,
129435933ddSDimitry Andric std::set<llvm::CallInst *> &rs_callsites,
130435933ddSDimitry Andric bool (*predicate)(llvm::Module &, llvm::CallInst *)) {
131435933ddSDimitry Andric bool found = false;
132435933ddSDimitry Andric
133435933ddSDimitry Andric for (auto &func : module.getFunctionList())
134435933ddSDimitry Andric for (auto &block : func.getBasicBlockList())
135435933ddSDimitry Andric for (auto &inst : block) {
136435933ddSDimitry Andric llvm::CallInst *call_inst =
137435933ddSDimitry Andric llvm::dyn_cast_or_null<llvm::CallInst>(&inst);
138435933ddSDimitry Andric if (!call_inst || !call_inst->getCalledFunction())
139435933ddSDimitry Andric // This is not the call-site you are looking for...
140435933ddSDimitry Andric continue;
141435933ddSDimitry Andric if (isRSAPICall(module, call_inst) && predicate(module, call_inst)) {
142435933ddSDimitry Andric rs_callsites.insert(call_inst);
143435933ddSDimitry Andric found = true;
144435933ddSDimitry Andric }
145435933ddSDimitry Andric }
146435933ddSDimitry Andric return found;
147435933ddSDimitry Andric }
148435933ddSDimitry Andric
fixupX86StructRetCalls(llvm::Module & module)149435933ddSDimitry Andric bool fixupX86StructRetCalls(llvm::Module &module) {
150435933ddSDimitry Andric bool changed = false;
151*4ba319b5SDimitry Andric // changing a basic block while iterating over it seems to have some
152*4ba319b5SDimitry Andric // undefined behaviour going on so we find all RS callsites first, then fix
153*4ba319b5SDimitry Andric // them up after consuming the iterator.
154435933ddSDimitry Andric std::set<llvm::CallInst *> rs_callsites;
155435933ddSDimitry Andric if (!findRSCallSites(module, rs_callsites, isRSLargeReturnCall))
156435933ddSDimitry Andric return false;
157435933ddSDimitry Andric
158435933ddSDimitry Andric for (auto call_inst : rs_callsites) {
159435933ddSDimitry Andric llvm::FunctionType *new_func_type = cloneToStructRetFnTy(call_inst);
160435933ddSDimitry Andric assert(new_func_type &&
161435933ddSDimitry Andric "failed to clone functionType for Renderscript ABI fixup");
162435933ddSDimitry Andric
163435933ddSDimitry Andric llvm::CallSite call_site(call_inst);
164435933ddSDimitry Andric llvm::Function *func = call_inst->getCalledFunction();
165435933ddSDimitry Andric assert(func && "cannot resolve function in RenderScriptRuntime");
166435933ddSDimitry Andric // Copy the original call arguments
167435933ddSDimitry Andric std::vector<llvm::Value *> new_call_args(call_site.arg_begin(),
168435933ddSDimitry Andric call_site.arg_end());
169435933ddSDimitry Andric
170435933ddSDimitry Andric // Allocate enough space to store the return value of the original function
171435933ddSDimitry Andric // we pass a pointer to this allocation as the StructRet param, and then
172*4ba319b5SDimitry Andric // copy its value into the lldb return value
173f678e45dSDimitry Andric const llvm::DataLayout &DL = module.getDataLayout();
174435933ddSDimitry Andric llvm::AllocaInst *return_value_alloc = new llvm::AllocaInst(
175f678e45dSDimitry Andric func->getReturnType(), DL.getAllocaAddrSpace(), "var_vector_return_alloc",
176f678e45dSDimitry Andric call_inst);
177435933ddSDimitry Andric // use the new allocation as the new first argument
178435933ddSDimitry Andric new_call_args.emplace(new_call_args.begin(),
179435933ddSDimitry Andric llvm::cast<llvm::Value>(return_value_alloc));
180435933ddSDimitry Andric llvm::PointerType *new_func_ptr_type =
181435933ddSDimitry Andric llvm::PointerType::get(new_func_type, 0);
182435933ddSDimitry Andric // Create the type cast from the old function type to the new one
183435933ddSDimitry Andric llvm::Constant *new_func_cast = llvm::ConstantExpr::getCast(
184435933ddSDimitry Andric llvm::Instruction::BitCast, func, new_func_ptr_type);
185435933ddSDimitry Andric // create an allocation for a new function pointer
186435933ddSDimitry Andric llvm::AllocaInst *new_func_ptr =
187f678e45dSDimitry Andric new llvm::AllocaInst(new_func_ptr_type, DL.getAllocaAddrSpace(),
188f678e45dSDimitry Andric "new_func_ptr", call_inst);
189435933ddSDimitry Andric // store the new_func_cast to the newly allocated space
19095ec533aSDimitry Andric (new llvm::StoreInst(new_func_cast, new_func_ptr, call_inst))
19195ec533aSDimitry Andric ->setName("new_func_ptr_load_cast");
192435933ddSDimitry Andric // load the new function address ready for a jump
193435933ddSDimitry Andric llvm::LoadInst *new_func_addr_load =
194435933ddSDimitry Andric new llvm::LoadInst(new_func_ptr, "load_func_pointer", call_inst);
195435933ddSDimitry Andric // and create a callinstruction from it
196435933ddSDimitry Andric llvm::CallInst *new_call_inst = llvm::CallInst::Create(
197435933ddSDimitry Andric new_func_addr_load, new_call_args, "new_func_call", call_inst);
198435933ddSDimitry Andric new_call_inst->setCallingConv(call_inst->getCallingConv());
199435933ddSDimitry Andric new_call_inst->setTailCall(call_inst->isTailCall());
200435933ddSDimitry Andric llvm::LoadInst *lldb_save_result_address =
201435933ddSDimitry Andric new llvm::LoadInst(return_value_alloc, "save_return_val", call_inst);
202435933ddSDimitry Andric
203435933ddSDimitry Andric // Now remove the old broken call
204435933ddSDimitry Andric call_inst->replaceAllUsesWith(lldb_save_result_address);
205435933ddSDimitry Andric call_inst->eraseFromParent();
206435933ddSDimitry Andric changed = true;
207435933ddSDimitry Andric }
208435933ddSDimitry Andric return changed;
209435933ddSDimitry Andric }
210435933ddSDimitry Andric
fixupRSAllocationStructByValCalls(llvm::Module & module)211435933ddSDimitry Andric bool fixupRSAllocationStructByValCalls(llvm::Module &module) {
212435933ddSDimitry Andric // On x86_64, calls to functions in the RS runtime that take an
213*4ba319b5SDimitry Andric // `rs_allocation` type argument are actually handled as by-ref params by
214*4ba319b5SDimitry Andric // bcc, but appear to be passed by value by lldb (the callsite all use
215*4ba319b5SDimitry Andric // `struct byval`). On x86_64 Linux, struct arguments are transferred in
216*4ba319b5SDimitry Andric // registers if the struct size is no bigger than 128bits
217*4ba319b5SDimitry Andric // [ref](http://www.agner.org/optimize/calling_conventions.pdf) section 7.1
218*4ba319b5SDimitry Andric // "Passing and returning objects" otherwise passed on the stack. an object
219*4ba319b5SDimitry Andric // of type `rs_allocation` is actually 256bits, so should be passed on the
220*4ba319b5SDimitry Andric // stack. However, code generated by bcc actually treats formal params of
221*4ba319b5SDimitry Andric // type `rs_allocation` as `rs_allocation *` so we need to convert the
222435933ddSDimitry Andric // calling convention to pass by reference, and remove any hint of byval from
223435933ddSDimitry Andric // formal parameters.
224435933ddSDimitry Andric bool changed = false;
225435933ddSDimitry Andric std::set<llvm::CallInst *> rs_callsites;
226435933ddSDimitry Andric if (!findRSCallSites(module, rs_callsites, isRSAllocationTyCallSite))
227435933ddSDimitry Andric return false;
228435933ddSDimitry Andric
229435933ddSDimitry Andric std::set<llvm::Function *> rs_functions;
230435933ddSDimitry Andric
231435933ddSDimitry Andric // for all call instructions
232435933ddSDimitry Andric for (auto call_inst : rs_callsites) {
233435933ddSDimitry Andric // add the called function to a set so that we can strip its byval
234435933ddSDimitry Andric // attributes in another pass
235435933ddSDimitry Andric rs_functions.insert(call_inst->getCalledFunction());
236435933ddSDimitry Andric
237435933ddSDimitry Andric // get the function attributes
238f678e45dSDimitry Andric llvm::AttributeList call_attribs = call_inst->getAttributes();
239435933ddSDimitry Andric
240435933ddSDimitry Andric // iterate over the argument attributes
241302affcbSDimitry Andric for (unsigned I = call_attribs.index_begin(); I != call_attribs.index_end();
242302affcbSDimitry Andric I++) {
243435933ddSDimitry Andric // if this argument is passed by val
244302affcbSDimitry Andric if (call_attribs.hasAttribute(I, llvm::Attribute::ByVal)) {
245435933ddSDimitry Andric // strip away the byval attribute
246302affcbSDimitry Andric call_inst->removeAttribute(I, llvm::Attribute::ByVal);
247435933ddSDimitry Andric changed = true;
248435933ddSDimitry Andric }
249435933ddSDimitry Andric }
250435933ddSDimitry Andric }
251435933ddSDimitry Andric
252435933ddSDimitry Andric // for all called function decls
253435933ddSDimitry Andric for (auto func : rs_functions) {
254435933ddSDimitry Andric // inspect all of the arguments in the call
255f678e45dSDimitry Andric for (auto &arg : func->args()) {
256435933ddSDimitry Andric if (arg.hasByValAttr()) {
257f678e45dSDimitry Andric arg.removeAttr(llvm::Attribute::ByVal);
258435933ddSDimitry Andric changed = true;
259435933ddSDimitry Andric }
260435933ddSDimitry Andric }
261435933ddSDimitry Andric }
262435933ddSDimitry Andric return changed;
263435933ddSDimitry Andric }
264435933ddSDimitry Andric } // end anonymous namespace
265435933ddSDimitry Andric
266435933ddSDimitry Andric namespace lldb_private {
267435933ddSDimitry Andric namespace lldb_renderscript {
268435933ddSDimitry Andric
fixupX86FunctionCalls(llvm::Module & module)269435933ddSDimitry Andric bool fixupX86FunctionCalls(llvm::Module &module) {
270435933ddSDimitry Andric return fixupX86StructRetCalls(module);
271435933ddSDimitry Andric }
272435933ddSDimitry Andric
fixupX86_64FunctionCalls(llvm::Module & module)273435933ddSDimitry Andric bool fixupX86_64FunctionCalls(llvm::Module &module) {
274435933ddSDimitry Andric bool changed = false;
275435933ddSDimitry Andric changed |= fixupX86StructRetCalls(module);
276435933ddSDimitry Andric changed |= fixupRSAllocationStructByValCalls(module);
277435933ddSDimitry Andric return changed;
278435933ddSDimitry Andric }
279435933ddSDimitry Andric
280435933ddSDimitry Andric } // end namespace lldb_renderscript
281435933ddSDimitry Andric } // end namespace lldb_private
282