Shervin
Shervin

Reputation: 409

Get the arguments of a function in bitcasts in calls, llvm

I am pretty new with llvm and having trouble digging deep into the following IR line:

%call2 = call float bitcast (float (float, i32*)* @function to float (float, i32 addrspace(1)*)*)(float %11, i32 addrspace(1)* %arrayidx)

What I need to extract from this is line the type of the arguments of the function (i.e., (float %11, i32 addrspace(1)* %arrayidx))

I have tried the following, and played arround with ConstExpr a little as well, but cannot get to extract that addrspace(1)

for (Function::iterator block = F.begin(), blockEnd = F.end(); block != blockEnd; ++block) {
  for (BasicBlock::iterator inst = block->begin(), instEnd = block->end(); inst != instEnd; ++inst) {
    if (CallInst *call = dyn_cast<CallInst>(inst)) {
      Function *calledFunction = call->getCalledFunction();
      if (calledFunction == NULL) { // Called function is wrapped in a bitcast
      Value* v = call->getCalledValue();
      calledFunction = dyn_cast<Function>(v->stripPointerCasts());
      FunctionType *ft = calledFunction->getFunctionType(); // This gives me the type "from" (the args without addrspace(1)
      for( Function::arg_iterator arg = calledFunction->arg_begin(), marg_end = calledFunction->arg_end(); arg != marg_end ; arg++){
        Type *argTy = arg->getType();
        if (PointerType *ptrTy = dyn_cast<PointerType>(argTy)) {
          if( ptrTy->getAddressSpace() !=0)
            ...
          }
        }
      }
    }
  }
}

The above code gives me the types (float, i32*) and not (float, i32 addrspace(1)*)

Any help please?

Upvotes: 2

Views: 1971

Answers (1)

Chirag Patel
Chirag Patel

Reputation: 1161

The llvm ir

%call2 = call float bitcast (float (float, i32*)* @function to float (float, i32 addrspace(1)*)*)(float %11, i32 addrspace(1)* %arrayidx)

is casting function type float (float, i32*) to float (float, i32 addrspace(1)*) and calling it with argument (%11, %arrayidx).

If you want the types of argument you can check it using callInst::getArgOperand to get arguments in call instruction itself.

for (Function::iterator block = F.begin(), blockEnd = F.end(); block != blockEnd; ++block) {
    for (BasicBlock::iterator inst = block->begin(), instEnd = block->end(); inst != instEnd; ++inst) {
        if (CallInst *call = dyn_cast<CallInst>(inst)) {
                Value *val11 = call->getArgOperand(0);
                Value *valarrayIdx = call->getArgOperand(1);

                Type *val11ty = val11->getType(); // this should be of float
                Type *valarrayIdx = valarrayIdx->getType(); // this should be of i32 address(1)*
            }
        }
    }

CallInst::getCalledFunction will give you the function.

For more info you can go through http://llvm.org/docs/doxygen/html/classllvm_1_1CallInst.html

Upvotes: 4

Related Questions