blob: eda16bc6949928f8b177335aa489e1a7c9f70ed5 [file] [log] [blame]
Chris Lattnerf4066b32002-03-27 19:45:12 +00001//===- FunctionRepBuilder.cpp - Build the local datastructure graph -------===//
Chris Lattnerbb2a28f2002-03-26 22:39:06 +00002//
3// Build the local datastructure graph for a single method.
4//
5//===----------------------------------------------------------------------===//
6
7#include "FunctionRepBuilder.h"
8#include "llvm/Function.h"
Chris Lattner42a41272002-04-09 18:37:46 +00009#include "llvm/BasicBlock.h"
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000010#include "llvm/iMemory.h"
11#include "llvm/iPHINode.h"
12#include "llvm/iOther.h"
13#include "llvm/iTerminators.h"
14#include "llvm/DerivedTypes.h"
Chris Lattner42a41272002-04-09 18:37:46 +000015#include "llvm/ConstantVals.h"
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000016#include "Support/STLExtras.h"
17#include <algorithm>
18
19// synthesizeNode - Create a new shadow node that is to be linked into this
20// chain..
21// FIXME: This should not take a FunctionRepBuilder as an argument!
22//
23ShadowDSNode *ShadowDSNode::synthesizeNode(const Type *Ty,
24 FunctionRepBuilder *Rep) {
25 // If we are a derived shadow node, defer to our parent to synthesize the node
26 if (ShadowParent) return ShadowParent->synthesizeNode(Ty, Rep);
27
28 // See if we have already synthesized a node of this type...
29 for (unsigned i = 0, e = SynthNodes.size(); i != e; ++i)
30 if (SynthNodes[i].first == Ty) return SynthNodes[i].second;
31
32 // No we haven't. Do so now and add it to our list of saved nodes...
33 ShadowDSNode *SN = new ShadowDSNode(Ty, Mod, this);
34 SynthNodes.push_back(make_pair(Ty, SN));
35 Rep->addShadowNode(SN);
36 return SN;
37}
38
39
40
41
42// visitOperand - If the specified instruction operand is a global value, add
43// a node for it...
44//
45void InitVisitor::visitOperand(Value *V) {
46 if (!Rep->ValueMap.count(V)) // Only process it once...
47 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
48 GlobalDSNode *N = new GlobalDSNode(GV);
Chris Lattner1120c8b2002-03-28 17:56:03 +000049 Rep->GlobalNodes.push_back(N);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000050 Rep->ValueMap[V].add(N);
51 Rep->addAllUsesToWorkList(GV);
Chris Lattnerf4066b32002-03-27 19:45:12 +000052
53 // FIXME: If the global variable has fields, we should add critical
54 // shadow nodes to represent them!
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000055 }
56}
57
58
59// visitCallInst - Create a call node for the callinst, and create as shadow
60// node if the call returns a pointer value. Check to see if the call node
61// uses any global variables...
62//
63void InitVisitor::visitCallInst(CallInst *CI) {
64 CallDSNode *C = new CallDSNode(CI);
Chris Lattner1120c8b2002-03-28 17:56:03 +000065 Rep->CallNodes.push_back(C);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000066 Rep->CallMap[CI] = C;
67
Chris Lattner3feaf022002-04-01 00:14:41 +000068 if (PointerType *PT = dyn_cast<PointerType>(CI->getType())) {
Chris Lattnerf4066b32002-03-27 19:45:12 +000069 // Create a critical shadow node to represent the memory object that the
70 // return value points to...
Chris Lattner3feaf022002-04-01 00:14:41 +000071 ShadowDSNode *Shad = new ShadowDSNode(PT->getElementType(),
72 Func->getParent(), true);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000073 Rep->ShadowNodes.push_back(Shad);
74
75 // The return value of the function is a pointer to the shadow value
76 // just created...
77 //
78 C->getLink(0).add(Shad);
79
80 // The call instruction returns a pointer to the shadow block...
81 Rep->ValueMap[CI].add(Shad, CI);
82
83 // If the call returns a value with pointer type, add all of the users
84 // of the call instruction to the work list...
85 Rep->addAllUsesToWorkList(CI);
86 }
87
88 // Loop over all of the operands of the call instruction (except the first
89 // one), to look for global variable references...
90 //
91 for_each(CI->op_begin()+1, CI->op_end(), // Skip first arg
92 bind_obj(this, &InitVisitor::visitOperand));
93}
94
95
96// visitAllocationInst - Create an allocation node for the allocation. Since
97// allocation instructions do not take pointer arguments, they cannot refer to
98// global vars...
99//
100void InitVisitor::visitAllocationInst(AllocationInst *AI) {
Chris Lattner1120c8b2002-03-28 17:56:03 +0000101 AllocDSNode *N = new AllocDSNode(AI);
102 Rep->AllocNodes.push_back(N);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000103
104 Rep->ValueMap[AI].add(N, AI);
105
106 // Add all of the users of the malloc instruction to the work list...
107 Rep->addAllUsesToWorkList(AI);
108}
109
110
111// Visit all other instruction types. Here we just scan, looking for uses of
112// global variables...
113//
114void InitVisitor::visitInstruction(Instruction *I) {
115 for_each(I->op_begin(), I->op_end(),
116 bind_obj(this, &InitVisitor::visitOperand));
117}
118
119
120// addAllUsesToWorkList - Add all of the instructions users of the specified
121// value to the work list for further processing...
122//
123void FunctionRepBuilder::addAllUsesToWorkList(Value *V) {
124 //cerr << "Adding all uses of " << V << "\n";
125 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
126 Instruction *Inst = cast<Instruction>(*I);
127 // When processing global values, it's possible that the instructions on
128 // the use list are not all in this method. Only add the instructions
129 // that _are_ in this method.
130 //
131 if (Inst->getParent()->getParent() == F->getFunction())
132 // Only let an instruction occur on the work list once...
133 if (std::find(WorkList.begin(), WorkList.end(), Inst) == WorkList.end())
134 WorkList.push_back(Inst);
135 }
136}
137
138
139
140
141void FunctionRepBuilder::initializeWorkList(Function *Func) {
142 // Add all of the arguments to the method to the graph and add all users to
143 // the worklists...
144 //
145 for (Function::ArgumentListType::iterator I = Func->getArgumentList().begin(),
Chris Lattner73e21422002-04-09 19:48:49 +0000146 E = Func->getArgumentList().end(); I != E; ++I) {
147 Value *Arg = (Value*)(*I);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000148 // Only process arguments that are of pointer type...
Chris Lattner73e21422002-04-09 19:48:49 +0000149 if (PointerType *PT = dyn_cast<PointerType>(Arg->getType())) {
150 ArgDSNode *ArgNode = new ArgDSNode(*I);
151 ArgNodes.push_back(ArgNode);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000152
Chris Lattnerf4066b32002-03-27 19:45:12 +0000153 // Add a critical shadow value for it to represent what it is pointing
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000154 // to and add this to the value map...
Chris Lattner3feaf022002-04-01 00:14:41 +0000155 ShadowDSNode *Shad = new ShadowDSNode(PT->getElementType(),
156 Func->getParent(), true);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000157 ShadowNodes.push_back(Shad);
Chris Lattner73e21422002-04-09 19:48:49 +0000158 ValueMap[Arg].add(PointerVal(Shad), Arg);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000159
160 // The value of the argument is the shadow value...
Chris Lattner73e21422002-04-09 19:48:49 +0000161 ArgNode->getLink(0).add(Shad);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000162
163 // Make sure that all users of the argument are processed...
Chris Lattner73e21422002-04-09 19:48:49 +0000164 addAllUsesToWorkList(Arg);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000165 }
Chris Lattner73e21422002-04-09 19:48:49 +0000166 }
167
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000168 // Iterate over the instructions in the method. Create nodes for malloc and
169 // call instructions. Add all uses of these to the worklist of instructions
170 // to process.
171 //
172 InitVisitor IV(this, Func);
173 IV.visit(Func);
174}
175
176
177
178
179PointerVal FunctionRepBuilder::getIndexedPointerDest(const PointerVal &InP,
180 const MemAccessInst *MAI) {
181 unsigned Index = InP.Index;
182 const Type *SrcTy = MAI->getPointerOperand()->getType();
183
184 for (MemAccessInst::const_op_iterator I = MAI->idx_begin(),
185 E = MAI->idx_end(); I != E; ++I)
186 if ((*I)->getType() == Type::UByteTy) { // Look for struct indices...
187 StructType *STy = cast<StructType>(SrcTy);
188 unsigned StructIdx = cast<ConstantUInt>(*I)->getValue();
189 for (unsigned i = 0; i != StructIdx; ++i)
190 Index += countPointerFields(STy->getContainedType(i));
191
192 // Advance SrcTy to be the new element type...
193 SrcTy = STy->getContainedType(StructIdx);
194 } else {
195 // Otherwise, stepping into array or initial pointer, just increment type
196 SrcTy = cast<SequentialType>(SrcTy)->getElementType();
197 }
198
199 return PointerVal(InP.Node, Index);
200}
201
202static PointerValSet &getField(const PointerVal &DestPtr) {
203 assert(DestPtr.Node != 0);
204
205 return DestPtr.Node->getLink(DestPtr.Index);
206}
207
208
209// Reprocessing a GEP instruction is the result of the pointer operand
210// changing. This means that the set of possible values for the GEP
211// needs to be expanded.
212//
213void FunctionRepBuilder::visitGetElementPtrInst(GetElementPtrInst *GEP) {
214 PointerValSet &GEPPVS = ValueMap[GEP]; // PointerValSet to expand
215
216 // Get the input pointer val set...
217 const PointerValSet &SrcPVS = ValueMap[GEP->getOperand(0)];
218
219 bool Changed = false; // Process each input value... propogating it.
220 for (unsigned i = 0, e = SrcPVS.size(); i != e; ++i) {
221 // Calculate where the resulting pointer would point based on an
222 // input of 'Val' as the pointer type... and add it to our outgoing
223 // value set. Keep track of whether or not we actually changed
224 // anything.
225 //
226 Changed |= GEPPVS.add(getIndexedPointerDest(SrcPVS[i], GEP));
227 }
228
229 // If our current value set changed, notify all of the users of our
230 // value.
231 //
232 if (Changed) addAllUsesToWorkList(GEP);
233}
234
235void FunctionRepBuilder::visitReturnInst(ReturnInst *RI) {
236 RetNode.add(ValueMap[RI->getOperand(0)]);
237}
238
239void FunctionRepBuilder::visitLoadInst(LoadInst *LI) {
240 // Only loads that return pointers are interesting...
241 if (!isa<PointerType>(LI->getType())) return;
242 const PointerType *DestTy = cast<PointerType>(LI->getType());
243
244 const PointerValSet &SrcPVS = ValueMap[LI->getOperand(0)];
245 PointerValSet &LIPVS = ValueMap[LI];
246
247 bool Changed = false;
248 for (unsigned si = 0, se = SrcPVS.size(); si != se; ++si) {
249 PointerVal Ptr = getIndexedPointerDest(SrcPVS[si], LI);
250 PointerValSet &Field = getField(Ptr);
251
252 if (Field.size()) { // Field loaded wasn't null?
253 Changed |= LIPVS.add(Field);
Chris Lattner3feaf022002-04-01 00:14:41 +0000254 } else if (ShadowDSNode *Shad = dyn_cast<ShadowDSNode>(Ptr.Node)) {
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000255 // If we are loading a null field out of a shadow node, we need to
256 // synthesize a new shadow node and link it in...
257 //
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000258 ShadowDSNode *SynthNode =
259 Shad->synthesizeNode(DestTy->getElementType(), this);
260 Field.add(SynthNode);
261
262 Changed |= LIPVS.add(Field);
263 }
264 }
265
266 if (Changed) addAllUsesToWorkList(LI);
267}
268
269void FunctionRepBuilder::visitStoreInst(StoreInst *SI) {
270 // The only stores that are interesting are stores the store pointers
271 // into data structures...
272 //
273 if (!isa<PointerType>(SI->getOperand(0)->getType())) return;
Chris Lattnere0d1d1a2002-04-01 00:45:09 +0000274 if (!ValueMap.count(SI->getOperand(0))) return; // Src scalar has no values!
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000275
276 const PointerValSet &SrcPVS = ValueMap[SI->getOperand(0)];
277 const PointerValSet &PtrPVS = ValueMap[SI->getOperand(1)];
278
279 for (unsigned si = 0, se = SrcPVS.size(); si != se; ++si) {
280 const PointerVal &SrcPtr = SrcPVS[si];
281 for (unsigned pi = 0, pe = PtrPVS.size(); pi != pe; ++pi) {
282 PointerVal Dest = getIndexedPointerDest(PtrPVS[pi], SI);
283
284#if 0
285 cerr << "Setting Dest:\n";
286 Dest.print(cerr);
287 cerr << "to point to Src:\n";
288 SrcPtr.print(cerr);
289#endif
290
291 // Add SrcPtr into the Dest field...
292 if (getField(Dest).add(SrcPtr)) {
293 // If we modified the dest field, then invalidate everyone that points
294 // to Dest.
295 const std::vector<Value*> &Ptrs = Dest.Node->getPointers();
296 for (unsigned i = 0, e = Ptrs.size(); i != e; ++i)
297 addAllUsesToWorkList(Ptrs[i]);
298 }
299 }
300 }
301}
302
303void FunctionRepBuilder::visitCallInst(CallInst *CI) {
304 CallDSNode *DSN = CallMap[CI];
305
306 unsigned PtrNum = 0, i = 0;
307 if (isa<Function>(CI->getOperand(0)))
308 ++i; // Not an Indirect function call? Skip the function pointer...
309
310 for (unsigned e = CI->getNumOperands(); i != e; ++i)
311 if (isa<PointerType>(CI->getOperand(i)->getType()))
312 DSN->addArgValue(PtrNum++, ValueMap[CI->getOperand(i)]);
313}
314
315void FunctionRepBuilder::visitPHINode(PHINode *PN) {
316 assert(isa<PointerType>(PN->getType()) && "Should only update ptr phis");
317
318 PointerValSet &PN_PVS = ValueMap[PN];
319 bool Changed = false;
320 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
321 Changed |= PN_PVS.add(ValueMap[PN->getIncomingValue(i)],
322 PN->getIncomingValue(i));
323
324 if (Changed) addAllUsesToWorkList(PN);
325}
326
327
328
329
330// FunctionDSGraph constructor - Perform the global analysis to determine
331// what the data structure usage behavior or a method looks like.
332//
333FunctionDSGraph::FunctionDSGraph(Function *F) : Func(F) {
334 FunctionRepBuilder Builder(this);
Chris Lattner1120c8b2002-03-28 17:56:03 +0000335 ArgNodes = Builder.getArgNodes();
336 AllocNodes = Builder.getAllocNodes();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000337 ShadowNodes = Builder.getShadowNodes();
Chris Lattner1120c8b2002-03-28 17:56:03 +0000338 GlobalNodes = Builder.getGlobalNodes();
339 CallNodes = Builder.getCallNodes();
340 RetNode = Builder.getRetNode();
341 ValueMap = Builder.getValueMap();
Chris Lattnerf4066b32002-03-27 19:45:12 +0000342
343 bool Changed = true;
344 while (Changed) {
345 // Eliminate shadow nodes that are not distinguishable from some other
346 // node in the graph...
347 //
Chris Lattner7d093d42002-03-28 19:16:48 +0000348 Changed = UnlinkUndistinguishableNodes();
Chris Lattnerf4066b32002-03-27 19:45:12 +0000349
350 // Eliminate shadow nodes that are now extraneous due to linking...
Chris Lattner7d093d42002-03-28 19:16:48 +0000351 Changed |= RemoveUnreachableNodes();
Chris Lattnerf4066b32002-03-27 19:45:12 +0000352 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000353}
354