1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -instcombine < %s | FileCheck %s
3 ; <rdar://problem/8558713>
5 declare void @throwAnExceptionOrWhatever()
7 define i32 @test1(i32 %a, i32 %b) nounwind ssp {
10 ; CHECK-NEXT: [[SADD:%.*]] = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[B:%.*]], i32 [[A:%.*]])
11 ; CHECK-NEXT: [[SADD_RESULT:%.*]] = extractvalue { i32, i1 } [[SADD]], 0
12 ; CHECK-NEXT: [[TMP0:%.*]] = extractvalue { i32, i1 } [[SADD]], 1
13 ; CHECK-NEXT: br i1 [[TMP0]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
15 ; CHECK-NEXT: tail call void @throwAnExceptionOrWhatever() #2
16 ; CHECK-NEXT: br label [[IF_END]]
18 ; CHECK-NEXT: ret i32 [[SADD_RESULT]]
21 %conv = sext i32 %a to i64
22 %conv2 = sext i32 %b to i64
23 %add = add nsw i64 %conv2, %conv
24 %add.off = add i64 %add, 2147483648
25 %0 = icmp ugt i64 %add.off, 4294967295
26 br i1 %0, label %if.then, label %if.end
29 tail call void @throwAnExceptionOrWhatever() nounwind
33 %conv9 = trunc i64 %add to i32
37 ; This form should not be promoted for two reasons: 1) it is unprofitable to
38 ; promote it since the add.off instruction has another use, and 2) it is unsafe
39 ; because the add-with-off makes the high bits of the original add live.
41 define i32 @test2(i32 %a, i32 %b, i64* %P) nounwind ssp {
42 ; CHECK-LABEL: @test2(
44 ; CHECK-NEXT: [[CONV:%.*]] = sext i32 [[A:%.*]] to i64
45 ; CHECK-NEXT: [[CONV2:%.*]] = sext i32 [[B:%.*]] to i64
46 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i64 [[CONV2]], [[CONV]]
47 ; CHECK-NEXT: [[ADD_OFF:%.*]] = add nsw i64 [[ADD]], 2147483648
48 ; CHECK-NEXT: store i64 [[ADD_OFF]], i64* [[P:%.*]], align 4
49 ; CHECK-NEXT: [[TMP0:%.*]] = icmp ugt i64 [[ADD_OFF]], 4294967295
50 ; CHECK-NEXT: br i1 [[TMP0]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
52 ; CHECK-NEXT: tail call void @throwAnExceptionOrWhatever() #2
53 ; CHECK-NEXT: br label [[IF_END]]
55 ; CHECK-NEXT: [[CONV9:%.*]] = trunc i64 [[ADD]] to i32
56 ; CHECK-NEXT: ret i32 [[CONV9]]
59 %conv = sext i32 %a to i64
60 %conv2 = sext i32 %b to i64
61 %add = add nsw i64 %conv2, %conv
62 %add.off = add i64 %add, 2147483648
63 store i64 %add.off, i64* %P
64 %0 = icmp ugt i64 %add.off, 4294967295
65 br i1 %0, label %if.then, label %if.end
68 tail call void @throwAnExceptionOrWhatever() nounwind
72 %conv9 = trunc i64 %add to i32
77 ; This is illegal to transform because the high bits of the original add are
79 define i64 @test3(i32 %a, i32 %b) nounwind ssp {
80 ; CHECK-LABEL: @test3(
82 ; CHECK-NEXT: [[CONV:%.*]] = sext i32 [[A:%.*]] to i64
83 ; CHECK-NEXT: [[CONV2:%.*]] = sext i32 [[B:%.*]] to i64
84 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i64 [[CONV2]], [[CONV]]
85 ; CHECK-NEXT: [[ADD_OFF:%.*]] = add nsw i64 [[ADD]], 2147483648
86 ; CHECK-NEXT: [[TMP0:%.*]] = icmp ugt i64 [[ADD_OFF]], 4294967295
87 ; CHECK-NEXT: br i1 [[TMP0]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
89 ; CHECK-NEXT: tail call void @throwAnExceptionOrWhatever() #2
90 ; CHECK-NEXT: br label [[IF_END]]
92 ; CHECK-NEXT: ret i64 [[ADD]]
95 %conv = sext i32 %a to i64
96 %conv2 = sext i32 %b to i64
97 %add = add nsw i64 %conv2, %conv
98 %add.off = add i64 %add, 2147483648
99 %0 = icmp ugt i64 %add.off, 4294967295
100 br i1 %0, label %if.then, label %if.end
103 tail call void @throwAnExceptionOrWhatever() nounwind
110 ; Should be able to form an i8 sadd computed in an i32.
112 define zeroext i8 @test4(i8 signext %a, i8 signext %b) nounwind ssp {
113 ; CHECK-LABEL: @test4(
115 ; CHECK-NEXT: [[SADD:%.*]] = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 [[B:%.*]], i8 [[A:%.*]])
116 ; CHECK-NEXT: [[CMP:%.*]] = extractvalue { i8, i1 } [[SADD]], 1
117 ; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
119 ; CHECK-NEXT: tail call void @throwAnExceptionOrWhatever() #2
120 ; CHECK-NEXT: unreachable
122 ; CHECK-NEXT: [[SADD_RESULT:%.*]] = extractvalue { i8, i1 } [[SADD]], 0
123 ; CHECK-NEXT: ret i8 [[SADD_RESULT]]
126 %conv = sext i8 %a to i32
127 %conv2 = sext i8 %b to i32
128 %add = add nsw i32 %conv2, %conv
129 %add4 = add nsw i32 %add, 128
130 %cmp = icmp ugt i32 %add4, 255
131 br i1 %cmp, label %if.then, label %if.end
133 tail call void @throwAnExceptionOrWhatever() nounwind
137 %conv7 = trunc i32 %add to i8
142 ; This is @test1, but the operands are not sign-extended. Make sure
143 ; we don't transform this case.
145 define i32 @test8(i64 %a, i64 %b) nounwind ssp {
146 ; CHECK-LABEL: @test8(
148 ; CHECK-NEXT: [[ADD:%.*]] = add i64 [[A:%.*]], [[B:%.*]]
149 ; CHECK-NEXT: [[ADD_OFF:%.*]] = add i64 [[ADD]], 2147483648
150 ; CHECK-NEXT: [[TMP0:%.*]] = icmp ugt i64 [[ADD_OFF]], 4294967295
151 ; CHECK-NEXT: br i1 [[TMP0]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
153 ; CHECK-NEXT: tail call void @throwAnExceptionOrWhatever() #2
154 ; CHECK-NEXT: br label [[IF_END]]
156 ; CHECK-NEXT: [[CONV9:%.*]] = trunc i64 [[ADD]] to i32
157 ; CHECK-NEXT: ret i32 [[CONV9]]
160 %add = add i64 %a, %b
161 %add.off = add i64 %add, 2147483648
162 %0 = icmp ugt i64 %add.off, 4294967295
163 br i1 %0, label %if.then, label %if.end
166 tail call void @throwAnExceptionOrWhatever() nounwind
170 %conv9 = trunc i64 %add to i32