llvm pass:如何使用现有变量值插入变量
问题描述:
我定义了int a = 5
;在源代码中,我变换源LLVM IR:llvm pass:如何使用现有变量值插入变量
%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,其中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,2个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归功于精心设计的继承层次的价值。
对不起,我想你误解了我的意思。我可以用'llc -march'得到类似的答案。我应该如何将IR问题转换为IR问题?由于在IR代码中存在alloca和store inst,我应该怎么做才能获得“a_alloca”? – winter333 2014-10-13 01:31:26
我不明白你的句子的英文。你想将“a”变量的名称改为“a_alloca”吗?如果是这样,请修改'AllocaInst'构造函数的'const Twine&Name ='“'参数 – 2014-10-13 07:21:59