From patchwork Tue Oct 4 12:53:00 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wilco Dijkstra X-Patchwork-Id: 678059 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 3spJkF2pnqz9sD6 for ; Tue, 4 Oct 2016 23:53:24 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=BI0rmBgE; 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:mime-version:content-type :content-transfer-encoding; q=dns; s=default; b=mGwEMlSa/THBLZpD axHClTeuxKK/yZWez9W1xCd76l7abqkXOg3mJQTV1p8Bbn6efImDYOsYkWDw0lH/ lnZ6edW/JC80IQUDo2IgQQBp0QpDpWmZZjeulqHggjBUBVGkGneEmZ5T86BW0ajl fo6CWDGAyVEFuRKQ7+7OzqlaS70= 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:mime-version:content-type :content-transfer-encoding; s=default; bh=emLcvQyoiZHphKdh51HHT9 pujso=; b=BI0rmBgETZIpdViVDFCNSXLq9uiFF8Y67yVfgfnt2A9Vf1FWPWEabA t217qm8r+/t6sxxo55oQ3LWjQsiToVGWdmWsbVXQ9Hdns9j5EYY1dDsMDA1vsMEw HCwn9O8WMRurC45RnZLuh/yWM9mj/NbCiV5OCHCQ710lDjwe3Y2do= Received: (qmail 90948 invoked by alias); 4 Oct 2016 12:53:16 -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 90926 invoked by uid 89); 4 Oct 2016 12:53:15 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=AWL, BAYES_00, SPF_PASS autolearn=ham version=3.3.2 spammy=HContent-Language:en-GB, MULT_EXPR, mult_expr, combinations X-HELO: eu-smtp-delivery-143.mimecast.com Received: from eu-smtp-delivery-143.mimecast.com (HELO eu-smtp-delivery-143.mimecast.com) (207.82.80.143) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 04 Oct 2016 12:53:04 +0000 Received: from EUR01-DB5-obe.outbound.protection.outlook.com (mail-db5eur01lp0181.outbound.protection.outlook.com [213.199.154.181]) (Using TLS) by eu-smtp-1.mimecast.com with ESMTP id uk-mta-2-7qnmNdkIPK6jNLLpcMNRng-1; Tue, 04 Oct 2016 13:53:01 +0100 Received: from AM5PR0802MB2610.eurprd08.prod.outlook.com (10.175.46.18) by AM5PR0802MB2610.eurprd08.prod.outlook.com (10.175.46.18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.649.16; Tue, 4 Oct 2016 12:53:00 +0000 Received: from AM5PR0802MB2610.eurprd08.prod.outlook.com ([10.175.46.18]) by AM5PR0802MB2610.eurprd08.prod.outlook.com ([10.175.46.18]) with mapi id 15.01.0649.021; Tue, 4 Oct 2016 12:53:00 +0000 From: Wilco Dijkstra To: GCC Patches CC: nd Subject: [RFC][PATCH] Canonicalize address multiplies Date: Tue, 4 Oct 2016 12:53:00 +0000 Message-ID: x-ms-office365-filtering-correlation-id: a8a5ce64-6abb-4574-15cc-08d3ec555f99 x-microsoft-exchange-diagnostics: 1; AM5PR0802MB2610; 7:dPmPonbq8HppOen65uNEapV9uIax8ysWFKOee0XVhZanmkxUEQZsna9G4lPI/gNyuXq+Hwr5sbzitQbjIzoA7IjSQMVvhjw0VSWsRWLrpGLYtpwVz+N46tE9Zaji0+Ydw/DIbPxeXZ80bwnL9xGkJ8613OKHqkqeoyYxxpQE7i/1umG3JesP4Pe+AGeyy4C3vtfZEq3rTzFGcvvBGUXst2ElkKdTdkRkwMJ46Ig1yPges6tMFqt3oIGOBBveT2AGsSa17RCe4igVyCdlFZXe2yvWDgG/94yWvDSmsZo3HDReRBgnzoRsw0Pedo8OaC8f6djSauj7BpbBBFvIQ491Uw==; 20:YlpYW6Obn36XTjvvtEpBFSm1gDBvuudC3yJATbeT4D+sWI6rUseFkgwlOdOp8Dqy6puHTYflz9I0w0WYw2A+JYcjBsXPZiUlxdAgKGnQvqvKV9UzYegdd1TbbFXBly1ZZ3HCyV3Mcb9k+f/5ylkKtfGVZgCpZXGdAY3SVwT6kqw= x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:AM5PR0802MB2610; nodisclaimer: True x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(180628864354917); x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(6040176)(601004)(2401047)(5005006)(8121501046)(3002001)(10201501046)(6055026); SRVR:AM5PR0802MB2610; BCL:0; PCL:0; RULEID:; SRVR:AM5PR0802MB2610; x-forefront-prvs: 00851CA28B x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(6009001)(7916002)(57704003)(189002)(54534003)(377424004)(199003)(101416001)(3280700002)(9686002)(8936002)(33656002)(6916009)(68736007)(97736004)(92566002)(5002640100001)(10400500002)(11100500001)(76576001)(5660300001)(7696004)(81166006)(8676002)(110136003)(54356999)(50986999)(81156014)(122556002)(77096005)(4326007)(7736002)(74316002)(305945005)(105586002)(86362001)(575784001)(450100001)(2900100001)(3660700001)(106356001)(7846002)(106116001)(19580395003)(229853001)(189998001)(3846002)(102836003)(66066001)(6116002)(2906002)(586003)(87936001)(19580405001); DIR:OUT; SFP:1101; SCL:1; SRVR:AM5PR0802MB2610; H:AM5PR0802MB2610.eurprd08.prod.outlook.com; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-originalarrivaltime: 04 Oct 2016 12:53:00.8774 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM5PR0802MB2610 X-MC-Unique: 7qnmNdkIPK6jNLLpcMNRng-1 GCC currently doesn't canonicalize address expressions. As a result inefficient code is generated even for trivial index address expressions, blocking CSE and other optimizations: int f(int *p, int i) { return p[i+2] + p[i+1]; } sxtw x1, w1 add x1, x1, 2 add x2, x0, x1, lsl 2 ldr w0, [x0, x1, lsl 2] ldr w1, [x2, -4] add w0, w1, w0 ret After this patch: add x1, x0, x1, sxtw 2 ldp w0, w2, [x1, 4] add w0, w2, w0 ret The reason for this is that array index expressions are preferably kept in the *(p + (i + C0) * C1) form eventhough it is best on most targets to make use of an offset in memory accesses - ie. *(p + i * C1 + (C0*C1)). This patch disables the folding in fold_plusminus_mult_expr that changes the latter form into the former. Unfortunately it isn't possible to know it is an address expression, and neither is there a way to decide when C0*C1 is too complex. So is there a better way/place to do this, or do we need an address canonicalization phase in the tree that ensures we expand addresses in an efficient manner, taking into account target offsets? ChangeLog: 2016-10-04 Wilco Dijkstra gcc/ * fold-const.c (fold_plusminus_mult_expr): Block folding of immediates into multiply. diff --git a/gcc/fold-const.c b/gcc/fold-const.c index e71ce5e0f23adbb1d4a73506769f7243900cfd2d..bc9fb1e8ff3e33c94e66a2d1282235b71fac2730 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -6912,7 +6912,9 @@ fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type, (A * C) +- A -> A * (C+-1). We are most concerned about the case where C is a constant, but other combinations show up during loop reduction. Since - it is not difficult, try all four possibilities. */ + it is not difficult, try all four possibilities. + However avoid moving integer constants into the multiply: + (A * C0) +- C1 is better than (A +- (C1/C0)) * C0. */ if (TREE_CODE (arg0) == MULT_EXPR) { @@ -6920,10 +6922,7 @@ fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type, arg01 = TREE_OPERAND (arg0, 1); } else if (TREE_CODE (arg0) == INTEGER_CST) - { - arg00 = build_one_cst (type); - arg01 = arg0; - } + return NULL_TREE; else { /* We cannot generate constant 1 for fract. */ @@ -6938,20 +6937,7 @@ fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type, arg11 = TREE_OPERAND (arg1, 1); } else if (TREE_CODE (arg1) == INTEGER_CST) - { - arg10 = build_one_cst (type); - /* As we canonicalize A - 2 to A + -2 get rid of that sign for - the purpose of this canonicalization. */ - if (wi::neg_p (arg1, TYPE_SIGN (TREE_TYPE (arg1))) - && negate_expr_p (arg1) - && code == PLUS_EXPR) - { - arg11 = negate_expr (arg1); - code = MINUS_EXPR; - } - else - arg11 = arg1; - } + return NULL_TREE; else { /* We cannot generate constant 1 for fract. */