https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105748
Bug ID: 105748 Summary: gcc ignores the pure attribute Product: gcc Version: 12.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: yubowen15 at foxmail dot com Target Milestone: --- Snippet: const int g_value() __attribute__((pure)); int bar(int n) { int s = 0; for (int i=0; i<n; ++i) { s += g_value() * i; } return s; } Expected: call g_value only once Actual: call g_value n times The problem can be reproduced in godbolt compiler explorer: 1. command line argument: -O3 -march=skylake-avx512 -std=c++2b 2. gcc failed to do loop-invariant code motion: https://godbolt.org/z/PvWc1xYvr 3. clang successfully performed loop-invariant code motion: https://godbolt.org/z/aEhMT88jM