blob: f9e6a994c684d2facd2652e78feb40e769fa737c [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
21//===----------------------------------------------------------------------===//
22// GraphBuilder Class
23//===----------------------------------------------------------------------===//
24//
25// This class is the builder class that constructs the local data structure
26// graph by performing a single pass over the function in question.
27//
28
29namespace {
30 class GraphBuilder : InstVisitor<GraphBuilder> {
31 DSGraph &G;
32 vector<DSNode*> &Nodes;
33 DSNodeHandle &RetNode; // Node that gets returned...
34 map<Value*, DSNodeHandle> &ValueMap;
35 vector<vector<DSNodeHandle> > &FunctionCalls;
36
37 public:
38 GraphBuilder(DSGraph &g, vector<DSNode*> &nodes, DSNodeHandle &retNode,
39 map<Value*, DSNodeHandle> &vm,
40 vector<vector<DSNodeHandle> > &fc)
41 : G(g), Nodes(nodes), RetNode(retNode), ValueMap(vm), FunctionCalls(fc) {
Chris Lattner0d9bab82002-07-18 00:12:30 +000042
43 // Create scalar nodes for all pointer arguments...
44 for (Function::aiterator I = G.getFunction().abegin(),
45 E = G.getFunction().aend(); I != E; ++I)
46 if (isa<PointerType>(I->getType()))
47 getValueNode(*I);
48
Chris Lattnerc68c31b2002-07-10 22:38:08 +000049 visit(G.getFunction()); // Single pass over the function
Chris Lattner0d9bab82002-07-18 00:12:30 +000050 G.removeDeadNodes();
Chris Lattnerc68c31b2002-07-10 22:38:08 +000051 }
52
53 private:
54 // Visitor functions, used to handle each instruction type we encounter...
55 friend class InstVisitor<GraphBuilder>;
56 void visitMallocInst(MallocInst &MI) { handleAlloc(MI, DSNode::NewNode); }
57 void visitAllocaInst(AllocaInst &AI) { handleAlloc(AI, DSNode::AllocaNode);}
58 void handleAlloc(AllocationInst &AI, DSNode::NodeTy NT);
59
60 void visitPHINode(PHINode &PN);
61
62 void visitGetElementPtrInst(GetElementPtrInst &GEP);
63 void visitReturnInst(ReturnInst &RI);
64 void visitLoadInst(LoadInst &LI);
65 void visitStoreInst(StoreInst &SI);
66 void visitCallInst(CallInst &CI);
67 void visitSetCondInst(SetCondInst &SCI) {} // SetEQ & friends are ignored
68 void visitFreeInst(FreeInst &FI) {} // Ignore free instructions
Chris Lattner055dc2c2002-07-18 15:54:42 +000069 void visitInstruction(Instruction &I); // Visit unsafe ptr instruction
Chris Lattnerc68c31b2002-07-10 22:38:08 +000070
71 private:
72 // Helper functions used to implement the visitation functions...
73
74 // createNode - Create a new DSNode, ensuring that it is properly added to
75 // the graph.
76 //
77 DSNode *createNode(DSNode::NodeTy NodeType, const Type *Ty);
78
79 // getValueNode - Return a DSNode that corresponds the the specified LLVM
80 // value. This either returns the already existing node, or creates a new
81 // one and adds it to the graph, if none exists.
82 //
83 DSNode *getValueNode(Value &V);
84
Chris Lattner0d9bab82002-07-18 00:12:30 +000085 // getGlobalNode - Just like getValueNode, except the global node itself is
86 // returned, not a scalar node pointing to a global.
87 //
88 DSNode *getGlobalNode(GlobalValue &V);
89
Chris Lattnerc68c31b2002-07-10 22:38:08 +000090 // getLink - This method is used to either return the specified link in the
91 // specified node if one exists. If a link does not already exist (it's
92 // null), then we create a new node, link it, then return it.
93 //
94 DSNode *getLink(DSNode *Node, unsigned Link);
95
96 // getSubscriptedNode - Perform the basic getelementptr functionality that
97 // must be factored out of gep, load and store while they are all MAI's.
98 //
99 DSNode *getSubscriptedNode(MemAccessInst &MAI, DSNode *Ptr);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000100 };
101}
102
103//===----------------------------------------------------------------------===//
104// DSGraph constructor - Simply use the GraphBuilder to construct the local
105// graph.
106DSGraph::DSGraph(Function &F) : Func(F), RetNode(0) {
107 // Use the graph builder to construct the local version of the graph
108 GraphBuilder B(*this, Nodes, RetNode, ValueMap, FunctionCalls);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000109 markIncompleteNodes();
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000110}
111
112
113//===----------------------------------------------------------------------===//
114// Helper method implementations...
115//
116
117
118// createNode - Create a new DSNode, ensuring that it is properly added to the
119// graph.
120//
121DSNode *GraphBuilder::createNode(DSNode::NodeTy NodeType, const Type *Ty) {
122 DSNode *N = new DSNode(NodeType, Ty);
123 Nodes.push_back(N);
124 return N;
125}
126
127
Chris Lattner0d9bab82002-07-18 00:12:30 +0000128// getGlobalNode - Just like getValueNode, except the global node itself is
129// returned, not a scalar node pointing to a global.
130//
131DSNode *GraphBuilder::getGlobalNode(GlobalValue &V) {
132 DSNodeHandle &NH = ValueMap[&V];
133 if (NH) return NH; // Already have a node? Just return it...
134
135 // Create a new global node for this global variable...
136 DSNode *G = createNode(DSNode::GlobalNode, V.getType()->getElementType());
137 G->addGlobal(&V);
138
139 // If this node has outgoing edges, make sure to recycle the same node for
140 // each use. For functions and other global variables, this is unneccesary,
141 // so avoid excessive merging by cloning these nodes on demand.
142 //
143 NH = G;
144 return G;
145}
146
147
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000148// getValueNode - Return a DSNode that corresponds the the specified LLVM value.
149// This either returns the already existing node, or creates a new one and adds
150// it to the graph, if none exists.
151//
152DSNode *GraphBuilder::getValueNode(Value &V) {
153 assert(isa<PointerType>(V.getType()) && "Should only use pointer scalars!");
Chris Lattnerc314ac42002-07-11 20:32:02 +0000154 if (!isa<GlobalValue>(V)) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000155 DSNodeHandle &NH = ValueMap[&V];
156 if (NH) return NH; // Already have a node? Just return it...
Chris Lattnerc314ac42002-07-11 20:32:02 +0000157 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000158
159 // Otherwise we need to create a new scalar node...
Chris Lattnerc314ac42002-07-11 20:32:02 +0000160 DSNode *N = createNode(DSNode::ScalarNode, V.getType());
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000161
Chris Lattner0d9bab82002-07-18 00:12:30 +0000162 // If this is a global value, create the global pointed to.
Chris Lattnerc314ac42002-07-11 20:32:02 +0000163 if (GlobalValue *GV = dyn_cast<GlobalValue>(&V)) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000164 DSNode *G = getGlobalNode(*GV);
165 N->addEdgeTo(G);
Chris Lattnerc314ac42002-07-11 20:32:02 +0000166 } else {
167 ValueMap[&V] = N;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000168 }
169
170 return N;
171}
172
Chris Lattner0d9bab82002-07-18 00:12:30 +0000173
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000174// getLink - This method is used to either return the specified link in the
175// specified node if one exists. If a link does not already exist (it's
176// null), then we create a new node, link it, then return it.
177//
178DSNode *GraphBuilder::getLink(DSNode *Node, unsigned Link) {
179 assert(Link < Node->getNumLinks() && "Link accessed out of range!");
180 if (Node->getLink(Link) == 0) {
181 DSNode::NodeTy NT;
182 const Type *Ty;
183
184 switch (Node->getType()->getPrimitiveID()) {
185 case Type::PointerTyID:
186 Ty = cast<PointerType>(Node->getType())->getElementType();
187 NT = DSNode::ShadowNode;
188 break;
189 case Type::ArrayTyID:
190 Ty = cast<ArrayType>(Node->getType())->getElementType();
191 NT = DSNode::SubElement;
192 break;
193 case Type::StructTyID:
194 Ty = cast<StructType>(Node->getType())->getContainedType(Link);
195 NT = DSNode::SubElement;
196 break;
197 default:
198 assert(0 && "Unexpected type to dereference!");
199 abort();
200 }
201
202 DSNode *New = createNode(NT, Ty);
203 Node->addEdgeTo(Link, New);
204 }
205
206 return Node->getLink(Link);
207}
208
209// getSubscriptedNode - Perform the basic getelementptr functionality that must
210// be factored out of gep, load and store while they are all MAI's.
211//
212DSNode *GraphBuilder::getSubscriptedNode(MemAccessInst &MAI, DSNode *Ptr) {
213 for (unsigned i = MAI.getFirstIndexOperandNumber(), e = MAI.getNumOperands();
214 i != e; ++i)
215 if (MAI.getOperand(i)->getType() == Type::UIntTy)
216 Ptr = getLink(Ptr, 0);
217 else if (MAI.getOperand(i)->getType() == Type::UByteTy)
218 Ptr = getLink(Ptr, cast<ConstantUInt>(MAI.getOperand(i))->getValue());
219
220 if (MAI.getFirstIndexOperandNumber() == MAI.getNumOperands())
221 Ptr = getLink(Ptr, 0); // All MAI's have an implicit 0 if nothing else.
222
223 return Ptr;
224}
225
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000226//===----------------------------------------------------------------------===//
227// Specific instruction type handler implementations...
228//
229
230// Alloca & Malloc instruction implementation - Simply create a new memory
231// object, pointing the scalar to it.
232//
233void GraphBuilder::handleAlloc(AllocationInst &AI, DSNode::NodeTy NodeType) {
234 DSNode *Scalar = getValueNode(AI);
235 DSNode *New = createNode(NodeType, AI.getAllocatedType());
236 Scalar->addEdgeTo(New); // Make the scalar point to the new node...
237}
238
239// PHINode - Make the scalar for the PHI node point to all of the things the
240// incoming values point to... which effectively causes them to be merged.
241//
242void GraphBuilder::visitPHINode(PHINode &PN) {
243 if (!isa<PointerType>(PN.getType())) return; // Only pointer PHIs
244
Chris Lattnerc314ac42002-07-11 20:32:02 +0000245 DSNode *Scalar = getValueNode(PN);
246 DSNode *ScalarDest = getLink(Scalar, 0);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000247 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
Chris Lattnerc314ac42002-07-11 20:32:02 +0000248 ScalarDest->mergeWith(getLink(getValueNode(*PN.getIncomingValue(i)), 0));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000249}
250
251void GraphBuilder::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000252 DSNode *Ptr = getSubscriptedNode(GEP, getValueNode(*GEP.getOperand(0)));
Chris Lattnerc314ac42002-07-11 20:32:02 +0000253 getValueNode(GEP)->addEdgeTo(Ptr);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000254}
255
256void GraphBuilder::visitLoadInst(LoadInst &LI) {
257 if (!isa<PointerType>(LI.getType())) return; // Only pointer PHIs
Chris Lattner0d9bab82002-07-18 00:12:30 +0000258 DSNode *Ptr = getSubscriptedNode(LI, getValueNode(*LI.getOperand(0)));
Chris Lattnerc314ac42002-07-11 20:32:02 +0000259 getValueNode(LI)->addEdgeTo(getLink(Ptr, 0));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000260}
261
262void GraphBuilder::visitStoreInst(StoreInst &SI) {
263 if (!isa<PointerType>(SI.getOperand(0)->getType())) return;
264 DSNode *Value = getValueNode(*SI.getOperand(0));
265 DSNode *DestPtr = getValueNode(*SI.getOperand(1));
Chris Lattnerc314ac42002-07-11 20:32:02 +0000266 getSubscriptedNode(SI, DestPtr)->addEdgeTo(getLink(Value, 0));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000267}
268
269void GraphBuilder::visitReturnInst(ReturnInst &RI) {
270 if (RI.getNumOperands() && isa<PointerType>(RI.getOperand(0)->getType())) {
Chris Lattnerc314ac42002-07-11 20:32:02 +0000271 DSNode *Value = getLink(getValueNode(*RI.getOperand(0)), 0);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000272 Value->mergeWith(RetNode);
273 RetNode = Value;
274 }
275}
276
277void GraphBuilder::visitCallInst(CallInst &CI) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000278 // Add a new function call entry...
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000279 FunctionCalls.push_back(vector<DSNodeHandle>());
280 vector<DSNodeHandle> &Args = FunctionCalls.back();
281
Chris Lattnerc314ac42002-07-11 20:32:02 +0000282 // Set up the return value...
283 if (isa<PointerType>(CI.getType()))
Chris Lattner0d9bab82002-07-18 00:12:30 +0000284 Args.push_back(getLink(getValueNode(CI), 0));
Chris Lattnerc314ac42002-07-11 20:32:02 +0000285 else
286 Args.push_back(0);
287
Chris Lattner0d9bab82002-07-18 00:12:30 +0000288 unsigned Start = 0;
289 // Special case for direct call, avoid creating spurious scalar node...
290 if (GlobalValue *GV = dyn_cast<GlobalValue>(CI.getOperand(0))) {
291 Args.push_back(getGlobalNode(*GV));
292 Start = 1;
293 }
294
Chris Lattnerc314ac42002-07-11 20:32:02 +0000295 // Pass the arguments in...
Chris Lattner0d9bab82002-07-18 00:12:30 +0000296 for (unsigned i = Start, e = CI.getNumOperands(); i != e; ++i)
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000297 if (isa<PointerType>(CI.getOperand(i)->getType()))
Chris Lattner0d9bab82002-07-18 00:12:30 +0000298 Args.push_back(getLink(getValueNode(*CI.getOperand(i)), 0));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000299}
Chris Lattner055dc2c2002-07-18 15:54:42 +0000300
301// visitInstruction - All safe instructions have been processed above, this case
302// is where unsafe ptr instructions land.
303//
304void GraphBuilder::visitInstruction(Instruction &I) {
305 // If the return type is a pointer, mark the pointed node as being a cast node
306 if (isa<PointerType>(I.getType()))
307 getLink(getValueNode(I), 0)->NodeType |= DSNode::CastNode;
308
309 // If any operands are pointers, mark the pointed nodes as being a cast node
310 for (Instruction::op_iterator i = I.op_begin(), E = I.op_end(); i!=E; ++i)
311 if (isa<PointerType>(i->get()->getType()))
312 getLink(getValueNode(*i->get()), 0)->NodeType |= DSNode::CastNode;
313}
314