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