1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instsimplify -S | FileCheck %s
4 ; %ret = add nuw i8 %x, C
5 ; nuw means no unsigned wrap, from -1 to 0.
6 ; So if C is -1, %x can only be 0, and the result is always -1.
8 define i8 @add_nuw (i8 %x) {
9 ; CHECK-LABEL: @add_nuw(
10 ; CHECK-NEXT: ret i8 -1
12 %ret = add nuw i8 %x, -1
13 ; nuw here means that %x can only be 0
17 define i8 @add_nuw_nsw (i8 %x) {
18 ; CHECK-LABEL: @add_nuw_nsw(
19 ; CHECK-NEXT: ret i8 -1
21 %ret = add nuw nsw i8 %x, -1
22 ; nuw here means that %x can only be 0
26 define i8 @add_nuw_commute (i8 %x) {
27 ; CHECK-LABEL: @add_nuw_commute(
28 ; CHECK-NEXT: ret i8 -1
30 %ret = add nuw i8 -1, %x ; swapped
31 ; nuw here means that %x can only be 0
35 ; ============================================================================ ;
36 ; Positive tests with value range known
37 ; ============================================================================ ;
39 declare void @llvm.assume(i1 %cond);
41 define i8 @knownbits_allones(i8 %x, i8 %y) {
42 ; CHECK-LABEL: @knownbits_allones(
43 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[Y:%.*]], -2
44 ; CHECK-NEXT: tail call void @llvm.assume(i1 [[CMP]])
45 ; CHECK-NEXT: [[RET:%.*]] = add nuw i8 [[X:%.*]], [[Y]]
46 ; CHECK-NEXT: ret i8 [[RET]]
48 %cmp = icmp slt i8 %y, 254
49 tail call void @llvm.assume(i1 %cmp)
50 %ret = add nuw i8 %x, %y
54 ; ============================================================================ ;
56 ; ============================================================================ ;
58 define <2 x i8> @add_vec(<2 x i8> %x) {
59 ; CHECK-LABEL: @add_vec(
60 ; CHECK-NEXT: ret <2 x i8> <i8 -1, i8 -1>
62 %ret = add nuw <2 x i8> %x, <i8 -1, i8 -1>
66 define <3 x i8> @add_vec_undef(<3 x i8> %x) {
67 ; CHECK-LABEL: @add_vec_undef(
68 ; CHECK-NEXT: ret <3 x i8> <i8 -1, i8 undef, i8 -1>
70 %ret = add nuw <3 x i8> %x, <i8 -1, i8 undef, i8 -1>
74 ; ============================================================================ ;
75 ; Negative tests. Should not be folded.
76 ; ============================================================================ ;
78 define i8 @bad_add (i8 %x) {
79 ; CHECK-LABEL: @bad_add(
80 ; CHECK-NEXT: [[RET:%.*]] = add i8 [[X:%.*]], -1
81 ; CHECK-NEXT: ret i8 [[RET]]
83 %ret = add i8 %x, -1 ; need nuw
87 define i8 @bad_add_nsw (i8 %x) {
88 ; CHECK-LABEL: @bad_add_nsw(
89 ; CHECK-NEXT: [[RET:%.*]] = add nsw i8 [[X:%.*]], -1
90 ; CHECK-NEXT: ret i8 [[RET]]
92 %ret = add nsw i8 %x, -1 ; need nuw
96 ; Second `add` operand is not `-1` constant
98 define i8 @bad_add0(i8 %x, i8 %addop2) {
99 ; CHECK-LABEL: @bad_add0(
100 ; CHECK-NEXT: [[RET:%.*]] = add nuw i8 [[X:%.*]], [[ADDOP2:%.*]]
101 ; CHECK-NEXT: ret i8 [[RET]]
103 %ret = add nuw i8 %x, %addop2
109 define i8 @bad_add1(i8 %x) {
110 ; CHECK-LABEL: @bad_add1(
111 ; CHECK-NEXT: [[RET:%.*]] = add nuw i8 [[X:%.*]], 1
112 ; CHECK-NEXT: ret i8 [[RET]]
114 %ret = add nuw i8 %x, 1 ; not -1
118 define <2 x i8> @bad_add_vec_nonsplat(<2 x i8> %x) {
119 ; CHECK-LABEL: @bad_add_vec_nonsplat(
120 ; CHECK-NEXT: [[RET:%.*]] = add nuw <2 x i8> [[X:%.*]], <i8 -1, i8 1>
121 ; CHECK-NEXT: ret <2 x i8> [[RET]]
123 %ret = add nuw <2 x i8> %x, <i8 -1, i8 1>
129 define i8 @bad_knownbits(i8 %x, i8 %y) {
130 ; CHECK-LABEL: @bad_knownbits(
131 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X:%.*]], -3
132 ; CHECK-NEXT: tail call void @llvm.assume(i1 [[CMP]])
133 ; CHECK-NEXT: [[RET:%.*]] = add nuw i8 [[X]], [[Y:%.*]]
134 ; CHECK-NEXT: ret i8 [[RET]]
136 %cmp = icmp slt i8 %x, 253
137 tail call void @llvm.assume(i1 %cmp)
138 %ret = add nuw i8 %x, %y