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"
387a7e6055SDimitry Andric #include "llvm/LTO/LTO.h"
393ca95b02SDimitry Andric #include "llvm/LTO/legacy/LTOModule.h"
403ca95b02SDimitry Andric #include "llvm/LTO/legacy/UpdateCompilerUsed.h"
4191bc56edSDimitry Andric #include "llvm/Linker/Linker.h"
42f785676fSDimitry Andric #include "llvm/MC/MCAsmInfo.h"
43f785676fSDimitry Andric #include "llvm/MC/MCContext.h"
44f785676fSDimitry Andric #include "llvm/MC/SubtargetFeature.h"
45f785676fSDimitry Andric #include "llvm/Support/CommandLine.h"
46f785676fSDimitry Andric #include "llvm/Support/FileSystem.h"
47f785676fSDimitry Andric #include "llvm/Support/Host.h"
48f785676fSDimitry Andric #include "llvm/Support/MemoryBuffer.h"
49f785676fSDimitry Andric #include "llvm/Support/Signals.h"
50f785676fSDimitry Andric #include "llvm/Support/TargetRegistry.h"
51f785676fSDimitry Andric #include "llvm/Support/TargetSelect.h"
52f785676fSDimitry Andric #include "llvm/Support/ToolOutputFile.h"
53d88c1a5aSDimitry Andric #include "llvm/Support/YAMLTraits.h"
5491bc56edSDimitry Andric #include "llvm/Support/raw_ostream.h"
55f785676fSDimitry Andric #include "llvm/Target/TargetLowering.h"
56f785676fSDimitry Andric #include "llvm/Target/TargetOptions.h"
57f785676fSDimitry Andric #include "llvm/Target/TargetRegisterInfo.h"
5839d628a0SDimitry Andric #include "llvm/Target/TargetSubtargetInfo.h"
59f785676fSDimitry Andric #include "llvm/Transforms/IPO.h"
603ca95b02SDimitry Andric #include "llvm/Transforms/IPO/Internalize.h"
61f785676fSDimitry Andric #include "llvm/Transforms/IPO/PassManagerBuilder.h"
62f785676fSDimitry Andric #include "llvm/Transforms/ObjCARC.h"
63d88c1a5aSDimitry Andric #include "llvm/Transforms/Utils/ModuleUtils.h"
6491bc56edSDimitry Andric #include <system_error>
65f785676fSDimitry Andric using namespace llvm;
66f785676fSDimitry Andric 
67f785676fSDimitry Andric const char* LTOCodeGenerator::getVersionString() {
68f785676fSDimitry Andric #ifdef LLVM_VERSION_INFO
69f785676fSDimitry Andric   return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
70f785676fSDimitry Andric #else
71f785676fSDimitry Andric   return PACKAGE_NAME " version " PACKAGE_VERSION;
72f785676fSDimitry Andric #endif
73f785676fSDimitry Andric }
74f785676fSDimitry Andric 
753ca95b02SDimitry Andric namespace llvm {
763ca95b02SDimitry Andric cl::opt<bool> LTODiscardValueNames(
773ca95b02SDimitry Andric     "lto-discard-value-names",
783ca95b02SDimitry Andric     cl::desc("Strip names from Value during LTO (other than GlobalValue)."),
793ca95b02SDimitry Andric #ifdef NDEBUG
803ca95b02SDimitry Andric     cl::init(true),
813ca95b02SDimitry Andric #else
823ca95b02SDimitry Andric     cl::init(false),
833ca95b02SDimitry Andric #endif
843ca95b02SDimitry Andric     cl::Hidden);
853ca95b02SDimitry Andric 
863ca95b02SDimitry Andric cl::opt<bool> LTOStripInvalidDebugInfo(
873ca95b02SDimitry Andric     "lto-strip-invalid-debug-info",
883ca95b02SDimitry Andric     cl::desc("Strip invalid debug info metadata during LTO instead of aborting."),
893ca95b02SDimitry Andric #ifdef NDEBUG
903ca95b02SDimitry Andric     cl::init(true),
913ca95b02SDimitry Andric #else
923ca95b02SDimitry Andric     cl::init(false),
933ca95b02SDimitry Andric #endif
943ca95b02SDimitry Andric     cl::Hidden);
95d88c1a5aSDimitry Andric 
96d88c1a5aSDimitry Andric cl::opt<std::string>
97d88c1a5aSDimitry Andric     LTORemarksFilename("lto-pass-remarks-output",
98d88c1a5aSDimitry Andric                        cl::desc("Output filename for pass remarks"),
99d88c1a5aSDimitry Andric                        cl::value_desc("filename"));
100d88c1a5aSDimitry Andric 
101d88c1a5aSDimitry Andric cl::opt<bool> LTOPassRemarksWithHotness(
102d88c1a5aSDimitry Andric     "lto-pass-remarks-with-hotness",
103d88c1a5aSDimitry Andric     cl::desc("With PGO, include profile count in optimization remarks"),
104d88c1a5aSDimitry Andric     cl::Hidden);
1053ca95b02SDimitry Andric }
1063ca95b02SDimitry Andric 
1077d523365SDimitry Andric LTOCodeGenerator::LTOCodeGenerator(LLVMContext &Context)
1087d523365SDimitry Andric     : Context(Context), MergedModule(new Module("ld-temp.o", Context)),
1097d523365SDimitry Andric       TheLinker(new Linker(*MergedModule)) {
1103ca95b02SDimitry Andric   Context.setDiscardValueNames(LTODiscardValueNames);
1113ca95b02SDimitry Andric   Context.enableDebugTypeODRUniquing();
112ff0cc061SDimitry Andric   initializeLTOPasses();
11339d628a0SDimitry Andric }
11439d628a0SDimitry Andric 
1157d523365SDimitry Andric LTOCodeGenerator::~LTOCodeGenerator() {}
116f785676fSDimitry Andric 
1177d523365SDimitry Andric // Initialize LTO passes. Please keep this function in sync with
118f785676fSDimitry Andric // PassManagerBuilder::populateLTOPassManager(), and make sure all LTO
119f785676fSDimitry Andric // passes are initialized.
120f785676fSDimitry Andric void LTOCodeGenerator::initializeLTOPasses() {
121f785676fSDimitry Andric   PassRegistry &R = *PassRegistry::getPassRegistry();
122f785676fSDimitry Andric 
1233ca95b02SDimitry Andric   initializeInternalizeLegacyPassPass(R);
1243ca95b02SDimitry Andric   initializeIPSCCPLegacyPassPass(R);
1253ca95b02SDimitry Andric   initializeGlobalOptLegacyPassPass(R);
1263ca95b02SDimitry Andric   initializeConstantMergeLegacyPassPass(R);
127f785676fSDimitry Andric   initializeDAHPass(R);
128ff0cc061SDimitry Andric   initializeInstructionCombiningPassPass(R);
129f785676fSDimitry Andric   initializeSimpleInlinerPass(R);
130f785676fSDimitry Andric   initializePruneEHPass(R);
1313ca95b02SDimitry Andric   initializeGlobalDCELegacyPassPass(R);
132f785676fSDimitry Andric   initializeArgPromotionPass(R);
133f785676fSDimitry Andric   initializeJumpThreadingPass(R);
1347d523365SDimitry Andric   initializeSROALegacyPassPass(R);
1353ca95b02SDimitry Andric   initializePostOrderFunctionAttrsLegacyPassPass(R);
1363ca95b02SDimitry Andric   initializeReversePostOrderFunctionAttrsLegacyPassPass(R);
1377d523365SDimitry Andric   initializeGlobalsAAWrapperPassPass(R);
1383ca95b02SDimitry Andric   initializeLegacyLICMPassPass(R);
1393ca95b02SDimitry Andric   initializeMergedLoadStoreMotionLegacyPassPass(R);
1403ca95b02SDimitry Andric   initializeGVNLegacyPassPass(R);
1413ca95b02SDimitry Andric   initializeMemCpyOptLegacyPassPass(R);
1423ca95b02SDimitry Andric   initializeDCELegacyPassPass(R);
143f785676fSDimitry Andric   initializeCFGSimplifyPassPass(R);
1447a7e6055SDimitry Andric   initializeLateCFGSimplifyPassPass(R);
145f785676fSDimitry Andric }
146f785676fSDimitry Andric 
147d88c1a5aSDimitry Andric void LTOCodeGenerator::setAsmUndefinedRefs(LTOModule *Mod) {
148d88c1a5aSDimitry Andric   const std::vector<StringRef> &undefs = Mod->getAsmUndefinedRefs();
149d88c1a5aSDimitry Andric   for (int i = 0, e = undefs.size(); i != e; ++i)
150d88c1a5aSDimitry Andric     AsmUndefinedRefs[undefs[i]] = 1;
151d88c1a5aSDimitry Andric }
152d88c1a5aSDimitry Andric 
1537d523365SDimitry Andric bool LTOCodeGenerator::addModule(LTOModule *Mod) {
1547d523365SDimitry Andric   assert(&Mod->getModule().getContext() == &Context &&
15539d628a0SDimitry Andric          "Expected module in same context");
15639d628a0SDimitry Andric 
1577d523365SDimitry Andric   bool ret = TheLinker->linkInModule(Mod->takeModule());
158d88c1a5aSDimitry Andric   setAsmUndefinedRefs(Mod);
159f785676fSDimitry Andric 
1603ca95b02SDimitry Andric   // We've just changed the input, so let's make sure we verify it.
1613ca95b02SDimitry Andric   HasVerifiedInput = false;
1623ca95b02SDimitry Andric 
163f785676fSDimitry Andric   return !ret;
164f785676fSDimitry Andric }
165f785676fSDimitry Andric 
1667d523365SDimitry Andric void LTOCodeGenerator::setModule(std::unique_ptr<LTOModule> Mod) {
167ff0cc061SDimitry Andric   assert(&Mod->getModule().getContext() == &Context &&
168ff0cc061SDimitry Andric          "Expected module in same context");
169ff0cc061SDimitry Andric 
170ff0cc061SDimitry Andric   AsmUndefinedRefs.clear();
171ff0cc061SDimitry Andric 
1727d523365SDimitry Andric   MergedModule = Mod->takeModule();
1737d523365SDimitry Andric   TheLinker = make_unique<Linker>(*MergedModule);
174d88c1a5aSDimitry Andric   setAsmUndefinedRefs(&*Mod);
1753ca95b02SDimitry Andric 
1763ca95b02SDimitry Andric   // We've just changed the input, so let's make sure we verify it.
1773ca95b02SDimitry Andric   HasVerifiedInput = false;
178ff0cc061SDimitry Andric }
179ff0cc061SDimitry Andric 
1803ca95b02SDimitry Andric void LTOCodeGenerator::setTargetOptions(const TargetOptions &Options) {
1817d523365SDimitry Andric   this->Options = Options;
182f785676fSDimitry Andric }
183f785676fSDimitry Andric 
1847d523365SDimitry Andric void LTOCodeGenerator::setDebugInfo(lto_debug_model Debug) {
1857d523365SDimitry Andric   switch (Debug) {
186f785676fSDimitry Andric   case LTO_DEBUG_MODEL_NONE:
187f785676fSDimitry Andric     EmitDwarfDebugInfo = false;
188f785676fSDimitry Andric     return;
189f785676fSDimitry Andric 
190f785676fSDimitry Andric   case LTO_DEBUG_MODEL_DWARF:
191f785676fSDimitry Andric     EmitDwarfDebugInfo = true;
192f785676fSDimitry Andric     return;
193f785676fSDimitry Andric   }
194f785676fSDimitry Andric   llvm_unreachable("Unknown debug format!");
195f785676fSDimitry Andric }
196f785676fSDimitry Andric 
1977d523365SDimitry Andric void LTOCodeGenerator::setOptLevel(unsigned Level) {
1987d523365SDimitry Andric   OptLevel = Level;
199ff0cc061SDimitry Andric   switch (OptLevel) {
200ff0cc061SDimitry Andric   case 0:
201ff0cc061SDimitry Andric     CGOptLevel = CodeGenOpt::None;
202d88c1a5aSDimitry Andric     return;
203ff0cc061SDimitry Andric   case 1:
204ff0cc061SDimitry Andric     CGOptLevel = CodeGenOpt::Less;
205d88c1a5aSDimitry Andric     return;
206ff0cc061SDimitry Andric   case 2:
207ff0cc061SDimitry Andric     CGOptLevel = CodeGenOpt::Default;
208d88c1a5aSDimitry Andric     return;
209ff0cc061SDimitry Andric   case 3:
210ff0cc061SDimitry Andric     CGOptLevel = CodeGenOpt::Aggressive;
211d88c1a5aSDimitry Andric     return;
212ff0cc061SDimitry Andric   }
213d88c1a5aSDimitry Andric   llvm_unreachable("Unknown optimization level!");
2147d523365SDimitry Andric }
215ff0cc061SDimitry Andric 
216d88c1a5aSDimitry Andric bool LTOCodeGenerator::writeMergedModules(StringRef Path) {
2177d523365SDimitry Andric   if (!determineTarget())
2187d523365SDimitry Andric     return false;
2197d523365SDimitry Andric 
2203ca95b02SDimitry Andric   // We always run the verifier once on the merged module.
2213ca95b02SDimitry Andric   verifyMergedModuleOnce();
2223ca95b02SDimitry Andric 
2237d523365SDimitry Andric   // mark which symbols can not be internalized
2247d523365SDimitry Andric   applyScopeRestrictions();
2257d523365SDimitry Andric 
2267d523365SDimitry Andric   // create output file
2277d523365SDimitry Andric   std::error_code EC;
2287d523365SDimitry Andric   tool_output_file Out(Path, EC, sys::fs::F_None);
2297d523365SDimitry Andric   if (EC) {
2307d523365SDimitry Andric     std::string ErrMsg = "could not open bitcode file for writing: ";
2317d523365SDimitry Andric     ErrMsg += Path;
2327d523365SDimitry Andric     emitError(ErrMsg);
2337d523365SDimitry Andric     return false;
2347d523365SDimitry Andric   }
2357d523365SDimitry Andric 
2367d523365SDimitry Andric   // write bitcode to it
2377d523365SDimitry Andric   WriteBitcodeToFile(MergedModule.get(), Out.os(), ShouldEmbedUselists);
2387d523365SDimitry Andric   Out.os().close();
2397d523365SDimitry Andric 
2407d523365SDimitry Andric   if (Out.os().has_error()) {
2417d523365SDimitry Andric     std::string ErrMsg = "could not write bitcode file: ";
2427d523365SDimitry Andric     ErrMsg += Path;
2437d523365SDimitry Andric     emitError(ErrMsg);
2447d523365SDimitry Andric     Out.os().clear_error();
2457d523365SDimitry Andric     return false;
2467d523365SDimitry Andric   }
2477d523365SDimitry Andric 
2487d523365SDimitry Andric   Out.keep();
2497d523365SDimitry Andric   return true;
2507d523365SDimitry Andric }
2517d523365SDimitry Andric 
2527d523365SDimitry Andric bool LTOCodeGenerator::compileOptimizedToFile(const char **Name) {
2537d523365SDimitry Andric   // make unique temp output file to put generated code
2547d523365SDimitry Andric   SmallString<128> Filename;
2557d523365SDimitry Andric   int FD;
2567d523365SDimitry Andric 
257d88c1a5aSDimitry Andric   StringRef Extension
2587d523365SDimitry Andric       (FileType == TargetMachine::CGFT_AssemblyFile ? "s" : "o");
2597d523365SDimitry Andric 
2607d523365SDimitry Andric   std::error_code EC =
2617d523365SDimitry Andric       sys::fs::createTemporaryFile("lto-llvm", Extension, FD, Filename);
2627d523365SDimitry Andric   if (EC) {
2637d523365SDimitry Andric     emitError(EC.message());
2647d523365SDimitry Andric     return false;
2657d523365SDimitry Andric   }
2667d523365SDimitry Andric 
2677d523365SDimitry Andric   // generate object file
268d88c1a5aSDimitry Andric   tool_output_file objFile(Filename, FD);
2697d523365SDimitry Andric 
2707d523365SDimitry Andric   bool genResult = compileOptimized(&objFile.os());
2717d523365SDimitry Andric   objFile.os().close();
2727d523365SDimitry Andric   if (objFile.os().has_error()) {
273d88c1a5aSDimitry Andric     emitError((Twine("could not write object file: ") + Filename).str());
2747d523365SDimitry Andric     objFile.os().clear_error();
2757d523365SDimitry Andric     sys::fs::remove(Twine(Filename));
2767d523365SDimitry Andric     return false;
2777d523365SDimitry Andric   }
2787d523365SDimitry Andric 
2797d523365SDimitry Andric   objFile.keep();
2807d523365SDimitry Andric   if (!genResult) {
2817d523365SDimitry Andric     sys::fs::remove(Twine(Filename));
2827d523365SDimitry Andric     return false;
2837d523365SDimitry Andric   }
2847d523365SDimitry Andric 
2857d523365SDimitry Andric   NativeObjectPath = Filename.c_str();
2867d523365SDimitry Andric   *Name = NativeObjectPath.c_str();
2877d523365SDimitry Andric   return true;
2887d523365SDimitry Andric }
2897d523365SDimitry Andric 
2907d523365SDimitry Andric std::unique_ptr<MemoryBuffer>
2917d523365SDimitry Andric LTOCodeGenerator::compileOptimized() {
2927d523365SDimitry Andric   const char *name;
2937d523365SDimitry Andric   if (!compileOptimizedToFile(&name))
2947d523365SDimitry Andric     return nullptr;
2957d523365SDimitry Andric 
2967d523365SDimitry Andric   // read .o file into memory buffer
2977d523365SDimitry Andric   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
2987d523365SDimitry Andric       MemoryBuffer::getFile(name, -1, false);
2997d523365SDimitry Andric   if (std::error_code EC = BufferOrErr.getError()) {
3007d523365SDimitry Andric     emitError(EC.message());
3017d523365SDimitry Andric     sys::fs::remove(NativeObjectPath);
3027d523365SDimitry Andric     return nullptr;
3037d523365SDimitry Andric   }
3047d523365SDimitry Andric 
3057d523365SDimitry Andric   // remove temp files
3067d523365SDimitry Andric   sys::fs::remove(NativeObjectPath);
3077d523365SDimitry Andric 
3087d523365SDimitry Andric   return std::move(*BufferOrErr);
3097d523365SDimitry Andric }
3107d523365SDimitry Andric 
3117d523365SDimitry Andric bool LTOCodeGenerator::compile_to_file(const char **Name, bool DisableVerify,
3127d523365SDimitry Andric                                        bool DisableInline,
3137d523365SDimitry Andric                                        bool DisableGVNLoadPRE,
3147d523365SDimitry Andric                                        bool DisableVectorization) {
3157d523365SDimitry Andric   if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
3167d523365SDimitry Andric                 DisableVectorization))
3177d523365SDimitry Andric     return false;
3187d523365SDimitry Andric 
3197d523365SDimitry Andric   return compileOptimizedToFile(Name);
3207d523365SDimitry Andric }
3217d523365SDimitry Andric 
3227d523365SDimitry Andric std::unique_ptr<MemoryBuffer>
3237d523365SDimitry Andric LTOCodeGenerator::compile(bool DisableVerify, bool DisableInline,
3247d523365SDimitry Andric                           bool DisableGVNLoadPRE, bool DisableVectorization) {
3257d523365SDimitry Andric   if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
3267d523365SDimitry Andric                 DisableVectorization))
3277d523365SDimitry Andric     return nullptr;
3287d523365SDimitry Andric 
3297d523365SDimitry Andric   return compileOptimized();
3307d523365SDimitry Andric }
3317d523365SDimitry Andric 
3327d523365SDimitry Andric bool LTOCodeGenerator::determineTarget() {
3337d523365SDimitry Andric   if (TargetMach)
3347d523365SDimitry Andric     return true;
3357d523365SDimitry Andric 
3363ca95b02SDimitry Andric   TripleStr = MergedModule->getTargetTriple();
3377d523365SDimitry Andric   if (TripleStr.empty()) {
3387d523365SDimitry Andric     TripleStr = sys::getDefaultTargetTriple();
3397d523365SDimitry Andric     MergedModule->setTargetTriple(TripleStr);
3407d523365SDimitry Andric   }
3417d523365SDimitry Andric   llvm::Triple Triple(TripleStr);
3427d523365SDimitry Andric 
3437d523365SDimitry Andric   // create target machine from info for merged modules
3447d523365SDimitry Andric   std::string ErrMsg;
3453ca95b02SDimitry Andric   MArch = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
3463ca95b02SDimitry Andric   if (!MArch) {
3477d523365SDimitry Andric     emitError(ErrMsg);
3487d523365SDimitry Andric     return false;
3497d523365SDimitry Andric   }
3507d523365SDimitry Andric 
3517d523365SDimitry Andric   // Construct LTOModule, hand over ownership of module and target. Use MAttr as
3527d523365SDimitry Andric   // the default set of features.
3537d523365SDimitry Andric   SubtargetFeatures Features(MAttr);
3547d523365SDimitry Andric   Features.getDefaultSubtargetFeatures(Triple);
3557d523365SDimitry Andric   FeatureStr = Features.getString();
3567d523365SDimitry Andric   // Set a default CPU for Darwin triples.
3577d523365SDimitry Andric   if (MCpu.empty() && Triple.isOSDarwin()) {
3587d523365SDimitry Andric     if (Triple.getArch() == llvm::Triple::x86_64)
3597d523365SDimitry Andric       MCpu = "core2";
3607d523365SDimitry Andric     else if (Triple.getArch() == llvm::Triple::x86)
3617d523365SDimitry Andric       MCpu = "yonah";
3627d523365SDimitry Andric     else if (Triple.getArch() == llvm::Triple::aarch64)
3637d523365SDimitry Andric       MCpu = "cyclone";
3647d523365SDimitry Andric   }
3657d523365SDimitry Andric 
3663ca95b02SDimitry Andric   TargetMach = createTargetMachine();
367f785676fSDimitry Andric   return true;
368f785676fSDimitry Andric }
369f785676fSDimitry Andric 
3703ca95b02SDimitry Andric std::unique_ptr<TargetMachine> LTOCodeGenerator::createTargetMachine() {
3713ca95b02SDimitry Andric   return std::unique_ptr<TargetMachine>(
3723ca95b02SDimitry Andric       MArch->createTargetMachine(TripleStr, MCpu, FeatureStr, Options,
3733ca95b02SDimitry Andric                                  RelocModel, CodeModel::Default, CGOptLevel));
374f785676fSDimitry Andric }
375f785676fSDimitry Andric 
3763ca95b02SDimitry Andric // If a linkonce global is present in the MustPreserveSymbols, we need to make
3773ca95b02SDimitry Andric // sure we honor this. To force the compiler to not drop it, we add it to the
3783ca95b02SDimitry Andric // "llvm.compiler.used" global.
3793ca95b02SDimitry Andric void LTOCodeGenerator::preserveDiscardableGVs(
3803ca95b02SDimitry Andric     Module &TheModule,
3813ca95b02SDimitry Andric     llvm::function_ref<bool(const GlobalValue &)> mustPreserveGV) {
382d88c1a5aSDimitry Andric   std::vector<GlobalValue *> Used;
3833ca95b02SDimitry Andric   auto mayPreserveGlobal = [&](GlobalValue &GV) {
384d88c1a5aSDimitry Andric     if (!GV.isDiscardableIfUnused() || GV.isDeclaration() ||
385d88c1a5aSDimitry Andric         !mustPreserveGV(GV))
3863ca95b02SDimitry Andric       return;
387d88c1a5aSDimitry Andric     if (GV.hasAvailableExternallyLinkage())
388d88c1a5aSDimitry Andric       return emitWarning(
3893ca95b02SDimitry Andric           (Twine("Linker asked to preserve available_externally global: '") +
3903ca95b02SDimitry Andric            GV.getName() + "'").str());
391d88c1a5aSDimitry Andric     if (GV.hasInternalLinkage())
392d88c1a5aSDimitry Andric       return emitWarning((Twine("Linker asked to preserve internal global: '") +
3933ca95b02SDimitry Andric                    GV.getName() + "'").str());
394d88c1a5aSDimitry Andric     Used.push_back(&GV);
3953ca95b02SDimitry Andric   };
3963ca95b02SDimitry Andric   for (auto &GV : TheModule)
3973ca95b02SDimitry Andric     mayPreserveGlobal(GV);
3983ca95b02SDimitry Andric   for (auto &GV : TheModule.globals())
3993ca95b02SDimitry Andric     mayPreserveGlobal(GV);
4003ca95b02SDimitry Andric   for (auto &GV : TheModule.aliases())
4013ca95b02SDimitry Andric     mayPreserveGlobal(GV);
402f785676fSDimitry Andric 
403d88c1a5aSDimitry Andric   if (Used.empty())
4043ca95b02SDimitry Andric     return;
4053ca95b02SDimitry Andric 
406d88c1a5aSDimitry Andric   appendToCompilerUsed(TheModule, Used);
407f785676fSDimitry Andric }
408f785676fSDimitry Andric 
409f785676fSDimitry Andric void LTOCodeGenerator::applyScopeRestrictions() {
4103ca95b02SDimitry Andric   if (ScopeRestrictionsDone)
411f785676fSDimitry Andric     return;
412f785676fSDimitry Andric 
4133ca95b02SDimitry Andric   // Declare a callback for the internalize pass that will ask for every
4143ca95b02SDimitry Andric   // candidate GlobalValue if it can be internalized or not.
415d88c1a5aSDimitry Andric   Mangler Mang;
4163ca95b02SDimitry Andric   SmallString<64> MangledName;
4173ca95b02SDimitry Andric   auto mustPreserveGV = [&](const GlobalValue &GV) -> bool {
4183ca95b02SDimitry Andric     // Unnamed globals can't be mangled, but they can't be preserved either.
4193ca95b02SDimitry Andric     if (!GV.hasName())
4203ca95b02SDimitry Andric       return false;
421f785676fSDimitry Andric 
4223ca95b02SDimitry Andric     // Need to mangle the GV as the "MustPreserveSymbols" StringSet is filled
4233ca95b02SDimitry Andric     // with the linker supplied name, which on Darwin includes a leading
4243ca95b02SDimitry Andric     // underscore.
4253ca95b02SDimitry Andric     MangledName.clear();
4263ca95b02SDimitry Andric     MangledName.reserve(GV.getName().size() + 1);
427d88c1a5aSDimitry Andric     Mang.getNameWithPrefix(MangledName, &GV, /*CannotUsePrivateLabel=*/false);
4283ca95b02SDimitry Andric     return MustPreserveSymbols.count(MangledName);
4293ca95b02SDimitry Andric   };
430ff0cc061SDimitry Andric 
4313ca95b02SDimitry Andric   // Preserve linkonce value on linker request
4323ca95b02SDimitry Andric   preserveDiscardableGVs(*MergedModule, mustPreserveGV);
433f785676fSDimitry Andric 
4343ca95b02SDimitry Andric   if (!ShouldInternalize)
4353ca95b02SDimitry Andric     return;
436f785676fSDimitry Andric 
4373ca95b02SDimitry Andric   if (ShouldRestoreGlobalsLinkage) {
4383ca95b02SDimitry Andric     // Record the linkage type of non-local symbols so they can be restored
4393ca95b02SDimitry Andric     // prior
4403ca95b02SDimitry Andric     // to module splitting.
4413ca95b02SDimitry Andric     auto RecordLinkage = [&](const GlobalValue &GV) {
4423ca95b02SDimitry Andric       if (!GV.hasAvailableExternallyLinkage() && !GV.hasLocalLinkage() &&
4433ca95b02SDimitry Andric           GV.hasName())
4443ca95b02SDimitry Andric         ExternalSymbols.insert(std::make_pair(GV.getName(), GV.getLinkage()));
4453ca95b02SDimitry Andric     };
4463ca95b02SDimitry Andric     for (auto &GV : *MergedModule)
4473ca95b02SDimitry Andric       RecordLinkage(GV);
4483ca95b02SDimitry Andric     for (auto &GV : MergedModule->globals())
4493ca95b02SDimitry Andric       RecordLinkage(GV);
4503ca95b02SDimitry Andric     for (auto &GV : MergedModule->aliases())
4513ca95b02SDimitry Andric       RecordLinkage(GV);
452f785676fSDimitry Andric   }
453f785676fSDimitry Andric 
4543ca95b02SDimitry Andric   // Update the llvm.compiler_used globals to force preserving libcalls and
4553ca95b02SDimitry Andric   // symbols referenced from asm
4563ca95b02SDimitry Andric   updateCompilerUsed(*MergedModule, *TargetMach, AsmUndefinedRefs);
457f785676fSDimitry Andric 
4583ca95b02SDimitry Andric   internalizeModule(*MergedModule, mustPreserveGV);
459f785676fSDimitry Andric 
460f785676fSDimitry Andric   ScopeRestrictionsDone = true;
461f785676fSDimitry Andric }
462f785676fSDimitry Andric 
4633ca95b02SDimitry Andric /// Restore original linkage for symbols that may have been internalized
4643ca95b02SDimitry Andric void LTOCodeGenerator::restoreLinkageForExternals() {
4653ca95b02SDimitry Andric   if (!ShouldInternalize || !ShouldRestoreGlobalsLinkage)
4663ca95b02SDimitry Andric     return;
4673ca95b02SDimitry Andric 
4683ca95b02SDimitry Andric   assert(ScopeRestrictionsDone &&
4693ca95b02SDimitry Andric          "Cannot externalize without internalization!");
4703ca95b02SDimitry Andric 
4713ca95b02SDimitry Andric   if (ExternalSymbols.empty())
4723ca95b02SDimitry Andric     return;
4733ca95b02SDimitry Andric 
4743ca95b02SDimitry Andric   auto externalize = [this](GlobalValue &GV) {
4753ca95b02SDimitry Andric     if (!GV.hasLocalLinkage() || !GV.hasName())
4763ca95b02SDimitry Andric       return;
4773ca95b02SDimitry Andric 
4783ca95b02SDimitry Andric     auto I = ExternalSymbols.find(GV.getName());
4793ca95b02SDimitry Andric     if (I == ExternalSymbols.end())
4803ca95b02SDimitry Andric       return;
4813ca95b02SDimitry Andric 
4823ca95b02SDimitry Andric     GV.setLinkage(I->second);
4833ca95b02SDimitry Andric   };
4843ca95b02SDimitry Andric 
4853ca95b02SDimitry Andric   std::for_each(MergedModule->begin(), MergedModule->end(), externalize);
4863ca95b02SDimitry Andric   std::for_each(MergedModule->global_begin(), MergedModule->global_end(),
4873ca95b02SDimitry Andric                 externalize);
4883ca95b02SDimitry Andric   std::for_each(MergedModule->alias_begin(), MergedModule->alias_end(),
4893ca95b02SDimitry Andric                 externalize);
4903ca95b02SDimitry Andric }
4913ca95b02SDimitry Andric 
4923ca95b02SDimitry Andric void LTOCodeGenerator::verifyMergedModuleOnce() {
4933ca95b02SDimitry Andric   // Only run on the first call.
4943ca95b02SDimitry Andric   if (HasVerifiedInput)
4953ca95b02SDimitry Andric     return;
4963ca95b02SDimitry Andric   HasVerifiedInput = true;
4973ca95b02SDimitry Andric 
4983ca95b02SDimitry Andric   if (LTOStripInvalidDebugInfo) {
4993ca95b02SDimitry Andric     bool BrokenDebugInfo = false;
5003ca95b02SDimitry Andric     if (verifyModule(*MergedModule, &dbgs(), &BrokenDebugInfo))
5013ca95b02SDimitry Andric       report_fatal_error("Broken module found, compilation aborted!");
5023ca95b02SDimitry Andric     if (BrokenDebugInfo) {
5033ca95b02SDimitry Andric       emitWarning("Invalid debug info found, debug info will be stripped");
5043ca95b02SDimitry Andric       StripDebugInfo(*MergedModule);
5053ca95b02SDimitry Andric     }
5063ca95b02SDimitry Andric   }
5073ca95b02SDimitry Andric   if (verifyModule(*MergedModule, &dbgs()))
5083ca95b02SDimitry Andric     report_fatal_error("Broken module found, compilation aborted!");
5093ca95b02SDimitry Andric }
5103ca95b02SDimitry Andric 
511d88c1a5aSDimitry Andric void LTOCodeGenerator::finishOptimizationRemarks() {
512d88c1a5aSDimitry Andric   if (DiagnosticOutputFile) {
513d88c1a5aSDimitry Andric     DiagnosticOutputFile->keep();
514d88c1a5aSDimitry Andric     // FIXME: LTOCodeGenerator dtor is not invoked on Darwin
515d88c1a5aSDimitry Andric     DiagnosticOutputFile->os().flush();
516d88c1a5aSDimitry Andric   }
517d88c1a5aSDimitry Andric }
518d88c1a5aSDimitry Andric 
519f785676fSDimitry Andric /// Optimize merged modules using various IPO passes
5207d523365SDimitry Andric bool LTOCodeGenerator::optimize(bool DisableVerify, bool DisableInline,
521f785676fSDimitry Andric                                 bool DisableGVNLoadPRE,
5227d523365SDimitry Andric                                 bool DisableVectorization) {
5237d523365SDimitry Andric   if (!this->determineTarget())
524f785676fSDimitry Andric     return false;
525f785676fSDimitry Andric 
5267a7e6055SDimitry Andric   auto DiagFileOrErr = lto::setupOptimizationRemarks(
5277a7e6055SDimitry Andric       Context, LTORemarksFilename, LTOPassRemarksWithHotness);
5287a7e6055SDimitry Andric   if (!DiagFileOrErr) {
5297a7e6055SDimitry Andric     errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";
5307a7e6055SDimitry Andric     report_fatal_error("Can't get an output file for the remarks");
5317a7e6055SDimitry Andric   }
5327a7e6055SDimitry Andric   DiagnosticOutputFile = std::move(*DiagFileOrErr);
533d88c1a5aSDimitry Andric 
5343ca95b02SDimitry Andric   // We always run the verifier once on the merged module, the `DisableVerify`
5353ca95b02SDimitry Andric   // parameter only applies to subsequent verify.
5363ca95b02SDimitry Andric   verifyMergedModuleOnce();
5373ca95b02SDimitry Andric 
538f785676fSDimitry Andric   // Mark which symbols can not be internalized
539f785676fSDimitry Andric   this->applyScopeRestrictions();
540f785676fSDimitry Andric 
541f785676fSDimitry Andric   // Instantiate the pass manager to organize the passes.
542ff0cc061SDimitry Andric   legacy::PassManager passes;
543f785676fSDimitry Andric 
544f785676fSDimitry Andric   // Add an appropriate DataLayout instance for this module...
5457d523365SDimitry Andric   MergedModule->setDataLayout(TargetMach->createDataLayout());
546ff0cc061SDimitry Andric 
547ff0cc061SDimitry Andric   passes.add(
548ff0cc061SDimitry Andric       createTargetTransformInfoWrapperPass(TargetMach->getTargetIRAnalysis()));
54991bc56edSDimitry Andric 
55039d628a0SDimitry Andric   Triple TargetTriple(TargetMach->getTargetTriple());
55139d628a0SDimitry Andric   PassManagerBuilder PMB;
55239d628a0SDimitry Andric   PMB.DisableGVNLoadPRE = DisableGVNLoadPRE;
55339d628a0SDimitry Andric   PMB.LoopVectorize = !DisableVectorization;
55439d628a0SDimitry Andric   PMB.SLPVectorize = !DisableVectorization;
55539d628a0SDimitry Andric   if (!DisableInline)
55639d628a0SDimitry Andric     PMB.Inliner = createFunctionInliningPass();
557ff0cc061SDimitry Andric   PMB.LibraryInfo = new TargetLibraryInfoImpl(TargetTriple);
5587a7e6055SDimitry Andric   if (Freestanding)
5597a7e6055SDimitry Andric     PMB.LibraryInfo->disableAllFunctions();
560ff0cc061SDimitry Andric   PMB.OptLevel = OptLevel;
5617d523365SDimitry Andric   PMB.VerifyInput = !DisableVerify;
5627d523365SDimitry Andric   PMB.VerifyOutput = !DisableVerify;
56391bc56edSDimitry Andric 
564ff0cc061SDimitry Andric   PMB.populateLTOPassManager(passes);
565f785676fSDimitry Andric 
566ff0cc061SDimitry Andric   // Run our queue of passes all at once now, efficiently.
5677d523365SDimitry Andric   passes.run(*MergedModule);
568f785676fSDimitry Andric 
569ff0cc061SDimitry Andric   return true;
570ff0cc061SDimitry Andric }
571f785676fSDimitry Andric 
5727d523365SDimitry Andric bool LTOCodeGenerator::compileOptimized(ArrayRef<raw_pwrite_stream *> Out) {
5737d523365SDimitry Andric   if (!this->determineTarget())
574ff0cc061SDimitry Andric     return false;
575ff0cc061SDimitry Andric 
5763ca95b02SDimitry Andric   // We always run the verifier once on the merged module.  If it has already
5773ca95b02SDimitry Andric   // been called in optimize(), this call will return early.
5783ca95b02SDimitry Andric   verifyMergedModuleOnce();
5793ca95b02SDimitry Andric 
5807d523365SDimitry Andric   legacy::PassManager preCodeGenPasses;
581f785676fSDimitry Andric 
582f785676fSDimitry Andric   // If the bitcode files contain ARC code and were compiled with optimization,
583f785676fSDimitry Andric   // the ObjCARCContractPass must be run, so do it unconditionally here.
5847d523365SDimitry Andric   preCodeGenPasses.add(createObjCARCContractPass());
5857d523365SDimitry Andric   preCodeGenPasses.run(*MergedModule);
586f785676fSDimitry Andric 
5873ca95b02SDimitry Andric   // Re-externalize globals that may have been internalized to increase scope
5883ca95b02SDimitry Andric   // for splitting
5893ca95b02SDimitry Andric   restoreLinkageForExternals();
5903ca95b02SDimitry Andric 
5917d523365SDimitry Andric   // Do code generation. We need to preserve the module in case the client calls
5927d523365SDimitry Andric   // writeMergedModules() after compilation, but we only need to allow this at
5937d523365SDimitry Andric   // parallelism level 1. This is achieved by having splitCodeGen return the
5947d523365SDimitry Andric   // original module at parallelism level 1 which we then assign back to
5957d523365SDimitry Andric   // MergedModule.
5963ca95b02SDimitry Andric   MergedModule = splitCodeGen(std::move(MergedModule), Out, {},
5973ca95b02SDimitry Andric                               [&]() { return createTargetMachine(); }, FileType,
5983ca95b02SDimitry Andric                               ShouldRestoreGlobalsLinkage);
5993ca95b02SDimitry Andric 
6003ca95b02SDimitry Andric   // If statistics were requested, print them out after codegen.
6013ca95b02SDimitry Andric   if (llvm::AreStatisticsEnabled())
6023ca95b02SDimitry Andric     llvm::PrintStatistics();
603f785676fSDimitry Andric 
604d88c1a5aSDimitry Andric   finishOptimizationRemarks();
605d88c1a5aSDimitry Andric 
606f785676fSDimitry Andric   return true;
607f785676fSDimitry Andric }
608f785676fSDimitry Andric 
609f785676fSDimitry Andric /// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
610f785676fSDimitry Andric /// LTO problems.
611d88c1a5aSDimitry Andric void LTOCodeGenerator::setCodeGenDebugOptions(StringRef Options) {
6127d523365SDimitry Andric   for (std::pair<StringRef, StringRef> o = getToken(Options); !o.first.empty();
6137d523365SDimitry Andric        o = getToken(o.second))
6147d523365SDimitry Andric     CodegenOptions.push_back(o.first);
615f785676fSDimitry Andric }
616f785676fSDimitry Andric 
617f785676fSDimitry Andric void LTOCodeGenerator::parseCodeGenDebugOptions() {
618f785676fSDimitry Andric   // if options were requested, set them
6197d523365SDimitry Andric   if (!CodegenOptions.empty()) {
6207d523365SDimitry Andric     // ParseCommandLineOptions() expects argv[0] to be program name.
6217d523365SDimitry Andric     std::vector<const char *> CodegenArgv(1, "libLLVMLTO");
6227d523365SDimitry Andric     for (std::string &Arg : CodegenOptions)
6237d523365SDimitry Andric       CodegenArgv.push_back(Arg.c_str());
6247d523365SDimitry Andric     cl::ParseCommandLineOptions(CodegenArgv.size(), CodegenArgv.data());
6257d523365SDimitry Andric   }
626f785676fSDimitry Andric }
62791bc56edSDimitry Andric 
62891bc56edSDimitry Andric void LTOCodeGenerator::DiagnosticHandler(const DiagnosticInfo &DI,
62991bc56edSDimitry Andric                                          void *Context) {
63091bc56edSDimitry Andric   ((LTOCodeGenerator *)Context)->DiagnosticHandler2(DI);
63191bc56edSDimitry Andric }
63291bc56edSDimitry Andric 
63391bc56edSDimitry Andric void LTOCodeGenerator::DiagnosticHandler2(const DiagnosticInfo &DI) {
63491bc56edSDimitry Andric   // Map the LLVM internal diagnostic severity to the LTO diagnostic severity.
63591bc56edSDimitry Andric   lto_codegen_diagnostic_severity_t Severity;
63691bc56edSDimitry Andric   switch (DI.getSeverity()) {
63791bc56edSDimitry Andric   case DS_Error:
63891bc56edSDimitry Andric     Severity = LTO_DS_ERROR;
63991bc56edSDimitry Andric     break;
64091bc56edSDimitry Andric   case DS_Warning:
64191bc56edSDimitry Andric     Severity = LTO_DS_WARNING;
64291bc56edSDimitry Andric     break;
64391bc56edSDimitry Andric   case DS_Remark:
64491bc56edSDimitry Andric     Severity = LTO_DS_REMARK;
64591bc56edSDimitry Andric     break;
64691bc56edSDimitry Andric   case DS_Note:
64791bc56edSDimitry Andric     Severity = LTO_DS_NOTE;
64891bc56edSDimitry Andric     break;
64991bc56edSDimitry Andric   }
65091bc56edSDimitry Andric   // Create the string that will be reported to the external diagnostic handler.
65191bc56edSDimitry Andric   std::string MsgStorage;
65291bc56edSDimitry Andric   raw_string_ostream Stream(MsgStorage);
65391bc56edSDimitry Andric   DiagnosticPrinterRawOStream DP(Stream);
65491bc56edSDimitry Andric   DI.print(DP);
65591bc56edSDimitry Andric   Stream.flush();
65691bc56edSDimitry Andric 
65791bc56edSDimitry Andric   // If this method has been called it means someone has set up an external
65891bc56edSDimitry Andric   // diagnostic handler. Assert on that.
65991bc56edSDimitry Andric   assert(DiagHandler && "Invalid diagnostic handler");
66091bc56edSDimitry Andric   (*DiagHandler)(Severity, MsgStorage.c_str(), DiagContext);
66191bc56edSDimitry Andric }
66291bc56edSDimitry Andric 
66391bc56edSDimitry Andric void
66491bc56edSDimitry Andric LTOCodeGenerator::setDiagnosticHandler(lto_diagnostic_handler_t DiagHandler,
66591bc56edSDimitry Andric                                        void *Ctxt) {
66691bc56edSDimitry Andric   this->DiagHandler = DiagHandler;
66791bc56edSDimitry Andric   this->DiagContext = Ctxt;
66891bc56edSDimitry Andric   if (!DiagHandler)
66991bc56edSDimitry Andric     return Context.setDiagnosticHandler(nullptr, nullptr);
67091bc56edSDimitry Andric   // Register the LTOCodeGenerator stub in the LLVMContext to forward the
67191bc56edSDimitry Andric   // diagnostic to the external DiagHandler.
67239d628a0SDimitry Andric   Context.setDiagnosticHandler(LTOCodeGenerator::DiagnosticHandler, this,
67339d628a0SDimitry Andric                                /* RespectFilters */ true);
67491bc56edSDimitry Andric }
6757d523365SDimitry Andric 
6767d523365SDimitry Andric namespace {
6777d523365SDimitry Andric class LTODiagnosticInfo : public DiagnosticInfo {
6787d523365SDimitry Andric   const Twine &Msg;
6797d523365SDimitry Andric public:
6807d523365SDimitry Andric   LTODiagnosticInfo(const Twine &DiagMsg, DiagnosticSeverity Severity=DS_Error)
6817d523365SDimitry Andric       : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {}
6827d523365SDimitry Andric   void print(DiagnosticPrinter &DP) const override { DP << Msg; }
6837d523365SDimitry Andric };
6847d523365SDimitry Andric }
6857d523365SDimitry Andric 
6867d523365SDimitry Andric void LTOCodeGenerator::emitError(const std::string &ErrMsg) {
6877d523365SDimitry Andric   if (DiagHandler)
6887d523365SDimitry Andric     (*DiagHandler)(LTO_DS_ERROR, ErrMsg.c_str(), DiagContext);
6897d523365SDimitry Andric   else
6907d523365SDimitry Andric     Context.diagnose(LTODiagnosticInfo(ErrMsg));
6917d523365SDimitry Andric }
6923ca95b02SDimitry Andric 
6933ca95b02SDimitry Andric void LTOCodeGenerator::emitWarning(const std::string &ErrMsg) {
6943ca95b02SDimitry Andric   if (DiagHandler)
6953ca95b02SDimitry Andric     (*DiagHandler)(LTO_DS_WARNING, ErrMsg.c_str(), DiagContext);
6963ca95b02SDimitry Andric   else
6973ca95b02SDimitry Andric     Context.diagnose(LTODiagnosticInfo(ErrMsg, DS_Warning));
6983ca95b02SDimitry Andric }
699