[PATCH] D148420: [InstCombine] Enhance select icmp and folding
Peixin Qiao via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sun May 7 08:05:21 PDT 2023
peixin updated this revision to Diff 520190.
peixin retitled this revision from "[InstSimplify] Enhance select icmp and simplification" to "[InstCombine] Enhance select icmp and folding".
peixin edited the summary of this revision.
peixin set the repository for this revision to rG LLVM Github Monorepo.
peixin removed subscribers: goldstein.w.n, StephenFan, llvm-commits, hiraditya.
peixin added a comment.
Address the comments from @nikic .
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D148420/new/
https://reviews.llvm.org/D148420
Files:
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
Index: llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -583,6 +583,38 @@
return new ZExtInst(ICmpNeZero, SelType);
}
+/// We want to turn:
+/// (select (icmp eq (and X, C), 0), 0, (shl (nsw/nuw) X, K));
+/// iff C is a mask and the number of its leading zeros is equal to K
+/// into:
+/// shl X, K
+static Value *foldSelectICmpAndAnd(const ICmpInst *Cmp,
+ Value *TVal, Value *FVal) {
+ ICmpInst::Predicate Pred;
+ Value *AndVal;
+ if (!match(ICmp, m_ICmp(Pred, m_Value(AndVal), m_Zero())))
+ return nullptr;
+
+ Value *X;
+ const APInt *K;
+ if (Pred != ICmpInst::ICMP_EQ ||
+ !match(AndVal, m_And(m_Specific(X), m_APInt(C))) ||
+ !match(TVal, m_Zero()) ||
+ !match(FVal, m_Shl(m_Value(X), m_APInt(K))))
+ return nullptr;
+
+ if (C->isMask() || (int64_t)C->countLeadingZeros() != K->getSExtValue())
+ return nullptr;
+
+ auto *FI = dyn_cast<Instruction>(FVal);
+ if (!FI)
+ return nullptr;
+
+ FI->setHasNoSignedWrap(false);
+ FI->setHasNoUnsignedWrap(false);
+ return FVal;
+}
+
/// We want to turn:
/// (select (icmp sgt x, C), lshr (X, Y), ashr (X, Y)); iff C s>= -1
/// (select (icmp slt x, C), ashr (X, Y), lshr (X, Y)); iff C s>= 0
@@ -1806,10 +1838,14 @@
}
}
+
if (Instruction *V =
foldSelectICmpAndAnd(SI.getType(), ICI, TrueVal, FalseVal, Builder))
return V;
+ if (Value *V = foldSelectICmpAndZeroShl(ICI, TrueVal, FalseVal))
+ return replaceInstUsesWith(SI, V);
+
if (Instruction *V = foldSelectCtlzToCttz(ICI, TrueVal, FalseVal, Builder))
return V;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148420.520190.patch
Type: text/x-patch
Size: 1792 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230507/1c08f517/attachment.bin>
More information about the llvm-commits
mailing list