-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[InstCombine] Preserve nneg in foldLogicCastConstant #157865
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] Preserve nneg in foldLogicCastConstant #157865
Conversation
|
@llvm/pr-subscribers-llvm-transforms Author: Hongyu Chen (XChy) ChangesThis patch makes use of the new public helper function to preserve nneg in Full diff: https://github.com/llvm/llvm-project/pull/157865.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 8b9df62d7c652..2d7524e8018b2 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -1801,10 +1801,14 @@ static Instruction *foldLogicCastConstant(BinaryOperator &Logic, CastInst *Cast,
Value *X;
auto &DL = IC.getDataLayout();
if (match(Cast, m_OneUse(m_ZExt(m_Value(X))))) {
- if (Constant *TruncC = getLosslessUnsignedTrunc(C, SrcTy, DL)) {
+ PreservedCastFlags Flags;
+ if (Constant *TruncC = getLosslessUnsignedTrunc(C, SrcTy, DL, &Flags)) {
// LogicOpc (zext X), C --> zext (LogicOpc X, C)
Value *NewOp = IC.Builder.CreateBinOp(LogicOpc, X, TruncC);
- return new ZExtInst(NewOp, DestTy);
+ auto *ZExt = new ZExtInst(NewOp, DestTy);
+ ZExt->setNonNeg(Flags.NNeg);
+ ZExt->andIRFlags(Cast);
+ return ZExt;
}
}
diff --git a/llvm/test/Transforms/InstCombine/or.ll b/llvm/test/Transforms/InstCombine/or.ll
index a5cc933278982..60a2c786c8bb4 100644
--- a/llvm/test/Transforms/InstCombine/or.ll
+++ b/llvm/test/Transforms/InstCombine/or.ll
@@ -2047,3 +2047,69 @@ define i1 @or_truncs(i8 %x) {
%or1 = or i1 %trunc1, %trunc2
ret i1 %or1
}
+
+define i32 @or_zext_constant(i8 %a) {
+; CHECK-LABEL: @or_zext_constant(
+; CHECK-NEXT: [[TMP1:%.*]] = or i8 [[A:%.*]], 1
+; CHECK-NEXT: [[AND:%.*]] = zext i8 [[TMP1]] to i32
+; CHECK-NEXT: ret i32 [[AND]]
+;
+ %zext = zext i8 %a to i32
+ %or = or i32 %zext, 1
+ ret i32 %or
+}
+
+define i32 @or_zext_minus_constant(i8 %a) {
+; CHECK-LABEL: @or_zext_minus_constant(
+; CHECK-NEXT: [[TMP1:%.*]] = or i8 [[A:%.*]], 1
+; CHECK-NEXT: [[OR:%.*]] = zext i8 [[TMP1]] to i32
+; CHECK-NEXT: ret i32 [[OR]]
+;
+ %zext = zext i8 %a to i32
+ %or = or i32 %zext, 1
+ ret i32 %or
+}
+
+define i32 @or_zext_nneg_constant(i8 %a) {
+; CHECK-LABEL: @or_zext_nneg_constant(
+; CHECK-NEXT: [[TMP1:%.*]] = or i8 [[A:%.*]], 9
+; CHECK-NEXT: [[AND:%.*]] = zext nneg i8 [[TMP1]] to i32
+; CHECK-NEXT: ret i32 [[AND]]
+;
+ %zext = zext nneg i8 %a to i32
+ %or = or i32 %zext, 9
+ ret i32 %or
+}
+
+define <4 x i32> @or_zext_nneg_constant_splat(<4 x i8> %a) {
+; CHECK-LABEL: @or_zext_nneg_constant_splat(
+; CHECK-NEXT: [[TMP1:%.*]] = or <4 x i8> [[A:%.*]], splat (i8 9)
+; CHECK-NEXT: [[OR:%.*]] = zext nneg <4 x i8> [[TMP1]] to <4 x i32>
+; CHECK-NEXT: ret <4 x i32> [[OR]]
+;
+ %zext = zext nneg <4 x i8> %a to <4 x i32>
+ %or = or <4 x i32> %zext, splat (i32 9)
+ ret <4 x i32> %or
+}
+
+define i32 @or_zext_nneg_minus_constant(i8 %a) {
+; CHECK-LABEL: @or_zext_nneg_minus_constant(
+; CHECK-NEXT: [[TMP1:%.*]] = or i8 [[A:%.*]], -9
+; CHECK-NEXT: [[ZEXT:%.*]] = sext i8 [[TMP1]] to i32
+; CHECK-NEXT: ret i32 [[ZEXT]]
+;
+ %zext = zext nneg i8 %a to i32
+ %or = or i32 %zext, -9
+ ret i32 %or
+}
+
+define <4 x i32> @or_zext_nneg_minus_constant_splat(<4 x i8> %a) {
+; CHECK-LABEL: @or_zext_nneg_minus_constant_splat(
+; CHECK-NEXT: [[TMP1:%.*]] = or <4 x i8> [[A:%.*]], splat (i8 -9)
+; CHECK-NEXT: [[OR:%.*]] = sext <4 x i8> [[TMP1]] to <4 x i32>
+; CHECK-NEXT: ret <4 x i32> [[OR]]
+;
+ %zext = zext nneg <4 x i8> %a to <4 x i32>
+ %or = or <4 x i32> %zext, splat (i32 -9)
+ ret <4 x i32> %or
+}
|
|
@zyw-bot mfuzz |
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.
LG
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/10/builds/13210 Here is the relevant piece of the build log for the reference |
This patch makes use of the new public helper function to preserve nneg in
foldLogicCastConstant.