forked from facebook/hhvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathir-instruction.h
440 lines (363 loc) · 11.9 KB
/
ir-instruction.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-present 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_IR_INSTRUCTION_H_
#define incl_HPHP_IR_INSTRUCTION_H_
#include "hphp/runtime/vm/jit/bc-marker.h"
#include "hphp/runtime/vm/jit/extra-data.h"
#include "hphp/runtime/vm/jit/ir-opcode.h"
#include "hphp/runtime/vm/jit/ssa-tmp.h"
#include "hphp/runtime/vm/jit/string-tag.h"
#include "hphp/runtime/vm/jit/type.h"
#include <boost/intrusive/list.hpp>
#include <folly/Range.h>
#include <cstdint>
#include <limits>
#include <string>
namespace HPHP { namespace jit {
///////////////////////////////////////////////////////////////////////////////
struct Block;
struct Edge;
struct IRUnit;
struct SSATmp;
///////////////////////////////////////////////////////////////////////////////
/*
* Bytecode-relative position context for an IRInstruction.
*
* These are threaded around to construct IRInstructions.
*/
struct BCContext {
BCMarker marker;
uint16_t iroff;
};
///////////////////////////////////////////////////////////////////////////////
/*
* An HHIR instruction.
*
* IRInstructions must be arena-allocatable. (Destructors are not called when
* they come from IRUnit.)
*/
struct IRInstruction {
enum Id { kTransient = 0xffffffff };
/////////////////////////////////////////////////////////////////////////////
/*
* Create an IRInstruction for the opcode `op'.
*
* IRInstruction creation is usually done through IRUnit or IRBuilder rather
* than directly via the constructor.
*/
explicit IRInstruction(Opcode op,
BCContext bcctx,
Edge* edges = nullptr,
uint32_t numSrcs = 0,
SSATmp** srcs = nullptr);
/*
* Construct an IRInstruction as a deep copy of `inst', using `arena' to
* allocate memory for its srcs/dsts.
*/
explicit IRInstruction(Arena& arena, const IRInstruction* inst, Id id);
/*
* Stringify the instruction.
*/
std::string toString() const;
IRInstruction(const IRInstruction&) = delete;
IRInstruction& operator=(const IRInstruction&) = delete;
/////////////////////////////////////////////////////////////////////////////
// Replacement.
/*
* Turn this instruction into the target instruction, without changing
* stable fields (id, current block, list fields).
*
* The existing destination SSATmp(s) will continue to think they came from
* this instruction, and the instruction's marker will not change.
*
* The target instruction may be transient---we'll clone anything we need to
* keep, using IRUnit for any needed memory.
*
* Pre: other->isTransient() || numDsts() == other->numDsts()
*/
void become(IRUnit&, const IRInstruction* other);
/*
* Replace the instruction in-place with a Nop.
*
* This is less general than become(), but it is fairly common, and doesn't
* require access to an IRUnit.
*/
void convertToNop();
/////////////////////////////////////////////////////////////////////////////
// OpcodeFlag helpers. [const]
/*
* Helper accessors for the OpcodeFlag bits for this instruction.
*/
bool hasDst() const;
bool naryDst() const;
bool consumesReferences() const;
bool mayRaiseError() const;
bool isTerminal() const;
bool hasEdges() const;
bool isPassthrough() const;
/*
* Whether the src numbered srcNo consumes a reference, or the dest produces
* a reference.
*/
bool consumesReference(int srcNo) const;
bool producesReference() const;
/*
* Get the src that is passed through.
*
* Currently, this is always src(0).
*/
SSATmp* getPassthroughValue() const;
/////////////////////////////////////////////////////////////////////////////
// Opcode, marker, and type.
/*
* The ID assigned by the owning IRUnit.
*
* The instruction ID is stable and useful as an array index. If the
* Instruction is not arena-allocated by an IRUnit, the `m_id' field will be
* kTransient (in which case this function should not be called).
*/
uint32_t id() const;
/*
* Return true if the instruction is in a transient state.
*
* "Transient" means that the instruction is allocated on the stack and we
* haven't yet committed to including it in the IRUnit's CFG.
*/
bool isTransient() const;
/*
* The index of this instruction relative to its BCMarker.
*/
uint16_t iroff() const;
/*
* Whether the instruction has one among a variadic list of opcodes.
*/
template<typename... Args>
bool is(Opcode op, Args&&... args) const;
bool is() const;
/*
* Get or set the opcode of the instruction.
*/
Opcode op() const;
void setOpcode(Opcode);
/*
* Get or set the BCMarker of the instruction.
*/
const BCMarker& marker() const;
BCMarker& marker();
/*
* Get the BCContext of the instruction.
*
* This is only used for threading through to IRInstruction's constructor.
*/
BCContext bcctx() const;
/*
* Return the current Func, or nullptr if not known (it should only
* be unknown in test code).
*/
const Func* func() const;
/*
* Return the current Func's cls(), or nullptr if not known.
*/
const Class* ctx() const;
/*
* Check for, get, or set the instruction's optional type parameter.
*/
bool hasTypeParam() const;
Type typeParam() const;
void setTypeParam(Type);
/////////////////////////////////////////////////////////////////////////////
// Srcs and dsts.
/*
* Initialize the source list for this IRInstruction. We must not have
* already had our sources initialized before this function is called.
*
* Memory for `srcs' is owned outside of this class and must outlive it.
*/
void initializeSrcs(uint32_t numSrcs, SSATmp** srcs);
/*
* Number of srcs/dsts.
*/
uint32_t numSrcs() const;
uint32_t numDsts() const;
/*
* Return the ith src of this instruction.
*/
SSATmp* src(uint32_t i) const;
/*
* Return the singular dst for this instruction.
*
* @requires: !naryDst()
*/
SSATmp* dst() const;
/*
* Returns the ith dst of this instruction.
*
* If the instruction has no dsts, dst(0) will return nullptr, and if the
* instruction is not NaryDst, dst(0) will return the single dst. Otherwise,
* it returns the first variadic dst.
*/
SSATmp* dst(uint32_t i) const;
/*
* Get the srcs/dsts as a folly::Range.
*/
folly::Range<SSATmp**> srcs() const;
folly::Range<SSATmp**> dsts();
/*
* Set a single src or all srcs.
*/
void setSrc(uint32_t i, SSATmp* newSrc);
void setSrcs(uint32_t numSrcs, SSATmp** newSrcs);
void deleteSrc(uint32_t i);
/*
* Set the dsts, either as a single dst, or as `numDsts' dsts (if the
* instruction has NaryDst).
*/
void setDst(SSATmp* newDst);
void setDsts(uint32_t numDsts, SSATmp** newDsts);
void deleteDst(uint32_t i);
/////////////////////////////////////////////////////////////////////////////
// ExtraData.
/*
* Whether or not this instruction has an ExtraData.
*/
bool hasExtra() const;
/*
* Return access to the ExtraData on this instruction, for the specified
* opcode type.
*
* Pre: op() == opc
*/
template<Opcode opc> const typename IRExtraDataType<opc>::type* extra() const;
template<Opcode opc> typename IRExtraDataType<opc>::type* extra();
/*
* Return access to ExtraData of type T. Requires that
* IRExtraDataType<opc>::type is T for this instruction's opcode.
*
* It's normally preferable to use the version of this function that takes
* the opcode instead of this one. This is for writing code that is supposed
* to be able to handle multiple opcode types that share the same kind of
* extra data.
*/
template<class T> const T* extra() const;
/*
* Return the raw ExtraData pointer, for pretty-printing.
*/
const IRExtraData* rawExtra() const;
/*
* Set the ExtraData for this instruction.
*
* The lifetime of the ExtraData must outlast this IRInstruction (and any of
* its clones).
*/
void setExtra(IRExtraData*);
/*
* Clear the extra data pointer in a IRInstruction.
*
* Used during IRUnit::gen to avoid having dangling IRExtraData*'s into stack
* memory.
*/
void clearExtra();
/////////////////////////////////////////////////////////////////////////////
// Blocks and edges.
/*
* Get/set the block containing this instruction.
*
* The block is also where the taken edge is coming from, if this instruction
* has a taken edge.
*/
Block* block() const;
void setBlock(Block* b);
/*
* Optional fall-through edge.
*
* If present, this instruction must also have a taken edge.
*/
Block* next() const;
Edge* nextEdge();
void setNext(Block* b);
/*
* Optional control flow edge.
*
* If present, this instruction must be the last one in the block.
*/
Block* taken() const;
Edge* takenEdge();
void setTaken(Block* b);
/*
* Whether this instruction is a control flow instruction, or ends a block.
*/
bool isControlFlow() const;
bool isBlockEnd() const;
bool isRawLoad() const;
/*
* Clear any outgoing edges this instruction has, if any.
*/
void clearEdges();
private:
/*
* Block/edge implementations.
*/
Block* succ(int i) const;
Edge* succEdge(int i);
void setSucc(int i, Block* b);
/////////////////////////////////////////////////////////////////////////////
// Data members.
private:
Type m_typeParam; // garbage unless m_hasTypeParam is true
Opcode m_op;
uint16_t m_iroff{std::numeric_limits<uint16_t>::max()};
uint16_t m_numSrcs;
uint16_t m_numDsts : 15;
bool m_hasTypeParam : 1;
BCMarker m_marker;
const Id m_id{kTransient};
// 4-byte hole
SSATmp** m_srcs{nullptr};
union {
SSATmp* m_dest; // if HasDest
SSATmp** m_dsts; // if NaryDest
};
Block* m_block{nullptr}; // what block owns this instruction
Edge* m_edges{nullptr}; // outgoing edges, if this is a block-end
IRExtraData* m_extra{nullptr};
public:
boost::intrusive::list_member_hook<> m_listNode; // for InstructionList
};
///////////////////////////////////////////////////////////////////////////////
using IRInstructionHookOption = boost::intrusive::member_hook<
IRInstruction,
boost::intrusive::list_member_hook<>,
&IRInstruction::m_listNode
>;
using InstructionList = boost::intrusive::list<IRInstruction,
IRInstructionHookOption>;
///////////////////////////////////////////////////////////////////////////////
/*
* Return the output type from a given IRInstruction.
*
* The destination type is always predictable from the types of the inputs, any
* type parameters to the instruction, and the id of the dst.
*/
Type outputType(const IRInstruction*, int dstId = 0);
///////////////////////////////////////////////////////////////////////////////
/*
* Return a type appropriate for $this given a function.
*/
Type thisTypeFromFunc(const Func* func);
///////////////////////////////////////////////////////////////////////////////
}}
#include "hphp/runtime/vm/jit/ir-instruction-inl.h"
#endif