Skip to content

Commit

Permalink
InstCombine: Propagate exact for (sdiv -X, C) -> (sdiv X, -C)
Browse files Browse the repository at this point in the history
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@222623 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
majnemer committed Nov 22, 2014
1 parent 53a43d3 commit 91349ee
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1082,10 +1082,12 @@ Instruction *InstCombiner::visitSDiv(BinaryOperator &I) {
return new ZExtInst(Builder->CreateICmpEQ(Op0, Op1), I.getType());

// -X/C --> X/-C provided the negation doesn't overflow.
if (SubOperator *Sub = dyn_cast<SubOperator>(Op0))
if (match(Sub->getOperand(0), m_Zero()) && Sub->hasNoSignedWrap())
return BinaryOperator::CreateSDiv(Sub->getOperand(1),
ConstantExpr::getNeg(RHS));
Value *X;
if (match(Op0, m_NSWSub(m_Zero(), m_Value(X)))) {
auto *BO = BinaryOperator::CreateSDiv(X, ConstantExpr::getNeg(RHS));
BO->setIsExact(I.isExact());
return BO;
}
}

// If the sign bits of both operands are zero (i.e. we can prove they are
Expand Down
9 changes: 9 additions & 0 deletions test/Transforms/InstCombine/div.ll
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,12 @@ define <2 x i64> @test33(<2 x i64> %x) nounwind {
; CHECK-NEXT: udiv exact <2 x i64> %x, <i64 192, i64 192>
; CHECK-NEXT: ret <2 x i64>
}

define <2 x i64> @test34(<2 x i64> %x) nounwind {
%neg = sub nsw <2 x i64> zeroinitializer, %x
%div = sdiv exact <2 x i64> %neg, <i64 3, i64 4>
ret <2 x i64> %div
; CHECK-LABEL: @test34(
; CHECK-NEXT: sdiv exact <2 x i64> %x, <i64 -3, i64 -4>
; CHECK-NEXT: ret <2 x i64>
}

0 comments on commit 91349ee

Please sign in to comment.