<div dir="ltr">Thanks! Yeah, looking over the detailed output, it looks like the most important step was global value numbering, of which it is said<div><br></div><div>> This pass performs global value numbering to eliminate fully and partially redundant instructions. It also performs redundant load elimination.</div><div><br></div><div>So yeah, it did indeed eliminate the redundant load.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Jan 17, 2022 at 9:50 AM Florian Hahn <<a href="mailto:florian_hahn@apple.com">florian_hahn@apple.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;"><br><div><br><blockquote type="cite"><div>On Jan 17, 2022, at 08:07, Russell Wallace via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:</div><br><div><div dir="ltr">All the optimization passes I can find in the documentation, deal with register, stack or global values; I cannot find any mention of anything trying to optimize values on the heap. But this:<div><br></div><div>#include <stdlib.h><br><br>int main(int argc, char **argv) {<br>  int *a = malloc(10 * sizeof(int));<br>  for (int i = 0; i < 10; i++)<br>    a[i] = i;<br>  return a[5];<br>}<br></div><div><br></div><div>compiles to 'ret 5' (as it should). Which pass does that?</div></div>
_____________________________________________<br></div></blockquote></div><br><div><br></div><div>You can use `-mllvm -print-changed` with clang when built with assertions to find this out, e.g. see <a href="https://clang.godbolt.org/z/aM18Tr7Mr" target="_blank">https://clang.godbolt.org/z/aM18Tr7Mr</a></div><div><br></div><div>It will be multiple optimizations working together. There are several passes that optimize memory operation, like DSE, GVN, EarlyCSE +MemorySSA.</div><div><br></div><div><br></div></div></blockquote></div>