Index: lib/Transforms/InstCombine/InstCombineSelect.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineSelect.cpp +++ lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -1480,6 +1480,38 @@ } } + /* + Try to fold a select of bittest. There are two patterns: + %2 = and i32 %X, %Y # Mask A + %3 = icmp eq i32 %2, 0 + %4 = and i32 %X, 1 # Mask B + %5 = select i1 %3, i32 %4, i32 1 + or + %2 = and i32 %X, %Y # Mask A + %3 = icmp eq i32 %2, 0 + %S = lshr i32 %X, C # C u< 32 + %5 = and i32 %S, 1 # Mask B + %6 = select i1 %3, i32 %5, i32 1 + */ + Value *X, *Y; + uint64_t C = 0; + ICmpInst::Predicate EqPred; + if (match(CondVal, m_ICmp(EqPred, m_And(m_Value(X), m_Value(Y)), m_Zero())) && + ICmpInst::Predicate::ICMP_EQ == EqPred && match(FalseVal, m_One())) { + // The TrueVal can be one of: + // and (lshr %X, C), 1 + // and %X, 1 (in which case C is 0) + if (match(TrueVal, m_And(m_Value(X), m_One())) || + match(TrueVal, m_And(m_LShr(m_Value(X), m_ConstantInt(C)), m_One()))) { + // These will be folded by this very pass right away. + Value *MaskB = Builder.CreateShl(ConstantInt::get(SelType, 1), C); + Value *NewMask = Builder.CreateOr(Y, MaskB); + Value *MaskedX = Builder.CreateAnd(X, NewMask); + Value *ICmpNeZero = Builder.CreateIsNotNull(MaskedX); + return new ZExtInst(ICmpNeZero, SelType); + } + } + // See if we are selecting two values based on a comparison of the two values. if (FCmpInst *FCI = dyn_cast(CondVal)) { if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) { Index: test/Transforms/InstCombine/select-of-bittest.ll =================================================================== --- test/Transforms/InstCombine/select-of-bittest.ll +++ test/Transforms/InstCombine/select-of-bittest.ll @@ -8,12 +8,9 @@ define i32 @f_m2(i32) { ; CHECK-LABEL: @f_m2( -; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 1 -; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 0 -; CHECK-NEXT: [[TMP4:%.*]] = lshr i32 [[TMP0]], 1 -; CHECK-NEXT: [[TMP5:%.*]] = and i32 [[TMP4]], 1 -; CHECK-NEXT: [[TMP6:%.*]] = select i1 [[TMP3]], i32 [[TMP5]], i32 1 -; CHECK-NEXT: ret i32 [[TMP6]] +; CHECK-NEXT: [[TMP2:%.*]] = lshr i32 [[TMP0:%.*]], 1 +; CHECK-NEXT: [[TMP3:%.*]] = and i32 [[TMP2]], 1 +; CHECK-NEXT: ret i32 [[TMP3]] ; %2 = and i32 %0, 1 %3 = icmp eq i32 %2, 0 @@ -25,12 +22,9 @@ define <2 x i32> @f_m2_vec(<2 x i32>) { ; CHECK-LABEL: @f_m2_vec( -; CHECK-NEXT: [[TMP2:%.*]] = and <2 x i32> [[TMP0:%.*]], -; CHECK-NEXT: [[TMP3:%.*]] = icmp eq <2 x i32> [[TMP2]], zeroinitializer -; CHECK-NEXT: [[TMP4:%.*]] = lshr <2 x i32> [[TMP0]], -; CHECK-NEXT: [[TMP5:%.*]] = and <2 x i32> [[TMP4]], -; CHECK-NEXT: [[TMP6:%.*]] = select <2 x i1> [[TMP3]], <2 x i32> [[TMP5]], <2 x i32> -; CHECK-NEXT: ret <2 x i32> [[TMP6]] +; CHECK-NEXT: [[TMP2:%.*]] = lshr <2 x i32> [[TMP0:%.*]], +; CHECK-NEXT: [[TMP3:%.*]] = and <2 x i32> [[TMP2]], +; CHECK-NEXT: ret <2 x i32> [[TMP3]] ; %2 = and <2 x i32> %0, %3 = icmp eq <2 x i32> %2, @@ -59,11 +53,10 @@ define i32 @f_m3(i32) { ; CHECK-LABEL: @f_m3( -; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 2 -; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 0 -; CHECK-NEXT: [[TMP4:%.*]] = and i32 [[TMP0]], 1 -; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[TMP3]], i32 [[TMP4]], i32 1 -; CHECK-NEXT: ret i32 [[TMP5]] +; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 3 +; CHECK-NEXT: [[TMP3:%.*]] = icmp ne i32 [[TMP2]], 0 +; CHECK-NEXT: [[TMP4:%.*]] = zext i1 [[TMP3]] to i32 +; CHECK-NEXT: ret i32 [[TMP4]] ; %2 = and i32 %0, 2 %3 = icmp eq i32 %2, 0 @@ -74,11 +67,10 @@ define <2 x i32> @f_m3_vec(<2 x i32>) { ; CHECK-LABEL: @f_m3_vec( -; CHECK-NEXT: [[TMP2:%.*]] = and <2 x i32> [[TMP0:%.*]], -; CHECK-NEXT: [[TMP3:%.*]] = icmp eq <2 x i32> [[TMP2]], zeroinitializer -; CHECK-NEXT: [[TMP4:%.*]] = and <2 x i32> [[TMP0]], -; CHECK-NEXT: [[TMP5:%.*]] = select <2 x i1> [[TMP3]], <2 x i32> [[TMP4]], <2 x i32> -; CHECK-NEXT: ret <2 x i32> [[TMP5]] +; CHECK-NEXT: [[TMP2:%.*]] = and <2 x i32> [[TMP0:%.*]], +; CHECK-NEXT: [[TMP3:%.*]] = icmp ne <2 x i32> [[TMP2]], zeroinitializer +; CHECK-NEXT: [[TMP4:%.*]] = zext <2 x i1> [[TMP3]] to <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[TMP4]] ; %2 = and <2 x i32> %0, %3 = icmp eq <2 x i32> %2, @@ -104,12 +96,10 @@ define i32 @f_m6(i32) { ; CHECK-LABEL: @f_m6( -; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 3 -; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 0 -; CHECK-NEXT: [[TMP4:%.*]] = lshr i32 [[TMP0]], 1 -; CHECK-NEXT: [[TMP5:%.*]] = and i32 [[TMP4]], 1 -; CHECK-NEXT: [[TMP6:%.*]] = select i1 [[TMP3]], i32 [[TMP5]], i32 1 -; CHECK-NEXT: ret i32 [[TMP6]] +; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 6 +; CHECK-NEXT: [[TMP3:%.*]] = icmp ne i32 [[TMP2]], 0 +; CHECK-NEXT: [[TMP4:%.*]] = zext i1 [[TMP3]] to i32 +; CHECK-NEXT: ret i32 [[TMP4]] ; %2 = and i32 %0, 3 %3 = icmp eq i32 %2, 0 @@ -121,12 +111,10 @@ define i32 @f_m10(i32) { ; CHECK-LABEL: @f_m10( -; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 4 -; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 0 -; CHECK-NEXT: [[TMP4:%.*]] = lshr i32 [[TMP0]], 1 -; CHECK-NEXT: [[TMP5:%.*]] = and i32 [[TMP4]], 1 -; CHECK-NEXT: [[TMP6:%.*]] = select i1 [[TMP3]], i32 [[TMP5]], i32 1 -; CHECK-NEXT: ret i32 [[TMP6]] +; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 10 +; CHECK-NEXT: [[TMP3:%.*]] = icmp ne i32 [[TMP2]], 0 +; CHECK-NEXT: [[TMP4:%.*]] = zext i1 [[TMP3]] to i32 +; CHECK-NEXT: ret i32 [[TMP4]] ; %2 = and i32 %0, 4 %3 = icmp eq i32 %2, 0 @@ -138,12 +126,10 @@ define i32 @f_m12(i32) { ; CHECK-LABEL: @f_m12( -; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 2 -; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 0 -; CHECK-NEXT: [[TMP4:%.*]] = lshr i32 [[TMP0]], 2 -; CHECK-NEXT: [[TMP5:%.*]] = and i32 [[TMP4]], 1 -; CHECK-NEXT: [[TMP6:%.*]] = select i1 [[TMP3]], i32 [[TMP5]], i32 1 -; CHECK-NEXT: ret i32 [[TMP6]] +; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP0:%.*]], 12 +; CHECK-NEXT: [[TMP3:%.*]] = icmp ne i32 [[TMP2]], 0 +; CHECK-NEXT: [[TMP4:%.*]] = zext i1 [[TMP3]] to i32 +; CHECK-NEXT: ret i32 [[TMP4]] ; %2 = and i32 %0, 2 %3 = icmp eq i32 %2, 0