[LLVMdev] Insert a call to a function that takes an LLVM instruction as argument - Calling a function with a bad signature!

Caparros Cabezas Victoria caparrov at inf.ethz.ch
Wed May 28 09:22:27 PDT 2014


Hi,

I would like to instrument LLVM IR such that whenever
a find a load, I insert a call to a self-implemented function
that takes this load as an argument and then do some
operations on the load.

The C++ signature of the function call is:

void analyzeInstruction(Instruction &I) {..}

which in LLVM IR corresponds to (if I am not mistaken):


define void @analyzeInstruction(%"class.llvm::Instruction"* %I).


For that I write a pass such that whenever I find a load, I insert

the following function call:




  virtual bool runOnBasicBlock(Function::iterator &BB) {

            for(BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; ++BI) {

                    if(isa<LoadInst>(&(*BI))){

                        LoadInst *CI = dyn_cast<LoadInst>(BI);

Instruction *CI = dyn_cast<Instruction>(BI);

    CallInst* void_114 = CallInst::Create(analyze_func, CI, "");

    void_114->setCallingConv(CallingConv::C);

  void_114->setTailCall(false);

  AttributeSet void_114_PAL;

  void_114->setAttributes(void_114_PAL);

BB->getInstList ().insertAfter ((Instruction *)CI , void_114 );

}
}
}

Where analyze_func is defined as:

 Function *analyze_func;
PointerType* PointerTy_420;
PointerType* PointerTy_421;
FunctionType* FuncTy_analyzeInstruction;
StructType *StructTy_class_llvm__LoadInst;

StructTy_class_llvm__LoadInst= M.getTypeByName("class.llvm::LoadInst");
 if (!StructTy_class_llvm__LoadInst) {
 StructTy_class_llvm__LoadInst = StructType::create(M.getContext(), "class.llvm::LoadInst");
 }

PointerTy_420 = PointerType::get(StructTy_class_llvm__LoadInst, 0);

 std::vector<Type*> FuncTy_analyzeInstruction_args;
 FuncTy_analyzeInstruction_args.push_back(PointerTy_420);


FuncTy_analyzeInstruction = FunctionType::get(
  Type::getVoidTy(M.getContext()),
  FuncTy_analyzeInstruction_args,
  false);


analyze_func =  M.getFunction("analyzeInstruction");
if (!analyze_funcion){

 analyze_func = Function::Create(
 FuncTy_analyzeInstruction,
   GlobalValue::ExternalLinkage,
   "analyzeInstruction", &M
  );
 analyze_func->setCallingConv (CallingConv::C);
}


The problem is that when the function call is going to be created, I get
the error that the function has bad signature because the expected type
of the argument according to the function definition is:

FTy->getParamType(i) -> %"class.llvm::LoadInst"*

but the type of the argument is the actual type of the value loaded, which
is:


Args[i]->getType() -> i32


How can I create the function call such that the function signature is correct?


Many thanks in advance,


Victoria







More information about the llvm-dev mailing list