Be a bit more efficient when processing the active and inactive
[oota-llvm.git] / tools / llc / llc.cpp
1 //===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This is the llc code generator driver. It provides a convenient
11 // command-line interface for generating native assembly-language code 
12 // or C code, given LLVM bytecode.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/Bytecode/Reader.h"
17 #include "llvm/Target/TargetMachine.h"
18 #include "llvm/Target/TargetMachineRegistry.h"
19 #include "llvm/Transforms/Scalar.h"
20 #include "llvm/Module.h"
21 #include "llvm/PassManager.h"
22 #include "llvm/Pass.h"
23 #include "Support/CommandLine.h"
24 #include "Support/PluginLoader.h"
25 #include "llvm/System/Signals.h"
26 #include <fstream>
27 #include <iostream>
28 #include <memory>
29
30 using namespace llvm;
31
32 // General options for llc.  Other pass-specific options are specified
33 // within the corresponding llc passes, and target-specific options
34 // and back-end code generation options are specified with the target machine.
35 // 
36 static cl::opt<std::string>
37 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
38
39 static cl::opt<std::string>
40 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
41
42 static cl::opt<bool> Force("f", cl::desc("Overwrite output files"));
43
44 static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser>
45 MArch("march", cl::desc("Architecture to generate assembly for:"));
46                
47 // GetFileNameRoot - Helper function to get the basename of a filename...
48 static inline std::string
49 GetFileNameRoot(const std::string &InputFilename) {
50   std::string IFN = InputFilename;
51   std::string outputFilename;
52   int Len = IFN.length();
53   if ((Len > 2) &&
54       IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
55     outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/
56   } else {
57     outputFilename = IFN;
58   }
59   return outputFilename;
60 }
61
62
63 // main - Entry point for the llc compiler.
64 //
65 int main(int argc, char **argv) {
66   cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n");
67   sys::PrintStackTraceOnErrorSignal();
68
69   // Load the module to be compiled...
70   std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename));
71   if (M.get() == 0) {
72     std::cerr << argv[0] << ": bytecode didn't read correctly.\n";
73     return 1;
74   }
75   Module &mod = *M.get();
76
77   // Allocate target machine.  First, check whether the user has
78   // explicitly specified an architecture to compile for.
79   TargetMachine* (*TargetMachineAllocator)(const Module&,
80                                            IntrinsicLowering *) = 0;
81   if (MArch == 0) {
82     std::string Err;
83     MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err);
84     if (MArch == 0) {
85       std::cerr << argv[0] << ": error auto-selecting target for module '"
86                 << Err << "'.  Please use the -march option to explicitly "
87                 << "pick a target.\n";
88       return 1;
89     }
90   }
91
92   std::auto_ptr<TargetMachine> target(MArch->CtorFn(mod, 0));
93   assert(target.get() && "Could not allocate target machine!");
94   TargetMachine &Target = *target.get();
95   const TargetData &TD = Target.getTargetData();
96
97   // Build up all of the passes that we want to do to the module...
98   PassManager Passes;
99   Passes.add(new TargetData("llc", TD.isLittleEndian(), TD.getPointerSize(),
100                             TD.getPointerAlignment(), TD.getDoubleAlignment()));
101
102   // Figure out where we are going to send the output...
103   std::ostream *Out = 0;
104   if (OutputFilename != "") {
105     if (OutputFilename != "-") {
106       // Specified an output filename?
107       if (!Force && std::ifstream(OutputFilename.c_str())) {
108         // If force is not specified, make sure not to overwrite a file!
109         std::cerr << argv[0] << ": error opening '" << OutputFilename
110                   << "': file exists!\n"
111                   << "Use -f command line argument to force output\n";
112         return 1;
113       }
114       Out = new std::ofstream(OutputFilename.c_str());
115
116       // Make sure that the Out file gets unlinked from the disk if we get a
117       // SIGINT
118       sys::RemoveFileOnSignal(OutputFilename);
119     } else {
120       Out = &std::cout;
121     }
122   } else {
123     if (InputFilename == "-") {
124       OutputFilename = "-";
125       Out = &std::cout;
126     } else {
127       OutputFilename = GetFileNameRoot(InputFilename); 
128
129       if (MArch->Name[0] != 'c' || MArch->Name[1] != 0)  // not CBE
130         OutputFilename += ".s";
131       else
132         OutputFilename += ".cbe.c";
133       
134       if (!Force && std::ifstream(OutputFilename.c_str())) {
135         // If force is not specified, make sure not to overwrite a file!
136         std::cerr << argv[0] << ": error opening '" << OutputFilename
137                   << "': file exists!\n"
138                   << "Use -f command line argument to force output\n";
139         return 1;
140       }
141       
142       Out = new std::ofstream(OutputFilename.c_str());
143       if (!Out->good()) {
144         std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
145         delete Out;
146         return 1;
147       }
148       
149       // Make sure that the Out file gets unlinked from the disk if we get a
150       // SIGINT
151       sys::RemoveFileOnSignal(OutputFilename);
152     }
153   }
154
155   // Ask the target to add backend passes as necessary
156   if (Target.addPassesToEmitAssembly(Passes, *Out)) {
157     std::cerr << argv[0] << ": target '" << Target.getName()
158               << "' does not support static compilation!\n";
159     if (Out != &std::cout) delete Out;
160     // And the Out file is empty and useless, so remove it now.
161     std::remove(OutputFilename.c_str());
162     return 1;
163   } else {
164     // Run our queue of passes all at once now, efficiently.
165     Passes.run(*M.get());
166   }
167
168   // Delete the ostream if it's not a stdout stream
169   if (Out != &std::cout) delete Out;
170
171   return 0;
172 }