From patchwork Mon Aug 28 20:16:44 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bill Schmidt X-Patchwork-Id: 806757 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-461048-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="eQNE4297"; 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 3xh32x0s1Cz9s7c for ; Tue, 29 Aug 2017 06:17:11 +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 :content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:references:to:message-id; q=dns; s= default; b=F92dfwGdpgGX+MdJ+gzrjDmooEMbHC3sWS3WuA60KtRT6RzSsAsIG jEJVNeKdjB7a32gBoCb86/qT9B4ezow2foLh9U9BqxBM84NQfSsYJbUhdpgubKZU TlMKGV84qb6+jkzLSX+09SpWHAdgy3cOLQfsnFN3gQEjNjy31+1k2Q= 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 :content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:references:to:message-id; s=default; bh=0T3/ryP9cztkUwnsWszMX7S9r00=; b=eQNE4297L1d/YCDpDKUM2Nb+U8y/ abuMLG9dwKosKC4Pq0XfPVvPabr35h9cqkW+M8D6aEII7CCc33fJmyEik2Xh5em1 poL8bv1wxMIi+SANDshD6+5mGJ+b8YbXlMhtwphhQCVZ+lfz/kMfhRoHDIA2ceJs FIJLM5j9yZ8o7SI= Received: (qmail 70894 invoked by alias); 28 Aug 2017 20:17:02 -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 69654 invoked by uid 89); 28 Aug 2017 20:17:01 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-10.4 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, KAM_LAZY_DOMAIN_SECURITY, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 spammy= X-HELO: mx0a-001b2d01.pphosted.com Received: from mx0b-001b2d01.pphosted.com (HELO mx0a-001b2d01.pphosted.com) (148.163.158.5) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 28 Aug 2017 20:16:51 +0000 Received: from pps.filterd (m0098420.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id v7SKDpC8019623 for ; Mon, 28 Aug 2017 16:16:50 -0400 Received: from e35.co.us.ibm.com (e35.co.us.ibm.com [32.97.110.153]) by mx0b-001b2d01.pphosted.com with ESMTP id 2cmr22phg4-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Mon, 28 Aug 2017 16:16:49 -0400 Received: from localhost by e35.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 28 Aug 2017 14:16:48 -0600 Received: from b03cxnp08028.gho.boulder.ibm.com (9.17.130.20) by e35.co.us.ibm.com (192.168.1.135) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 28 Aug 2017 14:16:46 -0600 Received: from b03ledav001.gho.boulder.ibm.com (b03ledav001.gho.boulder.ibm.com [9.17.130.232]) by b03cxnp08028.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v7SKGjgp27852978; Mon, 28 Aug 2017 13:16:45 -0700 Received: from b03ledav001.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 8B6426E045; Mon, 28 Aug 2017 14:16:45 -0600 (MDT) Received: from bigmac.rchland.ibm.com (unknown [9.10.86.161]) by b03ledav001.gho.boulder.ibm.com (Postfix) with ESMTPS id 4A9136E03F; Mon, 28 Aug 2017 14:16:45 -0600 (MDT) Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\)) Subject: [PATCH v3] Fix PR81503 (SLSR invalid fold) From: Bill Schmidt In-Reply-To: Date: Mon, 28 Aug 2017 15:16:44 -0500 Cc: Jakub Jelinek , GCC Patches References: <7bd4dcbb-cce0-82bb-b938-ffd85dd0e72a@linux.vnet.ibm.com> <20170803162022.GB2123@tucnak> <20170803163950.GD2123@tucnak> To: Richard Biener X-TM-AS-GCONF: 00 x-cbid: 17082820-0012-0000-0000-000014ECB031 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00007628; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000226; SDB=6.00908948; UDB=6.00455801; IPR=6.00689199; BA=6.00005557; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00016906; XFM=3.00000015; UTC=2017-08-28 20:16:47 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17082820-0013-0000-0000-00004F437442 Message-Id: <32664487-31CB-4383-B475-8AF865E05C72@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-08-28_11:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1707230000 definitions=main-1708280319 X-IsSubscribed: yes > On Aug 28, 2017, at 7:37 AM, Richard Biener wrote: > > Not sure, but would it be fixed in a similar way when writing > ? Thanks, Richard, that works very well. I decided this was a good opportunity to also simplify the control flow a little with early returns. Here's the result. Bootstrapped and tested on powerpc64le-linux-gnu with no regressions. Is this ok for trunk, and eventually for backport to gcc 5, 6, and 7? (I can omit the control flow cleanups for the older releases if desired.) Thanks, Bill [gcc] 2017-08-03 Bill Schmidt Jakub Jelinek Richard Biener PR tree-optimization/81503 * gimple-ssa-strength-reduction.c (replace_mult_candidate): Ensure folded constant fits in the target type; reorder tests for clarity. [gcc/testsuite] 2017-08-03 Bill Schmidt Jakub Jelinek Richard Biener PR tree-optimization/81503 * gcc.c-torture/execute/pr81503.c: New file. Index: gcc/gimple-ssa-strength-reduction.c =================================================================== --- gcc/gimple-ssa-strength-reduction.c (revision 251369) +++ gcc/gimple-ssa-strength-reduction.c (working copy) @@ -2089,104 +2089,104 @@ replace_mult_candidate (slsr_cand_t c, tree basis_ tree target_type = TREE_TYPE (gimple_assign_lhs (c->cand_stmt)); enum tree_code cand_code = gimple_assign_rhs_code (c->cand_stmt); - /* It is highly unlikely, but possible, that the resulting - bump doesn't fit in a HWI. Abandon the replacement - in this case. This does not affect siblings or dependents - of C. Restriction to signed HWI is conservative for unsigned - types but allows for safe negation without twisted logic. */ - if (wi::fits_shwi_p (bump) - && bump.to_shwi () != HOST_WIDE_INT_MIN - /* It is not useful to replace casts, copies, negates, or adds of - an SSA name and a constant. */ - && cand_code != SSA_NAME - && !CONVERT_EXPR_CODE_P (cand_code) - && cand_code != PLUS_EXPR - && cand_code != POINTER_PLUS_EXPR - && cand_code != MINUS_EXPR - && cand_code != NEGATE_EXPR) + /* It is not useful to replace casts, copies, negates, or adds of + an SSA name and a constant. */ + if (cand_code == SSA_NAME + || CONVERT_EXPR_CODE_P (cand_code) + || cand_code == PLUS_EXPR + || cand_code == POINTER_PLUS_EXPR + || cand_code == MINUS_EXPR + || cand_code == NEGATE_EXPR) + return; + + enum tree_code code = PLUS_EXPR; + tree bump_tree; + gimple *stmt_to_print = NULL; + + if (wi::neg_p (bump)) { - enum tree_code code = PLUS_EXPR; - tree bump_tree; - gimple *stmt_to_print = NULL; + code = MINUS_EXPR; + bump = -bump; + } - /* If the basis name and the candidate's LHS have incompatible - types, introduce a cast. */ - if (!useless_type_conversion_p (target_type, TREE_TYPE (basis_name))) - basis_name = introduce_cast_before_cand (c, target_type, basis_name); - if (wi::neg_p (bump)) + /* It is possible that the resulting bump doesn't fit in target_type. + Abandon the replacement in this case. This does not affect + siblings or dependents of C. */ + if (bump != wi::ext (bump, TYPE_PRECISION (target_type), + TYPE_SIGN (target_type))) + return; + + bump_tree = wide_int_to_tree (target_type, bump); + + /* If the basis name and the candidate's LHS have incompatible types, + introduce a cast. */ + if (!useless_type_conversion_p (target_type, TREE_TYPE (basis_name))) + basis_name = introduce_cast_before_cand (c, target_type, basis_name); + + if (dump_file && (dump_flags & TDF_DETAILS)) + { + fputs ("Replacing: ", dump_file); + print_gimple_stmt (dump_file, c->cand_stmt, 0); + } + + if (bump == 0) + { + tree lhs = gimple_assign_lhs (c->cand_stmt); + gassign *copy_stmt = gimple_build_assign (lhs, basis_name); + gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt); + slsr_cand_t cc = c; + gimple_set_location (copy_stmt, gimple_location (c->cand_stmt)); + gsi_replace (&gsi, copy_stmt, false); + c->cand_stmt = copy_stmt; + while (cc->next_interp) { - code = MINUS_EXPR; - bump = -bump; + cc = lookup_cand (cc->next_interp); + cc->cand_stmt = copy_stmt; } - - bump_tree = wide_int_to_tree (target_type, bump); - if (dump_file && (dump_flags & TDF_DETAILS)) + stmt_to_print = copy_stmt; + } + else + { + tree rhs1, rhs2; + if (cand_code != NEGATE_EXPR) { + rhs1 = gimple_assign_rhs1 (c->cand_stmt); + rhs2 = gimple_assign_rhs2 (c->cand_stmt); + } + if (cand_code != NEGATE_EXPR + && ((operand_equal_p (rhs1, basis_name, 0) + && operand_equal_p (rhs2, bump_tree, 0)) + || (operand_equal_p (rhs1, bump_tree, 0) + && operand_equal_p (rhs2, basis_name, 0)))) { - fputs ("Replacing: ", dump_file); - print_gimple_stmt (dump_file, c->cand_stmt, 0); + if (dump_file && (dump_flags & TDF_DETAILS)) + { + fputs ("(duplicate, not actually replacing)", dump_file); + stmt_to_print = c->cand_stmt; + } } - - if (bump == 0) + else { - tree lhs = gimple_assign_lhs (c->cand_stmt); - gassign *copy_stmt = gimple_build_assign (lhs, basis_name); gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt); slsr_cand_t cc = c; - gimple_set_location (copy_stmt, gimple_location (c->cand_stmt)); - gsi_replace (&gsi, copy_stmt, false); - c->cand_stmt = copy_stmt; + gimple_assign_set_rhs_with_ops (&gsi, code, basis_name, bump_tree); + update_stmt (gsi_stmt (gsi)); + c->cand_stmt = gsi_stmt (gsi); while (cc->next_interp) { cc = lookup_cand (cc->next_interp); - cc->cand_stmt = copy_stmt; + cc->cand_stmt = gsi_stmt (gsi); } if (dump_file && (dump_flags & TDF_DETAILS)) - stmt_to_print = copy_stmt; + stmt_to_print = gsi_stmt (gsi); } - else - { - tree rhs1, rhs2; - if (cand_code != NEGATE_EXPR) { - rhs1 = gimple_assign_rhs1 (c->cand_stmt); - rhs2 = gimple_assign_rhs2 (c->cand_stmt); - } - if (cand_code != NEGATE_EXPR - && ((operand_equal_p (rhs1, basis_name, 0) - && operand_equal_p (rhs2, bump_tree, 0)) - || (operand_equal_p (rhs1, bump_tree, 0) - && operand_equal_p (rhs2, basis_name, 0)))) - { - if (dump_file && (dump_flags & TDF_DETAILS)) - { - fputs ("(duplicate, not actually replacing)", dump_file); - stmt_to_print = c->cand_stmt; - } - } - else - { - gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt); - slsr_cand_t cc = c; - gimple_assign_set_rhs_with_ops (&gsi, code, - basis_name, bump_tree); - update_stmt (gsi_stmt (gsi)); - c->cand_stmt = gsi_stmt (gsi); - while (cc->next_interp) - { - cc = lookup_cand (cc->next_interp); - cc->cand_stmt = gsi_stmt (gsi); - } - if (dump_file && (dump_flags & TDF_DETAILS)) - stmt_to_print = gsi_stmt (gsi); - } - } + } - if (dump_file && (dump_flags & TDF_DETAILS)) - { - fputs ("With: ", dump_file); - print_gimple_stmt (dump_file, stmt_to_print, 0); - fputs ("\n", dump_file); - } + if (dump_file && (dump_flags & TDF_DETAILS)) + { + fputs ("With: ", dump_file); + print_gimple_stmt (dump_file, stmt_to_print, 0); + fputs ("\n", dump_file); } } Index: gcc/testsuite/gcc.c-torture/execute/pr81503.c =================================================================== --- gcc/testsuite/gcc.c-torture/execute/pr81503.c (nonexistent) +++ gcc/testsuite/gcc.c-torture/execute/pr81503.c (working copy) @@ -0,0 +1,15 @@ +unsigned short a = 41461; +unsigned short b = 3419; +int c = 0; + +void foo() { + if (a + b * ~(0 != 5)) + c = -~(b * ~(0 != 5)) + 2147483647; +} + +int main() { + foo(); + if (c != 2147476810) + return -1; + return 0; +}