Index: llvm/lib/Analysis/InstructionSimplify.cpp =================================================================== --- llvm/lib/Analysis/InstructionSimplify.cpp +++ llvm/lib/Analysis/InstructionSimplify.cpp @@ -4392,6 +4392,39 @@ return nullptr; } +/// This simplifies: +/// select (icmp eq (and X, C), 0), 0, (shl X, K) +/// or +/// select (icmp ne (and X, C), 0), (shl X, K), 0 +/// 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; + Value *AndVal; + if (!match(ICmp, m_ICmp(Pred, m_Value(AndVal), m_Zero()))) + return nullptr; + + const APInt *K; + Value *X; + if ((Pred == ICmpInst::ICMP_EQ && match(TrueVal, m_Zero()) && + match(FalseVal, m_Shl(m_Value(X), m_APInt(K)))) || + (Pred == ICmpInst::ICMP_NE && match(FalseVal, m_Zero()) && + match(TrueVal, m_Shl(m_Value(X), m_APInt(K))))) { + const APInt *C; + if (!match(AndVal, m_And(m_Specific(X), m_APInt(C)))) + return nullptr; + + if (!C->isMask() || (int64_t)C->countLeadingZeros() != K->getSExtValue()) + return nullptr; + + return Pred == ICmpInst::ICMP_EQ ? FalseVal : TrueVal; + } + + return nullptr; +} + static Value *simplifyCmpSelOfMaxMin(Value *CmpLHS, Value *CmpRHS, ICmpInst::Predicate Pred, Value *TVal, Value *FVal) { @@ -4484,6 +4517,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)))) Index: llvm/test/Transforms/InstSimplify/select.ll =================================================================== --- llvm/test/Transforms/InstSimplify/select.ll +++ llvm/test/Transforms/InstSimplify/select.ll @@ -1347,11 +1347,8 @@ define i32 @select_icmp_and_shl(i32 %x) { ; CHECK-LABEL: @select_icmp_and_shl( -; CHECK-NEXT: [[SHL_MASK:%.*]] = and i32 [[X:%.*]], 1073741823 -; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i32 [[SHL_MASK]], 0 -; CHECK-NEXT: [[MUL:%.*]] = shl i32 [[X]], 2 -; CHECK-NEXT: [[COND:%.*]] = select i1 [[TOBOOL_NOT]], i32 0, i32 [[MUL]] -; CHECK-NEXT: ret i32 [[COND]] +; 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 @@ -1362,11 +1359,8 @@ define <2 x i32> @select_icmp_and_shl_vect(<2 x i32> %x) { ; CHECK-LABEL: @select_icmp_and_shl_vect( -; CHECK-NEXT: [[SHL_MASK:%.*]] = and <2 x i32> [[X:%.*]], -; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq <2 x i32> [[SHL_MASK]], zeroinitializer -; CHECK-NEXT: [[MUL:%.*]] = shl <2 x i32> [[X]], -; CHECK-NEXT: [[COND:%.*]] = select <2 x i1> [[TOBOOL_NOT]], <2 x i32> zeroinitializer, <2 x i32> [[MUL]] -; CHECK-NEXT: ret <2 x i32> [[COND]] +; CHECK-NEXT: [[MUL:%.*]] = shl <2 x i32> [[X:%.*]], +; CHECK-NEXT: ret <2 x i32> [[MUL]] ; %shl.mask = and <2 x i32> %x, %tobool.not = icmp eq <2 x i32> %shl.mask, zeroinitializer @@ -1377,11 +1371,8 @@ define i32 @select_icmp_and_shl2(i32 %x) { ; CHECK-LABEL: @select_icmp_and_shl2( -; CHECK-NEXT: [[SHL_MASK:%.*]] = and i32 [[X:%.*]], 1073741823 -; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp ne i32 [[SHL_MASK]], 0 -; CHECK-NEXT: [[MUL:%.*]] = shl i32 [[X]], 2 -; CHECK-NEXT: [[COND:%.*]] = select i1 [[TOBOOL_NOT]], i32 [[MUL]], i32 0 -; CHECK-NEXT: ret i32 [[COND]] +; CHECK-NEXT: [[MUL:%.*]] = shl i32 [[X:%.*]], 2 +; CHECK-NEXT: ret i32 [[MUL]] ; %shl.mask = and i32 %x, 1073741823 %tobool.not = icmp ne i32 %shl.mask, 0 @@ -1392,11 +1383,8 @@ define <2 x i32> @select_icmp_and_shl2_vect(<2 x i32> %x) { ; CHECK-LABEL: @select_icmp_and_shl2_vect( -; CHECK-NEXT: [[SHL_MASK:%.*]] = and <2 x i32> [[X:%.*]], -; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp ne <2 x i32> [[SHL_MASK]], zeroinitializer -; CHECK-NEXT: [[MUL:%.*]] = shl <2 x i32> [[X]], -; CHECK-NEXT: [[COND:%.*]] = select <2 x i1> [[TOBOOL_NOT]], <2 x i32> [[MUL]], <2 x i32> zeroinitializer -; CHECK-NEXT: ret <2 x i32> [[COND]] +; CHECK-NEXT: [[MUL:%.*]] = shl <2 x i32> [[X:%.*]], +; CHECK-NEXT: ret <2 x i32> [[MUL]] ; %shl.mask = and <2 x i32> %x, %tobool.not = icmp ne <2 x i32> %shl.mask, zeroinitializer