/
gloop.d
3885 lines (3406 loc) · 117 KB
/
gloop.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
/**
* Compiler implementation of the
* $(LINK2 http://www.dlang.org, D programming language).
*
* Copyright: Copyright (C) 1985-1998 by Symantec
* Copyright (C) 2000-2021 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/gloop.d, backend/gloop.d)
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/backend/gloop.d
*/
module dmd.backend.gloop;
version (SCPP)
version = COMPILE;
version (MARS)
version = COMPILE;
version (COMPILE)
{
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.evalu8 : el_toldoubled;
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;
import dmd.backend.mem;
nothrow:
@safe:
@trusted
char symbol_isintab(const Symbol *s) { return sytab[s.Sclass] & SCSS; }
extern (C++):
bool findloopparameters(elem* erel, ref elem* rdeq, ref elem* rdinc);
alias Loops = Rarray!loop;
/*********************************
* Loop data structure.
*/
struct loop
{
nothrow:
vec_t Lloop; // Vector of blocks in this loop
vec_t Lexit; // Vector of exit blocks of loop
block *Lhead; // Pointer to header of loop
block *Ltail; // Pointer to tail
block *Lpreheader; // Pointer to preheader (if any)
Barray!(elem*) Llis; // loop invariant elems moved to Lpreheader, so
// redundant temporaries aren't created
Rarray!Iv Livlist; // basic induction variables
Rarray!Iv Lopeqlist; // list of other op= variables
/*************************
* Reset memory so this allocation can be re-used.
*/
@trusted
void reset()
{
vec_free(Lloop);
vec_free(Lexit);
foreach (ref iv; Livlist)
iv.reset();
foreach (ref iv; Lopeqlist)
iv.reset();
Llis.reset();
Livlist.reset();
Lopeqlist.reset();
}
/***********************
* Write loop.
*/
@trusted
void print()
{
debug
{
loop *l = &this;
printf("loop %p\n", l);
printf("\thead: B%d, tail: B%d, prehead: B%d\n",l.Lhead.Bdfoidx,
l.Ltail.Bdfoidx,(l.Lpreheader ) ? l.Lpreheader.Bdfoidx
: cast(uint)-1);
printf("\tLloop "); vec_println(l.Lloop);
printf("\tLexit "); vec_println(l.Lexit);
}
}
}
struct famlist
{
nothrow:
elem **FLpelem; /* parent of elem in the family */
elem *c1;
elem *c2; // c1*(basic IV) + c2
Symbol *FLtemp; // symbol index of temporary (FLELIM if */
/* this entry has no temporary) */
tym_t FLty; /* type of this induction variable */
tym_t FLivty; /* type of the basic IV elem (which is */
/* not necessarilly the type of the IV */
/* elem!) */
void reset()
{
el_free(c1);
el_free(c2);
}
@trusted
void print() const
{
debug
{
printf("famlist:\n");
printf("*FLpelem:\n");
elem_print(*FLpelem);
printf("c1:");
elem_print(c1);
printf("c2:");
elem_print(c2);
printf("FLty = "); WRTYxx(FLty);
printf("\nFLivty = "); WRTYxx(FLivty);
printf("\n");
}
}
}
@system
enum FLELIM = cast(Symbol *)-1;
struct Iv
{
nothrow:
Symbol *IVbasic; // symbol of basic IV
elem **IVincr; // pointer to parent of IV increment elem
Barray!famlist IVfamily; // variables in this family
@trusted
void reset()
{
foreach (ref fl; IVfamily)
{
fl.reset();
}
IVfamily.reset();
}
@trusted
void print() const
{
debug
{
printf("IV: '%s'\n",IVbasic.Sident.ptr);
printf("*IVincr:\n");
elem_print(*IVincr);
}
}
}
private __gshared bool addblk; /* if TRUE, then we added a block */
/* is elem loop invariant? */
int isLI(const elem* n) { return n.Nflags & NFLli; }
/* make elem loop invariant */
void makeLI(elem* n) { n.Nflags |= NFLli; }
/******************************
* Only variables that could only be unambiguously defined
* are candidates for loop invariant removal and induction
* variables.
* This means only variables that have the SFLunambig flag
* set for them.
* Doing this will still cover 90% (I hope) of the cases, and
* is a lot faster to compute.
*/
/*************
* Free loops.
*/
private void freeloop(ref Loops loops)
{
foreach (ref loop; loops)
loop.reset();
loops.reset();
}
/**********************************
* Initialize block information.
* Returns:
* !=0 contains BCasm block
*/
@trusted
int blockinit()
{
bool hasasm = false;
assert(dfo);
uint i = 0;
foreach (b; BlockRange(startblock))
{
debug /* check integrity of Bpred and Bsucc */
L1:
foreach (blp; ListRange(b.Bpred))
{
foreach (bls; ListRange(list_block(blp).Bsucc))
if (list_block(bls) == b)
continue L1;
assert(0);
}
++i;
if (b.BC == BCasm)
hasasm = true;
/* compute number of blocks */
}
foreach (j, b; dfo[])
{
assert(b.Bdfoidx == j);
b.Bdom = vec_realloc(b.Bdom, dfo.length); /* alloc Bdom vectors */
vec_clear(b.Bdom);
}
return hasasm;
}
/****************************************
* Compute dominators (Bdom) for each block.
* See Aho & Ullman Fig. 13.5.
* Note that flow graph is reducible if there is only one
* pass through the loop.
* Input:
* dfo[]
* Output:
* fills in the Bdom vector for each block
*/
@trusted
void compdom()
{
compdom(dfo[]);
}
@trusted
private extern (D) void compdom(block*[] dfo)
{
assert(dfo.length);
block* sb = dfo[0]; // starting block
vec_clear(sb.Bdom);
vec_setbit(0,sb.Bdom); // starting block only doms itself
foreach (b; dfo) // for all except startblock
{
if (b != sb)
vec_set(b.Bdom); // dominate all blocks
}
vec_t t1 = vec_calloc(vec_numbits(sb.Bdom)); // allocate a temporary
uint cntr = 0; // # of times thru loop
bool chgs;
do
{
chgs = false;
foreach (i, b; dfo) // for each block in dfo[]
{
if (i == 0)
continue; // except startblock
if (b.Bpred) // if there are predecessors
{
vec_set(t1);
foreach (bl; ListRange(b.Bpred))
{
vec_andass(t1,list_block(bl).Bdom);
}
}
else
vec_clear(t1); // no predecessors to dominate
vec_setbit(i,t1); // each block doms itself
if (chgs)
vec_copy(b.Bdom,t1);
else if (!vec_equal(b.Bdom,t1)) // if any changes
{
vec_copy(b.Bdom,t1);
chgs = true;
}
}
cntr++;
assert(cntr < 50); // should have converged by now
} while (chgs);
vec_free(t1);
debug if (debugc)
{
printf("Flow graph is%s reducible\n", cntr <= 2 ? "".ptr : " not".ptr);
}
}
/***************************
* Return !=0 if block A dominates block B.
*/
@trusted
bool dom(const block* A, const block* B)
{
assert(A && B && dfo && dfo[A.Bdfoidx] == A);
return vec_testbit(A.Bdfoidx,B.Bdom) != 0;
}
/**********************
* Find all the loops.
*/
private extern (D) void findloops(block*[] dfo, ref Loops loops)
{
freeloop(loops);
//printf("findloops()\n");
foreach (b; dfo)
b.Bweight = 1; // reset Bweights
foreach_reverse (b; dfo) // for each block (note reverse
// dfo order, so most nested
// loops are found first)
{
assert(b);
foreach (bl; ListRange(b.Bsucc))
{
block *s = list_block(bl); // each successor s to b
assert(s);
if (dom(s,b)) // if s dominates b
buildloop(loops, s, b); // we found a loop
}
}
debug if (debugc)
{
foreach (ref l; loops)
l.print();
}
}
/********************************
*/
private uint loop_weight(uint weight, int factor) pure
{
// Be careful not to overflow
if (weight < 0x1_0000)
weight *= 10 * factor;
else if (weight < 0x10_0000)
weight *= 2 * factor;
else
weight += factor;
assert(cast(int)weight > 0);
return weight;
}
/*****************************
* Construct natural loop.
* Algorithm 13.1 from Aho & Ullman.
* Note that head dom tail.
*/
@trusted
private void buildloop(ref Loops ploops,block *head,block *tail)
{
loop *l;
//printf("buildloop()\n");
/* See if this is part of an existing loop. If so, merge the two. */
foreach (ref lp; ploops)
if (lp.Lhead == head) /* two loops with same header */
{
vec_t v;
// Calculate loop contents separately so we get the Bweights
// done accurately.
v = vec_calloc(dfo.length);
vec_setbit(head.Bdfoidx,v);
head.Bweight = loop_weight(head.Bweight, 1);
insert(tail,v);
vec_orass(lp.Lloop,v); // merge into existing loop
vec_free(v);
vec_clear(lp.Lexit); // recompute exit blocks
l = &lp;
goto L1;
}
/* Allocate loop entry */
l = ploops.push();
l.Lloop = vec_calloc(dfo.length); // allocate loop bit vector
l.Lexit = vec_calloc(dfo.length); // bit vector for exit blocks
l.Lhead = head;
l.Ltail = tail;
l.Lpreheader = null;
vec_setbit(head.Bdfoidx,l.Lloop); /* add head to the loop */
head.Bweight = loop_weight(head.Bweight, 2); // *20 usage for loop header
insert(tail,l.Lloop); /* insert tail in loop */
L1:
/* Find all the exit blocks (those blocks with
* successors outside the loop).
*/
// for each block in this loop
for (uint i = 0; (i = cast(uint) vec_index(i, l.Lloop)) < dfo.length; ++i)
{
if (dfo[i].BC == BCret || dfo[i].BC == BCretexp || dfo[i].BC == BCexit)
vec_setbit(i,l.Lexit); /* ret blocks are exit blocks */
else
{
foreach (bl; ListRange(dfo[i].Bsucc))
if (!vec_testbit(list_block(bl).Bdfoidx,l.Lloop))
{
vec_setbit(i,l.Lexit);
break;
}
}
}
/* Find preheader, if any, to the loop.
The preheader is a block that has only the head as a successor.
All other predecessors of head must be inside the loop.
*/
l.Lpreheader = null;
foreach (bl; ListRange(head.Bpred))
{
block *b = list_block(bl);
if (!vec_testbit(b.Bdfoidx,l.Lloop)) /* if not in loop */
{
if (l.Lpreheader) /* if already one */
{
l.Lpreheader = null; /* can only be one */
break;
}
else
{
if (list_next(b.Bsucc)) // if more than 1 successor
break; // b can't be a preheader
l.Lpreheader = b;
}
}
}
}
/********************************
* Support routine for buildloop().
* Add a block b and all its predecessors to loop lv.
*/
private void insert(block *b, vec_t lv)
{
assert(b && lv);
if (!vec_testbit(b.Bdfoidx,lv)) /* if block is not in loop */
{
vec_setbit(b.Bdfoidx,lv); /* add block to loop */
b.Bweight = loop_weight(b.Bweight,1); // *10 usage count
foreach (bl; ListRange(b.Bpred))
insert(list_block(bl),lv); /* insert all its predecessors */
}
}
/**************************************
* Perform loop rotations.
* Loop starts as:
*
* prehead
* |
* v
* +->head---->
* | |
* | v
* | body
* | |
* | v
* +--tail
*
* Two types are done:
* 1) Header is moved to be past the tail.
*
* prehead
* |
* +---+
* |
* | body<-+
* | | |
* | v |
* | tail |
* | | |
* | v |
* +->head--+
* |
* v
*
* 2) Header is copied past the tail (done only if MFtime is set).
*
* prehead
* |
* v
* head1-----+
* | |
* v |
* body<--+ |
* | | |
* v | |
* tail | |
* | | |
* v | |
* head2--+ |
* | |
* +--------+
* v
*
* Input:
* Loop information (do not depend on the preheader information)
* Output:
* Revised list of blocks, a new dfo and new loop information
* Returns:
* true need to recompute loop data
*/
@trusted
private int looprotate(ref loop l)
{
block *tail = l.Ltail;
block *head = l.Lhead;
//printf("looprotate(%p)\n",l);
// Do not rotate loop if:
if (head == tail || // loop is only one block big
!vec_testbit(head.Bdfoidx,l.Lexit)) // header is not an exit block
goto Lret;
if (//iter != 1 &&
vec_testbit(tail.Bdfoidx,l.Lexit)) // tail is an exit block
goto Lret;
// Do not rotate if already rotated
foreach (b; BlockRange(tail.Bnext))
if (b == head) // if loop already rotated
goto Lret;
if (head.BC == BCtry)
goto Lret;
if (head.BC == BC_try)
goto Lret;
//if (debugc) { printf("looprotate: "); l.print(); }
if ((go.mfoptim & MFtime) && head.BC != BCswitch && head.BC != BCasm)
{ // Duplicate the header past the tail (but doing
// switches would be too expensive in terms of code
// generated).
auto head2 = block_calloc(); // create new head block
head2.Btry = head.Btry;
head2.Bflags = head.Bflags;
head.Bflags = BFLnomerg; // move flags over to head2
head2.Bflags |= BFLnomerg;
head2.BC = head.BC;
assert(head2.BC != BCswitch);
if (head.Belem) // copy expression tree
head2.Belem = el_copytree(head.Belem);
head2.Bnext = tail.Bnext;
tail.Bnext = head2;
// pred(head1) = pred(head) outside loop
// pred(head2) = pred(head) inside loop
list_t *pbln;
auto pbl2 = &(head2.Bpred);
for (list_t *pbl = &(head.Bpred); *pbl; pbl = pbln)
{
if (vec_testbit(list_block(*pbl).Bdfoidx, l.Lloop))
{ // if this predecessor is inside the loop
*pbl2 = *pbl;
*pbl = list_next(*pbl);
pbln = pbl; // don't skip this next one
(*pbl2).next = null;
auto bsucc = list_block(*pbl2).Bsucc;
pbl2 = &((*pbl2).next);
foreach (bl; ListRange(bsucc))
if (list_block(bl) == head)
{
bl.ptr = cast(void *)head2;
goto L2;
}
assert(0);
L2:
}
else
pbln = &((*pbl).next); // next predecessor in list
} // for each pred(head)
// succ(head2) = succ(head)
foreach (bl; ListRange(head.Bsucc))
{
list_append(&(head2.Bsucc),list_block(bl));
list_append(&(list_block(bl).Bpred),head2);
}
if (debugc) printf("1Rotated loop %p\n", &l);
go.changes++;
return true;
}
else if (startblock != head
/* This screws up the OPctor/OPdtor sequence for:
* struct CString
* { CString();
* ~CString();
* int GetLength();
* };
*
* void f(void)
* { for(;;)
* { CString s ;
* if(s.GetLength()!=0)
* break ;
* }
* }
*/
&& !(config.flags3 & CFG3eh)
)
{ // optimize for space
// Simply position the header past the tail
foreach (b; BlockRange(startblock))
{
if (b.Bnext == head)
{ // found parent b of head
b.Bnext = head.Bnext;
head.Bnext = tail.Bnext;
tail.Bnext = head;
if (debugc) printf("2Rotated loop %p\n", &l);
go.changes++;
return false;
}
}
assert(0);
}
Lret:
return false;
}
private __gshared
{
int gref; // parameter for markinvar()
block *gblock; // parameter for markinvar()
vec_t lv; // parameter for markinvar()
vec_t gin; // parameter for markinvar()
bool doflow; // true if flow analysis has to be redone
}
/*********************************
* Loop invariant and induction variable elimination.
* Input:
* iter which optimization iteration we are on
*/
@trusted
void loopopt()
{
__gshared Loops startloop_cache;
Loops startloop = startloop_cache;
if (debugc) printf("loopopt()\n");
restart:
file_progress();
if (blockinit()) // init block data
{
findloops(dfo[], startloop); // Compute Bweights
freeloop(startloop); // free existing loops
startloop_cache = startloop;
return; // can't handle ASM blocks
}
compdom(); // compute dominators
findloops(dfo[], startloop); // find the loops
L3:
while (1)
{
foreach (ref l; startloop)
{
if (looprotate(l)) // rotate the loop
{
compdfo();
blockinit();
compdom();
findloops(dfo[], startloop);
continue L3;
}
}
break;
}
// Make sure there is a preheader for each loop.
addblk = false; /* assume no blocks added */
foreach (ref l; startloop)
{
//if (debugc) l.print();
if (!l.Lpreheader) /* if no preheader */
{
block *h;
block *p;
if (debugc) printf("Generating preheader for loop\n");
addblk = true; /* add one */
p = block_calloc(); // the preheader
h = l.Lhead; /* loop header */
/* Find parent of h */
if (h == startblock)
startblock = p;
else
{
for (auto ph = startblock; 1; ph = ph.Bnext)
{
assert(ph); /* should have found it */
if (ph.Bnext == h)
{
// Link p into block list between ph and h
ph.Bnext = p;
break;
}
}
}
p.Bnext = h;
l.Lpreheader = p;
p.BC = BCgoto;
assert(p.Bsucc == null);
list_append(&(p.Bsucc),h); /* only successor is h */
p.Btry = h.Btry;
if (debugc) printf("Adding preheader %p to loop %p\n",p,&l);
// Move preds of h that aren't in the loop to preds of p
for (list_t bl = h.Bpred; bl;)
{
block *b = list_block(bl);
if (!vec_testbit (b.Bdfoidx, l.Lloop))
{
list_append(&(p.Bpred), b);
list_subtract(&(h.Bpred), b);
bl = h.Bpred; /* dunno what subtract did */
/* Fix up successors of predecessors */
foreach (bls; ListRange(b.Bsucc))
if (list_block(bls) == h)
bls.ptr = cast(void *)p;
}
else
bl = list_next(bl);
}
list_append(&(h.Bpred),p); /* p is a predecessor to h */
}
} /* for */
if (addblk) /* if any blocks were added */
{
compdfo(); /* compute depth-first order */
blockinit();
compdom();
findloops(dfo[], startloop); // recompute block info
addblk = false;
}
/* Do the loop optimizations.
*/
doflow = true; /* do flow analysis */
if (go.mfoptim & MFtime)
{
if (debugc) printf("Starting loop unrolling\n");
L2:
while (1)
{
foreach (ref l; startloop)
{
if (loopunroll(l))
{
compdfo(); // compute depth-first order
blockinit();
compdom();
findloops(dfo[], startloop); // recompute block info
doflow = true;
continue L2;
}
}
break;
}
}
/* Note that accessing the loops
* starting from startloop will access them in least nested
* one first, thus moving LIs out as far as possible
*/
if (debugc) printf("Starting loop invariants\n");
foreach_reverse (ref l; startloop)
{
//if (debugc) l.print();
file_progress();
assert(l.Lpreheader);
if (doflow)
{
flowrd(); /* compute reaching definitions */
flowlv(); /* compute live variables */
flowae(); // compute available expressions
doflow = false; /* no need to redo it */
if (go.defnod.length == 0) /* if no definition elems */
break; /* no need to optimize */
}
lv = l.Lloop;
if (debugc) printf("...Loop %p start...\n",&l);
/* Unmark all elems in this loop */
for (uint i = 0; (i = cast(uint) vec_index(i, lv)) < dfo.length; ++i)
if (dfo[i].Belem)
unmarkall(dfo[i].Belem); /* unmark all elems */
/* Find & mark all LIs */
gin = vec_clone(l.Lpreheader.Bout);
vec_t rd = vec_calloc(go.defnod.length); /* allocate our running RD vector */
for (uint i = 0; (i = cast(uint) vec_index(i, lv)) < dfo.length; ++i) // for each block in loop
{
block *b = dfo[i];
if (debugc) printf("B%d\n",i);
if (b.Belem)
{
vec_copy(rd, b.Binrd); // IN reaching defs
static if (0)
{
printf("i = %d\n",i);
{
for (int j = 0; j < go.defnod.length; j++)
elem_print(go.defnod[j].DNelem);
}
printf("rd : "); vec_println(rd);
}
gblock = b;
gref = 0;
if (b != l.Lhead)
gref = 1;
markinvar(b.Belem, rd);
static if (0)
{
printf("B%d\n", i);
{
foreach (j; 0 .. go.defnod.length)
{
printf(" [%2d] ", j);
WReqn(go.defnod[j].DNelem);
printf("\n");
}
}
printf("rd : "); vec_println(rd);
printf("Boutrd: "); vec_println(b.Boutrd);
}
assert(vec_equal(rd, b.Boutrd));
}
else
assert(vec_equal(b.Binrd, b.Boutrd));
}
vec_free(rd);
vec_free(gin);
/* Move loop invariants */
for (uint i = 0; (i = cast(uint) vec_index(i, lv)) < dfo.length; ++i)
{
uint domexit; // true if this block dominates all
// exit blocks of the loop
for (uint j = 0; (j = cast(uint) vec_index(j, l.Lexit)) < dfo.length; ++j) // for each exit block
{
if (!vec_testbit (i, dfo[j].Bdom))
{
domexit = 0;
goto L1; // break if !(i dom j)
}
}
// if i dom (all exit blocks)
domexit = 1;
L1:
if (dfo[i].Belem)
{ // If there is any hope of making an improvement
if (domexit || l.Llis.length)
{
//if (dfo[i] != l.Lhead)
//domexit |= 2;
movelis(dfo[i].Belem, dfo[i], l, domexit);
}
}
}
if (debugc) printf("...Loop %p done...\n",&l);
if (go.mfoptim & MFliv)
{
loopiv(l); /* induction variables */
if (addblk) /* if we added a block */
{
compdfo();
goto restart; /* play it safe and start over */
}
}
} /* for */
freeloop(startloop);
startloop_cache = startloop;
}
/*****************************
* If elem is loop invariant, mark it.
* Input:
* lv = vector of all the blocks in this loop.
* rd = vector of loop invariants for this elem. This must be
* continually updated.
* Note that we do not iterate until no more LIs are found. The only
* thing this would buy us is stuff that depends on LI assignments.
*/
@trusted
private void markinvar(elem *n,vec_t rd)
{
vec_t tmp;
uint i;
Symbol *v;
elem *n1;
assert(n && rd);
assert(vec_numbits(rd) == go.defnod.length);
switch (n.Eoper)
{
case OPaddass: case OPminass: case OPmulass: case OPandass:
case OPorass: case OPxorass: case OPdivass: case OPmodass:
case OPshlass: case OPshrass: case OPashrass:
case OPpostinc: case OPpostdec:
case OPcall:
case OPvecsto:
case OPcmpxchg:
markinvar(n.EV.E2,rd);
goto case OPnegass;
case OPnegass:
n1 = n.EV.E1;
if (n1.Eoper == OPind)
markinvar(n1.EV.E1,rd);
else if (OTbinary(n1.Eoper))
{ markinvar(n1.EV.E1,rd);
markinvar(n1.EV.E2,rd);
}
L2:
if (n.Eoper == OPcall ||
gblock.Btry ||
!(n1.Eoper == OPvar &&
symbol_isintab(n1.EV.Vsym)))
{
gref = 1;
}
updaterd(n,rd,null);
break;