/
jit.c
1847 lines (1538 loc) · 57.9 KB
/
jit.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
/*
Copyright (C) 2001-2008, Parrot Foundation.
$Id$
=head1 NAME
src/jit.c - JIT
=head1 DESCRIPTION
JIT (Just In Time) compilation converts bytecode to native machine code
instructions and executes the generated instruction sequence directly.
Actually it's not really just in time, it's just before this piece of code is
used and not per subroutine or even opcode, it works per bytecode segment.
=head2 Functions
=over 4
=cut
*/
/* HEADERIZER HFILE: none */
/* HEADERIZER STOP */
#include <parrot/parrot.h>
#if PARROT_EXEC_CAPABLE
# include "parrot/exec.h"
#endif
#include "jit.h"
#define JIT_EMIT 0
#include "jit_emit.h"
#include "parrot/packfile.h"
#include "parrot/oplib/ops.h"
#include "pmc/pmc_sub.h"
#include "pmc/pmc_managedstruct.h"
#define JIT_SEGS 0
extern int jit_op_count(void);
/*
* s. jit/$jitcpuarch/jit_emit.h for the meaning of these defs
*/
#ifndef ALLOCATE_REGISTERS_PER_SECTION
# define ALLOCATE_REGISTERS_PER_SECTION 1
#endif
#if defined __GNUC__ || defined __IBMC__
void Parrot_jit_debug(PARROT_INTERP);
#endif
/*
=item C<static void
insert_fixup_targets(PARROT_INTERP, char *branch,
size_t limit)>
Look at fixups, mark all fixup entries as branch target.
TODO: actually this is wrong: fixups belong only to one code segment.
The code below doesn't check, for which segments the fixups are
inserted.
=cut
*/
static void
insert_fixup_targets(PARROT_INTERP, char *branch,
size_t limit)
{
PackFile_FixupTable *ft = interp->code->fixups;
int i;
if (!ft)
return;
for (i = 0; i < ft->fixup_count; i++) {
if (ft->fixups[i]->type == enum_fixup_label) {
if ((size_t)ft->fixups[i]->offset < limit)
branch[ft->fixups[i]->offset] |= JIT_BRANCH_TARGET;
}
}
}
/*
=item C<static void
make_branch_list(PARROT_INTERP,
Parrot_jit_optimizer_t * optimizer,
opcode_t *code_start, opcode_t *code_end)>
C<< optimizer->map_branch >> parallels the opcodes with a list of
branch information and register mapping information
=over 4
=item branch instructions have C<JIT_BRANCH_SOURCE>
=item opcodes jumped to have C<JIT_BRANCH_TARGET>
=item mapped arguments have register type + 1 and finally
=item after register allocation these have the processor register that
got mapped
=back
=cut
*/
static void
make_branch_list(PARROT_INTERP,
Parrot_jit_optimizer_t * optimizer,
opcode_t *code_start, opcode_t *code_end)
{
op_info_t *op_info;
char *branch;
opcode_t *cur_op;
cur_op = code_start;
/* Allocate space for the branch information and register map */
optimizer->map_branch = branch =
(char *)mem_sys_allocate_zeroed((size_t)(code_end - code_start + 1));
/* Allocate space for the branch list */
optimizer->branch_list = (opcode_t **)
mem_sys_allocate_zeroed(
(size_t)(code_end - code_start) * sizeof (opcode_t *));
/* If the opcode jumps we may:
*
* PARROT_JUMP_RELATIVE:
* The op jumps to an address relative to the current position,
* thus we mark the branch target and the branch source.
*
* PARROT_JUMP_ADDRESS:
* The op jumps to an absolute address, thus we mark the branch
* target.
*
* PARROT_JUMP_POP:
* The op pops the address to jump to, thus we don't mark the
* branch target, anyway it may probably use expr(NEXT)
*
* PARROT_JUMP_ENEXT:
* The op does something with expr(NEXT),
* XXX I'll assume that it's stored in the control stack for
* later returning since that's the only way it's used now
* but this should go away by the time we add some metadata
* to the ops.
* So we will mark the branch target.
*
* PARROT_JUMP_GNEXT:
* Means the opcode does some other kind of jump, and also
* might goto(NEXT)
*
* PARROT_JUMP_UNPREDICTABLE:
* The branch target is unpredictable.
* Things get a little tricky since it's not 100% true that the
* target is unpredictable because of the set_addr opcode, we
* need to find a solution for this, in the mean time, we will
* make each section have its own arena and try to avoid
* going in and out from them as much as possible.
*
* PARROT_JUMP_RESTART
* If the parrot program counter is zero, fall out of the
* run loop.
*
*/
while (cur_op < code_end) {
opcode_t op = *cur_op;
int i, n;
size_t rel_offset;
/* Predereference the opcode information table for this opcode
* early since it's going to be used many times */
op_info = &interp->op_info_table[op];
/* if op_info->jump is not 0 this opcode may jump,
* so mark this opcode as a branch source */
rel_offset = cur_op - code_start;
n = op_info->op_count;
if (op == PARROT_OP_set_args_pc ||
op == PARROT_OP_set_returns_pc ||
op == PARROT_OP_get_results_pc)
goto no_branch;
if (op_info->jump)
branch[rel_offset] |= JIT_BRANCH_SOURCE;
for (i = 1; i < n; ++i) {
/* If it's not a constant, no joy */
if (op_info->types[i-1] == PARROT_ARG_IC && op_info->labels[i-1]) {
/* The branch target is relative,
* the offset is in the i argument
*/
if (op_info->jump & PARROT_JUMP_RELATIVE) {
/* Set the branch target */
optimizer->branch_list[rel_offset] = cur_op + cur_op[i];
branch[rel_offset + cur_op[i]] |= JIT_BRANCH_TARGET;
}
/* The branch target is absolute,
* the address is in the i argument
*/
else if (op_info->jump & PARROT_JUMP_ADDRESS) {
/* Set the branch target */
optimizer->branch_list[rel_offset] = cur_op + cur_op[i];
branch[cur_op[i]] |= JIT_BRANCH_TARGET;
}
/* the labels of set_addr and newsub are branch targets too
* this is needed e.g. for JIT_CGP
*/
else {
branch[rel_offset + cur_op[i]] |= JIT_BRANCH_TARGET;
}
}
}
/* The address of the next opcode */
if ((op_info->jump & PARROT_JUMP_ENEXT) ||
(op_info->jump & PARROT_JUMP_GNEXT))
branch[rel_offset + n] |= JIT_BRANCH_TARGET;
if (op_info->jump & PARROT_JUMP_UNPREDICTABLE) {
/*
* TODO
* this flag is currently not used or set
* and: if we have a branch that isn't going to a constant
* target like a calculated branch used by rx_ opcodes
* we are totally lost WRT register preservation.
* If we don't know, that the code is a branch target, inside
* a JITted code section, mapped registers might be
* not up to date WRT Parrot registers.
*/
optimizer->has_unpredictable_jump = 1;
}
no_branch:
/* Move to the next opcode */
ADD_OP_VAR_PART(interp, interp->code, cur_op, n);
cur_op += n;
}
insert_fixup_targets(interp, branch, code_end - code_start);
}
/*
=item C<static void
set_register_usage(PARROT_INTERP,
Parrot_jit_info_t *jit_info,
Parrot_jit_optimizer_section_ptr cur_section,
op_info_t *op_info, opcode_t *cur_op, opcode_t *code_start)>
Sets the register usage counts.
=cut
*/
static void
set_register_usage(PARROT_INTERP,
Parrot_jit_info_t *jit_info,
Parrot_jit_optimizer_section_ptr cur_section,
op_info_t *op_info, opcode_t *cur_op, opcode_t *code_start)
{
int argn, args, argt;
int typ;
Parrot_jit_register_usage_t *ru = cur_section->ru;
Parrot_jit_optimizer_t * optimizer = jit_info->optimizer;
char * const map = optimizer->map_branch;
/* For each argument that has the opcode increment the usage count,
* We move from the end since we need to check if the first opcode
* using the register will read or write it.
*
* registers are set per their type [IPSN]
* */
args = argt = op_info->op_count;
ADD_OP_VAR_PART(interp, interp->code, cur_op, argt);
for (argn = argt - 1; argn > 0; argn--) {
/* TODO check the argn-1 entries */
int idx = *(cur_op + argn);
int arg_type;
PMC *sig;
if (argn >= args) {
sig = CONTEXT(interp)->constants[cur_op[1]]->u.key;
arg_type = VTABLE_get_integer_keyed_int(interp,
sig, argn - args);
arg_type &= (PARROT_ARG_TYPE_MASK | PARROT_ARG_CONSTANT);
}
else
arg_type = op_info->types[argn - 1];
switch (arg_type) {
case PARROT_ARG_I:
case PARROT_ARG_KI:
typ = 0;
/*
* if the register number is negative, the register mapping
* was done by imcc/jit.c, which used negative numbers
* for allocated CPU registers. That's currently not
* functional because of changed register allocation
* strategy inside imcc.
* The code is still here and should probably be reactivated
* later, when things are stable: imcc has all the
* necessary information like basic blocks and loop depth
* calculated already. A lot is duplicated here to regain this
* information.
*/
if (idx < 0)
idx = -1 - idx;
break;
case PARROT_ARG_P:
case PARROT_ARG_K:
/*
* P and S regs aren't currently used at all. That's not
* really optimal. If we have plenty of mappable registers
* and if we can call vtables or MMD functions directly
* we should finally allocate P and S regs too.
*/
typ = 1;
break;
case PARROT_ARG_S:
typ = 2;
break;
case PARROT_ARG_N:
if (idx < 0)
idx = -1 - idx;
typ = 3;
break;
default:
typ = -1;
break;
}
/*
* JIT structures are NUM_REGISTERS big
* we can currently allocate only that much
*/
if (typ >= 0 && idx < NUM_REGISTERS) {
/* remember the register typ (+1) for this op argument
* for register allocation */
map[cur_op + argn - code_start] = typ + 1;
if ((!ru[typ].reg_count[idx]++) &&
(op_info->dirs[argn-1] & PARROT_ARGDIR_IN))
ru[typ].reg_dir[idx] |= PARROT_ARGDIR_IN;
if (op_info->dirs[argn-1] & PARROT_ARGDIR_OUT) {
ru[typ].reg_dir[idx] |= PARROT_ARGDIR_OUT;
}
}
/* key constants may have register keys */
else if (arg_type == PARROT_ARG_KC) {
PMC *key = interp->code->const_table->constants[idx]->u.key;
while (key) {
const UINTVAL flags = PObj_get_FLAGS(key);
if (flags & KEY_register_FLAG) {
INTVAL n = 0;
if (flags & KEY_integer_FLAG) {
n = VTABLE_get_integer(interp, key);
typ = 0;
if (n < 0)
n = -1 - n;
}
else if (flags & KEY_pmc_FLAG)
typ = 1;
else if (flags & KEY_string_FLAG)
typ = 2;
if (n < NUM_REGISTERS && !ru[typ].reg_count[n]++)
ru[typ].reg_dir[n] |= PARROT_ARGDIR_IN;
}
key = key_next(interp, key);
}
}
}
}
/*
=item C<static void
init_regusage(PARROT_INTERP,
Parrot_jit_optimizer_section_ptr cur_section)>
Init all register usage to Parrot register usage. Used when JITting
subroutines to registers only
=cut
*/
static void
init_regusage(PARROT_INTERP, Parrot_jit_optimizer_section_ptr cur_section)
{
int typ;
cur_section->ru[0].registers_used =
CONTEXT(interp)->n_regs_used[REGNO_INT];
cur_section->ru[3].registers_used =
CONTEXT(interp)->n_regs_used[REGNO_NUM];
cur_section->ru[1].registers_used =
cur_section->ru[2].registers_used = 0;
for (typ = 0; typ < 4; typ++) {
int j;
for (j = 0; j < cur_section->ru[typ].registers_used; j++)
cur_section->ru[typ].reg_usage[j] = j;
}
}
/*
=item C<static void make_sections(PARROT_INTERP,
Parrot_jit_info_t *jit_info,
opcode_t *code_start, opcode_t *code_end)>
I386 has JITed vtables, which have the vtable# in extcall.
This C<Parrot_jit_vtable_n_op()> does use register mappings.
=cut
*/
#ifndef EXTCALL
# define EXTCALL(op) (op_jit[(op)].extcall >= 1 || (op) >= jit_op_count())
# define CALLS_C_CODE(op) (op_func[(op)].extcall == -1)
#endif
static void
make_sections(PARROT_INTERP,
Parrot_jit_info_t *jit_info,
opcode_t *code_start, opcode_t *code_end)
{
Parrot_jit_optimizer_section_ptr cur_section, t_section, prev_section;
opcode_t *next_op;
op_info_t *op_info;
char *branch;
int branched, start_new;
opcode_t *cur_op;
Parrot_jit_optimizer_t * optimizer;
optimizer = jit_info->optimizer;
branch = optimizer->map_branch;
/* Allocate the first section */
cur_section = optimizer->sections = (Parrot_jit_optimizer_section_t *)
mem_sys_allocate_zeroed(sizeof (Parrot_jit_optimizer_section_t));
cur_section->begin = code_start;
prev_section = cur_section;
cur_op = code_start;
/* set all regs to Parrot's */
if (jit_info->code_type == JIT_CODE_SUB_REGS_ONLY)
init_regusage(interp, cur_section);
while (cur_section) {
opcode_t op = *cur_op;
branched = start_new = 0;
/* Predereference the opcode information for this opcode
* early since it's going to be used many times */
op_info = &interp->op_info_table[op];
/* Calculate the next pc */
next_op = cur_op + op_info->op_count;
ADD_OP_VAR_PART(interp, interp->code, cur_op, next_op);
/* Update op_count */
cur_section->op_count++;
/* set register usage for this section */
set_register_usage(interp, jit_info, cur_section,
op_info, cur_op, code_start);
/*
* End a section:
* If this opcode is jitted and next is a C function */
if (!EXTCALL(op)) {
cur_section->jit_op_count++;
if (next_op < code_end && EXTCALL(*next_op))
start_new = 1;
}
else
/* or if current section is not jitted, and the next opcode
* is. */
if (next_op < code_end && !EXTCALL(*next_op))
start_new = 1;
/* or when the current opcode is a branch source,
* in other words if the opcode jumps, or if the next opcode is
* a branch target, allocate a new section only if it's not the
* last opcode */
if ((branch[cur_op - code_start] & JIT_BRANCH_SOURCE)
|| (next_op < code_end &&
(branch[next_op - code_start] & JIT_BRANCH_TARGET))
|| (next_op >= code_end)) {
/* remember to start a new block */
branched = 1;
start_new = 1;
}
if (start_new) {
/* Set the type, depending on whether the current
* instruction is external or jitted. */
cur_section->isjit = !EXTCALL(op);
/* Save the address where the section ends */
cur_section->end = cur_op;
if (next_op < code_end) {
/* Allocate a new section */
t_section = (Parrot_jit_optimizer_section_t *)
mem_sys_allocate_zeroed(
sizeof (Parrot_jit_optimizer_section_t));
/* Add it to the double linked list */
cur_section->next = t_section;
t_section->prev = cur_section;
/* Make the new section be the current one */
cur_section = t_section;
/* set all regs to Parrot's */
if (jit_info->code_type == JIT_CODE_SUB_REGS_ONLY)
init_regusage(interp, cur_section);
/* registers get either allocated per section or
* per basic block (i.e. one or more sections divided
* by branches. When allocation per block is done
* all sections in one block have the same block number
*/
if (ALLOCATE_REGISTERS_PER_SECTION || branched) {
cur_section->block = prev_section->block + 1;
prev_section = cur_section;
}
else
cur_section->block = prev_section->block;
/* Save the address where the section begins */
cur_section->begin = next_op;
}
else {
cur_section = NULL;
}
}
/* Move to the next opcode */
cur_op = next_op;
}
}
/*
=item C<static void
make_branch_targets(
Parrot_jit_optimizer_t *optimizer, opcode_t * code_start)>
Makes the branch targets.
=cut
*/
static void
make_branch_targets(Parrot_jit_optimizer_t *optimizer, const opcode_t * code_start)
{
Parrot_jit_optimizer_section_ptr cur_section, t_section;
/* Set the branch target of this section, that is the section where
* the program execution continues, if it ends in a branch source we
* use the branch target and not the next section. */
cur_section = optimizer->sections;
while (cur_section) {
if (optimizer->branch_list[cur_section->end - code_start]) {
/* If the branch target is to a section before the current one
* move from the start, otherwise from the current section */
if (optimizer->branch_list[cur_section->end - code_start] <
cur_section->begin)
t_section = optimizer->sections;
else
t_section = cur_section;
while (t_section) {
/* If we find the section attach it to the current one. */
if (t_section->begin ==
optimizer->branch_list[cur_section->end - code_start]) {
cur_section->branch_target = t_section;
break;
}
/* If not move to the next. */
t_section = t_section->next;
}
}
/* Move to the next section */
cur_section = cur_section->next;
}
}
/*
=item C<static void
sort_registers(Parrot_jit_info_t *jit_info)>
Sorts the Parrot registers prior to mapping them to actual hardware registers.
=cut
*/
static void
sort_registers(Parrot_jit_info_t *jit_info)
{
Parrot_jit_optimizer_t *optimizer;
Parrot_jit_optimizer_section_ptr cur_section, next;
int any, k, typ, code_type;
int max_count, max_i = 0;
int to_map[] = { 0, 0, 0, 0 };
code_type = jit_info->code_type;
to_map[0] = jit_info->arch_info->regs[code_type].n_mapped_I;
to_map[3] = jit_info->arch_info->regs[code_type].n_mapped_F;
optimizer = jit_info->optimizer;
/* Start from the first section */
cur_section = optimizer->sections;
while (cur_section) {
Parrot_jit_register_usage_t *ru = cur_section->ru;
/* sum up register usage for one block, don't change
* reg_dir. If allocation is done per section, block numbers
* are different, so this is a nop
*/
next = cur_section->next;
while (next && next->block == cur_section->block) {
const Parrot_jit_register_usage_t * const nru = next->ru;
for (typ = 0; typ < 4; typ++) {
int i;
for (i = 0; i < NUM_REGISTERS; i++)
ru[typ].reg_count[i] += nru[typ].reg_count[i];
}
next = next->next;
}
/* now sort registers by their usage count */
for (typ = 0; typ < 4; typ++) {
/* find most used register */
int i;
for (i = max_count = 0; i < NUM_REGISTERS; i++) {
if (cur_section->ru[typ].reg_count[i] > max_count) {
max_count = cur_section->ru[typ].reg_count[i];
max_i = i;
}
}
/* start from this register and set usage */
k = ru[typ].registers_used = 0;
/* no usage, go on with next type */
if (max_count == 0 || !to_map[typ])
continue;
/* as long as we map registers for this typ */
while (1) {
if (max_i >= 0)
ru[typ].reg_usage[k++] = max_i;
/* all mapped? */
if (k == to_map[typ])
break;
/* now check for equal usage starting after maxi */
for (any = 0, i = max_i + 1; i < NUM_REGISTERS; i++) {
if (ru[typ].reg_count[i] == max_count) {
max_i = i;
any = 1;
break;
}
}
/* if same usage not found, look for lower usage */
if (any == 0) {
if (max_count > 1) {
max_count--;
max_i = -1;
continue;
}
break;
}
}
ru[typ].registers_used = k;
}
next = cur_section->next;
/* duplicate usage to all sections of block */
while (next && next->block == cur_section->block) {
Parrot_jit_register_usage_t * const nru = next->ru;
for (typ = 0; typ < 4; typ++) {
int i;
for (i = 0; i < ru[typ].registers_used; i++) {
nru[typ].reg_count[i] = ru[typ].reg_count[i];
nru[typ].reg_usage[i] = ru[typ].reg_usage[i];
}
nru[typ].registers_used = ru[typ].registers_used;
}
next = next->next;
}
/* Move to the next section */
cur_section = next;
}
}
/*
=item C<static void
assign_registers(PARROT_INTERP,
Parrot_jit_info_t *jit_info,
Parrot_jit_optimizer_section_ptr cur_section,
opcode_t * code_start, int from_imcc)>
Called by C<map_registers()> to actually assign the Parrot registers to
hardware registers.
TODO
Before actually assigning registers, we should optimize a bit:
1) calculate max use count of register types for all sections
2) calculate costs for register preserving and restoring
for two different allocation strategies:
a) allocate non-volatiles first
overhead for jit_begin, jit_end:
- 2 * max_used_non_volatile registers
overhead for register preserving around non-jitted sections:
- only used IN arguments are saved
- only OUT non-volatile arguments are restored
b) allocate volatiles first
no overhead for jit_begin, jit_end
overhead per JITed op that calls a C function:
- 2 * n_used_volatiles_to_preserve for each call
overhead for register preserving around non-jitted sections:
- all volatiles are saved and restored around non-jitted sections
NB for all cost estimations size does matter: a 64bit double counts as
two 32bit ints. Opcode count is assumed to be just one.
3) depending on costs from 2) use one of the strategies
That does still not account for any usage patterns. Imcc has loop
nesting depth, but that's not available here. OTOH smaller code tends
to perform better because of better cache usage.
Usage analysis could show that a mixture of both strategies is best, e.g:
allocate 2-4 non-volatiles and the rest from volatiles. But that would
complicate the allocation code a bit.
=cut
*/
static void
assign_registers(PARROT_INTERP,
Parrot_jit_info_t *jit_info,
Parrot_jit_optimizer_section_ptr cur_section,
opcode_t * code_start, int from_imcc)
{
char *map;
Parrot_jit_optimizer_t *optimizer;
int i;
opcode_t * cur_op;
const char * maps[] = {0, 0, 0, 0};
const int code_type = jit_info->code_type;
maps[0] = jit_info->arch_info->regs[code_type].map_I;
maps[3] = jit_info->arch_info->regs[code_type].map_F;
optimizer = jit_info->optimizer;
map = optimizer->map_branch;
/* For each opcode in this section */
cur_op = cur_section->begin;
while (cur_op <= cur_section->end) {
const opcode_t op = *cur_op;
const op_info_t * const op_info = &interp->op_info_table[op];
int op_arg;
int n;
/* For each argument of the current opcode */
n = op_info->op_count;
ADD_OP_VAR_PART(interp, interp->code, cur_op, n);
for (op_arg = 1; op_arg < n; op_arg++) {
/* get the register typ */
int typ = map[cur_op + op_arg - code_start];
/* clear the register typ/map */
map[cur_op + op_arg - code_start] = 0;
/* if not JITted, don't map */
if (!cur_section->isjit)
continue;
if (typ > 0) {
typ--; /* types are + 1 */
if (!maps[typ])
continue;
/* If the argument is in most used list for this typ */
for (i = 0; i < cur_section->ru[typ].registers_used; i++) {
opcode_t idx = cur_op[op_arg];
if (from_imcc)
idx = -1 - idx;
if (idx == (opcode_t)cur_section->ru[typ].reg_usage[i]) {
map[cur_op + op_arg - code_start] = maps[typ][i];
cur_section->maps++;
break;
}
}
}
}
/* Move to the next opcode */
cur_op += n;
}
}
/*
=item C<static void
map_registers(PARROT_INTERP,
Parrot_jit_info_t *jit_info, opcode_t * code_start)>
Maps the most used Parrot registers to hardware registers.
=cut
*/
static void
map_registers(PARROT_INTERP,
Parrot_jit_info_t *jit_info,
opcode_t * code_start)
{
/* Start from the first section */
Parrot_jit_optimizer_section_ptr cur_section = jit_info->optimizer->sections;
/* While there is section */
while (cur_section) {
assign_registers(interp, jit_info, cur_section, code_start, 0);
/* Move to the next section */
cur_section = cur_section->next;
}
}
#define JIT_DEBUG 0
#if JIT_DEBUG
/*
=item C<static void
debug_sections(PARROT_INTERP,
Parrot_jit_optimizer_t *optimizer, opcode_t * code_start)>
Prints out debugging info.
=cut
*/
static void
debug_sections(PARROT_INTERP,
Parrot_jit_optimizer_t *optimizer, opcode_t * code_start)
{
Parrot_jit_optimizer_section_ptr cur_section;
# if JIT_DEBUG > 1
char * map = optimizer->map_branch;
# endif
int n;
const char types[] = "IPSN";
int types_to_list[] = {0, 3};
cur_section = optimizer->sections;
while (cur_section) {
opcode_t * cur_op;
unsigned int j;
Parrot_jit_register_usage_t *ru = cur_section->ru;
Parrot_io_eprintf(interp, "\nSection:\n");
Parrot_io_eprintf(interp, "%s block %d\n",
(cur_section->isjit) ? "JITTED" : "NOT JITTED",
cur_section->block);
for (cur_op = cur_section->begin; cur_op <= cur_section->end;) {
char instr[256];
const opcode_t op = *cur_op;
const op_info_t * const op_info = &interp->op_info_table[op];
PDB_disassemble_op(interp, instr, sizeof (instr),
op_info, cur_op, NULL, code_start, 0);
Parrot_io_eprintf(interp, "\t\tOP%vu: ext %3d\t%s\n",
cur_op - code_start, op_jit[*cur_op].extcall, instr);
n = op_info->op_count;
ADD_OP_VAR_PART(interp, interp->code, cur_op, n);
# if JIT_DEBUG > 1
Parrot_io_eprintf(interp, "\t\t\tmap_branch: ");
for (i = 0; i < n; i++)
Parrot_io_eprintf(interp, "%02x ", map[cur_op-code_start+i]);
Parrot_io_eprintf(interp, "\n");
# endif
cur_op += n;
}
Parrot_io_eprintf(interp, "\tbegin:\t%#p\t(%Ou)\n",
cur_section->begin, *cur_section->begin);
Parrot_io_eprintf(interp, "\tend:\t%#p\t(%Ou)\n",
cur_section->end, *cur_section->end);
for (j = 0; j < sizeof (types_to_list)/sizeof (int); j++) {
const int typ = types_to_list[j];
const char t = types[typ];
Parrot_io_eprintf(interp, "\t%c registers used:\t%i\n",
t, ru[typ].registers_used);
if (ru[typ].registers_used) {
int i;
Parrot_io_eprintf(interp, "\t%c register count:\t", t);
for (i = 0; i < NUM_REGISTERS; i++)
Parrot_io_eprintf(interp, "%i ", ru[typ].reg_count[i]);
Parrot_io_eprintf(interp, "\n\t%c register usage:\t", t);
for (i = 0; i < NUM_REGISTERS; i++)
Parrot_io_eprintf(interp, "%i ", ru[typ].reg_usage[i]);
Parrot_io_eprintf(interp, "\n\t%c register direction:\t", t);
for (i = 0; i < NUM_REGISTERS; i++)
Parrot_io_eprintf(interp, "%i ", (int)ru[typ].reg_dir[i]);
Parrot_io_eprintf(interp, "\n");
}
}
Parrot_io_eprintf(interp, "\tJit opcodes:\t%u\n",
cur_section->jit_op_count);
Parrot_io_eprintf(interp, "\tTotal opcodes:\t%u\n",
cur_section->op_count);
if (cur_section->branch_target)
Parrot_io_eprintf(interp, "\tBranch target:\tOP%u\n",
cur_section->branch_target->begin - code_start);
cur_section = cur_section->next;
}
}
#endif
/*
=item C<static Parrot_jit_optimizer_t *
optimize_jit(PARROT_INTERP,
Parrot_jit_info_t *jit_info,
opcode_t *code_start, opcode_t *code_end)>
Called by C<parrot_build_asm()> to run the optimizer.
=cut
*/
static void
optimize_jit(PARROT_INTERP,
Parrot_jit_info_t *jit_info,
opcode_t *code_start, opcode_t *code_end)
{
Parrot_jit_optimizer_t *optimizer;
/* Allocate space for the optimizer */
jit_info->optimizer =
optimizer = (Parrot_jit_optimizer_t *)
mem_sys_allocate_zeroed(sizeof (Parrot_jit_optimizer_t));
/* Look, which opcodes might branch */
make_branch_list(interp, optimizer, code_start, code_end);
/* ok, let's loop again and generate the sections */
make_sections(interp, jit_info, code_start, code_end);
/* look where a section jumps to */
make_branch_targets(optimizer, code_start);
/* This is where we start deciding which Parrot registers get
* mapped to a hardware one in each different section. */
#if JIT_DEBUG > 2
debug_sections(interp, optimizer, code_start);
#endif
if (jit_info->code_type != JIT_CODE_SUB_REGS_ONLY)
sort_registers(jit_info);
map_registers(interp, jit_info, code_start);
#if JIT_DEBUG
debug_sections(interp, optimizer, code_start);
#endif
}
/*
=item C<static Parrot_jit_optimizer_t *
optimize_imcc_jit(PARROT_INTERP,
Parrot_jit_info_t *jit_info,
opcode_t *code_start, opcode_t *code_end,
PackFile_Segment *jit_seg)>
Generate optimizer stuff from the C<_JIT> section in the packfile.
=cut
*/
static void
optimize_imcc_jit(PARROT_INTERP,
Parrot_jit_info_t *jit_info,