简体   繁体   English

在 LLVM 中插入指令

[英]Insert instructions in LLVM

If I am trying to create an instruction and insert at the start of a function, will this be the right way as I could not see the instruction inserted when I used opt to load the .so file and process the .ll file.如果我试图创建一条指令并在函数的开头插入,这是否是正确的方法,因为当我使用 opt 加载 .so 文件并处理 .ll 文件时我看不到插入的指令。

if (auto* op = dyn_cast<Instruction>(&I))
{
    if(prepend_first == false)
    {
        llvm::LLVMContext& context = I.getContext();
        Value* lhs = ConstantInt::get(Type::getInt32Ty(context), 4);
        Value* rhs = ConstantInt::get(Type::getInt32Ty(context), 6);

        IRBuilder<> builder(op);
        builder.CreateMul(lhs, rhs);
        builder.SetInsertPoint(&I);

        prepend_first = true;
    }
}

EDIT (27 Nov 2020) This is the entire pass, which is based on other articles as well.编辑(2020 年 11 月 27 日)这是整个通行证,也基于其他文章。

using namespace llvm;

namespace
{
    struct customllvm : public PassInfoMixin<customllvm>
    {
        llvm:PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
        {
            for(BasicBlock &BB : F)
            {
                bool prepend_first = false;

                for(Instruction &I : BB)
                {
                    if(auto *op = dyn_cast<Instruction>(&I))
                    {
                        if(prepend_first != true)
                        {
                            llvm::LLVMContext& context = I.getContext();
                            Value* lhs = ConstantInt::get(Type::getInt32Ty(context), 4);
                            Value* rhs = ConstantInt::get(Type::getInt32Ty(context), 6);

                            IRBuilder<> builder(op);
                            builder.CreateMul(lhs, rhs);
                            builder.SetInsertPoint(&I);

                            prepend_first = true;
                        }
                    }
                }
            }
        }
    }
}

You need to set the insertion point first.您需要先设置插入点。 And only then create instruction.然后才创建指令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM