Transform (X/Y)*Y into X if the division is exact.  Instcombine already knows how
to do this and more, but would only do it if X/Y had only one use.  Spotted as the
most common missed simplification in SPEC by my auto-simplifier, now that it knows
about nuw/nsw/exact flags.  This removes a bunch of multiplications from 447.dealII
and 483.xalancbmk.  It also removes a lot from tramp3d-v4, which results in much
more inlining.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@124560 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Analysis/InstructionSimplify.cpp b/lib/Analysis/InstructionSimplify.cpp
index cf50668..fcbc6d5 100644
--- a/lib/Analysis/InstructionSimplify.cpp
+++ b/lib/Analysis/InstructionSimplify.cpp
@@ -710,6 +710,15 @@
   if (match(Op1, m_One()))
     return Op0;
 
+  // (X / Y) * Y -> X if the division is exact.
+  Value *X = 0, *Y = 0;
+  if ((match(Op0, m_SDiv(m_Value(X), m_Value(Y))) && Y == Op1) || // (X / Y) * Y
+      (match(Op1, m_SDiv(m_Value(X), m_Value(Y))) && Y == Op0)) { // Y * (X / Y)
+    BinaryOperator *SDiv = cast<BinaryOperator>(Y == Op1 ? Op0 : Op1);
+    if (SDiv->isExact())
+      return X;
+  }
+
   // i1 mul -> and.
   if (MaxRecurse && Op0->getType()->isIntegerTy(1))
     if (Value *V = SimplifyAndInst(Op0, Op1, TD, DT, MaxRecurse-1))