blob: b365c3900d8cc2673180a481e945d0873a2ba972 [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 Lattner3787ee62002-01-22 01:04:08 +000011#include "llvm/Transforms/ChangeAllocations.h"
Chris Lattner97fd6c42001-10-15 17:30:47 +000012#include "llvm/Transforms/HoistPHIConstants.h"
Vikram S. Adve025fdf92002-03-24 03:19:54 +000013#include "llvm/Transforms/Scalar/DecomposeMultiDimRefs.h"
Chris Lattner95f87b42001-10-18 20:32:07 +000014#include "llvm/Assembly/PrintModulePass.h"
15#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnerb9acf7e2001-10-18 20:06:31 +000016#include "llvm/Transforms/ConstantMerge.h"
Chris Lattnered226062001-09-07 21:26:31 +000017#include "llvm/Module.h"
Chris Lattner62b7fd12002-04-07 20:49:59 +000018#include "llvm/Function.h"
Chris Lattner7139f282002-01-31 00:46:45 +000019#include "llvm/PassManager.h"
Chris Lattner5de22042001-11-27 00:03:19 +000020#include "Support/CommandLine.h"
Chris Lattnerc065ad82002-04-18 19:55:25 +000021#include "Support/Signals.h"
Chris Lattner6c2c8702001-09-18 17:04:18 +000022#include <memory>
Vikram S. Adve2f084b22001-10-14 23:29:28 +000023#include <string>
Chris Lattner46f1b612001-09-19 16:52:09 +000024#include <fstream>
Chris Lattner7f74a562002-01-20 22:54:45 +000025using std::string;
Chris Lattner0af24642001-07-23 02:35:57 +000026
Chris Lattnerfc2ab9e2002-02-26 21:36:53 +000027static cl::String InputFilename ("", "Input filename", cl::NoFlags, "-");
28static cl::String OutputFilename("o", "Output filename", cl::NoFlags, "");
29static cl::Flag Force ("f", "Overwrite output files");
30static cl::Flag DumpAsm ("d", "Print bytecode before native code generation", cl::Hidden);
31
32enum TraceLevel {
Chris Lattner62b7fd12002-04-07 20:49:59 +000033 TraceOff, TraceFunctions, TraceBasicBlocks
Chris Lattnerfc2ab9e2002-02-26 21:36:53 +000034};
35
36static cl::Enum<enum TraceLevel> TraceValues("trace", cl::NoFlags,
Chris Lattner62b7fd12002-04-07 20:49:59 +000037 "Trace values through functions or basic blocks",
Chris Lattnerfc2ab9e2002-02-26 21:36:53 +000038 clEnumValN(TraceOff , "off", "Disable trace code"),
Chris Lattner62b7fd12002-04-07 20:49:59 +000039 clEnumValN(TraceFunctions , "function", "Trace each function"),
Chris Lattnerfc2ab9e2002-02-26 21:36:53 +000040 clEnumValN(TraceBasicBlocks, "basicblock", "Trace each basic block"), 0);
Chris Lattner0af24642001-07-23 02:35:57 +000041
Vikram S. Adve9d409352001-09-18 13:10:45 +000042
Chris Lattner97fd6c42001-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. Adve2f084b22001-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 Lattner97fd6c42001-10-15 17:30:47 +000051 outputFilename = IFN;
Vikram S. Adve2f084b22001-10-14 23:29:28 +000052 }
53 return outputFilename;
54}
55
Vikram S. Adve9d409352001-09-18 13:10:45 +000056
57//===---------------------------------------------------------------------===//
Vikram S. Adve2f084b22001-10-14 23:29:28 +000058// Function main()
59//
60// Entry point for the llc compiler.
61//===---------------------------------------------------------------------===//
62
Chris Lattnerd0c15402001-10-15 17:41:24 +000063int main(int argc, char **argv) {
Vikram S. Adve2f084b22001-10-14 23:29:28 +000064 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
65
66 // Allocate a target... in the future this will be controllable on the
67 // command line.
Chris Lattner7f74a562002-01-20 22:54:45 +000068 std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
Chris Lattner97fd6c42001-10-15 17:30:47 +000069 assert(target.get() && "Could not allocate target machine!");
70
71 TargetMachine &Target = *target.get();
Vikram S. Adve2f084b22001-10-14 23:29:28 +000072
73 // Load the module to be compiled...
Chris Lattner7f74a562002-01-20 22:54:45 +000074 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
Vikram S. Adve2f084b22001-10-14 23:29:28 +000075 if (M.get() == 0) {
76 cerr << "bytecode didn't read correctly.\n";
77 return 1;
78 }
Chris Lattner97fd6c42001-10-15 17:30:47 +000079
80 // Build up all of the passes that we want to do to the module...
Chris Lattner0686e432002-01-21 07:31:50 +000081 PassManager Passes;
Chris Lattner97fd6c42001-10-15 17:30:47 +000082
Chris Lattner97fd6c42001-10-15 17:30:47 +000083 // Hoist constants out of PHI nodes into predecessor BB's
Chris Lattnerfc2ab9e2002-02-26 21:36:53 +000084 Passes.add(createHoistPHIConstantsPass());
Chris Lattner97fd6c42001-10-15 17:30:47 +000085
Chris Lattnerfc2ab9e2002-02-26 21:36:53 +000086 if (TraceValues != TraceOff) { // If tracing enabled...
Chris Lattner62b7fd12002-04-07 20:49:59 +000087 // Insert trace code in all functions in the module
Chris Lattnerfc2ab9e2002-02-26 21:36:53 +000088 if (TraceValues == TraceBasicBlocks)
89 Passes.add(createTraceValuesPassForBasicBlocks());
Chris Lattner62b7fd12002-04-07 20:49:59 +000090 else if (TraceValues == TraceFunctions)
Chris Lattnerfc2ab9e2002-02-26 21:36:53 +000091 Passes.add(createTraceValuesPassForMethod());
92 else
93 assert(0 && "Bad value for TraceValues!");
Chris Lattnerb9acf7e2001-10-18 20:06:31 +000094
95 // Eliminate duplication in constant pool
Chris Lattnerfc2ab9e2002-02-26 21:36:53 +000096 Passes.add(createDynamicConstantMergePass());
Vikram S. Adve025fdf92002-03-24 03:19:54 +000097 }
98
99 // Decompose multi-dimensional refs into a sequence of 1D refs
100 Passes.add(createDecomposeMultiDimRefsPass());
101
102 // Write out the module with tracing code just before code generation
103 if (TraceValues != TraceOff) { // If tracing enabled...
Chris Lattnerb9acf7e2001-10-18 20:06:31 +0000104 assert(InputFilename != "-" &&
105 "files on stdin not supported with tracing");
106 string traceFileName = GetFileNameRoot(InputFilename) + ".trace.bc";
Chris Lattner7f74a562002-01-20 22:54:45 +0000107
Chris Lattner0e11e542002-01-22 21:07:24 +0000108 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000109 // If force is not specified, make sure not to overwrite a file!
110 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
111 << "Use -f command line argument to force output\n";
112 return 1;
113 }
Vikram S. Adve025fdf92002-03-24 03:19:54 +0000114
Chris Lattner7f74a562002-01-20 22:54:45 +0000115 std::ostream *os = new std::ofstream(traceFileName.c_str());
Chris Lattnerb9acf7e2001-10-18 20:06:31 +0000116 if (!os->good()) {
117 cerr << "Error opening " << traceFileName
118 << "! SKIPPING OUTPUT OF TRACE CODE\n";
119 delete os;
120 return 1;
Chris Lattner97fd6c42001-10-15 17:30:47 +0000121 }
Chris Lattnerb9acf7e2001-10-18 20:06:31 +0000122
Chris Lattner0686e432002-01-21 07:31:50 +0000123 Passes.add(new WriteBytecodePass(os, true));
Chris Lattnerb9acf7e2001-10-18 20:06:31 +0000124 }
Vikram S. Adveb2ac1e72001-10-18 13:51:20 +0000125
Vikram S. Adve71f16ec2001-10-18 18:20:20 +0000126 // Replace malloc and free instructions with library calls.
127 // Do this after tracing until lli implements these lib calls.
128 // For now, it will emulate malloc and free internally.
Chris Lattnerfc2ab9e2002-02-26 21:36:53 +0000129 Passes.add(createLowerAllocationsPass(Target.DataLayout));
Vikram S. Adve71f16ec2001-10-18 18:20:20 +0000130
Chris Lattner97fd6c42001-10-15 17:30:47 +0000131 // If LLVM dumping after transformations is requested, add it to the pipeline
132 if (DumpAsm)
Chris Lattner6e391312002-04-08 22:05:01 +0000133 Passes.add(new PrintFunctionPass("Code after xformations: \n", &cerr));
Chris Lattner97fd6c42001-10-15 17:30:47 +0000134
Chris Lattnerf7286372002-02-03 23:43:19 +0000135 // Figure out where we are going to send the output...
136 std::ostream *Out = 0;
137 if (OutputFilename != "") { // Specified an output filename?
138 if (!Force && std::ifstream(OutputFilename.c_str())) {
139 // If force is not specified, make sure not to overwrite a file!
140 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
141 << "Use -f command line argument to force output\n";
142 return 1;
143 }
144 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattnerc065ad82002-04-18 19:55:25 +0000145
146 // Make sure that the Out file gets unlink'd from the disk if we get a
147 // SIGINT
148 RemoveFileOnSignal(OutputFilename);
Chris Lattnerf7286372002-02-03 23:43:19 +0000149 } else {
150 if (InputFilename == "-") {
151 OutputFilename = "-";
152 Out = &std::cout;
153 } else {
154 string OutputFilename = GetFileNameRoot(InputFilename);
155 OutputFilename += ".s";
Chris Lattner97fd6c42001-10-15 17:30:47 +0000156
Chris Lattner0e11e542002-01-22 21:07:24 +0000157 if (!Force && std::ifstream(OutputFilename.c_str())) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000158 // If force is not specified, make sure not to overwrite a file!
159 cerr << "Error opening '" << OutputFilename << "': File exists!\n"
160 << "Use -f command line argument to force output\n";
161 return 1;
162 }
Chris Lattnerf7286372002-02-03 23:43:19 +0000163
Chris Lattner7f74a562002-01-20 22:54:45 +0000164 Out = new std::ofstream(OutputFilename.c_str());
Chris Lattnerf7286372002-02-03 23:43:19 +0000165 if (!Out->good()) {
166 cerr << "Error opening " << OutputFilename << "!\n";
167 delete Out;
168 return 1;
Chris Lattner97fd6c42001-10-15 17:30:47 +0000169 }
Chris Lattnerc065ad82002-04-18 19:55:25 +0000170 // Make sure that the Out file gets unlink'd from the disk if we get a
171 // SIGINT
172 RemoveFileOnSignal(OutputFilename);
Chris Lattner97fd6c42001-10-15 17:30:47 +0000173 }
Chris Lattner97fd6c42001-10-15 17:30:47 +0000174 }
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000175
Chris Lattnerf7286372002-02-03 23:43:19 +0000176 Target.addPassesToEmitAssembly(Passes, *Out);
177
Chris Lattner0686e432002-01-21 07:31:50 +0000178 // Run our queue of passes all at once now, efficiently.
179 Passes.run(M.get());
Chris Lattner2fa0dab2001-10-18 01:31:22 +0000180
Chris Lattnerf7286372002-02-03 23:43:19 +0000181 if (Out != &std::cout) delete Out;
182
Chris Lattner1a9680f2001-10-18 20:33:21 +0000183 return 0;
Vikram S. Adve2f084b22001-10-14 23:29:28 +0000184}