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