[llvm-commits] [llvm] r97305 - in /llvm/trunk/utils/TableGen: DAGISelMatcher.h DAGISelMatcherOpt.cpp

Chris Lattner sabre at nondot.org
Fri Feb 26 22:22:57 PST 2010


Author: lattner
Date: Sat Feb 27 00:22:57 2010
New Revision: 97305

URL: http://llvm.org/viewvc/llvm-project?rev=97305&view=rev
Log:
implement a new optimization to sink pattern predicates (like isSSE1) 
as deeply into the pattern as we can get away with.  In pratice, this 
means "all the way to to the emitter code, but not across 
ComplexPatterns".  This substantially increases the amount of factoring
we get.

Modified:
    llvm/trunk/utils/TableGen/DAGISelMatcher.h
    llvm/trunk/utils/TableGen/DAGISelMatcherOpt.cpp

Modified: llvm/trunk/utils/TableGen/DAGISelMatcher.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/DAGISelMatcher.h?rev=97305&r1=97304&r2=97305&view=diff
==============================================================================
--- llvm/trunk/utils/TableGen/DAGISelMatcher.h (original)
+++ llvm/trunk/utils/TableGen/DAGISelMatcher.h Sat Feb 27 00:22:57 2010
@@ -104,6 +104,12 @@
     return ((getHashImpl() << 4) ^ getKind()) & (~0U>>1);
   }
   
+  /// isSafeToReorderWithPatternPredicate - Return true if it is safe to sink a
+  /// PatternPredicate node past this one.
+  virtual bool isSafeToReorderWithPatternPredicate() const {
+    return false;
+  }
+  
   void print(raw_ostream &OS, unsigned indent = 0) const;
   void dump() const;
 protected:
@@ -173,6 +179,7 @@
     return N->getKind() == RecordNode;
   }
   
+  virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
 private:
   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
   virtual bool isEqualImpl(const Matcher *M) const { return true; }
@@ -199,6 +206,8 @@
     return N->getKind() == RecordChild;
   }
   
+  virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
+
 private:
   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
   virtual bool isEqualImpl(const Matcher *M) const {
@@ -216,6 +225,8 @@
     return N->getKind() == RecordMemRef;
   }
   
+  virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
+
 private:
   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
   virtual bool isEqualImpl(const Matcher *M) const { return true; }
@@ -233,6 +244,8 @@
     return N->getKind() == CaptureFlagInput;
   }
   
+  virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
+
 private:
   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
   virtual bool isEqualImpl(const Matcher *M) const { return true; }
@@ -252,6 +265,8 @@
     return N->getKind() == MoveChild;
   }
   
+  virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
+
 private:
   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
   virtual bool isEqualImpl(const Matcher *M) const {
@@ -270,6 +285,8 @@
     return N->getKind() == MoveParent;
   }
   
+  virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
+
 private:
   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
   virtual bool isEqualImpl(const Matcher *M) const { return true; }
@@ -291,6 +308,8 @@
     return N->getKind() == CheckSame;
   }
   
+  virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
+
 private:
   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
   virtual bool isEqualImpl(const Matcher *M) const {
@@ -314,6 +333,8 @@
     return N->getKind() == CheckPatternPredicate;
   }
   
+  virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
+
 private:
   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
   virtual bool isEqualImpl(const Matcher *M) const {
@@ -336,6 +357,9 @@
     return N->getKind() == CheckPredicate;
   }
   
+  // TODO: Ok?
+  //virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
+
 private:
   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
   virtual bool isEqualImpl(const Matcher *M) const {
@@ -359,6 +383,8 @@
     return N->getKind() == CheckOpcode;
   }
   
+  virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
+
 private:
   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
   virtual bool isEqualImpl(const Matcher *M) const {
@@ -382,6 +408,8 @@
     return N->getKind() == CheckMultiOpcode;
   }
   
+  virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
+
 private:
   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
   virtual bool isEqualImpl(const Matcher *M) const {
@@ -406,6 +434,8 @@
     return N->getKind() == CheckType;
   }
   
+  virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
+
 private:
   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
   virtual bool isEqualImpl(const Matcher *M) const {
@@ -430,6 +460,8 @@
     return N->getKind() == CheckChildType;
   }
   
+  virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
+
 private:
   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
   virtual bool isEqualImpl(const Matcher *M) const {
@@ -454,6 +486,8 @@
     return N->getKind() == CheckInteger;
   }
   
+  virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
+
 private:
   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
   virtual bool isEqualImpl(const Matcher *M) const {
@@ -476,6 +510,8 @@
     return N->getKind() == CheckCondCode;
   }
   
+  virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
+
 private:
   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
   virtual bool isEqualImpl(const Matcher *M) const {
@@ -498,6 +534,8 @@
     return N->getKind() == CheckValueType;
   }
   
+  virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
+
 private:
   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
   virtual bool isEqualImpl(const Matcher *M) const {
@@ -522,6 +560,9 @@
     return N->getKind() == CheckComplexPat;
   }
   
+  // Not safe to move a pattern predicate past a complex pattern.
+  virtual bool isSafeToReorderWithPatternPredicate() const { return false; }
+
 private:
   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
   virtual bool isEqualImpl(const Matcher *M) const {
@@ -546,6 +587,8 @@
     return N->getKind() == CheckAndImm;
   }
   
+  virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
+
 private:
   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
   virtual bool isEqualImpl(const Matcher *M) const {
@@ -568,6 +611,8 @@
     return N->getKind() == CheckOrImm;
   }
   
+  virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
+
 private:
   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
   virtual bool isEqualImpl(const Matcher *M) const {
@@ -587,6 +632,8 @@
     return N->getKind() == CheckFoldableChainNode;
   }
   
+  virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
+
 private:
   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
   virtual bool isEqualImpl(const Matcher *M) const { return true; }
@@ -607,6 +654,8 @@
     return N->getKind() == CheckChainCompatible;
   }
   
+  virtual bool isSafeToReorderWithPatternPredicate() const { return true; }
+
 private:
   virtual void printImpl(raw_ostream &OS, unsigned indent) const;
   virtual bool isEqualImpl(const Matcher *M) const {

Modified: llvm/trunk/utils/TableGen/DAGISelMatcherOpt.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/DAGISelMatcherOpt.cpp?rev=97305&r1=97304&r2=97305&view=diff
==============================================================================
--- llvm/trunk/utils/TableGen/DAGISelMatcherOpt.cpp (original)
+++ llvm/trunk/utils/TableGen/DAGISelMatcherOpt.cpp Sat Feb 27 00:22:57 2010
@@ -16,6 +16,8 @@
 #include <vector>
 using namespace llvm;
 
+/// ContractNodes - Turn multiple matcher node patterns like 'MoveChild+Record'
+/// into single compound nodes like RecordChild.
 static void ContractNodes(OwningPtr<Matcher> &MatcherPtr) {
   // If we reached the end of the chain, we're done.
   Matcher *N = MatcherPtr.get();
@@ -61,6 +63,71 @@
   ContractNodes(N->getNextPtr());
 }
 
+/// SinkPatternPredicates - Pattern predicates can be checked at any level of
+/// the matching tree.  The generator dumps them at the top level of the pattern
+/// though, which prevents factoring from being able to see past them.  This
+/// optimization sinks them as far down into the pattern as possible.
+///
+/// Conceptually, we'd like to sink these predicates all the way to the last
+/// matcher predicate in the series.  However, it turns out that some
+/// ComplexPatterns have side effects on the graph, so we really don't want to
+/// run a the complex pattern if the pattern predicate will fail.  For this
+/// reason, we refuse to sink the pattern predicate past a ComplexPattern.
+///
+static void SinkPatternPredicates(OwningPtr<Matcher> &MatcherPtr) {
+  // Recursively scan for a PatternPredicate.
+  // If we reached the end of the chain, we're done.
+  Matcher *N = MatcherPtr.get();
+  if (N == 0) return;
+  
+  // Walk down all members of a scope node.
+  if (ScopeMatcher *Scope = dyn_cast<ScopeMatcher>(N)) {
+    for (unsigned i = 0, e = Scope->getNumChildren(); i != e; ++i) {
+      OwningPtr<Matcher> Child(Scope->takeChild(i));
+      SinkPatternPredicates(Child);
+      Scope->resetChild(i, Child.take());
+    }
+    return;
+  }
+  
+  // If this node isn't a CheckPatternPredicateMatcher we keep scanning until
+  // we find one.
+  CheckPatternPredicateMatcher *CPPM =dyn_cast<CheckPatternPredicateMatcher>(N);
+  if (CPPM == 0)
+    return SinkPatternPredicates(N->getNextPtr());
+  
+  // Ok, we found one, lets try to sink it. Check if we can sink it past the
+  // next node in the chain.  If not, we won't be able to change anything and
+  // might as well bail.
+  if (!CPPM->getNext()->isSafeToReorderWithPatternPredicate())
+    return;
+  
+  // Okay, we know we can sink it past at least one node.  Unlink it from the
+  // chain and scan for the new insertion point.
+  MatcherPtr.take();  // Don't delete CPPM.
+  MatcherPtr.reset(CPPM->takeNext());
+  
+  N = MatcherPtr.get();
+  while (N->getNext()->isSafeToReorderWithPatternPredicate())
+    N = N->getNext();
+  
+  // At this point, we want to insert CPPM after N.
+  CPPM->setNext(N->takeNext());
+  N->setNext(CPPM);
+}
+
+/// FactorNodes - Turn matches like this:
+///   Scope
+///     OPC_CheckType i32
+///       ABC
+///     OPC_CheckType i32
+///       XYZ
+/// into:
+///   OPC_CheckType i32
+///     Scope
+///       ABC
+///       XYZ
+///
 static void FactorNodes(OwningPtr<Matcher> &MatcherPtr) {
   // If we reached the end of the chain, we're done.
   Matcher *N = MatcherPtr.get();
@@ -145,6 +212,7 @@
 Matcher *llvm::OptimizeMatcher(Matcher *TheMatcher) {
   OwningPtr<Matcher> MatcherPtr(TheMatcher);
   ContractNodes(MatcherPtr);
+  SinkPatternPredicates(MatcherPtr);
   FactorNodes(MatcherPtr);
   return MatcherPtr.take();
 }





More information about the llvm-commits mailing list