[InstCombine] Signed saturation patterns
[llvm-core.git] / test / Transforms / InstCombine / unsigned-sub-overflow-check.ll
blob124150cacf03ef7d91123597a8eab250d9030bbe
1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt %s -instcombine -S | FileCheck %s
4 ; Fold
5 ;   (%x - %y) u> %x
6 ; to
7 ;   @llvm.sub.with.overflow(%x, %y) + extractvalue
9 define i1 @t0_basic(i8 %x, i8 %y) {
10 ; CHECK-LABEL: @t0_basic(
11 ; CHECK-NEXT:    [[R:%.*]] = icmp ugt i8 [[Y:%.*]], [[X:%.*]]
12 ; CHECK-NEXT:    ret i1 [[R]]
14   %t0 = sub i8 %x, %y
15   %r = icmp ugt i8 %t0, %x
16   ret i1 %r
19 define <2 x i1> @t1_vec(<2 x i8> %x, <2 x i8> %y) {
20 ; CHECK-LABEL: @t1_vec(
21 ; CHECK-NEXT:    [[R:%.*]] = icmp ugt <2 x i8> [[Y:%.*]], [[X:%.*]]
22 ; CHECK-NEXT:    ret <2 x i1> [[R]]
24   %t0 = sub <2 x i8> %x, %y
25   %r = icmp ugt <2 x i8> %t0, %x
26   ret <2 x i1> %r
29 ; Commutativity
31 define i1 @t2_commutative(i8 %x, i8 %y) {
32 ; CHECK-LABEL: @t2_commutative(
33 ; CHECK-NEXT:    [[R:%.*]] = icmp ugt i8 [[Y:%.*]], [[X:%.*]]
34 ; CHECK-NEXT:    ret i1 [[R]]
36   %t0 = sub i8 %x, %y
37   %r = icmp ult i8 %x, %t0 ; swapped
38   ret i1 %r
41 ; Extra-use tests
43 declare void @use8(i8)
45 define i1 @t3_extrause0(i8 %x, i8 %y) {
46 ; CHECK-LABEL: @t3_extrause0(
47 ; CHECK-NEXT:    [[T0:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
48 ; CHECK-NEXT:    call void @use8(i8 [[T0]])
49 ; CHECK-NEXT:    [[R:%.*]] = icmp ult i8 [[X]], [[Y]]
50 ; CHECK-NEXT:    ret i1 [[R]]
52   %t0 = sub i8 %x, %y
53   call void @use8(i8 %t0)
54   %r = icmp ugt i8 %t0, %x
55   ret i1 %r
58 ; Negative tests
60 define i1 @n4_not_commutative(i8 %x, i8 %y) {
61 ; CHECK-LABEL: @n4_not_commutative(
62 ; CHECK-NEXT:    [[T0:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
63 ; CHECK-NEXT:    [[R:%.*]] = icmp ugt i8 [[T0]], [[Y]]
64 ; CHECK-NEXT:    ret i1 [[R]]
66   %t0 = sub i8 %x, %y
67   %r = icmp ugt i8 %t0, %y ; can't check against %y.
68   ret i1 %r
71 define i1 @n5_wrong_pred0(i8 %x, i8 %y) {
72 ; CHECK-LABEL: @n5_wrong_pred0(
73 ; CHECK-NEXT:    [[T0:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
74 ; CHECK-NEXT:    [[R:%.*]] = icmp uge i8 [[T0]], [[X]]
75 ; CHECK-NEXT:    ret i1 [[R]]
77   %t0 = sub i8 %x, %y
78   %r = icmp uge i8 %t0, %x
79   ret i1 %r
82 define i1 @n6_wrong_pred1(i8 %x, i8 %y) {
83 ; CHECK-LABEL: @n6_wrong_pred1(
84 ; CHECK-NEXT:    [[T0:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
85 ; CHECK-NEXT:    [[R:%.*]] = icmp ult i8 [[T0]], [[X]]
86 ; CHECK-NEXT:    ret i1 [[R]]
88   %t0 = sub i8 %x, %y
89   %r = icmp ult i8 %t0, %x
90   ret i1 %r
93 define i1 @n7_wrong_pred2(i8 %x, i8 %y) {
94 ; CHECK-LABEL: @n7_wrong_pred2(
95 ; CHECK-NEXT:    [[R:%.*]] = icmp eq i8 [[Y:%.*]], 0
96 ; CHECK-NEXT:    ret i1 [[R]]
98   %t0 = sub i8 %x, %y
99   %r = icmp eq i8 %t0, %x
100   ret i1 %r
103 define i1 @n8_wrong_pred3(i8 %x, i8 %y) {
104 ; CHECK-LABEL: @n8_wrong_pred3(
105 ; CHECK-NEXT:    [[R:%.*]] = icmp ne i8 [[Y:%.*]], 0
106 ; CHECK-NEXT:    ret i1 [[R]]
108   %t0 = sub i8 %x, %y
109   %r = icmp ne i8 %t0, %x
110   ret i1 %r
113 define i1 @n9_wrong_pred4(i8 %x, i8 %y) {
114 ; CHECK-LABEL: @n9_wrong_pred4(
115 ; CHECK-NEXT:    [[T0:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
116 ; CHECK-NEXT:    [[R:%.*]] = icmp slt i8 [[T0]], [[X]]
117 ; CHECK-NEXT:    ret i1 [[R]]
119   %t0 = sub i8 %x, %y
120   %r = icmp slt i8 %t0, %x
121   ret i1 %r
124 define i1 @n10_wrong_pred5(i8 %x, i8 %y) {
125 ; CHECK-LABEL: @n10_wrong_pred5(
126 ; CHECK-NEXT:    [[T0:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
127 ; CHECK-NEXT:    [[R:%.*]] = icmp sle i8 [[T0]], [[X]]
128 ; CHECK-NEXT:    ret i1 [[R]]
130   %t0 = sub i8 %x, %y
131   %r = icmp sle i8 %t0, %x
132   ret i1 %r
135 define i1 @n11_wrong_pred6(i8 %x, i8 %y) {
136 ; CHECK-LABEL: @n11_wrong_pred6(
137 ; CHECK-NEXT:    [[T0:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
138 ; CHECK-NEXT:    [[R:%.*]] = icmp sgt i8 [[T0]], [[X]]
139 ; CHECK-NEXT:    ret i1 [[R]]
141   %t0 = sub i8 %x, %y
142   %r = icmp sgt i8 %t0, %x
143   ret i1 %r
146 define i1 @n12_wrong_pred7(i8 %x, i8 %y) {
147 ; CHECK-LABEL: @n12_wrong_pred7(
148 ; CHECK-NEXT:    [[T0:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
149 ; CHECK-NEXT:    [[R:%.*]] = icmp sge i8 [[T0]], [[X]]
150 ; CHECK-NEXT:    ret i1 [[R]]
152   %t0 = sub i8 %x, %y
153   %r = icmp sge i8 %t0, %x
154   ret i1 %r