blob: da5f74f2aa75c6305634d4354f7558e14127d3e6 [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"
13#include "llvm/Transforms/PrintModulePass.h"
Chris Lattnerb9acf7e2001-10-18 20:06:31 +000014#include "llvm/Transforms/ConstantMerge.h"
Chris Lattnere2472bb2001-07-23 17:46:59 +000015#include "llvm/Support/CommandLine.h"
Chris Lattnered226062001-09-07 21:26:31 +000016#include "llvm/Module.h"
17#include "llvm/Method.h"
Chris Lattner6c2c8702001-09-18 17:04:18 +000018#include <memory>
Vikram S. Adve2f084b22001-10-14 23:29:28 +000019#include <string>
Chris Lattner46f1b612001-09-19 16:52:09 +000020#include <fstream>
Chris Lattner0af24642001-07-23 02:35:57 +000021
Chris Lattnerab0cc402001-07-23 19:27:24 +000022cl::String InputFilename ("", "Input filename", cl::NoFlags, "-");
Chris Lattner0af24642001-07-23 02:35:57 +000023cl::String OutputFilename("o", "Output filename", cl::NoFlags, "");
Chris Lattnerd0c15402001-10-15 17:41:24 +000024cl::Flag Force ("f", "Overwrite output files");
25cl::Flag DumpAsm ("d", "Print bytecode before native code generation",
26 cl::Hidden);
27cl::Flag DoNotEmitAssembly("noasm", "Do not emit assembly code", cl::Hidden);
Vikram S. Adve2f084b22001-10-14 23:29:28 +000028cl::Flag TraceBBValues ("trace",
Chris Lattnerd0c15402001-10-15 17:41:24 +000029 "Trace values at basic block and method exits");
30cl::Flag TraceMethodValues("tracem", "Trace values only at method exits");
Chris Lattner0af24642001-07-23 02:35:57 +000031
Vikram S. Adve9d409352001-09-18 13:10:45 +000032
Chris Lattner97fd6c42001-10-15 17:30:47 +000033// GetFileNameRoot - Helper function to get the basename of a filename...
34static inline string GetFileNameRoot(const string &InputFilename) {
Vikram S. Adve2f084b22001-10-14 23:29:28 +000035 string IFN = InputFilename;
36 string outputFilename;
37 int Len = IFN.length();
38 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
39 outputFilename = string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
40 } else {
Chris Lattner97fd6c42001-10-15 17:30:47 +000041 outputFilename = IFN;
Vikram S. Adve2f084b22001-10-14 23:29:28 +000042 }
43 return outputFilename;
44}
45
Vikram S. Adve9d409352001-09-18 13:10:45 +000046
47//===---------------------------------------------------------------------===//
Chris Lattner97fd6c42001-10-15 17:30:47 +000048// GenerateCodeForTarget Pass
Vikram S. Adve2f084b22001-10-14 23:29:28 +000049//
50// Native code generation for a specified target.
51//===---------------------------------------------------------------------===//
52
Chris Lattnerd054fae2001-10-18 05:28:44 +000053class GenerateCodeForTarget : public Pass {
Chris Lattner97fd6c42001-10-15 17:30:47 +000054 TargetMachine &Target;
55public:
56 inline GenerateCodeForTarget(TargetMachine &T) : Target(T) {}
57
58 // doPerMethodWork - This method does the actual work of generating code for
59 // the specified method.
60 //
Chris Lattnerd054fae2001-10-18 05:28:44 +000061 bool doPerMethodWork(Method *M) {
Chris Lattner97fd6c42001-10-15 17:30:47 +000062 if (!M->isExternal() && Target.compileMethod(M)) {
63 cerr << "Error compiling " << InputFilename << "!\n";
64 return true;
Vikram S. Adve2f084b22001-10-14 23:29:28 +000065 }
Chris Lattner97fd6c42001-10-15 17:30:47 +000066
67 return false;
68 }
69};
Vikram S. Adve2f084b22001-10-14 23:29:28 +000070
71
72//===---------------------------------------------------------------------===//
Chris Lattner97fd6c42001-10-15 17:30:47 +000073// EmitAssembly Pass
Vikram S. Adve2f084b22001-10-14 23:29:28 +000074//
Chris Lattner97fd6c42001-10-15 17:30:47 +000075// Write assembly code to specified output stream
Vikram S. Adve2f084b22001-10-14 23:29:28 +000076//===---------------------------------------------------------------------===//
77
Chris Lattnerd054fae2001-10-18 05:28:44 +000078class EmitAssembly : public Pass {
Chris Lattner97fd6c42001-10-15 17:30:47 +000079 const TargetMachine &Target; // Target to compile for
80 ostream *Out; // Stream to print on
81 bool DeleteStream; // Delete stream in dtor?
Chris Lattner97fd6c42001-10-15 17:30:47 +000082public:
83 inline EmitAssembly(const TargetMachine &T, ostream *O, bool D)
84 : Target(T), Out(O), DeleteStream(D) {}
85
Chris Lattner46f1b612001-09-19 16:52:09 +000086
Chris Lattnerd054fae2001-10-18 05:28:44 +000087 virtual bool doPassFinalization(Module *M) {
Chris Lattnerd054fae2001-10-18 05:28:44 +000088 Target.emitAssembly(M, *Out);
Chris Lattner46f1b612001-09-19 16:52:09 +000089
Chris Lattner97fd6c42001-10-15 17:30:47 +000090 if (DeleteStream) delete Out;
Chris Lattnerd054fae2001-10-18 05:28:44 +000091 return false;
Chris Lattner97fd6c42001-10-15 17:30:47 +000092 }
93};
Chris Lattner0a823a02001-09-14 03:37:52 +000094
Vikram S. Adve9d409352001-09-18 13:10:45 +000095
Vikram S. Adve2f084b22001-10-14 23:29:28 +000096//===---------------------------------------------------------------------===//
97// Function main()
98//
99// Entry point for the llc compiler.
100//===---------------------------------------------------------------------===//
101
Chris Lattnerd0c15402001-10-15 17:41:24 +0000102int main(int argc, char **argv) {
Vikram S. Adveb2ac1e72001-10-18 13:51:20 +0000103 int retCode = 0;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000104 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
105
106 // Allocate a target... in the future this will be controllable on the
107 // command line.
108 auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
Chris Lattner97fd6c42001-10-15 17:30:47 +0000109 assert(target.get() && "Could not allocate target machine!");
110
111 TargetMachine &Target = *target.get();
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000112
113 // Load the module to be compiled...
114 auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
115 if (M.get() == 0) {
116 cerr << "bytecode didn't read correctly.\n";
117 return 1;
118 }
Chris Lattner97fd6c42001-10-15 17:30:47 +0000119
120 // Build up all of the passes that we want to do to the module...
121 vector<Pass*> Passes;
122
Chris Lattner97fd6c42001-10-15 17:30:47 +0000123 // Hoist constants out of PHI nodes into predecessor BB's
124 Passes.push_back(new HoistPHIConstants());
125
Chris Lattnerb9acf7e2001-10-18 20:06:31 +0000126 if (TraceBBValues || TraceMethodValues) { // If tracing enabled...
127 // Insert trace code in all methods in the module
128 Passes.push_back(new InsertTraceCode(TraceBBValues,
129 TraceBBValues ||TraceMethodValues));
130
131 // Eliminate duplication in constant pool
132 Passes.push_back(new DynamicConstantMerge());
Vikram S. Adveb2ac1e72001-10-18 13:51:20 +0000133
Chris Lattnerb9acf7e2001-10-18 20:06:31 +0000134 // Then write out the module with tracing code before code generation
135 assert(InputFilename != "-" &&
136 "files on stdin not supported with tracing");
137 string traceFileName = GetFileNameRoot(InputFilename) + ".trace.bc";
138 ostream *os = new ofstream(traceFileName.c_str(),
139 (Force ? 0 : ios::noreplace)|ios::out);
140 if (!os->good()) {
141 cerr << "Error opening " << traceFileName
142 << "! SKIPPING OUTPUT OF TRACE CODE\n";
143 delete os;
144 return 1;
Chris Lattner97fd6c42001-10-15 17:30:47 +0000145 }
Chris Lattnerb9acf7e2001-10-18 20:06:31 +0000146
147 Passes.push_back(new WriteModuleBytecode(os, true));
148 }
Vikram S. Adveb2ac1e72001-10-18 13:51:20 +0000149
Vikram S. Adve71f16ec2001-10-18 18:20:20 +0000150 // Replace malloc and free instructions with library calls.
151 // Do this after tracing until lli implements these lib calls.
152 // For now, it will emulate malloc and free internally.
153 Passes.push_back(new LowerAllocations(Target.DataLayout));
154
Chris Lattner97fd6c42001-10-15 17:30:47 +0000155 // If LLVM dumping after transformations is requested, add it to the pipeline
156 if (DumpAsm)
Vikram S. Adveb2ac1e72001-10-18 13:51:20 +0000157 Passes.push_back(new PrintModulePass("Code after xformations: \n",&cerr));
Chris Lattner97fd6c42001-10-15 17:30:47 +0000158
159 // Generate Target code...
160 Passes.push_back(new GenerateCodeForTarget(Target));
161
162 if (!DoNotEmitAssembly) { // If asm output is enabled...
163 // Figure out where we are going to send the output...
164 ostream *Out = 0;
165 if (OutputFilename != "") { // Specified an output filename?
166 Out = new ofstream(OutputFilename.c_str(),
167 (Force ? 0 : ios::noreplace)|ios::out);
168 } else {
169 if (InputFilename == "-") {
170 OutputFilename = "-";
171 Out = &cout;
172 } else {
173 string OutputFilename = GetFileNameRoot(InputFilename);
174 OutputFilename += ".s";
175 Out = new ofstream(OutputFilename.c_str(),
176 (Force ? 0 : ios::noreplace)|ios::out);
177 if (!Out->good()) {
178 cerr << "Error opening " << OutputFilename << "!\n";
179 delete Out;
180 return 1;
181 }
182 }
183 }
184
185 // Output assembly language to the .s file
186 Passes.push_back(new EmitAssembly(Target, Out, Out != &cout));
187 }
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000188
Chris Lattner2fa0dab2001-10-18 01:31:22 +0000189 // Run our queue of passes all at once now, efficiently. This form of
190 // runAllPasses frees the Pass objects after runAllPasses completes.
191 Pass::runAllPassesAndFree(M.get(), Passes);
192
Vikram S. Adveb2ac1e72001-10-18 13:51:20 +0000193 return retCode;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000194}
195
Chris Lattner97fd6c42001-10-15 17:30:47 +0000196