llvm-6502/test/Transforms/InstCombine/2008-11-20-DivMulRem.ll
Duncan Sands 593faa53fa My auto-simplifier noticed that ((X/Y)*Y)/Y occurs several times in SPEC
benchmarks, and that it can be simplified to X/Y.  (In general you can only
simplify (Z*Y)/Y to Z if the multiplication did not overflow; if Z has the
form "X/Y" then this is the case).  This patch implements that transform and
moves some Div logic out of instcombine and into InstructionSimplify.
Unfortunately instcombine gets in the way somewhat, since it likes to change
(X/Y)*Y into X-(X rem Y), so I had to teach instcombine about this too.
Finally, thanks to the NSW/NUW flags, sometimes we know directly that "Z*Y"
does not overflow, because the flag says so, so I added that logic too.  This
eliminates a bunch of divisions and subtractions in 447.dealII, and has good
effects on some other benchmarks too.  It seems to have quite an effect on
tramp3d-v4 but it's hard to say if it's good or bad because inlining decisions
changed, resulting in massive changes all over.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@124487 91177308-0d34-0410-b5e6-96231b3b80d8
2011-01-28 16:51:11 +00:00

68 lines
1.2 KiB
LLVM

; RUN: opt < %s -instcombine -S | FileCheck %s
; PR3103
define i8 @test1(i8 %x, i8 %y) {
; CHECK: @test1
%A = udiv i8 %x, %y
; CHECK-NEXT: urem
%B = mul i8 %A, %y
%C = sub i8 %x, %B
ret i8 %C
; CHECK-NEXT: ret
}
define i8 @test2(i8 %x, i8 %y) {
; CHECK: @test2
%A = sdiv i8 %x, %y
; CHECK-NEXT: srem
%B = mul i8 %A, %y
%C = sub i8 %x, %B
ret i8 %C
; CHECK-NEXT: ret
}
define i8 @test3(i8 %x, i8 %y) {
; CHECK: @test3
%A = udiv i8 %x, %y
; CHECK-NEXT: urem
%B = mul i8 %A, %y
%C = sub i8 %B, %x
; CHECK-NEXT: sub
ret i8 %C
; CHECK-NEXT: ret
}
define i8 @test4(i8 %x) {
; CHECK: @test4
%A = udiv i8 %x, 3
; CHECK-NEXT: urem
%B = mul i8 %A, -3
; CHECK-NEXT: sub
%C = sub i8 %x, %B
; CHECK-NEXT: add
ret i8 %C
; CHECK-NEXT: ret
}
define i32 @test5(i32 %x, i32 %y) {
; CHECK: @test5
; (((X / Y) * Y) / Y) -> X / Y
%div = sdiv i32 %x, %y
; CHECK-NEXT: sdiv
%mul = mul i32 %div, %y
%r = sdiv i32 %mul, %y
ret i32 %r
; CHECK-NEXT: ret
}
define i32 @test6(i32 %x, i32 %y) {
; CHECK: @test6
; (((X / Y) * Y) / Y) -> X / Y
%div = udiv i32 %x, %y
; CHECK-NEXT: udiv
%mul = mul i32 %div, %y
%r = udiv i32 %mul, %y
ret i32 %r
; CHECK-NEXT: ret
}