blob: 9cb899cb374d014f347cc421ee9ced52c0b6abba [file] [log] [blame]
Chris Lattner193e6922002-10-01 22:33:50 +00001//===- Local.cpp - Compute a local data structure graph for a function ----===//
John Criswell482202a2003-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 Lattnerc0dab432002-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 Lattnerf6118db2004-07-07 06:32:21 +000015#include "llvm/Analysis/DataStructure/DataStructure.h"
16#include "llvm/Analysis/DataStructure/DSGraph.h"
Chris Lattnerc0dab432002-07-10 22:38:08 +000017#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
Chris Lattner04d9cb62003-09-20 16:34:13 +000019#include "llvm/Instructions.h"
Chris Lattner97612712004-02-13 16:09:54 +000020#include "llvm/Intrinsics.h"
Chris Lattner56744432003-11-25 20:19:55 +000021#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattnerc0dab432002-07-10 22:38:08 +000022#include "llvm/Support/InstVisitor.h"
Chris Lattner193e6922002-10-01 22:33:50 +000023#include "llvm/Target/TargetData.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000024#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/Debug.h"
26#include "llvm/Support/Timer.h"
Chris Lattner193e6922002-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 Lattner9e876552003-11-12 23:11:14 +000033using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000034
Chris Lattner96a0dfa2002-07-27 01:12:15 +000035static RegisterAnalysis<LocalDataStructures>
36X("datastructure", "Local Data Structure Analysis");
Chris Lattner96a0dfa2002-07-27 01:12:15 +000037
Chris Lattnerfb4c0572003-11-13 03:10:49 +000038static cl::opt<bool>
Chris Lattner7e369022004-08-02 20:16:21 +000039TrackIntegersAsPointers("dsa-track-integers", cl::Hidden,
Chris Lattnerfb4c0572003-11-13 03:10:49 +000040 cl::desc("If this is set, track integers as potential pointers"));
Chris Lattnerfb4c0572003-11-13 03:10:49 +000041
Chris Lattner9e876552003-11-12 23:11:14 +000042namespace llvm {
Chris Lattnerca03c3b2002-11-07 05:20:53 +000043namespace DS {
Chris Lattner193e6922002-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 Lattnerfb4c0572003-11-13 03:10:49 +000048 else if (TrackIntegersAsPointers && Ty->isPrimitiveType() &&Ty->isInteger())
Chris Lattner193e6922002-10-01 22:33:50 +000049 return Ty->getPrimitiveSize() >= PointerSize;
50 return false;
51 }
Chris Lattner9e876552003-11-12 23:11:14 +000052}}
Chris Lattner193e6922002-10-01 22:33:50 +000053
Brian Gaeke960707c2003-11-11 22:41:34 +000054using namespace DS;
Chris Lattnerc0dab432002-07-10 22:38:08 +000055
56namespace {
Chris Lattner80614ee2003-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 Lattnercb7d6422003-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 Lattner80614ee2003-02-05 21:59:58 +000064
Chris Lattner193e6922002-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 Lattnerc0dab432002-07-10 22:38:08 +000072 class GraphBuilder : InstVisitor<GraphBuilder> {
73 DSGraph &G;
Chris Lattner8c1a8352003-09-20 21:48:16 +000074 DSNodeHandle *RetNode; // Node that gets returned...
Chris Lattner9412b70a2004-01-28 09:15:42 +000075 DSScalarMap &ScalarMap;
Chris Lattnera1b39fa2005-01-30 23:51:02 +000076 std::list<DSCallSite> *FunctionCalls;
Chris Lattnerc0dab432002-07-10 22:38:08 +000077
78 public:
Chris Lattnerad7b9c12003-07-02 06:06:34 +000079 GraphBuilder(Function &f, DSGraph &g, DSNodeHandle &retNode,
Chris Lattnera1b39fa2005-01-30 23:51:02 +000080 std::list<DSCallSite> &fc)
Chris Lattner8c1a8352003-09-20 21:48:16 +000081 : G(g), RetNode(&retNode), ScalarMap(G.getScalarMap()),
82 FunctionCalls(&fc) {
Chris Lattner4c0d6202002-07-18 00:12:30 +000083
84 // Create scalar nodes for all pointer arguments...
Chris Lattner531f9e92005-03-15 04:54:21 +000085 for (Function::arg_iterator I = f.arg_begin(), E = f.arg_end(); I != E; ++I)
Chris Lattner193e6922002-10-01 22:33:50 +000086 if (isPointerType(I->getType()))
87 getValueDest(*I);
Chris Lattner4c0d6202002-07-18 00:12:30 +000088
Chris Lattner8c1a8352003-09-20 21:48:16 +000089 visit(f); // Single pass over the function
Chris Lattnerc0dab432002-07-10 22:38:08 +000090 }
91
Chris Lattner8c1a8352003-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 Lattnerc0dab432002-07-10 22:38:08 +000099 private:
100 // Visitor functions, used to handle each instruction type we encounter...
101 friend class InstVisitor<GraphBuilder>;
Chris Lattner4853d162003-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 Lattnerc0dab432002-07-10 22:38:08 +0000105
106 void visitPHINode(PHINode &PN);
Chris Lattnere99ee2b2005-02-25 01:27:48 +0000107 void visitSelectInst(SelectInst &SI);
Chris Lattnerc0dab432002-07-10 22:38:08 +0000108
Chris Lattner5e730ed2002-11-08 05:00:44 +0000109 void visitGetElementPtrInst(User &GEP);
Chris Lattnerc0dab432002-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 Lattner04d9cb62003-09-20 16:34:13 +0000114 void visitInvokeInst(InvokeInst &II);
Chris Lattner7c832fb2005-03-05 19:04:31 +0000115 void visitSetCondInst(SetCondInst &SCI);
Vikram S. Adve04aee942002-12-06 21:17:10 +0000116 void visitFreeInst(FreeInst &FI);
Chris Lattner193e6922002-10-01 22:33:50 +0000117 void visitCastInst(CastInst &CI);
Chris Lattner04fb4b52003-02-04 00:59:50 +0000118 void visitInstruction(Instruction &I);
Chris Lattnerc0dab432002-07-10 22:38:08 +0000119
Chris Lattner04d9cb62003-09-20 16:34:13 +0000120 void visitCallSite(CallSite CS);
Chris Lattner0ed81622004-03-04 20:33:47 +0000121 void visitVANextInst(VANextInst &I);
122 void visitVAArgInst(VAArgInst &I);
Chris Lattner8c1a8352003-09-20 21:48:16 +0000123
124 void MergeConstantInitIntoNode(DSNodeHandle &NH, Constant *C);
Chris Lattnerc0dab432002-07-10 22:38:08 +0000125 private:
126 // Helper functions used to implement the visitation functions...
127
Chris Lattner193e6922002-10-01 22:33:50 +0000128 /// createNode - Create a new DSNode, ensuring that it is properly added to
129 /// the graph.
130 ///
Chris Lattner4853d162003-06-19 21:15:11 +0000131 DSNode *createNode(const Type *Ty = 0) {
132 DSNode *N = new DSNode(Ty, &G); // Create the node
Chris Lattner39377262003-06-16 12:08:18 +0000133 if (DisableFieldSensitivity) {
Chris Lattnere657eb12004-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 Lattnercb7d6422003-02-08 20:18:39 +0000137 N->foldNodeCompletely();
Chris Lattner39377262003-06-16 12:08:18 +0000138 if (DSNode *FN = N->getForwardNode())
139 N = FN;
140 }
Chris Lattnera7b0d4e2002-11-02 00:13:20 +0000141 return N;
142 }
Chris Lattnerc0dab432002-07-10 22:38:08 +0000143
Chris Lattnera20fabc2002-11-03 21:27:48 +0000144 /// setDestTo - Set the ScalarMap entry for the specified value to point to
Chris Lattnera7b0d4e2002-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 Lattner193e6922002-10-01 22:33:50 +0000147 ///
Chris Lattnera7b0d4e2002-11-02 00:13:20 +0000148 void setDestTo(Value &V, const DSNodeHandle &NH);
Chris Lattnerc0dab432002-07-10 22:38:08 +0000149
Chris Lattnera7b0d4e2002-11-02 00:13:20 +0000150 /// getValueDest - Return the DSNode that the actual value points to.
Chris Lattner193e6922002-10-01 22:33:50 +0000151 ///
Chris Lattnera7b0d4e2002-11-02 00:13:20 +0000152 DSNodeHandle getValueDest(Value &V);
Chris Lattnerc0dab432002-07-10 22:38:08 +0000153
Chris Lattner193e6922002-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 Lattner5d428a02002-10-31 06:52:26 +0000156 /// null), then we create a new node, link it, then return it.
Chris Lattner193e6922002-10-01 22:33:50 +0000157 ///
Chris Lattner5d428a02002-10-31 06:52:26 +0000158 DSNodeHandle &getLink(const DSNodeHandle &Node, unsigned Link = 0);
Chris Lattnerc0dab432002-07-10 22:38:08 +0000159 };
160}
161
Brian Gaeke960707c2003-11-11 22:41:34 +0000162using namespace DS;
163
Chris Lattnerc0dab432002-07-10 22:38:08 +0000164//===----------------------------------------------------------------------===//
165// DSGraph constructor - Simply use the GraphBuilder to construct the local
166// graph.
Chris Lattner2b9926f2003-11-02 22:27:28 +0000167DSGraph::DSGraph(const TargetData &td, Function &F, DSGraph *GG)
168 : GlobalsGraph(GG), TD(td) {
Chris Lattner17da2872002-11-10 06:53:38 +0000169 PrintAuxCalls = false;
Chris Lattner3ff99e32003-07-02 04:37:26 +0000170
171 DEBUG(std::cerr << " [Loc] Calculating graph for: " << F.getName() << "\n");
172
Chris Lattner193e6922002-10-01 22:33:50 +0000173 // Use the graph builder to construct the local version of the graph
Chris Lattner8c1a8352003-09-20 21:48:16 +0000174 GraphBuilder B(F, *this, ReturnNodes[&F], FunctionCalls);
Chris Lattner63a9e5c2002-11-18 21:44:19 +0000175#ifndef NDEBUG
176 Timer::addPeakMemoryMeasurement();
177#endif
Chris Lattnerddd3b7f2003-02-14 04:55:58 +0000178
179 // Remove all integral constants from the scalarmap!
Chris Lattner9412b70a2004-01-28 09:15:42 +0000180 for (DSScalarMap::iterator I = ScalarMap.begin(); I != ScalarMap.end();)
Chris Lattner0e43b442004-01-27 21:51:19 +0000181 if (isa<ConstantIntegral>(I->first))
182 ScalarMap.erase(I++);
183 else
Chris Lattnerddd3b7f2003-02-14 04:55:58 +0000184 ++I;
185
Chris Lattnerfab28722004-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 Lattner55948732004-03-03 23:00:19 +0000194 if (!GV->isExternal() && GV->isConstant())
Chris Lattnerfab28722004-02-25 23:31:02 +0000195 RC.merge(ScalarMap[GV], GG->ScalarMap[GV]);
196 }
197
Chris Lattnera1d90112003-01-23 22:05:33 +0000198 markIncompleteNodes(DSGraph::MarkFormalArgs);
Chris Lattnere703c522002-11-09 20:55:24 +0000199
200 // Remove any nodes made dead due to merging...
Chris Lattnera1d90112003-01-23 22:05:33 +0000201 removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnerc0dab432002-07-10 22:38:08 +0000202}
203
204
205//===----------------------------------------------------------------------===//
206// Helper method implementations...
207//
208
Chris Lattnera7b0d4e2002-11-02 00:13:20 +0000209/// getValueDest - Return the DSNode that the actual value points to.
Chris Lattner193e6922002-10-01 22:33:50 +0000210///
Chris Lattner5e730ed2002-11-08 05:00:44 +0000211DSNodeHandle GraphBuilder::getValueDest(Value &Val) {
212 Value *V = &Val;
Chris Lattnera67a0302004-11-03 18:51:26 +0000213 if (isa<Constant>(V) && cast<Constant>(V)->isNullValue())
Chris Lattner5e730ed2002-11-08 05:00:44 +0000214 return 0; // Null doesn't point to anything, don't add to ScalarMap!
Chris Lattnera7b0d4e2002-11-02 00:13:20 +0000215
Vikram S. Adve04aee942002-12-06 21:17:10 +0000216 DSNodeHandle &NH = ScalarMap[V];
Chris Lattnerbc740092004-10-30 04:22:45 +0000217 if (!NH.isNull())
Vikram S. Adve04aee942002-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 Spencer30d69a52004-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 Lattner5e730ed2002-11-08 05:00:44 +0000230 if (CE->getOpcode() == Instruction::Cast)
Chris Lattner43df2882003-02-09 23:04:12 +0000231 NH = getValueDest(*CE->getOperand(0));
232 else if (CE->getOpcode() == Instruction::GetElementPtr) {
Chris Lattner5e730ed2002-11-08 05:00:44 +0000233 visitGetElementPtrInst(*CE);
Chris Lattner9412b70a2004-01-28 09:15:42 +0000234 DSScalarMap::iterator I = ScalarMap.find(CE);
Chris Lattner2dd1ada2002-11-09 20:14:03 +0000235 assert(I != ScalarMap.end() && "GEP didn't get processed right?");
Chris Lattner43df2882003-02-09 23:04:12 +0000236 NH = I->second;
237 } else {
238 // This returns a conservative unknown node for any unhandled ConstExpr
Chris Lattner4853d162003-06-19 21:15:11 +0000239 return NH = createNode()->setUnknownNodeMarker();
Chris Lattner5e730ed2002-11-08 05:00:44 +0000240 }
Chris Lattnerbc740092004-10-30 04:22:45 +0000241 if (NH.isNull()) { // (getelementptr null, X) returns null
Chris Lattner43df2882003-02-09 23:04:12 +0000242 ScalarMap.erase(V);
243 return 0;
244 }
245 return NH;
Chris Lattner5e730ed2002-11-08 05:00:44 +0000246
Chris Lattner5e730ed2002-11-08 05:00:44 +0000247 } else if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(C)) {
248 // Random constants are unknown mem
Chris Lattner4853d162003-06-19 21:15:11 +0000249 return NH = createNode()->setUnknownNodeMarker();
Chris Lattner61753bf2004-10-16 18:19:26 +0000250 } else if (isa<UndefValue>(C)) {
251 ScalarMap.erase(V);
252 return 0;
Chris Lattner5e730ed2002-11-08 05:00:44 +0000253 } else {
254 assert(0 && "Unknown constant type!");
255 }
Reid Spencer30d69a52004-07-18 00:18:30 +0000256 N = createNode(); // just create a shadow node
Chris Lattnera7b0d4e2002-11-02 00:13:20 +0000257 } else {
258 // Otherwise just create a shadow node
Chris Lattner4853d162003-06-19 21:15:11 +0000259 N = createNode();
Chris Lattnera7b0d4e2002-11-02 00:13:20 +0000260 }
261
Chris Lattnerdeb76762004-07-07 06:12:52 +0000262 NH.setTo(N, 0); // Remember that we are pointing to it...
Chris Lattnera7b0d4e2002-11-02 00:13:20 +0000263 return NH;
Chris Lattnerc0dab432002-07-10 22:38:08 +0000264}
265
Chris Lattner4c0d6202002-07-18 00:12:30 +0000266
Chris Lattner193e6922002-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 Lattner5d428a02002-10-31 06:52:26 +0000273DSNodeHandle &GraphBuilder::getLink(const DSNodeHandle &node, unsigned LinkNo) {
Chris Lattner193e6922002-10-01 22:33:50 +0000274 DSNodeHandle &Node = const_cast<DSNodeHandle&>(node);
Chris Lattner48e37d92002-11-06 06:20:27 +0000275 DSNodeHandle &Link = Node.getLink(LinkNo);
Chris Lattnerbc740092004-10-30 04:22:45 +0000276 if (Link.isNull()) {
Chris Lattner48e37d92002-11-06 06:20:27 +0000277 // If the link hasn't been created yet, make and return a new shadow node
Chris Lattner4853d162003-06-19 21:15:11 +0000278 Link = createNode();
Chris Lattner48e37d92002-11-06 06:20:27 +0000279 }
280 return Link;
Chris Lattnerc0dab432002-07-10 22:38:08 +0000281}
282
Chris Lattnerc0dab432002-07-10 22:38:08 +0000283
Chris Lattnera20fabc2002-11-03 21:27:48 +0000284/// setDestTo - Set the ScalarMap entry for the specified value to point to the
Chris Lattnera7b0d4e2002-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 Lattner0ed81622004-03-04 20:33:47 +0000289 ScalarMap[&V].mergeWith(NH);
Chris Lattnera7b0d4e2002-11-02 00:13:20 +0000290}
291
292
Chris Lattnerc0dab432002-07-10 22:38:08 +0000293//===----------------------------------------------------------------------===//
294// Specific instruction type handler implementations...
295//
296
Chris Lattner193e6922002-10-01 22:33:50 +0000297/// Alloca & Malloc instruction implementation - Simply create a new memory
298/// object, pointing the scalar to it.
299///
Chris Lattner4853d162003-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 Lattnerc0dab432002-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 Lattner193e6922002-10-01 22:33:50 +0000313 if (!isPointerType(PN.getType())) return; // Only pointer PHIs
Chris Lattnerc0dab432002-07-10 22:38:08 +0000314
Chris Lattnera20fabc2002-11-03 21:27:48 +0000315 DSNodeHandle &PNDest = ScalarMap[&PN];
Chris Lattnerc0dab432002-07-10 22:38:08 +0000316 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
Chris Lattnera7b0d4e2002-11-02 00:13:20 +0000317 PNDest.mergeWith(getValueDest(*PN.getIncomingValue(i)));
Chris Lattnerc0dab432002-07-10 22:38:08 +0000318}
319
Chris Lattnere99ee2b2005-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 Lattner7c832fb2005-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 Lattner5e730ed2002-11-08 05:00:44 +0000335void GraphBuilder::visitGetElementPtrInst(User &GEP) {
Chris Lattner193e6922002-10-01 22:33:50 +0000336 DSNodeHandle Value = getValueDest(*GEP.getOperand(0));
Chris Lattner468fd332005-02-24 19:55:31 +0000337 if (Value.isNull())
338 Value = createNode();
Chris Lattner193e6922002-10-01 22:33:50 +0000339
Chris Lattnerab6895e2003-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.
Chris Lattner5bd948b2005-03-18 23:18:20 +0000352 if (AllZeros || (!Value.isNull() &&
353 Value.getNode()->isNodeCompletelyFolded())) {
Chris Lattnerab6895e2003-11-14 17:09:46 +0000354 setDestTo(GEP, Value);
355 return;
356 }
357
358
Chris Lattner7d5f1982002-10-31 05:45:02 +0000359 const PointerType *PTy = cast<PointerType>(GEP.getOperand(0)->getType());
360 const Type *CurTy = PTy->getElementType();
Chris Lattner193e6922002-10-01 22:33:50 +0000361
Chris Lattner48e37d92002-11-06 06:20:27 +0000362 if (Value.getNode()->mergeTypeInfo(CurTy, Value.getOffset())) {
363 // If the node had to be folded... exit quickly
Chris Lattnera7b0d4e2002-11-02 00:13:20 +0000364 setDestTo(GEP, Value); // GEP result points to folded node
Chris Lattner7d5f1982002-10-31 05:45:02 +0000365 return;
366 }
367
Chris Lattner2b9926f2003-11-02 22:27:28 +0000368 const TargetData &TD = Value.getNode()->getTargetData();
369
Chris Lattner48e37d92002-11-06 06:20:27 +0000370#if 0
Chris Lattner7d5f1982002-10-31 05:45:02 +0000371 // Handle the pointer index specially...
372 if (GEP.getNumOperands() > 1 &&
Chris Lattner69193f92004-04-05 01:30:19 +0000373 (!isa<Constant>(GEP.getOperand(1)) ||
374 !cast<Constant>(GEP.getOperand(1))->isNullValue())) {
Chris Lattner7d5f1982002-10-31 05:45:02 +0000375
376 // If we already know this is an array being accessed, don't do anything...
377 if (!TopTypeRec.isArray) {
378 TopTypeRec.isArray = true;
379
380 // If we are treating some inner field pointer as an array, fold the node
381 // up because we cannot handle it right. This can come because of
382 // something like this: &((&Pt->X)[1]) == &Pt->Y
383 //
384 if (Value.getOffset()) {
385 // Value is now the pointer we want to GEP to be...
386 Value.getNode()->foldNodeCompletely();
Chris Lattnera7b0d4e2002-11-02 00:13:20 +0000387 setDestTo(GEP, Value); // GEP result points to folded node
Chris Lattner7d5f1982002-10-31 05:45:02 +0000388 return;
389 } else {
390 // This is a pointer to the first byte of the node. Make sure that we
391 // are pointing to the outter most type in the node.
392 // FIXME: We need to check one more case here...
393 }
394 }
395 }
Chris Lattner48e37d92002-11-06 06:20:27 +0000396#endif
Chris Lattner7d5f1982002-10-31 05:45:02 +0000397
398 // All of these subscripts are indexing INTO the elements we have...
Chris Lattner8c1a8352003-09-20 21:48:16 +0000399 unsigned Offset = 0;
Chris Lattner56744432003-11-25 20:19:55 +0000400 for (gep_type_iterator I = gep_type_begin(GEP), E = gep_type_end(GEP);
401 I != E; ++I)
402 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner2f8e4ad2005-01-12 04:51:37 +0000403 unsigned FieldNo =
404 (unsigned)cast<ConstantUInt>(I.getOperand())->getValue();
405 Offset += (unsigned)TD.getStructLayout(STy)->MemberOffsets[FieldNo];
Chris Lattnerc8d23b12004-03-01 19:02:54 +0000406 } else if (const PointerType *PTy = dyn_cast<PointerType>(*I)) {
407 if (!isa<Constant>(I.getOperand()) ||
408 !cast<Constant>(I.getOperand())->isNullValue())
409 Value.getNode()->setArrayMarker();
Chris Lattner56744432003-11-25 20:19:55 +0000410 }
411
412
Chris Lattner48e37d92002-11-06 06:20:27 +0000413#if 0
Chris Lattner56744432003-11-25 20:19:55 +0000414 if (const SequentialType *STy = cast<SequentialType>(*I)) {
415 CurTy = STy->getElementType();
Chris Lattnerc4d2ad22002-10-02 06:24:36 +0000416 if (ConstantSInt *CS = dyn_cast<ConstantSInt>(GEP.getOperand(i))) {
Chris Lattner7d5f1982002-10-31 05:45:02 +0000417 Offset += CS->getValue()*TD.getTypeSize(CurTy);
Chris Lattnerc4d2ad22002-10-02 06:24:36 +0000418 } else {
419 // Variable index into a node. We must merge all of the elements of the
420 // sequential type here.
421 if (isa<PointerType>(STy))
422 std::cerr << "Pointer indexing not handled yet!\n";
423 else {
424 const ArrayType *ATy = cast<ArrayType>(STy);
425 unsigned ElSize = TD.getTypeSize(CurTy);
426 DSNode *N = Value.getNode();
427 assert(N && "Value must have a node!");
428 unsigned RawOffset = Offset+Value.getOffset();
429
430 // Loop over all of the elements of the array, merging them into the
Misha Brukman32998322003-09-11 18:14:24 +0000431 // zeroth element.
Chris Lattnerc4d2ad22002-10-02 06:24:36 +0000432 for (unsigned i = 1, e = ATy->getNumElements(); i != e; ++i)
433 // Merge all of the byte components of this array element
434 for (unsigned j = 0; j != ElSize; ++j)
435 N->mergeIndexes(RawOffset+j, RawOffset+i*ElSize+j);
436 }
Chris Lattner193e6922002-10-01 22:33:50 +0000437 }
Chris Lattner193e6922002-10-01 22:33:50 +0000438 }
Chris Lattner56744432003-11-25 20:19:55 +0000439#endif
Chris Lattner193e6922002-10-01 22:33:50 +0000440
441 // Add in the offset calculated...
442 Value.setOffset(Value.getOffset()+Offset);
443
444 // Value is now the pointer we want to GEP to be...
Chris Lattnera7b0d4e2002-11-02 00:13:20 +0000445 setDestTo(GEP, Value);
Chris Lattnerc0dab432002-07-10 22:38:08 +0000446}
447
448void GraphBuilder::visitLoadInst(LoadInst &LI) {
Chris Lattnera7b0d4e2002-11-02 00:13:20 +0000449 DSNodeHandle Ptr = getValueDest(*LI.getOperand(0));
Chris Lattnere99ee2b2005-02-25 01:27:48 +0000450 if (Ptr.isNull())
451 Ptr = createNode();
Chris Lattnera7b0d4e2002-11-02 00:13:20 +0000452
453 // Make that the node is read from...
Chris Lattner4853d162003-06-19 21:15:11 +0000454 Ptr.getNode()->setReadMarker();
Chris Lattner7d5f1982002-10-31 05:45:02 +0000455
456 // Ensure a typerecord exists...
Chris Lattnerc12e5cc2003-03-03 17:13:31 +0000457 Ptr.getNode()->mergeTypeInfo(LI.getType(), Ptr.getOffset(), false);
Chris Lattnera7b0d4e2002-11-02 00:13:20 +0000458
Chris Lattner193e6922002-10-01 22:33:50 +0000459 if (isPointerType(LI.getType()))
Chris Lattnera7b0d4e2002-11-02 00:13:20 +0000460 setDestTo(LI, getLink(Ptr));
Chris Lattnerc0dab432002-07-10 22:38:08 +0000461}
462
463void GraphBuilder::visitStoreInst(StoreInst &SI) {
Chris Lattner7d5f1982002-10-31 05:45:02 +0000464 const Type *StoredTy = SI.getOperand(0)->getType();
Chris Lattnera7b0d4e2002-11-02 00:13:20 +0000465 DSNodeHandle Dest = getValueDest(*SI.getOperand(1));
Chris Lattner0ed81622004-03-04 20:33:47 +0000466 if (Dest.isNull()) return;
Chris Lattnera7b0d4e2002-11-02 00:13:20 +0000467
Vikram S. Adve04aee942002-12-06 21:17:10 +0000468 // Mark that the node is written to...
Chris Lattner4853d162003-06-19 21:15:11 +0000469 Dest.getNode()->setModifiedMarker();
Chris Lattner7d5f1982002-10-31 05:45:02 +0000470
Chris Lattner8c1a8352003-09-20 21:48:16 +0000471 // Ensure a type-record exists...
Chris Lattner48e37d92002-11-06 06:20:27 +0000472 Dest.getNode()->mergeTypeInfo(StoredTy, Dest.getOffset());
Chris Lattner193e6922002-10-01 22:33:50 +0000473
474 // Avoid adding edges from null, or processing non-"pointer" stores
Chris Lattnera7b0d4e2002-11-02 00:13:20 +0000475 if (isPointerType(StoredTy))
Chris Lattner193e6922002-10-01 22:33:50 +0000476 Dest.addEdgeTo(getValueDest(*SI.getOperand(0)));
Chris Lattnerc0dab432002-07-10 22:38:08 +0000477}
478
479void GraphBuilder::visitReturnInst(ReturnInst &RI) {
Chris Lattnera7b0d4e2002-11-02 00:13:20 +0000480 if (RI.getNumOperands() && isPointerType(RI.getOperand(0)->getType()))
Chris Lattner8c1a8352003-09-20 21:48:16 +0000481 RetNode->mergeWith(getValueDest(*RI.getOperand(0)));
Chris Lattnerc0dab432002-07-10 22:38:08 +0000482}
483
Chris Lattner0ed81622004-03-04 20:33:47 +0000484void GraphBuilder::visitVANextInst(VANextInst &I) {
485 getValueDest(*I.getOperand(0)).mergeWith(getValueDest(I));
486}
487
488void GraphBuilder::visitVAArgInst(VAArgInst &I) {
489 DSNodeHandle Ptr = getValueDest(*I.getOperand(0));
490 if (Ptr.isNull()) return;
491
492 // Make that the node is read from.
493 Ptr.getNode()->setReadMarker();
494
Chris Lattnerbc740092004-10-30 04:22:45 +0000495 // Ensure a type record exists.
496 DSNode *PtrN = Ptr.getNode();
497 PtrN->mergeTypeInfo(I.getType(), Ptr.getOffset(), false);
Chris Lattner0ed81622004-03-04 20:33:47 +0000498
499 if (isPointerType(I.getType()))
500 setDestTo(I, getLink(Ptr));
501}
502
503
Chris Lattnerc0dab432002-07-10 22:38:08 +0000504void GraphBuilder::visitCallInst(CallInst &CI) {
Chris Lattner04d9cb62003-09-20 16:34:13 +0000505 visitCallSite(&CI);
506}
507
508void GraphBuilder::visitInvokeInst(InvokeInst &II) {
509 visitCallSite(&II);
510}
511
512void GraphBuilder::visitCallSite(CallSite CS) {
Chris Lattner5ef16382004-02-26 22:07:22 +0000513 Value *Callee = CS.getCalledValue();
Chris Lattner5ef16382004-02-26 22:07:22 +0000514
Chris Lattner689b6812003-09-20 16:50:46 +0000515 // Special case handling of certain libc allocation functions here.
Chris Lattner5ef16382004-02-26 22:07:22 +0000516 if (Function *F = dyn_cast<Function>(Callee))
Chris Lattner689b6812003-09-20 16:50:46 +0000517 if (F->isExternal())
Chris Lattner97612712004-02-13 16:09:54 +0000518 switch (F->getIntrinsicID()) {
Chris Lattner071a5e52004-03-13 00:24:00 +0000519 case Intrinsic::vastart:
Chris Lattner0ed81622004-03-04 20:33:47 +0000520 getValueDest(*CS.getInstruction()).getNode()->setAllocaNodeMarker();
521 return;
Chris Lattner071a5e52004-03-13 00:24:00 +0000522 case Intrinsic::vacopy:
Chris Lattner0ed81622004-03-04 20:33:47 +0000523 getValueDest(*CS.getInstruction()).
524 mergeWith(getValueDest(**(CS.arg_begin())));
525 return;
Chris Lattner071a5e52004-03-13 00:24:00 +0000526 case Intrinsic::vaend:
Chris Lattner0ed81622004-03-04 20:33:47 +0000527 return; // noop
Chris Lattner97612712004-02-13 16:09:54 +0000528 case Intrinsic::memmove:
529 case Intrinsic::memcpy: {
530 // Merge the first & second arguments, and mark the memory read and
Chris Lattner12dd38a2003-11-08 21:55:50 +0000531 // modified.
Chris Lattner97612712004-02-13 16:09:54 +0000532 DSNodeHandle RetNH = getValueDest(**CS.arg_begin());
Chris Lattnercff83a22003-11-09 03:32:52 +0000533 RetNH.mergeWith(getValueDest(**(CS.arg_begin()+1)));
534 if (DSNode *N = RetNH.getNode())
535 N->setModifiedMarker()->setReadMarker();
536 return;
Chris Lattner97612712004-02-13 16:09:54 +0000537 }
Chris Lattnercedfcf52004-02-16 18:37:40 +0000538 case Intrinsic::memset:
539 // Mark the memory modified.
540 if (DSNode *N = getValueDest(**CS.arg_begin()).getNode())
541 N->setModifiedMarker();
542 return;
Chris Lattner97612712004-02-13 16:09:54 +0000543 default:
Vikram S. Advef6c4ee02004-05-25 08:14:52 +0000544 if (F->getName() == "calloc" || F->getName() == "posix_memalign" ||
545 F->getName() == "memalign" || F->getName() == "valloc") {
Chris Lattner97612712004-02-13 16:09:54 +0000546 setDestTo(*CS.getInstruction(),
547 createNode()->setHeapNodeMarker()->setModifiedMarker());
548 return;
549 } else if (F->getName() == "realloc") {
550 DSNodeHandle RetNH = getValueDest(*CS.getInstruction());
Chris Lattnera67a0302004-11-03 18:51:26 +0000551 if (CS.arg_begin() != CS.arg_end())
552 RetNH.mergeWith(getValueDest(**CS.arg_begin()));
Chris Lattner97612712004-02-13 16:09:54 +0000553 if (DSNode *N = RetNH.getNode())
554 N->setHeapNodeMarker()->setModifiedMarker()->setReadMarker();
555 return;
Chris Lattnerc8167b02004-02-26 03:43:08 +0000556 } else if (F->getName() == "memmove") {
557 // Merge the first & second arguments, and mark the memory read and
558 // modified.
559 DSNodeHandle RetNH = getValueDest(**CS.arg_begin());
560 RetNH.mergeWith(getValueDest(**(CS.arg_begin()+1)));
561 if (DSNode *N = RetNH.getNode())
562 N->setModifiedMarker()->setReadMarker();
563 return;
564
Chris Lattner396cdaf2004-02-24 22:02:48 +0000565 } else if (F->getName() == "atoi" || F->getName() == "atof" ||
Chris Lattner864c9012004-02-25 17:43:20 +0000566 F->getName() == "atol" || F->getName() == "atoll" ||
Chris Lattner9ccb1af2004-02-24 22:17:00 +0000567 F->getName() == "remove" || F->getName() == "unlink" ||
Chris Lattner864c9012004-02-25 17:43:20 +0000568 F->getName() == "rename" || F->getName() == "memcmp" ||
569 F->getName() == "strcmp" || F->getName() == "strncmp" ||
570 F->getName() == "execl" || F->getName() == "execlp" ||
571 F->getName() == "execle" || F->getName() == "execv" ||
Chris Lattner5e5e0602004-02-25 23:06:40 +0000572 F->getName() == "execvp" || F->getName() == "chmod" ||
573 F->getName() == "puts" || F->getName() == "write" ||
574 F->getName() == "open" || F->getName() == "create" ||
575 F->getName() == "truncate" || F->getName() == "chdir" ||
576 F->getName() == "mkdir" || F->getName() == "rmdir") {
Chris Lattner9ccb1af2004-02-24 22:17:00 +0000577 // These functions read all of their pointer operands.
578 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
579 AI != E; ++AI) {
580 if (isPointerType((*AI)->getType()))
581 if (DSNode *N = getValueDest(**AI).getNode())
582 N->setReadMarker();
583 }
Chris Lattner0cb88552004-02-16 22:57:19 +0000584 return;
Chris Lattner5e5e0602004-02-25 23:06:40 +0000585 } else if (F->getName() == "read" || F->getName() == "pipe" ||
Chris Lattner98f8ca42004-02-27 20:04:48 +0000586 F->getName() == "wait" || F->getName() == "time") {
Chris Lattner5e5e0602004-02-25 23:06:40 +0000587 // These functions write all of their pointer operands.
588 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
589 AI != E; ++AI) {
590 if (isPointerType((*AI)->getType()))
591 if (DSNode *N = getValueDest(**AI).getNode())
592 N->setModifiedMarker();
593 }
594 return;
Chris Lattner864c9012004-02-25 17:43:20 +0000595 } else if (F->getName() == "stat" || F->getName() == "fstat" ||
596 F->getName() == "lstat") {
597 // These functions read their first operand if its a pointer.
598 CallSite::arg_iterator AI = CS.arg_begin();
599 if (isPointerType((*AI)->getType())) {
600 DSNodeHandle Path = getValueDest(**AI);
601 if (DSNode *N = Path.getNode()) N->setReadMarker();
602 }
Chris Lattner0cb88552004-02-16 22:57:19 +0000603
Chris Lattner864c9012004-02-25 17:43:20 +0000604 // Then they write into the stat buffer.
605 DSNodeHandle StatBuf = getValueDest(**++AI);
606 if (DSNode *N = StatBuf.getNode()) {
607 N->setModifiedMarker();
608 const Type *StatTy = F->getFunctionType()->getParamType(1);
609 if (const PointerType *PTy = dyn_cast<PointerType>(StatTy))
610 N->mergeTypeInfo(PTy->getElementType(), StatBuf.getOffset());
611 }
Chris Lattner864c9012004-02-25 17:43:20 +0000612 return;
Chris Lattnerbc3381f2004-03-04 21:03:54 +0000613 } else if (F->getName() == "strtod" || F->getName() == "strtof" ||
614 F->getName() == "strtold") {
615 // These functions read the first pointer
616 if (DSNode *Str = getValueDest(**CS.arg_begin()).getNode()) {
617 Str->setReadMarker();
618 // If the second parameter is passed, it will point to the first
619 // argument node.
620 const DSNodeHandle &EndPtrNH = getValueDest(**(CS.arg_begin()+1));
621 if (DSNode *End = EndPtrNH.getNode()) {
622 End->mergeTypeInfo(PointerType::get(Type::SByteTy),
623 EndPtrNH.getOffset(), false);
624 End->setModifiedMarker();
625 DSNodeHandle &Link = getLink(EndPtrNH);
626 Link.mergeWith(getValueDest(**CS.arg_begin()));
627 }
628 }
629
630 return;
Chris Lattner98f8ca42004-02-27 20:04:48 +0000631 } else if (F->getName() == "fopen" || F->getName() == "fdopen" ||
632 F->getName() == "freopen") {
633 // These functions read all of their pointer operands.
634 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
635 AI != E; ++AI)
636 if (isPointerType((*AI)->getType()))
637 if (DSNode *N = getValueDest(**AI).getNode())
638 N->setReadMarker();
Chris Lattnere0022722004-02-13 20:05:32 +0000639
640 // fopen allocates in an unknown way and writes to the file
641 // descriptor. Also, merge the allocated type into the node.
642 DSNodeHandle Result = getValueDest(*CS.getInstruction());
Chris Lattner396cdaf2004-02-24 22:02:48 +0000643 if (DSNode *N = Result.getNode()) {
644 N->setModifiedMarker()->setUnknownNodeMarker();
645 const Type *RetTy = F->getFunctionType()->getReturnType();
646 if (const PointerType *PTy = dyn_cast<PointerType>(RetTy))
647 N->mergeTypeInfo(PTy->getElementType(), Result.getOffset());
648 }
Chris Lattner98f8ca42004-02-27 20:04:48 +0000649
650 // If this is freopen, merge the file descriptor passed in with the
651 // result.
Chris Lattnerd349d4a2004-12-08 16:22:26 +0000652 if (F->getName() == "freopen") {
653 // ICC doesn't handle getting the iterator, decrementing and
654 // dereferencing it in one operation without error. Do it in 2 steps
655 CallSite::arg_iterator compit = CS.arg_end();
656 Result.mergeWith(getValueDest(**--compit));
657 }
Chris Lattner396cdaf2004-02-24 22:02:48 +0000658 return;
Chris Lattnere0022722004-02-13 20:05:32 +0000659 } else if (F->getName() == "fclose" && CS.arg_end()-CS.arg_begin() ==1){
660 // fclose reads and deallocates the memory in an unknown way for the
661 // file descriptor. It merges the FILE type into the descriptor.
662 DSNodeHandle H = getValueDest(**CS.arg_begin());
Chris Lattner396cdaf2004-02-24 22:02:48 +0000663 if (DSNode *N = H.getNode()) {
664 N->setReadMarker()->setUnknownNodeMarker();
665 const Type *ArgTy = F->getFunctionType()->getParamType(0);
666 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
667 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
668 }
Chris Lattnere0022722004-02-13 20:05:32 +0000669 return;
Chris Lattner0cb88552004-02-16 22:57:19 +0000670 } else if (CS.arg_end()-CS.arg_begin() == 1 &&
671 (F->getName() == "fflush" || F->getName() == "feof" ||
672 F->getName() == "fileno" || F->getName() == "clearerr" ||
Chris Lattner5e5e0602004-02-25 23:06:40 +0000673 F->getName() == "rewind" || F->getName() == "ftell" ||
Chris Lattner98f8ca42004-02-27 20:04:48 +0000674 F->getName() == "ferror" || F->getName() == "fgetc" ||
675 F->getName() == "fgetc" || F->getName() == "_IO_getc")) {
Chris Lattner0cb88552004-02-16 22:57:19 +0000676 // fflush reads and writes the memory for the file descriptor. It
Chris Lattnerd17e15e2004-02-13 21:21:48 +0000677 // merges the FILE type into the descriptor.
678 DSNodeHandle H = getValueDest(**CS.arg_begin());
Chris Lattner396cdaf2004-02-24 22:02:48 +0000679 if (DSNode *N = H.getNode()) {
680 N->setReadMarker()->setModifiedMarker();
Chris Lattnerd17e15e2004-02-13 21:21:48 +0000681
Chris Lattner396cdaf2004-02-24 22:02:48 +0000682 const Type *ArgTy = F->getFunctionType()->getParamType(0);
683 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
684 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
685 }
686 return;
687 } else if (CS.arg_end()-CS.arg_begin() == 4 &&
688 (F->getName() == "fwrite" || F->getName() == "fread")) {
689 // fread writes the first operand, fwrite reads it. They both
690 // read/write the FILE descriptor, and merges the FILE type.
Chris Lattnerd349d4a2004-12-08 16:22:26 +0000691 CallSite::arg_iterator compit = CS.arg_end();
692 DSNodeHandle H = getValueDest(**--compit);
Chris Lattner396cdaf2004-02-24 22:02:48 +0000693 if (DSNode *N = H.getNode()) {
694 N->setReadMarker()->setModifiedMarker();
695 const Type *ArgTy = F->getFunctionType()->getParamType(3);
696 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
697 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
698 }
699
700 H = getValueDest(**CS.arg_begin());
701 if (DSNode *N = H.getNode())
702 if (F->getName() == "fwrite")
703 N->setReadMarker();
704 else
705 N->setModifiedMarker();
Chris Lattnerd17e15e2004-02-13 21:21:48 +0000706 return;
707 } else if (F->getName() == "fgets" && CS.arg_end()-CS.arg_begin() == 3){
Chris Lattner0cb88552004-02-16 22:57:19 +0000708 // fgets reads and writes the memory for the file descriptor. It
Chris Lattnerd17e15e2004-02-13 21:21:48 +0000709 // merges the FILE type into the descriptor, and writes to the
710 // argument. It returns the argument as well.
711 CallSite::arg_iterator AI = CS.arg_begin();
712 DSNodeHandle H = getValueDest(**AI);
713 if (DSNode *N = H.getNode())
714 N->setModifiedMarker(); // Writes buffer
715 H.mergeWith(getValueDest(*CS.getInstruction())); // Returns buffer
716 ++AI; ++AI;
717
718 // Reads and writes file descriptor, merge in FILE type.
Chris Lattner864c9012004-02-25 17:43:20 +0000719 H = getValueDest(**AI);
Chris Lattner396cdaf2004-02-24 22:02:48 +0000720 if (DSNode *N = H.getNode()) {
Chris Lattnerd17e15e2004-02-13 21:21:48 +0000721 N->setReadMarker()->setModifiedMarker();
Chris Lattner864c9012004-02-25 17:43:20 +0000722 const Type *ArgTy = F->getFunctionType()->getParamType(2);
723 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
724 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
725 }
726 return;
Chris Lattner5e5e0602004-02-25 23:06:40 +0000727 } else if (F->getName() == "ungetc" || F->getName() == "fputc" ||
728 F->getName() == "fputs" || F->getName() == "putc" ||
Chris Lattner98f8ca42004-02-27 20:04:48 +0000729 F->getName() == "ftell" || F->getName() == "rewind" ||
730 F->getName() == "_IO_putc") {
731 // These functions read and write the memory for the file descriptor,
732 // which is passes as the last argument.
Chris Lattnerd349d4a2004-12-08 16:22:26 +0000733 CallSite::arg_iterator compit = CS.arg_end();
734 DSNodeHandle H = getValueDest(**--compit);
Chris Lattner864c9012004-02-25 17:43:20 +0000735 if (DSNode *N = H.getNode()) {
736 N->setReadMarker()->setModifiedMarker();
Chris Lattnerd349d4a2004-12-08 16:22:26 +0000737 FunctionType::param_iterator compit2 = F->getFunctionType()->param_end();
738 const Type *ArgTy = *--compit2;
Chris Lattner396cdaf2004-02-24 22:02:48 +0000739 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
740 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
741 }
Chris Lattner5e5e0602004-02-25 23:06:40 +0000742
743 // Any pointer arguments are read.
744 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
745 AI != E; ++AI)
746 if (isPointerType((*AI)->getType()))
747 if (DSNode *N = getValueDest(**AI).getNode())
748 N->setReadMarker();
749 return;
750 } else if (F->getName() == "fseek" || F->getName() == "fgetpos" ||
751 F->getName() == "fsetpos") {
752 // These functions read and write the memory for the file descriptor,
753 // and read/write all other arguments.
754 DSNodeHandle H = getValueDest(**CS.arg_begin());
755 if (DSNode *N = H.getNode()) {
Chris Lattnerd349d4a2004-12-08 16:22:26 +0000756 FunctionType::param_iterator compit2 = F->getFunctionType()->param_end();
757 const Type *ArgTy = *--compit2;
Chris Lattner5e5e0602004-02-25 23:06:40 +0000758 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
759 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
760 }
761
762 // Any pointer arguments are read.
763 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
764 AI != E; ++AI)
765 if (isPointerType((*AI)->getType()))
766 if (DSNode *N = getValueDest(**AI).getNode())
767 N->setReadMarker()->setModifiedMarker();
Chris Lattnerd17e15e2004-02-13 21:21:48 +0000768 return;
Chris Lattnera061c3b2004-02-20 20:27:11 +0000769 } else if (F->getName() == "printf" || F->getName() == "fprintf" ||
770 F->getName() == "sprintf") {
Chris Lattnerd17e15e2004-02-13 21:21:48 +0000771 CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
772
773 if (F->getName() == "fprintf") {
774 // fprintf reads and writes the FILE argument, and applies the type
775 // to it.
776 DSNodeHandle H = getValueDest(**AI);
777 if (DSNode *N = H.getNode()) {
778 N->setModifiedMarker();
779 const Type *ArgTy = (*AI)->getType();
780 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
781 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
782 }
Chris Lattnera061c3b2004-02-20 20:27:11 +0000783 } else if (F->getName() == "sprintf") {
784 // sprintf writes the first string argument.
785 DSNodeHandle H = getValueDest(**AI++);
786 if (DSNode *N = H.getNode()) {
787 N->setModifiedMarker();
788 const Type *ArgTy = (*AI)->getType();
789 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
790 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
791 }
Chris Lattnerd17e15e2004-02-13 21:21:48 +0000792 }
793
794 for (; AI != E; ++AI) {
795 // printf reads all pointer arguments.
796 if (isPointerType((*AI)->getType()))
797 if (DSNode *N = getValueDest(**AI).getNode())
798 N->setReadMarker();
799 }
Chris Lattner7b0368ee2004-02-20 23:27:09 +0000800 return;
Chris Lattnerbc3381f2004-03-04 21:03:54 +0000801 } else if (F->getName() == "vprintf" || F->getName() == "vfprintf" ||
802 F->getName() == "vsprintf") {
803 CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
804
805 if (F->getName() == "vfprintf") {
806 // ffprintf reads and writes the FILE argument, and applies the type
807 // to it.
808 DSNodeHandle H = getValueDest(**AI);
809 if (DSNode *N = H.getNode()) {
810 N->setModifiedMarker()->setReadMarker();
811 const Type *ArgTy = (*AI)->getType();
812 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
813 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
814 }
815 ++AI;
816 } else if (F->getName() == "vsprintf") {
817 // vsprintf writes the first string argument.
818 DSNodeHandle H = getValueDest(**AI++);
819 if (DSNode *N = H.getNode()) {
820 N->setModifiedMarker();
821 const Type *ArgTy = (*AI)->getType();
822 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
823 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
824 }
825 }
826
827 // Read the format
828 if (AI != E) {
829 if (isPointerType((*AI)->getType()))
830 if (DSNode *N = getValueDest(**AI).getNode())
831 N->setReadMarker();
832 ++AI;
833 }
834
835 // Read the valist, and the pointed-to objects.
836 if (AI != E && isPointerType((*AI)->getType())) {
837 const DSNodeHandle &VAList = getValueDest(**AI);
838 if (DSNode *N = VAList.getNode()) {
839 N->setReadMarker();
840 N->mergeTypeInfo(PointerType::get(Type::SByteTy),
841 VAList.getOffset(), false);
842
843 DSNodeHandle &VAListObjs = getLink(VAList);
844 VAListObjs.getNode()->setReadMarker();
845 }
846 }
847
848 return;
Chris Lattnera061c3b2004-02-20 20:27:11 +0000849 } else if (F->getName() == "scanf" || F->getName() == "fscanf" ||
850 F->getName() == "sscanf") {
851 CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
Chris Lattnerd17e15e2004-02-13 21:21:48 +0000852
Chris Lattnera061c3b2004-02-20 20:27:11 +0000853 if (F->getName() == "fscanf") {
854 // fscanf reads and writes the FILE argument, and applies the type
855 // to it.
856 DSNodeHandle H = getValueDest(**AI);
857 if (DSNode *N = H.getNode()) {
858 N->setReadMarker();
859 const Type *ArgTy = (*AI)->getType();
860 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
861 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
862 }
863 } else if (F->getName() == "sscanf") {
864 // sscanf reads the first string argument.
865 DSNodeHandle H = getValueDest(**AI++);
866 if (DSNode *N = H.getNode()) {
867 N->setReadMarker();
868 const Type *ArgTy = (*AI)->getType();
869 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
870 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
871 }
872 }
873
874 for (; AI != E; ++AI) {
875 // scanf writes all pointer arguments.
876 if (isPointerType((*AI)->getType()))
877 if (DSNode *N = getValueDest(**AI).getNode())
878 N->setModifiedMarker();
879 }
Chris Lattner7b0368ee2004-02-20 23:27:09 +0000880 return;
Chris Lattnera061c3b2004-02-20 20:27:11 +0000881 } else if (F->getName() == "strtok") {
882 // strtok reads and writes the first argument, returning it. It reads
883 // its second arg. FIXME: strtok also modifies some hidden static
884 // data. Someday this might matter.
885 CallSite::arg_iterator AI = CS.arg_begin();
886 DSNodeHandle H = getValueDest(**AI++);
887 if (DSNode *N = H.getNode()) {
888 N->setReadMarker()->setModifiedMarker(); // Reads/Writes buffer
889 const Type *ArgTy = F->getFunctionType()->getParamType(0);
890 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
891 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
892 }
893 H.mergeWith(getValueDest(*CS.getInstruction())); // Returns buffer
894
895 H = getValueDest(**AI); // Reads delimiter
896 if (DSNode *N = H.getNode()) {
897 N->setReadMarker();
898 const Type *ArgTy = F->getFunctionType()->getParamType(1);
899 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
900 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
901 }
902 return;
Chris Lattnerc8167b02004-02-26 03:43:08 +0000903 } else if (F->getName() == "strchr" || F->getName() == "strrchr" ||
904 F->getName() == "strstr") {
905 // These read their arguments, and return the first one
Chris Lattner864c9012004-02-25 17:43:20 +0000906 DSNodeHandle H = getValueDest(**CS.arg_begin());
Chris Lattnerc8167b02004-02-26 03:43:08 +0000907 H.mergeWith(getValueDest(*CS.getInstruction())); // Returns buffer
908
909 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
910 AI != E; ++AI)
911 if (isPointerType((*AI)->getType()))
912 if (DSNode *N = getValueDest(**AI).getNode())
913 N->setReadMarker();
914
Chris Lattner864c9012004-02-25 17:43:20 +0000915 if (DSNode *N = H.getNode())
916 N->setReadMarker();
Chris Lattner864c9012004-02-25 17:43:20 +0000917 return;
Chris Lattner1feea5f2004-11-08 21:08:28 +0000918 } else if (F->getName() == "__assert_fail") {
919 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
920 AI != E; ++AI)
921 if (isPointerType((*AI)->getType()))
922 if (DSNode *N = getValueDest(**AI).getNode())
923 N->setReadMarker();
924 return;
Chris Lattner5e5e0602004-02-25 23:06:40 +0000925 } else if (F->getName() == "modf" && CS.arg_end()-CS.arg_begin() == 2) {
926 // This writes its second argument, and forces it to double.
Chris Lattnerd349d4a2004-12-08 16:22:26 +0000927 CallSite::arg_iterator compit = CS.arg_end();
928 DSNodeHandle H = getValueDest(**--compit);
Chris Lattner5e5e0602004-02-25 23:06:40 +0000929 if (DSNode *N = H.getNode()) {
930 N->setModifiedMarker();
931 N->mergeTypeInfo(Type::DoubleTy, H.getOffset());
932 }
933 return;
Chris Lattnerd17e15e2004-02-13 21:21:48 +0000934 } else {
Chris Lattner0cb88552004-02-16 22:57:19 +0000935 // Unknown function, warn if it returns a pointer type or takes a
936 // pointer argument.
937 bool Warn = isPointerType(CS.getInstruction()->getType());
938 if (!Warn)
939 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
940 I != E; ++I)
941 if (isPointerType((*I)->getType())) {
942 Warn = true;
943 break;
944 }
945 if (Warn)
946 std::cerr << "WARNING: Call to unknown external function '"
947 << F->getName() << "' will cause pessimistic results!\n";
Chris Lattner97612712004-02-13 16:09:54 +0000948 }
Chris Lattner689b6812003-09-20 16:50:46 +0000949 }
950
951
Chris Lattner7d0dafc2002-07-11 20:32:02 +0000952 // Set up the return value...
Chris Lattner5c3ce312002-10-21 02:08:03 +0000953 DSNodeHandle RetVal;
Chris Lattner04d9cb62003-09-20 16:34:13 +0000954 Instruction *I = CS.getInstruction();
955 if (isPointerType(I->getType()))
956 RetVal = getValueDest(*I);
Chris Lattner7d0dafc2002-07-11 20:32:02 +0000957
Chris Lattner5ef16382004-02-26 22:07:22 +0000958 DSNode *CalleeNode = 0;
959 if (DisableDirectCallOpt || !isa<Function>(Callee)) {
960 CalleeNode = getValueDest(*Callee).getNode();
961 if (CalleeNode == 0) {
962 std::cerr << "WARNING: Program is calling through a null pointer?\n"<< *I;
Chris Lattner66a47332003-09-24 23:42:58 +0000963 return; // Calling a null pointer?
964 }
965 }
Chris Lattner4c0d6202002-07-18 00:12:30 +0000966
Chris Lattner5c3ce312002-10-21 02:08:03 +0000967 std::vector<DSNodeHandle> Args;
Chris Lattner04d9cb62003-09-20 16:34:13 +0000968 Args.reserve(CS.arg_end()-CS.arg_begin());
Chris Lattner5c3ce312002-10-21 02:08:03 +0000969
970 // Calculate the arguments vector...
Chris Lattner04d9cb62003-09-20 16:34:13 +0000971 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E; ++I)
972 if (isPointerType((*I)->getType()))
973 Args.push_back(getValueDest(**I));
Chris Lattner5c3ce312002-10-21 02:08:03 +0000974
975 // Add a new function call entry...
Chris Lattner5ef16382004-02-26 22:07:22 +0000976 if (CalleeNode)
977 FunctionCalls->push_back(DSCallSite(CS, RetVal, CalleeNode, Args));
Chris Lattner80614ee2003-02-05 21:59:58 +0000978 else
Chris Lattner5ef16382004-02-26 22:07:22 +0000979 FunctionCalls->push_back(DSCallSite(CS, RetVal, cast<Function>(Callee),
Chris Lattner8c1a8352003-09-20 21:48:16 +0000980 Args));
Chris Lattnerc0dab432002-07-10 22:38:08 +0000981}
Chris Lattnerdb7a6802002-07-18 15:54:42 +0000982
Vikram S. Adve04aee942002-12-06 21:17:10 +0000983void GraphBuilder::visitFreeInst(FreeInst &FI) {
Vikram S. Adve04aee942002-12-06 21:17:10 +0000984 // Mark that the node is written to...
Chris Lattner396cdaf2004-02-24 22:02:48 +0000985 if (DSNode *N = getValueDest(*FI.getOperand(0)).getNode())
986 N->setModifiedMarker()->setHeapNodeMarker();
Vikram S. Adve04aee942002-12-06 21:17:10 +0000987}
988
Chris Lattner193e6922002-10-01 22:33:50 +0000989/// Handle casts...
990void GraphBuilder::visitCastInst(CastInst &CI) {
Chris Lattner63ba1ac2002-11-02 00:36:03 +0000991 if (isPointerType(CI.getType()))
992 if (isPointerType(CI.getOperand(0)->getType())) {
Chris Lattneraf88fcd2004-10-06 19:29:13 +0000993 DSNodeHandle Ptr = getValueDest(*CI.getOperand(0));
994 if (Ptr.getNode() == 0) return;
995
Chris Lattner63ba1ac2002-11-02 00:36:03 +0000996 // Cast one pointer to the other, just act like a copy instruction
Chris Lattneraf88fcd2004-10-06 19:29:13 +0000997 setDestTo(CI, Ptr);
Chris Lattner63ba1ac2002-11-02 00:36:03 +0000998 } else {
999 // Cast something (floating point, small integer) to a pointer. We need
1000 // to track the fact that the node points to SOMETHING, just something we
1001 // don't know about. Make an "Unknown" node.
1002 //
Chris Lattner4853d162003-06-19 21:15:11 +00001003 setDestTo(CI, createNode()->setUnknownNodeMarker());
Chris Lattner63ba1ac2002-11-02 00:36:03 +00001004 }
Chris Lattner193e6922002-10-01 22:33:50 +00001005}
Chris Lattnerdb7a6802002-07-18 15:54:42 +00001006
Chris Lattner193e6922002-10-01 22:33:50 +00001007
Chris Lattner04fb4b52003-02-04 00:59:50 +00001008// visitInstruction - For all other instruction types, if we have any arguments
1009// that are of pointer type, make them have unknown composition bits, and merge
1010// the nodes together.
1011void GraphBuilder::visitInstruction(Instruction &Inst) {
1012 DSNodeHandle CurNode;
1013 if (isPointerType(Inst.getType()))
1014 CurNode = getValueDest(Inst);
1015 for (User::op_iterator I = Inst.op_begin(), E = Inst.op_end(); I != E; ++I)
1016 if (isPointerType((*I)->getType()))
1017 CurNode.mergeWith(getValueDest(**I));
1018
Chris Lattnerbc740092004-10-30 04:22:45 +00001019 if (DSNode *N = CurNode.getNode())
1020 N->setUnknownNodeMarker();
Chris Lattner04fb4b52003-02-04 00:59:50 +00001021}
1022
Chris Lattner193e6922002-10-01 22:33:50 +00001023
1024
1025//===----------------------------------------------------------------------===//
1026// LocalDataStructures Implementation
1027//===----------------------------------------------------------------------===//
1028
Chris Lattner8c1a8352003-09-20 21:48:16 +00001029// MergeConstantInitIntoNode - Merge the specified constant into the node
1030// pointed to by NH.
1031void GraphBuilder::MergeConstantInitIntoNode(DSNodeHandle &NH, Constant *C) {
1032 // Ensure a type-record exists...
Chris Lattnerbc740092004-10-30 04:22:45 +00001033 DSNode *NHN = NH.getNode();
1034 NHN->mergeTypeInfo(C->getType(), NH.getOffset());
Chris Lattner8c1a8352003-09-20 21:48:16 +00001035
1036 if (C->getType()->isFirstClassType()) {
1037 if (isPointerType(C->getType()))
1038 // Avoid adding edges from null, or processing non-"pointer" stores
1039 NH.addEdgeTo(getValueDest(*C));
1040 return;
1041 }
Chris Lattner2b9926f2003-11-02 22:27:28 +00001042
1043 const TargetData &TD = NH.getNode()->getTargetData();
1044
Chris Lattner8c1a8352003-09-20 21:48:16 +00001045 if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
1046 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1047 // We don't currently do any indexing for arrays...
1048 MergeConstantInitIntoNode(NH, cast<Constant>(CA->getOperand(i)));
1049 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
1050 const StructLayout *SL = TD.getStructLayout(CS->getType());
1051 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
Chris Lattnerbc740092004-10-30 04:22:45 +00001052 DSNode *NHN = NH.getNode();
Chris Lattner2f8e4ad2005-01-12 04:51:37 +00001053 DSNodeHandle NewNH(NHN, NH.getOffset()+(unsigned)SL->MemberOffsets[i]);
Chris Lattner8c1a8352003-09-20 21:48:16 +00001054 MergeConstantInitIntoNode(NewNH, cast<Constant>(CS->getOperand(i)));
1055 }
Chris Lattnerfda51a592004-10-26 16:23:03 +00001056 } else if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) {
Chris Lattner218eb792004-02-15 05:53:42 +00001057 // Noop
Chris Lattner8c1a8352003-09-20 21:48:16 +00001058 } else {
1059 assert(0 && "Unknown constant type!");
1060 }
1061}
1062
1063void GraphBuilder::mergeInGlobalInitializer(GlobalVariable *GV) {
1064 assert(!GV->isExternal() && "Cannot merge in external global!");
1065 // Get a node handle to the global node and merge the initializer into it.
1066 DSNodeHandle NH = getValueDest(*GV);
1067 MergeConstantInitIntoNode(NH, GV->getInitializer());
1068}
1069
1070
Chris Lattner4f2cf032004-09-20 04:48:05 +00001071bool LocalDataStructures::runOnModule(Module &M) {
Chris Lattner2b9926f2003-11-02 22:27:28 +00001072 const TargetData &TD = getAnalysis<TargetData>();
Chris Lattner4b1be352002-11-09 21:12:07 +00001073
Chris Lattnerc01e2f72005-03-04 20:27:46 +00001074 GlobalsGraph = new DSGraph(TD);
1075
Chris Lattnerfab28722004-02-25 23:31:02 +00001076 {
1077 GraphBuilder GGB(*GlobalsGraph);
1078
1079 // Add initializers for all of the globals to the globals graph...
Chris Lattner531f9e92005-03-15 04:54:21 +00001080 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Chris Lattnerfab28722004-02-25 23:31:02 +00001081 if (!I->isExternal())
1082 GGB.mergeInGlobalInitializer(I);
1083 }
1084
Chris Lattner4b1be352002-11-09 21:12:07 +00001085 // Calculate all of the graphs...
1086 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1087 if (!I->isExternal())
Chris Lattner2b9926f2003-11-02 22:27:28 +00001088 DSInfo.insert(std::make_pair(I, new DSGraph(TD, *I, GlobalsGraph)));
Chris Lattner8c1a8352003-09-20 21:48:16 +00001089
Chris Lattner9ab85272004-02-08 01:51:48 +00001090 GlobalsGraph->removeTriviallyDeadNodes();
Chris Lattner8c1a8352003-09-20 21:48:16 +00001091 GlobalsGraph->markIncompleteNodes(DSGraph::MarkFormalArgs);
Chris Lattner4b1be352002-11-09 21:12:07 +00001092 return false;
1093}
1094
Chris Lattner193e6922002-10-01 22:33:50 +00001095// releaseMemory - If the pass pipeline is done with this pass, we can release
1096// our memory... here...
1097//
1098void LocalDataStructures::releaseMemory() {
Chris Lattner01877f52003-06-30 04:53:27 +00001099 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
1100 E = DSInfo.end(); I != E; ++I) {
1101 I->second->getReturnNodes().erase(I->first);
1102 if (I->second->getReturnNodes().empty())
1103 delete I->second;
1104 }
Chris Lattner193e6922002-10-01 22:33:50 +00001105
1106 // Empty map so next time memory is released, data structures are not
1107 // re-deleted.
1108 DSInfo.clear();
Chris Lattnere742f312002-11-09 20:01:01 +00001109 delete GlobalsGraph;
1110 GlobalsGraph = 0;
Chris Lattner193e6922002-10-01 22:33:50 +00001111}
Brian Gaeke960707c2003-11-11 22:41:34 +00001112