public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/112296] New: __builtin_constant_p doesn't propagate through member functions
@ 2023-10-30 14:22 barry.revzin at gmail dot com
  2023-10-30 14:26 ` [Bug c++/112296] " rguenth at gcc dot gnu.org
                   ` (14 more replies)
  0 siblings, 15 replies; 16+ messages in thread
From: barry.revzin at gmail dot com @ 2023-10-30 14:22 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 112296
           Summary: __builtin_constant_p doesn't propagate through member
                    functions
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: barry.revzin at gmail dot com
  Target Milestone: ---

Here's a short example:

using size_t = decltype(sizeof(0));

struct Span {
    int const* ptr;
    size_t len;

    inline constexpr auto size() const noexcept -> size_t { return len; }
};

inline int direct(Span span) {
    return __builtin_constant_p(span.size());
}

inline int indirect(Span span) {
    size_t s = span.size();
    return __builtin_constant_p(s);
}

int call_direct(int const* p) {
    return direct({.ptr=p, .len=8});
}

int call_indirect(int const* p) {
    return indirect({.ptr=p, .len=8});
}



The functions direct() and indirect() do the same thing - try to see if span's
size is constant, with direct checking size() directly and indirect first
caching it to a local variable and checking that variable. In both cases, the
size is constant - but on -O3 call_direct() returns 0 while call_indirect()
returns 1. That is, __builtin_constant_p tells me the size is constant - but
only if I put it into a variable first. 

Checking __builtin_constant_p(span.len) also returns 1, but in the real code
the member variable is private and my only access to it is via the size()
function.

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

end of thread, other threads:[~2023-11-06  7:38 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-30 14:22 [Bug c++/112296] New: __builtin_constant_p doesn't propagate through member functions barry.revzin at gmail dot com
2023-10-30 14:26 ` [Bug c++/112296] " rguenth at gcc dot gnu.org
2023-10-30 14:46 ` redi at gcc dot gnu.org
2023-10-30 14:49 ` redi at gcc dot gnu.org
2023-10-31  8:29 ` [Bug tree-optimization/112296] " rguenth at gcc dot gnu.org
2023-10-31  9:29 ` rguenth at gcc dot gnu.org
2023-10-31  9:36 ` rguenth at gcc dot gnu.org
2023-10-31 13:51 ` rth at gcc dot gnu.org
2023-10-31 13:57 ` rguenth at gcc dot gnu.org
2023-10-31 14:05 ` rth at gcc dot gnu.org
2023-10-31 14:13 ` rguenth at gcc dot gnu.org
2023-10-31 14:23 ` rguenth at gcc dot gnu.org
2023-10-31 16:55 ` joseph at codesourcery dot com
2023-11-03  7:11 ` rguenth at gcc dot gnu.org
2023-11-06  7:27 ` cvs-commit at gcc dot gnu.org
2023-11-06  7:38 ` rguenth at gcc dot gnu.org

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