1 //===--- BackendUtil.cpp - LLVM Backend Utilities -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "clang/CodeGen/BackendUtil.h"
11 #include "clang/Basic/Diagnostic.h"
12 #include "clang/Basic/LangOptions.h"
13 #include "clang/Basic/TargetOptions.h"
14 #include "clang/Frontend/CodeGenOptions.h"
15 #include "clang/Frontend/FrontendDiagnostic.h"
16 #include "clang/Frontend/Utils.h"
17 #include "clang/Lex/HeaderSearchOptions.h"
18 #include "llvm/ADT/SmallSet.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/ADT/StringSwitch.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/Analysis/TargetLibraryInfo.h"
23 #include "llvm/Analysis/TargetTransformInfo.h"
24 #include "llvm/Bitcode/BitcodeReader.h"
25 #include "llvm/Bitcode/BitcodeWriter.h"
26 #include "llvm/Bitcode/BitcodeWriterPass.h"
27 #include "llvm/CodeGen/RegAllocRegistry.h"
28 #include "llvm/CodeGen/SchedulerRegistry.h"
29 #include "llvm/CodeGen/TargetSubtargetInfo.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/IRPrintingPasses.h"
32 #include "llvm/IR/LegacyPassManager.h"
33 #include "llvm/IR/Module.h"
34 #include "llvm/IR/ModuleSummaryIndex.h"
35 #include "llvm/IR/Verifier.h"
36 #include "llvm/LTO/LTOBackend.h"
37 #include "llvm/MC/MCAsmInfo.h"
38 #include "llvm/MC/SubtargetFeature.h"
39 #include "llvm/Passes/PassBuilder.h"
40 #include "llvm/Support/CommandLine.h"
41 #include "llvm/Support/MemoryBuffer.h"
42 #include "llvm/Support/PrettyStackTrace.h"
43 #include "llvm/Support/TargetRegistry.h"
44 #include "llvm/Support/Timer.h"
45 #include "llvm/Support/raw_ostream.h"
46 #include "llvm/Target/TargetMachine.h"
47 #include "llvm/Target/TargetOptions.h"
48 #include "llvm/Transforms/Coroutines.h"
49 #include "llvm/Transforms/IPO.h"
50 #include "llvm/Transforms/IPO/AlwaysInliner.h"
51 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
52 #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
53 #include "llvm/Transforms/Instrumentation.h"
54 #include "llvm/Transforms/Instrumentation/BoundsChecking.h"
55 #include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
56 #include "llvm/Transforms/ObjCARC.h"
57 #include "llvm/Transforms/Scalar.h"
58 #include "llvm/Transforms/Scalar/GVN.h"
59 #include "llvm/Transforms/Utils.h"
60 #include "llvm/Transforms/Utils/NameAnonGlobals.h"
61 #include "llvm/Transforms/Utils/SymbolRewriter.h"
62 #include <memory>
63 using namespace clang;
64 using namespace llvm;
65 
66 namespace {
67 
68 // Default filename used for profile generation.
69 static constexpr StringLiteral DefaultProfileGenName = "default_%m.profraw";
70 
71 class EmitAssemblyHelper {
72   DiagnosticsEngine &Diags;
73   const HeaderSearchOptions &HSOpts;
74   const CodeGenOptions &CodeGenOpts;
75   const clang::TargetOptions &TargetOpts;
76   const LangOptions &LangOpts;
77   Module *TheModule;
78 
79   Timer CodeGenerationTime;
80 
81   std::unique_ptr<raw_pwrite_stream> OS;
82 
83   TargetIRAnalysis getTargetIRAnalysis() const {
84     if (TM)
85       return TM->getTargetIRAnalysis();
86 
87     return TargetIRAnalysis();
88   }
89 
90   void CreatePasses(legacy::PassManager &MPM, legacy::FunctionPassManager &FPM);
91 
92   /// Generates the TargetMachine.
93   /// Leaves TM unchanged if it is unable to create the target machine.
94   /// Some of our clang tests specify triples which are not built
95   /// into clang. This is okay because these tests check the generated
96   /// IR, and they require DataLayout which depends on the triple.
97   /// In this case, we allow this method to fail and not report an error.
98   /// When MustCreateTM is used, we print an error if we are unable to load
99   /// the requested target.
100   void CreateTargetMachine(bool MustCreateTM);
101 
102   /// Add passes necessary to emit assembly or LLVM IR.
103   ///
104   /// \return True on success.
105   bool AddEmitPasses(legacy::PassManager &CodeGenPasses, BackendAction Action,
106                      raw_pwrite_stream &OS);
107 
108 public:
109   EmitAssemblyHelper(DiagnosticsEngine &_Diags,
110                      const HeaderSearchOptions &HeaderSearchOpts,
111                      const CodeGenOptions &CGOpts,
112                      const clang::TargetOptions &TOpts,
113                      const LangOptions &LOpts, Module *M)
114       : Diags(_Diags), HSOpts(HeaderSearchOpts), CodeGenOpts(CGOpts),
115         TargetOpts(TOpts), LangOpts(LOpts), TheModule(M),
116         CodeGenerationTime("codegen", "Code Generation Time") {}
117 
118   ~EmitAssemblyHelper() {
119     if (CodeGenOpts.DisableFree)
120       BuryPointer(std::move(TM));
121   }
122 
123   std::unique_ptr<TargetMachine> TM;
124 
125   void EmitAssembly(BackendAction Action,
126                     std::unique_ptr<raw_pwrite_stream> OS);
127 
128   void EmitAssemblyWithNewPassManager(BackendAction Action,
129                                       std::unique_ptr<raw_pwrite_stream> OS);
130 };
131 
132 // We need this wrapper to access LangOpts and CGOpts from extension functions
133 // that we add to the PassManagerBuilder.
134 class PassManagerBuilderWrapper : public PassManagerBuilder {
135 public:
136   PassManagerBuilderWrapper(const Triple &TargetTriple,
137                             const CodeGenOptions &CGOpts,
138                             const LangOptions &LangOpts)
139       : PassManagerBuilder(), TargetTriple(TargetTriple), CGOpts(CGOpts),
140         LangOpts(LangOpts) {}
141   const Triple &getTargetTriple() const { return TargetTriple; }
142   const CodeGenOptions &getCGOpts() const { return CGOpts; }
143   const LangOptions &getLangOpts() const { return LangOpts; }
144 
145 private:
146   const Triple &TargetTriple;
147   const CodeGenOptions &CGOpts;
148   const LangOptions &LangOpts;
149 };
150 }
151 
152 static void addObjCARCAPElimPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
153   if (Builder.OptLevel > 0)
154     PM.add(createObjCARCAPElimPass());
155 }
156 
157 static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
158   if (Builder.OptLevel > 0)
159     PM.add(createObjCARCExpandPass());
160 }
161 
162 static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
163   if (Builder.OptLevel > 0)
164     PM.add(createObjCARCOptPass());
165 }
166 
167 static void addAddDiscriminatorsPass(const PassManagerBuilder &Builder,
168                                      legacy::PassManagerBase &PM) {
169   PM.add(createAddDiscriminatorsPass());
170 }
171 
172 static void addBoundsCheckingPass(const PassManagerBuilder &Builder,
173                                   legacy::PassManagerBase &PM) {
174   PM.add(createBoundsCheckingLegacyPass());
175 }
176 
177 static void addSanitizerCoveragePass(const PassManagerBuilder &Builder,
178                                      legacy::PassManagerBase &PM) {
179   const PassManagerBuilderWrapper &BuilderWrapper =
180       static_cast<const PassManagerBuilderWrapper&>(Builder);
181   const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
182   SanitizerCoverageOptions Opts;
183   Opts.CoverageType =
184       static_cast<SanitizerCoverageOptions::Type>(CGOpts.SanitizeCoverageType);
185   Opts.IndirectCalls = CGOpts.SanitizeCoverageIndirectCalls;
186   Opts.TraceBB = CGOpts.SanitizeCoverageTraceBB;
187   Opts.TraceCmp = CGOpts.SanitizeCoverageTraceCmp;
188   Opts.TraceDiv = CGOpts.SanitizeCoverageTraceDiv;
189   Opts.TraceGep = CGOpts.SanitizeCoverageTraceGep;
190   Opts.Use8bitCounters = CGOpts.SanitizeCoverage8bitCounters;
191   Opts.TracePC = CGOpts.SanitizeCoverageTracePC;
192   Opts.TracePCGuard = CGOpts.SanitizeCoverageTracePCGuard;
193   Opts.NoPrune = CGOpts.SanitizeCoverageNoPrune;
194   Opts.Inline8bitCounters = CGOpts.SanitizeCoverageInline8bitCounters;
195   Opts.PCTable = CGOpts.SanitizeCoveragePCTable;
196   Opts.StackDepth = CGOpts.SanitizeCoverageStackDepth;
197   PM.add(createSanitizerCoverageModulePass(Opts));
198 }
199 
200 // Check if ASan should use GC-friendly instrumentation for globals.
201 // First of all, there is no point if -fdata-sections is off (expect for MachO,
202 // where this is not a factor). Also, on ELF this feature requires an assembler
203 // extension that only works with -integrated-as at the moment.
204 static bool asanUseGlobalsGC(const Triple &T, const CodeGenOptions &CGOpts) {
205   if (!CGOpts.SanitizeAddressGlobalsDeadStripping)
206     return false;
207   switch (T.getObjectFormat()) {
208   case Triple::MachO:
209   case Triple::COFF:
210     return true;
211   case Triple::ELF:
212     return CGOpts.DataSections && !CGOpts.DisableIntegratedAS;
213   default:
214     return false;
215   }
216 }
217 
218 static void addAddressSanitizerPasses(const PassManagerBuilder &Builder,
219                                       legacy::PassManagerBase &PM) {
220   const PassManagerBuilderWrapper &BuilderWrapper =
221       static_cast<const PassManagerBuilderWrapper&>(Builder);
222   const Triple &T = BuilderWrapper.getTargetTriple();
223   const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
224   bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Address);
225   bool UseAfterScope = CGOpts.SanitizeAddressUseAfterScope;
226   bool UseGlobalsGC = asanUseGlobalsGC(T, CGOpts);
227   PM.add(createAddressSanitizerFunctionPass(/*CompileKernel*/ false, Recover,
228                                             UseAfterScope));
229   PM.add(createAddressSanitizerModulePass(/*CompileKernel*/ false, Recover,
230                                           UseGlobalsGC));
231 }
232 
233 static void addKernelAddressSanitizerPasses(const PassManagerBuilder &Builder,
234                                             legacy::PassManagerBase &PM) {
235   PM.add(createAddressSanitizerFunctionPass(
236       /*CompileKernel*/ true, /*Recover*/ true, /*UseAfterScope*/ false));
237   PM.add(createAddressSanitizerModulePass(
238       /*CompileKernel*/ true, /*Recover*/ true));
239 }
240 
241 static void addHWAddressSanitizerPasses(const PassManagerBuilder &Builder,
242                                             legacy::PassManagerBase &PM) {
243   const PassManagerBuilderWrapper &BuilderWrapper =
244       static_cast<const PassManagerBuilderWrapper &>(Builder);
245   const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
246   bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::HWAddress);
247   PM.add(createHWAddressSanitizerPass(/*CompileKernel*/ false, Recover));
248 }
249 
250 static void addKernelHWAddressSanitizerPasses(const PassManagerBuilder &Builder,
251                                             legacy::PassManagerBase &PM) {
252   PM.add(createHWAddressSanitizerPass(
253       /*CompileKernel*/ true, /*Recover*/ true));
254 }
255 
256 static void addMemorySanitizerPass(const PassManagerBuilder &Builder,
257                                    legacy::PassManagerBase &PM) {
258   const PassManagerBuilderWrapper &BuilderWrapper =
259       static_cast<const PassManagerBuilderWrapper&>(Builder);
260   const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
261   int TrackOrigins = CGOpts.SanitizeMemoryTrackOrigins;
262   bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Memory);
263   PM.add(createMemorySanitizerPass(TrackOrigins, Recover));
264 
265   // MemorySanitizer inserts complex instrumentation that mostly follows
266   // the logic of the original code, but operates on "shadow" values.
267   // It can benefit from re-running some general purpose optimization passes.
268   if (Builder.OptLevel > 0) {
269     PM.add(createEarlyCSEPass());
270     PM.add(createReassociatePass());
271     PM.add(createLICMPass());
272     PM.add(createGVNPass());
273     PM.add(createInstructionCombiningPass());
274     PM.add(createDeadStoreEliminationPass());
275   }
276 }
277 
278 static void addThreadSanitizerPass(const PassManagerBuilder &Builder,
279                                    legacy::PassManagerBase &PM) {
280   PM.add(createThreadSanitizerPass());
281 }
282 
283 static void addDataFlowSanitizerPass(const PassManagerBuilder &Builder,
284                                      legacy::PassManagerBase &PM) {
285   const PassManagerBuilderWrapper &BuilderWrapper =
286       static_cast<const PassManagerBuilderWrapper&>(Builder);
287   const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
288   PM.add(createDataFlowSanitizerPass(LangOpts.SanitizerBlacklistFiles));
289 }
290 
291 static void addEfficiencySanitizerPass(const PassManagerBuilder &Builder,
292                                        legacy::PassManagerBase &PM) {
293   const PassManagerBuilderWrapper &BuilderWrapper =
294       static_cast<const PassManagerBuilderWrapper&>(Builder);
295   const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
296   EfficiencySanitizerOptions Opts;
297   if (LangOpts.Sanitize.has(SanitizerKind::EfficiencyCacheFrag))
298     Opts.ToolType = EfficiencySanitizerOptions::ESAN_CacheFrag;
299   else if (LangOpts.Sanitize.has(SanitizerKind::EfficiencyWorkingSet))
300     Opts.ToolType = EfficiencySanitizerOptions::ESAN_WorkingSet;
301   PM.add(createEfficiencySanitizerPass(Opts));
302 }
303 
304 static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple,
305                                          const CodeGenOptions &CodeGenOpts) {
306   TargetLibraryInfoImpl *TLII = new TargetLibraryInfoImpl(TargetTriple);
307   if (!CodeGenOpts.SimplifyLibCalls)
308     TLII->disableAllFunctions();
309   else {
310     // Disable individual libc/libm calls in TargetLibraryInfo.
311     LibFunc F;
312     for (auto &FuncName : CodeGenOpts.getNoBuiltinFuncs())
313       if (TLII->getLibFunc(FuncName, F))
314         TLII->setUnavailable(F);
315   }
316 
317   switch (CodeGenOpts.getVecLib()) {
318   case CodeGenOptions::Accelerate:
319     TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::Accelerate);
320     break;
321   case CodeGenOptions::SVML:
322     TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::SVML);
323     break;
324   default:
325     break;
326   }
327   return TLII;
328 }
329 
330 static void addSymbolRewriterPass(const CodeGenOptions &Opts,
331                                   legacy::PassManager *MPM) {
332   llvm::SymbolRewriter::RewriteDescriptorList DL;
333 
334   llvm::SymbolRewriter::RewriteMapParser MapParser;
335   for (const auto &MapFile : Opts.RewriteMapFiles)
336     MapParser.parse(MapFile, &DL);
337 
338   MPM->add(createRewriteSymbolsPass(DL));
339 }
340 
341 static CodeGenOpt::Level getCGOptLevel(const CodeGenOptions &CodeGenOpts) {
342   switch (CodeGenOpts.OptimizationLevel) {
343   default:
344     llvm_unreachable("Invalid optimization level!");
345   case 0:
346     return CodeGenOpt::None;
347   case 1:
348     return CodeGenOpt::Less;
349   case 2:
350     return CodeGenOpt::Default; // O2/Os/Oz
351   case 3:
352     return CodeGenOpt::Aggressive;
353   }
354 }
355 
356 static Optional<llvm::CodeModel::Model>
357 getCodeModel(const CodeGenOptions &CodeGenOpts) {
358   unsigned CodeModel = llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel)
359                            .Case("small", llvm::CodeModel::Small)
360                            .Case("kernel", llvm::CodeModel::Kernel)
361                            .Case("medium", llvm::CodeModel::Medium)
362                            .Case("large", llvm::CodeModel::Large)
363                            .Case("default", ~1u)
364                            .Default(~0u);
365   assert(CodeModel != ~0u && "invalid code model!");
366   if (CodeModel == ~1u)
367     return None;
368   return static_cast<llvm::CodeModel::Model>(CodeModel);
369 }
370 
371 static TargetMachine::CodeGenFileType getCodeGenFileType(BackendAction Action) {
372   if (Action == Backend_EmitObj)
373     return TargetMachine::CGFT_ObjectFile;
374   else if (Action == Backend_EmitMCNull)
375     return TargetMachine::CGFT_Null;
376   else {
377     assert(Action == Backend_EmitAssembly && "Invalid action!");
378     return TargetMachine::CGFT_AssemblyFile;
379   }
380 }
381 
382 static void initTargetOptions(llvm::TargetOptions &Options,
383                               const CodeGenOptions &CodeGenOpts,
384                               const clang::TargetOptions &TargetOpts,
385                               const LangOptions &LangOpts,
386                               const HeaderSearchOptions &HSOpts) {
387   Options.ThreadModel =
388       llvm::StringSwitch<llvm::ThreadModel::Model>(CodeGenOpts.ThreadModel)
389           .Case("posix", llvm::ThreadModel::POSIX)
390           .Case("single", llvm::ThreadModel::Single);
391 
392   // Set float ABI type.
393   assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" ||
394           CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) &&
395          "Invalid Floating Point ABI!");
396   Options.FloatABIType =
397       llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI)
398           .Case("soft", llvm::FloatABI::Soft)
399           .Case("softfp", llvm::FloatABI::Soft)
400           .Case("hard", llvm::FloatABI::Hard)
401           .Default(llvm::FloatABI::Default);
402 
403   // Set FP fusion mode.
404   switch (LangOpts.getDefaultFPContractMode()) {
405   case LangOptions::FPC_Off:
406     // Preserve any contraction performed by the front-end.  (Strict performs
407     // splitting of the muladd instrinsic in the backend.)
408     Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
409     break;
410   case LangOptions::FPC_On:
411     Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
412     break;
413   case LangOptions::FPC_Fast:
414     Options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
415     break;
416   }
417 
418   Options.UseInitArray = CodeGenOpts.UseInitArray;
419   Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS;
420   Options.CompressDebugSections = CodeGenOpts.getCompressDebugSections();
421   Options.RelaxELFRelocations = CodeGenOpts.RelaxELFRelocations;
422 
423   // Set EABI version.
424   Options.EABIVersion = TargetOpts.EABIVersion;
425 
426   if (LangOpts.SjLjExceptions)
427     Options.ExceptionModel = llvm::ExceptionHandling::SjLj;
428   if (LangOpts.SEHExceptions)
429     Options.ExceptionModel = llvm::ExceptionHandling::WinEH;
430   if (LangOpts.DWARFExceptions)
431     Options.ExceptionModel = llvm::ExceptionHandling::DwarfCFI;
432 
433   Options.NoInfsFPMath = CodeGenOpts.NoInfsFPMath;
434   Options.NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath;
435   Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
436   Options.UnsafeFPMath = CodeGenOpts.UnsafeFPMath;
437   Options.StackAlignmentOverride = CodeGenOpts.StackAlignment;
438   Options.FunctionSections = CodeGenOpts.FunctionSections;
439   Options.DataSections = CodeGenOpts.DataSections;
440   Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames;
441   Options.EmulatedTLS = CodeGenOpts.EmulatedTLS;
442   Options.ExplicitEmulatedTLS = CodeGenOpts.ExplicitEmulatedTLS;
443   Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning();
444   Options.EmitStackSizeSection = CodeGenOpts.StackSizeSection;
445 
446   if (CodeGenOpts.EnableSplitDwarf)
447     Options.MCOptions.SplitDwarfFile = CodeGenOpts.SplitDwarfFile;
448   Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll;
449   Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels;
450   Options.MCOptions.MCUseDwarfDirectory = !CodeGenOpts.NoDwarfDirectoryAsm;
451   Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack;
452   Options.MCOptions.MCIncrementalLinkerCompatible =
453       CodeGenOpts.IncrementalLinkerCompatible;
454   Options.MCOptions.MCPIECopyRelocations = CodeGenOpts.PIECopyRelocations;
455   Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings;
456   Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose;
457   Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments;
458   Options.MCOptions.ABIName = TargetOpts.ABI;
459   for (const auto &Entry : HSOpts.UserEntries)
460     if (!Entry.IsFramework &&
461         (Entry.Group == frontend::IncludeDirGroup::Quoted ||
462          Entry.Group == frontend::IncludeDirGroup::Angled ||
463          Entry.Group == frontend::IncludeDirGroup::System))
464       Options.MCOptions.IASSearchPaths.push_back(
465           Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path);
466 }
467 static Optional<GCOVOptions> getGCOVOptions(const CodeGenOptions &CodeGenOpts) {
468   if (CodeGenOpts.DisableGCov)
469     return None;
470   if (!CodeGenOpts.EmitGcovArcs && !CodeGenOpts.EmitGcovNotes)
471     return None;
472   // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if
473   // LLVM's -default-gcov-version flag is set to something invalid.
474   GCOVOptions Options;
475   Options.EmitNotes = CodeGenOpts.EmitGcovNotes;
476   Options.EmitData = CodeGenOpts.EmitGcovArcs;
477   llvm::copy(CodeGenOpts.CoverageVersion, std::begin(Options.Version));
478   Options.UseCfgChecksum = CodeGenOpts.CoverageExtraChecksum;
479   Options.NoRedZone = CodeGenOpts.DisableRedZone;
480   Options.FunctionNamesInData = !CodeGenOpts.CoverageNoFunctionNamesInData;
481   Options.ExitBlockBeforeBody = CodeGenOpts.CoverageExitBlockBeforeBody;
482   return Options;
483 }
484 
485 void EmitAssemblyHelper::CreatePasses(legacy::PassManager &MPM,
486                                       legacy::FunctionPassManager &FPM) {
487   // Handle disabling of all LLVM passes, where we want to preserve the
488   // internal module before any optimization.
489   if (CodeGenOpts.DisableLLVMPasses)
490     return;
491 
492   // Figure out TargetLibraryInfo.  This needs to be added to MPM and FPM
493   // manually (and not via PMBuilder), since some passes (eg. InstrProfiling)
494   // are inserted before PMBuilder ones - they'd get the default-constructed
495   // TLI with an unknown target otherwise.
496   Triple TargetTriple(TheModule->getTargetTriple());
497   std::unique_ptr<TargetLibraryInfoImpl> TLII(
498       createTLII(TargetTriple, CodeGenOpts));
499 
500   PassManagerBuilderWrapper PMBuilder(TargetTriple, CodeGenOpts, LangOpts);
501 
502   // At O0 and O1 we only run the always inliner which is more efficient. At
503   // higher optimization levels we run the normal inliner.
504   if (CodeGenOpts.OptimizationLevel <= 1) {
505     bool InsertLifetimeIntrinsics = (CodeGenOpts.OptimizationLevel != 0 &&
506                                      !CodeGenOpts.DisableLifetimeMarkers);
507     PMBuilder.Inliner = createAlwaysInlinerLegacyPass(InsertLifetimeIntrinsics);
508   } else {
509     // We do not want to inline hot callsites for SamplePGO module-summary build
510     // because profile annotation will happen again in ThinLTO backend, and we
511     // want the IR of the hot path to match the profile.
512     PMBuilder.Inliner = createFunctionInliningPass(
513         CodeGenOpts.OptimizationLevel, CodeGenOpts.OptimizeSize,
514         (!CodeGenOpts.SampleProfileFile.empty() &&
515          CodeGenOpts.EmitSummaryIndex));
516   }
517 
518   PMBuilder.OptLevel = CodeGenOpts.OptimizationLevel;
519   PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize;
520   PMBuilder.SLPVectorize = CodeGenOpts.VectorizeSLP;
521   PMBuilder.LoopVectorize = CodeGenOpts.VectorizeLoop;
522 
523   PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
524   PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions;
525   PMBuilder.PrepareForThinLTO = CodeGenOpts.EmitSummaryIndex;
526   PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
527   PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
528 
529   MPM.add(new TargetLibraryInfoWrapperPass(*TLII));
530 
531   if (TM)
532     TM->adjustPassManager(PMBuilder);
533 
534   if (CodeGenOpts.DebugInfoForProfiling ||
535       !CodeGenOpts.SampleProfileFile.empty())
536     PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
537                            addAddDiscriminatorsPass);
538 
539   // In ObjC ARC mode, add the main ARC optimization passes.
540   if (LangOpts.ObjCAutoRefCount) {
541     PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
542                            addObjCARCExpandPass);
543     PMBuilder.addExtension(PassManagerBuilder::EP_ModuleOptimizerEarly,
544                            addObjCARCAPElimPass);
545     PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
546                            addObjCARCOptPass);
547   }
548 
549   if (LangOpts.CoroutinesTS)
550     addCoroutinePassesToExtensionPoints(PMBuilder);
551 
552   if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) {
553     PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
554                            addBoundsCheckingPass);
555     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
556                            addBoundsCheckingPass);
557   }
558 
559   if (CodeGenOpts.SanitizeCoverageType ||
560       CodeGenOpts.SanitizeCoverageIndirectCalls ||
561       CodeGenOpts.SanitizeCoverageTraceCmp) {
562     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
563                            addSanitizerCoveragePass);
564     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
565                            addSanitizerCoveragePass);
566   }
567 
568   if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
569     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
570                            addAddressSanitizerPasses);
571     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
572                            addAddressSanitizerPasses);
573   }
574 
575   if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) {
576     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
577                            addKernelAddressSanitizerPasses);
578     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
579                            addKernelAddressSanitizerPasses);
580   }
581 
582   if (LangOpts.Sanitize.has(SanitizerKind::HWAddress)) {
583     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
584                            addHWAddressSanitizerPasses);
585     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
586                            addHWAddressSanitizerPasses);
587   }
588 
589   if (LangOpts.Sanitize.has(SanitizerKind::KernelHWAddress)) {
590     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
591                            addKernelHWAddressSanitizerPasses);
592     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
593                            addKernelHWAddressSanitizerPasses);
594   }
595 
596   if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
597     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
598                            addMemorySanitizerPass);
599     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
600                            addMemorySanitizerPass);
601   }
602 
603   if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
604     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
605                            addThreadSanitizerPass);
606     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
607                            addThreadSanitizerPass);
608   }
609 
610   if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
611     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
612                            addDataFlowSanitizerPass);
613     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
614                            addDataFlowSanitizerPass);
615   }
616 
617   if (LangOpts.Sanitize.hasOneOf(SanitizerKind::Efficiency)) {
618     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
619                            addEfficiencySanitizerPass);
620     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
621                            addEfficiencySanitizerPass);
622   }
623 
624   // Set up the per-function pass manager.
625   FPM.add(new TargetLibraryInfoWrapperPass(*TLII));
626   if (CodeGenOpts.VerifyModule)
627     FPM.add(createVerifierPass());
628 
629   // Set up the per-module pass manager.
630   if (!CodeGenOpts.RewriteMapFiles.empty())
631     addSymbolRewriterPass(CodeGenOpts, &MPM);
632 
633   if (Optional<GCOVOptions> Options = getGCOVOptions(CodeGenOpts)) {
634     MPM.add(createGCOVProfilerPass(*Options));
635     if (CodeGenOpts.getDebugInfo() == codegenoptions::NoDebugInfo)
636       MPM.add(createStripSymbolsPass(true));
637   }
638 
639   if (CodeGenOpts.hasProfileClangInstr()) {
640     InstrProfOptions Options;
641     Options.NoRedZone = CodeGenOpts.DisableRedZone;
642     Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput;
643     MPM.add(createInstrProfilingLegacyPass(Options));
644   }
645   if (CodeGenOpts.hasProfileIRInstr()) {
646     PMBuilder.EnablePGOInstrGen = true;
647     if (!CodeGenOpts.InstrProfileOutput.empty())
648       PMBuilder.PGOInstrGen = CodeGenOpts.InstrProfileOutput;
649     else
650       PMBuilder.PGOInstrGen = DefaultProfileGenName;
651   }
652   if (CodeGenOpts.hasProfileIRUse())
653     PMBuilder.PGOInstrUse = CodeGenOpts.ProfileInstrumentUsePath;
654 
655   if (!CodeGenOpts.SampleProfileFile.empty())
656     PMBuilder.PGOSampleUse = CodeGenOpts.SampleProfileFile;
657 
658   PMBuilder.populateFunctionPassManager(FPM);
659   PMBuilder.populateModulePassManager(MPM);
660 }
661 
662 static void setCommandLineOpts(const CodeGenOptions &CodeGenOpts) {
663   SmallVector<const char *, 16> BackendArgs;
664   BackendArgs.push_back("clang"); // Fake program name.
665   if (!CodeGenOpts.DebugPass.empty()) {
666     BackendArgs.push_back("-debug-pass");
667     BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
668   }
669   if (!CodeGenOpts.LimitFloatPrecision.empty()) {
670     BackendArgs.push_back("-limit-float-precision");
671     BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
672   }
673   BackendArgs.push_back(nullptr);
674   llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
675                                     BackendArgs.data());
676 }
677 
678 void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
679   // Create the TargetMachine for generating code.
680   std::string Error;
681   std::string Triple = TheModule->getTargetTriple();
682   const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
683   if (!TheTarget) {
684     if (MustCreateTM)
685       Diags.Report(diag::err_fe_unable_to_create_target) << Error;
686     return;
687   }
688 
689   Optional<llvm::CodeModel::Model> CM = getCodeModel(CodeGenOpts);
690   std::string FeaturesStr =
691       llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ",");
692   llvm::Reloc::Model RM = CodeGenOpts.RelocationModel;
693   CodeGenOpt::Level OptLevel = getCGOptLevel(CodeGenOpts);
694 
695   llvm::TargetOptions Options;
696   initTargetOptions(Options, CodeGenOpts, TargetOpts, LangOpts, HSOpts);
697   TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr,
698                                           Options, RM, CM, OptLevel));
699 }
700 
701 bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses,
702                                        BackendAction Action,
703                                        raw_pwrite_stream &OS) {
704   // Add LibraryInfo.
705   llvm::Triple TargetTriple(TheModule->getTargetTriple());
706   std::unique_ptr<TargetLibraryInfoImpl> TLII(
707       createTLII(TargetTriple, CodeGenOpts));
708   CodeGenPasses.add(new TargetLibraryInfoWrapperPass(*TLII));
709 
710   // Normal mode, emit a .s or .o file by running the code generator. Note,
711   // this also adds codegenerator level optimization passes.
712   TargetMachine::CodeGenFileType CGFT = getCodeGenFileType(Action);
713 
714   // Add ObjC ARC final-cleanup optimizations. This is done as part of the
715   // "codegen" passes so that it isn't run multiple times when there is
716   // inlining happening.
717   if (CodeGenOpts.OptimizationLevel > 0)
718     CodeGenPasses.add(createObjCARCContractPass());
719 
720   if (TM->addPassesToEmitFile(CodeGenPasses, OS, CGFT,
721                               /*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
722     Diags.Report(diag::err_fe_unable_to_interface_with_target);
723     return false;
724   }
725 
726   return true;
727 }
728 
729 void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
730                                       std::unique_ptr<raw_pwrite_stream> OS) {
731   TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr);
732 
733   setCommandLineOpts(CodeGenOpts);
734 
735   bool UsesCodeGen = (Action != Backend_EmitNothing &&
736                       Action != Backend_EmitBC &&
737                       Action != Backend_EmitLL);
738   CreateTargetMachine(UsesCodeGen);
739 
740   if (UsesCodeGen && !TM)
741     return;
742   if (TM)
743     TheModule->setDataLayout(TM->createDataLayout());
744 
745   legacy::PassManager PerModulePasses;
746   PerModulePasses.add(
747       createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
748 
749   legacy::FunctionPassManager PerFunctionPasses(TheModule);
750   PerFunctionPasses.add(
751       createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
752 
753   CreatePasses(PerModulePasses, PerFunctionPasses);
754 
755   legacy::PassManager CodeGenPasses;
756   CodeGenPasses.add(
757       createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
758 
759   std::unique_ptr<raw_fd_ostream> ThinLinkOS;
760 
761   switch (Action) {
762   case Backend_EmitNothing:
763     break;
764 
765   case Backend_EmitBC:
766     if (CodeGenOpts.EmitSummaryIndex) {
767       if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
768         std::error_code EC;
769         ThinLinkOS.reset(new llvm::raw_fd_ostream(
770             CodeGenOpts.ThinLinkBitcodeFile, EC,
771             llvm::sys::fs::F_None));
772         if (EC) {
773           Diags.Report(diag::err_fe_unable_to_open_output) << CodeGenOpts.ThinLinkBitcodeFile
774                                                            << EC.message();
775           return;
776         }
777       }
778       PerModulePasses.add(
779           createWriteThinLTOBitcodePass(*OS, ThinLinkOS.get()));
780     }
781     else
782       PerModulePasses.add(
783           createBitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists));
784     break;
785 
786   case Backend_EmitLL:
787     PerModulePasses.add(
788         createPrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
789     break;
790 
791   default:
792     if (!AddEmitPasses(CodeGenPasses, Action, *OS))
793       return;
794   }
795 
796   // Before executing passes, print the final values of the LLVM options.
797   cl::PrintOptionValues();
798 
799   // Run passes. For now we do all passes at once, but eventually we
800   // would like to have the option of streaming code generation.
801 
802   {
803     PrettyStackTraceString CrashInfo("Per-function optimization");
804 
805     PerFunctionPasses.doInitialization();
806     for (Function &F : *TheModule)
807       if (!F.isDeclaration())
808         PerFunctionPasses.run(F);
809     PerFunctionPasses.doFinalization();
810   }
811 
812   {
813     PrettyStackTraceString CrashInfo("Per-module optimization passes");
814     PerModulePasses.run(*TheModule);
815   }
816 
817   {
818     PrettyStackTraceString CrashInfo("Code generation");
819     CodeGenPasses.run(*TheModule);
820   }
821 }
822 
823 static PassBuilder::OptimizationLevel mapToLevel(const CodeGenOptions &Opts) {
824   switch (Opts.OptimizationLevel) {
825   default:
826     llvm_unreachable("Invalid optimization level!");
827 
828   case 1:
829     return PassBuilder::O1;
830 
831   case 2:
832     switch (Opts.OptimizeSize) {
833     default:
834       llvm_unreachable("Invalid optimization level for size!");
835 
836     case 0:
837       return PassBuilder::O2;
838 
839     case 1:
840       return PassBuilder::Os;
841 
842     case 2:
843       return PassBuilder::Oz;
844     }
845 
846   case 3:
847     return PassBuilder::O3;
848   }
849 }
850 
851 /// A clean version of `EmitAssembly` that uses the new pass manager.
852 ///
853 /// Not all features are currently supported in this system, but where
854 /// necessary it falls back to the legacy pass manager to at least provide
855 /// basic functionality.
856 ///
857 /// This API is planned to have its functionality finished and then to replace
858 /// `EmitAssembly` at some point in the future when the default switches.
859 void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
860     BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS) {
861   TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr);
862   setCommandLineOpts(CodeGenOpts);
863 
864   // The new pass manager always makes a target machine available to passes
865   // during construction.
866   CreateTargetMachine(/*MustCreateTM*/ true);
867   if (!TM)
868     // This will already be diagnosed, just bail.
869     return;
870   TheModule->setDataLayout(TM->createDataLayout());
871 
872   Optional<PGOOptions> PGOOpt;
873 
874   if (CodeGenOpts.hasProfileIRInstr())
875     // -fprofile-generate.
876     PGOOpt = PGOOptions(CodeGenOpts.InstrProfileOutput.empty()
877                             ? DefaultProfileGenName
878                             : CodeGenOpts.InstrProfileOutput,
879                         "", "", true, CodeGenOpts.DebugInfoForProfiling);
880   else if (CodeGenOpts.hasProfileIRUse())
881     // -fprofile-use.
882     PGOOpt = PGOOptions("", CodeGenOpts.ProfileInstrumentUsePath, "", false,
883                         CodeGenOpts.DebugInfoForProfiling);
884   else if (!CodeGenOpts.SampleProfileFile.empty())
885     // -fprofile-sample-use
886     PGOOpt = PGOOptions("", "", CodeGenOpts.SampleProfileFile, false,
887                         CodeGenOpts.DebugInfoForProfiling);
888   else if (CodeGenOpts.DebugInfoForProfiling)
889     // -fdebug-info-for-profiling
890     PGOOpt = PGOOptions("", "", "", false, true);
891 
892   PassBuilder PB(TM.get(), PGOOpt);
893 
894   LoopAnalysisManager LAM(CodeGenOpts.DebugPassManager);
895   FunctionAnalysisManager FAM(CodeGenOpts.DebugPassManager);
896   CGSCCAnalysisManager CGAM(CodeGenOpts.DebugPassManager);
897   ModuleAnalysisManager MAM(CodeGenOpts.DebugPassManager);
898 
899   // Register the AA manager first so that our version is the one used.
900   FAM.registerPass([&] { return PB.buildDefaultAAPipeline(); });
901 
902   // Register the target library analysis directly and give it a customized
903   // preset TLI.
904   Triple TargetTriple(TheModule->getTargetTriple());
905   std::unique_ptr<TargetLibraryInfoImpl> TLII(
906       createTLII(TargetTriple, CodeGenOpts));
907   FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
908   MAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
909 
910   // Register all the basic analyses with the managers.
911   PB.registerModuleAnalyses(MAM);
912   PB.registerCGSCCAnalyses(CGAM);
913   PB.registerFunctionAnalyses(FAM);
914   PB.registerLoopAnalyses(LAM);
915   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
916 
917   ModulePassManager MPM(CodeGenOpts.DebugPassManager);
918 
919   if (!CodeGenOpts.DisableLLVMPasses) {
920     bool IsThinLTO = CodeGenOpts.EmitSummaryIndex;
921     bool IsLTO = CodeGenOpts.PrepareForLTO;
922 
923     if (CodeGenOpts.OptimizationLevel == 0) {
924       if (Optional<GCOVOptions> Options = getGCOVOptions(CodeGenOpts))
925         MPM.addPass(GCOVProfilerPass(*Options));
926 
927       // Build a minimal pipeline based on the semantics required by Clang,
928       // which is just that always inlining occurs.
929       MPM.addPass(AlwaysInlinerPass());
930 
931       // At -O0 we directly run necessary sanitizer passes.
932       if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds))
933         MPM.addPass(createModuleToFunctionPassAdaptor(BoundsCheckingPass()));
934 
935       // Lastly, add a semantically necessary pass for ThinLTO.
936       if (IsThinLTO)
937         MPM.addPass(NameAnonGlobalPass());
938     } else {
939       // Map our optimization levels into one of the distinct levels used to
940       // configure the pipeline.
941       PassBuilder::OptimizationLevel Level = mapToLevel(CodeGenOpts);
942 
943       // Register callbacks to schedule sanitizer passes at the appropriate part of
944       // the pipeline.
945       if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds))
946         PB.registerScalarOptimizerLateEPCallback(
947             [](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
948               FPM.addPass(BoundsCheckingPass());
949             });
950       if (Optional<GCOVOptions> Options = getGCOVOptions(CodeGenOpts))
951         PB.registerPipelineStartEPCallback([Options](ModulePassManager &MPM) {
952           MPM.addPass(GCOVProfilerPass(*Options));
953         });
954 
955       if (IsThinLTO) {
956         MPM = PB.buildThinLTOPreLinkDefaultPipeline(
957             Level, CodeGenOpts.DebugPassManager);
958         MPM.addPass(NameAnonGlobalPass());
959       } else if (IsLTO) {
960         MPM = PB.buildLTOPreLinkDefaultPipeline(Level,
961                                                 CodeGenOpts.DebugPassManager);
962       } else {
963         MPM = PB.buildPerModuleDefaultPipeline(Level,
964                                                CodeGenOpts.DebugPassManager);
965       }
966     }
967   }
968 
969   // FIXME: We still use the legacy pass manager to do code generation. We
970   // create that pass manager here and use it as needed below.
971   legacy::PassManager CodeGenPasses;
972   bool NeedCodeGen = false;
973   Optional<raw_fd_ostream> ThinLinkOS;
974 
975   // Append any output we need to the pass manager.
976   switch (Action) {
977   case Backend_EmitNothing:
978     break;
979 
980   case Backend_EmitBC:
981     if (CodeGenOpts.EmitSummaryIndex) {
982       if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
983         std::error_code EC;
984         ThinLinkOS.emplace(CodeGenOpts.ThinLinkBitcodeFile, EC,
985                            llvm::sys::fs::F_None);
986         if (EC) {
987           Diags.Report(diag::err_fe_unable_to_open_output)
988               << CodeGenOpts.ThinLinkBitcodeFile << EC.message();
989           return;
990         }
991       }
992       MPM.addPass(
993           ThinLTOBitcodeWriterPass(*OS, ThinLinkOS ? &*ThinLinkOS : nullptr));
994     } else {
995       MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
996                                     CodeGenOpts.EmitSummaryIndex,
997                                     CodeGenOpts.EmitSummaryIndex));
998     }
999     break;
1000 
1001   case Backend_EmitLL:
1002     MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
1003     break;
1004 
1005   case Backend_EmitAssembly:
1006   case Backend_EmitMCNull:
1007   case Backend_EmitObj:
1008     NeedCodeGen = true;
1009     CodeGenPasses.add(
1010         createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
1011     if (!AddEmitPasses(CodeGenPasses, Action, *OS))
1012       // FIXME: Should we handle this error differently?
1013       return;
1014     break;
1015   }
1016 
1017   // Before executing passes, print the final values of the LLVM options.
1018   cl::PrintOptionValues();
1019 
1020   // Now that we have all of the passes ready, run them.
1021   {
1022     PrettyStackTraceString CrashInfo("Optimizer");
1023     MPM.run(*TheModule, MAM);
1024   }
1025 
1026   // Now if needed, run the legacy PM for codegen.
1027   if (NeedCodeGen) {
1028     PrettyStackTraceString CrashInfo("Code generation");
1029     CodeGenPasses.run(*TheModule);
1030   }
1031 }
1032 
1033 Expected<BitcodeModule> clang::FindThinLTOModule(MemoryBufferRef MBRef) {
1034   Expected<std::vector<BitcodeModule>> BMsOrErr = getBitcodeModuleList(MBRef);
1035   if (!BMsOrErr)
1036     return BMsOrErr.takeError();
1037 
1038   // The bitcode file may contain multiple modules, we want the one that is
1039   // marked as being the ThinLTO module.
1040   if (const BitcodeModule *Bm = FindThinLTOModule(*BMsOrErr))
1041     return *Bm;
1042 
1043   return make_error<StringError>("Could not find module summary",
1044                                  inconvertibleErrorCode());
1045 }
1046 
1047 BitcodeModule *clang::FindThinLTOModule(MutableArrayRef<BitcodeModule> BMs) {
1048   for (BitcodeModule &BM : BMs) {
1049     Expected<BitcodeLTOInfo> LTOInfo = BM.getLTOInfo();
1050     if (LTOInfo && LTOInfo->IsThinLTO)
1051       return &BM;
1052   }
1053   return nullptr;
1054 }
1055 
1056 static void runThinLTOBackend(ModuleSummaryIndex *CombinedIndex, Module *M,
1057                               const HeaderSearchOptions &HeaderOpts,
1058                               const CodeGenOptions &CGOpts,
1059                               const clang::TargetOptions &TOpts,
1060                               const LangOptions &LOpts,
1061                               std::unique_ptr<raw_pwrite_stream> OS,
1062                               std::string SampleProfile,
1063                               BackendAction Action) {
1064   StringMap<DenseMap<GlobalValue::GUID, GlobalValueSummary *>>
1065       ModuleToDefinedGVSummaries;
1066   CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
1067 
1068   setCommandLineOpts(CGOpts);
1069 
1070   // We can simply import the values mentioned in the combined index, since
1071   // we should only invoke this using the individual indexes written out
1072   // via a WriteIndexesThinBackend.
1073   FunctionImporter::ImportMapTy ImportList;
1074   for (auto &GlobalList : *CombinedIndex) {
1075     // Ignore entries for undefined references.
1076     if (GlobalList.second.SummaryList.empty())
1077       continue;
1078 
1079     auto GUID = GlobalList.first;
1080     assert(GlobalList.second.SummaryList.size() == 1 &&
1081            "Expected individual combined index to have one summary per GUID");
1082     auto &Summary = GlobalList.second.SummaryList[0];
1083     // Skip the summaries for the importing module. These are included to
1084     // e.g. record required linkage changes.
1085     if (Summary->modulePath() == M->getModuleIdentifier())
1086       continue;
1087     // Doesn't matter what value we plug in to the map, just needs an entry
1088     // to provoke importing by thinBackend.
1089     ImportList[Summary->modulePath()][GUID] = 1;
1090   }
1091 
1092   std::vector<std::unique_ptr<llvm::MemoryBuffer>> OwnedImports;
1093   MapVector<llvm::StringRef, llvm::BitcodeModule> ModuleMap;
1094 
1095   for (auto &I : ImportList) {
1096     ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MBOrErr =
1097         llvm::MemoryBuffer::getFile(I.first());
1098     if (!MBOrErr) {
1099       errs() << "Error loading imported file '" << I.first()
1100              << "': " << MBOrErr.getError().message() << "\n";
1101       return;
1102     }
1103 
1104     Expected<BitcodeModule> BMOrErr = FindThinLTOModule(**MBOrErr);
1105     if (!BMOrErr) {
1106       handleAllErrors(BMOrErr.takeError(), [&](ErrorInfoBase &EIB) {
1107         errs() << "Error loading imported file '" << I.first()
1108                << "': " << EIB.message() << '\n';
1109       });
1110       return;
1111     }
1112     ModuleMap.insert({I.first(), *BMOrErr});
1113 
1114     OwnedImports.push_back(std::move(*MBOrErr));
1115   }
1116   auto AddStream = [&](size_t Task) {
1117     return llvm::make_unique<lto::NativeObjectStream>(std::move(OS));
1118   };
1119   lto::Config Conf;
1120   if (CGOpts.SaveTempsFilePrefix != "") {
1121     if (Error E = Conf.addSaveTemps(CGOpts.SaveTempsFilePrefix + ".",
1122                                     /* UseInputModulePath */ false)) {
1123       handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
1124         errs() << "Error setting up ThinLTO save-temps: " << EIB.message()
1125                << '\n';
1126       });
1127     }
1128   }
1129   Conf.CPU = TOpts.CPU;
1130   Conf.CodeModel = getCodeModel(CGOpts);
1131   Conf.MAttrs = TOpts.Features;
1132   Conf.RelocModel = CGOpts.RelocationModel;
1133   Conf.CGOptLevel = getCGOptLevel(CGOpts);
1134   initTargetOptions(Conf.Options, CGOpts, TOpts, LOpts, HeaderOpts);
1135   Conf.SampleProfile = std::move(SampleProfile);
1136   Conf.UseNewPM = CGOpts.ExperimentalNewPassManager;
1137   Conf.DebugPassManager = CGOpts.DebugPassManager;
1138   switch (Action) {
1139   case Backend_EmitNothing:
1140     Conf.PreCodeGenModuleHook = [](size_t Task, const Module &Mod) {
1141       return false;
1142     };
1143     break;
1144   case Backend_EmitLL:
1145     Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1146       M->print(*OS, nullptr, CGOpts.EmitLLVMUseLists);
1147       return false;
1148     };
1149     break;
1150   case Backend_EmitBC:
1151     Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1152       WriteBitcodeToFile(*M, *OS, CGOpts.EmitLLVMUseLists);
1153       return false;
1154     };
1155     break;
1156   default:
1157     Conf.CGFileType = getCodeGenFileType(Action);
1158     break;
1159   }
1160   if (Error E = thinBackend(
1161           Conf, 0, AddStream, *M, *CombinedIndex, ImportList,
1162           ModuleToDefinedGVSummaries[M->getModuleIdentifier()], ModuleMap)) {
1163     handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
1164       errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
1165     });
1166   }
1167 }
1168 
1169 void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
1170                               const HeaderSearchOptions &HeaderOpts,
1171                               const CodeGenOptions &CGOpts,
1172                               const clang::TargetOptions &TOpts,
1173                               const LangOptions &LOpts,
1174                               const llvm::DataLayout &TDesc, Module *M,
1175                               BackendAction Action,
1176                               std::unique_ptr<raw_pwrite_stream> OS) {
1177   std::unique_ptr<llvm::Module> EmptyModule;
1178   if (!CGOpts.ThinLTOIndexFile.empty()) {
1179     // If we are performing a ThinLTO importing compile, load the function index
1180     // into memory and pass it into runThinLTOBackend, which will run the
1181     // function importer and invoke LTO passes.
1182     Expected<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr =
1183         llvm::getModuleSummaryIndexForFile(CGOpts.ThinLTOIndexFile,
1184                                            /*IgnoreEmptyThinLTOIndexFile*/true);
1185     if (!IndexOrErr) {
1186       logAllUnhandledErrors(IndexOrErr.takeError(), errs(),
1187                             "Error loading index file '" +
1188                             CGOpts.ThinLTOIndexFile + "': ");
1189       return;
1190     }
1191     std::unique_ptr<ModuleSummaryIndex> CombinedIndex = std::move(*IndexOrErr);
1192     // A null CombinedIndex means we should skip ThinLTO compilation
1193     // (LLVM will optionally ignore empty index files, returning null instead
1194     // of an error).
1195     if (CombinedIndex) {
1196       if (!CombinedIndex->skipModuleByDistributedBackend()) {
1197         runThinLTOBackend(CombinedIndex.get(), M, HeaderOpts, CGOpts, TOpts,
1198                           LOpts, std::move(OS), CGOpts.SampleProfileFile,
1199                           Action);
1200         return;
1201       }
1202       // Distributed indexing detected that nothing from the module is needed
1203       // for the final linking. So we can skip the compilation. We sill need to
1204       // output an empty object file to make sure that a linker does not fail
1205       // trying to read it. Also for some features, like CFI, we must skip
1206       // the compilation as CombinedIndex does not contain all required
1207       // information.
1208       EmptyModule = llvm::make_unique<llvm::Module>("empty", M->getContext());
1209       EmptyModule->setTargetTriple(M->getTargetTriple());
1210       M = EmptyModule.get();
1211     }
1212   }
1213 
1214   EmitAssemblyHelper AsmHelper(Diags, HeaderOpts, CGOpts, TOpts, LOpts, M);
1215 
1216   if (CGOpts.ExperimentalNewPassManager)
1217     AsmHelper.EmitAssemblyWithNewPassManager(Action, std::move(OS));
1218   else
1219     AsmHelper.EmitAssembly(Action, std::move(OS));
1220 
1221   // Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
1222   // DataLayout.
1223   if (AsmHelper.TM) {
1224     std::string DLDesc = M->getDataLayout().getStringRepresentation();
1225     if (DLDesc != TDesc.getStringRepresentation()) {
1226       unsigned DiagID = Diags.getCustomDiagID(
1227           DiagnosticsEngine::Error, "backend data layout '%0' does not match "
1228                                     "expected target description '%1'");
1229       Diags.Report(DiagID) << DLDesc << TDesc.getStringRepresentation();
1230     }
1231   }
1232 }
1233 
1234 static const char* getSectionNameForBitcode(const Triple &T) {
1235   switch (T.getObjectFormat()) {
1236   case Triple::MachO:
1237     return "__LLVM,__bitcode";
1238   case Triple::COFF:
1239   case Triple::ELF:
1240   case Triple::Wasm:
1241   case Triple::UnknownObjectFormat:
1242     return ".llvmbc";
1243   }
1244   llvm_unreachable("Unimplemented ObjectFormatType");
1245 }
1246 
1247 static const char* getSectionNameForCommandline(const Triple &T) {
1248   switch (T.getObjectFormat()) {
1249   case Triple::MachO:
1250     return "__LLVM,__cmdline";
1251   case Triple::COFF:
1252   case Triple::ELF:
1253   case Triple::Wasm:
1254   case Triple::UnknownObjectFormat:
1255     return ".llvmcmd";
1256   }
1257   llvm_unreachable("Unimplemented ObjectFormatType");
1258 }
1259 
1260 // With -fembed-bitcode, save a copy of the llvm IR as data in the
1261 // __LLVM,__bitcode section.
1262 void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
1263                          llvm::MemoryBufferRef Buf) {
1264   if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off)
1265     return;
1266 
1267   // Save llvm.compiler.used and remote it.
1268   SmallVector<Constant*, 2> UsedArray;
1269   SmallSet<GlobalValue*, 4> UsedGlobals;
1270   Type *UsedElementType = Type::getInt8Ty(M->getContext())->getPointerTo(0);
1271   GlobalVariable *Used = collectUsedGlobalVariables(*M, UsedGlobals, true);
1272   for (auto *GV : UsedGlobals) {
1273     if (GV->getName() != "llvm.embedded.module" &&
1274         GV->getName() != "llvm.cmdline")
1275       UsedArray.push_back(
1276           ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
1277   }
1278   if (Used)
1279     Used->eraseFromParent();
1280 
1281   // Embed the bitcode for the llvm module.
1282   std::string Data;
1283   ArrayRef<uint8_t> ModuleData;
1284   Triple T(M->getTargetTriple());
1285   // Create a constant that contains the bitcode.
1286   // In case of embedding a marker, ignore the input Buf and use the empty
1287   // ArrayRef. It is also legal to create a bitcode marker even Buf is empty.
1288   if (CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker) {
1289     if (!isBitcode((const unsigned char *)Buf.getBufferStart(),
1290                    (const unsigned char *)Buf.getBufferEnd())) {
1291       // If the input is LLVM Assembly, bitcode is produced by serializing
1292       // the module. Use-lists order need to be perserved in this case.
1293       llvm::raw_string_ostream OS(Data);
1294       llvm::WriteBitcodeToFile(*M, OS, /* ShouldPreserveUseListOrder */ true);
1295       ModuleData =
1296           ArrayRef<uint8_t>((const uint8_t *)OS.str().data(), OS.str().size());
1297     } else
1298       // If the input is LLVM bitcode, write the input byte stream directly.
1299       ModuleData = ArrayRef<uint8_t>((const uint8_t *)Buf.getBufferStart(),
1300                                      Buf.getBufferSize());
1301   }
1302   llvm::Constant *ModuleConstant =
1303       llvm::ConstantDataArray::get(M->getContext(), ModuleData);
1304   llvm::GlobalVariable *GV = new llvm::GlobalVariable(
1305       *M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage,
1306       ModuleConstant);
1307   GV->setSection(getSectionNameForBitcode(T));
1308   UsedArray.push_back(
1309       ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
1310   if (llvm::GlobalVariable *Old =
1311           M->getGlobalVariable("llvm.embedded.module", true)) {
1312     assert(Old->hasOneUse() &&
1313            "llvm.embedded.module can only be used once in llvm.compiler.used");
1314     GV->takeName(Old);
1315     Old->eraseFromParent();
1316   } else {
1317     GV->setName("llvm.embedded.module");
1318   }
1319 
1320   // Skip if only bitcode needs to be embedded.
1321   if (CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode) {
1322     // Embed command-line options.
1323     ArrayRef<uint8_t> CmdData(const_cast<uint8_t *>(CGOpts.CmdArgs.data()),
1324                               CGOpts.CmdArgs.size());
1325     llvm::Constant *CmdConstant =
1326       llvm::ConstantDataArray::get(M->getContext(), CmdData);
1327     GV = new llvm::GlobalVariable(*M, CmdConstant->getType(), true,
1328                                   llvm::GlobalValue::PrivateLinkage,
1329                                   CmdConstant);
1330     GV->setSection(getSectionNameForCommandline(T));
1331     UsedArray.push_back(
1332         ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
1333     if (llvm::GlobalVariable *Old =
1334             M->getGlobalVariable("llvm.cmdline", true)) {
1335       assert(Old->hasOneUse() &&
1336              "llvm.cmdline can only be used once in llvm.compiler.used");
1337       GV->takeName(Old);
1338       Old->eraseFromParent();
1339     } else {
1340       GV->setName("llvm.cmdline");
1341     }
1342   }
1343 
1344   if (UsedArray.empty())
1345     return;
1346 
1347   // Recreate llvm.compiler.used.
1348   ArrayType *ATy = ArrayType::get(UsedElementType, UsedArray.size());
1349   auto *NewUsed = new GlobalVariable(
1350       *M, ATy, false, llvm::GlobalValue::AppendingLinkage,
1351       llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used");
1352   NewUsed->setSection("llvm.metadata");
1353 }
1354