This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug middle-end/71224] Looping over local copy of aggregate invokes undefined behavior -Waggressive-loop-optimizations
- From: "ibuclaw at gdcproject dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Mon, 23 May 2016 07:14:57 +0000
- Subject: [Bug middle-end/71224] Looping over local copy of aggregate invokes undefined behavior -Waggressive-loop-optimizations
- Auto-submitted: auto-generated
- References: <bug-71224-4 at http dot gcc dot gnu dot org/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71224
--- Comment #5 from Iain Buclaw <ibuclaw at gdcproject dot org> ---
And thinking more about it, I don't think it even explains why this triggers
the warning:
---
if (nlength < pthis->length)
{
Array aggr = {pthis->length - nlength, pthis->ptr + nlength};
for (size_t key = 0; key < aggr.length; key++)
aggr.ptr[key] = 0;
}
---
But this works just fine.
---
if (nlength < pthis->length)
{
if (pthis->length >= nlength)
__builtin_unreachable();
Array aggr = {pthis->length - nlength, pthis->ptr + nlength};
for (size_t key = 0; key < aggr.length; key++)
aggr.ptr[key] = 0;
}
---
Surely the optimizer must be able to deduce that if the first condition is
true, then the second condition must always be a given?