<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 - SimplifyCFGPass hoists unsafe instruction and caused undefined behaviour"
href="https://bugs.llvm.org/show_bug.cgi?id=51360">51360</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>SimplifyCFGPass hoists unsafe instruction and caused undefined behaviour
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>unspecified
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Windows NT
</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>C
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>Zhi.Zhuang@mediatek.com
</td>
</tr>
<tr>
<th>CC</th>
<td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
</td>
</tr></table>
<p>
<div>
<pre>Reduced test case:
#include <stdint.h>
int32_t foo(int32_t a, int32_t b) {
return (a>0 && b>0 && a>INT32_MAX-b) || (a<0 && b<0 && a<INT32_MAX-b) ? 0 :
(a+b);
}
and using sha <2e75986a21e5>, command is "clang -O3 -S -mllvm -print-after-all
test.c"
Before second SimplifyCFGPass, the IR was like follows:
2:
%3 = icmp sgt i32 %0, 0
%4 = icmp sgt i32 %1, 0
%5 = select i1 %3, i1 %4, i1 false
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = sub nsw i32 2147483647, %1
The sub nsw will only be executed when %1 was positive since that was the
condition when %6 was branched, so sub will not overflow and nsw keyword makes
sense.
But after second SimplifyCFGPass, that sub was hoisted to first block:
2:
%3 = icmp sgt i32 %0, 0
%4 = icmp sgt i32 %1, 0
%5 = select i1 %3, i1 %4, i1 false
%6 = sub nsw i32 2147483647, %1
%7 = icmp slt i32 %6, %0
%8 = select i1 %5, i1 %7, i1 false
br i1 %8, label %18, label %9
This %6=sub nsw does not makes sense anymore since %1 maybe negative, and this
block branching using the result of %6 and may cause undefined behavior</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>