IR: de-duplicate two CmpInst routines (NFC) (#116866)
[llvm-project.git] / flang / test / Lower / default-initialization-globals.f90
blobe9611dab467cbafb18ba2ca30c31a222a3fd435b
1 ! Test default initialization of global variables (static init)
2 ! RUN: bbc -hlfir=false %s -o - | FileCheck %s --check-prefixes=%if target={{.*-aix.*|sparc.*}} %{"CHECK","CHECK-BE"%} \
3 ! RUN: %else %{"CHECK","CHECK-LE"%}
5 module tinit
6 real, target :: ziel(100)
8 type tno_init
9 integer :: k
10 end type
12 type t0
13 integer :: k = 66
14 end type
16 ! Test type that combines all kinds of components with and without
17 ! default initialization.
18 type t1
19 ! Simple type component with default init
20 integer :: i = 42
21 ! Simple type component without default init
22 integer :: j
24 ! Pointer component with a default initial target
25 real, pointer :: x(:) => ziel
26 ! Pointer component with no init
27 real, pointer :: y(:)
28 ! Pointer component with null init
29 real, pointer :: z(:) => NULL()
31 ! Character component with init
32 character(10) c1 = "hello"
33 ! Character component without init
34 character(10) c2
36 ! Component with a derived type that has default init
37 type(t0) :: somet0
38 ! Component with a derived type that has no default init.
39 type(tno_init) :: sometno_init
40 ! Component whose type default init is overridden by
41 ! default init for the component.
42 type(t0) :: somet0_2 = t0(33)
43 ! Array component with a derived type that has default init
44 type(t0) :: somet0_array
45 end type
47 ! Test type that extends type with default init.
48 type, extends(t0) :: textendst0
49 integer :: l
50 end type
52 ! Test type with default init in equivalences
53 type tseq
54 sequence
55 integer :: i = 2
56 integer :: j = 3
57 end type
59 type tv
60 real, pointer :: v(:)
61 end type
63 real, pointer :: mv(:)
65 ! Test scalar with default init
66 type(t0) :: at0
67 ! CHECK-LABEL: fir.global @_QMtinitEat0 : !fir.type<_QMtinitTt0{k:i32}> {
68 ! CHECK: %[[VAL_0:.*]] = arith.constant 66 : i32
69 ! CHECK: %[[VAL_1:.*]] = fir.undefined !fir.type<_QMtinitTt0{k:i32}>
70 ! CHECK: %[[VAL_2:.*]] = fir.insert_value %[[VAL_1]], %[[VAL_0]], ["k", !fir.type<_QMtinitTt0{k:i32}>] : (!fir.type<_QMtinitTt0{k:i32}>, i32) -> !fir.type<_QMtinitTt0{k:i32}>
71 ! CHECK: fir.has_value %[[VAL_2]] : !fir.type<_QMtinitTt0{k:i32}>
73 ! Test array with default init
74 type(t0) :: bt0(100)
75 ! CHECK-LABEL: @_QMtinitEbt0 : !fir.array<100x!fir.type<_QMtinitTt0{k:i32}>> {
76 ! CHECK: %[[VAL_3:.*]] = arith.constant 66 : i32
77 ! CHECK: %[[VAL_4:.*]] = fir.undefined !fir.type<_QMtinitTt0{k:i32}>
78 ! CHECK: %[[VAL_5:.*]] = fir.insert_value %[[VAL_4]], %[[VAL_3]], ["k", !fir.type<_QMtinitTt0{k:i32}>] : (!fir.type<_QMtinitTt0{k:i32}>, i32) -> !fir.type<_QMtinitTt0{k:i32}>
79 ! CHECK: %[[VAL_6:.*]] = fir.undefined !fir.array<100x!fir.type<_QMtinitTt0{k:i32}>>
80 ! CHECK: %[[VAL_7:.*]] = fir.insert_on_range %[[VAL_6]], %[[VAL_5]] from (0) to (99) : (!fir.array<100x!fir.type<_QMtinitTt0{k:i32}>>, !fir.type<_QMtinitTt0{k:i32}>) -> !fir.array<100x!fir.type<_QMtinitTt0{k:i32}>>
81 ! CHECK: fir.has_value %[[VAL_7]] : !fir.array<100x!fir.type<_QMtinitTt0{k:i32}>>
83 ! Test default init overridden by explicit init
84 type(t0) :: ct0 = t0(42)
85 ! CHECK-LABEL: fir.global @_QMtinitEct0 : !fir.type<_QMtinitTt0{k:i32}> {
86 ! CHECK: %[[VAL_8:.*]] = arith.constant 42 : i32
87 ! CHECK: %[[VAL_9:.*]] = fir.undefined !fir.type<_QMtinitTt0{k:i32}>
88 ! CHECK: %[[VAL_10:.*]] = fir.insert_value %[[VAL_9]], %[[VAL_8]], ["k", !fir.type<_QMtinitTt0{k:i32}>] : (!fir.type<_QMtinitTt0{k:i32}>, i32) -> !fir.type<_QMtinitTt0{k:i32}>
89 ! CHECK: fir.has_value %[[VAL_10]] : !fir.type<_QMtinitTt0{k:i32}>
91 ! Test a non trivial derived type mixing all sorts of default initialization
92 type(t1) :: dt1
93 ! CHECK-LABEL: @_QMtinitEdt1 : !fir.type<_QMtinitTt1{{.*}}> {
94 ! CHECK-DAG: %[[VAL_11:.*]] = arith.constant 42 : i32
95 ! CHECK-DAG: %[[VAL_12:.*]] = arith.constant 100 : index
96 ! CHECK-DAG: %[[VAL_13:.*]] = arith.constant 0 : index
97 ! CHECK-DAG: %[[VAL_14:.*]] = arith.constant 33 : i32
98 ! CHECK-DAG: %[[VAL_15:.*]] = arith.constant 66 : i32
99 ! CHECK: %[[VAL_16:.*]] = fir.undefined !fir.type<_QMtinitTt1{{.*}}>
100 ! CHECK: %[[VAL_17:.*]] = fir.insert_value %[[VAL_16]], %[[VAL_11]], ["i", !fir.type<_QMtinitTt1{{.*}}>] : (!fir.type<_QMtinitTt1{{.*}}>, i32) -> !fir.type<_QMtinitTt1{{.*}}>
101 ! CHECK: %[[VAL_18:.*]] = fir.zero_bits i32
102 ! CHECK: %[[VAL_19:.*]] = fir.insert_value %[[VAL_17]], %[[VAL_18]], ["j", !fir.type<_QMtinitTt1{{.*}}>] : (!fir.type<_QMtinitTt1{{.*}}>, i32) -> !fir.type<_QMtinitTt1{{.*}}>
103 ! CHECK: %[[VAL_20:.*]] = fir.address_of(@_QMtinitEziel) : !fir.ref<!fir.array<100xf32>>
104 ! CHECK: %[[VAL_21:.*]] = fir.shape %[[VAL_12]] : (index) -> !fir.shape<1>
105 ! CHECK: %[[VAL_22:.*]] = fir.embox %[[VAL_20]](%[[VAL_21]]) : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.box<!fir.array<100xf32>>
106 ! CHECK: %[[VAL_22_B:.*]] = fir.rebox %[[VAL_22]] : (!fir.box<!fir.array<100xf32>>) -> !fir.box<!fir.ptr<!fir.array<?xf32>>>
107 ! CHECK: %[[VAL_23:.*]] = fir.insert_value %[[VAL_19]], %[[VAL_22_B]], ["x", !fir.type<_QMtinitTt1{{.*}}>] : (!fir.type<_QMtinitTt1{{.*}}>, !fir.box<!fir.ptr<!fir.array<?xf32>>>) -> !fir.type<_QMtinitTt1{{.*}}>
108 ! CHECK: %[[VAL_24:.*]] = fir.zero_bits !fir.ptr<!fir.array<?xf32>>
109 ! CHECK: %[[VAL_25:.*]] = fir.shape %[[VAL_13]] : (index) -> !fir.shape<1>
110 ! CHECK: %[[VAL_26:.*]] = fir.embox %[[VAL_24]](%[[VAL_25]]) : (!fir.ptr<!fir.array<?xf32>>, !fir.shape<1>) -> !fir.box<!fir.ptr<!fir.array<?xf32>>>
111 ! CHECK: %[[VAL_27:.*]] = fir.insert_value %[[VAL_23]], %[[VAL_26]], ["y", !fir.type<_QMtinitTt1{{.*}}>] : (!fir.type<_QMtinitTt1{{.*}}>, !fir.box<!fir.ptr<!fir.array<?xf32>>>) -> !fir.type<_QMtinitTt1{{.*}}>
112 ! CHECK: %[[VAL_28:.*]] = fir.insert_value %[[VAL_27]], %[[VAL_26]], ["z", !fir.type<_QMtinitTt1{{.*}}>] : (!fir.type<_QMtinitTt1{{.*}}>, !fir.box<!fir.ptr<!fir.array<?xf32>>>) -> !fir.type<_QMtinitTt1{{.*}}>
113 ! CHECK: %[[VAL_29:.*]] = fir.string_lit "hello "(10) : !fir.char<1,10>
114 ! CHECK: %[[VAL_30:.*]] = fir.insert_value %[[VAL_28]], %[[VAL_29]], ["c1", !fir.type<_QMtinitTt1{{.*}}>] : (!fir.type<_QMtinitTt1{{.*}}>, !fir.char<1,10>) -> !fir.type<_QMtinitTt1{{.*}}>
115 ! CHECK: %[[VAL_31:.*]] = fir.zero_bits !fir.char<1,10>
116 ! CHECK: %[[VAL_32:.*]] = fir.insert_value %[[VAL_30]], %[[VAL_31]], ["c2", !fir.type<_QMtinitTt1{{.*}}>] : (!fir.type<_QMtinitTt1{{.*}}>, !fir.char<1,10>) -> !fir.type<_QMtinitTt1{{.*}}>
117 ! CHECK: %[[VAL_33:.*]] = fir.undefined !fir.type<_QMtinitTt0{k:i32}>
118 ! CHECK: %[[VAL_34:.*]] = fir.insert_value %[[VAL_33]], %[[VAL_15]], ["k", !fir.type<_QMtinitTt0{k:i32}>] : (!fir.type<_QMtinitTt0{k:i32}>, i32) -> !fir.type<_QMtinitTt0{k:i32}>
119 ! CHECK: %[[VAL_35:.*]] = fir.insert_value %[[VAL_32]], %[[VAL_34]], ["somet0", !fir.type<_QMtinitTt1{{.*}}>] : (!fir.type<_QMtinitTt1{{.*}}>, !fir.type<_QMtinitTt0{k:i32}>) -> !fir.type<_QMtinitTt1{{.*}}>
120 ! CHECK: %[[VAL_36:.*]] = fir.zero_bits !fir.type<_QMtinitTtno_init{k:i32}>
121 ! CHECK: %[[VAL_37:.*]] = fir.insert_value %[[VAL_35]], %[[VAL_36]], ["sometno_init", !fir.type<_QMtinitTt1{{.*}}>] : (!fir.type<_QMtinitTt1{{.*}}>, !fir.type<_QMtinitTtno_init{k:i32}>) -> !fir.type<_QMtinitTt1{{.*}}>
122 ! CHECK: %[[VAL_38:.*]] = fir.insert_value %[[VAL_33]], %[[VAL_14]], ["k", !fir.type<_QMtinitTt0{k:i32}>] : (!fir.type<_QMtinitTt0{k:i32}>, i32) -> !fir.type<_QMtinitTt0{k:i32}>
123 ! CHECK: %[[VAL_39:.*]] = fir.insert_value %[[VAL_37]], %[[VAL_38]], ["somet0_2", !fir.type<_QMtinitTt1{{.*}}>] : (!fir.type<_QMtinitTt1{{.*}}>, !fir.type<_QMtinitTt0{k:i32}>) -> !fir.type<_QMtinitTt1{{.*}}>
124 ! CHECK: %[[VAL_40:.*]] = fir.insert_value %[[VAL_39]], %[[VAL_34]], ["somet0_array", !fir.type<_QMtinitTt1{{.*}}>] : (!fir.type<_QMtinitTt1{{.*}}>, !fir.type<_QMtinitTt0{k:i32}>) -> !fir.type<_QMtinitTt1{{.*}}>
125 ! CHECK: fir.has_value %[[VAL_40]] : !fir.type<_QMtinitTt1{{.*}}>
127 ! Test a type extending other type with a default init
128 type(textendst0) :: etextendst0
129 ! CHECK-LABEL: @_QMtinitEetextendst0 : !fir.type<_QMtinitTtextendst0{k:i32,l:i32}> {
130 ! CHECK: %[[VAL_42:.*]] = arith.constant 66 : i32
131 ! CHECK: %[[VAL_43:.*]] = fir.undefined !fir.type<_QMtinitTtextendst0{k:i32,l:i32}>
132 ! CHECK: %[[VAL_44:.*]] = fir.insert_value %[[VAL_43]], %[[VAL_42]], ["k", !fir.type<_QMtinitTtextendst0{k:i32,l:i32}>] : (!fir.type<_QMtinitTtextendst0{k:i32,l:i32}>, i32) -> !fir.type<_QMtinitTtextendst0{k:i32,l:i32}>
133 ! CHECK: %[[VAL_45:.*]] = fir.zero_bits i32
134 ! CHECK: %[[VAL_46:.*]] = fir.insert_value %[[VAL_44]], %[[VAL_45]], ["l", !fir.type<_QMtinitTtextendst0{k:i32,l:i32}>] : (!fir.type<_QMtinitTtextendst0{k:i32,l:i32}>, i32) -> !fir.type<_QMtinitTtextendst0{k:i32,l:i32}>
135 ! CHECK: fir.has_value %[[VAL_46]] : !fir.type<_QMtinitTtextendst0{k:i32,l:i32}>
137 type(tv) :: withmold = tv(null(mv))
138 ! CHECK-LABEL: fir.global @_QMtinitEwithmold
139 ! CHECK: %[[C0:.*]] = arith.constant 0 : index
140 ! CHECK: %[[UNDEF:.*]] = fir.undefined !fir.type<_QMtinitTtv{v:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>
141 ! CHECK: %[[ZERO:.*]] = fir.zero_bits !fir.ptr<!fir.array<?xf32>>
142 ! CHECK: %[[SHAPE:.*]] = fir.shape %[[C0]] : (index) -> !fir.shape<1>
143 ! CHECK: %[[ZEROBOX:.*]] = fir.embox %[[ZERO]](%[[SHAPE]]) : (!fir.ptr<!fir.array<?xf32>>, !fir.shape<1>) -> !fir.box<!fir.ptr<!fir.array<?xf32>>>
144 ! CHECK: %[[RET:.*]] = fir.insert_value %[[UNDEF]], %[[ZEROBOX]], ["v", !fir.type<_QMtinitTtv{v:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>] : (!fir.type<_QMtinitTtv{v:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>, !fir.box<!fir.ptr<!fir.array<?xf32>>>) -> !fir.type<_QMtinitTtv{v:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>
145 ! CHECK: fir.has_value %[[RET]] : !fir.type<_QMtinitTtv{v:!fir.box<!fir.ptr<!fir.array<?xf32>>>}>
147 end module
150 ! Test that default initialization is also applied to saved variables
151 subroutine saved()
152 use tinit
153 type(t0), save :: savedt0
154 ! CHECK-LABEL: fir.global internal @_QFsavedEsavedt0 : !fir.type<_QMtinitTt0{k:i32}> {
155 ! CHECK: %[[VAL_47:.*]] = arith.constant 66 : i32
156 ! CHECK: %[[VAL_48:.*]] = fir.undefined !fir.type<_QMtinitTt0{k:i32}>
157 ! CHECK: %[[VAL_49:.*]] = fir.insert_value %[[VAL_48]], %[[VAL_47]], ["k", !fir.type<_QMtinitTt0{k:i32}>] : (!fir.type<_QMtinitTt0{k:i32}>, i32) -> !fir.type<_QMtinitTt0{k:i32}>
158 ! CHECK: fir.has_value %[[VAL_49]] : !fir.type<_QMtinitTt0{k:i32}>
159 end subroutine
161 ! Test default initialization in equivalences
162 subroutine eqv()
163 use tinit
164 type(tseq), save :: somet
165 integer :: i(2)
166 equivalence (somet, i)
167 ! CHECK-LABEL: fir.global internal @_QFeqvEi : !fir.array<2xi32> {
168 ! CHECK-DAG: %[[VAL_50:.*]] = arith.constant 2 : i32
169 ! CHECK-DAG: %[[VAL_51:.*]] = arith.constant 3 : i32
170 ! CHECK: %[[VAL_52:.*]] = fir.undefined !fir.array<2xi32>
171 ! CHECK: %[[VAL_53:.*]] = fir.insert_value %[[VAL_52]], %[[VAL_50]], [0 : index] : (!fir.array<2xi32>, i32) -> !fir.array<2xi32>
172 ! CHECK: %[[VAL_54:.*]] = fir.insert_value %[[VAL_53]], %[[VAL_51]], [1 : index] : (!fir.array<2xi32>, i32) -> !fir.array<2xi32>
173 ! CHECK: fir.has_value %[[VAL_54]] : !fir.array<2xi32>
174 end subroutine
176 subroutine eqv_explicit_init()
177 use tinit
178 type(tseq), save :: somet
179 integer :: i(2) = [4, 5]
180 equivalence (somet, i)
181 ! CHECK-LABEL: fir.global internal @_QFeqv_explicit_initEi : !fir.array<2xi32> {
182 ! CHECK-DAG: %[[VAL_57:.*]] = arith.constant 4 : i32
183 ! CHECK-DAG: %[[VAL_58:.*]] = arith.constant 5 : i32
184 ! CHECK: %[[VAL_59:.*]] = fir.undefined !fir.array<2xi32>
185 ! CHECK: %[[VAL_60:.*]] = fir.insert_value %[[VAL_59]], %[[VAL_57]], [0 : index] : (!fir.array<2xi32>, i32) -> !fir.array<2xi32>
186 ! CHECK: %[[VAL_61:.*]] = fir.insert_value %[[VAL_60]], %[[VAL_58]], [1 : index] : (!fir.array<2xi32>, i32) -> !fir.array<2xi32>
187 ! CHECK: fir.has_value %[[VAL_61]] : !fir.array<2xi32>
188 end subroutine
190 subroutine eqv_same_default_init()
191 use tinit
192 type(tseq), save :: somet1(2), somet2
193 equivalence (somet1(1), somet2)
194 ! CHECK-LABEL: fir.global internal @_QFeqv_same_default_initEsomet1 : !fir.array<2xi64> {
195 ! CHECK-LE: %[[VAL_62:.*]] = arith.constant 12884901890 : i64
196 ! CHECK-BE: %[[VAL_62:.*]] = arith.constant 8589934595 : i64
197 ! CHECK: %[[VAL_63:.*]] = fir.undefined !fir.array<2xi64>
198 ! CHECK: %[[VAL_64:.*]] = fir.insert_on_range %[[VAL_63]], %[[VAL_62]] from (0) to (1) : (!fir.array<2xi64>, i64) -> !fir.array<2xi64>
199 ! CHECK: fir.has_value %[[VAL_64]] : !fir.array<2xi64>
200 end subroutine
202 subroutine eqv_full_overlaps_with_explicit_init()
203 use tinit
204 type(tseq), save :: somet
205 integer, save :: link(4)
206 integer :: i(2) = [5, 6]
207 integer :: j(2) = [7, 8]
208 ! Equivalence: somet fully covered by explicit init.
209 ! i(1)=5 | i(2)=6 | - | -
210 ! - | somet%i | somet%j |
211 ! - | - | j(1)=7 | j(2)=8
212 equivalence (i, link(1))
213 equivalence (somet, link(2))
214 equivalence (j, link(3))
215 ! CHECK-LABEL: fir.global internal @_QFeqv_full_overlaps_with_explicit_initEi : !fir.array<4xi32> {
216 ! CHECK-DAG: %[[VAL_73:.*]] = arith.constant 5 : i32
217 ! CHECK-DAG: %[[VAL_74:.*]] = arith.constant 6 : i32
218 ! CHECK-DAG: %[[VAL_75:.*]] = arith.constant 7 : i32
219 ! CHECK-DAG: %[[VAL_76:.*]] = arith.constant 8 : i32
220 ! CHECK-DAG: %[[VAL_77:.*]] = fir.undefined !fir.array<4xi32>
221 ! CHECK-DAG: %[[VAL_78:.*]] = fir.insert_value %[[VAL_77]], %[[VAL_73]], [0 : index] : (!fir.array<4xi32>, i32) -> !fir.array<4xi32>
222 ! CHECK-DAG: %[[VAL_79:.*]] = fir.insert_value %[[VAL_78]], %[[VAL_74]], [1 : index] : (!fir.array<4xi32>, i32) -> !fir.array<4xi32>
223 ! CHECK-DAG: %[[VAL_80:.*]] = fir.insert_value %[[VAL_79]], %[[VAL_75]], [2 : index] : (!fir.array<4xi32>, i32) -> !fir.array<4xi32>
224 ! CHECK-DAG: %[[VAL_81:.*]] = fir.insert_value %[[VAL_80]], %[[VAL_76]], [3 : index] : (!fir.array<4xi32>, i32) -> !fir.array<4xi32>
225 ! CHECK-DAG: fir.has_value %[[VAL_81]] : !fir.array<4xi32>
226 end subroutine
228 subroutine eqv_partial_overlaps_with_explicit_init()
229 use tinit
230 type(tseq), save :: somet
231 integer, save :: link(4)
232 integer :: i(2) = [5, 6]
233 integer :: j = 7
234 ! `somet` is only partially covered by explicit init, somet%j default
235 ! init value should be used in the equiv storage init to match nvfortran,
236 ! ifort, and nagfor behavior (gfortran refuses this code). 19.5.3.4 point
237 ! 10 specifies that explicit initialization overrides default initialization.
238 ! i(1)=5 | i(2)=6 | - | -
239 ! - | somet%i | somet%j |
240 ! - | - | - | j=7
241 equivalence (i, link(1))
242 equivalence (somet, link(2))
243 equivalence (j, link(4))
244 ! CHECK-LABEL: fir.global internal @_QFeqv_partial_overlaps_with_explicit_initEi : !fir.array<4xi32>
245 ! CHECK-DAG: %[[VAL_82:.*]] = arith.constant 5 : i32
246 ! CHECK-DAG: %[[VAL_83:.*]] = arith.constant 6 : i32
247 ! CHECK-DAG: %[[VAL_84:.*]] = arith.constant 3 : i32
248 ! CHECK-DAG: %[[VAL_85:.*]] = arith.constant 7 : i32
249 ! CHECK: %[[VAL_86:.*]] = fir.undefined !fir.array<4xi32>
250 ! CHECK: %[[VAL_87:.*]] = fir.insert_value %[[VAL_86]], %[[VAL_82]], [0 : index] : (!fir.array<4xi32>, i32) -> !fir.array<4xi32>
251 ! CHECK: %[[VAL_88:.*]] = fir.insert_value %[[VAL_87]], %[[VAL_83]], [1 : index] : (!fir.array<4xi32>, i32) -> !fir.array<4xi32>
252 ! CHECK: %[[VAL_89:.*]] = fir.insert_value %[[VAL_88]], %[[VAL_84]], [2 : index] : (!fir.array<4xi32>, i32) -> !fir.array<4xi32>
253 ! CHECK: %[[VAL_90:.*]] = fir.insert_value %[[VAL_89]], %[[VAL_85]], [3 : index] : (!fir.array<4xi32>, i32) -> !fir.array<4xi32>
254 ! CHECK: fir.has_value %[[VAL_90]] : !fir.array<4xi32>
255 end subroutine