[PATCH] c++: don't fold away 'if' with constant condition

Jason Merrill jason@redhat.com
Tue Nov 30 21:05:08 GMT 2021


richi's recent unreachable code warning experiments had trouble with the C++
front end folding away an 'if' with a constant condition.  Let's do less
folding at the statement level.  Thanks to Marek for finding the offending
code.

Tested x86_64-pc-linux-gnu, applying to trunk.

gcc/cp/ChangeLog:

	* cp-gimplify.c (genericize_if_stmt): Always build a COND_EXPR.
---
 gcc/cp/cp-gimplify.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c
index 0988655eeba..0a002db14e7 100644
--- a/gcc/cp/cp-gimplify.c
+++ b/gcc/cp/cp-gimplify.c
@@ -166,11 +166,8 @@ 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 = else_;
-  else if (integer_nonzerop (cond) && !TREE_SIDE_EFFECTS (else_))
-    stmt = then_;
-  else if (integer_zerop (cond) && !TREE_SIDE_EFFECTS (then_))
-    stmt = else_;
+    stmt = build3 (COND_EXPR, void_type_node, boolean_false_node,
+		   void_node, else_);
   else
     stmt = build3 (COND_EXPR, void_type_node, cond, then_, else_);
   protected_set_expr_location_if_unset (stmt, locus);

base-commit: 92de188ea3d36ec012b6d42959d4722e42524256
-- 
2.27.0



More information about the Gcc-patches mailing list