X86Subtarget.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. //===-- X86Subtarget.cpp - X86 Subtarget Information ----------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the X86 specific subclass of TargetSubtargetInfo.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "X86Subtarget.h"
  13. #include "MCTargetDesc/X86BaseInfo.h"
  14. #include "X86.h"
  15. #include "X86CallLowering.h"
  16. #include "X86LegalizerInfo.h"
  17. #include "X86MacroFusion.h"
  18. #include "X86RegisterBankInfo.h"
  19. #include "X86TargetMachine.h"
  20. #include "llvm/ADT/Triple.h"
  21. #include "llvm/CodeGen/GlobalISel/CallLowering.h"
  22. #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
  23. #include "llvm/IR/Attributes.h"
  24. #include "llvm/IR/ConstantRange.h"
  25. #include "llvm/IR/Function.h"
  26. #include "llvm/IR/GlobalValue.h"
  27. #include "llvm/Support/Casting.h"
  28. #include "llvm/Support/CodeGen.h"
  29. #include "llvm/Support/CommandLine.h"
  30. #include "llvm/Support/Debug.h"
  31. #include "llvm/Support/ErrorHandling.h"
  32. #include "llvm/Support/raw_ostream.h"
  33. #include "llvm/Target/TargetMachine.h"
  34. #if defined(_MSC_VER)
  35. #include <intrin.h>
  36. #endif
  37. using namespace llvm;
  38. #define DEBUG_TYPE "subtarget"
  39. #define GET_SUBTARGETINFO_TARGET_DESC
  40. #define GET_SUBTARGETINFO_CTOR
  41. #include "X86GenSubtargetInfo.inc"
  42. // Temporary option to control early if-conversion for x86 while adding machine
  43. // models.
  44. static cl::opt<bool>
  45. X86EarlyIfConv("x86-early-ifcvt", cl::Hidden,
  46. cl::desc("Enable early if-conversion on X86"));
  47. /// Classify a blockaddress reference for the current subtarget according to how
  48. /// we should reference it in a non-pcrel context.
  49. unsigned char X86Subtarget::classifyBlockAddressReference() const {
  50. return classifyLocalReference(nullptr);
  51. }
  52. /// Classify a global variable reference for the current subtarget according to
  53. /// how we should reference it in a non-pcrel context.
  54. unsigned char
  55. X86Subtarget::classifyGlobalReference(const GlobalValue *GV) const {
  56. return classifyGlobalReference(GV, *GV->getParent());
  57. }
  58. unsigned char
  59. X86Subtarget::classifyLocalReference(const GlobalValue *GV) const {
  60. // Tagged globals have non-zero upper bits, which makes direct references
  61. // require a 64-bit immediate. On the small code model this causes relocation
  62. // errors, so we go through the GOT instead.
  63. if (AllowTaggedGlobals && TM.getCodeModel() == CodeModel::Small && GV &&
  64. !isa<Function>(GV))
  65. return X86II::MO_GOTPCREL_NORELAX;
  66. // If we're not PIC, it's not very interesting.
  67. if (!isPositionIndependent())
  68. return X86II::MO_NO_FLAG;
  69. if (is64Bit()) {
  70. // 64-bit ELF PIC local references may use GOTOFF relocations.
  71. if (isTargetELF()) {
  72. switch (TM.getCodeModel()) {
  73. // 64-bit small code model is simple: All rip-relative.
  74. case CodeModel::Tiny:
  75. llvm_unreachable("Tiny codesize model not supported on X86");
  76. case CodeModel::Small:
  77. case CodeModel::Kernel:
  78. return X86II::MO_NO_FLAG;
  79. // The large PIC code model uses GOTOFF.
  80. case CodeModel::Large:
  81. return X86II::MO_GOTOFF;
  82. // Medium is a hybrid: RIP-rel for code, GOTOFF for DSO local data.
  83. case CodeModel::Medium:
  84. // Constant pool and jump table handling pass a nullptr to this
  85. // function so we need to use isa_and_nonnull.
  86. if (isa_and_nonnull<Function>(GV))
  87. return X86II::MO_NO_FLAG; // All code is RIP-relative
  88. return X86II::MO_GOTOFF; // Local symbols use GOTOFF.
  89. }
  90. llvm_unreachable("invalid code model");
  91. }
  92. // Otherwise, this is either a RIP-relative reference or a 64-bit movabsq,
  93. // both of which use MO_NO_FLAG.
  94. return X86II::MO_NO_FLAG;
  95. }
  96. // The COFF dynamic linker just patches the executable sections.
  97. if (isTargetCOFF())
  98. return X86II::MO_NO_FLAG;
  99. if (isTargetDarwin()) {
  100. // 32 bit macho has no relocation for a-b if a is undefined, even if
  101. // b is in the section that is being relocated.
  102. // This means we have to use o load even for GVs that are known to be
  103. // local to the dso.
  104. if (GV && (GV->isDeclarationForLinker() || GV->hasCommonLinkage()))
  105. return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
  106. return X86II::MO_PIC_BASE_OFFSET;
  107. }
  108. return X86II::MO_GOTOFF;
  109. }
  110. unsigned char X86Subtarget::classifyGlobalReference(const GlobalValue *GV,
  111. const Module &M) const {
  112. // The static large model never uses stubs.
  113. if (TM.getCodeModel() == CodeModel::Large && !isPositionIndependent())
  114. return X86II::MO_NO_FLAG;
  115. // Absolute symbols can be referenced directly.
  116. if (GV) {
  117. if (Optional<ConstantRange> CR = GV->getAbsoluteSymbolRange()) {
  118. // See if we can use the 8-bit immediate form. Note that some instructions
  119. // will sign extend the immediate operand, so to be conservative we only
  120. // accept the range [0,128).
  121. if (CR->getUnsignedMax().ult(128))
  122. return X86II::MO_ABS8;
  123. else
  124. return X86II::MO_NO_FLAG;
  125. }
  126. }
  127. if (TM.shouldAssumeDSOLocal(M, GV))
  128. return classifyLocalReference(GV);
  129. if (isTargetCOFF()) {
  130. // ExternalSymbolSDNode like _tls_index.
  131. if (!GV)
  132. return X86II::MO_NO_FLAG;
  133. if (GV->hasDLLImportStorageClass())
  134. return X86II::MO_DLLIMPORT;
  135. return X86II::MO_COFFSTUB;
  136. }
  137. // Some JIT users use *-win32-elf triples; these shouldn't use GOT tables.
  138. if (isOSWindows())
  139. return X86II::MO_NO_FLAG;
  140. if (is64Bit()) {
  141. // ELF supports a large, truly PIC code model with non-PC relative GOT
  142. // references. Other object file formats do not. Use the no-flag, 64-bit
  143. // reference for them.
  144. if (TM.getCodeModel() == CodeModel::Large)
  145. return isTargetELF() ? X86II::MO_GOT : X86II::MO_NO_FLAG;
  146. // Tagged globals have non-zero upper bits, which makes direct references
  147. // require a 64-bit immediate. So we can't let the linker relax the
  148. // relocation to a 32-bit RIP-relative direct reference.
  149. if (AllowTaggedGlobals && GV && !isa<Function>(GV))
  150. return X86II::MO_GOTPCREL_NORELAX;
  151. return X86II::MO_GOTPCREL;
  152. }
  153. if (isTargetDarwin()) {
  154. if (!isPositionIndependent())
  155. return X86II::MO_DARWIN_NONLAZY;
  156. return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
  157. }
  158. // 32-bit ELF references GlobalAddress directly in static relocation model.
  159. // We cannot use MO_GOT because EBX may not be set up.
  160. if (TM.getRelocationModel() == Reloc::Static)
  161. return X86II::MO_NO_FLAG;
  162. return X86II::MO_GOT;
  163. }
  164. unsigned char
  165. X86Subtarget::classifyGlobalFunctionReference(const GlobalValue *GV) const {
  166. return classifyGlobalFunctionReference(GV, *GV->getParent());
  167. }
  168. unsigned char
  169. X86Subtarget::classifyGlobalFunctionReference(const GlobalValue *GV,
  170. const Module &M) const {
  171. if (TM.shouldAssumeDSOLocal(M, GV))
  172. return X86II::MO_NO_FLAG;
  173. // Functions on COFF can be non-DSO local for three reasons:
  174. // - They are intrinsic functions (!GV)
  175. // - They are marked dllimport
  176. // - They are extern_weak, and a stub is needed
  177. if (isTargetCOFF()) {
  178. if (!GV)
  179. return X86II::MO_NO_FLAG;
  180. if (GV->hasDLLImportStorageClass())
  181. return X86II::MO_DLLIMPORT;
  182. return X86II::MO_COFFSTUB;
  183. }
  184. const Function *F = dyn_cast_or_null<Function>(GV);
  185. if (isTargetELF()) {
  186. if (is64Bit() && F && (CallingConv::X86_RegCall == F->getCallingConv()))
  187. // According to psABI, PLT stub clobbers XMM8-XMM15.
  188. // In Regcall calling convention those registers are used for passing
  189. // parameters. Thus we need to prevent lazy binding in Regcall.
  190. return X86II::MO_GOTPCREL;
  191. // If PLT must be avoided then the call should be via GOTPCREL.
  192. if (((F && F->hasFnAttribute(Attribute::NonLazyBind)) ||
  193. (!F && M.getRtLibUseGOT())) &&
  194. is64Bit())
  195. return X86II::MO_GOTPCREL;
  196. // Reference ExternalSymbol directly in static relocation model.
  197. if (!is64Bit() && !GV && TM.getRelocationModel() == Reloc::Static)
  198. return X86II::MO_NO_FLAG;
  199. return X86II::MO_PLT;
  200. }
  201. if (is64Bit()) {
  202. if (F && F->hasFnAttribute(Attribute::NonLazyBind))
  203. // If the function is marked as non-lazy, generate an indirect call
  204. // which loads from the GOT directly. This avoids runtime overhead
  205. // at the cost of eager binding (and one extra byte of encoding).
  206. return X86II::MO_GOTPCREL;
  207. return X86II::MO_NO_FLAG;
  208. }
  209. return X86II::MO_NO_FLAG;
  210. }
  211. /// Return true if the subtarget allows calls to immediate address.
  212. bool X86Subtarget::isLegalToCallImmediateAddr() const {
  213. // FIXME: I386 PE/COFF supports PC relative calls using IMAGE_REL_I386_REL32
  214. // but WinCOFFObjectWriter::RecordRelocation cannot emit them. Once it does,
  215. // the following check for Win32 should be removed.
  216. if (In64BitMode || isTargetWin32())
  217. return false;
  218. return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
  219. }
  220. void X86Subtarget::initSubtargetFeatures(StringRef CPU, StringRef TuneCPU,
  221. StringRef FS) {
  222. if (CPU.empty())
  223. CPU = "generic";
  224. if (TuneCPU.empty())
  225. TuneCPU = "i586"; // FIXME: "generic" is more modern than llc tests expect.
  226. std::string FullFS = X86_MC::ParseX86Triple(TargetTriple);
  227. assert(!FullFS.empty() && "Failed to parse X86 triple");
  228. if (!FS.empty())
  229. FullFS = (Twine(FullFS) + "," + FS).str();
  230. // Parse features string and set the CPU.
  231. ParseSubtargetFeatures(CPU, TuneCPU, FullFS);
  232. // All CPUs that implement SSE4.2 or SSE4A support unaligned accesses of
  233. // 16-bytes and under that are reasonably fast. These features were
  234. // introduced with Intel's Nehalem/Silvermont and AMD's Family10h
  235. // micro-architectures respectively.
  236. if (hasSSE42() || hasSSE4A())
  237. IsUAMem16Slow = false;
  238. LLVM_DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
  239. << ", 3DNowLevel " << X863DNowLevel << ", 64bit "
  240. << HasX86_64 << "\n");
  241. if (In64BitMode && !HasX86_64)
  242. report_fatal_error("64-bit code requested on a subtarget that doesn't "
  243. "support it!");
  244. // Stack alignment is 16 bytes on Darwin, Linux, kFreeBSD, NaCl, and for all
  245. // 64-bit targets. On Solaris (32-bit), stack alignment is 4 bytes
  246. // following the i386 psABI, while on Illumos it is always 16 bytes.
  247. if (StackAlignOverride)
  248. stackAlignment = *StackAlignOverride;
  249. else if (isTargetDarwin() || isTargetLinux() || isTargetKFreeBSD() ||
  250. isTargetNaCl() || In64BitMode)
  251. stackAlignment = Align(16);
  252. // Consume the vector width attribute or apply any target specific limit.
  253. if (PreferVectorWidthOverride)
  254. PreferVectorWidth = PreferVectorWidthOverride;
  255. else if (Prefer128Bit)
  256. PreferVectorWidth = 128;
  257. else if (Prefer256Bit)
  258. PreferVectorWidth = 256;
  259. }
  260. X86Subtarget &X86Subtarget::initializeSubtargetDependencies(StringRef CPU,
  261. StringRef TuneCPU,
  262. StringRef FS) {
  263. initSubtargetFeatures(CPU, TuneCPU, FS);
  264. return *this;
  265. }
  266. X86Subtarget::X86Subtarget(const Triple &TT, StringRef CPU, StringRef TuneCPU,
  267. StringRef FS, const X86TargetMachine &TM,
  268. MaybeAlign StackAlignOverride,
  269. unsigned PreferVectorWidthOverride,
  270. unsigned RequiredVectorWidth)
  271. : X86GenSubtargetInfo(TT, CPU, TuneCPU, FS),
  272. PICStyle(PICStyles::Style::None), TM(TM), TargetTriple(TT),
  273. StackAlignOverride(StackAlignOverride),
  274. PreferVectorWidthOverride(PreferVectorWidthOverride),
  275. RequiredVectorWidth(RequiredVectorWidth),
  276. InstrInfo(initializeSubtargetDependencies(CPU, TuneCPU, FS)),
  277. TLInfo(TM, *this), FrameLowering(*this, getStackAlignment()) {
  278. // Determine the PICStyle based on the target selected.
  279. if (!isPositionIndependent())
  280. setPICStyle(PICStyles::Style::None);
  281. else if (is64Bit())
  282. setPICStyle(PICStyles::Style::RIPRel);
  283. else if (isTargetCOFF())
  284. setPICStyle(PICStyles::Style::None);
  285. else if (isTargetDarwin())
  286. setPICStyle(PICStyles::Style::StubPIC);
  287. else if (isTargetELF())
  288. setPICStyle(PICStyles::Style::GOT);
  289. CallLoweringInfo.reset(new X86CallLowering(*getTargetLowering()));
  290. Legalizer.reset(new X86LegalizerInfo(*this, TM));
  291. auto *RBI = new X86RegisterBankInfo(*getRegisterInfo());
  292. RegBankInfo.reset(RBI);
  293. InstSelector.reset(createX86InstructionSelector(TM, *this, *RBI));
  294. }
  295. const CallLowering *X86Subtarget::getCallLowering() const {
  296. return CallLoweringInfo.get();
  297. }
  298. InstructionSelector *X86Subtarget::getInstructionSelector() const {
  299. return InstSelector.get();
  300. }
  301. const LegalizerInfo *X86Subtarget::getLegalizerInfo() const {
  302. return Legalizer.get();
  303. }
  304. const RegisterBankInfo *X86Subtarget::getRegBankInfo() const {
  305. return RegBankInfo.get();
  306. }
  307. bool X86Subtarget::enableEarlyIfConversion() const {
  308. return hasCMov() && X86EarlyIfConv;
  309. }
  310. void X86Subtarget::getPostRAMutations(
  311. std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations) const {
  312. Mutations.push_back(createX86MacroFusionDAGMutation());
  313. }
  314. bool X86Subtarget::isPositionIndependent() const {
  315. return TM.isPositionIndependent();
  316. }