-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
[InstCombine] Infer nsw flag for (X <<nuw C1) >>u C --> X << (C1 - C)
#72407
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
@llvm/pr-subscribers-llvm-transforms Author: Yingwei Zheng (dtcxzyw) ChangesAlive2: https://alive2.llvm.org/ce/z/nnHAPy Full diff: https://github.com/llvm/llvm-project/pull/72407.diff 3 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index f3f4183efbc7895..a234b916f72c0e2 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -1293,9 +1293,10 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
unsigned ShlAmtC = C1->getZExtValue();
Constant *ShiftDiff = ConstantInt::get(Ty, ShlAmtC - ShAmtC);
if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) {
- // (X <<nuw C1) >>u C --> X <<nuw (C1 - C)
+ // (X <<nuw C1) >>u C --> X <<nuw/nsw (C1 - C)
auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
NewShl->setHasNoUnsignedWrap(true);
+ NewShl->setHasNoSignedWrap(ShAmtC > 0);
return NewShl;
}
if (Op0->hasOneUse()) {
diff --git a/llvm/test/Transforms/InstCombine/canonicalize-shl-lshr-to-masking.ll b/llvm/test/Transforms/InstCombine/canonicalize-shl-lshr-to-masking.ll
index a12cea49786b60b..7c1c18cc214a5c5 100644
--- a/llvm/test/Transforms/InstCombine/canonicalize-shl-lshr-to-masking.ll
+++ b/llvm/test/Transforms/InstCombine/canonicalize-shl-lshr-to-masking.ll
@@ -90,7 +90,7 @@ define i32 @positive_sameconst_shlnuw(i32 %x) {
define i32 @positive_biggerShl_shlnuw(i32 %x) {
; CHECK-LABEL: @positive_biggerShl_shlnuw(
-; CHECK-NEXT: [[RET:%.*]] = shl nuw i32 [[X:%.*]], 5
+; CHECK-NEXT: [[RET:%.*]] = shl nuw nsw i32 [[X:%.*]], 5
; CHECK-NEXT: ret i32 [[RET]]
;
%t0 = shl nuw i32 %x, 10
@@ -288,7 +288,7 @@ define i32 @positive_biggerShl_shlnuw_multiuse(i32 %x) {
; CHECK-LABEL: @positive_biggerShl_shlnuw_multiuse(
; CHECK-NEXT: [[T0:%.*]] = shl nuw i32 [[X:%.*]], 10
; CHECK-NEXT: call void @use32(i32 [[T0]])
-; CHECK-NEXT: [[RET:%.*]] = shl nuw i32 [[X]], 5
+; CHECK-NEXT: [[RET:%.*]] = shl nuw nsw i32 [[X]], 5
; CHECK-NEXT: ret i32 [[RET]]
;
%t0 = shl nuw i32 %x, 10
diff --git a/llvm/test/Transforms/InstCombine/shift.ll b/llvm/test/Transforms/InstCombine/shift.ll
index f7135971cf9eebd..e928d99d2feb127 100644
--- a/llvm/test/Transforms/InstCombine/shift.ll
+++ b/llvm/test/Transforms/InstCombine/shift.ll
@@ -951,11 +951,11 @@ define <2 x i32> @test52_splat_vec(<2 x i32> %x) {
ret <2 x i32> %B
}
-; (X <<nuw C1) >>u C2 --> X <<nuw (C1 - C2)
+; (X <<nuw C1) >>u C2 --> X <<nuw/nsw (C1 - C2)
define i32 @test53(i32 %x) {
; CHECK-LABEL: @test53(
-; CHECK-NEXT: [[B:%.*]] = shl nuw i32 [[X:%.*]], 2
+; CHECK-NEXT: [[B:%.*]] = shl nuw nsw i32 [[X:%.*]], 2
; CHECK-NEXT: ret i32 [[B]]
;
%A = shl nuw i32 %x, 3
@@ -963,11 +963,11 @@ define i32 @test53(i32 %x) {
ret i32 %B
}
-; (X <<nuw C1) >>u C2 --> X <<nuw (C1 - C2)
+; (X <<nuw C1) >>u C2 --> X <<nuw/nsw (C1 - C2)
define <2 x i32> @test53_splat_vec(<2 x i32> %x) {
; CHECK-LABEL: @test53_splat_vec(
-; CHECK-NEXT: [[B:%.*]] = shl nuw <2 x i32> [[X:%.*]], <i32 2, i32 2>
+; CHECK-NEXT: [[B:%.*]] = shl nuw nsw <2 x i32> [[X:%.*]], <i32 2, i32 2>
; CHECK-NEXT: ret <2 x i32> [[B]]
;
%A = shl nuw <2 x i32> %x, <i32 3, i32 3>
|
nikic
approved these changes
Nov 15, 2023
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
zahiraam
pushed a commit
to zahiraam/llvm-project
that referenced
this pull request
Nov 20, 2023
…)` (llvm#72407) Alive2: https://alive2.llvm.org/ce/z/nnHAPy This missed optimization is discovered with the help of AliveToolkit/alive2#962.
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/nnHAPy
This missed optimization is discovered with the help of AliveToolkit/alive2#962.