[llvm-commits] [llvm] r46683 - /llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp

Nick Lewycky nicholas at mxc.ca
Sat Feb 2 23:42:09 PST 2008


Author: nicholas
Date: Sun Feb  3 01:42:09 2008
New Revision: 46683

URL: http://llvm.org/viewvc/llvm-project?rev=46683&view=rev
Log:
Fold away one multiply in instcombine. This would normally be caught in
reassociate anyways, but they could be generated during instcombine's run.

Modified:
    llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=46683&r1=46682&r2=46683&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Sun Feb  3 01:42:09 2008
@@ -2123,6 +2123,30 @@
     if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2)))
       return R;
 
+  // W*X + Y*Z --> W * (X+Z)  iff W == Y
+  if (I.getType()->isInteger()) {
+    Value *W, *X, *Y, *Z;
+    if (match(LHS, m_Mul(m_Value(W), m_Value(X))) &&
+        match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) {
+      if (W != Y) {
+        if (W == Z) {
+	  std::swap(Y, Z);
+        } else if (Y == X) {
+	  std::swap(W, X);
+	} else if (X == Z) {
+          std::swap(Y, Z);
+          std::swap(W, X);
+        }
+      }
+
+      if (W == Y) {
+        Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, Z,
+                                                            LHS->getName()), I);
+        return BinaryOperator::createMul(W, NewAdd);
+      }
+    }
+  }
+
   if (ConstantInt *CRHS = dyn_cast<ConstantInt>(RHS)) {
     Value *X = 0;
     if (match(LHS, m_Not(m_Value(X))))    // ~X + C --> (C-1) - X





More information about the llvm-commits mailing list