在 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和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 中的值。