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" 33207bce31SNick Lewycky #include "llvm/Transforms/Instrumentation.h" 34c1b1729bSDaniel Dunbar using namespace clang; 35c1b1729bSDaniel Dunbar using namespace llvm; 36c1b1729bSDaniel Dunbar 37c1b1729bSDaniel Dunbar namespace { 38c1b1729bSDaniel Dunbar 39c1b1729bSDaniel Dunbar class EmitAssemblyHelper { 40c1b1729bSDaniel Dunbar Diagnostic &Diags; 41c1b1729bSDaniel Dunbar const CodeGenOptions &CodeGenOpts; 42c1b1729bSDaniel Dunbar const TargetOptions &TargetOpts; 43c1b1729bSDaniel Dunbar Module *TheModule; 44c1b1729bSDaniel Dunbar 45c1b1729bSDaniel Dunbar Timer CodeGenerationTime; 46c1b1729bSDaniel Dunbar 47195fa003SDaniel Dunbar mutable PassManager *CodeGenPasses; 48c1b1729bSDaniel Dunbar mutable PassManager *PerModulePasses; 49c1b1729bSDaniel Dunbar mutable FunctionPassManager *PerFunctionPasses; 50c1b1729bSDaniel Dunbar 51c1b1729bSDaniel Dunbar private: 52195fa003SDaniel Dunbar PassManager *getCodeGenPasses() const { 53c1b1729bSDaniel Dunbar if (!CodeGenPasses) { 54195fa003SDaniel Dunbar CodeGenPasses = new PassManager(); 55c1b1729bSDaniel Dunbar CodeGenPasses->add(new TargetData(TheModule)); 56c1b1729bSDaniel Dunbar } 57c1b1729bSDaniel Dunbar return CodeGenPasses; 58c1b1729bSDaniel Dunbar } 59c1b1729bSDaniel Dunbar 60c1b1729bSDaniel Dunbar PassManager *getPerModulePasses() const { 61c1b1729bSDaniel Dunbar if (!PerModulePasses) { 62c1b1729bSDaniel Dunbar PerModulePasses = new PassManager(); 63c1b1729bSDaniel Dunbar PerModulePasses->add(new TargetData(TheModule)); 64c1b1729bSDaniel Dunbar } 65c1b1729bSDaniel Dunbar return PerModulePasses; 66c1b1729bSDaniel Dunbar } 67c1b1729bSDaniel Dunbar 68c1b1729bSDaniel Dunbar FunctionPassManager *getPerFunctionPasses() const { 69c1b1729bSDaniel Dunbar if (!PerFunctionPasses) { 70c1b1729bSDaniel Dunbar PerFunctionPasses = new FunctionPassManager(TheModule); 71c1b1729bSDaniel Dunbar PerFunctionPasses->add(new TargetData(TheModule)); 72c1b1729bSDaniel Dunbar } 73c1b1729bSDaniel Dunbar return PerFunctionPasses; 74c1b1729bSDaniel Dunbar } 75c1b1729bSDaniel Dunbar 76c1b1729bSDaniel Dunbar void CreatePasses(); 77c1b1729bSDaniel Dunbar 78c1b1729bSDaniel Dunbar /// AddEmitPasses - Add passes necessary to emit assembly or LLVM IR. 79c1b1729bSDaniel Dunbar /// 80c1b1729bSDaniel Dunbar /// \return True on success. 81c1b1729bSDaniel Dunbar bool AddEmitPasses(BackendAction Action, formatted_raw_ostream &OS); 82c1b1729bSDaniel Dunbar 83c1b1729bSDaniel Dunbar public: 84c1b1729bSDaniel Dunbar EmitAssemblyHelper(Diagnostic &_Diags, 85c1b1729bSDaniel Dunbar const CodeGenOptions &CGOpts, const TargetOptions &TOpts, 86c1b1729bSDaniel Dunbar Module *M) 87c1b1729bSDaniel Dunbar : Diags(_Diags), CodeGenOpts(CGOpts), TargetOpts(TOpts), 88c1b1729bSDaniel Dunbar TheModule(M), CodeGenerationTime("Code Generation Time"), 89c1b1729bSDaniel Dunbar CodeGenPasses(0), PerModulePasses(0), PerFunctionPasses(0) {} 90c1b1729bSDaniel Dunbar 91c1b1729bSDaniel Dunbar ~EmitAssemblyHelper() { 92c1b1729bSDaniel Dunbar delete CodeGenPasses; 93c1b1729bSDaniel Dunbar delete PerModulePasses; 94c1b1729bSDaniel Dunbar delete PerFunctionPasses; 95c1b1729bSDaniel Dunbar } 96c1b1729bSDaniel Dunbar 97c1b1729bSDaniel Dunbar void EmitAssembly(BackendAction Action, raw_ostream *OS); 98c1b1729bSDaniel Dunbar }; 99c1b1729bSDaniel Dunbar 100c1b1729bSDaniel Dunbar } 101c1b1729bSDaniel Dunbar 102c1b1729bSDaniel Dunbar void EmitAssemblyHelper::CreatePasses() { 103c1b1729bSDaniel Dunbar unsigned OptLevel = CodeGenOpts.OptimizationLevel; 104c1b1729bSDaniel Dunbar CodeGenOptions::InliningMethod Inlining = CodeGenOpts.Inlining; 105c1b1729bSDaniel Dunbar 106c1b1729bSDaniel Dunbar // Handle disabling of LLVM optimization, where we want to preserve the 107c1b1729bSDaniel Dunbar // internal module before any optimization. 108c1b1729bSDaniel Dunbar if (CodeGenOpts.DisableLLVMOpts) { 109c1b1729bSDaniel Dunbar OptLevel = 0; 110c1b1729bSDaniel Dunbar Inlining = CodeGenOpts.NoInlining; 111c1b1729bSDaniel Dunbar } 112c1b1729bSDaniel Dunbar 113d98cec5cSChris Lattner FunctionPassManager *FPM = getPerFunctionPasses(); 114d98cec5cSChris Lattner 115fa222dfbSChris Lattner TargetLibraryInfo *TLI = 116fa222dfbSChris Lattner new TargetLibraryInfo(Triple(TheModule->getTargetTriple())); 117fa222dfbSChris Lattner if (!CodeGenOpts.SimplifyLibCalls) 118fa222dfbSChris Lattner TLI->disableAllFunctions(); 119fa222dfbSChris Lattner FPM->add(TLI); 120d98cec5cSChris Lattner 121c1b1729bSDaniel Dunbar // In -O0 if checking is disabled, we don't even have per-function passes. 122c1b1729bSDaniel Dunbar if (CodeGenOpts.VerifyModule) 123d98cec5cSChris Lattner FPM->add(createVerifierPass()); 124c1b1729bSDaniel Dunbar 125c1b1729bSDaniel Dunbar // Assume that standard function passes aren't run for -O0. 126c1b1729bSDaniel Dunbar if (OptLevel > 0) 127d98cec5cSChris Lattner llvm::createStandardFunctionPasses(FPM, OptLevel); 128c1b1729bSDaniel Dunbar 129c1b1729bSDaniel Dunbar llvm::Pass *InliningPass = 0; 130c1b1729bSDaniel Dunbar switch (Inlining) { 131c1b1729bSDaniel Dunbar case CodeGenOptions::NoInlining: break; 132c1b1729bSDaniel Dunbar case CodeGenOptions::NormalInlining: { 133c1b1729bSDaniel Dunbar // Set the inline threshold following llvm-gcc. 134c1b1729bSDaniel Dunbar // 135c1b1729bSDaniel Dunbar // FIXME: Derive these constants in a principled fashion. 136c1b1729bSDaniel Dunbar unsigned Threshold = 225; 1379a5f84faSBob Wilson if (CodeGenOpts.OptimizeSize == 1) //-Os 138c1b1729bSDaniel Dunbar Threshold = 75; 1399a5f84faSBob Wilson else if (CodeGenOpts.OptimizeSize == 2) //-Oz 1409a5f84faSBob Wilson Threshold = 25; 141c1b1729bSDaniel Dunbar else if (OptLevel > 2) 142c1b1729bSDaniel Dunbar Threshold = 275; 143c1b1729bSDaniel Dunbar InliningPass = createFunctionInliningPass(Threshold); 144c1b1729bSDaniel Dunbar break; 145c1b1729bSDaniel Dunbar } 146c1b1729bSDaniel Dunbar case CodeGenOptions::OnlyAlwaysInlining: 147c1b1729bSDaniel Dunbar InliningPass = createAlwaysInlinerPass(); // Respect always_inline 148c1b1729bSDaniel Dunbar break; 149c1b1729bSDaniel Dunbar } 150c1b1729bSDaniel Dunbar 151d98cec5cSChris Lattner PassManager *MPM = getPerModulePasses(); 152d98cec5cSChris Lattner 153fa222dfbSChris Lattner TLI = new TargetLibraryInfo(Triple(TheModule->getTargetTriple())); 154fa222dfbSChris Lattner if (!CodeGenOpts.SimplifyLibCalls) 155fa222dfbSChris Lattner TLI->disableAllFunctions(); 156fa222dfbSChris Lattner MPM->add(TLI); 157d98cec5cSChris Lattner 158207bce31SNick Lewycky if (CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes) { 159207bce31SNick Lewycky MPM->add(createGCOVProfilerPass(CodeGenOpts.EmitGcovNotes, 160207bce31SNick Lewycky CodeGenOpts.EmitGcovArcs)); 161207bce31SNick Lewycky if (!CodeGenOpts.DebugInfo) 162207bce31SNick Lewycky MPM->add(createStripSymbolsPass(true)); 163207bce31SNick Lewycky } 164207bce31SNick Lewycky 165c1b1729bSDaniel Dunbar // For now we always create per module passes. 166d98cec5cSChris Lattner llvm::createStandardModulePasses(MPM, OptLevel, 167c1b1729bSDaniel Dunbar CodeGenOpts.OptimizeSize, 168c1b1729bSDaniel Dunbar CodeGenOpts.UnitAtATime, 169c1b1729bSDaniel Dunbar CodeGenOpts.UnrollLoops, 170c1b1729bSDaniel Dunbar CodeGenOpts.SimplifyLibCalls, 171c1b1729bSDaniel Dunbar /*HaveExceptions=*/true, 172c1b1729bSDaniel Dunbar InliningPass); 173c1b1729bSDaniel Dunbar } 174c1b1729bSDaniel Dunbar 175c1b1729bSDaniel Dunbar bool EmitAssemblyHelper::AddEmitPasses(BackendAction Action, 176c1b1729bSDaniel Dunbar formatted_raw_ostream &OS) { 177c1b1729bSDaniel Dunbar // Create the TargetMachine for generating code. 178c1b1729bSDaniel Dunbar std::string Error; 179c1b1729bSDaniel Dunbar std::string Triple = TheModule->getTargetTriple(); 180c1b1729bSDaniel Dunbar const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error); 181c1b1729bSDaniel Dunbar if (!TheTarget) { 182c1b1729bSDaniel Dunbar Diags.Report(diag::err_fe_unable_to_create_target) << Error; 183c1b1729bSDaniel Dunbar return false; 184c1b1729bSDaniel Dunbar } 185c1b1729bSDaniel Dunbar 186c1b1729bSDaniel Dunbar // FIXME: Expose these capabilities via actual APIs!!!! Aside from just 187c1b1729bSDaniel Dunbar // being gross, this is also totally broken if we ever care about 188c1b1729bSDaniel Dunbar // concurrency. 189bb7ac52eSDaniel Dunbar 190bb7ac52eSDaniel Dunbar // Set frame pointer elimination mode. 191bb7ac52eSDaniel Dunbar if (!CodeGenOpts.DisableFPElim) { 192bb7ac52eSDaniel Dunbar llvm::NoFramePointerElim = false; 193bb7ac52eSDaniel Dunbar llvm::NoFramePointerElimNonLeaf = false; 194bb7ac52eSDaniel Dunbar } else if (CodeGenOpts.OmitLeafFramePointer) { 195bb7ac52eSDaniel Dunbar llvm::NoFramePointerElim = false; 196bb7ac52eSDaniel Dunbar llvm::NoFramePointerElimNonLeaf = true; 197bb7ac52eSDaniel Dunbar } else { 198bb7ac52eSDaniel Dunbar llvm::NoFramePointerElim = true; 199bb7ac52eSDaniel Dunbar llvm::NoFramePointerElimNonLeaf = true; 200bb7ac52eSDaniel Dunbar } 201bb7ac52eSDaniel Dunbar 202bb7ac52eSDaniel Dunbar // Set float ABI type. 20345df3dd3SSandeep Patel if (CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp") 204c1b1729bSDaniel Dunbar llvm::FloatABIType = llvm::FloatABI::Soft; 205c1b1729bSDaniel Dunbar else if (CodeGenOpts.FloatABI == "hard") 206c1b1729bSDaniel Dunbar llvm::FloatABIType = llvm::FloatABI::Hard; 207c1b1729bSDaniel Dunbar else { 208c1b1729bSDaniel Dunbar assert(CodeGenOpts.FloatABI.empty() && "Invalid float abi!"); 209c1b1729bSDaniel Dunbar llvm::FloatABIType = llvm::FloatABI::Default; 210c1b1729bSDaniel Dunbar } 211bb7ac52eSDaniel Dunbar 21242254262SPeter Collingbourne llvm::LessPreciseFPMADOption = CodeGenOpts.LessPreciseFPMAD; 2130ba5ac85SPeter Collingbourne llvm::NoInfsFPMath = CodeGenOpts.NoInfsFPMath; 2140ba5ac85SPeter Collingbourne llvm::NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath; 215c1b1729bSDaniel Dunbar NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS; 216b8d9995cSPeter Collingbourne llvm::UnsafeFPMath = CodeGenOpts.UnsafeFPMath; 217c1b1729bSDaniel Dunbar llvm::UseSoftFloat = CodeGenOpts.SoftFloat; 218c1b1729bSDaniel Dunbar UnwindTablesMandatory = CodeGenOpts.UnwindTables; 219c1b1729bSDaniel Dunbar 220c1b1729bSDaniel Dunbar TargetMachine::setAsmVerbosityDefault(CodeGenOpts.AsmVerbose); 221c1b1729bSDaniel Dunbar 222c1b1729bSDaniel Dunbar TargetMachine::setFunctionSections(CodeGenOpts.FunctionSections); 223c1b1729bSDaniel Dunbar TargetMachine::setDataSections (CodeGenOpts.DataSections); 224c1b1729bSDaniel Dunbar 225c1b1729bSDaniel Dunbar // FIXME: Parse this earlier. 226c1b1729bSDaniel Dunbar if (CodeGenOpts.RelocationModel == "static") { 227c1b1729bSDaniel Dunbar TargetMachine::setRelocationModel(llvm::Reloc::Static); 228c1b1729bSDaniel Dunbar } else if (CodeGenOpts.RelocationModel == "pic") { 229c1b1729bSDaniel Dunbar TargetMachine::setRelocationModel(llvm::Reloc::PIC_); 230c1b1729bSDaniel Dunbar } else { 231c1b1729bSDaniel Dunbar assert(CodeGenOpts.RelocationModel == "dynamic-no-pic" && 232c1b1729bSDaniel Dunbar "Invalid PIC model!"); 233c1b1729bSDaniel Dunbar TargetMachine::setRelocationModel(llvm::Reloc::DynamicNoPIC); 234c1b1729bSDaniel Dunbar } 235c1b1729bSDaniel Dunbar // FIXME: Parse this earlier. 236c1b1729bSDaniel Dunbar if (CodeGenOpts.CodeModel == "small") { 237c1b1729bSDaniel Dunbar TargetMachine::setCodeModel(llvm::CodeModel::Small); 238c1b1729bSDaniel Dunbar } else if (CodeGenOpts.CodeModel == "kernel") { 239c1b1729bSDaniel Dunbar TargetMachine::setCodeModel(llvm::CodeModel::Kernel); 240c1b1729bSDaniel Dunbar } else if (CodeGenOpts.CodeModel == "medium") { 241c1b1729bSDaniel Dunbar TargetMachine::setCodeModel(llvm::CodeModel::Medium); 242c1b1729bSDaniel Dunbar } else if (CodeGenOpts.CodeModel == "large") { 243c1b1729bSDaniel Dunbar TargetMachine::setCodeModel(llvm::CodeModel::Large); 244c1b1729bSDaniel Dunbar } else { 245c1b1729bSDaniel Dunbar assert(CodeGenOpts.CodeModel.empty() && "Invalid code model!"); 246c1b1729bSDaniel Dunbar TargetMachine::setCodeModel(llvm::CodeModel::Default); 247c1b1729bSDaniel Dunbar } 248c1b1729bSDaniel Dunbar 249c1b1729bSDaniel Dunbar std::vector<const char *> BackendArgs; 250c1b1729bSDaniel Dunbar BackendArgs.push_back("clang"); // Fake program name. 251c1b1729bSDaniel Dunbar if (!CodeGenOpts.DebugPass.empty()) { 252c1b1729bSDaniel Dunbar BackendArgs.push_back("-debug-pass"); 253c1b1729bSDaniel Dunbar BackendArgs.push_back(CodeGenOpts.DebugPass.c_str()); 254c1b1729bSDaniel Dunbar } 255c1b1729bSDaniel Dunbar if (!CodeGenOpts.LimitFloatPrecision.empty()) { 256c1b1729bSDaniel Dunbar BackendArgs.push_back("-limit-float-precision"); 257c1b1729bSDaniel Dunbar BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str()); 258c1b1729bSDaniel Dunbar } 259c1b1729bSDaniel Dunbar if (llvm::TimePassesIsEnabled) 260c1b1729bSDaniel Dunbar BackendArgs.push_back("-time-passes"); 26112100e2cSDaniel Dunbar for (unsigned i = 0, e = CodeGenOpts.BackendOptions.size(); i != e; ++i) 26212100e2cSDaniel Dunbar BackendArgs.push_back(CodeGenOpts.BackendOptions[i].c_str()); 263c1b1729bSDaniel Dunbar BackendArgs.push_back(0); 264c1b1729bSDaniel Dunbar llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1, 265c1b1729bSDaniel Dunbar const_cast<char **>(&BackendArgs[0])); 266c1b1729bSDaniel Dunbar 267c1b1729bSDaniel Dunbar std::string FeaturesStr; 268c1b1729bSDaniel Dunbar if (TargetOpts.CPU.size() || TargetOpts.Features.size()) { 269c1b1729bSDaniel Dunbar SubtargetFeatures Features; 270c1b1729bSDaniel Dunbar Features.setCPU(TargetOpts.CPU); 271c1b1729bSDaniel Dunbar for (std::vector<std::string>::const_iterator 272c1b1729bSDaniel Dunbar it = TargetOpts.Features.begin(), 273c1b1729bSDaniel Dunbar ie = TargetOpts.Features.end(); it != ie; ++it) 274c1b1729bSDaniel Dunbar Features.AddFeature(*it); 275c1b1729bSDaniel Dunbar FeaturesStr = Features.getString(); 276c1b1729bSDaniel Dunbar } 277c1b1729bSDaniel Dunbar TargetMachine *TM = TheTarget->createTargetMachine(Triple, FeaturesStr); 278c1b1729bSDaniel Dunbar 279c1b1729bSDaniel Dunbar if (CodeGenOpts.RelaxAll) 280c1b1729bSDaniel Dunbar TM->setMCRelaxAll(true); 28167919b2aSDaniel Dunbar if (CodeGenOpts.SaveTempLabels) 28267919b2aSDaniel Dunbar TM->setMCSaveTempLabels(true); 283*e264187cSRafael Espindola if (CodeGenOpts.NoDwarf2CFIAsm) 284*e264187cSRafael Espindola TM->setMCUseCFI(false); 285c1b1729bSDaniel Dunbar 286c1b1729bSDaniel Dunbar // Create the code generator passes. 287195fa003SDaniel Dunbar PassManager *PM = getCodeGenPasses(); 288c1b1729bSDaniel Dunbar CodeGenOpt::Level OptLevel = CodeGenOpt::Default; 289c1b1729bSDaniel Dunbar 290c1b1729bSDaniel Dunbar switch (CodeGenOpts.OptimizationLevel) { 291c1b1729bSDaniel Dunbar default: break; 292c1b1729bSDaniel Dunbar case 0: OptLevel = CodeGenOpt::None; break; 293c1b1729bSDaniel Dunbar case 3: OptLevel = CodeGenOpt::Aggressive; break; 294c1b1729bSDaniel Dunbar } 295c1b1729bSDaniel Dunbar 296c1b1729bSDaniel Dunbar // Normal mode, emit a .s or .o file by running the code generator. Note, 297c1b1729bSDaniel Dunbar // this also adds codegenerator level optimization passes. 298c1b1729bSDaniel Dunbar TargetMachine::CodeGenFileType CGFT = TargetMachine::CGFT_AssemblyFile; 299c1b1729bSDaniel Dunbar if (Action == Backend_EmitObj) 300c1b1729bSDaniel Dunbar CGFT = TargetMachine::CGFT_ObjectFile; 301c1b1729bSDaniel Dunbar else if (Action == Backend_EmitMCNull) 302c1b1729bSDaniel Dunbar CGFT = TargetMachine::CGFT_Null; 303c1b1729bSDaniel Dunbar else 304c1b1729bSDaniel Dunbar assert(Action == Backend_EmitAssembly && "Invalid action!"); 305c1b1729bSDaniel Dunbar if (TM->addPassesToEmitFile(*PM, OS, CGFT, OptLevel, 306c1b1729bSDaniel Dunbar /*DisableVerify=*/!CodeGenOpts.VerifyModule)) { 307c1b1729bSDaniel Dunbar Diags.Report(diag::err_fe_unable_to_interface_with_target); 308c1b1729bSDaniel Dunbar return false; 309c1b1729bSDaniel Dunbar } 310c1b1729bSDaniel Dunbar 311c1b1729bSDaniel Dunbar return true; 312c1b1729bSDaniel Dunbar } 313c1b1729bSDaniel Dunbar 314c1b1729bSDaniel Dunbar void EmitAssemblyHelper::EmitAssembly(BackendAction Action, raw_ostream *OS) { 315c1b1729bSDaniel Dunbar TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : 0); 316c1b1729bSDaniel Dunbar llvm::formatted_raw_ostream FormattedOS; 317c1b1729bSDaniel Dunbar 318c1b1729bSDaniel Dunbar CreatePasses(); 319c1b1729bSDaniel Dunbar switch (Action) { 320c1b1729bSDaniel Dunbar case Backend_EmitNothing: 321c1b1729bSDaniel Dunbar break; 322c1b1729bSDaniel Dunbar 323c1b1729bSDaniel Dunbar case Backend_EmitBC: 324c1b1729bSDaniel Dunbar getPerModulePasses()->add(createBitcodeWriterPass(*OS)); 325c1b1729bSDaniel Dunbar break; 326c1b1729bSDaniel Dunbar 327c1b1729bSDaniel Dunbar case Backend_EmitLL: 328c1b1729bSDaniel Dunbar FormattedOS.setStream(*OS, formatted_raw_ostream::PRESERVE_STREAM); 329c1b1729bSDaniel Dunbar getPerModulePasses()->add(createPrintModulePass(&FormattedOS)); 330c1b1729bSDaniel Dunbar break; 331c1b1729bSDaniel Dunbar 332c1b1729bSDaniel Dunbar default: 333c1b1729bSDaniel Dunbar FormattedOS.setStream(*OS, formatted_raw_ostream::PRESERVE_STREAM); 334c1b1729bSDaniel Dunbar if (!AddEmitPasses(Action, FormattedOS)) 335c1b1729bSDaniel Dunbar return; 336c1b1729bSDaniel Dunbar } 337c1b1729bSDaniel Dunbar 33815e36e8eSAndrew Trick // Before executing passes, print the final values of the LLVM options. 33915e36e8eSAndrew Trick cl::PrintOptionValues(); 34015e36e8eSAndrew Trick 341c1b1729bSDaniel Dunbar // Run passes. For now we do all passes at once, but eventually we 342c1b1729bSDaniel Dunbar // would like to have the option of streaming code generation. 343c1b1729bSDaniel Dunbar 344c1b1729bSDaniel Dunbar if (PerFunctionPasses) { 345c1b1729bSDaniel Dunbar PrettyStackTraceString CrashInfo("Per-function optimization"); 346c1b1729bSDaniel Dunbar 347c1b1729bSDaniel Dunbar PerFunctionPasses->doInitialization(); 348c1b1729bSDaniel Dunbar for (Module::iterator I = TheModule->begin(), 349c1b1729bSDaniel Dunbar E = TheModule->end(); I != E; ++I) 350c1b1729bSDaniel Dunbar if (!I->isDeclaration()) 351c1b1729bSDaniel Dunbar PerFunctionPasses->run(*I); 352c1b1729bSDaniel Dunbar PerFunctionPasses->doFinalization(); 353c1b1729bSDaniel Dunbar } 354c1b1729bSDaniel Dunbar 355c1b1729bSDaniel Dunbar if (PerModulePasses) { 356c1b1729bSDaniel Dunbar PrettyStackTraceString CrashInfo("Per-module optimization passes"); 357c1b1729bSDaniel Dunbar PerModulePasses->run(*TheModule); 358c1b1729bSDaniel Dunbar } 359c1b1729bSDaniel Dunbar 360c1b1729bSDaniel Dunbar if (CodeGenPasses) { 361c1b1729bSDaniel Dunbar PrettyStackTraceString CrashInfo("Code generation"); 362195fa003SDaniel Dunbar CodeGenPasses->run(*TheModule); 363c1b1729bSDaniel Dunbar } 364c1b1729bSDaniel Dunbar } 365c1b1729bSDaniel Dunbar 366c1b1729bSDaniel Dunbar void clang::EmitBackendOutput(Diagnostic &Diags, const CodeGenOptions &CGOpts, 367c1b1729bSDaniel Dunbar const TargetOptions &TOpts, Module *M, 368c1b1729bSDaniel Dunbar BackendAction Action, raw_ostream *OS) { 369c1b1729bSDaniel Dunbar EmitAssemblyHelper AsmHelper(Diags, CGOpts, TOpts, M); 370c1b1729bSDaniel Dunbar 371c1b1729bSDaniel Dunbar AsmHelper.EmitAssembly(Action, OS); 372c1b1729bSDaniel Dunbar } 373