Otawa  0.10
CFGAdapter.h
Go to the documentation of this file.
1 /*
2  * $Id$
3  * ForwardCFGAdapter and BackwardCFGAdapter classes interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2010, IRIT UPS.
7  *
8  * OTAWA is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * OTAWA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with OTAWA; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #ifndef OTAWA_CFG_CFGADAPTER_H_
23 #define OTAWA_CFG_CFGADAPTER_H_
24 
25 #include <otawa/cfg.h>
26 #include <otawa/cfg/features.h>
27 
28 namespace otawa {
29 
31 public:
32 
33  typedef BasicBlock *Vertex;
34  typedef otawa::Edge *Edge;
35 
36  inline ForwardCFGAdapter(CFG *_cfg): cfg(_cfg) { }
37  inline int count(void) const { return cfg->countBB(); }
38  inline Vertex entry(void) const { return cfg->entry(); }
39  inline int index(Vertex v) const { return v->number(); }
40  inline Vertex sourceOf(Edge edge) const { return edge->source(); }
41  inline Vertex sinkOf(Edge edge) const { return edge->target(); }
42  inline int outDegree(Vertex vertex) const { int cnt = 0; for(Successor s(*this, vertex); s; s++) cnt++; return cnt; }
43  inline bool isSuccessorOf(Vertex succ, const Vertex& ref) { for(Successor s(*this, ref); s; s++) if(sinkOf(s) == succ) return true; return false; }
44  inline int inDegree(Vertex vertex) const { int cnt = 0; for(Predecessor s(*this, vertex); s; s++) cnt++; return cnt; }
45  inline bool isPredecessorOf(Vertex pred, const Vertex& ref) { for(Predecessor s(*this, ref); s; s++) if(sourceOf(s) == pred) return true; return false; }
46  inline bool isLoopHeader(Vertex v) const { return LOOP_HEADER(v); }
47 
48  class Predecessor: public PreIterator<Predecessor, Edge> {
49  public:
50  inline Predecessor(const ForwardCFGAdapter& g, const Vertex& v): iter(v) { }
51  inline bool ended (void) const { return iter.ended(); }
52  const Edge item (void) const { return iter.item(); }
53  void next(void) { iter++; }
54  private:
56  };
57 
58  class Successor: public PreIterator<Successor, Edge> {
59  public:
60  inline Successor(const ForwardCFGAdapter& g, const Vertex& v): iter(v) { step(); }
61  inline bool ended (void) const { return iter.ended(); }
62  const Edge item (void) const { return iter.item(); }
63  void next(void) { iter++; step(); }
64  private:
65  inline void step(void) {
66  while(iter && iter->kind() == otawa::Edge::CALL)
67  iter++;
68  }
70  };
71 
72  class Iterator: public CFG::BBIterator {
73  public:
74  inline Iterator(const ForwardCFGAdapter& cfga): CFG::BBIterator(cfga.cfg) { }
75  inline Iterator(const Iterator& iter): CFG::BBIterator(iter) { }
76  };
77 
78 private:
79  CFG *cfg;
80 };
81 
82 
84 public:
85 
86  typedef BasicBlock *Vertex;
87  typedef otawa::Edge *Edge;
88 
89  inline BackwardCFGAdapter(CFG *_cfg): cfg(_cfg) { }
90  inline int count(void) const { return cfg->countBB(); }
91  inline Vertex entry(void) const { return cfg->exit(); }
92  inline int index(Vertex v) const { return v->number(); }
93  inline Vertex sourceOf(Edge edge) const { return edge->target(); }
94  inline Vertex sinkOf(Edge edge) const { return edge->source(); }
95  inline int outDegree(Vertex vertex) const { int cnt = 0; for(Successor s(*this, vertex); s; s++) cnt++; return cnt; }
96  inline bool isSuccessorOf(Vertex succ, const Vertex& ref) { for(Successor s(*this, ref); s; s++) if(sinkOf(s) == succ) return true; return false; }
97  inline int inDegree(Vertex vertex) const { int cnt = 0; for(Predecessor s(*this, vertex); s; s++) cnt++; return cnt; }
98  inline bool isPredecessorOf(const Vertex& pred, const Vertex& ref) { for(Predecessor s(*this, ref); s; s++) if(sourceOf(s) == pred) return true; return false; }
99  inline bool isLoopHeader(Vertex v) const { return LOOP_HEADER(v); }
100 
101  class Successor: public PreIterator<Successor, Edge> {
102  public:
103  inline Successor(const BackwardCFGAdapter& g, const Vertex& v): iter(v) { }
104  inline bool ended (void) const { return iter.ended(); }
105  const Edge item (void) const { return iter.item(); }
106  void next(void) { iter++; }
107  private:
109  };
110 
111  class Predecessor: public PreIterator<Predecessor, Edge> {
112  public:
113  inline Predecessor(const BackwardCFGAdapter& g, const Vertex& v): iter(v) { step(); }
114  inline bool ended (void) const { return iter.ended(); }
115  const Edge item (void) const { return iter.item(); }
116  void next(void) { iter++; step(); }
117  private:
118  inline void step(void) {
119  while(iter && iter->kind() == otawa::Edge::CALL)
120  iter++;
121  }
123  };
124 
125  class Iterator: public CFG::BBIterator {
126  public:
127  inline Iterator(const BackwardCFGAdapter& cfga): CFG::BBIterator(cfga.cfg) { }
128  inline Iterator(const Iterator& iter): CFG::BBIterator(iter) { }
129  };
130 
131 private:
133 };
134 
135 } //otawa
136 
137 
138 #endif /* OTAWA_CFG_CFGADAPTER_H_ */
Definition: CFG.h:48
void step(void)
Definition: CFGAdapter.h:65
Iterator(const BackwardCFGAdapter &cfga)
Definition: CFGAdapter.h:127
const Edge item(void) const
Definition: CFGAdapter.h:115
Iterator(const Iterator &iter)
Definition: CFGAdapter.h:128
dtd::Element edge(dtd::make("edge", _EDGE).attr(source).attr(target).attr(called))
bool isSuccessorOf(Vertex succ, const Vertex &ref)
Definition: CFGAdapter.h:43
Definition: CFGAdapter.h:48
BasicBlock::OutIterator iter
Definition: CFGAdapter.h:69
int count(void) const
Definition: CFGAdapter.h:90
const Edge item(void) const
Definition: CFGAdapter.h:52
BasicBlock * source(void) const
Definition: Edge.h:52
Definition: CFGAdapter.h:72
Predecessor(const BackwardCFGAdapter &g, const Vertex &v)
Definition: CFGAdapter.h:113
bool ended(void) const
Definition: CFGAdapter.h:104
Kind of an edge matching a sub-program call.
Definition: Edge.h:40
otawa::Edge * Edge
Definition: CFGAdapter.h:87
bool isSuccessorOf(Vertex succ, const Vertex &ref)
Definition: CFGAdapter.h:96
int outDegree(Vertex vertex) const
Definition: CFGAdapter.h:42
Iterator(const Iterator &iter)
Definition: CFGAdapter.h:75
int count(void) const
Definition: CFGAdapter.h:37
Adapter of CFG to the concept of otawa::concept::DiGraphWithIndexedVertex.
Definition: CFGAdapter.h:30
Identifier< bool > LOOP_HEADER
Identifier for marking basic blocks that are entries of loops.
bool isLoopHeader(Vertex v) const
Definition: CFGAdapter.h:99
BasicBlock * entry(void)
Get the entry basic block of the CFG.
Definition: CFG.h:63
Successor(const BackwardCFGAdapter &g, const Vertex &v)
Definition: CFGAdapter.h:103
CFG * cfg
Definition: CFGAdapter.h:79
ForwardCFGAdapter(CFG *_cfg)
Definition: CFGAdapter.h:36
BasicBlock::OutIterator iter
Definition: CFGAdapter.h:122
void step(void)
Definition: CFGAdapter.h:118
Control Flow Graph representation.
Definition: CFG.h:42
Vertex entry(void) const
Definition: CFGAdapter.h:91
void next(void)
Definition: CFGAdapter.h:63
bool isLoopHeader(Vertex v) const
Definition: CFGAdapter.h:46
Vertex sourceOf(Edge edge) const
Definition: CFGAdapter.h:40
Predecessor(const ForwardCFGAdapter &g, const Vertex &v)
Definition: CFGAdapter.h:50
BasicBlock * exit(void)
Get the exit basic block of the CFG.
Definition: CFG.h:65
int outDegree(Vertex vertex) const
Definition: CFGAdapter.h:95
BasicBlock::InIterator iter
Definition: CFGAdapter.h:55
const Edge item(void) const
Definition: CFGAdapter.h:105
int index(Vertex v) const
Definition: CFGAdapter.h:39
Adapter of CFG to the concept of otawa::concept::DiGraphWithIndexedVertex but with backward traversal...
Definition: CFGAdapter.h:83
BasicBlock * Vertex
Definition: CFGAdapter.h:33
const Edge item(void) const
Definition: CFGAdapter.h:62
void next(void)
Definition: CFGAdapter.h:106
bool ended(void) const
Definition: CFGAdapter.h:51
int index(Vertex v) const
Definition: CFGAdapter.h:92
int inDegree(Vertex vertex) const
Definition: CFGAdapter.h:44
Vertex sinkOf(Edge edge) const
Definition: CFGAdapter.h:41
Definition: BasicBlock.h:165
int number(void) const
Get the number hooked on this basic block, that is, value of ID_Index property.
Definition: BasicBlock.h:146
otawa::Edge * Edge
Definition: CFGAdapter.h:34
bool ended(void) const
Definition: CFGAdapter.h:61
bool ended(void) const
Definition: CFGAdapter.h:114
Definition: BasicBlock.h:160
Successor(const ForwardCFGAdapter &g, const Vertex &v)
Definition: CFGAdapter.h:60
void next(void)
Definition: CFGAdapter.h:53
This class represents edges in the CFG representation.
Definition: Edge.h:33
BasicBlock * target(void) const
Definition: Edge.h:53
Definition: CFGAdapter.h:58
This is the minimal definition of a basic block.
Definition: BasicBlock.h:43
BackwardCFGAdapter(CFG *_cfg)
Definition: CFGAdapter.h:89
bool isPredecessorOf(const Vertex &pred, const Vertex &ref)
Definition: CFGAdapter.h:98
Definition: CFGAdapter.h:125
Definition: CFGAdapter.h:101
Iterator(const ForwardCFGAdapter &cfga)
Definition: CFGAdapter.h:74
BBIterator(CFG *cfg)
Definition: CFG.h:50
Vertex entry(void) const
Definition: CFGAdapter.h:38
int inDegree(Vertex vertex) const
Definition: CFGAdapter.h:97
void next(void)
Definition: CFGAdapter.h:116
Vertex sourceOf(Edge edge) const
Definition: CFGAdapter.h:93
Definition: CFGAdapter.h:111
BasicBlock * Vertex
Definition: CFGAdapter.h:86
int countBB(void)
Definition: CFG.h:105
CFG * cfg
Definition: CFGAdapter.h:132
Vertex sinkOf(Edge edge) const
Definition: CFGAdapter.h:94
BasicBlock::InIterator iter
Definition: CFGAdapter.h:108
bool isPredecessorOf(Vertex pred, const Vertex &ref)
Definition: CFGAdapter.h:45