仅在 2 的幂上错过了 clang 中的优化
Missed optimization in clang only on powers of two
使用 -Ofast 编译时,clang 正确地推断出以下函数将始终返回 0。
int zero(bool b) {
const int x = 5;
return (x * b) + (-x * b);
}
编译为
zero(bool): # @zero(bool)
xor eax, eax
ret
但是,如果我将常数更改为 2 的任意幂(1 或 0 除外(,则 clang 不再进行相同的扣除
int zero(bool b) {
const int x = 8;
return (x * b) + (-x * b);
}
编译为
zero(bool): # @zero(bool)
mov eax, edi
shl eax, 3
xor dil, 1
movzx ecx, dil
lea eax, [rax + 8*rcx]
add eax, -8
ret
使用编译器资源管理器编译的代码。
如果我将函数参数更改为更大的参数(短、整、长(,则优化正确。
是什么导致了这种奇怪的边缘情况?
这是 Clang 中的一个优化问题,在 10.0.0 版本之后修复。我们可以在版本 9.0.1 中重现此问题。
在编译器资源管理器中比较了版本 9.0.1 和 10.0.0。
相关文章:
- Clang 给了我符号更改的警告,但代码仍然产生正确的输出
- gcc 是否在 2 条短裤的过度对齐结构的比较中错过了优化机会?
- 我的C++合并排序代码不起作用。我在这里错过了什么?
- 我是否为邪恶刽子手的构造函数错过了什么?
- C++:将向量传递给函数,然后在main中调用函数.错过了什么
- 随机访问迭代器:我错过了什么?
- 当编译时已知引用占用结构中的空间时,是否错过了优化?
- std::元组大小,是不是错过了优化?
- 在这个while循环中我错过了什么?
- 编译器错过了无效的构造函数调用,并调用不存在的(或私有的)默认构造函数
- 我是否错过了什么,或者虚拟呼叫的性能并不像人们所说的那样糟糕
- C 中MAP的反向迭代错过了第一个元素
- 仅在 2 的幂上错过了 clang 中的优化
- 我错过了boost :: mpi ::请求?测试似乎改变了状态
- 我在变量上收到 3 个 C4703 错误,我认为我已经正确初始化了,但我不确定我错过了什么
- 如果这不是 boost::lockfree::d etail::freelist 中的错误,我在这里错过了什么
- 尝试使用类,但未打印任何内容.不会生成任何错误.我错过了什么吗?
- 尝试实现通过引用传递的向量以与二叉树一起使用,我错过了什么
- AFAIK,下面的代码不应该编译,但它可以在 clang 和 GCC 中编译。我在这里错过了什么?
- 据我所知,下面的函数不是constexpr,而是用clang和g++编译的代码.我错过了什么