创建一个新的存储指令 LLVM
Creating a new Store Instruction LLVM
我正在使用 LLVM IR 代码。我想创建一个新的 store
指令(例如:store i32 %add, i32* %temp1, align 4
),我需要在特定指令之后插入它,比如在 add
指令之后。我的意图是,加法运算的结果(某个指针)存储在 %add
中,我需要在 临时变量 中保留相同的副本 [=14] =].
为此,我首先创建了一个名为 temp1
的变量 (%temp1 = alloca i32, align 4
)。现在我想存储加法指令 (%add = add nsw i32 %0, %1
) 的结果,即 %add
到 temp1
。那么最终的存储指令将是这样的:store i32 %add, i32* %temp1, align 4
。如何做到这一点?
对一些例子有帮助吗?
为了创建 %temp1 = alloca i32, align 4
指令,我使用了以下语句:
AllocaInst* pa = new AllocaInst(llvm::Type::getInt32Ty(getGlobalContext()), 0, 4,"temp1");
用于创建和插入新的 store
指令:
StoreInst *str = new StoreInst(i, pa); // i -> current instruction pointer which represents %add ( source of store instruction ), pa -> destination. i.e., temp1
BB->getInstList().insert(ib, str); // ib -> instruction address before which you want to insert this store instruction
一些注意事项:
- 您应该使用 IRBuilder 来创建 IR。这要容易得多,并且在 Kaleidoscope 教程和 clang 本身中都有大量示例可供使用。
- 我不确定你为什么要创建额外的分配。一般来说,除非你有一个实际的局部变量来存储到一个值就足以保存结果。您要存储什么?为什么?
- 如果您确实需要为局部变量分配额外的分配,那么您应该在函数的入口块中创建它,否则您将在函数中间创建动态分配。
我正在使用 LLVM IR 代码。我想创建一个新的 store
指令(例如:store i32 %add, i32* %temp1, align 4
),我需要在特定指令之后插入它,比如在 add
指令之后。我的意图是,加法运算的结果(某个指针)存储在 %add
中,我需要在 临时变量 中保留相同的副本 [=14] =].
为此,我首先创建了一个名为 temp1
的变量 (%temp1 = alloca i32, align 4
)。现在我想存储加法指令 (%add = add nsw i32 %0, %1
) 的结果,即 %add
到 temp1
。那么最终的存储指令将是这样的:store i32 %add, i32* %temp1, align 4
。如何做到这一点?
对一些例子有帮助吗?
为了创建 %temp1 = alloca i32, align 4
指令,我使用了以下语句:
AllocaInst* pa = new AllocaInst(llvm::Type::getInt32Ty(getGlobalContext()), 0, 4,"temp1");
用于创建和插入新的 store
指令:
StoreInst *str = new StoreInst(i, pa); // i -> current instruction pointer which represents %add ( source of store instruction ), pa -> destination. i.e., temp1
BB->getInstList().insert(ib, str); // ib -> instruction address before which you want to insert this store instruction
一些注意事项:
- 您应该使用 IRBuilder 来创建 IR。这要容易得多,并且在 Kaleidoscope 教程和 clang 本身中都有大量示例可供使用。
- 我不确定你为什么要创建额外的分配。一般来说,除非你有一个实际的局部变量来存储到一个值就足以保存结果。您要存储什么?为什么?
- 如果您确实需要为局部变量分配额外的分配,那么您应该在函数的入口块中创建它,否则您将在函数中间创建动态分配。