2008-05-06 16:34:12 +00:00
|
|
|
//===--- CompilationGraph.h - The LLVM Compiler Driver ----------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open
|
|
|
|
// Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Compilation graph - definition.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H
|
|
|
|
#define LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H
|
|
|
|
|
|
|
|
#include "AutoGenerated.h"
|
|
|
|
#include "Tool.h"
|
|
|
|
|
2008-05-06 16:35:25 +00:00
|
|
|
#include "llvm/ADT/GraphTraits.h"
|
2008-05-06 16:36:06 +00:00
|
|
|
#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
2008-05-06 16:35:25 +00:00
|
|
|
#include "llvm/ADT/iterator"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2008-05-06 16:34:12 +00:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/System/Path.h"
|
|
|
|
|
2008-05-06 18:18:58 +00:00
|
|
|
#include <cassert>
|
2008-05-06 16:35:25 +00:00
|
|
|
#include <string>
|
|
|
|
|
2008-05-06 18:08:59 +00:00
|
|
|
namespace llvmc {
|
2008-05-06 16:34:12 +00:00
|
|
|
|
2008-05-06 18:18:58 +00:00
|
|
|
// A wrapper for StringMap that provides set-like functionality.
|
|
|
|
// Only insert() and count() methods are used by my code.
|
|
|
|
template <class AllocatorTy = llvm::MallocAllocator>
|
|
|
|
class StringSet : public llvm::StringMap<char, AllocatorTy> {
|
|
|
|
typedef llvm::StringMap<char, AllocatorTy> base;
|
|
|
|
public:
|
|
|
|
void insert (const std::string& InLang) {
|
|
|
|
assert(!InLang.empty());
|
|
|
|
const char* KeyStart = &InLang[0];
|
|
|
|
const char* KeyEnd = KeyStart + InLang.size();
|
|
|
|
base::insert(llvm::StringMapEntry<char>::
|
|
|
|
Create(KeyStart, KeyEnd, base::getAllocator(), '+'));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
typedef StringSet<> InputLanguagesSet;
|
2008-05-06 18:15:12 +00:00
|
|
|
|
2008-05-06 18:10:20 +00:00
|
|
|
// An edge of the compilation graph.
|
2008-05-06 16:36:06 +00:00
|
|
|
class Edge : public llvm::RefCountedBaseVPTR<Edge> {
|
|
|
|
public:
|
|
|
|
Edge(const std::string& T) : ToolName_(T) {}
|
|
|
|
virtual ~Edge() {};
|
|
|
|
|
|
|
|
const std::string& ToolName() const { return ToolName_; }
|
2008-05-06 18:15:12 +00:00
|
|
|
virtual unsigned Weight(const InputLanguagesSet& InLangs) const = 0;
|
2008-05-06 16:36:06 +00:00
|
|
|
private:
|
|
|
|
std::string ToolName_;
|
|
|
|
};
|
|
|
|
|
2008-05-06 18:10:20 +00:00
|
|
|
// Edges that have no properties are instances of this class.
|
2008-05-06 16:36:50 +00:00
|
|
|
class SimpleEdge : public Edge {
|
2008-05-06 16:36:06 +00:00
|
|
|
public:
|
2008-05-06 16:36:50 +00:00
|
|
|
SimpleEdge(const std::string& T) : Edge(T) {}
|
2008-05-06 18:15:12 +00:00
|
|
|
unsigned Weight(const InputLanguagesSet&) const { return 1; }
|
2008-05-06 16:36:06 +00:00
|
|
|
};
|
2008-05-06 16:35:25 +00:00
|
|
|
|
2008-05-06 18:10:20 +00:00
|
|
|
// A node of the compilation graph.
|
2008-05-06 16:35:25 +00:00
|
|
|
struct Node {
|
2008-05-06 18:10:20 +00:00
|
|
|
// A Node holds a list of the outward edges.
|
2008-05-06 16:36:06 +00:00
|
|
|
typedef llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> container_type;
|
|
|
|
typedef container_type::iterator iterator;
|
|
|
|
typedef container_type::const_iterator const_iterator;
|
2008-05-06 16:35:25 +00:00
|
|
|
|
2008-05-06 17:26:53 +00:00
|
|
|
Node() : OwningGraph(0), InEdges(0) {}
|
|
|
|
Node(CompilationGraph* G) : OwningGraph(G), InEdges(0) {}
|
|
|
|
Node(CompilationGraph* G, Tool* T) :
|
|
|
|
OwningGraph(G), ToolPtr(T), InEdges(0) {}
|
2008-05-06 16:35:25 +00:00
|
|
|
|
2008-05-06 16:36:50 +00:00
|
|
|
bool HasChildren() const { return !OutEdges.empty(); }
|
2008-05-06 18:07:14 +00:00
|
|
|
const std::string Name() const
|
|
|
|
{ return ToolPtr ? ToolPtr->Name() : "root"; }
|
2008-05-06 16:36:06 +00:00
|
|
|
|
2008-05-06 17:26:53 +00:00
|
|
|
// Iteration.
|
2008-05-06 16:36:06 +00:00
|
|
|
iterator EdgesBegin() { return OutEdges.begin(); }
|
|
|
|
const_iterator EdgesBegin() const { return OutEdges.begin(); }
|
|
|
|
iterator EdgesEnd() { return OutEdges.end(); }
|
|
|
|
const_iterator EdgesEnd() const { return OutEdges.end(); }
|
|
|
|
|
2008-05-06 17:26:53 +00:00
|
|
|
// Add an outward edge. Takes ownership of the Edge object.
|
2008-05-06 16:36:06 +00:00
|
|
|
void AddEdge(Edge* E)
|
|
|
|
{ OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(E)); }
|
|
|
|
|
2008-05-06 18:10:20 +00:00
|
|
|
// Inward edge counter. Used to implement topological sort.
|
|
|
|
// TOTHINK: Move the mutable counter back into Tool classes? Makes
|
|
|
|
// us more const-correct.
|
2008-05-06 17:26:53 +00:00
|
|
|
void IncrInEdges() { ++InEdges; }
|
|
|
|
void DecrInEdges() { --InEdges; }
|
|
|
|
bool HasNoInEdges() const { return InEdges == 0; }
|
|
|
|
|
2008-05-06 16:35:25 +00:00
|
|
|
// Needed to implement NodeChildIterator/GraphTraits
|
|
|
|
CompilationGraph* OwningGraph;
|
|
|
|
// The corresponding Tool.
|
2008-05-06 18:10:20 +00:00
|
|
|
// WARNING: ToolPtr can be NULL (for the root node).
|
2008-05-06 16:35:25 +00:00
|
|
|
llvm::IntrusiveRefCntPtr<Tool> ToolPtr;
|
|
|
|
// Links to children.
|
2008-05-06 16:36:06 +00:00
|
|
|
container_type OutEdges;
|
2008-05-06 18:10:20 +00:00
|
|
|
// Inward edge counter. Updated in
|
|
|
|
// CompilationGraph::insertEdge(). Used for topological sorting.
|
2008-05-06 17:26:53 +00:00
|
|
|
unsigned InEdges;
|
2008-05-06 16:35:25 +00:00
|
|
|
};
|
|
|
|
|
2008-05-06 16:36:06 +00:00
|
|
|
class NodesIterator;
|
2008-05-06 16:35:25 +00:00
|
|
|
|
2008-05-06 18:07:14 +00:00
|
|
|
// The compilation graph itself.
|
2008-05-06 16:35:25 +00:00
|
|
|
class CompilationGraph {
|
2008-05-06 17:28:03 +00:00
|
|
|
// Main data structure.
|
2008-05-06 16:36:06 +00:00
|
|
|
typedef llvm::StringMap<Node> nodes_map_type;
|
2008-05-06 18:10:20 +00:00
|
|
|
// These are used to map from language names to tools. (We can
|
|
|
|
// have several tools associated with each language name, hence
|
|
|
|
// the need for a vector of Edges.)
|
2008-05-06 17:28:03 +00:00
|
|
|
typedef
|
|
|
|
llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> tools_vector_type;
|
|
|
|
typedef llvm::StringMap<tools_vector_type> tools_map_type;
|
2008-05-06 16:35:25 +00:00
|
|
|
|
|
|
|
// Map from file extensions to language names.
|
2008-05-06 16:34:12 +00:00
|
|
|
LanguageMap ExtsToLangs;
|
2008-05-06 16:35:25 +00:00
|
|
|
// Map from language names to lists of tool names.
|
|
|
|
tools_map_type ToolsMap;
|
|
|
|
// Map from tool names to Tool objects.
|
|
|
|
nodes_map_type NodesMap;
|
|
|
|
|
|
|
|
public:
|
2008-05-06 16:34:12 +00:00
|
|
|
|
2008-05-06 16:35:25 +00:00
|
|
|
CompilationGraph();
|
|
|
|
|
2008-05-06 16:36:50 +00:00
|
|
|
// insertVertex - insert a new node into the graph. Takes
|
|
|
|
// ownership of the object.
|
2008-05-06 16:36:06 +00:00
|
|
|
void insertNode(Tool* T);
|
2008-05-06 16:35:25 +00:00
|
|
|
|
2008-05-06 16:36:50 +00:00
|
|
|
// insertEdge - Insert a new edge into the graph. Takes ownership
|
2008-05-06 17:26:53 +00:00
|
|
|
// of the Edge object.
|
2008-05-06 16:36:50 +00:00
|
|
|
void insertEdge(const std::string& A, Edge* E);
|
2008-05-06 16:35:25 +00:00
|
|
|
|
2008-05-06 16:36:50 +00:00
|
|
|
// Build - Build target(s) from the input file set. Command-line
|
|
|
|
// options are passed implicitly as global variables.
|
2008-05-06 18:07:14 +00:00
|
|
|
int Build(llvm::sys::Path const& tempDir);
|
2008-05-06 16:35:25 +00:00
|
|
|
|
2008-05-06 16:36:06 +00:00
|
|
|
// Return a reference to the node correponding to the given tool
|
|
|
|
// name. Throws std::runtime_error.
|
|
|
|
Node& getNode(const std::string& ToolName);
|
|
|
|
const Node& getNode(const std::string& ToolName) const;
|
|
|
|
|
|
|
|
// viewGraph - This function is meant for use from the debugger.
|
|
|
|
// You can just say 'call G->viewGraph()' and a ghostview window
|
|
|
|
// should pop up from the program, displaying the compilation
|
|
|
|
// graph. This depends on there being a 'dot' and 'gv' program
|
|
|
|
// in your path.
|
2008-05-06 16:35:25 +00:00
|
|
|
void viewGraph();
|
|
|
|
|
2008-05-06 16:36:06 +00:00
|
|
|
// Write a CompilationGraph.dot file.
|
2008-05-06 16:35:25 +00:00
|
|
|
void writeGraph();
|
|
|
|
|
|
|
|
// GraphTraits support
|
2008-05-06 16:36:06 +00:00
|
|
|
friend NodesIterator GraphBegin(CompilationGraph*);
|
|
|
|
friend NodesIterator GraphEnd(CompilationGraph*);
|
|
|
|
friend void PopulateCompilationGraph(CompilationGraph&);
|
2008-05-06 16:35:25 +00:00
|
|
|
|
2008-05-06 16:36:06 +00:00
|
|
|
private:
|
|
|
|
// Helper functions.
|
2008-05-06 16:35:25 +00:00
|
|
|
|
2008-05-06 16:36:06 +00:00
|
|
|
// Find out which language corresponds to the suffix of this file.
|
|
|
|
const std::string& getLanguage(const llvm::sys::Path& File) const;
|
2008-05-06 16:35:25 +00:00
|
|
|
|
2008-05-06 16:36:06 +00:00
|
|
|
// Return a reference to the list of tool names corresponding to
|
|
|
|
// the given language name. Throws std::runtime_error.
|
|
|
|
const tools_vector_type& getToolsVector(const std::string& LangName) const;
|
2008-05-06 17:25:51 +00:00
|
|
|
|
2008-05-06 18:15:35 +00:00
|
|
|
// Pass the input file through the toolchain starting at StartNode.
|
2008-05-06 18:10:20 +00:00
|
|
|
void PassThroughGraph (const llvm::sys::Path& In, const Node* StartNode,
|
2008-05-06 18:15:12 +00:00
|
|
|
const InputLanguagesSet& InLangs,
|
2008-05-06 18:10:20 +00:00
|
|
|
const llvm::sys::Path& TempDir) const;
|
2008-05-06 17:25:51 +00:00
|
|
|
|
2008-05-06 18:07:14 +00:00
|
|
|
// Find head of the toolchain corresponding to the given file.
|
2008-05-06 18:10:53 +00:00
|
|
|
const Node* FindToolChain(const llvm::sys::Path& In,
|
2008-05-06 18:15:12 +00:00
|
|
|
const std::string* forceLanguage,
|
|
|
|
InputLanguagesSet& InLangs) const;
|
2008-05-06 18:07:14 +00:00
|
|
|
|
2008-05-06 18:15:35 +00:00
|
|
|
// Traverse the initial parts of the toolchains.
|
|
|
|
void BuildInitial(InputLanguagesSet& InLangs,
|
|
|
|
const llvm::sys::Path& TempDir);
|
|
|
|
|
2008-05-06 18:07:14 +00:00
|
|
|
// Sort the nodes in topological order.
|
|
|
|
void TopologicalSort(std::vector<const Node*>& Out);
|
|
|
|
// Call TopologicalSort and filter the resulting list to include
|
|
|
|
// only Join nodes.
|
|
|
|
void TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out);
|
2008-05-06 16:36:06 +00:00
|
|
|
};
|
2008-05-06 16:35:25 +00:00
|
|
|
|
2008-05-06 16:36:06 +00:00
|
|
|
/// GraphTraits support code.
|
2008-05-06 16:35:25 +00:00
|
|
|
|
2008-05-06 16:36:06 +00:00
|
|
|
// Auxiliary class needed to implement GraphTraits support. Can be
|
|
|
|
// generalised to something like value_iterator for map-like
|
|
|
|
// containers.
|
|
|
|
class NodesIterator : public llvm::StringMap<Node>::iterator {
|
|
|
|
typedef llvm::StringMap<Node>::iterator super;
|
|
|
|
typedef NodesIterator ThisType;
|
|
|
|
typedef Node* pointer;
|
|
|
|
typedef Node& reference;
|
2008-05-06 16:35:25 +00:00
|
|
|
|
2008-05-06 16:36:06 +00:00
|
|
|
public:
|
|
|
|
NodesIterator(super I) : super(I) {}
|
2008-05-06 16:35:25 +00:00
|
|
|
|
2008-05-06 16:36:06 +00:00
|
|
|
inline reference operator*() const {
|
|
|
|
return super::operator->()->second;
|
|
|
|
}
|
|
|
|
inline pointer operator->() const {
|
|
|
|
return &super::operator->()->second;
|
|
|
|
}
|
2008-05-06 16:35:25 +00:00
|
|
|
};
|
|
|
|
|
2008-05-06 16:36:06 +00:00
|
|
|
inline NodesIterator GraphBegin(CompilationGraph* G) {
|
|
|
|
return NodesIterator(G->NodesMap.begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline NodesIterator GraphEnd(CompilationGraph* G) {
|
|
|
|
return NodesIterator(G->NodesMap.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Another auxiliary class needed by GraphTraits.
|
2008-05-06 16:35:25 +00:00
|
|
|
class NodeChildIterator : public bidirectional_iterator<Node, ptrdiff_t> {
|
|
|
|
typedef NodeChildIterator ThisType;
|
2008-05-06 16:36:06 +00:00
|
|
|
typedef Node::container_type::iterator iterator;
|
2008-05-06 16:35:25 +00:00
|
|
|
|
|
|
|
CompilationGraph* OwningGraph;
|
2008-05-06 16:36:06 +00:00
|
|
|
iterator EdgeIter;
|
2008-05-06 16:35:25 +00:00
|
|
|
public:
|
|
|
|
typedef Node* pointer;
|
|
|
|
typedef Node& reference;
|
|
|
|
|
|
|
|
NodeChildIterator(Node* N, iterator I) :
|
2008-05-06 16:36:06 +00:00
|
|
|
OwningGraph(N->OwningGraph), EdgeIter(I) {}
|
2008-05-06 16:35:25 +00:00
|
|
|
|
|
|
|
const ThisType& operator=(const ThisType& I) {
|
|
|
|
assert(OwningGraph == I.OwningGraph);
|
2008-05-06 16:36:06 +00:00
|
|
|
EdgeIter = I.EdgeIter;
|
2008-05-06 16:35:25 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator==(const ThisType& I) const
|
2008-05-06 16:36:06 +00:00
|
|
|
{ return EdgeIter == I.EdgeIter; }
|
2008-05-06 16:35:25 +00:00
|
|
|
inline bool operator!=(const ThisType& I) const
|
2008-05-06 16:36:06 +00:00
|
|
|
{ return EdgeIter != I.EdgeIter; }
|
2008-05-06 16:35:25 +00:00
|
|
|
|
|
|
|
inline pointer operator*() const {
|
2008-05-06 16:36:06 +00:00
|
|
|
return &OwningGraph->getNode((*EdgeIter)->ToolName());
|
2008-05-06 16:35:25 +00:00
|
|
|
}
|
|
|
|
inline pointer operator->() const {
|
2008-05-06 16:36:06 +00:00
|
|
|
return &OwningGraph->getNode((*EdgeIter)->ToolName());
|
2008-05-06 16:35:25 +00:00
|
|
|
}
|
|
|
|
|
2008-05-06 16:36:06 +00:00
|
|
|
ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement
|
2008-05-06 16:35:25 +00:00
|
|
|
ThisType operator++(int) { // Postincrement
|
|
|
|
ThisType tmp = *this;
|
|
|
|
++*this;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2008-05-06 16:36:06 +00:00
|
|
|
inline ThisType& operator--() { --EdgeIter; return *this; } // Predecrement
|
2008-05-06 16:35:25 +00:00
|
|
|
inline ThisType operator--(int) { // Postdecrement
|
|
|
|
ThisType tmp = *this;
|
|
|
|
--*this;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
template <>
|
2008-05-06 18:08:59 +00:00
|
|
|
struct GraphTraits<llvmc::CompilationGraph*> {
|
|
|
|
typedef llvmc::CompilationGraph GraphType;
|
|
|
|
typedef llvmc::Node NodeType;
|
|
|
|
typedef llvmc::NodeChildIterator ChildIteratorType;
|
2008-05-06 16:35:25 +00:00
|
|
|
|
|
|
|
static NodeType* getEntryNode(GraphType* G) {
|
|
|
|
return &G->getNode("root");
|
|
|
|
}
|
|
|
|
|
|
|
|
static ChildIteratorType child_begin(NodeType* N) {
|
2008-05-06 16:36:06 +00:00
|
|
|
return ChildIteratorType(N, N->OutEdges.begin());
|
2008-05-06 16:35:25 +00:00
|
|
|
}
|
|
|
|
static ChildIteratorType child_end(NodeType* N) {
|
2008-05-06 16:36:06 +00:00
|
|
|
return ChildIteratorType(N, N->OutEdges.end());
|
2008-05-06 16:35:25 +00:00
|
|
|
}
|
|
|
|
|
2008-05-06 18:08:59 +00:00
|
|
|
typedef llvmc::NodesIterator nodes_iterator;
|
2008-05-06 16:35:25 +00:00
|
|
|
static nodes_iterator nodes_begin(GraphType *G) {
|
2008-05-06 16:36:06 +00:00
|
|
|
return GraphBegin(G);
|
2008-05-06 16:35:25 +00:00
|
|
|
}
|
|
|
|
static nodes_iterator nodes_end(GraphType *G) {
|
2008-05-06 16:36:06 +00:00
|
|
|
return GraphEnd(G);
|
2008-05-06 16:35:25 +00:00
|
|
|
}
|
2008-05-06 16:34:12 +00:00
|
|
|
};
|
2008-05-06 16:35:25 +00:00
|
|
|
|
2008-05-06 16:34:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H
|