[PATCH] D65536: [ConstExprPreter] Overflow-detecting methods use GCC or clang builtins
Nandor Licker via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 31 13:32:55 PDT 2019
nand created this revision.
nand added a reviewer: jfb.
Herald added subscribers: llvm-commits, dexonsmith.
Herald added a project: LLVM.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D65536
Files:
llvm/include/llvm/Support/MathExtras.h
Index: llvm/include/llvm/Support/MathExtras.h
===================================================================
--- llvm/include/llvm/Support/MathExtras.h
+++ llvm/include/llvm/Support/MathExtras.h
@@ -860,6 +860,9 @@
template <typename T>
typename std::enable_if<std::is_signed<T>::value, T>::type
AddOverflow(T X, T Y, T &Result) {
+#if __has_builtin(__builtin_add_overflow) || LLVM_GNUC_PREREQ(4, 0, 0)
+ return __builtin_add_overflow(X, Y, &Result);
+#else
// Perform the unsigned addition.
using U = typename std::make_unsigned<T>::type;
const U UX = static_cast<U>(X);
@@ -876,6 +879,7 @@
if (X < 0 && Y < 0)
return Result >= 0;
return false;
+#endif
}
/// Subtract two signed integers, computing the two's complement truncated
@@ -883,6 +887,9 @@
template <typename T>
typename std::enable_if<std::is_signed<T>::value, T>::type
SubOverflow(T X, T Y, T &Result) {
+#if __has_builtin(__builtin_sub_overflow) || LLVM_GNUC_PREREQ(4, 0, 0)
+ return __builtin_sub_overflow(X, Y, &Result);
+#else
// Perform the unsigned addition.
using U = typename std::make_unsigned<T>::type;
const U UX = static_cast<U>(X);
@@ -899,6 +906,7 @@
if (X >= 0 && Y < 0)
return Result <= 0;
return false;
+#endif
}
@@ -907,6 +915,9 @@
template <typename T>
typename std::enable_if<std::is_signed<T>::value, T>::type
MulOverflow(T X, T Y, T &Result) {
+#if __has_builtin(__builtin_mul_overflow) || LLVM_GNUC_PREREQ(4, 0, 0)
+ return __builtin_mul_overflow(X, Y, &Result);
+#else
// Perform the unsigned multiplication on absolute values.
using U = typename std::make_unsigned<T>::type;
const U UX = X < 0 ? (0 - static_cast<U>(X)) : static_cast<U>(X);
@@ -928,6 +939,7 @@
return UX > (static_cast<U>(std::numeric_limits<T>::max()) + U(1)) / UY;
else
return UX > (static_cast<U>(std::numeric_limits<T>::max())) / UY;
+#endif
}
} // End llvm namespace
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65536.212655.patch
Type: text/x-patch
Size: 1919 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190731/d2702a71/attachment.bin>
More information about the llvm-commits
mailing list