]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: don't fold away 'if' with constant condition
authorJason Merrill <jason@redhat.com>
Thu, 25 Nov 2021 15:50:59 +0000 (10:50 -0500)
committerJason Merrill <jason@redhat.com>
Tue, 30 Nov 2021 21:05:24 +0000 (16:05 -0500)
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.

gcc/cp/ChangeLog:

* cp-gimplify.c (genericize_if_stmt): Always build a COND_EXPR.

gcc/cp/cp-gimplify.c

index 0988655eebab0a129e02d441ddcc1eaa09850f8c..0a002db14e709882ae12cc341a1a24183ef19d99 100644 (file)
@@ -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);