[PATCH] D153148: [InstCombine] Fold `(-1 + zext(B)) & X` into `B ? 0 : X` where B is effectively a bool
Yingwei Zheng via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 16 08:39:27 PDT 2023
dtcxzyw created this revision.
dtcxzyw added reviewers: nikic, RKSimon, goldstein.w.n.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
dtcxzyw requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Solves issue https://github.com/llvm/llvm-project/issues/63321.
This patch explicitly folds `(-1 + zext(B)) & X` into `B ? 0 : X`. Additional trunc will be created when `B` is neither i1 nor <N x i1>.
https://alive2.llvm.org/ce/z/pWv9jJ
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D153148
Files:
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
llvm/test/Transforms/InstCombine/binop-cast.ll
Index: llvm/test/Transforms/InstCombine/binop-cast.ll
===================================================================
--- llvm/test/Transforms/InstCombine/binop-cast.ll
+++ llvm/test/Transforms/InstCombine/binop-cast.ll
@@ -257,3 +257,17 @@
%r = xor i32 %sext, 42
ret i32 %r
}
+
+define i64 @PR63321(ptr %ptr, i64 %c) {
+; CHECK-LABEL: @PR63321(
+; CHECK-NEXT: [[VAL:%.*]] = load i8, ptr [[PTR:%.*]], align 1, !range [[RNG0:![0-9]+]]
+; CHECK-NEXT: [[DOTNOT:%.*]] = icmp eq i8 [[VAL]], 0
+; CHECK-NEXT: [[RES:%.*]] = select i1 [[DOTNOT]], i64 [[C:%.*]], i64 0
+; CHECK-NEXT: ret i64 [[RES]]
+;
+ %val = load i8, ptr %ptr, align 1, !range !{i8 0, i8 2}
+ %rhs = zext i8 %val to i64
+ %mask = add i64 -1, %rhs
+ %res = and i64 %mask, %c
+ ret i64 %res
+}
Index: llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -2564,6 +2564,21 @@
A->getType()->isIntOrIntVectorTy(1))
return SelectInst::Create(A, Constant::getNullValue(Ty), Op0);
+ // (-1 + zext(A)) & Op1 --> A ? 0 : Op1
+ // Op0 & (-1 + zext(A)) --> A ? 0 : Op0
+ if (match(Op0, m_NSWAdd(m_ZExt(m_Value(A)), m_AllOnes())) &&
+ (A->getType()->isIntOrIntVectorTy(1) ||
+ (~computeKnownBits(A, 0, dyn_cast<Instruction>(Op0)).Zero) == 1))
+ return SelectInst::Create(
+ Builder.CreateTrunc(A, A->getType()->getWithNewBitWidth(1)),
+ Constant::getNullValue(Ty), Op1);
+ if (match(Op1, m_NSWAdd(m_ZExt(m_Value(A)), m_AllOnes())) &&
+ (A->getType()->isIntOrIntVectorTy(1) ||
+ (~computeKnownBits(A, 0, dyn_cast<Instruction>(Op1)).Zero) == 1))
+ return SelectInst::Create(
+ Builder.CreateTrunc(A, A->getType()->getWithNewBitWidth(1)),
+ Constant::getNullValue(Ty), Op0);
+
// (iN X s>> (N-1)) & Y --> (X s< 0) ? Y : 0 -- with optional sext
if (match(&I, m_c_And(m_OneUse(m_SExtOrSelf(
m_AShr(m_Value(X), m_APIntAllowUndef(C)))),
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153148.532161.patch
Type: text/x-patch
Size: 2108 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230616/9c6c560c/attachment.bin>
More information about the llvm-commits
mailing list