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"
242cab237bSDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.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"
36*b5893f02SDimitry Andric #include "llvm/IR/PassTimingInfo.h"
3791bc56edSDimitry Andric #include "llvm/IR/Verifier.h"
38f785676fSDimitry Andric #include "llvm/InitializePasses.h"
397a7e6055SDimitry Andric #include "llvm/LTO/LTO.h"
403ca95b02SDimitry Andric #include "llvm/LTO/legacy/LTOModule.h"
413ca95b02SDimitry Andric #include "llvm/LTO/legacy/UpdateCompilerUsed.h"
4291bc56edSDimitry Andric #include "llvm/Linker/Linker.h"
43f785676fSDimitry Andric #include "llvm/MC/MCAsmInfo.h"
44f785676fSDimitry Andric #include "llvm/MC/MCContext.h"
45f785676fSDimitry Andric #include "llvm/MC/SubtargetFeature.h"
46f785676fSDimitry Andric #include "llvm/Support/CommandLine.h"
47f785676fSDimitry Andric #include "llvm/Support/FileSystem.h"
48f785676fSDimitry Andric #include "llvm/Support/Host.h"
49f785676fSDimitry Andric #include "llvm/Support/MemoryBuffer.h"
50f785676fSDimitry Andric #include "llvm/Support/Signals.h"
51f785676fSDimitry Andric #include "llvm/Support/TargetRegistry.h"
52f785676fSDimitry Andric #include "llvm/Support/TargetSelect.h"
53f785676fSDimitry Andric #include "llvm/Support/ToolOutputFile.h"
54d88c1a5aSDimitry Andric #include "llvm/Support/YAMLTraits.h"
5591bc56edSDimitry Andric #include "llvm/Support/raw_ostream.h"
56f785676fSDimitry Andric #include "llvm/Target/TargetOptions.h"
57f785676fSDimitry Andric #include "llvm/Transforms/IPO.h"
583ca95b02SDimitry Andric #include "llvm/Transforms/IPO/Internalize.h"
59f785676fSDimitry Andric #include "llvm/Transforms/IPO/PassManagerBuilder.h"
60f785676fSDimitry Andric #include "llvm/Transforms/ObjCARC.h"
61d88c1a5aSDimitry Andric #include "llvm/Transforms/Utils/ModuleUtils.h"
6291bc56edSDimitry Andric #include <system_error>
63f785676fSDimitry Andric using namespace llvm;
64f785676fSDimitry Andric 
getVersionString()65f785676fSDimitry Andric const char* LTOCodeGenerator::getVersionString() {
66f785676fSDimitry Andric #ifdef LLVM_VERSION_INFO
67f785676fSDimitry Andric   return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
68f785676fSDimitry Andric #else
69f785676fSDimitry Andric   return PACKAGE_NAME " version " PACKAGE_VERSION;
70f785676fSDimitry Andric #endif
71f785676fSDimitry Andric }
72f785676fSDimitry Andric 
733ca95b02SDimitry Andric namespace llvm {
743ca95b02SDimitry Andric cl::opt<bool> LTODiscardValueNames(
753ca95b02SDimitry Andric     "lto-discard-value-names",
763ca95b02SDimitry Andric     cl::desc("Strip names from Value during LTO (other than GlobalValue)."),
773ca95b02SDimitry Andric #ifdef NDEBUG
783ca95b02SDimitry Andric     cl::init(true),
793ca95b02SDimitry Andric #else
803ca95b02SDimitry Andric     cl::init(false),
813ca95b02SDimitry Andric #endif
823ca95b02SDimitry Andric     cl::Hidden);
833ca95b02SDimitry Andric 
84d88c1a5aSDimitry Andric cl::opt<std::string>
85d88c1a5aSDimitry Andric     LTORemarksFilename("lto-pass-remarks-output",
86d88c1a5aSDimitry Andric                        cl::desc("Output filename for pass remarks"),
87d88c1a5aSDimitry Andric                        cl::value_desc("filename"));
88d88c1a5aSDimitry Andric 
89d88c1a5aSDimitry Andric cl::opt<bool> LTOPassRemarksWithHotness(
90d88c1a5aSDimitry Andric     "lto-pass-remarks-with-hotness",
91d88c1a5aSDimitry Andric     cl::desc("With PGO, include profile count in optimization remarks"),
92d88c1a5aSDimitry Andric     cl::Hidden);
933ca95b02SDimitry Andric }
943ca95b02SDimitry Andric 
LTOCodeGenerator(LLVMContext & Context)957d523365SDimitry Andric LTOCodeGenerator::LTOCodeGenerator(LLVMContext &Context)
967d523365SDimitry Andric     : Context(Context), MergedModule(new Module("ld-temp.o", Context)),
977d523365SDimitry Andric       TheLinker(new Linker(*MergedModule)) {
983ca95b02SDimitry Andric   Context.setDiscardValueNames(LTODiscardValueNames);
993ca95b02SDimitry Andric   Context.enableDebugTypeODRUniquing();
100ff0cc061SDimitry Andric   initializeLTOPasses();
10139d628a0SDimitry Andric }
10239d628a0SDimitry Andric 
~LTOCodeGenerator()1037d523365SDimitry Andric LTOCodeGenerator::~LTOCodeGenerator() {}
104f785676fSDimitry Andric 
1057d523365SDimitry Andric // Initialize LTO passes. Please keep this function in sync with
106f785676fSDimitry Andric // PassManagerBuilder::populateLTOPassManager(), and make sure all LTO
107f785676fSDimitry Andric // passes are initialized.
initializeLTOPasses()108f785676fSDimitry Andric void LTOCodeGenerator::initializeLTOPasses() {
109f785676fSDimitry Andric   PassRegistry &R = *PassRegistry::getPassRegistry();
110f785676fSDimitry Andric 
1113ca95b02SDimitry Andric   initializeInternalizeLegacyPassPass(R);
1123ca95b02SDimitry Andric   initializeIPSCCPLegacyPassPass(R);
1133ca95b02SDimitry Andric   initializeGlobalOptLegacyPassPass(R);
1143ca95b02SDimitry Andric   initializeConstantMergeLegacyPassPass(R);
115f785676fSDimitry Andric   initializeDAHPass(R);
116ff0cc061SDimitry Andric   initializeInstructionCombiningPassPass(R);
117f785676fSDimitry Andric   initializeSimpleInlinerPass(R);
118f785676fSDimitry Andric   initializePruneEHPass(R);
1193ca95b02SDimitry Andric   initializeGlobalDCELegacyPassPass(R);
120f785676fSDimitry Andric   initializeArgPromotionPass(R);
121f785676fSDimitry Andric   initializeJumpThreadingPass(R);
1227d523365SDimitry Andric   initializeSROALegacyPassPass(R);
1233ca95b02SDimitry Andric   initializePostOrderFunctionAttrsLegacyPassPass(R);
1243ca95b02SDimitry Andric   initializeReversePostOrderFunctionAttrsLegacyPassPass(R);
1257d523365SDimitry Andric   initializeGlobalsAAWrapperPassPass(R);
1263ca95b02SDimitry Andric   initializeLegacyLICMPassPass(R);
1273ca95b02SDimitry Andric   initializeMergedLoadStoreMotionLegacyPassPass(R);
1283ca95b02SDimitry Andric   initializeGVNLegacyPassPass(R);
1293ca95b02SDimitry Andric   initializeMemCpyOptLegacyPassPass(R);
1303ca95b02SDimitry Andric   initializeDCELegacyPassPass(R);
131f785676fSDimitry Andric   initializeCFGSimplifyPassPass(R);
132f785676fSDimitry Andric }
133f785676fSDimitry Andric 
setAsmUndefinedRefs(LTOModule * Mod)134d88c1a5aSDimitry Andric void LTOCodeGenerator::setAsmUndefinedRefs(LTOModule *Mod) {
135d88c1a5aSDimitry Andric   const std::vector<StringRef> &undefs = Mod->getAsmUndefinedRefs();
136d88c1a5aSDimitry Andric   for (int i = 0, e = undefs.size(); i != e; ++i)
137d88c1a5aSDimitry Andric     AsmUndefinedRefs[undefs[i]] = 1;
138d88c1a5aSDimitry Andric }
139d88c1a5aSDimitry Andric 
addModule(LTOModule * Mod)1407d523365SDimitry Andric bool LTOCodeGenerator::addModule(LTOModule *Mod) {
1417d523365SDimitry Andric   assert(&Mod->getModule().getContext() == &Context &&
14239d628a0SDimitry Andric          "Expected module in same context");
14339d628a0SDimitry Andric 
1447d523365SDimitry Andric   bool ret = TheLinker->linkInModule(Mod->takeModule());
145d88c1a5aSDimitry Andric   setAsmUndefinedRefs(Mod);
146f785676fSDimitry Andric 
1473ca95b02SDimitry Andric   // We've just changed the input, so let's make sure we verify it.
1483ca95b02SDimitry Andric   HasVerifiedInput = false;
1493ca95b02SDimitry Andric 
150f785676fSDimitry Andric   return !ret;
151f785676fSDimitry Andric }
152f785676fSDimitry Andric 
setModule(std::unique_ptr<LTOModule> Mod)1537d523365SDimitry Andric void LTOCodeGenerator::setModule(std::unique_ptr<LTOModule> Mod) {
154ff0cc061SDimitry Andric   assert(&Mod->getModule().getContext() == &Context &&
155ff0cc061SDimitry Andric          "Expected module in same context");
156ff0cc061SDimitry Andric 
157ff0cc061SDimitry Andric   AsmUndefinedRefs.clear();
158ff0cc061SDimitry Andric 
1597d523365SDimitry Andric   MergedModule = Mod->takeModule();
1607d523365SDimitry Andric   TheLinker = make_unique<Linker>(*MergedModule);
161d88c1a5aSDimitry Andric   setAsmUndefinedRefs(&*Mod);
1623ca95b02SDimitry Andric 
1633ca95b02SDimitry Andric   // We've just changed the input, so let's make sure we verify it.
1643ca95b02SDimitry Andric   HasVerifiedInput = false;
165ff0cc061SDimitry Andric }
166ff0cc061SDimitry Andric 
setTargetOptions(const TargetOptions & Options)1673ca95b02SDimitry Andric void LTOCodeGenerator::setTargetOptions(const TargetOptions &Options) {
1687d523365SDimitry Andric   this->Options = Options;
169f785676fSDimitry Andric }
170f785676fSDimitry Andric 
setDebugInfo(lto_debug_model Debug)1717d523365SDimitry Andric void LTOCodeGenerator::setDebugInfo(lto_debug_model Debug) {
1727d523365SDimitry Andric   switch (Debug) {
173f785676fSDimitry Andric   case LTO_DEBUG_MODEL_NONE:
174f785676fSDimitry Andric     EmitDwarfDebugInfo = false;
175f785676fSDimitry Andric     return;
176f785676fSDimitry Andric 
177f785676fSDimitry Andric   case LTO_DEBUG_MODEL_DWARF:
178f785676fSDimitry Andric     EmitDwarfDebugInfo = true;
179f785676fSDimitry Andric     return;
180f785676fSDimitry Andric   }
181f785676fSDimitry Andric   llvm_unreachable("Unknown debug format!");
182f785676fSDimitry Andric }
183f785676fSDimitry Andric 
setOptLevel(unsigned Level)1847d523365SDimitry Andric void LTOCodeGenerator::setOptLevel(unsigned Level) {
1857d523365SDimitry Andric   OptLevel = Level;
186ff0cc061SDimitry Andric   switch (OptLevel) {
187ff0cc061SDimitry Andric   case 0:
188ff0cc061SDimitry Andric     CGOptLevel = CodeGenOpt::None;
189d88c1a5aSDimitry Andric     return;
190ff0cc061SDimitry Andric   case 1:
191ff0cc061SDimitry Andric     CGOptLevel = CodeGenOpt::Less;
192d88c1a5aSDimitry Andric     return;
193ff0cc061SDimitry Andric   case 2:
194ff0cc061SDimitry Andric     CGOptLevel = CodeGenOpt::Default;
195d88c1a5aSDimitry Andric     return;
196ff0cc061SDimitry Andric   case 3:
197ff0cc061SDimitry Andric     CGOptLevel = CodeGenOpt::Aggressive;
198d88c1a5aSDimitry Andric     return;
199ff0cc061SDimitry Andric   }
200d88c1a5aSDimitry Andric   llvm_unreachable("Unknown optimization level!");
2017d523365SDimitry Andric }
202ff0cc061SDimitry Andric 
writeMergedModules(StringRef Path)203d88c1a5aSDimitry Andric bool LTOCodeGenerator::writeMergedModules(StringRef Path) {
2047d523365SDimitry Andric   if (!determineTarget())
2057d523365SDimitry Andric     return false;
2067d523365SDimitry Andric 
2073ca95b02SDimitry Andric   // We always run the verifier once on the merged module.
2083ca95b02SDimitry Andric   verifyMergedModuleOnce();
2093ca95b02SDimitry Andric 
2107d523365SDimitry Andric   // mark which symbols can not be internalized
2117d523365SDimitry Andric   applyScopeRestrictions();
2127d523365SDimitry Andric 
2137d523365SDimitry Andric   // create output file
2147d523365SDimitry Andric   std::error_code EC;
2152cab237bSDimitry Andric   ToolOutputFile Out(Path, EC, sys::fs::F_None);
2167d523365SDimitry Andric   if (EC) {
2177d523365SDimitry Andric     std::string ErrMsg = "could not open bitcode file for writing: ";
2182cab237bSDimitry Andric     ErrMsg += Path.str() + ": " + EC.message();
2197d523365SDimitry Andric     emitError(ErrMsg);
2207d523365SDimitry Andric     return false;
2217d523365SDimitry Andric   }
2227d523365SDimitry Andric 
2237d523365SDimitry Andric   // write bitcode to it
2244ba319b5SDimitry Andric   WriteBitcodeToFile(*MergedModule, Out.os(), ShouldEmbedUselists);
2257d523365SDimitry Andric   Out.os().close();
2267d523365SDimitry Andric 
2277d523365SDimitry Andric   if (Out.os().has_error()) {
2287d523365SDimitry Andric     std::string ErrMsg = "could not write bitcode file: ";
2292cab237bSDimitry Andric     ErrMsg += Path.str() + ": " + Out.os().error().message();
2307d523365SDimitry Andric     emitError(ErrMsg);
2317d523365SDimitry Andric     Out.os().clear_error();
2327d523365SDimitry Andric     return false;
2337d523365SDimitry Andric   }
2347d523365SDimitry Andric 
2357d523365SDimitry Andric   Out.keep();
2367d523365SDimitry Andric   return true;
2377d523365SDimitry Andric }
2387d523365SDimitry Andric 
compileOptimizedToFile(const char ** Name)2397d523365SDimitry Andric bool LTOCodeGenerator::compileOptimizedToFile(const char **Name) {
2407d523365SDimitry Andric   // make unique temp output file to put generated code
2417d523365SDimitry Andric   SmallString<128> Filename;
2427d523365SDimitry Andric   int FD;
2437d523365SDimitry Andric 
244d88c1a5aSDimitry Andric   StringRef Extension
2457d523365SDimitry Andric       (FileType == TargetMachine::CGFT_AssemblyFile ? "s" : "o");
2467d523365SDimitry Andric 
2477d523365SDimitry Andric   std::error_code EC =
2487d523365SDimitry Andric       sys::fs::createTemporaryFile("lto-llvm", Extension, FD, Filename);
2497d523365SDimitry Andric   if (EC) {
2507d523365SDimitry Andric     emitError(EC.message());
2517d523365SDimitry Andric     return false;
2527d523365SDimitry Andric   }
2537d523365SDimitry Andric 
2547d523365SDimitry Andric   // generate object file
2552cab237bSDimitry Andric   ToolOutputFile objFile(Filename, FD);
2567d523365SDimitry Andric 
2577d523365SDimitry Andric   bool genResult = compileOptimized(&objFile.os());
2587d523365SDimitry Andric   objFile.os().close();
2597d523365SDimitry Andric   if (objFile.os().has_error()) {
2602cab237bSDimitry Andric     emitError((Twine("could not write object file: ") + Filename + ": " +
2612cab237bSDimitry Andric                objFile.os().error().message())
2622cab237bSDimitry Andric                   .str());
2637d523365SDimitry Andric     objFile.os().clear_error();
2647d523365SDimitry Andric     sys::fs::remove(Twine(Filename));
2657d523365SDimitry Andric     return false;
2667d523365SDimitry Andric   }
2677d523365SDimitry Andric 
2687d523365SDimitry Andric   objFile.keep();
2697d523365SDimitry Andric   if (!genResult) {
2707d523365SDimitry Andric     sys::fs::remove(Twine(Filename));
2717d523365SDimitry Andric     return false;
2727d523365SDimitry Andric   }
2737d523365SDimitry Andric 
2747d523365SDimitry Andric   NativeObjectPath = Filename.c_str();
2757d523365SDimitry Andric   *Name = NativeObjectPath.c_str();
2767d523365SDimitry Andric   return true;
2777d523365SDimitry Andric }
2787d523365SDimitry Andric 
2797d523365SDimitry Andric std::unique_ptr<MemoryBuffer>
compileOptimized()2807d523365SDimitry Andric LTOCodeGenerator::compileOptimized() {
2817d523365SDimitry Andric   const char *name;
2827d523365SDimitry Andric   if (!compileOptimizedToFile(&name))
2837d523365SDimitry Andric     return nullptr;
2847d523365SDimitry Andric 
2857d523365SDimitry Andric   // read .o file into memory buffer
2867d523365SDimitry Andric   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
2877d523365SDimitry Andric       MemoryBuffer::getFile(name, -1, false);
2887d523365SDimitry Andric   if (std::error_code EC = BufferOrErr.getError()) {
2897d523365SDimitry Andric     emitError(EC.message());
2907d523365SDimitry Andric     sys::fs::remove(NativeObjectPath);
2917d523365SDimitry Andric     return nullptr;
2927d523365SDimitry Andric   }
2937d523365SDimitry Andric 
2947d523365SDimitry Andric   // remove temp files
2957d523365SDimitry Andric   sys::fs::remove(NativeObjectPath);
2967d523365SDimitry Andric 
2977d523365SDimitry Andric   return std::move(*BufferOrErr);
2987d523365SDimitry Andric }
2997d523365SDimitry Andric 
compile_to_file(const char ** Name,bool DisableVerify,bool DisableInline,bool DisableGVNLoadPRE,bool DisableVectorization)3007d523365SDimitry Andric bool LTOCodeGenerator::compile_to_file(const char **Name, bool DisableVerify,
3017d523365SDimitry Andric                                        bool DisableInline,
3027d523365SDimitry Andric                                        bool DisableGVNLoadPRE,
3037d523365SDimitry Andric                                        bool DisableVectorization) {
3047d523365SDimitry Andric   if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
3057d523365SDimitry Andric                 DisableVectorization))
3067d523365SDimitry Andric     return false;
3077d523365SDimitry Andric 
3087d523365SDimitry Andric   return compileOptimizedToFile(Name);
3097d523365SDimitry Andric }
3107d523365SDimitry Andric 
3117d523365SDimitry Andric std::unique_ptr<MemoryBuffer>
compile(bool DisableVerify,bool DisableInline,bool DisableGVNLoadPRE,bool DisableVectorization)3127d523365SDimitry Andric LTOCodeGenerator::compile(bool DisableVerify, bool DisableInline,
3137d523365SDimitry Andric                           bool DisableGVNLoadPRE, bool DisableVectorization) {
3147d523365SDimitry Andric   if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
3157d523365SDimitry Andric                 DisableVectorization))
3167d523365SDimitry Andric     return nullptr;
3177d523365SDimitry Andric 
3187d523365SDimitry Andric   return compileOptimized();
3197d523365SDimitry Andric }
3207d523365SDimitry Andric 
determineTarget()3217d523365SDimitry Andric bool LTOCodeGenerator::determineTarget() {
3227d523365SDimitry Andric   if (TargetMach)
3237d523365SDimitry Andric     return true;
3247d523365SDimitry Andric 
3253ca95b02SDimitry Andric   TripleStr = MergedModule->getTargetTriple();
3267d523365SDimitry Andric   if (TripleStr.empty()) {
3277d523365SDimitry Andric     TripleStr = sys::getDefaultTargetTriple();
3287d523365SDimitry Andric     MergedModule->setTargetTriple(TripleStr);
3297d523365SDimitry Andric   }
3307d523365SDimitry Andric   llvm::Triple Triple(TripleStr);
3317d523365SDimitry Andric 
3327d523365SDimitry Andric   // create target machine from info for merged modules
3337d523365SDimitry Andric   std::string ErrMsg;
3343ca95b02SDimitry Andric   MArch = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
3353ca95b02SDimitry Andric   if (!MArch) {
3367d523365SDimitry Andric     emitError(ErrMsg);
3377d523365SDimitry Andric     return false;
3387d523365SDimitry Andric   }
3397d523365SDimitry Andric 
3407d523365SDimitry Andric   // Construct LTOModule, hand over ownership of module and target. Use MAttr as
3417d523365SDimitry Andric   // the default set of features.
3427d523365SDimitry Andric   SubtargetFeatures Features(MAttr);
3437d523365SDimitry Andric   Features.getDefaultSubtargetFeatures(Triple);
3447d523365SDimitry Andric   FeatureStr = Features.getString();
3457d523365SDimitry Andric   // Set a default CPU for Darwin triples.
3467d523365SDimitry Andric   if (MCpu.empty() && Triple.isOSDarwin()) {
3477d523365SDimitry Andric     if (Triple.getArch() == llvm::Triple::x86_64)
3487d523365SDimitry Andric       MCpu = "core2";
3497d523365SDimitry Andric     else if (Triple.getArch() == llvm::Triple::x86)
3507d523365SDimitry Andric       MCpu = "yonah";
3517d523365SDimitry Andric     else if (Triple.getArch() == llvm::Triple::aarch64)
3527d523365SDimitry Andric       MCpu = "cyclone";
3537d523365SDimitry Andric   }
3547d523365SDimitry Andric 
3553ca95b02SDimitry Andric   TargetMach = createTargetMachine();
356f785676fSDimitry Andric   return true;
357f785676fSDimitry Andric }
358f785676fSDimitry Andric 
createTargetMachine()3593ca95b02SDimitry Andric std::unique_ptr<TargetMachine> LTOCodeGenerator::createTargetMachine() {
3602cab237bSDimitry Andric   return std::unique_ptr<TargetMachine>(MArch->createTargetMachine(
3612cab237bSDimitry Andric       TripleStr, MCpu, FeatureStr, Options, RelocModel, None, CGOptLevel));
362f785676fSDimitry Andric }
363f785676fSDimitry Andric 
3643ca95b02SDimitry Andric // If a linkonce global is present in the MustPreserveSymbols, we need to make
3653ca95b02SDimitry Andric // sure we honor this. To force the compiler to not drop it, we add it to the
3663ca95b02SDimitry Andric // "llvm.compiler.used" global.
preserveDiscardableGVs(Module & TheModule,llvm::function_ref<bool (const GlobalValue &)> mustPreserveGV)3673ca95b02SDimitry Andric void LTOCodeGenerator::preserveDiscardableGVs(
3683ca95b02SDimitry Andric     Module &TheModule,
3693ca95b02SDimitry Andric     llvm::function_ref<bool(const GlobalValue &)> mustPreserveGV) {
370d88c1a5aSDimitry Andric   std::vector<GlobalValue *> Used;
3713ca95b02SDimitry Andric   auto mayPreserveGlobal = [&](GlobalValue &GV) {
372d88c1a5aSDimitry Andric     if (!GV.isDiscardableIfUnused() || GV.isDeclaration() ||
373d88c1a5aSDimitry Andric         !mustPreserveGV(GV))
3743ca95b02SDimitry Andric       return;
375d88c1a5aSDimitry Andric     if (GV.hasAvailableExternallyLinkage())
376d88c1a5aSDimitry Andric       return emitWarning(
3773ca95b02SDimitry Andric           (Twine("Linker asked to preserve available_externally global: '") +
3783ca95b02SDimitry Andric            GV.getName() + "'").str());
379d88c1a5aSDimitry Andric     if (GV.hasInternalLinkage())
380d88c1a5aSDimitry Andric       return emitWarning((Twine("Linker asked to preserve internal global: '") +
3813ca95b02SDimitry Andric                    GV.getName() + "'").str());
382d88c1a5aSDimitry Andric     Used.push_back(&GV);
3833ca95b02SDimitry Andric   };
3843ca95b02SDimitry Andric   for (auto &GV : TheModule)
3853ca95b02SDimitry Andric     mayPreserveGlobal(GV);
3863ca95b02SDimitry Andric   for (auto &GV : TheModule.globals())
3873ca95b02SDimitry Andric     mayPreserveGlobal(GV);
3883ca95b02SDimitry Andric   for (auto &GV : TheModule.aliases())
3893ca95b02SDimitry Andric     mayPreserveGlobal(GV);
390f785676fSDimitry Andric 
391d88c1a5aSDimitry Andric   if (Used.empty())
3923ca95b02SDimitry Andric     return;
3933ca95b02SDimitry Andric 
394d88c1a5aSDimitry Andric   appendToCompilerUsed(TheModule, Used);
395f785676fSDimitry Andric }
396f785676fSDimitry Andric 
applyScopeRestrictions()397f785676fSDimitry Andric void LTOCodeGenerator::applyScopeRestrictions() {
3983ca95b02SDimitry Andric   if (ScopeRestrictionsDone)
399f785676fSDimitry Andric     return;
400f785676fSDimitry Andric 
4013ca95b02SDimitry Andric   // Declare a callback for the internalize pass that will ask for every
4023ca95b02SDimitry Andric   // candidate GlobalValue if it can be internalized or not.
403d88c1a5aSDimitry Andric   Mangler Mang;
4043ca95b02SDimitry Andric   SmallString<64> MangledName;
4053ca95b02SDimitry Andric   auto mustPreserveGV = [&](const GlobalValue &GV) -> bool {
4063ca95b02SDimitry Andric     // Unnamed globals can't be mangled, but they can't be preserved either.
4073ca95b02SDimitry Andric     if (!GV.hasName())
4083ca95b02SDimitry Andric       return false;
409f785676fSDimitry Andric 
4103ca95b02SDimitry Andric     // Need to mangle the GV as the "MustPreserveSymbols" StringSet is filled
4113ca95b02SDimitry Andric     // with the linker supplied name, which on Darwin includes a leading
4123ca95b02SDimitry Andric     // underscore.
4133ca95b02SDimitry Andric     MangledName.clear();
4143ca95b02SDimitry Andric     MangledName.reserve(GV.getName().size() + 1);
415d88c1a5aSDimitry Andric     Mang.getNameWithPrefix(MangledName, &GV, /*CannotUsePrivateLabel=*/false);
4163ca95b02SDimitry Andric     return MustPreserveSymbols.count(MangledName);
4173ca95b02SDimitry Andric   };
418ff0cc061SDimitry Andric 
4193ca95b02SDimitry Andric   // Preserve linkonce value on linker request
4203ca95b02SDimitry Andric   preserveDiscardableGVs(*MergedModule, mustPreserveGV);
421f785676fSDimitry Andric 
4223ca95b02SDimitry Andric   if (!ShouldInternalize)
4233ca95b02SDimitry Andric     return;
424f785676fSDimitry Andric 
4253ca95b02SDimitry Andric   if (ShouldRestoreGlobalsLinkage) {
4263ca95b02SDimitry Andric     // Record the linkage type of non-local symbols so they can be restored
4273ca95b02SDimitry Andric     // prior
4283ca95b02SDimitry Andric     // to module splitting.
4293ca95b02SDimitry Andric     auto RecordLinkage = [&](const GlobalValue &GV) {
4303ca95b02SDimitry Andric       if (!GV.hasAvailableExternallyLinkage() && !GV.hasLocalLinkage() &&
4313ca95b02SDimitry Andric           GV.hasName())
4323ca95b02SDimitry Andric         ExternalSymbols.insert(std::make_pair(GV.getName(), GV.getLinkage()));
4333ca95b02SDimitry Andric     };
4343ca95b02SDimitry Andric     for (auto &GV : *MergedModule)
4353ca95b02SDimitry Andric       RecordLinkage(GV);
4363ca95b02SDimitry Andric     for (auto &GV : MergedModule->globals())
4373ca95b02SDimitry Andric       RecordLinkage(GV);
4383ca95b02SDimitry Andric     for (auto &GV : MergedModule->aliases())
4393ca95b02SDimitry Andric       RecordLinkage(GV);
440f785676fSDimitry Andric   }
441f785676fSDimitry Andric 
4423ca95b02SDimitry Andric   // Update the llvm.compiler_used globals to force preserving libcalls and
4433ca95b02SDimitry Andric   // symbols referenced from asm
4443ca95b02SDimitry Andric   updateCompilerUsed(*MergedModule, *TargetMach, AsmUndefinedRefs);
445f785676fSDimitry Andric 
4463ca95b02SDimitry Andric   internalizeModule(*MergedModule, mustPreserveGV);
447f785676fSDimitry Andric 
448f785676fSDimitry Andric   ScopeRestrictionsDone = true;
449f785676fSDimitry Andric }
450f785676fSDimitry Andric 
4513ca95b02SDimitry Andric /// Restore original linkage for symbols that may have been internalized
restoreLinkageForExternals()4523ca95b02SDimitry Andric void LTOCodeGenerator::restoreLinkageForExternals() {
4533ca95b02SDimitry Andric   if (!ShouldInternalize || !ShouldRestoreGlobalsLinkage)
4543ca95b02SDimitry Andric     return;
4553ca95b02SDimitry Andric 
4563ca95b02SDimitry Andric   assert(ScopeRestrictionsDone &&
4573ca95b02SDimitry Andric          "Cannot externalize without internalization!");
4583ca95b02SDimitry Andric 
4593ca95b02SDimitry Andric   if (ExternalSymbols.empty())
4603ca95b02SDimitry Andric     return;
4613ca95b02SDimitry Andric 
4623ca95b02SDimitry Andric   auto externalize = [this](GlobalValue &GV) {
4633ca95b02SDimitry Andric     if (!GV.hasLocalLinkage() || !GV.hasName())
4643ca95b02SDimitry Andric       return;
4653ca95b02SDimitry Andric 
4663ca95b02SDimitry Andric     auto I = ExternalSymbols.find(GV.getName());
4673ca95b02SDimitry Andric     if (I == ExternalSymbols.end())
4683ca95b02SDimitry Andric       return;
4693ca95b02SDimitry Andric 
4703ca95b02SDimitry Andric     GV.setLinkage(I->second);
4713ca95b02SDimitry Andric   };
4723ca95b02SDimitry Andric 
4732cab237bSDimitry Andric   llvm::for_each(MergedModule->functions(), externalize);
4742cab237bSDimitry Andric   llvm::for_each(MergedModule->globals(), externalize);
4752cab237bSDimitry Andric   llvm::for_each(MergedModule->aliases(), externalize);
4763ca95b02SDimitry Andric }
4773ca95b02SDimitry Andric 
verifyMergedModuleOnce()4783ca95b02SDimitry Andric void LTOCodeGenerator::verifyMergedModuleOnce() {
4793ca95b02SDimitry Andric   // Only run on the first call.
4803ca95b02SDimitry Andric   if (HasVerifiedInput)
4813ca95b02SDimitry Andric     return;
4823ca95b02SDimitry Andric   HasVerifiedInput = true;
4833ca95b02SDimitry Andric 
4843ca95b02SDimitry Andric   bool BrokenDebugInfo = false;
4852cab237bSDimitry Andric   if (verifyModule(*MergedModule, &dbgs(), &BrokenDebugInfo))
4863ca95b02SDimitry Andric     report_fatal_error("Broken module found, compilation aborted!");
4873ca95b02SDimitry Andric   if (BrokenDebugInfo) {
4883ca95b02SDimitry Andric     emitWarning("Invalid debug info found, debug info will be stripped");
4893ca95b02SDimitry Andric     StripDebugInfo(*MergedModule);
4903ca95b02SDimitry Andric   }
4913ca95b02SDimitry Andric }
4923ca95b02SDimitry Andric 
finishOptimizationRemarks()493d88c1a5aSDimitry Andric void LTOCodeGenerator::finishOptimizationRemarks() {
494d88c1a5aSDimitry Andric   if (DiagnosticOutputFile) {
495d88c1a5aSDimitry Andric     DiagnosticOutputFile->keep();
496d88c1a5aSDimitry Andric     // FIXME: LTOCodeGenerator dtor is not invoked on Darwin
497d88c1a5aSDimitry Andric     DiagnosticOutputFile->os().flush();
498d88c1a5aSDimitry Andric   }
499d88c1a5aSDimitry Andric }
500d88c1a5aSDimitry Andric 
501f785676fSDimitry Andric /// Optimize merged modules using various IPO passes
optimize(bool DisableVerify,bool DisableInline,bool DisableGVNLoadPRE,bool DisableVectorization)5027d523365SDimitry Andric bool LTOCodeGenerator::optimize(bool DisableVerify, bool DisableInline,
503f785676fSDimitry Andric                                 bool DisableGVNLoadPRE,
5047d523365SDimitry Andric                                 bool DisableVectorization) {
5057d523365SDimitry Andric   if (!this->determineTarget())
506f785676fSDimitry Andric     return false;
507f785676fSDimitry Andric 
5087a7e6055SDimitry Andric   auto DiagFileOrErr = lto::setupOptimizationRemarks(
5097a7e6055SDimitry Andric       Context, LTORemarksFilename, LTOPassRemarksWithHotness);
5107a7e6055SDimitry Andric   if (!DiagFileOrErr) {
5117a7e6055SDimitry Andric     errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";
5127a7e6055SDimitry Andric     report_fatal_error("Can't get an output file for the remarks");
5137a7e6055SDimitry Andric   }
5147a7e6055SDimitry Andric   DiagnosticOutputFile = std::move(*DiagFileOrErr);
515d88c1a5aSDimitry Andric 
5163ca95b02SDimitry Andric   // We always run the verifier once on the merged module, the `DisableVerify`
5173ca95b02SDimitry Andric   // parameter only applies to subsequent verify.
5183ca95b02SDimitry Andric   verifyMergedModuleOnce();
5193ca95b02SDimitry Andric 
520f785676fSDimitry Andric   // Mark which symbols can not be internalized
521f785676fSDimitry Andric   this->applyScopeRestrictions();
522f785676fSDimitry Andric 
523f785676fSDimitry Andric   // Instantiate the pass manager to organize the passes.
524ff0cc061SDimitry Andric   legacy::PassManager passes;
525f785676fSDimitry Andric 
526f785676fSDimitry Andric   // Add an appropriate DataLayout instance for this module...
5277d523365SDimitry Andric   MergedModule->setDataLayout(TargetMach->createDataLayout());
528ff0cc061SDimitry Andric 
529ff0cc061SDimitry Andric   passes.add(
530ff0cc061SDimitry Andric       createTargetTransformInfoWrapperPass(TargetMach->getTargetIRAnalysis()));
53191bc56edSDimitry Andric 
53239d628a0SDimitry Andric   Triple TargetTriple(TargetMach->getTargetTriple());
53339d628a0SDimitry Andric   PassManagerBuilder PMB;
53439d628a0SDimitry Andric   PMB.DisableGVNLoadPRE = DisableGVNLoadPRE;
53539d628a0SDimitry Andric   PMB.LoopVectorize = !DisableVectorization;
53639d628a0SDimitry Andric   PMB.SLPVectorize = !DisableVectorization;
53739d628a0SDimitry Andric   if (!DisableInline)
53839d628a0SDimitry Andric     PMB.Inliner = createFunctionInliningPass();
539ff0cc061SDimitry Andric   PMB.LibraryInfo = new TargetLibraryInfoImpl(TargetTriple);
5407a7e6055SDimitry Andric   if (Freestanding)
5417a7e6055SDimitry Andric     PMB.LibraryInfo->disableAllFunctions();
542ff0cc061SDimitry Andric   PMB.OptLevel = OptLevel;
5437d523365SDimitry Andric   PMB.VerifyInput = !DisableVerify;
5447d523365SDimitry Andric   PMB.VerifyOutput = !DisableVerify;
54591bc56edSDimitry Andric 
546ff0cc061SDimitry Andric   PMB.populateLTOPassManager(passes);
547f785676fSDimitry Andric 
548ff0cc061SDimitry Andric   // Run our queue of passes all at once now, efficiently.
5497d523365SDimitry Andric   passes.run(*MergedModule);
550f785676fSDimitry Andric 
551ff0cc061SDimitry Andric   return true;
552ff0cc061SDimitry Andric }
553f785676fSDimitry Andric 
compileOptimized(ArrayRef<raw_pwrite_stream * > Out)5547d523365SDimitry Andric bool LTOCodeGenerator::compileOptimized(ArrayRef<raw_pwrite_stream *> Out) {
5557d523365SDimitry Andric   if (!this->determineTarget())
556ff0cc061SDimitry Andric     return false;
557ff0cc061SDimitry Andric 
5583ca95b02SDimitry Andric   // We always run the verifier once on the merged module.  If it has already
5593ca95b02SDimitry Andric   // been called in optimize(), this call will return early.
5603ca95b02SDimitry Andric   verifyMergedModuleOnce();
5613ca95b02SDimitry Andric 
5627d523365SDimitry Andric   legacy::PassManager preCodeGenPasses;
563f785676fSDimitry Andric 
564f785676fSDimitry Andric   // If the bitcode files contain ARC code and were compiled with optimization,
565f785676fSDimitry Andric   // the ObjCARCContractPass must be run, so do it unconditionally here.
5667d523365SDimitry Andric   preCodeGenPasses.add(createObjCARCContractPass());
5677d523365SDimitry Andric   preCodeGenPasses.run(*MergedModule);
568f785676fSDimitry Andric 
5693ca95b02SDimitry Andric   // Re-externalize globals that may have been internalized to increase scope
5703ca95b02SDimitry Andric   // for splitting
5713ca95b02SDimitry Andric   restoreLinkageForExternals();
5723ca95b02SDimitry Andric 
5737d523365SDimitry Andric   // Do code generation. We need to preserve the module in case the client calls
5747d523365SDimitry Andric   // writeMergedModules() after compilation, but we only need to allow this at
5757d523365SDimitry Andric   // parallelism level 1. This is achieved by having splitCodeGen return the
5767d523365SDimitry Andric   // original module at parallelism level 1 which we then assign back to
5777d523365SDimitry Andric   // MergedModule.
5783ca95b02SDimitry Andric   MergedModule = splitCodeGen(std::move(MergedModule), Out, {},
5793ca95b02SDimitry Andric                               [&]() { return createTargetMachine(); }, FileType,
5803ca95b02SDimitry Andric                               ShouldRestoreGlobalsLinkage);
5813ca95b02SDimitry Andric 
5823ca95b02SDimitry Andric   // If statistics were requested, print them out after codegen.
5833ca95b02SDimitry Andric   if (llvm::AreStatisticsEnabled())
5843ca95b02SDimitry Andric     llvm::PrintStatistics();
5855517e702SDimitry Andric   reportAndResetTimings();
586f785676fSDimitry Andric 
587d88c1a5aSDimitry Andric   finishOptimizationRemarks();
588d88c1a5aSDimitry Andric 
589f785676fSDimitry Andric   return true;
590f785676fSDimitry Andric }
591f785676fSDimitry Andric 
592f785676fSDimitry Andric /// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
593f785676fSDimitry Andric /// LTO problems.
setCodeGenDebugOptions(StringRef Options)594d88c1a5aSDimitry Andric void LTOCodeGenerator::setCodeGenDebugOptions(StringRef Options) {
5957d523365SDimitry Andric   for (std::pair<StringRef, StringRef> o = getToken(Options); !o.first.empty();
5967d523365SDimitry Andric        o = getToken(o.second))
5977d523365SDimitry Andric     CodegenOptions.push_back(o.first);
598f785676fSDimitry Andric }
599f785676fSDimitry Andric 
parseCodeGenDebugOptions()600f785676fSDimitry Andric void LTOCodeGenerator::parseCodeGenDebugOptions() {
601f785676fSDimitry Andric   // if options were requested, set them
6027d523365SDimitry Andric   if (!CodegenOptions.empty()) {
6037d523365SDimitry Andric     // ParseCommandLineOptions() expects argv[0] to be program name.
6047d523365SDimitry Andric     std::vector<const char *> CodegenArgv(1, "libLLVMLTO");
6057d523365SDimitry Andric     for (std::string &Arg : CodegenOptions)
6067d523365SDimitry Andric       CodegenArgv.push_back(Arg.c_str());
6077d523365SDimitry Andric     cl::ParseCommandLineOptions(CodegenArgv.size(), CodegenArgv.data());
6087d523365SDimitry Andric   }
609f785676fSDimitry Andric }
61091bc56edSDimitry Andric 
61191bc56edSDimitry Andric 
DiagnosticHandler(const DiagnosticInfo & DI)6122cab237bSDimitry Andric void LTOCodeGenerator::DiagnosticHandler(const DiagnosticInfo &DI) {
61391bc56edSDimitry Andric   // Map the LLVM internal diagnostic severity to the LTO diagnostic severity.
61491bc56edSDimitry Andric   lto_codegen_diagnostic_severity_t Severity;
61591bc56edSDimitry Andric   switch (DI.getSeverity()) {
61691bc56edSDimitry Andric   case DS_Error:
61791bc56edSDimitry Andric     Severity = LTO_DS_ERROR;
61891bc56edSDimitry Andric     break;
61991bc56edSDimitry Andric   case DS_Warning:
62091bc56edSDimitry Andric     Severity = LTO_DS_WARNING;
62191bc56edSDimitry Andric     break;
62291bc56edSDimitry Andric   case DS_Remark:
62391bc56edSDimitry Andric     Severity = LTO_DS_REMARK;
62491bc56edSDimitry Andric     break;
62591bc56edSDimitry Andric   case DS_Note:
62691bc56edSDimitry Andric     Severity = LTO_DS_NOTE;
62791bc56edSDimitry Andric     break;
62891bc56edSDimitry Andric   }
62991bc56edSDimitry Andric   // Create the string that will be reported to the external diagnostic handler.
63091bc56edSDimitry Andric   std::string MsgStorage;
63191bc56edSDimitry Andric   raw_string_ostream Stream(MsgStorage);
63291bc56edSDimitry Andric   DiagnosticPrinterRawOStream DP(Stream);
63391bc56edSDimitry Andric   DI.print(DP);
63491bc56edSDimitry Andric   Stream.flush();
63591bc56edSDimitry Andric 
63691bc56edSDimitry Andric   // If this method has been called it means someone has set up an external
63791bc56edSDimitry Andric   // diagnostic handler. Assert on that.
63891bc56edSDimitry Andric   assert(DiagHandler && "Invalid diagnostic handler");
63991bc56edSDimitry Andric   (*DiagHandler)(Severity, MsgStorage.c_str(), DiagContext);
64091bc56edSDimitry Andric }
64191bc56edSDimitry Andric 
6422cab237bSDimitry Andric namespace {
6432cab237bSDimitry Andric struct LTODiagnosticHandler : public DiagnosticHandler {
6442cab237bSDimitry Andric   LTOCodeGenerator *CodeGenerator;
LTODiagnosticHandler__anonc8feaf040611::LTODiagnosticHandler6452cab237bSDimitry Andric   LTODiagnosticHandler(LTOCodeGenerator *CodeGenPtr)
6462cab237bSDimitry Andric       : CodeGenerator(CodeGenPtr) {}
handleDiagnostics__anonc8feaf040611::LTODiagnosticHandler6472cab237bSDimitry Andric   bool handleDiagnostics(const DiagnosticInfo &DI) override {
6482cab237bSDimitry Andric     CodeGenerator->DiagnosticHandler(DI);
6492cab237bSDimitry Andric     return true;
6502cab237bSDimitry Andric   }
6512cab237bSDimitry Andric };
6522cab237bSDimitry Andric }
6532cab237bSDimitry Andric 
65491bc56edSDimitry Andric void
setDiagnosticHandler(lto_diagnostic_handler_t DiagHandler,void * Ctxt)65591bc56edSDimitry Andric LTOCodeGenerator::setDiagnosticHandler(lto_diagnostic_handler_t DiagHandler,
65691bc56edSDimitry Andric                                        void *Ctxt) {
65791bc56edSDimitry Andric   this->DiagHandler = DiagHandler;
65891bc56edSDimitry Andric   this->DiagContext = Ctxt;
65991bc56edSDimitry Andric   if (!DiagHandler)
6602cab237bSDimitry Andric     return Context.setDiagnosticHandler(nullptr);
66191bc56edSDimitry Andric   // Register the LTOCodeGenerator stub in the LLVMContext to forward the
66291bc56edSDimitry Andric   // diagnostic to the external DiagHandler.
6632cab237bSDimitry Andric   Context.setDiagnosticHandler(llvm::make_unique<LTODiagnosticHandler>(this),
6642cab237bSDimitry Andric                                true);
66591bc56edSDimitry Andric }
6667d523365SDimitry Andric 
6677d523365SDimitry Andric namespace {
6687d523365SDimitry Andric class LTODiagnosticInfo : public DiagnosticInfo {
6697d523365SDimitry Andric   const Twine &Msg;
6707d523365SDimitry Andric public:
LTODiagnosticInfo(const Twine & DiagMsg,DiagnosticSeverity Severity=DS_Error)6717d523365SDimitry Andric   LTODiagnosticInfo(const Twine &DiagMsg, DiagnosticSeverity Severity=DS_Error)
6727d523365SDimitry Andric       : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {}
print(DiagnosticPrinter & DP) const6737d523365SDimitry Andric   void print(DiagnosticPrinter &DP) const override { DP << Msg; }
6747d523365SDimitry Andric };
6757d523365SDimitry Andric }
6767d523365SDimitry Andric 
emitError(const std::string & ErrMsg)6777d523365SDimitry Andric void LTOCodeGenerator::emitError(const std::string &ErrMsg) {
6787d523365SDimitry Andric   if (DiagHandler)
6797d523365SDimitry Andric     (*DiagHandler)(LTO_DS_ERROR, ErrMsg.c_str(), DiagContext);
6807d523365SDimitry Andric   else
6817d523365SDimitry Andric     Context.diagnose(LTODiagnosticInfo(ErrMsg));
6827d523365SDimitry Andric }
6833ca95b02SDimitry Andric 
emitWarning(const std::string & ErrMsg)6843ca95b02SDimitry Andric void LTOCodeGenerator::emitWarning(const std::string &ErrMsg) {
6853ca95b02SDimitry Andric   if (DiagHandler)
6863ca95b02SDimitry Andric     (*DiagHandler)(LTO_DS_WARNING, ErrMsg.c_str(), DiagContext);
6873ca95b02SDimitry Andric   else
6883ca95b02SDimitry Andric     Context.diagnose(LTODiagnosticInfo(ErrMsg, DS_Warning));
6893ca95b02SDimitry Andric }
690