https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80410
Bug ID: 80410
Summary: Improve merging of identical branches
Product: gcc
Version: 7.0.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: pascal_cuoq at hotmail dot com
Target Milestone: ---
Consider the following code:
va_list ap;
void *result;
va_start(ap, ptr_type);
switch (ptr_type) {
case 0: result = va_arg(ap, int*); break;
case 1: result = va_arg(ap, char*); break;
case 2: result = va_arg(ap, short*); break;
default: result = va_arg(ap, void*); break;
}
va_end(ap);
GCC correctly recognizes that the assembly code for the cases 0, 1, 2 is
identical, which is the difficult part (the source code for the branches is
different and cannot be merged by the programmer unless
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80409 is resolved). At the time of
writing, the Compiler Explorer trunk version produces:
movq %rax, -56(%rsp)
je .L15
cmpl $2, %edi
je .L15
testl %edi, %edi
je .L16
.L15:
movq -56(%rsp), %rax
.L16:
(Compiler Explorer link: https://godbolt.org/g/0NvnSX )
It would be nice if GCC was able to omit all the conditional branches, since
they all lead to doing the same thing at the assembly level.