[llvm] [AArch64] Consider negated powers of 2 when calculating throughput cost (PR #143013)

David Green via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 10 05:58:15 PDT 2025


================
@@ -4005,25 +4005,34 @@ InstructionCost AArch64TTIImpl::getArithmeticInstrCost(
       // have similar cost.
       auto VT = TLI->getValueType(DL, Ty);
       if (VT.isScalarInteger() && VT.getSizeInBits() <= 64) {
-        if (Op2Info.isPowerOf2()) {
+        if (Op2Info.isPowerOf2() || Op2Info.isNegatedPowerOf2()) {
+          // Neg can be folded into the asr instruction.
+          // FIXME: Is the throughput cost of asr + neg the same as just asr?
           return ISD == ISD::SDIV ? (3 * AddCost + AsrCost)
                                   : (3 * AsrCost + AddCost);
         } else {
           return MulCost + AsrCost + 2 * AddCost;
         }
       } else if (VT.isVector()) {
         InstructionCost UsraCost = 2 * AsrCost;
-        if (Op2Info.isPowerOf2()) {
+        if (Op2Info.isPowerOf2() || Op2Info.isNegatedPowerOf2()) {
           // Division with scalable types corresponds to native 'asrd'
           // instruction when SVE is available.
           // e.g. %1 = sdiv <vscale x 4 x i32> %a, splat (i32 8)
+
+          // One more for the negation in SDIV
+          InstructionCost cost =
----------------
davemgreen wrote:

cost -> Cost

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


More information about the llvm-commits mailing list