<div class="__aliyun_email_body_block"><div  style="clear:both;">Hi,</div><div  style="clear:both;"><br ></div><div  style="clear:both;">   The optimization should be done at InstructionCombine pass. See: https://godbolt.org/z/zGvrMerKv. Previous cleaning passes are necessary.</div><div  style="clear:both;"><br ></div><div  style="clear:both;">   But I feel this optimization is a little bit aggresive. Since it changes the behavior of the program. The program above might hit a memory leak while the optimized one would not.</div><div  style="clear:both;"><br ></div><div  style="clear:both;">   As a reference, GCC wouldn't do this under O2: https://godbolt.org/z/5z1GWMWPj. It would only do this under O3.</div><div  style="clear:both;"><br ></div><div  style="clear:both;">Thanks,</div><div  style="clear:both;">Chuanqi</div><blockquote  style="margin-right:0;margin-top:0;margin-bottom:0;"><div  style="clear:both;"><span  style="font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;color:#000000;">------------------------------------------------------------------</span></div><div  style="clear:both;"><span  style="font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;color:#000000;">From:llvm-dev <llvm-dev@lists.llvm.org></span></div><div  style="clear:both;"><span  style="font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;color:#000000;">Send Time:2022年1月17日(星期一) 16:08</span></div><div  style="clear:both;"><span  style="font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;color:#000000;">To:llvm-dev <llvm-dev@lists.llvm.org></span></div><div  style="clear:both;"><span  style="font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;color:#000000;">Subject:[llvm-dev] Which optimization pass deals with heap values?</span></div><div  style="clear:both;"><span  style="font-family:Tahoma,Arial,STHeiti,SimSun;font-size:14.0px;color:#000000;"><br ></span></div><div >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></blockquote><div ><br ></div></div>