diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -674,7 +674,6 @@ Value *CmpLHS = IC->getOperand(0); Value *CmpRHS = IC->getOperand(1); - Value *V; unsigned C1Log; bool IsEqualZero; bool NeedAnd = false; @@ -686,12 +685,10 @@ if (!match(CmpLHS, m_And(m_Value(), m_Power2(C1)))) return nullptr; - V = CmpLHS; C1Log = C1->logBase2(); IsEqualZero = IC->getPredicate() == ICmpInst::ICMP_EQ; } else { - // We also need to recognize (icmp slt (trunc (X)), 0) and - // (icmp sgt (trunc (X)), -1). + // We also need to recognize (icmp slt X, 0) and (icmp sgt X, -1). if (IC->getPredicate() == ICmpInst::ICMP_SGT && match(CmpRHS, m_AllOnes())) IsEqualZero = true; if (IC->getPredicate() == ICmpInst::ICMP_SLT && match(CmpRHS, m_Zero())) @@ -699,14 +696,11 @@ else return nullptr; - if (!match(CmpLHS, m_OneUse(m_Trunc(m_Value(V))))) - return nullptr; - C1Log = CmpLHS->getType()->getScalarSizeInBits() - 1; NeedAnd = true; } - Value *Or, *Y; + Value *Or, *Y, *V = CmpLHS; const APInt *C2; bool NeedXor; if (match(FalseVal, m_Or(m_Specific(TrueVal), m_Power2(C2)))) { @@ -728,7 +722,7 @@ V->getType()->getScalarSizeInBits(); // Make sure we don't create more instructions than we save. - if ((NeedShift + NeedXor + NeedZExtTrunc) > + if ((NeedShift + NeedXor + NeedZExtTrunc + NeedAnd) > (IC->hasOneUse() + Or->hasOneUse())) return nullptr; diff --git a/llvm/test/Transforms/InstCombine/select-with-bitwise-ops.ll b/llvm/test/Transforms/InstCombine/select-with-bitwise-ops.ll --- a/llvm/test/Transforms/InstCombine/select-with-bitwise-ops.ll +++ b/llvm/test/Transforms/InstCombine/select-with-bitwise-ops.ll @@ -760,9 +760,9 @@ define i8 @test70(i8 %x, i8 %y) { ; CHECK-LABEL: @test70( -; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 0 -; CHECK-NEXT: [[OR:%.*]] = or i8 [[Y:%.*]], 2 -; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], i8 [[OR]], i8 [[Y]] +; CHECK-NEXT: [[TMP1:%.*]] = lshr i8 [[X:%.*]], 6 +; CHECK-NEXT: [[TMP2:%.*]] = and i8 [[TMP1]], 2 +; CHECK-NEXT: [[SELECT:%.*]] = or i8 [[TMP2]], [[Y:%.*]] ; CHECK-NEXT: ret i8 [[SELECT]] ; %cmp = icmp slt i8 %x, 0