public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "bmei at broadcom dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/111036] New: Code generation error in handling __builtin_constant_p
Date: Wed, 16 Aug 2023 09:42:02 +0000	[thread overview]
Message-ID: <bug-111036-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 111036
           Summary: Code generation error in handling __builtin_constant_p
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bmei at broadcom dot com
  Target Milestone: ---

Compile and run following code


#include <stdio.h>
#define __align(n) __attribute__((aligned(n)))

__attribute__((aligned(32))) static struct {

    unsigned long long available_cmd_ids_per_core[2];
} _rl2c_cmd_id_data;

static inline void __attribute__((always_inline))
foo (void *base, size_t length)
{
    unsigned long int p = (unsigned long int) base;
    if (__builtin_constant_p(p) && (p & 31) == 0) { printf("constant p &&
aligned to 32\n"); } 
    else if (__builtin_constant_p(length)) { printf("constant length\n");} 
    else { printf("else\n"); }
}

int main(int argc, char **argv)
{
    foo(&_rl2c_cmd_id_data, sizeof(*(&_rl2c_cmd_id_data)));
    return 0;
}


With gcc 12.1.0 & gcc 13.1.0,  I got segmentation fault. With 11.1.0 and below,
I got correct result. I examined the dumped tree IR. In einline pass, a
__builtin_unreachable is inserted for else if/else branches as the compiler
probably thinks __builtin_constant_p(p) & (p&31) is always true. But the later
passes think __builtin_constant_p(p) is always false. Therefore all code are
optimized away.

             reply	other threads:[~2023-08-16  9:42 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-16  9:42 bmei at broadcom dot com [this message]
2023-08-17  0:19 ` [Bug ipa/111036] " pinskia at gcc dot gnu.org
2023-08-17  0:20 ` pinskia at gcc dot gnu.org
2023-08-20  8:25 ` pinskia at gcc dot gnu.org

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-111036-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).