From: Benjamin Kramer Date: Tue, 28 Aug 2012 13:08:13 +0000 (+0000) Subject: InstCombine: Guard the transform introduced in r162743 against large ints and non... X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=aac7c650a6cac09d42c5fda8d3761bc9c871ff03;p=oota-llvm.git InstCombine: Guard the transform introduced in r162743 against large ints and non-const shifts. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@162751 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp index 5eba463ec05..65a64b8321b 100644 --- a/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ b/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -464,11 +464,11 @@ Instruction *InstCombiner::visitUDiv(BinaryOperator &I) { // Udiv ((Lshl x, C1) , C2) -> x / (C2 * 1<(Op1)) { - Value *X = 0, *C1 = 0; - if (match(Op0, m_LShr(m_Value(X), m_Value(C1))) && C2->getBitWidth() < 65) { - uint64_t NC = cast(C2)->getZExtValue() * - (1<< cast(C1)->getZExtValue()); - return BinaryOperator::CreateUDiv(X, ConstantInt::get(I.getType(), NC)); + Value *X; + ConstantInt *C1; + if (match(Op0, m_LShr(m_Value(X), m_ConstantInt(C1)))) { + APInt NC = C2->getValue().shl(C1->getZExtValue()); + return BinaryOperator::CreateUDiv(X, Builder->getInt(NC)); } } @@ -545,11 +545,11 @@ Instruction *InstCombiner::visitSDiv(BinaryOperator &I) { // Sdiv ((Ashl x, C1) , C2) -> x / (C2 * 1<(Op1)) { - Value *X = 0, *C1 = 0; - if (match(Op0, m_AShr(m_Value(X), m_Value(C1))) && C2->getBitWidth() < 65) { - uint64_t NC = cast(C2)->getZExtValue() * - (1<< cast(C1)->getZExtValue()); - return BinaryOperator::CreateSDiv(X, ConstantInt::get(I.getType(), NC)); + Value *X; + ConstantInt *C1; + if (match(Op0, m_AShr(m_Value(X), m_ConstantInt(C1)))) { + APInt NC = C2->getValue().shl(C1->getZExtValue()); + return BinaryOperator::CreateSDiv(X, Builder->getInt(NC)); } } diff --git a/test/Transforms/InstCombine/2012-08-28-udiv_ashl.ll b/test/Transforms/InstCombine/2012-08-28-udiv_ashl.ll index 1bede348931..a35a816219a 100644 --- a/test/Transforms/InstCombine/2012-08-28-udiv_ashl.ll +++ b/test/Transforms/InstCombine/2012-08-28-udiv_ashl.ll @@ -49,9 +49,34 @@ entry: ret i32 %div1 } -define i80 @no_crash_i80(i80 %x) { -entry: + +; CHECK: @udiv_i80 +; CHECK: udiv i80 %x, 400 +; CHECK: ret +define i80 @udiv_i80(i80 %x) { %div = lshr i80 %x, 2 %div1 = udiv i80 %div, 100 ret i80 %div1 } + +; CHECK: @sdiv_i80 +; CHECK: sdiv i80 %x, 400 +; CHECK: ret +define i80 @sdiv_i80(i80 %x) { + %div = ashr i80 %x, 2 + %div1 = sdiv i80 %div, 100 + ret i80 %div1 +} + + +define i32 @no_crash_notconst_sdiv(i32 %x, i32 %notconst) { + %div = ashr i32 %x, %notconst + %div1 = sdiv i32 %div, 100 + ret i32 %div1 +} + +define i32 @no_crash_notconst_udiv(i32 %x, i32 %notconst) { + %div = lshr i32 %x, %notconst + %div1 = udiv i32 %div, 100 + ret i32 %div1 +}