remove parallel support.
[oota-llvm.git] / utils / TableGen / DAGISelMatcherEmitter.cpp
index 3f78cdd206b847b0a72717c62e68363425829ae4..22a3430f98a5d8264c46f5272af0c2c58f4daa08 100644 (file)
@@ -17,6 +17,7 @@
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringMap.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FormattedStream.h"
 using namespace llvm;
 
@@ -24,6 +25,11 @@ enum {
   CommentIndent = 30
 };
 
+// To reduce generated source code size.
+static cl::opt<bool>
+OmitComments("omit-comments", cl::desc("Do not generate comments"),
+             cl::init(false));
+
 namespace {
 class MatcherTableEmitter {
   StringMap<unsigned> NodePredicateMap, PatternPredicateMap;
@@ -34,19 +40,18 @@ class MatcherTableEmitter {
 
 
   DenseMap<Record*, unsigned> NodeXFormMap;
-  std::vector<const Record*> NodeXForms;
+  std::vector<Record*> NodeXForms;
 
-  // Per opcode frequence count. 
-  std::vector<unsigned> Histogram;
 public:
   MatcherTableEmitter() {}
 
   unsigned EmitMatcherList(const Matcher *N, unsigned Indent,
                            unsigned StartIdx, formatted_raw_ostream &OS);
   
-  void EmitPredicateFunctions(formatted_raw_ostream &OS);
+  void EmitPredicateFunctions(const CodeGenDAGPatterns &CGP,
+                              formatted_raw_ostream &OS);
   
-  void EmitHistogram(formatted_raw_ostream &OS);
+  void EmitHistogram(const Matcher *N, formatted_raw_ostream &OS);
 private:
   unsigned EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
                        formatted_raw_ostream &OS);
@@ -115,7 +120,10 @@ static uint64_t EmitVBRValue(uint64_t Val, raw_ostream &OS) {
     Val >>= 7;
     ++NumBytes;
   }
-  OS << Val << "/*" << InVal << "*/, ";
+  OS << Val;
+  if (!OmitComments)
+    OS << "/*" << InVal << "*/";
+  OS << ", ";
   return NumBytes+1;
 }
 
@@ -138,9 +146,12 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
       if (i == 0) {
         OS << "OPC_Scope, ";
         ++CurrentIdx;
-      } else {
-        OS << "/*" << CurrentIdx << "*/";
-        OS.PadToColumn(Indent*2) << "/*Scope*/ ";
+      } else  {
+        if (!OmitComments) {
+          OS << "/*" << CurrentIdx << "*/";
+          OS.PadToColumn(Indent*2) << "/*Scope*/ ";
+        } else
+          OS.PadToColumn(Indent*2);
       }
 
       // We need to encode the child and the offset of the failure code before
@@ -157,40 +168,52 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
         TmpBuf.clear();
         raw_svector_ostream OS(TmpBuf);
         formatted_raw_ostream FOS(OS);
-        ChildSize = EmitMatcherList(cast<ScopeMatcher>(N)->getChild(i),
-                                   Indent+1, CurrentIdx+VBRSize, FOS);
+        ChildSize = EmitMatcherList(SM->getChild(i), Indent+1,
+                                    CurrentIdx+VBRSize, FOS);
       } while (GetVBRSize(ChildSize) != VBRSize);
       
       assert(ChildSize != 0 && "Should not have a zero-sized child!");
     
       CurrentIdx += EmitVBRValue(ChildSize, OS);
-      OS << "/*->" << CurrentIdx+ChildSize << "*/";
+      if (!OmitComments) {
+        OS << "/*->" << CurrentIdx+ChildSize << "*/";
       
-      if (i == 0)
-        OS.PadToColumn(CommentIndent) << "// " << SM->getNumChildren()
-          << " children in Scope";
+        if (i == 0)
+          OS.PadToColumn(CommentIndent) << "// " << SM->getNumChildren()
+            << " children in Scope";
+      }
       
       OS << '\n' << TmpBuf.str();
       CurrentIdx += ChildSize;
     }
     
     // Emit a zero as a sentinel indicating end of 'Scope'.
-    OS << "/*" << CurrentIdx << "*/";
-    OS.PadToColumn(Indent*2) << "0, /*End of Scope*/\n";
+    if (!OmitComments)
+      OS << "/*" << CurrentIdx << "*/";
+    OS.PadToColumn(Indent*2) << "0, ";
+    if (!OmitComments)
+      OS << "/*End of Scope*/";
+    OS << '\n';
     return CurrentIdx - StartIdx + 1;
   }
       
   case Matcher::RecordNode:
     OS << "OPC_RecordNode,";
-    OS.PadToColumn(CommentIndent) << "// "
-       << cast<RecordMatcher>(N)->getWhatFor() << '\n';
+    if (!OmitComments)
+      OS.PadToColumn(CommentIndent) << "// #"
+        << cast<RecordMatcher>(N)->getResultNo() << " = "
+        << cast<RecordMatcher>(N)->getWhatFor();
+    OS << '\n';
     return 1;
 
   case Matcher::RecordChild:
     OS << "OPC_RecordChild" << cast<RecordChildMatcher>(N)->getChildNo()
        << ',';
-    OS.PadToColumn(CommentIndent) << "// "
-      << cast<RecordChildMatcher>(N)->getWhatFor() << '\n';
+    if (!OmitComments)
+      OS.PadToColumn(CommentIndent) << "// #"
+        << cast<RecordChildMatcher>(N)->getResultNo() << " = "
+        << cast<RecordChildMatcher>(N)->getWhatFor();
+    OS << '\n';
     return 1;
       
   case Matcher::RecordMemRef:
@@ -217,43 +240,126 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
   case Matcher::CheckPatternPredicate: {
     StringRef Pred = cast<CheckPatternPredicateMatcher>(N)->getPredicate();
     OS << "OPC_CheckPatternPredicate, " << getPatternPredicate(Pred) << ',';
-    OS.PadToColumn(CommentIndent) << "// " << Pred << '\n';
+    if (!OmitComments)
+      OS.PadToColumn(CommentIndent) << "// " << Pred;
+    OS << '\n';
     return 2;
   }
   case Matcher::CheckPredicate: {
     StringRef Pred = cast<CheckPredicateMatcher>(N)->getPredicateName();
     OS << "OPC_CheckPredicate, " << getNodePredicate(Pred) << ',';
-    OS.PadToColumn(CommentIndent) << "// " << Pred << '\n';
+    if (!OmitComments)
+      OS.PadToColumn(CommentIndent) << "// " << Pred;
+    OS << '\n';
     return 2;
   }
 
   case Matcher::CheckOpcode:
-    OS << "OPC_CheckOpcode, "
-       << cast<CheckOpcodeMatcher>(N)->getOpcode().getEnumName() << ",\n";
-    return 2;
+    OS << "OPC_CheckOpcode, TARGET_OPCODE("
+       << cast<CheckOpcodeMatcher>(N)->getOpcode().getEnumName() << "),\n";
+    return 3;
+      
+  case Matcher::SwitchOpcode:
+  case Matcher::SwitchType: {
+    unsigned StartIdx = CurrentIdx;
+    
+    unsigned NumCases;
+    if (const SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(N)) {
+      OS << "OPC_SwitchOpcode ";
+      NumCases = SOM->getNumCases();
+    } else {
+      OS << "OPC_SwitchType ";
+      NumCases = cast<SwitchTypeMatcher>(N)->getNumCases();
+    }
+
+    if (!OmitComments)
+      OS << "/*" << NumCases << " cases */";
+    OS << ", ";
+    ++CurrentIdx;
+    
+    // For each case we emit the size, then the opcode, then the matcher.
+    for (unsigned i = 0, e = NumCases; i != e; ++i) {
+      const Matcher *Child;
+      if (const SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(N))
+        Child = SOM->getCaseMatcher(i);
+      else
+        Child = cast<SwitchTypeMatcher>(N)->getCaseMatcher(i);
+      
+      // We need to encode the opcode and the offset of the case code before
+      // emitting the case code.  Handle this by buffering the output into a
+      // string while we get the size.  Unfortunately, the offset of the
+      // children depends on the VBR size of the child, so for large children we
+      // have to iterate a bit.
+      SmallString<128> TmpBuf;
+      unsigned ChildSize = 0;
+      unsigned VBRSize = 0;
+      do {
+        VBRSize = GetVBRSize(ChildSize);
+        
+        TmpBuf.clear();
+        raw_svector_ostream OS(TmpBuf);
+        formatted_raw_ostream FOS(OS);
+        ChildSize = EmitMatcherList(Child, Indent+1, CurrentIdx+VBRSize+1, FOS);
+      } while (GetVBRSize(ChildSize) != VBRSize);
+      
+      assert(ChildSize != 0 && "Should not have a zero-sized child!");
+      
+      if (i != 0) {
+        OS.PadToColumn(Indent*2);
+        if (!OmitComments)
+        OS << (isa<SwitchOpcodeMatcher>(N) ?
+                   "/*SwitchOpcode*/ " : "/*SwitchType*/ ");
+      }
+      
+      // Emit the VBR.
+      CurrentIdx += EmitVBRValue(ChildSize, OS);
       
-  case Matcher::CheckMultiOpcode: {
-    const CheckMultiOpcodeMatcher *CMO = cast<CheckMultiOpcodeMatcher>(N);
-    OS << "OPC_CheckMultiOpcode, " << CMO->getNumOpcodes() << ", ";
-    for (unsigned i = 0, e = CMO->getNumOpcodes(); i != e; ++i)
-      OS << CMO->getOpcode(i).getEnumName() << ", ";
+      OS << ' ';
+      if (const SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(N)) {
+        OS << "TARGET_OPCODE(" << SOM->getCaseOpcode(i).getEnumName() << "),";
+        CurrentIdx += 2;
+      } else {
+        OS << getEnumName(cast<SwitchTypeMatcher>(N)->getCaseType(i)) << ',';
+        ++CurrentIdx;
+      }
+      
+      if (!OmitComments)
+        OS << "// ->" << CurrentIdx+ChildSize;
+      OS << '\n';
+      OS << TmpBuf.str();
+      CurrentIdx += ChildSize;
+    }
+
+    // Emit the final zero to terminate the switch.
+    OS.PadToColumn(Indent*2) << "0, ";
+    if (!OmitComments)
+      OS << (isa<SwitchOpcodeMatcher>(N) ?
+             "// EndSwitchOpcode" : "// EndSwitchType");
+
     OS << '\n';
-    return 2 + CMO->getNumOpcodes();
+    ++CurrentIdx;
+    return CurrentIdx-StartIdx;
   }
-      
-  case Matcher::CheckType:
+
+ case Matcher::CheckType:
+    assert(cast<CheckTypeMatcher>(N)->getResNo() == 0 &&
+           "FIXME: Add support for CheckType of resno != 0");
     OS << "OPC_CheckType, "
        << getEnumName(cast<CheckTypeMatcher>(N)->getType()) << ",\n";
     return 2;
+      
   case Matcher::CheckChildType:
     OS << "OPC_CheckChild"
        << cast<CheckChildTypeMatcher>(N)->getChildNo() << "Type, "
        << getEnumName(cast<CheckChildTypeMatcher>(N)->getType()) << ",\n";
     return 2;
       
-  case Matcher::CheckInteger:
+  case Matcher::CheckInteger: {
     OS << "OPC_CheckInteger, ";
-    return 1+EmitVBRValue(cast<CheckIntegerMatcher>(N)->getValue(), OS);
+    unsigned Bytes=1+EmitVBRValue(cast<CheckIntegerMatcher>(N)->getValue(), OS);
+    OS << '\n';
+    return Bytes;
+  }
   case Matcher::CheckCondCode:
     OS << "OPC_CheckCondCode, ISD::"
        << cast<CheckCondCodeMatcher>(N)->getCondCodeName() << ",\n";
@@ -265,38 +371,49 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
     return 2;
 
   case Matcher::CheckComplexPat: {
-    const ComplexPattern &Pattern =
-      cast<CheckComplexPatMatcher>(N)->getPattern();
-    OS << "OPC_CheckComplexPat, " << getComplexPat(Pattern) << ',';
-    OS.PadToColumn(CommentIndent) << "// " << Pattern.getSelectFunc();
-    OS << ": " << Pattern.getNumOperands() << " operands";
-    if (Pattern.hasProperty(SDNPHasChain))
-      OS << " + chain result and input";
+    const CheckComplexPatMatcher *CCPM = cast<CheckComplexPatMatcher>(N);
+    const ComplexPattern &Pattern = CCPM->getPattern();
+    OS << "OPC_CheckComplexPat, /*CP*/" << getComplexPat(Pattern) << ", /*#*/"
+       << CCPM->getMatchNumber() << ',';
+    
+    if (!OmitComments) {
+      OS.PadToColumn(CommentIndent) << "// " << Pattern.getSelectFunc();
+      OS << ":$" << CCPM->getName();
+      for (unsigned i = 0, e = Pattern.getNumOperands(); i != e; ++i)
+        OS << " #" << CCPM->getFirstResult()+i;
+           
+      if (Pattern.hasProperty(SDNPHasChain))
+        OS << " + chain result";
+    }
     OS << '\n';
-    return 2;
+    return 3;
   }
       
-  case Matcher::CheckAndImm:
+  case Matcher::CheckAndImm: {
     OS << "OPC_CheckAndImm, ";
-    return 1+EmitVBRValue(cast<CheckAndImmMatcher>(N)->getValue(), OS);
+    unsigned Bytes=1+EmitVBRValue(cast<CheckAndImmMatcher>(N)->getValue(), OS);
+    OS << '\n';
+    return Bytes;
+  }
 
-  case Matcher::CheckOrImm:
+  case Matcher::CheckOrImm: {
     OS << "OPC_CheckOrImm, ";
-    return 1+EmitVBRValue(cast<CheckOrImmMatcher>(N)->getValue(), OS);
+    unsigned Bytes = 1+EmitVBRValue(cast<CheckOrImmMatcher>(N)->getValue(), OS);
+    OS << '\n';
+    return Bytes;
+  }
       
   case Matcher::CheckFoldableChainNode:
     OS << "OPC_CheckFoldableChainNode,\n";
     return 1;
-  case Matcher::CheckChainCompatible:
-    OS << "OPC_CheckChainCompatible, "
-       << cast<CheckChainCompatibleMatcher>(N)->getPreviousOp() << ",\n";
-    return 2;
       
   case Matcher::EmitInteger: {
     int64_t Val = cast<EmitIntegerMatcher>(N)->getValue();
     OS << "OPC_EmitInteger, "
        << getEnumName(cast<EmitIntegerMatcher>(N)->getVT()) << ", ";
-    return 2+EmitVBRValue(Val, OS);
+    unsigned Bytes = 2+EmitVBRValue(Val, OS);
+    OS << '\n';
+    return Bytes;
   }
   case Matcher::EmitStringInteger: {
     const std::string &Val = cast<EmitStringIntegerMatcher>(N)->getValue();
@@ -312,8 +429,12 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
        << getEnumName(cast<EmitRegisterMatcher>(N)->getVT()) << ", ";
     if (Record *R = cast<EmitRegisterMatcher>(N)->getReg())
       OS << getQualifiedName(R) << ",\n";
-    else
-      OS << "0 /*zero_reg*/,\n";
+    else {
+      OS << "0 ";
+      if (!OmitComments)
+        OS << "/*zero_reg*/";
+      OS << ",\n";
+    }
     return 3;
       
   case Matcher::EmitConvertToTarget:
@@ -340,7 +461,9 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
     const EmitNodeXFormMatcher *XF = cast<EmitNodeXFormMatcher>(N);
     OS << "OPC_EmitNodeXForm, " << getNodeXFormID(XF->getNodeXForm()) << ", "
        << XF->getSlot() << ',';
-    OS.PadToColumn(CommentIndent) << "// "<<XF->getNodeXForm()->getName()<<'\n';
+    if (!OmitComments)
+      OS.PadToColumn(CommentIndent) << "// "<<XF->getNodeXForm()->getName();
+    OS <<'\n';
     return 3;
   }
       
@@ -358,36 +481,41 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
       OS << "|OPFL_Variadic" << EN->getNumFixedArityOperands();
     OS << ",\n";
     
-    OS.PadToColumn(Indent*2+4) << EN->getNumVTs() << "/*#VTs*/, ";
+    OS.PadToColumn(Indent*2+4) << EN->getNumVTs();
+    if (!OmitComments)
+      OS << "/*#VTs*/";
+    OS << ", ";
     for (unsigned i = 0, e = EN->getNumVTs(); i != e; ++i)
       OS << getEnumName(EN->getVT(i)) << ", ";
 
-    OS << EN->getNumOperands() << "/*#Ops*/, ";
+    OS << EN->getNumOperands();
+    if (!OmitComments)
+      OS << "/*#Ops*/";
+    OS << ", ";
     unsigned NumOperandBytes = 0;
-    for (unsigned i = 0, e = EN->getNumOperands(); i != e; ++i) {
-      // We emit the operand numbers in VBR encoded format, in case the number
-      // is too large to represent with a byte.
+    for (unsigned i = 0, e = EN->getNumOperands(); i != e; ++i)
       NumOperandBytes += EmitVBRValue(EN->getOperand(i), OS);
-    }
     
-    // Print the result #'s for EmitNode.
-    if (const EmitNodeMatcher *E = dyn_cast<EmitNodeMatcher>(EN)) {
-      if (unsigned NumResults = EN->getNumVTs()) {
-        OS.PadToColumn(CommentIndent) << "// Results = ";
-        unsigned First = E->getFirstResultSlot();
-        for (unsigned i = 0; i != NumResults; ++i)
-          OS << "#" << First+i << " ";
+    if (!OmitComments) {
+      // Print the result #'s for EmitNode.
+      if (const EmitNodeMatcher *E = dyn_cast<EmitNodeMatcher>(EN)) {
+        if (unsigned NumResults = EN->getNumVTs()) {
+          OS.PadToColumn(CommentIndent) << "// Results = ";
+          unsigned First = E->getFirstResultSlot();
+          for (unsigned i = 0; i != NumResults; ++i)
+            OS << "#" << First+i << " ";
+        }
       }
-    }
-    OS << '\n';
-    
-    if (const MorphNodeToMatcher *SNT = dyn_cast<MorphNodeToMatcher>(N)) {
-      OS.PadToColumn(Indent*2) << "// Src: "
-      << *SNT->getPattern().getSrcPattern() << '\n';
-      OS.PadToColumn(Indent*2) << "// Dst: " 
-      << *SNT->getPattern().getDstPattern() << '\n';
-      
-    }
+      OS << '\n';
+
+      if (const MorphNodeToMatcher *SNT = dyn_cast<MorphNodeToMatcher>(N)) {
+        OS.PadToColumn(Indent*2) << "// Src: "
+          << *SNT->getPattern().getSrcPattern() << '\n';
+        OS.PadToColumn(Indent*2) << "// Dst: "
+          << *SNT->getPattern().getDstPattern() << '\n';
+      }
+    } else
+      OS << '\n';
     
     return 6+EN->getNumVTs()+NumOperandBytes;
   }
@@ -407,10 +535,13 @@ EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
     for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i)
       NumResultBytes += EmitVBRValue(CM->getResult(i), OS);
     OS << '\n';
-    OS.PadToColumn(Indent*2) << "// Src: "
-      << *CM->getPattern().getSrcPattern() << '\n';
-    OS.PadToColumn(Indent*2) << "// Dst: " 
-      << *CM->getPattern().getDstPattern() << '\n';
+    if (!OmitComments) {
+      OS.PadToColumn(Indent*2) << "// Src: "
+        << *CM->getPattern().getSrcPattern() << '\n';
+      OS.PadToColumn(Indent*2) << "// Dst: "
+        << *CM->getPattern().getDstPattern();
+    }
+    OS << '\n';
     return 2 + NumResultBytes;
   }
   }
@@ -424,11 +555,8 @@ EmitMatcherList(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
                 formatted_raw_ostream &OS) {
   unsigned Size = 0;
   while (N) {
-    if (unsigned(N->getKind()) >= Histogram.size())
-      Histogram.resize(N->getKind()+1);
-    Histogram[N->getKind()]++;
-    
-    OS << "/*" << CurrentIdx << "*/";
+    if (!OmitComments)
+      OS << "/*" << CurrentIdx << "*/";
     unsigned MatcherSize = EmitMatcher(N, Indent, CurrentIdx, OS);
     Size += MatcherSize;
     CurrentIdx += MatcherSize;
@@ -440,10 +568,8 @@ EmitMatcherList(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
   return Size;
 }
 
-void MatcherTableEmitter::EmitPredicateFunctions(formatted_raw_ostream &OS) {
-  // FIXME: Don't build off the DAGISelEmitter's predicates, emit them directly
-  // here into the case stmts.
-  
+void MatcherTableEmitter::EmitPredicateFunctions(const CodeGenDAGPatterns &CGP,
+                                                 formatted_raw_ostream &OS) {
   // Emit pattern predicates.
   if (!PatternPredicates.empty()) {
     OS << "bool CheckPatternPredicate(unsigned PredNo) const {\n";
@@ -454,15 +580,40 @@ void MatcherTableEmitter::EmitPredicateFunctions(formatted_raw_ostream &OS) {
     OS << "  }\n";
     OS << "}\n\n";
   }
-    
-
+   
   // Emit Node predicates.
+  // FIXME: Annoyingly, these are stored by name, which we never even emit. Yay?
+  StringMap<TreePattern*> PFsByName;
+  
+  for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end();
+       I != E; ++I)
+    PFsByName[I->first->getName()] = I->second;
+  
   if (!NodePredicates.empty()) {
-    OS << "bool CheckNodePredicate(SDNode *N, unsigned PredNo) const {\n";
+    OS << "bool CheckNodePredicate(SDNode *Node, unsigned PredNo) const {\n";
     OS << "  switch (PredNo) {\n";
     OS << "  default: assert(0 && \"Invalid predicate in table?\");\n";
-    for (unsigned i = 0, e = NodePredicates.size(); i != e; ++i)
-      OS << "  case " << i << ": return "  << NodePredicates[i] << "(N);\n";
+    for (unsigned i = 0, e = NodePredicates.size(); i != e; ++i) {
+      // FIXME: Storing this by name is horrible.
+      TreePattern *P =PFsByName[NodePredicates[i].substr(strlen("Predicate_"))];
+      assert(P && "Unknown name?");
+      
+      // Emit the predicate code corresponding to this pattern.
+      std::string Code = P->getRecord()->getValueAsCode("Predicate");
+      assert(!Code.empty() && "No code in this predicate");
+      OS << "  case " << i << ": { // " << NodePredicates[i] << '\n';
+      std::string ClassName;
+      if (P->getOnlyTree()->isLeaf())
+        ClassName = "SDNode";
+      else
+        ClassName =
+          CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName();
+      if (ClassName == "SDNode")
+        OS << "    SDNode *N = Node;\n";
+      else
+        OS << "    " << ClassName << "*N = cast<" << ClassName << ">(Node);\n";
+      OS << Code << "\n  }\n";
+    }
     OS << "  }\n";
     OS << "}\n\n";
   }
@@ -485,10 +636,6 @@ void MatcherTableEmitter::EmitPredicateFunctions(formatted_raw_ostream &OS) {
       OS << "    Result.resize(Result.size()+" << NumOps << ");\n";
       OS << "    return "  << P.getSelectFunc();
 
-      // FIXME: Temporary hack until old isel dies.
-      if (P.hasProperty(SDNPHasChain))
-        OS << "XXX";
-      
       OS << "(Root, N";
       for (unsigned i = 0; i != NumOps; ++i)
         OS << ", Result[Result.size()-" << (NumOps-i) << ']';
@@ -498,6 +645,7 @@ void MatcherTableEmitter::EmitPredicateFunctions(formatted_raw_ostream &OS) {
     OS << "}\n\n";
   }
   
+  
   // Emit SDNodeXForm handlers.
   // FIXME: This should be const.
   if (!NodeXForms.empty()) {
@@ -506,17 +654,63 @@ void MatcherTableEmitter::EmitPredicateFunctions(formatted_raw_ostream &OS) {
     OS << "  default: assert(0 && \"Invalid xform # in table?\");\n";
     
     // FIXME: The node xform could take SDValue's instead of SDNode*'s.
-    for (unsigned i = 0, e = NodeXForms.size(); i != e; ++i)
-      OS << "  case " << i << ": return Transform_" << NodeXForms[i]->getName()
-         << "(V.getNode());\n";
+    for (unsigned i = 0, e = NodeXForms.size(); i != e; ++i) {
+      const CodeGenDAGPatterns::NodeXForm &Entry =
+        CGP.getSDNodeTransform(NodeXForms[i]);
+      
+      Record *SDNode = Entry.first;
+      const std::string &Code = Entry.second;
+      
+      OS << "  case " << i << ": {  ";
+      if (!OmitComments)
+        OS << "// " << NodeXForms[i]->getName();
+      OS << '\n';
+      
+      std::string ClassName = CGP.getSDNodeInfo(SDNode).getSDClassName();
+      if (ClassName == "SDNode")
+        OS << "    SDNode *N = V.getNode();\n";
+      else
+        OS << "    " << ClassName << " *N = cast<" << ClassName
+           << ">(V.getNode());\n";
+      OS << Code << "\n  }\n";
+    }
     OS << "  }\n";
     OS << "}\n\n";
   }
 }
 
-void MatcherTableEmitter::EmitHistogram(formatted_raw_ostream &OS) {
+static void BuildHistogram(const Matcher *M, std::vector<unsigned> &OpcodeFreq){
+  for (; M != 0; M = M->getNext()) {
+    // Count this node.
+    if (unsigned(M->getKind()) >= OpcodeFreq.size())
+      OpcodeFreq.resize(M->getKind()+1);
+    OpcodeFreq[M->getKind()]++;
+  
+    // Handle recursive nodes.
+    if (const ScopeMatcher *SM = dyn_cast<ScopeMatcher>(M)) {
+      for (unsigned i = 0, e = SM->getNumChildren(); i != e; ++i)
+        BuildHistogram(SM->getChild(i), OpcodeFreq);
+    } else if (const SwitchOpcodeMatcher *SOM = 
+                 dyn_cast<SwitchOpcodeMatcher>(M)) {
+      for (unsigned i = 0, e = SOM->getNumCases(); i != e; ++i)
+        BuildHistogram(SOM->getCaseMatcher(i), OpcodeFreq);
+    } else if (const SwitchTypeMatcher *STM = dyn_cast<SwitchTypeMatcher>(M)) {
+      for (unsigned i = 0, e = STM->getNumCases(); i != e; ++i)
+        BuildHistogram(STM->getCaseMatcher(i), OpcodeFreq);
+    }
+  }
+}
+
+void MatcherTableEmitter::EmitHistogram(const Matcher *M,
+                                        formatted_raw_ostream &OS) {
+  if (OmitComments)
+    return;
+  
+  std::vector<unsigned> OpcodeFreq;
+  BuildHistogram(M, OpcodeFreq);
+  
   OS << "  // Opcode Histogram:\n";
-  for (unsigned i = 0, e = Histogram.size(); i != e; ++i) {
+  for (unsigned i = 0, e = OpcodeFreq.size(); i != e; ++i) {
     OS << "  // #";
     switch ((Matcher::KindTy)i) {
     case Matcher::Scope: OS << "OPC_Scope"; break; 
@@ -531,8 +725,9 @@ void MatcherTableEmitter::EmitHistogram(formatted_raw_ostream &OS) {
       OS << "OPC_CheckPatternPredicate"; break;
     case Matcher::CheckPredicate: OS << "OPC_CheckPredicate"; break;
     case Matcher::CheckOpcode: OS << "OPC_CheckOpcode"; break;
-    case Matcher::CheckMultiOpcode: OS << "OPC_CheckMultiOpcode"; break;
+    case Matcher::SwitchOpcode: OS << "OPC_SwitchOpcode"; break;
     case Matcher::CheckType: OS << "OPC_CheckType"; break;
+    case Matcher::SwitchType: OS << "OPC_SwitchType"; break;
     case Matcher::CheckChildType: OS << "OPC_CheckChildType"; break;
     case Matcher::CheckInteger: OS << "OPC_CheckInteger"; break;
     case Matcher::CheckCondCode: OS << "OPC_CheckCondCode"; break;
@@ -542,7 +737,6 @@ void MatcherTableEmitter::EmitHistogram(formatted_raw_ostream &OS) {
     case Matcher::CheckOrImm: OS << "OPC_CheckOrImm"; break;
     case Matcher::CheckFoldableChainNode:
       OS << "OPC_CheckFoldableChainNode"; break;
-    case Matcher::CheckChainCompatible: OS << "OPC_CheckChainCompatible"; break;
     case Matcher::EmitInteger: OS << "OPC_EmitInteger"; break;
     case Matcher::EmitStringInteger: OS << "OPC_EmitStringInteger"; break;
     case Matcher::EmitRegister: OS << "OPC_EmitRegister"; break;
@@ -556,13 +750,14 @@ void MatcherTableEmitter::EmitHistogram(formatted_raw_ostream &OS) {
     case Matcher::CompleteMatch: OS << "OPC_CompleteMatch"; break;    
     }
     
-    OS.PadToColumn(40) << " = " << Histogram[i] << '\n';
+    OS.PadToColumn(40) << " = " << OpcodeFreq[i] << '\n';
   }
   OS << '\n';
 }
 
 
-void llvm::EmitMatcherTable(const Matcher *TheMatcher, raw_ostream &O) {
+void llvm::EmitMatcherTable(const Matcher *TheMatcher,
+                            const CodeGenDAGPatterns &CGP, raw_ostream &O) {
   formatted_raw_ostream OS(O);
   
   OS << "// The main instruction selector code.\n";
@@ -576,12 +771,12 @@ void llvm::EmitMatcherTable(const Matcher *TheMatcher, raw_ostream &O) {
   unsigned TotalSize = MatcherEmitter.EmitMatcherList(TheMatcher, 5, 0, OS);
   OS << "    0\n  }; // Total Array size is " << (TotalSize+1) << " bytes\n\n";
   
-  MatcherEmitter.EmitHistogram(OS);
+  MatcherEmitter.EmitHistogram(TheMatcher, OS);
   
   OS << "  #undef TARGET_OPCODE\n";
   OS << "  return SelectCodeCommon(N, MatcherTable,sizeof(MatcherTable));\n}\n";
-  OS << "\n";
+  OS << '\n';
   
   // Next up, emit the function for node and pattern predicates:
-  MatcherEmitter.EmitPredicateFunctions(OS);
+  MatcherEmitter.EmitPredicateFunctions(CGP, OS);
 }