From patchwork Mon Oct 17 22:23:39 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bill Schmidt X-Patchwork-Id: 683453 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 3syXmm41Kcz9sCZ for ; Tue, 18 Oct 2016 09:24:06 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=RhHQXhaU; 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:to :from:subject:date:mime-version:content-type :content-transfer-encoding:message-id; q=dns; s=default; b=fNI2u C7CmYTYZFUt9Gokff2q3McjtXJRH1aMAPvtYIFluA2BfU+jJRWL5ymmiLcZcXnCb QixSDrTz64ABwwgO1Z+klzT/U7I/rQm14ECFutU2kDRzc0gDdHHFKl3McOXQ5rJ/ jO9KGV0r6Rl1tCDk7Q+uv+dEUdLBuk6JFhL+AE= 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:to :from:subject:date:mime-version:content-type :content-transfer-encoding:message-id; s=default; bh=zfgv5QlHJW5 pveS4Va4cOd7vKbo=; b=RhHQXhaUEBULuvkrafLHY75zeWVFLmAPygCw2I4i7HG fnX2X7JXKaCgXV2CwR0IhYOtuTeSfCqf1taEXf0p2kC4Q3Mg7ucNIn8BArQdTg1O rskW+yOhPIxNw08KAruppCZzC5c3Y/EwMorjzanbFFkUziseVVZxFctwa6SnWyH8 = Received: (qmail 47419 invoked by alias); 17 Oct 2016 22:23:58 -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 47402 invoked by uid 89); 17 Oct 2016 22:23:57 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.4 required=5.0 tests=AWL, BAYES_00, KAM_ASCII_DIVIDERS, KAM_LAZY_DOMAIN_SECURITY, RCVD_IN_DNSWL_LOW, RCVD_IN_SEMBACKSCATTER autolearn=no version=3.3.2 spammy=stride, H*F:U*wschmidt, recognizes, expressly 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, 17 Oct 2016 22:23:47 +0000 Received: from pps.filterd (m0098421.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.17/8.16.0.17) with SMTP id u9HMJ4Vl054265 for ; Mon, 17 Oct 2016 18:23:44 -0400 Received: from e33.co.us.ibm.com (e33.co.us.ibm.com [32.97.110.151]) by mx0a-001b2d01.pphosted.com with ESMTP id 2650khaj53-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Mon, 17 Oct 2016 18:23:44 -0400 Received: from localhost by e33.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 17 Oct 2016 16:23:43 -0600 Received: from d03dlp03.boulder.ibm.com (9.17.202.179) by e33.co.us.ibm.com (192.168.1.133) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 17 Oct 2016 16:23:40 -0600 Received: from b03cxnp08026.gho.boulder.ibm.com (b03cxnp08026.gho.boulder.ibm.com [9.17.130.18]) by d03dlp03.boulder.ibm.com (Postfix) with ESMTP id EDEA419D801C for ; Mon, 17 Oct 2016 16:23:05 -0600 (MDT) Received: from b03ledav004.gho.boulder.ibm.com (b03ledav004.gho.boulder.ibm.com [9.17.130.235]) by b03cxnp08026.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id u9HMNeDB15335912 for ; Mon, 17 Oct 2016 15:23:40 -0700 Received: from b03ledav004.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id E8F4178051 for ; Mon, 17 Oct 2016 16:23:39 -0600 (MDT) Received: from BigMac.local (unknown [9.85.135.45]) by b03ledav004.gho.boulder.ibm.com (Postfix) with ESMTP id A910978038 for ; Mon, 17 Oct 2016 16:23:39 -0600 (MDT) To: GCC Patches From: Bill Schmidt Subject: [PATCH] Fix PR77916 Date: Mon, 17 Oct 2016 17:23:39 -0500 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16101722-0008-0000-0000-000005D610CC X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00005930; HX=3.00000240; KW=3.00000007; PH=3.00000004; SC=3.00000187; SDB=6.00769503; UDB=6.00368677; IPR=6.00545921; BA=6.00004812; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00013021; XFM=3.00000011; UTC=2016-10-17 22:23:42 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 16101722-0009-0000-0000-00003C38E6E6 Message-Id: <36e7b4de-3d53-88c8-7819-54a2dacca710@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2016-10-17_08:, , 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-1609300000 definitions=main-1610170380 X-IsSubscribed: yes Hi, https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77916 identifies a situation where SLSR will ICE when exposed to a cast from integer to pointer. This is because we try to convert a PLUS_EXPR with an addend of -1 * S into a MINUS_EXPR with a subtrahend of S, but the base operand is unexpectedly of pointer type. This patch recognizes when pointer arithmetic is taking place and ensures that we use a POINTER_PLUS_EXPR at all such times. In the case of the PR, this occurs in the logic where the stride S is a known constant value, but the same problem could occur when it is an SSA_NAME without known value. Both possibilities are handled here. Fixing the code to ensure that the unknown stride case always uses an initializer for a negative increment allows us to remove the stopgap fix added for PR77937 as well. Bootstrapped and tested on powerpc64le-unknown-linux-gnu with no regressions, committed. Thanks, Bill [gcc] 2016-10-17 Bill Schmidt PR tree-optimization/77916 * gimple-ssa-strength-reduction.c (create_add_on_incoming_edge): Don't allow a MINUS_EXPR for pointer arithmetic for either known or unknown strides. (record_increment): Increments of -1 for unknown strides just use a multiply initializer like other negative values. (analyze_increments): Remove stopgap solution for -1 increment applied to pointer arithmetic. [gcc/testsuite] 2016-10-17 Bill Schmidt PR tree-optimization/77916 * gcc.dg/torture/pr77916.c: New. Index: gcc/gimple-ssa-strength-reduction.c =================================================================== --- gcc/gimple-ssa-strength-reduction.c (revision 241245) +++ gcc/gimple-ssa-strength-reduction.c (working copy) @@ -2154,35 +2154,41 @@ create_add_on_incoming_edge (slsr_cand_t c, tree b basis_type = TREE_TYPE (basis_name); lhs = make_temp_ssa_name (basis_type, NULL, "slsr"); + /* Occasionally people convert integers to pointers without a + cast, leading us into trouble if we aren't careful. */ + enum tree_code plus_code + = POINTER_TYPE_P (basis_type) ? POINTER_PLUS_EXPR : PLUS_EXPR; + if (known_stride) { tree bump_tree; - enum tree_code code = PLUS_EXPR; + enum tree_code code = plus_code; widest_int bump = increment * wi::to_widest (c->stride); - if (wi::neg_p (bump)) + if (wi::neg_p (bump) && !POINTER_TYPE_P (basis_type)) { code = MINUS_EXPR; bump = -bump; } - bump_tree = wide_int_to_tree (basis_type, bump); + tree stride_type = POINTER_TYPE_P (basis_type) ? sizetype : basis_type; + bump_tree = wide_int_to_tree (stride_type, bump); new_stmt = gimple_build_assign (lhs, code, basis_name, bump_tree); } else { int i; - bool negate_incr = (!address_arithmetic_p && wi::neg_p (increment)); + bool negate_incr = !POINTER_TYPE_P (basis_type) && wi::neg_p (increment); i = incr_vec_index (negate_incr ? -increment : increment); gcc_assert (i >= 0); if (incr_vec[i].initializer) { - enum tree_code code = negate_incr ? MINUS_EXPR : PLUS_EXPR; + enum tree_code code = negate_incr ? MINUS_EXPR : plus_code; new_stmt = gimple_build_assign (lhs, code, basis_name, incr_vec[i].initializer); } else if (increment == 1) - new_stmt = gimple_build_assign (lhs, PLUS_EXPR, basis_name, c->stride); + new_stmt = gimple_build_assign (lhs, plus_code, basis_name, c->stride); else if (increment == -1) new_stmt = gimple_build_assign (lhs, MINUS_EXPR, basis_name, c->stride); @@ -2500,12 +2506,14 @@ record_increment (slsr_cand_t c, widest_int increm /* Optimistically record the first occurrence of this increment as providing an initializer (if it does); we will revise this opinion later if it doesn't dominate all other occurrences. - Exception: increments of -1, 0, 1 never need initializers; - and phi adjustments don't ever provide initializers. */ + Exception: increments of 0, 1 never need initializers; + and phi adjustments don't ever provide initializers. Note + that we only will see an increment of -1 here for pointer + arithmetic (otherwise we will have an initializer). */ if (c->kind == CAND_ADD && !is_phi_adjust && c->index == increment - && (increment > 1 || increment < -1) + && (increment > 1 || increment < 0) && (gimple_assign_rhs_code (c->cand_stmt) == PLUS_EXPR || gimple_assign_rhs_code (c->cand_stmt) == POINTER_PLUS_EXPR)) { @@ -2819,11 +2827,6 @@ analyze_increments (slsr_cand_t first_dep, machine && !POINTER_TYPE_P (first_dep->cand_type))) incr_vec[i].cost = COST_NEUTRAL; - /* FIXME: We don't handle pointers with a -1 increment yet. - They are usually unprofitable anyway. */ - else if (incr == -1 && POINTER_TYPE_P (first_dep->cand_type)) - incr_vec[i].cost = COST_INFINITE; - /* FORNOW: If we need to add an initializer, give up if a cast from the candidate's type to its stride's type can lose precision. This could eventually be handled better by expressly retaining the Index: gcc/testsuite/gcc.dg/torture/pr77916.c =================================================================== --- gcc/testsuite/gcc.dg/torture/pr77916.c (revision 0) +++ gcc/testsuite/gcc.dg/torture/pr77916.c (working copy) @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -Wno-int-conversion" } */ + +/* PR77916: This failed with "error: invalid (pointer) operands to plus/minus" + after SLSR. */ + +typedef struct +{ + void *f1; +} S; + +S *a; +int b; + +void +fn1 (void) +{ + for (; b; b++, a++) + a->f1 = b; +}