fix PR8602, a bug in an assertion: a volatile store *of* a pointer
[oota-llvm.git] / tools / opt / opt.cpp
index ce92ec1b8b7fef94c3e8b712b00d4977c22c8760..85d365306a27b823009102f0d50398537290c07a 100644 (file)
 #include "llvm/CallGraphSCCPass.h"
 #include "llvm/Bitcode/ReaderWriter.h"
 #include "llvm/Assembly/PrintModulePass.h"
+#include "llvm/Analysis/DebugInfo.h"
 #include "llvm/Analysis/Verifier.h"
 #include "llvm/Analysis/LoopPass.h"
+#include "llvm/Analysis/RegionPass.h"
 #include "llvm/Analysis/CallGraph.h"
 #include "llvm/Target/TargetData.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Support/PassNameParser.h"
-#include "llvm/System/Signals.h"
+#include "llvm/Support/Signals.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/IRReader.h"
 #include "llvm/Support/ManagedStatic.h"
@@ -32,7 +34,7 @@
 #include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/StandardPasses.h"
 #include "llvm/Support/SystemUtils.h"
-#include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/ToolOutputFile.h"
 #include "llvm/LinkAllPasses.h"
 #include "llvm/LinkAllVMCore.h"
 #include <memory>
@@ -127,6 +129,10 @@ QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
 static cl::opt<bool>
 AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
 
+static cl::opt<bool>
+PrintBreakpoints("print-breakpoints-for-testing", 
+                 cl::desc("Print select breakpoints location for testing"));
+
 static cl::opt<std::string>
 DefaultDataLayout("default-data-layout", 
           cl::desc("data layout string to use if not specified by module"),
@@ -148,17 +154,16 @@ struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
     }
 
   virtual bool runOnSCC(CallGraphSCC &SCC) {
-    if (!Quiet) {
+    if (!Quiet)
       Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
 
-      for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
-        Function *F = (*I)->getFunction();
-        if (F)
-          getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, 
-                F->getParent());
-      }
-    }
     // Get and print pass...
+    for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
+      Function *F = (*I)->getFunction();
+      if (F)
+        getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
+                                                              F->getParent());
+    }
     return false;
   }
 
@@ -185,12 +190,11 @@ struct ModulePassPrinter : public ModulePass {
     }
 
   virtual bool runOnModule(Module &M) {
-    if (!Quiet) {
+    if (!Quiet)
       Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
-      getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, &M);
-    }
 
     // Get and print pass...
+    getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, &M);
     return false;
   }
 
@@ -216,10 +220,10 @@ struct FunctionPassPrinter : public FunctionPass {
     }
 
   virtual bool runOnFunction(Function &F) {
-    if (!Quiet) {
+    if (!Quiet)
       Out << "Printing analysis '" << PassToPrint->getPassName()
           << "' for function '" << F.getName() << "':\n";
-    }
+
     // Get and print pass...
     getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
             F.getParent());
@@ -250,12 +254,12 @@ struct LoopPassPrinter : public LoopPass {
 
 
   virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
-    if (!Quiet) {
+    if (!Quiet)
       Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
-      getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
-                                  L->getHeader()->getParent()->getParent());
-    }
+
     // Get and print pass...
+    getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
+                        L->getHeader()->getParent()->getParent());
     return false;
   }
 
@@ -269,6 +273,40 @@ struct LoopPassPrinter : public LoopPass {
 
 char LoopPassPrinter::ID = 0;
 
+struct RegionPassPrinter : public RegionPass {
+  static char ID;
+  const PassInfo *PassToPrint;
+  raw_ostream &Out;
+  std::string PassName;
+
+  RegionPassPrinter(const PassInfo *PI, raw_ostream &out) : RegionPass(ID),
+    PassToPrint(PI), Out(out) {
+    std::string PassToPrintName =  PassToPrint->getPassName();
+    PassName = "LoopPass Printer: " + PassToPrintName;
+  }
+
+  virtual bool runOnRegion(Region *R, RGPassManager &RGM) {
+    if (!Quiet) {
+      Out << "Printing analysis '" << PassToPrint->getPassName() << "' for "
+        << "region: '" << R->getNameStr() << "' in function '"
+        << R->getEntry()->getParent()->getNameStr() << "':\n";
+    }
+    // Get and print pass...
+   getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out,
+                       R->getEntry()->getParent()->getParent());
+    return false;
+  }
+
+  virtual const char *getPassName() const { return "'Pass' Printer"; }
+
+  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+    AU.addRequiredID(PassToPrint->getTypeInfo());
+    AU.setPreservesAll();
+  }
+};
+
+char RegionPassPrinter::ID = 0;
+
 struct BasicBlockPassPrinter : public BasicBlockPass {
   const PassInfo *PassToPrint;
   raw_ostream &Out;
@@ -282,10 +320,9 @@ struct BasicBlockPassPrinter : public BasicBlockPass {
     }
 
   virtual bool runOnBasicBlock(BasicBlock &BB) {
-    if (!Quiet) {
+    if (!Quiet)
       Out << "Printing Analysis info for BasicBlock '" << BB.getName()
           << "': Pass " << PassToPrint->getPassName() << ":\n";
-    }
 
     // Get and print pass...
     getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, 
@@ -302,6 +339,54 @@ struct BasicBlockPassPrinter : public BasicBlockPass {
 };
 
 char BasicBlockPassPrinter::ID = 0;
+
+struct BreakpointPrinter : public FunctionPass {
+  raw_ostream &Out;
+  static char ID;
+
+  BreakpointPrinter(raw_ostream &out)
+    : FunctionPass(ID), Out(out) {
+    }
+
+  virtual bool runOnFunction(Function &F) {
+    for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
+      BasicBlock::const_iterator BI = I->end();
+      --BI;
+      do {
+        const Instruction *In = BI;
+        const DebugLoc DL = In->getDebugLoc();
+        if (!DL.isUnknown()) {
+          DIScope S(DL.getScope(getGlobalContext()));
+          Out << S.getFilename() << " " << DL.getLine() << "\n";
+          break;
+        }
+        --BI;
+      } while (BI != I->begin());
+      break;
+    }
+    BasicBlock &EntryBB = F.getEntryBlock();
+    for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
+      BasicBlock *BB = I;
+      if (BB == &EntryBB) continue;
+      for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE; ++BI)
+        if (CallInst *CI = dyn_cast<CallInst>(BI)) {
+          const DebugLoc DL = CI->getDebugLoc();
+          if (!DL.isUnknown()) {
+            DIScope S(DL.getScope(getGlobalContext()));
+            Out << S.getFilename() << " " << DL.getLine() << "\n";
+          }
+        }
+    }
+    return false;
+  }
+
+  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+    AU.setPreservesAll();
+  }
+};
+
+char BreakpointPrinter::ID = 0;
+
 inline void addPass(PassManagerBase &PM, Pass *P) {
   // Add the pass to the pass manager...
   PM.add(P);
@@ -323,9 +408,9 @@ void AddOptimizationPasses(PassManagerBase &MPM, PassManagerBase &FPM,
   if (DisableInline) {
     // No inlining pass
   } else if (OptLevel) {
-    unsigned Threshold = 200;
+    unsigned Threshold = 225;
     if (OptLevel > 2)
-      Threshold = 250;
+      Threshold = 275;
     InliningPass = createFunctionInliningPass(Threshold);
   } else {
     InliningPass = createAlwaysInlinerPass();
@@ -386,20 +471,32 @@ int main(int argc, char **argv) {
   sys::PrintStackTraceOnErrorSignal();
   llvm::PrettyStackTraceProgram X(argc, argv);
 
-  if (AnalyzeOnly && NoOutput) {
-    errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
-    return 1;
-  }
-  
   // Enable debug stream buffering.
   EnableDebugBuffering = true;
 
   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
   LLVMContext &Context = getGlobalContext();
   
+  // Initialize passes
+  PassRegistry &Registry = *PassRegistry::getPassRegistry();
+  initializeCore(Registry);
+  initializeScalarOpts(Registry);
+  initializeIPO(Registry);
+  initializeAnalysis(Registry);
+  initializeIPA(Registry);
+  initializeTransformUtils(Registry);
+  initializeInstCombine(Registry);
+  initializeInstrumentation(Registry);
+  initializeTarget(Registry);
+  
   cl::ParseCommandLineOptions(argc, argv,
     "llvm .bc -> .bc modular optimizer and analysis printer\n");
 
+  if (AnalyzeOnly && NoOutput) {
+    errs() << argv[0] << ": analyze mode conflicts with no-output mode.\n";
+    return 1;
+  }
+
   // Allocate a full target machine description only if necessary.
   // FIXME: The choice of target should be controllable on the command line.
   std::auto_ptr<TargetMachine> target;
@@ -465,6 +562,24 @@ int main(int argc, char **argv) {
       FPasses->add(new TargetData(*TD));
   }
 
+  if (PrintBreakpoints) {
+    // Default to standard output.
+    if (!Out) {
+      if (OutputFilename.empty())
+        OutputFilename = "-";
+      
+      std::string ErrorInfo;
+      Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
+                                     raw_fd_ostream::F_Binary));
+      if (!ErrorInfo.empty()) {
+        errs() << ErrorInfo << '\n';
+        return 1;
+      }
+    }
+    Passes.add(new BreakpointPrinter(Out->os()));
+    NoOutput = true;
+  }
+
   // If the -strip-debug command line option was specified, add it.  If
   // -std-compile-opts was also specified, it will handle StripDebug.
   if (StripDebug && !StandardCompileOpts)
@@ -517,6 +632,9 @@ int main(int argc, char **argv) {
         case PT_BasicBlock:
           Passes.add(new BasicBlockPassPrinter(PassInf, Out->os()));
           break;
+        case PT_Region:
+          Passes.add(new RegionPassPrinter(PassInf, Out->os()));
+          break;
         case PT_Loop:
           Passes.add(new LoopPassPrinter(PassInf, Out->os()));
           break;
@@ -576,7 +694,7 @@ int main(int argc, char **argv) {
   Passes.run(*M.get());
 
   // Declare success.
-  if (!NoOutput)
+  if (!NoOutput || PrintBreakpoints)
     Out->keep();
 
   return 0;