R600: Don't unnecessarily repeat the register class
[oota-llvm.git] / tools / llvm-lto / llvm-lto.cpp
1 //===-- llvm-lto: a simple command-line program to link modules with LTO --===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This program takes in a list of bitcode files, links them, performs link-time
11 // optimization, and outputs an object file.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/ADT/StringSet.h"
16 #include "llvm/CodeGen/CommandFlags.h"
17 #include "llvm/LTO/LTOCodeGenerator.h"
18 #include "llvm/LTO/LTOModule.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/ManagedStatic.h"
22 #include "llvm/Support/PrettyStackTrace.h"
23 #include "llvm/Support/Signals.h"
24 #include "llvm/Support/TargetSelect.h"
25 #include "llvm/Support/raw_ostream.h"
26
27 using namespace llvm;
28
29 static cl::opt<bool>
30 DisableOpt("disable-opt", cl::init(false),
31   cl::desc("Do not run any optimization passes"));
32
33 static cl::opt<bool>
34 DisableInline("disable-inlining", cl::init(false),
35   cl::desc("Do not run the inliner pass"));
36
37 static cl::opt<bool>
38 DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
39   cl::desc("Do not run the GVN load PRE pass"));
40
41 static cl::opt<bool>
42 DisableLTOVectorization("disable-lto-vectorization", cl::init(false),
43   cl::desc("Do not run loop or slp vectorization during LTO"));
44
45 static cl::opt<bool>
46 UseDiagnosticHandler("use-diagnostic-handler", cl::init(false),
47   cl::desc("Use a diagnostic handler to test the handler interface"));
48
49 static cl::list<std::string>
50 InputFilenames(cl::Positional, cl::OneOrMore,
51   cl::desc("<input bitcode files>"));
52
53 static cl::opt<std::string>
54 OutputFilename("o", cl::init(""),
55   cl::desc("Override output filename"),
56   cl::value_desc("filename"));
57
58 static cl::list<std::string>
59 ExportedSymbols("exported-symbol",
60   cl::desc("Symbol to export from the resulting object file"),
61   cl::ZeroOrMore);
62
63 static cl::list<std::string>
64 DSOSymbols("dso-symbol",
65   cl::desc("Symbol to put in the symtab in the resulting dso"),
66   cl::ZeroOrMore);
67
68 namespace {
69 struct ModuleInfo {
70   std::vector<bool> CanBeHidden;
71 };
72 }
73
74 void handleDiagnostics(lto_codegen_diagnostic_severity_t Severity,
75                        const char *Msg, void *) {
76   switch (Severity) {
77   case LTO_DS_NOTE:
78     errs() << "note: ";
79     break;
80   case LTO_DS_REMARK:
81     errs() << "remark: ";
82     break;
83   case LTO_DS_ERROR:
84     errs() << "error: ";
85     break;
86   case LTO_DS_WARNING:
87     errs() << "warning: ";
88     break;
89   }
90   errs() << Msg << "\n";
91 }
92
93 int main(int argc, char **argv) {
94   // Print a stack trace if we signal out.
95   sys::PrintStackTraceOnErrorSignal();
96   PrettyStackTraceProgram X(argc, argv);
97
98   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
99   cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n");
100
101   // Initialize the configured targets.
102   InitializeAllTargets();
103   InitializeAllTargetMCs();
104   InitializeAllAsmPrinters();
105   InitializeAllAsmParsers();
106
107   // set up the TargetOptions for the machine
108   TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
109
110   unsigned BaseArg = 0;
111
112   LTOCodeGenerator CodeGen;
113
114   if (UseDiagnosticHandler)
115     CodeGen.setDiagnosticHandler(handleDiagnostics, nullptr);
116
117   switch (RelocModel) {
118   case Reloc::Static:
119     CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_STATIC);
120     break;
121   case Reloc::PIC_:
122     CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC);
123     break;
124   case Reloc::DynamicNoPIC:
125     CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC);
126     break;
127   default:
128     CodeGen.setCodePICModel(LTO_CODEGEN_PIC_MODEL_DEFAULT);
129   }
130
131   CodeGen.setDebugInfo(LTO_DEBUG_MODEL_DWARF);
132   CodeGen.setTargetOptions(Options);
133
134   llvm::StringSet<llvm::MallocAllocator> DSOSymbolsSet;
135   for (unsigned i = 0; i < DSOSymbols.size(); ++i)
136     DSOSymbolsSet.insert(DSOSymbols[i]);
137
138   std::vector<std::string> KeptDSOSyms;
139
140   for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
141     std::string error;
142     std::unique_ptr<LTOModule> Module(
143         LTOModule::createFromFile(InputFilenames[i].c_str(), Options, error));
144     if (!error.empty()) {
145       errs() << argv[0] << ": error loading file '" << InputFilenames[i]
146              << "': " << error << "\n";
147       return 1;
148     }
149
150     if (!CodeGen.addModule(Module.get()))
151       return 1;
152
153     unsigned NumSyms = Module->getSymbolCount();
154     for (unsigned I = 0; I < NumSyms; ++I) {
155       StringRef Name = Module->getSymbolName(I);
156       if (!DSOSymbolsSet.count(Name))
157         continue;
158       lto_symbol_attributes Attrs = Module->getSymbolAttributes(I);
159       unsigned Scope = Attrs & LTO_SYMBOL_SCOPE_MASK;
160       if (Scope != LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN)
161         KeptDSOSyms.push_back(Name);
162     }
163   }
164
165   // Add all the exported symbols to the table of symbols to preserve.
166   for (unsigned i = 0; i < ExportedSymbols.size(); ++i)
167     CodeGen.addMustPreserveSymbol(ExportedSymbols[i].c_str());
168
169   // Add all the dso symbols to the table of symbols to expose.
170   for (unsigned i = 0; i < KeptDSOSyms.size(); ++i)
171     CodeGen.addMustPreserveSymbol(KeptDSOSyms[i].c_str());
172
173   std::string attrs;
174   for (unsigned i = 0; i < MAttrs.size(); ++i) {
175     if (i > 0)
176       attrs.append(",");
177     attrs.append(MAttrs[i]);
178   }
179
180   if (!attrs.empty())
181     CodeGen.setAttr(attrs.c_str());
182
183   if (!OutputFilename.empty()) {
184     size_t len = 0;
185     std::string ErrorInfo;
186     const void *Code =
187         CodeGen.compile(&len, DisableOpt, DisableInline, DisableGVNLoadPRE,
188                         DisableLTOVectorization, ErrorInfo);
189     if (!Code) {
190       errs() << argv[0]
191              << ": error compiling the code: " << ErrorInfo << "\n";
192       return 1;
193     }
194
195     std::error_code EC;
196     raw_fd_ostream FileStream(OutputFilename, EC, sys::fs::F_None);
197     if (EC) {
198       errs() << argv[0] << ": error opening the file '" << OutputFilename
199              << "': " << EC.message() << "\n";
200       return 1;
201     }
202
203     FileStream.write(reinterpret_cast<const char *>(Code), len);
204   } else {
205     std::string ErrorInfo;
206     const char *OutputName = nullptr;
207     if (!CodeGen.compile_to_file(&OutputName, DisableOpt, DisableInline,
208                                  DisableGVNLoadPRE, DisableLTOVectorization,
209                                  ErrorInfo)) {
210       errs() << argv[0]
211              << ": error compiling the code: " << ErrorInfo
212              << "\n";
213       return 1;
214     }
215
216     outs() << "Wrote native object file '" << OutputName << "'\n";
217   }
218
219   return 0;
220 }