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