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/Passes/PassBuilder.h"
39 #include "llvm/Support/CommandLine.h"
40 #include "llvm/Support/MemoryBuffer.h"
41 #include "llvm/Support/PrettyStackTrace.h"
42 #include "llvm/Support/TargetRegistry.h"
43 #include "llvm/Support/Timer.h"
44 #include "llvm/Support/raw_ostream.h"
45 #include "llvm/Target/TargetMachine.h"
46 #include "llvm/Target/TargetOptions.h"
47 #include "llvm/Target/TargetSubtargetInfo.h"
48 #include "llvm/Transforms/Coroutines.h"
49 #include "llvm/Transforms/IPO.h"
50 #include "llvm/Transforms/IPO/AlwaysInliner.h"
51 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
52 #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.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/NameAnonGlobals.h"
58 #include "llvm/Transforms/Utils/SymbolRewriter.h"
60 using namespace clang;
65 // Default filename used for profile generation.
66 static constexpr StringLiteral DefaultProfileGenName = "default_%m.profraw";
68 class EmitAssemblyHelper {
69 DiagnosticsEngine &Diags;
70 const HeaderSearchOptions &HSOpts;
71 const CodeGenOptions &CodeGenOpts;
72 const clang::TargetOptions &TargetOpts;
73 const LangOptions &LangOpts;
76 Timer CodeGenerationTime;
78 std::unique_ptr<raw_pwrite_stream> OS;
80 TargetIRAnalysis getTargetIRAnalysis() const {
82 return TM->getTargetIRAnalysis();
84 return TargetIRAnalysis();
87 void CreatePasses(legacy::PassManager &MPM, legacy::FunctionPassManager &FPM);
89 /// Generates the TargetMachine.
90 /// Leaves TM unchanged if it is unable to create the target machine.
91 /// Some of our clang tests specify triples which are not built
92 /// into clang. This is okay because these tests check the generated
93 /// IR, and they require DataLayout which depends on the triple.
94 /// In this case, we allow this method to fail and not report an error.
95 /// When MustCreateTM is used, we print an error if we are unable to load
96 /// the requested target.
97 void CreateTargetMachine(bool MustCreateTM);
99 /// Add passes necessary to emit assembly or LLVM IR.
101 /// \return True on success.
102 bool AddEmitPasses(legacy::PassManager &CodeGenPasses, BackendAction Action,
103 raw_pwrite_stream &OS);
106 EmitAssemblyHelper(DiagnosticsEngine &_Diags,
107 const HeaderSearchOptions &HeaderSearchOpts,
108 const CodeGenOptions &CGOpts,
109 const clang::TargetOptions &TOpts,
110 const LangOptions &LOpts, Module *M)
111 : Diags(_Diags), HSOpts(HeaderSearchOpts), CodeGenOpts(CGOpts),
112 TargetOpts(TOpts), LangOpts(LOpts), TheModule(M),
113 CodeGenerationTime("codegen", "Code Generation Time") {}
115 ~EmitAssemblyHelper() {
116 if (CodeGenOpts.DisableFree)
117 BuryPointer(std::move(TM));
120 std::unique_ptr<TargetMachine> TM;
122 void EmitAssembly(BackendAction Action,
123 std::unique_ptr<raw_pwrite_stream> OS);
125 void EmitAssemblyWithNewPassManager(BackendAction Action,
126 std::unique_ptr<raw_pwrite_stream> OS);
129 // We need this wrapper to access LangOpts and CGOpts from extension functions
130 // that we add to the PassManagerBuilder.
131 class PassManagerBuilderWrapper : public PassManagerBuilder {
133 PassManagerBuilderWrapper(const Triple &TargetTriple,
134 const CodeGenOptions &CGOpts,
135 const LangOptions &LangOpts)
136 : PassManagerBuilder(), TargetTriple(TargetTriple), CGOpts(CGOpts),
137 LangOpts(LangOpts) {}
138 const Triple &getTargetTriple() const { return TargetTriple; }
139 const CodeGenOptions &getCGOpts() const { return CGOpts; }
140 const LangOptions &getLangOpts() const { return LangOpts; }
143 const Triple &TargetTriple;
144 const CodeGenOptions &CGOpts;
145 const LangOptions &LangOpts;
149 static void addObjCARCAPElimPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
150 if (Builder.OptLevel > 0)
151 PM.add(createObjCARCAPElimPass());
154 static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
155 if (Builder.OptLevel > 0)
156 PM.add(createObjCARCExpandPass());
159 static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
160 if (Builder.OptLevel > 0)
161 PM.add(createObjCARCOptPass());
164 static void addAddDiscriminatorsPass(const PassManagerBuilder &Builder,
165 legacy::PassManagerBase &PM) {
166 PM.add(createAddDiscriminatorsPass());
169 static void addBoundsCheckingPass(const PassManagerBuilder &Builder,
170 legacy::PassManagerBase &PM) {
171 PM.add(createBoundsCheckingPass());
174 static void addSanitizerCoveragePass(const PassManagerBuilder &Builder,
175 legacy::PassManagerBase &PM) {
176 const PassManagerBuilderWrapper &BuilderWrapper =
177 static_cast<const PassManagerBuilderWrapper&>(Builder);
178 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
179 SanitizerCoverageOptions Opts;
181 static_cast<SanitizerCoverageOptions::Type>(CGOpts.SanitizeCoverageType);
182 Opts.IndirectCalls = CGOpts.SanitizeCoverageIndirectCalls;
183 Opts.TraceBB = CGOpts.SanitizeCoverageTraceBB;
184 Opts.TraceCmp = CGOpts.SanitizeCoverageTraceCmp;
185 Opts.TraceDiv = CGOpts.SanitizeCoverageTraceDiv;
186 Opts.TraceGep = CGOpts.SanitizeCoverageTraceGep;
187 Opts.Use8bitCounters = CGOpts.SanitizeCoverage8bitCounters;
188 Opts.TracePC = CGOpts.SanitizeCoverageTracePC;
189 Opts.TracePCGuard = CGOpts.SanitizeCoverageTracePCGuard;
190 Opts.NoPrune = CGOpts.SanitizeCoverageNoPrune;
191 Opts.Inline8bitCounters = CGOpts.SanitizeCoverageInline8bitCounters;
192 PM.add(createSanitizerCoverageModulePass(Opts));
195 // Check if ASan should use GC-friendly instrumentation for globals.
196 // First of all, there is no point if -fdata-sections is off (expect for MachO,
197 // where this is not a factor). Also, on ELF this feature requires an assembler
198 // extension that only works with -integrated-as at the moment.
199 static bool asanUseGlobalsGC(const Triple &T, const CodeGenOptions &CGOpts) {
200 if (!CGOpts.SanitizeAddressGlobalsDeadStripping)
202 switch (T.getObjectFormat()) {
207 return CGOpts.DataSections && !CGOpts.DisableIntegratedAS;
213 static void addAddressSanitizerPasses(const PassManagerBuilder &Builder,
214 legacy::PassManagerBase &PM) {
215 const PassManagerBuilderWrapper &BuilderWrapper =
216 static_cast<const PassManagerBuilderWrapper&>(Builder);
217 const Triple &T = BuilderWrapper.getTargetTriple();
218 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
219 bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Address);
220 bool UseAfterScope = CGOpts.SanitizeAddressUseAfterScope;
221 bool UseGlobalsGC = asanUseGlobalsGC(T, CGOpts);
222 PM.add(createAddressSanitizerFunctionPass(/*CompileKernel*/ false, Recover,
224 PM.add(createAddressSanitizerModulePass(/*CompileKernel*/ false, Recover,
228 static void addKernelAddressSanitizerPasses(const PassManagerBuilder &Builder,
229 legacy::PassManagerBase &PM) {
230 PM.add(createAddressSanitizerFunctionPass(
231 /*CompileKernel*/ true,
232 /*Recover*/ true, /*UseAfterScope*/ false));
233 PM.add(createAddressSanitizerModulePass(/*CompileKernel*/true,
237 static void addMemorySanitizerPass(const PassManagerBuilder &Builder,
238 legacy::PassManagerBase &PM) {
239 const PassManagerBuilderWrapper &BuilderWrapper =
240 static_cast<const PassManagerBuilderWrapper&>(Builder);
241 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
242 int TrackOrigins = CGOpts.SanitizeMemoryTrackOrigins;
243 bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Memory);
244 PM.add(createMemorySanitizerPass(TrackOrigins, Recover));
246 // MemorySanitizer inserts complex instrumentation that mostly follows
247 // the logic of the original code, but operates on "shadow" values.
248 // It can benefit from re-running some general purpose optimization passes.
249 if (Builder.OptLevel > 0) {
250 PM.add(createEarlyCSEPass());
251 PM.add(createReassociatePass());
252 PM.add(createLICMPass());
253 PM.add(createGVNPass());
254 PM.add(createInstructionCombiningPass());
255 PM.add(createDeadStoreEliminationPass());
259 static void addThreadSanitizerPass(const PassManagerBuilder &Builder,
260 legacy::PassManagerBase &PM) {
261 PM.add(createThreadSanitizerPass());
264 static void addDataFlowSanitizerPass(const PassManagerBuilder &Builder,
265 legacy::PassManagerBase &PM) {
266 const PassManagerBuilderWrapper &BuilderWrapper =
267 static_cast<const PassManagerBuilderWrapper&>(Builder);
268 const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
269 PM.add(createDataFlowSanitizerPass(LangOpts.SanitizerBlacklistFiles));
272 static void addEfficiencySanitizerPass(const PassManagerBuilder &Builder,
273 legacy::PassManagerBase &PM) {
274 const PassManagerBuilderWrapper &BuilderWrapper =
275 static_cast<const PassManagerBuilderWrapper&>(Builder);
276 const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
277 EfficiencySanitizerOptions Opts;
278 if (LangOpts.Sanitize.has(SanitizerKind::EfficiencyCacheFrag))
279 Opts.ToolType = EfficiencySanitizerOptions::ESAN_CacheFrag;
280 else if (LangOpts.Sanitize.has(SanitizerKind::EfficiencyWorkingSet))
281 Opts.ToolType = EfficiencySanitizerOptions::ESAN_WorkingSet;
282 PM.add(createEfficiencySanitizerPass(Opts));
285 static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple,
286 const CodeGenOptions &CodeGenOpts) {
287 TargetLibraryInfoImpl *TLII = new TargetLibraryInfoImpl(TargetTriple);
288 if (!CodeGenOpts.SimplifyLibCalls)
289 TLII->disableAllFunctions();
291 // Disable individual libc/libm calls in TargetLibraryInfo.
293 for (auto &FuncName : CodeGenOpts.getNoBuiltinFuncs())
294 if (TLII->getLibFunc(FuncName, F))
295 TLII->setUnavailable(F);
298 switch (CodeGenOpts.getVecLib()) {
299 case CodeGenOptions::Accelerate:
300 TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::Accelerate);
302 case CodeGenOptions::SVML:
303 TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::SVML);
311 static void addSymbolRewriterPass(const CodeGenOptions &Opts,
312 legacy::PassManager *MPM) {
313 llvm::SymbolRewriter::RewriteDescriptorList DL;
315 llvm::SymbolRewriter::RewriteMapParser MapParser;
316 for (const auto &MapFile : Opts.RewriteMapFiles)
317 MapParser.parse(MapFile, &DL);
319 MPM->add(createRewriteSymbolsPass(DL));
322 static CodeGenOpt::Level getCGOptLevel(const CodeGenOptions &CodeGenOpts) {
323 switch (CodeGenOpts.OptimizationLevel) {
325 llvm_unreachable("Invalid optimization level!");
327 return CodeGenOpt::None;
329 return CodeGenOpt::Less;
331 return CodeGenOpt::Default; // O2/Os/Oz
333 return CodeGenOpt::Aggressive;
337 static llvm::CodeModel::Model getCodeModel(const CodeGenOptions &CodeGenOpts) {
339 llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel)
340 .Case("small", llvm::CodeModel::Small)
341 .Case("kernel", llvm::CodeModel::Kernel)
342 .Case("medium", llvm::CodeModel::Medium)
343 .Case("large", llvm::CodeModel::Large)
344 .Case("default", llvm::CodeModel::Default)
346 assert(CodeModel != ~0u && "invalid code model!");
347 return static_cast<llvm::CodeModel::Model>(CodeModel);
350 static llvm::Reloc::Model getRelocModel(const CodeGenOptions &CodeGenOpts) {
351 // Keep this synced with the equivalent code in
352 // lib/Frontend/CompilerInvocation.cpp
353 llvm::Optional<llvm::Reloc::Model> RM;
354 RM = llvm::StringSwitch<llvm::Reloc::Model>(CodeGenOpts.RelocationModel)
355 .Case("static", llvm::Reloc::Static)
356 .Case("pic", llvm::Reloc::PIC_)
357 .Case("ropi", llvm::Reloc::ROPI)
358 .Case("rwpi", llvm::Reloc::RWPI)
359 .Case("ropi-rwpi", llvm::Reloc::ROPI_RWPI)
360 .Case("dynamic-no-pic", llvm::Reloc::DynamicNoPIC);
361 assert(RM.hasValue() && "invalid PIC model!");
365 static TargetMachine::CodeGenFileType getCodeGenFileType(BackendAction Action) {
366 if (Action == Backend_EmitObj)
367 return TargetMachine::CGFT_ObjectFile;
368 else if (Action == Backend_EmitMCNull)
369 return TargetMachine::CGFT_Null;
371 assert(Action == Backend_EmitAssembly && "Invalid action!");
372 return TargetMachine::CGFT_AssemblyFile;
376 static void initTargetOptions(llvm::TargetOptions &Options,
377 const CodeGenOptions &CodeGenOpts,
378 const clang::TargetOptions &TargetOpts,
379 const LangOptions &LangOpts,
380 const HeaderSearchOptions &HSOpts) {
381 Options.ThreadModel =
382 llvm::StringSwitch<llvm::ThreadModel::Model>(CodeGenOpts.ThreadModel)
383 .Case("posix", llvm::ThreadModel::POSIX)
384 .Case("single", llvm::ThreadModel::Single);
386 // Set float ABI type.
387 assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" ||
388 CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) &&
389 "Invalid Floating Point ABI!");
390 Options.FloatABIType =
391 llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI)
392 .Case("soft", llvm::FloatABI::Soft)
393 .Case("softfp", llvm::FloatABI::Soft)
394 .Case("hard", llvm::FloatABI::Hard)
395 .Default(llvm::FloatABI::Default);
397 // Set FP fusion mode.
398 switch (LangOpts.getDefaultFPContractMode()) {
399 case LangOptions::FPC_Off:
400 // Preserve any contraction performed by the front-end. (Strict performs
401 // splitting of the muladd instrinsic in the backend.)
402 Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
404 case LangOptions::FPC_On:
405 Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
407 case LangOptions::FPC_Fast:
408 Options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
412 Options.UseInitArray = CodeGenOpts.UseInitArray;
413 Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS;
414 Options.CompressDebugSections = CodeGenOpts.getCompressDebugSections();
415 Options.RelaxELFRelocations = CodeGenOpts.RelaxELFRelocations;
418 Options.EABIVersion = TargetOpts.EABIVersion;
420 if (LangOpts.SjLjExceptions)
421 Options.ExceptionModel = llvm::ExceptionHandling::SjLj;
423 Options.NoInfsFPMath = CodeGenOpts.NoInfsFPMath;
424 Options.NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath;
425 Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
426 Options.UnsafeFPMath = CodeGenOpts.UnsafeFPMath;
427 Options.StackAlignmentOverride = CodeGenOpts.StackAlignment;
428 Options.FunctionSections = CodeGenOpts.FunctionSections;
429 Options.DataSections = CodeGenOpts.DataSections;
430 Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames;
431 Options.EmulatedTLS = CodeGenOpts.EmulatedTLS;
432 Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning();
434 if (CodeGenOpts.EnableSplitDwarf)
435 Options.MCOptions.SplitDwarfFile = CodeGenOpts.SplitDwarfFile;
436 Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll;
437 Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels;
438 Options.MCOptions.MCUseDwarfDirectory = !CodeGenOpts.NoDwarfDirectoryAsm;
439 Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack;
440 Options.MCOptions.MCIncrementalLinkerCompatible =
441 CodeGenOpts.IncrementalLinkerCompatible;
442 Options.MCOptions.MCPIECopyRelocations = CodeGenOpts.PIECopyRelocations;
443 Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings;
444 Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose;
445 Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments;
446 Options.MCOptions.ABIName = TargetOpts.ABI;
447 for (const auto &Entry : HSOpts.UserEntries)
448 if (!Entry.IsFramework &&
449 (Entry.Group == frontend::IncludeDirGroup::Quoted ||
450 Entry.Group == frontend::IncludeDirGroup::Angled ||
451 Entry.Group == frontend::IncludeDirGroup::System))
452 Options.MCOptions.IASSearchPaths.push_back(
453 Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path);
456 void EmitAssemblyHelper::CreatePasses(legacy::PassManager &MPM,
457 legacy::FunctionPassManager &FPM) {
458 // Handle disabling of all LLVM passes, where we want to preserve the
459 // internal module before any optimization.
460 if (CodeGenOpts.DisableLLVMPasses)
463 // Figure out TargetLibraryInfo. This needs to be added to MPM and FPM
464 // manually (and not via PMBuilder), since some passes (eg. InstrProfiling)
465 // are inserted before PMBuilder ones - they'd get the default-constructed
466 // TLI with an unknown target otherwise.
467 Triple TargetTriple(TheModule->getTargetTriple());
468 std::unique_ptr<TargetLibraryInfoImpl> TLII(
469 createTLII(TargetTriple, CodeGenOpts));
471 PassManagerBuilderWrapper PMBuilder(TargetTriple, CodeGenOpts, LangOpts);
473 // At O0 and O1 we only run the always inliner which is more efficient. At
474 // higher optimization levels we run the normal inliner.
475 if (CodeGenOpts.OptimizationLevel <= 1) {
476 bool InsertLifetimeIntrinsics = (CodeGenOpts.OptimizationLevel != 0 &&
477 !CodeGenOpts.DisableLifetimeMarkers);
478 PMBuilder.Inliner = createAlwaysInlinerLegacyPass(InsertLifetimeIntrinsics);
480 // We do not want to inline hot callsites for SamplePGO module-summary build
481 // because profile annotation will happen again in ThinLTO backend, and we
482 // want the IR of the hot path to match the profile.
483 PMBuilder.Inliner = createFunctionInliningPass(
484 CodeGenOpts.OptimizationLevel, CodeGenOpts.OptimizeSize,
485 (!CodeGenOpts.SampleProfileFile.empty() &&
486 CodeGenOpts.EmitSummaryIndex));
489 PMBuilder.OptLevel = CodeGenOpts.OptimizationLevel;
490 PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize;
491 PMBuilder.SLPVectorize = CodeGenOpts.VectorizeSLP;
492 PMBuilder.LoopVectorize = CodeGenOpts.VectorizeLoop;
494 PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
495 PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions;
496 PMBuilder.PrepareForThinLTO = CodeGenOpts.EmitSummaryIndex;
497 PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
498 PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
500 MPM.add(new TargetLibraryInfoWrapperPass(*TLII));
503 TM->adjustPassManager(PMBuilder);
505 if (CodeGenOpts.DebugInfoForProfiling ||
506 !CodeGenOpts.SampleProfileFile.empty())
507 PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
508 addAddDiscriminatorsPass);
510 // In ObjC ARC mode, add the main ARC optimization passes.
511 if (LangOpts.ObjCAutoRefCount) {
512 PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
513 addObjCARCExpandPass);
514 PMBuilder.addExtension(PassManagerBuilder::EP_ModuleOptimizerEarly,
515 addObjCARCAPElimPass);
516 PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
520 if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) {
521 PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
522 addBoundsCheckingPass);
523 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
524 addBoundsCheckingPass);
527 if (CodeGenOpts.SanitizeCoverageType ||
528 CodeGenOpts.SanitizeCoverageIndirectCalls ||
529 CodeGenOpts.SanitizeCoverageTraceCmp) {
530 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
531 addSanitizerCoveragePass);
532 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
533 addSanitizerCoveragePass);
536 if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
537 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
538 addAddressSanitizerPasses);
539 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
540 addAddressSanitizerPasses);
543 if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) {
544 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
545 addKernelAddressSanitizerPasses);
546 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
547 addKernelAddressSanitizerPasses);
550 if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
551 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
552 addMemorySanitizerPass);
553 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
554 addMemorySanitizerPass);
557 if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
558 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
559 addThreadSanitizerPass);
560 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
561 addThreadSanitizerPass);
564 if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
565 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
566 addDataFlowSanitizerPass);
567 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
568 addDataFlowSanitizerPass);
571 if (LangOpts.CoroutinesTS)
572 addCoroutinePassesToExtensionPoints(PMBuilder);
574 if (LangOpts.Sanitize.hasOneOf(SanitizerKind::Efficiency)) {
575 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
576 addEfficiencySanitizerPass);
577 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
578 addEfficiencySanitizerPass);
581 // Set up the per-function pass manager.
582 FPM.add(new TargetLibraryInfoWrapperPass(*TLII));
583 if (CodeGenOpts.VerifyModule)
584 FPM.add(createVerifierPass());
586 // Set up the per-module pass manager.
587 if (!CodeGenOpts.RewriteMapFiles.empty())
588 addSymbolRewriterPass(CodeGenOpts, &MPM);
590 if (!CodeGenOpts.DisableGCov &&
591 (CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes)) {
592 // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if
593 // LLVM's -default-gcov-version flag is set to something invalid.
595 Options.EmitNotes = CodeGenOpts.EmitGcovNotes;
596 Options.EmitData = CodeGenOpts.EmitGcovArcs;
597 memcpy(Options.Version, CodeGenOpts.CoverageVersion, 4);
598 Options.UseCfgChecksum = CodeGenOpts.CoverageExtraChecksum;
599 Options.NoRedZone = CodeGenOpts.DisableRedZone;
600 Options.FunctionNamesInData =
601 !CodeGenOpts.CoverageNoFunctionNamesInData;
602 Options.ExitBlockBeforeBody = CodeGenOpts.CoverageExitBlockBeforeBody;
603 MPM.add(createGCOVProfilerPass(Options));
604 if (CodeGenOpts.getDebugInfo() == codegenoptions::NoDebugInfo)
605 MPM.add(createStripSymbolsPass(true));
608 if (CodeGenOpts.hasProfileClangInstr()) {
609 InstrProfOptions Options;
610 Options.NoRedZone = CodeGenOpts.DisableRedZone;
611 Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput;
612 MPM.add(createInstrProfilingLegacyPass(Options));
614 if (CodeGenOpts.hasProfileIRInstr()) {
615 PMBuilder.EnablePGOInstrGen = true;
616 if (!CodeGenOpts.InstrProfileOutput.empty())
617 PMBuilder.PGOInstrGen = CodeGenOpts.InstrProfileOutput;
619 PMBuilder.PGOInstrGen = DefaultProfileGenName;
621 if (CodeGenOpts.hasProfileIRUse())
622 PMBuilder.PGOInstrUse = CodeGenOpts.ProfileInstrumentUsePath;
624 if (!CodeGenOpts.SampleProfileFile.empty())
625 PMBuilder.PGOSampleUse = CodeGenOpts.SampleProfileFile;
627 PMBuilder.populateFunctionPassManager(FPM);
628 PMBuilder.populateModulePassManager(MPM);
631 static void setCommandLineOpts(const CodeGenOptions &CodeGenOpts) {
632 SmallVector<const char *, 16> BackendArgs;
633 BackendArgs.push_back("clang"); // Fake program name.
634 if (!CodeGenOpts.DebugPass.empty()) {
635 BackendArgs.push_back("-debug-pass");
636 BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
638 if (!CodeGenOpts.LimitFloatPrecision.empty()) {
639 BackendArgs.push_back("-limit-float-precision");
640 BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
642 for (const std::string &BackendOption : CodeGenOpts.BackendOptions)
643 BackendArgs.push_back(BackendOption.c_str());
644 BackendArgs.push_back(nullptr);
645 llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
649 void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
650 // Create the TargetMachine for generating code.
652 std::string Triple = TheModule->getTargetTriple();
653 const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
656 Diags.Report(diag::err_fe_unable_to_create_target) << Error;
660 llvm::CodeModel::Model CM = getCodeModel(CodeGenOpts);
661 std::string FeaturesStr =
662 llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ",");
663 llvm::Reloc::Model RM = getRelocModel(CodeGenOpts);
664 CodeGenOpt::Level OptLevel = getCGOptLevel(CodeGenOpts);
666 llvm::TargetOptions Options;
667 initTargetOptions(Options, CodeGenOpts, TargetOpts, LangOpts, HSOpts);
668 TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr,
669 Options, RM, CM, OptLevel));
672 bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses,
673 BackendAction Action,
674 raw_pwrite_stream &OS) {
676 llvm::Triple TargetTriple(TheModule->getTargetTriple());
677 std::unique_ptr<TargetLibraryInfoImpl> TLII(
678 createTLII(TargetTriple, CodeGenOpts));
679 CodeGenPasses.add(new TargetLibraryInfoWrapperPass(*TLII));
681 // Normal mode, emit a .s or .o file by running the code generator. Note,
682 // this also adds codegenerator level optimization passes.
683 TargetMachine::CodeGenFileType CGFT = getCodeGenFileType(Action);
685 // Add ObjC ARC final-cleanup optimizations. This is done as part of the
686 // "codegen" passes so that it isn't run multiple times when there is
687 // inlining happening.
688 if (CodeGenOpts.OptimizationLevel > 0)
689 CodeGenPasses.add(createObjCARCContractPass());
691 if (TM->addPassesToEmitFile(CodeGenPasses, OS, CGFT,
692 /*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
693 Diags.Report(diag::err_fe_unable_to_interface_with_target);
700 void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
701 std::unique_ptr<raw_pwrite_stream> OS) {
702 TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr);
704 setCommandLineOpts(CodeGenOpts);
706 bool UsesCodeGen = (Action != Backend_EmitNothing &&
707 Action != Backend_EmitBC &&
708 Action != Backend_EmitLL);
709 CreateTargetMachine(UsesCodeGen);
711 if (UsesCodeGen && !TM)
714 TheModule->setDataLayout(TM->createDataLayout());
716 legacy::PassManager PerModulePasses;
718 createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
720 legacy::FunctionPassManager PerFunctionPasses(TheModule);
721 PerFunctionPasses.add(
722 createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
724 CreatePasses(PerModulePasses, PerFunctionPasses);
726 legacy::PassManager CodeGenPasses;
728 createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
730 std::unique_ptr<raw_fd_ostream> ThinLinkOS;
733 case Backend_EmitNothing:
737 if (CodeGenOpts.EmitSummaryIndex) {
738 if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
740 ThinLinkOS.reset(new llvm::raw_fd_ostream(
741 CodeGenOpts.ThinLinkBitcodeFile, EC,
742 llvm::sys::fs::F_None));
744 Diags.Report(diag::err_fe_unable_to_open_output) << CodeGenOpts.ThinLinkBitcodeFile
750 createWriteThinLTOBitcodePass(*OS, ThinLinkOS.get()));
754 createBitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists));
759 createPrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
763 if (!AddEmitPasses(CodeGenPasses, Action, *OS))
767 // Before executing passes, print the final values of the LLVM options.
768 cl::PrintOptionValues();
770 // Run passes. For now we do all passes at once, but eventually we
771 // would like to have the option of streaming code generation.
774 PrettyStackTraceString CrashInfo("Per-function optimization");
776 PerFunctionPasses.doInitialization();
777 for (Function &F : *TheModule)
778 if (!F.isDeclaration())
779 PerFunctionPasses.run(F);
780 PerFunctionPasses.doFinalization();
784 PrettyStackTraceString CrashInfo("Per-module optimization passes");
785 PerModulePasses.run(*TheModule);
789 PrettyStackTraceString CrashInfo("Code generation");
790 CodeGenPasses.run(*TheModule);
794 static PassBuilder::OptimizationLevel mapToLevel(const CodeGenOptions &Opts) {
795 switch (Opts.OptimizationLevel) {
797 llvm_unreachable("Invalid optimization level!");
800 return PassBuilder::O1;
803 switch (Opts.OptimizeSize) {
805 llvm_unreachable("Invalide optimization level for size!");
808 return PassBuilder::O2;
811 return PassBuilder::Os;
814 return PassBuilder::Oz;
818 return PassBuilder::O3;
822 /// A clean version of `EmitAssembly` that uses the new pass manager.
824 /// Not all features are currently supported in this system, but where
825 /// necessary it falls back to the legacy pass manager to at least provide
826 /// basic functionality.
828 /// This API is planned to have its functionality finished and then to replace
829 /// `EmitAssembly` at some point in the future when the default switches.
830 void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
831 BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS) {
832 TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr);
833 setCommandLineOpts(CodeGenOpts);
835 // The new pass manager always makes a target machine available to passes
836 // during construction.
837 CreateTargetMachine(/*MustCreateTM*/ true);
839 // This will already be diagnosed, just bail.
841 TheModule->setDataLayout(TM->createDataLayout());
845 // -fprofile-generate.
846 PGOOpt.RunProfileGen = CodeGenOpts.hasProfileIRInstr();
847 if (PGOOpt.RunProfileGen)
848 PGOOpt.ProfileGenFile = CodeGenOpts.InstrProfileOutput.empty() ?
849 DefaultProfileGenName : CodeGenOpts.InstrProfileOutput;
852 if (CodeGenOpts.hasProfileIRUse())
853 PGOOpt.ProfileUseFile = CodeGenOpts.ProfileInstrumentUsePath;
855 if (!CodeGenOpts.SampleProfileFile.empty())
856 PGOOpt.SampleProfileFile = CodeGenOpts.SampleProfileFile;
858 // Only pass a PGO options struct if -fprofile-generate or
859 // -fprofile-use were passed on the cmdline.
860 PassBuilder PB(TM.get(),
861 (PGOOpt.RunProfileGen ||
862 !PGOOpt.ProfileUseFile.empty() ||
863 !PGOOpt.SampleProfileFile.empty()) ?
864 Optional<PGOOptions>(PGOOpt) : None);
866 LoopAnalysisManager LAM;
867 FunctionAnalysisManager FAM;
868 CGSCCAnalysisManager CGAM;
869 ModuleAnalysisManager MAM;
871 // Register the AA manager first so that our version is the one used.
872 FAM.registerPass([&] { return PB.buildDefaultAAPipeline(); });
874 // Register all the basic analyses with the managers.
875 PB.registerModuleAnalyses(MAM);
876 PB.registerCGSCCAnalyses(CGAM);
877 PB.registerFunctionAnalyses(FAM);
878 PB.registerLoopAnalyses(LAM);
879 PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
881 ModulePassManager MPM(CodeGenOpts.DebugPassManager);
883 if (!CodeGenOpts.DisableLLVMPasses) {
884 bool IsThinLTO = CodeGenOpts.EmitSummaryIndex;
885 bool IsLTO = CodeGenOpts.PrepareForLTO;
887 if (CodeGenOpts.OptimizationLevel == 0) {
888 // Build a minimal pipeline based on the semantics required by Clang,
889 // which is just that always inlining occurs.
890 MPM.addPass(AlwaysInlinerPass());
892 MPM.addPass(NameAnonGlobalPass());
894 // Map our optimization levels into one of the distinct levels used to
895 // configure the pipeline.
896 PassBuilder::OptimizationLevel Level = mapToLevel(CodeGenOpts);
899 MPM = PB.buildThinLTOPreLinkDefaultPipeline(
900 Level, CodeGenOpts.DebugPassManager);
901 MPM.addPass(NameAnonGlobalPass());
903 MPM = PB.buildLTOPreLinkDefaultPipeline(Level,
904 CodeGenOpts.DebugPassManager);
906 MPM = PB.buildPerModuleDefaultPipeline(Level,
907 CodeGenOpts.DebugPassManager);
912 // FIXME: We still use the legacy pass manager to do code generation. We
913 // create that pass manager here and use it as needed below.
914 legacy::PassManager CodeGenPasses;
915 bool NeedCodeGen = false;
916 Optional<raw_fd_ostream> ThinLinkOS;
918 // Append any output we need to the pass manager.
920 case Backend_EmitNothing:
924 if (CodeGenOpts.EmitSummaryIndex) {
925 if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
927 ThinLinkOS.emplace(CodeGenOpts.ThinLinkBitcodeFile, EC,
928 llvm::sys::fs::F_None);
930 Diags.Report(diag::err_fe_unable_to_open_output)
931 << CodeGenOpts.ThinLinkBitcodeFile << EC.message();
936 ThinLTOBitcodeWriterPass(*OS, ThinLinkOS ? &*ThinLinkOS : nullptr));
938 MPM.addPass(BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists,
939 CodeGenOpts.EmitSummaryIndex,
940 CodeGenOpts.EmitSummaryIndex));
945 MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
948 case Backend_EmitAssembly:
949 case Backend_EmitMCNull:
950 case Backend_EmitObj:
953 createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
954 if (!AddEmitPasses(CodeGenPasses, Action, *OS))
955 // FIXME: Should we handle this error differently?
960 // Before executing passes, print the final values of the LLVM options.
961 cl::PrintOptionValues();
963 // Now that we have all of the passes ready, run them.
965 PrettyStackTraceString CrashInfo("Optimizer");
966 MPM.run(*TheModule, MAM);
969 // Now if needed, run the legacy PM for codegen.
971 PrettyStackTraceString CrashInfo("Code generation");
972 CodeGenPasses.run(*TheModule);
976 Expected<BitcodeModule> clang::FindThinLTOModule(MemoryBufferRef MBRef) {
977 Expected<std::vector<BitcodeModule>> BMsOrErr = getBitcodeModuleList(MBRef);
979 return BMsOrErr.takeError();
981 // The bitcode file may contain multiple modules, we want the one that is
982 // marked as being the ThinLTO module.
983 for (BitcodeModule &BM : *BMsOrErr) {
984 Expected<BitcodeLTOInfo> LTOInfo = BM.getLTOInfo();
985 if (LTOInfo && LTOInfo->IsThinLTO)
989 return make_error<StringError>("Could not find module summary",
990 inconvertibleErrorCode());
993 static void runThinLTOBackend(ModuleSummaryIndex *CombinedIndex, Module *M,
994 const HeaderSearchOptions &HeaderOpts,
995 const CodeGenOptions &CGOpts,
996 const clang::TargetOptions &TOpts,
997 const LangOptions &LOpts,
998 std::unique_ptr<raw_pwrite_stream> OS,
999 std::string SampleProfile,
1000 BackendAction Action) {
1001 StringMap<DenseMap<GlobalValue::GUID, GlobalValueSummary *>>
1002 ModuleToDefinedGVSummaries;
1003 CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
1005 setCommandLineOpts(CGOpts);
1007 // We can simply import the values mentioned in the combined index, since
1008 // we should only invoke this using the individual indexes written out
1009 // via a WriteIndexesThinBackend.
1010 FunctionImporter::ImportMapTy ImportList;
1011 for (auto &GlobalList : *CombinedIndex) {
1012 // Ignore entries for undefined references.
1013 if (GlobalList.second.SummaryList.empty())
1016 auto GUID = GlobalList.first;
1017 assert(GlobalList.second.SummaryList.size() == 1 &&
1018 "Expected individual combined index to have one summary per GUID");
1019 auto &Summary = GlobalList.second.SummaryList[0];
1020 // Skip the summaries for the importing module. These are included to
1021 // e.g. record required linkage changes.
1022 if (Summary->modulePath() == M->getModuleIdentifier())
1024 // Doesn't matter what value we plug in to the map, just needs an entry
1025 // to provoke importing by thinBackend.
1026 ImportList[Summary->modulePath()][GUID] = 1;
1029 std::vector<std::unique_ptr<llvm::MemoryBuffer>> OwnedImports;
1030 MapVector<llvm::StringRef, llvm::BitcodeModule> ModuleMap;
1032 for (auto &I : ImportList) {
1033 ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MBOrErr =
1034 llvm::MemoryBuffer::getFile(I.first());
1036 errs() << "Error loading imported file '" << I.first()
1037 << "': " << MBOrErr.getError().message() << "\n";
1041 Expected<BitcodeModule> BMOrErr = FindThinLTOModule(**MBOrErr);
1043 handleAllErrors(BMOrErr.takeError(), [&](ErrorInfoBase &EIB) {
1044 errs() << "Error loading imported file '" << I.first()
1045 << "': " << EIB.message() << '\n';
1049 ModuleMap.insert({I.first(), *BMOrErr});
1051 OwnedImports.push_back(std::move(*MBOrErr));
1053 auto AddStream = [&](size_t Task) {
1054 return llvm::make_unique<lto::NativeObjectStream>(std::move(OS));
1057 Conf.CPU = TOpts.CPU;
1058 Conf.CodeModel = getCodeModel(CGOpts);
1059 Conf.MAttrs = TOpts.Features;
1060 Conf.RelocModel = getRelocModel(CGOpts);
1061 Conf.CGOptLevel = getCGOptLevel(CGOpts);
1062 initTargetOptions(Conf.Options, CGOpts, TOpts, LOpts, HeaderOpts);
1063 Conf.SampleProfile = std::move(SampleProfile);
1064 Conf.UseNewPM = CGOpts.ExperimentalNewPassManager;
1066 case Backend_EmitNothing:
1067 Conf.PreCodeGenModuleHook = [](size_t Task, const Module &Mod) {
1071 case Backend_EmitLL:
1072 Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1073 M->print(*OS, nullptr, CGOpts.EmitLLVMUseLists);
1077 case Backend_EmitBC:
1078 Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1079 WriteBitcodeToFile(M, *OS, CGOpts.EmitLLVMUseLists);
1084 Conf.CGFileType = getCodeGenFileType(Action);
1087 if (Error E = thinBackend(
1088 Conf, 0, AddStream, *M, *CombinedIndex, ImportList,
1089 ModuleToDefinedGVSummaries[M->getModuleIdentifier()], ModuleMap)) {
1090 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
1091 errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
1096 void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
1097 const HeaderSearchOptions &HeaderOpts,
1098 const CodeGenOptions &CGOpts,
1099 const clang::TargetOptions &TOpts,
1100 const LangOptions &LOpts,
1101 const llvm::DataLayout &TDesc, Module *M,
1102 BackendAction Action,
1103 std::unique_ptr<raw_pwrite_stream> OS) {
1104 if (!CGOpts.ThinLTOIndexFile.empty()) {
1105 // If we are performing a ThinLTO importing compile, load the function index
1106 // into memory and pass it into runThinLTOBackend, which will run the
1107 // function importer and invoke LTO passes.
1108 Expected<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr =
1109 llvm::getModuleSummaryIndexForFile(CGOpts.ThinLTOIndexFile,
1110 /*IgnoreEmptyThinLTOIndexFile*/true);
1112 logAllUnhandledErrors(IndexOrErr.takeError(), errs(),
1113 "Error loading index file '" +
1114 CGOpts.ThinLTOIndexFile + "': ");
1117 std::unique_ptr<ModuleSummaryIndex> CombinedIndex = std::move(*IndexOrErr);
1118 // A null CombinedIndex means we should skip ThinLTO compilation
1119 // (LLVM will optionally ignore empty index files, returning null instead
1121 bool DoThinLTOBackend = CombinedIndex != nullptr;
1122 if (DoThinLTOBackend) {
1123 runThinLTOBackend(CombinedIndex.get(), M, HeaderOpts, CGOpts, TOpts,
1124 LOpts, std::move(OS), CGOpts.SampleProfileFile, Action);
1129 EmitAssemblyHelper AsmHelper(Diags, HeaderOpts, CGOpts, TOpts, LOpts, M);
1131 if (CGOpts.ExperimentalNewPassManager)
1132 AsmHelper.EmitAssemblyWithNewPassManager(Action, std::move(OS));
1134 AsmHelper.EmitAssembly(Action, std::move(OS));
1136 // Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
1139 std::string DLDesc = M->getDataLayout().getStringRepresentation();
1140 if (DLDesc != TDesc.getStringRepresentation()) {
1141 unsigned DiagID = Diags.getCustomDiagID(
1142 DiagnosticsEngine::Error, "backend data layout '%0' does not match "
1143 "expected target description '%1'");
1144 Diags.Report(DiagID) << DLDesc << TDesc.getStringRepresentation();
1149 static const char* getSectionNameForBitcode(const Triple &T) {
1150 switch (T.getObjectFormat()) {
1152 return "__LLVM,__bitcode";
1156 case Triple::UnknownObjectFormat:
1159 llvm_unreachable("Unimplemented ObjectFormatType");
1162 static const char* getSectionNameForCommandline(const Triple &T) {
1163 switch (T.getObjectFormat()) {
1165 return "__LLVM,__cmdline";
1169 case Triple::UnknownObjectFormat:
1172 llvm_unreachable("Unimplemented ObjectFormatType");
1175 // With -fembed-bitcode, save a copy of the llvm IR as data in the
1176 // __LLVM,__bitcode section.
1177 void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
1178 llvm::MemoryBufferRef Buf) {
1179 if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off)
1182 // Save llvm.compiler.used and remote it.
1183 SmallVector<Constant*, 2> UsedArray;
1184 SmallSet<GlobalValue*, 4> UsedGlobals;
1185 Type *UsedElementType = Type::getInt8Ty(M->getContext())->getPointerTo(0);
1186 GlobalVariable *Used = collectUsedGlobalVariables(*M, UsedGlobals, true);
1187 for (auto *GV : UsedGlobals) {
1188 if (GV->getName() != "llvm.embedded.module" &&
1189 GV->getName() != "llvm.cmdline")
1190 UsedArray.push_back(
1191 ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
1194 Used->eraseFromParent();
1196 // Embed the bitcode for the llvm module.
1198 ArrayRef<uint8_t> ModuleData;
1199 Triple T(M->getTargetTriple());
1200 // Create a constant that contains the bitcode.
1201 // In case of embedding a marker, ignore the input Buf and use the empty
1202 // ArrayRef. It is also legal to create a bitcode marker even Buf is empty.
1203 if (CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker) {
1204 if (!isBitcode((const unsigned char *)Buf.getBufferStart(),
1205 (const unsigned char *)Buf.getBufferEnd())) {
1206 // If the input is LLVM Assembly, bitcode is produced by serializing
1207 // the module. Use-lists order need to be perserved in this case.
1208 llvm::raw_string_ostream OS(Data);
1209 llvm::WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ true);
1211 ArrayRef<uint8_t>((const uint8_t *)OS.str().data(), OS.str().size());
1213 // If the input is LLVM bitcode, write the input byte stream directly.
1214 ModuleData = ArrayRef<uint8_t>((const uint8_t *)Buf.getBufferStart(),
1215 Buf.getBufferSize());
1217 llvm::Constant *ModuleConstant =
1218 llvm::ConstantDataArray::get(M->getContext(), ModuleData);
1219 llvm::GlobalVariable *GV = new llvm::GlobalVariable(
1220 *M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage,
1222 GV->setSection(getSectionNameForBitcode(T));
1223 UsedArray.push_back(
1224 ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
1225 if (llvm::GlobalVariable *Old =
1226 M->getGlobalVariable("llvm.embedded.module", true)) {
1227 assert(Old->hasOneUse() &&
1228 "llvm.embedded.module can only be used once in llvm.compiler.used");
1230 Old->eraseFromParent();
1232 GV->setName("llvm.embedded.module");
1235 // Skip if only bitcode needs to be embedded.
1236 if (CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode) {
1237 // Embed command-line options.
1238 ArrayRef<uint8_t> CmdData(const_cast<uint8_t *>(CGOpts.CmdArgs.data()),
1239 CGOpts.CmdArgs.size());
1240 llvm::Constant *CmdConstant =
1241 llvm::ConstantDataArray::get(M->getContext(), CmdData);
1242 GV = new llvm::GlobalVariable(*M, CmdConstant->getType(), true,
1243 llvm::GlobalValue::PrivateLinkage,
1245 GV->setSection(getSectionNameForCommandline(T));
1246 UsedArray.push_back(
1247 ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
1248 if (llvm::GlobalVariable *Old =
1249 M->getGlobalVariable("llvm.cmdline", true)) {
1250 assert(Old->hasOneUse() &&
1251 "llvm.cmdline can only be used once in llvm.compiler.used");
1253 Old->eraseFromParent();
1255 GV->setName("llvm.cmdline");
1259 if (UsedArray.empty())
1262 // Recreate llvm.compiler.used.
1263 ArrayType *ATy = ArrayType::get(UsedElementType, UsedArray.size());
1264 auto *NewUsed = new GlobalVariable(
1265 *M, ATy, false, llvm::GlobalValue::AppendingLinkage,
1266 llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used");
1267 NewUsed->setSection("llvm.metadata");