1696bd635SAlexander Shaposhnikov //===-- RenderScriptx86ABIFixups.cpp ----------------------------*- C++ -*-===//
219459580SLuke Drummond //
319459580SLuke Drummond //                     The LLVM Compiler Infrastructure
419459580SLuke Drummond //
519459580SLuke Drummond // This file is distributed under the University of Illinois Open Source
619459580SLuke Drummond // License. See LICENSE.TXT for details.
719459580SLuke Drummond //
819459580SLuke Drummond //===----------------------------------------------------------------------===//
919459580SLuke Drummond 
1019459580SLuke Drummond // C Includes
1119459580SLuke Drummond // C++ Includes
1219459580SLuke Drummond #include <set>
1319459580SLuke Drummond 
1419459580SLuke Drummond // Other libraries and framework includes
1519459580SLuke Drummond #include "llvm/ADT/StringRef.h"
1619459580SLuke Drummond #include "llvm/IR/BasicBlock.h"
1719459580SLuke Drummond #include "llvm/IR/CallSite.h"
1819459580SLuke Drummond #include "llvm/IR/Constants.h"
1919459580SLuke Drummond #include "llvm/IR/Function.h"
2019459580SLuke Drummond #include "llvm/IR/Instruction.h"
2119459580SLuke Drummond #include "llvm/IR/Instructions.h"
2219459580SLuke Drummond #include "llvm/IR/Module.h"
2319459580SLuke Drummond #include "llvm/IRReader/IRReader.h"
2419459580SLuke Drummond #include "llvm/Pass.h"
2519459580SLuke Drummond 
2619459580SLuke Drummond // Project includes
2719459580SLuke Drummond #include "lldb/Target/Process.h"
286f9e6901SZachary Turner #include "lldb/Utility/Log.h"
2919459580SLuke Drummond 
3019459580SLuke Drummond using namespace lldb_private;
31b9c1b51eSKate Stone namespace {
3219459580SLuke Drummond 
33b9c1b51eSKate Stone bool isRSAPICall(llvm::Module &module, llvm::CallInst *call_inst) {
3419459580SLuke Drummond   // TODO get the list of renderscript modules from lldb and check if
3519459580SLuke Drummond   // this llvm::Module calls into any of them.
3619459580SLuke Drummond   (void)module;
3719459580SLuke Drummond   const auto func_name = call_inst->getCalledFunction()->getName();
3819459580SLuke Drummond   if (func_name.startswith("llvm") || func_name.startswith("lldb"))
3919459580SLuke Drummond     return false;
4019459580SLuke Drummond 
4119459580SLuke Drummond   if (call_inst->getCalledFunction()->isIntrinsic())
4219459580SLuke Drummond     return false;
4319459580SLuke Drummond 
4419459580SLuke Drummond   return true;
4519459580SLuke Drummond }
4619459580SLuke Drummond 
47b9c1b51eSKate Stone bool isRSLargeReturnCall(llvm::Module &module, llvm::CallInst *call_inst) {
48b9c1b51eSKate Stone   // i686 and x86_64 returns for large vectors in the RenderScript API are not
49b9c1b51eSKate Stone   // handled as normal
50b9c1b51eSKate Stone   // register pairs, but as a hidden sret type. This is not reflected in the
51b9c1b51eSKate Stone   // debug info or mangled
52b9c1b51eSKate Stone   // symbol name, and the android ABI for x86 and x86_64, (as well as the
53b9c1b51eSKate Stone   // emulators) specifies there is
54b9c1b51eSKate Stone   // no AVX, so bcc generates an sret function because we cannot natively return
55b9c1b51eSKate Stone   // 256 bit vectors.
56b9c1b51eSKate Stone   // This function simply checks whether a function has a > 128bit return type.
57b9c1b51eSKate Stone   // It is perhaps an
58b9c1b51eSKate Stone   // unreliable heuristic, and relies on bcc not generating AVX code, so if the
59b9c1b51eSKate Stone   // android ABI one day
6019459580SLuke Drummond   // provides for AVX, this function may go out of fashion.
6119459580SLuke Drummond   (void)module;
6219459580SLuke Drummond   if (!call_inst || !call_inst->getCalledFunction())
6319459580SLuke Drummond     return false;
6419459580SLuke Drummond 
65b9c1b51eSKate Stone   return call_inst->getCalledFunction()
66b9c1b51eSKate Stone              ->getReturnType()
67b9c1b51eSKate Stone              ->getPrimitiveSizeInBits() > 128;
6819459580SLuke Drummond }
6919459580SLuke Drummond 
70b9c1b51eSKate Stone bool isRSAllocationPtrTy(const llvm::Type *type) {
7119459580SLuke Drummond   if (!type->isPointerTy())
7219459580SLuke Drummond     return false;
7319459580SLuke Drummond   auto ptr_type = type->getPointerElementType();
7419459580SLuke Drummond 
75b9c1b51eSKate Stone   return ptr_type->isStructTy() &&
76b9c1b51eSKate Stone          ptr_type->getStructName().startswith("struct.rs_allocation");
7719459580SLuke Drummond }
7819459580SLuke Drummond 
79b9c1b51eSKate Stone bool isRSAllocationTyCallSite(llvm::Module &module, llvm::CallInst *call_inst) {
8019459580SLuke Drummond   (void)module;
8119459580SLuke Drummond   if (!call_inst->hasByValArgument())
8219459580SLuke Drummond     return false;
8319459580SLuke Drummond   for (const auto &param : call_inst->operand_values())
8419459580SLuke Drummond     if (isRSAllocationPtrTy(param->getType()))
8519459580SLuke Drummond       return true;
8619459580SLuke Drummond   return false;
8719459580SLuke Drummond }
8819459580SLuke Drummond 
89b9c1b51eSKate Stone llvm::FunctionType *cloneToStructRetFnTy(llvm::CallInst *call_inst) {
90b9c1b51eSKate Stone   // on x86 StructReturn functions return a pointer to the return value, rather
91b9c1b51eSKate Stone   // than the return
92b9c1b51eSKate Stone   // value itself [ref](http://www.agner.org/optimize/calling_conventions.pdf
93b9c1b51eSKate Stone   // section 6).
94b9c1b51eSKate Stone   // We create a return type by getting the pointer type of the old return type,
95b9c1b51eSKate Stone   // and inserting a new
9619459580SLuke Drummond   // initial argument of pointer type of the original return type.
97b9c1b51eSKate Stone   Log *log(
98b9c1b51eSKate Stone       GetLogIfAnyCategoriesSet(LIBLLDB_LOG_LANGUAGE | LIBLLDB_LOG_EXPRESSIONS));
9919459580SLuke Drummond 
10019459580SLuke Drummond   assert(call_inst && "no CallInst");
10119459580SLuke Drummond   llvm::Function *orig = call_inst->getCalledFunction();
10219459580SLuke Drummond   assert(orig && "CallInst has no called function");
10319459580SLuke Drummond   llvm::FunctionType *orig_type = orig->getFunctionType();
10419459580SLuke Drummond   auto name = orig->getName();
10519459580SLuke Drummond   if (log)
106b9c1b51eSKate Stone     log->Printf("%s - cloning to StructRet function for '%s'", __FUNCTION__,
107b9c1b51eSKate Stone                 name.str().c_str());
10819459580SLuke Drummond 
10919459580SLuke Drummond   unsigned num_params = orig_type->getNumParams();
1109d83de42SLuke Drummond   std::vector<llvm::Type *> new_params{num_params + 1, nullptr};
111b9c1b51eSKate Stone   std::vector<llvm::Type *> params{orig_type->param_begin(),
112b9c1b51eSKate Stone                                    orig_type->param_end()};
11319459580SLuke Drummond 
114b9c1b51eSKate Stone   // This may not work if the function is somehow declared void as llvm is
115b9c1b51eSKate Stone   // strongly typed
11619459580SLuke Drummond   // and represents void* with i8*
117b9c1b51eSKate Stone   assert(!orig_type->getReturnType()->isVoidTy() &&
118b9c1b51eSKate Stone          "Cannot add StructRet attribute to void function");
119b9c1b51eSKate Stone   llvm::PointerType *return_type_ptr_type =
120b9c1b51eSKate Stone       llvm::PointerType::getUnqual(orig->getReturnType());
121b9c1b51eSKate Stone   assert(return_type_ptr_type &&
122b9c1b51eSKate Stone          "failed to get function return type PointerType");
12319459580SLuke Drummond   if (!return_type_ptr_type)
12419459580SLuke Drummond     return nullptr;
12519459580SLuke Drummond 
12619459580SLuke Drummond   if (log)
127b9c1b51eSKate Stone     log->Printf("%s - return type pointer type for StructRet clone @ '0x%p':\n",
128b9c1b51eSKate Stone                 __FUNCTION__, (void *)return_type_ptr_type);
129b9c1b51eSKate Stone   // put the the sret pointer argument in place at the beginning of the argument
130b9c1b51eSKate Stone   // list.
13119459580SLuke Drummond   params.emplace(params.begin(), return_type_ptr_type);
13219459580SLuke Drummond   assert(params.size() == num_params + 1);
133b9c1b51eSKate Stone   return llvm::FunctionType::get(return_type_ptr_type, params,
134b9c1b51eSKate Stone                                  orig->isVarArg());
13519459580SLuke Drummond }
13619459580SLuke Drummond 
137b9c1b51eSKate Stone bool findRSCallSites(llvm::Module &module,
138b9c1b51eSKate Stone                      std::set<llvm::CallInst *> &rs_callsites,
139b9c1b51eSKate Stone                      bool (*predicate)(llvm::Module &, llvm::CallInst *)) {
14019459580SLuke Drummond   bool found = false;
14119459580SLuke Drummond 
14219459580SLuke Drummond   for (auto &func : module.getFunctionList())
14319459580SLuke Drummond     for (auto &block : func.getBasicBlockList())
144b9c1b51eSKate Stone       for (auto &inst : block) {
145b9c1b51eSKate Stone         llvm::CallInst *call_inst =
146b9c1b51eSKate Stone             llvm::dyn_cast_or_null<llvm::CallInst>(&inst);
14719459580SLuke Drummond         if (!call_inst || !call_inst->getCalledFunction())
14819459580SLuke Drummond           // This is not the call-site you are looking for...
14919459580SLuke Drummond           continue;
150b9c1b51eSKate Stone         if (isRSAPICall(module, call_inst) && predicate(module, call_inst)) {
15119459580SLuke Drummond           rs_callsites.insert(call_inst);
15219459580SLuke Drummond           found = true;
15319459580SLuke Drummond         }
15419459580SLuke Drummond       }
15519459580SLuke Drummond   return found;
15619459580SLuke Drummond }
15719459580SLuke Drummond 
158b9c1b51eSKate Stone bool fixupX86StructRetCalls(llvm::Module &module) {
15919459580SLuke Drummond   bool changed = false;
160b9c1b51eSKate Stone   // changing a basic block while iterating over it seems to have some undefined
161b9c1b51eSKate Stone   // behaviour
162b9c1b51eSKate Stone   // going on so we find all RS callsites first, then fix them up after
163b9c1b51eSKate Stone   // consuming
16419459580SLuke Drummond   // the iterator.
16519459580SLuke Drummond   std::set<llvm::CallInst *> rs_callsites;
16619459580SLuke Drummond   if (!findRSCallSites(module, rs_callsites, isRSLargeReturnCall))
16719459580SLuke Drummond     return false;
16819459580SLuke Drummond 
169b9c1b51eSKate Stone   for (auto call_inst : rs_callsites) {
17019459580SLuke Drummond     llvm::FunctionType *new_func_type = cloneToStructRetFnTy(call_inst);
171b9c1b51eSKate Stone     assert(new_func_type &&
172b9c1b51eSKate Stone            "failed to clone functionType for Renderscript ABI fixup");
17319459580SLuke Drummond 
17419459580SLuke Drummond     llvm::CallSite call_site(call_inst);
17519459580SLuke Drummond     llvm::Function *func = call_inst->getCalledFunction();
17619459580SLuke Drummond     assert(func && "cannot resolve function in RenderScriptRuntime");
17719459580SLuke Drummond     // Copy the original call arguments
178b9c1b51eSKate Stone     std::vector<llvm::Value *> new_call_args(call_site.arg_begin(),
179b9c1b51eSKate Stone                                              call_site.arg_end());
18019459580SLuke Drummond 
18119459580SLuke Drummond     // Allocate enough space to store the return value of the original function
182b9c1b51eSKate Stone     // we pass a pointer to this allocation as the StructRet param, and then
183b9c1b51eSKate Stone     // copy its
18419459580SLuke Drummond     // value into the lldb return value
185*75696ffaSMatt Arsenault     const llvm::DataLayout &DL = module.getDataLayout();
186b9c1b51eSKate Stone     llvm::AllocaInst *return_value_alloc = new llvm::AllocaInst(
187*75696ffaSMatt Arsenault       func->getReturnType(), DL.getAllocaAddrSpace(), "var_vector_return_alloc",
188*75696ffaSMatt Arsenault       call_inst);
18919459580SLuke Drummond     // use the new allocation as the new first argument
190b9c1b51eSKate Stone     new_call_args.emplace(new_call_args.begin(),
191b9c1b51eSKate Stone                           llvm::cast<llvm::Value>(return_value_alloc));
192b9c1b51eSKate Stone     llvm::PointerType *new_func_ptr_type =
193b9c1b51eSKate Stone         llvm::PointerType::get(new_func_type, 0);
19419459580SLuke Drummond     // Create the type cast from the old function type to the new one
195b9c1b51eSKate Stone     llvm::Constant *new_func_cast = llvm::ConstantExpr::getCast(
196b9c1b51eSKate Stone         llvm::Instruction::BitCast, func, new_func_ptr_type);
19719459580SLuke Drummond     // create an allocation for a new function pointer
198b9c1b51eSKate Stone     llvm::AllocaInst *new_func_ptr =
199*75696ffaSMatt Arsenault         new llvm::AllocaInst(new_func_ptr_type, DL.getAllocaAddrSpace(),
200*75696ffaSMatt Arsenault                              "new_func_ptr", call_inst);
20119459580SLuke Drummond     // store the new_func_cast to the newly allocated space
202a322f36cSDavid Blaikie     (new llvm::StoreInst(new_func_cast, new_func_ptr, call_inst))
203a322f36cSDavid Blaikie         ->setName("new_func_ptr_load_cast");
20419459580SLuke Drummond     // load the new function address ready for a jump
205b9c1b51eSKate Stone     llvm::LoadInst *new_func_addr_load =
206b9c1b51eSKate Stone         new llvm::LoadInst(new_func_ptr, "load_func_pointer", call_inst);
20719459580SLuke Drummond     // and create a callinstruction from it
208b9c1b51eSKate Stone     llvm::CallInst *new_call_inst = llvm::CallInst::Create(
209b9c1b51eSKate Stone         new_func_addr_load, new_call_args, "new_func_call", call_inst);
21019459580SLuke Drummond     new_call_inst->setCallingConv(call_inst->getCallingConv());
21119459580SLuke Drummond     new_call_inst->setTailCall(call_inst->isTailCall());
212b9c1b51eSKate Stone     llvm::LoadInst *lldb_save_result_address =
213b9c1b51eSKate Stone         new llvm::LoadInst(return_value_alloc, "save_return_val", call_inst);
21419459580SLuke Drummond 
21519459580SLuke Drummond     // Now remove the old broken call
21619459580SLuke Drummond     call_inst->replaceAllUsesWith(lldb_save_result_address);
21719459580SLuke Drummond     call_inst->eraseFromParent();
21819459580SLuke Drummond     changed = true;
21919459580SLuke Drummond   }
22019459580SLuke Drummond   return changed;
22119459580SLuke Drummond }
22219459580SLuke Drummond 
223b9c1b51eSKate Stone bool fixupRSAllocationStructByValCalls(llvm::Module &module) {
224b9c1b51eSKate Stone   // On x86_64, calls to functions in the RS runtime that take an
225b9c1b51eSKate Stone   // `rs_allocation` type argument
226b9c1b51eSKate Stone   // are actually handled as by-ref params by bcc, but appear to be passed by
227b9c1b51eSKate Stone   // value by lldb (the callsite all use
22819459580SLuke Drummond   // `struct byval`).
229b9c1b51eSKate Stone   // On x86_64 Linux, struct arguments are transferred in registers if the
230b9c1b51eSKate Stone   // struct size is no bigger than
231b9c1b51eSKate Stone   // 128bits [ref](http://www.agner.org/optimize/calling_conventions.pdf)
232b9c1b51eSKate Stone   // section 7.1 "Passing and returning objects"
23319459580SLuke Drummond   // otherwise passed on the stack.
234b9c1b51eSKate Stone   // an object of type `rs_allocation` is actually 256bits, so should be passed
235b9c1b51eSKate Stone   // on the stack. However, code generated
236b9c1b51eSKate Stone   // by bcc actually treats formal params of type `rs_allocation` as
237b9c1b51eSKate Stone   // `rs_allocation *` so we need to convert the
238b9c1b51eSKate Stone   // calling convention to pass by reference, and remove any hint of byval from
239b9c1b51eSKate Stone   // formal parameters.
24019459580SLuke Drummond   bool changed = false;
24119459580SLuke Drummond   std::set<llvm::CallInst *> rs_callsites;
24219459580SLuke Drummond   if (!findRSCallSites(module, rs_callsites, isRSAllocationTyCallSite))
24319459580SLuke Drummond     return false;
24419459580SLuke Drummond 
24519459580SLuke Drummond   std::set<llvm::Function *> rs_functions;
24619459580SLuke Drummond 
24719459580SLuke Drummond   // for all call instructions
248b9c1b51eSKate Stone   for (auto call_inst : rs_callsites) {
249b9c1b51eSKate Stone     // add the called function to a set so that we can strip its byval
250b9c1b51eSKate Stone     // attributes in another pass
25119459580SLuke Drummond     rs_functions.insert(call_inst->getCalledFunction());
25219459580SLuke Drummond 
25319459580SLuke Drummond     // get the function attributes
254a3e3715cSReid Kleckner     llvm::AttributeList call_attribs = call_inst->getAttributes();
25519459580SLuke Drummond 
25619459580SLuke Drummond     // iterate over the argument attributes
257b9c1b51eSKate Stone     for (size_t i = 1; i <= call_attribs.getNumSlots(); ++i) {
25819459580SLuke Drummond       // if this argument is passed by val
259b9c1b51eSKate Stone       if (call_attribs.hasAttribute(i, llvm::Attribute::ByVal)) {
26019459580SLuke Drummond         // strip away the byval attribute
26180dfec8cSDimitar Vlahovski         call_inst->removeAttribute(i, llvm::Attribute::ByVal);
26219459580SLuke Drummond         changed = true;
26319459580SLuke Drummond       }
26419459580SLuke Drummond     }
26519459580SLuke Drummond   }
26619459580SLuke Drummond 
26719459580SLuke Drummond   // for all called function decls
268b9c1b51eSKate Stone   for (auto func : rs_functions) {
26919459580SLuke Drummond     // inspect all of the arguments in the call
27020670ba5SReid Kleckner     for (auto &arg : func->args()) {
271b9c1b51eSKate Stone       if (arg.hasByValAttr()) {
272f76a8ac5SReid Kleckner         arg.removeAttr(llvm::Attribute::ByVal);
27319459580SLuke Drummond         changed = true;
27419459580SLuke Drummond       }
27519459580SLuke Drummond     }
27619459580SLuke Drummond   }
27719459580SLuke Drummond   return changed;
27819459580SLuke Drummond }
27919459580SLuke Drummond } // end anonymous namespace
28019459580SLuke Drummond 
281b9c1b51eSKate Stone namespace lldb_private {
282b9c1b51eSKate Stone namespace lldb_renderscript {
28319459580SLuke Drummond 
284b9c1b51eSKate Stone bool fixupX86FunctionCalls(llvm::Module &module) {
28519459580SLuke Drummond   return fixupX86StructRetCalls(module);
28619459580SLuke Drummond }
28719459580SLuke Drummond 
288b9c1b51eSKate Stone bool fixupX86_64FunctionCalls(llvm::Module &module) {
28919459580SLuke Drummond   bool changed = false;
29019459580SLuke Drummond   changed |= fixupX86StructRetCalls(module);
29119459580SLuke Drummond   changed |= fixupRSAllocationStructByValCalls(module);
29219459580SLuke Drummond   return changed;
29319459580SLuke Drummond }
29419459580SLuke Drummond 
29519459580SLuke Drummond } // end namespace lldb_renderscript
29619459580SLuke Drummond } // end namespace lldb_private
297