diff options
Diffstat (limited to 'contrib/llvm/tools/opt')
-rw-r--r-- | contrib/llvm/tools/opt/AnalysisWrappers.cpp | 22 | ||||
-rw-r--r-- | contrib/llvm/tools/opt/GraphPrinters.cpp | 40 | ||||
-rw-r--r-- | contrib/llvm/tools/opt/PrintSCC.cpp | 38 | ||||
-rw-r--r-- | contrib/llvm/tools/opt/opt.cpp | 139 |
4 files changed, 124 insertions, 115 deletions
diff --git a/contrib/llvm/tools/opt/AnalysisWrappers.cpp b/contrib/llvm/tools/opt/AnalysisWrappers.cpp index f548d00..a2b57bb 100644 --- a/contrib/llvm/tools/opt/AnalysisWrappers.cpp +++ b/contrib/llvm/tools/opt/AnalysisWrappers.cpp @@ -31,7 +31,7 @@ namespace { /// or handle in alias analyses. struct ExternalFunctionsPassedConstants : public ModulePass { static char ID; // Pass ID, replacement for typeid - ExternalFunctionsPassedConstants() : ModulePass(&ID) {} + ExternalFunctionsPassedConstants() : ModulePass(ID) {} virtual bool runOnModule(Module &M) { for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { if (!I->isDeclaration()) continue; @@ -42,8 +42,8 @@ namespace { Instruction *User = dyn_cast<Instruction>(*UI); if (!User) continue; - CallSite CS = CallSite::get(User); - if (!CS.getInstruction()) continue; + CallSite CS(cast<Value>(User)); + if (!CS) continue; for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end(); AI != E; ++AI) { @@ -66,15 +66,17 @@ namespace { AU.setPreservesAll(); } }; +} - char ExternalFunctionsPassedConstants::ID = 0; - RegisterPass<ExternalFunctionsPassedConstants> +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) {} + CallGraphPrinter() : ModulePass(ID) {} virtual void getAnalysisUsage(AnalysisUsage &AU) const { AU.setPreservesAll(); @@ -85,8 +87,8 @@ namespace { return false; } }; - - char CallGraphPrinter::ID = 0; - RegisterPass<CallGraphPrinter> - P2("print-callgraph", "Print a call graph"); } + +char CallGraphPrinter::ID = 0; +static RegisterPass<CallGraphPrinter> + P2("print-callgraph", "Print a call graph"); diff --git a/contrib/llvm/tools/opt/GraphPrinters.cpp b/contrib/llvm/tools/opt/GraphPrinters.cpp index e7c6d1e..9de7d6a 100644 --- a/contrib/llvm/tools/opt/GraphPrinters.cpp +++ b/contrib/llvm/tools/opt/GraphPrinters.cpp @@ -28,13 +28,19 @@ static void WriteGraphToFile(raw_ostream &O, const std::string &GraphName, std::string Filename = GraphName + ".dot"; O << "Writing '" << Filename << "'..."; std::string ErrInfo; - raw_fd_ostream F(Filename.c_str(), ErrInfo); - - if (ErrInfo.empty()) - WriteGraph(F, GT); - else - O << " error opening file for writing!"; - O << "\n"; + tool_output_file F(Filename.c_str(), ErrInfo); + + if (ErrInfo.empty()) { + WriteGraph(F.os(), GT); + F.os().close(); + if (!F.os().has_error()) { + O << "\n"; + F.keep(); + return; + } + } + O << " error opening file for writing!\n"; + F.os().clear_error(); } @@ -65,7 +71,7 @@ namespace llvm { namespace { struct CallGraphPrinter : public ModulePass { static char ID; // Pass ID, replacement for typeid - CallGraphPrinter() : ModulePass(&ID) {} + CallGraphPrinter() : ModulePass(ID) {} virtual bool runOnModule(Module &M) { WriteGraphToFile(llvm::errs(), "callgraph", &getAnalysis<CallGraph>()); @@ -79,12 +85,12 @@ namespace { AU.setPreservesAll(); } }; - - char CallGraphPrinter::ID = 0; - RegisterPass<CallGraphPrinter> P2("dot-callgraph", - "Print Call Graph to 'dot' file"); } +char CallGraphPrinter::ID = 0; +static RegisterPass<CallGraphPrinter> P2("dot-callgraph", + "Print Call Graph to 'dot' file"); + //===----------------------------------------------------------------------===// // DomInfoPrinter Pass //===----------------------------------------------------------------------===// @@ -93,7 +99,7 @@ namespace { class DomInfoPrinter : public FunctionPass { public: static char ID; // Pass identification, replacement for typeid - DomInfoPrinter() : FunctionPass(&ID) {} + DomInfoPrinter() : FunctionPass(ID) {} virtual void getAnalysisUsage(AnalysisUsage &AU) const { AU.setPreservesAll(); @@ -110,8 +116,8 @@ namespace { return false; } }; - - char DomInfoPrinter::ID = 0; - static RegisterPass<DomInfoPrinter> - DIP("print-dom-info", "Dominator Info Printer", true, true); } + +char DomInfoPrinter::ID = 0; +static RegisterPass<DomInfoPrinter> +DIP("print-dom-info", "Dominator Info Printer", true, true); diff --git a/contrib/llvm/tools/opt/PrintSCC.cpp b/contrib/llvm/tools/opt/PrintSCC.cpp index ea486ca..533f49e 100644 --- a/contrib/llvm/tools/opt/PrintSCC.cpp +++ b/contrib/llvm/tools/opt/PrintSCC.cpp @@ -36,7 +36,7 @@ using namespace llvm; namespace { struct CFGSCC : public FunctionPass { static char ID; // Pass identification, replacement for typeid - CFGSCC() : FunctionPass(&ID) {} + CFGSCC() : FunctionPass(ID) {} bool runOnFunction(Function& func); void print(raw_ostream &O, const Module* = 0) const { } @@ -48,7 +48,7 @@ namespace { struct CallGraphSCC : public ModulePass { static char ID; // Pass identification, replacement for typeid - CallGraphSCC() : ModulePass(&ID) {} + CallGraphSCC() : ModulePass(ID) {} // run - Print out SCCs in the call graph for the specified module. bool runOnModule(Module &M); @@ -61,30 +61,30 @@ namespace { AU.addRequired<CallGraph>(); } }; +} - char CFGSCC::ID = 0; - RegisterPass<CFGSCC> - Y("print-cfg-sccs", "Print SCCs of each function CFG"); +char CFGSCC::ID = 0; +static RegisterPass<CFGSCC> +Y("print-cfg-sccs", "Print SCCs of each function CFG"); - char CallGraphSCC::ID = 0; - RegisterPass<CallGraphSCC> - Z("print-callgraph-sccs", "Print SCCs of the Call Graph"); -} +char CallGraphSCC::ID = 0; +static RegisterPass<CallGraphSCC> +Z("print-callgraph-sccs", "Print SCCs of the Call Graph"); bool CFGSCC::runOnFunction(Function &F) { unsigned sccNum = 0; - outs() << "SCCs for Function " << F.getName() << " in PostOrder:"; + errs() << "SCCs for Function " << F.getName() << " in PostOrder:"; for (scc_iterator<Function*> SCCI = scc_begin(&F), E = scc_end(&F); SCCI != E; ++SCCI) { std::vector<BasicBlock*> &nextSCC = *SCCI; - outs() << "\nSCC #" << ++sccNum << " : "; + errs() << "\nSCC #" << ++sccNum << " : "; for (std::vector<BasicBlock*>::const_iterator I = nextSCC.begin(), E = nextSCC.end(); I != E; ++I) - outs() << (*I)->getName() << ", "; + errs() << (*I)->getName() << ", "; if (nextSCC.size() == 1 && SCCI.hasLoop()) - outs() << " (Has self-loop)."; + errs() << " (Has self-loop)."; } - outs() << "\n"; + errs() << "\n"; return true; } @@ -94,19 +94,19 @@ bool CFGSCC::runOnFunction(Function &F) { bool CallGraphSCC::runOnModule(Module &M) { CallGraphNode* rootNode = getAnalysis<CallGraph>().getRoot(); unsigned sccNum = 0; - outs() << "SCCs for the program in PostOrder:"; + errs() << "SCCs for the program in PostOrder:"; for (scc_iterator<CallGraphNode*> SCCI = scc_begin(rootNode), E = scc_end(rootNode); SCCI != E; ++SCCI) { const std::vector<CallGraphNode*> &nextSCC = *SCCI; - outs() << "\nSCC #" << ++sccNum << " : "; + errs() << "\nSCC #" << ++sccNum << " : "; for (std::vector<CallGraphNode*>::const_iterator I = nextSCC.begin(), E = nextSCC.end(); I != E; ++I) - outs() << ((*I)->getFunction() ? (*I)->getFunction()->getNameStr() + errs() << ((*I)->getFunction() ? (*I)->getFunction()->getNameStr() : std::string("external node")) << ", "; if (nextSCC.size() == 1 && SCCI.hasLoop()) - outs() << " (Has self-loop)."; + errs() << " (Has self-loop)."; } - outs() << "\n"; + errs() << "\n"; return true; } diff --git a/contrib/llvm/tools/opt/opt.cpp b/contrib/llvm/tools/opt/opt.cpp index 0878737..d837185 100644 --- a/contrib/llvm/tools/opt/opt.cpp +++ b/contrib/llvm/tools/opt/opt.cpp @@ -53,7 +53,7 @@ InputFilename(cl::Positional, cl::desc("<input bitcode file>"), static cl::opt<std::string> OutputFilename("o", cl::desc("Override output filename"), - cl::value_desc("filename"), cl::init("-")); + cl::value_desc("filename")); static cl::opt<bool> Force("f", cl::desc("Enable binary output on terminals")); @@ -138,17 +138,19 @@ namespace { struct CallGraphSCCPassPrinter : public CallGraphSCCPass { static char ID; const PassInfo *PassToPrint; - CallGraphSCCPassPrinter(const PassInfo *PI) : - CallGraphSCCPass(&ID), PassToPrint(PI) {} + raw_ostream &Out; + CallGraphSCCPassPrinter(const PassInfo *PI, raw_ostream &out) : + CallGraphSCCPass(ID), PassToPrint(PI), Out(out) {} virtual bool runOnSCC(CallGraphSCC &SCC) { if (!Quiet) { - outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n"; + 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).print(outs(), F->getParent()); + getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, + F->getParent()); } } // Get and print pass... @@ -158,7 +160,7 @@ struct CallGraphSCCPassPrinter : public CallGraphSCCPass { virtual const char *getPassName() const { return "'Pass' Printer"; } virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequiredID(PassToPrint); + AU.addRequiredID(PassToPrint->getTypeInfo()); AU.setPreservesAll(); } }; @@ -168,13 +170,14 @@ char CallGraphSCCPassPrinter::ID = 0; struct ModulePassPrinter : public ModulePass { static char ID; const PassInfo *PassToPrint; - ModulePassPrinter(const PassInfo *PI) : ModulePass(&ID), - PassToPrint(PI) {} + raw_ostream &Out; + ModulePassPrinter(const PassInfo *PI, raw_ostream &out) + : ModulePass(ID), PassToPrint(PI), Out(out) {} virtual bool runOnModule(Module &M) { if (!Quiet) { - outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n"; - getAnalysisID<Pass>(PassToPrint).print(outs(), &M); + Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n"; + getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, &M); } // Get and print pass... @@ -184,7 +187,7 @@ struct ModulePassPrinter : public ModulePass { virtual const char *getPassName() const { return "'Pass' Printer"; } virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequiredID(PassToPrint); + AU.addRequiredID(PassToPrint->getTypeInfo()); AU.setPreservesAll(); } }; @@ -192,24 +195,26 @@ struct ModulePassPrinter : public ModulePass { char ModulePassPrinter::ID = 0; struct FunctionPassPrinter : public FunctionPass { const PassInfo *PassToPrint; + raw_ostream &Out; static char ID; - FunctionPassPrinter(const PassInfo *PI) : FunctionPass(&ID), - PassToPrint(PI) {} + FunctionPassPrinter(const PassInfo *PI, raw_ostream &out) + : FunctionPass(ID), PassToPrint(PI), Out(out) {} virtual bool runOnFunction(Function &F) { if (!Quiet) { - outs() << "Printing analysis '" << PassToPrint->getPassName() - << "' for function '" << F.getName() << "':\n"; + Out << "Printing analysis '" << PassToPrint->getPassName() + << "' for function '" << F.getName() << "':\n"; } // Get and print pass... - getAnalysisID<Pass>(PassToPrint).print(outs(), F.getParent()); + getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, + F.getParent()); return false; } virtual const char *getPassName() const { return "FunctionPass Printer"; } virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequiredID(PassToPrint); + AU.addRequiredID(PassToPrint->getTypeInfo()); AU.setPreservesAll(); } }; @@ -219,13 +224,14 @@ char FunctionPassPrinter::ID = 0; struct LoopPassPrinter : public LoopPass { static char ID; const PassInfo *PassToPrint; - LoopPassPrinter(const PassInfo *PI) : - LoopPass(&ID), PassToPrint(PI) {} + raw_ostream &Out; + LoopPassPrinter(const PassInfo *PI, raw_ostream &out) : + LoopPass(ID), PassToPrint(PI), Out(out) {} virtual bool runOnLoop(Loop *L, LPPassManager &LPM) { if (!Quiet) { - outs() << "Printing analysis '" << PassToPrint->getPassName() << "':\n"; - getAnalysisID<Pass>(PassToPrint).print(outs(), + Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n"; + getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, L->getHeader()->getParent()->getParent()); } // Get and print pass... @@ -235,7 +241,7 @@ struct LoopPassPrinter : public LoopPass { virtual const char *getPassName() const { return "'Pass' Printer"; } virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequiredID(PassToPrint); + AU.addRequiredID(PassToPrint->getTypeInfo()); AU.setPreservesAll(); } }; @@ -244,25 +250,27 @@ char LoopPassPrinter::ID = 0; struct BasicBlockPassPrinter : public BasicBlockPass { const PassInfo *PassToPrint; + raw_ostream &Out; static char ID; - BasicBlockPassPrinter(const PassInfo *PI) - : BasicBlockPass(&ID), PassToPrint(PI) {} + BasicBlockPassPrinter(const PassInfo *PI, raw_ostream &out) + : BasicBlockPass(ID), PassToPrint(PI), Out(out) {} virtual bool runOnBasicBlock(BasicBlock &BB) { if (!Quiet) { - outs() << "Printing Analysis info for BasicBlock '" << BB.getName() - << "': Pass " << PassToPrint->getPassName() << ":\n"; + Out << "Printing Analysis info for BasicBlock '" << BB.getName() + << "': Pass " << PassToPrint->getPassName() << ":\n"; } // Get and print pass... - getAnalysisID<Pass>(PassToPrint).print(outs(), BB.getParent()->getParent()); + getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, + BB.getParent()->getParent()); return false; } virtual const char *getPassName() const { return "BasicBlockPass Printer"; } virtual void getAnalysisUsage(AnalysisUsage &AU) const { - AU.addRequiredID(PassToPrint); + AU.addRequiredID(PassToPrint->getTypeInfo()); AU.setPreservesAll(); } }; @@ -351,6 +359,11 @@ void AddStandardLinkPasses(PassManagerBase &PM) { 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; @@ -377,35 +390,22 @@ int main(int argc, char **argv) { } // Figure out what stream we are supposed to write to... - raw_ostream *Out = 0; - bool DeleteStream = false; - if (!NoOutput && !AnalyzeOnly) { - if (OutputFilename == "-") { - // Print to stdout. - Out = &outs(); - // If we're printing a bitcode file, switch stdout to binary mode. - // FIXME: This switches outs() globally, not just for the bitcode output. - if (!OutputAssembly) - sys::Program::ChangeStdoutToBinary(); - } else { - if (NoOutput || AnalyzeOnly) { - errs() << "WARNING: The -o (output filename) option is ignored when\n" - "the --disable-output or --analyze options are used.\n"; - } else { - // Make sure that the Output file gets unlinked from the disk if we get - // a SIGINT. - sys::RemoveFileOnSignal(sys::Path(OutputFilename)); - - std::string ErrorInfo; - Out = new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo, - raw_fd_ostream::F_Binary); - if (!ErrorInfo.empty()) { - errs() << ErrorInfo << '\n'; - delete Out; - return 1; - } - DeleteStream = true; - } + OwningPtr<tool_output_file> Out; + if (NoOutput) { + if (!OutputFilename.empty()) + errs() << "WARNING: The -o (output filename) option is ignored when\n" + "the --disable-output option is used.\n"; + } else { + // Default to standard output. + 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; } } @@ -413,7 +413,7 @@ int main(int argc, char **argv) { // console, print out a warning message and refuse to do it. We don't // impress anyone by spewing tons of binary goo to a terminal. if (!Force && !NoOutput && !AnalyzeOnly && !OutputAssembly) - if (CheckBitcodeOutputToConsole(*Out, !Quiet)) + if (CheckBitcodeOutputToConsole(Out->os(), !Quiet)) NoOutput = true; // Create a PassManager to hold and optimize the collection of passes we are @@ -489,19 +489,19 @@ int main(int argc, char **argv) { if (AnalyzeOnly) { switch (Kind) { case PT_BasicBlock: - Passes.add(new BasicBlockPassPrinter(PassInf)); + Passes.add(new BasicBlockPassPrinter(PassInf, Out->os())); break; case PT_Loop: - Passes.add(new LoopPassPrinter(PassInf)); + Passes.add(new LoopPassPrinter(PassInf, Out->os())); break; case PT_Function: - Passes.add(new FunctionPassPrinter(PassInf)); + Passes.add(new FunctionPassPrinter(PassInf, Out->os())); break; case PT_CallGraphSCC: - Passes.add(new CallGraphSCCPassPrinter(PassInf)); + Passes.add(new CallGraphSCCPassPrinter(PassInf, Out->os())); break; default: - Passes.add(new ModulePassPrinter(PassInf)); + Passes.add(new ModulePassPrinter(PassInf, Out->os())); break; } } @@ -538,19 +538,20 @@ int main(int argc, char **argv) { if (!NoVerify && !VerifyEach) Passes.add(createVerifierPass()); - // Write bitcode or assembly out to disk or outs() as the last step... + // Write bitcode or assembly to the output as the last step... if (!NoOutput && !AnalyzeOnly) { if (OutputAssembly) - Passes.add(createPrintModulePass(Out)); + Passes.add(createPrintModulePass(&Out->os())); else - Passes.add(createBitcodeWriterPass(*Out)); + Passes.add(createBitcodeWriterPass(Out->os())); } // Now that we have all of the passes ready, run them. Passes.run(*M.get()); - // Delete the raw_fd_ostream. - if (DeleteStream) - delete Out; + // Declare success. + if (!NoOutput) + Out->keep(); + return 0; } |