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: [[NEG:%.*]] = sub nsw i32 0, [[X:%.*]]
7 ; CHECK-NEXT: [[SDIV:%.*]] = sdiv i32 [[NEG]], [[Y:%.*]]
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_op1(i32 %x, i32 %z) {
16 ; CHECK-LABEL: @test_sdiv_canonicalize_op1(
17 ; CHECK-NEXT: [[Y:%.*]] = mul i32 [[Z:%.*]], 3
18 ; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[X:%.*]]
19 ; CHECK-NEXT: [[SDIV:%.*]] = sdiv i32 [[Y]], [[NEG]]
20 ; CHECK-NEXT: ret i32 [[SDIV]]
23 %neg = sub nsw i32 0, %x
24 %sdiv = sdiv i32 %y, %neg
28 define i32 @test_sdiv_canonicalize_nonsw(i32 %x, i32 %y) {
29 ; CHECK-LABEL: @test_sdiv_canonicalize_nonsw(
30 ; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[X:%.*]]
31 ; CHECK-NEXT: [[SDIV:%.*]] = sdiv i32 [[NEG]], [[Y:%.*]]
32 ; CHECK-NEXT: ret i32 [[SDIV]]
35 %sdiv = sdiv i32 %neg, %y
39 define <2 x i32> @test_sdiv_canonicalize_vec(<2 x i32> %x, <2 x i32> %y) {
40 ; CHECK-LABEL: @test_sdiv_canonicalize_vec(
41 ; CHECK-NEXT: [[NEG:%.*]] = sub nsw <2 x i32> zeroinitializer, [[X:%.*]]
42 ; CHECK-NEXT: [[SDIV:%.*]] = sdiv <2 x i32> [[NEG]], [[Y:%.*]]
43 ; CHECK-NEXT: ret <2 x i32> [[SDIV]]
45 %neg = sub nsw <2 x i32> <i32 0, i32 0>, %x
46 %sdiv = sdiv <2 x i32> %neg, %y
50 define i32 @test_sdiv_canonicalize_multiple_uses(i32 %x, i32 %y) {
51 ; CHECK-LABEL: @test_sdiv_canonicalize_multiple_uses(
52 ; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[X:%.*]]
53 ; CHECK-NEXT: [[SDIV:%.*]] = sdiv i32 [[NEG]], [[Y:%.*]]
54 ; CHECK-NEXT: [[SDIV2:%.*]] = sdiv i32 [[SDIV]], [[NEG]]
55 ; CHECK-NEXT: ret i32 [[SDIV2]]
57 %neg = sub nsw i32 0, %x
58 %sdiv = sdiv i32 %neg, %y
59 %sdiv2 = sdiv i32 %sdiv, %neg
63 ; There is combination: (X/-CE) -> -(X/CE)
64 ; There is another combination: -(X/CE) -> (X/-CE)
65 ; Make sure don't combine them endless.
69 define i64 @test_sdiv_canonicalize_constexpr(i64 %L1) {
70 ; currently opt folds (sub nsw i64 0, constexpr) -> (sub i64, 0, constexpr).
71 ; sdiv canonicalize requires a nsw sub.
72 ; CHECK-LABEL: @test_sdiv_canonicalize_constexpr(
73 ; CHECK-NEXT: [[B4:%.*]] = sdiv i64 [[L1:%.*]], sub (i64 0, i64 ptrtoint (i32* @X to i64))
74 ; CHECK-NEXT: ret i64 [[B4]]
76 %v1 = ptrtoint i32* @X to i64
77 %B8 = sub nsw i64 0, %v1
78 %B4 = sdiv i64 %L1, %B8