Coverage Report

Created: 2022-01-25 06:29

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/BackendUtil.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- BackendUtil.cpp - LLVM Backend Utilities -------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "clang/CodeGen/BackendUtil.h"
10
#include "clang/Basic/CodeGenOptions.h"
11
#include "clang/Basic/Diagnostic.h"
12
#include "clang/Basic/LangOptions.h"
13
#include "clang/Basic/TargetOptions.h"
14
#include "clang/Frontend/FrontendDiagnostic.h"
15
#include "clang/Frontend/Utils.h"
16
#include "clang/Lex/HeaderSearchOptions.h"
17
#include "llvm/ADT/SmallSet.h"
18
#include "llvm/ADT/StringExtras.h"
19
#include "llvm/ADT/StringSwitch.h"
20
#include "llvm/ADT/Triple.h"
21
#include "llvm/Analysis/AliasAnalysis.h"
22
#include "llvm/Analysis/StackSafetyAnalysis.h"
23
#include "llvm/Analysis/TargetLibraryInfo.h"
24
#include "llvm/Analysis/TargetTransformInfo.h"
25
#include "llvm/Bitcode/BitcodeReader.h"
26
#include "llvm/Bitcode/BitcodeWriter.h"
27
#include "llvm/Bitcode/BitcodeWriterPass.h"
28
#include "llvm/CodeGen/RegAllocRegistry.h"
29
#include "llvm/CodeGen/SchedulerRegistry.h"
30
#include "llvm/CodeGen/TargetSubtargetInfo.h"
31
#include "llvm/IR/DataLayout.h"
32
#include "llvm/IR/IRPrintingPasses.h"
33
#include "llvm/IR/LegacyPassManager.h"
34
#include "llvm/IR/Module.h"
35
#include "llvm/IR/ModuleSummaryIndex.h"
36
#include "llvm/IR/PassManager.h"
37
#include "llvm/IR/Verifier.h"
38
#include "llvm/LTO/LTOBackend.h"
39
#include "llvm/MC/MCAsmInfo.h"
40
#include "llvm/MC/SubtargetFeature.h"
41
#include "llvm/MC/TargetRegistry.h"
42
#include "llvm/Passes/PassBuilder.h"
43
#include "llvm/Passes/PassPlugin.h"
44
#include "llvm/Passes/StandardInstrumentations.h"
45
#include "llvm/Support/BuryPointer.h"
46
#include "llvm/Support/CommandLine.h"
47
#include "llvm/Support/MemoryBuffer.h"
48
#include "llvm/Support/PrettyStackTrace.h"
49
#include "llvm/Support/TimeProfiler.h"
50
#include "llvm/Support/Timer.h"
51
#include "llvm/Support/ToolOutputFile.h"
52
#include "llvm/Support/raw_ostream.h"
53
#include "llvm/Target/TargetMachine.h"
54
#include "llvm/Target/TargetOptions.h"
55
#include "llvm/Transforms/Coroutines.h"
56
#include "llvm/Transforms/Coroutines/CoroCleanup.h"
57
#include "llvm/Transforms/Coroutines/CoroEarly.h"
58
#include "llvm/Transforms/Coroutines/CoroElide.h"
59
#include "llvm/Transforms/Coroutines/CoroSplit.h"
60
#include "llvm/Transforms/IPO.h"
61
#include "llvm/Transforms/IPO/AlwaysInliner.h"
62
#include "llvm/Transforms/IPO/LowerTypeTests.h"
63
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
64
#include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
65
#include "llvm/Transforms/InstCombine/InstCombine.h"
66
#include "llvm/Transforms/Instrumentation.h"
67
#include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
68
#include "llvm/Transforms/Instrumentation/AddressSanitizerOptions.h"
69
#include "llvm/Transforms/Instrumentation/BoundsChecking.h"
70
#include "llvm/Transforms/Instrumentation/DataFlowSanitizer.h"
71
#include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
72
#include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h"
73
#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
74
#include "llvm/Transforms/Instrumentation/MemProfiler.h"
75
#include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
76
#include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
77
#include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
78
#include "llvm/Transforms/ObjCARC.h"
79
#include "llvm/Transforms/Scalar.h"
80
#include "llvm/Transforms/Scalar/EarlyCSE.h"
81
#include "llvm/Transforms/Scalar/GVN.h"
82
#include "llvm/Transforms/Scalar/LowerMatrixIntrinsics.h"
83
#include "llvm/Transforms/Utils.h"
84
#include "llvm/Transforms/Utils/CanonicalizeAliases.h"
85
#include "llvm/Transforms/Utils/Debugify.h"
86
#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
87
#include "llvm/Transforms/Utils/NameAnonGlobals.h"
88
#include "llvm/Transforms/Utils/SymbolRewriter.h"
89
#include <memory>
90
using namespace clang;
91
using namespace llvm;
92
93
#define HANDLE_EXTENSION(Ext)                                                  \
94
  llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
95
#include "llvm/Support/Extension.def"
96
97
namespace llvm {
98
extern cl::opt<bool> DebugInfoCorrelate;
99
}
100
101
namespace {
102
103
// Default filename used for profile generation.
104
8
std::string getDefaultProfileGenName() {
105
8
  return DebugInfoCorrelate ? 
"default_%p.proflite"0
: "default_%m.profraw";
106
8
}
107
108
class EmitAssemblyHelper {
109
  DiagnosticsEngine &Diags;
110
  const HeaderSearchOptions &HSOpts;
111
  const CodeGenOptions &CodeGenOpts;
112
  const clang::TargetOptions &TargetOpts;
113
  const LangOptions &LangOpts;
114
  Module *TheModule;
115
116
  Timer CodeGenerationTime;
117
118
  std::unique_ptr<raw_pwrite_stream> OS;
119
120
4.43k
  TargetIRAnalysis getTargetIRAnalysis() const {
121
4.43k
    if (TM)
122
4.43k
      return TM->getTargetIRAnalysis();
123
124
0
    return TargetIRAnalysis();
125
4.43k
  }
126
127
  void CreatePasses(legacy::PassManager &MPM, legacy::FunctionPassManager &FPM);
128
129
  /// Generates the TargetMachine.
130
  /// Leaves TM unchanged if it is unable to create the target machine.
131
  /// Some of our clang tests specify triples which are not built
132
  /// into clang. This is okay because these tests check the generated
133
  /// IR, and they require DataLayout which depends on the triple.
134
  /// In this case, we allow this method to fail and not report an error.
135
  /// When MustCreateTM is used, we print an error if we are unable to load
136
  /// the requested target.
137
  void CreateTargetMachine(bool MustCreateTM);
138
139
  /// Add passes necessary to emit assembly or LLVM IR.
140
  ///
141
  /// \return True on success.
142
  bool AddEmitPasses(legacy::PassManager &CodeGenPasses, BackendAction Action,
143
                     raw_pwrite_stream &OS, raw_pwrite_stream *DwoOS);
144
145
8
  std::unique_ptr<llvm::ToolOutputFile> openOutputFile(StringRef Path) {
146
8
    std::error_code EC;
147
8
    auto F = std::make_unique<llvm::ToolOutputFile>(Path, EC,
148
8
                                                     llvm::sys::fs::OF_None);
149
8
    if (EC) {
150
0
      Diags.Report(diag::err_fe_unable_to_open_output) << Path << EC.message();
151
0
      F.reset();
152
0
    }
153
8
    return F;
154
8
  }
155
156
  void
157
  RunOptimizationPipeline(BackendAction Action,
158
                          std::unique_ptr<raw_pwrite_stream> &OS,
159
                          std::unique_ptr<llvm::ToolOutputFile> &ThinLinkOS);
160
  void RunCodegenPipeline(BackendAction Action,
161
                          std::unique_ptr<raw_pwrite_stream> &OS,
162
                          std::unique_ptr<llvm::ToolOutputFile> &DwoOS);
163
164
public:
165
  EmitAssemblyHelper(DiagnosticsEngine &_Diags,
166
                     const HeaderSearchOptions &HeaderSearchOpts,
167
                     const CodeGenOptions &CGOpts,
168
                     const clang::TargetOptions &TOpts,
169
                     const LangOptions &LOpts, Module *M)
170
      : Diags(_Diags), HSOpts(HeaderSearchOpts), CodeGenOpts(CGOpts),
171
        TargetOpts(TOpts), LangOpts(LOpts), TheModule(M),
172
19.5k
        CodeGenerationTime("codegen", "Code Generation Time") {}
173
174
19.5k
  ~EmitAssemblyHelper() {
175
19.5k
    if (CodeGenOpts.DisableFree)
176
4.18k
      BuryPointer(std::move(TM));
177
19.5k
  }
178
179
  std::unique_ptr<TargetMachine> TM;
180
181
  // Emit output using the legacy pass manager for the optimization pipeline.
182
  // This will be removed soon when using the legacy pass manager for the
183
  // optimization pipeline is no longer supported.
184
  void EmitAssemblyWithLegacyPassManager(BackendAction Action,
185
                                         std::unique_ptr<raw_pwrite_stream> OS);
186
187
  // Emit output using the new pass manager for the optimization pipeline. This
188
  // is the default.
189
  void EmitAssembly(BackendAction Action,
190
                    std::unique_ptr<raw_pwrite_stream> OS);
191
};
192
193
// We need this wrapper to access LangOpts and CGOpts from extension functions
194
// that we add to the PassManagerBuilder.
195
class PassManagerBuilderWrapper : public PassManagerBuilder {
196
public:
197
  PassManagerBuilderWrapper(const Triple &TargetTriple,
198
                            const CodeGenOptions &CGOpts,
199
                            const LangOptions &LangOpts)
200
59
      : TargetTriple(TargetTriple), CGOpts(CGOpts), LangOpts(LangOpts) {}
201
5
  const Triple &getTargetTriple() const { return TargetTriple; }
202
15
  const CodeGenOptions &getCGOpts() const { return CGOpts; }
203
0
  const LangOptions &getLangOpts() const { return LangOpts; }
204
205
private:
206
  const Triple &TargetTriple;
207
  const CodeGenOptions &CGOpts;
208
  const LangOptions &LangOpts;
209
};
210
}
211
212
0
static void addObjCARCAPElimPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
213
0
  if (Builder.OptLevel > 0)
214
0
    PM.add(createObjCARCAPElimPass());
215
0
}
216
217
0
static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
218
0
  if (Builder.OptLevel > 0)
219
0
    PM.add(createObjCARCExpandPass());
220
0
}
221
222
0
static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
223
0
  if (Builder.OptLevel > 0)
224
0
    PM.add(createObjCARCOptPass());
225
0
}
226
227
static void addAddDiscriminatorsPass(const PassManagerBuilder &Builder,
228
1
                                     legacy::PassManagerBase &PM) {
229
1
  PM.add(createAddDiscriminatorsPass());
230
1
}
231
232
static void addBoundsCheckingPass(const PassManagerBuilder &Builder,
233
1
                                  legacy::PassManagerBase &PM) {
234
1
  PM.add(createBoundsCheckingLegacyPass());
235
1
}
236
237
static SanitizerCoverageOptions
238
22
getSancovOptsFromCGOpts(const CodeGenOptions &CGOpts) {
239
22
  SanitizerCoverageOptions Opts;
240
22
  Opts.CoverageType =
241
22
      static_cast<SanitizerCoverageOptions::Type>(CGOpts.SanitizeCoverageType);
242
22
  Opts.IndirectCalls = CGOpts.SanitizeCoverageIndirectCalls;
243
22
  Opts.TraceBB = CGOpts.SanitizeCoverageTraceBB;
244
22
  Opts.TraceCmp = CGOpts.SanitizeCoverageTraceCmp;
245
22
  Opts.TraceDiv = CGOpts.SanitizeCoverageTraceDiv;
246
22
  Opts.TraceGep = CGOpts.SanitizeCoverageTraceGep;
247
22
  Opts.Use8bitCounters = CGOpts.SanitizeCoverage8bitCounters;
248
22
  Opts.TracePC = CGOpts.SanitizeCoverageTracePC;
249
22
  Opts.TracePCGuard = CGOpts.SanitizeCoverageTracePCGuard;
250
22
  Opts.NoPrune = CGOpts.SanitizeCoverageNoPrune;
251
22
  Opts.Inline8bitCounters = CGOpts.SanitizeCoverageInline8bitCounters;
252
22
  Opts.InlineBoolFlag = CGOpts.SanitizeCoverageInlineBoolFlag;
253
22
  Opts.PCTable = CGOpts.SanitizeCoveragePCTable;
254
22
  Opts.StackDepth = CGOpts.SanitizeCoverageStackDepth;
255
22
  Opts.TraceLoads = CGOpts.SanitizeCoverageTraceLoads;
256
22
  Opts.TraceStores = CGOpts.SanitizeCoverageTraceStores;
257
22
  return Opts;
258
22
}
259
260
static void addSanitizerCoveragePass(const PassManagerBuilder &Builder,
261
6
                                     legacy::PassManagerBase &PM) {
262
6
  const PassManagerBuilderWrapper &BuilderWrapper =
263
6
      static_cast<const PassManagerBuilderWrapper &>(Builder);
264
6
  const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
265
6
  auto Opts = getSancovOptsFromCGOpts(CGOpts);
266
6
  PM.add(createModuleSanitizerCoverageLegacyPassPass(
267
6
      Opts, CGOpts.SanitizeCoverageAllowlistFiles,
268
6
      CGOpts.SanitizeCoverageIgnorelistFiles));
269
6
}
270
271
// Check if ASan should use GC-friendly instrumentation for globals.
272
// First of all, there is no point if -fdata-sections is off (expect for MachO,
273
// where this is not a factor). Also, on ELF this feature requires an assembler
274
// extension that only works with -integrated-as at the moment.
275
105
static bool asanUseGlobalsGC(const Triple &T, const CodeGenOptions &CGOpts) {
276
105
  if (!CGOpts.SanitizeAddressGlobalsDeadStripping)
277
87
    return false;
278
18
  switch (T.getObjectFormat()) {
279
8
  case Triple::MachO:
280
10
  case Triple::COFF:
281
10
    return true;
282
8
  case Triple::ELF:
283
8
    return CGOpts.DataSections && 
!CGOpts.DisableIntegratedAS4
;
284
0
  case Triple::GOFF:
285
0
    llvm::report_fatal_error("ASan not implemented for GOFF");
286
0
  case Triple::XCOFF:
287
0
    llvm::report_fatal_error("ASan not implemented for XCOFF.");
288
0
  case Triple::Wasm:
289
0
  case Triple::UnknownObjectFormat:
290
0
    break;
291
18
  }
292
0
  return false;
293
18
}
294
295
static void addMemProfilerPasses(const PassManagerBuilder &Builder,
296
0
                                 legacy::PassManagerBase &PM) {
297
0
  PM.add(createMemProfilerFunctionPass());
298
0
  PM.add(createModuleMemProfilerLegacyPassPass());
299
0
}
300
301
static void addAddressSanitizerPasses(const PassManagerBuilder &Builder,
302
5
                                      legacy::PassManagerBase &PM) {
303
5
  const PassManagerBuilderWrapper &BuilderWrapper =
304
5
      static_cast<const PassManagerBuilderWrapper&>(Builder);
305
5
  const Triple &T = BuilderWrapper.getTargetTriple();
306
5
  const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
307
5
  bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Address);
308
5
  bool UseAfterScope = CGOpts.SanitizeAddressUseAfterScope;
309
5
  bool UseOdrIndicator = CGOpts.SanitizeAddressUseOdrIndicator;
310
5
  bool UseGlobalsGC = asanUseGlobalsGC(T, CGOpts);
311
5
  llvm::AsanDtorKind DestructorKind = CGOpts.getSanitizeAddressDtor();
312
5
  llvm::AsanDetectStackUseAfterReturnMode UseAfterReturn =
313
5
      CGOpts.getSanitizeAddressUseAfterReturn();
314
5
  PM.add(createAddressSanitizerFunctionPass(/*CompileKernel*/ false, Recover,
315
5
                                            UseAfterScope, UseAfterReturn));
316
5
  PM.add(createModuleAddressSanitizerLegacyPassPass(
317
5
      /*CompileKernel*/ false, Recover, UseGlobalsGC, UseOdrIndicator,
318
5
      DestructorKind));
319
5
}
320
321
static void addKernelAddressSanitizerPasses(const PassManagerBuilder &Builder,
322
0
                                            legacy::PassManagerBase &PM) {
323
0
  PM.add(createAddressSanitizerFunctionPass(
324
0
      /*CompileKernel*/ true, /*Recover*/ true, /*UseAfterScope*/ false,
325
0
      /*UseAfterReturn*/ llvm::AsanDetectStackUseAfterReturnMode::Never));
326
0
  PM.add(createModuleAddressSanitizerLegacyPassPass(
327
0
      /*CompileKernel*/ true, /*Recover*/ true, /*UseGlobalsGC*/ true,
328
0
      /*UseOdrIndicator*/ false));
329
0
}
330
331
static void addHWAddressSanitizerPasses(const PassManagerBuilder &Builder,
332
2
                                            legacy::PassManagerBase &PM) {
333
2
  const PassManagerBuilderWrapper &BuilderWrapper =
334
2
      static_cast<const PassManagerBuilderWrapper &>(Builder);
335
2
  const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
336
2
  bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::HWAddress);
337
2
  PM.add(createHWAddressSanitizerLegacyPassPass(
338
2
      /*CompileKernel*/ false, Recover,
339
2
      /*DisableOptimization*/ CGOpts.OptimizationLevel == 0));
340
2
}
341
342
static void addKernelHWAddressSanitizerPasses(const PassManagerBuilder &Builder,
343
0
                                              legacy::PassManagerBase &PM) {
344
0
  const PassManagerBuilderWrapper &BuilderWrapper =
345
0
      static_cast<const PassManagerBuilderWrapper &>(Builder);
346
0
  const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
347
0
  PM.add(createHWAddressSanitizerLegacyPassPass(
348
0
      /*CompileKernel*/ true, /*Recover*/ true,
349
0
      /*DisableOptimization*/ CGOpts.OptimizationLevel == 0));
350
0
}
351
352
static void addGeneralOptsForMemorySanitizer(const PassManagerBuilder &Builder,
353
                                             legacy::PassManagerBase &PM,
354
2
                                             bool CompileKernel) {
355
2
  const PassManagerBuilderWrapper &BuilderWrapper =
356
2
      static_cast<const PassManagerBuilderWrapper&>(Builder);
357
2
  const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
358
2
  int TrackOrigins = CGOpts.SanitizeMemoryTrackOrigins;
359
2
  bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Memory);
360
2
  PM.add(createMemorySanitizerLegacyPassPass(
361
2
      MemorySanitizerOptions{TrackOrigins, Recover, CompileKernel,
362
2
                             CGOpts.SanitizeMemoryParamRetval != 0}));
363
364
  // MemorySanitizer inserts complex instrumentation that mostly follows
365
  // the logic of the original code, but operates on "shadow" values.
366
  // It can benefit from re-running some general purpose optimization passes.
367
2
  if (Builder.OptLevel > 0) {
368
1
    PM.add(createEarlyCSEPass());
369
1
    PM.add(createReassociatePass());
370
1
    PM.add(createLICMPass());
371
1
    PM.add(createGVNPass());
372
1
    PM.add(createInstructionCombiningPass());
373
1
    PM.add(createDeadStoreEliminationPass());
374
1
  }
375
2
}
376
377
static void addMemorySanitizerPass(const PassManagerBuilder &Builder,
378
2
                                   legacy::PassManagerBase &PM) {
379
2
  addGeneralOptsForMemorySanitizer(Builder, PM, /*CompileKernel*/ false);
380
2
}
381
382
static void addKernelMemorySanitizerPass(const PassManagerBuilder &Builder,
383
0
                                         legacy::PassManagerBase &PM) {
384
0
  addGeneralOptsForMemorySanitizer(Builder, PM, /*CompileKernel*/ true);
385
0
}
386
387
static void addThreadSanitizerPass(const PassManagerBuilder &Builder,
388
3
                                   legacy::PassManagerBase &PM) {
389
3
  PM.add(createThreadSanitizerLegacyPassPass());
390
3
}
391
392
static void addDataFlowSanitizerPass(const PassManagerBuilder &Builder,
393
0
                                     legacy::PassManagerBase &PM) {
394
0
  const PassManagerBuilderWrapper &BuilderWrapper =
395
0
      static_cast<const PassManagerBuilderWrapper&>(Builder);
396
0
  const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
397
0
  PM.add(createDataFlowSanitizerLegacyPassPass(LangOpts.NoSanitizeFiles));
398
0
}
399
400
static void addEntryExitInstrumentationPass(const PassManagerBuilder &Builder,
401
0
                                            legacy::PassManagerBase &PM) {
402
0
  PM.add(createEntryExitInstrumenterPass());
403
0
}
404
405
static void
406
addPostInlineEntryExitInstrumentationPass(const PassManagerBuilder &Builder,
407
0
                                          legacy::PassManagerBase &PM) {
408
0
  PM.add(createPostInlineEntryExitInstrumenterPass());
409
0
}
410
411
static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple,
412
23.8k
                                         const CodeGenOptions &CodeGenOpts) {
413
23.8k
  TargetLibraryInfoImpl *TLII = new TargetLibraryInfoImpl(TargetTriple);
414
415
23.8k
  switch (CodeGenOpts.getVecLib()) {
416
0
  case CodeGenOptions::Accelerate:
417
0
    TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::Accelerate);
418
0
    break;
419
0
  case CodeGenOptions::LIBMVEC:
420
0
    switch(TargetTriple.getArch()) {
421
0
      default:
422
0
        break;
423
0
      case llvm::Triple::x86_64:
424
0
        TLII->addVectorizableFunctionsFromVecLib
425
0
                (TargetLibraryInfoImpl::LIBMVEC_X86);
426
0
        break;
427
0
    }
428
0
    break;
429
0
  case CodeGenOptions::MASSV:
430
0
    TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::MASSV);
431
0
    break;
432
0
  case CodeGenOptions::SVML:
433
0
    TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::SVML);
434
0
    break;
435
1
  case CodeGenOptions::Darwin_libsystem_m:
436
1
    TLII->addVectorizableFunctionsFromVecLib(
437
1
        TargetLibraryInfoImpl::DarwinLibSystemM);
438
1
    break;
439
23.8k
  default:
440
23.8k
    break;
441
23.8k
  }
442
23.8k
  return TLII;
443
23.8k
}
444
445
static void addSymbolRewriterPass(const CodeGenOptions &Opts,
446
0
                                  legacy::PassManager *MPM) {
447
0
  llvm::SymbolRewriter::RewriteDescriptorList DL;
448
449
0
  llvm::SymbolRewriter::RewriteMapParser MapParser;
450
0
  for (const auto &MapFile : Opts.RewriteMapFiles)
451
0
    MapParser.parse(MapFile, &DL);
452
453
0
  MPM->add(createRewriteSymbolsPass(DL));
454
0
}
455
456
16.0k
static CodeGenOpt::Level getCGOptLevel(const CodeGenOptions &CodeGenOpts) {
457
16.0k
  switch (CodeGenOpts.OptimizationLevel) {
458
0
  default:
459
0
    llvm_unreachable("Invalid optimization level!");
460
13.1k
  case 0:
461
13.1k
    return CodeGenOpt::None;
462
2.28k
  case 1:
463
2.28k
    return CodeGenOpt::Less;
464
478
  case 2:
465
478
    return CodeGenOpt::Default; // O2/Os/Oz
466
167
  case 3:
467
167
    return CodeGenOpt::Aggressive;
468
16.0k
  }
469
16.0k
}
470
471
static Optional<llvm::CodeModel::Model>
472
16.0k
getCodeModel(const CodeGenOptions &CodeGenOpts) {
473
16.0k
  unsigned CodeModel = llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel)
474
16.0k
                           .Case("tiny", llvm::CodeModel::Tiny)
475
16.0k
                           .Case("small", llvm::CodeModel::Small)
476
16.0k
                           .Case("kernel", llvm::CodeModel::Kernel)
477
16.0k
                           .Case("medium", llvm::CodeModel::Medium)
478
16.0k
                           .Case("large", llvm::CodeModel::Large)
479
16.0k
                           .Case("default", ~1u)
480
16.0k
                           .Default(~0u);
481
16.0k
  assert(CodeModel != ~0u && "invalid code model!");
482
16.0k
  if (CodeModel == ~1u)
483
16.0k
    return None;
484
5
  return static_cast<llvm::CodeModel::Model>(CodeModel);
485
16.0k
}
486
487
4.28k
static CodeGenFileType getCodeGenFileType(BackendAction Action) {
488
4.28k
  if (Action == Backend_EmitObj)
489
3.79k
    return CGFT_ObjectFile;
490
491
  else if (Action == Backend_EmitMCNull)
491
7
    return CGFT_Null;
492
484
  else {
493
484
    assert(Action == Backend_EmitAssembly && "Invalid action!");
494
0
    return CGFT_AssemblyFile;
495
484
  }
496
4.28k
}
497
498
39.0k
static bool actionRequiresCodeGen(BackendAction Action) {
499
39.0k
  return Action != Backend_EmitNothing && 
Action != Backend_EmitBC38.6k
&&
500
39.0k
         
Action != Backend_EmitLL37.5k
;
501
39.0k
}
502
503
static bool initTargetOptions(DiagnosticsEngine &Diags,
504
                              llvm::TargetOptions &Options,
505
                              const CodeGenOptions &CodeGenOpts,
506
                              const clang::TargetOptions &TargetOpts,
507
                              const LangOptions &LangOpts,
508
16.0k
                              const HeaderSearchOptions &HSOpts) {
509
16.0k
  switch (LangOpts.getThreadModel()) {
510
15.8k
  case LangOptions::ThreadModelKind::POSIX:
511
15.8k
    Options.ThreadModel = llvm::ThreadModel::POSIX;
512
15.8k
    break;
513
288
  case LangOptions::ThreadModelKind::Single:
514
288
    Options.ThreadModel = llvm::ThreadModel::Single;
515
288
    break;
516
16.0k
  }
517
518
  // Set float ABI type.
519
16.0k
  assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" ||
520
16.0k
          CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) &&
521
16.0k
         "Invalid Floating Point ABI!");
522
0
  Options.FloatABIType =
523
16.0k
      llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI)
524
16.0k
          .Case("soft", llvm::FloatABI::Soft)
525
16.0k
          .Case("softfp", llvm::FloatABI::Soft)
526
16.0k
          .Case("hard", llvm::FloatABI::Hard)
527
16.0k
          .Default(llvm::FloatABI::Default);
528
529
  // Set FP fusion mode.
530
16.0k
  switch (LangOpts.getDefaultFPContractMode()) {
531
11.8k
  case LangOptions::FPM_Off:
532
    // Preserve any contraction performed by the front-end.  (Strict performs
533
    // splitting of the muladd intrinsic in the backend.)
534
11.8k
    Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
535
11.8k
    break;
536
4.15k
  case LangOptions::FPM_On:
537
4.18k
  case LangOptions::FPM_FastHonorPragmas:
538
4.18k
    Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
539
4.18k
    break;
540
53
  case LangOptions::FPM_Fast:
541
53
    Options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
542
53
    break;
543
16.0k
  }
544
545
16.0k
  Options.BinutilsVersion =
546
16.0k
      llvm::TargetMachine::parseBinutilsVersion(CodeGenOpts.BinutilsVersion);
547
16.0k
  Options.UseInitArray = CodeGenOpts.UseInitArray;
548
16.0k
  Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS;
549
16.0k
  Options.CompressDebugSections = CodeGenOpts.getCompressDebugSections();
550
16.0k
  Options.RelaxELFRelocations = CodeGenOpts.RelaxELFRelocations;
551
552
  // Set EABI version.
553
16.0k
  Options.EABIVersion = TargetOpts.EABIVersion;
554
555
16.0k
  if (LangOpts.hasSjLjExceptions())
556
28
    Options.ExceptionModel = llvm::ExceptionHandling::SjLj;
557
16.0k
  if (LangOpts.hasSEHExceptions())
558
22
    Options.ExceptionModel = llvm::ExceptionHandling::WinEH;
559
16.0k
  if (LangOpts.hasDWARFExceptions())
560
21
    Options.ExceptionModel = llvm::ExceptionHandling::DwarfCFI;
561
16.0k
  if (LangOpts.hasWasmExceptions())
562
0
    Options.ExceptionModel = llvm::ExceptionHandling::Wasm;
563
564
16.0k
  Options.NoInfsFPMath = LangOpts.NoHonorInfs;
565
16.0k
  Options.NoNaNsFPMath = LangOpts.NoHonorNaNs;
566
16.0k
  Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
567
16.0k
  Options.UnsafeFPMath = LangOpts.UnsafeFPMath;
568
16.0k
  Options.ApproxFuncFPMath = LangOpts.ApproxFunc;
569
570
16.0k
  Options.BBSections =
571
16.0k
      llvm::StringSwitch<llvm::BasicBlockSection>(CodeGenOpts.BBSections)
572
16.0k
          .Case("all", llvm::BasicBlockSection::All)
573
16.0k
          .Case("labels", llvm::BasicBlockSection::Labels)
574
16.0k
          .StartsWith("list=", llvm::BasicBlockSection::List)
575
16.0k
          .Case("none", llvm::BasicBlockSection::None)
576
16.0k
          .Default(llvm::BasicBlockSection::None);
577
578
16.0k
  if (Options.BBSections == llvm::BasicBlockSection::List) {
579
2
    ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
580
2
        MemoryBuffer::getFile(CodeGenOpts.BBSections.substr(5));
581
2
    if (!MBOrErr) {
582
1
      Diags.Report(diag::err_fe_unable_to_load_basic_block_sections_file)
583
1
          << MBOrErr.getError().message();
584
1
      return false;
585
1
    }
586
1
    Options.BBSectionsFuncListBuf = std::move(*MBOrErr);
587
1
  }
588
589
16.0k
  Options.EnableMachineFunctionSplitter = CodeGenOpts.SplitMachineFunctions;
590
16.0k
  Options.FunctionSections = CodeGenOpts.FunctionSections;
591
16.0k
  Options.DataSections = CodeGenOpts.DataSections;
592
16.0k
  Options.IgnoreXCOFFVisibility = LangOpts.IgnoreXCOFFVisibility;
593
16.0k
  Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames;
594
16.0k
  Options.UniqueBasicBlockSectionNames =
595
16.0k
      CodeGenOpts.UniqueBasicBlockSectionNames;
596
16.0k
  Options.TLSSize = CodeGenOpts.TLSSize;
597
16.0k
  Options.EmulatedTLS = CodeGenOpts.EmulatedTLS;
598
16.0k
  Options.ExplicitEmulatedTLS = CodeGenOpts.ExplicitEmulatedTLS;
599
16.0k
  Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning();
600
16.0k
  Options.EmitStackSizeSection = CodeGenOpts.StackSizeSection;
601
16.0k
  Options.StackUsageOutput = CodeGenOpts.StackUsageOutput;
602
16.0k
  Options.EmitAddrsig = CodeGenOpts.Addrsig;
603
16.0k
  Options.ForceDwarfFrameSection = CodeGenOpts.ForceDwarfFrameSection;
604
16.0k
  Options.EmitCallSiteInfo = CodeGenOpts.EmitCallSiteInfo;
605
16.0k
  Options.EnableAIXExtendedAltivecABI = CodeGenOpts.EnableAIXExtendedAltivecABI;
606
16.0k
  Options.XRayOmitFunctionIndex = CodeGenOpts.XRayOmitFunctionIndex;
607
16.0k
  Options.LoopAlignment = CodeGenOpts.LoopAlignment;
608
609
16.0k
  switch (CodeGenOpts.getSwiftAsyncFramePointer()) {
610
4
  case CodeGenOptions::SwiftAsyncFramePointerKind::Auto:
611
4
    Options.SwiftAsyncFramePointer =
612
4
        SwiftAsyncFramePointerMode::DeploymentBased;
613
4
    break;
614
615
16.0k
  case CodeGenOptions::SwiftAsyncFramePointerKind::Always:
616
16.0k
    Options.SwiftAsyncFramePointer = SwiftAsyncFramePointerMode::Always;
617
16.0k
    break;
618
619
4
  case CodeGenOptions::SwiftAsyncFramePointerKind::Never:
620
4
    Options.SwiftAsyncFramePointer = SwiftAsyncFramePointerMode::Never;
621
4
    break;
622
16.0k
  }
623
624
16.0k
  Options.MCOptions.SplitDwarfFile = CodeGenOpts.SplitDwarfFile;
625
16.0k
  Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll;
626
16.0k
  Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels;
627
16.0k
  Options.MCOptions.MCUseDwarfDirectory = !CodeGenOpts.NoDwarfDirectoryAsm;
628
16.0k
  Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack;
629
16.0k
  Options.MCOptions.MCIncrementalLinkerCompatible =
630
16.0k
      CodeGenOpts.IncrementalLinkerCompatible;
631
16.0k
  Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings;
632
16.0k
  Options.MCOptions.MCNoWarn = CodeGenOpts.NoWarn;
633
16.0k
  Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose;
634
16.0k
  Options.MCOptions.Dwarf64 = CodeGenOpts.Dwarf64;
635
16.0k
  Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments;
636
16.0k
  Options.MCOptions.ABIName = TargetOpts.ABI;
637
16.0k
  for (const auto &Entry : HSOpts.UserEntries)
638
37.1k
    if (!Entry.IsFramework &&
639
37.1k
        
(37.0k
Entry.Group == frontend::IncludeDirGroup::Quoted37.0k
||
640
37.0k
         
Entry.Group == frontend::IncludeDirGroup::Angled37.0k
||
641
37.0k
         
Entry.Group == frontend::IncludeDirGroup::System26.5k
))
642
32.7k
      Options.MCOptions.IASSearchPaths.push_back(
643
32.7k
          Entry.IgnoreSysRoot ? Entry.Path : 
HSOpts.Sysroot + Entry.Path0
);
644
16.0k
  Options.MCOptions.Argv0 = CodeGenOpts.Argv0;
645
16.0k
  Options.MCOptions.CommandLineArgs = CodeGenOpts.CommandLineArgs;
646
16.0k
  Options.DebugStrictDwarf = CodeGenOpts.DebugStrictDwarf;
647
16.0k
  Options.ObjectFilenameForDebug = CodeGenOpts.ObjectFilenameForDebug;
648
16.0k
  Options.Hotpatch = CodeGenOpts.HotPatch;
649
650
16.0k
  return true;
651
16.0k
}
652
653
static Optional<GCOVOptions> getGCOVOptions(const CodeGenOptions &CodeGenOpts,
654
19.0k
                                            const LangOptions &LangOpts) {
655
19.0k
  if (!CodeGenOpts.EmitGcovArcs && 
!CodeGenOpts.EmitGcovNotes19.0k
)
656
19.0k
    return None;
657
  // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if
658
  // LLVM's -default-gcov-version flag is set to something invalid.
659
15
  GCOVOptions Options;
660
15
  Options.EmitNotes = CodeGenOpts.EmitGcovNotes;
661
15
  Options.EmitData = CodeGenOpts.EmitGcovArcs;
662
15
  llvm::copy(CodeGenOpts.CoverageVersion, std::begin(Options.Version));
663
15
  Options.NoRedZone = CodeGenOpts.DisableRedZone;
664
15
  Options.Filter = CodeGenOpts.ProfileFilterFiles;
665
15
  Options.Exclude = CodeGenOpts.ProfileExcludeFiles;
666
15
  Options.Atomic = CodeGenOpts.AtomicProfileUpdate;
667
15
  return Options;
668
19.0k
}
669
670
static Optional<InstrProfOptions>
671
getInstrProfOptions(const CodeGenOptions &CodeGenOpts,
672
19.0k
                    const LangOptions &LangOpts) {
673
19.0k
  if (!CodeGenOpts.hasProfileClangInstr())
674
18.9k
    return None;
675
116
  InstrProfOptions Options;
676
116
  Options.NoRedZone = CodeGenOpts.DisableRedZone;
677
116
  Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput;
678
116
  Options.Atomic = CodeGenOpts.AtomicProfileUpdate;
679
116
  return Options;
680
19.0k
}
681
682
void EmitAssemblyHelper::CreatePasses(legacy::PassManager &MPM,
683
59
                                      legacy::FunctionPassManager &FPM) {
684
  // Handle disabling of all LLVM passes, where we want to preserve the
685
  // internal module before any optimization.
686
59
  if (CodeGenOpts.DisableLLVMPasses)
687
0
    return;
688
689
  // Figure out TargetLibraryInfo.  This needs to be added to MPM and FPM
690
  // manually (and not via PMBuilder), since some passes (eg. InstrProfiling)
691
  // are inserted before PMBuilder ones - they'd get the default-constructed
692
  // TLI with an unknown target otherwise.
693
59
  Triple TargetTriple(TheModule->getTargetTriple());
694
59
  std::unique_ptr<TargetLibraryInfoImpl> TLII(
695
59
      createTLII(TargetTriple, CodeGenOpts));
696
697
  // If we reached here with a non-empty index file name, then the index file
698
  // was empty and we are not performing ThinLTO backend compilation (used in
699
  // testing in a distributed build environment). Drop any the type test
700
  // assume sequences inserted for whole program vtables so that codegen doesn't
701
  // complain.
702
59
  if (!CodeGenOpts.ThinLTOIndexFile.empty())
703
0
    MPM.add(createLowerTypeTestsPass(/*ExportSummary=*/nullptr,
704
0
                                     /*ImportSummary=*/nullptr,
705
0
                                     /*DropTypeTests=*/true));
706
707
59
  PassManagerBuilderWrapper PMBuilder(TargetTriple, CodeGenOpts, LangOpts);
708
709
  // At O0 and O1 we only run the always inliner which is more efficient. At
710
  // higher optimization levels we run the normal inliner.
711
59
  if (CodeGenOpts.OptimizationLevel <= 1) {
712
54
    bool InsertLifetimeIntrinsics = ((CodeGenOpts.OptimizationLevel != 0 &&
713
54
                                      
!CodeGenOpts.DisableLifetimeMarkers31
) ||
714
54
                                     
LangOpts.Coroutines23
);
715
54
    PMBuilder.Inliner = createAlwaysInlinerLegacyPass(InsertLifetimeIntrinsics);
716
54
  } else {
717
    // We do not want to inline hot callsites for SamplePGO module-summary build
718
    // because profile annotation will happen again in ThinLTO backend, and we
719
    // want the IR of the hot path to match the profile.
720
5
    PMBuilder.Inliner = createFunctionInliningPass(
721
5
        CodeGenOpts.OptimizationLevel, CodeGenOpts.OptimizeSize,
722
5
        (!CodeGenOpts.SampleProfileFile.empty() &&
723
5
         
CodeGenOpts.PrepareForThinLTO0
));
724
5
  }
725
726
59
  PMBuilder.OptLevel = CodeGenOpts.OptimizationLevel;
727
59
  PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize;
728
59
  PMBuilder.SLPVectorize = CodeGenOpts.VectorizeSLP;
729
59
  PMBuilder.LoopVectorize = CodeGenOpts.VectorizeLoop;
730
  // Only enable CGProfilePass when using integrated assembler, since
731
  // non-integrated assemblers don't recognize .cgprofile section.
732
59
  PMBuilder.CallGraphProfile = !CodeGenOpts.DisableIntegratedAS;
733
734
59
  PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
735
  // Loop interleaving in the loop vectorizer has historically been set to be
736
  // enabled when loop unrolling is enabled.
737
59
  PMBuilder.LoopsInterleaved = CodeGenOpts.UnrollLoops;
738
59
  PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions;
739
59
  PMBuilder.PrepareForThinLTO = CodeGenOpts.PrepareForThinLTO;
740
59
  PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
741
59
  PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
742
743
59
  MPM.add(new TargetLibraryInfoWrapperPass(*TLII));
744
745
59
  if (TM)
746
59
    TM->adjustPassManager(PMBuilder);
747
748
59
  if (CodeGenOpts.DebugInfoForProfiling ||
749
59
      !CodeGenOpts.SampleProfileFile.empty())
750
1
    PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
751
1
                           addAddDiscriminatorsPass);
752
753
  // In ObjC ARC mode, add the main ARC optimization passes.
754
59
  if (LangOpts.ObjCAutoRefCount) {
755
0
    PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
756
0
                           addObjCARCExpandPass);
757
0
    PMBuilder.addExtension(PassManagerBuilder::EP_ModuleOptimizerEarly,
758
0
                           addObjCARCAPElimPass);
759
0
    PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
760
0
                           addObjCARCOptPass);
761
0
  }
762
763
59
  if (LangOpts.Coroutines)
764
0
    addCoroutinePassesToExtensionPoints(PMBuilder);
765
766
59
  if (!CodeGenOpts.MemoryProfileOutput.empty()) {
767
0
    PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
768
0
                           addMemProfilerPasses);
769
0
    PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
770
0
                           addMemProfilerPasses);
771
0
  }
772
773
59
  if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) {
774
1
    PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
775
1
                           addBoundsCheckingPass);
776
1
    PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
777
1
                           addBoundsCheckingPass);
778
1
  }
779
780
59
  if (CodeGenOpts.hasSanitizeCoverage()) {
781
6
    PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
782
6
                           addSanitizerCoveragePass);
783
6
    PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
784
6
                           addSanitizerCoveragePass);
785
6
  }
786
787
59
  if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
788
5
    PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
789
5
                           addAddressSanitizerPasses);
790
5
    PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
791
5
                           addAddressSanitizerPasses);
792
5
  }
793
794
59
  if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) {
795
0
    PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
796
0
                           addKernelAddressSanitizerPasses);
797
0
    PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
798
0
                           addKernelAddressSanitizerPasses);
799
0
  }
800
801
59
  if (LangOpts.Sanitize.has(SanitizerKind::HWAddress)) {
802
2
    PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
803
2
                           addHWAddressSanitizerPasses);
804
2
    PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
805
2
                           addHWAddressSanitizerPasses);
806
2
  }
807
808
59
  if (LangOpts.Sanitize.has(SanitizerKind::KernelHWAddress)) {
809
0
    PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
810
0
                           addKernelHWAddressSanitizerPasses);
811
0
    PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
812
0
                           addKernelHWAddressSanitizerPasses);
813
0
  }
814
815
59
  if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
816
2
    PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
817
2
                           addMemorySanitizerPass);
818
2
    PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
819
2
                           addMemorySanitizerPass);
820
2
  }
821
822
59
  if (LangOpts.Sanitize.has(SanitizerKind::KernelMemory)) {
823
0
    PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
824
0
                           addKernelMemorySanitizerPass);
825
0
    PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
826
0
                           addKernelMemorySanitizerPass);
827
0
  }
828
829
59
  if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
830
3
    PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
831
3
                           addThreadSanitizerPass);
832
3
    PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
833
3
                           addThreadSanitizerPass);
834
3
  }
835
836
59
  if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
837
0
    PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
838
0
                           addDataFlowSanitizerPass);
839
0
    PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
840
0
                           addDataFlowSanitizerPass);
841
0
  }
842
843
59
  if (CodeGenOpts.InstrumentFunctions ||
844
59
      CodeGenOpts.InstrumentFunctionEntryBare ||
845
59
      CodeGenOpts.InstrumentFunctionsAfterInlining ||
846
59
      CodeGenOpts.InstrumentForProfiling) {
847
0
    PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
848
0
                           addEntryExitInstrumentationPass);
849
0
    PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
850
0
                           addEntryExitInstrumentationPass);
851
0
    PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
852
0
                           addPostInlineEntryExitInstrumentationPass);
853
0
    PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
854
0
                           addPostInlineEntryExitInstrumentationPass);
855
0
  }
856
857
  // Set up the per-function pass manager.
858
59
  FPM.add(new TargetLibraryInfoWrapperPass(*TLII));
859
59
  if (CodeGenOpts.VerifyModule)
860
57
    FPM.add(createVerifierPass());
861
862
  // Set up the per-module pass manager.
863
59
  if (!CodeGenOpts.RewriteMapFiles.empty())
864
0
    addSymbolRewriterPass(CodeGenOpts, &MPM);
865
866
59
  if (Optional<GCOVOptions> Options = getGCOVOptions(CodeGenOpts, LangOpts)) {
867
0
    MPM.add(createGCOVProfilerPass(*Options));
868
0
    if (CodeGenOpts.getDebugInfo() == codegenoptions::NoDebugInfo)
869
0
      MPM.add(createStripSymbolsPass(true));
870
0
  }
871
872
59
  if (Optional<InstrProfOptions> Options =
873
59
          getInstrProfOptions(CodeGenOpts, LangOpts))
874
0
    MPM.add(createInstrProfilingLegacyPass(*Options, false));
875
876
59
  bool hasIRInstr = false;
877
59
  if (CodeGenOpts.hasProfileIRInstr()) {
878
0
    PMBuilder.EnablePGOInstrGen = true;
879
0
    hasIRInstr = true;
880
0
  }
881
59
  if (CodeGenOpts.hasProfileCSIRInstr()) {
882
1
    assert(!CodeGenOpts.hasProfileCSIRUse() &&
883
1
           "Cannot have both CSProfileUse pass and CSProfileGen pass at the "
884
1
           "same time");
885
0
    assert(!hasIRInstr &&
886
1
           "Cannot have both ProfileGen pass and CSProfileGen pass at the "
887
1
           "same time");
888
0
    PMBuilder.EnablePGOCSInstrGen = true;
889
1
    hasIRInstr = true;
890
1
  }
891
59
  if (hasIRInstr) {
892
1
    if (!CodeGenOpts.InstrProfileOutput.empty())
893
1
      PMBuilder.PGOInstrGen = CodeGenOpts.InstrProfileOutput;
894
0
    else
895
0
      PMBuilder.PGOInstrGen = getDefaultProfileGenName();
896
1
  }
897
59
  if (CodeGenOpts.hasProfileIRUse()) {
898
2
    PMBuilder.PGOInstrUse = CodeGenOpts.ProfileInstrumentUsePath;
899
2
    PMBuilder.EnablePGOCSInstrUse = CodeGenOpts.hasProfileCSIRUse();
900
2
  }
901
902
59
  if (!CodeGenOpts.SampleProfileFile.empty())
903
1
    PMBuilder.PGOSampleUse = CodeGenOpts.SampleProfileFile;
904
905
59
  PMBuilder.populateFunctionPassManager(FPM);
906
59
  PMBuilder.populateModulePassManager(MPM);
907
59
}
908
909
19.6k
static void setCommandLineOpts(const CodeGenOptions &CodeGenOpts) {
910
19.6k
  SmallVector<const char *, 16> BackendArgs;
911
19.6k
  BackendArgs.push_back("clang"); // Fake program name.
912
19.6k
  if (!CodeGenOpts.DebugPass.empty()) {
913
2
    BackendArgs.push_back("-debug-pass");
914
2
    BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
915
2
  }
916
19.6k
  if (!CodeGenOpts.LimitFloatPrecision.empty()) {
917
0
    BackendArgs.push_back("-limit-float-precision");
918
0
    BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
919
0
  }
920
  // Check for the default "clang" invocation that won't set any cl::opt values.
921
  // Skip trying to parse the command line invocation to avoid the issues
922
  // described below.
923
19.6k
  if (BackendArgs.size() == 1)
924
19.6k
    return;
925
2
  BackendArgs.push_back(nullptr);
926
  // FIXME: The command line parser below is not thread-safe and shares a global
927
  // state, so this call might crash or overwrite the options of another Clang
928
  // instance in the same process.
929
2
  llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
930
2
                                    BackendArgs.data());
931
2
}
932
933
19.5k
void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
934
  // Create the TargetMachine for generating code.
935
19.5k
  std::string Error;
936
19.5k
  std::string Triple = TheModule->getTargetTriple();
937
19.5k
  const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
938
19.5k
  if (!TheTarget) {
939
3.52k
    if (MustCreateTM)
940
0
      Diags.Report(diag::err_fe_unable_to_create_target) << Error;
941
3.52k
    return;
942
3.52k
  }
943
944
16.0k
  Optional<llvm::CodeModel::Model> CM = getCodeModel(CodeGenOpts);
945
16.0k
  std::string FeaturesStr =
946
16.0k
      llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ",");
947
16.0k
  llvm::Reloc::Model RM = CodeGenOpts.RelocationModel;
948
16.0k
  CodeGenOpt::Level OptLevel = getCGOptLevel(CodeGenOpts);
949
950
16.0k
  llvm::TargetOptions Options;
951
16.0k
  if (!initTargetOptions(Diags, Options, CodeGenOpts, TargetOpts, LangOpts,
952
16.0k
                         HSOpts))
953
1
    return;
954
16.0k
  TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr,
955
16.0k
                                          Options, RM, CM, OptLevel));
956
16.0k
}
957
958
bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses,
959
                                       BackendAction Action,
960
                                       raw_pwrite_stream &OS,
961
4.25k
                                       raw_pwrite_stream *DwoOS) {
962
  // Add LibraryInfo.
963
4.25k
  llvm::Triple TargetTriple(TheModule->getTargetTriple());
964
4.25k
  std::unique_ptr<TargetLibraryInfoImpl> TLII(
965
4.25k
      createTLII(TargetTriple, CodeGenOpts));
966
4.25k
  CodeGenPasses.add(new TargetLibraryInfoWrapperPass(*TLII));
967
968
  // Normal mode, emit a .s or .o file by running the code generator. Note,
969
  // this also adds codegenerator level optimization passes.
970
4.25k
  CodeGenFileType CGFT = getCodeGenFileType(Action);
971
972
  // Add ObjC ARC final-cleanup optimizations. This is done as part of the
973
  // "codegen" passes so that it isn't run multiple times when there is
974
  // inlining happening.
975
4.25k
  if (CodeGenOpts.OptimizationLevel > 0)
976
379
    CodeGenPasses.add(createObjCARCContractPass());
977
978
4.25k
  if (TM->addPassesToEmitFile(CodeGenPasses, OS, DwoOS, CGFT,
979
4.25k
                              /*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
980
0
    Diags.Report(diag::err_fe_unable_to_interface_with_target);
981
0
    return false;
982
0
  }
983
984
4.25k
  return true;
985
4.25k
}
986
987
void EmitAssemblyHelper::EmitAssemblyWithLegacyPassManager(
988
59
    BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS) {
989
59
  TimeRegion Region(CodeGenOpts.TimePasses ? 
&CodeGenerationTime1
:
nullptr58
);
990
991
59
  setCommandLineOpts(CodeGenOpts);
992
993
59
  bool UsesCodeGen = actionRequiresCodeGen(Action);
994
59
  CreateTargetMachine(UsesCodeGen);
995
996
59
  if (UsesCodeGen && 
!TM1
)
997
0
    return;
998
59
  if (TM)
999
59
    TheModule->setDataLayout(TM->createDataLayout());
1000
1001
59
  DebugifyCustomPassManager PerModulePasses;
1002
59
  DebugInfoPerPassMap DIPreservationMap;
1003
59
  if (CodeGenOpts.EnableDIPreservationVerify) {
1004
0
    PerModulePasses.setDebugifyMode(DebugifyMode::OriginalDebugInfo);
1005
0
    PerModulePasses.setDIPreservationMap(DIPreservationMap);
1006
1007
0
    if (!CodeGenOpts.DIBugsReportFilePath.empty())
1008
0
      PerModulePasses.setOrigDIVerifyBugsReportFilePath(
1009
0
          CodeGenOpts.DIBugsReportFilePath);
1010
0
  }
1011
59
  PerModulePasses.add(
1012
59
      createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
1013
1014
59
  legacy::FunctionPassManager PerFunctionPasses(TheModule);
1015
59
  PerFunctionPasses.add(
1016
59
      createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
1017
1018
59
  CreatePasses(PerModulePasses, PerFunctionPasses);
1019
1020
  // Add a verifier pass if requested. We don't have to do this if the action
1021
  // requires code generation because there will already be a verifier pass in
1022
  // the code-generation pipeline.
1023
59
  if (!UsesCodeGen && 
CodeGenOpts.VerifyModule58
)
1024
56
    PerModulePasses.add(createVerifierPass());
1025
1026
59
  legacy::PassManager CodeGenPasses;
1027
59
  CodeGenPasses.add(
1028
59
      createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
1029
1030
59
  std::unique_ptr<llvm::ToolOutputFile> ThinLinkOS, DwoOS;
1031
1032
59
  switch (Action) {
1033
7
  case Backend_EmitNothing:
1034
7
    break;
1035
1036
5
  case Backend_EmitBC:
1037
5
    if (CodeGenOpts.PrepareForThinLTO && 
!CodeGenOpts.DisableLLVMPasses3
) {
1038
3
      if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
1039
0
        ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
1040
0
        if (!ThinLinkOS)
1041
0
          return;
1042
0
      }
1043
3
      if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
1044
2
        TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
1045
2
                                 CodeGenOpts.EnableSplitLTOUnit);
1046
3
      PerModulePasses.add(createWriteThinLTOBitcodePass(
1047
3
          *OS, ThinLinkOS ? 
&ThinLinkOS->os()0
: nullptr));
1048
3
    } else {
1049
      // Emit a module summary by default for Regular LTO except for ld64
1050
      // targets
1051
2
      bool EmitLTOSummary =
1052
2
          (CodeGenOpts.PrepareForLTO &&
1053
2
           
!CodeGenOpts.DisableLLVMPasses1
&&
1054
2
           llvm::Triple(TheModule->getTargetTriple()).getVendor() !=
1055
1
               llvm::Triple::Apple);
1056
2
      if (EmitLTOSummary) {
1057
0
        if (!TheModule->getModuleFlag("ThinLTO"))
1058
0
          TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
1059
0
        if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
1060
0
          TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
1061
0
                                   uint32_t(1));
1062
0
      }
1063
1064
2
      PerModulePasses.add(createBitcodeWriterPass(
1065
2
          *OS, CodeGenOpts.EmitLLVMUseLists, EmitLTOSummary));
1066
2
    }
1067
5
    break;
1068
1069
46
  case Backend_EmitLL:
1070
46
    PerModulePasses.add(
1071
46
        createPrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
1072
46
    break;
1073
1074
1
  default:
1075
1
    if (!CodeGenOpts.SplitDwarfOutput.empty()) {
1076
0
      DwoOS = openOutputFile(CodeGenOpts.SplitDwarfOutput);
1077
0
      if (!DwoOS)
1078
0
        return;
1079
0
    }
1080
1
    if (!AddEmitPasses(CodeGenPasses, Action, *OS,
1081
1
                       DwoOS ? 
&DwoOS->os()0
: nullptr))
1082
0
      return;
1083
59
  }
1084
1085
  // Before executing passes, print the final values of the LLVM options.
1086
59
  cl::PrintOptionValues();
1087
1088
  // Run passes. For now we do all passes at once, but eventually we
1089
  // would like to have the option of streaming code generation.
1090
1091
59
  {
1092
59
    PrettyStackTraceString CrashInfo("Per-function optimization");
1093
59
    llvm::TimeTraceScope TimeScope("PerFunctionPasses");
1094
1095
59
    PerFunctionPasses.doInitialization();
1096
59
    for (Function &F : *TheModule)
1097
1.63k
      if (!F.isDeclaration())
1098
1.23k
        PerFunctionPasses.run(F);
1099
59
    PerFunctionPasses.doFinalization();
1100
59
  }
1101
1102
59
  {
1103
59
    PrettyStackTraceString CrashInfo("Per-module optimization passes");
1104
59
    llvm::TimeTraceScope TimeScope("PerModulePasses");
1105
59
    PerModulePasses.run(*TheModule);
1106
59
  }
1107
1108
59
  {
1109
59
    PrettyStackTraceString CrashInfo("Code generation");
1110
59
    llvm::TimeTraceScope TimeScope("CodeGenPasses");
1111
59
    CodeGenPasses.run(*TheModule);
1112
59
  }
1113
1114
59
  if (ThinLinkOS)
1115
0
    ThinLinkOS->keep();
1116
59
  if (DwoOS)
1117
0
    DwoOS->keep();
1118
59
}
1119
1120
19.0k
static OptimizationLevel mapToLevel(const CodeGenOptions &Opts) {
1121
19.0k
  switch (Opts.OptimizationLevel) {
1122
0
  default:
1123
0
    llvm_unreachable("Invalid optimization level!");
1124
1125
16.2k
  case 0:
1126
16.2k
    return OptimizationLevel::O0;
1127
1128
2.14k
  case 1:
1129
2.14k
    return OptimizationLevel::O1;
1130
1131
469
  case 2:
1132
469
    switch (Opts.OptimizeSize) {
1133
0
    default:
1134
0
      llvm_unreachable("Invalid optimization level for size!");
1135
1136
398
    case 0:
1137
398
      return OptimizationLevel::O2;
1138
1139
51
    case 1:
1140
51
      return OptimizationLevel::Os;
1141
1142
20
    case 2:
1143
20
      return OptimizationLevel::Oz;
1144
469
    }
1145
1146
167
  case 3:
1147
167
    return OptimizationLevel::O3;
1148
19.0k
  }
1149
19.0k
}
1150
1151
static void addSanitizers(const Triple &TargetTriple,
1152
                          const CodeGenOptions &CodeGenOpts,
1153
19.0k
                          const LangOptions &LangOpts, PassBuilder &PB) {
1154
19.0k
  PB.registerOptimizerLastEPCallback([&](ModulePassManager &MPM,
1155
19.0k
                                         OptimizationLevel Level) {
1156
19.0k
    if (CodeGenOpts.hasSanitizeCoverage()) {
1157
16
      auto SancovOpts = getSancovOptsFromCGOpts(CodeGenOpts);
1158
16
      MPM.addPass(ModuleSanitizerCoveragePass(
1159
16
          SancovOpts, CodeGenOpts.SanitizeCoverageAllowlistFiles,
1160
16
          CodeGenOpts.SanitizeCoverageIgnorelistFiles));
1161
16
    }
1162
1163
38.0k
    auto MSanPass = [&](SanitizerMask Mask, bool CompileKernel) {
1164
38.0k
      if (LangOpts.Sanitize.has(Mask)) {
1165
41
        int TrackOrigins = CodeGenOpts.SanitizeMemoryTrackOrigins;
1166
41
        bool Recover = CodeGenOpts.SanitizeRecover.has(Mask);
1167
1168
41
        MemorySanitizerOptions options(TrackOrigins, Recover, CompileKernel,
1169
41
                                       CodeGenOpts.SanitizeMemoryParamRetval);
1170
41
        MPM.addPass(ModuleMemorySanitizerPass(options));
1171
41
        FunctionPassManager FPM;
1172
41
        FPM.addPass(MemorySanitizerPass(options));
1173
41
        if (Level != OptimizationLevel::O0) {
1174
          // MemorySanitizer inserts complex instrumentation that mostly
1175
          // follows the logic of the original code, but operates on
1176
          // "shadow" values. It can benefit from re-running some
1177
          // general purpose optimization passes.
1178
15
          FPM.addPass(EarlyCSEPass());
1179
          // TODO: Consider add more passes like in
1180
          // addGeneralOptsForMemorySanitizer. EarlyCSEPass makes visible
1181
          // difference on size. It's not clear if the rest is still
1182
          // usefull. InstCombinePass breakes
1183
          // compiler-rt/test/msan/select_origin.cpp.
1184
15
        }
1185
41
        MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1186
41
      }
1187
38.0k
    };
1188
19.0k
    MSanPass(SanitizerKind::Memory, false);
1189
19.0k
    MSanPass(SanitizerKind::KernelMemory, true);
1190
1191
19.0k
    if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
1192
36
      MPM.addPass(ModuleThreadSanitizerPass());
1193
36
      MPM.addPass(createModuleToFunctionPassAdaptor(ThreadSanitizerPass()));
1194
36
    }
1195
1196
38.0k
    auto ASanPass = [&](SanitizerMask Mask, bool CompileKernel) {
1197
38.0k
      if (LangOpts.Sanitize.has(Mask)) {
1198
100
        bool UseGlobalGC = asanUseGlobalsGC(TargetTriple, CodeGenOpts);
1199
100
        bool UseOdrIndicator = CodeGenOpts.SanitizeAddressUseOdrIndicator;
1200
100
        llvm::AsanDtorKind DestructorKind =
1201
100
            CodeGenOpts.getSanitizeAddressDtor();
1202
100
        AddressSanitizerOptions Opts;
1203
100
        Opts.CompileKernel = CompileKernel;
1204
100
        Opts.Recover = CodeGenOpts.SanitizeRecover.has(Mask);
1205
100
        Opts.UseAfterScope = CodeGenOpts.SanitizeAddressUseAfterScope;
1206
100
        Opts.UseAfterReturn = CodeGenOpts.getSanitizeAddressUseAfterReturn();
1207
100
        MPM.addPass(RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
1208
100
        MPM.addPass(ModuleAddressSanitizerPass(
1209
100
            Opts, UseGlobalGC, UseOdrIndicator, DestructorKind));
1210
100
      }
1211
38.0k
    };
1212
19.0k
    ASanPass(SanitizerKind::Address, false);
1213
19.0k
    ASanPass(SanitizerKind::KernelAddress, true);
1214
1215
38.0k
    auto HWASanPass = [&](SanitizerMask Mask, bool CompileKernel) {
1216
38.0k
      if (LangOpts.Sanitize.has(Mask)) {
1217
30
        bool Recover = CodeGenOpts.SanitizeRecover.has(Mask);
1218
30
        MPM.addPass(HWAddressSanitizerPass(
1219
30
            {CompileKernel, Recover,
1220
30
             /*DisableOptimization=*/CodeGenOpts.OptimizationLevel == 0}));
1221
30
      }
1222
38.0k
    };
1223
19.0k
    HWASanPass(SanitizerKind::HWAddress, false);
1224
19.0k
    HWASanPass(SanitizerKind::KernelHWAddress, true);
1225
1226
19.0k
    if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
1227
10
      MPM.addPass(DataFlowSanitizerPass(LangOpts.NoSanitizeFiles));
1228
10
    }
1229
19.0k
  });
1230
19.0k
}
1231
1232
void EmitAssemblyHelper::RunOptimizationPipeline(
1233
    BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS,
1234
19.5k
    std::unique_ptr<llvm::ToolOutputFile> &ThinLinkOS) {
1235
19.5k
  Optional<PGOOptions> PGOOpt;
1236
1237
19.5k
  if (CodeGenOpts.hasProfileIRInstr())
1238
    // -fprofile-generate.
1239
5
    PGOOpt = PGOOptions(CodeGenOpts.InstrProfileOutput.empty()
1240
5
                            ? getDefaultProfileGenName()
1241
5
                            : 
CodeGenOpts.InstrProfileOutput0
,
1242
5
                        "", "", PGOOptions::IRInstr, PGOOptions::NoCSAction,
1243
5
                        CodeGenOpts.DebugInfoForProfiling);
1244
19.5k
  else if (CodeGenOpts.hasProfileIRUse()) {
1245
    // -fprofile-use.
1246
18
    auto CSAction = CodeGenOpts.hasProfileCSIRUse() ? 
PGOOptions::CSIRUse4
1247
18
                                                    : 
PGOOptions::NoCSAction14
;
1248
18
    PGOOpt = PGOOptions(CodeGenOpts.ProfileInstrumentUsePath, "",
1249
18
                        CodeGenOpts.ProfileRemappingFile, PGOOptions::IRUse,
1250
18
                        CSAction, CodeGenOpts.DebugInfoForProfiling);
1251
19.4k
  } else if (!CodeGenOpts.SampleProfileFile.empty())
1252
    // -fprofile-sample-use
1253
11
    PGOOpt = PGOOptions(
1254
11
        CodeGenOpts.SampleProfileFile, "", CodeGenOpts.ProfileRemappingFile,
1255
11
        PGOOptions::SampleUse, PGOOptions::NoCSAction,
1256
11
        CodeGenOpts.DebugInfoForProfiling, CodeGenOpts.PseudoProbeForProfiling);
1257
19.4k
  else if (CodeGenOpts.PseudoProbeForProfiling)
1258
    // -fpseudo-probe-for-profiling
1259
4
    PGOOpt =
1260
4
        PGOOptions("", "", "", PGOOptions::NoAction, PGOOptions::NoCSAction,
1261
4
                   CodeGenOpts.DebugInfoForProfiling, true);
1262
19.4k
  else if (CodeGenOpts.DebugInfoForProfiling)
1263
    // -fdebug-info-for-profiling
1264
3
    PGOOpt = PGOOptions("", "", "", PGOOptions::NoAction,
1265
3
                        PGOOptions::NoCSAction, true);
1266
1267
  // Check to see if we want to generate a CS profile.
1268
19.5k
  if (CodeGenOpts.hasProfileCSIRInstr()) {
1269
6
    assert(!CodeGenOpts.hasProfileCSIRUse() &&
1270
6
           "Cannot have both CSProfileUse pass and CSProfileGen pass at "
1271
6
           "the same time");
1272
6
    if (PGOOpt.hasValue()) {
1273
3
      assert(PGOOpt->Action != PGOOptions::IRInstr &&
1274
3
             PGOOpt->Action != PGOOptions::SampleUse &&
1275
3
             "Cannot run CSProfileGen pass with ProfileGen or SampleUse "
1276
3
             " pass");
1277
3
      PGOOpt->CSProfileGenFile = CodeGenOpts.InstrProfileOutput.empty()
1278
3
                                     ? 
getDefaultProfileGenName()1
1279
3
                                     : 
CodeGenOpts.InstrProfileOutput2
;
1280
3
      PGOOpt->CSAction = PGOOptions::CSIRInstr;
1281
3
    } else
1282
3
      PGOOpt = PGOOptions("",
1283
3
                          CodeGenOpts.InstrProfileOutput.empty()
1284
3
                              ? 
getDefaultProfileGenName()2
1285
3
                              : 
CodeGenOpts.InstrProfileOutput1
,
1286
3
                          "", PGOOptions::NoAction, PGOOptions::CSIRInstr,
1287
3
                          CodeGenOpts.DebugInfoForProfiling);
1288
6
  }
1289
19.5k
  if (TM)
1290
15.9k
    TM->setPGOOption(PGOOpt);
1291
1292
19.5k
  PipelineTuningOptions PTO;
1293
19.5k
  PTO.LoopUnrolling = CodeGenOpts.UnrollLoops;
1294
  // For historical reasons, loop interleaving is set to mirror setting for loop
1295
  // unrolling.
1296
19.5k
  PTO.LoopInterleaving = CodeGenOpts.UnrollLoops;
1297
19.5k
  PTO.LoopVectorization = CodeGenOpts.VectorizeLoop;
1298
19.5k
  PTO.SLPVectorization = CodeGenOpts.VectorizeSLP;
1299
19.5k
  PTO.MergeFunctions = CodeGenOpts.MergeFunctions;
1300
  // Only enable CGProfilePass when using integrated assembler, since
1301
  // non-integrated assemblers don't recognize .cgprofile section.
1302
19.5k
  PTO.CallGraphProfile = !CodeGenOpts.DisableIntegratedAS;
1303
1304
19.5k
  LoopAnalysisManager LAM;
1305
19.5k
  FunctionAnalysisManager FAM;
1306
19.5k
  CGSCCAnalysisManager CGAM;
1307
19.5k
  ModuleAnalysisManager MAM;
1308
1309
19.5k
  bool DebugPassStructure = CodeGenOpts.DebugPass == "Structure";
1310
19.5k
  PassInstrumentationCallbacks PIC;
1311
19.5k
  PrintPassOptions PrintPassOpts;
1312
19.5k
  PrintPassOpts.Indent = DebugPassStructure;
1313
19.5k
  PrintPassOpts.SkipAnalyses = DebugPassStructure;
1314
19.5k
  StandardInstrumentations SI(CodeGenOpts.DebugPassManager ||
1315
19.5k
                                  
DebugPassStructure19.4k
,
1316
19.5k
                              /*VerifyEach*/ false, PrintPassOpts);
1317
19.5k
  SI.registerCallbacks(PIC, &FAM);
1318
19.5k
  PassBuilder PB(TM.get(), PTO, PGOOpt, &PIC);
1319
1320
  // Attempt to load pass plugins and register their callbacks with PB.
1321
19.5k
  for (auto &PluginFN : CodeGenOpts.PassPlugins) {
1322
0
    auto PassPlugin = PassPlugin::Load(PluginFN);
1323
0
    if (PassPlugin) {
1324
0
      PassPlugin->registerPassBuilderCallbacks(PB);
1325
0
    } else {
1326
0
      Diags.Report(diag::err_fe_unable_to_load_plugin)
1327
0
          << PluginFN << toString(PassPlugin.takeError());
1328
0
    }
1329
0
  }
1330
19.5k
#define HANDLE_EXTENSION(Ext)                                                  \
1331
19.5k
  get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
1332
19.5k
#include "llvm/Support/Extension.def"
1333
1334
  // Register the target library analysis directly and give it a customized
1335
  // preset TLI.
1336
19.5k
  Triple TargetTriple(TheModule->getTargetTriple());
1337
19.5k
  std::unique_ptr<TargetLibraryInfoImpl> TLII(
1338
19.5k
      createTLII(TargetTriple, CodeGenOpts));
1339
19.5k
  FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
1340
1341
  // Register all the basic analyses with the managers.
1342
19.5k
  PB.registerModuleAnalyses(MAM);
1343
19.5k
  PB.registerCGSCCAnalyses(CGAM);
1344
19.5k
  PB.registerFunctionAnalyses(FAM);
1345
19.5k
  PB.registerLoopAnalyses(LAM);
1346
19.5k
  PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
1347
1348
19.5k
  ModulePassManager MPM;
1349
1350
19.5k
  if (!CodeGenOpts.DisableLLVMPasses) {
1351
    // Map our optimization levels into one of the distinct levels used to
1352
    // configure the pipeline.
1353
19.0k
    OptimizationLevel Level = mapToLevel(CodeGenOpts);
1354
1355
19.0k
    bool IsThinLTO = CodeGenOpts.PrepareForThinLTO;
1356
19.0k
    bool IsLTO = CodeGenOpts.PrepareForLTO;
1357
1358
19.0k
    if (LangOpts.ObjCAutoRefCount) {
1359
137
      PB.registerPipelineStartEPCallback(
1360
137
          [](ModulePassManager &MPM, OptimizationLevel Level) {
1361
137
            if (Level != OptimizationLevel::O0)
1362
4
              MPM.addPass(
1363
4
                  createModuleToFunctionPassAdaptor(ObjCARCExpandPass()));
1364
137
          });
1365
137
      PB.registerPipelineEarlySimplificationEPCallback(
1366
137
          [](ModulePassManager &MPM, OptimizationLevel Level) {
1367
137
            if (Level != OptimizationLevel::O0)
1368
4
              MPM.addPass(ObjCARCAPElimPass());
1369
137
          });
1370
137
      PB.registerScalarOptimizerLateEPCallback(
1371
137
          [](FunctionPassManager &FPM, OptimizationLevel Level) {
1372
137
            if (Level != OptimizationLevel::O0)
1373
4
              FPM.addPass(ObjCARCOptPass());
1374
137
          });
1375
137
    }
1376
1377
    // If we reached here with a non-empty index file name, then the index
1378
    // file was empty and we are not performing ThinLTO backend compilation
1379
    // (used in testing in a distributed build environment).
1380
19.0k
    bool IsThinLTOPostLink = !CodeGenOpts.ThinLTOIndexFile.empty();
1381
    // If so drop any the type test assume sequences inserted for whole program
1382
    // vtables so that codegen doesn't complain.
1383
19.0k
    if (IsThinLTOPostLink)
1384
5
      PB.registerPipelineStartEPCallback(
1385
5
          [](ModulePassManager &MPM, OptimizationLevel Level) {
1386
5
            MPM.addPass(LowerTypeTestsPass(/*ExportSummary=*/nullptr,
1387
5
                                           /*ImportSummary=*/nullptr,
1388
5
                                           /*DropTypeTests=*/true));
1389
5
          });
1390
1391
19.0k
    if (CodeGenOpts.InstrumentFunctions ||
1392
19.0k
        
CodeGenOpts.InstrumentFunctionEntryBare19.0k
||
1393
19.0k
        
CodeGenOpts.InstrumentFunctionsAfterInlining19.0k
||
1394
19.0k
        
CodeGenOpts.InstrumentForProfiling19.0k
) {
1395
68
      PB.registerPipelineStartEPCallback(
1396
68
          [](ModulePassManager &MPM, OptimizationLevel Level) {
1397
68
            MPM.addPass(createModuleToFunctionPassAdaptor(
1398
68
                EntryExitInstrumenterPass(/*PostInlining=*/false)));
1399
68
          });
1400
68
      PB.registerOptimizerLastEPCallback(
1401
68
          [](ModulePassManager &MPM, OptimizationLevel Level) {
1402
68
            MPM.addPass(createModuleToFunctionPassAdaptor(
1403
68
                EntryExitInstrumenterPass(/*PostInlining=*/true)));
1404
68
          });
1405
68
    }
1406
1407
    // Register callbacks to schedule sanitizer passes at the appropriate part
1408
    // of the pipeline.
1409
19.0k
    if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds))
1410
5
      PB.registerScalarOptimizerLateEPCallback(
1411
5
          [](FunctionPassManager &FPM, OptimizationLevel Level) {
1412
5
            FPM.addPass(BoundsCheckingPass());
1413
5
          });
1414
1415
    // Don't add sanitizers if we are here from ThinLTO PostLink. That already
1416
    // done on PreLink stage.
1417
19.0k
    if (!IsThinLTOPostLink)
1418
19.0k
      addSanitizers(TargetTriple, CodeGenOpts, LangOpts, PB);
1419
1420
19.0k
    if (Optional<GCOVOptions> Options = getGCOVOptions(CodeGenOpts, LangOpts))
1421
15
      PB.registerPipelineStartEPCallback(
1422
15
          [Options](ModulePassManager &MPM, OptimizationLevel Level) {
1423
15
            MPM.addPass(GCOVProfilerPass(*Options));
1424
15
          });
1425
19.0k
    if (Optional<InstrProfOptions> Options =
1426
19.0k
            getInstrProfOptions(CodeGenOpts, LangOpts))
1427
116
      PB.registerPipelineStartEPCallback(
1428
116
          [Options](ModulePassManager &MPM, OptimizationLevel Level) {
1429
116
            MPM.addPass(InstrProfiling(*Options, false));
1430
116
          });
1431
1432
19.0k
    if (CodeGenOpts.OptimizationLevel == 0) {
1433
16.2k
      MPM = PB.buildO0DefaultPipeline(Level, IsLTO || 
IsThinLTO16.1k
);
1434
16.2k
    } else 
if (2.77k
IsThinLTO2.77k
) {
1435
31
      MPM = PB.buildThinLTOPreLinkDefaultPipeline(Level);
1436
2.74k
    } else if (IsLTO) {
1437
25
      MPM = PB.buildLTOPreLinkDefaultPipeline(Level);
1438
2.72k
    } else {
1439
2.72k
      MPM = PB.buildPerModuleDefaultPipeline(Level);
1440
2.72k
    }
1441
1442
19.0k
    if (!CodeGenOpts.MemoryProfileOutput.empty()) {
1443
4
      MPM.addPass(createModuleToFunctionPassAdaptor(MemProfilerPass()));
1444
4
      MPM.addPass(ModuleMemProfilerPass());
1445
4
    }
1446
19.0k
  }
1447
1448
  // Add a verifier pass if requested. We don't have to do this if the action
1449
  // requires code generation because there will already be a verifier pass in
1450
  // the code-generation pipeline.
1451
19.5k
  if (!actionRequiresCodeGen(Action) && 
CodeGenOpts.VerifyModule15.2k
)
1452
15.2k
    MPM.addPass(VerifierPass());
1453
1454
19.5k
  switch (Action) {
1455
546
  case Backend_EmitBC:
1456
546
    if (CodeGenOpts.PrepareForThinLTO && 
!CodeGenOpts.DisableLLVMPasses43
) {
1457
41
      if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
1458
2
        ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
1459
2
        if (!ThinLinkOS)
1460
0
          return;
1461
2
      }
1462
41
      if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
1463
40
        TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
1464
40
                                 CodeGenOpts.EnableSplitLTOUnit);
1465
41
      MPM.addPass(ThinLTOBitcodeWriterPass(*OS, ThinLinkOS ? 
&ThinLinkOS->os()2
1466
41
                                                           : 
nullptr39
));
1467
505
    } else {
1468
      // Emit a module summary by default for Regular LTO except for ld64
1469
      // targets
1470
505
      bool EmitLTOSummary =
1471
505
          (CodeGenOpts.PrepareForLTO && 
!CodeGenOpts.DisableLLVMPasses24
&&
1472
505
           llvm::Triple(TheModule->getTargetTriple()).getVendor() !=
1473
19
               llvm::Triple::Apple);
1474
505
      if (EmitLTOSummary) {
1475
15
        if (!TheModule->getModuleFlag("ThinLTO"))
1476
15
          TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
1477
15
        if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
1478
15
          TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
1479
15
                                   uint32_t(1));
1480
15
      }
1481
505
      MPM.addPass(
1482
505
          BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists, EmitLTOSummary));
1483
505
    }
1484
546
    break;
1485
1486
14.4k
  case Backend_EmitLL:
1487
14.4k
    MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
1488
14.4k
    break;
1489
1490
4.48k
  default:
1491
4.48k
    break;
1492
19.5k
  }
1493
1494
  // Now that we have all of the passes ready, run them.
1495
19.5k
  {
1496
19.5k
    PrettyStackTraceString CrashInfo("Optimizer");
1497
19.5k
    llvm::TimeTraceScope TimeScope("Optimizer");
1498
19.5k
    MPM.run(*TheModule, MAM);
1499
19.5k
  }
1500
19.5k
}
1501
1502
void EmitAssemblyHelper::RunCodegenPipeline(
1503
    BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS,
1504
19.5k
    std::unique_ptr<llvm::ToolOutputFile> &DwoOS) {
1505
  // We still use the legacy PM to run the codegen pipeline since the new PM
1506
  // does not work with the codegen pipeline.
1507
  // FIXME: make the new PM work with the codegen pipeline.
1508
19.5k
  legacy::PassManager CodeGenPasses;
1509
1510
  // Append any output we need to the pass manager.
1511
19.5k
  switch (Action) {
1512
477
  case Backend_EmitAssembly:
1513
484
  case Backend_EmitMCNull:
1514
4.25k
  case Backend_EmitObj:
1515
4.25k
    CodeGenPasses.add(
1516
4.25k
        createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
1517
4.25k
    if (!CodeGenOpts.SplitDwarfOutput.empty()) {
1518
6
      DwoOS = openOutputFile(CodeGenOpts.SplitDwarfOutput);
1519
6
      if (!DwoOS)
1520
0
        return;
1521
6
    }
1522
4.25k
    if (!AddEmitPasses(CodeGenPasses, Action, *OS,
1523
4.25k
                       DwoOS ? 
&DwoOS->os()6
:
nullptr4.25k
))
1524
      // FIXME: Should we handle this error differently?
1525
0
      return;
1526
4.25k
    break;
1527
15.2k
  default:
1528
15.2k
    return;
1529
19.5k
  }
1530
1531
4.25k
  {
1532
4.25k
    PrettyStackTraceString CrashInfo("Code generation");
1533
4.25k
    llvm::TimeTraceScope TimeScope("CodeGenPasses");
1534
4.25k
    CodeGenPasses.run(*TheModule);
1535
4.25k
  }
1536
4.25k
}
1537
1538
/// A clean version of `EmitAssembly` that uses the new pass manager.
1539
///
1540
/// Not all features are currently supported in this system, but where
1541
/// necessary it falls back to the legacy pass manager to at least provide
1542
/// basic functionality.
1543
///
1544
/// This API is planned to have its functionality finished and then to replace
1545
/// `EmitAssembly` at some point in the future when the default switches.
1546
void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
1547
19.5k
                                      std::unique_ptr<raw_pwrite_stream> OS) {
1548
19.5k
  TimeRegion Region(CodeGenOpts.TimePasses ? 
&CodeGenerationTime13
:
nullptr19.5k
);
1549
19.5k
  setCommandLineOpts(CodeGenOpts);
1550
1551
19.5k
  bool RequiresCodeGen = actionRequiresCodeGen(Action);
1552
19.5k
  CreateTargetMachine(RequiresCodeGen);
1553
1554
19.5k
  if (RequiresCodeGen && 
!TM4.25k
)
1555
1
    return;
1556
19.5k
  if (TM)
1557
15.9k
    TheModule->setDataLayout(TM->createDataLayout());
1558
1559
  // Before executing passes, print the final values of the LLVM options.
1560
19.5k
  cl::PrintOptionValues();
1561
1562
19.5k
  std::unique_ptr<llvm::ToolOutputFile> ThinLinkOS, DwoOS;
1563
19.5k
  RunOptimizationPipeline(Action, OS, ThinLinkOS);
1564
19.5k
  RunCodegenPipeline(Action, OS, DwoOS);
1565
1566
19.5k
  if (ThinLinkOS)
1567
2
    ThinLinkOS->keep();
1568
19.5k
  if (DwoOS)
1569
6
    DwoOS->keep();
1570
19.5k
}
1571
1572
static void runThinLTOBackend(
1573
    DiagnosticsEngine &Diags, ModuleSummaryIndex *CombinedIndex, Module *M,
1574
    const HeaderSearchOptions &HeaderOpts, const CodeGenOptions &CGOpts,
1575
    const clang::TargetOptions &TOpts, const LangOptions &LOpts,
1576
    std::unique_ptr<raw_pwrite_stream> OS, std::string SampleProfile,
1577
53
    std::string ProfileRemapping, BackendAction Action) {
1578
53
  StringMap<DenseMap<GlobalValue::GUID, GlobalValueSummary *>>
1579
53
      ModuleToDefinedGVSummaries;
1580
53
  CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
1581
1582
53
  setCommandLineOpts(CGOpts);
1583
1584
  // We can simply import the values mentioned in the combined index, since
1585
  // we should only invoke this using the individual indexes written out
1586
  // via a WriteIndexesThinBackend.
1587
53
  FunctionImporter::ImportMapTy ImportList;
1588
53
  if (!lto::initImportList(*M, *CombinedIndex, ImportList))
1589
0
    return;
1590
1591
53
  auto AddStream = [&](size_t Task) {
1592
26
    return std::make_unique<CachedFileStream>(std::move(OS),
1593
26
                                              CGOpts.ObjectFilenameForDebug);
1594
26
  };
1595
53
  lto::Config Conf;
1596
53
  if (CGOpts.SaveTempsFilePrefix != "") {
1597
5
    if (Error E = Conf.addSaveTemps(CGOpts.SaveTempsFilePrefix + ".",
1598
5
                                    /* UseInputModulePath */ false)) {
1599
0
      handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
1600
0
        errs() << "Error setting up ThinLTO save-temps: " << EIB.message()
1601
0
               << '\n';
1602
0
      });
1603
0
    }
1604
5
  }
1605
53
  Conf.CPU = TOpts.CPU;
1606
53
  Conf.CodeModel = getCodeModel(CGOpts);
1607
53
  Conf.MAttrs = TOpts.Features;
1608
53
  Conf.RelocModel = CGOpts.RelocationModel;
1609
53
  Conf.CGOptLevel = getCGOptLevel(CGOpts);
1610
53
  Conf.OptLevel = CGOpts.OptimizationLevel;
1611
53
  initTargetOptions(Diags, Conf.Options, CGOpts, TOpts, LOpts, HeaderOpts);
1612
53
  Conf.SampleProfile = std::move(SampleProfile);
1613
53
  Conf.PTO.LoopUnrolling = CGOpts.UnrollLoops;
1614
  // For historical reasons, loop interleaving is set to mirror setting for loop
1615
  // unrolling.
1616
53
  Conf.PTO.LoopInterleaving = CGOpts.UnrollLoops;
1617
53
  Conf.PTO.LoopVectorization = CGOpts.VectorizeLoop;
1618
53
  Conf.PTO.SLPVectorization = CGOpts.VectorizeSLP;
1619
  // Only enable CGProfilePass when using integrated assembler, since
1620
  // non-integrated assemblers don't recognize .cgprofile section.
1621
53
  Conf.PTO.CallGraphProfile = !CGOpts.DisableIntegratedAS;
1622
1623
  // Context sensitive profile.
1624
53
  if (CGOpts.hasProfileCSIRInstr()) {
1625
2
    Conf.RunCSIRInstr = true;
1626
2
    Conf.CSIRProfile = std::move(CGOpts.InstrProfileOutput);
1627
51
  } else if (CGOpts.hasProfileCSIRUse()) {
1628
3
    Conf.RunCSIRInstr = false;
1629
3
    Conf.CSIRProfile = std::move(CGOpts.ProfileInstrumentUsePath);
1630
3
  }
1631
1632
53
  Conf.ProfileRemapping = std::move(ProfileRemapping);
1633
53
  Conf.UseNewPM = !CGOpts.LegacyPassManager;
1634
53
  Conf.DebugPassManager = CGOpts.DebugPassManager;
1635
53
  Conf.RemarksWithHotness = CGOpts.DiagnosticsWithHotness;
1636
53
  Conf.RemarksFilename = CGOpts.OptRecordFile;
1637
53
  Conf.RemarksPasses = CGOpts.OptRecordPasses;
1638
53
  Conf.RemarksFormat = CGOpts.OptRecordFormat;
1639
53
  Conf.SplitDwarfFile = CGOpts.SplitDwarfFile;
1640
53
  Conf.SplitDwarfOutput = CGOpts.SplitDwarfOutput;
1641
53
  switch (Action) {
1642
0
  case Backend_EmitNothing:
1643
0
    Conf.PreCodeGenModuleHook = [](size_t Task, const Module &Mod) {
1644
0
      return false;
1645
0
    };
1646
0
    break;
1647
25
  case Backend_EmitLL:
1648
25
    Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1649
25
      M->print(*OS, nullptr, CGOpts.EmitLLVMUseLists);
1650
25
      return false;
1651
25
    };
1652
25
    break;
1653
1
  case Backend_EmitBC:
1654
1
    Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1655
1
      WriteBitcodeToFile(*M, *OS, CGOpts.EmitLLVMUseLists);
1656
1
      return false;
1657
1
    };
1658
1
    break;
1659
27
  default:
1660
27
    Conf.CGFileType = getCodeGenFileType(Action);
1661
27
    break;
1662
53
  }
1663
53
  if (Error E =
1664
53
          thinBackend(Conf, -1, AddStream, *M, *CombinedIndex, ImportList,
1665
53
                      ModuleToDefinedGVSummaries[M->getModuleIdentifier()],
1666
53
                      /* ModuleMap */ nullptr, CGOpts.CmdArgs)) {
1667
1
    handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
1668
1
      errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
1669
1
    });
1670
1
  }
1671
53
}
1672
1673
void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
1674
                              const HeaderSearchOptions &HeaderOpts,
1675
                              const CodeGenOptions &CGOpts,
1676
                              const clang::TargetOptions &TOpts,
1677
                              const LangOptions &LOpts,
1678
                              StringRef TDesc, Module *M,
1679
                              BackendAction Action,
1680
19.6k
                              std::unique_ptr<raw_pwrite_stream> OS) {
1681
1682
19.6k
  llvm::TimeTraceScope TimeScope("Backend");
1683
1684
19.6k
  std::unique_ptr<llvm::Module> EmptyModule;
1685
19.6k
  if (!CGOpts.ThinLTOIndexFile.empty()) {
1686
    // If we are performing a ThinLTO importing compile, load the function index
1687
    // into memory and pass it into runThinLTOBackend, which will run the
1688
    // function importer and invoke LTO passes.
1689
59
    std::unique_ptr<ModuleSummaryIndex> CombinedIndex;
1690
59
    if (Error E = llvm::getModuleSummaryIndexForFile(
1691
59
                      CGOpts.ThinLTOIndexFile,
1692
59
                      /*IgnoreEmptyThinLTOIndexFile*/ true)
1693
59
                      .moveInto(CombinedIndex)) {
1694
1
      logAllUnhandledErrors(std::move(E), errs(),
1695
1
                            "Error loading index file '" +
1696
1
                            CGOpts.ThinLTOIndexFile + "': ");
1697
1
      return;
1698
1
    }
1699
1700
    // A null CombinedIndex means we should skip ThinLTO compilation
1701
    // (LLVM will optionally ignore empty index files, returning null instead
1702
    // of an error).
1703
58
    if (CombinedIndex) {
1704
54
      if (!CombinedIndex->skipModuleByDistributedBackend()) {
1705
53
        runThinLTOBackend(Diags, CombinedIndex.get(), M, HeaderOpts, CGOpts,
1706
53
                          TOpts, LOpts, std::move(OS), CGOpts.SampleProfileFile,
1707
53
                          CGOpts.ProfileRemappingFile, Action);
1708
53
        return;
1709
53
      }
1710
      // Distributed indexing detected that nothing from the module is needed
1711
      // for the final linking. So we can skip the compilation. We sill need to
1712
      // output an empty object file to make sure that a linker does not fail
1713
      // trying to read it. Also for some features, like CFI, we must skip
1714
      // the compilation as CombinedIndex does not contain all required
1715
      // information.
1716
1
      EmptyModule = std::make_unique<llvm::Module>("empty", M->getContext());
1717
1
      EmptyModule->setTargetTriple(M->getTargetTriple());
1718
1
      M = EmptyModule.get();
1719
1
    }
1720
58
  }
1721
1722
19.5k
  EmitAssemblyHelper AsmHelper(Diags, HeaderOpts, CGOpts, TOpts, LOpts, M);
1723
1724
19.5k
  if (CGOpts.LegacyPassManager)
1725
59
    AsmHelper.EmitAssemblyWithLegacyPassManager(Action, std::move(OS));
1726
19.5k
  else
1727
19.5k
    AsmHelper.EmitAssembly(Action, std::move(OS));
1728
1729
  // Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
1730
  // DataLayout.
1731
19.5k
  if (AsmHelper.TM) {
1732
16.0k
    std::string DLDesc = M->getDataLayout().getStringRepresentation();
1733
16.0k
    if (DLDesc != TDesc) {
1734
0
      unsigned DiagID = Diags.getCustomDiagID(
1735
0
          DiagnosticsEngine::Error, "backend data layout '%0' does not match "
1736
0
                                    "expected target description '%1'");
1737
0
      Diags.Report(DiagID) << DLDesc << TDesc;
1738
0
    }
1739
16.0k
  }
1740
19.5k
}
1741
1742
// With -fembed-bitcode, save a copy of the llvm IR as data in the
1743
// __LLVM,__bitcode section.
1744
void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
1745
19.3k
                         llvm::MemoryBufferRef Buf) {
1746
19.3k
  if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off)
1747
19.3k
    return;
1748
22
  llvm::EmbedBitcodeInModule(
1749
22
      *M, Buf, CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker,
1750
22
      CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode,
1751
22
      CGOpts.CmdArgs);
1752
22
}