/
cgelem.c
4824 lines (4395 loc) · 134 KB
/
cgelem.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
// Copyright (C) 1985-1998 by Symantec
// Copyright (C) 2000-2011 by Digital Mars
// All Rights Reserved
// http://www.digitalmars.com
// Written by Walter Bright
/*
* This source file is made available for personal use
* only. The license is in /dmd/src/dmd/backendlicense.txt
* or /dm/src/dmd/backendlicense.txt
* For any other uses, please contact Digital Mars.
*/
#if !SPP
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "cc.h"
#include "oper.h"
#include "global.h"
#include "el.h"
#include "dt.h"
#include "code.h"
#include "type.h"
static char __file__[] = __FILE__; /* for tassert.h */
#include "tassert.h"
extern void error(const char *filename, unsigned linnum, const char *format, ...);
STATIC elem * optelem(elem *,HINT);
STATIC elem * elarray(elem *e);
STATIC elem * eldiv(elem *);
CEXTERN elem * evalu8(elem *);
static int expgoal;
static int again;
static int cgelem_goal;
/*****************************
*/
STATIC elem * cgel_lvalue(elem *e)
{ elem *e1;
elem *e11;
int op;
//printf("cgel_lvalue()\n"); elem_print(e);
e1 = e->E1;
op = e->Eoper;
if (e1->Eoper == OPbit)
{
e11 = e1->E1;
if (e11->Eoper == OPcomma)
{
// Replace (((e,v) bit x) op e2) with (e,((v bit x) op e2))
e1->E1 = e11->E2;
e11->E2 = e;
e11->Ety = e->Ety;
e11->ET = e->ET;
e = e11;
goto L1;
}
else if (OTassign(e11->Eoper))
{
// Replace (((e op= v) bit x) op e2) with ((e op= v) , ((e bit x) op e2))
e1->E1 = el_copytree(e11->E1);
e = el_bin(OPcomma,e->Ety,e11,e);
goto L1;
}
}
else if (e1->Eoper == OPcomma)
{
// Replace ((e,v) op e2) with (e,(v op e2))
e->Eoper = OPcomma;
e1->Eoper = op;
e1->Ety = e->Ety;
e1->ET = e->ET;
e->E1 = e1->E1;
e1->E1 = e1->E2;
e1->E2 = e->E2;
e->E2 = e1;
goto L1;
}
else if (OTassign(e1->Eoper))
{
// Replace ((e op= v) op e2) with ((e op= v) , (e op e2))
e->E1 = el_copytree(e1->E1);
e = el_bin(OPcomma,e->Ety,e1,e);
L1:
e = optelem(e,TRUE);
}
return e;
}
/******************************
* Scan down commas.
*/
STATIC elem * elscancommas(elem *e)
{
while (e->Eoper == OPcomma
#if SCPP
|| e->Eoper == OPinfo
#endif
)
e = e->E2;
return e;
}
/*************************
* Return TRUE if elem is the constant 1.
*/
int elemisone(elem *e)
{
if (e->Eoper == OPconst)
{ switch (tybasic(e->Ety))
{
case TYchar:
case TYuchar:
case TYschar:
case TYchar16:
case TYshort:
case TYushort:
case TYint:
case TYuint:
case TYlong:
case TYulong:
case TYllong:
case TYullong:
case TYnullptr:
#if JHANDLE
case TYjhandle:
#endif
#if TARGET_SEGMENTED
case TYsptr:
case TYcptr:
case TYhptr:
case TYfptr:
case TYvptr:
#endif
case TYnptr:
case TYbool:
case TYwchar_t:
case TYdchar:
if (el_tolong(e) != 1)
goto nomatch;
break;
case TYldouble:
case TYildouble:
if (e->EV.Vldouble != 1)
goto nomatch;
break;
case TYdouble:
case TYidouble:
case TYdouble_alias:
if (e->EV.Vdouble != 1)
goto nomatch;
break;
case TYfloat:
case TYifloat:
if (e->EV.Vfloat != 1)
goto nomatch;
break;
default:
goto nomatch;
}
return TRUE;
}
nomatch:
return FALSE;
}
/*************************
* Return TRUE if elem is the constant -1.
*/
int elemisnegone(elem *e)
{
if (e->Eoper == OPconst)
{ switch (tybasic(e->Ety))
{
case TYchar:
case TYuchar:
case TYschar:
case TYchar16:
case TYshort:
case TYushort:
case TYint:
case TYuint:
case TYlong:
case TYulong:
case TYllong:
case TYullong:
case TYnullptr:
case TYnptr:
#if JHANDLE
case TYjhandle:
#endif
#if TARGET_SEGMENTED
case TYsptr:
case TYcptr:
case TYhptr:
case TYfptr:
case TYvptr:
#endif
case TYbool:
case TYwchar_t:
case TYdchar:
if (el_tolong(e) != -1)
goto nomatch;
break;
case TYldouble:
//case TYildouble:
if (e->EV.Vldouble != -1)
goto nomatch;
break;
case TYdouble:
//case TYidouble:
case TYdouble_alias:
if (e->EV.Vdouble != -1)
goto nomatch;
break;
case TYfloat:
//case TYifloat:
if (e->EV.Vfloat != -1)
goto nomatch;
break;
default:
goto nomatch;
}
return TRUE;
}
nomatch:
return FALSE;
}
/**********************************
* Swap relational operators (like if we swapped the leaves).
*/
unsigned swaprel(unsigned op)
{
assert(op < (unsigned) OPMAX);
if (OTrel(op))
op = rel_swap(op);
return op;
}
/**************************
* Replace e1 by t=e1, replace e2 by t.
*/
STATIC void fixside(elem **pe1,elem **pe2)
{ tym_t tym;
elem *tmp,*e2;
tym = (*pe1)->Ety;
tmp = el_alloctmp(tym);
*pe1 = el_bin(OPeq,tym,tmp,*pe1);
e2 = el_copytree(tmp);
el_free(*pe2);
*pe2 = e2;
}
/****************************
* Compute the 'cost' of evaluating a elem. Could be done
* as Sethi-Ullman numbers, but that ain't worth the bother.
* We'll fake it.
*/
#define cost(n) (opcost[n->Eoper])
/*******************************
* For floating point expressions, the cost would be the number
* of registers in the FPU stack needed.
*/
int fcost(elem *e)
{
int cost;
int cost1;
int cost2;
//printf("fcost()\n");
switch (e->Eoper)
{
case OPadd:
case OPmin:
case OPmul:
case OPdiv:
cost1 = fcost(e->E1);
cost2 = fcost(e->E2);
cost = cost2 + 1;
if (cost1 > cost)
cost = cost1;
break;
case OPcall:
case OPucall:
cost = 8;
break;
case OPneg:
case OPabs:
return fcost(e->E1);
case OPvar:
case OPconst:
case OPind:
default:
return 1;
}
if (cost > 8)
cost = 8;
return cost;
}
/*******************************
* The lvalue of an op= is a conversion operator. Since the code
* generator cannot handle this, we will have to fix it here. The
* general strategy is:
* (conv) e1 op= e2 => e1 = (conv) e1 op e2
* Since e1 can only be evaluated once, if it is an expression we
* must use a temporary.
*/
STATIC elem *fixconvop(elem *e)
{ elem *e1,*e2,*ed,*T;
elem *ex;
elem **pe;
unsigned cop,icop,op;
tym_t tycop,tym,tyme;
static unsigned char invconvtab[] =
{
OPbool, // OPb_8
OPs32_d, // OPd_s32
OPd_s32, // OPs32_d
OPs16_d, /* OPd_s16 */
OPd_s16, /* OPs16_d */
OPu16_d, // OPd_u16
OPd_u16, // OPu16_d
OPu32_d, /* OPd_u32 */
OPd_u32, /* OPu32_d */
OPs64_d, // OPd_s64
OPd_s64, // OPs64_d
OPu64_d, // OPd_u64
OPd_u64, // OPu64_d
OPf_d, // OPd_f
OPd_f, // OPf_d
OP32_16, // OPs16_32
OP32_16, // OPu16_32
OPs16_32, // OP32_16
OP16_8, // OPu8_16
OP16_8, // OPs8_16
OPs8_16, // OP16_8
OP64_32, // OPu32_64
OP64_32, // OPs32_64
OPs32_64, // OP64_32
OP128_64, // OPu64_128
OP128_64, // OPs64_128
OPs64_128, // OP128_64
#if TARGET_SEGMENTED
0, /* OPvp_fp */
0, /* OPcvp_fp */
OPnp_fp, /* OPoffset */
OPoffset, /* OPnp_fp */
OPf16p_np, /* OPnp_f16p */
OPnp_f16p, /* OPf16p_np */
#endif
OPd_ld, // OPld_d
OPld_d, // OPd_ld
OPu64_d, // OPld_u64
};
//dbg_printf("fixconvop before\n");
//elem_print(e);
assert(arraysize(invconvtab) == CNVOPMAX - CNVOPMIN + 1);
assert(e);
tyme = e->Ety;
cop = e->E1->Eoper; /* the conversion operator */
assert(cop <= CNVOPMAX);
if (e->E1->E1->Eoper == OPcomma)
{ /* conv(a,b) op= e2
* =>
* a, (conv(b) op= e2)
*/
elem *ecomma = e->E1->E1;
e->E1->E1 = ecomma->E2;
e->E1->E1->Ety = ecomma->Ety;
ecomma->E2 = e;
ecomma->Ety = e->Ety;
return optelem(ecomma, TRUE);
}
tycop = e->E1->Ety;
tym = e->E1->E1->Ety;
e->E1 = el_selecte1(e->E1); /* dump it for now */
e1 = e->E1;
e1->Ety = tym;
e2 = e->E2;
assert(e1 && e2);
/* select inverse conversion operator */
icop = invconvtab[convidx(cop)];
/* First, let's see if we can just throw it away. */
/* (unslng or shtlng) e op= e2 => e op= (lngsht) e2 */
if (OTwid(e->Eoper) &&
(cop == OPs16_32 || cop == OPu16_32 ||
cop == OPu8_16 || cop == OPs8_16))
{ if (e->Eoper != OPshlass && e->Eoper != OPshrass && e->Eoper != OPashrass)
e->E2 = el_una(icop,tym,e2);
//dbg_printf("after1\n");
//elem_print(e);
return e;
}
/* Oh well, just split up the op and the =. */
op = opeqtoop(e->Eoper); /* convert op= to op */
e->Eoper = OPeq; /* just plain = */
ed = el_copytree(e1); /* duplicate e1 */
/* make: e1 = (icop) ((cop) ed op e2)*/
e->E2 = el_una(icop,e1->Ety,
el_bin(op,tycop,el_una(cop,tycop,ed),
e2));
//printf("after1\n");
//elem_print(e);
if (op == OPdiv &&
tybasic(e2->Ety) == TYcdouble)
{
if (tycop == TYdouble)
{
e->E2->E1->Ety = tybasic(e2->Ety);
e->E2->E1 = el_una(OPc_r, tycop, e->E2->E1);
}
else if (tycop == TYidouble)
{
e->E2->E1->Ety = tybasic(e2->Ety);
e->E2->E1 = el_una(OPc_i, tycop, e->E2->E1);
}
}
if (op == OPdiv &&
tybasic(e2->Ety) == TYcfloat)
{
if (tycop == TYfloat)
{
e->E2->E1->Ety = tybasic(e2->Ety);
e->E2->E1 = el_una(OPc_r, tycop, e->E2->E1);
}
else if (tycop == TYifloat)
{
e->E2->E1->Ety = tybasic(e2->Ety);
e->E2->E1 = el_una(OPc_i, tycop, e->E2->E1);
}
}
/* Handle case of multiple conversion operators on lvalue */
/* (such as (intdbl 8int char += double)) */
ex = e;
pe = &e;
while (OTconv(ed->Eoper))
{ unsigned copx = ed->Eoper;
tym_t tymx;
icop = invconvtab[convidx(copx)];
tymx = ex->E1->E1->Ety;
ex->E1 = el_selecte1(ex->E1); // dump it for now
e1 = ex->E1;
e1->Ety = tymx;
ex->E2 = el_una(icop,e1->Ety,ex->E2);
ex->Ety = tymx;
tym = tymx;
if (ex->Ety != tyme)
{ *pe = el_una(copx, ed->Ety, ex);
pe = &(*pe)->E1;
}
ed = ed->E1;
}
//dbg_printf("after2\n");
//elem_print(e);
e->Ety = tym;
if (tym != tyme &&
!(tyintegral(tym) && tyintegral(tyme) && tysize(tym) == tysize(tyme)))
e = el_una(cop, tyme, e);
if (ed->Eoper == OPbit) /* special handling */
{
ed = ed->E1;
e1 = e1->E1; /* go down one */
}
/* If we have a *, must assign a temporary to the expression */
/* underneath it (even if it's a var, as e2 may modify the var). */
if (ed->Eoper == OPind)
{ T = el_alloctmp(ed->E1->Ety); /* make temporary */
ed->E1 = el_bin(OPeq,T->Ety,T,ed->E1); /* ed: *(T=e) */
el_free(e1->E1);
e1->E1 = el_copytree(T);
}
//dbg_printf("after3\n");
//elem_print(e);
return e;
}
STATIC elem * elerr(elem *e)
{
#ifdef DEBUG
elem_print(e);
#endif
assert(0);
return (elem *)NULL;
}
/* For ops with no optimizations */
STATIC elem * elzot(elem *e)
{ return e; }
/****************************
*/
STATIC elem * elstring(elem *e)
{
#if 0 // now handled by el_convert()
if (!OPTIMIZER)
el_convstring(e); // convert string to OPrelconst
#endif
return e;
}
/************************
*/
#if TARGET_SEGMENTED
/************************
* Convert far pointer to pointer.
*/
STATIC void eltonear(elem **pe)
{ tym_t ty;
elem *e = *pe;
ty = e->E1->Ety;
e = el_selecte1(e);
e->Ety = ty;
*pe = optelem(e,TRUE);
}
#endif
/************************
*/
STATIC elem * elstrcpy(elem *e)
{ tym_t ty;
elem_debug(e);
switch (e->E2->Eoper)
{
#if TARGET_SEGMENTED
case OPnp_fp:
if (OPTIMIZER)
{
eltonear(&e->E2);
e = optelem(e,TRUE);
}
break;
#endif
case OPstring:
/* Replace strcpy(e1,"string") with memcpy(e1,"string",sizeof("string")) */
// As streq
e->Eoper = OPstreq;
type *t = type_allocn(TYarray, tschar);
t->Tdim = strlen(e->E2->EV.ss.Vstring) + 1;
e->ET = t;
t->Tcount++;
e->E1 = el_una(OPind,TYstruct,e->E1);
e->E2 = el_una(OPind,TYstruct,e->E2);
e = el_bin(OPcomma,e->Ety,e,el_copytree(e->E1->E1));
if (el_sideeffect(e->E2))
fixside(&e->E1->E1->E1,&e->E2);
e = optelem(e,TRUE);
break;
}
return e;
}
/************************
*/
STATIC elem * elstrcmp(elem *e)
{
elem_debug(e);
if (OPTIMIZER)
{
#if TARGET_SEGMENTED
if (e->E1->Eoper == OPnp_fp)
eltonear(&e->E1);
#endif
switch (e->E2->Eoper)
{
#if TARGET_SEGMENTED
case OPnp_fp:
eltonear(&e->E2);
break;
#endif
case OPstring:
// Replace strcmp(e1,"string") with memcmp(e1,"string",sizeof("string"))
e->Eoper = OPparam;
e = el_bin(OPmemcmp,e->Ety,e,el_long(TYint,strlen(e->E2->EV.ss.Vstring) + 1));
e = optelem(e,TRUE);
break;
}
}
return e;
}
/****************************
* For OPmemcmp, OPmemcpy, OPmemset.
*/
STATIC elem * elmemxxx(elem *e)
{
elem_debug(e);
if (OPTIMIZER)
{ elem *ex;
ex = e->E1;
switch (e->Eoper)
{ case OPmemcmp:
#if TARGET_SEGMENTED
if (ex->E1->Eoper == OPnp_fp)
eltonear(&ex->E1);
if (ex->E2->Eoper == OPnp_fp)
eltonear(&ex->E2);
#endif
break;
case OPmemset:
#if TARGET_SEGMENTED
if (ex->Eoper == OPnp_fp)
eltonear(&ex);
else
#endif
{
// lvalue OPmemset (nbytes param value)
elem *enbytes = e->E2->E1;
elem *evalue = e->E2->E2;
#if MARS
if (enbytes->Eoper == OPconst && evalue->Eoper == OPconst
/* && tybasic(e->E1->Ety) == TYstruct*/)
{ tym_t tym;
tym_t ety;
int nbytes = el_tolong(enbytes);
targ_llong value = el_tolong(evalue);
elem *e1 = e->E1;
elem *tmp;
if (e1->Eoper == OPcomma || OTassign(e1->Eoper))
return cgel_lvalue(e); // replace (e,v)op=e2 with e,(v op= e2)
switch (nbytes)
{
case CHARSIZE: tym = TYchar; goto L1;
case SHORTSIZE: tym = TYshort; goto L1;
case LONGSIZE: tym = TYlong; goto L1;
case LLONGSIZE: if (intsize == 2)
goto Ldefault;
tym = TYllong; goto L1;
L1:
ety = e->Ety;
memset(&value, value & 0xFF, sizeof(value));
evalue->EV.Vullong = value;
evalue->Ety = tym;
e->Eoper = OPeq;
e->Ety = (e->Ety & ~mTYbasic) | tym;
if (tybasic(e1->Ety) == TYstruct)
e1->Ety = tym;
else
e->E1 = el_una(OPind, tym, e1);
tmp = el_same(&e->E1);
tmp = el_una(OPaddr, ety, tmp);
e->E2->Ety = tym;
e->E2 = el_selecte2(e->E2);
e = el_combine(e, tmp);
e = optelem(e,TRUE);
break;
default:
Ldefault:
break;
}
}
#endif
}
break;
case OPmemcpy:
#if TARGET_SEGMENTED
if (ex->Eoper == OPnp_fp)
eltonear(&e->E1);
#endif
ex = e->E2;
#if TARGET_SEGMENTED
if (ex->E1->Eoper == OPnp_fp)
eltonear(&ex->E1);
#endif
if (ex->E2->Eoper == OPconst)
{
if (!boolres(ex->E2))
{ // Copying 0 bytes, so remove memcpy
e->E2 = e->E1;
e->E1 = ex->E1;
ex->E1 = NULL;
e->Eoper = OPcomma;
el_free(ex);
return optelem(e, TRUE);
}
// Convert OPmemcpy to OPstreq
e->Eoper = OPstreq;
type *t = type_allocn(TYarray, tschar);
t->Tdim = el_tolong(ex->E2);
e->ET = t;
t->Tcount++;
e->E1 = el_una(OPind,TYstruct,e->E1);
e->E2 = el_una(OPind,TYstruct,ex->E1);
ex->E1 = NULL;
el_free(ex);
ex = el_copytree(e->E1->E1);
#if TARGET_SEGMENTED
if (tysize(e->Ety) > tysize(ex->Ety))
ex = el_una(OPnp_fp,e->Ety,ex);
#endif
e = el_bin(OPcomma,e->Ety,e,ex);
if (el_sideeffect(e->E2))
fixside(&e->E1->E1->E1,&e->E2);
return optelem(e,TRUE);
}
break;
default:
assert(0);
}
}
return e;
}
/***********************
* + # (combine offsets with addresses)
* / \ => |
* # c v,c
* |
* v
*/
STATIC elem * eladd(elem *e)
{ elem *e1,*e2;
int sz;
//printf("eladd(%p)\n",e);
targ_size_t ptrmask = ~(targ_size_t)0;
if (NPTRSIZE <= 4)
ptrmask = 0xFFFFFFFF;
L1:
e1 = e->E1;
e2 = e->E2;
if (e2->Eoper == OPconst)
{
#if TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_OPENBSD || TARGET_SOLARIS
if (e1->Eoper == OPrelconst && e1->EV.sp.Vsym->Sfl == FLgot)
goto ret;
#endif
if (e1->Eoper == OPrelconst /* if (&v) + c */
|| e1->Eoper == OPstring
)
{
e1->EV.sp.Voffset += e2->EV.Vpointer;
e1->EV.sp.Voffset &= ptrmask;
e = el_selecte1(e);
goto ret;
}
}
else if (e1->Eoper == OPconst)
{
#if TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_OPENBSD || TARGET_SOLARIS
if (e2->Eoper == OPrelconst && e2->EV.sp.Vsym->Sfl == FLgot)
goto ret;
#endif
if (e2->Eoper == OPrelconst /* if c + (&v) */
|| e2->Eoper == OPstring
)
{
e2->EV.sp.Voffset += e1->EV.Vpointer;
e2->EV.sp.Voffset &= ptrmask;
e = el_selecte2(e);
goto ret;
}
}
if (!OPTIMIZER)
goto ret;
/* Replace ((e + &v) + c) with (e + (&v+c)) */
if (e2->Eoper == OPconst && e1->Eoper == OPadd &&
(e1->E2->Eoper == OPrelconst || e1->E2->Eoper == OPstring))
{
e1->E2->EV.sp.Voffset += e2->EV.Vpointer;
e1->E2->EV.sp.Voffset &= ptrmask;
e = el_selecte1(e);
goto L1;
}
/* Replace ((e + c) + &v) with (e + (&v+c)) */
else if ((e2->Eoper == OPrelconst || e2->Eoper == OPstring) &&
e1->Eoper == OPadd && cnst(e1->E2))
{
e2->EV.sp.Voffset += e1->E2->EV.Vpointer;
e2->EV.sp.Voffset &= ptrmask;
e->E1 = el_selecte1(e1);
goto L1; /* try and find some more */
}
/* Replace (e1 + -e) with (e1 - e) */
else if (e2->Eoper == OPneg)
{ e->E2 = el_selecte1(e2);
e->Eoper = OPmin;
again = 1;
return e;
}
/* Replace (-v + e) with (e + -v) */
else if (e1->Eoper == OPneg && OTleaf(e1->E1->Eoper))
{ e->E1 = e2;
e->E2 = e1; /* swap leaves */
goto L1;
}
/* Replace ((e - e2) + e2) with (e) */
/* The optimizer sometimes generates this case */
else if (!tyfloating(e->Ety) && /* no floating bugs */
e1->Eoper == OPmin &&
el_match(e1->E2,e2) &&
!el_sideeffect(e2))
{ tym_t tym = e->Ety;
e = el_selecte1(el_selecte1(e));
e->Ety = tym; /* retain original type */
return e;
}
/* Replace ((e - #v+c1) + #v+c2) with ((e - c1) + c2) */
else if (e2->Eoper == OPrelconst &&
e1->Eoper == OPmin &&
e1->E2->Eoper == OPrelconst &&
e1->E2->EV.sp.Vsym == e2->EV.sp.Vsym)
{ e2->Eoper = OPconst;
e2->Ety = TYint;
e1->Ety = e1->E1->Ety;
e1->E2->Eoper = OPconst;
e1->E2->Ety = TYint;
{
#if TARGET_SEGMENTED
/* Watch out for pointer types changing, requiring a conversion */
tym_t ety,e11ty;
ety = tybasic(e->Ety);
e11ty = tybasic(e1->E1->Ety);
if (typtr(ety) && typtr(e11ty) &&
tysize[ety] != tysize[e11ty])
{
e = el_una((tysize[ety] > tysize[e11ty]) ? OPnp_fp : OPoffset,
e->Ety,e);
e->E1->Ety = e1->Ety;
}
#endif
}
again = 1;
return e;
}
/* Replace (e + e) with (e * 2) */
else if (el_match(e1,e2) && !el_sideeffect(e1) && !tyfloating(e->Ety))
{
e->Eoper = OPmul;
el_free(e2);
e->E2 = el_long(e->Ety,2);
again = 1;
return e;
}
// Replace ((e11 + c) + e2) with ((e11 + e2) + c)
if (e1->Eoper == OPadd && e1->E2->Eoper == OPconst &&
(e2->Eoper == OPvar || !OTleaf(e2->Eoper)) &&
tysize(e1->Ety) == tysize(e2->Ety) &&
tysize(e1->E2->Ety) == tysize(e2->Ety))
{
e->E2 = e1->E2;
e1->E2 = e2;
e1->Ety = e->Ety;
goto ret;
}
// Replace ((e11 - e12) + e2) with ((e11 + e2) - e12)
// (this should increase the number of LEA possibilities)
sz = tysize(e->Ety);
if (e1->Eoper == OPmin &&
tysize(e1->Ety) == sz &&
tysize(e2->Ety) == sz &&
tysize(e1->E1->Ety) == sz &&
tysize(e1->E2->Ety) == sz &&
!tyfloating(e->Ety)
)
{
e->Eoper = OPmin;
e->E2 = e1->E2;
e1->E2 = e2;
e1->Eoper = OPadd;
}
ret:
return e;
}
/************************
* Multiply (for OPmul && OPmulass)
* e * (c**2) => e << c ;replace multiply by power of 2 with shift
*/
STATIC elem * elmul(elem *e)
{
tym_t tym = e->Ety;
if (OPTIMIZER)
{
// Replace -a*-b with a*b.
// This is valid for all floating point types as well as integers.
if (tyarithmetic(tym) && e->E2->Eoper == OPneg && e->E1->Eoper == OPneg)
{
e->E1 = el_selecte1(e->E1);
e->E2 = el_selecte1(e->E2);
}
}
elem *e2 = e->E2;
if (e2->Eoper == OPconst) /* try to replace multiplies with shifts */
{
if (OPTIMIZER)
{
elem *e1 = e->E1;
unsigned op1 = e1->Eoper;
if (tyintegral(tym) && // skip floating types
OTbinary(op1) &&
e1->E2->Eoper == OPconst
)
{
/* Attempt to replace ((e + c1) * c2) with (e * c2 + (c1 * c2))
* because the + can be frequently folded out (merged into an
* array offset, for example.
*/
if (op1 == OPadd)
{
e->Eoper = OPadd;
e1->Eoper = OPmul;
e->E2 = el_bin(OPmul,tym,e1->E2,e2);
e1->E2 = el_copytree(e2);
again = 1;
return e;
}
// ((e << c1) * c2) => e * ((1 << c1) * c2)
if (op1 == OPshl)
{
e2->EV.Vullong *= (targ_ullong)1 << el_tolong(e1->E2);
e1->E2->EV.Vullong = 0;
again = 1;
return e;
}
}
if (elemisnegone(e2))
{
e->Eoper = (e->Eoper == OPmul) ? OPneg : OPnegass;
e->E2 = NULL;
el_free(e2);
return e;
}
}