[llvm] [Support] Fix the return type of AddOverflow, SubOverflow, and MulOverflow (PR #223217)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 13 00:03:04 PDT 2026


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/223217

This patch fixes the return type of the 3-argument overloads of
AddOverflow, SubOverflow, and MulOverflow to bool.

Without this patch, these functions are declared with a return type
of T:

  template <typename T>
  std::enable_if_t<std::is_signed_v<T>, T> AddOverflow(T X, T Y, T &Result);

even though they return a boolean indicating whether an overflow
occurred, as stated in the comment:

  /// Add two signed integers, computing the two's complement truncated result,
  /// returning true if overflow occurred.

Assisted-by: Antigravity


>From 85ba856ac66c2e44dac36ca3f64cfbfad323644d Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sat, 12 Sep 2026 23:00:18 -0700
Subject: [PATCH] [Support] Fix the return type of AddOverflow, SubOverflow,
 and MulOverflow

This patch fixes the return type of the 3-argument overloads of
AddOverflow, SubOverflow, and MulOverflow to bool.

Without this patch, these functions are declared with a return type
of T:

  template <typename T>
  std::enable_if_t<std::is_signed_v<T>, T> AddOverflow(T X, T Y, T &Result);

even though they return a boolean indicating whether an overflow
occurred, as stated in the comment:

  /// Add two signed integers, computing the two's complement truncated result,
  /// returning true if overflow occurred.

Assisted-by: Antigravity
---
 llvm/include/llvm/Support/MathExtras.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h
index 2324565852815..e41e64fad0db6 100644
--- a/llvm/include/llvm/Support/MathExtras.h
+++ b/llvm/include/llvm/Support/MathExtras.h
@@ -717,7 +717,7 @@ AddOverflow(T X, T Y) {
 /// Add two signed integers, computing the two's complement truncated result,
 /// returning true if overflow occurred.
 template <typename T>
-std::enable_if_t<std::is_signed_v<T>, T> AddOverflow(T X, T Y, T &Result) {
+std::enable_if_t<std::is_signed_v<T>, bool> AddOverflow(T X, T Y, T &Result) {
 #if __has_builtin(__builtin_add_overflow)
   return __builtin_add_overflow(X, Y, &Result);
 #else
@@ -754,7 +754,7 @@ SubOverflow(T X, T Y) {
 /// Subtract two signed integers, computing the two's complement truncated
 /// result, returning true if an overflow occurred.
 template <typename T>
-std::enable_if_t<std::is_signed_v<T>, T> SubOverflow(T X, T Y, T &Result) {
+std::enable_if_t<std::is_signed_v<T>, bool> SubOverflow(T X, T Y, T &Result) {
 #if __has_builtin(__builtin_sub_overflow)
   return __builtin_sub_overflow(X, Y, &Result);
 #else
@@ -798,7 +798,7 @@ MulOverflow(T X, T Y) {
 /// Multiply two signed integers, computing the two's complement truncated
 /// result, returning true if an overflow occurred.
 template <typename T>
-std::enable_if_t<std::is_signed_v<T>, T> MulOverflow(T X, T Y, T &Result) {
+std::enable_if_t<std::is_signed_v<T>, bool> MulOverflow(T X, T Y, T &Result) {
 #if __has_builtin(__builtin_mul_overflow)
   return __builtin_mul_overflow(X, Y, &Result);
 #else



More information about the llvm-commits mailing list