From patchwork Wed Nov 30 11:00:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 1710436 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=wHNeVvKF; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4NMbpv5908z23ng for ; Wed, 30 Nov 2022 22:00:54 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2C3E1385B180 for ; Wed, 30 Nov 2022 11:00:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2C3E1385B180 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1669806051; bh=H4MAJ1PadN9F3BD/pfE6Fn7FCYbYURVmfMQnFXk9xx0=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=wHNeVvKFsDw3vnEBU5UyVDhqLmSDwG30pk/iEQ6ddzrJc1np3xA/6tbbpjscl4AXn PNOmXFZxVWSP9svfpah6QwSxB9gERS/6Kk0aL5uVaC+cgHgd8TPjUlVtzdORsOQSc3 LjioIY75C2gNMlsBFH4j8rWRnqelkqLh32vsTM9k= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id B54C03858D37 for ; Wed, 30 Nov 2022 11:00:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B54C03858D37 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 B23011FD70 for ; Wed, 30 Nov 2022 11:00:30 +0000 (UTC) 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 9E7981331F for ; Wed, 30 Nov 2022 11:00:30 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id xzRuJc43h2MZEQAAMHmgww (envelope-from ) for ; Wed, 30 Nov 2022 11:00:30 +0000 Date: Wed, 30 Nov 2022 12:00:30 +0100 (CET) To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/107919 - uninit diagnostic predicate simplification MIME-Version: 1.0 Message-Id: <20221130110030.9E7981331F@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.8 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 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: Richard Biener via Gcc-patches From: Richard Biener Reply-To: Richard Biener Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" We fail to simplify ((_145 != 0B) AND (_531 == 2) AND (_109 == 0)) OR ((NOT (_145 != 0B)) AND (_531 == 2) AND (_109 == 0)) OR ((NOT (_531 == 2)) AND (_109 == 0)) because the existing simplification of !A && B || A && B is implemented too simplistic. The following re-implements that which fixes the bogus uninit diagnostic when using -O1 but not yet at -O2. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/107919 * gimple-predicate-analysis.cc (predicate::simplify_2): Handle predicates of arbitrary length. * g++.dg/warn/Wuninitialized-pr107919-1.C: New testcase. --- gcc/gimple-predicate-analysis.cc | 71 ++++++++----------- .../g++.dg/warn/Wuninitialized-pr107919-1.C | 15 ++++ 2 files changed, 43 insertions(+), 43 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wuninitialized-pr107919-1.C diff --git a/gcc/gimple-predicate-analysis.cc b/gcc/gimple-predicate-analysis.cc index 5013a4447d6..23be4b69bab 100644 --- a/gcc/gimple-predicate-analysis.cc +++ b/gcc/gimple-predicate-analysis.cc @@ -1257,64 +1257,49 @@ predicate::simplify_2 () /* (X AND Y) OR (!X AND Y) is equivalent to Y. (X AND Y) OR (X AND !Y) is equivalent to X. */ - unsigned n = m_preds.length (); - for (unsigned i = 0; i < n; i++) + for (unsigned i = 0; i < m_preds.length (); i++) { pred_chain &a_chain = m_preds[i]; - if (a_chain.length () != 2) - continue; - - /* Create copies since the chain may be released below before - the copy is added to the other chain. */ - const pred_info x = a_chain[0]; - const pred_info y = a_chain[1]; - for (unsigned j = 0; j < n; j++) + for (unsigned j = i + 1; j < m_preds.length (); j++) { - if (j == i) - continue; - pred_chain &b_chain = m_preds[j]; - if (b_chain.length () != 2) + if (b_chain.length () != a_chain.length ()) continue; - const pred_info &x2 = b_chain[0]; - const pred_info &y2 = b_chain[1]; - - if (pred_equal_p (x, x2) && pred_neg_p (y, y2)) + unsigned neg_idx = -1U; + for (unsigned k = 0; k < a_chain.length (); ++k) { - /* Kill a_chain. */ - b_chain.release (); - a_chain.release (); - b_chain.safe_push (x); - simplified = true; - break; + if (pred_equal_p (a_chain[k], b_chain[k])) + continue; + if (neg_idx != -1U) + { + neg_idx = -1U; + break; + } + if (pred_neg_p (a_chain[k], b_chain[k])) + neg_idx = k; + else + break; } - if (pred_neg_p (x, x2) && pred_equal_p (y, y2)) + /* If we found equal chains with one negated predicate + simplify. */ + if (neg_idx != -1U) { - /* Kill a_chain. */ - a_chain.release (); - b_chain.release (); - b_chain.safe_push (y); + a_chain.ordered_remove (neg_idx); + m_preds.ordered_remove (j); simplified = true; + if (a_chain.is_empty ()) + { + /* A && !A simplifies to true, wipe the whole predicate. */ + for (unsigned k = 0; k < m_preds.length (); ++k) + m_preds[k].release (); + m_preds.truncate (0); + } break; } } } - /* Now clean up the chain. */ - if (simplified) - { - pred_chain_union s_preds = vNULL; - for (unsigned i = 0; i < n; i++) - { - if (m_preds[i].is_empty ()) - continue; - s_preds.safe_push (m_preds[i]); - } - m_preds.release (); - m_preds = s_preds; - s_preds = vNULL; - } return simplified; } diff --git a/gcc/testsuite/g++.dg/warn/Wuninitialized-pr107919-1.C b/gcc/testsuite/g++.dg/warn/Wuninitialized-pr107919-1.C new file mode 100644 index 00000000000..dd631dc8bfe --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wuninitialized-pr107919-1.C @@ -0,0 +1,15 @@ +// { dg-do compile } +// { dg-require-effective-target c++17 } +// { dg-options "-O -Wuninitialized" } + +#include +#include + +using Event = std::variant>>, int, char>; + +void do_something(void* storage) +{ + Event event {}; + auto& swappedValue = *reinterpret_cast(storage); + std::swap(event, swappedValue); +}