[Bug c/90293] New function attribute: expect_return

zev+gccbug at bewilderbeest dot net gcc-bugzilla@gcc.gnu.org
Wed Sep 20 06:32:11 GMT 2023


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

--- Comment #4 from Zev Weiss <zev+gccbug at bewilderbeest dot net> ---
I have no idea about the relative ease of implementation, but might it at least
partially suffice for the compiler to propagate the information provided by
__builtin_expect() beyond the expression it appears in?

So for the examples above, something like:

bool debug = false;
bool DebugModeEnabled()
{
  __builtin_expect(debug, false);
  return debug;
}

bool IsErrorCode(int code)
{
  __builtin_expect(code < 0, 0);
  return code < 0;
}

int CreateSocket()
{
  int fd = socket(...);
  __builtin_expect(fd == -1, 0);
  return fd;
}

I suppose it would have the disadvantage of not appearing in header file
declarations and thus (absent LTO) not being available to other translation
units, though for critical things you could work around that reasonably easily
by putting a tiny wrapper with the __builtin_expect() in an inline function in
the header.


More information about the Gcc-bugs mailing list