https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82689
Bug ID: 82689
Summary: merging writes of different types to the same location
Product: gcc
Version: 7.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: vanyacpp at gmail dot com
Target Milestone: ---
Consider the following code:
#include <cstdint>
union x
{
std::int64_t a;
std::uint64_t b;
};
void f(x& xx, bool flag, std::int64_t value)
{
if (flag)
xx.a = value;
else
xx.b = value;
}
The function f is written with standard compliance in mind so it tries to write
only that member that will be lately used/read.
The hope is that the compiler is able to optimize the redundant if.
And most compilers (clang, MSVC, icc) are able to optimize this function to a
single store instruction.
GCC optimized it to a single store (to some extend). Here what it generated:
f:
test sil, sil
mov QWORD PTR [rdi], rdx
jne .L5
rep ret
.L5:
rep ret
The expected behavior is that the function is optimized to a single store by
GCC too.