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