diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h index f0e4ee534ece3..adb5ba6e3dcc2 100644 --- a/llvm/include/llvm/Support/MathExtras.h +++ b/llvm/include/llvm/Support/MathExtras.h @@ -635,6 +635,9 @@ std::enable_if_t, T> SubOverflow(T X, T Y, T &Result) { /// result, returning true if an overflow ocurred. template 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); @@ -656,6 +659,7 @@ std::enable_if_t, T> MulOverflow(T X, T Y, T &Result) { return UX > (static_cast(std::numeric_limits::max()) + U(1)) / UY; else return UX > (static_cast(std::numeric_limits::max())) / UY; +#endif } } // namespace llvm