[llvm] r335512 - [InstSimplify] fold div/rem of zexted bool
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 25 11:51:21 PDT 2018
Author: spatel
Date: Mon Jun 25 11:51:21 2018
New Revision: 335512
URL: http://llvm.org/viewvc/llvm-project?rev=335512&view=rev
Log:
[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
Modified:
llvm/trunk/lib/Analysis/InstructionSimplify.cpp
llvm/trunk/test/Transforms/InstCombine/rem.ll
llvm/trunk/test/Transforms/InstSimplify/div.ll
llvm/trunk/test/Transforms/InstSimplify/rem.ll
Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=335512&r1=335511&r2=335512&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Mon Jun 25 11:51:21 2018
@@ -902,7 +902,10 @@ static Value *simplifyDivRem(Value *Op0,
// 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;
Modified: llvm/trunk/test/Transforms/InstCombine/rem.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/rem.ll?rev=335512&r1=335511&r2=335512&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/rem.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/rem.ll Mon Jun 25 11:51:21 2018
@@ -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
;
Modified: llvm/trunk/test/Transforms/InstSimplify/div.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/div.ll?rev=335512&r1=335511&r2=335512&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/div.ll (original)
+++ llvm/trunk/test/Transforms/InstSimplify/div.ll Mon Jun 25 11:51:21 2018
@@ -97,9 +97,7 @@ define <2 x i1> @udiv_bool_vec(<2 x i1>
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
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
Modified: llvm/trunk/test/Transforms/InstSimplify/rem.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/rem.ll?rev=335512&r1=335511&r2=335512&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/rem.ll (original)
+++ llvm/trunk/test/Transforms/InstSimplify/rem.ll Mon Jun 25 11:51:21 2018
@@ -97,9 +97,7 @@ define <2 x i1> @urem_bool_vec(<2 x i1>
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
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
More information about the llvm-commits
mailing list