/// selectTarget - Pick a target either via -march or by guessing the native /// arch. Add any CPU features specified via -mcpu or -mattr. TargetMachine *MCJIT::selectTarget(Module *Mod, StringRef MArch, StringRef MCPU, const SmallVectorImpl<std::string>& MAttrs, std::string *ErrorStr) { Triple TheTriple(Mod->getTargetTriple()); if (TheTriple.getTriple().empty()) TheTriple.setTriple(sys::getHostTriple()); // Adjust the triple to match what the user requested. const Target *TheTarget = 0; if (!MArch.empty()) { for (TargetRegistry::iterator it = TargetRegistry::begin(), ie = TargetRegistry::end(); it != ie; ++it) { if (MArch == it->getName()) { TheTarget = &*it; break; } } if (!TheTarget) { *ErrorStr = "No available targets are compatible with this -march, " "see -version for the available targets.\n"; return 0; } // Adjust the triple to match (if known), otherwise stick with the // module/host triple. Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch); if (Type != Triple::UnknownArch) TheTriple.setArch(Type); } else { std::string Error; TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error); if (TheTarget == 0) { if (ErrorStr) *ErrorStr = Error; return 0; } } if (!TheTarget->hasJIT()) { errs() << "WARNING: This target JIT is not designed for the host you are" << " running. If bad things happen, please choose a different " << "-march switch.\n"; } // Package up features to be passed to target/subtarget std::string FeaturesStr; if (!MCPU.empty() || !MAttrs.empty()) { SubtargetFeatures Features; Features.setCPU(MCPU); for (unsigned i = 0; i != MAttrs.size(); ++i) Features.AddFeature(MAttrs[i]); FeaturesStr = Features.getString(); } // Allocate a target... TargetMachine *Target = TheTarget->createTargetMachine(TheTriple.getTriple(), FeaturesStr); assert(Target && "Could not allocate target machine!"); return Target; }
// main - Entry point for the llc compiler. // int main(int argc, char **argv) { sys::PrintStackTraceOnErrorSignal(); PrettyStackTraceProgram X(argc, argv); // Enable debug stream buffering. EnableDebugBuffering = true; LLVMContext &Context = getGlobalContext(); llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. // Initialize targets first, so that --version shows registered targets. InitializeAllTargets(); InitializeAllAsmPrinters(); InitializeAllAsmParsers(); cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n"); // Load the module to be compiled... SMDiagnostic Err; std::auto_ptr<Module> M; M.reset(ParseIRFile(InputFilename, Err, Context)); if (M.get() == 0) { Err.Print(argv[0], errs()); return 1; } Module &mod = *M.get(); // If we are supposed to override the target triple, do so now. if (!TargetTriple.empty()) mod.setTargetTriple(Triple::normalize(TargetTriple)); Triple TheTriple(mod.getTargetTriple()); if (TheTriple.getTriple().empty()) TheTriple.setTriple(sys::getHostTriple()); // Allocate target machine. First, check whether the user has explicitly // specified an architecture to compile for. If so we have to look it up by // name, because it might be a backend that has no mapping to a target triple. const Target *TheTarget = 0; if (!MArch.empty()) { for (TargetRegistry::iterator it = TargetRegistry::begin(), ie = TargetRegistry::end(); it != ie; ++it) { if (MArch == it->getName()) { TheTarget = &*it; break; } } if (!TheTarget) { errs() << argv[0] << ": error: invalid target '" << MArch << "'.\n"; return 1; } // Adjust the triple to match (if known), otherwise stick with the // module/host triple. Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch); if (Type != Triple::UnknownArch) TheTriple.setArch(Type); } else { std::string Err; TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Err); if (TheTarget == 0) { errs() << argv[0] << ": error auto-selecting target for module '" << Err << "'. Please use the -march option to explicitly " << "pick a target.\n"; return 1; } } // Package up features to be passed to target/subtarget std::string FeaturesStr; if (MCPU.size() || MAttrs.size()) { SubtargetFeatures Features; Features.setCPU(MCPU); for (unsigned i = 0; i != MAttrs.size(); ++i) Features.AddFeature(MAttrs[i]); FeaturesStr = Features.getString(); } std::auto_ptr<TargetMachine> target(TheTarget->createTargetMachine(TheTriple.getTriple(), FeaturesStr)); assert(target.get() && "Could not allocate target machine!"); TargetMachine &Target = *target.get(); if (DisableDotLoc) Target.setMCUseLoc(false); if (TheTriple.getOS() == Triple::Darwin) { switch (TheTriple.getDarwinMajorNumber()) { case 7: case 8: case 9: // disable .loc support for older darwin OS. Target.setMCUseLoc(false); break; default: break; } } // Figure out where we are going to send the output... OwningPtr<tool_output_file> Out (GetOutputStream(TheTarget->getName(), TheTriple.getOS(), argv[0])); if (!Out) return 1; CodeGenOpt::Level OLvl = CodeGenOpt::Default; switch (OptLevel) { default: errs() << argv[0] << ": invalid optimization level.\n"; return 1; case ' ': break; case '0': OLvl = CodeGenOpt::None; break; case '1': OLvl = CodeGenOpt::Less; break; case '2': OLvl = CodeGenOpt::Default; break; case '3': OLvl = CodeGenOpt::Aggressive; break; } // Build up all of the passes that we want to do to the module. PassManager PM; // Add the target data from the target machine, if it exists, or the module. if (const TargetData *TD = Target.getTargetData()) PM.add(new TargetData(*TD)); else PM.add(new TargetData(&mod)); // Override default to generate verbose assembly. Target.setAsmVerbosityDefault(true); if (RelaxAll) { if (FileType != TargetMachine::CGFT_ObjectFile) errs() << argv[0] << ": warning: ignoring -mc-relax-all because filetype != obj"; else Target.setMCRelaxAll(true); } { formatted_raw_ostream FOS(Out->os()); // Ask the target to add backend passes as necessary. if (Target.addPassesToEmitFile(PM, FOS, FileType, OLvl, NoVerify)) { errs() << argv[0] << ": target does not support generation of this" << " file type!\n"; return 1; } // Before executing passes, print the final values of the LLVM options. cl::PrintOptionValues(); PM.run(mod); } // Declare success. Out->keep(); return 0; }
// main - Entry point for the llc compiler. // int main(int argc, char **argv) { llvm_shutdown_obj X; // Call llvm_shutdown() on exit. cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n"); sys::PrintStackTraceOnErrorSignal(); // Load the module to be compiled... std::string ErrorMessage; std::auto_ptr<Module> M; std::auto_ptr<MemoryBuffer> Buffer( MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)); if (Buffer.get()) M.reset(ParseBitcodeFile(Buffer.get(), &ErrorMessage)); if (M.get() == 0) { std::cerr << argv[0] << ": bitcode didn't read correctly.\n"; std::cerr << "Reason: " << ErrorMessage << "\n"; return 1; } Module &mod = *M.get(); // If we are supposed to override the target triple, do so now. if (!TargetTriple.empty()) mod.setTargetTriple(TargetTriple); // Allocate target machine. First, check whether the user has // explicitly specified an architecture to compile for. if (MArch == 0) { std::string Err; MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err); if (MArch == 0) { std::cerr << argv[0] << ": error auto-selecting target for module '" << Err << "'. Please use the -march option to explicitly " << "pick a target.\n"; return 1; } } // Package up features to be passed to target/subtarget std::string FeaturesStr; if (MCPU.size() || MAttrs.size()) { SubtargetFeatures Features; Features.setCPU(MCPU); for (unsigned i = 0; i != MAttrs.size(); ++i) Features.AddFeature(MAttrs[i]); FeaturesStr = Features.getString(); } std::auto_ptr<TargetMachine> target(MArch->CtorFn(mod, FeaturesStr)); assert(target.get() && "Could not allocate target machine!"); TargetMachine &Target = *target.get(); // Figure out where we are going to send the output... std::ostream *Out = GetOutputStream(argv[0]); if (Out == 0) return 1; // If this target requires addPassesToEmitWholeFile, do it now. This is // used by strange things like the C backend. if (Target.WantsWholeFile()) { PassManager PM; PM.add(new TargetData(*Target.getTargetData())); if (!NoVerify) PM.add(createVerifierPass()); // Ask the target to add backend passes as necessary. if (Target.addPassesToEmitWholeFile(PM, *Out, FileType, Fast)) { std::cerr << argv[0] << ": target does not support generation of this" << " file type!\n"; if (Out != &std::cout) delete Out; // And the Out file is empty and useless, so remove it now. sys::Path(OutputFilename).eraseFromDisk(); return 1; } PM.run(mod); } else { // Build up all of the passes that we want to do to the module. ExistingModuleProvider Provider(M.release()); FunctionPassManager Passes(&Provider); Passes.add(new TargetData(*Target.getTargetData())); #ifndef NDEBUG if (!NoVerify) Passes.add(createVerifierPass()); #endif // Ask the target to add backend passes as necessary. MachineCodeEmitter *MCE = 0; switch (Target.addPassesToEmitFile(Passes, *Out, FileType, Fast)) { default: assert(0 && "Invalid file model!"); return 1; case FileModel::Error: std::cerr << argv[0] << ": target does not support generation of this" << " file type!\n"; if (Out != &std::cout) delete Out; // And the Out file is empty and useless, so remove it now. sys::Path(OutputFilename).eraseFromDisk(); return 1; case FileModel::AsmFile: break; case FileModel::MachOFile: MCE = AddMachOWriter(Passes, *Out, Target); break; case FileModel::ElfFile: MCE = AddELFWriter(Passes, *Out, Target); break; } if (Target.addPassesToEmitFileFinish(Passes, MCE, Fast)) { std::cerr << argv[0] << ": target does not support generation of this" << " file type!\n"; if (Out != &std::cout) delete Out; // And the Out file is empty and useless, so remove it now. sys::Path(OutputFilename).eraseFromDisk(); return 1; } Passes.doInitialization(); // Run our queue of passes all at once now, efficiently. // TODO: this could lazily stream functions out of the module. for (Module::iterator I = mod.begin(), E = mod.end(); I != E; ++I) if (!I->isDeclaration()) Passes.run(*I); Passes.doFinalization(); } // Delete the ostream if it's not a stdout stream if (Out != &std::cout) delete Out; return 0; }
// main - Entry point for the llc compiler. // int main(int argc, char **argv) { sys::PrintStackTraceOnErrorSignal(); PrettyStackTraceProgram X(argc, argv); // Enable debug stream buffering. EnableDebugBuffering = true; LLVMContext &Context = getGlobalContext(); llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. // Initialize targets first, so that --version shows registered targets. InitializeAllTargets(); InitializeAllAsmPrinters(); cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n"); // Load the module to be compiled... SMDiagnostic Err; std::auto_ptr<Module> M; M.reset(ParseIRFile(InputFilename, Err, Context)); if (M.get() == 0) { Err.Print(argv[0], errs()); return 1; } Module &mod = *M.get(); // If we are supposed to override the target triple, do so now. if (!TargetTriple.empty()) mod.setTargetTriple(TargetTriple); Triple TheTriple(mod.getTargetTriple()); if (TheTriple.getTriple().empty()) TheTriple.setTriple(sys::getHostTriple()); // Allocate target machine. First, check whether the user has explicitly // specified an architecture to compile for. If so we have to look it up by // name, because it might be a backend that has no mapping to a target triple. const Target *TheTarget = 0; if (!MArch.empty()) { for (TargetRegistry::iterator it = TargetRegistry::begin(), ie = TargetRegistry::end(); it != ie; ++it) { if (MArch == it->getName()) { TheTarget = &*it; break; } } if (!TheTarget) { errs() << argv[0] << ": error: invalid target '" << MArch << "'.\n"; return 1; } // Adjust the triple to match (if known), otherwise stick with the // module/host triple. Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch); if (Type != Triple::UnknownArch) TheTriple.setArch(Type); } else { std::string Err; TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Err); if (TheTarget == 0) { errs() << argv[0] << ": error auto-selecting target for module '" << Err << "'. Please use the -march option to explicitly " << "pick a target.\n"; return 1; } } // Package up features to be passed to target/subtarget std::string FeaturesStr; if (MCPU.size() || MAttrs.size()) { SubtargetFeatures Features; Features.setCPU(MCPU); for (unsigned i = 0; i != MAttrs.size(); ++i) Features.AddFeature(MAttrs[i]); FeaturesStr = Features.getString(); } std::auto_ptr<TargetMachine> target(TheTarget->createTargetMachine(TheTriple.getTriple(), FeaturesStr)); assert(target.get() && "Could not allocate target machine!"); TargetMachine &Target = *target.get(); // Figure out where we are going to send the output... formatted_raw_ostream *Out = GetOutputStream(TheTarget->getName(), argv[0]); if (Out == 0) return 1; CodeGenOpt::Level OLvl = CodeGenOpt::Default; switch (OptLevel) { default: errs() << argv[0] << ": invalid optimization level.\n"; return 1; case ' ': break; case '0': OLvl = CodeGenOpt::None; break; case '1': OLvl = CodeGenOpt::Less; break; case '2': OLvl = CodeGenOpt::Default; break; case '3': OLvl = CodeGenOpt::Aggressive; break; } // Request that addPassesToEmitFile run the Verifier after running // passes which modify the IR. #ifndef NDEBUG bool DisableVerify = false; #else bool DisableVerify = true; #endif // If this target requires addPassesToEmitWholeFile, do it now. This is // used by strange things like the C backend. if (Target.WantsWholeFile()) { PassManager PM; // Add the target data from the target machine, if it exists, or the module. if (const TargetData *TD = Target.getTargetData()) PM.add(new TargetData(*TD)); else PM.add(new TargetData(&mod)); if (!NoVerify) PM.add(createVerifierPass()); // Ask the target to add backend passes as necessary. if (Target.addPassesToEmitWholeFile(PM, *Out, FileType, OLvl, DisableVerify)) { errs() << argv[0] << ": target does not support generation of this" << " file type!\n"; if (Out != &fouts()) delete Out; // And the Out file is empty and useless, so remove it now. sys::Path(OutputFilename).eraseFromDisk(); return 1; } PM.run(mod); } else { // Build up all of the passes that we want to do to the module. FunctionPassManager Passes(M.get()); // Add the target data from the target machine, if it exists, or the module. if (const TargetData *TD = Target.getTargetData()) Passes.add(new TargetData(*TD)); else Passes.add(new TargetData(&mod)); #ifndef NDEBUG if (!NoVerify) Passes.add(createVerifierPass()); #endif // Override default to generate verbose assembly. Target.setAsmVerbosityDefault(true); if (Target.addPassesToEmitFile(Passes, *Out, FileType, OLvl, DisableVerify)) { errs() << argv[0] << ": target does not support generation of this" << " file type!\n"; if (Out != &fouts()) delete Out; // And the Out file is empty and useless, so remove it now. sys::Path(OutputFilename).eraseFromDisk(); return 1; } Passes.doInitialization(); // Run our queue of passes all at once now, efficiently. // TODO: this could lazily stream functions out of the module. for (Module::iterator I = mod.begin(), E = mod.end(); I != E; ++I) if (!I->isDeclaration()) { if (DisableRedZone) I->addFnAttr(Attribute::NoRedZone); if (NoImplicitFloats) I->addFnAttr(Attribute::NoImplicitFloat); Passes.run(*I); } Passes.doFinalization(); } // Delete the ostream if it's not a stdout stream if (Out != &fouts()) delete Out; return 0; }
bool BackendConsumer::AddEmitPasses() { if (Action == Backend_EmitNothing) return true; if (Action == Backend_EmitBC) { getPerModulePasses()->add(createBitcodeWriterPass(FormattedOutStream)); } else if (Action == Backend_EmitLL) { getPerModulePasses()->add(createPrintModulePass(&FormattedOutStream)); } else { bool Fast = CodeGenOpts.OptimizationLevel == 0; // Create the TargetMachine for generating code. std::string Error; std::string Triple = TheModule->getTargetTriple(); const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error); if (!TheTarget) { Diags.Report(diag::err_fe_unable_to_create_target) << Error; return false; } // FIXME: Expose these capabilities via actual APIs!!!! Aside from just // being gross, this is also totally broken if we ever care about // concurrency. llvm::NoFramePointerElim = CodeGenOpts.DisableFPElim; if (CodeGenOpts.FloatABI == "soft") llvm::FloatABIType = llvm::FloatABI::Soft; else if (CodeGenOpts.FloatABI == "hard") llvm::FloatABIType = llvm::FloatABI::Hard; else { assert(CodeGenOpts.FloatABI.empty() && "Invalid float abi!"); llvm::FloatABIType = llvm::FloatABI::Default; } NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS; llvm::UseSoftFloat = CodeGenOpts.SoftFloat; UnwindTablesMandatory = CodeGenOpts.UnwindTables; TargetMachine::setAsmVerbosityDefault(CodeGenOpts.AsmVerbose); // FIXME: Parse this earlier. if (CodeGenOpts.RelocationModel == "static") { TargetMachine::setRelocationModel(llvm::Reloc::Static); } else if (CodeGenOpts.RelocationModel == "pic") { TargetMachine::setRelocationModel(llvm::Reloc::PIC_); } else { assert(CodeGenOpts.RelocationModel == "dynamic-no-pic" && "Invalid PIC model!"); TargetMachine::setRelocationModel(llvm::Reloc::DynamicNoPIC); } // FIXME: Parse this earlier. if (CodeGenOpts.CodeModel == "small") { TargetMachine::setCodeModel(llvm::CodeModel::Small); } else if (CodeGenOpts.CodeModel == "kernel") { TargetMachine::setCodeModel(llvm::CodeModel::Kernel); } else if (CodeGenOpts.CodeModel == "medium") { TargetMachine::setCodeModel(llvm::CodeModel::Medium); } else if (CodeGenOpts.CodeModel == "large") { TargetMachine::setCodeModel(llvm::CodeModel::Large); } else { assert(CodeGenOpts.CodeModel.empty() && "Invalid code model!"); TargetMachine::setCodeModel(llvm::CodeModel::Default); } std::vector<const char *> BackendArgs; BackendArgs.push_back("clang"); // Fake program name. if (!CodeGenOpts.DebugPass.empty()) { BackendArgs.push_back("-debug-pass"); BackendArgs.push_back(CodeGenOpts.DebugPass.c_str()); } if (!CodeGenOpts.LimitFloatPrecision.empty()) { BackendArgs.push_back("-limit-float-precision"); BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str()); } if (llvm::TimePassesIsEnabled) BackendArgs.push_back("-time-passes"); BackendArgs.push_back(0); llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1, (char**) &BackendArgs[0]); std::string FeaturesStr; if (TargetOpts.CPU.size() || TargetOpts.Features.size()) { SubtargetFeatures Features; Features.setCPU(TargetOpts.CPU); for (std::vector<std::string>::const_iterator it = TargetOpts.Features.begin(), ie = TargetOpts.Features.end(); it != ie; ++it) Features.AddFeature(*it); FeaturesStr = Features.getString(); } TargetMachine *TM = TheTarget->createTargetMachine(Triple, FeaturesStr); // Set register scheduler & allocation policy. RegisterScheduler::setDefault(createDefaultScheduler); RegisterRegAlloc::setDefault(Fast ? createLocalRegisterAllocator : createLinearScanRegisterAllocator); // From llvm-gcc: // If there are passes we have to run on the entire module, we do codegen // as a separate "pass" after that happens. // FIXME: This is disabled right now until bugs can be worked out. Reenable // this for fast -O0 compiles! FunctionPassManager *PM = getCodeGenPasses(); CodeGenOpt::Level OptLevel = CodeGenOpt::Default; switch (CodeGenOpts.OptimizationLevel) { default: break; case 0: OptLevel = CodeGenOpt::None; break; case 3: OptLevel = CodeGenOpt::Aggressive; break; } // Normal mode, emit a .s or .o file by running the code generator. Note, // this also adds codegenerator level optimization passes. TargetMachine::CodeGenFileType CGFT = TargetMachine::CGFT_AssemblyFile; if (Action == Backend_EmitObj) CGFT = TargetMachine::CGFT_ObjectFile; if (TM->addPassesToEmitFile(*PM, FormattedOutStream, CGFT, OptLevel)) { Diags.Report(diag::err_fe_unable_to_interface_with_target); return false; } } return true; }