00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00027
#ifndef SHCTRLGRAPH_HPP
00028
#define SHCTRLGRAPH_HPP
00029
00030
#include <vector>
00031
#include <list>
00032
#include <iosfwd>
00033
#include "ShDllExport.hpp"
00034
#include "ShRefCount.hpp"
00035
#include "ShBasicBlock.hpp"
00036
#include "ShVariable.hpp"
00037
#include "ShBlock.hpp"
00038
00039
namespace SH {
00040
00041
class ShCtrlGraphNode;
00042
00043
struct
00044
SH_DLLEXPORT ShCtrlGraphBranch {
00045 ShCtrlGraphBranch(
const ShPointer<ShCtrlGraphNode>& node,
00046 ShVariable cond);
00047
00048 ShPointer<ShCtrlGraphNode> node;
00049 ShVariable
cond;
00050 };
00051
00062 class
00063
SH_DLLEXPORT ShCtrlGraphNode :
public ShRefCountable {
00064
public:
00065 ShCtrlGraphNode();
00066 ~ShCtrlGraphNode();
00067
00068
ShBasicBlockPtr block;
00069
typedef std::vector<ShCtrlGraphBranch> SuccessorList;
00070 SuccessorList successors;
00071 ShPointer<ShCtrlGraphNode> follower;
00072
00073
typedef std::list<ShCtrlGraphNode*> ShPredList;
00074 ShPredList predecessors;
00075
00078 std::ostream& print(std::ostream& out,
int indent)
const;
00079
00082 std::ostream& graphvizDump(std::ostream& out)
const;
00083
00085
void append(
const ShPointer<ShCtrlGraphNode>& node);
00086
00088
void append(
const ShPointer<ShCtrlGraphNode>& node,
00089
ShVariable cond);
00090
00093
bool marked() const;
00094
00095
00096
00097
00098
void mark() const;
00099
void clearMarked() const;
00100
00101
00102 template<typename F>
00103
void dfs(F& functor);
00104
00105 template<typename F>
00106
void dfs(F& functor) const;
00107
00108 private:
00109 template<typename F>
00110
void real_dfs(F& functor);
00111
00112 template<typename F>
00113
void real_dfs(F& functor) const;
00114
00115 mutable
bool m_marked;
00116 };
00117
00118 typedef
ShPointer<ShCtrlGraphNode>
ShCtrlGraphNodePtr;
00119 typedef
ShPointer<const ShCtrlGraphNode>
ShCtrlGraphNodeCPtr;
00120
00124 class
00125 SH_DLLEXPORT
ShCtrlGraph : public
ShRefCountable {
00126
public:
00127
ShCtrlGraph(ShCtrlGraphNodePtr head, ShCtrlGraphNodePtr tail);
00128
ShCtrlGraph(
ShBlockListPtr blocks);
00129 ~
ShCtrlGraph();
00130
00131 std::ostream& print(std::ostream& out,
int indent)
const;
00132
00133 std::ostream& graphvizDump(std::ostream& out)
const;
00134
00135 ShCtrlGraphNodePtr entry()
const;
00136 ShCtrlGraphNodePtr exit()
const;
00137
00139
00140 ShCtrlGraphNodePtr prependEntry();
00141
00143 ShCtrlGraphNodePtr appendExit();
00144
00145
template<
typename F>
00146
void dfs(F& functor);
00147
00148
template<
typename F>
00149
void dfs(F& functor)
const;
00150
00152
void computePredecessors();
00153
00154
00155
void copy(ShCtrlGraphNodePtr& head, ShCtrlGraphNodePtr& tail)
const;
00156
00157
private:
00158 ShCtrlGraphNodePtr m_entry;
00159 ShCtrlGraphNodePtr m_exit;
00160
00161
00162 };
00163
00164
typedef ShPointer<ShCtrlGraph> ShCtrlGraphPtr;
00165
typedef ShPointer<const ShCtrlGraph> ShCtrlGraphCPtr;
00166
00167
template<
typename F>
00168
void ShCtrlGraphNode::real_dfs(F& functor)
00169 {
00170
if (
this == 0)
return;
00171
if (m_marked)
return;
00172 mark();
00173 functor(
this);
00174
for (std::vector<ShCtrlGraphBranch>::iterator I = successors.begin(); I != successors.end(); ++I) {
00175 I->node->real_dfs(functor);
00176 }
00177
if (follower) follower->real_dfs(functor);
00178 }
00179
00180
template<
typename F>
00181
void ShCtrlGraphNode::real_dfs(F& functor)
const
00182
{
00183
if (
this == 0)
return;
00184
if (m_marked)
return;
00185 mark();
00186 functor(
this);
00187
for (std::vector<ShCtrlGraphBranch>::const_iterator I = successors.begin();
00188 I != successors.end(); ++I) {
00189 I->node->real_dfs(functor);
00190 }
00191
if (follower) follower->real_dfs(functor);
00192 }
00193
00194
template<
typename F>
00195
void ShCtrlGraphNode::dfs(F& functor)
00196 {
00197 clearMarked();
00198 real_dfs(functor);
00199 clearMarked();
00200 }
00201
00202
template<
typename F>
00203
void ShCtrlGraphNode::dfs(F& functor)
const
00204
{
00205 clearMarked();
00206 real_dfs(functor);
00207 clearMarked();
00208 }
00209
00210
template<
typename F>
00211
void ShCtrlGraph::dfs(F& functor)
00212 {
00213 m_entry->dfs(functor);
00214 }
00215
00216
template<
typename F>
00217
void ShCtrlGraph::dfs(F& functor)
const
00218
{
00219 m_entry->dfs(functor);
00220 }
00221
00222 }
00223
00224
#endif