blob: 1ef8144f2a6f905d59e0044d82e5254d7dcee068 [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 Lattner65f1b892002-05-07 20:03:27 +000012#include "llvm/Transforms/Scalar.h"
Chris Lattner39fd6592002-05-20 21:20:08 +000013#include "llvm/Transforms/Utils/Linker.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 Lattner76d12292002-04-18 19:55:25 +000021#include "Support/Signals.h"
Chris Lattnerda784ee2001-09-18 17:04:18 +000022#include <memory>
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);
Chris Lattner39fd6592002-05-20 21:20:08 +000030static cl::String TraceLibPath ("tracelibpath", "Path to libinstr for trace code", cl::Hidden, "");
Chris Lattner4ddcd542002-02-26 21:36:53 +000031
32enum TraceLevel {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000033 TraceOff, TraceFunctions, TraceBasicBlocks
Chris Lattner4ddcd542002-02-26 21:36:53 +000034};
35
36static cl::Enum<enum TraceLevel> TraceValues("trace", cl::NoFlags,
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000037 "Trace values through functions or basic blocks",
Chris Lattner4ddcd542002-02-26 21:36:53 +000038 clEnumValN(TraceOff , "off", "Disable trace code"),
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000039 clEnumValN(TraceFunctions , "function", "Trace each function"),
Chris Lattner4ddcd542002-02-26 21:36:53 +000040 clEnumValN(TraceBasicBlocks, "basicblock", "Trace each basic block"), 0);
Chris Lattner8f367bd2001-07-23 02:35:57 +000041
Vikram S. Adve805eb962001-09-18 13:10:45 +000042
Chris Lattner3524fc22001-10-15 17:30:47 +000043// GetFileNameRoot - Helper function to get the basename of a filename...
44static inline string GetFileNameRoot(const string &InputFilename) {
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000045 string IFN = InputFilename;
46 string outputFilename;
47 int Len = IFN.length();
48 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
49 outputFilename = string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
50 } else {
Chris Lattner3524fc22001-10-15 17:30:47 +000051 outputFilename = IFN;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000052 }
53 return outputFilename;
54}
55
Chris Lattner7e708292002-06-25 16:13:24 +000056static void insertTraceCodeFor(Module &M) {
Chris Lattner39fd6592002-05-20 21:20:08 +000057 PassManager Passes;
58
59 // Insert trace code in all functions in the module
60 switch (TraceValues) {
61 case TraceBasicBlocks:
62 Passes.add(createTraceValuesPassForBasicBlocks());
63 break;
64 case TraceFunctions:
65 Passes.add(createTraceValuesPassForFunction());
66 break;
67 default:
68 assert(0 && "Bad value for TraceValues!");
69 abort();
70 }
71
72 // Eliminate duplication in constant pool
Chris Lattner7e708292002-06-25 16:13:24 +000073 Passes.add(createConstantMergePass());
Chris Lattner39fd6592002-05-20 21:20:08 +000074
75 // Run passes to insert and clean up trace code...
76 Passes.run(M);
77
78 std::string ErrorMessage;
79
80 // Load the module that contains the runtime helper routines neccesary for
81 // pointer hashing and stuff... link this module into the program if possible
82 //
83 Module *TraceModule = ParseBytecodeFile(TraceLibPath+"libinstr.bc");
84
85 // Ok, the TraceLibPath didn't contain a valid module. Try to load the module
86 // from the current LLVM-GCC install directory. This is kindof a hack, but
87 // allows people to not HAVE to have built the library.
88 //
89 if (TraceModule == 0)
90 TraceModule = ParseBytecodeFile("/home/vadve/lattner/cvs/gcc_install/lib/"
91 "gcc-lib/llvm/3.1/libinstr.bc");
92
93 // If we still didn't get it, cancel trying to link it in...
94 if (TraceModule == 0) {
95 cerr << "Warning, could not load trace routines to link into program!\n";
96 } else {
97
98 // Link in the trace routines... if the link fails, don't panic, because the
99 // compile should still succeed, just the native linker will probably fail.
100 //
101 std::auto_ptr<Module> TraceRoutines(TraceModule);
Chris Lattner7e708292002-06-25 16:13:24 +0000102 if (LinkModules(&M, TraceRoutines.get(), &ErrorMessage))
Chris Lattner39fd6592002-05-20 21:20:08 +0000103 cerr << "Warning: Error linking in trace routines: "
104 << ErrorMessage << "\n";
105 }
106
107
108 // Write out the module with tracing code just before code generation
109 if (InputFilename != "-") {
110 string TraceFilename = GetFileNameRoot(InputFilename) + ".trace.bc";
111
112 std::ofstream Out(TraceFilename.c_str());
113 if (!Out.good()) {
114 cerr << "Error opening '" << TraceFilename
115 << "'!: Skipping output of trace code as bytecode\n";
116 } else {
117 cerr << "Emitting trace code to '" << TraceFilename
118 << "' for comparison...\n";
Chris Lattner7e708292002-06-25 16:13:24 +0000119 WriteBytecodeToFile(&M, Out);
Chris Lattner39fd6592002-05-20 21:20:08 +0000120 }
121 }
122
123}
124
Vikram S. Adve805eb962001-09-18 13:10:45 +0000125
126//===---------------------------------------------------------------------===//
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000127// Function main()
128//
129// Entry point for the llc compiler.
130//===---------------------------------------------------------------------===//
131
Chris Lattner25c12292001-10-15 17:41:24 +0000132int main(int argc, char **argv) {
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000133 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
134
135 // Allocate a target... in the future this will be controllable on the
136 // command line.
Chris Lattner697954c2002-01-20 22:54:45 +0000137 std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
Chris Lattner3524fc22001-10-15 17:30:47 +0000138 assert(target.get() && "Could not allocate target machine!");
139
140 TargetMachine &Target = *target.get();
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000141
142 // Load the module to be compiled...
Chris Lattner697954c2002-01-20 22:54:45 +0000143 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000144 if (M.get() == 0) {
145 cerr << "bytecode didn't read correctly.\n";
146 return 1;
147 }
Chris Lattner3524fc22001-10-15 17:30:47 +0000148
Chris Lattner39fd6592002-05-20 21:20:08 +0000149 if (TraceValues != TraceOff) // If tracing enabled...
Chris Lattner7e708292002-06-25 16:13:24 +0000150 insertTraceCodeFor(*M.get()); // Hack up module before using passmanager...
Chris Lattner39fd6592002-05-20 21:20:08 +0000151
Chris Lattner3524fc22001-10-15 17:30:47 +0000152 // Build up all of the passes that we want to do to the module...
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000153 PassManager Passes;
Chris Lattner3524fc22001-10-15 17:30:47 +0000154
Vikram S. Adved32e70a2002-03-24 03:19:54 +0000155 // Decompose multi-dimensional refs into a sequence of 1D refs
156 Passes.add(createDecomposeMultiDimRefsPass());
157
Vikram S. Adve712ac2b2001-10-18 18:20:20 +0000158 // Replace malloc and free instructions with library calls.
159 // Do this after tracing until lli implements these lib calls.
160 // For now, it will emulate malloc and free internally.
Chris Lattner4ddcd542002-02-26 21:36:53 +0000161 Passes.add(createLowerAllocationsPass(Target.DataLayout));
Vikram S. Adve712ac2b2001-10-18 18:20:20 +0000162
Chris Lattner3524fc22001-10-15 17:30:47 +0000163 // If LLVM dumping after transformations is requested, add it to the pipeline
164 if (DumpAsm)
Chris Lattner02d6ef82002-04-08 22:05:01 +0000165 Passes.add(new PrintFunctionPass("Code after xformations: \n", &cerr));
Chris Lattner3524fc22001-10-15 17:30:47 +0000166
Chris Lattnere41576d2002-02-03 23:43:19 +0000167 // Figure out where we are going to send the output...
168 std::ostream *Out = 0;
169 if (OutputFilename != "") { // Specified an output filename?
170 if (!Force && std::ifstream(OutputFilename.c_str())) {
171 // If force is not specified, make sure not to overwrite a file!
172 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
173 << "Use -f command line argument to force output\n";
174 return 1;
175 }
176 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +0000177
178 // Make sure that the Out file gets unlink'd from the disk if we get a
179 // SIGINT
180 RemoveFileOnSignal(OutputFilename);
Chris Lattnere41576d2002-02-03 23:43:19 +0000181 } else {
182 if (InputFilename == "-") {
183 OutputFilename = "-";
184 Out = &std::cout;
185 } else {
186 string OutputFilename = GetFileNameRoot(InputFilename);
187 OutputFilename += ".s";
Chris Lattner3524fc22001-10-15 17:30:47 +0000188
Chris Lattner888912d2002-01-22 21:07:24 +0000189 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000190 // If force is not specified, make sure not to overwrite a file!
191 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
192 << "Use -f command line argument to force output\n";
193 return 1;
194 }
Chris Lattnere41576d2002-02-03 23:43:19 +0000195
Chris Lattner697954c2002-01-20 22:54:45 +0000196 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattnere41576d2002-02-03 23:43:19 +0000197 if (!Out->good()) {
198 cerr << "Error opening " << OutputFilename << "!\n";
199 delete Out;
200 return 1;
Chris Lattner3524fc22001-10-15 17:30:47 +0000201 }
Chris Lattner76d12292002-04-18 19:55:25 +0000202 // Make sure that the Out file gets unlink'd from the disk if we get a
203 // SIGINT
204 RemoveFileOnSignal(OutputFilename);
Chris Lattner3524fc22001-10-15 17:30:47 +0000205 }
Chris Lattner3524fc22001-10-15 17:30:47 +0000206 }
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000207
Chris Lattnere41576d2002-02-03 23:43:19 +0000208 Target.addPassesToEmitAssembly(Passes, *Out);
209
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000210 // Run our queue of passes all at once now, efficiently.
Chris Lattner7e708292002-06-25 16:13:24 +0000211 Passes.run(*M.get());
Chris Lattner05e5e072001-10-18 01:31:22 +0000212
Chris Lattnere41576d2002-02-03 23:43:19 +0000213 if (Out != &std::cout) delete Out;
214
Chris Lattnerd7477ee2001-10-18 20:33:21 +0000215 return 0;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000216}