On Tue, Jun 10, 2025 at 10:47 AM Eric Botcazou <botca...@adacore.com> wrote:
>
> Hi,
>
> the compiler can swap the operands of the short-circuit operators in some
> cases, which can be problematic for tools like Valgrind that detect uses of
> uninitialized data, and is probably counterproductive in most cases.
>
> The change prevents this from occurring, but also extends the use of jumps for
> conjunctions and disjunctions to Ada for targets where they are cheap.
>
> Tested on x86-64/Linux, OK for the mainline?

While you are at it

              if ((code2 == BIT_AND_EXPR
-                  && TYPE_PRECISION (TREE_TYPE (op0)) == 1
                   && TREE_CODE (gimple_assign_rhs2 (second)) != INTEGER_CST)
                  || code2 == TRUTH_AND_EXPR)

code2 can never be TRUTH_AND_EXPR (that doesn't exist on GIMPLE).

+      if (!is_gimple_assign (stmt))
+       continue;
+      enum tree_code code = gimple_assign_rhs_code (stmt);
+      if (!commutative_tree_code (code))
+       continue;
+      /* Do not change operations expand_gimple_cond can turn into jumps.  */
+      if (TREE_CODE (TREE_TYPE (gimple_assign_lhs (stmt))) == BOOLEAN_TYPE
+         && (code == TRUTH_AND_EXPR
+             || code == TRUTH_OR_EXPR
+             || code == BIT_AND_EXPR
+             || code == BIT_IOR_EXPR)

likewise here for TRUTH_AND/OR_EXPR.

I'll note the order is already "random" given we apply
tree_swap_operands_p during
folding to canonicalize.  So what eventually works for you in one case would now
break things in another.  In addition to that, the expand_gimple_cond case only
applies to defs that have uses in a gcond *.

Richard.

>
> 2025-06-10  Eric Botcazou  <ebotca...@adacore.com>
>
>         * cfgexpand.cc (has_cheap_jumps_and_no_conditional_compare): New
>         predicate.
>         (expand_gimple_cond): Call it.  Emit sequences of jumps for all
>         boolean types if it returns true.
>         (reorder_operands): Do not reorder the operands of expressions
>         that expand_gimple_cond can turn into jumps.
>
> --
> Eric Botcazou

Reply via email to