https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100801
Bug ID: 100801 Summary: Aggressive loop optimizations cause incorrect warning Product: gcc Version: 11.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: ipa Assignee: unassigned at gcc dot gnu.org Reporter: jl_gccbugs at conductive dot de CC: marxin at gcc dot gnu.org Target Milestone: --- The following warning is triggered > $ gcc-11 -c constproploopopt.c -O2 -Wall -mavx -g > constproploopopt.c: In function ‘test’: > constproploopopt.c:22:18: warning: iteration 4611686018427387903 invokes > undefined behavior [-Waggressive-loop-optimizations] > 22 | dest[i] = src[i]; > | ^ > constproploopopt.c:21:12: note: within this loop > 21 | for (; i < count; ++i) { // handle residual elements > | ~~^~~~~~~ by this (minimal) code: > #include <stdint.h> > #include <stdio.h> > #if defined(_MSC_VER) > #include <intrin.h> > #else > #include <x86intrin.h> > #endif > > void copy_32_unaligned(uint32_t* dest, const uint32_t* src, size_t count) { > // invariant/nop > __m128i shufmask = _mm_set_epi8(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, > 3, 2, 1, 0); > > size_t i; > for (i = 0; i + 4 <= count; i += 4) { > __m128i input = _mm_loadu_si128((const __m128i*)(&src[i])); > __m128i output = input; > // no warning without the shuffle: > output = _mm_shuffle_epi8(input, shufmask); > _mm_storeu_si128((__m128i*)(&dest[i]), output); > } > for (; i < count; ++i) { // handle residual elements > dest[i] = src[i]; > } > } > > void test(uint32_t* buf1, uint32_t* buf2) { > copy_32_unaligned(buf2, buf1, > // multiples of 4 and greater or equal then 12 trigger > it: > 12); > } >From objdump output I believe the generated code is correct though. The warning seems to be incorrect in this context, especially since the "residual" loop should be skipped for count=n*4 anyways.