c – 一切都是LLVM IR中的指针吗?

我遍历程序的全局变量并且对它们的类​​型感兴趣.

对于测试,例如:

#include <stdio.h>

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

我得到的输出是:

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

我的代码:

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";
            }

        }

将所有内容设置为指针的含义是什么?
有没有办法采取’真实’类型?例如,在我的例子中:获取i变量的Integer Type.

最佳答案 根据
LLVM IR Reference,全局变量定义在编译时而不是运行时分配的内存区域,并且必须对它们进行初始化.

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

要获取全局变量的实际类型,由于全局变量的LLVM IR内部类型是指针,因此可以使用Type :: getContainedType(int)或Type :: getPointerElementType()来获取指针的指针类型,因为指针type是派生类型.

点赞