From patchwork Tue Oct 17 13:08:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 154265 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp4122058vqb; Tue, 17 Oct 2023 06:09:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEGVVhGn8+PfFb3pHoZvU2GGNqB1LYlawFfjWZAbLC2uOT35GvdoWuBwRSDzA+K8cuj6Dy4 X-Received: by 2002:a25:8609:0:b0:d7a:8e37:6d4d with SMTP id y9-20020a258609000000b00d7a8e376d4dmr1967028ybk.43.1697548196399; Tue, 17 Oct 2023 06:09:56 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697548196; cv=pass; d=google.com; s=arc-20160816; b=mZH3ggh7gP3FJky3+ZDOFAO42H33DOsCHVVfTHyZRDjb9ztMpzDmNh+g/toytPzbQ1 lr3Hkyew9JhXqriM9WGEeeIKGQ0fEBuTzI/gA4TNwGwGwiLotGiY/hia1wGYuLVppFmL khUMcV89iFBjonQ4kzzchBm6LDHtCr83hsRWudZOcSF+GU3NtLTaBrlGmkbcXJi4gjR8 xk3+QuObo49weBdU8Uy0+muaTLJxQNwPdcbkmEQz3EcjjpB/8VkSn4AHV3EcGsH0HoEH rQW0l6PjFzt4TTbGAOul35q1o0x87jBl1yGDS3nfwElB19y/GU3Jra17Mz5Iop9aw5Qg J2Ag== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-language:thread-index :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=lorsakz8JdGauR/+qXJbtWuZsFUuB5x4TZqIBShIvd4=; fh=ez+UBk19YaOo+lQEyE9porlijlGbJDzUOtzUi3k96eQ=; b=Q9hVxaAbfpuRXFePcGY9QBG3RDOTESX44uNvYKeUbs2nJSlJ0lnWq91mN0w+UEm/Rq 0qnIMviMXxfqKvExPEfAYW3lTI+44JRgl8z6f0DxXDcrjb6/kqHaRTY2x2f2UR5NfZW3 aGR5X8YzmalyXReiV55KGrPT4bNLwjtHIaea+FzgCuvISUFpfSO3p9Xf3aBrU5/9S5Kw GTEBUczalZk4Z18zTXIHzZF1CoXFf0w6CfLOCuZ3wWQpWsn5FgwE5eE1gQQyADOro+Oc wZQ6BR0jvA7mz+4FAZ1FV4UIwYHTcXHec/1g0zHMewYJknZ/6LKrvHhOhBGCDRhHRvxD V6tQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@nextmovesoftware.com header.s=default header.b="Ujya/iTU"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id q8-20020a05620a024800b007788bb3ee9esi479237qkn.554.2023.10.17.06.09.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 06:09:56 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=fail header.i=@nextmovesoftware.com header.s=default header.b="Ujya/iTU"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4C6E0385CCBA for ; Tue, 17 Oct 2023 13:09:24 +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 [162.254.253.69]) by sourceware.org (Postfix) with ESMTPS id 220C9385840D for ; Tue, 17 Oct 2023 13:08:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 220C9385840D 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 220C9385840D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=162.254.253.69 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697548140; cv=none; b=jxOgNvFKFocgmKY2Epo9ctNRDXCxExei9TFM5etzRSSWX3fnuRp5IjQyPZJfbM4wLwvu1/keGZ0MJa5Nc0bUDNmPunk9BdEUIEbvlTudwJu1m6gdJ/ttwZZY9K55N9vhOs3Mnq0JLHTORSOigHPR2GRWTqpWR1Kngeaex+lnL2k= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697548140; c=relaxed/simple; bh=Qo0NjyzwnFJJ/Nj/qs+td+uzkXGTZoHO6MNeUZixtFE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=lNPOzIwPl8vNNgA0jCYH5ZAsmtJLcSyxQh4/8XzmQsOoewYFpcUizXg9SFxRVuvCgF/SVrIuDjcHzwGdwlwCL+K7g7phplYzOrwPLLxglFbEhz7JeM2z4mJItPE1oEDqGV2AbRD9AuoPQXW8I7semfeKDds70HndGHLx1McIg+4= 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=lorsakz8JdGauR/+qXJbtWuZsFUuB5x4TZqIBShIvd4=; b=Ujya/iTUUZB526XNbum43ImDdq Qqu0X+6uHvXvQM2gqu6LjSceAoGnyZElp7VZUTJyteAF8UzWlguNqswi1rWPBhLNPJpStOg7QLUpS URy0t9t+REe+K/fctXk5d6LzieFi39w3qauRDtybHQE6CUv5lkVXyOGG+wmT14N4WBLSFesFA2kHf NPr9nz4sPtax6L9PZwTzZpU+3SHay0K11cltEyr2GaicZ2IavBU3ipaBly0cRgfcbxOtAKaog4Ohc 2Ig6xOxnzYHKUQaRAjF7X7+nNz1vIhoHamhByLplc4bxDKB4DPByu9Hv/OBTBg1Tz1trsW4rxsIaz VHXxf91Q==; Received: from [185.62.158.67] (port=49429 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96.1) (envelope-from ) id 1qsjog-0007rv-1R; Tue, 17 Oct 2023 09:08:58 -0400 From: "Roger Sayle" To: Cc: "'Uros Bizjak'" Subject: [x86 PATCH] PR 106245: Split (x<<31)>>31 as -(x&1) in i386.md Date: Tue, 17 Oct 2023 14:08:57 +0100 Message-ID: <007b01da00fb$179e69e0$46db3da0$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdoA+pkQ+VatdKErRqGvTZUkdAaJCw== 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=-9.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, MEDICAL_SUBJECT, 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780008297895077588 X-GMAIL-MSGID: 1780008297895077588 This patch is the backend piece of a solution to PRs 101955 and 106245, that adds a define_insn_and_split to the i386 backend, to perform sign extension of a single (least significant) bit using AND $1 then NEG. Previously, (x<<31)>>31 would be generated as sall $31, %eax // 3 bytes sarl $31, %eax // 3 bytes with this patch the backend now generates: andl $1, %eax // 3 bytes negl %eax // 2 bytes Not only is this smaller in size, but microbenchmarking confirms that it's a performance win on both Intel and AMD; Intel sees only a 2% improvement (perhaps just a size effect), but AMD sees a 7% win. 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? 2023-10-17 Roger Sayle gcc/ChangeLog PR middle-end/101955 PR tree-optimization/106245 * config/i386/i386.md (*extv_1_0): New define_insn_and_split. gcc/testsuite/ChangeLog PR middle-end/101955 PR tree-optimization/106245 * gcc.target/i386/pr106245-2.c: New test case. * gcc.target/i386/pr106245-3.c: New 32-bit test case. * gcc.target/i386/pr106245-4.c: New 64-bit test case. * gcc.target/i386/pr106245-5.c: Likewise. Thanks in advance, Roger diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md index 2a60df5..b7309be0 100644 --- a/gcc/config/i386/i386.md +++ b/gcc/config/i386/i386.md @@ -3414,6 +3414,21 @@ [(set_attr "type" "imovx") (set_attr "mode" "SI")]) +;; Split sign-extension of single least significant bit as and x,$1;neg x +(define_insn_and_split "*extv_1_0" + [(set (match_operand:SWI48 0 "register_operand" "=r") + (sign_extract:SWI48 (match_operand:SWI48 1 "register_operand" "0") + (const_int 1) + (const_int 0))) + (clobber (reg:CC FLAGS_REG))] + "" + "#" + "&& 1" + [(parallel [(set (match_dup 0) (and:SWI48 (match_dup 1) (const_int 1))) + (clobber (reg:CC FLAGS_REG))]) + (parallel [(set (match_dup 0) (neg:SWI48 (match_dup 0))) + (clobber (reg:CC FLAGS_REG))])]) + (define_expand "extzv" [(set (match_operand:SWI248 0 "register_operand") (zero_extract:SWI248 (match_operand:SWI248 1 "register_operand") diff --git a/gcc/testsuite/gcc.target/i386/pr106245-2.c b/gcc/testsuite/gcc.target/i386/pr106245-2.c new file mode 100644 index 0000000..47b0d27 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr106245-2.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +int f(int a) +{ + return (a << 31) >> 31; +} + +/* { dg-final { scan-assembler "andl" } } */ +/* { dg-final { scan-assembler "negl" } } */ diff --git a/gcc/testsuite/gcc.target/i386/pr106245-3.c b/gcc/testsuite/gcc.target/i386/pr106245-3.c new file mode 100644 index 0000000..4ec6342 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr106245-3.c @@ -0,0 +1,11 @@ +/* { dg-do compile { target ia32 } } */ +/* { dg-options "-O2" } */ + +long long f(long long a) +{ + return (a << 63) >> 63; +} + +/* { dg-final { scan-assembler "andl" } } */ +/* { dg-final { scan-assembler "negl" } } */ +/* { dg-final { scan-assembler "cltd" } } */ diff --git a/gcc/testsuite/gcc.target/i386/pr106245-4.c b/gcc/testsuite/gcc.target/i386/pr106245-4.c new file mode 100644 index 0000000..ef77ee5 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr106245-4.c @@ -0,0 +1,10 @@ +/* { dg-do compile { target { ! ia32 } } } */ +/* { dg-options "-O2" } */ + +long long f(long long a) +{ + return (a << 63) >> 63; +} + +/* { dg-final { scan-assembler "andl" } } */ +/* { dg-final { scan-assembler "negq" } } */ diff --git a/gcc/testsuite/gcc.target/i386/pr106245-5.c b/gcc/testsuite/gcc.target/i386/pr106245-5.c new file mode 100644 index 0000000..0351866 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr106245-5.c @@ -0,0 +1,11 @@ +/* { dg-do compile { target int128 } } */ +/* { dg-options "-O2" } */ + +__int128 f(__int128 a) +{ + return (a << 127) >> 127; +} + +/* { dg-final { scan-assembler "andl" } } */ +/* { dg-final { scan-assembler "negq" } } */ +/* { dg-final { scan-assembler "cqto" } } */