[PATCH] Get rid of ? true : false and simplify ? false : true

Jakub Jelinek <[email protected]>
Newsgroups gmane.comp.gcc.patches
Message-ID <amyqINnHmvqKCveU@tucnak>
Hi!

Last night I've noticed in match.pd various places like
cmp == EQ_EXPR ? true : false
and
cmp == EQ_EXPR ? false : true
I don't think that is useful, neither for readers nor for code formatting.
Sure, x ? true : false is not always equivalent to just x, but if it is
passed to a bool argument or sets a bool variable or if x is actually
a comparison in C++, it is exactly the same.
I think using just cmp == EQ_EXPR and cmp != EQ_EXPR is better.

So far lightly tested, ok for trunk if it passes full bootstrap/regtest?

2026-07-31  Jakub Jelinek  <[email protected]>

	* ipa-polymorphic-call.cc (csftc_abort_walking_p): Remove useless
	"? true : false".
	* tree-ssa-loop-im.cc (ref_indep_loop_p): Likewise.
	* match.pd (X ==/!= !X is false/true): Replace "? false : true"
	with negation of the condition.
	(((C << x) & D) != 0): Likewise.
	(fold_sign_changed_comparison and fold_widened_comparison): Likewise.
	Remove useless "? true : false".
	(if the second operand is NaN, the result is constant): Replace
	"? false : true" with negation of the condition.
	(__builtin_ctz (x) >= C -> (x & ((1 << C) - 1)) == 0): Likewise.
	Remove useless "? true : false".
	(__builtin_ctz (x) == C -> (x & ((1 << (C + 1)) - 1)) == (1 << C)):
	Replace "? false : true" with negation of the condition.
	(__builtin_ffs (X) == 0 -> X == 0): Remove useless "? true : false".
	(__builtin_ffs (X) > 6 -> X != 0 && (X & 63) == 0): Likewise.
	Replace "? false : true" with negation of the condition.
	* gimple-pretty-print.cc (dump_phi_nodes):
	Use (flags & TDF_GIMPLE) == 0 instead
	of (flags & TDF_GIMPLE) ? false : true.
gcc/fortran/
	* expr.cc (gfc_check_init_expr): Remove useless "? true : false".
	(gfc_expr_check_typed): Replace "? false : true" with negation of the
	condition.
	* parse.cc (gfc_find_state): Likewise.
	* resolve.cc (impure_stmt_fcn): Likewise.
	* arith.cc (gfc_check_character_range): Remove useless
	"? true : false".
	* array.cc (is_constant_element): Likewise.
	* decl.cc (gfc_verify_c_interop): Likewise.
	* interface.cc (gfc_check_dummy_characteristics): Likewise.
	* io.cc (check_open_constraints): Likewise.
	(check_close_constraints): Likewise.
	(check_io_constraints): Likewise.
gcc/jit/
	* jit-recording.cc (recording::context::set_bool_option): Remove
	useless "? true : false".

--- gcc/ipa-polymorphic-call.cc.jj	2026-05-30 17:45:09.439109090 +0200
+++ gcc/ipa-polymorphic-call.cc	2026-07-31 15:17:30.309767738 +0200
@@ -1405,7 +1405,7 @@ static inline bool
 csftc_abort_walking_p (unsigned speculative)
 {
   unsigned max = param_max_speculative_devirt_maydefs;
-  return speculative > max ? true : false;
+  return speculative > max;
 }
 
 /* Callback of walk_aliased_vdefs and a helper function for
--- gcc/tree-ssa-loop-im.cc.jj	2026-06-25 10:03:50.887435500 +0200
+++ gcc/tree-ssa-loop-im.cc	2026-07-31 15:17:57.214437056 +0200
@@ -3257,7 +3257,7 @@ ref_indep_loop_p (class loop *loop, im_m
       /* tri-state, { unknown, independent, dependent }  */
       dep_state state = query_loop_dependence (loop, ref, kind);
       if (state != dep_unknown)
-	return state == dep_independent ? true : false;
+	return state == dep_independent;
 
       class loop *inner = loop->inner;
       while (inner)
--- gcc/match.pd.jj	2026-07-31 09:11:01.089161684 +0200
+++ gcc/match.pd	2026-07-31 15:19:53.051013328 +0200
@@ -2522,7 +2522,7 @@ (define_operator_list SYNC_FETCH_AND_AND
 (for op (bit_ior bit_xor eq ne)
  (simplify
   (op:c truth_valued_p@0 (logical_inverted_value @0))
-  { constant_boolean_node (op == EQ_EXPR ? false : true, type); }))
+  { constant_boolean_node (op != EQ_EXPR, type); }))
 
 /* ~~x -> x */
 (simplify
@@ -5289,7 +5289,7 @@ (define_operator_list SYNC_FETCH_AND_AND
 	    than D.  See PR126476.  */
 	 || !wi::fits_to_tree_p (wi::shwi (c1 - c2, HOST_BITS_PER_INT),
 				 TREE_TYPE (@0)))
-     { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
+     { constant_boolean_node (cmp != NE_EXPR, type); }
      (icmp @0 { build_int_cst (TREE_TYPE (@0), c1 - c2); }))))
  (simplify
   (cmp (bit_and (rshift integer_pow2p@1 @0) integer_pow2p@2) integer_zerop)
@@ -5299,7 +5299,7 @@ (define_operator_list SYNC_FETCH_AND_AND
      (if (c1 > c2
 	  || !wi::fits_to_tree_p (wi::shwi (c2 - c1, HOST_BITS_PER_INT),
 				  TREE_TYPE (@0)))
-      { constant_boolean_node (cmp == NE_EXPR ? false : true, type); }
+      { constant_boolean_node (cmp != NE_EXPR, type); }
       (icmp @0 { build_int_cst (TREE_TYPE (@0), c2 - c1); }))))))
 
 /* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
@@ -8104,11 +8104,11 @@ (define_operator_list SYNC_FETCH_AND_AND
 	}
 	(if (above || below)
 	 (if (cmp == EQ_EXPR || cmp == NE_EXPR)
-	  { constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
+	  { constant_boolean_node (cmp != EQ_EXPR, type); }
 	  (if (cmp == LT_EXPR || cmp == LE_EXPR)
-	   { constant_boolean_node (above ? true : false, type); }
+	   { constant_boolean_node (above, type); }
 	   (if (cmp == GT_EXPR || cmp == GE_EXPR)
-	    { constant_boolean_node (above ? false : true, type); })))))))
+	    { constant_boolean_node (!above, type); })))))))
 
     /* For eq/ne with narrowing conversion:
        (T)(X) == (T)(Y) -> (T)(X ^ Y) == 0  */
@@ -8584,8 +8584,8 @@ (define_operator_list SYNC_FETCH_AND_AND
   (cmp @0 REAL_CST@1)
   (if (REAL_VALUE_ISNAN (TREE_REAL_CST (@1))
        && (cmp != LTGT_EXPR || ! flag_trapping_math))
-   { constant_boolean_node (cmp == ORDERED_EXPR || cmp == LTGT_EXPR
-			    ? false : true, type); })))
+   { constant_boolean_node (cmp != ORDERED_EXPR && cmp != LTGT_EXPR,
+			    type); })))
 
 /* Fold UNORDERED if either operand must be NaN, or neither can be.  */
 (simplify
@@ -10535,9 +10535,9 @@ (define_operator_list SYNC_FETCH_AND_AND
 	  }
      (if (ok && prec <= MAX_FIXED_MODE_SIZE)
       (if (val <= 0)
-       { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); }
+       { constant_boolean_node (cmp == EQ_EXPR, type); }
        (if (val >= prec)
-	{ constant_boolean_node (cmp == EQ_EXPR ? false : true, type); }
+	{ constant_boolean_node (cmp != EQ_EXPR, type); }
 	(cmp (bit_and @0 { wide_int_to_tree (type0,
 					     wi::mask (val, false, prec)); })
 	     { build_zero_cst (type0); })))))))
@@ -10557,7 +10557,7 @@ (define_operator_list SYNC_FETCH_AND_AND
 	  }
      (if (ok && prec <= MAX_FIXED_MODE_SIZE)
       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
-       { constant_boolean_node (op == EQ_EXPR ? false : true, type); }
+       { constant_boolean_node (op != EQ_EXPR, type); }
        (op (bit_and @0 { wide_int_to_tree (type0,
 					   wi::mask (tree_to_uhwi (@1) + 1,
 						     false, prec)); })
@@ -10593,10 +10593,10 @@ (define_operator_list SYNC_FETCH_AND_AND
 	  }
      (if (val <= 0)
       (if (ok && zero_val >= val)
-       { constant_boolean_node (cmp == EQ_EXPR ? true : false, type); })
+       { constant_boolean_node (cmp == EQ_EXPR, type); })
       (if (val >= prec)
        (if (ok && zero_val < val)
-	{ constant_boolean_node (cmp == EQ_EXPR ? false : true, type); })
+	{ constant_boolean_node (cmp != EQ_EXPR, type); })
        (if (ok && (zero_val < 0 || zero_val >= prec))
 	(cmp (bit_and @0 { wide_int_to_tree (type0,
 					     wi::mask (val, false, prec)); })
@@ -10612,7 +10612,7 @@ (define_operator_list SYNC_FETCH_AND_AND
     (if (prec <= MAX_FIXED_MODE_SIZE)
      (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) >= prec)
       (if (zero_val != wi::to_widest (@1))
-       { constant_boolean_node (op == EQ_EXPR ? false : true, type); })
+       { constant_boolean_node (op != EQ_EXPR, type); })
       (if (zero_val < 0 || zero_val >= prec)
        (op (bit_and @0 { wide_int_to_tree (type0,
 					   wi::mask (tree_to_uhwi (@1) + 1,
@@ -11284,7 +11284,7 @@ (define_operator_list SYNC_FETCH_AND_AND
       (if (integer_zerop (@1))
        (cmp @0 { build_zero_cst (TREE_TYPE (@0)); }))
       (if (tree_int_cst_sgn (@1) < 0 || wi::to_widest (@1) > prec)
-       { constant_boolean_node (cmp == NE_EXPR ? true : false, type); })
+       { constant_boolean_node (cmp == NE_EXPR, type); })
       (if (single_use (@2))
        (cmp (bit_and @0 { wide_int_to_tree (TREE_TYPE (@0),
 					    wi::mask (tree_to_uhwi (@1),
@@ -11305,9 +11305,9 @@ (define_operator_list SYNC_FETCH_AND_AND
       (if (integer_zerop (@1))
        (cmp2 @0 { build_zero_cst (TREE_TYPE (@0)); }))
       (if (tree_int_cst_sgn (@1) < 0)
-       { constant_boolean_node (cmp == GT_EXPR ? true : false, type); })
+       { constant_boolean_node (cmp == GT_EXPR, type); })
       (if (wi::to_widest (@1) >= prec)
-       { constant_boolean_node (cmp == GT_EXPR ? false : true, type); })
+       { constant_boolean_node (cmp != GT_EXPR, type); })
       (if (wi::to_widest (@1) == prec - 1)
        (cmp3 @0 { wide_int_to_tree (TREE_TYPE (@0),
 				    wi::shifted_mask (prec - 1, 1,
--- gcc/gimple-pretty-print.cc.jj	2026-03-27 10:17:14.165330233 +0100
+++ gcc/gimple-pretty-print.cc	2026-07-31 15:21:17.902970820 +0200
@@ -3023,8 +3023,7 @@ dump_phi_nodes (pretty_printer *pp, basi
       if (!virtual_operand_p (gimple_phi_result (phi)) || (flags & TDF_VOPS))
         {
           INDENT (indent);
-	  dump_gimple_phi (pp, phi, indent,
-			   (flags & TDF_GIMPLE) ? false : true, flags);
+	  dump_gimple_phi (pp, phi, indent, (flags & TDF_GIMPLE) == 0, flags);
           pp_newline (pp);
         }
     }
--- gcc/fortran/expr.cc.jj	2026-07-29 10:01:14.292373398 +0200
+++ gcc/fortran/expr.cc	2026-07-31 15:25:27.963898620 +0200
@@ -3413,7 +3413,7 @@ gfc_check_init_expr (gfc_expr *e)
       break;
 
     case EXPR_STRUCTURE:
-      t = e->ts.is_iso_c ? true : false;
+      t = e->ts.is_iso_c;
       if (t)
 	break;
 
@@ -6112,7 +6112,7 @@ gfc_expr_check_typed (gfc_expr* e, gfc_n
   check_typed_ns = ns;
   error_found = gfc_traverse_expr (e, NULL, &expr_check_typed_help, 0);
 
-  return error_found ? false : true;
+  return !error_found;
 }
 
 
--- gcc/fortran/parse.cc.jj	2026-07-30 22:43:17.993110629 +0200
+++ gcc/fortran/parse.cc	2026-07-31 15:22:31.631065011 +0200
@@ -2129,7 +2129,7 @@ gfc_find_state (gfc_compile_state state)
     if (p->state == state)
       break;
 
-  return (p == NULL) ? false : true;
+  return p != NULL;
 }
 
 
--- gcc/fortran/resolve.cc.jj	2026-07-30 22:43:17.995110605 +0200
+++ gcc/fortran/resolve.cc	2026-07-31 15:22:54.200787725 +0200
@@ -3292,7 +3292,7 @@ impure_stmt_fcn (gfc_expr *e, gfc_symbol
 	|| e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
     return false;
 
-  return gfc_pure_function (e, &name) ? false : true;
+  return !gfc_pure_function (e, &name);
 }
 
 
--- gcc/fortran/arith.cc.jj	2026-03-27 10:17:14.129330821 +0100
+++ gcc/fortran/arith.cc	2026-07-31 15:24:08.186878747 +0200
@@ -354,7 +354,7 @@ gfc_check_character_range (gfc_char_t c,
     return true;
 
   if (kind == 1)
-    return c <= 255 ? true : false;
+    return c <= 255;
 
   gcc_unreachable ();
 }
--- gcc/fortran/array.cc.jj	2026-03-27 10:17:14.129330821 +0100
+++ gcc/fortran/array.cc	2026-07-31 15:24:20.620725987 +0200
@@ -2118,7 +2118,7 @@ is_constant_element (gfc_expr *e)
   rv = gfc_is_constant_expr (e);
   gfc_free_expr (e);
 
-  return rv ? true : false;
+  return rv;
 }
 
 
--- gcc/fortran/decl.cc.jj	2026-07-01 11:54:41.893877858 +0200
+++ gcc/fortran/decl.cc	2026-07-31 15:24:49.210374739 +0200
@@ -6592,8 +6592,7 @@ bool
 gfc_verify_c_interop (gfc_typespec *ts)
 {
   if (ts->type == BT_DERIVED && ts->u.derived != NULL)
-    return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
-	   ? true : false;
+    return ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c;
   else if (ts->type == BT_CLASS)
     return false;
   else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
--- gcc/fortran/interface.cc.jj	2026-07-16 09:44:33.624721933 +0200
+++ gcc/fortran/interface.cc	2026-07-31 15:25:41.168736386 +0200
@@ -1398,7 +1398,7 @@ gfc_check_dummy_characteristics (gfc_sym
 				 int err_len)
 {
   if (s1 == NULL || s2 == NULL)
-    return s1 == s2 ? true : false;
+    return s1 == s2;
 
   if (s1->attr.proc == PROC_ST_FUNCTION || s2->attr.proc == PROC_ST_FUNCTION)
     {
--- gcc/fortran/io.cc.jj	2026-07-08 11:10:07.737602225 +0200
+++ gcc/fortran/io.cc	2026-07-31 15:26:20.567252342 +0200
@@ -2223,7 +2223,7 @@ check_open_constraints (gfc_open *open,
     } \
 }
 
-  bool warn = (open->err || open->iostat) ? true : false;
+  bool warn = open->err || open->iostat;
 
   /* Checks on the ACCESS specifier.  */
   if (open->access && open->access->expr_type == EXPR_CONSTANT)
@@ -2750,7 +2750,7 @@ cleanup:
 static bool
 check_close_constraints (gfc_close *close, locus *where)
 {
-  bool warn = (close->iostat || close->err) ? true : false;
+  bool warn = close->iostat || close->err;
 
   if (close->unit == NULL)
     {
@@ -3842,7 +3842,7 @@ if (condition) \
   gfc_symbol *sym = NULL;
   bool warn, unformatted;
 
-  warn = (dt->err || dt->iostat) ? true : false;
+  warn = dt->err || dt->iostat;
   unformatted = dt->format_expr == NULL && dt->format_label == NULL
 		&& dt->namelist == NULL;
 
--- gcc/jit/jit-recording.cc.jj	2026-03-27 10:17:14.213329450 +0100
+++ gcc/jit/jit-recording.cc	2026-07-31 15:27:38.548294293 +0200
@@ -1546,7 +1546,7 @@ recording::context::set_bool_option (enu
 		 "unrecognized (enum gcc_jit_bool_option) value: %i", opt);
       return;
     }
-  m_bool_options[opt] = value ? true : false;
+  m_bool_options[opt] = value;
   log_bool_option (opt);
 }
 
@@ -1555,7 +1555,7 @@ recording::context::set_inner_bool_optio
 					   int value)
 {
   gcc_assert (inner_opt >= 0 && inner_opt < NUM_INNER_BOOL_OPTIONS);
-  m_inner_bool_options[inner_opt] = value ? true : false;
+  m_inner_bool_options[inner_opt] = value;
   log_inner_bool_option (inner_opt);
 }
 

	Jakub
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.