X86Subtarget.cpp revision dce4a407a24b04eebc6a376f8e62b41aaa7b071f
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
14#include "X86Subtarget.h"
15#include "X86InstrInfo.h"
16#include "llvm/IR/Attributes.h"
17#include "llvm/IR/Function.h"
18#include "llvm/IR/GlobalValue.h"
19#include "llvm/Support/CommandLine.h"
20#include "llvm/Support/Debug.h"
21#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/Host.h"
23#include "llvm/Support/raw_ostream.h"
24#include "llvm/Target/TargetMachine.h"
25#include "llvm/Target/TargetOptions.h"
26
27#if defined(_MSC_VER)
28#include <intrin.h>
29#endif
30
31using namespace llvm;
32
33#define DEBUG_TYPE "subtarget"
34
35#define GET_SUBTARGETINFO_TARGET_DESC
36#define GET_SUBTARGETINFO_CTOR
37#include "X86GenSubtargetInfo.inc"
38
39// Temporary option to control early if-conversion for x86 while adding machine
40// models.
41static cl::opt<bool>
42X86EarlyIfConv("x86-early-ifcvt", cl::Hidden,
43               cl::desc("Enable early if-conversion on X86"));
44
45
46/// ClassifyBlockAddressReference - Classify a blockaddress reference for the
47/// current subtarget according to how we should reference it in a non-pcrel
48/// context.
49unsigned char X86Subtarget::ClassifyBlockAddressReference() const {
50  if (isPICStyleGOT())    // 32-bit ELF targets.
51    return X86II::MO_GOTOFF;
52
53  if (isPICStyleStubPIC())   // Darwin/32 in PIC mode.
54    return X86II::MO_PIC_BASE_OFFSET;
55
56  // Direct static reference to label.
57  return X86II::MO_NO_FLAG;
58}
59
60/// ClassifyGlobalReference - Classify a global variable reference for the
61/// current subtarget according to how we should reference it in a non-pcrel
62/// context.
63unsigned char X86Subtarget::
64ClassifyGlobalReference(const GlobalValue *GV, const TargetMachine &TM) const {
65  // DLLImport only exists on windows, it is implemented as a load from a
66  // DLLIMPORT stub.
67  if (GV->hasDLLImportStorageClass())
68    return X86II::MO_DLLIMPORT;
69
70  // Determine whether this is a reference to a definition or a declaration.
71  // Materializable GVs (in JIT lazy compilation mode) do not require an extra
72  // load from stub.
73  bool isDecl = GV->hasAvailableExternallyLinkage();
74  if (GV->isDeclaration() && !GV->isMaterializable())
75    isDecl = true;
76
77  // X86-64 in PIC mode.
78  if (isPICStyleRIPRel()) {
79    // Large model never uses stubs.
80    if (TM.getCodeModel() == CodeModel::Large)
81      return X86II::MO_NO_FLAG;
82
83    if (isTargetDarwin()) {
84      // If symbol visibility is hidden, the extra load is not needed if
85      // target is x86-64 or the symbol is definitely defined in the current
86      // translation unit.
87      if (GV->hasDefaultVisibility() &&
88          (isDecl || GV->isWeakForLinker()))
89        return X86II::MO_GOTPCREL;
90    } else if (!isTargetWin64()) {
91      assert(isTargetELF() && "Unknown rip-relative target");
92
93      // Extra load is needed for all externally visible.
94      if (!GV->hasLocalLinkage() && GV->hasDefaultVisibility())
95        return X86II::MO_GOTPCREL;
96    }
97
98    return X86II::MO_NO_FLAG;
99  }
100
101  if (isPICStyleGOT()) {   // 32-bit ELF targets.
102    // Extra load is needed for all externally visible.
103    if (GV->hasLocalLinkage() || GV->hasHiddenVisibility())
104      return X86II::MO_GOTOFF;
105    return X86II::MO_GOT;
106  }
107
108  if (isPICStyleStubPIC()) {  // Darwin/32 in PIC mode.
109    // Determine whether we have a stub reference and/or whether the reference
110    // is relative to the PIC base or not.
111
112    // If this is a strong reference to a definition, it is definitely not
113    // through a stub.
114    if (!isDecl && !GV->isWeakForLinker())
115      return X86II::MO_PIC_BASE_OFFSET;
116
117    // Unless we have a symbol with hidden visibility, we have to go through a
118    // normal $non_lazy_ptr stub because this symbol might be resolved late.
119    if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
120      return X86II::MO_DARWIN_NONLAZY_PIC_BASE;
121
122    // If symbol visibility is hidden, we have a stub for common symbol
123    // references and external declarations.
124    if (isDecl || GV->hasCommonLinkage()) {
125      // Hidden $non_lazy_ptr reference.
126      return X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE;
127    }
128
129    // Otherwise, no stub.
130    return X86II::MO_PIC_BASE_OFFSET;
131  }
132
133  if (isPICStyleStubNoDynamic()) {  // Darwin/32 in -mdynamic-no-pic mode.
134    // Determine whether we have a stub reference.
135
136    // If this is a strong reference to a definition, it is definitely not
137    // through a stub.
138    if (!isDecl && !GV->isWeakForLinker())
139      return X86II::MO_NO_FLAG;
140
141    // Unless we have a symbol with hidden visibility, we have to go through a
142    // normal $non_lazy_ptr stub because this symbol might be resolved late.
143    if (!GV->hasHiddenVisibility())  // Non-hidden $non_lazy_ptr reference.
144      return X86II::MO_DARWIN_NONLAZY;
145
146    // Otherwise, no stub.
147    return X86II::MO_NO_FLAG;
148  }
149
150  // Direct static reference to global.
151  return X86II::MO_NO_FLAG;
152}
153
154
155/// getBZeroEntry - This function returns the name of a function which has an
156/// interface like the non-standard bzero function, if such a function exists on
157/// the current subtarget and it is considered prefereable over memset with zero
158/// passed as the second argument. Otherwise it returns null.
159const char *X86Subtarget::getBZeroEntry() const {
160  // Darwin 10 has a __bzero entry point for this purpose.
161  if (getTargetTriple().isMacOSX() &&
162      !getTargetTriple().isMacOSXVersionLT(10, 6))
163    return "__bzero";
164
165  return nullptr;
166}
167
168bool X86Subtarget::hasSinCos() const {
169  return getTargetTriple().isMacOSX() &&
170    !getTargetTriple().isMacOSXVersionLT(10, 9) &&
171    is64Bit();
172}
173
174/// IsLegalToCallImmediateAddr - Return true if the subtarget allows calls
175/// to immediate address.
176bool X86Subtarget::IsLegalToCallImmediateAddr(const TargetMachine &TM) const {
177  // FIXME: I386 PE/COFF supports PC relative calls using IMAGE_REL_I386_REL32
178  // but WinCOFFObjectWriter::RecordRelocation cannot emit them.  Once it does,
179  // the following check for Win32 should be removed.
180  if (In64BitMode || isTargetWin32())
181    return false;
182  return isTargetELF() || TM.getRelocationModel() == Reloc::Static;
183}
184
185void X86Subtarget::resetSubtargetFeatures(const MachineFunction *MF) {
186  AttributeSet FnAttrs = MF->getFunction()->getAttributes();
187  Attribute CPUAttr =
188      FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-cpu");
189  Attribute FSAttr =
190      FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-features");
191  std::string CPU =
192      !CPUAttr.hasAttribute(Attribute::None) ? CPUAttr.getValueAsString() : "";
193  std::string FS =
194      !FSAttr.hasAttribute(Attribute::None) ? FSAttr.getValueAsString() : "";
195  if (!FS.empty()) {
196    initializeEnvironment();
197    resetSubtargetFeatures(CPU, FS);
198  }
199}
200
201void X86Subtarget::resetSubtargetFeatures(StringRef CPU, StringRef FS) {
202  std::string CPUName = CPU;
203  if (CPUName.empty())
204    CPUName = "generic";
205
206  // Make sure 64-bit features are available in 64-bit mode. (But make sure
207  // SSE2 can be turned off explicitly.)
208  std::string FullFS = FS;
209  if (In64BitMode) {
210    if (!FullFS.empty())
211      FullFS = "+64bit,+sse2," + FullFS;
212    else
213      FullFS = "+64bit,+sse2";
214  }
215
216  // If feature string is not empty, parse features string.
217  ParseSubtargetFeatures(CPUName, FullFS);
218
219  // Make sure the right MCSchedModel is used.
220  InitCPUSchedModel(CPUName);
221
222  if (X86ProcFamily == IntelAtom || X86ProcFamily == IntelSLM)
223    PostRAScheduler = true;
224
225  InstrItins = getInstrItineraryForCPU(CPUName);
226
227  // It's important to keep the MCSubtargetInfo feature bits in sync with
228  // target data structure which is shared with MC code emitter, etc.
229  if (In64BitMode)
230    ToggleFeature(X86::Mode64Bit);
231  else if (In32BitMode)
232    ToggleFeature(X86::Mode32Bit);
233  else if (In16BitMode)
234    ToggleFeature(X86::Mode16Bit);
235  else
236    llvm_unreachable("Not 16-bit, 32-bit or 64-bit mode!");
237
238  DEBUG(dbgs() << "Subtarget features: SSELevel " << X86SSELevel
239               << ", 3DNowLevel " << X863DNowLevel
240               << ", 64bit " << HasX86_64 << "\n");
241  assert((!In64BitMode || HasX86_64) &&
242         "64-bit code requested on a subtarget that doesn't support it!");
243
244  // Stack alignment is 16 bytes on Darwin, Linux and Solaris (both
245  // 32 and 64 bit) and for all 64-bit targets.
246  if (StackAlignOverride)
247    stackAlignment = StackAlignOverride;
248  else if (isTargetDarwin() || isTargetLinux() || isTargetSolaris() ||
249           In64BitMode)
250    stackAlignment = 16;
251}
252
253void X86Subtarget::initializeEnvironment() {
254  X86SSELevel = NoMMXSSE;
255  X863DNowLevel = NoThreeDNow;
256  HasCMov = false;
257  HasX86_64 = false;
258  HasPOPCNT = false;
259  HasSSE4A = false;
260  HasAES = false;
261  HasPCLMUL = false;
262  HasFMA = false;
263  HasFMA4 = false;
264  HasXOP = false;
265  HasTBM = false;
266  HasMOVBE = false;
267  HasRDRAND = false;
268  HasF16C = false;
269  HasFSGSBase = false;
270  HasLZCNT = false;
271  HasBMI = false;
272  HasBMI2 = false;
273  HasRTM = false;
274  HasHLE = false;
275  HasERI = false;
276  HasCDI = false;
277  HasPFI = false;
278  HasADX = false;
279  HasSHA = false;
280  HasPRFCHW = false;
281  HasRDSEED = false;
282  IsBTMemSlow = false;
283  IsSHLDSlow = false;
284  IsUAMemFast = false;
285  HasVectorUAMem = false;
286  HasCmpxchg16b = false;
287  UseLeaForSP = false;
288  HasSlowDivide = false;
289  PostRAScheduler = false;
290  PadShortFunctions = false;
291  CallRegIndirect = false;
292  LEAUsesAG = false;
293  SlowLEA = false;
294  stackAlignment = 4;
295  // FIXME: this is a known good value for Yonah. How about others?
296  MaxInlineSizeThreshold = 128;
297}
298
299X86Subtarget::X86Subtarget(const std::string &TT, const std::string &CPU,
300                           const std::string &FS, unsigned StackAlignOverride)
301    : X86GenSubtargetInfo(TT, CPU, FS), X86ProcFamily(Others),
302      PICStyle(PICStyles::None), TargetTriple(TT),
303      StackAlignOverride(StackAlignOverride),
304      In64BitMode(TargetTriple.getArch() == Triple::x86_64),
305      In32BitMode(TargetTriple.getArch() == Triple::x86 &&
306                  TargetTriple.getEnvironment() != Triple::CODE16),
307      In16BitMode(TargetTriple.getArch() == Triple::x86 &&
308                  TargetTriple.getEnvironment() == Triple::CODE16) {
309  initializeEnvironment();
310  resetSubtargetFeatures(CPU, FS);
311}
312
313bool
314X86Subtarget::enablePostRAScheduler(CodeGenOpt::Level OptLevel,
315                                    TargetSubtargetInfo::AntiDepBreakMode &Mode,
316                                    RegClassVector &CriticalPathRCs) const {
317  Mode = TargetSubtargetInfo::ANTIDEP_CRITICAL;
318  CriticalPathRCs.clear();
319  return PostRAScheduler && OptLevel >= CodeGenOpt::Default;
320}
321
322bool
323X86Subtarget::enableEarlyIfConversion() const {
324  return hasCMov() && X86EarlyIfConv;
325}
326