blob: 1282d7a3b6c761309a163033951d2d7affe50143 [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 Lattner31bcdb82002-04-28 19:55:58 +000015#include "llvm/Constants.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//
Chris Lattnerfe145682002-04-17 03:24:59 +000023ShadowDSNode *DSNode::synthesizeNode(const Type *Ty,
24 FunctionRepBuilder *Rep) {
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000025 // If we are a derived shadow node, defer to our parent to synthesize the node
Chris Lattnerfe145682002-04-17 03:24:59 +000026 if (ShadowDSNode *Th = dyn_cast<ShadowDSNode>(this))
27 if (Th->getShadowParent())
28 return Th->getShadowParent()->synthesizeNode(Ty, Rep);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000029
30 // See if we have already synthesized a node of this type...
31 for (unsigned i = 0, e = SynthNodes.size(); i != e; ++i)
32 if (SynthNodes[i].first == Ty) return SynthNodes[i].second;
33
34 // No we haven't. Do so now and add it to our list of saved nodes...
Chris Lattnerfe145682002-04-17 03:24:59 +000035 ShadowDSNode *SN = Rep->makeSynthesizedShadow(Ty, this);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000036 SynthNodes.push_back(make_pair(Ty, SN));
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000037 return SN;
38}
39
Chris Lattnerfe145682002-04-17 03:24:59 +000040ShadowDSNode *FunctionRepBuilder::makeSynthesizedShadow(const Type *Ty,
41 DSNode *Parent) {
42 ShadowDSNode *Result = new ShadowDSNode(Ty, F->getFunction()->getParent(),
43 Parent);
44 ShadowNodes.push_back(Result);
45 return Result;
46}
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000047
48
49
50// visitOperand - If the specified instruction operand is a global value, add
51// a node for it...
52//
53void InitVisitor::visitOperand(Value *V) {
54 if (!Rep->ValueMap.count(V)) // Only process it once...
55 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
56 GlobalDSNode *N = new GlobalDSNode(GV);
Chris Lattner1120c8b2002-03-28 17:56:03 +000057 Rep->GlobalNodes.push_back(N);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000058 Rep->ValueMap[V].add(N);
59 Rep->addAllUsesToWorkList(GV);
Chris Lattnerf4066b32002-03-27 19:45:12 +000060
61 // FIXME: If the global variable has fields, we should add critical
62 // shadow nodes to represent them!
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000063 }
64}
65
66
67// visitCallInst - Create a call node for the callinst, and create as shadow
68// node if the call returns a pointer value. Check to see if the call node
69// uses any global variables...
70//
Chris Lattner18961502002-06-25 16:12:52 +000071void InitVisitor::visitCallInst(CallInst &CI) {
72 CallDSNode *C = new CallDSNode(&CI);
Chris Lattner1120c8b2002-03-28 17:56:03 +000073 Rep->CallNodes.push_back(C);
Chris Lattner18961502002-06-25 16:12:52 +000074 Rep->CallMap[&CI] = C;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000075
Chris Lattner18961502002-06-25 16:12:52 +000076 if (const PointerType *PT = dyn_cast<PointerType>(CI.getType())) {
Chris Lattnerf4066b32002-03-27 19:45:12 +000077 // Create a critical shadow node to represent the memory object that the
78 // return value points to...
Chris Lattner3feaf022002-04-01 00:14:41 +000079 ShadowDSNode *Shad = new ShadowDSNode(PT->getElementType(),
Chris Lattner7650b942002-04-16 20:39:59 +000080 Func->getParent());
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000081 Rep->ShadowNodes.push_back(Shad);
82
83 // The return value of the function is a pointer to the shadow value
84 // just created...
85 //
86 C->getLink(0).add(Shad);
87
88 // The call instruction returns a pointer to the shadow block...
Chris Lattner18961502002-06-25 16:12:52 +000089 Rep->ValueMap[&CI].add(Shad, &CI);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000090
91 // If the call returns a value with pointer type, add all of the users
92 // of the call instruction to the work list...
Chris Lattner18961502002-06-25 16:12:52 +000093 Rep->addAllUsesToWorkList(&CI);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +000094 }
95
96 // Loop over all of the operands of the call instruction (except the first
97 // one), to look for global variable references...
98 //
Chris Lattner18961502002-06-25 16:12:52 +000099 for_each(CI.op_begin(), CI.op_end(),
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000100 bind_obj(this, &InitVisitor::visitOperand));
101}
102
103
104// visitAllocationInst - Create an allocation node for the allocation. Since
105// allocation instructions do not take pointer arguments, they cannot refer to
106// global vars...
107//
Chris Lattner18961502002-06-25 16:12:52 +0000108void InitVisitor::visitAllocationInst(AllocationInst &AI) {
109 AllocDSNode *N = new AllocDSNode(&AI);
Chris Lattner1120c8b2002-03-28 17:56:03 +0000110 Rep->AllocNodes.push_back(N);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000111
Chris Lattner18961502002-06-25 16:12:52 +0000112 Rep->ValueMap[&AI].add(N, &AI);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000113
114 // Add all of the users of the malloc instruction to the work list...
Chris Lattner18961502002-06-25 16:12:52 +0000115 Rep->addAllUsesToWorkList(&AI);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000116}
117
118
119// Visit all other instruction types. Here we just scan, looking for uses of
120// global variables...
121//
Chris Lattner18961502002-06-25 16:12:52 +0000122void InitVisitor::visitInstruction(Instruction &I) {
123 for_each(I.op_begin(), I.op_end(),
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000124 bind_obj(this, &InitVisitor::visitOperand));
125}
126
127
128// addAllUsesToWorkList - Add all of the instructions users of the specified
129// value to the work list for further processing...
130//
131void FunctionRepBuilder::addAllUsesToWorkList(Value *V) {
132 //cerr << "Adding all uses of " << V << "\n";
133 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
134 Instruction *Inst = cast<Instruction>(*I);
135 // When processing global values, it's possible that the instructions on
136 // the use list are not all in this method. Only add the instructions
137 // that _are_ in this method.
138 //
139 if (Inst->getParent()->getParent() == F->getFunction())
140 // Only let an instruction occur on the work list once...
141 if (std::find(WorkList.begin(), WorkList.end(), Inst) == WorkList.end())
142 WorkList.push_back(Inst);
143 }
144}
145
146
147
148
149void FunctionRepBuilder::initializeWorkList(Function *Func) {
150 // Add all of the arguments to the method to the graph and add all users to
151 // the worklists...
152 //
Chris Lattner18961502002-06-25 16:12:52 +0000153 for (Function::aiterator I = Func->abegin(), E = Func->aend(); I != E; ++I) {
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000154 // Only process arguments that are of pointer type...
Chris Lattner18961502002-06-25 16:12:52 +0000155 if (const PointerType *PT = dyn_cast<PointerType>(I->getType())) {
Chris Lattner212be2e2002-04-16 03:44:03 +0000156 // Add a shadow value for it to represent what it is pointing to and add
157 // this to the value map...
Chris Lattner3feaf022002-04-01 00:14:41 +0000158 ShadowDSNode *Shad = new ShadowDSNode(PT->getElementType(),
Chris Lattner7650b942002-04-16 20:39:59 +0000159 Func->getParent());
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000160 ShadowNodes.push_back(Shad);
Chris Lattner18961502002-06-25 16:12:52 +0000161 ValueMap[I].add(PointerVal(Shad), I);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000162
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000163 // Make sure that all users of the argument are processed...
Chris Lattner18961502002-06-25 16:12:52 +0000164 addAllUsesToWorkList(I);
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,
Chris Lattner18961502002-06-25 16:12:52 +0000180 const MemAccessInst &MAI) {
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000181 unsigned Index = InP.Index;
Chris Lattner18961502002-06-25 16:12:52 +0000182 const Type *SrcTy = MAI.getPointerOperand()->getType();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000183
Chris Lattner18961502002-06-25 16:12:52 +0000184 for (MemAccessInst::const_op_iterator I = MAI.idx_begin(),
185 E = MAI.idx_end(); I != E; ++I)
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000186 if ((*I)->getType() == Type::UByteTy) { // Look for struct indices...
Chris Lattner18961502002-06-25 16:12:52 +0000187 const StructType *STy = cast<StructType>(SrcTy);
188 unsigned StructIdx = cast<ConstantUInt>(I->get())->getValue();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000189 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);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000204 return DestPtr.Node->getLink(DestPtr.Index);
205}
206
207
208// Reprocessing a GEP instruction is the result of the pointer operand
209// changing. This means that the set of possible values for the GEP
210// needs to be expanded.
211//
Chris Lattner18961502002-06-25 16:12:52 +0000212void FunctionRepBuilder::visitGetElementPtrInst(GetElementPtrInst &GEP) {
213 PointerValSet &GEPPVS = ValueMap[&GEP]; // PointerValSet to expand
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000214
215 // Get the input pointer val set...
Chris Lattner18961502002-06-25 16:12:52 +0000216 const PointerValSet &SrcPVS = ValueMap[GEP.getOperand(0)];
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000217
218 bool Changed = false; // Process each input value... propogating it.
219 for (unsigned i = 0, e = SrcPVS.size(); i != e; ++i) {
220 // Calculate where the resulting pointer would point based on an
221 // input of 'Val' as the pointer type... and add it to our outgoing
222 // value set. Keep track of whether or not we actually changed
223 // anything.
224 //
225 Changed |= GEPPVS.add(getIndexedPointerDest(SrcPVS[i], GEP));
226 }
227
228 // If our current value set changed, notify all of the users of our
229 // value.
230 //
Chris Lattner18961502002-06-25 16:12:52 +0000231 if (Changed) addAllUsesToWorkList(&GEP);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000232}
233
Chris Lattner18961502002-06-25 16:12:52 +0000234void FunctionRepBuilder::visitReturnInst(ReturnInst &RI) {
235 RetNode.add(ValueMap[RI.getOperand(0)]);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000236}
237
Chris Lattner18961502002-06-25 16:12:52 +0000238void FunctionRepBuilder::visitLoadInst(LoadInst &LI) {
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000239 // Only loads that return pointers are interesting...
Chris Lattner18961502002-06-25 16:12:52 +0000240 const PointerType *DestTy = dyn_cast<PointerType>(LI.getType());
Chris Lattnerfe145682002-04-17 03:24:59 +0000241 if (DestTy == 0) return;
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000242
Chris Lattner18961502002-06-25 16:12:52 +0000243 const PointerValSet &SrcPVS = ValueMap[LI.getOperand(0)];
244 PointerValSet &LIPVS = ValueMap[&LI];
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000245
246 bool Changed = false;
247 for (unsigned si = 0, se = SrcPVS.size(); si != se; ++si) {
248 PointerVal Ptr = getIndexedPointerDest(SrcPVS[si], LI);
249 PointerValSet &Field = getField(Ptr);
250
251 if (Field.size()) { // Field loaded wasn't null?
252 Changed |= LIPVS.add(Field);
Chris Lattnerfe145682002-04-17 03:24:59 +0000253 } else {
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000254 // If we are loading a null field out of a shadow node, we need to
255 // synthesize a new shadow node and link it in...
256 //
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000257 ShadowDSNode *SynthNode =
Chris Lattnerfe145682002-04-17 03:24:59 +0000258 Ptr.Node->synthesizeNode(DestTy->getElementType(), this);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000259 Field.add(SynthNode);
260
261 Changed |= LIPVS.add(Field);
262 }
263 }
264
Chris Lattner18961502002-06-25 16:12:52 +0000265 if (Changed) addAllUsesToWorkList(&LI);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000266}
267
Chris Lattner18961502002-06-25 16:12:52 +0000268void FunctionRepBuilder::visitStoreInst(StoreInst &SI) {
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000269 // The only stores that are interesting are stores the store pointers
270 // into data structures...
271 //
Chris Lattner18961502002-06-25 16:12:52 +0000272 if (!isa<PointerType>(SI.getOperand(0)->getType())) return;
273 if (!ValueMap.count(SI.getOperand(0))) return; // Src scalar has no values!
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000274
Chris Lattner18961502002-06-25 16:12:52 +0000275 const PointerValSet &SrcPVS = ValueMap[SI.getOperand(0)];
276 const PointerValSet &PtrPVS = ValueMap[SI.getOperand(1)];
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000277
278 for (unsigned si = 0, se = SrcPVS.size(); si != se; ++si) {
279 const PointerVal &SrcPtr = SrcPVS[si];
280 for (unsigned pi = 0, pe = PtrPVS.size(); pi != pe; ++pi) {
281 PointerVal Dest = getIndexedPointerDest(PtrPVS[pi], SI);
282
283#if 0
284 cerr << "Setting Dest:\n";
285 Dest.print(cerr);
286 cerr << "to point to Src:\n";
287 SrcPtr.print(cerr);
288#endif
289
290 // Add SrcPtr into the Dest field...
291 if (getField(Dest).add(SrcPtr)) {
292 // If we modified the dest field, then invalidate everyone that points
293 // to Dest.
294 const std::vector<Value*> &Ptrs = Dest.Node->getPointers();
295 for (unsigned i = 0, e = Ptrs.size(); i != e; ++i)
296 addAllUsesToWorkList(Ptrs[i]);
297 }
298 }
299 }
300}
301
Chris Lattner18961502002-06-25 16:12:52 +0000302void FunctionRepBuilder::visitCallInst(CallInst &CI) {
303 CallDSNode *DSN = CallMap[&CI];
Chris Lattner7650b942002-04-16 20:39:59 +0000304 unsigned PtrNum = 0;
Chris Lattner18961502002-06-25 16:12:52 +0000305 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
306 if (isa<PointerType>(CI.getOperand(i)->getType()))
307 DSN->addArgValue(PtrNum++, ValueMap[CI.getOperand(i)]);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000308}
309
Chris Lattner18961502002-06-25 16:12:52 +0000310void FunctionRepBuilder::visitPHINode(PHINode &PN) {
311 assert(isa<PointerType>(PN.getType()) && "Should only update ptr phis");
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000312
Chris Lattner18961502002-06-25 16:12:52 +0000313 PointerValSet &PN_PVS = ValueMap[&PN];
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000314 bool Changed = false;
Chris Lattner18961502002-06-25 16:12:52 +0000315 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
316 Changed |= PN_PVS.add(ValueMap[PN.getIncomingValue(i)],
317 PN.getIncomingValue(i));
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000318
Chris Lattner18961502002-06-25 16:12:52 +0000319 if (Changed) addAllUsesToWorkList(&PN);
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000320}
321
322
323
324
325// FunctionDSGraph constructor - Perform the global analysis to determine
326// what the data structure usage behavior or a method looks like.
327//
328FunctionDSGraph::FunctionDSGraph(Function *F) : Func(F) {
329 FunctionRepBuilder Builder(this);
Chris Lattner1120c8b2002-03-28 17:56:03 +0000330 AllocNodes = Builder.getAllocNodes();
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000331 ShadowNodes = Builder.getShadowNodes();
Chris Lattner1120c8b2002-03-28 17:56:03 +0000332 GlobalNodes = Builder.getGlobalNodes();
333 CallNodes = Builder.getCallNodes();
334 RetNode = Builder.getRetNode();
335 ValueMap = Builder.getValueMap();
Chris Lattnerf4066b32002-03-27 19:45:12 +0000336
Chris Lattner7650b942002-04-16 20:39:59 +0000337 // Remove all entries in the value map that consist of global values pointing
338 // at things. They can only point to their node, so there is no use keeping
339 // them.
340 //
341 for (map<Value*, PointerValSet>::iterator I = ValueMap.begin(),
342 E = ValueMap.end(); I != E;)
343 if (isa<GlobalValue>(I->first)) {
344#if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER
345 I = ValueMap.erase(I);
346#else
347 ValueMap.erase(I); // This is really lame.
348 I = ValueMap.begin(); // GCC's stdc++ lib doesn't return an it!
349#endif
350 } else
351 ++I;
352
Chris Lattnerf4066b32002-03-27 19:45:12 +0000353 bool Changed = true;
354 while (Changed) {
355 // Eliminate shadow nodes that are not distinguishable from some other
356 // node in the graph...
357 //
Chris Lattner7d093d42002-03-28 19:16:48 +0000358 Changed = UnlinkUndistinguishableNodes();
Chris Lattnerf4066b32002-03-27 19:45:12 +0000359
360 // Eliminate shadow nodes that are now extraneous due to linking...
Chris Lattner7d093d42002-03-28 19:16:48 +0000361 Changed |= RemoveUnreachableNodes();
Chris Lattnerf4066b32002-03-27 19:45:12 +0000362 }
Chris Lattnerbb2a28f2002-03-26 22:39:06 +0000363}