[llvm-bugs] [Bug 37368] New: Missed small switch optimizations
via llvm-bugs
llvm-bugs at lists.llvm.org
Tue May 8 07:59:02 PDT 2018
https://bugs.llvm.org/show_bug.cgi?id=37368
Bug ID: 37368
Summary: Missed small switch optimizations
Product: libraries
Version: trunk
Hardware: PC
OS: Linux
Status: NEW
Severity: enhancement
Priority: P
Component: Scalar Optimizations
Assignee: unassignedbugs at nondot.org
Reporter: david.bolvansky at gmail.com
CC: llvm-bugs at lists.llvm.org
Hello,
When we have small "switch"-es with two or three cases, clang could generate a
smarter code.
Example:
char * a(int e) {
switch (e) {
case 0:
return "0";
default:
return "default";
}
}
Clang's output:
a: # @a
push rbp
mov rbp, rsp
mov dword ptr [rbp - 12], edi
mov edi, dword ptr [rbp - 12]
test edi, edi
jne .LBB0_2
jmp .LBB0_1
.LBB0_1:
movabs rax, offset .L.str
mov qword ptr [rbp - 8], rax
jmp .LBB0_3
.LBB0_2:
movabs rax, offset .L.str.1
mov qword ptr [rbp - 8], rax
.LBB0_3:
mov rax, qword ptr [rbp - 8]
pop rbp
ret
Better code (GCC does it):
a:
test edi, edi
mov edx, OFFSET FLAT:.LC1
mov eax, OFFSET FLAT:.LC0
cmovne rax, rdx
ret
Also for this example:
char * a(int e) {
switch (e) {
case 0:
return "0";
case 1:
return "1";
default:
return "default";
}
}
A better code can be generated like:
a:
mov eax, OFFSET FLAT:.LC1
test edi, edi
je .L1
cmp edi, 1
mov eax, OFFSET FLAT:.LC2
mov edx, OFFSET FLAT:.LC0
cmovne rax, rdx
.L1:
ret
Thanks
--
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180508/5fe435b3/attachment.html>
More information about the llvm-bugs
mailing list