Changing the operands in the CallInst

I implement the following function,which gets CallInst * and should perform the following:

  1. Change the value of the argument if condition1 takes place

  2. Change the type of the argument if condition2 takes place

  3. Add addition argument/s if condition3 takes place

void argChange(CallInst * I)


for (unsigned index = 0; index < I->getNumOperands(); ++index) {
do_something_with (I->getOperand(index),condition);

I think that the 1. can be implemented using I->setOperand(index,newValue)
Can anyone advice me how 2. & 3. could be implemented.

Hi Yakov,

For cases 2) and 3) - CallInst is bound to the underlying function
type, to my knowledge. So you cannot modify the function type, but
instead you can create another CallInst with different function type
and replace original one with it. If you do not change the return
type, replaceAllUsesWith method would work. In attached code example
CallInst replacement logic is in lines 223-285.

- Dima.

compile.cpp (9.21 KB)