On Tue, Nov 10, 2026 at 11:01 PM Kyrylo Tkachov <[email protected]> wrote:
>
>
>
> > On 10 Nov 2026, at 23:08, Andrea Pinski <[email protected]> 
> > wrote:
> >
> > On Tue, Nov 10, 2026 at 1:37 PM Andrea Pinski
> > <[email protected]> wrote:
> >>
> >> On Mon, Nov 9, 2026 at 6:38 AM <[email protected]> wrote:
> >>>
> >>> From: Kyrylo Tkachov <[email protected]>
> >>>
> >>> Early phiopt keeps a PHI that forms a comparison mask:
> >>>
> >>>  if (a_1 > b_2)
> >>>    goto <bb 3>;
> >>>  else
> >>>    goto <bb 4>;
> >>>  ...
> >>>  # m_3 = PHI <-1(3), 0(2)>
> >>>
> >>> Each later test of the mask, such as m_3 == -1, then depends on the
> >>> condition only through the PHI.  The first full threader resolves these
> >>> tests by duplicating the blocks between the mask and its uses, which
> >>> hides the selects that use the mask from if-conversion.
> >>>
> >>> Allow early phiopt to form the negated mask -CMP or -(type) CMP:
> >>
> >> -CMP should not show up in gimple IR.  If it does then there is a bug in 
> >> the IR.
> >>
> >>>
> >>>  _4 = a_1 > b_2;
> >>>  _5 = (int) _4;
> >>>  m_3 = -_5;
> >>>
> >>> forwprop then folds m_3 == -1 to a_1 > b_2.  Check the match sequence
> >>> from its last statement: it can hold only the comparison and the
> >>> conversion.  A comparison in the sequence must not throw, because with
> >>> -fnon-call-exceptions it would be outside the EH region of the original
> >>> comparison.  As for a replacement by the condition itself, a user hot or
> >>> cold predictor keeps the branch.  The predictor check looks only at the
> >>> last statement of the sequence, a comparison or a negation that gives
> >>> the result, and discards the sequence when it rejects it.
> >>>
> >>> The 0/1 form (int) (a_1 > b_2) still gives worse code where ifcombine
> >>> merges the original branches, for example in ssa-ifcombine-ccmp-5.c,
> >>> ssa-ifcombine-ccmp-7.c and ssa-ifcombine-15.c on aarch64, so this patch
> >>> does not allow it.
> >>
> >> So these are being tracked as
> >> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127299 . I would rather
> >> fix that instead of not allowing `(Convert)CMP` as it seems odd to
> >> allow -(CONVERT)CMP and not without the NEG here.  The patch here
> >> should be a simple thing of allowing a convert from a boolean type to
> >> an non boolean for factoring out the convert expression.
> >
> > Actually this depends on the phiopt patch to support multiple
> > statements too.  So let's wait till that is in order to get this
> > changed.
>
> Thanks. Is that something you still intend to send out for this stage 1?

Yes I am working on iit before Friday.

> Happy to help if needed.
> Kyrill
>
> >
> >>
> >>>
> >>> ssa-ifcombine-7.c now gets the same code through the mask, which
> >>> ssa-ifcombine-7a.c already tests.  Disable phiopt1 there to keep testing
> >>> ifcombine.
> >>>
> >>> Bootstrapped and tested on aarch64-linux-gnu and x86_64-linux-gnu.
> >>> Ok for trunk?
> >>> Thanks,
> >>> Kyrill
> >>>
> >>> Cc: Andrea Pinski <[email protected]>
> >>>
> >>> gcc/ChangeLog:
> >>>
> >>>        PR tree-optimization/126043
> >>>        * tree-ssa-phiopt.cc (phiopt_comparison_seq_p): New function.
> >>>        (phiopt_early_allow): Allow negated comparison masks.
> >>>        (match_simplify_replacement): Check only the last statement of the
> >>>        sequence for user predictors, also for negated comparison masks.
> >>>        Discard the sequence when rejecting it.
> >>>
> >>> gcc/testsuite/ChangeLog:
> >>>
> >>>        PR tree-optimization/126043
> >>>        * g++.dg/opt/pr126043.C: New test.
> >>>        * g++.dg/opt/pr126043-2.C: New test.
> >>>        * gcc.dg/predict-26.c: New test.
> >>>        * gcc.dg/tree-ssa/pr126043-1.c: New test.
> >>>        * gcc.dg/tree-ssa/pr126043-2.c: New test.
> >>>        * gcc.dg/tree-ssa/ssa-ifcombine-7.c: Disable phiopt1.
> >>>
> >>> Signed-off-by: Kyrylo Tkachov <[email protected]>
> >>> ---
> >>> gcc/testsuite/g++.dg/opt/pr126043-2.C         | 20 ++++
> >>> gcc/testsuite/g++.dg/opt/pr126043.C           | 25 +++++
> >>> gcc/testsuite/gcc.dg/predict-26.c             | 50 ++++++++++
> >>> gcc/testsuite/gcc.dg/tree-ssa/pr126043-1.c    | 40 ++++++++
> >>> gcc/testsuite/gcc.dg/tree-ssa/pr126043-2.c    | 27 ++++++
> >>> .../gcc.dg/tree-ssa/ssa-ifcombine-7.c         |  3 +-
> >>> gcc/tree-ssa-phiopt.cc                        | 92 +++++++++++++------
> >>> 7 files changed, 228 insertions(+), 29 deletions(-)
> >>> create mode 100644 gcc/testsuite/g++.dg/opt/pr126043-2.C
> >>> create mode 100644 gcc/testsuite/g++.dg/opt/pr126043.C
> >>> create mode 100644 gcc/testsuite/gcc.dg/predict-26.c
> >>> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr126043-1.c
> >>> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr126043-2.c
> >>>
> >>> diff --git a/gcc/testsuite/g++.dg/opt/pr126043-2.C 
> >>> b/gcc/testsuite/g++.dg/opt/pr126043-2.C
> >>> new file mode 100644
> >>> index 00000000000..eea53c6428c
> >>> --- /dev/null
> >>> +++ b/gcc/testsuite/g++.dg/opt/pr126043-2.C
> >>> @@ -0,0 +1,20 @@
> >>> +// PR tree-optimization/126043
> >>> +// A mask formed early from a comparison that can throw feeds a range 
> >>> test.
> >>> +// { dg-do compile }
> >>> +// { dg-options "-O2 -fnon-call-exceptions -fdisable-tree-ethread" }
> >>> +
> >>> +unsigned
> >>> +f (float fa, float fb)
> >>> +{
> >>> +  unsigned s = 0;
> >>> +  int m0 = 0, m1 = 0;
> >>> +  try { m0 = fa > fb; } catch (...) { }
> >>> +  for (int i = 0; i < 4; i++)
> >>> +    {
> >>> +      m1 = fa > fb ? -1 : 0;
> >>> +      s = s * 5u + (m1 ? 0u : 3u);
> >>> +    }
> >>> +  if (m1 != -1 || fa > fb)
> >>> +    s++;
> >>> +  return s + m0;
> >>> +}
> >>> diff --git a/gcc/testsuite/g++.dg/opt/pr126043.C 
> >>> b/gcc/testsuite/g++.dg/opt/pr126043.C
> >>> new file mode 100644
> >>> index 00000000000..40313835490
> >>> --- /dev/null
> >>> +++ b/gcc/testsuite/g++.dg/opt/pr126043.C
> >>> @@ -0,0 +1,25 @@
> >>> +// PR tree-optimization/126043
> >>> +// Forming the mask early must keep the possibly trapping comparison in
> >>> +// its EH region.
> >>> +// { dg-do compile }
> >>> +// { dg-options "-O2 -fnon-call-exceptions -fdump-tree-optimized" }
> >>> +
> >>> +int
> >>> +f (float a, float b)
> >>> +{
> >>> +  int m;
> >>> +  try
> >>> +    {
> >>> +      if (a > b)
> >>> +       m = -1;
> >>> +      else
> >>> +       m = 0;
> >>> +    }
> >>> +  catch (...)
> >>> +    {
> >>> +      m = 5;
> >>> +    }
> >>> +  return m;
> >>> +}
> >>> +
> >>> +// { dg-final { scan-tree-dump "__cxa_begin_catch" "optimized" } }
> >>> diff --git a/gcc/testsuite/gcc.dg/predict-26.c 
> >>> b/gcc/testsuite/gcc.dg/predict-26.c
> >>> new file mode 100644
> >>> index 00000000000..8b0e18fc2e4
> >>> --- /dev/null
> >>> +++ b/gcc/testsuite/gcc.dg/predict-26.c
> >>> @@ -0,0 +1,50 @@
> >>> +/* { dg-do compile } */
> >>> +/* { dg-options "-O2 -fdump-tree-phiopt1-details 
> >>> -fdump-tree-profile_estimate" } */
> >>> +/* PR tree-optimization/126043 */
> >>> +
> >>> +/* The unannotated diamond is materialized as -(int) CMP by phiopt1.
> >>> +   Diamonds carrying user hot or cold predictors remain until profile
> >>> +   estimation.  */
> >>> +
> >>> +int
> >>> +hot_mask (int a, int b)
> >>> +{
> >>> +  int mask;
> >>> +  if (a > b)
> >>> +    {
> >>> +    hot: __attribute__ ((hot))
> >>> +      mask = -1;
> >>> +    }
> >>> +  else
> >>> +    mask = 0;
> >>> +  return mask;
> >>> +}
> >>> +
> >>> +int
> >>> +cold_mask (int a, int b)
> >>> +{
> >>> +  int mask;
> >>> +  if (a > b)
> >>> +    {
> >>> +    cold: __attribute__ ((cold))
> >>> +      mask = -1;
> >>> +    }
> >>> +  else
> >>> +    mask = 0;
> >>> +  return mask;
> >>> +}
> >>> +
> >>> +int
> >>> +plain_mask (int a, int b)
> >>> +{
> >>> +  int mask;
> >>> +  if (a > b)
> >>> +    mask = -1;
> >>> +  else
> >>> +    mask = 0;
> >>> +  return mask;
> >>> +}
> >>> +
> >>> +/* { dg-final { scan-tree-dump-times "hot label heuristics" 1 
> >>> "profile_estimate" } } */
> >>> +/* { dg-final { scan-tree-dump-times "cold label heuristics" 1 
> >>> "profile_estimate" } } */
> >>> +/* { dg-final { scan-tree-dump-times "converted to straightline code" 1 
> >>> "phiopt1" } } */
> >>> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr126043-1.c 
> >>> b/gcc/testsuite/gcc.dg/tree-ssa/pr126043-1.c
> >>> new file mode 100644
> >>> index 00000000000..5bc44ca7c69
> >>> --- /dev/null
> >>> +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr126043-1.c
> >>> @@ -0,0 +1,40 @@
> >>> +/* PR tree-optimization/126043 */
> >>> +/* Early phiopt turns negated comparison masks into -(type) (a CMP b), so
> >>> +   later tests of a mask fold to the comparison before jump threading.  
> >>> */
> >>> +/* { dg-do compile } */
> >>> +/* { dg-options "-O2 -fdump-tree-phiopt1 -fdump-tree-forwprop2" } */
> >>> +
> >>> +void
> >>> +f (float a, float b, float c, float d,
> >>> +   int x0, int y0, int x1, int y1,
> >>> +   int u0, int v0, int u1, int v1, int *o)
> >>> +{
> >>> +  int m0 = (a > b) ? -1 : 0;
> >>> +  int m1 = (c > d) ? -1 : 0;
> >>> +  o[0] = (m0 == -1) ? x0 : y0;
> >>> +  o[1] = (m1 == -1) ? x1 : y1;
> >>> +  o[2] = (m0 == -1) ? u0 : v0;
> >>> +  o[3] = (m1 == -1) ? u1 : v1;
> >>> +}
> >>> +
> >>> +int
> >>> +g (int a, int b)
> >>> +{
> >>> +  int m;
> >>> +  if (a > b)
> >>> +    m = 0;
> >>> +  else
> >>> +    m = -1;
> >>> +  return m & 7;
> >>> +}
> >>> +
> >>> +long
> >>> +h (unsigned a, unsigned b)
> >>> +{
> >>> +  return a < b ? -1L : 0L;
> >>> +}
> >>> +
> >>> +/* { dg-final { scan-tree-dump-not "PHI <-1\\(" "phiopt1" } } */
> >>> +/* { dg-final { scan-tree-dump-not "PHI <0\\(" "phiopt1" } } */
> >>> +/* { dg-final { scan-tree-dump-not "== -1" "forwprop2" } } */
> >>> +/* { dg-final { scan-tree-dump-times " = -_" 4 "phiopt1" } } */
> >>> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr126043-2.c 
> >>> b/gcc/testsuite/gcc.dg/tree-ssa/pr126043-2.c
> >>> new file mode 100644
> >>> index 00000000000..5082bd25e7f
> >>> --- /dev/null
> >>> +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr126043-2.c
> >>> @@ -0,0 +1,27 @@
> >>> +/* PR tree-optimization/126043 */
> >>> +/* Tests of masks formed early must stay correct for NaNs when the
> >>> +   comparisons are separate statements.  */
> >>> +/* { dg-do run } */
> >>> +/* { dg-options "-O2 -fnon-call-exceptions" } */
> >>> +/* { dg-add-options ieee } */
> >>> +/* { dg-require-effective-target exceptions } */
> >>> +
> >>> +__attribute__ ((noipa)) int
> >>> +f (double a, double b)
> >>> +{
> >>> +  int m = (a == b) ? -1 : 0;
> >>> +  int k = (a < b) ? -1 : 0;
> >>> +  if (k)
> >>> +    return 2;
> >>> +  if (m)
> >>> +    return 2;
> >>> +  return 0;
> >>> +}
> >>> +
> >>> +int
> >>> +main (void)
> >>> +{
> >>> +  if (f (0.0, __builtin_nan ("")) != 0)
> >>> +    __builtin_abort ();
> >>> +  return 0;
> >>> +}
> >>> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-7.c 
> >>> b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-7.c
> >>> index 7eab7fd4636..b88501ad0a5 100644
> >>> --- a/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-7.c
> >>> +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-7.c
> >>> @@ -1,5 +1,6 @@
> >>> /* { dg-do compile } */
> >>> -/* { dg-options "-O -fdump-tree-ifcombine-details-blocks" } */
> >>> +/* Early phiopt would form the -1 mask first, see ssa-ifcombine-7a.c.  */
> >>> +/* { dg-options "-O -fdisable-tree-phiopt1 
> >>> -fdump-tree-ifcombine-details-blocks" } */
> >>>
> >>> int test1 (int i, int j)
> >>> {
> >>> diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc
> >>> index 875f8e4af34..312de79fe91 100644
> >>> --- a/gcc/tree-ssa-phiopt.cc
> >>> +++ b/gcc/tree-ssa-phiopt.cc
> >>> @@ -659,8 +659,45 @@ factor_out_conditional_operation (edge e0, edge e1, 
> >>> basic_block merge,
> >>> }
> >>>
> >>>
> >>> +/* Return true if SEQ computes VAL by a comparison, optionally followed 
> >>> by
> >>> +   one conversion, and computes nothing else.  SEQ is checked from its
> >>> +   last statement.  The comparison can also precede SEQ, which then holds
> >>> +   only the conversion.  A comparison in SEQ must not throw, because with
> >>> +   non-call exceptions it would be outside the EH region of the original
> >>> +   comparison.  */
> >>> +
> >>> +static bool
> >>> +phiopt_comparison_seq_p (gimple_seq seq, tree val)
> >>> +{
> >>> +  gimple_stmt_iterator gsi = gsi_last (seq);
> >>> +  gassign *stmt = safe_dyn_cast <gassign *> (gsi_stmt (gsi));
> >>> +  if (!stmt || gimple_assign_lhs (stmt) != val)
> >>> +    return false;
> >>> +  gsi_prev (&gsi);
> >>> +  if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt)))
> >>> +    {
> >>> +      val = gimple_assign_rhs1 (stmt);
> >>> +      if (TREE_CODE (val) != SSA_NAME)
> >>> +       return false;
> >>> +      if (gsi_end_p (gsi))
> >>> +       stmt = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (val));
> >>> +      else
> >>> +       {
> >>> +         stmt = dyn_cast <gassign *> (gsi_stmt (gsi));
> >>> +         if (!stmt || gimple_assign_lhs (stmt) != val)
> >>> +           return false;
> >>> +         gsi_prev (&gsi);
> >>> +       }
> >>> +    }
> >>> +  return (stmt
> >>> +         && gsi_end_p (gsi)
> >>> +         && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == 
> >>> tcc_comparison
> >>> +         && (gimple_bb (stmt) || !stmt_could_throw_p (cfun, stmt)));
> >>> +}
> >>> +
> >>> /* Return TRUE if SEQ/OP pair should be allowed during early phiopt.
> >>> -   Currently this is to allow MIN/MAX and ABS/NEGATE and constants.  */
> >>> +   Currently this is to allow MIN/MAX and ABS/NEGATE, constants and
> >>> +   negated comparison masks.  */
> >>> static bool
> >>> phiopt_early_allow (gimple_seq &seq, gimple_match_op &op)
> >>> {
> >>> @@ -669,8 +706,8 @@ phiopt_early_allow (gimple_seq &seq, gimple_match_op 
> >>> &op)
> >>>     return false;
> >>>   tree_code code = (tree_code)op.code;
> >>>
> >>> -  /* For non-empty sequence, only allow one statement
> >>> -     a MIN/MAX and an original MIN/MAX.  */
> >>> +  /* For a non-empty sequence, only allow a MIN/MAX computed by a single
> >>> +     MIN/MAX statement, or a negated comparison mask.  */
> >>>   if (!gimple_seq_empty_p (seq))
> >>>     {
> >>>       if (code == MIN_EXPR || code == MAX_EXPR)
> >>> @@ -685,6 +722,9 @@ phiopt_early_allow (gimple_seq &seq, gimple_match_op 
> >>> &op)
> >>>          code = gimple_assign_rhs_code (stmt);
> >>>          return code == MIN_EXPR || code == MAX_EXPR;
> >>>        }
> >>> +      /* Allow a negated comparison mask such as -(int) (a > b).  */
> >>> +      if (code == NEGATE_EXPR)
> >>> +       return phiopt_comparison_seq_p (seq, op.ops[0]);
> >>>       return false;
> >>>     }
> >>>
> >>> @@ -1184,33 +1224,29 @@ match_simplify_replacement (basic_block cond_bb, 
> >>> basic_block middle_bb,
> >>>   }
> >>>
> >>>   /* For early phiopt, we don't want to lose user generated predictors
> >>> -     if the phiopt is converting `if (a)` into `a` as that might
> >>> -     be jump threaded later on so we want to keep around the
> >>> -     predictors.  */
> >>> +     if the phiopt is converting `if (a)` into `a` or into a mask
> >>> +     `-(type) a` as that might be jump threaded later on so we want to
> >>> +     keep around the predictors.  */
> >>>   if (early_p && result && TREE_CODE (result) == SSA_NAME)
> >>>     {
> >>> -      bool check_it = false;
> >>> -      tree cmp0 = gimple_cond_lhs (stmt);
> >>> -      tree cmp1 = gimple_cond_rhs (stmt);
> >>> -      if (result == cmp0 || result == cmp1)
> >>> -       check_it = true;
> >>> -      else if (gimple_seq_singleton_p (seq))
> >>> -        {
> >>> -         gimple *stmt = gimple_seq_first_stmt (seq);
> >>> -         if (is_gimple_assign (stmt)
> >>> -             && result == gimple_assign_lhs (stmt)
> >>> -             && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt))
> >>> -                  == tcc_comparison)
> >>> -           check_it = true;
> >>> -        }
> >>> -      if (!check_it)
> >>> -       ;
> >>> -      else if (contains_hot_cold_predict (middle_bb))
> >>> -       return false;
> >>> -      else if (threeway_p
> >>> -              && middle_bb != middle_bb_alt
> >>> -              && contains_hot_cold_predict (middle_bb_alt))
> >>> -       return false;
> >>> +      gassign *last = safe_dyn_cast <gassign *> (gimple_seq_last_stmt 
> >>> (seq));
> >>> +      bool check_it
> >>> +       = (result == gimple_cond_lhs (stmt)
> >>> +          || result == gimple_cond_rhs (stmt)
> >>> +          || (last
> >>> +              && result == gimple_assign_lhs (last)
> >>> +              && (TREE_CODE_CLASS (gimple_assign_rhs_code (last))
> >>> +                    == tcc_comparison
> >>> +                  || gimple_assign_rhs_code (last) == NEGATE_EXPR)));
> >>> +      if (check_it
> >>> +         && (contains_hot_cold_predict (middle_bb)
> >>> +             || (threeway_p
> >>> +                 && middle_bb != middle_bb_alt
> >>> +                 && contains_hot_cold_predict (middle_bb_alt))))
> >>> +       {
> >>> +         gimple_seq_discard (seq);
> >>> +         return false;
> >>> +       }
> >>>     }
> >>>
> >>>   if (!result)
> >>> --
> >>> 2.54.0 (Apple Git-157)
>
>

Reply via email to