1 //===--- BackendUtil.cpp - LLVM Backend Utilities -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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/ObjCARC.h"
38 #include "llvm/Transforms/Scalar.h"
39 using namespace clang;
44 class EmitAssemblyHelper {
45 DiagnosticsEngine &Diags;
46 const CodeGenOptions &CodeGenOpts;
47 const clang::TargetOptions &TargetOpts;
48 const LangOptions &LangOpts;
51 Timer CodeGenerationTime;
53 mutable PassManager *CodeGenPasses;
54 mutable PassManager *PerModulePasses;
55 mutable FunctionPassManager *PerFunctionPasses;
58 PassManager *getCodeGenPasses(TargetMachine *TM) const {
60 CodeGenPasses = new PassManager();
61 CodeGenPasses->add(new DataLayout(TheModule));
63 TM->addAnalysisPasses(*CodeGenPasses);
68 PassManager *getPerModulePasses(TargetMachine *TM) const {
69 if (!PerModulePasses) {
70 PerModulePasses = new PassManager();
71 PerModulePasses->add(new DataLayout(TheModule));
73 TM->addAnalysisPasses(*PerModulePasses);
75 return PerModulePasses;
78 FunctionPassManager *getPerFunctionPasses(TargetMachine *TM) const {
79 if (!PerFunctionPasses) {
80 PerFunctionPasses = new FunctionPassManager(TheModule);
81 PerFunctionPasses->add(new DataLayout(TheModule));
83 TM->addAnalysisPasses(*PerFunctionPasses);
85 return PerFunctionPasses;
89 void CreatePasses(TargetMachine *TM);
91 /// CreateTargetMachine - Generates the TargetMachine.
92 /// Returns Null if it is unable to create the target machine.
93 /// Some of our clang tests specify triples which are not built
94 /// into clang. This is okay because these tests check the generated
95 /// IR, and they require DataLayout which depends on the triple.
96 /// In this case, we allow this method to fail and not report an error.
97 /// When MustCreateTM is used, we print an error if we are unable to load
98 /// the requested target.
99 TargetMachine *CreateTargetMachine(bool MustCreateTM);
101 /// AddEmitPasses - Add passes necessary to emit assembly or LLVM IR.
103 /// \return True on success.
104 bool AddEmitPasses(BackendAction Action, formatted_raw_ostream &OS,
108 EmitAssemblyHelper(DiagnosticsEngine &_Diags,
109 const CodeGenOptions &CGOpts,
110 const clang::TargetOptions &TOpts,
111 const LangOptions &LOpts,
113 : Diags(_Diags), CodeGenOpts(CGOpts), TargetOpts(TOpts), LangOpts(LOpts),
114 TheModule(M), CodeGenerationTime("Code Generation Time"),
115 CodeGenPasses(0), PerModulePasses(0), PerFunctionPasses(0) {}
117 ~EmitAssemblyHelper() {
118 delete CodeGenPasses;
119 delete PerModulePasses;
120 delete PerFunctionPasses;
123 void EmitAssembly(BackendAction Action, raw_ostream *OS);
126 // We need this wrapper to access LangOpts and CGOpts from extension functions
127 // that we add to the PassManagerBuilder.
128 class PassManagerBuilderWrapper : public PassManagerBuilder {
130 PassManagerBuilderWrapper(const CodeGenOptions &CGOpts,
131 const LangOptions &LangOpts)
132 : PassManagerBuilder(), CGOpts(CGOpts), LangOpts(LangOpts) {}
133 const CodeGenOptions &getCGOpts() const { return CGOpts; }
134 const LangOptions &getLangOpts() const { return LangOpts; }
136 const CodeGenOptions &CGOpts;
137 const LangOptions &LangOpts;
142 static void addObjCARCAPElimPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
143 if (Builder.OptLevel > 0)
144 PM.add(createObjCARCAPElimPass());
147 static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
148 if (Builder.OptLevel > 0)
149 PM.add(createObjCARCExpandPass());
152 static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
153 if (Builder.OptLevel > 0)
154 PM.add(createObjCARCOptPass());
157 static void addSampleProfileLoaderPass(const PassManagerBuilder &Builder,
158 PassManagerBase &PM) {
159 const PassManagerBuilderWrapper &BuilderWrapper =
160 static_cast<const PassManagerBuilderWrapper &>(Builder);
161 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
162 PM.add(createSampleProfileLoaderPass(CGOpts.SampleProfileFile));
165 static void addBoundsCheckingPass(const PassManagerBuilder &Builder,
166 PassManagerBase &PM) {
167 PM.add(createBoundsCheckingPass());
170 static void addAddressSanitizerPasses(const PassManagerBuilder &Builder,
171 PassManagerBase &PM) {
172 const PassManagerBuilderWrapper &BuilderWrapper =
173 static_cast<const PassManagerBuilderWrapper&>(Builder);
174 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
175 const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
176 PM.add(createAddressSanitizerFunctionPass(
177 LangOpts.Sanitize.InitOrder,
178 LangOpts.Sanitize.UseAfterReturn,
179 LangOpts.Sanitize.UseAfterScope,
180 CGOpts.SanitizerBlacklistFile,
181 CGOpts.SanitizeAddressZeroBaseShadow));
182 PM.add(createAddressSanitizerModulePass(
183 LangOpts.Sanitize.InitOrder,
184 CGOpts.SanitizerBlacklistFile,
185 CGOpts.SanitizeAddressZeroBaseShadow));
188 static void addMemorySanitizerPass(const PassManagerBuilder &Builder,
189 PassManagerBase &PM) {
190 const PassManagerBuilderWrapper &BuilderWrapper =
191 static_cast<const PassManagerBuilderWrapper&>(Builder);
192 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
193 PM.add(createMemorySanitizerPass(CGOpts.SanitizeMemoryTrackOrigins,
194 CGOpts.SanitizerBlacklistFile));
196 // MemorySanitizer inserts complex instrumentation that mostly follows
197 // the logic of the original code, but operates on "shadow" values.
198 // It can benefit from re-running some general purpose optimization passes.
199 if (Builder.OptLevel > 0) {
200 PM.add(createEarlyCSEPass());
201 PM.add(createReassociatePass());
202 PM.add(createLICMPass());
203 PM.add(createGVNPass());
204 PM.add(createInstructionCombiningPass());
205 PM.add(createDeadStoreEliminationPass());
209 static void addThreadSanitizerPass(const PassManagerBuilder &Builder,
210 PassManagerBase &PM) {
211 const PassManagerBuilderWrapper &BuilderWrapper =
212 static_cast<const PassManagerBuilderWrapper&>(Builder);
213 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
214 PM.add(createThreadSanitizerPass(CGOpts.SanitizerBlacklistFile));
217 static void addDataFlowSanitizerPass(const PassManagerBuilder &Builder,
218 PassManagerBase &PM) {
219 const PassManagerBuilderWrapper &BuilderWrapper =
220 static_cast<const PassManagerBuilderWrapper&>(Builder);
221 const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
222 PM.add(createDataFlowSanitizerPass(CGOpts.SanitizerBlacklistFile));
225 void EmitAssemblyHelper::CreatePasses(TargetMachine *TM) {
226 unsigned OptLevel = CodeGenOpts.OptimizationLevel;
227 CodeGenOptions::InliningMethod Inlining = CodeGenOpts.getInlining();
229 // Handle disabling of LLVM optimization, where we want to preserve the
230 // internal module before any optimization.
231 if (CodeGenOpts.DisableLLVMOpts) {
233 Inlining = CodeGenOpts.NoInlining;
236 PassManagerBuilderWrapper PMBuilder(CodeGenOpts, LangOpts);
237 PMBuilder.OptLevel = OptLevel;
238 PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize;
239 PMBuilder.BBVectorize = CodeGenOpts.VectorizeBB;
240 PMBuilder.SLPVectorize = CodeGenOpts.VectorizeSLP;
241 PMBuilder.LoopVectorize = CodeGenOpts.VectorizeLoop;
243 PMBuilder.DisableUnitAtATime = !CodeGenOpts.UnitAtATime;
244 PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
245 PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
247 if (!CodeGenOpts.SampleProfileFile.empty())
248 PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
249 addSampleProfileLoaderPass);
251 // In ObjC ARC mode, add the main ARC optimization passes.
252 if (LangOpts.ObjCAutoRefCount) {
253 PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
254 addObjCARCExpandPass);
255 PMBuilder.addExtension(PassManagerBuilder::EP_ModuleOptimizerEarly,
256 addObjCARCAPElimPass);
257 PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
261 if (LangOpts.Sanitize.LocalBounds) {
262 PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
263 addBoundsCheckingPass);
264 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
265 addBoundsCheckingPass);
268 if (LangOpts.Sanitize.Address) {
269 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
270 addAddressSanitizerPasses);
271 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
272 addAddressSanitizerPasses);
275 if (LangOpts.Sanitize.Memory) {
276 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
277 addMemorySanitizerPass);
278 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
279 addMemorySanitizerPass);
282 if (LangOpts.Sanitize.Thread) {
283 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
284 addThreadSanitizerPass);
285 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
286 addThreadSanitizerPass);
289 if (LangOpts.Sanitize.DataFlow) {
290 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
291 addDataFlowSanitizerPass);
292 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
293 addDataFlowSanitizerPass);
296 // Figure out TargetLibraryInfo.
297 Triple TargetTriple(TheModule->getTargetTriple());
298 PMBuilder.LibraryInfo = new TargetLibraryInfo(TargetTriple);
299 if (!CodeGenOpts.SimplifyLibCalls)
300 PMBuilder.LibraryInfo->disableAllFunctions();
303 case CodeGenOptions::NoInlining: break;
304 case CodeGenOptions::NormalInlining: {
305 // FIXME: Derive these constants in a principled fashion.
306 unsigned Threshold = 225;
307 if (CodeGenOpts.OptimizeSize == 1) // -Os
309 else if (CodeGenOpts.OptimizeSize == 2) // -Oz
311 else if (OptLevel > 2)
313 PMBuilder.Inliner = createFunctionInliningPass(Threshold);
316 case CodeGenOptions::OnlyAlwaysInlining:
317 // Respect always_inline.
319 // Do not insert lifetime intrinsics at -O0.
320 PMBuilder.Inliner = createAlwaysInlinerPass(false);
322 PMBuilder.Inliner = createAlwaysInlinerPass();
326 // Set up the per-function pass manager.
327 FunctionPassManager *FPM = getPerFunctionPasses(TM);
328 if (CodeGenOpts.VerifyModule)
329 FPM->add(createVerifierPass());
330 PMBuilder.populateFunctionPassManager(*FPM);
332 // Set up the per-module pass manager.
333 PassManager *MPM = getPerModulePasses(TM);
335 if (!CodeGenOpts.DisableGCov &&
336 (CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes)) {
337 // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if
338 // LLVM's -default-gcov-version flag is set to something invalid.
340 Options.EmitNotes = CodeGenOpts.EmitGcovNotes;
341 Options.EmitData = CodeGenOpts.EmitGcovArcs;
342 memcpy(Options.Version, CodeGenOpts.CoverageVersion, 4);
343 Options.UseCfgChecksum = CodeGenOpts.CoverageExtraChecksum;
344 Options.NoRedZone = CodeGenOpts.DisableRedZone;
345 Options.FunctionNamesInData =
346 !CodeGenOpts.CoverageNoFunctionNamesInData;
347 MPM->add(createGCOVProfilerPass(Options));
348 if (CodeGenOpts.getDebugInfo() == CodeGenOptions::NoDebugInfo)
349 MPM->add(createStripSymbolsPass(true));
352 PMBuilder.populateModulePassManager(*MPM);
355 TargetMachine *EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
356 // Create the TargetMachine for generating code.
358 std::string Triple = TheModule->getTargetTriple();
359 const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
362 Diags.Report(diag::err_fe_unable_to_create_target) << Error;
366 // FIXME: Expose these capabilities via actual APIs!!!! Aside from just
367 // being gross, this is also totally broken if we ever care about
370 TargetMachine::setAsmVerbosityDefault(CodeGenOpts.AsmVerbose);
372 TargetMachine::setFunctionSections(CodeGenOpts.FunctionSections);
373 TargetMachine::setDataSections (CodeGenOpts.DataSections);
375 // FIXME: Parse this earlier.
376 llvm::CodeModel::Model CM;
377 if (CodeGenOpts.CodeModel == "small") {
378 CM = llvm::CodeModel::Small;
379 } else if (CodeGenOpts.CodeModel == "kernel") {
380 CM = llvm::CodeModel::Kernel;
381 } else if (CodeGenOpts.CodeModel == "medium") {
382 CM = llvm::CodeModel::Medium;
383 } else if (CodeGenOpts.CodeModel == "large") {
384 CM = llvm::CodeModel::Large;
386 assert(CodeGenOpts.CodeModel.empty() && "Invalid code model!");
387 CM = llvm::CodeModel::Default;
390 SmallVector<const char *, 16> BackendArgs;
391 BackendArgs.push_back("clang"); // Fake program name.
392 if (!CodeGenOpts.DebugPass.empty()) {
393 BackendArgs.push_back("-debug-pass");
394 BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
396 if (!CodeGenOpts.LimitFloatPrecision.empty()) {
397 BackendArgs.push_back("-limit-float-precision");
398 BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
400 if (llvm::TimePassesIsEnabled)
401 BackendArgs.push_back("-time-passes");
402 for (unsigned i = 0, e = CodeGenOpts.BackendOptions.size(); i != e; ++i)
403 BackendArgs.push_back(CodeGenOpts.BackendOptions[i].c_str());
404 if (CodeGenOpts.NoGlobalMerge)
405 BackendArgs.push_back("-global-merge=false");
406 BackendArgs.push_back(0);
407 llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
410 std::string FeaturesStr;
411 if (TargetOpts.Features.size()) {
412 SubtargetFeatures Features;
413 for (std::vector<std::string>::const_iterator
414 it = TargetOpts.Features.begin(),
415 ie = TargetOpts.Features.end(); it != ie; ++it)
416 Features.AddFeature(*it);
417 FeaturesStr = Features.getString();
420 llvm::Reloc::Model RM = llvm::Reloc::Default;
421 if (CodeGenOpts.RelocationModel == "static") {
422 RM = llvm::Reloc::Static;
423 } else if (CodeGenOpts.RelocationModel == "pic") {
424 RM = llvm::Reloc::PIC_;
426 assert(CodeGenOpts.RelocationModel == "dynamic-no-pic" &&
427 "Invalid PIC model!");
428 RM = llvm::Reloc::DynamicNoPIC;
431 CodeGenOpt::Level OptLevel = CodeGenOpt::Default;
432 switch (CodeGenOpts.OptimizationLevel) {
434 case 0: OptLevel = CodeGenOpt::None; break;
435 case 3: OptLevel = CodeGenOpt::Aggressive; break;
438 llvm::TargetOptions Options;
440 // Set frame pointer elimination mode.
441 if (!CodeGenOpts.DisableFPElim) {
442 Options.NoFramePointerElim = false;
443 } else if (CodeGenOpts.OmitLeafFramePointer) {
444 Options.NoFramePointerElim = false;
446 Options.NoFramePointerElim = true;
449 if (CodeGenOpts.UseInitArray)
450 Options.UseInitArray = true;
452 // Set float ABI type.
453 if (CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp")
454 Options.FloatABIType = llvm::FloatABI::Soft;
455 else if (CodeGenOpts.FloatABI == "hard")
456 Options.FloatABIType = llvm::FloatABI::Hard;
458 assert(CodeGenOpts.FloatABI.empty() && "Invalid float abi!");
459 Options.FloatABIType = llvm::FloatABI::Default;
462 // Set FP fusion mode.
463 switch (CodeGenOpts.getFPContractMode()) {
464 case CodeGenOptions::FPC_Off:
465 Options.AllowFPOpFusion = llvm::FPOpFusion::Strict;
467 case CodeGenOptions::FPC_On:
468 Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
470 case CodeGenOptions::FPC_Fast:
471 Options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
475 Options.LessPreciseFPMADOption = CodeGenOpts.LessPreciseFPMAD;
476 Options.NoInfsFPMath = CodeGenOpts.NoInfsFPMath;
477 Options.NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath;
478 Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
479 Options.UnsafeFPMath = CodeGenOpts.UnsafeFPMath;
480 Options.UseSoftFloat = CodeGenOpts.SoftFloat;
481 Options.StackAlignmentOverride = CodeGenOpts.StackAlignment;
482 Options.DisableTailCalls = CodeGenOpts.DisableTailCalls;
483 Options.TrapFuncName = CodeGenOpts.TrapFuncName;
484 Options.PositionIndependentExecutable = LangOpts.PIELevel != 0;
485 Options.EnableSegmentedStacks = CodeGenOpts.EnableSegmentedStacks;
487 TargetMachine *TM = TheTarget->createTargetMachine(Triple, TargetOpts.CPU,
488 FeaturesStr, Options,
491 if (CodeGenOpts.RelaxAll)
492 TM->setMCRelaxAll(true);
493 if (CodeGenOpts.SaveTempLabels)
494 TM->setMCSaveTempLabels(true);
495 if (CodeGenOpts.NoDwarf2CFIAsm)
496 TM->setMCUseCFI(false);
497 if (!CodeGenOpts.NoDwarfDirectoryAsm)
498 TM->setMCUseDwarfDirectory(true);
499 if (CodeGenOpts.NoExecStack)
500 TM->setMCNoExecStack(true);
505 bool EmitAssemblyHelper::AddEmitPasses(BackendAction Action,
506 formatted_raw_ostream &OS,
509 // Create the code generator passes.
510 PassManager *PM = getCodeGenPasses(TM);
513 llvm::Triple TargetTriple(TheModule->getTargetTriple());
514 TargetLibraryInfo *TLI = new TargetLibraryInfo(TargetTriple);
515 if (!CodeGenOpts.SimplifyLibCalls)
516 TLI->disableAllFunctions();
519 // Add Target specific analysis passes.
520 TM->addAnalysisPasses(*PM);
522 // Normal mode, emit a .s or .o file by running the code generator. Note,
523 // this also adds codegenerator level optimization passes.
524 TargetMachine::CodeGenFileType CGFT = TargetMachine::CGFT_AssemblyFile;
525 if (Action == Backend_EmitObj)
526 CGFT = TargetMachine::CGFT_ObjectFile;
527 else if (Action == Backend_EmitMCNull)
528 CGFT = TargetMachine::CGFT_Null;
530 assert(Action == Backend_EmitAssembly && "Invalid action!");
532 // Add ObjC ARC final-cleanup optimizations. This is done as part of the
533 // "codegen" passes so that it isn't run multiple times when there is
534 // inlining happening.
535 if (LangOpts.ObjCAutoRefCount &&
536 CodeGenOpts.OptimizationLevel > 0)
537 PM->add(createObjCARCContractPass());
539 if (TM->addPassesToEmitFile(*PM, OS, CGFT,
540 /*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
541 Diags.Report(diag::err_fe_unable_to_interface_with_target);
548 void EmitAssemblyHelper::EmitAssembly(BackendAction Action, raw_ostream *OS) {
549 TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : 0);
550 llvm::formatted_raw_ostream FormattedOS;
552 bool UsesCodeGen = (Action != Backend_EmitNothing &&
553 Action != Backend_EmitBC &&
554 Action != Backend_EmitLL);
555 TargetMachine *TM = CreateTargetMachine(UsesCodeGen);
556 if (UsesCodeGen && !TM) return;
557 llvm::OwningPtr<TargetMachine> TMOwner(CodeGenOpts.DisableFree ? 0 : TM);
561 case Backend_EmitNothing:
565 getPerModulePasses(TM)->add(createBitcodeWriterPass(*OS));
569 FormattedOS.setStream(*OS, formatted_raw_ostream::PRESERVE_STREAM);
570 getPerModulePasses(TM)->add(createPrintModulePass(&FormattedOS));
574 FormattedOS.setStream(*OS, formatted_raw_ostream::PRESERVE_STREAM);
575 if (!AddEmitPasses(Action, FormattedOS, TM))
579 // Before executing passes, print the final values of the LLVM options.
580 cl::PrintOptionValues();
582 // Run passes. For now we do all passes at once, but eventually we
583 // would like to have the option of streaming code generation.
585 if (PerFunctionPasses) {
586 PrettyStackTraceString CrashInfo("Per-function optimization");
588 PerFunctionPasses->doInitialization();
589 for (Module::iterator I = TheModule->begin(),
590 E = TheModule->end(); I != E; ++I)
591 if (!I->isDeclaration())
592 PerFunctionPasses->run(*I);
593 PerFunctionPasses->doFinalization();
596 if (PerModulePasses) {
597 PrettyStackTraceString CrashInfo("Per-module optimization passes");
598 PerModulePasses->run(*TheModule);
602 PrettyStackTraceString CrashInfo("Code generation");
603 CodeGenPasses->run(*TheModule);
607 void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
608 const CodeGenOptions &CGOpts,
609 const clang::TargetOptions &TOpts,
610 const LangOptions &LOpts,
612 BackendAction Action, raw_ostream *OS) {
613 EmitAssemblyHelper AsmHelper(Diags, CGOpts, TOpts, LOpts, M);
615 AsmHelper.EmitAssembly(Action, OS);