To understand strict-aliasing and the usage of restrict keyword, I am trying the program shown below. In this case, same memory location is referenced by two pointers. Hence without user explicitly telling, compiler can't do any optimization (i.e. without using restrict keyword). Hence my understanding was that without restrict keyword, output of the program will be 40 and with 'restrict' output will be 30 (as 'a' does not need to be read from memory after "*b += *a"). However even with restrict, output is 40. why the optimization is not happening?
I am following "What is the strict aliasing rule?" for understanding.
#include <stdio.h>
void merge_two_ints(int * restrict a, int * restrict b) {
*b += *a;
*a += *b;
}
int
main(void)
{
int x = 10;
int *a = &x;
int *b = &x;
merge_two_ints(a, b);
printf("%d\n", x);
return 0;
}
bash-3.2$ gcc -Wall -O3 -fstrict-aliasing -std=c99 strict_alias2.c
bash-3.2$ ./a.out 40