xref: /llvm-project/flang/test/Fir/undo-complex-pattern.fir (revision c4204c0b29a6721267b1bcbaeedd7b1118e42396)
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<complex<f64>>, %z1 : complex<f64>, %z2 : complex<f64>) {
7  %c0 = arith.constant 0 : index
8  %c1 = arith.constant 1 : index
9  %real1 = fir.extract_value %z1, [0 : index] : (complex<f64>) -> f64
10  %imag1 = fir.extract_value %z1, [1 : index] : (complex<f64>) -> f64
11  %real2 = fir.extract_value %z2, [0 : index] : (complex<f64>) -> f64
12  %imag2 = fir.extract_value %z2, [1 : index] : (complex<f64>) -> 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 complex<f64>
18  %insert_real = fir.insert_value %undef, %real, [0 : index] : (complex<f64>, f64) -> complex<f64>
19  %insert_imag = fir.insert_value %insert_real, %imag, [1 : index] : (complex<f64>, f64) -> complex<f64>
20  fir.store %insert_imag to %z : !fir.ref<complex<f64>>
21  return
22}
23
24// CHECK-LABEL: @sub
25func.func @sub(%z: !fir.ref<complex<f64>>, %z1 : complex<f64>, %z2 : complex<f64>) {
26  %c0 = arith.constant 0 : index
27  %c1 = arith.constant 1 : index
28  %real1 = fir.extract_value %z1, [0 : index] : (complex<f64>) -> f64
29  %imag1 = fir.extract_value %z1, [1 : index] : (complex<f64>) -> f64
30  %real2 = fir.extract_value %z2, [0 : index] : (complex<f64>) -> f64
31  %imag2 = fir.extract_value %z2, [1 : index] : (complex<f64>) -> 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 complex<f64>
37  %insert_real = fir.insert_value %undef, %real, [0 : index] : (complex<f64>, f64) -> complex<f64>
38  %insert_imag = fir.insert_value %insert_real, %imag, [1 : index] : (complex<f64>, f64) -> complex<f64>
39  fir.store %insert_imag to %z : !fir.ref<complex<f64>>
40  return
41}
42
43// CHECK-LABEL: @undefOpHiddenByBranch
44func.func @undefOpHiddenByBranch(%z: !fir.ref<complex<f64>>, %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 complex<f64>
50  %z1l = fir.call @bar1() : () -> complex<f64>
51  %z1r = fir.call @bar1() : () -> complex<f64>
52  cf.br ^bb3(%u1, %z1l, %z1r : complex<f64>, complex<f64>, complex<f64>)
53^bb2:  // pred: ^bb0
54  %u2 = fir.undefined complex<f64>
55  %z2l = fir.call @bar2() : () -> complex<f64>
56  %z2r = fir.call @bar2() : () -> complex<f64>
57  cf.br ^bb3(%u2, %z2l, %z2r : complex<f64>, complex<f64>, complex<f64>)
58
59// CHECK: ^bb3(%[[z1:.*]]: complex<f64>, %[[z2:.*]]: complex<f64>):  // 2 preds: ^bb1, ^bb2
60// CHECK:  fir.addc %[[z1]], %[[z2]] : complex<f64>
61
62^bb3(%undef : complex<f64>, %z1 : complex<f64>, %z2 : complex<f64>):  // 2 preds: ^bb1, ^bb2
63  %real1 = fir.extract_value %z1, [0 : index] : (complex<f64>) -> f64
64  %imag1 = fir.extract_value %z1, [1 : index] : (complex<f64>) -> f64
65  %real2 = fir.extract_value %z2, [0 : index] : (complex<f64>) -> f64
66  %imag2 = fir.extract_value %z2, [1 : index] : (complex<f64>) -> 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] : (complex<f64>, f64) -> complex<f64>
70  %insert_imag = fir.insert_value %insert_real, %imag, [1 : index] : (complex<f64>, f64) -> complex<f64>
71  fir.store %insert_imag to %z : !fir.ref<complex<f64>>
72  return
73}
74func.func private @bar1() -> complex<f64>
75func.func private @bar2() -> complex<f64>
76
77// CHECK-LABEL: @close_but_bad_pattern
78func.func @close_but_bad_pattern(%z: !fir.ref<complex<f64>>, %z1 : complex<f64>, %z2 : complex<f64>) {
79  %c0 = arith.constant 0 : index
80  %c1 = arith.constant 1 : index
81  %real1 = fir.extract_value %z1, [0 : index] : (complex<f64>) -> f64
82  // extracting %c0 instead of %c1
83  %imag1 = fir.extract_value %z1, [0 : index] : (complex<f64>) -> f64
84  %real2 = fir.extract_value %z2, [0 : index] : (complex<f64>) -> f64
85  %imag2 = fir.extract_value %z2, [1 : index] : (complex<f64>) -> 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 complex<f64>
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] : (complex<f64>, f64) -> complex<f64>
94  %insert_imag = fir.insert_value %insert_real, %imag, [1 : index] : (complex<f64>, f64) -> complex<f64>
95  // CHECK: fir.store %[[insert2]] to {{.*}}
96  fir.store %insert_imag to %z : !fir.ref<complex<f64>>
97  return
98}
99