public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/99755] New: failure to fold a conditional that's a subset of another expression
@ 2021-03-24 23:36 msebor at gcc dot gnu.org
  2021-03-25  7:57 ` [Bug tree-optimization/99755] " rguenth at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-03-24 23:36 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99755

            Bug ID: 99755
           Summary: failure to fold a conditional that's a subset of
                    another expression
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

GCC successfully folds to false the second conditional expression in f1() but
it fails to do the same in f2() and f3().  In addition (and likely as a
result), it triggers a bogus -Wmaybe-uninitialized in both functions. 

Clang and ICC fold all three expressions to false and emit optimal code for all
three functions.

Initializing the local variable to any value lets GCC fold the conditional and
avoid the warning.  But then, replacing  the test for x != i + 1 in f3() with x
!= 3 as shown at below the first test case, the conditional is again not folded
 (making the same change in f2() allows the folding to take place).

The bogus -Wmaybe-uninitialized first appeared in 4.9.  As far as I can tell
none of the failures to fold is a regression.

$ cat t.c && gcc -O2 -S -Wall t.c
void f1 (int i)
{ 
  int x;
  if (i > 1)
    x = i + 1;

  if (i == 2 && x != i + 1)   // folded to false
    __builtin_abort ();
}

void f2 (int i, int j)
{ 
  int x;
  if (i > 1 && j > 2)
    x = i + 1;

  if (i == 2 && j == 3 && x != i + 1)   // not folded
    __builtin_abort ();
}


void f3 (int i, int j, int k)
{
  int x;
  if (i > 1 && j > 2 && k > 3)
    x = i + 1;

  if (i == 2 && j == 3 && k == 4 && x != i + 1)   // not folded
    __builtin_abort ();
}


;; Function f1 (f1, funcdef_no=0, decl_uid=1943, cgraph_uid=1, symbol_order=0)

void f1 (int i)
{
  <bb 2> [local count: 1073741824]:
  return;

}
t.c: In function ‘f2’:
t.c:17:24: warning: ‘x’ may be used uninitialized in this function
[-Wmaybe-uninitialized]
   17 |   if (i == 2 && j == 3 && x != i + 1)
      |       ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
t.c: In function ‘f3’:
t.c:28:34: warning: ‘x’ may be used uninitialized in this function
[-Wmaybe-uninitialized]
   28 |   if (i == 2 && j == 3 && k == 4 && x != i + 1)
      |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~


The following is also not folded:

void f3 (int i, int j, int k)
{ 
  int x = 0;
  if (i > 1 && j > 2 && k > 3)
    x = i + 1;

  if (i == 2 && j == 3 && k == 4 && x != 3)   // not folded
    __builtin_abort ();
}

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-01-12  0:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-24 23:36 [Bug tree-optimization/99755] New: failure to fold a conditional that's a subset of another expression msebor at gcc dot gnu.org
2021-03-25  7:57 ` [Bug tree-optimization/99755] " rguenth at gcc dot gnu.org
2021-03-25 13:52 ` amacleod at redhat dot com
2022-01-12  0:39 ` amacleod at redhat dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).