-
-
Notifications
You must be signed in to change notification settings - Fork 593
/
gother.c
1836 lines (1648 loc) · 63.2 KB
/
gother.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-1998 by Symantec
// Copyright (C) 2000-2011 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.
*/
#if (SCPP || MARS) && !HTOD
#include <stdio.h>
#include <time.h>
#include "cc.h"
#include "global.h"
#include "el.h"
#include "go.h"
#include "oper.h"
#include "list.h"
#include "type.h"
#if SCPP
#include "parser.h"
#endif
static char __file__[] = __FILE__; /* for tassert.h */
#include "tassert.h"
extern void error(const char *filename, unsigned linnum, unsigned charnum, const char *format, ...);
STATIC void rd_free_elem(elem *e);
STATIC void rd_compute();
STATIC void conpropwalk(elem *n , vec_t IN);
STATIC void chkrd(elem *n , list_t rdlist);
STATIC elem * chkprop(elem *n , list_t rdlist);
STATIC void arrayboundsrds(void);
STATIC void eqeqranges(void);
STATIC void intranges(void);
STATIC int loopcheck(block *start , block *inc , block *rel);
STATIC void cpwalk(elem *n , vec_t IN);
STATIC unsigned numasg(elem *e);
STATIC void accumda(elem *n , vec_t DEAD , vec_t POSS);
STATIC void dvwalk(elem *n , unsigned i);
STATIC int killed(unsigned j , block *bp , block *b);
STATIC int ispath(unsigned j , block *bp , block *b);
/**********************************************************************/
// Lists to help identify ranges of variables
struct Elemdata
{ Elemdata *next; // linked list
elem *pelem; // the elem in question
block *pblock; // which block it's in
list_t rdlist; // list of definition elems for *pelem
static Elemdata *ctor(elem *e,block *b,list_t rd);
};
Elemdata *Elemdata::ctor(elem *e,block *b,list_t rd)
{ Elemdata *ed;
ed = (Elemdata *) MEM_BEF_CALLOC(sizeof(Elemdata));
ed->pelem = e;
ed->pblock = b;
ed->rdlist = rd;
return ed;
}
/*****************
* Free list of Elemdata's.
*/
STATIC void elemdatafree(Elemdata **plist)
{ Elemdata *el;
Elemdata *eln;
for (el = *plist; el; el = eln)
{ eln = el->next;
list_free(&el->rdlist);
MEM_BEF_FREE(el);
}
*plist = NULL;
}
static Elemdata *arraylist = NULL; // list of Elemdata's of OParray elems
static Elemdata *eqeqlist = NULL; // list of Elemdata's of OPeqeq & OPne elems
static Elemdata *rellist = NULL; // list of Elemdata's of relop elems
static Elemdata *inclist = NULL; // list of Elemdata's of increment elems
enum Rdtype { RDarraybounds, RDconstprop };
static Rdtype rdtype;
/*************************** Constant Propagation ***************************/
/**************************
* Constant propagation.
* Also detects use of variable before any possible def.
*/
void constprop()
{
rdtype = RDconstprop;
rd_compute();
intranges(); // compute integer ranges
#if 0
eqeqranges(); // see if we can eliminate some relationals
#endif
elemdatafree(&eqeqlist);
elemdatafree(&rellist);
elemdatafree(&inclist);
}
/************************************
* Compute reaching definitions.
* Note: RD vectors are destroyed by this.
*/
static block *thisblock;
STATIC void rd_compute()
{ register unsigned i;
cmes("constprop()\n");
assert(dfo);
flowrd(); /* compute reaching definitions (rd) */
if (deftop == 0) /* if no reaching defs */
return;
assert(rellist == NULL && inclist == NULL && eqeqlist == NULL && arraylist == NULL);
block_clearvisit();
for (i = 0; i < dfotop; i++) /* for each block */
{ block *b = dfo[i];
switch (b->BC)
{
#if MARS
case BCjcatch:
#endif
case BC_finally:
case BCasm:
case BCcatch:
block_visit(b);
break;
}
}
for (i = 0; i < dfotop; i++) /* for each block */
{ register block *b;
b = dfo[i];
thisblock = b;
#if 0
dbg_printf("block %d Bin ",i); vec_println(b->Binrd);
dbg_printf(" Bout "); vec_println(b->Boutrd);
#endif
if (b->Bflags & BFLvisited)
continue; // not reliable for this block
if (b->Belem)
{
conpropwalk(b->Belem,b->Binrd);
#ifdef DEBUG
if (!(vec_equal(b->Binrd,b->Boutrd)))
{ int j;
dbg_printf("block %d Binrd ",i); vec_println(b->Binrd);
dbg_printf(" Boutrd "); vec_println(b->Boutrd);
WReqn(b->Belem);
dbg_printf("\n");
vec_xorass(b->Binrd,b->Boutrd);
j = vec_index(0,b->Binrd);
WReqn(defnod[j].DNelem);
dbg_printf("\n");
}
#endif
assert(vec_equal(b->Binrd,b->Boutrd));
}
}
}
/***************************
* Support routine for constprop().
* Visit each elem in order
* If elem is a reference to a variable, and
* all the reaching defs of that variable are
* defining it to be a specific constant,
* Replace reference with that constant.
* Generate warning if no reaching defs for a
* variable, and the variable is on the stack
* or in a register.
* If elem is an assignment or function call or OPasm
* Modify vector of reaching defs.
*/
STATIC void conpropwalk(elem *n,vec_t IN)
{ register unsigned op;
Elemdata *pdata;
vec_t L,R;
elem *t;
assert(n && IN);
/*chkvecdim(deftop,0);*/
//printf("conpropwalk()\n"),elem_print(n);
op = n->Eoper;
if (op == OPcolon || op == OPcolon2)
{ L = vec_clone(IN);
conpropwalk(n->E1,L);
conpropwalk(n->E2,IN);
vec_orass(IN,L); /* IN = L | R */
vec_free(L);
}
else if (op == OPandand || op == OPoror)
{ conpropwalk(n->E1,IN);
R = vec_clone(IN);
conpropwalk(n->E2,R);
vec_orass(IN,R); /* IN |= R */
vec_free(R);
}
else if (OTunary(op))
goto L3;
else if (ERTOL(n))
{ conpropwalk(n->E2,IN);
L3:
t = n->E1;
if (OTassign(op))
{
if (t->Eoper == OPvar)
{
// Note that the following ignores OPnegass
if (rdtype == RDconstprop &&
OTopeq(op) && sytab[t->EV.sp.Vsym->Sclass] & SCRD)
{ elem *e;
list_t rdl;
rdl = listrds(IN,t,NULL);
if (!(config.flags & CFGnowarning)) // if warnings are enabled
chkrd(t,rdl);
e = chkprop(t,rdl);
if (e)
{ // Replace (t op= exp) with (t = e op exp)
e = el_copytree(e);
e->Ety = t->Ety;
n->E2 = el_bin(opeqtoop(op),n->Ety,e,n->E2);
n->Eoper = OPeq;
}
list_free(&rdl);
}
}
else
conpropwalk(t,IN);
}
else
conpropwalk(t,IN);
}
else if (OTbinary(op))
{
if (OTassign(op))
{ t = Elvalue(n);
if (t->Eoper != OPvar)
conpropwalk(t,IN);
}
else
conpropwalk(n->E1,IN);
conpropwalk(n->E2,IN);
}
// Collect data for subsequent optimizations
if (OTbinary(op) && n->E1->Eoper == OPvar && n->E2->Eoper == OPconst)
{
switch (op)
{
case OPlt:
case OPgt:
case OPle:
case OPge:
// Collect compare elems and their rd's in the rellist list
if (rdtype == RDconstprop &&
tyintegral(n->E1->Ety) &&
tyintegral(n->E2->Ety)
)
{
//dbg_printf("appending to rellist\n"); elem_print(n);
pdata = Elemdata::ctor(n,thisblock,listrds(IN,n->E1,NULL));
pdata->next = rellist;
rellist = pdata;
}
break;
case OPaddass:
case OPminass:
case OPpostinc:
case OPpostdec:
// Collect increment elems and their rd's in the inclist list
if (rdtype == RDconstprop &&
tyintegral(n->E1->Ety))
{
//dbg_printf("appending to inclist\n"); elem_print(n);
pdata = Elemdata::ctor(n,thisblock,listrds(IN,n->E1,NULL));
pdata->next = inclist;
inclist = pdata;
}
break;
case OPne:
case OPeqeq:
// Collect compare elems and their rd's in the rellist list
if (rdtype == RDconstprop &&
tyintegral(n->E1->Ety))
{ //dbg_printf("appending to eqeqlist\n"); elem_print(n);
pdata = Elemdata::ctor(n,thisblock,listrds(IN,n->E1,NULL));
pdata->next = eqeqlist;
eqeqlist = pdata;
}
break;
}
}
if (OTdef(op)) /* if definition elem */
updaterd(n,IN,NULL); /* then update IN vector */
/* now we get to the part that checks to see if we can */
/* propagate a constant. */
if (op == OPvar && sytab[n->EV.sp.Vsym->Sclass] & SCRD)
{ list_t rdl;
//printf("const prop: %s\n", n->EV.sp.Vsym->Sident);
rdl = listrds(IN,n,NULL);
if (rdtype == RDconstprop)
{ elem *e;
if (!(config.flags & CFGnowarning)) // if warnings are enabled
chkrd(n,rdl);
e = chkprop(n,rdl);
if (e)
{ tym_t nty;
nty = n->Ety;
el_copy(n,e);
n->Ety = nty; // retain original type
}
list_free(&rdl);
}
}
}
/******************************
* Give error if there are no reaching defs for variable v.
*/
STATIC void chkrd(elem *n,list_t rdlist)
{ unsigned i;
symbol *sv;
int unambig;
sv = n->EV.sp.Vsym;
assert(sytab[sv->Sclass] & SCRD);
if (sv->Sflags & SFLnord) // if already printed a warning
return;
if (sv->ty() & mTYvolatile)
return;
unambig = sv->Sflags & SFLunambig;
for (list_t l = rdlist; l; l = list_next(l))
{ elem *d = (elem *) list_ptr(l);
elem_debug(d);
if (d->Eoper == OPasm) /* OPasm elems ruin everything */
return;
if (OTassign(d->Eoper))
{
if (d->E1->Eoper == OPvar)
{ if (d->E1->EV.sp.Vsym == sv)
return;
}
else if (!unambig)
return;
}
else
{ if (!unambig)
return;
}
}
// If there are any asm blocks, don't print the message
for (i = 0; i < dfotop; i++)
if (dfo[i]->BC == BCasm)
return;
// If variable contains bit fields, don't print message (because if
// bit field is the first set, then we get a spurious warning).
// STL uses 0 sized structs to transmit type information, so
// don't complain about them being used before set.
if (type_struct(sv->Stype))
{
if (sv->Stype->Ttag->Sstruct->Sflags & (STRbitfields | STR0size))
return;
}
#if 0
// If variable is zero length static array, don't print message.
// BUG: Suppress error even if variable is initialized with void.
if (sv->Stype->Tty == TYarray && sv->Stype->Tdim == 0)
{
printf("sv->Sident = %s\n", sv->Sident);
return;
}
#endif
#if SCPP
{ Outbuffer buf;
char *p2;
type_tostring(&buf, sv->Stype);
buf.writeByte(' ');
buf.write(sv->Sident);
p2 = buf.toString();
warerr(WM_used_b4_set, p2); // variable used before set
}
#endif
#if 1 && MARS
/* Watch out for:
void test()
{
void[0] x;
auto y = x;
}
*/
if (type_size(sv->Stype) != 0)
{
error(n->Esrcpos.Sfilename, n->Esrcpos.Slinnum, n->Esrcpos.Scharnum,
"variable %s used before set", sv->Sident);
}
#endif
sv->Sflags |= SFLnord; // no redundant messages
#ifdef DEBUG
//elem_print(n);
#endif
}
/**********************************
* Look through the vector of reaching defs (IN) to see
* if all defs of n are of the same constant. If so, replace
* n with that constant.
* Bit fields are gross, so don't propagate anything with assignments
* to a bit field.
* Note the flaw in the reaching def vector. There is currently no way
* to detect RDs from when the function is invoked, i.e. RDs for parameters,
* statics and globals. This could be fixed by adding dummy defs for
* them before startblock, but we just kludge it and don't propagate
* stuff for them.
* Returns:
* NULL do not propagate constant
* e constant elem that we should replace n with
*/
STATIC elem * chkprop(elem *n,list_t rdlist)
{
elem *foundelem = NULL;
int unambig;
symbol *sv;
tym_t nty;
unsigned nsize;
targ_size_t noff;
list_t l;
//printf("checkprop: "); WReqn(n); printf("\n");
assert(n && n->Eoper == OPvar);
elem_debug(n);
sv = n->EV.sp.Vsym;
assert(sytab[sv->Sclass] & SCRD);
nty = n->Ety;
if (!tyscalar(nty))
goto noprop;
nsize = size(nty);
noff = n->EV.sp.Voffset;
unambig = sv->Sflags & SFLunambig;
for (l = rdlist; l; l = list_next(l))
{ elem *d = (elem *) list_ptr(l);
elem_debug(d);
//printf("\trd: "); WReqn(d); printf("\n");
if (d->Eoper == OPasm) /* OPasm elems ruin everything */
goto noprop;
#if 0
// Runs afoul of Buzilla 4506
if (OTassign(d->Eoper) && EBIN(d)) // if assignment elem
#else
if (OTassign(d->Eoper)) // if assignment elem
#endif
{ elem *t = Elvalue(d);
if (t->Eoper == OPvar)
{ assert(t->EV.sp.Vsym == sv);
if (d->Eoper == OPstreq ||
!tyscalar(t->Ety))
goto noprop; // not worth bothering with these cases
if (d->Eoper == OPnegass)
goto noprop; // don't bother with this case, either
/* Everything must match or we must skip this variable */
/* (in case of assigning to overlapping unions, etc.) */
if (t->EV.sp.Voffset != noff ||
/* If sizes match, we are ok */
size(t->Ety) != nsize &&
!(d->E2->Eoper == OPconst && size(t->Ety) > nsize && !tyfloating(d->E2->Ety)))
goto noprop;
}
else
{ if (unambig) /* unambiguous assignments only */
continue;
goto noprop;
}
if (d->Eoper != OPeq)
goto noprop;
}
else /* must be a call elem */
{
if (unambig)
continue;
else
goto noprop; /* could be affected */
}
if (d->E2->Eoper == OPconst || d->E2->Eoper == OPrelconst)
{
if (foundelem) /* already found one */
{ /* then they must be the same */
if (!el_match(foundelem,d->E2))
goto noprop;
}
else /* else this is it */
foundelem = d->E2;
}
else
goto noprop;
}
if (foundelem) /* if we got one */
{ /* replace n with foundelem */
#ifdef DEBUG
if (debugc)
{ dbg_printf("const prop (");
WReqn(n);
dbg_printf(" replaced by ");
WReqn(foundelem);
dbg_printf("), %p to %p\n",foundelem,n);
}
#endif
changes++;
return foundelem;
}
noprop:
return NULL;
}
/***********************************
* Find all the reaching defs of OPvar e.
* Put into a linked list, or just set the RD bits in a vector.
*
*/
list_t listrds(vec_t IN,elem *e,vec_t f)
{
unsigned i;
unsigned unambig;
Symbol *s;
unsigned nsize;
targ_size_t noff;
tym_t ty;
//printf("listrds: "); WReqn(e); printf("\n");
assert(IN);
list_t rdlist = NULL;
assert(e->Eoper == OPvar);
s = e->EV.sp.Vsym;
ty = e->Ety;
if (tyscalar(ty))
nsize = size(ty);
noff = e->EV.sp.Voffset;
unambig = s->Sflags & SFLunambig;
if (f)
vec_clear(f);
foreach (i, deftop, IN)
{
elem *d = defnod[i].DNelem;
//dbg_printf("\tlooking at "); WReqn(d); dbg_printf("\n");
unsigned op = d->Eoper;
if (op == OPasm) // assume ASM elems define everything
goto listit;
if (OTassign(op))
{ elem *t = Elvalue(d);
if (t->Eoper == OPvar && t->EV.sp.Vsym == s)
{ if (op == OPstreq)
goto listit;
if (!tyscalar(ty) || !tyscalar(t->Ety))
goto listit;
// If t does not overlap e, then it doesn't affect things
if (noff + nsize > t->EV.sp.Voffset &&
t->EV.sp.Voffset + size(t->Ety) > noff)
goto listit; // it's an assignment to s
}
else if (t->Eoper != OPvar && !unambig)
goto listit; /* assignment through pointer */
}
else if (!unambig)
goto listit; /* probably a function call */
continue;
listit:
//dbg_printf("\tlisting "); WReqn(d); dbg_printf("\n");
if (f)
vec_setbit(i,f);
else
list_prepend(&rdlist,d); // add the definition node
}
return rdlist;
}
/********************************************
* Look at reaching defs for expressions of the form (v == c) and (v != c).
* If all definitions of v are c or are not c, then we can replace the
* expression with 1 or 0.
*/
STATIC void eqeqranges()
{ Elemdata *rel;
list_t rdl;
Symbol *v;
int sz;
elem *e;
targ_llong c;
int result;
for (rel = eqeqlist; rel; rel = rel->next)
{
e = rel->pelem;
v = e->E1->EV.sp.Vsym;
if (!(sytab[v->Sclass] & SCRD))
continue;
sz = tysize(e->E1->Ety);
c = el_tolong(e->E2);
result = -1; // result not known yet
for (rdl = rel->rdlist; rdl; rdl = list_next(rdl))
{ elem *erd = (elem *) list_ptr(rdl);
elem *erd1;
int szrd;
int tmp;
elem_debug(erd);
if (erd->Eoper != OPeq ||
(erd1 = erd->E1)->Eoper != OPvar ||
erd->E2->Eoper != OPconst
)
goto L1;
szrd = tysize(erd1->Ety);
if (erd1->EV.sp.Voffset + szrd <= e->E1->EV.sp.Voffset ||
e->E1->EV.sp.Voffset + sz <= erd1->EV.sp.Voffset)
continue; // doesn't affect us, skip it
if (szrd != sz || e->E1->EV.sp.Voffset != erd1->EV.sp.Voffset)
goto L1; // overlapping - forget it
tmp = (c == el_tolong(erd->E2));
if (result == -1)
result = tmp;
else if (result != tmp)
goto L1;
}
if (result >= 0)
{
//printf("replacing with %d\n",result);
el_free(e->E1);
el_free(e->E2);
e->EV.Vint = (e->Eoper == OPeqeq) ? result : result ^ 1;
e->Eoper = OPconst;
}
L1: ;
}
}
/******************************
* Examine rellist and inclist to determine if any of the signed compare
* elems in rellist can be replace by unsigned compares.
* rellist is list of relationals in function.
* inclist is list of increment elems in function.
*/
STATIC void intranges()
{ Elemdata *rel,*iel;
block *rb,*ib;
symbol *v;
elem *rdeq,*rdinc;
unsigned incop,relatop;
targ_int initial,increment,final;
cmes("intranges()\n");
for (rel = rellist; rel; rel = rel->next)
{
rb = rel->pblock;
//dbg_printf("rel->pelem: "); WReqn(rel->pelem); dbg_printf("\n");
assert(rel->pelem->E1->Eoper == OPvar);
v = rel->pelem->E1->EV.sp.Vsym;
// RD info is only reliable for registers and autos
if (!(sytab[v->Sclass] & SCRD))
continue;
/* Look for two rd's: an = and an increment */
if (list_nitems(rel->rdlist) != 2)
continue;
rdeq = list_elem(list_next(rel->rdlist));
if (rdeq->Eoper != OPeq)
{ rdinc = rdeq;
rdeq = list_elem(rel->rdlist);
if (rdeq->Eoper != OPeq)
continue;
}
else
rdinc = list_elem(rel->rdlist);
#if 0
printf("\neq: "); WReqn(rdeq); printf("\n");
printf("rel: "); WReqn(rel->pelem); printf("\n");
printf("inc: "); WReqn(rdinc); printf("\n");
#endif
incop = rdinc->Eoper;
if (!OTpost(incop) && incop != OPaddass && incop != OPminass)
continue;
/* lvalues should be unambiguous defs */
if (rdeq->E1->Eoper != OPvar || rdinc->E1->Eoper != OPvar)
continue;
/* rvalues should be constants */
if (rdeq->E2->Eoper != OPconst || rdinc->E2->Eoper != OPconst)
continue;
/* Ensure that the only defs reaching the increment elem (rdinc) */
/* are rdeq and rdinc. */
for (iel = inclist; TRUE; iel = iel->next)
{ elem *rd1,*rd2;
if (!iel)
goto nextrel;
ib = iel->pblock;
if (iel->pelem != rdinc)
continue; /* not our increment elem */
if (list_nitems(iel->rdlist) != 2)
{ //printf("!= 2\n");
goto nextrel;
}
rd1 = list_elem(iel->rdlist);
rd2 = list_elem(list_next(iel->rdlist));
/* The rd's for the relational elem (rdeq,rdinc) must be */
/* the same as the rd's for tne increment elem (rd1,rd2). */
if (rd1 == rdeq && rd2 == rdinc || rd1 == rdinc && rd2 == rdeq)
break;
}
// Check that all paths from rdinc to rdinc must pass through rdrel
{ int i;
// ib: block of increment
// rb: block of relational
i = loopcheck(ib,ib,rb);
block_clearvisit();
if (i)
continue;
}
/* Gather initial, increment, and final values for loop */
initial = el_tolong(rdeq->E2);
increment = el_tolong(rdinc->E2);
if (incop == OPpostdec || incop == OPminass)
increment = -increment;
relatop = rel->pelem->Eoper;
final = el_tolong(rel->pelem->E2);
// dbg_printf("initial = %d, increment = %d, final = %d\n",
// initial,increment,final);
/* Determine if we can make the relational an unsigned */
if (initial >= 0)
{ if (final >= initial)
{ if (increment > 0 && ((final - initial) % increment) == 0)
goto makeuns;
}
else if (final >= 0)
{ /* 0 <= final < initial */
if (increment < 0 && ((final - initial) % increment) == 0 &&
!(final + increment < 0 &&
(relatop == OPge || relatop == OPlt)
)
)
{
makeuns:
if (!tyuns(rel->pelem->E2->Ety))
{
rel->pelem->E2->Ety = touns(rel->pelem->E2->Ety);
rel->pelem->Nflags |= NFLtouns;
#ifdef DEBUG
if (debugc)
{ WReqn(rel->pelem);
dbg_printf(" made unsigned, initial = %ld, increment = %ld,\
final = %ld\n",(long int)initial,(long int)increment,(long int)final);
}
#endif
changes++;
}
#if 0
// Eliminate loop if it is empty
if (relatop == OPlt &&
rb->BC == BCiftrue &&
list_block(rb->Bsucc) == rb &&
rb->Belem->Eoper == OPcomma &&
rb->Belem->E1 == rdinc &&
rb->Belem->E2 == rel->pelem
)
{
rel->pelem->Eoper = OPeq;
rel->pelem->Ety = rel->pelem->E1->Ety;
rb->BC = BCgoto;
list_subtract(&rb->Bsucc,rb);
list_subtract(&rb->Bpred,rb);
#ifdef DEBUG
if (debugc)
{ WReqn(rel->pelem);
dbg_printf(" eliminated loop\n");
}
#endif
changes++;
}
#endif
}
}
}
nextrel:
;
}
}
/***********************
* Return TRUE if there is a path from start to inc without
* passing through rel.
*/
STATIC int loopcheck(block *start,block *inc,block *rel)
{ list_t list;
block *b;
#if __ZTC__
_chkstack(); /* always check stack on recursive routines */
#endif
if (!(start->Bflags & BFLvisited))
{ start->Bflags |= BFLvisited; /* guarantee eventual termination */
for (list = start->Bsucc; list; list = list_next(list))
{ b = (block *) list_ptr(list);
if (b != rel && (b == inc || loopcheck(b,inc,rel)))
return TRUE;
}
}
return FALSE;
}
/****************************
* Do copy propagation.
* Copy propagation elems are of the form OPvar=OPvar, and they are
* in expnod[].
*/
static int recalc;
void copyprop()
{ register unsigned i;
out_regcand(&globsym);
cmes("copyprop()\n");
assert(dfo);
Lagain:
flowcp(); /* compute available copy statements */
if (exptop <= 1)
return; /* none available */
#if 0
for (i = 1; i < exptop; i++)
{ dbg_printf("expnod[%d] = (",i);
WReqn(expnod[i]);
dbg_printf(");\n");
}
#endif
recalc = 0;
for (i = 0; i < dfotop; i++) /* for each block */
{ register block *b;
b = dfo[i];
if (b->Belem)
{
#if 0
dbg_printf("B%d, elem (",i);
WReqn(b->Belem); dbg_printf(")\nBin ");
vec_println(b->Bin);
cpwalk(b->Belem,b->Bin);
dbg_printf("Bino ");
vec_println(b->Bin);
dbg_printf("Bout ");
vec_println(b->Bout);
#else
cpwalk(b->Belem,b->Bin);
#endif
/*assert(vec_equal(b->Bin,b->Bout)); */
/* The previous assert() is correct except */
/* for the following case: */
/* a=b; d=a; a=b; */
/* The vectors don't match because the */
/* equations changed to: */
/* a=b; d=b; a=b; */
/* and the d=b copy elem now reaches the end */
/* of the block (the d=a elem didn't). */
}
if (recalc)
goto Lagain;
}
}
/*****************************
* Walk tree n, doing copy propagation as we go.
* Keep IN up to date.
*/
STATIC void cpwalk(register elem *n,vec_t IN)
{ register unsigned op,i;
register elem *t;
vec_t L;
static int nocp;
assert(n && IN);
/*chkvecdim(exptop,0);*/
if (recalc)
return;
op = n->Eoper;
if (op == OPcolon || op == OPcolon2)
{
L = vec_clone(IN);
cpwalk(n->E1,L);
cpwalk(n->E2,IN);
vec_andass(IN,L); // IN = L & R
vec_free(L);
}
else if (op == OPandand || op == OPoror)
{ cpwalk(n->E1,IN);
L = vec_clone(IN);
cpwalk(n->E2,L);
vec_andass(IN,L); // IN = L & R
vec_free(L);
}
else if (OTunary(op))
{
t = n->E1;
if (OTassign(op))
{ if (t->Eoper == OPind)
cpwalk(t->E1,IN);
}
else if (op == OPctor || op == OPdtor)
{
/* This kludge is necessary because in except_pop()
* an el_match is done on the lvalue. If copy propagation
* changes the OPctor but not the corresponding OPdtor,
* then the match won't happen and except_pop()
* will fail.
*/
nocp++;
cpwalk(t,IN);
nocp--;
}
else
cpwalk(t,IN);
}
else if (OTassign(op))
{ cpwalk(n->E2,IN);
t = Elvalue(n);
if (t->Eoper == OPind)
cpwalk(t,IN);
else
{
#ifdef DEBUG
if (t->Eoper != OPvar) elem_print(n);
#endif
assert(t->Eoper == OPvar);
}
}
else if (ERTOL(n))
{ cpwalk(n->E2,IN);
cpwalk(n->E1,IN);
}
else if (OTbinary(op))