Author: Vitaly Buka Date: 2026-11-17T15:54:38-08:00 New Revision: 0e32a4d767c3583be1be459aedc31e87cc316d4d
URL: https://github.com/llvm/llvm-project/commit/0e32a4d767c3583be1be459aedc31e87cc316d4d DIFF: https://github.com/llvm/llvm-project/commit/0e32a4d767c3583be1be459aedc31e87cc316d4d.diff LOG: Revert "[ConstraintElim] Add guarded add/sub.with.overflow results as EQ fact…" This reverts commit 07a94e61110e600d82ddabf1680de6f9728296c3. Added: Modified: llvm/lib/Transforms/Scalar/ConstraintElimination.cpp llvm/test/Transforms/ConstraintElimination/sadd-with-overflow.ll llvm/test/Transforms/ConstraintElimination/ssub-with-overflow.ll llvm/test/Transforms/ConstraintElimination/uadd-with-overflow.ll llvm/test/Transforms/ConstraintElimination/usub-with-overflow.ll Removed: ################################################################################ diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp index bd6392c69f379..c79aa32a67777 100644 --- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp +++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp @@ -774,29 +774,16 @@ static Decomposition decompose(Value *V, ConstraintInfo &Info, bool IsSigned, static Decomposition decomposeImpl(Value *V, ConstraintInfo &Info, bool IsSigned, const DataLayout &DL) { - auto MergeResults = [&Info, IsSigned, &DL]( - Value *A, Value *B, bool IsSignedB, - bool IsSub = false) -> std::optional<Decomposition> { + auto MergeResults = [&Info, IsSigned, + &DL](Value *A, Value *B, + bool IsSignedB) -> std::optional<Decomposition> { auto ResA = decompose(A, Info, IsSigned, DL); auto ResB = decompose(B, Info, IsSignedB, DL); - if (IsSub ? ResA.sub(ResB) : ResA.add(ResB)) + if (ResA.add(ResB)) return std::nullopt; return ResA; }; - // An add/sub.with.overflow is only decomposed for the fact queued on its - // no-overflow edge, where it does not overflow. - if (auto *WO = dyn_cast<WithOverflowInst>(V)) { - Instruction::BinaryOps Opc = WO->getBinaryOp(); - if ((Opc == Instruction::Add || Opc == Instruction::Sub) && - isKnownNoWrap(Opc, WO->getLHS(), WO->getRHS(), WO->getNoWrapKind(), - Info, IsSigned)) - if (auto Decomp = MergeResults(WO->getLHS(), WO->getRHS(), IsSigned, - Opc == Instruction::Sub)) - return *Decomp; - return V; - } - Type *Ty = V->getType()->getScalarType(); if (Ty->isPointerTy() && !IsSigned) { if (auto *GEP = dyn_cast<GEPOperator>(V)) @@ -1840,17 +1827,6 @@ void State::addInfoFor(BasicBlock &BB) { Value *Cond = Br->getCondition(); - // On the false successor of a branch on the overflow bit of an add/sub - // overflow intrinsic, its results equal the plain add/sub. - WithOverflowInst *WO; - if (match(Cond, m_ExtractValue<1>(m_WithOverflowInst(WO))) && - WO->getBinaryOp() != Instruction::Mul && - canAddSuccessor(BB, Br->getSuccessor(1))) - for (User *U : WO->users()) - if (match(U, m_ExtractValue<0>(m_Value()))) - WorkList.push_back(FactOrCheck::getConditionFact( - DT.getNode(Br->getSuccessor(1)), CmpInst::ICMP_EQ, U, WO)); - // If the condition is a chain of ORs/AND and the successor only has the // current block as predecessor, queue conditions for the successor. Value *Op0, *Op1; diff --git a/llvm/test/Transforms/ConstraintElimination/sadd-with-overflow.ll b/llvm/test/Transforms/ConstraintElimination/sadd-with-overflow.ll index e24a282f50eec..4194797fa5ea0 100644 --- a/llvm/test/Transforms/ConstraintElimination/sadd-with-overflow.ll +++ b/llvm/test/Transforms/ConstraintElimination/sadd-with-overflow.ll @@ -318,252 +318,3 @@ then: else: ret i8 0 } - -; The result is only used on the no-overflow edge, so it is %a + %b there. -define void @sadd_result_guarded_nonneg_ops(i8 %a, i8 %b) { -; CHECK-LABEL: define void @sadd_result_guarded_nonneg_ops( -; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) { -; CHECK-NEXT: [[ENTRY:.*:]] -; CHECK-NEXT: [[A_NONNEG:%.*]] = icmp sge i8 [[A]], 0 -; CHECK-NEXT: call void @llvm.assume(i1 [[A_NONNEG]]) -; CHECK-NEXT: [[B_NONNEG:%.*]] = icmp sge i8 [[B]], 0 -; CHECK-NEXT: call void @llvm.assume(i1 [[B_NONNEG]]) -; CHECK-NEXT: [[WO:%.*]] = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 [[A]], i8 [[B]]) -; CHECK-NEXT: [[OV:%.*]] = extractvalue { i8, i1 } [[WO]], 1 -; CHECK-NEXT: br i1 [[OV]], label %[[EXIT:.*]], label %[[NO_OV:.*]] -; CHECK: [[NO_OV]]: -; CHECK-NEXT: [[S:%.*]] = extractvalue { i8, i1 } [[WO]], 0 -; CHECK-NEXT: call void @use(i1 true) -; CHECK-NEXT: call void @use(i1 true) -; CHECK-NEXT: call void @use(i1 true) -; CHECK-NEXT: ret void -; CHECK: [[EXIT]]: -; CHECK-NEXT: ret void -; -entry: - %a.nonneg = icmp sge i8 %a, 0 - call void @llvm.assume(i1 %a.nonneg) - %b.nonneg = icmp sge i8 %b, 0 - call void @llvm.assume(i1 %b.nonneg) - %wo = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 %a, i8 %b) - %ov = extractvalue { i8, i1 } %wo, 1 - br i1 %ov, label %exit, label %no.ov - -no.ov: - %s = extractvalue { i8, i1 } %wo, 0 - %t.1 = icmp sge i8 %s, 0 - call void @use(i1 %t.1) - %t.2 = icmp sge i8 %s, %a - call void @use(i1 %t.2) - %t.3 = icmp uge i8 %s, %b - call void @use(i1 %t.3) - ret void - -exit: - ret void -} - -; srem of the guarded non-negative sum by %n is in [0, %n) and so below %d. -define i1 @sadd_result_guarded_srem_index(i8 %i, i8 %off, i8 %n, i8 %d) { -; CHECK-LABEL: define i1 @sadd_result_guarded_srem_index( -; CHECK-SAME: i8 [[I:%.*]], i8 [[OFF:%.*]], i8 [[N:%.*]], i8 [[D:%.*]]) { -; CHECK-NEXT: [[ENTRY:.*:]] -; CHECK-NEXT: [[I_NONNEG:%.*]] = icmp sge i8 [[I]], 0 -; CHECK-NEXT: call void @llvm.assume(i1 [[I_NONNEG]]) -; CHECK-NEXT: [[OFF_NONNEG:%.*]] = icmp sge i8 [[OFF]], 0 -; CHECK-NEXT: call void @llvm.assume(i1 [[OFF_NONNEG]]) -; CHECK-NEXT: [[N_POS:%.*]] = icmp sgt i8 [[N]], 0 -; CHECK-NEXT: call void @llvm.assume(i1 [[N_POS]]) -; CHECK-NEXT: [[N_ULE_D:%.*]] = icmp ule i8 [[N]], [[D]] -; CHECK-NEXT: call void @llvm.assume(i1 [[N_ULE_D]]) -; CHECK-NEXT: [[WO:%.*]] = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 [[I]], i8 [[OFF]]) -; CHECK-NEXT: [[OV:%.*]] = extractvalue { i8, i1 } [[WO]], 1 -; CHECK-NEXT: br i1 [[OV]], label %[[EXIT:.*]], label %[[NO_OV:.*]] -; CHECK: [[NO_OV]]: -; CHECK-NEXT: [[S:%.*]] = extractvalue { i8, i1 } [[WO]], 0 -; CHECK-NEXT: [[IDX:%.*]] = srem i8 [[S]], [[N]] -; CHECK-NEXT: ret i1 true -; CHECK: [[EXIT]]: -; CHECK-NEXT: ret i1 false -; -entry: - %i.nonneg = icmp sge i8 %i, 0 - call void @llvm.assume(i1 %i.nonneg) - %off.nonneg = icmp sge i8 %off, 0 - call void @llvm.assume(i1 %off.nonneg) - %n.pos = icmp sgt i8 %n, 0 - call void @llvm.assume(i1 %n.pos) - %n.ule.d = icmp ule i8 %n, %d - call void @llvm.assume(i1 %n.ule.d) - %wo = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 %i, i8 %off) - %ov = extractvalue { i8, i1 } %wo, 1 - br i1 %ov, label %exit, label %no.ov - -no.ov: - %s = extractvalue { i8, i1 } %wo, 0 - %idx = srem i8 %s, %n - %t = icmp ult i8 %idx, %d - ret i1 %t - -exit: - ret i1 false -} - -; %s is also used on the overflow edge; only the no-overflow use is simplified. -define void @sadd_result_used_on_overflow(i8 %a, i8 %b) { -; CHECK-LABEL: define void @sadd_result_used_on_overflow( -; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) { -; CHECK-NEXT: [[ENTRY:.*:]] -; CHECK-NEXT: [[A_NONNEG:%.*]] = icmp sge i8 [[A]], 0 -; CHECK-NEXT: call void @llvm.assume(i1 [[A_NONNEG]]) -; CHECK-NEXT: [[B_NONNEG:%.*]] = icmp sge i8 [[B]], 0 -; CHECK-NEXT: call void @llvm.assume(i1 [[B_NONNEG]]) -; CHECK-NEXT: [[WO:%.*]] = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 [[A]], i8 [[B]]) -; CHECK-NEXT: [[S:%.*]] = extractvalue { i8, i1 } [[WO]], 0 -; CHECK-NEXT: [[OV:%.*]] = extractvalue { i8, i1 } [[WO]], 1 -; CHECK-NEXT: br i1 [[OV]], label %[[OVF:.*]], label %[[NO_OV:.*]] -; CHECK: [[NO_OV]]: -; CHECK-NEXT: call void @use(i1 true) -; CHECK-NEXT: ret void -; CHECK: [[OVF]]: -; CHECK-NEXT: [[C_2:%.*]] = icmp sge i8 [[S]], 0 -; CHECK-NEXT: call void @use(i1 [[C_2]]) -; CHECK-NEXT: ret void -; -entry: - %a.nonneg = icmp sge i8 %a, 0 - call void @llvm.assume(i1 %a.nonneg) - %b.nonneg = icmp sge i8 %b, 0 - call void @llvm.assume(i1 %b.nonneg) - %wo = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 %a, i8 %b) - %s = extractvalue { i8, i1 } %wo, 0 - %ov = extractvalue { i8, i1 } %wo, 1 - br i1 %ov, label %ovf, label %no.ov - -no.ov: - %c.1 = icmp sge i8 %s, 0 - call void @use(i1 %c.1) - ret void - -ovf: - %c.2 = icmp sge i8 %s, 0 - call void @use(i1 %c.2) - ret void -} - -; Negative test: %b may be negative, so the sum can be below %a unsigned. -define void @sadd_result_guarded_unsigned_neg_op(i8 %a, i8 %b) { -; CHECK-LABEL: define void @sadd_result_guarded_unsigned_neg_op( -; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) { -; CHECK-NEXT: [[ENTRY:.*:]] -; CHECK-NEXT: [[A_NONNEG:%.*]] = icmp sge i8 [[A]], 0 -; CHECK-NEXT: call void @llvm.assume(i1 [[A_NONNEG]]) -; CHECK-NEXT: [[WO:%.*]] = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 [[A]], i8 [[B]]) -; CHECK-NEXT: [[OV:%.*]] = extractvalue { i8, i1 } [[WO]], 1 -; CHECK-NEXT: br i1 [[OV]], label %[[EXIT:.*]], label %[[NO_OV:.*]] -; CHECK: [[NO_OV]]: -; CHECK-NEXT: [[S:%.*]] = extractvalue { i8, i1 } [[WO]], 0 -; CHECK-NEXT: [[C:%.*]] = icmp uge i8 [[S]], [[A]] -; CHECK-NEXT: call void @use(i1 [[C]]) -; CHECK-NEXT: ret void -; CHECK: [[EXIT]]: -; CHECK-NEXT: ret void -; -entry: - %a.nonneg = icmp sge i8 %a, 0 - call void @llvm.assume(i1 %a.nonneg) - %wo = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 %a, i8 %b) - %ov = extractvalue { i8, i1 } %wo, 1 - br i1 %ov, label %exit, label %no.ov - -no.ov: - %s = extractvalue { i8, i1 } %wo, 0 - %c = icmp uge i8 %s, %a - call void @use(i1 %c) - ret void - -exit: - ret void -} - -; The no-overflow successor is a loop header that also gets induction facts, -; so %s >=s %a folds in the loop. -define void @sadd_result_no_overflow_succ_loop_header(i8 %a, i8 %b, i8 %n) { -; CHECK-LABEL: define void @sadd_result_no_overflow_succ_loop_header( -; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]], i8 [[N:%.*]]) { -; CHECK-NEXT: [[ENTRY:.*]]: -; CHECK-NEXT: [[B_NN:%.*]] = icmp sge i8 [[B]], 0 -; CHECK-NEXT: call void @llvm.assume(i1 [[B_NN]]) -; CHECK-NEXT: [[WO:%.*]] = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 [[A]], i8 [[B]]) -; CHECK-NEXT: [[S:%.*]] = extractvalue { i8, i1 } [[WO]], 0 -; CHECK-NEXT: [[OV:%.*]] = extractvalue { i8, i1 } [[WO]], 1 -; CHECK-NEXT: br i1 [[OV]], label %[[EXIT:.*]], label %[[LOOP:.*]] -; CHECK: [[LOOP]]: -; CHECK-NEXT: [[IV:%.*]] = phi i8 [ 0, %[[ENTRY]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ] -; CHECK-NEXT: call void @use(i1 true) -; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i8 [[IV]], 1 -; CHECK-NEXT: [[CONT:%.*]] = icmp ult i8 [[IV_NEXT]], [[N]] -; CHECK-NEXT: br i1 [[CONT]], label %[[LOOP]], label %[[EXIT]] -; CHECK: [[EXIT]]: -; CHECK-NEXT: ret void -; -entry: - %b.nn = icmp sge i8 %b, 0 - call void @llvm.assume(i1 %b.nn) - %wo = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 %a, i8 %b) - %s = extractvalue { i8, i1 } %wo, 0 - %ov = extractvalue { i8, i1 } %wo, 1 - br i1 %ov, label %exit, label %loop - -loop: - %iv = phi i8 [ 0, %entry ], [ %iv.next, %loop ] - %c = icmp sge i8 %s, %a - call void @use(i1 %c) - %iv.next = add nuw nsw i8 %iv, 1 - %cont = icmp ult i8 %iv.next, %n - br i1 %cont, label %loop, label %exit - -exit: - ret void -} - -; The result is used in a fact before the overflow check. -define void @sadd_result_fact_before_guard(i8 %a, i8 %b, i8 %n) { -; CHECK-LABEL: define void @sadd_result_fact_before_guard( -; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]], i8 [[N:%.*]]) { -; CHECK-NEXT: [[ENTRY:.*:]] -; CHECK-NEXT: [[B_NN:%.*]] = icmp sge i8 [[B]], 0 -; CHECK-NEXT: call void @llvm.assume(i1 [[B_NN]]) -; CHECK-NEXT: [[WO:%.*]] = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 [[A]], i8 [[B]]) -; CHECK-NEXT: [[S:%.*]] = extractvalue { i8, i1 } [[WO]], 0 -; CHECK-NEXT: [[C0:%.*]] = icmp sle i8 [[S]], [[N]] -; CHECK-NEXT: br i1 [[C0]], label %[[GUARD:.*]], label %[[EXIT:.*]] -; CHECK: [[GUARD]]: -; CHECK-NEXT: [[OV:%.*]] = extractvalue { i8, i1 } [[WO]], 1 -; CHECK-NEXT: br i1 [[OV]], label %[[EXIT]], label %[[NOOV:.*]] -; CHECK: [[NOOV]]: -; CHECK-NEXT: call void @use(i1 true) -; CHECK-NEXT: ret void -; CHECK: [[EXIT]]: -; CHECK-NEXT: ret void -; -entry: - %b.nn = icmp sge i8 %b, 0 - call void @llvm.assume(i1 %b.nn) - %wo = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 %a, i8 %b) - %s = extractvalue { i8, i1 } %wo, 0 - %c0 = icmp sle i8 %s, %n - br i1 %c0, label %guard, label %exit - -guard: - %ov = extractvalue { i8, i1 } %wo, 1 - br i1 %ov, label %exit, label %noov - -noov: - %c = icmp sle i8 %a, %n - call void @use(i1 %c) - ret void - -exit: - ret void -} diff --git a/llvm/test/Transforms/ConstraintElimination/ssub-with-overflow.ll b/llvm/test/Transforms/ConstraintElimination/ssub-with-overflow.ll index 6e697b4a20598..9077c3647589d 100644 --- a/llvm/test/Transforms/ConstraintElimination/ssub-with-overflow.ll +++ b/llvm/test/Transforms/ConstraintElimination/ssub-with-overflow.ll @@ -395,38 +395,3 @@ check.2: exit: ret i1 false } - -declare void @use(i1) - -; The result is only used on the no-overflow edge, so it is %a - %b there. -define void @ssub_result_guarded_nonneg_sub(i8 %a, i8 %b) { -; CHECK-LABEL: @ssub_result_guarded_nonneg_sub( -; CHECK-NEXT: entry: -; CHECK-NEXT: [[B_NONNEG:%.*]] = icmp sge i8 [[B:%.*]], 0 -; CHECK-NEXT: call void @llvm.assume(i1 [[B_NONNEG]]) -; CHECK-NEXT: [[WO:%.*]] = call { i8, i1 } @llvm.ssub.with.overflow.i8(i8 [[A:%.*]], i8 [[B]]) -; CHECK-NEXT: [[OV:%.*]] = extractvalue { i8, i1 } [[WO]], 1 -; CHECK-NEXT: br i1 [[OV]], label [[EXIT:%.*]], label [[NO_OV:%.*]] -; CHECK: no.ov: -; CHECK-NEXT: [[S:%.*]] = extractvalue { i8, i1 } [[WO]], 0 -; CHECK-NEXT: call void @use(i1 true) -; CHECK-NEXT: ret void -; CHECK: exit: -; CHECK-NEXT: ret void -; -entry: - %b.nonneg = icmp sge i8 %b, 0 - call void @llvm.assume(i1 %b.nonneg) - %wo = call { i8, i1 } @llvm.ssub.with.overflow.i8(i8 %a, i8 %b) - %ov = extractvalue { i8, i1 } %wo, 1 - br i1 %ov, label %exit, label %no.ov - -no.ov: - %s = extractvalue { i8, i1 } %wo, 0 - %t = icmp sle i8 %s, %a - call void @use(i1 %t) - ret void - -exit: - ret void -} diff --git a/llvm/test/Transforms/ConstraintElimination/uadd-with-overflow.ll b/llvm/test/Transforms/ConstraintElimination/uadd-with-overflow.ll index b3e723820fa8b..843c48c88ea7a 100644 --- a/llvm/test/Transforms/ConstraintElimination/uadd-with-overflow.ll +++ b/llvm/test/Transforms/ConstraintElimination/uadd-with-overflow.ll @@ -69,33 +69,3 @@ entry: call void @use(i1 %o) ret i8 %v } - -; The result is only used on the no-overflow edge, so it is %a + %b there. -define void @uadd_result_guarded(i8 %a, i8 %b) { -; CHECK-LABEL: define void @uadd_result_guarded( -; CHECK-SAME: i8 [[A:%.*]], i8 [[B:%.*]]) { -; CHECK-NEXT: [[ENTRY:.*:]] -; CHECK-NEXT: [[WO:%.*]] = call { i8, i1 } @llvm.uadd.with.overflow.i8(i8 [[A]], i8 [[B]]) -; CHECK-NEXT: [[OV:%.*]] = extractvalue { i8, i1 } [[WO]], 1 -; CHECK-NEXT: br i1 [[OV]], label %[[EXIT:.*]], label %[[NO_OV:.*]] -; CHECK: [[NO_OV]]: -; CHECK-NEXT: [[S:%.*]] = extractvalue { i8, i1 } [[WO]], 0 -; CHECK-NEXT: call void @use(i1 true) -; CHECK-NEXT: ret void -; CHECK: [[EXIT]]: -; CHECK-NEXT: ret void -; -entry: - %wo = call { i8, i1 } @llvm.uadd.with.overflow.i8(i8 %a, i8 %b) - %ov = extractvalue { i8, i1 } %wo, 1 - br i1 %ov, label %exit, label %no.ov - -no.ov: - %s = extractvalue { i8, i1 } %wo, 0 - %t = icmp uge i8 %s, %a - call void @use(i1 %t) - ret void - -exit: - ret void -} diff --git a/llvm/test/Transforms/ConstraintElimination/usub-with-overflow.ll b/llvm/test/Transforms/ConstraintElimination/usub-with-overflow.ll index e46047261b7f2..b4b3fcce2d31a 100644 --- a/llvm/test/Transforms/ConstraintElimination/usub-with-overflow.ll +++ b/llvm/test/Transforms/ConstraintElimination/usub-with-overflow.ll @@ -293,34 +293,3 @@ exit.fail: ret i8 0 } - -declare void @use(i1) - -; The result is only used on the no-overflow edge, so it is %a - %b there. -define void @usub_result_guarded(i8 %a, i8 %b) { -; CHECK-LABEL: @usub_result_guarded( -; CHECK-NEXT: entry: -; CHECK-NEXT: [[WO:%.*]] = call { i8, i1 } @llvm.usub.with.overflow.i8(i8 [[A:%.*]], i8 [[B:%.*]]) -; CHECK-NEXT: [[OV:%.*]] = extractvalue { i8, i1 } [[WO]], 1 -; CHECK-NEXT: br i1 [[OV]], label [[EXIT:%.*]], label [[NO_OV:%.*]] -; CHECK: no.ov: -; CHECK-NEXT: [[S:%.*]] = extractvalue { i8, i1 } [[WO]], 0 -; CHECK-NEXT: call void @use(i1 true) -; CHECK-NEXT: ret void -; CHECK: exit: -; CHECK-NEXT: ret void -; -entry: - %wo = call { i8, i1 } @llvm.usub.with.overflow.i8(i8 %a, i8 %b) - %ov = extractvalue { i8, i1 } %wo, 1 - br i1 %ov, label %exit, label %no.ov - -no.ov: - %s = extractvalue { i8, i1 } %wo, 0 - %t = icmp ule i8 %s, %a - call void @use(i1 %t) - ret void - -exit: - ret void -} _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
