blob: cd838d483ae7fd336c4a386a930211b74c901d51 [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 Lattnerc68c31b2002-07-10 22:38:08 +000028using std::map;
29using std::vector;
30
Chris Lattner97f51a32002-07-27 01:12:15 +000031static RegisterAnalysis<LocalDataStructures>
32X("datastructure", "Local Data Structure Analysis");
Chris Lattner97f51a32002-07-27 01:12:15 +000033
Chris Lattnerb1060432002-11-07 05:20:53 +000034namespace DS {
Chris Lattnerfccd06f2002-10-01 22:33:50 +000035 // FIXME: Do something smarter with target data!
36 TargetData TD("temp-td");
Chris Lattnerfccd06f2002-10-01 22:33:50 +000037
38 // isPointerType - Return true if this type is big enough to hold a pointer.
39 bool isPointerType(const Type *Ty) {
40 if (isa<PointerType>(Ty))
41 return true;
42 else if (Ty->isPrimitiveType() && Ty->isInteger())
43 return Ty->getPrimitiveSize() >= PointerSize;
44 return false;
45 }
46}
Chris Lattnerb1060432002-11-07 05:20:53 +000047using namespace DS;
Chris Lattnerfccd06f2002-10-01 22:33:50 +000048
Chris Lattnerc68c31b2002-07-10 22:38:08 +000049
50namespace {
Chris Lattnerfccd06f2002-10-01 22:33:50 +000051 //===--------------------------------------------------------------------===//
52 // GraphBuilder Class
53 //===--------------------------------------------------------------------===//
54 //
55 /// This class is the builder class that constructs the local data structure
56 /// graph by performing a single pass over the function in question.
57 ///
Chris Lattnerc68c31b2002-07-10 22:38:08 +000058 class GraphBuilder : InstVisitor<GraphBuilder> {
59 DSGraph &G;
60 vector<DSNode*> &Nodes;
61 DSNodeHandle &RetNode; // Node that gets returned...
Chris Lattnerc875f022002-11-03 21:27:48 +000062 map<Value*, DSNodeHandle> &ScalarMap;
Vikram S. Adve42fd1692002-10-20 18:07:37 +000063 vector<DSCallSite> &FunctionCalls;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000064
65 public:
66 GraphBuilder(DSGraph &g, vector<DSNode*> &nodes, DSNodeHandle &retNode,
Chris Lattnerc875f022002-11-03 21:27:48 +000067 map<Value*, DSNodeHandle> &SM,
Vikram S. Adve42fd1692002-10-20 18:07:37 +000068 vector<DSCallSite> &fc)
Chris Lattnerc875f022002-11-03 21:27:48 +000069 : G(g), Nodes(nodes), RetNode(retNode), ScalarMap(SM), FunctionCalls(fc) {
Chris Lattner0d9bab82002-07-18 00:12:30 +000070
71 // Create scalar nodes for all pointer arguments...
72 for (Function::aiterator I = G.getFunction().abegin(),
73 E = G.getFunction().aend(); I != E; ++I)
Chris Lattnerfccd06f2002-10-01 22:33:50 +000074 if (isPointerType(I->getType()))
75 getValueDest(*I);
Chris Lattner0d9bab82002-07-18 00:12:30 +000076
Chris Lattnerc68c31b2002-07-10 22:38:08 +000077 visit(G.getFunction()); // Single pass over the function
Chris Lattnerc68c31b2002-07-10 22:38:08 +000078 }
79
80 private:
81 // Visitor functions, used to handle each instruction type we encounter...
82 friend class InstVisitor<GraphBuilder>;
Chris Lattnerd18f3422002-11-03 21:24:04 +000083 void visitMallocInst(MallocInst &MI) { handleAlloc(MI, DSNode::HeapNode); }
Chris Lattnerc68c31b2002-07-10 22:38:08 +000084 void visitAllocaInst(AllocaInst &AI) { handleAlloc(AI, DSNode::AllocaNode);}
85 void handleAlloc(AllocationInst &AI, DSNode::NodeTy NT);
86
87 void visitPHINode(PHINode &PN);
88
Chris Lattner51340062002-11-08 05:00:44 +000089 void visitGetElementPtrInst(User &GEP);
Chris Lattnerc68c31b2002-07-10 22:38:08 +000090 void visitReturnInst(ReturnInst &RI);
91 void visitLoadInst(LoadInst &LI);
92 void visitStoreInst(StoreInst &SI);
93 void visitCallInst(CallInst &CI);
94 void visitSetCondInst(SetCondInst &SCI) {} // SetEQ & friends are ignored
95 void visitFreeInst(FreeInst &FI) {} // Ignore free instructions
Chris Lattnerfccd06f2002-10-01 22:33:50 +000096 void visitCastInst(CastInst &CI);
97 void visitInstruction(Instruction &I) {}
Chris Lattnerc68c31b2002-07-10 22:38:08 +000098
99 private:
100 // Helper functions used to implement the visitation functions...
101
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000102 /// createNode - Create a new DSNode, ensuring that it is properly added to
103 /// the graph.
104 ///
Chris Lattner92673292002-11-02 00:13:20 +0000105 DSNode *createNode(DSNode::NodeTy NodeType, const Type *Ty = 0) {
106 DSNode *N = new DSNode(NodeType, Ty); // Create the node
107 Nodes.push_back(N); // Add node to nodes list
108 return N;
109 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000110
Chris Lattnerc875f022002-11-03 21:27:48 +0000111 /// setDestTo - Set the ScalarMap entry for the specified value to point to
Chris Lattner92673292002-11-02 00:13:20 +0000112 /// the specified destination. If the Value already points to a node, make
113 /// sure to merge the two destinations together.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000114 ///
Chris Lattner92673292002-11-02 00:13:20 +0000115 void setDestTo(Value &V, const DSNodeHandle &NH);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000116
Chris Lattner92673292002-11-02 00:13:20 +0000117 /// getValueDest - Return the DSNode that the actual value points to.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000118 ///
Chris Lattner92673292002-11-02 00:13:20 +0000119 DSNodeHandle getValueDest(Value &V);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000120
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000121 /// getLink - This method is used to return the specified link in the
122 /// specified node if one exists. If a link does not already exist (it's
Chris Lattner7e51c872002-10-31 06:52:26 +0000123 /// null), then we create a new node, link it, then return it.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000124 ///
Chris Lattner7e51c872002-10-31 06:52:26 +0000125 DSNodeHandle &getLink(const DSNodeHandle &Node, unsigned Link = 0);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000126 };
127}
128
129//===----------------------------------------------------------------------===//
130// DSGraph constructor - Simply use the GraphBuilder to construct the local
131// graph.
Chris Lattner2e4f9bf2002-11-09 20:01:01 +0000132DSGraph::DSGraph(Function &F, DSGraph *GG) : Func(&F), GlobalsGraph(GG) {
Chris Lattner2a068862002-11-10 06:53:38 +0000133 PrintAuxCalls = false;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000134 // Use the graph builder to construct the local version of the graph
Chris Lattnerc875f022002-11-03 21:27:48 +0000135 GraphBuilder B(*this, Nodes, RetNode, ScalarMap, FunctionCalls);
Chris Lattner4fe34612002-11-18 21:44:19 +0000136#ifndef NDEBUG
137 Timer::addPeakMemoryMeasurement();
138#endif
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000139 markIncompleteNodes();
Chris Lattner96517252002-11-09 20:55:24 +0000140
141 // Remove any nodes made dead due to merging...
Chris Lattnerf40f0a32002-11-09 22:07:02 +0000142 removeDeadNodes();
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000143}
144
145
146//===----------------------------------------------------------------------===//
147// Helper method implementations...
148//
149
Chris Lattner92673292002-11-02 00:13:20 +0000150/// getValueDest - Return the DSNode that the actual value points to.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000151///
Chris Lattner51340062002-11-08 05:00:44 +0000152DSNodeHandle GraphBuilder::getValueDest(Value &Val) {
153 Value *V = &Val;
154 if (V == Constant::getNullValue(V->getType()))
155 return 0; // Null doesn't point to anything, don't add to ScalarMap!
Chris Lattner92673292002-11-02 00:13:20 +0000156
Chris Lattner51340062002-11-08 05:00:44 +0000157 if (Constant *C = dyn_cast<Constant>(V))
158 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
Chris Lattner2cfbaaf2002-11-09 20:14:03 +0000159 return getValueDest(*CPR->getValue());
Chris Lattner51340062002-11-08 05:00:44 +0000160 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
161 if (CE->getOpcode() == Instruction::Cast)
Chris Lattner2cfbaaf2002-11-09 20:14:03 +0000162 return getValueDest(*CE->getOperand(0));
Chris Lattner51340062002-11-08 05:00:44 +0000163 if (CE->getOpcode() == Instruction::GetElementPtr) {
164 visitGetElementPtrInst(*CE);
Chris Lattner2cfbaaf2002-11-09 20:14:03 +0000165 std::map<Value*, DSNodeHandle>::iterator I = ScalarMap.find(CE);
166 assert(I != ScalarMap.end() && "GEP didn't get processed right?");
167 DSNodeHandle NH = I->second;
168 ScalarMap.erase(I); // Remove constant from scalarmap
169 return NH;
Chris Lattner51340062002-11-08 05:00:44 +0000170 }
171
172 // This returns a conservative unknown node for any unhandled ConstExpr
Chris Lattner2cfbaaf2002-11-09 20:14:03 +0000173 return createNode(DSNode::UnknownNode);
Chris Lattner51340062002-11-08 05:00:44 +0000174 } else if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(C)) {
175 // Random constants are unknown mem
Chris Lattner2cfbaaf2002-11-09 20:14:03 +0000176 return createNode(DSNode::UnknownNode);
Chris Lattner51340062002-11-08 05:00:44 +0000177 } else {
178 assert(0 && "Unknown constant type!");
179 }
180
Chris Lattner2cfbaaf2002-11-09 20:14:03 +0000181 DSNodeHandle &NH = ScalarMap[V];
182 if (NH.getNode())
183 return NH; // Already have a node? Just return it...
184
Chris Lattner92673292002-11-02 00:13:20 +0000185 // Otherwise we need to create a new node to point to...
186 DSNode *N;
Chris Lattner51340062002-11-08 05:00:44 +0000187 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner92673292002-11-02 00:13:20 +0000188 // Create a new global node for this global variable...
189 N = createNode(DSNode::GlobalNode, GV->getType()->getElementType());
190 N->addGlobal(GV);
191 } else {
192 // Otherwise just create a shadow node
193 N = createNode(DSNode::ShadowNode);
194 }
195
196 NH.setNode(N); // Remember that we are pointing to it...
197 NH.setOffset(0);
198 return NH;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000199}
200
Chris Lattner0d9bab82002-07-18 00:12:30 +0000201
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000202/// getLink - This method is used to return the specified link in the
203/// specified node if one exists. If a link does not already exist (it's
204/// null), then we create a new node, link it, then return it. We must
205/// specify the type of the Node field we are accessing so that we know what
206/// type should be linked to if we need to create a new node.
207///
Chris Lattner7e51c872002-10-31 06:52:26 +0000208DSNodeHandle &GraphBuilder::getLink(const DSNodeHandle &node, unsigned LinkNo) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000209 DSNodeHandle &Node = const_cast<DSNodeHandle&>(node);
Chris Lattner08db7192002-11-06 06:20:27 +0000210 DSNodeHandle &Link = Node.getLink(LinkNo);
211 if (!Link.getNode()) {
212 // If the link hasn't been created yet, make and return a new shadow node
213 Link = createNode(DSNode::ShadowNode);
214 }
215 return Link;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000216}
217
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000218
Chris Lattnerc875f022002-11-03 21:27:48 +0000219/// setDestTo - Set the ScalarMap entry for the specified value to point to the
Chris Lattner92673292002-11-02 00:13:20 +0000220/// specified destination. If the Value already points to a node, make sure to
221/// merge the two destinations together.
222///
223void GraphBuilder::setDestTo(Value &V, const DSNodeHandle &NH) {
Chris Lattnerc875f022002-11-03 21:27:48 +0000224 DSNodeHandle &AINH = ScalarMap[&V];
Chris Lattner92673292002-11-02 00:13:20 +0000225 if (AINH.getNode() == 0) // Not pointing to anything yet?
226 AINH = NH; // Just point directly to NH
227 else
228 AINH.mergeWith(NH);
229}
230
231
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000232//===----------------------------------------------------------------------===//
233// Specific instruction type handler implementations...
234//
235
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000236/// Alloca & Malloc instruction implementation - Simply create a new memory
237/// object, pointing the scalar to it.
238///
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000239void GraphBuilder::handleAlloc(AllocationInst &AI, DSNode::NodeTy NodeType) {
Chris Lattner92673292002-11-02 00:13:20 +0000240 setDestTo(AI, createNode(NodeType));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000241}
242
243// PHINode - Make the scalar for the PHI node point to all of the things the
244// incoming values point to... which effectively causes them to be merged.
245//
246void GraphBuilder::visitPHINode(PHINode &PN) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000247 if (!isPointerType(PN.getType())) return; // Only pointer PHIs
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000248
Chris Lattnerc875f022002-11-03 21:27:48 +0000249 DSNodeHandle &PNDest = ScalarMap[&PN];
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000250 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
Chris Lattner92673292002-11-02 00:13:20 +0000251 PNDest.mergeWith(getValueDest(*PN.getIncomingValue(i)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000252}
253
Chris Lattner51340062002-11-08 05:00:44 +0000254void GraphBuilder::visitGetElementPtrInst(User &GEP) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000255 DSNodeHandle Value = getValueDest(*GEP.getOperand(0));
Chris Lattner92673292002-11-02 00:13:20 +0000256 if (Value.getNode() == 0) return;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000257
258 unsigned Offset = 0;
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000259 const PointerType *PTy = cast<PointerType>(GEP.getOperand(0)->getType());
260 const Type *CurTy = PTy->getElementType();
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000261
Chris Lattner08db7192002-11-06 06:20:27 +0000262 if (Value.getNode()->mergeTypeInfo(CurTy, Value.getOffset())) {
263 // If the node had to be folded... exit quickly
Chris Lattner92673292002-11-02 00:13:20 +0000264 setDestTo(GEP, Value); // GEP result points to folded node
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000265 return;
266 }
267
Chris Lattner08db7192002-11-06 06:20:27 +0000268#if 0
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000269 // Handle the pointer index specially...
270 if (GEP.getNumOperands() > 1 &&
271 GEP.getOperand(1) != ConstantSInt::getNullValue(Type::LongTy)) {
272
273 // If we already know this is an array being accessed, don't do anything...
274 if (!TopTypeRec.isArray) {
275 TopTypeRec.isArray = true;
276
277 // If we are treating some inner field pointer as an array, fold the node
278 // up because we cannot handle it right. This can come because of
279 // something like this: &((&Pt->X)[1]) == &Pt->Y
280 //
281 if (Value.getOffset()) {
282 // Value is now the pointer we want to GEP to be...
283 Value.getNode()->foldNodeCompletely();
Chris Lattner92673292002-11-02 00:13:20 +0000284 setDestTo(GEP, Value); // GEP result points to folded node
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000285 return;
286 } else {
287 // This is a pointer to the first byte of the node. Make sure that we
288 // are pointing to the outter most type in the node.
289 // FIXME: We need to check one more case here...
290 }
291 }
292 }
Chris Lattner08db7192002-11-06 06:20:27 +0000293#endif
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000294
295 // All of these subscripts are indexing INTO the elements we have...
296 for (unsigned i = 2, e = GEP.getNumOperands(); i < e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000297 if (GEP.getOperand(i)->getType() == Type::LongTy) {
Chris Lattnere03f32b2002-10-02 06:24:36 +0000298 // Get the type indexing into...
299 const SequentialType *STy = cast<SequentialType>(CurTy);
300 CurTy = STy->getElementType();
Chris Lattner08db7192002-11-06 06:20:27 +0000301#if 0
Chris Lattnere03f32b2002-10-02 06:24:36 +0000302 if (ConstantSInt *CS = dyn_cast<ConstantSInt>(GEP.getOperand(i))) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000303 Offset += CS->getValue()*TD.getTypeSize(CurTy);
Chris Lattnere03f32b2002-10-02 06:24:36 +0000304 } else {
305 // Variable index into a node. We must merge all of the elements of the
306 // sequential type here.
307 if (isa<PointerType>(STy))
308 std::cerr << "Pointer indexing not handled yet!\n";
309 else {
310 const ArrayType *ATy = cast<ArrayType>(STy);
311 unsigned ElSize = TD.getTypeSize(CurTy);
312 DSNode *N = Value.getNode();
313 assert(N && "Value must have a node!");
314 unsigned RawOffset = Offset+Value.getOffset();
315
316 // Loop over all of the elements of the array, merging them into the
317 // zero'th element.
318 for (unsigned i = 1, e = ATy->getNumElements(); i != e; ++i)
319 // Merge all of the byte components of this array element
320 for (unsigned j = 0; j != ElSize; ++j)
321 N->mergeIndexes(RawOffset+j, RawOffset+i*ElSize+j);
322 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000323 }
Chris Lattner08db7192002-11-06 06:20:27 +0000324#endif
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000325 } else if (GEP.getOperand(i)->getType() == Type::UByteTy) {
326 unsigned FieldNo = cast<ConstantUInt>(GEP.getOperand(i))->getValue();
327 const StructType *STy = cast<StructType>(CurTy);
328 Offset += TD.getStructLayout(STy)->MemberOffsets[FieldNo];
329 CurTy = STy->getContainedType(FieldNo);
330 }
331
332 // Add in the offset calculated...
333 Value.setOffset(Value.getOffset()+Offset);
334
335 // Value is now the pointer we want to GEP to be...
Chris Lattner92673292002-11-02 00:13:20 +0000336 setDestTo(GEP, Value);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000337}
338
339void GraphBuilder::visitLoadInst(LoadInst &LI) {
Chris Lattner92673292002-11-02 00:13:20 +0000340 DSNodeHandle Ptr = getValueDest(*LI.getOperand(0));
341 if (Ptr.getNode() == 0) return;
342
343 // Make that the node is read from...
Chris Lattner06285232002-10-17 22:13:19 +0000344 Ptr.getNode()->NodeType |= DSNode::Read;
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000345
346 // Ensure a typerecord exists...
Chris Lattner08db7192002-11-06 06:20:27 +0000347 Ptr.getNode()->mergeTypeInfo(LI.getType(), Ptr.getOffset());
Chris Lattner92673292002-11-02 00:13:20 +0000348
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000349 if (isPointerType(LI.getType()))
Chris Lattner92673292002-11-02 00:13:20 +0000350 setDestTo(LI, getLink(Ptr));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000351}
352
353void GraphBuilder::visitStoreInst(StoreInst &SI) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000354 const Type *StoredTy = SI.getOperand(0)->getType();
Chris Lattner92673292002-11-02 00:13:20 +0000355 DSNodeHandle Dest = getValueDest(*SI.getOperand(1));
356 if (Dest.getNode() == 0) return;
357
358 // Make that the node is written to...
359 Dest.getNode()->NodeType |= DSNode::Modified;
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000360
361 // Ensure a typerecord exists...
Chris Lattner08db7192002-11-06 06:20:27 +0000362 Dest.getNode()->mergeTypeInfo(StoredTy, Dest.getOffset());
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000363
364 // Avoid adding edges from null, or processing non-"pointer" stores
Chris Lattner92673292002-11-02 00:13:20 +0000365 if (isPointerType(StoredTy))
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000366 Dest.addEdgeTo(getValueDest(*SI.getOperand(0)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000367}
368
369void GraphBuilder::visitReturnInst(ReturnInst &RI) {
Chris Lattner92673292002-11-02 00:13:20 +0000370 if (RI.getNumOperands() && isPointerType(RI.getOperand(0)->getType()))
371 RetNode.mergeWith(getValueDest(*RI.getOperand(0)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000372}
373
374void GraphBuilder::visitCallInst(CallInst &CI) {
Chris Lattnerc314ac42002-07-11 20:32:02 +0000375 // Set up the return value...
Chris Lattner0969c502002-10-21 02:08:03 +0000376 DSNodeHandle RetVal;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000377 if (isPointerType(CI.getType()))
Chris Lattner92673292002-11-02 00:13:20 +0000378 RetVal = getValueDest(CI);
Chris Lattnerc314ac42002-07-11 20:32:02 +0000379
Chris Lattner92673292002-11-02 00:13:20 +0000380 DSNodeHandle Callee = getValueDest(*CI.getOperand(0));
Chris Lattner0d9bab82002-07-18 00:12:30 +0000381
Chris Lattner0969c502002-10-21 02:08:03 +0000382 std::vector<DSNodeHandle> Args;
383 Args.reserve(CI.getNumOperands()-1);
384
385 // Calculate the arguments vector...
386 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000387 if (isPointerType(CI.getOperand(i)->getType()))
Chris Lattner92673292002-11-02 00:13:20 +0000388 Args.push_back(getValueDest(*CI.getOperand(i)));
Chris Lattner0969c502002-10-21 02:08:03 +0000389
390 // Add a new function call entry...
391 FunctionCalls.push_back(DSCallSite(CI, RetVal, Callee, Args));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000392}
Chris Lattner055dc2c2002-07-18 15:54:42 +0000393
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000394/// Handle casts...
395void GraphBuilder::visitCastInst(CastInst &CI) {
Chris Lattner5af344d2002-11-02 00:36:03 +0000396 if (isPointerType(CI.getType()))
397 if (isPointerType(CI.getOperand(0)->getType())) {
398 // Cast one pointer to the other, just act like a copy instruction
Chris Lattner92673292002-11-02 00:13:20 +0000399 setDestTo(CI, getValueDest(*CI.getOperand(0)));
Chris Lattner5af344d2002-11-02 00:36:03 +0000400 } else {
401 // Cast something (floating point, small integer) to a pointer. We need
402 // to track the fact that the node points to SOMETHING, just something we
403 // don't know about. Make an "Unknown" node.
404 //
405 setDestTo(CI, createNode(DSNode::UnknownNode));
406 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000407}
Chris Lattner055dc2c2002-07-18 15:54:42 +0000408
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000409
410
411
412//===----------------------------------------------------------------------===//
413// LocalDataStructures Implementation
414//===----------------------------------------------------------------------===//
415
Chris Lattneraa0b4682002-11-09 21:12:07 +0000416bool LocalDataStructures::run(Module &M) {
417 GlobalsGraph = new DSGraph();
418
419 // Calculate all of the graphs...
420 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
421 if (!I->isExternal())
422 DSInfo.insert(std::make_pair(I, new DSGraph(*I, GlobalsGraph)));
423 return false;
424}
425
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000426// releaseMemory - If the pass pipeline is done with this pass, we can release
427// our memory... here...
428//
429void LocalDataStructures::releaseMemory() {
430 for (std::map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
431 E = DSInfo.end(); I != E; ++I)
432 delete I->second;
433
434 // Empty map so next time memory is released, data structures are not
435 // re-deleted.
436 DSInfo.clear();
Chris Lattner2e4f9bf2002-11-09 20:01:01 +0000437 delete GlobalsGraph;
438 GlobalsGraph = 0;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000439}