blob: ff492aa59116835a86543e6870e5ac8ec9f58748 [file] [log] [blame]
Chris Lattnerb27d4742001-10-04 01:40:53 +00001//===-- llc.cpp - Implement the LLVM Compiler -----------------------------===//
Chris Lattner2cf137b2001-09-07 22:20:50 +00002//
3// This is the llc compiler driver.
4//
Chris Lattnerb27d4742001-10-04 01:40:53 +00005//===----------------------------------------------------------------------===//
Vikram S. Adve2d94a342001-07-21 12:42:29 +00006
Vikram S. Adve2d94a342001-07-21 12:42:29 +00007#include "llvm/Bytecode/Reader.h"
Chris Lattner22a6a902001-09-14 05:34:53 +00008#include "llvm/Target/Sparc.h"
Vikram S. Adve9d409352001-09-18 13:10:45 +00009#include "llvm/Target/TargetMachine.h"
Vikram S. Adve2f084b22001-10-14 23:29:28 +000010#include "llvm/Transforms/Instrumentation/TraceValues.h"
Chris Lattner97fd6c42001-10-15 17:30:47 +000011#include "llvm/Transforms/LowerAllocations.h"
12#include "llvm/Transforms/HoistPHIConstants.h"
Chris Lattner95f87b42001-10-18 20:32:07 +000013#include "llvm/Assembly/PrintModulePass.h"
14#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnerb9acf7e2001-10-18 20:06:31 +000015#include "llvm/Transforms/ConstantMerge.h"
Chris Lattnere2472bb2001-07-23 17:46:59 +000016#include "llvm/Support/CommandLine.h"
Chris Lattnered226062001-09-07 21:26:31 +000017#include "llvm/Module.h"
18#include "llvm/Method.h"
Chris Lattner6c2c8702001-09-18 17:04:18 +000019#include <memory>
Vikram S. Adve2f084b22001-10-14 23:29:28 +000020#include <string>
Chris Lattner46f1b612001-09-19 16:52:09 +000021#include <fstream>
Chris Lattner0af24642001-07-23 02:35:57 +000022
Chris Lattnerab0cc402001-07-23 19:27:24 +000023cl::String InputFilename ("", "Input filename", cl::NoFlags, "-");
Chris Lattner0af24642001-07-23 02:35:57 +000024cl::String OutputFilename("o", "Output filename", cl::NoFlags, "");
Chris Lattnerd0c15402001-10-15 17:41:24 +000025cl::Flag Force ("f", "Overwrite output files");
26cl::Flag DumpAsm ("d", "Print bytecode before native code generation",
27 cl::Hidden);
28cl::Flag DoNotEmitAssembly("noasm", "Do not emit assembly code", cl::Hidden);
Vikram S. Adve2f084b22001-10-14 23:29:28 +000029cl::Flag TraceBBValues ("trace",
Chris Lattnerd0c15402001-10-15 17:41:24 +000030 "Trace values at basic block and method exits");
31cl::Flag TraceMethodValues("tracem", "Trace values only at method exits");
Chris Lattner0af24642001-07-23 02:35:57 +000032
Vikram S. Adve9d409352001-09-18 13:10:45 +000033
Chris Lattner97fd6c42001-10-15 17:30:47 +000034// GetFileNameRoot - Helper function to get the basename of a filename...
35static inline string GetFileNameRoot(const string &InputFilename) {
Vikram S. Adve2f084b22001-10-14 23:29:28 +000036 string IFN = InputFilename;
37 string outputFilename;
38 int Len = IFN.length();
39 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
40 outputFilename = string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
41 } else {
Chris Lattner97fd6c42001-10-15 17:30:47 +000042 outputFilename = IFN;
Vikram S. Adve2f084b22001-10-14 23:29:28 +000043 }
44 return outputFilename;
45}
46
Vikram S. Adve9d409352001-09-18 13:10:45 +000047
48//===---------------------------------------------------------------------===//
Chris Lattner97fd6c42001-10-15 17:30:47 +000049// GenerateCodeForTarget Pass
Vikram S. Adve2f084b22001-10-14 23:29:28 +000050//
51// Native code generation for a specified target.
52//===---------------------------------------------------------------------===//
53
Chris Lattnerd054fae2001-10-18 05:28:44 +000054class GenerateCodeForTarget : public Pass {
Chris Lattner97fd6c42001-10-15 17:30:47 +000055 TargetMachine &Target;
56public:
57 inline GenerateCodeForTarget(TargetMachine &T) : Target(T) {}
58
59 // doPerMethodWork - This method does the actual work of generating code for
60 // the specified method.
61 //
Chris Lattnerd054fae2001-10-18 05:28:44 +000062 bool doPerMethodWork(Method *M) {
Chris Lattner97fd6c42001-10-15 17:30:47 +000063 if (!M->isExternal() && Target.compileMethod(M)) {
64 cerr << "Error compiling " << InputFilename << "!\n";
65 return true;
Vikram S. Adve2f084b22001-10-14 23:29:28 +000066 }
Chris Lattner97fd6c42001-10-15 17:30:47 +000067
68 return false;
69 }
70};
Vikram S. Adve2f084b22001-10-14 23:29:28 +000071
72
73//===---------------------------------------------------------------------===//
Chris Lattner97fd6c42001-10-15 17:30:47 +000074// EmitAssembly Pass
Vikram S. Adve2f084b22001-10-14 23:29:28 +000075//
Chris Lattner97fd6c42001-10-15 17:30:47 +000076// Write assembly code to specified output stream
Vikram S. Adve2f084b22001-10-14 23:29:28 +000077//===---------------------------------------------------------------------===//
78
Chris Lattnerd054fae2001-10-18 05:28:44 +000079class EmitAssembly : public Pass {
Chris Lattner97fd6c42001-10-15 17:30:47 +000080 const TargetMachine &Target; // Target to compile for
81 ostream *Out; // Stream to print on
82 bool DeleteStream; // Delete stream in dtor?
Chris Lattner97fd6c42001-10-15 17:30:47 +000083public:
84 inline EmitAssembly(const TargetMachine &T, ostream *O, bool D)
85 : Target(T), Out(O), DeleteStream(D) {}
86
Chris Lattner46f1b612001-09-19 16:52:09 +000087
Chris Lattnerd054fae2001-10-18 05:28:44 +000088 virtual bool doPassFinalization(Module *M) {
Chris Lattnerd054fae2001-10-18 05:28:44 +000089 Target.emitAssembly(M, *Out);
Chris Lattner46f1b612001-09-19 16:52:09 +000090
Chris Lattner97fd6c42001-10-15 17:30:47 +000091 if (DeleteStream) delete Out;
Chris Lattnerd054fae2001-10-18 05:28:44 +000092 return false;
Chris Lattner97fd6c42001-10-15 17:30:47 +000093 }
94};
Chris Lattner0a823a02001-09-14 03:37:52 +000095
Vikram S. Adve9d409352001-09-18 13:10:45 +000096
Vikram S. Adve2f084b22001-10-14 23:29:28 +000097//===---------------------------------------------------------------------===//
98// Function main()
99//
100// Entry point for the llc compiler.
101//===---------------------------------------------------------------------===//
102
Chris Lattnerd0c15402001-10-15 17:41:24 +0000103int main(int argc, char **argv) {
Vikram S. Adveb2ac1e72001-10-18 13:51:20 +0000104 int retCode = 0;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000105 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
106
107 // Allocate a target... in the future this will be controllable on the
108 // command line.
109 auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
Chris Lattner97fd6c42001-10-15 17:30:47 +0000110 assert(target.get() && "Could not allocate target machine!");
111
112 TargetMachine &Target = *target.get();
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000113
114 // Load the module to be compiled...
115 auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
116 if (M.get() == 0) {
117 cerr << "bytecode didn't read correctly.\n";
118 return 1;
119 }
Chris Lattner97fd6c42001-10-15 17:30:47 +0000120
121 // Build up all of the passes that we want to do to the module...
122 vector<Pass*> Passes;
123
Chris Lattner97fd6c42001-10-15 17:30:47 +0000124 // Hoist constants out of PHI nodes into predecessor BB's
125 Passes.push_back(new HoistPHIConstants());
126
Chris Lattnerb9acf7e2001-10-18 20:06:31 +0000127 if (TraceBBValues || TraceMethodValues) { // If tracing enabled...
128 // Insert trace code in all methods in the module
129 Passes.push_back(new InsertTraceCode(TraceBBValues,
130 TraceBBValues ||TraceMethodValues));
131
132 // Eliminate duplication in constant pool
133 Passes.push_back(new DynamicConstantMerge());
Vikram S. Adveb2ac1e72001-10-18 13:51:20 +0000134
Chris Lattnerb9acf7e2001-10-18 20:06:31 +0000135 // Then write out the module with tracing code before code generation
136 assert(InputFilename != "-" &&
137 "files on stdin not supported with tracing");
138 string traceFileName = GetFileNameRoot(InputFilename) + ".trace.bc";
139 ostream *os = new ofstream(traceFileName.c_str(),
140 (Force ? 0 : ios::noreplace)|ios::out);
141 if (!os->good()) {
142 cerr << "Error opening " << traceFileName
143 << "! SKIPPING OUTPUT OF TRACE CODE\n";
144 delete os;
145 return 1;
Chris Lattner97fd6c42001-10-15 17:30:47 +0000146 }
Chris Lattnerb9acf7e2001-10-18 20:06:31 +0000147
Chris Lattner95f87b42001-10-18 20:32:07 +0000148 Passes.push_back(new WriteBytecodePass(os, true));
Chris Lattnerb9acf7e2001-10-18 20:06:31 +0000149 }
Vikram S. Adveb2ac1e72001-10-18 13:51:20 +0000150
Vikram S. Adve71f16ec2001-10-18 18:20:20 +0000151 // Replace malloc and free instructions with library calls.
152 // Do this after tracing until lli implements these lib calls.
153 // For now, it will emulate malloc and free internally.
154 Passes.push_back(new LowerAllocations(Target.DataLayout));
155
Chris Lattner97fd6c42001-10-15 17:30:47 +0000156 // If LLVM dumping after transformations is requested, add it to the pipeline
157 if (DumpAsm)
Vikram S. Adveb2ac1e72001-10-18 13:51:20 +0000158 Passes.push_back(new PrintModulePass("Code after xformations: \n",&cerr));
Chris Lattner97fd6c42001-10-15 17:30:47 +0000159
160 // Generate Target code...
161 Passes.push_back(new GenerateCodeForTarget(Target));
162
163 if (!DoNotEmitAssembly) { // If asm output is enabled...
164 // Figure out where we are going to send the output...
165 ostream *Out = 0;
166 if (OutputFilename != "") { // Specified an output filename?
167 Out = new ofstream(OutputFilename.c_str(),
168 (Force ? 0 : ios::noreplace)|ios::out);
169 } else {
170 if (InputFilename == "-") {
171 OutputFilename = "-";
172 Out = &cout;
173 } else {
174 string OutputFilename = GetFileNameRoot(InputFilename);
175 OutputFilename += ".s";
176 Out = new ofstream(OutputFilename.c_str(),
177 (Force ? 0 : ios::noreplace)|ios::out);
178 if (!Out->good()) {
179 cerr << "Error opening " << OutputFilename << "!\n";
180 delete Out;
181 return 1;
182 }
183 }
184 }
185
186 // Output assembly language to the .s file
187 Passes.push_back(new EmitAssembly(Target, Out, Out != &cout));
188 }
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000189
Chris Lattner2fa0dab2001-10-18 01:31:22 +0000190 // Run our queue of passes all at once now, efficiently. This form of
191 // runAllPasses frees the Pass objects after runAllPasses completes.
192 Pass::runAllPassesAndFree(M.get(), Passes);
193
Vikram S. Adveb2ac1e72001-10-18 13:51:20 +0000194 return retCode;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000195}
196
Chris Lattner97fd6c42001-10-15 17:30:47 +0000197