aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Kramer <benny.kra@googlemail.com>2012-08-28 13:08:13 +0000
committerBenjamin Kramer <benny.kra@googlemail.com>2012-08-28 13:08:13 +0000
commitaac7c650a6cac09d42c5fda8d3761bc9c871ff03 (patch)
tree9397d631f2aeda94317ab5f84c34aa2870ff612a
parenta694e2a6910a33596ca706e2c6fc713f02b64c50 (diff)
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
-rw-r--r--lib/Transforms/InstCombine/InstCombineMulDivRem.cpp20
-rw-r--r--test/Transforms/InstCombine/2012-08-28-udiv_ashl.ll29
2 files changed, 37 insertions, 12 deletions
diff --git a/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index 5eba463ec0..65a64b8321 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<<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));
}
}
diff --git a/test/Transforms/InstCombine/2012-08-28-udiv_ashl.ll b/test/Transforms/InstCombine/2012-08-28-udiv_ashl.ll
index 1bede34893..a35a816219 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
+}