blob: 30777b02df2cf967017de607845a822e8af1753f [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 Lattnere2472bb2001-07-23 17:46:59 +000014#include "llvm/Support/CommandLine.h"
Chris Lattnered226062001-09-07 21:26:31 +000015#include "llvm/Module.h"
16#include "llvm/Method.h"
Chris Lattner6c2c8702001-09-18 17:04:18 +000017#include <memory>
Vikram S. Adve2f084b22001-10-14 23:29:28 +000018#include <string>
Chris Lattner46f1b612001-09-19 16:52:09 +000019#include <fstream>
Chris Lattner0af24642001-07-23 02:35:57 +000020
Chris Lattnerab0cc402001-07-23 19:27:24 +000021cl::String InputFilename ("", "Input filename", cl::NoFlags, "-");
Chris Lattner0af24642001-07-23 02:35:57 +000022cl::String OutputFilename("o", "Output filename", cl::NoFlags, "");
Chris Lattnerd0c15402001-10-15 17:41:24 +000023cl::Flag Force ("f", "Overwrite output files");
24cl::Flag DumpAsm ("d", "Print bytecode before native code generation",
25 cl::Hidden);
26cl::Flag DoNotEmitAssembly("noasm", "Do not emit assembly code", cl::Hidden);
Vikram S. Adve2f084b22001-10-14 23:29:28 +000027cl::Flag TraceBBValues ("trace",
Chris Lattnerd0c15402001-10-15 17:41:24 +000028 "Trace values at basic block and method exits");
29cl::Flag TraceMethodValues("tracem", "Trace values only at method exits");
Chris Lattner97fd6c42001-10-15 17:30:47 +000030cl::Flag DebugTrace ("dumptrace",
31 "output trace code to a <fn>.trace.ll file",
Chris Lattnerd0c15402001-10-15 17:41:24 +000032 cl::Hidden);
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 Lattner97fd6c42001-10-15 17:30:47 +000055class GenerateCodeForTarget : public ConcretePass<GenerateCodeForTarget> {
56 TargetMachine &Target;
57public:
58 inline GenerateCodeForTarget(TargetMachine &T) : Target(T) {}
59
60 // doPerMethodWork - This method does the actual work of generating code for
61 // the specified method.
62 //
63 bool doPerMethodWorkVirt(Method *M) {
64 if (!M->isExternal() && Target.compileMethod(M)) {
65 cerr << "Error compiling " << InputFilename << "!\n";
66 return true;
Vikram S. Adve2f084b22001-10-14 23:29:28 +000067 }
Chris Lattner97fd6c42001-10-15 17:30:47 +000068
69 return false;
70 }
71};
Vikram S. Adve2f084b22001-10-14 23:29:28 +000072
73
74//===---------------------------------------------------------------------===//
Chris Lattner97fd6c42001-10-15 17:30:47 +000075// EmitAssembly Pass
Vikram S. Adve2f084b22001-10-14 23:29:28 +000076//
Chris Lattner97fd6c42001-10-15 17:30:47 +000077// Write assembly code to specified output stream
Vikram S. Adve2f084b22001-10-14 23:29:28 +000078//===---------------------------------------------------------------------===//
79
Chris Lattner97fd6c42001-10-15 17:30:47 +000080class EmitAssembly : public ConcretePass<EmitAssembly> {
81 const TargetMachine &Target; // Target to compile for
82 ostream *Out; // Stream to print on
83 bool DeleteStream; // Delete stream in dtor?
84
85 Module *TheMod;
86public:
87 inline EmitAssembly(const TargetMachine &T, ostream *O, bool D)
88 : Target(T), Out(O), DeleteStream(D) {}
89
90 virtual bool doPassInitializationVirt(Module *M) {
91 TheMod = M;
92 return false;
Chris Lattner46f1b612001-09-19 16:52:09 +000093 }
94
Chris Lattner97fd6c42001-10-15 17:30:47 +000095 ~EmitAssembly() {
Chris Lattnerd0c15402001-10-15 17:41:24 +000096 // TODO: This should be performed as a moduleCleanup function, but we don't
97 // have one yet!
Chris Lattner97fd6c42001-10-15 17:30:47 +000098 Target.emitAssembly(TheMod, *Out);
Chris Lattner46f1b612001-09-19 16:52:09 +000099
Chris Lattner97fd6c42001-10-15 17:30:47 +0000100 if (DeleteStream) delete Out;
101 }
102};
Chris Lattner0a823a02001-09-14 03:37:52 +0000103
Vikram S. Adve9d409352001-09-18 13:10:45 +0000104
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000105//===---------------------------------------------------------------------===//
106// Function main()
107//
108// Entry point for the llc compiler.
109//===---------------------------------------------------------------------===//
110
Chris Lattnerd0c15402001-10-15 17:41:24 +0000111int main(int argc, char **argv) {
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000112 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
113
114 // Allocate a target... in the future this will be controllable on the
115 // command line.
116 auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
Chris Lattner97fd6c42001-10-15 17:30:47 +0000117 assert(target.get() && "Could not allocate target machine!");
118
119 TargetMachine &Target = *target.get();
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000120
121 // Load the module to be compiled...
122 auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
123 if (M.get() == 0) {
124 cerr << "bytecode didn't read correctly.\n";
125 return 1;
126 }
Chris Lattner97fd6c42001-10-15 17:30:47 +0000127
128 // Build up all of the passes that we want to do to the module...
129 vector<Pass*> Passes;
130
131 // Replace malloc and free instructions with library calls
132 Passes.push_back(new LowerAllocations(Target.DataLayout));
133
134 // Hoist constants out of PHI nodes into predecessor BB's
135 Passes.push_back(new HoistPHIConstants());
136
137 if (TraceBBValues || TraceMethodValues) // If tracing enabled...
138 // Insert trace code in all methods in the module
139 Passes.push_back(new InsertTraceCode(TraceBBValues,
140 TraceBBValues || TraceMethodValues));
141
142
143 if (DebugTrace) { // If Trace Debugging is enabled...
144 // Then write the module with tracing code out in assembly form
145 assert(InputFilename != "-" && "files on stdin not supported with tracing");
146 string traceFileName = GetFileNameRoot(InputFilename) + ".trace.ll";
147
148 ostream *os = new ofstream(traceFileName.c_str(),
149 (Force ? 0 : ios::noreplace)|ios::out);
150 if (!os->good()) {
151 cerr << "Error opening " << traceFileName << "!\n";
152 delete os;
153 return 1;
154 }
155
156 Passes.push_back(new PrintModulePass("", os, true));
157 }
158
159 // If LLVM dumping after transformations is requested, add it to the pipeline
160 if (DumpAsm)
161 Passes.push_back(new PrintModulePass("Method after xformations: \n",&cerr));
162
163 // Generate Target code...
164 Passes.push_back(new GenerateCodeForTarget(Target));
165
166 if (!DoNotEmitAssembly) { // If asm output is enabled...
167 // Figure out where we are going to send the output...
168 ostream *Out = 0;
169 if (OutputFilename != "") { // Specified an output filename?
170 Out = new ofstream(OutputFilename.c_str(),
171 (Force ? 0 : ios::noreplace)|ios::out);
172 } else {
173 if (InputFilename == "-") {
174 OutputFilename = "-";
175 Out = &cout;
176 } else {
177 string OutputFilename = GetFileNameRoot(InputFilename);
178 OutputFilename += ".s";
179 Out = new ofstream(OutputFilename.c_str(),
180 (Force ? 0 : ios::noreplace)|ios::out);
181 if (!Out->good()) {
182 cerr << "Error opening " << OutputFilename << "!\n";
183 delete Out;
184 return 1;
185 }
186 }
187 }
188
189 // Output assembly language to the .s file
190 Passes.push_back(new EmitAssembly(Target, Out, Out != &cout));
191 }
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000192
Chris Lattner97fd6c42001-10-15 17:30:47 +0000193 // Run our queue of passes all at once now, efficiently.
194 return Pass::runAllPassesAndFree(M.get(), Passes);
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000195}
196
Chris Lattner97fd6c42001-10-15 17:30:47 +0000197