From patchwork Thu Nov 16 10:47:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: liuhongt X-Patchwork-Id: 165755 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b909:0:b0:403:3b70:6f57 with SMTP id t9csp3124896vqg; Thu, 16 Nov 2023 02:49:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IF6iVe8w7+dWOe1evkDl68npaieGyiVQOBTwsTQJS6kq4idf7ud1yc3Ly9DUYZXpznFXGZ7 X-Received: by 2002:a05:620a:1725:b0:774:3963:41a5 with SMTP id az37-20020a05620a172500b00774396341a5mr9273942qkb.13.1700131782051; Thu, 16 Nov 2023 02:49:42 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1700131782; cv=pass; d=google.com; s=arc-20160816; b=a6J2+fLs3dDqzLudjJJoMzN7SsKl0WDay7i8LyYH6VstUjefhpE3MS5NKdMduaF120 UfPJDdBkMQXQrRGreY5/KkzyJBcmUo2ccD/g4troa0EX/bsssVYa+yEKDQzjrhlkoPhx SC/X7AxzlFoHFRLLtkpePs/UNNNS/pVyuZ7pGfhJpob4p/ktpAsmIPF7aSmvjn5ODguz ADXcrXu3QshpJKi9I+iT7uN8FE5Leqc/U/yiOBXIbSN/tDtYXfoFDTvD6t6PLRaYD5dh Fl/nGFg4Nkrf8zBR+RSsfBRXirSdc0jejuHKwAWd7DiWMtsYNgilvGQ9rZXeYJZBiDxq gCEA== 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=bOs3ifWdbKpIRIyX45zWGjXKwz5aeH8C0dGiyT0WkL8=; fh=ChXOctppJn0KECDRINafwUY5xHRufGHaa0Ju9pddrcQ=; b=r4dmsSAqhbFZnFIuj2vdMpFGRoX/3LnX2qd77WASYyvwZoY/SWC0zLL80zpYZ+CqAe wIXfd3mIAh+IBaJiodQnYGmu/zxeykrXLluArX7Q2+WhaWtH3i28TgNqyFmmIHXVNsyR vuW3hKTXXOvkTx7DohPJW1AUgmfFr9L6gcHiq3E0xvKGMF8mu46wEq4MSRvz0alaUCei jOtHBjBkK61U29zl98oQHZtFsxhAXXEKj2+6M00yQ9MGAtcP79IbBuyBB29s9YxqgNGl kRfMdcPToZSzIoF+En2Zy9rQia2RXPfV5BGgrC4aJ5wcVw9Baz6De3JQag+oHIT7zxSL Oz3g== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=YUhTYMwL; 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"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id pj23-20020a05620a1d9700b00775c0be5457si10028315qkn.553.2023.11.16.02.49.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Nov 2023 02:49:42 -0800 (PST) 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=@intel.com header.s=Intel header.b=YUhTYMwL; 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"; 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 CE7433858421 for ; Thu, 16 Nov 2023 10:49:41 +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 [192.55.52.93]) by sourceware.org (Postfix) with ESMTPS id A799B3858D35 for ; Thu, 16 Nov 2023 10:49:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A799B3858D35 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 A799B3858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=192.55.52.93 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700131756; cv=none; b=nkWmdwTT9X2Hg6RLJHZe6NodxUVdkOfxxyfFjR9XxyVKxFi3/iXDuPblbttGsBvGQMLReBTR6bDu3tWmoEIkqcJALfNVEU0oYNEcg6fMWK8mLeBSAEw0asETlKWNnHriofA4zu6NCV71hEQNmj9IyHUr9t65zgOg6FUqTWNSvlk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700131756; c=relaxed/simple; bh=GkdzR4U8sRjOjog9ErskdiNYyWN7PSO+5Wg/f93USvg=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=ZyBJzHmeFRZFdi80IURl8etIjESnw1KD+scmhETB2ruvoJM9MLOwAUrWn/oN00+uGc792tlY5VKPWbxsp8cZP5KXKmGWs5TasCPU4TTcB8aQ82PM1UL6giFsso3T0DcsT60HSKjxlNINcZqzC+6If2xaHgFZX7IWbCX7G4Y8Jes= 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=1700131754; x=1731667754; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=GkdzR4U8sRjOjog9ErskdiNYyWN7PSO+5Wg/f93USvg=; b=YUhTYMwLtn54MMm65SJc3mODzCQq0T1ajRTaJuBZasZ1gRSVWHU2mf04 XNTXYyeaD4rTnRbYOD0Jp5Q4KQLD8e27Jvz+oqTG1+jlMjur8PaPScDeA 5yze/NF+pfLGuzfm2OueHugrs7rDgqF9RzB3s+RsKOqQuHk/HJoYzPRvb vVQX1zXTuyIZvBC5hFXSQKF3oHZN7xapIpIpquDOhHpE+8+aAtK8FjPz/ eMhn+Sxi+Aj0O/sDgqAcKkq8iqeuml5TJ+bCdZ3uFIeCtjZWu4m7j5le3 kk38a7IGSalBm+7E07HFUfMYDW/dJ8rlc1UUu5k6ZFbkENKW6pwb+tgFJ g==; X-IronPort-AV: E=McAfee;i="6600,9927,10895"; a="388230550" X-IronPort-AV: E=Sophos;i="6.03,308,1694761200"; d="scan'208";a="388230550" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Nov 2023 02:49:13 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.03,308,1694761200"; d="scan'208";a="13518876" Received: from shvmail03.sh.intel.com ([10.239.245.20]) by fmviesa001.fm.intel.com with ESMTP; 16 Nov 2023 02:49:12 -0800 Received: from shliclel4217.sh.intel.com (shliclel4217.sh.intel.com [10.239.240.127]) by shvmail03.sh.intel.com (Postfix) with ESMTP id 83C0010056BB; Thu, 16 Nov 2023 18:49:10 +0800 (CST) From: liuhongt To: gcc-patches@gcc.gnu.org Cc: crazylht@gmail.com, hjl.tools@gmail.com Subject: [V2 PATCH] Simplify vector ((VCE (a cmp b ? -1 : 0)) < 0) ? c : d to just (VCE ((a cmp b) ? (VCE c) : (VCE d))). Date: Thu, 16 Nov 2023 18:47:10 +0800 Message-Id: <20231116104710.1279964-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=-12.1 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: 1782717383160927850 X-GMAIL-MSGID: 1782717383160927850 Update in V2: 1) Add some comments before the pattern. 2) Remove ? from view_convert. Bootstrapped and regtested on x86_64-pc-linux-gnu{-m32,}. Ok for trunk? 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 | 22 +++++++++ .../gcc.target/i386/avx512vl-blendv-3.c | 6 +++ gcc/testsuite/gcc.target/i386/blendv-3.c | 46 +++++++++++++++++++ 3 files changed, 74 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..2a69622a300 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5170,6 +5170,28 @@ 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))) +/* ((VCE (a cmp b ? -1 : 0)) < 0) ? c : d is just + (VCE ((a cmp b) ? (VCE c) : (VCE d))) when TYPE_PRECISION of the + component type of the outer vec_cond is greater equal the inner one. */ +(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); +}