有什么方法可以获取llvm顺从指针值的原始类型(即指针类型)

Is any way to get llvm deference pointer value's raw type(i.e. pointer type)

本文关键字:类型 指针 原始 方法 什么 获取 llvm      更新时间:2023-10-16

也许标题有点混乱。但让我给你举个例子。

void foo(int val)
{
    // do something
}
int i = 27;
int* pi = &i;
foo(*pi);

这里,如果我们使用clang编译它,*pi的类型将是i32,但我们知道pi是指针类型。

我的问题是我们使用函数::getgetFunctionParamType方法,结果将是i32。但是我如何使用一些方法来获得"pi"类型,而不是"*pi"类型?这个问题让我困惑了好几天。

更新:

我看到有些人混淆了这个问题。好吧,我已经把这个源代码编译成了LLVM中间格式的flie(即.ll文件),所以我已经达到了生成中间代码的步骤,我可以处理的是与LLVM IR相关的,我只能看到i32、i32*等等(现在没有int、int*)。我不想构造一个指针类型,我只是想以某种方式将*pi"反转"为pi,这样我就可以检查"pi"是指针类型。情况是这样的:我在.ll文件中有*pi,可能pi是

%pi = alloca i32*, align 32 
%1 = load i32** %pi, align 32  
%2 = load volatile i32* %1, align 1
%3 = call foo(i32 %2)  

所以如果我检查函数的参数类型,我只能得到i32,因为它现在是pi。但如果我能得到pi,即%pi=alloca i32align 32,我就能知道pi是指针类型。

我想您正在寻找PointerType* PointerType::get(Type*)

如果我正确理解你的问题,你需要的是调用函数的CallInst的操作数,而不是函数声明。

假设您的Function* F指向foo(i32):

(我在脑海中写道,如果它不能编译,很抱歉)

for(auto U : F->users())
{
    if (CallInst* CI = dyn_cast<CallInst>(U))
    {
        Value* O0 = CI->getOperand(0) // we simply know foo needs one operand
        if (Constant* C = dyn_cast<Constant>(O0))
        {
            // parameter is a constant you can get the type as usual from every value
        }
        else if (LoadInst* LI = dyn_cast<LoadInst>(O0))
        {
            // since the argument is not a constant it must be a value loaded by
            // a LoadInst and LoadInst has the function getPointerOperand()
            Value* PO = LI->getPointerOperand();
            // since we know it's a pointer Operand we can cast safely here
            PointerType* PT = cast<PointerType>(PO->getType());
            PT->dump(); // will print i32* 
        }
    }
}