[llvm] 33df888 - [RISCV] Teach RISCVTargetLowering::isFPImmLegal about fli+fneg (#149075)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 22 06:22:31 PDT 2025


Author: Alex Bradbury
Date: 2025-07-22T14:22:26+01:00
New Revision: 33df8882172dc3eb06db9846250de03dd3f7fff3

URL: https://github.com/llvm/llvm-project/commit/33df8882172dc3eb06db9846250de03dd3f7fff3
DIFF: https://github.com/llvm/llvm-project/commit/33df8882172dc3eb06db9846250de03dd3f7fff3.diff

LOG: [RISCV] Teach RISCVTargetLowering::isFPImmLegal about fli+fneg (#149075)

There was a mismatch between isFPImmlegal and the cases that are handled
by lowerConstantFP. isFPImmLegal didn't check for the case where we
support `fli` of a negated constant (and so can lower to fli+fneg). This
has very minimal impact (42 insertion, 47 deletions across an
rv22u64_zfa llvm-test-suite build including SPEC CPU 2017) but is added
here for completeness.

See the PR thread https://github.com/llvm/llvm-project/pull/149075 for furrther discussion about the degree to which isFPImmLegal and lowerConstantFP are consistent. We ultimately agreed it makes sense to add fli+fneg, but there may be other future cases where it doesn't make sense to match.

Added: 
    

Modified: 
    llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index 4845a9c84e01f..d859db3a965dd 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -2319,6 +2319,10 @@ bool RISCVTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT,
   if (getLegalZfaFPImm(Imm, VT) >= 0)
     return true;
 
+  // Some constants can be produced by fli+fneg.
+  if (Imm.isNegative() && getLegalZfaFPImm(-Imm, VT) >= 0)
+    return true;
+
   // Cannot create a 64 bit floating-point immediate value for rv32.
   if (Subtarget.getXLen() < VT.getScalarSizeInBits()) {
     // td can handle +0.0 or -0.0 already.


        


More information about the llvm-commits mailing list