From 5c42d3414397b59b0431be58732900c2c0165c72 Mon Sep 17 00:00:00 2001 From: Kyrylo Tkachov Date: Wed, 25 May 2016 15:53:21 +0000 Subject: [PATCH] [RTL ifcvt] PR rtl-optimization/66940: Avoid signed overflow in noce_get_alt_condition PR rtl-optimization/66940 * ifcvt.c (noce_get_alt_condition): Check that incrementing or decrementing desired_val will not overflow before performing these operations. * gcc.c-torture/execute/pr66940.c: New test. From-SVN: r236728 --- gcc/ChangeLog | 7 +++++++ gcc/ifcvt.c | 12 +++++++---- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.c-torture/execute/pr66940.c | 20 +++++++++++++++++++ 4 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr66940.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 4af0254ca98..92f094b5655 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2016-05-25 Kyrylo Tkachov + + PR rtl-optimization/66940 + * ifcvt.c (noce_get_alt_condition): Check that incrementing or + decrementing desired_val will not overflow before performing these + operations. + 2016-05-25 Ilya Verbin * config/i386/i386-builtin-types.def: Add V16SI_FTYPE_V16SF, diff --git a/gcc/ifcvt.c b/gcc/ifcvt.c index 4949965c9dc..44ae020db24 100644 --- a/gcc/ifcvt.c +++ b/gcc/ifcvt.c @@ -2364,28 +2364,32 @@ noce_get_alt_condition (struct noce_if_info *if_info, rtx target, switch (code) { case LT: - if (actual_val == desired_val + 1) + if (desired_val != HOST_WIDE_INT_MAX + && actual_val == desired_val + 1) { code = LE; op_b = GEN_INT (desired_val); } break; case LE: - if (actual_val == desired_val - 1) + if (desired_val != HOST_WIDE_INT_MIN + && actual_val == desired_val - 1) { code = LT; op_b = GEN_INT (desired_val); } break; case GT: - if (actual_val == desired_val - 1) + if (desired_val != HOST_WIDE_INT_MIN + && actual_val == desired_val - 1) { code = GE; op_b = GEN_INT (desired_val); } break; case GE: - if (actual_val == desired_val + 1) + if (desired_val != HOST_WIDE_INT_MAX + && actual_val == desired_val + 1) { code = GT; op_b = GEN_INT (desired_val); diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 8070adb9df0..fb7713d3255 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2016-05-25 Kyrylo Tkachov + + PR rtl-optimization/66940 + * gcc.c-torture/execute/pr66940.c: New test. + 2016-05-25 Ilya Verbin * gcc.target/i386/avx512f-ceil-vec-1.c: New test. diff --git a/gcc/testsuite/gcc.c-torture/execute/pr66940.c b/gcc/testsuite/gcc.c-torture/execute/pr66940.c new file mode 100644 index 00000000000..fbd109dccc7 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr66940.c @@ -0,0 +1,20 @@ +long long __attribute__ ((noinline, noclone)) +foo (long long ival) +{ + if (ival <= 0) + return -0x7fffffffffffffffL - 1; + + return 0x7fffffffffffffffL; +} + +int +main (void) +{ + if (foo (-1) != (-0x7fffffffffffffffL - 1)) + __builtin_abort (); + + if (foo (1) != 0x7fffffffffffffffL) + __builtin_abort (); + + return 0; +} -- 2.30.2