llvm.org GIT mirror llvm / a541b0f
Change CallGraphNode to maintain it's Function as an AssertingVH for sanity. This didn't turn up any bugs. Change CallGraphNode to maintain its "callsite" information in the call edges list as a WeakVH instead of as an instruction*. This fixes a broad class of dangling pointer bugs, and makes CallGraph have a number of useful invariants again. This fixes the class of problem indicated by PR4029 and PR3601. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@80663 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 10 years ago
6 changed file(s) with 81 addition(s) and 56 deletion(s). Raw diff Collapse all Expand all
5454 #include "llvm/ADT/STLExtras.h"
5555 #include "llvm/Pass.h"
5656 #include "llvm/Support/CallSite.h"
57 #include "llvm/Support/ValueHandle.h"
5758 #include "llvm/System/IncludeFile.h"
5859 #include
5960
157158 };
158159
159160 //===----------------------------------------------------------------------===//
160 // CallGraphNode class definition
161 // CallGraphNode class definition.
161162 //
162163 class CallGraphNode {
163 Function *F;
164 typedef std::pair CallRecord;
164 AssertingVH F;
165
166 // CallRecord - This is a pair of the calling instruction (a call or invoke)
167 // and the callgraph node being called.
168 public:
169 typedef std::pair CallRecord;
170 private:
165171 std::vector CalledFunctions;
166172
167173 /// NumReferences - This is the number of times that this CallGraphNode occurs
239245 /// addCalledFunction - Add a function to the list of functions called by this
240246 /// one.
241247 void addCalledFunction(CallSite CS, CallGraphNode *M) {
242 CalledFunctions.push_back(std::make_pair(CS, M));
248 CalledFunctions.push_back(std::make_pair(CS.getInstruction(), M));
243249 M->AddRef();
244250 }
245251
252 void removeCallEdge(iterator I) {
253 I->second->DropRef();
254 *I = CalledFunctions.back();
255 CalledFunctions.pop_back();
256 }
257
258
246259 /// removeCallEdgeFor - This method removes the edge in the node for the
247260 /// specified call site. Note that this method takes linear time, so it
248261 /// should be used sparingly.
249262 void removeCallEdgeFor(CallSite CS);
250263
251 // FIXME: REMOVE THIS WHEN HACK IS REMOVED FROM CGSCCPASSMGR.
252 void removeCallEdgeFor(Instruction *CS);
253
254
255264 /// removeAnyCallEdgeTo - This method removes all call edges from this node
256265 /// to the specified callee function. This takes more time to execute than
257266 /// removeCallEdgeTo, so it should not be used unless necessary.
277286 template <> struct GraphTraits {
278287 typedef CallGraphNode NodeType;
279288
280 typedef std::pair CGNPairTy;
289 typedef CallGraphNode::CallRecord CGNPairTy;
281290 typedef std::pointer_to_unary_function CGNDerefFun;
282291
283292 static NodeType *getEntryNode(CallGraphNode *CGN) { return CGN; }
240240 void CallGraphNode::removeCallEdgeFor(CallSite CS) {
241241 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
242242 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
243 if (I->first == CS) {
243 if (I->first == CS.getInstruction()) {
244244 I->second->DropRef();
245245 *I = CalledFunctions.back();
246246 CalledFunctions.pop_back();
248248 }
249249 }
250250 }
251
252 // FIXME: REMOVE THIS WHEN HACK IS REMOVED FROM CGSCCPASSMGR.
253 void CallGraphNode::removeCallEdgeFor(Instruction *CS) {
254 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
255 assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
256 if (I->first.getInstruction() == CS) {
257 I->second->DropRef();
258 *I = CalledFunctions.back();
259 CalledFunctions.pop_back();
260 return;
261 }
262 }
263
264 }
265
266251
267252
268253 // removeAnyCallEdgeTo - This method removes any call edges from this node to
284269 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
285270 assert(I != CalledFunctions.end() && "Cannot find callee to remove!");
286271 CallRecord &CR = *I;
287 if (CR.second == Callee && CR.first.getInstruction() == 0) {
272 if (CR.second == Callee && CR.first == 0) {
288273 Callee->DropRef();
289274 *I = CalledFunctions.back();
290275 CalledFunctions.pop_back();
300285 CallGraphNode *NewCallee) {
301286 for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
302287 assert(I != CalledFunctions.end() && "Cannot find callsite to replace!");
303 if (I->first != Old) continue;
304
305 I->first = New;
288 if (I->first != Old.getInstruction()) continue;
289
290 I->first = New.getInstruction();
306291
307292 // If the callee is changing, not just the callsite, then update it as
308293 // well.
124124
125125 void CGPassManager::RefreshCallGraph(std::vector &CurSCC,
126126 CallGraph &CG) {
127 DenseMap<Instruction*, CallGraphNode*> CallSites;
127 DenseMap<Value*, CallGraphNode*> CallSites;
128128
129129 DEBUG(errs() << "CGSCCPASSMGR: Refreshing SCC with " << CurSCC.size()
130130 << " nodes:\n";
145145
146146 // Get the set of call sites currently in the function.
147147 for (CallGraphNode::iterator I = CGN->begin(), E = CGN->end(); I != E; ++I){
148 assert(I->first.getInstruction() &&
149 "Call site record in function should not be abstract");
150 assert(!CallSites.count(I->first.getInstruction()) &&
148 // If this call site is null, then the function pass deleted the call
149 // entirely and the WeakVH nulled it out.
150 if (I->first == 0 ||
151 // If we've already seen this call site, then the FunctionPass RAUW'd
152 // one call with another, which resulted in two "uses" in the edge
153 // list of the same call.
154 CallSites.count(I->first) ||
155
156 // If the call edge is not from a call or invoke, then the function
157 // pass RAUW'd a call with another value. This can happen when
158 // constant folding happens of well known functions etc.
159 CallSite::get(I->first).getInstruction() == 0) {
160 // Just remove the edge from the set of callees.
161 CGN->removeCallEdge(I);
162 E = CGN->end();
163 --I;
164 continue;
165 }
166
167 assert(!CallSites.count(I->first) &&
151168 "Call site occurs in node multiple times");
152 CallSites.insert(std::make_pair(I->first.getInstruction(),
153 I->second));
169 CallSites.insert(std::make_pair(I->first, I->second));
154170 }
155171
156172 // Loop over all of the instructions in the function, getting the callsites.
161177
162178 // If this call site already existed in the callgraph, just verify it
163179 // matches up to expectations and remove it from CallSites.
164 DenseMap<Instruction*, CallGraphNode*>::iterator ExistingIt =
180 DenseMap<Value*, CallGraphNode*>::iterator ExistingIt =
165181 CallSites.find(CS.getInstruction());
166182 if (ExistingIt != CallSites.end()) {
167183 CallGraphNode *ExistingNode = ExistingIt->second;
200216 }
201217
202218 // After scanning this function, if we still have entries in callsites, then
203 // they are dangling pointers. Crap. Well, until we change CallGraph to
204 // use CallbackVH, we'll just zap them here. When we have that, this should
205 // turn into an assertion.
206 if (CallSites.empty()) continue;
207
208 for (DenseMap::iterator I = CallSites.begin(),
209 E = CallSites.end(); I != E; ++I)
210 // FIXME: I had to add a special horrible form of removeCallEdgeFor to
211 // support this. Remove the Instruction* version of it when we can.
212 CGN->removeCallEdgeFor(I->first);
213 MadeChange = true;
214 CallSites.clear();
219 // they are dangling pointers. WeakVH should save us for this, so abort if
220 // this happens.
221 assert(CallSites.empty() && "Dangling pointers found in call sites map");
222
223 // Periodically do an explicit clear to remove tombstones when processing
224 // large scc's.
225 if ((sccidx & 15) == 0)
226 CallSites.clear();
215227 }
216228
217229 DEBUG(if (MadeChange) {
164164 // function if we have invokes to non-unwinding functions or code after calls to
165165 // no-return functions.
166166 bool PruneEH::SimplifyFunction(Function *F) {
167 CallGraph &CG = getAnalysis();
168 CallGraphNode *CGN = CG[F];
169
170167 bool MadeChange = false;
171168 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
172169 if (InvokeInst *II = dyn_cast(BB->getTerminator()))
180177 Call->setAttributes(II->getAttributes());
181178
182179 // Anything that used the value produced by the invoke instruction
183 // now uses the value produced by the call instruction.
180 // now uses the value produced by the call instruction. Note that we
181 // do this even for void functions and calls with no uses so that the
182 // callgraph edge is updated.
184183 II->replaceAllUsesWith(Call);
185184 BasicBlock *UnwindBlock = II->getUnwindDest();
186185 UnwindBlock->removePredecessor(II->getParent());
187
188 // Fix up the call graph.
189 CGN->replaceCallSite(II, Call, 0/*keep callee*/);
190186
191187 // Insert a branch to the normal destination right before the
192188 // invoke.
211211 }
212212
213213 for (; I != E; ++I) {
214 const Instruction *OrigCall = I->first.getInstruction();
214 const Value *OrigCall = I->first;
215215
216216 DenseMap::iterator VMI = ValueMap.find(OrigCall);
217217 // Only copy the edge if the call was inlined!
0 ; RUN: llvm-as < %s | opt -argpromotion -simplifycfg -constmerge | llvm-dis
1 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
2 target triple = "i386-apple-darwin10.0"
3
4 %struct.VEC2 = type { double, double, double }
5 %struct.VERTEX = type { %struct.VEC2, %struct.VERTEX*, %struct.VERTEX* }
6 %struct.edge_rec = type { %struct.VERTEX*, %struct.edge_rec*, i32, i8* }
7
8 declare %struct.edge_rec* @alloc_edge() nounwind ssp
9
10 define i64 @build_delaunay(%struct.VERTEX* %tree, %struct.VERTEX* %extra) nounwind ssp {
11 entry:
12 br i1 undef, label %bb11, label %bb12
13
14 bb11: ; preds = %bb10
15 %a = call %struct.edge_rec* @alloc_edge() nounwind ; <%struct.edge_rec*> [#uses=0]
16 ret i64 123
17
18 bb12: ; preds = %bb10
19 %b = call %struct.edge_rec* @alloc_edge() nounwind ; <%struct.edge_rec*> [#uses=1]
20 %c = ptrtoint %struct.edge_rec* %b to i64
21 ret i64 %c
22 }