Re-sort all of the includes with ./utils/sort_includes.py so that
[oota-llvm.git] / lib / LTO / LTOCodeGenerator.cpp
1 //===-LTOCodeGenerator.cpp - LLVM Link Time Optimizer ---------------------===//
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 file implements the Link Time Optimization library. This library is
11 // intended to be used by linker to optimize code at link time.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/LTO/LTOCodeGenerator.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/Analysis/Passes.h"
18 #include "llvm/Analysis/Verifier.h"
19 #include "llvm/Bitcode/ReaderWriter.h"
20 #include "llvm/CodeGen/RuntimeLibcalls.h"
21 #include "llvm/Config/config.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/DerivedTypes.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/InitializePasses.h"
28 #include "llvm/LTO/LTOModule.h"
29 #include "llvm/Linker.h"
30 #include "llvm/MC/MCAsmInfo.h"
31 #include "llvm/MC/MCContext.h"
32 #include "llvm/MC/SubtargetFeature.h"
33 #include "llvm/PassManager.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/FileSystem.h"
36 #include "llvm/Support/FormattedStream.h"
37 #include "llvm/Support/Host.h"
38 #include "llvm/Support/MemoryBuffer.h"
39 #include "llvm/Support/Signals.h"
40 #include "llvm/Support/TargetRegistry.h"
41 #include "llvm/Support/TargetSelect.h"
42 #include "llvm/Support/ToolOutputFile.h"
43 #include "llvm/Support/system_error.h"
44 #include "llvm/Target/Mangler.h"
45 #include "llvm/Target/TargetLibraryInfo.h"
46 #include "llvm/Target/TargetLowering.h"
47 #include "llvm/Target/TargetOptions.h"
48 #include "llvm/Target/TargetRegisterInfo.h"
49 #include "llvm/Transforms/IPO.h"
50 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
51 #include "llvm/Transforms/ObjCARC.h"
52 using namespace llvm;
53
54 const char* LTOCodeGenerator::getVersionString() {
55 #ifdef LLVM_VERSION_INFO
56   return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
57 #else
58   return PACKAGE_NAME " version " PACKAGE_VERSION;
59 #endif
60 }
61
62 LTOCodeGenerator::LTOCodeGenerator()
63     : Context(getGlobalContext()), Linker(new Module("ld-temp.o", Context)),
64       TargetMach(NULL), EmitDwarfDebugInfo(false), ScopeRestrictionsDone(false),
65       CodeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC), NativeObjectFile(NULL) {
66   initializeLTOPasses();
67 }
68
69 LTOCodeGenerator::~LTOCodeGenerator() {
70   delete TargetMach;
71   delete NativeObjectFile;
72   TargetMach = NULL;
73   NativeObjectFile = NULL;
74
75   Linker.deleteModule();
76
77   for (std::vector<char *>::iterator I = CodegenOptions.begin(),
78                                      E = CodegenOptions.end();
79        I != E; ++I)
80     free(*I);
81 }
82
83 // Initialize LTO passes. Please keep this funciton in sync with
84 // PassManagerBuilder::populateLTOPassManager(), and make sure all LTO
85 // passes are initialized. 
86 //
87 void LTOCodeGenerator::initializeLTOPasses() {
88   PassRegistry &R = *PassRegistry::getPassRegistry();
89
90   initializeInternalizePassPass(R);
91   initializeIPSCCPPass(R);
92   initializeGlobalOptPass(R);
93   initializeConstantMergePass(R);
94   initializeDAHPass(R);
95   initializeInstCombinerPass(R);
96   initializeSimpleInlinerPass(R);
97   initializePruneEHPass(R);
98   initializeGlobalDCEPass(R);
99   initializeArgPromotionPass(R);
100   initializeJumpThreadingPass(R);
101   initializeSROAPass(R);
102   initializeSROA_DTPass(R);
103   initializeSROA_SSAUpPass(R);
104   initializeFunctionAttrsPass(R);
105   initializeGlobalsModRefPass(R);
106   initializeLICMPass(R);
107   initializeGVNPass(R);
108   initializeMemCpyOptPass(R);
109   initializeDCEPass(R);
110   initializeCFGSimplifyPassPass(R);
111 }
112
113 bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) {
114   bool ret = Linker.linkInModule(mod->getLLVVMModule(), &errMsg);
115
116   const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs();
117   for (int i = 0, e = undefs.size(); i != e; ++i)
118     AsmUndefinedRefs[undefs[i]] = 1;
119
120   return !ret;
121 }
122
123 void LTOCodeGenerator::setTargetOptions(TargetOptions options) {
124   Options.LessPreciseFPMADOption = options.LessPreciseFPMADOption;
125   Options.NoFramePointerElim = options.NoFramePointerElim;
126   Options.AllowFPOpFusion = options.AllowFPOpFusion;
127   Options.UnsafeFPMath = options.UnsafeFPMath;
128   Options.NoInfsFPMath = options.NoInfsFPMath;
129   Options.NoNaNsFPMath = options.NoNaNsFPMath;
130   Options.HonorSignDependentRoundingFPMathOption =
131     options.HonorSignDependentRoundingFPMathOption;
132   Options.UseSoftFloat = options.UseSoftFloat;
133   Options.FloatABIType = options.FloatABIType;
134   Options.NoZerosInBSS = options.NoZerosInBSS;
135   Options.GuaranteedTailCallOpt = options.GuaranteedTailCallOpt;
136   Options.DisableTailCalls = options.DisableTailCalls;
137   Options.StackAlignmentOverride = options.StackAlignmentOverride;
138   Options.TrapFuncName = options.TrapFuncName;
139   Options.PositionIndependentExecutable = options.PositionIndependentExecutable;
140   Options.EnableSegmentedStacks = options.EnableSegmentedStacks;
141   Options.UseInitArray = options.UseInitArray;
142 }
143
144 void LTOCodeGenerator::setDebugInfo(lto_debug_model debug) {
145   switch (debug) {
146   case LTO_DEBUG_MODEL_NONE:
147     EmitDwarfDebugInfo = false;
148     return;
149
150   case LTO_DEBUG_MODEL_DWARF:
151     EmitDwarfDebugInfo = true;
152     return;
153   }
154   llvm_unreachable("Unknown debug format!");
155 }
156
157 void LTOCodeGenerator::setCodePICModel(lto_codegen_model model) {
158   switch (model) {
159   case LTO_CODEGEN_PIC_MODEL_STATIC:
160   case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
161   case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
162     CodeModel = model;
163     return;
164   }
165   llvm_unreachable("Unknown PIC model!");
166 }
167
168 bool LTOCodeGenerator::writeMergedModules(const char *path,
169                                           std::string &errMsg) {
170   if (!determineTarget(errMsg))
171     return false;
172
173   // mark which symbols can not be internalized
174   applyScopeRestrictions();
175
176   // create output file
177   std::string ErrInfo;
178   tool_output_file Out(path, ErrInfo, sys::fs::F_Binary);
179   if (!ErrInfo.empty()) {
180     errMsg = "could not open bitcode file for writing: ";
181     errMsg += path;
182     return false;
183   }
184
185   // write bitcode to it
186   WriteBitcodeToFile(Linker.getModule(), Out.os());
187   Out.os().close();
188
189   if (Out.os().has_error()) {
190     errMsg = "could not write bitcode file: ";
191     errMsg += path;
192     Out.os().clear_error();
193     return false;
194   }
195
196   Out.keep();
197   return true;
198 }
199
200 bool LTOCodeGenerator::compile_to_file(const char** name, 
201                                        bool disableOpt,
202                                        bool disableInline,
203                                        bool disableGVNLoadPRE,
204                                        std::string& errMsg) {
205   // make unique temp .o file to put generated object file
206   SmallString<128> Filename;
207   int FD;
208   error_code EC = sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filename);
209   if (EC) {
210     errMsg = EC.message();
211     return false;
212   }
213
214   // generate object file
215   tool_output_file objFile(Filename.c_str(), FD);
216
217   bool genResult = generateObjectFile(objFile.os(), disableOpt, disableInline,
218                                       disableGVNLoadPRE, errMsg);
219   objFile.os().close();
220   if (objFile.os().has_error()) {
221     objFile.os().clear_error();
222     sys::fs::remove(Twine(Filename));
223     return false;
224   }
225
226   objFile.keep();
227   if (!genResult) {
228     sys::fs::remove(Twine(Filename));
229     return false;
230   }
231
232   NativeObjectPath = Filename.c_str();
233   *name = NativeObjectPath.c_str();
234   return true;
235 }
236
237 const void* LTOCodeGenerator::compile(size_t* length,
238                                       bool disableOpt,
239                                       bool disableInline,
240                                       bool disableGVNLoadPRE,
241                                       std::string& errMsg) {
242   const char *name;
243   if (!compile_to_file(&name, disableOpt, disableInline, disableGVNLoadPRE,
244                        errMsg))
245     return NULL;
246
247   // remove old buffer if compile() called twice
248   delete NativeObjectFile;
249
250   // read .o file into memory buffer
251   OwningPtr<MemoryBuffer> BuffPtr;
252   if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) {
253     errMsg = ec.message();
254     sys::fs::remove(NativeObjectPath);
255     return NULL;
256   }
257   NativeObjectFile = BuffPtr.take();
258
259   // remove temp files
260   sys::fs::remove(NativeObjectPath);
261
262   // return buffer, unless error
263   if (NativeObjectFile == NULL)
264     return NULL;
265   *length = NativeObjectFile->getBufferSize();
266   return NativeObjectFile->getBufferStart();
267 }
268
269 bool LTOCodeGenerator::determineTarget(std::string &errMsg) {
270   if (TargetMach != NULL)
271     return true;
272
273   std::string TripleStr = Linker.getModule()->getTargetTriple();
274   if (TripleStr.empty())
275     TripleStr = sys::getDefaultTargetTriple();
276   llvm::Triple Triple(TripleStr);
277
278   // create target machine from info for merged modules
279   const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
280   if (march == NULL)
281     return false;
282
283   // The relocation model is actually a static member of TargetMachine and
284   // needs to be set before the TargetMachine is instantiated.
285   Reloc::Model RelocModel = Reloc::Default;
286   switch (CodeModel) {
287   case LTO_CODEGEN_PIC_MODEL_STATIC:
288     RelocModel = Reloc::Static;
289     break;
290   case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
291     RelocModel = Reloc::PIC_;
292     break;
293   case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
294     RelocModel = Reloc::DynamicNoPIC;
295     break;
296   }
297
298   // construct LTOModule, hand over ownership of module and target
299   SubtargetFeatures Features;
300   Features.getDefaultSubtargetFeatures(Triple);
301   std::string FeatureStr = Features.getString();
302   // Set a default CPU for Darwin triples.
303   if (MCpu.empty() && Triple.isOSDarwin()) {
304     if (Triple.getArch() == llvm::Triple::x86_64)
305       MCpu = "core2";
306     else if (Triple.getArch() == llvm::Triple::x86)
307       MCpu = "yonah";
308   }
309
310   TargetMach = march->createTargetMachine(TripleStr, MCpu, FeatureStr, Options,
311                                           RelocModel, CodeModel::Default,
312                                           CodeGenOpt::Aggressive);
313   return true;
314 }
315
316 void LTOCodeGenerator::
317 applyRestriction(GlobalValue &GV,
318                  const ArrayRef<StringRef> &Libcalls,
319                  std::vector<const char*> &MustPreserveList,
320                  SmallPtrSet<GlobalValue*, 8> &AsmUsed,
321                  Mangler &Mangler) {
322   SmallString<64> Buffer;
323   Mangler.getNameWithPrefix(Buffer, &GV);
324
325   if (GV.isDeclaration())
326     return;
327   if (MustPreserveSymbols.count(Buffer))
328     MustPreserveList.push_back(GV.getName().data());
329   if (AsmUndefinedRefs.count(Buffer))
330     AsmUsed.insert(&GV);
331
332   // Conservatively append user-supplied runtime library functions to
333   // llvm.compiler.used.  These could be internalized and deleted by
334   // optimizations like -globalopt, causing problems when later optimizations
335   // add new library calls (e.g., llvm.memset => memset and printf => puts).
336   // Leave it to the linker to remove any dead code (e.g. with -dead_strip).
337   if (isa<Function>(GV) &&
338       std::binary_search(Libcalls.begin(), Libcalls.end(), GV.getName()))
339     AsmUsed.insert(&GV);
340 }
341
342 static void findUsedValues(GlobalVariable *LLVMUsed,
343                            SmallPtrSet<GlobalValue*, 8> &UsedValues) {
344   if (LLVMUsed == 0) return;
345
346   ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
347   for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
348     if (GlobalValue *GV =
349         dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
350       UsedValues.insert(GV);
351 }
352
353 static void accumulateAndSortLibcalls(std::vector<StringRef> &Libcalls,
354                                       const TargetLibraryInfo& TLI,
355                                       const TargetLowering *Lowering)
356 {
357   // TargetLibraryInfo has info on C runtime library calls on the current
358   // target.
359   for (unsigned I = 0, E = static_cast<unsigned>(LibFunc::NumLibFuncs);
360        I != E; ++I) {
361     LibFunc::Func F = static_cast<LibFunc::Func>(I);
362     if (TLI.has(F))
363       Libcalls.push_back(TLI.getName(F));
364   }
365
366   // TargetLowering has info on library calls that CodeGen expects to be
367   // available, both from the C runtime and compiler-rt.
368   if (Lowering)
369     for (unsigned I = 0, E = static_cast<unsigned>(RTLIB::UNKNOWN_LIBCALL);
370          I != E; ++I)
371       if (const char *Name
372           = Lowering->getLibcallName(static_cast<RTLIB::Libcall>(I)))
373         Libcalls.push_back(Name);
374
375   array_pod_sort(Libcalls.begin(), Libcalls.end());
376   Libcalls.erase(std::unique(Libcalls.begin(), Libcalls.end()),
377                  Libcalls.end());
378 }
379
380 void LTOCodeGenerator::applyScopeRestrictions() {
381   if (ScopeRestrictionsDone)
382     return;
383   Module *mergedModule = Linker.getModule();
384
385   // Start off with a verification pass.
386   PassManager passes;
387   passes.add(createVerifierPass());
388
389   // mark which symbols can not be internalized
390   Mangler Mangler(TargetMach->getDataLayout());
391   std::vector<const char*> MustPreserveList;
392   SmallPtrSet<GlobalValue*, 8> AsmUsed;
393   std::vector<StringRef> Libcalls;
394   TargetLibraryInfo TLI(Triple(TargetMach->getTargetTriple()));
395   accumulateAndSortLibcalls(Libcalls, TLI, TargetMach->getTargetLowering());
396
397   for (Module::iterator f = mergedModule->begin(),
398          e = mergedModule->end(); f != e; ++f)
399     applyRestriction(*f, Libcalls, MustPreserveList, AsmUsed, Mangler);
400   for (Module::global_iterator v = mergedModule->global_begin(),
401          e = mergedModule->global_end(); v !=  e; ++v)
402     applyRestriction(*v, Libcalls, MustPreserveList, AsmUsed, Mangler);
403   for (Module::alias_iterator a = mergedModule->alias_begin(),
404          e = mergedModule->alias_end(); a != e; ++a)
405     applyRestriction(*a, Libcalls, MustPreserveList, AsmUsed, Mangler);
406
407   GlobalVariable *LLVMCompilerUsed =
408     mergedModule->getGlobalVariable("llvm.compiler.used");
409   findUsedValues(LLVMCompilerUsed, AsmUsed);
410   if (LLVMCompilerUsed)
411     LLVMCompilerUsed->eraseFromParent();
412
413   if (!AsmUsed.empty()) {
414     llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(Context);
415     std::vector<Constant*> asmUsed2;
416     for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = AsmUsed.begin(),
417            e = AsmUsed.end(); i !=e; ++i) {
418       GlobalValue *GV = *i;
419       Constant *c = ConstantExpr::getBitCast(GV, i8PTy);
420       asmUsed2.push_back(c);
421     }
422
423     llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size());
424     LLVMCompilerUsed =
425       new llvm::GlobalVariable(*mergedModule, ATy, false,
426                                llvm::GlobalValue::AppendingLinkage,
427                                llvm::ConstantArray::get(ATy, asmUsed2),
428                                "llvm.compiler.used");
429
430     LLVMCompilerUsed->setSection("llvm.metadata");
431   }
432
433   passes.add(createInternalizePass(MustPreserveList));
434
435   // apply scope restrictions
436   passes.run(*mergedModule);
437
438   ScopeRestrictionsDone = true;
439 }
440
441 /// Optimize merged modules using various IPO passes
442 bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
443                                           bool DisableOpt,
444                                           bool DisableInline,
445                                           bool DisableGVNLoadPRE,
446                                           std::string &errMsg) {
447   if (!this->determineTarget(errMsg))
448     return false;
449
450   Module *mergedModule = Linker.getModule();
451
452   // Mark which symbols can not be internalized
453   this->applyScopeRestrictions();
454
455   // Instantiate the pass manager to organize the passes.
456   PassManager passes;
457
458   // Start off with a verification pass.
459   passes.add(createVerifierPass());
460
461   // Add an appropriate DataLayout instance for this module...
462   passes.add(new DataLayout(*TargetMach->getDataLayout()));
463
464   // Add appropriate TargetLibraryInfo for this module. 
465   passes.add(new TargetLibraryInfo(Triple(TargetMach->getTargetTriple())));
466
467   TargetMach->addAnalysisPasses(passes);
468
469   // Enabling internalize here would use its AllButMain variant. It
470   // keeps only main if it exists and does nothing for libraries. Instead
471   // we create the pass ourselves with the symbol list provided by the linker.
472   if (!DisableOpt)
473     PassManagerBuilder().populateLTOPassManager(passes,
474                                               /*Internalize=*/false,
475                                               !DisableInline,
476                                               DisableGVNLoadPRE);
477
478   // Make sure everything is still good.
479   passes.add(createVerifierPass());
480
481   PassManager codeGenPasses;
482
483   codeGenPasses.add(new DataLayout(*TargetMach->getDataLayout()));
484   TargetMach->addAnalysisPasses(codeGenPasses);
485
486   formatted_raw_ostream Out(out);
487
488   // If the bitcode files contain ARC code and were compiled with optimization,
489   // the ObjCARCContractPass must be run, so do it unconditionally here.
490   codeGenPasses.add(createObjCARCContractPass());
491
492   if (TargetMach->addPassesToEmitFile(codeGenPasses, Out,
493                                       TargetMachine::CGFT_ObjectFile)) {
494     errMsg = "target file type not supported";
495     return false;
496   }
497
498   // Run our queue of passes all at once now, efficiently.
499   passes.run(*mergedModule);
500
501   // Run the code generator, and write assembly file
502   codeGenPasses.run(*mergedModule);
503
504   return true;
505 }
506
507 /// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
508 /// LTO problems.
509 void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
510   for (std::pair<StringRef, StringRef> o = getToken(options);
511        !o.first.empty(); o = getToken(o.second)) {
512     // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
513     // that.
514     if (CodegenOptions.empty())
515       CodegenOptions.push_back(strdup("libLLVMLTO"));
516     CodegenOptions.push_back(strdup(o.first.str().c_str()));
517   }
518 }
519
520 void LTOCodeGenerator::parseCodeGenDebugOptions() {
521   // if options were requested, set them
522   if (!CodegenOptions.empty())
523     cl::ParseCommandLineOptions(CodegenOptions.size(),
524                                 const_cast<char **>(&CodegenOptions[0]));
525 }