Is any way to get llvm deference pointer value'

2019-09-05 11:14发布

问题:

Maybe the title is somehow confused. but let me show you a example.

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

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

foo(*pi);

Here, if we compile it using clang, the type of *pi will be i32, but we know pi is pointer type.

my question is we use Function::getgetFunctionParamType method, the result will be i32. but how do I use some wayst to get ' pi ' type, not ' *pi ' type? This problem has confused me some days.

Update:

I see some people confused this question. Alright, I have compiled this source code to the LLVM intermediate format flie(i.e. .ll file), so I have reached the step that intermediate code generation, what I can handle is LLVM IR related and I can only see i32, i32* and so on(there is no int, int* now). And I do not want to construct one pointer type, I just somehow want to 'reverse' *pi to pi so that I can check 'pi' is pointer type. The situation is like this: I have *pi, in the .ll file, maybe pi is

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

so if I check the argument type of function, I can only get i32, because it is pi now. But if I can get pi, i.e. %pi = alloca i32 align 32, I can know pi is pointer type.

回答1:

If I understand your question correctly what you need are the Operands of the CallInst calling the function, not the function declaration.

Assuming you have your Function* F pointing to foo(i32):

(I write from mind so sorry if it won't compile)

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* 
        }
    }
}


回答2:

I think you're looking for PointerType* PointerType::get(Type*)