[ConstraintElimination] Defer removal of simplified ssub.with.overflow - #215135
Open
fhahn wants to merge 1 commit into
Open
[ConstraintElimination] Defer removal of simplified ssub.with.overflow#215135fhahn wants to merge 1 commit into
fhahn wants to merge 1 commit into
Conversation
|
@llvm/pr-subscribers-llvm-transforms Author: Florian Hahn (fhahn) ChangesreplaceSubOverflowUses erased the intrinsic as soon as it became dead. That frees the intrinsic's operand Use array, but the worklist can still hold UseCheck entries pointing into it, storing a now invalid pointer to a Use *. Instead of erasing the intrinsic in place, poison its arguments and push it onto ToRemove. Full diff: https://github.com/llvm/llvm-project/pull/215135.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index b31ae075e8f4a..3feb9eceec8b5 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -1950,7 +1950,13 @@ static bool replaceSubOverflowUses(IntrinsicInst *II, Value *A, Value *B,
}
if (II->use_empty()) {
- II->eraseFromParent();
+ // Do not erase II here: the worklist may still hold Uses of II's operands,
+ // and evaluating those entries after II has been freed reads freed memory.
+ // Poison the operands to invalidate such entries and defer the removal,
+ // like for the dead extractvalue users above.
+ for (Use &Arg : II->args())
+ Arg.set(PoisonValue::get(Arg->getType()));
+ ToRemove.push_back(II);
Changed = true;
}
return Changed;
diff --git a/llvm/test/Transforms/ConstraintElimination/ssub-with-overflow.ll b/llvm/test/Transforms/ConstraintElimination/ssub-with-overflow.ll
index 4a270881bebf8..9077c3647589d 100644
--- a/llvm/test/Transforms/ConstraintElimination/ssub-with-overflow.ll
+++ b/llvm/test/Transforms/ConstraintElimination/ssub-with-overflow.ll
@@ -2,6 +2,7 @@
; RUN: opt -passes=constraint-elimination -S %s | FileCheck %s
declare { i8, i1 } @llvm.ssub.with.overflow.i8(i8, i8)
+declare { i1, i1 } @llvm.ssub.with.overflow.i1(i1, i1)
define i8 @ssub_no_overflow_due_to_or_conds(i8 %a, i8 %b) {
; CHECK-LABEL: @ssub_no_overflow_due_to_or_conds(
@@ -349,3 +350,48 @@ exit.ok:
exit.fail:
ret i8 0
}
+
+; The arguments of the intrinsic are compares that are themselves checked, and
+; the block holding the intrinsic comes before the block defining them.
+define i1 @ssub_simplified_before_uses_of_arguments(i32 %a) {
+; CHECK-LABEL: @ssub_simplified_before_uses_of_arguments(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: br label [[CHECK_1:%.*]]
+; CHECK: math:
+; CHECK-NEXT: [[TMP0:%.*]] = sub nsw i1 [[C:%.*]], [[D:%.*]]
+; CHECK-NEXT: [[R:%.*]] = or i1 [[TMP0]], false
+; CHECK-NEXT: ret i1 [[R]]
+; CHECK: check.1:
+; CHECK-NEXT: [[C]] = icmp sgt i32 [[A:%.*]], 0
+; CHECK-NEXT: [[D]] = icmp sgt i32 [[A]], 1
+; CHECK-NEXT: [[C_1:%.*]] = icmp sge i1 [[C]], [[D]]
+; CHECK-NEXT: br i1 [[C_1]], label [[CHECK_2:%.*]], label [[EXIT:%.*]]
+; CHECK: check.2:
+; CHECK-NEXT: [[C_2:%.*]] = icmp sge i1 [[D]], false
+; CHECK-NEXT: br i1 [[C_2]], label [[MATH:%.*]], label [[EXIT]]
+; CHECK: exit:
+; CHECK-NEXT: ret i1 false
+;
+entry:
+ br label %check.1
+
+math:
+ %op = call { i1, i1 } @llvm.ssub.with.overflow.i1(i1 %c, i1 %d)
+ %res = extractvalue { i1, i1 } %op, 0
+ %status = extractvalue { i1, i1 } %op, 1
+ %r = or i1 %res, %status
+ ret i1 %r
+
+check.1:
+ %c = icmp sgt i32 %a, 0
+ %d = icmp sgt i32 %a, 1
+ %c.1 = icmp sge i1 %c, %d
+ br i1 %c.1, label %check.2, label %exit
+
+check.2:
+ %c.2 = icmp sge i1 %d, 0
+ br i1 %c.2, label %math, label %exit
+
+exit:
+ ret i1 false
+}
|
replaceSubOverflowUses erased the intrinsic as soon as it became dead. That frees the intrinsic's operand Use array, but the worklist can still hold UseCheck entries pointing into it, storing a now invalid pointer to a Use *. Instead of erasing the intrinsic in place, poison its arguments and push it onto ToRemove.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
replaceSubOverflowUses erased the intrinsic as soon as it became dead. That frees the intrinsic's operand Use array, but the worklist can still hold UseCheck entries pointing into it, storing a now invalid pointer to a Use *.
Instead of erasing the intrinsic in place, poison its arguments and push it onto ToRemove.