1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
4 ; TODO: https://alive2.llvm.org/ce/z/3ybZRl
5 define i32 @pr54735_slt(i32 %x, i32 %y) {
6 ; CHECK-LABEL: @pr54735_slt(
8 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[X:%.*]], [[Y:%.*]]
9 ; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[COND_END:%.*]]
11 ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[X]], [[Y]]
12 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[SUB]], 1
13 ; CHECK-NEXT: [[NEG:%.*]] = xor i32 [[SUB]], -1
14 ; CHECK-NEXT: [[ABSCOND:%.*]] = icmp sle i32 [[SUB]], -1
15 ; CHECK-NEXT: [[ABS:%.*]] = select i1 [[ABSCOND]], i32 [[NEG]], i32 [[ADD]]
16 ; CHECK-NEXT: ret i32 [[ABS]]
18 ; CHECK-NEXT: ret i32 0
21 %cmp = icmp slt i32 %x, %y ; x<y ? abs (x-y+1): 0
22 br i1 %cmp, label %cond.true, label %cond.end
24 cond.true: ; preds = %entry
25 %sub = sub nsw i32 %x, %y
26 %add = add nsw i32 %sub, 1
27 %neg = xor i32 %sub, -1 ; sub nsw i32 0, %add
28 %abscond = icmp sle i32 %sub, -1
29 %abs = select i1 %abscond, i32 %neg, i32 %add
32 cond.end: ; preds = %entry, %cond.true
36 ; https://alive2.llvm.org/ce/z/fTTsdT
37 define i32 @pr54735_sgt(i32 %x, i32 %y) {
38 ; CHECK-LABEL: @pr54735_sgt(
40 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X:%.*]], [[Y:%.*]]
41 ; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[COND_END:%.*]]
43 ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[X]], [[Y]]
44 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[SUB]], 1
45 ; CHECK-NEXT: ret i32 [[ADD]]
47 ; CHECK-NEXT: ret i32 0
50 %cmp = icmp sgt i32 %x, %y ; x>y ? abs (x-y+1): 0
51 br i1 %cmp, label %cond.true, label %cond.end
53 cond.true: ; preds = %entry
54 %sub = sub nsw i32 %x, %y
55 %add = add nsw i32 %sub, 1
56 %neg = xor i32 %sub, -1 ; sub nsw i32 0, %add
57 %abscond = icmp slt i32 %sub, -1
58 %abs = select i1 %abscond, i32 %neg, i32 %add
61 cond.end: ; preds = %entry, %cond.true
65 ; https://alive2.llvm.org/ce/z/k9v75c
66 define i32 @pr54735_sge(i32 %x, i32 %y) {
67 ; CHECK-LABEL: @pr54735_sge(
69 ; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[X:%.*]], [[Y:%.*]]
70 ; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[COND_END:%.*]]
72 ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[X]], [[Y]]
73 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[SUB]], 1
74 ; CHECK-NEXT: ret i32 [[ADD]]
76 ; CHECK-NEXT: ret i32 0
79 %cmp = icmp sge i32 %x, %y ; x>y ? abs (x-y+1): 0
80 br i1 %cmp, label %cond.true, label %cond.end
82 cond.true: ; preds = %entry
83 %sub = sub nsw i32 %x, %y
84 %add = add nsw i32 %sub, 1
85 %neg = xor i32 %sub, -1 ; sub nsw i32 0, %add
86 %abscond = icmp slt i32 %sub, -1
87 %abs = select i1 %abscond, i32 %neg, i32 %add
90 cond.end: ; preds = %entry, %cond.true
94 ; Negative test: https://alive2.llvm.org/ce/z/oZyu4M
95 define i8 @pr54735_without_nsw (i8 %x, i8 %y) {
96 ; CHECK-LABEL: @pr54735_without_nsw(
98 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[X:%.*]], [[Y:%.*]]
99 ; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[COND_END:%.*]]
101 ; CHECK-NEXT: [[SUB:%.*]] = sub i8 [[X]], [[Y]]
102 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[SUB]], 1
103 ; CHECK-NEXT: [[NEG:%.*]] = xor i8 [[SUB]], -1
104 ; CHECK-NEXT: [[ABSCOND:%.*]] = icmp slt i8 [[SUB]], -1
105 ; CHECK-NEXT: [[ABS:%.*]] = select i1 [[ABSCOND]], i8 [[NEG]], i8 [[ADD]]
106 ; CHECK-NEXT: ret i8 [[ABS]]
108 ; CHECK-NEXT: ret i8 0
111 %cmp = icmp sgt i8 %x, %y
112 br i1 %cmp, label %cond.true, label %cond.end
114 cond.true: ; preds = %entry
116 %add = add i8 %sub, 1
117 %neg = xor i8 %sub, -1
118 %abscond = icmp slt i8 %sub, -1
119 %abs = select i1 %abscond, i8 %neg, i8 %add
122 cond.end: ; preds = %entry, %cond.true
126 define i32 @pr54735_sle(i32 %x, i32 %y) {
127 ; CHECK-LABEL: @pr54735_sle(
129 ; CHECK-NEXT: [[CMP:%.*]] = icmp sle i32 [[X:%.*]], [[Y:%.*]]
130 ; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[COND_END:%.*]]
132 ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[X]], [[Y]]
133 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[SUB]], 1
134 ; CHECK-NEXT: [[NEG:%.*]] = xor i32 [[SUB]], -1
135 ; CHECK-NEXT: [[ABSCOND:%.*]] = icmp slt i32 [[SUB]], -1
136 ; CHECK-NEXT: [[ABS:%.*]] = select i1 [[ABSCOND]], i32 [[NEG]], i32 [[ADD]]
137 ; CHECK-NEXT: ret i32 [[ABS]]
139 ; CHECK-NEXT: ret i32 0
142 %cmp = icmp sle i32 %x, %y ; x<=y ? abs (x-y+1): 0
143 br i1 %cmp, label %cond.true, label %cond.end
145 cond.true: ; preds = %entry
146 %sub = sub nsw i32 %x, %y
147 %add = add nsw i32 %sub, 1
148 %neg = xor i32 %sub, -1 ; sub nsw i32 0, %add
149 %abscond = icmp slt i32 %sub, -1
150 %abs = select i1 %abscond, i32 %neg, i32 %add
153 cond.end: ; preds = %entry, %cond.true
157 ; https://alive2.llvm.org/ce/z/pp9zJi
158 define i32 @pr54735_slt_neg(i32 %x, i32 %y) {
159 ; CHECK-LABEL: @pr54735_slt_neg(
161 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[X:%.*]], [[Y:%.*]]
162 ; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[COND_END:%.*]]
164 ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[X]], [[Y]]
165 ; CHECK-NEXT: [[NEG:%.*]] = xor i32 [[SUB]], 12
166 ; CHECK-NEXT: ret i32 [[NEG]]
168 ; CHECK-NEXT: ret i32 0
171 %cmp = icmp slt i32 %x, %y ; x<y ? abs (x-y-12): 0
172 br i1 %cmp, label %cond.true, label %cond.end
174 cond.true: ; preds = %entry
175 %sub = sub nsw i32 %x, %y
176 %add = add nsw i32 %sub, -12 ; %sub - 12
177 %neg = xor i32 %sub, 12 ; 12 - %sub
178 %abscond = icmp sle i32 %sub, 12
179 %abs = select i1 %abscond, i32 %neg, i32 %add
182 cond.end: ; preds = %entry, %cond.true
186 ; https://alive2.llvm.org/ce/z/9P6grR
187 define i32 @pr54735_sle_neg(i32 %x, i32 %y) {
188 ; CHECK-LABEL: @pr54735_sle_neg(
190 ; CHECK-NEXT: [[CMP:%.*]] = icmp sle i32 [[X:%.*]], [[Y:%.*]]
191 ; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[COND_END:%.*]]
193 ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[X]], [[Y]]
194 ; CHECK-NEXT: [[NEG:%.*]] = xor i32 [[SUB]], 12
195 ; CHECK-NEXT: ret i32 [[NEG]]
197 ; CHECK-NEXT: ret i32 0
200 %cmp = icmp sle i32 %x, %y ; x<=y ? abs (x-y-12): 0
201 br i1 %cmp, label %cond.true, label %cond.end
203 cond.true: ; preds = %entry
204 %sub = sub nsw i32 %x, %y
205 %add = add nsw i32 %sub, -12
206 %neg = xor i32 %sub, 12 ; %sub - 12
207 %abscond = icmp sle i32 %sub, 12
208 %abs = select i1 %abscond, i32 %neg, i32 %add
211 cond.end: ; preds = %entry, %cond.true
215 ; Negative test: https://alive2.llvm.org/ce/z/Yqv4x2
216 define i8 @pr54735_unexpect_const (i8 %x, i8 %y) {
217 ; CHECK-LABEL: @pr54735_unexpect_const(
219 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[X:%.*]], [[Y:%.*]]
220 ; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[COND_END:%.*]]
222 ; CHECK-NEXT: [[SUB:%.*]] = sub i8 [[X]], [[Y]]
223 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[SUB]], 2
224 ; CHECK-NEXT: [[NEG:%.*]] = xor i8 [[SUB]], -1
225 ; CHECK-NEXT: [[ABSCOND:%.*]] = icmp slt i8 [[SUB]], -2
226 ; CHECK-NEXT: [[ABS:%.*]] = select i1 [[ABSCOND]], i8 [[NEG]], i8 [[ADD]]
227 ; CHECK-NEXT: ret i8 [[ABS]]
229 ; CHECK-NEXT: ret i8 0
232 %cmp = icmp sgt i8 %x, %y ; x>y ? abs (x-y+2): 0
233 br i1 %cmp, label %cond.true, label %cond.end
235 cond.true: ; preds = %entry
237 %add = add i8 %sub, 2 ; x-y+2
238 %neg = xor i8 %sub, -1 ; y-x-1
239 %neg1 = sub i8 %neg, 1 ; y-x-2
240 %abscond = icmp slt i8 %sub, -2
241 %abs = select i1 %abscond, i8 %neg, i8 %add
244 cond.end: ; preds = %entry, %cond.true