[llvm-commits] [llvm] r125004 - in /llvm/trunk: include/llvm/Support/PatternMatch.h lib/Analysis/InstructionSimplify.cpp

Duncan Sands baldrick at free.fr
Mon Feb 7 01:36:32 PST 2011


Author: baldrick
Date: Mon Feb  7 03:36:32 2011
New Revision: 125004

URL: http://llvm.org/viewvc/llvm-project?rev=125004&view=rev
Log:
Add an m_Div pattern for matching either a udiv or an sdiv and use it
to simplify the "(X/Y)*Y->X when the division is exact" transform.

Modified:
    llvm/trunk/include/llvm/Support/PatternMatch.h
    llvm/trunk/lib/Analysis/InstructionSimplify.cpp

Modified: llvm/trunk/include/llvm/Support/PatternMatch.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/PatternMatch.h?rev=125004&r1=125003&r2=125004&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/PatternMatch.h (original)
+++ llvm/trunk/include/llvm/Support/PatternMatch.h Mon Feb  7 03:36:32 2011
@@ -347,6 +347,40 @@
 }
 
 //===----------------------------------------------------------------------===//
+// Matchers for either SDiv or UDiv .. for convenience
+//
+template<typename LHS_t, typename RHS_t, typename ConcreteTy = BinaryOperator>
+struct Div_match {
+  LHS_t L;
+  RHS_t R;
+
+  Div_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
+
+  template<typename OpTy>
+  bool match(OpTy *V) {
+    if (V->getValueID() == Value::InstructionVal + Instruction::SDiv ||
+        V->getValueID() == Value::InstructionVal + Instruction::UDiv) {
+      ConcreteTy *I = cast<ConcreteTy>(V);
+      return (I->getOpcode() == Instruction::UDiv ||
+              I->getOpcode() == Instruction::SDiv) &&
+             L.match(I->getOperand(0)) &&
+             R.match(I->getOperand(1));
+    }
+    if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
+      return (CE->getOpcode() == Instruction::SDiv ||
+              CE->getOpcode() == Instruction::UDiv) &&
+             L.match(CE->getOperand(0)) &&
+             R.match(CE->getOperand(1));
+    return false;
+  }
+};
+
+template<typename LHS, typename RHS>
+inline Div_match<LHS, RHS> m_Div(const LHS &L, const RHS &R) {
+  return Div_match<LHS, RHS>(L, R);
+}
+
+//===----------------------------------------------------------------------===//
 // Matchers for binary classes
 //
 

Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=125004&r1=125003&r2=125004&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Mon Feb  7 03:36:32 2011
@@ -734,10 +734,8 @@
 
   // (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(Op0, m_UDiv(m_Value(X), m_Value(Y))) && Y == Op1) ||
-      (match(Op1, m_SDiv(m_Value(X), m_Value(Y))) && Y == Op0) || // Y * (X / Y)
-      (match(Op1, m_UDiv(m_Value(X), m_Value(Y))) && Y == Op0)) {
+  if ((match(Op0, m_Div(m_Value(X), m_Value(Y))) && Y == Op1) || // (X / Y) * Y
+      (match(Op1, m_Div(m_Value(X), m_Value(Y))) && Y == Op0)) { // Y * (X / Y)
     BinaryOperator *Div = cast<BinaryOperator>(Y == Op1 ? Op0 : Op1);
     if (Div->isExact())
       return X;





More information about the llvm-commits mailing list