<div dir="ltr"><div><div>Hello,<br><br>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.<br><br></div>I first calculate a list of functions I want to keep:<br><br> //Find all called functions from this CallNode recursively<br> std::vector<llvm::Function*> calledFunctionsFromEntryPointList;<br> calcCalledFunctionsFromEntryPoint(entry, &calledFunctionsFromEntryPointList);<br><br></div>Then I am dropping all calls to CallGraphNodes not in the list and try to delete them afterwards.<br><div> <br> //Drop calls //TODO<br> for (llvm::CallGraph::iterator G = callGraph.begin(), end = callGraph.end(); G != end; ++G) {<br> if(G->second->getFunction())<br> {<br> if(std::find(calledFunctionsFromEntryPointList.begin(),calledFunctionsFromEntryPointList.end(),G->second->getFunction()) == calledFunctionsFromEntryPointList.end())<br> {<br> std::cout << "Function:" << G->second->getFunction()->getName().str() << " not in call List dropping called functions\n";<br> G->second->removeAllCalledFunctions();<br> }<br> }<br> else {<br> for (llvm::CallGraphNode::const_iterator CI = G->second->begin(), CE = G->second->end(); CI != CE; ++CI) {<br> if(std::find(calledFunctionsFromEntryPointList.begin(),calledFunctionsFromEntryPointList.end(),CI->second->getFunction()) == calledFunctionsFromEntryPointList.end())<br> {<br> if(CI->second->getFunction()){<br> G->second->removeAnyCallEdgeTo(CI->second);<br> }<br> }<br> }<br> }<br> }<br><br> //TODO Somehow there is a Bug that sometimes a Function after dropping calls is no longer a function and does not get deleted<br><br> //Drop not called functions //TODO<br> for (llvm::CallGraph::iterator G = callGraph.begin(), end = callGraph.end(); G != end; ++G) {<br> if(G->second->getFunction())<br> {<br> if(std::find(calledFunctionsFromEntryPointList.begin(),calledFunctionsFromEntryPointList.end(),G->second->getFunction()) == calledFunctionsFromEntryPointList.end())<br> {<br> std::cout << "Function:" << G->second->getFunction()->getName().str() << " not in call List deleting it\n";<br> G->second->getFunction()->dropAllReferences();<br> callGraph.removeFunctionFromModule(G->second);<br> }<br> }<br> }<br><br></div><div>The Problem now is that somehow functions become something else and i can not delete them.<br><br></div><div>However they are still in the list as they can be found with the iterator and addressed with the Callgraph node. <br><br> for (llvm::Module::iterator F = callGraph.getModule().begin(), end = callGraph.getModule().end(); F != end; ++F) {<br> if(std::find(calledFunctionsFromEntryPointList.begin(),calledFunctionsFromEntryPointList.end(),F) == calledFunctionsFromEntryPointList.end()){<br> std::cout << "Found Bug Function: " << callGraph[F]->getFunction()->getName().str() << std::endl;<br> callGraph[F]->getFunction()->dropAllReferences();<br> callGraph.getModule().getFunctionList().remove(F);<br> }<br> }<br><br></div><div>If I however try to delete it from the Callgraph instead just from the List with:<br><br>callGraph.removeFunctionFromModule(callGraph[F]); <br><br></div><div>I get a Segmentation fault.<br></div><div><br></div><div><br></div><div>And to make it even less understandable if I run the same code again I find new function not found in the first round.<br><br><br> for (llvm::Module::iterator F = callGraph.getModule().begin(), end = callGraph.getModule().end(); F != end; ++F) {<br>
if(std::find(calledFunctionsFromEntryPointList.begin(),calledFunctionsFromEntryPointList.end(),F)
== calledFunctionsFromEntryPointList.end()){<br>
std::cout << "Found Bug Function: " <<
callGraph[F]->getFunction()->getName().str() << std::endl;<br> callGraph[F]->getFunction()->dropAllReferences();<br> callGraph.getModule().getFunctionList().remove(F);<br> }<br> }<br></div><div><br></div><div>Any help is appreciated.<br><br></div><div>Many Thanks<br><br></div><div>Andreas<br></div><div><br></div></div>