2
0

TargetSelect.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //===-- TargetSelect.cpp - Target Chooser Code ----------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This just asks the TargetRegistry for the appropriate target to use, and
  11. // allows the user to specify a specific one on the commandline with -march=x,
  12. // -mcpu=y, and -mattr=a,-b,+c. Clients should initialize targets prior to
  13. // calling selectTarget().
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/ExecutionEngine/ExecutionEngine.h"
  17. #include "llvm/ADT/Triple.h"
  18. #include "llvm/IR/Module.h"
  19. #include "llvm/MC/SubtargetFeature.h"
  20. #include "llvm/Support/CommandLine.h"
  21. #include "llvm/Support/Host.h"
  22. #include "llvm/Support/TargetRegistry.h"
  23. #include "llvm/Target/TargetMachine.h"
  24. using namespace llvm;
  25. TargetMachine *EngineBuilder::selectTarget() {
  26. Triple TT;
  27. // MCJIT can generate code for remote targets, but the old JIT and Interpreter
  28. // must use the host architecture.
  29. if (WhichEngine != EngineKind::Interpreter && M)
  30. TT.setTriple(M->getTargetTriple());
  31. return selectTarget(TT, MArch, MCPU, MAttrs);
  32. }
  33. /// selectTarget - Pick a target either via -march or by guessing the native
  34. /// arch. Add any CPU features specified via -mcpu or -mattr.
  35. TargetMachine *EngineBuilder::selectTarget(const Triple &TargetTriple,
  36. StringRef MArch,
  37. StringRef MCPU,
  38. const SmallVectorImpl<std::string>& MAttrs) {
  39. Triple TheTriple(TargetTriple);
  40. if (TheTriple.getTriple().empty())
  41. TheTriple.setTriple(sys::getProcessTriple());
  42. // Adjust the triple to match what the user requested.
  43. const Target *TheTarget = nullptr;
  44. if (!MArch.empty()) {
  45. auto I = std::find_if(
  46. TargetRegistry::targets().begin(), TargetRegistry::targets().end(),
  47. [&](const Target &T) { return MArch == T.getName(); });
  48. if (I == TargetRegistry::targets().end()) {
  49. if (ErrorStr)
  50. *ErrorStr = "No available targets are compatible with this -march, "
  51. "see -version for the available targets.\n";
  52. return nullptr;
  53. }
  54. TheTarget = &*I;
  55. // Adjust the triple to match (if known), otherwise stick with the
  56. // requested/host triple.
  57. Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch);
  58. if (Type != Triple::UnknownArch)
  59. TheTriple.setArch(Type);
  60. } else {
  61. std::string Error;
  62. TheTarget = TargetRegistry::lookupTarget(TheTriple.getTriple(), Error);
  63. if (!TheTarget) {
  64. if (ErrorStr)
  65. *ErrorStr = Error;
  66. return nullptr;
  67. }
  68. }
  69. // Package up features to be passed to target/subtarget
  70. std::string FeaturesStr;
  71. if (!MAttrs.empty()) {
  72. SubtargetFeatures Features;
  73. for (unsigned i = 0; i != MAttrs.size(); ++i)
  74. Features.AddFeature(MAttrs[i]);
  75. FeaturesStr = Features.getString();
  76. }
  77. // FIXME: non-iOS ARM FastISel is broken with MCJIT.
  78. if (TheTriple.getArch() == Triple::arm &&
  79. !TheTriple.isiOS() &&
  80. OptLevel == CodeGenOpt::None) {
  81. OptLevel = CodeGenOpt::Less;
  82. }
  83. // Allocate a target...
  84. TargetMachine *Target = TheTarget->createTargetMachine(TheTriple.getTriple(),
  85. MCPU, FeaturesStr,
  86. Options,
  87. RelocModel, CMModel,
  88. OptLevel);
  89. assert(Target && "Could not allocate target machine!");
  90. return Target;
  91. }