<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - Ternary form is optimized better than corresponding if / if else"
href="https://bugs.llvm.org/show_bug.cgi?id=42847">42847</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Ternary form is optimized better than corresponding if / if else
</td>
</tr>
<tr>
<th>Product</th>
<td>new-bugs
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Linux
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>new bugs
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>jameshamm1995@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre>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 (<a href="https://godbolt.org/z/vuDVaP">https://godbolt.org/z/vuDVaP</a>) 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)</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>