Have TableGen emit setSubgraphColor calls under control of a -gen-debug
authorDavid Greene <greened@obbligato.org>
Mon, 27 Oct 2008 21:56:29 +0000 (21:56 +0000)
committerDavid Greene <greened@obbligato.org>
Mon, 27 Oct 2008 21:56:29 +0000 (21:56 +0000)
flag.  Then in a debugger developers can set breakpoints at these calls
to see waht is about to be selected and what the resulting subgraph
looks like.  This really helps when debugging instruction selection.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@58278 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/CodeGen/DAGISelHeader.h
lib/Target/ARM/ARMISelDAGToDAG.cpp
lib/Target/Alpha/AlphaISelDAGToDAG.cpp
lib/Target/CellSPU/SPUISelDAGToDAG.cpp
lib/Target/IA64/IA64ISelDAGToDAG.cpp
lib/Target/Mips/MipsISelDAGToDAG.cpp
lib/Target/PIC16/PIC16ISelDAGToDAG.cpp
lib/Target/PowerPC/PPCISelDAGToDAG.cpp
lib/Target/Sparc/SparcISelDAGToDAG.cpp
lib/Target/X86/X86ISelDAGToDAG.cpp
utils/TableGen/DAGISelEmitter.cpp

index 3542ebcbd025dad8b9b53708ae48020b8d20241d..e50ee7c00add377ce865755e37a1674054d6b50e 100644 (file)
@@ -153,7 +153,7 @@ void ReplaceUses(SDNode *F, SDNode *T) DISABLE_INLINE {
 
 /// SelectRoot - Top level entry to DAG instruction selector.
 /// Selects instructions starting at the root of the current DAG.
-void SelectRoot() {
+void SelectRoot(SelectionDAG &DAG) {
   SelectRootInit();
   unsigned NumBytes = (DAGSize + 7) / 8;
   ISelQueued   = new unsigned char[NumBytes];
@@ -176,14 +176,18 @@ void SelectRoot() {
     // Skip already selected nodes.
     if (isSelected(Node->getNodeId()))
       continue;
+    DAG.setSubgraphColor(Node, "red");
     SDNode *ResNode = Select(SDValue(Node, 0));
     // If node should not be replaced, 
     // continue with the next one.
     if (ResNode == Node)
       continue;
     // Replace node.
-    if (ResNode)
+    if (ResNode) {
+      DAG.setSubgraphColor(ResNode, "yellow");
+      DAG.setSubgraphColor(ResNode, "black");
       ReplaceUses(Node, ResNode);
+    }
     // If after the replacement this node is not used any more,
     // remove this dead node.
     if (Node->use_empty()) { // Don't delete EntryToken, etc.
@@ -191,6 +195,7 @@ void SelectRoot() {
       CurDAG->RemoveDeadNode(Node, &ISQU);
       UpdateQueue(ISQU);
     }
+    //DAG.setSubgraphColor(Node, "black");
   }
 
   delete[] ISelQueued;
index 560be0ad96e21eccbdd19e6958c2dc2b625af4bb..060f61ca70cb412d225b16fc5b94fa592c3ad71f 100644 (file)
@@ -94,7 +94,7 @@ public:
 void ARMDAGToDAGISel::InstructionSelect() {
   DEBUG(BB->dump());
 
-  SelectRoot();
+  SelectRoot(*CurDAG);
   CurDAG->RemoveDeadNodes();
 }
 
index e4bd152885b162fdc858b7e4a56f1a4a56f65400..e0ae5564ed81841ab1eca7e647732190a61d9fc5 100644 (file)
@@ -230,7 +230,7 @@ void AlphaDAGToDAGISel::InstructionSelect() {
   DEBUG(BB->dump());
   
   // Select target instructions for the DAG.
-  SelectRoot();
+  SelectRoot(*CurDAG);
   CurDAG->RemoveDeadNodes();
 }
 
index 92c76af1d065608c64362941136161d3ae827422..d6a492b575c74778160d3769b3bf27c12834d6fe 100644 (file)
@@ -344,7 +344,7 @@ SPUDAGToDAGISel::InstructionSelect()
   DEBUG(BB->dump());
 
   // Select target instructions for the DAG.
-  SelectRoot();
+  SelectRoot(*CurDAG);
   CurDAG->RemoveDeadNodes();
 }
 
index 6d3199307e56a9b3b10edfda4d10ec81058e8cc8..ae59ea2489d4d2070ad0165033274f58f196961a 100644 (file)
@@ -97,7 +97,7 @@ void IA64DAGToDAGISel::InstructionSelect() {
   DEBUG(BB->dump());
 
   // Select target instructions for the DAG.
-  SelectRoot();
+  SelectRoot(*CurDAG);
   CurDAG->RemoveDeadNodes();
 }
 
index 2e3756a8715ab98bd5f2fb78e64ebfe03ebf4c83..b31bcbdc0b365b83da4d2d444033ce1277270d32 100644 (file)
@@ -105,7 +105,7 @@ InstructionSelect()
   #endif
 
   // Select target instructions for the DAG.
-  SelectRoot();
+  SelectRoot(*CurDAG);
 
   #ifndef NDEBUG
   DOUT << "===== Instruction selection ends:\n";
index 4d06089e9c1dc78e7d7e3ed237cfca5096d8aec3..d98be99f86c5b8cbd846ab8496fc7fbde58001bc 100644 (file)
@@ -102,7 +102,7 @@ void PIC16DAGToDAGISel::InstructionSelect()
 #endif
 
   // Select target instructions for the DAG.
-  SelectRoot();
+  SelectRoot(*CurDAG);
 
   DOUT << "===== Instruction selection ends:\n";
 
index e2ffb31fcc9c6cc0eb5105b6a403b49b926cdc06..37ca2be85d69a6f4e54f5a2f4fd89b3e3f96042a 100644 (file)
@@ -204,7 +204,7 @@ void PPCDAGToDAGISel::InstructionSelect() {
   DEBUG(BB->dump());
 
   // Select target instructions for the DAG.
-  SelectRoot();
+  SelectRoot(*CurDAG);
   CurDAG->RemoveDeadNodes();
 }
 
index 66e143de3fed54f3ee2d1419fc1663a617989a51..ceba75d788955b6dfe7a42d21658e7eb66ddd0f3 100644 (file)
@@ -70,7 +70,7 @@ void SparcDAGToDAGISel::InstructionSelect() {
   DEBUG(BB->dump());
 
   // Select target instructions for the DAG.
-  SelectRoot();
+  SelectRoot(*CurDAG);
   CurDAG->RemoveDeadNodes();
 }
 
index 6735f1d80b783e8e6857c74d5c1db06d11263e32..c3419809786365f79528d4a0186b3fc3ddb5ed7c 100644 (file)
@@ -655,7 +655,7 @@ void X86DAGToDAGISel::InstructionSelect() {
   DOUT << "===== Instruction selection begins:\n";
   Indent = 0;
 #endif
-  SelectRoot();
+  SelectRoot(*CurDAG);
 #ifndef NDEBUG
   DOUT << "===== Instruction selection ends:\n";
 #endif
index 299612977a1f381f8cb5ea7c6dfb114a28387177..82d414884b57bff3adff8096f28de246ab61643c 100644 (file)
 #include "DAGISelEmitter.h"
 #include "Record.h"
 #include "llvm/ADT/StringExtras.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/MathExtras.h"
+#include "llvm/Support/Debug.h"
 #include "llvm/Support/Streams.h"
 #include <algorithm>
 #include <deque>
 using namespace llvm;
 
+namespace {
+  cl::opt<bool>
+  GenDebug("gen-debug", cl::desc("Generate debug code"),
+              cl::init(false));
+}
+
 //===----------------------------------------------------------------------===//
 // DAGISelEmitter Helper methods
 //
@@ -969,6 +977,10 @@ public:
         emitCode("InChains.push_back(" + ChainName + ");");
         emitCode(ChainName + " = CurDAG->getNode(ISD::TokenFactor, MVT::Other, "
                  "&InChains[0], InChains.size());");
+        if (GenDebug) {
+          emitCode("CurDAG->setSubgraphColor(" + ChainName +".getNode(), \"yellow\");");
+          emitCode("CurDAG->setSubgraphColor(" + ChainName +".getNode(), \"black\");");
+        }
       }
 
       // Loop over all of the operands of the instruction pattern, emitting code
@@ -1096,13 +1108,18 @@ public:
       if (II.isSimpleLoad | II.mayLoad | II.mayStore) {
         std::vector<std::string>::const_iterator mi, mie;
         for (mi = LSI.begin(), mie = LSI.end(); mi != mie; ++mi) {
-          emitCode("SDValue LSI_" + *mi + " = "
+          std::string LSIName = "LSI_" + *mi;
+          emitCode("SDValue " + LSIName + " = "
                    "CurDAG->getMemOperand(cast<MemSDNode>(" +
                    *mi + ")->getMemOperand());");
+          if (GenDebug) {
+            emitCode("CurDAG->setSubgraphColor(" + LSIName +".getNode(), \"yellow\");");
+            emitCode("CurDAG->setSubgraphColor(" + LSIName +".getNode(), \"black\");");
+          }
           if (IsVariadic)
-            emitCode("Ops" + utostr(OpsNo) + ".push_back(LSI_" + *mi + ");");
+            emitCode("Ops" + utostr(OpsNo) + ".push_back(" + LSIName + ");");
           else
-            AllOps.push_back("LSI_" + *mi);
+            AllOps.push_back(LSIName);
         }
       }
 
@@ -1269,6 +1286,18 @@ public:
       }
 
       emitCode(CodePrefix + Code + ");");
+
+      if (GenDebug) {
+        if (!isRoot) {
+          emitCode("CurDAG->setSubgraphColor(" + NodeName +".getNode(), \"yellow\");");
+          emitCode("CurDAG->setSubgraphColor(" + NodeName +".getNode(), \"black\");");
+        }
+        else {
+          emitCode("CurDAG->setSubgraphColor(" + NodeName +", \"yellow\");");
+          emitCode("CurDAG->setSubgraphColor(" + NodeName +", \"black\");");
+        }
+      }
+
       for (unsigned i = 0, e = After.size(); i != e; ++i)
         emitCode(After[i]);
 
@@ -1766,8 +1795,19 @@ void DAGISelEmitter::EmitInstructionSelector(std::ostream &OS) {
 
         // Replace the emission code within selection routines with calls to the
         // emission functions.
-        CallerCode = "return Emit_" + utostr(EmitFuncNum) + CallerCode;
-        GeneratedCode.push_back(std::make_pair(false, CallerCode));
+        if (GenDebug) {
+          GeneratedCode.push_back(std::make_pair(0, "CurDAG->setSubgraphColor(N.getNode(), \"red\");"));
+        }
+        CallerCode = "SDNode *Result = Emit_" + utostr(EmitFuncNum) + CallerCode;
+        GeneratedCode.push_back(std::make_pair(3, CallerCode));
+        if (GenDebug) {
+          GeneratedCode.push_back(std::make_pair(0, "if(Result) {"));
+          GeneratedCode.push_back(std::make_pair(0, "  CurDAG->setSubgraphColor(Result, \"yellow\");"));
+          GeneratedCode.push_back(std::make_pair(0, "  CurDAG->setSubgraphColor(Result, \"black\");"));
+          GeneratedCode.push_back(std::make_pair(0, "}"));
+          //GeneratedCode.push_back(std::make_pair(0, "CurDAG->setSubgraphColor(N.getNode(), \"black\");"));
+        }
+        GeneratedCode.push_back(std::make_pair(0, "return Result;"));
       }
 
       // Print function.