blob: 106f3a10345aa95bb03dddf0e4a9854a179689b6 [file] [log] [blame]
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001//===- Local.cpp - Compute a local data structure graph for a function ----===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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:
Misha Brukman2b37d7c2005-04-21 21:13:18 +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 Lattnera513fb12005-03-22 02:45:13 +000085 for (Function::arg_iterator I = f.arg_begin(), E = f.arg_end();
86 I != E; ++I)
Chris Lattnerfccd06f2002-10-01 22:33:50 +000087 if (isPointerType(I->getType()))
88 getValueDest(*I);
Chris Lattner0d9bab82002-07-18 00:12:30 +000089
Chris Lattner26c4fc32003-09-20 21:48:16 +000090 visit(f); // Single pass over the function
Chris Lattnerc68c31b2002-07-10 22:38:08 +000091 }
92
Chris Lattner26c4fc32003-09-20 21:48:16 +000093 // GraphBuilder ctor for working on the globals graph
94 GraphBuilder(DSGraph &g)
95 : G(g), RetNode(0), ScalarMap(G.getScalarMap()), FunctionCalls(0) {
96 }
97
98 void mergeInGlobalInitializer(GlobalVariable *GV);
99
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000100 private:
101 // Visitor functions, used to handle each instruction type we encounter...
102 friend class InstVisitor<GraphBuilder>;
Chris Lattnerbd92b732003-06-19 21:15:11 +0000103 void visitMallocInst(MallocInst &MI) { handleAlloc(MI, true); }
104 void visitAllocaInst(AllocaInst &AI) { handleAlloc(AI, false); }
105 void handleAlloc(AllocationInst &AI, bool isHeap);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000106
107 void visitPHINode(PHINode &PN);
Chris Lattner6e84bd72005-02-25 01:27:48 +0000108 void visitSelectInst(SelectInst &SI);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000109
Chris Lattner51340062002-11-08 05:00:44 +0000110 void visitGetElementPtrInst(User &GEP);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000111 void visitReturnInst(ReturnInst &RI);
112 void visitLoadInst(LoadInst &LI);
113 void visitStoreInst(StoreInst &SI);
114 void visitCallInst(CallInst &CI);
Chris Lattner808a7ae2003-09-20 16:34:13 +0000115 void visitInvokeInst(InvokeInst &II);
Chris Lattner32672652005-03-05 19:04:31 +0000116 void visitSetCondInst(SetCondInst &SCI);
Vikram S. Advebac06222002-12-06 21:17:10 +0000117 void visitFreeInst(FreeInst &FI);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000118 void visitCastInst(CastInst &CI);
Chris Lattner878e5212003-02-04 00:59:50 +0000119 void visitInstruction(Instruction &I);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000120
Chris Lattner808a7ae2003-09-20 16:34:13 +0000121 void visitCallSite(CallSite CS);
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000122 void visitVANextInst(VANextInst &I);
123 void visitVAArgInst(VAArgInst &I);
Chris Lattner26c4fc32003-09-20 21:48:16 +0000124
125 void MergeConstantInitIntoNode(DSNodeHandle &NH, Constant *C);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000126 private:
127 // Helper functions used to implement the visitation functions...
128
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000129 /// createNode - Create a new DSNode, ensuring that it is properly added to
130 /// the graph.
131 ///
Chris Lattnerbd92b732003-06-19 21:15:11 +0000132 DSNode *createNode(const Type *Ty = 0) {
133 DSNode *N = new DSNode(Ty, &G); // Create the node
Chris Lattnere158b192003-06-16 12:08:18 +0000134 if (DisableFieldSensitivity) {
Chris Lattner2412a052004-05-23 21:14:09 +0000135 // Create node handle referring to the old node so that it is
136 // immediately removed from the graph when the node handle is destroyed.
137 DSNodeHandle OldNNH = N;
Chris Lattnerca3f7902003-02-08 20:18:39 +0000138 N->foldNodeCompletely();
Chris Lattnere158b192003-06-16 12:08:18 +0000139 if (DSNode *FN = N->getForwardNode())
140 N = FN;
141 }
Chris Lattner92673292002-11-02 00:13:20 +0000142 return N;
143 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000144
Chris Lattnerc875f022002-11-03 21:27:48 +0000145 /// setDestTo - Set the ScalarMap entry for the specified value to point to
Chris Lattner92673292002-11-02 00:13:20 +0000146 /// the specified destination. If the Value already points to a node, make
147 /// sure to merge the two destinations together.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000148 ///
Chris Lattner92673292002-11-02 00:13:20 +0000149 void setDestTo(Value &V, const DSNodeHandle &NH);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000150
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000151 /// getValueDest - Return the DSNode that the actual value points to.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000152 ///
Chris Lattner92673292002-11-02 00:13:20 +0000153 DSNodeHandle getValueDest(Value &V);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000154
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000155 /// getLink - This method is used to return the specified link in the
156 /// specified node if one exists. If a link does not already exist (it's
Chris Lattner7e51c872002-10-31 06:52:26 +0000157 /// null), then we create a new node, link it, then return it.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000158 ///
Chris Lattner7e51c872002-10-31 06:52:26 +0000159 DSNodeHandle &getLink(const DSNodeHandle &Node, unsigned Link = 0);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000160 };
161}
162
Brian Gaeked0fde302003-11-11 22:41:34 +0000163using namespace DS;
164
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000165//===----------------------------------------------------------------------===//
166// DSGraph constructor - Simply use the GraphBuilder to construct the local
167// graph.
Chris Lattnerf4f62272005-03-19 22:23:45 +0000168DSGraph::DSGraph(EquivalenceClasses<GlobalValue*> &ECs, const TargetData &td,
169 Function &F, DSGraph *GG)
170 : GlobalsGraph(GG), ScalarMap(ECs), TD(td) {
Chris Lattner2a068862002-11-10 06:53:38 +0000171 PrintAuxCalls = false;
Chris Lattner30514192003-07-02 04:37:26 +0000172
173 DEBUG(std::cerr << " [Loc] Calculating graph for: " << F.getName() << "\n");
174
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000175 // Use the graph builder to construct the local version of the graph
Chris Lattner26c4fc32003-09-20 21:48:16 +0000176 GraphBuilder B(F, *this, ReturnNodes[&F], FunctionCalls);
Chris Lattner4fe34612002-11-18 21:44:19 +0000177#ifndef NDEBUG
178 Timer::addPeakMemoryMeasurement();
179#endif
Chris Lattner1a1a85d2003-02-14 04:55:58 +0000180
Chris Lattnerc420ab62004-02-25 23:31:02 +0000181 // If there are any constant globals referenced in this function, merge their
182 // initializers into the local graph from the globals graph.
183 if (ScalarMap.global_begin() != ScalarMap.global_end()) {
184 ReachabilityCloner RC(*this, *GG, 0);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000185
Chris Lattnerc420ab62004-02-25 23:31:02 +0000186 for (DSScalarMap::global_iterator I = ScalarMap.global_begin();
187 I != ScalarMap.global_end(); ++I)
188 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I))
Chris Lattner76a9eb32004-03-03 23:00:19 +0000189 if (!GV->isExternal() && GV->isConstant())
Chris Lattnerc420ab62004-02-25 23:31:02 +0000190 RC.merge(ScalarMap[GV], GG->ScalarMap[GV]);
191 }
192
Chris Lattner394471f2003-01-23 22:05:33 +0000193 markIncompleteNodes(DSGraph::MarkFormalArgs);
Chris Lattner96517252002-11-09 20:55:24 +0000194
195 // Remove any nodes made dead due to merging...
Chris Lattner394471f2003-01-23 22:05:33 +0000196 removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000197}
198
199
200//===----------------------------------------------------------------------===//
201// Helper method implementations...
202//
203
Chris Lattner92673292002-11-02 00:13:20 +0000204/// getValueDest - Return the DSNode that the actual value points to.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000205///
Chris Lattner51340062002-11-08 05:00:44 +0000206DSNodeHandle GraphBuilder::getValueDest(Value &Val) {
207 Value *V = &Val;
Chris Lattner82962de2004-11-03 18:51:26 +0000208 if (isa<Constant>(V) && cast<Constant>(V)->isNullValue())
Chris Lattner51340062002-11-08 05:00:44 +0000209 return 0; // Null doesn't point to anything, don't add to ScalarMap!
Chris Lattner92673292002-11-02 00:13:20 +0000210
Vikram S. Advebac06222002-12-06 21:17:10 +0000211 DSNodeHandle &NH = ScalarMap[V];
Chris Lattner62c3a952004-10-30 04:22:45 +0000212 if (!NH.isNull())
Vikram S. Advebac06222002-12-06 21:17:10 +0000213 return NH; // Already have a node? Just return it...
214
215 // Otherwise we need to create a new node to point to.
216 // Check first for constant expressions that must be traversed to
217 // extract the actual value.
Reid Spencere8404342004-07-18 00:18:30 +0000218 DSNode* N;
219 if (GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner48427b52005-03-20 01:18:00 +0000220 // Create a new global node for this global variable.
Reid Spencere8404342004-07-18 00:18:30 +0000221 N = createNode(GV->getType()->getElementType());
222 N->addGlobal(GV);
223 } else if (Constant *C = dyn_cast<Constant>(V)) {
224 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnera513fb12005-03-22 02:45:13 +0000225 if (CE->getOpcode() == Instruction::Cast) {
226 if (isa<PointerType>(CE->getOperand(0)->getType()))
227 NH = getValueDest(*CE->getOperand(0));
228 else
229 NH = createNode()->setUnknownNodeMarker();
230 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
Chris Lattner51340062002-11-08 05:00:44 +0000231 visitGetElementPtrInst(*CE);
Chris Lattner62482e52004-01-28 09:15:42 +0000232 DSScalarMap::iterator I = ScalarMap.find(CE);
Chris Lattner2cfbaaf2002-11-09 20:14:03 +0000233 assert(I != ScalarMap.end() && "GEP didn't get processed right?");
Chris Lattner1e56c542003-02-09 23:04:12 +0000234 NH = I->second;
235 } else {
236 // This returns a conservative unknown node for any unhandled ConstExpr
Chris Lattnerbd92b732003-06-19 21:15:11 +0000237 return NH = createNode()->setUnknownNodeMarker();
Chris Lattner51340062002-11-08 05:00:44 +0000238 }
Chris Lattner62c3a952004-10-30 04:22:45 +0000239 if (NH.isNull()) { // (getelementptr null, X) returns null
Chris Lattner1e56c542003-02-09 23:04:12 +0000240 ScalarMap.erase(V);
241 return 0;
242 }
243 return NH;
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000244 } else if (isa<UndefValue>(C)) {
245 ScalarMap.erase(V);
246 return 0;
Chris Lattner51340062002-11-08 05:00:44 +0000247 } else {
248 assert(0 && "Unknown constant type!");
249 }
Reid Spencere8404342004-07-18 00:18:30 +0000250 N = createNode(); // just create a shadow node
Chris Lattner92673292002-11-02 00:13:20 +0000251 } else {
252 // Otherwise just create a shadow node
Chris Lattnerbd92b732003-06-19 21:15:11 +0000253 N = createNode();
Chris Lattner92673292002-11-02 00:13:20 +0000254 }
255
Chris Lattnerefffdc92004-07-07 06:12:52 +0000256 NH.setTo(N, 0); // Remember that we are pointing to it...
Chris Lattner92673292002-11-02 00:13:20 +0000257 return NH;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000258}
259
Chris Lattner0d9bab82002-07-18 00:12:30 +0000260
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000261/// getLink - This method is used to return the specified link in the
262/// specified node if one exists. If a link does not already exist (it's
263/// null), then we create a new node, link it, then return it. We must
264/// specify the type of the Node field we are accessing so that we know what
265/// type should be linked to if we need to create a new node.
266///
Chris Lattner7e51c872002-10-31 06:52:26 +0000267DSNodeHandle &GraphBuilder::getLink(const DSNodeHandle &node, unsigned LinkNo) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000268 DSNodeHandle &Node = const_cast<DSNodeHandle&>(node);
Chris Lattner08db7192002-11-06 06:20:27 +0000269 DSNodeHandle &Link = Node.getLink(LinkNo);
Chris Lattner62c3a952004-10-30 04:22:45 +0000270 if (Link.isNull()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000271 // If the link hasn't been created yet, make and return a new shadow node
Chris Lattnerbd92b732003-06-19 21:15:11 +0000272 Link = createNode();
Chris Lattner08db7192002-11-06 06:20:27 +0000273 }
274 return Link;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000275}
276
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000277
Chris Lattnerc875f022002-11-03 21:27:48 +0000278/// setDestTo - Set the ScalarMap entry for the specified value to point to the
Chris Lattner92673292002-11-02 00:13:20 +0000279/// specified destination. If the Value already points to a node, make sure to
280/// merge the two destinations together.
281///
282void GraphBuilder::setDestTo(Value &V, const DSNodeHandle &NH) {
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000283 ScalarMap[&V].mergeWith(NH);
Chris Lattner92673292002-11-02 00:13:20 +0000284}
285
286
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000287//===----------------------------------------------------------------------===//
288// Specific instruction type handler implementations...
289//
290
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000291/// Alloca & Malloc instruction implementation - Simply create a new memory
292/// object, pointing the scalar to it.
293///
Chris Lattnerbd92b732003-06-19 21:15:11 +0000294void GraphBuilder::handleAlloc(AllocationInst &AI, bool isHeap) {
295 DSNode *N = createNode();
296 if (isHeap)
297 N->setHeapNodeMarker();
298 else
299 N->setAllocaNodeMarker();
300 setDestTo(AI, N);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000301}
302
303// PHINode - Make the scalar for the PHI node point to all of the things the
304// incoming values point to... which effectively causes them to be merged.
305//
306void GraphBuilder::visitPHINode(PHINode &PN) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000307 if (!isPointerType(PN.getType())) return; // Only pointer PHIs
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000308
Chris Lattnerc875f022002-11-03 21:27:48 +0000309 DSNodeHandle &PNDest = ScalarMap[&PN];
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000310 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
Chris Lattner92673292002-11-02 00:13:20 +0000311 PNDest.mergeWith(getValueDest(*PN.getIncomingValue(i)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000312}
313
Chris Lattner6e84bd72005-02-25 01:27:48 +0000314void GraphBuilder::visitSelectInst(SelectInst &SI) {
315 if (!isPointerType(SI.getType())) return; // Only pointer Selects
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000316
Chris Lattner6e84bd72005-02-25 01:27:48 +0000317 DSNodeHandle &Dest = ScalarMap[&SI];
318 Dest.mergeWith(getValueDest(*SI.getOperand(1)));
319 Dest.mergeWith(getValueDest(*SI.getOperand(2)));
320}
321
Chris Lattner32672652005-03-05 19:04:31 +0000322void GraphBuilder::visitSetCondInst(SetCondInst &SCI) {
323 if (!isPointerType(SCI.getOperand(0)->getType()) ||
324 isa<ConstantPointerNull>(SCI.getOperand(1))) return; // Only pointers
325 ScalarMap[SCI.getOperand(0)].mergeWith(getValueDest(*SCI.getOperand(1)));
326}
327
328
Chris Lattner51340062002-11-08 05:00:44 +0000329void GraphBuilder::visitGetElementPtrInst(User &GEP) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000330 DSNodeHandle Value = getValueDest(*GEP.getOperand(0));
Chris Lattner753b1132005-02-24 19:55:31 +0000331 if (Value.isNull())
332 Value = createNode();
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000333
Chris Lattner179bc7d2003-11-14 17:09:46 +0000334 // As a special case, if all of the index operands of GEP are constant zeros,
335 // handle this just like we handle casts (ie, don't do much).
336 bool AllZeros = true;
337 for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i)
338 if (GEP.getOperand(i) !=
339 Constant::getNullValue(GEP.getOperand(i)->getType())) {
340 AllZeros = false;
341 break;
342 }
343
344 // If all of the indices are zero, the result points to the operand without
345 // applying the type.
Chris Lattner0c9707a2005-03-18 23:18:20 +0000346 if (AllZeros || (!Value.isNull() &&
347 Value.getNode()->isNodeCompletelyFolded())) {
Chris Lattner179bc7d2003-11-14 17:09:46 +0000348 setDestTo(GEP, Value);
349 return;
350 }
351
352
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000353 const PointerType *PTy = cast<PointerType>(GEP.getOperand(0)->getType());
354 const Type *CurTy = PTy->getElementType();
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000355
Chris Lattner08db7192002-11-06 06:20:27 +0000356 if (Value.getNode()->mergeTypeInfo(CurTy, Value.getOffset())) {
357 // If the node had to be folded... exit quickly
Chris Lattner92673292002-11-02 00:13:20 +0000358 setDestTo(GEP, Value); // GEP result points to folded node
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000359 return;
360 }
361
Chris Lattner15869aa2003-11-02 22:27:28 +0000362 const TargetData &TD = Value.getNode()->getTargetData();
363
Chris Lattner08db7192002-11-06 06:20:27 +0000364#if 0
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000365 // Handle the pointer index specially...
366 if (GEP.getNumOperands() > 1 &&
Chris Lattner28977af2004-04-05 01:30:19 +0000367 (!isa<Constant>(GEP.getOperand(1)) ||
368 !cast<Constant>(GEP.getOperand(1))->isNullValue())) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000369
370 // If we already know this is an array being accessed, don't do anything...
371 if (!TopTypeRec.isArray) {
372 TopTypeRec.isArray = true;
373
374 // If we are treating some inner field pointer as an array, fold the node
375 // up because we cannot handle it right. This can come because of
376 // something like this: &((&Pt->X)[1]) == &Pt->Y
377 //
378 if (Value.getOffset()) {
379 // Value is now the pointer we want to GEP to be...
380 Value.getNode()->foldNodeCompletely();
Chris Lattner92673292002-11-02 00:13:20 +0000381 setDestTo(GEP, Value); // GEP result points to folded node
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000382 return;
383 } else {
384 // This is a pointer to the first byte of the node. Make sure that we
385 // are pointing to the outter most type in the node.
386 // FIXME: We need to check one more case here...
387 }
388 }
389 }
Chris Lattner08db7192002-11-06 06:20:27 +0000390#endif
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000391
392 // All of these subscripts are indexing INTO the elements we have...
Chris Lattner26c4fc32003-09-20 21:48:16 +0000393 unsigned Offset = 0;
Chris Lattnerfa3711a2003-11-25 20:19:55 +0000394 for (gep_type_iterator I = gep_type_begin(GEP), E = gep_type_end(GEP);
395 I != E; ++I)
396 if (const StructType *STy = dyn_cast<StructType>(*I)) {
Chris Lattner507bdf92005-01-12 04:51:37 +0000397 unsigned FieldNo =
398 (unsigned)cast<ConstantUInt>(I.getOperand())->getValue();
399 Offset += (unsigned)TD.getStructLayout(STy)->MemberOffsets[FieldNo];
Chris Lattner82e9d722004-03-01 19:02:54 +0000400 } else if (const PointerType *PTy = dyn_cast<PointerType>(*I)) {
401 if (!isa<Constant>(I.getOperand()) ||
402 !cast<Constant>(I.getOperand())->isNullValue())
403 Value.getNode()->setArrayMarker();
Chris Lattnerfa3711a2003-11-25 20:19:55 +0000404 }
405
406
Chris Lattner08db7192002-11-06 06:20:27 +0000407#if 0
Chris Lattnerfa3711a2003-11-25 20:19:55 +0000408 if (const SequentialType *STy = cast<SequentialType>(*I)) {
409 CurTy = STy->getElementType();
Chris Lattnere03f32b2002-10-02 06:24:36 +0000410 if (ConstantSInt *CS = dyn_cast<ConstantSInt>(GEP.getOperand(i))) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000411 Offset += CS->getValue()*TD.getTypeSize(CurTy);
Chris Lattnere03f32b2002-10-02 06:24:36 +0000412 } else {
413 // Variable index into a node. We must merge all of the elements of the
414 // sequential type here.
415 if (isa<PointerType>(STy))
416 std::cerr << "Pointer indexing not handled yet!\n";
417 else {
418 const ArrayType *ATy = cast<ArrayType>(STy);
419 unsigned ElSize = TD.getTypeSize(CurTy);
420 DSNode *N = Value.getNode();
421 assert(N && "Value must have a node!");
422 unsigned RawOffset = Offset+Value.getOffset();
423
424 // Loop over all of the elements of the array, merging them into the
Misha Brukman2f2d0652003-09-11 18:14:24 +0000425 // zeroth element.
Chris Lattnere03f32b2002-10-02 06:24:36 +0000426 for (unsigned i = 1, e = ATy->getNumElements(); i != e; ++i)
427 // Merge all of the byte components of this array element
428 for (unsigned j = 0; j != ElSize; ++j)
429 N->mergeIndexes(RawOffset+j, RawOffset+i*ElSize+j);
430 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000431 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000432 }
Chris Lattnerfa3711a2003-11-25 20:19:55 +0000433#endif
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000434
435 // Add in the offset calculated...
436 Value.setOffset(Value.getOffset()+Offset);
437
438 // Value is now the pointer we want to GEP to be...
Chris Lattner92673292002-11-02 00:13:20 +0000439 setDestTo(GEP, Value);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000440}
441
442void GraphBuilder::visitLoadInst(LoadInst &LI) {
Chris Lattner92673292002-11-02 00:13:20 +0000443 DSNodeHandle Ptr = getValueDest(*LI.getOperand(0));
Chris Lattner6e84bd72005-02-25 01:27:48 +0000444 if (Ptr.isNull())
445 Ptr = createNode();
Chris Lattner92673292002-11-02 00:13:20 +0000446
447 // Make that the node is read from...
Chris Lattnerbd92b732003-06-19 21:15:11 +0000448 Ptr.getNode()->setReadMarker();
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000449
450 // Ensure a typerecord exists...
Chris Lattner088b6392003-03-03 17:13:31 +0000451 Ptr.getNode()->mergeTypeInfo(LI.getType(), Ptr.getOffset(), false);
Chris Lattner92673292002-11-02 00:13:20 +0000452
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000453 if (isPointerType(LI.getType()))
Chris Lattner92673292002-11-02 00:13:20 +0000454 setDestTo(LI, getLink(Ptr));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000455}
456
457void GraphBuilder::visitStoreInst(StoreInst &SI) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000458 const Type *StoredTy = SI.getOperand(0)->getType();
Chris Lattner92673292002-11-02 00:13:20 +0000459 DSNodeHandle Dest = getValueDest(*SI.getOperand(1));
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000460 if (Dest.isNull()) return;
Chris Lattner92673292002-11-02 00:13:20 +0000461
Vikram S. Advebac06222002-12-06 21:17:10 +0000462 // Mark that the node is written to...
Chris Lattnerbd92b732003-06-19 21:15:11 +0000463 Dest.getNode()->setModifiedMarker();
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000464
Chris Lattner26c4fc32003-09-20 21:48:16 +0000465 // Ensure a type-record exists...
Chris Lattner08db7192002-11-06 06:20:27 +0000466 Dest.getNode()->mergeTypeInfo(StoredTy, Dest.getOffset());
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000467
468 // Avoid adding edges from null, or processing non-"pointer" stores
Chris Lattner92673292002-11-02 00:13:20 +0000469 if (isPointerType(StoredTy))
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000470 Dest.addEdgeTo(getValueDest(*SI.getOperand(0)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000471}
472
473void GraphBuilder::visitReturnInst(ReturnInst &RI) {
Chris Lattner92673292002-11-02 00:13:20 +0000474 if (RI.getNumOperands() && isPointerType(RI.getOperand(0)->getType()))
Chris Lattner26c4fc32003-09-20 21:48:16 +0000475 RetNode->mergeWith(getValueDest(*RI.getOperand(0)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000476}
477
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000478void GraphBuilder::visitVANextInst(VANextInst &I) {
479 getValueDest(*I.getOperand(0)).mergeWith(getValueDest(I));
480}
481
482void GraphBuilder::visitVAArgInst(VAArgInst &I) {
483 DSNodeHandle Ptr = getValueDest(*I.getOperand(0));
484 if (Ptr.isNull()) return;
485
486 // Make that the node is read from.
487 Ptr.getNode()->setReadMarker();
488
Chris Lattner62c3a952004-10-30 04:22:45 +0000489 // Ensure a type record exists.
490 DSNode *PtrN = Ptr.getNode();
491 PtrN->mergeTypeInfo(I.getType(), Ptr.getOffset(), false);
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000492
493 if (isPointerType(I.getType()))
494 setDestTo(I, getLink(Ptr));
495}
496
497
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000498void GraphBuilder::visitCallInst(CallInst &CI) {
Chris Lattner808a7ae2003-09-20 16:34:13 +0000499 visitCallSite(&CI);
500}
501
502void GraphBuilder::visitInvokeInst(InvokeInst &II) {
503 visitCallSite(&II);
504}
505
506void GraphBuilder::visitCallSite(CallSite CS) {
Chris Lattnercb582402004-02-26 22:07:22 +0000507 Value *Callee = CS.getCalledValue();
Chris Lattnercb582402004-02-26 22:07:22 +0000508
Chris Lattner894263b2003-09-20 16:50:46 +0000509 // Special case handling of certain libc allocation functions here.
Chris Lattnercb582402004-02-26 22:07:22 +0000510 if (Function *F = dyn_cast<Function>(Callee))
Chris Lattner894263b2003-09-20 16:50:46 +0000511 if (F->isExternal())
Chris Lattnera07b72f2004-02-13 16:09:54 +0000512 switch (F->getIntrinsicID()) {
Chris Lattner317201d2004-03-13 00:24:00 +0000513 case Intrinsic::vastart:
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000514 getValueDest(*CS.getInstruction()).getNode()->setAllocaNodeMarker();
515 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000516 case Intrinsic::vacopy:
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000517 getValueDest(*CS.getInstruction()).
518 mergeWith(getValueDest(**(CS.arg_begin())));
519 return;
Chris Lattner317201d2004-03-13 00:24:00 +0000520 case Intrinsic::vaend:
Chris Lattner6b3e3cc2004-03-04 20:33:47 +0000521 return; // noop
Chris Lattnera07b72f2004-02-13 16:09:54 +0000522 case Intrinsic::memmove:
523 case Intrinsic::memcpy: {
524 // Merge the first & second arguments, and mark the memory read and
Chris Lattnerfb8c6102003-11-08 21:55:50 +0000525 // modified.
Chris Lattnera07b72f2004-02-13 16:09:54 +0000526 DSNodeHandle RetNH = getValueDest(**CS.arg_begin());
Chris Lattner67ce57a2003-11-09 03:32:52 +0000527 RetNH.mergeWith(getValueDest(**(CS.arg_begin()+1)));
528 if (DSNode *N = RetNH.getNode())
529 N->setModifiedMarker()->setReadMarker();
530 return;
Chris Lattnera07b72f2004-02-13 16:09:54 +0000531 }
Chris Lattnereee33b22004-02-16 18:37:40 +0000532 case Intrinsic::memset:
533 // Mark the memory modified.
534 if (DSNode *N = getValueDest(**CS.arg_begin()).getNode())
535 N->setModifiedMarker();
536 return;
Chris Lattnera07b72f2004-02-13 16:09:54 +0000537 default:
Vikram S. Advee4e97ef2004-05-25 08:14:52 +0000538 if (F->getName() == "calloc" || F->getName() == "posix_memalign" ||
539 F->getName() == "memalign" || F->getName() == "valloc") {
Chris Lattnera07b72f2004-02-13 16:09:54 +0000540 setDestTo(*CS.getInstruction(),
541 createNode()->setHeapNodeMarker()->setModifiedMarker());
542 return;
543 } else if (F->getName() == "realloc") {
544 DSNodeHandle RetNH = getValueDest(*CS.getInstruction());
Chris Lattner82962de2004-11-03 18:51:26 +0000545 if (CS.arg_begin() != CS.arg_end())
546 RetNH.mergeWith(getValueDest(**CS.arg_begin()));
Chris Lattnera07b72f2004-02-13 16:09:54 +0000547 if (DSNode *N = RetNH.getNode())
548 N->setHeapNodeMarker()->setModifiedMarker()->setReadMarker();
549 return;
Chris Lattner1fe98742004-02-26 03:43:08 +0000550 } else if (F->getName() == "memmove") {
551 // Merge the first & second arguments, and mark the memory read and
552 // modified.
553 DSNodeHandle RetNH = getValueDest(**CS.arg_begin());
554 RetNH.mergeWith(getValueDest(**(CS.arg_begin()+1)));
555 if (DSNode *N = RetNH.getNode())
556 N->setModifiedMarker()->setReadMarker();
557 return;
558
Chris Lattnerd5612092004-02-24 22:02:48 +0000559 } else if (F->getName() == "atoi" || F->getName() == "atof" ||
Chris Lattneradc1efe2004-02-25 17:43:20 +0000560 F->getName() == "atol" || F->getName() == "atoll" ||
Chris Lattner39bb2dc2004-02-24 22:17:00 +0000561 F->getName() == "remove" || F->getName() == "unlink" ||
Chris Lattneradc1efe2004-02-25 17:43:20 +0000562 F->getName() == "rename" || F->getName() == "memcmp" ||
563 F->getName() == "strcmp" || F->getName() == "strncmp" ||
564 F->getName() == "execl" || F->getName() == "execlp" ||
565 F->getName() == "execle" || F->getName() == "execv" ||
Chris Lattner52fc8d72004-02-25 23:06:40 +0000566 F->getName() == "execvp" || F->getName() == "chmod" ||
567 F->getName() == "puts" || F->getName() == "write" ||
568 F->getName() == "open" || F->getName() == "create" ||
569 F->getName() == "truncate" || F->getName() == "chdir" ||
570 F->getName() == "mkdir" || F->getName() == "rmdir") {
Chris Lattner39bb2dc2004-02-24 22:17:00 +0000571 // These functions read all of their pointer operands.
572 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
573 AI != E; ++AI) {
574 if (isPointerType((*AI)->getType()))
575 if (DSNode *N = getValueDest(**AI).getNode())
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000576 N->setReadMarker();
Chris Lattner39bb2dc2004-02-24 22:17:00 +0000577 }
Chris Lattner304e1432004-02-16 22:57:19 +0000578 return;
Chris Lattner52fc8d72004-02-25 23:06:40 +0000579 } else if (F->getName() == "read" || F->getName() == "pipe" ||
Chris Lattner6b586df2004-02-27 20:04:48 +0000580 F->getName() == "wait" || F->getName() == "time") {
Chris Lattner52fc8d72004-02-25 23:06:40 +0000581 // These functions write all of their pointer operands.
582 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
583 AI != E; ++AI) {
584 if (isPointerType((*AI)->getType()))
585 if (DSNode *N = getValueDest(**AI).getNode())
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000586 N->setModifiedMarker();
Chris Lattner52fc8d72004-02-25 23:06:40 +0000587 }
588 return;
Chris Lattneradc1efe2004-02-25 17:43:20 +0000589 } else if (F->getName() == "stat" || F->getName() == "fstat" ||
590 F->getName() == "lstat") {
591 // These functions read their first operand if its a pointer.
592 CallSite::arg_iterator AI = CS.arg_begin();
593 if (isPointerType((*AI)->getType())) {
594 DSNodeHandle Path = getValueDest(**AI);
595 if (DSNode *N = Path.getNode()) N->setReadMarker();
596 }
Chris Lattner304e1432004-02-16 22:57:19 +0000597
Chris Lattneradc1efe2004-02-25 17:43:20 +0000598 // Then they write into the stat buffer.
599 DSNodeHandle StatBuf = getValueDest(**++AI);
600 if (DSNode *N = StatBuf.getNode()) {
601 N->setModifiedMarker();
602 const Type *StatTy = F->getFunctionType()->getParamType(1);
603 if (const PointerType *PTy = dyn_cast<PointerType>(StatTy))
604 N->mergeTypeInfo(PTy->getElementType(), StatBuf.getOffset());
605 }
Chris Lattneradc1efe2004-02-25 17:43:20 +0000606 return;
Chris Lattner3aeb40c2004-03-04 21:03:54 +0000607 } else if (F->getName() == "strtod" || F->getName() == "strtof" ||
608 F->getName() == "strtold") {
609 // These functions read the first pointer
610 if (DSNode *Str = getValueDest(**CS.arg_begin()).getNode()) {
611 Str->setReadMarker();
612 // If the second parameter is passed, it will point to the first
613 // argument node.
614 const DSNodeHandle &EndPtrNH = getValueDest(**(CS.arg_begin()+1));
615 if (DSNode *End = EndPtrNH.getNode()) {
616 End->mergeTypeInfo(PointerType::get(Type::SByteTy),
617 EndPtrNH.getOffset(), false);
618 End->setModifiedMarker();
619 DSNodeHandle &Link = getLink(EndPtrNH);
620 Link.mergeWith(getValueDest(**CS.arg_begin()));
621 }
622 }
623
624 return;
Chris Lattner6b586df2004-02-27 20:04:48 +0000625 } else if (F->getName() == "fopen" || F->getName() == "fdopen" ||
626 F->getName() == "freopen") {
627 // These functions read all of their pointer operands.
628 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
629 AI != E; ++AI)
630 if (isPointerType((*AI)->getType()))
631 if (DSNode *N = getValueDest(**AI).getNode())
632 N->setReadMarker();
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000633
Chris Lattner68300db2004-02-13 20:05:32 +0000634 // fopen allocates in an unknown way and writes to the file
635 // descriptor. Also, merge the allocated type into the node.
636 DSNodeHandle Result = getValueDest(*CS.getInstruction());
Chris Lattnerd5612092004-02-24 22:02:48 +0000637 if (DSNode *N = Result.getNode()) {
638 N->setModifiedMarker()->setUnknownNodeMarker();
639 const Type *RetTy = F->getFunctionType()->getReturnType();
640 if (const PointerType *PTy = dyn_cast<PointerType>(RetTy))
641 N->mergeTypeInfo(PTy->getElementType(), Result.getOffset());
642 }
Chris Lattner6b586df2004-02-27 20:04:48 +0000643
644 // If this is freopen, merge the file descriptor passed in with the
645 // result.
Chris Lattnerfe781652004-12-08 16:22:26 +0000646 if (F->getName() == "freopen") {
647 // ICC doesn't handle getting the iterator, decrementing and
648 // dereferencing it in one operation without error. Do it in 2 steps
649 CallSite::arg_iterator compit = CS.arg_end();
650 Result.mergeWith(getValueDest(**--compit));
651 }
Chris Lattnerd5612092004-02-24 22:02:48 +0000652 return;
Chris Lattner68300db2004-02-13 20:05:32 +0000653 } else if (F->getName() == "fclose" && CS.arg_end()-CS.arg_begin() ==1){
654 // fclose reads and deallocates the memory in an unknown way for the
655 // file descriptor. It merges the FILE type into the descriptor.
656 DSNodeHandle H = getValueDest(**CS.arg_begin());
Chris Lattnerd5612092004-02-24 22:02:48 +0000657 if (DSNode *N = H.getNode()) {
658 N->setReadMarker()->setUnknownNodeMarker();
659 const Type *ArgTy = F->getFunctionType()->getParamType(0);
660 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
661 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
662 }
Chris Lattner68300db2004-02-13 20:05:32 +0000663 return;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000664 } else if (CS.arg_end()-CS.arg_begin() == 1 &&
Chris Lattner304e1432004-02-16 22:57:19 +0000665 (F->getName() == "fflush" || F->getName() == "feof" ||
666 F->getName() == "fileno" || F->getName() == "clearerr" ||
Chris Lattner52fc8d72004-02-25 23:06:40 +0000667 F->getName() == "rewind" || F->getName() == "ftell" ||
Chris Lattner6b586df2004-02-27 20:04:48 +0000668 F->getName() == "ferror" || F->getName() == "fgetc" ||
669 F->getName() == "fgetc" || F->getName() == "_IO_getc")) {
Chris Lattner304e1432004-02-16 22:57:19 +0000670 // fflush reads and writes the memory for the file descriptor. It
Chris Lattner339d8df2004-02-13 21:21:48 +0000671 // merges the FILE type into the descriptor.
672 DSNodeHandle H = getValueDest(**CS.arg_begin());
Chris Lattnerd5612092004-02-24 22:02:48 +0000673 if (DSNode *N = H.getNode()) {
674 N->setReadMarker()->setModifiedMarker();
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000675
Chris Lattnerd5612092004-02-24 22:02:48 +0000676 const Type *ArgTy = F->getFunctionType()->getParamType(0);
677 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
678 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
679 }
680 return;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000681 } else if (CS.arg_end()-CS.arg_begin() == 4 &&
Chris Lattnerd5612092004-02-24 22:02:48 +0000682 (F->getName() == "fwrite" || F->getName() == "fread")) {
683 // fread writes the first operand, fwrite reads it. They both
684 // read/write the FILE descriptor, and merges the FILE type.
Chris Lattnerfe781652004-12-08 16:22:26 +0000685 CallSite::arg_iterator compit = CS.arg_end();
686 DSNodeHandle H = getValueDest(**--compit);
Chris Lattnerd5612092004-02-24 22:02:48 +0000687 if (DSNode *N = H.getNode()) {
688 N->setReadMarker()->setModifiedMarker();
689 const Type *ArgTy = F->getFunctionType()->getParamType(3);
690 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
691 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
692 }
693
694 H = getValueDest(**CS.arg_begin());
695 if (DSNode *N = H.getNode())
696 if (F->getName() == "fwrite")
697 N->setReadMarker();
698 else
699 N->setModifiedMarker();
Chris Lattner339d8df2004-02-13 21:21:48 +0000700 return;
701 } else if (F->getName() == "fgets" && CS.arg_end()-CS.arg_begin() == 3){
Chris Lattner304e1432004-02-16 22:57:19 +0000702 // fgets reads and writes the memory for the file descriptor. It
Chris Lattner339d8df2004-02-13 21:21:48 +0000703 // merges the FILE type into the descriptor, and writes to the
704 // argument. It returns the argument as well.
705 CallSite::arg_iterator AI = CS.arg_begin();
706 DSNodeHandle H = getValueDest(**AI);
707 if (DSNode *N = H.getNode())
708 N->setModifiedMarker(); // Writes buffer
709 H.mergeWith(getValueDest(*CS.getInstruction())); // Returns buffer
710 ++AI; ++AI;
711
712 // Reads and writes file descriptor, merge in FILE type.
Chris Lattneradc1efe2004-02-25 17:43:20 +0000713 H = getValueDest(**AI);
Chris Lattnerd5612092004-02-24 22:02:48 +0000714 if (DSNode *N = H.getNode()) {
Chris Lattner339d8df2004-02-13 21:21:48 +0000715 N->setReadMarker()->setModifiedMarker();
Chris Lattneradc1efe2004-02-25 17:43:20 +0000716 const Type *ArgTy = F->getFunctionType()->getParamType(2);
717 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
718 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
719 }
720 return;
Chris Lattner52fc8d72004-02-25 23:06:40 +0000721 } else if (F->getName() == "ungetc" || F->getName() == "fputc" ||
722 F->getName() == "fputs" || F->getName() == "putc" ||
Chris Lattner6b586df2004-02-27 20:04:48 +0000723 F->getName() == "ftell" || F->getName() == "rewind" ||
724 F->getName() == "_IO_putc") {
725 // These functions read and write the memory for the file descriptor,
726 // which is passes as the last argument.
Chris Lattnerfe781652004-12-08 16:22:26 +0000727 CallSite::arg_iterator compit = CS.arg_end();
728 DSNodeHandle H = getValueDest(**--compit);
Chris Lattneradc1efe2004-02-25 17:43:20 +0000729 if (DSNode *N = H.getNode()) {
730 N->setReadMarker()->setModifiedMarker();
Chris Lattnerfe781652004-12-08 16:22:26 +0000731 FunctionType::param_iterator compit2 = F->getFunctionType()->param_end();
732 const Type *ArgTy = *--compit2;
Chris Lattnerd5612092004-02-24 22:02:48 +0000733 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
734 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
735 }
Chris Lattner52fc8d72004-02-25 23:06:40 +0000736
737 // Any pointer arguments are read.
738 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
739 AI != E; ++AI)
740 if (isPointerType((*AI)->getType()))
741 if (DSNode *N = getValueDest(**AI).getNode())
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000742 N->setReadMarker();
Chris Lattner52fc8d72004-02-25 23:06:40 +0000743 return;
744 } else if (F->getName() == "fseek" || F->getName() == "fgetpos" ||
745 F->getName() == "fsetpos") {
746 // These functions read and write the memory for the file descriptor,
747 // and read/write all other arguments.
748 DSNodeHandle H = getValueDest(**CS.arg_begin());
749 if (DSNode *N = H.getNode()) {
Chris Lattnerfe781652004-12-08 16:22:26 +0000750 FunctionType::param_iterator compit2 = F->getFunctionType()->param_end();
751 const Type *ArgTy = *--compit2;
Chris Lattner52fc8d72004-02-25 23:06:40 +0000752 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
753 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
754 }
755
756 // Any pointer arguments are read.
757 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
758 AI != E; ++AI)
759 if (isPointerType((*AI)->getType()))
760 if (DSNode *N = getValueDest(**AI).getNode())
761 N->setReadMarker()->setModifiedMarker();
Chris Lattner339d8df2004-02-13 21:21:48 +0000762 return;
Chris Lattner8ecc27e2004-02-20 20:27:11 +0000763 } else if (F->getName() == "printf" || F->getName() == "fprintf" ||
764 F->getName() == "sprintf") {
Chris Lattner339d8df2004-02-13 21:21:48 +0000765 CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
766
767 if (F->getName() == "fprintf") {
768 // fprintf reads and writes the FILE argument, and applies the type
769 // to it.
770 DSNodeHandle H = getValueDest(**AI);
771 if (DSNode *N = H.getNode()) {
772 N->setModifiedMarker();
773 const Type *ArgTy = (*AI)->getType();
774 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
775 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
776 }
Chris Lattner8ecc27e2004-02-20 20:27:11 +0000777 } else if (F->getName() == "sprintf") {
778 // sprintf writes the first string argument.
779 DSNodeHandle H = getValueDest(**AI++);
780 if (DSNode *N = H.getNode()) {
781 N->setModifiedMarker();
782 const Type *ArgTy = (*AI)->getType();
783 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
784 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
785 }
Chris Lattner339d8df2004-02-13 21:21:48 +0000786 }
787
788 for (; AI != E; ++AI) {
789 // printf reads all pointer arguments.
790 if (isPointerType((*AI)->getType()))
791 if (DSNode *N = getValueDest(**AI).getNode())
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000792 N->setReadMarker();
Chris Lattner339d8df2004-02-13 21:21:48 +0000793 }
Chris Lattner4e46e322004-02-20 23:27:09 +0000794 return;
Chris Lattner3aeb40c2004-03-04 21:03:54 +0000795 } else if (F->getName() == "vprintf" || F->getName() == "vfprintf" ||
796 F->getName() == "vsprintf") {
797 CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
798
799 if (F->getName() == "vfprintf") {
800 // ffprintf reads and writes the FILE argument, and applies the type
801 // to it.
802 DSNodeHandle H = getValueDest(**AI);
803 if (DSNode *N = H.getNode()) {
804 N->setModifiedMarker()->setReadMarker();
805 const Type *ArgTy = (*AI)->getType();
806 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
807 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
808 }
809 ++AI;
810 } else if (F->getName() == "vsprintf") {
811 // vsprintf writes the first string argument.
812 DSNodeHandle H = getValueDest(**AI++);
813 if (DSNode *N = H.getNode()) {
814 N->setModifiedMarker();
815 const Type *ArgTy = (*AI)->getType();
816 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
817 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
818 }
819 }
820
821 // Read the format
822 if (AI != E) {
823 if (isPointerType((*AI)->getType()))
824 if (DSNode *N = getValueDest(**AI).getNode())
825 N->setReadMarker();
826 ++AI;
827 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000828
Chris Lattner3aeb40c2004-03-04 21:03:54 +0000829 // Read the valist, and the pointed-to objects.
830 if (AI != E && isPointerType((*AI)->getType())) {
831 const DSNodeHandle &VAList = getValueDest(**AI);
832 if (DSNode *N = VAList.getNode()) {
833 N->setReadMarker();
834 N->mergeTypeInfo(PointerType::get(Type::SByteTy),
835 VAList.getOffset(), false);
836
837 DSNodeHandle &VAListObjs = getLink(VAList);
838 VAListObjs.getNode()->setReadMarker();
839 }
840 }
841
842 return;
Chris Lattner8ecc27e2004-02-20 20:27:11 +0000843 } else if (F->getName() == "scanf" || F->getName() == "fscanf" ||
844 F->getName() == "sscanf") {
845 CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
Chris Lattner339d8df2004-02-13 21:21:48 +0000846
Chris Lattner8ecc27e2004-02-20 20:27:11 +0000847 if (F->getName() == "fscanf") {
848 // fscanf reads and writes the FILE argument, and applies the type
849 // to it.
850 DSNodeHandle H = getValueDest(**AI);
851 if (DSNode *N = H.getNode()) {
852 N->setReadMarker();
853 const Type *ArgTy = (*AI)->getType();
854 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
855 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
856 }
857 } else if (F->getName() == "sscanf") {
858 // sscanf reads the first string argument.
859 DSNodeHandle H = getValueDest(**AI++);
860 if (DSNode *N = H.getNode()) {
861 N->setReadMarker();
862 const Type *ArgTy = (*AI)->getType();
863 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
864 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
865 }
866 }
867
868 for (; AI != E; ++AI) {
869 // scanf writes all pointer arguments.
870 if (isPointerType((*AI)->getType()))
871 if (DSNode *N = getValueDest(**AI).getNode())
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000872 N->setModifiedMarker();
Chris Lattner8ecc27e2004-02-20 20:27:11 +0000873 }
Chris Lattner4e46e322004-02-20 23:27:09 +0000874 return;
Chris Lattner8ecc27e2004-02-20 20:27:11 +0000875 } else if (F->getName() == "strtok") {
876 // strtok reads and writes the first argument, returning it. It reads
877 // its second arg. FIXME: strtok also modifies some hidden static
878 // data. Someday this might matter.
879 CallSite::arg_iterator AI = CS.arg_begin();
880 DSNodeHandle H = getValueDest(**AI++);
881 if (DSNode *N = H.getNode()) {
882 N->setReadMarker()->setModifiedMarker(); // Reads/Writes buffer
883 const Type *ArgTy = F->getFunctionType()->getParamType(0);
884 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
885 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
886 }
887 H.mergeWith(getValueDest(*CS.getInstruction())); // Returns buffer
888
889 H = getValueDest(**AI); // Reads delimiter
890 if (DSNode *N = H.getNode()) {
891 N->setReadMarker();
892 const Type *ArgTy = F->getFunctionType()->getParamType(1);
893 if (const PointerType *PTy = dyn_cast<PointerType>(ArgTy))
894 N->mergeTypeInfo(PTy->getElementType(), H.getOffset());
895 }
896 return;
Chris Lattner1fe98742004-02-26 03:43:08 +0000897 } else if (F->getName() == "strchr" || F->getName() == "strrchr" ||
898 F->getName() == "strstr") {
899 // These read their arguments, and return the first one
Chris Lattneradc1efe2004-02-25 17:43:20 +0000900 DSNodeHandle H = getValueDest(**CS.arg_begin());
Chris Lattner1fe98742004-02-26 03:43:08 +0000901 H.mergeWith(getValueDest(*CS.getInstruction())); // Returns buffer
902
903 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
904 AI != E; ++AI)
905 if (isPointerType((*AI)->getType()))
906 if (DSNode *N = getValueDest(**AI).getNode())
907 N->setReadMarker();
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000908
Chris Lattneradc1efe2004-02-25 17:43:20 +0000909 if (DSNode *N = H.getNode())
910 N->setReadMarker();
Chris Lattneradc1efe2004-02-25 17:43:20 +0000911 return;
Chris Lattnerbeacefa2004-11-08 21:08:28 +0000912 } else if (F->getName() == "__assert_fail") {
913 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
914 AI != E; ++AI)
915 if (isPointerType((*AI)->getType()))
916 if (DSNode *N = getValueDest(**AI).getNode())
917 N->setReadMarker();
918 return;
Chris Lattner52fc8d72004-02-25 23:06:40 +0000919 } else if (F->getName() == "modf" && CS.arg_end()-CS.arg_begin() == 2) {
920 // This writes its second argument, and forces it to double.
Chris Lattnerfe781652004-12-08 16:22:26 +0000921 CallSite::arg_iterator compit = CS.arg_end();
922 DSNodeHandle H = getValueDest(**--compit);
Chris Lattner52fc8d72004-02-25 23:06:40 +0000923 if (DSNode *N = H.getNode()) {
924 N->setModifiedMarker();
925 N->mergeTypeInfo(Type::DoubleTy, H.getOffset());
926 }
927 return;
Chris Lattner339d8df2004-02-13 21:21:48 +0000928 } else {
Chris Lattner304e1432004-02-16 22:57:19 +0000929 // Unknown function, warn if it returns a pointer type or takes a
930 // pointer argument.
931 bool Warn = isPointerType(CS.getInstruction()->getType());
932 if (!Warn)
933 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
934 I != E; ++I)
935 if (isPointerType((*I)->getType())) {
936 Warn = true;
937 break;
938 }
939 if (Warn)
940 std::cerr << "WARNING: Call to unknown external function '"
941 << F->getName() << "' will cause pessimistic results!\n";
Chris Lattnera07b72f2004-02-13 16:09:54 +0000942 }
Chris Lattner894263b2003-09-20 16:50:46 +0000943 }
944
945
Chris Lattnerc314ac42002-07-11 20:32:02 +0000946 // Set up the return value...
Chris Lattner0969c502002-10-21 02:08:03 +0000947 DSNodeHandle RetVal;
Chris Lattner808a7ae2003-09-20 16:34:13 +0000948 Instruction *I = CS.getInstruction();
949 if (isPointerType(I->getType()))
950 RetVal = getValueDest(*I);
Chris Lattnerc314ac42002-07-11 20:32:02 +0000951
Chris Lattnercb582402004-02-26 22:07:22 +0000952 DSNode *CalleeNode = 0;
953 if (DisableDirectCallOpt || !isa<Function>(Callee)) {
954 CalleeNode = getValueDest(*Callee).getNode();
955 if (CalleeNode == 0) {
956 std::cerr << "WARNING: Program is calling through a null pointer?\n"<< *I;
Chris Lattnerfbc2d842003-09-24 23:42:58 +0000957 return; // Calling a null pointer?
958 }
959 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000960
Chris Lattner0969c502002-10-21 02:08:03 +0000961 std::vector<DSNodeHandle> Args;
Chris Lattner808a7ae2003-09-20 16:34:13 +0000962 Args.reserve(CS.arg_end()-CS.arg_begin());
Chris Lattner0969c502002-10-21 02:08:03 +0000963
964 // Calculate the arguments vector...
Chris Lattner808a7ae2003-09-20 16:34:13 +0000965 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E; ++I)
966 if (isPointerType((*I)->getType()))
967 Args.push_back(getValueDest(**I));
Chris Lattner0969c502002-10-21 02:08:03 +0000968
969 // Add a new function call entry...
Chris Lattnercb582402004-02-26 22:07:22 +0000970 if (CalleeNode)
971 FunctionCalls->push_back(DSCallSite(CS, RetVal, CalleeNode, Args));
Chris Lattner923fc052003-02-05 21:59:58 +0000972 else
Chris Lattnercb582402004-02-26 22:07:22 +0000973 FunctionCalls->push_back(DSCallSite(CS, RetVal, cast<Function>(Callee),
Chris Lattner26c4fc32003-09-20 21:48:16 +0000974 Args));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000975}
Chris Lattner055dc2c2002-07-18 15:54:42 +0000976
Vikram S. Advebac06222002-12-06 21:17:10 +0000977void GraphBuilder::visitFreeInst(FreeInst &FI) {
Vikram S. Advebac06222002-12-06 21:17:10 +0000978 // Mark that the node is written to...
Chris Lattnerd5612092004-02-24 22:02:48 +0000979 if (DSNode *N = getValueDest(*FI.getOperand(0)).getNode())
980 N->setModifiedMarker()->setHeapNodeMarker();
Vikram S. Advebac06222002-12-06 21:17:10 +0000981}
982
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000983/// Handle casts...
984void GraphBuilder::visitCastInst(CastInst &CI) {
Chris Lattner5af344d2002-11-02 00:36:03 +0000985 if (isPointerType(CI.getType()))
986 if (isPointerType(CI.getOperand(0)->getType())) {
Chris Lattner157b2522004-10-06 19:29:13 +0000987 DSNodeHandle Ptr = getValueDest(*CI.getOperand(0));
988 if (Ptr.getNode() == 0) return;
989
Chris Lattner5af344d2002-11-02 00:36:03 +0000990 // Cast one pointer to the other, just act like a copy instruction
Chris Lattner157b2522004-10-06 19:29:13 +0000991 setDestTo(CI, Ptr);
Chris Lattner5af344d2002-11-02 00:36:03 +0000992 } else {
993 // Cast something (floating point, small integer) to a pointer. We need
994 // to track the fact that the node points to SOMETHING, just something we
995 // don't know about. Make an "Unknown" node.
996 //
Chris Lattnerbd92b732003-06-19 21:15:11 +0000997 setDestTo(CI, createNode()->setUnknownNodeMarker());
Chris Lattner5af344d2002-11-02 00:36:03 +0000998 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000999}
Chris Lattner055dc2c2002-07-18 15:54:42 +00001000
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001001
Chris Lattner878e5212003-02-04 00:59:50 +00001002// visitInstruction - For all other instruction types, if we have any arguments
1003// that are of pointer type, make them have unknown composition bits, and merge
1004// the nodes together.
1005void GraphBuilder::visitInstruction(Instruction &Inst) {
1006 DSNodeHandle CurNode;
1007 if (isPointerType(Inst.getType()))
1008 CurNode = getValueDest(Inst);
1009 for (User::op_iterator I = Inst.op_begin(), E = Inst.op_end(); I != E; ++I)
1010 if (isPointerType((*I)->getType()))
1011 CurNode.mergeWith(getValueDest(**I));
1012
Chris Lattner62c3a952004-10-30 04:22:45 +00001013 if (DSNode *N = CurNode.getNode())
1014 N->setUnknownNodeMarker();
Chris Lattner878e5212003-02-04 00:59:50 +00001015}
1016
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001017
1018
1019//===----------------------------------------------------------------------===//
1020// LocalDataStructures Implementation
1021//===----------------------------------------------------------------------===//
1022
Chris Lattner26c4fc32003-09-20 21:48:16 +00001023// MergeConstantInitIntoNode - Merge the specified constant into the node
1024// pointed to by NH.
1025void GraphBuilder::MergeConstantInitIntoNode(DSNodeHandle &NH, Constant *C) {
1026 // Ensure a type-record exists...
Chris Lattner62c3a952004-10-30 04:22:45 +00001027 DSNode *NHN = NH.getNode();
1028 NHN->mergeTypeInfo(C->getType(), NH.getOffset());
Chris Lattner26c4fc32003-09-20 21:48:16 +00001029
1030 if (C->getType()->isFirstClassType()) {
1031 if (isPointerType(C->getType()))
1032 // Avoid adding edges from null, or processing non-"pointer" stores
1033 NH.addEdgeTo(getValueDest(*C));
1034 return;
1035 }
Chris Lattner15869aa2003-11-02 22:27:28 +00001036
1037 const TargetData &TD = NH.getNode()->getTargetData();
1038
Chris Lattner26c4fc32003-09-20 21:48:16 +00001039 if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
1040 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1041 // We don't currently do any indexing for arrays...
1042 MergeConstantInitIntoNode(NH, cast<Constant>(CA->getOperand(i)));
1043 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
1044 const StructLayout *SL = TD.getStructLayout(CS->getType());
1045 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
Chris Lattner62c3a952004-10-30 04:22:45 +00001046 DSNode *NHN = NH.getNode();
Chris Lattner507bdf92005-01-12 04:51:37 +00001047 DSNodeHandle NewNH(NHN, NH.getOffset()+(unsigned)SL->MemberOffsets[i]);
Chris Lattner26c4fc32003-09-20 21:48:16 +00001048 MergeConstantInitIntoNode(NewNH, cast<Constant>(CS->getOperand(i)));
1049 }
Chris Lattner48b2f6b2004-10-26 16:23:03 +00001050 } else if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) {
Chris Lattner896481e2004-02-15 05:53:42 +00001051 // Noop
Chris Lattner26c4fc32003-09-20 21:48:16 +00001052 } else {
1053 assert(0 && "Unknown constant type!");
1054 }
1055}
1056
1057void GraphBuilder::mergeInGlobalInitializer(GlobalVariable *GV) {
1058 assert(!GV->isExternal() && "Cannot merge in external global!");
1059 // Get a node handle to the global node and merge the initializer into it.
1060 DSNodeHandle NH = getValueDest(*GV);
1061 MergeConstantInitIntoNode(NH, GV->getInitializer());
1062}
1063
1064
Chris Lattner9b426bd2005-03-20 03:32:35 +00001065/// BuildGlobalECs - Look at all of the nodes in the globals graph. If any node
1066/// contains multiple globals, DSA will never, ever, be able to tell the globals
1067/// apart. Instead of maintaining this information in all of the graphs
1068/// throughout the entire program, store only a single global (the "leader") in
1069/// the graphs, and build equivalence classes for the rest of the globals.
1070static void BuildGlobalECs(DSGraph &GG, std::set<GlobalValue*> &ECGlobals) {
1071 DSScalarMap &SM = GG.getScalarMap();
1072 EquivalenceClasses<GlobalValue*> &GlobalECs = SM.getGlobalECs();
1073 for (DSGraph::node_iterator I = GG.node_begin(), E = GG.node_end();
1074 I != E; ++I) {
1075 if (I->getGlobalsList().size() <= 1) continue;
1076
1077 // First, build up the equivalence set for this block of globals.
1078 const std::vector<GlobalValue*> &GVs = I->getGlobalsList();
1079 GlobalValue *First = GVs[0];
1080 for (unsigned i = 1, e = GVs.size(); i != e; ++i)
1081 GlobalECs.unionSets(First, GVs[i]);
Misha Brukman2b37d7c2005-04-21 21:13:18 +00001082
Chris Lattner9b426bd2005-03-20 03:32:35 +00001083 // Next, get the leader element.
1084 assert(First == GlobalECs.getLeaderValue(First) &&
1085 "First did not end up being the leader?");
Misha Brukman2b37d7c2005-04-21 21:13:18 +00001086
Chris Lattner9b426bd2005-03-20 03:32:35 +00001087 // Next, remove all globals from the scalar map that are not the leader.
1088 assert(GVs[0] == First && "First had to be at the front!");
1089 for (unsigned i = 1, e = GVs.size(); i != e; ++i) {
1090 ECGlobals.insert(GVs[i]);
1091 SM.erase(SM.find(GVs[i]));
1092 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00001093
Chris Lattner9b426bd2005-03-20 03:32:35 +00001094 // Finally, change the global node to only contain the leader.
1095 I->clearGlobals();
1096 I->addGlobal(First);
1097 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +00001098
Chris Lattner9b426bd2005-03-20 03:32:35 +00001099 DEBUG(GG.AssertGraphOK());
1100}
1101
1102/// EliminateUsesOfECGlobals - Once we have determined that some globals are in
1103/// really just equivalent to some other globals, remove the globals from the
1104/// specified DSGraph (if present), and merge any nodes with their leader nodes.
1105static void EliminateUsesOfECGlobals(DSGraph &G,
1106 const std::set<GlobalValue*> &ECGlobals) {
1107 DSScalarMap &SM = G.getScalarMap();
1108 EquivalenceClasses<GlobalValue*> &GlobalECs = SM.getGlobalECs();
1109
1110 bool MadeChange = false;
1111 for (DSScalarMap::global_iterator GI = SM.global_begin(), E = SM.global_end();
1112 GI != E; ) {
1113 GlobalValue *GV = *GI++;
1114 if (!ECGlobals.count(GV)) continue;
1115
1116 const DSNodeHandle &GVNH = SM[GV];
1117 assert(!GVNH.isNull() && "Global has null NH!?");
1118
1119 // Okay, this global is in some equivalence class. Start by finding the
1120 // leader of the class.
1121 GlobalValue *Leader = GlobalECs.getLeaderValue(GV);
1122
1123 // If the leader isn't already in the graph, insert it into the node
1124 // corresponding to GV.
1125 if (!SM.global_count(Leader)) {
1126 GVNH.getNode()->addGlobal(Leader);
1127 SM[Leader] = GVNH;
1128 } else {
1129 // Otherwise, the leader is in the graph, make sure the nodes are the
1130 // merged in the specified graph.
1131 const DSNodeHandle &LNH = SM[Leader];
1132 if (LNH.getNode() != GVNH.getNode())
1133 LNH.mergeWith(GVNH);
1134 }
1135
1136 // Next step, remove the global from the DSNode.
1137 GVNH.getNode()->removeGlobal(GV);
1138
1139 // Finally, remove the global from the ScalarMap.
1140 SM.erase(GV);
1141 MadeChange = true;
1142 }
1143
1144 DEBUG(if(MadeChange) G.AssertGraphOK());
1145}
1146
Chris Lattnerb12914b2004-09-20 04:48:05 +00001147bool LocalDataStructures::runOnModule(Module &M) {
Chris Lattner15869aa2003-11-02 22:27:28 +00001148 const TargetData &TD = getAnalysis<TargetData>();
Chris Lattneraa0b4682002-11-09 21:12:07 +00001149
Chris Lattnerf4f62272005-03-19 22:23:45 +00001150 // First step, build the globals graph.
1151 GlobalsGraph = new DSGraph(GlobalECs, TD);
Chris Lattnerc420ab62004-02-25 23:31:02 +00001152 {
1153 GraphBuilder GGB(*GlobalsGraph);
Misha Brukman2b37d7c2005-04-21 21:13:18 +00001154
Chris Lattnerf4f62272005-03-19 22:23:45 +00001155 // Add initializers for all of the globals to the globals graph.
1156 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
1157 I != E; ++I)
Chris Lattnerc420ab62004-02-25 23:31:02 +00001158 if (!I->isExternal())
1159 GGB.mergeInGlobalInitializer(I);
1160 }
1161
Chris Lattnerf4f62272005-03-19 22:23:45 +00001162 // Next step, iterate through the nodes in the globals graph, unioning
1163 // together the globals into equivalence classes.
Chris Lattner9b426bd2005-03-20 03:32:35 +00001164 std::set<GlobalValue*> ECGlobals;
1165 BuildGlobalECs(*GlobalsGraph, ECGlobals);
1166 DEBUG(std::cerr << "Eliminating " << ECGlobals.size() << " EC Globals!\n");
1167 ECGlobals.clear();
Chris Lattnerf4f62272005-03-19 22:23:45 +00001168
Chris Lattneraa0b4682002-11-09 21:12:07 +00001169 // Calculate all of the graphs...
1170 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1171 if (!I->isExternal())
Chris Lattnerf4f62272005-03-19 22:23:45 +00001172 DSInfo.insert(std::make_pair(I, new DSGraph(GlobalECs, TD, *I,
1173 GlobalsGraph)));
Chris Lattner26c4fc32003-09-20 21:48:16 +00001174
Chris Lattnerc3f5f772004-02-08 01:51:48 +00001175 GlobalsGraph->removeTriviallyDeadNodes();
Chris Lattner26c4fc32003-09-20 21:48:16 +00001176 GlobalsGraph->markIncompleteNodes(DSGraph::MarkFormalArgs);
Chris Lattner9b426bd2005-03-20 03:32:35 +00001177
1178 // Now that we've computed all of the graphs, and merged all of the info into
1179 // the globals graph, see if we have further constrained the globals in the
1180 // program if so, update GlobalECs and remove the extraneous globals from the
1181 // program.
1182 BuildGlobalECs(*GlobalsGraph, ECGlobals);
1183 if (!ECGlobals.empty()) {
1184 DEBUG(std::cerr << "Eliminating " << ECGlobals.size() << " EC Globals!\n");
1185 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
1186 E = DSInfo.end(); I != E; ++I)
1187 EliminateUsesOfECGlobals(*I->second, ECGlobals);
1188 }
1189
Chris Lattneraa0b4682002-11-09 21:12:07 +00001190 return false;
1191}
1192
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001193// releaseMemory - If the pass pipeline is done with this pass, we can release
1194// our memory... here...
1195//
1196void LocalDataStructures::releaseMemory() {
Chris Lattner81d924d2003-06-30 04:53:27 +00001197 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
1198 E = DSInfo.end(); I != E; ++I) {
1199 I->second->getReturnNodes().erase(I->first);
1200 if (I->second->getReturnNodes().empty())
1201 delete I->second;
1202 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001203
1204 // Empty map so next time memory is released, data structures are not
1205 // re-deleted.
1206 DSInfo.clear();
Chris Lattner2e4f9bf2002-11-09 20:01:01 +00001207 delete GlobalsGraph;
1208 GlobalsGraph = 0;
Chris Lattnerfccd06f2002-10-01 22:33:50 +00001209}
Brian Gaeked0fde302003-11-11 22:41:34 +00001210