[llvm-commits] [llvm] r96747 - /llvm/trunk/utils/TableGen/DAGISelEmitter.cpp

Chris Lattner sabre at nondot.org
Sun Feb 21 11:22:06 PST 2010


Author: lattner
Date: Sun Feb 21 13:22:06 2010
New Revision: 96747

URL: http://llvm.org/viewvc/llvm-project?rev=96747&view=rev
Log:
Sort the patterns before adding them to the FA so that we get the
least cost matches.  This gets us from 195 -> 208 passes on the ppc codegen tests.

Modified:
    llvm/trunk/utils/TableGen/DAGISelEmitter.cpp

Modified: llvm/trunk/utils/TableGen/DAGISelEmitter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/DAGISelEmitter.cpp?rev=96747&r1=96746&r2=96747&view=diff
==============================================================================
--- llvm/trunk/utils/TableGen/DAGISelEmitter.cpp (original)
+++ llvm/trunk/utils/TableGen/DAGISelEmitter.cpp Sun Feb 21 13:22:06 2010
@@ -141,7 +141,6 @@
 
   typedef std::pair<unsigned, std::string> CodeLine;
   typedef std::vector<CodeLine> CodeList;
-  typedef std::vector<std::pair<const PatternToMatch*, CodeList> > PatternList;
 
   bool operator()(const std::pair<const PatternToMatch*, CodeList> &LHSPair,
                   const std::pair<const PatternToMatch*, CodeList> &RHSPair) {
@@ -1889,6 +1888,36 @@
      << "}\n\n";
 }
 
+namespace {
+// PatternSortingPredicate - return true if we prefer to match LHS before RHS.
+// In particular, we want to match maximal patterns first and lowest cost within
+// a particular complexity first.
+struct PatternSortingPredicate2 {
+  PatternSortingPredicate2(CodeGenDAGPatterns &cgp) : CGP(cgp) {}
+  CodeGenDAGPatterns &CGP;
+  
+  bool operator()(const PatternToMatch *LHS,
+                  const PatternToMatch *RHS) {
+    unsigned LHSSize = getPatternSize(LHS->getSrcPattern(), CGP);
+    unsigned RHSSize = getPatternSize(RHS->getSrcPattern(), CGP);
+    LHSSize += LHS->getAddedComplexity();
+    RHSSize += RHS->getAddedComplexity();
+    if (LHSSize > RHSSize) return true;   // LHS -> bigger -> less cost
+    if (LHSSize < RHSSize) return false;
+    
+    // If the patterns have equal complexity, compare generated instruction cost
+    unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP);
+    unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP);
+    if (LHSCost < RHSCost) return true;
+    if (LHSCost > RHSCost) return false;
+    
+    return getResultPatternSize(LHS->getDstPattern(), CGP) <
+           getResultPatternSize(RHS->getDstPattern(), CGP);
+  }
+};
+}
+
+
 void DAGISelEmitter::run(raw_ostream &OS) {
   EmitSourceFileHeader("DAG Instruction Selector for the " +
                        CGP.getTargetInfo().getName() + " target", OS);
@@ -1919,11 +1948,27 @@
   
 #if 0
   MatcherNode *Matcher = 0;
-  // Walk the patterns backwards, building a matcher for each and adding it to
-  // the matcher for the whole target.
-  for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
-       E = CGP.ptm_end(); I != E;) {
-    const PatternToMatch &Pattern = *--E;
+
+  // Add all the patterns to a temporary list so we can sort them.
+  std::vector<const PatternToMatch*> Patterns;
+  for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end();
+       I != E; ++I)
+    Patterns.push_back(&*I);
+
+  // We want to process the matches in order of minimal cost.  Sort the patterns
+  // so the least cost one is at the start.
+  // FIXME: Eliminate "PatternSortingPredicate" and rename.
+  std::stable_sort(Patterns.begin(), Patterns.end(),
+                   PatternSortingPredicate2(CGP));
+  
+  
+  // Walk the patterns backwards (since we append to the front of the generated
+  // code), building a matcher for each and adding it to the matcher for the
+  // whole target.
+  while (!Patterns.empty()) {
+    const PatternToMatch &Pattern = *Patterns.back();
+    Patterns.pop_back();
+    
     MatcherNode *N = ConvertPatternToMatcher(Pattern, CGP);
     
     if (Matcher == 0)
@@ -1933,8 +1978,8 @@
   }
 
   // OptimizeMatcher(Matcher);
-  EmitMatcherTable(Matcher, OS);
   //Matcher->dump();
+  EmitMatcherTable(Matcher, OS);
   delete Matcher;
 #endif
 }





More information about the llvm-commits mailing list