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 void CreatePasses(legacy::PassManager &MPM, legacy::FunctionPassManager &FPM);
88 /// Generates the TargetMachine.
89 /// Leaves TM unchanged if it is unable to create the target machine.
90 /// Some of our clang tests specify triples which are not built
91 /// into clang. This is okay because these tests check the generated
92 /// IR, and they require DataLayout which depends on the triple.
93 /// In this case, we allow this method to fail and not report an error.
94 /// When MustCreateTM is used, we print an error if we are unable to load
95 /// the requested target.
96 void CreateTargetMachine(bool MustCreateTM);
98 /// Add passes necessary to emit assembly or LLVM IR.
100 /// \return True on success.
101 bool AddEmitPasses(legacy::PassManager &CodeGenPasses, BackendAction Action,
102 raw_pwrite_stream &OS);
105 EmitAssemblyHelper(DiagnosticsEngine &_Diags,
106 const HeaderSearchOptions &HeaderSearchOpts,
107 const CodeGenOptions &CGOpts,
108 const clang::TargetOptions &TOpts,
109 const LangOptions &LOpts, Module *M)
110 : Diags(_Diags), HSOpts(HeaderSearchOpts), CodeGenOpts(CGOpts),
111 TargetOpts(TOpts), LangOpts(LOpts), TheModule(M),
112 CodeGenerationTime("codegen", "Code Generation Time") {}
114 ~EmitAssemblyHelper() {
115 if (CodeGenOpts.DisableFree)
116 BuryPointer(std::move(TM));
119 std::unique_ptr<TargetMachine> TM;
121 void EmitAssembly(BackendAction Action,
122 std::unique_ptr<raw_pwrite_stream> OS);
124 void EmitAssemblyWithNewPassManager(BackendAction Action,
125 std::unique_ptr<raw_pwrite_stream> OS);
128 // We need this wrapper to access LangOpts and CGOpts from extension functions
129 // that we add to the PassManagerBuilder.
130 class PassManagerBuilderWrapper : public PassManagerBuilder {
132 PassManagerBuilderWrapper(const CodeGenOptions &CGOpts,
133 const LangOptions &LangOpts)
134 : PassManagerBuilder(), CGOpts(CGOpts), LangOpts(LangOpts) {}
135 const CodeGenOptions &getCGOpts() const { return CGOpts; }
136 const LangOptions &getLangOpts() const { return LangOpts; }
138 const CodeGenOptions &CGOpts;
139 const LangOptions &LangOpts;
144 static void addObjCARCAPElimPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
145 if (Builder.OptLevel > 0)
146 PM.add(createObjCARCAPElimPass());
149 static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
150 if (Builder.OptLevel > 0)
151 PM.add(createObjCARCExpandPass());
154 static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
155 if (Builder.OptLevel > 0)
156 PM.add(createObjCARCOptPass());
159 static void addAddDiscriminatorsPass(const PassManagerBuilder &Builder,
160 legacy::PassManagerBase &PM) {
161 PM.add(createAddDiscriminatorsPass());
164 static void addBoundsCheckingPass(const PassManagerBuilder &Builder,
165 legacy::PassManagerBase &PM) {
166 PM.add(createBoundsCheckingPass());
169 static void addSanitizerCoveragePass(const PassManagerBuilder &Builder,
170 legacy::PassManagerBase &PM) {
171 const PassManagerBuilderWrapper &BuilderWrapper =
172 static_cast<const PassManagerBuilderWrapper&>(Builder);
173 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
174 SanitizerCoverageOptions Opts;
176 static_cast<SanitizerCoverageOptions::Type>(CGOpts.SanitizeCoverageType);
177 Opts.IndirectCalls = CGOpts.SanitizeCoverageIndirectCalls;
178 Opts.TraceBB = CGOpts.SanitizeCoverageTraceBB;
179 Opts.TraceCmp = CGOpts.SanitizeCoverageTraceCmp;
180 Opts.TraceDiv = CGOpts.SanitizeCoverageTraceDiv;
181 Opts.TraceGep = CGOpts.SanitizeCoverageTraceGep;
182 Opts.Use8bitCounters = CGOpts.SanitizeCoverage8bitCounters;
183 Opts.TracePC = CGOpts.SanitizeCoverageTracePC;
184 Opts.TracePCGuard = CGOpts.SanitizeCoverageTracePCGuard;
185 PM.add(createSanitizerCoverageModulePass(Opts));
188 static void addAddressSanitizerPasses(const PassManagerBuilder &Builder,
189 legacy::PassManagerBase &PM) {
190 const PassManagerBuilderWrapper &BuilderWrapper =
191 static_cast<const PassManagerBuilderWrapper&>(Builder);
192 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
193 bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Address);
194 bool UseAfterScope = CGOpts.SanitizeAddressUseAfterScope;
195 PM.add(createAddressSanitizerFunctionPass(/*CompileKernel*/ false, Recover,
197 PM.add(createAddressSanitizerModulePass(/*CompileKernel*/false, Recover));
200 static void addKernelAddressSanitizerPasses(const PassManagerBuilder &Builder,
201 legacy::PassManagerBase &PM) {
202 PM.add(createAddressSanitizerFunctionPass(
203 /*CompileKernel*/ true,
204 /*Recover*/ true, /*UseAfterScope*/ false));
205 PM.add(createAddressSanitizerModulePass(/*CompileKernel*/true,
209 static void addMemorySanitizerPass(const PassManagerBuilder &Builder,
210 legacy::PassManagerBase &PM) {
211 const PassManagerBuilderWrapper &BuilderWrapper =
212 static_cast<const PassManagerBuilderWrapper&>(Builder);
213 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
214 int TrackOrigins = CGOpts.SanitizeMemoryTrackOrigins;
215 bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Memory);
216 PM.add(createMemorySanitizerPass(TrackOrigins, Recover));
218 // MemorySanitizer inserts complex instrumentation that mostly follows
219 // the logic of the original code, but operates on "shadow" values.
220 // It can benefit from re-running some general purpose optimization passes.
221 if (Builder.OptLevel > 0) {
222 PM.add(createEarlyCSEPass());
223 PM.add(createReassociatePass());
224 PM.add(createLICMPass());
225 PM.add(createGVNPass());
226 PM.add(createInstructionCombiningPass());
227 PM.add(createDeadStoreEliminationPass());
231 static void addThreadSanitizerPass(const PassManagerBuilder &Builder,
232 legacy::PassManagerBase &PM) {
233 PM.add(createThreadSanitizerPass());
236 static void addDataFlowSanitizerPass(const PassManagerBuilder &Builder,
237 legacy::PassManagerBase &PM) {
238 const PassManagerBuilderWrapper &BuilderWrapper =
239 static_cast<const PassManagerBuilderWrapper&>(Builder);
240 const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
241 PM.add(createDataFlowSanitizerPass(LangOpts.SanitizerBlacklistFiles));
244 static void addEfficiencySanitizerPass(const PassManagerBuilder &Builder,
245 legacy::PassManagerBase &PM) {
246 const PassManagerBuilderWrapper &BuilderWrapper =
247 static_cast<const PassManagerBuilderWrapper&>(Builder);
248 const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
249 EfficiencySanitizerOptions Opts;
250 if (LangOpts.Sanitize.has(SanitizerKind::EfficiencyCacheFrag))
251 Opts.ToolType = EfficiencySanitizerOptions::ESAN_CacheFrag;
252 else if (LangOpts.Sanitize.has(SanitizerKind::EfficiencyWorkingSet))
253 Opts.ToolType = EfficiencySanitizerOptions::ESAN_WorkingSet;
254 PM.add(createEfficiencySanitizerPass(Opts));
257 static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple,
258 const CodeGenOptions &CodeGenOpts) {
259 TargetLibraryInfoImpl *TLII = new TargetLibraryInfoImpl(TargetTriple);
260 if (!CodeGenOpts.SimplifyLibCalls)
261 TLII->disableAllFunctions();
263 // Disable individual libc/libm calls in TargetLibraryInfo.
265 for (auto &FuncName : CodeGenOpts.getNoBuiltinFuncs())
266 if (TLII->getLibFunc(FuncName, F))
267 TLII->setUnavailable(F);
270 switch (CodeGenOpts.getVecLib()) {
271 case CodeGenOptions::Accelerate:
272 TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::Accelerate);
274 case CodeGenOptions::SVML:
275 TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::SVML);
283 static void addSymbolRewriterPass(const CodeGenOptions &Opts,
284 legacy::PassManager *MPM) {
285 llvm::SymbolRewriter::RewriteDescriptorList DL;
287 llvm::SymbolRewriter::RewriteMapParser MapParser;
288 for (const auto &MapFile : Opts.RewriteMapFiles)
289 MapParser.parse(MapFile, &DL);
291 MPM->add(createRewriteSymbolsPass(DL));
294 static CodeGenOpt::Level getCGOptLevel(const CodeGenOptions &CodeGenOpts) {
295 switch (CodeGenOpts.OptimizationLevel) {
297 llvm_unreachable("Invalid optimization level!");
299 return CodeGenOpt::None;
301 return CodeGenOpt::Less;
303 return CodeGenOpt::Default; // O2/Os/Oz
305 return CodeGenOpt::Aggressive;
309 static llvm::CodeModel::Model getCodeModel(const CodeGenOptions &CodeGenOpts) {
311 llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel)
312 .Case("small", llvm::CodeModel::Small)
313 .Case("kernel", llvm::CodeModel::Kernel)
314 .Case("medium", llvm::CodeModel::Medium)
315 .Case("large", llvm::CodeModel::Large)
316 .Case("default", llvm::CodeModel::Default)
318 assert(CodeModel != ~0u && "invalid code model!");
319 return static_cast<llvm::CodeModel::Model>(CodeModel);
322 static llvm::Reloc::Model getRelocModel(const CodeGenOptions &CodeGenOpts) {
323 // Keep this synced with the equivalent code in
324 // lib/Frontend/CompilerInvocation.cpp
325 llvm::Optional<llvm::Reloc::Model> RM;
326 RM = llvm::StringSwitch<llvm::Reloc::Model>(CodeGenOpts.RelocationModel)
327 .Case("static", llvm::Reloc::Static)
328 .Case("pic", llvm::Reloc::PIC_)
329 .Case("ropi", llvm::Reloc::ROPI)
330 .Case("rwpi", llvm::Reloc::RWPI)
331 .Case("ropi-rwpi", llvm::Reloc::ROPI_RWPI)
332 .Case("dynamic-no-pic", llvm::Reloc::DynamicNoPIC);
333 assert(RM.hasValue() && "invalid PIC model!");
337 static TargetMachine::CodeGenFileType getCodeGenFileType(BackendAction Action) {
338 if (Action == Backend_EmitObj)
339 return TargetMachine::CGFT_ObjectFile;
340 else if (Action == Backend_EmitMCNull)
341 return TargetMachine::CGFT_Null;
343 assert(Action == Backend_EmitAssembly && "Invalid action!");
344 return TargetMachine::CGFT_AssemblyFile;
348 static void initTargetOptions(llvm::TargetOptions &Options,
349 const CodeGenOptions &CodeGenOpts,
350 const clang::TargetOptions &TargetOpts,
351 const LangOptions &LangOpts,
352 const HeaderSearchOptions &HSOpts) {
353 Options.ThreadModel =
354 llvm::StringSwitch<llvm::ThreadModel::Model>(CodeGenOpts.ThreadModel)
355 .Case("posix", llvm::ThreadModel::POSIX)
356 .Case("single", llvm::ThreadModel::Single);
358 // Set float ABI type.
359 assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" ||
360 CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) &&
361 "Invalid Floating Point ABI!");
362 Options.FloatABIType =
363 llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI)
364 .Case("soft", llvm::FloatABI::Soft)
365 .Case("softfp", llvm::FloatABI::Soft)
366 .Case("hard", llvm::FloatABI::Hard)
367 .Default(llvm::FloatABI::Default);
369 // Set FP fusion mode.
370 switch (LangOpts.getDefaultFPContractMode()) {
371 case LangOptions::FPC_Off:
372 // Preserve any contraction performed by the front-end. (Strict performs
373 // splitting of the muladd instrinsic in the backend.)
374 Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
376 case LangOptions::FPC_On:
377 Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
379 case LangOptions::FPC_Fast:
380 Options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
384 Options.UseInitArray = CodeGenOpts.UseInitArray;
385 Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS;
386 Options.CompressDebugSections = CodeGenOpts.CompressDebugSections;
387 Options.RelaxELFRelocations = CodeGenOpts.RelaxELFRelocations;
390 Options.EABIVersion = llvm::StringSwitch<llvm::EABI>(TargetOpts.EABIVersion)
391 .Case("4", llvm::EABI::EABI4)
392 .Case("5", llvm::EABI::EABI5)
393 .Case("gnu", llvm::EABI::GNU)
394 .Default(llvm::EABI::Default);
396 if (LangOpts.SjLjExceptions)
397 Options.ExceptionModel = llvm::ExceptionHandling::SjLj;
399 Options.NoInfsFPMath = CodeGenOpts.NoInfsFPMath;
400 Options.NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath;
401 Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
402 Options.UnsafeFPMath = CodeGenOpts.UnsafeFPMath;
403 Options.StackAlignmentOverride = CodeGenOpts.StackAlignment;
404 Options.FunctionSections = CodeGenOpts.FunctionSections;
405 Options.DataSections = CodeGenOpts.DataSections;
406 Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames;
407 Options.EmulatedTLS = CodeGenOpts.EmulatedTLS;
408 Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning();
410 Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll;
411 Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels;
412 Options.MCOptions.MCUseDwarfDirectory = !CodeGenOpts.NoDwarfDirectoryAsm;
413 Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack;
414 Options.MCOptions.MCIncrementalLinkerCompatible =
415 CodeGenOpts.IncrementalLinkerCompatible;
416 Options.MCOptions.MCPIECopyRelocations = CodeGenOpts.PIECopyRelocations;
417 Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings;
418 Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose;
419 Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments;
420 Options.MCOptions.ABIName = TargetOpts.ABI;
421 for (const auto &Entry : HSOpts.UserEntries)
422 if (!Entry.IsFramework &&
423 (Entry.Group == frontend::IncludeDirGroup::Quoted ||
424 Entry.Group == frontend::IncludeDirGroup::Angled ||
425 Entry.Group == frontend::IncludeDirGroup::System))
426 Options.MCOptions.IASSearchPaths.push_back(
427 Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path);
430 void EmitAssemblyHelper::CreatePasses(legacy::PassManager &MPM,
431 legacy::FunctionPassManager &FPM) {
432 // Handle disabling of all LLVM passes, where we want to preserve the
433 // internal module before any optimization.
434 if (CodeGenOpts.DisableLLVMPasses)
437 PassManagerBuilderWrapper PMBuilder(CodeGenOpts, LangOpts);
439 // Figure out TargetLibraryInfo. This needs to be added to MPM and FPM
440 // manually (and not via PMBuilder), since some passes (eg. InstrProfiling)
441 // are inserted before PMBuilder ones - they'd get the default-constructed
442 // TLI with an unknown target otherwise.
443 Triple TargetTriple(TheModule->getTargetTriple());
444 std::unique_ptr<TargetLibraryInfoImpl> TLII(
445 createTLII(TargetTriple, CodeGenOpts));
447 // At O0 and O1 we only run the always inliner which is more efficient. At
448 // higher optimization levels we run the normal inliner.
449 if (CodeGenOpts.OptimizationLevel <= 1) {
450 bool InsertLifetimeIntrinsics = (CodeGenOpts.OptimizationLevel != 0 &&
451 !CodeGenOpts.DisableLifetimeMarkers);
452 PMBuilder.Inliner = createAlwaysInlinerLegacyPass(InsertLifetimeIntrinsics);
454 // We do not want to inline hot callsites for SamplePGO module-summary build
455 // because profile annotation will happen again in ThinLTO backend, and we
456 // want the IR of the hot path to match the profile.
457 PMBuilder.Inliner = createFunctionInliningPass(
458 CodeGenOpts.OptimizationLevel, CodeGenOpts.OptimizeSize,
459 (!CodeGenOpts.SampleProfileFile.empty() &&
460 CodeGenOpts.EmitSummaryIndex));
463 PMBuilder.OptLevel = CodeGenOpts.OptimizationLevel;
464 PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize;
465 PMBuilder.BBVectorize = CodeGenOpts.VectorizeBB;
466 PMBuilder.SLPVectorize = CodeGenOpts.VectorizeSLP;
467 PMBuilder.LoopVectorize = CodeGenOpts.VectorizeLoop;
469 PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
470 PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions;
471 PMBuilder.PrepareForThinLTO = CodeGenOpts.EmitSummaryIndex;
472 PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
473 PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
475 MPM.add(new TargetLibraryInfoWrapperPass(*TLII));
478 TM->adjustPassManager(PMBuilder);
480 if (CodeGenOpts.DebugInfoForProfiling ||
481 !CodeGenOpts.SampleProfileFile.empty())
482 PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
483 addAddDiscriminatorsPass);
485 // In ObjC ARC mode, add the main ARC optimization passes.
486 if (LangOpts.ObjCAutoRefCount) {
487 PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
488 addObjCARCExpandPass);
489 PMBuilder.addExtension(PassManagerBuilder::EP_ModuleOptimizerEarly,
490 addObjCARCAPElimPass);
491 PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
495 if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) {
496 PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
497 addBoundsCheckingPass);
498 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
499 addBoundsCheckingPass);
502 if (CodeGenOpts.SanitizeCoverageType ||
503 CodeGenOpts.SanitizeCoverageIndirectCalls ||
504 CodeGenOpts.SanitizeCoverageTraceCmp) {
505 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
506 addSanitizerCoveragePass);
507 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
508 addSanitizerCoveragePass);
511 if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
512 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
513 addAddressSanitizerPasses);
514 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
515 addAddressSanitizerPasses);
518 if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) {
519 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
520 addKernelAddressSanitizerPasses);
521 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
522 addKernelAddressSanitizerPasses);
525 if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
526 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
527 addMemorySanitizerPass);
528 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
529 addMemorySanitizerPass);
532 if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
533 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
534 addThreadSanitizerPass);
535 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
536 addThreadSanitizerPass);
539 if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
540 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
541 addDataFlowSanitizerPass);
542 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
543 addDataFlowSanitizerPass);
546 if (LangOpts.CoroutinesTS)
547 addCoroutinePassesToExtensionPoints(PMBuilder);
549 if (LangOpts.Sanitize.hasOneOf(SanitizerKind::Efficiency)) {
550 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
551 addEfficiencySanitizerPass);
552 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
553 addEfficiencySanitizerPass);
556 // Set up the per-function pass manager.
557 FPM.add(new TargetLibraryInfoWrapperPass(*TLII));
558 if (CodeGenOpts.VerifyModule)
559 FPM.add(createVerifierPass());
561 // Set up the per-module pass manager.
562 if (!CodeGenOpts.RewriteMapFiles.empty())
563 addSymbolRewriterPass(CodeGenOpts, &MPM);
565 if (!CodeGenOpts.DisableGCov &&
566 (CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes)) {
567 // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if
568 // LLVM's -default-gcov-version flag is set to something invalid.
570 Options.EmitNotes = CodeGenOpts.EmitGcovNotes;
571 Options.EmitData = CodeGenOpts.EmitGcovArcs;
572 memcpy(Options.Version, CodeGenOpts.CoverageVersion, 4);
573 Options.UseCfgChecksum = CodeGenOpts.CoverageExtraChecksum;
574 Options.NoRedZone = CodeGenOpts.DisableRedZone;
575 Options.FunctionNamesInData =
576 !CodeGenOpts.CoverageNoFunctionNamesInData;
577 Options.ExitBlockBeforeBody = CodeGenOpts.CoverageExitBlockBeforeBody;
578 MPM.add(createGCOVProfilerPass(Options));
579 if (CodeGenOpts.getDebugInfo() == codegenoptions::NoDebugInfo)
580 MPM.add(createStripSymbolsPass(true));
583 if (CodeGenOpts.hasProfileClangInstr()) {
584 InstrProfOptions Options;
585 Options.NoRedZone = CodeGenOpts.DisableRedZone;
586 Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput;
587 MPM.add(createInstrProfilingLegacyPass(Options));
589 if (CodeGenOpts.hasProfileIRInstr()) {
590 PMBuilder.EnablePGOInstrGen = true;
591 if (!CodeGenOpts.InstrProfileOutput.empty())
592 PMBuilder.PGOInstrGen = CodeGenOpts.InstrProfileOutput;
594 PMBuilder.PGOInstrGen = DefaultProfileGenName;
596 if (CodeGenOpts.hasProfileIRUse())
597 PMBuilder.PGOInstrUse = CodeGenOpts.ProfileInstrumentUsePath;
599 if (!CodeGenOpts.SampleProfileFile.empty())
600 PMBuilder.PGOSampleUse = CodeGenOpts.SampleProfileFile;
602 PMBuilder.populateFunctionPassManager(FPM);
603 PMBuilder.populateModulePassManager(MPM);
606 static void setCommandLineOpts(const CodeGenOptions &CodeGenOpts) {
607 SmallVector<const char *, 16> BackendArgs;
608 BackendArgs.push_back("clang"); // Fake program name.
609 if (!CodeGenOpts.DebugPass.empty()) {
610 BackendArgs.push_back("-debug-pass");
611 BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
613 if (!CodeGenOpts.LimitFloatPrecision.empty()) {
614 BackendArgs.push_back("-limit-float-precision");
615 BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
617 for (const std::string &BackendOption : CodeGenOpts.BackendOptions)
618 BackendArgs.push_back(BackendOption.c_str());
619 BackendArgs.push_back(nullptr);
620 llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
624 void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
625 // Create the TargetMachine for generating code.
627 std::string Triple = TheModule->getTargetTriple();
628 const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
631 Diags.Report(diag::err_fe_unable_to_create_target) << Error;
635 llvm::CodeModel::Model CM = getCodeModel(CodeGenOpts);
636 std::string FeaturesStr =
637 llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ",");
638 llvm::Reloc::Model RM = getRelocModel(CodeGenOpts);
639 CodeGenOpt::Level OptLevel = getCGOptLevel(CodeGenOpts);
641 llvm::TargetOptions Options;
642 initTargetOptions(Options, CodeGenOpts, TargetOpts, LangOpts, HSOpts);
643 TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr,
644 Options, RM, CM, OptLevel));
647 bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses,
648 BackendAction Action,
649 raw_pwrite_stream &OS) {
651 llvm::Triple TargetTriple(TheModule->getTargetTriple());
652 std::unique_ptr<TargetLibraryInfoImpl> TLII(
653 createTLII(TargetTriple, CodeGenOpts));
654 CodeGenPasses.add(new TargetLibraryInfoWrapperPass(*TLII));
656 // Normal mode, emit a .s or .o file by running the code generator. Note,
657 // this also adds codegenerator level optimization passes.
658 TargetMachine::CodeGenFileType CGFT = getCodeGenFileType(Action);
660 // Add ObjC ARC final-cleanup optimizations. This is done as part of the
661 // "codegen" passes so that it isn't run multiple times when there is
662 // inlining happening.
663 if (CodeGenOpts.OptimizationLevel > 0)
664 CodeGenPasses.add(createObjCARCContractPass());
666 if (TM->addPassesToEmitFile(CodeGenPasses, OS, CGFT,
667 /*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
668 Diags.Report(diag::err_fe_unable_to_interface_with_target);
675 void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
676 std::unique_ptr<raw_pwrite_stream> OS) {
677 TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr);
679 setCommandLineOpts(CodeGenOpts);
681 bool UsesCodeGen = (Action != Backend_EmitNothing &&
682 Action != Backend_EmitBC &&
683 Action != Backend_EmitLL);
684 CreateTargetMachine(UsesCodeGen);
686 if (UsesCodeGen && !TM)
689 TheModule->setDataLayout(TM->createDataLayout());
691 legacy::PassManager PerModulePasses;
693 createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
695 legacy::FunctionPassManager PerFunctionPasses(TheModule);
696 PerFunctionPasses.add(
697 createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
699 CreatePasses(PerModulePasses, PerFunctionPasses);
701 legacy::PassManager CodeGenPasses;
703 createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
705 std::unique_ptr<raw_fd_ostream> ThinLinkOS;
708 case Backend_EmitNothing:
712 if (CodeGenOpts.EmitSummaryIndex) {
713 if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
715 ThinLinkOS.reset(new llvm::raw_fd_ostream(
716 CodeGenOpts.ThinLinkBitcodeFile, EC,
717 llvm::sys::fs::F_None));
719 Diags.Report(diag::err_fe_unable_to_open_output) << CodeGenOpts.ThinLinkBitcodeFile
725 createWriteThinLTOBitcodePass(*OS, ThinLinkOS.get()));
729 createBitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists));
734 createPrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
738 if (!AddEmitPasses(CodeGenPasses, Action, *OS))
742 // Before executing passes, print the final values of the LLVM options.
743 cl::PrintOptionValues();
745 // Run passes. For now we do all passes at once, but eventually we
746 // would like to have the option of streaming code generation.
749 PrettyStackTraceString CrashInfo("Per-function optimization");
751 PerFunctionPasses.doInitialization();
752 for (Function &F : *TheModule)
753 if (!F.isDeclaration())
754 PerFunctionPasses.run(F);
755 PerFunctionPasses.doFinalization();
759 PrettyStackTraceString CrashInfo("Per-module optimization passes");
760 PerModulePasses.run(*TheModule);
764 PrettyStackTraceString CrashInfo("Code generation");
765 CodeGenPasses.run(*TheModule);
769 static PassBuilder::OptimizationLevel mapToLevel(const CodeGenOptions &Opts) {
770 switch (Opts.OptimizationLevel) {
772 llvm_unreachable("Invalid optimization level!");
775 return PassBuilder::O1;
778 switch (Opts.OptimizeSize) {
780 llvm_unreachable("Invalide optimization level for size!");
783 return PassBuilder::O2;
786 return PassBuilder::Os;
789 return PassBuilder::Oz;
793 return PassBuilder::O3;
797 /// A clean version of `EmitAssembly` that uses the new pass manager.
799 /// Not all features are currently supported in this system, but where
800 /// necessary it falls back to the legacy pass manager to at least provide
801 /// basic functionality.
803 /// This API is planned to have its functionality finished and then to replace
804 /// `EmitAssembly` at some point in the future when the default switches.
805 void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
806 BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS) {
807 TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr);
808 setCommandLineOpts(CodeGenOpts);
810 // The new pass manager always makes a target machine available to passes
811 // during construction.
812 CreateTargetMachine(/*MustCreateTM*/ true);
814 // This will already be diagnosed, just bail.
816 TheModule->setDataLayout(TM->createDataLayout());
820 // -fprofile-generate.
821 PGOOpt.RunProfileGen = CodeGenOpts.hasProfileIRInstr();
822 if (PGOOpt.RunProfileGen)
823 PGOOpt.ProfileGenFile = CodeGenOpts.InstrProfileOutput.empty() ?
824 DefaultProfileGenName : CodeGenOpts.InstrProfileOutput;
827 if (CodeGenOpts.hasProfileIRUse())
828 PGOOpt.ProfileUseFile = CodeGenOpts.ProfileInstrumentUsePath;
830 // Only pass a PGO options struct if -fprofile-generate or
831 // -fprofile-use were passed on the cmdline.
832 PassBuilder PB(TM.get(),
833 (PGOOpt.RunProfileGen ||
834 !PGOOpt.ProfileUseFile.empty()) ?
835 Optional<PGOOptions>(PGOOpt) : None);
837 LoopAnalysisManager LAM;
838 FunctionAnalysisManager FAM;
839 CGSCCAnalysisManager CGAM;
840 ModuleAnalysisManager MAM;
842 // Register the AA manager first so that our version is the one used.
843 FAM.registerPass([&] { return PB.buildDefaultAAPipeline(); });
845 // Register all the basic analyses with the managers.
846 PB.registerModuleAnalyses(MAM);
847 PB.registerCGSCCAnalyses(CGAM);
848 PB.registerFunctionAnalyses(FAM);
849 PB.registerLoopAnalyses(LAM);
850 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
852 ModulePassManager MPM;
854 if (!CodeGenOpts.DisableLLVMPasses) {
855 if (CodeGenOpts.OptimizationLevel == 0) {
856 // Build a minimal pipeline based on the semantics required by Clang,
857 // which is just that always inlining occurs.
858 MPM.addPass(AlwaysInlinerPass());
860 // Otherwise, use the default pass pipeline. We also have to map our
861 // optimization levels into one of the distinct levels used to configure
863 PassBuilder::OptimizationLevel Level = mapToLevel(CodeGenOpts);
865 MPM = PB.buildPerModuleDefaultPipeline(Level);
869 // FIXME: We still use the legacy pass manager to do code generation. We
870 // create that pass manager here and use it as needed below.
871 legacy::PassManager CodeGenPasses;
872 bool NeedCodeGen = false;
874 // Append any output we need to the pass manager.
876 case Backend_EmitNothing:
880 MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
881 CodeGenOpts.EmitSummaryIndex,
882 CodeGenOpts.EmitSummaryIndex));
886 MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
889 case Backend_EmitAssembly:
890 case Backend_EmitMCNull:
891 case Backend_EmitObj:
894 createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
895 if (!AddEmitPasses(CodeGenPasses, Action, *OS))
896 // FIXME: Should we handle this error differently?
901 // Before executing passes, print the final values of the LLVM options.
902 cl::PrintOptionValues();
904 // Now that we have all of the passes ready, run them.
906 PrettyStackTraceString CrashInfo("Optimizer");
907 MPM.run(*TheModule, MAM);
910 // Now if needed, run the legacy PM for codegen.
912 PrettyStackTraceString CrashInfo("Code generation");
913 CodeGenPasses.run(*TheModule);
917 Expected<BitcodeModule> clang::FindThinLTOModule(MemoryBufferRef MBRef) {
918 Expected<std::vector<BitcodeModule>> BMsOrErr = getBitcodeModuleList(MBRef);
920 return BMsOrErr.takeError();
922 // The bitcode file may contain multiple modules, we want the one with a
924 for (BitcodeModule &BM : *BMsOrErr) {
925 Expected<bool> HasSummary = BM.hasSummary();
926 if (HasSummary && *HasSummary)
930 return make_error<StringError>("Could not find module summary",
931 inconvertibleErrorCode());
934 static void runThinLTOBackend(ModuleSummaryIndex *CombinedIndex, Module *M,
935 const HeaderSearchOptions &HeaderOpts,
936 const CodeGenOptions &CGOpts,
937 const clang::TargetOptions &TOpts,
938 const LangOptions &LOpts,
939 std::unique_ptr<raw_pwrite_stream> OS,
940 std::string SampleProfile,
941 BackendAction Action) {
942 StringMap<std::map<GlobalValue::GUID, GlobalValueSummary *>>
943 ModuleToDefinedGVSummaries;
944 CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
946 setCommandLineOpts(CGOpts);
948 // We can simply import the values mentioned in the combined index, since
949 // we should only invoke this using the individual indexes written out
950 // via a WriteIndexesThinBackend.
951 FunctionImporter::ImportMapTy ImportList;
952 for (auto &GlobalList : *CombinedIndex) {
953 auto GUID = GlobalList.first;
954 assert(GlobalList.second.size() == 1 &&
955 "Expected individual combined index to have one summary per GUID");
956 auto &Summary = GlobalList.second[0];
957 // Skip the summaries for the importing module. These are included to
958 // e.g. record required linkage changes.
959 if (Summary->modulePath() == M->getModuleIdentifier())
961 // Doesn't matter what value we plug in to the map, just needs an entry
962 // to provoke importing by thinBackend.
963 ImportList[Summary->modulePath()][GUID] = 1;
966 std::vector<std::unique_ptr<llvm::MemoryBuffer>> OwnedImports;
967 MapVector<llvm::StringRef, llvm::BitcodeModule> ModuleMap;
969 for (auto &I : ImportList) {
970 ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MBOrErr =
971 llvm::MemoryBuffer::getFile(I.first());
973 errs() << "Error loading imported file '" << I.first()
974 << "': " << MBOrErr.getError().message() << "\n";
978 Expected<BitcodeModule> BMOrErr = FindThinLTOModule(**MBOrErr);
980 handleAllErrors(BMOrErr.takeError(), [&](ErrorInfoBase &EIB) {
981 errs() << "Error loading imported file '" << I.first()
982 << "': " << EIB.message() << '\n';
986 ModuleMap.insert({I.first(), *BMOrErr});
988 OwnedImports.push_back(std::move(*MBOrErr));
990 auto AddStream = [&](size_t Task) {
991 return llvm::make_unique<lto::NativeObjectStream>(std::move(OS));
994 Conf.CPU = TOpts.CPU;
995 Conf.CodeModel = getCodeModel(CGOpts);
996 Conf.MAttrs = TOpts.Features;
997 Conf.RelocModel = getRelocModel(CGOpts);
998 Conf.CGOptLevel = getCGOptLevel(CGOpts);
999 initTargetOptions(Conf.Options, CGOpts, TOpts, LOpts, HeaderOpts);
1000 Conf.SampleProfile = std::move(SampleProfile);
1002 case Backend_EmitNothing:
1003 Conf.PreCodeGenModuleHook = [](size_t Task, const Module &Mod) {
1007 case Backend_EmitLL:
1008 Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1009 M->print(*OS, nullptr, CGOpts.EmitLLVMUseLists);
1013 case Backend_EmitBC:
1014 Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1015 WriteBitcodeToFile(M, *OS, CGOpts.EmitLLVMUseLists);
1020 Conf.CGFileType = getCodeGenFileType(Action);
1023 if (Error E = thinBackend(
1024 Conf, 0, AddStream, *M, *CombinedIndex, ImportList,
1025 ModuleToDefinedGVSummaries[M->getModuleIdentifier()], ModuleMap)) {
1026 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
1027 errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
1032 void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
1033 const HeaderSearchOptions &HeaderOpts,
1034 const CodeGenOptions &CGOpts,
1035 const clang::TargetOptions &TOpts,
1036 const LangOptions &LOpts,
1037 const llvm::DataLayout &TDesc, Module *M,
1038 BackendAction Action,
1039 std::unique_ptr<raw_pwrite_stream> OS) {
1040 if (!CGOpts.ThinLTOIndexFile.empty()) {
1041 // If we are performing a ThinLTO importing compile, load the function index
1042 // into memory and pass it into runThinLTOBackend, which will run the
1043 // function importer and invoke LTO passes.
1044 Expected<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr =
1045 llvm::getModuleSummaryIndexForFile(CGOpts.ThinLTOIndexFile);
1047 logAllUnhandledErrors(IndexOrErr.takeError(), errs(),
1048 "Error loading index file '" +
1049 CGOpts.ThinLTOIndexFile + "': ");
1052 std::unique_ptr<ModuleSummaryIndex> CombinedIndex = std::move(*IndexOrErr);
1053 // A null CombinedIndex means we should skip ThinLTO compilation
1054 // (LLVM will optionally ignore empty index files, returning null instead
1056 bool DoThinLTOBackend = CombinedIndex != nullptr;
1057 if (DoThinLTOBackend) {
1058 runThinLTOBackend(CombinedIndex.get(), M, HeaderOpts, CGOpts, TOpts,
1059 LOpts, std::move(OS), CGOpts.SampleProfileFile, Action);
1064 EmitAssemblyHelper AsmHelper(Diags, HeaderOpts, CGOpts, TOpts, LOpts, M);
1066 if (CGOpts.ExperimentalNewPassManager)
1067 AsmHelper.EmitAssemblyWithNewPassManager(Action, std::move(OS));
1069 AsmHelper.EmitAssembly(Action, std::move(OS));
1071 // Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
1074 std::string DLDesc = M->getDataLayout().getStringRepresentation();
1075 if (DLDesc != TDesc.getStringRepresentation()) {
1076 unsigned DiagID = Diags.getCustomDiagID(
1077 DiagnosticsEngine::Error, "backend data layout '%0' does not match "
1078 "expected target description '%1'");
1079 Diags.Report(DiagID) << DLDesc << TDesc.getStringRepresentation();
1084 static const char* getSectionNameForBitcode(const Triple &T) {
1085 switch (T.getObjectFormat()) {
1087 return "__LLVM,__bitcode";
1091 case Triple::UnknownObjectFormat:
1094 llvm_unreachable("Unimplemented ObjectFormatType");
1097 static const char* getSectionNameForCommandline(const Triple &T) {
1098 switch (T.getObjectFormat()) {
1100 return "__LLVM,__cmdline";
1104 case Triple::UnknownObjectFormat:
1107 llvm_unreachable("Unimplemented ObjectFormatType");
1110 // With -fembed-bitcode, save a copy of the llvm IR as data in the
1111 // __LLVM,__bitcode section.
1112 void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
1113 llvm::MemoryBufferRef Buf) {
1114 if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off)
1117 // Save llvm.compiler.used and remote it.
1118 SmallVector<Constant*, 2> UsedArray;
1119 SmallSet<GlobalValue*, 4> UsedGlobals;
1120 Type *UsedElementType = Type::getInt8Ty(M->getContext())->getPointerTo(0);
1121 GlobalVariable *Used = collectUsedGlobalVariables(*M, UsedGlobals, true);
1122 for (auto *GV : UsedGlobals) {
1123 if (GV->getName() != "llvm.embedded.module" &&
1124 GV->getName() != "llvm.cmdline")
1125 UsedArray.push_back(
1126 ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
1129 Used->eraseFromParent();
1131 // Embed the bitcode for the llvm module.
1133 ArrayRef<uint8_t> ModuleData;
1134 Triple T(M->getTargetTriple());
1135 // Create a constant that contains the bitcode.
1136 // In case of embedding a marker, ignore the input Buf and use the empty
1137 // ArrayRef. It is also legal to create a bitcode marker even Buf is empty.
1138 if (CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker) {
1139 if (!isBitcode((const unsigned char *)Buf.getBufferStart(),
1140 (const unsigned char *)Buf.getBufferEnd())) {
1141 // If the input is LLVM Assembly, bitcode is produced by serializing
1142 // the module. Use-lists order need to be perserved in this case.
1143 llvm::raw_string_ostream OS(Data);
1144 llvm::WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ true);
1146 ArrayRef<uint8_t>((const uint8_t *)OS.str().data(), OS.str().size());
1148 // If the input is LLVM bitcode, write the input byte stream directly.
1149 ModuleData = ArrayRef<uint8_t>((const uint8_t *)Buf.getBufferStart(),
1150 Buf.getBufferSize());
1152 llvm::Constant *ModuleConstant =
1153 llvm::ConstantDataArray::get(M->getContext(), ModuleData);
1154 llvm::GlobalVariable *GV = new llvm::GlobalVariable(
1155 *M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage,
1157 GV->setSection(getSectionNameForBitcode(T));
1158 UsedArray.push_back(
1159 ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
1160 if (llvm::GlobalVariable *Old =
1161 M->getGlobalVariable("llvm.embedded.module", true)) {
1162 assert(Old->hasOneUse() &&
1163 "llvm.embedded.module can only be used once in llvm.compiler.used");
1165 Old->eraseFromParent();
1167 GV->setName("llvm.embedded.module");
1170 // Skip if only bitcode needs to be embedded.
1171 if (CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode) {
1172 // Embed command-line options.
1173 ArrayRef<uint8_t> CmdData(const_cast<uint8_t *>(CGOpts.CmdArgs.data()),
1174 CGOpts.CmdArgs.size());
1175 llvm::Constant *CmdConstant =
1176 llvm::ConstantDataArray::get(M->getContext(), CmdData);
1177 GV = new llvm::GlobalVariable(*M, CmdConstant->getType(), true,
1178 llvm::GlobalValue::PrivateLinkage,
1180 GV->setSection(getSectionNameForCommandline(T));
1181 UsedArray.push_back(
1182 ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
1183 if (llvm::GlobalVariable *Old =
1184 M->getGlobalVariable("llvm.cmdline", true)) {
1185 assert(Old->hasOneUse() &&
1186 "llvm.cmdline can only be used once in llvm.compiler.used");
1188 Old->eraseFromParent();
1190 GV->setName("llvm.cmdline");
1194 if (UsedArray.empty())
1197 // Recreate llvm.compiler.used.
1198 ArrayType *ATy = ArrayType::get(UsedElementType, UsedArray.size());
1199 auto *NewUsed = new GlobalVariable(
1200 *M, ATy, false, llvm::GlobalValue::AppendingLinkage,
1201 llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used");
1202 NewUsed->setSection("llvm.metadata");