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