我看不出gcc代碼限制指針的任何區別。爲什麼沒有限制指針的效果
file1的
void test (int *a, int *b, int *c)
{
while (*a)
{
*c++ = *a++ + *b++;
}
}
file2的
void test (int *restrict a, int *restrict b, int *restrict c)
{
while (*a)
{
*c++ = *a++ + *b++;
}
}
編譯
gcc -S -std=c99 -masm=intel file1.c
gcc -S -std=c99 -masm=intel file2.c
file1.s和file2.s兩者都是相同的,除了在.file
線,它告訴的文件名:
.file "file1.c"
.text
.globl test
.type test, @function
test:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movq %rdi, -8(%rbp)
movq %rsi, -16(%rbp)
movq %rdx, -24(%rbp)
jmp .L2
.L3:
movq -8(%rbp), %rax
movl (%rax), %edx
movq -16(%rbp), %rax
movl (%rax), %eax
addl %eax, %edx
movq -24(%rbp), %rax
movl %edx, (%rax)
addq $4, -24(%rbp)
addq $4, -8(%rbp)
addq $4, -16(%rbp)
.L2:
movq -8(%rbp), %rax
movl (%rax), %eax
testl %eax, %eax
jne .L3
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size test, .-test
.ident "GCC: (GNU) 4.6.3 20120306 (Red Hat 4.6.3-2)"
.section .note.GNU-stack,"",@progbits
的這兩個代碼從存儲器讀出,然後分配的存儲器位置所指向的a
到b
。在我預計restrict
版本將不會重新讀取a
和b
的地址,並且a
和b
的地址將在寄存器中遞增,並在最後寫入內存。
我在做什麼錯?或者,示例的選擇是否正確?
我試圖與不同的開關-O0
,-O1
,-O2
,-O3
,-Ofast
和-fstrict-aliasing
有用於這兩個文件的相同的相同的結果。
注:的gcc --version =海合會(GCC)4.6.3 20120306(紅帽4.6.3-2)
編輯代碼改變。
明白了,謝謝。 – phoxis