-//===- CallGraph.h - Build a Module's call graph -----------------*- C++ -*--=//
+//===- CallGraph.h - Build a Module's call graph ----------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
//
// This interface is used to build and manipulate a call graph, which is a very
// useful tool for interprocedural optimization.
#define LLVM_ANALYSIS_CALLGRAPH_H
#include "Support/GraphTraits.h"
+#include "Support/STLExtras.h"
#include "llvm/Pass.h"
+
+namespace llvm {
+
class Function;
class Module;
class CallGraphNode;
// Functions to keep a call graph up to date with a function that has been
// modified
//
- void addFunctionToModule(Function *Meth);
+ void addFunctionToModule(Function *F);
// removeFunctionFromModule - Unlink the function from this module, returning
// is to dropAllReferences before calling this.
//
Function *removeFunctionFromModule(CallGraphNode *CGN);
- Function *removeFunctionFromModule(Function *Meth) {
- return removeFunctionFromModule((*this)[Meth]);
+ Function *removeFunctionFromModule(Function *F) {
+ return removeFunctionFromModule((*this)[F]);
}
//===---------------------------------------------------------------------
// Pass infrastructure interface glue code...
//
- static AnalysisID ID; // We are an analysis, we must have an ID
-
CallGraph() : Root(0) {}
~CallGraph() { destroy(); }
destroy();
}
+ /// Print the types found in the module. If the optional Module parameter is
+ /// passed in, then the types are printed symbolically if possible, using the
+ /// symbol table from the module.
+ ///
+ void print(std::ostream &o, const Module *M) const;
+
+ // stub - dummy function, just ignore it
+ static void stub();
private:
//===---------------------------------------------------------------------
// Implementation of CallGraph construction
// CallGraphNode class definition
//
class CallGraphNode {
- Function *Meth;
+ Function *F;
std::vector<CallGraphNode*> CalledFunctions;
CallGraphNode(const CallGraphNode &); // Do not implement
typedef std::vector<CallGraphNode*>::const_iterator const_iterator;
// getFunction - Return the function that this call graph node represents...
- Function *getFunction() const { return Meth; }
+ Function *getFunction() const { return F; }
inline iterator begin() { return CalledFunctions.begin(); }
inline iterator end() { return CalledFunctions.end(); }
friend class CallGraph;
// CallGraphNode ctor - Create a node for the specified function...
- inline CallGraphNode(Function *F) : Meth(F) {}
+ inline CallGraphNode(Function *f) : F(f) {}
// addCalledFunction add a function to the list of functions called by this
// one
static inline ChildIteratorType child_end (NodeType *N) { return N->end(); }
};
-
-template<> struct GraphTraits<CallGraph*> :
- public GraphTraits<CallGraphNode*> {
+template<> struct GraphTraits<CallGraph*> : public GraphTraits<CallGraphNode*> {
static NodeType *getEntryNode(CallGraph *CGN) {
return CGN->getExternalNode(); // Start at the external node!
}
+ typedef std::pair<const Function*, CallGraphNode*> PairTy;
+ typedef std::pointer_to_unary_function<PairTy, CallGraphNode&> DerefFun;
+
+ // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
+ typedef mapped_iterator<CallGraph::iterator, DerefFun> nodes_iterator;
+ static nodes_iterator nodes_begin(CallGraph *CG) {
+ return map_iterator(CG->begin(), DerefFun(CGdereference));
+ }
+ static nodes_iterator nodes_end (CallGraph *CG) {
+ return map_iterator(CG->end(), DerefFun(CGdereference));
+ }
+
+ static CallGraphNode &CGdereference (std::pair<const Function*,
+ CallGraphNode*> P) {
+ return *P.second;
+ }
};
template<> struct GraphTraits<const CallGraph*> :
public GraphTraits<const CallGraphNode*> {
static NodeType *getEntryNode(const CallGraph *CGN) {
return CGN->getExternalNode();
}
+ // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
+ typedef CallGraph::const_iterator nodes_iterator;
+ static nodes_iterator nodes_begin(const CallGraph *CG) { return CG->begin(); }
+ static nodes_iterator nodes_end (const CallGraph *CG) { return CG->end(); }
};
+// Make sure that any clients of this file link in PostDominators.cpp
+static IncludeFile
+CALLGRAPH_INCLUDE_FILE((void*)&CallGraph::stub);
-//===----------------------------------------------------------------------===//
-// Printing support for Call Graphs
-//
-
-// Stuff for printing out a callgraph...
-
-void WriteToOutput(const CallGraph &, std::ostream &o);
-inline std::ostream &operator <<(std::ostream &o, const CallGraph &CG) {
- WriteToOutput(CG, o); return o;
-}
-
-void WriteToOutput(const CallGraphNode *, std::ostream &o);
-inline std::ostream &operator <<(std::ostream &o, const CallGraphNode *CGN) {
- WriteToOutput(CGN, o); return o;
-}
+} // End llvm namespace
#endif