<div dir="ltr">Hello,<div><br></div><div>I am currently in the process of understanding what are the loop-invariant code motion (LICM) capabilities of the LLVM optimizer. Right now I am working with a very simple example:</div><div><br></div><div><div>int f() {</div><div>  int a = 0;</div><div>  int x = 5;</div><div>  for (int i = 0; i < 10; i++) {</div><div>    a += x*x;</div><div>  }</div><div>  return a;</div><div>}</div></div><div><br></div><div>My intuition would be that applying the loop-invariant code motion pass alone to this piece of code would store x*x in a temp and then use that in the loop, instead of computing x*x for every iteration. However, this doesn't happen for unoptimized clang-generated code (attached), and after inspecting the LICM implementation and adding debug prints I managed to understand why. </div><div><br></div><div>Of course, in this simple example the entire result can be precomputed using constant propagation and similar techniques - which happens at any level of optimization with clang. However, I was hoping that I could still apply LICM individually and see its transformations in action.</div><div><br></div><div>My question is thus: Does LICM assume the IR it operates on has already been transformed by other passes (apart from the ones listed as dependencies)? And if yes, is there a place where I can find information on the preferred pass scheduling for this case? I tried searching but didn't manage to find anything.</div><div><br></div><div>Thank you in advance,</div><div>Paul </div><div><br></div></div>