在调用llvm中获取bitcasts中函数的参数

时间:2016-11-25 03:51:21

标签: llvm llvm-gcc

我对llvm很新,并且无法深入挖掘以下IR线:

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

我需要从中提取的是函数参数的类型(即(float%11,i32 addrspace(1)*%arrayidx))

我已经尝试过以下内容,并且使用ConstExpr进行了一些调整,但无法提取该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)
            ...
          }
        }
      }
    }
  }
}

上面的代码给了我类型(float,i32 *)而不是(float,i32 addrspace(1)*)

请帮忙吗?

1 个答案:

答案 0 :(得分:4)

llvm ir

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

将函数类型float (float, i32*)转换为float (float, i32 addrspace(1)*)并使用参数(%11, %arrayidx)调用它。

如果你想要参数的类型,你可以使用callInst::getArgOperand来检查它,以便在调用指令本身中获取参数。

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会为您提供此功能。

有关详细信息,您可以浏览http://llvm.org/docs/doxygen/html/classllvm_1_1CallInst.html