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

            Bug ID: 104601
           Summary: [11 Regression] Invalid branch elimination at -O2
           Product: gcc
           Version: 11.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: markus.boeck02 at gmail dot com
  Target Milestone: ---

Following code has been produced via reduction with `creduce`.
When compiled with `-O2`, GCC 11 and later versions will incorrectly print `f`,
while if `-O1` or lower, or an older version of GCC is used, it will correctly
print 't'.

#include <algorithm>
#include <cstdio>
#include <optional>
inline std::optional<int> a(std::vector<int>::iterator b,
                            std::vector<int>::iterator c,
                            std::optional<int> h(int)) {
  std::optional<int> d;
  find_if(b, c, [&](auto e) {
    d = h(e);
    return d;
  });
  return d;
}
std::optional<int> f(int) { return 1; }
main() {
  std::vector<int> g(100);
  auto e = a(g.begin(), g.end(), f);
  printf("%c", e ? 't' : 'f');
}

For the sake of completion, this was the original code: 
https://godbolt.org/z/enx19v7E5

Reply via email to