https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112905
Bug ID: 112905 Summary: [missed optimization] fail to reduce a loop that increments two locations of the same buffer Product: gcc Version: 13.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: AceSrc at outlook dot com Target Milestone: --- Previously, I posted a question in Stackoverflow about loop optimization (https://stackoverflow.com/questions/77618348/why-does-gcc-fail-to-reduce-a-loop-that-increments-two-locations-of-the-same-buf), and the answer suggests me to post a [missed optimization] report here. The question is about GCC does not optimize the following C++ codes into `counter[A] += n; counter[B] += n;`. In contrast, GCC optimizes the loop into `counter[A] += n` if `++counter[B]` is deleted. ``` unsigned int getid(); void foo(unsigned int *counter, unsigned int n) { unsigned int A = getid(); unsigned int B = getid(); for (unsigned int i = 0; i < n; i++) { ++counter[A]; ++counter[B]; } } ``` LLVM has the same behavior.