PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/external/llvm/lib/Target/X86/X86Subtarget.cpp

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk
C++ | 436 lines | 304 code | 54 blank | 78 comment | 127 complexity | fd99191d38d874cca03b4d41449ddfc0 MD5 | raw file
  1. //===-- X86Subtarget.cpp - X86 Subtarget 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 implements the X86 specific subclass of TargetSubtargetInfo.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #define DEBUG_TYPE "subtarget"
  14. #include "X86Subtarget.h"
  15. #include "X86InstrInfo.h"
  16. #include "llvm/GlobalValue.h"
  17. #include "llvm/Support/Debug.h"
  18. #include "llvm/Support/ErrorHandling.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. #include "llvm/Support/Host.h"
  21. #include "llvm/Target/TargetMachine.h"
  22. #include "llvm/Target/TargetOptions.h"
  23. #define GET_SUBTARGETINFO_TARGET_DESC
  24. #define GET_SUBTARGETINFO_CTOR
  25. #include "X86GenSubtargetInfo.inc"
  26. using namespace llvm;
  27. #if defined(_MSC_VER)
  28. #include <intrin.h>
  29. #endif
  30. /// ClassifyBlockAddressReference - Classify a blockaddress reference for the
  31. /// current subtarget according to how we should reference it in a non-pcrel
  32. /// context.
  33. unsigned char X86Subtarget::
  34. ClassifyBlockAddressReference() const {
  35. if (isPICStyleGOT()) // 32-bit ELF targets.
  36. return X86II::MO_GOTOFF;
  37. if (isPICStyleStubPIC()) // Darwin/32 in PIC mode.
  38. return X86II::MO_PIC_BASE_OFFSET;
  39. // Direct static reference to label.
  40. return X86II::MO_NO_FLAG;
  41. }
  42. /// ClassifyGlobalReference - Classify a global variable reference for the
  43. /// current subtarget according to how we should reference it in a non-pcrel
  44. /// context.
  45. unsigned char X86Subtarget::
  46. ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
  47. // DLLImport only exists on windows, it is implemented as a load from a
  48. // DLLIMPORT stub.
  49. if (GV->hasDLLImportLinkage())
  50. return X86II::MO_DLLIMPORT;
  51. // Determine whether this is a reference to a definition or a declaration.
  52. // Materializable GVs (in JIT lazy compilation mode) do not require an extra
  53. // load from stub.
  54. bool isDecl = GV->hasAvailableExternallyLinkage();
  55. if (GV->isDeclaration() && !GV->isMaterializable())
  56. isDecl = true;
  57. // X86-64 in PIC mode.
  58. if (isPICStyleRIPRel()) {
  59. // Large model never uses stubs.
  60. if (TM.getCodeModel() == CodeModel::Large)
  61. return X86II::MO_NO_FLAG;
  62. if (isTargetDarwin()) {
  63. // If symbol visibility is hidden, the extra load is not needed if
  64. // target is x86-64 or the symbol is definitely defined in the current
  65. // translation unit.
  66. if (GV->hasDefaultVisibility() &&
  67. (isDecl || GV->isWeakForLinker()))
  68. return X86II::MO_GOTPCREL;
  69. } else if (!isTargetWin64()) {
  70. assert(isTargetELF() && "Unknown rip-relative target");
  71. // Extra load is needed for all externally visible.
  72. if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
  73. return X86II::MO_GOTPCREL;
  74. }
  75. return X86II::MO_NO_FLAG;
  76. }
  77. if (isPICStyleGOT()) { // 32-bit ELF targets.
  78. // Extra load is needed for all externally visible.
  79. if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
  80. return X86II::MO_GOTOFF;
  81. return X86II::MO_GOT;
  82. }
  83. if (isPICStyleStubPIC()) { // Darwin/32 in PIC mode.
  84. // Determine whether we have a stub reference and/or whether the reference
  85. // is relative to the PIC base or not.
  86. // If this is a strong reference to a definition, it is definitely not
  87. // through a stub.
  88. if (!isDecl && !GV->isWeakForLinker())
  89. return X86II::MO_PIC_BASE_OFFSET;
  90. // Unless we have a symbol with hidden visibility, we have to go through a
  91. // normal $non_lazy_ptr stub because this symbol might be resolved late.
  92. if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
  93. return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
  94. // If symbol visibility is hidden, we have a stub for common symbol
  95. // references and external declarations.
  96. if (isDecl || GV->hasCommonLinkage()) {
  97. // Hidden $non_lazy_ptr reference.
  98. return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
  99. }
  100. // Otherwise, no stub.
  101. return X86II::MO_PIC_BASE_OFFSET;
  102. }
  103. if (isPICStyleStubNoDynamic()) { // Darwin/32 in -mdynamic-no-pic mode.
  104. // Determine whether we have a stub reference.
  105. // If this is a strong reference to a definition, it is definitely not
  106. // through a stub.
  107. if (!isDecl && !GV->isWeakForLinker())
  108. return X86II::MO_NO_FLAG;
  109. // Unless we have a symbol with hidden visibility, we have to go through a
  110. // normal $non_lazy_ptr stub because this symbol might be resolved late.
  111. if (!GV->hasHiddenVisibility()) // Non-hidden $non_lazy_ptr reference.
  112. return X86II::MO_DARWIN_NONLAZY;
  113. // Otherwise, no stub.
  114. return X86II::MO_NO_FLAG;
  115. }
  116. // Direct static reference to global.
  117. return X86II::MO_NO_FLAG;
  118. }
  119. /// getBZeroEntry - This function returns the name of a function which has an
  120. /// interface like the non-standard bzero function, if such a function exists on
  121. /// the current subtarget and it is considered prefereable over memset with zero
  122. /// passed as the second argument. Otherwise it returns null.
  123. const char *X86Subtarget::getBZeroEntry() const {
  124. // Darwin 10 has a __bzero entry point for this purpose.
  125. if (getTargetTriple().isMacOSX() &&
  126. !getTargetTriple().isMacOSXVersionLT(10, 6))
  127. return "__bzero";
  128. return 0;
  129. }
  130. /// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
  131. /// to immediate address.
  132. bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
  133. if (In64BitMode)
  134. return false;
  135. return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
  136. }
  137. /// getSpecialAddressLatency - For targets where it is beneficial to
  138. /// backschedule instructions that compute addresses, return a value
  139. /// indicating the number of scheduling cycles of backscheduling that
  140. /// should be attempted.
  141. unsigned X86Subtarget::getSpecialAddressLatency() const {
  142. // For x86 out-of-order targets, back-schedule address computations so
  143. // that loads and stores aren't blocked.
  144. // This value was chosen arbitrarily.
  145. return 200;
  146. }
  147. void X86Subtarget::AutoDetectSubtargetFeatures() {
  148. unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
  149. unsigned MaxLevel;
  150. union {
  151. unsigned u[3];
  152. char c[12];
  153. } text;
  154. if (X86_MC::GetCpuIDAndInfo(0, &MaxLevel, text.u+0, text.u+2, text.u+1) ||
  155. MaxLevel < 1)
  156. return;
  157. X86_MC::GetCpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
  158. if ((EDX >> 15) & 1) { HasCMov = true; ToggleFeature(X86::FeatureCMOV); }
  159. if ((EDX >> 23) & 1) { X86SSELevel = MMX; ToggleFeature(X86::FeatureMMX); }
  160. if ((EDX >> 25) & 1) { X86SSELevel = SSE1; ToggleFeature(X86::FeatureSSE1); }
  161. if ((EDX >> 26) & 1) { X86SSELevel = SSE2; ToggleFeature(X86::FeatureSSE2); }
  162. if (ECX & 0x1) { X86SSELevel = SSE3; ToggleFeature(X86::FeatureSSE3); }
  163. if ((ECX >> 9) & 1) { X86SSELevel = SSSE3; ToggleFeature(X86::FeatureSSSE3);}
  164. if ((ECX >> 19) & 1) { X86SSELevel = SSE41; ToggleFeature(X86::FeatureSSE41);}
  165. if ((ECX >> 20) & 1) { X86SSELevel = SSE42; ToggleFeature(X86::FeatureSSE42);}
  166. if ((ECX >> 28) & 1) { X86SSELevel = AVX; ToggleFeature(X86::FeatureAVX); }
  167. bool IsIntel = memcmp(text.c, "GenuineIntel", 12) == 0;
  168. bool IsAMD = !IsIntel && memcmp(text.c, "AuthenticAMD", 12) == 0;
  169. if ((ECX >> 1) & 0x1) {
  170. HasPCLMUL = true;
  171. ToggleFeature(X86::FeaturePCLMUL);
  172. }
  173. if ((ECX >> 12) & 0x1) {
  174. HasFMA = true;
  175. ToggleFeature(X86::FeatureFMA);
  176. }
  177. if (IsIntel && ((ECX >> 22) & 0x1)) {
  178. HasMOVBE = true;
  179. ToggleFeature(X86::FeatureMOVBE);
  180. }
  181. if ((ECX >> 23) & 0x1) {
  182. HasPOPCNT = true;
  183. ToggleFeature(X86::FeaturePOPCNT);
  184. }
  185. if ((ECX >> 25) & 0x1) {
  186. HasAES = true;
  187. ToggleFeature(X86::FeatureAES);
  188. }
  189. if ((ECX >> 29) & 0x1) {
  190. HasF16C = true;
  191. ToggleFeature(X86::FeatureF16C);
  192. }
  193. if (IsIntel && ((ECX >> 30) & 0x1)) {
  194. HasRDRAND = true;
  195. ToggleFeature(X86::FeatureRDRAND);
  196. }
  197. if ((ECX >> 13) & 0x1) {
  198. HasCmpxchg16b = true;
  199. ToggleFeature(X86::FeatureCMPXCHG16B);
  200. }
  201. if (IsIntel || IsAMD) {
  202. // Determine if bit test memory instructions are slow.
  203. unsigned Family = 0;
  204. unsigned Model = 0;
  205. X86_MC::DetectFamilyModel(EAX, Family, Model);
  206. if (IsAMD || (Family == 6 && Model >= 13)) {
  207. IsBTMemSlow = true;
  208. ToggleFeature(X86::FeatureSlowBTMem);
  209. }
  210. // If it's Nehalem, unaligned memory access is fast.
  211. // Include Westmere and Sandy Bridge as well.
  212. // FIXME: add later processors.
  213. if (IsIntel && ((Family == 6 && Model == 26) ||
  214. (Family == 6 && Model == 44) ||
  215. (Family == 6 && Model == 42))) {
  216. IsUAMemFast = true;
  217. ToggleFeature(X86::FeatureFastUAMem);
  218. }
  219. // Set processor type. Currently only Atom is detected.
  220. if (Family == 6 &&
  221. (Model == 28 || Model == 38 || Model == 39
  222. || Model == 53 || Model == 54)) {
  223. X86ProcFamily = IntelAtom;
  224. UseLeaForSP = true;
  225. ToggleFeature(X86::FeatureLeaForSP);
  226. }
  227. unsigned MaxExtLevel;
  228. X86_MC::GetCpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
  229. if (MaxExtLevel >= 0x80000001) {
  230. X86_MC::GetCpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
  231. if ((EDX >> 29) & 0x1) {
  232. HasX86_64 = true;
  233. ToggleFeature(X86::Feature64Bit);
  234. }
  235. if ((ECX >> 5) & 0x1) {
  236. HasLZCNT = true;
  237. ToggleFeature(X86::FeatureLZCNT);
  238. }
  239. if (IsAMD) {
  240. if ((ECX >> 6) & 0x1) {
  241. HasSSE4A = true;
  242. ToggleFeature(X86::FeatureSSE4A);
  243. }
  244. if ((ECX >> 11) & 0x1) {
  245. HasXOP = true;
  246. ToggleFeature(X86::FeatureXOP);
  247. }
  248. if ((ECX >> 16) & 0x1) {
  249. HasFMA4 = true;
  250. ToggleFeature(X86::FeatureFMA4);
  251. }
  252. }
  253. }
  254. }
  255. if (MaxLevel >= 7) {
  256. if (!X86_MC::GetCpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX)) {
  257. if (IsIntel && (EBX & 0x1)) {
  258. HasFSGSBase = true;
  259. ToggleFeature(X86::FeatureFSGSBase);
  260. }
  261. if ((EBX >> 3) & 0x1) {
  262. HasBMI = true;
  263. ToggleFeature(X86::FeatureBMI);
  264. }
  265. if (IsIntel && ((EBX >> 5) & 0x1)) {
  266. X86SSELevel = AVX2;
  267. ToggleFeature(X86::FeatureAVX2);
  268. }
  269. if (IsIntel && ((EBX >> 8) & 0x1)) {
  270. HasBMI2 = true;
  271. ToggleFeature(X86::FeatureBMI2);
  272. }
  273. }
  274. }
  275. }
  276. X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
  277. const std::string &FS,
  278. unsigned StackAlignOverride, bool is64Bit)
  279. : X86GenSubtargetInfo(TT, CPU, FS)
  280. , X86ProcFamily(Others)
  281. , PICStyle(PICStyles::None)
  282. , X86SSELevel(NoMMXSSE)
  283. , X863DNowLevel(NoThreeDNow)
  284. , HasCMov(false)
  285. , HasX86_64(false)
  286. , HasPOPCNT(false)
  287. , HasSSE4A(false)
  288. , HasAES(false)
  289. , HasPCLMUL(false)
  290. , HasFMA(false)
  291. , HasFMA4(false)
  292. , HasXOP(false)
  293. , HasMOVBE(false)
  294. , HasRDRAND(false)
  295. , HasF16C(false)
  296. , HasFSGSBase(false)
  297. , HasLZCNT(false)
  298. , HasBMI(false)
  299. , HasBMI2(false)
  300. , IsBTMemSlow(false)
  301. , IsUAMemFast(false)
  302. , HasVectorUAMem(false)
  303. , HasCmpxchg16b(false)
  304. , UseLeaForSP(false)
  305. , HasSlowDivide(false)
  306. , PostRAScheduler(false)
  307. , stackAlignment(4)
  308. // FIXME: this is a known good value for Yonah. How about others?
  309. , MaxInlineSizeThreshold(128)
  310. , TargetTriple(TT)
  311. , In64BitMode(is64Bit) {
  312. // Determine default and user specified characteristics
  313. std::string CPUName = CPU;
  314. if (!FS.empty() || !CPU.empty()) {
  315. if (CPUName.empty()) {
  316. #if defined(i386) || defined(__i386__) || defined(__x86__) || defined(_M_IX86)\
  317. || defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
  318. CPUName = sys::getHostCPUName();
  319. #else
  320. CPUName = "generic";
  321. #endif
  322. }
  323. // Make sure 64-bit features are available in 64-bit mode. (But make sure
  324. // SSE2 can be turned off explicitly.)
  325. std::string FullFS = FS;
  326. if (In64BitMode) {
  327. if (!FullFS.empty())
  328. FullFS = "+64bit,+sse2," + FullFS;
  329. else
  330. FullFS = "+64bit,+sse2";
  331. }
  332. // If feature string is not empty, parse features string.
  333. ParseSubtargetFeatures(CPUName, FullFS);
  334. } else {
  335. if (CPUName.empty()) {
  336. #if defined (__x86_64__) || defined(__i386__)
  337. CPUName = sys::getHostCPUName();
  338. #else
  339. CPUName = "generic";
  340. #endif
  341. }
  342. // Otherwise, use CPUID to auto-detect feature set.
  343. AutoDetectSubtargetFeatures();
  344. // Make sure 64-bit features are available in 64-bit mode.
  345. if (In64BitMode) {
  346. HasX86_64 = true; ToggleFeature(X86::Feature64Bit);
  347. HasCMov = true; ToggleFeature(X86::FeatureCMOV);
  348. if (X86SSELevel < SSE2) {
  349. X86SSELevel = SSE2;
  350. ToggleFeature(X86::FeatureSSE1);
  351. ToggleFeature(X86::FeatureSSE2);
  352. }
  353. }
  354. }
  355. if (X86ProcFamily == IntelAtom)
  356. PostRAScheduler = true;
  357. InstrItins = getInstrItineraryForCPU(CPUName);
  358. // It's important to keep the MCSubtargetInfo feature bits in sync with
  359. // target data structure which is shared with MC code emitter, etc.
  360. if (In64BitMode)
  361. ToggleFeature(X86::Mode64Bit);
  362. DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
  363. << ", 3DNowLevel " << X863DNowLevel
  364. << ", 64bit " << HasX86_64 << "\n");
  365. assert((!In64BitMode || HasX86_64) &&
  366. "64-bit code requested on a subtarget that doesn't support it!");
  367. // Stack alignment is 16 bytes on Darwin, FreeBSD, Linux and Solaris (both
  368. // 32 and 64 bit) and for all 64-bit targets.
  369. if (StackAlignOverride)
  370. stackAlignment = StackAlignOverride;
  371. else if (isTargetDarwin() || isTargetFreeBSD() || isTargetLinux() ||
  372. isTargetSolaris() || In64BitMode)
  373. stackAlignment = 16;
  374. }
  375. bool X86Subtarget::enablePostRAScheduler(
  376. CodeGenOpt::Level OptLevel,
  377. TargetSubtargetInfo::AntiDepBreakMode& Mode,
  378. RegClassVector& CriticalPathRCs) const {
  379. Mode = TargetSubtargetInfo::ANTIDEP_CRITICAL;
  380. CriticalPathRCs.clear();
  381. return PostRAScheduler && OptLevel >= CodeGenOpt::Default;
  382. }