[LLVMdev] Remove functions from module

Andreas Weber weber.andreas.b1 at web.de
Thu Dec 18 20:32:49 PST 2014


Hello,

I have a Problem I just can not understand. I want to reduce the Module to
the Subgraph which has a given function as root.

I first calculate a list of functions I want to keep:

        //Find all called functions from this CallNode recursively
        std::vector<llvm::Function*> calledFunctionsFromEntryPointList;
        calcCalledFunctionsFromEntryPoint(entry,
&calledFunctionsFromEntryPointList);

Then I am dropping all calls to CallGraphNodes not in the list and try to
delete them afterwards.

        //Drop calls //TODO
        for (llvm::CallGraph::iterator G = callGraph.begin(), end =
callGraph.end(); G != end; ++G) {
            if(G->second->getFunction())
            {

if(std::find(calledFunctionsFromEntryPointList.begin(),calledFunctionsFromEntryPointList.end(),G->second->getFunction())
== calledFunctionsFromEntryPointList.end())
                {
                    std::cout << "Function:" <<
G->second->getFunction()->getName().str() << " not in call List dropping
called functions\n";
                    G->second->removeAllCalledFunctions();
                }
            }
            else {
                for (llvm::CallGraphNode::const_iterator CI =
G->second->begin(), CE = G->second->end(); CI != CE; ++CI) {

if(std::find(calledFunctionsFromEntryPointList.begin(),calledFunctionsFromEntryPointList.end(),CI->second->getFunction())
== calledFunctionsFromEntryPointList.end())
                    {
                        if(CI->second->getFunction()){
                            G->second->removeAnyCallEdgeTo(CI->second);
                        }
                    }
                }
            }
        }

        //TODO Somehow there is a Bug that sometimes a Function after
dropping calls is no longer a function and does not get deleted

        //Drop not called functions //TODO
        for (llvm::CallGraph::iterator G = callGraph.begin(), end =
callGraph.end(); G != end; ++G) {
            if(G->second->getFunction())
            {

if(std::find(calledFunctionsFromEntryPointList.begin(),calledFunctionsFromEntryPointList.end(),G->second->getFunction())
== calledFunctionsFromEntryPointList.end())
                {
                    std::cout << "Function:" <<
G->second->getFunction()->getName().str() << " not in call List deleting
it\n";
                    G->second->getFunction()->dropAllReferences();
                    callGraph.removeFunctionFromModule(G->second);
                }
            }
        }

The Problem now is that somehow functions become something else and i can
not delete them.

However they are still in the list as they can be found with the iterator
and addressed with the Callgraph node.

      for (llvm::Module::iterator F = callGraph.getModule().begin(), end =
callGraph.getModule().end(); F != end; ++F) {

if(std::find(calledFunctionsFromEntryPointList.begin(),calledFunctionsFromEntryPointList.end(),F)
== calledFunctionsFromEntryPointList.end()){
                std::cout << "Found Bug Function: " <<
callGraph[F]->getFunction()->getName().str() << std::endl;
                callGraph[F]->getFunction()->dropAllReferences();
              callGraph.getModule().getFunctionList().remove(F);
            }
        }

If I however try to delete it from the Callgraph instead just from the List
with:

callGraph.removeFunctionFromModule(callGraph[F]);

I get a Segmentation fault.


And to make it even less understandable if I run the same code again I find
new function not found in the first round.


      for (llvm::Module::iterator F = callGraph.getModule().begin(), end =
callGraph.getModule().end(); F != end; ++F) {

if(std::find(calledFunctionsFromEntryPointList.begin(),calledFunctionsFromEntryPointList.end(),F)
== calledFunctionsFromEntryPointList.end()){
                std::cout << "Found Bug Function: " <<
callGraph[F]->getFunction()->getName().str() << std::endl;
                callGraph[F]->getFunction()->dropAllReferences();
              callGraph.getModule().getFunctionList().remove(F);
            }
        }

Any help is appreciated.

Many Thanks

Andreas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20141219/aee108cf/attachment.html>


More information about the llvm-dev mailing list