blob: 32a06dd291e9e8da3ecb9bc6924f259e369ed29f [file] [log] [blame]
Chris Lattnerb79757c2001-10-04 01:40:53 +00001//===-- llc.cpp - Implement the LLVM Compiler -----------------------------===//
Chris Lattnere737c7a2001-09-07 22:20:50 +00002//
3// This is the llc compiler driver.
4//
Chris Lattnerb79757c2001-10-04 01:40:53 +00005//===----------------------------------------------------------------------===//
Vikram S. Advecb465fc2001-07-21 12:42:29 +00006
Vikram S. Advecb465fc2001-07-21 12:42:29 +00007#include "llvm/Bytecode/Reader.h"
Chris Lattnerb26bcc52001-09-14 05:34:53 +00008#include "llvm/Target/Sparc.h"
Vikram S. Adve805eb962001-09-18 13:10:45 +00009#include "llvm/Target/TargetMachine.h"
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000010#include "llvm/Transforms/Instrumentation/TraceValues.h"
Chris Lattnerd7db8632002-01-22 01:04:08 +000011#include "llvm/Transforms/ChangeAllocations.h"
Chris Lattner3524fc22001-10-15 17:30:47 +000012#include "llvm/Transforms/HoistPHIConstants.h"
Vikram S. Adved32e70a2002-03-24 03:19:54 +000013#include "llvm/Transforms/Scalar/DecomposeMultiDimRefs.h"
Chris Lattner2197d212001-10-18 20:32:07 +000014#include "llvm/Assembly/PrintModulePass.h"
15#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnerc2c9dd12001-10-18 20:06:31 +000016#include "llvm/Transforms/ConstantMerge.h"
Chris Lattner46ac43c2001-09-07 21:26:31 +000017#include "llvm/Module.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000018#include "llvm/Function.h"
Chris Lattnercd50d3f2002-01-31 00:46:45 +000019#include "llvm/PassManager.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000020#include "Support/CommandLine.h"
Chris Lattnerda784ee2001-09-18 17:04:18 +000021#include <memory>
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000022#include <string>
Chris Lattner78f7e1a2001-09-19 16:52:09 +000023#include <fstream>
Chris Lattner697954c2002-01-20 22:54:45 +000024using std::string;
Chris Lattner8f367bd2001-07-23 02:35:57 +000025
Chris Lattner4ddcd542002-02-26 21:36:53 +000026static cl::String InputFilename ("", "Input filename", cl::NoFlags, "-");
27static cl::String OutputFilename("o", "Output filename", cl::NoFlags, "");
28static cl::Flag Force ("f", "Overwrite output files");
29static cl::Flag DumpAsm ("d", "Print bytecode before native code generation", cl::Hidden);
30
31enum TraceLevel {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000032 TraceOff, TraceFunctions, TraceBasicBlocks
Chris Lattner4ddcd542002-02-26 21:36:53 +000033};
34
35static cl::Enum<enum TraceLevel> TraceValues("trace", cl::NoFlags,
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000036 "Trace values through functions or basic blocks",
Chris Lattner4ddcd542002-02-26 21:36:53 +000037 clEnumValN(TraceOff , "off", "Disable trace code"),
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000038 clEnumValN(TraceFunctions , "function", "Trace each function"),
Chris Lattner4ddcd542002-02-26 21:36:53 +000039 clEnumValN(TraceBasicBlocks, "basicblock", "Trace each basic block"), 0);
Chris Lattner8f367bd2001-07-23 02:35:57 +000040
Vikram S. Adve805eb962001-09-18 13:10:45 +000041
Chris Lattner3524fc22001-10-15 17:30:47 +000042// GetFileNameRoot - Helper function to get the basename of a filename...
43static inline string GetFileNameRoot(const string &InputFilename) {
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000044 string IFN = InputFilename;
45 string outputFilename;
46 int Len = IFN.length();
47 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
48 outputFilename = string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
49 } else {
Chris Lattner3524fc22001-10-15 17:30:47 +000050 outputFilename = IFN;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000051 }
52 return outputFilename;
53}
54
Vikram S. Adve805eb962001-09-18 13:10:45 +000055
56//===---------------------------------------------------------------------===//
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000057// Function main()
58//
59// Entry point for the llc compiler.
60//===---------------------------------------------------------------------===//
61
Chris Lattner25c12292001-10-15 17:41:24 +000062int main(int argc, char **argv) {
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000063 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
64
65 // Allocate a target... in the future this will be controllable on the
66 // command line.
Chris Lattner697954c2002-01-20 22:54:45 +000067 std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
Chris Lattner3524fc22001-10-15 17:30:47 +000068 assert(target.get() && "Could not allocate target machine!");
69
70 TargetMachine &Target = *target.get();
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000071
72 // Load the module to be compiled...
Chris Lattner697954c2002-01-20 22:54:45 +000073 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000074 if (M.get() == 0) {
75 cerr << "bytecode didn't read correctly.\n";
76 return 1;
77 }
Chris Lattner3524fc22001-10-15 17:30:47 +000078
79 // Build up all of the passes that we want to do to the module...
Chris Lattnerf4de63f2002-01-21 07:31:50 +000080 PassManager Passes;
Chris Lattner3524fc22001-10-15 17:30:47 +000081
Chris Lattner3524fc22001-10-15 17:30:47 +000082 // Hoist constants out of PHI nodes into predecessor BB's
Chris Lattner4ddcd542002-02-26 21:36:53 +000083 Passes.add(createHoistPHIConstantsPass());
Chris Lattner3524fc22001-10-15 17:30:47 +000084
Chris Lattner4ddcd542002-02-26 21:36:53 +000085 if (TraceValues != TraceOff) { // If tracing enabled...
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000086 // Insert trace code in all functions in the module
Chris Lattner4ddcd542002-02-26 21:36:53 +000087 if (TraceValues == TraceBasicBlocks)
88 Passes.add(createTraceValuesPassForBasicBlocks());
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000089 else if (TraceValues == TraceFunctions)
Chris Lattner4ddcd542002-02-26 21:36:53 +000090 Passes.add(createTraceValuesPassForMethod());
91 else
92 assert(0 && "Bad value for TraceValues!");
Chris Lattnerc2c9dd12001-10-18 20:06:31 +000093
94 // Eliminate duplication in constant pool
Chris Lattner4ddcd542002-02-26 21:36:53 +000095 Passes.add(createDynamicConstantMergePass());
Vikram S. Adved32e70a2002-03-24 03:19:54 +000096 }
97
98 // Decompose multi-dimensional refs into a sequence of 1D refs
99 Passes.add(createDecomposeMultiDimRefsPass());
100
101 // Write out the module with tracing code just before code generation
102 if (TraceValues != TraceOff) { // If tracing enabled...
Chris Lattnerc2c9dd12001-10-18 20:06:31 +0000103 assert(InputFilename != "-" &&
104 "files on stdin not supported with tracing");
105 string traceFileName = GetFileNameRoot(InputFilename) + ".trace.bc";
Chris Lattner697954c2002-01-20 22:54:45 +0000106
Chris Lattner888912d2002-01-22 21:07:24 +0000107 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000108 // If force is not specified, make sure not to overwrite a file!
109 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
110 << "Use -f command line argument to force output\n";
111 return 1;
112 }
Vikram S. Adved32e70a2002-03-24 03:19:54 +0000113
Chris Lattner697954c2002-01-20 22:54:45 +0000114 std::ostream *os = new std::ofstream(traceFileName.c_str());
Chris Lattnerc2c9dd12001-10-18 20:06:31 +0000115 if (!os->good()) {
116 cerr << "Error opening " << traceFileName
117 << "! SKIPPING OUTPUT OF TRACE CODE\n";
118 delete os;
119 return 1;
Chris Lattner3524fc22001-10-15 17:30:47 +0000120 }
Chris Lattnerc2c9dd12001-10-18 20:06:31 +0000121
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000122 Passes.add(new WriteBytecodePass(os, true));
Chris Lattnerc2c9dd12001-10-18 20:06:31 +0000123 }
Vikram S. Adve79a33492001-10-18 13:51:20 +0000124
Vikram S. Adve712ac2b2001-10-18 18:20:20 +0000125 // Replace malloc and free instructions with library calls.
126 // Do this after tracing until lli implements these lib calls.
127 // For now, it will emulate malloc and free internally.
Chris Lattner4ddcd542002-02-26 21:36:53 +0000128 Passes.add(createLowerAllocationsPass(Target.DataLayout));
Vikram S. Adve712ac2b2001-10-18 18:20:20 +0000129
Chris Lattner3524fc22001-10-15 17:30:47 +0000130 // If LLVM dumping after transformations is requested, add it to the pipeline
131 if (DumpAsm)
Chris Lattner02d6ef82002-04-08 22:05:01 +0000132 Passes.add(new PrintFunctionPass("Code after xformations: \n", &cerr));
Chris Lattner3524fc22001-10-15 17:30:47 +0000133
Chris Lattnere41576d2002-02-03 23:43:19 +0000134 // Figure out where we are going to send the output...
135 std::ostream *Out = 0;
136 if (OutputFilename != "") { // Specified an output filename?
137 if (!Force && std::ifstream(OutputFilename.c_str())) {
138 // If force is not specified, make sure not to overwrite a file!
139 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
140 << "Use -f command line argument to force output\n";
141 return 1;
142 }
143 Out = new std::ofstream(OutputFilename.c_str());
144 } else {
145 if (InputFilename == "-") {
146 OutputFilename = "-";
147 Out = &std::cout;
148 } else {
149 string OutputFilename = GetFileNameRoot(InputFilename);
150 OutputFilename += ".s";
Chris Lattner3524fc22001-10-15 17:30:47 +0000151
Chris Lattner888912d2002-01-22 21:07:24 +0000152 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000153 // If force is not specified, make sure not to overwrite a file!
154 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
155 << "Use -f command line argument to force output\n";
156 return 1;
157 }
Chris Lattnere41576d2002-02-03 23:43:19 +0000158
Chris Lattner697954c2002-01-20 22:54:45 +0000159 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattnere41576d2002-02-03 23:43:19 +0000160 if (!Out->good()) {
161 cerr << "Error opening " << OutputFilename << "!\n";
162 delete Out;
163 return 1;
Chris Lattner3524fc22001-10-15 17:30:47 +0000164 }
165 }
Chris Lattner3524fc22001-10-15 17:30:47 +0000166 }
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000167
Chris Lattnere41576d2002-02-03 23:43:19 +0000168 Target.addPassesToEmitAssembly(Passes, *Out);
169
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000170 // Run our queue of passes all at once now, efficiently.
171 Passes.run(M.get());
Chris Lattner05e5e072001-10-18 01:31:22 +0000172
Chris Lattnere41576d2002-02-03 23:43:19 +0000173 if (Out != &std::cout) delete Out;
174
Chris Lattnerd7477ee2001-10-18 20:33:21 +0000175 return 0;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000176}