Index: llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp @@ -1415,6 +1415,23 @@ return &I; } + // TODO: Match the related pattern with sub instead of inner add. + Value *X, *Y, *Z; + if (match(&I, m_c_Add(m_OneUse(m_Mul(m_Value(X), m_Value(Y))), + m_OneUse(m_c_Add(m_Deferred(X), m_Value(Z)))))) { + // (X * Y) + (X + Z) --> ((Y + 1) * X) + Z + Value *IncY = Builder.CreateAdd(Y, ConstantInt::get(I.getType(), 1)); + Value *NewMul = Builder.CreateMul(IncY, X); + return BinaryOperator::CreateAdd(NewMul, Z); + } + if (match(&I, m_c_Add(m_OneUse(m_Mul(m_Value(X), m_Value(Y))), + m_OneUse(m_c_Add(m_Deferred(Y), m_Value(Z)))))) { + // (X * Y) + (Y + Z) --> ((X + 1) * Y) + Z + Value *IncX = Builder.CreateAdd(X, ConstantInt::get(I.getType(), 1)); + Value *NewMul = Builder.CreateMul(IncX, Y); + return BinaryOperator::CreateAdd(NewMul, Z); + } + // TODO(jingyue): Consider willNotOverflowSignedAdd and // willNotOverflowUnsignedAdd to reduce the number of invocations of // computeKnownBits. Index: llvm/test/Transforms/InstCombine/add.ll =================================================================== --- llvm/test/Transforms/InstCombine/add.ll +++ llvm/test/Transforms/InstCombine/add.ll @@ -1758,9 +1758,9 @@ define i8 @mul_add_common_factor_commute1(i8 %x, i8 %y, i8 %z) { ; CHECK-LABEL: @mul_add_common_factor_commute1( -; CHECK-NEXT: [[M:%.*]] = mul i8 [[X:%.*]], [[Y:%.*]] -; CHECK-NEXT: [[A:%.*]] = add i8 [[X]], [[Z:%.*]] -; CHECK-NEXT: [[R:%.*]] = add i8 [[M]], [[A]] +; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[Y:%.*]], 1 +; CHECK-NEXT: [[TMP2:%.*]] = mul i8 [[TMP1]], [[X:%.*]] +; CHECK-NEXT: [[R:%.*]] = add i8 [[TMP2]], [[Z:%.*]] ; CHECK-NEXT: ret i8 [[R]] ; %m = mul i8 %x, %y @@ -1771,9 +1771,9 @@ define i8 @mul_add_common_factor_commute2(i8 %x, i8 %y, i8 %z) { ; CHECK-LABEL: @mul_add_common_factor_commute2( -; CHECK-NEXT: [[M:%.*]] = mul i8 [[Y:%.*]], [[X:%.*]] -; CHECK-NEXT: [[A:%.*]] = add i8 [[X]], [[Z:%.*]] -; CHECK-NEXT: [[R:%.*]] = add i8 [[M]], [[A]] +; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[Y:%.*]], 1 +; CHECK-NEXT: [[TMP2:%.*]] = mul i8 [[TMP1]], [[X:%.*]] +; CHECK-NEXT: [[R:%.*]] = add i8 [[TMP2]], [[Z:%.*]] ; CHECK-NEXT: ret i8 [[R]] ; %m = mul i8 %y, %x @@ -1784,9 +1784,8 @@ define i8 @mul_add_common_factor_commute3(i8 %x, i8 %y, i8 %z) { ; CHECK-LABEL: @mul_add_common_factor_commute3( -; CHECK-NEXT: [[M:%.*]] = mul i8 [[X:%.*]], 42 -; CHECK-NEXT: [[A:%.*]] = add i8 [[Z:%.*]], [[X]] -; CHECK-NEXT: [[R:%.*]] = add i8 [[M]], [[A]] +; CHECK-NEXT: [[TMP1:%.*]] = mul i8 [[X:%.*]], 43 +; CHECK-NEXT: [[R:%.*]] = add i8 [[TMP1]], [[Z:%.*]] ; CHECK-NEXT: ret i8 [[R]] ; %m = mul i8 %x, 42 @@ -1797,9 +1796,9 @@ define <2 x i8> @mul_add_common_factor_commute4(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z) { ; CHECK-LABEL: @mul_add_common_factor_commute4( -; CHECK-NEXT: [[M:%.*]] = mul <2 x i8> [[Y:%.*]], [[X:%.*]] -; CHECK-NEXT: [[A:%.*]] = add <2 x i8> [[Z:%.*]], [[X]] -; CHECK-NEXT: [[R:%.*]] = add <2 x i8> [[M]], [[A]] +; CHECK-NEXT: [[TMP1:%.*]] = add <2 x i8> [[Y:%.*]], +; CHECK-NEXT: [[TMP2:%.*]] = mul <2 x i8> [[TMP1]], [[X:%.*]] +; CHECK-NEXT: [[R:%.*]] = add <2 x i8> [[TMP2]], [[Z:%.*]] ; CHECK-NEXT: ret <2 x i8> [[R]] ; %m = mul <2 x i8> %y, %x @@ -1810,9 +1809,9 @@ define i8 @mul_add_common_factor_commute5(i8 %x, i8 %y, i8 %z) { ; CHECK-LABEL: @mul_add_common_factor_commute5( -; CHECK-NEXT: [[M:%.*]] = mul i8 [[X:%.*]], [[Y:%.*]] -; CHECK-NEXT: [[A:%.*]] = add i8 [[X]], 43 -; CHECK-NEXT: [[R:%.*]] = add i8 [[A]], [[M]] +; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[Y:%.*]], 1 +; CHECK-NEXT: [[TMP2:%.*]] = mul i8 [[TMP1]], [[X:%.*]] +; CHECK-NEXT: [[R:%.*]] = add i8 [[TMP2]], 43 ; CHECK-NEXT: ret i8 [[R]] ; %m = mul i8 %x, %y @@ -1823,9 +1822,9 @@ define i8 @mul_add_common_factor_commute6(i8 %x, i8 %y, i8 %z) { ; CHECK-LABEL: @mul_add_common_factor_commute6( -; CHECK-NEXT: [[M:%.*]] = mul i8 [[Y:%.*]], [[X:%.*]] -; CHECK-NEXT: [[A:%.*]] = add i8 [[X]], [[Z:%.*]] -; CHECK-NEXT: [[R:%.*]] = add i8 [[A]], [[M]] +; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[Y:%.*]], 1 +; CHECK-NEXT: [[TMP2:%.*]] = mul i8 [[TMP1]], [[X:%.*]] +; CHECK-NEXT: [[R:%.*]] = add i8 [[TMP2]], [[Z:%.*]] ; CHECK-NEXT: ret i8 [[R]] ; %m = mul i8 %y, %x @@ -1836,9 +1835,9 @@ define i8 @mul_add_common_factor_commute7(i8 %x, i8 %y, i8 %z) { ; CHECK-LABEL: @mul_add_common_factor_commute7( -; CHECK-NEXT: [[M:%.*]] = mul i8 [[X:%.*]], [[Y:%.*]] -; CHECK-NEXT: [[A:%.*]] = add i8 [[Z:%.*]], [[X]] -; CHECK-NEXT: [[R:%.*]] = add i8 [[A]], [[M]] +; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[Y:%.*]], 1 +; CHECK-NEXT: [[TMP2:%.*]] = mul i8 [[TMP1]], [[X:%.*]] +; CHECK-NEXT: [[R:%.*]] = add i8 [[TMP2]], [[Z:%.*]] ; CHECK-NEXT: ret i8 [[R]] ; %m = mul i8 %x, %y @@ -1849,9 +1848,9 @@ define i8 @mul_add_common_factor_commute8(i8 %x, i8 %y, i8 %z) { ; CHECK-LABEL: @mul_add_common_factor_commute8( -; CHECK-NEXT: [[M:%.*]] = mul i8 [[Y:%.*]], [[X:%.*]] -; CHECK-NEXT: [[A:%.*]] = add i8 [[Z:%.*]], [[X]] -; CHECK-NEXT: [[R:%.*]] = add i8 [[A]], [[M]] +; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[Y:%.*]], 1 +; CHECK-NEXT: [[TMP2:%.*]] = mul i8 [[TMP1]], [[X:%.*]] +; CHECK-NEXT: [[R:%.*]] = add i8 [[TMP2]], [[Z:%.*]] ; CHECK-NEXT: ret i8 [[R]] ; %m = mul i8 %y, %x @@ -1860,6 +1859,8 @@ ret i8 %r } +; negative test - extra use + define i8 @mul_add_common_factor_use1(i8 %x, i8 %y, i8 %z) { ; CHECK-LABEL: @mul_add_common_factor_use1( ; CHECK-NEXT: [[M:%.*]] = mul i8 [[X:%.*]], [[Y:%.*]] @@ -1875,6 +1876,8 @@ ret i8 %r } +; negative test - extra use + define i8 @mul_add_common_factor_use2(i8 %x, i8 %y, i8 %z) { ; CHECK-LABEL: @mul_add_common_factor_use2( ; CHECK-NEXT: [[M:%.*]] = mul i8 [[X:%.*]], [[Y:%.*]] Index: llvm/test/Transforms/PGOProfile/chr.ll =================================================================== --- llvm/test/Transforms/PGOProfile/chr.ll +++ llvm/test/Transforms/PGOProfile/chr.ll @@ -1004,7 +1004,7 @@ ; t1 = *j ; if ((t1 & 2) != 0) // Likely true ; foo() -; return (t1 * 42) - (t1 - 99) +; return (t1 * 42) + (t1 - 99) ; -> ; t0 = *i ; if ((t0 & 3) == 3) { // Likely true @@ -1020,7 +1020,7 @@ ; } ; } ; // A new phi for t1 is inserted here. -; return (t1 * 42) - (t1 - 99) +; return (t1 * 43) - 99 define i32 @test_chr_10(ptr %i, ptr %j) !prof !14 { ; CHECK-LABEL: @test_chr_10( ; CHECK-NEXT: entry: @@ -1050,10 +1050,9 @@ ; CHECK-NEXT: br label [[BB3]] ; CHECK: bb3: ; CHECK-NEXT: [[TMP8:%.*]] = phi i32 [ [[TMP3]], [[BB0]] ], [ [[TMP5]], [[BB2_NONCHR]] ], [ [[TMP5]], [[BB1_NONCHR]] ] -; CHECK-NEXT: [[TMP9:%.*]] = mul i32 [[TMP8]], 42 -; CHECK-NEXT: [[TMP10:%.*]] = add i32 [[TMP8]], -99 -; CHECK-NEXT: [[TMP11:%.*]] = add i32 [[TMP9]], [[TMP10]] -; CHECK-NEXT: ret i32 [[TMP11]] +; CHECK-NEXT: [[TMP9:%.*]] = mul i32 [[TMP8]], 43 +; CHECK-NEXT: [[TMP10:%.*]] = add i32 [[TMP9]], -99 +; CHECK-NEXT: ret i32 [[TMP10]] ; entry: %0 = load i32, ptr %i