blob: 3308fd195bb81ef69925f257cf723b3933529108 [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 Lattner055dc2c2002-07-18 15:54:42 +00008#include "llvm/Analysis/DataStructure.h"
Chris Lattner0779ba62002-11-09 19:25:10 +00009#include "llvm/Analysis/DSGraph.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"
Chris Lattner4fe34612002-11-18 21:44:19 +000021#include "Support/Timer.h"
Chris Lattnerfccd06f2002-10-01 22:33:50 +000022
23// FIXME: This should eventually be a FunctionPass that is automatically
24// aggregated into a Pass.
25//
26#include "llvm/Module.h"
27
Chris Lattner97f51a32002-07-27 01:12:15 +000028static RegisterAnalysis<LocalDataStructures>
29X("datastructure", "Local Data Structure Analysis");
Chris Lattner97f51a32002-07-27 01:12:15 +000030
Chris Lattnerb1060432002-11-07 05:20:53 +000031namespace DS {
Chris Lattnerfccd06f2002-10-01 22:33:50 +000032 // FIXME: Do something smarter with target data!
33 TargetData TD("temp-td");
Chris Lattnerfccd06f2002-10-01 22:33:50 +000034
35 // isPointerType - Return true if this type is big enough to hold a pointer.
36 bool isPointerType(const Type *Ty) {
37 if (isa<PointerType>(Ty))
38 return true;
39 else if (Ty->isPrimitiveType() && Ty->isInteger())
40 return Ty->getPrimitiveSize() >= PointerSize;
41 return false;
42 }
43}
Chris Lattnerb1060432002-11-07 05:20:53 +000044using namespace DS;
Chris Lattnerfccd06f2002-10-01 22:33:50 +000045
Chris Lattnerc68c31b2002-07-10 22:38:08 +000046
47namespace {
Chris Lattnerfccd06f2002-10-01 22:33:50 +000048 //===--------------------------------------------------------------------===//
49 // GraphBuilder Class
50 //===--------------------------------------------------------------------===//
51 //
52 /// This class is the builder class that constructs the local data structure
53 /// graph by performing a single pass over the function in question.
54 ///
Chris Lattnerc68c31b2002-07-10 22:38:08 +000055 class GraphBuilder : InstVisitor<GraphBuilder> {
56 DSGraph &G;
Chris Lattnerb3416bc2003-02-01 04:01:21 +000057 std::vector<DSNode*> &Nodes;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000058 DSNodeHandle &RetNode; // Node that gets returned...
Chris Lattner41c04f72003-02-01 04:52:08 +000059 hash_map<Value*, DSNodeHandle> &ScalarMap;
Chris Lattnerb3416bc2003-02-01 04:01:21 +000060 std::vector<DSCallSite> &FunctionCalls;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000061
62 public:
Chris Lattnerb3416bc2003-02-01 04:01:21 +000063 GraphBuilder(DSGraph &g, std::vector<DSNode*> &nodes, DSNodeHandle &retNode,
Chris Lattner41c04f72003-02-01 04:52:08 +000064 hash_map<Value*, DSNodeHandle> &SM,
Chris Lattnerb3416bc2003-02-01 04:01:21 +000065 std::vector<DSCallSite> &fc)
Chris Lattnerc875f022002-11-03 21:27:48 +000066 : G(g), Nodes(nodes), RetNode(retNode), ScalarMap(SM), FunctionCalls(fc) {
Chris Lattner0d9bab82002-07-18 00:12:30 +000067
68 // Create scalar nodes for all pointer arguments...
69 for (Function::aiterator I = G.getFunction().abegin(),
70 E = G.getFunction().aend(); I != E; ++I)
Chris Lattnerfccd06f2002-10-01 22:33:50 +000071 if (isPointerType(I->getType()))
72 getValueDest(*I);
Chris Lattner0d9bab82002-07-18 00:12:30 +000073
Chris Lattnerc68c31b2002-07-10 22:38:08 +000074 visit(G.getFunction()); // Single pass over the function
Chris Lattnerc68c31b2002-07-10 22:38:08 +000075 }
76
77 private:
78 // Visitor functions, used to handle each instruction type we encounter...
79 friend class InstVisitor<GraphBuilder>;
Chris Lattnerd18f3422002-11-03 21:24:04 +000080 void visitMallocInst(MallocInst &MI) { handleAlloc(MI, DSNode::HeapNode); }
Chris Lattnerc68c31b2002-07-10 22:38:08 +000081 void visitAllocaInst(AllocaInst &AI) { handleAlloc(AI, DSNode::AllocaNode);}
82 void handleAlloc(AllocationInst &AI, DSNode::NodeTy NT);
83
84 void visitPHINode(PHINode &PN);
85
Chris Lattner51340062002-11-08 05:00:44 +000086 void visitGetElementPtrInst(User &GEP);
Chris Lattnerc68c31b2002-07-10 22:38:08 +000087 void visitReturnInst(ReturnInst &RI);
88 void visitLoadInst(LoadInst &LI);
89 void visitStoreInst(StoreInst &SI);
90 void visitCallInst(CallInst &CI);
91 void visitSetCondInst(SetCondInst &SCI) {} // SetEQ & friends are ignored
Vikram S. Advebac06222002-12-06 21:17:10 +000092 void visitFreeInst(FreeInst &FI);
Chris Lattnerfccd06f2002-10-01 22:33:50 +000093 void visitCastInst(CastInst &CI);
94 void visitInstruction(Instruction &I) {}
Chris Lattnerc68c31b2002-07-10 22:38:08 +000095
96 private:
97 // Helper functions used to implement the visitation functions...
98
Chris Lattnerfccd06f2002-10-01 22:33:50 +000099 /// createNode - Create a new DSNode, ensuring that it is properly added to
100 /// the graph.
101 ///
Chris Lattner92673292002-11-02 00:13:20 +0000102 DSNode *createNode(DSNode::NodeTy NodeType, const Type *Ty = 0) {
103 DSNode *N = new DSNode(NodeType, Ty); // Create the node
104 Nodes.push_back(N); // Add node to nodes list
105 return N;
106 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000107
Chris Lattnerc875f022002-11-03 21:27:48 +0000108 /// setDestTo - Set the ScalarMap entry for the specified value to point to
Chris Lattner92673292002-11-02 00:13:20 +0000109 /// the specified destination. If the Value already points to a node, make
110 /// sure to merge the two destinations together.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000111 ///
Chris Lattner92673292002-11-02 00:13:20 +0000112 void setDestTo(Value &V, const DSNodeHandle &NH);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000113
Chris Lattner92673292002-11-02 00:13:20 +0000114 /// getValueDest - Return the DSNode that the actual value points to.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000115 ///
Chris Lattner92673292002-11-02 00:13:20 +0000116 DSNodeHandle getValueDest(Value &V);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000117
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000118 /// getLink - This method is used to return the specified link in the
119 /// specified node if one exists. If a link does not already exist (it's
Chris Lattner7e51c872002-10-31 06:52:26 +0000120 /// null), then we create a new node, link it, then return it.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000121 ///
Chris Lattner7e51c872002-10-31 06:52:26 +0000122 DSNodeHandle &getLink(const DSNodeHandle &Node, unsigned Link = 0);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000123 };
124}
125
126//===----------------------------------------------------------------------===//
127// DSGraph constructor - Simply use the GraphBuilder to construct the local
128// graph.
Chris Lattner2e4f9bf2002-11-09 20:01:01 +0000129DSGraph::DSGraph(Function &F, DSGraph *GG) : Func(&F), GlobalsGraph(GG) {
Chris Lattner2a068862002-11-10 06:53:38 +0000130 PrintAuxCalls = false;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000131 // Use the graph builder to construct the local version of the graph
Chris Lattnerc875f022002-11-03 21:27:48 +0000132 GraphBuilder B(*this, Nodes, RetNode, ScalarMap, FunctionCalls);
Chris Lattner4fe34612002-11-18 21:44:19 +0000133#ifndef NDEBUG
134 Timer::addPeakMemoryMeasurement();
135#endif
Chris Lattner394471f2003-01-23 22:05:33 +0000136 markIncompleteNodes(DSGraph::MarkFormalArgs);
Chris Lattner96517252002-11-09 20:55:24 +0000137
138 // Remove any nodes made dead due to merging...
Chris Lattner394471f2003-01-23 22:05:33 +0000139 removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000140}
141
142
143//===----------------------------------------------------------------------===//
144// Helper method implementations...
145//
146
Chris Lattner92673292002-11-02 00:13:20 +0000147/// getValueDest - Return the DSNode that the actual value points to.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000148///
Chris Lattner51340062002-11-08 05:00:44 +0000149DSNodeHandle GraphBuilder::getValueDest(Value &Val) {
150 Value *V = &Val;
151 if (V == Constant::getNullValue(V->getType()))
152 return 0; // Null doesn't point to anything, don't add to ScalarMap!
Chris Lattner92673292002-11-02 00:13:20 +0000153
Vikram S. Advebac06222002-12-06 21:17:10 +0000154 DSNodeHandle &NH = ScalarMap[V];
155 if (NH.getNode())
156 return NH; // Already have a node? Just return it...
157
158 // Otherwise we need to create a new node to point to.
159 // Check first for constant expressions that must be traversed to
160 // extract the actual value.
Chris Lattner51340062002-11-08 05:00:44 +0000161 if (Constant *C = dyn_cast<Constant>(V))
162 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
Vikram S. Advebac06222002-12-06 21:17:10 +0000163 return NH = getValueDest(*CPR->getValue());
Chris Lattner51340062002-11-08 05:00:44 +0000164 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
165 if (CE->getOpcode() == Instruction::Cast)
Vikram S. Advebac06222002-12-06 21:17:10 +0000166 return NH = getValueDest(*CE->getOperand(0));
Chris Lattner51340062002-11-08 05:00:44 +0000167 if (CE->getOpcode() == Instruction::GetElementPtr) {
168 visitGetElementPtrInst(*CE);
Chris Lattner41c04f72003-02-01 04:52:08 +0000169 hash_map<Value*, DSNodeHandle>::iterator I = ScalarMap.find(CE);
Chris Lattner2cfbaaf2002-11-09 20:14:03 +0000170 assert(I != ScalarMap.end() && "GEP didn't get processed right?");
Vikram S. Advebac06222002-12-06 21:17:10 +0000171 return NH = I->second;
Chris Lattner51340062002-11-08 05:00:44 +0000172 }
173
174 // This returns a conservative unknown node for any unhandled ConstExpr
Vikram S. Advebac06222002-12-06 21:17:10 +0000175 return NH = createNode(DSNode::UnknownNode);
Chris Lattner51340062002-11-08 05:00:44 +0000176 } else if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(C)) {
177 // Random constants are unknown mem
Vikram S. Advebac06222002-12-06 21:17:10 +0000178 return NH = createNode(DSNode::UnknownNode);
Chris Lattner51340062002-11-08 05:00:44 +0000179 } else {
180 assert(0 && "Unknown constant type!");
181 }
182
Chris Lattner92673292002-11-02 00:13:20 +0000183 // Otherwise we need to create a new node to point to...
184 DSNode *N;
Chris Lattner51340062002-11-08 05:00:44 +0000185 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner92673292002-11-02 00:13:20 +0000186 // Create a new global node for this global variable...
187 N = createNode(DSNode::GlobalNode, GV->getType()->getElementType());
188 N->addGlobal(GV);
189 } else {
190 // Otherwise just create a shadow node
191 N = createNode(DSNode::ShadowNode);
192 }
193
194 NH.setNode(N); // Remember that we are pointing to it...
195 NH.setOffset(0);
196 return NH;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000197}
198
Chris Lattner0d9bab82002-07-18 00:12:30 +0000199
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000200/// getLink - This method is used to return the specified link in the
201/// specified node if one exists. If a link does not already exist (it's
202/// null), then we create a new node, link it, then return it. We must
203/// specify the type of the Node field we are accessing so that we know what
204/// type should be linked to if we need to create a new node.
205///
Chris Lattner7e51c872002-10-31 06:52:26 +0000206DSNodeHandle &GraphBuilder::getLink(const DSNodeHandle &node, unsigned LinkNo) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000207 DSNodeHandle &Node = const_cast<DSNodeHandle&>(node);
Chris Lattner08db7192002-11-06 06:20:27 +0000208 DSNodeHandle &Link = Node.getLink(LinkNo);
209 if (!Link.getNode()) {
210 // If the link hasn't been created yet, make and return a new shadow node
211 Link = createNode(DSNode::ShadowNode);
212 }
213 return Link;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000214}
215
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000216
Chris Lattnerc875f022002-11-03 21:27:48 +0000217/// setDestTo - Set the ScalarMap entry for the specified value to point to the
Chris Lattner92673292002-11-02 00:13:20 +0000218/// specified destination. If the Value already points to a node, make sure to
219/// merge the two destinations together.
220///
221void GraphBuilder::setDestTo(Value &V, const DSNodeHandle &NH) {
Chris Lattnerc875f022002-11-03 21:27:48 +0000222 DSNodeHandle &AINH = ScalarMap[&V];
Chris Lattner92673292002-11-02 00:13:20 +0000223 if (AINH.getNode() == 0) // Not pointing to anything yet?
224 AINH = NH; // Just point directly to NH
225 else
226 AINH.mergeWith(NH);
227}
228
229
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000230//===----------------------------------------------------------------------===//
231// Specific instruction type handler implementations...
232//
233
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000234/// Alloca & Malloc instruction implementation - Simply create a new memory
235/// object, pointing the scalar to it.
236///
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000237void GraphBuilder::handleAlloc(AllocationInst &AI, DSNode::NodeTy NodeType) {
Chris Lattner92673292002-11-02 00:13:20 +0000238 setDestTo(AI, createNode(NodeType));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000239}
240
241// PHINode - Make the scalar for the PHI node point to all of the things the
242// incoming values point to... which effectively causes them to be merged.
243//
244void GraphBuilder::visitPHINode(PHINode &PN) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000245 if (!isPointerType(PN.getType())) return; // Only pointer PHIs
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000246
Chris Lattnerc875f022002-11-03 21:27:48 +0000247 DSNodeHandle &PNDest = ScalarMap[&PN];
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000248 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
Chris Lattner92673292002-11-02 00:13:20 +0000249 PNDest.mergeWith(getValueDest(*PN.getIncomingValue(i)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000250}
251
Chris Lattner51340062002-11-08 05:00:44 +0000252void GraphBuilder::visitGetElementPtrInst(User &GEP) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000253 DSNodeHandle Value = getValueDest(*GEP.getOperand(0));
Chris Lattner92673292002-11-02 00:13:20 +0000254 if (Value.getNode() == 0) return;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000255
256 unsigned Offset = 0;
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000257 const PointerType *PTy = cast<PointerType>(GEP.getOperand(0)->getType());
258 const Type *CurTy = PTy->getElementType();
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000259
Chris Lattner08db7192002-11-06 06:20:27 +0000260 if (Value.getNode()->mergeTypeInfo(CurTy, Value.getOffset())) {
261 // If the node had to be folded... exit quickly
Chris Lattner92673292002-11-02 00:13:20 +0000262 setDestTo(GEP, Value); // GEP result points to folded node
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000263 return;
264 }
265
Chris Lattner08db7192002-11-06 06:20:27 +0000266#if 0
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000267 // Handle the pointer index specially...
268 if (GEP.getNumOperands() > 1 &&
269 GEP.getOperand(1) != ConstantSInt::getNullValue(Type::LongTy)) {
270
271 // If we already know this is an array being accessed, don't do anything...
272 if (!TopTypeRec.isArray) {
273 TopTypeRec.isArray = true;
274
275 // If we are treating some inner field pointer as an array, fold the node
276 // up because we cannot handle it right. This can come because of
277 // something like this: &((&Pt->X)[1]) == &Pt->Y
278 //
279 if (Value.getOffset()) {
280 // Value is now the pointer we want to GEP to be...
281 Value.getNode()->foldNodeCompletely();
Chris Lattner92673292002-11-02 00:13:20 +0000282 setDestTo(GEP, Value); // GEP result points to folded node
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000283 return;
284 } else {
285 // This is a pointer to the first byte of the node. Make sure that we
286 // are pointing to the outter most type in the node.
287 // FIXME: We need to check one more case here...
288 }
289 }
290 }
Chris Lattner08db7192002-11-06 06:20:27 +0000291#endif
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000292
293 // All of these subscripts are indexing INTO the elements we have...
294 for (unsigned i = 2, e = GEP.getNumOperands(); i < e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000295 if (GEP.getOperand(i)->getType() == Type::LongTy) {
Chris Lattnere03f32b2002-10-02 06:24:36 +0000296 // Get the type indexing into...
297 const SequentialType *STy = cast<SequentialType>(CurTy);
298 CurTy = STy->getElementType();
Chris Lattner08db7192002-11-06 06:20:27 +0000299#if 0
Chris Lattnere03f32b2002-10-02 06:24:36 +0000300 if (ConstantSInt *CS = dyn_cast<ConstantSInt>(GEP.getOperand(i))) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000301 Offset += CS->getValue()*TD.getTypeSize(CurTy);
Chris Lattnere03f32b2002-10-02 06:24:36 +0000302 } else {
303 // Variable index into a node. We must merge all of the elements of the
304 // sequential type here.
305 if (isa<PointerType>(STy))
306 std::cerr << "Pointer indexing not handled yet!\n";
307 else {
308 const ArrayType *ATy = cast<ArrayType>(STy);
309 unsigned ElSize = TD.getTypeSize(CurTy);
310 DSNode *N = Value.getNode();
311 assert(N && "Value must have a node!");
312 unsigned RawOffset = Offset+Value.getOffset();
313
314 // Loop over all of the elements of the array, merging them into the
315 // zero'th element.
316 for (unsigned i = 1, e = ATy->getNumElements(); i != e; ++i)
317 // Merge all of the byte components of this array element
318 for (unsigned j = 0; j != ElSize; ++j)
319 N->mergeIndexes(RawOffset+j, RawOffset+i*ElSize+j);
320 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000321 }
Chris Lattner08db7192002-11-06 06:20:27 +0000322#endif
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000323 } else if (GEP.getOperand(i)->getType() == Type::UByteTy) {
324 unsigned FieldNo = cast<ConstantUInt>(GEP.getOperand(i))->getValue();
325 const StructType *STy = cast<StructType>(CurTy);
326 Offset += TD.getStructLayout(STy)->MemberOffsets[FieldNo];
327 CurTy = STy->getContainedType(FieldNo);
328 }
329
330 // Add in the offset calculated...
331 Value.setOffset(Value.getOffset()+Offset);
332
333 // Value is now the pointer we want to GEP to be...
Chris Lattner92673292002-11-02 00:13:20 +0000334 setDestTo(GEP, Value);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000335}
336
337void GraphBuilder::visitLoadInst(LoadInst &LI) {
Chris Lattner92673292002-11-02 00:13:20 +0000338 DSNodeHandle Ptr = getValueDest(*LI.getOperand(0));
339 if (Ptr.getNode() == 0) return;
340
341 // Make that the node is read from...
Chris Lattner06285232002-10-17 22:13:19 +0000342 Ptr.getNode()->NodeType |= DSNode::Read;
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000343
344 // Ensure a typerecord exists...
Chris Lattner08db7192002-11-06 06:20:27 +0000345 Ptr.getNode()->mergeTypeInfo(LI.getType(), Ptr.getOffset());
Chris Lattner92673292002-11-02 00:13:20 +0000346
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000347 if (isPointerType(LI.getType()))
Chris Lattner92673292002-11-02 00:13:20 +0000348 setDestTo(LI, getLink(Ptr));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000349}
350
351void GraphBuilder::visitStoreInst(StoreInst &SI) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000352 const Type *StoredTy = SI.getOperand(0)->getType();
Chris Lattner92673292002-11-02 00:13:20 +0000353 DSNodeHandle Dest = getValueDest(*SI.getOperand(1));
354 if (Dest.getNode() == 0) return;
355
Vikram S. Advebac06222002-12-06 21:17:10 +0000356 // Mark that the node is written to...
Chris Lattner92673292002-11-02 00:13:20 +0000357 Dest.getNode()->NodeType |= DSNode::Modified;
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000358
359 // Ensure a typerecord exists...
Chris Lattner08db7192002-11-06 06:20:27 +0000360 Dest.getNode()->mergeTypeInfo(StoredTy, Dest.getOffset());
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000361
362 // Avoid adding edges from null, or processing non-"pointer" stores
Chris Lattner92673292002-11-02 00:13:20 +0000363 if (isPointerType(StoredTy))
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000364 Dest.addEdgeTo(getValueDest(*SI.getOperand(0)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000365}
366
367void GraphBuilder::visitReturnInst(ReturnInst &RI) {
Chris Lattner92673292002-11-02 00:13:20 +0000368 if (RI.getNumOperands() && isPointerType(RI.getOperand(0)->getType()))
369 RetNode.mergeWith(getValueDest(*RI.getOperand(0)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000370}
371
372void GraphBuilder::visitCallInst(CallInst &CI) {
Chris Lattnerc314ac42002-07-11 20:32:02 +0000373 // Set up the return value...
Chris Lattner0969c502002-10-21 02:08:03 +0000374 DSNodeHandle RetVal;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000375 if (isPointerType(CI.getType()))
Chris Lattner92673292002-11-02 00:13:20 +0000376 RetVal = getValueDest(CI);
Chris Lattnerc314ac42002-07-11 20:32:02 +0000377
Chris Lattner92673292002-11-02 00:13:20 +0000378 DSNodeHandle Callee = getValueDest(*CI.getOperand(0));
Chris Lattner0d9bab82002-07-18 00:12:30 +0000379
Chris Lattner0969c502002-10-21 02:08:03 +0000380 std::vector<DSNodeHandle> Args;
381 Args.reserve(CI.getNumOperands()-1);
382
383 // Calculate the arguments vector...
384 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000385 if (isPointerType(CI.getOperand(i)->getType()))
Chris Lattner92673292002-11-02 00:13:20 +0000386 Args.push_back(getValueDest(*CI.getOperand(i)));
Chris Lattner0969c502002-10-21 02:08:03 +0000387
388 // Add a new function call entry...
389 FunctionCalls.push_back(DSCallSite(CI, RetVal, Callee, Args));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000390}
Chris Lattner055dc2c2002-07-18 15:54:42 +0000391
Vikram S. Advebac06222002-12-06 21:17:10 +0000392void GraphBuilder::visitFreeInst(FreeInst &FI) {
Vikram S. Advebac06222002-12-06 21:17:10 +0000393 // Mark that the node is written to...
Chris Lattner3f24d7a2003-01-28 20:59:57 +0000394 getValueDest(*FI.getOperand(0)).getNode()->NodeType
395 |= DSNode::Modified | DSNode::HeapNode;
Vikram S. Advebac06222002-12-06 21:17:10 +0000396}
397
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000398/// Handle casts...
399void GraphBuilder::visitCastInst(CastInst &CI) {
Chris Lattner5af344d2002-11-02 00:36:03 +0000400 if (isPointerType(CI.getType()))
401 if (isPointerType(CI.getOperand(0)->getType())) {
402 // Cast one pointer to the other, just act like a copy instruction
Chris Lattner92673292002-11-02 00:13:20 +0000403 setDestTo(CI, getValueDest(*CI.getOperand(0)));
Chris Lattner5af344d2002-11-02 00:36:03 +0000404 } else {
405 // Cast something (floating point, small integer) to a pointer. We need
406 // to track the fact that the node points to SOMETHING, just something we
407 // don't know about. Make an "Unknown" node.
408 //
409 setDestTo(CI, createNode(DSNode::UnknownNode));
410 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000411}
Chris Lattner055dc2c2002-07-18 15:54:42 +0000412
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000413
414
415
416//===----------------------------------------------------------------------===//
417// LocalDataStructures Implementation
418//===----------------------------------------------------------------------===//
419
Chris Lattneraa0b4682002-11-09 21:12:07 +0000420bool LocalDataStructures::run(Module &M) {
421 GlobalsGraph = new DSGraph();
422
423 // Calculate all of the graphs...
424 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
425 if (!I->isExternal())
426 DSInfo.insert(std::make_pair(I, new DSGraph(*I, GlobalsGraph)));
427 return false;
428}
429
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000430// releaseMemory - If the pass pipeline is done with this pass, we can release
431// our memory... here...
432//
433void LocalDataStructures::releaseMemory() {
Chris Lattner41c04f72003-02-01 04:52:08 +0000434 for (hash_map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000435 E = DSInfo.end(); I != E; ++I)
436 delete I->second;
437
438 // Empty map so next time memory is released, data structures are not
439 // re-deleted.
440 DSInfo.clear();
Chris Lattner2e4f9bf2002-11-09 20:01:01 +0000441 delete GlobalsGraph;
442 GlobalsGraph = 0;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000443}