提取PointerType中包含的值

问题描述

LLVM IR

  call void @llvm.dbg.declare(Metadata i32* %z,Metadata !24,Metadata !DIExpression()),!dbg !25
  %arraydecay1 = getelementptr inbounds [55 x i8],[55 x i8]* %input,i32 0,!dbg !26
  %call2 = call i64 @strlen(i8* %arraydecay1) #4,!dbg !28

strlen doc https://www.cplusplus.com/reference/cstring/strlen/

%call2是strlen函数的返回值,它是size_t类型。我认为它是一种struct类型,但事实证明它是一个指针类型,类型为i64(i8 *)*

如何取消引用并获取指针值中包含的整数值。

编辑:我使用了错误的操作数,但问题尚未解决。它不是关于指针类型的,我有问题的int 64类型转换。见下文

 CallInst I; //passed by reference
  CallSite cs(&I);
  if(!cs.getInstruction()){
    return;
  } else {
    for (User* user : cs.getInstruction()->users()) {
      if (Instruction* i = dyn_cast<Instruction>(user)) {
        Value *v1 = dyn_cast<Value>(i->getoperand(0));
        errs() << "Type:==" << *(v1) << "\n";
        errs() << "is integer type=" << (v1->getType()->isIntegerTy()) << "\n";
        ConstantInt *cint = dyn_cast<ConstantInt>(v1);
        if (cint) {
          errs() << "constant int" << *cint << "\n";
        } else {
          errs() << "not a constant int??" << "\n";
        }
      }
    }
  }

Type:==  %call2 = call i64 @strlen(i8* %arraydecay1) #4,!dbg !28
is integer type=1
not a constant int??

解决方法

如果您谈论的是%call2调用的返回值strlen,则其类型为i64,就像它写在call i64 @strlen(...)中一样。

i64 (i8*)*类型是指向接受i8*并返回i64的函数的指针。它对应于C语言中&strlen表达式的类型。

因此,如果要使用strlen的返回值,则只需使用%call2的值。