From 69c1f2776efc5b6b413712dc396fa1eb0650f3a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=83=E5=9C=8B=E5=BA=AD?= <37643576+houngkoungting@users.noreply.github.com> Date: Sat, 15 Mar 2025 23:14:33 +0800 Subject: [PATCH 1/6] Update InstCombineShifts.cpp --- llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp index 90cd279e8a457..8404629810918 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp @@ -994,6 +994,11 @@ static bool setShiftFlags(BinaryOperator &I, const SimplifyQuery &Q) { I.setIsExact(); return true; } + // shr X, cttz(X) + if (match(I.getOperand(1), m_Intrinsic(m_Specific(I.getOperand(0))))) { + I.setIsExact(); + return true; + } } // Compute what we know about shift count. From a5e73a03430cf2218aa23a3e89150fb6d474a4be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=83=E5=9C=8B=E5=BA=AD?= <37643576+houngkoungting@users.noreply.github.com> Date: Sat, 15 Mar 2025 23:33:20 +0800 Subject: [PATCH 2/6] Update InstCombineShifts.cpp --- llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp index 8404629810918..e73bd002b8dc0 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp @@ -994,11 +994,7 @@ static bool setShiftFlags(BinaryOperator &I, const SimplifyQuery &Q) { I.setIsExact(); return true; } - // shr X, cttz(X) - if (match(I.getOperand(1), m_Intrinsic(m_Specific(I.getOperand(0))))) { - I.setIsExact(); - return true; - } + } // Compute what we know about shift count. From 2d5181371c2b645b856b9a15c01cbfa86af46b1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=83=E5=9C=8B=E5=BA=AD?= <37643576+houngkoungting@users.noreply.github.com> Date: Tue, 25 Mar 2025 20:57:55 +0800 Subject: [PATCH 3/6] Update InstCombineShifts.cpp --- llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp index e73bd002b8dc0..90cd279e8a457 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp @@ -994,7 +994,6 @@ static bool setShiftFlags(BinaryOperator &I, const SimplifyQuery &Q) { I.setIsExact(); return true; } - } // Compute what we know about shift count. From 9a0d4adee2504b3ddced4f38c0ee4a358e49ef36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=83=E5=9C=8B=E5=BA=AD?= <37643576+houngkoungting@users.noreply.github.com> Date: Sun, 30 Mar 2025 12:10:55 +0800 Subject: [PATCH 4/6] Update MathExtras.h --- llvm/include/llvm/Support/MathExtras.h | 47 ++++++++++++++++---------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h index 519fcc8fde5d5..d66fe4884bc28 100644 --- a/llvm/include/llvm/Support/MathExtras.h +++ b/llvm/include/llvm/Support/MathExtras.h @@ -764,27 +764,40 @@ std::enable_if_t, T> MulOverflow(T X, T Y, T &Result) { #if __has_builtin(__builtin_mul_overflow) return __builtin_mul_overflow(X, Y, &Result); #else - // Perform the unsigned multiplication on absolute values. using U = std::make_unsigned_t; - const U UX = X < 0 ? (0 - static_cast(X)) : static_cast(X); - const U UY = Y < 0 ? (0 - static_cast(Y)) : static_cast(Y); - const U UResult = UX * UY; - // Convert to signed. - const bool IsNegative = (X < 0) ^ (Y < 0); - Result = IsNegative ? (0 - UResult) : UResult; - - // If any of the args was 0, result is 0 and no overflow occurs. - if (UX == 0 || UY == 0) + // Handle zero case + if (X == 0 || Y == 0) { + Result = 0; return false; + } - // UX and UY are in [1, 2^n], where n is the number of digits. - // Check how the max allowed absolute value (2^n for negative, 2^(n-1) for - // positive) divided by an argument compares to the other. - if (IsNegative) - return UX > (static_cast(std::numeric_limits::max()) + U(1)) / UY; - else - return UX > (static_cast(std::numeric_limits::max())) / UY; + bool IsNegative = (X < 0) ^ (Y < 0); + + // Safely compute absolute values + const U AbsX = X < 0 ? (0 - static_cast(X+1))+1 : static_cast(X); + const U AbsY = Y < 0 ? (0 - static_cast(Y+1))+1 : static_cast(Y); + + // Overflow check before actual multiplication + constexpr U MaxPositive = static_cast(std::numeric_limits::max()); + constexpr U MaxNegative = static_cast(std::numeric_limits::max()) + 1; + + // Safe to multiply + U AbsResult = AbsX * AbsY; + Result = IsNegative ? static_cast(0-AbsResult) : static_cast(AbsResult); + + // Handle INT_MIN * -1 overflow case explicitly + if ((X == std::numeric_limits::min() && Y == -1) || + (Y == std::numeric_limits::min() && X == -1)) { + return true; // overflow + } + + U Limit = IsNegative ? MaxNegative : MaxPositive; + + if (AbsX > Limit / AbsY) + return true; + + return false; #endif } From 5307a5705b83f540d43666c180fdc31fe9dca0e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=83=E5=9C=8B=E5=BA=AD?= <37643576+houngkoungting@users.noreply.github.com> Date: Sun, 30 Mar 2025 13:39:06 +0800 Subject: [PATCH 5/6] Update MathExtras.h --- llvm/include/llvm/Support/MathExtras.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h index d66fe4884bc28..d0ed2521ef9bf 100644 --- a/llvm/include/llvm/Support/MathExtras.h +++ b/llvm/include/llvm/Support/MathExtras.h @@ -760,7 +760,7 @@ std::enable_if_t, T> SubOverflow(T X, T Y, T &Result) { /// Multiply two signed integers, computing the two's complement truncated /// result, returning true if an overflow occurred. template -std::enable_if_t, T> MulOverflow(T X, T Y, T &Result) { +std::enable_if_t, bool> MulOverflow(T X, T Y, T &Result) { #if __has_builtin(__builtin_mul_overflow) return __builtin_mul_overflow(X, Y, &Result); #else @@ -775,23 +775,26 @@ std::enable_if_t, T> MulOverflow(T X, T Y, T &Result) { bool IsNegative = (X < 0) ^ (Y < 0); // Safely compute absolute values - const U AbsX = X < 0 ? (0 - static_cast(X+1))+1 : static_cast(X); - const U AbsY = Y < 0 ? (0 - static_cast(Y+1))+1 : static_cast(Y); + const U AbsX = X < 0 ? (0 - static_cast(X)) : static_cast(X); + const U AbsY = Y < 0 ? (0 - static_cast(Y)) : static_cast(Y); // Overflow check before actual multiplication constexpr U MaxPositive = static_cast(std::numeric_limits::max()); constexpr U MaxNegative = static_cast(std::numeric_limits::max()) + 1; - + // Safe to multiply U AbsResult = AbsX * AbsY; Result = IsNegative ? static_cast(0-AbsResult) : static_cast(AbsResult); + + // Handle INT_MIN * -1 overflow case explicitly if ((X == std::numeric_limits::min() && Y == -1) || (Y == std::numeric_limits::min() && X == -1)) { return true; // overflow } + U Limit = IsNegative ? MaxNegative : MaxPositive; if (AbsX > Limit / AbsY) From e9732627ec5f1cac01c356287914aa90764afe44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=83=E5=9C=8B=E5=BA=AD?= <37643576+houngkoungting@users.noreply.github.com> Date: Sun, 30 Mar 2025 13:42:36 +0800 Subject: [PATCH 6/6] Update MathExtras.h --- llvm/include/llvm/Support/MathExtras.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h index d0ed2521ef9bf..fe6db8fe14019 100644 --- a/llvm/include/llvm/Support/MathExtras.h +++ b/llvm/include/llvm/Support/MathExtras.h @@ -786,15 +786,12 @@ std::enable_if_t, bool> MulOverflow(T X, T Y, T &Result) { U AbsResult = AbsX * AbsY; Result = IsNegative ? static_cast(0-AbsResult) : static_cast(AbsResult); - - // Handle INT_MIN * -1 overflow case explicitly if ((X == std::numeric_limits::min() && Y == -1) || (Y == std::numeric_limits::min() && X == -1)) { return true; // overflow } - U Limit = IsNegative ? MaxNegative : MaxPositive; if (AbsX > Limit / AbsY)