From patchwork Fri Nov 4 00:04:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hongyu Wang X-Patchwork-Id: 15262 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp69701wru; Thu, 3 Nov 2022 17:07:25 -0700 (PDT) X-Google-Smtp-Source: AMsMyM42pP5AcJGHntc5hyST6GsveQU325pVDwHwqBRd2N9NH3QD4RVV40ikw8aS0irS35rZEsJn X-Received: by 2002:a05:6402:378c:b0:458:8053:6c5f with SMTP id et12-20020a056402378c00b0045880536c5fmr32296491edb.9.1667520445516; Thu, 03 Nov 2022 17:07:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667520445; cv=none; d=google.com; s=arc-20160816; b=YtCQZDjAascwNopbLApsEo2aJzlWf2X/ZGRPhXtso1ZszXl9K54lPrxCt/tcK0aWAF p3s4Ck5CZ9hTluKvW8WNTRRQiRsrBupXkQ/oyp6LVBYur5+IEcHQ9vVDVxAIqEbMxIq5 oJnkmsP1hMvH+rMeIE2wBgweCSBjAUxwwwiKBFjGE/Hyhxz4te5R0esJ7VQ1rgr4Slwp hdeKhywK0kF2HpJsbYx8FhRQ9VIY4mME2J53fx8iEyQIUEtSVReWIABEUQDMRYmc2f2a IGbf1ahL2hCJ6mrKOiSXWwOdt78kW87A8KUI7u1bJuhDu2HfgWvqsi5lYYGgd/fEEah9 y7Jg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :message-id:date:subject:to:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=5k+QUm8GCx7c7pVIunHmdB+16+Uk0/IbHQ9pzJgqOIE=; b=P0NWQfBpfF3zyaucXN9N+CPKCuV8aZ3ztOyuu/IB7jSgiE/KSruxyS8HPuTu7iFYrL QcBpvgfFgMH/9JF7e5AGl/ncDWKjMjjJhPEShNuikIRyXdE3/iMiE8m9m+vVgp9O8bkj QIqmxDvwbamjAH4ZxMf8C5jkERKvtzNtu6bKC8BYeq3y3HkyOBDUSfNkZwgOTeuK5Zks c6yaspuFkDo+rAZjS3r1GI99uvYclwiIZ7XmmXOf2xq8VPzSVaLonmv0SPyT80Fvqb8i 2IKOxMDt4OvYcXKr+zjkW/asOx+f9I14VMWhl54Xo7AfqWBllweFJwz+nbNkoQk4TwoK /kKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=bz5Z8k1C; 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"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id o7-20020a056402038700b004593fb0c125si2390036edv.103.2022.11.03.17.07.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Nov 2022 17:07:25 -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=pass header.i=@gcc.gnu.org header.s=default header.b=bz5Z8k1C; 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"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5FFC2385841E for ; Fri, 4 Nov 2022 00:07:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5FFC2385841E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667520444; bh=5k+QUm8GCx7c7pVIunHmdB+16+Uk0/IbHQ9pzJgqOIE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=bz5Z8k1C1/c+a46sntiiyGnuBvalOkSRRy8AUG5mRHGYqUAtF6Gn0VhTJCkfOwyGg WHetpcOonHHJKKefhSiLESmu+EViQ8u4zScOZd+aWOPL9qaOKMGWqDvTKO8tHv3EL8 zcdKj8ENOt6liZ8P2SPeT8KBx8qt0npsRakX9kFM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by sourceware.org (Postfix) with ESMTPS id 2F2813858D3C for ; Fri, 4 Nov 2022 00:06:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2F2813858D3C X-IronPort-AV: E=McAfee;i="6500,9779,10520"; a="309830879" X-IronPort-AV: E=Sophos;i="5.96,135,1665471600"; d="scan'208";a="309830879" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Nov 2022 17:06:35 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10520"; a="703881785" X-IronPort-AV: E=Sophos;i="5.96,135,1665471600"; d="scan'208";a="703881785" Received: from shvmail03.sh.intel.com ([10.239.245.20]) by fmsmga004.fm.intel.com with ESMTP; 03 Nov 2022 17:06:33 -0700 Received: from shliclel320.sh.intel.com (shliclel320.sh.intel.com [10.239.240.127]) by shvmail03.sh.intel.com (Postfix) with ESMTP id 0B0451005705; Fri, 4 Nov 2022 08:06:33 +0800 (CST) To: gcc-patches@gcc.gnu.org Subject: [PATCH] Optimize VEC_PERM_EXPR with same permutation index and operation [PR98167] Date: Fri, 4 Nov 2022 08:04:32 +0800 Message-Id: <20221104000432.15254-1-hongyu.wang@intel.com> X-Mailer: git-send-email 2.18.1 X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_SHORT, SPF_HELO_NONE, SPF_SOFTFAIL, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Hongyu Wang via Gcc-patches From: Hongyu Wang Reply-To: Hongyu Wang Cc: hongtao.liu@intel.com Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748521918816109222?= X-GMAIL-MSGID: =?utf-8?q?1748521918816109222?= Hi, This is a follow-up patch for PR98167 The sequence c1 = VEC_PERM_EXPR (a, a, mask) c2 = VEC_PERM_EXPR (b, b, mask) c3 = c1 op c2 can be optimized to c = a op b c3 = VEC_PERM_EXPR (c, c, mask) for all integer vector operation, and float operation with full permutation. Bootstrapped & regrtested on x86_64-pc-linux-gnu. Ok for trunk? gcc/ChangeLog: PR target/98167 * match.pd: New perm + vector op patterns for int and fp vector. gcc/testsuite/ChangeLog: PR target/98167 * gcc.target/i386/pr98167.c: New test. --- gcc/match.pd | 49 +++++++++++++++++++++++++ gcc/testsuite/gcc.target/i386/pr98167.c | 44 ++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 gcc/testsuite/gcc.target/i386/pr98167.c diff --git a/gcc/match.pd b/gcc/match.pd index 194ba8f5188..b85ad34f609 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -8189,3 +8189,52 @@ and, (bit_and (negate @0) integer_onep@1) (if (!TYPE_OVERFLOW_SANITIZED (type)) (bit_and @0 @1))) + +/* Optimize + c1 = VEC_PERM_EXPR (a, a, mask) + c2 = VEC_PERM_EXPR (b, b, mask) + c3 = c1 op c2 + --> + c = a op b + c3 = VEC_PERM_EXPR (c, c, mask) + For all integer non-div operations. */ +(for op (plus minus mult bit_and bit_ior bit_xor + lshift rshift) + (simplify + (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2)) + (if (VECTOR_INTEGER_TYPE_P (type)) + (vec_perm (op @0 @1) (op @0 @1) @2)))) + +/* Similar for float arithmetic when permutation constant covers + all vector elements. */ +(for op (plus minus mult) + (simplify + (op (vec_perm @0 @0 VECTOR_CST@2) (vec_perm @1 @1 VECTOR_CST@2)) + (if (VECTOR_FLOAT_TYPE_P (type)) + (with + { + tree perm_cst = @2; + vec_perm_builder builder; + bool full_perm_p = false; + if (tree_to_vec_perm_builder (&builder, perm_cst)) + { + /* Create a vec_perm_indices for the integer vector. */ + int nelts = TYPE_VECTOR_SUBPARTS (type).to_constant (); + vec_perm_indices sel (builder, 1, nelts); + + /* Check if perm indices covers all vector elements. */ + int count = 0, i, j; + for (i = 0; i < nelts; i++) + for (j = 0; j < nelts; j++) + { + if (sel[j].to_constant () == i) + { + count++; + break; + } + } + full_perm_p = count == nelts; + } + } + (if (full_perm_p) + (vec_perm (op @0 @1) (op @0 @1) @2)))))) diff --git a/gcc/testsuite/gcc.target/i386/pr98167.c b/gcc/testsuite/gcc.target/i386/pr98167.c new file mode 100644 index 00000000000..40e0ac11332 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr98167.c @@ -0,0 +1,44 @@ +/* PR target/98167 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -mavx2" } */ + +/* { dg-final { scan-assembler-times "vpshufd\t" 8 } } */ +/* { dg-final { scan-assembler-times "vpermilps\t" 3 } } */ + +#define VEC_PERM_4 \ + 2, 3, 1, 0 +#define VEC_PERM_8 \ + 4, 5, 6, 7, 3, 2, 1, 0 +#define VEC_PERM_16 \ + 8, 9, 10, 11, 12, 13, 14, 15, 7, 6, 5, 4, 3, 2, 1, 0 + +#define TYPE_PERM_OP(type, size, op, name) \ + typedef type v##size##s##type __attribute__ ((vector_size(4*size))); \ + v##size##s##type type##foo##size##i_##name (v##size##s##type a, \ + v##size##s##type b) \ + { \ + v##size##s##type a1 = __builtin_shufflevector (a, a, \ + VEC_PERM_##size); \ + v##size##s##type b1 = __builtin_shufflevector (b, b, \ + VEC_PERM_##size); \ + return a1 op b1; \ + } + +#define INT_PERMS(op, name) \ + TYPE_PERM_OP (int, 4, op, name) \ + +#define FP_PERMS(op, name) \ + TYPE_PERM_OP (float, 4, op, name) \ + +INT_PERMS (+, add) +INT_PERMS (-, sub) +INT_PERMS (*, mul) +INT_PERMS (|, ior) +INT_PERMS (^, xor) +INT_PERMS (&, and) +INT_PERMS (<<, shl) +INT_PERMS (>>, shr) +FP_PERMS (+, add) +FP_PERMS (-, sub) +FP_PERMS (*, mul) +