From patchwork Thu Jul 25 13:33:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Dapp X-Patchwork-Id: 1964772 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=Ym1sPKYJ; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WVBgN5xJWz1yXx for ; Thu, 25 Jul 2024 23:34:04 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 03763385841D for ; Thu, 25 Jul 2024 13:34:03 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by sourceware.org (Postfix) with ESMTPS id 104D73858D35 for ; Thu, 25 Jul 2024 13:33:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 104D73858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 104D73858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::630 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721914419; cv=none; b=MA+rucTamCl9mvLNz+e6OZBSYUD4pUiFHAYWMpAFPjtyHqC4ddHU4bucsbFRAYEVepnziShH/ZIZAczSo1qPCYCBdkhuI2JQ1e47/MesKVFbenZm4gqB+iNbab15+G/0yDBXE8Frrl5E7TSMIhrX8Ta8CIrnuAsXT0WL0AoSwfE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721914419; c=relaxed/simple; bh=ISvMdn1xQfwW51+fiiwtWEKeD42lykHVAmzhQzdY2/4=; h=DKIM-Signature:Mime-Version:Date:Message-Id:Subject:To:From; b=DExzHD1YDCOs6gpv/JDbY0gAJMFaWBA4APGFuJriH4Wbw86oCm5UC5hgqoLaoAt/ZjYvhKg0Xu1SeFMpGFo8h05ck1JXfksbZ5tmtrwAdTB1vzCiHm2VUwnaeR1umyLJDo+XjFT+eUvnoauROrir30NNvC/q6Uw4y4u0nKoEtnM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-a7ab76558a9so99544466b.1 for ; Thu, 25 Jul 2024 06:33:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721914416; x=1722519216; darn=gcc.gnu.org; h=from:to:cc:subject:message-id:date:content-transfer-encoding :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=EYAfpyw53ul8Dpi3bUde0caXV/L5+KrvC9VVHAiNKgo=; b=Ym1sPKYJRF+3rFCDfEdb3HQI0emwWJMaq1gjkTD+FbbRxmgrHvBI15fGZDT8PYWETA P3omtRxlWfROxSi6USPxZBxNJ1U8IZ169nAQwfDtuH3WJS3F0awmz/Q43rEVZs0aI9vb ZainGpwFRfKFotylv7zHs3mEmc9TA7ajqqA9SGlJNqgU7qPLbqhVjYYGJzFqA/BdNyiS 0sr9f7y7fpc99n2PeFaQn2S3lXFiWbrPxGiusoAKk5OtDh85YegrNmbCsoDT7UBLpq7k 6SDz5BL2Njk3+5mWLsx5A3xRzQM6IlU/WYxh7UvfO6hzPRm+NkG4POyVfXb8gXQ3RUte qD5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721914416; x=1722519216; h=from:to:cc:subject:message-id:date:content-transfer-encoding :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=EYAfpyw53ul8Dpi3bUde0caXV/L5+KrvC9VVHAiNKgo=; b=qy7FpxY0q25Do2GIVah5dUBARx1QK5i5Siq2J7bK+pXg5Z9wPtq2JNQCwYL7s0yagZ Fw98WSPqFBNKwKyEpQGt8s96EcA6km1fPxzatxPmoFQCCODn/mjLOw16TnUd+4Gal8Zh yPd0MoUDs3umrk1t+xnw+WCY+6NRFD8sO0DjEcDc5Ml4etAleonMwneq3gOGWozQarqg IGUiFaY7OCdha1202aDiG+DrTfJzG2ew3qJfaTbwX+R0VlFMlOgeIPg8LIfWtd8pcw8p 7XL2PNmMRFApOEXy69fqKkKA/2ks0O6RB2oh6hpLWeBe/cQv3eacgCnNz4O53GITiQyM FZpA== X-Gm-Message-State: AOJu0YyOU5YS3rXCFW2BX2U7iwaMf6zA97anU2c52HFOX3JKfTnOTVb7 JqzFOrwhJP940zpJiochpvMLYmCWhgjvhJLzkC3sUiP/XppnKouC6Vsutw== X-Google-Smtp-Source: AGHT+IEQ/1VGxCVKVyQQX1Erj0pJchrdMKWKddP+VY8zmzvye6vpp2aco9sl2pujlwYHvKVppUcwGg== X-Received: by 2002:a17:907:7249:b0:a77:c693:9ddf with SMTP id a640c23a62f3a-a7ab31d333cmr543586066b.34.1721914415362; Thu, 25 Jul 2024 06:33:35 -0700 (PDT) Received: from localhost (ip-149-172-150-237.um42.pools.vodafone-ip.de. [149.172.150.237]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a7acad9079bsm73756966b.166.2024.07.25.06.33.34 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 25 Jul 2024 06:33:34 -0700 (PDT) Mime-Version: 1.0 Date: Thu, 25 Jul 2024 15:33:34 +0200 Message-Id: Subject: [PATCH] fold: Allow SSA names in inverse_conditions_p and fold VCOND_MASK. Cc: To: "gcc-patches" From: "Robin Dapp" X-Spam-Status: No, score=-9.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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~incoming=patchwork.ozlabs.org@gcc.gnu.org Hi, In preparation for the maskload else operand I split off this patch. The patch looks through SSA names for the conditions passed to inverse_conditions_p which helps match.pd recognize more redundant vec_cond expressions. It also adds VCOND_MASK to the respective iterators in match.pd. Is this acceptable without a separate test? There will of course be several hits once we emit VEC_COND_EXPRs after masked loads. The initial version of the patch looked "through" each condition individually. That caused the following problem on p10 during phiopt: foo = blah <= 0 cond2: foo ? c : x cond1: blah > 0 ? b : cond1 -> (match.pd:6205) res = blah > 0 ? b : c which is invalid gimple (as blah > 0 is directly used and not put in a variable). Therefore, for now, I restricted the SSA_NAME check to both conditions simultaneously so we don't run into this situation. There must be a better way, though? Bootstrapped and regtested on x86, aarch64 and power10. Regtested on armv8.8-a+sve using qemu as well as riscv64. Regards Robin gcc/ChangeLog: * fold-const.cc (inverse_conditions_p): Look through SSA names. * match.pd: Add VCOND_MASK to "cond" iterators. --- gcc/fold-const.cc | 22 ++++++++++++++++++++++ gcc/match.pd | 28 +++++++++++++++------------- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc index 83c32dd10d4..1fc5d97dccc 100644 --- a/gcc/fold-const.cc +++ b/gcc/fold-const.cc @@ -86,6 +86,7 @@ along with GCC; see the file COPYING3. If not see #include "vec-perm-indices.h" #include "asan.h" #include "gimple-range.h" +#include "cfgexpand.h" /* Nonzero if we are folding constants inside an initializer or a C++ manifestly-constant-evaluated context; zero otherwise. @@ -3010,6 +3011,27 @@ compcode_to_comparison (enum comparison_code code) bool inverse_conditions_p (const_tree cond1, const_tree cond2) { + /* If both conditions are SSA names, look through them. + Right now callees in match use one of the conditions directly and + we might end up having one in a COND_EXPR like + res = a > b ? c : d + instead of + cnd = a > b + res = cnd ? c : d. + + Therefore always consider both conditions simultaneously. */ + if (TREE_CODE (cond1) == SSA_NAME + && TREE_CODE (cond2) == SSA_NAME) + { + gimple *gcond1 = SSA_NAME_DEF_STMT (cond1); + if (is_gimple_assign (gcond1)) + cond1 = gimple_assign_rhs_to_tree (gcond1); + + gimple *gcond2 = SSA_NAME_DEF_STMT (cond2); + if (is_gimple_assign (gcond2)) + cond2 = gimple_assign_rhs_to_tree (gcond2); + } + return (COMPARISON_CLASS_P (cond1) && COMPARISON_CLASS_P (cond2) && (invert_tree_comparison diff --git a/gcc/match.pd b/gcc/match.pd index cf359b0ec0f..f244e6deff5 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5601,7 +5601,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* (a ? x : y) == (b ? x : y) --> (a^b) ? FALSE : TRUE */ /* (a ? x : y) != (b ? y : x) --> (a^b) ? FALSE : TRUE */ /* (a ? x : y) == (b ? y : x) --> (a^b) ? TRUE : FALSE */ -(for cnd (cond vec_cond) +(for cnd (cond vec_cond IFN_VCOND_MASK) (for eqne (eq ne) (simplify (eqne:c (cnd @0 @1 @2) (cnd @3 @1 @2)) @@ -5614,14 +5614,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask types are compatible. */ -(simplify - (vec_cond @0 VECTOR_CST@1 VECTOR_CST@2) - (if (VECTOR_BOOLEAN_TYPE_P (type) - && types_match (type, TREE_TYPE (@0))) - (if (integer_zerop (@1) && integer_all_onesp (@2)) - (bit_not @0) - (if (integer_all_onesp (@1) && integer_zerop (@2)) - @0)))) +(for cnd (vec_cond IFN_VCOND_MASK) + (simplify + (cnd @0 VECTOR_CST@1 VECTOR_CST@2) + (if (VECTOR_BOOLEAN_TYPE_P (type) + && types_match (type, TREE_TYPE (@0))) + (if (integer_zerop (@1) && integer_all_onesp (@2)) + (bit_not @0) + (if (integer_all_onesp (@1) && integer_zerop (@2)) + @0))))) /* A few simplifications of "a ? CST1 : CST2". */ /* NOTE: Only do this on gimple as the if-chain-to-switch @@ -6049,7 +6050,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) { build_int_cst (integer_type_node, prec - 1);})))))) #endif -(for cnd (cond vec_cond) +(for cnd (cond vec_cond IFN_VCOND_MASK) /* (a != b) ? (a - b) : 0 -> (a - b) */ (simplify (cnd (ne:c @0 @1) (minus@2 @0 @1) integer_zerop) @@ -6185,7 +6186,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (TYPE_UNSIGNED (type)) (cond (ge @0 @1) (negate @0) @2))) -(for cnd (cond vec_cond) +(for cnd (cond vec_cond IFN_VCOND_MASK) /* A ? B : (A ? X : C) -> A ? B : C. */ (simplify (cnd @0 (cnd @0 @1 @2) @3) @@ -6210,8 +6211,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* A ? B : B -> B. */ (simplify (cnd @0 @1 @1) - @1) + @1)) +(for cnd (cond vec_cond) /* !A ? B : C -> A ? C : B. */ (simplify (cnd (logical_inverted_value truth_valued_p@0) @1 @2) @@ -6232,7 +6234,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) Note that all these transformations are correct if A is NaN, since the two alternatives (A and -A) are also NaNs. */ -(for cnd (cond vec_cond) +(for cnd (cond vec_cond IFN_VCOND_MASK) /* A == 0 ? A : -A same as -A */ (for cmp (eq uneq) (simplify