[llvm] r335527 - [InstCombine] fold sdiv with sext bool divisor
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 25 14:39:41 PDT 2018
Author: spatel
Date: Mon Jun 25 14:39:41 2018
New Revision: 335527
URL: http://llvm.org/viewvc/llvm-project?rev=335527&view=rev
Log:
[InstCombine] fold sdiv with sext bool divisor
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
llvm/trunk/test/Transforms/InstCombine/div.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp?rev=335527&r1=335526&r2=335527&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp Mon Jun 25 14:39:41 2018
@@ -1019,12 +1019,15 @@ Instruction *InstCombiner::visitSDiv(Bin
return Common;
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
+ Value *X;
+ // sdiv Op0, -1 --> -Op0
+ // sdiv Op0, (sext i1 X) --> -Op0 (because if X is 0, the op is undefined)
+ if (match(Op1, m_AllOnes()) ||
+ (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)))
+ return BinaryOperator::CreateNeg(Op0);
+
const APInt *Op1C;
if (match(Op1, m_APInt(Op1C))) {
- // sdiv X, -1 == -X
- if (Op1C->isAllOnesValue())
- return BinaryOperator::CreateNeg(Op0);
-
// sdiv exact X, C --> ashr exact X, log2(C)
if (I.isExact() && Op1C->isNonNegative() && Op1C->isPowerOf2()) {
Value *ShAmt = ConstantInt::get(Op1->getType(), Op1C->exactLogBase2());
Modified: llvm/trunk/test/Transforms/InstCombine/div.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/div.ll?rev=335527&r1=335526&r2=335527&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/div.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/div.ll Mon Jun 25 14:39:41 2018
@@ -48,8 +48,7 @@ define <2 x i64> @sdiv_by_minus1_vec_und
define i32 @sdiv_by_sext_minus1(i1 %x, i32 %y) {
; CHECK-LABEL: @sdiv_by_sext_minus1(
-; CHECK-NEXT: [[SEXT:%.*]] = sext i1 [[X:%.*]] to i32
-; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[Y:%.*]], [[SEXT]]
+; CHECK-NEXT: [[DIV:%.*]] = sub i32 0, [[Y:%.*]]
; CHECK-NEXT: ret i32 [[DIV]]
;
%sext = sext i1 %x to i32
@@ -59,8 +58,7 @@ define i32 @sdiv_by_sext_minus1(i1 %x, i
define <2 x i32> @sdiv_by_sext_minus1_vec(<2 x i1> %x, <2 x i32> %y) {
; CHECK-LABEL: @sdiv_by_sext_minus1_vec(
-; CHECK-NEXT: [[SEXT:%.*]] = sext <2 x i1> [[X:%.*]] to <2 x i32>
-; CHECK-NEXT: [[DIV:%.*]] = sdiv <2 x i32> [[Y:%.*]], [[SEXT]]
+; CHECK-NEXT: [[DIV:%.*]] = sub <2 x i32> zeroinitializer, [[Y:%.*]]
; CHECK-NEXT: ret <2 x i32> [[DIV]]
;
%sext = sext <2 x i1> %x to <2 x i32>
More information about the llvm-commits
mailing list