-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathiliutil.cpp
15252 lines (14413 loc) · 386 KB
/
iliutil.cpp
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
/*
* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
* See https://llvm.org/LICENSE.txt for license information.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*
*/
/**
\file
\brief ILI utility module
*/
#include "pgifeat.h" // defines TARGET_64BIT et al.
#include "iliutil.h"
#include "exputil.h"
#include "expreg.h"
#include "regutil.h"
#include "machreg.h"
#define EXPANDER_DECLARE_INTERNAL
#include "expand.h"
#include "machar.h"
#include "outliner.h"
#include "assem.h"
#include "mach.h"
#include "dtypeutl.h"
#include "llutil.h"
#include "llassem.h"
#include "llmputil.h"
#include "expsmp.h"
#include "verify.h"
#if DEBUG
#include "nme.h"
#endif
#include <stdarg.h>
#include "scutil.h"
#include "symfun.h"
#if defined(OMP_OFFLOAD_PGI) || defined(OMP_OFFLOAD_LLVM)
#include "ompaccel.h"
#endif
/*
* MTH, FMTH, ... names
*/
#include "mth.h"
/** Union used to encode ATOMIC_INFO as an int or decode it. The union
contains a compacted version of ATOMIC_INFO using bitfields. The bitfields
are unsigned rather than the proper enum type so that the values aren't
sign extended when extracted. */
union ATOMIC_ENCODER {
struct {
unsigned msz : 8;
unsigned op : 8;
unsigned origin : 2;
unsigned scope : 1;
} info;
int encoding;
};
#define IL_spfunc IL_DFRSP
#define IL_dpfunc IL_DFRDP
#define IL_qpfunc IL_DFRQP
bool share_proc_ili = false;
bool share_qjsr_ili = false;
extern bool ishft;
static int addarth(ILI *);
static int red_iadd(int, INT);
static int red_kadd(int, INT[2]);
static int red_aadd(int, SPTR, ISZ_T, int);
static int red_damv(int, int, int);
static int red_minmax(ILI_OP, int, int);
static int red_negate(int, ILI_OP, int, int);
static int addbran(ILI *);
static int addother(ILI *);
static INT icmp(INT, INT);
static INT cmp_to_log(INT, int);
static bool isub_ovf(INT, INT, INT *);
static bool iadd_ovf(INT, INT, INT *);
static int get_ili(ILI *);
static int new_ili(ILI *);
static int ad1altili(ILI_OP, int, int);
static int ad2altili(ILI_OP, int, int, int);
static int ad3altili(ILI_OP, int, int, int, int);
static int ad2func_int(ILI_OP, const char *, int, int);
static int gen_sincos(ILI_OP, int, ILI_OP, ILI_OP, MTH_FN, DTYPE, ILI_OP);
#if defined(TARGET_X8664) || defined(TARGET_POWER)
static int _newton_fdiv(int, int);
#endif
static bool do_newton_sqrt(void);
static int _pwr2(INT, int);
static int _kpwr2(INT, INT, int);
static int _ipowi(int, int);
static int _xpowi(int, int, ILI_OP);
#if defined(TARGET_X8664) || defined(TARGET_POWER) || !defined(TARGET_LLVM_ARM)
static int _frsqrt(int);
#endif
static int _mkfunc(const char *);
static int DblIsSingle(SPTR dd);
static int _lshift_one(int);
static int cmpz_of_cmp(int, CC_RELATION);
static bool is_zero_one(int);
static bool _is_nanf(int);
static INT value_of_irlnk_operand(int ilix, int default_value);
#if defined(TARGET_WIN_X8664)
static void insert_argrsrv(ILI *);
#endif
// FIXME: mk_prototype_llvm should return an SPTR
#define mk_prototype (SPTR) mk_prototype_llvm
#define ILTABSZ 5
#define ILHSHSZ 173
#define MAXILIS 67108864
static int ilhsh[ILTABSZ][ILHSHSZ];
static bool safe_qjsr = false;
#define GARB_UNREACHABLE 0
#define GARB_VISITED 1
#ifdef __cplusplus
inline CC_RELATION CCRelationILIOpnd(ILI *p, int n) {
return static_cast<CC_RELATION>(p->opnd[n]);
}
inline DTYPE DTypeILIOpnd(ILI *p, int n) {
return static_cast<DTYPE>(p->opnd[n]);
}
inline MSZ ConvertMSZ(int n) {
return static_cast<MSZ>(n);
}
inline ATOMIC_RMW_OP ConvertATOMIC_RMW_OP(int n) {
return static_cast<ATOMIC_RMW_OP>(n);
}
inline SPTR sptrGetILI(ILI *p) {
return static_cast<SPTR>(get_ili(p));
}
#else
#define CCRelationILIOpnd(p,n) (p)->opnd[n]
#define DTypeILIOpnd(p,n) (p)->opnd[n]
#define ConvertMSZ(n) (n)
#define ConvertATOMIC_RMW_OP(n) (n)
#define sptrGetILI get_ili
#endif
/**
\brief initialize ili area
*/
void
ili_init(void)
{
int *p, cnt;
static int firstcall = 1;
STG_ALLOC(ilib, 2048);
STG_SET_FREELINK(ilib, ILI, hshlnk);
cnt = ILHSHSZ * ILTABSZ;
if (firstcall) {
firstcall = 0;
} else {
p = (int *)ilhsh;
do {
*p++ = 0;
} while (--cnt > 0);
}
/* reserve ili index 1 to be the NULL ili. done so that a traversal
* which uses the ILI_VISIT field as a thread can use an ili (#1) to
* terminate the threaded list
*/
ad1ili(IL_NULL, 0);
}
void
ili_cleanup(void)
{
STG_DELETE(ilib);
}
/**
\brief principle add ili routine
*/
int
addili(ILI *ilip)
{
ILI_OP opc; /* opcode of ili */
int ilix = 0; /* ili area index where ili was added */
int tmp; /* temporary */
int cons1;
INT numi[2];
opc = ilip->opc;
switch (IL_TYPE(opc)) {
case ILTY_ARTH:
ilix = addarth(ilip);
break;
case ILTY_CONS:
case ILTY_LOAD:
case ILTY_STORE:
case ILTY_DEFINE:
ilix = get_ili(ilip);
break;
case ILTY_PROC:
if (opc == IL_QJSR && share_qjsr_ili) {
/*
* normally (while expanding), we want qjsr's to be shared.
* in other cases (vectorizer), the qjsr's created may have
* side-effects (i.e., streamin) so we don't to share these
* (expand brackets its use with true, false).
*/
ilix = get_ili(ilip);
if (!safe_qjsr)
expb.qjsr_flag = true;
break;
}
if (share_proc_ili)
ilix = get_ili(ilip); /* share proc ili if option requested */
else
ilix = new_ili(ilip); /* o.w., ensure unique ili for proc */
if (opc == IL_QJSR) {
if (!safe_qjsr)
expb.qjsr_flag = true;
iltb.qjsrfg = true;
BIH_QJSR(expb.curbih) = 1;
}
break;
case ILTY_MOVE:
switch (opc) {
int op1, op2;
case IL_KIMV:
if (ILI_OPC(op1 = ilip->opnd[0]) == IL_KCON) {
ilix = ad_icon(CONVAL2G(ILI_OPND(op1, 1)));
} else if (ILI_OPC(ilip->opnd[0]) == IL_IKMV)
ilix = ILI_OPND(op1, 1);
else
ilix = get_ili(ilip);
break;
case IL_IKMV:
switch (ILI_OPC(op1 = ilip->opnd[0])) {
case IL_ICON:
ilix = ad_kconi(CONVAL2G(ILI_OPND(op1, 1)));
break;
case IL_IADD:
op2 = ILI_OPND(op1, 2);
if (ILI_OPC(op2) == IL_ICON) {
op2 = ad_kconi(CONVAL2G(ILI_OPND(op2, 1)));
op1 = ad1ili(IL_IKMV, ILI_OPND(op1, 1));
ilix = ad2ili(IL_KADD, op1, op2);
} else
ilix = get_ili(ilip);
break;
case IL_ISUB:
op2 = ILI_OPND(op1, 2);
op1 = ILI_OPND(op1, 1);
if (ILI_OPC(op2) == IL_ICON) {
op2 = ad_kconi(CONVAL2G(ILI_OPND(op2, 1)));
op1 = ad1ili(IL_IKMV, op1);
ilix = ad2ili(IL_KSUB, op1, op2);
} else if (ILI_OPC(op1) == IL_ICON) {
op1 = ad_kconi(CONVAL2G(ILI_OPND(op1, 1)));
op2 = ad1ili(IL_IKMV, op2);
ilix = ad2ili(IL_KSUB, op1, op2);
} else
ilix = get_ili(ilip);
break;
default:
ilix = get_ili(ilip);
}
break;
case IL_UIKMV:
if (ILI_OPC(op1 = ilip->opnd[0]) == IL_ICON) {
numi[0] = CONVAL1G(op1 = ILI_OPND(op1, 1));
numi[1] = CONVAL2G(op1);
ilix = ad1ili(IL_KCON, getcon(numi, DT_INT8));
} else
ilix = get_ili(ilip);
break;
case IL_MVKR:
case IL_MVIR:
case IL_MVSP:
#ifdef IL_MVSPSP
case IL_MVSPSP:
#endif
case IL_MVQ: /*m128*/
case IL_MV256: /*m256*/
case IL_MVDP:
case IL_MVAR:
#ifdef LONG_DOUBLE_FLOAT128
case IL_FLOAT128RETURN:
#endif
if (ilip->opnd[1] == -1)
ilix = ilip->opnd[0];
else
ilix = get_ili(ilip);
break;
#ifdef IL_MVSPX87
case IL_MVSPX87:
case IL_MVDPX87:
ilix = get_ili(ilip);
break;
#endif
case IL_IAMV:
if (ILI_OPC(tmp = ilip->opnd[0]) == IL_ICON)
ilix = ad_aconi(CONVAL2G(ILI_OPND(tmp, 1)));
else if (ILI_OPC(tmp) == IL_AIMV)
ilix = ILI_OPND(tmp, 1);
else
ilix = get_ili(ilip);
break;
case IL_KAMV:
if (ILI_OPC(tmp = ilip->opnd[0]) == IL_KCON) {
cons1 = ILI_OPND(tmp, 1);
ilix = ad_aconk(CONVAL1G(cons1), ACONOFFG(cons1));
return ilix;
}
tmp = ilip->opnd[0];
if (ILI_OPC(tmp) == IL_AKMV)
ilix = ILI_OPND(tmp, 1);
else
ilix = get_ili(ilip);
break;
case IL_AIMV:
if ((ILI_OPC(tmp = ilip->opnd[0]) == IL_ACON) &&
(CONVAL1G((tmp = ILI_OPND(tmp, 1))) == 0)) {
ilix = ad_icon(CONVAL2G(tmp));
} else if (ILI_OPC(tmp = ilip->opnd[0]) == IL_IAMV)
ilix = ILI_OPND(tmp, 1);
else
ilix = get_ili(ilip);
break;
case IL_AKMV:
if ((ILI_OPC(tmp = ilip->opnd[0]) == IL_ACON) &&
(CONVAL1G((tmp = ILI_OPND(tmp, 1))) == 0)) {
ISZ_2_INT64(ACONOFFG(tmp), numi);
ilix = ad1ili(IL_KCON, getcon(numi, DT_INT8));
} else if (ILI_OPC(tmp = ilip->opnd[0]) == IL_KAMV)
ilix = ILI_OPND(tmp, 1);
else
ilix = get_ili(ilip);
break;
case IL_RETURN:
ilix = get_ili(ilip);
break;
default:
assert(false, "addili: unrec move opcode:", opc, ERR_Severe);
}
break;
case ILTY_BRANCH:
ilix = addbran(ilip);
break;
case ILTY_OTHER:
#ifdef ILTY_PSTORE
case ILTY_PSTORE:
#endif
#ifdef ILTY_PLOAD
case ILTY_PLOAD:
#endif
ilix = addother(ilip);
break;
#if DEBUG
default:
interr("addili: illegal IL_TYPE(opc)", IL_TYPE(opc), ERR_Fatal);
break;
#endif
}
#if DEBUG
if (DBGBIT(10, 32)) {
if (ilix)
dump_ili(gbl.dbgfil, ilix);
}
if (ilix)
verify_ili(ilix, VERIFY_ILI_SHALLOW);
#endif
return ilix;
}
/**
\brief add ili with one operand
*/
int
ad1ili(ILI_OP opc, int opn1)
{
ILI newili;
newili.opc = opc;
newili.opnd[0] = opn1;
newili.opnd[1] = 0; /* cause some FREE ili have two opnds (target dep)*/
newili.opnd[2] = 0;
newili.opnd[3] = 0;
newili.opnd[4] = 0;
return addili(&newili);
}
/** \brief add an ili with one operand which has an alternate
*
* The routine is static -- only addili should create ALT ili.
*/
static int
ad1altili(ILI_OP opc, int opn1, int alt)
{
ILI newili;
int ilix;
newili.opc = opc;
newili.opnd[0] = opn1;
newili.opnd[1] = 0; /* cause some FREE ili have two opnds (target dep)*/
newili.opnd[2] = 0;
newili.opnd[3] = 0;
newili.opnd[4] = 0;
ilix = get_ili(&newili);
if (ILI_ALT(ilix) == 0)
ILI_ALT(ilix) = alt;
return ilix;
}
/**
\brief add ili with two operands
*/
int
ad2ili(ILI_OP opc, int opn1, int opn2)
{
ILI newili;
newili.opc = opc;
newili.opnd[0] = opn1;
newili.opnd[1] = opn2;
newili.opnd[2] = 0;
newili.opnd[3] = 0;
newili.opnd[4] = 0;
return addili(&newili);
}
/** \brief Add an ili with two operands which has an alternate
*
* The routine isstatic -- only addili should create ALT ili.
*/
static int
ad2altili(ILI_OP opc, int opn1, int opn2, int alt)
{
ILI newili;
int ilix;
newili.opc = opc;
newili.opnd[0] = opn1;
newili.opnd[1] = opn2;
newili.opnd[2] = 0;
newili.opnd[3] = 0;
newili.opnd[4] = 0;
ilix = get_ili(&newili);
if (ILI_ALT(ilix) == 0)
ILI_ALT(ilix) = alt;
return ilix;
}
/** \brief Add func call with 2 integer arguments returning integer value
*
* \param opc must be a function call ili opcode: QJSR, JSR
*/
static int
ad2func_int(ILI_OP opc, const char *name, int opn1, int opn2)
{
int tmp, tmp1, tmp2;
tmp1 = ad1ili(IL_NULL, 0);
tmp1 = ad2ili(IL_ARGIR, opn2, tmp1);
tmp2 = ad2ili(IL_ARGIR, opn1, tmp1);
tmp = ad2ili(opc, _mkfunc(name), tmp2);
return ad2ili(IL_DFRIR, tmp, IR_RETVAL);
}
/** \brief Add func call with 1 complex argument returning complex value
*
* Note that a double complex value will be presented as a packed argument,
* i.e., a double complex vector of length 1
*
* \param opc must be a function call ili opcode: QJSR, JSR
*/
static int
ad1func_cmplx(ILI_OP opc, char *name, int opn1)
{
int tmp, tmp1, tmp2;
tmp1 = ad1ili(IL_NULL, 0);
if (IL_RES(ILI_OPC(opn1)) == ILIA_CS) {
tmp2 = ad3ili(IL_DACS, opn1, DP(0), tmp1);
tmp = ad2ili(opc, _mkfunc(name), tmp2);
return ad2ili(IL_DFRCS, tmp, CS_RETVAL);
}
tmp2 = ad3ili(IL_DACD, opn1, DP(0), tmp1);
tmp = ad2ili(opc, _mkfunc(name), tmp2);
return ad2ili(IL_DFRCD, tmp, CD_RETVAL);
}
/** \brief Add func call with 2 arguments returning complex value
*
* The 2 arguments could be double complex or the 1st argument is double
* complex and the 2nd argument is integer.
*
* A double complex value will be presented as a packed argument, i.e.,
* a double complex vector of length 1
*
* \param opc must be a function call ili opcode: QJSR, JSR
*/
static int
ad2func_cmplx(ILI_OP opc, char *name, int opn1, int opn2)
{
int tmp, tmp1, tmp2;
int ireg; /* for integer pow argument just in case */
#if !defined(TARGET_WIN)
ireg = IR(0);
#else
ireg = IR(1); /* positional on windows */
#endif
tmp1 = ad1ili(IL_NULL, 0);
switch (IL_RES(ILI_OPC(opn2))) {
case ILIA_CS:
tmp1 = ad3ili(IL_DACS, opn2, DP(1), tmp1);
break;
case ILIA_CD:
tmp1 = ad3ili(IL_DACD, opn2, DP(1), tmp1);
break;
case ILIA_IR:
#if defined(TARGET_X8664)
tmp1 = ad3ili(IL_DAIR, opn2, ireg, tmp1);
#else
tmp1 = ad3ili(IL_ARGIR, opn2, tmp1, 0);
#endif
break;
case ILIA_KR:
#if defined(TARGET_X8664)
tmp1 = ad3ili(IL_DAKR, opn2, ireg, tmp1);
#else
tmp1 = ad3ili(IL_ARGKR, opn2, tmp1, 0);
#endif
break;
default:
interr("ad2func_cmplx: illegal ILIA arg2", opn2, ERR_unused);
tmp1 = ad1ili(IL_NULL, 0);
}
if (IL_RES(ILI_OPC(opn1)) == ILIA_CS) {
tmp2 = ad3ili(IL_DACS, opn1, DP(0), tmp1);
tmp = ad2ili(opc, _mkfunc(name), tmp2);
return ad2ili(IL_DFRCS, tmp, CS_RETVAL);
}
tmp2 = ad3ili(IL_DACD, opn1, DP(0), tmp1);
tmp = ad2ili(opc, _mkfunc(name), tmp2);
return ad2ili(IL_DFRCD, tmp, CD_RETVAL);
}
/** \brief Add func call with 1 complex argument returning complex value
*
* The C ABI is used here. A complex will be treated as if it's a struct
* 2 parts. On x64, a complex double will be passed as 2 arguments; the
* vector abi will pass the complex double packed in a single register or
* memory unit.
*
* \param opc must be a function call ili opcode: QJSR, JSR
*/
static int
ad1func_cmplx_abi(ILI_OP opc, char *name, int opn1)
{
int tmp, tmp1, tmp2;
tmp1 = ad1ili(IL_NULL, 0);
if (IL_RES(ILI_OPC(opn1)) == ILIA_CS) {
tmp2 = ad3ili(IL_DACS, opn1, DP(0), tmp1);
tmp = ad2ili(opc, _mkfunc(name), tmp2);
return ad2ili(IL_DFRCS, tmp, CS_RETVAL);
}
tmp2 = ad3ili(IL_DACD, opn1, DP(0), tmp1);
tmp = ad2ili(opc, _mkfunc(name), tmp2);
return ad2ili(IL_DFRCD, tmp, CD_RETVAL);
}
/** \brief Add func call with 2 arguments returning complex value
*
* The 2 arguments could be double complex or the 1st argument is double
* complex and the 2nd argument is integer.
* The C ABI is used here. A complex will be treated as if it's a struct
* 2 parts. On x64, a complex double will be passed as 2 arguments; the
* vector abi will pass the complex double packed in a single register or
* memory unit.
*
* \param opc must be a function call ili opcode: QJSR, JSR
*/
static int
ad2func_cmplx_abi(ILI_OP opc, char *name, int opn1, int opn2)
{
int tmp, tmp1, tmp2;
int ireg; /* for integer pow argument just in case */
#if !defined(TARGET_WIN)
ireg = IR(0);
#else
ireg = IR(1); /* positional on windows */
#endif
tmp1 = ad1ili(IL_NULL, 0);
switch (IL_RES(ILI_OPC(opn2))) {
case ILIA_CS:
tmp1 = ad3ili(IL_DACS, opn2, DP(1), tmp1);
break;
case ILIA_CD:
tmp1 = ad3ili(IL_DACD, opn2, DP(1), tmp1);
break;
case ILIA_IR:
#if defined(TARGET_X8664)
tmp1 = ad3ili(IL_DAIR, opn2, ireg, tmp1);
#else
tmp1 = ad3ili(IL_ARGIR, opn2, tmp1, 0);
#endif
break;
case ILIA_KR:
#if defined(TARGET_X8664)
tmp1 = ad3ili(IL_DAKR, opn2, ireg, tmp1);
#else
tmp1 = ad3ili(IL_ARGKR, opn2, tmp1, 0);
#endif
break;
default:
interr("ad2func_cmplx: illegal ILIA arg2", opn2, ERR_unused);
tmp1 = ad1ili(IL_NULL, 0);
}
if (IL_RES(ILI_OPC(opn1)) == ILIA_CS) {
tmp2 = ad3ili(IL_DACS, opn1, DP(0), tmp1);
tmp = ad2ili(opc, _mkfunc(name), tmp2);
return ad2ili(IL_DFRCS, tmp, CS_RETVAL);
}
tmp2 = ad3ili(IL_DACD, opn1, DP(0), tmp1);
tmp = ad2ili(opc, _mkfunc(name), tmp2);
return ad2ili(IL_DFRCD, tmp, CD_RETVAL);
}
/** \brief Add func call with 1 complex argument returning complex value
*
* Assumes the new (as defined by make_math) naming scheme. For passing
* complex and returning types, we can either follow the C ABI which says
* complex is the same as a struct of two parts; or, we can follow the vector
* ABI which views a complex scalar as a vector complex vector of length 1,
* i.e., the complex is 'packed'. For now, we use the C abi'.
*
* \param opc must be a function call ili opcode: QJSR, JSR
*/
static int
ad1mathfunc_cmplx(MTH_FN fn, ILI_OP opc, int op1, DTYPE res_dt, DTYPE arg1_dt)
{
char *fname;
int ilix;
fname = make_math(fn, NULL, 1, false, res_dt, 1, arg1_dt);
if (!XBIT_VECTORABI_FOR_SCALAR)
ilix = ad1func_cmplx_abi(IL_QJSR, fname, op1);
else
ilix = ad1func_cmplx(IL_QJSR, fname, op1);
ilix = ad1altili(opc, op1, ilix);
return ilix;
}
/** \brief Add func call with 2 complex arguments returning complex value
*
* Assumes the new (as defined by make_math) naming scheme. For passing
* complex and returning types, we can either follow the C ABI which says
* complex is the same as a struct of two parts; or, we can follow the vector
* ABI which views a complex scalar as a vector complex vector of length 1,
* i.e., the complex is 'packed'. For now, we use the 'vector abi'.
*
* \param opc must be a function call ili opcode: QJSR, JSR
*/
static int
ad2mathfunc_cmplx(MTH_FN fn, ILI_OP opc, int op1, int op2, DTYPE res_dt,
DTYPE arg1_dt, DTYPE arg2_dt)
{
char *fname;
int ilix;
fname = make_math(fn, NULL, 1, false, res_dt, 2, arg1_dt, arg2_dt);
if (!XBIT_VECTORABI_FOR_SCALAR)
ilix = ad2func_cmplx_abi(IL_QJSR, fname, op1, op2);
else
ilix = ad2func_cmplx(IL_QJSR, fname, op1, op2);
ilix = ad2altili(opc, op1, op2, ilix);
return ilix;
}
/*
* WARNING - the arguments to ad_func are in lexical order
*/
static int
ad_func(ILI_OP result_opc, ILI_OP call_opc, const char *func_name, int nargs, ...)
{
va_list vargs;
int rg;
int irg;
int frg;
int func;
int argl;
int ilix;
int i;
struct {
ILI_OP opc;
int arg;
int reg;
int is_argili;
} args[6];
va_start(vargs, nargs);
#if DEBUG
assert((size_t)nargs <= sizeof(args) / sizeof(args[0]),
"iliutil.c:ad_func, increase the size of args[]",
sizeof(args) / sizeof(args[0]), ERR_unused);
#endif
rg = 0;
irg = 0;
frg = 0;
argl = ad1ili(IL_NULL, 0);
BZERO(args, char, sizeof(args));
for (i = 0; i < nargs; i++) {
args[i].arg = va_arg(vargs, int);
if (IL_VECT(ILI_OPC(args[i].arg))) {
args[i].opc = IL_GARG;
args[i].is_argili = 1;
} else
switch (IL_RES(ILI_OPC(args[i].arg))) {
case ILIA_AR:
#if defined(TARGET_X8664)
args[i].opc = IL_DAAR;
args[i].reg = ARG_IR(irg);
#else
args[i].opc = IL_ARGAR;
args[i].is_argili = 1;
#endif
rg++;
irg++;
break;
case ILIA_IR:
#if defined(TARGET_X8664)
args[i].opc = IL_DAIR;
args[i].reg = IR(irg);
#else
args[i].opc = IL_ARGIR;
args[i].is_argili = 1;
#endif
rg++;
irg++;
break;
case ILIA_SP:
args[i].opc = IL_DASP;
args[i].reg = SP(frg);
rg++;
frg++;
break;
case ILIA_DP:
args[i].opc = IL_DADP;
args[i].reg = DP(frg);
rg++;
frg++;
break;
#ifdef TARGET_SUPPORTS_QUADFP
case ILIA_QP:
args[i].opc = IL_DAQP;
args[i].reg = QP(frg);
rg++;
frg++;
break;
#endif
case ILIA_CS:
args[i].opc = IL_DACS;
args[i].reg = DP(frg);
rg++;
frg++;
break;
case ILIA_CD:
args[i].opc = IL_DACD; /* assumed to be packed when passed */
args[i].reg = DP(frg);
rg++;
frg++;
break;
case ILIA_KR:
#if defined(TARGET_X8664)
args[i].opc = IL_DAKR;
args[i].reg = IR(irg);
rg++;
irg++;
#else
args[i].opc = IL_ARGKR;
args[i].is_argili = 1;
rg += 2;
#endif
break;
#ifdef LONG_DOUBLE_FLOAT128
case ILIA_FLOAT128:
args[i].opc = IL_FLOAT128ARG;
args[i].is_argili = 1;
rg++;
break;
#endif
default:
interr("ad_func: illegal arg", args[i].arg, ERR_Severe);
args[i].opc = IL_ARGIR;
args[i].is_argili = 1;
break;
}
#if defined(TARGET_WIN_X8664)
irg = rg; /* on win64, register # is positional */
frg = rg; /* on win64, register # is positional */
#endif
}
for (i = nargs - 1; i >= 0; i--) {
if (!args[i].is_argili) {
argl = ad3ili(args[i].opc, args[i].arg, args[i].reg, argl);
} else {
if (IL_VECT(ILI_OPC(args[i].arg))) {
int arg_dtype = 0, dtype_slot, arg_nme = 0;
switch (IL_TYPE(ILI_OPC(args[i].arg))) {
case ILTY_CONS:
arg_nme = 0;
arg_dtype = DTYPEG(ILI_OPND(args[i].arg, 1));
break;
case ILTY_LOAD:
arg_nme = ILI_OPND(args[i].arg, 2);
arg_dtype = ILI_OPND(args[i].arg, 3);
break;
case ILTY_ARTH:
case ILTY_OTHER:
arg_nme = 0;
dtype_slot = IL_OPRS(ILI_OPC(args[i].arg));
arg_dtype = ILI_OPND(args[i].arg, dtype_slot);
break;
default:
assert(false, "ad_func(): unhandled vect ILI type",
IL_TYPE(ILI_OPC(args[i].arg)), ERR_Fatal);
}
argl = ad4ili(args[i].opc, args[i].arg, argl, arg_dtype, arg_nme);
} else
/* the 3rd argument is for dttype for an IL_ARGAR */
argl = ad3ili(args[i].opc, args[i].arg, argl, 0);
}
}
func = _mkfunc(func_name);
ilix = call_opc == IL_GJSR ? ad3ili(IL_GJSR, func, argl, 0)
: ad2ili(call_opc, func, argl);
switch (result_opc) {
case IL_NONE:
break; /* no return value */
case IL_DFRAR:
ilix = ad2ili(result_opc, ilix, AR_RETVAL);
break;
case IL_DFRIR:
ilix = ad2ili(result_opc, ilix, IR_RETVAL);
break;
case IL_DFRKR:
ilix = ad2ili(result_opc, ilix, KR_RETVAL);
break;
case IL_DFRSP:
ilix = ad2ili(result_opc, ilix, SP_RETVAL);
break;
case IL_DFRDP:
ilix = ad2ili(result_opc, ilix, DP_RETVAL);
break;
#ifdef TARGET_SUPPORTS_QUADFP
case IL_DFRQP:
ilix = ad2ili(result_opc, ilix, QP_RETVAL);
break;
#endif
case IL_DFRCS:
ilix = ad2ili(result_opc, ilix, CS_RETVAL);
break;
#ifdef IL_DFRSPX87
case IL_DFRSPX87:
ilix = ad1ili(result_opc, ilix);
break;
#endif
#ifdef IL_DFRDPX87
case IL_DFRDPX87:
ilix = ad1ili(result_opc, ilix);
break;
#endif
default:
interr("ad_func: illegal result_opc", result_opc, ERR_Severe);
}
va_end(vargs);
return ilix;
}
#if defined(TARGET_X8664)
static char *
fmth_name(const char *root)
{
static char bf[64];
const char *suf;
suf = "";
if (TEST_MACH(MACH_AMD_GH)) {
suf = "_gh";
}
sprintf(bf, "%s%s", root, suf);
return bf;
}
#endif
/*
* fast math naming convention:
* __g[sv][sdcz]_BASE[L]
* [sv] - scalar vector
* [sdcz] - single/double/singlecomplex/doublecomplex
* [L] - vector length, i.e., 2, 4, 8, 16
*/
char *
gnr_math(const char *root, int widthc, int typec, const char *oldname, int masked)
{
static char bf[32];
/*
* widthc - 's' (scalar), 'v' (vector)
* N (vector length` N) (2, 4, 8, 16, ...)
* typec - 's' (single), 'd' (double),
* 'c'( single complex), 'z' (double complex)
* oldname - old 'mth' name (only if scalar)
*/
#if defined(TARGET_OSX) || defined(TARGET_WIN)
sprintf(bf, "%s", oldname);
#else
if (widthc == 's' || widthc == 'v')
sprintf(bf, "__g%c%c_%s", widthc, typec, root);
else {
const char *mk;
mk = !masked ? "" : "_mask";
sprintf(bf, "__g%c%c_%s%d%s", 'v', typec, root, widthc, mk);
}
#endif
return bf;
}
static char *
vect_math(MTH_FN fn, const char *root, int nargs, DTYPE vdt, int vopc, int vdt1,
int vdt2, bool mask)
{
int typec = 0;
int num_elem;
DTYPE vdt_mask = DT_NONE;
int func;
char *func_name;
char oldname[32];
/*
* determine type/precision (single,double, ...) and
* construct the names for the lowest common denominator architecture
* on x86 - MACH_AMD_K8 or MACH_INTEL without SSE3
*/
if (DTY(vdt) != TY_VECT) {
interr("vect_math: dtype is not vector", vdt, ERR_Severe);
vdt = get_vector_dtype(DT_DBLE, 2);
}
if (XBIT_NEW_MATH_NAMES && fn != MTH_mod) {
/*
* DTY(vdt+1) -- res_dt
* DTY(vdt+2) -- vect_len
*/
func_name =
make_math_name(fn, DTyVecLength(vdt), mask, DTySeqTyElement(vdt));
} else {
switch (DTY(DTySeqTyElement(vdt))) {
case TY_FLOAT:
typec = 's';
sprintf(oldname, "__fvs_%s", root);
break;
case TY_DBLE:
typec = 'd';
sprintf(oldname, "__fvd_%s", root);
break;
default:
interr("vect_math: unexpected element dtype", DTySeqTyElement(vdt),
ERR_Severe);
typec = 'd';
break;
}
#if defined(TARGET_LINUX_X8664)
if (XBIT_NEW_RELAXEDMATH) {
switch (vopc) {
case IL_VEXP:
func_name = relaxed_math(root, 'v', typec, oldname);
goto llvm_hk;
case IL_VTAN:
case IL_VPOW: