<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Aug 11, 2014 at 11:44 PM, Kevin Qin <span dir="ltr"><<a href="mailto:kevinqindev@gmail.com" target="_blank">kevinqindev@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi all,<div><br></div><div>Thanks for you paying time to look at this issue. I'm not an expert for C/C++ language, so I can just post some experiment results from LLVM and GCC. </div>
<div><br></div><div>
If we make minor changes to the test, gcc may give different results.</div><div><br></div><div><div style="font-family:arial,sans-serif;font-size:14.166666030883789px"><div class=""><div>#include <stdio.h></div><div>
struct heap {int index; int b;};</div>
<div>struct heap **ptr;</div><div>int aa;</div><div><br></div><div>int main() {</div><div>  struct heap element;<br></div><div>  struct heap *array[2];</div><div>  array[0] = (struct heap *)&aa;</div></div><div>  array[1] = &element;</div>

<div>  ptr = array;</div><div>  aa = 1;</div><div>  int i;</div><div class=""><div>  for (i =0; i< 2; i++) {</div></div><div class=""><div>    printf("i is %d, aa is %d\n", i, aa);</div></div><div>    array[i]->index = 0;  // we replace ptr to array here. so no global lvalue is used.</div>

<div>  }</div><div>  return 0;</div><div>}</div></div><div style="font-family:arial,sans-serif;font-size:14.166666030883789px"><br></div><div style="font-family:arial,sans-serif;font-size:14.166666030883789px">Result didn't get changed,</div>

<div style="font-family:arial,sans-serif;font-size:14.166666030883789px"><br></div><div style="font-family:arial,sans-serif;font-size:14.166666030883789px">$gcc test.c -O0<br></div><div style="font-family:arial,sans-serif;font-size:14.166666030883789px">

$./a.out</div><div style="font-family:arial,sans-serif;font-size:14.166666030883789px"><div>i is 0, aa is 1</div><div>i is 1, aa is 0</div></div><div style="font-family:arial,sans-serif;font-size:14.166666030883789px"><br>

</div><div style="font-family:arial,sans-serif;font-size:14.166666030883789px"><div>$gcc test.c -O2<br></div><div>$./a.out</div><div><div>i is 0, aa is 1</div><div>i is 1, aa is 1</div></div><div><br></div><div>But if we change a bit more, like</div>

<div><br></div><div><div class=""><div>#include <stdio.h></div><div>struct heap {int index; int b;};</div><div>struct heap **ptr;</div><div>int aa;</div><div><br></div><div>int main() {</div><div>  struct heap element;<br>
</div><div>
  struct heap *array[2];</div><div>  array[0] = (struct heap *)&aa;</div></div><div>  array[1] = &element;</div><div>  //ptr = array; // remove this assignment as well.</div><div>  aa = 1;</div><div>  int i;</div>
<div class=""><div>  for (i =0; i< 2; i++) {</div>
</div><div class=""><div>    printf("i is %d, aa is %d\n", i, aa);</div></div><div>    array[i]->index = 0;  // we replace ptr to array here. so no global lvalue is used.</div><div>  }</div><div>  return 0;</div>
<div>}</div></div><div>
<br></div><div><div>Result changed to be the same as LLVM.</div><div><br></div><div>$gcc test.c -O0<br></div><div>$./a.out</div><div><div>i is 0, aa is 1</div><div>i is 1, aa is 0</div></div><div><br></div><div><div>$gcc test.c -O2<br>

</div><div>$./a.out</div><div><div>i is 0, aa is 1</div><div>i is 1, aa is 0</div></div></div></div><div><br></div><div>I don't know why a assignement statment to a unrelated global pointer will affect gcc's aliasing work,</div>
</div></div></div></blockquote><div><br></div><div>Because it blocks the load elimination/copy propagation.  With that pointer assignment there, GCC sees it as a global aliasing the same memory location as the array, and that global escapes the function.  Because of that, it no longer believes it knows what happens to the memory once the printf call happens (since it's really a call to printf_chk, and because of the way glibc works, printf is not a readonly functiojn)</div>
<div><br></div><div><br></div></div></div></div>