From 0b46f19a9ecd6215cffb51d19f2403c18b0226f5 Mon Sep 17 00:00:00 2001 From: Simon Pilgrim Date: Thu, 14 Jan 2021 14:50:09 +0000 Subject: [PATCH] [Support] Ensure KnownBits::sextInReg can handle the src == dst sext-in-reg case. This was resulting in assertions inside APInt::zext that we were extending to the same bitwidth. --- llvm/lib/Support/KnownBits.cpp | 6 +++++- llvm/unittests/Support/KnownBitsTest.cpp | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp index 0f36c6a9ef1d..a46a90bb97d4 100644 --- a/llvm/lib/Support/KnownBits.cpp +++ b/llvm/lib/Support/KnownBits.cpp @@ -85,7 +85,11 @@ KnownBits KnownBits::computeForAddSub(bool Add, bool NSW, KnownBits KnownBits::sextInReg(unsigned SrcBitWidth) const { unsigned BitWidth = getBitWidth(); - assert(BitWidth >= SrcBitWidth && "Illegal sext-in-register"); + assert(0 < SrcBitWidth && SrcBitWidth <= BitWidth && + "Illegal sext-in-register"); + + if (SrcBitWidth == BitWidth) + return *this; // Sign extension. Compute the demanded bits in the result that are not // present in the input. diff --git a/llvm/unittests/Support/KnownBitsTest.cpp b/llvm/unittests/Support/KnownBitsTest.cpp index 991096098b8e..4e69df49837e 100644 --- a/llvm/unittests/Support/KnownBitsTest.cpp +++ b/llvm/unittests/Support/KnownBitsTest.cpp @@ -427,7 +427,7 @@ TEST(KnownBitsTest, SExtOrTrunc) { TEST(KnownBitsTest, SExtInReg) { unsigned Bits = 4; - for (unsigned FromBits = 1; FromBits != Bits; ++FromBits) { + for (unsigned FromBits = 1; FromBits <= Bits; ++FromBits) { ForeachKnownBits(Bits, [&](const KnownBits &Known) { APInt CommonOne = APInt::getAllOnesValue(Bits); APInt CommonZero = APInt::getAllOnesValue(Bits);