1 //===----------------------------------------------------------------------===// 2 // LLVM Modular Optimizer Utility: opt 3 // 4 // Optimizations may be specified an arbitrary number of times on the command 5 // line, they are run in the order specified. 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/Module.h" 10 #include "llvm/PassManager.h" 11 #include "llvm/Bytecode/Reader.h" 12 #include "llvm/Bytecode/WriteBytecodePass.h" 13 #include "llvm/Assembly/PrintModulePass.h" 14 #include "llvm/Analysis/Verifier.h" 15 #include "llvm/Target/TargetMachine.h" 16 #include "llvm/Target/TargetMachineImpls.h" 17 #include "llvm/Support/PassNameParser.h" 18 #include "Support/Signals.h" 19 #include <fstream> 20 #include <memory> 21 #include <algorithm> 22 23 24 // The OptimizationList is automatically populated with registered Passes by the 25 // PassNameParser. 26 // 27 static cl::list<const PassInfo*, bool, 28 FilteredPassNameParser<PassInfo::Optimization> > 29 OptimizationList(cl::desc("Optimizations available:")); 30 31 32 // Other command line options... 33 // 34 static cl::opt<std::string> 35 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-")); 36 37 static cl::opt<std::string> 38 OutputFilename("o", cl::desc("Override output filename"), 39 cl::value_desc("filename")); 40 41 static cl::opt<bool> 42 Force("f", cl::desc("Overwrite output files")); 43 44 static cl::opt<bool> 45 PrintEachXForm("p", cl::desc("Print module after each transformation")); 46 47 static cl::opt<bool> 48 NoOutput("disable-output", 49 cl::desc("Do not write result bytecode file"), cl::Hidden); 50 51 static cl::opt<bool> 52 NoVerify("disable-verify", cl::desc("Do not verify result module"), cl::Hidden); 53 54 static cl::opt<bool> 55 Quiet("q", cl::desc("Don't print 'program modified' message")); 56 57 static cl::alias 58 QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet)); 59 60 61 //===----------------------------------------------------------------------===// 62 // main for opt 63 // 64 int main(int argc, char **argv) { 65 cl::ParseCommandLineOptions(argc, argv, 66 " llvm .bc -> .bc modular optimizer\n"); 67 68 // Allocate a full target machine description only if necessary... 69 // FIXME: The choice of target should be controllable on the command line. 70 std::auto_ptr<TargetMachine> target; 71 72 TargetMachine* TM = NULL; 73 std::string ErrorMessage; 74 75 // Load the input module... 76 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename, &ErrorMessage)); 77 if (M.get() == 0) { 78 std::cerr << argv[0] << ": "; 79 if (ErrorMessage.size()) 80 std::cerr << ErrorMessage << "\n"; 81 else 82 std::cerr << "bytecode didn't read correctly.\n"; 83 return 1; 84 } 85 86 // Figure out what stream we are supposed to write to... 87 std::ostream *Out = &std::cout; // Default to printing to stdout... 88 if (OutputFilename != "") { 89 if (!Force && std::ifstream(OutputFilename.c_str())) { 90 // If force is not specified, make sure not to overwrite a file! 91 std::cerr << argv[0] << ": error opening '" << OutputFilename 92 << "': file exists!\n" 93 << "Use -f command line argument to force output\n"; 94 return 1; 95 } 96 Out = new std::ofstream(OutputFilename.c_str()); 97 98 if (!Out->good()) { 99 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n"; 100 return 1; 101 } 102 103 // Make sure that the Output file gets unlink'd from the disk if we get a 104 // SIGINT 105 RemoveFileOnSignal(OutputFilename); 106 } 107 108 // Create a PassManager to hold and optimize the collection of passes we are 109 // about to build... 110 // 111 PassManager Passes; 112 113 // Add an appropriate TargetData instance for this module... 114 Passes.add(new TargetData("opt", M.get())); 115 116 // Create a new optimization pass for each one specified on the command line 117 for (unsigned i = 0; i < OptimizationList.size(); ++i) { 118 const PassInfo *Opt = OptimizationList[i]; 119 120 if (Opt->getNormalCtor()) 121 Passes.add(Opt->getNormalCtor()()); 122 else if (Opt->getTargetCtor()) { 123 #if 0 124 if (target.get() == NULL) 125 target.reset(allocateSparcTargetMachine()); // FIXME: target option 126 #endif 127 assert(target.get() && "Could not allocate target machine!"); 128 Passes.add(Opt->getTargetCtor()(*target.get())); 129 } else 130 std::cerr << argv[0] << ": cannot create pass: " << Opt->getPassName() 131 << "\n"; 132 133 if (PrintEachXForm) 134 Passes.add(new PrintModulePass(&std::cerr)); 135 } 136 137 // Check that the module is well formed on completion of optimization 138 if (!NoVerify) 139 Passes.add(createVerifierPass()); 140 141 // Write bytecode out to disk or cout as the last step... 142 if (!NoOutput) 143 Passes.add(new WriteBytecodePass(Out, Out != &std::cout)); 144 145 // Now that we have all of the passes ready, run them. 146 if (Passes.run(*M.get()) && !Quiet) 147 std::cerr << "Program modified.\n"; 148 149 return 0; 150 } 151