我看不出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