叮当优化:&& VS&:优化与非优化的(对于某些值)

发布于 2025-02-12 03:05:21 字数 717 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清君侧 2025-02-19 03:05:21

为什么?

效率低下的代码生成(由于“丢失了noffing for bitwise逻辑的狭窄变换”)。

Why?

Inefficient code generation (due to "missing narrowing transforms for bitwise logic").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文