From 65aedc1a4edc146c1368a0f4e8d67ff6026a38e5 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 13 Nov 2004 19:31:40 +0000 Subject: [PATCH] Fold: (X + (X << C2)) --> X * ((1 << C2) + 1) ((X << C2) + X) --> X * ((1 << C2) + 1) This means that we now canonicalize "Y+Y+Y" into: %tmp.2 = mul long %Y, 3 ; [#uses=1] instead of: %tmp.10 = shl long %Y, ubyte 1 ; [#uses=1] %tmp.6 = add long %Y, %tmp.10 ; [#uses=1] git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@17701 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/InstructionCombining.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 6689039fbe3..3f746a59ee3 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -618,6 +618,19 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2))) return R; + // (X + (X << C2)) --> X * ((1 << C2) + 1) + // ((X << C2) + X) --> X * ((1 << C2) + 1) + Value *Tmp; + if ((RHS->hasOneUse() && match(RHS, m_Shl(m_Value(Tmp), + m_ConstantInt(C2))) && LHS == Tmp)|| + (LHS->hasOneUse() && match(LHS, m_Shl(m_Value(Tmp), + m_ConstantInt(C2))) && RHS == Tmp)){ + ConstantInt *One = ConstantInt::get(LHS->getType(), 1); + Constant *NewRHS = + ConstantExpr::getAdd(ConstantExpr::getShl(One, C2), One); + return BinaryOperator::createMul(Tmp, NewRHS); + } + if (ConstantInt *CRHS = dyn_cast(RHS)) { Value *X; if (match(LHS, m_Not(m_Value(X)))) { // ~X + C --> (C-1) - X