From 00085092c5e78d076c4259e1f955ce1b98090624 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Fri, 24 Jun 2016 13:04:29 +0200 Subject: call.c (magic_varargs_p): Return 3 for __builtin_*_overflow_p. * call.c (magic_varargs_p): Return 3 for __builtin_*_overflow_p. (build_over_call): For magic == 3, do no conversion only on 3rd argument. * c-c++-common/torture/builtin-arith-overflow-p-19.c: Run for C++ too. * g++.dg/ext/builtin-arith-overflow-2.C: New test. From-SVN: r237755 --- gcc/cp/ChangeLog | 6 +++ gcc/cp/call.c | 12 +++-- gcc/testsuite/ChangeLog | 3 ++ .../torture/builtin-arith-overflow-p-19.c | 2 +- .../g++.dg/ext/builtin-arith-overflow-2.C | 53 ++++++++++++++++++++++ 5 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/ext/builtin-arith-overflow-2.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 9ef7d4e..78dd88f 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2016-06-24 Jakub Jelinek + + * call.c (magic_varargs_p): Return 3 for __builtin_*_overflow_p. + (build_over_call): For magic == 3, do no conversion only on 3rd + argument. + 2016-06-23 Andi Kleen * Make-lang.in: Add support for autofdo. diff --git a/gcc/cp/call.c b/gcc/cp/call.c index b739fa0..d77092b 100644 --- a/gcc/cp/call.c +++ b/gcc/cp/call.c @@ -7132,7 +7132,8 @@ convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain) which just decay_conversion or no conversions at all should be done. This is true for some builtins which don't act like normal functions. Return 2 if no conversions at all should be done, 1 if just - decay_conversion. */ + decay_conversion. Return 3 for special treatment of the 3rd argument + for __builtin_*_overflow_p. */ int magic_varargs_p (tree fn) @@ -7149,6 +7150,11 @@ magic_varargs_p (tree fn) case BUILT_IN_VA_START: return 1; + case BUILT_IN_ADD_OVERFLOW_P: + case BUILT_IN_SUB_OVERFLOW_P: + case BUILT_IN_MUL_OVERFLOW_P: + return 3; + default:; return lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0; @@ -7606,14 +7612,14 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain) for (; arg_index < vec_safe_length (args); ++arg_index) { tree a = (*args)[arg_index]; - if (magic == 2) + if ((magic == 3 && arg_index == 2) || magic == 2) { /* Do no conversions for certain magic varargs. */ a = mark_type_use (a); if (TREE_CODE (a) == FUNCTION_DECL && reject_gcc_builtin (a)) return error_mark_node; } - else if (magic == 1) + else if (magic != 0) /* For other magic varargs only do decay_conversion. */ a = decay_conversion (a, complain); else if (DECL_CONSTRUCTOR_P (fn) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 0276069..589657b 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,8 @@ 2016-06-24 Jakub Jelinek + * c-c++-common/torture/builtin-arith-overflow-p-19.c: Run for C++ too. + * g++.dg/ext/builtin-arith-overflow-2.C: New test. + * c-c++-common/builtin-arith-overflow-1.c (generic_wrong_type, f3, f4): Adjust expected diagnostics. * c-c++-common/torture/builtin-arith-overflow.h (TP): New macro. diff --git a/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-p-19.c b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-p-19.c index 38f6d07..0fd8e69 100644 --- a/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-p-19.c +++ b/gcc/testsuite/c-c++-common/torture/builtin-arith-overflow-p-19.c @@ -1,5 +1,5 @@ /* Test __builtin_{add,sub,mul}_overflow_p. */ -/* { dg-do run { target c } } */ +/* { dg-do run } */ /* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ #include "builtin-arith-overflow.h" diff --git a/gcc/testsuite/g++.dg/ext/builtin-arith-overflow-2.C b/gcc/testsuite/g++.dg/ext/builtin-arith-overflow-2.C new file mode 100644 index 0000000..340ab77 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/builtin-arith-overflow-2.C @@ -0,0 +1,53 @@ +// { dg-do run } +// { dg-options "-O2" } + +struct A { int i : 1; }; +struct B { int j : 3; }; + +template +int +foo (int x, int y) +{ + A a = {}; + S s = {}; + return __builtin_add_overflow_p (x, y, a.i) + 2 * __builtin_mul_overflow_p (x, y, s.j); +} + +__attribute__((noinline, noclone)) int +bar (int x, int y) +{ + return foo (x, y); +} + +#if __cplusplus >= 201402L +template +constexpr int +baz (int x, int y) +{ + A a = {}; + S s = {}; + return __builtin_add_overflow_p (x, y, a.i) + 2 * __builtin_mul_overflow_p (x, y, s.j); +} + +constexpr int t1 = baz (0, 0); +constexpr int t2 = baz (1, -1); +constexpr int t3 = baz (1, -4); +constexpr int t4 = baz (-4, 4); +constexpr int t5 = baz (4, 2); +static_assert (t1 == 0, ""); +static_assert (t2 == 0, ""); +static_assert (t3 == 1, ""); +static_assert (t4 == 2, ""); +static_assert (t5 == 3, ""); +#endif + +int +main () +{ + if (bar (0, 0) != 0 + || bar (-1, 1) != 0 + || bar (-4, 1) != 1 + || bar (4, -4) != 2 + || bar (2, 4) != 3) + __builtin_abort (); +} -- cgit v1.1