Index: lib/Transforms/InstCombine/InstCombineAddSub.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineAddSub.cpp +++ lib/Transforms/InstCombine/InstCombineAddSub.cpp @@ -871,22 +871,25 @@ // Otherwise, return null. // static inline Value *dyn_castFoldableMul(Value *V, Constant *&CST) { - if (!V->hasOneUse() || !V->getType()->isIntOrIntVectorTy()) + if (!V->getType()->isIntOrIntVectorTy()) return nullptr; - Instruction *I = dyn_cast(V); - if (!I) return nullptr; - - if (I->getOpcode() == Instruction::Mul) - if ((CST = dyn_cast(I->getOperand(1)))) - return I->getOperand(0); - if (I->getOpcode() == Instruction::Shl) - if ((CST = dyn_cast(I->getOperand(1)))) { - // The multiplier is really 1 << CST. - CST = ConstantExpr::getShl(ConstantInt::get(V->getType(), 1), CST); - return I->getOperand(0); + if (V->hasOneUse()) { + if (Instruction *I = dyn_cast(V)) { + if (I->getOpcode() == Instruction::Mul) + if ((CST = dyn_cast(I->getOperand(1)))) + return I->getOperand(0); + if (I->getOpcode() == Instruction::Shl) + if ((CST = dyn_cast(I->getOperand(1)))) { + // The multiplier is really 1 << CST. + CST = ConstantExpr::getShl(ConstantInt::get(V->getType(), 1), CST); + return I->getOperand(0); + } } - return nullptr; + } + + CST = ConstantInt::get(V->getType(), 1); + return V; } // If one of the operands only has one non-zero bit, and if the other @@ -923,6 +926,22 @@ // There are different heuristics we can use for this. Here are some simple // ones. + // if both are constant, we can simply add them and check for sign change. Add + // of opposite signed numbers will never overflow. + if (ConstantInt *CI1 = dyn_cast(LHS)) + if (ConstantInt *CI2 = dyn_cast(RHS)) { + APInt ACI1 = CI1->getValue(); + APInt ACI2 = CI2->getValue(); + bool IsLHSNegative = ACI1.isNegative(); + bool IsRHSNegative = ACI2.isNegative(); + bool IsResultNegative = (ACI1 + ACI2).isNegative(); + + if ((IsLHSNegative != IsRHSNegative) || + (IsLHSNegative == IsResultNegative)) + return true; + return false; + } + // If LHS and RHS each have at least two sign bits, the addition will look // like // @@ -1074,22 +1093,22 @@ if (Value *V = dyn_castNegVal(RHS)) return BinaryOperator::CreateSub(LHS, V); - { - Constant *C2; - if (Value *X = dyn_castFoldableMul(LHS, C2)) { - if (X == RHS) // X*C + X --> X * (C+1) - return BinaryOperator::CreateMul(RHS, AddOne(C2)); - - // X*C1 + X*C2 --> X * (C1+C2) - Constant *C1; - if (X == dyn_castFoldableMul(RHS, C1)) - return BinaryOperator::CreateMul(X, ConstantExpr::getAdd(C1, C2)); - } - - // X + X*C --> X * (C+1) - if (dyn_castFoldableMul(RHS, C2) == LHS) - return BinaryOperator::CreateMul(LHS, AddOne(C2)); + Constant *C1, *C2; + // X * C1 + X * C2 --> X * (C1 + C2) + if (Value *X = dyn_castFoldableMul(LHS, C1)) + if (Value *Y = dyn_castFoldableMul(RHS, C2)) { + if ((X == Y) && WillNotOverflowSignedAdd(C1, C2)) { + if (BinaryOperator *NewInst = + BinaryOperator::CreateMul(X, ConstantExpr::getAdd(C1, C2))) { + + NewInst->setHasNoSignedWrap(I.hasNoSignedWrap()); + + // TODO: Check for unsigned wrap + return NewInst; + } + } + } } // A+B --> A|B iff A and B have no bits set in common. @@ -1571,15 +1590,12 @@ } } - Constant *C1; - if (Value *X = dyn_castFoldableMul(Op0, C1)) { - if (X == Op1) // X*C - X --> X * (C-1) - return BinaryOperator::CreateMul(Op1, SubOne(C1)); - - Constant *C2; // X*C1 - X*C2 -> X * (C1-C2) - if (X == dyn_castFoldableMul(Op1, C2)) - return BinaryOperator::CreateMul(X, ConstantExpr::getSub(C1, C2)); - } + Constant *C1, *C2; + // X * C1 - X * C2 --> X * (C1 - C2) + if (Value *X = dyn_castFoldableMul(Op0, C1)) + if (Value *Y = dyn_castFoldableMul(Op1, C2)) + if (X == Y) + return BinaryOperator::CreateMul(X, ConstantExpr::getSub(C1, C2)); // Optimize pointer differences into the same array into a size. Consider: // &A[10] - &A[0]: we should compile this to "10". Index: test/Transforms/InstCombine/add2.ll =================================================================== --- test/Transforms/InstCombine/add2.ll +++ test/Transforms/InstCombine/add2.ll @@ -76,3 +76,30 @@ ; CHECK-NEXT: %add = sub <2 x i64> , %A ; CHECK-NEXT: ret <2 x i64> %add } + +define i32 @add_nsw_mul_nsw(i32 %x) { + %add1 = add nsw i32 %x, %x + %add2 = add nsw i32 %add1, %x + ret i32 %add2 +; CHECK-LABEL: @add_nsw_mul_nsw( +; CHECK-NEXT: %add2 = mul nsw i32 %x, 3 +; CHECK-NEXT: ret i32 %add2 +} + +define i32 @mul_add_to_mul_1(i32 %x) { + %mul1 = mul nsw i32 %x, 8 + %add2 = add nsw i32 %x, %mul1 + ret i32 %add2 +; CHECK-LABEL: @mul_add_to_mul_1( +; CHECK-NEXT: %add2 = mul nsw i32 %x, 9 +; CHECK-NEXT: ret i32 %add2 +} + +define i32 @mul_add_to_mul_2(i32 %x) { + %mul1 = mul nsw i32 %x, 8 + %add2 = add nsw i32 %mul1, %x + ret i32 %add2 +; CHECK-LABEL: @mul_add_to_mul_2( +; CHECK-NEXT: %add2 = mul nsw i32 %x, 9 +; CHECK-NEXT: ret i32 %add2 +} \ No newline at end of file