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

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

可能标题有点乱。但让我给你举个例子。

void foo(int val)
{
    // do something
}

int i = 27;
int* pi = &i;

foo(*pi);

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

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

更新:

我看到有些人对这个问题感到困惑。好吧,我已经把这个源代码编译成LLVM中间格式flie(即.ll文件),所以我已经到了中间代码生成的步骤,我能处理的是LLVM IR相关的,我只能看到i32,i32 *和依此类推(现在没有 int, int* 了)。而且我不想构造一种指针类型,我只是想以某种方式 'reverse' *pi 到 pi 以便我可以检查 'pi' 是指针类型。情况是这样的:我有*pi,在.ll文件里,可能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 i32 align 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* 
        }
    }
}