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