xref: /aosp_15_r20/external/llvm/test/Transforms/InstCombine/overflow.ll (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker; RUN: opt -S -instcombine < %s | FileCheck %s
2*9880d681SAndroid Build Coastguard Worker; <rdar://problem/8558713>
3*9880d681SAndroid Build Coastguard Worker
4*9880d681SAndroid Build Coastguard Workerdeclare void @throwAnExceptionOrWhatever()
5*9880d681SAndroid Build Coastguard Worker
6*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test1(
7*9880d681SAndroid Build Coastguard Workerdefine i32 @test1(i32 %a, i32 %b) nounwind ssp {
8*9880d681SAndroid Build Coastguard Workerentry:
9*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: sext
10*9880d681SAndroid Build Coastguard Worker  %conv = sext i32 %a to i64
11*9880d681SAndroid Build Coastguard Worker  %conv2 = sext i32 %b to i64
12*9880d681SAndroid Build Coastguard Worker  %add = add nsw i64 %conv2, %conv
13*9880d681SAndroid Build Coastguard Worker  %add.off = add i64 %add, 2147483648
14*9880d681SAndroid Build Coastguard Worker; CHECK: llvm.sadd.with.overflow.i32
15*9880d681SAndroid Build Coastguard Worker  %0 = icmp ugt i64 %add.off, 4294967295
16*9880d681SAndroid Build Coastguard Worker  br i1 %0, label %if.then, label %if.end
17*9880d681SAndroid Build Coastguard Worker
18*9880d681SAndroid Build Coastguard Workerif.then:
19*9880d681SAndroid Build Coastguard Worker  tail call void @throwAnExceptionOrWhatever() nounwind
20*9880d681SAndroid Build Coastguard Worker  br label %if.end
21*9880d681SAndroid Build Coastguard Worker
22*9880d681SAndroid Build Coastguard Workerif.end:
23*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: trunc
24*9880d681SAndroid Build Coastguard Worker  %conv9 = trunc i64 %add to i32
25*9880d681SAndroid Build Coastguard Worker; CHECK: ret i32
26*9880d681SAndroid Build Coastguard Worker  ret i32 %conv9
27*9880d681SAndroid Build Coastguard Worker}
28*9880d681SAndroid Build Coastguard Worker
29*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test2(
30*9880d681SAndroid Build Coastguard Worker; This form should not be promoted for two reasons: 1) it is unprofitable to
31*9880d681SAndroid Build Coastguard Worker; promote it since the add.off instruction has another use, and 2) it is unsafe
32*9880d681SAndroid Build Coastguard Worker; because the add-with-off makes the high bits of the original add live.
33*9880d681SAndroid Build Coastguard Workerdefine i32 @test2(i32 %a, i32 %b, i64* %P) nounwind ssp {
34*9880d681SAndroid Build Coastguard Workerentry:
35*9880d681SAndroid Build Coastguard Worker  %conv = sext i32 %a to i64
36*9880d681SAndroid Build Coastguard Worker  %conv2 = sext i32 %b to i64
37*9880d681SAndroid Build Coastguard Worker  %add = add nsw i64 %conv2, %conv
38*9880d681SAndroid Build Coastguard Worker  %add.off = add i64 %add, 2147483648
39*9880d681SAndroid Build Coastguard Worker
40*9880d681SAndroid Build Coastguard Worker  store i64 %add.off, i64* %P
41*9880d681SAndroid Build Coastguard Worker
42*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: llvm.sadd.with.overflow
43*9880d681SAndroid Build Coastguard Worker  %0 = icmp ugt i64 %add.off, 4294967295
44*9880d681SAndroid Build Coastguard Worker  br i1 %0, label %if.then, label %if.end
45*9880d681SAndroid Build Coastguard Worker
46*9880d681SAndroid Build Coastguard Workerif.then:
47*9880d681SAndroid Build Coastguard Worker  tail call void @throwAnExceptionOrWhatever() nounwind
48*9880d681SAndroid Build Coastguard Worker  br label %if.end
49*9880d681SAndroid Build Coastguard Worker
50*9880d681SAndroid Build Coastguard Workerif.end:
51*9880d681SAndroid Build Coastguard Worker  %conv9 = trunc i64 %add to i32
52*9880d681SAndroid Build Coastguard Worker; CHECK: ret i32
53*9880d681SAndroid Build Coastguard Worker  ret i32 %conv9
54*9880d681SAndroid Build Coastguard Worker}
55*9880d681SAndroid Build Coastguard Worker
56*9880d681SAndroid Build Coastguard Worker; CHECK: test3
57*9880d681SAndroid Build Coastguard Worker; PR8816
58*9880d681SAndroid Build Coastguard Worker; This is illegal to transform because the high bits of the original add are
59*9880d681SAndroid Build Coastguard Worker; live out.
60*9880d681SAndroid Build Coastguard Workerdefine i64 @test3(i32 %a, i32 %b) nounwind ssp {
61*9880d681SAndroid Build Coastguard Workerentry:
62*9880d681SAndroid Build Coastguard Worker  %conv = sext i32 %a to i64
63*9880d681SAndroid Build Coastguard Worker  %conv2 = sext i32 %b to i64
64*9880d681SAndroid Build Coastguard Worker  %add = add nsw i64 %conv2, %conv
65*9880d681SAndroid Build Coastguard Worker  %add.off = add i64 %add, 2147483648
66*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: llvm.sadd.with.overflow
67*9880d681SAndroid Build Coastguard Worker  %0 = icmp ugt i64 %add.off, 4294967295
68*9880d681SAndroid Build Coastguard Worker  br i1 %0, label %if.then, label %if.end
69*9880d681SAndroid Build Coastguard Worker
70*9880d681SAndroid Build Coastguard Workerif.then:
71*9880d681SAndroid Build Coastguard Worker  tail call void @throwAnExceptionOrWhatever() nounwind
72*9880d681SAndroid Build Coastguard Worker  br label %if.end
73*9880d681SAndroid Build Coastguard Worker
74*9880d681SAndroid Build Coastguard Workerif.end:
75*9880d681SAndroid Build Coastguard Worker  ret i64 %add
76*9880d681SAndroid Build Coastguard Worker; CHECK: ret i64
77*9880d681SAndroid Build Coastguard Worker}
78*9880d681SAndroid Build Coastguard Worker
79*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test4(
80*9880d681SAndroid Build Coastguard Worker; Should be able to form an i8 sadd computed in an i32.
81*9880d681SAndroid Build Coastguard Workerdefine zeroext i8 @test4(i8 signext %a, i8 signext %b) nounwind ssp {
82*9880d681SAndroid Build Coastguard Workerentry:
83*9880d681SAndroid Build Coastguard Worker  %conv = sext i8 %a to i32
84*9880d681SAndroid Build Coastguard Worker  %conv2 = sext i8 %b to i32
85*9880d681SAndroid Build Coastguard Worker  %add = add nsw i32 %conv2, %conv
86*9880d681SAndroid Build Coastguard Worker  %add4 = add nsw i32 %add, 128
87*9880d681SAndroid Build Coastguard Worker  %cmp = icmp ugt i32 %add4, 255
88*9880d681SAndroid Build Coastguard Worker  br i1 %cmp, label %if.then, label %if.end
89*9880d681SAndroid Build Coastguard Worker; CHECK: llvm.sadd.with.overflow.i8
90*9880d681SAndroid Build Coastguard Workerif.then:                                          ; preds = %entry
91*9880d681SAndroid Build Coastguard Worker  tail call void @throwAnExceptionOrWhatever() nounwind
92*9880d681SAndroid Build Coastguard Worker  unreachable
93*9880d681SAndroid Build Coastguard Worker
94*9880d681SAndroid Build Coastguard Workerif.end:                                           ; preds = %entry
95*9880d681SAndroid Build Coastguard Worker  %conv7 = trunc i32 %add to i8
96*9880d681SAndroid Build Coastguard Worker  ret i8 %conv7
97*9880d681SAndroid Build Coastguard Worker; CHECK: ret i8
98*9880d681SAndroid Build Coastguard Worker}
99*9880d681SAndroid Build Coastguard Worker
100*9880d681SAndroid Build Coastguard Worker; CHECK-LABEL: @test8(
101*9880d681SAndroid Build Coastguard Worker; PR11438
102*9880d681SAndroid Build Coastguard Worker; This is @test1, but the operands are not sign-extended.  Make sure
103*9880d681SAndroid Build Coastguard Worker; we don't transform this case.
104*9880d681SAndroid Build Coastguard Workerdefine i32 @test8(i64 %a, i64 %b) nounwind ssp {
105*9880d681SAndroid Build Coastguard Workerentry:
106*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: llvm.sadd
107*9880d681SAndroid Build Coastguard Worker; CHECK: add i64 %a, %b
108*9880d681SAndroid Build Coastguard Worker; CHECK-NOT: llvm.sadd
109*9880d681SAndroid Build Coastguard Worker; CHECK: ret
110*9880d681SAndroid Build Coastguard Worker  %add = add i64 %a, %b
111*9880d681SAndroid Build Coastguard Worker  %add.off = add i64 %add, 2147483648
112*9880d681SAndroid Build Coastguard Worker  %0 = icmp ugt i64 %add.off, 4294967295
113*9880d681SAndroid Build Coastguard Worker  br i1 %0, label %if.then, label %if.end
114*9880d681SAndroid Build Coastguard Worker
115*9880d681SAndroid Build Coastguard Workerif.then:
116*9880d681SAndroid Build Coastguard Worker  tail call void @throwAnExceptionOrWhatever() nounwind
117*9880d681SAndroid Build Coastguard Worker  br label %if.end
118*9880d681SAndroid Build Coastguard Worker
119*9880d681SAndroid Build Coastguard Workerif.end:
120*9880d681SAndroid Build Coastguard Worker  %conv9 = trunc i64 %add to i32
121*9880d681SAndroid Build Coastguard Worker  ret i32 %conv9
122*9880d681SAndroid Build Coastguard Worker}
123