blob: 8eacc1f38e0d852d2433ccb8a53cd18391028764 [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 Lattnere4d5c442005-03-15 04:54:21 +000085 for (Function::arg_iterator I = f.arg_begin(), E = f.arg_end(); I != E; ++I)
Chris Lattnerfccd06f2002-10-01 22:33:50 +000086 if (isPointerType(I->getType()))
87 getValueDest(*I);
Chris Lattner0d9bab82002-07-18 00:12:30 +000088
Chris Lattner26c4fc32003-09-20 21:48:16 +000089 visit(f); // Single pass over the function
Chris Lattnerc68c31b2002-07-10 22:38:08 +000090 }
91
Chris Lattner26c4fc32003-09-20 21:48:16 +000092 // GraphBuilder ctor for working on the globals graph
93 GraphBuilder(DSGraph &g)
94 : G(g), RetNode(0), ScalarMap(G.getScalarMap()), FunctionCalls(0) {
95 }
96
97 void mergeInGlobalInitializer(GlobalVariable *GV);
98
Chris Lattnerc68c31b2002-07-10 22:38:08 +000099 private:
100 // Visitor functions, used to handle each instruction type we encounter...
101 friend class InstVisitor<GraphBuilder>;
Chris Lattnerbd92b732003-06-19 21:15:11 +0000102 void visitMallocInst(MallocInst &MI) { handleAlloc(MI, true); }
103 void visitAllocaInst(AllocaInst &AI) { handleAlloc(AI, false); }
104 void handleAlloc(AllocationInst &AI, bool isHeap);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000105
106 void visitPHINode(PHINode &PN);
Chris Lattner6e84bd72005-02-25 01:27:48 +0000107 void visitSelectInst(SelectInst &SI);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000108
Chris Lattner51340062002-11-08 05:00:44 +0000109 void visitGetElementPtrInst(User &GEP);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000110 void visitReturnInst(ReturnInst &RI);
111 void visitLoadInst(LoadInst &LI);
112 void visitStoreInst(StoreInst &SI);
113 void visitCallInst(CallInst &CI);
Chris Lattner808a7ae2003-09-20 16:34:13 +0000114 void visitInvokeInst(InvokeInst &II);
Chris Lattner32672652005-03-05 19:04:31 +0000115 void visitSetCondInst(SetCondInst &SCI);
Vikram S. Advebac06222002-12-06 21:17:10 +0000116 void visitFreeInst(FreeInst &FI);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000117 void visitCastInst(CastInst &CI);
Chris Lattner878e5212003-02-04 00:59:50 +0000118 void visitInstruction(Instruction &I);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000119
Chris Lattner808a7ae2003-09-20 16:34:13 +0000120 void visitCallSite(CallSite CS);
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000121 void visitVANextInst(VANextInst &I);
122 void visitVAArgInst(VAArgInst &I);
Chris Lattner26c4fc32003-09-20 21:48:16 +0000123
124 void MergeConstantInitIntoNode(DSNodeHandle &NH, Constant *C);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000125 private:
126 // Helper functions used to implement the visitation functions...
127
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000128 /// createNode - Create a new DSNode, ensuring that it is properly added to
129 /// the graph.
130 ///
Chris Lattnerbd92b732003-06-19 21:15:11 +0000131 DSNode *createNode(const Type *Ty = 0) {
132 DSNode *N = new DSNode(Ty, &G); // Create the node
Chris Lattnere158b192003-06-16 12:08:18 +0000133 if (DisableFieldSensitivity) {
Chris Lattner2412a052004-05-23 21:14:09 +0000134 // Create node handle referring to the old node so that it is
135 // immediately removed from the graph when the node handle is destroyed.
136 DSNodeHandle OldNNH = N;
Chris Lattnerca3f7902003-02-08 20:18:39 +0000137 N->foldNodeCompletely();
Chris Lattnere158b192003-06-16 12:08:18 +0000138 if (DSNode *FN = N->getForwardNode())
139 N = FN;
140 }
Chris Lattner92673292002-11-02 00:13:20 +0000141 return N;
142 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000143
Chris Lattnerc875f022002-11-03 21:27:48 +0000144 /// setDestTo - Set the ScalarMap entry for the specified value to point to
Chris Lattner92673292002-11-02 00:13:20 +0000145 /// the specified destination. If the Value already points to a node, make
146 /// sure to merge the two destinations together.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000147 ///
Chris Lattner92673292002-11-02 00:13:20 +0000148 void setDestTo(Value &V, const DSNodeHandle &NH);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000149
Chris Lattner92673292002-11-02 00:13:20 +0000150 /// getValueDest - Return the DSNode that the actual value points to.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000151 ///
Chris Lattner92673292002-11-02 00:13:20 +0000152 DSNodeHandle getValueDest(Value &V);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000153
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000154 /// getLink - This method is used to return the specified link in the
155 /// specified node if one exists. If a link does not already exist (it's
Chris Lattner7e51c872002-10-31 06:52:26 +0000156 /// null), then we create a new node, link it, then return it.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000157 ///
Chris Lattner7e51c872002-10-31 06:52:26 +0000158 DSNodeHandle &getLink(const DSNodeHandle &Node, unsigned Link = 0);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000159 };
160}
161
Brian Gaeked0fde302003-11-11 22:41:34 +0000162using namespace DS;
163
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000164//===----------------------------------------------------------------------===//
165// DSGraph constructor - Simply use the GraphBuilder to construct the local
166// graph.
Chris Lattnerf4f62272005-03-19 22:23:45 +0000167DSGraph::DSGraph(EquivalenceClasses<GlobalValue*> &ECs, const TargetData &td,
168 Function &F, DSGraph *GG)
169 : GlobalsGraph(GG), ScalarMap(ECs), TD(td) {
Chris Lattner2a068862002-11-10 06:53:38 +0000170 PrintAuxCalls = false;
Chris Lattner30514192003-07-02 04:37:26 +0000171
172 DEBUG(std::cerr << " [Loc] Calculating graph for: " << F.getName() << "\n");
173
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000174 // Use the graph builder to construct the local version of the graph
Chris Lattner26c4fc32003-09-20 21:48:16 +0000175 GraphBuilder B(F, *this, ReturnNodes[&F], FunctionCalls);
Chris Lattner4fe34612002-11-18 21:44:19 +0000176#ifndef NDEBUG
177 Timer::addPeakMemoryMeasurement();
178#endif
Chris Lattner1a1a85d2003-02-14 04:55:58 +0000179
180 // Remove all integral constants from the scalarmap!
Chris Lattner62482e52004-01-28 09:15:42 +0000181 for (DSScalarMap::iterator I = ScalarMap.begin(); I != ScalarMap.end();)
Chris Lattner02da0322004-01-27 21:51:19 +0000182 if (isa<ConstantIntegral>(I->first))
183 ScalarMap.erase(I++);
184 else
Chris Lattner1a1a85d2003-02-14 04:55:58 +0000185 ++I;
186
Chris Lattnerc420ab62004-02-25 23:31:02 +0000187 // If there are any constant globals referenced in this function, merge their
188 // initializers into the local graph from the globals graph.
189 if (ScalarMap.global_begin() != ScalarMap.global_end()) {
190 ReachabilityCloner RC(*this, *GG, 0);
191
192 for (DSScalarMap::global_iterator I = ScalarMap.global_begin();
193 I != ScalarMap.global_end(); ++I)
194 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I))
Chris Lattner76a9eb32004-03-03 23:00:19 +0000195 if (!GV->isExternal() && GV->isConstant())
Chris Lattnerc420ab62004-02-25 23:31:02 +0000196 RC.merge(ScalarMap[GV], GG->ScalarMap[GV]);
197 }
198
Chris Lattner394471f2003-01-23 22:05:33 +0000199 markIncompleteNodes(DSGraph::MarkFormalArgs);
Chris Lattner96517252002-11-09 20:55:24 +0000200
201 // Remove any nodes made dead due to merging...
Chris Lattner394471f2003-01-23 22:05:33 +0000202 removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000203}
204
205
206//===----------------------------------------------------------------------===//
207// Helper method implementations...
208//
209
Chris Lattner92673292002-11-02 00:13:20 +0000210/// getValueDest - Return the DSNode that the actual value points to.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000211///
Chris Lattner51340062002-11-08 05:00:44 +0000212DSNodeHandle GraphBuilder::getValueDest(Value &Val) {
213 Value *V = &Val;
Chris Lattner82962de2004-11-03 18:51:26 +0000214 if (isa<Constant>(V) && cast<Constant>(V)->isNullValue())
Chris Lattner51340062002-11-08 05:00:44 +0000215 return 0; // Null doesn't point to anything, don't add to ScalarMap!
Chris Lattner92673292002-11-02 00:13:20 +0000216
Vikram S. Advebac06222002-12-06 21:17:10 +0000217 DSNodeHandle &NH = ScalarMap[V];
Chris Lattner62c3a952004-10-30 04:22:45 +0000218 if (!NH.isNull())
Vikram S. Advebac06222002-12-06 21:17:10 +0000219 return NH; // Already have a node? Just return it...
220
221 // Otherwise we need to create a new node to point to.
222 // Check first for constant expressions that must be traversed to
223 // extract the actual value.
Reid Spencere8404342004-07-18 00:18:30 +0000224 DSNode* N;
225 if (GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner48427b52005-03-20 01:18:00 +0000226 // Create a new global node for this global variable.
Reid Spencere8404342004-07-18 00:18:30 +0000227 N = createNode(GV->getType()->getElementType());
228 N->addGlobal(GV);
229 } else if (Constant *C = dyn_cast<Constant>(V)) {
230 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattner51340062002-11-08 05:00:44 +0000231 if (CE->getOpcode() == Instruction::Cast)
Chris Lattner1e56c542003-02-09 23:04:12 +0000232 NH = getValueDest(*CE->getOperand(0));
233 else if (CE->getOpcode() == Instruction::GetElementPtr) {
Chris Lattner51340062002-11-08 05:00:44 +0000234 visitGetElementPtrInst(*CE);
Chris Lattner62482e52004-01-28 09:15:42 +0000235 DSScalarMap::iterator I = ScalarMap.find(CE);
Chris Lattner2cfbaaf2002-11-09 20:14:03 +0000236 assert(I != ScalarMap.end() && "GEP didn't get processed right?");
Chris Lattner1e56c542003-02-09 23:04:12 +0000237 NH = I->second;
238 } else {
239 // This returns a conservative unknown node for any unhandled ConstExpr
Chris Lattnerbd92b732003-06-19 21:15:11 +0000240 return NH = createNode()->setUnknownNodeMarker();
Chris Lattner51340062002-11-08 05:00:44 +0000241 }
Chris Lattner62c3a952004-10-30 04:22:45 +0000242 if (NH.isNull()) { // (getelementptr null, X) returns null
Chris Lattner1e56c542003-02-09 23:04:12 +0000243 ScalarMap.erase(V);
244 return 0;
245 }
246 return NH;
Chris Lattner51340062002-11-08 05:00:44 +0000247
Chris Lattner51340062002-11-08 05:00:44 +0000248 } else if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(C)) {
249 // Random constants are unknown mem
Chris Lattnerbd92b732003-06-19 21:15:11 +0000250 return NH = createNode()->setUnknownNodeMarker();
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000251 } else if (isa<UndefValue>(C)) {
252 ScalarMap.erase(V);
253 return 0;
Chris Lattner51340062002-11-08 05:00:44 +0000254 } else {
255 assert(0 && "Unknown constant type!");
256 }
Reid Spencere8404342004-07-18 00:18:30 +0000257 N = createNode(); // just create a shadow node
Chris Lattner92673292002-11-02 00:13:20 +0000258 } else {
259 // Otherwise just create a shadow node
Chris Lattnerbd92b732003-06-19 21:15:11 +0000260 N = createNode();
Chris Lattner92673292002-11-02 00:13:20 +0000261 }
262
Chris Lattnerefffdc92004-07-07 06:12:52 +0000263 NH.setTo(N, 0); // Remember that we are pointing to it...
Chris Lattner92673292002-11-02 00:13:20 +0000264 return NH;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000265}
266
Chris Lattner0d9bab82002-07-18 00:12:30 +0000267
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000268/// getLink - This method is used to return the specified link in the
269/// specified node if one exists. If a link does not already exist (it's
270/// null), then we create a new node, link it, then return it. We must
271/// specify the type of the Node field we are accessing so that we know what
272/// type should be linked to if we need to create a new node.
273///
Chris Lattner7e51c872002-10-31 06:52:26 +0000274DSNodeHandle &GraphBuilder::getLink(const DSNodeHandle &node, unsigned LinkNo) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000275 DSNodeHandle &Node = const_cast<DSNodeHandle&>(node);
Chris Lattner08db7192002-11-06 06:20:27 +0000276 DSNodeHandle &Link = Node.getLink(LinkNo);
Chris Lattner62c3a952004-10-30 04:22:45 +0000277 if (Link.isNull()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000278 // If the link hasn't been created yet, make and return a new shadow node
Chris Lattnerbd92b732003-06-19 21:15:11 +0000279 Link = createNode();
Chris Lattner08db7192002-11-06 06:20:27 +0000280 }
281 return Link;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000282}
283
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000284
Chris Lattnerc875f022002-11-03 21:27:48 +0000285/// setDestTo - Set the ScalarMap entry for the specified value to point to the
Chris Lattner92673292002-11-02 00:13:20 +0000286/// specified destination. If the Value already points to a node, make sure to
287/// merge the two destinations together.
288///
289void GraphBuilder::setDestTo(Value &V, const DSNodeHandle &NH) {
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000290 ScalarMap[&V].mergeWith(NH);
Chris Lattner92673292002-11-02 00:13:20 +0000291}
292
293
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000294//===----------------------------------------------------------------------===//
295// Specific instruction type handler implementations...
296//
297
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000298/// Alloca & Malloc instruction implementation - Simply create a new memory
299/// object, pointing the scalar to it.
300///
Chris Lattnerbd92b732003-06-19 21:15:11 +0000301void GraphBuilder::handleAlloc(AllocationInst &AI, bool isHeap) {
302 DSNode *N = createNode();
303 if (isHeap)
304 N->setHeapNodeMarker();
305 else
306 N->setAllocaNodeMarker();
307 setDestTo(AI, N);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000308}
309
310// PHINode - Make the scalar for the PHI node point to all of the things the
311// incoming values point to... which effectively causes them to be merged.
312//
313void GraphBuilder::visitPHINode(PHINode &PN) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000314 if (!isPointerType(PN.getType())) return; // Only pointer PHIs
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000315
Chris Lattnerc875f022002-11-03 21:27:48 +0000316 DSNodeHandle &PNDest = ScalarMap[&PN];
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000317 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
Chris Lattner92673292002-11-02 00:13:20 +0000318 PNDest.mergeWith(getValueDest(*PN.getIncomingValue(i)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000319}
320
Chris Lattner6e84bd72005-02-25 01:27:48 +0000321void GraphBuilder::visitSelectInst(SelectInst &SI) {
322 if (!isPointerType(SI.getType())) return; // Only pointer Selects
323
324 DSNodeHandle &Dest = ScalarMap[&SI];
325 Dest.mergeWith(getValueDest(*SI.getOperand(1)));
326 Dest.mergeWith(getValueDest(*SI.getOperand(2)));
327}
328
Chris Lattner32672652005-03-05 19:04:31 +0000329void GraphBuilder::visitSetCondInst(SetCondInst &SCI) {
330 if (!isPointerType(SCI.getOperand(0)->getType()) ||
331 isa<ConstantPointerNull>(SCI.getOperand(1))) return; // Only pointers
332 ScalarMap[SCI.getOperand(0)].mergeWith(getValueDest(*SCI.getOperand(1)));
333}
334
335
Chris Lattner51340062002-11-08 05:00:44 +0000336void GraphBuilder::visitGetElementPtrInst(User &GEP) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000337 DSNodeHandle Value = getValueDest(*GEP.getOperand(0));
Chris Lattner753b1132005-02-24 19:55:31 +0000338 if (Value.isNull())
339 Value = createNode();
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000340
Chris Lattner179bc7d2003-11-14 17:09:46 +0000341 // As a special case, if all of the index operands of GEP are constant zeros,
342 // handle this just like we handle casts (ie, don't do much).
343 bool AllZeros = true;
344 for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i)
345 if (GEP.getOperand(i) !=
346 Constant::getNullValue(GEP.getOperand(i)->getType())) {
347 AllZeros = false;
348 break;
349 }
350
351 // If all of the indices are zero, the result points to the operand without
352 // applying the type.
Chris Lattner0c9707a2005-03-18 23:18:20 +0000353 if (AllZeros || (!Value.isNull() &&
354 Value.getNode()->isNodeCompletelyFolded())) {
Chris Lattner179bc7d2003-11-14 17:09:46 +0000355 setDestTo(GEP, Value);
356 return;
357 }
358
359
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000360 const PointerType *PTy = cast<PointerType>(GEP.getOperand(0)->getType());
361 const Type *CurTy = PTy->getElementType();
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000362
Chris Lattner08db7192002-11-06 06:20:27 +0000363 if (Value.getNode()->mergeTypeInfo(CurTy, Value.getOffset())) {
364 // If the node had to be folded... exit quickly
Chris Lattner92673292002-11-02 00:13:20 +0000365 setDestTo(GEP, Value); // GEP result points to folded node
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000366 return;
367 }
368
Chris Lattner15869aa2003-11-02 22:27:28 +0000369 const TargetData &TD = Value.getNode()->getTargetData();
370
Chris Lattner08db7192002-11-06 06:20:27 +0000371#if 0
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000372 // Handle the pointer index specially...
373 if (GEP.getNumOperands() > 1 &&
Chris Lattner28977af2004-04-05 01:30:19 +0000374 (!isa<Constant>(GEP.getOperand(1)) ||
375 !cast<Constant>(GEP.getOperand(1))->isNullValue())) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000376
377 // If we already know this is an array being accessed, don't do anything...
378 if (!TopTypeRec.isArray) {
379 TopTypeRec.isArray = true;
380
381 // If we are treating some inner field pointer as an array, fold the node
382 // up because we cannot handle it right. This can come because of
383 // something like this: &((&Pt->X)[1]) == &Pt->Y
384 //
385 if (Value.getOffset()) {
386 // Value is now the pointer we want to GEP to be...
387 Value.getNode()->foldNodeCompletely();
Chris Lattner92673292002-11-02 00:13:20 +0000388 setDestTo(GEP, Value); // GEP result points to folded node
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000389 return;
390 } else {
391 // This is a pointer to the first byte of the node. Make sure that we
392 // are pointing to the outter most type in the node.
393 // FIXME: We need to check one more case here...
394 }
395 }
396 }
Chris Lattner08db7192002-11-06 06:20:27 +0000397#endif
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000398
399 // All of these subscripts are indexing INTO the elements we have...
Chris Lattner26c4fc32003-09-20 21:48:16 +0000400 unsigned Offset = 0;
Chris Lattnerfa3711a2003-11-25 20:19:55 +0000401 for (gep_type_iterator I = gep_type_begin(GEP), E = gep_type_end(GEP);
402 I != E; ++I)
403 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner507bdf92005-01-12 04:51:37 +0000404 unsigned FieldNo =
405 (unsigned)cast<ConstantUInt>(I.getOperand())->getValue();
406 Offset += (unsigned)TD.getStructLayout(STy)->MemberOffsets[FieldNo];
Chris Lattner82e9d722004-03-01 19:02:54 +0000407 } else if (const PointerType *PTy = dyn_cast<PointerType>(*I)) {
408 if (!isa<Constant>(I.getOperand()) ||
409 !cast<Constant>(I.getOperand())->isNullValue())
410 Value.getNode()->setArrayMarker();
Chris Lattnerfa3711a2003-11-25 20:19:55 +0000411 }
412
413
Chris Lattner08db7192002-11-06 06:20:27 +0000414#if 0
Chris Lattnerfa3711a2003-11-25 20:19:55 +0000415 if (const SequentialType *STy = cast<SequentialType>(*I)) {
416 CurTy = STy->getElementType();
Chris Lattnere03f32b2002-10-02 06:24:36 +0000417 if (ConstantSInt *CS = dyn_cast<ConstantSInt>(GEP.getOperand(i))) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000418 Offset += CS->getValue()*TD.getTypeSize(CurTy);
Chris Lattnere03f32b2002-10-02 06:24:36 +0000419 } else {
420 // Variable index into a node. We must merge all of the elements of the
421 // sequential type here.
422 if (isa<PointerType>(STy))
423 std::cerr << "Pointer indexing not handled yet!\n";
424 else {
425 const ArrayType *ATy = cast<ArrayType>(STy);
426 unsigned ElSize = TD.getTypeSize(CurTy);
427 DSNode *N = Value.getNode();
428 assert(N && "Value must have a node!");
429 unsigned RawOffset = Offset+Value.getOffset();
430
431 // Loop over all of the elements of the array, merging them into the
Misha Brukman2f2d0652003-09-11 18:14:24 +0000432 // zeroth element.
Chris Lattnere03f32b2002-10-02 06:24:36 +0000433 for (unsigned i = 1, e = ATy->getNumElements(); i != e; ++i)
434 // Merge all of the byte components of this array element
435 for (unsigned j = 0; j != ElSize; ++j)
436 N->mergeIndexes(RawOffset+j, RawOffset+i*ElSize+j);
437 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000438 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000439 }
Chris Lattnerfa3711a2003-11-25 20:19:55 +0000440#endif
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000441
442 // Add in the offset calculated...
443 Value.setOffset(Value.getOffset()+Offset);
444
445 // Value is now the pointer we want to GEP to be...
Chris Lattner92673292002-11-02 00:13:20 +0000446 setDestTo(GEP, Value);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000447}
448
449void GraphBuilder::visitLoadInst(LoadInst &LI) {
Chris Lattner92673292002-11-02 00:13:20 +0000450 DSNodeHandle Ptr = getValueDest(*LI.getOperand(0));
Chris Lattner6e84bd72005-02-25 01:27:48 +0000451 if (Ptr.isNull())
452 Ptr = createNode();
Chris Lattner92673292002-11-02 00:13:20 +0000453
454 // Make that the node is read from...
Chris Lattnerbd92b732003-06-19 21:15:11 +0000455 Ptr.getNode()->setReadMarker();
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000456
457 // Ensure a typerecord exists...
Chris Lattner088b6392003-03-03 17:13:31 +0000458 Ptr.getNode()->mergeTypeInfo(LI.getType(), Ptr.getOffset(), false);
Chris Lattner92673292002-11-02 00:13:20 +0000459
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000460 if (isPointerType(LI.getType()))
Chris Lattner92673292002-11-02 00:13:20 +0000461 setDestTo(LI, getLink(Ptr));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000462}
463
464void GraphBuilder::visitStoreInst(StoreInst &SI) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000465 const Type *StoredTy = SI.getOperand(0)->getType();
Chris Lattner92673292002-11-02 00:13:20 +0000466 DSNodeHandle Dest = getValueDest(*SI.getOperand(1));
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000467 if (Dest.isNull()) return;
Chris Lattner92673292002-11-02 00:13:20 +0000468
Vikram S. Advebac06222002-12-06 21:17:10 +0000469 // Mark that the node is written to...
Chris Lattnerbd92b732003-06-19 21:15:11 +0000470 Dest.getNode()->setModifiedMarker();
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000471
Chris Lattner26c4fc32003-09-20 21:48:16 +0000472 // Ensure a type-record exists...
Chris Lattner08db7192002-11-06 06:20:27 +0000473 Dest.getNode()->mergeTypeInfo(StoredTy, Dest.getOffset());
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000474
475 // Avoid adding edges from null, or processing non-"pointer" stores
Chris Lattner92673292002-11-02 00:13:20 +0000476 if (isPointerType(StoredTy))
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000477 Dest.addEdgeTo(getValueDest(*SI.getOperand(0)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000478}
479
480void GraphBuilder::visitReturnInst(ReturnInst &RI) {
Chris Lattner92673292002-11-02 00:13:20 +0000481 if (RI.getNumOperands() && isPointerType(RI.getOperand(0)->getType()))
Chris Lattner26c4fc32003-09-20 21:48:16 +0000482 RetNode->mergeWith(getValueDest(*RI.getOperand(0)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000483}
484
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000485void GraphBuilder::visitVANextInst(VANextInst &I) {
486 getValueDest(*I.getOperand(0)).mergeWith(getValueDest(I));
487}
488
489void GraphBuilder::visitVAArgInst(VAArgInst &I) {
490 DSNodeHandle Ptr = getValueDest(*I.getOperand(0));
491 if (Ptr.isNull()) return;
492
493 // Make that the node is read from.
494 Ptr.getNode()->setReadMarker();
495
Chris Lattner62c3a952004-10-30 04:22:45 +0000496 // Ensure a type record exists.
497 DSNode *PtrN = Ptr.getNode();
498 PtrN->mergeTypeInfo(I.getType(), Ptr.getOffset(), false);
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000499
500 if (isPointerType(I.getType()))
501 setDestTo(I, getLink(Ptr));
502}
503
504
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000505void GraphBuilder::visitCallInst(CallInst &CI) {
Chris Lattner808a7ae2003-09-20 16:34:13 +0000506 visitCallSite(&CI);
507}
508
509void GraphBuilder::visitInvokeInst(InvokeInst &II) {
510 visitCallSite(&II);
511}
512
513void GraphBuilder::visitCallSite(CallSite CS) {
Chris Lattnercb582402004-02-26 22:07:22 +0000514 Value *Callee = CS.getCalledValue();
Chris Lattnercb582402004-02-26 22:07:22 +0000515
Chris Lattner894263b2003-09-20 16:50:46 +0000516 // Special case handling of certain libc allocation functions here.
Chris Lattnercb582402004-02-26 22:07:22 +0000517 if (Function *F = dyn_cast<Function>(Callee))
Chris Lattner894263b2003-09-20 16:50:46 +0000518 if (F->isExternal())
Chris Lattnera07b72f2004-02-13 16:09:54 +0000519 switch (F->getIntrinsicID()) {
Chris Lattner317201d2004-03-13 00:24:00 +0000520 case Intrinsic::vastart:
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000521 getValueDest(*CS.getInstruction()).getNode()->setAllocaNodeMarker();
522 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000523 case Intrinsic::vacopy:
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000524 getValueDest(*CS.getInstruction()).
525 mergeWith(getValueDest(**(CS.arg_begin())));
526 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000527 case Intrinsic::vaend:
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000528 return; // noop
Chris Lattnera07b72f2004-02-13 16:09:54 +0000529 case Intrinsic::memmove:
530 case Intrinsic::memcpy: {
531 // Merge the first & second arguments, and mark the memory read and
Chris Lattnerfb8c6102003-11-08 21:55:50 +0000532 // modified.
Chris Lattnera07b72f2004-02-13 16:09:54 +0000533 DSNodeHandle RetNH = getValueDest(**CS.arg_begin());
Chris Lattner67ce57a2003-11-09 03:32:52 +0000534 RetNH.mergeWith(getValueDest(**(CS.arg_begin()+1)));
535 if (DSNode *N = RetNH.getNode())
536 N->setModifiedMarker()->setReadMarker();
537 return;
Chris Lattnera07b72f2004-02-13 16:09:54 +0000538 }
Chris Lattnereee33b22004-02-16 18:37:40 +0000539 case Intrinsic::memset:
540 // Mark the memory modified.
541 if (DSNode *N = getValueDest(**CS.arg_begin()).getNode())
542 N->setModifiedMarker();
543 return;
Chris Lattnera07b72f2004-02-13 16:09:54 +0000544 default:
Vikram S. Advee4e97ef2004-05-25 08:14:52 +0000545 if (F->getName() == "calloc" || F->getName() == "posix_memalign" ||
546 F->getName() == "memalign" || F->getName() == "valloc") {
Chris Lattnera07b72f2004-02-13 16:09:54 +0000547 setDestTo(*CS.getInstruction(),
548 createNode()->setHeapNodeMarker()->setModifiedMarker());
549 return;
550 } else if (F->getName() == "realloc") {
551 DSNodeHandle RetNH = getValueDest(*CS.getInstruction());
Chris Lattner82962de2004-11-03 18:51:26 +0000552 if (CS.arg_begin() != CS.arg_end())
553 RetNH.mergeWith(getValueDest(**CS.arg_begin()));
Chris Lattnera07b72f2004-02-13 16:09:54 +0000554 if (DSNode *N = RetNH.getNode())
555 N->setHeapNodeMarker()->setModifiedMarker()->setReadMarker();
556 return;
Chris Lattner1fe98742004-02-26 03:43:08 +0000557 } else if (F->getName() == "memmove") {
558 // Merge the first & second arguments, and mark the memory read and
559 // modified.
560 DSNodeHandle RetNH = getValueDest(**CS.arg_begin());
561 RetNH.mergeWith(getValueDest(**(CS.arg_begin()+1)));
562 if (DSNode *N = RetNH.getNode())
563 N->setModifiedMarker()->setReadMarker();
564 return;
565
Chris Lattnerd5612092004-02-24 22:02:48 +0000566 } else if (F->getName() == "atoi" || F->getName() == "atof" ||
Chris Lattneradc1efe2004-02-25 17:43:20 +0000567 F->getName() == "atol" || F->getName() == "atoll" ||
Chris Lattner39bb2dc2004-02-24 22:17:00 +0000568 F->getName() == "remove" || F->getName() == "unlink" ||
Chris Lattneradc1efe2004-02-25 17:43:20 +0000569 F->getName() == "rename" || F->getName() == "memcmp" ||
570 F->getName() == "strcmp" || F->getName() == "strncmp" ||
571 F->getName() == "execl" || F->getName() == "execlp" ||
572 F->getName() == "execle" || F->getName() == "execv" ||
Chris Lattner52fc8d72004-02-25 23:06:40 +0000573 F->getName() == "execvp" || F->getName() == "chmod" ||
574 F->getName() == "puts" || F->getName() == "write" ||
575 F->getName() == "open" || F->getName() == "create" ||
576 F->getName() == "truncate" || F->getName() == "chdir" ||
577 F->getName() == "mkdir" || F->getName() == "rmdir") {
Chris Lattner39bb2dc2004-02-24 22:17:00 +0000578 // These functions read all of their pointer operands.
579 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
580 AI != E; ++AI) {
581 if (isPointerType((*AI)->getType()))
582 if (DSNode *N = getValueDest(**AI).getNode())
583 N->setReadMarker();
584 }
Chris Lattner304e1432004-02-16 22:57:19 +0000585 return;
Chris Lattner52fc8d72004-02-25 23:06:40 +0000586 } else if (F->getName() == "read" || F->getName() == "pipe" ||
Chris Lattner6b586df2004-02-27 20:04:48 +0000587 F->getName() == "wait" || F->getName() == "time") {
Chris Lattner52fc8d72004-02-25 23:06:40 +0000588 // These functions write all of their pointer operands.
589 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
590 AI != E; ++AI) {
591 if (isPointerType((*AI)->getType()))
592 if (DSNode *N = getValueDest(**AI).getNode())
593 N->setModifiedMarker();
594 }
595 return;
Chris Lattneradc1efe2004-02-25 17:43:20 +0000596 } else if (F->getName() == "stat" || F->getName() == "fstat" ||
597 F->getName() == "lstat") {
598 // These functions read their first operand if its a pointer.
599 CallSite::arg_iterator AI = CS.arg_begin();
600 if (isPointerType((*AI)->getType())) {
601 DSNodeHandle Path = getValueDest(**AI);
602 if (DSNode *N = Path.getNode()) N->setReadMarker();
603 }
Chris Lattner304e1432004-02-16 22:57:19 +0000604
Chris Lattneradc1efe2004-02-25 17:43:20 +0000605 // Then they write into the stat buffer.
606 DSNodeHandle StatBuf = getValueDest(**++AI);
607 if (DSNode *N = StatBuf.getNode()) {
608 N->setModifiedMarker();
609 const Type *StatTy = F->getFunctionType()->getParamType(1);
610 if (const PointerType *PTy = dyn_cast<PointerType>(StatTy))
611 N->mergeTypeInfo(PTy->getElementType(), StatBuf.getOffset());
612 }
Chris Lattneradc1efe2004-02-25 17:43:20 +0000613 return;
Chris Lattner3aeb40c2004-03-04 21:03:54 +0000614 } else if (F->getName() == "strtod" || F->getName() == "strtof" ||
615 F->getName() == "strtold") {
616 // These functions read the first pointer
617 if (DSNode *Str = getValueDest(**CS.arg_begin()).getNode()) {
618 Str->setReadMarker();
619 // If the second parameter is passed, it will point to the first
620 // argument node.
621 const DSNodeHandle &EndPtrNH = getValueDest(**(CS.arg_begin()+1));
622 if (DSNode *End = EndPtrNH.getNode()) {
623 End->mergeTypeInfo(PointerType::get(Type::SByteTy),
624 EndPtrNH.getOffset(), false);
625 End->setModifiedMarker();
626 DSNodeHandle &Link = getLink(EndPtrNH);
627 Link.mergeWith(getValueDest(**CS.arg_begin()));
628 }
629 }
630
631 return;
Chris Lattner6b586df2004-02-27 20:04:48 +0000632 } else if (F->getName() == "fopen" || F->getName() == "fdopen" ||
633 F->getName() == "freopen") {
634 // These functions read all of their pointer operands.
635 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
636 AI != E; ++AI)
637 if (isPointerType((*AI)->getType()))
638 if (DSNode *N = getValueDest(**AI).getNode())
639 N->setReadMarker();
Chris Lattner68300db2004-02-13 20:05:32 +0000640
641 // fopen allocates in an unknown way and writes to the file
642 // descriptor. Also, merge the allocated type into the node.
643 DSNodeHandle Result = getValueDest(*CS.getInstruction());
Chris Lattnerd5612092004-02-24 22:02:48 +0000644 if (DSNode *N = Result.getNode()) {
645 N->setModifiedMarker()->setUnknownNodeMarker();
646 const Type *RetTy = F->getFunctionType()->getReturnType();
647 if (const PointerType *PTy = dyn_cast<PointerType>(RetTy))
648 N->mergeTypeInfo(PTy->getElementType(), Result.getOffset());
649 }
Chris Lattner6b586df2004-02-27 20:04:48 +0000650
651 // If this is freopen, merge the file descriptor passed in with the
652 // result.
Chris Lattnerfe781652004-12-08 16:22:26 +0000653 if (F->getName() == "freopen") {
654 // ICC doesn't handle getting the iterator, decrementing and
655 // dereferencing it in one operation without error. Do it in 2 steps
656 CallSite::arg_iterator compit = CS.arg_end();
657 Result.mergeWith(getValueDest(**--compit));
658 }
Chris Lattnerd5612092004-02-24 22:02:48 +0000659 return;
Chris Lattner68300db2004-02-13 20:05:32 +0000660 } else if (F->getName() == "fclose" && CS.arg_end()-CS.arg_begin() ==1){
661 // fclose reads and deallocates the memory in an unknown way for the
662 // file descriptor. It merges the FILE type into the descriptor.
663 DSNodeHandle H = getValueDest(**CS.arg_begin());
Chris Lattnerd5612092004-02-24 22:02:48 +0000664 if (DSNode *N = H.getNode()) {
665 N->setReadMarker()->setUnknownNodeMarker();
666 const Type *ArgTy = F->getFunctionType()->getParamType(0);
667 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
668 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
669 }
Chris Lattner68300db2004-02-13 20:05:32 +0000670 return;
Chris Lattner304e1432004-02-16 22:57:19 +0000671 } else if (CS.arg_end()-CS.arg_begin() == 1 &&
672 (F->getName() == "fflush" || F->getName() == "feof" ||
673 F->getName() == "fileno" || F->getName() == "clearerr" ||
Chris Lattner52fc8d72004-02-25 23:06:40 +0000674 F->getName() == "rewind" || F->getName() == "ftell" ||
Chris Lattner6b586df2004-02-27 20:04:48 +0000675 F->getName() == "ferror" || F->getName() == "fgetc" ||
676 F->getName() == "fgetc" || F->getName() == "_IO_getc")) {
Chris Lattner304e1432004-02-16 22:57:19 +0000677 // fflush reads and writes the memory for the file descriptor. It
Chris Lattner339d8df2004-02-13 21:21:48 +0000678 // merges the FILE type into the descriptor.
679 DSNodeHandle H = getValueDest(**CS.arg_begin());
Chris Lattnerd5612092004-02-24 22:02:48 +0000680 if (DSNode *N = H.getNode()) {
681 N->setReadMarker()->setModifiedMarker();
Chris Lattner339d8df2004-02-13 21:21:48 +0000682
Chris Lattnerd5612092004-02-24 22:02:48 +0000683 const Type *ArgTy = F->getFunctionType()->getParamType(0);
684 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
685 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
686 }
687 return;
688 } else if (CS.arg_end()-CS.arg_begin() == 4 &&
689 (F->getName() == "fwrite" || F->getName() == "fread")) {
690 // fread writes the first operand, fwrite reads it. They both
691 // read/write the FILE descriptor, and merges the FILE type.
Chris Lattnerfe781652004-12-08 16:22:26 +0000692 CallSite::arg_iterator compit = CS.arg_end();
693 DSNodeHandle H = getValueDest(**--compit);
Chris Lattnerd5612092004-02-24 22:02:48 +0000694 if (DSNode *N = H.getNode()) {
695 N->setReadMarker()->setModifiedMarker();
696 const Type *ArgTy = F->getFunctionType()->getParamType(3);
697 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
698 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
699 }
700
701 H = getValueDest(**CS.arg_begin());
702 if (DSNode *N = H.getNode())
703 if (F->getName() == "fwrite")
704 N->setReadMarker();
705 else
706 N->setModifiedMarker();
Chris Lattner339d8df2004-02-13 21:21:48 +0000707 return;
708 } else if (F->getName() == "fgets" && CS.arg_end()-CS.arg_begin() == 3){
Chris Lattner304e1432004-02-16 22:57:19 +0000709 // fgets reads and writes the memory for the file descriptor. It
Chris Lattner339d8df2004-02-13 21:21:48 +0000710 // merges the FILE type into the descriptor, and writes to the
711 // argument. It returns the argument as well.
712 CallSite::arg_iterator AI = CS.arg_begin();
713 DSNodeHandle H = getValueDest(**AI);
714 if (DSNode *N = H.getNode())
715 N->setModifiedMarker(); // Writes buffer
716 H.mergeWith(getValueDest(*CS.getInstruction())); // Returns buffer
717 ++AI; ++AI;
718
719 // Reads and writes file descriptor, merge in FILE type.
Chris Lattneradc1efe2004-02-25 17:43:20 +0000720 H = getValueDest(**AI);
Chris Lattnerd5612092004-02-24 22:02:48 +0000721 if (DSNode *N = H.getNode()) {
Chris Lattner339d8df2004-02-13 21:21:48 +0000722 N->setReadMarker()->setModifiedMarker();
Chris Lattneradc1efe2004-02-25 17:43:20 +0000723 const Type *ArgTy = F->getFunctionType()->getParamType(2);
724 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
725 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
726 }
727 return;
Chris Lattner52fc8d72004-02-25 23:06:40 +0000728 } else if (F->getName() == "ungetc" || F->getName() == "fputc" ||
729 F->getName() == "fputs" || F->getName() == "putc" ||
Chris Lattner6b586df2004-02-27 20:04:48 +0000730 F->getName() == "ftell" || F->getName() == "rewind" ||
731 F->getName() == "_IO_putc") {
732 // These functions read and write the memory for the file descriptor,
733 // which is passes as the last argument.
Chris Lattnerfe781652004-12-08 16:22:26 +0000734 CallSite::arg_iterator compit = CS.arg_end();
735 DSNodeHandle H = getValueDest(**--compit);
Chris Lattneradc1efe2004-02-25 17:43:20 +0000736 if (DSNode *N = H.getNode()) {
737 N->setReadMarker()->setModifiedMarker();
Chris Lattnerfe781652004-12-08 16:22:26 +0000738 FunctionType::param_iterator compit2 = F->getFunctionType()->param_end();
739 const Type *ArgTy = *--compit2;
Chris Lattnerd5612092004-02-24 22:02:48 +0000740 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
741 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
742 }
Chris Lattner52fc8d72004-02-25 23:06:40 +0000743
744 // Any pointer arguments are read.
745 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
746 AI != E; ++AI)
747 if (isPointerType((*AI)->getType()))
748 if (DSNode *N = getValueDest(**AI).getNode())
749 N->setReadMarker();
750 return;
751 } else if (F->getName() == "fseek" || F->getName() == "fgetpos" ||
752 F->getName() == "fsetpos") {
753 // These functions read and write the memory for the file descriptor,
754 // and read/write all other arguments.
755 DSNodeHandle H = getValueDest(**CS.arg_begin());
756 if (DSNode *N = H.getNode()) {
Chris Lattnerfe781652004-12-08 16:22:26 +0000757 FunctionType::param_iterator compit2 = F->getFunctionType()->param_end();
758 const Type *ArgTy = *--compit2;
Chris Lattner52fc8d72004-02-25 23:06:40 +0000759 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
760 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
761 }
762
763 // Any pointer arguments are read.
764 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
765 AI != E; ++AI)
766 if (isPointerType((*AI)->getType()))
767 if (DSNode *N = getValueDest(**AI).getNode())
768 N->setReadMarker()->setModifiedMarker();
Chris Lattner339d8df2004-02-13 21:21:48 +0000769 return;
Chris Lattner8ecc27e2004-02-20 20:27:11 +0000770 } else if (F->getName() == "printf" || F->getName() == "fprintf" ||
771 F->getName() == "sprintf") {
Chris Lattner339d8df2004-02-13 21:21:48 +0000772 CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
773
774 if (F->getName() == "fprintf") {
775 // fprintf reads and writes the FILE argument, and applies the type
776 // to it.
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 Lattner8ecc27e2004-02-20 20:27:11 +0000784 } else if (F->getName() == "sprintf") {
785 // sprintf writes the first string argument.
786 DSNodeHandle H = getValueDest(**AI++);
787 if (DSNode *N = H.getNode()) {
788 N->setModifiedMarker();
789 const Type *ArgTy = (*AI)->getType();
790 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
791 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
792 }
Chris Lattner339d8df2004-02-13 21:21:48 +0000793 }
794
795 for (; AI != E; ++AI) {
796 // printf reads all pointer arguments.
797 if (isPointerType((*AI)->getType()))
798 if (DSNode *N = getValueDest(**AI).getNode())
799 N->setReadMarker();
800 }
Chris Lattner4e46e322004-02-20 23:27:09 +0000801 return;
Chris Lattner3aeb40c2004-03-04 21:03:54 +0000802 } else if (F->getName() == "vprintf" || F->getName() == "vfprintf" ||
803 F->getName() == "vsprintf") {
804 CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
805
806 if (F->getName() == "vfprintf") {
807 // ffprintf reads and writes the FILE argument, and applies the type
808 // to it.
809 DSNodeHandle H = getValueDest(**AI);
810 if (DSNode *N = H.getNode()) {
811 N->setModifiedMarker()->setReadMarker();
812 const Type *ArgTy = (*AI)->getType();
813 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
814 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
815 }
816 ++AI;
817 } else if (F->getName() == "vsprintf") {
818 // vsprintf writes the first string argument.
819 DSNodeHandle H = getValueDest(**AI++);
820 if (DSNode *N = H.getNode()) {
821 N->setModifiedMarker();
822 const Type *ArgTy = (*AI)->getType();
823 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
824 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
825 }
826 }
827
828 // Read the format
829 if (AI != E) {
830 if (isPointerType((*AI)->getType()))
831 if (DSNode *N = getValueDest(**AI).getNode())
832 N->setReadMarker();
833 ++AI;
834 }
835
836 // Read the valist, and the pointed-to objects.
837 if (AI != E && isPointerType((*AI)->getType())) {
838 const DSNodeHandle &VAList = getValueDest(**AI);
839 if (DSNode *N = VAList.getNode()) {
840 N->setReadMarker();
841 N->mergeTypeInfo(PointerType::get(Type::SByteTy),
842 VAList.getOffset(), false);
843
844 DSNodeHandle &VAListObjs = getLink(VAList);
845 VAListObjs.getNode()->setReadMarker();
846 }
847 }
848
849 return;
Chris Lattner8ecc27e2004-02-20 20:27:11 +0000850 } else if (F->getName() == "scanf" || F->getName() == "fscanf" ||
851 F->getName() == "sscanf") {
852 CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
Chris Lattner339d8df2004-02-13 21:21:48 +0000853
Chris Lattner8ecc27e2004-02-20 20:27:11 +0000854 if (F->getName() == "fscanf") {
855 // fscanf reads and writes the FILE argument, and applies the type
856 // to it.
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 } else if (F->getName() == "sscanf") {
865 // sscanf reads the first string argument.
866 DSNodeHandle H = getValueDest(**AI++);
867 if (DSNode *N = H.getNode()) {
868 N->setReadMarker();
869 const Type *ArgTy = (*AI)->getType();
870 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
871 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
872 }
873 }
874
875 for (; AI != E; ++AI) {
876 // scanf writes all pointer arguments.
877 if (isPointerType((*AI)->getType()))
878 if (DSNode *N = getValueDest(**AI).getNode())
879 N->setModifiedMarker();
880 }
Chris Lattner4e46e322004-02-20 23:27:09 +0000881 return;
Chris Lattner8ecc27e2004-02-20 20:27:11 +0000882 } else if (F->getName() == "strtok") {
883 // strtok reads and writes the first argument, returning it. It reads
884 // its second arg. FIXME: strtok also modifies some hidden static
885 // data. Someday this might matter.
886 CallSite::arg_iterator AI = CS.arg_begin();
887 DSNodeHandle H = getValueDest(**AI++);
888 if (DSNode *N = H.getNode()) {
889 N->setReadMarker()->setModifiedMarker(); // Reads/Writes buffer
890 const Type *ArgTy = F->getFunctionType()->getParamType(0);
891 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
892 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
893 }
894 H.mergeWith(getValueDest(*CS.getInstruction())); // Returns buffer
895
896 H = getValueDest(**AI); // Reads delimiter
897 if (DSNode *N = H.getNode()) {
898 N->setReadMarker();
899 const Type *ArgTy = F->getFunctionType()->getParamType(1);
900 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
901 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
902 }
903 return;
Chris Lattner1fe98742004-02-26 03:43:08 +0000904 } else if (F->getName() == "strchr" || F->getName() == "strrchr" ||
905 F->getName() == "strstr") {
906 // These read their arguments, and return the first one
Chris Lattneradc1efe2004-02-25 17:43:20 +0000907 DSNodeHandle H = getValueDest(**CS.arg_begin());
Chris Lattner1fe98742004-02-26 03:43:08 +0000908 H.mergeWith(getValueDest(*CS.getInstruction())); // Returns buffer
909
910 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
911 AI != E; ++AI)
912 if (isPointerType((*AI)->getType()))
913 if (DSNode *N = getValueDest(**AI).getNode())
914 N->setReadMarker();
915
Chris Lattneradc1efe2004-02-25 17:43:20 +0000916 if (DSNode *N = H.getNode())
917 N->setReadMarker();
Chris Lattneradc1efe2004-02-25 17:43:20 +0000918 return;
Chris Lattnerbeacefa2004-11-08 21:08:28 +0000919 } else if (F->getName() == "__assert_fail") {
920 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
921 AI != E; ++AI)
922 if (isPointerType((*AI)->getType()))
923 if (DSNode *N = getValueDest(**AI).getNode())
924 N->setReadMarker();
925 return;
Chris Lattner52fc8d72004-02-25 23:06:40 +0000926 } else if (F->getName() == "modf" && CS.arg_end()-CS.arg_begin() == 2) {
927 // This writes its second argument, and forces it to double.
Chris Lattnerfe781652004-12-08 16:22:26 +0000928 CallSite::arg_iterator compit = CS.arg_end();
929 DSNodeHandle H = getValueDest(**--compit);
Chris Lattner52fc8d72004-02-25 23:06:40 +0000930 if (DSNode *N = H.getNode()) {
931 N->setModifiedMarker();
932 N->mergeTypeInfo(Type::DoubleTy, H.getOffset());
933 }
934 return;
Chris Lattner339d8df2004-02-13 21:21:48 +0000935 } else {
Chris Lattner304e1432004-02-16 22:57:19 +0000936 // Unknown function, warn if it returns a pointer type or takes a
937 // pointer argument.
938 bool Warn = isPointerType(CS.getInstruction()->getType());
939 if (!Warn)
940 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
941 I != E; ++I)
942 if (isPointerType((*I)->getType())) {
943 Warn = true;
944 break;
945 }
946 if (Warn)
947 std::cerr << "WARNING: Call to unknown external function '"
948 << F->getName() << "' will cause pessimistic results!\n";
Chris Lattnera07b72f2004-02-13 16:09:54 +0000949 }
Chris Lattner894263b2003-09-20 16:50:46 +0000950 }
951
952
Chris Lattnerc314ac42002-07-11 20:32:02 +0000953 // Set up the return value...
Chris Lattner0969c502002-10-21 02:08:03 +0000954 DSNodeHandle RetVal;
Chris Lattner808a7ae2003-09-20 16:34:13 +0000955 Instruction *I = CS.getInstruction();
956 if (isPointerType(I->getType()))
957 RetVal = getValueDest(*I);
Chris Lattnerc314ac42002-07-11 20:32:02 +0000958
Chris Lattnercb582402004-02-26 22:07:22 +0000959 DSNode *CalleeNode = 0;
960 if (DisableDirectCallOpt || !isa<Function>(Callee)) {
961 CalleeNode = getValueDest(*Callee).getNode();
962 if (CalleeNode == 0) {
963 std::cerr << "WARNING: Program is calling through a null pointer?\n"<< *I;
Chris Lattnerfbc2d842003-09-24 23:42:58 +0000964 return; // Calling a null pointer?
965 }
966 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000967
Chris Lattner0969c502002-10-21 02:08:03 +0000968 std::vector<DSNodeHandle> Args;
Chris Lattner808a7ae2003-09-20 16:34:13 +0000969 Args.reserve(CS.arg_end()-CS.arg_begin());
Chris Lattner0969c502002-10-21 02:08:03 +0000970
971 // Calculate the arguments vector...
Chris Lattner808a7ae2003-09-20 16:34:13 +0000972 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E; ++I)
973 if (isPointerType((*I)->getType()))
974 Args.push_back(getValueDest(**I));
Chris Lattner0969c502002-10-21 02:08:03 +0000975
976 // Add a new function call entry...
Chris Lattnercb582402004-02-26 22:07:22 +0000977 if (CalleeNode)
978 FunctionCalls->push_back(DSCallSite(CS, RetVal, CalleeNode, Args));
Chris Lattner923fc052003-02-05 21:59:58 +0000979 else
Chris Lattnercb582402004-02-26 22:07:22 +0000980 FunctionCalls->push_back(DSCallSite(CS, RetVal, cast<Function>(Callee),
Chris Lattner26c4fc32003-09-20 21:48:16 +0000981 Args));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000982}
Chris Lattner055dc2c2002-07-18 15:54:42 +0000983
Vikram S. Advebac06222002-12-06 21:17:10 +0000984void GraphBuilder::visitFreeInst(FreeInst &FI) {
Vikram S. Advebac06222002-12-06 21:17:10 +0000985 // Mark that the node is written to...
Chris Lattnerd5612092004-02-24 22:02:48 +0000986 if (DSNode *N = getValueDest(*FI.getOperand(0)).getNode())
987 N->setModifiedMarker()->setHeapNodeMarker();
Vikram S. Advebac06222002-12-06 21:17:10 +0000988}
989
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000990/// Handle casts...
991void GraphBuilder::visitCastInst(CastInst &CI) {
Chris Lattner5af344d2002-11-02 00:36:03 +0000992 if (isPointerType(CI.getType()))
993 if (isPointerType(CI.getOperand(0)->getType())) {
Chris Lattner157b2522004-10-06 19:29:13 +0000994 DSNodeHandle Ptr = getValueDest(*CI.getOperand(0));
995 if (Ptr.getNode() == 0) return;
996
Chris Lattner5af344d2002-11-02 00:36:03 +0000997 // Cast one pointer to the other, just act like a copy instruction
Chris Lattner157b2522004-10-06 19:29:13 +0000998 setDestTo(CI, Ptr);
Chris Lattner5af344d2002-11-02 00:36:03 +0000999 } else {
1000 // Cast something (floating point, small integer) to a pointer. We need
1001 // to track the fact that the node points to SOMETHING, just something we
1002 // don't know about. Make an "Unknown" node.
1003 //
Chris Lattnerbd92b732003-06-19 21:15:11 +00001004 setDestTo(CI, createNode()->setUnknownNodeMarker());
Chris Lattner5af344d2002-11-02 00:36:03 +00001005 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001006}
Chris Lattner055dc2c2002-07-18 15:54:42 +00001007
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001008
Chris Lattner878e5212003-02-04 00:59:50 +00001009// visitInstruction - For all other instruction types, if we have any arguments
1010// that are of pointer type, make them have unknown composition bits, and merge
1011// the nodes together.
1012void GraphBuilder::visitInstruction(Instruction &Inst) {
1013 DSNodeHandle CurNode;
1014 if (isPointerType(Inst.getType()))
1015 CurNode = getValueDest(Inst);
1016 for (User::op_iterator I = Inst.op_begin(), E = Inst.op_end(); I != E; ++I)
1017 if (isPointerType((*I)->getType()))
1018 CurNode.mergeWith(getValueDest(**I));
1019
Chris Lattner62c3a952004-10-30 04:22:45 +00001020 if (DSNode *N = CurNode.getNode())
1021 N->setUnknownNodeMarker();
Chris Lattner878e5212003-02-04 00:59:50 +00001022}
1023
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001024
1025
1026//===----------------------------------------------------------------------===//
1027// LocalDataStructures Implementation
1028//===----------------------------------------------------------------------===//
1029
Chris Lattner26c4fc32003-09-20 21:48:16 +00001030// MergeConstantInitIntoNode - Merge the specified constant into the node
1031// pointed to by NH.
1032void GraphBuilder::MergeConstantInitIntoNode(DSNodeHandle &NH, Constant *C) {
1033 // Ensure a type-record exists...
Chris Lattner62c3a952004-10-30 04:22:45 +00001034 DSNode *NHN = NH.getNode();
1035 NHN->mergeTypeInfo(C->getType(), NH.getOffset());
Chris Lattner26c4fc32003-09-20 21:48:16 +00001036
1037 if (C->getType()->isFirstClassType()) {
1038 if (isPointerType(C->getType()))
1039 // Avoid adding edges from null, or processing non-"pointer" stores
1040 NH.addEdgeTo(getValueDest(*C));
1041 return;
1042 }
Chris Lattner15869aa2003-11-02 22:27:28 +00001043
1044 const TargetData &TD = NH.getNode()->getTargetData();
1045
Chris Lattner26c4fc32003-09-20 21:48:16 +00001046 if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
1047 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1048 // We don't currently do any indexing for arrays...
1049 MergeConstantInitIntoNode(NH, cast<Constant>(CA->getOperand(i)));
1050 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
1051 const StructLayout *SL = TD.getStructLayout(CS->getType());
1052 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
Chris Lattner62c3a952004-10-30 04:22:45 +00001053 DSNode *NHN = NH.getNode();
Chris Lattner507bdf92005-01-12 04:51:37 +00001054 DSNodeHandle NewNH(NHN, NH.getOffset()+(unsigned)SL->MemberOffsets[i]);
Chris Lattner26c4fc32003-09-20 21:48:16 +00001055 MergeConstantInitIntoNode(NewNH, cast<Constant>(CS->getOperand(i)));
1056 }
Chris Lattner48b2f6b2004-10-26 16:23:03 +00001057 } else if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) {
Chris Lattner896481e2004-02-15 05:53:42 +00001058 // Noop
Chris Lattner26c4fc32003-09-20 21:48:16 +00001059 } else {
1060 assert(0 && "Unknown constant type!");
1061 }
1062}
1063
1064void GraphBuilder::mergeInGlobalInitializer(GlobalVariable *GV) {
1065 assert(!GV->isExternal() && "Cannot merge in external global!");
1066 // Get a node handle to the global node and merge the initializer into it.
1067 DSNodeHandle NH = getValueDest(*GV);
1068 MergeConstantInitIntoNode(NH, GV->getInitializer());
1069}
1070
1071
Chris Lattnerb12914b2004-09-20 04:48:05 +00001072bool LocalDataStructures::runOnModule(Module &M) {
Chris Lattner15869aa2003-11-02 22:27:28 +00001073 const TargetData &TD = getAnalysis<TargetData>();
Chris Lattneraa0b4682002-11-09 21:12:07 +00001074
Chris Lattnerf4f62272005-03-19 22:23:45 +00001075 // First step, build the globals graph.
1076 GlobalsGraph = new DSGraph(GlobalECs, TD);
Chris Lattnerc420ab62004-02-25 23:31:02 +00001077 {
1078 GraphBuilder GGB(*GlobalsGraph);
1079
Chris Lattnerf4f62272005-03-19 22:23:45 +00001080 // Add initializers for all of the globals to the globals graph.
1081 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
1082 I != E; ++I)
Chris Lattnerc420ab62004-02-25 23:31:02 +00001083 if (!I->isExternal())
1084 GGB.mergeInGlobalInitializer(I);
1085 }
1086
Chris Lattnerf4f62272005-03-19 22:23:45 +00001087 // Next step, iterate through the nodes in the globals graph, unioning
1088 // together the globals into equivalence classes.
1089 for (DSGraph::node_iterator I = GlobalsGraph->node_begin(),
1090 E = GlobalsGraph->node_end(); I != E; ++I)
1091 if (I->getGlobals().size() > 1) {
1092 // First, build up the equivalence set for this block of globals.
1093 const std::vector<GlobalValue*> &GVs = I->getGlobals();
1094 GlobalValue *First = GVs[0];
1095 for (unsigned i = 1, e = GVs.size(); i != e; ++i)
1096 GlobalECs.unionSets(First, GVs[i]);
1097
1098 // Next, get the leader element.
1099 First = GlobalECs.getLeaderValue(First);
1100
1101 // Next, remove all globals from the scalar map that are not the leader.
1102 DSScalarMap &SM = GlobalsGraph->getScalarMap();
1103 for (unsigned i = 0, e = GVs.size(); i != e; ++i)
1104 if (GVs[i] != First)
1105 SM.erase(SM.find(GVs[i]));
1106
1107 // Finally, change the global node to only contain the leader.
1108 I->clearGlobals();
1109 I->addGlobal(First);
1110 }
1111 DEBUG(GlobalsGraph->AssertGraphOK());
1112
Chris Lattneraa0b4682002-11-09 21:12:07 +00001113 // Calculate all of the graphs...
1114 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1115 if (!I->isExternal())
Chris Lattnerf4f62272005-03-19 22:23:45 +00001116 DSInfo.insert(std::make_pair(I, new DSGraph(GlobalECs, TD, *I,
1117 GlobalsGraph)));
Chris Lattner26c4fc32003-09-20 21:48:16 +00001118
Chris Lattnerc3f5f772004-02-08 01:51:48 +00001119 GlobalsGraph->removeTriviallyDeadNodes();
Chris Lattner26c4fc32003-09-20 21:48:16 +00001120 GlobalsGraph->markIncompleteNodes(DSGraph::MarkFormalArgs);
Chris Lattneraa0b4682002-11-09 21:12:07 +00001121 return false;
1122}
1123
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001124// releaseMemory - If the pass pipeline is done with this pass, we can release
1125// our memory... here...
1126//
1127void LocalDataStructures::releaseMemory() {
Chris Lattner81d924d2003-06-30 04:53:27 +00001128 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
1129 E = DSInfo.end(); I != E; ++I) {
1130 I->second->getReturnNodes().erase(I->first);
1131 if (I->second->getReturnNodes().empty())
1132 delete I->second;
1133 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001134
1135 // Empty map so next time memory is released, data structures are not
1136 // re-deleted.
1137 DSInfo.clear();
Chris Lattner2e4f9bf2002-11-09 20:01:01 +00001138 delete GlobalsGraph;
1139 GlobalsGraph = 0;
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001140}
Brian Gaeked0fde302003-11-11 22:41:34 +00001141