1//===--- BackendUtil.cpp - LLVM Backend Utilities -------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/CodeGen/BackendUtil.h"
11#include "clang/Basic/Diagnostic.h"
12#include "clang/Basic/LangOptions.h"
13#include "clang/Basic/TargetOptions.h"
14#include "clang/Frontend/CodeGenOptions.h"
15#include "clang/Frontend/FrontendDiagnostic.h"
16#include "clang/Frontend/Utils.h"
17#include "llvm/ADT/StringExtras.h"
18#include "llvm/ADT/StringSwitch.h"
19#include "llvm/ADT/Triple.h"
20#include "llvm/Analysis/TargetLibraryInfo.h"
21#include "llvm/Analysis/TargetTransformInfo.h"
22#include "llvm/Bitcode/BitcodeWriterPass.h"
23#include "llvm/Bitcode/ReaderWriter.h"
24#include "llvm/CodeGen/RegAllocRegistry.h"
25#include "llvm/CodeGen/SchedulerRegistry.h"
26#include "llvm/IR/DataLayout.h"
27#include "llvm/IR/ModuleSummaryIndex.h"
28#include "llvm/IR/IRPrintingPasses.h"
29#include "llvm/IR/LegacyPassManager.h"
30#include "llvm/IR/Module.h"
31#include "llvm/IR/Verifier.h"
32#include "llvm/MC/SubtargetFeature.h"
33#include "llvm/Object/ModuleSummaryIndexObjectFile.h"
34#include "llvm/Support/CommandLine.h"
35#include "llvm/Support/PrettyStackTrace.h"
36#include "llvm/Support/TargetRegistry.h"
37#include "llvm/Support/Timer.h"
38#include "llvm/Support/raw_ostream.h"
39#include "llvm/Target/TargetMachine.h"
40#include "llvm/Target/TargetOptions.h"
41#include "llvm/Target/TargetSubtargetInfo.h"
42#include "llvm/Transforms/IPO.h"
43#include "llvm/Transforms/IPO/PassManagerBuilder.h"
44#include "llvm/Transforms/Instrumentation.h"
45#include "llvm/Transforms/ObjCARC.h"
46#include "llvm/Transforms/Scalar.h"
47#include "llvm/Transforms/Scalar/GVN.h"
48#include "llvm/Transforms/Utils/SymbolRewriter.h"
49#include <memory>
50using namespace clang;
51using namespace llvm;
52
53namespace {
54
55class EmitAssemblyHelper {
56  DiagnosticsEngine &Diags;
57  const CodeGenOptions &CodeGenOpts;
58  const clang::TargetOptions &TargetOpts;
59  const LangOptions &LangOpts;
60  Module *TheModule;
61
62  Timer CodeGenerationTime;
63
64  mutable legacy::PassManager *CodeGenPasses;
65  mutable legacy::PassManager *PerModulePasses;
66  mutable legacy::FunctionPassManager *PerFunctionPasses;
67
68private:
69  TargetIRAnalysis getTargetIRAnalysis() const {
70    if (TM)
71      return TM->getTargetIRAnalysis();
72
73    return TargetIRAnalysis();
74  }
75
76  legacy::PassManager *getCodeGenPasses() const {
77    if (!CodeGenPasses) {
78      CodeGenPasses = new legacy::PassManager();
79      CodeGenPasses->add(
80          createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
81    }
82    return CodeGenPasses;
83  }
84
85  legacy::PassManager *getPerModulePasses() const {
86    if (!PerModulePasses) {
87      PerModulePasses = new legacy::PassManager();
88      PerModulePasses->add(
89          createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
90    }
91    return PerModulePasses;
92  }
93
94  legacy::FunctionPassManager *getPerFunctionPasses() const {
95    if (!PerFunctionPasses) {
96      PerFunctionPasses = new legacy::FunctionPassManager(TheModule);
97      PerFunctionPasses->add(
98          createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
99    }
100    return PerFunctionPasses;
101  }
102
103  /// Set LLVM command line options passed through -backend-option.
104  void setCommandLineOpts();
105
106  void CreatePasses(ModuleSummaryIndex *ModuleSummary);
107
108  /// Generates the TargetMachine.
109  /// Returns Null if it is unable to create the target machine.
110  /// Some of our clang tests specify triples which are not built
111  /// into clang. This is okay because these tests check the generated
112  /// IR, and they require DataLayout which depends on the triple.
113  /// In this case, we allow this method to fail and not report an error.
114  /// When MustCreateTM is used, we print an error if we are unable to load
115  /// the requested target.
116  TargetMachine *CreateTargetMachine(bool MustCreateTM);
117
118  /// Add passes necessary to emit assembly or LLVM IR.
119  ///
120  /// \return True on success.
121  bool AddEmitPasses(BackendAction Action, raw_pwrite_stream &OS);
122
123public:
124  EmitAssemblyHelper(DiagnosticsEngine &_Diags, const CodeGenOptions &CGOpts,
125                     const clang::TargetOptions &TOpts,
126                     const LangOptions &LOpts, Module *M)
127      : Diags(_Diags), CodeGenOpts(CGOpts), TargetOpts(TOpts), LangOpts(LOpts),
128        TheModule(M), CodeGenerationTime("Code Generation Time"),
129        CodeGenPasses(nullptr), PerModulePasses(nullptr),
130        PerFunctionPasses(nullptr) {}
131
132  ~EmitAssemblyHelper() {
133    delete CodeGenPasses;
134    delete PerModulePasses;
135    delete PerFunctionPasses;
136    if (CodeGenOpts.DisableFree)
137      BuryPointer(std::move(TM));
138  }
139
140  std::unique_ptr<TargetMachine> TM;
141
142  void EmitAssembly(BackendAction Action, raw_pwrite_stream *OS);
143};
144
145// We need this wrapper to access LangOpts and CGOpts from extension functions
146// that we add to the PassManagerBuilder.
147class PassManagerBuilderWrapper : public PassManagerBuilder {
148public:
149  PassManagerBuilderWrapper(const CodeGenOptions &CGOpts,
150                            const LangOptions &LangOpts)
151      : PassManagerBuilder(), CGOpts(CGOpts), LangOpts(LangOpts) {}
152  const CodeGenOptions &getCGOpts() const { return CGOpts; }
153  const LangOptions &getLangOpts() const { return LangOpts; }
154private:
155  const CodeGenOptions &CGOpts;
156  const LangOptions &LangOpts;
157};
158
159}
160
161static void addObjCARCAPElimPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
162  if (Builder.OptLevel > 0)
163    PM.add(createObjCARCAPElimPass());
164}
165
166static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
167  if (Builder.OptLevel > 0)
168    PM.add(createObjCARCExpandPass());
169}
170
171static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
172  if (Builder.OptLevel > 0)
173    PM.add(createObjCARCOptPass());
174}
175
176static void addAddDiscriminatorsPass(const PassManagerBuilder &Builder,
177                                     legacy::PassManagerBase &PM) {
178  PM.add(createAddDiscriminatorsPass());
179}
180
181static void addCleanupPassesForSampleProfiler(
182    const PassManagerBuilder &Builder, legacy::PassManagerBase &PM) {
183  // instcombine is needed before sample profile annotation because it converts
184  // certain function calls to be inlinable. simplifycfg and sroa are needed
185  // before instcombine for necessary preparation. E.g. load store is eliminated
186  // properly so that instcombine will not introduce unecessary liverange.
187  PM.add(createCFGSimplificationPass());
188  PM.add(createSROAPass());
189  PM.add(createInstructionCombiningPass());
190}
191
192static void addBoundsCheckingPass(const PassManagerBuilder &Builder,
193                                  legacy::PassManagerBase &PM) {
194  PM.add(createBoundsCheckingPass());
195}
196
197static void addSanitizerCoveragePass(const PassManagerBuilder &Builder,
198                                     legacy::PassManagerBase &PM) {
199  const PassManagerBuilderWrapper &BuilderWrapper =
200      static_cast<const PassManagerBuilderWrapper&>(Builder);
201  const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
202  SanitizerCoverageOptions Opts;
203  Opts.CoverageType =
204      static_cast<SanitizerCoverageOptions::Type>(CGOpts.SanitizeCoverageType);
205  Opts.IndirectCalls = CGOpts.SanitizeCoverageIndirectCalls;
206  Opts.TraceBB = CGOpts.SanitizeCoverageTraceBB;
207  Opts.TraceCmp = CGOpts.SanitizeCoverageTraceCmp;
208  Opts.Use8bitCounters = CGOpts.SanitizeCoverage8bitCounters;
209  Opts.TracePC = CGOpts.SanitizeCoverageTracePC;
210  PM.add(createSanitizerCoverageModulePass(Opts));
211}
212
213static void addAddressSanitizerPasses(const PassManagerBuilder &Builder,
214                                      legacy::PassManagerBase &PM) {
215  const PassManagerBuilderWrapper &BuilderWrapper =
216      static_cast<const PassManagerBuilderWrapper&>(Builder);
217  const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
218  bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Address);
219  bool UseAfterScope = CGOpts.SanitizeAddressUseAfterScope;
220  PM.add(createAddressSanitizerFunctionPass(/*CompileKernel*/ false, Recover,
221                                            UseAfterScope));
222  PM.add(createAddressSanitizerModulePass(/*CompileKernel*/false, Recover));
223}
224
225static void addKernelAddressSanitizerPasses(const PassManagerBuilder &Builder,
226                                            legacy::PassManagerBase &PM) {
227  PM.add(createAddressSanitizerFunctionPass(
228      /*CompileKernel*/ true,
229      /*Recover*/ true, /*UseAfterScope*/ false));
230  PM.add(createAddressSanitizerModulePass(/*CompileKernel*/true,
231                                          /*Recover*/true));
232}
233
234static void addMemorySanitizerPass(const PassManagerBuilder &Builder,
235                                   legacy::PassManagerBase &PM) {
236  const PassManagerBuilderWrapper &BuilderWrapper =
237      static_cast<const PassManagerBuilderWrapper&>(Builder);
238  const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
239  PM.add(createMemorySanitizerPass(CGOpts.SanitizeMemoryTrackOrigins));
240
241  // MemorySanitizer inserts complex instrumentation that mostly follows
242  // the logic of the original code, but operates on "shadow" values.
243  // It can benefit from re-running some general purpose optimization passes.
244  if (Builder.OptLevel > 0) {
245    PM.add(createEarlyCSEPass());
246    PM.add(createReassociatePass());
247    PM.add(createLICMPass());
248    PM.add(createGVNPass());
249    PM.add(createInstructionCombiningPass());
250    PM.add(createDeadStoreEliminationPass());
251  }
252}
253
254static void addThreadSanitizerPass(const PassManagerBuilder &Builder,
255                                   legacy::PassManagerBase &PM) {
256  PM.add(createThreadSanitizerPass());
257}
258
259static void addDataFlowSanitizerPass(const PassManagerBuilder &Builder,
260                                     legacy::PassManagerBase &PM) {
261  const PassManagerBuilderWrapper &BuilderWrapper =
262      static_cast<const PassManagerBuilderWrapper&>(Builder);
263  const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
264  PM.add(createDataFlowSanitizerPass(LangOpts.SanitizerBlacklistFiles));
265}
266
267static void addEfficiencySanitizerPass(const PassManagerBuilder &Builder,
268                                       legacy::PassManagerBase &PM) {
269  const PassManagerBuilderWrapper &BuilderWrapper =
270      static_cast<const PassManagerBuilderWrapper&>(Builder);
271  const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
272  EfficiencySanitizerOptions Opts;
273  if (LangOpts.Sanitize.has(SanitizerKind::EfficiencyCacheFrag))
274    Opts.ToolType = EfficiencySanitizerOptions::ESAN_CacheFrag;
275  else if (LangOpts.Sanitize.has(SanitizerKind::EfficiencyWorkingSet))
276    Opts.ToolType = EfficiencySanitizerOptions::ESAN_WorkingSet;
277  PM.add(createEfficiencySanitizerPass(Opts));
278}
279
280static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple,
281                                         const CodeGenOptions &CodeGenOpts) {
282  TargetLibraryInfoImpl *TLII = new TargetLibraryInfoImpl(TargetTriple);
283  if (!CodeGenOpts.SimplifyLibCalls)
284    TLII->disableAllFunctions();
285  else {
286    // Disable individual libc/libm calls in TargetLibraryInfo.
287    LibFunc::Func F;
288    for (auto &FuncName : CodeGenOpts.getNoBuiltinFuncs())
289      if (TLII->getLibFunc(FuncName, F))
290        TLII->setUnavailable(F);
291  }
292
293  switch (CodeGenOpts.getVecLib()) {
294  case CodeGenOptions::Accelerate:
295    TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::Accelerate);
296    break;
297  default:
298    break;
299  }
300  return TLII;
301}
302
303static void addSymbolRewriterPass(const CodeGenOptions &Opts,
304                                  legacy::PassManager *MPM) {
305  llvm::SymbolRewriter::RewriteDescriptorList DL;
306
307  llvm::SymbolRewriter::RewriteMapParser MapParser;
308  for (const auto &MapFile : Opts.RewriteMapFiles)
309    MapParser.parse(MapFile, &DL);
310
311  MPM->add(createRewriteSymbolsPass(DL));
312}
313
314void EmitAssemblyHelper::CreatePasses(ModuleSummaryIndex *ModuleSummary) {
315  if (CodeGenOpts.DisableLLVMPasses)
316    return;
317
318  unsigned OptLevel = CodeGenOpts.OptimizationLevel;
319  CodeGenOptions::InliningMethod Inlining = CodeGenOpts.getInlining();
320
321  // Handle disabling of LLVM optimization, where we want to preserve the
322  // internal module before any optimization.
323  if (CodeGenOpts.DisableLLVMOpts) {
324    OptLevel = 0;
325    Inlining = CodeGenOpts.NoInlining;
326  }
327
328  PassManagerBuilderWrapper PMBuilder(CodeGenOpts, LangOpts);
329
330  // Figure out TargetLibraryInfo.
331  Triple TargetTriple(TheModule->getTargetTriple());
332  PMBuilder.LibraryInfo = createTLII(TargetTriple, CodeGenOpts);
333
334  switch (Inlining) {
335  case CodeGenOptions::NoInlining:
336    break;
337  case CodeGenOptions::NormalInlining:
338  case CodeGenOptions::OnlyHintInlining: {
339    PMBuilder.Inliner =
340        createFunctionInliningPass(OptLevel, CodeGenOpts.OptimizeSize);
341    break;
342  }
343  case CodeGenOptions::OnlyAlwaysInlining:
344    // Respect always_inline.
345    if (OptLevel == 0)
346      // Do not insert lifetime intrinsics at -O0.
347      PMBuilder.Inliner = createAlwaysInlinerPass(false);
348    else
349      PMBuilder.Inliner = createAlwaysInlinerPass();
350    break;
351  }
352
353  PMBuilder.OptLevel = OptLevel;
354  PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize;
355  PMBuilder.BBVectorize = CodeGenOpts.VectorizeBB;
356  PMBuilder.SLPVectorize = CodeGenOpts.VectorizeSLP;
357  PMBuilder.LoopVectorize = CodeGenOpts.VectorizeLoop;
358
359  PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
360  PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions;
361  PMBuilder.PrepareForThinLTO = CodeGenOpts.EmitSummaryIndex;
362  PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
363  PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
364
365  legacy::PassManager *MPM = getPerModulePasses();
366
367  // If we are performing a ThinLTO importing compile, invoke the LTO
368  // pipeline and pass down the in-memory module summary index.
369  if (ModuleSummary) {
370    PMBuilder.ModuleSummary = ModuleSummary;
371    PMBuilder.populateThinLTOPassManager(*MPM);
372    return;
373  }
374
375  // Add target-specific passes that need to run as early as possible.
376  if (TM)
377    PMBuilder.addExtension(
378        PassManagerBuilder::EP_EarlyAsPossible,
379        [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
380          TM->addEarlyAsPossiblePasses(PM);
381        });
382
383  PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
384                         addAddDiscriminatorsPass);
385
386  // In ObjC ARC mode, add the main ARC optimization passes.
387  if (LangOpts.ObjCAutoRefCount) {
388    PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
389                           addObjCARCExpandPass);
390    PMBuilder.addExtension(PassManagerBuilder::EP_ModuleOptimizerEarly,
391                           addObjCARCAPElimPass);
392    PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
393                           addObjCARCOptPass);
394  }
395
396  if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) {
397    PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
398                           addBoundsCheckingPass);
399    PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
400                           addBoundsCheckingPass);
401  }
402
403  if (CodeGenOpts.SanitizeCoverageType ||
404      CodeGenOpts.SanitizeCoverageIndirectCalls ||
405      CodeGenOpts.SanitizeCoverageTraceCmp) {
406    PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
407                           addSanitizerCoveragePass);
408    PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
409                           addSanitizerCoveragePass);
410  }
411
412  if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
413    PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
414                           addAddressSanitizerPasses);
415    PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
416                           addAddressSanitizerPasses);
417  }
418
419  if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) {
420    PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
421                           addKernelAddressSanitizerPasses);
422    PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
423                           addKernelAddressSanitizerPasses);
424  }
425
426  if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
427    PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
428                           addMemorySanitizerPass);
429    PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
430                           addMemorySanitizerPass);
431  }
432
433  if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
434    PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
435                           addThreadSanitizerPass);
436    PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
437                           addThreadSanitizerPass);
438  }
439
440  if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
441    PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
442                           addDataFlowSanitizerPass);
443    PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
444                           addDataFlowSanitizerPass);
445  }
446
447  if (LangOpts.Sanitize.hasOneOf(SanitizerKind::Efficiency)) {
448    PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
449                           addEfficiencySanitizerPass);
450    PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
451                           addEfficiencySanitizerPass);
452  }
453
454  // Set up the per-function pass manager.
455  legacy::FunctionPassManager *FPM = getPerFunctionPasses();
456  if (CodeGenOpts.VerifyModule)
457    FPM->add(createVerifierPass());
458
459  // Set up the per-module pass manager.
460  if (!CodeGenOpts.RewriteMapFiles.empty())
461    addSymbolRewriterPass(CodeGenOpts, MPM);
462
463  if (!CodeGenOpts.DisableGCov &&
464      (CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes)) {
465    // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if
466    // LLVM's -default-gcov-version flag is set to something invalid.
467    GCOVOptions Options;
468    Options.EmitNotes = CodeGenOpts.EmitGcovNotes;
469    Options.EmitData = CodeGenOpts.EmitGcovArcs;
470    memcpy(Options.Version, CodeGenOpts.CoverageVersion, 4);
471    Options.UseCfgChecksum = CodeGenOpts.CoverageExtraChecksum;
472    Options.NoRedZone = CodeGenOpts.DisableRedZone;
473    Options.FunctionNamesInData =
474        !CodeGenOpts.CoverageNoFunctionNamesInData;
475    Options.ExitBlockBeforeBody = CodeGenOpts.CoverageExitBlockBeforeBody;
476    MPM->add(createGCOVProfilerPass(Options));
477    if (CodeGenOpts.getDebugInfo() == codegenoptions::NoDebugInfo)
478      MPM->add(createStripSymbolsPass(true));
479  }
480
481  if (CodeGenOpts.hasProfileClangInstr()) {
482    InstrProfOptions Options;
483    Options.NoRedZone = CodeGenOpts.DisableRedZone;
484    Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput;
485    MPM->add(createInstrProfilingLegacyPass(Options));
486  }
487  if (CodeGenOpts.hasProfileIRInstr()) {
488    if (!CodeGenOpts.InstrProfileOutput.empty())
489      PMBuilder.PGOInstrGen = CodeGenOpts.InstrProfileOutput;
490    else
491      PMBuilder.PGOInstrGen = "default.profraw";
492  }
493  if (CodeGenOpts.hasProfileIRUse())
494    PMBuilder.PGOInstrUse = CodeGenOpts.ProfileInstrumentUsePath;
495
496  if (!CodeGenOpts.SampleProfileFile.empty()) {
497    MPM->add(createPruneEHPass());
498    MPM->add(createSampleProfileLoaderPass(CodeGenOpts.SampleProfileFile));
499    PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
500                           addCleanupPassesForSampleProfiler);
501  }
502
503  PMBuilder.populateFunctionPassManager(*FPM);
504  PMBuilder.populateModulePassManager(*MPM);
505}
506
507void EmitAssemblyHelper::setCommandLineOpts() {
508  SmallVector<const char *, 16> BackendArgs;
509  BackendArgs.push_back("clang"); // Fake program name.
510  if (!CodeGenOpts.DebugPass.empty()) {
511    BackendArgs.push_back("-debug-pass");
512    BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
513  }
514  if (!CodeGenOpts.LimitFloatPrecision.empty()) {
515    BackendArgs.push_back("-limit-float-precision");
516    BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
517  }
518  for (const std::string &BackendOption : CodeGenOpts.BackendOptions)
519    BackendArgs.push_back(BackendOption.c_str());
520  BackendArgs.push_back(nullptr);
521  llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
522                                    BackendArgs.data());
523}
524
525TargetMachine *EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
526  // Create the TargetMachine for generating code.
527  std::string Error;
528  std::string Triple = TheModule->getTargetTriple();
529  const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
530  if (!TheTarget) {
531    if (MustCreateTM)
532      Diags.Report(diag::err_fe_unable_to_create_target) << Error;
533    return nullptr;
534  }
535
536  unsigned CodeModel =
537    llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel)
538      .Case("small", llvm::CodeModel::Small)
539      .Case("kernel", llvm::CodeModel::Kernel)
540      .Case("medium", llvm::CodeModel::Medium)
541      .Case("large", llvm::CodeModel::Large)
542      .Case("default", llvm::CodeModel::Default)
543      .Default(~0u);
544  assert(CodeModel != ~0u && "invalid code model!");
545  llvm::CodeModel::Model CM = static_cast<llvm::CodeModel::Model>(CodeModel);
546
547  std::string FeaturesStr =
548      llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ",");
549
550  // Keep this synced with the equivalent code in tools/driver/cc1as_main.cpp.
551  llvm::Optional<llvm::Reloc::Model> RM;
552  if (CodeGenOpts.RelocationModel == "static") {
553    RM = llvm::Reloc::Static;
554  } else if (CodeGenOpts.RelocationModel == "pic") {
555    RM = llvm::Reloc::PIC_;
556  } else {
557    assert(CodeGenOpts.RelocationModel == "dynamic-no-pic" &&
558           "Invalid PIC model!");
559    RM = llvm::Reloc::DynamicNoPIC;
560  }
561
562  CodeGenOpt::Level OptLevel = CodeGenOpt::Default;
563  switch (CodeGenOpts.OptimizationLevel) {
564  default: break;
565  case 0: OptLevel = CodeGenOpt::None; break;
566  case 3: OptLevel = CodeGenOpt::Aggressive; break;
567  }
568
569  llvm::TargetOptions Options;
570
571  if (!TargetOpts.Reciprocals.empty())
572    Options.Reciprocals = TargetRecip(TargetOpts.Reciprocals);
573
574  Options.ThreadModel =
575    llvm::StringSwitch<llvm::ThreadModel::Model>(CodeGenOpts.ThreadModel)
576      .Case("posix", llvm::ThreadModel::POSIX)
577      .Case("single", llvm::ThreadModel::Single);
578
579  // Set float ABI type.
580  assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" ||
581          CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) &&
582         "Invalid Floating Point ABI!");
583  Options.FloatABIType =
584      llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI)
585          .Case("soft", llvm::FloatABI::Soft)
586          .Case("softfp", llvm::FloatABI::Soft)
587          .Case("hard", llvm::FloatABI::Hard)
588          .Default(llvm::FloatABI::Default);
589
590  // Set FP fusion mode.
591  switch (CodeGenOpts.getFPContractMode()) {
592  case CodeGenOptions::FPC_Off:
593    Options.AllowFPOpFusion = llvm::FPOpFusion::Strict;
594    break;
595  case CodeGenOptions::FPC_On:
596    Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
597    break;
598  case CodeGenOptions::FPC_Fast:
599    Options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
600    break;
601  }
602
603  Options.UseInitArray = CodeGenOpts.UseInitArray;
604  Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS;
605  Options.CompressDebugSections = CodeGenOpts.CompressDebugSections;
606  Options.RelaxELFRelocations = CodeGenOpts.RelaxELFRelocations;
607
608  // Set EABI version.
609  Options.EABIVersion = llvm::StringSwitch<llvm::EABI>(TargetOpts.EABIVersion)
610                            .Case("4", llvm::EABI::EABI4)
611                            .Case("5", llvm::EABI::EABI5)
612                            .Case("gnu", llvm::EABI::GNU)
613                            .Default(llvm::EABI::Default);
614
615  if (LangOpts.SjLjExceptions)
616    Options.ExceptionModel = llvm::ExceptionHandling::SjLj;
617
618  Options.LessPreciseFPMADOption = CodeGenOpts.LessPreciseFPMAD;
619  Options.NoInfsFPMath = CodeGenOpts.NoInfsFPMath;
620  Options.NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath;
621  Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
622  Options.UnsafeFPMath = CodeGenOpts.UnsafeFPMath;
623  Options.StackAlignmentOverride = CodeGenOpts.StackAlignment;
624  Options.FunctionSections = CodeGenOpts.FunctionSections;
625  Options.DataSections = CodeGenOpts.DataSections;
626  Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames;
627  Options.EmulatedTLS = CodeGenOpts.EmulatedTLS;
628  Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning();
629
630  Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll;
631  Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels;
632  Options.MCOptions.MCUseDwarfDirectory = !CodeGenOpts.NoDwarfDirectoryAsm;
633  Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack;
634  Options.MCOptions.MCIncrementalLinkerCompatible =
635      CodeGenOpts.IncrementalLinkerCompatible;
636  Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings;
637  Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose;
638  Options.MCOptions.ABIName = TargetOpts.ABI;
639
640  TargetMachine *TM = TheTarget->createTargetMachine(Triple, TargetOpts.CPU,
641                                                     FeaturesStr, Options,
642                                                     RM, CM, OptLevel);
643
644  return TM;
645}
646
647bool EmitAssemblyHelper::AddEmitPasses(BackendAction Action,
648                                       raw_pwrite_stream &OS) {
649
650  // Create the code generator passes.
651  legacy::PassManager *PM = getCodeGenPasses();
652
653  // Add LibraryInfo.
654  llvm::Triple TargetTriple(TheModule->getTargetTriple());
655  std::unique_ptr<TargetLibraryInfoImpl> TLII(
656      createTLII(TargetTriple, CodeGenOpts));
657  PM->add(new TargetLibraryInfoWrapperPass(*TLII));
658
659  // Normal mode, emit a .s or .o file by running the code generator. Note,
660  // this also adds codegenerator level optimization passes.
661  TargetMachine::CodeGenFileType CGFT = TargetMachine::CGFT_AssemblyFile;
662  if (Action == Backend_EmitObj)
663    CGFT = TargetMachine::CGFT_ObjectFile;
664  else if (Action == Backend_EmitMCNull)
665    CGFT = TargetMachine::CGFT_Null;
666  else
667    assert(Action == Backend_EmitAssembly && "Invalid action!");
668
669  // Add ObjC ARC final-cleanup optimizations. This is done as part of the
670  // "codegen" passes so that it isn't run multiple times when there is
671  // inlining happening.
672  if (CodeGenOpts.OptimizationLevel > 0)
673    PM->add(createObjCARCContractPass());
674
675  if (TM->addPassesToEmitFile(*PM, OS, CGFT,
676                              /*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
677    Diags.Report(diag::err_fe_unable_to_interface_with_target);
678    return false;
679  }
680
681  return true;
682}
683
684void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
685                                      raw_pwrite_stream *OS) {
686  TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr);
687
688  setCommandLineOpts();
689
690  bool UsesCodeGen = (Action != Backend_EmitNothing &&
691                      Action != Backend_EmitBC &&
692                      Action != Backend_EmitLL);
693  if (!TM)
694    TM.reset(CreateTargetMachine(UsesCodeGen));
695
696  if (UsesCodeGen && !TM)
697    return;
698  if (TM)
699    TheModule->setDataLayout(TM->createDataLayout());
700
701  // If we are performing a ThinLTO importing compile, load the function
702  // index into memory and pass it into CreatePasses, which will add it
703  // to the PassManagerBuilder and invoke LTO passes.
704  std::unique_ptr<ModuleSummaryIndex> ModuleSummary;
705  if (!CodeGenOpts.ThinLTOIndexFile.empty()) {
706    ErrorOr<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr =
707        llvm::getModuleSummaryIndexForFile(
708            CodeGenOpts.ThinLTOIndexFile, [&](const DiagnosticInfo &DI) {
709              TheModule->getContext().diagnose(DI);
710            });
711    if (std::error_code EC = IndexOrErr.getError()) {
712      std::string Error = EC.message();
713      errs() << "Error loading index file '" << CodeGenOpts.ThinLTOIndexFile
714             << "': " << Error << "\n";
715      return;
716    }
717    ModuleSummary = std::move(IndexOrErr.get());
718    assert(ModuleSummary && "Expected non-empty module summary index");
719  }
720
721  CreatePasses(ModuleSummary.get());
722
723  switch (Action) {
724  case Backend_EmitNothing:
725    break;
726
727  case Backend_EmitBC:
728    getPerModulePasses()->add(createBitcodeWriterPass(
729        *OS, CodeGenOpts.EmitLLVMUseLists, CodeGenOpts.EmitSummaryIndex,
730        CodeGenOpts.EmitSummaryIndex));
731    break;
732
733  case Backend_EmitLL:
734    getPerModulePasses()->add(
735        createPrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
736    break;
737
738  default:
739    if (!AddEmitPasses(Action, *OS))
740      return;
741  }
742
743  // Before executing passes, print the final values of the LLVM options.
744  cl::PrintOptionValues();
745
746  // Run passes. For now we do all passes at once, but eventually we
747  // would like to have the option of streaming code generation.
748
749  if (PerFunctionPasses) {
750    PrettyStackTraceString CrashInfo("Per-function optimization");
751
752    PerFunctionPasses->doInitialization();
753    for (Function &F : *TheModule)
754      if (!F.isDeclaration())
755        PerFunctionPasses->run(F);
756    PerFunctionPasses->doFinalization();
757  }
758
759  if (PerModulePasses) {
760    PrettyStackTraceString CrashInfo("Per-module optimization passes");
761    PerModulePasses->run(*TheModule);
762  }
763
764  if (CodeGenPasses) {
765    PrettyStackTraceString CrashInfo("Code generation");
766    CodeGenPasses->run(*TheModule);
767  }
768}
769
770void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
771                              const CodeGenOptions &CGOpts,
772                              const clang::TargetOptions &TOpts,
773                              const LangOptions &LOpts, const llvm::DataLayout &TDesc,
774                              Module *M, BackendAction Action,
775                              raw_pwrite_stream *OS) {
776  EmitAssemblyHelper AsmHelper(Diags, CGOpts, TOpts, LOpts, M);
777
778  AsmHelper.EmitAssembly(Action, OS);
779
780  // Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
781  // DataLayout.
782  if (AsmHelper.TM) {
783    std::string DLDesc = M->getDataLayout().getStringRepresentation();
784    if (DLDesc != TDesc.getStringRepresentation()) {
785      unsigned DiagID = Diags.getCustomDiagID(
786          DiagnosticsEngine::Error, "backend data layout '%0' does not match "
787                                    "expected target description '%1'");
788      Diags.Report(DiagID) << DLDesc << TDesc.getStringRepresentation();
789    }
790  }
791}
792
793static const char* getSectionNameForBitcode(const Triple &T) {
794  switch (T.getObjectFormat()) {
795  case Triple::MachO:
796    return "__LLVM,__bitcode";
797  case Triple::COFF:
798  case Triple::ELF:
799  case Triple::UnknownObjectFormat:
800    return ".llvmbc";
801  }
802  llvm_unreachable("Unimplemented ObjectFormatType");
803}
804
805static const char* getSectionNameForCommandline(const Triple &T) {
806  switch (T.getObjectFormat()) {
807  case Triple::MachO:
808    return "__LLVM,__cmdline";
809  case Triple::COFF:
810  case Triple::ELF:
811  case Triple::UnknownObjectFormat:
812    return ".llvmcmd";
813  }
814  llvm_unreachable("Unimplemented ObjectFormatType");
815}
816
817// With -fembed-bitcode, save a copy of the llvm IR as data in the
818// __LLVM,__bitcode section.
819void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
820                         llvm::MemoryBufferRef Buf) {
821  if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off)
822    return;
823
824  // Save llvm.compiler.used and remote it.
825  SmallVector<Constant*, 2> UsedArray;
826  SmallSet<GlobalValue*, 4> UsedGlobals;
827  Type *UsedElementType = Type::getInt8Ty(M->getContext())->getPointerTo(0);
828  GlobalVariable *Used = collectUsedGlobalVariables(*M, UsedGlobals, true);
829  for (auto *GV : UsedGlobals) {
830    if (GV->getName() != "llvm.embedded.module" &&
831        GV->getName() != "llvm.cmdline")
832      UsedArray.push_back(
833          ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
834  }
835  if (Used)
836    Used->eraseFromParent();
837
838  // Embed the bitcode for the llvm module.
839  std::string Data;
840  ArrayRef<uint8_t> ModuleData;
841  Triple T(M->getTargetTriple());
842  // Create a constant that contains the bitcode.
843  // In case of embedding a marker, ignore the input Buf and use the empty
844  // ArrayRef. It is also legal to create a bitcode marker even Buf is empty.
845  if (CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker) {
846    if (!isBitcode((const unsigned char *)Buf.getBufferStart(),
847                   (const unsigned char *)Buf.getBufferEnd())) {
848      // If the input is LLVM Assembly, bitcode is produced by serializing
849      // the module. Use-lists order need to be perserved in this case.
850      llvm::raw_string_ostream OS(Data);
851      llvm::WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ true);
852      ModuleData =
853          ArrayRef<uint8_t>((const uint8_t *)OS.str().data(), OS.str().size());
854    } else
855      // If the input is LLVM bitcode, write the input byte stream directly.
856      ModuleData = ArrayRef<uint8_t>((const uint8_t *)Buf.getBufferStart(),
857                                     Buf.getBufferSize());
858  }
859  llvm::Constant *ModuleConstant =
860      llvm::ConstantDataArray::get(M->getContext(), ModuleData);
861  llvm::GlobalVariable *GV = new llvm::GlobalVariable(
862      *M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage,
863      ModuleConstant);
864  GV->setSection(getSectionNameForBitcode(T));
865  UsedArray.push_back(
866      ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
867  if (llvm::GlobalVariable *Old =
868          M->getGlobalVariable("llvm.embedded.module", true)) {
869    assert(Old->hasOneUse() &&
870           "llvm.embedded.module can only be used once in llvm.compiler.used");
871    GV->takeName(Old);
872    Old->eraseFromParent();
873  } else {
874    GV->setName("llvm.embedded.module");
875  }
876
877  // Skip if only bitcode needs to be embedded.
878  if (CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode) {
879    // Embed command-line options.
880    ArrayRef<uint8_t> CmdData(const_cast<uint8_t *>(CGOpts.CmdArgs.data()),
881                              CGOpts.CmdArgs.size());
882    llvm::Constant *CmdConstant =
883      llvm::ConstantDataArray::get(M->getContext(), CmdData);
884    GV = new llvm::GlobalVariable(*M, CmdConstant->getType(), true,
885                                  llvm::GlobalValue::PrivateLinkage,
886                                  CmdConstant);
887    GV->setSection(getSectionNameForCommandline(T));
888    UsedArray.push_back(
889        ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
890    if (llvm::GlobalVariable *Old =
891            M->getGlobalVariable("llvm.cmdline", true)) {
892      assert(Old->hasOneUse() &&
893             "llvm.cmdline can only be used once in llvm.compiler.used");
894      GV->takeName(Old);
895      Old->eraseFromParent();
896    } else {
897      GV->setName("llvm.cmdline");
898    }
899  }
900
901  if (UsedArray.empty())
902    return;
903
904  // Recreate llvm.compiler.used.
905  ArrayType *ATy = ArrayType::get(UsedElementType, UsedArray.size());
906  auto *NewUsed = new GlobalVariable(
907      *M, ATy, false, llvm::GlobalValue::AppendingLinkage,
908      llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used");
909  NewUsed->setSection("llvm.metadata");
910}
911