1 //===- DataStructure.h - Build a Module's call graph -------------*- 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"
16 class FunctionArgument;
18 class FunctionRepBuilder;
20 class FunctionDSGraph;
23 // FIXME: move this somewhere private
24 unsigned countPointerFields(const Type *Ty);
26 // PointerVal - Represent a pointer to a datastructure. The pointer points to
27 // a node, and can index into it. This is used for getelementptr instructions,
28 // which do not affect which node a pointer points to, but does change the field
33 unsigned Index; // Index into Node->FieldLinks[]
35 PointerVal(DSNode *N, unsigned Idx = 0) : Node(N), Index(Idx) {}
37 DSNode *getNode() const { return Node; }
38 unsigned getIndex() const { return Index; }
40 inline bool operator==(DSNode *N) const { return Node == N; }
41 inline bool operator!=(DSNode *N) const { return Node != N; }
43 // operator< - Allow insertion into a map...
44 bool operator<(const PointerVal &PV) const {
45 return Node < PV.Node || (Node == PV.Node && Index < PV.Index);
48 inline bool operator==(const PointerVal &PV) const {
49 return Node == PV.Node && Index == PV.Index;
51 inline bool operator!=(const PointerVal &PV) const { return !operator==(PV); }
53 void print(std::ostream &O) const;
57 // PointerValSet - This class represents a list of pointer values. The add
58 // method is used to add values to the set, and ensures that duplicates cannot
62 std::vector<PointerVal> Vals;
67 PointerValSet(const PointerValSet &PVS) : Vals(PVS.Vals) { addRefs(); }
68 ~PointerValSet() { dropRefs(); }
69 const PointerValSet &operator=(const PointerValSet &PVS);
71 // operator< - Allow insertion into a map...
72 bool operator<(const PointerValSet &PVS) const;
73 bool operator==(const PointerValSet &PVS) const;
75 const PointerVal &operator[](unsigned i) const { return Vals[i]; }
77 unsigned size() const { return Vals.size(); }
78 bool empty() const { return Vals.empty(); }
79 void clear() { dropRefs(); Vals.clear(); }
81 // add - Add the specified pointer, or contents of the specified PVS to this
82 // pointer set. If a 'Pointer' value is provided, notify the underlying data
83 // structure node that the pointer is pointing to it, so that it can be
84 // invalidated if neccesary later. True is returned if the value is new to
87 bool add(const PointerVal &PV, Value *Pointer = 0);
88 bool add(const PointerValSet &PVS, Value *Pointer = 0) {
90 for (unsigned i = 0, e = PVS.size(); i != e; ++i)
91 Changed |= add(PVS[i], Pointer);
95 // removePointerTo - Remove a single pointer val that points to the specified
97 void removePointerTo(DSNode *Node);
99 void print(std::ostream &O) const;
103 //===----------------------------------------------------------------------===//
104 // DSNode - Base class for all data structure nodes...
106 // This class keeps track of its type, the pointer fields in the data structure,
107 // and a list of LLVM values that are pointing to this node.
110 friend class FunctionDSGraph;
112 std::vector<PointerValSet> FieldLinks;
113 std::vector<Value*> Pointers; // Values pointing to me...
114 std::vector<PointerValSet*> Referrers;
116 DSNode(const DSNode &); // DO NOT IMPLEMENT
117 void operator=(const DSNode &); // DO NOT IMPLEMENT
120 NewNode, CallNode, ShadowNode, ArgNode, GlobalNode
123 DSNode(enum NodeTy NT, const Type *T);
126 assert(Referrers.empty() && "Referrers to dead node exist!");
129 unsigned getNumLinks() const { return FieldLinks.size(); }
130 PointerValSet &getLink(unsigned i) {
131 assert(i < getNumLinks() && "Field links access out of range...");
132 return FieldLinks[i];
135 // addReferrer - Keep the referrer set up to date...
136 void addReferrer(PointerValSet *PVS) { Referrers.push_back(PVS); }
137 void removeReferrer(PointerValSet *PVS);
138 const std::vector<PointerValSet*> &getReferrers() const { return Referrers; }
140 // removeAllIncomingEdges - Erase all edges in the graph that point to
142 void removeAllIncomingEdges();
144 void addPointer(Value *V) { Pointers.push_back(V); }
145 const std::vector<Value*> &getPointers() const { return Pointers; }
147 const Type *getType() const { return Ty; }
149 void print(std::ostream &O) const;
151 virtual std::string getCaption() const = 0;
152 virtual const std::vector<PointerValSet> *getAuxLinks() const {
153 return 0; // Default to nothing...
156 // isEquivalentTo - Return true if the nodes should be merged...
157 virtual bool isEquivalentTo(DSNode *Node) const = 0;
159 DSNode *clone() const {
160 DSNode *New = cloneImpl();
161 // Add all of the pointers to the new node...
162 for (unsigned pn = 0, pe = Pointers.size(); pn != pe; ++pn)
163 New->addPointer(Pointers[pn]);
168 virtual void dropAllReferences() {
172 static bool classof(const DSNode *N) { return true; }
174 virtual DSNode *cloneImpl() const = 0;
175 virtual void mapNode(std::map<const DSNode*, DSNode*> &NodeMap,
180 // AllocDSNode - Represent all allocation (malloc or alloca) in the program.
182 class AllocDSNode : public DSNode {
183 AllocationInst *Allocation;
185 AllocDSNode(AllocationInst *V);
187 virtual std::string getCaption() const;
189 bool isAllocaNode() const;
190 bool isMallocNode() const { return !isAllocaNode(); }
192 AllocationInst *getAllocation() const { return Allocation; }
194 // isEquivalentTo - Return true if the nodes should be merged...
195 virtual bool isEquivalentTo(DSNode *Node) const;
197 // Support type inquiry through isa, cast, and dyn_cast...
198 static bool classof(const AllocDSNode *) { return true; }
199 static bool classof(const DSNode *N) { return N->NodeType == NewNode; }
201 virtual AllocDSNode *cloneImpl() const { return new AllocDSNode(Allocation); }
205 // GlobalDSNode - Represent the memory location that a global variable occupies
207 class GlobalDSNode : public DSNode {
210 GlobalDSNode(GlobalValue *V);
212 GlobalValue *getGlobal() const { return Val; }
214 virtual std::string getCaption() const;
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 GlobalDSNode *) { return true; }
221 static bool classof(const DSNode *N) { return N->NodeType == GlobalNode; }
223 virtual GlobalDSNode *cloneImpl() const { return new GlobalDSNode(Val); }
227 // CallDSNode - Represent a call instruction in the program...
229 class CallDSNode : public DSNode {
230 friend class FunctionDSGraph;
232 std::vector<PointerValSet> ArgLinks;
234 CallDSNode(CallInst *CI);
236 CallInst *getCall() const { return CI; }
238 const std::vector<PointerValSet> *getAuxLinks() const { return &ArgLinks; }
239 virtual std::string getCaption() const;
241 bool addArgValue(unsigned ArgNo, const PointerValSet &PVS) {
242 return ArgLinks[ArgNo].add(PVS);
245 unsigned getNumArgs() const { return ArgLinks.size(); }
246 const PointerValSet &getArgValues(unsigned ArgNo) const {
247 assert(ArgNo < ArgLinks.size() && "Arg # out of range!");
248 return ArgLinks[ArgNo];
250 const std::vector<PointerValSet> &getArgs() const { return ArgLinks; }
252 virtual void dropAllReferences() {
253 DSNode::dropAllReferences();
257 // isEquivalentTo - Return true if the nodes should be merged...
258 virtual bool isEquivalentTo(DSNode *Node) const;
260 // Support type inquiry through isa, cast, and dyn_cast...
261 static bool classof(const CallDSNode *) { return true; }
262 static bool classof(const DSNode *N) { return N->NodeType == CallNode; }
264 virtual CallDSNode *cloneImpl() const { return new CallDSNode(CI); }
265 virtual void mapNode(std::map<const DSNode*, DSNode*> &NodeMap,
270 // ArgDSNode - Represent an incoming argument to the current function...
272 class ArgDSNode : public DSNode {
273 FunctionArgument *FuncArg;
275 ArgDSNode(FunctionArgument *MA);
276 virtual std::string getCaption() const;
278 // isEquivalentTo - Return true if the nodes should be merged...
279 virtual bool isEquivalentTo(DSNode *Node) const;
281 // Support type inquiry through isa, cast, and dyn_cast...
282 static bool classof(const ArgDSNode *) { return true; }
283 static bool classof(const DSNode *N) { return N->NodeType == ArgNode; }
285 virtual ArgDSNode *cloneImpl() const { return new ArgDSNode(FuncArg); }
289 // ShadowDSNode - Represent a chunk of memory that we need to be able to
290 // address. These are generated due to (for example) pointer type method
291 // arguments... if the pointer is dereferenced, we need to have a node to point
292 // to. When functions are integrated into each other, shadow nodes are
295 // Shadow nodes may be marked as "critical" nodes when they are created. This
296 // mark indicates that the node is the result of a function call, the value
297 // pointed to by an incoming argument, or the value pointed to by a global
298 // variable [fixme todo]. Since it is not possible to know what these nodes
299 // point to, given just the current context, they are marked "Critical" to avoid
300 // having the shadow node merger eliminate them.
302 class ShadowDSNode : public DSNode {
303 friend class FunctionDSGraph;
306 ShadowDSNode *ShadowParent; // Nonnull if this is a synthesized node...
307 std::vector<std::pair<const Type *, ShadowDSNode *> > SynthNodes;
310 ShadowDSNode(DSNode *Parent, Module *M, bool Critical = false);
311 virtual std::string getCaption() const;
313 // synthesizeNode - Create a new shadow node that is to be linked into this
316 ShadowDSNode *synthesizeNode(const Type *Ty, FunctionRepBuilder *Rep);
318 bool isCriticalNode() const { return CriticalNode; }
319 void resetCriticalMark() { CriticalNode = false; }
321 // isEquivalentTo - Return true if the nodes should be merged...
322 virtual bool isEquivalentTo(DSNode *Node) const;
324 // Support type inquiry through isa, cast, and dyn_cast...
325 static bool classof(const ShadowDSNode *) { return true; }
326 static bool classof(const DSNode *N) { return N->NodeType == ShadowNode; }
329 ShadowDSNode(const Type *Ty, Module *M, ShadowDSNode *ShadParent);
331 virtual void mapNode(std::map<const DSNode*, DSNode*> &NodeMap,
333 virtual ShadowDSNode *cloneImpl() const {
335 return new ShadowDSNode(getType(), Mod, ShadowParent);
337 return new ShadowDSNode(Parent, Mod, CriticalNode);
342 // FunctionDSGraph - The graph that represents a method.
344 class FunctionDSGraph {
346 std::vector<ArgDSNode*> ArgNodes;
347 std::vector<AllocDSNode*> AllocNodes;
348 std::vector<ShadowDSNode*> ShadowNodes;
349 std::vector<GlobalDSNode*> GlobalNodes;
350 std::vector<CallDSNode*> CallNodes;
351 PointerValSet RetNode; // Node that gets returned...
352 std::map<Value*, PointerValSet> ValueMap;
354 // cloneFunctionIntoSelf - Clone the specified method graph into the current
355 // method graph, returning the Return's set of the graph. If ValueMap is set
356 // to true, the ValueMap of the function is cloned into this function as well
357 // as the data structure graph itself.
359 PointerValSet cloneFunctionIntoSelf(const FunctionDSGraph &G, bool ValueMap);
360 bool RemoveUnreachableNodes();
361 bool UnlinkUndistinguishableNodes();
362 void MarkEscapeableNodesReachable(std::vector<bool> &RSN,
363 std::vector<bool> &RAN);
366 // Define the interface only accessable to DataStructure
367 friend class DataStructure;
368 FunctionDSGraph(Function *F);
369 FunctionDSGraph(const FunctionDSGraph &DSG);
372 void computeClosure(const DataStructure &DS);
375 Function *getFunction() const { return Func; }
377 // getEscapingAllocations - Add all allocations that escape the current
378 // function to the specified vector.
380 void getEscapingAllocations(std::vector<AllocDSNode*> &Allocs);
382 // getNonEscapingAllocations - Add all allocations that do not escape the
383 // current function to the specified vector.
385 void getNonEscapingAllocations(std::vector<AllocDSNode*> &Allocs);
387 // getValueMap - Get a map that describes what the nodes the scalars in this
388 // function point to...
390 std::map<Value*, PointerValSet> &getValueMap() { return ValueMap; }
394 void printFunction(std::ostream &O, const char *Label) const;
398 // FIXME: This should be a FunctionPass. When the pass framework sees a 'Pass'
399 // that uses the output of a FunctionPass, it should automatically build a map
400 // of output from the method pass that the pass can use.
402 class DataStructure : public Pass {
403 // DSInfo, one intraprocedural and one closed graph for each method...
404 typedef std::map<Function*, std::pair<FunctionDSGraph*,
405 FunctionDSGraph*> > InfoMap;
406 mutable InfoMap DSInfo;
408 static AnalysisID ID; // DataStructure Analysis ID
410 DataStructure(AnalysisID id) { assert(id == ID); }
411 ~DataStructure() { releaseMemory(); }
413 // run - Do nothing, because methods are analyzed lazily
414 virtual bool run(Module *TheModule) { return false; }
416 // getDSGraph - Return the data structure graph for the specified method.
417 // Since method graphs are lazily computed, we may have to create one on the
420 FunctionDSGraph &getDSGraph(Function *F) const {
421 std::pair<FunctionDSGraph*, FunctionDSGraph*> &N = DSInfo[F];
422 if (N.first) return *N.first;
423 return *(N.first = new FunctionDSGraph(F));
426 // getClosedDSGraph - Return the data structure graph for the specified
427 // method. Since method graphs are lazily computed, we may have to create one
428 // on the fly here. This is different than the normal DSGraph for the method
429 // because any function calls that are resolvable will have the data structure
430 // graphs of the called function incorporated into this function as well.
432 FunctionDSGraph &getClosedDSGraph(Function *F) const {
433 std::pair<FunctionDSGraph*, FunctionDSGraph*> &N = DSInfo[F];
434 if (N.second) return *N.second;
435 N.second = new FunctionDSGraph(getDSGraph(F));
436 N.second->computeClosure(*this);
440 // invalidateFunction - Inform this analysis that you changed the specified
441 // function, so the graphs that depend on it are out of date.
443 void invalidateFunction(Function *F) const {
444 // FIXME: THis should invalidate all functions who have inlined the
447 std::pair<FunctionDSGraph*, FunctionDSGraph*> &N = DSInfo[F];
450 N.first = N.second = 0;
453 // print - Print out the analysis results...
454 void print(std::ostream &O, Module *M) const;
456 // If the pass pipeline is done with this pass, we can release our memory...
457 virtual void releaseMemory();
459 // getAnalysisUsageInfo - This obviously provides a call graph
460 virtual void getAnalysisUsageInfo(AnalysisSet &Required,
461 AnalysisSet &Destroyed,
462 AnalysisSet &Provided) {
463 Provided.push_back(ID);