From patchwork Mon Sep 30 05:30:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jovan Vukic X-Patchwork-Id: 1990734 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=rt-rk.com header.i=@rt-rk.com header.a=rsa-sha256 header.s=s2021 header.b=hI0sjqhY; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XH8mg4wFqz1xtM for ; Mon, 30 Sep 2024 15:30:39 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 028E2385EC2D for ; Mon, 30 Sep 2024 05:30:37 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx08-0061a602.pphosted.com (mx08-0061a602.pphosted.com [205.220.185.213]) by sourceware.org (Postfix) with ESMTPS id 37A543858C41; Mon, 30 Sep 2024 05:30:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 37A543858C41 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=rt-rk.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rt-rk.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 37A543858C41 Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=205.220.185.213 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1727674213; cv=pass; b=QUEU4r1ikVgboQW+kt+WkCZrlZp5/i9Ws32x7RNU27K4Z5+EJbp6DSkDjwUKKwMsd9+Rv5F1CrYPVbXtNg7Vw0FNizt2EPvj0aAjgjySPS3KEesLW3Zm2zGRGclHM00X0FJ/YeyXh0JF3/5vzUqo3R9BXvzFKLu8yOntYhYyafQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1727674213; c=relaxed/simple; bh=UIfd6LEb9Uy4EeIx+lTO9hklb9KD54GEDeZ+rOyuaIw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=FMk3eYxOxQpzEAYQHoev0bJoNAjztqGKPW1Oo6VeS/lsiDeZSD/vvoAk+DfT53BwvyU1yeBG3tjJyg/iSODqkxyg8AAL8Y75xSYem8t3X2IhkTLdDNTB0Lh+uUB+xYdQk1qiIf6ZuepSRBVNjJglE+WHtGiUG7kuyB5wTSDB0Oo= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from pps.filterd (m0278994.ppops.net [127.0.0.1]) by mx07-0061a602.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 48U1oVDF002525; Mon, 30 Sep 2024 05:30:08 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rt-rk.com; h=cc :content-type:date:from:message-id:mime-version:subject:to; s= s2021; bh=9IMklqVGVGYUSEneKZrv7Czh3Ac/+wsRC+7PnzaPQhY=; b=hI0sjq hYlCC7XBGUqR5IcKRuSnBDdxm3/bOx/Gxe9Ju2RAEtk8OrK4QMRQdnYfZA4eu5gY Dx3HH8eXqax52LXGivh/Y+WLN9QZT+eRzA//mrxlU3ds0s4x9z1y9aiAYXz84U6B kCo6Lp76bYl3a5ozRKQbAZQA/SROqHYPcaYMVcq3AP8NCC2rxQCaJpytYzz4ppqv Qlk9YjX2/gAJ9Ib99mUf1meOGXu9wG7rQRGPHZwGXOpxyjZU4mxB6c1GZp80dUac Uvny0xDzfupRFPp5QP13Hn+H8I8/a6V7ls+hWFHvX+PemoA0z+NiKEyn/pOW3RW6 quj1J4rXMyPo36iQ== Received: from eur05-vi1-obe.outbound.protection.outlook.com (mail-vi1eur05lp2168.outbound.protection.outlook.com [104.47.17.168]) by mx07-0061a602.pphosted.com (PPS) with ESMTPS id 41x710hjcu-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 30 Sep 2024 05:30:07 +0000 (GMT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=FyhGDWnPfn0QZf3U+ifMnPFzmxsYJ9ny69vfyq8c2OU6xcVBrl20yvaaupA3d5b6SJkWUeBgRsu2bxugijS8AcuIvP0Qby1MzMlStFtxSsBzYeoQ8mVEI1JSjfQHUW+OCmFE6CAbnU9kxNOEF3V3fzc4fxhK7Jo9MV5co5sJT9ACEky0fkkjKi9+d7IxZCJ0Jmq7JNeSOpoWc2eXip8eUtvIbuVVKTxhLmJOrSU2IVIkEPeJalzoIeBx5XgzFY3xyXDmU0yY/kA9ABsrdiBEJ89MFFsbnsRVFNRV6R1y/6nNgbJ+FEaSTSbJvx5e2tjy2SGahpYwfVyLCYlR/trguQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=9IMklqVGVGYUSEneKZrv7Czh3Ac/+wsRC+7PnzaPQhY=; b=DZwf4hvdf8tLApScBsMOELpN4xa6eUK1MLfsAzs8mfu6KS4Skv7QLOWv7JszLM4IOjNM3gHHKR7gDBaF98Tb83sX8GpkGd59nEGkBSwbXjhIkR4og+DKLbSzygtnWDA48TCqLWn/VkP2/eCTBh2rk9sk1sRpQIZXFG/XhFMX5kltGwXP8e3cS2WIsiROSh3L6bvGpwbQwPwwoiG1nKHlrh3D7cpKWHcHULeCP/ZGbORFUnhmF9FTbIcv2KDhmhyfp18VZxCXO2iFJKySfsl5VfL9SbXRt1+4khFf5L4jIHOvX8vwDUzMMYYf10Dk9uHgRd/vAo85F4cL0hn3lIU4sA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=rt-rk.com; dmarc=pass action=none header.from=rt-rk.com; dkim=pass header.d=rt-rk.com; arc=none Received: from DB9PR08MB6634.eurprd08.prod.outlook.com (2603:10a6:10:23f::5) by DB4PR08MB9287.eurprd08.prod.outlook.com (2603:10a6:10:3f5::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8026.13; Mon, 30 Sep 2024 05:30:02 +0000 Received: from DB9PR08MB6634.eurprd08.prod.outlook.com ([fe80::3b98:f8f6:2fc8:a5a]) by DB9PR08MB6634.eurprd08.prod.outlook.com ([fe80::3b98:f8f6:2fc8:a5a%4]) with mapi id 15.20.8026.005; Mon, 30 Sep 2024 05:30:02 +0000 From: Jovan Vukic To: "gcc-patches@gcc.gnu.org" CC: "law@gcc.gnu.org" , Mile Davidovic , Dusan Stojkovic Subject: [PATCH v2] RISC-V: Optimize branches with shifted immediate operands Thread-Topic: [PATCH v2] RISC-V: Optimize branches with shifted immediate operands Thread-Index: AQHbETCl21OmKLx6O06dMXG1jRhHYw== Date: Mon, 30 Sep 2024 05:30:00 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: yes X-MS-TNEF-Correlator: msip_labels: x-ms-publictraffictype: Email x-ms-traffictypediagnostic: DB9PR08MB6634:EE_|DB4PR08MB9287:EE_ x-ms-office365-filtering-correlation-id: 6296ef25-9bac-4157-7cf0-08dce110eeaa x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; ARA:13230040|366016|1800799024|376014|38070700018; x-microsoft-antispam-message-info: =?iso-8859-1?q?6tC8UNhpd99mggO76l8ZVo8DBY?= =?iso-8859-1?q?gn8wDyMzHzwl1DimuYn/gBzf8AV54WRjYyWx7d83KsSfKjBydWUyqC1HrOpM?= =?iso-8859-1?q?dSKqoMoEd8DZDZyvC0wA0vw84M+CBzPD3xysLQL49bZiu+I9NbJZE/CekO2+?= =?iso-8859-1?q?936MsQC8ZOakqPU5MOC0dxKCzUsrcMairw/uEYCEyO+WmN0OP3ven19fee6f?= =?iso-8859-1?q?smewq2aoecz0WFC8yjJc7nVCpxF3WnZC9MiNunbPWa8cWiUdWH+2T0x8vazg?= =?iso-8859-1?q?JLtWTzDg+a1J5IPpe9so5ClNiYIvB9WWMXX2gq2VIKC9uWyaARYBuHJdQgJS?= =?iso-8859-1?q?R6j0JEVLpWu3QM1OSvycFA+AmyibtX3AIFuvV8UZcftHAfnbbOnPKqriz01e?= =?iso-8859-1?q?C+BiA/54n/KIN1FrVqVvLB84o1qX2IM/f0bLhSW5eLik0R4iFc2CTz+qBTcd?= =?iso-8859-1?q?mnLizL9ZkFmgOmj6yeuS/FfnyPCHM8VsY0O/2ws9hdofHJkOVynkLlDwVVjq?= =?iso-8859-1?q?yi9muDCs144xa9x+DzkvzDX9rfcR9AdPCffrwOBA8OoCwIMWcNbqv8xFUANY?= =?iso-8859-1?q?ReL65ry6YdF6wVUHJYOUrY4WDRkGfknfrKTWGZ4V0t8x/WopE4g4t4XNHP/T?= =?iso-8859-1?q?kwy1BOG99v6ZqTjGqXOWQA0T4QeJ2NEejQYuY7sovk2sEIy/XLvwbKnyXuDS?= =?iso-8859-1?q?6BGg2XMpGyU8U40ziqnGGWNXx34lLsVDOLUaqHUJNoYoffiHnl+r71hxX7sn?= =?iso-8859-1?q?9A4n1o/iWFiTRwMQoecFz/32oeHupgdjtObyFzVA0KBDQXSwRtSrvs7icrSs?= =?iso-8859-1?q?i9p/fpgg6/tZ63vaUV+2JSVgjO82vlGzNRZp2rcHeW2cr5eMH9zRqAT78IK5?= =?iso-8859-1?q?DYWTjwL8glwKe4y8CoCeyyfrWNe7jip/UaN32i0IvLLT6e5Cyj4sqKeTQFgC?= =?iso-8859-1?q?WbQfvdAKqxwX39VJRd4wNXBZE14HVECO2DtYPCQkpRbTDv6qejFBzxiRoLd9?= =?iso-8859-1?q?2EmJiP4rYhoICRSjr14meb4HEWc/j8d6xy6yTkDPgjbUfH00sC+uMIafXQPd?= =?iso-8859-1?q?jeBlxBxehd6L/WvfAMPTjiMB/hvPRQNZKb8jAb1CY28rcmCcdHgIEezcmtDd?= =?iso-8859-1?q?3PVeVesnXJxy88SG3NmquFHgJrxllKKl9a83bgjYXlFnnB8PaffslDc88Am2?= =?iso-8859-1?q?M26w3PSmF+MrmGEGGJxnf60u5D6sXr563DDNMBVLpjI/Nm4XOSZCH8oEcfzN?= =?iso-8859-1?q?eH+jnY1RXO4PV4aynjMS8+PJJMcHvJ5Umxgk+2zHrA1H4n/7SlOxxouXKdGT?= =?iso-8859-1?q?OwMUG5tUB1nzg6bQ=3D=3D?= x-forefront-antispam-report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DB9PR08MB6634.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(366016)(1800799024)(376014)(38070700018); DIR:OUT; SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?Av4WjuYz8l8Vp2ltv4MJ4T7?= =?iso-8859-1?q?hlQWXfCg/kGvj9SIzeMIeM+71QsWd4CwVyBnL8pv829cFzq6ud39/m+jmHLZ?= =?iso-8859-1?q?/JvgMB1PV7VPZkLFh9XO+JHiN/7scb3D8Oxh0mR0XifCJN6YjPW9YD1bYbTq?= =?iso-8859-1?q?LKSoKX8D5i+ggasYie6djFGNc/au0P/i/e8dI4yz4K4o5qoXSnuV78TNUueN?= =?iso-8859-1?q?01oi0t9Q5+h1pzrIZA8yd31ThKwF4fXN0AqqNOwD7iUq/9v4DpXv0hqv2hge?= =?iso-8859-1?q?n1/hGPDU8Tew/ehZfugUKqxE3neyM9QJ+qWAgU72fntwd/478hjZRI69QVmL?= =?iso-8859-1?q?NtWjQEioGfcdD1gtA+d1aWXOvovXJFJmCNJDJ7EFKtHbybZxQmdZG6W4bSqT?= =?iso-8859-1?q?081X5I7tSrO9/+bpsKTuyZX33lR8xuOw1+pYRJdpCW/8PZ2RM2/EbOk92NXq?= =?iso-8859-1?q?9ZjDKCgOysNhevnmLOpTHUyvdTYNs3YY9fZZ5EP+aUJQjfiyBzuW00soVf5U?= =?iso-8859-1?q?VtPSYDCSP6V5K3xzrjNApRldwM1fHSEvzh7VLbi7N44nX+JTWkkoG2wOKM2R?= =?iso-8859-1?q?ePvThNxP0vhAnXZbKIPrWUYr5UWdpYLzs7gTWkqPMXDXfBGk1igrd87941lj?= =?iso-8859-1?q?EscKZx2hc9iK3T8aBlk35jZX6Atim2ZCnX3+9zSBQKT5Af0F2M1cUVXOLEqc?= =?iso-8859-1?q?1FaFjd7MJUR0aHN9xPOBteJwMgMKoh5xR4JFSwSwls6D6c7VeCGA5NljKVY0?= =?iso-8859-1?q?oQGZsX1J7iaM1LtjqFcOsRCQf2kfhTe5tgdEitDM0bcgrR19d1lKLXy8k129?= =?iso-8859-1?q?iisdVipn+j7LZANApftFjNSRJMkLn6LJVJ8o/xrOvSXfDKScRaeSHGnOIzhv?= =?iso-8859-1?q?sk1Jff2Wy7rDnWOfbm20kiu6o/8m+B7WX1nx++3O4c02w18Bqf2jvqOCWEy3?= =?iso-8859-1?q?wKJdIw10jxXxCjJVfZLFjXuLFZE+7e4Bs77HTkJe3SdvZZ+KNzPRVXdh8LzJ?= =?iso-8859-1?q?AnkqZtUNrIjL0XnD71I3v4jhy9jabfp5viOpPivLxxbAhOku/a2pOG7NIPji?= =?iso-8859-1?q?8AInwAV1jXbewuUA1KFtBtR9xdckIx4fh6m/hbr8BEYBHv3ljnYKFyU0qJrR?= =?iso-8859-1?q?Ccb2KMebf0J/VZwZ1wKxmzG8US8E3/dNgVozmwofugBtTOt3OuDcOAvuSjwl?= =?iso-8859-1?q?KksFuSmC/5Hk2tP0W5jMN1r2JTNrRcjEXN6zmHEudEA01VGbkDURfrB9iDCC?= =?iso-8859-1?q?TiLsI8wn8G5yEEnHyN4rIeu5MJo4lm/B1mkcsn236xHGbdvj7e+Xf0FvTGyg?= =?iso-8859-1?q?Gp2BbRygKAbnpKaWRbKRgBLdkYrmBnUEoi/WtusNTAtYO2AaQ9fXwhPpgDDf?= =?iso-8859-1?q?xMwrG78yZ82cY4hlWGP9onDxE1DuuG5LYSn8mjzhxwEgaYFHJ3Vorlrc13Wd?= =?iso-8859-1?q?HyyeweXQXH4xGyRXPyjzxnb4WEaipip9F2Ljk4FBTZ8kkGXl310NS/VicjmB?= =?iso-8859-1?q?TXkbPv/IHqhy85C0GVpOb36MXuZoLzVIodEwSvdinHb053sWqqoxEQsfo2ms?= =?iso-8859-1?q?raM9AfjabjmYxP0xnqhwIgzTDLy/CqiazQ3OC7zAXArp0xkYWmm1JbcQ8BAc?= =?iso-8859-1?q?P6aoCfK203tZ9JK64?= MIME-Version: 1.0 X-OriginatorOrg: rt-rk.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: DB9PR08MB6634.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6296ef25-9bac-4157-7cf0-08dce110eeaa X-MS-Exchange-CrossTenant-originalarrivaltime: 30 Sep 2024 05:30:01.9452 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 9bc3ed46-a3ca-43f0-b84e-9a557209a7df X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: hFPcuk3xS4oqJzImMCKRIl+lGyH7L6HqBCtzNyKfthxeLZjHLl2XFcPimc97pe3p8+rvVrhHcCPE1qenu9BmYg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB4PR08MB9287 X-Proofpoint-GUID: WcVXwoefv_aNDRNuEeJ6WaTpwGhiNETg X-Proofpoint-ORIG-GUID: WcVXwoefv_aNDRNuEeJ6WaTpwGhiNETg X-Authority-Analysis: v=2.4 cv=DsoE+3/+ c=1 sm=1 tr=0 ts=66fa375f cx=c_pps a=9TK2mEVt+YNFxHG6VsEebQ==:117 a=Ol13hO9ccFRV9qXi2t6ftBPywas=:19 a=xqWC_Br6kY4A:10 a=EaEq8P2WXUwA:10 a=bp5mhLeJ-vUA:10 a=PeOOapuUAAAA:8 a=wT45_P3iAAAA:8 a=ZQtGCOBiG07DkmffAFEA:9 a=wPNLvfGTeEIA:10 a=UhGwg5SC1JaIX7o3GcQA:9 a=m-Z_27IZkzAA:10 a=0BaqRfgCL6CLbWgV2pdm:22 a=RJRP_u8kRGsxlTjV8B5_:22 X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Based on the feedback I received, the patch now uses a custom code iterator instead of relying on match_operator. Since both operands (2 and 3) have trailing zeros, an additional check was introduced to verify if they remain SMALL_OPERANDs after shifting by the smaller number of trailing zeros. To avoid complex inline check, I introduced two new macros, ensuring that the check is encapsulated and reusable, as suggested in the original feedback. It was pointed out that this check should be added because expressions like (x & 0x81000) == 0x8100 or (x & 0x8100) == 0x81000 might lead to unrecognized instructions during splitting. However, both expressions immediately evaluate to false (https://godbolt.org/z/Y11EGMb4f) due to the bit arrangement in constants 0x8100 and 0x81000. As a result, this pattern won't be applied in such cases, so it cannot cause any issues. Therefore, it wasn't necessary to include this as a negative test in the testsuite. Despite my efforts, I couldn't find another example for a negative test. All similar cases evaluated to false immediately due to the bit arrangement. I mentioned this in a follow-up comment on the previous patch version. Regardless, the necessary check has been added, so if a negative case exists, an unrecognized instruction will not be created. 2024-09-30 Jovan Vukic PR target/115921 gcc/ChangeLog: * config/riscv/iterators.md (any_eq): New code iterator. * config/riscv/riscv.h (COMMON_TRAILING_ZEROS): New macro. (SMALL_AFTER_COMMON_TRAILING_SHIFT): Ditto. * config/riscv/riscv.md (*branch_shiftedarith__shifted): New pattern. gcc/testsuite/ChangeLog: * gcc.target/riscv/branch-1.c: Additional tests. CONFIDENTIALITY: The contents of this e-mail are confidential and intended only for the above addressee(s). If you are not the intended recipient, or the person responsible for delivering it to the intended recipient, copying or delivering it to anyone else or using it in any unauthorized manner is prohibited and may be unlawful. If you receive this e-mail by mistake, please notify the sender and the systems administrator at straymail@rt-rk.com immediately. --- gcc/config/riscv/iterators.md | 3 +++ gcc/config/riscv/riscv.h | 12 +++++++++ gcc/config/riscv/riscv.md | 32 +++++++++++++++++++++++ gcc/testsuite/gcc.target/riscv/branch-1.c | 16 +++++++++--- 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/gcc/config/riscv/iterators.md b/gcc/config/riscv/iterators.md index 2844cb02ff0..288f11464a2 100644 --- a/gcc/config/riscv/iterators.md +++ b/gcc/config/riscv/iterators.md @@ -233,6 +233,7 @@ (define_code_iterator any_ge [ge geu]) (define_code_iterator any_lt [lt ltu]) (define_code_iterator any_le [le leu]) +(define_code_iterator any_eq [eq ne]) ; atomics code iterator (define_code_iterator any_atomic [plus ior xor and]) @@ -283,6 +284,8 @@ (le "le") (gt "gt") (lt "lt") + (eq "eq") + (ne "ne") (ior "ior") (xor "xor") (and "and") diff --git a/gcc/config/riscv/riscv.h b/gcc/config/riscv/riscv.h index 3aecb43f831..c782499b2bf 100644 --- a/gcc/config/riscv/riscv.h +++ b/gcc/config/riscv/riscv.h @@ -667,6 +667,18 @@ enum reg_class /* True if bit BIT is set in VALUE. */ #define BITSET_P(VALUE, BIT) (((VALUE) & (1ULL << (BIT))) != 0) +/* Returns the smaller (common) number of trailing zeros for VAL1 and VAL2. */ +#define COMMON_TRAILING_ZEROS(VAL1, VAL2) \ + (ctz_hwi (VAL1) < ctz_hwi (VAL2) \ + ? ctz_hwi (VAL1) \ + : ctz_hwi (VAL2)) + +/* Returns true if both VAL1 and VAL2 are SMALL_OPERANDs after shifting by + the common number of trailing zeros. */ +#define SMALL_AFTER_COMMON_TRAILING_SHIFT(VAL1, VAL2) \ + (SMALL_OPERAND ((VAL1) >> COMMON_TRAILING_ZEROS (VAL1, VAL2)) \ + && SMALL_OPERAND ((VAL2) >> COMMON_TRAILING_ZEROS (VAL1, VAL2))) + /* Stack layout; function entry, exit and calling. */ #define STACK_GROWS_DOWNWARD 1 diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md index 0410d990ec5..afc534d050f 100644 --- a/gcc/config/riscv/riscv.md +++ b/gcc/config/riscv/riscv.md @@ -3129,6 +3129,38 @@ } [(set_attr "type" "branch")]) +(define_insn_and_split "*branch_shiftedarith__shifted" + [(set (pc) + (if_then_else (any_eq + (and:ANYI (match_operand:ANYI 1 "register_operand" "r") + (match_operand 2 "shifted_const_arith_operand" "i")) + (match_operand 3 "shifted_const_arith_operand" "i")) + (label_ref (match_operand 0 "" "")) + (pc))) + (clobber (match_scratch:X 4 "=&r")) + (clobber (match_scratch:X 5 "=&r"))] + "!SMALL_OPERAND (INTVAL (operands[2])) + && !SMALL_OPERAND (INTVAL (operands[3])) + && SMALL_AFTER_COMMON_TRAILING_SHIFT (INTVAL (operands[2]), + INTVAL (operands[3]))" + "#" + "&& reload_completed" + [(set (match_dup 4) (lshiftrt:X (match_dup 1) (match_dup 7))) + (set (match_dup 4) (and:X (match_dup 4) (match_dup 8))) + (set (match_dup 5) (match_dup 9)) + (set (pc) (if_then_else (any_eq (match_dup 4) (match_dup 5)) + (label_ref (match_dup 0)) (pc)))] +{ + HOST_WIDE_INT mask1 = INTVAL (operands[2]); + HOST_WIDE_INT mask2 = INTVAL (operands[3]); + int trailing_shift = COMMON_TRAILING_ZEROS (mask1, mask2); + + operands[7] = GEN_INT (trailing_shift); + operands[8] = GEN_INT (mask1 >> trailing_shift); + operands[9] = GEN_INT (mask2 >> trailing_shift); +} +[(set_attr "type" "branch")]) + (define_insn_and_split "*branch_shiftedmask_equals_zero" [(set (pc) (if_then_else (match_operator 1 "equality_operator" diff --git a/gcc/testsuite/gcc.target/riscv/branch-1.c b/gcc/testsuite/gcc.target/riscv/branch-1.c index b4a3a946379..e09328fe705 100644 --- a/gcc/testsuite/gcc.target/riscv/branch-1.c +++ b/gcc/testsuite/gcc.target/riscv/branch-1.c @@ -28,10 +28,20 @@ void f4(long long a) g(); } +void f5(long long a) { + if ((a & 0x2120000) == 0x2000000) + g(); +} + +void f6(long long a) { + if ((a & 0x70000000) == 0x30000000) + g(); +} + /* { dg-final { scan-assembler-times "slli\t" 2 } } */ -/* { dg-final { scan-assembler-times "srli\t" 3 } } */ -/* { dg-final { scan-assembler-times "andi\t" 1 } } */ -/* { dg-final { scan-assembler-times "\tli\t" 1 } } */ +/* { dg-final { scan-assembler-times "srli\t" 5 } } */ +/* { dg-final { scan-assembler-times "andi\t" 3 } } */ +/* { dg-final { scan-assembler-times "\tli\t" 3 } } */ /* { dg-final { scan-assembler-not "addi\t" } } */ /* { dg-final { scan-assembler-not "and\t" } } */