This fixes all kinds of problems with array handling. There are still bugs to
[oota-llvm.git] / lib / Analysis / DataStructure / Local.cpp
1 //===- Local.cpp - Compute a local data structure graph for a function ----===//
2 //
3 // Compute the local version of the data structure graph for a function.  The
4 // external interface to this file is the DSGraph constructor.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "llvm/Analysis/DSGraph.h"
9 #include "llvm/Analysis/DataStructure.h"
10 #include "llvm/iMemory.h"
11 #include "llvm/iTerminators.h"
12 #include "llvm/iPHINode.h"
13 #include "llvm/iOther.h"
14 #include "llvm/Constants.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/Function.h"
17 #include "llvm/GlobalVariable.h"
18 #include "llvm/Support/InstVisitor.h"
19 #include "llvm/Target/TargetData.h"
20 #include "Support/Statistic.h"
21
22 // FIXME: This should eventually be a FunctionPass that is automatically
23 // aggregated into a Pass.
24 //
25 #include "llvm/Module.h"
26
27 using std::map;
28 using std::vector;
29
30 static RegisterAnalysis<LocalDataStructures>
31 X("datastructure", "Local Data Structure Analysis");
32
33 using namespace DataStructureAnalysis;
34
35 namespace DataStructureAnalysis {
36   // FIXME: Do something smarter with target data!
37   TargetData TD("temp-td");
38   unsigned PointerSize(TD.getPointerSize());
39
40   // isPointerType - Return true if this type is big enough to hold a pointer.
41   bool isPointerType(const Type *Ty) {
42     if (isa<PointerType>(Ty))
43       return true;
44     else if (Ty->isPrimitiveType() && Ty->isInteger())
45       return Ty->getPrimitiveSize() >= PointerSize;
46     return false;
47   }
48 }
49
50
51 namespace {
52   //===--------------------------------------------------------------------===//
53   //  GraphBuilder Class
54   //===--------------------------------------------------------------------===//
55   //
56   /// This class is the builder class that constructs the local data structure
57   /// graph by performing a single pass over the function in question.
58   ///
59   class GraphBuilder : InstVisitor<GraphBuilder> {
60     DSGraph &G;
61     vector<DSNode*> &Nodes;
62     DSNodeHandle &RetNode;               // Node that gets returned...
63     map<Value*, DSNodeHandle> &ValueMap;
64     map<GlobalValue*, DSNodeHandle> GlobalScalarValueMap;
65     vector<DSCallSite> &FunctionCalls;
66
67   public:
68     GraphBuilder(DSGraph &g, vector<DSNode*> &nodes, DSNodeHandle &retNode,
69                  map<Value*, DSNodeHandle> &vm,
70                  vector<DSCallSite> &fc)
71       : G(g), Nodes(nodes), RetNode(retNode), ValueMap(vm), FunctionCalls(fc) {
72
73       // Create scalar nodes for all pointer arguments...
74       for (Function::aiterator I = G.getFunction().abegin(),
75              E = G.getFunction().aend(); I != E; ++I)
76         if (isPointerType(I->getType()))
77           getValueDest(*I);
78
79       visit(G.getFunction());  // Single pass over the function
80
81       // Not inlining, only eliminate trivially dead nodes.
82       G.removeTriviallyDeadNodes();
83     }
84
85   private:
86     // Visitor functions, used to handle each instruction type we encounter...
87     friend class InstVisitor<GraphBuilder>;
88     void visitMallocInst(MallocInst &MI) { handleAlloc(MI, DSNode::NewNode); }
89     void visitAllocaInst(AllocaInst &AI) { handleAlloc(AI, DSNode::AllocaNode);}
90     void handleAlloc(AllocationInst &AI, DSNode::NodeTy NT);
91
92     void visitPHINode(PHINode &PN);
93
94     void visitGetElementPtrInst(GetElementPtrInst &GEP);
95     void visitReturnInst(ReturnInst &RI);
96     void visitLoadInst(LoadInst &LI);
97     void visitStoreInst(StoreInst &SI);
98     void visitCallInst(CallInst &CI);
99     void visitSetCondInst(SetCondInst &SCI) {}  // SetEQ & friends are ignored
100     void visitFreeInst(FreeInst &FI) {}         // Ignore free instructions
101     void visitCastInst(CastInst &CI);
102     void visitInstruction(Instruction &I) {}
103
104   private:
105     // Helper functions used to implement the visitation functions...
106
107     /// createNode - Create a new DSNode, ensuring that it is properly added to
108     /// the graph.
109     ///
110     DSNode *createNode(DSNode::NodeTy NodeType, const Type *Ty);
111
112     /// getValueNode - Return a DSNode that corresponds the the specified LLVM
113     /// value.  This either returns the already existing node, or creates a new
114     /// one and adds it to the graph, if none exists.
115     ///
116     DSNodeHandle &getValueNode(Value &V);
117
118     /// getValueDest - Return the DSNode that the actual value points to.  This
119     /// is basically the same thing as: getLink(getValueNode(V), 0)
120     ///
121     DSNodeHandle &getValueDest(Value &V);
122
123     /// getGlobalNode - Just like getValueNode, except the global node itself is
124     /// returned, not a scalar node pointing to a global.
125     ///
126     DSNodeHandle &getGlobalNode(GlobalValue &V);
127
128     /// getLink - This method is used to return the specified link in the
129     /// specified node if one exists.  If a link does not already exist (it's
130     /// null), then we create a new node, link it, then return it.  We must
131     /// specify the type of the Node field we are accessing so that we know what
132     /// type should be linked to if we need to create a new node.
133     ///
134     DSNodeHandle &getLink(const DSNodeHandle &Node, unsigned Link,
135                           const Type *FieldTy);
136   };
137 }
138
139 //===----------------------------------------------------------------------===//
140 // DSGraph constructor - Simply use the GraphBuilder to construct the local
141 // graph.
142 DSGraph::DSGraph(Function &F) : Func(&F) {
143   // Use the graph builder to construct the local version of the graph
144   GraphBuilder B(*this, Nodes, RetNode, ValueMap, FunctionCalls);
145   markIncompleteNodes();
146 }
147
148
149 //===----------------------------------------------------------------------===//
150 // Helper method implementations...
151 //
152
153
154 // createNode - Create a new DSNode, ensuring that it is properly added to the
155 // graph.
156 //
157 DSNode *GraphBuilder::createNode(DSNode::NodeTy NodeType, const Type *Ty) {
158   DSNode *N = new DSNode(NodeType, Ty);
159   Nodes.push_back(N);
160   return N;
161 }
162
163
164 // getGlobalNode - Just like getValueNode, except the global node itself is
165 // returned, not a scalar node pointing to a global.
166 //
167 DSNodeHandle &GraphBuilder::getGlobalNode(GlobalValue &V) {
168   DSNodeHandle &NH = ValueMap[&V];
169   if (NH.getNode()) return NH;       // Already have a node?  Just return it...
170
171   // Create a new global node for this global variable...
172   DSNode *G = createNode(DSNode::GlobalNode, V.getType()->getElementType());
173   G->addGlobal(&V);
174
175   // If this node has outgoing edges, make sure to recycle the same node for
176   // each use.  For functions and other global variables, this is unneccesary,
177   // so avoid excessive merging by cloning these nodes on demand.
178   //
179   NH.setNode(G);
180   return NH;
181 }
182
183
184 // getValueNode - Return a DSNode that corresponds the the specified LLVM value.
185 // This either returns the already existing node, or creates a new one and adds
186 // it to the graph, if none exists.
187 //
188 DSNodeHandle &GraphBuilder::getValueNode(Value &V) {
189   assert(isPointerType(V.getType()) && "Should only use pointer scalars!");
190
191   if (GlobalValue *GV = dyn_cast<GlobalValue>(&V)) {
192     // The GlobalScalarValueMap keeps track of the scalar nodes that point to
193     // global values...  The ValueMap contains pointers to the global memory
194     // object itself, not the scalar constant that points to the memory.
195     //
196     DSNodeHandle &NH = GlobalScalarValueMap[GV];
197     if (NH.getNode()) return NH;
198
199     // If this is a global value, create the global pointed to.
200     DSNode *N = createNode(DSNode::ScalarNode, V.getType());
201     NH.setOffset(0);
202     NH.setNode(N);
203
204     N->addEdgeTo(0, getGlobalNode(*GV));
205     return NH;
206     
207   } else {
208     DSNodeHandle &NH = ValueMap[&V];
209     if (NH.getNode())
210       return NH;     // Already have a node?  Just return it...
211   
212     // Otherwise we need to create a new scalar node...
213     DSNode *N = createNode(DSNode::ScalarNode, V.getType());
214
215     NH.setOffset(0);
216     NH.setNode(N);
217     return NH;
218   }
219 }
220
221 /// getValueDest - Return the DSNode that the actual value points to.  This
222 /// is basically the same thing as: getLink(getValueNode(V), 0)
223 ///
224 DSNodeHandle &GraphBuilder::getValueDest(Value &V) {
225   return getLink(getValueNode(V), 0, V.getType());
226 }
227
228
229 /// getLink - This method is used to return the specified link in the
230 /// specified node if one exists.  If a link does not already exist (it's
231 /// null), then we create a new node, link it, then return it.  We must
232 /// specify the type of the Node field we are accessing so that we know what
233 /// type should be linked to if we need to create a new node.
234 ///
235 DSNodeHandle &GraphBuilder::getLink(const DSNodeHandle &node,
236                                     unsigned LinkNo,
237                                     const Type *FieldTy  // FIXME: eliminate
238                                     ) {
239   DSNodeHandle &Node = const_cast<DSNodeHandle&>(node);
240   DSNodeHandle *Link = Node.getLink(LinkNo);
241   if (Link) return *Link;
242
243 #if 0    // FIXME: delete
244   // If we are indexing with a typed pointer, then the thing we are pointing
245   // to is of the pointed type.  If we are pointing to it with an integer
246   // (because of cast to an integer), we represent it with a void type.
247   //
248   const Type *ReqTy = 0;
249   if (const PointerType *Ptr = dyn_cast<PointerType>(FieldTy))
250     ReqTy = Ptr->getElementType();
251 #endif
252   
253   // If the link hasn't been created yet, make and return a new shadow node
254   DSNode *N = createNode(DSNode::ShadowNode, 0);
255   Node.setLink(LinkNo, N);
256   return *Node.getLink(LinkNo);
257 }
258
259
260 //===----------------------------------------------------------------------===//
261 // Specific instruction type handler implementations...
262 //
263
264 /// Alloca & Malloc instruction implementation - Simply create a new memory
265 /// object, pointing the scalar to it.
266 ///
267 void GraphBuilder::handleAlloc(AllocationInst &AI, DSNode::NodeTy NodeType) {
268   DSNode *New = createNode(NodeType, 0);
269
270   // Make the scalar point to the new node...
271   getValueNode(AI).addEdgeTo(New);
272 }
273
274 // PHINode - Make the scalar for the PHI node point to all of the things the
275 // incoming values point to... which effectively causes them to be merged.
276 //
277 void GraphBuilder::visitPHINode(PHINode &PN) {
278   if (!isPointerType(PN.getType())) return; // Only pointer PHIs
279
280   DSNodeHandle &ScalarDest = getValueDest(PN);
281   for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
282     if (!isa<ConstantPointerNull>(PN.getIncomingValue(i)))
283       ScalarDest.mergeWith(getValueDest(*PN.getIncomingValue(i)));
284 }
285
286 void GraphBuilder::visitGetElementPtrInst(GetElementPtrInst &GEP) {
287   DSNodeHandle Value = getValueDest(*GEP.getOperand(0));
288
289   unsigned Offset = 0;
290   const PointerType *PTy = cast<PointerType>(GEP.getOperand(0)->getType());
291   const Type *CurTy = PTy->getElementType();
292   DSTypeRec &TopTypeRec =
293     Value.getNode()->getTypeRec(PTy->getElementType(), Value.getOffset());
294
295   // If the node had to be folded... exit quickly
296   if (TopTypeRec.Ty == Type::VoidTy) {
297     getValueNode(GEP).addEdgeTo(Value);  // GEP result points to folded node
298     return;
299   }
300
301   // Handle the pointer index specially...
302   if (GEP.getNumOperands() > 1 &&
303       GEP.getOperand(1) != ConstantSInt::getNullValue(Type::LongTy)) {
304
305     // If we already know this is an array being accessed, don't do anything...
306     if (!TopTypeRec.isArray) {
307       TopTypeRec.isArray = true;
308
309       // If we are treating some inner field pointer as an array, fold the node
310       // up because we cannot handle it right.  This can come because of
311       // something like this:  &((&Pt->X)[1]) == &Pt->Y
312       //
313       if (Value.getOffset()) {
314         // Value is now the pointer we want to GEP to be...
315         Value.getNode()->foldNodeCompletely();
316         getValueNode(GEP).addEdgeTo(Value);  // GEP result points to folded node
317         return;
318       } else {
319         // This is a pointer to the first byte of the node.  Make sure that we
320         // are pointing to the outter most type in the node.
321         // FIXME: We need to check one more case here...
322       }
323     }
324   }
325
326   // All of these subscripts are indexing INTO the elements we have...
327   for (unsigned i = 2, e = GEP.getNumOperands(); i < e; ++i)
328     if (GEP.getOperand(i)->getType() == Type::LongTy) {
329       // Get the type indexing into...
330       const SequentialType *STy = cast<SequentialType>(CurTy);
331       CurTy = STy->getElementType();
332       if (ConstantSInt *CS = dyn_cast<ConstantSInt>(GEP.getOperand(i))) {
333         Offset += CS->getValue()*TD.getTypeSize(CurTy);
334       } else {
335         // Variable index into a node.  We must merge all of the elements of the
336         // sequential type here.
337         if (isa<PointerType>(STy))
338           std::cerr << "Pointer indexing not handled yet!\n";
339         else {
340           const ArrayType *ATy = cast<ArrayType>(STy);
341           unsigned ElSize = TD.getTypeSize(CurTy);
342           DSNode *N = Value.getNode();
343           assert(N && "Value must have a node!");
344           unsigned RawOffset = Offset+Value.getOffset();
345
346           // Loop over all of the elements of the array, merging them into the
347           // zero'th element.
348           for (unsigned i = 1, e = ATy->getNumElements(); i != e; ++i)
349             // Merge all of the byte components of this array element
350             for (unsigned j = 0; j != ElSize; ++j)
351               N->mergeIndexes(RawOffset+j, RawOffset+i*ElSize+j);
352         }
353       }
354     } else if (GEP.getOperand(i)->getType() == Type::UByteTy) {
355       unsigned FieldNo = cast<ConstantUInt>(GEP.getOperand(i))->getValue();
356       const StructType *STy = cast<StructType>(CurTy);
357       Offset += TD.getStructLayout(STy)->MemberOffsets[FieldNo];
358       CurTy = STy->getContainedType(FieldNo);
359     }
360
361   // Add in the offset calculated...
362   Value.setOffset(Value.getOffset()+Offset);
363
364   // Value is now the pointer we want to GEP to be...
365   getValueNode(GEP).addEdgeTo(Value);
366 }
367
368 void GraphBuilder::visitLoadInst(LoadInst &LI) {
369   DSNodeHandle &Ptr = getValueDest(*LI.getOperand(0));
370   Ptr.getNode()->NodeType |= DSNode::Read;
371
372   // Ensure a typerecord exists...
373   Ptr.getNode()->getTypeRec(LI.getType(), Ptr.getOffset());
374   
375   if (isPointerType(LI.getType()))
376     getValueNode(LI).addEdgeTo(getLink(Ptr, 0, LI.getType()));
377 }
378
379 void GraphBuilder::visitStoreInst(StoreInst &SI) {
380   DSNodeHandle &Dest = getValueDest(*SI.getOperand(1));
381   Dest.getNode()->NodeType |= DSNode::Modified;
382   const Type *StoredTy = SI.getOperand(0)->getType();
383
384   // Ensure a typerecord exists...
385   Dest.getNode()->getTypeRec(StoredTy, Dest.getOffset());
386
387   // Avoid adding edges from null, or processing non-"pointer" stores
388   if (isPointerType(StoredTy) &&
389       !isa<ConstantPointerNull>(SI.getOperand(0))) {
390     Dest.addEdgeTo(getValueDest(*SI.getOperand(0)));
391   }
392 }
393
394 void GraphBuilder::visitReturnInst(ReturnInst &RI) {
395   if (RI.getNumOperands() && isPointerType(RI.getOperand(0)->getType()) &&
396       !isa<ConstantPointerNull>(RI.getOperand(0))) {
397     DSNodeHandle &Value = getValueDest(*RI.getOperand(0));
398     Value.mergeWith(RetNode);
399     RetNode = Value;
400   }
401 }
402
403 void GraphBuilder::visitCallInst(CallInst &CI) {
404   // Set up the return value...
405   DSNodeHandle RetVal;
406   if (isPointerType(CI.getType()))
407     RetVal = getLink(getValueNode(CI), 0, CI.getType());
408
409   DSNodeHandle Callee;
410   // Special case for a direct call, avoid creating spurious scalar node...
411   if (GlobalValue *GV = dyn_cast<GlobalValue>(CI.getOperand(0)))
412     Callee = getGlobalNode(*GV);
413   else
414     Callee = getLink(getValueNode(*CI.getOperand(0)), 0,
415                      CI.getOperand(0)->getType());
416
417   std::vector<DSNodeHandle> Args;
418   Args.reserve(CI.getNumOperands()-1);
419
420   // Calculate the arguments vector...
421   for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
422     if (isPointerType(CI.getOperand(i)->getType()))
423       Args.push_back(getLink(getValueNode(*CI.getOperand(i)), 0,
424                              CI.getOperand(i)->getType()));
425
426   // Add a new function call entry...
427   FunctionCalls.push_back(DSCallSite(CI, RetVal, Callee, Args));
428 }
429
430 /// Handle casts...
431 void GraphBuilder::visitCastInst(CastInst &CI) {
432   if (isPointerType(CI.getType()) && isPointerType(CI.getOperand(0)->getType()))
433     getValueNode(CI).addEdgeTo(getLink(getValueNode(*CI.getOperand(0)), 0,
434                                        CI.getOperand(0)->getType()));
435 }
436
437
438
439
440 //===----------------------------------------------------------------------===//
441 // LocalDataStructures Implementation
442 //===----------------------------------------------------------------------===//
443
444 // releaseMemory - If the pass pipeline is done with this pass, we can release
445 // our memory... here...
446 //
447 void LocalDataStructures::releaseMemory() {
448   for (std::map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
449          E = DSInfo.end(); I != E; ++I)
450     delete I->second;
451
452   // Empty map so next time memory is released, data structures are not
453   // re-deleted.
454   DSInfo.clear();
455 }
456
457 bool LocalDataStructures::run(Module &M) {
458   // Calculate all of the graphs...
459   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
460     if (!I->isExternal())
461       DSInfo.insert(std::make_pair(I, new DSGraph(*I)));
462   return false;
463 }