[llvm] [LSR] Deduplicate cast instructions across ICmpZero fixups (PR #201970)
Timur Golubovich via llvm-commits
llvm-commits at lists.llvm.org
Sat Jun 6 12:46:00 PDT 2026
timurgol007 wrote:
> The logic itself looks correct to me, but I want to double check that we're fixing the correct problem here. Assuming that the test case is somewhat representative of what you're interested in, the code LSR generates there is very weird: We have an integer IV, and then we have comparisons that are performed on ptr by casting _both sides_ of the comparison from integer. That seems... pointless? We generally really don't want LLVM to insert inttoptr/ptrtoint instructions for provenance reasons, and their use here seems entirely unnecessary. Would your motivation for this change also be addressed by generating these directly as integer comparisons instead, avoiding the need for a cast entirely?
Hi! Thanks for review at first. You are right that emitting the comparison directly as `icmp eq i64` avoiding the inttoptr entirely would be the best solution. I think that is a worthwhile follow-up investigation - it would require teaching the Expand() path to adjust the icmp operand type rather than blindly casting the scaled value to match the original.
Nevertheless getOrCreateCast also serves the RewriteForPHI and Rewrite call sites where casts can be genuinely necessary (like trunc when LSR widens a narrow IV). So this patch addresses the immediate duplication in what LSR currently produces, while the deeper change of avoiding unnecessary inttoptr can come as a separate improvement.
https://github.com/llvm/llvm-project/pull/201970
More information about the llvm-commits
mailing list