blob: 070b695b34c31fc3166c1ba482c519c607945750 [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 Lattnerc68c31b2002-07-10 22:38:08 +0000115 void visitSetCondInst(SetCondInst &SCI) {} // SetEQ & friends are ignored
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 Lattner51340062002-11-08 05:00:44 +0000328void GraphBuilder::visitGetElementPtrInst(User &GEP) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000329 DSNodeHandle Value = getValueDest(*GEP.getOperand(0));
Chris Lattner753b1132005-02-24 19:55:31 +0000330 if (Value.isNull())
331 Value = createNode();
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000332
Chris Lattner179bc7d2003-11-14 17:09:46 +0000333 // As a special case, if all of the index operands of GEP are constant zeros,
334 // handle this just like we handle casts (ie, don't do much).
335 bool AllZeros = true;
336 for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i)
337 if (GEP.getOperand(i) !=
338 Constant::getNullValue(GEP.getOperand(i)->getType())) {
339 AllZeros = false;
340 break;
341 }
342
343 // If all of the indices are zero, the result points to the operand without
344 // applying the type.
345 if (AllZeros) {
346 setDestTo(GEP, Value);
347 return;
348 }
349
350
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000351 const PointerType *PTy = cast<PointerType>(GEP.getOperand(0)->getType());
352 const Type *CurTy = PTy->getElementType();
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000353
Chris Lattner08db7192002-11-06 06:20:27 +0000354 if (Value.getNode()->mergeTypeInfo(CurTy, Value.getOffset())) {
355 // If the node had to be folded... exit quickly
Chris Lattner92673292002-11-02 00:13:20 +0000356 setDestTo(GEP, Value); // GEP result points to folded node
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000357 return;
358 }
359
Chris Lattner15869aa2003-11-02 22:27:28 +0000360 const TargetData &TD = Value.getNode()->getTargetData();
361
Chris Lattner08db7192002-11-06 06:20:27 +0000362#if 0
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000363 // Handle the pointer index specially...
364 if (GEP.getNumOperands() > 1 &&
Chris Lattner28977af2004-04-05 01:30:19 +0000365 (!isa<Constant>(GEP.getOperand(1)) ||
366 !cast<Constant>(GEP.getOperand(1))->isNullValue())) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000367
368 // If we already know this is an array being accessed, don't do anything...
369 if (!TopTypeRec.isArray) {
370 TopTypeRec.isArray = true;
371
372 // If we are treating some inner field pointer as an array, fold the node
373 // up because we cannot handle it right. This can come because of
374 // something like this: &((&Pt->X)[1]) == &Pt->Y
375 //
376 if (Value.getOffset()) {
377 // Value is now the pointer we want to GEP to be...
378 Value.getNode()->foldNodeCompletely();
Chris Lattner92673292002-11-02 00:13:20 +0000379 setDestTo(GEP, Value); // GEP result points to folded node
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000380 return;
381 } else {
382 // This is a pointer to the first byte of the node. Make sure that we
383 // are pointing to the outter most type in the node.
384 // FIXME: We need to check one more case here...
385 }
386 }
387 }
Chris Lattner08db7192002-11-06 06:20:27 +0000388#endif
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000389
390 // All of these subscripts are indexing INTO the elements we have...
Chris Lattner26c4fc32003-09-20 21:48:16 +0000391 unsigned Offset = 0;
Chris Lattnerfa3711a2003-11-25 20:19:55 +0000392 for (gep_type_iterator I = gep_type_begin(GEP), E = gep_type_end(GEP);
393 I != E; ++I)
394 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner507bdf92005-01-12 04:51:37 +0000395 unsigned FieldNo =
396 (unsigned)cast<ConstantUInt>(I.getOperand())->getValue();
397 Offset += (unsigned)TD.getStructLayout(STy)->MemberOffsets[FieldNo];
Chris Lattner82e9d722004-03-01 19:02:54 +0000398 } else if (const PointerType *PTy = dyn_cast<PointerType>(*I)) {
399 if (!isa<Constant>(I.getOperand()) ||
400 !cast<Constant>(I.getOperand())->isNullValue())
401 Value.getNode()->setArrayMarker();
Chris Lattnerfa3711a2003-11-25 20:19:55 +0000402 }
403
404
Chris Lattner08db7192002-11-06 06:20:27 +0000405#if 0
Chris Lattnerfa3711a2003-11-25 20:19:55 +0000406 if (const SequentialType *STy = cast<SequentialType>(*I)) {
407 CurTy = STy->getElementType();
Chris Lattnere03f32b2002-10-02 06:24:36 +0000408 if (ConstantSInt *CS = dyn_cast<ConstantSInt>(GEP.getOperand(i))) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000409 Offset += CS->getValue()*TD.getTypeSize(CurTy);
Chris Lattnere03f32b2002-10-02 06:24:36 +0000410 } else {
411 // Variable index into a node. We must merge all of the elements of the
412 // sequential type here.
413 if (isa<PointerType>(STy))
414 std::cerr << "Pointer indexing not handled yet!\n";
415 else {
416 const ArrayType *ATy = cast<ArrayType>(STy);
417 unsigned ElSize = TD.getTypeSize(CurTy);
418 DSNode *N = Value.getNode();
419 assert(N && "Value must have a node!");
420 unsigned RawOffset = Offset+Value.getOffset();
421
422 // Loop over all of the elements of the array, merging them into the
Misha Brukman2f2d0652003-09-11 18:14:24 +0000423 // zeroth element.
Chris Lattnere03f32b2002-10-02 06:24:36 +0000424 for (unsigned i = 1, e = ATy->getNumElements(); i != e; ++i)
425 // Merge all of the byte components of this array element
426 for (unsigned j = 0; j != ElSize; ++j)
427 N->mergeIndexes(RawOffset+j, RawOffset+i*ElSize+j);
428 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000429 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000430 }
Chris Lattnerfa3711a2003-11-25 20:19:55 +0000431#endif
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000432
433 // Add in the offset calculated...
434 Value.setOffset(Value.getOffset()+Offset);
435
436 // Value is now the pointer we want to GEP to be...
Chris Lattner92673292002-11-02 00:13:20 +0000437 setDestTo(GEP, Value);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000438}
439
440void GraphBuilder::visitLoadInst(LoadInst &LI) {
Chris Lattner92673292002-11-02 00:13:20 +0000441 DSNodeHandle Ptr = getValueDest(*LI.getOperand(0));
Chris Lattner6e84bd72005-02-25 01:27:48 +0000442 if (Ptr.isNull())
443 Ptr = createNode();
Chris Lattner92673292002-11-02 00:13:20 +0000444
445 // Make that the node is read from...
Chris Lattnerbd92b732003-06-19 21:15:11 +0000446 Ptr.getNode()->setReadMarker();
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000447
448 // Ensure a typerecord exists...
Chris Lattner088b6392003-03-03 17:13:31 +0000449 Ptr.getNode()->mergeTypeInfo(LI.getType(), Ptr.getOffset(), false);
Chris Lattner92673292002-11-02 00:13:20 +0000450
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000451 if (isPointerType(LI.getType()))
Chris Lattner92673292002-11-02 00:13:20 +0000452 setDestTo(LI, getLink(Ptr));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000453}
454
455void GraphBuilder::visitStoreInst(StoreInst &SI) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000456 const Type *StoredTy = SI.getOperand(0)->getType();
Chris Lattner92673292002-11-02 00:13:20 +0000457 DSNodeHandle Dest = getValueDest(*SI.getOperand(1));
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000458 if (Dest.isNull()) return;
Chris Lattner92673292002-11-02 00:13:20 +0000459
Vikram S. Advebac06222002-12-06 21:17:10 +0000460 // Mark that the node is written to...
Chris Lattnerbd92b732003-06-19 21:15:11 +0000461 Dest.getNode()->setModifiedMarker();
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000462
Chris Lattner26c4fc32003-09-20 21:48:16 +0000463 // Ensure a type-record exists...
Chris Lattner08db7192002-11-06 06:20:27 +0000464 Dest.getNode()->mergeTypeInfo(StoredTy, Dest.getOffset());
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000465
466 // Avoid adding edges from null, or processing non-"pointer" stores
Chris Lattner92673292002-11-02 00:13:20 +0000467 if (isPointerType(StoredTy))
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000468 Dest.addEdgeTo(getValueDest(*SI.getOperand(0)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000469}
470
471void GraphBuilder::visitReturnInst(ReturnInst &RI) {
Chris Lattner92673292002-11-02 00:13:20 +0000472 if (RI.getNumOperands() && isPointerType(RI.getOperand(0)->getType()))
Chris Lattner26c4fc32003-09-20 21:48:16 +0000473 RetNode->mergeWith(getValueDest(*RI.getOperand(0)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000474}
475
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000476void GraphBuilder::visitVANextInst(VANextInst &I) {
477 getValueDest(*I.getOperand(0)).mergeWith(getValueDest(I));
478}
479
480void GraphBuilder::visitVAArgInst(VAArgInst &I) {
481 DSNodeHandle Ptr = getValueDest(*I.getOperand(0));
482 if (Ptr.isNull()) return;
483
484 // Make that the node is read from.
485 Ptr.getNode()->setReadMarker();
486
Chris Lattner62c3a952004-10-30 04:22:45 +0000487 // Ensure a type record exists.
488 DSNode *PtrN = Ptr.getNode();
489 PtrN->mergeTypeInfo(I.getType(), Ptr.getOffset(), false);
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000490
491 if (isPointerType(I.getType()))
492 setDestTo(I, getLink(Ptr));
493}
494
495
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000496void GraphBuilder::visitCallInst(CallInst &CI) {
Chris Lattner808a7ae2003-09-20 16:34:13 +0000497 visitCallSite(&CI);
498}
499
500void GraphBuilder::visitInvokeInst(InvokeInst &II) {
501 visitCallSite(&II);
502}
503
504void GraphBuilder::visitCallSite(CallSite CS) {
Chris Lattnercb582402004-02-26 22:07:22 +0000505 Value *Callee = CS.getCalledValue();
Chris Lattnercb582402004-02-26 22:07:22 +0000506
Chris Lattner894263b2003-09-20 16:50:46 +0000507 // Special case handling of certain libc allocation functions here.
Chris Lattnercb582402004-02-26 22:07:22 +0000508 if (Function *F = dyn_cast<Function>(Callee))
Chris Lattner894263b2003-09-20 16:50:46 +0000509 if (F->isExternal())
Chris Lattnera07b72f2004-02-13 16:09:54 +0000510 switch (F->getIntrinsicID()) {
Chris Lattner317201d2004-03-13 00:24:00 +0000511 case Intrinsic::vastart:
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000512 getValueDest(*CS.getInstruction()).getNode()->setAllocaNodeMarker();
513 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000514 case Intrinsic::vacopy:
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000515 getValueDest(*CS.getInstruction()).
516 mergeWith(getValueDest(**(CS.arg_begin())));
517 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000518 case Intrinsic::vaend:
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000519 return; // noop
Chris Lattnera07b72f2004-02-13 16:09:54 +0000520 case Intrinsic::memmove:
521 case Intrinsic::memcpy: {
522 // Merge the first & second arguments, and mark the memory read and
Chris Lattnerfb8c6102003-11-08 21:55:50 +0000523 // modified.
Chris Lattnera07b72f2004-02-13 16:09:54 +0000524 DSNodeHandle RetNH = getValueDest(**CS.arg_begin());
Chris Lattner67ce57a2003-11-09 03:32:52 +0000525 RetNH.mergeWith(getValueDest(**(CS.arg_begin()+1)));
526 if (DSNode *N = RetNH.getNode())
527 N->setModifiedMarker()->setReadMarker();
528 return;
Chris Lattnera07b72f2004-02-13 16:09:54 +0000529 }
Chris Lattnereee33b22004-02-16 18:37:40 +0000530 case Intrinsic::memset:
531 // Mark the memory modified.
532 if (DSNode *N = getValueDest(**CS.arg_begin()).getNode())
533 N->setModifiedMarker();
534 return;
Chris Lattnera07b72f2004-02-13 16:09:54 +0000535 default:
Vikram S. Advee4e97ef2004-05-25 08:14:52 +0000536 if (F->getName() == "calloc" || F->getName() == "posix_memalign" ||
537 F->getName() == "memalign" || F->getName() == "valloc") {
Chris Lattnera07b72f2004-02-13 16:09:54 +0000538 setDestTo(*CS.getInstruction(),
539 createNode()->setHeapNodeMarker()->setModifiedMarker());
540 return;
541 } else if (F->getName() == "realloc") {
542 DSNodeHandle RetNH = getValueDest(*CS.getInstruction());
Chris Lattner82962de2004-11-03 18:51:26 +0000543 if (CS.arg_begin() != CS.arg_end())
544 RetNH.mergeWith(getValueDest(**CS.arg_begin()));
Chris Lattnera07b72f2004-02-13 16:09:54 +0000545 if (DSNode *N = RetNH.getNode())
546 N->setHeapNodeMarker()->setModifiedMarker()->setReadMarker();
547 return;
Chris Lattner1fe98742004-02-26 03:43:08 +0000548 } else if (F->getName() == "memmove") {
549 // Merge the first & second arguments, and mark the memory read and
550 // modified.
551 DSNodeHandle RetNH = getValueDest(**CS.arg_begin());
552 RetNH.mergeWith(getValueDest(**(CS.arg_begin()+1)));
553 if (DSNode *N = RetNH.getNode())
554 N->setModifiedMarker()->setReadMarker();
555 return;
556
Chris Lattnerd5612092004-02-24 22:02:48 +0000557 } else if (F->getName() == "atoi" || F->getName() == "atof" ||
Chris Lattneradc1efe2004-02-25 17:43:20 +0000558 F->getName() == "atol" || F->getName() == "atoll" ||
Chris Lattner39bb2dc2004-02-24 22:17:00 +0000559 F->getName() == "remove" || F->getName() == "unlink" ||
Chris Lattneradc1efe2004-02-25 17:43:20 +0000560 F->getName() == "rename" || F->getName() == "memcmp" ||
561 F->getName() == "strcmp" || F->getName() == "strncmp" ||
562 F->getName() == "execl" || F->getName() == "execlp" ||
563 F->getName() == "execle" || F->getName() == "execv" ||
Chris Lattner52fc8d72004-02-25 23:06:40 +0000564 F->getName() == "execvp" || F->getName() == "chmod" ||
565 F->getName() == "puts" || F->getName() == "write" ||
566 F->getName() == "open" || F->getName() == "create" ||
567 F->getName() == "truncate" || F->getName() == "chdir" ||
568 F->getName() == "mkdir" || F->getName() == "rmdir") {
Chris Lattner39bb2dc2004-02-24 22:17:00 +0000569 // These functions read all of their pointer operands.
570 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
571 AI != E; ++AI) {
572 if (isPointerType((*AI)->getType()))
573 if (DSNode *N = getValueDest(**AI).getNode())
574 N->setReadMarker();
575 }
Chris Lattner304e1432004-02-16 22:57:19 +0000576 return;
Chris Lattner52fc8d72004-02-25 23:06:40 +0000577 } else if (F->getName() == "read" || F->getName() == "pipe" ||
Chris Lattner6b586df2004-02-27 20:04:48 +0000578 F->getName() == "wait" || F->getName() == "time") {
Chris Lattner52fc8d72004-02-25 23:06:40 +0000579 // These functions write all of their pointer operands.
580 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
581 AI != E; ++AI) {
582 if (isPointerType((*AI)->getType()))
583 if (DSNode *N = getValueDest(**AI).getNode())
584 N->setModifiedMarker();
585 }
586 return;
Chris Lattneradc1efe2004-02-25 17:43:20 +0000587 } else if (F->getName() == "stat" || F->getName() == "fstat" ||
588 F->getName() == "lstat") {
589 // These functions read their first operand if its a pointer.
590 CallSite::arg_iterator AI = CS.arg_begin();
591 if (isPointerType((*AI)->getType())) {
592 DSNodeHandle Path = getValueDest(**AI);
593 if (DSNode *N = Path.getNode()) N->setReadMarker();
594 }
Chris Lattner304e1432004-02-16 22:57:19 +0000595
Chris Lattneradc1efe2004-02-25 17:43:20 +0000596 // Then they write into the stat buffer.
597 DSNodeHandle StatBuf = getValueDest(**++AI);
598 if (DSNode *N = StatBuf.getNode()) {
599 N->setModifiedMarker();
600 const Type *StatTy = F->getFunctionType()->getParamType(1);
601 if (const PointerType *PTy = dyn_cast<PointerType>(StatTy))
602 N->mergeTypeInfo(PTy->getElementType(), StatBuf.getOffset());
603 }
Chris Lattneradc1efe2004-02-25 17:43:20 +0000604 return;
Chris Lattner3aeb40c2004-03-04 21:03:54 +0000605 } else if (F->getName() == "strtod" || F->getName() == "strtof" ||
606 F->getName() == "strtold") {
607 // These functions read the first pointer
608 if (DSNode *Str = getValueDest(**CS.arg_begin()).getNode()) {
609 Str->setReadMarker();
610 // If the second parameter is passed, it will point to the first
611 // argument node.
612 const DSNodeHandle &EndPtrNH = getValueDest(**(CS.arg_begin()+1));
613 if (DSNode *End = EndPtrNH.getNode()) {
614 End->mergeTypeInfo(PointerType::get(Type::SByteTy),
615 EndPtrNH.getOffset(), false);
616 End->setModifiedMarker();
617 DSNodeHandle &Link = getLink(EndPtrNH);
618 Link.mergeWith(getValueDest(**CS.arg_begin()));
619 }
620 }
621
622 return;
Chris Lattner6b586df2004-02-27 20:04:48 +0000623 } else if (F->getName() == "fopen" || F->getName() == "fdopen" ||
624 F->getName() == "freopen") {
625 // These functions read all of their pointer operands.
626 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
627 AI != E; ++AI)
628 if (isPointerType((*AI)->getType()))
629 if (DSNode *N = getValueDest(**AI).getNode())
630 N->setReadMarker();
Chris Lattner68300db2004-02-13 20:05:32 +0000631
632 // fopen allocates in an unknown way and writes to the file
633 // descriptor. Also, merge the allocated type into the node.
634 DSNodeHandle Result = getValueDest(*CS.getInstruction());
Chris Lattnerd5612092004-02-24 22:02:48 +0000635 if (DSNode *N = Result.getNode()) {
636 N->setModifiedMarker()->setUnknownNodeMarker();
637 const Type *RetTy = F->getFunctionType()->getReturnType();
638 if (const PointerType *PTy = dyn_cast<PointerType>(RetTy))
639 N->mergeTypeInfo(PTy->getElementType(), Result.getOffset());
640 }
Chris Lattner6b586df2004-02-27 20:04:48 +0000641
642 // If this is freopen, merge the file descriptor passed in with the
643 // result.
Chris Lattnerfe781652004-12-08 16:22:26 +0000644 if (F->getName() == "freopen") {
645 // ICC doesn't handle getting the iterator, decrementing and
646 // dereferencing it in one operation without error. Do it in 2 steps
647 CallSite::arg_iterator compit = CS.arg_end();
648 Result.mergeWith(getValueDest(**--compit));
649 }
Chris Lattnerd5612092004-02-24 22:02:48 +0000650 return;
Chris Lattner68300db2004-02-13 20:05:32 +0000651 } else if (F->getName() == "fclose" && CS.arg_end()-CS.arg_begin() ==1){
652 // fclose reads and deallocates the memory in an unknown way for the
653 // file descriptor. It merges the FILE type into the descriptor.
654 DSNodeHandle H = getValueDest(**CS.arg_begin());
Chris Lattnerd5612092004-02-24 22:02:48 +0000655 if (DSNode *N = H.getNode()) {
656 N->setReadMarker()->setUnknownNodeMarker();
657 const Type *ArgTy = F->getFunctionType()->getParamType(0);
658 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
659 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
660 }
Chris Lattner68300db2004-02-13 20:05:32 +0000661 return;
Chris Lattner304e1432004-02-16 22:57:19 +0000662 } else if (CS.arg_end()-CS.arg_begin() == 1 &&
663 (F->getName() == "fflush" || F->getName() == "feof" ||
664 F->getName() == "fileno" || F->getName() == "clearerr" ||
Chris Lattner52fc8d72004-02-25 23:06:40 +0000665 F->getName() == "rewind" || F->getName() == "ftell" ||
Chris Lattner6b586df2004-02-27 20:04:48 +0000666 F->getName() == "ferror" || F->getName() == "fgetc" ||
667 F->getName() == "fgetc" || F->getName() == "_IO_getc")) {
Chris Lattner304e1432004-02-16 22:57:19 +0000668 // fflush reads and writes the memory for the file descriptor. It
Chris Lattner339d8df2004-02-13 21:21:48 +0000669 // merges the FILE type into the descriptor.
670 DSNodeHandle H = getValueDest(**CS.arg_begin());
Chris Lattnerd5612092004-02-24 22:02:48 +0000671 if (DSNode *N = H.getNode()) {
672 N->setReadMarker()->setModifiedMarker();
Chris Lattner339d8df2004-02-13 21:21:48 +0000673
Chris Lattnerd5612092004-02-24 22:02:48 +0000674 const Type *ArgTy = F->getFunctionType()->getParamType(0);
675 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
676 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
677 }
678 return;
679 } else if (CS.arg_end()-CS.arg_begin() == 4 &&
680 (F->getName() == "fwrite" || F->getName() == "fread")) {
681 // fread writes the first operand, fwrite reads it. They both
682 // read/write the FILE descriptor, and merges the FILE type.
Chris Lattnerfe781652004-12-08 16:22:26 +0000683 CallSite::arg_iterator compit = CS.arg_end();
684 DSNodeHandle H = getValueDest(**--compit);
Chris Lattnerd5612092004-02-24 22:02:48 +0000685 if (DSNode *N = H.getNode()) {
686 N->setReadMarker()->setModifiedMarker();
687 const Type *ArgTy = F->getFunctionType()->getParamType(3);
688 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
689 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
690 }
691
692 H = getValueDest(**CS.arg_begin());
693 if (DSNode *N = H.getNode())
694 if (F->getName() == "fwrite")
695 N->setReadMarker();
696 else
697 N->setModifiedMarker();
Chris Lattner339d8df2004-02-13 21:21:48 +0000698 return;
699 } else if (F->getName() == "fgets" && CS.arg_end()-CS.arg_begin() == 3){
Chris Lattner304e1432004-02-16 22:57:19 +0000700 // fgets reads and writes the memory for the file descriptor. It
Chris Lattner339d8df2004-02-13 21:21:48 +0000701 // merges the FILE type into the descriptor, and writes to the
702 // argument. It returns the argument as well.
703 CallSite::arg_iterator AI = CS.arg_begin();
704 DSNodeHandle H = getValueDest(**AI);
705 if (DSNode *N = H.getNode())
706 N->setModifiedMarker(); // Writes buffer
707 H.mergeWith(getValueDest(*CS.getInstruction())); // Returns buffer
708 ++AI; ++AI;
709
710 // Reads and writes file descriptor, merge in FILE type.
Chris Lattneradc1efe2004-02-25 17:43:20 +0000711 H = getValueDest(**AI);
Chris Lattnerd5612092004-02-24 22:02:48 +0000712 if (DSNode *N = H.getNode()) {
Chris Lattner339d8df2004-02-13 21:21:48 +0000713 N->setReadMarker()->setModifiedMarker();
Chris Lattneradc1efe2004-02-25 17:43:20 +0000714 const Type *ArgTy = F->getFunctionType()->getParamType(2);
715 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
716 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
717 }
718 return;
Chris Lattner52fc8d72004-02-25 23:06:40 +0000719 } else if (F->getName() == "ungetc" || F->getName() == "fputc" ||
720 F->getName() == "fputs" || F->getName() == "putc" ||
Chris Lattner6b586df2004-02-27 20:04:48 +0000721 F->getName() == "ftell" || F->getName() == "rewind" ||
722 F->getName() == "_IO_putc") {
723 // These functions read and write the memory for the file descriptor,
724 // which is passes as the last argument.
Chris Lattnerfe781652004-12-08 16:22:26 +0000725 CallSite::arg_iterator compit = CS.arg_end();
726 DSNodeHandle H = getValueDest(**--compit);
Chris Lattneradc1efe2004-02-25 17:43:20 +0000727 if (DSNode *N = H.getNode()) {
728 N->setReadMarker()->setModifiedMarker();
Chris Lattnerfe781652004-12-08 16:22:26 +0000729 FunctionType::param_iterator compit2 = F->getFunctionType()->param_end();
730 const Type *ArgTy = *--compit2;
Chris Lattnerd5612092004-02-24 22:02:48 +0000731 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
732 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
733 }
Chris Lattner52fc8d72004-02-25 23:06:40 +0000734
735 // Any pointer arguments are read.
736 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
737 AI != E; ++AI)
738 if (isPointerType((*AI)->getType()))
739 if (DSNode *N = getValueDest(**AI).getNode())
740 N->setReadMarker();
741 return;
742 } else if (F->getName() == "fseek" || F->getName() == "fgetpos" ||
743 F->getName() == "fsetpos") {
744 // These functions read and write the memory for the file descriptor,
745 // and read/write all other arguments.
746 DSNodeHandle H = getValueDest(**CS.arg_begin());
747 if (DSNode *N = H.getNode()) {
Chris Lattnerfe781652004-12-08 16:22:26 +0000748 FunctionType::param_iterator compit2 = F->getFunctionType()->param_end();
749 const Type *ArgTy = *--compit2;
Chris Lattner52fc8d72004-02-25 23:06:40 +0000750 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
751 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
752 }
753
754 // Any pointer arguments are read.
755 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
756 AI != E; ++AI)
757 if (isPointerType((*AI)->getType()))
758 if (DSNode *N = getValueDest(**AI).getNode())
759 N->setReadMarker()->setModifiedMarker();
Chris Lattner339d8df2004-02-13 21:21:48 +0000760 return;
Chris Lattner8ecc27e2004-02-20 20:27:11 +0000761 } else if (F->getName() == "printf" || F->getName() == "fprintf" ||
762 F->getName() == "sprintf") {
Chris Lattner339d8df2004-02-13 21:21:48 +0000763 CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
764
765 if (F->getName() == "fprintf") {
766 // fprintf reads and writes the FILE argument, and applies the type
767 // to it.
768 DSNodeHandle H = getValueDest(**AI);
769 if (DSNode *N = H.getNode()) {
770 N->setModifiedMarker();
771 const Type *ArgTy = (*AI)->getType();
772 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
773 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
774 }
Chris Lattner8ecc27e2004-02-20 20:27:11 +0000775 } else if (F->getName() == "sprintf") {
776 // sprintf writes the first string argument.
777 DSNodeHandle H = getValueDest(**AI++);
778 if (DSNode *N = H.getNode()) {
779 N->setModifiedMarker();
780 const Type *ArgTy = (*AI)->getType();
781 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
782 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
783 }
Chris Lattner339d8df2004-02-13 21:21:48 +0000784 }
785
786 for (; AI != E; ++AI) {
787 // printf reads all pointer arguments.
788 if (isPointerType((*AI)->getType()))
789 if (DSNode *N = getValueDest(**AI).getNode())
790 N->setReadMarker();
791 }
Chris Lattner4e46e322004-02-20 23:27:09 +0000792 return;
Chris Lattner3aeb40c2004-03-04 21:03:54 +0000793 } else if (F->getName() == "vprintf" || F->getName() == "vfprintf" ||
794 F->getName() == "vsprintf") {
795 CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
796
797 if (F->getName() == "vfprintf") {
798 // ffprintf reads and writes the FILE argument, and applies the type
799 // to it.
800 DSNodeHandle H = getValueDest(**AI);
801 if (DSNode *N = H.getNode()) {
802 N->setModifiedMarker()->setReadMarker();
803 const Type *ArgTy = (*AI)->getType();
804 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
805 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
806 }
807 ++AI;
808 } else if (F->getName() == "vsprintf") {
809 // vsprintf writes the first string argument.
810 DSNodeHandle H = getValueDest(**AI++);
811 if (DSNode *N = H.getNode()) {
812 N->setModifiedMarker();
813 const Type *ArgTy = (*AI)->getType();
814 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
815 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
816 }
817 }
818
819 // Read the format
820 if (AI != E) {
821 if (isPointerType((*AI)->getType()))
822 if (DSNode *N = getValueDest(**AI).getNode())
823 N->setReadMarker();
824 ++AI;
825 }
826
827 // Read the valist, and the pointed-to objects.
828 if (AI != E && isPointerType((*AI)->getType())) {
829 const DSNodeHandle &VAList = getValueDest(**AI);
830 if (DSNode *N = VAList.getNode()) {
831 N->setReadMarker();
832 N->mergeTypeInfo(PointerType::get(Type::SByteTy),
833 VAList.getOffset(), false);
834
835 DSNodeHandle &VAListObjs = getLink(VAList);
836 VAListObjs.getNode()->setReadMarker();
837 }
838 }
839
840 return;
Chris Lattner8ecc27e2004-02-20 20:27:11 +0000841 } else if (F->getName() == "scanf" || F->getName() == "fscanf" ||
842 F->getName() == "sscanf") {
843 CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
Chris Lattner339d8df2004-02-13 21:21:48 +0000844
Chris Lattner8ecc27e2004-02-20 20:27:11 +0000845 if (F->getName() == "fscanf") {
846 // fscanf reads and writes the FILE argument, and applies the type
847 // to it.
848 DSNodeHandle H = getValueDest(**AI);
849 if (DSNode *N = H.getNode()) {
850 N->setReadMarker();
851 const Type *ArgTy = (*AI)->getType();
852 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
853 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
854 }
855 } else if (F->getName() == "sscanf") {
856 // sscanf reads the first string argument.
857 DSNodeHandle H = getValueDest(**AI++);
858 if (DSNode *N = H.getNode()) {
859 N->setReadMarker();
860 const Type *ArgTy = (*AI)->getType();
861 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
862 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
863 }
864 }
865
866 for (; AI != E; ++AI) {
867 // scanf writes all pointer arguments.
868 if (isPointerType((*AI)->getType()))
869 if (DSNode *N = getValueDest(**AI).getNode())
870 N->setModifiedMarker();
871 }
Chris Lattner4e46e322004-02-20 23:27:09 +0000872 return;
Chris Lattner8ecc27e2004-02-20 20:27:11 +0000873 } else if (F->getName() == "strtok") {
874 // strtok reads and writes the first argument, returning it. It reads
875 // its second arg. FIXME: strtok also modifies some hidden static
876 // data. Someday this might matter.
877 CallSite::arg_iterator AI = CS.arg_begin();
878 DSNodeHandle H = getValueDest(**AI++);
879 if (DSNode *N = H.getNode()) {
880 N->setReadMarker()->setModifiedMarker(); // Reads/Writes buffer
881 const Type *ArgTy = F->getFunctionType()->getParamType(0);
882 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
883 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
884 }
885 H.mergeWith(getValueDest(*CS.getInstruction())); // Returns buffer
886
887 H = getValueDest(**AI); // Reads delimiter
888 if (DSNode *N = H.getNode()) {
889 N->setReadMarker();
890 const Type *ArgTy = F->getFunctionType()->getParamType(1);
891 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
892 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
893 }
894 return;
Chris Lattner1fe98742004-02-26 03:43:08 +0000895 } else if (F->getName() == "strchr" || F->getName() == "strrchr" ||
896 F->getName() == "strstr") {
897 // These read their arguments, and return the first one
Chris Lattneradc1efe2004-02-25 17:43:20 +0000898 DSNodeHandle H = getValueDest(**CS.arg_begin());
Chris Lattner1fe98742004-02-26 03:43:08 +0000899 H.mergeWith(getValueDest(*CS.getInstruction())); // Returns buffer
900
901 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
902 AI != E; ++AI)
903 if (isPointerType((*AI)->getType()))
904 if (DSNode *N = getValueDest(**AI).getNode())
905 N->setReadMarker();
906
Chris Lattneradc1efe2004-02-25 17:43:20 +0000907 if (DSNode *N = H.getNode())
908 N->setReadMarker();
Chris Lattneradc1efe2004-02-25 17:43:20 +0000909 return;
Chris Lattnerbeacefa2004-11-08 21:08:28 +0000910 } else if (F->getName() == "__assert_fail") {
911 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
912 AI != E; ++AI)
913 if (isPointerType((*AI)->getType()))
914 if (DSNode *N = getValueDest(**AI).getNode())
915 N->setReadMarker();
916 return;
Chris Lattner52fc8d72004-02-25 23:06:40 +0000917 } else if (F->getName() == "modf" && CS.arg_end()-CS.arg_begin() == 2) {
918 // This writes its second argument, and forces it to double.
Chris Lattnerfe781652004-12-08 16:22:26 +0000919 CallSite::arg_iterator compit = CS.arg_end();
920 DSNodeHandle H = getValueDest(**--compit);
Chris Lattner52fc8d72004-02-25 23:06:40 +0000921 if (DSNode *N = H.getNode()) {
922 N->setModifiedMarker();
923 N->mergeTypeInfo(Type::DoubleTy, H.getOffset());
924 }
925 return;
Chris Lattner339d8df2004-02-13 21:21:48 +0000926 } else {
Chris Lattner304e1432004-02-16 22:57:19 +0000927 // Unknown function, warn if it returns a pointer type or takes a
928 // pointer argument.
929 bool Warn = isPointerType(CS.getInstruction()->getType());
930 if (!Warn)
931 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
932 I != E; ++I)
933 if (isPointerType((*I)->getType())) {
934 Warn = true;
935 break;
936 }
937 if (Warn)
938 std::cerr << "WARNING: Call to unknown external function '"
939 << F->getName() << "' will cause pessimistic results!\n";
Chris Lattnera07b72f2004-02-13 16:09:54 +0000940 }
Chris Lattner894263b2003-09-20 16:50:46 +0000941 }
942
943
Chris Lattnerc314ac42002-07-11 20:32:02 +0000944 // Set up the return value...
Chris Lattner0969c502002-10-21 02:08:03 +0000945 DSNodeHandle RetVal;
Chris Lattner808a7ae2003-09-20 16:34:13 +0000946 Instruction *I = CS.getInstruction();
947 if (isPointerType(I->getType()))
948 RetVal = getValueDest(*I);
Chris Lattnerc314ac42002-07-11 20:32:02 +0000949
Chris Lattnercb582402004-02-26 22:07:22 +0000950 DSNode *CalleeNode = 0;
951 if (DisableDirectCallOpt || !isa<Function>(Callee)) {
952 CalleeNode = getValueDest(*Callee).getNode();
953 if (CalleeNode == 0) {
954 std::cerr << "WARNING: Program is calling through a null pointer?\n"<< *I;
Chris Lattnerfbc2d842003-09-24 23:42:58 +0000955 return; // Calling a null pointer?
956 }
957 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000958
Chris Lattner0969c502002-10-21 02:08:03 +0000959 std::vector<DSNodeHandle> Args;
Chris Lattner808a7ae2003-09-20 16:34:13 +0000960 Args.reserve(CS.arg_end()-CS.arg_begin());
Chris Lattner0969c502002-10-21 02:08:03 +0000961
962 // Calculate the arguments vector...
Chris Lattner808a7ae2003-09-20 16:34:13 +0000963 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E; ++I)
964 if (isPointerType((*I)->getType()))
965 Args.push_back(getValueDest(**I));
Chris Lattner0969c502002-10-21 02:08:03 +0000966
967 // Add a new function call entry...
Chris Lattnercb582402004-02-26 22:07:22 +0000968 if (CalleeNode)
969 FunctionCalls->push_back(DSCallSite(CS, RetVal, CalleeNode, Args));
Chris Lattner923fc052003-02-05 21:59:58 +0000970 else
Chris Lattnercb582402004-02-26 22:07:22 +0000971 FunctionCalls->push_back(DSCallSite(CS, RetVal, cast<Function>(Callee),
Chris Lattner26c4fc32003-09-20 21:48:16 +0000972 Args));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000973}
Chris Lattner055dc2c2002-07-18 15:54:42 +0000974
Vikram S. Advebac06222002-12-06 21:17:10 +0000975void GraphBuilder::visitFreeInst(FreeInst &FI) {
Vikram S. Advebac06222002-12-06 21:17:10 +0000976 // Mark that the node is written to...
Chris Lattnerd5612092004-02-24 22:02:48 +0000977 if (DSNode *N = getValueDest(*FI.getOperand(0)).getNode())
978 N->setModifiedMarker()->setHeapNodeMarker();
Vikram S. Advebac06222002-12-06 21:17:10 +0000979}
980
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000981/// Handle casts...
982void GraphBuilder::visitCastInst(CastInst &CI) {
Chris Lattner5af344d2002-11-02 00:36:03 +0000983 if (isPointerType(CI.getType()))
984 if (isPointerType(CI.getOperand(0)->getType())) {
Chris Lattner157b2522004-10-06 19:29:13 +0000985 DSNodeHandle Ptr = getValueDest(*CI.getOperand(0));
986 if (Ptr.getNode() == 0) return;
987
Chris Lattner5af344d2002-11-02 00:36:03 +0000988 // Cast one pointer to the other, just act like a copy instruction
Chris Lattner157b2522004-10-06 19:29:13 +0000989 setDestTo(CI, Ptr);
Chris Lattner5af344d2002-11-02 00:36:03 +0000990 } else {
991 // Cast something (floating point, small integer) to a pointer. We need
992 // to track the fact that the node points to SOMETHING, just something we
993 // don't know about. Make an "Unknown" node.
994 //
Chris Lattnerbd92b732003-06-19 21:15:11 +0000995 setDestTo(CI, createNode()->setUnknownNodeMarker());
Chris Lattner5af344d2002-11-02 00:36:03 +0000996 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000997}
Chris Lattner055dc2c2002-07-18 15:54:42 +0000998
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000999
Chris Lattner878e5212003-02-04 00:59:50 +00001000// visitInstruction - For all other instruction types, if we have any arguments
1001// that are of pointer type, make them have unknown composition bits, and merge
1002// the nodes together.
1003void GraphBuilder::visitInstruction(Instruction &Inst) {
1004 DSNodeHandle CurNode;
1005 if (isPointerType(Inst.getType()))
1006 CurNode = getValueDest(Inst);
1007 for (User::op_iterator I = Inst.op_begin(), E = Inst.op_end(); I != E; ++I)
1008 if (isPointerType((*I)->getType()))
1009 CurNode.mergeWith(getValueDest(**I));
1010
Chris Lattner62c3a952004-10-30 04:22:45 +00001011 if (DSNode *N = CurNode.getNode())
1012 N->setUnknownNodeMarker();
Chris Lattner878e5212003-02-04 00:59:50 +00001013}
1014
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001015
1016
1017//===----------------------------------------------------------------------===//
1018// LocalDataStructures Implementation
1019//===----------------------------------------------------------------------===//
1020
Chris Lattner26c4fc32003-09-20 21:48:16 +00001021// MergeConstantInitIntoNode - Merge the specified constant into the node
1022// pointed to by NH.
1023void GraphBuilder::MergeConstantInitIntoNode(DSNodeHandle &NH, Constant *C) {
1024 // Ensure a type-record exists...
Chris Lattner62c3a952004-10-30 04:22:45 +00001025 DSNode *NHN = NH.getNode();
1026 NHN->mergeTypeInfo(C->getType(), NH.getOffset());
Chris Lattner26c4fc32003-09-20 21:48:16 +00001027
1028 if (C->getType()->isFirstClassType()) {
1029 if (isPointerType(C->getType()))
1030 // Avoid adding edges from null, or processing non-"pointer" stores
1031 NH.addEdgeTo(getValueDest(*C));
1032 return;
1033 }
Chris Lattner15869aa2003-11-02 22:27:28 +00001034
1035 const TargetData &TD = NH.getNode()->getTargetData();
1036
Chris Lattner26c4fc32003-09-20 21:48:16 +00001037 if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
1038 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1039 // We don't currently do any indexing for arrays...
1040 MergeConstantInitIntoNode(NH, cast<Constant>(CA->getOperand(i)));
1041 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
1042 const StructLayout *SL = TD.getStructLayout(CS->getType());
1043 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
Chris Lattner62c3a952004-10-30 04:22:45 +00001044 DSNode *NHN = NH.getNode();
Chris Lattner507bdf92005-01-12 04:51:37 +00001045 DSNodeHandle NewNH(NHN, NH.getOffset()+(unsigned)SL->MemberOffsets[i]);
Chris Lattner26c4fc32003-09-20 21:48:16 +00001046 MergeConstantInitIntoNode(NewNH, cast<Constant>(CS->getOperand(i)));
1047 }
Chris Lattner48b2f6b2004-10-26 16:23:03 +00001048 } else if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) {
Chris Lattner896481e2004-02-15 05:53:42 +00001049 // Noop
Chris Lattner26c4fc32003-09-20 21:48:16 +00001050 } else {
1051 assert(0 && "Unknown constant type!");
1052 }
1053}
1054
1055void GraphBuilder::mergeInGlobalInitializer(GlobalVariable *GV) {
1056 assert(!GV->isExternal() && "Cannot merge in external global!");
1057 // Get a node handle to the global node and merge the initializer into it.
1058 DSNodeHandle NH = getValueDest(*GV);
1059 MergeConstantInitIntoNode(NH, GV->getInitializer());
1060}
1061
1062
Chris Lattnerb12914b2004-09-20 04:48:05 +00001063bool LocalDataStructures::runOnModule(Module &M) {
Chris Lattner15869aa2003-11-02 22:27:28 +00001064 const TargetData &TD = getAnalysis<TargetData>();
Chris Lattneraa0b4682002-11-09 21:12:07 +00001065
Chris Lattnerf57cc3b2005-03-04 20:27:46 +00001066 GlobalsGraph = new DSGraph(TD);
1067
Chris Lattnerc420ab62004-02-25 23:31:02 +00001068 {
1069 GraphBuilder GGB(*GlobalsGraph);
1070
1071 // Add initializers for all of the globals to the globals graph...
1072 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
1073 if (!I->isExternal())
1074 GGB.mergeInGlobalInitializer(I);
1075 }
1076
Chris Lattneraa0b4682002-11-09 21:12:07 +00001077 // Calculate all of the graphs...
1078 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1079 if (!I->isExternal())
Chris Lattner15869aa2003-11-02 22:27:28 +00001080 DSInfo.insert(std::make_pair(I, new DSGraph(TD, *I, GlobalsGraph)));
Chris Lattner26c4fc32003-09-20 21:48:16 +00001081
Chris Lattnerc3f5f772004-02-08 01:51:48 +00001082 GlobalsGraph->removeTriviallyDeadNodes();
Chris Lattner26c4fc32003-09-20 21:48:16 +00001083 GlobalsGraph->markIncompleteNodes(DSGraph::MarkFormalArgs);
Chris Lattneraa0b4682002-11-09 21:12:07 +00001084 return false;
1085}
1086
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001087// releaseMemory - If the pass pipeline is done with this pass, we can release
1088// our memory... here...
1089//
1090void LocalDataStructures::releaseMemory() {
Chris Lattner81d924d2003-06-30 04:53:27 +00001091 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
1092 E = DSInfo.end(); I != E; ++I) {
1093 I->second->getReturnNodes().erase(I->first);
1094 if (I->second->getReturnNodes().empty())
1095 delete I->second;
1096 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001097
1098 // Empty map so next time memory is released, data structures are not
1099 // re-deleted.
1100 DSInfo.clear();
Chris Lattner2e4f9bf2002-11-09 20:01:01 +00001101 delete GlobalsGraph;
1102 GlobalsGraph = 0;
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001103}
Brian Gaeked0fde302003-11-11 22:41:34 +00001104