https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117279
Bug ID: 117279 Summary: std::string bounds check from -D_GLIBCXX_ASSERTIONS not optimised out Product: gcc Version: 15.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: sjames at gcc dot gnu.org Target Milestone: --- Even at -O3, we (and Clang) don't seem to optimise out the unnecessary bounds checks from -D_GLIBCXX_ASSERTIONS for this using `-O3 -D_GLIBCXX_ASSERTIONS`: ``` #include <string> int foo(int index) { if (index < 0 || index > 4) { return 0; } std::string p = "Hello world"; __builtin_printf("%c\n", p[index]); return p[index]; } ``` If we rewrite it as this, we can as well as Clang: ``` #include <string> void foo(int index) { if (index < 0 || index > 4) { return; } std::string p = "Hello world"; __builtin_printf("%c\n", p[index]); } ``` https://godbolt.org/z/6PGf4WKnK