From patchwork Tue Sep 1 18:17:35 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 142657 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 64B604BA23E2 for ; Tue, 1 Sep 2026 18:18:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 64B604BA23E2 Authentication-Results: sourceware.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=gXw14CZw 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 9A9D84BA2E12 for ; Tue, 1 Sep 2026 18:17:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9A9D84BA2E12 Authentication-Results: sourceware.org; dmarc=pass (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 9A9D84BA2E12 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=69.48.154.134 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1788286657; cv=none; b=i8OmB7+XqjAYfA1pWXBGM3xA+sOVyhrysLYg2a0QOvQmYNjPqyoojlDV4GHR2x8ls4KHYpWFZdEGxQIfi45+4k/k4q0qH+CDy50mTr8TfNEnCpMPzOzLxqcy8YkF6jIW+CftC4j+fEnXqltABaz+S1teljuug+D1zRsgOB7Jbgo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1788286657; c=relaxed/simple; bh=W7IbIUrj5Ls2uqlA6y8Zhv9dJb0ZEEfVp5bmGCnshpg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=UNHnS7kRdvnQxHKlCvSMb2fmDCMNdQn5gWfUwQKo8v4u+Y+drXmhRlAbjN5vGTM7OZsZuxMp/puzh1alqTjcw7myX+qiDWeR1Y4RzrETl8Uw3d1HH5MeDtJ77gX8PQq6DJWstiaJIoeWFt7e/AcLMyZn+3ogWojzzbBorSlMKpQ= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=nextmovesoftware.com header.i=@nextmovesoftware.com header.a=rsa-sha256 header.s=default header.b=gXw14CZw DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9A9D84BA2E12 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:To:From:Sender:Reply-To:Cc: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=5PnNH9t2xihet1dQqEipo6ER47r4gK9kZ4NNBE9iB1A=; b=gXw14CZwvkBU/fEBmN9VM2e+1R ox2IFHOZSq8+sGBYH8jLUUtlO3i9oY/8seg1jv7LLTy7/OEM31bjpJ4bLwuln6EDx1rCTNvvjW0aO +f81PBpolx1lFcQvZCYZjn7CsOqHWhluUsb4nS8ILWa3z50Qv60CkbrU0X3VcrrfPIWID7n65Z6ns QbGLBnC2sUs6icu6X0iFs55j5Ra2z/yoQQQcLmjm89LM6Y91sN2sImzwi+SsC5dSio0KkaxWdGcOn 98FcErx1m6gXGeW17ge3dr2eWbchrOY/kAdFA+RZ2y7EVrXMxcsJCBxP/PrMjelg/B95GBjHlFjKp 3f3rboNg==; Received: from [168.86.198.68] (port=49709 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.99.5) (envelope-from ) id 1x1T3A-0000000B42W-2l5z for gcc-patches@gcc.gnu.org; Tue, 01 Sep 2026 14:17:36 -0400 From: "Roger Sayle" To: Subject: [AArch64 PATCH] Use any_or_plus in movk define_insn pattern. Date: Tue, 1 Sep 2026 19:17:35 +0100 Message-ID: <003f01dd3a3e$2aa73f10$7ff5bd30$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: Ad06PaS4YnpZJULPQjKCU8/bmbEvgA== 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.6 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 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on 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~patchwork=sourceware.org@gcc.gnu.org Many thanks to Linaroo's CI tester (and the folks that run it) for pointing out that my (upcoming) patch to introduce aop_optab triggers a testsuite failure on aarch64. The issue is a missed optimization: the aarch.md backend recognizes the IOR form of movk, but not the PLUS (or XOR) forms. Easily fixed/avoided with the small change below. As a motivating example, consider the function: unsigned int foo(unsigned int x) { return (x & ~0xffff) + 0x02; } Currently with gcc -O2, we generate: foo: and w0, w0, -65536 add w0, w0, 2 ret with this patch, we instead generate: foo: movk w0, #0x2, lsl 0 ret This patch has been tested on aarch64-apple-darwin24.3.0 (using iains' fork) with make bootstrap and make -k check with no new failures. Ok for mainline? 2026-09-01 Roger Sayle gcc/ChangeLog * config/aarch64/aarch64.md (*aarch_movk): Prepend asterisk. Use any_or_plus iterator to also recognize PLUS and XOR forms. gcc/testsuite/ChangeLog * gcc.target/aarch64/movk_4.c: New test case. Thank in advance, Roger diff --git a/gcc/config/aarch64/aarch64.md b/gcc/config/aarch64/aarch64.md index 8e844beaee9..b3452d89989 100644 --- a/gcc/config/aarch64/aarch64.md +++ b/gcc/config/aarch64/aarch64.md @@ -1915,11 +1915,12 @@ ) ;; Match MOVK as a normal AND and IOR operation. -(define_insn "aarch64_movk" +(define_insn "*aarch64_movk" [(set (match_operand:GPI 0 "register_operand" "=r") - (ior:GPI (and:GPI (match_operand:GPI 1 "register_operand" "0") - (match_operand:GPI 2 "const_int_operand")) - (match_operand:GPI 3 "const_int_operand")))] + (any_or_plus:GPI + (and:GPI (match_operand:GPI 1 "register_operand" "0") + (match_operand:GPI 2 "const_int_operand")) + (match_operand:GPI 3 "const_int_operand")))] "aarch64_movk_shift (rtx_mode_t (operands[2], mode), rtx_mode_t (operands[3], mode)) >= 0" { diff --git a/gcc/testsuite/gcc.target/aarch64/movk_4.c b/gcc/testsuite/gcc.target/aarch64/movk_4.c new file mode 100644 index 00000000000..1173e58cf0f --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/movk_4.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +unsigned int iorhi(unsigned int x) { return (x & 0xffff) | 0x20000; } +unsigned int xorhi(unsigned int x) { return (x & 0xffff) ^ 0x20000; } +unsigned int addhi(unsigned int x) { return (x & 0xffff) + 0x20000; } + +unsigned int iorlo(unsigned int x) { return (x & ~0xffff) | 0x02; } +unsigned int xorlo(unsigned int x) { return (x & ~0xffff) ^ 0x02; } +unsigned int addlo(unsigned int x) { return (x & ~0xffff) + 0x02; } + +/* { dg-final { scan-assembler-times "movk\t" 6 } } */