For lshr by 0 bits, just return *this as a short cut. This also prevents
authorReid Spencer <rspencer@reidspencer.com>
Thu, 17 May 2007 06:26:29 +0000 (06:26 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Thu, 17 May 2007 06:26:29 +0000 (06:26 +0000)
undefined behavior when the width > 64 bits.

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

lib/Support/APInt.cpp

index 460cf557610c55751d8967e64708b61c2a648d4f..2ed9b07f1ea83edf24b05340b1dbba4b1a305015 100644 (file)
@@ -1149,6 +1149,12 @@ APInt APInt::lshr(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
+  // issues with shifting byt he size of the integer type, which produces 
+  // undefined results in the code below. This is also an optimization.
+  if (shiftAmt == 0)
+    return *this;
+
   // Create some space for the result.
   uint64_t * val = new uint64_t[getNumWords()];