blob: 20f45a7b27d44c003592ff00e50b977d307e6a32 [file] [log] [blame]
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001//===- Local.cpp - Compute a local data structure graph for a function ----===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerc68c31b2002-07-10 22:38:08 +00009//
10// Compute the local version of the data structure graph for a function. The
11// external interface to this file is the DSGraph constructor.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner4dabb2c2004-07-07 06:32:21 +000015#include "llvm/Analysis/DataStructure/DataStructure.h"
16#include "llvm/Analysis/DataStructure/DSGraph.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000017#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
Chris Lattner808a7ae2003-09-20 16:34:13 +000019#include "llvm/Instructions.h"
Chris Lattnera07b72f2004-02-13 16:09:54 +000020#include "llvm/Intrinsics.h"
Chris Lattnerfa3711a2003-11-25 20:19:55 +000021#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000022#include "llvm/Support/InstVisitor.h"
Chris Lattnerfccd06f2002-10-01 22:33:50 +000023#include "llvm/Target/TargetData.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/Debug.h"
26#include "llvm/Support/Timer.h"
Chris Lattnerfccd06f2002-10-01 22:33:50 +000027
28// FIXME: This should eventually be a FunctionPass that is automatically
29// aggregated into a Pass.
30//
31#include "llvm/Module.h"
32
Chris Lattner9a927292003-11-12 23:11:14 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattner97f51a32002-07-27 01:12:15 +000035static RegisterAnalysis<LocalDataStructures>
36X("datastructure", "Local Data Structure Analysis");
Chris Lattner97f51a32002-07-27 01:12:15 +000037
Chris Lattnera1907662003-11-13 03:10:49 +000038static cl::opt<bool>
Chris Lattnerf0431b02004-08-02 20:16:21 +000039TrackIntegersAsPointers("dsa-track-integers", cl::Hidden,
Chris Lattnera1907662003-11-13 03:10:49 +000040 cl::desc("If this is set, track integers as potential pointers"));
Chris Lattnera1907662003-11-13 03:10:49 +000041
Chris Lattner9a927292003-11-12 23:11:14 +000042namespace llvm {
Chris Lattnerb1060432002-11-07 05:20:53 +000043namespace DS {
Chris Lattnerfccd06f2002-10-01 22:33:50 +000044 // isPointerType - Return true if this type is big enough to hold a pointer.
45 bool isPointerType(const Type *Ty) {
46 if (isa<PointerType>(Ty))
47 return true;
Chris Lattnera1907662003-11-13 03:10:49 +000048 else if (TrackIntegersAsPointers && Ty->isPrimitiveType() &&Ty->isInteger())
Chris Lattnerfccd06f2002-10-01 22:33:50 +000049 return Ty->getPrimitiveSize() >= PointerSize;
50 return false;
51 }
Chris Lattner9a927292003-11-12 23:11:14 +000052}}
Chris Lattnerfccd06f2002-10-01 22:33:50 +000053
Brian Gaeked0fde302003-11-11 22:41:34 +000054using namespace DS;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000055
56namespace {
Chris Lattner923fc052003-02-05 21:59:58 +000057 cl::opt<bool>
58 DisableDirectCallOpt("disable-direct-call-dsopt", cl::Hidden,
59 cl::desc("Disable direct call optimization in "
60 "DSGraph construction"));
Chris Lattnerca3f7902003-02-08 20:18:39 +000061 cl::opt<bool>
62 DisableFieldSensitivity("disable-ds-field-sensitivity", cl::Hidden,
63 cl::desc("Disable field sensitivity in DSGraphs"));
Chris Lattner923fc052003-02-05 21:59:58 +000064
Chris Lattnerfccd06f2002-10-01 22:33:50 +000065 //===--------------------------------------------------------------------===//
66 // GraphBuilder Class
67 //===--------------------------------------------------------------------===//
68 //
69 /// This class is the builder class that constructs the local data structure
70 /// graph by performing a single pass over the function in question.
71 ///
Chris Lattnerc68c31b2002-07-10 22:38:08 +000072 class GraphBuilder : InstVisitor<GraphBuilder> {
73 DSGraph &G;
Chris Lattner26c4fc32003-09-20 21:48:16 +000074 DSNodeHandle *RetNode; // Node that gets returned...
Chris Lattner62482e52004-01-28 09:15:42 +000075 DSScalarMap &ScalarMap;
Chris Lattnera9548d92005-01-30 23:51:02 +000076 std::list<DSCallSite> *FunctionCalls;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000077
78 public:
Chris Lattner44cfdf92003-07-02 06:06:34 +000079 GraphBuilder(Function &f, DSGraph &g, DSNodeHandle &retNode,
Chris Lattnera9548d92005-01-30 23:51:02 +000080 std::list<DSCallSite> &fc)
Chris Lattner26c4fc32003-09-20 21:48:16 +000081 : G(g), RetNode(&retNode), ScalarMap(G.getScalarMap()),
82 FunctionCalls(&fc) {
Chris Lattner0d9bab82002-07-18 00:12:30 +000083
84 // Create scalar nodes for all pointer arguments...
Chris Lattner26c4fc32003-09-20 21:48:16 +000085 for (Function::aiterator I = f.abegin(), E = f.aend(); I != E; ++I)
Chris Lattnerfccd06f2002-10-01 22:33:50 +000086 if (isPointerType(I->getType()))
87 getValueDest(*I);
Chris Lattner0d9bab82002-07-18 00:12:30 +000088
Chris Lattner26c4fc32003-09-20 21:48:16 +000089 visit(f); // Single pass over the function
Chris Lattnerc68c31b2002-07-10 22:38:08 +000090 }
91
Chris Lattner26c4fc32003-09-20 21:48:16 +000092 // GraphBuilder ctor for working on the globals graph
93 GraphBuilder(DSGraph &g)
94 : G(g), RetNode(0), ScalarMap(G.getScalarMap()), FunctionCalls(0) {
95 }
96
97 void mergeInGlobalInitializer(GlobalVariable *GV);
98
Chris Lattnerc68c31b2002-07-10 22:38:08 +000099 private:
100 // Visitor functions, used to handle each instruction type we encounter...
101 friend class InstVisitor<GraphBuilder>;
Chris Lattnerbd92b732003-06-19 21:15:11 +0000102 void visitMallocInst(MallocInst &MI) { handleAlloc(MI, true); }
103 void visitAllocaInst(AllocaInst &AI) { handleAlloc(AI, false); }
104 void handleAlloc(AllocationInst &AI, bool isHeap);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000105
106 void visitPHINode(PHINode &PN);
Chris Lattner6e84bd72005-02-25 01:27:48 +0000107 void visitSelectInst(SelectInst &SI);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000108
Chris Lattner51340062002-11-08 05:00:44 +0000109 void visitGetElementPtrInst(User &GEP);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000110 void visitReturnInst(ReturnInst &RI);
111 void visitLoadInst(LoadInst &LI);
112 void visitStoreInst(StoreInst &SI);
113 void visitCallInst(CallInst &CI);
Chris Lattner808a7ae2003-09-20 16:34:13 +0000114 void visitInvokeInst(InvokeInst &II);
Chris Lattner32672652005-03-05 19:04:31 +0000115 void visitSetCondInst(SetCondInst &SCI);
Vikram S. Advebac06222002-12-06 21:17:10 +0000116 void visitFreeInst(FreeInst &FI);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000117 void visitCastInst(CastInst &CI);
Chris Lattner878e5212003-02-04 00:59:50 +0000118 void visitInstruction(Instruction &I);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000119
Chris Lattner808a7ae2003-09-20 16:34:13 +0000120 void visitCallSite(CallSite CS);
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000121 void visitVANextInst(VANextInst &I);
122 void visitVAArgInst(VAArgInst &I);
Chris Lattner26c4fc32003-09-20 21:48:16 +0000123
124 void MergeConstantInitIntoNode(DSNodeHandle &NH, Constant *C);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000125 private:
126 // Helper functions used to implement the visitation functions...
127
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000128 /// createNode - Create a new DSNode, ensuring that it is properly added to
129 /// the graph.
130 ///
Chris Lattnerbd92b732003-06-19 21:15:11 +0000131 DSNode *createNode(const Type *Ty = 0) {
132 DSNode *N = new DSNode(Ty, &G); // Create the node
Chris Lattnere158b192003-06-16 12:08:18 +0000133 if (DisableFieldSensitivity) {
Chris Lattner2412a052004-05-23 21:14:09 +0000134 // Create node handle referring to the old node so that it is
135 // immediately removed from the graph when the node handle is destroyed.
136 DSNodeHandle OldNNH = N;
Chris Lattnerca3f7902003-02-08 20:18:39 +0000137 N->foldNodeCompletely();
Chris Lattnere158b192003-06-16 12:08:18 +0000138 if (DSNode *FN = N->getForwardNode())
139 N = FN;
140 }
Chris Lattner92673292002-11-02 00:13:20 +0000141 return N;
142 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000143
Chris Lattnerc875f022002-11-03 21:27:48 +0000144 /// setDestTo - Set the ScalarMap entry for the specified value to point to
Chris Lattner92673292002-11-02 00:13:20 +0000145 /// the specified destination. If the Value already points to a node, make
146 /// sure to merge the two destinations together.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000147 ///
Chris Lattner92673292002-11-02 00:13:20 +0000148 void setDestTo(Value &V, const DSNodeHandle &NH);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000149
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 Lattner92673292002-11-02 00:13:20 +0000152 DSNodeHandle getValueDest(Value &V);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000153
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000154 /// getLink - This method is used to return the specified link in the
155 /// specified node if one exists. If a link does not already exist (it's
Chris Lattner7e51c872002-10-31 06:52:26 +0000156 /// null), then we create a new node, link it, then return it.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000157 ///
Chris Lattner7e51c872002-10-31 06:52:26 +0000158 DSNodeHandle &getLink(const DSNodeHandle &Node, unsigned Link = 0);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000159 };
160}
161
Brian Gaeked0fde302003-11-11 22:41:34 +0000162using namespace DS;
163
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000164//===----------------------------------------------------------------------===//
165// DSGraph constructor - Simply use the GraphBuilder to construct the local
166// graph.
Chris Lattner15869aa2003-11-02 22:27:28 +0000167DSGraph::DSGraph(const TargetData &td, Function &F, DSGraph *GG)
168 : GlobalsGraph(GG), TD(td) {
Chris Lattner2a068862002-11-10 06:53:38 +0000169 PrintAuxCalls = false;
Chris Lattner30514192003-07-02 04:37:26 +0000170
171 DEBUG(std::cerr << " [Loc] Calculating graph for: " << F.getName() << "\n");
172
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000173 // Use the graph builder to construct the local version of the graph
Chris Lattner26c4fc32003-09-20 21:48:16 +0000174 GraphBuilder B(F, *this, ReturnNodes[&F], FunctionCalls);
Chris Lattner4fe34612002-11-18 21:44:19 +0000175#ifndef NDEBUG
176 Timer::addPeakMemoryMeasurement();
177#endif
Chris Lattner1a1a85d2003-02-14 04:55:58 +0000178
179 // Remove all integral constants from the scalarmap!
Chris Lattner62482e52004-01-28 09:15:42 +0000180 for (DSScalarMap::iterator I = ScalarMap.begin(); I != ScalarMap.end();)
Chris Lattner02da0322004-01-27 21:51:19 +0000181 if (isa<ConstantIntegral>(I->first))
182 ScalarMap.erase(I++);
183 else
Chris Lattner1a1a85d2003-02-14 04:55:58 +0000184 ++I;
185
Chris Lattnerc420ab62004-02-25 23:31:02 +0000186 // If there are any constant globals referenced in this function, merge their
187 // initializers into the local graph from the globals graph.
188 if (ScalarMap.global_begin() != ScalarMap.global_end()) {
189 ReachabilityCloner RC(*this, *GG, 0);
190
191 for (DSScalarMap::global_iterator I = ScalarMap.global_begin();
192 I != ScalarMap.global_end(); ++I)
193 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I))
Chris Lattner76a9eb32004-03-03 23:00:19 +0000194 if (!GV->isExternal() && GV->isConstant())
Chris Lattnerc420ab62004-02-25 23:31:02 +0000195 RC.merge(ScalarMap[GV], GG->ScalarMap[GV]);
196 }
197
Chris Lattner394471f2003-01-23 22:05:33 +0000198 markIncompleteNodes(DSGraph::MarkFormalArgs);
Chris Lattner96517252002-11-09 20:55:24 +0000199
200 // Remove any nodes made dead due to merging...
Chris Lattner394471f2003-01-23 22:05:33 +0000201 removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000202}
203
204
205//===----------------------------------------------------------------------===//
206// Helper method implementations...
207//
208
Chris Lattner92673292002-11-02 00:13:20 +0000209/// getValueDest - Return the DSNode that the actual value points to.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000210///
Chris Lattner51340062002-11-08 05:00:44 +0000211DSNodeHandle GraphBuilder::getValueDest(Value &Val) {
212 Value *V = &Val;
Chris Lattner82962de2004-11-03 18:51:26 +0000213 if (isa<Constant>(V) && cast<Constant>(V)->isNullValue())
Chris Lattner51340062002-11-08 05:00:44 +0000214 return 0; // Null doesn't point to anything, don't add to ScalarMap!
Chris Lattner92673292002-11-02 00:13:20 +0000215
Vikram S. Advebac06222002-12-06 21:17:10 +0000216 DSNodeHandle &NH = ScalarMap[V];
Chris Lattner62c3a952004-10-30 04:22:45 +0000217 if (!NH.isNull())
Vikram S. Advebac06222002-12-06 21:17:10 +0000218 return NH; // Already have a node? Just return it...
219
220 // Otherwise we need to create a new node to point to.
221 // Check first for constant expressions that must be traversed to
222 // extract the actual value.
Reid Spencere8404342004-07-18 00:18:30 +0000223 DSNode* N;
224 if (GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
225 // Create a new global node for this global variable...
226 N = createNode(GV->getType()->getElementType());
227 N->addGlobal(GV);
228 } else if (Constant *C = dyn_cast<Constant>(V)) {
229 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattner51340062002-11-08 05:00:44 +0000230 if (CE->getOpcode() == Instruction::Cast)
Chris Lattner1e56c542003-02-09 23:04:12 +0000231 NH = getValueDest(*CE->getOperand(0));
232 else if (CE->getOpcode() == Instruction::GetElementPtr) {
Chris Lattner51340062002-11-08 05:00:44 +0000233 visitGetElementPtrInst(*CE);
Chris Lattner62482e52004-01-28 09:15:42 +0000234 DSScalarMap::iterator I = ScalarMap.find(CE);
Chris Lattner2cfbaaf2002-11-09 20:14:03 +0000235 assert(I != ScalarMap.end() && "GEP didn't get processed right?");
Chris Lattner1e56c542003-02-09 23:04:12 +0000236 NH = I->second;
237 } else {
238 // This returns a conservative unknown node for any unhandled ConstExpr
Chris Lattnerbd92b732003-06-19 21:15:11 +0000239 return NH = createNode()->setUnknownNodeMarker();
Chris Lattner51340062002-11-08 05:00:44 +0000240 }
Chris Lattner62c3a952004-10-30 04:22:45 +0000241 if (NH.isNull()) { // (getelementptr null, X) returns null
Chris Lattner1e56c542003-02-09 23:04:12 +0000242 ScalarMap.erase(V);
243 return 0;
244 }
245 return NH;
Chris Lattner51340062002-11-08 05:00:44 +0000246
Chris Lattner51340062002-11-08 05:00:44 +0000247 } else if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(C)) {
248 // Random constants are unknown mem
Chris Lattnerbd92b732003-06-19 21:15:11 +0000249 return NH = createNode()->setUnknownNodeMarker();
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000250 } else if (isa<UndefValue>(C)) {
251 ScalarMap.erase(V);
252 return 0;
Chris Lattner51340062002-11-08 05:00:44 +0000253 } else {
254 assert(0 && "Unknown constant type!");
255 }
Reid Spencere8404342004-07-18 00:18:30 +0000256 N = createNode(); // just create a shadow node
Chris Lattner92673292002-11-02 00:13:20 +0000257 } else {
258 // Otherwise just create a shadow node
Chris Lattnerbd92b732003-06-19 21:15:11 +0000259 N = createNode();
Chris Lattner92673292002-11-02 00:13:20 +0000260 }
261
Chris Lattnerefffdc92004-07-07 06:12:52 +0000262 NH.setTo(N, 0); // Remember that we are pointing to it...
Chris Lattner92673292002-11-02 00:13:20 +0000263 return NH;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000264}
265
Chris Lattner0d9bab82002-07-18 00:12:30 +0000266
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000267/// getLink - This method is used to return the specified link in the
268/// specified node if one exists. If a link does not already exist (it's
269/// null), then we create a new node, link it, then return it. We must
270/// specify the type of the Node field we are accessing so that we know what
271/// type should be linked to if we need to create a new node.
272///
Chris Lattner7e51c872002-10-31 06:52:26 +0000273DSNodeHandle &GraphBuilder::getLink(const DSNodeHandle &node, unsigned LinkNo) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000274 DSNodeHandle &Node = const_cast<DSNodeHandle&>(node);
Chris Lattner08db7192002-11-06 06:20:27 +0000275 DSNodeHandle &Link = Node.getLink(LinkNo);
Chris Lattner62c3a952004-10-30 04:22:45 +0000276 if (Link.isNull()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000277 // If the link hasn't been created yet, make and return a new shadow node
Chris Lattnerbd92b732003-06-19 21:15:11 +0000278 Link = createNode();
Chris Lattner08db7192002-11-06 06:20:27 +0000279 }
280 return Link;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000281}
282
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000283
Chris Lattnerc875f022002-11-03 21:27:48 +0000284/// setDestTo - Set the ScalarMap entry for the specified value to point to the
Chris Lattner92673292002-11-02 00:13:20 +0000285/// specified destination. If the Value already points to a node, make sure to
286/// merge the two destinations together.
287///
288void GraphBuilder::setDestTo(Value &V, const DSNodeHandle &NH) {
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000289 ScalarMap[&V].mergeWith(NH);
Chris Lattner92673292002-11-02 00:13:20 +0000290}
291
292
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000293//===----------------------------------------------------------------------===//
294// Specific instruction type handler implementations...
295//
296
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000297/// Alloca & Malloc instruction implementation - Simply create a new memory
298/// object, pointing the scalar to it.
299///
Chris Lattnerbd92b732003-06-19 21:15:11 +0000300void GraphBuilder::handleAlloc(AllocationInst &AI, bool isHeap) {
301 DSNode *N = createNode();
302 if (isHeap)
303 N->setHeapNodeMarker();
304 else
305 N->setAllocaNodeMarker();
306 setDestTo(AI, N);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000307}
308
309// PHINode - Make the scalar for the PHI node point to all of the things the
310// incoming values point to... which effectively causes them to be merged.
311//
312void GraphBuilder::visitPHINode(PHINode &PN) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000313 if (!isPointerType(PN.getType())) return; // Only pointer PHIs
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000314
Chris Lattnerc875f022002-11-03 21:27:48 +0000315 DSNodeHandle &PNDest = ScalarMap[&PN];
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000316 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
Chris Lattner92673292002-11-02 00:13:20 +0000317 PNDest.mergeWith(getValueDest(*PN.getIncomingValue(i)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000318}
319
Chris Lattner6e84bd72005-02-25 01:27:48 +0000320void GraphBuilder::visitSelectInst(SelectInst &SI) {
321 if (!isPointerType(SI.getType())) return; // Only pointer Selects
322
323 DSNodeHandle &Dest = ScalarMap[&SI];
324 Dest.mergeWith(getValueDest(*SI.getOperand(1)));
325 Dest.mergeWith(getValueDest(*SI.getOperand(2)));
326}
327
Chris Lattner32672652005-03-05 19:04:31 +0000328void GraphBuilder::visitSetCondInst(SetCondInst &SCI) {
329 if (!isPointerType(SCI.getOperand(0)->getType()) ||
330 isa<ConstantPointerNull>(SCI.getOperand(1))) return; // Only pointers
331 ScalarMap[SCI.getOperand(0)].mergeWith(getValueDest(*SCI.getOperand(1)));
332}
333
334
Chris Lattner51340062002-11-08 05:00:44 +0000335void GraphBuilder::visitGetElementPtrInst(User &GEP) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000336 DSNodeHandle Value = getValueDest(*GEP.getOperand(0));
Chris Lattner753b1132005-02-24 19:55:31 +0000337 if (Value.isNull())
338 Value = createNode();
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000339
Chris Lattner179bc7d2003-11-14 17:09:46 +0000340 // As a special case, if all of the index operands of GEP are constant zeros,
341 // handle this just like we handle casts (ie, don't do much).
342 bool AllZeros = true;
343 for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i)
344 if (GEP.getOperand(i) !=
345 Constant::getNullValue(GEP.getOperand(i)->getType())) {
346 AllZeros = false;
347 break;
348 }
349
350 // If all of the indices are zero, the result points to the operand without
351 // applying the type.
352 if (AllZeros) {
353 setDestTo(GEP, Value);
354 return;
355 }
356
357
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000358 const PointerType *PTy = cast<PointerType>(GEP.getOperand(0)->getType());
359 const Type *CurTy = PTy->getElementType();
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000360
Chris Lattner08db7192002-11-06 06:20:27 +0000361 if (Value.getNode()->mergeTypeInfo(CurTy, Value.getOffset())) {
362 // If the node had to be folded... exit quickly
Chris Lattner92673292002-11-02 00:13:20 +0000363 setDestTo(GEP, Value); // GEP result points to folded node
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000364 return;
365 }
366
Chris Lattner15869aa2003-11-02 22:27:28 +0000367 const TargetData &TD = Value.getNode()->getTargetData();
368
Chris Lattner08db7192002-11-06 06:20:27 +0000369#if 0
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000370 // Handle the pointer index specially...
371 if (GEP.getNumOperands() > 1 &&
Chris Lattner28977af2004-04-05 01:30:19 +0000372 (!isa<Constant>(GEP.getOperand(1)) ||
373 !cast<Constant>(GEP.getOperand(1))->isNullValue())) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000374
375 // If we already know this is an array being accessed, don't do anything...
376 if (!TopTypeRec.isArray) {
377 TopTypeRec.isArray = true;
378
379 // If we are treating some inner field pointer as an array, fold the node
380 // up because we cannot handle it right. This can come because of
381 // something like this: &((&Pt->X)[1]) == &Pt->Y
382 //
383 if (Value.getOffset()) {
384 // Value is now the pointer we want to GEP to be...
385 Value.getNode()->foldNodeCompletely();
Chris Lattner92673292002-11-02 00:13:20 +0000386 setDestTo(GEP, Value); // GEP result points to folded node
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000387 return;
388 } else {
389 // This is a pointer to the first byte of the node. Make sure that we
390 // are pointing to the outter most type in the node.
391 // FIXME: We need to check one more case here...
392 }
393 }
394 }
Chris Lattner08db7192002-11-06 06:20:27 +0000395#endif
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000396
397 // All of these subscripts are indexing INTO the elements we have...
Chris Lattner26c4fc32003-09-20 21:48:16 +0000398 unsigned Offset = 0;
Chris Lattnerfa3711a2003-11-25 20:19:55 +0000399 for (gep_type_iterator I = gep_type_begin(GEP), E = gep_type_end(GEP);
400 I != E; ++I)
401 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner507bdf92005-01-12 04:51:37 +0000402 unsigned FieldNo =
403 (unsigned)cast<ConstantUInt>(I.getOperand())->getValue();
404 Offset += (unsigned)TD.getStructLayout(STy)->MemberOffsets[FieldNo];
Chris Lattner82e9d722004-03-01 19:02:54 +0000405 } else if (const PointerType *PTy = dyn_cast<PointerType>(*I)) {
406 if (!isa<Constant>(I.getOperand()) ||
407 !cast<Constant>(I.getOperand())->isNullValue())
408 Value.getNode()->setArrayMarker();
Chris Lattnerfa3711a2003-11-25 20:19:55 +0000409 }
410
411
Chris Lattner08db7192002-11-06 06:20:27 +0000412#if 0
Chris Lattnerfa3711a2003-11-25 20:19:55 +0000413 if (const SequentialType *STy = cast<SequentialType>(*I)) {
414 CurTy = STy->getElementType();
Chris Lattnere03f32b2002-10-02 06:24:36 +0000415 if (ConstantSInt *CS = dyn_cast<ConstantSInt>(GEP.getOperand(i))) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000416 Offset += CS->getValue()*TD.getTypeSize(CurTy);
Chris Lattnere03f32b2002-10-02 06:24:36 +0000417 } else {
418 // Variable index into a node. We must merge all of the elements of the
419 // sequential type here.
420 if (isa<PointerType>(STy))
421 std::cerr << "Pointer indexing not handled yet!\n";
422 else {
423 const ArrayType *ATy = cast<ArrayType>(STy);
424 unsigned ElSize = TD.getTypeSize(CurTy);
425 DSNode *N = Value.getNode();
426 assert(N && "Value must have a node!");
427 unsigned RawOffset = Offset+Value.getOffset();
428
429 // Loop over all of the elements of the array, merging them into the
Misha Brukman2f2d0652003-09-11 18:14:24 +0000430 // zeroth element.
Chris Lattnere03f32b2002-10-02 06:24:36 +0000431 for (unsigned i = 1, e = ATy->getNumElements(); i != e; ++i)
432 // Merge all of the byte components of this array element
433 for (unsigned j = 0; j != ElSize; ++j)
434 N->mergeIndexes(RawOffset+j, RawOffset+i*ElSize+j);
435 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000436 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000437 }
Chris Lattnerfa3711a2003-11-25 20:19:55 +0000438#endif
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000439
440 // Add in the offset calculated...
441 Value.setOffset(Value.getOffset()+Offset);
442
443 // Value is now the pointer we want to GEP to be...
Chris Lattner92673292002-11-02 00:13:20 +0000444 setDestTo(GEP, Value);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000445}
446
447void GraphBuilder::visitLoadInst(LoadInst &LI) {
Chris Lattner92673292002-11-02 00:13:20 +0000448 DSNodeHandle Ptr = getValueDest(*LI.getOperand(0));
Chris Lattner6e84bd72005-02-25 01:27:48 +0000449 if (Ptr.isNull())
450 Ptr = createNode();
Chris Lattner92673292002-11-02 00:13:20 +0000451
452 // Make that the node is read from...
Chris Lattnerbd92b732003-06-19 21:15:11 +0000453 Ptr.getNode()->setReadMarker();
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000454
455 // Ensure a typerecord exists...
Chris Lattner088b6392003-03-03 17:13:31 +0000456 Ptr.getNode()->mergeTypeInfo(LI.getType(), Ptr.getOffset(), false);
Chris Lattner92673292002-11-02 00:13:20 +0000457
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000458 if (isPointerType(LI.getType()))
Chris Lattner92673292002-11-02 00:13:20 +0000459 setDestTo(LI, getLink(Ptr));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000460}
461
462void GraphBuilder::visitStoreInst(StoreInst &SI) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000463 const Type *StoredTy = SI.getOperand(0)->getType();
Chris Lattner92673292002-11-02 00:13:20 +0000464 DSNodeHandle Dest = getValueDest(*SI.getOperand(1));
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000465 if (Dest.isNull()) return;
Chris Lattner92673292002-11-02 00:13:20 +0000466
Vikram S. Advebac06222002-12-06 21:17:10 +0000467 // Mark that the node is written to...
Chris Lattnerbd92b732003-06-19 21:15:11 +0000468 Dest.getNode()->setModifiedMarker();
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000469
Chris Lattner26c4fc32003-09-20 21:48:16 +0000470 // Ensure a type-record exists...
Chris Lattner08db7192002-11-06 06:20:27 +0000471 Dest.getNode()->mergeTypeInfo(StoredTy, Dest.getOffset());
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000472
473 // Avoid adding edges from null, or processing non-"pointer" stores
Chris Lattner92673292002-11-02 00:13:20 +0000474 if (isPointerType(StoredTy))
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000475 Dest.addEdgeTo(getValueDest(*SI.getOperand(0)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000476}
477
478void GraphBuilder::visitReturnInst(ReturnInst &RI) {
Chris Lattner92673292002-11-02 00:13:20 +0000479 if (RI.getNumOperands() && isPointerType(RI.getOperand(0)->getType()))
Chris Lattner26c4fc32003-09-20 21:48:16 +0000480 RetNode->mergeWith(getValueDest(*RI.getOperand(0)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000481}
482
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000483void GraphBuilder::visitVANextInst(VANextInst &I) {
484 getValueDest(*I.getOperand(0)).mergeWith(getValueDest(I));
485}
486
487void GraphBuilder::visitVAArgInst(VAArgInst &I) {
488 DSNodeHandle Ptr = getValueDest(*I.getOperand(0));
489 if (Ptr.isNull()) return;
490
491 // Make that the node is read from.
492 Ptr.getNode()->setReadMarker();
493
Chris Lattner62c3a952004-10-30 04:22:45 +0000494 // Ensure a type record exists.
495 DSNode *PtrN = Ptr.getNode();
496 PtrN->mergeTypeInfo(I.getType(), Ptr.getOffset(), false);
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000497
498 if (isPointerType(I.getType()))
499 setDestTo(I, getLink(Ptr));
500}
501
502
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000503void GraphBuilder::visitCallInst(CallInst &CI) {
Chris Lattner808a7ae2003-09-20 16:34:13 +0000504 visitCallSite(&CI);
505}
506
507void GraphBuilder::visitInvokeInst(InvokeInst &II) {
508 visitCallSite(&II);
509}
510
511void GraphBuilder::visitCallSite(CallSite CS) {
Chris Lattnercb582402004-02-26 22:07:22 +0000512 Value *Callee = CS.getCalledValue();
Chris Lattnercb582402004-02-26 22:07:22 +0000513
Chris Lattner894263b2003-09-20 16:50:46 +0000514 // Special case handling of certain libc allocation functions here.
Chris Lattnercb582402004-02-26 22:07:22 +0000515 if (Function *F = dyn_cast<Function>(Callee))
Chris Lattner894263b2003-09-20 16:50:46 +0000516 if (F->isExternal())
Chris Lattnera07b72f2004-02-13 16:09:54 +0000517 switch (F->getIntrinsicID()) {
Chris Lattner317201d2004-03-13 00:24:00 +0000518 case Intrinsic::vastart:
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000519 getValueDest(*CS.getInstruction()).getNode()->setAllocaNodeMarker();
520 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000521 case Intrinsic::vacopy:
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000522 getValueDest(*CS.getInstruction()).
523 mergeWith(getValueDest(**(CS.arg_begin())));
524 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000525 case Intrinsic::vaend:
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000526 return; // noop
Chris Lattnera07b72f2004-02-13 16:09:54 +0000527 case Intrinsic::memmove:
528 case Intrinsic::memcpy: {
529 // Merge the first & second arguments, and mark the memory read and
Chris Lattnerfb8c6102003-11-08 21:55:50 +0000530 // modified.
Chris Lattnera07b72f2004-02-13 16:09:54 +0000531 DSNodeHandle RetNH = getValueDest(**CS.arg_begin());
Chris Lattner67ce57a2003-11-09 03:32:52 +0000532 RetNH.mergeWith(getValueDest(**(CS.arg_begin()+1)));
533 if (DSNode *N = RetNH.getNode())
534 N->setModifiedMarker()->setReadMarker();
535 return;
Chris Lattnera07b72f2004-02-13 16:09:54 +0000536 }
Chris Lattnereee33b22004-02-16 18:37:40 +0000537 case Intrinsic::memset:
538 // Mark the memory modified.
539 if (DSNode *N = getValueDest(**CS.arg_begin()).getNode())
540 N->setModifiedMarker();
541 return;
Chris Lattnera07b72f2004-02-13 16:09:54 +0000542 default:
Vikram S. Advee4e97ef2004-05-25 08:14:52 +0000543 if (F->getName() == "calloc" || F->getName() == "posix_memalign" ||
544 F->getName() == "memalign" || F->getName() == "valloc") {
Chris Lattnera07b72f2004-02-13 16:09:54 +0000545 setDestTo(*CS.getInstruction(),
546 createNode()->setHeapNodeMarker()->setModifiedMarker());
547 return;
548 } else if (F->getName() == "realloc") {
549 DSNodeHandle RetNH = getValueDest(*CS.getInstruction());
Chris Lattner82962de2004-11-03 18:51:26 +0000550 if (CS.arg_begin() != CS.arg_end())
551 RetNH.mergeWith(getValueDest(**CS.arg_begin()));
Chris Lattnera07b72f2004-02-13 16:09:54 +0000552 if (DSNode *N = RetNH.getNode())
553 N->setHeapNodeMarker()->setModifiedMarker()->setReadMarker();
554 return;
Chris Lattner1fe98742004-02-26 03:43:08 +0000555 } else if (F->getName() == "memmove") {
556 // Merge the first & second arguments, and mark the memory read and
557 // modified.
558 DSNodeHandle RetNH = getValueDest(**CS.arg_begin());
559 RetNH.mergeWith(getValueDest(**(CS.arg_begin()+1)));
560 if (DSNode *N = RetNH.getNode())
561 N->setModifiedMarker()->setReadMarker();
562 return;
563
Chris Lattnerd5612092004-02-24 22:02:48 +0000564 } else if (F->getName() == "atoi" || F->getName() == "atof" ||
Chris Lattneradc1efe2004-02-25 17:43:20 +0000565 F->getName() == "atol" || F->getName() == "atoll" ||
Chris Lattner39bb2dc2004-02-24 22:17:00 +0000566 F->getName() == "remove" || F->getName() == "unlink" ||
Chris Lattneradc1efe2004-02-25 17:43:20 +0000567 F->getName() == "rename" || F->getName() == "memcmp" ||
568 F->getName() == "strcmp" || F->getName() == "strncmp" ||
569 F->getName() == "execl" || F->getName() == "execlp" ||
570 F->getName() == "execle" || F->getName() == "execv" ||
Chris Lattner52fc8d72004-02-25 23:06:40 +0000571 F->getName() == "execvp" || F->getName() == "chmod" ||
572 F->getName() == "puts" || F->getName() == "write" ||
573 F->getName() == "open" || F->getName() == "create" ||
574 F->getName() == "truncate" || F->getName() == "chdir" ||
575 F->getName() == "mkdir" || F->getName() == "rmdir") {
Chris Lattner39bb2dc2004-02-24 22:17:00 +0000576 // These functions read all of their pointer operands.
577 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
578 AI != E; ++AI) {
579 if (isPointerType((*AI)->getType()))
580 if (DSNode *N = getValueDest(**AI).getNode())
581 N->setReadMarker();
582 }
Chris Lattner304e1432004-02-16 22:57:19 +0000583 return;
Chris Lattner52fc8d72004-02-25 23:06:40 +0000584 } else if (F->getName() == "read" || F->getName() == "pipe" ||
Chris Lattner6b586df2004-02-27 20:04:48 +0000585 F->getName() == "wait" || F->getName() == "time") {
Chris Lattner52fc8d72004-02-25 23:06:40 +0000586 // These functions write all of their pointer operands.
587 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
588 AI != E; ++AI) {
589 if (isPointerType((*AI)->getType()))
590 if (DSNode *N = getValueDest(**AI).getNode())
591 N->setModifiedMarker();
592 }
593 return;
Chris Lattneradc1efe2004-02-25 17:43:20 +0000594 } else if (F->getName() == "stat" || F->getName() == "fstat" ||
595 F->getName() == "lstat") {
596 // These functions read their first operand if its a pointer.
597 CallSite::arg_iterator AI = CS.arg_begin();
598 if (isPointerType((*AI)->getType())) {
599 DSNodeHandle Path = getValueDest(**AI);
600 if (DSNode *N = Path.getNode()) N->setReadMarker();
601 }
Chris Lattner304e1432004-02-16 22:57:19 +0000602
Chris Lattneradc1efe2004-02-25 17:43:20 +0000603 // Then they write into the stat buffer.
604 DSNodeHandle StatBuf = getValueDest(**++AI);
605 if (DSNode *N = StatBuf.getNode()) {
606 N->setModifiedMarker();
607 const Type *StatTy = F->getFunctionType()->getParamType(1);
608 if (const PointerType *PTy = dyn_cast<PointerType>(StatTy))
609 N->mergeTypeInfo(PTy->getElementType(), StatBuf.getOffset());
610 }
Chris Lattneradc1efe2004-02-25 17:43:20 +0000611 return;
Chris Lattner3aeb40c2004-03-04 21:03:54 +0000612 } else if (F->getName() == "strtod" || F->getName() == "strtof" ||
613 F->getName() == "strtold") {
614 // These functions read the first pointer
615 if (DSNode *Str = getValueDest(**CS.arg_begin()).getNode()) {
616 Str->setReadMarker();
617 // If the second parameter is passed, it will point to the first
618 // argument node.
619 const DSNodeHandle &EndPtrNH = getValueDest(**(CS.arg_begin()+1));
620 if (DSNode *End = EndPtrNH.getNode()) {
621 End->mergeTypeInfo(PointerType::get(Type::SByteTy),
622 EndPtrNH.getOffset(), false);
623 End->setModifiedMarker();
624 DSNodeHandle &Link = getLink(EndPtrNH);
625 Link.mergeWith(getValueDest(**CS.arg_begin()));
626 }
627 }
628
629 return;
Chris Lattner6b586df2004-02-27 20:04:48 +0000630 } else if (F->getName() == "fopen" || F->getName() == "fdopen" ||
631 F->getName() == "freopen") {
632 // These functions read all of their pointer operands.
633 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
634 AI != E; ++AI)
635 if (isPointerType((*AI)->getType()))
636 if (DSNode *N = getValueDest(**AI).getNode())
637 N->setReadMarker();
Chris Lattner68300db2004-02-13 20:05:32 +0000638
639 // fopen allocates in an unknown way and writes to the file
640 // descriptor. Also, merge the allocated type into the node.
641 DSNodeHandle Result = getValueDest(*CS.getInstruction());
Chris Lattnerd5612092004-02-24 22:02:48 +0000642 if (DSNode *N = Result.getNode()) {
643 N->setModifiedMarker()->setUnknownNodeMarker();
644 const Type *RetTy = F->getFunctionType()->getReturnType();
645 if (const PointerType *PTy = dyn_cast<PointerType>(RetTy))
646 N->mergeTypeInfo(PTy->getElementType(), Result.getOffset());
647 }
Chris Lattner6b586df2004-02-27 20:04:48 +0000648
649 // If this is freopen, merge the file descriptor passed in with the
650 // result.
Chris Lattnerfe781652004-12-08 16:22:26 +0000651 if (F->getName() == "freopen") {
652 // ICC doesn't handle getting the iterator, decrementing and
653 // dereferencing it in one operation without error. Do it in 2 steps
654 CallSite::arg_iterator compit = CS.arg_end();
655 Result.mergeWith(getValueDest(**--compit));
656 }
Chris Lattnerd5612092004-02-24 22:02:48 +0000657 return;
Chris Lattner68300db2004-02-13 20:05:32 +0000658 } else if (F->getName() == "fclose" && CS.arg_end()-CS.arg_begin() ==1){
659 // fclose reads and deallocates the memory in an unknown way for the
660 // file descriptor. It merges the FILE type into the descriptor.
661 DSNodeHandle H = getValueDest(**CS.arg_begin());
Chris Lattnerd5612092004-02-24 22:02:48 +0000662 if (DSNode *N = H.getNode()) {
663 N->setReadMarker()->setUnknownNodeMarker();
664 const Type *ArgTy = F->getFunctionType()->getParamType(0);
665 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
666 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
667 }
Chris Lattner68300db2004-02-13 20:05:32 +0000668 return;
Chris Lattner304e1432004-02-16 22:57:19 +0000669 } else if (CS.arg_end()-CS.arg_begin() == 1 &&
670 (F->getName() == "fflush" || F->getName() == "feof" ||
671 F->getName() == "fileno" || F->getName() == "clearerr" ||
Chris Lattner52fc8d72004-02-25 23:06:40 +0000672 F->getName() == "rewind" || F->getName() == "ftell" ||
Chris Lattner6b586df2004-02-27 20:04:48 +0000673 F->getName() == "ferror" || F->getName() == "fgetc" ||
674 F->getName() == "fgetc" || F->getName() == "_IO_getc")) {
Chris Lattner304e1432004-02-16 22:57:19 +0000675 // fflush reads and writes the memory for the file descriptor. It
Chris Lattner339d8df2004-02-13 21:21:48 +0000676 // merges the FILE type into the descriptor.
677 DSNodeHandle H = getValueDest(**CS.arg_begin());
Chris Lattnerd5612092004-02-24 22:02:48 +0000678 if (DSNode *N = H.getNode()) {
679 N->setReadMarker()->setModifiedMarker();
Chris Lattner339d8df2004-02-13 21:21:48 +0000680
Chris Lattnerd5612092004-02-24 22:02:48 +0000681 const Type *ArgTy = F->getFunctionType()->getParamType(0);
682 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
683 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
684 }
685 return;
686 } else if (CS.arg_end()-CS.arg_begin() == 4 &&
687 (F->getName() == "fwrite" || F->getName() == "fread")) {
688 // fread writes the first operand, fwrite reads it. They both
689 // read/write the FILE descriptor, and merges the FILE type.
Chris Lattnerfe781652004-12-08 16:22:26 +0000690 CallSite::arg_iterator compit = CS.arg_end();
691 DSNodeHandle H = getValueDest(**--compit);
Chris Lattnerd5612092004-02-24 22:02:48 +0000692 if (DSNode *N = H.getNode()) {
693 N->setReadMarker()->setModifiedMarker();
694 const Type *ArgTy = F->getFunctionType()->getParamType(3);
695 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
696 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
697 }
698
699 H = getValueDest(**CS.arg_begin());
700 if (DSNode *N = H.getNode())
701 if (F->getName() == "fwrite")
702 N->setReadMarker();
703 else
704 N->setModifiedMarker();
Chris Lattner339d8df2004-02-13 21:21:48 +0000705 return;
706 } else if (F->getName() == "fgets" && CS.arg_end()-CS.arg_begin() == 3){
Chris Lattner304e1432004-02-16 22:57:19 +0000707 // fgets reads and writes the memory for the file descriptor. It
Chris Lattner339d8df2004-02-13 21:21:48 +0000708 // merges the FILE type into the descriptor, and writes to the
709 // argument. It returns the argument as well.
710 CallSite::arg_iterator AI = CS.arg_begin();
711 DSNodeHandle H = getValueDest(**AI);
712 if (DSNode *N = H.getNode())
713 N->setModifiedMarker(); // Writes buffer
714 H.mergeWith(getValueDest(*CS.getInstruction())); // Returns buffer
715 ++AI; ++AI;
716
717 // Reads and writes file descriptor, merge in FILE type.
Chris Lattneradc1efe2004-02-25 17:43:20 +0000718 H = getValueDest(**AI);
Chris Lattnerd5612092004-02-24 22:02:48 +0000719 if (DSNode *N = H.getNode()) {
Chris Lattner339d8df2004-02-13 21:21:48 +0000720 N->setReadMarker()->setModifiedMarker();
Chris Lattneradc1efe2004-02-25 17:43:20 +0000721 const Type *ArgTy = F->getFunctionType()->getParamType(2);
722 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
723 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
724 }
725 return;
Chris Lattner52fc8d72004-02-25 23:06:40 +0000726 } else if (F->getName() == "ungetc" || F->getName() == "fputc" ||
727 F->getName() == "fputs" || F->getName() == "putc" ||
Chris Lattner6b586df2004-02-27 20:04:48 +0000728 F->getName() == "ftell" || F->getName() == "rewind" ||
729 F->getName() == "_IO_putc") {
730 // These functions read and write the memory for the file descriptor,
731 // which is passes as the last argument.
Chris Lattnerfe781652004-12-08 16:22:26 +0000732 CallSite::arg_iterator compit = CS.arg_end();
733 DSNodeHandle H = getValueDest(**--compit);
Chris Lattneradc1efe2004-02-25 17:43:20 +0000734 if (DSNode *N = H.getNode()) {
735 N->setReadMarker()->setModifiedMarker();
Chris Lattnerfe781652004-12-08 16:22:26 +0000736 FunctionType::param_iterator compit2 = F->getFunctionType()->param_end();
737 const Type *ArgTy = *--compit2;
Chris Lattnerd5612092004-02-24 22:02:48 +0000738 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
739 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
740 }
Chris Lattner52fc8d72004-02-25 23:06:40 +0000741
742 // Any pointer arguments are read.
743 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
744 AI != E; ++AI)
745 if (isPointerType((*AI)->getType()))
746 if (DSNode *N = getValueDest(**AI).getNode())
747 N->setReadMarker();
748 return;
749 } else if (F->getName() == "fseek" || F->getName() == "fgetpos" ||
750 F->getName() == "fsetpos") {
751 // These functions read and write the memory for the file descriptor,
752 // and read/write all other arguments.
753 DSNodeHandle H = getValueDest(**CS.arg_begin());
754 if (DSNode *N = H.getNode()) {
Chris Lattnerfe781652004-12-08 16:22:26 +0000755 FunctionType::param_iterator compit2 = F->getFunctionType()->param_end();
756 const Type *ArgTy = *--compit2;
Chris Lattner52fc8d72004-02-25 23:06:40 +0000757 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
758 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
759 }
760
761 // Any pointer arguments are read.
762 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
763 AI != E; ++AI)
764 if (isPointerType((*AI)->getType()))
765 if (DSNode *N = getValueDest(**AI).getNode())
766 N->setReadMarker()->setModifiedMarker();
Chris Lattner339d8df2004-02-13 21:21:48 +0000767 return;
Chris Lattner8ecc27e2004-02-20 20:27:11 +0000768 } else if (F->getName() == "printf" || F->getName() == "fprintf" ||
769 F->getName() == "sprintf") {
Chris Lattner339d8df2004-02-13 21:21:48 +0000770 CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
771
772 if (F->getName() == "fprintf") {
773 // fprintf reads and writes the FILE argument, and applies the type
774 // to it.
775 DSNodeHandle H = getValueDest(**AI);
776 if (DSNode *N = H.getNode()) {
777 N->setModifiedMarker();
778 const Type *ArgTy = (*AI)->getType();
779 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
780 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
781 }
Chris Lattner8ecc27e2004-02-20 20:27:11 +0000782 } else if (F->getName() == "sprintf") {
783 // sprintf writes the first string argument.
784 DSNodeHandle H = getValueDest(**AI++);
785 if (DSNode *N = H.getNode()) {
786 N->setModifiedMarker();
787 const Type *ArgTy = (*AI)->getType();
788 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
789 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
790 }
Chris Lattner339d8df2004-02-13 21:21:48 +0000791 }
792
793 for (; AI != E; ++AI) {
794 // printf reads all pointer arguments.
795 if (isPointerType((*AI)->getType()))
796 if (DSNode *N = getValueDest(**AI).getNode())
797 N->setReadMarker();
798 }
Chris Lattner4e46e322004-02-20 23:27:09 +0000799 return;
Chris Lattner3aeb40c2004-03-04 21:03:54 +0000800 } else if (F->getName() == "vprintf" || F->getName() == "vfprintf" ||
801 F->getName() == "vsprintf") {
802 CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
803
804 if (F->getName() == "vfprintf") {
805 // ffprintf reads and writes the FILE argument, and applies the type
806 // to it.
807 DSNodeHandle H = getValueDest(**AI);
808 if (DSNode *N = H.getNode()) {
809 N->setModifiedMarker()->setReadMarker();
810 const Type *ArgTy = (*AI)->getType();
811 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
812 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
813 }
814 ++AI;
815 } else if (F->getName() == "vsprintf") {
816 // vsprintf writes the first string argument.
817 DSNodeHandle H = getValueDest(**AI++);
818 if (DSNode *N = H.getNode()) {
819 N->setModifiedMarker();
820 const Type *ArgTy = (*AI)->getType();
821 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
822 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
823 }
824 }
825
826 // Read the format
827 if (AI != E) {
828 if (isPointerType((*AI)->getType()))
829 if (DSNode *N = getValueDest(**AI).getNode())
830 N->setReadMarker();
831 ++AI;
832 }
833
834 // Read the valist, and the pointed-to objects.
835 if (AI != E && isPointerType((*AI)->getType())) {
836 const DSNodeHandle &VAList = getValueDest(**AI);
837 if (DSNode *N = VAList.getNode()) {
838 N->setReadMarker();
839 N->mergeTypeInfo(PointerType::get(Type::SByteTy),
840 VAList.getOffset(), false);
841
842 DSNodeHandle &VAListObjs = getLink(VAList);
843 VAListObjs.getNode()->setReadMarker();
844 }
845 }
846
847 return;
Chris Lattner8ecc27e2004-02-20 20:27:11 +0000848 } else if (F->getName() == "scanf" || F->getName() == "fscanf" ||
849 F->getName() == "sscanf") {
850 CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
Chris Lattner339d8df2004-02-13 21:21:48 +0000851
Chris Lattner8ecc27e2004-02-20 20:27:11 +0000852 if (F->getName() == "fscanf") {
853 // fscanf reads and writes the FILE argument, and applies the type
854 // to it.
855 DSNodeHandle H = getValueDest(**AI);
856 if (DSNode *N = H.getNode()) {
857 N->setReadMarker();
858 const Type *ArgTy = (*AI)->getType();
859 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
860 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
861 }
862 } else if (F->getName() == "sscanf") {
863 // sscanf reads the first string argument.
864 DSNodeHandle H = getValueDest(**AI++);
865 if (DSNode *N = H.getNode()) {
866 N->setReadMarker();
867 const Type *ArgTy = (*AI)->getType();
868 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
869 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
870 }
871 }
872
873 for (; AI != E; ++AI) {
874 // scanf writes all pointer arguments.
875 if (isPointerType((*AI)->getType()))
876 if (DSNode *N = getValueDest(**AI).getNode())
877 N->setModifiedMarker();
878 }
Chris Lattner4e46e322004-02-20 23:27:09 +0000879 return;
Chris Lattner8ecc27e2004-02-20 20:27:11 +0000880 } else if (F->getName() == "strtok") {
881 // strtok reads and writes the first argument, returning it. It reads
882 // its second arg. FIXME: strtok also modifies some hidden static
883 // data. Someday this might matter.
884 CallSite::arg_iterator AI = CS.arg_begin();
885 DSNodeHandle H = getValueDest(**AI++);
886 if (DSNode *N = H.getNode()) {
887 N->setReadMarker()->setModifiedMarker(); // Reads/Writes buffer
888 const Type *ArgTy = F->getFunctionType()->getParamType(0);
889 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
890 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
891 }
892 H.mergeWith(getValueDest(*CS.getInstruction())); // Returns buffer
893
894 H = getValueDest(**AI); // Reads delimiter
895 if (DSNode *N = H.getNode()) {
896 N->setReadMarker();
897 const Type *ArgTy = F->getFunctionType()->getParamType(1);
898 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
899 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
900 }
901 return;
Chris Lattner1fe98742004-02-26 03:43:08 +0000902 } else if (F->getName() == "strchr" || F->getName() == "strrchr" ||
903 F->getName() == "strstr") {
904 // These read their arguments, and return the first one
Chris Lattneradc1efe2004-02-25 17:43:20 +0000905 DSNodeHandle H = getValueDest(**CS.arg_begin());
Chris Lattner1fe98742004-02-26 03:43:08 +0000906 H.mergeWith(getValueDest(*CS.getInstruction())); // Returns buffer
907
908 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
909 AI != E; ++AI)
910 if (isPointerType((*AI)->getType()))
911 if (DSNode *N = getValueDest(**AI).getNode())
912 N->setReadMarker();
913
Chris Lattneradc1efe2004-02-25 17:43:20 +0000914 if (DSNode *N = H.getNode())
915 N->setReadMarker();
Chris Lattneradc1efe2004-02-25 17:43:20 +0000916 return;
Chris Lattnerbeacefa2004-11-08 21:08:28 +0000917 } else if (F->getName() == "__assert_fail") {
918 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
919 AI != E; ++AI)
920 if (isPointerType((*AI)->getType()))
921 if (DSNode *N = getValueDest(**AI).getNode())
922 N->setReadMarker();
923 return;
Chris Lattner52fc8d72004-02-25 23:06:40 +0000924 } else if (F->getName() == "modf" && CS.arg_end()-CS.arg_begin() == 2) {
925 // This writes its second argument, and forces it to double.
Chris Lattnerfe781652004-12-08 16:22:26 +0000926 CallSite::arg_iterator compit = CS.arg_end();
927 DSNodeHandle H = getValueDest(**--compit);
Chris Lattner52fc8d72004-02-25 23:06:40 +0000928 if (DSNode *N = H.getNode()) {
929 N->setModifiedMarker();
930 N->mergeTypeInfo(Type::DoubleTy, H.getOffset());
931 }
932 return;
Chris Lattner339d8df2004-02-13 21:21:48 +0000933 } else {
Chris Lattner304e1432004-02-16 22:57:19 +0000934 // Unknown function, warn if it returns a pointer type or takes a
935 // pointer argument.
936 bool Warn = isPointerType(CS.getInstruction()->getType());
937 if (!Warn)
938 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
939 I != E; ++I)
940 if (isPointerType((*I)->getType())) {
941 Warn = true;
942 break;
943 }
944 if (Warn)
945 std::cerr << "WARNING: Call to unknown external function '"
946 << F->getName() << "' will cause pessimistic results!\n";
Chris Lattnera07b72f2004-02-13 16:09:54 +0000947 }
Chris Lattner894263b2003-09-20 16:50:46 +0000948 }
949
950
Chris Lattnerc314ac42002-07-11 20:32:02 +0000951 // Set up the return value...
Chris Lattner0969c502002-10-21 02:08:03 +0000952 DSNodeHandle RetVal;
Chris Lattner808a7ae2003-09-20 16:34:13 +0000953 Instruction *I = CS.getInstruction();
954 if (isPointerType(I->getType()))
955 RetVal = getValueDest(*I);
Chris Lattnerc314ac42002-07-11 20:32:02 +0000956
Chris Lattnercb582402004-02-26 22:07:22 +0000957 DSNode *CalleeNode = 0;
958 if (DisableDirectCallOpt || !isa<Function>(Callee)) {
959 CalleeNode = getValueDest(*Callee).getNode();
960 if (CalleeNode == 0) {
961 std::cerr << "WARNING: Program is calling through a null pointer?\n"<< *I;
Chris Lattnerfbc2d842003-09-24 23:42:58 +0000962 return; // Calling a null pointer?
963 }
964 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000965
Chris Lattner0969c502002-10-21 02:08:03 +0000966 std::vector<DSNodeHandle> Args;
Chris Lattner808a7ae2003-09-20 16:34:13 +0000967 Args.reserve(CS.arg_end()-CS.arg_begin());
Chris Lattner0969c502002-10-21 02:08:03 +0000968
969 // Calculate the arguments vector...
Chris Lattner808a7ae2003-09-20 16:34:13 +0000970 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E; ++I)
971 if (isPointerType((*I)->getType()))
972 Args.push_back(getValueDest(**I));
Chris Lattner0969c502002-10-21 02:08:03 +0000973
974 // Add a new function call entry...
Chris Lattnercb582402004-02-26 22:07:22 +0000975 if (CalleeNode)
976 FunctionCalls->push_back(DSCallSite(CS, RetVal, CalleeNode, Args));
Chris Lattner923fc052003-02-05 21:59:58 +0000977 else
Chris Lattnercb582402004-02-26 22:07:22 +0000978 FunctionCalls->push_back(DSCallSite(CS, RetVal, cast<Function>(Callee),
Chris Lattner26c4fc32003-09-20 21:48:16 +0000979 Args));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000980}
Chris Lattner055dc2c2002-07-18 15:54:42 +0000981
Vikram S. Advebac06222002-12-06 21:17:10 +0000982void GraphBuilder::visitFreeInst(FreeInst &FI) {
Vikram S. Advebac06222002-12-06 21:17:10 +0000983 // Mark that the node is written to...
Chris Lattnerd5612092004-02-24 22:02:48 +0000984 if (DSNode *N = getValueDest(*FI.getOperand(0)).getNode())
985 N->setModifiedMarker()->setHeapNodeMarker();
Vikram S. Advebac06222002-12-06 21:17:10 +0000986}
987
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000988/// Handle casts...
989void GraphBuilder::visitCastInst(CastInst &CI) {
Chris Lattner5af344d2002-11-02 00:36:03 +0000990 if (isPointerType(CI.getType()))
991 if (isPointerType(CI.getOperand(0)->getType())) {
Chris Lattner157b2522004-10-06 19:29:13 +0000992 DSNodeHandle Ptr = getValueDest(*CI.getOperand(0));
993 if (Ptr.getNode() == 0) return;
994
Chris Lattner5af344d2002-11-02 00:36:03 +0000995 // Cast one pointer to the other, just act like a copy instruction
Chris Lattner157b2522004-10-06 19:29:13 +0000996 setDestTo(CI, Ptr);
Chris Lattner5af344d2002-11-02 00:36:03 +0000997 } else {
998 // Cast something (floating point, small integer) to a pointer. We need
999 // to track the fact that the node points to SOMETHING, just something we
1000 // don't know about. Make an "Unknown" node.
1001 //
Chris Lattnerbd92b732003-06-19 21:15:11 +00001002 setDestTo(CI, createNode()->setUnknownNodeMarker());
Chris Lattner5af344d2002-11-02 00:36:03 +00001003 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001004}
Chris Lattner055dc2c2002-07-18 15:54:42 +00001005
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001006
Chris Lattner878e5212003-02-04 00:59:50 +00001007// visitInstruction - For all other instruction types, if we have any arguments
1008// that are of pointer type, make them have unknown composition bits, and merge
1009// the nodes together.
1010void GraphBuilder::visitInstruction(Instruction &Inst) {
1011 DSNodeHandle CurNode;
1012 if (isPointerType(Inst.getType()))
1013 CurNode = getValueDest(Inst);
1014 for (User::op_iterator I = Inst.op_begin(), E = Inst.op_end(); I != E; ++I)
1015 if (isPointerType((*I)->getType()))
1016 CurNode.mergeWith(getValueDest(**I));
1017
Chris Lattner62c3a952004-10-30 04:22:45 +00001018 if (DSNode *N = CurNode.getNode())
1019 N->setUnknownNodeMarker();
Chris Lattner878e5212003-02-04 00:59:50 +00001020}
1021
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001022
1023
1024//===----------------------------------------------------------------------===//
1025// LocalDataStructures Implementation
1026//===----------------------------------------------------------------------===//
1027
Chris Lattner26c4fc32003-09-20 21:48:16 +00001028// MergeConstantInitIntoNode - Merge the specified constant into the node
1029// pointed to by NH.
1030void GraphBuilder::MergeConstantInitIntoNode(DSNodeHandle &NH, Constant *C) {
1031 // Ensure a type-record exists...
Chris Lattner62c3a952004-10-30 04:22:45 +00001032 DSNode *NHN = NH.getNode();
1033 NHN->mergeTypeInfo(C->getType(), NH.getOffset());
Chris Lattner26c4fc32003-09-20 21:48:16 +00001034
1035 if (C->getType()->isFirstClassType()) {
1036 if (isPointerType(C->getType()))
1037 // Avoid adding edges from null, or processing non-"pointer" stores
1038 NH.addEdgeTo(getValueDest(*C));
1039 return;
1040 }
Chris Lattner15869aa2003-11-02 22:27:28 +00001041
1042 const TargetData &TD = NH.getNode()->getTargetData();
1043
Chris Lattner26c4fc32003-09-20 21:48:16 +00001044 if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
1045 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1046 // We don't currently do any indexing for arrays...
1047 MergeConstantInitIntoNode(NH, cast<Constant>(CA->getOperand(i)));
1048 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
1049 const StructLayout *SL = TD.getStructLayout(CS->getType());
1050 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
Chris Lattner62c3a952004-10-30 04:22:45 +00001051 DSNode *NHN = NH.getNode();
Chris Lattner507bdf92005-01-12 04:51:37 +00001052 DSNodeHandle NewNH(NHN, NH.getOffset()+(unsigned)SL->MemberOffsets[i]);
Chris Lattner26c4fc32003-09-20 21:48:16 +00001053 MergeConstantInitIntoNode(NewNH, cast<Constant>(CS->getOperand(i)));
1054 }
Chris Lattner48b2f6b2004-10-26 16:23:03 +00001055 } else if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) {
Chris Lattner896481e2004-02-15 05:53:42 +00001056 // Noop
Chris Lattner26c4fc32003-09-20 21:48:16 +00001057 } else {
1058 assert(0 && "Unknown constant type!");
1059 }
1060}
1061
1062void GraphBuilder::mergeInGlobalInitializer(GlobalVariable *GV) {
1063 assert(!GV->isExternal() && "Cannot merge in external global!");
1064 // Get a node handle to the global node and merge the initializer into it.
1065 DSNodeHandle NH = getValueDest(*GV);
1066 MergeConstantInitIntoNode(NH, GV->getInitializer());
1067}
1068
1069
Chris Lattnerb12914b2004-09-20 04:48:05 +00001070bool LocalDataStructures::runOnModule(Module &M) {
Chris Lattner15869aa2003-11-02 22:27:28 +00001071 const TargetData &TD = getAnalysis<TargetData>();
Chris Lattneraa0b4682002-11-09 21:12:07 +00001072
Chris Lattnerf57cc3b2005-03-04 20:27:46 +00001073 GlobalsGraph = new DSGraph(TD);
1074
Chris Lattnerc420ab62004-02-25 23:31:02 +00001075 {
1076 GraphBuilder GGB(*GlobalsGraph);
1077
1078 // Add initializers for all of the globals to the globals graph...
1079 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
1080 if (!I->isExternal())
1081 GGB.mergeInGlobalInitializer(I);
1082 }
1083
Chris Lattneraa0b4682002-11-09 21:12:07 +00001084 // Calculate all of the graphs...
1085 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1086 if (!I->isExternal())
Chris Lattner15869aa2003-11-02 22:27:28 +00001087 DSInfo.insert(std::make_pair(I, new DSGraph(TD, *I, GlobalsGraph)));
Chris Lattner26c4fc32003-09-20 21:48:16 +00001088
Chris Lattnerc3f5f772004-02-08 01:51:48 +00001089 GlobalsGraph->removeTriviallyDeadNodes();
Chris Lattner26c4fc32003-09-20 21:48:16 +00001090 GlobalsGraph->markIncompleteNodes(DSGraph::MarkFormalArgs);
Chris Lattneraa0b4682002-11-09 21:12:07 +00001091 return false;
1092}
1093
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001094// releaseMemory - If the pass pipeline is done with this pass, we can release
1095// our memory... here...
1096//
1097void LocalDataStructures::releaseMemory() {
Chris Lattner81d924d2003-06-30 04:53:27 +00001098 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
1099 E = DSInfo.end(); I != E; ++I) {
1100 I->second->getReturnNodes().erase(I->first);
1101 if (I->second->getReturnNodes().empty())
1102 delete I->second;
1103 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001104
1105 // Empty map so next time memory is released, data structures are not
1106 // re-deleted.
1107 DSInfo.clear();
Chris Lattner2e4f9bf2002-11-09 20:01:01 +00001108 delete GlobalsGraph;
1109 GlobalsGraph = 0;
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001110}
Brian Gaeked0fde302003-11-11 22:41:34 +00001111