From c0140e3c5d3f3b8090e8c21ad530a8986525eb76 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Sat, 10 Dec 2016 13:06:12 +0100 Subject: [PATCH] re PR tree-optimization/78720 (Illegal instruction in generated code) PR tree-optimization/78720 * match.pd (A < 0 ? C : 0): Only optimize for signed A. If shift is negative, sign extend to @1's type and than AND with C. * gcc.c-torture/execute/pr78720.c: New test. Co-Authored-By: Marc Glisse From-SVN: r243516 --- gcc/ChangeLog | 7 +++++ gcc/match.pd | 17 +++++++---- gcc/testsuite/ChangeLog | 3 ++ gcc/testsuite/gcc.c-torture/execute/pr78720.c | 29 +++++++++++++++++++ 4 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr78720.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 883bf48b36a..f5dfabf9b5f 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2016-12-10 Jakub Jelinek + Marc Glisse + + PR tree-optimization/78720 + * match.pd (A < 0 ? C : 0): Only optimize for signed A. If shift + is negative, sign extend to @1's type and than AND with C. + 2016-12-10 Jakub Jelinek PR fortran/78758 diff --git a/gcc/match.pd b/gcc/match.pd index feaa4a14478..f4cc2d810c3 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -2768,17 +2768,22 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (ncmp (convert:stype @0) { build_zero_cst (stype); }))))) /* If we have A < 0 ? C : 0 where C is a power of 2, convert - this into a right shift followed by ANDing with C. */ + this into a right shift or sign extension followed by ANDing with C. */ (simplify (cond (lt @0 integer_zerop) integer_pow2p@1 integer_zerop) - (with { + (if (!TYPE_UNSIGNED (TREE_TYPE (@0))) + (with { int shift = element_precision (@0) - wi::exact_log2 (@1) - 1; - } - (bit_and - (convert (rshift @0 { build_int_cst (integer_type_node, shift); })) - @1))) + } + (if (shift >= 0) + (bit_and + (convert (rshift @0 { build_int_cst (integer_type_node, shift); })) + @1) + /* Otherwise ctype must be wider than TREE_TYPE (@0) and pure + sign extension followed by AND with C will achieve the effect. */ + (bit_and (convert @0) @1))))) /* When the addresses are not directly of decls compare base and offset. This implements some remaining parts of fold_comparison address diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 3760c66e107..9bd73899c97 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,8 @@ 2016-12-10 Jakub Jelinek + PR tree-optimization/78720 + * gcc.c-torture/execute/pr78720.c: New test. + PR fortran/78758 * gfortran.dg/pr78758.f90: New test. * gfortran.dg/pr38868.f: Remove again bogus warning. diff --git a/gcc/testsuite/gcc.c-torture/execute/pr78720.c b/gcc/testsuite/gcc.c-torture/execute/pr78720.c new file mode 100644 index 00000000000..b99c232a1ba --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr78720.c @@ -0,0 +1,29 @@ +/* PR tree-optimization/78720 */ + +__attribute__((noinline, noclone)) long int +foo (signed char x) +{ + return x < 0 ? 0x80000L : 0L; +} + +__attribute__((noinline, noclone)) long int +bar (signed char x) +{ + return x < 0 ? 0x80L : 0L; +} + +__attribute__((noinline, noclone)) long int +baz (signed char x) +{ + return x < 0 ? 0x20L : 0L; +} + +int +main () +{ + if (foo (-1) != 0x80000L || bar (-1) != 0x80L || baz (-1) != 0x20L + || foo (0) != 0L || bar (0) != 0L || baz (0) != 0L + || foo (31) != 0L || bar (31) != 0L || baz (31) != 0L) + __builtin_abort (); + return 0; +} -- 2.30.2