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 "llvm/Analysis/Verifier.h"
17 #include "llvm/Assembly/PrintModulePass.h"
18 #include "llvm/Bitcode/ReaderWriter.h"
19 #include "llvm/CodeGen/RegAllocRegistry.h"
20 #include "llvm/CodeGen/SchedulerRegistry.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/MC/SubtargetFeature.h"
24 #include "llvm/PassManager.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/FormattedStream.h"
27 #include "llvm/Support/PrettyStackTrace.h"
28 #include "llvm/Support/TargetRegistry.h"
29 #include "llvm/Support/Timer.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/Target/TargetLibraryInfo.h"
32 #include "llvm/Target/TargetMachine.h"
33 #include "llvm/Target/TargetOptions.h"
34 #include "llvm/Transforms/IPO.h"
35 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
36 #include "llvm/Transforms/Instrumentation.h"
37 #include "llvm/Transforms/Scalar.h"
38 using namespace clang;
39 using namespace llvm;
40 
41 namespace {
42 
43 class EmitAssemblyHelper {
44   DiagnosticsEngine &Diags;
45   const CodeGenOptions &CodeGenOpts;
46   const clang::TargetOptions &TargetOpts;
47   const LangOptions &LangOpts;
48   Module *TheModule;
49 
50   Timer CodeGenerationTime;
51 
52   mutable PassManager *CodeGenPasses;
53   mutable PassManager *PerModulePasses;
54   mutable FunctionPassManager *PerFunctionPasses;
55 
56 private:
57   PassManager *getCodeGenPasses(TargetMachine *TM) const {
58     if (!CodeGenPasses) {
59       CodeGenPasses = new PassManager();
60       CodeGenPasses->add(new DataLayout(TheModule));
61       if (TM)
62         TM->addAnalysisPasses(*CodeGenPasses);
63     }
64     return CodeGenPasses;
65   }
66 
67   PassManager *getPerModulePasses(TargetMachine *TM) const {
68     if (!PerModulePasses) {
69       PerModulePasses = new PassManager();
70       PerModulePasses->add(new DataLayout(TheModule));
71       if (TM)
72         TM->addAnalysisPasses(*PerModulePasses);
73     }
74     return PerModulePasses;
75   }
76 
77   FunctionPassManager *getPerFunctionPasses(TargetMachine *TM) const {
78     if (!PerFunctionPasses) {
79       PerFunctionPasses = new FunctionPassManager(TheModule);
80       PerFunctionPasses->add(new DataLayout(TheModule));
81       if (TM)
82         TM->addAnalysisPasses(*PerFunctionPasses);
83     }
84     return PerFunctionPasses;
85   }
86 
87 
88   void CreatePasses(TargetMachine *TM);
89 
90   /// CreateTargetMachine - Generates the TargetMachine.
91   /// Returns Null if it is unable to create the target machine.
92   /// Some of our clang tests specify triples which are not built
93   /// into clang. This is okay because these tests check the generated
94   /// IR, and they require DataLayout which depends on the triple.
95   /// In this case, we allow this method to fail and not report an error.
96   /// When MustCreateTM is used, we print an error if we are unable to load
97   /// the requested target.
98   TargetMachine *CreateTargetMachine(bool MustCreateTM);
99 
100   /// AddEmitPasses - Add passes necessary to emit assembly or LLVM IR.
101   ///
102   /// \return True on success.
103   bool AddEmitPasses(BackendAction Action, formatted_raw_ostream &OS,
104                      TargetMachine *TM);
105 
106 public:
107   EmitAssemblyHelper(DiagnosticsEngine &_Diags,
108                      const CodeGenOptions &CGOpts,
109                      const clang::TargetOptions &TOpts,
110                      const LangOptions &LOpts,
111                      Module *M)
112     : Diags(_Diags), CodeGenOpts(CGOpts), TargetOpts(TOpts), LangOpts(LOpts),
113       TheModule(M), CodeGenerationTime("Code Generation Time"),
114       CodeGenPasses(0), PerModulePasses(0), PerFunctionPasses(0) {}
115 
116   ~EmitAssemblyHelper() {
117     delete CodeGenPasses;
118     delete PerModulePasses;
119     delete PerFunctionPasses;
120   }
121 
122   void EmitAssembly(BackendAction Action, raw_ostream *OS);
123 };
124 
125 // We need this wrapper to access LangOpts and CGOpts from extension functions
126 // that we add to the PassManagerBuilder.
127 class PassManagerBuilderWrapper : public PassManagerBuilder {
128 public:
129   PassManagerBuilderWrapper(const CodeGenOptions &CGOpts,
130                             const LangOptions &LangOpts)
131       : PassManagerBuilder(), CGOpts(CGOpts), LangOpts(LangOpts) {}
132   const CodeGenOptions &getCGOpts() const { return CGOpts; }
133   const LangOptions &getLangOpts() const { return LangOpts; }
134 private:
135   const CodeGenOptions &CGOpts;
136   const LangOptions &LangOpts;
137 };
138 
139 }
140 
141 static void addObjCARCAPElimPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
142   if (Builder.OptLevel > 0)
143     PM.add(createObjCARCAPElimPass());
144 }
145 
146 static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
147   if (Builder.OptLevel > 0)
148     PM.add(createObjCARCExpandPass());
149 }
150 
151 static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
152   if (Builder.OptLevel > 0)
153     PM.add(createObjCARCOptPass());
154 }
155 
156 static void addBoundsCheckingPass(const PassManagerBuilder &Builder,
157                                     PassManagerBase &PM) {
158   PM.add(createBoundsCheckingPass());
159 }
160 
161 static void addAddressSanitizerPasses(const PassManagerBuilder &Builder,
162                                       PassManagerBase &PM) {
163   const PassManagerBuilderWrapper &BuilderWrapper =
164       static_cast<const PassManagerBuilderWrapper&>(Builder);
165   const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
166   const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
167   PM.add(createAddressSanitizerFunctionPass(LangOpts.SanitizeInitOrder,
168                                             LangOpts.SanitizeUseAfterReturn,
169                                             LangOpts.SanitizeUseAfterScope,
170                                             CGOpts.SanitizerBlacklistFile));
171   PM.add(createAddressSanitizerModulePass(LangOpts.SanitizeInitOrder,
172                                           CGOpts.SanitizerBlacklistFile));
173 }
174 
175 static void addMemorySanitizerPass(const PassManagerBuilder &Builder,
176                                    PassManagerBase &PM) {
177   const PassManagerBuilderWrapper &BuilderWrapper =
178       static_cast<const PassManagerBuilderWrapper&>(Builder);
179   const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
180   PM.add(createMemorySanitizerPass(CGOpts.MemorySanitizerTrackOrigins,
181                                    CGOpts.SanitizerBlacklistFile));
182 }
183 
184 static void addThreadSanitizerPass(const PassManagerBuilder &Builder,
185                                    PassManagerBase &PM) {
186   const PassManagerBuilderWrapper &BuilderWrapper =
187       static_cast<const PassManagerBuilderWrapper&>(Builder);
188   const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
189   PM.add(createThreadSanitizerPass(CGOpts.SanitizerBlacklistFile));
190 }
191 
192 void EmitAssemblyHelper::CreatePasses(TargetMachine *TM) {
193   unsigned OptLevel = CodeGenOpts.OptimizationLevel;
194   CodeGenOptions::InliningMethod Inlining = CodeGenOpts.getInlining();
195 
196   // Handle disabling of LLVM optimization, where we want to preserve the
197   // internal module before any optimization.
198   if (CodeGenOpts.DisableLLVMOpts) {
199     OptLevel = 0;
200     Inlining = CodeGenOpts.NoInlining;
201   }
202 
203   PassManagerBuilderWrapper PMBuilder(CodeGenOpts, LangOpts);
204   PMBuilder.OptLevel = OptLevel;
205   PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize;
206 
207   PMBuilder.DisableSimplifyLibCalls = !CodeGenOpts.SimplifyLibCalls;
208   PMBuilder.DisableUnitAtATime = !CodeGenOpts.UnitAtATime;
209   PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
210 
211   // In ObjC ARC mode, add the main ARC optimization passes.
212   if (LangOpts.ObjCAutoRefCount) {
213     PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
214                            addObjCARCExpandPass);
215     PMBuilder.addExtension(PassManagerBuilder::EP_ModuleOptimizerEarly,
216                            addObjCARCAPElimPass);
217     PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
218                            addObjCARCOptPass);
219   }
220 
221   if (LangOpts.SanitizeBounds) {
222     PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
223                            addBoundsCheckingPass);
224     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
225                            addBoundsCheckingPass);
226   }
227 
228   if (LangOpts.SanitizeAddress) {
229     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
230                            addAddressSanitizerPasses);
231     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
232                            addAddressSanitizerPasses);
233   }
234 
235   if (LangOpts.SanitizeMemory) {
236     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
237                            addMemorySanitizerPass);
238     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
239                            addMemorySanitizerPass);
240   }
241 
242   if (LangOpts.SanitizeThread) {
243     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
244                            addThreadSanitizerPass);
245     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
246                            addThreadSanitizerPass);
247   }
248 
249   // Figure out TargetLibraryInfo.
250   Triple TargetTriple(TheModule->getTargetTriple());
251   PMBuilder.LibraryInfo = new TargetLibraryInfo(TargetTriple);
252   if (!CodeGenOpts.SimplifyLibCalls)
253     PMBuilder.LibraryInfo->disableAllFunctions();
254 
255   switch (Inlining) {
256   case CodeGenOptions::NoInlining: break;
257   case CodeGenOptions::NormalInlining: {
258     // FIXME: Derive these constants in a principled fashion.
259     unsigned Threshold = 225;
260     if (CodeGenOpts.OptimizeSize == 1)      // -Os
261       Threshold = 75;
262     else if (CodeGenOpts.OptimizeSize == 2) // -Oz
263       Threshold = 25;
264     else if (OptLevel > 2)
265       Threshold = 275;
266     PMBuilder.Inliner = createFunctionInliningPass(Threshold);
267     break;
268   }
269   case CodeGenOptions::OnlyAlwaysInlining:
270     // Respect always_inline.
271     if (OptLevel == 0)
272       // Do not insert lifetime intrinsics at -O0.
273       PMBuilder.Inliner = createAlwaysInlinerPass(false);
274     else
275       PMBuilder.Inliner = createAlwaysInlinerPass();
276     break;
277   }
278 
279   // Set up the per-function pass manager.
280   FunctionPassManager *FPM = getPerFunctionPasses(TM);
281   if (CodeGenOpts.VerifyModule)
282     FPM->add(createVerifierPass());
283   PMBuilder.populateFunctionPassManager(*FPM);
284 
285   // Set up the per-module pass manager.
286   PassManager *MPM = getPerModulePasses(TM);
287 
288   if (CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes) {
289     MPM->add(createGCOVProfilerPass(CodeGenOpts.EmitGcovNotes,
290                                     CodeGenOpts.EmitGcovArcs,
291                                     TargetTriple.isMacOSX(),
292                                     false,
293                                     CodeGenOpts.DisableRedZone));
294 
295     if (CodeGenOpts.getDebugInfo() == CodeGenOptions::NoDebugInfo)
296       MPM->add(createStripSymbolsPass(true));
297   }
298 
299   PMBuilder.populateModulePassManager(*MPM);
300 }
301 
302 TargetMachine *EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
303   // Create the TargetMachine for generating code.
304   std::string Error;
305   std::string Triple = TheModule->getTargetTriple();
306   const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
307   if (!TheTarget) {
308     if (MustCreateTM)
309       Diags.Report(diag::err_fe_unable_to_create_target) << Error;
310     return 0;
311   }
312 
313   // FIXME: Expose these capabilities via actual APIs!!!! Aside from just
314   // being gross, this is also totally broken if we ever care about
315   // concurrency.
316 
317   TargetMachine::setAsmVerbosityDefault(CodeGenOpts.AsmVerbose);
318 
319   TargetMachine::setFunctionSections(CodeGenOpts.FunctionSections);
320   TargetMachine::setDataSections    (CodeGenOpts.DataSections);
321 
322   // FIXME: Parse this earlier.
323   llvm::CodeModel::Model CM;
324   if (CodeGenOpts.CodeModel == "small") {
325     CM = llvm::CodeModel::Small;
326   } else if (CodeGenOpts.CodeModel == "kernel") {
327     CM = llvm::CodeModel::Kernel;
328   } else if (CodeGenOpts.CodeModel == "medium") {
329     CM = llvm::CodeModel::Medium;
330   } else if (CodeGenOpts.CodeModel == "large") {
331     CM = llvm::CodeModel::Large;
332   } else {
333     assert(CodeGenOpts.CodeModel.empty() && "Invalid code model!");
334     CM = llvm::CodeModel::Default;
335   }
336 
337   SmallVector<const char *, 16> BackendArgs;
338   BackendArgs.push_back("clang"); // Fake program name.
339   if (!CodeGenOpts.DebugPass.empty()) {
340     BackendArgs.push_back("-debug-pass");
341     BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
342   }
343   if (!CodeGenOpts.LimitFloatPrecision.empty()) {
344     BackendArgs.push_back("-limit-float-precision");
345     BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
346   }
347   if (llvm::TimePassesIsEnabled)
348     BackendArgs.push_back("-time-passes");
349   for (unsigned i = 0, e = CodeGenOpts.BackendOptions.size(); i != e; ++i)
350     BackendArgs.push_back(CodeGenOpts.BackendOptions[i].c_str());
351   if (CodeGenOpts.NoGlobalMerge)
352     BackendArgs.push_back("-global-merge=false");
353   BackendArgs.push_back(0);
354   llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
355                                     BackendArgs.data());
356 
357   std::string FeaturesStr;
358   if (TargetOpts.Features.size()) {
359     SubtargetFeatures Features;
360     for (std::vector<std::string>::const_iterator
361            it = TargetOpts.Features.begin(),
362            ie = TargetOpts.Features.end(); it != ie; ++it)
363       Features.AddFeature(*it);
364     FeaturesStr = Features.getString();
365   }
366 
367   llvm::Reloc::Model RM = llvm::Reloc::Default;
368   if (CodeGenOpts.RelocationModel == "static") {
369     RM = llvm::Reloc::Static;
370   } else if (CodeGenOpts.RelocationModel == "pic") {
371     RM = llvm::Reloc::PIC_;
372   } else {
373     assert(CodeGenOpts.RelocationModel == "dynamic-no-pic" &&
374            "Invalid PIC model!");
375     RM = llvm::Reloc::DynamicNoPIC;
376   }
377 
378   CodeGenOpt::Level OptLevel = CodeGenOpt::Default;
379   switch (CodeGenOpts.OptimizationLevel) {
380   default: break;
381   case 0: OptLevel = CodeGenOpt::None; break;
382   case 3: OptLevel = CodeGenOpt::Aggressive; break;
383   }
384 
385   llvm::TargetOptions Options;
386 
387   // Set frame pointer elimination mode.
388   if (!CodeGenOpts.DisableFPElim) {
389     Options.NoFramePointerElim = false;
390     Options.NoFramePointerElimNonLeaf = false;
391   } else if (CodeGenOpts.OmitLeafFramePointer) {
392     Options.NoFramePointerElim = false;
393     Options.NoFramePointerElimNonLeaf = true;
394   } else {
395     Options.NoFramePointerElim = true;
396     Options.NoFramePointerElimNonLeaf = true;
397   }
398 
399   if (CodeGenOpts.UseInitArray)
400     Options.UseInitArray = true;
401 
402   // Set float ABI type.
403   if (CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp")
404     Options.FloatABIType = llvm::FloatABI::Soft;
405   else if (CodeGenOpts.FloatABI == "hard")
406     Options.FloatABIType = llvm::FloatABI::Hard;
407   else {
408     assert(CodeGenOpts.FloatABI.empty() && "Invalid float abi!");
409     Options.FloatABIType = llvm::FloatABI::Default;
410   }
411 
412   // Set FP fusion mode.
413   switch (CodeGenOpts.getFPContractMode()) {
414   case CodeGenOptions::FPC_Off:
415     Options.AllowFPOpFusion = llvm::FPOpFusion::Strict;
416     break;
417   case CodeGenOptions::FPC_On:
418     Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
419     break;
420   case CodeGenOptions::FPC_Fast:
421     Options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
422     break;
423   }
424 
425   Options.LessPreciseFPMADOption = CodeGenOpts.LessPreciseFPMAD;
426   Options.NoInfsFPMath = CodeGenOpts.NoInfsFPMath;
427   Options.NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath;
428   Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
429   Options.UnsafeFPMath = CodeGenOpts.UnsafeFPMath;
430   Options.UseSoftFloat = CodeGenOpts.SoftFloat;
431   Options.StackAlignmentOverride = CodeGenOpts.StackAlignment;
432   Options.RealignStack = CodeGenOpts.StackRealignment;
433   Options.DisableTailCalls = CodeGenOpts.DisableTailCalls;
434   Options.TrapFuncName = CodeGenOpts.TrapFuncName;
435   Options.PositionIndependentExecutable = LangOpts.PIELevel != 0;
436   Options.SSPBufferSize = CodeGenOpts.SSPBufferSize;
437 
438   TargetMachine *TM = TheTarget->createTargetMachine(Triple, TargetOpts.CPU,
439                                                      FeaturesStr, Options,
440                                                      RM, CM, OptLevel);
441 
442   if (CodeGenOpts.RelaxAll)
443     TM->setMCRelaxAll(true);
444   if (CodeGenOpts.SaveTempLabels)
445     TM->setMCSaveTempLabels(true);
446   if (CodeGenOpts.NoDwarf2CFIAsm)
447     TM->setMCUseCFI(false);
448   if (!CodeGenOpts.NoDwarfDirectoryAsm)
449     TM->setMCUseDwarfDirectory(true);
450   if (CodeGenOpts.NoExecStack)
451     TM->setMCNoExecStack(true);
452 
453   return TM;
454 }
455 
456 bool EmitAssemblyHelper::AddEmitPasses(BackendAction Action,
457                                        formatted_raw_ostream &OS,
458                                        TargetMachine *TM) {
459 
460   // Create the code generator passes.
461   PassManager *PM = getCodeGenPasses(TM);
462 
463   // Add LibraryInfo.
464   llvm::Triple TargetTriple(TheModule->getTargetTriple());
465   TargetLibraryInfo *TLI = new TargetLibraryInfo(TargetTriple);
466   if (!CodeGenOpts.SimplifyLibCalls)
467     TLI->disableAllFunctions();
468   PM->add(TLI);
469 
470   // Add Target specific analysis passes.
471   TM->addAnalysisPasses(*PM);
472 
473   // Normal mode, emit a .s or .o file by running the code generator. Note,
474   // this also adds codegenerator level optimization passes.
475   TargetMachine::CodeGenFileType CGFT = TargetMachine::CGFT_AssemblyFile;
476   if (Action == Backend_EmitObj)
477     CGFT = TargetMachine::CGFT_ObjectFile;
478   else if (Action == Backend_EmitMCNull)
479     CGFT = TargetMachine::CGFT_Null;
480   else
481     assert(Action == Backend_EmitAssembly && "Invalid action!");
482 
483   // Add ObjC ARC final-cleanup optimizations. This is done as part of the
484   // "codegen" passes so that it isn't run multiple times when there is
485   // inlining happening.
486   if (LangOpts.ObjCAutoRefCount &&
487       CodeGenOpts.OptimizationLevel > 0)
488     PM->add(createObjCARCContractPass());
489 
490   if (TM->addPassesToEmitFile(*PM, OS, CGFT,
491                               /*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
492     Diags.Report(diag::err_fe_unable_to_interface_with_target);
493     return false;
494   }
495 
496   return true;
497 }
498 
499 void EmitAssemblyHelper::EmitAssembly(BackendAction Action, raw_ostream *OS) {
500   TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : 0);
501   llvm::formatted_raw_ostream FormattedOS;
502 
503   bool UsesCodeGen = (Action != Backend_EmitNothing &&
504                       Action != Backend_EmitBC &&
505                       Action != Backend_EmitLL);
506   TargetMachine *TM = CreateTargetMachine(UsesCodeGen);
507   CreatePasses(TM);
508 
509   switch (Action) {
510   case Backend_EmitNothing:
511     break;
512 
513   case Backend_EmitBC:
514     getPerModulePasses(TM)->add(createBitcodeWriterPass(*OS));
515     break;
516 
517   case Backend_EmitLL:
518     FormattedOS.setStream(*OS, formatted_raw_ostream::PRESERVE_STREAM);
519     getPerModulePasses(TM)->add(createPrintModulePass(&FormattedOS));
520     break;
521 
522   default:
523     FormattedOS.setStream(*OS, formatted_raw_ostream::PRESERVE_STREAM);
524     if (!AddEmitPasses(Action, FormattedOS, TM))
525       return;
526   }
527 
528   // Before executing passes, print the final values of the LLVM options.
529   cl::PrintOptionValues();
530 
531   // Run passes. For now we do all passes at once, but eventually we
532   // would like to have the option of streaming code generation.
533 
534   if (PerFunctionPasses) {
535     PrettyStackTraceString CrashInfo("Per-function optimization");
536 
537     PerFunctionPasses->doInitialization();
538     for (Module::iterator I = TheModule->begin(),
539            E = TheModule->end(); I != E; ++I)
540       if (!I->isDeclaration())
541         PerFunctionPasses->run(*I);
542     PerFunctionPasses->doFinalization();
543   }
544 
545   if (PerModulePasses) {
546     PrettyStackTraceString CrashInfo("Per-module optimization passes");
547     PerModulePasses->run(*TheModule);
548   }
549 
550   if (CodeGenPasses) {
551     PrettyStackTraceString CrashInfo("Code generation");
552     CodeGenPasses->run(*TheModule);
553   }
554 }
555 
556 void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
557                               const CodeGenOptions &CGOpts,
558                               const clang::TargetOptions &TOpts,
559                               const LangOptions &LOpts,
560                               Module *M,
561                               BackendAction Action, raw_ostream *OS) {
562   EmitAssemblyHelper AsmHelper(Diags, CGOpts, TOpts, LOpts, M);
563 
564   AsmHelper.EmitAssembly(Action, OS);
565 }
566