From patchwork Fri Nov 11 10:19:44 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Hurugalawadi, Naveen" X-Patchwork-Id: 693623 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 3tFbWx274jz9t0w for ; Fri, 11 Nov 2016 21:20:13 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="sdqfqM7B"; 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:references:in-reply-to :content-type:mime-version; q=dns; s=default; b=c5/nHu5K8jBpVdml 8keHOAxyPqJcDOKnrRFBk70RlYTxiw+B7ZdznsNGtUR6o8tbelzSqWo3Cw6+p9zB /LFhZnMy4Mo04ZqmzQsfy1n37GjNOGw/XcmhoQp4aBE2KTkVdyyyanUPX9kNJj/z ZX7sSCIzzuwv1jO5+gcMe4b88Fw= 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:references:in-reply-to :content-type:mime-version; s=default; bh=ypbS1lipPPh19YZaDcljrw f5TZ4=; b=sdqfqM7B1iWKQMbqI8yjBtLM8p7u8MyT7blgtEi2nWnsVCZEm6wI+p O8z64AWM/SQ+SuWPcPy1oxcRbmWFMP7QLr/thoz6qK1PCK+ZYVEjxgyaAc7RBvmr zkc256JTJgiIAYG2i0NKEYRM4pIfl6lHSWyqgkRC0Jm4l3dxVmBmw= Received: (qmail 109573 invoked by alias); 11 Nov 2016 10:20:00 -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 109547 invoked by uid 89); 11 Nov 2016 10:20:00 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=hs, HX-MS-Has-Attach:yes, sk:TYPE_OV, fold-const.h X-HELO: NAM02-BL2-obe.outbound.protection.outlook.com Received: from mail-bl2nam02on0079.outbound.protection.outlook.com (HELO NAM02-BL2-obe.outbound.protection.outlook.com) (104.47.38.79) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 11 Nov 2016 10:19:49 +0000 Received: from CO2PR07MB2694.namprd07.prod.outlook.com (10.166.214.7) by CO2PR07MB2694.namprd07.prod.outlook.com (10.166.214.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.721.10; Fri, 11 Nov 2016 10:19:45 +0000 Received: from CO2PR07MB2694.namprd07.prod.outlook.com ([10.166.214.7]) by CO2PR07MB2694.namprd07.prod.outlook.com ([10.166.214.7]) with mapi id 15.01.0721.010; Fri, 11 Nov 2016 10:19:45 +0000 From: "Hurugalawadi, Naveen" To: Jeff Law , "marc.glisse@inria.fr" CC: "ramana.radhakrishnan@foss.arm.com" , "gcc-patches@gcc.gnu.org" Subject: Re: [PATCH] Fix PR31096 Date: Fri, 11 Nov 2016 10:19:44 +0000 Message-ID: References: , <24a5ed27-a4da-043c-4e99-18db1f1dad7b@redhat.com> In-Reply-To: <24a5ed27-a4da-043c-4e99-18db1f1dad7b@redhat.com> authentication-results: spf=none (sender IP is ) smtp.mailfrom=Naveen.Hurugalawadi@cavium.com; x-microsoft-exchange-diagnostics: 1; CO2PR07MB2694; 7:QOCHbb1ZjAduSMhyv/zEB2tUMbB0ZWh6+sIKKyhCtGc9jeKvh1ck/QUbkO9mjD94F1USh5IoncfMzItGChDC2eW5SYxKEREUHWgooa8lBzsmBF3tDvK1t3sBpnohTDb1ZNzpeTMGYBpiS9IohletnWtHT+3mcBB6GyiYoj/+gPGRO74l1vI6x8MuV0kgJP4tqTubEujGPKz5l5XfhzLWSoE8PTw7bCL1vt3iXjbFQY0ptExXMuCFZrzfvCnwZAzpT4v36MbcAijKhwfl9MAkYgSkzzpgxtldcmzUfLqPUDa/dLJ3Eqpv/Q/l2VmSGBN80zkvV3ZCGmB+1lPe/fsNctHq+fJcNMDzdtrkXnDloRc= x-ms-office365-filtering-correlation-id: eba393d4-5d33-4dc9-fdc0-08d40a1c4224 x-microsoft-antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001); SRVR:CO2PR07MB2694; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:; x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(102415395)(6060305)(601004)(2401047)(8121501046)(5005006)(3002001)(10201501046)(6061300); SRVR:CO2PR07MB2694; BCL:0; PCL:0; RULEID:; SRVR:CO2PR07MB2694; x-forefront-prvs: 012349AD1C x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(6009001)(7916002)(199003)(377424004)(189002)(2501003)(7696004)(2950100002)(5660300001)(33656002)(81156014)(81166006)(87936001)(229853002)(106116001)(586003)(189998001)(99286002)(106356001)(77096005)(2900100001)(105586002)(66066001)(9686002)(92566002)(102836003)(6116002)(4001150100001)(54356999)(76176999)(74316002)(68736007)(3846002)(99936001)(3280700002)(101416001)(8676002)(97736004)(5001770100001)(50986999)(76576001)(122556002)(93886004)(8936002)(7736002)(3660700001)(4326007)(7846002)(86362001)(2906002)(305945005); DIR:OUT; SFP:1101; SCL:1; SRVR:CO2PR07MB2694; H:CO2PR07MB2694.namprd07.prod.outlook.com; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; received-spf: None (protection.outlook.com: cavium.com does not designate permitted sender hosts) spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: cavium.com X-MS-Exchange-CrossTenant-originalarrivaltime: 11 Nov 2016 10:19:44.9123 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 711e4ccf-2e9b-4bcf-a551-4094005b6194 X-MS-Exchange-Transport-CrossTenantHeadersStamped: CO2PR07MB2694 Hi, Sorry for a very late reply as the mail was missed or overlooked. >> could now move the test tree_expr_nonzero_p next to >> tree_expr_nonnegative_p (it is redundant for the last case). Done. >> Often just a comment can really help here. Comments updated as per the suggestion >> when C is zero and verify this transformation doesn't fire on that case. Updated test to check with zero. >> verifying that the operand orders change appropriately when dealing >> with a negative constant. Done. >> verify nothing happens with floating point or vector types. Done. Please review the patch and let me know if any modifications are required. Regression tested on X86 and AArch64. Thanks, Naveen 2016-11-11 Naveen H.S gcc * fold-const.c (tree_expr_nonzero_p) : Make non-static. * fold-const.h (tree_expr_nonzero_p) : Declare. * match.pd (cmp (mult:c @0 @1) (mult:c @2 @1) : New Pattern. * match.pd (cmp (mult:c @0 @1) (mult:c @2 @1) : New Pattern. gcc/testsuite * gcc.dg/pr31096.c: New testcase. * gcc.dg/pr31096-1.c: New testcase. diff --git a/gcc/fold-const.c b/gcc/fold-const.c index e14471e..8f13807 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -9015,7 +9015,7 @@ tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p) /* Return true when T is an address and is known to be nonzero. Handle warnings about undefined signed overflow. */ -static bool +bool tree_expr_nonzero_p (tree t) { bool ret, strict_overflow_p; diff --git a/gcc/fold-const.h b/gcc/fold-const.h index 46dcd28..fbe1328 100644 --- a/gcc/fold-const.h +++ b/gcc/fold-const.h @@ -169,6 +169,7 @@ extern tree size_diffop_loc (location_t, tree, tree); #define non_lvalue(T) non_lvalue_loc (UNKNOWN_LOCATION, T) extern tree non_lvalue_loc (location_t, tree); +extern bool tree_expr_nonzero_p (tree); extern bool tree_expr_nonnegative_p (tree); extern bool tree_expr_nonnegative_warnv_p (tree, bool *, int = 0); extern tree make_range (tree, int *, tree *, tree *, bool *); diff --git a/gcc/match.pd b/gcc/match.pd index 29ddcd8..eecfe23 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -31,6 +31,7 @@ along with GCC; see the file COPYING3. If not see zerop CONSTANT_CLASS_P tree_expr_nonnegative_p + tree_expr_nonzero_p integer_valued_real_p integer_pow2p HONOR_NANS) @@ -1017,7 +1018,31 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && tree_nop_conversion_p (type, TREE_TYPE (@1))) (convert (bit_and (bit_not @1) @0)))) +/* For integral types with undefined overflow and C != 0 fold + x * C EQ/NE y * C into x EQ/NE y. */ +(for cmp (eq ne) + (simplify + (cmp (mult:c @0 @1) (mult:c @2 @1)) + (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) + && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)) + && tree_expr_nonzero_p (@1)) + (cmp @0 @2)))) + +/* For integral types with undefined overflow and C != 0 fold + x * C RELOP y * C into: + x RELOP y for nonnegative C + y RELOP x for negative C */ +(for cmp (lt gt le ge) + (simplify + (cmp (mult:c @0 @1) (mult:c @2 @1)) + (if (INTEGRAL_TYPE_P (TREE_TYPE (@1)) + && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) + (if (tree_expr_nonnegative_p (@1) && tree_expr_nonzero_p (@1)) + (cmp @0 @2) + (if (TREE_CODE (@1) == INTEGER_CST + && wi::lt_p (@1, 0, TYPE_SIGN (TREE_TYPE (@1)))) + (cmp @2 @0)))))) /* ((X inner_op C0) outer_op C1) With X being a tree where value_range has reasoned certain bits to always be diff --git a/gcc/testsuite/gcc.dg/pr31096-1.c b/gcc/testsuite/gcc.dg/pr31096-1.c new file mode 100644 index 0000000..e681f0f --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr31096-1.c @@ -0,0 +1,51 @@ +/* PR middle-end/31096 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +#define zero(name, op) \ +int name (int a, int b) \ +{ return a * 0 op b * 0; } + +zero(zeq, ==) zero(zne, !=) zero(zlt, <) +zero(zgt, >) zero(zge, >=) zero(zle, <=) + +#define unsign_pos(name, op) \ +int name (unsigned a, unsigned b) \ +{ return a * 4 op b * 4; } + +unsign_pos(upeq, ==) unsign_pos(upne, !=) unsign_pos(uplt, <) +unsign_pos(upgt, >) unsign_pos(upge, >=) unsign_pos(uple, <=) + +#define unsign_neg(name, op) \ +int name (unsigned a, unsigned b) \ +{ return a * -2 op b * -2; } + +unsign_neg(uneq, ==) unsign_neg(unne, !=) unsign_neg(unlt, <) +unsign_neg(ungt, >) unsign_neg(unge, >=) unsign_neg(unle, <=) + +#define float(name, op) \ +int name (float a, float b) \ +{ return a * 5 op b * 5; } + +float(feq, ==) float(fne, !=) float(flt, <) +float(fgt, >) float(fge, >=) float(fle, <=) + +#define float_val(name, op) \ +int name (int a, int b) \ +{ return a * 54.0 op b * 54.0; } + +float_val(fveq, ==) float_val(fvne, !=) float_val(fvlt, <) +float_val(fvgt, >) float_val(fvge, >=) float_val(fvle, <=) + +#define vec(name, op) \ +int name (int a, int b) \ +{ int c[10]; return a * c[1] op b * c[1]; } + +vec(veq, ==) vec(vne, !=) vec(vlt, <) +vec(vgt, >) vec(vge, >=) vec(vle, <=) + +/* { dg-final { scan-tree-dump-times "\\(D\\) \\* 4" 24 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "\\(D\\) \\* 4294967294" 12 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "\\(D\\) \\* 5\\.0e\\+0" 12 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "\\* 5\\.4e\\+1" 12 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "\\(D\\) \\* c\\\$1_8\\(D\\)" 12 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/pr31096.c b/gcc/testsuite/gcc.dg/pr31096.c new file mode 100644 index 0000000..32c979e --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr31096.c @@ -0,0 +1,36 @@ +/* PR middle-end/31096 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +#define signval_pos(name, op) \ +int name (int a, int b) \ +{ return a * 4 op b * 4; } + +signval_pos(peq, ==) signval_pos(pne, !=) signval_pos(plt, <) +signval_pos(pgt, >) signval_pos(pge, >=) signval_pos(ple, <=) + +#define signval_neg(name, op) \ +int name (int a, int b) \ +{ return a * -23 op b * -23; } + +signval_neg(neq, ==) signval_neg(nne, !=) signval_neg(nlt, <) +signval_neg(ngt, >) signval_neg(nge, >=) signval_neg(nle, <=) + +#define vec_pos(name, op) \ +int name (int a[10], int b[10]) \ +{ return a[3] * 4 op b[8] * 4; } + +vec_pos(vpeq, ==) vec_pos(vpne, !=) vec_pos(vplt, <) +vec_pos(vpgt, >) vec_pos(vpge, >=) vec_pos(vple, <=) + +#define vec_neg(name, op) \ +int name (int a[10], int b[10]) \ +{ return a[3] * -23 op b[8] * -23; } + +vec_neg(vneq, ==) vec_neg(vnne, !=) vec_neg(vnlt, <) +vec_neg(vngt, >) vec_neg(vnge, >=) vec_neg(vnle, <=) + +/* { dg-final { scan-tree-dump-not "\\(D\\) \\* 4" "optimized" } } */ +/* { dg-final { scan-tree-dump-not "\\(D\\) \\* -23" "optimized" } } */ +/* { dg-final { scan-tree-dump-times "_1 = b_2\\(D\\)" 4 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "_1 = MEM\\\[\\(int \\*\\)b" 4 "optimized" } } */