https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122779
Bug ID: 122779
Summary: "[[assume(x)]];" vs "if (!x) __builtin_unreachable();"
Product: gcc
Version: 15.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: terra at gnome dot org
Target Milestone: ---
Here are two ways of telling the compiler that it may assume the truth of some
statement:
[[assume(x)]];
if (!x) __builtin_unreachable();
I naively thought they would act identically, provided the evaluation of x
doesn't change anything. They do not.
Consider:
https://godbolt.org/z/Gh18e4bGc
and
https://godbolt.org/z/5o5Ys6Eff
(sources to be attached.)
Gcc is able to make great use of
if (!(res.size() + s.size() <= res.capacity()))
__builtin_unreachable();
but no use of
[[assume(res.size() + s.size() <= res.capacity())]];
(This is spun off from bug 122727.)