<html>
<head>
<base href="https://llvm.org/bugs/" />
</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 --- - logically identical functions do not result in the same IR"
href="https://llvm.org/bugs/show_bug.cgi?id=24766">24766</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>logically identical functions do not result in the same IR
</td>
</tr>
<tr>
<th>Product</th>
<td>libraries
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>All
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>normal
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>Transformation Utilities
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>spatel+llvm@rotateright.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org
</td>
</tr>
<tr>
<th>Classification</th>
<td>Unclassified
</td>
</tr></table>
<p>
<div>
<pre>Based on the discussions in <a class="bz_bug_link
bz_status_NEW "
title="NEW --- - Using bitwise-or versus logical-or on boolean values produces different assembly at -O3"
href="show_bug.cgi?id=22723">bug 22723</a> and <a class="bz_bug_link
bz_status_RESOLVED bz_closed"
title="RESOLVED INVALID - bad generated code for conditionals connected by (&)"
href="show_bug.cgi?id=23827">bug 23827</a>, logically identical
functions should result in the same optimized IR and ultimately the same
machine code regardless of how they're coded in the higher-level language.
The following two C functions are logically equivalent for all possible inputs:
#include <stdbool.h>
bool switchy(char x1, char x2, char condition) {
bool conditionMet = false;
switch (condition) {
case 0: conditionMet = (x1 == x2);
break;
case 1: conditionMet = (x1 <= x2);
break;
}
return conditionMet;
}
bool sneaky(char x1, char x2, char condition) {
bool equals = (x1 == x2);
bool lessThanOrEquals = (x1 <= x2);
bool conditionIsEquals = (0 == condition);
bool conditionIsLessThanOrEquals = (1 == condition);
return (equals & conditionIsEquals) | (lessThanOrEquals &
conditionIsLessThanOrEquals);
}
The optimized IR is quite different though:
define i1 @switchy(i8 %x1, i8 %x2, i8 %condition) {
entry:
%conv = sext i8 %condition to i32
switch i32 %conv, label %epilog [
i32 0, label %sw1
i32 1, label %sw2
]
sw1:
%cmp1 = icmp eq i8 %x1, %x2
%frombool1 = zext i1 %cmp1 to i8
br label %epilog
sw2:
%cmp2 = icmp sle i8 %x1, %x2
%frombool2 = zext i1 %cmp2 to i8
br label %epilog
epilog:
%conditionMet = phi i8 [ 0, %entry ], [ %frombool2, %sw2 ], [ %frombool1,
%sw1 ]
%tobool = icmp ne i8 %conditionMet, 0
ret i1 %tobool
}
define i1 @sneaky(i8 %x1, i8 %x2, i8 %condition) {
%cmp1 = icmp eq i8 %x1, %x2
%cmp2 = icmp sle i8 %x1, %x2
%cmp3 = icmp eq i8 %condition, 0
%cmp4 = icmp eq i8 %condition, 1
%and1 = and i1 %cmp1, %cmp3
%and2 = and i1 %cmp2, %cmp4
%or = or i1 %and1, %and2
ret i1 %or
}
---------------------------------------------------------------------------
Which of these, if any, is the optimal/canonical form? And how do we get the
one(s) that aren't into that form?</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>