mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-12-25 14:32:53 +00:00
593faa53fa
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
149 lines
2.6 KiB
LLVM
149 lines
2.6 KiB
LLVM
; RUN: opt < %s -instsimplify -S | FileCheck %s
|
|
|
|
define i32 @add1(i32 %x) {
|
|
; CHECK: @add1
|
|
; (X + -1) + 1 -> X
|
|
%l = add i32 %x, -1
|
|
%r = add i32 %l, 1
|
|
ret i32 %r
|
|
; CHECK: ret i32 %x
|
|
}
|
|
|
|
define i32 @and1(i32 %x, i32 %y) {
|
|
; CHECK: @and1
|
|
; (X & Y) & X -> X & Y
|
|
%l = and i32 %x, %y
|
|
%r = and i32 %l, %x
|
|
ret i32 %r
|
|
; CHECK: ret i32 %l
|
|
}
|
|
|
|
define i32 @and2(i32 %x, i32 %y) {
|
|
; CHECK: @and2
|
|
; X & (X & Y) -> X & Y
|
|
%r = and i32 %x, %y
|
|
%l = and i32 %x, %r
|
|
ret i32 %l
|
|
; CHECK: ret i32 %r
|
|
}
|
|
|
|
define i32 @or1(i32 %x, i32 %y) {
|
|
; CHECK: @or1
|
|
; (X | Y) | X -> X | Y
|
|
%l = or i32 %x, %y
|
|
%r = or i32 %l, %x
|
|
ret i32 %r
|
|
; CHECK: ret i32 %l
|
|
}
|
|
|
|
define i32 @or2(i32 %x, i32 %y) {
|
|
; CHECK: @or2
|
|
; X | (X | Y) -> X | Y
|
|
%r = or i32 %x, %y
|
|
%l = or i32 %x, %r
|
|
ret i32 %l
|
|
; CHECK: ret i32 %r
|
|
}
|
|
|
|
define i32 @xor1(i32 %x, i32 %y) {
|
|
; CHECK: @xor1
|
|
; (X ^ Y) ^ X = Y
|
|
%l = xor i32 %x, %y
|
|
%r = xor i32 %l, %x
|
|
ret i32 %r
|
|
; CHECK: ret i32 %y
|
|
}
|
|
|
|
define i32 @xor2(i32 %x, i32 %y) {
|
|
; CHECK: @xor2
|
|
; X ^ (X ^ Y) = Y
|
|
%r = xor i32 %x, %y
|
|
%l = xor i32 %x, %r
|
|
ret i32 %l
|
|
; CHECK: ret i32 %y
|
|
}
|
|
|
|
define i32 @sub1(i32 %x, i32 %y) {
|
|
; CHECK: @sub1
|
|
%d = sub i32 %x, %y
|
|
%r = sub i32 %x, %d
|
|
ret i32 %r
|
|
; CHECK: ret i32 %y
|
|
}
|
|
|
|
define i32 @sub2(i32 %x) {
|
|
; CHECK: @sub2
|
|
; X - (X + 1) -> -1
|
|
%xp1 = add i32 %x, 1
|
|
%r = sub i32 %x, %xp1
|
|
ret i32 %r
|
|
; CHECK: ret i32 -1
|
|
}
|
|
|
|
define i32 @sub3(i32 %x, i32 %y) {
|
|
; CHECK: @sub3
|
|
; ((X + 1) + Y) - (Y + 1) -> X
|
|
%xp1 = add i32 %x, 1
|
|
%lhs = add i32 %xp1, %y
|
|
%rhs = add i32 %y, 1
|
|
%r = sub i32 %lhs, %rhs
|
|
ret i32 %r
|
|
; CHECK: ret i32 %x
|
|
}
|
|
|
|
define i32 @sdiv1(i32 %x, i32 %y) {
|
|
; CHECK: @sdiv1
|
|
; (no overflow X * Y) / Y -> X
|
|
%mul = mul nsw i32 %x, %y
|
|
%r = sdiv i32 %mul, %y
|
|
ret i32 %r
|
|
; CHECK: ret i32 %x
|
|
}
|
|
|
|
define i32 @sdiv2(i32 %x, i32 %y) {
|
|
; CHECK: @sdiv2
|
|
; (((X / Y) * Y) / Y) -> X / Y
|
|
%div = sdiv i32 %x, %y
|
|
%mul = mul i32 %div, %y
|
|
%r = sdiv i32 %mul, %y
|
|
ret i32 %r
|
|
; CHECK: ret i32 %div
|
|
}
|
|
|
|
define i32 @sdiv3(i32 %x, i32 %y) {
|
|
; CHECK: @sdiv3
|
|
; (X rem Y) / Y -> 0
|
|
%rem = srem i32 %x, %y
|
|
%div = sdiv i32 %rem, %y
|
|
ret i32 %div
|
|
; CHECK: ret i32 0
|
|
}
|
|
|
|
define i32 @udiv1(i32 %x, i32 %y) {
|
|
; CHECK: @udiv1
|
|
; (no overflow X * Y) / Y -> X
|
|
%mul = mul nuw i32 %x, %y
|
|
%r = udiv i32 %mul, %y
|
|
ret i32 %r
|
|
; CHECK: ret i32 %x
|
|
}
|
|
|
|
define i32 @udiv2(i32 %x, i32 %y) {
|
|
; CHECK: @udiv2
|
|
; (((X / Y) * Y) / Y) -> X / Y
|
|
%div = udiv i32 %x, %y
|
|
%mul = mul i32 %div, %y
|
|
%r = udiv i32 %mul, %y
|
|
ret i32 %r
|
|
; CHECK: ret i32 %div
|
|
}
|
|
|
|
define i32 @udiv3(i32 %x, i32 %y) {
|
|
; CHECK: @udiv3
|
|
; (X rem Y) / Y -> 0
|
|
%rem = urem i32 %x, %y
|
|
%div = udiv i32 %rem, %y
|
|
ret i32 %div
|
|
; CHECK: ret i32 0
|
|
}
|