X86Subtarget.cpp 13 KB

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