From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 17FFE385841D; Thu, 30 Mar 2023 13:03:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 17FFE385841D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1680181434; bh=GpOFap+RMvMdN8Nj2itjNngTZKsErk4SDLaeA8cn4XI=; h=From:To:Subject:Date:In-Reply-To:References:From; b=CLEpYmz53ooScBPTuAHK3H23HZJTdl+qlMIwS6y02pUhGtBANupB2gygoJLH5WIeB 71F4TE2l4TO/yRLFhaNiXZNzu6weMsCFYdLjSVZfUZrDdRoOG5qx+DivV4aRt7UrTy wuoNgN09T1kPCxP5NaJvH6SfmxKKvBsoG74SmwIY= From: "jakub at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/91645] Missed optimization with sqrt(x*x) Date: Thu, 30 Mar 2023 13:03:52 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 9.2.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: jakub at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D91645 --- Comment #12 from Jakub Jelinek --- WIP which still doesn't work: --- gcc/value-query.cc.jj 2023-03-23 15:25:47.069740988 +0100 +++ gcc/value-query.cc 2023-03-30 14:56:58.809298424 +0200 @@ -230,9 +230,11 @@ range_query::get_tree_range (vrange &r, default: break; } - if (BINARY_CLASS_P (expr)) + if (BINARY_CLASS_P (expr) || COMPARISON_CLASS_P (expr)) { - range_op_handler op (TREE_CODE (expr), type); + range_op_handler op (TREE_CODE (expr), + BINARY_CLASS_P (expr) ? type + : TREE_TYPE (TREE_OPERAND (expr, 0))); if (op) { Value_Range r0 (TREE_TYPE (TREE_OPERAND (expr, 0))); --- gcc/tree-call-cdce.cc.jj 2023-01-02 09:32:45.940944935 +0100 +++ gcc/tree-call-cdce.cc 2023-03-30 14:54:25.248544702 +0200 @@ -36,6 +36,7 @@ along with GCC; see the file COPYING3. #include "builtins.h" #include "internal-fn.h" #include "tree-dfa.h" +#include "gimple-range.h" /* This pass serves two closely-related purposes: @@ -425,12 +426,9 @@ comparison_code_if_no_nans (tree_code co null tree. */ static void -gen_one_condition (tree arg, int lbub, - enum tree_code tcode, - const char *temp_name1, - const char *temp_name2, - vec conds, - unsigned *nconds) +gen_one_condition (tree arg, int lbub, enum tree_code tcode, + const char *temp_name1, const char *temp_name2, + vec conds, unsigned *nconds, gimple *stmt) { if (!HONOR_NANS (arg)) tcode =3D comparison_code_if_no_nans (tcode); @@ -451,10 +449,24 @@ gen_one_condition (tree arg, int lbub, gimple_assign_set_lhs (stmt1, tempn); tempc =3D create_tmp_var (boolean_type_node, temp_name2); - stmt2 =3D gimple_build_assign (tempc, - fold_build2 (tcode, - boolean_type_node, - tempn, lbub_real_cst)); + tree tcond =3D build2 (tcode, boolean_type_node, arg, lbub_real_cst); + int_range_max r; + range_query *q =3D get_range_query (cfun); + if (q =3D=3D get_global_range_query ()) + q =3D enable_ranger (cfun); + /* Ask the ranger whether it knows the condition will be always false or + always true. */ + if (!q->range_of_expr (r, tcond, stmt) || r.undefined_p ()) + tcond =3D NULL_TREE; + else if (r.upper_bound () =3D=3D 0) + tcond =3D boolean_false_node; + else if (r.lower_bound () =3D=3D 1) + tcond =3D boolean_true_node; + else + tcond =3D NULL_TREE; + if (!tcond) + tcond =3D fold_build2 (tcode, boolean_type_node, tempn, lbub_real_cst); + stmt2 =3D gimple_build_assign (tempc, tcond); tempcn =3D make_ssa_name (tempc, stmt2); gimple_assign_set_lhs (stmt2, tempcn); @@ -475,16 +487,15 @@ gen_one_condition (tree arg, int lbub, for lower bound check, one for upper bound check. */ static void -gen_conditions_for_domain (tree arg, inp_domain domain, - vec conds, - unsigned *nconds) +gen_conditions_for_domain (tree arg, inp_domain domain, vec cond= s, + unsigned *nconds, gimple *stmt) { if (domain.has_lb) gen_one_condition (arg, domain.lb, (domain.is_lb_inclusive ? UNGE_EXPR : UNGT_EXPR), "DCE_COND_LB", "DCE_COND_LB_TEST", - conds, nconds); + conds, nconds, stmt); if (domain.has_ub) { @@ -496,7 +507,7 @@ gen_conditions_for_domain (tree arg, inp (domain.is_ub_inclusive ? UNLE_EXPR : UNLT_EXPR), "DCE_COND_UB", "DCE_COND_UB_TEST", - conds, nconds); + conds, nconds, stmt); } } @@ -518,9 +529,8 @@ gen_conditions_for_domain (tree arg, inp and *NCONDS is the number of logical conditions. */ static void -gen_conditions_for_pow_cst_base (tree base, tree expn, - vec conds, - unsigned *nconds) +gen_conditions_for_pow_cst_base (tree base, tree expn, vec conds, + unsigned *nconds, gimple *stmt) { inp_domain exp_domain; /* Validate the range of the base constant to make @@ -532,11 +542,9 @@ gen_conditions_for_pow_cst_base (tree ba real_from_integer (&mv, TYPE_MODE (TREE_TYPE (base)), 256, UNSIGNED); gcc_assert (!real_less (&mv, &bcv)); - exp_domain =3D get_domain (0, false, false, - 127, true, false); + exp_domain =3D get_domain (0, false, false, 127, true, false); - gen_conditions_for_domain (expn, exp_domain, - conds, nconds); + gen_conditions_for_domain (expn, exp_domain, conds, nconds, stmt); } /* Generate error condition code for pow calls with @@ -554,9 +562,8 @@ gen_conditions_for_pow_cst_base (tree ba conditions. */ static void -gen_conditions_for_pow_int_base (tree base, tree expn, - vec conds, - unsigned *nconds) +gen_conditions_for_pow_int_base (tree base, tree expn, vec conds, + unsigned *nconds, gimple *stmt) { gimple *base_def; tree base_val0; @@ -600,11 +607,9 @@ gen_conditions_for_pow_int_base (tree ba /* Generate condition in reverse order -- first the condition for the exp argument. */ - exp_domain =3D get_domain (0, false, false, - max_exp, true, true); + exp_domain =3D get_domain (0, false, false, max_exp, true, true); - gen_conditions_for_domain (expn, exp_domain, - conds, nconds); + gen_conditions_for_domain (expn, exp_domain, conds, nconds, stmt); /* Now generate condition for the base argument. Note it does not use the helper function @@ -660,9 +665,9 @@ gen_conditions_for_pow (gcall *pow_call, bc =3D TREE_CODE (base); if (bc =3D=3D REAL_CST) - gen_conditions_for_pow_cst_base (base, expn, conds, nconds); + gen_conditions_for_pow_cst_base (base, expn, conds, nconds, pow_call); else if (bc =3D=3D SSA_NAME) - gen_conditions_for_pow_int_base (base, expn, conds, nconds); + gen_conditions_for_pow_int_base (base, expn, conds, nconds, pow_call); else gcc_unreachable (); } @@ -852,7 +857,7 @@ gen_shrink_wrap_conditions (gcall *bi_ca inp_domain domain =3D get_no_error_domain (fnc); *nconds =3D 0; arg =3D gimple_call_arg (bi_call, 0); - gen_conditions_for_domain (arg, domain, conds, nconds); + gen_conditions_for_domain (arg, domain, conds, nconds, bi_call); } return; @@ -1290,6 +1295,8 @@ pass_call_cdce::execute (function *fun) return 0; shrink_wrap_conditional_dead_built_in_calls (cond_dead_built_in_calls); + if (get_range_query (fun) !=3D get_global_range_query ()) + disable_ranger (fun); free_dominance_info (CDI_POST_DOMINATORS); /* As we introduced new control-flow we need to insert PHI-nodes for the call-clobbers of the remaining call. */=