2

我定义了int a = 5;在源代码中,我将源代码转换为 LLVM IR:

%a = alloca i32, align 4
store i32 5, i32* %a, align 4

我想int b = a;通过写通行证来插入。我编译int a=5; int b=a成 LLVM IR,它首先加载“a”,然后存储它。我还检查了 doxygen,其中 LoadInstLoadInst (Value *Ptr, const Twine &NameStr, Instruction *InsertBefore)仍然存在,我不知道如何获得Value“a”。

如何获取变量值?

4

1 回答 1

3

在 LLVM IR 中,序列

int a = 5;
int b = a;

没有任何优化,被翻译为

%a = alloca i32, align 4
%b = alloca i32, align 4
store i32 5, i32* %a, align 4
%0 = load i32* %a, align 4
store i32 %0, i32* %b, align 4

这对应两个AllocaInsts,两个StoreInsts和aLoadInst如下

警告:前面未经测试/未编译的伪代码

ConstantInt* const_int_5 = ConstantInt::get(llvmContext, APInt(32, StringRef("5"), 10));

AllocaInst* a_alloc = new AllocaInst(IntegerType::get(llvmContext, 32), "a");
AllocaInst* b_alloc = new AllocaInst(IntegerType::get(llvmContext, 32), "b");
StoreInst* store_5 = new StoreInst(const_int_5, a_alloc, false);
LoadInst* load_from_a = new LoadInst(a_alloc, "", false);
StoreInst* store_b = new StoreInst(load_from_a, b_alloc, false);

您可能会感到困惑,因为由于精心设计的继承层次结构,指令是 LLVM API 中的值。

于 2014-10-12T13:44:17.190 回答