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

FWIW, inspecting the LLVM IR of `clang -O3`, I get the equivalent of the following:

  #include <stdio.h>
  #include <stdlib.h>
  
  int main() {
    int *p = (int*)malloc(sizeof(int));
    int *q = (int*)reallocf(p, sizeof(int));
    if (p == q) {
      *q = 2;
      printf("%d %d\n", 2, 2);
    }
    return 0;
  }
Note how it removed the write to p and removed the read of the pointer value.

Here what it's done is assumed that because p == q, that means they alias, and therefore the write to p will be overwritten by the write to q, and that it doesn't have to read the value again to know what will be printed.

So the optimization here seems to be proceeding under the assumption that realloc() does not necessarily invalidate the pointer. And it behaves the same way with reallocf() as well.



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

Search: