forked from facebook/hhvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.h
333 lines (275 loc) · 10.7 KB
/
block.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-2014 Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#ifndef incl_HPHP_VM_BLOCK_H_
#define incl_HPHP_VM_BLOCK_H_
#include <algorithm>
#include "hphp/runtime/vm/jit/containers.h"
#include "hphp/runtime/vm/jit/edge.h"
#include "hphp/runtime/vm/jit/ir-instruction.h"
#include "hphp/runtime/vm/jit/ir-opcode.h"
namespace HPHP { namespace jit {
/*
* A Block refers to a basic block: single-entry, single-exit, list of
* instructions. The instruction list is an intrusive list, so each
* instruction can only be in one block at a time. Likewise, a block
* can only be owned by one trace at a time.
*
* Block owns the InstructionList, but exposes several list methods itself
* so usually you can use Block directly. These methods also update
* IRInstruction::m_block transparently.
*/
struct Block : boost::noncopyable {
typedef InstructionList::iterator iterator;
typedef InstructionList::const_iterator const_iterator;
typedef InstructionList::reference reference;
typedef InstructionList::const_reference const_reference;
/*
* Execution frequency hint; codegen will put Unlikely blocks in acold,
* and Unused blocks in afrozen.
*
* 'Main' code, or code that executes most frequently, should have either
* the 'Likely' or 'Neither' Block::Hint. Code for these blocks is
* emitted into the 'a' section.
*
* Code that handles infrequent cases should have the 'Unlikely'
* Block::Hint. Example of such code are decref helpers that free objects
* when the ref-count goes to zero. Code for these blocks is emitted into
* the 'acold' section.
*
* Code that is either executed once, or is highly unlikely to be ever
* executed, or code that will become dead in the future should have
* the 'Unlikely' Hint. Examples of these include Service Request stubs
* (executed once), Catch blocks (highly unlikely), and cold code
* emitted in profiling mode (which become dead after optimized code is
* emitted). Code for these blocks is emitted into the 'afrozen' section.
*
* See also util/code-cache.h for comment on the 'ahot' and 'aprof' sections.
*/
enum class Hint { Neither, Likely, Unlikely, Unused };
explicit Block(unsigned id)
: m_id(id)
, m_hint(Hint::Neither)
{}
unsigned id() const { return m_id; }
Hint hint() const { return m_hint; }
void setHint(Hint hint) { m_hint = hint; }
// Returns true if this block has no successors.
bool isExit() const { return !taken() && !next(); }
// Returns whether this block is the initial entry block for the tracelet.
bool isEntry() const { return id() == 0; }
// Returns whether this block starts with BeginCatch
bool isCatch() const;
// If its a catch block, the BeginCatch's marker
BCMarker catchMarker() const;
// return the fallthrough block. Should be nullptr if the last instruction
// is a Terminal.
Block* next() const { return back().next(); }
Edge* nextEdge() { return back().nextEdge(); }
// return the target block if the last instruction is a branch.
Block* taken() const { return back().taken(); }
Edge* takenEdge() { return back().takenEdge(); }
// returns the number of successors.
size_t numSuccs() const { return (bool)taken() + (bool)next(); }
// return the postorder number of this block. (updated each time
// sortBlocks() is called.
// Insert inst after this block's optional DefLabel and BeginCatch,
// then return an iterator to the newly inserted instruction.
iterator prepend(IRInstruction* inst);
// return iterator to first instruction after the DefLabel (if
// present) and BeginCatch (if present).
iterator skipHeader();
const_iterator skipHeader() const;
// return iterator to last instruction
iterator backIter();
// return an iterator to a specific instruction
iterator iteratorTo(IRInstruction* inst);
// Accessors of list of predecessor edges. Each edge has a inst() property
// which is the instruction in the predecessor block.
EdgeList& preds() { return m_preds; }
const EdgeList& preds() const { return m_preds; }
size_t numPreds() const { return m_preds.size(); }
// Remove edge from its destination's predecessor list and insert it in
// new_to's predecessor list.
static Block* updatePreds(Edge* edge, Block* new_to);
// visit each src that provides a value to label->dsts[i]. body
// should take an IRInstruction* and an SSATmp*.
template<typename L> void forEachSrc(unsigned i, L body) const;
// return the first src providing a value to label->dsts[i] for
// which body(src) returns true, or nullptr if none are found.
template<typename L> SSATmp* findSrc(unsigned i, L body);
// execute body(P) for each predecessor block P of this block
template <typename L> void forEachPred(L body);
// list-compatible interface; these delegate to m_instrs but also update
// inst.m_block
InstructionList& instrs() { return m_instrs; }
bool empty() const { return m_instrs.empty(); }
iterator begin() { return m_instrs.begin(); }
iterator end() { return m_instrs.end(); }
const_iterator begin() const { return m_instrs.begin(); }
const_iterator end() const { return m_instrs.end(); }
iterator erase(iterator pos);
iterator erase(IRInstruction* inst);
iterator insert(iterator pos, IRInstruction* inst);
void splice(iterator pos, Block* from, iterator begin, iterator end);
void push_back(std::initializer_list<IRInstruction*> insts);
void push_back(IRInstruction* inst);
template <class Predicate> void remove_if(Predicate p);
InstructionList&& moveInstrs();
// return the first instruction in the block.
reference front();
const_reference front() const;
// return the last instruction in the block
reference back();
const_reference back() const;
friend const Edge* nextEdge(Block*); // only for validation
std::string toString() const;
private:
InstructionList m_instrs; // instructions in this block
const unsigned m_id; // unit-assigned unique id of this block
EdgeList m_preds; // Edges that point to this block
Hint m_hint; // execution frequency hint
};
using BlockList = jit::vector<Block*>;
using BlockSet = jit::flat_set<Block*>;
inline Block::reference Block::front() {
assert(!m_instrs.empty());
return m_instrs.front();
}
inline Block::const_reference Block::front() const {
return const_cast<Block*>(this)->front();
}
inline Block::reference Block::back() {
assert(!m_instrs.empty());
return m_instrs.back();
}
inline Block::const_reference Block::back() const {
return const_cast<Block*>(this)->back();
}
inline Block::iterator Block::erase(iterator pos) {
pos->setBlock(nullptr);
return m_instrs.erase(pos);
}
inline Block::iterator Block::erase(IRInstruction* inst) {
assert(inst->block() == this);
return erase(iteratorTo(inst));
}
inline Block::iterator Block::prepend(IRInstruction* inst) {
assert(inst->marker().valid());
auto it = skipHeader();
return insert(it, inst);
}
inline Block::iterator Block::skipHeader() {
auto it = begin();
auto e = end();
if (it != e && it->op() == DefLabel) ++it;
if (it != e && it->op() == BeginCatch) ++it;
return it;
}
inline Block::const_iterator Block::skipHeader() const {
return const_cast<Block*>(this)->skipHeader();
}
inline Block::iterator Block::backIter() {
assert(!empty());
auto it = end();
return --it;
}
inline Block::iterator Block::iteratorTo(IRInstruction* inst) {
assert(inst->block() == this);
return m_instrs.iterator_to(*inst);
}
inline Block* Block::updatePreds(Edge* edge, Block* new_to) {
if (Block* old_to = edge->to()) {
auto &preds = old_to->m_preds;
preds.erase(preds.iterator_to(*edge));
}
if (new_to) {
new_to->m_preds.push_front(*edge);
}
return new_to;
}
template<typename L> inline
void Block::forEachSrc(unsigned i, L body) const {
for (auto const& e : m_preds) {
auto jmp = e.inst();
assert(jmp->op() == Jmp && jmp->taken() == this);
body(jmp, jmp->src(i));
}
}
template<typename L> inline
SSATmp* Block::findSrc(unsigned i, L body) {
for (Edge& e : m_preds) {
SSATmp* src = e.inst()->src(i);
if (body(src)) return src;
}
return nullptr;
}
template <typename L> inline
void Block::forEachPred(L body) {
for (auto i = m_preds.begin(), e = m_preds.end(); i != e;) {
auto inst = i->inst();
++i;
body(inst->block());
}
}
inline Block::iterator Block::insert(iterator pos, IRInstruction* inst) {
assert(inst->marker().valid());
inst->setBlock(this);
return m_instrs.insert(pos, *inst);
}
inline
void Block::splice(iterator pos, Block* from, iterator begin, iterator end) {
assert(from != this);
for (auto i = begin; i != end; ++i) i->setBlock(this);
m_instrs.splice(pos, from->instrs(), begin, end);
}
inline void Block::push_back(std::initializer_list<IRInstruction*> insts) {
for (auto inst : insts) { push_back(inst); }
}
inline void Block::push_back(IRInstruction* inst) {
assert(inst->marker().valid());
inst->setBlock(this);
return m_instrs.push_back(*inst);
}
template <class Predicate>
inline void Block::remove_if(Predicate p) {
m_instrs.remove_if(p);
}
inline InstructionList&& Block::moveInstrs() {
for (auto i = begin(); i != end(); ++i) i->setBlock(nullptr);
return std::move(m_instrs);
}
inline bool Block::isCatch() const {
// Catch blocks always start with DefLabel; BeginCatch.
if (empty()) return false;
auto it = skipHeader();
if (it == begin()) return false;
return (--it)->op() == BeginCatch;
}
inline BCMarker Block::catchMarker() const {
assert(isCatch());
auto it = skipHeader();
assert(it != begin());
return (--it)->marker();
}
// defined here to avoid circular dependencies
inline void Edge::setTo(Block* to) {
m_to = Block::updatePreds(this, to);
}
inline Block* Edge::from() const {
return inst() != nullptr ? inst()->block() : nullptr;
}
}}
#endif