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