| Chris Lattner | 3334c88 | 2003-10-15 21:49:57 +0000 | [diff] [blame] | 1 | //===-- gccas.cpp - The "optimizing assembler" used by the GCC frontend ---===// |
| John Criswell | 09344dc | 2003-10-20 17:47:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| Chris Lattner | 1a306176 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 9 | // |
| Misha Brukman | 250bf36 | 2003-08-07 21:23:52 +0000 | [diff] [blame] | 10 | // This utility is designed to be used by the GCC frontend for creating bytecode |
| 11 | // files from its intermediate LLVM assembly. The requirements for this utility |
| 12 | // are thus slightly different than that of the standard `as' util. |
| Chris Lattner | 1a306176 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 13 | // |
| Chris Lattner | 3bb02e4 | 2002-01-22 03:30:46 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| Chris Lattner | 1a306176 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 15 | |
| 16 | #include "llvm/Module.h" |
| Chris Lattner | ad50ec2 | 2002-01-31 00:46:22 +0000 | [diff] [blame] | 17 | #include "llvm/PassManager.h" |
| Misha Brukman | 250bf36 | 2003-08-07 21:23:52 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/LoadValueNumbering.h" |
| 19 | #include "llvm/Analysis/Verifier.h" |
| Chris Lattner | 1a306176 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 20 | #include "llvm/Assembly/Parser.h" |
| Misha Brukman | 250bf36 | 2003-08-07 21:23:52 +0000 | [diff] [blame] | 21 | #include "llvm/Bytecode/WriteBytecodePass.h" |
| 22 | #include "llvm/Target/TargetData.h" |
| Chris Lattner | 35c4541 | 2002-07-23 22:04:43 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/IPO.h" |
| Chris Lattner | 89a20ef | 2002-05-07 20:03:27 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/Scalar.h" |
| Chris Lattner | 5de2204 | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 25 | #include "Support/CommandLine.h" |
| Chris Lattner | c065ad8 | 2002-04-18 19:55:25 +0000 | [diff] [blame] | 26 | #include "Support/Signals.h" |
| Chris Lattner | 1a306176 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 27 | #include <memory> |
| 28 | #include <fstream> |
| Chris Lattner | 1a306176 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 29 | |
| Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
| Chris Lattner | 1f412ae | 2003-04-16 17:34:29 +0000 | [diff] [blame] | 32 | namespace { |
| Chris Lattner | 1f412ae | 2003-04-16 17:34:29 +0000 | [diff] [blame] | 33 | cl::opt<std::string> |
| Chris Lattner | 12fe9b4 | 2003-04-16 17:49:18 +0000 | [diff] [blame] | 34 | InputFilename(cl::Positional,cl::desc("<input llvm assembly>"),cl::init("-")); |
| Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 35 | |
| Chris Lattner | 1f412ae | 2003-04-16 17:34:29 +0000 | [diff] [blame] | 36 | cl::opt<std::string> |
| 37 | OutputFilename("o", cl::desc("Override output filename"), |
| 38 | cl::value_desc("filename")); |
| Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 39 | |
| Chris Lattner | 1f412ae | 2003-04-16 17:34:29 +0000 | [diff] [blame] | 40 | cl::opt<bool> |
| 41 | Verify("verify", cl::desc("Verify each pass result")); |
| Chris Lattner | e01ba00 | 2003-10-10 18:18:53 +0000 | [diff] [blame] | 42 | |
| 43 | cl::opt<bool> |
| 44 | DisableInline("disable-inlining", cl::desc("Do not run the inliner pass")); |
| Chris Lattner | 1f412ae | 2003-04-16 17:34:29 +0000 | [diff] [blame] | 45 | } |
| Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 46 | |
| Chris Lattner | 5aa3a07 | 2002-06-25 15:57:43 +0000 | [diff] [blame] | 47 | |
| 48 | static inline void addPass(PassManager &PM, Pass *P) { |
| Chris Lattner | ad7b556 | 2003-08-31 21:47:24 +0000 | [diff] [blame] | 49 | // Add the pass to the pass manager... |
| 50 | PM.add(P); |
| Chris Lattner | 5aa3a07 | 2002-06-25 15:57:43 +0000 | [diff] [blame] | 51 | |
| Chris Lattner | ad7b556 | 2003-08-31 21:47:24 +0000 | [diff] [blame] | 52 | // If we are verifying all of the intermediate steps, add the verifier... |
| 53 | if (Verify) PM.add(createVerifierPass()); |
| Chris Lattner | 5aa3a07 | 2002-06-25 15:57:43 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | |
| 57 | void AddConfiguredTransformationPasses(PassManager &PM) { |
| Chris Lattner | e81e0d8 | 2003-06-22 20:11:45 +0000 | [diff] [blame] | 58 | PM.add(createVerifierPass()); // Verify that input is correct |
| Chris Lattner | cf82b00 | 2003-09-15 04:56:44 +0000 | [diff] [blame] | 59 | addPass(PM, createLowerSetJmpPass()); // Lower llvm.setjmp/.longjmp |
| Chris Lattner | 3f5b426 | 2002-08-30 22:55:32 +0000 | [diff] [blame] | 60 | addPass(PM, createFunctionResolvingPass()); // Resolve (...) functions |
| Chris Lattner | db8a179 | 2003-11-22 19:07:47 +0000 | [diff] [blame^] | 61 | addPass(PM, createCFGSimplificationPass()); // Clean up disgusting code |
| Chris Lattner | 52b4fb3 | 2003-11-21 21:44:35 +0000 | [diff] [blame] | 62 | addPass(PM, createRaiseAllocationsPass()); // call %malloc -> malloc inst |
| Chris Lattner | db8a179 | 2003-11-22 19:07:47 +0000 | [diff] [blame^] | 63 | addPass(PM, createGlobalDCEPass()); // Remove unused globals |
| Chris Lattner | 95ee0b0 | 2003-10-23 18:25:57 +0000 | [diff] [blame] | 64 | addPass(PM, createIPConstantPropagationPass());// IP Constant Propagation |
| 65 | addPass(PM, createDeadArgEliminationPass()); // Dead argument elimination |
| 66 | |
| Chris Lattner | 6f21fe9 | 2003-08-31 21:45:55 +0000 | [diff] [blame] | 67 | addPass(PM, createPruneEHPass()); // Remove dead EH info |
| Chris Lattner | e01ba00 | 2003-10-10 18:18:53 +0000 | [diff] [blame] | 68 | |
| 69 | if (!DisableInline) |
| 70 | addPass(PM, createFunctionInliningPass()); // Inline small functions |
| Chris Lattner | 6f21fe9 | 2003-08-31 21:45:55 +0000 | [diff] [blame] | 71 | |
| Chris Lattner | e22031e | 2003-05-02 18:19:05 +0000 | [diff] [blame] | 72 | addPass(PM, createInstructionCombiningPass()); // Cleanup code for raise |
| Chris Lattner | 093cc6d | 2003-11-13 19:26:54 +0000 | [diff] [blame] | 73 | |
| 74 | // HACK HACK HACK. This pass should be extended to support calls like 'call |
| 75 | // (const expr cast (free))(Ty *). Until it does so, we have to run it after |
| 76 | // instruction combining. This should be removed after PLDI! |
| 77 | addPass(PM, createRaiseAllocationsPass()); // call %malloc -> malloc inst |
| 78 | |
| Chris Lattner | 3f8b7c2 | 2003-10-16 16:50:34 +0000 | [diff] [blame] | 79 | addPass(PM, createRaisePointerReferencesPass());// Recover type information |
| Chris Lattner | e81e0d8 | 2003-06-22 20:11:45 +0000 | [diff] [blame] | 80 | addPass(PM, createTailDuplicationPass()); // Simplify cfg by copying code |
| 81 | addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs |
| Chris Lattner | fdb2f4b | 2003-05-30 19:24:06 +0000 | [diff] [blame] | 82 | addPass(PM, createScalarReplAggregatesPass()); // Break up aggregate allocas |
| Chris Lattner | 25963c8 | 2003-09-20 05:26:22 +0000 | [diff] [blame] | 83 | addPass(PM, createTailCallEliminationPass()); // Eliminate tail calls |
| Chris Lattner | 6f21fe9 | 2003-08-31 21:45:55 +0000 | [diff] [blame] | 84 | addPass(PM, createInstructionCombiningPass()); // Combine silly seq's |
| 85 | |
| Chris Lattner | f125759 | 2002-10-31 17:13:11 +0000 | [diff] [blame] | 86 | addPass(PM, createReassociatePass()); // Reassociate expressions |
| Chris Lattner | c70222d | 2002-09-06 18:41:33 +0000 | [diff] [blame] | 87 | addPass(PM, createInstructionCombiningPass()); // Combine silly seq's |
| 88 | addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs |
| Chris Lattner | 3f5b426 | 2002-08-30 22:55:32 +0000 | [diff] [blame] | 89 | addPass(PM, createLICMPass()); // Hoist loop invariants |
| 90 | addPass(PM, createLoadValueNumberingPass()); // GVN for load instructions |
| 91 | addPass(PM, createGCSEPass()); // Remove common subexprs |
| 92 | addPass(PM, createSCCPPass()); // Constant prop with SCCP |
| Chris Lattner | 5aa3a07 | 2002-06-25 15:57:43 +0000 | [diff] [blame] | 93 | |
| 94 | // Run instcombine after redundancy elimination to exploit opportunities |
| 95 | // opened up by them. |
| 96 | addPass(PM, createInstructionCombiningPass()); |
| Chris Lattner | 2f6de8b | 2003-09-11 16:34:07 +0000 | [diff] [blame] | 97 | addPass(PM, createIndVarSimplifyPass()); // Canonicalize indvars |
| Chris Lattner | e81e0d8 | 2003-06-22 20:11:45 +0000 | [diff] [blame] | 98 | addPass(PM, createAggressiveDCEPass()); // SSA based 'Aggressive DCE' |
| Chris Lattner | c70222d | 2002-09-06 18:41:33 +0000 | [diff] [blame] | 99 | addPass(PM, createCFGSimplificationPass()); // Merge & remove BBs |
| Chris Lattner | 6f21fe9 | 2003-08-31 21:45:55 +0000 | [diff] [blame] | 100 | addPass(PM, createDeadTypeEliminationPass()); // Eliminate dead types |
| 101 | addPass(PM, createConstantMergePass()); // Merge dup global constants |
| Chris Lattner | 5aa3a07 | 2002-06-25 15:57:43 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| Chris Lattner | 1a306176 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 104 | |
| 105 | int main(int argc, char **argv) { |
| Chris Lattner | ae6de30 | 2001-10-31 04:33:33 +0000 | [diff] [blame] | 106 | cl::ParseCommandLineOptions(argc, argv, " llvm .s -> .o assembler for GCC\n"); |
| Chris Lattner | 1a306176 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 107 | |
| Chris Lattner | 1a306176 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 108 | std::auto_ptr<Module> M; |
| 109 | try { |
| 110 | // Parse the file now... |
| 111 | M.reset(ParseAssemblyFile(InputFilename)); |
| 112 | } catch (const ParseException &E) { |
| Chris Lattner | 3708870 | 2003-04-16 17:41:08 +0000 | [diff] [blame] | 113 | std::cerr << argv[0] << ": " << E.getMessage() << "\n"; |
| Chris Lattner | 1a306176 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 114 | return 1; |
| 115 | } |
| 116 | |
| 117 | if (M.get() == 0) { |
| Chris Lattner | 3708870 | 2003-04-16 17:41:08 +0000 | [diff] [blame] | 118 | std::cerr << argv[0] << ": assembly didn't read correctly.\n"; |
| Chris Lattner | 1a306176 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 119 | return 1; |
| 120 | } |
| Chris Lattner | 12fe9b4 | 2003-04-16 17:49:18 +0000 | [diff] [blame] | 121 | |
| 122 | std::ostream *Out = 0; |
| Chris Lattner | 1a306176 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 123 | if (OutputFilename == "") { // Didn't specify an output filename? |
| Chris Lattner | 12fe9b4 | 2003-04-16 17:49:18 +0000 | [diff] [blame] | 124 | if (InputFilename == "-") { |
| 125 | OutputFilename = "-"; |
| Chris Lattner | 1a306176 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 126 | } else { |
| Chris Lattner | 12fe9b4 | 2003-04-16 17:49:18 +0000 | [diff] [blame] | 127 | std::string IFN = InputFilename; |
| 128 | int Len = IFN.length(); |
| 129 | if (IFN[Len-2] == '.' && IFN[Len-1] == 's') { // Source ends in .s? |
| 130 | OutputFilename = std::string(IFN.begin(), IFN.end()-2); |
| 131 | } else { |
| 132 | OutputFilename = IFN; // Append a .o to it |
| 133 | } |
| 134 | OutputFilename += ".o"; |
| Chris Lattner | 1a306176 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 135 | } |
| Chris Lattner | 1a306176 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| Chris Lattner | 12fe9b4 | 2003-04-16 17:49:18 +0000 | [diff] [blame] | 138 | if (OutputFilename == "-") |
| 139 | Out = &std::cout; |
| 140 | else { |
| 141 | Out = new std::ofstream(OutputFilename.c_str(), std::ios::out); |
| 142 | |
| Misha Brukman | d676974 | 2003-10-10 17:56:49 +0000 | [diff] [blame] | 143 | // Make sure that the Out file gets unlinked from the disk if we get a |
| Chris Lattner | 12fe9b4 | 2003-04-16 17:49:18 +0000 | [diff] [blame] | 144 | // signal |
| 145 | RemoveFileOnSignal(OutputFilename); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | if (!Out->good()) { |
| Chris Lattner | 3708870 | 2003-04-16 17:41:08 +0000 | [diff] [blame] | 150 | std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n"; |
| Chris Lattner | 1a306176 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 151 | return 1; |
| 152 | } |
| 153 | |
| 154 | // In addition to just parsing the input from GCC, we also want to spiff it up |
| 155 | // a little bit. Do this now. |
| 156 | // |
| Chris Lattner | 0686e43 | 2002-01-21 07:31:50 +0000 | [diff] [blame] | 157 | PassManager Passes; |
| Chris Lattner | c914944 | 2002-05-14 16:23:14 +0000 | [diff] [blame] | 158 | |
| Chris Lattner | d571e2a | 2003-04-24 19:13:02 +0000 | [diff] [blame] | 159 | // Add an appropriate TargetData instance for this module... |
| 160 | Passes.add(new TargetData("gccas", M.get())); |
| 161 | |
| Chris Lattner | 5aa3a07 | 2002-06-25 15:57:43 +0000 | [diff] [blame] | 162 | // Add all of the transformation passes to the pass manager to do the cleanup |
| 163 | // and optimization of the GCC output. |
| 164 | // |
| 165 | AddConfiguredTransformationPasses(Passes); |
| 166 | |
| 167 | // Write bytecode to file... |
| Chris Lattner | 12fe9b4 | 2003-04-16 17:49:18 +0000 | [diff] [blame] | 168 | Passes.add(new WriteBytecodePass(Out)); |
| Chris Lattner | 1a306176 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 169 | |
| Chris Lattner | 3bb02e4 | 2002-01-22 03:30:46 +0000 | [diff] [blame] | 170 | // Run our queue of passes all at once now, efficiently. |
| Chris Lattner | 5aa3a07 | 2002-06-25 15:57:43 +0000 | [diff] [blame] | 171 | Passes.run(*M.get()); |
| Chris Lattner | 12fe9b4 | 2003-04-16 17:49:18 +0000 | [diff] [blame] | 172 | |
| 173 | if (Out != &std::cout) delete Out; |
| Chris Lattner | 1a306176 | 2001-10-31 04:28:11 +0000 | [diff] [blame] | 174 | return 0; |
| 175 | } |