llvm.org GIT mirror llvm / 5095e3d
Fix some nasty callgraph dangling pointer problems in argpromotion and structretpromote. Basically, when replacing a function, they used the 'changeFunction' api which changes the entry in the function map (and steals/reuses the callgraph node). This has some interesting effects: first, the problem is that it doesn't update the "callee" edges in any callees of the function in the call graph. Second, this covers for a major problem in all the CGSCC pass stuff, which is that it is completely broken when functions are deleted if they *don't* reuse a CGN. (there is a cute little fixme about this though :). This patch changes the protocol that CGSCC passes must obey: now the CGSCC pass manager copies the SCC and preincrements its iterator to avoid passes invalidating it. This allows CGSCC passes to mutate the current SCC. However multiple passes may be run on that SCC, so if passes do this, they are now required to *update* the SCC to be current when they return. Other less interesting parts of this patch are that it makes passes update the CG more directly, eliminates changeFunction, and requires clients of replaceCallSite to specify the new callee CGN if they are changing it. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@80527 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 10 years ago
11 changed file(s) with 115 addition(s) and 91 deletion(s). Raw diff Collapse all Expand all
129129 return removeFunctionFromModule((*this)[F]);
130130 }
131131
132 /// changeFunction - This method changes the function associated with this
133 /// CallGraphNode, for use by transformations that need to change the
134 /// prototype of a Function (thus they must create a new Function and move the
135 /// old code over).
136 void changeFunction(Function *OldF, Function *NewF);
137
138132 /// getOrInsertFunction - This method is identical to calling operator[], but
139133 /// it will insert a new CallGraphNode for the specified function if one does
140134 /// not already exist.
211205 void removeAllCalledFunctions() {
212206 CalledFunctions.clear();
213207 }
208
209 /// stealCalledFunctionsFrom - Move all the callee information from N to this
210 /// node.
211 void stealCalledFunctionsFrom(CallGraphNode *N) {
212 assert(CalledFunctions.empty() &&
213 "Cannot steal callsite information if I already have some");
214 std::swap(CalledFunctions, N->CalledFunctions);
215 }
216
214217
215218 /// addCalledFunction - Add a function to the list of functions called by this
216219 /// one.
235238 /// replaceCallSite - Make the edge in the node for Old CallSite be for
236239 /// New CallSite instead. Note that this method takes linear time, so it
237240 /// should be used sparingly.
238 void replaceCallSite(CallSite Old, CallSite New);
241 void replaceCallSite(CallSite Old, CallSite New, CallGraphNode *NewCallee);
239242
240243 friend class CallGraph;
241244
4545 /// non-recursive (or only self-recursive) functions will have an SCC size of
4646 /// 1, where recursive portions of the call graph will have SCC size > 1.
4747 ///
48 virtual bool runOnSCC(const std::vector &SCC) = 0;
48 /// SCC passes that add or delete functions to the SCC are required to update
49 /// the SCC list, otherwise stale pointers may be dereferenced.
50 ///
51 virtual bool runOnSCC(std::vector &SCC) = 0;
4952
5053 /// doFinalization - This method is called after the SCC's of the program has
5154 /// been processed, allowing the pass to do final cleanup as necessary.
3939
4040 // Main run interface method, this implements the interface required by the
4141 // Pass class.
42 virtual bool runOnSCC(const std::vector &SCC);
42 virtual bool runOnSCC(std::vector &SCC);
4343
4444 // doFinalization - Remove now-dead linkonce functions at the end of
4545 // processing to avoid breaking the SCC traversal.
205205 return F;
206206 }
207207
208 // changeFunction - This method changes the function associated with this
209 // CallGraphNode, for use by transformations that need to change the prototype
210 // of a Function (thus they must create a new Function and move the old code
211 // over).
212 void CallGraph::changeFunction(Function *OldF, Function *NewF) {
213 iterator I = FunctionMap.find(OldF);
214 CallGraphNode *&New = FunctionMap[NewF];
215 assert(I != FunctionMap.end() && I->second && !New &&
216 "OldF didn't exist in CG or NewF already does!");
217 New = I->second;
218 New->F = NewF;
219 FunctionMap.erase(I);
220 }
221
222208 // getOrInsertFunction - This method is identical to calling operator[], but
223209 // it will insert a new CallGraphNode for the specified function if one does
224210 // not already exist.
232218
233219 void CallGraphNode::print(raw_ostream &OS) const {
234220 if (Function *F = getFunction())
235 OS << "Call graph node for function: '" << F->getName() <<"'\n";
221 OS << "Call graph node for function: '" << F->getName()
222 << "'<<0x" << this << ">>\n";
236223 else
237224 OS << "Call graph node <>:\n";
238225
288275 /// replaceCallSite - Make the edge in the node for Old CallSite be for
289276 /// New CallSite instead. Note that this method takes linear time, so it
290277 /// should be used sparingly.
291 void CallGraphNode::replaceCallSite(CallSite Old, CallSite New) {
278 void CallGraphNode::replaceCallSite(CallSite Old, CallSite New,
279 CallGraphNode *NewCallee) {
292280 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
293281 assert(I != CalledFunctions.end() && "Cannot find callsite to replace!");
294282 if (I->first == Old) {
295283 I->first = New;
284
285 // If the callee is changing, not just the callsite, then update it as
286 // well.
287 if (NewCallee)
288 I->second = NewCallee;
296289 return;
297290 }
298291 }
8383 CallGraph &CG = getAnalysis();
8484 bool Changed = doInitialization(CG);
8585
86 std::vector CurSCC;
87
8688 // Walk SCC
87 for (scc_iterator I = scc_begin(&CG), E = scc_end(&CG);
88 I != E; ++I) {
89
89 for (scc_iterator CGI = scc_begin(&CG), E = scc_end(&CG);
90 CGI != E;) {
91 // Copy the current SCC and increment past it so that the pass can hack
92 // on the SCC if it wants to without invalidating our iterator.
93 CurSCC = *CGI;
94 ++CGI;
95
9096 // Run all passes on current SCC
9197 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
9298 Pass *P = getContainedPass(Index);
98104
99105 StartPassTimer(P);
100106 if (CallGraphSCCPass *CGSP = dynamic_cast(P))
101 Changed |= CGSP->runOnSCC(*I); // TODO : What if CG is changed ?
107 Changed |= CGSP->runOnSCC(CurSCC);
102108 else {
103109 FPPassManager *FPP = dynamic_cast(P);
104110 assert (FPP && "Invalid CGPassManager member");
105111
106112 // Run pass P on all functions current SCC
107 std::vector &SCC = *I;
108 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
109 Function *F = SCC[i]->getFunction();
110 if (F) {
113 for (unsigned i = 0, e = CurSCC.size(); i != e; ++i) {
114 if (Function *F = CurSCC[i]->getFunction()) {
111115 dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName());
112116 Changed |= FPP->runOnFunction(*F);
113117 }
6464 CallGraphSCCPass::getAnalysisUsage(AU);
6565 }
6666
67 virtual bool runOnSCC(const std::vector &SCC);
67 virtual bool runOnSCC(std::vector &SCC);
6868 static char ID; // Pass identification, replacement for typeid
6969 explicit ArgPromotion(unsigned maxElements = 3)
7070 : CallGraphSCCPass(&ID), maxElements(maxElements) {}
7373 typedef std::vector IndicesVector;
7474
7575 private:
76 bool PromoteArguments(CallGraphNode *CGN);
76 CallGraphNode *PromoteArguments(CallGraphNode *CGN);
7777 bool isSafeToPromoteArgument(Argument *Arg, bool isByVal) const;
78 Function *DoPromotion(Function *F,
79 SmallPtrSet &ArgsToPromote,
80 SmallPtrSet &ByValArgsToTransform);
78 CallGraphNode *DoPromotion(Function *F,
79 SmallPtrSet &ArgsToPromote,
80 SmallPtrSet &ByValArgsToTransform);
8181 /// The maximum number of elements to expand, or 0 for unlimited.
8282 unsigned maxElements;
8383 };
9191 return new ArgPromotion(maxElements);
9292 }
9393
94 bool ArgPromotion::runOnSCC(const std::vector &SCC) {
94 bool ArgPromotion::runOnSCC(std::vector &SCC) {
9595 bool Changed = false, LocalChange;
9696
9797 do { // Iterate until we stop promoting from this SCC.
9898 LocalChange = false;
9999 // Attempt to promote arguments from all functions in this SCC.
100100 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
101 LocalChange |= PromoteArguments(SCC[i]);
101 if (CallGraphNode *CGN = PromoteArguments(SCC[i])) {
102 LocalChange = true;
103 SCC[i] = CGN;
104 }
102105 Changed |= LocalChange; // Remember that we changed something.
103106 } while (LocalChange);
104107
110113 /// example, all callers are direct). If safe to promote some arguments, it
111114 /// calls the DoPromotion method.
112115 ///
113 bool ArgPromotion::PromoteArguments(CallGraphNode *CGN) {
116 CallGraphNode *ArgPromotion::PromoteArguments(CallGraphNode *CGN) {
114117 Function *F = CGN->getFunction();
115118
116119 // Make sure that it is local to this module.
117 if (!F || !F->hasLocalLinkage()) return false;
120 if (!F || !F->hasLocalLinkage()) return 0;
118121
119122 // First check: see if there are any pointer arguments! If not, quick exit.
120123 SmallVector, 16> PointerArgs;
123126 I != E; ++I, ++ArgNo)
124127 if (isa(I->getType()))
125128 PointerArgs.push_back(std::pair(I, ArgNo));
126 if (PointerArgs.empty()) return false;
129 if (PointerArgs.empty()) return 0;
127130
128131 // Second check: make sure that all callers are direct callers. We can't
129132 // transform functions that have indirect callers.
130133 if (F->hasAddressTaken())
131 return false;
134 return 0;
132135
133136 // Check to see which arguments are promotable. If an argument is promotable,
134137 // add it to ArgsToPromote.
173176 }
174177
175178 // No promotable pointer arguments.
176 if (ArgsToPromote.empty() && ByValArgsToTransform.empty()) return false;
177
178 Function *NewF = DoPromotion(F, ArgsToPromote, ByValArgsToTransform);
179
180 // Update the call graph to know that the function has been transformed.
181 getAnalysis().changeFunction(F, NewF);
182 return true;
179 if (ArgsToPromote.empty() && ByValArgsToTransform.empty())
180 return 0;
181
182 return DoPromotion(F, ArgsToPromote, ByValArgsToTransform);
183183 }
184184
185185 /// IsAlwaysValidPointer - Return true if the specified pointer is always legal
468468 /// DoPromotion - This method actually performs the promotion of the specified
469469 /// arguments, and returns the new function. At this point, we know that it's
470470 /// safe to do so.
471 Function *ArgPromotion::DoPromotion(Function *F,
472 SmallPtrSet &ArgsToPromote,
471 CallGraphNode *ArgPromotion::DoPromotion(Function *F,
472 SmallPtrSet &ArgsToPromote,
473473 SmallPtrSet &ByValArgsToTransform) {
474474
475475 // Start by computing a new prototype for the function, which is the same as
607607 // Get the callgraph information that we need to update to reflect our
608608 // changes.
609609 CallGraph &CG = getAnalysis();
610
611 // Get a new callgraph node for NF.
612 CallGraphNode *NF_CGN = CG.getOrInsertFunction(NF);
613
610614
611615 // Loop over all of the callers of the function, transforming the call sites
612616 // to pass in the loaded pointers.
719723 AA.replaceWithNewValue(Call, New);
720724
721725 // Update the callgraph to know that the callsite has been transformed.
722 CG[Call->getParent()->getParent()]->replaceCallSite(Call, New);
726 CG[Call->getParent()->getParent()]->replaceCallSite(Call, New, NF_CGN);
723727
724728 if (!Call->use_empty()) {
725729 Call->replaceAllUsesWith(New);
855859 // Tell the alias analysis that the old function is about to disappear.
856860 AA.replaceWithNewValue(F, NF);
857861
862
863 NF_CGN->stealCalledFunctionsFrom(CG[F]);
864
858865 // Now that the old function is dead, delete it.
859 F->eraseFromParent();
860 return NF;
861 }
866 delete CG.removeFunctionFromModule(F);
867
868 return NF_CGN;
869 }
4343 FunctionAttrs() : CallGraphSCCPass(&ID) {}
4444
4545 // runOnSCC - Analyze the SCC, performing the transformation if possible.
46 bool runOnSCC(const std::vector &SCC);
46 bool runOnSCC(std::vector &SCC);
4747
4848 // AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
4949 bool AddReadAttrs(const std::vector &SCC);
338338 return MadeChange;
339339 }
340340
341 bool FunctionAttrs::runOnSCC(const std::vector &SCC) {
341 bool FunctionAttrs::runOnSCC(std::vector &SCC) {
342342 bool Changed = AddReadAttrs(SCC);
343343 Changed |= AddNoCaptureAttrs(SCC);
344344 Changed |= AddNoAliasAttrs(SCC);
206206 return true;
207207 }
208208
209 bool Inliner::runOnSCC(const std::vector &SCC) {
209 bool Inliner::runOnSCC(std::vector &SCC) {
210210 CallGraph &CG = getAnalysis();
211211 const TargetData *TD = getAnalysisIfAvailable();
212212
4040 PruneEH() : CallGraphSCCPass(&ID) {}
4141
4242 // runOnSCC - Analyze the SCC, performing the transformation if possible.
43 bool runOnSCC(const std::vector &SCC);
43 bool runOnSCC(std::vector &SCC);
4444
4545 bool SimplifyFunction(Function *F);
4646 void DeleteBasicBlock(BasicBlock *BB);
5454 Pass *llvm::createPruneEHPass() { return new PruneEH(); }
5555
5656
57 bool PruneEH::runOnSCC(const std::vector &SCC) {
57 bool PruneEH::runOnSCC(std::vector &SCC) {
5858 SmallPtrSet SCCNodes;
5959 CallGraph &CG = getAnalysis();
6060 bool MadeChange = false;
186186 UnwindBlock->removePredecessor(II->getParent());
187187
188188 // Fix up the call graph.
189 CGN->replaceCallSite(II, Call);
189 CGN->replaceCallSite(II, Call, 0/*keep callee*/);
190190
191191 // Insert a branch to the normal destination right before the
192192 // invoke.
4848 CallGraphSCCPass::getAnalysisUsage(AU);
4949 }
5050
51 virtual bool runOnSCC(const std::vector &SCC);
51 virtual bool runOnSCC(std::vector &SCC);
5252 static char ID; // Pass identification, replacement for typeid
5353 SRETPromotion() : CallGraphSCCPass(&ID) {}
5454
5555 private:
56 bool PromoteReturn(CallGraphNode *CGN);
56 CallGraphNode *PromoteReturn(CallGraphNode *CGN);
5757 bool isSafeToUpdateAllCallers(Function *F);
5858 Function *cloneFunctionBody(Function *F, const StructType *STy);
59 void updateCallSites(Function *F, Function *NF);
59 CallGraphNode *updateCallSites(Function *F, Function *NF);
6060 bool nestedStructType(const StructType *STy);
6161 };
6262 }
6969 return new SRETPromotion();
7070 }
7171
72 bool SRETPromotion::runOnSCC(const std::vector &SCC) {
72 bool SRETPromotion::runOnSCC(std::vector &SCC) {
7373 bool Changed = false;
7474
7575 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
76 Changed |= PromoteReturn(SCC[i]);
76 if (CallGraphNode *NewNode = PromoteReturn(SCC[i])) {
77 SCC[i] = NewNode;
78 Changed = true;
79 }
7780
7881 return Changed;
7982 }
8083
8184 /// PromoteReturn - This method promotes function that uses StructRet paramater
82 /// into a function that uses mulitple return value.
83 bool SRETPromotion::PromoteReturn(CallGraphNode *CGN) {
85 /// into a function that uses multiple return values.
86 CallGraphNode *SRETPromotion::PromoteReturn(CallGraphNode *CGN) {
8487 Function *F = CGN->getFunction();
8588
8689 if (!F || F->isDeclaration() || !F->hasLocalLinkage())
87 return false;
90 return 0;
8891
8992 // Make sure that function returns struct.
9093 if (F->arg_size() == 0 || !F->hasStructRetAttr() || F->doesNotReturn())
91 return false;
94 return 0;
9295
9396 DEBUG(errs() << "SretPromotion: Looking at sret function "
9497 << F->getName() << "\n");
9598
96 assert (F->getReturnType() == Type::getVoidTy(F->getContext()) &&
97 "Invalid function return type");
99 assert(F->getReturnType() == Type::getVoidTy(F->getContext()) &&
100 "Invalid function return type");
98101 Function::arg_iterator AI = F->arg_begin();
99102 const llvm::PointerType *FArgType = dyn_cast(AI->getType());
100 assert (FArgType && "Invalid sret parameter type");
103 assert(FArgType && "Invalid sret parameter type");
101104 const llvm::StructType *STy =
102105 dyn_cast(FArgType->getElementType());
103 assert (STy && "Invalid sret parameter element type");
106 assert(STy && "Invalid sret parameter element type");
104107
105108 // Check if it is ok to perform this promotion.
106109 if (isSafeToUpdateAllCallers(F) == false) {
107110 DEBUG(errs() << "SretPromotion: Not all callers can be updated\n");
108111 NumRejectedSRETUses++;
109 return false;
112 return 0;
110113 }
111114
112115 DEBUG(errs() << "SretPromotion: sret argument will be promoted\n");
134137 Function *NF = cloneFunctionBody(F, STy);
135138
136139 // [4] Update all call sites to use new function
137 updateCallSites(F, NF);
138
139 F->eraseFromParent();
140 getAnalysis().changeFunction(F, NF);
141 return true;
140 CallGraphNode *NF_CFN = updateCallSites(F, NF);
141
142 CallGraph &CG = getAnalysis();
143 NF_CFN->stealCalledFunctionsFrom(CG[F]);
144
145 delete CG.removeFunctionFromModule(F);
146 return NF_CFN;
142147 }
143148
144149 // Check if it is ok to perform this promotion.
246251 Function::arg_iterator NI = NF->arg_begin();
247252 ++I;
248253 while (I != E) {
249 I->replaceAllUsesWith(NI);
250 NI->takeName(I);
251 ++I;
252 ++NI;
254 I->replaceAllUsesWith(NI);
255 NI->takeName(I);
256 ++I;
257 ++NI;
253258 }
254259
255260 return NF;
256261 }
257262
258263 /// updateCallSites - Update all sites that call F to use NF.
259 void SRETPromotion::updateCallSites(Function *F, Function *NF) {
264 CallGraphNode *SRETPromotion::updateCallSites(Function *F, Function *NF) {
260265 CallGraph &CG = getAnalysis();
261266 SmallVector Args;
262267
263268 // Attributes - Keep track of the parameter attributes for the arguments.
264269 SmallVector ArgAttrsVec;
270
271 // Get a new callgraph node for NF.
272 CallGraphNode *NF_CGN = CG.getOrInsertFunction(NF);
265273
266274 while (!F->use_empty()) {
267275 CallSite CS = CallSite::get(*F->use_begin());
312320 New->takeName(Call);
313321
314322 // Update the callgraph to know that the callsite has been transformed.
315 CG[Call->getParent()->getParent()]->replaceCallSite(Call, New);
323 CG[Call->getParent()->getParent()]->replaceCallSite(Call, New, NF_CGN);
316324
317325 // Update all users of sret parameter to extract value using extractvalue.
318326 for (Value::use_iterator UI = FirstCArg->use_begin(),
321329 CallInst *C2 = dyn_cast(U2);
322330 if (C2 && (C2 == Call))
323331 continue;
324 else if (GetElementPtrInst *UGEP = dyn_cast(U2)) {
332
333 if (GetElementPtrInst *UGEP = dyn_cast(U2)) {
325334 ConstantInt *Idx = dyn_cast(UGEP->getOperand(2));
326335 assert (Idx && "Unexpected getelementptr index!");
327336 Value *GR = ExtractValueInst::Create(New, Idx->getZExtValue(),
334343 L->eraseFromParent();
335344 }
336345 UGEP->eraseFromParent();
346 continue;
337347 }
338 else assert( 0 && "Unexpected sret parameter use");
348
349 assert(0 && "Unexpected sret parameter use");
339350 }
340351 Call->eraseFromParent();
341352 }
353
354 return NF_CGN;
342355 }
343356
344357 /// nestedStructType - Return true if STy includes any
131131 CallGraphSCCPassPrinter(const PassInfo *PI) :
132132 CallGraphSCCPass(&ID), PassToPrint(PI) {}
133133
134 virtual bool runOnSCC(const std::vector&SCC) {
134 virtual bool runOnSCC(std::vector&SCC) {
135135 if (!Quiet) {
136136 outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
137137