-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopt.c
2300 lines (2191 loc) · 64.2 KB
/
opt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $VER: vbcc (opt.c) $Revision: 1.52 $ */
/* allgemeine Routinen fuer den Optimizer und Steuerung der einzelnen */
/* Laeufe */
#include "opt.h"
#include "vbc.h"
static char FILE_[]=__FILE__;
/* die naechsten Funktionen sollten evtl. in ic.c */
/* Sind use/change-Listen initialisiert? */
int have_alias;
int static_cse=1,dref_cse=1;
int no_eff_ics,early_eff_ics;
#ifdef ALEX_REG
extern flowgraph *pFg;
#endif
void insert_IC(IC *p,IC *new)
/* fuegt new hinter p ein; p darf 0 sein */
{
if((new->code==ADDI2P||new->code==SUBIFP||new->code==SUBPFP)&&!ISPOINTER(new->typf2))
ierror(0);
new->prev=p;
if(p){
new->next=p->next; p->next=new;
}else{
new->next=first_ic; first_ic=new;
}
if(new->next)
new->next->prev=new;
else
last_ic=new;
new->q1.am=new->q2.am=new->z.am=0;
}
#ifndef NO_OPTIMIZER
int gchanged; /* Merker, ob Optimierungslauf etwas geaendert hat */
int norek; /* diese Funktion wird nicht rekursiv auf */
int nocall; /* diese Funktion kehrt nicht zum Caller zurueck */
void fix_shortop(flowgraph *fg,IC *p)
{
if((p->typf&NQ)<INT&&!shortcut(p->code,p->typf)){
static type ta;
type *t;
IC *new;
if(DEBUG&1024){puts("fix shortop");pric2(stdout,p);puts("to;");}
ta.flags=int_erw(p->typf);
t=arith_typ(&ta,&ta);
new=new_IC();
new->code=CONVERT;
new->q1=p->q1;
new->z.flags=VAR;
new->z.v=add_tmp_var(clone_typ(t));
new->typf2=p->typf|UNSIGNED;
new->typf=t->flags|UNSIGNED;
p->q1=new->z;
insert_IC_fg(fg,p->prev,new);
new=new_IC();
new->code=CONVERT;
new->q1=p->q2;
new->z.flags=VAR;
new->z.v=add_tmp_var(clone_typ(t));
new->typf2=p->typf|UNSIGNED;
new->typf=t->flags|UNSIGNED;
p->q2=new->z;
insert_IC_fg(fg,p->prev,new);
new=new_IC();
new->code=CONVERT;
new->z=p->z;
new->q1.flags=VAR;
new->q1.v=add_tmp_var(t);
new->typf=p->typf|UNSIGNED;
new->typf2=t->flags|UNSIGNED;
p->z=new->q1;
insert_IC_fg(fg,p,new);
p->typf=t->flags|UNSIGNED;
if(DEBUG&1024){
pric2(stdout,p->prev->prev);
pric2(stdout,p->prev);
pric2(stdout,p);
pric2(stdout,p->next);
}
}
}
#if HAVE_LIBCALLS
extern np gen_libcall(char *fname,np arg1,type *t1,np arg2,type *t2);
/* insert libcalls just before register allocation */
static int insert_libcalls(flowgraph *fg)
{
IC *p,*next,*add;
int replaced=0;
static node n,nl,nr;
static type t,tl,tr;
if(DEBUG&1024) printf("insert_libcalls\n");
while(fg){
for(p=fg->start;p;p=next){
int c=p->code,end=0;
char *libname;
next=p->next;
if((c>=OR&&c<=AND)||(c>=LSHIFT&&c<=KOMPLEMENT)||c==COMPARE||c==CONVERT||c==MINUS||c==TEST){
if(libname=use_libcall(c,p->typf,p->typf2)){
IC *merk_first,*merk_last;
static node n1,n2;
static type t1,t2;
if(DEBUG&1024){
printf("converting IC to libcall:\n");
pric2(stdout,p);
}
#if FIXED_SP
{
/* move nested pushs */
IC *m;flowgraph *g=fg;int fd=0,i;
for(i=0;i<p->arg_cnt;i++){
m=p->arg_list[i];
if(m->code==PUSH){
Var *v;type *t;IC *new;
fd=1;
if(DEBUG&1024){
printf("moving push\n");
pric2(stdout,m);
}
if((m->typf&NQ)==STRUCT) ierror(0);
if((m->typf&NQ)==CHAR&&!zmeqto(m->q2.val.vmax,Z1)) ierror(0);
t=new_typ();
t->flags=m->typf;
v=add_tmp_var(t);
new=new_IC();
new->code=PUSH;
new->q2.val.vmax=m->q2.val.vmax;
new->typf=m->typf;
new->q1.flags=VAR;
new->q1.v=v;
new->q1.val.vmax=Z0;
m->z=new->q1;
m->code=ASSIGN;
insert_IC_fg(g,p,new);
if(DEBUG&1024){
pric2(stdout,m);
pric2(stdout,new);
}
p->arg_list[i]=new;
}
}
}
#endif
replaced=1;
merk_last=last_ic;
merk_first=first_ic;
first_ic=last_ic=0;
n1.flags=REINTERPRET;
n1.o=p->q1;
n1.ntyp=&t1;
t1.flags=q1typ(p);
if(p->q2.flags){
n2.flags=REINTERPRET;
n2.o=p->q2;
n2.ntyp=&t2;
t2.flags=q2typ(p);
gen_libcall(libname,&n1,&t1,&n2,&t2);
}else
gen_libcall(libname,&n1,&t1,0,0);
if(!last_ic||last_ic->code!=GETRETURN) ierror(0);
last_ic->z=p->z;
add=first_ic;
last_ic=merk_last;
first_ic=merk_first;
for(;add;add=next){
next=add->next;
insert_IC_fg(fg,p->prev,add);
}
next=p->next;
if(fg->end==p) end=1;
if(p->z.flags){
remove_IC_fg(fg,p);
}else{
type *t=new_typ();
t->flags=LIBCALL_CMPTYPE;
p->code=COMPARE;
p->q2.flags=KONST;
gval.vmax=Z0;
eval_const(&gval,MAXINT);
insert_const(&p->q2.val,LIBCALL_CMPTYPE);
p->typf=LIBCALL_CMPTYPE;
p->q1.flags=VAR;
p->q1.v=add_tmp_var(t);
p->q1.val.vmax=l2zm(0L);
p->prev->z=p->q1;
p->z.flags=0;
}
}
}
if(end||p==fg->end) break;
}
fg=fg->normalout;
}
return replaced;
}
#endif
/* temporary fuer verschiedene Bitvektoren */
bvtype *tmp;
int in_varlist(varlist *vl,int cnt,Var *v,int flags)
{
int i;
/*FIXME: slow */
for(i=0;i<cnt;i++){
if(vl[i].v==v&&vl[i].flags==flags) return 1;
}
return 0;
}
static void add_call_list(void)
{
IC *p;
for(p=first_ic;p;p=p->next){
if(p->code==CALL&&p->call_cnt==0){
if((p->q1.flags&(VAR|DREFOBJ))==VAR){
p->call_cnt=1;
p->call_list=mymalloc(sizeof(*p->call_list));
p->call_list[0].v=p->q1.v;
p->call_list[0].flags=0;
}
}
}
}
void calc_finfo(Var *v,int flags)
{
IC *p;int i,known,maxtyp,t,c;
/* 128 types should be enough for everyone :-) */
#define TSIZE BVSIZE(128)
static bvtype tf[TSIZE];
if(!v->fi) ierror(0);
if(flags&CALC_USES){
if(v->fi->flags&ALL_USES) return;
free(v->fi->use_list);
v->fi->use_cnt=0;
v->fi->use_list=0;
memset(tf,0,TSIZE);
known=1;maxtyp=-1;
for(p=first_ic;p;p=p->next){
c=p->code;
if(p->q1.flags&DREFOBJ){
t=(q1typ(p)&NQ);
if(t>maxtyp) maxtyp=t;
BSET(tf,t);
}
if(p->q2.flags&DREFOBJ){
t=(q2typ(p)&NQ);
if(t>maxtyp) maxtyp=t;
BSET(tf,t);
}
if(c==CALL){
if((p->q1.flags&(VAR|DREFOBJ))!=VAR||!p->q1.v->fi||!(p->q1.v->fi->flags&ALL_USES)){
free(v->fi->use_list);
v->fi->use_cnt=0;
v->fi->use_list=0;
known=0;
break;
}else{
for(i=0;i<p->q1.v->fi->use_cnt;i++){
if(p->q1.v->fi->use_list[i].v){
if(!in_varlist(v->fi->use_list,v->fi->use_cnt,p->q1.v->fi->use_list[i].v,p->q1.v->fi->use_list[i].flags)){
v->fi->use_cnt++;
v->fi->use_list=myrealloc(v->fi->use_list,v->fi->use_cnt*sizeof(varlist));
v->fi->use_list[v->fi->use_cnt-1]=p->q1.v->fi->use_list[i];
}
}else{
BSET(tf,p->q1.v->fi->use_list[i].flags&NQ);
}
}
}
}else{
for(i=0;i<p->use_cnt;i++){
if(p->use_list[i].v->nesting!=0) continue;
if(!in_varlist(v->fi->use_list,v->fi->use_cnt,p->use_list[i].v,p->use_list[i].flags)){
v->fi->use_cnt++;
v->fi->use_list=myrealloc(v->fi->use_list,v->fi->use_cnt*sizeof(varlist));
v->fi->use_list[v->fi->use_cnt-1]=p->use_list[i];
}
}
}
}
for(c=0;c<=maxtyp;c++){
if(BTST(tf,c)){
v->fi->use_cnt++;
v->fi->use_list=myrealloc(v->fi->use_list,v->fi->use_cnt*sizeof(varlist));
v->fi->use_list[v->fi->use_cnt-1].v=0;
v->fi->use_list[v->fi->use_cnt-1].flags=c;
}
}
if(known) v->fi->flags|=ALL_USES;
}
if(flags&CALC_CHANGES){
if(v->fi->flags&ALL_MODS) return;
free(v->fi->change_list);
v->fi->change_cnt=0;
v->fi->change_list=0;
memset(tf,0,TSIZE);
known=1;maxtyp=-1;
for(p=first_ic;p;p=p->next){
c=p->code;
if(p->z.flags&DREFOBJ){
t=(ztyp(p)&NQ);
if(t>maxtyp) maxtyp=t;
BSET(tf,t);
}
if(c==CALL){
if((p->q1.flags&(VAR|DREFOBJ))!=VAR||!p->q1.v->fi||!(p->q1.v->fi->flags&ALL_MODS)){
free(v->fi->change_list);
v->fi->change_cnt=0;
v->fi->change_list=0;
known=0;
break;
}else{
for(i=0;i<p->q1.v->fi->change_cnt;i++){
if(p->q1.v->fi->change_list[i].v){
if(!in_varlist(v->fi->change_list,v->fi->change_cnt,p->q1.v->fi->change_list[i].v,p->q1.v->fi->change_list[i].flags)){
v->fi->change_cnt++;
v->fi->change_list=myrealloc(v->fi->change_list,v->fi->change_cnt*sizeof(varlist));
v->fi->change_list[v->fi->change_cnt-1]=p->q1.v->fi->change_list[i];
}
}else{
BSET(tf,p->q1.v->fi->change_list[i].flags&NQ);
}
}
}
}else{
for(i=0;i<p->change_cnt;i++){
if(p->change_list[i].v->nesting!=0) continue;
if(!in_varlist(v->fi->change_list,v->fi->change_cnt,p->change_list[i].v,p->change_list[i].flags)){
v->fi->change_cnt++;
v->fi->change_list=myrealloc(v->fi->change_list,v->fi->change_cnt*sizeof(varlist));
v->fi->change_list[v->fi->change_cnt-1]=p->change_list[i];
}
}
}
}
for(c=0;c<=maxtyp;c++){
if(BTST(tf,c)){
v->fi->change_cnt++;
v->fi->change_list=myrealloc(v->fi->change_list,v->fi->change_cnt*sizeof(varlist));
v->fi->change_list[v->fi->change_cnt-1].v=0;
v->fi->change_list[v->fi->change_cnt-1].flags=c;
}
}
if(known) v->fi->flags|=ALL_MODS;
}
if(flags&CALC_CALLS){
if(v->fi->flags&ALL_CALLS) return;
free(v->fi->call_list);
v->fi->call_list=0;
v->fi->call_cnt=0;
p=v->fi->opt_ic;
if(!p)
p=v->fi->first_ic;
for(;p;p=p->next){
if(p->code==CALL){
if(p->q1.flags&DREFOBJ) return;
if(!in_varlist(v->fi->call_list,v->fi->call_cnt,p->q1.v,0)){
v->fi->call_cnt++;
v->fi->call_list=myrealloc(v->fi->call_list,v->fi->call_cnt*sizeof(varlist));
v->fi->call_list[v->fi->call_cnt-1].v=p->q1.v;
v->fi->call_list[v->fi->call_cnt-1].flags=0;
}
}
}
v->fi->flags|=ALL_CALLS;
}
}
void used_clist(type *t,const_list *cl)
{
int i;zmax l;
if(ISARRAY(t->flags)){
for(l=l2zm(0L);!zmleq(t->size,l)&&cl;l=zmadd(l,l2zm(1L)),cl=cl->next){
if(!cl->other){ierror(0);return;}
used_clist(t->next,cl->other);
}
return;
}
if(ISUNION(t->flags)&&!cl->tree){
used_clist((*t->exact->sl)[zm2l(cl->idx)].styp,cl->other);
return;
}
if(ISSTRUCT(t->flags)){
type *st;
if(!cl||!cl->tree){
for(i=0;i<t->exact->count&&cl;i++){
st=(*t->exact->sl)[i].styp;
if(!(*t->exact->sl)[i].identifier) ierror(0);
if(zmeqto(l2zm((long)i),cl->idx)){
if((*t->exact->sl)[i].identifier[0]){
if(cl->other) used_clist(st,cl->other);
cl=cl->next;
}
}
}
}
return;
}
if(cl->tree&&(cl->tree->o.flags&VAR))
used_objects(cl->tree->o.v);
return;
}
void used_objects(Var *v)
{
int i;
if(v->flags&REFERENCED) return;
v->flags|=REFERENCED;
if(ISFUNC(v->vtyp->flags)){
IC *p;
if(!(v->flags&DEFINED)) return;
if(!v->fi) ierror(0);
for(i=0;i<v->fi->call_cnt;i++){
if(v->fi->call_list[i].v->flags&DEFINED)
used_objects(v->fi->call_list[i].v);
}
/*FIXME: use/change aus fi */
for(p=v->fi->opt_ic;p;p=p->next){
if(p->q1.flags&VAR) used_objects(p->q1.v);
if(p->q2.flags&VAR) used_objects(p->q2.v);
if(p->z.flags&VAR) used_objects(p->z.v);
}
}else{
if(v->clist) used_clist(v->vtyp,v->clist);
}
}
zmax recalc_start_offset;
static void mark_na(bvtype *tmp,int r,int *eqto)
{
int i;
if(r<0||r>=vcount-rcount||BTST(tmp,r)) return;
/*if(DEBUG&1024) printf("mark_na %s(%p):\n",vilist[r]->identifier,(void*)vilist[r]);*/
BSET(tmp,r);
for(i=0;i<vcount-rcount;i++){
if(eqto[i]!=r&&eqto[r]!=i) continue;
/*if((DEBUG&1024)&&!BTST(tmp,i)) printf(" mark_na %s(%p)\n",vilist[i]->identifier,(void*)vilist[i]);*/
mark_na(tmp,i,eqto);
}
}
/* TODO: remove ugly hack by decent exit criteria */
static int reccheck_maxcnt;
/* check if function v can not call callee */
static int check_nonrecursive(Var *v,Var *callee)
{
int i;
Var *f;
if(--reccheck_maxcnt==0) return 0;
if(v->fi&&(v->fi->flags&ALL_CALLS)){
for(i=0;i<v->fi->call_cnt;i++){
f=v->fi->call_list[i].v;
if(f==v)
return 0;
if(!check_nonrecursive(f,callee))
return 0;
}
return 1;
}
return 0;
}
static void cnv_static(Var *v)
{
if((v->storage_class==AUTO||v->storage_class==REGISTER)&&zmleq(Z0,v->offset)&&v->reg==0/*&&!(v->flags&CONVPARAMETER)*/){
v->storage_class=STATIC;
v->offset=++label;
v->flags|=(USEDASSOURCE|USEDASDEST|DEFINED|STATICAUTO);
if(v->clist){free_clist(v->clist);v->clist=0;}
if(DEBUG&1024) printf("changing %s(%p) to static (L%ld)\n",v->identifier,v,zm2l(v->offset));
}
}
/* replace auto variables by statics if possible */
void replace_statics(Var *v)
{
IC *p;
if(DEBUG&1024) printf("replace_statics()\n");
if(force_statics||(v->fi&&(v->fi->flags&ALL_CALLS))){
if(!force_statics){
reccheck_maxcnt=100;
if(!check_nonrecursive(v,v))
return;
if(DEBUG&1024) printf("non-recursive\n");
}
for(p=first_ic;p;p=p->next){
if((p->q1.flags&(VAR|REG))==VAR) cnv_static(p->q1.v);
if((p->q2.flags&(VAR|REG))==VAR) cnv_static(p->q2.v);
if((p->z.flags&(VAR|REG))==VAR) cnv_static(p->z.v);
if(p->code==ADDRESS&&p->q1.v->storage_class==STATIC){
p->code=ASSIGN;
p->q1.flags|=VARADR;
p->q2.val.vmax=sizetab[p->typf2&NQ];
p->typf=p->typf2;
}
}
}
}
void recalc_offsets(flowgraph *g)
/* berechnet Offsets fuer auto-Variablen neu und versucht, fuer Variablen, */
/* die nicht gleichzeitig aktiv sind, den gleichen Platz zu belegen */
{
int i,j,b,bcnt,r,*eqto,pass;size_t bsize,vsize;zmax *al,*sz;
bvtype **used,*tmp,*empty,*vtmp,*vpos;
IC *p;
flowgraph *fg;
if(DEBUG&1024) printf("recalculating offsets\n");
if(DEBUG&1024) printf("setting up arrays\n");
/*bsize=(basic_blocks+CHAR_BIT-1)/CHAR_BIT; /* we don's use fg->index but count from 0 */
bsize=BVSIZE(basic_blocks);
if(DEBUG&1024) printf("bsize=%lu\n",(unsigned long)bsize);
tmp=mymalloc(bsize);
al=mymalloc(sizeof(*al)*(vcount-rcount));
eqto=mymalloc(sizeof(int)*(vcount-rcount));
sz=mymalloc(sizeof(*sz)*(vcount-rcount));
empty=mymalloc(bsize);
memset(empty,0,bsize);
used=mymalloc(sizeof(bvtype *)*(vcount-rcount));
/* Tabelle, welche Variable in welchem Block belegt ist, aufbauen */
for(i=0;i<vcount-rcount;i++){
if((vilist[i]->storage_class==AUTO||vilist[i]->storage_class==REGISTER)&&zmleq(l2zm(0L),vilist[i]->offset)){
if(DEBUG&2048) printf("setting up for %s,%ld\n",vilist[i]->identifier,zm2l(vilist[i]->offset));
used[i]=mymalloc(bsize);
memset(used[i],0,bsize);
}else{
used[i]=0;
}
sz[i]=szof(vilist[i]->vtyp);
al[i]=falign(vilist[i]->vtyp);
eqto[i]=-1;
}
b=0; fg=g;
while(fg){
if(b>=basic_blocks) ierror(0);
for(i=0;i<vcount-rcount;i++){
if(used[i]&&(BTST(fg->av_in,i)||BTST(fg->av_out,i))){
BSET(used[i],b);
for(r=1;r<=MAXR;r++)
if(fg->regv[r]&&fg->regv[r]->index==i) BCLR(used[i],b);
}
}
for(p=fg->start;p;p=p->next){
if((p->q1.flags&(VAR|REG))==VAR){
i=p->q1.v->index;
if(i!=-1&&i<vcount-rcount){
if(i<0||i>=vcount) ierror(0);
if(i>=0&&used[i]){
BSET(used[i],b);
}
}
}
if((p->q2.flags&(VAR|REG))==VAR){
i=p->q2.v->index;
if(i!=-1&&i<vcount-rcount){
if(i<0||i>=vcount) ierror(0);
if(i>=0&&used[i]){
BSET(used[i],b);
}
}
}
if((p->z.flags&(VAR|REG))==VAR){
i=p->z.v->index;
if(i!=-1){
if(i<0||i>=vcount) ierror(0);
if(i>=0&&used[i]){
BSET(used[i],b);
}
}
}
if(p==fg->end) break;
}
fg=fg->normalout;
b++;
}
bcnt=b;
if(DEBUG&1024) printf("local recalc\n");
vsize=BVSIZE(vcount-rcount);
vtmp=mymalloc(vsize);
vpos=mymalloc(vsize);
j=0;
for(fg=g;fg;fg=fg->normalout){
bvcopy(vtmp,fg->av_in,vsize);
bvunite(vtmp,fg->av_out,vsize);
for(r=1;r<=MAXR;r++)
if(fg->regv[r]&&fg->regv[r]->index<vcount-rcount) BSET(vtmp,fg->regv[r]->index);
for(i=0;i<vcount-rcount;i++){
if(!BTST(vtmp,i)){
if(!used[i]||!BTST(used[i],j)||(vilist[i]->storage_class!=AUTO&&vilist[i]->storage_class!=REGISTER))
BSET(vtmp,i);
else{
for(r=0;r<bcnt;r++)
if(r!=j&&BTST(used[i],r))
BSET(vtmp,i);
}
}
}
#if 1
if(DEBUG&1024){
printf("possible for local recalc block %d (%d):\n",j,fg->index);
for(i=0;i<vcount-rcount;i++)
if(!BTST(vtmp,i))
printf(" %s(%p)\n",vilist[i]->identifier,(void*)vilist[i]);
}
#endif
for(p=fg->start;p;p=p->next){
if((p->z.flags&(VAR|DREFOBJ))==VAR){
b=p->z.v->index;
if(b>=0&&b<vcount-rcount&&eqto[b]<0&&!BTST(vtmp,b)){
/* imprecise, but should do the job */
IC *m;
/*if(DEBUG&1024) printf("checking %s(%p)\n",vilist[b]->identifier,(void*)vilist[b]);*/
bvcopy(vpos,vtmp,vsize);
for(m=p->next;m;m=m->next){
if(m->q1.flags&VAR) mark_na(vpos,m->q1.v->index,eqto);
if(m->q2.flags&VAR) mark_na(vpos,m->q2.v->index,eqto);
if(m->z.flags&VAR){
mark_na(vpos,m->z.v->index,eqto);
if(m->z.v->index==b) BSET(vtmp,b);
}
if(m==fg->end) break;
}
for(i=vcount-rcount-1;i>=0;i--){
if(!BTST(vpos,i)&&i!=b){
if(DEBUG&1024) printf("local memory for %s(%p) and %s(%p) equal",vilist[i]->identifier,(void *)vilist[i],vilist[b]->identifier,(void *)vilist[b]);
eqto[b]=i;
if(!zmleq(al[b],al[i])) al[i]=al[b];
if(!zmleq(sz[b],sz[i])) sz[i]=sz[b];
if(!zmleq(al[i],al[b])) al[b]=al[i];
if(!zmleq(sz[i],sz[b])) sz[b]=sz[i];
if(DEBUG&1024) printf(" sz=%ld al=%ld\n",(long)zm2l(al[i]),(long)zm2l(sz[i]));
BSET(vtmp,i);
/*bvunite(used[i],used[b],bsize);*/
break;
}
}
}
}
if(p==fg->end) break;
}
j++;
}
free(vtmp);
free(vpos);
/* schauen, ob Variablen in gleichen Speicher koennen */
if(DEBUG&1024) printf("looking for distinct variables\n");
for(i=0;i<vcount-rcount;i++){
if(!used[i]||eqto[i]>=0) continue;
if(!memcmp(used[i],empty,bsize)){ free(used[i]);used[i]=0;continue;}
for(b=i+1;b<vcount-rcount;b++){
if(!used[b]||eqto[b]>=0) continue;
if(!memcmp(used[b],empty,bsize)){ free(used[b]);used[b]=0;continue;}
if(DEBUG&2048) printf("comparing %s(%p) and %s(%p)\n",vilist[i]->identifier,(void *)vilist[i],vilist[b]->identifier,(void *)vilist[b]);
memcpy(tmp,used[i],bsize);
bvintersect(tmp,used[b],bsize);
if(!memcmp(tmp,empty,bsize)){
if(DEBUG&1024) printf("memory for %s(%p) and %s(%p) equal\n",vilist[i]->identifier,(void *)vilist[i],vilist[b]->identifier,(void *)vilist[b]);
eqto[b]=i;
if(!zmleq(al[b],al[i])) al[i]=al[b];
if(!zmleq(sz[b],sz[i])) sz[i]=sz[b];
if(!zmleq(al[i],al[b])) al[b]=al[i];
if(!zmleq(sz[i],sz[b])) sz[b]=sz[i];
bvunite(used[i],used[b],bsize);
}
}
}
if(DEBUG&1024) printf("propagating sz/al\n");
do{
if(DEBUG&1024) printf("ppass\n");
b=0;
for(i=0;i<vcount-rcount;i++){
j=i;
while((j=eqto[j])>=0){
if(!zmleq(sz[j],sz[i])){sz[i]=sz[j];b=1;j=i;if(DEBUG&1024) printf("lmset %d(%p) to sz %ld\n",i,vilist[i],zm2l(sz[i]));}
if(!zmleq(sz[i],sz[j])){sz[j]=sz[i];b=1;if(DEBUG&1024) printf("lmset %d(%p) to sz %ld\n",j,vilist[j],zm2l(sz[j]));}
if(!zmleq(al[j],al[i])){al[i]=al[j];b=1;j=i;if(DEBUG&1024) printf("lmset %d(%p) to al %ld\n",i,vilist[i],zm2l(al[i]));}
if(!zmleq(al[i],al[j])){al[j]=al[i];b=1;if(DEBUG&1024) printf("lmset %d(%p) to al %ld\n",j,vilist[j],zm2l(al[j]));}
}
}
}while(b);
if(DEBUG&1024) printf("final recalculating\n");
max_offset=recalc_start_offset;
for(pass=0;pass<3;pass++){
zmax a;
for(a=maxalign;!zmeqto(a,Z0);a=zmsub(a,Z1)){
for(i=0;i<vcount-rcount;i++){
if(!used[i]) continue;
if(!zmeqto(a,al[i])) continue;
if(eqto[i]>=0&&pass<2) continue;
if(pass==0){
if(!zmleq(sz[i],sizetab[MAXINT])&&!zmleq(sz[i],sizetab[LDOUBLE])) continue;
}
free(used[i]);
used[i]=0;
if(DEBUG&2048) printf("adjusting offset for %s,%ld(%p)\n",vilist[i]->identifier,zm2l(vilist[i]->offset),(void*)vilist[i]);
if(eqto[i]>=0){
j=eqto[i];
do{
}while(used[j]&&(j=eqto[j])>=0);
if(j<0) ierror(0);
if(!zmleq(sz[i],sz[j]))
{printf("%d(%p) %d(%p) %ld %ld\n",i,vilist[i],j,vilist[j],zm2l(sz[i]),zm2l(sz[j]));ierror(0);}
if(!zmleq(al[i],al[j]))
{printf("%d(%p) %d(%p) %ld %ld\n",i,vilist[i],j,vilist[j],zm2l(al[i]),zm2l(al[j]));ierror(0);}
vilist[i]->offset=vilist[j]->offset;
if(DEBUG&2048) printf("set to %ld (eqto)\n",(long)zm2l(vilist[i]->offset));
continue;
}
vilist[i]->offset=zmmult(zmdiv(zmadd(max_offset,zmsub(al[i],l2zm(1L))),al[i]),al[i]);
max_offset=zmadd(vilist[i]->offset,sz[i]);
if(DEBUG&2048) printf("set to %ld (std)\n",(long)zm2l(vilist[i]->offset));
}
}
}
recalc_start_offset=l2zm(0L);
free(used);
free(sz);
free(al);
free(tmp);
free(empty);
free(eqto);
}
void remove_IC_fg(flowgraph *g,IC *p)
/* Entfernt IC p und beachtet Flussgraph. Ausserdem werden */
/* use/change-Listen freigegeben. */
{
if(p->q1.am||p->q2.am||p->z.am) ierror(0);
if(have_alias){
free(p->use_list);
free(p->change_list);
}
if(g->start==g->end){
g->start=g->end=0;
}else{
if(p==g->end) g->end=p->prev;
if(p==g->start) g->start=p->next;
}
remove_IC(p);
}
/* function to decide whether a possible const memcpy shall
be implemented using typ t; if noptr is non-zero decide
if not introducing a pointer is preferable */
int decide_const_memcpy(IC *p,int typ,int noptr)
{
zmax cnt;
/* usually int is the most efficient */
if(typ==CHAR){
}else if(typ==BESTCOPYT){
if((p->z.flags&(VAR|DREFOBJ))!=VAR)
return 0;
if(!zmleq(align[typ],falign(p->z.v->vtyp)))
return 0;
if(!zmeqto(zmmod(p->z.val.vmax,align[typ]),Z0))
return 0;
}else
return 0;
cnt=zmdiv(p->q2.val.vmax,sizetab[typ]);
if((p->z.flags&(DREFOBJ|KONST))!=DREFOBJ){
int sc;
if(p->z.flags&DREFOBJ)
sc=STATIC;
else
sc=p->z.v->storage_class;
if(sc==AUTO||sc==REGISTER){
/* we assume that writes on the stack can be combined */
if(typ==CHAR&&noptr)
if(zmleq(cnt,zmmult(clist_copy_pointer,sizetab[BESTCOPYT])))
return 1;
if(zmleq(cnt,l2zm(clist_copy_stack)))
return 1;
}else{
if(zmleq(cnt,l2zm(clist_copy_static)))
return 1;
}
}
if(noptr==0&&zmleq(cnt,l2zm(clist_copy_pointer)))
return 1;
return 0;
}
int insert_const_memcpy(flowgraph *fg)
{
int changed =0;
flowgraph *g;
for(g=fg;g;g=g->normalout){
IC *p;
for(p=g->start;p;p=p->next){
int c=p->code;
/* TODO: finish support for PUSH */
if((c==ASSIGN/*||c==PUSH*/)&&(p->q1.flags&(VAR|VARADR|DREFOBJ))==VAR&&p->q1.v->clist&&is_const(p->q1.v->vtyp)){
zmax i,sz=p->q2.val.vmax;
zuchar zuc;
Var *v=p->q1.v;
type *t=v->vtyp;
int ok=1;
for(i=Z0;zmleq(i,sz);i=zmadd(i,Z1)){
if(!get_clist_byte(t,v->clist,i,&zuc))
ok=0;
}
if(ok==1){
int typ=0,noptr=1;
int a1,a2;
if(DEBUG&1024) {printf("can replace const memcpy:\n");pric2(stdout,p);}
if(c==ASSIGN&&(p->z.flags&(DREFOBJ|KONST))!=DREFOBJ){
if(decide_const_memcpy(p,BESTCOPYT,noptr))
typ=BESTCOPYT;
else if(decide_const_memcpy(p,CHAR,noptr))
typ=CHAR;
}
if(typ==0){
noptr=0;
if(decide_const_memcpy(p,BESTCOPYT,noptr))
typ=BESTCOPYT;
else if(decide_const_memcpy(p,CHAR,noptr))
typ=CHAR;
}
if(typ){
Var *ptr;
IC *new;
if(DEBUG&1024) printf("will do it (typ=%d noptr=%d)\n",typ,noptr);
if(!noptr){
type *pt;
if(p->z.flags&VAR){
if(p->z.flags&DREFOBJ){
type *new=new_typ();
new->flags=p->z.dtyp&NQ;
new->next=new_typ();
new->next->flags=typ;
pt=new;
}else{
pt=new_typ();
pt->flags=POINTER_TYPE(p->z.v->vtyp);
pt->next=clone_typ(p->z.v->vtyp);
}
}else{
static type ct={CHAR};
type *new;
ct.flags=typ;
new=new_typ();
new->flags=POINTER_TYPE(&ct);
new->next=new_typ();
new->next->flags=typ;
pt=new;
}
ptr=add_tmp_var(pt);
new=new_IC();
new->z.flags=VAR;
new->z.v=ptr;
new->q1=p->z;
if(p->z.flags&DREFOBJ){
new->code=ASSIGN;
new->q1.flags&=~DREFOBJ;
new->typf=ptr->vtyp->flags;
}else if(p->z.v->storage_class==STATIC||p->z.v->storage_class==EXTERN){
new->code=ASSIGN;
new->q1.flags|=VARADR;
new->typf=ptr->vtyp->flags;
}else if(p->z.v->storage_class==AUTO||p->z.v->storage_class==REGISTER){
new->code=ADDRESS;
new->typf=p->z.v->vtyp->flags;
new->typf2=ptr->vtyp->flags;
}else
ierror(0);
new->q2.val.vmax=sizetab[new->typf];
insert_IC_fg(g,p->prev,new);
}
i=Z0;
while(!zmleq(sz,i)){
/* use CHAR for possibly remaining bytes */
if(!zmleq(zmadd(sizetab[typ],i),sz))
typ=CHAR;
new=new_IC();
new->code=p->code;
new->typf=typ;
new->q1.flags=KONST;
if(typ==CHAR){
if(!get_clist_byte(t,v->clist,zmadd(i,p->q1.val.vmax),&zuc))
ierror(0);
new->q1.val.vuchar=zuc;
}else{
int state;
gval.vmax=get_clist_int(t,v->clist,zmadd(i,p->q1.val.vmax),zm2l(sizetab[typ]),&state);
if(!state) ierror(0);
eval_const(&gval,MAXINT);
insert_const(&new->q1.val,typ);
}
new->q2.val.vmax=sizetab[new->typf];
if(new->code==ASSIGN){
if(noptr){
new->z=p->z;
new->z.val.vmax=zmadd(new->z.val.vmax,i);
}else{
new->z.flags=VAR|DREFOBJ;
new->z.v=ptr;
new->z.dtyp=ptr->vtyp->flags;
}
}else
ierror(0); /* TODO: PUSH */
insert_IC_fg(g,p->prev,new);
if(!noptr){
new=new_IC();
new->code=ADDI2P;
new->q1.flags=VAR;
new->q1.v=ptr;
new->z=new->q1;
new->q2.flags=KONST;
new->q2.val.vint=zm2zi(sizetab[typ]);
new->typf=INT;
new->typf2=ptr->vtyp->flags;
insert_IC_fg(g,p->prev,new);
}
i=zmadd(i,sizetab[typ]);
}
remove_IC_fg(g,p);
p=new;
changed=1;
}
}
}
if(p==g->end) break;
}
}
return changed;
}
/* combines assignments of constants into a larger type */
void combine_const_assigns(void)
{
static int ctype[BESTCOPYT],i,j;
IC *p=first_ic;
/* calculate table of combine types */
for(i=CHAR;i<BESTCOPYT;i++){
ctype[i]=0;
for(j=BESTCOPYT;j>CHAR;j--){
if(zmeqto(sizetab[j],zmadd(sizetab[i],sizetab[i]))){
if(DEBUG&1024) printf("possible combine type %s=>%s\n",typname[i],typname[j]);
ctype[i]=j;
break;
}
}
}
while(p){
if(p->code==ASSIGN&&(p->q1.flags&(KONST|DREFOBJ))==KONST&&(p->z.flags&(DREFOBJ|VAR))==VAR){
IC *p2=p->next;
if(p2&&p2->code==ASSIGN&&(p2->q1.flags&(KONST|DREFOBJ))==KONST&&(p2->z.flags&(DREFOBJ|VAR))==VAR&&p->z.v==p2->z.v&&p->typf==p2->typf&&zmeqto(p->q2.val.vmax,p2->q2.val.vmax)&&zmeqto(p2->z.val.vmax,zmadd(p->z.val.vmax,sizetab[p->typf&NQ]))){
int dt,t=p->typf&NQ,sc=p->z.v->storage_class;
if(t<BESTCOPYT&&(dt=ctype[t])){
zmax os;
/* for stack variables we can use the actual offset, for statics
we rely on the type alignment */
if(sc==AUTO||sc==REGISTER)
os=zmadd(p->z.v->offset,p->z.val.vmax);
else{
if(zmeqto(zmmod(falign(p->z.v->vtyp),align[dt]),Z0))
os=p->z.val.vmax;