Index: lib/Support/APInt.cpp =================================================================== --- lib/Support/APInt.cpp +++ lib/Support/APInt.cpp @@ -1043,9 +1043,11 @@ if (shiftAmt == BitWidth) return APInt(BitWidth, 0); // undefined else { - unsigned SignBit = APINT_BITS_PER_WORD - BitWidth; - return APInt(BitWidth, - (((int64_t(VAL) << SignBit) >> SignBit) >> shiftAmt)); + if (unsigned SignBit = APINT_BITS_PER_WORD - BitWidth) + return APInt(BitWidth, + (((int64_t(VAL) << SignBit) >> SignBit) >> shiftAmt)); + else + return APInt(BitWidth, int64_t(VAL) >> shiftAmt); } } Index: unittests/ADT/APIntTest.cpp =================================================================== --- unittests/ADT/APIntTest.cpp +++ unittests/ADT/APIntTest.cpp @@ -32,6 +32,11 @@ EXPECT_FALSE(Shl[1]); } +TEST(APIntTest, i64_ArithmeticRightShiftNegative) { + const APInt neg_one(64, static_cast(-1), true); + EXPECT_EQ(neg_one, neg_one.ashr(7)); +} + TEST(APIntTest, i128_NegativeCount) { APInt Minus3(128, static_cast(-3), true); EXPECT_EQ(126u, Minus3.countLeadingOnes());