1c1b1729bSDaniel Dunbar //===--- BackendUtil.cpp - LLVM Backend Utilities -------------------------===// 2c1b1729bSDaniel Dunbar // 3c1b1729bSDaniel Dunbar // The LLVM Compiler Infrastructure 4c1b1729bSDaniel Dunbar // 5c1b1729bSDaniel Dunbar // This file is distributed under the University of Illinois Open Source 6c1b1729bSDaniel Dunbar // License. See LICENSE.TXT for details. 7c1b1729bSDaniel Dunbar // 8c1b1729bSDaniel Dunbar //===----------------------------------------------------------------------===// 9c1b1729bSDaniel Dunbar 10c1b1729bSDaniel Dunbar #include "clang/CodeGen/BackendUtil.h" 11c1b1729bSDaniel Dunbar #include "clang/Basic/Diagnostic.h" 12c1b1729bSDaniel Dunbar #include "clang/Basic/TargetOptions.h" 138509824cSChandler Carruth #include "clang/Frontend/CodeGenOptions.h" 14c1b1729bSDaniel Dunbar #include "clang/Frontend/FrontendDiagnostic.h" 15c1b1729bSDaniel Dunbar #include "llvm/Module.h" 16c1b1729bSDaniel Dunbar #include "llvm/PassManager.h" 17c1b1729bSDaniel Dunbar #include "llvm/Assembly/PrintModulePass.h" 18c1b1729bSDaniel Dunbar #include "llvm/Bitcode/ReaderWriter.h" 19c1b1729bSDaniel Dunbar #include "llvm/CodeGen/RegAllocRegistry.h" 20c1b1729bSDaniel Dunbar #include "llvm/CodeGen/SchedulerRegistry.h" 21c1b1729bSDaniel Dunbar #include "llvm/Support/CommandLine.h" 22c1b1729bSDaniel Dunbar #include "llvm/Support/FormattedStream.h" 23c1b1729bSDaniel Dunbar #include "llvm/Support/PrettyStackTrace.h" 24c1b1729bSDaniel Dunbar #include "llvm/Support/StandardPasses.h" 25c1b1729bSDaniel Dunbar #include "llvm/Support/Timer.h" 26c1b1729bSDaniel Dunbar #include "llvm/Support/raw_ostream.h" 27c1b1729bSDaniel Dunbar #include "llvm/Target/SubtargetFeature.h" 28c1b1729bSDaniel Dunbar #include "llvm/Target/TargetData.h" 29d98cec5cSChris Lattner #include "llvm/Target/TargetLibraryInfo.h" 30c1b1729bSDaniel Dunbar #include "llvm/Target/TargetMachine.h" 31c1b1729bSDaniel Dunbar #include "llvm/Target/TargetOptions.h" 32c1b1729bSDaniel Dunbar #include "llvm/Target/TargetRegistry.h" 33c1b1729bSDaniel Dunbar using namespace clang; 34c1b1729bSDaniel Dunbar using namespace llvm; 35c1b1729bSDaniel Dunbar 36c1b1729bSDaniel Dunbar namespace { 37c1b1729bSDaniel Dunbar 38c1b1729bSDaniel Dunbar class EmitAssemblyHelper { 39c1b1729bSDaniel Dunbar Diagnostic &Diags; 40c1b1729bSDaniel Dunbar const CodeGenOptions &CodeGenOpts; 41c1b1729bSDaniel Dunbar const TargetOptions &TargetOpts; 42c1b1729bSDaniel Dunbar Module *TheModule; 43c1b1729bSDaniel Dunbar 44c1b1729bSDaniel Dunbar Timer CodeGenerationTime; 45c1b1729bSDaniel Dunbar 46195fa003SDaniel Dunbar mutable PassManager *CodeGenPasses; 47c1b1729bSDaniel Dunbar mutable PassManager *PerModulePasses; 48c1b1729bSDaniel Dunbar mutable FunctionPassManager *PerFunctionPasses; 49c1b1729bSDaniel Dunbar 50c1b1729bSDaniel Dunbar private: 51195fa003SDaniel Dunbar PassManager *getCodeGenPasses() const { 52c1b1729bSDaniel Dunbar if (!CodeGenPasses) { 53195fa003SDaniel Dunbar CodeGenPasses = new PassManager(); 54c1b1729bSDaniel Dunbar CodeGenPasses->add(new TargetData(TheModule)); 55c1b1729bSDaniel Dunbar } 56c1b1729bSDaniel Dunbar return CodeGenPasses; 57c1b1729bSDaniel Dunbar } 58c1b1729bSDaniel Dunbar 59c1b1729bSDaniel Dunbar PassManager *getPerModulePasses() const { 60c1b1729bSDaniel Dunbar if (!PerModulePasses) { 61c1b1729bSDaniel Dunbar PerModulePasses = new PassManager(); 62c1b1729bSDaniel Dunbar PerModulePasses->add(new TargetData(TheModule)); 63c1b1729bSDaniel Dunbar } 64c1b1729bSDaniel Dunbar return PerModulePasses; 65c1b1729bSDaniel Dunbar } 66c1b1729bSDaniel Dunbar 67c1b1729bSDaniel Dunbar FunctionPassManager *getPerFunctionPasses() const { 68c1b1729bSDaniel Dunbar if (!PerFunctionPasses) { 69c1b1729bSDaniel Dunbar PerFunctionPasses = new FunctionPassManager(TheModule); 70c1b1729bSDaniel Dunbar PerFunctionPasses->add(new TargetData(TheModule)); 71c1b1729bSDaniel Dunbar } 72c1b1729bSDaniel Dunbar return PerFunctionPasses; 73c1b1729bSDaniel Dunbar } 74c1b1729bSDaniel Dunbar 75c1b1729bSDaniel Dunbar void CreatePasses(); 76c1b1729bSDaniel Dunbar 77c1b1729bSDaniel Dunbar /// AddEmitPasses - Add passes necessary to emit assembly or LLVM IR. 78c1b1729bSDaniel Dunbar /// 79c1b1729bSDaniel Dunbar /// \return True on success. 80c1b1729bSDaniel Dunbar bool AddEmitPasses(BackendAction Action, formatted_raw_ostream &OS); 81c1b1729bSDaniel Dunbar 82c1b1729bSDaniel Dunbar public: 83c1b1729bSDaniel Dunbar EmitAssemblyHelper(Diagnostic &_Diags, 84c1b1729bSDaniel Dunbar const CodeGenOptions &CGOpts, const TargetOptions &TOpts, 85c1b1729bSDaniel Dunbar Module *M) 86c1b1729bSDaniel Dunbar : Diags(_Diags), CodeGenOpts(CGOpts), TargetOpts(TOpts), 87c1b1729bSDaniel Dunbar TheModule(M), CodeGenerationTime("Code Generation Time"), 88c1b1729bSDaniel Dunbar CodeGenPasses(0), PerModulePasses(0), PerFunctionPasses(0) {} 89c1b1729bSDaniel Dunbar 90c1b1729bSDaniel Dunbar ~EmitAssemblyHelper() { 91c1b1729bSDaniel Dunbar delete CodeGenPasses; 92c1b1729bSDaniel Dunbar delete PerModulePasses; 93c1b1729bSDaniel Dunbar delete PerFunctionPasses; 94c1b1729bSDaniel Dunbar } 95c1b1729bSDaniel Dunbar 96c1b1729bSDaniel Dunbar void EmitAssembly(BackendAction Action, raw_ostream *OS); 97c1b1729bSDaniel Dunbar }; 98c1b1729bSDaniel Dunbar 99c1b1729bSDaniel Dunbar } 100c1b1729bSDaniel Dunbar 101c1b1729bSDaniel Dunbar void EmitAssemblyHelper::CreatePasses() { 102c1b1729bSDaniel Dunbar unsigned OptLevel = CodeGenOpts.OptimizationLevel; 103c1b1729bSDaniel Dunbar CodeGenOptions::InliningMethod Inlining = CodeGenOpts.Inlining; 104c1b1729bSDaniel Dunbar 105c1b1729bSDaniel Dunbar // Handle disabling of LLVM optimization, where we want to preserve the 106c1b1729bSDaniel Dunbar // internal module before any optimization. 107c1b1729bSDaniel Dunbar if (CodeGenOpts.DisableLLVMOpts) { 108c1b1729bSDaniel Dunbar OptLevel = 0; 109c1b1729bSDaniel Dunbar Inlining = CodeGenOpts.NoInlining; 110c1b1729bSDaniel Dunbar } 111c1b1729bSDaniel Dunbar 112d98cec5cSChris Lattner FunctionPassManager *FPM = getPerFunctionPasses(); 113d98cec5cSChris Lattner 114*fa222dfbSChris Lattner TargetLibraryInfo *TLI = 115*fa222dfbSChris Lattner new TargetLibraryInfo(Triple(TheModule->getTargetTriple())); 116*fa222dfbSChris Lattner if (!CodeGenOpts.SimplifyLibCalls) 117*fa222dfbSChris Lattner TLI->disableAllFunctions(); 118*fa222dfbSChris Lattner FPM->add(TLI); 119d98cec5cSChris Lattner 120c1b1729bSDaniel Dunbar // In -O0 if checking is disabled, we don't even have per-function passes. 121c1b1729bSDaniel Dunbar if (CodeGenOpts.VerifyModule) 122d98cec5cSChris Lattner FPM->add(createVerifierPass()); 123c1b1729bSDaniel Dunbar 124c1b1729bSDaniel Dunbar // Assume that standard function passes aren't run for -O0. 125c1b1729bSDaniel Dunbar if (OptLevel > 0) 126d98cec5cSChris Lattner llvm::createStandardFunctionPasses(FPM, OptLevel); 127c1b1729bSDaniel Dunbar 128c1b1729bSDaniel Dunbar llvm::Pass *InliningPass = 0; 129c1b1729bSDaniel Dunbar switch (Inlining) { 130c1b1729bSDaniel Dunbar case CodeGenOptions::NoInlining: break; 131c1b1729bSDaniel Dunbar case CodeGenOptions::NormalInlining: { 132c1b1729bSDaniel Dunbar // Set the inline threshold following llvm-gcc. 133c1b1729bSDaniel Dunbar // 134c1b1729bSDaniel Dunbar // FIXME: Derive these constants in a principled fashion. 135c1b1729bSDaniel Dunbar unsigned Threshold = 225; 136c1b1729bSDaniel Dunbar if (CodeGenOpts.OptimizeSize) 137c1b1729bSDaniel Dunbar Threshold = 75; 138c1b1729bSDaniel Dunbar else if (OptLevel > 2) 139c1b1729bSDaniel Dunbar Threshold = 275; 140c1b1729bSDaniel Dunbar InliningPass = createFunctionInliningPass(Threshold); 141c1b1729bSDaniel Dunbar break; 142c1b1729bSDaniel Dunbar } 143c1b1729bSDaniel Dunbar case CodeGenOptions::OnlyAlwaysInlining: 144c1b1729bSDaniel Dunbar InliningPass = createAlwaysInlinerPass(); // Respect always_inline 145c1b1729bSDaniel Dunbar break; 146c1b1729bSDaniel Dunbar } 147c1b1729bSDaniel Dunbar 148d98cec5cSChris Lattner PassManager *MPM = getPerModulePasses(); 149d98cec5cSChris Lattner 150*fa222dfbSChris Lattner TLI = new TargetLibraryInfo(Triple(TheModule->getTargetTriple())); 151*fa222dfbSChris Lattner if (!CodeGenOpts.SimplifyLibCalls) 152*fa222dfbSChris Lattner TLI->disableAllFunctions(); 153*fa222dfbSChris Lattner MPM->add(TLI); 154d98cec5cSChris Lattner 155c1b1729bSDaniel Dunbar // For now we always create per module passes. 156d98cec5cSChris Lattner llvm::createStandardModulePasses(MPM, OptLevel, 157c1b1729bSDaniel Dunbar CodeGenOpts.OptimizeSize, 158c1b1729bSDaniel Dunbar CodeGenOpts.UnitAtATime, 159c1b1729bSDaniel Dunbar CodeGenOpts.UnrollLoops, 160c1b1729bSDaniel Dunbar CodeGenOpts.SimplifyLibCalls, 161c1b1729bSDaniel Dunbar /*HaveExceptions=*/true, 162c1b1729bSDaniel Dunbar InliningPass); 163c1b1729bSDaniel Dunbar } 164c1b1729bSDaniel Dunbar 165c1b1729bSDaniel Dunbar bool EmitAssemblyHelper::AddEmitPasses(BackendAction Action, 166c1b1729bSDaniel Dunbar formatted_raw_ostream &OS) { 167c1b1729bSDaniel Dunbar // Create the TargetMachine for generating code. 168c1b1729bSDaniel Dunbar std::string Error; 169c1b1729bSDaniel Dunbar std::string Triple = TheModule->getTargetTriple(); 170c1b1729bSDaniel Dunbar const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error); 171c1b1729bSDaniel Dunbar if (!TheTarget) { 172c1b1729bSDaniel Dunbar Diags.Report(diag::err_fe_unable_to_create_target) << Error; 173c1b1729bSDaniel Dunbar return false; 174c1b1729bSDaniel Dunbar } 175c1b1729bSDaniel Dunbar 176c1b1729bSDaniel Dunbar // FIXME: Expose these capabilities via actual APIs!!!! Aside from just 177c1b1729bSDaniel Dunbar // being gross, this is also totally broken if we ever care about 178c1b1729bSDaniel Dunbar // concurrency. 179bb7ac52eSDaniel Dunbar 180bb7ac52eSDaniel Dunbar // Set frame pointer elimination mode. 181bb7ac52eSDaniel Dunbar if (!CodeGenOpts.DisableFPElim) { 182bb7ac52eSDaniel Dunbar llvm::NoFramePointerElim = false; 183bb7ac52eSDaniel Dunbar llvm::NoFramePointerElimNonLeaf = false; 184bb7ac52eSDaniel Dunbar } else if (CodeGenOpts.OmitLeafFramePointer) { 185bb7ac52eSDaniel Dunbar llvm::NoFramePointerElim = false; 186bb7ac52eSDaniel Dunbar llvm::NoFramePointerElimNonLeaf = true; 187bb7ac52eSDaniel Dunbar } else { 188bb7ac52eSDaniel Dunbar llvm::NoFramePointerElim = true; 189bb7ac52eSDaniel Dunbar llvm::NoFramePointerElimNonLeaf = true; 190bb7ac52eSDaniel Dunbar } 191bb7ac52eSDaniel Dunbar 192bb7ac52eSDaniel Dunbar // Set float ABI type. 193c1b1729bSDaniel Dunbar if (CodeGenOpts.FloatABI == "soft") 194c1b1729bSDaniel Dunbar llvm::FloatABIType = llvm::FloatABI::Soft; 195c1b1729bSDaniel Dunbar else if (CodeGenOpts.FloatABI == "hard") 196c1b1729bSDaniel Dunbar llvm::FloatABIType = llvm::FloatABI::Hard; 197c1b1729bSDaniel Dunbar else { 198c1b1729bSDaniel Dunbar assert(CodeGenOpts.FloatABI.empty() && "Invalid float abi!"); 199c1b1729bSDaniel Dunbar llvm::FloatABIType = llvm::FloatABI::Default; 200c1b1729bSDaniel Dunbar } 201bb7ac52eSDaniel Dunbar 20242254262SPeter Collingbourne llvm::LessPreciseFPMADOption = CodeGenOpts.LessPreciseFPMAD; 2030ba5ac85SPeter Collingbourne llvm::NoInfsFPMath = CodeGenOpts.NoInfsFPMath; 2040ba5ac85SPeter Collingbourne llvm::NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath; 205c1b1729bSDaniel Dunbar NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS; 206b8d9995cSPeter Collingbourne llvm::UnsafeFPMath = CodeGenOpts.UnsafeFPMath; 207c1b1729bSDaniel Dunbar llvm::UseSoftFloat = CodeGenOpts.SoftFloat; 208c1b1729bSDaniel Dunbar UnwindTablesMandatory = CodeGenOpts.UnwindTables; 209c1b1729bSDaniel Dunbar 210c1b1729bSDaniel Dunbar TargetMachine::setAsmVerbosityDefault(CodeGenOpts.AsmVerbose); 211c1b1729bSDaniel Dunbar 212c1b1729bSDaniel Dunbar TargetMachine::setFunctionSections(CodeGenOpts.FunctionSections); 213c1b1729bSDaniel Dunbar TargetMachine::setDataSections (CodeGenOpts.DataSections); 214c1b1729bSDaniel Dunbar 215c1b1729bSDaniel Dunbar // FIXME: Parse this earlier. 216c1b1729bSDaniel Dunbar if (CodeGenOpts.RelocationModel == "static") { 217c1b1729bSDaniel Dunbar TargetMachine::setRelocationModel(llvm::Reloc::Static); 218c1b1729bSDaniel Dunbar } else if (CodeGenOpts.RelocationModel == "pic") { 219c1b1729bSDaniel Dunbar TargetMachine::setRelocationModel(llvm::Reloc::PIC_); 220c1b1729bSDaniel Dunbar } else { 221c1b1729bSDaniel Dunbar assert(CodeGenOpts.RelocationModel == "dynamic-no-pic" && 222c1b1729bSDaniel Dunbar "Invalid PIC model!"); 223c1b1729bSDaniel Dunbar TargetMachine::setRelocationModel(llvm::Reloc::DynamicNoPIC); 224c1b1729bSDaniel Dunbar } 225c1b1729bSDaniel Dunbar // FIXME: Parse this earlier. 226c1b1729bSDaniel Dunbar if (CodeGenOpts.CodeModel == "small") { 227c1b1729bSDaniel Dunbar TargetMachine::setCodeModel(llvm::CodeModel::Small); 228c1b1729bSDaniel Dunbar } else if (CodeGenOpts.CodeModel == "kernel") { 229c1b1729bSDaniel Dunbar TargetMachine::setCodeModel(llvm::CodeModel::Kernel); 230c1b1729bSDaniel Dunbar } else if (CodeGenOpts.CodeModel == "medium") { 231c1b1729bSDaniel Dunbar TargetMachine::setCodeModel(llvm::CodeModel::Medium); 232c1b1729bSDaniel Dunbar } else if (CodeGenOpts.CodeModel == "large") { 233c1b1729bSDaniel Dunbar TargetMachine::setCodeModel(llvm::CodeModel::Large); 234c1b1729bSDaniel Dunbar } else { 235c1b1729bSDaniel Dunbar assert(CodeGenOpts.CodeModel.empty() && "Invalid code model!"); 236c1b1729bSDaniel Dunbar TargetMachine::setCodeModel(llvm::CodeModel::Default); 237c1b1729bSDaniel Dunbar } 238c1b1729bSDaniel Dunbar 239c1b1729bSDaniel Dunbar std::vector<const char *> BackendArgs; 240c1b1729bSDaniel Dunbar BackendArgs.push_back("clang"); // Fake program name. 241c1b1729bSDaniel Dunbar if (!CodeGenOpts.DebugPass.empty()) { 242c1b1729bSDaniel Dunbar BackendArgs.push_back("-debug-pass"); 243c1b1729bSDaniel Dunbar BackendArgs.push_back(CodeGenOpts.DebugPass.c_str()); 244c1b1729bSDaniel Dunbar } 245c1b1729bSDaniel Dunbar if (!CodeGenOpts.LimitFloatPrecision.empty()) { 246c1b1729bSDaniel Dunbar BackendArgs.push_back("-limit-float-precision"); 247c1b1729bSDaniel Dunbar BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str()); 248c1b1729bSDaniel Dunbar } 249c1b1729bSDaniel Dunbar if (llvm::TimePassesIsEnabled) 250c1b1729bSDaniel Dunbar BackendArgs.push_back("-time-passes"); 251c1b1729bSDaniel Dunbar BackendArgs.push_back(0); 252c1b1729bSDaniel Dunbar llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1, 253c1b1729bSDaniel Dunbar const_cast<char **>(&BackendArgs[0])); 254c1b1729bSDaniel Dunbar 255c1b1729bSDaniel Dunbar std::string FeaturesStr; 256c1b1729bSDaniel Dunbar if (TargetOpts.CPU.size() || TargetOpts.Features.size()) { 257c1b1729bSDaniel Dunbar SubtargetFeatures Features; 258c1b1729bSDaniel Dunbar Features.setCPU(TargetOpts.CPU); 259c1b1729bSDaniel Dunbar for (std::vector<std::string>::const_iterator 260c1b1729bSDaniel Dunbar it = TargetOpts.Features.begin(), 261c1b1729bSDaniel Dunbar ie = TargetOpts.Features.end(); it != ie; ++it) 262c1b1729bSDaniel Dunbar Features.AddFeature(*it); 263c1b1729bSDaniel Dunbar FeaturesStr = Features.getString(); 264c1b1729bSDaniel Dunbar } 265c1b1729bSDaniel Dunbar TargetMachine *TM = TheTarget->createTargetMachine(Triple, FeaturesStr); 266c1b1729bSDaniel Dunbar 267c1b1729bSDaniel Dunbar if (CodeGenOpts.RelaxAll) 268c1b1729bSDaniel Dunbar TM->setMCRelaxAll(true); 269c1b1729bSDaniel Dunbar 270c1b1729bSDaniel Dunbar // Create the code generator passes. 271195fa003SDaniel Dunbar PassManager *PM = getCodeGenPasses(); 272c1b1729bSDaniel Dunbar CodeGenOpt::Level OptLevel = CodeGenOpt::Default; 273c1b1729bSDaniel Dunbar 274c1b1729bSDaniel Dunbar switch (CodeGenOpts.OptimizationLevel) { 275c1b1729bSDaniel Dunbar default: break; 276c1b1729bSDaniel Dunbar case 0: OptLevel = CodeGenOpt::None; break; 277c1b1729bSDaniel Dunbar case 3: OptLevel = CodeGenOpt::Aggressive; break; 278c1b1729bSDaniel Dunbar } 279c1b1729bSDaniel Dunbar 280c1b1729bSDaniel Dunbar // Normal mode, emit a .s or .o file by running the code generator. Note, 281c1b1729bSDaniel Dunbar // this also adds codegenerator level optimization passes. 282c1b1729bSDaniel Dunbar TargetMachine::CodeGenFileType CGFT = TargetMachine::CGFT_AssemblyFile; 283c1b1729bSDaniel Dunbar if (Action == Backend_EmitObj) 284c1b1729bSDaniel Dunbar CGFT = TargetMachine::CGFT_ObjectFile; 285c1b1729bSDaniel Dunbar else if (Action == Backend_EmitMCNull) 286c1b1729bSDaniel Dunbar CGFT = TargetMachine::CGFT_Null; 287c1b1729bSDaniel Dunbar else 288c1b1729bSDaniel Dunbar assert(Action == Backend_EmitAssembly && "Invalid action!"); 289c1b1729bSDaniel Dunbar if (TM->addPassesToEmitFile(*PM, OS, CGFT, OptLevel, 290c1b1729bSDaniel Dunbar /*DisableVerify=*/!CodeGenOpts.VerifyModule)) { 291c1b1729bSDaniel Dunbar Diags.Report(diag::err_fe_unable_to_interface_with_target); 292c1b1729bSDaniel Dunbar return false; 293c1b1729bSDaniel Dunbar } 294c1b1729bSDaniel Dunbar 295c1b1729bSDaniel Dunbar return true; 296c1b1729bSDaniel Dunbar } 297c1b1729bSDaniel Dunbar 298c1b1729bSDaniel Dunbar void EmitAssemblyHelper::EmitAssembly(BackendAction Action, raw_ostream *OS) { 299c1b1729bSDaniel Dunbar TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : 0); 300c1b1729bSDaniel Dunbar llvm::formatted_raw_ostream FormattedOS; 301c1b1729bSDaniel Dunbar 302c1b1729bSDaniel Dunbar CreatePasses(); 303c1b1729bSDaniel Dunbar switch (Action) { 304c1b1729bSDaniel Dunbar case Backend_EmitNothing: 305c1b1729bSDaniel Dunbar break; 306c1b1729bSDaniel Dunbar 307c1b1729bSDaniel Dunbar case Backend_EmitBC: 308c1b1729bSDaniel Dunbar getPerModulePasses()->add(createBitcodeWriterPass(*OS)); 309c1b1729bSDaniel Dunbar break; 310c1b1729bSDaniel Dunbar 311c1b1729bSDaniel Dunbar case Backend_EmitLL: 312c1b1729bSDaniel Dunbar FormattedOS.setStream(*OS, formatted_raw_ostream::PRESERVE_STREAM); 313c1b1729bSDaniel Dunbar getPerModulePasses()->add(createPrintModulePass(&FormattedOS)); 314c1b1729bSDaniel Dunbar break; 315c1b1729bSDaniel Dunbar 316c1b1729bSDaniel Dunbar default: 317c1b1729bSDaniel Dunbar FormattedOS.setStream(*OS, formatted_raw_ostream::PRESERVE_STREAM); 318c1b1729bSDaniel Dunbar if (!AddEmitPasses(Action, FormattedOS)) 319c1b1729bSDaniel Dunbar return; 320c1b1729bSDaniel Dunbar } 321c1b1729bSDaniel Dunbar 322c1b1729bSDaniel Dunbar // Run passes. For now we do all passes at once, but eventually we 323c1b1729bSDaniel Dunbar // would like to have the option of streaming code generation. 324c1b1729bSDaniel Dunbar 325c1b1729bSDaniel Dunbar if (PerFunctionPasses) { 326c1b1729bSDaniel Dunbar PrettyStackTraceString CrashInfo("Per-function optimization"); 327c1b1729bSDaniel Dunbar 328c1b1729bSDaniel Dunbar PerFunctionPasses->doInitialization(); 329c1b1729bSDaniel Dunbar for (Module::iterator I = TheModule->begin(), 330c1b1729bSDaniel Dunbar E = TheModule->end(); I != E; ++I) 331c1b1729bSDaniel Dunbar if (!I->isDeclaration()) 332c1b1729bSDaniel Dunbar PerFunctionPasses->run(*I); 333c1b1729bSDaniel Dunbar PerFunctionPasses->doFinalization(); 334c1b1729bSDaniel Dunbar } 335c1b1729bSDaniel Dunbar 336c1b1729bSDaniel Dunbar if (PerModulePasses) { 337c1b1729bSDaniel Dunbar PrettyStackTraceString CrashInfo("Per-module optimization passes"); 338c1b1729bSDaniel Dunbar PerModulePasses->run(*TheModule); 339c1b1729bSDaniel Dunbar } 340c1b1729bSDaniel Dunbar 341c1b1729bSDaniel Dunbar if (CodeGenPasses) { 342c1b1729bSDaniel Dunbar PrettyStackTraceString CrashInfo("Code generation"); 343195fa003SDaniel Dunbar CodeGenPasses->run(*TheModule); 344c1b1729bSDaniel Dunbar } 345c1b1729bSDaniel Dunbar } 346c1b1729bSDaniel Dunbar 347c1b1729bSDaniel Dunbar void clang::EmitBackendOutput(Diagnostic &Diags, const CodeGenOptions &CGOpts, 348c1b1729bSDaniel Dunbar const TargetOptions &TOpts, Module *M, 349c1b1729bSDaniel Dunbar BackendAction Action, raw_ostream *OS) { 350c1b1729bSDaniel Dunbar EmitAssemblyHelper AsmHelper(Diags, CGOpts, TOpts, M); 351c1b1729bSDaniel Dunbar 352c1b1729bSDaniel Dunbar AsmHelper.EmitAssembly(Action, OS); 353c1b1729bSDaniel Dunbar } 354