Chris Lattner | 3dc67dd | 2002-01-22 03:30:46 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
Chris Lattner | ecbde33 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 2 | // LLVM 'GCCAS' UTILITY |
| 3 | // |
| 4 | // This utility is designed to be used by the GCC frontend for creating |
| 5 | // bytecode files from it's intermediate llvm assembly. The requirements for |
| 6 | // this utility are thus slightly different than that of the standard as util. |
| 7 | // |
Chris Lattner | 3dc67dd | 2002-01-22 03:30:46 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | ecbde33 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 9 | |
| 10 | #include "llvm/Module.h" |
Chris Lattner | 0f3bfff | 2002-01-31 00:46:22 +0000 | [diff] [blame] | 11 | #include "llvm/PassManager.h" |
Chris Lattner | ecbde33 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 12 | #include "llvm/Assembly/Parser.h" |
| 13 | #include "llvm/Transforms/CleanupGCCOutput.h" |
Chris Lattner | 068f487 | 2001-11-01 02:41:09 +0000 | [diff] [blame] | 14 | #include "llvm/Transforms/LevelChange.h" |
Chris Lattner | dbe0514 | 2001-10-31 06:36:48 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/ConstantMerge.h" |
Chris Lattner | d7db863 | 2002-01-22 01:04:08 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/ChangeAllocations.h" |
Chris Lattner | 65f1b89 | 2002-05-07 20:03:27 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 624c3e0 | 2002-06-25 15:57:43 +0000 | [diff] [blame^] | 18 | #include "llvm/Analysis/Verifier.h" |
Chris Lattner | 3dc67dd | 2002-01-22 03:30:46 +0000 | [diff] [blame] | 19 | #include "llvm/Bytecode/WriteBytecodePass.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 20 | #include "Support/CommandLine.h" |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 21 | #include "Support/Signals.h" |
Chris Lattner | ecbde33 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 22 | #include <memory> |
| 23 | #include <fstream> |
Chris Lattner | ecbde33 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 624c3e0 | 2002-06-25 15:57:43 +0000 | [diff] [blame^] | 25 | static cl::String InputFilename ("", "Parse <arg> file, compile to bytecode", |
| 26 | cl::Required, ""); |
| 27 | static cl::String OutputFilename ("o", "Override output filename"); |
| 28 | static cl::Int RunNPasses ("stopAfterNPasses", "Only run the first N " |
| 29 | "passes of gccas", cl::Hidden); |
| 30 | static cl::Flag StopAtLevelRaise("stopraise", "Stop optimization before " |
| 31 | "level raise", cl::Hidden); |
| 32 | static cl::Flag Verify ("verify", "Verify each pass result"); |
| 33 | |
| 34 | static inline void addPass(PassManager &PM, Pass *P) { |
| 35 | static int NumPassesCreated = 0; |
| 36 | |
| 37 | // If we haven't already created the number of passes that was requested... |
| 38 | if (RunNPasses == 0 || RunNPasses > NumPassesCreated) { |
| 39 | // Add the pass to the pass manager... |
| 40 | PM.add(P); |
| 41 | |
| 42 | // If we are verifying all of the intermediate steps, add the verifier... |
| 43 | if (Verify) PM.add(createVerifierPass()); |
| 44 | |
| 45 | // Keep track of how many passes we made for -stopAfterNPasses |
| 46 | ++NumPassesCreated; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | |
| 51 | void AddConfiguredTransformationPasses(PassManager &PM) { |
| 52 | if (Verify) PM.add(createVerifierPass()); |
| 53 | |
| 54 | addPass(PM, createFunctionResolvingPass()); // Resolve (...) functions |
| 55 | addPass(PM, createConstantMergePass()); // Merge dup global constants |
| 56 | addPass(PM, createDeadInstEliminationPass()); // Remove Dead code/vars |
| 57 | addPass(PM, createRaiseAllocationsPass()); // call %malloc -> malloc inst |
| 58 | addPass(PM, createCleanupGCCOutputPass()); // Fix gccisms |
| 59 | addPass(PM, createIndVarSimplifyPass()); // Simplify indvars |
| 60 | |
| 61 | // Level raise is eternally buggy/in need of enhancements. Allow |
| 62 | // transformation to stop right before it runs. |
| 63 | if (StopAtLevelRaise) return; |
| 64 | |
| 65 | addPass(PM, createRaisePointerReferencesPass());// Eliminate casts |
| 66 | addPass(PM, createPromoteMemoryToRegister()); // Promote alloca's to regs |
| 67 | addPass(PM, createReassociatePass()); // Reassociate expressions |
| 68 | addPass(PM, createInstructionCombiningPass()); // Combine silly seq's |
| 69 | addPass(PM, createDeadInstEliminationPass()); // Kill InstCombine remnants |
| 70 | addPass(PM, createLICMPass()); // Hoist loop invariants |
| 71 | addPass(PM, createGCSEPass()); // Remove common subexprs |
| 72 | addPass(PM, createSCCPPass()); // Constant prop with SCCP |
| 73 | |
| 74 | // Run instcombine after redundancy elimination to exploit opportunities |
| 75 | // opened up by them. |
| 76 | addPass(PM, createInstructionCombiningPass()); |
| 77 | addPass(PM, createAggressiveDCEPass()); // SSA based 'Agressive DCE' |
| 78 | addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs |
| 79 | } |
| 80 | |
Chris Lattner | ecbde33 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 81 | |
| 82 | int main(int argc, char **argv) { |
Chris Lattner | 11c862c | 2001-10-31 04:33:33 +0000 | [diff] [blame] | 83 | cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n"); |
Chris Lattner | ecbde33 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 84 | |
Chris Lattner | ecbde33 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 85 | std::auto_ptr<Module> M; |
| 86 | try { |
| 87 | // Parse the file now... |
| 88 | M.reset(ParseAssemblyFile(InputFilename)); |
| 89 | } catch (const ParseException &E) { |
| 90 | cerr << E.getMessage() << endl; |
| 91 | return 1; |
| 92 | } |
| 93 | |
| 94 | if (M.get() == 0) { |
| 95 | cerr << "assembly didn't read correctly.\n"; |
| 96 | return 1; |
| 97 | } |
| 98 | |
| 99 | if (OutputFilename == "") { // Didn't specify an output filename? |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 100 | std::string IFN = InputFilename; |
Chris Lattner | ecbde33 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 101 | int Len = IFN.length(); |
| 102 | if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s? |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 103 | OutputFilename = std::string(IFN.begin(), IFN.end()-2); |
Chris Lattner | ecbde33 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 104 | } else { |
| 105 | OutputFilename = IFN; // Append a .o to it |
| 106 | } |
| 107 | OutputFilename += ".o"; |
| 108 | } |
| 109 | |
Chris Lattner | 3dc67dd | 2002-01-22 03:30:46 +0000 | [diff] [blame] | 110 | std::ofstream Out(OutputFilename.c_str(), ios::out); |
| 111 | if (!Out.good()) { |
Chris Lattner | ecbde33 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 112 | cerr << "Error opening " << OutputFilename << "!\n"; |
| 113 | return 1; |
| 114 | } |
| 115 | |
Chris Lattner | 76d1229 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 116 | // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT |
| 117 | RemoveFileOnSignal(OutputFilename); |
| 118 | |
Chris Lattner | ecbde33 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 119 | // In addition to just parsing the input from GCC, we also want to spiff it up |
| 120 | // a little bit. Do this now. |
| 121 | // |
Chris Lattner | f4de63f | 2002-01-21 07:31:50 +0000 | [diff] [blame] | 122 | PassManager Passes; |
Chris Lattner | 4ad5322 | 2002-05-14 16:23:14 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 624c3e0 | 2002-06-25 15:57:43 +0000 | [diff] [blame^] | 124 | // Add all of the transformation passes to the pass manager to do the cleanup |
| 125 | // and optimization of the GCC output. |
| 126 | // |
| 127 | AddConfiguredTransformationPasses(Passes); |
| 128 | |
| 129 | // Write bytecode to file... |
| 130 | Passes.add(new WriteBytecodePass(&Out)); |
Chris Lattner | ecbde33 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 131 | |
Chris Lattner | 3dc67dd | 2002-01-22 03:30:46 +0000 | [diff] [blame] | 132 | // Run our queue of passes all at once now, efficiently. |
Chris Lattner | 624c3e0 | 2002-06-25 15:57:43 +0000 | [diff] [blame^] | 133 | Passes.run(*M.get()); |
Chris Lattner | ecbde33 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 134 | return 0; |
| 135 | } |