在 llvm 中用 Undef 值替换对删除指令的所有使用?

Replace all uses of an instruction to delete with an Undef value at llvm?

我想用一个 Undef 值替换一条指令的所有使用,这是一个我想删除的函数调用。

首先,我像这样声明我的 undef 值

UndefValue *undefval;

然后我尝试替换我指令的所有用途

currentInst->replaceAllUsesWith(undefval);

currentInst 是一个

Instruction* currentInst; 

value 指的是我当前的指令。这导致 LLVM 产生以下错误和断言:

opt: /home/troulakis/Documents/LLVM_Project/llvm/llvm/lib/IR/Value.cpp:332: void llvm::Value::replaceAllUsesWith(llvm::Value *): Assertion `New->getType() == getType() && "replaceAllUses of value with new value of different type!"' failed.

0 0x160e678 llvm::sys::PrintStackTrace(_IO_FILE*) (/usr/local/bin/opt+0x160e678) 1 0x160fbdb (/usr/local/bin/opt+0x160fbdb) 2 0x7f7752596340 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x10340) 3 0x7f77515aacc9 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x36cc9) 4 0x7f77515ae0d8 abort (/lib/x86_64-linux-gnu/libc.so.6+0x3a0d8) 5 0x7f77515a3b86 (/lib/x86_64-linux-gnu/libc.so.6+0x2fb86) 6 0x7f77515a3c32 (/lib/x86_64-linux-gnu/libc.so.6+0x2fc32) 7 0x15be04f (/usr/local/bin/opt+0x15be04f) 8 0x7f775136fda7 (anonymous namespace)::MyPass::runOnFunction(llvm::Function&) (../../../Release+Asserts/lib/PassRAF.so+0x6da7) 9 0x15a1ab4 llvm::FPPassManager::runOnFunction(llvm::Function&) (/usr/local/bin/opt+0x15a1ab4) 10 0x15a1d3b llvm::FPPassManager::runOnModule(llvm::Module&) (/usr/local/bin/opt+0x15a1d3b) 11 0x15a22d7 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/usr/local/bin/opt+0x15a22d7) 12 0x5af6db main (/usr/local/bin/opt+0x5af6db) 13 0x7f7751595ec5 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21ec5) 14 0x59f559 _start (/usr/local/bin/opt+0x59f559)

Stack dump:

  1. Program arguments: opt -load ../../../Release+Asserts/lib/PassRAF.so -time-passes -instnamer -PassRAF

  2. Running pass 'Function Pass Manager' on module ''.

  3. Running pass 'R A F' on function '@main'

./PassRAF: line 15: 7227 Aborted (core dumped)

有什么问题吗?我对 undef 值的声明有误?

当你这样写的时候:

UndefValue *undefval;

您只是声明了一个 UndefValue 类型的指针,并没有在其中存储任何内容。相反,您需要使用 UndefValue::get 工厂函数为要替换的指令类型获取 UndefValue 的实例。像这样:

currentInst->replaceAllUsesWith(UndefValue::get(currentInst->getType())