/
gdag.d
958 lines (872 loc) · 28 KB
/
gdag.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
/**
* Compiler implementation of the
* $(LINK2 http://www.dlang.org, D programming language).
*
* Copyright: Copyright (C) 1986-1998 by Symantec
* Copyright (C) 2000-2020 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 http://www.digitalmars.com, Walter Bright)
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/backend/gdag.d, backend/gdag.d)
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/backend/gdag.d
*/
module dmd.backend.gdag;
version (SCPP)
version = COMPILE;
version (MARS)
version = COMPILE;
version (COMPILE)
{
import core.stdc.stdio;
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.ty;
import dmd.backend.type;
import dmd.backend.dlist;
import dmd.backend.dvec;
extern (C++):
nothrow:
enum Aetype { cse, arraybounds }
private __gshared Aetype aetype;
bool Eunambig(elem* e) { return OTassign(e.Eoper) && e.EV.E1.Eoper == OPvar; }
/*************************************
* Determine if floating point should be cse'd.
* Returns:
* true if should be cse'd
*/
private bool cse_float(elem *e)
{
// Don't CSE floating stuff if generating
// inline 8087 code, the code generator
// can't handle it yet
return !(tyfloating(e.Ety) && config.inline8087 &&
e.Eoper != OPvar && e.Eoper != OPconst) ||
(tyxmmreg(e.Ety) && config.fpxmmregs);
}
/************************************
* Build DAGs (basically find all the common subexpressions).
* Must be done after all other optimizations, because most
* of them depend on the trees being trees, not DAGs.
* The general strategy is:
* Compute available expressions (AEs)
* For each block
* stick together nodes that match, keeping AEs up to date
* For each block
* unstick unprofitable common subexpressions
* (this is generally target-dependent)
*/
void builddags()
{
vec_t aevec;
debug if (debugc) printf("builddags()\n");
assert(dfo);
flowae(); /* compute available expressions */
if (go.exptop <= 1) /* if no AEs */
return;
aetype = Aetype.cse;
debug
foreach (i, e; go.expnod[])
{
//printf("go.expnod[%d] = %p\n",i,e);
if (e)
elem_debug(e);
}
static if (0)
{
printf("defkill "); vec_println(go.defkill,go.exptop);
printf("starkill "); vec_println(go.starkill,go.exptop);
printf("vptrkill "); vec_println(go.vptrkill,go.exptop);
}
static if (0)
{
/* This is the 'correct' algorithm for CSEs. We can't use it */
/* till we fix the code generator. */
foreach (i, b; dfo[])
{
if (b.Belem)
{
static if (0)
{
printf("dfo[%d] = %p\n",i,b);
printf("b.Bin "); vec_println(b.Bin,go.exptop);
printf("b.Bout "); vec_println(b.Bout,go.exptop);
aewalk(&(b.Belem),b.Bin);
printf("b.Bin "); vec_println(b.Bin,go.exptop);
printf("b.Bout "); vec_println(b.Bout,go.exptop);
}
else
{
aewalk(&(b.Belem),b.Bin);
}
/* Bin and Bout would be equal at this point */
/* except that we deleted some elems from */
/* go.expnod[] and so it's a subset of Bout */
/* assert(veceq(b.Bin,b.Bout)); */
}
}
}
else
{
/* Do CSEs across extended basic blocks only. This is because */
/* the code generator can only track register contents */
/* properly across extended basic blocks. */
aevec = vec_calloc(go.exptop);
foreach (i, b; dfo[])
{
/* if not first block and (there are more than one */
/* predecessor or the only predecessor is not the */
/* previous block), then zero out the available */
/* expressions. */
if ((i != 0 &&
(list_block(b.Bpred) != dfo[i - 1] ||
list_next(b.Bpred) != null))
|| b.BC == BCasm
|| b.BC == BC_finally
|| b.BC == BC_lpad
|| b.BC == BCcatch
|| b.BC == BCjcatch
)
vec_clear(aevec);
if (b.Belem) /* if there is an expression */
aewalk(&(b.Belem),aevec);
}
vec_free(aevec);
}
// Need 2 passes to converge on solution
foreach (j; 0 .. 2)
foreach (b; dfo[])
{
if (b.Belem)
{
//printf("b = 0x%x\n",b);
removecses(&(b.Belem));
}
}
}
/****************************
* Walk tree, rewriting *pn into a DAG as we go.
* Params:
* pn = pointer to expression tree to convert to DAG
* ae = vector of available expressions
*/
private void aewalk(elem **pn,vec_t ae)
{
elem* n = *pn;
assert(n && ae);
//printf("visiting %d: (",n.Eexp); WReqn(*pn); printf(")\n");
//chkvecdim(go.exptop);
const op = n.Eoper;
if (n.Eexp) // if an AE
{ // Try to find an equivalent AE, and point to it instead
assert(go.expnod[n.Eexp] == n);
if (aetype == Aetype.cse)
{
for (uint i = 0; (i = cast(uint) vec_index(i, ae)) < go.exptop; ++i)
{ elem *e = go.expnod[i];
// Attempt to replace n with e
if (e == null) // if elem no longer exists
vec_clearbit(i,ae); // it's not available
else if (n != e &&
el_match(n,e) &&
e.Ecount < 0xFF-1 && // must fit in unsigned char
cse_float(n)
)
{
*pn = e; // replace n with e
//printf("cse: %p (",n); WReqn(*pn); printf(")\n");
e.Ecount++;
debug assert(e.Ecount != 0);
void aeclear(elem *n)
{
while (1)
{
const i = n.Eexp;
assert(i);
if (n.Ecount)
break;
go.expnod[i] = null;
vec_clearbit(i,ae);
if (OTunary(n.Eoper))
{
n = n.EV.E1;
continue;
}
else if (OTbinary(n.Eoper))
{
aeclear(n.EV.E1);
n = n.EV.E2;
continue;
}
break;
}
}
aeclear(n);
el_free(n);
return;
}
}
}
}
elem *t;
switch (op)
{
case OPcolon:
case OPcolon2:
{
// ae = ae & ael & aer
// AEs gened by ael and aer are mutually exclusive
vec_t aer = vec_clone(ae);
aewalk(&(n.EV.E1),ae);
aewalk(&(n.EV.E2),aer);
vec_andass(ae,aer);
vec_free(aer);
break;
}
case OPandand:
case OPoror:
{
aewalk(&(n.EV.E1),ae);
/* ae &= aer */
vec_t aer = vec_clone(ae);
aewalk(&(n.EV.E2),aer);
if (el_returns(n.EV.E2))
vec_andass(ae,aer);
vec_free(aer);
break;
}
case OPnegass:
t = n.EV.E1;
if (t.Eoper == OPind)
aewalk(&(t.EV.E1),ae);
break;
case OPctor:
case OPdtor:
case OPdctor:
break;
case OPasm:
case OPddtor:
vec_clear(ae); // kill everything
return;
default:
if (OTbinary(op))
{
if (ERTOL(n))
{
// Don't CSE constants that will turn into
// an INC or DEC anyway
if (n.EV.E2.Eoper == OPconst &&
n.EV.E2.EV.Vint == 1 &&
(op == OPaddass || op == OPminass ||
op == OPpostinc || op == OPpostdec)
)
{ }
else
aewalk(&(n.EV.E2),ae);
}
if (OTassign(op))
{
t = n.EV.E1;
if (t.Eoper == OPind)
aewalk(&(t.EV.E1),ae);
}
else
aewalk(&(n.EV.E1),ae);
if (!ERTOL(n))
aewalk(&(n.EV.E2),ae);
}
else if (OTunary(op))
{
assert(op != OPnegass);
aewalk(&(n.EV.E1),ae);
}
}
if (OTdef(op))
{
assert(n.Eexp == 0); // should not be an AE
/* remove all AEs that could be affected by this def */
if (Eunambig(n)) // if unambiguous definition
{
assert(t.Eoper == OPvar);
Symbol* s = t.EV.Vsym;
if (!(s.Sflags & SFLunambig))
vec_subass(ae,go.starkill);
for (uint i = 0; (i = cast(uint) vec_index(i, ae)) < go.exptop; ++i) // for each ae elem
{
elem *e = go.expnod[i];
if (!e) continue;
if (OTunary(e.Eoper))
{
if (vec_testbit(e.EV.E1.Eexp,ae))
continue;
}
else if (OTbinary(e.Eoper))
{
if (vec_testbit(e.EV.E1.Eexp,ae) &&
vec_testbit(e.EV.E2.Eexp,ae))
continue;
}
else if (e.Eoper == OPvar)
{
if (e.EV.Vsym != s)
continue;
}
else
continue;
vec_clearbit(i,ae);
}
}
else /* else ambiguous definition */
{
vec_subass(ae,go.defkill);
if (OTcalldef(op))
vec_subass(ae,go.vptrkill);
}
// GEN the lvalue of an assignment operator
if (OTassign(op) && !OTpost(op) && t.Eexp)
vec_setbit(t.Eexp,ae);
}
if (n.Eexp) // if an AE
{
if (op == OPvp_fp || op == OPcvp_fp)
/* Invalidate all other OPvp_fps */
vec_subass(ae,go.vptrkill);
/*printf("available: ("); WReqn(n); printf(")\n");
elem_print(n);*/
vec_setbit(n.Eexp,ae); /* mark this elem as available */
}
}
/**************************
* Remove a CSE.
* Input:
* pe pointer to pointer to CSE
* Output:
* *pe new elem to replace the old
* Returns:
* *pe
*/
private elem * delcse(elem **pe)
{
elem *e;
e = el_calloc();
el_copy(e,*pe);
debug if (debugc)
{
printf("deleting unprofitable CSE %p (", *pe);
WReqn(e);
printf(")\n");
}
assert(e.Ecount != 0);
if (!OTleaf(e.Eoper))
{
if (e.EV.E1.Ecount == 0xFF-1)
{
elem *ereplace;
ereplace = el_calloc();
el_copy(ereplace,e.EV.E1);
e.EV.E1 = ereplace;
ereplace.Ecount = 0;
}
else
{
e.EV.E1.Ecount++;
debug assert(e.EV.E1.Ecount != 0);
}
if (OTbinary(e.Eoper))
{
if (e.EV.E2.Ecount == 0xFF-1)
{
elem *ereplace;
ereplace = el_calloc();
el_copy(ereplace,e.EV.E2);
e.EV.E2 = ereplace;
ereplace.Ecount = 0;
}
else
{
e.EV.E2.Ecount++;
debug assert(e.EV.E2.Ecount != 0);
}
}
}
--(*pe).Ecount;
debug assert((*pe).Ecount != 0xFF);
(*pe).Nflags |= NFLdelcse; // not generating node
e.Ecount = 0;
*pe = e;
return *pe;
}
/******************************
* 'Unstick' CSEs that would be unprofitable to do. These are usually
* things like addressing modes, and are usually target-dependent.
*/
private void removecses(elem **pe)
{
L1:
elem* e = *pe;
//printf(" removecses(%p) ", e); WReqn(e); printf("\n");
assert(e);
elem_debug(e);
if (e.Nflags & NFLdelcse && e.Ecount)
{
delcse(pe);
goto L1;
}
const op = e.Eoper;
if (OTunary(op))
{
if (op == OPind)
{
bool scaledIndex = I32 || I64; // if scaled index addressing mode support
elem *e1 = e.EV.E1;
if (e1.Eoper == OPadd &&
e1.Ecount
)
{
if (scaledIndex)
{
e1 = delcse(&e.EV.E1);
if (e1.EV.E1.Ecount) // == 1)
delcse(&e1.EV.E1);
if (e1.EV.E2.Ecount && e1.EV.E2.Eoper != OPind)
delcse(&e1.EV.E2);
}
/* *(v +. c)
* *(*pc +. c)
* The + and the const shouldn't be CSEs.
*/
else if (e1.EV.E2.Eoper == OPconst &&
(e1.EV.E1.Eoper == OPvar || (e1.EV.E1.Eoper == OPind && e1.EV.E1.Ety & (mTYconst | mTYimmutable)))
)
{
e1 = delcse(&e.EV.E1);
}
}
/* *(((e <<. 3) + e) + e)
*/
if (scaledIndex && e1.Eoper == OPadd &&
e1.EV.E1.Eoper == OPadd &&
e1.EV.E1.EV.E1.Ecount &&
e1.EV.E1.EV.E1.Eoper == OPshl &&
e1.EV.E1.EV.E1.EV.E2.Eoper == OPconst &&
e1.EV.E1.EV.E1.EV.E2.EV.Vuns <= 3
)
{
delcse(&e1.EV.E1.EV.E1); // the <<. operator
}
/* *(((e << 3) +. e) + e)
*/
if (scaledIndex && e1.Eoper == OPadd &&
e1.EV.E1.Eoper == OPadd &&
e1.EV.E1.Ecount &&
e1.EV.E1.EV.E1.Eoper == OPshl &&
e1.EV.E1.EV.E1.EV.E2.Eoper == OPconst &&
e1.EV.E1.EV.E1.EV.E2.EV.Vuns <= 3
)
{
delcse(&e1.EV.E1); // the +. operator
}
/* *((e <<. 3) + e)
*/
else if (scaledIndex && e1.Eoper == OPadd &&
e1.EV.E1.Ecount &&
e1.EV.E1.Eoper == OPshl &&
e1.EV.E1.EV.E2.Eoper == OPconst &&
e1.EV.E1.EV.E2.EV.Vuns <= 3
)
{
delcse(&e1.EV.E1); // the <<. operator
}
// Remove *e1 where it's a double
if (e.Ecount && tyfloating(e.Ety))
e = delcse(pe);
}
// This CSE is too easy to regenerate
else if (op == OPu16_32 && I16 && e.Ecount)
e = delcse(pe);
else if (op == OPd_ld && e.EV.E1.Ecount > 0)
delcse(&e.EV.E1);
// OPremquo is only worthwhile if its result is used more than once
else if (e.EV.E1.Eoper == OPremquo &&
(op == OP64_32 || op == OP128_64 || op == OPmsw) &&
e.EV.E1.Ecount == 0)
{ // Convert back to OPdiv or OPmod
elem *e1 = e.EV.E1;
e.Eoper = (op == OPmsw) ? OPmod : OPdiv;
e.EV.E1 = e1.EV.E1;
e.EV.E2 = e1.EV.E2;
e1.EV.E1 = null;
e1.EV.E2 = null;
el_free(e1);
removecses(&(e.EV.E1));
pe = &(e.EV.E2);
goto L1;
}
}
else if (OTbinary(op))
{
if (e.Ecount > 0 && OTrel(op) && e.Ecount < 4
/* Don't CSE floating stuff if generating */
/* inline 8087 code, the code generator */
/* can't handle it yet */
&& !(tyfloating(e.EV.E1.Ety) && config.inline8087)
)
e = delcse(pe);
if (ERTOL(e))
{
removecses(&(e.EV.E2));
pe = &(e.EV.E1);
}
else
{
removecses(&(e.EV.E1));
pe = &(e.EV.E2);
}
goto L1;
}
else /* leaf node */
{
return;
}
pe = &(e.EV.E1);
goto L1;
}
/*****************************************
* Do optimizations based on if we know an expression is
* 0 or !=0, even though we don't know anything else.
*/
void boolopt()
{
vec_t aevec;
vec_t aevecval;
debug if (debugc) printf("boolopt()\n");
if (!dfo.length)
compdfo();
flowae(); /* compute available expressions */
if (go.exptop <= 1) /* if no AEs */
return;
static if (0)
{
for (uint i = 0; i < go.exptop; i++)
printf("go.expnod[%d] = 0x%x\n",i,go.expnod[i]);
printf("defkill "); vec_println(go.defkill,go.exptop);
printf("starkill "); vec_println(go.starkill,go.exptop);
printf("vptrkill "); vec_println(go.vptrkill,go.exptop);
}
/* Do CSEs across extended basic blocks only. This is because */
/* the code generator can only track register contents */
/* properly across extended basic blocks. */
aevec = vec_calloc(go.exptop);
aevecval = vec_calloc(go.exptop);
// Mark each expression that we know starts off with a non-zero value
for (uint i = 0; i < go.exptop; i++)
{
elem *e = go.expnod[i];
if (e)
{
elem_debug(e);
if (e.Eoper == OPvar && e.EV.Vsym.Sflags & SFLtrue)
{
vec_setbit(i,aevec);
vec_setbit(i,aevecval);
}
}
}
foreach (i, b; dfo[])
{
/* if not first block and (there are more than one */
/* predecessor or the only predecessor is not the */
/* previous block), then zero out the available */
/* expressions. */
if ((i != 0 &&
(list_block(b.Bpred) != dfo[i - 1] ||
list_next(b.Bpred) != null))
|| b.BC == BCasm
|| b.BC == BC_finally
|| b.BC == BC_lpad
|| b.BC == BCcatch
|| b.BC == BCjcatch
)
vec_clear(aevec);
if (b.Belem) /* if there is an expression */
abewalk(b.Belem,aevec,aevecval);
}
vec_free(aevec);
vec_free(aevecval);
}
/****************************
* Walk tree, replacing bool expressions that we know
* ae = vector of available boolean expressions
* aeval = parallel vector of values corresponding to whether bool
* value is 1 or 0
* n = elem tree to look at
*/
private void abewalk(elem *n,vec_t ae,vec_t aeval)
{
elem *t;
assert(n && ae);
elem_debug(n);
/*printf("visiting: ("); WReqn(*pn); printf("), Eexp = %d\n",n.Eexp);*/
/*chkvecdim(go.exptop);*/
const op = n.Eoper;
switch (op)
{
case OPcond:
{
assert(n.EV.E2.Eoper == OPcolon || n.EV.E2.Eoper == OPcolon2);
abewalk(n.EV.E1,ae,aeval);
abeboolres(n.EV.E1,ae,aeval);
vec_t aer = vec_clone(ae);
vec_t aerval = vec_clone(aeval);
if (!el_returns(n.EV.E2.EV.E1))
{
abeset(n.EV.E1,aer,aerval,true);
abewalk(n.EV.E2.EV.E1,aer,aerval);
abeset(n.EV.E1,ae,aeval,false);
abewalk(n.EV.E2.EV.E2,ae,aeval);
}
else if (!el_returns(n.EV.E2.EV.E2))
{
abeset(n.EV.E1,ae,aeval,true);
abewalk(n.EV.E2.EV.E1,ae,aeval);
abeset(n.EV.E1,aer,aerval,false);
abewalk(n.EV.E2.EV.E2,aer,aerval);
}
else
{
/* ae = ae & ael & aer
* AEs gened by ael and aer are mutually exclusive
*/
abeset(n.EV.E1,aer,aerval,true);
abewalk(n.EV.E2.EV.E1,aer,aerval);
abeset(n.EV.E1,ae,aeval,false);
abewalk(n.EV.E2.EV.E2,ae,aeval);
vec_xorass(aerval,aeval);
vec_subass(aer,aerval);
vec_andass(ae,aer);
}
vec_free(aer);
vec_free(aerval);
break;
}
case OPcolon:
case OPcolon2:
assert(0);
case OPandand:
case OPoror:
{
//printf("test1 %p: ", n); WReqn(n); printf("\n");
abewalk(n.EV.E1,ae,aeval);
abeboolres(n.EV.E1,ae,aeval);
vec_t aer = vec_clone(ae);
vec_t aerval = vec_clone(aeval);
if (!el_returns(n.EV.E2))
{
abeset(n.EV.E1,aer,aerval,(op == OPandand));
abewalk(n.EV.E2,aer,aerval);
abeset(n.EV.E1,ae,aeval,(op != OPandand));
}
else
{
/* ae &= aer
*/
abeset(n.EV.E1,aer,aerval,(op == OPandand));
abewalk(n.EV.E2,aer,aerval);
vec_xorass(aerval,aeval);
vec_subass(aer,aerval);
vec_andass(ae,aer);
}
vec_free(aer);
vec_free(aerval);
break;
}
case OPbool:
case OPnot:
abewalk(n.EV.E1,ae,aeval);
abeboolres(n.EV.E1,ae,aeval);
break;
case OPeqeq:
case OPne:
case OPlt:
case OPle:
case OPgt:
case OPge:
case OPunord: case OPlg: case OPleg: case OPule:
case OPul: case OPuge: case OPug: case OPue:
case OPngt: case OPnge: case OPnlt: case OPnle:
case OPord: case OPnlg: case OPnleg: case OPnule:
case OPnul: case OPnuge: case OPnug: case OPnue:
abewalk(n.EV.E1,ae,aeval);
abewalk(n.EV.E2,ae,aeval);
abeboolres(n,ae,aeval);
break;
case OPnegass:
t = n.EV.E1;
if (t.Eoper == OPind)
abewalk(t.EV.E1,ae,aeval);
break;
case OPasm:
vec_clear(ae); // kill everything
return;
default:
if (OTbinary(op))
{ if (ERTOL(n))
abewalk(n.EV.E2,ae,aeval);
if (OTassign(op))
{ t = n.EV.E1;
if (t.Eoper == OPind)
abewalk(t.EV.E1,ae,aeval);
}
else
abewalk(n.EV.E1,ae,aeval);
if (!ERTOL(n))
abewalk(n.EV.E2,ae,aeval);
}
else if (OTunary(op))
abewalk(n.EV.E1,ae,aeval);
break;
}
if (OTdef(op))
{
assert(n.Eexp == 0); // should not be an AE
/* remove all AEs that could be affected by this def */
if (Eunambig(n)) /* if unambiguous definition */
{
Symbol *s;
assert(t.Eoper == OPvar);
s = t.EV.Vsym;
if (!(s.Sflags & SFLunambig))
vec_subass(ae,go.starkill);
for (uint i = 0; (i = cast(uint) vec_index(i, ae)) < go.exptop; ++i) // for each ae elem
{
elem *e = go.expnod[i];
if (!e) continue;
if (el_appears(e,s))
vec_clearbit(i,ae);
}
}
else /* else ambiguous definition */
{
vec_subass(ae,go.defkill);
if (OTcalldef(op))
vec_subass(ae,go.vptrkill);
}
/* GEN the lvalue of an assignment operator */
uint i1, i2;
if (op == OPeq && (i1 = t.Eexp) != 0 && (i2 = n.EV.E2.Eexp) != 0)
{
if (vec_testbit(i2,ae))
{
vec_setbit(i1,ae);
if (vec_testbit(i2,aeval))
vec_setbit(i1,aeval);
else
vec_clearbit(i1,aeval);
}
}
}
else if (n.Eexp) /* if an AE */
{
if (op == OPvp_fp || op == OPcvp_fp)
/* Invalidate all other OPvp_fps */
vec_subass(ae,go.vptrkill);
/*printf("available: ("); WReqn(n); printf(")\n");
elem_print(n);*/
// vec_setbit(n.Eexp,ae); /* mark this elem as available */
}
}
/************************************
* Elem e is to be evaluated for a boolean result.
* See if we already know its value.
*/
private void abeboolres(elem *n,vec_t ae,vec_t aeval)
{
//printf("abeboolres()[%d %p] ", n.Eexp, go.expnod[n.Eexp]); WReqn(n); printf("\n");
elem_debug(n);
if (n.Eexp && go.expnod[n.Eexp])
{ /* Try to find an equivalent AE, and point to it instead */
assert(go.expnod[n.Eexp] == n);
uint i;
for (i = 0; (i = cast(uint) vec_index(i, ae)) < go.exptop; ++i) // for each ae elem
{ elem *e = go.expnod[i];
// Attempt to replace n with the boolean result of e
//printf("Looking at go.expnod[%d] = %p\n",i,e);
assert(e);
elem_debug(e);
if (n != e && el_match(n,e))
{
debug if (debugc)
{ printf("Elem %p: ",n);
WReqn(n);
printf(" is replaced by %d\n",vec_testbit(i,aeval) != 0);
}
abefree(n,ae);
n.EV.Vlong = vec_testbit(i,aeval) != 0;
n.Eoper = OPconst;
n.Ety = TYint;
go.changes++;
break;
}
}
}
}
/****************************
* Remove e from available expressions, and its children.
*/
private void abefree(elem *e,vec_t ae)
{
//printf("abefree [%d %p]: ", e.Eexp, e); WReqn(e); printf("\n");
assert(e.Eexp);
vec_clearbit(e.Eexp,ae);
go.expnod[e.Eexp] = null;
if (!OTleaf(e.Eoper))
{
if (OTbinary(e.Eoper))
{
abefree(e.EV.E2,ae);
el_free(e.EV.E2);
e.EV.E2 = null;
}
abefree(e.EV.E1,ae);
el_free(e.EV.E1);
e.EV.E1 = null;
}
}
/************************************
* Elem e is to be evaluated for a boolean result.
* Set its result according to flag.
*/
private void abeset(elem *e,vec_t ae,vec_t aeval,int flag)
{
while (1)
{
uint i = e.Eexp;
if (i && go.expnod[i])
{
//printf("abeset for go.expnod[%d] = %p: ",i,e); WReqn(e); printf("\n");
vec_setbit(i,ae);
if (flag)
vec_setbit(i,aeval);
else
vec_clearbit(i,aeval);
}
switch (e.Eoper)
{ case OPnot:
flag ^= 1;
e = e.EV.E1;
continue;
case OPbool:
case OPeq:
e = e.EV.E1;
continue;
default:
break;
}
break;
}
}
}