From patchwork Tue Oct 17 16:28:49 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wilco Dijkstra X-Patchwork-Id: 827117 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-464348-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="BQ115l1q"; 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 3yGgcm1Pdcz9t2Z for ; Wed, 18 Oct 2017 03:29:11 +1100 (AEDT) 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:content-type :content-transfer-encoding:mime-version; q=dns; s=default; b=JPv UZ0q2+z76Gn6VtzyAkV5WuW5FDNzhk1Zvqv3YM1sOmrgJTPQ5/e23UIpHH9OhLEz jcXYap7Tsgk+ZC093tR5ihyvMmkAMWkTt7zDxQ6d+gUxS4b3dqz3KX8BtvW2I2sw xQYZ8PEN5eMreA0Ak8E7J7+6z4J5oA7qiZMsq70s= 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:content-type :content-transfer-encoding:mime-version; s=default; bh=6Nptq0GJ2 oDv3xuyDM0m1ayxRNU=; b=BQ115l1qavHavNZ1dl2L1UC63CSHSrTVWLYQxvsiY 9E1/wDSl97lZRUlS6dP/qtKc8ipadR1DQF6nUp7BVb0jjYluwRTdyhdMYT0KgcCT 5SpFZGy8bOk2FbixqhbxIBYy343XwbjttdJodq6wNjDW45Ps7RnKo+iSMazL50Pa +E= Received: (qmail 21943 invoked by alias); 17 Oct 2017 16:28:56 -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 21853 invoked by uid 89); 17 Oct 2017 16:28:55 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.1 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 spammy=D*2, sk:jackson X-HELO: EUR01-DB5-obe.outbound.protection.outlook.com Received: from mail-db5eur01on0087.outbound.protection.outlook.com (HELO EUR01-DB5-obe.outbound.protection.outlook.com) (104.47.2.87) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 17 Oct 2017 16:28:53 +0000 Received: from DB6PR0801MB2053.eurprd08.prod.outlook.com (10.168.86.22) by DB6SPR00MB2500.eurprd08.prod.outlook.com (10.169.217.151) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.77.7; Tue, 17 Oct 2017 16:28:49 +0000 Received: from DB6PR0801MB2053.eurprd08.prod.outlook.com ([fe80::4106:9fa0:b29a:d31e]) by DB6PR0801MB2053.eurprd08.prod.outlook.com ([fe80::4106:9fa0:b29a:d31e%18]) with mapi id 15.20.0077.022; Tue, 17 Oct 2017 16:28:49 +0000 From: Wilco Dijkstra To: GCC Patches CC: nd Subject: [PATCH] Simplify floating point comparisons Date: Tue, 17 Oct 2017 16:28:49 +0000 Message-ID: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Wilco.Dijkstra@arm.com; x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; DB6SPR00MB2500; 6:DMPewRz5i7LziTw1pOuAUOhNFA+9wYb5EuQaY9dIlYl5M2EhbjxJEDr1vDjbdIb6CyJ++o0WRJMNzvLOYrKknluKMPAKdlY66yzMGuuRLT+SB6IM5S/u5/BoOvwh19vKhifJ3arYgFycdMzgbo+OoKTGLMBtkNfnqL5Gbec7pM2IL2X+vsZrAQ0ibvJoc64H6eB13BN6uk9/pIRlB6VBwTpGWnTrae3jjjjw4OdN1mvNimRWe0lLRUZc8x9lwF16z2FPAgMhMIPzo2qAznflRs4dPOqpFvX5ex6JAPUU0F+9nEj1wbOaHvZUVBMxmtakFaeHg6DCIy8ioq042S2owg==; 5:hDaBdxrpyJeCaZzGqDjJhR2C0E0lCvJ4yHJxWcHR0o8QtH22jcsuPDK1209IiyZcpG9GME6u6sW7slwQ+BdRAdsPkXWVsE1iarJCmTjuCd5CeUnJvF5vaL8Rpz6zGnleVFZOCPwHTlN17JuINt2kvA==; 24:bG+TUFrR0Bnc90Fwg83oFPs/9GL1BiaXWh23YNfi+CNo2YtYdQezrhoYZuQrzvR0FxZbsvaOZxAuukwWJ24SKJ5dBXryUnFvwqAd9bZwnPs=; 7:QsZhLx+N8enBdD+RXh0EFMw1ruoy7YLn+PcA3cceshj/6BT2XpZM0h/qahglV3ogD1Wfc+0Z3LDIU1nMQq9LrJK3whPWMe+GpxfeartfkMTDUSut4ZiZzg8p7LOAvjD89u1ACJOJ3LdM0WwRm5v2+hdggLQdykwu+6CjvAoNkqIG9234m9jWcpcowIyZ0ctYHrJ7+ewXEqhIA0S+j8NHq5Yi+WWC5XXDi96vWsLndPw= x-ms-exchange-antispam-srfa-diagnostics: SSOS; x-ms-office365-filtering-correlation-id: a6a6f8ee-0f9b-4199-2a21-08d5157c25d7 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(2017030254152)(48565401081)(2017052603199)(201703131423075)(201703031133081)(201702281549075); SRVR:DB6SPR00MB2500; x-ms-traffictypediagnostic: DB6SPR00MB2500: nodisclaimer: True x-exchange-antispam-report-test: UriScan:(180628864354917)(22074186197030)(183786458502308); x-microsoft-antispam-prvs: x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(2401047)(5005006)(8121501046)(3002001)(93006095)(93001095)(100000703101)(100105400095)(10201501046)(6055026)(6041248)(20161123560025)(20161123558100)(20161123562025)(20161123564025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123555025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095); SRVR:DB6SPR00MB2500; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:DB6SPR00MB2500; x-forefront-prvs: 04631F8F77 x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(6009001)(39860400002)(376002)(346002)(377424004)(54534003)(199003)(189002)(6506006)(50986999)(74316002)(101416001)(6916009)(7736002)(54356999)(305945005)(478600001)(66066001)(2906002)(316002)(55016002)(4326008)(72206003)(7696004)(189998001)(966005)(25786009)(102836003)(97736004)(106356001)(3846002)(6116002)(6306002)(105586002)(53936002)(6436002)(33656002)(5660300001)(99286003)(14454004)(4001150100001)(575784001)(3660700001)(8936002)(5250100002)(9686003)(2900100001)(86362001)(3280700002)(8676002)(68736007)(81156014)(81166006); DIR:OUT; SFP:1101; SCL:1; SRVR:DB6SPR00MB2500; H:DB6PR0801MB2053.eurprd08.prod.outlook.com; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; received-spf: None (protection.outlook.com: arm.com does not designate permitted sender hosts) spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-originalarrivaltime: 17 Oct 2017 16:28:49.7310 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB6SPR00MB2500 This patch implements some of the optimizations discussed in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71026. Simplify (C / x > 0.0) into x > 0.0. If C is negative the comparison is reversed. Simplify (x * C1) > C2 into x > (C2 / C1). Again, if C1 is negative the comparison is reversed. Both transformations are only done with -funsafe-math-optimizations, the constant is non-zero, and not a NaN. OK for commit? ChangeLog 2017-10-17 Wilco Dijkstra Jackson Woodruff gcc/ PR 71026/tree-optimization * match.pd: Simplify floating point comparisons. gcc/testsuite/ PR 71026/tree-optimization * gcc.dg/associate_comparison_1.c: New test. diff --git a/gcc/match.pd b/gcc/match.pd index e58a65af59b44a6b82ed8705f62966c5e6f251ac..cb48f079b4a310272e49cc319a1b3b0ff2023ba4 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -352,6 +352,19 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (rdiv @0 (rdiv:s @1 @2)) (mult (rdiv @0 @1) @2))) +(if (flag_unsafe_math_optimizations) + /* Simplify (C / x op 0.0) to x op 0.0 for C > 0. */ + (for op (lt le gt ge) + neg_op (gt ge lt le) + (simplify + (op (rdiv REAL_CST@0 @1) real_zerop@2) + (switch + (if (real_less (&dconst0, TREE_REAL_CST_PTR (@0))) + (op @1 @2)) + /* For C < 0, use the inverted operator. */ + (if (real_less (TREE_REAL_CST_PTR (@0), &dconst0)) + (neg_op @1 @2)))))) + /* Optimize (X & (-A)) / A where A is a power of 2, to X >> log2(A) */ (for div (trunc_div ceil_div floor_div round_div exact_div) (simplify @@ -3546,6 +3559,20 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (rdiv @2 @1)) (rdiv (op @0 @2) @1))) + (for cmp (lt le gt ge) + neg_cmp (gt ge lt le) + /* Simplify (x * C1) cmp C2 -> x cmp (C2 / C1), where C1 != 0. */ + (simplify + (cmp (mult @0 REAL_CST@1) REAL_CST@2) + (with + { tree tem = const_binop (RDIV_EXPR, type, @2, @1); } + (if (tem) + (switch + (if (real_less (&dconst0, TREE_REAL_CST_PTR (@1))) + (cmp @0 { tem; })) + (if (real_less (TREE_REAL_CST_PTR (@1), &dconst0)) + (neg_cmp @0 { tem; }))))))) + /* Simplify sqrt(x) * sqrt(y) -> sqrt(x*y). */ (for root (SQRT CBRT) (simplify diff --git a/gcc/testsuite/gcc.dg/associate_comparison_1.c b/gcc/testsuite/gcc.dg/associate_comparison_1.c new file mode 100644 index 0000000000000000000000000000000000000000..d051f052e13812c91cbd2d559bf2af8fae128ee1 --- /dev/null +++ b/gcc/testsuite/gcc.dg/associate_comparison_1.c @@ -0,0 +1,34 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -funsafe-math-optimizations -fdump-tree-optimized" } */ + +int +cmp_mul_1 (float x) +{ + return x * 3 <= 100; +} + +int +cmp_mul_2 (float x) +{ + return x * -5 > 100; +} + +int +div_cmp_1 (float x, float y) +{ + return x / 3 <= y; +} + +int +div_cmp_2 (float x, float y) +{ + return x / 3 <= 1; +} + +int +inv_cmp (float x) +{ + return 5 / x >= 0; +} + +/* { dg-final { scan-tree-dump-not " / " "optimized" } } */