【发布时间】:2020-04-14 22:43:21
【问题描述】:
我想知道 GCC 是否会使用我创建的结构过度对齐的知识,并在生成比较 asm 时将其简单地视为一个整数。
它没有。 我想知道这是否是由于 ABI 问题,我错过了其他一些因素,还是真的缺少优化?
代码:
struct S{
short a,b;
bool operator == (const S&) const = default;
};
struct alignas(alignof(int)) S2 {
short a,b;
bool operator == (const S2&) const = default;
};
bool f(const S& s){
constinit static S c{11,22};
return c==s;
}
bool f2(const S2& s2){
constinit static S2 c2{11,22};
return c2==s2;
}
static_assert(alignof(S)==2);
static_assert(alignof(S2)==4);
【问题讨论】:
-
我认为这与对齐没有任何关系,特别是因为您链接了 x86 版本。
-
有趣的事实:
return memcmp(this, &s, sizeof(s)) == 0;被编译为单个整数大小的比较,但这似乎又不使用常量。
标签: c++ assembly optimization language-lawyer alignas