-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[GVN] Drop nsw/nuw flags when replacing the result of a with.overflow intrinsic with a overflowing binary operator #82935
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
Conversation
This file contains 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
… intrinsic with a overflowing binary operator
@llvm/pr-subscribers-llvm-transforms Author: Yingwei Zheng (dtcxzyw) ChangesAlive2: https://alive2.llvm.org/ce/z/gyL7mn Full diff: https://github.com/llvm/llvm-project/pull/82935.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index 1373f5f7f4490c..075eeb5b19fd2b 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -3369,11 +3369,17 @@ void llvm::patchReplacementInstruction(Instruction *I, Value *Repl) {
// Patch the replacement so that it is not more restrictive than the value
// being replaced.
+ WithOverflowInst *UnusedWO;
+ // When replacing the result of a llvm.*.with.overflow intrinsic with a
+ // overflowing binary operator, nuw/nsw flags may no longer hold.
+ if (isa<OverflowingBinaryOperator>(ReplInst) &&
+ match(I, m_ExtractValue<0>(m_WithOverflowInst(UnusedWO))))
+ ReplInst->dropPoisonGeneratingFlags();
// Note that if 'I' is a load being replaced by some operation,
// for example, by an arithmetic operation, then andIRFlags()
// would just erase all math flags from the original arithmetic
// operation, which is clearly not wanted and not needed.
- if (!isa<LoadInst>(I))
+ else if (!isa<LoadInst>(I))
ReplInst->andIRFlags(I);
// FIXME: If both the original and replacement value are part of the
diff --git a/llvm/test/Transforms/GVN/pr82884.ll b/llvm/test/Transforms/GVN/pr82884.ll
new file mode 100644
index 00000000000000..71abafda60d93d
--- /dev/null
+++ b/llvm/test/Transforms/GVN/pr82884.ll
@@ -0,0 +1,21 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
+; RUN: opt -S -passes=gvn < %s | FileCheck %s
+
+; Make sure nsw/nuw flags are dropped.
+
+define i32 @pr82884(i32 %x) {
+; CHECK-LABEL: define i32 @pr82884(
+; CHECK-SAME: i32 [[X:%.*]]) {
+; CHECK-NEXT: [[MUL:%.*]] = mul i32 [[X]], [[X]]
+; CHECK-NEXT: call void @use(i32 [[MUL]])
+; CHECK-NEXT: [[MUL2:%.*]] = call { i32, i1 } @llvm.smul.with.overflow.i32(i32 [[X]], i32 [[X]])
+; CHECK-NEXT: ret i32 [[MUL]]
+;
+ %mul = mul nsw nuw i32 %x, %x
+ call void @use(i32 %mul)
+ %mul2 = call { i32, i1 } @llvm.smul.with.overflow.i32(i32 %x, i32 %x)
+ %ret = extractvalue { i32, i1 } %mul2, 0
+ ret i32 %ret
+}
+
+declare void @use(i32)
|
nikic
approved these changes
Feb 26, 2024
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
llvmbot
pushed a commit
to llvmbot/llvm-project
that referenced
this pull request
Feb 26, 2024
… intrinsic with a overflowing binary operator (llvm#82935) Alive2: https://alive2.llvm.org/ce/z/gyL7mn Fixes llvm#82884. (cherry picked from commit 892b4be)
llvmbot
pushed a commit
to llvmbot/llvm-project
that referenced
this pull request
Feb 26, 2024
… intrinsic with a overflowing binary operator (llvm#82935) Alive2: https://alive2.llvm.org/ce/z/gyL7mn Fixes llvm#82884. (cherry picked from commit 892b4be)
Closed
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.
Alive2: https://alive2.llvm.org/ce/z/gyL7mn
Fixes #82884.