1 //===--- BackendUtil.cpp - LLVM Backend Utilities -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
59 using namespace clang;
64 // Default filename used for profile generation.
65 static constexpr StringLiteral DefaultProfileGenName = "default_%m.profraw";
67 class EmitAssemblyHelper {
68 DiagnosticsEngine &Diags;
69 const HeaderSearchOptions &HSOpts;
70 const CodeGenOptions &CodeGenOpts;
71 const clang::TargetOptions &TargetOpts;
72 const LangOptions &LangOpts;
75 Timer CodeGenerationTime;
77 std::unique_ptr<raw_pwrite_stream> OS;
79 TargetIRAnalysis getTargetIRAnalysis() const {
81 return TM->getTargetIRAnalysis();
83 return TargetIRAnalysis();
86 /// Set LLVM command line options passed through -backend-option.
87 void setCommandLineOpts();
89 void CreatePasses(legacy::PassManager &MPM, legacy::FunctionPassManager &FPM);
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);
101 /// Add passes necessary to emit assembly or LLVM IR.
103 /// \return True on success.
104 bool AddEmitPasses(legacy::PassManager &CodeGenPasses, BackendAction Action,
105 raw_pwrite_stream &OS);
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") {}
117 ~EmitAssemblyHelper() {
118 if (CodeGenOpts.DisableFree)
119 BuryPointer(std::move(TM));
122 std::unique_ptr<TargetMachine> TM;
124 void EmitAssembly(BackendAction Action,
125 std::unique_ptr<raw_pwrite_stream> OS);
127 void EmitAssemblyWithNewPassManager(BackendAction Action,
128 std::unique_ptr<raw_pwrite_stream> OS);
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 {
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; }
141 const CodeGenOptions &CGOpts;
142 const LangOptions &LangOpts;
147 static void addObjCARCAPElimPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
148 if (Builder.OptLevel > 0)
149 PM.add(createObjCARCAPElimPass());
152 static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
153 if (Builder.OptLevel > 0)
154 PM.add(createObjCARCExpandPass());
157 static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
158 if (Builder.OptLevel > 0)
159 PM.add(createObjCARCOptPass());
162 static void addAddDiscriminatorsPass(const PassManagerBuilder &Builder,
163 legacy::PassManagerBase &PM) {
164 PM.add(createAddDiscriminatorsPass());
167 static void addBoundsCheckingPass(const PassManagerBuilder &Builder,
168 legacy::PassManagerBase &PM) {
169 PM.add(createBoundsCheckingPass());
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;
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));
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,
200 PM.add(createAddressSanitizerModulePass(/*CompileKernel*/false, Recover));
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,
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));
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());
234 static void addThreadSanitizerPass(const PassManagerBuilder &Builder,
235 legacy::PassManagerBase &PM) {
236 PM.add(createThreadSanitizerPass());
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));
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));
260 static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple,
261 const CodeGenOptions &CodeGenOpts) {
262 TargetLibraryInfoImpl *TLII = new TargetLibraryInfoImpl(TargetTriple);
263 if (!CodeGenOpts.SimplifyLibCalls)
264 TLII->disableAllFunctions();
266 // Disable individual libc/libm calls in TargetLibraryInfo.
268 for (auto &FuncName : CodeGenOpts.getNoBuiltinFuncs())
269 if (TLII->getLibFunc(FuncName, F))
270 TLII->setUnavailable(F);
273 switch (CodeGenOpts.getVecLib()) {
274 case CodeGenOptions::Accelerate:
275 TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::Accelerate);
277 case CodeGenOptions::SVML:
278 TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::SVML);
286 static void addSymbolRewriterPass(const CodeGenOptions &Opts,
287 legacy::PassManager *MPM) {
288 llvm::SymbolRewriter::RewriteDescriptorList DL;
290 llvm::SymbolRewriter::RewriteMapParser MapParser;
291 for (const auto &MapFile : Opts.RewriteMapFiles)
292 MapParser.parse(MapFile, &DL);
294 MPM->add(createRewriteSymbolsPass(DL));
297 static CodeGenOpt::Level getCGOptLevel(const CodeGenOptions &CodeGenOpts) {
298 switch (CodeGenOpts.OptimizationLevel) {
300 llvm_unreachable("Invalid optimization level!");
302 return CodeGenOpt::None;
304 return CodeGenOpt::Less;
306 return CodeGenOpt::Default; // O2/Os/Oz
308 return CodeGenOpt::Aggressive;
312 static llvm::CodeModel::Model getCodeModel(const CodeGenOptions &CodeGenOpts) {
314 llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel)
315 .Case("small", llvm::CodeModel::Small)
316 .Case("kernel", llvm::CodeModel::Kernel)
317 .Case("medium", llvm::CodeModel::Medium)
318 .Case("large", llvm::CodeModel::Large)
319 .Case("default", llvm::CodeModel::Default)
321 assert(CodeModel != ~0u && "invalid code model!");
322 return static_cast<llvm::CodeModel::Model>(CodeModel);
325 static llvm::Reloc::Model getRelocModel(const CodeGenOptions &CodeGenOpts) {
326 // Keep this synced with the equivalent code in
327 // lib/Frontend/CompilerInvocation.cpp
328 llvm::Optional<llvm::Reloc::Model> RM;
329 RM = llvm::StringSwitch<llvm::Reloc::Model>(CodeGenOpts.RelocationModel)
330 .Case("static", llvm::Reloc::Static)
331 .Case("pic", llvm::Reloc::PIC_)
332 .Case("ropi", llvm::Reloc::ROPI)
333 .Case("rwpi", llvm::Reloc::RWPI)
334 .Case("ropi-rwpi", llvm::Reloc::ROPI_RWPI)
335 .Case("dynamic-no-pic", llvm::Reloc::DynamicNoPIC);
336 assert(RM.hasValue() && "invalid PIC model!");
340 static TargetMachine::CodeGenFileType getCodeGenFileType(BackendAction Action) {
341 if (Action == Backend_EmitObj)
342 return TargetMachine::CGFT_ObjectFile;
343 else if (Action == Backend_EmitMCNull)
344 return TargetMachine::CGFT_Null;
346 assert(Action == Backend_EmitAssembly && "Invalid action!");
347 return TargetMachine::CGFT_AssemblyFile;
351 static void initTargetOptions(llvm::TargetOptions &Options,
352 const CodeGenOptions &CodeGenOpts,
353 const clang::TargetOptions &TargetOpts,
354 const LangOptions &LangOpts,
355 const HeaderSearchOptions &HSOpts) {
356 Options.ThreadModel =
357 llvm::StringSwitch<llvm::ThreadModel::Model>(CodeGenOpts.ThreadModel)
358 .Case("posix", llvm::ThreadModel::POSIX)
359 .Case("single", llvm::ThreadModel::Single);
361 // Set float ABI type.
362 assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" ||
363 CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) &&
364 "Invalid Floating Point ABI!");
365 Options.FloatABIType =
366 llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI)
367 .Case("soft", llvm::FloatABI::Soft)
368 .Case("softfp", llvm::FloatABI::Soft)
369 .Case("hard", llvm::FloatABI::Hard)
370 .Default(llvm::FloatABI::Default);
372 // Set FP fusion mode.
373 switch (LangOpts.getDefaultFPContractMode()) {
374 case LangOptions::FPC_Off:
375 Options.AllowFPOpFusion = llvm::FPOpFusion::Strict;
377 case LangOptions::FPC_On:
378 Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
380 case LangOptions::FPC_Fast:
381 Options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
385 Options.UseInitArray = CodeGenOpts.UseInitArray;
386 Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS;
387 Options.CompressDebugSections = CodeGenOpts.CompressDebugSections;
388 Options.RelaxELFRelocations = CodeGenOpts.RelaxELFRelocations;
391 Options.EABIVersion = llvm::StringSwitch<llvm::EABI>(TargetOpts.EABIVersion)
392 .Case("4", llvm::EABI::EABI4)
393 .Case("5", llvm::EABI::EABI5)
394 .Case("gnu", llvm::EABI::GNU)
395 .Default(llvm::EABI::Default);
397 if (LangOpts.SjLjExceptions)
398 Options.ExceptionModel = llvm::ExceptionHandling::SjLj;
400 Options.NoInfsFPMath = CodeGenOpts.NoInfsFPMath;
401 Options.NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath;
402 Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
403 Options.UnsafeFPMath = CodeGenOpts.UnsafeFPMath;
404 Options.StackAlignmentOverride = CodeGenOpts.StackAlignment;
405 Options.FunctionSections = CodeGenOpts.FunctionSections;
406 Options.DataSections = CodeGenOpts.DataSections;
407 Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames;
408 Options.EmulatedTLS = CodeGenOpts.EmulatedTLS;
409 Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning();
411 Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll;
412 Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels;
413 Options.MCOptions.MCUseDwarfDirectory = !CodeGenOpts.NoDwarfDirectoryAsm;
414 Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack;
415 Options.MCOptions.MCIncrementalLinkerCompatible =
416 CodeGenOpts.IncrementalLinkerCompatible;
417 Options.MCOptions.MCPIECopyRelocations = CodeGenOpts.PIECopyRelocations;
418 Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings;
419 Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose;
420 Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments;
421 Options.MCOptions.ABIName = TargetOpts.ABI;
422 for (const auto &Entry : HSOpts.UserEntries)
423 if (!Entry.IsFramework &&
424 (Entry.Group == frontend::IncludeDirGroup::Quoted ||
425 Entry.Group == frontend::IncludeDirGroup::Angled ||
426 Entry.Group == frontend::IncludeDirGroup::System))
427 Options.MCOptions.IASSearchPaths.push_back(
428 Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path);
431 void EmitAssemblyHelper::CreatePasses(legacy::PassManager &MPM,
432 legacy::FunctionPassManager &FPM) {
433 // Handle disabling of all LLVM passes, where we want to preserve the
434 // internal module before any optimization.
435 if (CodeGenOpts.DisableLLVMPasses)
438 PassManagerBuilderWrapper PMBuilder(CodeGenOpts, LangOpts);
440 // Figure out TargetLibraryInfo. This needs to be added to MPM and FPM
441 // manually (and not via PMBuilder), since some passes (eg. InstrProfiling)
442 // are inserted before PMBuilder ones - they'd get the default-constructed
443 // TLI with an unknown target otherwise.
444 Triple TargetTriple(TheModule->getTargetTriple());
445 std::unique_ptr<TargetLibraryInfoImpl> TLII(
446 createTLII(TargetTriple, CodeGenOpts));
448 // At O0 and O1 we only run the always inliner which is more efficient. At
449 // higher optimization levels we run the normal inliner.
450 if (CodeGenOpts.OptimizationLevel <= 1) {
451 bool InsertLifetimeIntrinsics = (CodeGenOpts.OptimizationLevel != 0 &&
452 !CodeGenOpts.DisableLifetimeMarkers);
453 PMBuilder.Inliner = createAlwaysInlinerLegacyPass(InsertLifetimeIntrinsics);
455 // We do not want to inline hot callsites for SamplePGO module-summary build
456 // because profile annotation will happen again in ThinLTO backend, and we
457 // want the IR of the hot path to match the profile.
458 PMBuilder.Inliner = createFunctionInliningPass(
459 CodeGenOpts.OptimizationLevel, CodeGenOpts.OptimizeSize,
460 (!CodeGenOpts.SampleProfileFile.empty() &&
461 CodeGenOpts.EmitSummaryIndex));
464 PMBuilder.OptLevel = CodeGenOpts.OptimizationLevel;
465 PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize;
466 PMBuilder.BBVectorize = CodeGenOpts.VectorizeBB;
467 PMBuilder.SLPVectorize = CodeGenOpts.VectorizeSLP;
468 PMBuilder.LoopVectorize = CodeGenOpts.VectorizeLoop;
470 PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
471 PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions;
472 PMBuilder.PrepareForThinLTO = CodeGenOpts.EmitSummaryIndex;
473 PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
474 PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
476 MPM.add(new TargetLibraryInfoWrapperPass(*TLII));
479 TM->adjustPassManager(PMBuilder);
481 if (CodeGenOpts.DebugInfoForProfiling ||
482 !CodeGenOpts.SampleProfileFile.empty())
483 PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
484 addAddDiscriminatorsPass);
486 // In ObjC ARC mode, add the main ARC optimization passes.
487 if (LangOpts.ObjCAutoRefCount) {
488 PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
489 addObjCARCExpandPass);
490 PMBuilder.addExtension(PassManagerBuilder::EP_ModuleOptimizerEarly,
491 addObjCARCAPElimPass);
492 PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
496 if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) {
497 PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
498 addBoundsCheckingPass);
499 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
500 addBoundsCheckingPass);
503 if (CodeGenOpts.SanitizeCoverageType ||
504 CodeGenOpts.SanitizeCoverageIndirectCalls ||
505 CodeGenOpts.SanitizeCoverageTraceCmp) {
506 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
507 addSanitizerCoveragePass);
508 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
509 addSanitizerCoveragePass);
512 if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
513 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
514 addAddressSanitizerPasses);
515 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
516 addAddressSanitizerPasses);
519 if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) {
520 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
521 addKernelAddressSanitizerPasses);
522 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
523 addKernelAddressSanitizerPasses);
526 if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
527 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
528 addMemorySanitizerPass);
529 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
530 addMemorySanitizerPass);
533 if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
534 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
535 addThreadSanitizerPass);
536 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
537 addThreadSanitizerPass);
540 if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
541 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
542 addDataFlowSanitizerPass);
543 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
544 addDataFlowSanitizerPass);
547 if (LangOpts.CoroutinesTS)
548 addCoroutinePassesToExtensionPoints(PMBuilder);
550 if (LangOpts.Sanitize.hasOneOf(SanitizerKind::Efficiency)) {
551 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
552 addEfficiencySanitizerPass);
553 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
554 addEfficiencySanitizerPass);
557 // Set up the per-function pass manager.
558 FPM.add(new TargetLibraryInfoWrapperPass(*TLII));
559 if (CodeGenOpts.VerifyModule)
560 FPM.add(createVerifierPass());
562 // Set up the per-module pass manager.
563 if (!CodeGenOpts.RewriteMapFiles.empty())
564 addSymbolRewriterPass(CodeGenOpts, &MPM);
566 if (!CodeGenOpts.DisableGCov &&
567 (CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes)) {
568 // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if
569 // LLVM's -default-gcov-version flag is set to something invalid.
571 Options.EmitNotes = CodeGenOpts.EmitGcovNotes;
572 Options.EmitData = CodeGenOpts.EmitGcovArcs;
573 memcpy(Options.Version, CodeGenOpts.CoverageVersion, 4);
574 Options.UseCfgChecksum = CodeGenOpts.CoverageExtraChecksum;
575 Options.NoRedZone = CodeGenOpts.DisableRedZone;
576 Options.FunctionNamesInData =
577 !CodeGenOpts.CoverageNoFunctionNamesInData;
578 Options.ExitBlockBeforeBody = CodeGenOpts.CoverageExitBlockBeforeBody;
579 MPM.add(createGCOVProfilerPass(Options));
580 if (CodeGenOpts.getDebugInfo() == codegenoptions::NoDebugInfo)
581 MPM.add(createStripSymbolsPass(true));
584 if (CodeGenOpts.hasProfileClangInstr()) {
585 InstrProfOptions Options;
586 Options.NoRedZone = CodeGenOpts.DisableRedZone;
587 Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput;
588 MPM.add(createInstrProfilingLegacyPass(Options));
590 if (CodeGenOpts.hasProfileIRInstr()) {
591 PMBuilder.EnablePGOInstrGen = true;
592 if (!CodeGenOpts.InstrProfileOutput.empty())
593 PMBuilder.PGOInstrGen = CodeGenOpts.InstrProfileOutput;
595 PMBuilder.PGOInstrGen = DefaultProfileGenName;
597 if (CodeGenOpts.hasProfileIRUse())
598 PMBuilder.PGOInstrUse = CodeGenOpts.ProfileInstrumentUsePath;
600 if (!CodeGenOpts.SampleProfileFile.empty())
601 PMBuilder.PGOSampleUse = CodeGenOpts.SampleProfileFile;
603 PMBuilder.populateFunctionPassManager(FPM);
604 PMBuilder.populateModulePassManager(MPM);
607 void EmitAssemblyHelper::setCommandLineOpts() {
608 SmallVector<const char *, 16> BackendArgs;
609 BackendArgs.push_back("clang"); // Fake program name.
610 if (!CodeGenOpts.DebugPass.empty()) {
611 BackendArgs.push_back("-debug-pass");
612 BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
614 if (!CodeGenOpts.LimitFloatPrecision.empty()) {
615 BackendArgs.push_back("-limit-float-precision");
616 BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
618 for (const std::string &BackendOption : CodeGenOpts.BackendOptions)
619 BackendArgs.push_back(BackendOption.c_str());
620 BackendArgs.push_back(nullptr);
621 llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
625 void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
626 // Create the TargetMachine for generating code.
628 std::string Triple = TheModule->getTargetTriple();
629 const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
632 Diags.Report(diag::err_fe_unable_to_create_target) << Error;
636 llvm::CodeModel::Model CM = getCodeModel(CodeGenOpts);
637 std::string FeaturesStr =
638 llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ",");
639 llvm::Reloc::Model RM = getRelocModel(CodeGenOpts);
640 CodeGenOpt::Level OptLevel = getCGOptLevel(CodeGenOpts);
642 llvm::TargetOptions Options;
643 initTargetOptions(Options, CodeGenOpts, TargetOpts, LangOpts, HSOpts);
644 TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr,
645 Options, RM, CM, OptLevel));
648 bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses,
649 BackendAction Action,
650 raw_pwrite_stream &OS) {
652 llvm::Triple TargetTriple(TheModule->getTargetTriple());
653 std::unique_ptr<TargetLibraryInfoImpl> TLII(
654 createTLII(TargetTriple, CodeGenOpts));
655 CodeGenPasses.add(new TargetLibraryInfoWrapperPass(*TLII));
657 // Normal mode, emit a .s or .o file by running the code generator. Note,
658 // this also adds codegenerator level optimization passes.
659 TargetMachine::CodeGenFileType CGFT = getCodeGenFileType(Action);
661 // Add ObjC ARC final-cleanup optimizations. This is done as part of the
662 // "codegen" passes so that it isn't run multiple times when there is
663 // inlining happening.
664 if (CodeGenOpts.OptimizationLevel > 0)
665 CodeGenPasses.add(createObjCARCContractPass());
667 if (TM->addPassesToEmitFile(CodeGenPasses, OS, CGFT,
668 /*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
669 Diags.Report(diag::err_fe_unable_to_interface_with_target);
676 void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
677 std::unique_ptr<raw_pwrite_stream> OS) {
678 TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr);
680 setCommandLineOpts();
682 bool UsesCodeGen = (Action != Backend_EmitNothing &&
683 Action != Backend_EmitBC &&
684 Action != Backend_EmitLL);
685 CreateTargetMachine(UsesCodeGen);
687 if (UsesCodeGen && !TM)
690 TheModule->setDataLayout(TM->createDataLayout());
692 legacy::PassManager PerModulePasses;
694 createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
696 legacy::FunctionPassManager PerFunctionPasses(TheModule);
697 PerFunctionPasses.add(
698 createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
700 CreatePasses(PerModulePasses, PerFunctionPasses);
702 legacy::PassManager CodeGenPasses;
704 createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
706 std::unique_ptr<raw_fd_ostream> ThinLinkOS;
709 case Backend_EmitNothing:
713 if (CodeGenOpts.EmitSummaryIndex) {
714 if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
716 ThinLinkOS.reset(new llvm::raw_fd_ostream(
717 CodeGenOpts.ThinLinkBitcodeFile, EC,
718 llvm::sys::fs::F_None));
720 Diags.Report(diag::err_fe_unable_to_open_output) << CodeGenOpts.ThinLinkBitcodeFile
726 createWriteThinLTOBitcodePass(*OS, ThinLinkOS.get()));
730 createBitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists));
735 createPrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
739 if (!AddEmitPasses(CodeGenPasses, Action, *OS))
743 // Before executing passes, print the final values of the LLVM options.
744 cl::PrintOptionValues();
746 // Run passes. For now we do all passes at once, but eventually we
747 // would like to have the option of streaming code generation.
750 PrettyStackTraceString CrashInfo("Per-function optimization");
752 PerFunctionPasses.doInitialization();
753 for (Function &F : *TheModule)
754 if (!F.isDeclaration())
755 PerFunctionPasses.run(F);
756 PerFunctionPasses.doFinalization();
760 PrettyStackTraceString CrashInfo("Per-module optimization passes");
761 PerModulePasses.run(*TheModule);
765 PrettyStackTraceString CrashInfo("Code generation");
766 CodeGenPasses.run(*TheModule);
770 static PassBuilder::OptimizationLevel mapToLevel(const CodeGenOptions &Opts) {
771 switch (Opts.OptimizationLevel) {
773 llvm_unreachable("Invalid optimization level!");
776 return PassBuilder::O1;
779 switch (Opts.OptimizeSize) {
781 llvm_unreachable("Invalide optimization level for size!");
784 return PassBuilder::O2;
787 return PassBuilder::Os;
790 return PassBuilder::Oz;
794 return PassBuilder::O3;
798 /// A clean version of `EmitAssembly` that uses the new pass manager.
800 /// Not all features are currently supported in this system, but where
801 /// necessary it falls back to the legacy pass manager to at least provide
802 /// basic functionality.
804 /// This API is planned to have its functionality finished and then to replace
805 /// `EmitAssembly` at some point in the future when the default switches.
806 void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
807 BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS) {
808 TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr);
809 setCommandLineOpts();
811 // The new pass manager always makes a target machine available to passes
812 // during construction.
813 CreateTargetMachine(/*MustCreateTM*/ true);
815 // This will already be diagnosed, just bail.
817 TheModule->setDataLayout(TM->createDataLayout());
821 // -fprofile-generate.
822 PGOOpt.RunProfileGen = CodeGenOpts.hasProfileIRInstr();
823 if (PGOOpt.RunProfileGen)
824 PGOOpt.ProfileGenFile = CodeGenOpts.InstrProfileOutput.empty() ?
825 DefaultProfileGenName : CodeGenOpts.InstrProfileOutput;
828 if (CodeGenOpts.hasProfileIRUse())
829 PGOOpt.ProfileUseFile = CodeGenOpts.ProfileInstrumentUsePath;
831 // Only pass a PGO options struct if -fprofile-generate or
832 // -fprofile-use were passed on the cmdline.
833 PassBuilder PB(TM.get(),
834 (PGOOpt.RunProfileGen ||
835 !PGOOpt.ProfileUseFile.empty()) ?
836 Optional<PGOOptions>(PGOOpt) : None);
838 LoopAnalysisManager LAM;
839 FunctionAnalysisManager FAM;
840 CGSCCAnalysisManager CGAM;
841 ModuleAnalysisManager MAM;
843 // Register the AA manager first so that our version is the one used.
844 FAM.registerPass([&] { return PB.buildDefaultAAPipeline(); });
846 // Register all the basic analyses with the managers.
847 PB.registerModuleAnalyses(MAM);
848 PB.registerCGSCCAnalyses(CGAM);
849 PB.registerFunctionAnalyses(FAM);
850 PB.registerLoopAnalyses(LAM);
851 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
853 ModulePassManager MPM;
855 if (!CodeGenOpts.DisableLLVMPasses) {
856 if (CodeGenOpts.OptimizationLevel == 0) {
857 // Build a minimal pipeline based on the semantics required by Clang,
858 // which is just that always inlining occurs.
859 MPM.addPass(AlwaysInlinerPass());
861 // Otherwise, use the default pass pipeline. We also have to map our
862 // optimization levels into one of the distinct levels used to configure
864 PassBuilder::OptimizationLevel Level = mapToLevel(CodeGenOpts);
866 MPM = PB.buildPerModuleDefaultPipeline(Level);
870 // FIXME: We still use the legacy pass manager to do code generation. We
871 // create that pass manager here and use it as needed below.
872 legacy::PassManager CodeGenPasses;
873 bool NeedCodeGen = false;
875 // Append any output we need to the pass manager.
877 case Backend_EmitNothing:
881 MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
882 CodeGenOpts.EmitSummaryIndex,
883 CodeGenOpts.EmitSummaryIndex));
887 MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
890 case Backend_EmitAssembly:
891 case Backend_EmitMCNull:
892 case Backend_EmitObj:
895 createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
896 if (!AddEmitPasses(CodeGenPasses, Action, *OS))
897 // FIXME: Should we handle this error differently?
902 // Before executing passes, print the final values of the LLVM options.
903 cl::PrintOptionValues();
905 // Now that we have all of the passes ready, run them.
907 PrettyStackTraceString CrashInfo("Optimizer");
908 MPM.run(*TheModule, MAM);
911 // Now if needed, run the legacy PM for codegen.
913 PrettyStackTraceString CrashInfo("Code generation");
914 CodeGenPasses.run(*TheModule);
918 Expected<BitcodeModule> clang::FindThinLTOModule(MemoryBufferRef MBRef) {
919 Expected<std::vector<BitcodeModule>> BMsOrErr = getBitcodeModuleList(MBRef);
921 return BMsOrErr.takeError();
923 // The bitcode file may contain multiple modules, we want the one with a
925 for (BitcodeModule &BM : *BMsOrErr) {
926 Expected<bool> HasSummary = BM.hasSummary();
927 if (HasSummary && *HasSummary)
931 return make_error<StringError>("Could not find module summary",
932 inconvertibleErrorCode());
935 static void runThinLTOBackend(ModuleSummaryIndex *CombinedIndex, Module *M,
936 const HeaderSearchOptions &HeaderOpts,
937 const CodeGenOptions &CGOpts,
938 const clang::TargetOptions &TOpts,
939 const LangOptions &LOpts,
940 std::unique_ptr<raw_pwrite_stream> OS,
941 std::string SampleProfile,
942 BackendAction Action) {
943 StringMap<std::map<GlobalValue::GUID, GlobalValueSummary *>>
944 ModuleToDefinedGVSummaries;
945 CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
947 // We can simply import the values mentioned in the combined index, since
948 // we should only invoke this using the individual indexes written out
949 // via a WriteIndexesThinBackend.
950 FunctionImporter::ImportMapTy ImportList;
951 for (auto &GlobalList : *CombinedIndex) {
952 auto GUID = GlobalList.first;
953 assert(GlobalList.second.size() == 1 &&
954 "Expected individual combined index to have one summary per GUID");
955 auto &Summary = GlobalList.second[0];
956 // Skip the summaries for the importing module. These are included to
957 // e.g. record required linkage changes.
958 if (Summary->modulePath() == M->getModuleIdentifier())
960 // Doesn't matter what value we plug in to the map, just needs an entry
961 // to provoke importing by thinBackend.
962 ImportList[Summary->modulePath()][GUID] = 1;
965 std::vector<std::unique_ptr<llvm::MemoryBuffer>> OwnedImports;
966 MapVector<llvm::StringRef, llvm::BitcodeModule> ModuleMap;
968 for (auto &I : ImportList) {
969 ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MBOrErr =
970 llvm::MemoryBuffer::getFile(I.first());
972 errs() << "Error loading imported file '" << I.first()
973 << "': " << MBOrErr.getError().message() << "\n";
977 Expected<BitcodeModule> BMOrErr = FindThinLTOModule(**MBOrErr);
979 handleAllErrors(BMOrErr.takeError(), [&](ErrorInfoBase &EIB) {
980 errs() << "Error loading imported file '" << I.first()
981 << "': " << EIB.message() << '\n';
985 ModuleMap.insert({I.first(), *BMOrErr});
987 OwnedImports.push_back(std::move(*MBOrErr));
989 auto AddStream = [&](size_t Task) {
990 return llvm::make_unique<lto::NativeObjectStream>(std::move(OS));
993 Conf.CPU = TOpts.CPU;
994 Conf.CodeModel = getCodeModel(CGOpts);
995 Conf.MAttrs = TOpts.Features;
996 Conf.RelocModel = getRelocModel(CGOpts);
997 Conf.CGOptLevel = getCGOptLevel(CGOpts);
998 initTargetOptions(Conf.Options, CGOpts, TOpts, LOpts, HeaderOpts);
999 Conf.SampleProfile = std::move(SampleProfile);
1001 case Backend_EmitNothing:
1002 Conf.PreCodeGenModuleHook = [](size_t Task, const Module &Mod) {
1006 case Backend_EmitLL:
1007 Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1008 M->print(*OS, nullptr, CGOpts.EmitLLVMUseLists);
1012 case Backend_EmitBC:
1013 Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1014 WriteBitcodeToFile(M, *OS, CGOpts.EmitLLVMUseLists);
1019 Conf.CGFileType = getCodeGenFileType(Action);
1022 if (Error E = thinBackend(
1023 Conf, 0, AddStream, *M, *CombinedIndex, ImportList,
1024 ModuleToDefinedGVSummaries[M->getModuleIdentifier()], ModuleMap)) {
1025 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
1026 errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
1031 void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
1032 const HeaderSearchOptions &HeaderOpts,
1033 const CodeGenOptions &CGOpts,
1034 const clang::TargetOptions &TOpts,
1035 const LangOptions &LOpts,
1036 const llvm::DataLayout &TDesc, Module *M,
1037 BackendAction Action,
1038 std::unique_ptr<raw_pwrite_stream> OS) {
1039 if (!CGOpts.ThinLTOIndexFile.empty()) {
1040 // If we are performing a ThinLTO importing compile, load the function index
1041 // into memory and pass it into runThinLTOBackend, which will run the
1042 // function importer and invoke LTO passes.
1043 Expected<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr =
1044 llvm::getModuleSummaryIndexForFile(CGOpts.ThinLTOIndexFile);
1046 logAllUnhandledErrors(IndexOrErr.takeError(), errs(),
1047 "Error loading index file '" +
1048 CGOpts.ThinLTOIndexFile + "': ");
1051 std::unique_ptr<ModuleSummaryIndex> CombinedIndex = std::move(*IndexOrErr);
1052 // A null CombinedIndex means we should skip ThinLTO compilation
1053 // (LLVM will optionally ignore empty index files, returning null instead
1055 bool DoThinLTOBackend = CombinedIndex != nullptr;
1056 if (DoThinLTOBackend) {
1057 runThinLTOBackend(CombinedIndex.get(), M, HeaderOpts, CGOpts, TOpts,
1058 LOpts, std::move(OS), CGOpts.SampleProfileFile, Action);
1063 EmitAssemblyHelper AsmHelper(Diags, HeaderOpts, CGOpts, TOpts, LOpts, M);
1065 if (CGOpts.ExperimentalNewPassManager)
1066 AsmHelper.EmitAssemblyWithNewPassManager(Action, std::move(OS));
1068 AsmHelper.EmitAssembly(Action, std::move(OS));
1070 // Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
1073 std::string DLDesc = M->getDataLayout().getStringRepresentation();
1074 if (DLDesc != TDesc.getStringRepresentation()) {
1075 unsigned DiagID = Diags.getCustomDiagID(
1076 DiagnosticsEngine::Error, "backend data layout '%0' does not match "
1077 "expected target description '%1'");
1078 Diags.Report(DiagID) << DLDesc << TDesc.getStringRepresentation();
1083 static const char* getSectionNameForBitcode(const Triple &T) {
1084 switch (T.getObjectFormat()) {
1086 return "__LLVM,__bitcode";
1090 case Triple::UnknownObjectFormat:
1093 llvm_unreachable("Unimplemented ObjectFormatType");
1096 static const char* getSectionNameForCommandline(const Triple &T) {
1097 switch (T.getObjectFormat()) {
1099 return "__LLVM,__cmdline";
1103 case Triple::UnknownObjectFormat:
1106 llvm_unreachable("Unimplemented ObjectFormatType");
1109 // With -fembed-bitcode, save a copy of the llvm IR as data in the
1110 // __LLVM,__bitcode section.
1111 void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
1112 llvm::MemoryBufferRef Buf) {
1113 if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off)
1116 // Save llvm.compiler.used and remote it.
1117 SmallVector<Constant*, 2> UsedArray;
1118 SmallSet<GlobalValue*, 4> UsedGlobals;
1119 Type *UsedElementType = Type::getInt8Ty(M->getContext())->getPointerTo(0);
1120 GlobalVariable *Used = collectUsedGlobalVariables(*M, UsedGlobals, true);
1121 for (auto *GV : UsedGlobals) {
1122 if (GV->getName() != "llvm.embedded.module" &&
1123 GV->getName() != "llvm.cmdline")
1124 UsedArray.push_back(
1125 ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
1128 Used->eraseFromParent();
1130 // Embed the bitcode for the llvm module.
1132 ArrayRef<uint8_t> ModuleData;
1133 Triple T(M->getTargetTriple());
1134 // Create a constant that contains the bitcode.
1135 // In case of embedding a marker, ignore the input Buf and use the empty
1136 // ArrayRef. It is also legal to create a bitcode marker even Buf is empty.
1137 if (CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker) {
1138 if (!isBitcode((const unsigned char *)Buf.getBufferStart(),
1139 (const unsigned char *)Buf.getBufferEnd())) {
1140 // If the input is LLVM Assembly, bitcode is produced by serializing
1141 // the module. Use-lists order need to be perserved in this case.
1142 llvm::raw_string_ostream OS(Data);
1143 llvm::WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ true);
1145 ArrayRef<uint8_t>((const uint8_t *)OS.str().data(), OS.str().size());
1147 // If the input is LLVM bitcode, write the input byte stream directly.
1148 ModuleData = ArrayRef<uint8_t>((const uint8_t *)Buf.getBufferStart(),
1149 Buf.getBufferSize());
1151 llvm::Constant *ModuleConstant =
1152 llvm::ConstantDataArray::get(M->getContext(), ModuleData);
1153 llvm::GlobalVariable *GV = new llvm::GlobalVariable(
1154 *M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage,
1156 GV->setSection(getSectionNameForBitcode(T));
1157 UsedArray.push_back(
1158 ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
1159 if (llvm::GlobalVariable *Old =
1160 M->getGlobalVariable("llvm.embedded.module", true)) {
1161 assert(Old->hasOneUse() &&
1162 "llvm.embedded.module can only be used once in llvm.compiler.used");
1164 Old->eraseFromParent();
1166 GV->setName("llvm.embedded.module");
1169 // Skip if only bitcode needs to be embedded.
1170 if (CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode) {
1171 // Embed command-line options.
1172 ArrayRef<uint8_t> CmdData(const_cast<uint8_t *>(CGOpts.CmdArgs.data()),
1173 CGOpts.CmdArgs.size());
1174 llvm::Constant *CmdConstant =
1175 llvm::ConstantDataArray::get(M->getContext(), CmdData);
1176 GV = new llvm::GlobalVariable(*M, CmdConstant->getType(), true,
1177 llvm::GlobalValue::PrivateLinkage,
1179 GV->setSection(getSectionNameForCommandline(T));
1180 UsedArray.push_back(
1181 ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
1182 if (llvm::GlobalVariable *Old =
1183 M->getGlobalVariable("llvm.cmdline", true)) {
1184 assert(Old->hasOneUse() &&
1185 "llvm.cmdline can only be used once in llvm.compiler.used");
1187 Old->eraseFromParent();
1189 GV->setName("llvm.cmdline");
1193 if (UsedArray.empty())
1196 // Recreate llvm.compiler.used.
1197 ArrayType *ATy = ArrayType::get(UsedElementType, UsedArray.size());
1198 auto *NewUsed = new GlobalVariable(
1199 *M, ATy, false, llvm::GlobalValue::AppendingLinkage,
1200 llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used");
1201 NewUsed->setSection("llvm.metadata");