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