InstCombine: Guard the transform introduced in r162743 against large ints and non...
authorBenjamin Kramer <benny.kra@googlemail.com>
Tue, 28 Aug 2012 13:08:13 +0000 (13:08 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Tue, 28 Aug 2012 13:08:13 +0000 (13:08 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@162751 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
test/Transforms/InstCombine/2012-08-28-udiv_ashl.ll

index 5eba463ec05d1500c6b87d0e1e98fc4732648964..65a64b8321b319d05c824bd9428892107892470c 100644 (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));
     }
   }
 
index 1bede3489314a2d8ed1a006cbaad914002929b83..a35a816219a0517e9ebeb4b783d1d8432a472af9 100644 (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
+}