TargetSelect.cpp revision 263508
1279418Smarkj//===-- TargetSelect.cpp - Target Chooser Code ----------------------------===// 2279418Smarkj// 3279418Smarkj// The LLVM Compiler Infrastructure 4279418Smarkj// 5279418Smarkj// This file is distributed under the University of Illinois Open Source 6279418Smarkj// License. See LICENSE.TXT for details. 7279418Smarkj// 8279418Smarkj//===----------------------------------------------------------------------===// 9279418Smarkj// 10279418Smarkj// This just asks the TargetRegistry for the appropriate target to use, and 11279418Smarkj// allows the user to specify a specific one on the commandline with -march=x, 12279418Smarkj// -mcpu=y, and -mattr=a,-b,+c. Clients should initialize targets prior to 13279418Smarkj// calling selectTarget(). 14279418Smarkj// 15279418Smarkj//===----------------------------------------------------------------------===// 16279418Smarkj 17279418Smarkj#include "llvm/ExecutionEngine/ExecutionEngine.h" 18279418Smarkj#include "llvm/ADT/Triple.h" 19279418Smarkj#include "llvm/IR/Module.h" 20279418Smarkj#include "llvm/MC/SubtargetFeature.h" 21279418Smarkj#include "llvm/Support/CommandLine.h" 22279418Smarkj#include "llvm/Support/Host.h" 23279418Smarkj#include "llvm/Support/TargetRegistry.h" 24279418Smarkj#include "llvm/Target/TargetMachine.h" 25279418Smarkj 26279418Smarkjusing namespace llvm; 27 28TargetMachine *EngineBuilder::selectTarget() { 29 Triple TT; 30 31 // MCJIT can generate code for remote targets, but the old JIT and Interpreter 32 // must use the host architecture. 33 if (UseMCJIT && WhichEngine != EngineKind::Interpreter && M) 34 TT.setTriple(M->getTargetTriple()); 35 36 return selectTarget(TT, MArch, MCPU, MAttrs); 37} 38 39/// selectTarget - Pick a target either via -march or by guessing the native 40/// arch. Add any CPU features specified via -mcpu or -mattr. 41TargetMachine *EngineBuilder::selectTarget(const Triple &TargetTriple, 42 StringRef MArch, 43 StringRef MCPU, 44 const SmallVectorImpl<std::string>& MAttrs) { 45 Triple TheTriple(TargetTriple); 46 if (TheTriple.getTriple().empty()) 47 TheTriple.setTriple(sys::getProcessTriple()); 48 49 // Adjust the triple to match what the user requested. 50 const Target *TheTarget = 0; 51 if (!MArch.empty()) { 52 for (TargetRegistry::iterator it = TargetRegistry::begin(), 53 ie = TargetRegistry::end(); it != ie; ++it) { 54 if (MArch == it->getName()) { 55 TheTarget = &*it; 56 break; 57 } 58 } 59 60 if (!TheTarget) { 61 if (ErrorStr) 62 *ErrorStr = "No available targets are compatible with this -march, " 63 "see -version for the available targets.\n"; 64 return 0; 65 } 66 67 // Adjust the triple to match (if known), otherwise stick with the 68 // requested/host triple. 69 Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch); 70 if (Type != Triple::UnknownArch) 71 TheTriple.setArch(Type); 72 } else { 73 std::string Error; 74 TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error); 75 if (TheTarget == 0) { 76 if (ErrorStr) 77 *ErrorStr = Error; 78 return 0; 79 } 80 } 81 82 // Package up features to be passed to target/subtarget 83 std::string FeaturesStr; 84 if (!MAttrs.empty()) { 85 SubtargetFeatures Features; 86 for (unsigned i = 0; i != MAttrs.size(); ++i) 87 Features.AddFeature(MAttrs[i]); 88 FeaturesStr = Features.getString(); 89 } 90 91 // FIXME: non-iOS ARM FastISel is broken with MCJIT. 92 if (UseMCJIT && 93 TheTriple.getArch() == Triple::arm && 94 !TheTriple.isiOS() && 95 OptLevel == CodeGenOpt::None) { 96 OptLevel = CodeGenOpt::Less; 97 } 98 99 // Allocate a target... 100 TargetMachine *Target = TheTarget->createTargetMachine(TheTriple.getTriple(), 101 MCPU, FeaturesStr, 102 Options, 103 RelocModel, CMModel, 104 OptLevel); 105 assert(Target && "Could not allocate target machine!"); 106 return Target; 107} 108