1dda28197Spatrick //===-- RenderScriptx86ABIFixups.cpp --------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick
9061da546Spatrick #include <set>
10061da546Spatrick
11061da546Spatrick #include "llvm/ADT/StringRef.h"
12061da546Spatrick #include "llvm/IR/BasicBlock.h"
13061da546Spatrick #include "llvm/IR/Constants.h"
14061da546Spatrick #include "llvm/IR/Function.h"
15061da546Spatrick #include "llvm/IR/Instruction.h"
16061da546Spatrick #include "llvm/IR/Instructions.h"
17061da546Spatrick #include "llvm/IR/Module.h"
18061da546Spatrick #include "llvm/IRReader/IRReader.h"
19061da546Spatrick #include "llvm/Pass.h"
20061da546Spatrick
21061da546Spatrick #include "lldb/Target/Process.h"
22*f6aab3d8Srobert #include "lldb/Utility/LLDBLog.h"
23061da546Spatrick #include "lldb/Utility/Log.h"
24061da546Spatrick
25061da546Spatrick using namespace lldb_private;
26061da546Spatrick
isRSAPICall(llvm::CallInst * call_inst)27*f6aab3d8Srobert static bool isRSAPICall(llvm::CallInst *call_inst) {
28061da546Spatrick // TODO get the list of renderscript modules from lldb and check if
29061da546Spatrick // this llvm::Module calls into any of them.
30061da546Spatrick const auto func_name = call_inst->getCalledFunction()->getName();
31061da546Spatrick if (func_name.startswith("llvm") || func_name.startswith("lldb"))
32061da546Spatrick return false;
33061da546Spatrick
34061da546Spatrick if (call_inst->getCalledFunction()->isIntrinsic())
35061da546Spatrick return false;
36061da546Spatrick
37061da546Spatrick return true;
38061da546Spatrick }
39061da546Spatrick
isRSLargeReturnCall(llvm::CallInst * call_inst)40*f6aab3d8Srobert static bool isRSLargeReturnCall(llvm::CallInst *call_inst) {
41061da546Spatrick // i686 and x86_64 returns for large vectors in the RenderScript API are not
42061da546Spatrick // handled as normal register pairs, but as a hidden sret type. This is not
43061da546Spatrick // reflected in the debug info or mangled symbol name, and the android ABI
44061da546Spatrick // for x86 and x86_64, (as well as the emulators) specifies there is no AVX,
45061da546Spatrick // so bcc generates an sret function because we cannot natively return
46061da546Spatrick // 256 bit vectors.
47061da546Spatrick // This function simply checks whether a function has a > 128bit return type.
48061da546Spatrick // It is perhaps an unreliable heuristic, and relies on bcc not generating
49061da546Spatrick // AVX code, so if the android ABI one day provides for AVX, this function
50061da546Spatrick // may go out of fashion.
51061da546Spatrick if (!call_inst || !call_inst->getCalledFunction())
52061da546Spatrick return false;
53061da546Spatrick
54061da546Spatrick return call_inst->getCalledFunction()
55061da546Spatrick ->getReturnType()
56061da546Spatrick ->getPrimitiveSizeInBits() > 128;
57061da546Spatrick }
58061da546Spatrick
isRSAllocationTy(const llvm::Type * type)59*f6aab3d8Srobert static bool isRSAllocationTy(const llvm::Type *type) {
60*f6aab3d8Srobert return type->isStructTy() &&
61*f6aab3d8Srobert type->getStructName().startswith("struct.rs_allocation");
62061da546Spatrick }
63061da546Spatrick
isRSAllocationTyCallSite(llvm::CallInst * call_inst)64*f6aab3d8Srobert static bool isRSAllocationTyCallSite(llvm::CallInst *call_inst) {
65061da546Spatrick if (!call_inst->hasByValArgument())
66061da546Spatrick return false;
67*f6aab3d8Srobert for (unsigned i = 0; i < call_inst->arg_size(); ++i) {
68*f6aab3d8Srobert if (llvm::Type *ByValTy = call_inst->getParamByValType(i))
69*f6aab3d8Srobert if (isRSAllocationTy(ByValTy))
70061da546Spatrick return true;
71*f6aab3d8Srobert }
72061da546Spatrick return false;
73061da546Spatrick }
74061da546Spatrick
cloneToStructRetFnTy(llvm::CallInst * call_inst)75*f6aab3d8Srobert static llvm::FunctionType *cloneToStructRetFnTy(llvm::CallInst *call_inst) {
76061da546Spatrick // on x86 StructReturn functions return a pointer to the return value, rather
77061da546Spatrick // than the return value itself
78061da546Spatrick // [ref](http://www.agner.org/optimize/calling_conventions.pdf section 6). We
79061da546Spatrick // create a return type by getting the pointer type of the old return type,
80061da546Spatrick // and inserting a new initial argument of pointer type of the original
81061da546Spatrick // return type.
82*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Language | LLDBLog::Expressions);
83061da546Spatrick
84061da546Spatrick assert(call_inst && "no CallInst");
85061da546Spatrick llvm::Function *orig = call_inst->getCalledFunction();
86061da546Spatrick assert(orig && "CallInst has no called function");
87061da546Spatrick llvm::FunctionType *orig_type = orig->getFunctionType();
88061da546Spatrick auto name = orig->getName();
89061da546Spatrick LLDB_LOGF(log, "%s - cloning to StructRet function for '%s'", __FUNCTION__,
90061da546Spatrick name.str().c_str());
91061da546Spatrick
92061da546Spatrick unsigned num_params = orig_type->getNumParams();
93061da546Spatrick std::vector<llvm::Type *> new_params{num_params + 1, nullptr};
94061da546Spatrick std::vector<llvm::Type *> params{orig_type->param_begin(),
95061da546Spatrick orig_type->param_end()};
96061da546Spatrick
97061da546Spatrick // This may not work if the function is somehow declared void as llvm is
98061da546Spatrick // strongly typed and represents void* with i8*
99061da546Spatrick assert(!orig_type->getReturnType()->isVoidTy() &&
100061da546Spatrick "Cannot add StructRet attribute to void function");
101061da546Spatrick llvm::PointerType *return_type_ptr_type =
102061da546Spatrick llvm::PointerType::getUnqual(orig->getReturnType());
103061da546Spatrick assert(return_type_ptr_type &&
104061da546Spatrick "failed to get function return type PointerType");
105061da546Spatrick if (!return_type_ptr_type)
106061da546Spatrick return nullptr;
107061da546Spatrick
108061da546Spatrick LLDB_LOGF(log,
109061da546Spatrick "%s - return type pointer type for StructRet clone @ '0x%p':\n",
110061da546Spatrick __FUNCTION__, (void *)return_type_ptr_type);
111061da546Spatrick // put the sret pointer argument in place at the beginning of the
112061da546Spatrick // argument list.
113061da546Spatrick params.emplace(params.begin(), return_type_ptr_type);
114061da546Spatrick assert(params.size() == num_params + 1);
115061da546Spatrick return llvm::FunctionType::get(return_type_ptr_type, params,
116061da546Spatrick orig->isVarArg());
117061da546Spatrick }
118061da546Spatrick
119*f6aab3d8Srobert static bool
findRSCallSites(llvm::Module & module,std::set<llvm::CallInst * > & rs_callsites,bool (* predicate)(llvm::CallInst *))120*f6aab3d8Srobert findRSCallSites(llvm::Module &module, std::set<llvm::CallInst *> &rs_callsites,
121*f6aab3d8Srobert bool (*predicate)(llvm::CallInst *)) {
122061da546Spatrick bool found = false;
123061da546Spatrick
124061da546Spatrick for (auto &func : module.getFunctionList())
125*f6aab3d8Srobert for (auto &block : func)
126061da546Spatrick for (auto &inst : block) {
127061da546Spatrick llvm::CallInst *call_inst =
128061da546Spatrick llvm::dyn_cast_or_null<llvm::CallInst>(&inst);
129061da546Spatrick if (!call_inst || !call_inst->getCalledFunction())
130061da546Spatrick // This is not the call-site you are looking for...
131061da546Spatrick continue;
132*f6aab3d8Srobert if (isRSAPICall(call_inst) && predicate(call_inst)) {
133061da546Spatrick rs_callsites.insert(call_inst);
134061da546Spatrick found = true;
135061da546Spatrick }
136061da546Spatrick }
137061da546Spatrick return found;
138061da546Spatrick }
139061da546Spatrick
fixupX86StructRetCalls(llvm::Module & module)140*f6aab3d8Srobert static bool fixupX86StructRetCalls(llvm::Module &module) {
141061da546Spatrick bool changed = false;
142061da546Spatrick // changing a basic block while iterating over it seems to have some
143061da546Spatrick // undefined behaviour going on so we find all RS callsites first, then fix
144061da546Spatrick // them up after consuming the iterator.
145061da546Spatrick std::set<llvm::CallInst *> rs_callsites;
146061da546Spatrick if (!findRSCallSites(module, rs_callsites, isRSLargeReturnCall))
147061da546Spatrick return false;
148061da546Spatrick
149061da546Spatrick for (auto call_inst : rs_callsites) {
150061da546Spatrick llvm::FunctionType *new_func_type = cloneToStructRetFnTy(call_inst);
151061da546Spatrick assert(new_func_type &&
152061da546Spatrick "failed to clone functionType for Renderscript ABI fixup");
153061da546Spatrick
154061da546Spatrick llvm::Function *func = call_inst->getCalledFunction();
155061da546Spatrick assert(func && "cannot resolve function in RenderScriptRuntime");
156061da546Spatrick // Copy the original call arguments
157dda28197Spatrick std::vector<llvm::Value *> new_call_args(call_inst->arg_begin(),
158dda28197Spatrick call_inst->arg_end());
159061da546Spatrick
160061da546Spatrick // Allocate enough space to store the return value of the original function
161061da546Spatrick // we pass a pointer to this allocation as the StructRet param, and then
162061da546Spatrick // copy its value into the lldb return value
163061da546Spatrick const llvm::DataLayout &DL = module.getDataLayout();
164061da546Spatrick llvm::AllocaInst *return_value_alloc = new llvm::AllocaInst(
165061da546Spatrick func->getReturnType(), DL.getAllocaAddrSpace(), "var_vector_return_alloc",
166061da546Spatrick call_inst);
167061da546Spatrick // use the new allocation as the new first argument
168061da546Spatrick new_call_args.emplace(new_call_args.begin(),
169061da546Spatrick llvm::cast<llvm::Value>(return_value_alloc));
170061da546Spatrick llvm::PointerType *new_func_ptr_type =
171061da546Spatrick llvm::PointerType::get(new_func_type, 0);
172061da546Spatrick // Create the type cast from the old function type to the new one
173061da546Spatrick llvm::Constant *new_func_cast = llvm::ConstantExpr::getCast(
174061da546Spatrick llvm::Instruction::BitCast, func, new_func_ptr_type);
175061da546Spatrick // create an allocation for a new function pointer
176061da546Spatrick llvm::AllocaInst *new_func_ptr =
177061da546Spatrick new llvm::AllocaInst(new_func_ptr_type, DL.getAllocaAddrSpace(),
178061da546Spatrick "new_func_ptr", call_inst);
179061da546Spatrick // store the new_func_cast to the newly allocated space
180061da546Spatrick (new llvm::StoreInst(new_func_cast, new_func_ptr, call_inst))
181061da546Spatrick ->setName("new_func_ptr_load_cast");
182061da546Spatrick // load the new function address ready for a jump
183*f6aab3d8Srobert llvm::LoadInst *new_func_addr_load = new llvm::LoadInst(
184*f6aab3d8Srobert new_func_ptr_type, new_func_ptr, "load_func_pointer", call_inst);
185061da546Spatrick // and create a callinstruction from it
186061da546Spatrick llvm::CallInst *new_call_inst =
187061da546Spatrick llvm::CallInst::Create(new_func_type, new_func_addr_load, new_call_args,
188061da546Spatrick "new_func_call", call_inst);
189061da546Spatrick new_call_inst->setCallingConv(call_inst->getCallingConv());
190061da546Spatrick new_call_inst->setTailCall(call_inst->isTailCall());
191*f6aab3d8Srobert llvm::LoadInst *lldb_save_result_address =
192*f6aab3d8Srobert new llvm::LoadInst(func->getReturnType(), return_value_alloc,
193*f6aab3d8Srobert "save_return_val", call_inst);
194061da546Spatrick
195061da546Spatrick // Now remove the old broken call
196061da546Spatrick call_inst->replaceAllUsesWith(lldb_save_result_address);
197061da546Spatrick call_inst->eraseFromParent();
198061da546Spatrick changed = true;
199061da546Spatrick }
200061da546Spatrick return changed;
201061da546Spatrick }
202061da546Spatrick
fixupRSAllocationStructByValCalls(llvm::Module & module)203*f6aab3d8Srobert static bool fixupRSAllocationStructByValCalls(llvm::Module &module) {
204061da546Spatrick // On x86_64, calls to functions in the RS runtime that take an
205061da546Spatrick // `rs_allocation` type argument are actually handled as by-ref params by
206061da546Spatrick // bcc, but appear to be passed by value by lldb (the callsite all use
207061da546Spatrick // `struct byval`). On x86_64 Linux, struct arguments are transferred in
208061da546Spatrick // registers if the struct size is no bigger than 128bits
209061da546Spatrick // [ref](http://www.agner.org/optimize/calling_conventions.pdf) section 7.1
210061da546Spatrick // "Passing and returning objects" otherwise passed on the stack. an object
211061da546Spatrick // of type `rs_allocation` is actually 256bits, so should be passed on the
212061da546Spatrick // stack. However, code generated by bcc actually treats formal params of
213061da546Spatrick // type `rs_allocation` as `rs_allocation *` so we need to convert the
214061da546Spatrick // calling convention to pass by reference, and remove any hint of byval from
215061da546Spatrick // formal parameters.
216061da546Spatrick bool changed = false;
217061da546Spatrick std::set<llvm::CallInst *> rs_callsites;
218061da546Spatrick if (!findRSCallSites(module, rs_callsites, isRSAllocationTyCallSite))
219061da546Spatrick return false;
220061da546Spatrick
221061da546Spatrick std::set<llvm::Function *> rs_functions;
222061da546Spatrick
223061da546Spatrick // for all call instructions
224061da546Spatrick for (auto call_inst : rs_callsites) {
225061da546Spatrick // add the called function to a set so that we can strip its byval
226061da546Spatrick // attributes in another pass
227061da546Spatrick rs_functions.insert(call_inst->getCalledFunction());
228061da546Spatrick
229061da546Spatrick // get the function attributes
230061da546Spatrick llvm::AttributeList call_attribs = call_inst->getAttributes();
231061da546Spatrick
232061da546Spatrick // iterate over the argument attributes
233*f6aab3d8Srobert for (unsigned I : call_attribs.indexes()) {
234061da546Spatrick // if this argument is passed by val
235*f6aab3d8Srobert if (call_attribs.hasAttributeAtIndex(I, llvm::Attribute::ByVal)) {
236061da546Spatrick // strip away the byval attribute
237*f6aab3d8Srobert call_inst->removeAttributeAtIndex(I, llvm::Attribute::ByVal);
238061da546Spatrick changed = true;
239061da546Spatrick }
240061da546Spatrick }
241061da546Spatrick }
242061da546Spatrick
243061da546Spatrick // for all called function decls
244061da546Spatrick for (auto func : rs_functions) {
245061da546Spatrick // inspect all of the arguments in the call
246061da546Spatrick for (auto &arg : func->args()) {
247061da546Spatrick if (arg.hasByValAttr()) {
248061da546Spatrick arg.removeAttr(llvm::Attribute::ByVal);
249061da546Spatrick changed = true;
250061da546Spatrick }
251061da546Spatrick }
252061da546Spatrick }
253061da546Spatrick return changed;
254061da546Spatrick }
255061da546Spatrick
256061da546Spatrick namespace lldb_private {
257061da546Spatrick namespace lldb_renderscript {
258061da546Spatrick
fixupX86FunctionCalls(llvm::Module & module)259061da546Spatrick bool fixupX86FunctionCalls(llvm::Module &module) {
260061da546Spatrick return fixupX86StructRetCalls(module);
261061da546Spatrick }
262061da546Spatrick
fixupX86_64FunctionCalls(llvm::Module & module)263061da546Spatrick bool fixupX86_64FunctionCalls(llvm::Module &module) {
264061da546Spatrick bool changed = false;
265061da546Spatrick changed |= fixupX86StructRetCalls(module);
266061da546Spatrick changed |= fixupRSAllocationStructByValCalls(module);
267061da546Spatrick return changed;
268061da546Spatrick }
269061da546Spatrick
270061da546Spatrick } // end namespace lldb_renderscript
271061da546Spatrick } // end namespace lldb_private
272