From c94fe21a56f9e4e5ebbe99252a25636c36392509 Mon Sep 17 00:00:00 2001 From: Duncan Sands Date: Fri, 5 Sep 2008 21:43:04 +0000 Subject: [PATCH] Delete the removeCallEdgeTo callgraph method, because it does not maintain a correct list of callsites. I discovered (see following commit) that the inliner will create a wrong callgraph if it is fed a callgraph with correct edges but incorrect callsites. These were created by Prune-EH, and while it wasn't done via removeCallEdgeTo, it could have been done via removeCallEdgeTo, which is an accident waiting to happen. Use removeCallEdgeFor instead. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@55859 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Analysis/CallGraph.h | 7 +------ lib/Analysis/IPA/CallGraph.cpp | 10 ---------- lib/Transforms/IPO/PruneEH.cpp | 11 ++++------- lib/Transforms/Utils/InlineFunction.cpp | 13 +++++++------ 4 files changed, 12 insertions(+), 29 deletions(-) diff --git a/include/llvm/Analysis/CallGraph.h b/include/llvm/Analysis/CallGraph.h index 5fd0bdc8a04..a04c7757550 100644 --- a/include/llvm/Analysis/CallGraph.h +++ b/include/llvm/Analysis/CallGraph.h @@ -214,17 +214,12 @@ public: CalledFunctions.clear(); } - /// addCalledFunction add a function to the list of functions called by this + /// addCalledFunction - Add a function to the list of functions called by this /// one. void addCalledFunction(CallSite CS, CallGraphNode *M) { CalledFunctions.push_back(std::make_pair(CS, M)); } - /// removeCallEdgeTo - This method removes a *single* edge to the specified - /// callee function. Note that this method takes linear time, so it should be - /// used sparingly. - void removeCallEdgeTo(CallGraphNode *Callee); - /// removeCallEdgeFor - This method removes the edge in the node for the /// specified call site. Note that this method takes linear time, so it /// should be used sparingly. diff --git a/lib/Analysis/IPA/CallGraph.cpp b/lib/Analysis/IPA/CallGraph.cpp index 5616ee9a4b0..4a31295a72b 100644 --- a/lib/Analysis/IPA/CallGraph.cpp +++ b/lib/Analysis/IPA/CallGraph.cpp @@ -282,16 +282,6 @@ void CallGraphNode::print(std::ostream &OS) const { void CallGraphNode::dump() const { print(cerr); } -void CallGraphNode::removeCallEdgeTo(CallGraphNode *Callee) { - for (unsigned i = CalledFunctions.size(); ; --i) { - assert(i && "Cannot find callee to remove!"); - if (CalledFunctions[i-1].second == Callee) { - CalledFunctions.erase(CalledFunctions.begin()+i-1); - return; - } - } -} - /// removeCallEdgeFor - This method removes the edge in the node for the /// specified call site. Note that this method takes linear time, so it /// should be used sparingly. diff --git a/lib/Transforms/IPO/PruneEH.cpp b/lib/Transforms/IPO/PruneEH.cpp index 8fb904b1ea5..94d6331dddc 100644 --- a/lib/Transforms/IPO/PruneEH.cpp +++ b/lib/Transforms/IPO/PruneEH.cpp @@ -222,13 +222,10 @@ void PruneEH::DeleteBasicBlock(BasicBlock *BB) { CallGraphNode *CGN = CG[BB->getParent()]; for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; ) { --I; - if (CallInst *CI = dyn_cast(I)) { - if (Function *Callee = CI->getCalledFunction()) - CGN->removeCallEdgeTo(CG[Callee]); - } else if (InvokeInst *II = dyn_cast(I)) { - if (Function *Callee = II->getCalledFunction()) - CGN->removeCallEdgeTo(CG[Callee]); - } + if (CallInst *CI = dyn_cast(I)) + CGN->removeCallEdgeFor(CI); + else if (InvokeInst *II = dyn_cast(I)) + CGN->removeCallEdgeFor(II); if (!I->use_empty()) I->replaceAllUsesWith(UndefValue::get(I->getType())); } diff --git a/lib/Transforms/Utils/InlineFunction.cpp b/lib/Transforms/Utils/InlineFunction.cpp index cb9c28dcabb..b0e70bb89ad 100644 --- a/lib/Transforms/Utils/InlineFunction.cpp +++ b/lib/Transforms/Utils/InlineFunction.cpp @@ -143,16 +143,18 @@ static void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock, /// UpdateCallGraphAfterInlining - Once we have cloned code over from a callee /// into the caller, update the specified callgraph to reflect the changes we /// made. Note that it's possible that not all code was copied over, so only -/// some edges of the callgraph will be remain. -static void UpdateCallGraphAfterInlining(const Function *Caller, - const Function *Callee, +/// some edges of the callgraph may remain. +static void UpdateCallGraphAfterInlining(CallSite CS, Function::iterator FirstNewBlock, DenseMap &ValueMap, CallGraph &CG) { + const Function *Caller = CS.getInstruction()->getParent()->getParent(); + const Function *Callee = CS.getCalledFunction(); + // Update the call graph by deleting the edge from Callee to Caller CallGraphNode *CalleeNode = CG[Callee]; CallGraphNode *CallerNode = CG[Caller]; - CallerNode->removeCallEdgeTo(CalleeNode); + CallerNode->removeCallEdgeFor(CS); // Since we inlined some uninlined call sites in the callee into the caller, // add edges from the caller to all of the callees of the callee. @@ -302,8 +304,7 @@ bool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD) { // Update the callgraph if requested. if (CG) - UpdateCallGraphAfterInlining(Caller, CalledFunc, FirstNewBlock, ValueMap, - *CG); + UpdateCallGraphAfterInlining(CS, FirstNewBlock, ValueMap, *CG); } // If there are any alloca instructions in the block that used to be the entry