From patchwork Fri Oct 30 13:41:14 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Oliva X-Patchwork-Id: 538326 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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 41DF6140D5F for ; Sat, 31 Oct 2015 00:41:46 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=NZ35izQZ; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:mime-version:content-type; q=dns; s=default; b=XHpvCfDN+XShJZdypQitgAmugZFik7BlsPVh1R11/PJV8y7sYw i20m2pE62l4ltdaCItAPGMZEqhJng+gVbrR5+psJHMIOw2sHjpIciTc+R8qOuLsy 5VpZXqVxDW7/opaIfj3lIkJWM03BhmdSX+1lvFbFUCT2MgMPP6EVOx/io= 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:from :to:cc:subject:date:message-id:mime-version:content-type; s= default; bh=XLHvFCWVtPYX5gxku+1TRELZLDk=; b=NZ35izQZt13e/BvqUmc1 se0CwmGqX3k99tmVLhiweh8oEpHV18L5X3l8VAJ94Vumya/y0HnZG44ckvT7m0cC cdcielArzk0GyIfv6oYdsYpUKqFT1IihaUJMIxks96HYqPU7G7Z44n8pv5Dikmi7 rj7ZNL2TNKlDdrwrzLaHDmI= Received: (qmail 43423 invoked by alias); 30 Oct 2015 13:41:38 -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 43413 invoked by uid 89); 30 Oct 2015 13:41:37 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.2 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Fri, 30 Oct 2015 13:41:36 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (Postfix) with ESMTPS id 8310235BA75; Fri, 30 Oct 2015 13:41:34 +0000 (UTC) Received: from freie.home (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t9UDfVSg003550 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Fri, 30 Oct 2015 09:41:33 -0400 Received: from livre.home (livre.home [172.31.160.2]) by freie.home (8.14.9/8.14.9) with ESMTP id t9UDfHZ5017437; Fri, 30 Oct 2015 11:41:18 -0200 From: Alexandre Oliva To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PR68083] stop ifcombine from moving uninitialized uses before their guards Date: Fri, 30 Oct 2015 11:41:14 -0200 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) MIME-Version: 1.0 The ifcombine pass may move a conditional access to an uninitialized value before the condition that ensures it is always well-defined, thus introducing undefined behavior. Stop it from doing so. Regstrapped on x86_64-linux-gnu and i686-linux-gnu. Ok to install? Incidentally, bb_no_side_effects_p (inner_cond_bb) is called in all four tests in tree_ssa_ifcombine_bb_1, for each outer_cond_bb that tree_ssa_ifcombine_bb might choose. Is there any reason to not factor it out to the test that checks whether the inner_cond_bb is indeed an if_then_else block, early in tree_ssa_ifcombine_bb, so as to short-circuit the whole thing when the inner block is not viable? for gcc/ChangeLog PR tree-optimization/68083 * tree-ssa-ifcombine.c: Include tree-ssa.h. (bb_no_side_effects_p): Test for undefined uses too. * tree-ssa.c (gimple_uses_undefined_value_p): New. * tree-ssa.h (gimple_uses_undefined_value_p): Declare. for gcc/testsuite/ChangeLog PR tree-optimization/68083 * gcc.dg/torture/pr68083.c: New. From Zhendong Su. --- gcc/testsuite/gcc.dg/torture/pr68083.c | 35 ++++++++++++++++++++++++++++++++ gcc/tree-ssa-ifcombine.c | 2 ++ gcc/tree-ssa.c | 18 ++++++++++++++++ gcc/tree-ssa.h | 1 + 4 files changed, 56 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/torture/pr68083.c diff --git a/gcc/testsuite/gcc.dg/torture/pr68083.c b/gcc/testsuite/gcc.dg/torture/pr68083.c new file mode 100644 index 0000000..ae24781 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr68083.c @@ -0,0 +1,35 @@ +/* { dg-do run } */ + +int a = 2, b = 1, c = 1; + +int +fn1 () +{ + int d; + for (; a; a--) + { + for (d = 0; d < 4; d++) + { + int k; + if (c < 1) + if (k) + c = 0; + if (b) + continue; + return 0; + } + b = !1; + } + return 0; +} + +int +main () +{ + fn1 (); + + if (a != 1) + __builtin_abort (); + + return 0; +} diff --git a/gcc/tree-ssa-ifcombine.c b/gcc/tree-ssa-ifcombine.c index ca55b57..622dc6b 100644 --- a/gcc/tree-ssa-ifcombine.c +++ b/gcc/tree-ssa-ifcombine.c @@ -41,6 +41,7 @@ along with GCC; see the file COPYING3. If not see #include "gimple-iterator.h" #include "gimplify-me.h" #include "tree-cfg.h" +#include "tree-ssa.h" #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT #define LOGICAL_OP_NON_SHORT_CIRCUIT \ @@ -125,6 +126,7 @@ bb_no_side_effects_p (basic_block bb) continue; if (gimple_has_side_effects (stmt) + || gimple_uses_undefined_value_p (stmt) || gimple_could_trap_p (stmt) || gimple_vuse (stmt)) return false; diff --git a/gcc/tree-ssa.c b/gcc/tree-ssa.c index c7be442..8dc2d61 100644 --- a/gcc/tree-ssa.c +++ b/gcc/tree-ssa.c @@ -1210,6 +1210,24 @@ ssa_undefined_value_p (tree t, bool partial) } +/* Return TRUE iff STMT, a gimple statement, references an undefined + SSA name. */ + +bool +gimple_uses_undefined_value_p (gimple *stmt) +{ + ssa_op_iter iter; + tree op; + + FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE) + if (ssa_undefined_value_p (op)) + return true; + + return false; +} + + + /* If necessary, rewrite the base of the reference tree *TP from a MEM_REF to a plain or converted symbol. */ diff --git a/gcc/tree-ssa.h b/gcc/tree-ssa.h index 5a409e5..3b5bd70 100644 --- a/gcc/tree-ssa.h +++ b/gcc/tree-ssa.h @@ -51,6 +51,7 @@ extern bool tree_ssa_useless_type_conversion (tree); extern tree tree_ssa_strip_useless_type_conversions (tree); extern bool ssa_undefined_value_p (tree, bool = true); +extern bool gimple_uses_undefined_value_p (gimple *); extern void execute_update_addresses_taken (void); /* Given an edge_var_map V, return the PHI arg definition. */