BackendUtil.cpp revision 263508
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" 39using namespace clang; 40using namespace llvm; 41 42namespace { 43 44class 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 57private: 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 107public: 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. 128class PassManagerBuilderWrapper : public PassManagerBuilder { 129public: 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; } 135private: 136 const CodeGenOptions &CGOpts; 137 const LangOptions &LangOpts; 138}; 139 140} 141 142static void addObjCARCAPElimPass(const PassManagerBuilder &Builder, PassManagerBase &PM) { 143 if (Builder.OptLevel > 0) 144 PM.add(createObjCARCAPElimPass()); 145} 146 147static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) { 148 if (Builder.OptLevel > 0) 149 PM.add(createObjCARCExpandPass()); 150} 151 152static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) { 153 if (Builder.OptLevel > 0) 154 PM.add(createObjCARCOptPass()); 155} 156 157static 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)); 163} 164 165static void addBoundsCheckingPass(const PassManagerBuilder &Builder, 166 PassManagerBase &PM) { 167 PM.add(createBoundsCheckingPass()); 168} 169 170static 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)); 186} 187 188static 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)); 195 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()); 206 } 207} 208 209static 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)); 215} 216 217static 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)); 223} 224 225void EmitAssemblyHelper::CreatePasses(TargetMachine *TM) { 226 unsigned OptLevel = CodeGenOpts.OptimizationLevel; 227 CodeGenOptions::InliningMethod Inlining = CodeGenOpts.getInlining(); 228 229 // Handle disabling of LLVM optimization, where we want to preserve the 230 // internal module before any optimization. 231 if (CodeGenOpts.DisableLLVMOpts) { 232 OptLevel = 0; 233 Inlining = CodeGenOpts.NoInlining; 234 } 235 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; 242 243 PMBuilder.DisableUnitAtATime = !CodeGenOpts.UnitAtATime; 244 PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops; 245 PMBuilder.RerollLoops = CodeGenOpts.RerollLoops; 246 247 if (!CodeGenOpts.SampleProfileFile.empty()) 248 PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible, 249 addSampleProfileLoaderPass); 250 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, 258 addObjCARCOptPass); 259 } 260 261 if (LangOpts.Sanitize.LocalBounds) { 262 PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate, 263 addBoundsCheckingPass); 264 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 265 addBoundsCheckingPass); 266 } 267 268 if (LangOpts.Sanitize.Address) { 269 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 270 addAddressSanitizerPasses); 271 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 272 addAddressSanitizerPasses); 273 } 274 275 if (LangOpts.Sanitize.Memory) { 276 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 277 addMemorySanitizerPass); 278 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 279 addMemorySanitizerPass); 280 } 281 282 if (LangOpts.Sanitize.Thread) { 283 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 284 addThreadSanitizerPass); 285 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 286 addThreadSanitizerPass); 287 } 288 289 if (LangOpts.Sanitize.DataFlow) { 290 PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast, 291 addDataFlowSanitizerPass); 292 PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 293 addDataFlowSanitizerPass); 294 } 295 296 // Figure out TargetLibraryInfo. 297 Triple TargetTriple(TheModule->getTargetTriple()); 298 PMBuilder.LibraryInfo = new TargetLibraryInfo(TargetTriple); 299 if (!CodeGenOpts.SimplifyLibCalls) 300 PMBuilder.LibraryInfo->disableAllFunctions(); 301 302 switch (Inlining) { 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 308 Threshold = 75; 309 else if (CodeGenOpts.OptimizeSize == 2) // -Oz 310 Threshold = 25; 311 else if (OptLevel > 2) 312 Threshold = 275; 313 PMBuilder.Inliner = createFunctionInliningPass(Threshold); 314 break; 315 } 316 case CodeGenOptions::OnlyAlwaysInlining: 317 // Respect always_inline. 318 if (OptLevel == 0) 319 // Do not insert lifetime intrinsics at -O0. 320 PMBuilder.Inliner = createAlwaysInlinerPass(false); 321 else 322 PMBuilder.Inliner = createAlwaysInlinerPass(); 323 break; 324 } 325 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); 331 332 // Set up the per-module pass manager. 333 PassManager *MPM = getPerModulePasses(TM); 334 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. 339 GCOVOptions Options; 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)); 350 } 351 352 PMBuilder.populateModulePassManager(*MPM); 353} 354 355TargetMachine *EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) { 356 // Create the TargetMachine for generating code. 357 std::string Error; 358 std::string Triple = TheModule->getTargetTriple(); 359 const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error); 360 if (!TheTarget) { 361 if (MustCreateTM) 362 Diags.Report(diag::err_fe_unable_to_create_target) << Error; 363 return 0; 364 } 365 366 // FIXME: Expose these capabilities via actual APIs!!!! Aside from just 367 // being gross, this is also totally broken if we ever care about 368 // concurrency. 369 370 TargetMachine::setAsmVerbosityDefault(CodeGenOpts.AsmVerbose); 371 372 TargetMachine::setFunctionSections(CodeGenOpts.FunctionSections); 373 TargetMachine::setDataSections (CodeGenOpts.DataSections); 374 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; 385 } else { 386 assert(CodeGenOpts.CodeModel.empty() && "Invalid code model!"); 387 CM = llvm::CodeModel::Default; 388 } 389 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()); 395 } 396 if (!CodeGenOpts.LimitFloatPrecision.empty()) { 397 BackendArgs.push_back("-limit-float-precision"); 398 BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str()); 399 } 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, 408 BackendArgs.data()); 409 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(); 418 } 419 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_; 425 } else { 426 assert(CodeGenOpts.RelocationModel == "dynamic-no-pic" && 427 "Invalid PIC model!"); 428 RM = llvm::Reloc::DynamicNoPIC; 429 } 430 431 CodeGenOpt::Level OptLevel = CodeGenOpt::Default; 432 switch (CodeGenOpts.OptimizationLevel) { 433 default: break; 434 case 0: OptLevel = CodeGenOpt::None; break; 435 case 3: OptLevel = CodeGenOpt::Aggressive; break; 436 } 437 438 llvm::TargetOptions Options; 439 440 // Set frame pointer elimination mode. 441 if (!CodeGenOpts.DisableFPElim) { 442 Options.NoFramePointerElim = false; 443 } else if (CodeGenOpts.OmitLeafFramePointer) { 444 Options.NoFramePointerElim = false; 445 } else { 446 Options.NoFramePointerElim = true; 447 } 448 449 if (CodeGenOpts.UseInitArray) 450 Options.UseInitArray = true; 451 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; 457 else { 458 assert(CodeGenOpts.FloatABI.empty() && "Invalid float abi!"); 459 Options.FloatABIType = llvm::FloatABI::Default; 460 } 461 462 // Set FP fusion mode. 463 switch (CodeGenOpts.getFPContractMode()) { 464 case CodeGenOptions::FPC_Off: 465 Options.AllowFPOpFusion = llvm::FPOpFusion::Strict; 466 break; 467 case CodeGenOptions::FPC_On: 468 Options.AllowFPOpFusion = llvm::FPOpFusion::Standard; 469 break; 470 case CodeGenOptions::FPC_Fast: 471 Options.AllowFPOpFusion = llvm::FPOpFusion::Fast; 472 break; 473 } 474 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; 486 487 TargetMachine *TM = TheTarget->createTargetMachine(Triple, TargetOpts.CPU, 488 FeaturesStr, Options, 489 RM, CM, OptLevel); 490 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); 501 502 return TM; 503} 504 505bool EmitAssemblyHelper::AddEmitPasses(BackendAction Action, 506 formatted_raw_ostream &OS, 507 TargetMachine *TM) { 508 509 // Create the code generator passes. 510 PassManager *PM = getCodeGenPasses(TM); 511 512 // Add LibraryInfo. 513 llvm::Triple TargetTriple(TheModule->getTargetTriple()); 514 TargetLibraryInfo *TLI = new TargetLibraryInfo(TargetTriple); 515 if (!CodeGenOpts.SimplifyLibCalls) 516 TLI->disableAllFunctions(); 517 PM->add(TLI); 518 519 // Add Target specific analysis passes. 520 TM->addAnalysisPasses(*PM); 521 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; 529 else 530 assert(Action == Backend_EmitAssembly && "Invalid action!"); 531 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()); 538 539 if (TM->addPassesToEmitFile(*PM, OS, CGFT, 540 /*DisableVerify=*/!CodeGenOpts.VerifyModule)) { 541 Diags.Report(diag::err_fe_unable_to_interface_with_target); 542 return false; 543 } 544 545 return true; 546} 547 548void EmitAssemblyHelper::EmitAssembly(BackendAction Action, raw_ostream *OS) { 549 TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : 0); 550 llvm::formatted_raw_ostream FormattedOS; 551 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); 558 CreatePasses(TM); 559 560 switch (Action) { 561 case Backend_EmitNothing: 562 break; 563 564 case Backend_EmitBC: 565 getPerModulePasses(TM)->add(createBitcodeWriterPass(*OS)); 566 break; 567 568 case Backend_EmitLL: 569 FormattedOS.setStream(*OS, formatted_raw_ostream::PRESERVE_STREAM); 570 getPerModulePasses(TM)->add(createPrintModulePass(&FormattedOS)); 571 break; 572 573 default: 574 FormattedOS.setStream(*OS, formatted_raw_ostream::PRESERVE_STREAM); 575 if (!AddEmitPasses(Action, FormattedOS, TM)) 576 return; 577 } 578 579 // Before executing passes, print the final values of the LLVM options. 580 cl::PrintOptionValues(); 581 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. 584 585 if (PerFunctionPasses) { 586 PrettyStackTraceString CrashInfo("Per-function optimization"); 587 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(); 594 } 595 596 if (PerModulePasses) { 597 PrettyStackTraceString CrashInfo("Per-module optimization passes"); 598 PerModulePasses->run(*TheModule); 599 } 600 601 if (CodeGenPasses) { 602 PrettyStackTraceString CrashInfo("Code generation"); 603 CodeGenPasses->run(*TheModule); 604 } 605} 606 607void clang::EmitBackendOutput(DiagnosticsEngine &Diags, 608 const CodeGenOptions &CGOpts, 609 const clang::TargetOptions &TOpts, 610 const LangOptions &LOpts, 611 Module *M, 612 BackendAction Action, raw_ostream *OS) { 613 EmitAssemblyHelper AsmHelper(Diags, CGOpts, TOpts, LOpts, M); 614 615 AsmHelper.EmitAssembly(Action, OS); 616} 617