Changes for 64bit gcc
[oota-llvm.git] / tools / llc / llc.cpp
1 //===-- llc.cpp - Implement the LLVM Compiler -----------------------------===//
2 //
3 // This is the llc compiler driver.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/Bytecode/Reader.h"
8 #include "llvm/Target/Sparc.h"
9 #include "llvm/Target/TargetMachine.h"
10 #include "llvm/Transforms/Instrumentation/TraceValues.h"
11 #include "llvm/Transforms/ChangeAllocations.h"
12 #include "llvm/Transforms/Scalar.h"
13 #include "llvm/Transforms/Utils/Linker.h"
14 #include "llvm/Assembly/PrintModulePass.h"
15 #include "llvm/Bytecode/WriteBytecodePass.h"
16 #include "llvm/Transforms/ConstantMerge.h"
17 #include "llvm/Module.h"
18 #include "llvm/Function.h"
19 #include "llvm/PassManager.h"
20 #include "Support/CommandLine.h"
21 #include "Support/Signals.h"
22 #include <memory>
23 #include <fstream>
24 using std::string;
25 using std::cerr;
26
27 static cl::String InputFilename ("", "Input filename", cl::NoFlags, "-");
28 static cl::String OutputFilename("o", "Output filename", cl::NoFlags, "");
29 static cl::Flag   Force         ("f", "Overwrite output files");
30 static cl::Flag   DumpAsm       ("d", "Print bytecode before native code generation", cl::Hidden);
31 static cl::String TraceLibPath  ("tracelibpath", "Path to libinstr for trace code", cl::Hidden, "");
32
33 enum TraceLevel {
34   TraceOff, TraceFunctions, TraceBasicBlocks
35 };
36
37 static cl::Enum<enum TraceLevel> TraceValues("trace", cl::NoFlags,
38   "Trace values through functions or basic blocks",
39   clEnumValN(TraceOff        , "off",        "Disable trace code"),
40   clEnumValN(TraceFunctions  , "function",   "Trace each function"),
41   clEnumValN(TraceBasicBlocks, "basicblock", "Trace each basic block"), 0);
42
43
44 // GetFileNameRoot - Helper function to get the basename of a filename...
45 static inline string GetFileNameRoot(const string &InputFilename) {
46   string IFN = InputFilename;
47   string outputFilename;
48   int Len = IFN.length();
49   if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
50     outputFilename = string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
51   } else {
52     outputFilename = IFN;
53   }
54   return outputFilename;
55 }
56
57 static void insertTraceCodeFor(Module &M) {
58   PassManager Passes;
59
60   // Insert trace code in all functions in the module
61   switch (TraceValues) {
62   case TraceBasicBlocks:
63     Passes.add(createTraceValuesPassForBasicBlocks());
64     break;
65   case TraceFunctions:
66     Passes.add(createTraceValuesPassForFunction());
67     break;
68   default:
69     assert(0 && "Bad value for TraceValues!");
70     abort();
71   }
72   
73   // Eliminate duplication in constant pool
74   Passes.add(createConstantMergePass());
75
76   // Run passes to insert and clean up trace code...
77   Passes.run(M);
78
79   std::string ErrorMessage;
80
81   // Load the module that contains the runtime helper routines neccesary for
82   // pointer hashing and stuff...  link this module into the program if possible
83   //
84   Module *TraceModule = ParseBytecodeFile(TraceLibPath+"libinstr.bc");
85
86   // Ok, the TraceLibPath didn't contain a valid module.  Try to load the module
87   // from the current LLVM-GCC install directory.  This is kindof a hack, but
88   // allows people to not HAVE to have built the library.
89   //
90   if (TraceModule == 0)
91     TraceModule = ParseBytecodeFile("/home/vadve/lattner/cvs/gcc_install/lib/"
92                                     "gcc-lib/llvm/3.1/libinstr.bc");
93
94   // If we still didn't get it, cancel trying to link it in...
95   if (TraceModule == 0) {
96     cerr << "Warning, could not load trace routines to link into program!\n";
97   } else {
98
99     // Link in the trace routines... if the link fails, don't panic, because the
100     // compile should still succeed, just the native linker will probably fail.
101     //
102     std::auto_ptr<Module> TraceRoutines(TraceModule);
103     if (LinkModules(&M, TraceRoutines.get(), &ErrorMessage))
104       cerr << "Warning: Error linking in trace routines: "
105            << ErrorMessage << "\n";
106   }
107
108
109   // Write out the module with tracing code just before code generation
110   if (InputFilename != "-") {
111     string TraceFilename = GetFileNameRoot(InputFilename) + ".trace.bc";
112
113     std::ofstream Out(TraceFilename.c_str());
114     if (!Out.good()) {
115       cerr << "Error opening '" << TraceFilename
116            << "'!: Skipping output of trace code as bytecode\n";
117     } else {
118       cerr << "Emitting trace code to '" << TraceFilename
119            << "' for comparison...\n";
120       WriteBytecodeToFile(&M, Out);
121     }
122   }
123
124 }
125   
126
127 //===---------------------------------------------------------------------===//
128 // Function main()
129 // 
130 // Entry point for the llc compiler.
131 //===---------------------------------------------------------------------===//
132
133 int main(int argc, char **argv) {
134   cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
135   
136   // Allocate a target... in the future this will be controllable on the
137   // command line.
138   std::auto_ptr<TargetMachine> target(allocateSparcTargetMachine());
139   assert(target.get() && "Could not allocate target machine!");
140
141   TargetMachine &Target = *target.get();
142   
143   // Load the module to be compiled...
144   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
145   if (M.get() == 0) {
146     cerr << "bytecode didn't read correctly.\n";
147     return 1;
148   }
149
150   if (TraceValues != TraceOff)    // If tracing enabled...
151     insertTraceCodeFor(*M.get()); // Hack up module before using passmanager...
152
153   // Build up all of the passes that we want to do to the module...
154   PassManager Passes;
155
156   // Decompose multi-dimensional refs into a sequence of 1D refs
157   Passes.add(createDecomposeMultiDimRefsPass());
158   
159   // Replace malloc and free instructions with library calls.
160   // Do this after tracing until lli implements these lib calls.
161   // For now, it will emulate malloc and free internally.
162   Passes.add(createLowerAllocationsPass(Target.DataLayout));
163   
164   // If LLVM dumping after transformations is requested, add it to the pipeline
165   if (DumpAsm)
166     Passes.add(new PrintFunctionPass("Code after xformations: \n", &cerr));
167
168   // Figure out where we are going to send the output...
169   std::ostream *Out = 0;
170   if (OutputFilename != "") {   // Specified an output filename?
171     if (!Force && std::ifstream(OutputFilename.c_str())) {
172       // If force is not specified, make sure not to overwrite a file!
173       cerr << "Error opening '" << OutputFilename << "': File exists!\n"
174            << "Use -f command line argument to force output\n";
175       return 1;
176     }
177     Out = new std::ofstream(OutputFilename.c_str());
178
179     // Make sure that the Out file gets unlink'd from the disk if we get a
180     // SIGINT
181     RemoveFileOnSignal(OutputFilename);
182   } else {
183     if (InputFilename == "-") {
184       OutputFilename = "-";
185       Out = &std::cout;
186     } else {
187       string OutputFilename = GetFileNameRoot(InputFilename); 
188       OutputFilename += ".s";
189
190       if (!Force && std::ifstream(OutputFilename.c_str())) {
191         // If force is not specified, make sure not to overwrite a file!
192         cerr << "Error opening '" << OutputFilename << "': File exists!\n"
193              << "Use -f command line argument to force output\n";
194         return 1;
195       }
196
197       Out = new std::ofstream(OutputFilename.c_str());
198       if (!Out->good()) {
199         cerr << "Error opening " << OutputFilename << "!\n";
200         delete Out;
201         return 1;
202       }
203       // Make sure that the Out file gets unlink'd from the disk if we get a
204       // SIGINT
205       RemoveFileOnSignal(OutputFilename);
206     }
207   }
208   
209   Target.addPassesToEmitAssembly(Passes, *Out);
210   
211   // Run our queue of passes all at once now, efficiently.
212   Passes.run(*M.get());
213
214   if (Out != &std::cout) delete Out;
215
216   return 0;
217 }