/
blockopt.c
2201 lines (1927 loc) · 62.9 KB
/
blockopt.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) 1986-1997 by Symantec
// Copyright (C) 2000-2013 by Digital Mars
// All Rights Reserved
// http://www.digitalmars.com
// Written by Walter Bright
/*
* This source file is made available for personal use
* only. The license is in backendlicense.txt
* For any other uses, please contact Digital Mars.
*/
/****************************************************************
* Handle basic blocks.
*/
#if !SPP
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include "cc.h"
#include "oper.h"
#include "el.h"
#include "type.h"
#include "global.h"
#include "go.h"
#include "code.h"
#if SCPP
#include "parser.h"
#include "iasm.h"
#endif
static char __file__[] = __FILE__; /* for tassert.h */
#include "tassert.h"
STATIC void bropt(void);
STATIC void brrear(void);
STATIC void search(block *b);
STATIC void elimblks(void);
STATIC int mergeblks(void);
STATIC void blident(void);
STATIC void blreturn(void);
STATIC void brmin(void);
STATIC void bltailmerge(void);
STATIC void block_check();
STATIC void brtailrecursion();
STATIC elem * assignparams(elem **pe,int *psi,elem **pe2);
STATIC void emptyloops();
int el_anyframeptr(elem *e);
unsigned numblks; // number of basic blocks in current function
block *startblock; /* beginning block of function */
/* (can have no predecessors) */
block **dfo = NULL; /* array of depth first order */
unsigned dfotop; /* # of items in dfo[] */
block *curblock; /* current block being read in */
block *block_last; // last block read in
static block * block_freelist;
////////////////////////////
// Storage allocator.
static block blkzero;
__inline block *block_calloc_i()
{ block *b;
if (block_freelist)
{ b = block_freelist;
block_freelist = b->Bnext;
*b = blkzero;
}
else
b = (block *) mem_fcalloc(sizeof(block));
return b;
}
block *block_calloc()
{
return block_calloc_i();
}
//////////////////////////////////
//
goal_t bc_goal[BCMAX];
void block_init()
{
for (size_t i = 0; i < BCMAX; i++)
bc_goal[i] = GOALvalue;
bc_goal[BCgoto] = GOALnone;
bc_goal[BCret ] = GOALnone;
bc_goal[BCexit] = GOALnone;
bc_goal[BCiftrue] = GOALflags;
}
//////////////////////////////////
//
void block_term()
{
#if TERMCODE
block *b;
while (block_freelist)
{ b = block_freelist->Bnext;
mem_ffree(block_freelist);
block_freelist = b;
}
#endif
}
/**************************
* Finish up this block and start the next one.
*/
#if MARS
void block_next(Blockx *bctx,enum BC bc,block *bn)
{
bctx->curblock->BC = bc;
block_last = bctx->curblock;
if (!bn)
bn = block_calloc_i();
bctx->curblock->Bnext = bn; // next block
bctx->curblock = bctx->curblock->Bnext; // new current block
#if NTEXCEPTIONS
bctx->curblock->Btry = bctx->tryblock;
#endif
bctx->curblock->Bflags |= bctx->flags;
}
#else
void block_next(enum BC bc,block *bn)
{
curblock->BC = bc;
curblock->Bsymend = globsym.top;
block_last = curblock;
if (!bn)
bn = block_calloc_i();
curblock->Bnext = bn; // next block
curblock = curblock->Bnext; // new current block
curblock->Bsymstart = globsym.top;
curblock->Btry = pstate.STbtry;
}
void block_next()
{
block_next((enum BC)curblock->BC,NULL);
}
#endif
/**************************
* Finish up this block and start the next one.
*/
#if MARS
block *block_goto(Blockx *bx,enum BC bc,block *bn)
{ block *b;
b = bx->curblock;
block_next(bx,bc,bn);
list_append(&b->Bsucc,bx->curblock);
return bx->curblock;
}
#endif
/****************************
* Goto a block named gotolbl.
* Start a new block that is labelled by newlbl.
*/
#if SCPP
void block_goto()
{
block_goto(block_calloc());
}
void block_goto(block *bn)
{
block_goto(bn,bn);
}
void block_goto(block *bgoto,block *bnew)
{
enum BC bc;
assert(bgoto);
list_append(&(curblock->Bsucc),bgoto);
if (curblock->Bcode) // If there is already code in the block
// then this is an ASM block
bc = BCasm;
else
bc = BCgoto; // fall thru to next block
block_next(bc,bnew);
}
#endif
/**********************************
* Replace block numbers with block pointers.
* Also compute numblks and maxblks.
*/
void block_ptr()
{ block *b;
/* dbg_printf("block_ptr()\n");*/
numblks = 0;
for (b = startblock; b; b = b->Bnext) /* for each block */
{
#if SCPP
b->Bblknum = numblks;
#endif
numblks++;
}
maxblks = 3 * numblks; /* allow for increase in # of blocks */
}
/*******************************
* Build predecessor list (Bpred) for each block.
*/
void block_pred()
{ block *b;
//dbg_printf("block_pred()\n");
for (b = startblock; b; b = b->Bnext) /* for each block */
list_free(&b->Bpred,FPNULL);
for (b = startblock; b; b = b->Bnext) /* for each block */
{ list_t bp;
//printf("b = %p, BC = ",b); WRBC(b->BC); printf("\n");
for (bp = b->Bsucc; bp; bp = list_next(bp))
{ /* for each successor to b */
//printf("\tbs = %p\n",list_block(bp));
assert(list_block(bp));
list_prepend(&(list_block(bp)->Bpred),b);
}
}
assert(startblock->Bpred == NULL); /* startblock has no preds */
}
/********************************************
* Clear visit.
*/
void block_clearvisit()
{ block *b;
for (b = startblock; b; b = b->Bnext) // for each block
b->Bflags &= ~BFLvisited; // mark as unvisited
}
/********************************************
* Visit block and each of its predecessors.
*/
void block_visit(block *b)
{ list_t l;
b->Bflags |= BFLvisited;
for (l = b->Bsucc; l; l = list_next(l)) // for each successor
{ block *bs = list_block(l);
assert(bs);
if ((bs->Bflags & BFLvisited) == 0) // if not visited
block_visit(bs);
}
}
/*****************************
* Compute number of parents (Bcount) of each basic block.
*/
void block_compbcount()
{
block_clearvisit();
block_visit(startblock); // visit all reachable blocks
elimblks(); // eliminate unvisited blocks
}
/*******************************
* Free list of blocks.
*/
void blocklist_free(block **pb)
{ block *b,*bn;
for (b = *pb; b; b = bn)
{ bn = b->Bnext;
block_free(b);
}
*pb = NULL;
}
/********************************
* Free optimizer gathered data.
*/
void block_optimizer_free(block *b)
{
vec_free(b->Bdom);
vec_free(b->Binrd);
vec_free(b->Boutrd);
vec_free(b->Binlv);
vec_free(b->Boutlv);
vec_free(b->Bin);
vec_free(b->Bout);
vec_free(b->Bgen);
vec_free(b->Bkill);
vec_free(b->Bout2);
vec_free(b->Bgen2);
vec_free(b->Bkill2);
memset(&b->_BLU,0,sizeof(b->_BLU));
}
/****************************
* Free a block.
*/
void block_free(block *b)
{
assert(b);
if (b->Belem)
el_free(b->Belem);
list_free(&b->Bsucc,FPNULL);
list_free(&b->Bpred,FPNULL);
if (OPTIMIZER)
block_optimizer_free(b);
switch (b->BC)
{ case BCswitch:
case BCifthen:
case BCjmptab:
#if MARS
free(b->BS.Bswitch);
#else
MEM_PH_FREE(b->BS.Bswitch);
#endif
break;
#if SCPP
case BCcatch:
type_free(b->Bcatchtype);
break;
#endif
case BCasm:
code_free(b->Bcode);
break;
}
b->Bnext = block_freelist;
block_freelist = b;
}
/****************************
* Hydrate/dehydrate a list of blocks.
*/
#if HYDRATE
void blocklist_hydrate(block **pb)
{ block *b;
while (isdehydrated(*pb))
{
/*dbg_printf("blocklist_hydrate(*pb = %p) =",*pb);*/
b = (block *)ph_hydrate(pb);
/*dbg_printf(" %p\n",b);*/
el_hydrate(&b->Belem);
list_hydrate(&b->Bsucc,FPNULL);
list_hydrate(&b->Bpred,FPNULL);
(void) ph_hydrate(&b->Btry);
#if SCPP
(void) ph_hydrate(&b->Bendscope);
symbol_hydrate(&b->Binitvar);
#endif
switch (b->BC)
{
#if SCPP
case BCtry:
symbol_hydrate(&b->catchvar);
break;
case BCcatch:
type_hydrate(&b->Bcatchtype);
break;
#endif
case BCswitch:
(void) ph_hydrate(&b->BS.Bswitch);
break;
case BC_finally:
//(void) ph_hydrate(&b->B_ret);
break;
case BCasm:
code_hydrate(&b->Bcode);
break;
}
#if TX86
filename_translate(&b->Bsrcpos);
srcpos_hydrate(&b->Bsrcpos);
#endif
pb = &b->Bnext;
}
}
#endif
#if DEHYDRATE
void blocklist_dehydrate(block **pb)
{ block *b;
while ((b = *pb) != NULL && !isdehydrated(b))
{
#if DEBUG_XSYMGEN
if (xsym_gen && ph_in_head(b))
return;
#endif
/*dbg_printf("blocklist_dehydrate(*pb = %p) =",b);*/
ph_dehydrate(pb);
/*dbg_printf(" %p\n",*pb);*/
el_dehydrate(&b->Belem);
list_dehydrate(&b->Bsucc,FPNULL);
list_dehydrate(&b->Bpred,FPNULL);
ph_dehydrate(&b->Btry);
#if SCPP
ph_dehydrate(&b->Bendscope);
symbol_dehydrate(&b->Binitvar);
#endif
switch (b->BC)
{
#if SCPP
case BCtry:
symbol_dehydrate(&b->catchvar);
break;
case BCcatch:
type_dehydrate(&b->Bcatchtype);
break;
#endif
case BCswitch:
ph_dehydrate(&b->BS.Bswitch);
break;
case BC_finally:
//ph_dehydrate(&b->B_ret);
break;
case BCasm:
code_dehydrate(&b->Bcode);
break;
}
#if TX86
srcpos_dehydrate(&b->Bsrcpos);
#endif
pb = &b->Bnext;
}
}
#endif
/****************************
* Append elem to the elems comprising the current block.
* Read in an expression and put it in curblock->Belem.
* If there is one already there, create a tree like:
* ,
* / \
* old e
*/
void block_appendexp(block *b,elem *e)
{ elem *ec;
elem **pe;
assert(MARS || PARSER);
if (e)
{
assert(b);
elem_debug(e);
pe = &b->Belem;
ec = *pe;
if (ec != NULL)
{
type *t = e->ET;
if (t)
type_debug(t);
elem_debug(e);
#if MARS
tym_t ty = e->Ety;
elem_debug(e);
/* Build tree such that (a,b) => (a,(b,e)) */
while (ec->Eoper == OPcomma)
{
ec->Ety = ty;
ec->ET = t;
pe = &(ec->E2);
ec = *pe;
}
e = el_bin(OPcomma,ty,ec,e);
e->ET = t;
#else
/* Build tree such that (a,b) => (a,(b,e)) */
while (ec->Eoper == OPcomma)
{
el_settype(ec,t);
pe = &(ec->E2);
ec = *pe;
}
e = el_bint(OPcomma,t,ec,e);
#endif
}
*pe = e;
}
}
/************************
* Mark curblock as initializing symbol s.
*/
#if SCPP
#undef block_initvar
void block_initvar(symbol *s)
{
#ifdef DEBUG
assert(curblock);
#endif
symbol_debug(s);
curblock->Binitvar = s;
}
#endif
/*******************
* Mark end of function.
* flag:
* 0 do a "return"
* 1 do a "return 0"
*/
void block_endfunc(int flag)
{
#if SCPP
curblock->Bsymend = globsym.top;
curblock->Bendscope = curblock;
#endif
if (flag)
{ elem *e;
e = el_longt(tsint, 0);
block_appendexp(curblock, e);
curblock->BC = BCretexp; // put a return at the end
}
else
curblock->BC = BCret; // put a return at the end
curblock = NULL; // undefined from now on
block_last = NULL;
}
/******************************
* Perform branch optimization on basic blocks.
*/
void blockopt(int iter)
{ block *b;
int count;
if (OPTIMIZER)
{
int iterationLimit = 200;
if (iterationLimit < numblks)
iterationLimit = numblks;
count = 0;
do
{
//printf("changes = %d, count = %d, dfotop = %d\n",changes,count,dfotop);
#if MARS
util_progress();
#else
if (controlc_saw)
util_exit(EXIT_BREAK);
#endif
changes = 0;
bropt(); // branch optimization
brrear(); // branch rearrangement
blident(); // combine identical blocks
blreturn(); // split out return blocks
bltailmerge(); // do tail merging
brtailrecursion(); // do tail recursion
brcombine(); // convert graph to expressions
if (iter >= 2)
brmin(); // minimize branching
do
{
compdfo(); /* compute depth first order (DFO) */
elimblks(); /* remove blocks not in DFO */
assert(count < iterationLimit);
count++;
} while (mergeblks()); /* merge together blocks */
} while (changes);
#ifdef DEBUG
if (debugw)
for (b = startblock; b; b = b->Bnext)
WRblock(b);
#endif
}
else
{
/* canonicalize the trees */
for (b = startblock; b; b = b->Bnext)
{
#ifdef DEBUG
if (debugb)
WRblock(b);
#endif
if (b->Belem)
{ b->Belem = doptelem(b->Belem,bc_goal[b->BC] | GOALstruct);
if (b->Belem)
b->Belem = el_convert(b->Belem);
}
#ifdef DEBUG
if (debugb)
{ dbg_printf("after optelem():\n");
WRblock(b);
}
#endif
}
if (localgot)
{ // Initialize with:
// localgot = OPgot;
elem *e = el_long(TYnptr, 0);
e->Eoper = OPgot;
e = el_bin(OPeq, TYnptr, el_var(localgot), e);
startblock->Belem = el_combine(e, startblock->Belem);
}
bropt(); /* branch optimization */
brrear(); /* branch rearrangement */
comsubs(); /* eliminate common subexpressions */
#ifdef DEBUG
if (debugb)
for (b = startblock; b; b = b->Bnext)
WRblock(b);
#endif
}
}
/***********************************
* Try to remove control structure.
* That is, try to resolve if-else, goto and return statements
* into &&, || and ?: combinations.
*/
void brcombine()
{ block *b;
block *b2,*b3;
int op;
int anychanges;
cmes("brcombine()\n");
//for (b = startblock; b; b = b->Bnext)
//WRblock(b);
if (funcsym_p->Sfunc->Fflags3 & (Fcppeh | Fnteh))
{ // Don't mess up extra EH info by eliminating blocks
return;
}
do
{
anychanges = 0;
for (b = startblock; b; b = b->Bnext) /* for each block */
{ unsigned char bc;
/* Look for [e1 IFFALSE L3,L2] L2: [e2 GOTO L3] L3: [e3] */
/* Replace with [(e1 && e2),e3] */
bc = b->BC;
if (bc == BCiftrue)
{ unsigned char bc2;
b2 = list_block(b->Bsucc);
b3 = list_block(list_next(b->Bsucc));
if (list_next(b2->Bpred)) // if more than one predecessor
continue;
if (b2 == b3)
continue;
if (b2 == startblock)
continue;
if (!PARSER && b2->Belem && EOP(b2->Belem))
continue;
bc2 = b2->BC;
if (bc2 == BCgoto &&
b3 == list_block(b2->Bsucc))
{
b->BC = BCgoto;
if (b2->Belem)
{
op = OPandand;
b->Belem = PARSER ? el_bint(op,tsint,b->Belem,b2->Belem)
: el_bin(op,TYint,b->Belem,b2->Belem);
b2->Belem = NULL;
}
list_subtract(&(b->Bsucc),b2);
list_subtract(&(b2->Bpred),b);
cmes("brcombine(): if !e1 then e2 => e1 || e2\n");
anychanges++;
}
else if (list_next(b3->Bpred) || b3 == startblock)
continue;
else if ((bc2 == BCretexp && b3->BC == BCretexp)
//|| (bc2 == BCret && b3->BC == BCret)
)
{ elem *e;
if (PARSER)
{
type *t = (bc2 == BCretexp) ? b2->Belem->ET : tsvoid;
e = el_bint(OPcolon2,t,b2->Belem,b3->Belem);
b->Belem = el_bint(OPcond,t,b->Belem,e);
}
else
{
if (EOP(b3->Belem))
continue;
tym_t ty = (bc2 == BCretexp) ? b2->Belem->Ety : TYvoid;
e = el_bin(OPcolon2,ty,b2->Belem,b3->Belem);
b->Belem = el_bin(OPcond,ty,b->Belem,e);
}
b->BC = bc2;
b->Belem->ET = b2->Belem->ET;
b2->Belem = NULL;
b3->Belem = NULL;
list_free(&b->Bsucc,FPNULL);
list_subtract(&(b2->Bpred),b);
list_subtract(&(b3->Bpred),b);
cmes("brcombine(): if e1 return e2 else return e3 => ret e1?e2:e3\n");
anychanges++;
}
else if (bc2 == BCgoto &&
b3->BC == BCgoto &&
list_block(b2->Bsucc) == list_block(b3->Bsucc))
{ elem *e;
block *bsucc;
bsucc = list_block(b2->Bsucc);
if (b2->Belem)
{
if (PARSER)
{
if (b3->Belem)
{
e = el_bint(OPcolon2,b2->Belem->ET,
b2->Belem,b3->Belem);
e = el_bint(OPcond,e->ET,b->Belem,e);
}
else
{
op = OPandand;
e = el_bint(op,tsint,b->Belem,b2->Belem);
}
}
else
{
if (b3->Belem)
{
if (EOP(b3->Belem))
continue;
e = el_bin(OPcolon2,b2->Belem->Ety,
b2->Belem,b3->Belem);
e = el_bin(OPcond,e->Ety,b->Belem,e);
e->ET = b2->Belem->ET;
}
else
{
op = OPandand;
e = el_bin(op,TYint,b->Belem,b2->Belem);
}
}
b2->Belem = NULL;
b->Belem = e;
}
else if (b3->Belem)
{
op = OPoror;
b->Belem = PARSER ? el_bint(op,tsint,b->Belem,b3->Belem)
: el_bin(op,TYint,b->Belem,b3->Belem);
}
b->BC = BCgoto;
b3->Belem = NULL;
list_free(&b->Bsucc,FPNULL);
list_append(&b->Bsucc,bsucc);
list_append(&bsucc->Bpred,b);
list_free(&(b2->Bpred),FPNULL);
list_free(&(b2->Bsucc),FPNULL);
list_free(&(b3->Bpred),FPNULL);
list_free(&(b3->Bsucc),FPNULL);
b2->BC = BCret;
b3->BC = BCret;
list_subtract(&(bsucc->Bpred),b2);
list_subtract(&(bsucc->Bpred),b3);
cmes("brcombine(): if e1 goto e2 else goto e3 => ret e1?e2:e3\n");
anychanges++;
}
}
else if (bc == BCgoto && PARSER)
{
b2 = list_block(b->Bsucc);
if (!list_next(b2->Bpred) && b2->BC != BCasm // if b is only parent
&& b2 != startblock
#if SCPP
&& b2->BC != BCtry
#endif
&& b2->BC != BC_try
&& b->Btry == b2->Btry
)
{ list_t bl;
if (b2->Belem)
{
if (PARSER)
{
block_appendexp(b,b2->Belem);
}
else if (b->Belem)
b->Belem = el_bin(OPcomma,b2->Belem->Ety,b->Belem,b2->Belem);
else
b->Belem = b2->Belem;
b2->Belem = NULL;
}
list_subtract(&b->Bsucc,b2);
list_subtract(&b2->Bpred,b);
/* change predecessor of successors of b2 from b2 to b */
for (bl = b2->Bsucc; bl; bl = list_next(bl))
{ list_t bp;
for (bp = list_block(bl)->Bpred; bp; bp = list_next(bp))
{
if (list_block(bp) == b2)
list_ptr(bp) = (void *)b;
}
}
b->BC = b2->BC;
b->BS = b2->BS;
b->Bsucc = b2->Bsucc;
b2->Bsucc = NULL;
b2->BC = BCret; /* a harmless one */
cmes3("brcombine(): %p goto %p eliminated\n",b,b2);
anychanges++;
}
}
}
if (anychanges)
{ changes++;
continue;
}
} while (0);
}
/***********************
* Branch optimization.
*/
STATIC void bropt()
{ block *b,*db;
elem *n,**pn;
cmes("bropt()\n");
assert(!PARSER);
for (b = startblock; b; b = b->Bnext) /* for each block */
{
pn = &(b->Belem);
if (OPTIMIZER && *pn)
while ((*pn)->Eoper == OPcomma)
pn = &((*pn)->E2);
n = *pn;
if (b->BC == BCiftrue)
{
assert(n);
/* Replace IF (!e) GOTO ... with */
/* IF OPnot (e) GOTO ... */
if (n->Eoper == OPnot)
{
tym_t tym;
tym = n->E1->Ety;
*pn = el_selecte1(n);
(*pn)->Ety = tym;
for (n = b->Belem; n->Eoper == OPcomma; n = n->E2)
n->Ety = tym;
b->Bsucc = list_reverse(b->Bsucc);
cmes("CHANGE: if (!e)\n");
changes++;
}
/* Take care of IF (constant) */
if (iftrue(n)) /* if elem is TRUE */
{
// select first succ
db = list_block(list_next(b->Bsucc));
goto L1;
}
else if (iffalse(n))
{
// select second succ
db = list_block(b->Bsucc);
L1: list_subtract(&(b->Bsucc),db);
list_subtract(&(db->Bpred),b);
b->BC = BCgoto;
/* delete elem if it has no side effects */
b->Belem = doptelem(b->Belem,GOALnone | GOALagain);
cmes("CHANGE: if (const)\n");
changes++;
}
/* Look for both destinations being the same */
else if (list_block(b->Bsucc) ==
list_block(list_next(b->Bsucc)))
{ b->BC = BCgoto;
db = list_block(b->Bsucc);
list_subtract(&(b->Bsucc),db);
list_subtract(&(db->Bpred),b);
cmes("CHANGE: if (e) goto L1; else goto L1;\n");
changes++;
}
}
else if (b->BC == BCswitch)
{ /* see we can evaluate this switch now */
unsigned i,ncases;
targ_llong *p,value;
list_t bl;
while (n->Eoper == OPcomma)
n = n->E2;
if (n->Eoper != OPconst)
continue;
assert(tyintegral(n->Ety));
value = el_tolong(n);
p = b->BS.Bswitch; /* ptr to switch data */
assert(p);
ncases = *p++; /* # of cases */
i = 1; /* first case */
while (1)
{
if (i > ncases)
{ i = 0; /* select default */
break;
}
if (*p++ == value)
break; /* found it */
i++; /* next case */
}
/* the ith entry in Bsucc is the one we want */
db = list_block(list_nth(b->Bsucc,i));
/* delete predecessors of successors (!) */
for (bl = b->Bsucc; bl; bl = list_next(bl))
if (i--) // if not ith successor
{ void *p;
p = list_subtract(
&(list_block(bl)->Bpred),b);
assert(p == b);
}
/* dump old successor list and create a new one */
list_free(&b->Bsucc,FPNULL);
list_append(&b->Bsucc,db);
b->BC = BCgoto;
b->Belem = doptelem(b->Belem,GOALnone | GOALagain);
cmes("CHANGE: switch (const)\n");
changes++;
}
}
}
/*********************************
* Do branch rearrangement.
*/
STATIC void brrear()
{ block *b;
cmes("brrear()\n");