1// Test regrouping of + and - operations on complex components into complex operations
2// RUN: fir-opt --canonicalize %s | FileCheck %s
3
4
5// CHECK-LABEL: @add
6func.func @add(%z: !fir.ref<!fir.complex<8>>, %z1 : !fir.complex<8>, %z2 : !fir.complex<8>) {
7  %c0 = arith.constant 0 : index
8  %c1 = arith.constant 1 : index
9  %real1 = fir.extract_value %z1, [0 : index] : (!fir.complex<8>) -> f64
10  %imag1 = fir.extract_value %z1, [1 : index] : (!fir.complex<8>) -> f64
11  %real2 = fir.extract_value %z2, [0 : index] : (!fir.complex<8>) -> f64
12  %imag2 = fir.extract_value %z2, [1 : index] : (!fir.complex<8>) -> f64
13
14  // CHECK-LABEL: fir.addc
15  %real = arith.addf %real1, %real2 : f64
16  %imag = arith.addf %imag1, %imag2 : f64
17  %undef = fir.undefined !fir.complex<8>
18  %insert_real = fir.insert_value %undef, %real, [0 : index] : (!fir.complex<8>, f64) -> !fir.complex<8>
19  %insert_imag = fir.insert_value %insert_real, %imag, [1 : index] : (!fir.complex<8>, f64) -> !fir.complex<8>
20  fir.store %insert_imag to %z : !fir.ref<!fir.complex<8>>
21  return
22}
23
24// CHECK-LABEL: @sub
25func.func @sub(%z: !fir.ref<!fir.complex<8>>, %z1 : !fir.complex<8>, %z2 : !fir.complex<8>) {
26  %c0 = arith.constant 0 : index
27  %c1 = arith.constant 1 : index
28  %real1 = fir.extract_value %z1, [0 : index] : (!fir.complex<8>) -> f64
29  %imag1 = fir.extract_value %z1, [1 : index] : (!fir.complex<8>) -> f64
30  %real2 = fir.extract_value %z2, [0 : index] : (!fir.complex<8>) -> f64
31  %imag2 = fir.extract_value %z2, [1 : index] : (!fir.complex<8>) -> f64
32
33  // CHECK-LABEL: fir.subc
34  %real = arith.subf %real1, %real2 : f64
35  %imag = arith.subf %imag1, %imag2 : f64
36  %undef = fir.undefined !fir.complex<8>
37  %insert_real = fir.insert_value %undef, %real, [0 : index] : (!fir.complex<8>, f64) -> !fir.complex<8>
38  %insert_imag = fir.insert_value %insert_real, %imag, [1 : index] : (!fir.complex<8>, f64) -> !fir.complex<8>
39  fir.store %insert_imag to %z : !fir.ref<!fir.complex<8>>
40  return
41}
42
43// CHECK-LABEL: @undefOpHiddenByBranch
44func.func @undefOpHiddenByBranch(%z: !fir.ref<!fir.complex<8>>, %b: i1) {
45  %c0 = arith.constant 0 : index
46  %c1 = arith.constant 1 : index
47  cf.cond_br %b, ^bb1, ^bb2
48^bb1:  // pred: ^bb0
49  %u1 = fir.undefined !fir.complex<8>
50  %z1l = fir.call @bar1() : () -> !fir.complex<8>
51  %z1r = fir.call @bar1() : () -> !fir.complex<8>
52  cf.br ^bb3(%u1, %z1l, %z1r : !fir.complex<8>, !fir.complex<8>, !fir.complex<8>)
53^bb2:  // pred: ^bb0
54  %u2 = fir.undefined !fir.complex<8>
55  %z2l = fir.call @bar2() : () -> !fir.complex<8>
56  %z2r = fir.call @bar2() : () -> !fir.complex<8>
57  cf.br ^bb3(%u2, %z2l, %z2r : !fir.complex<8>, !fir.complex<8>, !fir.complex<8>)
58
59// CHECK: ^bb3(%[[z1:.*]]: !fir.complex<8>, %[[z2:.*]]: !fir.complex<8>):  // 2 preds: ^bb1, ^bb2
60// CHECK:  fir.addc %[[z1]], %[[z2]] : !fir.complex<8>
61
62^bb3(%undef : !fir.complex<8>, %z1 : !fir.complex<8>, %z2 : !fir.complex<8>):  // 2 preds: ^bb1, ^bb2
63  %real1 = fir.extract_value %z1, [0 : index] : (!fir.complex<8>) -> f64
64  %imag1 = fir.extract_value %z1, [1 : index] : (!fir.complex<8>) -> f64
65  %real2 = fir.extract_value %z2, [0 : index] : (!fir.complex<8>) -> f64
66  %imag2 = fir.extract_value %z2, [1 : index] : (!fir.complex<8>) -> f64
67  %real = arith.addf %real1, %real2 : f64
68  %imag = arith.addf %imag1, %imag2 : f64
69  %insert_real = fir.insert_value %undef, %real, [0 : index] : (!fir.complex<8>, f64) -> !fir.complex<8>
70  %insert_imag = fir.insert_value %insert_real, %imag, [1 : index] : (!fir.complex<8>, f64) -> !fir.complex<8>
71  fir.store %insert_imag to %z : !fir.ref<!fir.complex<8>>
72  return
73}
74func.func private @bar1() -> !fir.complex<8>
75func.func private @bar2() -> !fir.complex<8>
76
77// CHECK-LABEL: @close_but_bad_pattern
78func.func @close_but_bad_pattern(%z: !fir.ref<!fir.complex<8>>, %z1 : !fir.complex<8>, %z2 : !fir.complex<8>) {
79  %c0 = arith.constant 0 : index
80  %c1 = arith.constant 1 : index
81  %real1 = fir.extract_value %z1, [0 : index] : (!fir.complex<8>) -> f64
82  // extracting %c0 instead of %c1
83  %imag1 = fir.extract_value %z1, [0 : index] : (!fir.complex<8>) -> f64
84  %real2 = fir.extract_value %z2, [0 : index] : (!fir.complex<8>) -> f64
85  %imag2 = fir.extract_value %z2, [1 : index] : (!fir.complex<8>) -> f64
86  // CHECK: arith.subf
87  // CHECK: subf
88  %real = arith.subf %real1, %real2 : f64
89  %imag = arith.subf %imag1, %imag2 : f64
90  %undef = fir.undefined !fir.complex<8>
91  // CHECK: %[[insert1:.*]] = fir.insert_value %{{.*}}, %{{.*}}, [0
92  // CHECK: %[[insert2:.*]] = fir.insert_value %[[insert1]], %{{.*}}, [1
93  %insert_real = fir.insert_value %undef, %real, [0 : index] : (!fir.complex<8>, f64) -> !fir.complex<8>
94  %insert_imag = fir.insert_value %insert_real, %imag, [1 : index] : (!fir.complex<8>, f64) -> !fir.complex<8>
95  // CHECK: fir.store %[[insert2]] to {{.*}}
96  fir.store %insert_imag to %z : !fir.ref<!fir.complex<8>>
97  return
98}
99