aboutsummaryrefslogtreecommitdiff
path: root/include/llvm/Analysis/DataStructure.h
blob: dda0adb214ed90c390437c606de95351bc9ccfc2 (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
//===- DataStructure.h - Build data structure graphs -------------*- C++ -*--=//
//
// Implement the LLVM data structure analysis library.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_ANALYSIS_DATA_STRUCTURE_H
#define LLVM_ANALYSIS_DATA_STRUCTURE_H

#include "llvm/Pass.h"
#include <string>

class Type;
class GlobalValue;
class DSNode;                  // Each node in the graph
class DSGraph;                 // A graph for a function
class DSNodeIterator;          // Data structure graph traversal iterator
class LocalDataStructures;     // A collection of local graphs for a program
class BUDataStructures;        // A collection of bu graphs for a program
class TDDataStructures;        // A collection of td graphs for a program

//===----------------------------------------------------------------------===//
// DSNodeHandle - Implement a "handle" to a data structure node that takes care
// of all of the add/un'refing of the node to prevent the backpointers in the
// graph from getting out of date.
//
class DSNodeHandle {
  DSNode *N;
public:
  // Allow construction, destruction, and assignment...
  DSNodeHandle(DSNode *n = 0) : N(0) { operator=(n); }
  DSNodeHandle(const DSNodeHandle &H) : N(0) { operator=(H.N); }
  ~DSNodeHandle() { operator=(0); }
  DSNodeHandle &operator=(const DSNodeHandle &H) {operator=(H.N); return *this;}

  // Assignment of DSNode*, implement all of the add/un'refing (defined later)
  inline DSNodeHandle &operator=(DSNode *n);

  // Allow automatic, implicit, conversion to DSNode*
  operator DSNode*() { return N; }
  operator const DSNode*() const { return N; }
  operator bool() const { return N != 0; }
  operator bool() { return N != 0; }

  bool operator<(const DSNodeHandle &H) const {  // Allow sorting
    return N < H.N;
  }
  bool operator==(const DSNodeHandle &H) const { return N == H.N; }
  bool operator!=(const DSNodeHandle &H) const { return N != H.N; }
  bool operator==(const DSNode *Node) const { return N == Node; }
  bool operator!=(const DSNode *Node) const { return N != Node; }
  bool operator==(DSNode *Node) const { return N == Node; }
  bool operator!=(DSNode *Node) const { return N != Node; }

  // Avoid having comparisons to null cause errors...
  bool operator==(int X) const {
    assert(X == 0 && "Bad comparison!");
    return operator==((DSNode*)0);
  }
  bool operator!=(int X) const { return !operator==(X); }

  // Allow explicit conversion to DSNode...
  DSNode *get() { return N; }
  const DSNode *get() const { return N; }

  // Allow this to be treated like a pointer...
  DSNode *operator->() { return N; }
  const DSNode *operator->() const { return N; }
};


//===----------------------------------------------------------------------===//
// DSNode - Data structure node class
//
// This class keeps track of a node's type, and the fields in the data
// structure.
//
//
class DSNode {
  const Type *Ty;
  std::vector<DSNodeHandle> Links;
  std::vector<DSNodeHandle*> Referrers;

  // Globals - The list of global values that are merged into this node.
  std::vector<GlobalValue*> Globals;

  void operator=(const DSNode &); // DO NOT IMPLEMENT
public:
  enum NodeTy {
    ShadowNode = 0 << 0,   // Nothing is known about this node...
    ScalarNode = 1 << 0,   // Scalar of the current function contains this value
    AllocaNode = 1 << 1,   // This node was allocated with alloca
    NewNode    = 1 << 2,   // This node was allocated with malloc
    GlobalNode = 1 << 3,   // This node was allocated by a global var decl
    SubElement = 1 << 4,   // This node is a part of some other node
    CastNode   = 1 << 5,   // This node is accessed in unsafe ways
    Incomplete = 1 << 6,   // This node may not be complete
  };

  // NodeType - A union of the above bits.  "Shadow" nodes do not add any flags
  // to the nodes in the data structure graph, so it is possible to have nodes
  // with a value of 0 for their NodeType.  Scalar and Alloca markers go away
  // when function graphs are inlined.
  //
  unsigned char NodeType;

  DSNode(enum NodeTy NT, const Type *T);
  DSNode(const DSNode &);

  ~DSNode() {
#ifndef NDEBUG
    dropAllReferences();  // Only needed to satisfy assertion checks...
#endif
    assert(Referrers.empty() && "Referrers to dead node exist!");
  }

  // Iterator for graph interface...
  typedef DSNodeIterator iterator;
  inline iterator begin();   // Defined in DataStructureGraph.h
  inline iterator end();

  // Accessors
  const Type *getType() const { return Ty; }

  unsigned getNumLinks() const { return Links.size(); }
  DSNode *getLink(unsigned i) {
    assert(i < getNumLinks() && "Field links access out of range...");
    return Links[i];
  }
  const DSNode *getLink(unsigned i) const {
    assert(i < getNumLinks() && "Field links access out of range...");
    return Links[i];
  }

  void setLink(unsigned i, DSNode *N) {
    assert(i < getNumLinks() && "Field links access out of range...");
    Links[i] = N;
  }

  // addGlobal - Add an entry for a global value to the Globals list.  This also
  // marks the node with the 'G' flag if it does not already have it.
  //
  void addGlobal(GlobalValue *GV);
  const std::vector<GlobalValue*> &getGlobals() const { return Globals; }
  std::vector<GlobalValue*> &getGlobals() { return Globals; }

  // addEdgeTo - Add an edge from the current node to the specified node.  This
  // can cause merging of nodes in the graph.
  //
  void addEdgeTo(unsigned LinkNo, DSNode *N);
  void addEdgeTo(DSNode *N) {
    assert(getNumLinks() == 1 && "Must specify a field number to add edge if "
           " more than one field exists!");
    addEdgeTo(0, N);
  }

  // mergeWith - Merge this node into the specified node, moving all links to
  // and from the argument node into the current node.  The specified node may
  // be a null pointer (in which case, nothing happens).
  //
  void mergeWith(DSNode *N);

  // addReferrer - Keep the referrer set up to date...
  void addReferrer(DSNodeHandle *H) { Referrers.push_back(H); }
  void removeReferrer(DSNodeHandle *H);
  const std::vector<DSNodeHandle*> &getReferrers() const { return Referrers; }

  void print(std::ostream &O, const DSGraph *G) const;
  void dump() const;

  std::string getCaption(const DSGraph *G) const;

  void dropAllReferences() {
    Links.clear();
  }
};


inline DSNodeHandle &DSNodeHandle::operator=(DSNode *n) {
  if (N) N->removeReferrer(this);
  N = n;
  if (N) N->addReferrer(this);
  return *this;
}


// DSGraph - The graph that represents a function.
//
class DSGraph {
  Function &Func;
  std::vector<DSNode*> Nodes;
  DSNodeHandle RetNode;               // Node that gets returned...
  std::map<Value*, DSNodeHandle> ValueMap;

  // FunctionCalls - This vector maintains a single entry for each call
  // instruction in the current graph.  Each call entry contains DSNodeHandles
  // that refer to the arguments that are passed into the function call.  The
  // first entry in the vector is the scalar that holds the return value for the
  // call, the second is the function scalar being invoked, and the rest are
  // pointer arguments to the function.
  //
  std::vector<std::vector<DSNodeHandle> > FunctionCalls;

  // OrigFunctionCal