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