From patchwork Thu Nov 30 12:30:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 81026 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2AD2C3857C44 for ; Thu, 30 Nov 2023 12:33:58 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by sourceware.org (Postfix) with ESMTPS id D3ADA3857C42 for ; Thu, 30 Nov 2023 12:33:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D3ADA3857C42 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D3ADA3857C42 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701347623; cv=none; b=JEIldXZoJzU0QtE9A1LzYccQXML9M0iERPsKcxoFVVu9ZZaCGx3HkfjX+E2Mg+y+HK9R4RDMqutNTw+96FDZ3ibrJimJygVOSG+3s1+udf0hm/fAtICJiygRsfcdAmC5Wt6Q5624OenScO2EWaOe18sHUkvMzywU0bAOn1bjk98= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701347623; c=relaxed/simple; bh=siBH0cnlnmS7a7B4jkLQ0AqkVo9v+BUdxVW4NOjX3c4=; h=DKIM-Signature:DKIM-Signature:Date:From:To:Subject:MIME-Version; b=a01NnH1fLMwV3gwpB/OZlDPnh0sywoUKcu1WsdnOeVLLbRUq3PZ94GSAQPMI+IKlH1kfaWUCyTwH6U2lM2eZ9NVgMwYUsAGQwfillarhXSMa4DeBIT9qYteiK8bVbV2NUeeGWoBN35A+Mo5Qg/STwgylFycqLN9vujOzPdRgEks= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from [10.168.4.150] (unknown [10.168.4.150]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 8930D21AE6 for ; Thu, 30 Nov 2023 12:33:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1701347619; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=utyMhCUeCr1dyaOtShUSknz0Ro226aqFtMF1+wfC6UI=; b=IbWmyfcGV6/csq1NzHuAZ1j9Qr4twVrEUiZd0pdbeppAr21w9l1BsPJuM+B17QWKj9clHd wi3Jb+BTkFjRs7jQ99c2SgAXva1D+dNHQVuEmxy3mj37ICTKJ8hD3NkSIEz7XFgFSskXzw 67SqYg6As9A4xBe6QXMJ/zs7za35gUI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1701347619; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=utyMhCUeCr1dyaOtShUSknz0Ro226aqFtMF1+wfC6UI=; b=bE+nJ+/SLgXz0u9hme7Gea9by+9pzB+/3EDoRmxJtO5kWl2K8rBXo43PRd3n/QStFhO+3+ 4Qm0P6RQgRAmjjAg== Date: Thu, 30 Nov 2023 13:30:00 +0100 (CET) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-optimization/112766 - improve pruning of uninit diagnostics MIME-Version: 1.0 Authentication-Results: smtp-out1.suse.de; none X-Spam-Score: 2.90 X-Spamd-Result: default: False [2.90 / 50.00]; ARC_NA(0.00)[]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; MISSING_MID(2.50)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_SPAM_LONG(3.50)[1.000]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_COUNT_ZERO(0.00)[0]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; BAYES_HAM(-3.00)[100.00%] X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, MISSING_MID, 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 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+patchwork=sourceware.org@gcc.gnu.org Message-Id: <20231130123358.2AD2C3857C44@sourceware.org> Uninit diagnostics has code to prune based on incoming PHI args that prove the uninit code is never executed. But that only looks at the first found flag candidate while in the PRs case only the second candidate would be the one to prune on. The following patch makes us consider all of the flag candidates which is cycles well spent IMHO. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR tree-optimization/112766 * gimple-predicate-analysis.cc (find_var_cmp_const): Support continuing the iteration and report every candidate. (uninit_analysis::overlap): Iterate over all flag var candidates. * g++.dg/torture/uninit-pr112766.C: New testcase. --- gcc/gimple-predicate-analysis.cc | 78 +++++++------------ .../g++.dg/torture/uninit-pr112766.C | 17 ++++ 2 files changed, 47 insertions(+), 48 deletions(-) create mode 100644 gcc/testsuite/g++.dg/torture/uninit-pr112766.C diff --git a/gcc/gimple-predicate-analysis.cc b/gcc/gimple-predicate-analysis.cc index ad2c35524ce..5e231a6a469 100644 --- a/gcc/gimple-predicate-analysis.cc +++ b/gcc/gimple-predicate-analysis.cc @@ -244,21 +244,18 @@ find_matching_predicate_in_rest_chains (const pred_info &pred, of that's the form "FLAG_VAR CMP FLAG_VAR" with value range info. PHI is the phi node whose incoming (interesting) paths need to be examined. On success, return the comparison code, set defintion - gimple of FLAG_DEF and BOUNDARY_CST. Otherwise return ERROR_MARK. */ + gimple of FLAG_DEF and BOUNDARY_CST. Otherwise return ERROR_MARK. + I is the running iterator so the function can be called repeatedly + to gather all candidates. */ static tree_code find_var_cmp_const (pred_chain_union preds, gphi *phi, gimple **flag_def, - tree *boundary_cst) + tree *boundary_cst, unsigned &i) { - tree_code vrinfo_code = ERROR_MARK; - gimple *vrinfo_def = NULL; - tree vrinfo_cst = NULL; - gcc_assert (preds.length () > 0); pred_chain chain = preds[0]; - for (unsigned i = 0; i < chain.length (); i++) + for (; i < chain.length (); i++) { - bool use_vrinfo_p = false; const pred_info &pred = chain[i]; tree cond_lhs = pred.pred_lhs; tree cond_rhs = pred.pred_rhs; @@ -282,8 +279,7 @@ find_var_cmp_const (pred_chain_union preds, gphi *phi, gimple **flag_def, } /* Check if we can take advantage of FLAG_VAR COMP FLAG_VAR predicate with value range info. Note only first of such case is handled. */ - else if (vrinfo_code == ERROR_MARK - && TREE_CODE (cond_lhs) == SSA_NAME + else if (TREE_CODE (cond_lhs) == SSA_NAME && TREE_CODE (cond_rhs) == SSA_NAME) { gimple* lhs_def = SSA_NAME_DEF_STMT (cond_lhs); @@ -331,8 +327,6 @@ find_var_cmp_const (pred_chain_union preds, gphi *phi, gimple **flag_def, cond_rhs = wide_int_to_tree (type, min); else continue; - - use_vrinfo_p = true; } else continue; @@ -345,27 +339,13 @@ find_var_cmp_const (pred_chain_union preds, gphi *phi, gimple **flag_def, || !find_matching_predicate_in_rest_chains (pred, preds)) continue; - /* Return if any "flag_var comp const" predicate is found. */ - if (!use_vrinfo_p) - { - *boundary_cst = cond_rhs; - return code; - } - /* Record if any "flag_var comp flag_var[vinfo]" predicate is found. */ - else if (vrinfo_code == ERROR_MARK) - { - vrinfo_code = code; - vrinfo_def = *flag_def; - vrinfo_cst = cond_rhs; - } - } - /* Return the "flag_var cmp flag_var[vinfo]" predicate we found. */ - if (vrinfo_code != ERROR_MARK) - { - *flag_def = vrinfo_def; - *boundary_cst = vrinfo_cst; + /* Return predicate found. */ + *boundary_cst = cond_rhs; + ++i; + return code; } - return vrinfo_code; + + return ERROR_MARK; } /* Return true if all interesting opnds are pruned, false otherwise. @@ -641,27 +621,29 @@ uninit_analysis::overlap (gphi *phi, unsigned opnds, hash_set *visited, { gimple *flag_def = NULL; tree boundary_cst = NULL_TREE; - bitmap visited_flag_phis = NULL; /* Find within the common prefix of multiple predicate chains a predicate that is a comparison of a flag variable against a constant. */ - tree_code cmp_code = find_var_cmp_const (use_preds.chain (), phi, &flag_def, - &boundary_cst); - if (cmp_code == ERROR_MARK) - return true; - - /* Now check all the uninit incoming edges have a constant flag - value that is in conflict with the use guard/predicate. */ - gphi *phi_def = as_a (flag_def); - bool all_pruned = prune_phi_opnds (phi, opnds, phi_def, boundary_cst, - cmp_code, visited, - &visited_flag_phis); - - if (visited_flag_phis) - BITMAP_FREE (visited_flag_phis); + unsigned i = 0; + tree_code cmp_code; + while ((cmp_code = find_var_cmp_const (use_preds.chain (), phi, &flag_def, + &boundary_cst, i)) != ERROR_MARK) + { + /* Now check all the uninit incoming edges have a constant flag + value that is in conflict with the use guard/predicate. */ + bitmap visited_flag_phis = NULL; + gphi *phi_def = as_a (flag_def); + bool all_pruned = prune_phi_opnds (phi, opnds, phi_def, boundary_cst, + cmp_code, visited, + &visited_flag_phis); + if (visited_flag_phis) + BITMAP_FREE (visited_flag_phis); + if (all_pruned) + return false; + } - return !all_pruned; + return true; } /* Return true if two predicates PRED1 and X2 are equivalent. Assume diff --git a/gcc/testsuite/g++.dg/torture/uninit-pr112766.C b/gcc/testsuite/g++.dg/torture/uninit-pr112766.C new file mode 100644 index 00000000000..028056e92d9 --- /dev/null +++ b/gcc/testsuite/g++.dg/torture/uninit-pr112766.C @@ -0,0 +1,17 @@ +// { dg-do compile } +// { dg-additional-options "-Wuninitialized" } + +void* operator new[](__SIZE_TYPE__, void* __p) ; + +class Result +{ +public: + Result(); + ~Result(); +}; + +void *foo(long nElements, void *p) +{ + return p ? new((int*)p) Result[nElements] : new Result[nElements]; // { dg-bogus "uninitialized" } +} +