diff options
-rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp | 8 | ||||
-rw-r--r-- | llvm/test/Transforms/InstCombine/div.ll | 8 |
2 files changed, 11 insertions, 5 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp index bb714d00d69..7a8f2c27adc 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -954,9 +954,15 @@ Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { // Op0 / C where C is large (negative) --> zext (Op0 >= C) // TODO: Could use isKnownNegative() to handle non-constant values. + Type *Ty = I.getType(); if (match(Op1, m_Negative())) { Value *Cmp = Builder.CreateICmpUGE(Op0, Op1); - return CastInst::CreateZExtOrBitCast(Cmp, I.getType()); + return CastInst::CreateZExtOrBitCast(Cmp, Ty); + } + // Op0 / (sext i1 X) --> zext (Op0 == -1) (if X is 0, the div is undefined) + if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) { + Value *Cmp = Builder.CreateICmpEQ(Op0, ConstantInt::getAllOnesValue(Ty)); + return CastInst::CreateZExtOrBitCast(Cmp, Ty); } if (Instruction *NarrowDiv = narrowUDivURem(I, Builder)) diff --git a/llvm/test/Transforms/InstCombine/div.ll b/llvm/test/Transforms/InstCombine/div.ll index 4bcf13a14b4..56e887e5b14 100644 --- a/llvm/test/Transforms/InstCombine/div.ll +++ b/llvm/test/Transforms/InstCombine/div.ll @@ -98,8 +98,8 @@ define <2 x i64> @udiv_by_minus1_vec(<2 x i64> %x) { define i32 @udiv_by_sext_all_ones(i1 %x, i32 %y) { ; CHECK-LABEL: @udiv_by_sext_all_ones( -; CHECK-NEXT: [[SEXT:%.*]] = sext i1 [[X:%.*]] to i32 -; CHECK-NEXT: [[DIV:%.*]] = udiv i32 [[Y:%.*]], [[SEXT]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[Y:%.*]], -1 +; CHECK-NEXT: [[DIV:%.*]] = zext i1 [[TMP1]] to i32 ; CHECK-NEXT: ret i32 [[DIV]] ; %sext = sext i1 %x to i32 @@ -109,8 +109,8 @@ define i32 @udiv_by_sext_all_ones(i1 %x, i32 %y) { define <2 x i32> @udiv_by_sext_all_ones_vec(<2 x i1> %x, <2 x i32> %y) { ; CHECK-LABEL: @udiv_by_sext_all_ones_vec( -; CHECK-NEXT: [[SEXT:%.*]] = sext <2 x i1> [[X:%.*]] to <2 x i32> -; CHECK-NEXT: [[DIV:%.*]] = udiv <2 x i32> [[Y:%.*]], [[SEXT]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i32> [[Y:%.*]], <i32 -1, i32 -1> +; CHECK-NEXT: [[DIV:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i32> ; CHECK-NEXT: ret <2 x i32> [[DIV]] ; %sext = sext <2 x i1> %x to <2 x i32> |