[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
Mon Mar 13 09:38:30 PDT 2023


jonpa updated this revision to Diff 504716.
jonpa added a comment.

ok, I think I got it now: we can drop the NW flags because the backend will then later properly handle OF as well during comparison elimination (and the program prints the right result).

Patch updated to drop the flags instead, per review.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D145811/new/

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,39 @@
+; RUN: llc < %s -mtriple=s390x-linux-gnu -mcpu=z15 | FileCheck %s
+;
+; Test that a CC result of a sub that can overflow is tested with the right predicate.
+
+define i32 @fun0(i32 %a, i32 %b, ptr %dest) {
+; CHECK-LABEL: fun0
+; CHECK: s %r2, 0(%r4)
+; CHECK: bner %r14
+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: fun1
+; CHECK: s %r2, 0(%r4)
+; CHECK: bner %r14
+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
@@ -2425,6 +2425,12 @@
       if (N->getOpcode() == ISD::SUB &&
           ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
            (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
+        // Disable the nsw and nuw flags: the backend needs to handle
+        // overflow as well during comparison elimination.
+        SDNodeFlags Flags = N->getFlags();
+        Flags.setNoSignedWrap(false);
+        Flags.setNoUnsignedWrap(false);
+        N->setFlags(Flags);
         C.Op0 = SDValue(N, 0);
         C.Op1 = DAG.getConstant(0, DL, N->getValueType(0));
         return;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145811.504716.patch
Type: text/x-patch
Size: 1854 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230313/0c44ad68/attachment.bin>


More information about the llvm-commits mailing list