From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id 709CA385841B; Fri, 4 Nov 2022 08:30:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 709CA385841B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667550632; bh=5zdcl9RGqWLVsobcEhQ11VouaJ/viEuvSUvAavfwZFY=; h=From:To:Subject:Date:From; b=kGDYK7vK1vywLpVAEyrWLuhDnv1EE22qAeQZhKXBOPjyheI62h9ehvDdEiXBOlMu5 3GO1mY4aH2v9yiZEG0SbvPUIE5chEnJjWfGQUJLcxC8MZbhbZyaYPffkaGzczHmW44 axbqF3HGwa8K/BehAsOlH4Txem3YcrH/Tq5nAg34= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r11-10354] ifcvt: Don't introduce trapping or faulting reads in noce_try_sign_mask [PR106032] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: d9b72d8d03134813d5fa84cffb190027fd29f6d6 X-Git-Newrev: fe6e8a09a5338c0acda23ab2d3ef4433fb83637f Message-Id: <20221104083032.709CA385841B@sourceware.org> Date: Fri, 4 Nov 2022 08:30:32 +0000 (GMT) List-Id: https://gcc.gnu.org/g:fe6e8a09a5338c0acda23ab2d3ef4433fb83637f commit r11-10354-gfe6e8a09a5338c0acda23ab2d3ef4433fb83637f Author: Jakub Jelinek Date: Tue Jun 21 11:40:16 2022 +0200 ifcvt: Don't introduce trapping or faulting reads in noce_try_sign_mask [PR106032] noce_try_sign_mask as documented will optimize if (c < 0) x = t; else x = 0; into x = (c >> bitsm1) & t; The optimization is done if either t is unconditional (e.g. for x = t; if (c >= 0) x = 0; ) or if it is cheap. We already check that t doesn't have side-effects, but if t is conditional, we need to punt also if it may trap or fault, as we make it unconditional. I've briefly skimmed other noce_try* optimizations and didn't find one that would suffer from the same problem. 2022-06-21 Jakub Jelinek PR rtl-optimization/106032 * ifcvt.c (noce_try_sign_mask): Punt if !t_unconditional, and t may_trap_or_fault_p, even if it is cheap. * gcc.c-torture/execute/pr106032.c: New test. (cherry picked from commit a0c30fe3b888f20215f3e040d21b62b603804ca9) Diff: --- gcc/ifcvt.c | 15 ++++++++------- gcc/testsuite/gcc.c-torture/execute/pr106032.c | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/gcc/ifcvt.c b/gcc/ifcvt.c index 0439a8aacff..367d5c25c56 100644 --- a/gcc/ifcvt.c +++ b/gcc/ifcvt.c @@ -2814,18 +2814,19 @@ noce_try_sign_mask (struct noce_if_info *if_info) return FALSE; /* This is only profitable if T is unconditionally executed/evaluated in the - original insn sequence or T is cheap. The former happens if B is the - non-zero (T) value and if INSN_B was taken from TEST_BB, or there was no - INSN_B which can happen for e.g. conditional stores to memory. For the - cost computation use the block TEST_BB where the evaluation will end up - after the transformation. */ + original insn sequence or T is cheap and can't trap or fault. The former + happens if B is the non-zero (T) value and if INSN_B was taken from + TEST_BB, or there was no INSN_B which can happen for e.g. conditional + stores to memory. For the cost computation use the block TEST_BB where + the evaluation will end up after the transformation. */ t_unconditional = (t == if_info->b && (if_info->insn_b == NULL_RTX || BLOCK_FOR_INSN (if_info->insn_b) == if_info->test_bb)); if (!(t_unconditional - || (set_src_cost (t, mode, if_info->speed_p) - < COSTS_N_INSNS (2)))) + || ((set_src_cost (t, mode, if_info->speed_p) + < COSTS_N_INSNS (2)) + && !may_trap_or_fault_p (t)))) return FALSE; if (!noce_can_force_operand (t)) diff --git a/gcc/testsuite/gcc.c-torture/execute/pr106032.c b/gcc/testsuite/gcc.c-torture/execute/pr106032.c new file mode 100644 index 00000000000..d2ccf6a5b52 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr106032.c @@ -0,0 +1,21 @@ +/* PR rtl-optimization/106032 */ + +__attribute__((noipa)) int +foo (int x, int *y) +{ + int a = 0; + if (x < 0) + a = *y; + return a; +} + +int +main () +{ + int a = 42; + if (foo (0, 0) != 0 || foo (1, 0) != 0) + __builtin_abort (); + if (foo (-1, &a) != 42 || foo (-42, &a) != 42) + __builtin_abort (); + return 0; +}