blob: 142133dbefd9a625614e7a0593660ec67709df05 [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
8#include "llvm/Analysis/DataStructure.h"
9#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"
15#include "llvm/DerivedTypes.h"
16#include "llvm/Support/InstVisitor.h"
17using std::map;
18using std::vector;
19
20//===----------------------------------------------------------------------===//
21// GraphBuilder Class
22//===----------------------------------------------------------------------===//
23//
24// This class is the builder class that constructs the local data structure
25// graph by performing a single pass over the function in question.
26//
27
28namespace {
29 class GraphBuilder : InstVisitor<GraphBuilder> {
30 DSGraph &G;
31 vector<DSNode*> &Nodes;
32 DSNodeHandle &RetNode; // Node that gets returned...
33 map<Value*, DSNodeHandle> &ValueMap;
34 vector<vector<DSNodeHandle> > &FunctionCalls;
35
36 public:
37 GraphBuilder(DSGraph &g, vector<DSNode*> &nodes, DSNodeHandle &retNode,
38 map<Value*, DSNodeHandle> &vm,
39 vector<vector<DSNodeHandle> > &fc)
40 : G(g), Nodes(nodes), RetNode(retNode), ValueMap(vm), FunctionCalls(fc) {
41 visit(G.getFunction()); // Single pass over the function
42 removeDeadNodes();
43 }
44
45 private:
46 // Visitor functions, used to handle each instruction type we encounter...
47 friend class InstVisitor<GraphBuilder>;
48 void visitMallocInst(MallocInst &MI) { handleAlloc(MI, DSNode::NewNode); }
49 void visitAllocaInst(AllocaInst &AI) { handleAlloc(AI, DSNode::AllocaNode);}
50 void handleAlloc(AllocationInst &AI, DSNode::NodeTy NT);
51
52 void visitPHINode(PHINode &PN);
53
54 void visitGetElementPtrInst(GetElementPtrInst &GEP);
55 void visitReturnInst(ReturnInst &RI);
56 void visitLoadInst(LoadInst &LI);
57 void visitStoreInst(StoreInst &SI);
58 void visitCallInst(CallInst &CI);
59 void visitSetCondInst(SetCondInst &SCI) {} // SetEQ & friends are ignored
60 void visitFreeInst(FreeInst &FI) {} // Ignore free instructions
61 void visitInstruction(Instruction &I) {
62#ifndef NDEBUG
63 bool bad = isa<PointerType>(I.getType());
64 for (Instruction::op_iterator i = I.op_begin(), E = I.op_end(); i!=E; ++i)
65 bad |= isa<PointerType>(i->get()->getType());
66 if (bad) {
67 std::cerr << "\n\n\nUNKNOWN PTR INSTRUCTION type: " << I << "\n\n\n";
68 assert(0 && "Cannot proceed");
69 }
70#endif
71 }
72
73 private:
74 // Helper functions used to implement the visitation functions...
75
76 // createNode - Create a new DSNode, ensuring that it is properly added to
77 // the graph.
78 //
79 DSNode *createNode(DSNode::NodeTy NodeType, const Type *Ty);
80
81 // getValueNode - Return a DSNode that corresponds the the specified LLVM
82 // value. This either returns the already existing node, or creates a new
83 // one and adds it to the graph, if none exists.
84 //
85 DSNode *getValueNode(Value &V);
86
87 // getLink - This method is used to either return the specified link in the
88 // specified node if one exists. If a link does not already exist (it's
89 // null), then we create a new node, link it, then return it.
90 //
91 DSNode *getLink(DSNode *Node, unsigned Link);
92
93 // getSubscriptedNode - Perform the basic getelementptr functionality that
94 // must be factored out of gep, load and store while they are all MAI's.
95 //
96 DSNode *getSubscriptedNode(MemAccessInst &MAI, DSNode *Ptr);
97
98 // removeDeadNodes - After the graph has been constructed, this method
99 // removes all unreachable nodes that are created because they got merged
100 // with other nodes in the graph.
101 //
102 void removeDeadNodes();
103 };
104}
105
106//===----------------------------------------------------------------------===//
107// DSGraph constructor - Simply use the GraphBuilder to construct the local
108// graph.
109DSGraph::DSGraph(Function &F) : Func(F), RetNode(0) {
110 // Use the graph builder to construct the local version of the graph
111 GraphBuilder B(*this, Nodes, RetNode, ValueMap, FunctionCalls);
112}
113
114
115//===----------------------------------------------------------------------===//
116// Helper method implementations...
117//
118
119
120// createNode - Create a new DSNode, ensuring that it is properly added to the
121// graph.
122//
123DSNode *GraphBuilder::createNode(DSNode::NodeTy NodeType, const Type *Ty) {
124 DSNode *N = new DSNode(NodeType, Ty);
125 Nodes.push_back(N);
126 return N;
127}
128
129
130// getValueNode - Return a DSNode that corresponds the the specified LLVM value.
131// This either returns the already existing node, or creates a new one and adds
132// it to the graph, if none exists.
133//
134DSNode *GraphBuilder::getValueNode(Value &V) {
135 assert(isa<PointerType>(V.getType()) && "Should only use pointer scalars!");
Chris Lattnerc314ac42002-07-11 20:32:02 +0000136 if (!isa<GlobalValue>(V)) {
137 DSNodeHandle &N = ValueMap[&V];
138 if (N) return N; // Already have a node? Just return it...
139 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000140
141 // Otherwise we need to create a new scalar node...
Chris Lattnerc314ac42002-07-11 20:32:02 +0000142 DSNode *N = createNode(DSNode::ScalarNode, V.getType());
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000143
Chris Lattnerc314ac42002-07-11 20:32:02 +0000144 if (GlobalValue *GV = dyn_cast<GlobalValue>(&V)) {
145 DSNodeHandle &GVH = ValueMap[GV];
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000146 DSNode *G = getLink(N, 0);
Chris Lattnerc314ac42002-07-11 20:32:02 +0000147
148 if (GVH == 0) {
149 // Traverse the global graph, adding nodes for them all, and marking them
150 // all globals. Be careful to mark functions global as well as the
151 // potential graph of global variables.
152 //
153 G->addGlobal(GV);
154 GVH = G;
155 } else {
156 GVH->mergeWith(G);
157 }
158 } else {
159 ValueMap[&V] = N;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000160 }
161
162 return N;
163}
164
165// getLink - This method is used to either return the specified link in the
166// specified node if one exists. If a link does not already exist (it's
167// null), then we create a new node, link it, then return it.
168//
169DSNode *GraphBuilder::getLink(DSNode *Node, unsigned Link) {
170 assert(Link < Node->getNumLinks() && "Link accessed out of range!");
171 if (Node->getLink(Link) == 0) {
172 DSNode::NodeTy NT;
173 const Type *Ty;
174
175 switch (Node->getType()->getPrimitiveID()) {
176 case Type::PointerTyID:
177 Ty = cast<PointerType>(Node->getType())->getElementType();
178 NT = DSNode::ShadowNode;
179 break;
180 case Type::ArrayTyID:
181 Ty = cast<ArrayType>(Node->getType())->getElementType();
182 NT = DSNode::SubElement;
183 break;
184 case Type::StructTyID:
185 Ty = cast<StructType>(Node->getType())->getContainedType(Link);
186 NT = DSNode::SubElement;
187 break;
188 default:
189 assert(0 && "Unexpected type to dereference!");
190 abort();
191 }
192
193 DSNode *New = createNode(NT, Ty);
194 Node->addEdgeTo(Link, New);
195 }
196
197 return Node->getLink(Link);
198}
199
200// getSubscriptedNode - Perform the basic getelementptr functionality that must
201// be factored out of gep, load and store while they are all MAI's.
202//
203DSNode *GraphBuilder::getSubscriptedNode(MemAccessInst &MAI, DSNode *Ptr) {
204 for (unsigned i = MAI.getFirstIndexOperandNumber(), e = MAI.getNumOperands();
205 i != e; ++i)
206 if (MAI.getOperand(i)->getType() == Type::UIntTy)
207 Ptr = getLink(Ptr, 0);
208 else if (MAI.getOperand(i)->getType() == Type::UByteTy)
209 Ptr = getLink(Ptr, cast<ConstantUInt>(MAI.getOperand(i))->getValue());
210
211 if (MAI.getFirstIndexOperandNumber() == MAI.getNumOperands())
212 Ptr = getLink(Ptr, 0); // All MAI's have an implicit 0 if nothing else.
213
214 return Ptr;
215}
216
217
218// removeDeadNodes - After the graph has been constructed, this method removes
219// all unreachable nodes that are created because they got merged with other
220// nodes in the graph. These nodes will all be trivially unreachable, so we
221// don't have to perform any non-trivial analysis here.
222//
223void GraphBuilder::removeDeadNodes() {
224 for (unsigned i = 0; i != Nodes.size(); )
Chris Lattnerc314ac42002-07-11 20:32:02 +0000225 if (Nodes[i]->NodeType || !Nodes[i]->getReferrers().empty())
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000226 ++i; // This node is alive!
227 else { // This node is dead!
228 delete Nodes[i]; // Free memory...
229 Nodes.erase(Nodes.begin()+i); // Remove from node list...
230 }
231}
232
233
234
235
236//===----------------------------------------------------------------------===//
237// Specific instruction type handler implementations...
238//
239
240// Alloca & Malloc instruction implementation - Simply create a new memory
241// object, pointing the scalar to it.
242//
243void GraphBuilder::handleAlloc(AllocationInst &AI, DSNode::NodeTy NodeType) {
244 DSNode *Scalar = getValueNode(AI);
245 DSNode *New = createNode(NodeType, AI.getAllocatedType());
246 Scalar->addEdgeTo(New); // Make the scalar point to the new node...
247}
248
249// PHINode - Make the scalar for the PHI node point to all of the things the
250// incoming values point to... which effectively causes them to be merged.
251//
252void GraphBuilder::visitPHINode(PHINode &PN) {
253 if (!isa<PointerType>(PN.getType())) return; // Only pointer PHIs
254
Chris Lattnerc314ac42002-07-11 20:32:02 +0000255 DSNode *Scalar = getValueNode(PN);
256 DSNode *ScalarDest = getLink(Scalar, 0);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000257 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
Chris Lattnerc314ac42002-07-11 20:32:02 +0000258 ScalarDest->mergeWith(getLink(getValueNode(*PN.getIncomingValue(i)), 0));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000259}
260
261void GraphBuilder::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000262 DSNode *Ptr = getSubscriptedNode(GEP, getValueNode(*GEP.getOperand(0)));
Chris Lattnerc314ac42002-07-11 20:32:02 +0000263 getValueNode(GEP)->addEdgeTo(Ptr);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000264}
265
266void GraphBuilder::visitLoadInst(LoadInst &LI) {
267 if (!isa<PointerType>(LI.getType())) return; // Only pointer PHIs
268 DSNode *Ptr = getSubscriptedNode(LI, getValueNode(*LI.getOperand(0)));
Chris Lattnerc314ac42002-07-11 20:32:02 +0000269 getValueNode(LI)->addEdgeTo(getLink(Ptr, 0));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000270}
271
272void GraphBuilder::visitStoreInst(StoreInst &SI) {
273 if (!isa<PointerType>(SI.getOperand(0)->getType())) return;
274 DSNode *Value = getValueNode(*SI.getOperand(0));
275 DSNode *DestPtr = getValueNode(*SI.getOperand(1));
Chris Lattnerc314ac42002-07-11 20:32:02 +0000276 getSubscriptedNode(SI, DestPtr)->addEdgeTo(getLink(Value, 0));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000277}
278
279void GraphBuilder::visitReturnInst(ReturnInst &RI) {
280 if (RI.getNumOperands() && isa<PointerType>(RI.getOperand(0)->getType())) {
Chris Lattnerc314ac42002-07-11 20:32:02 +0000281 DSNode *Value = getLink(getValueNode(*RI.getOperand(0)), 0);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000282 Value->mergeWith(RetNode);
283 RetNode = Value;
284 }
285}
286
287void GraphBuilder::visitCallInst(CallInst &CI) {
288 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()))
293 Args.push_back(getValueNode(CI));
294 else
295 Args.push_back(0);
296
297 // Pass the arguments in...
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000298 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
299 if (isa<PointerType>(CI.getOperand(i)->getType()))
300 Args.push_back(getValueNode(*CI.getOperand(i)));
301}