{"id":2232218,"url":"http://patchwork.ozlabs.org/api/patches/2232218/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/patch/20260503235736.389141-1-herumi@nifty.com/","project":{"id":17,"url":"http://patchwork.ozlabs.org/api/projects/17/?format=json","name":"GNU Compiler Collection","link_name":"gcc","list_id":"gcc-patches.gcc.gnu.org","list_email":"gcc-patches@gcc.gnu.org","web_url":null,"scm_url":null,"webscm_url":null,"list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<20260503235736.389141-1-herumi@nifty.com>","list_archive_url":null,"date":"2026-05-03T23:57:36","name":"[v3,2/3] i386: Add BMI2 MULX pattern for highpart-only multiplication","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"3db230fde6fa4c74e1216dee2ae8e7025939c21d","submitter":{"id":92964,"url":"http://patchwork.ozlabs.org/api/people/92964/?format=json","name":null,"email":"herumi@nifty.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/gcc/patch/20260503235736.389141-1-herumi@nifty.com/mbox/","series":[{"id":502594,"url":"http://patchwork.ozlabs.org/api/series/502594/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/list/?series=502594","date":"2026-05-03T23:57:36","name":null,"version":3,"mbox":"http://patchwork.ozlabs.org/series/502594/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2232218/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2232218/checks/","tags":{},"related":[],"headers":{"Return-Path":"<gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org>","X-Original-To":["incoming@patchwork.ozlabs.org","gcc-patches@gcc.gnu.org"],"Delivered-To":["patchwork-incoming@legolas.ozlabs.org","gcc-patches@gcc.gnu.org"],"Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=nifty.com header.i=@nifty.com header.a=rsa-sha256\n header.s=default-1th84yt82rvi header.b=Se5HtQbK;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org\n (client-ip=38.145.34.32; helo=vm01.sourceware.org;\n envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org;\n receiver=patchwork.ozlabs.org)","sourceware.org;\n\tdkim=pass (2048-bit key,\n unprotected) header.d=nifty.com header.i=@nifty.com header.a=rsa-sha256\n header.s=default-1th84yt82rvi header.b=Se5HtQbK","sourceware.org;\n dmarc=pass (p=none dis=none) header.from=nifty.com","sourceware.org; spf=pass smtp.mailfrom=nifty.com","server2.sourceware.org;\n arc=none smtp.remote-ip=106.153.227.42"],"Received":["from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g81v45KCPz1yJV\n\tfor <incoming@patchwork.ozlabs.org>; Mon, 04 May 2026 09:58:23 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id 729834B99F58\n\tfor <incoming@patchwork.ozlabs.org>; Sun,  3 May 2026 23:58:20 +0000 (GMT)","from mta-snd-w10.mail.nifty.com (mta-snd-w10.mail.nifty.com\n [106.153.227.42])\n by sourceware.org (Postfix) with ESMTPS id 2142F4B9DB7F\n for <gcc-patches@gcc.gnu.org>; Sun,  3 May 2026 23:57:47 +0000 (GMT)","from sapp by mta-snd-w10.mail.nifty.com with ESMTP\n id <20260503235745723.TQVA.44671.sapp@nifty.com>;\n Mon, 4 May 2026 08:57:45 +0900"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org 729834B99F58","OpenDKIM Filter v2.11.0 sourceware.org 2142F4B9DB7F"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org 2142F4B9DB7F","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org 2142F4B9DB7F","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1777852669; cv=none;\n b=eUH+me1KYxrk/JuwK8I3zoSpVarGf0m9qbTvIR4mPYaNYHRc0wIbEJ/olSwDqSbsjr0AQgpTUXMqkKoRW/o3+gFAv8KOhC/tcMtvn5N0BUhWF6BhlF7uMSAQHO58EEEree056O9Syz6KIP/haMSP50qHp+4vHpcSVusiuscZPfg=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1777852669; c=relaxed/simple;\n bh=Vn+mRTlfibiolRiNsmfXie2gOAgfHHpjF4ocVoHIknc=;\n h=From:To:Subject:Date:Message-ID:MIME-Version:DKIM-Signature;\n b=eSRPp2O84iNe0N1nfIpyDgMonHWfR4xBTT3uawgLP69+zq5F4H9hUGXSTFUdJtijbnWrWTlWdH0N5/HZ2mIbf3gDWiVekUN5CIfUWF5ZVZ429YqT/7wwLFtG+1XvbyafAkW3ZALK7vJXjIdTqcV1bxXI0rp31gY/6L3LtaIgEXU=","ARC-Authentication-Results":"i=1; server2.sourceware.org","From":"herumi@nifty.com","To":"gcc-patches@gcc.gnu.org","Cc":"MITSUNARI Shigeo <herumi@nifty.com>","Subject":"[PATCH v3 2/3] i386: Add BMI2 MULX pattern for highpart-only\n multiplication","Date":"Mon,  4 May 2026 08:57:36 +0900","Message-ID":"<20260503235736.389141-1-herumi@nifty.com>","X-Mailer":"git-send-email 2.43.0","In-Reply-To":"<20260503053451.48504-3-herumi@nifty.com>","References":"<20260503053451.48504-3-herumi@nifty.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com;\n s=default-1th84yt82rvi; t=1777852665;\n bh=FuORLj9oBM21E2QfBa1o/maQNdVJJeB/R+xujwf228c=;\n h=From:To:Cc:Subject:Date:In-Reply-To:References;\n b=Se5HtQbKqf7JqgcLolGlzLJWKqdKSPvbdC0DU8DAyvYctrVQHeM51qwNOxcT1MXOTCitJ9us\n eEWYg2t9VsAneOA0svyeISrhhKXPKQyY+/mVRu+JHgvu5vRLqI40J26qsYa1O+ozn4SH3zb6yb\n PkRRUjBpWvmzcjzMsc7nU6Sc8fMBcg28obnHVOETLIf4nNH6HOyhRukEQGONEiNSpsS8ukVwQS\n ypJ+e7W2gGnVsTlVu1RAmP36v6dZpOxWMdyyLKRze7SYpioyw1AUVUSJeOulzDKIjwiB8FYKlI\n NOJCYpkttGeioqvunNlKR5ouTqrr72GEzcHEzjtySLsiFE6w==","X-BeenThere":"gcc-patches@gcc.gnu.org","X-Mailman-Version":"2.1.30","Precedence":"list","List-Id":"Gcc-patches mailing list <gcc-patches.gcc.gnu.org>","List-Unsubscribe":"<https://gcc.gnu.org/mailman/options/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe>","List-Archive":"<https://gcc.gnu.org/pipermail/gcc-patches/>","List-Post":"<mailto:gcc-patches@gcc.gnu.org>","List-Help":"<mailto:gcc-patches-request@gcc.gnu.org?subject=help>","List-Subscribe":"<https://gcc.gnu.org/mailman/listinfo/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe>","Errors-To":"gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org"},"content":"From: MITSUNARI Shigeo <herumi@nifty.com>\n\nAdd a new instruction pattern that uses MULX to compute only the high\npart of an unsigned multiplication on BMI2 targets.  Previously, when\nonly the high part was needed, GCC would emit MULQ followed by a MOV\nto retrieve the result from RDX.  With this pattern, MULX writes the\nhigh part directly to the destination register, saving one instruction.\n\nThis benefits unsigned 32-bit integer division by constants that require\n33-bit magic multipliers (e.g., division by 7), reducing the sequence\nfrom 4 instructions to 3 on BMI2 targets.\n\nBefore:\n    movabsq $2635249153617166336, %rcx\n    movl    %edi, %eax\n    mulq    %rcx\n    movl    %edx, %eax\n\nAfter:\n    movabsq $2635249153617166336, %rax\n    movl    %edi, %edx\n    mulx    %rax, %rdx, %rax\n\ngcc/ChangeLog:\n\n\t* config/i386/i386.md (*bmi2_umul<mode>3_highpart): New pattern.\n---\n gcc/config/i386/i386.md                        | 15 +++++++++++++++\n .../gcc.target/i386/bmi2-mulx-highpart-1.c     | 18 ++++++++++++++++++\n 2 files changed, 33 insertions(+)\n create mode 100644 gcc/testsuite/gcc.target/i386/bmi2-mulx-highpart-1.c","diff":"diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md\nindex e514809453d..472f9d41332 100644\n--- a/gcc/config/i386/i386.md\n+++ b/gcc/config/i386/i386.md\n@@ -11507,6 +11507,21 @@\n \t      (set (match_dup 5)\n \t\t   (umul_highpart:DWIH (match_dup 2) (match_dup 3)))])])\n \n+;; BMI2 MULX highpart-only pattern.  Uses MULX to get only the high part,\n+;; discarding the low part into a scratch register.  This avoids the\n+;; mov from rdx after mulq when only the high part is needed.\n+(define_insn \"*bmi2_umul<mode>3_highpart\"\n+  [(set (match_operand:DWIH 0 \"register_operand\" \"=r\")\n+\t(umul_highpart:DWIH\n+\t  (match_operand:DWIH 1 \"register_operand\" \"d\")\n+\t  (match_operand:DWIH 2 \"nonimmediate_operand\" \"rm\")))\n+   (clobber (match_scratch:DWIH 3 \"=r\"))]\n+  \"TARGET_BMI2\"\n+  \"mulx\\t{%2, %3, %0|%0, %3, %2}\"\n+  [(set_attr \"type\" \"imulx\")\n+   (set_attr \"prefix\" \"vex\")\n+   (set_attr \"mode\" \"<MODE>\")])\n+\n ;; Highpart multiplication patterns\n (define_insn \"<s>mul<mode>3_highpart\"\n   [(set (match_operand:DWIH 0 \"register_operand\" \"=d\")\ndiff --git a/gcc/testsuite/gcc.target/i386/bmi2-mulx-highpart-1.c b/gcc/testsuite/gcc.target/i386/bmi2-mulx-highpart-1.c\nnew file mode 100644\nindex 00000000000..dd7b32aa0b0\n--- /dev/null\n+++ b/gcc/testsuite/gcc.target/i386/bmi2-mulx-highpart-1.c\n@@ -0,0 +1,18 @@\n+/* { dg-do compile { target { ! ia32 } } } */\n+/* { dg-options \"-O2 -mbmi2\" } */\n+/* { dg-final { check-function-bodies \"**\" \"\" \"\" { target *-*-linux* *-*-gnu* } } } */\n+\n+/*\n+**div7:\n+**\tmovabsq\t\\$2635249153617166336, %rax\n+**\tmovl\t%edi, %edx\n+**\tmulx\t%rax, %rdx, %rax\n+**\tret\n+**...\n+*/\n+\n+unsigned int\n+div7 (unsigned int x)\n+{\n+  return x / 7;\n+}\n","prefixes":["v3","2/3"]}