Move all of the header files which are involved in modelling the LLVM IR
[oota-llvm.git] / tools / opt / AnalysisWrappers.cpp
index 46ede0a15342c21dbe08d71f55b80b31f32e9e24..55f544ff5e5c1ff8ae42798db431d3cc151006e2 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 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/Analysis/CallGraph.h"
+#include "llvm/IR/Module.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/CallSite.h"
-#include <iostream>
+#include "llvm/Support/raw_ostream.h"
 using namespace llvm;
 
 namespace {
@@ -29,39 +30,65 @@ namespace {
   /// 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(ID) {}
     virtual bool runOnModule(Module &M) {
-      for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
-        if (I->isExternal()) {
-          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;
-                  }
-              }
+      for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
+        if (!I->isDeclaration()) continue;
+        
+        bool PrintedFn = false;
+        for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
+             UI != E; ++UI) {
+          Instruction *User = dyn_cast<Instruction>(*UI);
+          if (!User) continue;
+          
+          CallSite CS(cast<Value>(User));
+          if (!CS) continue;
+          
+          for (CallSite::arg_iterator AI = CS.arg_begin(),
+               E = CS.arg_end(); AI != E; ++AI) {
+            if (!isa<Constant>(*AI)) continue;
+
+            if (!PrintedFn) {
+              errs() << "Function '" << I->getName() << "':\n";
+              PrintedFn = true;
             }
+            errs() << *User;
+            break;
+          }
         }
+      }
 
       return false;
     }
 
-    void print(std::ostream &OS) const {}
-    
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
       AU.setPreservesAll();
     }
   };
+}
 
-  RegisterAnalysis<ExternalFunctionsPassedConstants>
-  P2("externalfnconstants", "Print external fn callsites passed constants");
+char ExternalFunctionsPassedConstants::ID = 0;
+static RegisterPass<ExternalFunctionsPassedConstants>
+  P1("print-externalfnconstants",
+     "Print external fn callsites passed constants");
+
+namespace {
+  struct CallGraphPrinter : public ModulePass {
+    static char ID; // Pass ID, replacement for typeid
+    CallGraphPrinter() : ModulePass(ID) {}
+
+    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+      AU.setPreservesAll();
+      AU.addRequiredTransitive<CallGraph>();
+    }
+    virtual bool runOnModule(Module &M) {
+      getAnalysis<CallGraph>().print(errs(), &M);
+      return false;
+    }
+  };
 }
+
+char CallGraphPrinter::ID = 0;
+static RegisterPass<CallGraphPrinter>
+  P2("print-callgraph", "Print a call graph");