1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -nary-reassociate -S | FileCheck %s
3 ; RUN: opt < %s -passes='nary-reassociate' -S | FileCheck %s
5 declare i32 @llvm.umax.i32(i32 %a, i32 %b)
7 ; m1 = umax(a,b) ; has side uses
8 ; m2 = umax(umax((b,c), a) -> m2 = umax(m1, c)
9 define i32 @umax_test1(i32 %a, i32 %b, i32 %c) {
10 ; CHECK-LABEL: @umax_test1(
11 ; CHECK-NEXT: [[C1:%.*]] = icmp ugt i32 [[A:%.*]], [[B:%.*]]
12 ; CHECK-NEXT: [[UMAX1:%.*]] = select i1 [[C1]], i32 [[A]], i32 [[B]]
13 ; CHECK-NEXT: [[UMAX3_NARY:%.*]] = call i32 @llvm.umax.i32(i32 [[UMAX1]], i32 [[C:%.*]])
14 ; CHECK-NEXT: [[RES:%.*]] = add i32 [[UMAX1]], [[UMAX3_NARY]]
15 ; CHECK-NEXT: ret i32 [[RES]]
17 %c1 = icmp ugt i32 %a, %b
18 %umax1 = select i1 %c1, i32 %a, i32 %b
19 %c2 = icmp ugt i32 %b, %c
20 %umax2 = select i1 %c2, i32 %b, i32 %c
21 %c3 = icmp ugt i32 %umax2, %a
22 %umax3 = select i1 %c3, i32 %umax2, i32 %a
23 %res = add i32 %umax1, %umax3
27 ; m1 = umax(a,b) ; has side uses
28 ; m2 = umax(b, (umax(a, c))) -> m2 = umax(m1, c)
29 define i32 @umax_test2(i32 %a, i32 %b, i32 %c) {
30 ; CHECK-LABEL: @umax_test2(
31 ; CHECK-NEXT: [[C1:%.*]] = icmp ugt i32 [[A:%.*]], [[B:%.*]]
32 ; CHECK-NEXT: [[UMAX1:%.*]] = select i1 [[C1]], i32 [[A]], i32 [[B]]
33 ; CHECK-NEXT: [[UMAX3_NARY:%.*]] = call i32 @llvm.umax.i32(i32 [[UMAX1]], i32 [[C:%.*]])
34 ; CHECK-NEXT: [[RES:%.*]] = add i32 [[UMAX1]], [[UMAX3_NARY]]
35 ; CHECK-NEXT: ret i32 [[RES]]
37 %c1 = icmp ugt i32 %a, %b
38 %umax1 = select i1 %c1, i32 %a, i32 %b
39 %c2 = icmp ugt i32 %a, %c
40 %umax2 = select i1 %c2, i32 %a, i32 %c
41 %c3 = icmp ugt i32 %b, %umax2
42 %umax3 = select i1 %c3, i32 %b, i32 %umax2
43 %res = add i32 %umax1, %umax3
47 ; Same test as umax_test1 but uses @llvm.umax intrinsic
48 define i32 @umax_test3(i32 %a, i32 %b, i32 %c) {
49 ; CHECK-LABEL: @umax_test3(
50 ; CHECK-NEXT: [[UMAX1:%.*]] = call i32 @llvm.umax.i32(i32 [[A:%.*]], i32 [[B:%.*]])
51 ; CHECK-NEXT: [[UMAX3_NARY:%.*]] = call i32 @llvm.umax.i32(i32 [[UMAX1]], i32 [[C:%.*]])
52 ; CHECK-NEXT: [[RES:%.*]] = add i32 [[UMAX1]], [[UMAX3_NARY]]
53 ; CHECK-NEXT: ret i32 [[RES]]
55 %umax1 = call i32 @llvm.umax.i32(i32 %a, i32 %b)
56 %umax2 = call i32 @llvm.umax.i32(i32 %b, i32 %c)
57 %umax3 = call i32 @llvm.umax.i32(i32 %umax2, i32 %a)
58 %res = add i32 %umax1, %umax3
62 ; m1 = umax(a,b) ; has side uses
63 ; m2 = umax(umax_or_eq((b,c), a) -> m2 = umax(m1, c)
64 define i32 @umax_test4(i32 %a, i32 %b, i32 %c) {
65 ; CHECK-LABEL: @umax_test4(
66 ; CHECK-NEXT: [[C1:%.*]] = icmp ugt i32 [[A:%.*]], [[B:%.*]]
67 ; CHECK-NEXT: [[UMAX1:%.*]] = select i1 [[C1]], i32 [[A]], i32 [[B]]
68 ; CHECK-NEXT: [[UMAX3_NARY:%.*]] = call i32 @llvm.umax.i32(i32 [[UMAX1]], i32 [[C:%.*]])
69 ; CHECK-NEXT: [[RES:%.*]] = add i32 [[UMAX1]], [[UMAX3_NARY]]
70 ; CHECK-NEXT: ret i32 [[RES]]
72 %c1 = icmp ugt i32 %a, %b
73 %umax1 = select i1 %c1, i32 %a, i32 %b
74 %c2 = icmp uge i32 %b, %c
75 %umax_or_eq2 = select i1 %c2, i32 %b, i32 %c
76 %c3 = icmp ugt i32 %umax_or_eq2, %a
77 %umax3 = select i1 %c3, i32 %umax_or_eq2, i32 %a
78 %res = add i32 %umax1, %umax3
82 ; m1 = umax_or_eq(a,b) ; has side uses
83 ; m2 = umax_or_eq(umax((b,c), a) -> m2 = umax(m1, c)
84 define i32 @umax_test5(i32 %a, i32 %b, i32 %c) {
85 ; CHECK-LABEL: @umax_test5(
86 ; CHECK-NEXT: [[C1:%.*]] = icmp uge i32 [[A:%.*]], [[B:%.*]]
87 ; CHECK-NEXT: [[UMAX_OR_EQ1:%.*]] = select i1 [[C1]], i32 [[A]], i32 [[B]]
88 ; CHECK-NEXT: [[UMAX_OR_EQ3_NARY:%.*]] = call i32 @llvm.umax.i32(i32 [[UMAX_OR_EQ1]], i32 [[C:%.*]])
89 ; CHECK-NEXT: [[RES:%.*]] = add i32 [[UMAX_OR_EQ1]], [[UMAX_OR_EQ3_NARY]]
90 ; CHECK-NEXT: ret i32 [[RES]]
92 %c1 = icmp uge i32 %a, %b
93 %umax_or_eq1 = select i1 %c1, i32 %a, i32 %b
94 %c2 = icmp ugt i32 %b, %c
95 %umax2 = select i1 %c2, i32 %b, i32 %c
96 %c3 = icmp uge i32 %umax2, %a
97 %umax_or_eq3 = select i1 %c3, i32 %umax2, i32 %a
98 %res = add i32 %umax_or_eq1, %umax_or_eq3
102 ; m1 = umax(a,b) ; has side uses
103 ; m2 = umax(smax((b,c), a) ; check that signed and unsigned maxs are not mixed
104 define i32 @umax_test6(i32 %a, i32 %b, i32 %c) {
105 ; CHECK-LABEL: @umax_test6(
106 ; CHECK-NEXT: [[C1:%.*]] = icmp ugt i32 [[A:%.*]], [[B:%.*]]
107 ; CHECK-NEXT: [[UMAX1:%.*]] = select i1 [[C1]], i32 [[A]], i32 [[B]]
108 ; CHECK-NEXT: [[C2:%.*]] = icmp sgt i32 [[B]], [[C:%.*]]
109 ; CHECK-NEXT: [[SMAX2:%.*]] = select i1 [[C2]], i32 [[B]], i32 [[C]]
110 ; CHECK-NEXT: [[C3:%.*]] = icmp ugt i32 [[SMAX2]], [[A]]
111 ; CHECK-NEXT: [[UMAX3:%.*]] = select i1 [[C3]], i32 [[SMAX2]], i32 [[A]]
112 ; CHECK-NEXT: [[RES:%.*]] = add i32 [[UMAX1]], [[UMAX3]]
113 ; CHECK-NEXT: ret i32 [[RES]]
115 %c1 = icmp ugt i32 %a, %b
116 %umax1 = select i1 %c1, i32 %a, i32 %b
117 %c2 = icmp sgt i32 %b, %c
118 %smax2 = select i1 %c2, i32 %b, i32 %c
119 %c3 = icmp ugt i32 %smax2, %a
120 %umax3 = select i1 %c3, i32 %smax2, i32 %a
121 %res = add i32 %umax1, %umax3
125 ; m1 = umax(a,b) ; has side uses
126 ; m2 = umax(umin((b,c), a) ; check that max and min are not mixed
127 define i32 @umax_test7(i32 %a, i32 %b, i32 %c) {
128 ; CHECK-LABEL: @umax_test7(
129 ; CHECK-NEXT: [[C1:%.*]] = icmp ugt i32 [[A:%.*]], [[B:%.*]]
130 ; CHECK-NEXT: [[UMAX1:%.*]] = select i1 [[C1]], i32 [[A]], i32 [[B]]
131 ; CHECK-NEXT: [[C2:%.*]] = icmp ult i32 [[B]], [[C:%.*]]
132 ; CHECK-NEXT: [[UMAX2:%.*]] = select i1 [[C2]], i32 [[B]], i32 [[C]]
133 ; CHECK-NEXT: [[C3:%.*]] = icmp ugt i32 [[UMAX2]], [[A]]
134 ; CHECK-NEXT: [[UMAX3:%.*]] = select i1 [[C3]], i32 [[UMAX2]], i32 [[A]]
135 ; CHECK-NEXT: [[RES:%.*]] = add i32 [[UMAX1]], [[UMAX3]]
136 ; CHECK-NEXT: ret i32 [[RES]]
138 %c1 = icmp ugt i32 %a, %b
139 %umax1 = select i1 %c1, i32 %a, i32 %b
140 %c2 = icmp ult i32 %b, %c
141 %umax2 = select i1 %c2, i32 %b, i32 %c
142 %c3 = icmp ugt i32 %umax2, %a
143 %umax3 = select i1 %c3, i32 %umax2, i32 %a
144 %res = add i32 %umax1, %umax3
148 ; Pointer types are not supported yet
149 define i32* @umax_test8(i32* %a, i32* %b, i32* %c) {
150 ; CHECK-LABEL: @umax_test8(
151 ; CHECK-NEXT: [[C1:%.*]] = icmp ugt i32* [[A:%.*]], [[B:%.*]]
152 ; CHECK-NEXT: [[SMAX1:%.*]] = select i1 [[C1]], i32* [[A]], i32* [[B]]
153 ; CHECK-NEXT: [[C2:%.*]] = icmp ugt i32* [[B]], [[C:%.*]]
154 ; CHECK-NEXT: [[SMAX2:%.*]] = select i1 [[C2]], i32* [[B]], i32* [[C]]
155 ; CHECK-NEXT: [[C3:%.*]] = icmp ugt i32* [[SMAX2]], [[A]]
156 ; CHECK-NEXT: [[SMAX3:%.*]] = select i1 [[C3]], i32* [[SMAX2]], i32* [[A]]
157 ; CHECK-NEXT: [[C4:%.*]] = icmp ugt i32* [[SMAX1]], [[SMAX3]]
158 ; CHECK-NEXT: [[RES:%.*]] = select i1 [[C4]], i32* [[SMAX1]], i32* [[SMAX3]]
159 ; CHECK-NEXT: ret i32* [[RES]]
161 %c1 = icmp ugt i32* %a, %b
162 %smax1 = select i1 %c1, i32* %a, i32* %b
163 %c2 = icmp ugt i32* %b, %c
164 %smax2 = select i1 %c2, i32* %b, i32* %c
165 %c3 = icmp ugt i32* %smax2, %a
166 %smax3 = select i1 %c3, i32* %smax2, i32* %a
167 %c4 = icmp ugt i32* %smax1, %smax3
168 %res = select i1 %c4, i32* %smax1, i32* %smax3