InstCombine: Guard the transform introduced in r162743 against large ints and non-const shifts.

llvm-svn: 162751
This commit is contained in:
Benjamin Kramer 2012-08-28 13:08:13 +00:00
parent 50e8a6a7df
commit 9c0a807c27
2 changed files with 37 additions and 12 deletions

View File

@ -464,11 +464,11 @@ Instruction *InstCombiner::visitUDiv(BinaryOperator &I) {
// Udiv ((Lshl x, C1) , C2) -> x / (C2 * 1<<C1);
if (ConstantInt *C2 = dyn_cast<ConstantInt>(Op1)) {
Value *X = 0, *C1 = 0;
if (match(Op0, m_LShr(m_Value(X), m_Value(C1))) && C2->getBitWidth() < 65) {
uint64_t NC = cast<ConstantInt>(C2)->getZExtValue() *
(1<< cast<ConstantInt>(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<<C1);
if (ConstantInt *C2 = dyn_cast<ConstantInt>(Op1)) {
Value *X = 0, *C1 = 0;
if (match(Op0, m_AShr(m_Value(X), m_Value(C1))) && C2->getBitWidth() < 65) {
uint64_t NC = cast<ConstantInt>(C2)->getZExtValue() *
(1<< cast<ConstantInt>(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));
}
}

View File

@ -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
}