[PATCH] D144324: [AArch64][SelectionDAG] Perfer CMN for (0 - Y) == Y

Allen zhong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 18 00:49:06 PST 2023


Allen created this revision.
Allen added reviewers: paulwalker-arm, david-arm, spatel.
Herald added subscribers: StephenFan, ecnelises, hiraditya, kristof.beyls.
Herald added a project: All.
Allen requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

cmn a, b means a - (-b) - which under two's complement arithmetic
is exactly equivalent to a + b. so Y == -Y is just match the cmn Y, Y

      

Fix https://github.com/llvm/llvm-project/issues/60818


https://reviews.llvm.org/D144324

Files:
  llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
  llvm/test/CodeGen/AArch64/cmp-to-cmn.ll


Index: llvm/test/CodeGen/AArch64/cmp-to-cmn.ll
===================================================================
--- llvm/test/CodeGen/AArch64/cmp-to-cmn.ll
+++ llvm/test/CodeGen/AArch64/cmp-to-cmn.ll
@@ -397,3 +397,15 @@
   %cmp = icmp ne i32 %conv, %add
   ret i1 %cmp
 }
+
+define i1 @PR60818(i32 %a) {
+; CHECK-LABEL: PR60818:
+; CHECK:       // %bb.0: // %entry
+; CHECK-NEXT:    cmn	w0, w0
+; CHECK-NEXT:    cset w0, ne
+; CHECK-NEXT:    ret
+entry:
+  %sub = sub i32 0, %a
+  %cmp = icmp ne i32 %sub, %a
+  ret i1 %cmp
+}
Index: llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
===================================================================
--- llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -4011,6 +4011,11 @@
   if (!N0.hasOneUse() || OpVT.getScalarSizeInBits() == 1)
     return SDValue();
 
+  // The shift would not be disable as we prefer CMN for (0 - Y) == Y
+  if (ConstantSDNode *CTVal = isConstOrConstSplat(X))
+    if (CTVal->isZero())
+      return SDValue();
+
   // (X - Y) == Y --> X == Y << 1
   EVT ShiftVT = getShiftAmountTy(OpVT, DAG.getDataLayout(),
                                  !DCI.isBeforeLegalize());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144324.498568.patch
Type: text/x-patch
Size: 1203 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230218/9bc46baf/attachment.bin>


More information about the llvm-commits mailing list