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" 13fec0ff84SDan Gohman #include "clang/Basic/LangOptions.h" 148509824cSChandler Carruth #include "clang/Frontend/CodeGenOptions.h" 15c1b1729bSDaniel Dunbar #include "clang/Frontend/FrontendDiagnostic.h" 16c1b1729bSDaniel Dunbar #include "llvm/Module.h" 17c1b1729bSDaniel Dunbar #include "llvm/PassManager.h" 1856a7dab0SRafael Espindola #include "llvm/Analysis/Verifier.h" 19c1b1729bSDaniel Dunbar #include "llvm/Assembly/PrintModulePass.h" 20c1b1729bSDaniel Dunbar #include "llvm/Bitcode/ReaderWriter.h" 21c1b1729bSDaniel Dunbar #include "llvm/CodeGen/RegAllocRegistry.h" 22c1b1729bSDaniel Dunbar #include "llvm/CodeGen/SchedulerRegistry.h" 23eeb486ddSEvan Cheng #include "llvm/MC/SubtargetFeature.h" 24c1b1729bSDaniel Dunbar #include "llvm/Support/CommandLine.h" 25c1b1729bSDaniel Dunbar #include "llvm/Support/FormattedStream.h" 26c1b1729bSDaniel Dunbar #include "llvm/Support/PrettyStackTrace.h" 27494eb062SEvan Cheng #include "llvm/Support/TargetRegistry.h" 28c1b1729bSDaniel Dunbar #include "llvm/Support/Timer.h" 29c1b1729bSDaniel Dunbar #include "llvm/Support/raw_ostream.h" 30c1b1729bSDaniel Dunbar #include "llvm/Target/TargetData.h" 3156a7dab0SRafael Espindola #include "llvm/Target/TargetLibraryInfo.h" 32c1b1729bSDaniel Dunbar #include "llvm/Target/TargetMachine.h" 33c1b1729bSDaniel Dunbar #include "llvm/Target/TargetOptions.h" 34207bce31SNick Lewycky #include "llvm/Transforms/Instrumentation.h" 3556a7dab0SRafael Espindola #include "llvm/Transforms/IPO.h" 3656a7dab0SRafael Espindola #include "llvm/Transforms/IPO/PassManagerBuilder.h" 3756a7dab0SRafael Espindola #include "llvm/Transforms/Scalar.h" 38c1b1729bSDaniel Dunbar using namespace clang; 39c1b1729bSDaniel Dunbar using namespace llvm; 40c1b1729bSDaniel Dunbar 41c1b1729bSDaniel Dunbar namespace { 42c1b1729bSDaniel Dunbar 43c1b1729bSDaniel Dunbar class EmitAssemblyHelper { 449c902b55SDavid Blaikie DiagnosticsEngine &Diags; 45c1b1729bSDaniel Dunbar const CodeGenOptions &CodeGenOpts; 46432add5bSNick Lewycky const clang::TargetOptions &TargetOpts; 47fec0ff84SDan Gohman const LangOptions &LangOpts; 48c1b1729bSDaniel Dunbar Module *TheModule; 49c1b1729bSDaniel Dunbar 50c1b1729bSDaniel Dunbar Timer CodeGenerationTime; 51c1b1729bSDaniel Dunbar 52195fa003SDaniel Dunbar mutable PassManager *CodeGenPasses; 53c1b1729bSDaniel Dunbar mutable PassManager *PerModulePasses; 54c1b1729bSDaniel Dunbar mutable FunctionPassManager *PerFunctionPasses; 55c1b1729bSDaniel Dunbar 56c1b1729bSDaniel Dunbar private: 57195fa003SDaniel Dunbar PassManager *getCodeGenPasses() const { 58c1b1729bSDaniel Dunbar if (!CodeGenPasses) { 59195fa003SDaniel Dunbar CodeGenPasses = new PassManager(); 60c1b1729bSDaniel Dunbar CodeGenPasses->add(new TargetData(TheModule)); 61c1b1729bSDaniel Dunbar } 62c1b1729bSDaniel Dunbar return CodeGenPasses; 63c1b1729bSDaniel Dunbar } 64c1b1729bSDaniel Dunbar 65c1b1729bSDaniel Dunbar PassManager *getPerModulePasses() const { 66c1b1729bSDaniel Dunbar if (!PerModulePasses) { 67c1b1729bSDaniel Dunbar PerModulePasses = new PassManager(); 68c1b1729bSDaniel Dunbar PerModulePasses->add(new TargetData(TheModule)); 69c1b1729bSDaniel Dunbar } 70c1b1729bSDaniel Dunbar return PerModulePasses; 71c1b1729bSDaniel Dunbar } 72c1b1729bSDaniel Dunbar 73c1b1729bSDaniel Dunbar FunctionPassManager *getPerFunctionPasses() const { 74c1b1729bSDaniel Dunbar if (!PerFunctionPasses) { 75c1b1729bSDaniel Dunbar PerFunctionPasses = new FunctionPassManager(TheModule); 76c1b1729bSDaniel Dunbar PerFunctionPasses->add(new TargetData(TheModule)); 77c1b1729bSDaniel Dunbar } 78c1b1729bSDaniel Dunbar return PerFunctionPasses; 79c1b1729bSDaniel Dunbar } 80c1b1729bSDaniel Dunbar 81c1b1729bSDaniel Dunbar void CreatePasses(); 82c1b1729bSDaniel Dunbar 83c1b1729bSDaniel Dunbar /// AddEmitPasses - Add passes necessary to emit assembly or LLVM IR. 84c1b1729bSDaniel Dunbar /// 85c1b1729bSDaniel Dunbar /// \return True on success. 86c1b1729bSDaniel Dunbar bool AddEmitPasses(BackendAction Action, formatted_raw_ostream &OS); 87c1b1729bSDaniel Dunbar 88c1b1729bSDaniel Dunbar public: 899c902b55SDavid Blaikie EmitAssemblyHelper(DiagnosticsEngine &_Diags, 90432add5bSNick Lewycky const CodeGenOptions &CGOpts, 91432add5bSNick Lewycky const clang::TargetOptions &TOpts, 92fec0ff84SDan Gohman const LangOptions &LOpts, 93c1b1729bSDaniel Dunbar Module *M) 94fec0ff84SDan Gohman : Diags(_Diags), CodeGenOpts(CGOpts), TargetOpts(TOpts), LangOpts(LOpts), 95c1b1729bSDaniel Dunbar TheModule(M), CodeGenerationTime("Code Generation Time"), 96c1b1729bSDaniel Dunbar CodeGenPasses(0), PerModulePasses(0), PerFunctionPasses(0) {} 97c1b1729bSDaniel Dunbar 98c1b1729bSDaniel Dunbar ~EmitAssemblyHelper() { 99c1b1729bSDaniel Dunbar delete CodeGenPasses; 100c1b1729bSDaniel Dunbar delete PerModulePasses; 101c1b1729bSDaniel Dunbar delete PerFunctionPasses; 102c1b1729bSDaniel Dunbar } 103c1b1729bSDaniel Dunbar 104c1b1729bSDaniel Dunbar void EmitAssembly(BackendAction Action, raw_ostream *OS); 105c1b1729bSDaniel Dunbar }; 106c1b1729bSDaniel Dunbar 107c1b1729bSDaniel Dunbar } 108c1b1729bSDaniel Dunbar 1095932ce25SDan Gohman static void addObjCARCAPElimPass(const PassManagerBuilder &Builder, PassManagerBase &PM) { 1105932ce25SDan Gohman if (Builder.OptLevel > 0) 1115932ce25SDan Gohman PM.add(createObjCARCAPElimPass()); 1125932ce25SDan Gohman } 1135932ce25SDan Gohman 114fec0ff84SDan Gohman static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) { 115fec0ff84SDan Gohman if (Builder.OptLevel > 0) 116fec0ff84SDan Gohman PM.add(createObjCARCExpandPass()); 117fec0ff84SDan Gohman } 118fec0ff84SDan Gohman 119fec0ff84SDan Gohman static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) { 120fec0ff84SDan Gohman if (Builder.OptLevel > 0) 121fec0ff84SDan Gohman PM.add(createObjCARCOptPass()); 122fec0ff84SDan Gohman } 123fec0ff84SDan Gohman 1248855ff61SKostya Serebryany static void addAddressSanitizerPass(const PassManagerBuilder &Builder, 1258855ff61SKostya Serebryany PassManagerBase &PM) { 1268855ff61SKostya Serebryany PM.add(createAddressSanitizerPass()); 1278855ff61SKostya Serebryany } 1288855ff61SKostya Serebryany 129c1b1729bSDaniel Dunbar void EmitAssemblyHelper::CreatePasses() { 130c1b1729bSDaniel Dunbar unsigned OptLevel = CodeGenOpts.OptimizationLevel; 131c1b1729bSDaniel Dunbar CodeGenOptions::InliningMethod Inlining = CodeGenOpts.Inlining; 132c1b1729bSDaniel Dunbar 133c1b1729bSDaniel Dunbar // Handle disabling of LLVM optimization, where we want to preserve the 134c1b1729bSDaniel Dunbar // internal module before any optimization. 135c1b1729bSDaniel Dunbar if (CodeGenOpts.DisableLLVMOpts) { 136c1b1729bSDaniel Dunbar OptLevel = 0; 137c1b1729bSDaniel Dunbar Inlining = CodeGenOpts.NoInlining; 138c1b1729bSDaniel Dunbar } 139c1b1729bSDaniel Dunbar 1405c12367bSChris Lattner PassManagerBuilder PMBuilder; 141ecf0ba5bSChris Lattner PMBuilder.OptLevel = OptLevel; 142ecf0ba5bSChris Lattner PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize; 143d98cec5cSChris Lattner 144ecf0ba5bSChris Lattner PMBuilder.DisableSimplifyLibCalls = !CodeGenOpts.SimplifyLibCalls; 145ecf0ba5bSChris Lattner PMBuilder.DisableUnitAtATime = !CodeGenOpts.UnitAtATime; 146ecf0ba5bSChris Lattner PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops; 1475c12367bSChris Lattner 148fec0ff84SDan Gohman // In ObjC ARC mode, add the main ARC optimization passes. 149fec0ff84SDan Gohman if (LangOpts.ObjCAutoRefCount) { 150fec0ff84SDan Gohman PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible, 151fec0ff84SDan Gohman addObjCARCExpandPass); 1525932ce25SDan Gohman PMBuilder.addExtension(PassManagerBuilder::EP_ModuleOptimizerEarly, 1535932ce25SDan Gohman addObjCARCAPElimPass); 154fec0ff84SDan Gohman PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate, 155fec0ff84SDan Gohman addObjCARCOptPass); 156fec0ff84SDan Gohman } 157fec0ff84SDan Gohman 158c3333e87SKostya Serebryany if (LangOpts.AddressSanitizer) { 1598855ff61SKostya Serebryany PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate, 1608855ff61SKostya Serebryany addAddressSanitizerPass); 161d4768576SKostya Serebryany PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, 162d4768576SKostya Serebryany addAddressSanitizerPass); 1638855ff61SKostya Serebryany } 1648855ff61SKostya Serebryany 1655c12367bSChris Lattner // Figure out TargetLibraryInfo. 16628b9e8b3SBill Wendling Triple TargetTriple(TheModule->getTargetTriple()); 167ecf0ba5bSChris Lattner PMBuilder.LibraryInfo = new TargetLibraryInfo(TargetTriple); 168fa222dfbSChris Lattner if (!CodeGenOpts.SimplifyLibCalls) 169ecf0ba5bSChris Lattner PMBuilder.LibraryInfo->disableAllFunctions(); 170c1b1729bSDaniel Dunbar 171c1b1729bSDaniel Dunbar switch (Inlining) { 172c1b1729bSDaniel Dunbar case CodeGenOptions::NoInlining: break; 173c1b1729bSDaniel Dunbar case CodeGenOptions::NormalInlining: { 174c1b1729bSDaniel Dunbar // FIXME: Derive these constants in a principled fashion. 175c1b1729bSDaniel Dunbar unsigned Threshold = 225; 1769a5f84faSBob Wilson if (CodeGenOpts.OptimizeSize == 1) // -Os 177c1b1729bSDaniel Dunbar Threshold = 75; 1789a5f84faSBob Wilson else if (CodeGenOpts.OptimizeSize == 2) // -Oz 1799a5f84faSBob Wilson Threshold = 25; 180c1b1729bSDaniel Dunbar else if (OptLevel > 2) 181c1b1729bSDaniel Dunbar Threshold = 275; 182ecf0ba5bSChris Lattner PMBuilder.Inliner = createFunctionInliningPass(Threshold); 183c1b1729bSDaniel Dunbar break; 184c1b1729bSDaniel Dunbar } 185c1b1729bSDaniel Dunbar case CodeGenOptions::OnlyAlwaysInlining: 1865c12367bSChris Lattner // Respect always_inline. 187ecf0ba5bSChris Lattner PMBuilder.Inliner = createAlwaysInlinerPass(); 188c1b1729bSDaniel Dunbar break; 189c1b1729bSDaniel Dunbar } 190c1b1729bSDaniel Dunbar 191d98cec5cSChris Lattner 1925c12367bSChris Lattner // Set up the per-function pass manager. 1935c12367bSChris Lattner FunctionPassManager *FPM = getPerFunctionPasses(); 1945c12367bSChris Lattner if (CodeGenOpts.VerifyModule) 1955c12367bSChris Lattner FPM->add(createVerifierPass()); 1965c12367bSChris Lattner PMBuilder.populateFunctionPassManager(*FPM); 1975c12367bSChris Lattner 1985c12367bSChris Lattner // Set up the per-module pass manager. 1995c12367bSChris Lattner PassManager *MPM = getPerModulePasses(); 200d98cec5cSChris Lattner 201207bce31SNick Lewycky if (CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes) { 202207bce31SNick Lewycky MPM->add(createGCOVProfilerPass(CodeGenOpts.EmitGcovNotes, 20328b9e8b3SBill Wendling CodeGenOpts.EmitGcovArcs, 20428b9e8b3SBill Wendling TargetTriple.isMacOSX())); 20528b9e8b3SBill Wendling 206207bce31SNick Lewycky if (!CodeGenOpts.DebugInfo) 207207bce31SNick Lewycky MPM->add(createStripSymbolsPass(true)); 208207bce31SNick Lewycky } 209207bce31SNick Lewycky 2105c12367bSChris Lattner 2115c12367bSChris Lattner PMBuilder.populateModulePassManager(*MPM); 212c1b1729bSDaniel Dunbar } 213c1b1729bSDaniel Dunbar 214c1b1729bSDaniel Dunbar bool EmitAssemblyHelper::AddEmitPasses(BackendAction Action, 215c1b1729bSDaniel Dunbar formatted_raw_ostream &OS) { 216c1b1729bSDaniel Dunbar // Create the TargetMachine for generating code. 217c1b1729bSDaniel Dunbar std::string Error; 218c1b1729bSDaniel Dunbar std::string Triple = TheModule->getTargetTriple(); 219c1b1729bSDaniel Dunbar const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error); 220c1b1729bSDaniel Dunbar if (!TheTarget) { 221c1b1729bSDaniel Dunbar Diags.Report(diag::err_fe_unable_to_create_target) << Error; 222c1b1729bSDaniel Dunbar return false; 223c1b1729bSDaniel Dunbar } 224c1b1729bSDaniel Dunbar 225c1b1729bSDaniel Dunbar // FIXME: Expose these capabilities via actual APIs!!!! Aside from just 226c1b1729bSDaniel Dunbar // being gross, this is also totally broken if we ever care about 227c1b1729bSDaniel Dunbar // concurrency. 228bb7ac52eSDaniel Dunbar 229c1b1729bSDaniel Dunbar TargetMachine::setAsmVerbosityDefault(CodeGenOpts.AsmVerbose); 230c1b1729bSDaniel Dunbar 231c1b1729bSDaniel Dunbar TargetMachine::setFunctionSections(CodeGenOpts.FunctionSections); 232c1b1729bSDaniel Dunbar TargetMachine::setDataSections (CodeGenOpts.DataSections); 233c1b1729bSDaniel Dunbar 234c1b1729bSDaniel Dunbar // FIXME: Parse this earlier. 2350d3d7770SBenjamin Kramer llvm::CodeModel::Model CM; 236c1b1729bSDaniel Dunbar if (CodeGenOpts.CodeModel == "small") { 2370d3d7770SBenjamin Kramer CM = llvm::CodeModel::Small; 238c1b1729bSDaniel Dunbar } else if (CodeGenOpts.CodeModel == "kernel") { 2390d3d7770SBenjamin Kramer CM = llvm::CodeModel::Kernel; 240c1b1729bSDaniel Dunbar } else if (CodeGenOpts.CodeModel == "medium") { 2410d3d7770SBenjamin Kramer CM = llvm::CodeModel::Medium; 242c1b1729bSDaniel Dunbar } else if (CodeGenOpts.CodeModel == "large") { 2430d3d7770SBenjamin Kramer CM = llvm::CodeModel::Large; 244c1b1729bSDaniel Dunbar } else { 245c1b1729bSDaniel Dunbar assert(CodeGenOpts.CodeModel.empty() && "Invalid code model!"); 2460d3d7770SBenjamin Kramer CM = llvm::CodeModel::Default; 247c1b1729bSDaniel Dunbar } 248c1b1729bSDaniel Dunbar 249*09d20eefSDavid Blaikie SmallVector<const char *, 16> 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()); 263ba3df1d3SChad Rosier if (CodeGenOpts.NoGlobalMerge) 264ba3df1d3SChad Rosier BackendArgs.push_back("-global-merge=false"); 265c1b1729bSDaniel Dunbar BackendArgs.push_back(0); 266c1b1729bSDaniel Dunbar llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1, 267*09d20eefSDavid Blaikie BackendArgs.data()); 268c1b1729bSDaniel Dunbar 269c1b1729bSDaniel Dunbar std::string FeaturesStr; 270adc79598SEvan Cheng if (TargetOpts.Features.size()) { 271c1b1729bSDaniel Dunbar SubtargetFeatures Features; 272c1b1729bSDaniel Dunbar for (std::vector<std::string>::const_iterator 273c1b1729bSDaniel Dunbar it = TargetOpts.Features.begin(), 274c1b1729bSDaniel Dunbar ie = TargetOpts.Features.end(); it != ie; ++it) 275c1b1729bSDaniel Dunbar Features.AddFeature(*it); 276c1b1729bSDaniel Dunbar FeaturesStr = Features.getString(); 277c1b1729bSDaniel Dunbar } 2783f37dd06SEvan Cheng 2793f37dd06SEvan Cheng llvm::Reloc::Model RM = llvm::Reloc::Default; 2803f37dd06SEvan Cheng if (CodeGenOpts.RelocationModel == "static") { 2813f37dd06SEvan Cheng RM = llvm::Reloc::Static; 2823f37dd06SEvan Cheng } else if (CodeGenOpts.RelocationModel == "pic") { 2833f37dd06SEvan Cheng RM = llvm::Reloc::PIC_; 2843f37dd06SEvan Cheng } else { 2853f37dd06SEvan Cheng assert(CodeGenOpts.RelocationModel == "dynamic-no-pic" && 2863f37dd06SEvan Cheng "Invalid PIC model!"); 2873f37dd06SEvan Cheng RM = llvm::Reloc::DynamicNoPIC; 2883f37dd06SEvan Cheng } 2893f37dd06SEvan Cheng 290dd286bceSEvan Cheng CodeGenOpt::Level OptLevel = CodeGenOpt::Default; 291dd286bceSEvan Cheng switch (CodeGenOpts.OptimizationLevel) { 292dd286bceSEvan Cheng default: break; 293dd286bceSEvan Cheng case 0: OptLevel = CodeGenOpt::None; break; 294dd286bceSEvan Cheng case 3: OptLevel = CodeGenOpt::Aggressive; break; 295dd286bceSEvan Cheng } 296dd286bceSEvan Cheng 297432add5bSNick Lewycky llvm::TargetOptions Options; 298432add5bSNick Lewycky 299432add5bSNick Lewycky // Set frame pointer elimination mode. 300432add5bSNick Lewycky if (!CodeGenOpts.DisableFPElim) { 301432add5bSNick Lewycky Options.NoFramePointerElim = false; 302432add5bSNick Lewycky Options.NoFramePointerElimNonLeaf = false; 303432add5bSNick Lewycky } else if (CodeGenOpts.OmitLeafFramePointer) { 304432add5bSNick Lewycky Options.NoFramePointerElim = false; 305432add5bSNick Lewycky Options.NoFramePointerElimNonLeaf = true; 306432add5bSNick Lewycky } else { 307432add5bSNick Lewycky Options.NoFramePointerElim = true; 308432add5bSNick Lewycky Options.NoFramePointerElimNonLeaf = true; 309432add5bSNick Lewycky } 310432add5bSNick Lewycky 311432add5bSNick Lewycky // Set float ABI type. 312432add5bSNick Lewycky if (CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp") 313432add5bSNick Lewycky Options.FloatABIType = llvm::FloatABI::Soft; 314432add5bSNick Lewycky else if (CodeGenOpts.FloatABI == "hard") 315432add5bSNick Lewycky Options.FloatABIType = llvm::FloatABI::Hard; 316432add5bSNick Lewycky else { 317432add5bSNick Lewycky assert(CodeGenOpts.FloatABI.empty() && "Invalid float abi!"); 318432add5bSNick Lewycky Options.FloatABIType = llvm::FloatABI::Default; 319432add5bSNick Lewycky } 320432add5bSNick Lewycky 321432add5bSNick Lewycky Options.LessPreciseFPMADOption = CodeGenOpts.LessPreciseFPMAD; 322432add5bSNick Lewycky Options.NoInfsFPMath = CodeGenOpts.NoInfsFPMath; 323432add5bSNick Lewycky Options.NoNaNsFPMath = CodeGenOpts.NoNaNsFPMath; 324432add5bSNick Lewycky Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS; 325432add5bSNick Lewycky Options.UnsafeFPMath = CodeGenOpts.UnsafeFPMath; 326432add5bSNick Lewycky Options.UseSoftFloat = CodeGenOpts.SoftFloat; 327f4d3f7a0SNick Lewycky Options.StackAlignmentOverride = CodeGenOpts.StackAlignment; 328f4d3f7a0SNick Lewycky Options.RealignStack = CodeGenOpts.StackRealignment; 3291c8c436aSNick Lewycky Options.DisableTailCalls = CodeGenOpts.DisableTailCalls; 33014adb360SBob Wilson Options.TrapFuncName = CodeGenOpts.TrapFuncName; 331432add5bSNick Lewycky 332adc79598SEvan Cheng TargetMachine *TM = TheTarget->createTargetMachine(Triple, TargetOpts.CPU, 333432add5bSNick Lewycky FeaturesStr, Options, 334432add5bSNick Lewycky RM, CM, OptLevel); 335c1b1729bSDaniel Dunbar 336c1b1729bSDaniel Dunbar if (CodeGenOpts.RelaxAll) 337c1b1729bSDaniel Dunbar TM->setMCRelaxAll(true); 33867919b2aSDaniel Dunbar if (CodeGenOpts.SaveTempLabels) 33967919b2aSDaniel Dunbar TM->setMCSaveTempLabels(true); 340e264187cSRafael Espindola if (CodeGenOpts.NoDwarf2CFIAsm) 341e264187cSRafael Espindola TM->setMCUseCFI(false); 342dd424a5bSNick Lewycky if (!CodeGenOpts.NoDwarfDirectoryAsm) 343dd424a5bSNick Lewycky TM->setMCUseDwarfDirectory(true); 344ca6b90d8SNick Lewycky if (CodeGenOpts.NoExecStack) 345ca6b90d8SNick Lewycky TM->setMCNoExecStack(true); 346c1b1729bSDaniel Dunbar 347c1b1729bSDaniel Dunbar // Create the code generator passes. 348195fa003SDaniel Dunbar PassManager *PM = getCodeGenPasses(); 349c1b1729bSDaniel Dunbar 350c1b1729bSDaniel Dunbar // Normal mode, emit a .s or .o file by running the code generator. Note, 351c1b1729bSDaniel Dunbar // this also adds codegenerator level optimization passes. 352c1b1729bSDaniel Dunbar TargetMachine::CodeGenFileType CGFT = TargetMachine::CGFT_AssemblyFile; 353c1b1729bSDaniel Dunbar if (Action == Backend_EmitObj) 354c1b1729bSDaniel Dunbar CGFT = TargetMachine::CGFT_ObjectFile; 355c1b1729bSDaniel Dunbar else if (Action == Backend_EmitMCNull) 356c1b1729bSDaniel Dunbar CGFT = TargetMachine::CGFT_Null; 357c1b1729bSDaniel Dunbar else 358c1b1729bSDaniel Dunbar assert(Action == Backend_EmitAssembly && "Invalid action!"); 359fec0ff84SDan Gohman 360fec0ff84SDan Gohman // Add ObjC ARC final-cleanup optimizations. This is done as part of the 361fec0ff84SDan Gohman // "codegen" passes so that it isn't run multiple times when there is 362fec0ff84SDan Gohman // inlining happening. 363fec0ff84SDan Gohman if (LangOpts.ObjCAutoRefCount) 364fec0ff84SDan Gohman PM->add(createObjCARCContractPass()); 365fec0ff84SDan Gohman 366dd286bceSEvan Cheng if (TM->addPassesToEmitFile(*PM, OS, CGFT, 367c1b1729bSDaniel Dunbar /*DisableVerify=*/!CodeGenOpts.VerifyModule)) { 368c1b1729bSDaniel Dunbar Diags.Report(diag::err_fe_unable_to_interface_with_target); 369c1b1729bSDaniel Dunbar return false; 370c1b1729bSDaniel Dunbar } 371c1b1729bSDaniel Dunbar 372c1b1729bSDaniel Dunbar return true; 373c1b1729bSDaniel Dunbar } 374c1b1729bSDaniel Dunbar 375c1b1729bSDaniel Dunbar void EmitAssemblyHelper::EmitAssembly(BackendAction Action, raw_ostream *OS) { 376c1b1729bSDaniel Dunbar TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : 0); 377c1b1729bSDaniel Dunbar llvm::formatted_raw_ostream FormattedOS; 378c1b1729bSDaniel Dunbar 379c1b1729bSDaniel Dunbar CreatePasses(); 380c1b1729bSDaniel Dunbar switch (Action) { 381c1b1729bSDaniel Dunbar case Backend_EmitNothing: 382c1b1729bSDaniel Dunbar break; 383c1b1729bSDaniel Dunbar 384c1b1729bSDaniel Dunbar case Backend_EmitBC: 385c1b1729bSDaniel Dunbar getPerModulePasses()->add(createBitcodeWriterPass(*OS)); 386c1b1729bSDaniel Dunbar break; 387c1b1729bSDaniel Dunbar 388c1b1729bSDaniel Dunbar case Backend_EmitLL: 389c1b1729bSDaniel Dunbar FormattedOS.setStream(*OS, formatted_raw_ostream::PRESERVE_STREAM); 390c1b1729bSDaniel Dunbar getPerModulePasses()->add(createPrintModulePass(&FormattedOS)); 391c1b1729bSDaniel Dunbar break; 392c1b1729bSDaniel Dunbar 393c1b1729bSDaniel Dunbar default: 394c1b1729bSDaniel Dunbar FormattedOS.setStream(*OS, formatted_raw_ostream::PRESERVE_STREAM); 395c1b1729bSDaniel Dunbar if (!AddEmitPasses(Action, FormattedOS)) 396c1b1729bSDaniel Dunbar return; 397c1b1729bSDaniel Dunbar } 398c1b1729bSDaniel Dunbar 39915e36e8eSAndrew Trick // Before executing passes, print the final values of the LLVM options. 40015e36e8eSAndrew Trick cl::PrintOptionValues(); 40115e36e8eSAndrew Trick 402c1b1729bSDaniel Dunbar // Run passes. For now we do all passes at once, but eventually we 403c1b1729bSDaniel Dunbar // would like to have the option of streaming code generation. 404c1b1729bSDaniel Dunbar 405c1b1729bSDaniel Dunbar if (PerFunctionPasses) { 406c1b1729bSDaniel Dunbar PrettyStackTraceString CrashInfo("Per-function optimization"); 407c1b1729bSDaniel Dunbar 408c1b1729bSDaniel Dunbar PerFunctionPasses->doInitialization(); 409c1b1729bSDaniel Dunbar for (Module::iterator I = TheModule->begin(), 410c1b1729bSDaniel Dunbar E = TheModule->end(); I != E; ++I) 411c1b1729bSDaniel Dunbar if (!I->isDeclaration()) 412c1b1729bSDaniel Dunbar PerFunctionPasses->run(*I); 413c1b1729bSDaniel Dunbar PerFunctionPasses->doFinalization(); 414c1b1729bSDaniel Dunbar } 415c1b1729bSDaniel Dunbar 416c1b1729bSDaniel Dunbar if (PerModulePasses) { 417c1b1729bSDaniel Dunbar PrettyStackTraceString CrashInfo("Per-module optimization passes"); 418c1b1729bSDaniel Dunbar PerModulePasses->run(*TheModule); 419c1b1729bSDaniel Dunbar } 420c1b1729bSDaniel Dunbar 421c1b1729bSDaniel Dunbar if (CodeGenPasses) { 422c1b1729bSDaniel Dunbar PrettyStackTraceString CrashInfo("Code generation"); 423195fa003SDaniel Dunbar CodeGenPasses->run(*TheModule); 424c1b1729bSDaniel Dunbar } 425c1b1729bSDaniel Dunbar } 426c1b1729bSDaniel Dunbar 4279c902b55SDavid Blaikie void clang::EmitBackendOutput(DiagnosticsEngine &Diags, 4289c902b55SDavid Blaikie const CodeGenOptions &CGOpts, 429432add5bSNick Lewycky const clang::TargetOptions &TOpts, 430fec0ff84SDan Gohman const LangOptions &LOpts, 431fec0ff84SDan Gohman Module *M, 432c1b1729bSDaniel Dunbar BackendAction Action, raw_ostream *OS) { 433fec0ff84SDan Gohman EmitAssemblyHelper AsmHelper(Diags, CGOpts, TOpts, LOpts, M); 434c1b1729bSDaniel Dunbar 435c1b1729bSDaniel Dunbar AsmHelper.EmitAssembly(Action, OS); 436c1b1729bSDaniel Dunbar } 437