【发布时间】:2020-07-30 04:35:16
【问题描述】:
我在 struct(LLVM 10) 的内存对齐方面得到了有线行为,它与我对内存对齐的学习不匹配。
对于以下c++代码:
struct CC {
char c1 = 'a';
double d1 = 2.0;
int i1 = 12;
bool b1 = true;
int i2 = 13;
bool b2 = true;
} cc1;
int main() {
CC cc2;
}
它会生成像这样的 IR:
%struct.CC = type <{ i8, [7 x i8], double, i32, i8, [3 x i8], i32, i8, [3 x i8] }>
@cc1 = global { i8, double, i32, i8, i32, i8 } { i8 97, double 2.000000e+00, i32 12, i8 1, i32 13, i8 1 }, align 8
define linkonce_odr void @_ZN2CCC2Ev(%struct.CC*) unnamed_addr #1 align 2 {
%2 = alloca %struct.CC*, align 8
store %struct.CC* %0, %struct.CC** %2, align 8
%3 = load %struct.CC*, %struct.CC** %2, align 8
%4 = getelementptr inbounds %struct.CC, %struct.CC* %3, i32 0, i32 0
store i8 97, i8* %4, align 8
%5 = getelementptr inbounds %struct.CC, %struct.CC* %3, i32 0, i32 2
store double 2.000000e+00, double* %5, align 8
%6 = getelementptr inbounds %struct.CC, %struct.CC* %3, i32 0, i32 3
store i32 12, i32* %6, align 8
%7 = getelementptr inbounds %struct.CC, %struct.CC* %3, i32 0, i32 4
store i8 1, i8* %7, align 4
%8 = getelementptr inbounds %struct.CC, %struct.CC* %3, i32 0, i32 6
store i32 13, i32* %8, align 8
%9 = getelementptr inbounds %struct.CC, %struct.CC* %3, i32 0, i32 7
store i8 1, i8* %9, align 4
ret void
}
我的问题:
- 必须
%struct.CC必须添加额外的数据类型([7xi8],[3xi8])才能对齐?还有其他对齐结构类型的方法吗? - 为什么
@cc1不使用%struct.CC? - 为什么
@cc1不为对齐添加额外的数据类型? - 为什么在使用
store时i1对齐8 而不是4?如果对齐 4 怎么办? - 为什么在使用
store时i2对齐8 而不是4?
问题太多了,如果有人能回答其中的一些,非常感谢。
【问题讨论】:
-
如果您需要字节对齐的成员,请使用适当的
#pragma或编译器指令。