brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.5 KiB · e3e4a0c Raw
146 lines · plain
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py2; RUN: opt < %s -passes=instcombine -S | FileCheck %s3 4declare void @use(i32)5declare i32 @llvm.abs.i32(i32, i1)6declare <4 x i32> @llvm.abs.v4i32(<4 x i32>, i1)7 8define 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]]13;14  %neg = sub nsw i32 0, %x15  %sdiv = sdiv i32 %neg, %y16  ret i32 %sdiv17}18 19define 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]]24;25  %neg = sub nsw i32 0, %x26  %sdiv = sdiv exact i32 %neg, %y27  ret i32 %sdiv28}29 30; (X/-Y) is not equal to -(X/Y), don't canonicalize.31define i32 @test_sdiv_canonicalize_op1(i32 %x, i32 %z) {32; CHECK-LABEL: @test_sdiv_canonicalize_op1(33; CHECK-NEXT:    [[Y:%.*]] = mul i32 [[Z:%.*]], 334; CHECK-NEXT:    [[NEG:%.*]] = sub nsw i32 0, [[X:%.*]]35; CHECK-NEXT:    [[SDIV:%.*]] = sdiv i32 [[Y]], [[NEG]]36; CHECK-NEXT:    ret i32 [[SDIV]]37;38  %y = mul i32 %z, 339  %neg = sub nsw i32 0, %x40  %sdiv = sdiv i32 %y, %neg41  ret i32 %sdiv42}43 44define 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]]49;50  %neg = sub i32 0, %x51  %sdiv = sdiv i32 %neg, %y52  ret i32 %sdiv53}54 55define <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]]60;61  %neg = sub nsw <2 x i32> <i32 0, i32 0>, %x62  %sdiv = sdiv <2 x i32> %neg, %y63  ret <2 x i32> %sdiv64}65 66define 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]]72;73  %neg = sub nsw i32 0, %x74  %sdiv = sdiv i32 %neg, %y75  %sdiv2 = sdiv i32 %sdiv, %neg76  ret i32 %sdiv277}78 79; There is combination: -(X/CE) -> (X/-CE).80; If combines (X/-CE) to -(X/CE), make sure don't combine them endless.81 82@X = global i32 583 84define 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]]90;91  %v1 = ptrtoint ptr @X to i6492  %B8 = sub nsw i64 0, %v193  %B4 = sdiv i64 %L1, %B894  ret i64 %B495}96 97define i32 @sdiv_abs_nsw(i32 %x) {98; CHECK-LABEL: @sdiv_abs_nsw(99; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], -1100; CHECK-NEXT:    [[R:%.*]] = select i1 [[TMP1]], i32 1, i32 -1101; CHECK-NEXT:    ret i32 [[R]]102;103  %a = call i32 @llvm.abs.i32(i32 %x, i1 true)104  %r = sdiv i32 %a, %x105  ret i32 %r106}107 108define <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:%.*]], splat (i32 -1)111; CHECK-NEXT:    [[R:%.*]] = select <4 x i1> [[TMP1]], <4 x i32> splat (i32 1), <4 x i32> splat (i32 -1)112; CHECK-NEXT:    ret <4 x i32> [[R]]113;114  %a = call <4 x i32> @llvm.abs.v4i32(<4 x i32> %x, i1 true)115  %r = sdiv <4 x i32> %x, %a116  ret <4 x i32> %r117}118 119; Negative test - requires poison int min (nsw)120 121define 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]]126;127  %a = call i32 @llvm.abs.i32(i32 %x, i1 false)128  %r = sdiv i32 %a, %x129  ret i32 %r130}131 132; Negative test133 134define 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]]140;141  %a = call i32 @llvm.abs.i32(i32 %x, i1 true)142  call void @use(i32 %a)143  %r = sdiv i32 %a, %x144  ret i32 %r145}146