How to change the type of operands in the Callinst in llvm? -
i trying implement transformation of callinst , perform following:
change type of arguments of function calls
change type of return value
for example, want change following ir:
%call = call double @add(double %0, double %1) define double @add(double %x, double %y) #0 { entry: %x.addr = alloca double, align 8 %y.addr = alloca double, align 8 store double %x, double* %x.addr, align 8 store double %y, double* %y.addr, align 8 %0 = load double, double* %x.addr, align 8 %1 = load double, double* %x.addr, align 8 %add = fadd double %0, %1 ret double %add }
to ir_new:
%call = call x86_fp80 @new_add(x86_fp80 %0, x86_fp80 %1) define x86_fp80 @new_add(x86_fp80 %x, x86_fp80 %y) #0 { entry: %x.addr = alloca x86_fp80, align 16 %y.addr = alloca x86_fp80, align 16 store x86_fp80 %x, x86_fp80* %x.addr, align 16 store x86_fp80 %y, x86_fp80* %y.addr, align 16 %0 = load x86_fp80, x86_fp80* %x.addr, align 16 %1 = load x86_fp80, x86_fp80* %x.addr, align 16 %add = fadd x86_fp80 %0, %1 ret x86_fp80 %add }
i have finished changing type of allocainst, storeinst, loadinst, binaryoperator , returninst.
i confused how deal callinst.
my original idea when iterating instructions, if find callinst,
if (callinst *call = dyn_cast<callinst>(it)){
do following 3 steps:
construct new functiontype
x86_fp80(x86_fp80, x86_fp80)
using
std::vector<type*> paramtys; paramtys.push_back(type::getx86_fp80ty(context)); paramtys.push_back(type::getx86_fp80ty(context)); functiontype *new_fun_type = functiontype::get(type::getx86_fp80ty(context), paramtys, true);
construct function new type in step 1, i.e. construct new_add in example
function *fun = call->getcalledfunction(); function *new_fun = function::create(new_fun_type,fun->getlinkage(), "", fun->getparent());
construct new callinst new function obtained step 2.
callinst *new_call = callinst::create(new_fun, *arrayrefoperands, "newcall", call); new_call->takename(call); }
however, in way, got following ir instead of ir_new want:
%call = call x86_fp80 (x86_fp80, x86_fp80, ...) @0(x86_fp80 %5, x86_fp80 %7) declare x86_fp80 @new_add(x86_fp80, x86_fp80, ...)
a new definition of called function constructed(declare x86_fp80 @new_add(x86_fp80, x86_fp80, ...)), body of new function empty. confused how add body , ir_new want. naive idea is:
(instruction : called function(add in example)){ create new_i type x86_fp80; insert new_i in new function constructed(new_add in example); }
is way achieve goal?
any advice appreciated :)
you can use llvm::value::mutatetype(llvm::ty) change double type value x86_fp80 if no longer using original function somewhere else. goto function definition using callinst->getcalledfunction() , iterate on value mutate double types x86_fp80.
ref: http://llvm.org/docs/doxygen/html/classllvm_1_1value.html#ac0f09c2c9951158f9eecfaf7068d7b20
Comments
Post a Comment