Index: lib/Transforms/InstCombine/InstCombineMulDivRem.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -891,6 +891,7 @@ /// @brief Common integer divide transforms Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) { Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); + bool IsSigned = I.getOpcode() == Instruction::SDiv; // The RHS is known non-zero. if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I)) { @@ -908,7 +909,6 @@ if (match(Op1, m_APInt(C2))) { Value *X; const APInt *C1; - bool IsSigned = I.getOpcode() == Instruction::SDiv; // (X / C1) / C2 -> X / (C1*C2) if ((IsSigned && match(LHS, m_SDiv(m_Value(X), m_APInt(C1)))) || @@ -999,12 +999,21 @@ return &I; // (X - (X rem Y)) / Y -> X / Y; usually originates as ((X / Y) * Y) / Y - Value *X = nullptr, *Z = nullptr; - if (match(Op0, m_Sub(m_Value(X), m_Value(Z)))) { // (X - Z) / Y; Y = Op1 - bool isSigned = I.getOpcode() == Instruction::SDiv; - if ((isSigned && match(Z, m_SRem(m_Specific(X), m_Specific(Op1)))) || - (!isSigned && match(Z, m_URem(m_Specific(X), m_Specific(Op1))))) + Value *X, *Z; + if (match(Op0, m_Sub(m_Value(X), m_Value(Z)))) // (X - Z) / Y; Y = Op1 + if ((IsSigned && match(Z, m_SRem(m_Specific(X), m_Specific(Op1)))) || + (!IsSigned && match(Z, m_URem(m_Specific(X), m_Specific(Op1))))) return BinaryOperator::Create(I.getOpcode(), X, Op1); + + // (X << Y) / X -> 1 << Y + Value *Y; + if (IsSigned && match(Op0, m_NSWShl(m_Specific(Op1), m_Value(Y)))) { + Constant *C = ConstantInt::get(Op1->getType(), 1); + return BinaryOperator::CreateNSWShl(C, Y); + } + if (!IsSigned && match(Op0, m_NUWShl(m_Specific(Op1), m_Value(Y)))) { + Constant *C = ConstantInt::get(Op1->getType(), 1); + return BinaryOperator::CreateNUWShl(C, Y); } return nullptr; Index: test/Transforms/InstCombine/div-shift.ll =================================================================== --- test/Transforms/InstCombine/div-shift.ll +++ test/Transforms/InstCombine/div-shift.ll @@ -4,8 +4,8 @@ define i32 @t1(i16 zeroext %x, i32 %y) { ; CHECK-LABEL: @t1( ; CHECK-NEXT: entry: -; CHECK-NEXT: [[CONV:%.*]] = zext i16 %x to i32 -; CHECK-NEXT: [[TMP0:%.*]] = add i32 %y, 1 +; CHECK-NEXT: [[CONV:%.*]] = zext i16 [[X:%.*]] to i32 +; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[Y:%.*]], 1 ; CHECK-NEXT: [[D:%.*]] = lshr i32 [[CONV]], [[TMP0]] ; CHECK-NEXT: ret i32 [[D]] ; @@ -34,8 +34,8 @@ ; rdar://11721329 define i64 @t2(i64 %x, i32 %y) { ; CHECK-LABEL: @t2( -; CHECK-NEXT: [[TMP1:%.*]] = zext i32 %y to i64 -; CHECK-NEXT: [[TMP2:%.*]] = lshr i64 %x, [[TMP1]] +; CHECK-NEXT: [[TMP1:%.*]] = zext i32 [[Y:%.*]] to i64 +; CHECK-NEXT: [[TMP2:%.*]] = lshr i64 [[X:%.*]], [[TMP1]] ; CHECK-NEXT: ret i64 [[TMP2]] ; %1 = shl i32 1, %y @@ -47,9 +47,9 @@ ; PR13250 define i64 @t3(i64 %x, i32 %y) { ; CHECK-LABEL: @t3( -; CHECK-NEXT: [[TMP1:%.*]] = add i32 %y, 2 +; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[Y:%.*]], 2 ; CHECK-NEXT: [[TMP2:%.*]] = zext i32 [[TMP1]] to i64 -; CHECK-NEXT: [[TMP3:%.*]] = lshr i64 %x, [[TMP2]] +; CHECK-NEXT: [[TMP3:%.*]] = lshr i64 [[X:%.*]], [[TMP2]] ; CHECK-NEXT: ret i64 [[TMP3]] ; %1 = shl i32 4, %y @@ -60,9 +60,9 @@ define i32 @t4(i32 %x, i32 %y) { ; CHECK-LABEL: @t4( -; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 %y, 5 -; CHECK-NEXT: [[DOTV:%.*]] = select i1 [[TMP1]], i32 %y, i32 5 -; CHECK-NEXT: [[TMP2:%.*]] = lshr i32 %x, [[DOTV]] +; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[Y:%.*]], 5 +; CHECK-NEXT: [[DOTV:%.*]] = select i1 [[TMP1]], i32 [[Y]], i32 5 +; CHECK-NEXT: [[TMP2:%.*]] = lshr i32 [[X:%.*]], [[DOTV]] ; CHECK-NEXT: ret i32 [[TMP2]] ; %1 = shl i32 1, %y @@ -74,9 +74,9 @@ define i32 @t5(i1 %x, i1 %y, i32 %V) { ; CHECK-LABEL: @t5( -; CHECK-NEXT: [[DOTV:%.*]] = select i1 %x, i32 5, i32 6 -; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 %V, [[DOTV]] -; CHECK-NEXT: [[TMP2:%.*]] = select i1 %y, i32 [[TMP1]], i32 0 +; CHECK-NEXT: [[DOTV:%.*]] = select i1 [[X:%.*]], i32 5, i32 6 +; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[V:%.*]], [[DOTV]] +; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[Y:%.*]], i32 [[TMP1]], i32 0 ; CHECK-NEXT: ret i32 [[TMP2]] ; %1 = shl i32 1, %V @@ -88,9 +88,9 @@ define i32 @t6(i32 %x, i32 %z) { ; CHECK-LABEL: @t6( -; CHECK-NEXT: [[X_IS_ZERO:%.*]] = icmp eq i32 %x, 0 -; CHECK-NEXT: [[DIVISOR:%.*]] = select i1 [[X_IS_ZERO]], i32 1, i32 %x -; CHECK-NEXT: [[Y:%.*]] = udiv i32 %z, [[DIVISOR]] +; CHECK-NEXT: [[X_IS_ZERO:%.*]] = icmp eq i32 [[X:%.*]], 0 +; CHECK-NEXT: [[DIVISOR:%.*]] = select i1 [[X_IS_ZERO]], i32 1, i32 [[X]] +; CHECK-NEXT: [[Y:%.*]] = udiv i32 [[Z:%.*]], [[DIVISOR]] ; CHECK-NEXT: ret i32 [[Y]] ; %x_is_zero = icmp eq i32 %x, 0 @@ -98,3 +98,107 @@ %y = udiv i32 %z, %divisor ret i32 %y } + +; (X << C1) / X -> 1 << C1 optimizations + +define i32 @t7(i32 %x) { +; CHECK-LABEL: @t7( +; CHECK-NEXT: ret i32 4 +; + %shl = shl nsw i32 %x, 2 + %r = sdiv i32 %shl, %x + ret i32 %r +} + +; make sure the previous opt doesn't take place for wrapped shifts + +define i32 @t8(i32 %x) { +; CHECK-LABEL: @t8( +; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[X:%.*]], 2 +; CHECK-NEXT: [[R:%.*]] = sdiv i32 [[SHL]], [[X]] +; CHECK-NEXT: ret i32 [[R]] +; + %shl = shl i32 %x, 2 + %r = sdiv i32 %shl, %x + ret i32 %r +} + +define <2 x i32> @t9(<2 x i32> %x) { +; CHECK-LABEL: @t9( +; CHECK-NEXT: ret <2 x i32> +; + %shl = shl nsw <2 x i32> %x, + %r = sdiv <2 x i32> %shl, %x + ret <2 x i32> %r +} + +define i32 @t10(i32 %x, i32 %y) { +; CHECK-LABEL: @t10( +; CHECK-NEXT: [[R:%.*]] = shl nsw i32 1, [[Y:%.*]] +; CHECK-NEXT: ret i32 [[R]] +; + %shl = shl nsw i32 %x, %y + %r = sdiv i32 %shl, %x + ret i32 %r +} + +define <2 x i32> @t11(<2 x i32> %x, <2 x i32> %y) { +; CHECK-LABEL: @t11( +; CHECK-NEXT: [[R:%.*]] = shl nsw <2 x i32> , [[Y:%.*]] +; CHECK-NEXT: ret <2 x i32> [[R]] +; + %shl = shl nsw <2 x i32> %x, %y + %r = sdiv <2 x i32> %shl, %x + ret <2 x i32> %r +} + +define i32 @t12(i32 %x) { +; CHECK-LABEL: @t12( +; CHECK-NEXT: ret i32 4 +; + %shl = shl nuw i32 %x, 2 + %r = udiv i32 %shl, %x + ret i32 %r +} + +; make sure the previous opt doesn't take place for wrapped shifts + +define i32 @t13(i32 %x) { +; CHECK-LABEL: @t13( +; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[X:%.*]], 2 +; CHECK-NEXT: [[R:%.*]] = udiv i32 [[SHL]], [[X]] +; CHECK-NEXT: ret i32 [[R]] +; + %shl = shl i32 %x, 2 + %r = udiv i32 %shl, %x + ret i32 %r +} + +define <2 x i32> @t14(<2 x i32> %x) { +; CHECK-LABEL: @t14( +; CHECK-NEXT: ret <2 x i32> +; + %shl = shl nuw <2 x i32> %x, + %r = udiv <2 x i32> %shl, %x + ret <2 x i32> %r +} + +define i32 @t15(i32 %x, i32 %y) { +; CHECK-LABEL: @t15( +; CHECK-NEXT: [[R:%.*]] = shl nuw i32 1, [[Y:%.*]] +; CHECK-NEXT: ret i32 [[R]] +; + %shl = shl nuw i32 %x, %y + %r = udiv i32 %shl, %x + ret i32 %r +} + +define <2 x i32> @t16(<2 x i32> %x, <2 x i32> %y) { +; CHECK-LABEL: @t16( +; CHECK-NEXT: [[R:%.*]] = shl nuw <2 x i32> , [[Y:%.*]] +; CHECK-NEXT: ret <2 x i32> [[R]] +; + %shl = shl nuw <2 x i32> %x, %y + %r = udiv <2 x i32> %shl, %x + ret <2 x i32> %r +}