[llvm] [ARM] Swap sides of cmp/cmn based on folding ability (PR #191915)

John Brawn via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 29 03:44:02 PDT 2026


================
@@ -4544,12 +4580,22 @@ SDValue ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
         break;
       }
     }
-  } else if ((ARM_AM::getShiftOpcForNode(LHS.getOpcode()) != ARM_AM::no_shift) &&
-             (ARM_AM::getShiftOpcForNode(RHS.getOpcode()) == ARM_AM::no_shift)) {
-    // In ARM and Thumb-2, the compare instructions can shift their second
-    // operand.
-    CC = ISD::getSetCCSwappedOperands(CC);
-    std::swap(LHS, RHS);
+  }
+
+  // Prefer folding shifts / CMN into the cmp/cmn second operand (so_reg /
+  // t2_so_reg). When both sides compete, pick the higher
+  // getCmpOperandFoldingProfit (operands are peeled if CMN). Only when RHS
+  // is not a legal icmp immediate: otherwise keep the canonical (reg, imm)
+  // form.
+  bool RHSFitsICmpImmediate = false;
+  if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getNode()))
+    RHSFitsICmpImmediate = isLegalICmpImmediate(C->getSExtValue());
+  if (!isa<ConstantSDNode>(RHS) || !RHSFitsICmpImmediate) {
----------------
john-brawn-arm wrote:

This can be simplified to something like
```
ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getNode());
if (!C || isLegalICmpImmediate(C->getSExtValue()))
```

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


More information about the llvm-commits mailing list