forked from scarybeasts/beebjit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjit_optimizer.c
1015 lines (951 loc) · 29.8 KB
/
jit_optimizer.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 "jit_optimizer.h"
#include "defs_6502.h"
#include "jit_opcode.h"
#include "asm/asm_jit.h"
#include "asm/asm_opcodes.h"
#include "asm/asm_util.h"
#include <assert.h>
#include <string.h>
static const int32_t k_value_unknown = -1;
static void
jit_optimizer_merge_opcodes(struct jit_opcode_details* p_opcodes) {
struct jit_opcode_details* p_prev_opcode = NULL;
int32_t prev_optype = -1;
int32_t uopcode = -1;
struct jit_opcode_details* p_opcode;
for (p_opcode = p_opcodes;
p_opcode->addr_6502 != -1;
p_opcode += p_opcode->num_bytes_6502) {
uint8_t optype;
if (p_opcode->ends_block) {
continue;
}
if (p_opcode->opmode_6502 != k_acc) {
p_prev_opcode = NULL;
continue;
}
optype = p_opcode->optype_6502;
switch (optype) {
case k_asl: uopcode = k_opcode_ASL_acc; break;
case k_lsr: uopcode = k_opcode_LSR_acc; break;
case k_rol: uopcode = k_opcode_ROL_acc; break;
case k_ror: uopcode = k_opcode_ROR_acc; break;
default: assert(0); break;
}
if ((p_prev_opcode != NULL) && (optype == prev_optype)) {
int32_t index;
struct asm_uop* p_uop = jit_opcode_find_uop(p_prev_opcode,
&index,
uopcode);
assert(p_uop != NULL);
if (p_uop->value1 < 7) {
p_uop->value1++;
jit_opcode_eliminate(p_opcode);
}
} else {
p_prev_opcode = p_opcode;
prev_optype = optype;
}
}
}
static void
jit_optimizer_calculate_known_values(struct jit_opcode_details* p_opcodes) {
struct jit_opcode_details* p_opcode;
int32_t reg_a = k_value_unknown;
int32_t reg_x = k_value_unknown;
int32_t reg_y = k_value_unknown;
int32_t flag_carry = k_value_unknown;
int32_t flag_decimal = k_value_unknown;
for (p_opcode = p_opcodes;
p_opcode->addr_6502 != -1;
p_opcode += p_opcode->num_bytes_6502) {
uint16_t operand_6502 = p_opcode->operand_6502;
uint8_t optype = p_opcode->optype_6502;
uint8_t opreg = p_opcode->opreg_6502;
uint8_t opmode = p_opcode->opmode_6502;
int changes_carry = g_optype_changes_carry[optype];
p_opcode->reg_a = reg_a;
p_opcode->reg_x = reg_x;
p_opcode->reg_y = reg_y;
p_opcode->flag_carry = flag_carry;
p_opcode->flag_decimal = flag_decimal;
if (p_opcode->ends_block) {
continue;
}
switch (optype) {
case k_clc:
case k_bcs:
flag_carry = 0;
break;
case k_sec:
case k_bcc:
flag_carry = 1;
break;
case k_dey:
if (reg_y != k_value_unknown) {
reg_y = (uint8_t) (reg_y - 1);
}
break;
case k_txa:
reg_a = reg_x;
break;
case k_tya:
reg_a = reg_y;
break;
case k_ldy:
if ((opmode == k_imm) && (!p_opcode->is_dynamic_operand)) {
reg_y = operand_6502;
} else {
reg_y = k_value_unknown;
}
break;
case k_ldx:
if ((opmode == k_imm) && !p_opcode->is_dynamic_operand) {
reg_x = operand_6502;
} else {
reg_x = k_value_unknown;
}
break;
case k_tay:
reg_y = reg_a;
break;
case k_lda:
if ((opmode == k_imm) && !p_opcode->is_dynamic_operand) {
reg_a = operand_6502;
} else {
reg_a = k_value_unknown;
}
break;
case k_tax:
reg_x = reg_a;
break;
case k_iny:
if (reg_y != k_value_unknown) {
reg_y = (uint8_t) (reg_y + 1);
}
break;
case k_dex:
if (reg_x != k_value_unknown) {
reg_x = (uint8_t) (reg_x - 1);
}
break;
case k_cld:
flag_decimal = 0;
break;
case k_inx:
if (reg_x != k_value_unknown) {
reg_x = (uint8_t) (reg_x + 1);
}
break;
case k_sed:
flag_decimal = 1;
break;
default:
switch (opreg) {
case k_a:
reg_a = k_value_unknown;
break;
case k_x:
reg_x = k_value_unknown;
break;
case k_y:
reg_y = k_value_unknown;
break;
default:
break;
}
if (changes_carry) {
flag_carry = k_value_unknown;
}
break;
}
}
}
static void
jit_optimizer_replace_uops(struct jit_opcode_details* p_opcodes) {
struct jit_opcode_details* p_opcode;
int had_check_bcd = 0;
for (p_opcode = p_opcodes;
p_opcode->addr_6502 != -1;
p_opcode += p_opcode->num_bytes_6502) {
int32_t index;
struct asm_uop* p_uop;
int32_t load_uopcode_old = -1;
int32_t load_uopcode_new = -1;
uint8_t load_uopcode_value = 0;
int do_eliminate_check_bcd = 0;
int do_eliminate_load_carry = 0;
/* The transforms below will crash if we've written the opcode to be an
* interp or inturbo bail.
*/
if (p_opcode->ends_block) {
continue;
}
if (p_opcode->is_dynamic_operand) {
continue;
}
switch (p_opcode->optype_6502) {
case k_adc:
if ((p_opcode->flag_decimal == 0) || had_check_bcd) {
do_eliminate_check_bcd = 1;
}
if (p_opcode->flag_carry == 0) {
p_uop = jit_opcode_find_uop(p_opcode, &index, k_opcode_ADC);
assert(p_uop != NULL);
asm_make_uop0(p_uop, k_opcode_ADD);
do_eliminate_load_carry = 1;
}
had_check_bcd = 1;
break;
case k_dex:
if (p_opcode->reg_x == k_value_unknown) {
break;
}
load_uopcode_old = k_opcode_DEX;
load_uopcode_new = k_opcode_LDX;
load_uopcode_value = (p_opcode->reg_x - 1);
break;
case k_dey:
if (p_opcode->reg_y == k_value_unknown) {
break;
}
load_uopcode_old = k_opcode_DEY;
load_uopcode_new = k_opcode_LDY;
load_uopcode_value = (p_opcode->reg_y - 1);
break;
case k_inx:
if (p_opcode->reg_x == k_value_unknown) {
break;
}
load_uopcode_old = k_opcode_INX;
load_uopcode_new = k_opcode_LDX;
load_uopcode_value = (p_opcode->reg_x + 1);
break;
case k_iny:
if (p_opcode->reg_y == k_value_unknown) {
break;
}
load_uopcode_old = k_opcode_INY;
load_uopcode_new = k_opcode_LDY;
load_uopcode_value = (p_opcode->reg_y + 1);
break;
case k_sbc:
if ((p_opcode->flag_decimal == 0) || had_check_bcd) {
do_eliminate_check_bcd = 1;
}
if (p_opcode->flag_carry == 1) {
p_uop = jit_opcode_find_uop(p_opcode, &index, k_opcode_SBC);
assert(p_uop != NULL);
asm_make_uop0(p_uop, k_opcode_SUB);
do_eliminate_load_carry = 1;
}
had_check_bcd = 1;
break;
case k_sta:
if (p_opcode->reg_a == k_value_unknown) {
break;
}
if ((p_opcode->opmode_6502 != k_zpg) &&
(p_opcode->opmode_6502 != k_abs)) {
break;
}
if (!asm_jit_supports_uopcode(k_opcode_ST_IMM)) {
break;
}
p_uop = jit_opcode_find_uop(p_opcode, &index, k_opcode_STA);
assert(p_uop != NULL);
asm_make_uop0(p_uop, k_opcode_ST_IMM);
p_uop->value2 = p_opcode->reg_a;
break;
case k_stx:
if (p_opcode->reg_x == k_value_unknown) {
break;
}
if ((p_opcode->opmode_6502 != k_zpg) &&
(p_opcode->opmode_6502 != k_abs)) {
break;
}
if (!asm_jit_supports_uopcode(k_opcode_ST_IMM)) {
break;
}
p_uop = jit_opcode_find_uop(p_opcode, &index, k_opcode_STX);
assert(p_uop != NULL);
asm_make_uop0(p_uop, k_opcode_ST_IMM);
p_uop->value2 = p_opcode->reg_x;
break;
case k_sty:
if (p_opcode->reg_y == k_value_unknown) {
break;
}
if ((p_opcode->opmode_6502 != k_zpg) &&
(p_opcode->opmode_6502 != k_abs)) {
break;
}
if (!asm_jit_supports_uopcode(k_opcode_ST_IMM)) {
break;
}
p_uop = jit_opcode_find_uop(p_opcode, &index, k_opcode_STY);
assert(p_uop != NULL);
asm_make_uop0(p_uop, k_opcode_ST_IMM);
p_uop->value2 = p_opcode->reg_y;
break;
case k_tax:
if (p_opcode->reg_a == k_value_unknown) {
break;
}
load_uopcode_old = k_opcode_TAX;
load_uopcode_new = k_opcode_LDX;
load_uopcode_value = p_opcode->reg_a;
break;
case k_tay:
if (p_opcode->reg_a == k_value_unknown) {
break;
}
load_uopcode_old = k_opcode_TAY;
load_uopcode_new = k_opcode_LDY;
load_uopcode_value = p_opcode->reg_a;
break;
case k_txa:
if (p_opcode->reg_x == k_value_unknown) {
break;
}
load_uopcode_old = k_opcode_TXA;
load_uopcode_new = k_opcode_LDA;
load_uopcode_value = p_opcode->reg_x;
break;
case k_tya:
if (p_opcode->reg_y == k_value_unknown) {
break;
}
load_uopcode_old = k_opcode_TYA;
load_uopcode_new = k_opcode_LDA;
load_uopcode_value = p_opcode->reg_y;
break;
default:
break;
}
if ((p_opcode->opmode_6502 == k_idy) &&
(p_opcode->reg_y != k_value_unknown)) {
uint8_t reg_y = p_opcode->reg_y;
p_uop = jit_opcode_find_uop(p_opcode, &index, k_opcode_addr_add_base_y);
assert(p_uop != NULL);
p_uop->uopcode = k_opcode_addr_add_base_constant;
p_uop->value1 = reg_y;
p_uop = jit_opcode_find_uop(p_opcode,
&index,
k_opcode_check_page_crossing_y);
if (p_uop != NULL) {
p_uop->uopcode = k_opcode_check_page_crossing_n;
p_uop->value1 = reg_y;
if (reg_y == 0) {
/* Y is known to be zero, so skip the page crossing check entirely. */
p_uop->is_eliminated = 1;
p_opcode->max_cycles = 5;
}
}
}
if (do_eliminate_check_bcd) {
p_uop = jit_opcode_find_uop(p_opcode, &index, k_opcode_check_bcd);
assert(p_uop != NULL);
p_uop->is_eliminated = 1;
}
if (do_eliminate_load_carry) {
p_uop = jit_opcode_find_uop(p_opcode, &index, k_opcode_load_carry);
assert(p_uop != NULL);
p_uop->is_eliminated = 1;
}
if (load_uopcode_old != -1) {
p_uop = jit_opcode_find_uop(p_opcode, &index, load_uopcode_old);
assert(p_uop != NULL);
asm_make_uop1(p_uop, k_opcode_value_set, load_uopcode_value);
p_uop = jit_opcode_insert_uop(p_opcode, (index + 1));
asm_make_uop0(p_uop, load_uopcode_new);
}
}
}
static void
jit_optimizer_eliminate_mode_loads(struct jit_opcode_details* p_opcodes) {
struct jit_opcode_details* p_opcode;
int32_t curr_base_addr_index = -1;
for (p_opcode = p_opcodes;
p_opcode->addr_6502 != -1;
p_opcode += p_opcode->num_bytes_6502) {
uint32_t i_uops;
uint32_t num_uops = p_opcode->num_uops;
struct asm_uop* p_addr_set_uop = NULL;
struct asm_uop* p_addr_add_uop = NULL;
struct asm_uop* p_addr_load_uop = NULL;
int is_write = 0;
int is_simple_addr = 1;
int is_changing_addr_reg = 0;
int is_tricky_opcode = 0;
if (p_opcode->ends_block) {
continue;
}
if (p_opcode->is_eliminated) {
continue;
}
for (i_uops = 0; i_uops < num_uops; ++i_uops) {
struct asm_uop* p_uop = &p_opcode->uops[i_uops];
int32_t uopcode = p_uop->uopcode;
switch (uopcode) {
case k_opcode_addr_set:
p_addr_set_uop = p_uop;
break;
case k_opcode_addr_add_x_8bit:
p_addr_add_uop = p_uop;
break;
case k_opcode_addr_base_load_16bit_wrap:
p_addr_load_uop = p_uop;
break;
case k_opcode_PHP:
case k_opcode_PLP:
is_tricky_opcode = 1;
break;
default:
break;
}
if ((uopcode > k_opcode_addr_begin) && (uopcode < k_opcode_addr_end)) {
if (uopcode != k_opcode_addr_set) {
is_simple_addr = 0;
}
if (!p_uop->is_eliminated) {
is_changing_addr_reg = 1;
}
}
}
if ((p_addr_load_uop != NULL) && (p_addr_add_uop == NULL)) {
/* It's mode IDY. */
int32_t this_base_addr_index;
assert(p_addr_set_uop != NULL);
this_base_addr_index = p_addr_set_uop->value1;
if (this_base_addr_index == curr_base_addr_index) {
p_addr_set_uop->is_eliminated = 1;
p_addr_load_uop->is_eliminated = 1;
}
curr_base_addr_index = this_base_addr_index;
} else if (is_changing_addr_reg) {
/* Changing the address register invalidates the optimization. */
curr_base_addr_index = -1;
}
/* Writes to where the base address is stored invaldates the cached base
* address.
*/
is_write = !!(p_opcode->opmem_6502 & k_opmem_write_flag);
if (is_write && (curr_base_addr_index != -1)) {
uint16_t simple_addr = p_addr_set_uop->value1;
uint16_t curr_base_addr_index_next = (uint8_t) (curr_base_addr_index + 1);
if (is_simple_addr &&
(curr_base_addr_index != simple_addr) &&
(curr_base_addr_index_next != simple_addr)) {
/* This write doesn't affect the cached base address. */
} else {
curr_base_addr_index = -1;
}
}
/* This is hacky, but for now don't carry the optimization across a couple
* of "tricky" opcodes where the backends might re-use the cached address
* register as a scratch space.
*/
if (is_tricky_opcode) {
curr_base_addr_index = -1;
}
}
}
static void
jit_optimizer_eliminate_nz_flag_saving(struct jit_opcode_details* p_opcodes) {
struct jit_opcode_details* p_opcode;
struct asm_uop* p_nz_flags_uop = NULL;
uint16_t nz_mem_addr = 0;
for (p_opcode = p_opcodes;
p_opcode->addr_6502 != -1;
p_opcode += p_opcode->num_bytes_6502) {
uint32_t num_uops = p_opcode->num_uops;
uint32_t i_uops;
if (p_opcode->ends_block) {
continue;
}
if (p_opcode->is_eliminated) {
continue;
}
if (p_nz_flags_uop != NULL) {
if (p_nz_flags_uop->uopcode == k_opcode_flags_nz_mem) {
p_opcode->nz_flags_location = nz_mem_addr;
} else {
p_opcode->nz_flags_location = -p_nz_flags_uop->uopcode;
}
}
/* PHP needs the NZ flags. */
if (p_opcode->optype_6502 == k_php) {
p_nz_flags_uop = NULL;
}
/* Any jump, including conditional, must commit flags. */
if (p_opcode->opbranch_6502 != k_bra_n) {
p_nz_flags_uop = NULL;
}
/* A write might invalidate flag state stored in memory. */
if ((p_nz_flags_uop != NULL) &&
(p_nz_flags_uop->uopcode == k_opcode_flags_nz_mem) &&
jit_opcode_can_write_to_addr(p_opcode, nz_mem_addr)) {
p_nz_flags_uop = NULL;
}
for (i_uops = 0; i_uops < num_uops; ++i_uops) {
struct asm_uop* p_uop = &p_opcode->uops[i_uops];
int32_t nz_flags_uopcode = 0;
switch (p_uop->uopcode) {
case k_opcode_flags_nz_a:
case k_opcode_flags_nz_x:
case k_opcode_flags_nz_y:
case k_opcode_flags_nz_value:
case k_opcode_flags_nz_mem:
nz_flags_uopcode = p_uop->uopcode;
break;
default:
break;
}
if (nz_flags_uopcode == 0) {
continue;
}
/* Eliminate the previous flag set, if appropriate. */
if (p_nz_flags_uop != NULL) {
p_nz_flags_uop->is_eliminated = 1;
}
if (p_uop->is_merged) {
/* The x64 rewriter will have merge eliminated a lot of NZ flag
* writes.
*/
assert(p_uop->is_eliminated);
p_nz_flags_uop = NULL;
} else {
p_nz_flags_uop = p_uop;
if (nz_flags_uopcode == k_opcode_flags_nz_mem) {
nz_mem_addr = p_uop->value1;
}
}
}
}
}
static void
jit_optimizer_eliminate_c_v_flag_saving(struct jit_opcode_details* p_opcodes) {
struct jit_opcode_details* p_opcode;
struct asm_uop* p_save_carry_uop = NULL;
struct asm_uop* p_save_overflow_uop = NULL;
for (p_opcode = p_opcodes;
p_opcode->addr_6502 != -1;
p_opcode += p_opcode->num_bytes_6502) {
uint32_t num_uops = p_opcode->num_uops;
uint32_t i_uops;
int had_save_carry = 0;
int had_save_overflow = 0;
int32_t index;
if (p_opcode->ends_block) {
continue;
}
if (p_opcode->is_eliminated) {
continue;
}
if (p_save_carry_uop != NULL) {
p_opcode->c_flag_location = p_save_carry_uop->uopcode;
}
if (p_save_overflow_uop != NULL) {
p_opcode->v_flag_location = p_save_overflow_uop->uopcode;
}
/* If this is a carry-related branch, we might be able to collapse out the
* carry load, if we've tracking a carry save.
*/
if ((p_opcode->optype_6502 == k_bcc) || (p_opcode->optype_6502 == k_bcs)) {
if (p_save_carry_uop != NULL) {
if ((p_save_carry_uop->uopcode == k_opcode_save_carry) ||
(p_save_carry_uop->uopcode == k_opcode_save_carry_inverted)) {
int is_inversion =
(p_save_carry_uop->uopcode == k_opcode_save_carry_inverted);
struct asm_uop* p_load_carry_uop =
jit_opcode_find_uop(p_opcode, &index, k_opcode_load_carry);
p_load_carry_uop->is_eliminated = 1;
/* If it's an inversion, flip BCC <-> BCS. */
if (is_inversion) {
if (p_opcode->optype_6502 == k_bcc) {
struct asm_uop* p_branch_uop =
jit_opcode_find_uop(p_opcode, &index, k_opcode_BCC);
p_branch_uop->uopcode = k_opcode_BCS;
} else {
struct asm_uop* p_branch_uop =
jit_opcode_find_uop(p_opcode, &index, k_opcode_BCS);
p_branch_uop->uopcode = k_opcode_BCC;
}
}
}
}
}
/* Any jump, including conditional, must commit flags. */
if (p_opcode->opbranch_6502 != k_bra_n) {
p_save_carry_uop = NULL;
p_save_overflow_uop = NULL;
}
for (i_uops = 0; i_uops < num_uops; ++i_uops) {
struct asm_uop* p_uop = &p_opcode->uops[i_uops];
switch (p_uop->uopcode) {
case k_opcode_load_carry:
case k_opcode_load_carry_inverted:
/* The carry load will be eliminated if this was made an ADD / SUB, or
* by the ARM64 backend.
*/
if (p_uop->is_eliminated && !p_uop->is_merged) {
break;
}
/* Eliminate the store and the load together if we've got a pair. */
if (!p_uop->is_merged &&
(p_save_carry_uop != NULL) &&
((p_save_carry_uop->uopcode == k_opcode_save_carry) ||
(p_save_carry_uop->uopcode == k_opcode_save_carry_inverted))) {
int do_invert =
(p_save_carry_uop->uopcode == k_opcode_save_carry_inverted);
if (p_uop->uopcode == k_opcode_load_carry_inverted) {
do_invert ^= 1;
}
p_save_carry_uop->is_eliminated = 1;
if (do_invert) {
p_uop->uopcode = k_opcode_carry_invert;
} else {
p_uop->is_eliminated = 1;
}
}
p_save_carry_uop = NULL;
break;
case k_opcode_save_carry:
case k_opcode_save_carry_inverted:
had_save_carry = 1;
/* FALL THROUGH */
case k_opcode_CLC:
case k_opcode_SEC:
if (p_save_carry_uop != NULL) {
p_save_carry_uop->is_eliminated = 1;
}
if (p_uop->is_merged) {
assert(p_uop->is_eliminated);
p_save_carry_uop = NULL;
} else {
p_save_carry_uop = p_uop;
}
break;
case k_opcode_load_overflow:
p_save_overflow_uop = NULL;
break;
case k_opcode_save_overflow:
if (p_save_overflow_uop != NULL) {
p_save_overflow_uop->is_eliminated = 1;
}
p_save_overflow_uop = p_uop;
had_save_overflow = 1;
break;
case k_opcode_flags_nz_a:
case k_opcode_flags_nz_x:
case k_opcode_flags_nz_y:
case k_opcode_flags_nz_value:
case k_opcode_flags_nz_mem:
/* This might be best abstracted into the asm backends somehow, but for
* now, let's observe that both the x64 and ARM64 backends cannot
* preserve the host carry / overflow flags across a "test" instruction.
*/
if (!p_uop->is_eliminated) {
p_save_carry_uop = NULL;
p_save_overflow_uop = NULL;
}
/* The NZ flag set might be part of a host instruction, in which case
* it's also typical to trash host carry / overflow flags, unless the
* carry / overflow is being set at the same time.
*/
if (p_uop->is_merged) {
if (!had_save_carry) {
p_save_carry_uop = NULL;
}
if (!had_save_overflow) {
p_save_overflow_uop = NULL;
}
}
break;
case k_opcode_CLD:
case k_opcode_CLI:
case k_opcode_SED:
case k_opcode_SEI:
case k_opcode_BIT:
/* TODO: the Intel x64 backend trashes the host carry / overflow on
* these and it probably shouldn't.
*/
p_save_carry_uop = NULL;
p_save_overflow_uop = NULL;
break;
default:
break;
}
}
}
}
static void
jit_optimizer_eliminate_axy_loads(struct jit_opcode_details* p_opcodes) {
struct jit_opcode_details* p_opcode;
struct asm_uop* p_load_a_uop = NULL;
struct asm_uop* p_load_x_uop = NULL;
struct asm_uop* p_load_y_uop = NULL;
for (p_opcode = p_opcodes;
p_opcode->addr_6502 != -1;
p_opcode += p_opcode->num_bytes_6502) {
uint32_t i_uops;
uint32_t num_uops = p_opcode->num_uops;
int32_t imm_value = -1;
struct asm_uop* p_load_uop = NULL;
struct asm_uop* p_load_flags_uop = NULL;
int is_self_modify_invalidated = p_opcode->self_modify_invalidated;
if (p_opcode->ends_block) {
continue;
}
if (p_opcode->is_eliminated) {
continue;
}
/* Any jump, including conditional, must commit register values. */
if (p_opcode->opbranch_6502 != k_bra_n) {
p_load_a_uop = NULL;
p_load_x_uop = NULL;
p_load_y_uop = NULL;
}
for (i_uops = 0; i_uops < num_uops; ++i_uops) {
struct asm_uop* p_uop = &p_opcode->uops[i_uops];
switch (p_uop->uopcode) {
case k_opcode_value_set:
imm_value = p_uop->value1;
break;
case k_opcode_LDA:
if (p_load_a_uop != NULL) {
p_load_a_uop->is_eliminated = 1;
}
if (imm_value >= 0) {
p_load_a_uop = p_uop;
} else {
p_load_a_uop = NULL;
}
p_load_uop = p_uop;
break;
case k_opcode_LDX:
if (p_load_x_uop != NULL) {
p_load_x_uop->is_eliminated = 1;
}
if (imm_value >= 0) {
p_load_x_uop = p_uop;
} else {
p_load_x_uop = NULL;
}
p_load_uop = p_uop;
break;
case k_opcode_LDY:
if (p_load_y_uop != NULL) {
p_load_y_uop->is_eliminated = 1;
}
if (imm_value >= 0) {
p_load_y_uop = p_uop;
} else {
p_load_y_uop = NULL;
}
p_load_uop = p_uop;
break;
case k_opcode_flags_nz_a:
p_load_flags_uop = p_uop;
/* FALL THROUGH */
case k_opcode_ADC:
case k_opcode_ADD:
case k_opcode_ALR:
case k_opcode_AND:
case k_opcode_ASL_acc:
case k_opcode_BIT:
case k_opcode_CMP:
case k_opcode_EOR:
case k_opcode_LSR_acc:
case k_opcode_ORA:
case k_opcode_PHA:
case k_opcode_ROL_acc:
case k_opcode_ROR_acc:
case k_opcode_SBC:
case k_opcode_SLO:
case k_opcode_STA:
case k_opcode_SUB:
case k_opcode_TAX:
case k_opcode_TAY:
if (!p_uop->is_eliminated || p_uop->is_merged) {
p_load_a_uop = NULL;
}
break;
case k_opcode_flags_nz_x:
p_load_flags_uop = p_uop;
/* FALL THROUGH */
case k_opcode_check_page_crossing_x:
case k_opcode_addr_add_x:
case k_opcode_addr_add_x_8bit:
case k_opcode_CPX:
case k_opcode_DEX:
case k_opcode_INX:
case k_opcode_STX:
case k_opcode_TXS:
case k_opcode_TXA:
if (!p_uop->is_eliminated || p_uop->is_merged) {
p_load_x_uop = NULL;
}
break;
case k_opcode_flags_nz_y:
p_load_flags_uop = p_uop;
/* FALL THROUGH */
case k_opcode_check_page_crossing_y:
case k_opcode_addr_add_y:
case k_opcode_addr_add_y_8bit:
case k_opcode_CPY:
case k_opcode_DEY:
case k_opcode_INY:
case k_opcode_STY:
case k_opcode_TYA:
if (!p_uop->is_eliminated || p_uop->is_merged) {
p_load_y_uop = NULL;
}
break;
case k_opcode_flags_nz_value:
case k_opcode_flags_nz_mem:
p_load_flags_uop = p_uop;
break;
default:
if (p_uop->uopcode == k_opcode_SAX) {
if (!p_uop->is_eliminated || p_uop->is_merged) {
p_load_a_uop = NULL;
p_load_x_uop = NULL;
}
}
break;
}
}
/*if ((p_countdown_uop != NULL) && (p_load_flags_uop != NULL)) {*/
/* If the opcode immediately following a countdown check sets the NZ
* flags, then we can safely use a faster countdown check that clobbers
* the NZ flags.
* Even if the countdown check fires, and an IRQ is raised, the IRQ won't
* trigger until after the opcode immediately following the countdown
* check.
*/
/* Disabled: buggy, hit by Exile.
* Optimization is worthwhile, and can likely be re-enabled but this
* will require some work.
* p_countdown_uop->uopcode = k_opcode_countdown_no_preserve_nz_flags;
*/
/*}*/
/* This is subtle, but if we're in the middle of resolving self-modification
* on a merged (or merged into) opcode, don't eliminate the merged opcode.
* This will enable the self-modification detector to emit a dynamic
* operand for the correct opcode. After this, the resulting recompile
* will still retain the elimination optimization if appropriate.
*/
if (is_self_modify_invalidated) {
p_load_a_uop = NULL;
p_load_x_uop = NULL;
p_load_y_uop = NULL;
}
/* Replace loads of immediate #0 + flags setting with a single uopcode. */
if (p_load_uop != NULL) {
assert(p_load_flags_uop != NULL);
if ((imm_value == 0) && !p_load_flags_uop->is_eliminated) {
switch (p_load_uop->uopcode) {
case k_opcode_LDA:
p_load_uop->uopcode = k_opcode_LDA_zero_and_flags;
break;
case k_opcode_LDX:
p_load_uop->uopcode = k_opcode_LDX_zero_and_flags;
break;
case k_opcode_LDY:
p_load_uop->uopcode = k_opcode_LDY_zero_and_flags;
break;
default:
assert(0);
break;
}
p_load_uop->backend_tag = 0;
/* This whole shebang might already be eliminated, but no harm in doing
* part of it again if that's the case.
*/
p_load_flags_uop->is_eliminated = 1;
p_load_flags_uop->is_merged = 1;
}
}
}
}
static void
jit_optimizer_merge_countdowns(struct jit_opcode_details* p_opcodes) {
struct jit_opcode_details* p_opcode;
struct asm_uop* p_add_cycles_uop = NULL;
for (p_opcode = p_opcodes;
p_opcode->addr_6502 != -1;
p_opcode += p_opcode->num_bytes_6502) {
uint32_t i_uops;
uint32_t num_uops = p_opcode->num_uops;
struct asm_uop* p_countdown_uop = NULL;
if (p_opcode->is_eliminated) {
continue;
}
for (i_uops = 0; i_uops < num_uops; ++i_uops) {
struct asm_uop* p_uop = &p_opcode->uops[i_uops];
switch (p_uop->uopcode) {
case k_opcode_add_cycles:
p_add_cycles_uop = p_uop;
break;
case k_opcode_countdown:
/* Merge any branch-not-taken countdown fixup into the countdown check
* subtraction itself.
*/
assert(p_opcode->cycles_run_start >= 0);
p_countdown_uop = p_uop;
if (p_add_cycles_uop != NULL) {
if (p_countdown_uop->value2 >= p_add_cycles_uop->value1) {
p_countdown_uop->value2 -= p_add_cycles_uop->value1;
p_add_cycles_uop->is_eliminated = 1;
p_add_cycles_uop->is_merged = 1;
}
}
break;
default:
break;
}
}
}
}
struct jit_opcode_details*
jit_optimizer_optimize_pre_rewrite(struct jit_opcode_details* p_opcodes) {
/* Pass 1: opcode merging. LSR A and similar opcodes. */
jit_optimizer_merge_opcodes(p_opcodes);
/* Pass 2: tag opcodes with any known register and flag values. */
jit_optimizer_calculate_known_values(p_opcodes);
/* Pass 3: replacements of uops with better ones if known state offers the
* opportunity.
* 1) Classic example is CLC; ADC. At the ADC instruction, it is known that
* CF==0 so the ADC can become just an ADD.
* 2) We rewrite e.g. INY to be LDY #n if the value of Y is statically known.
* This is common for unrolled loops.
* 3) We rewrite e.g. LDA ($3A),Y to make the "Y" addition in the address
* calculation constant, if Y is statically known. This is common for
* unrolled loops.
*/
jit_optimizer_replace_uops(p_opcodes);
return NULL;
}
void
jit_optimizer_optimize_post_rewrite(struct jit_opcode_details* p_opcodes) {
/* Pass 1: NZ flag saving elimination. */
jit_optimizer_eliminate_nz_flag_saving(p_opcodes);
/* Pass 2: carry and overflow flag saving elimination. */
jit_optimizer_eliminate_c_v_flag_saving(p_opcodes);
/* Pass 3: eliminate redundant register sets. This comes alive after previous