1 //===- Local.cpp - Compute a local data structure graph for a function ----===//
3 // Compute the local version of the data structure graph for a function. The
4 // external interface to this file is the DSGraph constructor.
6 //===----------------------------------------------------------------------===//
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"
22 // FIXME: This should eventually be a FunctionPass that is automatically
23 // aggregated into a Pass.
25 #include "llvm/Module.h"
30 static RegisterAnalysis<LocalDataStructures>
31 X("datastructure", "Local Data Structure Analysis");
33 using namespace DataStructureAnalysis;
35 namespace DataStructureAnalysis {
36 // FIXME: Do something smarter with target data!
37 TargetData TD("temp-td");
38 unsigned PointerSize(TD.getPointerSize());
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))
44 else if (Ty->isPrimitiveType() && Ty->isInteger())
45 return Ty->getPrimitiveSize() >= PointerSize;
52 //===--------------------------------------------------------------------===//
54 //===--------------------------------------------------------------------===//
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.
59 class GraphBuilder : InstVisitor<GraphBuilder> {
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;
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) {
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()))
79 visit(G.getFunction()); // Single pass over the function
81 // Not inlining, only eliminate trivially dead nodes.
82 G.removeTriviallyDeadNodes();
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);
92 void visitPHINode(PHINode &PN);
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) {}
105 // Helper functions used to implement the visitation functions...
107 /// createNode - Create a new DSNode, ensuring that it is properly added to
110 DSNode *createNode(DSNode::NodeTy NodeType, const Type *Ty);
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.
116 DSNodeHandle &getValueNode(Value &V);
118 /// getValueDest - Return the DSNode that the actual value points to. This
119 /// is basically the same thing as: getLink(getValueNode(V), 0)
121 DSNodeHandle &getValueDest(Value &V);
123 /// getGlobalNode - Just like getValueNode, except the global node itself is
124 /// returned, not a scalar node pointing to a global.
126 DSNodeHandle &getGlobalNode(GlobalValue &V);
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.
134 DSNodeHandle &getLink(const DSNodeHandle &Node, unsigned Link,
135 const Type *FieldTy);
139 //===----------------------------------------------------------------------===//
140 // DSGraph constructor - Simply use the GraphBuilder to construct the local
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();
149 //===----------------------------------------------------------------------===//
150 // Helper method implementations...
154 // createNode - Create a new DSNode, ensuring that it is properly added to the
157 DSNode *GraphBuilder::createNode(DSNode::NodeTy NodeType, const Type *Ty) {
158 DSNode *N = new DSNode(NodeType, Ty);
164 // getGlobalNode - Just like getValueNode, except the global node itself is
165 // returned, not a scalar node pointing to a global.
167 DSNodeHandle &GraphBuilder::getGlobalNode(GlobalValue &V) {
168 DSNodeHandle &NH = ValueMap[&V];
169 if (NH.getNode()) return NH; // Already have a node? Just return it...
171 // Create a new global node for this global variable...
172 DSNode *G = createNode(DSNode::GlobalNode, V.getType()->getElementType());
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.
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.
188 DSNodeHandle &GraphBuilder::getValueNode(Value &V) {
189 assert(isPointerType(V.getType()) && "Should only use pointer scalars!");
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.
196 DSNodeHandle &NH = GlobalScalarValueMap[GV];
197 if (NH.getNode()) return NH;
199 // If this is a global value, create the global pointed to.
200 DSNode *N = createNode(DSNode::ScalarNode, V.getType());
204 N->addEdgeTo(0, getGlobalNode(*GV));
208 DSNodeHandle &NH = ValueMap[&V];
210 return NH; // Already have a node? Just return it...
212 // Otherwise we need to create a new scalar node...
213 DSNode *N = createNode(DSNode::ScalarNode, V.getType());
221 /// getValueDest - Return the DSNode that the actual value points to. This
222 /// is basically the same thing as: getLink(getValueNode(V), 0)
224 DSNodeHandle &GraphBuilder::getValueDest(Value &V) {
225 return getLink(getValueNode(V), 0, V.getType());
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.
235 DSNodeHandle &GraphBuilder::getLink(const DSNodeHandle &node,
237 const Type *FieldTy // FIXME: eliminate
239 DSNodeHandle &Node = const_cast<DSNodeHandle&>(node);
240 DSNodeHandle *Link = Node.getLink(LinkNo);
241 if (Link) return *Link;
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.
248 const Type *ReqTy = 0;
249 if (const PointerType *Ptr = dyn_cast<PointerType>(FieldTy))
250 ReqTy = Ptr->getElementType();
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);
260 //===----------------------------------------------------------------------===//
261 // Specific instruction type handler implementations...
264 /// Alloca & Malloc instruction implementation - Simply create a new memory
265 /// object, pointing the scalar to it.
267 void GraphBuilder::handleAlloc(AllocationInst &AI, DSNode::NodeTy NodeType) {
268 DSNode *New = createNode(NodeType, 0);
270 // Make the scalar point to the new node...
271 getValueNode(AI).addEdgeTo(New);
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.
277 void GraphBuilder::visitPHINode(PHINode &PN) {
278 if (!isPointerType(PN.getType())) return; // Only pointer PHIs
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)));
286 void GraphBuilder::visitGetElementPtrInst(GetElementPtrInst &GEP) {
287 DSNodeHandle Value = getValueDest(*GEP.getOperand(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());
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
301 // Handle the pointer index specially...
302 if (GEP.getNumOperands() > 1 &&
303 GEP.getOperand(1) != ConstantSInt::getNullValue(Type::LongTy)) {
305 // If we already know this is an array being accessed, don't do anything...
306 if (!TopTypeRec.isArray) {
307 TopTypeRec.isArray = true;
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
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
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...
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);
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";
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();
346 // Loop over all of the elements of the array, merging them into the
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);
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);
361 // Add in the offset calculated...
362 Value.setOffset(Value.getOffset()+Offset);
364 // Value is now the pointer we want to GEP to be...
365 getValueNode(GEP).addEdgeTo(Value);
368 void GraphBuilder::visitLoadInst(LoadInst &LI) {
369 DSNodeHandle &Ptr = getValueDest(*LI.getOperand(0));
370 Ptr.getNode()->NodeType |= DSNode::Read;
372 // Ensure a typerecord exists...
373 Ptr.getNode()->getTypeRec(LI.getType(), Ptr.getOffset());
375 if (isPointerType(LI.getType()))
376 getValueNode(LI).addEdgeTo(getLink(Ptr, 0, LI.getType()));
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();
384 // Ensure a typerecord exists...
385 Dest.getNode()->getTypeRec(StoredTy, Dest.getOffset());
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)));
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);
403 void GraphBuilder::visitCallInst(CallInst &CI) {
404 // Set up the return value...
406 if (isPointerType(CI.getType()))
407 RetVal = getLink(getValueNode(CI), 0, CI.getType());
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);
414 Callee = getLink(getValueNode(*CI.getOperand(0)), 0,
415 CI.getOperand(0)->getType());
417 std::vector<DSNodeHandle> Args;
418 Args.reserve(CI.getNumOperands()-1);
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()));
426 // Add a new function call entry...
427 FunctionCalls.push_back(DSCallSite(CI, RetVal, Callee, Args));
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()));
440 //===----------------------------------------------------------------------===//
441 // LocalDataStructures Implementation
442 //===----------------------------------------------------------------------===//
444 // releaseMemory - If the pass pipeline is done with this pass, we can release
445 // our memory... here...
447 void LocalDataStructures::releaseMemory() {
448 for (std::map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
449 E = DSInfo.end(); I != E; ++I)
452 // Empty map so next time memory is released, data structures are not
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)));