Index: llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -1885,6 +1885,24 @@ return new ZExtInst(And, Ty); } } + + const APInt *C1, *C2, *C3 = C; + Value *X; + if (C3->isPowerOf2() && + match(Op0, + m_OneUse(m_LShr(m_Shl(m_Power2(C1), m_Value(X)), m_APInt(C2))))) { + unsigned Log2C1 = C1->countTrailingZeros(); + unsigned Log2C3 = C3->countTrailingZeros(); + unsigned LShrC = Log2C3 + C2->getZExtValue(); + if (LShrC < Width) { + // iff C1,C3 is pow2 and C2 + cttz(C3) < BitWidth: + // ((C1 << X) >> C2) & C3 -> X == (cttz(C3)+C2-cttz(C1)) ? C3 : 0 + unsigned CmpC = LShrC - Log2C1; + Value *Cmp = Builder.CreateICmpEQ(X, ConstantInt::get(Ty, CmpC)); + return SelectInst::Create(Cmp, ConstantInt::get(Ty, *C3), + ConstantInt::getNullValue(Ty)); + } + } } if (match(&I, m_And(m_OneUse(m_Shl(m_ZExt(m_Value(X)), m_Value(Y))), Index: llvm/test/Transforms/InstCombine/and.ll =================================================================== --- llvm/test/Transforms/InstCombine/and.ll +++ llvm/test/Transforms/InstCombine/and.ll @@ -1622,11 +1622,11 @@ ret i8 %r } +; CTTZ(ShlC) < LShrC define i16 @shl_lshr_pow2_const_case1(i16 %x) { ; CHECK-LABEL: @shl_lshr_pow2_const_case1( -; CHECK-NEXT: [[SHL:%.*]] = shl i16 4, [[X:%.*]] -; CHECK-NEXT: [[LSHR:%.*]] = lshr i16 [[SHL]], 6 -; CHECK-NEXT: [[R:%.*]] = and i16 [[LSHR]], 8 +; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i16 [[X:%.*]], 7 +; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i16 8, i16 0 ; CHECK-NEXT: ret i16 [[R]] ; %shl = shl i16 4, %x @@ -1635,6 +1635,19 @@ ret i16 %r } +; LShrC < CTTZ(ShlC) < LShrC + CTTZ(AndC) +define i16 @shl_lshr_pow2_const_case2(i16 %x) { +; CHECK-LABEL: @shl_lshr_pow2_const_case2( +; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i16 [[X:%.*]], 2 +; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i16 8, i16 0 +; CHECK-NEXT: ret i16 [[R]] +; + %shl = shl i16 16, %x + %lshr = lshr i16 %shl, 3 + %r = and i16 %lshr, 8 + ret i16 %r +} + define i16 @shl_lshr_pow2_const_negative_oneuse(i16 %x) { ; CHECK-LABEL: @shl_lshr_pow2_const_negative_oneuse( ; CHECK-NEXT: [[SHL:%.*]] = shl i16 4, [[X:%.*]] Index: llvm/test/Transforms/InstCombine/icmp-and-shift.ll =================================================================== --- llvm/test/Transforms/InstCombine/icmp-and-shift.ll +++ llvm/test/Transforms/InstCombine/icmp-and-shift.ll @@ -57,11 +57,9 @@ define i32 @icmp_eq_and_pow2_shl_pow2(i32 %0) { ; CHECK-LABEL: @icmp_eq_and_pow2_shl_pow2( -; CHECK-NEXT: [[SHL:%.*]] = shl i32 2, [[TMP0:%.*]] -; CHECK-NEXT: [[AND:%.*]] = lshr i32 [[SHL]], 4 -; CHECK-NEXT: [[AND_LOBIT:%.*]] = and i32 [[AND]], 1 -; CHECK-NEXT: [[TMP2:%.*]] = xor i32 [[AND_LOBIT]], 1 -; CHECK-NEXT: ret i32 [[TMP2]] +; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i32 [[TMP0:%.*]], 3 +; CHECK-NEXT: [[TMP3:%.*]] = zext i1 [[TMP2]] to i32 +; CHECK-NEXT: ret i32 [[TMP3]] ; %shl = shl i32 2, %0 %and = and i32 %shl, 16 @@ -72,11 +70,9 @@ define <2 x i32> @icmp_eq_and_pow2_shl_pow2_vec(<2 x i32> %0) { ; CHECK-LABEL: @icmp_eq_and_pow2_shl_pow2_vec( -; CHECK-NEXT: [[SHL:%.*]] = shl <2 x i32> , [[TMP0:%.*]] -; CHECK-NEXT: [[AND:%.*]] = lshr <2 x i32> [[SHL]], -; CHECK-NEXT: [[AND_LOBIT:%.*]] = and <2 x i32> [[AND]], -; CHECK-NEXT: [[TMP2:%.*]] = xor <2 x i32> [[AND_LOBIT]], -; CHECK-NEXT: ret <2 x i32> [[TMP2]] +; CHECK-NEXT: [[TMP2:%.*]] = icmp ne <2 x i32> [[TMP0:%.*]], +; CHECK-NEXT: [[TMP3:%.*]] = zext <2 x i1> [[TMP2]] to <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[TMP3]] ; %shl = shl <2 x i32> , %0 %and = and <2 x i32> %shl, @@ -87,9 +83,8 @@ define i32 @icmp_ne_and_pow2_shl_pow2(i32 %0) { ; CHECK-LABEL: @icmp_ne_and_pow2_shl_pow2( -; CHECK-NEXT: [[SHL:%.*]] = shl i32 2, [[TMP0:%.*]] -; CHECK-NEXT: [[AND:%.*]] = lshr i32 [[SHL]], 4 -; CHECK-NEXT: [[AND_LOBIT:%.*]] = and i32 [[AND]], 1 +; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i32 [[TMP0:%.*]], 3 +; CHECK-NEXT: [[AND_LOBIT:%.*]] = zext i1 [[TMP2]] to i32 ; CHECK-NEXT: ret i32 [[AND_LOBIT]] ; %shl = shl i32 2, %0 @@ -101,9 +96,8 @@ define <2 x i32> @icmp_ne_and_pow2_shl_pow2_vec(<2 x i32> %0) { ; CHECK-LABEL: @icmp_ne_and_pow2_shl_pow2_vec( -; CHECK-NEXT: [[SHL:%.*]] = shl <2 x i32> , [[TMP0:%.*]] -; CHECK-NEXT: [[AND:%.*]] = lshr <2 x i32> [[SHL]], -; CHECK-NEXT: [[AND_LOBIT:%.*]] = and <2 x i32> [[AND]], +; CHECK-NEXT: [[TMP2:%.*]] = icmp eq <2 x i32> [[TMP0:%.*]], +; CHECK-NEXT: [[AND_LOBIT:%.*]] = zext <2 x i1> [[TMP2]] to <2 x i32> ; CHECK-NEXT: ret <2 x i32> [[AND_LOBIT]] ; %shl = shl <2 x i32> , %0