[PATCH] D148420: [InstSimplify] Enhance select icmp and simplification
Peixin Qiao via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sun Apr 16 07:04:57 PDT 2023
peixin updated this revision to Diff 514004.
peixin retitled this revision from "[InstCombine] Enhance select icmp and folding" to "[InstSimplify] Enhance select icmp and simplification".
peixin edited the summary of this revision.
peixin added a comment.
Address the comments.
1. Move to instsimplify.
2. Add support for vector type.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D148420/new/
https://reviews.llvm.org/D148420
Files:
llvm/lib/Analysis/InstructionSimplify.cpp
llvm/test/Transforms/InstSimplify/select.ll
Index: llvm/test/Transforms/InstSimplify/select.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/select.ll
+++ llvm/test/Transforms/InstSimplify/select.ll
@@ -1342,3 +1342,29 @@
%sel = select i1 %eq42, i8 0, i8 %and
ret i8 %sel
}
+
+; (x << k) ? 2^k * x : 0 --> 2^k * x
+
+define i32 @select_icmp_and_shl(i32 %x) {
+; CHECK-LABEL: @select_icmp_and_shl(
+; CHECK-NEXT: [[MUL:%.*]] = shl i32 [[X:%.*]], 2
+; CHECK-NEXT: ret i32 [[MUL]]
+;
+ %shl.mask = and i32 %x, 1073741823
+ %tobool.not = icmp eq i32 %shl.mask, 0
+ %mul = shl i32 %x, 2
+ %cond = select i1 %tobool.not, i32 0, i32 %mul
+ ret i32 %cond
+}
+
+define <2 x i32> @select_icmp_and_shl_vect(<2 x i32> %x) {
+; CHECK-LABEL: @select_icmp_and_shl_vect(
+; CHECK-NEXT: [[MUL:%.*]] = shl <2 x i32> [[X:%.*]], <i32 2, i32 2>
+; CHECK-NEXT: ret <2 x i32> [[MUL]]
+;
+ %shl.mask = and <2 x i32> %x, <i32 1073741823, i32 1073741823>
+ %tobool.not = icmp eq <2 x i32> %shl.mask, zeroinitializer
+ %mul = shl <2 x i32> %x, <i32 2, i32 2>
+ %cond = select <2 x i1> %tobool.not, <2 x i32> zeroinitializer, <2 x i32> %mul
+ ret <2 x i32> %cond
+}
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4392,6 +4392,32 @@
return nullptr;
}
+/// This simplifies:
+/// select (icmp eq (and X, C), 0), 0, (shl X, K)
+/// iff C is a mask and the number of its leading zeros is equal to K.
+/// To the following:
+/// shl X, K
+static Value *simplifySelectICmpAndZeroShl(Value *ICmp, Value *TrueVal,
+ Value *FalseVal) {
+ ICmpInst::Predicate Pred;
+ const APInt *K;
+ Value *X, *AndVal;
+ if (!match(TrueVal, m_Zero()) ||
+ !match(FalseVal, m_Shl(m_Value(X), m_APInt(K))) ||
+ !match(ICmp, m_ICmp(Pred, m_Value(AndVal), m_Zero())))
+ return nullptr;
+
+ const APInt *C;
+ if (Pred != ICmpInst::ICMP_EQ ||
+ !match(AndVal, m_And(m_Specific(X), m_APInt(C))))
+ return nullptr;
+
+ if (!C->isMask() || (int64_t)C->countLeadingZeros() != K->getSExtValue())
+ return nullptr;
+
+ return FalseVal;
+}
+
static Value *simplifyCmpSelOfMaxMin(Value *CmpLHS, Value *CmpRHS,
ICmpInst::Predicate Pred, Value *TVal,
Value *FVal) {
@@ -4484,6 +4510,9 @@
Value *FalseVal,
const SimplifyQuery &Q,
unsigned MaxRecurse) {
+ if (Value *V = simplifySelectICmpAndZeroShl(CondVal, TrueVal, FalseVal))
+ return V;
+
ICmpInst::Predicate Pred;
Value *CmpLHS, *CmpRHS;
if (!match(CondVal, m_ICmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS))))
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148420.514004.patch
Type: text/x-patch
Size: 2902 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230416/24e57d39/attachment.bin>
More information about the llvm-commits
mailing list