From patchwork Sat Jun 29 16:21:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 1954266 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=nextmovesoftware.com header.i=@nextmovesoftware.com header.a=rsa-sha256 header.s=default header.b=Zun916P/; 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 (unknown [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 4WBHd86m41z20Xf for ; Sun, 30 Jun 2024 02:22:02 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2E040389CF3B for ; Sat, 29 Jun 2024 16:21:50 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [69.48.154.134]) by sourceware.org (Postfix) with ESMTPS id DC2FD38185E4 for ; Sat, 29 Jun 2024 16:21:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DC2FD38185E4 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nextmovesoftware.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org DC2FD38185E4 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=69.48.154.134 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719678089; cv=none; b=dxqRUhrYLgbCRO1CDz64bSfATMZik6Q5svhXgaf/rwWlRc31egrH1qoFlHtkMQlRt6zD5wVIExlSt/T+HwVVbqnZbEEPvhKlKOJVwlAS10hGkvhnFehOU/pHbjBpglT3h5qE2KujUhAKNTaSqdTyxdPtzlPgyJKp83wD+7YsfoU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719678089; c=relaxed/simple; bh=FeRHLVYHL7VYhVGsVqrnkieMUDm/fQxsBIIYJ3o7UEE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=qUSD+EjdpKJN9Hm79Or+dcXmEkk9ZNqNqJBjZcUJr1OjsZL0X3iAzhAxlNs0NQ4xY3Q1LsFOXisUzc8/09TJ7ARulGGlygh2vwhkKlBC6ZbqAT45N1nIQhFVa6kT4+5iq8KNNp0fjomLnMeQI+oWQx2e/ASIcc4sKKKm/AlJi/o= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nextmovesoftware.com; s=default; h=Content-Type:MIME-Version:Message-ID: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=zLxvOikccIe5DixtV075r/c61ZvNKbiQQyHyvyzo+X8=; b=Zun916P/gJ+cAyob4zYM4OupU4 NQenH0niCydy/qmFgi6MJYuaSWUpcuzBARKVGCjpltbZlRN3V7LhIrXuPAUWZcdMEPBXjQlRdhTP6 Y4Ee7KmZMlmO9BwSsxuL6JEiuiNSwS0BoiQEPDy4tpFzkTnMCwoWO5Jwf/wrmoaN8z9CCBBcWwdaJ 3LASCAT14+ZAJQcnj0Z2G7aFHCbSKD8R8QUGCFCxCAkKYdUAUcFzKbX5TkURPUXqor54sPcxKg/Ow r9AqB0GWJKv7uhguCsVmMa0VpjMY9+oehN/LN0OXEWVI21pwRsDDoVbPBqVaIIrNWdQQD7E+ieeG7 ILsd0X4Q==; Received: from [168.86.198.82] (port=53573 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.97.1) (envelope-from ) id 1sNapL-000000024zQ-1IYZ; Sat, 29 Jun 2024 12:21:27 -0400 From: "Roger Sayle" To: Cc: "'Uros Bizjak'" Subject: [x86 PATCH]: Additional peephole2 to use lea in round-up integer division. Date: Sat, 29 Jun 2024 17:21:24 +0100 Message-ID: <00c401daca40$64626a60$2d273f20$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdrKPfTa3M+grNzTSKSxR84jGukUqQ== Content-Language: en-gb X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server.nextmovesoftware.com X-AntiAbuse: Original Domain - gcc.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - nextmovesoftware.com X-Get-Message-Sender-Via: server.nextmovesoftware.com: authenticated_id: roger@nextmovesoftware.com X-Authenticated-Sender: server.nextmovesoftware.com: roger@nextmovesoftware.com X-Source: X-Source-Args: X-Source-Dir: X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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 A common idiom for implementing an integer division that rounds upwards is to write (x + y - 1) / y. Conveniently on x86, the two additions to form the numerator can be performed by a single lea instruction, and indeed gcc currently generates a lea when x and y both registers. int foo(int x, int y) { return (x+y-1)/y; } generates with -O2: foo: leal -1(%rsi,%rdi), %eax // 4 bytes cltd idivl %esi ret Oddly, however, if x is a memory, gcc currently uses two instructions: int m; int bar(int y) { return (m+y-1)/y; } generates: foo: movl m(%rip), %eax addl %edi, %eax // 2 bytes subl $1, %eax // 3 bytes cltd idivl %edi ret This discrepancy is caused by the late decision (in peephole2) to split an addition with a memory operand, into a load followed by a reg-reg addition. This patch improves this situation by adding a peephole2 to recognized consecutive additions and transform them into lea if profitable. My first attempt at fixing this was to use a define_insn_and_split: (define_insn_and_split "*lea3_reg_mem_imm" [(set (match_operand:SWI48 0 "register_operand") (plus:SWI48 (plus:SWI48 (match_operand:SWI48 1 "register_operand") (match_operand:SWI48 2 "memory_operand")) (match_operand:SWI48 3 "x86_64_immediate_operand")))] "ix86_pre_reload_split ()" "#" "&& 1" [(set (match_dup 4) (match_dup 2)) (set (match_dup 0) (plus:SWI48 (plus:SWI48 (match_dup 1) (match_dup 4)) (match_dup 3)))] "operands[4] = gen_reg_rtx (mode);") using combine to combine instructions. Unfortunately, this approach interferes with (reload's) subtle balance of deciding when to use/avoid lea, which can be observed as a code size regression in CSiBE. The peephole2 approach (proposed here) uniformly improves CSiBE results. This patch has been tested on x86_64-pc-linux-gnu with make bootstrap and make -k check, both with and without --target_board=unix{-m32} with no new failures. Ok for mainline? 2024-06-29 Roger Sayle gcc/ChangeLog * config/i386/i386.md (peephole2): Transform two consecutive additions into a 3-component lea if !TARGET_AVOID_LEA_FOR_ADDR. gcc/testsuite/ChageLog * gcc.target/i386/lea-3.c: New test case. Thanks in advance, Roger diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md index fd48e76..66ef234 100644 --- a/gcc/config/i386/i386.md +++ b/gcc/config/i386/i386.md @@ -6332,6 +6332,21 @@ "TARGET_APX_NF && reload_completed" [(set (match_dup 0) (ashift:SWI48 (match_dup 0) (match_dup 1)))] "operands[1] = GEN_INT (exact_log2 (INTVAL (operands[1])));") + +;; The peephole2 pass may expose consecutive additions suitable for lea. +(define_peephole2 + [(parallel [(set (match_operand:SWI48 0 "register_operand") + (plus:SWI48 (match_dup 0) + (match_operand 1 "register_operand"))) + (clobber (reg:CC FLAGS_REG))]) + (parallel [(set (match_dup 0) + (plus:SWI48 (match_dup 0) + (match_operand 2 "x86_64_immediate_operand"))) + (clobber (reg:CC FLAGS_REG))])] + "!TARGET_AVOID_LEA_FOR_ADDR || optimize_function_for_size_p (cfun)" + [(set (match_dup 0) (plus:SWI48 (plus:SWI48 (match_dup 0) + (match_dup 1)) + (match_dup 2)))]) ;; Add instructions