Otawa  0.10
BasicBlock.h
Go to the documentation of this file.
1 /*
2  * $Id$
3  * BasicBlock class interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2003-08, 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 
23 #ifndef OTAWA_CFG_BASIC_BLOCK_H
24 #define OTAWA_CFG_BASIC_BLOCK_H
25 
26 #include <otawa/prop/Identifier.h>
27 #include <elm/genstruct/SLList.h>
28 #include <elm/inhstruct/DLList.h>
29 #include <elm/utility.h>
30 #include <elm/PreIterator.h>
31 #include <otawa/program.h>
32 #include <otawa/instruction.h>
33 
34 namespace otawa {
35 
36 // Extern class
37 class WorkSpace;
38 class Edge;
39 class CFG;
40 extern Identifier<int> INDEX;
41 
42 // BasicBlock class
43 class BasicBlock: public PropList {
44  friend class CFGBuilder;
45  friend class CFGInfo;
46  friend class CFG;
47  friend class VirtualCFG;
48  friend class Edge;
49 protected:
50  static const unsigned long
51  FLAG_Call = 0x01,
52  FLAG_Unknown = 0x02,
53  FLAG_Return = 0x04,
54  FLAG_Entry = 0x08,
55  FLAG_Exit = 0x10,
56  FLAG_Virtual = 0x20,
57  FLAG_Cond = 0x40;
58 
59  // EdgeIterator class
60  class EdgeIterator: public elm::genstruct::SLList<Edge *>::Iterator {
61  public:
63  : elm::genstruct::SLList<Edge *>::Iterator(edges) { };
64  };
65 
66 public:
67  class Bundle;
68  class BundleIter;
69 
70  class InstIter: public PreIterator<InstIter, Inst *> {
71  public:
72  inline InstIter(void): inst(0) { }
73  inline InstIter(const BasicBlock *bb)
74  { ASSERT(bb); if(bb->isEnd()) inst = 0; else { inst = bb->firstInst(); top = bb->topAddress(); } }
75  //inline InstIter(const InstIter& iter): inst(iter.inst) { }
76  inline bool ended(void) const { return !inst; }
77  inline Inst *item(void) const { return inst; }
78  inline void next(void) { if(inst->topAddress() >= top) inst = 0; else inst = inst->nextInst(); }
79  private:
80  friend class Bundle;
81  inline InstIter(Inst *i, Address t): inst(i), top(t) { }
84  };
86 
87  class Bundle {
88  public:
89  inline Address address(void) const { return fi->address(); }
90  inline Address topAddress(void) const { return li->topAddress(); }
91  inline t::uint32 size(void) const { return topAddress() - address(); }
92  inline InstIter insts(void) const { return InstIter(fi, li->topAddress()); }
93  void semInsts(sem::Block& block);
94  void readRegSet(RegSet& set);
95  void writeRegSet(RegSet& set);
96 
97  private:
98  friend class BundleIter;
99  inline Bundle(void): fi(0), li(0) { }
100  inline void move(Inst *inst, Address top)
101  { fi = inst; li = fi; while(!li->isBundleEnd() && li->topAddress() < top) li = li->nextInst(); }
102  inline void end(void) { fi = li = 0; }
103  Inst *fi, *li;
104  };
105 
106  class BundleIter: public PreIterator<BundleIter, Bundle> {
107  public:
108  inline BundleIter(void) { }
110  { ASSERT(bb); if(!bb->isEnd()) { top = bb->topAddress(); b.move(bb->firstInst(), top); } }
111  inline bool ended(void) const { return !b.li; }
112  inline const Bundle& item(void) const { return b; }
113  inline void next(void) { if(b.li->topAddress() >= top) b.end(); else b.move(b.li->nextInst(), top); }
114  private:
117  };
118 
119 protected:
121  virtual ~BasicBlock(void);
122  void setTaken(BasicBlock *bb);
123  void setNotTaken(BasicBlock *bb);
124 
125 public:
127 
128  // Constructors
129  BasicBlock(void);
130 
131  // Generic accessors
132  inline bool isCall(void) const { return (flags & FLAG_Call) != 0; };
133  inline bool isReturn(void) const { return (flags & FLAG_Return) != 0; };
134  inline bool isTargetUnknown(void) const
135  { return (flags & FLAG_Unknown) != 0; };
136  inline bool isEntry(void) const { return flags & FLAG_Entry; };
137  inline bool isExit(void) const { return flags & FLAG_Exit; };
138  inline bool isEnd(void) const { return flags & (FLAG_Entry | FLAG_Exit); };
139  inline bool isConditional(void) const { return flags & FLAG_Cond; }
140  inline Address address(void) const { return first->address(); };
141  inline Address topAddress(void) const { return address() + size(); }
142  virtual int countInsts(void) const;
143  inline t::uint32 size(void) const { return _size; }
144  inline bool isVirtual(void) const { return flags & FLAG_Virtual; };
145  inline unsigned long getFlags(void) const { return flags; };
146  inline int number(void) const { return INDEX(this); };
147  inline CFG *cfg(void) { return _cfg; }
148  inline Inst *firstInst(void) const { return first; }
149  Inst *lastInst(void) const;
150  Inst *controlInst(void) const;
151  void print(io::Output& out) const;
152 
153  // Edge management
154  inline void addInEdge(Edge *edge) { ins.addFirst(edge); };
155  void addOutEdge(Edge *edge) { outs.addFirst(edge); };
156  void removeInEdge(Edge *edge) { ins.remove(edge); };
157  void removeOutEdge(Edge *edge) { outs.remove(edge); };
158 
159  // Edge iterators
160  class InIterator: public EdgeIterator {
161  public:
163  : EdgeIterator(bb->ins) { ASSERT(bb); };
164  };
165  class OutIterator: public EdgeIterator {
166  public:
168  : EdgeIterator(bb->outs) { ASSERT(bb); };
169  };
170 
171  // data
174  unsigned long flags;
177 
178  // Deprecated
179  BasicBlock *getTaken(void);
180  BasicBlock *getNotTaken(void);
181  inline t::uint32 getBlockSize(void) const { return size(); };
182  inline int countInstructions(void) const { return countInsts(); }
183 };
184 inline Output& operator<<(Output& out, BasicBlock *bb) { bb->print(out); return out; }
185 
186 
187 // BasicBlock class
188 class CodeBasicBlock: public BasicBlock {
189  friend class CFGInfo;
190 public:
192  inline void set(Inst *_first, t::size size) { first = _first; _size = size; }
193  inline void setSize(t::size size) { _size = size; }
194  inline void setFirst(Inst *_first) { first = _first; }
195  inline void setReturn(void) { flags |= FLAG_Return; flags &= ~ FLAG_Call; }
196  inline void setCall(void) { flags |= FLAG_Call; flags &= ~ FLAG_Return; }
197  inline void setCond(void) { flags |= FLAG_Cond; }
198  inline void setUnknown(void) { flags |= FLAG_Unknown; }
199 };
200 
201 
202 // EndBasicBlock class
203 class EndBasicBlock: public BasicBlock {
204 public:
205  inline EndBasicBlock(unsigned long _flags = 0) {
206  flags = _flags;
207  first = null_bb.firstInst();
208  };
209 };
210 
211 } // otawa
212 
213 #endif // OTAWA_CFG_BASIC_BLOCK_H
bool isEnd(void) const
Test if the basic block is the CFG end, entry or exit.
Definition: BasicBlock.h:138
struct otawa::sem::inst inst
void setFirst(Inst *_first)
Definition: BasicBlock.h:194
BasicBlock(void)
Build an empty basic block.
Definition: BasicBlock.cpp:57
void readRegSet(RegSet &set)
Get the set of registers read by the bundle.
Definition: BasicBlock.cpp:436
Iterator on bundles composing a basic block.
Definition: BasicBlock.h:106
dtd::Element edge(dtd::make("edge", _EDGE).attr(source).attr(target).attr(called))
CodeBasicBlock(Inst *head, t::size size=0)
Build a basic block from its container code and its sequence of instructions.
Definition: BasicBlock.cpp:339
Address topAddress(void) const
Get the top address of the bundle.
Definition: BasicBlock.h:90
CFG * _cfg
Definition: BasicBlock.h:176
Bundle b
Definition: BasicBlock.h:115
Definition: BasicBlock.h:60
static BasicBlock & null_bb
Definition: BasicBlock.h:120
InstIter(void)
Definition: BasicBlock.h:72
BundleIter(BasicBlock *bb)
Definition: BasicBlock.h:109
Inst * fi
Definition: BasicBlock.h:103
void setNotTaken(BasicBlock *bb)
Set the following basic block.
Definition: BasicBlock.cpp:104
bool isTargetUnknown(void) const
Test if the target of the branch instruction of this basic block is known or not. ...
Definition: BasicBlock.h:134
void set(Inst *_first, t::size size)
Definition: BasicBlock.h:192
BasicBlock * getTaken(void)
Get the target basic block if the last branch instruction is taken.
Definition: BasicBlock.cpp:66
void next(void)
Definition: BasicBlock.h:78
void addInEdge(Edge *edge)
Add an input edge to the basic block input list.
Definition: BasicBlock.h:154
Inst * nextInst(void) const
Definition: Inst.h:94
virtual int countInsts(void) const
Count the number of instructions in the basic block.
Definition: BasicBlock.cpp:173
Address address(void) const
Get the base address of the bundle.
Definition: BasicBlock.h:89
void setTaken(BasicBlock *bb)
Set the target basic block of the branch last instruction.
Definition: BasicBlock.cpp:92
InIterator(BasicBlock *bb)
Definition: BasicBlock.h:162
unsigned long getFlags(void) const
Definition: BasicBlock.h:145
t::uint32 size(void) const
Get the size of the bundle.
Definition: BasicBlock.h:91
virtual ~BasicBlock(void)
Definition: BasicBlock.cpp:183
void next(void)
Definition: BasicBlock.h:113
void writeRegSet(RegSet &set)
Get the set of registers written by the bundle.
Definition: BasicBlock.cpp:446
Identifier< int > INDEX
Identifier used for storing in each basic block from the CFG its index.
Definition: CFG.h:39
dtd::Element bb(dtd::make("bb", _BB).attr(id).attr(address).attr(size))
int countInstructions(void) const
Definition: BasicBlock.h:182
A block represents a sequence of semantic instructions inst.
Definition: inst.h:183
bool isReturn(void) const
Test if the basic block is ended by a sub-program return.
Definition: BasicBlock.h:133
Represent a basic block in the strictess meaning: a sequence of instructions containing no branch...
Definition: BasicBlock.h:188
elm::io::Output & operator<<(elm::io::Output &out, Address addr)
Definition: base.cpp:188
OutIterator(BasicBlock *bb)
Definition: BasicBlock.h:167
Control Flow Graph representation.
Definition: CFG.h:42
Address topAddress(void) const
Definition: BasicBlock.h:141
void setUnknown(void)
Definition: BasicBlock.h:198
uint32 size
bool ended(void) const
Definition: BasicBlock.h:111
InstIter InstIterator
Definition: BasicBlock.h:85
bool isEntry(void) const
Test if the basic block is the CFG entry.
Definition: BasicBlock.h:136
void setReturn(void)
Definition: BasicBlock.h:195
static const unsigned long FLAG_Unknown
Definition: BasicBlock.h:52
Inst * lastInst(void) const
Get the last instruction of a basic block.
Definition: BasicBlock.cpp:357
void semInsts(sem::Block &block)
Get the semantic instruction to perform semantic analysis on the bundle.
Definition: BasicBlock.cpp:422
A bundle, in a VLIW processors, is a group of instructions executed in parallel.
Definition: BasicBlock.h:87
void print(io::Output &out) const
Print the reference for a basic block.
Definition: BasicBlock.cpp:278
BasicBlock * getNotTaken(void)
Get the following basic block if the last branch instruction is not taken.
Definition: BasicBlock.cpp:79
Definition: BasicBlock.h:203
elm::genstruct::SLList< Edge * > outs
Definition: BasicBlock.h:175
Inst * firstInst(void) const
Definition: BasicBlock.h:148
bool isExit(void) const
Test if the basic block is the CFG exit.
Definition: BasicBlock.h:137
bool isBundleEnd(void)
On VLIW architecture, mark an instruction that is the last instruction of a bundle.
Definition: Inst.h:124
static Identifier< BasicBlock * > ID
Identifier of the basic block pseudo-instruction.
Definition: BasicBlock.h:126
bool isConditional(void) const
Test if the BB is ended by a conditional branch.
Definition: BasicBlock.h:139
void removeOutEdge(Edge *edge)
Remove an output edge from the basic block output list.
Definition: BasicBlock.h:157
address_t topAddress(void) const
Compute the address of the item immediately following the current item.
Definition: ProgItem.h:30
void setCall(void)
Definition: BasicBlock.h:196
void setSize(t::size size)
Definition: BasicBlock.h:193
t::uint32 getBlockSize(void) const
Compute the size of the basic block.
Definition: BasicBlock.h:181
InstIter insts(void) const
Get an iterator on instructions composing the bundle.
Definition: BasicBlock.h:92
The representation of an address in OTAWA.
Definition: base.h:54
static const unsigned long FLAG_Cond
Definition: BasicBlock.h:57
Inst * controlInst(void) const
Find the last control instruction of the basic block.
Definition: BasicBlock.cpp:372
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
virtual address_t address(void) const =0
Get the address of the item .
sys::SystemOutStream & out
otawa::Inst * inst
Definition: BasicBlock.h:82
static const unsigned long FLAG_Virtual
Definition: BasicBlock.h:56
elm::genstruct::SLList< Edge * > ins
Definition: BasicBlock.h:175
void move(Inst *inst, Address top)
Move the bundle to a next bundle in program image.
Definition: BasicBlock.h:100
bool isVirtual(void) const
Definition: BasicBlock.h:144
A virtual CFG is a CFG not-mapped to real code, that is, it may contains virtual nodes for inlining f...
Definition: VirtualCFG.h:29
Definition: BasicBlock.h:160
InstIter(Inst *i, Address t)
Definition: BasicBlock.h:81
This processor is used for building the CFG from the binary program representation.
Definition: CFGBuilder.h:34
Inst * first
Definition: BasicBlock.h:172
static const unsigned long FLAG_Entry
Definition: BasicBlock.h:54
static const unsigned long FLAG_Return
Definition: BasicBlock.h:53
This class represents edges in the CFG representation.
Definition: Edge.h:33
This class represents identifier with a typed associated value.
Definition: Identifier.h:51
This is the minimal definition of a basic block.
Definition: BasicBlock.h:43
bool ended(void) const
Definition: BasicBlock.h:76
Inst * li
Definition: BasicBlock.h:103
void end(void)
Mark the bundle as empty.
Definition: BasicBlock.h:102
This class represents assembly instruction of a piece of code.
Definition: Inst.h:62
EdgeIterator(elm::genstruct::SLList< Edge * > &edges)
Definition: BasicBlock.h:62
Bundle(void)
Simple bundle constructor.
Definition: BasicBlock.h:99
EndBasicBlock(unsigned long _flags=0)
Definition: BasicBlock.h:205
bool isCall(void) const
Test if the basic block is ended by a call to a sub-program.
Definition: BasicBlock.h:132
unsigned long flags
Definition: BasicBlock.h:174
Inst * item(void) const
Definition: BasicBlock.h:77
void setCond(void)
Definition: BasicBlock.h:197
This allows storing all CFG available in a workspace.
Definition: CFGInfo.h:29
This a list of properties.
Definition: PropList.h:63
Address top
Definition: BasicBlock.h:83
InstIter(const BasicBlock *bb)
Definition: BasicBlock.h:73
BundleIter(void)
Definition: BasicBlock.h:108
static const unsigned long FLAG_Call
Definition: BasicBlock.h:51
CFG * cfg(void)
Definition: BasicBlock.h:147
void removeInEdge(Edge *edge)
Remove an input edge from the basic block input list.
Definition: BasicBlock.h:156
t::size _size
Definition: BasicBlock.h:173
t::uint32 size(void) const
Get the size of the basic block.
Definition: BasicBlock.h:143
Address address(void) const
Definition: BasicBlock.h:140
uint32_t uint32
Property * head
Definition: PropList.h:64
static const unsigned long FLAG_Exit
Definition: BasicBlock.h:55
void addOutEdge(Edge *edge)
Add an output edge to the basic block output list.
Definition: BasicBlock.h:155
Iterator for instructions in the basic block.
Definition: BasicBlock.h:70
Address top
Definition: BasicBlock.h:116
const Bundle & item(void) const
Definition: BasicBlock.h:112