/
inline.c
1022 lines (945 loc) · 44.5 KB
/
inline.c
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "moar.h"
/* Ensures that a given compilation unit has access to the specified extop. */
static void demand_extop(MVMThreadContext *tc, MVMCompUnit *target_cu, MVMCompUnit *source_cu,
const MVMOpInfo *info) {
MVMExtOpRecord *extops;
MVMuint16 i, num_extops;
uv_mutex_lock(target_cu->body.inline_tweak_mutex);
/* See if the target compunit already has the extop. */
extops = target_cu->body.extops;
num_extops = target_cu->body.num_extops;
for (i = 0; i < num_extops; i++)
if (extops[i].info == info) {
uv_mutex_unlock(target_cu->body.inline_tweak_mutex);
return;
}
/* If not, need to add it. Locate it in the source CU. */
extops = source_cu->body.extops;
num_extops = source_cu->body.num_extops;
for (i = 0; i < num_extops; i++) {
if (extops[i].info == info) {
MVMuint32 orig_size = target_cu->body.num_extops * sizeof(MVMExtOpRecord);
MVMuint32 new_size = (target_cu->body.num_extops + 1) * sizeof(MVMExtOpRecord);
MVMExtOpRecord *new_extops = MVM_fixed_size_alloc(tc,
tc->instance->fsa, new_size);
memcpy(new_extops, target_cu->body.extops, orig_size);
memcpy(&new_extops[target_cu->body.num_extops], &extops[i], sizeof(MVMExtOpRecord));
if (target_cu->body.extops)
MVM_fixed_size_free_at_safepoint(tc, tc->instance->fsa, orig_size,
target_cu->body.extops);
target_cu->body.extops = new_extops;
target_cu->body.num_extops++;
uv_mutex_unlock(target_cu->body.inline_tweak_mutex);
return;
}
}
/* Didn't find it; should be impossible. */
uv_mutex_unlock(target_cu->body.inline_tweak_mutex);
MVM_oops(tc, "Spesh: inline failed to find source CU extop entry");
}
/* Sees if it will be possible to inline the target code ref, given we could
* already identify a spesh candidate. Returns NULL if no inlining is possible
* or a graph ready to be merged if it will be possible. */
MVMSpeshGraph * MVM_spesh_inline_try_get_graph(MVMThreadContext *tc, MVMSpeshGraph *inliner,
MVMStaticFrame *target_sf,
MVMSpeshCandidate *cand,
MVMSpeshIns *invoke_ins,
char **no_inline_reason) {
MVMSpeshGraph *ig;
MVMSpeshBB *bb;
MVMint32 same_hll;
/* Check inlining is enabled. */
if (!tc->instance->spesh_inline_enabled) {
*no_inline_reason = "inlining is disabled";
return NULL;
}
/* Check frame is not marked as not being allowed to be inlined. */
if (target_sf->body.no_inline) {
*no_inline_reason = "the frame is marked as no-inline";
return NULL;
}
/* Check bytecode size is within the inline limit. */
if (cand->bytecode_size > MVM_SPESH_MAX_INLINE_SIZE) {
*no_inline_reason = "bytecode is too large to inline";
return NULL;
}
/* Ensure that this isn't a recursive inlining. */
if (target_sf == inliner->sf) {
*no_inline_reason = "recursive calls cannot be inlined";
return NULL;
}
/* Ensure it has no state vars (these need the setup code in frame
* invoke). */
if (target_sf->body.has_state_vars) {
*no_inline_reason = "cannot inline code that declares a state variable";
return NULL;
}
/* Ensure it's not a thunk (need to skip over those in exception search). */
if (target_sf->body.is_thunk) {
*no_inline_reason = "cannot inline code marked as a thunk";
return NULL;
}
/* If they're from the same HLL, we'll need to watch out for ops that are
* HLL sensitive. */
same_hll = target_sf->body.cu->body.hll_config == inliner->sf->body.cu->body.hll_config;
/* Build graph from the already-specialized bytecode. */
ig = MVM_spesh_graph_create_from_cand(tc, target_sf, cand, 0);
/* Traverse graph, looking for anything that might prevent inlining and
* also building usage counts up. */
bb = ig->entry;
while (bb) {
MVMSpeshIns *ins = bb->first_ins;
while (ins) {
/* Track usages. */
MVMint32 opcode = ins->info->opcode;
MVMint32 is_phi = opcode == MVM_SSA_PHI;
MVMuint8 i;
for (i = 0; i < ins->info->num_operands; i++)
if ((is_phi && i > 0)
|| (!is_phi && (ins->info->operands[i] & MVM_operand_rw_mask) == MVM_operand_read_reg))
ig->facts[ins->operands[i].reg.orig][ins->operands[i].reg.i].usages++;
if (opcode == MVM_OP_inc_i || opcode == MVM_OP_inc_u ||
opcode == MVM_OP_dec_i || opcode == MVM_OP_dec_u)
ig->facts[ins->operands[0].reg.orig][ins->operands[0].reg.i - 1].usages++;
/* Instruction may be marked directly as not being inlinable, in
* which case we're done. */
if (!is_phi && ins->info->no_inline) {
*no_inline_reason = "target has a :noinline instruction";
goto not_inlinable;
}
/* If we don't have the same HLL and there's a :useshll op, we
* cannot inline. */
if (!same_hll && ins->info->uses_hll) {
*no_inline_reason = "target has a :useshll instruction and HLLs are different";
goto not_inlinable;
}
/* If we have an invoke_o, but a return_[ins] that would require
* boxing, we can't inline if it's not the same HLL. */
if (!same_hll && invoke_ins->info->opcode == MVM_OP_invoke_o) {
switch (ins->info->opcode) {
case MVM_OP_return_i:
case MVM_OP_return_n:
case MVM_OP_return_s:
*no_inline_reason = "target needs a return boxing and HLLs are different";
goto not_inlinable;
}
}
/* If we have lexical bind, make sure it's within the frame. */
if (ins->info->opcode == MVM_OP_bindlex) {
if (ins->operands[0].lex.outers > 0) {
*no_inline_reason = "target has bind to outer lexical";
goto not_inlinable;
}
}
/* Check we don't have too many args for inlining to work out. */
else if (ins->info->opcode == MVM_OP_sp_getarg_o ||
ins->info->opcode == MVM_OP_sp_getarg_i ||
ins->info->opcode == MVM_OP_sp_getarg_n ||
ins->info->opcode == MVM_OP_sp_getarg_s) {
if (ins->operands[1].lit_i16 >= MAX_ARGS_FOR_OPT) {
*no_inline_reason = "too many arguments to inline";
goto not_inlinable;
}
}
/* Ext-ops need special care in inter-comp-unit inlines. */
if (ins->info->opcode == (MVMuint16)-1) {
MVMCompUnit *target_cu = inliner->sf->body.cu;
MVMCompUnit *source_cu = target_sf->body.cu;
if (source_cu != target_cu)
demand_extop(tc, target_cu, source_cu, ins->info);
}
ins = ins->next;
}
bb = bb->linear_next;
}
/* If we found nothing we can't inline, inlining is fine. */
return ig;
/* If we can't find a way to inline, we end up here. */
not_inlinable:
MVM_spesh_graph_destroy(tc, ig);
return NULL;
}
/* Finds the deopt index of the return. */
static MVMint32 return_deopt_idx(MVMThreadContext *tc, MVMSpeshIns *invoke_ins) {
MVMSpeshAnn *ann = invoke_ins->annotations;
while (ann) {
if (ann->type == MVM_SPESH_ANN_DEOPT_ALL_INS)
return ann->data.deopt_idx;
ann = ann->next;
}
MVM_oops(tc, "Spesh inline: return_deopt_idx failed");
}
/* The following routines fix references to per-compilation-unit things
* that would be broken by inlining. */
static void fix_callsite(MVMThreadContext *tc, MVMSpeshGraph *inliner,
MVMSpeshGraph *inlinee, MVMSpeshOperand *to_fix) {
to_fix->callsite_idx = MVM_cu_callsite_add(tc, inliner->sf->body.cu,
inlinee->sf->body.cu->body.callsites[to_fix->callsite_idx]);
}
static void fix_coderef(MVMThreadContext *tc, MVMSpeshGraph *inliner,
MVMSpeshGraph *inlinee, MVMSpeshIns *to_fix) {
MVM_oops(tc, "Spesh inline: fix_coderef NYI");
}
static void fix_const_str(MVMThreadContext *tc, MVMSpeshGraph *inliner,
MVMSpeshGraph *inlinee, MVMSpeshIns *to_fix) {
MVMCompUnit *cu = inlinee->sf->body.cu;
MVMuint16 sslot;
MVMSpeshOperand dest = to_fix->operands[0];
MVMuint16 str_idx = to_fix->operands[1].lit_str_idx;
sslot = MVM_spesh_add_spesh_slot_try_reuse(tc, inlinee, (MVMCollectable *)cu);
/*fprintf(stderr, "fixed up string get to use spesh slot %u\n", sslot);*/
to_fix->info = MVM_op_get_op(MVM_OP_sp_getstringfrom);
to_fix->operands = MVM_spesh_alloc(tc, inliner, 3 * sizeof(MVMSpeshOperand));
to_fix->operands[0] = dest;
to_fix->operands[1].lit_i16 = sslot;
to_fix->operands[2].lit_str_idx = str_idx;
}
static void fix_str(MVMThreadContext *tc, MVMSpeshGraph *inliner,
MVMSpeshGraph *inlinee, MVMSpeshOperand *to_fix) {
to_fix->lit_str_idx = MVM_cu_string_add(tc, inliner->sf->body.cu,
MVM_cu_string(tc, inlinee->sf->body.cu, to_fix->lit_str_idx));
}
static void fix_wval(MVMThreadContext *tc, MVMSpeshGraph *inliner,
MVMSpeshGraph *inlinee, MVMSpeshIns *to_fix) {
/* We have three different ways to make a wval refer to the right
* object after an inline:
* - If a simple change of the dep parameter lets us refer to the
* same SC in the inliner's dependency list, just change that.
* - If the object had already been deserialized, we can just
* stash it away in a spesh slot.
* - If the object hadn't been deserialized yet, an indirect lookup
* via a spesh slot holding the actual SC is possible.
*
* The last option is needed because deserializing objects causes
* allocations, which lead to GC, which is not supported inside
* the spesh process.
*/
MVMCompUnit *targetcu = inliner->sf->body.cu;
MVMCompUnit *sourcecu = inlinee->sf->body.cu;
MVMint16 dep = to_fix->operands[1].lit_i16;
MVMint64 idx = to_fix->info->opcode == MVM_OP_wval
? to_fix->operands[2].lit_i16
: to_fix->operands[2].lit_i64;
if (dep >= 0 && dep < sourcecu->body.num_scs) {
MVMSerializationContext *sc = MVM_sc_get_sc(tc, sourcecu, dep);
if (sc) {
MVMuint16 otherdep;
for (otherdep = 0; otherdep < targetcu->body.num_scs; otherdep++) {
MVMSerializationContext *othersc = MVM_sc_get_sc(tc, targetcu, otherdep);
if (sc == othersc) {
to_fix->operands[1].lit_i16 = otherdep;
return;
}
}
if (MVM_sc_is_object_immediately_available(tc, sc, idx)) {
MVMint16 ss = MVM_spesh_add_spesh_slot_try_reuse(tc, inliner, (MVMCollectable *)MVM_sc_get_object(tc, sc, idx));
to_fix->info = MVM_op_get_op(MVM_OP_sp_getspeshslot);
to_fix->operands[1].lit_i16 = ss;
}
else {
MVMint16 ss = MVM_spesh_add_spesh_slot_try_reuse(tc, inliner, (MVMCollectable *)sc);
to_fix->info = MVM_op_get_op(MVM_OP_sp_getwvalfrom);
to_fix->operands[1].lit_i16 = ss;
to_fix->operands[2].lit_i64 = idx;
}
}
else {
MVM_oops(tc,
"Spesh inline: SC not yet resolved; lookup failed");
}
}
else {
MVM_oops(tc,
"Spesh inline: invalid SC index %d found", dep);
}
}
/* Resizes the handlers table, making a copy if needed. */
static void resize_handlers_table(MVMThreadContext *tc, MVMSpeshGraph *inliner, MVMuint32 new_handler_count) {
if (inliner->handlers == inliner->sf->body.handlers) {
/* Original handlers table; need a copy. */
MVMFrameHandler *new_handlers = MVM_malloc(new_handler_count * sizeof(MVMFrameHandler));
if (inliner->handlers)
memcpy(new_handlers, inliner->handlers,
inliner->num_handlers * sizeof(MVMFrameHandler));
inliner->handlers = new_handlers;
}
else {
/* Probably already did some inlines into this frame; resize. */
inliner->handlers = MVM_realloc(inliner->handlers,
new_handler_count * sizeof(MVMFrameHandler));
}
}
/* Rewrites a lexical lookup to an outer to be done via. a register holding
* the outer coderef. */
static void rewrite_outer_lookup(MVMThreadContext *tc, MVMSpeshGraph *g,
MVMSpeshIns *ins, MVMuint16 num_locals,
MVMuint16 op, MVMSpeshOperand code_ref_reg) {
MVMSpeshOperand *new_operands = MVM_spesh_alloc(tc, g, 4 * sizeof(MVMSpeshOperand));
new_operands[0] = ins->operands[0];
new_operands[0].reg.orig += num_locals;
new_operands[1].lit_ui16 = ins->operands[1].lex.idx;
new_operands[2].lit_ui16 = ins->operands[1].lex.outers;
new_operands[3] = code_ref_reg;
ins->info = MVM_op_get_op(op);
ins->operands = new_operands;
}
/* Merges the inlinee's spesh graph into the inliner. */
MVMSpeshBB * merge_graph(MVMThreadContext *tc, MVMSpeshGraph *inliner,
MVMSpeshGraph *inlinee, MVMStaticFrame *inlinee_sf,
MVMSpeshBB *invoke_bb, MVMSpeshIns *invoke_ins,
MVMSpeshOperand code_ref_reg,
MVMuint32 *inline_boundary_handler) {
MVMSpeshFacts **merged_facts;
MVMuint16 *merged_fact_counts;
MVMint32 i, orig_inlines, total_inlines, orig_deopt_addrs;
MVMuint32 total_handlers = inliner->num_handlers + inlinee->num_handlers + 1;
MVMSpeshBB *inlinee_first_bb = NULL, *inlinee_last_bb = NULL;
/* If the inliner and inlinee are from different compilation units, we
* potentially have to fix up extra things. */
MVMint32 same_comp_unit = inliner->sf->body.cu == inlinee->sf->body.cu;
/* Renumber the locals, lexicals, and basic blocks of the inlinee; also
* re-write any indexes in annotations that need it. */
MVMSpeshBB *bb = inlinee->entry;
while (bb) {
MVMSpeshIns *ins = bb->first_ins;
while (ins) {
MVMuint16 opcode = ins->info->opcode;
MVMSpeshAnn *ann = ins->annotations;
while (ann) {
switch (ann->type) {
case MVM_SPESH_ANN_DEOPT_INLINE:
ann->data.deopt_idx += inliner->num_deopt_addrs;
break;
case MVM_SPESH_ANN_INLINE_START:
case MVM_SPESH_ANN_INLINE_END:
ann->data.inline_idx += inliner->num_inlines;
break;
}
ann = ann->next;
}
if (opcode == MVM_SSA_PHI) {
for (i = 0; i < ins->info->num_operands; i++)
ins->operands[i].reg.orig += inliner->num_locals;
}
else if (opcode == MVM_OP_sp_getlex_o && ins->operands[1].lex.outers > 0) {
rewrite_outer_lookup(tc, inliner, ins, inliner->num_locals,
MVM_OP_sp_getlexvia_o, code_ref_reg);
}
else if (opcode == MVM_OP_sp_getlex_ins && ins->operands[1].lex.outers > 0) {
rewrite_outer_lookup(tc, inliner, ins, inliner->num_locals,
MVM_OP_sp_getlexvia_ins, code_ref_reg);
}
else if (opcode == MVM_OP_getlex && ins->operands[1].lex.outers > 0) {
MVMuint16 outers = ins->operands[1].lex.outers;
MVMStaticFrame *outer = inlinee_sf;
while (outers--)
outer = outer->body.outer;
if (outer->body.lexical_types[ins->operands[1].lex.idx] == MVM_reg_obj)
rewrite_outer_lookup(tc, inliner, ins, inliner->num_locals,
MVM_OP_sp_getlexvia_o, code_ref_reg);
else
rewrite_outer_lookup(tc, inliner, ins, inliner->num_locals,
MVM_OP_sp_getlexvia_ins, code_ref_reg);
}
else {
if (!same_comp_unit) {
if (ins->info->opcode == MVM_OP_const_s) {
fix_const_str(tc, inliner, inlinee, ins);
}
}
for (i = 0; i < ins->info->num_operands; i++) {
MVMuint8 flags = ins->info->operands[i];
switch (flags & MVM_operand_rw_mask) {
case MVM_operand_read_reg:
case MVM_operand_write_reg:
ins->operands[i].reg.orig += inliner->num_locals;
break;
case MVM_operand_read_lex:
case MVM_operand_write_lex:
ins->operands[i].lex.idx += inliner->num_lexicals;
break;
default: {
MVMuint32 type = flags & MVM_operand_type_mask;
if (type == MVM_operand_spesh_slot) {
ins->operands[i].lit_i16 += inliner->num_spesh_slots;
}
else if (type == MVM_operand_coderef) {
if (!same_comp_unit)
/* NYI, but apparently this never happens?! */
fix_coderef(tc, inliner, inlinee, ins);
}
else if (type == MVM_operand_str) {
if (!same_comp_unit)
fix_str(tc, inliner, inlinee, &(ins->operands[i]));
}
else if (type == MVM_operand_callsite) {
if (!same_comp_unit)
fix_callsite(tc, inliner, inlinee, &(ins->operands[i]));
}
break;
}
}
}
}
ins = ins->next;
}
bb->idx += inliner->num_bbs - 1; /* -1 as we won't include entry */
bb->inlined = 1;
if (!bb->linear_next)
inlinee_last_bb = bb;
bb = bb->linear_next;
}
bb = invoke_bb->linear_next;
invoke_bb->linear_next = inlinee_first_bb = inlinee->entry->linear_next;
inlinee_last_bb->linear_next = bb;
bb = NULL;
/* Make all of the inlinee's entry block's successors (except the linear
* next) also be successors of the inliner's entry block; this keeps any
* exception handlers alive in the graph. */
while (inlinee->entry->num_succ > 1) {
MVMSpeshBB *move = inlinee->entry->succ[0] == inlinee->entry->linear_next
? inlinee->entry->succ[1]
: inlinee->entry->succ[0];
MVM_spesh_manipulate_remove_successor(tc, inlinee->entry, move);
MVM_spesh_manipulate_add_successor(tc, inliner, inliner->entry, move);
}
/* Merge facts. */
merged_facts = MVM_spesh_alloc(tc, inliner,
(inliner->num_locals + inlinee->num_locals) * sizeof(MVMSpeshFacts *));
memcpy(merged_facts, inliner->facts,
inliner->num_locals * sizeof(MVMSpeshFacts *));
memcpy(merged_facts + inliner->num_locals, inlinee->facts,
inlinee->num_locals * sizeof(MVMSpeshFacts *));
inliner->facts = merged_facts;
merged_fact_counts = MVM_spesh_alloc(tc, inliner,
(inliner->num_locals + inlinee->num_locals) * sizeof(MVMuint16));
memcpy(merged_fact_counts, inliner->fact_counts,
inliner->num_locals * sizeof(MVMuint16));
memcpy(merged_fact_counts + inliner->num_locals, inlinee->fact_counts,
inlinee->num_locals * sizeof(MVMuint16));
inliner->fact_counts = merged_fact_counts;
/* Copy over spesh slots. */
for (i = 0; i < inlinee->num_spesh_slots; i++)
MVM_spesh_add_spesh_slot(tc, inliner, inlinee->spesh_slots[i]);
/* If they are from separate compilation units, make another pass through
* to fix up on wvals. Note we can't do this in the first pass as we must
* not modify the spesh slots once we've got started with the rewrites.
* Now we've resolved all that, we're good to map wvals elsewhere into
* some extra spesh slots. */
if (!same_comp_unit) {
bb = inlinee->entry;
while (1) {
MVMSpeshIns *ins = bb->first_ins;
while (ins) {
MVMuint16 opcode = ins->info->opcode;
if (opcode == MVM_OP_wval || opcode == MVM_OP_wval_wide)
fix_wval(tc, inliner, inlinee, ins);
ins = ins->next;
}
if (bb == inlinee_last_bb) break;
bb = bb->linear_next;
}
}
/* Merge de-opt tables, if needed. */
orig_deopt_addrs = inliner->num_deopt_addrs;
if (inlinee->num_deopt_addrs) {
assert(inlinee->deopt_addrs != inliner->deopt_addrs);
inliner->alloc_deopt_addrs += inlinee->alloc_deopt_addrs;
if (inliner->deopt_addrs)
inliner->deopt_addrs = MVM_realloc(inliner->deopt_addrs,
inliner->alloc_deopt_addrs * sizeof(MVMint32) * 2);
else
inliner->deopt_addrs = MVM_malloc(inliner->alloc_deopt_addrs * sizeof(MVMint32) * 2);
memcpy(inliner->deopt_addrs + inliner->num_deopt_addrs * 2,
inlinee->deopt_addrs, inlinee->alloc_deopt_addrs * sizeof(MVMint32) * 2);
inliner->num_deopt_addrs += inlinee->num_deopt_addrs;
}
/* Merge inlines table, and add us an entry too. */
orig_inlines = inliner->num_inlines;
total_inlines = inliner->num_inlines + inlinee->num_inlines + 1;
inliner->inlines = inliner->num_inlines
? MVM_realloc(inliner->inlines, total_inlines * sizeof(MVMSpeshInline))
: MVM_malloc(total_inlines * sizeof(MVMSpeshInline));
if (inlinee->num_inlines)
memcpy(inliner->inlines + inliner->num_inlines, inlinee->inlines,
inlinee->num_inlines * sizeof(MVMSpeshInline));
for (i = inliner->num_inlines; i < total_inlines - 1; i++) {
inliner->inlines[i].code_ref_reg += inliner->num_locals;
inliner->inlines[i].locals_start += inliner->num_locals;
inliner->inlines[i].lexicals_start += inliner->num_lexicals;
inliner->inlines[i].return_deopt_idx += orig_deopt_addrs;
}
inliner->inlines[total_inlines - 1].sf = inlinee_sf;
inliner->inlines[total_inlines - 1].code_ref_reg = code_ref_reg.reg.orig;
inliner->inlines[total_inlines - 1].g = inlinee;
inliner->inlines[total_inlines - 1].locals_start = inliner->num_locals;
inliner->inlines[total_inlines - 1].lexicals_start = inliner->num_lexicals;
inliner->inlines[total_inlines - 1].num_locals = inlinee->num_locals;
switch (invoke_ins->info->opcode) {
case MVM_OP_invoke_v:
inliner->inlines[total_inlines - 1].res_type = MVM_RETURN_VOID;
break;
case MVM_OP_invoke_o:
inliner->inlines[total_inlines - 1].res_reg = invoke_ins->operands[0].reg.orig;
inliner->inlines[total_inlines - 1].res_type = MVM_RETURN_OBJ;
break;
case MVM_OP_invoke_i:
inliner->inlines[total_inlines - 1].res_reg = invoke_ins->operands[0].reg.orig;
inliner->inlines[total_inlines - 1].res_type = MVM_RETURN_INT;
break;
case MVM_OP_invoke_n:
inliner->inlines[total_inlines - 1].res_reg = invoke_ins->operands[0].reg.orig;
inliner->inlines[total_inlines - 1].res_type = MVM_RETURN_NUM;
break;
case MVM_OP_invoke_s:
inliner->inlines[total_inlines - 1].res_reg = invoke_ins->operands[0].reg.orig;
inliner->inlines[total_inlines - 1].res_type = MVM_RETURN_STR;
break;
default:
MVM_oops(tc, "Spesh inline: unknown invoke instruction");
}
inliner->inlines[total_inlines - 1].return_deopt_idx = return_deopt_idx(tc, invoke_ins);
inliner->inlines[total_inlines - 1].unreachable = 0;
inliner->inlines[total_inlines - 1].deopt_named_used_bit_field =
inlinee->deopt_named_used_bit_field;
inliner->num_inlines = total_inlines;
/* Create/update per-specialization local and lexical type maps. */
if (!inliner->local_types && inliner->num_locals) {
MVMint32 local_types_size = inliner->num_locals * sizeof(MVMuint16);
inliner->local_types = MVM_malloc(local_types_size);
memcpy(inliner->local_types, inliner->sf->body.local_types, local_types_size);
}
inliner->local_types = MVM_realloc(inliner->local_types,
(inliner->num_locals + inlinee->num_locals) * sizeof(MVMuint16));
if (inlinee->num_locals)
memcpy(inliner->local_types + inliner->num_locals,
inlinee->local_types ? inlinee->local_types : inlinee->sf->body.local_types,
inlinee->num_locals * sizeof(MVMuint16));
if (!inliner->lexical_types && inliner->num_lexicals) {
MVMint32 lexical_types_size = inliner->num_lexicals * sizeof(MVMuint16);
inliner->lexical_types = MVM_malloc(lexical_types_size);
memcpy(inliner->lexical_types, inliner->sf->body.lexical_types, lexical_types_size);
}
inliner->lexical_types = MVM_realloc(inliner->lexical_types,
(inliner->num_lexicals + inlinee->num_lexicals) * sizeof(MVMuint16));
if (inlinee->num_lexicals)
memcpy(inliner->lexical_types + inliner->num_lexicals,
inlinee->lexical_types ? inlinee->lexical_types : inlinee->sf->body.lexical_types,
inlinee->num_lexicals * sizeof(MVMuint16));
/* Merge unreachable handlers array if needed. */
if (inliner->unreachable_handlers || inlinee->unreachable_handlers) {
MVMint8 *new_uh = MVM_spesh_alloc(tc, inliner, total_handlers);
if (inlinee->unreachable_handlers)
memcpy(new_uh, inlinee->unreachable_handlers,
inlinee->num_handlers);
new_uh[inlinee->num_handlers] = 0;
if (inliner->unreachable_handlers)
memcpy(new_uh + inlinee->num_handlers + 1, inliner->unreachable_handlers, inliner->num_handlers);
inliner->unreachable_handlers = new_uh;
}
/* Merge handlers from inlinee. */
resize_handlers_table(tc, inliner, total_handlers);
if (inliner->num_handlers > 0)
memmove(inliner->handlers + inlinee->num_handlers + 1, inliner->handlers,
inliner->num_handlers * sizeof(MVMFrameHandler));
if (inlinee->num_handlers > 0) {
memcpy(inliner->handlers, inlinee->handlers,
inlinee->num_handlers * sizeof(MVMFrameHandler));
for (i = 0; i < inlinee->num_handlers; i++) {
inliner->handlers[i].block_reg += inliner->num_locals;
inliner->handlers[i].label_reg += inliner->num_locals;
if (inliner->handlers[i].inlinee == -1)
inliner->handlers[i].inlinee = total_inlines - 1;
else
inliner->handlers[i].inlinee += orig_inlines;
}
}
/* Adjust indexes in inliner's frame handler annotations */
bb = inliner->entry;
while (bb) {
if (bb == inlinee_first_bb) /* No need to adjust inlinee's annotations */
bb = inlinee_last_bb->linear_next;
if (bb) {
MVMSpeshIns *ins = bb->first_ins;
while (ins) {
MVMSpeshAnn *ann = ins->annotations;
while (ann) {
switch (ann->type) {
case MVM_SPESH_ANN_FH_START:
case MVM_SPESH_ANN_FH_END:
case MVM_SPESH_ANN_FH_GOTO:
ann->data.frame_handler_index += inlinee->num_handlers + 1;
}
ann = ann->next;
}
ins = ins->next;
}
bb = bb->linear_next;
}
}
/* Insert inline boundary entry into the handlers table. */
*inline_boundary_handler = inlinee->num_handlers;
inliner->handlers[*inline_boundary_handler].category_mask = MVM_EX_INLINE_BOUNDARY;
inliner->handlers[*inline_boundary_handler].action = 0;
inliner->handlers[*inline_boundary_handler].inlinee = total_inlines - 1;
/* Update total locals, lexicals, basic blocks, and handlers of the
* inliner. */
inliner->num_bbs += inlinee->num_bbs - 1;
inliner->num_locals += inlinee->num_locals;
inliner->num_lexicals += inlinee->num_lexicals;
inliner->num_handlers += inlinee->num_handlers + 1;
return inlinee_last_bb;
}
/* Tweak the successor of a BB, also updating the target BBs pred. */
static void tweak_succ(MVMThreadContext *tc, MVMSpeshGraph *g, MVMSpeshBB *bb, MVMSpeshBB *new_succ) {
if (bb->num_succ == 0) {
/* It had no successors, so we'll add one. */
bb->succ = MVM_spesh_alloc(tc, g, sizeof(MVMSpeshBB *));
bb->num_succ = 1;
bb->succ[0] = new_succ;
}
else {
/* Otherwise, we can assume that the first successor is the one to
* update; others will be there as a result of control handlers, but
* these are always added last. */
bb->succ[0] = new_succ;
}
if (new_succ->num_pred == 0) {
new_succ->pred = MVM_spesh_alloc(tc, g, sizeof(MVMSpeshBB *));
new_succ->num_pred = 1;
new_succ->pred[0] = bb;
}
else {
MVMint32 found = 0;
MVMint32 i;
for (i = 0; i < new_succ->num_pred; i++)
if (new_succ->pred[i]->idx + 1 == new_succ->idx) {
new_succ->pred[i] = bb;
found = 1;
break;
}
if (!found)
MVM_oops(tc,
"Spesh inline: could not find appropriate pred to update\n");
}
}
/* Finds return instructions and re-writes them into gotos, doing any needed
* boxing or unboxing. */
static void return_to_set(MVMThreadContext *tc, MVMSpeshGraph *g, MVMSpeshIns *return_ins, MVMSpeshOperand target) {
MVMSpeshOperand *operands = MVM_spesh_alloc(tc, g, 2 * sizeof(MVMSpeshOperand));
operands[0] = target;
operands[1] = return_ins->operands[0];
return_ins->info = MVM_op_get_op(MVM_OP_set);
return_ins->operands = operands;
}
static void return_to_box(MVMThreadContext *tc, MVMSpeshGraph *g, MVMSpeshBB *return_bb,
MVMSpeshIns *return_ins, MVMSpeshOperand target,
MVMuint16 box_type_op, MVMuint16 box_op) {
/* Create and insert boxing instruction after current return instruction. */
MVMSpeshIns *box_ins = MVM_spesh_alloc(tc, g, sizeof(MVMSpeshIns));
MVMSpeshOperand *box_operands = MVM_spesh_alloc(tc, g, 3 * sizeof(MVMSpeshOperand));
box_ins->info = MVM_op_get_op(box_op);
box_ins->operands = box_operands;
box_operands[0] = target;
box_operands[1] = return_ins->operands[0];
box_operands[2] = target;
MVM_spesh_manipulate_insert_ins(tc, return_bb, return_ins, box_ins);
/* Now turn return instruction node into lookup of appropriate box
* type. */
return_ins->info = MVM_op_get_op(box_type_op);
return_ins->operands[0] = target;
}
static void rewrite_int_return(MVMThreadContext *tc, MVMSpeshGraph *g,
MVMSpeshBB *return_bb, MVMSpeshIns *return_ins,
MVMSpeshBB *invoke_bb, MVMSpeshIns *invoke_ins) {
switch (invoke_ins->info->opcode) {
case MVM_OP_invoke_v:
MVM_spesh_manipulate_delete_ins(tc, g, return_bb, return_ins);
break;
case MVM_OP_invoke_i:
return_to_set(tc, g, return_ins, invoke_ins->operands[0]);
break;
case MVM_OP_invoke_o:
return_to_box(tc, g, return_bb, return_ins, invoke_ins->operands[0],
MVM_OP_hllboxtype_i, MVM_OP_box_i);
break;
default:
MVM_oops(tc,
"Spesh inline: unhandled case of return_i");
}
}
static void rewrite_num_return(MVMThreadContext *tc, MVMSpeshGraph *g,
MVMSpeshBB *return_bb, MVMSpeshIns *return_ins,
MVMSpeshBB *invoke_bb, MVMSpeshIns *invoke_ins) {
switch (invoke_ins->info->opcode) {
case MVM_OP_invoke_v:
MVM_spesh_manipulate_delete_ins(tc, g, return_bb, return_ins);
break;
case MVM_OP_invoke_n:
return_to_set(tc, g, return_ins, invoke_ins->operands[0]);
break;
case MVM_OP_invoke_o:
return_to_box(tc, g, return_bb, return_ins, invoke_ins->operands[0],
MVM_OP_hllboxtype_n, MVM_OP_box_n);
break;
default:
MVM_oops(tc,
"Spesh inline: unhandled case of return_n");
}
}
static void rewrite_str_return(MVMThreadContext *tc, MVMSpeshGraph *g,
MVMSpeshBB *return_bb, MVMSpeshIns *return_ins,
MVMSpeshBB *invoke_bb, MVMSpeshIns *invoke_ins) {
switch (invoke_ins->info->opcode) {
case MVM_OP_invoke_v:
MVM_spesh_manipulate_delete_ins(tc, g, return_bb, return_ins);
break;
case MVM_OP_invoke_s:
return_to_set(tc, g, return_ins, invoke_ins->operands[0]);
break;
case MVM_OP_invoke_o:
return_to_box(tc, g, return_bb, return_ins, invoke_ins->operands[0],
MVM_OP_hllboxtype_s, MVM_OP_box_s);
break;
default:
MVM_oops(tc,
"Spesh inline: unhandled case of return_s");
}
}
static void rewrite_obj_return(MVMThreadContext *tc, MVMSpeshGraph *g,
MVMSpeshBB *return_bb, MVMSpeshIns *return_ins,
MVMSpeshBB *invoke_bb, MVMSpeshIns *invoke_ins) {
switch (invoke_ins->info->opcode) {
case MVM_OP_invoke_v:
MVM_spesh_manipulate_delete_ins(tc, g, return_bb, return_ins);
break;
case MVM_OP_invoke_o:
return_to_set(tc, g, return_ins, invoke_ins->operands[0]);
break;
default:
MVM_oops(tc,
"Spesh inline: unhandled case of return_o");
}
}
static void rewrite_returns(MVMThreadContext *tc, MVMSpeshGraph *inliner,
MVMSpeshGraph *inlinee, MVMSpeshBB *invoke_bb,
MVMSpeshIns *invoke_ins, MVMSpeshBB *inlinee_last_bb) {
/* Locate return instructions. */
MVMSpeshBB *bb = inlinee->entry;
while (bb) {
MVMSpeshIns *ins = bb->first_ins;
while (ins) {
MVMuint16 opcode = ins->info->opcode;
switch (opcode) {
case MVM_OP_return:
if (invoke_ins->info->opcode == MVM_OP_invoke_v) {
MVM_spesh_manipulate_insert_goto(tc, inliner, bb, ins,
invoke_bb->succ[0]);
tweak_succ(tc, inliner, bb, invoke_bb->succ[0]);
}
else {
MVM_oops(tc,
"Spesh inline: return_v/invoke_[!v] mismatch");
}
break;
case MVM_OP_return_i:
MVM_spesh_manipulate_insert_goto(tc, inliner, bb, ins,
invoke_bb->succ[0]);
tweak_succ(tc, inliner, bb, invoke_bb->succ[0]);
rewrite_int_return(tc, inliner, bb, ins, invoke_bb, invoke_ins);
break;
case MVM_OP_return_n:
MVM_spesh_manipulate_insert_goto(tc, inliner, bb, ins,
invoke_bb->succ[0]);
tweak_succ(tc, inliner, bb, invoke_bb->succ[0]);
rewrite_num_return(tc, inliner, bb, ins, invoke_bb, invoke_ins);
break;
case MVM_OP_return_s:
MVM_spesh_manipulate_insert_goto(tc, inliner, bb, ins,
invoke_bb->succ[0]);
tweak_succ(tc, inliner, bb, invoke_bb->succ[0]);
rewrite_str_return(tc, inliner, bb, ins, invoke_bb, invoke_ins);
break;
case MVM_OP_return_o:
MVM_spesh_manipulate_insert_goto(tc, inliner, bb, ins,
invoke_bb->succ[0]);
tweak_succ(tc, inliner, bb, invoke_bb->succ[0]);
rewrite_obj_return(tc, inliner, bb, ins, invoke_bb, invoke_ins);
break;
}
ins = ins->next;
}
if (bb == inlinee_last_bb) break;
bb = bb->linear_next;
}
}
/* Re-writes argument passing and parameter taking instructions to simple
* register set operations. */
static void rewrite_args(MVMThreadContext *tc, MVMSpeshGraph *inliner,
MVMSpeshGraph *inlinee, MVMSpeshBB *invoke_bb,
MVMSpeshCallInfo *call_info, MVMSpeshBB *inlinee_last_bb) {
/* Look for param-taking instructions. Track what arg instructions we
* use in the process. */
MVMSpeshBB *bb = inlinee->entry;
while (bb) {
MVMSpeshIns *ins = bb->first_ins;
while (ins) {
MVMuint16 opcode = ins->info->opcode;
MVMSpeshIns *next = ins->next;
switch (opcode) {
case MVM_OP_sp_getarg_o:
case MVM_OP_sp_getarg_i:
case MVM_OP_sp_getarg_n:
case MVM_OP_sp_getarg_s: {
MVMuint16 idx = ins->operands[1].lit_i16;
MVMSpeshIns *arg_ins = call_info->arg_ins[idx];
switch (arg_ins->info->opcode) {
case MVM_OP_arg_i:
case MVM_OP_arg_n:
case MVM_OP_arg_s:
case MVM_OP_arg_o:
/* Receiver just becomes a set instruction; delete the
* argument passing instruction. */
ins->info = MVM_op_get_op(MVM_OP_set);
ins->operands[1] = arg_ins->operands[1];
MVM_spesh_get_facts(tc, inliner, ins->operands[1])->usages++;
MVM_spesh_manipulate_delete_ins(tc, inliner,
call_info->prepargs_bb, arg_ins);
break;
case MVM_OP_argconst_i:
arg_ins->info = MVM_op_get_op(MVM_OP_const_i64);
arg_ins->operands[0] = ins->operands[0];
MVM_spesh_manipulate_delete_ins(tc, inliner, bb, ins);
MVM_spesh_get_facts(tc, inliner, arg_ins->operands[0])->usages++;
break;
case MVM_OP_argconst_n:
arg_ins->info = MVM_op_get_op(MVM_OP_const_n64);
arg_ins->operands[0] = ins->operands[0];
MVM_spesh_manipulate_delete_ins(tc, inliner, bb, ins);
MVM_spesh_get_facts(tc, inliner, arg_ins->operands[0])->usages++;
break;
case MVM_OP_argconst_s:
arg_ins->info = MVM_op_get_op(MVM_OP_const_s);
arg_ins->operands[0] = ins->operands[0];
MVM_spesh_manipulate_delete_ins(tc, inliner, bb, ins);
MVM_spesh_get_facts(tc, inliner, arg_ins->operands[0])->usages++;
break;
default:
MVM_oops(tc,
"Spesh inline: unhandled arg instruction %d",
arg_ins->info->opcode);
}
break;
}
}
ins = next;
}
if (bb == inlinee_last_bb) break;
bb = bb->linear_next;
}
{
MVMSpeshIns *arg_ins = call_info->prepargs_ins->next;
/* If there's some args that are not fetched by our inlinee,
* we have to kick them out, as arg_* ops are only valid between
* a prepargs and invoke_* op. */
while (arg_ins) {
MVMuint16 opcode = arg_ins->info->opcode;
MVMSpeshIns *next = arg_ins->next;
switch (opcode) {
case MVM_OP_arg_i:
case MVM_OP_arg_n:
case MVM_OP_arg_s:
case MVM_OP_arg_o:
case MVM_OP_argconst_i:
case MVM_OP_argconst_n:
case MVM_OP_argconst_s:
MVM_spesh_manipulate_delete_ins(tc, inliner, call_info->prepargs_bb, arg_ins);
break;
case MVM_OP_set:
break;
case MVM_OP_invoke_i:
case MVM_OP_invoke_n:
case MVM_OP_invoke_s:
case MVM_OP_invoke_o:
case MVM_OP_invoke_v:
default:
next = NULL;
}
arg_ins = next;
}
}
/* Delete the prepargs instruction. */
MVM_spesh_manipulate_delete_ins(tc, inliner, invoke_bb, call_info->prepargs_ins);
}
/* Annotates first and last instruction in post-processed inlinee with start
* and end inline annotations. */
static void annotate_inline_start_end(MVMThreadContext *tc, MVMSpeshGraph *inliner,
MVMSpeshGraph *inlinee, MVMint32 idx,
MVMSpeshBB *inlinee_last_bb,
MVMuint32 inline_boundary_handler) {
/* Annotate first instruction as an inline start. */
MVMSpeshAnn *start_ann = MVM_spesh_alloc(tc, inliner, sizeof(MVMSpeshAnn));
MVMSpeshAnn *end_ann = MVM_spesh_alloc(tc, inliner, sizeof(MVMSpeshAnn));
MVMSpeshBB *bb = inlinee->entry->succ[0];
start_ann->next = bb->first_ins->annotations;
start_ann->type = MVM_SPESH_ANN_INLINE_START;
start_ann->data.inline_idx = idx;
bb->first_ins->annotations = start_ann;
/* Insert annotation for handler boundary indicator fixup. */
start_ann = MVM_spesh_alloc(tc, inliner, sizeof(MVMSpeshAnn));
start_ann->next = bb->first_ins->annotations;
start_ann->type = MVM_SPESH_ANN_FH_START;
start_ann->data.frame_handler_index = inline_boundary_handler;
bb->first_ins->annotations = start_ann;
/* Now look for last instruction and annotate it. */
end_ann->next = inlinee_last_bb->last_ins->annotations;
end_ann->type = MVM_SPESH_ANN_INLINE_END;
end_ann->data.inline_idx = idx;
inlinee_last_bb->last_ins->annotations = end_ann;
/* Insert annotation for handler boundary fixup; we add the end
* one that is needed and also a dummy goto one to keep things
* that want all three happy. */
end_ann = MVM_spesh_alloc(tc, inliner, sizeof(MVMSpeshAnn));
end_ann->next = inlinee_last_bb->last_ins->annotations;
end_ann->type = MVM_SPESH_ANN_FH_END;
end_ann->data.frame_handler_index = inline_boundary_handler;
inlinee_last_bb->last_ins->annotations = end_ann;
end_ann = MVM_spesh_alloc(tc, inliner, sizeof(MVMSpeshAnn));
end_ann->next = inlinee_last_bb->last_ins->annotations;
end_ann->type = MVM_SPESH_ANN_FH_GOTO;
end_ann->data.frame_handler_index = inline_boundary_handler;
inlinee_last_bb->last_ins->annotations = end_ann;
return;
}
/* Drives the overall inlining process. */
void MVM_spesh_inline(MVMThreadContext *tc, MVMSpeshGraph *inliner,
MVMSpeshCallInfo *call_info, MVMSpeshBB *invoke_bb,
MVMSpeshIns *invoke_ins, MVMSpeshGraph *inlinee,
MVMStaticFrame *inlinee_sf, MVMSpeshOperand code_ref_reg) {
/* Merge inlinee's graph into the inliner. */
MVMuint32 inline_boundary_handler;
MVMSpeshBB *inlinee_last_bb = merge_graph(tc, inliner, inlinee, inlinee_sf,
invoke_bb, invoke_ins, code_ref_reg, &inline_boundary_handler);
/* If we're profiling, note it's an inline. */
if (inlinee->entry->linear_next->first_ins->info->opcode == MVM_OP_prof_enterspesh) {
MVMSpeshIns *profenter = inlinee->entry->linear_next->first_ins;
profenter->info = MVM_op_get_op(MVM_OP_prof_enterinline);
profenter->operands = MVM_spesh_alloc(tc, inliner, sizeof(MVMSpeshOperand));