blob: 0cdb0e16a906e88a598d1ce5ee291ac240a52575 [file] [log] [blame]
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001//===- Local.cpp - Compute a local data structure graph for a function ----===//
Chris Lattnerc68c31b2002-07-10 22:38:08 +00002//
3// Compute the local version of the data structure graph for a function. The
4// external interface to this file is the DSGraph constructor.
5//
6//===----------------------------------------------------------------------===//
7
Chris Lattnerc5f21de2002-10-02 22:14:38 +00008#include "llvm/Analysis/DSGraph.h"
Chris Lattner055dc2c2002-07-18 15:54:42 +00009#include "llvm/Analysis/DataStructure.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000010#include "llvm/iMemory.h"
11#include "llvm/iTerminators.h"
12#include "llvm/iPHINode.h"
13#include "llvm/iOther.h"
14#include "llvm/Constants.h"
15#include "llvm/DerivedTypes.h"
Chris Lattnerfccd06f2002-10-01 22:33:50 +000016#include "llvm/Function.h"
17#include "llvm/GlobalVariable.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000018#include "llvm/Support/InstVisitor.h"
Chris Lattnerfccd06f2002-10-01 22:33:50 +000019#include "llvm/Target/TargetData.h"
20#include "Support/Statistic.h"
21
22// FIXME: This should eventually be a FunctionPass that is automatically
23// aggregated into a Pass.
24//
25#include "llvm/Module.h"
26
Chris Lattnerc68c31b2002-07-10 22:38:08 +000027using std::map;
28using std::vector;
29
Chris Lattner97f51a32002-07-27 01:12:15 +000030static RegisterAnalysis<LocalDataStructures>
31X("datastructure", "Local Data Structure Analysis");
Chris Lattner97f51a32002-07-27 01:12:15 +000032
Chris Lattnerfccd06f2002-10-01 22:33:50 +000033using namespace DataStructureAnalysis;
34
35namespace DataStructureAnalysis {
36 // FIXME: Do something smarter with target data!
37 TargetData TD("temp-td");
38 unsigned PointerSize(TD.getPointerSize());
39
40 // isPointerType - Return true if this type is big enough to hold a pointer.
41 bool isPointerType(const Type *Ty) {
42 if (isa<PointerType>(Ty))
43 return true;
44 else if (Ty->isPrimitiveType() && Ty->isInteger())
45 return Ty->getPrimitiveSize() >= PointerSize;
46 return false;
47 }
48}
49
Chris Lattnerc68c31b2002-07-10 22:38:08 +000050
51namespace {
Chris Lattnerfccd06f2002-10-01 22:33:50 +000052 //===--------------------------------------------------------------------===//
53 // GraphBuilder Class
54 //===--------------------------------------------------------------------===//
55 //
56 /// This class is the builder class that constructs the local data structure
57 /// graph by performing a single pass over the function in question.
58 ///
Chris Lattnerc68c31b2002-07-10 22:38:08 +000059 class GraphBuilder : InstVisitor<GraphBuilder> {
60 DSGraph &G;
61 vector<DSNode*> &Nodes;
62 DSNodeHandle &RetNode; // Node that gets returned...
63 map<Value*, DSNodeHandle> &ValueMap;
Chris Lattner8f0a16e2002-10-31 05:45:02 +000064 map<GlobalValue*, DSNodeHandle> GlobalScalarValueMap;
Vikram S. Adve42fd1692002-10-20 18:07:37 +000065 vector<DSCallSite> &FunctionCalls;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000066
67 public:
68 GraphBuilder(DSGraph &g, vector<DSNode*> &nodes, DSNodeHandle &retNode,
69 map<Value*, DSNodeHandle> &vm,
Vikram S. Adve42fd1692002-10-20 18:07:37 +000070 vector<DSCallSite> &fc)
Chris Lattnerc68c31b2002-07-10 22:38:08 +000071 : G(g), Nodes(nodes), RetNode(retNode), ValueMap(vm), FunctionCalls(fc) {
Chris Lattner0d9bab82002-07-18 00:12:30 +000072
73 // Create scalar nodes for all pointer arguments...
74 for (Function::aiterator I = G.getFunction().abegin(),
75 E = G.getFunction().aend(); I != E; ++I)
Chris Lattnerfccd06f2002-10-01 22:33:50 +000076 if (isPointerType(I->getType()))
77 getValueDest(*I);
Chris Lattner0d9bab82002-07-18 00:12:30 +000078
Chris Lattnerc68c31b2002-07-10 22:38:08 +000079 visit(G.getFunction()); // Single pass over the function
Chris Lattner2a2c4902002-07-18 18:19:09 +000080
81 // Not inlining, only eliminate trivially dead nodes.
82 G.removeTriviallyDeadNodes();
Chris Lattnerc68c31b2002-07-10 22:38:08 +000083 }
84
85 private:
86 // Visitor functions, used to handle each instruction type we encounter...
87 friend class InstVisitor<GraphBuilder>;
88 void visitMallocInst(MallocInst &MI) { handleAlloc(MI, DSNode::NewNode); }
89 void visitAllocaInst(AllocaInst &AI) { handleAlloc(AI, DSNode::AllocaNode);}
90 void handleAlloc(AllocationInst &AI, DSNode::NodeTy NT);
91
92 void visitPHINode(PHINode &PN);
93
94 void visitGetElementPtrInst(GetElementPtrInst &GEP);
95 void visitReturnInst(ReturnInst &RI);
96 void visitLoadInst(LoadInst &LI);
97 void visitStoreInst(StoreInst &SI);
98 void visitCallInst(CallInst &CI);
99 void visitSetCondInst(SetCondInst &SCI) {} // SetEQ & friends are ignored
100 void visitFreeInst(FreeInst &FI) {} // Ignore free instructions
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000101 void visitCastInst(CastInst &CI);
102 void visitInstruction(Instruction &I) {}
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000103
104 private:
105 // Helper functions used to implement the visitation functions...
106
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000107 /// createNode - Create a new DSNode, ensuring that it is properly added to
108 /// the graph.
109 ///
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000110 DSNode *createNode(DSNode::NodeTy NodeType, const Type *Ty);
111
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000112 /// getValueNode - Return a DSNode that corresponds the the specified LLVM
113 /// value. This either returns the already existing node, or creates a new
114 /// one and adds it to the graph, if none exists.
115 ///
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000116 DSNodeHandle &getValueNode(Value &V);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000117
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000118 /// getValueDest - Return the DSNode that the actual value points to. This
119 /// is basically the same thing as: getLink(getValueNode(V), 0)
120 ///
121 DSNodeHandle &getValueDest(Value &V);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000122
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000123 /// getGlobalNode - Just like getValueNode, except the global node itself is
124 /// returned, not a scalar node pointing to a global.
125 ///
126 DSNodeHandle &getGlobalNode(GlobalValue &V);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000127
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000128 /// getLink - This method is used to return the specified link in the
129 /// specified node if one exists. If a link does not already exist (it's
130 /// null), then we create a new node, link it, then return it. We must
131 /// specify the type of the Node field we are accessing so that we know what
132 /// type should be linked to if we need to create a new node.
133 ///
134 DSNodeHandle &getLink(const DSNodeHandle &Node, unsigned Link,
135 const Type *FieldTy);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000136 };
137}
138
139//===----------------------------------------------------------------------===//
140// DSGraph constructor - Simply use the GraphBuilder to construct the local
141// graph.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000142DSGraph::DSGraph(Function &F) : Func(&F) {
143 // Use the graph builder to construct the local version of the graph
144 GraphBuilder B(*this, Nodes, RetNode, ValueMap, FunctionCalls);
145 markIncompleteNodes();
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000146}
147
148
149//===----------------------------------------------------------------------===//
150// Helper method implementations...
151//
152
153
154// createNode - Create a new DSNode, ensuring that it is properly added to the
155// graph.
156//
157DSNode *GraphBuilder::createNode(DSNode::NodeTy NodeType, const Type *Ty) {
158 DSNode *N = new DSNode(NodeType, Ty);
159 Nodes.push_back(N);
160 return N;
161}
162
163
Chris Lattner0d9bab82002-07-18 00:12:30 +0000164// getGlobalNode - Just like getValueNode, except the global node itself is
165// returned, not a scalar node pointing to a global.
166//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000167DSNodeHandle &GraphBuilder::getGlobalNode(GlobalValue &V) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000168 DSNodeHandle &NH = ValueMap[&V];
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000169 if (NH.getNode()) return NH; // Already have a node? Just return it...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000170
171 // Create a new global node for this global variable...
172 DSNode *G = createNode(DSNode::GlobalNode, V.getType()->getElementType());
173 G->addGlobal(&V);
174
175 // If this node has outgoing edges, make sure to recycle the same node for
176 // each use. For functions and other global variables, this is unneccesary,
177 // so avoid excessive merging by cloning these nodes on demand.
178 //
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000179 NH.setNode(G);
180 return NH;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000181}
182
183
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000184// getValueNode - Return a DSNode that corresponds the the specified LLVM value.
185// This either returns the already existing node, or creates a new one and adds
186// it to the graph, if none exists.
187//
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000188DSNodeHandle &GraphBuilder::getValueNode(Value &V) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000189 assert(isPointerType(V.getType()) && "Should only use pointer scalars!");
Chris Lattner448f4942002-10-21 13:51:30 +0000190
Chris Lattnerc314ac42002-07-11 20:32:02 +0000191 if (GlobalValue *GV = dyn_cast<GlobalValue>(&V)) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000192 // The GlobalScalarValueMap keeps track of the scalar nodes that point to
193 // global values... The ValueMap contains pointers to the global memory
194 // object itself, not the scalar constant that points to the memory.
195 //
196 DSNodeHandle &NH = GlobalScalarValueMap[GV];
197 if (NH.getNode()) return NH;
198
199 // If this is a global value, create the global pointed to.
200 DSNode *N = createNode(DSNode::ScalarNode, V.getType());
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000201 NH.setOffset(0);
202 NH.setNode(N);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000203
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000204 N->addEdgeTo(0, getGlobalNode(*GV));
205 return NH;
206
207 } else {
208 DSNodeHandle &NH = ValueMap[&V];
209 if (NH.getNode())
210 return NH; // Already have a node? Just return it...
211
212 // Otherwise we need to create a new scalar node...
213 DSNode *N = createNode(DSNode::ScalarNode, V.getType());
214
215 NH.setOffset(0);
216 NH.setNode(N);
217 return NH;
218 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000219}
220
221/// getValueDest - Return the DSNode that the actual value points to. This
222/// is basically the same thing as: getLink(getValueNode(V), 0)
223///
224DSNodeHandle &GraphBuilder::getValueDest(Value &V) {
225 return getLink(getValueNode(V), 0, V.getType());
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000226}
227
Chris Lattner0d9bab82002-07-18 00:12:30 +0000228
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000229/// getLink - This method is used to return the specified link in the
230/// specified node if one exists. If a link does not already exist (it's
231/// null), then we create a new node, link it, then return it. We must
232/// specify the type of the Node field we are accessing so that we know what
233/// type should be linked to if we need to create a new node.
234///
235DSNodeHandle &GraphBuilder::getLink(const DSNodeHandle &node,
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000236 unsigned LinkNo,
237 const Type *FieldTy // FIXME: eliminate
238 ) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000239 DSNodeHandle &Node = const_cast<DSNodeHandle&>(node);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000240 DSNodeHandle *Link = Node.getLink(LinkNo);
241 if (Link) return *Link;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000242
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000243#if 0 // FIXME: delete
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000244 // If we are indexing with a typed pointer, then the thing we are pointing
245 // to is of the pointed type. If we are pointing to it with an integer
246 // (because of cast to an integer), we represent it with a void type.
247 //
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000248 const Type *ReqTy = 0;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000249 if (const PointerType *Ptr = dyn_cast<PointerType>(FieldTy))
250 ReqTy = Ptr->getElementType();
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000251#endif
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000252
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000253 // If the link hasn't been created yet, make and return a new shadow node
254 DSNode *N = createNode(DSNode::ShadowNode, 0);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000255 Node.setLink(LinkNo, N);
256 return *Node.getLink(LinkNo);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000257}
258
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000259
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000260//===----------------------------------------------------------------------===//
261// Specific instruction type handler implementations...
262//
263
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000264/// Alloca & Malloc instruction implementation - Simply create a new memory
265/// object, pointing the scalar to it.
266///
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000267void GraphBuilder::handleAlloc(AllocationInst &AI, DSNode::NodeTy NodeType) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000268 DSNode *New = createNode(NodeType, 0);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000269
270 // Make the scalar point to the new node...
271 getValueNode(AI).addEdgeTo(New);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000272}
273
274// PHINode - Make the scalar for the PHI node point to all of the things the
275// incoming values point to... which effectively causes them to be merged.
276//
277void GraphBuilder::visitPHINode(PHINode &PN) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000278 if (!isPointerType(PN.getType())) return; // Only pointer PHIs
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000279
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000280 DSNodeHandle &ScalarDest = getValueDest(PN);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000281 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000282 if (!isa<ConstantPointerNull>(PN.getIncomingValue(i)))
283 ScalarDest.mergeWith(getValueDest(*PN.getIncomingValue(i)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000284}
285
286void GraphBuilder::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000287 DSNodeHandle Value = getValueDest(*GEP.getOperand(0));
288
289 unsigned Offset = 0;
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000290 const PointerType *PTy = cast<PointerType>(GEP.getOperand(0)->getType());
291 const Type *CurTy = PTy->getElementType();
292 DSTypeRec &TopTypeRec =
293 Value.getNode()->getTypeRec(PTy->getElementType(), Value.getOffset());
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000294
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000295 // If the node had to be folded... exit quickly
296 if (TopTypeRec.Ty == Type::VoidTy) {
297 getValueNode(GEP).addEdgeTo(Value); // GEP result points to folded node
298 return;
299 }
300
301 // Handle the pointer index specially...
302 if (GEP.getNumOperands() > 1 &&
303 GEP.getOperand(1) != ConstantSInt::getNullValue(Type::LongTy)) {
304
305 // If we already know this is an array being accessed, don't do anything...
306 if (!TopTypeRec.isArray) {
307 TopTypeRec.isArray = true;
308
309 // If we are treating some inner field pointer as an array, fold the node
310 // up because we cannot handle it right. This can come because of
311 // something like this: &((&Pt->X)[1]) == &Pt->Y
312 //
313 if (Value.getOffset()) {
314 // Value is now the pointer we want to GEP to be...
315 Value.getNode()->foldNodeCompletely();
316 getValueNode(GEP).addEdgeTo(Value); // GEP result points to folded node
317 return;
318 } else {
319 // This is a pointer to the first byte of the node. Make sure that we
320 // are pointing to the outter most type in the node.
321 // FIXME: We need to check one more case here...
322 }
323 }
324 }
325
326 // All of these subscripts are indexing INTO the elements we have...
327 for (unsigned i = 2, e = GEP.getNumOperands(); i < e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000328 if (GEP.getOperand(i)->getType() == Type::LongTy) {
Chris Lattnere03f32b2002-10-02 06:24:36 +0000329 // Get the type indexing into...
330 const SequentialType *STy = cast<SequentialType>(CurTy);
331 CurTy = STy->getElementType();
332 if (ConstantSInt *CS = dyn_cast<ConstantSInt>(GEP.getOperand(i))) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000333 Offset += CS->getValue()*TD.getTypeSize(CurTy);
Chris Lattnere03f32b2002-10-02 06:24:36 +0000334 } else {
335 // Variable index into a node. We must merge all of the elements of the
336 // sequential type here.
337 if (isa<PointerType>(STy))
338 std::cerr << "Pointer indexing not handled yet!\n";
339 else {
340 const ArrayType *ATy = cast<ArrayType>(STy);
341 unsigned ElSize = TD.getTypeSize(CurTy);
342 DSNode *N = Value.getNode();
343 assert(N && "Value must have a node!");
344 unsigned RawOffset = Offset+Value.getOffset();
345
346 // Loop over all of the elements of the array, merging them into the
347 // zero'th element.
348 for (unsigned i = 1, e = ATy->getNumElements(); i != e; ++i)
349 // Merge all of the byte components of this array element
350 for (unsigned j = 0; j != ElSize; ++j)
351 N->mergeIndexes(RawOffset+j, RawOffset+i*ElSize+j);
352 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000353 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000354 } else if (GEP.getOperand(i)->getType() == Type::UByteTy) {
355 unsigned FieldNo = cast<ConstantUInt>(GEP.getOperand(i))->getValue();
356 const StructType *STy = cast<StructType>(CurTy);
357 Offset += TD.getStructLayout(STy)->MemberOffsets[FieldNo];
358 CurTy = STy->getContainedType(FieldNo);
359 }
360
361 // Add in the offset calculated...
362 Value.setOffset(Value.getOffset()+Offset);
363
364 // Value is now the pointer we want to GEP to be...
365 getValueNode(GEP).addEdgeTo(Value);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000366}
367
368void GraphBuilder::visitLoadInst(LoadInst &LI) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000369 DSNodeHandle &Ptr = getValueDest(*LI.getOperand(0));
Chris Lattner06285232002-10-17 22:13:19 +0000370 Ptr.getNode()->NodeType |= DSNode::Read;
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000371
372 // Ensure a typerecord exists...
373 Ptr.getNode()->getTypeRec(LI.getType(), Ptr.getOffset());
Chris Lattner06285232002-10-17 22:13:19 +0000374
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000375 if (isPointerType(LI.getType()))
376 getValueNode(LI).addEdgeTo(getLink(Ptr, 0, LI.getType()));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000377}
378
379void GraphBuilder::visitStoreInst(StoreInst &SI) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000380 DSNodeHandle &Dest = getValueDest(*SI.getOperand(1));
Chris Lattner06285232002-10-17 22:13:19 +0000381 Dest.getNode()->NodeType |= DSNode::Modified;
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000382 const Type *StoredTy = SI.getOperand(0)->getType();
383
384 // Ensure a typerecord exists...
385 Dest.getNode()->getTypeRec(StoredTy, Dest.getOffset());
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000386
387 // Avoid adding edges from null, or processing non-"pointer" stores
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000388 if (isPointerType(StoredTy) &&
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000389 !isa<ConstantPointerNull>(SI.getOperand(0))) {
390 Dest.addEdgeTo(getValueDest(*SI.getOperand(0)));
391 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000392}
393
394void GraphBuilder::visitReturnInst(ReturnInst &RI) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000395 if (RI.getNumOperands() && isPointerType(RI.getOperand(0)->getType()) &&
396 !isa<ConstantPointerNull>(RI.getOperand(0))) {
397 DSNodeHandle &Value = getValueDest(*RI.getOperand(0));
398 Value.mergeWith(RetNode);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000399 RetNode = Value;
400 }
401}
402
403void GraphBuilder::visitCallInst(CallInst &CI) {
Chris Lattnerc314ac42002-07-11 20:32:02 +0000404 // Set up the return value...
Chris Lattner0969c502002-10-21 02:08:03 +0000405 DSNodeHandle RetVal;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000406 if (isPointerType(CI.getType()))
Chris Lattner0969c502002-10-21 02:08:03 +0000407 RetVal = getLink(getValueNode(CI), 0, CI.getType());
Chris Lattnerc314ac42002-07-11 20:32:02 +0000408
Chris Lattner0969c502002-10-21 02:08:03 +0000409 DSNodeHandle Callee;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000410 // Special case for a direct call, avoid creating spurious scalar node...
Chris Lattner0969c502002-10-21 02:08:03 +0000411 if (GlobalValue *GV = dyn_cast<GlobalValue>(CI.getOperand(0)))
412 Callee = getGlobalNode(*GV);
413 else
414 Callee = getLink(getValueNode(*CI.getOperand(0)), 0,
415 CI.getOperand(0)->getType());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000416
Chris Lattner0969c502002-10-21 02:08:03 +0000417 std::vector<DSNodeHandle> Args;
418 Args.reserve(CI.getNumOperands()-1);
419
420 // Calculate the arguments vector...
421 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000422 if (isPointerType(CI.getOperand(i)->getType()))
423 Args.push_back(getLink(getValueNode(*CI.getOperand(i)), 0,
424 CI.getOperand(i)->getType()));
Chris Lattner0969c502002-10-21 02:08:03 +0000425
426 // Add a new function call entry...
427 FunctionCalls.push_back(DSCallSite(CI, RetVal, Callee, Args));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000428}
Chris Lattner055dc2c2002-07-18 15:54:42 +0000429
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000430/// Handle casts...
431void GraphBuilder::visitCastInst(CastInst &CI) {
432 if (isPointerType(CI.getType()) && isPointerType(CI.getOperand(0)->getType()))
433 getValueNode(CI).addEdgeTo(getLink(getValueNode(*CI.getOperand(0)), 0,
434 CI.getOperand(0)->getType()));
435}
Chris Lattner055dc2c2002-07-18 15:54:42 +0000436
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000437
438
439
440//===----------------------------------------------------------------------===//
441// LocalDataStructures Implementation
442//===----------------------------------------------------------------------===//
443
444// releaseMemory - If the pass pipeline is done with this pass, we can release
445// our memory... here...
446//
447void LocalDataStructures::releaseMemory() {
448 for (std::map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
449 E = DSInfo.end(); I != E; ++I)
450 delete I->second;
451
452 // Empty map so next time memory is released, data structures are not
453 // re-deleted.
454 DSInfo.clear();
455}
456
457bool LocalDataStructures::run(Module &M) {
458 // Calculate all of the graphs...
459 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
460 if (!I->isExternal())
461 DSInfo.insert(std::make_pair(I, new DSGraph(*I)));
462 return false;
Chris Lattner055dc2c2002-07-18 15:54:42 +0000463}