/
gflow.d
1960 lines (1726 loc) · 55.8 KB
/
gflow.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
/**
* Code to do the Data Flow Analysis (doesn't act on the data).
*
* Copyright: Copyright (C) 1985-1998 by Symantec
* Copyright (C) 2000-2024 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/backend/gflow.d, backend/gflow.d)
* Documentation: https://dlang.org/phobos/dmd_backend_gflow.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/backend/gflow.d
*/
module dmd.backend.gflow;
import core.stdc.stdio;
import core.stdc.stdlib;
import core.stdc.string;
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.barray;
import dmd.backend.dlist;
import dmd.backend.dvec;
nothrow:
@safe:
void vec_setclear(size_t b, vec_t vs, vec_t vc) { vec_setbit(b, vs); vec_clearbit(b, vc); }
@trusted
bool Eunambig(elem* e) { return OTassign(e.Eoper) && e.EV.E1.Eoper == OPvar; }
@trusted
char symbol_isintab(const Symbol* s) { return sytab[s.Sclass] & SCSS; }
@trusted
void util_free(void* p) { if (p) free(p); }
@trusted
void *util_calloc(uint n, uint size)
{
void* p = calloc(n, size);
if (n * size && !p)
err_nomem();
return p;
}
@trusted
void *util_realloc(void* p, size_t n, size_t size)
{
void* q = realloc(p, n * size);
if (n * size && !q)
err_nomem();
return q;
}
/* Since many routines are nearly identical, we can combine them with */
/* this flag: */
private enum
{
AE = 1,
CP,
VBE
}
private __gshared
{
int flowxx; // one of the above values
}
/***************** REACHING DEFINITIONS *********************/
/************************************
* Compute reaching definitions (RDs).
* That is, for each block B and each program variable X
* find all elems that could be the last elem that defines
* X along some path to B.
* Binrd = the set of defs reaching the beginning of B.
* Boutrd = the set of defs reaching the end of B.
* Bkillrd = set of defs that are killed by some def in B.
* Bgenrd = set of defs in B that reach the end of B.
*/
@trusted
void flowrd()
{
rdgenkill(); /* Compute Bgen and Bkill for RDs */
if (go.defnod.length == 0) /* if no definition elems */
return; /* no analysis to be done */
/* The transfer equation is: */
/* Bin = union of Bouts of all predecessors of B. */
/* Bout = (Bin - Bkill) | Bgen */
/* Using Ullman's algorithm: */
foreach (b; dfo[])
vec_copy(b.Boutrd, b.Bgen);
bool anychng;
vec_t tmp = vec_calloc(go.defnod.length);
do
{
anychng = false;
foreach (b; dfo[]) // for each block
{
/* Binrd = union of Boutrds of all predecessors of b */
vec_clear(b.Binrd);
if (b.BC != BCcatch /*&& b.BC != BCjcatch*/)
{
/* Set Binrd to 0 to account for:
* i = 0;
* try { i = 1; throw; } catch () { x = i; }
*/
foreach (bp; ListRange(b.Bpred))
vec_orass(b.Binrd,list_block(bp).Boutrd);
}
/* Bout = (Bin - Bkill) | Bgen */
vec_sub(tmp,b.Binrd,b.Bkill);
vec_orass(tmp,b.Bgen);
if (!anychng)
anychng = !vec_equal(tmp,b.Boutrd);
vec_copy(b.Boutrd,tmp);
}
} while (anychng); /* while any changes to Boutrd */
vec_free(tmp);
static if (0)
{
dbg_printf("Reaching definitions\n");
foreach (i, b; dfo[]) // for each block
{
assert(vec_numbits(b.Binrd) == go.defnod.length);
dbg_printf("B%d Bin ", cast(int)i); vec_println(b.Binrd);
dbg_printf(" Bgen "); vec_println(b.Bgen);
dbg_printf(" Bkill "); vec_println(b.Bkill);
dbg_printf(" Bout "); vec_println(b.Boutrd);
}
}
}
/***************************
* Compute Bgen and Bkill for RDs.
*/
@trusted
private void rdgenkill()
{
/* Compute number of definition elems. */
uint num_unambig_def = 0;
uint deftop = 0;
foreach (b; dfo[]) // for each block
if (b.Belem)
{
deftop += numdefelems(b.Belem, num_unambig_def);
}
/* Allocate array of pointers to all definition elems */
/* The elems are in dfo order. */
/* go.defnod[]s consist of a elem pointer and a pointer */
/* to the enclosing block. */
go.defnod.setLength(deftop);
if (deftop == 0)
return;
/* Allocate buffer for the DNunambig vectors
*/
const size_t dim = (deftop + (VECBITS - 1)) >> VECSHIFT;
const sz = (dim + 2) * num_unambig_def;
go.dnunambig.setLength(sz);
go.dnunambig[] = 0;
go.defnod.setLength(deftop);
size_t i = deftop;
foreach_reverse (b; dfo[]) // for each block
if (b.Belem)
asgdefelems(b, b.Belem, go.defnod[], i); // fill in go.defnod[]
assert(i == 0);
initDNunambigVectors(go.defnod[]);
foreach (b; dfo[]) // for each block
{
/* dump any existing vectors */
vec_free(b.Bgen);
vec_free(b.Bkill);
vec_free(b.Binrd);
vec_free(b.Boutrd);
/* calculate and create new vectors */
rdelem(b.Bgen, b.Bkill, b.Belem, deftop);
if (b.BC == BCasm)
{
vec_clear(b.Bkill); // KILL nothing
vec_set(b.Bgen); // GEN everything
}
b.Binrd = vec_calloc(deftop);
b.Boutrd = vec_calloc(deftop);
}
}
/**********************
* Compute and return # of definition elems in e.
* Params:
* e = elem tree to search
* num_unambig_def = accumulate the number of unambiguous
* definition elems
* Returns:
* number of definition elems
*/
@trusted
private uint numdefelems(const(elem)* e, ref uint num_unambig_def)
{
uint n = 0;
while (1)
{
assert(e);
if (OTdef(e.Eoper))
{
++n;
if (OTassign(e.Eoper) && e.EV.E1.Eoper == OPvar)
++num_unambig_def;
}
if (OTbinary(e.Eoper))
{
n += numdefelems(e.EV.E1, num_unambig_def);
e = e.EV.E2;
}
else if (OTunary(e.Eoper))
{
e = e.EV.E1;
}
else
break;
}
return n;
}
/**************************
* Load defnod[] array.
* Loaded in order of execution of the elems. Not sure if this is
* necessary.
*/
@trusted
private void asgdefelems(block *b,elem *n, DefNode[] defnod, ref size_t i)
{
assert(b && n);
while (1)
{
const op = n.Eoper;
if (OTdef(op))
{
--i;
defnod[i] = DefNode(n, b, null);
n.Edef = cast(uint)i;
}
else
n.Edef = ~0; // just to ensure it is not in the array
if (ERTOL(n))
{
asgdefelems(b,n.EV.E1,defnod,i);
n = n.EV.E2;
continue;
}
else if (OTbinary(op))
{
asgdefelems(b,n.EV.E2,defnod,i);
n = n.EV.E1;
continue;
}
else if (OTunary(op))
{
n = n.EV.E1;
continue;
}
break;
}
}
/*************************************
* Allocate and initialize DNumambig vectors in go.defnod[]
*/
@trusted
private void initDNunambigVectors(DefNode[] defnod)
{
//printf("initDNunambigVectors()\n");
const size_t numbits = defnod.length;
const size_t dim = (numbits + (VECBITS - 1)) >> VECSHIFT;
/* Initialize vector for DNunambig for each defnod[] entry that
* is an assignment to a variable
*/
size_t j = 0;
foreach (const i; 0 .. defnod.length)
{
elem *e = defnod[i].DNelem;
if (OTassign(e.Eoper) && e.EV.E1.Eoper == OPvar)
{
vec_t v = &go.dnunambig[j] + 2;
assert(vec_dim(v) == 0);
vec_dim(v) = dim;
vec_numbits(v) = numbits;
j += dim + 2;
defnod[i].DNunambig = v;
}
}
assert(j <= go.dnunambig.length);
foreach (const i; 0 .. defnod.length)
{
if (vec_t v = defnod[i].DNunambig)
{
elem *e = defnod[i].DNelem;
vec_setbit(cast(uint) i, v); // of course it modifies itself
fillInDNunambig(v, e, i, defnod[]);
}
}
}
/**************************************
* Fill in the DefNode.DNumambig vector.
* Set bits defnod[] indices for entries
* which are completely destroyed when e is
* unambiguously assigned to.
* Note that results for indices less than `start`
* are already computed, so skip them.
* Params:
* v = vector to fill in
* e = defnod[] entry that is an assignment to a variable
* start = starting index in defnod[]
* defnod = array of definition nodes
*/
@trusted
private void fillInDNunambig(vec_t v, elem *e, size_t start, DefNode[] defnod)
{
assert(OTassign(e.Eoper));
elem *t = e.EV.E1;
assert(t.Eoper == OPvar);
Symbol *d = t.EV.Vsym;
targ_size_t toff = t.EV.Voffset;
targ_size_t tsize = (e.Eoper == OPstreq) ? type_size(e.ET) : tysize(t.Ety);
targ_size_t ttop = toff + tsize;
// for all unambig defs in defnod[]
foreach (const i; start + 1 .. defnod.length)
{
vec_t v2 = defnod[i].DNunambig;
if (!v2)
continue;
elem *tn = defnod[i].DNelem;
elem *tn1;
targ_size_t tn1size;
// If not same variable then no overlap
tn1 = tn.EV.E1;
if (d != tn1.EV.Vsym)
continue;
tn1size = (tn.Eoper == OPstreq)
? type_size(tn.ET) : tysize(tn1.Ety);
// If t completely overlaps tn1
if (toff <= tn1.EV.Voffset && tn1.EV.Voffset + tn1size <= ttop)
{
vec_setbit(cast(uint)i, v);
}
// if tn1 completely overlaps t
if (tn1.EV.Voffset <= toff && ttop <= tn1.EV.Voffset + tn1size)
{
vec_setbit(cast(uint)start, v2);
}
}
}
/*************************************
* Allocate and compute rd GEN and KILL.
* Params:
* GEN = gen vector to create
* KILL = kill vector to create
* n = elem tree to evaluate for GEN and KILL
* deftop = number of bits in vectors
*/
@trusted
private void rdelem(out vec_t GEN, out vec_t KILL, elem *n, uint deftop)
{
GEN = vec_calloc(deftop);
KILL = vec_calloc(deftop);
if (n)
accumrd(GEN, KILL, n, deftop);
}
/**************************************
* Accumulate GEN and KILL vectors for this elem.
*/
@trusted
private void accumrd(vec_t GEN,vec_t KILL,elem *n,uint deftop)
{
assert(GEN && KILL && n);
const op = n.Eoper;
if (OTunary(op))
accumrd(GEN,KILL,n.EV.E1,deftop);
else if (OTbinary(op))
{
if (op == OPcolon || op == OPcolon2)
{
vec_t Gl,Kl,Gr,Kr;
rdelem(Gl, Kl, n.EV.E1, deftop);
rdelem(Gr, Kr, n.EV.E2, deftop);
switch (el_returns(n.EV.E1) * 2 | int(el_returns(n.EV.E2)))
{
case 3: // E1 and E2 return
/* GEN = (GEN - Kl) | Gl |
* (GEN - Kr) | Gr
* KILL |= Kl & Kr
* This simplifies to:
* GEN = GEN | (Gl | Gr) | (GEN - (Kl & Kr)
* KILL |= Kl & Kr
*/
vec_andass(Kl,Kr);
vec_orass(KILL,Kl);
vec_orass(Gl,Gr);
vec_sub(Gr,GEN,Kl); // (GEN - (Kl & Kr)
vec_or(GEN,Gl,Gr);
break;
case 2: // E1 returns
/* GEN = (GEN - Kl) | Gl
* KILL |= Kl
*/
vec_subass(GEN,Kl);
vec_orass(GEN,Gl);
vec_orass(KILL,Kl);
break;
case 1: // E2 returns
/* GEN = (GEN - Kr) | Gr
* KILL |= Kr
*/
vec_subass(GEN,Kr);
vec_orass(GEN,Gr);
vec_orass(KILL,Kr);
break;
case 0: // neither returns
break;
default:
assert(0);
}
vec_free(Gl);
vec_free(Kl);
vec_free(Gr);
vec_free(Kr);
}
else if (op == OPandand || op == OPoror)
{
accumrd(GEN,KILL,n.EV.E1,deftop);
vec_t Gr,Kr;
rdelem(Gr, Kr, n.EV.E2, deftop);
if (el_returns(n.EV.E2))
vec_orass(GEN,Gr); // GEN |= Gr
vec_free(Gr);
vec_free(Kr);
}
else if (OTrtol(op) && ERTOL(n))
{
accumrd(GEN,KILL,n.EV.E2,deftop);
accumrd(GEN,KILL,n.EV.E1,deftop);
}
else
{
accumrd(GEN,KILL,n.EV.E1,deftop);
accumrd(GEN,KILL,n.EV.E2,deftop);
}
}
if (OTdef(op)) /* if definition elem */
updaterd(n,GEN,KILL);
}
/******************** AVAILABLE EXPRESSIONS ***********************/
/************************************
* Compute available expressions (AEs).
* That is, expressions whose result is still current.
* Bin = the set of AEs reaching the beginning of B.
* Bout = the set of AEs reaching the end of B.
*/
@trusted
void flowae()
{
flowxx = AE;
flowaecp(AE);
}
/**************************** COPY PROPAGATION ************************/
/***************************************
* Compute copy propagation info (CPs).
* Very similar to AEs (the same code is used).
* Using RDs for copy propagation is WRONG!
* That is, set of copy statements still valid.
* Bin = the set of CPs reaching the beginning of B.
* Bout = the set of CPs reaching the end of B.
*/
@trusted
void flowcp()
{
flowxx = CP;
flowaecp(CP);
}
/*****************************************
* Common flow analysis routines for Available Expressions and
* Copy Propagation.
* Input:
* flowxx
*/
@trusted
private void flowaecp(int flowxx)
{
aecpgenkill(go, flowxx); // Compute Bgen and Bkill for AEs or CPs
if (go.exptop <= 1) /* if no expressions */
return;
/* The transfer equation is: */
/* Bin = & Bout(all predecessors P of B) */
/* Bout = (Bin - Bkill) | Bgen */
/* Using Ullman's algorithm: */
vec_clear(startblock.Bin);
vec_copy(startblock.Bout,startblock.Bgen); /* these never change */
if (startblock.BC == BCiftrue)
vec_copy(startblock.Bout2,startblock.Bgen2); // these never change
/* For all blocks except startblock */
foreach (b; dfo[1 .. $])
{
vec_set(b.Bin); /* Bin = all expressions */
/* Bout = (Bin - Bkill) | Bgen */
vec_sub(b.Bout,b.Bin,b.Bkill);
vec_orass(b.Bout,b.Bgen);
if (b.BC == BCiftrue)
{
vec_sub(b.Bout2,b.Bin,b.Bkill2);
vec_orass(b.Bout2,b.Bgen2);
}
}
vec_t tmp = vec_calloc(go.exptop);
bool anychng;
do
{
anychng = false;
// For all blocks except startblock
foreach (b; dfo[1 .. $])
{
// Bin = & of Bout of all predecessors
// Bout = (Bin - Bkill) | Bgen
bool first = true;
foreach (bl; ListRange(b.Bpred))
{
block* bp = list_block(bl);
if (bp.BC == BCiftrue && bp.nthSucc(0) != b)
{
if (first)
vec_copy(b.Bin,bp.Bout2);
else
vec_andass(b.Bin,bp.Bout2);
}
else
{
if (first)
vec_copy(b.Bin,bp.Bout);
else
vec_andass(b.Bin,bp.Bout);
}
first = false;
}
assert(!first); // it must have had predecessors
if (b.BC == BCjcatch)
{
/* Set Bin to 0 to account for:
void* pstart = p;
try
{
p = null; // account for this
throw;
}
catch (Throwable o) { assert(p != pstart); }
*/
vec_clear(b.Bin);
}
if (anychng)
{
vec_sub(b.Bout,b.Bin,b.Bkill);
vec_orass(b.Bout,b.Bgen);
}
else
{
vec_sub(tmp,b.Bin,b.Bkill);
vec_orass(tmp,b.Bgen);
if (!vec_equal(tmp,b.Bout))
{ // Swap Bout and tmp instead of
// copying tmp over Bout
vec_t v = tmp;
tmp = b.Bout;
b.Bout = v;
anychng = true;
}
}
if (b.BC == BCiftrue)
{ // Bout2 = (Bin - Bkill2) | Bgen2
if (anychng)
{
vec_sub(b.Bout2,b.Bin,b.Bkill2);
vec_orass(b.Bout2,b.Bgen2);
}
else
{
vec_sub(tmp,b.Bin,b.Bkill2);
vec_orass(tmp,b.Bgen2);
if (!vec_equal(tmp,b.Bout2))
{ // Swap Bout and tmp instead of
// copying tmp over Bout2
vec_t v = tmp;
tmp = b.Bout2;
b.Bout2 = v;
anychng = true;
}
}
}
}
} while (anychng);
vec_free(tmp);
}
/***********************************
* Compute Bgen and Bkill for AEs, CPs, and VBEs.
*/
@trusted
private void aecpgenkill(ref GlobalOptimizer go, int flowxx)
{
block* this_block;
/********************************
* Assign cp elems to go.expnod[] (in order of evaluation).
*/
void asgcpelems(elem *n)
{
while (1)
{
const op = n.Eoper;
if (OTunary(op))
{
n.Eexp = 0;
n = n.EV.E1;
continue;
}
else if (OTbinary(op))
{
if (ERTOL(n))
{
asgcpelems(n.EV.E2);
asgcpelems(n.EV.E1);
}
else
{
asgcpelems(n.EV.E1);
asgcpelems(n.EV.E2);
}
/* look for elem of the form OPvar=OPvar, where they aren't the
* same variable.
* Don't mix XMM and integer registers.
*/
elem* e1;
elem* e2;
if ((op == OPeq || op == OPstreq) &&
(e1 = n.EV.E1).Eoper == OPvar &&
(e2 = n.EV.E2).Eoper == OPvar &&
!((e1.Ety | e2.Ety) & (mTYvolatile | mTYshared)) &&
(!config.fpxmmregs ||
(!tyfloating(e1.EV.Vsym.Stype.Tty) == !tyfloating(e2.EV.Vsym.Stype.Tty))) &&
e1.EV.Vsym != e2.EV.Vsym)
{
n.Eexp = cast(uint)go.expnod.length;
go.expnod.push(n);
}
else
n.Eexp = 0;
}
else
n.Eexp = 0;
return;
}
}
/********************************
* Assign ae and vbe elems to go.expnod[] (in order of evaluation).
*/
bool asgaeelems(elem *n)
{
bool ae;
assert(n);
const op = n.Eoper;
if (OTunary(op))
{
ae = asgaeelems(n.EV.E1);
// Disallow starred references to avoid problems with VBE's
// being hoisted before tests of an invalid pointer.
if (flowxx == VBE && op == OPind)
{
n.Eexp = 0;
return false;
}
}
else if (OTbinary(op))
{
if (ERTOL(n))
ae = asgaeelems(n.EV.E2) & asgaeelems(n.EV.E1);
else
ae = asgaeelems(n.EV.E1) & asgaeelems(n.EV.E2);
}
else
ae = true;
if (ae && OTae(op) && !(n.Ety & (mTYvolatile | mTYshared)) &&
// Disallow struct AEs, because we can't handle CSEs that are structs
tybasic(n.Ety) != TYstruct &&
tybasic(n.Ety) != TYarray)
{
n.Eexp = cast(uint)go.expnod.length; // remember index into go.expnod[]
go.expnod.push(n);
if (flowxx == VBE)
go.expblk.push(this_block);
return true;
}
else
{
n.Eexp = 0;
return false;
}
}
go.expnod.setLength(0); // dump any existing one
go.expnod.push(null);
go.expblk.setLength(0); // dump any existing one
go.expblk.push(null);
foreach (b; dfo[])
{
if (b.Belem)
{
if (flowxx == CP)
asgcpelems(b.Belem);
else
{
this_block = b; // so asgaeelems knows about this
asgaeelems(b.Belem);
}
}
}
go.exptop = cast(uint)go.expnod.length;
if (go.exptop <= 1)
return;
defstarkill(); /* compute go.defkill and go.starkill */
static if (0)
{
assert(vec_numbits(go.defkill) == go.expnod.length);
assert(vec_numbits(go.starkill) == go.expnod.length);
assert(vec_numbits(go.vptrkill) == go.expnod.length);
dbg_printf("defkill "); vec_println(go.defkill);
if (go.starkill)
{ dbg_printf("starkill "); vec_println(go.starkill);}
if (go.vptrkill)
{ dbg_printf("vptrkill "); vec_println(go.vptrkill); }
}
foreach (i, b; dfo[])
{
/* dump any existing vectors */
vec_free(b.Bin);
vec_free(b.Bout);
vec_free(b.Bgen);
vec_free(b.Bkill);
b.Bgen = vec_calloc(go.expnod.length);
b.Bkill = vec_calloc(go.expnod.length);
switch (b.BC)
{
case BCiftrue:
vec_free(b.Bout2);
vec_free(b.Bgen2);
vec_free(b.Bkill2);
elem* e;
for (e = b.Belem; e.Eoper == OPcomma; e = e.EV.E2)
accumaecp(b.Bgen,b.Bkill,e.EV.E1);
if (e.Eoper == OPandand || e.Eoper == OPoror)
{
accumaecp(b.Bgen,b.Bkill,e.EV.E1);
vec_t Kr = vec_calloc(go.expnod.length);
vec_t Gr = vec_calloc(go.expnod.length);
accumaecp(Gr,Kr,e.EV.E2);
// We might or might not have executed E2
// KILL1 = KILL | Kr
// GEN1 = GEN & ((GEN - Kr) | Gr)
// We definitely executed E2
// KILL2 = (KILL - Gr) | Kr
// GEN2 = (GEN - Kr) | Gr
const uint dim = cast(uint)vec_dim(Kr);
vec_t KILL = b.Bkill;
vec_t GEN = b.Bgen;
foreach (j; 0 .. dim)
{
vec_base_t KILL1 = KILL[j] | Kr[j];
vec_base_t GEN1 = GEN[j] & ((GEN[j] & ~Kr[j]) | Gr[j]);
vec_base_t KILL2 = (KILL[j] & ~Gr[j]) | Kr[j];
vec_base_t GEN2 = (GEN[j] & ~Kr[j]) | Gr[j];
KILL[j] = KILL1;
GEN[j] = GEN1;
Kr[j] = KILL2;
Gr[j] = GEN2;
}
if (e.Eoper == OPandand)
{ b.Bkill = Kr;
b.Bgen = Gr;
b.Bkill2 = KILL;
b.Bgen2 = GEN;
}
else
{ b.Bkill = KILL;
b.Bgen = GEN;
b.Bkill2 = Kr;
b.Bgen2 = Gr;
}
}
else
{
accumaecp(b.Bgen,b.Bkill,e);
b.Bgen2 = vec_clone(b.Bgen);
b.Bkill2 = vec_clone(b.Bkill);
}
b.Bout2 = vec_calloc(go.expnod.length);
break;
case BCasm:
vec_set(b.Bkill); // KILL everything
vec_clear(b.Bgen); // GEN nothing
break;
default:
// calculate GEN & KILL vectors
if (b.Belem)
accumaecp(b.Bgen,b.Bkill,b.Belem);
break;
}
static if (0)
{
printf("block %d Bgen ",i); vec_println(b.Bgen);
printf(" Bkill "); vec_println(b.Bkill);
}
b.Bin = vec_calloc(go.expnod.length);
b.Bout = vec_calloc(go.expnod.length);
}
}
/********************************
* Compute defkill, starkill and vptrkill vectors.
* starkill: set of expressions killed when a variable is
* changed that somebody could be pointing to.
* (not needed for cp)
* starkill is a subset of defkill.
* defkill: set of expressions killed by an ambiguous
* definition.
* vptrkill: set of expressions killed by an access to a vptr.
*/
@trusted
private void defstarkill()
{
const exptop = go.exptop;
vec_recycle(go.defkill, exptop);
if (flowxx == CP)
{
vec_recycle(go.starkill, 0);
vec_recycle(go.vptrkill, 0);
}
else
{
vec_recycle(go.starkill, exptop); // and create new ones
vec_recycle(go.vptrkill, exptop); // and create new ones
}
if (!exptop)
return;
auto defkill = go.defkill;
if (flowxx == CP)
{
foreach (i, n; go.expnod[1 .. exptop])
{
const op = n.Eoper;
assert(op == OPeq || op == OPstreq);
assert(n.EV.E1.Eoper==OPvar && n.EV.E2.Eoper==OPvar);
// Set bit in defkill if either the left or the
// right variable is killed by an ambiguous def.
if (Symbol_isAffected(*n.EV.E1.EV.Vsym) ||
Symbol_isAffected(*n.EV.E2.EV.Vsym))
{
vec_setbit(i + 1,defkill);
}
}
}
else
{
auto starkill = go.starkill;
auto vptrkill = go.vptrkill;
foreach (j, n; go.expnod[1 .. exptop])
{
const i = j + 1;
const op = n.Eoper;
switch (op)
{
case OPvar:
if (Symbol_isAffected(*n.EV.Vsym))
vec_setbit(i,defkill);
break;
case OPind: // if a 'starred' ref
if (tybasic(n.EV.E1.Ety) == TYimmutPtr)
break;
goto case OPstrlen;
case OPstrlen:
case OPstrcmp:
case OPmemcmp:
case OPbt: // OPbt is like OPind
vec_setbit(i,defkill);
vec_setbit(i,starkill);
break;
case OPvp_fp:
case OPcvp_fp:
vec_setbit(i,vptrkill);
goto Lunary;
default:
if (OTunary(op))
{
Lunary: