[PATCH] Fix big shift constant folding

Paweł Bylica chfast at gmail.com
Tue Feb 17 12:14:32 PST 2015


Constant folding for shift IR instructions ignores all bits above 32 of second argument (shift amount). Because of that, some undef results are not recognized and APInt can raise an assert failure if second argument has more than 64 bits.

REPOSITORY
  rL LLVM

http://reviews.llvm.org/D7701

Files:
  lib/IR/ConstantFold.cpp

Index: lib/IR/ConstantFold.cpp
===================================================================
--- lib/IR/ConstantFold.cpp
+++ lib/IR/ConstantFold.cpp
@@ -1120,27 +1120,18 @@
         return ConstantInt::get(CI1->getContext(), C1V | C2V);
       case Instruction::Xor:
         return ConstantInt::get(CI1->getContext(), C1V ^ C2V);
-      case Instruction::Shl: {
-        uint32_t shiftAmt = C2V.getZExtValue();
-        if (shiftAmt < C1V.getBitWidth())
-          return ConstantInt::get(CI1->getContext(), C1V.shl(shiftAmt));
-        else
+      case Instruction::Shl:
+        if (C2V.getActiveBits() > 32 || C2V.getZExtValue() >= C1V.getBitWidth())
           return UndefValue::get(C1->getType()); // too big shift is undef
-      }
-      case Instruction::LShr: {
-        uint32_t shiftAmt = C2V.getZExtValue();
-        if (shiftAmt < C1V.getBitWidth())
-          return ConstantInt::get(CI1->getContext(), C1V.lshr(shiftAmt));
-        else
+        return ConstantInt::get(CI1->getContext(), C1V.shl(C2V.getZExtValue()));
+      case Instruction::LShr:
+        if (C2V.getActiveBits() > 32 || C2V.getZExtValue() >= C1V.getBitWidth())
           return UndefValue::get(C1->getType()); // too big shift is undef
-      }
-      case Instruction::AShr: {
-        uint32_t shiftAmt = C2V.getZExtValue();
-        if (shiftAmt < C1V.getBitWidth())
-          return ConstantInt::get(CI1->getContext(), C1V.ashr(shiftAmt));
-        else
+        return ConstantInt::get(CI1->getContext(), C1V.lshr(C2V.getZExtValue()));
+      case Instruction::AShr:
+        if (C2V.getActiveBits() > 32 || C2V.getZExtValue() >= C1V.getBitWidth())
           return UndefValue::get(C1->getType()); // too big shift is undef
-      }
+        return ConstantInt::get(CI1->getContext(), C1V.ashr(C2V.getZExtValue()));
       }
     }

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D7701.20101.patch
Type: text/x-patch
Size: 1836 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150217/7664ef73/attachment.bin>


More information about the llvm-commits mailing list