Coverage Report

Created: 2021-01-26 06:56

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