diff --git a/tools/opt/opt.cpp b/tools/opt/opt.cpp index 2b6109f2bc1..c1e0ad128bc 100644 --- a/tools/opt/opt.cpp +++ b/tools/opt/opt.cpp @@ -18,6 +18,7 @@ #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" @@ -128,6 +129,10 @@ QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet)); static cl::opt AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization")); +static cl::opt +PrintBreakpoints("print-breakpoints-for-testing", + cl::desc("Print select breakpoints location for testing")); + static cl::opt DefaultDataLayout("default-data-layout", cl::desc("data layout string to use if not specified by module"), @@ -334,6 +339,41 @@ 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; + } + 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); @@ -509,6 +549,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) @@ -623,7 +681,7 @@ int main(int argc, char **argv) { Passes.run(*M.get()); // Declare success. - if (!NoOutput) + if (!NoOutput || PrintBreakpoints) Out->keep(); return 0;