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/ADT/Triple.h"
20 #include "llvm/Analysis/TargetLibraryInfo.h"
21 #include "llvm/Analysis/TargetTransformInfo.h"
22 #include "llvm/Bitcode/BitcodeWriterPass.h"
23 #include "llvm/Bitcode/ReaderWriter.h"
24 #include "llvm/CodeGen/RegAllocRegistry.h"
25 #include "llvm/CodeGen/SchedulerRegistry.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/ModuleSummaryIndex.h"
28 #include "llvm/IR/IRPrintingPasses.h"
29 #include "llvm/IR/LegacyPassManager.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/IR/Verifier.h"
32 #include "llvm/LTO/LTOBackend.h"
33 #include "llvm/MC/SubtargetFeature.h"
34 #include "llvm/Object/ModuleSummaryIndexObjectFile.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/MemoryBuffer.h"
37 #include "llvm/Support/PrettyStackTrace.h"
38 #include "llvm/Support/TargetRegistry.h"
39 #include "llvm/Support/Timer.h"
40 #include "llvm/Support/raw_ostream.h"
41 #include "llvm/Target/TargetMachine.h"
42 #include "llvm/Target/TargetOptions.h"
43 #include "llvm/Target/TargetSubtargetInfo.h"
44 #include "llvm/Transforms/IPO.h"
45 #include "llvm/Transforms/IPO/AlwaysInliner.h"
46 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
47 #include "llvm/Transforms/Instrumentation.h"
48 #include "llvm/Transforms/ObjCARC.h"
49 #include "llvm/Transforms/Scalar.h"
50 #include "llvm/Transforms/Scalar/GVN.h"
51 #include "llvm/Transforms/Utils/SymbolRewriter.h"
52 #include <memory>
53 using namespace clang;
54 using namespace llvm;
55 
56 namespace {
57 
58 class EmitAssemblyHelper {
59   DiagnosticsEngine &Diags;
60   const CodeGenOptions &CodeGenOpts;
61   const clang::TargetOptions &TargetOpts;
62   const LangOptions &LangOpts;
63   Module *TheModule;
64 
65   Timer CodeGenerationTime;
66 
67   std::unique_ptr<raw_pwrite_stream> OS;
68 
69 private:
70   TargetIRAnalysis getTargetIRAnalysis() const {
71     if (TM)
72       return TM->getTargetIRAnalysis();
73 
74     return TargetIRAnalysis();
75   }
76 
77   /// Set LLVM command line options passed through -backend-option.
78   void setCommandLineOpts();
79 
80   void CreatePasses(legacy::PassManager &MPM, legacy::FunctionPassManager &FPM);
81 
82   /// Generates the TargetMachine.
83   /// Leaves TM unchanged if it is unable to create the target machine.
84   /// Some of our clang tests specify triples which are not built
85   /// into clang. This is okay because these tests check the generated
86   /// IR, and they require DataLayout which depends on the triple.
87   /// In this case, we allow this method to fail and not report an error.
88   /// When MustCreateTM is used, we print an error if we are unable to load
89   /// the requested target.
90   void CreateTargetMachine(bool MustCreateTM);
91 
92   /// Add passes necessary to emit assembly or LLVM IR.
93   ///
94   /// \return True on success.
95   bool AddEmitPasses(legacy::PassManager &CodeGenPasses, BackendAction Action,
96                      raw_pwrite_stream &OS);
97 
98 public:
99   EmitAssemblyHelper(DiagnosticsEngine &_Diags, const CodeGenOptions &CGOpts,
100                      const clang::TargetOptions &TOpts,
101                      const LangOptions &LOpts, Module *M)
102       : Diags(_Diags), CodeGenOpts(CGOpts), TargetOpts(TOpts), LangOpts(LOpts),
103         TheModule(M), CodeGenerationTime("Code Generation Time") {}
104 
105   ~EmitAssemblyHelper() {
106     if (CodeGenOpts.DisableFree)
107       BuryPointer(std::move(TM));
108   }
109 
110   std::unique_ptr<TargetMachine> TM;
111 
112   void EmitAssembly(BackendAction Action,
113                     std::unique_ptr<raw_pwrite_stream> OS);
114 };
115 
116 // We need this wrapper to access LangOpts and CGOpts from extension functions
117 // that we add to the PassManagerBuilder.
118 class PassManagerBuilderWrapper : public PassManagerBuilder {
119 public:
120   PassManagerBuilderWrapper(const CodeGenOptions &CGOpts,
121                             const LangOptions &LangOpts)
122       : PassManagerBuilder(), CGOpts(CGOpts), LangOpts(LangOpts) {}
123   const CodeGenOptions &getCGOpts() const { return CGOpts; }
124   const LangOptions &getLangOpts() const { return LangOpts; }
125 private:
126   const CodeGenOptions &CGOpts;
127   const LangOptions &LangOpts;
128 };
129 
130 }
131 
132 static void addObjCARCAPElimPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
133   if (Builder.OptLevel > 0)
134     PM.add(createObjCARCAPElimPass());
135 }
136 
137 static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
138   if (Builder.OptLevel > 0)
139     PM.add(createObjCARCExpandPass());
140 }
141 
142 static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
143   if (Builder.OptLevel > 0)
144     PM.add(createObjCARCOptPass());
145 }
146 
147 static void addAddDiscriminatorsPass(const PassManagerBuilder &Builder,
148                                      legacy::PassManagerBase &PM) {
149   PM.add(createAddDiscriminatorsPass());
150 }
151 
152 static void addCleanupPassesForSampleProfiler(
153     const PassManagerBuilder &Builder, legacy::PassManagerBase &PM) {
154   // instcombine is needed before sample profile annotation because it converts
155   // certain function calls to be inlinable. simplifycfg and sroa are needed
156   // before instcombine for necessary preparation. E.g. load store is eliminated
157   // properly so that instcombine will not introduce unecessary liverange.
158   PM.add(createCFGSimplificationPass());
159   PM.add(createSROAPass());
160   PM.add(createInstructionCombiningPass());
161 }
162 
163 static void addBoundsCheckingPass(const PassManagerBuilder &Builder,
164                                   legacy::PassManagerBase &PM) {
165   PM.add(createBoundsCheckingPass());
166 }
167 
168 static void addSanitizerCoveragePass(const PassManagerBuilder &Builder,
169                                      legacy::PassManagerBase &PM) {
170   const PassManagerBuilderWrapper &BuilderWrapper =
171       static_cast<const PassManagerBuilderWrapper&>(Builder);
172   const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
173   SanitizerCoverageOptions Opts;
174   Opts.CoverageType =
175       static_cast<SanitizerCoverageOptions::Type>(CGOpts.SanitizeCoverageType);
176   Opts.IndirectCalls = CGOpts.SanitizeCoverageIndirectCalls;
177   Opts.TraceBB = CGOpts.SanitizeCoverageTraceBB;
178   Opts.TraceCmp = CGOpts.SanitizeCoverageTraceCmp;
179   Opts.Use8bitCounters = CGOpts.SanitizeCoverage8bitCounters;
180   Opts.TracePC = CGOpts.SanitizeCoverageTracePC;
181   PM.add(createSanitizerCoverageModulePass(Opts));
182 }
183 
184 static void addAddressSanitizerPasses(const PassManagerBuilder &Builder,
185                                       legacy::PassManagerBase &PM) {
186   const PassManagerBuilderWrapper &BuilderWrapper =
187       static_cast<const PassManagerBuilderWrapper&>(Builder);
188   const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
189   bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Address);
190   bool UseAfterScope = CGOpts.SanitizeAddressUseAfterScope;
191   PM.add(createAddressSanitizerFunctionPass(/*CompileKernel*/ false, Recover,
192                                             UseAfterScope));
193   PM.add(createAddressSanitizerModulePass(/*CompileKernel*/false, Recover));
194 }
195 
196 static void addKernelAddressSanitizerPasses(const PassManagerBuilder &Builder,
197                                             legacy::PassManagerBase &PM) {
198   PM.add(createAddressSanitizerFunctionPass(
199       /*CompileKernel*/ true,
200       /*Recover*/ true, /*UseAfterScope*/ false));
201   PM.add(createAddressSanitizerModulePass(/*CompileKernel*/true,
202                                           /*Recover*/true));
203 }
204 
205 static void addMemorySanitizerPass(const PassManagerBuilder &Builder,
206                                    legacy::PassManagerBase &PM) {
207   const PassManagerBuilderWrapper &BuilderWrapper =
208       static_cast<const PassManagerBuilderWrapper&>(Builder);
209   const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
210   PM.add(createMemorySanitizerPass(CGOpts.SanitizeMemoryTrackOrigins));
211 
212   // MemorySanitizer inserts complex instrumentation that mostly follows
213   // the logic of the original code, but operates on "shadow" values.
214   // It can benefit from re-running some general purpose optimization passes.
215   if (Builder.OptLevel > 0) {
216     PM.add(createEarlyCSEPass());
217     PM.add(createReassociatePass());
218     PM.add(createLICMPass());
219     PM.add(createGVNPass());
220     PM.add(createInstructionCombiningPass());
221     PM.add(createDeadStoreEliminationPass());
222   }
223 }
224 
225 static void addThreadSanitizerPass(const PassManagerBuilder &Builder,
226                                    legacy::PassManagerBase &PM) {
227   PM.add(createThreadSanitizerPass());
228 }
229 
230 static void addDataFlowSanitizerPass(const PassManagerBuilder &Builder,
231                                      legacy::PassManagerBase &PM) {
232   const PassManagerBuilderWrapper &BuilderWrapper =
233       static_cast<const PassManagerBuilderWrapper&>(Builder);
234   const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
235   PM.add(createDataFlowSanitizerPass(LangOpts.SanitizerBlacklistFiles));
236 }
237 
238 static void addEfficiencySanitizerPass(const PassManagerBuilder &Builder,
239                                        legacy::PassManagerBase &PM) {
240   const PassManagerBuilderWrapper &BuilderWrapper =
241       static_cast<const PassManagerBuilderWrapper&>(Builder);
242   const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
243   EfficiencySanitizerOptions Opts;
244   if (LangOpts.Sanitize.has(SanitizerKind::EfficiencyCacheFrag))
245     Opts.ToolType = EfficiencySanitizerOptions::ESAN_CacheFrag;
246   else if (LangOpts.Sanitize.has(SanitizerKind::EfficiencyWorkingSet))
247     Opts.ToolType = EfficiencySanitizerOptions::ESAN_WorkingSet;
248   PM.add(createEfficiencySanitizerPass(Opts));
249 }
250 
251 static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple,
252                                          const CodeGenOptions &CodeGenOpts) {
253   TargetLibraryInfoImpl *TLII = new TargetLibraryInfoImpl(TargetTriple);
254   if (!CodeGenOpts.SimplifyLibCalls)
255     TLII->disableAllFunctions();
256   else {
257     // Disable individual libc/libm calls in TargetLibraryInfo.
258     LibFunc::Func F;
259     for (auto &FuncName : CodeGenOpts.getNoBuiltinFuncs())
260       if (TLII->getLibFunc(FuncName, F))
261         TLII->setUnavailable(F);
262   }
263 
264   switch (CodeGenOpts.getVecLib()) {
265   case CodeGenOptions::Accelerate:
266     TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::Accelerate);
267     break;
268   case CodeGenOptions::SVML:
269     TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::SVML);
270     break;
271   default:
272     break;
273   }
274   return TLII;
275 }
276 
277 static void addSymbolRewriterPass(const CodeGenOptions &Opts,
278                                   legacy::PassManager *MPM) {
279   llvm::SymbolRewriter::RewriteDescriptorList DL;
280 
281   llvm::SymbolRewriter::RewriteMapParser MapParser;
282   for (const auto &MapFile : Opts.RewriteMapFiles)
283     MapParser.parse(MapFile, &DL);
284 
285   MPM->add(createRewriteSymbolsPass(DL));
286 }
287 
288 void EmitAssemblyHelper::CreatePasses(legacy::PassManager &MPM,
289                                       legacy::FunctionPassManager &FPM) {
290   if (CodeGenOpts.DisableLLVMPasses)
291     return;
292 
293   unsigned OptLevel = CodeGenOpts.OptimizationLevel;
294   CodeGenOptions::InliningMethod Inlining = CodeGenOpts.getInlining();
295 
296   // Handle disabling of LLVM optimization, where we want to preserve the
297   // internal module before any optimization.
298   if (CodeGenOpts.DisableLLVMOpts) {
299     OptLevel = 0;
300     Inlining = CodeGenOpts.NoInlining;
301   }
302 
303   PassManagerBuilderWrapper PMBuilder(CodeGenOpts, LangOpts);
304 
305   // Figure out TargetLibraryInfo.
306   Triple TargetTriple(TheModule->getTargetTriple());
307   PMBuilder.LibraryInfo = createTLII(TargetTriple, CodeGenOpts);
308 
309   switch (Inlining) {
310   case CodeGenOptions::NoInlining:
311     break;
312   case CodeGenOptions::NormalInlining:
313   case CodeGenOptions::OnlyHintInlining: {
314     PMBuilder.Inliner =
315         createFunctionInliningPass(OptLevel, CodeGenOpts.OptimizeSize);
316     break;
317   }
318   case CodeGenOptions::OnlyAlwaysInlining:
319     // Respect always_inline.
320     if (OptLevel == 0)
321       // Do not insert lifetime intrinsics at -O0.
322       PMBuilder.Inliner = createAlwaysInlinerLegacyPass(false);
323     else
324       PMBuilder.Inliner = createAlwaysInlinerLegacyPass();
325     break;
326   }
327 
328   PMBuilder.OptLevel = OptLevel;
329   PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize;
330   PMBuilder.BBVectorize = CodeGenOpts.VectorizeBB;
331   PMBuilder.SLPVectorize = CodeGenOpts.VectorizeSLP;
332   PMBuilder.LoopVectorize = CodeGenOpts.VectorizeLoop;
333 
334   PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
335   PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions;
336   PMBuilder.PrepareForThinLTO = CodeGenOpts.EmitSummaryIndex;
337   PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
338   PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
339 
340   // Add target-specific passes that need to run as early as possible.
341   if (TM)
342     PMBuilder.addExtension(
343         PassManagerBuilder::EP_EarlyAsPossible,
344         [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
345           TM->addEarlyAsPossiblePasses(PM);
346         });
347 
348   PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
349                          addAddDiscriminatorsPass);
350 
351   // In ObjC ARC mode, add the main ARC optimization passes.
352   if (LangOpts.ObjCAutoRefCount) {
353     PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
354                            addObjCARCExpandPass);
355     PMBuilder.addExtension(PassManagerBuilder::EP_ModuleOptimizerEarly,
356                            addObjCARCAPElimPass);
357     PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
358                            addObjCARCOptPass);
359   }
360 
361   if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) {
362     PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
363                            addBoundsCheckingPass);
364     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
365                            addBoundsCheckingPass);
366   }
367 
368   if (CodeGenOpts.SanitizeCoverageType ||
369       CodeGenOpts.SanitizeCoverageIndirectCalls ||
370       CodeGenOpts.SanitizeCoverageTraceCmp) {
371     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
372                            addSanitizerCoveragePass);
373     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
374                            addSanitizerCoveragePass);
375   }
376 
377   if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
378     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
379                            addAddressSanitizerPasses);
380     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
381                            addAddressSanitizerPasses);
382   }
383 
384   if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) {
385     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
386                            addKernelAddressSanitizerPasses);
387     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
388                            addKernelAddressSanitizerPasses);
389   }
390 
391   if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
392     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
393                            addMemorySanitizerPass);
394     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
395                            addMemorySanitizerPass);
396   }
397 
398   if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
399     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
400                            addThreadSanitizerPass);
401     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
402                            addThreadSanitizerPass);
403   }
404 
405   if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
406     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
407                            addDataFlowSanitizerPass);
408     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
409                            addDataFlowSanitizerPass);
410   }
411 
412   if (LangOpts.Sanitize.hasOneOf(SanitizerKind::Efficiency)) {
413     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
414                            addEfficiencySanitizerPass);
415     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
416                            addEfficiencySanitizerPass);
417   }
418 
419   // Set up the per-function pass manager.
420   if (CodeGenOpts.VerifyModule)
421     FPM.add(createVerifierPass());
422 
423   // Set up the per-module pass manager.
424   if (!CodeGenOpts.RewriteMapFiles.empty())
425     addSymbolRewriterPass(CodeGenOpts, &MPM);
426 
427   if (!CodeGenOpts.DisableGCov &&
428       (CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes)) {
429     // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if
430     // LLVM's -default-gcov-version flag is set to something invalid.
431     GCOVOptions Options;
432     Options.EmitNotes = CodeGenOpts.EmitGcovNotes;
433     Options.EmitData = CodeGenOpts.EmitGcovArcs;
434     memcpy(Options.Version, CodeGenOpts.CoverageVersion, 4);
435     Options.UseCfgChecksum = CodeGenOpts.CoverageExtraChecksum;
436     Options.NoRedZone = CodeGenOpts.DisableRedZone;
437     Options.FunctionNamesInData =
438         !CodeGenOpts.CoverageNoFunctionNamesInData;
439     Options.ExitBlockBeforeBody = CodeGenOpts.CoverageExitBlockBeforeBody;
440     MPM.add(createGCOVProfilerPass(Options));
441     if (CodeGenOpts.getDebugInfo() == codegenoptions::NoDebugInfo)
442       MPM.add(createStripSymbolsPass(true));
443   }
444 
445   if (CodeGenOpts.hasProfileClangInstr()) {
446     InstrProfOptions Options;
447     Options.NoRedZone = CodeGenOpts.DisableRedZone;
448     Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput;
449     MPM.add(createInstrProfilingLegacyPass(Options));
450   }
451   if (CodeGenOpts.hasProfileIRInstr()) {
452     PMBuilder.EnablePGOInstrGen = true;
453     if (!CodeGenOpts.InstrProfileOutput.empty())
454       PMBuilder.PGOInstrGen = CodeGenOpts.InstrProfileOutput;
455     else
456       PMBuilder.PGOInstrGen = "default_%m.profraw";
457   }
458   if (CodeGenOpts.hasProfileIRUse())
459     PMBuilder.PGOInstrUse = CodeGenOpts.ProfileInstrumentUsePath;
460 
461   if (!CodeGenOpts.SampleProfileFile.empty()) {
462     MPM.add(createPruneEHPass());
463     MPM.add(createSampleProfileLoaderPass(CodeGenOpts.SampleProfileFile));
464     PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
465                            addCleanupPassesForSampleProfiler);
466   }
467 
468   PMBuilder.populateFunctionPassManager(FPM);
469   PMBuilder.populateModulePassManager(MPM);
470 }
471 
472 void EmitAssemblyHelper::setCommandLineOpts() {
473   SmallVector<const char *, 16> BackendArgs;
474   BackendArgs.push_back("clang"); // Fake program name.
475   if (!CodeGenOpts.DebugPass.empty()) {
476     BackendArgs.push_back("-debug-pass");
477     BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
478   }
479   if (!CodeGenOpts.LimitFloatPrecision.empty()) {
480     BackendArgs.push_back("-limit-float-precision");
481     BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
482   }
483   for (const std::string &BackendOption : CodeGenOpts.BackendOptions)
484     BackendArgs.push_back(BackendOption.c_str());
485   BackendArgs.push_back(nullptr);
486   llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
487                                     BackendArgs.data());
488 }
489 
490 void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
491   // Create the TargetMachine for generating code.
492   std::string Error;
493   std::string Triple = TheModule->getTargetTriple();
494   const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
495   if (!TheTarget) {
496     if (MustCreateTM)
497       Diags.Report(diag::err_fe_unable_to_create_target) << Error;
498     return;
499   }
500 
501   unsigned CodeModel =
502     llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel)
503       .Case("small", llvm::CodeModel::Small)
504       .Case("kernel", llvm::CodeModel::Kernel)
505       .Case("medium", llvm::CodeModel::Medium)
506       .Case("large", llvm::CodeModel::Large)
507       .Case("default", llvm::CodeModel::Default)
508       .Default(~0u);
509   assert(CodeModel != ~0u && "invalid code model!");
510   llvm::CodeModel::Model CM = static_cast<llvm::CodeModel::Model>(CodeModel);
511 
512   std::string FeaturesStr =
513       llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ",");
514 
515   // Keep this synced with the equivalent code in tools/driver/cc1as_main.cpp.
516   llvm::Optional<llvm::Reloc::Model> RM;
517   if (CodeGenOpts.RelocationModel == "static") {
518     RM = llvm::Reloc::Static;
519   } else if (CodeGenOpts.RelocationModel == "pic") {
520     RM = llvm::Reloc::PIC_;
521   } else if (CodeGenOpts.RelocationModel == "ropi") {
522     RM = llvm::Reloc::ROPI;
523   } else if (CodeGenOpts.RelocationModel == "rwpi") {
524     RM = llvm::Reloc::RWPI;
525   } else if (CodeGenOpts.RelocationModel == "ropi-rwpi") {
526     RM = llvm::Reloc::ROPI_RWPI;
527   } else {
528     assert(CodeGenOpts.RelocationModel == "dynamic-no-pic" &&
529            "Invalid PIC model!");
530     RM = llvm::Reloc::DynamicNoPIC;
531   }
532 
533   CodeGenOpt::Level OptLevel = CodeGenOpt::Default;
534   switch (CodeGenOpts.OptimizationLevel) {
535   default: break;
536   case 0: OptLevel = CodeGenOpt::None; break;
537   case 3: OptLevel = CodeGenOpt::Aggressive; break;
538   }
539 
540   llvm::TargetOptions Options;
541 
542   if (!TargetOpts.Reciprocals.empty())
543     Options.Reciprocals = TargetRecip(TargetOpts.Reciprocals);
544 
545   Options.ThreadModel =
546     llvm::StringSwitch<llvm::ThreadModel::Model>(CodeGenOpts.ThreadModel)
547       .Case("posix", llvm::ThreadModel::POSIX)
548       .Case("single", llvm::ThreadModel::Single);
549 
550   // Set float ABI type.
551   assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" ||
552           CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) &&
553          "Invalid Floating Point ABI!");
554   Options.FloatABIType =
555       llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI)
556           .Case("soft", llvm::FloatABI::Soft)
557           .Case("softfp", llvm::FloatABI::Soft)
558           .Case("hard", llvm::FloatABI::Hard)
559           .Default(llvm::FloatABI::Default);
560 
561   // Set FP fusion mode.
562   switch (CodeGenOpts.getFPContractMode()) {
563   case CodeGenOptions::FPC_Off:
564     Options.AllowFPOpFusion = llvm::FPOpFusion::Strict;
565     break;
566   case CodeGenOptions::FPC_On:
567     Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
568     break;
569   case CodeGenOptions::FPC_Fast:
570     Options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
571     break;
572   }
573 
574   Options.UseInitArray = CodeGenOpts.UseInitArray;
575   Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS;
576   Options.CompressDebugSections = CodeGenOpts.CompressDebugSections;
577   Options.RelaxELFRelocations = CodeGenOpts.RelaxELFRelocations;
578 
579   // Set EABI version.
580   Options.EABIVersion = llvm::StringSwitch<llvm::EABI>(TargetOpts.EABIVersion)
581                             .Case("4", llvm::EABI::EABI4)
582                             .Case("5", llvm::EABI::EABI5)
583                             .Case("gnu", llvm::EABI::GNU)
584                             .Default(llvm::EABI::Default);
585 
586   if (LangOpts.SjLjExceptions)
587     Options.ExceptionModel = llvm::ExceptionHandling::SjLj;
588 
589   Options.LessPreciseFPMADOption = CodeGenOpts.LessPreciseFPMAD;
590   Options.NoInfsFPMath = CodeGenOpts.NoInfsFPMath;
591   Options.NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath;
592   Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
593   Options.UnsafeFPMath = CodeGenOpts.UnsafeFPMath;
594   Options.StackAlignmentOverride = CodeGenOpts.StackAlignment;
595   Options.FunctionSections = CodeGenOpts.FunctionSections;
596   Options.DataSections = CodeGenOpts.DataSections;
597   Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames;
598   Options.EmulatedTLS = CodeGenOpts.EmulatedTLS;
599   Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning();
600 
601   Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll;
602   Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels;
603   Options.MCOptions.MCUseDwarfDirectory = !CodeGenOpts.NoDwarfDirectoryAsm;
604   Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack;
605   Options.MCOptions.MCIncrementalLinkerCompatible =
606       CodeGenOpts.IncrementalLinkerCompatible;
607   Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings;
608   Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose;
609   Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments;
610   Options.MCOptions.ABIName = TargetOpts.ABI;
611 
612   TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr,
613                                           Options, RM, CM, OptLevel));
614 }
615 
616 bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses,
617                                        BackendAction Action,
618                                        raw_pwrite_stream &OS) {
619   // Add LibraryInfo.
620   llvm::Triple TargetTriple(TheModule->getTargetTriple());
621   std::unique_ptr<TargetLibraryInfoImpl> TLII(
622       createTLII(TargetTriple, CodeGenOpts));
623   CodeGenPasses.add(new TargetLibraryInfoWrapperPass(*TLII));
624 
625   // Normal mode, emit a .s or .o file by running the code generator. Note,
626   // this also adds codegenerator level optimization passes.
627   TargetMachine::CodeGenFileType CGFT = TargetMachine::CGFT_AssemblyFile;
628   if (Action == Backend_EmitObj)
629     CGFT = TargetMachine::CGFT_ObjectFile;
630   else if (Action == Backend_EmitMCNull)
631     CGFT = TargetMachine::CGFT_Null;
632   else
633     assert(Action == Backend_EmitAssembly && "Invalid action!");
634 
635   // Add ObjC ARC final-cleanup optimizations. This is done as part of the
636   // "codegen" passes so that it isn't run multiple times when there is
637   // inlining happening.
638   if (CodeGenOpts.OptimizationLevel > 0)
639     CodeGenPasses.add(createObjCARCContractPass());
640 
641   if (TM->addPassesToEmitFile(CodeGenPasses, OS, CGFT,
642                               /*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
643     Diags.Report(diag::err_fe_unable_to_interface_with_target);
644     return false;
645   }
646 
647   return true;
648 }
649 
650 void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
651                                       std::unique_ptr<raw_pwrite_stream> OS) {
652   TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : nullptr);
653 
654   setCommandLineOpts();
655 
656   bool UsesCodeGen = (Action != Backend_EmitNothing &&
657                       Action != Backend_EmitBC &&
658                       Action != Backend_EmitLL);
659   CreateTargetMachine(UsesCodeGen);
660 
661   if (UsesCodeGen && !TM)
662     return;
663   if (TM)
664     TheModule->setDataLayout(TM->createDataLayout());
665 
666   legacy::PassManager PerModulePasses;
667   PerModulePasses.add(
668       createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
669 
670   legacy::FunctionPassManager PerFunctionPasses(TheModule);
671   PerFunctionPasses.add(
672       createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
673 
674   CreatePasses(PerModulePasses, PerFunctionPasses);
675 
676   legacy::PassManager CodeGenPasses;
677   CodeGenPasses.add(
678       createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
679 
680   switch (Action) {
681   case Backend_EmitNothing:
682     break;
683 
684   case Backend_EmitBC:
685     PerModulePasses.add(createBitcodeWriterPass(
686         *OS, CodeGenOpts.EmitLLVMUseLists, CodeGenOpts.EmitSummaryIndex,
687         CodeGenOpts.EmitSummaryIndex));
688     break;
689 
690   case Backend_EmitLL:
691     PerModulePasses.add(
692         createPrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
693     break;
694 
695   default:
696     if (!AddEmitPasses(CodeGenPasses, Action, *OS))
697       return;
698   }
699 
700   // Before executing passes, print the final values of the LLVM options.
701   cl::PrintOptionValues();
702 
703   // Run passes. For now we do all passes at once, but eventually we
704   // would like to have the option of streaming code generation.
705 
706   {
707     PrettyStackTraceString CrashInfo("Per-function optimization");
708 
709     PerFunctionPasses.doInitialization();
710     for (Function &F : *TheModule)
711       if (!F.isDeclaration())
712         PerFunctionPasses.run(F);
713     PerFunctionPasses.doFinalization();
714   }
715 
716   {
717     PrettyStackTraceString CrashInfo("Per-module optimization passes");
718     PerModulePasses.run(*TheModule);
719   }
720 
721   {
722     PrettyStackTraceString CrashInfo("Code generation");
723     CodeGenPasses.run(*TheModule);
724   }
725 }
726 
727 namespace {
728 // Wrapper prodiving a stream for the ThinLTO backend.
729 class ThinLTOOutputWrapper : public lto::NativeObjectOutput {
730   std::unique_ptr<raw_pwrite_stream> OS;
731 
732 public:
733   ThinLTOOutputWrapper(std::unique_ptr<raw_pwrite_stream> OS)
734       : OS(std::move(OS)) {}
735   std::unique_ptr<raw_pwrite_stream> getStream() override {
736     return std::move(OS);
737   }
738 };
739 }
740 
741 static void runThinLTOBackend(const CodeGenOptions &CGOpts, Module *M,
742                               std::unique_ptr<raw_pwrite_stream> OS) {
743   // If we are performing a ThinLTO importing compile, load the function index
744   // into memory and pass it into thinBackend, which will run the function
745   // importer and invoke LTO passes.
746   ErrorOr<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr =
747       llvm::getModuleSummaryIndexForFile(
748           CGOpts.ThinLTOIndexFile,
749           [&](const DiagnosticInfo &DI) { M->getContext().diagnose(DI); });
750   if (std::error_code EC = IndexOrErr.getError()) {
751     std::string Error = EC.message();
752     errs() << "Error loading index file '" << CGOpts.ThinLTOIndexFile
753            << "': " << Error << "\n";
754     return;
755   }
756   std::unique_ptr<ModuleSummaryIndex> CombinedIndex = std::move(*IndexOrErr);
757 
758   StringMap<std::map<GlobalValue::GUID, GlobalValueSummary *>>
759       ModuleToDefinedGVSummaries;
760   CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
761 
762   // FIXME: We could simply import the modules mentioned in the combined index
763   // here.
764   FunctionImporter::ImportMapTy ImportList;
765   ComputeCrossModuleImportForModule(M->getModuleIdentifier(), *CombinedIndex,
766                                     ImportList);
767 
768   std::vector<std::unique_ptr<llvm::MemoryBuffer>> OwnedImports;
769   MapVector<llvm::StringRef, llvm::MemoryBufferRef> ModuleMap;
770 
771   for (auto &I : ImportList) {
772     ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MBOrErr =
773         llvm::MemoryBuffer::getFile(I.first());
774     if (!MBOrErr) {
775       errs() << "Error loading imported file '" << I.first()
776              << "': " << MBOrErr.getError().message() << "\n";
777       return;
778     }
779     ModuleMap[I.first()] = (*MBOrErr)->getMemBufferRef();
780     OwnedImports.push_back(std::move(*MBOrErr));
781   }
782   auto AddOutput = [&](size_t Task) {
783     return llvm::make_unique<ThinLTOOutputWrapper>(std::move(OS));
784   };
785   lto::Config Conf;
786   if (Error E = thinBackend(
787           Conf, 0, AddOutput, *M, *CombinedIndex, ImportList,
788           ModuleToDefinedGVSummaries[M->getModuleIdentifier()], ModuleMap)) {
789     handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
790       errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
791     });
792   }
793 }
794 
795 void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
796                               const CodeGenOptions &CGOpts,
797                               const clang::TargetOptions &TOpts,
798                               const LangOptions &LOpts, const llvm::DataLayout &TDesc,
799                               Module *M, BackendAction Action,
800                               std::unique_ptr<raw_pwrite_stream> OS) {
801   if (!CGOpts.ThinLTOIndexFile.empty()) {
802     runThinLTOBackend(CGOpts, M, std::move(OS));
803     return;
804   }
805 
806   EmitAssemblyHelper AsmHelper(Diags, CGOpts, TOpts, LOpts, M);
807 
808   AsmHelper.EmitAssembly(Action, std::move(OS));
809 
810   // Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
811   // DataLayout.
812   if (AsmHelper.TM) {
813     std::string DLDesc = M->getDataLayout().getStringRepresentation();
814     if (DLDesc != TDesc.getStringRepresentation()) {
815       unsigned DiagID = Diags.getCustomDiagID(
816           DiagnosticsEngine::Error, "backend data layout '%0' does not match "
817                                     "expected target description '%1'");
818       Diags.Report(DiagID) << DLDesc << TDesc.getStringRepresentation();
819     }
820   }
821 }
822 
823 static const char* getSectionNameForBitcode(const Triple &T) {
824   switch (T.getObjectFormat()) {
825   case Triple::MachO:
826     return "__LLVM,__bitcode";
827   case Triple::COFF:
828   case Triple::ELF:
829   case Triple::UnknownObjectFormat:
830     return ".llvmbc";
831   }
832   llvm_unreachable("Unimplemented ObjectFormatType");
833 }
834 
835 static const char* getSectionNameForCommandline(const Triple &T) {
836   switch (T.getObjectFormat()) {
837   case Triple::MachO:
838     return "__LLVM,__cmdline";
839   case Triple::COFF:
840   case Triple::ELF:
841   case Triple::UnknownObjectFormat:
842     return ".llvmcmd";
843   }
844   llvm_unreachable("Unimplemented ObjectFormatType");
845 }
846 
847 // With -fembed-bitcode, save a copy of the llvm IR as data in the
848 // __LLVM,__bitcode section.
849 void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
850                          llvm::MemoryBufferRef Buf) {
851   if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off)
852     return;
853 
854   // Save llvm.compiler.used and remote it.
855   SmallVector<Constant*, 2> UsedArray;
856   SmallSet<GlobalValue*, 4> UsedGlobals;
857   Type *UsedElementType = Type::getInt8Ty(M->getContext())->getPointerTo(0);
858   GlobalVariable *Used = collectUsedGlobalVariables(*M, UsedGlobals, true);
859   for (auto *GV : UsedGlobals) {
860     if (GV->getName() != "llvm.embedded.module" &&
861         GV->getName() != "llvm.cmdline")
862       UsedArray.push_back(
863           ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
864   }
865   if (Used)
866     Used->eraseFromParent();
867 
868   // Embed the bitcode for the llvm module.
869   std::string Data;
870   ArrayRef<uint8_t> ModuleData;
871   Triple T(M->getTargetTriple());
872   // Create a constant that contains the bitcode.
873   // In case of embedding a marker, ignore the input Buf and use the empty
874   // ArrayRef. It is also legal to create a bitcode marker even Buf is empty.
875   if (CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker) {
876     if (!isBitcode((const unsigned char *)Buf.getBufferStart(),
877                    (const unsigned char *)Buf.getBufferEnd())) {
878       // If the input is LLVM Assembly, bitcode is produced by serializing
879       // the module. Use-lists order need to be perserved in this case.
880       llvm::raw_string_ostream OS(Data);
881       llvm::WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ true);
882       ModuleData =
883           ArrayRef<uint8_t>((const uint8_t *)OS.str().data(), OS.str().size());
884     } else
885       // If the input is LLVM bitcode, write the input byte stream directly.
886       ModuleData = ArrayRef<uint8_t>((const uint8_t *)Buf.getBufferStart(),
887                                      Buf.getBufferSize());
888   }
889   llvm::Constant *ModuleConstant =
890       llvm::ConstantDataArray::get(M->getContext(), ModuleData);
891   llvm::GlobalVariable *GV = new llvm::GlobalVariable(
892       *M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage,
893       ModuleConstant);
894   GV->setSection(getSectionNameForBitcode(T));
895   UsedArray.push_back(
896       ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
897   if (llvm::GlobalVariable *Old =
898           M->getGlobalVariable("llvm.embedded.module", true)) {
899     assert(Old->hasOneUse() &&
900            "llvm.embedded.module can only be used once in llvm.compiler.used");
901     GV->takeName(Old);
902     Old->eraseFromParent();
903   } else {
904     GV->setName("llvm.embedded.module");
905   }
906 
907   // Skip if only bitcode needs to be embedded.
908   if (CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode) {
909     // Embed command-line options.
910     ArrayRef<uint8_t> CmdData(const_cast<uint8_t *>(CGOpts.CmdArgs.data()),
911                               CGOpts.CmdArgs.size());
912     llvm::Constant *CmdConstant =
913       llvm::ConstantDataArray::get(M->getContext(), CmdData);
914     GV = new llvm::GlobalVariable(*M, CmdConstant->getType(), true,
915                                   llvm::GlobalValue::PrivateLinkage,
916                                   CmdConstant);
917     GV->setSection(getSectionNameForCommandline(T));
918     UsedArray.push_back(
919         ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
920     if (llvm::GlobalVariable *Old =
921             M->getGlobalVariable("llvm.cmdline", true)) {
922       assert(Old->hasOneUse() &&
923              "llvm.cmdline can only be used once in llvm.compiler.used");
924       GV->takeName(Old);
925       Old->eraseFromParent();
926     } else {
927       GV->setName("llvm.cmdline");
928     }
929   }
930 
931   if (UsedArray.empty())
932     return;
933 
934   // Recreate llvm.compiler.used.
935   ArrayType *ATy = ArrayType::get(UsedElementType, UsedArray.size());
936   auto *NewUsed = new GlobalVariable(
937       *M, ATy, false, llvm::GlobalValue::AppendingLinkage,
938       llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used");
939   NewUsed->setSection("llvm.metadata");
940 }
941