叮当优化:&& VS&:优化与非优化的(对于某些值)
clang -o3
优化此代码:
_Bool f1(char x)
{
_Bool b1 = x == 4;
_Bool b2 = x & 3;
return b1 & b2;
}
to:to:
f1:
xor eax, eax
ret
但是,clang -o3
不优化此代码:
_Bool f1(char x)
{
_Bool b1 = x == 2;
_Bool b2 = x & 1;
return b1 & b2;
}
f1:
cmp dil, 2
sete al
and al, dil
ret
为什么?
注意:&
b1& B2
有意使用。如果使用&&
,则clang -o3
将其优化为:
f1:
xor eax, eax
ret
如何解释?
clang -O3
optimizes this code:
_Bool f1(char x)
{
_Bool b1 = x == 4;
_Bool b2 = x & 3;
return b1 & b2;
}
to:
f1:
xor eax, eax
ret
However, clang -O3
does not optimize this code:
_Bool f1(char x)
{
_Bool b1 = x == 2;
_Bool b2 = x & 1;
return b1 & b2;
}
f1:
cmp dil, 2
sete al
and al, dil
ret
Why?
Note: the &
b1 & b2
is used intentionally. If &&
is used, then clang -O3
optimizes it to:
f1:
xor eax, eax
ret
How it can be explained?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
效率低下的代码生成(由于“丢失了noffing for bitwise逻辑的狭窄变换”)。
Inefficient code generation (due to "missing narrowing transforms for bitwise logic").