<div dir="ltr"><div><div><div>
        <span class="gmail-vote-count-post gmail-"></span>Hi all,<br><br>I am Sue. I am writing a LLVM pass to change the type of float-point variables in the original program to long double. My framework is like this:<br><pre><code>for (auto B : F) {
//for every basic block B in the funtion F
for (auto I : B){
//for every instruction I in the basic block B
    if (I is a AllocaInst){
       If the type of I is float or double,
       then change the type to long double, construct new I';
         for (auto U: I->users()){
              //propogate the new type to I's users
              transform(U, I, I');
            }
       }
   }
}

transform(U, I, I'){
    if (U is StoreInst)
        Construct new storeinst;
    if(U is LoadInst){
        Construct new_load;
        for(auto W: U->users())
           transform(W, U, new_load);
     }
    ... ....   
}</code></pre><br><div><div>
    <div class="gmail-post-text">

<p>I have finished changing the type of AllocaInst, StoreInst, LoadInst, BinaryOperator. </p>

<p>However, I have some trouble when implementing the transformation of CallInst:</p>

<ol><li><p>Change the type of the arguments of function calls</p></li><li>Construct the body of called function with new type<br></li><li><p>Change the type of the return value</p></li></ol>

<p>For example, I want to change the following IR:</p>

<pre><code>    %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
    }
</code></pre>

<p>To IR_New:</p>

<pre><code>    %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
    }
</code></pre><br><p>My original idea is when I find a CallInst,</p>

<pre><code>   if (CallInst *call = dyn_cast<CallInst>(it)){
</code></pre>

<p>do the following three steps:</p>

<ol><li><p>Construct the new FunctionType </p>

<pre><code>x86_fp80(x86_fp80, x86_fp80)
</code></pre>

<p>using</p>

<pre><code>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);
</code></pre></li><li><p>Construct function with new type in Step 1, i.e. construct new_add in the example</p>

<pre><code>Function *fun = call->getCalledFunction();
Function *new_fun = Function::Create(new_fun_type,fun->getLinkage(), "new_add", fun->getParent());
</code></pre></li><li><p>Construct a new CallInst with the new function obtained from step 2.</p>

<pre><code>CallInst *new_call = CallInst::Create(new_fun, *arrayRefOperands, "newCall", call);
new_call->takeName(call);
}
</code></pre></li></ol>

<p>However, in this way, I got the following:<br></p>

<pre><code>   %call = call x86_fp80 (x86_fp80, x86_fp80, ...) @0(x86_fp80 %5, x86_fp80 %7)

   declare x86_fp80 @new_add(x86_fp80, x86_fp80, ...)<br><br></code></pre><p>A new definition of called function is constructed(declare x86_fp80 
@new_add(x86_fp80, x86_fp80, ...)), but the body of this new function is
 empty. I am very confused how to add the body and get the IR_New I 
want. My naive idea is:</p>

<code>   for (Instruction i : called function(add in the example)){
<br>    create new_i with type x86_fp80;
       <br>    insert new_i in the new function constructed(new_add in the example);
<br>     }   
</code>

<p>Is this a good way to achieve my goal please?</p>

<p>Any advice will be greatly appreciated :)</p><p><br></p><p>Sincerely,</p><p>Suhua<br></p><pre><br></pre></div></div></div></div></div></div></div>