From patchwork Fri Nov 10 06:12:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: liuhongt X-Patchwork-Id: 163775 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b129:0:b0:403:3b70:6f57 with SMTP id q9csp918395vqs; Thu, 9 Nov 2023 22:14:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IE5zYUaWt7rQT1zi/pD7gspyNBeWa5BXzyim/5l4p1rP64yI/GQMzMgxi4rLGfSQ8oS3A+G X-Received: by 2002:a05:622a:1885:b0:418:1f52:1421 with SMTP id v5-20020a05622a188500b004181f521421mr8819117qtc.9.1699596897918; Thu, 09 Nov 2023 22:14:57 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1699596897; cv=pass; d=google.com; s=arc-20160816; b=dlGd/HlMal/Popo1A5QWHWMPBgkuU3Ad5lwWe5uxBCn7pSZkiNPemJ5cAU8MVJRe+2 Cdl+UYRG2Y1UQUN56FSSvSNvS+azrXX06c0WfJ3xO+XqI1DKwHBN0sZZhzTfnNSvXX/D RsUGMF3Li2NKS6DHk1WMskGNPL5qDnSH4HRGj83p4XiSCVTTPYe7dyBkvV3ybe5H7kvb Vc+XQr/Gks7GdOEdogOky5ameSNUFCx537t4U8HfoXVmsZXJ+yHRVMsrIJYOTkHYM6hM Q+YoE45Q9QPvXHb7SH9fduDCX5Z87R+h0Q8Ow8pKYh/HoX6vn82SlQUK99Brl5NMRmu/ aVXA== 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-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=RZUxMtNPAiEh8m2M5dGERRqUxkNKA0w/cr4iJJ/4NhA=; fh=ChXOctppJn0KECDRINafwUY5xHRufGHaa0Ju9pddrcQ=; b=OTofWBXGylSAsxKL90ZIk7+m2qq4BzknCFEiOqUNwHBa1eSUHrw7/qPTUHO2W9PdlZ XG7NR50sRKroPzjI2i+Z3a7K6gRdp/KNFd+d/sQjDBTjGK1ETBoRN8TOWIJ/2GqNeY3o fGZ/ze+g2Y5MZY6JMnahYp9BYWy8J1Lusg0lMtktttRnkxSz2I6JpJ0jP6hhhUO6kOkf KOeSKaSRzgVNEui+iM9Marnkhjyr13vcekHsfxm3s4ygzuUUOjb+DTa7yLfAuGXyWDbN 1Sd4/FPaaryasYfkhs6xfC45jZtU3T9Xpk/t1JAERyYU42ig59r/ZCa+Tg61m2uF+zK9 BEuw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="J/8JIzjR"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id y15-20020a05622a164f00b0041cbefbf430si3989110qtj.240.2023.11.09.22.14.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 22:14:57 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="J/8JIzjR"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id ADFE23857714 for ; Fri, 10 Nov 2023 06:14:57 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.10]) by sourceware.org (Postfix) with ESMTPS id 9F7E13858D1E for ; Fri, 10 Nov 2023 06:14:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9F7E13858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=intel.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9F7E13858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.10 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699596874; cv=none; b=MOhFd/63wnRkYDek/V+DdOQZ9YEppFFBzRCC8WzimvPRz1H6vBEvvRXsLdX6s4eaYl+jP57gIMOQ1z7ROhI1QPMWyRHTMp+6FaalRfqhfBsRsON9ATcWiBYOO15vJF2OxLfLjJZ9brxhinmbQ5qL2ogc/lj+8Ss3KdqKOgpVvxU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699596874; c=relaxed/simple; bh=WBGeHUeiB3TLqeI+SOLdztd/+MPhWjoSAc8GBEIP6FM=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=S0rc09PJuVaPK4Ir0wqO79ID0ykzmuH1Uyb8mSRnfOvXEwT3qqC1Ljnpbss2QH2gWpHO3UW6tbF2wdbE5HlOYcz5DgbrHbc74goBZ3HJyhuMzLZdM9is/525mrPjIuM9v7qfg284jUToCcgsQxGb+NGYROcRJkHmv1at/5uxHCQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1699596873; x=1731132873; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=WBGeHUeiB3TLqeI+SOLdztd/+MPhWjoSAc8GBEIP6FM=; b=J/8JIzjRuoI+Dyy4LSRv34QTstWfgW5yNU4FCOf4HwvvtRAbyn1o0gXt ONnjNNqnMyAn+69qk7tYEMiV8sYPGBQLBfA4cwBHUoy3TabkUWKlJSaMW joUEgCJW9dOSa5A61Ys+7fvJrS28PUJSL1dzqnyc0JikFzlU6YkNFcST3 8D1HQgvgcrFkhk+mYF68tiAOgRegj+9Jgcykple1UaF4fpL+2nmYGOH2c rsvaXCqWFxMjVx1er+IjuPZ3B5dRXAQILEUmbyVzvrGehFu0sYKInQvsa L11qZ6cRp0Lg27RIWKVZfpSSPShXYjTw/cLKPO5YWX76Sca/Lhyw+21VY A==; X-IronPort-AV: E=McAfee;i="6600,9927,10889"; a="3175436" X-IronPort-AV: E=Sophos;i="6.03,291,1694761200"; d="scan'208";a="3175436" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Nov 2023 22:14:32 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10889"; a="829553615" X-IronPort-AV: E=Sophos;i="6.03,291,1694761200"; d="scan'208";a="829553615" Received: from shvmail03.sh.intel.com ([10.239.245.20]) by fmsmga008.fm.intel.com with ESMTP; 09 Nov 2023 22:14:28 -0800 Received: from shliclel4217.sh.intel.com (shliclel4217.sh.intel.com [10.239.240.127]) by shvmail03.sh.intel.com (Postfix) with ESMTP id 3E7381005660; Fri, 10 Nov 2023 14:14:28 +0800 (CST) From: liuhongt To: gcc-patches@gcc.gnu.org Cc: crazylht@gmail.com, hjl.tools@gmail.com Subject: [PATCH] Simplify vector ((VCE?(a cmp b ? -1 : 0)) < 0) ? c : d to just VCE:((a cmp b) ? (VCE c) : (VCE d)). Date: Fri, 10 Nov 2023 14:12:28 +0800 Message-Id: <20231110061228.1411882-1-hongtao.liu@intel.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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: 1782156516994439803 X-GMAIL-MSGID: 1782156516994439803 When I'm working on PR112443, I notice there's some misoptimizations: after we fold _mm{,256}_blendv_epi8/pd/ps into gimple, the backend fails to combine it back to v{,p}blendv{v,ps,pd} since the pattern is too complicated, so I think maybe we should hanlde it in the gimple level. The dump is like _1 = c_3(D) >= { 0, 0, 0, 0 }; _2 = VEC_COND_EXPR <_1, { -1, -1, -1, -1 }, { 0, 0, 0, 0 }>; _7 = VIEW_CONVERT_EXPR(_2); _8 = VIEW_CONVERT_EXPR(b_6(D)); _9 = VIEW_CONVERT_EXPR(a_5(D)); _10 = _7 < { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; _11 = VEC_COND_EXPR <_10, _8, _9>; It can be optimized to _1 = c_2(D) >= { 0, 0, 0, 0 }; _6 = VEC_COND_EXPR <_1, b_5(D), a_4(D)>; since _7 is either -1 or 0, the selection of _7 < 0 ? _8 : _9 should be euqal to _1 ? b : a as long as TYPE_PRECISION of the component type of the second VEC_COND_EXPR is less equal to the first one. The patch add a gimple pattern to handle that. gcc/ChangeLog: * match.pd (VCE:(a cmp b ? -1 : 0) < 0) ? c : d ---> VCE:((a cmp b) ? (VCE:c) : (VCE:d)): New gimple simplication. gcc/testsuite/ChangeLog: * gcc.target/i386/avx512vl-blendv-3.c: New test. * gcc.target/i386/blendv-3.c: New test. --- gcc/match.pd | 19 ++++++++ .../gcc.target/i386/avx512vl-blendv-3.c | 6 +++ gcc/testsuite/gcc.target/i386/blendv-3.c | 46 +++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 gcc/testsuite/gcc.target/i386/avx512vl-blendv-3.c create mode 100644 gcc/testsuite/gcc.target/i386/blendv-3.c diff --git a/gcc/match.pd b/gcc/match.pd index dbc811b2b38..4d823882a7c 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5170,6 +5170,25 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (optimize_vectors_before_lowering_p () && types_match (@0, @3)) (vec_cond (bit_and @0 (bit_not @3)) @2 @1))) +(for cmp (simple_comparison) + (simplify + (vec_cond + (lt (view_convert?@5 (vec_cond@6 (cmp@4 @0 @1) + integer_all_onesp + integer_zerop)) + integer_zerop) @2 @3) + (if (VECTOR_INTEGER_TYPE_P (TREE_TYPE (@0)) + && VECTOR_INTEGER_TYPE_P (TREE_TYPE (@5)) + && !TYPE_UNSIGNED (TREE_TYPE (@5)) + && VECTOR_TYPE_P (TREE_TYPE (@6)) + && VECTOR_TYPE_P (type) + && (TYPE_PRECISION (TREE_TYPE (type)) + <= TYPE_PRECISION (TREE_TYPE (TREE_TYPE (@6)))) + && TYPE_SIZE (type) == TYPE_SIZE (TREE_TYPE (@6))) + (with { tree vtype = TREE_TYPE (@6);} + (view_convert:type + (vec_cond @4 (view_convert:vtype @2) (view_convert:vtype @3))))))) + /* c1 ? c2 ? a : b : b --> (c1 & c2) ? a : b */ (simplify (vec_cond @0 (vec_cond:s @1 @2 @3) @3) diff --git a/gcc/testsuite/gcc.target/i386/avx512vl-blendv-3.c b/gcc/testsuite/gcc.target/i386/avx512vl-blendv-3.c new file mode 100644 index 00000000000..2777e72ab5f --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/avx512vl-blendv-3.c @@ -0,0 +1,6 @@ +/* { dg-do compile } */ +/* { dg-options "-mavx512vl -mavx512bw -O2" } */ +/* { dg-final { scan-assembler-times {vp?blendv(?:b|p[sd])[ \t]*} 6 } } */ +/* { dg-final { scan-assembler-not {vpcmp} } } */ + +#include "blendv-3.c" diff --git a/gcc/testsuite/gcc.target/i386/blendv-3.c b/gcc/testsuite/gcc.target/i386/blendv-3.c new file mode 100644 index 00000000000..fa0fb067a73 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/blendv-3.c @@ -0,0 +1,46 @@ +/* { dg-do compile } */ +/* { dg-options "-mavx2 -O2" } */ +/* { dg-final { scan-assembler-times {vp?blendv(?:b|p[sd])[ \t]*} 6 } } */ +/* { dg-final { scan-assembler-not {vpcmp} } } */ + +#include + +__m256i +foo (__m256i a, __m256i b, __m256i c) +{ + return _mm256_blendv_epi8 (a, b, ~c < 0); +} + +__m256d +foo1 (__m256d a, __m256d b, __m256i c) +{ + __m256i d = ~c < 0; + return _mm256_blendv_pd (a, b, (__m256d)d); +} + +__m256 +foo2 (__m256 a, __m256 b, __m256i c) +{ + __m256i d = ~c < 0; + return _mm256_blendv_ps (a, b, (__m256)d); +} + +__m128i +foo4 (__m128i a, __m128i b, __m128i c) +{ + return _mm_blendv_epi8 (a, b, ~c < 0); +} + +__m128d +foo5 (__m128d a, __m128d b, __m128i c) +{ + __m128i d = ~c < 0; + return _mm_blendv_pd (a, b, (__m128d)d); +} + +__m128 +foo6 (__m128 a, __m128 b, __m128i c) +{ + __m128i d = ~c < 0; + return _mm_blendv_ps (a, b, (__m128)d); +}