1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
5 declare i32 @llvm.abs.i32(i32, i1)
6 declare <4 x i32> @llvm.abs.v4i32(<4 x i32>, i1)
8 define i32 @test_sdiv_canonicalize_op0(i32 %x, i32 %y) {
9 ; CHECK-LABEL: @test_sdiv_canonicalize_op0(
10 ; CHECK-NEXT: [[SDIV1:%.*]] = sdiv i32 [[X:%.*]], [[Y:%.*]]
11 ; CHECK-NEXT: [[SDIV:%.*]] = sub nsw i32 0, [[SDIV1]]
12 ; CHECK-NEXT: ret i32 [[SDIV]]
14 %neg = sub nsw i32 0, %x
15 %sdiv = sdiv i32 %neg, %y
19 define i32 @test_sdiv_canonicalize_op0_exact(i32 %x, i32 %y) {
20 ; CHECK-LABEL: @test_sdiv_canonicalize_op0_exact(
21 ; CHECK-NEXT: [[SDIV1:%.*]] = sdiv exact i32 [[X:%.*]], [[Y:%.*]]
22 ; CHECK-NEXT: [[SDIV:%.*]] = sub nsw i32 0, [[SDIV1]]
23 ; CHECK-NEXT: ret i32 [[SDIV]]
25 %neg = sub nsw i32 0, %x
26 %sdiv = sdiv exact i32 %neg, %y
30 ; (X/-Y) is not equal to -(X/Y), don't canonicalize.
31 define i32 @test_sdiv_canonicalize_op1(i32 %x, i32 %z) {
32 ; CHECK-LABEL: @test_sdiv_canonicalize_op1(
33 ; CHECK-NEXT: [[Y:%.*]] = mul i32 [[Z:%.*]], 3
34 ; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[X:%.*]]
35 ; CHECK-NEXT: [[SDIV:%.*]] = sdiv i32 [[Y]], [[NEG]]
36 ; CHECK-NEXT: ret i32 [[SDIV]]
39 %neg = sub nsw i32 0, %x
40 %sdiv = sdiv i32 %y, %neg
44 define i32 @test_sdiv_canonicalize_nonsw(i32 %x, i32 %y) {
45 ; CHECK-LABEL: @test_sdiv_canonicalize_nonsw(
46 ; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[X:%.*]]
47 ; CHECK-NEXT: [[SDIV:%.*]] = sdiv i32 [[NEG]], [[Y:%.*]]
48 ; CHECK-NEXT: ret i32 [[SDIV]]
51 %sdiv = sdiv i32 %neg, %y
55 define <2 x i32> @test_sdiv_canonicalize_vec(<2 x i32> %x, <2 x i32> %y) {
56 ; CHECK-LABEL: @test_sdiv_canonicalize_vec(
57 ; CHECK-NEXT: [[SDIV1:%.*]] = sdiv <2 x i32> [[X:%.*]], [[Y:%.*]]
58 ; CHECK-NEXT: [[SDIV:%.*]] = sub nsw <2 x i32> zeroinitializer, [[SDIV1]]
59 ; CHECK-NEXT: ret <2 x i32> [[SDIV]]
61 %neg = sub nsw <2 x i32> <i32 0, i32 0>, %x
62 %sdiv = sdiv <2 x i32> %neg, %y
66 define i32 @test_sdiv_canonicalize_multiple_uses(i32 %x, i32 %y) {
67 ; CHECK-LABEL: @test_sdiv_canonicalize_multiple_uses(
68 ; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[X:%.*]]
69 ; CHECK-NEXT: [[SDIV:%.*]] = sdiv i32 [[NEG]], [[Y:%.*]]
70 ; CHECK-NEXT: [[SDIV2:%.*]] = sdiv i32 [[SDIV]], [[NEG]]
71 ; CHECK-NEXT: ret i32 [[SDIV2]]
73 %neg = sub nsw i32 0, %x
74 %sdiv = sdiv i32 %neg, %y
75 %sdiv2 = sdiv i32 %sdiv, %neg
79 ; There is combination: -(X/CE) -> (X/-CE).
80 ; If combines (X/-CE) to -(X/CE), make sure don't combine them endless.
84 define i64 @test_sdiv_canonicalize_constexpr(i64 %L1) {
85 ; currently opt folds (sub nsw i64 0, constexpr) -> (sub i64, 0, constexpr).
86 ; sdiv canonicalize requires a nsw sub.
87 ; CHECK-LABEL: @test_sdiv_canonicalize_constexpr(
88 ; CHECK-NEXT: [[B4:%.*]] = sdiv i64 [[L1:%.*]], sub (i64 0, i64 ptrtoint (ptr @X to i64))
89 ; CHECK-NEXT: ret i64 [[B4]]
91 %v1 = ptrtoint ptr @X to i64
92 %B8 = sub nsw i64 0, %v1
93 %B4 = sdiv i64 %L1, %B8
97 define i32 @sdiv_abs_nsw(i32 %x) {
98 ; CHECK-LABEL: @sdiv_abs_nsw(
99 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], -1
100 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i32 1, i32 -1
101 ; CHECK-NEXT: ret i32 [[R]]
103 %a = call i32 @llvm.abs.i32(i32 %x, i1 true)
108 define <4 x i32> @sdiv_abs_nsw_vec(<4 x i32> %x) {
109 ; CHECK-LABEL: @sdiv_abs_nsw_vec(
110 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt <4 x i32> [[X:%.*]], <i32 -1, i32 -1, i32 -1, i32 -1>
111 ; CHECK-NEXT: [[R:%.*]] = select <4 x i1> [[TMP1]], <4 x i32> <i32 1, i32 1, i32 1, i32 1>, <4 x i32> <i32 -1, i32 -1, i32 -1, i32 -1>
112 ; CHECK-NEXT: ret <4 x i32> [[R]]
114 %a = call <4 x i32> @llvm.abs.v4i32(<4 x i32> %x, i1 true)
115 %r = sdiv <4 x i32> %x, %a
119 ; Negative test - requires poison int min (nsw)
121 define i32 @sdiv_abs(i32 %x) {
122 ; CHECK-LABEL: @sdiv_abs(
123 ; CHECK-NEXT: [[A:%.*]] = call i32 @llvm.abs.i32(i32 [[X:%.*]], i1 false)
124 ; CHECK-NEXT: [[R:%.*]] = sdiv i32 [[A]], [[X]]
125 ; CHECK-NEXT: ret i32 [[R]]
127 %a = call i32 @llvm.abs.i32(i32 %x, i1 false)
134 define i32 @sdiv_abs_extra_use(i32 %x) {
135 ; CHECK-LABEL: @sdiv_abs_extra_use(
136 ; CHECK-NEXT: [[A:%.*]] = call i32 @llvm.abs.i32(i32 [[X:%.*]], i1 true)
137 ; CHECK-NEXT: call void @use(i32 [[A]])
138 ; CHECK-NEXT: [[R:%.*]] = sdiv i32 [[A]], [[X]]
139 ; CHECK-NEXT: ret i32 [[R]]
141 %a = call i32 @llvm.abs.i32(i32 %x, i1 true)
142 call void @use(i32 %a)