Fold:
authorChris Lattner <sabre@nondot.org>
Sat, 13 Nov 2004 19:31:40 +0000 (19:31 +0000)
committerChris Lattner <sabre@nondot.org>
Sat, 13 Nov 2004 19:31:40 +0000 (19:31 +0000)
   (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         ; <long> [#uses=1]

instead of:

        %tmp.10 = shl long %Y, ubyte 1          ; <long> [#uses=1]
        %tmp.6 = add long %Y, %tmp.10               ; <long> [#uses=1]

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@17701 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/InstructionCombining.cpp

index 6689039fbe3d7032b2d90234649cda29f54994ac..3f746a59ee345d48c7c3596a26172e1da7831bb0 100644 (file)
@@ -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<ConstantInt>(RHS)) {
     Value *X;
     if (match(LHS, m_Not(m_Value(X)))) {   // ~X + C --> (C-1) - X