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 "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/Analysis/TargetLibraryInfo.h"
20 #include "llvm/Analysis/TargetTransformInfo.h"
21 #include "llvm/Bitcode/BitcodeWriterPass.h"
22 #include "llvm/CodeGen/RegAllocRegistry.h"
23 #include "llvm/CodeGen/SchedulerRegistry.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/FunctionInfo.h"
26 #include "llvm/IR/IRPrintingPasses.h"
27 #include "llvm/IR/LegacyPassManager.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/IR/Verifier.h"
30 #include "llvm/MC/SubtargetFeature.h"
31 #include "llvm/Object/FunctionIndexObjectFile.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/PrettyStackTrace.h"
34 #include "llvm/Support/TargetRegistry.h"
35 #include "llvm/Support/Timer.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Target/TargetMachine.h"
38 #include "llvm/Target/TargetOptions.h"
39 #include "llvm/Target/TargetSubtargetInfo.h"
40 #include "llvm/Transforms/IPO.h"
41 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
42 #include "llvm/Transforms/Instrumentation.h"
43 #include "llvm/Transforms/ObjCARC.h"
44 #include "llvm/Transforms/Scalar.h"
45 #include "llvm/Transforms/Utils/SymbolRewriter.h"
47 using namespace clang;
52 class EmitAssemblyHelper {
53 DiagnosticsEngine &Diags;
54 const CodeGenOptions &CodeGenOpts;
55 const clang::TargetOptions &TargetOpts;
56 const LangOptions &LangOpts;
59 Timer CodeGenerationTime;
61 mutable legacy::PassManager *CodeGenPasses;
62 mutable legacy::PassManager *PerModulePasses;
63 mutable legacy::FunctionPassManager *PerFunctionPasses;
66 TargetIRAnalysis getTargetIRAnalysis() const {
68 return TM->getTargetIRAnalysis();
70 return TargetIRAnalysis();
73 legacy::PassManager *getCodeGenPasses() const {
75 CodeGenPasses = new legacy::PassManager();
77 createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
82 legacy::PassManager *getPerModulePasses() const {
83 if (!PerModulePasses) {
84 PerModulePasses = new legacy::PassManager();
86 createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
88 return PerModulePasses;
91 legacy::FunctionPassManager *getPerFunctionPasses() const {
92 if (!PerFunctionPasses) {
93 PerFunctionPasses = new legacy::FunctionPassManager(TheModule);
94 PerFunctionPasses->add(
95 createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
97 return PerFunctionPasses;
100 void CreatePasses(FunctionInfoIndex *FunctionIndex);
102 /// Generates the TargetMachine.
103 /// Returns Null if it is unable to create the target machine.
104 /// Some of our clang tests specify triples which are not built
105 /// into clang. This is okay because these tests check the generated
106 /// IR, and they require DataLayout which depends on the triple.
107 /// In this case, we allow this method to fail and not report an error.
108 /// When MustCreateTM is used, we print an error if we are unable to load
109 /// the requested target.
110 TargetMachine *CreateTargetMachine(bool MustCreateTM);
112 /// Add passes necessary to emit assembly or LLVM IR.
114 /// \return True on success.
115 bool AddEmitPasses(BackendAction Action, raw_pwrite_stream &OS);
118 EmitAssemblyHelper(DiagnosticsEngine &_Diags, const CodeGenOptions &CGOpts,
119 const clang::TargetOptions &TOpts,
120 const LangOptions &LOpts, Module *M)
121 : Diags(_Diags), CodeGenOpts(CGOpts), TargetOpts(TOpts), LangOpts(LOpts),
122 TheModule(M), CodeGenerationTime("Code Generation Time"),
123 CodeGenPasses(nullptr), PerModulePasses(nullptr),
124 PerFunctionPasses(nullptr) {}
126 ~EmitAssemblyHelper() {
127 delete CodeGenPasses;
128 delete PerModulePasses;
129 delete PerFunctionPasses;
130 if (CodeGenOpts.DisableFree)
131 BuryPointer(std::move(TM));
134 std::unique_ptr<TargetMachine> TM;
136 void EmitAssembly(BackendAction Action, raw_pwrite_stream *OS);
139 // We need this wrapper to access LangOpts and CGOpts from extension functions
140 // that we add to the PassManagerBuilder.
141 class PassManagerBuilderWrapper : public PassManagerBuilder {
143 PassManagerBuilderWrapper(const CodeGenOptions &CGOpts,
144 const LangOptions &LangOpts)
145 : PassManagerBuilder(), CGOpts(CGOpts), LangOpts(LangOpts) {}
146 const CodeGenOptions &getCGOpts() const { return CGOpts; }
147 const LangOptions &getLangOpts() const { return LangOpts; }
149 const CodeGenOptions &CGOpts;
150 const LangOptions &LangOpts;
155 static void addObjCARCAPElimPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
156 if (Builder.OptLevel > 0)
157 PM.add(createObjCARCAPElimPass());
160 static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
161 if (Builder.OptLevel > 0)
162 PM.add(createObjCARCExpandPass());
165 static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
166 if (Builder.OptLevel > 0)
167 PM.add(createObjCARCOptPass());
170 static void addAddDiscriminatorsPass(const PassManagerBuilder &Builder,
171 legacy::PassManagerBase &PM) {
172 PM.add(createAddDiscriminatorsPass());
175 static void addBoundsCheckingPass(const PassManagerBuilder &Builder,
176 legacy::PassManagerBase &PM) {
177 PM.add(createBoundsCheckingPass());
180 static void addSanitizerCoveragePass(const PassManagerBuilder &Builder,
181 legacy::PassManagerBase &PM) {
182 const PassManagerBuilderWrapper &BuilderWrapper =
183 static_cast<const PassManagerBuilderWrapper&>(Builder);
184 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
185 SanitizerCoverageOptions Opts;
187 static_cast<SanitizerCoverageOptions::Type>(CGOpts.SanitizeCoverageType);
188 Opts.IndirectCalls = CGOpts.SanitizeCoverageIndirectCalls;
189 Opts.TraceBB = CGOpts.SanitizeCoverageTraceBB;
190 Opts.TraceCmp = CGOpts.SanitizeCoverageTraceCmp;
191 Opts.Use8bitCounters = CGOpts.SanitizeCoverage8bitCounters;
192 PM.add(createSanitizerCoverageModulePass(Opts));
195 static void addAddressSanitizerPasses(const PassManagerBuilder &Builder,
196 legacy::PassManagerBase &PM) {
197 const PassManagerBuilderWrapper &BuilderWrapper =
198 static_cast<const PassManagerBuilderWrapper&>(Builder);
199 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
200 bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Address);
201 PM.add(createAddressSanitizerFunctionPass(/*CompileKernel*/false, Recover));
202 PM.add(createAddressSanitizerModulePass(/*CompileKernel*/false, Recover));
205 static void addKernelAddressSanitizerPasses(const PassManagerBuilder &Builder,
206 legacy::PassManagerBase &PM) {
207 PM.add(createAddressSanitizerFunctionPass(/*CompileKernel*/true,
209 PM.add(createAddressSanitizerModulePass(/*CompileKernel*/true,
213 static void addMemorySanitizerPass(const PassManagerBuilder &Builder,
214 legacy::PassManagerBase &PM) {
215 const PassManagerBuilderWrapper &BuilderWrapper =
216 static_cast<const PassManagerBuilderWrapper&>(Builder);
217 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
218 PM.add(createMemorySanitizerPass(CGOpts.SanitizeMemoryTrackOrigins));
220 // MemorySanitizer inserts complex instrumentation that mostly follows
221 // the logic of the original code, but operates on "shadow" values.
222 // It can benefit from re-running some general purpose optimization passes.
223 if (Builder.OptLevel > 0) {
224 PM.add(createEarlyCSEPass());
225 PM.add(createReassociatePass());
226 PM.add(createLICMPass());
227 PM.add(createGVNPass());
228 PM.add(createInstructionCombiningPass());
229 PM.add(createDeadStoreEliminationPass());
233 static void addThreadSanitizerPass(const PassManagerBuilder &Builder,
234 legacy::PassManagerBase &PM) {
235 PM.add(createThreadSanitizerPass());
238 static void addDataFlowSanitizerPass(const PassManagerBuilder &Builder,
239 legacy::PassManagerBase &PM) {
240 const PassManagerBuilderWrapper &BuilderWrapper =
241 static_cast<const PassManagerBuilderWrapper&>(Builder);
242 const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
243 PM.add(createDataFlowSanitizerPass(LangOpts.SanitizerBlacklistFiles));
246 static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple,
247 const CodeGenOptions &CodeGenOpts) {
248 TargetLibraryInfoImpl *TLII = new TargetLibraryInfoImpl(TargetTriple);
249 if (!CodeGenOpts.SimplifyLibCalls)
250 TLII->disableAllFunctions();
252 // Disable individual libc/libm calls in TargetLibraryInfo.
254 for (auto &FuncName : CodeGenOpts.getNoBuiltinFuncs())
255 if (TLII->getLibFunc(FuncName, F))
256 TLII->setUnavailable(F);
259 switch (CodeGenOpts.getVecLib()) {
260 case CodeGenOptions::Accelerate:
261 TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::Accelerate);
269 static void addSymbolRewriterPass(const CodeGenOptions &Opts,
270 legacy::PassManager *MPM) {
271 llvm::SymbolRewriter::RewriteDescriptorList DL;
273 llvm::SymbolRewriter::RewriteMapParser MapParser;
274 for (const auto &MapFile : Opts.RewriteMapFiles)
275 MapParser.parse(MapFile, &DL);
277 MPM->add(createRewriteSymbolsPass(DL));
280 void EmitAssemblyHelper::CreatePasses(FunctionInfoIndex *FunctionIndex) {
281 if (CodeGenOpts.DisableLLVMPasses)
284 unsigned OptLevel = CodeGenOpts.OptimizationLevel;
285 CodeGenOptions::InliningMethod Inlining = CodeGenOpts.getInlining();
287 // Handle disabling of LLVM optimization, where we want to preserve the
288 // internal module before any optimization.
289 if (CodeGenOpts.DisableLLVMOpts) {
291 Inlining = CodeGenOpts.NoInlining;
294 PassManagerBuilderWrapper PMBuilder(CodeGenOpts, LangOpts);
296 // Figure out TargetLibraryInfo.
297 Triple TargetTriple(TheModule->getTargetTriple());
298 PMBuilder.LibraryInfo = createTLII(TargetTriple, CodeGenOpts);
301 case CodeGenOptions::NoInlining:
303 case CodeGenOptions::NormalInlining: {
305 createFunctionInliningPass(OptLevel, CodeGenOpts.OptimizeSize);
308 case CodeGenOptions::OnlyAlwaysInlining:
309 // Respect always_inline.
311 // Do not insert lifetime intrinsics at -O0.
312 PMBuilder.Inliner = createAlwaysInlinerPass(false);
314 PMBuilder.Inliner = createAlwaysInlinerPass();
318 PMBuilder.OptLevel = OptLevel;
319 PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize;
320 PMBuilder.BBVectorize = CodeGenOpts.VectorizeBB;
321 PMBuilder.SLPVectorize = CodeGenOpts.VectorizeSLP;
322 PMBuilder.LoopVectorize = CodeGenOpts.VectorizeLoop;
324 PMBuilder.DisableUnitAtATime = !CodeGenOpts.UnitAtATime;
325 PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
326 PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions;
327 PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
328 PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
330 legacy::PassManager *MPM = getPerModulePasses();
332 // If we are performing a ThinLTO importing compile, invoke the LTO
333 // pipeline and pass down the in-memory function index.
335 PMBuilder.FunctionIndex = FunctionIndex;
336 PMBuilder.populateLTOPassManager(*MPM);
340 PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
341 addAddDiscriminatorsPass);
343 // In ObjC ARC mode, add the main ARC optimization passes.
344 if (LangOpts.ObjCAutoRefCount) {
345 PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
346 addObjCARCExpandPass);
347 PMBuilder.addExtension(PassManagerBuilder::EP_ModuleOptimizerEarly,
348 addObjCARCAPElimPass);
349 PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
353 if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) {
354 PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
355 addBoundsCheckingPass);
356 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
357 addBoundsCheckingPass);
360 if (CodeGenOpts.SanitizeCoverageType ||
361 CodeGenOpts.SanitizeCoverageIndirectCalls ||
362 CodeGenOpts.SanitizeCoverageTraceCmp) {
363 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
364 addSanitizerCoveragePass);
365 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
366 addSanitizerCoveragePass);
369 if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
370 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
371 addAddressSanitizerPasses);
372 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
373 addAddressSanitizerPasses);
376 if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) {
377 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
378 addKernelAddressSanitizerPasses);
379 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
380 addKernelAddressSanitizerPasses);
383 if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
384 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
385 addMemorySanitizerPass);
386 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
387 addMemorySanitizerPass);
390 if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
391 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
392 addThreadSanitizerPass);
393 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
394 addThreadSanitizerPass);
397 if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
398 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
399 addDataFlowSanitizerPass);
400 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
401 addDataFlowSanitizerPass);
404 // Set up the per-function pass manager.
405 legacy::FunctionPassManager *FPM = getPerFunctionPasses();
406 if (CodeGenOpts.VerifyModule)
407 FPM->add(createVerifierPass());
408 PMBuilder.populateFunctionPassManager(*FPM);
410 // Set up the per-module pass manager.
411 if (!CodeGenOpts.RewriteMapFiles.empty())
412 addSymbolRewriterPass(CodeGenOpts, MPM);
414 if (!CodeGenOpts.DisableGCov &&
415 (CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes)) {
416 // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if
417 // LLVM's -default-gcov-version flag is set to something invalid.
419 Options.EmitNotes = CodeGenOpts.EmitGcovNotes;
420 Options.EmitData = CodeGenOpts.EmitGcovArcs;
421 memcpy(Options.Version, CodeGenOpts.CoverageVersion, 4);
422 Options.UseCfgChecksum = CodeGenOpts.CoverageExtraChecksum;
423 Options.NoRedZone = CodeGenOpts.DisableRedZone;
424 Options.FunctionNamesInData =
425 !CodeGenOpts.CoverageNoFunctionNamesInData;
426 Options.ExitBlockBeforeBody = CodeGenOpts.CoverageExitBlockBeforeBody;
427 MPM->add(createGCOVProfilerPass(Options));
428 if (CodeGenOpts.getDebugInfo() == CodeGenOptions::NoDebugInfo)
429 MPM->add(createStripSymbolsPass(true));
432 if (CodeGenOpts.ProfileInstrGenerate) {
433 InstrProfOptions Options;
434 Options.NoRedZone = CodeGenOpts.DisableRedZone;
435 Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput;
436 MPM->add(createInstrProfilingPass(Options));
439 if (!CodeGenOpts.SampleProfileFile.empty())
440 MPM->add(createSampleProfileLoaderPass(CodeGenOpts.SampleProfileFile));
442 PMBuilder.populateModulePassManager(*MPM);
445 TargetMachine *EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
446 // Create the TargetMachine for generating code.
448 std::string Triple = TheModule->getTargetTriple();
449 const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
452 Diags.Report(diag::err_fe_unable_to_create_target) << Error;
457 llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel)
458 .Case("small", llvm::CodeModel::Small)
459 .Case("kernel", llvm::CodeModel::Kernel)
460 .Case("medium", llvm::CodeModel::Medium)
461 .Case("large", llvm::CodeModel::Large)
462 .Case("default", llvm::CodeModel::Default)
464 assert(CodeModel != ~0u && "invalid code model!");
465 llvm::CodeModel::Model CM = static_cast<llvm::CodeModel::Model>(CodeModel);
467 SmallVector<const char *, 16> BackendArgs;
468 BackendArgs.push_back("clang"); // Fake program name.
469 if (!CodeGenOpts.DebugPass.empty()) {
470 BackendArgs.push_back("-debug-pass");
471 BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
473 if (!CodeGenOpts.LimitFloatPrecision.empty()) {
474 BackendArgs.push_back("-limit-float-precision");
475 BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
477 for (const std::string &BackendOption : CodeGenOpts.BackendOptions)
478 BackendArgs.push_back(BackendOption.c_str());
479 BackendArgs.push_back(nullptr);
480 llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
483 std::string FeaturesStr =
484 llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ",");
486 // Keep this synced with the equivalent code in tools/driver/cc1as_main.cpp.
487 llvm::Reloc::Model RM = llvm::Reloc::Default;
488 if (CodeGenOpts.RelocationModel == "static") {
489 RM = llvm::Reloc::Static;
490 } else if (CodeGenOpts.RelocationModel == "pic") {
491 RM = llvm::Reloc::PIC_;
493 assert(CodeGenOpts.RelocationModel == "dynamic-no-pic" &&
494 "Invalid PIC model!");
495 RM = llvm::Reloc::DynamicNoPIC;
498 CodeGenOpt::Level OptLevel = CodeGenOpt::Default;
499 switch (CodeGenOpts.OptimizationLevel) {
501 case 0: OptLevel = CodeGenOpt::None; break;
502 case 3: OptLevel = CodeGenOpt::Aggressive; break;
505 llvm::TargetOptions Options;
507 if (!TargetOpts.Reciprocals.empty())
508 Options.Reciprocals = TargetRecip(TargetOpts.Reciprocals);
510 Options.ThreadModel =
511 llvm::StringSwitch<llvm::ThreadModel::Model>(CodeGenOpts.ThreadModel)
512 .Case("posix", llvm::ThreadModel::POSIX)
513 .Case("single", llvm::ThreadModel::Single);
515 // Set float ABI type.
516 assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" ||
517 CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) &&
518 "Invalid Floating Point ABI!");
519 Options.FloatABIType =
520 llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI)
521 .Case("soft", llvm::FloatABI::Soft)
522 .Case("softfp", llvm::FloatABI::Soft)
523 .Case("hard", llvm::FloatABI::Hard)
524 .Default(llvm::FloatABI::Default);
526 // Set FP fusion mode.
527 switch (CodeGenOpts.getFPContractMode()) {
528 case CodeGenOptions::FPC_Off:
529 Options.AllowFPOpFusion = llvm::FPOpFusion::Strict;
531 case CodeGenOptions::FPC_On:
532 Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
534 case CodeGenOptions::FPC_Fast:
535 Options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
539 Options.UseInitArray = CodeGenOpts.UseInitArray;
540 Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS;
541 Options.CompressDebugSections = CodeGenOpts.CompressDebugSections;
544 Options.EABIVersion = llvm::StringSwitch<llvm::EABI>(CodeGenOpts.EABIVersion)
545 .Case("4", llvm::EABI::EABI4)
546 .Case("5", llvm::EABI::EABI5)
547 .Case("gnu", llvm::EABI::GNU)
548 .Default(llvm::EABI::Default);
550 Options.LessPreciseFPMADOption = CodeGenOpts.LessPreciseFPMAD;
551 Options.NoInfsFPMath = CodeGenOpts.NoInfsFPMath;
552 Options.NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath;
553 Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
554 Options.UnsafeFPMath = CodeGenOpts.UnsafeFPMath;
555 Options.StackAlignmentOverride = CodeGenOpts.StackAlignment;
556 Options.PositionIndependentExecutable = LangOpts.PIELevel != 0;
557 Options.FunctionSections = CodeGenOpts.FunctionSections;
558 Options.DataSections = CodeGenOpts.DataSections;
559 Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames;
560 Options.EmulatedTLS = CodeGenOpts.EmulatedTLS;
561 switch (CodeGenOpts.getDebuggerTuning()) {
562 case CodeGenOptions::DebuggerKindGDB:
563 Options.DebuggerTuning = llvm::DebuggerKind::GDB;
565 case CodeGenOptions::DebuggerKindLLDB:
566 Options.DebuggerTuning = llvm::DebuggerKind::LLDB;
568 case CodeGenOptions::DebuggerKindSCE:
569 Options.DebuggerTuning = llvm::DebuggerKind::SCE;
575 Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll;
576 Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels;
577 Options.MCOptions.MCUseDwarfDirectory = !CodeGenOpts.NoDwarfDirectoryAsm;
578 Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack;
579 Options.MCOptions.MCIncrementalLinkerCompatible =
580 CodeGenOpts.IncrementalLinkerCompatible;
581 Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings;
582 Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose;
583 Options.MCOptions.ABIName = TargetOpts.ABI;
585 TargetMachine *TM = TheTarget->createTargetMachine(Triple, TargetOpts.CPU,
586 FeaturesStr, Options,
592 bool EmitAssemblyHelper::AddEmitPasses(BackendAction Action,
593 raw_pwrite_stream &OS) {
595 // Create the code generator passes.
596 legacy::PassManager *PM = getCodeGenPasses();
599 llvm::Triple TargetTriple(TheModule->getTargetTriple());
600 std::unique_ptr<TargetLibraryInfoImpl> TLII(
601 createTLII(TargetTriple, CodeGenOpts));
602 PM->add(new TargetLibraryInfoWrapperPass(*TLII));
604 // Normal mode, emit a .s or .o file by running the code generator. Note,
605 // this also adds codegenerator level optimization passes.
606 TargetMachine::CodeGenFileType CGFT = TargetMachine::CGFT_AssemblyFile;
607 if (Action == Backend_EmitObj)
608 CGFT = TargetMachine::CGFT_ObjectFile;
609 else if (Action == Backend_EmitMCNull)
610 CGFT = TargetMachine::CGFT_Null;
612 assert(Action == Backend_EmitAssembly && "Invalid action!");
614 // Add ObjC ARC final-cleanup optimizations. This is done as part of the
615 // "codegen" passes so that it isn't run multiple times when there is
616 // inlining happening.
617 if (CodeGenOpts.OptimizationLevel > 0)
618 PM->add(createObjCARCContractPass());
620 if (TM->addPassesToEmitFile(*PM, OS, CGFT,
621 /*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
622 Diags.Report(diag::err_fe_unable_to_interface_with_target);
629 void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
630 raw_pwrite_stream *OS) {
631 TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr);
633 bool UsesCodeGen = (Action != Backend_EmitNothing &&
634 Action != Backend_EmitBC &&
635 Action != Backend_EmitLL);
637 TM.reset(CreateTargetMachine(UsesCodeGen));
639 if (UsesCodeGen && !TM)
642 TheModule->setDataLayout(TM->createDataLayout());
644 // If we are performing a ThinLTO importing compile, load the function
645 // index into memory and pass it into CreatePasses, which will add it
646 // to the PassManagerBuilder and invoke LTO passes.
647 std::unique_ptr<FunctionInfoIndex> FunctionIndex;
648 if (!CodeGenOpts.ThinLTOIndexFile.empty()) {
649 ErrorOr<std::unique_ptr<FunctionInfoIndex>> IndexOrErr =
650 llvm::getFunctionIndexForFile(CodeGenOpts.ThinLTOIndexFile,
651 [&](const DiagnosticInfo &DI) {
652 TheModule->getContext().diagnose(DI);
654 if (std::error_code EC = IndexOrErr.getError()) {
655 std::string Error = EC.message();
656 errs() << "Error loading index file '" << CodeGenOpts.ThinLTOIndexFile
657 << "': " << Error << "\n";
660 FunctionIndex = std::move(IndexOrErr.get());
661 assert(FunctionIndex && "Expected non-empty function index");
664 CreatePasses(FunctionIndex.get());
667 case Backend_EmitNothing:
671 getPerModulePasses()->add(createBitcodeWriterPass(
672 *OS, CodeGenOpts.EmitLLVMUseLists, CodeGenOpts.EmitFunctionSummary));
676 getPerModulePasses()->add(
677 createPrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
681 if (!AddEmitPasses(Action, *OS))
685 // Before executing passes, print the final values of the LLVM options.
686 cl::PrintOptionValues();
688 // Run passes. For now we do all passes at once, but eventually we
689 // would like to have the option of streaming code generation.
691 if (PerFunctionPasses) {
692 PrettyStackTraceString CrashInfo("Per-function optimization");
694 PerFunctionPasses->doInitialization();
695 for (Function &F : *TheModule)
696 if (!F.isDeclaration())
697 PerFunctionPasses->run(F);
698 PerFunctionPasses->doFinalization();
701 if (PerModulePasses) {
702 PrettyStackTraceString CrashInfo("Per-module optimization passes");
703 PerModulePasses->run(*TheModule);
707 PrettyStackTraceString CrashInfo("Code generation");
708 CodeGenPasses->run(*TheModule);
712 void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
713 const CodeGenOptions &CGOpts,
714 const clang::TargetOptions &TOpts,
715 const LangOptions &LOpts, StringRef TDesc,
716 Module *M, BackendAction Action,
717 raw_pwrite_stream *OS) {
718 EmitAssemblyHelper AsmHelper(Diags, CGOpts, TOpts, LOpts, M);
720 AsmHelper.EmitAssembly(Action, OS);
722 // If an optional clang TargetInfo description string was passed in, use it to
723 // verify the LLVM TargetMachine's DataLayout.
724 if (AsmHelper.TM && !TDesc.empty()) {
725 std::string DLDesc = M->getDataLayout().getStringRepresentation();
726 if (DLDesc != TDesc) {
727 unsigned DiagID = Diags.getCustomDiagID(
728 DiagnosticsEngine::Error, "backend data layout '%0' does not match "
729 "expected target description '%1'");
730 Diags.Report(DiagID) << DLDesc << TDesc;