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