我定义了int a = 5
;在源代码中,我将源代码转换为LLVM IR:
%a = alloca i32, align 4
store i32 5, i32* %a, align 4
我想通过写pass插入int b = a;
。我把int a=5; int b=a
编译到LLVM IR中,它先加载"a",然后存储它。我还检查了doxygen,其中LoadInst是LoadInst (Value *Ptr, const Twine &NameStr, Instruction *InsertBefore)
。但是,我不知道如何获得"a"的Value
。
如何获取变量值?
在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
这对应于两个AllocaInst
s、两个StoreInst
s和一个LoadInst
,如下所示
警告:前方有未测试/未编译的伪代码
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中的值,这要归功于精心设计的继承层次结构。