diff options
author | Martin Sebor <msebor@redhat.com> | 2016-06-08 19:03:17 +0000 |
---|---|---|
committer | Jakub Jelinek <jakub@gcc.gnu.org> | 2016-06-08 21:03:17 +0200 |
commit | 44a845ca0e592ca187fbab07ac9e055b20fb11cc (patch) | |
tree | 3a15f43474585970a2b22a98bf6ff1b46fcf075f /gcc/doc/extend.texi | |
parent | 379aea728edb345f42b19dfade5529f8f9e8a6da (diff) | |
download | gcc-44a845ca0e592ca187fbab07ac9e055b20fb11cc.zip gcc-44a845ca0e592ca187fbab07ac9e055b20fb11cc.tar.gz gcc-44a845ca0e592ca187fbab07ac9e055b20fb11cc.tar.bz2 |
re PR c++/70507 (integer overflow builtins not constant expressions)
PR c++/70507
PR c/68120
* builtins.def (BUILT_IN_ADD_OVERFLOW_P, BUILT_IN_SUB_OVERFLOW_P,
BUILT_IN_MUL_OVERFLOW_P): New builtins.
* builtins.c: Include gimple-fold.h.
(fold_builtin_arith_overflow): Handle
BUILT_IN_{ADD,SUB,MUL}_OVERFLOW_P.
(fold_builtin_3): Likewise.
* doc/extend.texi (Integer Overflow Builtins): Document
__builtin_{add,sub,mul}_overflow_p.
gcc/c/
* c-typeck.c (convert_arguments): Don't promote last argument
of BUILT_IN_{ADD,SUB,MUL}_OVERFLOW_P.
gcc/cp/
* constexpr.c: Include gimple-fold.h.
(cxx_eval_internal_function): New function.
(cxx_eval_call_expression): Call it.
(potential_constant_expression_1): Handle integer arithmetic
overflow built-ins.
* tree.c (builtin_valid_in_constant_expr_p): Handle
BUILT_IN_{ADD,SUB,MUL}_OVERFLOW_P.
gcc/c-family/
* c-common.c (check_builtin_function_arguments): Handle
BUILT_IN_{ADD,SUB,MUL}_OVERFLOW_P.
gcc/testsuite/
* c-c++-common/builtin-arith-overflow-1.c: Add test cases.
* c-c++-common/builtin-arith-overflow-2.c: New test.
* g++.dg/ext/builtin-arith-overflow-1.C: New test.
* g++.dg/cpp0x/constexpr-arith-overflow.C: New test.
* g++.dg/cpp1y/constexpr-arith-overflow.C: New test.
Co-Authored-By: Jakub Jelinek <jakub@redhat.com>
From-SVN: r237238
Diffstat (limited to 'gcc/doc/extend.texi')
-rw-r--r-- | gcc/doc/extend.texi | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 7208f33..7da516d 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -9869,6 +9869,47 @@ functions above, except they perform multiplication, instead of addition. @end deftypefn +The following built-in functions allow checking if simple arithmetic operation +would overflow. + +@deftypefn {Built-in Function} bool __builtin_add_overflow_p (@var{type1} a, @var{type2} b, @var{type3} c) +@deftypefnx {Built-in Function} bool __builtin_sub_overflow_p (@var{type1} a, @var{type2} b, @var{type3} c) +@deftypefnx {Built-in Function} bool __builtin_mul_overflow_p (@var{type1} a, @var{type2} b, @var{type3} c) + +These built-in functions are similar to @code{__builtin_add_overflow}, +@code{__builtin_sub_overflow}, or @code{__builtin_mul_overflow}, except that +they don't store the result of the arithmetic operation anywhere and the +last argument is not a pointer, but some integral expression. + +The built-in functions promote the first two operands into infinite precision signed type +and perform addition on those promoted operands. The result is then +cast to the type of the third argument. If the cast result is equal to the infinite +precision result, the built-in functions return false, otherwise they return true. +The value of the third argument is ignored, just the side-effects in the third argument +are evaluated, and no integral argument promotions are performed on the last argument. + +For example, the following macro can be used to portably check, at +compile-time, whether or not adding two constant integers will overflow, +and perform the addition only when it is known to be safe and not to trigger +a @option{-Woverflow} warning. + +@smallexample +#define INT_ADD_OVERFLOW_P(a, b) \ + __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0) + +enum @{ + A = INT_MAX, B = 3, + C = INT_ADD_OVERFLOW_P (A, B) ? 0 : A + B, + D = __builtin_add_overflow_p (1, SCHAR_MAX, (signed char) 0) +@}; +@end smallexample + +The compiler will attempt to use hardware instructions to implement +these built-in functions where possible, like conditional jump on overflow +after addition, conditional jump on carry etc. + +@end deftypefn + @node x86 specific memory model extensions for transactional memory @section x86-Specific Memory Model Extensions for Transactional Memory |