blob: 1d63b28b3a273c9a150079e296b2b810042094d1 [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 Lattner3787ee62002-01-22 01:04:08 +000011#include "llvm/Transforms/ChangeAllocations.h"
Chris Lattner97fd6c42001-10-15 17:30:47 +000012#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 Lattnered226062001-09-07 21:26:31 +000016#include "llvm/Module.h"
17#include "llvm/Method.h"
Chris Lattner5de22042001-11-27 00:03:19 +000018#include "Support/CommandLine.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 Lattner7f74a562002-01-20 22:54:45 +000022using std::string;
Chris Lattner0af24642001-07-23 02:35:57 +000023
Chris Lattnerab0cc402001-07-23 19:27:24 +000024cl::String InputFilename ("", "Input filename", cl::NoFlags, "-");
Chris Lattner0af24642001-07-23 02:35:57 +000025cl::String OutputFilename("o", "Output filename", cl::NoFlags, "");
Chris Lattnerd0c15402001-10-15 17:41:24 +000026cl::Flag Force ("f", "Overwrite output files");
27cl::Flag DumpAsm ("d", "Print bytecode before native code generation",
28 cl::Hidden);
29cl::Flag DoNotEmitAssembly("noasm", "Do not emit assembly code", cl::Hidden);
Vikram S. Adve2f084b22001-10-14 23:29:28 +000030cl::Flag TraceBBValues ("trace",
Chris Lattnerd0c15402001-10-15 17:41:24 +000031 "Trace values at basic block and method exits");
32cl::Flag TraceMethodValues("tracem", "Trace values only at method exits");
Chris Lattner0af24642001-07-23 02:35:57 +000033
Vikram S. Adve9d409352001-09-18 13:10:45 +000034
Chris Lattner97fd6c42001-10-15 17:30:47 +000035// GetFileNameRoot - Helper function to get the basename of a filename...
36static inline string GetFileNameRoot(const string &InputFilename) {
Vikram S. Adve2f084b22001-10-14 23:29:28 +000037 string IFN = InputFilename;
38 string outputFilename;
39 int Len = IFN.length();
40 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
41 outputFilename = string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
42 } else {
Chris Lattner97fd6c42001-10-15 17:30:47 +000043 outputFilename = IFN;
Vikram S. Adve2f084b22001-10-14 23:29:28 +000044 }
45 return outputFilename;
46}
47
Vikram S. Adve9d409352001-09-18 13:10:45 +000048
49//===---------------------------------------------------------------------===//
Chris Lattner97fd6c42001-10-15 17:30:47 +000050// GenerateCodeForTarget Pass
Vikram S. Adve2f084b22001-10-14 23:29:28 +000051//
52// Native code generation for a specified target.
53//===---------------------------------------------------------------------===//
54
Chris Lattner0686e432002-01-21 07:31:50 +000055class GenerateCodeForTarget : public MethodPass {
Chris Lattner97fd6c42001-10-15 17:30:47 +000056 TargetMachine &Target;
57public:
58 inline GenerateCodeForTarget(TargetMachine &T) : Target(T) {}
59
Chris Lattner0686e432002-01-21 07:31:50 +000060 // runOnMethod - This method does the actual work of generating code for
Chris Lattner97fd6c42001-10-15 17:30:47 +000061 // the specified method.
62 //
Chris Lattner0686e432002-01-21 07:31:50 +000063 bool runOnMethod(Method *M) {
Chris Lattner97fd6c42001-10-15 17:30:47 +000064 if (!M->isExternal() && Target.compileMethod(M)) {
65 cerr << "Error compiling " << InputFilename << "!\n";
Vikram S. Adve2f084b22001-10-14 23:29:28 +000066 }
Chris Lattner97fd6c42001-10-15 17:30:47 +000067
Chris Lattner0686e432002-01-21 07:31:50 +000068 return true;
Chris Lattner97fd6c42001-10-15 17:30:47 +000069 }
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
Chris Lattner7f74a562002-01-20 22:54:45 +000081 std::ostream *Out; // Stream to print on
Chris Lattner97fd6c42001-10-15 17:30:47 +000082 bool DeleteStream; // Delete stream in dtor?
Chris Lattner97fd6c42001-10-15 17:30:47 +000083public:
Chris Lattner7f74a562002-01-20 22:54:45 +000084 inline EmitAssembly(const TargetMachine &T, std::ostream *O, bool D)
Chris Lattner97fd6c42001-10-15 17:30:47 +000085 : Target(T), Out(O), DeleteStream(D) {}
86
Chris Lattner0686e432002-01-21 07:31:50 +000087 virtual bool run(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
Chris Lattner0686e432002-01-21 07:31:50 +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. 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.
Chris Lattner7f74a562002-01-20 22:54:45 +0000108 std::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...
Chris Lattner7f74a562002-01-20 22:54:45 +0000114 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000115 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...
Chris Lattner0686e432002-01-21 07:31:50 +0000121 PassManager Passes;
Chris Lattner97fd6c42001-10-15 17:30:47 +0000122
Chris Lattner97fd6c42001-10-15 17:30:47 +0000123 // Hoist constants out of PHI nodes into predecessor BB's
Chris Lattner0686e432002-01-21 07:31:50 +0000124 Passes.add(new HoistPHIConstants());
Chris Lattner97fd6c42001-10-15 17:30:47 +0000125
Chris Lattnerb9acf7e2001-10-18 20:06:31 +0000126 if (TraceBBValues || TraceMethodValues) { // If tracing enabled...
127 // Insert trace code in all methods in the module
Chris Lattner0686e432002-01-21 07:31:50 +0000128 Passes.add(new InsertTraceCode(TraceBBValues,
129 TraceBBValues ||TraceMethodValues));
Chris Lattnerb9acf7e2001-10-18 20:06:31 +0000130
131 // Eliminate duplication in constant pool
Chris Lattner0686e432002-01-21 07:31:50 +0000132 Passes.add(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";
Chris Lattner7f74a562002-01-20 22:54:45 +0000138
139 if (!Force && !std::ifstream(OutputFilename.c_str())) {
140 // If force is not specified, make sure not to overwrite a file!
141 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
142 << "Use -f command line argument to force output\n";
143 return 1;
144 }
145
146 std::ostream *os = new std::ofstream(traceFileName.c_str());
Chris Lattnerb9acf7e2001-10-18 20:06:31 +0000147 if (!os->good()) {
148 cerr << "Error opening " << traceFileName
149 << "! SKIPPING OUTPUT OF TRACE CODE\n";
150 delete os;
151 return 1;
Chris Lattner97fd6c42001-10-15 17:30:47 +0000152 }
Chris Lattnerb9acf7e2001-10-18 20:06:31 +0000153
Chris Lattner0686e432002-01-21 07:31:50 +0000154 Passes.add(new WriteBytecodePass(os, true));
Chris Lattnerb9acf7e2001-10-18 20:06:31 +0000155 }
Vikram S. Adveb2ac1e72001-10-18 13:51:20 +0000156
Vikram S. Adve71f16ec2001-10-18 18:20:20 +0000157 // Replace malloc and free instructions with library calls.
158 // Do this after tracing until lli implements these lib calls.
159 // For now, it will emulate malloc and free internally.
Chris Lattner0686e432002-01-21 07:31:50 +0000160 Passes.add(new LowerAllocations(Target.DataLayout));
Vikram S. Adve71f16ec2001-10-18 18:20:20 +0000161
Chris Lattner97fd6c42001-10-15 17:30:47 +0000162 // If LLVM dumping after transformations is requested, add it to the pipeline
163 if (DumpAsm)
Chris Lattner0686e432002-01-21 07:31:50 +0000164 Passes.add(new PrintMethodPass("Code after xformations: \n",&cerr));
Chris Lattner97fd6c42001-10-15 17:30:47 +0000165
166 // Generate Target code...
Chris Lattner0686e432002-01-21 07:31:50 +0000167 Passes.add(new GenerateCodeForTarget(Target));
Chris Lattner97fd6c42001-10-15 17:30:47 +0000168
169 if (!DoNotEmitAssembly) { // If asm output is enabled...
170 // Figure out where we are going to send the output...
Chris Lattner7f74a562002-01-20 22:54:45 +0000171 std::ostream *Out = 0;
Chris Lattner97fd6c42001-10-15 17:30:47 +0000172 if (OutputFilename != "") { // Specified an output filename?
Chris Lattner7f74a562002-01-20 22:54:45 +0000173 if (!Force && !std::ifstream(OutputFilename.c_str())) {
174 // If force is not specified, make sure not to overwrite a file!
175 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
176 << "Use -f command line argument to force output\n";
177 return 1;
178 }
179 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner97fd6c42001-10-15 17:30:47 +0000180 } else {
181 if (InputFilename == "-") {
182 OutputFilename = "-";
Chris Lattner7f74a562002-01-20 22:54:45 +0000183 Out = &std::cout;
Chris Lattner97fd6c42001-10-15 17:30:47 +0000184 } else {
185 string OutputFilename = GetFileNameRoot(InputFilename);
186 OutputFilename += ".s";
Chris Lattner7f74a562002-01-20 22:54:45 +0000187
188 if (!Force && !std::ifstream(OutputFilename.c_str())) {
189 // If force is not specified, make sure not to overwrite a file!
190 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
191 << "Use -f command line argument to force output\n";
192 return 1;
193 }
194
195 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner97fd6c42001-10-15 17:30:47 +0000196 if (!Out->good()) {
197 cerr << "Error opening " << OutputFilename << "!\n";
198 delete Out;
199 return 1;
200 }
201 }
202 }
203
204 // Output assembly language to the .s file
Chris Lattner0686e432002-01-21 07:31:50 +0000205 Passes.add(new EmitAssembly(Target, Out, Out != &std::cout));
Chris Lattner97fd6c42001-10-15 17:30:47 +0000206 }
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000207
Chris Lattner0686e432002-01-21 07:31:50 +0000208 // Run our queue of passes all at once now, efficiently.
209 Passes.run(M.get());
Chris Lattner2fa0dab2001-10-18 01:31:22 +0000210
Chris Lattner1a9680f2001-10-18 20:33:21 +0000211 return 0;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000212}
213
Chris Lattner97fd6c42001-10-15 17:30:47 +0000214