Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

clang -O3 on linux amd64. I'm told that aliasing is unrelated to equality, and I guess the trick is that realloc's returned pointer is attributed noalias. I get assembly that does the check, does the stores, but omits the loads:

    cmp    rbx, rax
    jne    .LBB0_2
    mov    dword ptr [rax], 2
    mov    dword ptr [rbx], 1
    mov    edi, .L.str
    mov    esi, 1
    mov    edx, 2
    xor    eax, eax
    call    printf
  .LBB0_2:


Omitting the loads doesn't mean it thinks the pointer is invalid, it just means it thinks the pointer doesn't alias. If it doesn't alias, and it's not volatile, then it can assume that what it just stored is what it would get by loading, so it can skip the load.

I guess the only difference between what you described and what I'm seeing is my clang recognizes that since p == q, then a store to one will overwrite the store to the other, and therefore it can skip storing the 1 and it can assuming reading both will return 2.




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: