Bug 106744 - [13 Regression] phiopt miscompiles min/max since r13-1950-g9bb19e143cfe8863
Summary: [13 Regression] phiopt miscompiles min/max since r13-1950-g9bb19e143cfe8863
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 13.0
: P1 normal
Target Milestone: 13.0
Assignee: Tamar Christina
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2022-08-26 03:40 UTC by Krister Walfridsson
Modified: 2022-08-30 07:07 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-08-26 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Krister Walfridsson 2022-08-26 03:40:41 UTC
GCC miscompiles the following test at -O1 or higher optimization levels:

#include <stdint.h>

__attribute__((noinline)) uint8_t
three_minmax1 (uint8_t xc, uint8_t xm, uint8_t xy) {
  uint8_t  xk;
  if (xc > xm) {
    xk = (uint8_t) (xc < xy ? xc : xy);
  } else {
    xk = (uint8_t) (xm < xy ? xm : xy);
  }
  return xk;
}

int
main (void)
{
  volatile uint8_t xy = 255;
  volatile uint8_t xm = 0;
  volatile uint8_t xc = 255;
  if (three_minmax1 (xc, xm, xy) != 255)
    __builtin_abort ();
  return 0;
}

What is happening is that phiopt transforms three_minmax1 to

  _7 = MAX_EXPR <xc_2(D), xy_4(D)>;
  _9 = MIN_EXPR <_7, xm_3(D)>;
  return _9;

instead of the intended

  _7 = MAX_EXPR <xc_2(D), xm_3(D)>;
  _9 = MIN_EXPR <_7, xy_4(D)>;
  return _9;
Comment 1 Richard Biener 2022-08-26 06:19:14 UTC
Confirmed.  CCing suspect.
Comment 2 Tamar Christina 2022-08-26 10:03:21 UTC
Guilty as charged. Should have made the tests check for SSA names too.
Comment 3 Martin Liška 2022-08-26 12:47:44 UTC
Started with r13-1950-g9bb19e143cfe8863.
Comment 4 CVS Commits 2022-08-30 07:01:54 UTC
The master branch has been updated by Tamar Christina <tnfchris@gcc.gnu.org>:

https://gcc.gnu.org/g:37ebaabde2b88d446369240ae8f03b8e6a284a7b

commit r13-2259-g37ebaabde2b88d446369240ae8f03b8e6a284a7b
Author: Tamar Christina <tamar.christina@arm.com>
Date:   Tue Aug 30 07:49:02 2022 +0100

    middle-end: fix min/max phiopts reduction [PR106744]
    
    This corrects the argument usage to use them in the order that they occur in
    the comparisons in gimple.
    
    gcc/ChangeLog:
    
            PR tree-optimization/106744
            * tree-ssa-phiopt.cc (minmax_replacement): Correct arguments.
    
    gcc/testsuite/ChangeLog:
    
            PR tree-optimization/106744
            * gcc.dg/tree-ssa/minmax-10.c: Make runtime test.
            * gcc.dg/tree-ssa/minmax-11.c: Likewise.
            * gcc.dg/tree-ssa/minmax-12.c: Likewise.
            * gcc.dg/tree-ssa/minmax-13.c: Likewise.
            * gcc.dg/tree-ssa/minmax-14.c: Likewise.
            * gcc.dg/tree-ssa/minmax-15.c: Likewise.
            * gcc.dg/tree-ssa/minmax-16.c: Likewise.
            * gcc.dg/tree-ssa/minmax-3.c: Likewise.
            * gcc.dg/tree-ssa/minmax-4.c: Likewise.
            * gcc.dg/tree-ssa/minmax-5.c: Likewise.
            * gcc.dg/tree-ssa/minmax-6.c: Likewise.
            * gcc.dg/tree-ssa/minmax-7.c: Likewise.
            * gcc.dg/tree-ssa/minmax-8.c: Likewise.
            * gcc.dg/tree-ssa/minmax-9.c: Likewise.
Comment 5 Tamar Christina 2022-08-30 07:07:18 UTC
Fixed, thanks for the report.