rtroulak
rtroulak

Reputation: 469

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

I would like to replace all uses of an instruction, which is a function call that I want to delete, with an Undef value.

First, I declare my undef value like this

UndefValue *undefval;

and then I try to replace all the uses of my instruction

currentInst->replaceAllUsesWith(undefval);

currentInst is an

Instruction* currentInst; 

value which refers to my current instruction. This causes LLVM to produce the following error ans assertion:

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)

Any ideas what's wrong? I have wrong declare of undef value?

Upvotes: 1

Views: 3035

Answers (1)

Ismail Badawi
Ismail Badawi

Reputation: 37187

When you write this:

UndefValue *undefval;

You're just declaring a pointer of type UndefValue, and not storing anything in it. Instead, you need to get an instance of UndefValue for the type of the instruction you want to replace, using the UndefValue::get factory function. Something like this:

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

Upvotes: 3

Related Questions