aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Analysis/DataStructure/NodeImpl.cpp
blob: 47a7dc28e65119592fd00f6dfd87a683c44ddc30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//===- NodeImpl.cpp - Implement the data structure analysis nodes ---------===//
//
// Implement the LLVM data structure analysis library.
//
//===----------------------------------------------------------------------===//

#include "llvm/Analysis/DataStructureGraph.h"
#include "llvm/Assembly/Writer.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Function.h"
#include "llvm/BasicBlock.h"
#include "llvm/iMemory.h"
#include "llvm/iOther.h"
#include "Support/STLExtras.h"
#include <algorithm>
#include <sstream>

bool AllocDSNode::isEquivalentTo(DSNode *Node) const {
  if (AllocDSNode *N = dyn_cast<AllocDSNode>(Node))
    return getType() == Node->getType();
  return false;
}

bool GlobalDSNode::isEquivalentTo(DSNode *Node) const {
  if (GlobalDSNode *G = dyn_cast<GlobalDSNode>(Node)) {
    if (G->Val != Val) return false;

    // Check that the outgoing links are identical...
    assert(getNumLinks() == G->getNumLinks() && "Not identical shape?");
    for (unsigned i = 0, e = getNumLinks(); i != e; ++i)
      if (getLink(i) != G->getLink(i))          // Check links
        return false;
    return true;
  }
  return false;
}

// Call node equivalency - Two call nodes are identical if all of the outgoing
// links are the same, AND if all of the incoming links are identical.
//
bool CallDSNode::isEquivalentTo(DSNode *Node) const {
  if (CallDSNode *C = dyn_cast<CallDSNode>(Node)) {
    if (C->CI->getCalledFunction() != CI->getCalledFunction() ||
        getReferrers().size() != C->getReferrers().size())
      return false; // Quick check...

    // Check that the outgoing links are identical...
    assert(getNumLinks() == C->getNumLinks() && "Not identical shape?");
    for (unsigned i = 0, e = getNumLinks(); i != e; ++i)
      if (getLink(i) != C->getLink(i))          // Check links
        return false;


    std::vector<PointerValSet*> Refs1 = C->getReferrers();
    std::vector<PointerValSet*> Refs2 = getReferrers();

    sort(Refs1.begin(), Refs1.end());
    sort(Refs2.begin(), Refs2.end());
    if (Refs1 != Refs2) return false;               // Incoming edges different?

    // Check that all outgoing links are the same...
    return C->ArgLinks == ArgLinks;   // Check that the arguments are identical
  }
  return false;
}

// NodesAreEquivalent - Check to see if the nodes are equivalent in all ways
// except node type.  Since we know N1 is a shadow node, N2 is allowed to be
// any type.
//
bool ShadowDSNode::isEquivalentTo(DSNode *Node) const {
  return getType() == Node->getType();
  return !isCriticalNode();              // Must not be a critical node...
}




//===----------------------------------------------------------------------===//
//  DSNode Class Implementation
//

static void MapPVS(PointerValSet &PVSOut, const PointerValSet &PVSIn,
                   map<const DSNode*, DSNode*> &NodeMap, bool ReinitOk = false){
  assert((ReinitOk || PVSOut.empty()) && "Value set already initialized!");

  for (unsigned i = 0, e = PVSIn.size(); i != e; ++i)
    PVSOut.add(PointerVal(NodeMap[PVSIn[i].Node], PVSIn[i].Index));
}



unsigned countPointerFields(const Type *Ty) {
  switch (Ty->getPrimitiveID()) {
  case Type::StructTyID: {
    const StructType *ST = cast<StructType>(Ty);
    unsigned Sum = 0;
    for (unsigned i = 0, e = ST->getNumContainedTypes(); i != e; ++i)
      Sum += countPointerFields(ST->getContainedType(i));

    return Sum;
  }

  case Type::ArrayTyID:
    // All array elements are folded together...
    return countPointerFields(cast<ArrayType>(Ty)->getElementType());

  case Type::PointerTyID:
    return 1;
    
  default:                     // Some other type, just treat it like a scalar
    return 0;
  }
}

DSNode::DSNode(enum NodeTy NT, const Type *T) : Ty(T), NodeType(NT) {
  // Create field entries for all of the values in this type...
  FieldLinks.resize(countPointerFields(getType()));
}

void DSNode::removeReferrer(PointerValSet *PVS) {
  vector<PointerValSet*>::iterator I = std::find(Referrers.begin(),
                                                 Referrers.end(), PVS);
  assert(I != Referrers.end() && "PVS not pointing to node!");
  Referrers.erase(I);
}


// removeAllIncomingEdges - Erase all edges in the graph that point to this node
void DSNode::removeAllIncomingEdges() {
  while (!Referrers.empty())
    Referrers.back()->removePointerTo(this);
}


static void replaceIn(std::string &S, char From, const std::string &To) {
  for (unsigned i = 0; i < S.size(); )
    if (S[i] == From) {
      S.replace(S.begin()+i, S.begin()+i+1,
                To.begin(), To.end());
      i += To.size();
    } else {
      ++i;
    }
}

static void writeEdges(std::ostream &O, const void *SrcNode,
                       const char *SrcNodePortName, int SrcNodeIdx,
                       const PointerValSet &VS, const string &EdgeAttr = "") {
  for (unsigned j = 0, je = VS.size(); j != je; ++j) {
    O << "\t\tNode" << SrcNode << SrcNodePortName;
    if (SrcNodeIdx != -1) O << SrcNodeIdx;

    O << " -> Node" << VS[j].Node;
    if (VS[j].Index)
      O << ":g" << VS[j].Index;

    if (!EdgeAttr.empty())
      O << "[" << EdgeAttr << "]";
    O << ";\n";
  }
}

static string escapeLabel(const string &In) {
  string Label(In);
  replaceIn(Label, '\\', "\\\\\\\\");  // Escape caption...
  replaceIn(Label, ' ', "\\ ");
  replaceIn(Label, '{', "\\{");
  replaceIn(Label, '}', "\\}");
  return Label;
}

void DSNode::dump() const { print(cerr); }

void DSNode::print(std::ostream &O) const {
  string Caption = escapeLabel(getCaption());

  O << "\t\tNode" << (void*)this << " [ label =\"{" << Caption;

  const vector<PointerValSet> *Links = getAuxLinks();
  if (Links && !Links->empty()) {
    O << "|{";
    for (unsigned i = 0; i < Links->size(); ++i) {
      if (i) O << "|";
      O << "<f" << i << ">";
    }
    O << "}";
  }

  if (!FieldLinks.empty()) {
    O << "|{";
    for (unsigned i = 0; i < FieldLinks.size(); ++i) {
      if (i) O << "|";
      O << "<g" << i << ">";
    }
    O << "}";
  }
  O << "}\"];\n";

  if (Links)
    for (unsigned i = 0; i < Links->size(); ++i)
      writeEdges(O, this, ":f", i, (*Links)[i]);
  for (unsigned i = 0; i < FieldLinks.size(); ++i)
    writeEdges(O, this, ":g", i, FieldLinks[i]);
}

void DSNode::mapNode(map<const DSNode*, DSNode*> &NodeMap, const DSNode *Old) {
  assert(FieldLinks.size() == Old->FieldLinks.size() &&
         "Cloned nodes do not have the same number of links!");
  for (unsigned j = 0, je = FieldLinks.size(); j != je; ++j)
    MapPVS(FieldLinks[j], Old->FieldLinks[j], NodeMap);
}

AllocDSNode::AllocDSNode(AllocationInst *V)
  : DSNode(NewNode, V->getType()->getElementType()), Allocation(V) {
}

bool AllocDSNode::isAllocaNode() const {
  return isa<AllocaInst>(Allocation);
}


string AllocDSNode::getCaption() const {
  stringstream OS;
  OS << (isMallocNode() ? "new " : "alloca ");

  WriteTypeSymbolic(OS, getType(),
                    Allocation->getParent()->getParent()->getParent());
  if (Allocation->isArrayAllocation())
    OS << "[ ]";
  return OS.str();
}

GlobalDSNode::GlobalDSNode(GlobalValue *V)
  : DSNode(GlobalNode, V->getType()->getElementType()), Val(V) {
}

string GlobalDSNode::getCaption() const {
  stringstream OS;
  WriteTypeSymbolic(OS, getType(), Val->getParent());
  return "global " + OS.str() + " %" + Val->getName();
}


ShadowDSNode::ShadowDSNode(const Type *Ty, Module *M, bool C = false)
  : DSNode(ShadowNode, Ty) {
  Mod = M;
  ShadowParent = 0;
  CriticalNode = C;
}

ShadowDSNode::ShadowDSNode(const Type *Ty, Module *M, ShadowDSNode *ShadParent)
  : DSNode(ShadowNode, Ty) {
  Mod = M;
  ShadowParent = ShadParent;
  CriticalNode = false;
}

std::string ShadowDSNode::getCaption() const {
  stringstream OS;
  if (CriticalNode) OS << "# ";
  OS << "shadow ";
  WriteTypeSymbolic(OS, getType(), Mod);
  if (CriticalNode) OS << " #";
  return OS.str();
}

void ShadowDSNode::mapNode(map<const DSNode*, DSNode*> &NodeMap,
                           const DSNode *O) {
  const ShadowDSNode *Old = (ShadowDSNode*)O;
  DSNode::mapNode(NodeMap, Old);  // Map base portions first...

  // Map our SynthNodes...
  assert(SynthNodes.empty() && "Synthnodes already mapped?");
  SynthNodes.reserve(Old->SynthNodes.size());
  for (unsigned i = 0, e = Old->SynthNodes.size(); i != e; ++i)
    SynthNodes.push_back(std::make_pair(Old->SynthNodes[i].first,
                    (ShadowDSNode*)NodeMap[Old->SynthNodes[i].second]));
}


CallDSNode::CallDSNode(CallInst *ci) : DSNode(CallNode, ci->getType()), CI(ci) {
  unsigned NumPtrs = 0;
  if (!isa<Function>(ci->getOperand(0)))
    NumPtrs++;   // Include the method pointer...

  for (unsigned i = 1, e = ci->getNumOperands(); i != e; ++i)
    if (isa<PointerType>(ci->getOperand(i)->getType()))
      NumPtrs++;
  ArgLinks.resize(NumPtrs);
}

string CallDSNode::getCaption() const {
  stringstream OS;
  if (const Function *CM = CI->getCalledFunction())
    OS << "call " << CM->getName();
  else
    OS << "call <indirect>";
  OS << "|Ret: ";
  WriteTypeSymbolic(OS, getType(),
                    CI->getParent()->getParent()->getParent());
  return OS.str();
}

void CallDSNode::mapNode(map<const DSNode*, DSNode*> &NodeMap,
                         const DSNode *O) {
  const CallDSNode *Old = cast<CallDSNode>(O);
  DSNode::mapNode(NodeMap, Old);  // Map base portions first...

  assert(ArgLinks.size() == Old->ArgLinks.size() && "# Arguments changed!?");
  for (unsigned i = 0, e = Old->ArgLinks.size(); i != e; ++i)
    MapPVS(ArgLinks[i], Old->ArgLinks[i], NodeMap);
}

void FunctionDSGraph::printFunction(std::ostream &O,
                                    const char *Label) const {
  O << "\tsubgraph cluster_" << Label << "_Function" << (void*)this << " {\n";
  O << "\t\tlabel=\"" << Label << " Function\\ " << Func->getName() << "\";\n";
  for (unsigned i = 0, e = AllocNodes.size(); i != e; ++i)
    AllocNodes[i]->print(O);
  for (unsigned i = 0, e = ShadowNodes.size(); i != e; ++i)
    ShadowNodes[i]->print(O);
  for (unsigned i = 0, e = GlobalNodes.size(); i != e; ++i)
    GlobalNodes[i]->print(O);
  for (unsigned i = 0, e = CallNodes.size(); i != e; ++i)
    CallNodes[i]->print(O);

  if (RetNode.size()) {
    O << "\t\tNode" << (void*)this << Label
      << " [shape=\"ellipse\", label=\"Returns\"];\n";
    writeEdges(O, this, Label, -1, RetNode);
  }

  O << "\n";
  for (std::map<Value*, PointerValSet>::const_iterator I = ValueMap.begin(),
         E = ValueMap.end(); I != E; ++I) {
    if (I->second.size()) {  // Only output nodes with edges...
      stringstream OS;
      WriteTypeSymbolic(OS, I->first->getType(), Func->getParent());

      // Create node for I->first
      O << "\t\tNode" << (void*)I->first << Label << " [shape=\"box\", label=\""
        << escapeLabel(OS.str()) << "\\n%" << escapeLabel(I->first->getName())
        << "\",fontsize=\"12.0\",color=\"gray70\"];\n";
      
      // add edges from I->first to all pointers in I->second
      writeEdges(O, I->first, Label, -1, I->second,
                 "weight=\"0.9\",color=\"gray70\"");
    }
  }
  
  O << "\t}\n";
}

// Copy constructor - Since we copy the nodes over, we have to be sure to go
// through and fix pointers to point into the new graph instead of into the old
// graph...
//
FunctionDSGraph::FunctionDSGraph(const FunctionDSGraph &DSG) : Func(DSG.Func) {
  vector<PointerValSet> Args;
  RetNode = cloneFunctionIntoSelf(DSG, true, Args);
}


// cloneFunctionIntoSelf - Clone the specified method graph into the current
// method graph, returning the Return's set of the graph.   If ValueMap is set
// to true, the ValueMap of the function is cloned into this function as well
// as the data structure graph itself.  Regardless, the arguments value sets
// of DSG are copied into Args.
//
PointerValSet FunctionDSGraph::cloneFunctionIntoSelf(const FunctionDSGraph &DSG,
                                                     bool CloneValueMap,
                                                  vector<PointerValSet> &Args) {
  map<const DSNode*, DSNode*> NodeMap;  // Map from old graph to new graph...
  unsigned StartAllocSize = AllocNodes.size();
  AllocNodes.reserve(StartAllocSize+DSG.AllocNodes.size());
  unsigned StartShadowSize = ShadowNodes.size();
  ShadowNodes.reserve(StartShadowSize+DSG.ShadowNodes.size());
  unsigned StartGlobalSize = GlobalNodes.size();
  GlobalNodes.reserve(StartGlobalSize+DSG.GlobalNodes.size());
  unsigned StartCallSize = CallNodes.size();
  CallNodes.reserve(StartCallSize+DSG.CallNodes.size());

  // Clone all of the alloc nodes similarly...
  for (unsigned i = 0, e = DSG.AllocNodes.size(); i != e; ++i) {
    AllocDSNode *New = cast<AllocDSNode>(DSG.AllocNodes[i]->clone());
    NodeMap[DSG.AllocNodes[i]] = New;
    AllocNodes.push_back(New);
  }

  // Clone all of the shadow nodes similarly...
  for (unsigned i = 0, e = DSG.ShadowNodes.size(); i != e; ++i) {
    ShadowDSNode *New = cast<ShadowDSNode>(DSG.ShadowNodes[i]->clone());
    NodeMap[DSG.ShadowNodes[i]] = New;
    ShadowNodes.push_back(New);
  }

  // Clone all of the global nodes...
  for (unsigned i = 0, e = DSG.GlobalNodes.size(); i != e; ++i) {
    GlobalDSNode *New = cast<GlobalDSNode>(DSG.GlobalNodes[i]->clone());
    NodeMap[DSG.GlobalNodes[i]] = New;
    GlobalNodes.push_back(New);
  }

  // Clone all of the call nodes...
  for (unsigned i = 0, e = DSG.CallNodes.size(); i != e; ++i) {
    CallDSNode *New = cast<CallDSNode>(DSG.CallNodes[i]->clone());
    NodeMap[DSG.CallNodes[i]] = New;
    CallNodes.push_back(New);
  }

  // Convert all of the links over in the nodes now that the map has been filled
  // in all the way...
  //
  for (unsigned i = 0, e = DSG.AllocNodes.size(); i != e; ++i)
    AllocNodes[i+StartAllocSize]->mapNode(NodeMap, DSG.AllocNodes[i]);
  for (unsigned i = 0, e = DSG.ShadowNodes.size(); i != e; ++i)
    ShadowNodes[i+StartShadowSize]->mapNode(NodeMap, DSG.ShadowNodes[i]);
  for (unsigned i = 0, e = DSG.GlobalNodes.size(); i != e; ++i)
    GlobalNodes[i+StartGlobalSize]->mapNode(NodeMap, DSG.GlobalNodes[i]);
  for (unsigned i = 0, e = DSG.CallNodes.size(); i != e; ++i)
    CallNodes[i+StartCallSize]->mapNode(NodeMap, DSG.CallNodes[i]);

  // Convert over the arguments...
  Function *OF = DSG.getFunction();
  for (Function::ArgumentListType::iterator I = OF->getArgumentList().begin(),
         E = OF->getArgumentList().end(); I != E; ++I)
    if (isa<PointerType>(((Value*)*I)->getType())) {
      PointerValSet ArgPVS;
      assert(DSG.getValueMap().find((Value*)*I) != DSG.getValueMap().end());
      MapPVS(ArgPVS, DSG.getValueMap().find((Value*)*I)->second, NodeMap);
      assert(!ArgPVS.empty() && "Argument has no links!");
      Args.push_back(ArgPVS);
    }


  if (CloneValueMap) {
    // Convert value map... the values themselves stay the same, just the nodes
    // have to change...
    //
    for (std::map<Value*,PointerValSet>::const_iterator I =DSG.ValueMap.begin(),
           E = DSG.ValueMap.end(); I != E; ++I)
      MapPVS(ValueMap[I->first], I->second, NodeMap, true);
  }

  // Convert over return node...
  PointerValSet RetVals;
  MapPVS(RetVals, DSG.RetNode, NodeMap);
  return RetVals;
}


FunctionDSGraph::~FunctionDSGraph() {
  RetNode.clear();
  ValueMap.clear();
  for_each(AllocNodes.begin(), AllocNodes.end(),
           mem_fun(&DSNode::dropAllReferences));
  for_each(ShadowNodes.begin(), ShadowNodes.end(),
           mem_fun(&DSNode::dropAllReferences));
  for_each(GlobalNodes.begin(), GlobalNodes.end(),
           mem_fun(&DSNode::dropAllReferences));
  for_each(CallNodes.begin(), CallNodes.end(),
           mem_fun(&DSNode::dropAllReferences));
  for_each(AllocNodes.begin(),  AllocNodes.end(),  deleter<DSNode>);
  for_each(ShadowNodes.begin(), ShadowNodes.end(), deleter<DSNode>);
  for_each(GlobalNodes.begin(), GlobalNodes.end(), deleter<DSNode>);
  for_each(CallNodes.begin(),   CallNodes.end(),   deleter<DSNode>);
}