<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">
<div>
<div>On 20 Aug 2013, at 08:23, 王振江 <<a href="mailto:cryst216@gmail.com">cryst216@gmail.com</a>> wrote:</div>
<blockquote type="cite">
<div dir="ltr">
<div><font face="courier new, monospace"><br>
</font></div>
<div><font face="courier new, monospace">A GlobalValue was declared and mapped to the variable p.</font></div>
<div><font face="courier new, monospace">Some LLVM IR instructions were created according to those generated by LLVM from source.</font></div>
<div><font face="courier new, monospace">I.e., load p, load a[1] based on p, load p again, store a[2] based on p, etc.</font></div>
<div><font face="courier new, monospace">The machine code turned out to be slightly optmized, as shown on the left.</font></div>
</div>
</blockquote>
<br>
I suspect this is due to possible aliasing. If p somehow pointed to itself then the store p->a[x] might change the value of of p so p must be reloaded each time. Clang will emit TBAA metadata nodes (<a href="http://llvm.org/docs/LangRef.html#tbaa-metadata">http://llvm.org/docs/LangRef.html#tbaa-metadata</a>)
 that let the optimizers know the load of p can't alias the stores through p since they are have different high-level types. Without the TBAA metadata the optimizers must be conservative.</div>
<div><br>
</div>
<div>
<blockquote type="cite">
<div dir="ltr">
<div><font face="courier new, monospace"><br>
</font></div>
<div><font face="courier new, monospace">Things were getting better after the GlobalVariable of p was set as a constant.</font></div>
<div><font face="courier new, monospace">Redundant Loads of p (line 5, 8 and 11) were removed, and so was line 12 because of line 10.</font></div>
</div>
</blockquote>
<br>
This makes sense - if p is constant no store can possibly change the value of p so it doesn't need to be reloaded.</div>
<div><br>
<blockquote type="cite">
<div dir="ltr">
<div><font face="courier new, monospace">However, I could not make it better any more, although optimal machine code just need those marked with '*'.</font></div>
</div>
</blockquote>
<div>This is strange, I'm not what sure what is going on here - assuming you are running the same passes I'd expect no difference here.</div>
</div>
</body>
</html>