mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-10-02 21:17:17 +00:00
Fix an incredibly nasty iterator invalidation problem. I am too spoiled by ilists :)
Eventually it would be nice if CallGraph maintained an ilist of CallGraphNode's instead of a vector of pointers to them, but today is not that day. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@13100 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -106,14 +106,6 @@ bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) {
|
|||||||
E = CalleeNode->end(); I != E; ++I)
|
E = CalleeNode->end(); I != E; ++I)
|
||||||
CallerNode->addCalledFunction(*I);
|
CallerNode->addCalledFunction(*I);
|
||||||
|
|
||||||
// If the only remaining use of the function is a dead constant
|
|
||||||
// pointer ref, remove it.
|
|
||||||
if (Callee->hasOneUse())
|
|
||||||
if (ConstantPointerRef *CPR =
|
|
||||||
dyn_cast<ConstantPointerRef>(Callee->use_back()))
|
|
||||||
if (CPR->use_empty())
|
|
||||||
CPR->destroyConstant();
|
|
||||||
|
|
||||||
// If we inlined the last possible call site to the function,
|
// If we inlined the last possible call site to the function,
|
||||||
// delete the function body now.
|
// delete the function body now.
|
||||||
if (Callee->use_empty() && Callee != Caller &&
|
if (Callee->use_empty() && Callee != Caller &&
|
||||||
@@ -142,27 +134,55 @@ bool Inliner::runOnSCC(const std::vector<CallGraphNode*> &SCC) {
|
|||||||
// doFinalization - Remove now-dead linkonce functions at the end of
|
// doFinalization - Remove now-dead linkonce functions at the end of
|
||||||
// processing to avoid breaking the SCC traversal.
|
// processing to avoid breaking the SCC traversal.
|
||||||
bool Inliner::doFinalization(CallGraph &CG) {
|
bool Inliner::doFinalization(CallGraph &CG) {
|
||||||
bool Changed = false;
|
std::set<CallGraphNode*> FunctionsToRemove;
|
||||||
for (CallGraph::iterator I = CG.begin(), E = CG.end(); I != E; ) {
|
|
||||||
CallGraphNode *CGN = (I++)->second;
|
// Scan for all of the functions, looking for ones that should now be removed
|
||||||
|
// from the program. Insert the dead ones in the FunctionsToRemove set.
|
||||||
|
for (CallGraph::iterator I = CG.begin(), E = CG.end(); I != E; ++I) {
|
||||||
|
CallGraphNode *CGN = I->second;
|
||||||
Function *F = CGN ? CGN->getFunction() : 0;
|
Function *F = CGN ? CGN->getFunction() : 0;
|
||||||
|
|
||||||
|
// If the only remaining use of the function is a dead constant
|
||||||
|
// pointer ref, remove it.
|
||||||
|
if (F && F->hasOneUse())
|
||||||
|
if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(F->use_back()))
|
||||||
|
if (CPR->use_empty()) {
|
||||||
|
CPR->destroyConstant();
|
||||||
|
if (F->hasInternalLinkage()) {
|
||||||
|
// There *MAY* be an edge from the external call node to this
|
||||||
|
// function. If so, remove it.
|
||||||
|
CallGraphNode *EN = CG.getExternalCallingNode();
|
||||||
|
CallGraphNode::iterator I = std::find(EN->begin(), EN->end(), CGN);
|
||||||
|
if (I != EN->end()) EN->removeCallEdgeTo(CGN);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (F && (F->hasLinkOnceLinkage() || F->hasInternalLinkage()) &&
|
if (F && (F->hasLinkOnceLinkage() || F->hasInternalLinkage()) &&
|
||||||
F->use_empty()) {
|
F->use_empty()) {
|
||||||
// Remove any call graph edges from the callee to its callees.
|
// Remove any call graph edges from the function to its callees.
|
||||||
while (CGN->begin() != CGN->end())
|
while (CGN->begin() != CGN->end())
|
||||||
CGN->removeCallEdgeTo(*(CGN->end()-1));
|
CGN->removeCallEdgeTo(*(CGN->end()-1));
|
||||||
|
|
||||||
// If the function has external linkage (basically if it's a
|
// If the function has external linkage (basically if it's a linkonce
|
||||||
// linkonce function) remove the edge from the external node to the
|
// function) remove the edge from the external node to the callee node.
|
||||||
// callee node.
|
|
||||||
if (!F->hasInternalLinkage())
|
if (!F->hasInternalLinkage())
|
||||||
CG.getExternalCallingNode()->removeCallEdgeTo(CGN);
|
CG.getExternalCallingNode()->removeCallEdgeTo(CGN);
|
||||||
|
|
||||||
// Removing the node for callee from the call graph and delete it.
|
// Removing the node for callee from the call graph and delete it.
|
||||||
delete CG.removeFunctionFromModule(CGN);
|
FunctionsToRemove.insert(CGN);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now that we know which functions to delete, do so. We didn't want to do
|
||||||
|
// this inline, because that would invalidate our CallGraph::iterator
|
||||||
|
// objects. :(
|
||||||
|
bool Changed = false;
|
||||||
|
for (std::set<CallGraphNode*>::iterator I = FunctionsToRemove.begin(),
|
||||||
|
E = FunctionsToRemove.end(); I != E; ++I) {
|
||||||
|
delete CG.removeFunctionFromModule(*I);
|
||||||
++NumDeleted;
|
++NumDeleted;
|
||||||
Changed = true;
|
Changed = true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return Changed;
|
return Changed;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user