From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1734) id 22A873858D35; Thu, 19 Oct 2023 20:20:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 22A873858D35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1697746824; bh=sq8ZE8Nq00zcYgEhtUoAzDsDz8Lq2nZ0XyNE74bC5Pk=; h=From:To:Subject:Date:From; b=Yn8cUb3IxoUrtcCGukafvywnKIq3dW8SVVsoEaNO/+BRtr8kn7mld9uf4T02Yeb08 wd7KYgNO8rJK28ygfMlVNe41iRaVE80y5YG2PQKKvz+xz1rb641Q9ElR3P32DwhHcc xVZADWuXG3NOtSCKmkrVLAuw/IGCD9xemmv9Y8JM= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Marek Polacek To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-4768] c++: small tweak for cp_fold_r X-Act-Checkin: gcc X-Git-Author: Marek Polacek X-Git-Refname: refs/heads/trunk X-Git-Oldrev: 86d0b086648540f8318c7454be2061bd8fc030df X-Git-Newrev: 4d81962ba00ab74c4bc5fac685988f9cf778a9bb Message-Id: <20231019202024.22A873858D35@sourceware.org> Date: Thu, 19 Oct 2023 20:20:24 +0000 (GMT) List-Id: https://gcc.gnu.org/g:4d81962ba00ab74c4bc5fac685988f9cf778a9bb commit r14-4768-g4d81962ba00ab74c4bc5fac685988f9cf778a9bb Author: Marek Polacek Date: Thu Oct 19 09:57:53 2023 -0400 c++: small tweak for cp_fold_r This patch is an optimization tweak for cp_fold_r. If we cp_fold_r the COND_EXPR's op0 first, we may be able to evaluate it to a constant if -O. cp_fold has: 3143 if (callee && DECL_DECLARED_CONSTEXPR_P (callee) 3144 && !flag_no_inline) ... 3151 r = maybe_constant_value (x, /*decl=*/NULL_TREE, flag_no_inline is 1 for -O0: 1124 if (opts->x_optimize == 0) 1125 { 1126 /* Inlining does not work if not optimizing, 1127 so force it not to be done. */ 1128 opts->x_warn_inline = 0; 1129 opts->x_flag_no_inline = 1; 1130 } but otherwise it's 0 and cp_fold will maybe_constant_value calls to constexpr functions. And if it doesn't, then folding the COND_EXPR will keep both arms, and we can avoid calling maybe_constant_value. gcc/cp/ChangeLog: * cp-gimplify.cc (cp_fold_r): Don't call maybe_constant_value. Diff: --- gcc/cp/cp-gimplify.cc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc index a282c3930a37..33e9411f10c6 100644 --- a/gcc/cp/cp-gimplify.cc +++ b/gcc/cp/cp-gimplify.cc @@ -1152,13 +1152,12 @@ cp_fold_r (tree *stmt_p, int *walk_subtrees, void *data_) auto then_fn = cp_fold_r, else_fn = cp_fold_r; /* See if we can figure out if either of the branches is dead. If it is, we don't need to do everything that cp_fold_r does. */ - tree cond = maybe_constant_value (TREE_OPERAND (stmt, 0)); - if (integer_zerop (cond)) + cp_walk_tree (&TREE_OPERAND (stmt, 0), cp_fold_r, data, nullptr); + if (integer_zerop (TREE_OPERAND (stmt, 0))) then_fn = cp_fold_immediate_r; - else if (TREE_CODE (cond) == INTEGER_CST) + else if (integer_nonzerop (TREE_OPERAND (stmt, 0))) else_fn = cp_fold_immediate_r; - cp_walk_tree (&TREE_OPERAND (stmt, 0), cp_fold_r, data, nullptr); if (TREE_OPERAND (stmt, 1)) cp_walk_tree (&TREE_OPERAND (stmt, 1), then_fn, data, nullptr);