public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "msebor at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/99755] New: failure to fold a conditional that's a subset of another expression
Date: Wed, 24 Mar 2021 23:36:30 +0000	[thread overview]
Message-ID: <bug-99755-4@http.gcc.gnu.org/bugzilla/> (raw)

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 ();
}

             reply	other threads:[~2021-03-24 23:36 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-24 23:36 msebor at gcc dot gnu.org [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-99755-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).