1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 define i32 @test_sdiv_canonicalize_op0(i32 %x, i32 %y) {
5 ; CHECK-LABEL: @test_sdiv_canonicalize_op0(
6 ; CHECK-NEXT: [[SDIV1:%.*]] = sdiv i32 [[X:%.*]], [[Y:%.*]]
7 ; CHECK-NEXT: [[SDIV:%.*]] = sub nsw i32 0, [[SDIV1]]
8 ; CHECK-NEXT: ret i32 [[SDIV]]
10 %neg = sub nsw i32 0, %x
11 %sdiv = sdiv i32 %neg, %y
15 define i32 @test_sdiv_canonicalize_op0_exact(i32 %x, i32 %y) {
16 ; CHECK-LABEL: @test_sdiv_canonicalize_op0_exact(
17 ; CHECK-NEXT: [[SDIV1:%.*]] = sdiv exact i32 [[X:%.*]], [[Y:%.*]]
18 ; CHECK-NEXT: [[SDIV:%.*]] = sub nsw i32 0, [[SDIV1]]
19 ; CHECK-NEXT: ret i32 [[SDIV]]
21 %neg = sub nsw i32 0, %x
22 %sdiv = sdiv exact i32 %neg, %y
26 ; (X/-Y) is not equal to -(X/Y), don't canonicalize.
27 define i32 @test_sdiv_canonicalize_op1(i32 %x, i32 %z) {
28 ; CHECK-LABEL: @test_sdiv_canonicalize_op1(
29 ; CHECK-NEXT: [[Y:%.*]] = mul i32 [[Z:%.*]], 3
30 ; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[X:%.*]]
31 ; CHECK-NEXT: [[SDIV:%.*]] = sdiv i32 [[Y]], [[NEG]]
32 ; CHECK-NEXT: ret i32 [[SDIV]]
35 %neg = sub nsw i32 0, %x
36 %sdiv = sdiv i32 %y, %neg
40 define i32 @test_sdiv_canonicalize_nonsw(i32 %x, i32 %y) {
41 ; CHECK-LABEL: @test_sdiv_canonicalize_nonsw(
42 ; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[X:%.*]]
43 ; CHECK-NEXT: [[SDIV:%.*]] = sdiv i32 [[NEG]], [[Y:%.*]]
44 ; CHECK-NEXT: ret i32 [[SDIV]]
47 %sdiv = sdiv i32 %neg, %y
51 define <2 x i32> @test_sdiv_canonicalize_vec(<2 x i32> %x, <2 x i32> %y) {
52 ; CHECK-LABEL: @test_sdiv_canonicalize_vec(
53 ; CHECK-NEXT: [[SDIV1:%.*]] = sdiv <2 x i32> [[X:%.*]], [[Y:%.*]]
54 ; CHECK-NEXT: [[SDIV:%.*]] = sub nsw <2 x i32> zeroinitializer, [[SDIV1]]
55 ; CHECK-NEXT: ret <2 x i32> [[SDIV]]
57 %neg = sub nsw <2 x i32> <i32 0, i32 0>, %x
58 %sdiv = sdiv <2 x i32> %neg, %y
62 define i32 @test_sdiv_canonicalize_multiple_uses(i32 %x, i32 %y) {
63 ; CHECK-LABEL: @test_sdiv_canonicalize_multiple_uses(
64 ; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[X:%.*]]
65 ; CHECK-NEXT: [[SDIV:%.*]] = sdiv i32 [[NEG]], [[Y:%.*]]
66 ; CHECK-NEXT: [[SDIV2:%.*]] = sdiv i32 [[SDIV]], [[NEG]]
67 ; CHECK-NEXT: ret i32 [[SDIV2]]
69 %neg = sub nsw i32 0, %x
70 %sdiv = sdiv i32 %neg, %y
71 %sdiv2 = sdiv i32 %sdiv, %neg
75 ; There is combination: -(X/CE) -> (X/-CE).
76 ; If combines (X/-CE) to -(X/CE), make sure don't combine them endless.
80 define i64 @test_sdiv_canonicalize_constexpr(i64 %L1) {
81 ; currently opt folds (sub nsw i64 0, constexpr) -> (sub i64, 0, constexpr).
82 ; sdiv canonicalize requires a nsw sub.
83 ; CHECK-LABEL: @test_sdiv_canonicalize_constexpr(
84 ; CHECK-NEXT: [[B4:%.*]] = sdiv i64 [[L1:%.*]], sub (i64 0, i64 ptrtoint (i32* @X to i64))
85 ; CHECK-NEXT: ret i64 [[B4]]
87 %v1 = ptrtoint i32* @X to i64
88 %B8 = sub nsw i64 0, %v1
89 %B4 = sdiv i64 %L1, %B8