Chris Lattner | b27d474 | 2001-10-04 01:40:53 +0000 | [diff] [blame] | 1 | //===-- llc.cpp - Implement the LLVM Compiler -----------------------------===// |
Chris Lattner | 2cf137b | 2001-09-07 22:20:50 +0000 | [diff] [blame] | 2 | // |
| 3 | // This is the llc compiler driver. |
| 4 | // |
Chris Lattner | b27d474 | 2001-10-04 01:40:53 +0000 | [diff] [blame] | 5 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | 2d94a34 | 2001-07-21 12:42:29 +0000 | [diff] [blame] | 6 | |
Vikram S. Adve | 2d94a34 | 2001-07-21 12:42:29 +0000 | [diff] [blame] | 7 | #include "llvm/Bytecode/Reader.h" |
Chris Lattner | 22a6a90 | 2001-09-14 05:34:53 +0000 | [diff] [blame] | 8 | #include "llvm/Target/Sparc.h" |
Vikram S. Adve | 9d40935 | 2001-09-18 13:10:45 +0000 | [diff] [blame] | 9 | #include "llvm/Target/TargetMachine.h" |
Vikram S. Adve | 2f084b2 | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 10 | #include "llvm/Transforms/Instrumentation/TraceValues.h" |
Chris Lattner | 97fd6c4 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 11 | #include "llvm/Transforms/LowerAllocations.h" |
| 12 | #include "llvm/Transforms/HoistPHIConstants.h" |
| 13 | #include "llvm/Transforms/PrintModulePass.h" |
Chris Lattner | e2472bb | 2001-07-23 17:46:59 +0000 | [diff] [blame] | 14 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | ed22606 | 2001-09-07 21:26:31 +0000 | [diff] [blame] | 15 | #include "llvm/Module.h" |
| 16 | #include "llvm/Method.h" |
Chris Lattner | 6c2c870 | 2001-09-18 17:04:18 +0000 | [diff] [blame] | 17 | #include <memory> |
Vikram S. Adve | 2f084b2 | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 18 | #include <string> |
Chris Lattner | 46f1b61 | 2001-09-19 16:52:09 +0000 | [diff] [blame] | 19 | #include <fstream> |
Chris Lattner | 0af2464 | 2001-07-23 02:35:57 +0000 | [diff] [blame] | 20 | |
Chris Lattner | ab0cc40 | 2001-07-23 19:27:24 +0000 | [diff] [blame] | 21 | cl::String InputFilename ("", "Input filename", cl::NoFlags, "-"); |
Chris Lattner | 0af2464 | 2001-07-23 02:35:57 +0000 | [diff] [blame] | 22 | cl::String OutputFilename("o", "Output filename", cl::NoFlags, ""); |
Chris Lattner | 46f1b61 | 2001-09-19 16:52:09 +0000 | [diff] [blame] | 23 | cl::Flag Force ("f", "Overwrite output files", cl::NoFlags, false); |
Vikram S. Adve | 2f084b2 | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 24 | cl::Flag DumpAsm ("d", "Print bytecode before native code generation", cl::Hidden,false); |
| 25 | cl::Flag DoNotEmitAssembly("noasm", "Do not emit assembly code", cl::Hidden, false); |
| 26 | cl::Flag TraceBBValues ("trace", |
| 27 | "Trace values at basic block and method exits", |
| 28 | cl::NoFlags, false); |
| 29 | cl::Flag TraceMethodValues("tracem", "Trace values only at method exits", |
| 30 | cl::NoFlags, false); |
Chris Lattner | 97fd6c4 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 31 | cl::Flag DebugTrace ("dumptrace", |
| 32 | "output trace code to a <fn>.trace.ll file", |
| 33 | cl::Hidden, false); |
Chris Lattner | 0af2464 | 2001-07-23 02:35:57 +0000 | [diff] [blame] | 34 | |
Vikram S. Adve | 9d40935 | 2001-09-18 13:10:45 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 97fd6c4 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 36 | // GetFileNameRoot - Helper function to get the basename of a filename... |
| 37 | static inline string GetFileNameRoot(const string &InputFilename) { |
Vikram S. Adve | 2f084b2 | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 38 | string IFN = InputFilename; |
| 39 | string outputFilename; |
| 40 | int Len = IFN.length(); |
| 41 | if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') { |
| 42 | outputFilename = string(IFN.begin(), IFN.end()-3); // s/.bc/.s/ |
| 43 | } else { |
Chris Lattner | 97fd6c4 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 44 | outputFilename = IFN; |
Vikram S. Adve | 2f084b2 | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 45 | } |
| 46 | return outputFilename; |
| 47 | } |
| 48 | |
Vikram S. Adve | 9d40935 | 2001-09-18 13:10:45 +0000 | [diff] [blame] | 49 | |
| 50 | //===---------------------------------------------------------------------===// |
Chris Lattner | 97fd6c4 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 51 | // GenerateCodeForTarget Pass |
Vikram S. Adve | 2f084b2 | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 52 | // |
| 53 | // Native code generation for a specified target. |
| 54 | //===---------------------------------------------------------------------===// |
| 55 | |
Chris Lattner | 97fd6c4 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 56 | class GenerateCodeForTarget : public ConcretePass<GenerateCodeForTarget> { |
| 57 | TargetMachine &Target; |
| 58 | public: |
| 59 | inline GenerateCodeForTarget(TargetMachine &T) : Target(T) {} |
| 60 | |
| 61 | // doPerMethodWork - This method does the actual work of generating code for |
| 62 | // the specified method. |
| 63 | // |
| 64 | bool doPerMethodWorkVirt(Method *M) { |
| 65 | if (!M->isExternal() && Target.compileMethod(M)) { |
| 66 | cerr << "Error compiling " << InputFilename << "!\n"; |
| 67 | return true; |
Vikram S. Adve | 2f084b2 | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 68 | } |
Chris Lattner | 97fd6c4 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 69 | |
| 70 | return false; |
| 71 | } |
| 72 | }; |
Vikram S. Adve | 2f084b2 | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 73 | |
| 74 | |
| 75 | //===---------------------------------------------------------------------===// |
Chris Lattner | 97fd6c4 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 76 | // EmitAssembly Pass |
Vikram S. Adve | 2f084b2 | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 77 | // |
Chris Lattner | 97fd6c4 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 78 | // Write assembly code to specified output stream |
Vikram S. Adve | 2f084b2 | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 79 | //===---------------------------------------------------------------------===// |
| 80 | |
Chris Lattner | 97fd6c4 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 81 | class EmitAssembly : public ConcretePass<EmitAssembly> { |
| 82 | const TargetMachine &Target; // Target to compile for |
| 83 | ostream *Out; // Stream to print on |
| 84 | bool DeleteStream; // Delete stream in dtor? |
| 85 | |
| 86 | Module *TheMod; |
| 87 | public: |
| 88 | inline EmitAssembly(const TargetMachine &T, ostream *O, bool D) |
| 89 | : Target(T), Out(O), DeleteStream(D) {} |
| 90 | |
| 91 | virtual bool doPassInitializationVirt(Module *M) { |
| 92 | TheMod = M; |
| 93 | return false; |
Chris Lattner | 46f1b61 | 2001-09-19 16:52:09 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Chris Lattner | 97fd6c4 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 96 | ~EmitAssembly() { |
| 97 | Target.emitAssembly(TheMod, *Out); |
Chris Lattner | 46f1b61 | 2001-09-19 16:52:09 +0000 | [diff] [blame] | 98 | |
Chris Lattner | 97fd6c4 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 99 | if (DeleteStream) delete Out; |
| 100 | } |
| 101 | }; |
Chris Lattner | 0a823a0 | 2001-09-14 03:37:52 +0000 | [diff] [blame] | 102 | |
Vikram S. Adve | 9d40935 | 2001-09-18 13:10:45 +0000 | [diff] [blame] | 103 | |
Vikram S. Adve | 2f084b2 | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 104 | //===---------------------------------------------------------------------===// |
| 105 | // Function main() |
| 106 | // |
| 107 | // Entry point for the llc compiler. |
| 108 | //===---------------------------------------------------------------------===// |
| 109 | |
| 110 | int |
| 111 | main(int argc, char **argv) |
| 112 | { |
| 113 | // Parse command line options... |
| 114 | cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n"); |
| 115 | |
| 116 | // Allocate a target... in the future this will be controllable on the |
| 117 | // command line. |
| 118 | auto_ptr<TargetMachine> target(allocateSparcTargetMachine()); |
Chris Lattner | 97fd6c4 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 119 | assert(target.get() && "Could not allocate target machine!"); |
| 120 | |
| 121 | TargetMachine &Target = *target.get(); |
Vikram S. Adve | 2f084b2 | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 122 | |
| 123 | // Load the module to be compiled... |
| 124 | auto_ptr<Module> M(ParseBytecodeFile(InputFilename)); |
| 125 | if (M.get() == 0) { |
| 126 | cerr << "bytecode didn't read correctly.\n"; |
| 127 | return 1; |
| 128 | } |
Chris Lattner | 97fd6c4 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 129 | |
| 130 | // Build up all of the passes that we want to do to the module... |
| 131 | vector<Pass*> Passes; |
| 132 | |
| 133 | // Replace malloc and free instructions with library calls |
| 134 | Passes.push_back(new LowerAllocations(Target.DataLayout)); |
| 135 | |
| 136 | // Hoist constants out of PHI nodes into predecessor BB's |
| 137 | Passes.push_back(new HoistPHIConstants()); |
| 138 | |
| 139 | if (TraceBBValues || TraceMethodValues) // If tracing enabled... |
| 140 | // Insert trace code in all methods in the module |
| 141 | Passes.push_back(new InsertTraceCode(TraceBBValues, |
| 142 | TraceBBValues || TraceMethodValues)); |
| 143 | |
| 144 | |
| 145 | if (DebugTrace) { // If Trace Debugging is enabled... |
| 146 | // Then write the module with tracing code out in assembly form |
| 147 | assert(InputFilename != "-" && "files on stdin not supported with tracing"); |
| 148 | string traceFileName = GetFileNameRoot(InputFilename) + ".trace.ll"; |
| 149 | |
| 150 | ostream *os = new ofstream(traceFileName.c_str(), |
| 151 | (Force ? 0 : ios::noreplace)|ios::out); |
| 152 | if (!os->good()) { |
| 153 | cerr << "Error opening " << traceFileName << "!\n"; |
| 154 | delete os; |
| 155 | return 1; |
| 156 | } |
| 157 | |
| 158 | Passes.push_back(new PrintModulePass("", os, true)); |
| 159 | } |
| 160 | |
| 161 | // If LLVM dumping after transformations is requested, add it to the pipeline |
| 162 | if (DumpAsm) |
| 163 | Passes.push_back(new PrintModulePass("Method after xformations: \n",&cerr)); |
| 164 | |
| 165 | // Generate Target code... |
| 166 | Passes.push_back(new GenerateCodeForTarget(Target)); |
| 167 | |
| 168 | if (!DoNotEmitAssembly) { // If asm output is enabled... |
| 169 | // Figure out where we are going to send the output... |
| 170 | ostream *Out = 0; |
| 171 | if (OutputFilename != "") { // Specified an output filename? |
| 172 | Out = new ofstream(OutputFilename.c_str(), |
| 173 | (Force ? 0 : ios::noreplace)|ios::out); |
| 174 | } else { |
| 175 | if (InputFilename == "-") { |
| 176 | OutputFilename = "-"; |
| 177 | Out = &cout; |
| 178 | } else { |
| 179 | string OutputFilename = GetFileNameRoot(InputFilename); |
| 180 | OutputFilename += ".s"; |
| 181 | Out = new ofstream(OutputFilename.c_str(), |
| 182 | (Force ? 0 : ios::noreplace)|ios::out); |
| 183 | if (!Out->good()) { |
| 184 | cerr << "Error opening " << OutputFilename << "!\n"; |
| 185 | delete Out; |
| 186 | return 1; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // Output assembly language to the .s file |
| 192 | Passes.push_back(new EmitAssembly(Target, Out, Out != &cout)); |
| 193 | } |
Vikram S. Adve | 2f084b2 | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 194 | |
Chris Lattner | 97fd6c4 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 195 | // Run our queue of passes all at once now, efficiently. |
| 196 | return Pass::runAllPassesAndFree(M.get(), Passes); |
Vikram S. Adve | 2f084b2 | 2001-10-14 23:29:28 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Chris Lattner | 97fd6c4 | 2001-10-15 17:30:47 +0000 | [diff] [blame] | 199 | |