TargetMachine.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //===-- TargetMachine.cpp - General Target Information ---------------------==//
  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 file describes the general parts of a Target machine.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Target/TargetMachine.h"
  14. #include "llvm/Analysis/TargetTransformInfo.h"
  15. #include "llvm/CodeGen/MachineFunction.h"
  16. #include "llvm/IR/Function.h"
  17. #include "llvm/IR/GlobalAlias.h"
  18. #include "llvm/IR/GlobalValue.h"
  19. #include "llvm/IR/GlobalVariable.h"
  20. #include "llvm/IR/Mangler.h"
  21. #include "llvm/MC/MCAsmInfo.h"
  22. #include "llvm/MC/MCCodeGenInfo.h"
  23. #include "llvm/MC/MCContext.h"
  24. #include "llvm/MC/MCInstrInfo.h"
  25. #include "llvm/MC/MCSectionMachO.h"
  26. #include "llvm/MC/MCTargetOptions.h"
  27. #include "llvm/MC/SectionKind.h"
  28. #include "llvm/IR/LegacyPassManager.h"
  29. #include "llvm/Support/CommandLine.h"
  30. #include "llvm/Target/TargetLowering.h"
  31. #include "llvm/Target/TargetLoweringObjectFile.h"
  32. #include "llvm/Target/TargetSubtargetInfo.h"
  33. using namespace llvm;
  34. //---------------------------------------------------------------------------
  35. // TargetMachine Class
  36. //
  37. TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString,
  38. const Triple &TT, StringRef CPU, StringRef FS,
  39. const TargetOptions &Options)
  40. : TheTarget(T), DL(DataLayoutString), TargetTriple(TT), TargetCPU(CPU),
  41. TargetFS(FS), CodeGenInfo(nullptr), AsmInfo(nullptr), MRI(nullptr),
  42. MII(nullptr), STI(nullptr), RequireStructuredCFG(false),
  43. Options(Options) {}
  44. TargetMachine::~TargetMachine() {
  45. delete CodeGenInfo;
  46. delete AsmInfo;
  47. delete MRI;
  48. delete MII;
  49. delete STI;
  50. }
  51. /// \brief Reset the target options based on the function's attributes.
  52. // FIXME: This function needs to go away for a number of reasons:
  53. // a) global state on the TargetMachine is terrible in general,
  54. // b) there's no default state here to keep,
  55. // c) these target options should be passed only on the function
  56. // and not on the TargetMachine (via TargetOptions) at all.
  57. void TargetMachine::resetTargetOptions(const Function &F) const {
  58. #define RESET_OPTION(X, Y) \
  59. do { \
  60. if (F.hasFnAttribute(Y)) \
  61. Options.X = (F.getFnAttribute(Y).getValueAsString() == "true"); \
  62. } while (0)
  63. RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
  64. RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
  65. RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
  66. RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
  67. }
  68. /// getRelocationModel - Returns the code generation relocation model. The
  69. /// choices are static, PIC, and dynamic-no-pic, and target default.
  70. Reloc::Model TargetMachine::getRelocationModel() const {
  71. if (!CodeGenInfo)
  72. return Reloc::Default;
  73. return CodeGenInfo->getRelocationModel();
  74. }
  75. /// getCodeModel - Returns the code model. The choices are small, kernel,
  76. /// medium, large, and target default.
  77. CodeModel::Model TargetMachine::getCodeModel() const {
  78. if (!CodeGenInfo)
  79. return CodeModel::Default;
  80. return CodeGenInfo->getCodeModel();
  81. }
  82. /// Get the IR-specified TLS model for Var.
  83. static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) {
  84. switch (GV->getThreadLocalMode()) {
  85. case GlobalVariable::NotThreadLocal:
  86. llvm_unreachable("getSelectedTLSModel for non-TLS variable");
  87. break;
  88. case GlobalVariable::GeneralDynamicTLSModel:
  89. return TLSModel::GeneralDynamic;
  90. case GlobalVariable::LocalDynamicTLSModel:
  91. return TLSModel::LocalDynamic;
  92. case GlobalVariable::InitialExecTLSModel:
  93. return TLSModel::InitialExec;
  94. case GlobalVariable::LocalExecTLSModel:
  95. return TLSModel::LocalExec;
  96. }
  97. llvm_unreachable("invalid TLS model");
  98. }
  99. TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
  100. bool isLocal = GV->hasLocalLinkage();
  101. bool isDeclaration = GV->isDeclaration();
  102. bool isPIC = getRelocationModel() == Reloc::PIC_;
  103. bool isPIE = Options.PositionIndependentExecutable;
  104. // FIXME: what should we do for protected and internal visibility?
  105. // For variables, is internal different from hidden?
  106. bool isHidden = GV->hasHiddenVisibility();
  107. TLSModel::Model Model;
  108. if (isPIC && !isPIE) {
  109. if (isLocal || isHidden)
  110. Model = TLSModel::LocalDynamic;
  111. else
  112. Model = TLSModel::GeneralDynamic;
  113. } else {
  114. if (!isDeclaration || isHidden)
  115. Model = TLSModel::LocalExec;
  116. else
  117. Model = TLSModel::InitialExec;
  118. }
  119. // If the user specified a more specific model, use that.
  120. TLSModel::Model SelectedModel = getSelectedTLSModel(GV);
  121. if (SelectedModel > Model)
  122. return SelectedModel;
  123. return Model;
  124. }
  125. /// getOptLevel - Returns the optimization level: None, Less,
  126. /// Default, or Aggressive.
  127. CodeGenOpt::Level TargetMachine::getOptLevel() const {
  128. if (!CodeGenInfo)
  129. return CodeGenOpt::Default;
  130. return CodeGenInfo->getOptLevel();
  131. }
  132. void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
  133. if (CodeGenInfo)
  134. CodeGenInfo->setOptLevel(Level);
  135. }
  136. TargetIRAnalysis TargetMachine::getTargetIRAnalysis() {
  137. return TargetIRAnalysis([](Function &F) {
  138. return TargetTransformInfo(F.getParent()->getDataLayout());
  139. });
  140. }
  141. static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
  142. const MCSection &Section) {
  143. if (!AsmInfo.isSectionAtomizableBySymbols(Section))
  144. return true;
  145. // If it is not dead stripped, it is safe to use private labels.
  146. const MCSectionMachO &SMO = cast<MCSectionMachO>(Section);
  147. if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP))
  148. return true;
  149. return false;
  150. }
  151. void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
  152. const GlobalValue *GV, Mangler &Mang,
  153. bool MayAlwaysUsePrivate) const {
  154. if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
  155. // Simple case: If GV is not private, it is not important to find out if
  156. // private labels are legal in this case or not.
  157. Mang.getNameWithPrefix(Name, GV, false);
  158. return;
  159. }
  160. SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this);
  161. const TargetLoweringObjectFile *TLOF = getObjFileLowering();
  162. const MCSection *TheSection = TLOF->SectionForGlobal(GV, GVKind, Mang, *this);
  163. bool CannotUsePrivateLabel = !canUsePrivateLabel(*AsmInfo, *TheSection);
  164. TLOF->getNameWithPrefix(Name, GV, CannotUsePrivateLabel, Mang, *this);
  165. }
  166. MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const {
  167. SmallString<128> NameStr;
  168. getNameWithPrefix(NameStr, GV, Mang);
  169. const TargetLoweringObjectFile *TLOF = getObjFileLowering();
  170. return TLOF->getContext().getOrCreateSymbol(NameStr);
  171. }