[PATCH] D145811: [SystemZ] Don't reuse a sub that can overflow during isel.

Jonas Paulsson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 10 10:05:47 PST 2023


jonpa created this revision.
jonpa added a reviewer: uweigand.
Herald added a subscriber: hiraditya.
Herald added a project: All.
jonpa requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Don't use a possibly overflowing ISD::SUB node as a check against 0.

Removing this optimization for the cases of SUBs with the NSW/NUW flag set only gives only a negligible difference over benchmarks: ~50 more compares and no noticeable performance changes on SPEC. Removing the adjustForSubtraction() entirely however gave more changes: 1530 more compares, so it seems advisable to keep that function for the SUBs without NSW/NUW flags.

Fixes: https://github.com/llvm/llvm-project/issues/61268


https://reviews.llvm.org/D145811

Files:
  llvm/lib/Target/SystemZ/SystemZISelLowering.cpp
  llvm/test/CodeGen/SystemZ/int-cmp-62.ll


Index: llvm/test/CodeGen/SystemZ/int-cmp-62.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/SystemZ/int-cmp-62.ll
@@ -0,0 +1,41 @@
+; RUN: llc < %s -mtriple=s390x-linux-gnu -mcpu=z15 -stop-after=instruction-select | FileCheck %s
+;
+; Test that a sub that can overflow is not reused during isel as a comparison.
+
+define i32 @fun0(i32 %a, i32 %b, ptr %dest) {
+; CHECK-LABEL: name: fun0
+; CHECK: $r0l = LR $r2l
+; CHECK-NEXT: renamable $r2l = nsw SR killed $r2l, renamable $r1l, implicit-def dead $cc
+; CHECK-NEXT: CRBReturn killed renamable $r0l, killed renamable $r1l, 6, implicit $r2l
+entry:
+  %cur = load i32, ptr %dest
+  %res = sub nsw i32 %a, %cur
+  %cmp = icmp ne i32 %a, %cur
+  br i1 %cmp, label %exit, label %store
+
+store:
+  store i32 %b, ptr %dest
+  br label %exit
+
+exit:
+  ret i32 %res
+}
+
+define i32 @fun1(i32 %a, i32 %b, ptr %dest) {
+; CHECK-LABEL: name: fun1
+; CHECK: $r0l = LR $r2l
+; CHECK-NEXT: renamable $r2l = nuw SR killed $r2l, renamable $r1l, implicit-def dead $cc
+; CHECK-NEXT: CRBReturn killed renamable $r0l, killed renamable $r1l, 6, implicit $r2l
+entry:
+  %cur = load i32, ptr %dest
+  %res = sub nuw i32 %a, %cur
+  %cmp = icmp ne i32 %a, %cur
+  br i1 %cmp, label %exit, label %store
+
+store:
+  store i32 %b, ptr %dest
+  br label %exit
+
+exit:
+  ret i32 %res
+}
Index: llvm/lib/Target/SystemZ/SystemZISelLowering.cpp
===================================================================
--- llvm/lib/Target/SystemZ/SystemZISelLowering.cpp
+++ llvm/lib/Target/SystemZ/SystemZISelLowering.cpp
@@ -2423,6 +2423,7 @@
       C.CCMask == SystemZ::CCMASK_CMP_NE) {
     for (SDNode *N : C.Op0->uses()) {
       if (N->getOpcode() == ISD::SUB &&
+          (!N->getFlags().hasNoSignedWrap() && !N->getFlags().hasNoUnsignedWrap()) &&
           ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
            (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
         C.Op0 = SDValue(N, 0);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145811.504195.patch
Type: text/x-patch
Size: 2015 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230310/8ba51e72/attachment.bin>


More information about the llvm-commits mailing list