コンパイラによると思いますが、A,B,C,Dが全てint型の変数であると仮定すると、gcc4.8.1では最適化オプションなし、-O2ともに(2)相当になりました。
実行したコマンド
コード:
YUKI.N>gcc -S -o test.s test.c
YUKI.N>gcc -S -O2 -o test_o2.s test.c
test.c
コード:
int A,B,C,D;
void test1(void) {
A=B=C=D=100;
}
void test2(void) {
A=100;
B=100;
C=100;
D=100;
}
void test3(void) {
D = 100;
C = D;
B = C;
A = B;
}
test.s
コード:
.file "test.c"
.comm _A, 4, 2
.comm _B, 4, 2
.comm _C, 4, 2
.comm _D, 4, 2
.text
.globl _test1
.def _test1; .scl 2; .type 32; .endef
_test1:
LFB0:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
movl $100, _D
movl _D, %eax
movl %eax, _C
movl _C, %eax
movl %eax, _B
movl _B, %eax
movl %eax, _A
popl %ebp
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE0:
.globl _test2
.def _test2; .scl 2; .type 32; .endef
_test2:
LFB1:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
movl $100, _A
movl $100, _B
movl $100, _C
movl $100, _D
popl %ebp
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE1:
.globl _test3
.def _test3; .scl 2; .type 32; .endef
_test3:
LFB2:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
movl $100, _D
movl _D, %eax
movl %eax, _C
movl _C, %eax
movl %eax, _B
movl _B, %eax
movl %eax, _A
popl %ebp
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE2:
.ident "GCC: (GNU) 4.8.1"
test_o2.s
コード:
.file "test.c"
.text
.p2align 4,,15
.globl _test1
.def _test1; .scl 2; .type 32; .endef
_test1:
LFB0:
.cfi_startproc
movl $100, _D
movl $100, _C
movl $100, _B
movl $100, _A
ret
.cfi_endproc
LFE0:
.p2align 4,,15
.globl _test2
.def _test2; .scl 2; .type 32; .endef
_test2:
LFB1:
.cfi_startproc
movl $100, _A
movl $100, _B
movl $100, _C
movl $100, _D
ret
.cfi_endproc
LFE1:
.p2align 4,,15
.globl _test3
.def _test3; .scl 2; .type 32; .endef
_test3:
LFB2:
.cfi_startproc
movl $100, _D
movl $100, _C
movl $100, _B
movl $100, _A
ret
.cfi_endproc
LFE2:
.comm _D, 4, 2
.comm _C, 4, 2
.comm _B, 4, 2
.comm _A, 4, 2
.ident "GCC: (GNU) 4.8.1"
みどり さんが書きました:また順番にD,C,Bをアクセスしなければならないので、その内に他のthreadにD,C,Bのどれかを別の値で更新しちゃう可能性もあるので、
データの整合性問題が起きます。
ロックなど、他の対策を考えてください。