Break circular dependency between FIR dialect and utilities
[llvm-project.git] / flang / test / Lower / Intrinsics / merge.f90
blobcfd224ef0a2f79126e7a02b23591d3252474e267
1 ! RUN: bbc -emit-fir %s -o - | FileCheck %s
2 ! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck %s
4 ! CHECK-LABEL: func @_QPmerge_test(
5 ! CHECK-SAME: %[[arg0:.*]]: !fir.ref<!fir.char<1>>{{.*}}, %[[arg1:.*]]: index{{.*}}, %[[arg2:[^:]+]]: !fir.boxchar<1>{{.*}}, %[[arg3:[^:]+]]: !fir.boxchar<1>{{.*}}, %[[arg4:.*]]: !fir.ref<!fir.logical<4>>{{.*}}) -> !fir.boxchar<1> {
6 function merge_test(o1, o2, mask)
7 character :: o1, o2, merge_test
8 logical :: mask
9 merge_test = merge(o1, o2, mask)
10 ! CHECK: %[[a0:.*]]:2 = fir.unboxchar %[[arg2]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
11 ! CHECK-DAG: %[[a1:.*]]:2 = fir.unboxchar %[[arg3]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
12 ! CHECK: %[[a2:.*]] = fir.load %[[arg4]] : !fir.ref<!fir.logical<4>>
13 ! CHECK: %[[a3:.*]] = fir.convert %[[a2]] : (!fir.logical<4>) -> i1
14 ! CHECK: %[[a4:.*]] = arith.select %[[a3]], %[[a0]]#0, %[[a1]]#0 : !fir.ref<!fir.char<1,?>>
15 ! CHECK-DAG: %{{.*}} = fir.convert %[[a4]] : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<i8>
16 end
18 ! CHECK-LABEL: func @_QPmerge_test2(
19 ! CHECK-SAME: %[[arg0:[^:]+]]: !fir.ref<i32>{{.*}}, %[[arg1:[^:]+]]: !fir.ref<i32>{{.*}}, %[[arg2:.*]]: !fir.ref<!fir.logical<4>>{{.*}}) -> i32 {
20 function merge_test2(o1, o2, mask)
21 integer :: o1, o2, merge_test2
22 logical :: mask
23 merge_test2 = merge(o1, o2, mask)
24 ! CHECK: %[[a1:.*]] = fir.load %[[arg0]] : !fir.ref<i32>
25 ! CHECK: %[[a2:.*]] = fir.load %[[arg1]] : !fir.ref<i32>
26 ! CHECK: %[[a3:.*]] = fir.load %[[arg2]] : !fir.ref<!fir.logical<4>>
27 ! CHECK: %[[a4:.*]] = fir.convert %[[a3]] : (!fir.logical<4>) -> i1
28 ! CHECK: %{{.*}} = arith.select %[[a4]], %[[a1]], %[[a2]] : i32
29 end
31 ! CHECK-LABEL: func @_QPmerge_test3(
32 ! CHECK-SAME: %[[arg0:[^:]+]]: !fir.ref<!fir.array<10x!fir.type<_QFmerge_test3Tt{i:i32}>>>{{.*}}, %[[arg1:[^:]+]]: !fir.ref<!fir.type<_QFmerge_test3Tt{i:i32}>>{{.*}}, %[[arg2:[^:]+]]: !fir.ref<!fir.type<_QFmerge_test3Tt{i:i32}>>{{.*}}, %[[arg3:.*]]: !fir.ref<!fir.logical<4>>{{.*}}) {
33 subroutine merge_test3(result, o1, o2, mask)
34 type t
35 integer :: i
36 end type
37 type(t) :: result(10), o1, o2
38 logical :: mask
39 result = merge(o1, o2, mask)
40 ! CHECK: %[[mask:.*]] = fir.load %[[arg3]] : !fir.ref<!fir.logical<4>>
41 ! CHECK: %[[mask_cast:.*]] = fir.convert %[[mask]] : (!fir.logical<4>) -> i1
42 ! CHECK: = arith.select %[[mask_cast]], %[[arg1]], %[[arg2]] : !fir.ref<!fir.type<_QFmerge_test3Tt{i:i32}>>
43 end
45 ! CHECK-LABEL: func @_QPmerge_logical_var_and_expr(
46 subroutine merge_logical_var_and_expr(l1, l2)
47 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<!fir.logical<4>> {fir.bindc_name = "l1"},
48 ! CHECK-SAME: %[[VAL_1:.*]]: !fir.ref<!fir.logical<4>> {fir.bindc_name = "l2"}) {
49 logical :: l1, l2
50 call bar(merge(l1, .true., l2))
51 ! CHECK: %[[VAL_2:.*]] = fir.alloca !fir.logical<4>
52 ! CHECK: %[[VAL_3:.*]] = fir.load %[[VAL_0]] : !fir.ref<!fir.logical<4>>
53 ! CHECK: %[[VAL_4:.*]] = arith.constant true
54 ! CHECK: %[[VAL_5:.*]] = fir.load %[[VAL_1]] : !fir.ref<!fir.logical<4>>
55 ! CHECK: %[[VAL_6:.*]] = fir.convert %[[VAL_5]] : (!fir.logical<4>) -> i1
56 ! CHECK: %[[VAL_7:.*]] = fir.convert %[[VAL_4]] : (i1) -> !fir.logical<4>
57 ! CHECK: %[[VAL_8:.*]] = arith.select %[[VAL_6]], %[[VAL_3]], %[[VAL_7]] : !fir.logical<4>
58 ! CHECK: fir.store %[[VAL_8]] to %[[VAL_2]] : !fir.ref<!fir.logical<4>>
59 ! CHECK: fir.call @_QPbar(%[[VAL_2]]) {{.*}}: (!fir.ref<!fir.logical<4>>) -> ()
60 end subroutine
62 ! CHECK-LABEL: func @_QPmerge_cst_and_dyn_char(
63 subroutine merge_cst_and_dyn_char(dyn, l)
64 ! CHECK-SAME: %[[VAL_0:.*]]: !fir.boxchar<1> {fir.bindc_name = "dyn"},
65 ! CHECK-SAME: %[[VAL_1:.*]]: !fir.ref<!fir.logical<4>> {fir.bindc_name = "l"}) {
66 character(4) :: cst = "abcde"
67 character(*) :: dyn
68 logical :: l
69 print *, merge(cst, dyn, l)
70 ! CHECK: %[[VAL_2:.*]] = fir.address_of(@_QFmerge_cst_and_dyn_charEcst) : !fir.ref<!fir.char<1,4>>
71 ! CHECK: %[[VAL_3:.*]] = arith.constant 4 : index
72 ! CHECK: %[[VAL_4:.*]]:2 = fir.unboxchar %[[VAL_0]] : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
73 ! CHECK: %[[VAL_10:.*]] = fir.load %[[VAL_1]] : !fir.ref<!fir.logical<4>>
74 ! CHECK: %[[VAL_11:.*]] = fir.convert %[[VAL_10]] : (!fir.logical<4>) -> i1
75 ! CHECK: %[[VAL_12:.*]] = fir.convert %[[VAL_4]]#0 : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<!fir.char<1,4>>
76 ! CHECK: %[[VAL_13:.*]] = arith.select %[[VAL_11]], %[[VAL_2]], %[[VAL_12]] : !fir.ref<!fir.char<1,4>>
77 ! CHECK: %[[VAL_14:.*]] = fir.convert %[[VAL_13]] : (!fir.ref<!fir.char<1,4>>) -> !fir.ref<i8>
78 ! CHECK: %[[VAL_15:.*]] = fir.convert %[[VAL_3]] : (index) -> i64
79 ! CHECK: fir.call @_FortranAioOutputAscii(%{{.*}}, %[[VAL_14]], %[[VAL_15]]) {{.*}}: (!fir.ref<i8>, !fir.ref<i8>, i64) -> i1
80 end subroutine