From patchwork Wed Jun 20 21:14:40 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom de Vries X-Patchwork-Id: 932395 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-480154-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="o3QolsWU"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 419yJt3RXYz9s2R for ; Thu, 21 Jun 2018 07:14:54 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; q=dns; s=default; b=pd1tiIks2pUoxGW4dSVri7huvUSrRvnVxpAFu/FDAh3AVDZDWl Px7xS7jZsZrYQijH5rKLXkbWnHrpKrQE2IN6WU9wZFHrP1atNSLrAykt6ZIH1wiy HN9TZ0M8hhiB2ERWyaqQDEtXUuDPZ7Nw73Ro2smflP12avIQSpODwFNhY= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; s= default; bh=jr/WHE7wfE1bDF7YuVv7fQZmytA=; b=o3QolsWU6G2XLJNr6M3A 9cVDpzgcbbs5LN9Ny8gC8y/r0DqB/hM3fcC0aVslw1It6qVXnvIp5Rk8YLjHYPtw ZtUJbBTWZ3cNoOOR71KYr4XlRD7Lcd7l8fRUF9vkEqw6sPOJkgCr0M460RzEdwz8 9okblIZjPKdFvOIHWs+gldc= Received: (qmail 35528 invoked by alias); 20 Jun 2018 21:14:46 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 35514 invoked by uid 89); 20 Jun 2018 21:14:46 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, SPF_PASS autolearn=ham version=3.3.2 spammy=Factor X-HELO: mx2.suse.de Received: from mx2.suse.de (HELO mx2.suse.de) (195.135.220.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 20 Jun 2018 21:14:44 +0000 Received: from relay2.suse.de (charybdis-ext-too.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id CDD42ACE8 for ; Wed, 20 Jun 2018 21:14:41 +0000 (UTC) Date: Wed, 20 Jun 2018 23:14:40 +0200 From: Tom de Vries To: gcc-patches@gcc.gnu.org Cc: Richard Biener Subject: [PATCH, PR85859][tail-merge] Factor out gimple_may_have_side_effects_p and use in stmt_local_def Message-ID: <20180620211437.5nyhgazj3xukmd37@localhost.localdomain> MIME-Version: 1.0 Content-Disposition: inline User-Agent: NeoMutt/20170912 (1.9.0) X-IsSubscribed: yes Hi, Consider the test-case from the patch. When compiled with "-O2 -fno-dce -fno-isolate-erroneous-paths-dereference -fno-tree-dce -fno-tree-vrp" and run, we get: ... $ ./a.out Floating point exception ... The problem is introduced by -ftree-tail-merge (enabled by -O2), so it executes fine when compiled with -fno-tree-tail-merge. Tail-merge merges two blocks it considers equal: ... find_duplicates: duplicate of Removing basic block 4 _6 = foo (0); iftmp.2_10 = (long int) _6; goto ; [100.00%] iftmp.2_11 = (long int) &c; ... while the blocks in fact aren't equal from the point of view of side effects. Executing bb3 causes the 'Floating point exception', while executing bb4 doesn't. This patch fixes the problem by factoring out a new function gimple_may_have_side_effects_p from bb_no_side_effects_p, and reusing that function in the side-effect test in stmt_local_def in tail-merge. The patch inhibits tail-merge in pr81192.c, because gimple_may_have_side_effects_p tests for gimple_uses_undefined_value_p, which triggers for this particular test-case. Bootstrapped and reg-tested on x86_64. OK for trunk? Thanks, - Tom [tail-merge] Factor out gimple_may_have_side_effects_p and use in stmt_local_def 2018-06-20 Tom de Vries PR tree-optimization/85859 * tree-ssa-ifcombine.c (gimple_may_have_side_effects_p): Factor out of ... (bb_no_side_effects_p): ... here. * tree-ssa-ifcombine.h: New file. (gimple_may_have_side_effects_p): Declare. * tree-ssa-tail-merge.c (stmt_local_def): Use gimple_may_have_side_effects_p. * gcc.dg/pr85859.c: New test. * gcc.dg/pr81192.c: Update. --- gcc/testsuite/gcc.dg/pr81192.c | 2 +- gcc/testsuite/gcc.dg/pr85859.c | 19 +++++++++++++++++++ gcc/tree-ssa-ifcombine.c | 34 +++++++++++++++++++++++----------- gcc/tree-ssa-ifcombine.h | 24 ++++++++++++++++++++++++ gcc/tree-ssa-tail-merge.c | 5 ++--- 5 files changed, 69 insertions(+), 15 deletions(-) diff --git a/gcc/testsuite/gcc.dg/pr81192.c b/gcc/testsuite/gcc.dg/pr81192.c index 0049f371b3d..9db641ba91d 100644 --- a/gcc/testsuite/gcc.dg/pr81192.c +++ b/gcc/testsuite/gcc.dg/pr81192.c @@ -24,4 +24,4 @@ fn2 (void) ; } -/* { dg-final { scan-tree-dump-times "(?n)find_duplicates: duplicate of " 1 "pre" } } */ +/* { dg-final { scan-tree-dump-not "(?n)find_duplicates: duplicate of " "pre" } } */ diff --git a/gcc/testsuite/gcc.dg/pr85859.c b/gcc/testsuite/gcc.dg/pr85859.c new file mode 100644 index 00000000000..96eb9671137 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr85859.c @@ -0,0 +1,19 @@ +/* { dg-do run } */ +/* { dg-options "-ftree-tail-merge -Wno-div-by-zero -O2 -fno-dce -fno-isolate-erroneous-paths-dereference -fno-tree-dce -fno-tree-vrp" } */ + +int b, c, d, e; + +__attribute__ ((noinline, noclone)) +int foo (short f) +{ + f %= 0; + return f; +} + +int +main (void) +{ + b = (unsigned char) __builtin_parity (d); + e ? foo (0) : (long) &c; + return 0; +} diff --git a/gcc/tree-ssa-ifcombine.c b/gcc/tree-ssa-ifcombine.c index b63c600c47b..8ea51a793f9 100644 --- a/gcc/tree-ssa-ifcombine.c +++ b/gcc/tree-ssa-ifcombine.c @@ -40,6 +40,7 @@ along with GCC; see the file COPYING3. If not see #include "gimplify-me.h" #include "tree-cfg.h" #include "tree-ssa.h" +#include "tree-ssa-ifcombine.h" #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT #define LOGICAL_OP_NON_SHORT_CIRCUIT \ @@ -108,6 +109,27 @@ recognize_if_then_else (basic_block cond_bb, return true; } +/* Return true if gimple STMT may have side effect. */ + +bool +gimple_may_have_side_effects_p (gimple *stmt) +{ + if (gimple_has_side_effects (stmt) + || gimple_uses_undefined_value_p (stmt) + || gimple_could_trap_p (stmt) + || gimple_vuse (stmt) + /* const calls don't match any of the above, yet they could + still have some side-effects - they could contain + gimple_could_trap_p statements, like floating point + exceptions or integer division by zero. See PR70586. + FIXME: perhaps gimple_has_side_effects or gimple_could_trap_p + should handle this. */ + || is_gimple_call (stmt)) + return true; + + return false; +} + /* Verify if the basic block BB does not have side-effects. Return true in this case, else false. */ @@ -123,17 +145,7 @@ bb_no_side_effects_p (basic_block bb) if (is_gimple_debug (stmt)) continue; - if (gimple_has_side_effects (stmt) - || gimple_uses_undefined_value_p (stmt) - || gimple_could_trap_p (stmt) - || gimple_vuse (stmt) - /* const calls don't match any of the above, yet they could - still have some side-effects - they could contain - gimple_could_trap_p statements, like floating point - exceptions or integer division by zero. See PR70586. - FIXME: perhaps gimple_has_side_effects or gimple_could_trap_p - should handle this. */ - || is_gimple_call (stmt)) + if (gimple_may_have_side_effects_p (stmt)) return false; } diff --git a/gcc/tree-ssa-ifcombine.h b/gcc/tree-ssa-ifcombine.h new file mode 100644 index 00000000000..8c62ff450eb --- /dev/null +++ b/gcc/tree-ssa-ifcombine.h @@ -0,0 +1,24 @@ +/* Copyright (C) 2018 Free Software Foundation, Inc. + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 3, or (at your option) any +later version. + +GCC is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +. */ + +#ifndef TREE_SSA_IFCOMBINE_H +#define TREE_SSA_IFCOMBINE_H_ + +extern bool gimple_may_have_side_effects_p (gimple *); + +#endif diff --git a/gcc/tree-ssa-tail-merge.c b/gcc/tree-ssa-tail-merge.c index f482ce197cd..ec8219b993b 100644 --- a/gcc/tree-ssa-tail-merge.c +++ b/gcc/tree-ssa-tail-merge.c @@ -206,6 +206,7 @@ along with GCC; see the file COPYING3. If not see #include "cfgloop.h" #include "tree-eh.h" #include "tree-cfgcleanup.h" +#include "tree-ssa-ifcombine.h" const int ignore_edge_flags = EDGE_DFS_BACK | EDGE_EXECUTABLE; @@ -299,9 +300,7 @@ stmt_local_def (gimple *stmt) def_operand_p def_p; if (gimple_vdef (stmt) != NULL_TREE - || gimple_has_side_effects (stmt) - || gimple_could_trap_p_1 (stmt, false, false) - || gimple_vuse (stmt) != NULL_TREE) + || gimple_may_have_side_effects_p (stmt)) return false; def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);