blob: dddd54ad4925ef9226e3a05d993b861a2f9da0d1 [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
Chris Lattner7e51c872002-10-31 06:52:26 +0000119 /// is the same thing as: getLink(getValueNode(V))
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000120 ///
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
Chris Lattner7e51c872002-10-31 06:52:26 +0000130 /// null), then we create a new node, link it, then return it.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000131 ///
Chris Lattner7e51c872002-10-31 06:52:26 +0000132 DSNodeHandle &getLink(const DSNodeHandle &Node, unsigned Link = 0);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000133 };
134}
135
136//===----------------------------------------------------------------------===//
137// DSGraph constructor - Simply use the GraphBuilder to construct the local
138// graph.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000139DSGraph::DSGraph(Function &F) : Func(&F) {
140 // Use the graph builder to construct the local version of the graph
141 GraphBuilder B(*this, Nodes, RetNode, ValueMap, FunctionCalls);
142 markIncompleteNodes();
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000143}
144
145
146//===----------------------------------------------------------------------===//
147// Helper method implementations...
148//
149
150
151// createNode - Create a new DSNode, ensuring that it is properly added to the
152// graph.
153//
154DSNode *GraphBuilder::createNode(DSNode::NodeTy NodeType, const Type *Ty) {
155 DSNode *N = new DSNode(NodeType, Ty);
156 Nodes.push_back(N);
157 return N;
158}
159
160
Chris Lattner0d9bab82002-07-18 00:12:30 +0000161// getGlobalNode - Just like getValueNode, except the global node itself is
162// returned, not a scalar node pointing to a global.
163//
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000164DSNodeHandle &GraphBuilder::getGlobalNode(GlobalValue &V) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000165 DSNodeHandle &NH = ValueMap[&V];
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000166 if (NH.getNode()) return NH; // Already have a node? Just return it...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000167
168 // Create a new global node for this global variable...
169 DSNode *G = createNode(DSNode::GlobalNode, V.getType()->getElementType());
170 G->addGlobal(&V);
171
172 // If this node has outgoing edges, make sure to recycle the same node for
173 // each use. For functions and other global variables, this is unneccesary,
174 // so avoid excessive merging by cloning these nodes on demand.
175 //
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000176 NH.setNode(G);
177 return NH;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000178}
179
180
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000181// getValueNode - Return a DSNode that corresponds the the specified LLVM value.
182// This either returns the already existing node, or creates a new one and adds
183// it to the graph, if none exists.
184//
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000185DSNodeHandle &GraphBuilder::getValueNode(Value &V) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000186 assert(isPointerType(V.getType()) && "Should only use pointer scalars!");
Chris Lattner448f4942002-10-21 13:51:30 +0000187
Chris Lattnerc314ac42002-07-11 20:32:02 +0000188 if (GlobalValue *GV = dyn_cast<GlobalValue>(&V)) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000189 // The GlobalScalarValueMap keeps track of the scalar nodes that point to
190 // global values... The ValueMap contains pointers to the global memory
191 // object itself, not the scalar constant that points to the memory.
192 //
193 DSNodeHandle &NH = GlobalScalarValueMap[GV];
194 if (NH.getNode()) return NH;
195
196 // If this is a global value, create the global pointed to.
197 DSNode *N = createNode(DSNode::ScalarNode, V.getType());
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000198 NH.setOffset(0);
199 NH.setNode(N);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000200
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000201 N->addEdgeTo(0, getGlobalNode(*GV));
202 return NH;
203
204 } else {
205 DSNodeHandle &NH = ValueMap[&V];
206 if (NH.getNode())
207 return NH; // Already have a node? Just return it...
208
209 // Otherwise we need to create a new scalar node...
210 DSNode *N = createNode(DSNode::ScalarNode, V.getType());
211
212 NH.setOffset(0);
213 NH.setNode(N);
214 return NH;
215 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000216}
217
Chris Lattner7e51c872002-10-31 06:52:26 +0000218/// getValueDest - Return the DSNode that the actual value points to. This is
219/// the same thing as: getLink(getValueNode(V), 0)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000220///
221DSNodeHandle &GraphBuilder::getValueDest(Value &V) {
Chris Lattner7e51c872002-10-31 06:52:26 +0000222 return getLink(getValueNode(V));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000223}
224
Chris Lattner0d9bab82002-07-18 00:12:30 +0000225
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000226/// getLink - This method is used to return the specified link in the
227/// specified node if one exists. If a link does not already exist (it's
228/// null), then we create a new node, link it, then return it. We must
229/// specify the type of the Node field we are accessing so that we know what
230/// type should be linked to if we need to create a new node.
231///
Chris Lattner7e51c872002-10-31 06:52:26 +0000232DSNodeHandle &GraphBuilder::getLink(const DSNodeHandle &node, unsigned LinkNo) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000233 DSNodeHandle &Node = const_cast<DSNodeHandle&>(node);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000234 DSNodeHandle *Link = Node.getLink(LinkNo);
235 if (Link) return *Link;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000236
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000237 // If the link hasn't been created yet, make and return a new shadow node
238 DSNode *N = createNode(DSNode::ShadowNode, 0);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000239 Node.setLink(LinkNo, N);
240 return *Node.getLink(LinkNo);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000241}
242
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000243
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000244//===----------------------------------------------------------------------===//
245// Specific instruction type handler implementations...
246//
247
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000248/// Alloca & Malloc instruction implementation - Simply create a new memory
249/// object, pointing the scalar to it.
250///
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000251void GraphBuilder::handleAlloc(AllocationInst &AI, DSNode::NodeTy NodeType) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000252 DSNode *New = createNode(NodeType, 0);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000253
254 // Make the scalar point to the new node...
255 getValueNode(AI).addEdgeTo(New);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000256}
257
258// PHINode - Make the scalar for the PHI node point to all of the things the
259// incoming values point to... which effectively causes them to be merged.
260//
261void GraphBuilder::visitPHINode(PHINode &PN) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000262 if (!isPointerType(PN.getType())) return; // Only pointer PHIs
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000263
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000264 DSNodeHandle &ScalarDest = getValueDest(PN);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000265 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000266 if (!isa<ConstantPointerNull>(PN.getIncomingValue(i)))
267 ScalarDest.mergeWith(getValueDest(*PN.getIncomingValue(i)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000268}
269
270void GraphBuilder::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000271 DSNodeHandle Value = getValueDest(*GEP.getOperand(0));
272
273 unsigned Offset = 0;
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000274 const PointerType *PTy = cast<PointerType>(GEP.getOperand(0)->getType());
275 const Type *CurTy = PTy->getElementType();
276 DSTypeRec &TopTypeRec =
277 Value.getNode()->getTypeRec(PTy->getElementType(), Value.getOffset());
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000278
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000279 // If the node had to be folded... exit quickly
280 if (TopTypeRec.Ty == Type::VoidTy) {
281 getValueNode(GEP).addEdgeTo(Value); // GEP result points to folded node
282 return;
283 }
284
285 // Handle the pointer index specially...
286 if (GEP.getNumOperands() > 1 &&
287 GEP.getOperand(1) != ConstantSInt::getNullValue(Type::LongTy)) {
288
289 // If we already know this is an array being accessed, don't do anything...
290 if (!TopTypeRec.isArray) {
291 TopTypeRec.isArray = true;
292
293 // If we are treating some inner field pointer as an array, fold the node
294 // up because we cannot handle it right. This can come because of
295 // something like this: &((&Pt->X)[1]) == &Pt->Y
296 //
297 if (Value.getOffset()) {
298 // Value is now the pointer we want to GEP to be...
299 Value.getNode()->foldNodeCompletely();
300 getValueNode(GEP).addEdgeTo(Value); // GEP result points to folded node
301 return;
302 } else {
303 // This is a pointer to the first byte of the node. Make sure that we
304 // are pointing to the outter most type in the node.
305 // FIXME: We need to check one more case here...
306 }
307 }
308 }
309
310 // All of these subscripts are indexing INTO the elements we have...
311 for (unsigned i = 2, e = GEP.getNumOperands(); i < e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000312 if (GEP.getOperand(i)->getType() == Type::LongTy) {
Chris Lattnere03f32b2002-10-02 06:24:36 +0000313 // Get the type indexing into...
314 const SequentialType *STy = cast<SequentialType>(CurTy);
315 CurTy = STy->getElementType();
316 if (ConstantSInt *CS = dyn_cast<ConstantSInt>(GEP.getOperand(i))) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000317 Offset += CS->getValue()*TD.getTypeSize(CurTy);
Chris Lattnere03f32b2002-10-02 06:24:36 +0000318 } else {
319 // Variable index into a node. We must merge all of the elements of the
320 // sequential type here.
321 if (isa<PointerType>(STy))
322 std::cerr << "Pointer indexing not handled yet!\n";
323 else {
324 const ArrayType *ATy = cast<ArrayType>(STy);
325 unsigned ElSize = TD.getTypeSize(CurTy);
326 DSNode *N = Value.getNode();
327 assert(N && "Value must have a node!");
328 unsigned RawOffset = Offset+Value.getOffset();
329
330 // Loop over all of the elements of the array, merging them into the
331 // zero'th element.
332 for (unsigned i = 1, e = ATy->getNumElements(); i != e; ++i)
333 // Merge all of the byte components of this array element
334 for (unsigned j = 0; j != ElSize; ++j)
335 N->mergeIndexes(RawOffset+j, RawOffset+i*ElSize+j);
336 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000337 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000338 } else if (GEP.getOperand(i)->getType() == Type::UByteTy) {
339 unsigned FieldNo = cast<ConstantUInt>(GEP.getOperand(i))->getValue();
340 const StructType *STy = cast<StructType>(CurTy);
341 Offset += TD.getStructLayout(STy)->MemberOffsets[FieldNo];
342 CurTy = STy->getContainedType(FieldNo);
343 }
344
345 // Add in the offset calculated...
346 Value.setOffset(Value.getOffset()+Offset);
347
348 // Value is now the pointer we want to GEP to be...
349 getValueNode(GEP).addEdgeTo(Value);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000350}
351
352void GraphBuilder::visitLoadInst(LoadInst &LI) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000353 DSNodeHandle &Ptr = getValueDest(*LI.getOperand(0));
Chris Lattner06285232002-10-17 22:13:19 +0000354 Ptr.getNode()->NodeType |= DSNode::Read;
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000355
356 // Ensure a typerecord exists...
357 Ptr.getNode()->getTypeRec(LI.getType(), Ptr.getOffset());
Chris Lattner06285232002-10-17 22:13:19 +0000358
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000359 if (isPointerType(LI.getType()))
Chris Lattner7e51c872002-10-31 06:52:26 +0000360 getValueNode(LI).addEdgeTo(getLink(Ptr));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000361}
362
363void GraphBuilder::visitStoreInst(StoreInst &SI) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000364 DSNodeHandle &Dest = getValueDest(*SI.getOperand(1));
Chris Lattner06285232002-10-17 22:13:19 +0000365 Dest.getNode()->NodeType |= DSNode::Modified;
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000366 const Type *StoredTy = SI.getOperand(0)->getType();
367
368 // Ensure a typerecord exists...
369 Dest.getNode()->getTypeRec(StoredTy, Dest.getOffset());
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000370
371 // Avoid adding edges from null, or processing non-"pointer" stores
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000372 if (isPointerType(StoredTy) &&
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000373 !isa<ConstantPointerNull>(SI.getOperand(0))) {
374 Dest.addEdgeTo(getValueDest(*SI.getOperand(0)));
375 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000376}
377
378void GraphBuilder::visitReturnInst(ReturnInst &RI) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000379 if (RI.getNumOperands() && isPointerType(RI.getOperand(0)->getType()) &&
380 !isa<ConstantPointerNull>(RI.getOperand(0))) {
381 DSNodeHandle &Value = getValueDest(*RI.getOperand(0));
382 Value.mergeWith(RetNode);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000383 RetNode = Value;
384 }
385}
386
387void GraphBuilder::visitCallInst(CallInst &CI) {
Chris Lattnerc314ac42002-07-11 20:32:02 +0000388 // Set up the return value...
Chris Lattner0969c502002-10-21 02:08:03 +0000389 DSNodeHandle RetVal;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000390 if (isPointerType(CI.getType()))
Chris Lattner7e51c872002-10-31 06:52:26 +0000391 RetVal = getLink(getValueNode(CI));
Chris Lattnerc314ac42002-07-11 20:32:02 +0000392
Chris Lattner0969c502002-10-21 02:08:03 +0000393 DSNodeHandle Callee;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000394 // Special case for a direct call, avoid creating spurious scalar node...
Chris Lattner0969c502002-10-21 02:08:03 +0000395 if (GlobalValue *GV = dyn_cast<GlobalValue>(CI.getOperand(0)))
396 Callee = getGlobalNode(*GV);
397 else
Chris Lattner7e51c872002-10-31 06:52:26 +0000398 Callee = getLink(getValueNode(*CI.getOperand(0)));
Chris Lattner0d9bab82002-07-18 00:12:30 +0000399
Chris Lattner0969c502002-10-21 02:08:03 +0000400 std::vector<DSNodeHandle> Args;
401 Args.reserve(CI.getNumOperands()-1);
402
403 // Calculate the arguments vector...
404 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000405 if (isPointerType(CI.getOperand(i)->getType()))
Chris Lattner7e51c872002-10-31 06:52:26 +0000406 Args.push_back(getLink(getValueNode(*CI.getOperand(i))));
Chris Lattner0969c502002-10-21 02:08:03 +0000407
408 // Add a new function call entry...
409 FunctionCalls.push_back(DSCallSite(CI, RetVal, Callee, Args));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000410}
Chris Lattner055dc2c2002-07-18 15:54:42 +0000411
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000412/// Handle casts...
413void GraphBuilder::visitCastInst(CastInst &CI) {
414 if (isPointerType(CI.getType()) && isPointerType(CI.getOperand(0)->getType()))
Chris Lattner7e51c872002-10-31 06:52:26 +0000415 getValueNode(CI).addEdgeTo(getLink(getValueNode(*CI.getOperand(0))));
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000416}
Chris Lattner055dc2c2002-07-18 15:54:42 +0000417
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000418
419
420
421//===----------------------------------------------------------------------===//
422// LocalDataStructures Implementation
423//===----------------------------------------------------------------------===//
424
425// releaseMemory - If the pass pipeline is done with this pass, we can release
426// our memory... here...
427//
428void LocalDataStructures::releaseMemory() {
429 for (std::map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
430 E = DSInfo.end(); I != E; ++I)
431 delete I->second;
432
433 // Empty map so next time memory is released, data structures are not
434 // re-deleted.
435 DSInfo.clear();
436}
437
438bool LocalDataStructures::run(Module &M) {
439 // Calculate all of the graphs...
440 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
441 if (!I->isExternal())
442 DSInfo.insert(std::make_pair(I, new DSGraph(*I)));
443 return false;
Chris Lattner055dc2c2002-07-18 15:54:42 +0000444}