1f785676fSDimitry Andric //===-LTOCodeGenerator.cpp - LLVM Link Time Optimizer ---------------------===//
2f785676fSDimitry Andric //
3f785676fSDimitry Andric //                     The LLVM Compiler Infrastructure
4f785676fSDimitry Andric //
5f785676fSDimitry Andric // This file is distributed under the University of Illinois Open Source
6f785676fSDimitry Andric // License. See LICENSE.TXT for details.
7f785676fSDimitry Andric //
8f785676fSDimitry Andric //===----------------------------------------------------------------------===//
9f785676fSDimitry Andric //
10f785676fSDimitry Andric // This file implements the Link Time Optimization library. This library is
11f785676fSDimitry Andric // intended to be used by linker to optimize code at link time.
12f785676fSDimitry Andric //
13f785676fSDimitry Andric //===----------------------------------------------------------------------===//
14f785676fSDimitry Andric 
153ca95b02SDimitry Andric #include "llvm/LTO/legacy/LTOCodeGenerator.h"
163ca95b02SDimitry Andric 
173ca95b02SDimitry Andric #include "llvm/ADT/Statistic.h"
18f785676fSDimitry Andric #include "llvm/ADT/StringExtras.h"
19f785676fSDimitry Andric #include "llvm/Analysis/Passes.h"
20ff0cc061SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
21ff0cc061SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
22d88c1a5aSDimitry Andric #include "llvm/Bitcode/BitcodeWriter.h"
237d523365SDimitry Andric #include "llvm/CodeGen/ParallelCG.h"
24f785676fSDimitry Andric #include "llvm/CodeGen/RuntimeLibcalls.h"
25f785676fSDimitry Andric #include "llvm/Config/config.h"
26f785676fSDimitry Andric #include "llvm/IR/Constants.h"
27f785676fSDimitry Andric #include "llvm/IR/DataLayout.h"
283ca95b02SDimitry Andric #include "llvm/IR/DebugInfo.h"
29f785676fSDimitry Andric #include "llvm/IR/DerivedTypes.h"
3091bc56edSDimitry Andric #include "llvm/IR/DiagnosticInfo.h"
3191bc56edSDimitry Andric #include "llvm/IR/DiagnosticPrinter.h"
32f785676fSDimitry Andric #include "llvm/IR/LLVMContext.h"
33ff0cc061SDimitry Andric #include "llvm/IR/LegacyPassManager.h"
3491bc56edSDimitry Andric #include "llvm/IR/Mangler.h"
35f785676fSDimitry Andric #include "llvm/IR/Module.h"
3691bc56edSDimitry Andric #include "llvm/IR/Verifier.h"
37f785676fSDimitry Andric #include "llvm/InitializePasses.h"
383ca95b02SDimitry Andric #include "llvm/LTO/legacy/LTOModule.h"
393ca95b02SDimitry Andric #include "llvm/LTO/legacy/UpdateCompilerUsed.h"
4091bc56edSDimitry Andric #include "llvm/Linker/Linker.h"
41f785676fSDimitry Andric #include "llvm/MC/MCAsmInfo.h"
42f785676fSDimitry Andric #include "llvm/MC/MCContext.h"
43f785676fSDimitry Andric #include "llvm/MC/SubtargetFeature.h"
44f785676fSDimitry Andric #include "llvm/Support/CommandLine.h"
45f785676fSDimitry Andric #include "llvm/Support/FileSystem.h"
46f785676fSDimitry Andric #include "llvm/Support/Host.h"
47f785676fSDimitry Andric #include "llvm/Support/MemoryBuffer.h"
48f785676fSDimitry Andric #include "llvm/Support/Signals.h"
49f785676fSDimitry Andric #include "llvm/Support/TargetRegistry.h"
50f785676fSDimitry Andric #include "llvm/Support/TargetSelect.h"
51f785676fSDimitry Andric #include "llvm/Support/ToolOutputFile.h"
52d88c1a5aSDimitry Andric #include "llvm/Support/YAMLTraits.h"
5391bc56edSDimitry Andric #include "llvm/Support/raw_ostream.h"
54f785676fSDimitry Andric #include "llvm/Target/TargetLowering.h"
55f785676fSDimitry Andric #include "llvm/Target/TargetOptions.h"
56f785676fSDimitry Andric #include "llvm/Target/TargetRegisterInfo.h"
5739d628a0SDimitry Andric #include "llvm/Target/TargetSubtargetInfo.h"
58f785676fSDimitry Andric #include "llvm/Transforms/IPO.h"
593ca95b02SDimitry Andric #include "llvm/Transforms/IPO/Internalize.h"
60f785676fSDimitry Andric #include "llvm/Transforms/IPO/PassManagerBuilder.h"
61f785676fSDimitry Andric #include "llvm/Transforms/ObjCARC.h"
62d88c1a5aSDimitry Andric #include "llvm/Transforms/Utils/ModuleUtils.h"
6391bc56edSDimitry Andric #include <system_error>
64f785676fSDimitry Andric using namespace llvm;
65f785676fSDimitry Andric 
66f785676fSDimitry Andric const char* LTOCodeGenerator::getVersionString() {
67f785676fSDimitry Andric #ifdef LLVM_VERSION_INFO
68f785676fSDimitry Andric   return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
69f785676fSDimitry Andric #else
70f785676fSDimitry Andric   return PACKAGE_NAME " version " PACKAGE_VERSION;
71f785676fSDimitry Andric #endif
72f785676fSDimitry Andric }
73f785676fSDimitry Andric 
743ca95b02SDimitry Andric namespace llvm {
753ca95b02SDimitry Andric cl::opt<bool> LTODiscardValueNames(
763ca95b02SDimitry Andric     "lto-discard-value-names",
773ca95b02SDimitry Andric     cl::desc("Strip names from Value during LTO (other than GlobalValue)."),
783ca95b02SDimitry Andric #ifdef NDEBUG
793ca95b02SDimitry Andric     cl::init(true),
803ca95b02SDimitry Andric #else
813ca95b02SDimitry Andric     cl::init(false),
823ca95b02SDimitry Andric #endif
833ca95b02SDimitry Andric     cl::Hidden);
843ca95b02SDimitry Andric 
853ca95b02SDimitry Andric cl::opt<bool> LTOStripInvalidDebugInfo(
863ca95b02SDimitry Andric     "lto-strip-invalid-debug-info",
873ca95b02SDimitry Andric     cl::desc("Strip invalid debug info metadata during LTO instead of aborting."),
883ca95b02SDimitry Andric #ifdef NDEBUG
893ca95b02SDimitry Andric     cl::init(true),
903ca95b02SDimitry Andric #else
913ca95b02SDimitry Andric     cl::init(false),
923ca95b02SDimitry Andric #endif
933ca95b02SDimitry Andric     cl::Hidden);
94d88c1a5aSDimitry Andric 
95d88c1a5aSDimitry Andric cl::opt<std::string>
96d88c1a5aSDimitry Andric     LTORemarksFilename("lto-pass-remarks-output",
97d88c1a5aSDimitry Andric                        cl::desc("Output filename for pass remarks"),
98d88c1a5aSDimitry Andric                        cl::value_desc("filename"));
99d88c1a5aSDimitry Andric 
100d88c1a5aSDimitry Andric cl::opt<bool> LTOPassRemarksWithHotness(
101d88c1a5aSDimitry Andric     "lto-pass-remarks-with-hotness",
102d88c1a5aSDimitry Andric     cl::desc("With PGO, include profile count in optimization remarks"),
103d88c1a5aSDimitry Andric     cl::Hidden);
1043ca95b02SDimitry Andric }
1053ca95b02SDimitry Andric 
1067d523365SDimitry Andric LTOCodeGenerator::LTOCodeGenerator(LLVMContext &Context)
1077d523365SDimitry Andric     : Context(Context), MergedModule(new Module("ld-temp.o", Context)),
1087d523365SDimitry Andric       TheLinker(new Linker(*MergedModule)) {
1093ca95b02SDimitry Andric   Context.setDiscardValueNames(LTODiscardValueNames);
1103ca95b02SDimitry Andric   Context.enableDebugTypeODRUniquing();
111ff0cc061SDimitry Andric   initializeLTOPasses();
11239d628a0SDimitry Andric }
11339d628a0SDimitry Andric 
1147d523365SDimitry Andric LTOCodeGenerator::~LTOCodeGenerator() {}
115f785676fSDimitry Andric 
1167d523365SDimitry Andric // Initialize LTO passes. Please keep this function in sync with
117f785676fSDimitry Andric // PassManagerBuilder::populateLTOPassManager(), and make sure all LTO
118f785676fSDimitry Andric // passes are initialized.
119f785676fSDimitry Andric void LTOCodeGenerator::initializeLTOPasses() {
120f785676fSDimitry Andric   PassRegistry &R = *PassRegistry::getPassRegistry();
121f785676fSDimitry Andric 
1223ca95b02SDimitry Andric   initializeInternalizeLegacyPassPass(R);
1233ca95b02SDimitry Andric   initializeIPSCCPLegacyPassPass(R);
1243ca95b02SDimitry Andric   initializeGlobalOptLegacyPassPass(R);
1253ca95b02SDimitry Andric   initializeConstantMergeLegacyPassPass(R);
126f785676fSDimitry Andric   initializeDAHPass(R);
127ff0cc061SDimitry Andric   initializeInstructionCombiningPassPass(R);
128f785676fSDimitry Andric   initializeSimpleInlinerPass(R);
129f785676fSDimitry Andric   initializePruneEHPass(R);
1303ca95b02SDimitry Andric   initializeGlobalDCELegacyPassPass(R);
131f785676fSDimitry Andric   initializeArgPromotionPass(R);
132f785676fSDimitry Andric   initializeJumpThreadingPass(R);
1337d523365SDimitry Andric   initializeSROALegacyPassPass(R);
1343ca95b02SDimitry Andric   initializePostOrderFunctionAttrsLegacyPassPass(R);
1353ca95b02SDimitry Andric   initializeReversePostOrderFunctionAttrsLegacyPassPass(R);
1367d523365SDimitry Andric   initializeGlobalsAAWrapperPassPass(R);
1373ca95b02SDimitry Andric   initializeLegacyLICMPassPass(R);
1383ca95b02SDimitry Andric   initializeMergedLoadStoreMotionLegacyPassPass(R);
1393ca95b02SDimitry Andric   initializeGVNLegacyPassPass(R);
1403ca95b02SDimitry Andric   initializeMemCpyOptLegacyPassPass(R);
1413ca95b02SDimitry Andric   initializeDCELegacyPassPass(R);
142f785676fSDimitry Andric   initializeCFGSimplifyPassPass(R);
143f785676fSDimitry Andric }
144f785676fSDimitry Andric 
145d88c1a5aSDimitry Andric void LTOCodeGenerator::setAsmUndefinedRefs(LTOModule *Mod) {
146d88c1a5aSDimitry Andric   const std::vector<StringRef> &undefs = Mod->getAsmUndefinedRefs();
147d88c1a5aSDimitry Andric   for (int i = 0, e = undefs.size(); i != e; ++i)
148d88c1a5aSDimitry Andric     AsmUndefinedRefs[undefs[i]] = 1;
149d88c1a5aSDimitry Andric }
150d88c1a5aSDimitry Andric 
1517d523365SDimitry Andric bool LTOCodeGenerator::addModule(LTOModule *Mod) {
1527d523365SDimitry Andric   assert(&Mod->getModule().getContext() == &Context &&
15339d628a0SDimitry Andric          "Expected module in same context");
15439d628a0SDimitry Andric 
1557d523365SDimitry Andric   bool ret = TheLinker->linkInModule(Mod->takeModule());
156d88c1a5aSDimitry Andric   setAsmUndefinedRefs(Mod);
157f785676fSDimitry Andric 
1583ca95b02SDimitry Andric   // We've just changed the input, so let's make sure we verify it.
1593ca95b02SDimitry Andric   HasVerifiedInput = false;
1603ca95b02SDimitry Andric 
161f785676fSDimitry Andric   return !ret;
162f785676fSDimitry Andric }
163f785676fSDimitry Andric 
1647d523365SDimitry Andric void LTOCodeGenerator::setModule(std::unique_ptr<LTOModule> Mod) {
165ff0cc061SDimitry Andric   assert(&Mod->getModule().getContext() == &Context &&
166ff0cc061SDimitry Andric          "Expected module in same context");
167ff0cc061SDimitry Andric 
168ff0cc061SDimitry Andric   AsmUndefinedRefs.clear();
169ff0cc061SDimitry Andric 
1707d523365SDimitry Andric   MergedModule = Mod->takeModule();
1717d523365SDimitry Andric   TheLinker = make_unique<Linker>(*MergedModule);
172d88c1a5aSDimitry Andric   setAsmUndefinedRefs(&*Mod);
1733ca95b02SDimitry Andric 
1743ca95b02SDimitry Andric   // We've just changed the input, so let's make sure we verify it.
1753ca95b02SDimitry Andric   HasVerifiedInput = false;
176ff0cc061SDimitry Andric }
177ff0cc061SDimitry Andric 
1783ca95b02SDimitry Andric void LTOCodeGenerator::setTargetOptions(const TargetOptions &Options) {
1797d523365SDimitry Andric   this->Options = Options;
180f785676fSDimitry Andric }
181f785676fSDimitry Andric 
1827d523365SDimitry Andric void LTOCodeGenerator::setDebugInfo(lto_debug_model Debug) {
1837d523365SDimitry Andric   switch (Debug) {
184f785676fSDimitry Andric   case LTO_DEBUG_MODEL_NONE:
185f785676fSDimitry Andric     EmitDwarfDebugInfo = false;
186f785676fSDimitry Andric     return;
187f785676fSDimitry Andric 
188f785676fSDimitry Andric   case LTO_DEBUG_MODEL_DWARF:
189f785676fSDimitry Andric     EmitDwarfDebugInfo = true;
190f785676fSDimitry Andric     return;
191f785676fSDimitry Andric   }
192f785676fSDimitry Andric   llvm_unreachable("Unknown debug format!");
193f785676fSDimitry Andric }
194f785676fSDimitry Andric 
1957d523365SDimitry Andric void LTOCodeGenerator::setOptLevel(unsigned Level) {
1967d523365SDimitry Andric   OptLevel = Level;
197ff0cc061SDimitry Andric   switch (OptLevel) {
198ff0cc061SDimitry Andric   case 0:
199ff0cc061SDimitry Andric     CGOptLevel = CodeGenOpt::None;
200d88c1a5aSDimitry Andric     return;
201ff0cc061SDimitry Andric   case 1:
202ff0cc061SDimitry Andric     CGOptLevel = CodeGenOpt::Less;
203d88c1a5aSDimitry Andric     return;
204ff0cc061SDimitry Andric   case 2:
205ff0cc061SDimitry Andric     CGOptLevel = CodeGenOpt::Default;
206d88c1a5aSDimitry Andric     return;
207ff0cc061SDimitry Andric   case 3:
208ff0cc061SDimitry Andric     CGOptLevel = CodeGenOpt::Aggressive;
209d88c1a5aSDimitry Andric     return;
210ff0cc061SDimitry Andric   }
211d88c1a5aSDimitry Andric   llvm_unreachable("Unknown optimization level!");
2127d523365SDimitry Andric }
213ff0cc061SDimitry Andric 
214d88c1a5aSDimitry Andric bool LTOCodeGenerator::writeMergedModules(StringRef Path) {
2157d523365SDimitry Andric   if (!determineTarget())
2167d523365SDimitry Andric     return false;
2177d523365SDimitry Andric 
2183ca95b02SDimitry Andric   // We always run the verifier once on the merged module.
2193ca95b02SDimitry Andric   verifyMergedModuleOnce();
2203ca95b02SDimitry Andric 
2217d523365SDimitry Andric   // mark which symbols can not be internalized
2227d523365SDimitry Andric   applyScopeRestrictions();
2237d523365SDimitry Andric 
2247d523365SDimitry Andric   // create output file
2257d523365SDimitry Andric   std::error_code EC;
2267d523365SDimitry Andric   tool_output_file Out(Path, EC, sys::fs::F_None);
2277d523365SDimitry Andric   if (EC) {
2287d523365SDimitry Andric     std::string ErrMsg = "could not open bitcode file for writing: ";
2297d523365SDimitry Andric     ErrMsg += Path;
2307d523365SDimitry Andric     emitError(ErrMsg);
2317d523365SDimitry Andric     return false;
2327d523365SDimitry Andric   }
2337d523365SDimitry Andric 
2347d523365SDimitry Andric   // write bitcode to it
2357d523365SDimitry Andric   WriteBitcodeToFile(MergedModule.get(), Out.os(), ShouldEmbedUselists);
2367d523365SDimitry Andric   Out.os().close();
2377d523365SDimitry Andric 
2387d523365SDimitry Andric   if (Out.os().has_error()) {
2397d523365SDimitry Andric     std::string ErrMsg = "could not write bitcode file: ";
2407d523365SDimitry Andric     ErrMsg += Path;
2417d523365SDimitry Andric     emitError(ErrMsg);
2427d523365SDimitry Andric     Out.os().clear_error();
2437d523365SDimitry Andric     return false;
2447d523365SDimitry Andric   }
2457d523365SDimitry Andric 
2467d523365SDimitry Andric   Out.keep();
2477d523365SDimitry Andric   return true;
2487d523365SDimitry Andric }
2497d523365SDimitry Andric 
2507d523365SDimitry Andric bool LTOCodeGenerator::compileOptimizedToFile(const char **Name) {
2517d523365SDimitry Andric   // make unique temp output file to put generated code
2527d523365SDimitry Andric   SmallString<128> Filename;
2537d523365SDimitry Andric   int FD;
2547d523365SDimitry Andric 
255d88c1a5aSDimitry Andric   StringRef Extension
2567d523365SDimitry Andric       (FileType == TargetMachine::CGFT_AssemblyFile ? "s" : "o");
2577d523365SDimitry Andric 
2587d523365SDimitry Andric   std::error_code EC =
2597d523365SDimitry Andric       sys::fs::createTemporaryFile("lto-llvm", Extension, FD, Filename);
2607d523365SDimitry Andric   if (EC) {
2617d523365SDimitry Andric     emitError(EC.message());
2627d523365SDimitry Andric     return false;
2637d523365SDimitry Andric   }
2647d523365SDimitry Andric 
2657d523365SDimitry Andric   // generate object file
266d88c1a5aSDimitry Andric   tool_output_file objFile(Filename, FD);
2677d523365SDimitry Andric 
2687d523365SDimitry Andric   bool genResult = compileOptimized(&objFile.os());
2697d523365SDimitry Andric   objFile.os().close();
2707d523365SDimitry Andric   if (objFile.os().has_error()) {
271d88c1a5aSDimitry Andric     emitError((Twine("could not write object file: ") + Filename).str());
2727d523365SDimitry Andric     objFile.os().clear_error();
2737d523365SDimitry Andric     sys::fs::remove(Twine(Filename));
2747d523365SDimitry Andric     return false;
2757d523365SDimitry Andric   }
2767d523365SDimitry Andric 
2777d523365SDimitry Andric   objFile.keep();
2787d523365SDimitry Andric   if (!genResult) {
2797d523365SDimitry Andric     sys::fs::remove(Twine(Filename));
2807d523365SDimitry Andric     return false;
2817d523365SDimitry Andric   }
2827d523365SDimitry Andric 
2837d523365SDimitry Andric   NativeObjectPath = Filename.c_str();
2847d523365SDimitry Andric   *Name = NativeObjectPath.c_str();
2857d523365SDimitry Andric   return true;
2867d523365SDimitry Andric }
2877d523365SDimitry Andric 
2887d523365SDimitry Andric std::unique_ptr<MemoryBuffer>
2897d523365SDimitry Andric LTOCodeGenerator::compileOptimized() {
2907d523365SDimitry Andric   const char *name;
2917d523365SDimitry Andric   if (!compileOptimizedToFile(&name))
2927d523365SDimitry Andric     return nullptr;
2937d523365SDimitry Andric 
2947d523365SDimitry Andric   // read .o file into memory buffer
2957d523365SDimitry Andric   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
2967d523365SDimitry Andric       MemoryBuffer::getFile(name, -1, false);
2977d523365SDimitry Andric   if (std::error_code EC = BufferOrErr.getError()) {
2987d523365SDimitry Andric     emitError(EC.message());
2997d523365SDimitry Andric     sys::fs::remove(NativeObjectPath);
3007d523365SDimitry Andric     return nullptr;
3017d523365SDimitry Andric   }
3027d523365SDimitry Andric 
3037d523365SDimitry Andric   // remove temp files
3047d523365SDimitry Andric   sys::fs::remove(NativeObjectPath);
3057d523365SDimitry Andric 
3067d523365SDimitry Andric   return std::move(*BufferOrErr);
3077d523365SDimitry Andric }
3087d523365SDimitry Andric 
3097d523365SDimitry Andric bool LTOCodeGenerator::compile_to_file(const char **Name, bool DisableVerify,
3107d523365SDimitry Andric                                        bool DisableInline,
3117d523365SDimitry Andric                                        bool DisableGVNLoadPRE,
3127d523365SDimitry Andric                                        bool DisableVectorization) {
3137d523365SDimitry Andric   if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
3147d523365SDimitry Andric                 DisableVectorization))
3157d523365SDimitry Andric     return false;
3167d523365SDimitry Andric 
3177d523365SDimitry Andric   return compileOptimizedToFile(Name);
3187d523365SDimitry Andric }
3197d523365SDimitry Andric 
3207d523365SDimitry Andric std::unique_ptr<MemoryBuffer>
3217d523365SDimitry Andric LTOCodeGenerator::compile(bool DisableVerify, bool DisableInline,
3227d523365SDimitry Andric                           bool DisableGVNLoadPRE, bool DisableVectorization) {
3237d523365SDimitry Andric   if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
3247d523365SDimitry Andric                 DisableVectorization))
3257d523365SDimitry Andric     return nullptr;
3267d523365SDimitry Andric 
3277d523365SDimitry Andric   return compileOptimized();
3287d523365SDimitry Andric }
3297d523365SDimitry Andric 
3307d523365SDimitry Andric bool LTOCodeGenerator::determineTarget() {
3317d523365SDimitry Andric   if (TargetMach)
3327d523365SDimitry Andric     return true;
3337d523365SDimitry Andric 
3343ca95b02SDimitry Andric   TripleStr = MergedModule->getTargetTriple();
3357d523365SDimitry Andric   if (TripleStr.empty()) {
3367d523365SDimitry Andric     TripleStr = sys::getDefaultTargetTriple();
3377d523365SDimitry Andric     MergedModule->setTargetTriple(TripleStr);
3387d523365SDimitry Andric   }
3397d523365SDimitry Andric   llvm::Triple Triple(TripleStr);
3407d523365SDimitry Andric 
3417d523365SDimitry Andric   // create target machine from info for merged modules
3427d523365SDimitry Andric   std::string ErrMsg;
3433ca95b02SDimitry Andric   MArch = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
3443ca95b02SDimitry Andric   if (!MArch) {
3457d523365SDimitry Andric     emitError(ErrMsg);
3467d523365SDimitry Andric     return false;
3477d523365SDimitry Andric   }
3487d523365SDimitry Andric 
3497d523365SDimitry Andric   // Construct LTOModule, hand over ownership of module and target. Use MAttr as
3507d523365SDimitry Andric   // the default set of features.
3517d523365SDimitry Andric   SubtargetFeatures Features(MAttr);
3527d523365SDimitry Andric   Features.getDefaultSubtargetFeatures(Triple);
3537d523365SDimitry Andric   FeatureStr = Features.getString();
3547d523365SDimitry Andric   // Set a default CPU for Darwin triples.
3557d523365SDimitry Andric   if (MCpu.empty() && Triple.isOSDarwin()) {
3567d523365SDimitry Andric     if (Triple.getArch() == llvm::Triple::x86_64)
3577d523365SDimitry Andric       MCpu = "core2";
3587d523365SDimitry Andric     else if (Triple.getArch() == llvm::Triple::x86)
3597d523365SDimitry Andric       MCpu = "yonah";
3607d523365SDimitry Andric     else if (Triple.getArch() == llvm::Triple::aarch64)
3617d523365SDimitry Andric       MCpu = "cyclone";
3627d523365SDimitry Andric   }
3637d523365SDimitry Andric 
3643ca95b02SDimitry Andric   TargetMach = createTargetMachine();
365f785676fSDimitry Andric   return true;
366f785676fSDimitry Andric }
367f785676fSDimitry Andric 
3683ca95b02SDimitry Andric std::unique_ptr<TargetMachine> LTOCodeGenerator::createTargetMachine() {
3693ca95b02SDimitry Andric   return std::unique_ptr<TargetMachine>(
3703ca95b02SDimitry Andric       MArch->createTargetMachine(TripleStr, MCpu, FeatureStr, Options,
3713ca95b02SDimitry Andric                                  RelocModel, CodeModel::Default, CGOptLevel));
372f785676fSDimitry Andric }
373f785676fSDimitry Andric 
3743ca95b02SDimitry Andric // If a linkonce global is present in the MustPreserveSymbols, we need to make
3753ca95b02SDimitry Andric // sure we honor this. To force the compiler to not drop it, we add it to the
3763ca95b02SDimitry Andric // "llvm.compiler.used" global.
3773ca95b02SDimitry Andric void LTOCodeGenerator::preserveDiscardableGVs(
3783ca95b02SDimitry Andric     Module &TheModule,
3793ca95b02SDimitry Andric     llvm::function_ref<bool(const GlobalValue &)> mustPreserveGV) {
380d88c1a5aSDimitry Andric   std::vector<GlobalValue *> Used;
3813ca95b02SDimitry Andric   auto mayPreserveGlobal = [&](GlobalValue &GV) {
382d88c1a5aSDimitry Andric     if (!GV.isDiscardableIfUnused() || GV.isDeclaration() ||
383d88c1a5aSDimitry Andric         !mustPreserveGV(GV))
3843ca95b02SDimitry Andric       return;
385d88c1a5aSDimitry Andric     if (GV.hasAvailableExternallyLinkage())
386d88c1a5aSDimitry Andric       return emitWarning(
3873ca95b02SDimitry Andric           (Twine("Linker asked to preserve available_externally global: '") +
3883ca95b02SDimitry Andric            GV.getName() + "'").str());
389d88c1a5aSDimitry Andric     if (GV.hasInternalLinkage())
390d88c1a5aSDimitry Andric       return emitWarning((Twine("Linker asked to preserve internal global: '") +
3913ca95b02SDimitry Andric                    GV.getName() + "'").str());
392d88c1a5aSDimitry Andric     Used.push_back(&GV);
3933ca95b02SDimitry Andric   };
3943ca95b02SDimitry Andric   for (auto &GV : TheModule)
3953ca95b02SDimitry Andric     mayPreserveGlobal(GV);
3963ca95b02SDimitry Andric   for (auto &GV : TheModule.globals())
3973ca95b02SDimitry Andric     mayPreserveGlobal(GV);
3983ca95b02SDimitry Andric   for (auto &GV : TheModule.aliases())
3993ca95b02SDimitry Andric     mayPreserveGlobal(GV);
400f785676fSDimitry Andric 
401d88c1a5aSDimitry Andric   if (Used.empty())
4023ca95b02SDimitry Andric     return;
4033ca95b02SDimitry Andric 
404d88c1a5aSDimitry Andric   appendToCompilerUsed(TheModule, Used);
405f785676fSDimitry Andric }
406f785676fSDimitry Andric 
407f785676fSDimitry Andric void LTOCodeGenerator::applyScopeRestrictions() {
4083ca95b02SDimitry Andric   if (ScopeRestrictionsDone)
409f785676fSDimitry Andric     return;
410f785676fSDimitry Andric 
4113ca95b02SDimitry Andric   // Declare a callback for the internalize pass that will ask for every
4123ca95b02SDimitry Andric   // candidate GlobalValue if it can be internalized or not.
413d88c1a5aSDimitry Andric   Mangler Mang;
4143ca95b02SDimitry Andric   SmallString<64> MangledName;
4153ca95b02SDimitry Andric   auto mustPreserveGV = [&](const GlobalValue &GV) -> bool {
4163ca95b02SDimitry Andric     // Unnamed globals can't be mangled, but they can't be preserved either.
4173ca95b02SDimitry Andric     if (!GV.hasName())
4183ca95b02SDimitry Andric       return false;
419f785676fSDimitry Andric 
4203ca95b02SDimitry Andric     // Need to mangle the GV as the "MustPreserveSymbols" StringSet is filled
4213ca95b02SDimitry Andric     // with the linker supplied name, which on Darwin includes a leading
4223ca95b02SDimitry Andric     // underscore.
4233ca95b02SDimitry Andric     MangledName.clear();
4243ca95b02SDimitry Andric     MangledName.reserve(GV.getName().size() + 1);
425d88c1a5aSDimitry Andric     Mang.getNameWithPrefix(MangledName, &GV, /*CannotUsePrivateLabel=*/false);
4263ca95b02SDimitry Andric     return MustPreserveSymbols.count(MangledName);
4273ca95b02SDimitry Andric   };
428ff0cc061SDimitry Andric 
4293ca95b02SDimitry Andric   // Preserve linkonce value on linker request
4303ca95b02SDimitry Andric   preserveDiscardableGVs(*MergedModule, mustPreserveGV);
431f785676fSDimitry Andric 
4323ca95b02SDimitry Andric   if (!ShouldInternalize)
4333ca95b02SDimitry Andric     return;
434f785676fSDimitry Andric 
4353ca95b02SDimitry Andric   if (ShouldRestoreGlobalsLinkage) {
4363ca95b02SDimitry Andric     // Record the linkage type of non-local symbols so they can be restored
4373ca95b02SDimitry Andric     // prior
4383ca95b02SDimitry Andric     // to module splitting.
4393ca95b02SDimitry Andric     auto RecordLinkage = [&](const GlobalValue &GV) {
4403ca95b02SDimitry Andric       if (!GV.hasAvailableExternallyLinkage() && !GV.hasLocalLinkage() &&
4413ca95b02SDimitry Andric           GV.hasName())
4423ca95b02SDimitry Andric         ExternalSymbols.insert(std::make_pair(GV.getName(), GV.getLinkage()));
4433ca95b02SDimitry Andric     };
4443ca95b02SDimitry Andric     for (auto &GV : *MergedModule)
4453ca95b02SDimitry Andric       RecordLinkage(GV);
4463ca95b02SDimitry Andric     for (auto &GV : MergedModule->globals())
4473ca95b02SDimitry Andric       RecordLinkage(GV);
4483ca95b02SDimitry Andric     for (auto &GV : MergedModule->aliases())
4493ca95b02SDimitry Andric       RecordLinkage(GV);
450f785676fSDimitry Andric   }
451f785676fSDimitry Andric 
4523ca95b02SDimitry Andric   // Update the llvm.compiler_used globals to force preserving libcalls and
4533ca95b02SDimitry Andric   // symbols referenced from asm
4543ca95b02SDimitry Andric   updateCompilerUsed(*MergedModule, *TargetMach, AsmUndefinedRefs);
455f785676fSDimitry Andric 
4563ca95b02SDimitry Andric   internalizeModule(*MergedModule, mustPreserveGV);
457f785676fSDimitry Andric 
458f785676fSDimitry Andric   ScopeRestrictionsDone = true;
459f785676fSDimitry Andric }
460f785676fSDimitry Andric 
4613ca95b02SDimitry Andric /// Restore original linkage for symbols that may have been internalized
4623ca95b02SDimitry Andric void LTOCodeGenerator::restoreLinkageForExternals() {
4633ca95b02SDimitry Andric   if (!ShouldInternalize || !ShouldRestoreGlobalsLinkage)
4643ca95b02SDimitry Andric     return;
4653ca95b02SDimitry Andric 
4663ca95b02SDimitry Andric   assert(ScopeRestrictionsDone &&
4673ca95b02SDimitry Andric          "Cannot externalize without internalization!");
4683ca95b02SDimitry Andric 
4693ca95b02SDimitry Andric   if (ExternalSymbols.empty())
4703ca95b02SDimitry Andric     return;
4713ca95b02SDimitry Andric 
4723ca95b02SDimitry Andric   auto externalize = [this](GlobalValue &GV) {
4733ca95b02SDimitry Andric     if (!GV.hasLocalLinkage() || !GV.hasName())
4743ca95b02SDimitry Andric       return;
4753ca95b02SDimitry Andric 
4763ca95b02SDimitry Andric     auto I = ExternalSymbols.find(GV.getName());
4773ca95b02SDimitry Andric     if (I == ExternalSymbols.end())
4783ca95b02SDimitry Andric       return;
4793ca95b02SDimitry Andric 
4803ca95b02SDimitry Andric     GV.setLinkage(I->second);
4813ca95b02SDimitry Andric   };
4823ca95b02SDimitry Andric 
4833ca95b02SDimitry Andric   std::for_each(MergedModule->begin(), MergedModule->end(), externalize);
4843ca95b02SDimitry Andric   std::for_each(MergedModule->global_begin(), MergedModule->global_end(),
4853ca95b02SDimitry Andric                 externalize);
4863ca95b02SDimitry Andric   std::for_each(MergedModule->alias_begin(), MergedModule->alias_end(),
4873ca95b02SDimitry Andric                 externalize);
4883ca95b02SDimitry Andric }
4893ca95b02SDimitry Andric 
4903ca95b02SDimitry Andric void LTOCodeGenerator::verifyMergedModuleOnce() {
4913ca95b02SDimitry Andric   // Only run on the first call.
4923ca95b02SDimitry Andric   if (HasVerifiedInput)
4933ca95b02SDimitry Andric     return;
4943ca95b02SDimitry Andric   HasVerifiedInput = true;
4953ca95b02SDimitry Andric 
4963ca95b02SDimitry Andric   if (LTOStripInvalidDebugInfo) {
4973ca95b02SDimitry Andric     bool BrokenDebugInfo = false;
4983ca95b02SDimitry Andric     if (verifyModule(*MergedModule, &dbgs(), &BrokenDebugInfo))
4993ca95b02SDimitry Andric       report_fatal_error("Broken module found, compilation aborted!");
5003ca95b02SDimitry Andric     if (BrokenDebugInfo) {
5013ca95b02SDimitry Andric       emitWarning("Invalid debug info found, debug info will be stripped");
5023ca95b02SDimitry Andric       StripDebugInfo(*MergedModule);
5033ca95b02SDimitry Andric     }
5043ca95b02SDimitry Andric   }
5053ca95b02SDimitry Andric   if (verifyModule(*MergedModule, &dbgs()))
5063ca95b02SDimitry Andric     report_fatal_error("Broken module found, compilation aborted!");
5073ca95b02SDimitry Andric }
5083ca95b02SDimitry Andric 
509d88c1a5aSDimitry Andric bool LTOCodeGenerator::setupOptimizationRemarks() {
510d88c1a5aSDimitry Andric   if (LTORemarksFilename != "") {
511d88c1a5aSDimitry Andric     std::error_code EC;
512d88c1a5aSDimitry Andric     DiagnosticOutputFile = llvm::make_unique<tool_output_file>(
513d88c1a5aSDimitry Andric         LTORemarksFilename, EC, sys::fs::F_None);
514d88c1a5aSDimitry Andric     if (EC) {
515d88c1a5aSDimitry Andric       emitError(EC.message());
516d88c1a5aSDimitry Andric       return false;
517d88c1a5aSDimitry Andric     }
518d88c1a5aSDimitry Andric     Context.setDiagnosticsOutputFile(
519d88c1a5aSDimitry Andric         llvm::make_unique<yaml::Output>(DiagnosticOutputFile->os()));
520d88c1a5aSDimitry Andric   }
521d88c1a5aSDimitry Andric 
522d88c1a5aSDimitry Andric   if (LTOPassRemarksWithHotness)
523d88c1a5aSDimitry Andric     Context.setDiagnosticHotnessRequested(true);
524d88c1a5aSDimitry Andric 
525d88c1a5aSDimitry Andric   return true;
526d88c1a5aSDimitry Andric }
527d88c1a5aSDimitry Andric 
528d88c1a5aSDimitry Andric void LTOCodeGenerator::finishOptimizationRemarks() {
529d88c1a5aSDimitry Andric   if (DiagnosticOutputFile) {
530d88c1a5aSDimitry Andric     DiagnosticOutputFile->keep();
531d88c1a5aSDimitry Andric     // FIXME: LTOCodeGenerator dtor is not invoked on Darwin
532d88c1a5aSDimitry Andric     DiagnosticOutputFile->os().flush();
533d88c1a5aSDimitry Andric   }
534d88c1a5aSDimitry Andric }
535d88c1a5aSDimitry Andric 
536f785676fSDimitry Andric /// Optimize merged modules using various IPO passes
5377d523365SDimitry Andric bool LTOCodeGenerator::optimize(bool DisableVerify, bool DisableInline,
538f785676fSDimitry Andric                                 bool DisableGVNLoadPRE,
5397d523365SDimitry Andric                                 bool DisableVectorization) {
5407d523365SDimitry Andric   if (!this->determineTarget())
541f785676fSDimitry Andric     return false;
542f785676fSDimitry Andric 
543d88c1a5aSDimitry Andric   if (!setupOptimizationRemarks())
544d88c1a5aSDimitry Andric     return false;
545d88c1a5aSDimitry Andric 
5463ca95b02SDimitry Andric   // We always run the verifier once on the merged module, the `DisableVerify`
5473ca95b02SDimitry Andric   // parameter only applies to subsequent verify.
5483ca95b02SDimitry Andric   verifyMergedModuleOnce();
5493ca95b02SDimitry Andric 
550f785676fSDimitry Andric   // Mark which symbols can not be internalized
551f785676fSDimitry Andric   this->applyScopeRestrictions();
552f785676fSDimitry Andric 
553f785676fSDimitry Andric   // Instantiate the pass manager to organize the passes.
554ff0cc061SDimitry Andric   legacy::PassManager passes;
555f785676fSDimitry Andric 
556f785676fSDimitry Andric   // Add an appropriate DataLayout instance for this module...
5577d523365SDimitry Andric   MergedModule->setDataLayout(TargetMach->createDataLayout());
558ff0cc061SDimitry Andric 
559ff0cc061SDimitry Andric   passes.add(
560ff0cc061SDimitry Andric       createTargetTransformInfoWrapperPass(TargetMach->getTargetIRAnalysis()));
56191bc56edSDimitry Andric 
56239d628a0SDimitry Andric   Triple TargetTriple(TargetMach->getTargetTriple());
56339d628a0SDimitry Andric   PassManagerBuilder PMB;
56439d628a0SDimitry Andric   PMB.DisableGVNLoadPRE = DisableGVNLoadPRE;
56539d628a0SDimitry Andric   PMB.LoopVectorize = !DisableVectorization;
56639d628a0SDimitry Andric   PMB.SLPVectorize = !DisableVectorization;
56739d628a0SDimitry Andric   if (!DisableInline)
56839d628a0SDimitry Andric     PMB.Inliner = createFunctionInliningPass();
569ff0cc061SDimitry Andric   PMB.LibraryInfo = new TargetLibraryInfoImpl(TargetTriple);
570ff0cc061SDimitry Andric   PMB.OptLevel = OptLevel;
5717d523365SDimitry Andric   PMB.VerifyInput = !DisableVerify;
5727d523365SDimitry Andric   PMB.VerifyOutput = !DisableVerify;
57391bc56edSDimitry Andric 
574ff0cc061SDimitry Andric   PMB.populateLTOPassManager(passes);
575f785676fSDimitry Andric 
576ff0cc061SDimitry Andric   // Run our queue of passes all at once now, efficiently.
5777d523365SDimitry Andric   passes.run(*MergedModule);
578f785676fSDimitry Andric 
579ff0cc061SDimitry Andric   return true;
580ff0cc061SDimitry Andric }
581f785676fSDimitry Andric 
5827d523365SDimitry Andric bool LTOCodeGenerator::compileOptimized(ArrayRef<raw_pwrite_stream *> Out) {
5837d523365SDimitry Andric   if (!this->determineTarget())
584ff0cc061SDimitry Andric     return false;
585ff0cc061SDimitry Andric 
5863ca95b02SDimitry Andric   // We always run the verifier once on the merged module.  If it has already
5873ca95b02SDimitry Andric   // been called in optimize(), this call will return early.
5883ca95b02SDimitry Andric   verifyMergedModuleOnce();
5893ca95b02SDimitry Andric 
5907d523365SDimitry Andric   legacy::PassManager preCodeGenPasses;
591f785676fSDimitry Andric 
592f785676fSDimitry Andric   // If the bitcode files contain ARC code and were compiled with optimization,
593f785676fSDimitry Andric   // the ObjCARCContractPass must be run, so do it unconditionally here.
5947d523365SDimitry Andric   preCodeGenPasses.add(createObjCARCContractPass());
5957d523365SDimitry Andric   preCodeGenPasses.run(*MergedModule);
596f785676fSDimitry Andric 
5973ca95b02SDimitry Andric   // Re-externalize globals that may have been internalized to increase scope
5983ca95b02SDimitry Andric   // for splitting
5993ca95b02SDimitry Andric   restoreLinkageForExternals();
6003ca95b02SDimitry Andric 
6017d523365SDimitry Andric   // Do code generation. We need to preserve the module in case the client calls
6027d523365SDimitry Andric   // writeMergedModules() after compilation, but we only need to allow this at
6037d523365SDimitry Andric   // parallelism level 1. This is achieved by having splitCodeGen return the
6047d523365SDimitry Andric   // original module at parallelism level 1 which we then assign back to
6057d523365SDimitry Andric   // MergedModule.
6063ca95b02SDimitry Andric   MergedModule = splitCodeGen(std::move(MergedModule), Out, {},
6073ca95b02SDimitry Andric                               [&]() { return createTargetMachine(); }, FileType,
6083ca95b02SDimitry Andric                               ShouldRestoreGlobalsLinkage);
6093ca95b02SDimitry Andric 
6103ca95b02SDimitry Andric   // If statistics were requested, print them out after codegen.
6113ca95b02SDimitry Andric   if (llvm::AreStatisticsEnabled())
6123ca95b02SDimitry Andric     llvm::PrintStatistics();
613f785676fSDimitry Andric 
614d88c1a5aSDimitry Andric   finishOptimizationRemarks();
615d88c1a5aSDimitry Andric 
616f785676fSDimitry Andric   return true;
617f785676fSDimitry Andric }
618f785676fSDimitry Andric 
619f785676fSDimitry Andric /// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
620f785676fSDimitry Andric /// LTO problems.
621d88c1a5aSDimitry Andric void LTOCodeGenerator::setCodeGenDebugOptions(StringRef Options) {
6227d523365SDimitry Andric   for (std::pair<StringRef, StringRef> o = getToken(Options); !o.first.empty();
6237d523365SDimitry Andric        o = getToken(o.second))
6247d523365SDimitry Andric     CodegenOptions.push_back(o.first);
625f785676fSDimitry Andric }
626f785676fSDimitry Andric 
627f785676fSDimitry Andric void LTOCodeGenerator::parseCodeGenDebugOptions() {
628f785676fSDimitry Andric   // if options were requested, set them
6297d523365SDimitry Andric   if (!CodegenOptions.empty()) {
6307d523365SDimitry Andric     // ParseCommandLineOptions() expects argv[0] to be program name.
6317d523365SDimitry Andric     std::vector<const char *> CodegenArgv(1, "libLLVMLTO");
6327d523365SDimitry Andric     for (std::string &Arg : CodegenOptions)
6337d523365SDimitry Andric       CodegenArgv.push_back(Arg.c_str());
6347d523365SDimitry Andric     cl::ParseCommandLineOptions(CodegenArgv.size(), CodegenArgv.data());
6357d523365SDimitry Andric   }
636f785676fSDimitry Andric }
63791bc56edSDimitry Andric 
63891bc56edSDimitry Andric void LTOCodeGenerator::DiagnosticHandler(const DiagnosticInfo &DI,
63991bc56edSDimitry Andric                                          void *Context) {
64091bc56edSDimitry Andric   ((LTOCodeGenerator *)Context)->DiagnosticHandler2(DI);
64191bc56edSDimitry Andric }
64291bc56edSDimitry Andric 
64391bc56edSDimitry Andric void LTOCodeGenerator::DiagnosticHandler2(const DiagnosticInfo &DI) {
64491bc56edSDimitry Andric   // Map the LLVM internal diagnostic severity to the LTO diagnostic severity.
64591bc56edSDimitry Andric   lto_codegen_diagnostic_severity_t Severity;
64691bc56edSDimitry Andric   switch (DI.getSeverity()) {
64791bc56edSDimitry Andric   case DS_Error:
64891bc56edSDimitry Andric     Severity = LTO_DS_ERROR;
64991bc56edSDimitry Andric     break;
65091bc56edSDimitry Andric   case DS_Warning:
65191bc56edSDimitry Andric     Severity = LTO_DS_WARNING;
65291bc56edSDimitry Andric     break;
65391bc56edSDimitry Andric   case DS_Remark:
65491bc56edSDimitry Andric     Severity = LTO_DS_REMARK;
65591bc56edSDimitry Andric     break;
65691bc56edSDimitry Andric   case DS_Note:
65791bc56edSDimitry Andric     Severity = LTO_DS_NOTE;
65891bc56edSDimitry Andric     break;
65991bc56edSDimitry Andric   }
66091bc56edSDimitry Andric   // Create the string that will be reported to the external diagnostic handler.
66191bc56edSDimitry Andric   std::string MsgStorage;
66291bc56edSDimitry Andric   raw_string_ostream Stream(MsgStorage);
66391bc56edSDimitry Andric   DiagnosticPrinterRawOStream DP(Stream);
66491bc56edSDimitry Andric   DI.print(DP);
66591bc56edSDimitry Andric   Stream.flush();
66691bc56edSDimitry Andric 
66791bc56edSDimitry Andric   // If this method has been called it means someone has set up an external
66891bc56edSDimitry Andric   // diagnostic handler. Assert on that.
66991bc56edSDimitry Andric   assert(DiagHandler && "Invalid diagnostic handler");
67091bc56edSDimitry Andric   (*DiagHandler)(Severity, MsgStorage.c_str(), DiagContext);
67191bc56edSDimitry Andric }
67291bc56edSDimitry Andric 
67391bc56edSDimitry Andric void
67491bc56edSDimitry Andric LTOCodeGenerator::setDiagnosticHandler(lto_diagnostic_handler_t DiagHandler,
67591bc56edSDimitry Andric                                        void *Ctxt) {
67691bc56edSDimitry Andric   this->DiagHandler = DiagHandler;
67791bc56edSDimitry Andric   this->DiagContext = Ctxt;
67891bc56edSDimitry Andric   if (!DiagHandler)
67991bc56edSDimitry Andric     return Context.setDiagnosticHandler(nullptr, nullptr);
68091bc56edSDimitry Andric   // Register the LTOCodeGenerator stub in the LLVMContext to forward the
68191bc56edSDimitry Andric   // diagnostic to the external DiagHandler.
68239d628a0SDimitry Andric   Context.setDiagnosticHandler(LTOCodeGenerator::DiagnosticHandler, this,
68339d628a0SDimitry Andric                                /* RespectFilters */ true);
68491bc56edSDimitry Andric }
6857d523365SDimitry Andric 
6867d523365SDimitry Andric namespace {
6877d523365SDimitry Andric class LTODiagnosticInfo : public DiagnosticInfo {
6887d523365SDimitry Andric   const Twine &Msg;
6897d523365SDimitry Andric public:
6907d523365SDimitry Andric   LTODiagnosticInfo(const Twine &DiagMsg, DiagnosticSeverity Severity=DS_Error)
6917d523365SDimitry Andric       : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {}
6927d523365SDimitry Andric   void print(DiagnosticPrinter &DP) const override { DP << Msg; }
6937d523365SDimitry Andric };
6947d523365SDimitry Andric }
6957d523365SDimitry Andric 
6967d523365SDimitry Andric void LTOCodeGenerator::emitError(const std::string &ErrMsg) {
6977d523365SDimitry Andric   if (DiagHandler)
6987d523365SDimitry Andric     (*DiagHandler)(LTO_DS_ERROR, ErrMsg.c_str(), DiagContext);
6997d523365SDimitry Andric   else
7007d523365SDimitry Andric     Context.diagnose(LTODiagnosticInfo(ErrMsg));
7017d523365SDimitry Andric }
7023ca95b02SDimitry Andric 
7033ca95b02SDimitry Andric void LTOCodeGenerator::emitWarning(const std::string &ErrMsg) {
7043ca95b02SDimitry Andric   if (DiagHandler)
7053ca95b02SDimitry Andric     (*DiagHandler)(LTO_DS_WARNING, ErrMsg.c_str(), DiagContext);
7063ca95b02SDimitry Andric   else
7073ca95b02SDimitry Andric     Context.diagnose(LTODiagnosticInfo(ErrMsg, DS_Warning));
7083ca95b02SDimitry Andric }
709