Add new file Support/DataFlow.h.
[oota-llvm.git] / include / llvm / Support / DataFlow.h
1 //===-- llvm/Support/DataFlow.h - dataflow as graphs ------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines specializations of GraphTraits that allows Use-Def and 
11 // Def-Use relations to be treated as proper graphs for generic algorithms.
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SUPPORT_DATAFLOW_H
15 #define LLVM_SUPPORT_DATAFLOW_H
16
17 #include "llvm/User.h"
18 #include "llvm/Value.h"
19 #include "llvm/ADT/GraphTraits.h"
20
21 namespace llvm {
22
23 //===----------------------------------------------------------------------===//
24 // Provide specializations of GraphTraits to be able to treat def-use/use-def 
25 // chains as graphs
26
27 template <> struct GraphTraits<const User*> {
28   typedef const Value NodeType;
29   typedef Value::use_const_iterator ChildIteratorType;
30
31   static NodeType *getEntryNode(const User *G) {
32     return G;
33   }
34
35   static inline ChildIteratorType child_begin(NodeType *N) {
36     return N->use_begin();
37   }
38
39   static inline ChildIteratorType child_end(NodeType *N) {
40     return N->use_end();
41   }
42 };
43
44 template <> struct GraphTraits<User*> {
45   typedef Value NodeType;
46   typedef Value::use_iterator ChildIteratorType;
47
48   static NodeType *getEntryNode(User *G) {
49     return G;
50   }
51
52   static inline ChildIteratorType child_begin(NodeType *N) {
53     return N->use_begin();
54   }
55
56   static inline ChildIteratorType child_end(NodeType *N) {
57     return N->use_end();
58   }
59 };
60
61 template <> struct GraphTraits<Inverse<const User*> > {
62   typedef const Value NodeType;
63   typedef User::const_op_iterator ChildIteratorType;
64
65   static NodeType *getEntryNode(Inverse<const User*> G) {
66     return G.Graph;
67   }
68
69   static inline ChildIteratorType child_begin(NodeType *N) {
70     if (const User *U = dyn_cast<User>(N))
71       return U->op_begin();
72     return NULL;
73   }
74
75   static inline ChildIteratorType child_end(NodeType *N) {
76     if(const User *U = dyn_cast<User>(N))
77       return U->op_end();
78     return NULL;
79   }
80 };
81
82 template <> struct GraphTraits<Inverse<User*> > {
83   typedef Value NodeType;
84   typedef User::op_iterator ChildIteratorType;
85
86   static NodeType *getEntryNode(Inverse<User*> G) {
87     return G.Graph;
88   }
89
90   static inline ChildIteratorType child_begin(NodeType *N) {
91     if (User *U = dyn_cast<User>(N))
92       return U->op_begin();
93     return NULL;
94   }
95
96   static inline ChildIteratorType child_end(NodeType *N) {
97     if (User *U = dyn_cast<User>(N))
98       return U->op_end();
99     return NULL;
100   }
101 };
102
103 }
104 #endif