llvm pass:如何使用现有变量值插入变量 [英] llvm pass: How to insert a variable using existing variable value

查看:126
本文介绍了llvm pass:如何使用现有变量值插入变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I defined int a = 5; in the source code, and I transform the source to 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,其中LoadInst为LoadInst (Value *Ptr, const Twine &NameStr, Instruction *InsertBefore)仍然,我不知道如何获取"a"的Value.

I want to insert int b = a; by writing a pass. I compile int a=5; int b=a into LLVM IR, it load "a" first, then store it. I also checked the doxygen, in which the LoadInst is LoadInst (Value *Ptr, const Twine &NameStr, Instruction *InsertBefore) Still, I don't know how to get the Value of "a".

如何获取变量值?

推荐答案

在LLVM IR中,序列

In LLVM IR the sequence

int a = 5;
int b = a;

未经任何优化,将翻译为

without any optimization, is translated as

%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,两个StoreInst和一个LoadInst

This corresponds to two AllocaInsts, two StoreInsts and a LoadInst as follows

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

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中的值.

You're probably being confused since the instruction is the value in LLVM API thanks to a well-designed inheritance hierarchy.

这篇关于llvm pass:如何使用现有变量值插入变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆