[LLVMdev] Remove function from module

Dmitry N. Mikushin maemarcus at gmail.com
Sat Apr 21 13:03:18 PDT 2012


Hi Михаил,

You can also use GVExtractionPass:

	if (!strcmp(argv[1], "pass"))
	{
		PassManager manager;
		manager.add(new TargetData(m.get()));
		
		// Delete functions specified in list of functions.
		vector<GlobalValue*> functions;
		functions.push_back(m.get()->getFunction("f1"));
		functions.push_back(m.get()->getFunction("f2"));
		manager.add(createGVExtractionPass(functions, true));

		// Delete unreachable globals.
		manager.add(createGlobalDCEPass());
		
		// Remove dead debug info.
		manager.add(createStripDeadDebugInfoPass());
		
		// Remove dead func decls.
		manager.add(createStripDeadPrototypesPass());

		manager.run(*m.get());
	}

	if (!strcmp(argv[1], "manual"))
	{
		Function* f1 = m.get()->getFunction("f1");
		Function* f2 = m.get()->getFunction("f2");
		
		f1->replaceAllUsesWith(UndefValue::get((Type*)f1->getType()));
		f2->replaceAllUsesWith(UndefValue::get((Type*)f2->getType()));

		f1->eraseFromParent();
		f2->eraseFromParent();
	}

- D.

2012/4/21 Nick Lewycky <nicholas at mxc.ca>:
> Михаил wrote:
>> How correctly remove function from module?
>> For example:
>>
>> int f1(int x) {
>> ...
>> a = f2(smth);
>> ...
>> }
>> int f2 (int y) {
>> ...
>> b = f1(smth);
>> ...
>> }
>>
>> I need delete from module both f1 and f2. They haven't uses in other
>> part of module, but I can't delete them with eraseFromParent, because
>> they are use each other.
>
> Call X->replaceAllUsesWith(UndefValue::get(X->getType)) before calling
> X->eraseFromParent().
>
> Nick
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
-------------- next part --------------
A non-text attachment was scrubbed...
Name: makefile
Type: application/octet-stream
Size: 388 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120422/f64ca9da/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.c
Type: text/x-csrc
Size: 98 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120422/f64ca9da/attachment.c>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.ll
Type: application/octet-stream
Size: 1755 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120422/f64ca9da/attachment-0001.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: transform.cxx
Type: application/octet-stream
Size: 1875 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120422/f64ca9da/attachment-0002.obj>


More information about the llvm-dev mailing list