-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[PredicateInfo] Handle trunc nuw i1 condition. #152988
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
Merged
Merged
Conversation
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
Member
|
@llvm/pr-subscribers-llvm-transforms Author: Andreas Jonson (andjo403) Changesproof: https://alive2.llvm.org/ce/z/mxtn4L part of changes needed for move of #151961 to SCCP Full diff: https://github.com/llvm/llvm-project/pull/152988.diff 3 Files Affected:
diff --git a/llvm/lib/Transforms/Utils/PredicateInfo.cpp b/llvm/lib/Transforms/Utils/PredicateInfo.cpp
index 02420fa8abd97..38a312a3715c9 100644
--- a/llvm/lib/Transforms/Utils/PredicateInfo.cpp
+++ b/llvm/lib/Transforms/Utils/PredicateInfo.cpp
@@ -370,6 +370,8 @@ void PredicateInfoBuilder::processAssume(
Values.push_back(Cond);
if (auto *Cmp = dyn_cast<CmpInst>(Cond))
collectCmpOps(Cmp, Values);
+ else if (match(Cond, m_NUWTrunc(m_Value(Op0))))
+ Values.push_back(Op0);
for (Value *V : Values) {
if (shouldRename(V)) {
@@ -416,6 +418,8 @@ void PredicateInfoBuilder::processBranch(
Values.push_back(Cond);
if (auto *Cmp = dyn_cast<CmpInst>(Cond))
collectCmpOps(Cmp, Values);
+ else if (match(Cond, m_NUWTrunc(m_Value(Op0))))
+ Values.push_back(Op0);
for (Value *V : Values) {
if (shouldRename(V)) {
@@ -709,6 +713,11 @@ std::optional<PredicateConstraint> PredicateBase::getConstraint() const {
: ConstantInt::getFalse(Condition->getType())}};
}
+ if (match(Condition, m_NUWTrunc(m_Specific(RenamedOp)))) {
+ return {{TrueEdge ? CmpInst::ICMP_NE : CmpInst::ICMP_EQ,
+ ConstantInt::getNullValue(RenamedOp->getType())}};
+ }
+
CmpInst *Cmp = dyn_cast<CmpInst>(Condition);
if (!Cmp) {
// TODO: Make this an assertion once RenamedOp is fully accurate.
diff --git a/llvm/test/Transforms/SCCP/assume.ll b/llvm/test/Transforms/SCCP/assume.ll
index 8146d58d9a897..9beee934bb509 100644
--- a/llvm/test/Transforms/SCCP/assume.ll
+++ b/llvm/test/Transforms/SCCP/assume.ll
@@ -69,3 +69,53 @@ define void @nonnull(ptr %v) {
call void @use(i1 %c4)
ret void
}
+
+define void @trunc_nuw(i8 %v) {
+; CHECK-LABEL: @trunc_nuw(
+; CHECK-NEXT: [[A:%.*]] = trunc nuw i8 [[V:%.*]] to i1
+; CHECK-NEXT: call void @llvm.assume(i1 [[A]])
+; CHECK-NEXT: call void @use(i1 false)
+; CHECK-NEXT: call void @use(i1 true)
+; CHECK-NEXT: call void @use(i1 false)
+; CHECK-NEXT: call void @use(i1 true)
+; CHECK-NEXT: ret void
+;
+ %a = trunc nuw i8 %v to i1
+ call void @llvm.assume(i1 %a)
+ %c1 = icmp eq i8 %v, 0
+ call void @use(i1 %c1)
+ %c2 = icmp ne i8 %v, 0
+ call void @use(i1 %c2)
+ %c3 = icmp eq i8 0, %v
+ call void @use(i1 %c3)
+ %c4 = icmp ne i8 0, %v
+ call void @use(i1 %c4)
+ ret void
+}
+
+define void @neg_trunc(i8 %v) {
+; CHECK-LABEL: @neg_trunc(
+; CHECK-NEXT: [[A:%.*]] = trunc i8 [[V:%.*]] to i1
+; CHECK-NEXT: call void @llvm.assume(i1 [[A]])
+; CHECK-NEXT: [[C1:%.*]] = icmp eq i8 [[V]], 0
+; CHECK-NEXT: call void @use(i1 [[C1]])
+; CHECK-NEXT: [[C2:%.*]] = icmp ne i8 [[V]], 0
+; CHECK-NEXT: call void @use(i1 [[C2]])
+; CHECK-NEXT: [[C3:%.*]] = icmp eq i8 0, [[V]]
+; CHECK-NEXT: call void @use(i1 [[C3]])
+; CHECK-NEXT: [[C4:%.*]] = icmp ne i8 0, [[V]]
+; CHECK-NEXT: call void @use(i1 [[C4]])
+; CHECK-NEXT: ret void
+;
+ %a = trunc i8 %v to i1
+ call void @llvm.assume(i1 %a)
+ %c1 = icmp eq i8 %v, 0
+ call void @use(i1 %c1)
+ %c2 = icmp ne i8 %v, 0
+ call void @use(i1 %c2)
+ %c3 = icmp eq i8 0, %v
+ call void @use(i1 %c3)
+ %c4 = icmp ne i8 0, %v
+ call void @use(i1 %c4)
+ ret void
+}
diff --git a/llvm/test/Transforms/SCCP/conditions-ranges.ll b/llvm/test/Transforms/SCCP/conditions-ranges.ll
index bb3764160f724..7a64778b60789 100644
--- a/llvm/test/Transforms/SCCP/conditions-ranges.ll
+++ b/llvm/test/Transforms/SCCP/conditions-ranges.ll
@@ -1444,3 +1444,65 @@ define i1 @ptr_icmp_data_layout() {
%cmp = icmp eq ptr %a.end, @A
ret i1 %cmp
}
+
+define void @trunc_nuw_1_dominating_icmp_ne_0(i8 %x) {
+; CHECK-LABEL: @trunc_nuw_1_dominating_icmp_ne_0(
+; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw i8 [[X:%.*]] to i1
+; CHECK-NEXT: br i1 [[TRUNC]], label [[BB1:%.*]], label [[BB2:%.*]]
+; CHECK: bb1:
+; CHECK-NEXT: call void @use(i1 true)
+; CHECK-NEXT: call void @use(i1 true)
+; CHECK-NEXT: ret void
+; CHECK: bb2:
+; CHECK-NEXT: call void @use(i1 false)
+; CHECK-NEXT: call void @use(i1 false)
+; CHECK-NEXT: ret void
+;
+ %trunc = trunc nuw i8 %x to i1
+ br i1 %trunc, label %bb1, label %bb2
+bb1:
+ %c1 = icmp ne i8 %x , 0
+ call void @use(i1 %c1)
+ %c2 = icmp ne i8 0, %x
+ call void @use(i1 %c2)
+ ret void
+bb2:
+ %c3 = icmp ne i8 %x , 0
+ call void @use(i1 %c3)
+ %c4 = icmp ne i8 0, %x
+ call void @use(i1 %c4)
+ ret void
+}
+
+define void @neg_trunc_1_dominating_icmp_ne_0(i8 %x) {
+; CHECK-LABEL: @neg_trunc_1_dominating_icmp_ne_0(
+; CHECK-NEXT: [[TRUNC:%.*]] = trunc i8 [[X:%.*]] to i1
+; CHECK-NEXT: br i1 [[TRUNC]], label [[BB1:%.*]], label [[BB2:%.*]]
+; CHECK: bb1:
+; CHECK-NEXT: [[C1:%.*]] = icmp ne i8 [[X]], 0
+; CHECK-NEXT: call void @use(i1 [[C1]])
+; CHECK-NEXT: [[C2:%.*]] = icmp ne i8 0, [[X]]
+; CHECK-NEXT: call void @use(i1 [[C2]])
+; CHECK-NEXT: ret void
+; CHECK: bb2:
+; CHECK-NEXT: [[C3:%.*]] = icmp ne i8 [[X]], 0
+; CHECK-NEXT: call void @use(i1 [[C3]])
+; CHECK-NEXT: [[C4:%.*]] = icmp ne i8 0, [[X]]
+; CHECK-NEXT: call void @use(i1 [[C4]])
+; CHECK-NEXT: ret void
+;
+ %trunc = trunc i8 %x to i1
+ br i1 %trunc, label %bb1, label %bb2
+bb1:
+ %c1 = icmp ne i8 %x , 0
+ call void @use(i1 %c1)
+ %c2 = icmp ne i8 0, %x
+ call void @use(i1 %c2)
+ ret void
+bb2:
+ %c3 = icmp ne i8 %x , 0
+ call void @use(i1 %c3)
+ %c4 = icmp ne i8 0, %x
+ call void @use(i1 %c4)
+ ret void
+}
|
nikic
approved these changes
Aug 11, 2025
Contributor
nikic
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
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.
proof: https://alive2.llvm.org/ce/z/mxtn4L
part of changes needed for move of #151961 to SCCP