forked from freewilll/wcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstrsel.c
More file actions
1579 lines (1283 loc) · 57.4 KB
/
instrsel.c
File metadata and controls
1579 lines (1283 loc) · 57.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wcc.h"
enum {
MAX_INSTRUCTION_GRAPH_EDGE_COUNT = 128,
MAX_INSTRUCTION_GRAPH_CHOICE_NODE_COUNT = 1024,
MAX_INSTRUCTION_GRAPH_CHOICE_EDGE_COUNT = 1024,
MAX_CHOICE_TRAIL_COUNT = 32,
MAX_SAVED_REGISTERS = 8,
};
IGraph *igraphs; // The current block's igraphs
int instr_count; // The current block's instruction count
Graph *cost_graph; // Graph of all possible options when tiling
int cost_graph_node_count; // Number of nodes in cost graph
int *cost_to_igraph_map; // Mapping of a cost graph node back to the instruction graph node
int *cost_rules; // Mapping of cost graph node id to x86 instruction rule id
int *accumulated_cost; // Total cost of sub tree of a cost graph node
LongSet **igraph_labels; // Matched instruction rule ids for a igraph node id
Rule **igraph_rules; // Matched lowest cost rule id for a igraph node id
List *allocated_graphs; // Track Graph allocations
List *allocated_igraphs; // Track IGraph allocations
List *allocated_igraph_nodes; // Track IGraphNode allocations
List *allocated_things; // Track anything that can be freed with a simple wfree()
static int recursive_tile_igraphs(IGraph *igraph, int node_id);
void init_instrsel() {
allocated_things = new_list(1024);
}
void free_instrsel() {
for (int i = 0; i < allocated_things->length; i++)
wfree(allocated_things->elements[i]);
free_list(allocated_things);
}
static void transform_lvalues(Function *function) {
for (Tac *tac = function->ir; tac; tac = tac->next) {
if (tac->operation == IR_MOVE_TO_PTR) {
tac->src1 = dup_value(tac->src1);
tac->src1->type = make_pointer(tac->src1->type);
tac->src1->is_lvalue = 0;
// Note: tac ->dst remains zero. src1 is the target of the pointer write, but is itself not modified
}
else {
// Ensure type of dst and src1 matches in a pointer addition operation
if (tac->operation == IR_ADD && tac->dst && tac->dst->is_lvalue_in_register) {
tac->dst = dup_value(tac->dst);
tac->dst->type = make_pointer(tac->dst->type);
tac->dst->is_lvalue = 0;
}
if (tac->dst && tac->dst->vreg && tac->dst->is_lvalue && !tac->dst->is_lvalue_in_register) {
tac->dst->type = make_pointer(tac->dst->type);
tac->dst->is_lvalue = 0;
}
if (tac->src1 && tac->src1->vreg && tac->src1->is_lvalue) {
tac->src1->type = make_pointer(tac->src1->type);
tac->src1->is_lvalue = 0;
}
if (tac->src2 && tac->src2->vreg && tac->src2->is_lvalue) {
tac->src2->type = make_pointer(tac->src2->type);
tac->src2->is_lvalue = 0;
}
}
}
}
static void recursive_dump_igraph(IGraph *ig, int node, int indent, int include_rules) {
int c = indent * 2;
IGraphNode *ign = &(ig->nodes[node]);
printf("%3d ", node);
for (int i = 0; i < indent; i++) printf(" ");
if (ign->tac) {
int operation = ign->tac->operation;
switch (operation) {
case IR_MOVE: c += printf("="); break;
case IR_MOVE_PREG_CLASS: c += printf("move to preg class"); break;
case IR_MOVE_STACK_PTR: c += printf("stack ptr to"); break;
case IR_DECL_LOCAL_COMP_OBJ: c += printf("declare"); break;
case IR_LOAD_BIT_FIELD: c += printf("load bit field"); break;
case IR_SAVE_BIT_FIELD: c += printf("save bit field"); break;
case IR_LOAD_FROM_GOT: c += printf("load from GOT"); break;
case IR_ADDRESS_OF_FROM_GOT: c += printf("& from GOT"); break;
case IR_ADD: c += printf("+"); break;
case IR_SUB: c += printf("-"); break;
case IR_MUL: c += printf("*"); break;
case IR_DIV: c += printf("/"); break;
case IR_BSHL: c += printf("<<"); break;
case IR_BSHR: c += printf(">>"); break;
case IR_ASHR: c += printf("a>>"); break;
case IR_BNOT: c += printf("!"); break;
case IR_BOR: c += printf("|"); break;
case IR_BAND: c += printf("&"); break;
case IR_XOR: c += printf("~"); break;
case IR_INDIRECT: c += printf("indirect"); break;
case IR_ADDRESS_OF: c += printf("&"); break;
case IR_MOVE_TO_PTR: c += printf("move to ptr"); break;
case IR_NOP: c += printf("noop"); break;
case IR_RETURN: c += printf("return"); break;
case IR_LOAD_LONG_DOUBLE: c += printf("load long double"); break;
case IR_START_CALL: c += printf("start call"); break;
case IR_END_CALL: c += printf("end call"); break;
case IR_ARG: c += printf("arg"); break;
case IR_ARG_STACK_PADDING: c += printf("arg stack padding"); break;
case IR_CALL: c += printf("call"); break;
case IR_CALL_ARG_REG: c += printf("call arg reg"); break;
case IR_START_LOOP: c += printf("start loop"); break;
case IR_END_LOOP: c += printf("end loop"); break;
case IR_ALLOCATE_STACK: c += printf("allocate stack"); break;
case IR_JZ: c += printf("jz"); break;
case IR_JNZ: c += printf("jnz"); break;
case IR_JMP: c += printf("jmp"); break;
case IR_EQ: c += printf("=="); break;
case IR_NE: c += printf("!="); break;
case IR_LT: c += printf("<"); break;
case IR_GT: c += printf(">"); break;
case IR_LE: c += printf(">="); break;
case IR_GE: c += printf("<="); break;
default: c += printf("Operation %d", operation);
}
if (ign->tac->dst) {
c += printf(" -> ");
c += print_value(stdout, ign->tac->dst, 0);
}
}
else
c += print_value(stdout, ign->value, 0);
if (include_rules && igraph_rules[node]) {
if (igraph_rules[node]) {
for (int i = 0; i < 60 - c; i++) printf(" ");
print_rule(igraph_rules[node], 0, 0);
}
}
else
printf("\n");
GraphEdge *e = ig->graph->nodes[node].succ;
while (e) {
recursive_dump_igraph(ig, e->to->id, indent + 1, include_rules);
e = e->next_succ;
}
}
static void dump_igraph(IGraph *ig, int include_rules) {
if (ig->node_count == 0) printf("Empty igraph\n");
recursive_dump_igraph(ig, 0, 0, include_rules);
}
static void dup_inode(IGraphNode *src, IGraphNode *dst) {
dst->tac = src->tac;
dst->value = src->value;
}
static IGraph *shallow_dup_igraph(IGraph *src, IGraph *dst) {
dst->nodes = src->nodes;
dst->graph = src->graph;
dst->node_count = src->node_count;
}
// Merge g2 into g1. The merge point is vreg
static IGraph *merge_igraphs(IGraph *g1, IGraph *g2, int vreg) {
if (debug_instsel_tree_merging) {
printf("g1 dst=%d\n", g1->nodes[0].tac->dst ? g1->nodes[0].tac->dst->vreg : -1);
dump_igraph(g1, 0);
printf("g2 dst=%d\n", g2->nodes[0].tac->dst ? g2->nodes[0].tac->dst->vreg : -1);
dump_igraph(g2, 0);
printf("\n");
}
IGraph *g = wcalloc(1, sizeof(IGraph));
append_to_list(allocated_igraphs, g);
int node_count = g1->node_count + g2->node_count;
IGraphNode *inodes = wcalloc(node_count, sizeof(IGraphNode));
append_to_list(allocated_igraph_nodes, inodes);
g->nodes = inodes;
g->graph = new_graph(node_count, MAX_INSTRUCTION_GRAPH_EDGE_COUNT);
append_to_list(allocated_graphs, g->graph);
g->node_count = node_count;
int join_from = -1;
int join_to = -1;
IGraphNode *in1;
if (g1->node_count == 0) panic("Unexpectedly got 0 g1->node_count");
if (debug_instsel_tree_merging_deep) printf("g1->node_count=%d\n", g1->node_count);
if (debug_instsel_tree_merging_deep) printf("g2->node_count=%d\n", g2->node_count);
for (int i = 0; i < g1->node_count; i++) {
if (debug_instsel_tree_merging_deep) printf("Copying g1 %d to %d\n", i, i);
dup_inode(&(g1->nodes[i]), &(inodes[i]));
IGraphNode *g1_inodes = g1->nodes;
GraphNode *n = &(g1->graph->nodes[i]);
GraphEdge *e = n->succ;
while (e) {
IGraphNode *in = &(g1_inodes[e->to->id]);
if (in->value && in->value->vreg == vreg) {
in1 = in;
join_from = e->from->id;
join_to = e->to->id;
if (debug_instsel_tree_merging_deep) printf("Adding join edge %d -> %d\n", join_from, join_to);
add_graph_edge(g->graph, join_from, join_to);
}
else {
if (debug_instsel_tree_merging_deep) printf("Adding g1 edge %d -> %d\n", e->from->id, e->to->id);
add_graph_edge(g->graph, e->from->id, e->to->id);
}
e = e->next_succ;
}
}
if (join_from == -1 || join_to == -1) panic("Attempt to join two trees without a join node");
// The g2 graph starts at g1->node_count
for (int i = 0; i < g2->node_count; i++) {
int d = g1->node_count + i;
if (debug_instsel_tree_merging_deep) printf("Copying g2 node from %d to %d\n", i, d);
dup_inode(&(g2->nodes[i]), &(inodes[d]));
GraphNode *n2 = &(g2->graph->nodes[i]);
GraphEdge *e = n2->succ;
while (e) {
int from = e->from->id + g1->node_count ;
int to = e->to->id + g1->node_count;
if (debug_instsel_tree_merging_deep) printf("Adding g2 edge %d -> %d\n", from, to);
add_graph_edge(g->graph, from, to);
e = e->next_succ;
}
}
if (debug_instsel_tree_merging_deep) printf("\n");
IGraphNode *in2 = &(g2->nodes[0]);
// The coalesce live ranges code already removed any possible IR_MOVE, including
// coercions. This means that any type change left must be made explicit with an
// IR_MOVE, so that suitable instruction selection rules are matched, leading to
// possible code generation.
if (!type_eq(in1->value->type, in2->tac->dst->type)) {
if (debug_instsel_tree_merging) {
printf("Replacing %d with IR_MOVE tac for a type change from ", join_to);
print_type(stdout, in2->tac->dst->type);
printf(" -> ");
print_type(stdout, in1->value->type);
printf("\n");
}
IGraphNode *in = &(inodes[join_to]);
in->tac = new_instruction(IR_MOVE);
in->tac->src1 = in2->tac->dst;
in->tac->dst = in1->value;
if (debug_instsel_tree_merging) print_instruction(stdout, in->tac, 0);
if (debug_instsel_tree_merging) printf("Adding inter graph join edge %d -> %d\n", join_to, g1->node_count);
add_graph_edge(g->graph, join_to, g1->node_count);
}
else {
if (debug_instsel_tree_merging_deep) printf("Repointing inter graph join node %d->%d to %d->%d\n", join_from, join_to, join_from, g1->node_count);
GraphEdge *e = g->graph->nodes[join_from].succ;
if (e->to->id != join_to) e = e->next_succ;
e->to = &(g->graph->nodes[g1->node_count]);
}
if (debug_instsel_tree_merging) {
printf("\ng:\n");
dump_igraph(g, 0);
}
return g;
}
static int igraphs_are_neighbors(IGraph *igraphs, int i1, int i2) {
i1++;
if (i1 > i2) panic("Internal error: ordering issue in igraphs_are_neighbors()");
while (i1 <= i2) {
if (i1 == i2) return 1;
int is_nop = igraphs[i1].node_count == 1 && igraphs[i1].nodes[0].tac->operation == IR_NOP;
if (!is_nop && igraphs[i1].node_count != 0) return 0;
i1++;
}
return 1;
}
static void make_igraphs(Function *function, int block_id) {
allocated_graphs = new_list(1024);
allocated_igraphs = new_list(1024);
allocated_igraph_nodes = new_list(1024);
Block *blocks = function->blocks;
int vreg_count = 0;
instr_count = 0;
Tac *tac = blocks[block_id].start;
while (1) {
if (debug_instsel_tree_merging) print_instruction(stdout, tac, 0);
if (tac->src1 && tac->src1->vreg && tac->src1->vreg > vreg_count) vreg_count = tac->src1->vreg;
if (tac->src2 && tac->src2->vreg && tac->src2->vreg > vreg_count) vreg_count = tac->src2->vreg;
if (tac-> dst && tac->dst ->vreg && tac->dst ->vreg > vreg_count) vreg_count = tac->dst ->vreg;
instr_count++;
if (tac == blocks[block_id].end) break;
tac = tac->next;
}
// Allocate global igraphs
igraphs = wmalloc(instr_count * sizeof(IGraph));
int i = 0;
tac = blocks[block_id].start;
while (tac) {
int node_count = 1;
if (tac->src1) node_count++;
if (tac->src2) node_count++;
IGraphNode *nodes = wcalloc(node_count, sizeof(IGraphNode));
append_to_list(allocated_igraph_nodes, nodes);
Graph *graph = new_graph(node_count, MAX_INSTRUCTION_GRAPH_EDGE_COUNT);
append_to_list(allocated_graphs, graph);
nodes[0].tac = tac;
node_count = 1;
if (tac->src1) { nodes[node_count].value = tac->src1; add_graph_edge(graph, 0, node_count); node_count++; }
if (tac->src2) { nodes[node_count].value = tac->src2; add_graph_edge(graph, 0, node_count); node_count++; }
igraphs[i].graph = graph;
igraphs[i].nodes = nodes;
igraphs[i].node_count = node_count;
i++;
if (tac == blocks[block_id].end) break;
tac = tac->next;
}
// Mark liveouts as off-limits for merging
LongSet *liveout = function->liveout[block_id];
int liveout_vreg_count = 0;
for (LongSetIterator it = longset_iterator(liveout); !longset_iterator_finished(&it); longset_iterator_next(&it)) {
int vreg = longset_iterator_element(&it);
if (vreg > liveout_vreg_count) liveout_vreg_count = vreg;
}
if (liveout_vreg_count > vreg_count) vreg_count = liveout_vreg_count;
VregIGraph* vreg_igraphs = wcalloc(vreg_count + 1, sizeof(VregIGraph));
for (LongSetIterator it = longset_iterator(liveout); !longset_iterator_finished(&it); longset_iterator_next(&it)) {
int vreg = longset_iterator_element(&it);
vreg_igraphs[vreg].count++;
vreg_igraphs[vreg].igraph_id = -1;
}
i = instr_count - 1;
tac = blocks[block_id].end;
while (tac) {
int dst = 0;
int src1 = 0;
int src2 = 0;
if (tac->dst && tac->dst ->vreg) dst = tac->dst ->vreg;
if (tac->src1 && tac->src1->vreg) src1 = tac->src1->vreg;
if (tac->src2 && tac->src2->vreg) src2 = tac->src2->vreg;
if (src1) {
vreg_igraphs[src1].count++;
vreg_igraphs[src1].igraph_id = i;
}
if (src2) {
vreg_igraphs[src2].count++;
vreg_igraphs[src2].igraph_id = i;
}
if (tac->operation != IR_MOVE_TO_PTR && dst && ((src1 && dst == src1) || (src2 && dst == src2))) {
print_instruction(stdout, tac, 0);
panic("Illegal assignment of src1/src2 to dst");
}
if (src1 && src2 && src1 == src2) {
print_instruction(stdout, tac, 0);
panic("src1 == src2 not handled");
}
int g1_igraph_id = vreg_igraphs[dst].igraph_id;
// If dst is only used once and it's not in liveout, merge it.
// Also, don't merge IR_CALLs. The IR_START_CALL and IR_END_CALL constraints don't permit
// rearranging function calls without dire dowmstream side effects.
// IR_CALL_ARG_REG is also off limits, since it's a placeholder for function
// arg registers and no code is actually emitted.
if (vreg_igraphs[dst].count == 1 && vreg_igraphs[dst].igraph_id != -1 &&
tac->operation != IR_CALL && tac->operation != IR_MOVE_TO_PTR &&
igraphs[g1_igraph_id].nodes[0].tac->operation != IR_CALL_ARG_REG &&
tac->operation != IR_CALL_ARG_REG &&
igraphs_are_neighbors(igraphs, i, g1_igraph_id)
) {
// The instruction tiling code assumes dst != src1 and dst != src2..
// Ensure that if there is a dst of target graph that it doesn't match src1
IGraphNode *ign_g1 = &(igraphs[g1_igraph_id].nodes[0]);
int ign_vreg = ign_g1->tac ? (ign_g1->tac->dst ? ign_g1->tac->dst->vreg : 0) : 0;
if (ign_vreg && ign_vreg == src1) {
if (debug_instsel_tree_merging)
printf("Not merging on vreg=%d since the target dst=%d would match src1=%d", dst, ign_vreg, src1);
}
else {
if (debug_instsel_tree_merging) {
printf("\nMerging dst=%d src1=%d src2=%d ", dst, src1, src2);
printf("in locs %d and %d on vreg=%d\n----------------------------------------------------------\n", g1_igraph_id, i, dst);
}
IGraph *ig = merge_igraphs(&(igraphs[g1_igraph_id]), &(igraphs[i]), dst);
igraphs[g1_igraph_id].nodes = ig->nodes;
igraphs[g1_igraph_id].graph = ig->graph;
igraphs[g1_igraph_id].node_count = ig->node_count;
igraphs[i].node_count = 0;
if (src1) vreg_igraphs[src1].igraph_id = g1_igraph_id;
if (src2) vreg_igraphs[src2].igraph_id = g1_igraph_id;
}
}
if (dst && tac->operation != IR_MOVE_TO_PTR)
vreg_igraphs[dst].count = 0;
i--;
if (tac == blocks[block_id].start) break;
tac = tac->prev;
}
wfree(vreg_igraphs);
if (debug_instsel_tree_merging)
printf("\n=================================\n");
if (debug_instsel_tree_merging) {
for (int i = 0; i < instr_count; i++) {
if (!igraphs[i].node_count) continue;
tac = igraphs[i].nodes[0].tac;
if (tac && tac->operation == IR_NOP) continue;
if (tac) {
Value *v = igraphs[i].nodes[0].tac->dst;
if (v) {
print_value(stdout, v, 0);
printf(" = \n");
}
}
dump_igraph(&(igraphs[i]), 0);
}
}
}
static void free_igraphs(Function *function) {
for (int i = 0; i < allocated_graphs->length; i++)
free_graph(allocated_graphs->elements[i]);
free_list(allocated_graphs);
for (int i = 0; i < allocated_igraphs->length; i++)
wfree(allocated_igraphs->elements[i]);
free_list(allocated_igraphs);
for (int i = 0; i < allocated_igraph_nodes->length; i++)
wfree(allocated_igraph_nodes->elements[i]);
free_list(allocated_igraph_nodes);
wfree(igraphs); // Free global igraphs
}
// Recurse down src igraph, copying nodes to dst igraph and adding edges
static void recursive_simplify_igraph(IGraph *src, IGraph *dst, int src_node_id, int dst_parent_node_id, int *dst_child_node_id) {
IGraphNode *ign = &(src->nodes[src_node_id]);
GraphEdge *e = src->graph->nodes[src_node_id].succ;
Tac *tac = ign->tac;
// If src is not the root node, the operation is a move, and it's not a type change,
// recurse, with dst moved further down the tree
int operation;
if (tac) operation = tac->operation; else operation = 0;
if (operation == IR_MOVE && src_node_id != 0 && type_eq(tac->dst->type, tac->src1->type)) {
recursive_simplify_igraph(src, dst, e->to->id, dst_parent_node_id, dst_child_node_id);
return;
}
// Copy src node to dst
dup_inode(&(src->nodes[src_node_id]), &(dst->nodes[*dst_child_node_id]));
if (dst_parent_node_id != -1) {
// add an edge if it's not the root node
add_graph_edge(dst->graph, dst_parent_node_id, *dst_child_node_id);
}
dst_parent_node_id = *dst_child_node_id;
(*dst_child_node_id)++;
// Recurse down the children
while (e) {
recursive_simplify_igraph(src, dst, e->to->id, dst_parent_node_id, dst_child_node_id);
e = e->next_succ;
}
}
// Remove sequences of moves from the instruction graph. A new graph is created by
// recursing through the src.
static IGraph *simplify_igraph(IGraph *src) {
IGraph *dst = wcalloc(1, sizeof(IGraph));
append_to_list(allocated_igraphs, dst);
int node_count = src->node_count;
IGraphNode *inodes = wcalloc(node_count, sizeof(IGraphNode));
append_to_list(allocated_igraph_nodes, inodes);
dst->nodes = inodes;
dst->graph = new_graph(node_count, MAX_INSTRUCTION_GRAPH_EDGE_COUNT);
append_to_list(allocated_graphs, dst->graph);
dst->node_count = node_count;
if (debug_instsel_igraph_simplification) {
printf("simplify_igraph() on:\n");
dump_igraph(src, 0);
}
int dst_node_id = 0; // The root of the dst igraph
recursive_simplify_igraph(src, dst, 0, -1, &dst_node_id);
if (debug_instsel_igraph_simplification) {
printf("\n");
printf("Igraph simplification result:\n");
dump_igraph(dst, 0);
printf("\n");
}
dst->node_count = dst_node_id;
return dst;
}
static void simplify_igraphs() {
for (int i = 0; i < instr_count; i++) {
IGraphNode *ign = &(igraphs[i].nodes[0]);
int operation;
if (ign->tac) operation = ign->tac->operation; else operation = 0;
if (operation != IR_NOP && igraphs[i].node_count) {
IGraph *ig = simplify_igraph(&(igraphs[i]));
shallow_dup_igraph(ig, &(igraphs[i]));
}
}
}
// Convert an instruction graph node from an operation that puts the result
// into a register to an assigment, using a constant value.
static Value *merge_cst_node(IGraph *igraph, int node_id, Value *v) {
GraphNode *src_node = &(igraph->graph->nodes[node_id]);
if (node_id == 0) {
// Root node, convert it into a move, since it has to end up
// in a register
igraph->nodes[node_id].tac->operation = IR_MOVE;
igraph->nodes[node_id].tac->src1 = v;
igraph->nodes[node_id].tac->src2 = 0;
igraph->nodes[src_node->succ->to->id].value = v;
src_node->succ->next_succ = 0;
}
else {
// Not root-node, convert it from a tac into a value node
igraph->nodes[node_id].tac = 0;
igraph->nodes[node_id].value = v;
src_node->succ = 0;
}
return v;
}
static Value *merge_cst_int_node(IGraph *igraph, int node_id, long constant_value, int is_unsigned) {
Value *v = new_value();
v->type = new_type(TYPE_LONG);
v->type->is_unsigned = is_unsigned;
v->is_constant = 1;
v->int_value = constant_value;
return merge_cst_node(igraph, node_id, v);
}
static Value *merge_cst_fp_node(IGraph *igraph, int node_id, Type *type, long double constant_value) {
Value *v = new_value();
v->type = dup_type(type);
v->is_constant = 1;
v->fp_value = constant_value;
return merge_cst_node(igraph, node_id, v);
}
static Value* merge_integer_constants(IGraph *igraph, int node_id, int operation, Value *src1, Value *src2) {
Value *value = evaluate_const_binary_int_operation(operation, src1, src2);
if (!value)
return 0;
else
return merge_cst_int_node(igraph, node_id, value->int_value, value->type->is_unsigned);
}
static Value* merge_fp_constants(IGraph *igraph, int node_id, int operation, Value *src1, Value *src2, Type *type) {
Value *value = evaluate_const_binary_fp_operation(operation, src1, src2, type);
if (!value)
return 0;
else if (is_floating_point_type(value->type))
merge_cst_fp_node(igraph, node_id, type, value->fp_value);
else
merge_cst_int_node(igraph, node_id, value->int_value, 0);
}
static Value *recursive_merge_constants(IGraph *igraph, int node_id) {
GraphNode *src_node = &(igraph->graph->nodes[node_id]);
Tac *tac = igraph->nodes[node_id].tac;
if (!tac) return igraph->nodes[node_id].value;
int operation = tac->operation;
if (operation == IR_NOP) return 0;
int i = 1;
Value *src1 = 0;
Value *src2 = 0;
GraphEdge *e = src_node->succ;
while (e) {
Value *v = recursive_merge_constants(igraph, e->to->id);
if (i == 1) src1 = v; else src2 = v;
e = e->next_succ;
i++;
}
int cst1 = src1 && src1->is_constant;
if (!src1 || !cst1) return 0;
// Unary operations
if (operation == IR_BNOT) return merge_cst_int_node(igraph, node_id, ~src1->int_value, src1->type->is_unsigned);
// Binary operations
int cst2 = cst1 && (src2 && src2->is_constant);
if (!src2 || !cst2) return 0;
Type *common_type = operation_type(src1, src2, 0);
if (is_floating_point_type(src1->type) && is_floating_point_type(src2->type)) {
return merge_fp_constants(igraph, node_id, operation, src1, src2, common_type);
}
else
return merge_integer_constants(igraph, node_id, operation, src1, src2);
}
// Walk all instruction trees and merge nodes where there are constant operations
static void merge_constants(Function *function) {
for (int i = 0; i < instr_count; i++) {
if (!igraphs[i].node_count) continue;
recursive_merge_constants(&(igraphs[i]), 0);
}
}
// Print the cost graph, only showing choices where parent src and child dst match
static void recursive_print_cost_graph(Graph *cost_graph, int *cost_rules, int *accumulated_cost, int node_id, int parent_node_id, int parent_src, int indent) {
GraphEdge *choice_edge = cost_graph->nodes[node_id].succ;
while (choice_edge) {
int choice_node_id = choice_edge->to->id;
GraphNode *choice_node = &(cost_graph->nodes[choice_node_id]);
int match = 1;
if (parent_node_id != -1) {
Rule *parent_rule = &(instr_rules[cost_rules[parent_node_id]]);
int src = (parent_src == 1) ? parent_rule->src1 : parent_rule->src2;
match = (src == instr_rules[cost_rules[choice_node_id]].dst);
if (match) {
printf("%-3d ", choice_node_id);
for (int i = 0; i < indent; i++) printf(" ");
printf("%d ", cost_rules[choice_node_id]);
}
}
else
printf("%d ", cost_rules[choice_node_id]);
if (match) {
printf(" (%d)\n", accumulated_cost[choice_node_id]);
GraphEdge *e = choice_node->succ;
int j = 1;
while (e) {
printf("%-3d ", choice_node_id);
for (int i = 0; i < indent + 1; i++) printf(" ");
printf("src%d\n", j);
recursive_print_cost_graph(cost_graph, cost_rules, accumulated_cost, e->to->id, choice_node_id, j, indent + 2);
e = e->next_succ;
j++;
}
}
choice_edge = choice_edge->next_succ;
}
}
static void print_cost_graph(Graph *cost_graph, int *cost_rules, int *accumulated_cost) {
recursive_print_cost_graph(cost_graph, cost_rules, accumulated_cost, 0, -1, -1, 0);
}
static int new_cost_graph_node(void) {
if (cost_graph_node_count == MAX_INSTRUCTION_GRAPH_CHOICE_NODE_COUNT)
panic("MAX_INSTRUCTION_GRAPH_CHOICE_NODE_COUNT");
return cost_graph_node_count++;
}
static void calculate_accumulated_cost_for_new_cost_node(Rule *r, int src1_cost_graph_node_id, int src2_cost_graph_node_id, int choice_node_id) {
// Do some on the fly accounting, find the minimal cost sub trees
int min_cost_src1 = 100000000;
int min_cost_src2 = 100000000;
// Loop over src1 and src2 if there is one
for (int i = 1; i <= 2; i++) {
if (!src2_cost_graph_node_id && i == 2) continue; // Unary operation
int src = i == 1 ? r->src1 : r->src2;
GraphEdge *e = i == 1
? cost_graph->nodes[src1_cost_graph_node_id].succ
: cost_graph->nodes[src2_cost_graph_node_id].succ;
// Find the lowest cost from all the edges
int min_cost = 100000000;
while (e) {
Rule *child_rule = &(instr_rules[cost_rules[e->to->id]]);
if (src == child_rule->dst) {
int cost = accumulated_cost[e->to->id];
if (cost < min_cost) min_cost = cost;
}
e = e->next_succ;
}
if (i == 1)
min_cost_src1 = min_cost;
else
min_cost_src2 = min_cost;
}
if (!src2_cost_graph_node_id) min_cost_src2 = 0;
accumulated_cost[choice_node_id] = min_cost_src1 + min_cost_src2 + r->cost;
}
// Tile a graph node that has an operation but no operands.
// These are used e.g. in a parameterless return statement.
static int tile_igraph_operand_less_node(IGraph *igraph, int node_id) {
if (debug_instsel_tiling) printf("tile_igraph_operand_less_node on node=%d\n", node_id);
int cost_graph_node_id = new_cost_graph_node();
cost_to_igraph_map[cost_graph_node_id] = node_id;
Tac *tac = igraph->nodes[node_id].tac;
List *rules = longmap_get(instr_rules_by_operation, tac->operation);
for (int i = 0; i < rules->length; i++) {
Rule *r = rules->elements[i];
if (debug_instsel_tiling) {
printf("matched rule %d:\n", r->index);
print_rule(r, 0, 0);
}
int choice_node_id = new_cost_graph_node();
cost_to_igraph_map[choice_node_id] = node_id;
cost_rules[choice_node_id] = r->index;
accumulated_cost[choice_node_id] = r->cost;
add_graph_edge(cost_graph, cost_graph_node_id, choice_node_id);
return cost_graph_node_id;
}
dump_igraph(igraph, 0);
panic("Did not match any rules");
}
// Tile a leaf node in the instruction tree. Rules for leaf nodes all have a zero
// operation. This is simply a case of matching the value to the rule src1.
static int tile_igraph_leaf_node(IGraph *igraph, int node_id) {
if (debug_instsel_tiling) dump_igraph(igraph, 0);
int cost_graph_node_id = new_cost_graph_node();
cost_to_igraph_map[cost_graph_node_id] = node_id;
Value *v = igraph->nodes[node_id].value;
// Find a matching instruction
int matched = 0;
List *rules = longmap_get(instr_rules_by_operation, 0); // Rules without an operation
for (int i = 0; i < rules->length; i++) {
Rule *r = rules->elements[i];
if (!match_value_to_rule_src(v, r->src1)) continue;
if (!v->is_constant && !v->label && v->type->type != TYPE_FUNCTION && !match_value_type_to_rule_dst(v, r->dst)) continue;
if (debug_instsel_tiling) {
printf("matched rule %d:\n", r->index);
print_rule(r, 0, 0);
}
longset_add(igraph_labels[node_id], r->index);
int choice_node_id = new_cost_graph_node();
cost_to_igraph_map[choice_node_id] = node_id;
cost_rules[choice_node_id] = r->index;
accumulated_cost[choice_node_id] = r->cost;
add_graph_edge(cost_graph, cost_graph_node_id, choice_node_id);
matched = 1;
}
if (!matched) {
dump_igraph(igraph, 0);
panic("Did not match any rules");
}
return cost_graph_node_id;
}
// Check if the dst for any label in the child tree for src_id matches rule_src.
static int match_subtree_labels_to_rule(int src_id, int rule_src) {
longset_foreach(igraph_labels[src_id], it) {
int rule = longset_iterator_element(&it);
if (rule_src == instr_rules[rule].dst) return 1;
}
return 0;
}
// Tile an instruction graph node that has an operation with 0, 1 or 2 operands.
static int tile_igraph_operation_node(IGraph *igraph, int node_id) {
IGraphNode *inode = &(igraph->nodes[node_id]);
Tac *tac = inode->tac;
int operation = tac->operation;
if (debug_instsel_tiling) {
printf("tile_igraph_operation_node on node=%d\n", node_id);
print_instruction(stdout, tac, 0);
dump_igraph(igraph, 0);
}
// Fetch the operands from the graph
int src1_id = 0;
int src2_id = 0;
GraphEdge *e = igraph->graph->nodes[node_id].succ;
if (e) {
src1_id = e->to->id;
e = e->next_succ;
if (e) src2_id = e->to->id;
}
// Special case: no operands
if (!src1_id) return tile_igraph_operand_less_node(igraph, node_id);
// Create the root cost instruction graph node
int cost_graph_node_id = new_cost_graph_node();
cost_to_igraph_map[cost_graph_node_id] = node_id;
// Recurse down the src1 and src2 trees
int src1_cost_graph_node_id = recursive_tile_igraphs(igraph, src1_id);
int src2_cost_graph_node_id;
if (src2_id)
src2_cost_graph_node_id = recursive_tile_igraphs(igraph, src2_id);
else
src2_cost_graph_node_id = 0;
if (debug_instsel_tiling) {
printf("back from recursing at node=%d\n\n", node_id);
printf("src1 labels: "); print_longset(igraph_labels[src1_id]); printf(" ");
printf("\n");
if (src2_id) {
printf("src2 labels: ");
print_longset(igraph_labels[src2_id]);
printf("\n");
}
}
if (debug_instsel_tiling && tac->dst)
printf("Want dst %s\n", value_to_non_terminal_string(tac->dst));
// Loop over all rules and gather matches in the cost graph
int matched = 0;
List *rules = longmap_get(instr_rules_by_operation, operation);
for (int i = 0; i < rules->length; i++) {
Rule *r = rules->elements[i];
if (src2_id && !r->src2 || !src2_id && r->src2) continue; // Filter rules requiring src2
// If this is the top level or a rule requires it, ensure the dst matches.
int match_dst = tac->dst && node_id == 0;
// IR_MOVE_TO_PTR is a special case since it doesn't have a dst. The dst is actually
// src1, which isn't modified.
if (match_dst) {
if (tac->operation != IR_MOVE_TO_PTR && !match_value_to_rule_dst(tac->dst, r->dst)) continue;
if (tac->operation == IR_MOVE_TO_PTR && !match_value_to_rule_dst(tac->src1, r->dst)) continue;
}
if (tac->operation == IR_MOVE_TO_PTR && !match_value_type_to_rule_dst(tac->src1, r->dst)) continue;
else if (tac->dst && !match_value_type_to_rule_dst(tac->dst, r->dst)) continue;
// Check dst of the subtree tile matches what is needed
int matched_src = match_subtree_labels_to_rule(src1_id, r->src1);
if (!matched_src) continue;
if (src2_id) {
matched_src = match_subtree_labels_to_rule(src2_id, r->src2);
if (!matched_src) continue;
}
// We have a winner
matched = 1;
if (debug_instsel_tiling) {
printf("matched rule %d:\n", r->index);
print_rule(r, 1, 0);
}
longset_add(igraph_labels[node_id], r->index); // Add a label
// Add cost graph nodes for src1 and potentially src2
int choice_node_id = new_cost_graph_node();
cost_rules[choice_node_id] = r->index;
cost_to_igraph_map[choice_node_id] = node_id;
add_graph_edge(cost_graph, cost_graph_node_id, choice_node_id);
add_graph_edge(cost_graph, choice_node_id, src1_cost_graph_node_id);
if (src2_id) add_graph_edge(cost_graph, choice_node_id, src2_cost_graph_node_id);
calculate_accumulated_cost_for_new_cost_node(r, src1_cost_graph_node_id, src2_cost_graph_node_id, choice_node_id);
}
// All rules have been visited, if there are no matches, we have a problem, bail
// with an error.
if (!matched) {
printf("\nNo rules matched\n");
if (tac->dst) printf("Want dst %s\n", value_to_non_terminal_string(tac->dst));
print_instruction(stdout, tac, 0);
dump_igraph(igraph, 0);
exit(1);
}
return cost_graph_node_id;
}
// Algorithm from page 618 of Engineering a compiler
// with additional minimal cost tiling determination as suggested on page 619.
static int recursive_tile_igraphs(IGraph *igraph, int node_id) {
if (debug_instsel_tiling) printf("\nrecursive_tile_igraphs on node=%d\n", node_id);
if (!igraph->nodes[node_id].tac)
return tile_igraph_leaf_node(igraph, node_id);
else
return tile_igraph_operation_node(igraph, node_id);
}
// find the lowest cost successor in the cost graph at node_id
static int get_least_expensive_choice_node_id(int node_id, int parent_node_id, int parent_src) {
int min_cost = 100000000;
int least_expensive_choice_node_id = -1;
GraphEdge *choice_edge = cost_graph->nodes[node_id].succ;
while (choice_edge) {
int choice_node_id = choice_edge->to->id;
int match;