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 TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple,
252                                          const CodeGenOptions &CodeGenOpts) {
253   TargetLibraryInfoImpl *TLII = new TargetLibraryInfoImpl(TargetTriple);
254   if (!CodeGenOpts.SimplifyLibCalls)
255     TLII->disableAllFunctions();
256   else {
257     // Disable individual libc/libm calls in TargetLibraryInfo.
258     LibFunc::Func F;
259     for (auto &FuncName : CodeGenOpts.getNoBuiltinFuncs())
260       if (TLII->getLibFunc(FuncName, F))
261         TLII->setUnavailable(F);
262   }
263 
264   switch (CodeGenOpts.getVecLib()) {
265   case CodeGenOptions::Accelerate:
266     TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::Accelerate);
267     break;
268   default:
269     break;
270   }
271   return TLII;
272 }
273 
274 static void addSymbolRewriterPass(const CodeGenOptions &Opts,
275                                   legacy::PassManager *MPM) {
276   llvm::SymbolRewriter::RewriteDescriptorList DL;
277 
278   llvm::SymbolRewriter::RewriteMapParser MapParser;
279   for (const auto &MapFile : Opts.RewriteMapFiles)
280     MapParser.parse(MapFile, &DL);
281 
282   MPM->add(createRewriteSymbolsPass(DL));
283 }
284 
285 void EmitAssemblyHelper::CreatePasses(ModuleSummaryIndex *ModuleSummary) {
286   if (CodeGenOpts.DisableLLVMPasses)
287     return;
288 
289   unsigned OptLevel = CodeGenOpts.OptimizationLevel;
290   CodeGenOptions::InliningMethod Inlining = CodeGenOpts.getInlining();
291 
292   // Handle disabling of LLVM optimization, where we want to preserve the
293   // internal module before any optimization.
294   if (CodeGenOpts.DisableLLVMOpts) {
295     OptLevel = 0;
296     Inlining = CodeGenOpts.NoInlining;
297   }
298 
299   PassManagerBuilderWrapper PMBuilder(CodeGenOpts, LangOpts);
300 
301   // Figure out TargetLibraryInfo.
302   Triple TargetTriple(TheModule->getTargetTriple());
303   PMBuilder.LibraryInfo = createTLII(TargetTriple, CodeGenOpts);
304 
305   switch (Inlining) {
306   case CodeGenOptions::NoInlining:
307     break;
308   case CodeGenOptions::NormalInlining: {
309     PMBuilder.Inliner =
310         createFunctionInliningPass(OptLevel, CodeGenOpts.OptimizeSize);
311     break;
312   }
313   case CodeGenOptions::OnlyAlwaysInlining:
314     // Respect always_inline.
315     if (OptLevel == 0)
316       // Do not insert lifetime intrinsics at -O0.
317       PMBuilder.Inliner = createAlwaysInlinerPass(false);
318     else
319       PMBuilder.Inliner = createAlwaysInlinerPass();
320     break;
321   }
322 
323   PMBuilder.OptLevel = OptLevel;
324   PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize;
325   PMBuilder.BBVectorize = CodeGenOpts.VectorizeBB;
326   PMBuilder.SLPVectorize = CodeGenOpts.VectorizeSLP;
327   PMBuilder.LoopVectorize = CodeGenOpts.VectorizeLoop;
328 
329   PMBuilder.DisableUnitAtATime = !CodeGenOpts.UnitAtATime;
330   PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
331   PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions;
332   PMBuilder.PrepareForThinLTO = CodeGenOpts.EmitSummaryIndex;
333   PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
334   PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
335 
336   legacy::PassManager *MPM = getPerModulePasses();
337 
338   // If we are performing a ThinLTO importing compile, invoke the LTO
339   // pipeline and pass down the in-memory module summary index.
340   if (ModuleSummary) {
341     PMBuilder.ModuleSummary = ModuleSummary;
342     PMBuilder.populateThinLTOPassManager(*MPM);
343     return;
344   }
345 
346   PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
347                          addAddDiscriminatorsPass);
348 
349   // In ObjC ARC mode, add the main ARC optimization passes.
350   if (LangOpts.ObjCAutoRefCount) {
351     PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
352                            addObjCARCExpandPass);
353     PMBuilder.addExtension(PassManagerBuilder::EP_ModuleOptimizerEarly,
354                            addObjCARCAPElimPass);
355     PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
356                            addObjCARCOptPass);
357   }
358 
359   if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) {
360     PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
361                            addBoundsCheckingPass);
362     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
363                            addBoundsCheckingPass);
364   }
365 
366   if (CodeGenOpts.SanitizeCoverageType ||
367       CodeGenOpts.SanitizeCoverageIndirectCalls ||
368       CodeGenOpts.SanitizeCoverageTraceCmp) {
369     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
370                            addSanitizerCoveragePass);
371     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
372                            addSanitizerCoveragePass);
373   }
374 
375   if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
376     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
377                            addAddressSanitizerPasses);
378     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
379                            addAddressSanitizerPasses);
380   }
381 
382   if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) {
383     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
384                            addKernelAddressSanitizerPasses);
385     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
386                            addKernelAddressSanitizerPasses);
387   }
388 
389   if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
390     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
391                            addMemorySanitizerPass);
392     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
393                            addMemorySanitizerPass);
394   }
395 
396   if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
397     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
398                            addThreadSanitizerPass);
399     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
400                            addThreadSanitizerPass);
401   }
402 
403   if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
404     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
405                            addDataFlowSanitizerPass);
406     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
407                            addDataFlowSanitizerPass);
408   }
409 
410   // Set up the per-function pass manager.
411   legacy::FunctionPassManager *FPM = getPerFunctionPasses();
412   if (CodeGenOpts.VerifyModule)
413     FPM->add(createVerifierPass());
414   PMBuilder.populateFunctionPassManager(*FPM);
415 
416   // Set up the per-module pass manager.
417   if (!CodeGenOpts.RewriteMapFiles.empty())
418     addSymbolRewriterPass(CodeGenOpts, MPM);
419 
420   if (!CodeGenOpts.DisableGCov &&
421       (CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes)) {
422     // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if
423     // LLVM's -default-gcov-version flag is set to something invalid.
424     GCOVOptions Options;
425     Options.EmitNotes = CodeGenOpts.EmitGcovNotes;
426     Options.EmitData = CodeGenOpts.EmitGcovArcs;
427     memcpy(Options.Version, CodeGenOpts.CoverageVersion, 4);
428     Options.UseCfgChecksum = CodeGenOpts.CoverageExtraChecksum;
429     Options.NoRedZone = CodeGenOpts.DisableRedZone;
430     Options.FunctionNamesInData =
431         !CodeGenOpts.CoverageNoFunctionNamesInData;
432     Options.ExitBlockBeforeBody = CodeGenOpts.CoverageExitBlockBeforeBody;
433     MPM->add(createGCOVProfilerPass(Options));
434     if (CodeGenOpts.getDebugInfo() == codegenoptions::NoDebugInfo)
435       MPM->add(createStripSymbolsPass(true));
436   }
437 
438   if (CodeGenOpts.hasProfileClangInstr()) {
439     InstrProfOptions Options;
440     Options.NoRedZone = CodeGenOpts.DisableRedZone;
441     Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput;
442     MPM->add(createInstrProfilingPass(Options));
443   }
444   if (CodeGenOpts.hasProfileIRInstr()) {
445     if (!CodeGenOpts.InstrProfileOutput.empty())
446       PMBuilder.PGOInstrGen = CodeGenOpts.InstrProfileOutput;
447     else
448       PMBuilder.PGOInstrGen = "default.profraw";
449   }
450   if (CodeGenOpts.hasProfileIRUse())
451     PMBuilder.PGOInstrUse = CodeGenOpts.ProfileInstrumentUsePath;
452 
453   if (!CodeGenOpts.SampleProfileFile.empty())
454     MPM->add(createSampleProfileLoaderPass(CodeGenOpts.SampleProfileFile));
455 
456   PMBuilder.populateModulePassManager(*MPM);
457 }
458 
459 void EmitAssemblyHelper::setCommandLineOpts() {
460   SmallVector<const char *, 16> BackendArgs;
461   BackendArgs.push_back("clang"); // Fake program name.
462   if (!CodeGenOpts.DebugPass.empty()) {
463     BackendArgs.push_back("-debug-pass");
464     BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
465   }
466   if (!CodeGenOpts.LimitFloatPrecision.empty()) {
467     BackendArgs.push_back("-limit-float-precision");
468     BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
469   }
470   for (const std::string &BackendOption : CodeGenOpts.BackendOptions)
471     BackendArgs.push_back(BackendOption.c_str());
472   BackendArgs.push_back(nullptr);
473   llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
474                                     BackendArgs.data());
475 }
476 
477 TargetMachine *EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
478   // Create the TargetMachine for generating code.
479   std::string Error;
480   std::string Triple = TheModule->getTargetTriple();
481   const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
482   if (!TheTarget) {
483     if (MustCreateTM)
484       Diags.Report(diag::err_fe_unable_to_create_target) << Error;
485     return nullptr;
486   }
487 
488   unsigned CodeModel =
489     llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel)
490       .Case("small", llvm::CodeModel::Small)
491       .Case("kernel", llvm::CodeModel::Kernel)
492       .Case("medium", llvm::CodeModel::Medium)
493       .Case("large", llvm::CodeModel::Large)
494       .Case("default", llvm::CodeModel::Default)
495       .Default(~0u);
496   assert(CodeModel != ~0u && "invalid code model!");
497   llvm::CodeModel::Model CM = static_cast<llvm::CodeModel::Model>(CodeModel);
498 
499   std::string FeaturesStr =
500       llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ",");
501 
502   // Keep this synced with the equivalent code in tools/driver/cc1as_main.cpp.
503   llvm::Reloc::Model RM = llvm::Reloc::Default;
504   if (CodeGenOpts.RelocationModel == "static") {
505     RM = llvm::Reloc::Static;
506   } else if (CodeGenOpts.RelocationModel == "pic") {
507     RM = llvm::Reloc::PIC_;
508   } else {
509     assert(CodeGenOpts.RelocationModel == "dynamic-no-pic" &&
510            "Invalid PIC model!");
511     RM = llvm::Reloc::DynamicNoPIC;
512   }
513 
514   CodeGenOpt::Level OptLevel = CodeGenOpt::Default;
515   switch (CodeGenOpts.OptimizationLevel) {
516   default: break;
517   case 0: OptLevel = CodeGenOpt::None; break;
518   case 3: OptLevel = CodeGenOpt::Aggressive; break;
519   }
520 
521   llvm::TargetOptions Options;
522 
523   if (!TargetOpts.Reciprocals.empty())
524     Options.Reciprocals = TargetRecip(TargetOpts.Reciprocals);
525 
526   Options.ThreadModel =
527     llvm::StringSwitch<llvm::ThreadModel::Model>(CodeGenOpts.ThreadModel)
528       .Case("posix", llvm::ThreadModel::POSIX)
529       .Case("single", llvm::ThreadModel::Single);
530 
531   // Set float ABI type.
532   assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" ||
533           CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) &&
534          "Invalid Floating Point ABI!");
535   Options.FloatABIType =
536       llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI)
537           .Case("soft", llvm::FloatABI::Soft)
538           .Case("softfp", llvm::FloatABI::Soft)
539           .Case("hard", llvm::FloatABI::Hard)
540           .Default(llvm::FloatABI::Default);
541 
542   // Set FP fusion mode.
543   switch (CodeGenOpts.getFPContractMode()) {
544   case CodeGenOptions::FPC_Off:
545     Options.AllowFPOpFusion = llvm::FPOpFusion::Strict;
546     break;
547   case CodeGenOptions::FPC_On:
548     Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
549     break;
550   case CodeGenOptions::FPC_Fast:
551     Options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
552     break;
553   }
554 
555   Options.UseInitArray = CodeGenOpts.UseInitArray;
556   Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS;
557   Options.CompressDebugSections = CodeGenOpts.CompressDebugSections;
558 
559   // Set EABI version.
560   Options.EABIVersion = llvm::StringSwitch<llvm::EABI>(TargetOpts.EABIVersion)
561                             .Case("4", llvm::EABI::EABI4)
562                             .Case("5", llvm::EABI::EABI5)
563                             .Case("gnu", llvm::EABI::GNU)
564                             .Default(llvm::EABI::Default);
565 
566   Options.LessPreciseFPMADOption = CodeGenOpts.LessPreciseFPMAD;
567   Options.NoInfsFPMath = CodeGenOpts.NoInfsFPMath;
568   Options.NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath;
569   Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
570   Options.UnsafeFPMath = CodeGenOpts.UnsafeFPMath;
571   Options.StackAlignmentOverride = CodeGenOpts.StackAlignment;
572   Options.PositionIndependentExecutable = LangOpts.PIELevel != 0;
573   Options.FunctionSections = CodeGenOpts.FunctionSections;
574   Options.DataSections = CodeGenOpts.DataSections;
575   Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames;
576   Options.EmulatedTLS = CodeGenOpts.EmulatedTLS;
577   Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning();
578 
579   Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll;
580   Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels;
581   Options.MCOptions.MCUseDwarfDirectory = !CodeGenOpts.NoDwarfDirectoryAsm;
582   Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack;
583   Options.MCOptions.MCIncrementalLinkerCompatible =
584       CodeGenOpts.IncrementalLinkerCompatible;
585   Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings;
586   Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose;
587   Options.MCOptions.ABIName = TargetOpts.ABI;
588 
589   TargetMachine *TM = TheTarget->createTargetMachine(Triple, TargetOpts.CPU,
590                                                      FeaturesStr, Options,
591                                                      RM, CM, OptLevel);
592 
593   return TM;
594 }
595 
596 bool EmitAssemblyHelper::AddEmitPasses(BackendAction Action,
597                                        raw_pwrite_stream &OS) {
598 
599   // Create the code generator passes.
600   legacy::PassManager *PM = getCodeGenPasses();
601 
602   // Add LibraryInfo.
603   llvm::Triple TargetTriple(TheModule->getTargetTriple());
604   std::unique_ptr<TargetLibraryInfoImpl> TLII(
605       createTLII(TargetTriple, CodeGenOpts));
606   PM->add(new TargetLibraryInfoWrapperPass(*TLII));
607 
608   // Normal mode, emit a .s or .o file by running the code generator. Note,
609   // this also adds codegenerator level optimization passes.
610   TargetMachine::CodeGenFileType CGFT = TargetMachine::CGFT_AssemblyFile;
611   if (Action == Backend_EmitObj)
612     CGFT = TargetMachine::CGFT_ObjectFile;
613   else if (Action == Backend_EmitMCNull)
614     CGFT = TargetMachine::CGFT_Null;
615   else
616     assert(Action == Backend_EmitAssembly && "Invalid action!");
617 
618   // Add ObjC ARC final-cleanup optimizations. This is done as part of the
619   // "codegen" passes so that it isn't run multiple times when there is
620   // inlining happening.
621   if (CodeGenOpts.OptimizationLevel > 0)
622     PM->add(createObjCARCContractPass());
623 
624   if (TM->addPassesToEmitFile(*PM, OS, CGFT,
625                               /*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
626     Diags.Report(diag::err_fe_unable_to_interface_with_target);
627     return false;
628   }
629 
630   return true;
631 }
632 
633 void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
634                                       raw_pwrite_stream *OS) {
635   TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr);
636 
637   setCommandLineOpts();
638 
639   bool UsesCodeGen = (Action != Backend_EmitNothing &&
640                       Action != Backend_EmitBC &&
641                       Action != Backend_EmitLL);
642   if (!TM)
643     TM.reset(CreateTargetMachine(UsesCodeGen));
644 
645   if (UsesCodeGen && !TM)
646     return;
647   if (TM)
648     TheModule->setDataLayout(TM->createDataLayout());
649 
650   // If we are performing a ThinLTO importing compile, load the function
651   // index into memory and pass it into CreatePasses, which will add it
652   // to the PassManagerBuilder and invoke LTO passes.
653   std::unique_ptr<ModuleSummaryIndex> ModuleSummary;
654   if (!CodeGenOpts.ThinLTOIndexFile.empty()) {
655     ErrorOr<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr =
656         llvm::getModuleSummaryIndexForFile(
657             CodeGenOpts.ThinLTOIndexFile, [&](const DiagnosticInfo &DI) {
658               TheModule->getContext().diagnose(DI);
659             });
660     if (std::error_code EC = IndexOrErr.getError()) {
661       std::string Error = EC.message();
662       errs() << "Error loading index file '" << CodeGenOpts.ThinLTOIndexFile
663              << "': " << Error << "\n";
664       return;
665     }
666     ModuleSummary = std::move(IndexOrErr.get());
667     assert(ModuleSummary && "Expected non-empty module summary index");
668   }
669 
670   CreatePasses(ModuleSummary.get());
671 
672   switch (Action) {
673   case Backend_EmitNothing:
674     break;
675 
676   case Backend_EmitBC:
677     getPerModulePasses()->add(createBitcodeWriterPass(
678         *OS, CodeGenOpts.EmitLLVMUseLists, CodeGenOpts.EmitSummaryIndex,
679         CodeGenOpts.EmitSummaryIndex));
680     break;
681 
682   case Backend_EmitLL:
683     getPerModulePasses()->add(
684         createPrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
685     break;
686 
687   default:
688     if (!AddEmitPasses(Action, *OS))
689       return;
690   }
691 
692   // Before executing passes, print the final values of the LLVM options.
693   cl::PrintOptionValues();
694 
695   // Run passes. For now we do all passes at once, but eventually we
696   // would like to have the option of streaming code generation.
697 
698   if (PerFunctionPasses) {
699     PrettyStackTraceString CrashInfo("Per-function optimization");
700 
701     PerFunctionPasses->doInitialization();
702     for (Function &F : *TheModule)
703       if (!F.isDeclaration())
704         PerFunctionPasses->run(F);
705     PerFunctionPasses->doFinalization();
706   }
707 
708   if (PerModulePasses) {
709     PrettyStackTraceString CrashInfo("Per-module optimization passes");
710     PerModulePasses->run(*TheModule);
711   }
712 
713   if (CodeGenPasses) {
714     PrettyStackTraceString CrashInfo("Code generation");
715     CodeGenPasses->run(*TheModule);
716   }
717 }
718 
719 void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
720                               const CodeGenOptions &CGOpts,
721                               const clang::TargetOptions &TOpts,
722                               const LangOptions &LOpts, const llvm::DataLayout &TDesc,
723                               Module *M, BackendAction Action,
724                               raw_pwrite_stream *OS) {
725   EmitAssemblyHelper AsmHelper(Diags, CGOpts, TOpts, LOpts, M);
726 
727   AsmHelper.EmitAssembly(Action, OS);
728 
729   // Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
730   // DataLayout.
731   if (AsmHelper.TM) {
732     std::string DLDesc = M->getDataLayout().getStringRepresentation();
733     if (DLDesc != TDesc.getStringRepresentation()) {
734       unsigned DiagID = Diags.getCustomDiagID(
735           DiagnosticsEngine::Error, "backend data layout '%0' does not match "
736                                     "expected target description '%1'");
737       Diags.Report(DiagID) << DLDesc << TDesc.getStringRepresentation();
738     }
739   }
740 }
741