<div dir="ltr"><div>Hi all,</div><div><br></div><div>We noticed a lot of unnecessary ptrtoint instructions that stand in way of some of our optimizations; the code pattern looks like this:</div><div><br></div><div>bb1:</div><div>  %int1 = ptrtoint %struct.s* %ptr1 to i64</div><div><br></div><div>bb2:</div><div>  %int2 = ptrtoint %struct.s* %ptr2 to i64</div><div><br></div><div>%bb3:</div><div>  %phi.node = phi i64 [ %int1, %bb1 ], [%int2, %bb2 ]</div><div>  %ptr = inttoptr i64 %phi.node to %struct.s*</div><div><br></div><div>In short, the pattern above arises due to:</div><div>1. InstCombine introducing a bitcast while performing a canonicalization in combineLoadToOperationType() [1]</div><div>2. GVN performing "load coercion" and replacing a load with a ptrtoint (ptrtoint is due to the bitcast)<br></div><div>3. SROA replacing store- and load-instructions with phi nodes.</div><div><br></div><div>The question is: is it a good idea to clean ptrtoint/inttoptr instructions inside SROA or should it be done higher in the pass pipeline? More details below.</div><div><br></div><div>The canonicalization in instruction combining is the root cause of the problem, more information about the actual transformation and motivation behind it can be found in this RFC [2]. Based on our experiments, getting rid of the canonicalization reduces the number of ptrtoint instructions by 16% and inttoptr instructions by 25%. However, it seems that overall, LLVM benefits from the canonicalization and, at least back in 2015, many people supported adding it to LLVM. So, I would like to find a way to keep it while removing the ptrtoint-s that are unnecessary, and doing clean-up in SROA is a straightforward way to achieve that.<br></div><div><br></div><div>What does everybody think?</div><div><br></div><div>Thanks,</div><div>Alexey<br></div><div><br></div><div>[1] combineLoadToOperationType() <a href="https://github.com/llvm/llvm-project/blob/master/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp#L556">https://github.com/llvm/llvm-project/blob/master/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp#L556</a><br>[2] [LLVMdev] RFC: Missing canonicalization in LLVM <a href="http://lists.llvm.org/pipermail/llvm-dev/2015-January/080956.html">http://lists.llvm.org/pipermail/llvm-dev/2015-January/080956.html</a></div><div><br></div><div><br></div><div><br><span class="gmail-pl-en"></span></div></div>