1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
4 declare { <2 x i32>, <2 x i1> } @llvm.ssub.with.overflow.v2i32(<2 x i32>, <2 x i32>)
6 declare { <2 x i8>, <2 x i1> } @llvm.ssub.with.overflow.v2i8(<2 x i8>, <2 x i8>)
8 declare { i32, i1 } @llvm.ssub.with.overflow.i32(i32, i32)
10 declare { i8, i1 } @llvm.ssub.with.overflow.i8(i8, i8)
12 define { i32, i1 } @simple_fold(i32 %x) {
13 ; CHECK-LABEL: @simple_fold(
14 ; CHECK-NEXT: [[B:%.*]] = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[X:%.*]], i32 -20)
15 ; CHECK-NEXT: ret { i32, i1 } [[B]]
17 %a = sub nsw i32 %x, 7
18 %b = tail call { i32, i1 } @llvm.ssub.with.overflow.i32(i32 %a, i32 13)
22 define { i32, i1 } @fold_mixed_signs(i32 %x) {
23 ; CHECK-LABEL: @fold_mixed_signs(
24 ; CHECK-NEXT: [[B:%.*]] = add nsw i32 [[X:%.*]], -6
25 ; CHECK-NEXT: [[TMP1:%.*]] = insertvalue { i32, i1 } { i32 poison, i1 false }, i32 [[B]], 0
26 ; CHECK-NEXT: ret { i32, i1 } [[TMP1]]
28 %a = sub nsw i32 %x, 13
29 %b = tail call { i32, i1 } @llvm.ssub.with.overflow.i32(i32 %a, i32 -7)
33 define { i8, i1 } @fold_on_constant_sub_no_overflow(i8 %x) {
34 ; CHECK-LABEL: @fold_on_constant_sub_no_overflow(
35 ; CHECK-NEXT: [[B:%.*]] = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 [[X:%.*]], i8 -128)
36 ; CHECK-NEXT: ret { i8, i1 } [[B]]
38 %a = sub nsw i8 %x, 100
39 %b = tail call { i8, i1 } @llvm.ssub.with.overflow.i8(i8 %a, i8 28)
43 define { i8, i1 } @no_fold_on_constant_sub_overflow(i8 %x) {
44 ; CHECK-LABEL: @no_fold_on_constant_sub_overflow(
45 ; CHECK-NEXT: [[A:%.*]] = add nsw i8 [[X:%.*]], -100
46 ; CHECK-NEXT: [[B:%.*]] = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 [[A]], i8 -29)
47 ; CHECK-NEXT: ret { i8, i1 } [[B]]
49 %a = sub nsw i8 %x, 100
50 %b = tail call { i8, i1 } @llvm.ssub.with.overflow.i8(i8 %a, i8 29)
54 define { <2 x i32>, <2 x i1> } @fold_simple_splat_constant(<2 x i32> %x) {
55 ; CHECK-LABEL: @fold_simple_splat_constant(
56 ; CHECK-NEXT: [[B:%.*]] = call { <2 x i32>, <2 x i1> } @llvm.sadd.with.overflow.v2i32(<2 x i32> [[X:%.*]], <2 x i32> <i32 -42, i32 -42>)
57 ; CHECK-NEXT: ret { <2 x i32>, <2 x i1> } [[B]]
59 %a = sub nsw <2 x i32> %x, <i32 12, i32 12>
60 %b = tail call { <2 x i32>, <2 x i1> } @llvm.ssub.with.overflow.v2i32(<2 x i32> %a, <2 x i32> <i32 30, i32 30>)
61 ret { <2 x i32>, <2 x i1> } %b
64 define { <2 x i32>, <2 x i1> } @no_fold_splat_undef_constant(<2 x i32> %x) {
65 ; CHECK-LABEL: @no_fold_splat_undef_constant(
66 ; CHECK-NEXT: [[A:%.*]] = add <2 x i32> [[X:%.*]], <i32 -12, i32 undef>
67 ; CHECK-NEXT: [[B:%.*]] = call { <2 x i32>, <2 x i1> } @llvm.sadd.with.overflow.v2i32(<2 x i32> [[A]], <2 x i32> <i32 -30, i32 -30>)
68 ; CHECK-NEXT: ret { <2 x i32>, <2 x i1> } [[B]]
70 %a = sub nsw <2 x i32> %x, <i32 12, i32 undef>
71 %b = tail call { <2 x i32>, <2 x i1> } @llvm.ssub.with.overflow.v2i32(<2 x i32> %a, <2 x i32> <i32 30, i32 30>)
72 ret { <2 x i32>, <2 x i1> } %b
75 define { <2 x i32>, <2 x i1> } @no_fold_splat_not_constant(<2 x i32> %x, <2 x i32> %y) {
76 ; CHECK-LABEL: @no_fold_splat_not_constant(
77 ; CHECK-NEXT: [[A:%.*]] = sub nsw <2 x i32> [[X:%.*]], [[Y:%.*]]
78 ; CHECK-NEXT: [[B:%.*]] = call { <2 x i32>, <2 x i1> } @llvm.sadd.with.overflow.v2i32(<2 x i32> [[A]], <2 x i32> <i32 -30, i32 -30>)
79 ; CHECK-NEXT: ret { <2 x i32>, <2 x i1> } [[B]]
81 %a = sub nsw <2 x i32> %x, %y
82 %b = tail call { <2 x i32>, <2 x i1> } @llvm.ssub.with.overflow.v2i32(<2 x i32> %a, <2 x i32> <i32 30, i32 30>)
83 ret { <2 x i32>, <2 x i1> } %b
86 define { i32, i1 } @fold_nuwnsw(i32 %x) {
87 ; CHECK-LABEL: @fold_nuwnsw(
88 ; CHECK-NEXT: [[B:%.*]] = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[X:%.*]], i32 -42)
89 ; CHECK-NEXT: ret { i32, i1 } [[B]]
91 %a = sub nuw nsw i32 %x, 12
92 %b = tail call { i32, i1 } @llvm.ssub.with.overflow.i32(i32 %a, i32 30)
96 define { i32, i1 } @no_fold_nuw(i32 %x) {
97 ; CHECK-LABEL: @no_fold_nuw(
98 ; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], -12
99 ; CHECK-NEXT: [[B:%.*]] = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[A]], i32 -30)
100 ; CHECK-NEXT: ret { i32, i1 } [[B]]
102 %a = sub nuw i32 %x, 12
103 %b = tail call { i32, i1 } @llvm.ssub.with.overflow.i32(i32 %a, i32 30)
107 define { i32, i1 } @no_fold_wrapped_sub(i32 %x) {
108 ; CHECK-LABEL: @no_fold_wrapped_sub(
109 ; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], -12
110 ; CHECK-NEXT: [[B:%.*]] = tail call { i32, i1 } @llvm.ssub.with.overflow.i32(i32 30, i32 [[A]])
111 ; CHECK-NEXT: ret { i32, i1 } [[B]]
114 %b = tail call { i32, i1 } @llvm.ssub.with.overflow.i32(i32 30, i32 %a)
118 define { i32, i1 } @fold_add_simple(i32 %x) {
119 ; CHECK-LABEL: @fold_add_simple(
120 ; CHECK-NEXT: [[B:%.*]] = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[X:%.*]], i32 -42)
121 ; CHECK-NEXT: ret { i32, i1 } [[B]]
123 %a = add nsw i32 %x, -12
124 %b = tail call { i32, i1 } @llvm.ssub.with.overflow.i32(i32 %a, i32 30)
128 define { <2 x i32>, <2 x i1> } @keep_ssubo_undef(<2 x i32> %x) {
129 ; CHECK-LABEL: @keep_ssubo_undef(
130 ; CHECK-NEXT: [[A:%.*]] = tail call { <2 x i32>, <2 x i1> } @llvm.ssub.with.overflow.v2i32(<2 x i32> [[X:%.*]], <2 x i32> <i32 30, i32 undef>)
131 ; CHECK-NEXT: ret { <2 x i32>, <2 x i1> } [[A]]
133 %a = tail call { <2 x i32>, <2 x i1> } @llvm.ssub.with.overflow.v2i32(<2 x i32> %x, <2 x i32> <i32 30, i32 undef>)
134 ret { <2 x i32>, <2 x i1> } %a
137 define { <2 x i32>, <2 x i1> } @keep_ssubo_non_splat(<2 x i32> %x) {
138 ; CHECK-LABEL: @keep_ssubo_non_splat(
139 ; CHECK-NEXT: [[A:%.*]] = call { <2 x i32>, <2 x i1> } @llvm.sadd.with.overflow.v2i32(<2 x i32> [[X:%.*]], <2 x i32> <i32 -30, i32 -31>)
140 ; CHECK-NEXT: ret { <2 x i32>, <2 x i1> } [[A]]
142 %a = tail call { <2 x i32>, <2 x i1> } @llvm.ssub.with.overflow.v2i32(<2 x i32> %x, <2 x i32> <i32 30, i32 31>)
143 ret { <2 x i32>, <2 x i1> } %a
146 define { <2 x i8>, <2 x i1> } @keep_ssubo_one_element_is_128(<2 x i8> %x) {
147 ; CHECK-LABEL: @keep_ssubo_one_element_is_128(
148 ; CHECK-NEXT: [[A:%.*]] = tail call { <2 x i8>, <2 x i1> } @llvm.ssub.with.overflow.v2i8(<2 x i8> [[X:%.*]], <2 x i8> <i8 0, i8 -128>)
149 ; CHECK-NEXT: ret { <2 x i8>, <2 x i1> } [[A]]
151 %a = tail call { <2 x i8>, <2 x i1> } @llvm.ssub.with.overflow.v2i8(<2 x i8> %x, <2 x i8> <i8 0, i8 -128>)
152 ret { <2 x i8>, <2 x i1> } %a
155 define { i8, i1 } @keep_ssubo_128(i8 %x) {
156 ; CHECK-LABEL: @keep_ssubo_128(
157 ; CHECK-NEXT: [[A:%.*]] = tail call { i8, i1 } @llvm.ssub.with.overflow.i8(i8 [[X:%.*]], i8 -128)
158 ; CHECK-NEXT: ret { i8, i1 } [[A]]
160 %a = tail call { i8, i1 } @llvm.ssub.with.overflow.i8(i8 %x, i8 -128)