[llvm] [ConstraintElim] Simplify `sadd_with_overflow` if A and B have different signs (PR #135784)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 30 01:23:08 PDT 2025
================
@@ -1719,17 +1752,39 @@ tryToSimplifyOverflowMath(IntrinsicInst *II, ConstraintInfo &Info,
};
bool Changed = false;
- if (II->getIntrinsicID() == Intrinsic::ssub_with_overflow) {
+ Value *A = II->getArgOperand(0);
+ Value *B = II->getArgOperand(1);
+ Type *Ty = A->getType();
+ auto *Zero = ConstantInt::get(Ty, 0);
+
+ switch (II->getIntrinsicID()) {
+ default:
+ llvm_unreachable("Unexpected intrinsic.");
+ case Intrinsic::sadd_with_overflow: {
+ bool ASgeZero = DoesConditionHold(CmpInst::ICMP_SGE, A, Zero, Info);
+ bool BSgeZero = DoesConditionHold(CmpInst::ICMP_SGE, B, Zero, Info);
+ bool ASleZero = DoesConditionHold(CmpInst::ICMP_SLE, A, Zero, Info);
+ bool BSleZero = DoesConditionHold(CmpInst::ICMP_SLE, B, Zero, Info);
+
+ // If A and B have different signs, sadd.with.overflow(a, b) should not
+ // overflow.
+ if ((ASgeZero && BSleZero) || (ASleZero && BSgeZero)) {
----------------
dtcxzyw wrote:
```suggestion
if ((DoesConditionHold(CmpInst::ICMP_SGE, A, Zero, Info) && DoesConditionHold(CmpInst::ICMP_SLE, B, Zero, Info)) || (DoesConditionHold(CmpInst::ICMP_SLE, A, Zero, Info) && DoesConditionHold(CmpInst::ICMP_SGE, B, Zero, Info))) {
```
`DoesConditionHold` is expensive.
https://github.com/llvm/llvm-project/pull/135784
More information about the llvm-commits
mailing list