blob: 785f0a624ca4670d8f93ec31ff25f232ac05648c [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;
Anand Shuklacf17bcc2002-06-25 21:57:48 +000025using std::cerr;
Chris Lattner8f367bd2001-07-23 02:35:57 +000026
Chris Lattner5ff62e92002-07-22 02:10:13 +000027static cl::opt<string>
28InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
29
30static cl::opt<string>
31OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
32
33static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
34
35static cl::opt<bool>
36DumpAsm("d", cl::desc("Print bytecode before native code generation"),
37 cl::Hidden);
38
39static cl::opt<string>
40TraceLibPath("tracelibpath", cl::desc("Path to libinstr for trace code"),
41 cl::value_desc("directory"), cl::Hidden);
Chris Lattner4ddcd542002-02-26 21:36:53 +000042
43enum TraceLevel {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000044 TraceOff, TraceFunctions, TraceBasicBlocks
Chris Lattner4ddcd542002-02-26 21:36:53 +000045};
46
Chris Lattner5ff62e92002-07-22 02:10:13 +000047static cl::opt<TraceLevel>
48TraceValues("trace", cl::desc("Trace values through functions or basic blocks"),
49 cl::values(
Chris Lattner4ddcd542002-02-26 21:36:53 +000050 clEnumValN(TraceOff , "off", "Disable trace code"),
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000051 clEnumValN(TraceFunctions , "function", "Trace each function"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000052 clEnumValN(TraceBasicBlocks, "basicblock", "Trace each basic block"),
53 0));
Vikram S. Adve805eb962001-09-18 13:10:45 +000054
Chris Lattner3524fc22001-10-15 17:30:47 +000055// GetFileNameRoot - Helper function to get the basename of a filename...
56static inline string GetFileNameRoot(const string &InputFilename) {
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000057 string IFN = InputFilename;
58 string outputFilename;
59 int Len = IFN.length();
60 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
61 outputFilename = string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
62 } else {
Chris Lattner3524fc22001-10-15 17:30:47 +000063 outputFilename = IFN;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000064 }
65 return outputFilename;
66}
67
Chris Lattner7e708292002-06-25 16:13:24 +000068static void insertTraceCodeFor(Module &M) {
Chris Lattner39fd6592002-05-20 21:20:08 +000069 PassManager Passes;
70
71 // Insert trace code in all functions in the module
72 switch (TraceValues) {
73 case TraceBasicBlocks:
74 Passes.add(createTraceValuesPassForBasicBlocks());
75 break;
76 case TraceFunctions:
77 Passes.add(createTraceValuesPassForFunction());
78 break;
79 default:
80 assert(0 && "Bad value for TraceValues!");
81 abort();
82 }
83
84 // Eliminate duplication in constant pool
Chris Lattner7e708292002-06-25 16:13:24 +000085 Passes.add(createConstantMergePass());
Chris Lattner39fd6592002-05-20 21:20:08 +000086
87 // Run passes to insert and clean up trace code...
88 Passes.run(M);
89
90 std::string ErrorMessage;
91
92 // Load the module that contains the runtime helper routines neccesary for
93 // pointer hashing and stuff... link this module into the program if possible
94 //
95 Module *TraceModule = ParseBytecodeFile(TraceLibPath+"libinstr.bc");
96
97 // Ok, the TraceLibPath didn't contain a valid module. Try to load the module
98 // from the current LLVM-GCC install directory. This is kindof a hack, but
99 // allows people to not HAVE to have built the library.
100 //
101 if (TraceModule == 0)
102 TraceModule = ParseBytecodeFile("/home/vadve/lattner/cvs/gcc_install/lib/"
103 "gcc-lib/llvm/3.1/libinstr.bc");
104
105 // If we still didn't get it, cancel trying to link it in...
106 if (TraceModule == 0) {
107 cerr << "Warning, could not load trace routines to link into program!\n";
108 } else {
109
110 // Link in the trace routines... if the link fails, don't panic, because the
111 // compile should still succeed, just the native linker will probably fail.
112 //
113 std::auto_ptr<Module> TraceRoutines(TraceModule);
Chris Lattner7e708292002-06-25 16:13:24 +0000114 if (LinkModules(&M, TraceRoutines.get(), &ErrorMessage))
Chris Lattner39fd6592002-05-20 21:20:08 +0000115 cerr << "Warning: Error linking in trace routines: "
116 << ErrorMessage << "\n";
117 }
118
119
120 // Write out the module with tracing code just before code generation
121 if (InputFilename != "-") {
122 string TraceFilename = GetFileNameRoot(InputFilename) + ".trace.bc";
123
124 std::ofstream Out(TraceFilename.c_str());
125 if (!Out.good()) {
126 cerr << "Error opening '" << TraceFilename
127 << "'!: Skipping output of trace code as bytecode\n";
128 } else {
129 cerr << "Emitting trace code to '" << TraceFilename
130 << "' for comparison...\n";
Chris Lattner7e708292002-06-25 16:13:24 +0000131 WriteBytecodeToFile(&M, Out);
Chris Lattner39fd6592002-05-20 21:20:08 +0000132 }
133 }
134
135}
136
Vikram S. Adve805eb962001-09-18 13:10:45 +0000137
138//===---------------------------------------------------------------------===//
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000139// Function main()
140//
141// Entry point for the llc compiler.
142//===---------------------------------------------------------------------===//
143
Chris Lattner25c12292001-10-15 17:41:24 +0000144int main(int argc, char **argv) {
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000145 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
146
147 // Allocate a target... in the future this will be controllable on the
148 // command line.
Chris Lattner697954c2002-01-20 22:54:45 +0000149 std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
Chris Lattner3524fc22001-10-15 17:30:47 +0000150 assert(target.get() && "Could not allocate target machine!");
151
152 TargetMachine &Target = *target.get();
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000153
154 // Load the module to be compiled...
Chris Lattner697954c2002-01-20 22:54:45 +0000155 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000156 if (M.get() == 0) {
157 cerr << "bytecode didn't read correctly.\n";
158 return 1;
159 }
Chris Lattner3524fc22001-10-15 17:30:47 +0000160
Chris Lattner39fd6592002-05-20 21:20:08 +0000161 if (TraceValues != TraceOff) // If tracing enabled...
Chris Lattner7e708292002-06-25 16:13:24 +0000162 insertTraceCodeFor(*M.get()); // Hack up module before using passmanager...
Chris Lattner39fd6592002-05-20 21:20:08 +0000163
Chris Lattner3524fc22001-10-15 17:30:47 +0000164 // Build up all of the passes that we want to do to the module...
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000165 PassManager Passes;
Chris Lattner3524fc22001-10-15 17:30:47 +0000166
Vikram S. Adved32e70a2002-03-24 03:19:54 +0000167 // Decompose multi-dimensional refs into a sequence of 1D refs
168 Passes.add(createDecomposeMultiDimRefsPass());
169
Vikram S. Adve712ac2b2001-10-18 18:20:20 +0000170 // Replace malloc and free instructions with library calls.
171 // Do this after tracing until lli implements these lib calls.
172 // For now, it will emulate malloc and free internally.
Chris Lattner4ddcd542002-02-26 21:36:53 +0000173 Passes.add(createLowerAllocationsPass(Target.DataLayout));
Vikram S. Adve712ac2b2001-10-18 18:20:20 +0000174
Chris Lattner3524fc22001-10-15 17:30:47 +0000175 // If LLVM dumping after transformations is requested, add it to the pipeline
176 if (DumpAsm)
Chris Lattner02d6ef82002-04-08 22:05:01 +0000177 Passes.add(new PrintFunctionPass("Code after xformations: \n", &cerr));
Chris Lattner3524fc22001-10-15 17:30:47 +0000178
Chris Lattnerc5fbf712002-06-30 16:25:07 +0000179 // Strip all of the symbols from the bytecode so that it will be smaller...
180 Passes.add(createSymbolStrippingPass());
181
Chris Lattnere41576d2002-02-03 23:43:19 +0000182 // Figure out where we are going to send the output...
183 std::ostream *Out = 0;
184 if (OutputFilename != "") { // Specified an output filename?
185 if (!Force && std::ifstream(OutputFilename.c_str())) {
186 // If force is not specified, make sure not to overwrite a file!
187 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
188 << "Use -f command line argument to force output\n";
189 return 1;
190 }
191 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattner76d12292002-04-18 19:55:25 +0000192
193 // Make sure that the Out file gets unlink'd from the disk if we get a
194 // SIGINT
195 RemoveFileOnSignal(OutputFilename);
Chris Lattnere41576d2002-02-03 23:43:19 +0000196 } else {
197 if (InputFilename == "-") {
198 OutputFilename = "-";
199 Out = &std::cout;
200 } else {
201 string OutputFilename = GetFileNameRoot(InputFilename);
202 OutputFilename += ".s";
Chris Lattner3524fc22001-10-15 17:30:47 +0000203
Chris Lattner888912d2002-01-22 21:07:24 +0000204 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner697954c2002-01-20 22:54:45 +0000205 // If force is not specified, make sure not to overwrite a file!
206 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
207 << "Use -f command line argument to force output\n";
208 return 1;
209 }
Chris Lattnere41576d2002-02-03 23:43:19 +0000210
Chris Lattner697954c2002-01-20 22:54:45 +0000211 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattnere41576d2002-02-03 23:43:19 +0000212 if (!Out->good()) {
213 cerr << "Error opening " << OutputFilename << "!\n";
214 delete Out;
215 return 1;
Chris Lattner3524fc22001-10-15 17:30:47 +0000216 }
Chris Lattner76d12292002-04-18 19:55:25 +0000217 // Make sure that the Out file gets unlink'd from the disk if we get a
218 // SIGINT
219 RemoveFileOnSignal(OutputFilename);
Chris Lattner3524fc22001-10-15 17:30:47 +0000220 }
Chris Lattner3524fc22001-10-15 17:30:47 +0000221 }
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000222
Chris Lattnere41576d2002-02-03 23:43:19 +0000223 Target.addPassesToEmitAssembly(Passes, *Out);
224
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000225 // Run our queue of passes all at once now, efficiently.
Chris Lattner7e708292002-06-25 16:13:24 +0000226 Passes.run(*M.get());
Chris Lattner05e5e072001-10-18 01:31:22 +0000227
Chris Lattnere41576d2002-02-03 23:43:19 +0000228 if (Out != &std::cout) delete Out;
229
Chris Lattnerd7477ee2001-10-18 20:33:21 +0000230 return 0;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000231}