Added an early implementation of Live-Variables analysis built on
source-level CFGs.  This code may change significantly in the near
future as we explore different means to implement dataflow analyses.

Added a driver option, -dump-live-variables, to view the output of
live variable analysis.  This output is very ALPHA; it will be improved shortly.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@41737 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/Driver/ASTStreamers.cpp b/Driver/ASTStreamers.cpp
index f77505b..a1494e4 100644
--- a/Driver/ASTStreamers.cpp
+++ b/Driver/ASTStreamers.cpp
@@ -14,6 +14,7 @@
 #include "ASTStreamers.h"
 #include "clang/AST/AST.h"
 #include "clang/AST/CFG.h"
+#include "clang/Analysis/LiveVariables.h"
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Sema/ASTStreamer.h"
 using namespace clang;
@@ -184,4 +185,28 @@
   ASTStreamer_Terminate(Streamer);
 }
 
+void clang::AnalyzeLiveVariables(Preprocessor &PP, unsigned MainFileID)
+{
+  ASTContext Context(PP.getTargetInfo(), PP.getIdentifierTable());
+  ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID);
+  
+  while (Decl *D = ASTStreamer_ReadTopLevelDecl(Streamer)) {
+    if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {      
+      if (FD->getBody()) {
+        PrintFunctionDeclStart(FD);
+        fprintf(stderr,"\n");
+        if (CFG* C = CFG::buildCFG(FD->getBody())) {
+          LiveVariables L;
+          L.runOnCFG(*C);
+          L.DumpBlockLiveness();
+        }
+        else
+          fprintf(stderr," Error processing CFG.\n");
+      }
+    }
+  }
+      
+  ASTStreamer_Terminate(Streamer);
+}
+
 
diff --git a/Driver/ASTStreamers.h b/Driver/ASTStreamers.h
index dfd9d54..34ce864 100644
--- a/Driver/ASTStreamers.h
+++ b/Driver/ASTStreamers.h
@@ -26,6 +26,8 @@
 
 void DumpCFGs(Preprocessor &PP, unsigned MainFileID,
               bool Stats, bool use_graphviz = false);  
+              
+void AnalyzeLiveVariables(Preprocessor &PP, unsigned MainFileID);
 
 } // end clang namespace
 
diff --git a/Driver/Makefile b/Driver/Makefile
index 4c9db0d..eca316e 100644
--- a/Driver/Makefile
+++ b/Driver/Makefile
@@ -3,6 +3,6 @@
 CXXFLAGS = -fno-rtti
 
 TOOLNAME = clang
-USEDLIBS = clangCodeGen.a clangSEMA.a clangAST.a clangParse.a clangLex.a clangBasic.a LLVMCore.a LLVMSupport.a LLVMSystem.a
+USEDLIBS = clangCodeGen.a clangSEMA.a clangAnalysis.a clangAST.a clangParse.a clangLex.a clangBasic.a LLVMCore.a LLVMSupport.a LLVMSystem.a
 
 include $(LEVEL)/Makefile.common
diff --git a/Driver/clang.cpp b/Driver/clang.cpp
index 2bb4422..dde1c57 100644
--- a/Driver/clang.cpp
+++ b/Driver/clang.cpp
@@ -54,6 +54,7 @@
   ParseAST,                     // Parse ASTs.
   ParseCFGDump,                 // Parse ASTS. Build CFGs. Print CFGs.
   ParseCFGView,                 // Parse ASTS. Build CFGs. View CFGs (Graphviz).
+  AnalysisLiveVariables,        // Print results of live-variable analysis.
   ParsePrintCallbacks,          // Parse and print each callback.
   ParseSyntaxOnly,              // Parse and perform semantic analysis.
   ParseNoop,                    // Parse with noop callbacks.
@@ -89,9 +90,11 @@
              clEnumValN(ParseCFGDump, "dump-cfg",
                         "Run parser, then build and print CFGs."),
              clEnumValN(ParseCFGView, "view-cfg",
-                        "Run parser, then build and view CFGs with Graphviz."),  
+                        "Run parser, then build and view CFGs with Graphviz."),
+             clEnumValN(AnalysisLiveVariables, "dump-live-variables",
+                     "Run parser and print results of live variable analysis."),
              clEnumValN(EmitLLVM, "emit-llvm",
-                        "Build ASTs then convert to LLVM, emit .ll file"),
+                     "Build ASTs then convert to LLVM, emit .ll file"),
              clEnumValEnd));
 
 //===----------------------------------------------------------------------===//
@@ -846,6 +849,9 @@
   case ParseCFGView:
     DumpCFGs(PP, MainFileID, Stats, true);
     break;
+  case AnalysisLiveVariables:
+    AnalyzeLiveVariables(PP, MainFileID);
+    break;  
   case EmitLLVM:
     EmitLLVMFromASTs(PP, MainFileID, Stats);
     break;