[llvm] [LSR] Add unequal cost eval for dropping solutions (PR #178039)

Michael Berg via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 18 12:50:55 PST 2026


================
@@ -481,6 +477,8 @@ class RISCVTTIImpl final : public BasicTTIImplBase<RISCVTTIImpl> {
     llvm_unreachable("unknown register class");
   }
 
+  bool canMacroFuseCmp() const override;
----------------
mcberg2021 wrote:

> This comment is coming mostly from memory, and memory may be faulty. Treat this as something to investigate, not a definitely true statement.
> 
> I remember looking at this before, and getting stuck with a mix of profitable and unprofitable changes. Glancing through the code a bit, I think the issue I ran into was costing for the end value contributing to the solution cost. X86, the primary user of this feature, allows an immediate to be encoded into the compare, but RISC-V does not (in the base ISA without the recent scalar efficiency proposals). This results in the costing implicitly ignoring the cost of materializing the loop invariant end value. This is "close enough" on x86, but not for RISC-V. We might need to use (or revise) one of the APIs for checking if a immediate is legal on a compare, and then adjust the cost if not.

I think part of what you are referring to is handled in loop term folding, for which we have some changes to handle cases like the one mentioned above (loop-strength-reduce-loop-invar.ll).  That will come in a subsequent patch though.  It doesn't change costing, but does handle some of the folding cases for immediates through the compare and iv with the cmp/branch fuse change.

https://github.com/llvm/llvm-project/pull/178039


More information about the llvm-commits mailing list