blob: d5c488f05c31aa61d8b2b7c1a9f5b9c96dfd8eda [file] [log] [blame]
Chris Lattnerc68c31b2002-07-10 22:38:08 +00001//===- ComputeLocal.cpp - Compute a local data structure graph for a fn ---===//
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
Chris Lattner055dc2c2002-07-18 15:54:42 +00008#include "llvm/Analysis/DataStructure.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +00009#include "llvm/Function.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"
Chris Lattner0d9bab82002-07-18 00:12:30 +000015#include "llvm/GlobalVariable.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000016#include "llvm/DerivedTypes.h"
17#include "llvm/Support/InstVisitor.h"
18using std::map;
19using std::vector;
20
Chris Lattner97f51a32002-07-27 01:12:15 +000021static RegisterAnalysis<LocalDataStructures>
22X("datastructure", "Local Data Structure Analysis");
Chris Lattner97f51a32002-07-27 01:12:15 +000023
Chris Lattnerc68c31b2002-07-10 22:38:08 +000024//===----------------------------------------------------------------------===//
25// GraphBuilder Class
26//===----------------------------------------------------------------------===//
27//
28// This class is the builder class that constructs the local data structure
29// graph by performing a single pass over the function in question.
30//
31
32namespace {
33 class GraphBuilder : InstVisitor<GraphBuilder> {
34 DSGraph &G;
35 vector<DSNode*> &Nodes;
36 DSNodeHandle &RetNode; // Node that gets returned...
37 map<Value*, DSNodeHandle> &ValueMap;
38 vector<vector<DSNodeHandle> > &FunctionCalls;
39
40 public:
41 GraphBuilder(DSGraph &g, vector<DSNode*> &nodes, DSNodeHandle &retNode,
42 map<Value*, DSNodeHandle> &vm,
43 vector<vector<DSNodeHandle> > &fc)
44 : G(g), Nodes(nodes), RetNode(retNode), ValueMap(vm), FunctionCalls(fc) {
Chris Lattner0d9bab82002-07-18 00:12:30 +000045
46 // Create scalar nodes for all pointer arguments...
47 for (Function::aiterator I = G.getFunction().abegin(),
48 E = G.getFunction().aend(); I != E; ++I)
49 if (isa<PointerType>(I->getType()))
50 getValueNode(*I);
51
Chris Lattnerc68c31b2002-07-10 22:38:08 +000052 visit(G.getFunction()); // Single pass over the function
Chris Lattner2a2c4902002-07-18 18:19:09 +000053
54 // Not inlining, only eliminate trivially dead nodes.
55 G.removeTriviallyDeadNodes();
Chris Lattnerc68c31b2002-07-10 22:38:08 +000056 }
57
58 private:
59 // Visitor functions, used to handle each instruction type we encounter...
60 friend class InstVisitor<GraphBuilder>;
61 void visitMallocInst(MallocInst &MI) { handleAlloc(MI, DSNode::NewNode); }
62 void visitAllocaInst(AllocaInst &AI) { handleAlloc(AI, DSNode::AllocaNode);}
63 void handleAlloc(AllocationInst &AI, DSNode::NodeTy NT);
64
65 void visitPHINode(PHINode &PN);
66
67 void visitGetElementPtrInst(GetElementPtrInst &GEP);
68 void visitReturnInst(ReturnInst &RI);
69 void visitLoadInst(LoadInst &LI);
70 void visitStoreInst(StoreInst &SI);
71 void visitCallInst(CallInst &CI);
72 void visitSetCondInst(SetCondInst &SCI) {} // SetEQ & friends are ignored
73 void visitFreeInst(FreeInst &FI) {} // Ignore free instructions
Chris Lattner055dc2c2002-07-18 15:54:42 +000074 void visitInstruction(Instruction &I); // Visit unsafe ptr instruction
Chris Lattnerc68c31b2002-07-10 22:38:08 +000075
76 private:
77 // Helper functions used to implement the visitation functions...
78
79 // createNode - Create a new DSNode, ensuring that it is properly added to
80 // the graph.
81 //
82 DSNode *createNode(DSNode::NodeTy NodeType, const Type *Ty);
83
84 // getValueNode - Return a DSNode that corresponds the the specified LLVM
85 // value. This either returns the already existing node, or creates a new
86 // one and adds it to the graph, if none exists.
87 //
88 DSNode *getValueNode(Value &V);
89
Chris Lattner0d9bab82002-07-18 00:12:30 +000090 // getGlobalNode - Just like getValueNode, except the global node itself is
91 // returned, not a scalar node pointing to a global.
92 //
93 DSNode *getGlobalNode(GlobalValue &V);
94
Chris Lattnerc68c31b2002-07-10 22:38:08 +000095 // getLink - This method is used to either return the specified link in the
96 // specified node if one exists. If a link does not already exist (it's
97 // null), then we create a new node, link it, then return it.
98 //
99 DSNode *getLink(DSNode *Node, unsigned Link);
100
101 // getSubscriptedNode - Perform the basic getelementptr functionality that
102 // must be factored out of gep, load and store while they are all MAI's.
103 //
104 DSNode *getSubscriptedNode(MemAccessInst &MAI, DSNode *Ptr);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000105 };
106}
107
108//===----------------------------------------------------------------------===//
109// DSGraph constructor - Simply use the GraphBuilder to construct the local
110// graph.
Vikram S. Adve358fc382002-07-30 22:08:08 +0000111DSGraph::DSGraph(Function &F, GlobalDSGraph* GlobalsG)
112 : Func(F), RetNode(0), GlobalsGraph(GlobalsG) {
113 if (GlobalsGraph != this) {
114 GlobalsGraph->addReference(this);
115 // Use the graph builder to construct the local version of the graph
116 GraphBuilder B(*this, Nodes, RetNode, ValueMap, FunctionCalls);
117 markIncompleteNodes();
118 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000119}
120
121
122//===----------------------------------------------------------------------===//
123// Helper method implementations...
124//
125
126
127// createNode - Create a new DSNode, ensuring that it is properly added to the
128// graph.
129//
130DSNode *GraphBuilder::createNode(DSNode::NodeTy NodeType, const Type *Ty) {
131 DSNode *N = new DSNode(NodeType, Ty);
132 Nodes.push_back(N);
133 return N;
134}
135
136
Chris Lattner0d9bab82002-07-18 00:12:30 +0000137// getGlobalNode - Just like getValueNode, except the global node itself is
138// returned, not a scalar node pointing to a global.
139//
140DSNode *GraphBuilder::getGlobalNode(GlobalValue &V) {
141 DSNodeHandle &NH = ValueMap[&V];
142 if (NH) return NH; // Already have a node? Just return it...
143
144 // Create a new global node for this global variable...
145 DSNode *G = createNode(DSNode::GlobalNode, V.getType()->getElementType());
146 G->addGlobal(&V);
147
148 // If this node has outgoing edges, make sure to recycle the same node for
149 // each use. For functions and other global variables, this is unneccesary,
150 // so avoid excessive merging by cloning these nodes on demand.
151 //
152 NH = G;
153 return G;
154}
155
156
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000157// getValueNode - Return a DSNode that corresponds the the specified LLVM value.
158// This either returns the already existing node, or creates a new one and adds
159// it to the graph, if none exists.
160//
161DSNode *GraphBuilder::getValueNode(Value &V) {
162 assert(isa<PointerType>(V.getType()) && "Should only use pointer scalars!");
Chris Lattnerc314ac42002-07-11 20:32:02 +0000163 if (!isa<GlobalValue>(V)) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000164 DSNodeHandle &NH = ValueMap[&V];
165 if (NH) return NH; // Already have a node? Just return it...
Chris Lattnerc314ac42002-07-11 20:32:02 +0000166 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000167
168 // Otherwise we need to create a new scalar node...
Chris Lattnerc314ac42002-07-11 20:32:02 +0000169 DSNode *N = createNode(DSNode::ScalarNode, V.getType());
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000170
Chris Lattner0d9bab82002-07-18 00:12:30 +0000171 // If this is a global value, create the global pointed to.
Chris Lattnerc314ac42002-07-11 20:32:02 +0000172 if (GlobalValue *GV = dyn_cast<GlobalValue>(&V)) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000173 DSNode *G = getGlobalNode(*GV);
174 N->addEdgeTo(G);
Chris Lattnerc314ac42002-07-11 20:32:02 +0000175 } else {
176 ValueMap[&V] = N;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000177 }
178
179 return N;
180}
181
Chris Lattner0d9bab82002-07-18 00:12:30 +0000182
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000183// getLink - This method is used to either return the specified link in the
184// specified node if one exists. If a link does not already exist (it's
185// null), then we create a new node, link it, then return it.
186//
187DSNode *GraphBuilder::getLink(DSNode *Node, unsigned Link) {
188 assert(Link < Node->getNumLinks() && "Link accessed out of range!");
189 if (Node->getLink(Link) == 0) {
190 DSNode::NodeTy NT;
191 const Type *Ty;
192
193 switch (Node->getType()->getPrimitiveID()) {
194 case Type::PointerTyID:
195 Ty = cast<PointerType>(Node->getType())->getElementType();
196 NT = DSNode::ShadowNode;
197 break;
198 case Type::ArrayTyID:
199 Ty = cast<ArrayType>(Node->getType())->getElementType();
200 NT = DSNode::SubElement;
201 break;
202 case Type::StructTyID:
203 Ty = cast<StructType>(Node->getType())->getContainedType(Link);
204 NT = DSNode::SubElement;
205 break;
206 default:
207 assert(0 && "Unexpected type to dereference!");
208 abort();
209 }
210
211 DSNode *New = createNode(NT, Ty);
212 Node->addEdgeTo(Link, New);
213 }
214
215 return Node->getLink(Link);
216}
217
218// getSubscriptedNode - Perform the basic getelementptr functionality that must
219// be factored out of gep, load and store while they are all MAI's.
220//
221DSNode *GraphBuilder::getSubscriptedNode(MemAccessInst &MAI, DSNode *Ptr) {
222 for (unsigned i = MAI.getFirstIndexOperandNumber(), e = MAI.getNumOperands();
223 i != e; ++i)
224 if (MAI.getOperand(i)->getType() == Type::UIntTy)
225 Ptr = getLink(Ptr, 0);
226 else if (MAI.getOperand(i)->getType() == Type::UByteTy)
227 Ptr = getLink(Ptr, cast<ConstantUInt>(MAI.getOperand(i))->getValue());
228
229 if (MAI.getFirstIndexOperandNumber() == MAI.getNumOperands())
230 Ptr = getLink(Ptr, 0); // All MAI's have an implicit 0 if nothing else.
231
232 return Ptr;
233}
234
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000235//===----------------------------------------------------------------------===//
236// Specific instruction type handler implementations...
237//
238
239// Alloca & Malloc instruction implementation - Simply create a new memory
240// object, pointing the scalar to it.
241//
242void GraphBuilder::handleAlloc(AllocationInst &AI, DSNode::NodeTy NodeType) {
243 DSNode *Scalar = getValueNode(AI);
244 DSNode *New = createNode(NodeType, AI.getAllocatedType());
245 Scalar->addEdgeTo(New); // Make the scalar point to the new node...
246}
247
248// PHINode - Make the scalar for the PHI node point to all of the things the
249// incoming values point to... which effectively causes them to be merged.
250//
251void GraphBuilder::visitPHINode(PHINode &PN) {
252 if (!isa<PointerType>(PN.getType())) return; // Only pointer PHIs
253
Chris Lattnerc314ac42002-07-11 20:32:02 +0000254 DSNode *Scalar = getValueNode(PN);
255 DSNode *ScalarDest = getLink(Scalar, 0);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000256 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
Chris Lattnerc314ac42002-07-11 20:32:02 +0000257 ScalarDest->mergeWith(getLink(getValueNode(*PN.getIncomingValue(i)), 0));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000258}
259
260void GraphBuilder::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000261 DSNode *Ptr = getSubscriptedNode(GEP, getValueNode(*GEP.getOperand(0)));
Chris Lattnerc314ac42002-07-11 20:32:02 +0000262 getValueNode(GEP)->addEdgeTo(Ptr);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000263}
264
265void GraphBuilder::visitLoadInst(LoadInst &LI) {
Chris Lattner24ea74e2002-08-22 22:49:05 +0000266 DSNode *Ptr = getValueNode(*LI.getOperand(0));
267 if (!isa<PointerType>(LI.getType())) return; // only loads OF pointers
Chris Lattnerc314ac42002-07-11 20:32:02 +0000268 getValueNode(LI)->addEdgeTo(getLink(Ptr, 0));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000269}
270
271void GraphBuilder::visitStoreInst(StoreInst &SI) {
Chris Lattner24ea74e2002-08-22 22:49:05 +0000272 DSNode *DestPtr = getValueNode(*SI.getOperand(1));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000273 if (!isa<PointerType>(SI.getOperand(0)->getType())) return;
274 DSNode *Value = getValueNode(*SI.getOperand(0));
Chris Lattnereef796c2002-07-22 16:35:53 +0000275 DestPtr->addEdgeTo(getLink(Value, 0));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000276}
277
278void GraphBuilder::visitReturnInst(ReturnInst &RI) {
279 if (RI.getNumOperands() && isa<PointerType>(RI.getOperand(0)->getType())) {
Chris Lattnerc314ac42002-07-11 20:32:02 +0000280 DSNode *Value = getLink(getValueNode(*RI.getOperand(0)), 0);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000281 Value->mergeWith(RetNode);
282 RetNode = Value;
283 }
284}
285
286void GraphBuilder::visitCallInst(CallInst &CI) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000287 // Add a new function call entry...
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000288 FunctionCalls.push_back(vector<DSNodeHandle>());
289 vector<DSNodeHandle> &Args = FunctionCalls.back();
290
Chris Lattnerc314ac42002-07-11 20:32:02 +0000291 // Set up the return value...
292 if (isa<PointerType>(CI.getType()))
Chris Lattner0d9bab82002-07-18 00:12:30 +0000293 Args.push_back(getLink(getValueNode(CI), 0));
Chris Lattnerc314ac42002-07-11 20:32:02 +0000294 else
295 Args.push_back(0);
296
Chris Lattner0d9bab82002-07-18 00:12:30 +0000297 unsigned Start = 0;
298 // Special case for direct call, avoid creating spurious scalar node...
299 if (GlobalValue *GV = dyn_cast<GlobalValue>(CI.getOperand(0))) {
300 Args.push_back(getGlobalNode(*GV));
301 Start = 1;
302 }
303
Chris Lattnerc314ac42002-07-11 20:32:02 +0000304 // Pass the arguments in...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000305 for (unsigned i = Start, e = CI.getNumOperands(); i != e; ++i)
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000306 if (isa<PointerType>(CI.getOperand(i)->getType()))
Chris Lattner0d9bab82002-07-18 00:12:30 +0000307 Args.push_back(getLink(getValueNode(*CI.getOperand(i)), 0));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000308}
Chris Lattner055dc2c2002-07-18 15:54:42 +0000309
310// visitInstruction - All safe instructions have been processed above, this case
311// is where unsafe ptr instructions land.
312//
313void GraphBuilder::visitInstruction(Instruction &I) {
314 // If the return type is a pointer, mark the pointed node as being a cast node
315 if (isa<PointerType>(I.getType()))
316 getLink(getValueNode(I), 0)->NodeType |= DSNode::CastNode;
317
318 // If any operands are pointers, mark the pointed nodes as being a cast node
319 for (Instruction::op_iterator i = I.op_begin(), E = I.op_end(); i!=E; ++i)
320 if (isa<PointerType>(i->get()->getType()))
321 getLink(getValueNode(*i->get()), 0)->NodeType |= DSNode::CastNode;
322}
323