Is everything a pointer in LLVM IR?

I am iterating over global vars programs and interested in their types.

For a test, for example:

#include <stdio.h>

int i=0;
int main(){
    printf("lala %d \n",i);
    return 0;
}

      

What I get in the output:

Globals: 
i Type: 14  //14 ==> POINTER TYPE ID !
StackLock: Stack1 
Function Argument: i32* @i

      

My code:

for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) {
            std::cout << I->getName().str() << " Type: "<< I->getType()->getTypeID() << std::endl;

            if (I->getType()->isPointerTy() ) {
                std::string o1;
                {
                    raw_string_ostream os1(o1);
                    I->printAsOperand(os1, true);
                }
                char* stackLoc = new char[50];
                sprintf(stackLoc, "Stack%d", StackCounter++);
                errs() << "StackLock: " << stackLoc << "\n";
                errs() << "Function Argument: " << o1 << "\n";
            }

        }

      

What does it mean to set everything as pointers? Is there a way to take the "real" type? for example in my example: to get the Integer variable for the i variable.

+3


source to share


1 answer


According to the LLVM IR Reference , globals define areas of memory allocated at compile time instead of run time, and they must be initialized.

As SSA values, global variables define the pointer values ​​that are found in (i.e. dominate) all the basic blocks in the program. Global variables always define a pointer to their "content", because they describe a region of memory, and all memory objects in LLVM are accessed through pointers.



To get the actual type of your global variable, since the internal type of LLVM IR is a pointer, Type::getContainedType(int)

or Type::getPointerElementType()

can be used to get the type of a pointer of a pointer, since the type of a pointer is a derived type.

+1


source







All Articles