[InstSimplify] fold div/rem of zexted bool

I was looking at an unrelated fold and noticed that
we don't have this simplification (because the other
fold would break existing tests).

Name: zext udiv
  %z = zext i1 %x to i32
  %r = udiv i32 %y, %z
=>
  %r = %y

Name: zext urem
  %z = zext i1 %x to i32
  %r = urem i32 %y, %z
=>
  %r = 0

Name: zext sdiv
  %z = zext i1 %x to i32
  %r = sdiv i32 %y, %z
=>
  %r = %y

Name: zext srem
  %z = zext i1 %x to i32
  %r = srem i32 %y, %z
=>
  %r = 0

https://rise4fun.com/Alive/LZ9

llvm-svn: 335512
This commit is contained in:
Sanjay Patel 2018-06-25 18:51:21 +00:00
parent a8448ad098
commit 1e911fa746
4 changed files with 10 additions and 19 deletions

View File

@ -902,7 +902,10 @@ static Value *simplifyDivRem(Value *Op0, Value *Op1, bool IsDiv) {
// X % 1 -> 0
// If this is a boolean op (single-bit element type), we can't have
// division-by-zero or remainder-by-zero, so assume the divisor is 1.
if (match(Op1, m_One()) || Ty->isIntOrIntVectorTy(1))
// Similarly, if we're zero-extending a boolean divisor, then assume it's a 1.
Value *X;
if (match(Op1, m_One()) || Ty->isIntOrIntVectorTy(1) ||
(match(Op1, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)))
return IsDiv ? Op0 : Constant::getNullValue(Ty);
return nullptr;

View File

@ -485,11 +485,9 @@ define i32 @pr27968_0(i1 %c0, i32* %p) {
; CHECK-NEXT: [[V:%.*]] = load volatile i32, i32* [[P:%.*]], align 4
; CHECK-NEXT: br label [[IF_END]]
; CHECK: if.end:
; CHECK-NEXT: [[LHS:%.*]] = phi i32 [ [[V]], [[IF_THEN]] ], [ 5, [[ENTRY:%.*]] ]
; CHECK-NEXT: br i1 icmp eq (i16* getelementptr inbounds ([5 x i16], [5 x i16]* @a, i64 0, i64 4), i16* @b), label [[REM_IS_SAFE:%.*]], label [[REM_IS_UNSAFE:%.*]]
; CHECK: rem.is.safe:
; CHECK-NEXT: [[REM:%.*]] = srem i32 [[LHS]], zext (i1 icmp eq (i16* getelementptr inbounds ([5 x i16], [5 x i16]* @a, i64 0, i64 4), i16* @b) to i32)
; CHECK-NEXT: ret i32 [[REM]]
; CHECK-NEXT: ret i32 0
; CHECK: rem.is.unsafe:
; CHECK-NEXT: ret i32 0
;
@ -555,11 +553,9 @@ define i32 @pr27968_2(i1 %c0, i32* %p) {
; CHECK-NEXT: [[V:%.*]] = load volatile i32, i32* [[P:%.*]], align 4
; CHECK-NEXT: br label [[IF_END]]
; CHECK: if.end:
; CHECK-NEXT: [[LHS:%.*]] = phi i32 [ [[V]], [[IF_THEN]] ], [ 5, [[ENTRY:%.*]] ]
; CHECK-NEXT: br i1 icmp eq (i16* getelementptr inbounds ([5 x i16], [5 x i16]* @a, i64 0, i64 4), i16* @b), label [[REM_IS_SAFE:%.*]], label [[REM_IS_UNSAFE:%.*]]
; CHECK: rem.is.safe:
; CHECK-NEXT: [[REM:%.*]] = urem i32 [[LHS]], zext (i1 icmp eq (i16* getelementptr inbounds ([5 x i16], [5 x i16]* @a, i64 0, i64 4), i16* @b) to i32)
; CHECK-NEXT: ret i32 [[REM]]
; CHECK-NEXT: ret i32 0
; CHECK: rem.is.unsafe:
; CHECK-NEXT: ret i32 0
;

View File

@ -97,9 +97,7 @@ define <2 x i1> @udiv_bool_vec(<2 x i1> %x, <2 x i1> %y) {
define i32 @zext_bool_udiv_divisor(i1 %x, i32 %y) {
; CHECK-LABEL: @zext_bool_udiv_divisor(
; CHECK-NEXT: [[EXT:%.*]] = zext i1 [[X:%.*]] to i32
; CHECK-NEXT: [[R:%.*]] = udiv i32 [[Y:%.*]], [[EXT]]
; CHECK-NEXT: ret i32 [[R]]
; CHECK-NEXT: ret i32 [[Y:%.*]]
;
%ext = zext i1 %x to i32
%r = udiv i32 %y, %ext
@ -108,9 +106,7 @@ define i32 @zext_bool_udiv_divisor(i1 %x, i32 %y) {
define <2 x i32> @zext_bool_sdiv_divisor_vec(<2 x i1> %x, <2 x i32> %y) {
; CHECK-LABEL: @zext_bool_sdiv_divisor_vec(
; CHECK-NEXT: [[EXT:%.*]] = zext <2 x i1> [[X:%.*]] to <2 x i32>
; CHECK-NEXT: [[R:%.*]] = sdiv <2 x i32> [[Y:%.*]], [[EXT]]
; CHECK-NEXT: ret <2 x i32> [[R]]
; CHECK-NEXT: ret <2 x i32> [[Y:%.*]]
;
%ext = zext <2 x i1> %x to <2 x i32>
%r = sdiv <2 x i32> %y, %ext

View File

@ -97,9 +97,7 @@ define <2 x i1> @urem_bool_vec(<2 x i1> %x, <2 x i1> %y) {
define <2 x i32> @zext_bool_urem_divisor_vec(<2 x i1> %x, <2 x i32> %y) {
; CHECK-LABEL: @zext_bool_urem_divisor_vec(
; CHECK-NEXT: [[EXT:%.*]] = zext <2 x i1> [[X:%.*]] to <2 x i32>
; CHECK-NEXT: [[R:%.*]] = urem <2 x i32> [[Y:%.*]], [[EXT]]
; CHECK-NEXT: ret <2 x i32> [[R]]
; CHECK-NEXT: ret <2 x i32> zeroinitializer
;
%ext = zext <2 x i1> %x to <2 x i32>
%r = urem <2 x i32> %y, %ext
@ -108,9 +106,7 @@ define <2 x i32> @zext_bool_urem_divisor_vec(<2 x i1> %x, <2 x i32> %y) {
define i32 @zext_bool_srem_divisor(i1 %x, i32 %y) {
; CHECK-LABEL: @zext_bool_srem_divisor(
; CHECK-NEXT: [[EXT:%.*]] = zext i1 [[X:%.*]] to i32
; CHECK-NEXT: [[R:%.*]] = srem i32 [[Y:%.*]], [[EXT]]
; CHECK-NEXT: ret i32 [[R]]
; CHECK-NEXT: ret i32 0
;
%ext = zext i1 %x to i32
%r = srem i32 %y, %ext