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)))) 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:%.*]], +; CHECK-NEXT: ret <2 x i32> [[MUL]] +; + %shl.mask = and <2 x i32> %x, + %tobool.not = icmp eq <2 x i32> %shl.mask, zeroinitializer + %mul = shl <2 x i32> %x, + %cond = select <2 x i1> %tobool.not, <2 x i32> zeroinitializer, <2 x i32> %mul + ret <2 x i32> %cond +}