[llvm] Add more cases for computeOverflowForSignedAdd (PR #99900)

via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 28 08:13:33 PDT 2024


https://github.com/AtariDreams updated https://github.com/llvm/llvm-project/pull/99900

>From 82f6056a9ac2f25bbe198b0fcd258aa0af9d8d00 Mon Sep 17 00:00:00 2001
From: Rose <gfunni234 at gmail.com>
Date: Mon, 22 Jul 2024 12:39:08 -0400
Subject: [PATCH 1/2] Add more cases for computeOverflowForSignedAdd for
 ValueTracking

Even with fwrapv, IR knows this does not wrap:

https://alive2.llvm.org/ce/z/jsbr78
---
 .../lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 23 +++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index bbc44a4716405..8381f607db0e4 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -4243,8 +4243,27 @@ SelectionDAG::computeOverflowForSignedAdd(SDValue N0, SDValue N1) const {
   if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
     return OFK_Never;
 
-  // TODO: Add ConstantRange::signedAddMayOverflow handling.
-  return OFK_Sometime;
+  // smulhi + any value less than half of signed max
+  KnownBits N1Known = computeKnownBits(N1);
+  if (N0.getOpcode() == ISD::SMUL_LOHI && N0.getResNo() == 1) {
+    APInt Max = APInt::getSignedMaxValue(N1.getScalarValueSizeInBits());
+    Max <<= 1;
+    if (N1Known.getMaxValue().slt(Max))
+      return OFK_Never;
+  }
+
+  KnownBits N0Known = computeKnownBits(N0);
+  if (N1.getOpcode() == ISD::SMUL_LOHI && N1.getResNo() == 1) {
+    APInt Max = APInt::getSignedMaxValue(N0.getScalarValueSizeInBits());
+    Max <<= 1;
+    if (N0Known.getMaxValue().slt(Max))
+      return OFK_Never;
+  }
+
+  // Fallback to ConstantRange::signedAddMayOverflow handling.
+  ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
+  ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
+  return mapOverflowResult(N0Range.signedAddMayOverflow(N1Range));
 }
 
 SelectionDAG::OverflowKind

>From 680907347a1710de20d3514009ccff0af84bfcfc Mon Sep 17 00:00:00 2001
From: AtariDreams <gfunni234 at gmail.com>
Date: Sun, 28 Jul 2024 11:13:25 -0400
Subject: [PATCH 2/2] Update SelectionDAG.cpp

---
 llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 8381f607db0e4..7b18232e6960e 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -4247,7 +4247,8 @@ SelectionDAG::computeOverflowForSignedAdd(SDValue N0, SDValue N1) const {
   KnownBits N1Known = computeKnownBits(N1);
   if (N0.getOpcode() == ISD::SMUL_LOHI && N0.getResNo() == 1) {
     APInt Max = APInt::getSignedMaxValue(N1.getScalarValueSizeInBits());
-    Max <<= 1;
+    Max >>= 1;
+    ++Max;
     if (N1Known.getMaxValue().slt(Max))
       return OFK_Never;
   }
@@ -4255,7 +4256,8 @@ SelectionDAG::computeOverflowForSignedAdd(SDValue N0, SDValue N1) const {
   KnownBits N0Known = computeKnownBits(N0);
   if (N1.getOpcode() == ISD::SMUL_LOHI && N1.getResNo() == 1) {
     APInt Max = APInt::getSignedMaxValue(N0.getScalarValueSizeInBits());
-    Max <<= 1;
+    Max >>= 1;
+    ++Max;
     if (N0Known.getMaxValue().slt(Max))
       return OFK_Never;
   }



More information about the llvm-commits mailing list