Add r122359 back now that the bug in MCDwarfLineAddrFragment fragment has been
[oota-llvm.git] / tools / llvm-prof / llvm-prof.cpp
index cff139e1251a7e98f1f07fab57a218b672492d34..9d0b46833befa606f2ad581f61e849bc3bdfceb4 100644 (file)
 #include "llvm/LLVMContext.h"
 #include "llvm/Module.h"
 #include "llvm/PassManager.h"
-#include "llvm/Assembly/AsmAnnotationWriter.h"
+#include "llvm/Assembly/AssemblyAnnotationWriter.h"
 #include "llvm/Analysis/ProfileInfo.h"
 #include "llvm/Analysis/ProfileInfoLoader.h"
 #include "llvm/Analysis/Passes.h"
 #include "llvm/Bitcode/ReaderWriter.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/FormattedStream.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Support/Format.h"
-#include "llvm/System/Signals.h"
+#include "llvm/Support/Signals.h"
+#include "llvm/Support/system_error.h"
 #include <algorithm>
 #include <iomanip>
 #include <map>
@@ -75,9 +77,10 @@ namespace {
   class ProfileAnnotator : public AssemblyAnnotationWriter {
     ProfileInfo &PI;
   public:
-    ProfileAnnotator(ProfileInfopi) : PI(pi) {}
+    ProfileAnnotator(ProfileInfo &pi) : PI(pi) {}
 
-    virtual void emitFunctionAnnot(const Function *F, raw_ostream &OS) {
+    virtual void emitFunctionAnnot(const Function *F,
+                                   formatted_raw_ostream &OS) {
       double w = PI.getExecutionCount(F);
       if (w != ProfileInfo::MissingValue) {
         OS << ";;; %" << F->getName() << " called "<<(unsigned)w
@@ -85,7 +88,7 @@ namespace {
       }
     }
     virtual void emitBasicBlockStartAnnot(const BasicBlock *BB,
-                                          raw_ostream &OS) {
+                                          formatted_raw_ostream &OS) {
       double w = PI.getExecutionCount(BB);
       if (w != ProfileInfo::MissingValue) {
         if (w != 0) {
@@ -96,7 +99,8 @@ namespace {
       }
     }
 
-    virtual void emitBasicBlockEndAnnot(const BasicBlock *BB, raw_ostream &OS) {
+    virtual void emitBasicBlockEndAnnot(const BasicBlock *BB,
+                                        formatted_raw_ostream &OS) {
       // Figure out how many times each successor executed.
       std::vector<std::pair<ProfileInfo::Edge, double> > SuccCounts;
 
@@ -128,7 +132,7 @@ namespace {
   public:
     static char ID; // Class identification, replacement for typeinfo.
     explicit ProfileInfoPrinterPass(ProfileInfoLoader &_PIL) 
-      : ModulePass(&ID), PIL(_PIL) {}
+      : ModulePass(ID), PIL(_PIL) {}
 
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
       AU.setPreservesAll();
@@ -255,41 +259,35 @@ int main(int argc, char **argv) {
 
   LLVMContext &Context = getGlobalContext();
   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
-  try {
-    cl::ParseCommandLineOptions(argc, argv, "llvm profile dump decoder\n");
-
-    // Read in the bitcode file...
-    std::string ErrorMessage;
-    Module *M = 0;
-    if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(BitcodeFile,
-                                                            &ErrorMessage)) {
-      M = ParseBitcodeFile(Buffer, Context, &ErrorMessage);
-      delete Buffer;
-    }
-    if (M == 0) {
-      errs() << argv[0] << ": " << BitcodeFile << ": "
-        << ErrorMessage << "\n";
-      return 1;
-    }
-
-    // Read the profiling information. This is redundant since we load it again
-    // using the standard profile info provider pass, but for now this gives us
-    // access to additional information not exposed via the ProfileInfo
-    // interface.
-    ProfileInfoLoader PIL(argv[0], ProfileDataFile, *M);
-
-    // Run the printer pass.
-    PassManager PassMgr;
-    PassMgr.add(createProfileLoaderPass(ProfileDataFile));
-    PassMgr.add(new ProfileInfoPrinterPass(PIL));
-    PassMgr.run(*M);
-
-    return 0;
-  } catch (const std::string& msg) {
-    errs() << argv[0] << ": " << msg << "\n";
-  } catch (...) {
-    errs() << argv[0] << ": Unexpected unknown exception occurred.\n";
-  }
   
-  return 1;
+  cl::ParseCommandLineOptions(argc, argv, "llvm profile dump decoder\n");
+
+  // Read in the bitcode file...
+  std::string ErrorMessage;
+  OwningPtr<MemoryBuffer> Buffer;
+  error_code ec;
+  Module *M = 0;
+  if (!(ec = MemoryBuffer::getFileOrSTDIN(BitcodeFile, Buffer))) {
+    M = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage);
+  } else
+    ErrorMessage = ec.message();
+  if (M == 0) {
+    errs() << argv[0] << ": " << BitcodeFile << ": "
+      << ErrorMessage << "\n";
+    return 1;
+  }
+
+  // Read the profiling information. This is redundant since we load it again
+  // using the standard profile info provider pass, but for now this gives us
+  // access to additional information not exposed via the ProfileInfo
+  // interface.
+  ProfileInfoLoader PIL(argv[0], ProfileDataFile, *M);
+
+  // Run the printer pass.
+  PassManager PassMgr;
+  PassMgr.add(createProfileLoaderPass(ProfileDataFile));
+  PassMgr.add(new ProfileInfoPrinterPass(PIL));
+  PassMgr.run(*M);
+
+  return 0;
 }