Break circular dependency between FIR dialect and utilities
[llvm-project.git] / flang / test / Lower / Intrinsics / max.f90
blobbdc7e0be43d23d1ec0ae296f830258a8c7eb631d
1 ! RUN: bbc -emit-fir %s -o - | FileCheck %s
3 module max_test
4 contains
5 ! CHECK-LABEL: func @_QMmax_testPdynamic_optional(
6 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "a"},
7 ! CHECK-SAME: %[[VAL_1:.*]]: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "b"},
8 ! CHECK-SAME: %[[VAL_2:.*]]: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "c", fir.optional}) {
9 subroutine dynamic_optional(a, b, c)
10 integer :: a(:), b(:)
11 integer, optional :: c(:)
12 ! CHECK: %[[VAL_10:.*]] = fir.array_load %[[VAL_0]] : (!fir.box<!fir.array<?xi32>>) -> !fir.array<?xi32>
13 ! CHECK: %[[VAL_11:.*]] = fir.array_load %[[VAL_1]] : (!fir.box<!fir.array<?xi32>>) -> !fir.array<?xi32>
14 ! CHECK: %[[VAL_12:.*]] = fir.is_present %[[VAL_2]] : (!fir.box<!fir.array<?xi32>>) -> i1
15 ! CHECK: %[[VAL_17:.*]] = arith.select %[[VAL_12]], %[[VAL_2]], %{{.*}} : !fir.box<!fir.array<?xi32>>
16 ! CHECK: %[[VAL_18:.*]] = fir.array_load %[[VAL_17]] {fir.optional} : (!fir.box<!fir.array<?xi32>>) -> !fir.array<?xi32>
17 ! CHECK: fir.do_loop %[[VAL_26:.*]] = %{{.*}} to %{{.*}} step %{{.*}} unordered iter_args(%[[VAL_27:.*]] = %{{.*}}) -> (!fir.array<?xi32>) {
18 ! CHECK: %[[VAL_28:.*]] = fir.array_fetch %[[VAL_10]], %[[VAL_26]] : (!fir.array<?xi32>, index) -> i32
19 ! CHECK: %[[VAL_29:.*]] = fir.array_fetch %[[VAL_11]], %[[VAL_26]] : (!fir.array<?xi32>, index) -> i32
20 ! CHECK: %[[VAL_30:.*]] = arith.cmpi sgt, %[[VAL_28]], %[[VAL_29]] : i32
21 ! CHECK: %[[VAL_31:.*]] = arith.select %[[VAL_30]], %[[VAL_28]], %[[VAL_29]] : i32
22 ! CHECK: %[[VAL_32:.*]] = fir.if %[[VAL_12]] -> (i32) {
23 ! CHECK: %[[VAL_33:.*]] = fir.array_fetch %[[VAL_18]], %[[VAL_26]] : (!fir.array<?xi32>, index) -> i32
24 ! CHECK: %[[VAL_34:.*]] = arith.cmpi sgt, %[[VAL_31]], %[[VAL_33]] : i32
25 ! CHECK: %[[VAL_35:.*]] = arith.select %[[VAL_34]], %[[VAL_31]], %[[VAL_33]] : i32
26 ! CHECK: fir.result %[[VAL_35]] : i32
27 ! CHECK: } else {
28 ! CHECK: fir.result %[[VAL_31]] : i32
29 ! CHECK: }
30 ! CHECK: %[[VAL_36:.*]] = fir.array_update %[[VAL_27]], %[[VAL_32]], %[[VAL_26]] : (!fir.array<?xi32>, i32, index) -> !fir.array<?xi32>
31 ! CHECK: fir.result %[[VAL_36]] : !fir.array<?xi32>
32 ! CHECK: }
33 print *, max(a, b, c)
34 end subroutine
36 ! CHECK-LABEL: func @_QMmax_testPdynamic_optional_array_expr_scalar_optional(
37 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "a"},
38 ! CHECK-SAME: %[[VAL_1:.*]]: !fir.box<!fir.array<?xi32>> {fir.bindc_name = "b"},
39 ! CHECK-SAME: %[[VAL_2:.*]]: !fir.ref<i32> {fir.bindc_name = "c", fir.optional}) {
40 subroutine dynamic_optional_array_expr_scalar_optional(a, b, c)
41 integer :: a(:), b(:)
42 integer, optional :: c
43 print *, max(a, b, c)
44 ! CHECK: %[[VAL_10:.*]] = fir.array_load %[[VAL_0]] : (!fir.box<!fir.array<?xi32>>) -> !fir.array<?xi32>
45 ! CHECK: %[[VAL_11:.*]] = fir.array_load %[[VAL_1]] : (!fir.box<!fir.array<?xi32>>) -> !fir.array<?xi32>
46 ! CHECK: %[[VAL_12:.*]] = fir.is_present %[[VAL_2]] : (!fir.ref<i32>) -> i1
47 ! CHECK: fir.do_loop %[[VAL_20:.*]] = %{{.*}} to %{{.*}} step %{{.*}} unordered iter_args(%[[VAL_21:.*]] = %{{.*}}) -> (!fir.array<?xi32>) {
48 ! CHECK: %[[VAL_22:.*]] = fir.array_fetch %[[VAL_10]], %[[VAL_20]] : (!fir.array<?xi32>, index) -> i32
49 ! CHECK: %[[VAL_23:.*]] = fir.array_fetch %[[VAL_11]], %[[VAL_20]] : (!fir.array<?xi32>, index) -> i32
50 ! CHECK: %[[VAL_24:.*]] = arith.cmpi sgt, %[[VAL_22]], %[[VAL_23]] : i32
51 ! CHECK: %[[VAL_25:.*]] = arith.select %[[VAL_24]], %[[VAL_22]], %[[VAL_23]] : i32
52 ! CHECK: %[[VAL_26:.*]] = fir.if %[[VAL_12]] -> (i32) {
53 ! CHECK: %[[VAL_27:.*]] = fir.load %[[VAL_2]] : !fir.ref<i32>
54 ! CHECK: %[[VAL_28:.*]] = arith.cmpi sgt, %[[VAL_25]], %[[VAL_27]] : i32
55 ! CHECK: %[[VAL_29:.*]] = arith.select %[[VAL_28]], %[[VAL_25]], %[[VAL_27]] : i32
56 ! CHECK: fir.result %[[VAL_29]] : i32
57 ! CHECK: } else {
58 ! CHECK: fir.result %[[VAL_25]] : i32
59 ! CHECK: }
60 ! CHECK: %[[VAL_30:.*]] = fir.array_update %[[VAL_21]], %[[VAL_26]], %[[VAL_20]] : (!fir.array<?xi32>, i32, index) -> !fir.array<?xi32>
61 ! CHECK: fir.result %[[VAL_30]] : !fir.array<?xi32>
62 ! CHECK: }
63 end subroutine
65 ! CHECK-LABEL: func @_QMmax_testPdynamic_optional_scalar(
66 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<i32> {fir.bindc_name = "a"},
67 ! CHECK-SAME: %[[VAL_1:.*]]: !fir.ref<i32> {fir.bindc_name = "b"},
68 ! CHECK-SAME: %[[VAL_2:.*]]: !fir.ref<i32> {fir.bindc_name = "c", fir.optional}) {
69 subroutine dynamic_optional_scalar(a, b, c)
70 integer :: a, b
71 integer, optional :: c
72 print *, max(a, b, c)
73 ! CHECK: %[[VAL_8:.*]] = fir.load %[[VAL_0]] : !fir.ref<i32>
74 ! CHECK: %[[VAL_9:.*]] = fir.load %[[VAL_1]] : !fir.ref<i32>
75 ! CHECK: %[[VAL_10:.*]] = fir.is_present %[[VAL_2]] : (!fir.ref<i32>) -> i1
76 ! CHECK: %[[VAL_11:.*]] = arith.cmpi sgt, %[[VAL_8]], %[[VAL_9]] : i32
77 ! CHECK: %[[VAL_12:.*]] = arith.select %[[VAL_11]], %[[VAL_8]], %[[VAL_9]] : i32
78 ! CHECK: %[[VAL_13:.*]] = fir.if %[[VAL_10]] -> (i32) {
79 ! CHECK: %[[VAL_14:.*]] = fir.load %[[VAL_2]] : !fir.ref<i32>
80 ! CHECK: %[[VAL_15:.*]] = arith.cmpi sgt, %[[VAL_12]], %[[VAL_14]] : i32
81 ! CHECK: %[[VAL_16:.*]] = arith.select %[[VAL_15]], %[[VAL_12]], %[[VAL_14]] : i32
82 ! CHECK: fir.result %[[VAL_16]] : i32
83 ! CHECK: } else {
84 ! CHECK: fir.result %[[VAL_12]] : i32
85 ! CHECK: }
86 ! CHECK: fir.call @_FortranAioOutputInteger32(%{{.*}}, %[[VAL_13]]) {{.*}}: (!fir.ref<i8>, i32) -> i1
87 end subroutine
89 ! CHECK-LABEL: func @_QMmax_testPdynamic_optional_weird(
90 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<i32> {fir.bindc_name = "a"},
91 ! CHECK-SAME: %[[VAL_1:.*]]: !fir.ref<i32> {fir.bindc_name = "b"},
92 ! CHECK-SAME: %[[VAL_2:.*]]: !fir.ref<i32> {fir.bindc_name = "c", fir.optional},
93 ! CHECK-SAME: %[[VAL_3:.*]]: !fir.ref<i32> {fir.bindc_name = "d"},
94 ! CHECK-SAME: %[[VAL_4:.*]]: !fir.ref<i32> {fir.bindc_name = "e", fir.optional}) {
95 subroutine dynamic_optional_weird(a, b, c, d, e)
96 integer :: a, b, d
97 integer, optional :: c, e
98 ! a3, a4, a6, a8 statically missing. a5, a9 dynamically optional.
99 print *, max(a1=a, a2=b, a5=c, a7=d, a9 = e)
100 ! CHECK: %[[VAL_10:.*]] = fir.load %[[VAL_0]] : !fir.ref<i32>
101 ! CHECK: %[[VAL_11:.*]] = fir.load %[[VAL_1]] : !fir.ref<i32>
102 ! CHECK: %[[VAL_12:.*]] = fir.is_present %[[VAL_2]] : (!fir.ref<i32>) -> i1
103 ! CHECK: %[[VAL_13:.*]] = fir.load %[[VAL_3]] : !fir.ref<i32>
104 ! CHECK: %[[VAL_14:.*]] = fir.is_present %[[VAL_4]] : (!fir.ref<i32>) -> i1
105 ! CHECK: %[[VAL_15:.*]] = arith.cmpi sgt, %[[VAL_10]], %[[VAL_11]] : i32
106 ! CHECK: %[[VAL_16:.*]] = arith.select %[[VAL_15]], %[[VAL_10]], %[[VAL_11]] : i32
107 ! CHECK: %[[VAL_17:.*]] = fir.if %[[VAL_12]] -> (i32) {
108 ! CHECK: %[[VAL_18:.*]] = fir.load %[[VAL_2]] : !fir.ref<i32>
109 ! CHECK: %[[VAL_19:.*]] = arith.cmpi sgt, %[[VAL_16]], %[[VAL_18]] : i32
110 ! CHECK: %[[VAL_20:.*]] = arith.select %[[VAL_19]], %[[VAL_16]], %[[VAL_18]] : i32
111 ! CHECK: fir.result %[[VAL_20]] : i32
112 ! CHECK: } else {
113 ! CHECK: fir.result %[[VAL_16]] : i32
114 ! CHECK: }
115 ! CHECK: %[[VAL_21:.*]] = arith.cmpi sgt, %[[VAL_17]], %[[VAL_13]] : i32
116 ! CHECK: %[[VAL_23:.*]] = arith.select %[[VAL_21]], %[[VAL_17]], %[[VAL_13]] : i32
117 ! CHECK: %[[VAL_24:.*]] = fir.if %[[VAL_14]] -> (i32) {
118 ! CHECK: %[[VAL_25:.*]] = fir.load %[[VAL_4]] : !fir.ref<i32>
119 ! CHECK: %[[VAL_26:.*]] = arith.cmpi sgt, %[[VAL_23]], %[[VAL_25]] : i32
120 ! CHECK: %[[VAL_27:.*]] = arith.select %[[VAL_26]], %[[VAL_23]], %[[VAL_25]] : i32
121 ! CHECK: fir.result %[[VAL_27]] : i32
122 ! CHECK: } else {
123 ! CHECK: fir.result %[[VAL_23]] : i32
124 ! CHECK: }
125 ! CHECK: fir.call @_FortranAioOutputInteger32(%{{.*}}, %[[VAL_24]]) {{.*}}: (!fir.ref<i8>, i32) -> i1
126 end subroutine
127 end module
129 use :: max_test
130 integer :: a(4) = [1,12,23, 34]
131 integer :: b(4) = [31,22,13, 4]
132 integer :: c(4) = [21,32,3, 14]
133 call dynamic_optional(a, b)
134 call dynamic_optional(a, b, c)
135 call dynamic_optional_array_expr_scalar_optional(a, b)
136 call dynamic_optional_array_expr_scalar_optional(a, b, c(2))
137 call dynamic_optional_scalar(a(2), b(2))
138 call dynamic_optional_scalar(a(2), b(2), c(2))