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