[llvm] [AArch64] Sub, add, and icmp should have the fact they can be negated in the cost (PR #142844)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 4 13:26:54 PDT 2025


https://github.com/AZero13 created https://github.com/llvm/llvm-project/pull/142844

None

>From 57ea8aa7f1d2025b7db9a99f64480f271404584a Mon Sep 17 00:00:00 2001
From: Rose <gfunni234 at gmail.com>
Date: Wed, 4 Jun 2025 16:24:48 -0400
Subject: [PATCH] Update costs for AArch64

---
 llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp b/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
index 68aec80f07e1d..542b34df8b345 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
+++ b/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
@@ -473,7 +473,13 @@ InstructionCost AArch64TTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx,
 
   if (Idx == ImmIdx) {
     int NumConstants = (BitSize + 63) / 64;
-    InstructionCost Cost = AArch64TTIImpl::getIntImmCost(Imm, Ty, CostKind);
+    InstructionCost Cost;
+    if (Opcode == Instruction::Add || Opcode == Instruction::Sub ||
+        Opcode == Instruction::ICmp) {
+      // Add/Sub/ICmp immediates can be flipped.
+      Cost = AArch64TTIImpl::getIntImmCost(Imm.abs(), Ty, CostKind);
+    } else
+      Cost = AArch64TTIImpl::getIntImmCost(Imm, Ty, CostKind);
     return (Cost <= NumConstants * TTI::TCC_Basic)
                ? static_cast<int>(TTI::TCC_Free)
                : Cost;



More information about the llvm-commits mailing list