[llvm-bugs] [Bug 42847] New: Ternary form is optimized better than corresponding if / if else
via llvm-bugs
llvm-bugs at lists.llvm.org
Wed Jul 31 10:52:04 PDT 2019
https://bugs.llvm.org/show_bug.cgi?id=42847
Bug ID: 42847
Summary: Ternary form is optimized better than corresponding if
/ if else
Product: new-bugs
Version: trunk
Hardware: PC
OS: Linux
Status: NEW
Severity: enhancement
Priority: P
Component: new bugs
Assignee: unassignedbugs at nondot.org
Reporter: jameshamm1995 at gmail.com
CC: htmldeveloper at gmail.com, llvm-bugs at lists.llvm.org
Compiling a function written as a ternary expression gave better assembly than
an equivalent function written with a single if statement and another with an
if else.
Example code (https://godbolt.org/z/vuDVaP) compiled with clang (trunk,
currently 10.0.0) -Ofast -march=skylake
// Case 1, let a != b => (true || a != b + 1) => (true)
// Case 2: let !(a != b) => (a == b), (false || a != a + 1) => (true)
// So the condition is a tautology.
int f(int a, int b) {
return (a != b || a != b + 1) ? 200 : 500;
}
int g(int a, int b) {
if (a != b || a != b + 1) {
return 200;
}
return 500;
}
int h(int a, int b) {
if (a != b || a != b + 1) {
return 200;
} else {
return 500;
}
}
Produces the following assembly
f(int, int): # @f(int, int)
mov eax, 200
ret
g(int, int): # @g(int, int)
mov eax, edi
xor eax, esi
inc esi
xor esi, edi
or esi, eax
mov ecx, 500
mov eax, 200
cmove eax, ecx
ret
(h is omitted as it is identical to g)
--
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/20190731/942d7eb1/attachment.html>
More information about the llvm-bugs
mailing list