Fix infinite recursion for when extract_vector_elt is legal. Unfortunately no public...
[oota-llvm.git] / tools / opt / AnalysisWrappers.cpp
index aa9f03d88388202c77712dd17013af93afef9045..ce2c34dcd25711a5d5fa45a8003ec7ffcf864564 100644 (file)
@@ -1,10 +1,10 @@
 //===- AnalysisWrappers.cpp - Wrappers around non-pass analyses -----------===//
-// 
+//
 //                     The LLVM Compiler Infrastructure
 //
 // This file was developed by the LLVM research group and is distributed under
 // the University of Illinois Open Source License. See LICENSE.TXT for details.
-// 
+//
 //===----------------------------------------------------------------------===//
 //
 // This file defines pass wrappers around LLVM analyses that don't make sense to
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/Module.h"
 #include "llvm/Pass.h"
-#include "llvm/Analysis/InstForest.h"
-
+#include "llvm/Support/CallSite.h"
+#include "llvm/Analysis/CallGraph.h"
+#include <iostream>
 using namespace llvm;
 
 namespace {
-  struct InstForestHelper : public FunctionPass {
-    Function *F;
-    virtual bool runOnFunction(Function &Func) { F = &Func; return false; }
+  /// ExternalFunctionsPassedConstants - This pass prints out call sites to
+  /// external functions that are called with constant arguments.  This can be
+  /// useful when looking for standard library functions we should constant fold
+  /// or handle in alias analyses.
+  struct ExternalFunctionsPassedConstants : public ModulePass {
+    static char ID; // Pass ID, replacement for typeid
+    ExternalFunctionsPassedConstants() : ModulePass((intptr_t)&ID) {}
+    virtual bool runOnModule(Module &M) {
+      for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
+        if (I->isDeclaration()) {
+          bool PrintedFn = false;
+          for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
+               UI != E; ++UI)
+            if (Instruction *User = dyn_cast<Instruction>(*UI)) {
+              CallSite CS = CallSite::get(User);
+              if (CS.getInstruction()) {
+                for (CallSite::arg_iterator AI = CS.arg_begin(),
+                       E = CS.arg_end(); AI != E; ++AI)
+                  if (isa<Constant>(*AI)) {
+                    if (!PrintedFn) {
+                      std::cerr << "Function '" << I->getName() << "':\n";
+                      PrintedFn = true;
+                    }
+                    std::cerr << *User;
+                    break;
+                  }
+              }
+            }
+        }
 
-    void print(std::ostream &OS) const {
-      std::cout << InstForest<char>(F);
+      return false;
     }
-    
+
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
       AU.setPreservesAll();
     }
   };
 
-  RegisterAnalysis<InstForestHelper> P1("instforest", "InstForest Printer");
+  char ExternalFunctionsPassedConstants::ID = 0;
+  RegisterPass<ExternalFunctionsPassedConstants>
+  P1("externalfnconstants", "Print external fn callsites passed constants");
+  
+  struct CallGraphPrinter : public ModulePass {
+    static char ID; // Pass ID, replacement for typeid
+    CallGraphPrinter() : ModulePass((intptr_t)&ID) {}
+
+    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+      AU.setPreservesAll();
+      AU.addRequiredTransitive<CallGraph>();
+    }
+    virtual bool runOnModule(Module &M) { return false; }
+
+    virtual void print(std::ostream &OS, const Module *M) const {
+      getAnalysis<CallGraph>().print(OS, M);
+    }
+  };
+  
+  char CallGraphPrinter::ID = 0;
+  RegisterPass<CallGraphPrinter>
+    P2("callgraph", "Print a call graph");
 }