1 //===- NodeImpl.cpp - Implement the data structure analysis nodes ---------===//
3 // Implement the LLVM data structure analysis library.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/Analysis/DataStructureGraph.h"
8 #include "llvm/Assembly/Writer.h"
9 #include "llvm/DerivedTypes.h"
10 #include "llvm/Function.h"
11 #include "llvm/iMemory.h"
12 #include "llvm/iOther.h"
13 #include "Support/STLExtras.h"
19 bool AllocDSNode::isEquivalentTo(DSNode *Node) const {
20 if (AllocDSNode *N = dyn_cast<AllocDSNode>(Node))
21 return getType() == Node->getType();
22 //&& isAllocaNode() == N->isAllocaNode();
26 void AllocDSNode::mergeInto(DSNode *Node) const {
27 // Make sure the merged node is variable size if this node is var size
28 AllocDSNode *N = cast<AllocDSNode>(Node);
29 N->isVarSize |= isVarSize;
32 bool GlobalDSNode::isEquivalentTo(DSNode *Node) const {
33 if (const GlobalDSNode *G = dyn_cast<GlobalDSNode>(Node)) {
34 if (G->Val != Val) return false;
36 // Check that the outgoing links are identical...
37 assert(getNumLinks() == G->getNumLinks() && "Not identical shape?");
38 for (unsigned i = 0, e = getNumLinks(); i != e; ++i)
39 if (getLink(i) != G->getLink(i)) // Check links
46 // Call node equivalency - Two call nodes are identical if all of the outgoing
47 // links are the same, AND if all of the incoming links are identical.
49 bool CallDSNode::isEquivalentTo(DSNode *Node) const {
50 if (CallDSNode *C = dyn_cast<CallDSNode>(Node)) {
51 if (getReferrers().size() != C->getReferrers().size() ||
52 C->getType() != getType())
53 return false; // Quick check...
55 // Check that the outgoing links are identical...
56 assert(getNumLinks() == C->getNumLinks() && "Not identical shape?");
57 for (unsigned i = 0, e = getNumLinks(); i != e; ++i)
58 if (getLink(i) != C->getLink(i)) // Check links
62 std::vector<PointerValSet*> Refs1 = C->getReferrers();
63 std::vector<PointerValSet*> Refs2 = getReferrers();
65 sort(Refs1.begin(), Refs1.end());
66 sort(Refs2.begin(), Refs2.end());
67 if (Refs1 != Refs2) return false; // Incoming edges different?
69 // Check that all outgoing links are the same...
70 return C->ArgLinks == ArgLinks; // Check that the arguments are identical
75 // NodesAreEquivalent - Check to see if the nodes are equivalent in all ways
76 // except node type. Since we know N1 is a shadow node, N2 is allowed to be
79 bool ShadowDSNode::isEquivalentTo(DSNode *Node) const {
80 return getType() == Node->getType();
86 //===----------------------------------------------------------------------===//
87 // DSNode Class Implementation
90 static void MapPVS(PointerValSet &PVSOut, const PointerValSet &PVSIn,
91 map<const DSNode*, DSNode*> &NodeMap, bool ReinitOk = false){
92 assert((ReinitOk || PVSOut.empty()) && "Value set already initialized!");
94 for (unsigned i = 0, e = PVSIn.size(); i != e; ++i)
95 PVSOut.add(PointerVal(NodeMap[PVSIn[i].Node], PVSIn[i].Index));
100 unsigned countPointerFields(const Type *Ty) {
101 switch (Ty->getPrimitiveID()) {
102 case Type::StructTyID: {
103 const StructType *ST = cast<StructType>(Ty);
105 for (unsigned i = 0, e = ST->getNumContainedTypes(); i != e; ++i)
106 Sum += countPointerFields(ST->getContainedType(i));
111 case Type::ArrayTyID:
112 // All array elements are folded together...
113 return countPointerFields(cast<ArrayType>(Ty)->getElementType());
115 case Type::PointerTyID:
118 default: // Some other type, just treat it like a scalar
123 DSNode::DSNode(enum NodeTy NT, const Type *T) : Ty(T), NodeType(NT) {
124 // Create field entries for all of the values in this type...
125 FieldLinks.resize(countPointerFields(getType()));
128 void DSNode::removeReferrer(PointerValSet *PVS) {
129 std::vector<PointerValSet*>::iterator I = std::find(Referrers.begin(),
130 Referrers.end(), PVS);
131 assert(I != Referrers.end() && "PVS not pointing to node!");
136 // removeAllIncomingEdges - Erase all edges in the graph that point to this node
137 void DSNode::removeAllIncomingEdges() {
138 while (!Referrers.empty())
139 Referrers.back()->removePointerTo(this);
143 static void replaceIn(std::string &S, char From, const std::string &To) {
144 for (unsigned i = 0; i < S.size(); )
146 S.replace(S.begin()+i, S.begin()+i+1,
147 To.begin(), To.end());
154 static void writeEdges(std::ostream &O, const void *SrcNode,
155 const char *SrcNodePortName, int SrcNodeIdx,
156 const PointerValSet &VS, const string &EdgeAttr = "") {
157 for (unsigned j = 0, je = VS.size(); j != je; ++j) {
158 O << "\t\tNode" << SrcNode << SrcNodePortName;
159 if (SrcNodeIdx != -1) O << SrcNodeIdx;
161 O << " -> Node" << VS[j].Node;
163 O << ":g" << VS[j].Index;
165 if (!EdgeAttr.empty())
166 O << "[" << EdgeAttr << "]";
171 static string escapeLabel(const string &In) {
173 replaceIn(Label, '\\', "\\\\\\\\"); // Escape caption...
174 replaceIn(Label, ' ', "\\ ");
175 replaceIn(Label, '{', "\\{");
176 replaceIn(Label, '}', "\\}");
180 void DSNode::dump() const { print(std::cerr); }
182 void DSNode::print(std::ostream &O) const {
183 string Caption = escapeLabel(getCaption());
185 O << "\t\tNode" << (void*)this << " [ label =\"{" << Caption;
187 const std::vector<PointerValSet> *Links = getAuxLinks();
188 if (Links && !Links->empty()) {
190 for (unsigned i = 0; i < Links->size(); ++i) {
192 O << "<f" << i << ">";
197 if (!FieldLinks.empty()) {
199 for (unsigned i = 0; i < FieldLinks.size(); ++i) {
201 O << "<g" << i << ">";
208 for (unsigned i = 0; i < Links->size(); ++i)
209 writeEdges(O, this, ":f", i, (*Links)[i]);
210 for (unsigned i = 0; i < FieldLinks.size(); ++i)
211 writeEdges(O, this, ":g", i, FieldLinks[i]);
214 void DSNode::mapNode(map<const DSNode*, DSNode*> &NodeMap, const DSNode *Old) {
215 assert(FieldLinks.size() == Old->FieldLinks.size() &&
216 "Cloned nodes do not have the same number of links!");
217 for (unsigned j = 0, je = FieldLinks.size(); j != je; ++j)
218 MapPVS(FieldLinks[j], Old->FieldLinks[j], NodeMap);
220 // Map our SynthNodes...
221 assert(SynthNodes.empty() && "Synthnodes already mapped?");
222 SynthNodes.reserve(Old->SynthNodes.size());
223 for (unsigned i = 0, e = Old->SynthNodes.size(); i != e; ++i)
224 SynthNodes.push_back(std::make_pair(Old->SynthNodes[i].first,
225 (ShadowDSNode*)NodeMap[Old->SynthNodes[i].second]));
228 AllocDSNode::AllocDSNode(AllocationInst *V, bool isvarsize)
229 : DSNode(NewNode, V->getType()->getElementType()), Allocation(V) {
231 // Is variable size if incoming flag says so, or if allocation is var size
233 isVarSize = isvarsize || !isa<Constant>(V->getArraySize());
236 bool AllocDSNode::isAllocaNode() const {
237 return isa<AllocaInst>(Allocation);
241 string AllocDSNode::getCaption() const {
242 std::stringstream OS;
243 OS << (isMallocNode() ? "new " : "alloca ");
245 WriteTypeSymbolic(OS, getType(),
246 Allocation->getParent()->getParent()->getParent());
252 GlobalDSNode::GlobalDSNode(GlobalValue *V)
253 : DSNode(GlobalNode, V->getType()->getElementType()), Val(V) {
256 string GlobalDSNode::getCaption() const {
257 std::stringstream OS;
258 if (isa<Function>(Val))
263 WriteTypeSymbolic(OS, getType(), Val->getParent());
264 return OS.str() + " %" + Val->getName();
268 ShadowDSNode::ShadowDSNode(const Type *Ty, Module *M) : DSNode(ShadowNode, Ty) {
273 ShadowDSNode::ShadowDSNode(const Type *Ty, Module *M, DSNode *ShadParent)
274 : DSNode(ShadowNode, Ty) {
276 ShadowParent = ShadParent;
279 std::string ShadowDSNode::getCaption() const {
280 std::stringstream OS;
282 WriteTypeSymbolic(OS, getType(), Mod);
286 CallDSNode::CallDSNode(CallInst *ci) : DSNode(CallNode, ci->getType()), CI(ci) {
287 unsigned NumPtrs = 0;
288 for (unsigned i = 0, e = ci->getNumOperands(); i != e; ++i)
289 if (isa<PointerType>(ci->getOperand(i)->getType()))
291 ArgLinks.resize(NumPtrs);
294 string CallDSNode::getCaption() const {
295 std::stringstream OS;
296 if (const Function *CM = CI->getCalledFunction())
297 OS << "call " << CM->getName();
299 OS << "call <indirect>";
301 WriteTypeSymbolic(OS, getType(),
302 CI->getParent()->getParent()->getParent());
306 void CallDSNode::mapNode(map<const DSNode*, DSNode*> &NodeMap,
308 const CallDSNode *Old = cast<CallDSNode>(O);
309 DSNode::mapNode(NodeMap, Old); // Map base portions first...
311 assert(ArgLinks.size() == Old->ArgLinks.size() && "# Arguments changed!?");
312 for (unsigned i = 0, e = Old->ArgLinks.size(); i != e; ++i)
313 MapPVS(ArgLinks[i], Old->ArgLinks[i], NodeMap);
316 void FunctionDSGraph::printFunction(std::ostream &O,
317 const char *Label) const {
318 O << "\tsubgraph cluster_" << Label << "_Function" << (void*)this << " {\n";
319 O << "\t\tlabel=\"" << Label << " Function\\ " << Func->getName() << "\";\n";
320 for (unsigned i = 0, e = AllocNodes.size(); i != e; ++i)
321 AllocNodes[i]->print(O);
322 for (unsigned i = 0, e = ShadowNodes.size(); i != e; ++i)
323 ShadowNodes[i]->print(O);
324 for (unsigned i = 0, e = GlobalNodes.size(); i != e; ++i)
325 GlobalNodes[i]->print(O);
326 for (unsigned i = 0, e = CallNodes.size(); i != e; ++i)
327 CallNodes[i]->print(O);
329 if (RetNode.size()) {
330 O << "\t\tNode" << (void*)this << Label
331 << " [shape=\"ellipse\", label=\"Returns\"];\n";
332 writeEdges(O, this, Label, -1, RetNode);
336 for (std::map<Value*, PointerValSet>::const_iterator I = ValueMap.begin(),
337 E = ValueMap.end(); I != E; ++I) {
338 if (I->second.size()) { // Only output nodes with edges...
339 std::stringstream OS;
340 WriteTypeSymbolic(OS, I->first->getType(), Func->getParent());
342 // Create node for I->first
343 O << "\t\tNode" << (void*)I->first << Label << " [shape=\""
344 << (isa<Argument>(I->first) ? "ellipse" : "box") << "\", label=\""
345 << escapeLabel(OS.str()) << "\\n%" << escapeLabel(I->first->getName())
346 << "\",fontsize=\"12.0\",color=\"gray70\"];\n";
348 // add edges from I->first to all pointers in I->second
349 writeEdges(O, I->first, Label, -1, I->second,
350 "weight=\"0.9\",color=\"gray70\"");
357 // Copy constructor - Since we copy the nodes over, we have to be sure to go
358 // through and fix pointers to point into the new graph instead of into the old
361 FunctionDSGraph::FunctionDSGraph(const FunctionDSGraph &DSG) : Func(DSG.Func) {
362 std::vector<PointerValSet> Args;
363 RetNode = cloneFunctionIntoSelf(DSG, true, Args);
367 // cloneFunctionIntoSelf - Clone the specified method graph into the current
368 // method graph, returning the Return's set of the graph. If ValueMap is set
369 // to true, the ValueMap of the function is cloned into this function as well
370 // as the data structure graph itself. Regardless, the arguments value sets
371 // of DSG are copied into Args.
373 PointerValSet FunctionDSGraph::cloneFunctionIntoSelf(const FunctionDSGraph &DSG,
375 std::vector<PointerValSet> &Args) {
376 map<const DSNode*, DSNode*> NodeMap; // Map from old graph to new graph...
377 unsigned StartAllocSize = AllocNodes.size();
378 AllocNodes.reserve(StartAllocSize+DSG.AllocNodes.size());
379 unsigned StartShadowSize = ShadowNodes.size();
380 ShadowNodes.reserve(StartShadowSize+DSG.ShadowNodes.size());
381 unsigned StartGlobalSize = GlobalNodes.size();
382 GlobalNodes.reserve(StartGlobalSize+DSG.GlobalNodes.size());
383 unsigned StartCallSize = CallNodes.size();
384 CallNodes.reserve(StartCallSize+DSG.CallNodes.size());
386 // Clone all of the alloc nodes similarly...
387 for (unsigned i = 0, e = DSG.AllocNodes.size(); i != e; ++i) {
388 AllocDSNode *New = cast<AllocDSNode>(DSG.AllocNodes[i]->clone());
389 NodeMap[DSG.AllocNodes[i]] = New;
390 AllocNodes.push_back(New);
393 // Clone all of the shadow nodes similarly...
394 for (unsigned i = 0, e = DSG.ShadowNodes.size(); i != e; ++i) {
395 ShadowDSNode *New = cast<ShadowDSNode>(DSG.ShadowNodes[i]->clone());
396 NodeMap[DSG.ShadowNodes[i]] = New;
397 ShadowNodes.push_back(New);
400 // Clone all of the global nodes...
401 for (unsigned i = 0, e = DSG.GlobalNodes.size(); i != e; ++i) {
402 GlobalDSNode *New = cast<GlobalDSNode>(DSG.GlobalNodes[i]->clone());
403 NodeMap[DSG.GlobalNodes[i]] = New;
404 GlobalNodes.push_back(New);
407 // Clone all of the call nodes...
408 for (unsigned i = 0, e = DSG.CallNodes.size(); i != e; ++i) {
409 CallDSNode *New = cast<CallDSNode>(DSG.CallNodes[i]->clone());
410 NodeMap[DSG.CallNodes[i]] = New;
411 CallNodes.push_back(New);
414 // Convert all of the links over in the nodes now that the map has been filled
417 for (unsigned i = 0, e = DSG.AllocNodes.size(); i != e; ++i)
418 AllocNodes[i+StartAllocSize]->mapNode(NodeMap, DSG.AllocNodes[i]);
419 for (unsigned i = 0, e = DSG.ShadowNodes.size(); i != e; ++i)
420 ShadowNodes[i+StartShadowSize]->mapNode(NodeMap, DSG.ShadowNodes[i]);
421 for (unsigned i = 0, e = DSG.GlobalNodes.size(); i != e; ++i)
422 GlobalNodes[i+StartGlobalSize]->mapNode(NodeMap, DSG.GlobalNodes[i]);
423 for (unsigned i = 0, e = DSG.CallNodes.size(); i != e; ++i)
424 CallNodes[i+StartCallSize]->mapNode(NodeMap, DSG.CallNodes[i]);
426 // Convert over the arguments...
427 Function *OF = DSG.getFunction();
428 for (Function::aiterator I = OF->abegin(), E = OF->aend(); I != E; ++I)
429 if (isa<PointerType>(I->getType())) {
430 PointerValSet ArgPVS;
431 assert(DSG.getValueMap().find(I) != DSG.getValueMap().end());
432 MapPVS(ArgPVS, DSG.getValueMap().find(I)->second, NodeMap);
433 assert(!ArgPVS.empty() && "Argument has no links!");
434 Args.push_back(ArgPVS);
439 // Convert value map... the values themselves stay the same, just the nodes
442 for (std::map<Value*,PointerValSet>::const_iterator I =DSG.ValueMap.begin(),
443 E = DSG.ValueMap.end(); I != E; ++I)
444 MapPVS(ValueMap[I->first], I->second, NodeMap, true);
447 // Convert over return node...
448 PointerValSet RetVals;
449 MapPVS(RetVals, DSG.RetNode, NodeMap);
454 FunctionDSGraph::~FunctionDSGraph() {
457 for_each(AllocNodes.begin(), AllocNodes.end(),
458 std::mem_fun(&DSNode::dropAllReferences));
459 for_each(ShadowNodes.begin(), ShadowNodes.end(),
460 std::mem_fun(&DSNode::dropAllReferences));
461 for_each(GlobalNodes.begin(), GlobalNodes.end(),
462 std::mem_fun(&DSNode::dropAllReferences));
463 for_each(CallNodes.begin(), CallNodes.end(),
464 std::mem_fun(&DSNode::dropAllReferences));
465 for_each(AllocNodes.begin(), AllocNodes.end(), deleter<DSNode>);
466 for_each(ShadowNodes.begin(), ShadowNodes.end(), deleter<DSNode>);
467 for_each(GlobalNodes.begin(), GlobalNodes.end(), deleter<DSNode>);
468 for_each(CallNodes.begin(), CallNodes.end(), deleter<DSNode>);