Coverage Report

Created: 2021-08-24 07:12

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