From patchwork Thu Apr 30 07:53:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xionghu Luo X-Patchwork-Id: 1280004 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=uPWZI7m6; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49CSLs6Glcz9sRY for ; Thu, 30 Apr 2020 17:53:55 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E85853896827; Thu, 30 Apr 2020 07:53:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E85853896827 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1588233233; bh=L+5NspE4hXAD3ar+zAXpqaaMANsYTXT04ss8iw+ZH08=; h=Subject:To:References:Date:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=uPWZI7m6v6QbwE/ZAZdpzEi8womZyAmNEm1qWlAuMTFiKrtGmFEfZAjarFdc2xuGs VTfCif1kRxt0GZsE3RV7PnCIGgajmRgqC+Q7swrcENiYM5DBTVOOK62V2yzgP+ocBl hh1ddr710+Ikm7C0lvTYzhr9gh7zlYN88n1k7/IM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id B92E73892007; Thu, 30 Apr 2020 07:53:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org B92E73892007 Received: from pps.filterd (m0187473.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 03U7XCpA184036; Thu, 30 Apr 2020 03:53:49 -0400 Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com with ESMTP id 30mguy0mnn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 30 Apr 2020 03:53:49 -0400 Received: from m0187473.ppops.net (m0187473.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.36/8.16.0.36) with SMTP id 03U7XOnM184568; Thu, 30 Apr 2020 03:53:49 -0400 Received: from ppma03ams.nl.ibm.com (62.31.33a9.ip4.static.sl-reverse.com [169.51.49.98]) by mx0a-001b2d01.pphosted.com with ESMTP id 30mguy0mmf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 30 Apr 2020 03:53:48 -0400 Received: from pps.filterd (ppma03ams.nl.ibm.com [127.0.0.1]) by ppma03ams.nl.ibm.com (8.16.0.27/8.16.0.27) with SMTP id 03U7p89k023073; Thu, 30 Apr 2020 07:53:46 GMT Received: from b06cxnps4076.portsmouth.uk.ibm.com (d06relay13.portsmouth.uk.ibm.com [9.149.109.198]) by ppma03ams.nl.ibm.com with ESMTP id 30mcu5t2gm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 30 Apr 2020 07:53:46 +0000 Received: from d06av24.portsmouth.uk.ibm.com (mk.ibm.com [9.149.105.60]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 03U7rh0253018672 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 30 Apr 2020 07:53:43 GMT Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 4C62942045; Thu, 30 Apr 2020 07:53:43 +0000 (GMT) Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 9A24C42047; Thu, 30 Apr 2020 07:53:39 +0000 (GMT) Received: from 192.168.1.105 (unknown [9.197.243.104]) by d06av24.portsmouth.uk.ibm.com (Postfix) with ESMTP; Thu, 30 Apr 2020 07:53:39 +0000 (GMT) Subject: [PATCH v2] Add handling of MULT_EXPR/PLUS_EXPR for wrapping overflow in affine combination(PR83403) To: Richard Biener References: <20200428061710.111160-1-luoxhu@linux.vnet.ibm.com> <189e41d0-9545-7cc0-4d82-0162e20328c2@linux.ibm.com> Message-ID: <81360e11-d013-4443-93ca-d76598b957e2@linux.ibm.com> Date: Thu, 30 Apr 2020 15:53:37 +0800 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: Content-Language: en-US X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.138, 18.0.676 definitions=2020-04-30_02:2020-04-30, 2020-04-30 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 lowpriorityscore=0 clxscore=1015 mlxlogscore=999 mlxscore=0 suspectscore=0 bulkscore=0 adultscore=0 impostorscore=0 phishscore=0 spamscore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2004300061 X-Spam-Status: No, score=-25.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_DMARC_STATUS, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: luoxhu via Gcc-patches From: Xionghu Luo Reply-To: luoxhu Cc: segher@kernel.crashing.org, ook@ucw.cz, wschmidt@linux.ibm.com, linkw@gcc.gnu.org, gcc-patches@gcc.gnu.org, dje.gcc@gmail.com Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Update the patch with overflow check. Bootstrap and regression tested PASS on Power8-LE. Use determine_value_range to get value range info for fold convert expressions with internal operation PLUS_EXPR/MINUS_EXPR/MULT_EXPR when not overflow on wrapping overflow inner type. i.e.: (long unsigned int)((unsigned int)n * 10 + 1) => (long unsigned int)n * (long unsigned int)10 + (long unsigned int)1 With this patch for affine combination, load/store motion could detect more address refs independency and promote some memory expressions to registers within loop. PS: Replace the previous "(T1)(X + CST) as (T1)X - (T1)(-CST))" to "(T1)(X + CST) as (T1)X + (T1)(CST))" for wrapping overflow. gcc/ChangeLog 2020-04-30 Xiong Hu Luo PR tree-optimization/83403 * tree-affine.c (expr_to_aff_combination): Replace SSA_NAME with determine_value_range, Add fold conversion of MULT_EXPR, fix the previous PLUS_EXPR. gcc/testsuite/ChangeLog 2020-04-30 Xiong Hu Luo PR tree-optimization/83403 * gcc.dg/tree-ssa/pr83403-1.c: New test. * gcc.dg/tree-ssa/pr83403-2.c: New test. * gcc.dg/tree-ssa/pr83403.h: New header. --- gcc/testsuite/gcc.dg/tree-ssa/pr83403-1.c | 8 ++++++ gcc/testsuite/gcc.dg/tree-ssa/pr83403-2.c | 8 ++++++ gcc/testsuite/gcc.dg/tree-ssa/pr83403.h | 30 +++++++++++++++++++++++ gcc/tree-affine.c | 24 ++++++++++-------- 4 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr83403-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr83403-2.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr83403.h diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr83403-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr83403-1.c new file mode 100644 index 00000000000..748375b03af --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr83403-1.c @@ -0,0 +1,8 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -funroll-loops -fdump-tree-lim2-details" } */ + +#define TYPE unsigned int + +#include "pr83403.h" + +/* { dg-final { scan-tree-dump-times "Executing store motion of" 10 "lim2" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr83403-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr83403-2.c new file mode 100644 index 00000000000..ca2e6bbd61c --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr83403-2.c @@ -0,0 +1,8 @@ +/* { dg-do compile } */ +/* { dg-options "-O3 -funroll-loops -fdump-tree-lim2-details" } */ + +#define TYPE int + +#include "pr83403.h" + +/* { dg-final { scan-tree-dump-times "Executing store motion of" 10 "lim2" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr83403.h b/gcc/testsuite/gcc.dg/tree-ssa/pr83403.h new file mode 100644 index 00000000000..0da8a835b5f --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr83403.h @@ -0,0 +1,30 @@ +__attribute__ ((noinline)) void +calculate (const double *__restrict__ A, const double *__restrict__ B, + double *__restrict__ C) +{ + TYPE m = 0; + TYPE n = 0; + TYPE k = 0; + + A = (const double *) __builtin_assume_aligned (A, 16); + B = (const double *) __builtin_assume_aligned (B, 16); + C = (double *) __builtin_assume_aligned (C, 16); + + for (n = 0; n < 9; n++) + { + for (m = 0; m < 10; m++) + { + C[(n * 10) + m] = 0.0; + } + + for (k = 0; k < 17; k++) + { +#pragma simd + for (m = 0; m < 10; m++) + { + C[(n * 10) + m] += A[(k * 20) + m] * B[(n * 20) + k]; + } + } + } +} + diff --git a/gcc/tree-affine.c b/gcc/tree-affine.c index 0eb8db1b086..5620e6bf28f 100644 --- a/gcc/tree-affine.c +++ b/gcc/tree-affine.c @@ -343,24 +343,28 @@ expr_to_aff_combination (aff_tree *comb, tree_code code, tree type, wide_int minv, maxv; /* If inner type has wrapping overflow behavior, fold conversion for below case: - (T1)(X - CST) -> (T1)X - (T1)CST - if X - CST doesn't overflow by range information. Also handle - (T1)(X + CST) as (T1)(X - (-CST)). */ + (T1)(X *+- CST) -> (T1)X *+- (T1)CST + if X *+- CST doesn't overflow by range information. */ if (TYPE_UNSIGNED (itype) && TYPE_OVERFLOW_WRAPS (itype) - && TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == INTEGER_CST - && icode != MULT_EXPR - && get_range_info (op0, &minv, &maxv) == VR_RANGE) + && determine_value_range (op0, &minv, &maxv) == VR_RANGE) { + wi::overflow_type overflow = wi::OVF_NONE; + signop sign = UNSIGNED; if (icode == PLUS_EXPR) - op1 = wide_int_to_tree (itype, -wi::to_wide (op1)); - if (wi::geu_p (minv, wi::to_wide (op1))) + wi::add (maxv, wi::to_wide (op1), sign, &overflow); + else if (icode == MULT_EXPR) + wi::mul (maxv, wi::to_wide (op1), sign, &overflow); + else + wi::sub (minv, wi::to_wide (op1), sign, &overflow); + + if (overflow == wi::OVF_NONE) { op0 = fold_convert (otype, op0); op1 = fold_convert (otype, op1); - return expr_to_aff_combination (comb, MINUS_EXPR, otype, - op0, op1); + return expr_to_aff_combination (comb, icode, otype, op0, + op1); }1 } }