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: unassignedb...@nondot.org
Reporter: jameshamm1...@gmail.com
CC: htmldevelo...@gmail.com, llvm-bugs@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.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs