blob: e731d5cb7bbf13a37804d2601158e3ce517acf2d [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"
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 Lattnerb1060432002-11-07 05:20:53 +000033namespace DS {
Chris Lattnerfccd06f2002-10-01 22:33:50 +000034 // FIXME: Do something smarter with target data!
35 TargetData TD("temp-td");
Chris Lattnerfccd06f2002-10-01 22:33:50 +000036
37 // isPointerType - Return true if this type is big enough to hold a pointer.
38 bool isPointerType(const Type *Ty) {
39 if (isa<PointerType>(Ty))
40 return true;
41 else if (Ty->isPrimitiveType() && Ty->isInteger())
42 return Ty->getPrimitiveSize() >= PointerSize;
43 return false;
44 }
45}
Chris Lattnerb1060432002-11-07 05:20:53 +000046using namespace DS;
Chris Lattnerfccd06f2002-10-01 22:33:50 +000047
Chris Lattnerc68c31b2002-07-10 22:38:08 +000048
49namespace {
Chris Lattnerfccd06f2002-10-01 22:33:50 +000050 //===--------------------------------------------------------------------===//
51 // GraphBuilder Class
52 //===--------------------------------------------------------------------===//
53 //
54 /// This class is the builder class that constructs the local data structure
55 /// graph by performing a single pass over the function in question.
56 ///
Chris Lattnerc68c31b2002-07-10 22:38:08 +000057 class GraphBuilder : InstVisitor<GraphBuilder> {
58 DSGraph &G;
59 vector<DSNode*> &Nodes;
60 DSNodeHandle &RetNode; // Node that gets returned...
Chris Lattnerc875f022002-11-03 21:27:48 +000061 map<Value*, DSNodeHandle> &ScalarMap;
Vikram S. Adve42fd1692002-10-20 18:07:37 +000062 vector<DSCallSite> &FunctionCalls;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000063
64 public:
65 GraphBuilder(DSGraph &g, vector<DSNode*> &nodes, DSNodeHandle &retNode,
Chris Lattnerc875f022002-11-03 21:27:48 +000066 map<Value*, DSNodeHandle> &SM,
Vikram S. Adve42fd1692002-10-20 18:07:37 +000067 vector<DSCallSite> &fc)
Chris Lattnerc875f022002-11-03 21:27:48 +000068 : G(g), Nodes(nodes), RetNode(retNode), ScalarMap(SM), FunctionCalls(fc) {
Chris Lattner0d9bab82002-07-18 00:12:30 +000069
70 // Create scalar nodes for all pointer arguments...
71 for (Function::aiterator I = G.getFunction().abegin(),
72 E = G.getFunction().aend(); I != E; ++I)
Chris Lattnerfccd06f2002-10-01 22:33:50 +000073 if (isPointerType(I->getType()))
74 getValueDest(*I);
Chris Lattner0d9bab82002-07-18 00:12:30 +000075
Chris Lattnerc68c31b2002-07-10 22:38:08 +000076 visit(G.getFunction()); // Single pass over the function
Chris Lattner2a2c4902002-07-18 18:19:09 +000077
78 // Not inlining, only eliminate trivially dead nodes.
79 G.removeTriviallyDeadNodes();
Chris Lattnerc68c31b2002-07-10 22:38:08 +000080 }
81
82 private:
83 // Visitor functions, used to handle each instruction type we encounter...
84 friend class InstVisitor<GraphBuilder>;
Chris Lattnerd18f3422002-11-03 21:24:04 +000085 void visitMallocInst(MallocInst &MI) { handleAlloc(MI, DSNode::HeapNode); }
Chris Lattnerc68c31b2002-07-10 22:38:08 +000086 void visitAllocaInst(AllocaInst &AI) { handleAlloc(AI, DSNode::AllocaNode);}
87 void handleAlloc(AllocationInst &AI, DSNode::NodeTy NT);
88
89 void visitPHINode(PHINode &PN);
90
Chris Lattner51340062002-11-08 05:00:44 +000091 void visitGetElementPtrInst(User &GEP);
Chris Lattnerc68c31b2002-07-10 22:38:08 +000092 void visitReturnInst(ReturnInst &RI);
93 void visitLoadInst(LoadInst &LI);
94 void visitStoreInst(StoreInst &SI);
95 void visitCallInst(CallInst &CI);
96 void visitSetCondInst(SetCondInst &SCI) {} // SetEQ & friends are ignored
97 void visitFreeInst(FreeInst &FI) {} // Ignore free instructions
Chris Lattnerfccd06f2002-10-01 22:33:50 +000098 void visitCastInst(CastInst &CI);
99 void visitInstruction(Instruction &I) {}
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000100
101 private:
102 // Helper functions used to implement the visitation functions...
103
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000104 /// createNode - Create a new DSNode, ensuring that it is properly added to
105 /// the graph.
106 ///
Chris Lattner92673292002-11-02 00:13:20 +0000107 DSNode *createNode(DSNode::NodeTy NodeType, const Type *Ty = 0) {
108 DSNode *N = new DSNode(NodeType, Ty); // Create the node
109 Nodes.push_back(N); // Add node to nodes list
110 return N;
111 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000112
Chris Lattnerc875f022002-11-03 21:27:48 +0000113 /// setDestTo - Set the ScalarMap entry for the specified value to point to
Chris Lattner92673292002-11-02 00:13:20 +0000114 /// the specified destination. If the Value already points to a node, make
115 /// sure to merge the two destinations together.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000116 ///
Chris Lattner92673292002-11-02 00:13:20 +0000117 void setDestTo(Value &V, const DSNodeHandle &NH);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000118
Chris Lattner92673292002-11-02 00:13:20 +0000119 /// getValueDest - Return the DSNode that the actual value points to.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000120 ///
Chris Lattner92673292002-11-02 00:13:20 +0000121 DSNodeHandle getValueDest(Value &V);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000122
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000123 /// getLink - This method is used to return the specified link in the
124 /// specified node if one exists. If a link does not already exist (it's
Chris Lattner7e51c872002-10-31 06:52:26 +0000125 /// null), then we create a new node, link it, then return it.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000126 ///
Chris Lattner7e51c872002-10-31 06:52:26 +0000127 DSNodeHandle &getLink(const DSNodeHandle &Node, unsigned Link = 0);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000128 };
129}
130
131//===----------------------------------------------------------------------===//
132// DSGraph constructor - Simply use the GraphBuilder to construct the local
133// graph.
Chris Lattner2e4f9bf2002-11-09 20:01:01 +0000134DSGraph::DSGraph(Function &F, DSGraph *GG) : Func(&F), GlobalsGraph(GG) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000135 // Use the graph builder to construct the local version of the graph
Chris Lattnerc875f022002-11-03 21:27:48 +0000136 GraphBuilder B(*this, Nodes, RetNode, ScalarMap, FunctionCalls);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000137 markIncompleteNodes();
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000138}
139
140
141//===----------------------------------------------------------------------===//
142// Helper method implementations...
143//
144
145
Chris Lattner92673292002-11-02 00:13:20 +0000146/// getValueDest - Return the DSNode that the actual value points to.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000147///
Chris Lattner51340062002-11-08 05:00:44 +0000148DSNodeHandle GraphBuilder::getValueDest(Value &Val) {
149 Value *V = &Val;
150 if (V == Constant::getNullValue(V->getType()))
151 return 0; // Null doesn't point to anything, don't add to ScalarMap!
Chris Lattner92673292002-11-02 00:13:20 +0000152
Chris Lattner51340062002-11-08 05:00:44 +0000153 if (Constant *C = dyn_cast<Constant>(V))
154 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
Chris Lattner2cfbaaf2002-11-09 20:14:03 +0000155 return getValueDest(*CPR->getValue());
Chris Lattner51340062002-11-08 05:00:44 +0000156 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
157 if (CE->getOpcode() == Instruction::Cast)
Chris Lattner2cfbaaf2002-11-09 20:14:03 +0000158 return getValueDest(*CE->getOperand(0));
Chris Lattner51340062002-11-08 05:00:44 +0000159 if (CE->getOpcode() == Instruction::GetElementPtr) {
160 visitGetElementPtrInst(*CE);
Chris Lattner2cfbaaf2002-11-09 20:14:03 +0000161 std::map<Value*, DSNodeHandle>::iterator I = ScalarMap.find(CE);
162 assert(I != ScalarMap.end() && "GEP didn't get processed right?");
163 DSNodeHandle NH = I->second;
164 ScalarMap.erase(I); // Remove constant from scalarmap
165 return NH;
Chris Lattner51340062002-11-08 05:00:44 +0000166 }
167
168 // This returns a conservative unknown node for any unhandled ConstExpr
Chris Lattner2cfbaaf2002-11-09 20:14:03 +0000169 return createNode(DSNode::UnknownNode);
Chris Lattner51340062002-11-08 05:00:44 +0000170 } else if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(C)) {
171 // Random constants are unknown mem
Chris Lattner2cfbaaf2002-11-09 20:14:03 +0000172 return createNode(DSNode::UnknownNode);
Chris Lattner51340062002-11-08 05:00:44 +0000173 } else {
174 assert(0 && "Unknown constant type!");
175 }
176
Chris Lattner2cfbaaf2002-11-09 20:14:03 +0000177 DSNodeHandle &NH = ScalarMap[V];
178 if (NH.getNode())
179 return NH; // Already have a node? Just return it...
180
Chris Lattner92673292002-11-02 00:13:20 +0000181 // Otherwise we need to create a new node to point to...
182 DSNode *N;
Chris Lattner51340062002-11-08 05:00:44 +0000183 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner92673292002-11-02 00:13:20 +0000184 // Create a new global node for this global variable...
185 N = createNode(DSNode::GlobalNode, GV->getType()->getElementType());
186 N->addGlobal(GV);
187 } else {
188 // Otherwise just create a shadow node
189 N = createNode(DSNode::ShadowNode);
190 }
191
192 NH.setNode(N); // Remember that we are pointing to it...
193 NH.setOffset(0);
194 return NH;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000195}
196
Chris Lattner0d9bab82002-07-18 00:12:30 +0000197
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000198/// getLink - This method is used to return the specified link in the
199/// specified node if one exists. If a link does not already exist (it's
200/// null), then we create a new node, link it, then return it. We must
201/// specify the type of the Node field we are accessing so that we know what
202/// type should be linked to if we need to create a new node.
203///
Chris Lattner7e51c872002-10-31 06:52:26 +0000204DSNodeHandle &GraphBuilder::getLink(const DSNodeHandle &node, unsigned LinkNo) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000205 DSNodeHandle &Node = const_cast<DSNodeHandle&>(node);
Chris Lattner08db7192002-11-06 06:20:27 +0000206 DSNodeHandle &Link = Node.getLink(LinkNo);
207 if (!Link.getNode()) {
208 // If the link hasn't been created yet, make and return a new shadow node
209 Link = createNode(DSNode::ShadowNode);
210 }
211 return Link;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000212}
213
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000214
Chris Lattnerc875f022002-11-03 21:27:48 +0000215/// setDestTo - Set the ScalarMap entry for the specified value to point to the
Chris Lattner92673292002-11-02 00:13:20 +0000216/// specified destination. If the Value already points to a node, make sure to
217/// merge the two destinations together.
218///
219void GraphBuilder::setDestTo(Value &V, const DSNodeHandle &NH) {
Chris Lattnerc875f022002-11-03 21:27:48 +0000220 DSNodeHandle &AINH = ScalarMap[&V];
Chris Lattner92673292002-11-02 00:13:20 +0000221 if (AINH.getNode() == 0) // Not pointing to anything yet?
222 AINH = NH; // Just point directly to NH
223 else
224 AINH.mergeWith(NH);
225}
226
227
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000228//===----------------------------------------------------------------------===//
229// Specific instruction type handler implementations...
230//
231
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000232/// Alloca & Malloc instruction implementation - Simply create a new memory
233/// object, pointing the scalar to it.
234///
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000235void GraphBuilder::handleAlloc(AllocationInst &AI, DSNode::NodeTy NodeType) {
Chris Lattner92673292002-11-02 00:13:20 +0000236 setDestTo(AI, createNode(NodeType));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000237}
238
239// PHINode - Make the scalar for the PHI node point to all of the things the
240// incoming values point to... which effectively causes them to be merged.
241//
242void GraphBuilder::visitPHINode(PHINode &PN) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000243 if (!isPointerType(PN.getType())) return; // Only pointer PHIs
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000244
Chris Lattnerc875f022002-11-03 21:27:48 +0000245 DSNodeHandle &PNDest = ScalarMap[&PN];
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000246 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
Chris Lattner92673292002-11-02 00:13:20 +0000247 PNDest.mergeWith(getValueDest(*PN.getIncomingValue(i)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000248}
249
Chris Lattner51340062002-11-08 05:00:44 +0000250void GraphBuilder::visitGetElementPtrInst(User &GEP) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000251 DSNodeHandle Value = getValueDest(*GEP.getOperand(0));
Chris Lattner92673292002-11-02 00:13:20 +0000252 if (Value.getNode() == 0) return;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000253
254 unsigned Offset = 0;
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000255 const PointerType *PTy = cast<PointerType>(GEP.getOperand(0)->getType());
256 const Type *CurTy = PTy->getElementType();
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000257
Chris Lattner08db7192002-11-06 06:20:27 +0000258 if (Value.getNode()->mergeTypeInfo(CurTy, Value.getOffset())) {
259 // If the node had to be folded... exit quickly
Chris Lattner92673292002-11-02 00:13:20 +0000260 setDestTo(GEP, Value); // GEP result points to folded node
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000261 return;
262 }
263
Chris Lattner08db7192002-11-06 06:20:27 +0000264#if 0
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000265 // Handle the pointer index specially...
266 if (GEP.getNumOperands() > 1 &&
267 GEP.getOperand(1) != ConstantSInt::getNullValue(Type::LongTy)) {
268
269 // If we already know this is an array being accessed, don't do anything...
270 if (!TopTypeRec.isArray) {
271 TopTypeRec.isArray = true;
272
273 // If we are treating some inner field pointer as an array, fold the node
274 // up because we cannot handle it right. This can come because of
275 // something like this: &((&Pt->X)[1]) == &Pt->Y
276 //
277 if (Value.getOffset()) {
278 // Value is now the pointer we want to GEP to be...
279 Value.getNode()->foldNodeCompletely();
Chris Lattner92673292002-11-02 00:13:20 +0000280 setDestTo(GEP, Value); // GEP result points to folded node
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000281 return;
282 } else {
283 // This is a pointer to the first byte of the node. Make sure that we
284 // are pointing to the outter most type in the node.
285 // FIXME: We need to check one more case here...
286 }
287 }
288 }
Chris Lattner08db7192002-11-06 06:20:27 +0000289#endif
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000290
291 // All of these subscripts are indexing INTO the elements we have...
292 for (unsigned i = 2, e = GEP.getNumOperands(); i < e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000293 if (GEP.getOperand(i)->getType() == Type::LongTy) {
Chris Lattnere03f32b2002-10-02 06:24:36 +0000294 // Get the type indexing into...
295 const SequentialType *STy = cast<SequentialType>(CurTy);
296 CurTy = STy->getElementType();
Chris Lattner08db7192002-11-06 06:20:27 +0000297#if 0
Chris Lattnere03f32b2002-10-02 06:24:36 +0000298 if (ConstantSInt *CS = dyn_cast<ConstantSInt>(GEP.getOperand(i))) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000299 Offset += CS->getValue()*TD.getTypeSize(CurTy);
Chris Lattnere03f32b2002-10-02 06:24:36 +0000300 } else {
301 // Variable index into a node. We must merge all of the elements of the
302 // sequential type here.
303 if (isa<PointerType>(STy))
304 std::cerr << "Pointer indexing not handled yet!\n";
305 else {
306 const ArrayType *ATy = cast<ArrayType>(STy);
307 unsigned ElSize = TD.getTypeSize(CurTy);
308 DSNode *N = Value.getNode();
309 assert(N && "Value must have a node!");
310 unsigned RawOffset = Offset+Value.getOffset();
311
312 // Loop over all of the elements of the array, merging them into the
313 // zero'th element.
314 for (unsigned i = 1, e = ATy->getNumElements(); i != e; ++i)
315 // Merge all of the byte components of this array element
316 for (unsigned j = 0; j != ElSize; ++j)
317 N->mergeIndexes(RawOffset+j, RawOffset+i*ElSize+j);
318 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000319 }
Chris Lattner08db7192002-11-06 06:20:27 +0000320#endif
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000321 } else if (GEP.getOperand(i)->getType() == Type::UByteTy) {
322 unsigned FieldNo = cast<ConstantUInt>(GEP.getOperand(i))->getValue();
323 const StructType *STy = cast<StructType>(CurTy);
324 Offset += TD.getStructLayout(STy)->MemberOffsets[FieldNo];
325 CurTy = STy->getContainedType(FieldNo);
326 }
327
328 // Add in the offset calculated...
329 Value.setOffset(Value.getOffset()+Offset);
330
331 // Value is now the pointer we want to GEP to be...
Chris Lattner92673292002-11-02 00:13:20 +0000332 setDestTo(GEP, Value);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000333}
334
335void GraphBuilder::visitLoadInst(LoadInst &LI) {
Chris Lattner92673292002-11-02 00:13:20 +0000336 DSNodeHandle Ptr = getValueDest(*LI.getOperand(0));
337 if (Ptr.getNode() == 0) return;
338
339 // Make that the node is read from...
Chris Lattner06285232002-10-17 22:13:19 +0000340 Ptr.getNode()->NodeType |= DSNode::Read;
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000341
342 // Ensure a typerecord exists...
Chris Lattner08db7192002-11-06 06:20:27 +0000343 Ptr.getNode()->mergeTypeInfo(LI.getType(), Ptr.getOffset());
Chris Lattner92673292002-11-02 00:13:20 +0000344
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000345 if (isPointerType(LI.getType()))
Chris Lattner92673292002-11-02 00:13:20 +0000346 setDestTo(LI, getLink(Ptr));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000347}
348
349void GraphBuilder::visitStoreInst(StoreInst &SI) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000350 const Type *StoredTy = SI.getOperand(0)->getType();
Chris Lattner92673292002-11-02 00:13:20 +0000351 DSNodeHandle Dest = getValueDest(*SI.getOperand(1));
352 if (Dest.getNode() == 0) return;
353
354 // Make that the node is written to...
355 Dest.getNode()->NodeType |= DSNode::Modified;
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000356
357 // Ensure a typerecord exists...
Chris Lattner08db7192002-11-06 06:20:27 +0000358 Dest.getNode()->mergeTypeInfo(StoredTy, Dest.getOffset());
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000359
360 // Avoid adding edges from null, or processing non-"pointer" stores
Chris Lattner92673292002-11-02 00:13:20 +0000361 if (isPointerType(StoredTy))
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000362 Dest.addEdgeTo(getValueDest(*SI.getOperand(0)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000363}
364
365void GraphBuilder::visitReturnInst(ReturnInst &RI) {
Chris Lattner92673292002-11-02 00:13:20 +0000366 if (RI.getNumOperands() && isPointerType(RI.getOperand(0)->getType()))
367 RetNode.mergeWith(getValueDest(*RI.getOperand(0)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000368}
369
370void GraphBuilder::visitCallInst(CallInst &CI) {
Chris Lattnerc314ac42002-07-11 20:32:02 +0000371 // Set up the return value...
Chris Lattner0969c502002-10-21 02:08:03 +0000372 DSNodeHandle RetVal;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000373 if (isPointerType(CI.getType()))
Chris Lattner92673292002-11-02 00:13:20 +0000374 RetVal = getValueDest(CI);
Chris Lattnerc314ac42002-07-11 20:32:02 +0000375
Chris Lattner92673292002-11-02 00:13:20 +0000376 DSNodeHandle Callee = getValueDest(*CI.getOperand(0));
Chris Lattner0d9bab82002-07-18 00:12:30 +0000377
Chris Lattner0969c502002-10-21 02:08:03 +0000378 std::vector<DSNodeHandle> Args;
379 Args.reserve(CI.getNumOperands()-1);
380
381 // Calculate the arguments vector...
382 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000383 if (isPointerType(CI.getOperand(i)->getType()))
Chris Lattner92673292002-11-02 00:13:20 +0000384 Args.push_back(getValueDest(*CI.getOperand(i)));
Chris Lattner0969c502002-10-21 02:08:03 +0000385
386 // Add a new function call entry...
387 FunctionCalls.push_back(DSCallSite(CI, RetVal, Callee, Args));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000388}
Chris Lattner055dc2c2002-07-18 15:54:42 +0000389
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000390/// Handle casts...
391void GraphBuilder::visitCastInst(CastInst &CI) {
Chris Lattner5af344d2002-11-02 00:36:03 +0000392 if (isPointerType(CI.getType()))
393 if (isPointerType(CI.getOperand(0)->getType())) {
394 // Cast one pointer to the other, just act like a copy instruction
Chris Lattner92673292002-11-02 00:13:20 +0000395 setDestTo(CI, getValueDest(*CI.getOperand(0)));
Chris Lattner5af344d2002-11-02 00:36:03 +0000396 } else {
397 // Cast something (floating point, small integer) to a pointer. We need
398 // to track the fact that the node points to SOMETHING, just something we
399 // don't know about. Make an "Unknown" node.
400 //
401 setDestTo(CI, createNode(DSNode::UnknownNode));
402 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000403}
Chris Lattner055dc2c2002-07-18 15:54:42 +0000404
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000405
406
407
408//===----------------------------------------------------------------------===//
409// LocalDataStructures Implementation
410//===----------------------------------------------------------------------===//
411
412// releaseMemory - If the pass pipeline is done with this pass, we can release
413// our memory... here...
414//
415void LocalDataStructures::releaseMemory() {
416 for (std::map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
417 E = DSInfo.end(); I != E; ++I)
418 delete I->second;
419
420 // Empty map so next time memory is released, data structures are not
421 // re-deleted.
422 DSInfo.clear();
Chris Lattner2e4f9bf2002-11-09 20:01:01 +0000423 delete GlobalsGraph;
424 GlobalsGraph = 0;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000425}
426
427bool LocalDataStructures::run(Module &M) {
Chris Lattner2e4f9bf2002-11-09 20:01:01 +0000428 GlobalsGraph = new DSGraph();
429
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000430 // Calculate all of the graphs...
431 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
432 if (!I->isExternal())
Chris Lattner2e4f9bf2002-11-09 20:01:01 +0000433 DSInfo.insert(std::make_pair(I, new DSGraph(*I, GlobalsGraph)));
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000434 return false;
Chris Lattner055dc2c2002-07-18 15:54:42 +0000435}