forked from facebook/hhvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-gen-cf.h
More file actions
379 lines (315 loc) · 12.1 KB
/
Copy pathcode-gen-cf.h
File metadata and controls
379 lines (315 loc) · 12.1 KB
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
/*
+----------------------------------------------------------------------+
| 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 |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#ifndef incl_HPHP_VM_CODE_GEN_CF_H_
#define incl_HPHP_VM_CODE_GEN_CF_H_
#include "hphp/runtime/vm/jit/smashable-instr.h"
#include "hphp/runtime/vm/jit/string-tag.h"
#include "hphp/runtime/vm/jit/vasm-gen.h"
#include "hphp/runtime/vm/jit/vasm-instr.h"
#include "hphp/runtime/vm/jit/vasm-reg.h"
namespace HPHP { namespace jit {
///////////////////////////////////////////////////////////////////////////////
namespace code_gen_detail {
///////////////////////////////////////////////////////////////////////////////
template <class Then>
void ifThen(Vout& vmain, Vout& vcold, ConditionCode cc, Vreg sf,
Then thenBlock, StringTag tag) {
auto then = vcold.makeBlock();
auto done = vmain.makeBlock();
vmain << jcc{cc, sf, {done, then}, tag};
vcold = then;
thenBlock(vcold);
if (!vcold.closed()) vcold << jmp{done};
vmain = done;
}
template <class Then, class Else>
void ifThenElse(Vout& vmain, Vout& vcold, ConditionCode cc, Vreg sf,
Then thenBlock, Else elseBlock, StringTag tag) {
auto elze = vmain.makeBlock();
auto then = vcold.makeBlock();
auto done = vmain.makeBlock();
vmain << jcc{cc, sf, {elze, then}, tag};
vmain = elze;
elseBlock(vmain);
if (!vmain.closed()) vmain << jmp{done};
vcold = then;
thenBlock(vcold);
if (!vcold.closed()) vcold << jmp{done};
vmain = done;
}
template <class Then, class Else>
Vreg cond(Vout& vmain, Vout& vcold, ConditionCode cc, Vreg sf,
Vreg dst, Then thenBlock, Else elseBlock, StringTag tag) {
auto elze = vmain.makeBlock();
auto then = vcold.makeBlock();
auto done = vmain.makeBlock();
vmain << jcc{cc, sf, {elze, then}, tag};
vmain = elze;
auto r1 = elseBlock(vmain);
vmain << phijmp{done, vmain.makeTuple({r1})};
vcold = then;
auto r2 = thenBlock(vcold);
vcold << phijmp{done, vcold.makeTuple({r2})};
vmain = done;
vmain << phidef{vmain.makeTuple({dst})};
return dst;
}
///////////////////////////////////////////////////////////////////////////////
}
///////////////////////////////////////////////////////////////////////////////
// Conditionals.
//
// Each conditional control-flow helper comes in three flavors:
// - Emit `thenBlock' to `vmain' (along with all other blocks).
// - Emit `thenBlock' to `vcold' (with all other blocks in `vmain').
// - Emit `thenBlock' to `unlikely ? vcold : vmain'.
/*
* Generate an if-then block construct.
*
* Tests `sf' for the branch condition `cc', and jumps to the code generated by
* the `thenBlock' lambda if the condition holds. A jmp past the construct is
* emitted if `thenBlock' is not terminated.
*
* `thenBlock' takes a single argument: the Vout to emit to.
*/
template <class Then>
void ifThen(Vout& vmain, ConditionCode cc, Vreg sf, Then thenBlock,
StringTag tag = StringTag{}) {
code_gen_detail::ifThen(vmain, vmain, cc, sf, thenBlock, tag);
}
template <class Then>
void unlikelyIfThen(Vout& vmain, Vout& vcold, ConditionCode cc, Vreg sf,
Then thenBlock, StringTag tag = StringTag{}) {
code_gen_detail::ifThen(vmain, vcold, cc, sf, thenBlock, tag);
}
template <class Then>
void ifThen(Vout& vmain, Vout& vcold, ConditionCode cc, Vreg sf,
Then thenBlock, bool unlikely, StringTag tag = StringTag{}) {
code_gen_detail::ifThen(vmain, unlikely ? vcold : vmain,
cc, sf, thenBlock, tag);
}
/*
* Like the above flavors of ifThen(), except with a block label instead of a
* block-emitting lambda.
*/
inline void ifThen(Vout& v, ConditionCode cc, Vreg sf, Vlabel then,
StringTag tag = StringTag{}) {
auto const done = v.makeBlock();
v << jcc{cc, sf, {done, then}, tag};
v = done;
}
/*
* Generate an if-then-else block construct.
*
* Like ifThen(), except that in addition, we jump to the code generated by
* `elseBlock' if the condition does not hold.
*
* `elseBlock' takes the same arguments as `thenBlock', and we likewise close
* it with a jmp to past the construct if it is not terminated.
*/
template <class Then, class Else>
void ifThenElse(Vout& vmain, ConditionCode cc, Vreg sf,
Then thenBlock, Else elseBlock, StringTag tag = StringTag{}) {
code_gen_detail::ifThenElse(vmain, vmain, cc, sf, thenBlock, elseBlock, tag);
}
template <class Then, class Else>
void unlikelyIfThenElse(Vout& vmain, Vout& vcold, ConditionCode cc, Vreg sf,
Then thenBlock, Else elseBlock,
StringTag tag = StringTag{}) {
code_gen_detail::ifThenElse(vmain, vcold, cc, sf, thenBlock, elseBlock, tag);
}
template <class Then, class Else>
void ifThenElse(Vout& vmain, Vout& vcold, ConditionCode cc, Vreg sf,
Then thenBlock, Else elseBlock, bool unlikely,
StringTag tag = StringTag{}) {
code_gen_detail::ifThenElse(vmain, unlikely ? vcold : vmain,
cc, sf, thenBlock, elseBlock, tag);
}
/*
* Generate an if-then-else block construct with a single dst.
*
* Like ifThenElse(), except that the blocks are expected to return a dst Vreg.
* The dsts are phi'd into `dst'.
*
* Returns `dst' unaltered, for convenience.
*/
template <class Then, class Else>
Vreg cond(Vout& vmain, ConditionCode cc, Vreg sf,
Vreg dst, Then thenBlock, Else elseBlock,
StringTag tag = StringTag{}) {
return code_gen_detail::cond(vmain, vmain, cc, sf, dst,
thenBlock, elseBlock, tag);
}
template <class Then, class Else>
Vreg unlikelyCond(Vout& vmain, Vout& vcold, ConditionCode cc, Vreg sf,
Vreg dst, Then thenBlock, Else elseBlock,
StringTag tag = StringTag{}) {
return code_gen_detail::cond(vmain, vcold, cc, sf, dst,
thenBlock, elseBlock, tag);
}
template <class Then, class Else>
Vreg cond(Vout& vmain, Vout& vcold, ConditionCode cc, Vreg sf,
Vreg dst, Then thenBlock, Else elseBlock, bool unlikely,
StringTag tag = StringTag{}) {
return code_gen_detail::cond(vmain, unlikely ? vcold : vmain,
cc, sf, dst, thenBlock, elseBlock, tag);
}
///////////////////////////////////////////////////////////////////////////////
/*
* Emit an if-then-else where each side of the branch may collapse to a no-op
* once it has executed once.
*
* Once both sides have executed, we collapse the whole structure into a no-op.
* Note that we don't induce any atomicity, so each side of the branch executes
* "at most once per thread" rather than "exactly once".
*
* Lambda signatures:
* void do_branch(Vout& v, Vlabel next, Vlabel taken);
* void do_then(Vout& v);
* void do_else(Vout& v);
*/
template<typename Branch, typename Then, typename Else>
void implodingIFTE(Vout& v, Vout& vc, Branch do_branch,
Then do_then, Else do_else) {
// Emit a smashable jump forward, getting a handle to the jump instruction's
// eventual address as well as its eventual target. Once we've visited both
// paths, we'll smash over this jump to completely skip this sequence.
auto const branch = v.makeBlock();
auto const end = v.makeBlock();
auto const jmp_watch = v.makeAddr();
auto const end_watch = v.makeAddr();
v << jmps{{branch, end}, jmp_watch, end_watch};
v = branch;
auto const mov_watch = v.makeAddr();
auto const bitsq = v.makeReg();
auto const bits = v.makeReg();
auto const mov_addr = v.makeReg();
// Load a smashable byte of data out of the instruction stream, and save the
// address of the mov instruction.
v << movqs{0x0, bitsq, mov_watch};
v << movtqb{bitsq, bits};
v << leav{mov_watch, mov_addr};
auto const next = v.makeBlock();
auto const taken = v.makeBlock();
do_branch(v, next, taken);
auto const done = v.makeBlock();
auto const gen_side = [&] (Vout& v, Vout& vc, auto&& do_side,
Vlabel side, uint8_t bit_imm) {
v = side;
auto const skip = v.makeBlock();
auto const after = v.makeBlock();
auto const slow = vc.makeBlock();
// Test the "side taken" bit in the inline data. If it's set, skip all of
// this work.
auto const sf = v.makeReg();
v << testbi{bit_imm, bits, sf};
v << jcc{CC_Z, sf, {skip, slow}};
v = skip;
v << phijmp{after, v.makeTuple({bits})};
vc = slow;
auto const updated = vc.makeReg();
auto const sf2 = vc.makeReg();
// If it's not set, smash the immediate so that the bit is set, then
// actually perform the work of this side of the branch.
vc << orbi{bit_imm, bits, updated, sf2};
vc << vcall{
CallSpec::direct(smashMovq),
vc.makeVcallArgs({{mov_addr, updated}}),
vc.makeTuple({})
};
do_side(vc);
vc << phijmp{after, vc.makeTuple({updated})};
// Phi some stuff.
v = after;
auto const def = v.makeReg();
v << phidef{v.makeTuple({def})};
v << phijmp{done, v.makeTuple({def})};
};
gen_side(v, vc, do_else, next, 0b01);
gen_side(v, vc, do_then, taken, 0b10);
v = done;
auto const bits_out = v.makeReg();
v << phidef{v.makeTuple({bits_out})};
auto const implode = v.makeBlock();
// Check to see if both bits are set.
auto const sf = v.makeReg();
v << cmpbi{0b11, bits_out, sf};
v << jcc{CC_Z, sf, {end, implode}};
v = implode;
auto const jmp_addr = v.makeReg();
auto const end_addr = v.makeReg();
// If so, smash the jmp so that it points to the return address of this call
// to smash the jmp.
v << leav{jmp_watch, jmp_addr};
v << leav{end_watch, end_addr};
v << vcall{
CallSpec::direct(smashJmp),
vc.makeVcallArgs({{jmp_addr, end_addr}}),
vc.makeTuple({})
};
v << jmp{end};
v = end;
}
///////////////////////////////////////////////////////////////////////////////
/*
* Generate a do-while loop.
*
* The `regs' list is the list of initial loop registers, which will be phi'd
* appropriately for the loop.
*
* `loopBlock' is the lambda responsible for generating the code. It takes
* both the input phidef and output phijmp loop registers as arguments, and
* should return a single SF Vreg to be tested against `cc'.
*
* `count' is an optional hint specifying how many times the loop is
* likely to execute. This will be used to adjust the block weights
* appropriately.
*/
template <class Loop>
VregList doWhile(Vout& v, ConditionCode cc,
const VregList& regs, Loop loopBlock,
int64_t count = 10) {
auto loop = v.makeBlock();
auto const done = v.makeBlock();
auto loopSplit = v.makeBlock();
auto const doneSplit = v.makeBlock();
if (count >= 0) {
loop.addWeightScale(count);
loopSplit.addWeightScale(count > 0 ? (count - 1) : 0);
}
auto const freshRegs = [&] {
auto copy = regs;
for (auto& reg : copy) reg = v.makeReg();
return copy;
};
auto in = freshRegs(), out = freshRegs();
v << phijmp{loop, v.makeTuple(regs)};
v = loop;
v << phidef{v.makeTuple(in)};
auto const sf = loopBlock(in, out);
v << jcc{cc, sf, {doneSplit, loopSplit}};
v = loopSplit;
v << phijmp{loop, v.makeTuple(out)};
v = doneSplit;
v << phijmp{done, v.makeTuple(out)};
v = done;
auto fout = freshRegs();
v << phidef{v.makeTuple(fout)};
return fout;
}
///////////////////////////////////////////////////////////////////////////////
}}
#endif