2010-12-21 16:49:00 +08:00
|
|
|
; RUN: opt < %s -instsimplify -S | FileCheck %s
|
|
|
|
|
|
|
|
define i32 @add1(i32 %x) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @add1(
|
2010-12-21 16:49:00 +08:00
|
|
|
; (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) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @and1(
|
2010-12-21 16:49:00 +08:00
|
|
|
; (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) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @and2(
|
2010-12-21 16:49:00 +08:00
|
|
|
; 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) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @or1(
|
2010-12-21 16:49:00 +08:00
|
|
|
; (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) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @or2(
|
2010-12-21 16:49:00 +08:00
|
|
|
; 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) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @xor1(
|
2010-12-21 16:49:00 +08:00
|
|
|
; (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) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @xor2(
|
2010-12-21 16:49:00 +08:00
|
|
|
; X ^ (X ^ Y) = Y
|
|
|
|
%r = xor i32 %x, %y
|
|
|
|
%l = xor i32 %x, %r
|
|
|
|
ret i32 %l
|
|
|
|
; CHECK: ret i32 %y
|
|
|
|
}
|
2011-01-14 23:26:10 +08:00
|
|
|
|
|
|
|
define i32 @sub1(i32 %x, i32 %y) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @sub1(
|
2011-01-14 23:26:10 +08:00
|
|
|
%d = sub i32 %x, %y
|
|
|
|
%r = sub i32 %x, %d
|
|
|
|
ret i32 %r
|
|
|
|
; CHECK: ret i32 %y
|
|
|
|
}
|
2011-01-18 19:50:19 +08:00
|
|
|
|
|
|
|
define i32 @sub2(i32 %x) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @sub2(
|
2011-01-18 19:50:19 +08:00
|
|
|
; 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) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @sub3(
|
2011-01-18 19:50:19 +08:00
|
|
|
; ((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
|
|
|
|
}
|
2011-01-29 00:51:11 +08:00
|
|
|
|
|
|
|
define i32 @sdiv1(i32 %x, i32 %y) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @sdiv1(
|
2011-01-29 00:51:11 +08:00
|
|
|
; (no overflow X * Y) / Y -> X
|
|
|
|
%mul = mul nsw i32 %x, %y
|
|
|
|
%r = sdiv i32 %mul, %y
|
|
|
|
ret i32 %r
|
2011-02-03 04:52:00 +08:00
|
|
|
; CHECK: ret i32 %x
|
2011-01-29 00:51:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
define i32 @sdiv2(i32 %x, i32 %y) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @sdiv2(
|
2011-01-29 00:51:11 +08:00
|
|
|
; (((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) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @sdiv3(
|
2011-01-29 00:51:11 +08:00
|
|
|
; (X rem Y) / Y -> 0
|
|
|
|
%rem = srem i32 %x, %y
|
|
|
|
%div = sdiv i32 %rem, %y
|
|
|
|
ret i32 %div
|
|
|
|
; CHECK: ret i32 0
|
|
|
|
}
|
|
|
|
|
2011-01-31 02:03:50 +08:00
|
|
|
define i32 @sdiv4(i32 %x, i32 %y) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @sdiv4(
|
2011-01-31 02:03:50 +08:00
|
|
|
; (X / Y) * Y -> X if the division is exact
|
|
|
|
%div = sdiv exact i32 %x, %y
|
|
|
|
%mul = mul i32 %div, %y
|
|
|
|
ret i32 %mul
|
|
|
|
; CHECK: ret i32 %x
|
|
|
|
}
|
|
|
|
|
|
|
|
define i32 @sdiv5(i32 %x, i32 %y) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @sdiv5(
|
2011-01-31 02:03:50 +08:00
|
|
|
; Y * (X / Y) -> X if the division is exact
|
|
|
|
%div = sdiv exact i32 %x, %y
|
|
|
|
%mul = mul i32 %y, %div
|
|
|
|
ret i32 %mul
|
|
|
|
; CHECK: ret i32 %x
|
|
|
|
}
|
|
|
|
|
2011-02-07 06:05:31 +08:00
|
|
|
|
2011-01-29 00:51:11 +08:00
|
|
|
define i32 @udiv1(i32 %x, i32 %y) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @udiv1(
|
2011-01-29 00:51:11 +08:00
|
|
|
; (no overflow X * Y) / Y -> X
|
|
|
|
%mul = mul nuw i32 %x, %y
|
|
|
|
%r = udiv i32 %mul, %y
|
|
|
|
ret i32 %r
|
2011-02-03 04:52:00 +08:00
|
|
|
; CHECK: ret i32 %x
|
2011-01-29 00:51:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
define i32 @udiv2(i32 %x, i32 %y) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @udiv2(
|
2011-01-29 00:51:11 +08:00
|
|
|
; (((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) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @udiv3(
|
2011-01-29 00:51:11 +08:00
|
|
|
; (X rem Y) / Y -> 0
|
|
|
|
%rem = urem i32 %x, %y
|
|
|
|
%div = udiv i32 %rem, %y
|
|
|
|
ret i32 %div
|
|
|
|
; CHECK: ret i32 0
|
|
|
|
}
|
2011-02-07 06:05:31 +08:00
|
|
|
|
|
|
|
define i32 @udiv4(i32 %x, i32 %y) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @udiv4(
|
2011-02-07 06:05:31 +08:00
|
|
|
; (X / Y) * Y -> X if the division is exact
|
|
|
|
%div = udiv exact i32 %x, %y
|
|
|
|
%mul = mul i32 %div, %y
|
|
|
|
ret i32 %mul
|
|
|
|
; CHECK: ret i32 %x
|
|
|
|
}
|
|
|
|
|
|
|
|
define i32 @udiv5(i32 %x, i32 %y) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @udiv5(
|
2011-02-07 06:05:31 +08:00
|
|
|
; Y * (X / Y) -> X if the division is exact
|
|
|
|
%div = udiv exact i32 %x, %y
|
|
|
|
%mul = mul i32 %y, %div
|
|
|
|
ret i32 %mul
|
|
|
|
; CHECK: ret i32 %x
|
|
|
|
}
|
|
|
|
|
2012-03-13 22:07:05 +08:00
|
|
|
define i16 @trunc1(i32 %x) {
|
2013-07-14 09:42:54 +08:00
|
|
|
; CHECK-LABEL: @trunc1(
|
2012-03-13 22:07:05 +08:00
|
|
|
%y = add i32 %x, 1
|
|
|
|
%tx = trunc i32 %x to i16
|
|
|
|
%ty = trunc i32 %y to i16
|
|
|
|
%d = sub i16 %ty, %tx
|
|
|
|
ret i16 %d
|
|
|
|
; CHECK: ret i16 1
|
|
|
|
}
|