1*0b57cec5SDimitry Andric //===-- RenderScriptx86ABIFixups.cpp --------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include <set>
10*0b57cec5SDimitry Andric 
11*0b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
12*0b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
13*0b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
14*0b57cec5SDimitry Andric #include "llvm/IR/Function.h"
15*0b57cec5SDimitry Andric #include "llvm/IR/Instruction.h"
16*0b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
17*0b57cec5SDimitry Andric #include "llvm/IR/Module.h"
18*0b57cec5SDimitry Andric #include "llvm/IRReader/IRReader.h"
19*0b57cec5SDimitry Andric #include "llvm/Pass.h"
20*0b57cec5SDimitry Andric 
21*0b57cec5SDimitry Andric #include "lldb/Target/Process.h"
22*0b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
23*0b57cec5SDimitry Andric 
24*0b57cec5SDimitry Andric using namespace lldb_private;
25*0b57cec5SDimitry Andric namespace {
26*0b57cec5SDimitry Andric 
isRSAPICall(llvm::Module & module,llvm::CallInst * call_inst)27*0b57cec5SDimitry Andric bool isRSAPICall(llvm::Module &module, llvm::CallInst *call_inst) {
28*0b57cec5SDimitry Andric   // TODO get the list of renderscript modules from lldb and check if
29*0b57cec5SDimitry Andric   // this llvm::Module calls into any of them.
30*0b57cec5SDimitry Andric   (void)module;
31*0b57cec5SDimitry Andric   const auto func_name = call_inst->getCalledFunction()->getName();
32*0b57cec5SDimitry Andric   if (func_name.startswith("llvm") || func_name.startswith("lldb"))
33*0b57cec5SDimitry Andric     return false;
34*0b57cec5SDimitry Andric 
35*0b57cec5SDimitry Andric   if (call_inst->getCalledFunction()->isIntrinsic())
36*0b57cec5SDimitry Andric     return false;
37*0b57cec5SDimitry Andric 
38*0b57cec5SDimitry Andric   return true;
39*0b57cec5SDimitry Andric }
40*0b57cec5SDimitry Andric 
isRSLargeReturnCall(llvm::Module & module,llvm::CallInst * call_inst)41*0b57cec5SDimitry Andric bool isRSLargeReturnCall(llvm::Module &module, llvm::CallInst *call_inst) {
42*0b57cec5SDimitry Andric   // i686 and x86_64 returns for large vectors in the RenderScript API are not
43*0b57cec5SDimitry Andric   // handled as normal register pairs, but as a hidden sret type. This is not
44*0b57cec5SDimitry Andric   // reflected in the debug info or mangled symbol name, and the android ABI
45*0b57cec5SDimitry Andric   // for x86 and x86_64, (as well as the emulators) specifies there is no AVX,
46*0b57cec5SDimitry Andric   // so bcc generates an sret function because we cannot natively return
47*0b57cec5SDimitry Andric   // 256 bit vectors.
48*0b57cec5SDimitry Andric   // This function simply checks whether a function has a > 128bit return type.
49*0b57cec5SDimitry Andric   // It is perhaps an unreliable heuristic, and relies on bcc not generating
50*0b57cec5SDimitry Andric   // AVX code, so if the android ABI one day provides for AVX, this function
51*0b57cec5SDimitry Andric   // may go out of fashion.
52*0b57cec5SDimitry Andric   (void)module;
53*0b57cec5SDimitry Andric   if (!call_inst || !call_inst->getCalledFunction())
54*0b57cec5SDimitry Andric     return false;
55*0b57cec5SDimitry Andric 
56*0b57cec5SDimitry Andric   return call_inst->getCalledFunction()
57*0b57cec5SDimitry Andric              ->getReturnType()
58*0b57cec5SDimitry Andric              ->getPrimitiveSizeInBits() > 128;
59*0b57cec5SDimitry Andric }
60*0b57cec5SDimitry Andric 
isRSAllocationPtrTy(const llvm::Type * type)61*0b57cec5SDimitry Andric bool isRSAllocationPtrTy(const llvm::Type *type) {
62*0b57cec5SDimitry Andric   if (!type->isPointerTy())
63*0b57cec5SDimitry Andric     return false;
64*0b57cec5SDimitry Andric   auto ptr_type = type->getPointerElementType();
65*0b57cec5SDimitry Andric 
66*0b57cec5SDimitry Andric   return ptr_type->isStructTy() &&
67*0b57cec5SDimitry Andric          ptr_type->getStructName().startswith("struct.rs_allocation");
68*0b57cec5SDimitry Andric }
69*0b57cec5SDimitry Andric 
isRSAllocationTyCallSite(llvm::Module & module,llvm::CallInst * call_inst)70*0b57cec5SDimitry Andric bool isRSAllocationTyCallSite(llvm::Module &module, llvm::CallInst *call_inst) {
71*0b57cec5SDimitry Andric   (void)module;
72*0b57cec5SDimitry Andric   if (!call_inst->hasByValArgument())
73*0b57cec5SDimitry Andric     return false;
74*0b57cec5SDimitry Andric   for (const auto *param : call_inst->operand_values())
75*0b57cec5SDimitry Andric     if (isRSAllocationPtrTy(param->getType()))
76*0b57cec5SDimitry Andric       return true;
77*0b57cec5SDimitry Andric   return false;
78*0b57cec5SDimitry Andric }
79*0b57cec5SDimitry Andric 
cloneToStructRetFnTy(llvm::CallInst * call_inst)80*0b57cec5SDimitry Andric llvm::FunctionType *cloneToStructRetFnTy(llvm::CallInst *call_inst) {
81*0b57cec5SDimitry Andric   // on x86 StructReturn functions return a pointer to the return value, rather
82*0b57cec5SDimitry Andric   // than the return value itself
83*0b57cec5SDimitry Andric   // [ref](http://www.agner.org/optimize/calling_conventions.pdf section 6). We
84*0b57cec5SDimitry Andric   // create a return type by getting the pointer type of the old return type,
85*0b57cec5SDimitry Andric   // and inserting a new initial argument of pointer type of the original
86*0b57cec5SDimitry Andric   // return type.
87*0b57cec5SDimitry Andric   Log *log(
88*0b57cec5SDimitry Andric       GetLogIfAnyCategoriesSet(LIBLLDB_LOG_LANGUAGE | LIBLLDB_LOG_EXPRESSIONS));
89*0b57cec5SDimitry Andric 
90*0b57cec5SDimitry Andric   assert(call_inst && "no CallInst");
91*0b57cec5SDimitry Andric   llvm::Function *orig = call_inst->getCalledFunction();
92*0b57cec5SDimitry Andric   assert(orig && "CallInst has no called function");
93*0b57cec5SDimitry Andric   llvm::FunctionType *orig_type = orig->getFunctionType();
94*0b57cec5SDimitry Andric   auto name = orig->getName();
95*0b57cec5SDimitry Andric   LLDB_LOGF(log, "%s - cloning to StructRet function for '%s'", __FUNCTION__,
96*0b57cec5SDimitry Andric             name.str().c_str());
97*0b57cec5SDimitry Andric 
98*0b57cec5SDimitry Andric   unsigned num_params = orig_type->getNumParams();
99*0b57cec5SDimitry Andric   std::vector<llvm::Type *> new_params{num_params + 1, nullptr};
100*0b57cec5SDimitry Andric   std::vector<llvm::Type *> params{orig_type->param_begin(),
101*0b57cec5SDimitry Andric                                    orig_type->param_end()};
102*0b57cec5SDimitry Andric 
103*0b57cec5SDimitry Andric   // This may not work if the function is somehow declared void as llvm is
104*0b57cec5SDimitry Andric   // strongly typed and represents void* with i8*
105*0b57cec5SDimitry Andric   assert(!orig_type->getReturnType()->isVoidTy() &&
106*0b57cec5SDimitry Andric          "Cannot add StructRet attribute to void function");
107*0b57cec5SDimitry Andric   llvm::PointerType *return_type_ptr_type =
108*0b57cec5SDimitry Andric       llvm::PointerType::getUnqual(orig->getReturnType());
109*0b57cec5SDimitry Andric   assert(return_type_ptr_type &&
110*0b57cec5SDimitry Andric          "failed to get function return type PointerType");
111*0b57cec5SDimitry Andric   if (!return_type_ptr_type)
112*0b57cec5SDimitry Andric     return nullptr;
113*0b57cec5SDimitry Andric 
114*0b57cec5SDimitry Andric   LLDB_LOGF(log,
115*0b57cec5SDimitry Andric             "%s - return type pointer type for StructRet clone @ '0x%p':\n",
116*0b57cec5SDimitry Andric             __FUNCTION__, (void *)return_type_ptr_type);
117*0b57cec5SDimitry Andric   // put the sret pointer argument in place at the beginning of the
118*0b57cec5SDimitry Andric   // argument list.
119*0b57cec5SDimitry Andric   params.emplace(params.begin(), return_type_ptr_type);
120*0b57cec5SDimitry Andric   assert(params.size() == num_params + 1);
121*0b57cec5SDimitry Andric   return llvm::FunctionType::get(return_type_ptr_type, params,
122*0b57cec5SDimitry Andric                                  orig->isVarArg());
123*0b57cec5SDimitry Andric }
124*0b57cec5SDimitry Andric 
findRSCallSites(llvm::Module & module,std::set<llvm::CallInst * > & rs_callsites,bool (* predicate)(llvm::Module &,llvm::CallInst *))125*0b57cec5SDimitry Andric bool findRSCallSites(llvm::Module &module,
126*0b57cec5SDimitry Andric                      std::set<llvm::CallInst *> &rs_callsites,
127*0b57cec5SDimitry Andric                      bool (*predicate)(llvm::Module &, llvm::CallInst *)) {
128*0b57cec5SDimitry Andric   bool found = false;
129*0b57cec5SDimitry Andric 
130*0b57cec5SDimitry Andric   for (auto &func : module.getFunctionList())
131*0b57cec5SDimitry Andric     for (auto &block : func.getBasicBlockList())
132*0b57cec5SDimitry Andric       for (auto &inst : block) {
133*0b57cec5SDimitry Andric         llvm::CallInst *call_inst =
134*0b57cec5SDimitry Andric             llvm::dyn_cast_or_null<llvm::CallInst>(&inst);
135*0b57cec5SDimitry Andric         if (!call_inst || !call_inst->getCalledFunction())
136*0b57cec5SDimitry Andric           // This is not the call-site you are looking for...
137*0b57cec5SDimitry Andric           continue;
138*0b57cec5SDimitry Andric         if (isRSAPICall(module, call_inst) && predicate(module, call_inst)) {
139*0b57cec5SDimitry Andric           rs_callsites.insert(call_inst);
140*0b57cec5SDimitry Andric           found = true;
141*0b57cec5SDimitry Andric         }
142*0b57cec5SDimitry Andric       }
143*0b57cec5SDimitry Andric   return found;
144*0b57cec5SDimitry Andric }
145*0b57cec5SDimitry Andric 
fixupX86StructRetCalls(llvm::Module & module)146*0b57cec5SDimitry Andric bool fixupX86StructRetCalls(llvm::Module &module) {
147*0b57cec5SDimitry Andric   bool changed = false;
148*0b57cec5SDimitry Andric   // changing a basic block while iterating over it seems to have some
149*0b57cec5SDimitry Andric   // undefined behaviour going on so we find all RS callsites first, then fix
150*0b57cec5SDimitry Andric   // them up after consuming the iterator.
151*0b57cec5SDimitry Andric   std::set<llvm::CallInst *> rs_callsites;
152*0b57cec5SDimitry Andric   if (!findRSCallSites(module, rs_callsites, isRSLargeReturnCall))
153*0b57cec5SDimitry Andric     return false;
154*0b57cec5SDimitry Andric 
155*0b57cec5SDimitry Andric   for (auto call_inst : rs_callsites) {
156*0b57cec5SDimitry Andric     llvm::FunctionType *new_func_type = cloneToStructRetFnTy(call_inst);
157*0b57cec5SDimitry Andric     assert(new_func_type &&
158*0b57cec5SDimitry Andric            "failed to clone functionType for Renderscript ABI fixup");
159*0b57cec5SDimitry Andric 
160*0b57cec5SDimitry Andric     llvm::Function *func = call_inst->getCalledFunction();
161*0b57cec5SDimitry Andric     assert(func && "cannot resolve function in RenderScriptRuntime");
162*0b57cec5SDimitry Andric     // Copy the original call arguments
163*0b57cec5SDimitry Andric     std::vector<llvm::Value *> new_call_args(call_inst->arg_begin(),
164*0b57cec5SDimitry Andric                                              call_inst->arg_end());
165*0b57cec5SDimitry Andric 
166*0b57cec5SDimitry Andric     // Allocate enough space to store the return value of the original function
167*0b57cec5SDimitry Andric     // we pass a pointer to this allocation as the StructRet param, and then
168*0b57cec5SDimitry Andric     // copy its value into the lldb return value
169*0b57cec5SDimitry Andric     const llvm::DataLayout &DL = module.getDataLayout();
170*0b57cec5SDimitry Andric     llvm::AllocaInst *return_value_alloc = new llvm::AllocaInst(
171*0b57cec5SDimitry Andric       func->getReturnType(), DL.getAllocaAddrSpace(), "var_vector_return_alloc",
172*0b57cec5SDimitry Andric       call_inst);
173*0b57cec5SDimitry Andric     // use the new allocation as the new first argument
174*0b57cec5SDimitry Andric     new_call_args.emplace(new_call_args.begin(),
175*0b57cec5SDimitry Andric                           llvm::cast<llvm::Value>(return_value_alloc));
176*0b57cec5SDimitry Andric     llvm::PointerType *new_func_ptr_type =
177*0b57cec5SDimitry Andric         llvm::PointerType::get(new_func_type, 0);
178*0b57cec5SDimitry Andric     // Create the type cast from the old function type to the new one
179*0b57cec5SDimitry Andric     llvm::Constant *new_func_cast = llvm::ConstantExpr::getCast(
180*0b57cec5SDimitry Andric         llvm::Instruction::BitCast, func, new_func_ptr_type);
181*0b57cec5SDimitry Andric     // create an allocation for a new function pointer
182*0b57cec5SDimitry Andric     llvm::AllocaInst *new_func_ptr =
183*0b57cec5SDimitry Andric         new llvm::AllocaInst(new_func_ptr_type, DL.getAllocaAddrSpace(),
184*0b57cec5SDimitry Andric                              "new_func_ptr", call_inst);
185*0b57cec5SDimitry Andric     // store the new_func_cast to the newly allocated space
186*0b57cec5SDimitry Andric     (new llvm::StoreInst(new_func_cast, new_func_ptr, call_inst))
187*0b57cec5SDimitry Andric         ->setName("new_func_ptr_load_cast");
188*0b57cec5SDimitry Andric     // load the new function address ready for a jump
189*0b57cec5SDimitry Andric     llvm::LoadInst *new_func_addr_load =
190*0b57cec5SDimitry Andric         new llvm::LoadInst(new_func_ptr->getType()->getPointerElementType(),
191*0b57cec5SDimitry Andric                            new_func_ptr, "load_func_pointer", call_inst);
192*0b57cec5SDimitry Andric     // and create a callinstruction from it
193*0b57cec5SDimitry Andric     llvm::CallInst *new_call_inst =
194*0b57cec5SDimitry Andric         llvm::CallInst::Create(new_func_type, new_func_addr_load, new_call_args,
195*0b57cec5SDimitry Andric                                "new_func_call", call_inst);
196*0b57cec5SDimitry Andric     new_call_inst->setCallingConv(call_inst->getCallingConv());
197*0b57cec5SDimitry Andric     new_call_inst->setTailCall(call_inst->isTailCall());
198*0b57cec5SDimitry Andric     llvm::LoadInst *lldb_save_result_address = new llvm::LoadInst(
199*0b57cec5SDimitry Andric         return_value_alloc->getType()->getPointerElementType(),
200*0b57cec5SDimitry Andric         return_value_alloc, "save_return_val", call_inst);
201*0b57cec5SDimitry Andric 
202*0b57cec5SDimitry Andric     // Now remove the old broken call
203*0b57cec5SDimitry Andric     call_inst->replaceAllUsesWith(lldb_save_result_address);
204*0b57cec5SDimitry Andric     call_inst->eraseFromParent();
205*0b57cec5SDimitry Andric     changed = true;
206*0b57cec5SDimitry Andric   }
207*0b57cec5SDimitry Andric   return changed;
208*0b57cec5SDimitry Andric }
209*0b57cec5SDimitry Andric 
fixupRSAllocationStructByValCalls(llvm::Module & module)210*0b57cec5SDimitry Andric bool fixupRSAllocationStructByValCalls(llvm::Module &module) {
211*0b57cec5SDimitry Andric   // On x86_64, calls to functions in the RS runtime that take an
212*0b57cec5SDimitry Andric   // `rs_allocation` type argument are actually handled as by-ref params by
213*0b57cec5SDimitry Andric   // bcc, but appear to be passed by value by lldb (the callsite all use
214*0b57cec5SDimitry Andric   // `struct byval`). On x86_64 Linux, struct arguments are transferred in
215*0b57cec5SDimitry Andric   // registers if the struct size is no bigger than 128bits
216*0b57cec5SDimitry Andric   // [ref](http://www.agner.org/optimize/calling_conventions.pdf) section 7.1
217*0b57cec5SDimitry Andric   // "Passing and returning objects" otherwise passed on the stack. an object
218*0b57cec5SDimitry Andric   // of type `rs_allocation` is actually 256bits, so should be passed on the
219*0b57cec5SDimitry Andric   // stack. However, code generated by bcc actually treats formal params of
220*0b57cec5SDimitry Andric   // type `rs_allocation` as `rs_allocation *` so we need to convert the
221*0b57cec5SDimitry Andric   // calling convention to pass by reference, and remove any hint of byval from
222*0b57cec5SDimitry Andric   // formal parameters.
223*0b57cec5SDimitry Andric   bool changed = false;
224*0b57cec5SDimitry Andric   std::set<llvm::CallInst *> rs_callsites;
225*0b57cec5SDimitry Andric   if (!findRSCallSites(module, rs_callsites, isRSAllocationTyCallSite))
226*0b57cec5SDimitry Andric     return false;
227*0b57cec5SDimitry Andric 
228*0b57cec5SDimitry Andric   std::set<llvm::Function *> rs_functions;
229*0b57cec5SDimitry Andric 
230*0b57cec5SDimitry Andric   // for all call instructions
231*0b57cec5SDimitry Andric   for (auto call_inst : rs_callsites) {
232*0b57cec5SDimitry Andric     // add the called function to a set so that we can strip its byval
233*0b57cec5SDimitry Andric     // attributes in another pass
234*0b57cec5SDimitry Andric     rs_functions.insert(call_inst->getCalledFunction());
235*0b57cec5SDimitry Andric 
236*0b57cec5SDimitry Andric     // get the function attributes
237*0b57cec5SDimitry Andric     llvm::AttributeList call_attribs = call_inst->getAttributes();
238*0b57cec5SDimitry Andric 
239*0b57cec5SDimitry Andric     // iterate over the argument attributes
240*0b57cec5SDimitry Andric     for (unsigned I = call_attribs.index_begin(); I != call_attribs.index_end();
241*0b57cec5SDimitry Andric          I++) {
242*0b57cec5SDimitry Andric       // if this argument is passed by val
243*0b57cec5SDimitry Andric       if (call_attribs.hasAttribute(I, llvm::Attribute::ByVal)) {
244*0b57cec5SDimitry Andric         // strip away the byval attribute
245*0b57cec5SDimitry Andric         call_inst->removeAttribute(I, llvm::Attribute::ByVal);
246*0b57cec5SDimitry Andric         changed = true;
247*0b57cec5SDimitry Andric       }
248*0b57cec5SDimitry Andric     }
249*0b57cec5SDimitry Andric   }
250*0b57cec5SDimitry Andric 
251*0b57cec5SDimitry Andric   // for all called function decls
252*0b57cec5SDimitry Andric   for (auto func : rs_functions) {
253*0b57cec5SDimitry Andric     // inspect all of the arguments in the call
254*0b57cec5SDimitry Andric     for (auto &arg : func->args()) {
255*0b57cec5SDimitry Andric       if (arg.hasByValAttr()) {
256*0b57cec5SDimitry Andric         arg.removeAttr(llvm::Attribute::ByVal);
257*0b57cec5SDimitry Andric         changed = true;
258*0b57cec5SDimitry Andric       }
259*0b57cec5SDimitry Andric     }
260*0b57cec5SDimitry Andric   }
261*0b57cec5SDimitry Andric   return changed;
262*0b57cec5SDimitry Andric }
263*0b57cec5SDimitry Andric } // end anonymous namespace
264*0b57cec5SDimitry Andric 
265*0b57cec5SDimitry Andric namespace lldb_private {
266*0b57cec5SDimitry Andric namespace lldb_renderscript {
267*0b57cec5SDimitry Andric 
fixupX86FunctionCalls(llvm::Module & module)268*0b57cec5SDimitry Andric bool fixupX86FunctionCalls(llvm::Module &module) {
269*0b57cec5SDimitry Andric   return fixupX86StructRetCalls(module);
270*0b57cec5SDimitry Andric }
271*0b57cec5SDimitry Andric 
fixupX86_64FunctionCalls(llvm::Module & module)272*0b57cec5SDimitry Andric bool fixupX86_64FunctionCalls(llvm::Module &module) {
273*0b57cec5SDimitry Andric   bool changed = false;
274*0b57cec5SDimitry Andric   changed |= fixupX86StructRetCalls(module);
275*0b57cec5SDimitry Andric   changed |= fixupRSAllocationStructByValCalls(module);
276*0b57cec5SDimitry Andric   return changed;
277*0b57cec5SDimitry Andric }
278*0b57cec5SDimitry Andric 
279*0b57cec5SDimitry Andric } // end namespace lldb_renderscript
280*0b57cec5SDimitry Andric } // end namespace lldb_private
281*0b57cec5SDimitry Andric