https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103991
--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
For consteval if I guess we could do:
--- gcc/cp/cp-objcp-common.c.jj 2022-01-11 23:11:22.091294356 +0100
+++ gcc/cp/cp-objcp-common.c 2022-01-12 17:57:18.232202275 +0100
@@ -313,6 +313,13 @@ cxx_block_may_fallthru (const_tree stmt)
return false;
case IF_STMT:
+ if (IF_STMT_CONSTEXPR_P (stmt))
+ {
+ if (integer_nonzerop (IF_COND (stmt)))
+ return block_may_fallthru (THEN_CLAUSE (stmt));
+ if (integer_zerop (IF_COND (stmt)))
+ return block_may_fallthru (ELSE_CLAUSE (stmt));
+ }
if (block_may_fallthru (THEN_CLAUSE (stmt)))
return true;
return block_may_fallthru (ELSE_CLAUSE (stmt));
--- gcc/cp/cp-gimplify.c.jj 2022-01-11 23:11:22.090294370 +0100
+++ gcc/cp/cp-gimplify.c 2022-01-12 17:53:57.431036236 +0100
@@ -166,8 +166,13 @@ genericize_if_stmt (tree *stmt_p)
can contain unfolded immediate function calls, we have to discard
the then_ block regardless of whether else_ has side-effects or not. */
if (IF_STMT_CONSTEVAL_P (stmt))
- stmt = build3 (COND_EXPR, void_type_node, boolean_false_node,
- void_node, else_);
+ {
+ if (block_may_fallthru (then_))
+ stmt = build3 (COND_EXPR, void_type_node, boolean_false_node,
+ void_node, else_);
+ else
+ stmt = else_;
+ }
else
stmt = build3 (COND_EXPR, void_type_node, cond, then_, else_);
protected_set_expr_location_if_unset (stmt, locus);