删除llvm中的一系列指令

时间:2017-06-12 16:07:12

标签: llvm instructions

我正在尝试删除一系列指令(指定在[startIns,endIns之间))。 endIns可能与开始时不在同一个基本块中。

尝试删除最后一条指令时,我一直收到以下错误 - 引用llvm :: ilist_iterator,false,false> :: operator *()const [OptionsT = llvm :: ilist_detail :: node_options,IsReverse = false,IsConst = false]:断言`!NodePtr-> isKnownSentinel() '失败。

以下是我的C ++代码 -

//删除[start,end)

之间的所有指令

// startInst在下面的IR中为ConcurrentHashMap。"

// endInst在下面的IR中为"%res = alloca i8 "

"%resclone0 = alloca i10"

以下是相关的IR

void deleteAllInstructionsInRange(Instruction* startInst,Instruction* endInst)
    {
        BasicBlock::iterator it(startInst);
        BasicBlock::iterator it_end(endInst);

        Instruction* currentInst ;

        while(it != it_end)
        {
            currentInst = &*it;

            ++it;


            if (!currentInst->use_empty())
            {   
                currentInst->replaceAllUsesWith(UndefValue::get(currentInst->getType()));
            }

            currentInst->eraseFromParent();

        }



    }

任何帮助将不胜感激。

感谢。

2 个答案:

答案 0 :(得分:0)

首先看看this答案,了解isKnownSentinel()的含义。

我的猜测是,因为你在迭代它时修改基本块,这是不安全的,你得到错误。

答案 1 :(得分:0)

我的猜测是ret终结者并且删除它会使IR无效。 引用this question

  

每个基本块必须以终结符结束。

我建议用非语言跳转替换终止符指令到下一个块(未测试):

if (currentInst->isTerminator()) {
    // works only if block has only one successor
    currentInst->replaceAllUsesWith(
        builder->CreateBr(
            builder->GetInsertBlock()->getSingleSuccessor())); 
}