-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[RISCV][GlobalISel][TargetLowering] Change SSUBO to do (LHS < RHS) XOR (RESULT < 0) #150872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
@llvm/pr-subscribers-backend-risc-v @llvm/pr-subscribers-llvm-selectiondag Author: AZero13 (AZero13) ChangesThis folds better. Full diff: https://github.com/llvm/llvm-project/pull/150872.diff 1 Files Affected:
diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
index 1764910861df4..cbd42251bc841 100644
--- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -11447,13 +11447,25 @@ void TargetLowering::expandSADDSUBO(
// For a subtraction, the result should be less than one of the operands
// (LHS) if and only if the other operand (RHS) is (non-zero) positive,
// otherwise there will be overflow.
- SDValue ResultLowerThanLHS = DAG.getSetCC(dl, OType, Result, LHS, ISD::SETLT);
- SDValue ConditionRHS =
- DAG.getSetCC(dl, OType, RHS, Zero, IsAdd ? ISD::SETLT : ISD::SETGT);
-
- Overflow = DAG.getBoolExtOrTrunc(
- DAG.getNode(ISD::XOR, dl, OType, ConditionRHS, ResultLowerThanLHS), dl,
- ResultType, ResultType);
+
+ if (IsAdd) {
+ // For addition, the result should be less than one of the operands (LHS)
+ // if and only if the other operand (RHS) is negative, otherwise there will
+ // be overflow.
+ SDValue ResultLowerThanLHS = DAG.getSetCC(dl, OType, Result, LHS, ISD::SETLT);
+ SDValue ConditionRHS = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETLT);
+ Overflow = DAG.getBoolExtOrTrunc(
+ DAG.getNode(ISD::XOR, dl, OType, ConditionRHS, ResultLowerThanLHS), dl,
+ ResultType, ResultType);
+ } else {
+ // For subtraction, overflow occurs when the signed comparison of operands
+ // doesn't match the sign of the result
+ SDValue LHSLessThanRHS = DAG.getSetCC(dl, OType, LHS, RHS, ISD::SETLT);
+ SDValue ResultNegative = DAG.getSetCC(dl, OType, Result, Zero, ISD::SETLT);
+ Overflow = DAG.getBoolExtOrTrunc(
+ DAG.getNode(ISD::XOR, dl, OType, LHSLessThanRHS, ResultNegative), dl,
+ ResultType, ResultType);
+ }
}
bool TargetLowering::expandMULO(SDNode *Node, SDValue &Result,
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
Have to rebase to resolve conflicts with ssubo.ll ... |
7915652
to
1b27e5f
Compare
The title needs to include GlobalISel and RISCV too since TargetLowering is a SelectionDAG file. |
Please can you improve the patch summary description. |
Taking inspiration from the way ARM detects overflow, we should check the LHS and RHS as well as the difference when detecting subtraction.
Title should mention SSUBO or sub overflow. Just saying "subtraction" is confusing. |
You changed the title for one of my comments but this is still not addressed |
Taking inspiration from the way ARM detects overflow, we should check the LHS and RHS as well as the difference when detecting subtraction.