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