[llvm] Add more cases for computeOverflowForSignedAdd (PR #99900)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Jul 27 16:33:16 PDT 2024
https://github.com/AtariDreams updated https://github.com/llvm/llvm-project/pull/99900
>From c4b6f11c9f72ec22f56a66815e0ab1580e5c445b Mon Sep 17 00:00:00 2001
From: Rose <gfunni234 at gmail.com>
Date: Mon, 22 Jul 2024 12:39:08 -0400
Subject: [PATCH] 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
More information about the llvm-commits
mailing list