Fix shl to produce the correct result when the bitwidth is > 64 and the
authorReid Spencer <rspencer@reidspencer.com>
Sat, 12 May 2007 18:01:57 +0000 (18:01 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Sat, 12 May 2007 18:01:57 +0000 (18:01 +0000)
shift amount is 0. Previously this code would do a lshr by the bit width
which can lead to incorrect results.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@37010 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/APInt.cpp

index 4142c6ec8bc4a495f9178617ad61871737d8d3c0..2a35aa057ef3e54be32c25c187a409d6f4a5ca7c 100644 (file)
@@ -1199,6 +1199,12 @@ APInt APInt::shl(uint32_t shiftAmt) const {
   if (shiftAmt == BitWidth)
     return APInt(BitWidth, 0);
 
+  // If none of the bits are shifted out, the result is *this. This avoids a
+  // lshr by the words size in the loop below which can produce incorrect
+  // results. It also avoids the expensive computation below for a common case.
+  if (shiftAmt == 0)
+    return *this;
+
   // Create some space for the result.
   uint64_t * val = new uint64_t[getNumWords()];