-
-
Notifications
You must be signed in to change notification settings - Fork 605
/
gother.d
2123 lines (1896 loc) · 63.8 KB
/
gother.d
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
/**
* Other data flow analysis based optimizations.
*
* Copyright: Copyright (C) 1986-1998 by Symantec
* Copyright (C) 2000-2021 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 http://www.digitalmars.com, Walter Bright)
* License: Distributed under the Boost Software License, Version 1.0.
* http://www.boost.org/LICENSE_1_0.txt
* Source: https://github.com/dlang/dmd/blob/master/src/dmd/backend/gother.d
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/backend/gother.d
* Documentation: https://dlang.org/phobos/dmd_backend_gother.html
*/
module dmd.backend.gother;
version (SCPP)
version = COMPILE;
version (MARS)
version = COMPILE;
version (COMPILE)
{
import core.stdc.stdio;
import core.stdc.stdlib;
import core.stdc.time;
import dmd.backend.cc;
import dmd.backend.cdef;
import dmd.backend.code_x86;
import dmd.backend.oper;
import dmd.backend.global;
import dmd.backend.goh;
import dmd.backend.el;
import dmd.backend.outbuf;
import dmd.backend.symtab;
import dmd.backend.ty;
import dmd.backend.type;
import dmd.backend.barray;
import dmd.backend.dlist;
import dmd.backend.dvec;
nothrow:
@safe:
@trusted
char symbol_isintab(const Symbol *s) { return sytab[s.Sclass] & SCSS; }
extern (C++):
version (SCPP)
import parser;
version (MARS)
import dmd.backend.errors;
/**********************************************************************/
alias Elemdatas = Rarray!(Elemdata);
// Lists to help identify ranges of variables
struct Elemdata
{
nothrow:
elem *pelem; // the elem in question
block *pblock; // which block it's in
Barray!(elem*) rdlist; // list of definition elems for *pelem
/***************************
* Reset memory so this allocation can be re-used.
*/
@trusted
void reset()
{
rdlist.reset();
}
/******************
* Initialize instance at ed.
*/
void emplace(elem *e,block *b)
{
this.pelem = e;
this.pblock = b;
}
}
/********************************
* Find `e` in Elemdata list.
* Params:
* e = elem to find
* Returns:
* Elemdata entry if found,
* null if not
*/
@trusted
Elemdata* find(ref Elemdatas eds, elem *e)
{
foreach (ref edl; eds)
{
if (edl.pelem == e)
return &edl;
}
return null;
}
/*****************
* Free list of Elemdata's.
*/
private void elemdatafree(ref Elemdatas eds)
{
foreach (ref ed; eds)
ed.reset();
eds.reset();
}
private __gshared
{
Elemdatas eqeqlist; // array of Elemdata's of OPeqeq & OPne elems
Elemdatas rellist; // array of Elemdata's of relop elems
Elemdatas inclist; // array of Elemdata's of increment elems
}
/*************************** Constant Propagation ***************************/
/**************************
* Constant propagation.
* Also detects use of variable before any possible def.
*/
@trusted
void constprop()
{
rd_compute();
intranges(rellist, inclist); // compute integer ranges
eqeqranges(eqeqlist); // see if we can eliminate some relationals
elemdatafree(eqeqlist);
elemdatafree(rellist);
elemdatafree(inclist);
}
/************************************
* Compute reaching definitions.
* Note: RD vectors are destroyed by this.
*/
private __gshared block *thisblock;
@trusted
private void rd_compute()
{
if (debugc) printf("constprop()\n");
assert(dfo);
flowrd(); /* compute reaching definitions (rd) */
if (go.defnod.length == 0) /* if no reaching defs */
return;
assert(rellist.length == 0 && inclist.length == 0 && eqeqlist.length == 0);
block_clearvisit();
foreach (b; dfo[]) // for each block
{
switch (b.BC)
{
case BCjcatch:
case BC_finally:
case BC_lpad:
case BCasm:
case BCcatch:
block_visit(b);
break;
default:
break;
}
}
foreach (i, b; dfo[]) // for each block
{
thisblock = b;
//printf("block %d Bin ",i); vec_println(b.Binrd);
//printf(" Bout "); vec_println(b.Boutrd);
if (b.Bflags & BFLvisited)
continue; // not reliable for this block
if (b.Belem)
{
conpropwalk(b.Belem,b.Binrd);
debug
if (!(vec_equal(b.Binrd,b.Boutrd)))
{
int j;
printf("block %d Binrd ", cast(int) i); vec_println(b.Binrd);
printf(" Boutrd "); vec_println(b.Boutrd);
WReqn(b.Belem);
printf("\n");
vec_xorass(b.Binrd,b.Boutrd);
j = cast(int)vec_index(0,b.Binrd);
WReqn(go.defnod[j].DNelem);
printf("\n");
}
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.
*/
@trusted
private void conpropwalk(elem *n,vec_t IN)
{
vec_t L,R;
elem *t;
assert(n && IN);
//printf("conpropwalk()\n"),elem_print(n);
const op = n.Eoper;
if (op == OPcolon || op == OPcolon2)
{
L = vec_clone(IN);
switch (el_returns(n.EV.E1) * 2 | el_returns(n.EV.E2))
{
case 3: // E1 and E2 return
conpropwalk(n.EV.E1,L);
conpropwalk(n.EV.E2,IN);
vec_orass(IN,L); // IN = L | R
break;
case 2: // E1 returns
conpropwalk(n.EV.E1,IN);
conpropwalk(n.EV.E2,L);
break;
case 1: // E2 returns
conpropwalk(n.EV.E1,L);
conpropwalk(n.EV.E2,IN);
break;
case 0: // neither returns
conpropwalk(n.EV.E1,L);
vec_copy(L,IN);
conpropwalk(n.EV.E2,L);
break;
default:
break;
}
vec_free(L);
}
else if (op == OPandand || op == OPoror)
{
conpropwalk(n.EV.E1,IN);
R = vec_clone(IN);
conpropwalk(n.EV.E2,R);
if (el_returns(n.EV.E2))
vec_orass(IN,R); // IN |= R
vec_free(R);
}
else if (OTunary(op))
goto L3;
else if (ERTOL(n))
{
conpropwalk(n.EV.E2,IN);
L3:
t = n.EV.E1;
if (OTassign(op))
{
if (t.Eoper == OPvar)
{
// Note that the following ignores OPnegass
if (OTopeq(op) && sytab[t.EV.Vsym.Sclass] & SCRD)
{
Barray!(elem*) rdl;
listrds(IN,t,null,&rdl);
if (!(config.flags & CFGnowarning)) // if warnings are enabled
chkrd(t,rdl);
if (auto e = chkprop(t,rdl))
{ // Replace (t op= exp) with (t = e op exp)
e = el_copytree(e);
e.Ety = t.Ety;
n.EV.E2 = el_bin(opeqtoop(op),n.Ety,e,n.EV.E2);
n.Eoper = OPeq;
}
rdl.dtor();
}
}
else
conpropwalk(t,IN);
}
else
conpropwalk(t,IN);
}
else if (OTbinary(op))
{
if (OTassign(op))
{ t = n.EV.E1;
if (t.Eoper != OPvar)
conpropwalk(t,IN);
}
else
conpropwalk(n.EV.E1,IN);
conpropwalk(n.EV.E2,IN);
}
// Collect data for subsequent optimizations
if (OTbinary(op) && n.EV.E1.Eoper == OPvar && n.EV.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 (tyintegral(n.EV.E1.Ety) &&
tyintegral(n.EV.E2.Ety)
)
{
//printf("appending to rellist\n"); elem_print(n);
//printf("\trellist IN: "); vec_print(IN); printf("\n");
auto pdata = rellist.push();
pdata.emplace(n,thisblock);
listrds(IN, n.EV.E1, null, &pdata.rdlist);
}
break;
case OPaddass:
case OPminass:
case OPpostinc:
case OPpostdec:
// Collect increment elems and their rd's in the inclist list
if (tyintegral(n.EV.E1.Ety))
{
//printf("appending to inclist\n"); elem_print(n);
//printf("\tinclist IN: "); vec_print(IN); printf("\n");
auto pdata = inclist.push();
pdata.emplace(n,thisblock);
listrds(IN, n.EV.E1, null, &pdata.rdlist);
}
break;
case OPne:
case OPeqeq:
// Collect compare elems and their rd's in the rellist list
if (tyintegral(n.EV.E1.Ety))
{ //printf("appending to eqeqlist\n"); elem_print(n);
auto pdata = eqeqlist.push();
pdata.emplace(n,thisblock);
listrds(IN, n.EV.E1, null, &pdata.rdlist);
}
break;
default:
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.Vsym.Sclass] & SCRD)
{
//printf("const prop: %s\n", n.EV.Vsym.Sident);
Barray!(elem*) rdl;
listrds(IN,n,null,&rdl);
if (!(config.flags & CFGnowarning)) // if warnings are enabled
chkrd(n,rdl);
elem *e = chkprop(n,rdl);
if (e)
{ tym_t nty;
nty = n.Ety;
el_copy(n,e);
n.Ety = nty; // retain original type
}
rdl.dtor();
}
}
/******************************
* Give error if there are no reaching defs for variable v.
*/
@trusted
private void chkrd(elem *n, Barray!(elem*) rdlist)
{
Symbol *sv;
int unambig;
sv = n.EV.Vsym;
assert(sytab[sv.Sclass] & SCRD);
if (sv.Sflags & SFLnord) // if already printed a warning
return;
if (sv.ty() & (mTYvolatile | mTYshared))
return;
unambig = sv.Sflags & SFLunambig;
foreach (d; rdlist)
{
elem_debug(d);
if (d.Eoper == OPasm) /* OPasm elems ruin everything */
return;
if (OTassign(d.Eoper))
{
if (d.EV.E1.Eoper == OPvar)
{
if (d.EV.E1.EV.Vsym == sv)
return;
}
else if (!unambig)
return;
}
else
{
if (!unambig)
return;
}
}
// If there are any asm blocks, don't print the message
foreach (b; dfo[])
if (b.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;
}
static 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;
}
}
version (SCPP)
{
{ Outbuffer buf;
char *p2;
type_tostring(&buf, sv.Stype);
buf.writeByte(' ');
buf.write(sv.Sident.ptr);
p2 = buf.toString();
warerr(WM.WM_used_b4_set, p2); // variable used before set
}
}
version (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.ptr);
}
}
sv.Sflags |= SFLnord; // no redundant messages
//elem_print(n);
}
/**********************************
* 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
*/
@trusted
private elem * chkprop(elem *n, Barray!(elem*) rdlist)
{
elem *foundelem = null;
int unambig;
Symbol *sv;
tym_t nty;
uint nsize;
targ_size_t noff;
//printf("checkprop: "); WReqn(n); printf("\n");
assert(n && n.Eoper == OPvar);
elem_debug(n);
sv = n.EV.Vsym;
assert(sytab[sv.Sclass] & SCRD);
nty = n.Ety;
if (!tyscalar(nty))
goto noprop;
nsize = cast(uint)size(nty);
noff = n.EV.Voffset;
unambig = sv.Sflags & SFLunambig;
foreach (d; rdlist)
{
elem_debug(d);
//printf("\trd: "); WReqn(d); printf("\n");
if (d.Eoper == OPasm) /* OPasm elems ruin everything */
goto noprop;
// Runs afoul of Buzilla 4506
/*if (OTassign(d.Eoper) && EBIN(d))*/ // if assignment elem
if (OTassign(d.Eoper)) // if assignment elem
{
elem *t = d.EV.E1;
if (t.Eoper == OPvar)
{
assert(t.EV.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.Voffset != noff ||
/* If sizes match, we are ok */
size(t.Ety) != nsize &&
!(d.EV.E2.Eoper == OPconst && size(t.Ety) > nsize && !tyfloating(d.EV.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.EV.E2.Eoper == OPconst || d.EV.E2.Eoper == OPrelconst)
{
if (foundelem) /* already found one */
{ /* then they must be the same */
if (!el_match(foundelem,d.EV.E2))
goto noprop;
}
else /* else this is it */
foundelem = d.EV.E2;
}
else
goto noprop;
}
if (foundelem) /* if we got one */
{ /* replace n with foundelem */
debug if (debugc)
{
printf("const prop (");
WReqn(n);
printf(" replaced by ");
WReqn(foundelem);
printf("), %p to %p\n",foundelem,n);
}
go.changes++;
return foundelem;
}
noprop:
return null;
}
/***********************************
* Find all the reaching defs of OPvar e.
* Params:
* IN = vector of definition nodes
* e = OPvar
* f = if not null, set RD bits in it
* rdlist = if not null, append reaching defs to it
*/
@trusted
void listrds(vec_t IN,elem *e,vec_t f, Barray!(elem*)* rdlist)
{
uint i;
uint unambig;
Symbol *s;
uint nsize;
targ_size_t noff;
tym_t ty;
//printf("listrds: "); WReqn(e); printf("\n");
assert(IN);
assert(e.Eoper == OPvar);
s = e.EV.Vsym;
ty = e.Ety;
if (tyscalar(ty))
nsize = cast(uint)size(ty);
noff = e.EV.Voffset;
unambig = s.Sflags & SFLunambig;
if (f)
vec_clear(f);
for (i = 0; (i = cast(uint) vec_index(i, IN)) < go.defnod.length; ++i)
{
elem *d = go.defnod[i].DNelem;
//printf("\tlooking at "); WReqn(d); printf("\n");
const op = d.Eoper;
if (op == OPasm) // assume ASM elems define everything
goto listit;
if (OTassign(op))
{ elem *t = d.EV.E1;
if (t.Eoper == OPvar && t.EV.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.Voffset &&
t.EV.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:
//printf("\tlisting "); WReqn(d); printf("\n");
if (f)
vec_setbit(i,f);
else
(*rdlist).push(d); // add the definition node
}
}
/********************************************
* 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.
* Params:
* eqeqlist = array of == and != expressions
*/
@trusted
private void eqeqranges(ref Elemdatas eqeqlist)
{
Symbol *v;
int sz;
elem *e;
targ_llong c;
int result;
foreach (ref rel; eqeqlist)
{
e = rel.pelem;
v = e.EV.E1.EV.Vsym;
if (!(sytab[v.Sclass] & SCRD))
continue;
sz = tysize(e.EV.E1.Ety);
c = el_tolong(e.EV.E2);
result = -1; // result not known yet
foreach (erd; rel.rdlist)
{
elem *erd1;
int szrd;
int tmp;
elem_debug(erd);
if (erd.Eoper != OPeq ||
(erd1 = erd.EV.E1).Eoper != OPvar ||
erd.EV.E2.Eoper != OPconst
)
goto L1;
szrd = tysize(erd1.Ety);
if (erd1.EV.Voffset + szrd <= e.EV.E1.EV.Voffset ||
e.EV.E1.EV.Voffset + sz <= erd1.EV.Voffset)
continue; // doesn't affect us, skip it
if (szrd != sz || e.EV.E1.EV.Voffset != erd1.EV.Voffset)
goto L1; // overlapping - forget it
tmp = (c == el_tolong(erd.EV.E2));
if (result == -1)
result = tmp;
else if (result != tmp)
goto L1;
}
if (result >= 0)
{
//printf("replacing with %d\n",result);
el_free(e.EV.E1);
el_free(e.EV.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.
* Params:
* rellist = array of relationals in function
* inclist = array of increment elems in function
*/
@trusted
private void intranges(ref Elemdatas rellist, ref Elemdatas inclist)
{
block *rb;
block *ib;
Symbol *v;
elem *rdeq;
elem *rdinc;
uint incop,relatop;
targ_llong initial,increment,final_;
if (debugc) printf("intranges()\n");
static if (0)
{
foreach (int i, ref rel; rellist)
{
printf("[%d] rel.pelem: ", i); WReqn(rel.pelem); printf("\n");
}
}
foreach (ref rel; rellist)
{
rb = rel.pblock;
//printf("rel.pelem: "); WReqn(rel.pelem); printf("\n");
assert(rel.pelem.EV.E1.Eoper == OPvar);
v = rel.pelem.EV.E1.EV.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 (rel.rdlist.length != 2)
continue;
rdeq = rel.rdlist[1];
if (rdeq.Eoper != OPeq)
{ rdinc = rdeq;
rdeq = rel.rdlist[0];
if (rdeq.Eoper != OPeq)
continue;
}
else
rdinc = rel.rdlist[0];
static if (0)
{
printf("\neq: "); WReqn(rdeq); printf("\n");
printf("rel: "); WReqn(rel.pelem); printf("\n");
printf("inc: "); WReqn(rdinc); printf("\n");
}
incop = rdinc.Eoper;
if (!OTpost(incop) && incop != OPaddass && incop != OPminass)
continue;
/* lvalues should be unambiguous defs */
if (rdeq.EV.E1.Eoper != OPvar || rdinc.EV.E1.Eoper != OPvar)
continue;
/* rvalues should be constants */
if (rdeq.EV.E2.Eoper != OPconst || rdinc.EV.E2.Eoper != OPconst)
continue;
/* Ensure that the only defs reaching the increment elem (rdinc) */
/* are rdeq and rdinc. */
foreach (ref iel; inclist)
{
elem *rd1;
elem *rd2;
ib = iel.pblock;
if (iel.pelem != rdinc)
continue; /* not our increment elem */
if (iel.rdlist.length != 2)
{
//printf("!= 2\n");
break;
}
rd1 = iel.rdlist[0];
rd2 = iel.rdlist[1];
/* 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)
goto found;
}
goto nextrel;
found:
// 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.EV.E2);
increment = el_tolong(rdinc.EV.E2);
if (incop == OPpostdec || incop == OPminass)
increment = -increment;
relatop = rel.pelem.Eoper;
final_ = el_tolong(rel.pelem.EV.E2);
//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_ == 0 && relatop == OPge)
{
/* if the relation is (i >= 0) there is likely some dependency
* on switching sign, so do not make it unsigned
*/
}
else 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.EV.E2.Ety))
{
rel.pelem.EV.E2.Ety = touns(rel.pelem.EV.E2.Ety);
rel.pelem.Nflags |= NFLtouns;
debug
if (debugc)
{ WReqn(rel.pelem);
printf(" made unsigned, initial = %lld, increment = %lld," ~
" final_ = %lld\n",cast(long)initial,cast(long)increment,cast(long)final_);
}
go.changes++;
}
static 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.EV.E1 == rdinc &&
rb.Belem.EV.E2 == rel.pelem
)
{
rel.pelem.Eoper = OPeq;
rel.pelem.Ety = rel.pelem.EV.E1.Ety;
rb.BC = BCgoto;
list_subtract(&rb.Bsucc,rb);
list_subtract(&rb.Bpred,rb);
debug
if (debugc)
{
WReqn(rel.pelem);
printf(" eliminated loop\n");
}
go.changes++;
}
}
}
}
}
nextrel:
}
}
/******************************
* Look for initialization and increment expressions in loop.
* Very similar to intranges().
* Params:
* rellist = list of relationals in function
* inclist = list of increment elems in function.
* erel = loop compare expression of the form (v < c)
* rdeq = set to loop initialization of v
* rdinc = set to loop increment of v
* Returns:
* false if cannot find rdeq or rdinc
*/
@trusted
private bool returnResult(bool result)
{
elemdatafree(eqeqlist);
elemdatafree(rellist);
elemdatafree(inclist);
return result;
}
@trusted
bool findloopparameters(elem* erel, ref elem* rdeq, ref elem* rdinc)
{
if (debugc) printf("findloopparameters()\n");
const bool log = false;
assert(erel.EV.E1.Eoper == OPvar);
Symbol* v = erel.EV.E1.EV.Vsym;
// RD info is only reliable for registers and autos
if (!(sytab[v.Sclass] & SCRD))
return false;
rd_compute(); // compute rellist, inclist, eqeqlist
/* Find `erel` in `rellist`
*/
Elemdata* rel = rellist.find(erel);
if (!rel)
{
if (log) printf("\trel not found\n");
return returnResult(false);
}
block* rb = rel.pblock;
//printf("rel.pelem: "); WReqn(rel.pelem); printf("\n");
// Look for one reaching definition: an increment
if (rel.rdlist.length != 1)
{
if (log) printf("\tnitems = %d\n", cast(int)rel.rdlist.length);
return returnResult(false);
}
rdinc = rel.rdlist[0];
static if (0)
{
printf("\neq: "); WReqn(rdeq); printf("\n");
printf("rel: "); WReqn(rel.pelem); printf("\n");
printf("inc: "); WReqn(rdinc); printf("\n");
}