blob: 296195cb1f9942a43e6f158aedefd81bfd874284 [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");
23AnalysisID LocalDataStructures::ID = X;
24
Chris Lattnerc68c31b2002-07-10 22:38:08 +000025//===----------------------------------------------------------------------===//
26// GraphBuilder Class
27//===----------------------------------------------------------------------===//
28//
29// This class is the builder class that constructs the local data structure
30// graph by performing a single pass over the function in question.
31//
32
33namespace {
34 class GraphBuilder : InstVisitor<GraphBuilder> {
35 DSGraph &G;
36 vector<DSNode*> &Nodes;
37 DSNodeHandle &RetNode; // Node that gets returned...
38 map<Value*, DSNodeHandle> &ValueMap;
39 vector<vector<DSNodeHandle> > &FunctionCalls;
40
41 public:
42 GraphBuilder(DSGraph &g, vector<DSNode*> &nodes, DSNodeHandle &retNode,
43 map<Value*, DSNodeHandle> &vm,
44 vector<vector<DSNodeHandle> > &fc)
45 : G(g), Nodes(nodes), RetNode(retNode), ValueMap(vm), FunctionCalls(fc) {
Chris Lattner0d9bab82002-07-18 00:12:30 +000046
47 // Create scalar nodes for all pointer arguments...
48 for (Function::aiterator I = G.getFunction().abegin(),
49 E = G.getFunction().aend(); I != E; ++I)
50 if (isa<PointerType>(I->getType()))
51 getValueNode(*I);
52
Chris Lattnerc68c31b2002-07-10 22:38:08 +000053 visit(G.getFunction()); // Single pass over the function
Chris Lattner2a2c4902002-07-18 18:19:09 +000054
55 // Not inlining, only eliminate trivially dead nodes.
56 G.removeTriviallyDeadNodes();
Chris Lattnerc68c31b2002-07-10 22:38:08 +000057 }
58
59 private:
60 // Visitor functions, used to handle each instruction type we encounter...
61 friend class InstVisitor<GraphBuilder>;
62 void visitMallocInst(MallocInst &MI) { handleAlloc(MI, DSNode::NewNode); }
63 void visitAllocaInst(AllocaInst &AI) { handleAlloc(AI, DSNode::AllocaNode);}
64 void handleAlloc(AllocationInst &AI, DSNode::NodeTy NT);
65
66 void visitPHINode(PHINode &PN);
67
68 void visitGetElementPtrInst(GetElementPtrInst &GEP);
69 void visitReturnInst(ReturnInst &RI);
70 void visitLoadInst(LoadInst &LI);
71 void visitStoreInst(StoreInst &SI);
72 void visitCallInst(CallInst &CI);
73 void visitSetCondInst(SetCondInst &SCI) {} // SetEQ & friends are ignored
74 void visitFreeInst(FreeInst &FI) {} // Ignore free instructions
Chris Lattner055dc2c2002-07-18 15:54:42 +000075 void visitInstruction(Instruction &I); // Visit unsafe ptr instruction
Chris Lattnerc68c31b2002-07-10 22:38:08 +000076
77 private:
78 // Helper functions used to implement the visitation functions...
79
80 // createNode - Create a new DSNode, ensuring that it is properly added to
81 // the graph.
82 //
83 DSNode *createNode(DSNode::NodeTy NodeType, const Type *Ty);
84
85 // getValueNode - Return a DSNode that corresponds the the specified LLVM
86 // value. This either returns the already existing node, or creates a new
87 // one and adds it to the graph, if none exists.
88 //
89 DSNode *getValueNode(Value &V);
90
Chris Lattner0d9bab82002-07-18 00:12:30 +000091 // getGlobalNode - Just like getValueNode, except the global node itself is
92 // returned, not a scalar node pointing to a global.
93 //
94 DSNode *getGlobalNode(GlobalValue &V);
95
Chris Lattnerc68c31b2002-07-10 22:38:08 +000096 // getLink - This method is used to either return the specified link in the
97 // specified node if one exists. If a link does not already exist (it's
98 // null), then we create a new node, link it, then return it.
99 //
100 DSNode *getLink(DSNode *Node, unsigned Link);
101
102 // getSubscriptedNode - Perform the basic getelementptr functionality that
103 // must be factored out of gep, load and store while they are all MAI's.
104 //
105 DSNode *getSubscriptedNode(MemAccessInst &MAI, DSNode *Ptr);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000106 };
107}
108
109//===----------------------------------------------------------------------===//
110// DSGraph constructor - Simply use the GraphBuilder to construct the local
111// graph.
112DSGraph::DSGraph(Function &F) : Func(F), RetNode(0) {
113 // Use the graph builder to construct the local version of the graph
114 GraphBuilder B(*this, Nodes, RetNode, ValueMap, FunctionCalls);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000115 markIncompleteNodes();
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000116}
117
118
119//===----------------------------------------------------------------------===//
120// Helper method implementations...
121//
122
123
124// createNode - Create a new DSNode, ensuring that it is properly added to the
125// graph.
126//
127DSNode *GraphBuilder::createNode(DSNode::NodeTy NodeType, const Type *Ty) {
128 DSNode *N = new DSNode(NodeType, Ty);
129 Nodes.push_back(N);
130 return N;
131}
132
133
Chris Lattner0d9bab82002-07-18 00:12:30 +0000134// getGlobalNode - Just like getValueNode, except the global node itself is
135// returned, not a scalar node pointing to a global.
136//
137DSNode *GraphBuilder::getGlobalNode(GlobalValue &V) {
138 DSNodeHandle &NH = ValueMap[&V];
139 if (NH) return NH; // Already have a node? Just return it...
140
141 // Create a new global node for this global variable...
142 DSNode *G = createNode(DSNode::GlobalNode, V.getType()->getElementType());
143 G->addGlobal(&V);
144
145 // If this node has outgoing edges, make sure to recycle the same node for
146 // each use. For functions and other global variables, this is unneccesary,
147 // so avoid excessive merging by cloning these nodes on demand.
148 //
149 NH = G;
150 return G;
151}
152
153
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000154// getValueNode - Return a DSNode that corresponds the the specified LLVM value.
155// This either returns the already existing node, or creates a new one and adds
156// it to the graph, if none exists.
157//
158DSNode *GraphBuilder::getValueNode(Value &V) {
159 assert(isa<PointerType>(V.getType()) && "Should only use pointer scalars!");
Chris Lattnerc314ac42002-07-11 20:32:02 +0000160 if (!isa<GlobalValue>(V)) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000161 DSNodeHandle &NH = ValueMap[&V];
162 if (NH) return NH; // Already have a node? Just return it...
Chris Lattnerc314ac42002-07-11 20:32:02 +0000163 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000164
165 // Otherwise we need to create a new scalar node...
Chris Lattnerc314ac42002-07-11 20:32:02 +0000166 DSNode *N = createNode(DSNode::ScalarNode, V.getType());
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000167
Chris Lattner0d9bab82002-07-18 00:12:30 +0000168 // If this is a global value, create the global pointed to.
Chris Lattnerc314ac42002-07-11 20:32:02 +0000169 if (GlobalValue *GV = dyn_cast<GlobalValue>(&V)) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000170 DSNode *G = getGlobalNode(*GV);
171 N->addEdgeTo(G);
Chris Lattnerc314ac42002-07-11 20:32:02 +0000172 } else {
173 ValueMap[&V] = N;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000174 }
175
176 return N;
177}
178
Chris Lattner0d9bab82002-07-18 00:12:30 +0000179
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000180// getLink - This method is used to either return the specified link in the
181// specified node if one exists. If a link does not already exist (it's
182// null), then we create a new node, link it, then return it.
183//
184DSNode *GraphBuilder::getLink(DSNode *Node, unsigned Link) {
185 assert(Link < Node->getNumLinks() && "Link accessed out of range!");
186 if (Node->getLink(Link) == 0) {
187 DSNode::NodeTy NT;
188 const Type *Ty;
189
190 switch (Node->getType()->getPrimitiveID()) {
191 case Type::PointerTyID:
192 Ty = cast<PointerType>(Node->getType())->getElementType();
193 NT = DSNode::ShadowNode;
194 break;
195 case Type::ArrayTyID:
196 Ty = cast<ArrayType>(Node->getType())->getElementType();
197 NT = DSNode::SubElement;
198 break;
199 case Type::StructTyID:
200 Ty = cast<StructType>(Node->getType())->getContainedType(Link);
201 NT = DSNode::SubElement;
202 break;
203 default:
204 assert(0 && "Unexpected type to dereference!");
205 abort();
206 }
207
208 DSNode *New = createNode(NT, Ty);
209 Node->addEdgeTo(Link, New);
210 }
211
212 return Node->getLink(Link);
213}
214
215// getSubscriptedNode - Perform the basic getelementptr functionality that must
216// be factored out of gep, load and store while they are all MAI's.
217//
218DSNode *GraphBuilder::getSubscriptedNode(MemAccessInst &MAI, DSNode *Ptr) {
219 for (unsigned i = MAI.getFirstIndexOperandNumber(), e = MAI.getNumOperands();
220 i != e; ++i)
221 if (MAI.getOperand(i)->getType() == Type::UIntTy)
222 Ptr = getLink(Ptr, 0);
223 else if (MAI.getOperand(i)->getType() == Type::UByteTy)
224 Ptr = getLink(Ptr, cast<ConstantUInt>(MAI.getOperand(i))->getValue());
225
226 if (MAI.getFirstIndexOperandNumber() == MAI.getNumOperands())
227 Ptr = getLink(Ptr, 0); // All MAI's have an implicit 0 if nothing else.
228
229 return Ptr;
230}
231
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000232//===----------------------------------------------------------------------===//
233// Specific instruction type handler implementations...
234//
235
236// Alloca & Malloc instruction implementation - Simply create a new memory
237// object, pointing the scalar to it.
238//
239void GraphBuilder::handleAlloc(AllocationInst &AI, DSNode::NodeTy NodeType) {
240 DSNode *Scalar = getValueNode(AI);
241 DSNode *New = createNode(NodeType, AI.getAllocatedType());
242 Scalar->addEdgeTo(New); // Make the scalar point to the new node...
243}
244
245// PHINode - Make the scalar for the PHI node point to all of the things the
246// incoming values point to... which effectively causes them to be merged.
247//
248void GraphBuilder::visitPHINode(PHINode &PN) {
249 if (!isa<PointerType>(PN.getType())) return; // Only pointer PHIs
250
Chris Lattnerc314ac42002-07-11 20:32:02 +0000251 DSNode *Scalar = getValueNode(PN);
252 DSNode *ScalarDest = getLink(Scalar, 0);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000253 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
Chris Lattnerc314ac42002-07-11 20:32:02 +0000254 ScalarDest->mergeWith(getLink(getValueNode(*PN.getIncomingValue(i)), 0));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000255}
256
257void GraphBuilder::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000258 DSNode *Ptr = getSubscriptedNode(GEP, getValueNode(*GEP.getOperand(0)));
Chris Lattnerc314ac42002-07-11 20:32:02 +0000259 getValueNode(GEP)->addEdgeTo(Ptr);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000260}
261
262void GraphBuilder::visitLoadInst(LoadInst &LI) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000263 DSNode *Ptr = getSubscriptedNode(LI, getValueNode(*LI.getOperand(0)));
Chris Lattnereef796c2002-07-22 16:35:53 +0000264 if (!isa<PointerType>(LI.getType())) return; // Only pointer PHIs
Chris Lattnerc314ac42002-07-11 20:32:02 +0000265 getValueNode(LI)->addEdgeTo(getLink(Ptr, 0));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000266}
267
268void GraphBuilder::visitStoreInst(StoreInst &SI) {
Chris Lattnereef796c2002-07-22 16:35:53 +0000269 DSNode *DestPtr = getSubscriptedNode(SI, getValueNode(*SI.getOperand(1)));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000270 if (!isa<PointerType>(SI.getOperand(0)->getType())) return;
271 DSNode *Value = getValueNode(*SI.getOperand(0));
Chris Lattnereef796c2002-07-22 16:35:53 +0000272 DestPtr->addEdgeTo(getLink(Value, 0));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000273}
274
275void GraphBuilder::visitReturnInst(ReturnInst &RI) {
276 if (RI.getNumOperands() && isa<PointerType>(RI.getOperand(0)->getType())) {
Chris Lattnerc314ac42002-07-11 20:32:02 +0000277 DSNode *Value = getLink(getValueNode(*RI.getOperand(0)), 0);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000278 Value->mergeWith(RetNode);
279 RetNode = Value;
280 }
281}
282
283void GraphBuilder::visitCallInst(CallInst &CI) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000284 // Add a new function call entry...
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000285 FunctionCalls.push_back(vector<DSNodeHandle>());
286 vector<DSNodeHandle> &Args = FunctionCalls.back();
287
Chris Lattnerc314ac42002-07-11 20:32:02 +0000288 // Set up the return value...
289 if (isa<PointerType>(CI.getType()))
Chris Lattner0d9bab82002-07-18 00:12:30 +0000290 Args.push_back(getLink(getValueNode(CI), 0));
Chris Lattnerc314ac42002-07-11 20:32:02 +0000291 else
292 Args.push_back(0);
293
Chris Lattner0d9bab82002-07-18 00:12:30 +0000294 unsigned Start = 0;
295 // Special case for direct call, avoid creating spurious scalar node...
296 if (GlobalValue *GV = dyn_cast<GlobalValue>(CI.getOperand(0))) {
297 Args.push_back(getGlobalNode(*GV));
298 Start = 1;
299 }
300
Chris Lattnerc314ac42002-07-11 20:32:02 +0000301 // Pass the arguments in...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000302 for (unsigned i = Start, e = CI.getNumOperands(); i != e; ++i)
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000303 if (isa<PointerType>(CI.getOperand(i)->getType()))
Chris Lattner0d9bab82002-07-18 00:12:30 +0000304 Args.push_back(getLink(getValueNode(*CI.getOperand(i)), 0));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000305}
Chris Lattner055dc2c2002-07-18 15:54:42 +0000306
307// visitInstruction - All safe instructions have been processed above, this case
308// is where unsafe ptr instructions land.
309//
310void GraphBuilder::visitInstruction(Instruction &I) {
311 // If the return type is a pointer, mark the pointed node as being a cast node
312 if (isa<PointerType>(I.getType()))
313 getLink(getValueNode(I), 0)->NodeType |= DSNode::CastNode;
314
315 // If any operands are pointers, mark the pointed nodes as being a cast node
316 for (Instruction::op_iterator i = I.op_begin(), E = I.op_end(); i!=E; ++i)
317 if (isa<PointerType>(i->get()->getType()))
318 getLink(getValueNode(*i->get()), 0)->NodeType |= DSNode::CastNode;
319}
320