blob: 88f327d7aed9f2ebbb4d19804c963720e8bf1266 [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 Lattner65f1b892002-05-07 20:03:27 +000011#include "llvm/Transforms/Scalar.h"
Chris Lattner39fd6592002-05-20 21:20:08 +000012#include "llvm/Transforms/Utils/Linker.h"
Chris Lattner2197d212001-10-18 20:32:07 +000013#include "llvm/Assembly/PrintModulePass.h"
14#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnerd9d8c072002-07-23 22:04:43 +000015#include "llvm/Transforms/IPO.h"
Chris Lattner46ac43c2001-09-07 21:26:31 +000016#include "llvm/Module.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000017#include "llvm/Function.h"
Chris Lattnercd50d3f2002-01-31 00:46:45 +000018#include "llvm/PassManager.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000019#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000020#include "Support/Signals.h"
Chris Lattnerda784ee2001-09-18 17:04:18 +000021#include <memory>
Chris Lattner78f7e1a2001-09-19 16:52:09 +000022#include <fstream>
Chris Lattner697954c2002-01-20 22:54:45 +000023using std::string;
Anand Shuklacf17bcc2002-06-25 21:57:48 +000024using std::cerr;
Chris Lattner8f367bd2001-07-23 02:35:57 +000025
Chris Lattner5ff62e92002-07-22 02:10:13 +000026static cl::opt<string>
27InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
28
29static cl::opt<string>
30OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
31
32static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
33
34static cl::opt<bool>
35DumpAsm("d", cl::desc("Print bytecode before native code generation"),
36 cl::Hidden);
37
38static cl::opt<string>
39TraceLibPath("tracelibpath", cl::desc("Path to libinstr for trace code"),
40 cl::value_desc("directory"), cl::Hidden);
Chris Lattner4ddcd542002-02-26 21:36:53 +000041
42enum TraceLevel {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000043 TraceOff, TraceFunctions, TraceBasicBlocks
Chris Lattner4ddcd542002-02-26 21:36:53 +000044};
45
Chris Lattner5ff62e92002-07-22 02:10:13 +000046static cl::opt<TraceLevel>
47TraceValues("trace", cl::desc("Trace values through functions or basic blocks"),
48 cl::values(
Chris Lattner4ddcd542002-02-26 21:36:53 +000049 clEnumValN(TraceOff , "off", "Disable trace code"),
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000050 clEnumValN(TraceFunctions , "function", "Trace each function"),
Chris Lattner5ff62e92002-07-22 02:10:13 +000051 clEnumValN(TraceBasicBlocks, "basicblock", "Trace each basic block"),
52 0));
Vikram S. Adve805eb962001-09-18 13:10:45 +000053
Chris Lattner3524fc22001-10-15 17:30:47 +000054// GetFileNameRoot - Helper function to get the basename of a filename...
55static inline string GetFileNameRoot(const string &InputFilename) {
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000056 string IFN = InputFilename;
57 string outputFilename;
58 int Len = IFN.length();
59 if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
60 outputFilename = string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
61 } else {
Chris Lattner3524fc22001-10-15 17:30:47 +000062 outputFilename = IFN;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +000063 }
64 return outputFilename;
65}
66
Chris Lattner7e708292002-06-25 16:13:24 +000067static void insertTraceCodeFor(Module &M) {
Chris Lattner39fd6592002-05-20 21:20:08 +000068 PassManager Passes;
69
70 // Insert trace code in all functions in the module
71 switch (TraceValues) {
72 case TraceBasicBlocks:
73 Passes.add(createTraceValuesPassForBasicBlocks());
74 break;
75 case TraceFunctions:
76 Passes.add(createTraceValuesPassForFunction());
77 break;
78 default:
79 assert(0 && "Bad value for TraceValues!");
80 abort();
81 }
82
83 // Eliminate duplication in constant pool
Chris Lattner7e708292002-06-25 16:13:24 +000084 Passes.add(createConstantMergePass());
Chris Lattner39fd6592002-05-20 21:20:08 +000085
86 // Run passes to insert and clean up trace code...
87 Passes.run(M);
88
89 std::string ErrorMessage;
90
91 // Load the module that contains the runtime helper routines neccesary for
92 // pointer hashing and stuff... link this module into the program if possible
93 //
94 Module *TraceModule = ParseBytecodeFile(TraceLibPath+"libinstr.bc");
95
96 // Ok, the TraceLibPath didn't contain a valid module. Try to load the module
97 // from the current LLVM-GCC install directory. This is kindof a hack, but
98 // allows people to not HAVE to have built the library.
99 //
100 if (TraceModule == 0)
101 TraceModule = ParseBytecodeFile("/home/vadve/lattner/cvs/gcc_install/lib/"
102 "gcc-lib/llvm/3.1/libinstr.bc");
103
104 // If we still didn't get it, cancel trying to link it in...
105 if (TraceModule == 0) {
106 cerr << "Warning, could not load trace routines to link into program!\n";
107 } else {
108
109 // Link in the trace routines... if the link fails, don't panic, because the
110 // compile should still succeed, just the native linker will probably fail.
111 //
112 std::auto_ptr<Module> TraceRoutines(TraceModule);
Chris Lattner7e708292002-06-25 16:13:24 +0000113 if (LinkModules(&M, TraceRoutines.get(), &ErrorMessage))
Chris Lattner39fd6592002-05-20 21:20:08 +0000114 cerr << "Warning: Error linking in trace routines: "
115 << ErrorMessage << "\n";
116 }
117
118
119 // Write out the module with tracing code just before code generation
120 if (InputFilename != "-") {
121 string TraceFilename = GetFileNameRoot(InputFilename) + ".trace.bc";
122
123 std::ofstream Out(TraceFilename.c_str());
124 if (!Out.good()) {
125 cerr << "Error opening '" << TraceFilename
126 << "'!: Skipping output of trace code as bytecode\n";
127 } else {
128 cerr << "Emitting trace code to '" << TraceFilename
129 << "' for comparison...\n";
Chris Lattner7e708292002-06-25 16:13:24 +0000130 WriteBytecodeToFile(&M, Out);
Chris Lattner39fd6592002-05-20 21:20:08 +0000131 }
132 }
133
134}
135
Vikram S. Adve805eb962001-09-18 13:10:45 +0000136
137//===---------------------------------------------------------------------===//
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000138// Function main()
139//
140// Entry point for the llc compiler.
141//===---------------------------------------------------------------------===//
142
Chris Lattner25c12292001-10-15 17:41:24 +0000143int main(int argc, char **argv) {
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000144 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
145
146 // Allocate a target... in the future this will be controllable on the
147 // command line.
Chris Lattner697954c2002-01-20 22:54:45 +0000148 std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
Chris Lattner3524fc22001-10-15 17:30:47 +0000149 assert(target.get() && "Could not allocate target machine!");
150
151 TargetMachine &Target = *target.get();
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000152
153 // Load the module to be compiled...
Chris Lattner697954c2002-01-20 22:54:45 +0000154 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000155 if (M.get() == 0) {
Chris Lattner50e3a202002-07-30 21:43:22 +0000156 cerr << argv[0] << ": bytecode didn't read correctly.\n";
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000157 return 1;
158 }
Chris Lattner3524fc22001-10-15 17:30:47 +0000159
Chris Lattner39fd6592002-05-20 21:20:08 +0000160 if (TraceValues != TraceOff) // If tracing enabled...
Chris Lattner7e708292002-06-25 16:13:24 +0000161 insertTraceCodeFor(*M.get()); // Hack up module before using passmanager...
Chris Lattner39fd6592002-05-20 21:20:08 +0000162
Chris Lattner3524fc22001-10-15 17:30:47 +0000163 // Build up all of the passes that we want to do to the module...
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000164 PassManager Passes;
Chris Lattner3524fc22001-10-15 17:30:47 +0000165
Vikram S. Adved32e70a2002-03-24 03:19:54 +0000166 // Decompose multi-dimensional refs into a sequence of 1D refs
167 Passes.add(createDecomposeMultiDimRefsPass());
168
Vikram S. Adve712ac2b2001-10-18 18:20:20 +0000169 // Replace malloc and free instructions with library calls.
170 // Do this after tracing until lli implements these lib calls.
171 // For now, it will emulate malloc and free internally.
Chris Lattner4ddcd542002-02-26 21:36:53 +0000172 Passes.add(createLowerAllocationsPass(Target.DataLayout));
Vikram S. Adve712ac2b2001-10-18 18:20:20 +0000173
Chris Lattner3524fc22001-10-15 17:30:47 +0000174 // If LLVM dumping after transformations is requested, add it to the pipeline
175 if (DumpAsm)
Chris Lattner02d6ef82002-04-08 22:05:01 +0000176 Passes.add(new PrintFunctionPass("Code after xformations: \n", &cerr));
Chris Lattner3524fc22001-10-15 17:30:47 +0000177
Chris Lattnerc5fbf712002-06-30 16:25:07 +0000178 // Strip all of the symbols from the bytecode so that it will be smaller...
179 Passes.add(createSymbolStrippingPass());
180
Chris Lattnere41576d2002-02-03 23:43:19 +0000181 // Figure out where we are going to send the output...
182 std::ostream *Out = 0;
183 if (OutputFilename != "") { // Specified an output filename?
184 if (!Force && std::ifstream(OutputFilename.c_str())) {
185 // If force is not specified, make sure not to overwrite a file!
Chris Lattner50e3a202002-07-30 21:43:22 +0000186 cerr << argv[0] << ": error opening '" << OutputFilename
187 << "': file exists!\n"
Chris Lattnere41576d2002-02-03 23:43:19 +0000188 << "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!
Chris Lattner50e3a202002-07-30 21:43:22 +0000206 cerr << argv[0] << ": error opening '" << OutputFilename
207 << "': file exists!\n"
Chris Lattner697954c2002-01-20 22:54:45 +0000208 << "Use -f command line argument to force output\n";
209 return 1;
210 }
Chris Lattnere41576d2002-02-03 23:43:19 +0000211
Chris Lattner697954c2002-01-20 22:54:45 +0000212 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattnere41576d2002-02-03 23:43:19 +0000213 if (!Out->good()) {
Chris Lattner50e3a202002-07-30 21:43:22 +0000214 cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
Chris Lattnere41576d2002-02-03 23:43:19 +0000215 delete Out;
216 return 1;
Chris Lattner3524fc22001-10-15 17:30:47 +0000217 }
Chris Lattner76d12292002-04-18 19:55:25 +0000218 // Make sure that the Out file gets unlink'd from the disk if we get a
219 // SIGINT
220 RemoveFileOnSignal(OutputFilename);
Chris Lattner3524fc22001-10-15 17:30:47 +0000221 }
Chris Lattner3524fc22001-10-15 17:30:47 +0000222 }
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000223
Chris Lattnere41576d2002-02-03 23:43:19 +0000224 Target.addPassesToEmitAssembly(Passes, *Out);
225
Chris Lattnerf4de63f2002-01-21 07:31:50 +0000226 // Run our queue of passes all at once now, efficiently.
Chris Lattner7e708292002-06-25 16:13:24 +0000227 Passes.run(*M.get());
Chris Lattner05e5e072001-10-18 01:31:22 +0000228
Chris Lattnere41576d2002-02-03 23:43:19 +0000229 if (Out != &std::cout) delete Out;
230
Chris Lattnerd7477ee2001-10-18 20:33:21 +0000231 return 0;
Vikram S. Adve2f64f9f2001-10-14 23:29:28 +0000232}