<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 --- - LLVM O2: LLVM opt -O2 did wrong optimization happened on Power / x86."
href="https://llvm.org/bugs/show_bug.cgi?id=29118">29118</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>LLVM O2: LLVM opt -O2 did wrong optimization happened on Power / x86.
</td>
</tr>
<tr>
<th>Product</th>
<td>new-bugs
</td>
</tr>
<tr>
<th>Version</th>
<td>unspecified
</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>normal
</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>bluechristlove@163.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>Considered following case:
[code]
template <class _Tp, int _Size>
struct __attribute__ ((__type_visibility__("default"))) array
{
typedef _Tp value_type;
value_type __elems_[_Size > 0 ? _Size : 1];
};
template <class _T1, class _T2 = _T1>
struct __equal_to
{
__attribute__ ((__visibility__("hidden"), __always_inline__)) bool
operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
__attribute__ ((__visibility__("hidden"), __always_inline__)) bool
operator()(const _T1& __x, const _T2& __y) const {return __x == __y;}
__attribute__ ((__visibility__("hidden"), __always_inline__)) bool
operator()(const _T2& __x, const _T1& __y) const {return __x == __y;}
__attribute__ ((__visibility__("hidden"), __always_inline__)) bool
operator()(const _T2& __x, const _T2& __y) const {return __x == __y;}
};
template <class _T1>
struct __equal_to<_T1, _T1>
{
__attribute__ ((__visibility__("hidden"), __always_inline__))
bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
};
template <class _T1>
struct __equal_to<const _T1, _T1>
{
__attribute__ ((__visibility__("hidden"), __always_inline__))
bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
};
template <class _T1>
struct __equal_to<_T1, const _T1>
{
__attribute__ ((__visibility__("hidden"), __always_inline__))
bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
};
template <class _InputIterator1, class _InputIterator2, class _BinaryPredicate>
inline __attribute__ ((__visibility__("hidden"), __always_inline__))
bool
equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2
__first2, _BinaryPredicate __pred)
{
for (; __first1 != __last1; ++__first1, (void) ++__first2)
if (!__pred(*__first1, *__first2))
return false;
return true;
}
template <class _InputIterator1, class _InputIterator2>
inline __attribute__ ((__visibility__("hidden"), __always_inline__))
bool
equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2
__first2)
{
typedef decltype(*__first1) __v1;
typedef decltype(*__first2) __v2;
return equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
}
template <class _Tp, int _Size>
inline __attribute__ ((__visibility__("hidden"), __always_inline__))
bool
operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
{
return equal(__x.__elems_, __x.__elems_ + _Size, __y.__elems_);
}
int main()
{
typedef array<char, 10> Con;
Con v0;
__builtin_printf("%d\n", v0 == v0);
}
[/code]
use clang trunk -std=c++11 -O2
wuzhao@lep82446v:~/wk.dev$ clang++ --version
clang version 4.0.0 (trunk)
Target: powerpc64le-unknown-linux-gnu
Thread model: posix
wuzhao@lep82446v:~/wk.dev$ clang++ x.C -std=c++11 -O0
wuzhao@lep82446v:~/wk.dev$ ./a.out
1
but on -O2:
wuzhao@lep82446v:~/wk.dev$ clang++ x.C -std=c++11 -O2
wuzhao@lep82446v:~/wk.dev$ ./a.out
0
If we use O0 LLVM IR and use llvm-opt to do O2 optimization.
we can find like this:
block-frequency: main
=====================
reverse-post-order-traversal
- 0: _ZeqIcLi10EEbRK5arrayIT_XT0_EES4_.exit
loop-detection
compute-mass-in-function
- node: _ZeqIcLi10EEbRK5arrayIT_XT0_EES4_.exit
=> mass: ffffffffffffffff
float-to-int: min = 1.0, max = 1.0, factor = 8.0
- _ZeqIcLi10EEbRK5arrayIT_XT0_EES4_.exit: float = 1.0, scaled = 8.0, int = 8
block-frequency-info: main
- _ZeqIcLi10EEbRK5arrayIT_XT0_EES4_.exit: float = 1.0, int = 8
INSTCOMBINE ITERATION #1 on main
IC: ADDING: 2 instrs to worklist
IC: Visiting: %call1 = call signext i32 (i8*, ...) @printf(i8* getelementptr
inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 signext 0)
IC: Visiting: ret i32 0
SLP: Analyzing blocks in main.
INSTCOMBINE ITERATION #1 on main
IC: ADDING: 2 instrs to worklist
IC: Visiting: %call1 = call signext i32 (i8*, ...) @printf(i8* getelementptr
inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 signext 0)
IC: Visiting: ret i32 0
INSTCOMBINE ITERATION #1 on main
IC: ADDING: 2 instrs to worklist
IC: Visiting: %call1 = call signext i32 (i8*, ...) @printf(i8* getelementptr
inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 signext 0)
IC: Visiting: ret i32 0
We can find that O2 made the result be 0. But if we use O1:
INSTCOMBINE ITERATION #1 on main
IC: ADDING: 2 instrs to worklist
IC: Visiting: %call1 = call signext i32 (i8*, ...) @printf(i8* getelementptr
inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 signext 1)
IC: Visiting: ret i32 0
INSTCOMBINE ITERATION #1 on main
IC: ADDING: 2 instrs to worklist
IC: Visiting: %call1 = call signext i32 (i8*, ...) @printf(i8* getelementptr
inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 signext 1)
IC: Visiting: ret i32 0
INSTCOMBINE ITERATION #1 on main
IC: ADDING: 2 instrs to worklist
IC: Visiting: %call1 = call signext i32 (i8*, ...) @printf(i8* getelementptr
inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 signext 1)
IC: Visiting: ret i32 0
It is fine.
So, O2 did something wrong. Not only on PowerPC but also on x86.
Addtionally, Clang 3.5 is OK with O2.
Similiar Bugs: <a class="bz_bug_link
bz_status_NEW "
title="NEW --- - -O2: __always_inline__ does not have effect with visibility attribute hidden."
href="show_bug.cgi?id=29085">https://llvm.org/bugs/show_bug.cgi?id=29085</a></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>