/
func.c
7429 lines (7018 loc) · 215 KB
/
func.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) 1994-2018, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/**
\file
\brief rewrite function args, etc
*/
#include "gbldefs.h"
#include "global.h"
#include "error.h"
#include "comm.h"
#include "symtab.h"
#include "symutl.h"
#include "dtypeutl.h"
#include "soc.h"
#include "semant.h"
#include "ast.h"
#include "transfrm.h"
#include "gramtk.h"
#include "extern.h"
#include "hpfutl.h"
#include "ccffinfo.h"
#include "dinit.h"
#include "rte.h"
#include "direct.h"
#ifdef TARGET_X86
#include "x86.h"
#endif
#include "rtlRtns.h"
static LOGICAL matmul_use_lhs(int, int, int);
static int triplet_extent(int);
static int misalignment(int, int, int);
static LOGICAL is_another_shift(int, int, int, int);
static LOGICAL _is_another_shift(int, LOGICAL *);
static int transform_associated(int, int);
static void transform_mvbits(int, int);
static void transform_merge(int, int);
static void transform_elemental(int, int);
static void transform_c_f_pointer(int, int);
static void transform_c_f_procpointer(int, int);
static void transform_move_alloc(int, int);
static void check_arg_isalloc(int);
static int rewrite_func_ast(int, int, int);
static int rewrite_intr_allocatable(int, int, int);
static LOGICAL ast_has_allocatable_member(int);
static int rewrite_sub_ast(int, int);
static int mk_result_sptr(int, int, int *, int, int, int *);
static LOGICAL take_out_user_def_func(int);
static int matmul(int, int, int);
static int mmul(int, int, int); /* fast matmul */
static int reshape(int, int, int);
static int _reshape(int, DTYPE, int);
static int inline_reduction_f90(int ast, int dest, int lc, LOGICAL *doremove);
static int inline_reduction_craft(int, int, int);
static void move_alloc_type(int, int, int);
static void nop_dealloc(int, int);
static void handle_shift(int s);
/*------ Argument & Expression Rewriting ----------*/
int
gen_islocal_index(int ast, int sptr, int dim, int subAst)
{
int nargs, argt;
int newast;
int align;
int descr;
int olb, oub;
int tmp1, tmp2;
align = ALIGNG(sptr);
descr = DESCRG(sptr);
DESCUSEDP(sptr, TRUE);
if (!XBIT(47, 0x80) && align) {
/* inline it; if (idx.ge.sd$desc(olb).and.idx.le.sd$descr(oub)) then */
olb = check_member(ast, get_owner_lower(descr, dim));
oub = check_member(ast, get_owner_upper(descr, dim));
if (normalize_bounds(sptr)) {
olb = add_lbnd(DTYPEG(sptr), dim, olb, ast);
oub = add_lbnd(DTYPEG(sptr), dim, oub, ast);
}
tmp1 = mk_binop(OP_GE, subAst, olb, DT_LOG);
tmp2 = mk_binop(OP_LE, subAst, oub, DT_LOG);
newast = mk_binop(OP_LAND, tmp1, tmp2, DT_LOG);
return newast;
}
nargs = 3;
argt = mk_argt(nargs);
ARGT_ARG(argt, 0) = check_member(ast, mk_id(descr));
ARGT_ARG(argt, 1) = mk_cval(dim + 1, astb.bnd.dtype);
newast = mk_default_int(subAst);
if (normalize_bounds(sptr))
newast = sub_lbnd(DTYPEG(sptr), dim, newast, ast);
ARGT_ARG(argt, 2) = newast;
newast = mk_func_node(A_FUNC,
mk_id(sym_mkfunc(mkRteRtnNm(RTE_islocal_idx), DT_LOG)),
nargs, argt);
NODESCP(A_SPTRG(A_LOPG(newast)), 1);
A_DTYPEP(newast, DT_LOG);
return newast;
} /* gen_islocal_index */
static int
gen_scalar_mask(int ast, int list)
{
return 0;
} /* gen_scalar_mask */
/*
* SUM and PRODUCT reductions use a longer datatype for
* the reduction temporary; for instance, they use
* REAL*8 for a REAL*4 SUM call
*/
static int
reduction_type(DTYPE dtype)
{
switch (DTY(dtype)) {
case TY_BINT:
case TY_SINT:
case TY_INT:
return DT_INT;
case TY_INT8:
return dtype;
case TY_REAL:
return DT_REAL8;
case TY_DBLE:
if (XBIT(57, 0x14) || XBIT(51, 0x80)) {
/* no real*16, or map real*16 to real*8,
* or don't use quad precision accumulators */
return dtype;
} else {
return DT_QUAD;
}
case TY_QUAD:
return dtype;
case TY_CMPLX:
return DT_CMPLX16;
case TY_DCMPLX:
if (XBIT(57, 0x18) || XBIT(51, 0x80)) {
/* no complex*32, or map complex*32 to complex*16,
* or don't use quad precision accumulators */
return dtype;
} else {
return DT_QCMPLX;
}
case TY_QCMPLX:
return dtype;
default:
return dtype;
}
} /* reduction_type */
static int
assign_result(int sptr, int ast, DTYPE dtype, DTYPE dtyperes, int stdnext,
int lineno)
{
int tsclr, tsclrAst, asn, std;
if (dtyperes == dtype)
return ast;
/* we had a SUM or PRODUCT where we used a REAL*8 temp for a REAL*4
* reduction, for instance. Now, coerce back to REAL*4 */
tsclr = sym_get_scalar(SYMNAME(sptr), "rr", dtyperes);
tsclrAst = mk_id(tsclr);
asn = mk_assn_stmt(tsclrAst, ast, dtyperes);
std = add_stmt_before(asn, stdnext);
STD_LINENO(std) = lineno;
STD_LOCAL(std) = 1;
STD_PAR(std) = STD_PAR(stdnext);
STD_TASK(std) = STD_TASK(stdnext);
STD_ACCEL(std) = STD_ACCEL(stdnext);
STD_KERNEL(std) = STD_KERNEL(stdnext);
return tsclrAst;
} /* assign_result */
/* this will check whether cshift or eoshift needs any communication. */
static LOGICAL
is_no_comm_shift(int func_ast, int func_args)
{
return TRUE;
}
/*
* generate inline loops for CSHIFT and EOSHIFT
*/
#define SHIFTMAX 7
/* shift structure */
static struct {
int shift; /* shift distance */
int dim, cdim; /* which dimension being shifted */
int boundary; /* for EOSHIFT, boundary value */
int shifttype; /* CSHIFT or EOSHIFT */
int dim_dest, dim_src; /* which dimensions get shifted */
int n, m, k; /* extent, positive shift amount */
int nc, mc, kc; /* constant value of above */
LOGICAL lt; /* less than */
LOGICAL then_part, else_part; /* nonzero shift, zero shift */
} ss[SHIFTMAX]; /* shift data */
static struct {
int shiftcount; /* how many nested shifts */
int subssrc[MAXSUBS], subsdest[MAXSUBS];
int src, dest;
int ndimsrc, ndimdest;
} sg; /* shift global data */
static void
recurse_shift(int s)
{
if (s < sg.shiftcount) {
handle_shift(s);
} else {
int ast_lhs, ast_rhs, ast;
ast_lhs =
mk_subscr(A_LOPG(sg.dest), sg.subsdest, sg.ndimdest, A_DTYPEG(sg.dest));
ast_rhs =
mk_subscr(A_LOPG(sg.src), sg.subssrc, sg.ndimsrc, A_DTYPEG(sg.src));
ast = mk_assn_stmt(ast_lhs, ast_rhs, DTY(A_DTYPEG(A_LOPG(sg.dest)) + 1));
add_stmt_before(ast, arg_gbl.std);
}
} /* recurse_shift */
static void
recurse_eoshift(int s)
{
if (s < sg.shiftcount) {
handle_shift(s);
} else {
int ast_lhs, ast_rhs, ast;
ast_lhs =
mk_subscr(A_LOPG(sg.dest), sg.subsdest, sg.ndimdest, A_DTYPEG(sg.dest));
ast_rhs = ss[s - 1].boundary;
ast = mk_assn_stmt(ast_lhs, ast_rhs, DTY(A_DTYPEG(A_LOPG(sg.dest)) + 1));
add_stmt_before(ast, arg_gbl.std);
}
} /* recurse_eoshift */
static void
handle_shift(int s)
{
if (A_TYPEG(ss[s].m) != A_CNST) {
int ast, expr;
ast = mk_stmt(A_IFTHEN, 0);
expr = mk_binop(OP_NE, ss[s].m, astb.bnd.zero, DT_LOG);
A_IFEXPRP(ast, expr);
add_stmt_before(ast, arg_gbl.std);
}
if (ss[s].then_part) {
int ta, la, ua, xa, lla, uua, sa;
int tb, lb, ub, xb, llb, uub, sb;
int tmp1, tmp2;
ta = sg.subsdest[ss[s].dim_dest];
la = A_LBDG(ta);
ua = A_UPBDG(ta);
sa = A_STRIDEG(ta);
xa = triplet_extent(ta);
tb = sg.subssrc[ss[s].dim_src];
lb = A_LBDG(tb);
ub = A_UPBDG(tb);
sb = A_STRIDEG(tb);
xb = triplet_extent(tb);
if (ss[s].shifttype == I_CSHIFT) {
/* a(la : ua - m*sa : sa) = b(lb + m*sb : ub : sb) */
tmp1 = opt_binop(OP_MUL, ss[s].m, sa, astb.bnd.dtype);
uua = opt_binop(OP_SUB, ua, tmp1, astb.bnd.dtype);
sg.subsdest[ss[s].dim_dest] = mk_triple(la, uua, sa);
tmp1 = opt_binop(OP_MUL, ss[s].m, sb, astb.bnd.dtype);
llb = opt_binop(OP_ADD, lb, tmp1, astb.bnd.dtype);
sg.subssrc[ss[s].dim_src] = mk_triple(llb, ub, sb);
recurse_shift(s + 1);
/* a(la + (n - m)*sa : ua : sa) = b(lb : ub - (n - m)*sb : sb) */
tmp1 = opt_binop(OP_SUB, xa, ss[s].m, astb.bnd.dtype);
tmp2 = opt_binop(OP_MUL, tmp1, sa, astb.bnd.dtype);
lla = opt_binop(OP_ADD, la, tmp2, astb.bnd.dtype);
sg.subsdest[ss[s].dim_dest] = mk_triple(lla, ua, sa);
tmp1 = opt_binop(OP_SUB, xb, ss[s].m, astb.bnd.dtype);
tmp2 = opt_binop(OP_MUL, tmp1, sb, astb.bnd.dtype);
uub = opt_binop(OP_SUB, ub, tmp2, astb.bnd.dtype);
sg.subssrc[ss[s].dim_src] = mk_triple(lb, uub, sb);
recurse_shift(s + 1);
} else if (ss[s].shifttype == I_EOSHIFT) {
int ast_lhs, ast_rhs, ast, x;
/* handle case with m > 0 */
x = 0;
if (A_TYPEG(ss[s].m) == A_CNST) {
if (ss[s].mc > 0) {
x = 1;
}
} else {
int ast, expr;
x = 1;
/* test whether the shift distance is < 0 or > 0 */
ast = mk_stmt(A_IFTHEN, 0);
expr = mk_binop(OP_GT, ss[s].m, astb.bnd.zero, DT_LOG);
A_IFEXPRP(ast, expr);
add_stmt_before(ast, arg_gbl.std);
}
if (x) {
/* a(la : ua - m*sa : sa) = b(lb + m*sb : ub : sb) */
tmp1 = opt_binop(OP_MUL, ss[s].m, sa, astb.bnd.dtype);
uua = opt_binop(OP_SUB, ua, tmp1, astb.bnd.dtype);
sg.subsdest[ss[s].dim_dest] = mk_triple(la, uua, sa);
tmp1 = opt_binop(OP_MUL, ss[s].m, sb, astb.bnd.dtype);
llb = opt_binop(OP_ADD, lb, tmp1, astb.bnd.dtype);
sg.subssrc[ss[s].dim_src] = mk_triple(llb, ub, sb);
recurse_shift(s + 1);
/* a(la + (n - m)*sa : ua : sa) = boundary */
tmp1 = opt_binop(OP_SUB, xa, ss[s].m, astb.bnd.dtype);
tmp2 = opt_binop(OP_MUL, tmp1, sa, astb.bnd.dtype);
lla = opt_binop(OP_ADD, la, tmp2, astb.bnd.dtype);
sg.subsdest[ss[s].dim_dest] = mk_triple(lla, ua, sa);
ast_lhs = mk_subscr(A_LOPG(sg.dest), sg.subsdest, sg.ndimdest,
A_DTYPEG(sg.dest));
ast_rhs = ss[s].boundary; /* boundary have to be spread if array */
if (A_SHAPEG(ast_rhs)) {
/* add spread call */
int newargt, spread;
newargt = mk_argt(3);
ARGT_ARG(newargt, 0) = ast_rhs;
ARGT_ARG(newargt, 1) = mk_cval(ss[s].dim_dest + 1, astb.bnd.dtype);
tmp2 = opt_binop(OP_SUB, ua, lla, astb.bnd.dtype);
if (sa != astb.i1 && sa != astb.bnd.one) {
tmp2 = opt_binop(OP_DIV, tmp2, sa, astb.bnd.dtype);
}
ARGT_ARG(newargt, 2) = mk_cval(tmp2, astb.bnd.dtype);
spread = mk_id(intast_sym[I_SPREAD]);
ast_rhs = mk_func_node(A_INTR, spread, 3, newargt);
A_OPTYPEP(ast_rhs, I_SPREAD);
}
ast =
mk_assn_stmt(ast_lhs, ast_rhs, DTY(A_DTYPEG(A_LOPG(sg.dest)) + 1));
add_stmt_before(ast, arg_gbl.std);
}
/* handle case with m < 0 */
x = 0;
if (A_TYPEG(ss[s].m) == A_CNST) {
if (ss[s].mc < 0) {
x = 1;
}
} else {
int ast, expr;
x = 1;
ast = mk_stmt(A_ELSE, 0);
add_stmt_before(ast, arg_gbl.std);
}
if (x) {
/* a(la - m*sa : ua : sa) = b(lb : ub - m*sb : sb) */
tmp1 = opt_binop(OP_MUL, ss[s].m, sa, astb.bnd.dtype);
lla = opt_binop(OP_SUB, la, tmp1, astb.bnd.dtype);
sg.subsdest[ss[s].dim_dest] = mk_triple(lla, ua, sa);
tmp1 = opt_binop(OP_MUL, ss[s].m, sb, astb.bnd.dtype);
uub = opt_binop(OP_SUB, ub, tmp1, astb.bnd.dtype);
sg.subssrc[ss[s].dim_src] = mk_triple(lb, uub, sb);
recurse_shift(s + 1);
/* a(la : (la-m*sa)-1 : sa) = boundary */
lla = opt_binop(OP_SUB, lla, astb.bnd.one, astb.bnd.dtype);
sg.subsdest[ss[s].dim_dest] = mk_triple(la, lla, sa);
ast_lhs = mk_subscr(A_LOPG(sg.dest), sg.subsdest, sg.ndimdest,
A_DTYPEG(sg.dest));
ast_rhs = ss[s].boundary; /* boundary have to be spread if array */
if (A_SHAPEG(ast_rhs)) {
/* add spread call */
int newargt, spread;
newargt = mk_argt(3);
ARGT_ARG(newargt, 0) = ast_rhs;
ARGT_ARG(newargt, 1) = mk_cval(ss[s].dim_dest + 1, astb.bnd.dtype);
tmp2 = opt_binop(OP_SUB, ua, lla, astb.bnd.dtype);
if (sa != astb.i1 && sa != astb.bnd.one) {
tmp2 = opt_binop(OP_DIV, tmp2, sa, astb.bnd.dtype);
}
ARGT_ARG(newargt, 2) = mk_cval(tmp2, astb.bnd.dtype);
spread = mk_id(intast_sym[I_SPREAD]);
ast_rhs = mk_func_node(A_INTR, spread, 3, newargt);
A_OPTYPEP(ast_rhs, I_SPREAD);
}
ast =
mk_assn_stmt(ast_lhs, ast_rhs, DTY(A_DTYPEG(A_LOPG(sg.dest)) + 1));
add_stmt_before(ast, arg_gbl.std);
}
if (A_TYPEG(ss[s].m) != A_CNST) {
int ast, expr;
ast = mk_stmt(A_ENDIF, 0);
add_stmt_before(ast, arg_gbl.std);
}
}
sg.subsdest[ss[s].dim_dest] = ta;
sg.subssrc[ss[s].dim_src] = tb;
}
if (A_TYPEG(ss[s].m) != A_CNST) {
int ast;
ast = mk_stmt(A_ELSE, 0);
add_stmt_before(ast, arg_gbl.std);
}
if (ss[s].else_part) {
/* a(la:ua:sa) = b(lb:ub:sb) */
if (ss[s].shifttype == I_EOSHIFT)
recurse_eoshift(s + 1);
else
recurse_shift(s + 1);
}
if (A_TYPEG(ss[s].m) != A_CNST) {
int ast;
ast = mk_stmt(A_ENDIF, 0);
add_stmt_before(ast, arg_gbl.std);
}
} /* handle_shift */
/*
* for an EOSHIFT call with an omitted boundary value,
* use zero. This functions returns an AST referencing
* an appropriate 'zero' value for the given array datatype.
*/
static int
_makezero(DTYPE dtype)
{
int v[4], w[4], sptr;
INT V;
int sub, ndims, i;
int firstast, lastast, ast, member;
char *str;
int l, len;
switch (DTY(dtype)) {
case TY_HOLL:
case TY_WORD:
case TY_INT:
case TY_LOG:
case TY_REAL:
case TY_SINT:
case TY_BINT:
case TY_SLOG:
case TY_BLOG:
V = 0;
return mk_cval1(V, dtype);
case TY_DBLE:
case TY_QUAD:
case TY_DWORD:
case TY_LOG8:
case TY_INT8:
v[0] = v[1] = v[2] = v[3] = 0;
sptr = getcon(v, dtype);
return mk_cval1((INT)sptr, dtype);
case TY_CMPLX:
v[0] = stb.flt0;
v[1] = stb.flt0;
sptr = getcon(v, dtype);
return mk_cval(sptr, dtype);
case TY_DCMPLX:
v[0] = stb.dbl0;
v[1] = stb.dbl0;
sptr = getcon(v, dtype);
return mk_cval1(sptr, dtype);
case TY_QCMPLX:
v[0] = v[1] = v[2] = v[3] = 0;
v[0] = getcon(v, DT_QUAD);
v[1] = v[0];
sptr = getcon(v, dtype);
return mk_cval1(sptr, dtype);
case TY_CHAR:
case TY_NCHAR:
/* make blank */
len = DTY(dtype + 1);
if (!A_ALIASG(len)) {
len = 1;
} else {
len = A_ALIASG(len);
len = A_SPTRG(len);
len = CONVAL2G(len);
}
str = (char *)malloc(len + 1);
for (l = 0; l < len; ++l)
str[l] = ' ';
str[len] = '\0';
sptr = getstring(str, len);
free(str);
return mk_id(sptr);
break;
case TY_ARRAY:
/* make an array of zeros */
sub = _makezero(DTY(dtype + 1));
ndims = ADD_NUMDIM(dtype);
for (i = 0; i < ndims; ++i) {
int j, extent, prevast, ast;
extent = ADD_EXTNTAST(dtype, i);
if (!A_ALIASG(extent)) {
extent = 1;
} else {
extent = A_ALIASG(extent);
extent = A_SPTRG(extent);
extent = CONVAL2G(extent);
}
prevast = 0;
for (j = 0; j < extent; ++j) {
ast = mk_init(sub, DTY(dtype + 1));
A_RIGHTP(ast, prevast);
prevast = ast;
}
sub = ast;
}
return sub;
case TY_STRUCT:
case TY_DERIVED:
/* make a structure of zeros */
firstast = 0;
lastast = 0;
for (member = DTY(dtype + 1); member > NOSYM; member = SYMLKG(member)) {
sub = _makezero(DTYPEG(member));
ast = mk_init(sub, DTYPEG(member));
if (firstast == 0) {
firstast = ast;
} else {
A_RIGHTP(lastast, ast);
}
lastast = ast;
A_SPTRP(ast, member);
}
return firstast;
case TY_UNION:
case TY_PTR:
case TY_NONE:
default:
interr("makezero: unknown datatype", DTY(dtype), 4);
break;
}
return 0;
} /* _makezero */
/*
* write data-initialization to dinit file for array/structure
*/
static void
putzero(int ast)
{
/* derived type? */
for (; ast; ast = A_RIGHTG(ast)) {
int a, dtype, sptr;
a = A_LEFTG(ast);
switch (A_TYPEG(a)) {
case A_INIT:
dtype = A_DTYPEG(a);
if (DTY(dtype) == TY_DERIVED || DTY(dtype) == TY_STRUCT) {
if (DTY(dtype + 3)) {
dinit_put(DINIT_TYPEDEF, DTY(dtype + 3));
}
}
putzero(a);
if (DTY(dtype) == TY_DERIVED || DTY(dtype) == TY_STRUCT) {
if (DTY(dtype + 3)) {
dinit_put(DINIT_ENDTYPE, DTY(dtype + 3));
}
}
break;
case A_ID:
case A_CNST:
sptr = A_SPTRG(a);
dtype = DTYPEG(sptr);
switch (DTY(dtype)) {
case TY_BINT:
case TY_SINT:
case TY_INT:
case TY_BLOG:
case TY_SLOG:
case TY_LOG:
case TY_FLOAT:
dinit_put(dtype, CONVAL2G(sptr));
break;
case TY_DBLE:
case TY_CMPLX:
case TY_DCMPLX:
case TY_QUAD:
case TY_QCMPLX:
case TY_INT8:
case TY_LOG8:
case TY_CHAR:
dinit_put(dtype, sptr);
break;
}
break;
}
}
} /* putzero */
/*
* for an EOSHIFT call with an omitted boundary value,
* use zero. This functions returns an AST referencing
* an appropriate 'zero' value for the given array datatype.
*/
static int
makezero(DTYPE dtype)
{
int sub, sptr;
switch (DTY(dtype)) {
default:
return _makezero(dtype);
case TY_ARRAY:
/* make an array of zeros */
sub = _makezero(dtype);
sptr = get_next_sym("init", "r");
STYPEP(sptr, ST_ARRAY);
DTYPEP(sptr, dtype);
SCP(sptr, SC_STATIC);
DINITP(sptr, 1);
SEQP(sptr, 1);
PARAMP(sptr, 1);
PARAMVALP(sptr, sub);
dinit_put(DINIT_LOC, sptr);
putzero(sub);
dinit_put(DINIT_END, 0);
return mk_id(sptr);
case TY_STRUCT:
case TY_UNION:
case TY_DERIVED:
/* make an array of zeros */
sub = _makezero(dtype);
sptr = get_next_sym("init", "r");
STYPEP(sptr, ST_VAR);
DTYPEP(sptr, dtype);
SCP(sptr, SC_STATIC);
DINITP(sptr, 1);
SEQP(sptr, 1);
PARAMP(sptr, 1);
PARAMVALP(sptr, sub);
/* dump out the values to the data initialization file */
dinit_put(DINIT_LOC, sptr);
if (DTY(dtype + 3)) {
dinit_put(DINIT_TYPEDEF, DTY(dtype + 3));
}
putzero(sub);
if (DTY(dtype + 3)) {
dinit_put(DINIT_ENDTYPE, DTY(dtype + 3));
}
dinit_put(DINIT_END, 0);
return mk_id(sptr);
}
} /* makezero */
static void
inline_shifts(int func_ast, int func_args, int lhs)
{
int srcarray;
int s;
int sptrsrc, sptrdest;
int asdsrc, asddest;
int count;
int i;
int args;
sg.shiftcount = 0;
srcarray = func_ast;
args = func_args;
/* find all nested cshift/eoshift calls */
while (A_TYPEG(srcarray) == A_INTR) {
if (A_OPTYPEG(srcarray) == I_CSHIFT) {
/* cshift(array, shift, [dim]) */
assert(sg.shiftcount < SHIFTMAX, "inline_shifts: too many nested shifts",
func_ast, 3);
srcarray = ARGT_ARG(args, 0);
s = sg.shiftcount;
ss[s].shift = ARGT_ARG(args, 1);
ss[s].dim = ARGT_ARG(args, 2);
ss[s].shifttype = I_CSHIFT;
} else if (A_OPTYPEG(srcarray) == I_EOSHIFT) {
/* eoshift(array, shift, [boundary, dim]); */
assert(sg.shiftcount < SHIFTMAX, "inline_shifts: too many nested shifts",
func_ast, 3);
srcarray = ARGT_ARG(args, 0);
s = sg.shiftcount;
ss[s].shift = ARGT_ARG(args, 1);
ss[s].boundary = ARGT_ARG(args, 2);
if (ss[s].boundary == 0) {
/* must create a 'zero' */
if (DTY(A_DTYPEG(srcarray)) == TY_ARRAY) {
ss[s].boundary = makezero(DTY(A_DTYPEG(srcarray) + 1));
} else {
ss[s].boundary = makezero(A_DTYPEG(srcarray));
}
}
ss[s].dim = ARGT_ARG(args, 3);
ss[s].shifttype = I_EOSHIFT;
} else {
interr("inline_shifts: must be CSHIFT or EOSHIFT", srcarray, 3);
}
if (ss[s].dim == 0)
ss[s].dim = mk_cval(1, astb.bnd.dtype);
assert(A_TYPEG(ss[s].dim) == A_CNST,
"inline_shifts: variable dimension not implemented", srcarray, 3);
ss[s].cdim = get_int_cval(A_SPTRG(A_ALIASG(ss[s].dim)));
++sg.shiftcount;
args = A_ARGSG(srcarray);
}
assert(lhs, "inline_shifts: no lhs", func_ast, 3);
assert(A_TYPEG(lhs) == A_ID || A_TYPEG(lhs) == A_SUBSCR ||
A_TYPEG(lhs) == A_MEM,
"inline_shifts: bad lhs type", func_ast, 3);
assert(A_TYPEG(srcarray) == A_ID || A_TYPEG(srcarray) == A_SUBSCR ||
A_TYPEG(srcarray) == A_MEM,
"inline_shifts: bad source type", func_ast, 3);
sg.src = convert_subscript(srcarray);
sg.dest = convert_subscript(lhs);
sptrsrc = memsym_of_ast(sg.src);
sptrdest = memsym_of_ast(sg.dest);
asdsrc = A_ASDG(sg.src);
sg.ndimsrc = ASD_NDIM(asdsrc);
for (s = 0; s < sg.shiftcount; ++s) {
if (ss[s].cdim > sg.ndimsrc || (ss[s].cdim < 1 || ss[s].cdim > 7)) {
error(504, 3, gbl.lineno, SYMNAME(sptrsrc), CNULL);
ss[s].cdim = 1;
}
}
count = 0;
for (i = 0; i < sg.ndimsrc; ++i) {
if (A_TYPEG(ASD_SUBS(asdsrc, i)) == A_TRIPLE ||
A_SHAPEG(ASD_SUBS(asdsrc, i))) {
++count;
for (s = 0; s < sg.shiftcount; ++s) {
if (count == ss[s].cdim) {
ss[s].dim_src = i;
break;
}
}
}
}
asddest = A_ASDG(sg.dest);
sg.ndimdest = ASD_NDIM(asddest);
count = 0;
for (i = 0; i < sg.ndimdest; ++i) {
if (A_TYPEG(ASD_SUBS(asddest, i)) == A_TRIPLE ||
A_SHAPEG(ASD_SUBS(asddest, i))) {
++count;
for (s = 0; s < sg.shiftcount; ++s) {
if (count == ss[s].cdim) {
ss[s].dim_dest = i;
break;
}
}
}
}
/* Determine the section extent */
for (s = 0; s < sg.shiftcount; ++s) {
ss[s].n = triplet_extent(ASD_SUBS(asdsrc, ss[s].dim_src));
if (A_TYPEG(ss[s].n) != A_CNST) {
int tmp, ast;
tmp = sym_get_scalar("n", "s", astb.bnd.dtype);
ast = mk_assn_stmt(mk_id(tmp), ss[s].n, astb.bnd.dtype);
add_stmt_before(ast, arg_gbl.std);
ss[s].n = mk_id(tmp);
} else {
ss[s].nc = get_int_cval(A_SPTRG(A_ALIASG(ss[s].n)));
}
/* Determine the net positive shift amount for CSHIFT
* m = MOD(k, n)
* if (m .lt. 0) then
* m = n + m
* endif
*/
ss[s].k = ss[s].shift;
if (A_TYPEG(ss[s].k) == A_CNST && A_TYPEG(ss[s].n) == A_CNST) {
int result;
ss[s].kc = get_int_cval(A_SPTRG(A_ALIASG(ss[s].k)));
result = ss[s].kc % ss[s].nc;
ss[s].m = mk_cval(result, astb.bnd.dtype);
} else {
int mod, tmp, ast;
mod = ast_intr(I_MOD, DT_INT, 2, ss[s].k, ss[s].n);
tmp = sym_get_scalar("m", "s", astb.bnd.dtype);
ss[s].m = mk_id(tmp);
ast = mk_assn_stmt(ss[s].m, mod, astb.bnd.dtype);
add_stmt_before(ast, arg_gbl.std);
}
ss[s].lt = TRUE;
if (A_TYPEG(ss[s].m) == A_CNST) {
ss[s].mc = get_int_cval(A_SPTRG(A_ALIASG(ss[s].m)));
if (ss[s].mc >= 0) {
ss[s].lt = FALSE;
} else if (ss[s].shifttype == I_CSHIFT) {
if (A_TYPEG(ss[s].n) == A_CNST) {
ss[s].mc = ss[s].mc + ss[s].nc;
ss[s].m = mk_cval(ss[s].mc, astb.bnd.dtype);
ss[s].lt = FALSE;
} else {
int ast, tmp;
ast = opt_binop(OP_ADD, ss[s].m, ss[s].n, astb.bnd.dtype);
tmp = sym_get_scalar("m", "s", astb.bnd.dtype);
ss[s].m = mk_id(tmp);
ast = mk_assn_stmt(ss[s].m, ast, astb.bnd.dtype);
add_stmt_before(ast, arg_gbl.std);
}
}
}
if (ss[s].lt && ss[s].shifttype == I_CSHIFT) {
int ast, expr;
ast = mk_stmt(A_IFTHEN, 0);
expr = mk_binop(OP_LT, ss[s].m, astb.bnd.zero, DT_LOG);
A_IFEXPRP(ast, expr);
add_stmt_before(ast, arg_gbl.std);
ast = mk_assn_stmt(ss[s].m,
opt_binop(OP_ADD, ss[s].n, ss[s].m, astb.bnd.dtype),
astb.bnd.dtype);
add_stmt_before(ast, arg_gbl.std);
ast = mk_stmt(A_ENDIF, 0);
add_stmt_before(ast, arg_gbl.std);
}
ss[s].then_part = FALSE;
ss[s].else_part = FALSE;
if (A_TYPEG(ss[s].m) != A_CNST) {
ss[s].then_part = TRUE;
ss[s].else_part = TRUE;
} else if (ss[s].mc != 0) {
ss[s].then_part = TRUE;
} else {
ss[s].else_part = TRUE;
}
}
for (i = 0; i < sg.ndimdest; ++i) {
sg.subsdest[i] = ASD_SUBS(asddest, i);
}
for (i = 0; i < sg.ndimsrc; ++i) {
sg.subssrc[i] = ASD_SUBS(asdsrc, i);
}
handle_shift(0);
} /* inline_shifts */
/* Determine the section extent
* n = (ub - lb + sb) / sb
*/
static int
triplet_extent(int t)
{
int lb, ub, sb;
int tmp1, tmp2, tmp3;
assert(A_TYPEG(t) == A_TRIPLE, "triplet_extent: should be triplet", t, 3);
lb = A_LBDG(t);
ub = A_UPBDG(t);
sb = A_STRIDEG(t);
tmp1 = opt_binop(OP_SUB, ub, lb, astb.bnd.dtype);
tmp2 = opt_binop(OP_ADD, tmp1, sb, astb.bnd.dtype);
tmp3 = opt_binop(OP_DIV, tmp2, sb, astb.bnd.dtype);
return tmp3;
}
static LOGICAL
is_inline_overlap_shifts(int func_ast, int func_args, int lhs)
{
return FALSE;
}
LOGICAL
is_shift_conflict(int func_ast, int func_args, int expr)
{
int srcarray;
int boundary;
int sptr;
srcarray = ARGT_ARG(func_args, 0);
sptr = memsym_of_ast(srcarray);
boundary = -1;
if (A_OPTYPEG(func_ast) == I_EOSHIFT)
boundary = ARGT_ARG(func_args, 2);
if (A_OPTYPEG(func_ast) == I_CSHIFT)
if (expr && is_another_shift(expr, sptr, I_EOSHIFT, boundary))
return TRUE;
if (A_OPTYPEG(func_ast) == I_EOSHIFT) {
if (expr && is_another_shift(expr, sptr, I_CSHIFT, boundary))
return TRUE;
if (expr && is_another_shift(expr, sptr, I_EOSHIFT, boundary))
return TRUE;
}
return FALSE;
}
static struct {
int sptr;
int type;
int boundary;
} expp;
static LOGICAL
is_another_shift(int expr, int sptr, int type, int boundary)
{
LOGICAL result = FALSE;
expp.sptr = sptr;
expp.type = type;
expp.boundary = boundary;
ast_visit(1, 1);
ast_traverse(expr, _is_another_shift, NULL, &result);
ast_unvisit();
return result;
}
static LOGICAL
_is_another_shift(int targast, LOGICAL *pflag)
{
int boundary;
int sptr;
int type;
int srcarray;
int args;
int check_boundary;
if (A_TYPEG(targast) == A_INTR) {
if (A_OPTYPEG(targast) == I_CSHIFT || A_OPTYPEG(targast) == I_EOSHIFT) {
type = A_OPTYPEG(targast);
args = A_ARGSG(targast);
srcarray = ARGT_ARG(args, 0);
boundary = 0;
if (type == I_EOSHIFT)
boundary = ARGT_ARG(args, 2);
sptr = 0;
switch (A_TYPEG(srcarray)) {
case A_ID:
case A_SUBSCR:
sptr = memsym_of_ast(srcarray);
break;
}
check_boundary = 1;
if (expp.boundary != -1)
if (expp.boundary == boundary)
check_boundary = 0;
if (expp.sptr == sptr && expp.type == type && check_boundary) {
*pflag = TRUE;
return TRUE;
}
}
}
return FALSE;
}
static int
stride_one(int lw, int up)
{
if (A_TYPEG(lw) == A_CNST && A_TYPEG(up) == A_CNST &&
ad_val_of(A_SPTRG(lw)) > ad_val_of(A_SPTRG(up)))
return mk_isz_cval((ISZ_T)-1, astb.bnd.dtype);
return astb.bnd.one;
}
int
convert_subscript(int a)
{
ADSC *ad;
int sptr, parent, member;
int ndim;
int lb, ub, st;
int i;
int subs[MAXSUBS];