https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107639
Bug ID: 107639 Summary: GCC unable to reason about range of idx/len Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: jmuizelaar at mozilla dot com Target Milestone: --- Clang 14 is able to optimize this function to just 'ret'. GCC 12.2 is not. #include <cstddef> void do_checks(const int* begin, const size_t len){ size_t idx = 0; const auto end = begin + len; for (const int* it = begin; it!=end; ++it, ++idx){ if (idx <= len){ // Do something useful } else { throw 5; } } } https://godbolt.org/z/f7PjjqG9T