1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=nary-reassociate -S | FileCheck %s
3 ; RUN: opt < %s -passes='nary-reassociate' -S | FileCheck %s
5 declare i32 @llvm.smax.i32(i32 %a, i32 %b)
6 declare i64 @llvm.umin.i64(i64, i64)
8 ; This is a negative test. We should not optimize if intermediate result
9 ; has a use outside of optimizable pattern. In other words %smax2 has one
10 ; use from %smax3 and side use from %res2.
11 define i32 @smax_test1(i32 %a, i32 %b, i32 %c) {
12 ; CHECK-LABEL: @smax_test1(
13 ; CHECK-NEXT: [[C1:%.*]] = icmp sgt i32 [[A:%.*]], [[B:%.*]]
14 ; CHECK-NEXT: [[SMAX1:%.*]] = select i1 [[C1]], i32 [[A]], i32 [[B]]
15 ; CHECK-NEXT: [[C2:%.*]] = icmp sgt i32 [[B]], [[C:%.*]]
16 ; CHECK-NEXT: [[SMAX2:%.*]] = select i1 [[C2]], i32 [[B]], i32 [[C]]
17 ; CHECK-NEXT: [[C3:%.*]] = icmp sgt i32 [[SMAX2]], [[A]]
18 ; CHECK-NEXT: [[SMAX3:%.*]] = select i1 [[C3]], i32 [[SMAX2]], i32 [[A]]
19 ; CHECK-NEXT: [[RES:%.*]] = add i32 [[SMAX1]], [[SMAX3]]
20 ; CHECK-NEXT: [[RES2:%.*]] = add i32 [[RES]], [[SMAX2]]
21 ; CHECK-NEXT: ret i32 [[RES]]
23 %c1 = icmp sgt i32 %a, %b
24 %smax1 = select i1 %c1, i32 %a, i32 %b
25 %c2 = icmp sgt i32 %b, %c
26 %smax2 = select i1 %c2, i32 %b, i32 %c
27 %c3 = icmp sgt i32 %smax2, %a
28 %smax3 = select i1 %c3, i32 %smax2, i32 %a
29 %res = add i32 %smax1, %smax3
30 %res2 = add i32 %res, %smax2
34 ; This is a negative test. It similar to the previous one
35 ; but a bit more complex. In particular after first iteration
36 ; e10 is replaced with %e10.nary = call i64 @llvm.umin.i64(i64 %e5, i64 %e).
37 ; No more reassociation should be applied to %e10.nary since
38 ; %e5 has side use in %e6.
39 define void @test2(i64 %arg) {
40 ; CHECK-LABEL: @test2(
42 ; CHECK-NEXT: [[E:%.*]] = sub i64 undef, 0
43 ; CHECK-NEXT: [[E1:%.*]] = sub i64 [[ARG:%.*]], 0
44 ; CHECK-NEXT: [[E2:%.*]] = call i64 @llvm.umin.i64(i64 [[E]], i64 [[E1]])
45 ; CHECK-NEXT: [[E3:%.*]] = call i64 @llvm.umin.i64(i64 [[E2]], i64 16384)
46 ; CHECK-NEXT: [[E4:%.*]] = sub i64 [[ARG]], 0
47 ; CHECK-NEXT: [[E5:%.*]] = call i64 @llvm.umin.i64(i64 [[E4]], i64 16384)
48 ; CHECK-NEXT: [[E6:%.*]] = icmp ugt i64 [[E5]], 0
49 ; CHECK-NEXT: [[E7:%.*]] = sub i64 undef, 0
50 ; CHECK-NEXT: [[E10_NARY:%.*]] = call i64 @llvm.umin.i64(i64 [[E5]], i64 [[E7]])
51 ; CHECK-NEXT: unreachable
56 %e2 = call i64 @llvm.umin.i64(i64 %e, i64 %e1)
57 %e3 = call i64 @llvm.umin.i64(i64 %e2, i64 16384)
59 %e5 = call i64 @llvm.umin.i64(i64 %e4, i64 16384)
60 %e6 = icmp ugt i64 %e5, 0
61 %e7 = sub i64 undef, 0
63 %e9 = call i64 @llvm.umin.i64(i64 %e7, i64 %e8)
64 %e10 = call i64 @llvm.umin.i64(i64 %e9, i64 16384)
68 ; Make sure we don't fall into infinte loop optimizing %sel5.
69 ; The subtle thing is that %sel3 is min/max as well and
70 ; there is "unexpected" reassociation coming from SCEV Expander
71 ; during %sel5 rewrite. That results in a new chain of min/max
72 ; which is matched on the next iteration.
73 define i32 @nary_infinite_loop_minmax(i32 %d0, i32 %d1, i32 %d2, i32 %d3) {
74 ; CHECK-LABEL: @nary_infinite_loop_minmax(
75 ; CHECK-NEXT: [[CMP0:%.*]] = icmp slt i32 [[D2:%.*]], [[D1:%.*]]
76 ; CHECK-NEXT: [[SEL0:%.*]] = select i1 [[CMP0]], i32 [[D1]], i32 [[D2]]
77 ; CHECK-NEXT: [[CMP1:%.*]] = icmp slt i32 [[D3:%.*]], [[D0:%.*]]
78 ; CHECK-NEXT: [[SEL1:%.*]] = select i1 [[CMP1]], i32 [[D0]], i32 [[D3]]
79 ; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i32 [[SEL1]], [[SEL0]]
80 ; CHECK-NEXT: [[SEL2:%.*]] = select i1 [[CMP2]], i32 [[SEL1]], i32 [[SEL0]]
81 ; CHECK-NEXT: [[CMP3:%.*]] = icmp slt i32 [[D3]], [[D0]]
82 ; CHECK-NEXT: [[SEL3:%.*]] = select i1 [[CMP3]], i32 [[D0]], i32 [[D3]]
83 ; CHECK-NEXT: [[SEL5_NARY:%.*]] = call i32 @llvm.smax.i32(i32 [[SEL0]], i32 [[SEL3]])
84 ; CHECK-NEXT: ret i32 [[SEL5_NARY]]
86 %cmp0 = icmp slt i32 %d2, %d1
87 %sel0 = select i1 %cmp0, i32 %d1, i32 %d2
89 %cmp1 = icmp slt i32 %d3, %d0
90 %sel1 = select i1 %cmp1, i32 %d0, i32 %d3
92 %cmp2 = icmp slt i32 %sel1, %sel0
93 %sel2 = select i1 %cmp2, i32 %sel1, i32 %sel0
95 %cmp3 = icmp slt i32 %d3, %d0
96 %sel3 = select i1 %cmp3, i32 %d0, i32 %d3
98 %cmp4 = icmp slt i32 %sel3, %d2
99 %sel4 = select i1 %cmp4, i32 %d2, i32 %sel3
101 %cmp5 = icmp slt i32 %sel4, %d1
102 %sel5 = select i1 %cmp5, i32 %d1, i32 %sel4