From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id B52F33858431 for ; Fri, 16 Jun 2023 08:25:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B52F33858431 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 993871F45A for ; Fri, 16 Jun 2023 08:25:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1686903958; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=TDho8gC2TLNmKAQMj1c+wMjZaYT6kSWWCSzMgrw2iWY=; b=iwcEelsXkhowrBRkdHLNixQeEV5wKYCbjSpmjha3W+Tj85/UiXdayOQsxnlMdFZgrwUPOt dqXKW5jMAtWiDLgoZ6iS/mImIhU5zZiqCyrOHWi51jng8b7NFeRCWcWy9tSzDkwp+AdwIN m/0QxArxYMCtIe7sPMIfFqkK2cZWrRk= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1686903958; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=TDho8gC2TLNmKAQMj1c+wMjZaYT6kSWWCSzMgrw2iWY=; b=m91BtJ3E7SmxfQxlMHvJLJRshX7qPxIHG75W95HV4ij3/hnuq4yaJWm1JpKv3dGzEY4L24 j5fUETQlOupxKADw== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 855921330B for ; Fri, 16 Jun 2023 08:25:58 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id UaJJH5YcjGQxLQAAMHmgww (envelope-from ) for ; Fri, 16 Jun 2023 08:25:58 +0000 Date: Fri, 16 Jun 2023 10:25:58 +0200 (CEST) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/110269 - restore missed condition folding MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Message-Id: <20230616082558.855921330B@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,SPF_HELO_NONE,SPF_PASS,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 List-Id: The following makes sure we optimize x != 0 using range info via tree_expr_nonzero_p via match.pd. Bootstrapped and tested on x86_64-unknown-linux-gnu. This causes FAIL: gcc.dg/tree-ssa/pr103257-1.c scan-tree-dump-times optimized "link_error" 0 because we now improve early folding the following way - if (a ((unsigned int) ((0, 1) && b != 0) > b, (int) (short int) c) != 0) + if (a ((unsigned int) (b != 0) > b, (int) (short int) c) != 0) and that causes some initial CFG turned into straight-line code which we not handle as expected. I'm going to file a bug for this but go ahead pushing this patch. Richard. PR tree-optimization/110269 * fold-const.cc (fold_binary_loc): Merge x != 0 folding with tree_expr_nonzero_p ... * match.pd (cmp (convert? addr@0) integer_zerop): With this pattern. * gcc.dg/tree-ssa/pr110269.c: New testcase. --- gcc/fold-const.cc | 7 ----- gcc/match.pd | 4 +-- gcc/testsuite/gcc.dg/tree-ssa/pr110269.c | 34 ++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110269.c diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc index 34c5f192a1d..7589498dbd0 100644 --- a/gcc/fold-const.cc +++ b/gcc/fold-const.cc @@ -12298,13 +12298,6 @@ fold_binary_loc (location_t loc, enum tree_code code, tree type, tem, build_int_cst (TREE_TYPE (tem), 0)); } - if (integer_zerop (arg1) - && tree_expr_nonzero_p (arg0)) - { - tree res = constant_boolean_node (code==NE_EXPR, type); - return omit_one_operand_loc (loc, type, res, arg0); - } - if (TREE_CODE (arg0) == BIT_XOR_EXPR && TREE_CODE (arg1) == BIT_XOR_EXPR) { diff --git a/gcc/match.pd b/gcc/match.pd index 0fcf91f0eeb..264f9cb8a40 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -6120,8 +6120,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (cmp @0 (bit_xor @1 (convert @2))))) (simplify - (cmp (convert? addr@0) integer_zerop) - (if (tree_single_nonzero_warnv_p (@0, NULL)) + (cmp (nop_convert? @0) integer_zerop) + (if (tree_expr_nonzero_p (@0)) { constant_boolean_node (cmp == NE_EXPR, type); })) /* (X & C) op (Y & C) into (X ^ Y) & C op 0. */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110269.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110269.c new file mode 100644 index 00000000000..c68a6f91604 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110269.c @@ -0,0 +1,34 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-ccp2 -fdump-tree-optimized" } */ + +void foo(void); +static int a = 1, c; +static int *b = &a; +static int **d = &b; +static int ***e = &d; +void __assert_fail() __attribute__((__noreturn__)); +static int f() { + if (a) return a; + for (; c;) *e = 0; + if (b) __assert_fail(); + return 6; +} +int main() { + if (f()) { + *d = 0; + if (b == 0) + ; + else { + __builtin_unreachable(); + __assert_fail(); + } + } + if (b == 0) + ; + else + foo(); + ; +} + +/* { dg-final { scan-tree-dump-times "Folding predicate" 2 "ccp2" } } */ +/* { dg-final { scan-tree-dump-not "foo" "optimized" } } */ -- 2.35.3