1e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith//===-- TargetSelect.cpp - Target Chooser Code ----------------------------===// 2e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith// 3e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith// The LLVM Compiler Infrastructure 4e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith// 5e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith// This file is distributed under the University of Illinois Open Source 6e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith// License. See LICENSE.TXT for details. 7e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith// 8e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith//===----------------------------------------------------------------------===// 9e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith// 109ea47179e647e806a2c67639bfead9d254514e59Dylan Noblesmith// This just asks the TargetRegistry for the appropriate target to use, and 119ea47179e647e806a2c67639bfead9d254514e59Dylan Noblesmith// allows the user to specify a specific one on the commandline with -march=x, 129ea47179e647e806a2c67639bfead9d254514e59Dylan Noblesmith// -mcpu=y, and -mattr=a,-b,+c. Clients should initialize targets prior to 139ea47179e647e806a2c67639bfead9d254514e59Dylan Noblesmith// calling selectTarget(). 14e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith// 15e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith//===----------------------------------------------------------------------===// 16e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith 172ea29ba2a8ddd7ba4b946eb754f1a39304d9fc09Dylan Noblesmith#include "llvm/ExecutionEngine/ExecutionEngine.h" 188e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson#include "llvm/Module.h" 19e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith#include "llvm/ADT/Triple.h" 20ab8be96fd30ca9396e6b84fdddf1ac6208984cadEvan Cheng#include "llvm/MC/SubtargetFeature.h" 213e74d6fdd248e20a280f1dff3da9a6c689c2c4c3Evan Cheng#include "llvm/Target/TargetMachine.h" 22e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith#include "llvm/Support/CommandLine.h" 23e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith#include "llvm/Support/Host.h" 243e74d6fdd248e20a280f1dff3da9a6c689c2c4c3Evan Cheng#include "llvm/Support/TargetRegistry.h" 259ea47179e647e806a2c67639bfead9d254514e59Dylan Noblesmith 26e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmithusing namespace llvm; 27e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith 288e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen AndersonTargetMachine *EngineBuilder::selectTarget() { 29956994c2edeccaeab708983257ea62d622d72dc2NAKAMURA Takumi Triple TT(LLVM_HOSTTRIPLE); 308e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson return selectTarget(TT, MArch, MCPU, MAttrs); 318e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson} 328e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson 33e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith/// selectTarget - Pick a target either via -march or by guessing the native 34e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith/// arch. Add any CPU features specified via -mcpu or -mattr. 359ea47179e647e806a2c67639bfead9d254514e59Dylan NoblesmithTargetMachine *EngineBuilder::selectTarget(const Triple &TargetTriple, 362ea29ba2a8ddd7ba4b946eb754f1a39304d9fc09Dylan Noblesmith StringRef MArch, 372ea29ba2a8ddd7ba4b946eb754f1a39304d9fc09Dylan Noblesmith StringRef MCPU, 388e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson const SmallVectorImpl<std::string>& MAttrs) { 399ea47179e647e806a2c67639bfead9d254514e59Dylan Noblesmith Triple TheTriple(TargetTriple); 40e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith if (TheTriple.getTriple().empty()) 410173864d8a87d9243d304fbf91b556e20b5a32fcSebastian Pop TheTriple.setTriple(sys::getDefaultTargetTriple()); 42e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith 43e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith // Adjust the triple to match what the user requested. 44e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith const Target *TheTarget = 0; 45e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith if (!MArch.empty()) { 46e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith for (TargetRegistry::iterator it = TargetRegistry::begin(), 47e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith ie = TargetRegistry::end(); it != ie; ++it) { 48e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith if (MArch == it->getName()) { 49e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith TheTarget = &*it; 50e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith break; 51e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith } 52e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith } 53e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith 54e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith if (!TheTarget) { 5577966fab9e777e7d22e5391fbfbd42658e8f8f03Jim Grosbach if (ErrorStr) 5677966fab9e777e7d22e5391fbfbd42658e8f8f03Jim Grosbach *ErrorStr = "No available targets are compatible with this -march, " 5777966fab9e777e7d22e5391fbfbd42658e8f8f03Jim Grosbach "see -version for the available targets.\n"; 58e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith return 0; 59e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith } 60e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith 61e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith // Adjust the triple to match (if known), otherwise stick with the 629ea47179e647e806a2c67639bfead9d254514e59Dylan Noblesmith // requested/host triple. 63e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch); 64e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith if (Type != Triple::UnknownArch) 65e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith TheTriple.setArch(Type); 66e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith } else { 67e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith std::string Error; 68e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error); 69e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith if (TheTarget == 0) { 70e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith if (ErrorStr) 71e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith *ErrorStr = Error; 72e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith return 0; 73e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith } 74e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith } 75e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith 76e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith // Package up features to be passed to target/subtarget 77e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith std::string FeaturesStr; 78276365dd4bc0c2160f91fd8062ae1fc90c86c324Evan Cheng if (!MAttrs.empty()) { 79e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith SubtargetFeatures Features; 80e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith for (unsigned i = 0; i != MAttrs.size(); ++i) 81e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith Features.AddFeature(MAttrs[i]); 82e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith FeaturesStr = Features.getString(); 83e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith } 84e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith 85e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith // Allocate a target... 86439661395fd2a2a832dba01c65bc88718528313cEvan Cheng TargetMachine *Target = TheTarget->createTargetMachine(TheTriple.getTriple(), 8734ad6db8b958fdc0d38e122edf753b5326e69b03Evan Cheng MCPU, FeaturesStr, 888a8d479214745c82ef00f08d4e4f1c173b5f9ce2Nick Lewycky Options, 898e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson RelocModel, CMModel, 908e1fc56b2496270d1d6040cb648eef5d5aeb6079Owen Anderson OptLevel); 91e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith assert(Target && "Could not allocate target machine!"); 92e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith return Target; 93e6297010a6235f3a558f1441abe79cddcb05dc24Dylan Noblesmith} 94