blob: 253cbf7a3511f216745b84e7251c880040b5d057 [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 Lattner055dc2c2002-07-18 15:54:42 +000015#include "llvm/Analysis/DataStructure.h"
Chris Lattner0779ba62002-11-09 19:25:10 +000016#include "llvm/Analysis/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 Lattnerc68c31b2002-07-10 22:38:08 +000020#include "llvm/Support/InstVisitor.h"
Chris Lattnerfccd06f2002-10-01 22:33:50 +000021#include "llvm/Target/TargetData.h"
Chris Lattner923fc052003-02-05 21:59:58 +000022#include "Support/CommandLine.h"
Chris Lattner6806f562003-08-01 22:15:03 +000023#include "Support/Debug.h"
24#include "Support/Timer.h"
Chris Lattnerfccd06f2002-10-01 22:33:50 +000025
26// FIXME: This should eventually be a FunctionPass that is automatically
27// aggregated into a Pass.
28//
29#include "llvm/Module.h"
30
Brian Gaeked0fde302003-11-11 22:41:34 +000031namespace llvm {
32
Chris Lattner97f51a32002-07-27 01:12:15 +000033static RegisterAnalysis<LocalDataStructures>
34X("datastructure", "Local Data Structure Analysis");
Chris Lattner97f51a32002-07-27 01:12:15 +000035
Chris Lattnerb1060432002-11-07 05:20:53 +000036namespace DS {
Chris Lattnerfccd06f2002-10-01 22:33:50 +000037 // isPointerType - Return true if this type is big enough to hold a pointer.
38 bool isPointerType(const Type *Ty) {
39 if (isa<PointerType>(Ty))
40 return true;
41 else if (Ty->isPrimitiveType() && Ty->isInteger())
42 return Ty->getPrimitiveSize() >= PointerSize;
43 return false;
44 }
45}
46
Brian Gaeked0fde302003-11-11 22:41:34 +000047using namespace DS;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000048
49namespace {
Chris Lattner923fc052003-02-05 21:59:58 +000050 cl::opt<bool>
51 DisableDirectCallOpt("disable-direct-call-dsopt", cl::Hidden,
52 cl::desc("Disable direct call optimization in "
53 "DSGraph construction"));
Chris Lattnerca3f7902003-02-08 20:18:39 +000054 cl::opt<bool>
55 DisableFieldSensitivity("disable-ds-field-sensitivity", cl::Hidden,
56 cl::desc("Disable field sensitivity in DSGraphs"));
Chris Lattner923fc052003-02-05 21:59:58 +000057
Chris Lattnerfccd06f2002-10-01 22:33:50 +000058 //===--------------------------------------------------------------------===//
59 // GraphBuilder Class
60 //===--------------------------------------------------------------------===//
61 //
62 /// This class is the builder class that constructs the local data structure
63 /// graph by performing a single pass over the function in question.
64 ///
Chris Lattnerc68c31b2002-07-10 22:38:08 +000065 class GraphBuilder : InstVisitor<GraphBuilder> {
66 DSGraph &G;
Chris Lattner26c4fc32003-09-20 21:48:16 +000067 DSNodeHandle *RetNode; // Node that gets returned...
Chris Lattner5a540632003-06-30 03:15:25 +000068 DSGraph::ScalarMapTy &ScalarMap;
Chris Lattner26c4fc32003-09-20 21:48:16 +000069 std::vector<DSCallSite> *FunctionCalls;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000070
71 public:
Chris Lattner44cfdf92003-07-02 06:06:34 +000072 GraphBuilder(Function &f, DSGraph &g, DSNodeHandle &retNode,
Chris Lattner26c4fc32003-09-20 21:48:16 +000073 std::vector<DSCallSite> &fc)
74 : G(g), RetNode(&retNode), ScalarMap(G.getScalarMap()),
75 FunctionCalls(&fc) {
Chris Lattner0d9bab82002-07-18 00:12:30 +000076
77 // Create scalar nodes for all pointer arguments...
Chris Lattner26c4fc32003-09-20 21:48:16 +000078 for (Function::aiterator I = f.abegin(), E = f.aend(); I != E; ++I)
Chris Lattnerfccd06f2002-10-01 22:33:50 +000079 if (isPointerType(I->getType()))
80 getValueDest(*I);
Chris Lattner0d9bab82002-07-18 00:12:30 +000081
Chris Lattner26c4fc32003-09-20 21:48:16 +000082 visit(f); // Single pass over the function
Chris Lattnerc68c31b2002-07-10 22:38:08 +000083 }
84
Chris Lattner26c4fc32003-09-20 21:48:16 +000085 // GraphBuilder ctor for working on the globals graph
86 GraphBuilder(DSGraph &g)
87 : G(g), RetNode(0), ScalarMap(G.getScalarMap()), FunctionCalls(0) {
88 }
89
90 void mergeInGlobalInitializer(GlobalVariable *GV);
91
Chris Lattnerc68c31b2002-07-10 22:38:08 +000092 private:
93 // Visitor functions, used to handle each instruction type we encounter...
94 friend class InstVisitor<GraphBuilder>;
Chris Lattnerbd92b732003-06-19 21:15:11 +000095 void visitMallocInst(MallocInst &MI) { handleAlloc(MI, true); }
96 void visitAllocaInst(AllocaInst &AI) { handleAlloc(AI, false); }
97 void handleAlloc(AllocationInst &AI, bool isHeap);
Chris Lattnerc68c31b2002-07-10 22:38:08 +000098
99 void visitPHINode(PHINode &PN);
100
Chris Lattner51340062002-11-08 05:00:44 +0000101 void visitGetElementPtrInst(User &GEP);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000102 void visitReturnInst(ReturnInst &RI);
103 void visitLoadInst(LoadInst &LI);
104 void visitStoreInst(StoreInst &SI);
105 void visitCallInst(CallInst &CI);
Chris Lattner808a7ae2003-09-20 16:34:13 +0000106 void visitInvokeInst(InvokeInst &II);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000107 void visitSetCondInst(SetCondInst &SCI) {} // SetEQ & friends are ignored
Vikram S. Advebac06222002-12-06 21:17:10 +0000108 void visitFreeInst(FreeInst &FI);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000109 void visitCastInst(CastInst &CI);
Chris Lattner878e5212003-02-04 00:59:50 +0000110 void visitInstruction(Instruction &I);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000111
Chris Lattner808a7ae2003-09-20 16:34:13 +0000112 void visitCallSite(CallSite CS);
Chris Lattner26c4fc32003-09-20 21:48:16 +0000113
114 void MergeConstantInitIntoNode(DSNodeHandle &NH, Constant *C);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000115 private:
116 // Helper functions used to implement the visitation functions...
117
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000118 /// createNode - Create a new DSNode, ensuring that it is properly added to
119 /// the graph.
120 ///
Chris Lattnerbd92b732003-06-19 21:15:11 +0000121 DSNode *createNode(const Type *Ty = 0) {
122 DSNode *N = new DSNode(Ty, &G); // Create the node
Chris Lattnere158b192003-06-16 12:08:18 +0000123 if (DisableFieldSensitivity) {
Chris Lattnerca3f7902003-02-08 20:18:39 +0000124 N->foldNodeCompletely();
Chris Lattnere158b192003-06-16 12:08:18 +0000125 if (DSNode *FN = N->getForwardNode())
126 N = FN;
127 }
Chris Lattner92673292002-11-02 00:13:20 +0000128 return N;
129 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000130
Chris Lattnerc875f022002-11-03 21:27:48 +0000131 /// setDestTo - Set the ScalarMap entry for the specified value to point to
Chris Lattner92673292002-11-02 00:13:20 +0000132 /// the specified destination. If the Value already points to a node, make
133 /// sure to merge the two destinations together.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000134 ///
Chris Lattner92673292002-11-02 00:13:20 +0000135 void setDestTo(Value &V, const DSNodeHandle &NH);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000136
Chris Lattner92673292002-11-02 00:13:20 +0000137 /// getValueDest - Return the DSNode that the actual value points to.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000138 ///
Chris Lattner92673292002-11-02 00:13:20 +0000139 DSNodeHandle getValueDest(Value &V);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000140
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000141 /// getLink - This method is used to return the specified link in the
142 /// specified node if one exists. If a link does not already exist (it's
Chris Lattner7e51c872002-10-31 06:52:26 +0000143 /// null), then we create a new node, link it, then return it.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000144 ///
Chris Lattner7e51c872002-10-31 06:52:26 +0000145 DSNodeHandle &getLink(const DSNodeHandle &Node, unsigned Link = 0);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000146 };
147}
148
Brian Gaeked0fde302003-11-11 22:41:34 +0000149using namespace DS;
150
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000151//===----------------------------------------------------------------------===//
152// DSGraph constructor - Simply use the GraphBuilder to construct the local
153// graph.
Chris Lattner15869aa2003-11-02 22:27:28 +0000154DSGraph::DSGraph(const TargetData &td, Function &F, DSGraph *GG)
155 : GlobalsGraph(GG), TD(td) {
Chris Lattner2a068862002-11-10 06:53:38 +0000156 PrintAuxCalls = false;
Chris Lattner30514192003-07-02 04:37:26 +0000157
158 DEBUG(std::cerr << " [Loc] Calculating graph for: " << F.getName() << "\n");
159
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000160 // Use the graph builder to construct the local version of the graph
Chris Lattner26c4fc32003-09-20 21:48:16 +0000161 GraphBuilder B(F, *this, ReturnNodes[&F], FunctionCalls);
Chris Lattner4fe34612002-11-18 21:44:19 +0000162#ifndef NDEBUG
163 Timer::addPeakMemoryMeasurement();
164#endif
Chris Lattner1a1a85d2003-02-14 04:55:58 +0000165
166 // Remove all integral constants from the scalarmap!
Chris Lattner8d327672003-06-30 03:36:09 +0000167 for (ScalarMapTy::iterator I = ScalarMap.begin(); I != ScalarMap.end();)
Chris Lattner1a1a85d2003-02-14 04:55:58 +0000168 if (isa<ConstantIntegral>(I->first)) {
Chris Lattner8d327672003-06-30 03:36:09 +0000169 ScalarMapTy::iterator J = I++;
Chris Lattner1a1a85d2003-02-14 04:55:58 +0000170 ScalarMap.erase(J);
171 } else
172 ++I;
173
Chris Lattner394471f2003-01-23 22:05:33 +0000174 markIncompleteNodes(DSGraph::MarkFormalArgs);
Chris Lattner96517252002-11-09 20:55:24 +0000175
176 // Remove any nodes made dead due to merging...
Chris Lattner394471f2003-01-23 22:05:33 +0000177 removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000178}
179
180
181//===----------------------------------------------------------------------===//
182// Helper method implementations...
183//
184
Chris Lattner92673292002-11-02 00:13:20 +0000185/// getValueDest - Return the DSNode that the actual value points to.
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000186///
Chris Lattner51340062002-11-08 05:00:44 +0000187DSNodeHandle GraphBuilder::getValueDest(Value &Val) {
188 Value *V = &Val;
189 if (V == Constant::getNullValue(V->getType()))
190 return 0; // Null doesn't point to anything, don't add to ScalarMap!
Chris Lattner92673292002-11-02 00:13:20 +0000191
Vikram S. Advebac06222002-12-06 21:17:10 +0000192 DSNodeHandle &NH = ScalarMap[V];
193 if (NH.getNode())
194 return NH; // Already have a node? Just return it...
195
196 // Otherwise we need to create a new node to point to.
197 // Check first for constant expressions that must be traversed to
198 // extract the actual value.
Chris Lattner51340062002-11-08 05:00:44 +0000199 if (Constant *C = dyn_cast<Constant>(V))
200 if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)) {
Vikram S. Advebac06222002-12-06 21:17:10 +0000201 return NH = getValueDest(*CPR->getValue());
Chris Lattner51340062002-11-08 05:00:44 +0000202 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
203 if (CE->getOpcode() == Instruction::Cast)
Chris Lattner1e56c542003-02-09 23:04:12 +0000204 NH = getValueDest(*CE->getOperand(0));
205 else if (CE->getOpcode() == Instruction::GetElementPtr) {
Chris Lattner51340062002-11-08 05:00:44 +0000206 visitGetElementPtrInst(*CE);
Chris Lattner8d327672003-06-30 03:36:09 +0000207 DSGraph::ScalarMapTy::iterator I = ScalarMap.find(CE);
Chris Lattner2cfbaaf2002-11-09 20:14:03 +0000208 assert(I != ScalarMap.end() && "GEP didn't get processed right?");
Chris Lattner1e56c542003-02-09 23:04:12 +0000209 NH = I->second;
210 } else {
211 // This returns a conservative unknown node for any unhandled ConstExpr
Chris Lattnerbd92b732003-06-19 21:15:11 +0000212 return NH = createNode()->setUnknownNodeMarker();
Chris Lattner51340062002-11-08 05:00:44 +0000213 }
Chris Lattner1e56c542003-02-09 23:04:12 +0000214 if (NH.getNode() == 0) { // (getelementptr null, X) returns null
215 ScalarMap.erase(V);
216 return 0;
217 }
218 return NH;
Chris Lattner51340062002-11-08 05:00:44 +0000219
Chris Lattner51340062002-11-08 05:00:44 +0000220 } else if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(C)) {
221 // Random constants are unknown mem
Chris Lattnerbd92b732003-06-19 21:15:11 +0000222 return NH = createNode()->setUnknownNodeMarker();
Chris Lattner51340062002-11-08 05:00:44 +0000223 } else {
224 assert(0 && "Unknown constant type!");
225 }
226
Chris Lattner92673292002-11-02 00:13:20 +0000227 // Otherwise we need to create a new node to point to...
228 DSNode *N;
Chris Lattner51340062002-11-08 05:00:44 +0000229 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner92673292002-11-02 00:13:20 +0000230 // Create a new global node for this global variable...
Chris Lattnerbd92b732003-06-19 21:15:11 +0000231 N = createNode(GV->getType()->getElementType());
Chris Lattner92673292002-11-02 00:13:20 +0000232 N->addGlobal(GV);
233 } else {
234 // Otherwise just create a shadow node
Chris Lattnerbd92b732003-06-19 21:15:11 +0000235 N = createNode();
Chris Lattner92673292002-11-02 00:13:20 +0000236 }
237
238 NH.setNode(N); // Remember that we are pointing to it...
239 NH.setOffset(0);
240 return NH;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000241}
242
Chris Lattner0d9bab82002-07-18 00:12:30 +0000243
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000244/// getLink - This method is used to return the specified link in the
245/// specified node if one exists. If a link does not already exist (it's
246/// null), then we create a new node, link it, then return it. We must
247/// specify the type of the Node field we are accessing so that we know what
248/// type should be linked to if we need to create a new node.
249///
Chris Lattner7e51c872002-10-31 06:52:26 +0000250DSNodeHandle &GraphBuilder::getLink(const DSNodeHandle &node, unsigned LinkNo) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000251 DSNodeHandle &Node = const_cast<DSNodeHandle&>(node);
Chris Lattner08db7192002-11-06 06:20:27 +0000252 DSNodeHandle &Link = Node.getLink(LinkNo);
253 if (!Link.getNode()) {
254 // If the link hasn't been created yet, make and return a new shadow node
Chris Lattnerbd92b732003-06-19 21:15:11 +0000255 Link = createNode();
Chris Lattner08db7192002-11-06 06:20:27 +0000256 }
257 return Link;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000258}
259
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000260
Chris Lattnerc875f022002-11-03 21:27:48 +0000261/// setDestTo - Set the ScalarMap entry for the specified value to point to the
Chris Lattner92673292002-11-02 00:13:20 +0000262/// specified destination. If the Value already points to a node, make sure to
263/// merge the two destinations together.
264///
265void GraphBuilder::setDestTo(Value &V, const DSNodeHandle &NH) {
Chris Lattnerc875f022002-11-03 21:27:48 +0000266 DSNodeHandle &AINH = ScalarMap[&V];
Chris Lattner92673292002-11-02 00:13:20 +0000267 if (AINH.getNode() == 0) // Not pointing to anything yet?
268 AINH = NH; // Just point directly to NH
269 else
270 AINH.mergeWith(NH);
271}
272
273
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000274//===----------------------------------------------------------------------===//
275// Specific instruction type handler implementations...
276//
277
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000278/// Alloca & Malloc instruction implementation - Simply create a new memory
279/// object, pointing the scalar to it.
280///
Chris Lattnerbd92b732003-06-19 21:15:11 +0000281void GraphBuilder::handleAlloc(AllocationInst &AI, bool isHeap) {
282 DSNode *N = createNode();
283 if (isHeap)
284 N->setHeapNodeMarker();
285 else
286 N->setAllocaNodeMarker();
287 setDestTo(AI, N);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000288}
289
290// PHINode - Make the scalar for the PHI node point to all of the things the
291// incoming values point to... which effectively causes them to be merged.
292//
293void GraphBuilder::visitPHINode(PHINode &PN) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000294 if (!isPointerType(PN.getType())) return; // Only pointer PHIs
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000295
Chris Lattnerc875f022002-11-03 21:27:48 +0000296 DSNodeHandle &PNDest = ScalarMap[&PN];
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000297 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
Chris Lattner92673292002-11-02 00:13:20 +0000298 PNDest.mergeWith(getValueDest(*PN.getIncomingValue(i)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000299}
300
Chris Lattner51340062002-11-08 05:00:44 +0000301void GraphBuilder::visitGetElementPtrInst(User &GEP) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000302 DSNodeHandle Value = getValueDest(*GEP.getOperand(0));
Chris Lattner92673292002-11-02 00:13:20 +0000303 if (Value.getNode() == 0) return;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000304
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000305 const PointerType *PTy = cast<PointerType>(GEP.getOperand(0)->getType());
306 const Type *CurTy = PTy->getElementType();
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000307
Chris Lattner08db7192002-11-06 06:20:27 +0000308 if (Value.getNode()->mergeTypeInfo(CurTy, Value.getOffset())) {
309 // If the node had to be folded... exit quickly
Chris Lattner92673292002-11-02 00:13:20 +0000310 setDestTo(GEP, Value); // GEP result points to folded node
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000311 return;
312 }
313
Chris Lattner15869aa2003-11-02 22:27:28 +0000314 const TargetData &TD = Value.getNode()->getTargetData();
315
Chris Lattner08db7192002-11-06 06:20:27 +0000316#if 0
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000317 // Handle the pointer index specially...
318 if (GEP.getNumOperands() > 1 &&
319 GEP.getOperand(1) != ConstantSInt::getNullValue(Type::LongTy)) {
320
321 // If we already know this is an array being accessed, don't do anything...
322 if (!TopTypeRec.isArray) {
323 TopTypeRec.isArray = true;
324
325 // If we are treating some inner field pointer as an array, fold the node
326 // up because we cannot handle it right. This can come because of
327 // something like this: &((&Pt->X)[1]) == &Pt->Y
328 //
329 if (Value.getOffset()) {
330 // Value is now the pointer we want to GEP to be...
331 Value.getNode()->foldNodeCompletely();
Chris Lattner92673292002-11-02 00:13:20 +0000332 setDestTo(GEP, Value); // GEP result points to folded node
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000333 return;
334 } else {
335 // This is a pointer to the first byte of the node. Make sure that we
336 // are pointing to the outter most type in the node.
337 // FIXME: We need to check one more case here...
338 }
339 }
340 }
Chris Lattner08db7192002-11-06 06:20:27 +0000341#endif
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000342
343 // All of these subscripts are indexing INTO the elements we have...
Chris Lattner26c4fc32003-09-20 21:48:16 +0000344 unsigned Offset = 0;
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000345 for (unsigned i = 2, e = GEP.getNumOperands(); i < e; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000346 if (GEP.getOperand(i)->getType() == Type::LongTy) {
Chris Lattnere03f32b2002-10-02 06:24:36 +0000347 // Get the type indexing into...
348 const SequentialType *STy = cast<SequentialType>(CurTy);
349 CurTy = STy->getElementType();
Chris Lattner08db7192002-11-06 06:20:27 +0000350#if 0
Chris Lattnere03f32b2002-10-02 06:24:36 +0000351 if (ConstantSInt *CS = dyn_cast<ConstantSInt>(GEP.getOperand(i))) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000352 Offset += CS->getValue()*TD.getTypeSize(CurTy);
Chris Lattnere03f32b2002-10-02 06:24:36 +0000353 } else {
354 // Variable index into a node. We must merge all of the elements of the
355 // sequential type here.
356 if (isa<PointerType>(STy))
357 std::cerr << "Pointer indexing not handled yet!\n";
358 else {
359 const ArrayType *ATy = cast<ArrayType>(STy);
360 unsigned ElSize = TD.getTypeSize(CurTy);
361 DSNode *N = Value.getNode();
362 assert(N && "Value must have a node!");
363 unsigned RawOffset = Offset+Value.getOffset();
364
365 // Loop over all of the elements of the array, merging them into the
Misha Brukman2f2d0652003-09-11 18:14:24 +0000366 // zeroth element.
Chris Lattnere03f32b2002-10-02 06:24:36 +0000367 for (unsigned i = 1, e = ATy->getNumElements(); i != e; ++i)
368 // Merge all of the byte components of this array element
369 for (unsigned j = 0; j != ElSize; ++j)
370 N->mergeIndexes(RawOffset+j, RawOffset+i*ElSize+j);
371 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000372 }
Chris Lattner08db7192002-11-06 06:20:27 +0000373#endif
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000374 } else if (GEP.getOperand(i)->getType() == Type::UByteTy) {
375 unsigned FieldNo = cast<ConstantUInt>(GEP.getOperand(i))->getValue();
376 const StructType *STy = cast<StructType>(CurTy);
377 Offset += TD.getStructLayout(STy)->MemberOffsets[FieldNo];
378 CurTy = STy->getContainedType(FieldNo);
379 }
380
381 // Add in the offset calculated...
382 Value.setOffset(Value.getOffset()+Offset);
383
384 // Value is now the pointer we want to GEP to be...
Chris Lattner92673292002-11-02 00:13:20 +0000385 setDestTo(GEP, Value);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000386}
387
388void GraphBuilder::visitLoadInst(LoadInst &LI) {
Chris Lattner92673292002-11-02 00:13:20 +0000389 DSNodeHandle Ptr = getValueDest(*LI.getOperand(0));
390 if (Ptr.getNode() == 0) return;
391
392 // Make that the node is read from...
Chris Lattnerbd92b732003-06-19 21:15:11 +0000393 Ptr.getNode()->setReadMarker();
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000394
395 // Ensure a typerecord exists...
Chris Lattner088b6392003-03-03 17:13:31 +0000396 Ptr.getNode()->mergeTypeInfo(LI.getType(), Ptr.getOffset(), false);
Chris Lattner92673292002-11-02 00:13:20 +0000397
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000398 if (isPointerType(LI.getType()))
Chris Lattner92673292002-11-02 00:13:20 +0000399 setDestTo(LI, getLink(Ptr));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000400}
401
402void GraphBuilder::visitStoreInst(StoreInst &SI) {
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000403 const Type *StoredTy = SI.getOperand(0)->getType();
Chris Lattner92673292002-11-02 00:13:20 +0000404 DSNodeHandle Dest = getValueDest(*SI.getOperand(1));
405 if (Dest.getNode() == 0) return;
406
Vikram S. Advebac06222002-12-06 21:17:10 +0000407 // Mark that the node is written to...
Chris Lattnerbd92b732003-06-19 21:15:11 +0000408 Dest.getNode()->setModifiedMarker();
Chris Lattner8f0a16e2002-10-31 05:45:02 +0000409
Chris Lattner26c4fc32003-09-20 21:48:16 +0000410 // Ensure a type-record exists...
Chris Lattner08db7192002-11-06 06:20:27 +0000411 Dest.getNode()->mergeTypeInfo(StoredTy, Dest.getOffset());
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000412
413 // Avoid adding edges from null, or processing non-"pointer" stores
Chris Lattner92673292002-11-02 00:13:20 +0000414 if (isPointerType(StoredTy))
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000415 Dest.addEdgeTo(getValueDest(*SI.getOperand(0)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000416}
417
418void GraphBuilder::visitReturnInst(ReturnInst &RI) {
Chris Lattner92673292002-11-02 00:13:20 +0000419 if (RI.getNumOperands() && isPointerType(RI.getOperand(0)->getType()))
Chris Lattner26c4fc32003-09-20 21:48:16 +0000420 RetNode->mergeWith(getValueDest(*RI.getOperand(0)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000421}
422
423void GraphBuilder::visitCallInst(CallInst &CI) {
Chris Lattner808a7ae2003-09-20 16:34:13 +0000424 visitCallSite(&CI);
425}
426
427void GraphBuilder::visitInvokeInst(InvokeInst &II) {
428 visitCallSite(&II);
429}
430
431void GraphBuilder::visitCallSite(CallSite CS) {
Chris Lattner894263b2003-09-20 16:50:46 +0000432 // Special case handling of certain libc allocation functions here.
433 if (Function *F = CS.getCalledFunction())
434 if (F->isExternal())
435 if (F->getName() == "calloc") {
436 setDestTo(*CS.getInstruction(),
437 createNode()->setHeapNodeMarker()->setModifiedMarker());
438 return;
439 } else if (F->getName() == "realloc") {
440 DSNodeHandle RetNH = getValueDest(*CS.getInstruction());
441 RetNH.mergeWith(getValueDest(**CS.arg_begin()));
Chris Lattnerfb8c6102003-11-08 21:55:50 +0000442 if (DSNode *N = RetNH.getNode())
443 N->setHeapNodeMarker()->setModifiedMarker()->setReadMarker();
444 return;
445 } else if (F->getName() == "memset") {
446 // Merge the first argument with the return value, and mark the memory
447 // modified.
448 DSNodeHandle RetNH = getValueDest(*CS.getInstruction());
449 RetNH.mergeWith(getValueDest(**CS.arg_begin()));
450 if (DSNode *N = RetNH.getNode())
451 N->setModifiedMarker();
452 return;
Chris Lattner67ce57a2003-11-09 03:32:52 +0000453 } else if (F->getName() == "memmove") {
454 // Merge the first & second arguments with the result, and mark the
455 // memory read and modified.
456 DSNodeHandle RetNH = getValueDest(*CS.getInstruction());
457 RetNH.mergeWith(getValueDest(**CS.arg_begin()));
458 RetNH.mergeWith(getValueDest(**(CS.arg_begin()+1)));
459 if (DSNode *N = RetNH.getNode())
460 N->setModifiedMarker()->setReadMarker();
461 return;
Chris Lattnerfb8c6102003-11-08 21:55:50 +0000462 } else if (F->getName() == "bzero") {
463 // Mark the memory modified.
464 DSNodeHandle H = getValueDest(**CS.arg_begin());
465 if (DSNode *N = H.getNode())
466 N->setModifiedMarker();
Chris Lattner894263b2003-09-20 16:50:46 +0000467 return;
468 }
469
470
Chris Lattnerc314ac42002-07-11 20:32:02 +0000471 // Set up the return value...
Chris Lattner0969c502002-10-21 02:08:03 +0000472 DSNodeHandle RetVal;
Chris Lattner808a7ae2003-09-20 16:34:13 +0000473 Instruction *I = CS.getInstruction();
474 if (isPointerType(I->getType()))
475 RetVal = getValueDest(*I);
Chris Lattnerc314ac42002-07-11 20:32:02 +0000476
Chris Lattner923fc052003-02-05 21:59:58 +0000477 DSNode *Callee = 0;
Chris Lattnerfbc2d842003-09-24 23:42:58 +0000478 if (DisableDirectCallOpt || !isa<Function>(CS.getCalledValue())) {
Chris Lattner808a7ae2003-09-20 16:34:13 +0000479 Callee = getValueDest(*CS.getCalledValue()).getNode();
Chris Lattnerfbc2d842003-09-24 23:42:58 +0000480 if (Callee == 0) {
481 std::cerr << "WARNING: Program is calling through a null pointer?\n"
482 << *I;
483 return; // Calling a null pointer?
484 }
485 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000486
Chris Lattner0969c502002-10-21 02:08:03 +0000487 std::vector<DSNodeHandle> Args;
Chris Lattner808a7ae2003-09-20 16:34:13 +0000488 Args.reserve(CS.arg_end()-CS.arg_begin());
Chris Lattner0969c502002-10-21 02:08:03 +0000489
490 // Calculate the arguments vector...
Chris Lattner808a7ae2003-09-20 16:34:13 +0000491 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E; ++I)
492 if (isPointerType((*I)->getType()))
493 Args.push_back(getValueDest(**I));
Chris Lattner0969c502002-10-21 02:08:03 +0000494
495 // Add a new function call entry...
Chris Lattner923fc052003-02-05 21:59:58 +0000496 if (Callee)
Chris Lattner26c4fc32003-09-20 21:48:16 +0000497 FunctionCalls->push_back(DSCallSite(CS, RetVal, Callee, Args));
Chris Lattner923fc052003-02-05 21:59:58 +0000498 else
Chris Lattner26c4fc32003-09-20 21:48:16 +0000499 FunctionCalls->push_back(DSCallSite(CS, RetVal, CS.getCalledFunction(),
500 Args));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000501}
Chris Lattner055dc2c2002-07-18 15:54:42 +0000502
Vikram S. Advebac06222002-12-06 21:17:10 +0000503void GraphBuilder::visitFreeInst(FreeInst &FI) {
Vikram S. Advebac06222002-12-06 21:17:10 +0000504 // Mark that the node is written to...
Chris Lattnerbd92b732003-06-19 21:15:11 +0000505 DSNode *N = getValueDest(*FI.getOperand(0)).getNode();
506 N->setModifiedMarker();
507 N->setHeapNodeMarker();
Vikram S. Advebac06222002-12-06 21:17:10 +0000508}
509
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000510/// Handle casts...
511void GraphBuilder::visitCastInst(CastInst &CI) {
Chris Lattner5af344d2002-11-02 00:36:03 +0000512 if (isPointerType(CI.getType()))
513 if (isPointerType(CI.getOperand(0)->getType())) {
514 // Cast one pointer to the other, just act like a copy instruction
Chris Lattner92673292002-11-02 00:13:20 +0000515 setDestTo(CI, getValueDest(*CI.getOperand(0)));
Chris Lattner5af344d2002-11-02 00:36:03 +0000516 } else {
517 // Cast something (floating point, small integer) to a pointer. We need
518 // to track the fact that the node points to SOMETHING, just something we
519 // don't know about. Make an "Unknown" node.
520 //
Chris Lattnerbd92b732003-06-19 21:15:11 +0000521 setDestTo(CI, createNode()->setUnknownNodeMarker());
Chris Lattner5af344d2002-11-02 00:36:03 +0000522 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000523}
Chris Lattner055dc2c2002-07-18 15:54:42 +0000524
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000525
Chris Lattner878e5212003-02-04 00:59:50 +0000526// visitInstruction - For all other instruction types, if we have any arguments
527// that are of pointer type, make them have unknown composition bits, and merge
528// the nodes together.
529void GraphBuilder::visitInstruction(Instruction &Inst) {
530 DSNodeHandle CurNode;
531 if (isPointerType(Inst.getType()))
532 CurNode = getValueDest(Inst);
533 for (User::op_iterator I = Inst.op_begin(), E = Inst.op_end(); I != E; ++I)
534 if (isPointerType((*I)->getType()))
535 CurNode.mergeWith(getValueDest(**I));
536
537 if (CurNode.getNode())
Chris Lattnerbd92b732003-06-19 21:15:11 +0000538 CurNode.getNode()->setUnknownNodeMarker();
Chris Lattner878e5212003-02-04 00:59:50 +0000539}
540
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000541
542
543//===----------------------------------------------------------------------===//
544// LocalDataStructures Implementation
545//===----------------------------------------------------------------------===//
546
Chris Lattner26c4fc32003-09-20 21:48:16 +0000547// MergeConstantInitIntoNode - Merge the specified constant into the node
548// pointed to by NH.
549void GraphBuilder::MergeConstantInitIntoNode(DSNodeHandle &NH, Constant *C) {
550 // Ensure a type-record exists...
551 NH.getNode()->mergeTypeInfo(C->getType(), NH.getOffset());
552
553 if (C->getType()->isFirstClassType()) {
554 if (isPointerType(C->getType()))
555 // Avoid adding edges from null, or processing non-"pointer" stores
556 NH.addEdgeTo(getValueDest(*C));
557 return;
558 }
Chris Lattner15869aa2003-11-02 22:27:28 +0000559
560 const TargetData &TD = NH.getNode()->getTargetData();
561
Chris Lattner26c4fc32003-09-20 21:48:16 +0000562 if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) {
563 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
564 // We don't currently do any indexing for arrays...
565 MergeConstantInitIntoNode(NH, cast<Constant>(CA->getOperand(i)));
566 } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
567 const StructLayout *SL = TD.getStructLayout(CS->getType());
568 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
569 DSNodeHandle NewNH(NH.getNode(), NH.getOffset()+SL->MemberOffsets[i]);
570 MergeConstantInitIntoNode(NewNH, cast<Constant>(CS->getOperand(i)));
571 }
572 } else {
573 assert(0 && "Unknown constant type!");
574 }
575}
576
577void GraphBuilder::mergeInGlobalInitializer(GlobalVariable *GV) {
578 assert(!GV->isExternal() && "Cannot merge in external global!");
579 // Get a node handle to the global node and merge the initializer into it.
580 DSNodeHandle NH = getValueDest(*GV);
581 MergeConstantInitIntoNode(NH, GV->getInitializer());
582}
583
584
Chris Lattneraa0b4682002-11-09 21:12:07 +0000585bool LocalDataStructures::run(Module &M) {
Chris Lattner15869aa2003-11-02 22:27:28 +0000586 GlobalsGraph = new DSGraph(getAnalysis<TargetData>());
587
588 const TargetData &TD = getAnalysis<TargetData>();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000589
590 // Calculate all of the graphs...
591 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
592 if (!I->isExternal())
Chris Lattner15869aa2003-11-02 22:27:28 +0000593 DSInfo.insert(std::make_pair(I, new DSGraph(TD, *I, GlobalsGraph)));
Chris Lattner26c4fc32003-09-20 21:48:16 +0000594
595 GraphBuilder GGB(*GlobalsGraph);
596
597 // Add initializers for all of the globals to the globals graph...
598 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
599 if (!I->isExternal())
600 GGB.mergeInGlobalInitializer(I);
601
602 GlobalsGraph->markIncompleteNodes(DSGraph::MarkFormalArgs);
603 GlobalsGraph->removeTriviallyDeadNodes();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000604 return false;
605}
606
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000607// releaseMemory - If the pass pipeline is done with this pass, we can release
608// our memory... here...
609//
610void LocalDataStructures::releaseMemory() {
Chris Lattner81d924d2003-06-30 04:53:27 +0000611 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
612 E = DSInfo.end(); I != E; ++I) {
613 I->second->getReturnNodes().erase(I->first);
614 if (I->second->getReturnNodes().empty())
615 delete I->second;
616 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000617
618 // Empty map so next time memory is released, data structures are not
619 // re-deleted.
620 DSInfo.clear();
Chris Lattner2e4f9bf2002-11-09 20:01:01 +0000621 delete GlobalsGraph;
622 GlobalsGraph = 0;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000623}
Brian Gaeked0fde302003-11-11 22:41:34 +0000624
625} // End llvm namespace