1 //===- DataStructure.h - Build data structure graphs -------------*- C++ -*--=//
3 // Implement the LLVM data structure analysis library.
5 //===----------------------------------------------------------------------===//
7 #ifndef LLVM_ANALYSIS_DATA_STRUCTURE_H
8 #define LLVM_ANALYSIS_DATA_STRUCTURE_H
10 #include "llvm/Pass.h"
18 class FunctionRepBuilder;
20 class FunctionDSGraph;
24 // FIXME: move this somewhere private
25 unsigned countPointerFields(const Type *Ty);
27 // PointerVal - Represent a pointer to a datastructure. The pointer points to
28 // a node, and can index into it. This is used for getelementptr instructions,
29 // which do not affect which node a pointer points to, but does change the field
34 unsigned Index; // Index into Node->FieldLinks[]
36 PointerVal(DSNode *N, unsigned Idx = 0) : Node(N), Index(Idx) {}
38 DSNode *getNode() const { return Node; }
39 unsigned getIndex() const { return Index; }
41 inline bool operator==(DSNode *N) const { return Node == N; }
42 inline bool operator!=(DSNode *N) const { return Node != N; }
44 // operator< - Allow insertion into a map...
45 bool operator<(const PointerVal &PV) const {
46 return Node < PV.Node || (Node == PV.Node && Index < PV.Index);
49 inline bool operator==(const PointerVal &PV) const {
50 return Node == PV.Node && Index == PV.Index;
52 inline bool operator!=(const PointerVal &PV) const { return !operator==(PV); }
54 void print(std::ostream &O) const;
58 // PointerValSet - This class represents a list of pointer values. The add
59 // method is used to add values to the set, and ensures that duplicates cannot
63 std::vector<PointerVal> Vals;
68 PointerValSet(const PointerValSet &PVS) : Vals(PVS.Vals) { addRefs(); }
69 ~PointerValSet() { dropRefs(); }
70 const PointerValSet &operator=(const PointerValSet &PVS);
72 // operator< - Allow insertion into a map...
73 bool operator<(const PointerValSet &PVS) const;
74 bool operator==(const PointerValSet &PVS) const;
76 const PointerVal &operator[](unsigned i) const { return Vals[i]; }
78 unsigned size() const { return Vals.size(); }
79 bool empty() const { return Vals.empty(); }
80 void clear() { dropRefs(); Vals.clear(); }
82 // add - Add the specified pointer, or contents of the specified PVS to this
83 // pointer set. If a 'Pointer' value is provided, notify the underlying data
84 // structure node that the pointer is pointing to it, so that it can be
85 // invalidated if neccesary later. True is returned if the value is new to
88 bool add(const PointerVal &PV, Value *Pointer = 0);
89 bool add(const PointerValSet &PVS, Value *Pointer = 0) {
91 for (unsigned i = 0, e = PVS.size(); i != e; ++i)
92 Changed |= add(PVS[i], Pointer);
96 // removePointerTo - Remove a single pointer val that points to the specified
98 void removePointerTo(DSNode *Node);
100 void print(std::ostream &O) const;
104 //===----------------------------------------------------------------------===//
105 // DSNode - Base class for all data structure nodes...
107 // This class keeps track of its type, the pointer fields in the data structure,
108 // and a list of LLVM values that are pointing to this node.
111 friend class FunctionDSGraph;
113 std::vector<PointerValSet> FieldLinks;
114 std::vector<Value*> Pointers; // Values pointing to me...
115 std::vector<PointerValSet*> Referrers;
117 DSNode(const DSNode &); // DO NOT IMPLEMENT
118 void operator=(const DSNode &); // DO NOT IMPLEMENT
121 NewNode, CallNode, ShadowNode, ArgNode, GlobalNode
124 DSNode(enum NodeTy NT, const Type *T);
127 assert(Referrers.empty() && "Referrers to dead node exist!");
130 typedef DSNodeIterator iterator;
131 inline iterator begin(); // Defined in DataStructureGraph.h
132 inline iterator end();
134 unsigned getNumLinks() const { return FieldLinks.size(); }
135 PointerValSet &getLink(unsigned i) {
136 assert(i < getNumLinks() && "Field links access out of range...");
137 return FieldLinks[i];
139 const PointerValSet &getLink(unsigned i) const {
140 assert(i < getNumLinks() && "Field links access out of range...");
141 return FieldLinks[i];
144 // addReferrer - Keep the referrer set up to date...
145 void addReferrer(PointerValSet *PVS) { Referrers.push_back(PVS); }
146 void removeReferrer(PointerValSet *PVS);
147 const std::vector<PointerValSet*> &getReferrers() const { return Referrers; }
149 // removeAllIncomingEdges - Erase all edges in the graph that point to
151 void removeAllIncomingEdges();
153 void addPointer(Value *V) { Pointers.push_back(V); }
154 const std::vector<Value*> &getPointers() const { return Pointers; }
156 const Type *getType() const { return Ty; }
158 // getNumOutgoingLinks - Return the number of outgoing links, which is usually
159 // the number of normal links, but for call nodes it also includes their
162 virtual unsigned getNumOutgoingLinks() const { return getNumLinks(); }
163 virtual PointerValSet &getOutgoingLink(unsigned Link) {
164 return getLink(Link);
166 virtual const PointerValSet &getOutgoingLink(unsigned Link) const {
167 return getLink(Link);
170 void print(std::ostream &O) const;
173 virtual std::string getCaption() const = 0;
174 virtual const std::vector<PointerValSet> *getAuxLinks() const {
175 return 0; // Default to nothing...
178 // isEquivalentTo - Return true if the nodes should be merged...
179 virtual bool isEquivalentTo(DSNode *Node) const = 0;
181 DSNode *clone() const {
182 DSNode *New = cloneImpl();
183 // Add all of the pointers to the new node...
184 for (unsigned pn = 0, pe = Pointers.size(); pn != pe; ++pn)
185 New->addPointer(Pointers[pn]);
190 virtual void dropAllReferences() {
194 static bool classof(const DSNode *N) { return true; }
196 virtual DSNode *cloneImpl() const = 0;
197 virtual void mapNode(std::map<const DSNode*, DSNode*> &NodeMap,
202 // AllocDSNode - Represent all allocation (malloc or alloca) in the program.
204 class AllocDSNode : public DSNode {
205 AllocationInst *Allocation;
207 AllocDSNode(AllocationInst *V);
209 virtual std::string getCaption() const;
211 bool isAllocaNode() const;
212 bool isMallocNode() const { return !isAllocaNode(); }
214 AllocationInst *getAllocation() const { return Allocation; }
216 // isEquivalentTo - Return true if the nodes should be merged...
217 virtual bool isEquivalentTo(DSNode *Node) const;
219 // Support type inquiry through isa, cast, and dyn_cast...
220 static bool classof(const AllocDSNode *) { return true; }
221 static bool classof(const DSNode *N) { return N->NodeType == NewNode; }
223 virtual AllocDSNode *cloneImpl() const { return new AllocDSNode(Allocation); }
227 // GlobalDSNode - Represent the memory location that a global variable occupies
229 class GlobalDSNode : public DSNode {
232 GlobalDSNode(GlobalValue *V);
234 GlobalValue *getGlobal() const { return Val; }
236 virtual std::string getCaption() const;
238 // isEquivalentTo - Return true if the nodes should be merged...
239 virtual bool isEquivalentTo(DSNode *Node) const;
241 // Support type inquiry through isa, cast, and dyn_cast...
242 static bool classof(const GlobalDSNode *) { return true; }
243 static bool classof(const DSNode *N) { return N->NodeType == GlobalNode; }
245 virtual GlobalDSNode *cloneImpl() const { return new GlobalDSNode(Val); }
249 // CallDSNode - Represent a call instruction in the program...
251 class CallDSNode : public DSNode {
252 friend class FunctionDSGraph;
254 std::vector<PointerValSet> ArgLinks;
256 CallDSNode(CallInst *CI);
258 CallInst *getCall() const { return CI; }
260 const std::vector<PointerValSet> *getAuxLinks() const { return &ArgLinks; }
261 virtual std::string getCaption() const;
263 bool addArgValue(unsigned ArgNo, const PointerValSet &PVS) {
264 return ArgLinks[ArgNo].add(PVS);
267 unsigned getNumArgs() const { return ArgLinks.size(); }
268 const PointerValSet &getArgValues(unsigned ArgNo) const {
269 assert(ArgNo < ArgLinks.size() && "Arg # out of range!");
270 return ArgLinks[ArgNo];
272 PointerValSet &getArgValues(unsigned ArgNo) {
273 assert(ArgNo < ArgLinks.size() && "Arg # out of range!");
274 return ArgLinks[ArgNo];
276 const std::vector<PointerValSet> &getArgs() const { return ArgLinks; }
278 virtual void dropAllReferences() {
279 DSNode::dropAllReferences();
283 // getNumOutgoingLinks - Return the number of outgoing links, which is usually
284 // the number of normal links, but for call nodes it also includes their
287 virtual unsigned getNumOutgoingLinks() const {
288 return getNumLinks() + getNumArgs();
290 virtual PointerValSet &getOutgoingLink(unsigned Link) {
291 if (Link < getNumLinks()) return getLink(Link);
292 return getArgValues(Link-getNumLinks());
294 virtual const PointerValSet &getOutgoingLink(unsigned Link) const {
295 if (Link < getNumLinks()) return getLink(Link);
296 return getArgValues(Link-getNumLinks());
299 // isEquivalentTo - Return true if the nodes should be merged...
300 virtual bool isEquivalentTo(DSNode *Node) const;
302 // Support type inquiry through isa, cast, and dyn_cast...
303 static bool classof(const CallDSNode *) { return true; }
304 static bool classof(const DSNode *N) { return N->NodeType == CallNode; }
306 virtual CallDSNode *cloneImpl() const { return new CallDSNode(CI); }
307 virtual void mapNode(std::map<const DSNode*, DSNode*> &NodeMap,
312 // ArgDSNode - Represent an incoming argument to the current function...
314 class ArgDSNode : public DSNode {
317 ArgDSNode(Argument *FA);
318 virtual std::string getCaption() const;
320 // isEquivalentTo - Return true if the nodes should be merged...
321 virtual bool isEquivalentTo(DSNode *Node) const;
323 // Support type inquiry through isa, cast, and dyn_cast...
324 static bool classof(const ArgDSNode *) { return true; }
325 static bool classof(const DSNode *N) { return N->NodeType == ArgNode; }
327 virtual ArgDSNode *cloneImpl() const { return new ArgDSNode(FuncArg); }
331 // ShadowDSNode - Represent a chunk of memory that we need to be able to
332 // address. These are generated due to (for example) pointer type method
333 // arguments... if the pointer is dereferenced, we need to have a node to point
334 // to. When functions are integrated into each other, shadow nodes are
337 // Shadow nodes may be marked as "critical" nodes when they are created. This
338 // mark indicates that the node is the result of a function call, the value
339 // pointed to by an incoming argument, or the value pointed to by a global
340 // variable [fixme todo]. Since it is not possible to know what these nodes
341 // point to, given just the current context, they are marked "Critical" to avoid
342 // having the shadow node merger eliminate them.
344 class ShadowDSNode : public DSNode {
345 friend class FunctionDSGraph;
347 ShadowDSNode *ShadowParent; // Nonnull if this is a synthesized node...
348 std::vector<std::pair<const Type *, ShadowDSNode *> > SynthNodes;
351 ShadowDSNode(const Type *Ty, Module *M, bool Critical = false);
352 virtual std::string getCaption() const;
354 // synthesizeNode - Create a new shadow node that is to be linked into this
357 ShadowDSNode *synthesizeNode(const Type *Ty, FunctionRepBuilder *Rep);
359 bool isCriticalNode() const { return CriticalNode; }
360 void resetCriticalMark() { CriticalNode = false; }
362 // isEquivalentTo - Return true if the nodes should be merged...
363 virtual bool isEquivalentTo(DSNode *Node) const;
365 // Support type inquiry through isa, cast, and dyn_cast...
366 static bool classof(const ShadowDSNode *) { return true; }
367 static bool classof(const DSNode *N) { return N->NodeType == ShadowNode; }
370 ShadowDSNode(const Type *Ty, Module *M, ShadowDSNode *ShadParent);
372 virtual void mapNode(std::map<const DSNode*, DSNode*> &NodeMap,
374 virtual ShadowDSNode *cloneImpl() const {
376 return new ShadowDSNode(getType(), Mod, ShadowParent);
378 return new ShadowDSNode(getType(), Mod, CriticalNode);
383 // FunctionDSGraph - The graph that represents a method.
385 class FunctionDSGraph {
387 std::vector<ArgDSNode*> ArgNodes;
388 std::vector<AllocDSNode*> AllocNodes;
389 std::vector<ShadowDSNode*> ShadowNodes;
390 std::vector<GlobalDSNode*> GlobalNodes;
391 std::vector<CallDSNode*> CallNodes;
392 PointerValSet RetNode; // Node that gets returned...
393 std::map<Value*, PointerValSet> ValueMap;
395 // cloneFunctionIntoSelf - Clone the specified method graph into the current
396 // method graph, returning the Return's set of the graph. If ValueMap is set
397 // to true, the ValueMap of the function is cloned into this function as well
398 // as the data structure graph itself.
400 PointerValSet cloneFunctionIntoSelf(const FunctionDSGraph &G, bool ValueMap);
401 bool RemoveUnreachableNodes();
402 bool UnlinkUndistinguishableNodes();
403 void MarkEscapeableNodesReachable(std::vector<bool> &RSN,
404 std::vector<bool> &RAN);
407 // Define the interface only accessable to DataStructure
408 friend class DataStructure;
409 FunctionDSGraph(Function *F);
410 FunctionDSGraph(const FunctionDSGraph &DSG);
413 void computeClosure(const DataStructure &DS);
416 Function *getFunction() const { return Func; }
418 // getEscapingAllocations - Add all allocations that escape the current
419 // function to the specified vector.
421 void getEscapingAllocations(std::vector<AllocDSNode*> &Allocs);
423 // getNonEscapingAllocations - Add all allocations that do not escape the
424 // current function to the specified vector.
426 void getNonEscapingAllocations(std::vector<AllocDSNode*> &Allocs);
428 // getValueMap - Get a map that describes what the nodes the scalars in this
429 // function point to...
431 std::map<Value*, PointerValSet> &getValueMap() { return ValueMap; }
433 const PointerValSet &getRetNodes() const { return RetNode; }
435 unsigned getGraphSize() const {
436 return ArgNodes.size() + AllocNodes.size() + ShadowNodes.size() +
437 GlobalNodes.size() + CallNodes.size();
440 void printFunction(std::ostream &O, const char *Label) const;
444 // FIXME: This should be a FunctionPass. When the pass framework sees a 'Pass'
445 // that uses the output of a FunctionPass, it should automatically build a map
446 // of output from the method pass that the pass can use.
448 class DataStructure : public Pass {
449 // DSInfo, one intraprocedural and one closed graph for each method...
450 typedef std::map<Function*, std::pair<FunctionDSGraph*,
451 FunctionDSGraph*> > InfoMap;
452 mutable InfoMap DSInfo;
454 static AnalysisID ID; // DataStructure Analysis ID
456 DataStructure(AnalysisID id) { assert(id == ID); }
457 ~DataStructure() { releaseMemory(); }
459 // run - Do nothing, because methods are analyzed lazily
460 virtual bool run(Module *TheModule) { return false; }
462 // getDSGraph - Return the data structure graph for the specified method.
463 // Since method graphs are lazily computed, we may have to create one on the
466 FunctionDSGraph &getDSGraph(Function *F) const {
467 std::pair<FunctionDSGraph*, FunctionDSGraph*> &N = DSInfo[F];
468 if (N.first) return *N.first;
469 return *(N.first = new FunctionDSGraph(F));
472 // getClosedDSGraph - Return the data structure graph for the specified
473 // method. Since method graphs are lazily computed, we may have to create one
474 // on the fly here. This is different than the normal DSGraph for the method
475 // because any function calls that are resolvable will have the data structure
476 // graphs of the called function incorporated into this function as well.
478 FunctionDSGraph &getClosedDSGraph(Function *F) const {
479 std::pair<FunctionDSGraph*, FunctionDSGraph*> &N = DSInfo[F];
480 if (N.second) return *N.second;
481 N.second = new FunctionDSGraph(getDSGraph(F));
482 N.second->computeClosure(*this);
486 // invalidateFunction - Inform this analysis that you changed the specified
487 // function, so the graphs that depend on it are out of date.
489 void invalidateFunction(Function *F) const {
490 // FIXME: THis should invalidate all functions who have inlined the
493 std::pair<FunctionDSGraph*, FunctionDSGraph*> &N = DSInfo[F];
496 N.first = N.second = 0;
499 // print - Print out the analysis results...
500 void print(std::ostream &O, Module *M) const;
502 // If the pass pipeline is done with this pass, we can release our memory...
503 virtual void releaseMemory();
505 // getAnalysisUsageInfo - This obviously provides a call graph
506 virtual void getAnalysisUsageInfo(AnalysisSet &Required,
507 AnalysisSet &Destroyed,
508 AnalysisSet &Provided) {
509 Provided.push_back(ID);