1 //===-- X86Subtarget.h - Define Subtarget for the X86 ----------*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the X86 specific subclass of TargetSubtargetInfo.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_TARGET_X86_X86SUBTARGET_H
15 #define LLVM_LIB_TARGET_X86_X86SUBTARGET_H
17 #include "X86FrameLowering.h"
18 #include "X86ISelLowering.h"
19 #include "X86InstrInfo.h"
20 #include "X86SelectionDAGInfo.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Triple.h"
23 #include "llvm/CodeGen/GlobalISel/GISelAccessor.h"
24 #include "llvm/IR/CallingConv.h"
25 #include "llvm/MC/MCInstrItineraries.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Target/TargetSubtargetInfo.h"
30 #define GET_SUBTARGETINFO_HEADER
31 #include "X86GenSubtargetInfo.inc"
37 /// The X86 backend supports a number of different styles of PIC.
42 StubPIC, // Used on i386-darwin in pic mode.
43 GOT, // Used on 32 bit elf on when in pic mode.
44 RIPRel, // Used on X86-64 when in pic mode.
45 None // Set when not in pic mode.
48 } // end namespace PICStyles
50 class X86Subtarget final : public X86GenSubtargetInfo {
53 NoSSE, SSE1, SSE2, SSE3, SSSE3, SSE41, SSE42, AVX, AVX2, AVX512F
57 NoThreeDNow, MMX, ThreeDNow, ThreeDNowA
60 enum X86ProcFamilyEnum {
61 Others, IntelAtom, IntelSLM, IntelGLM
64 /// X86 processor family: Intel Atom, and others
65 X86ProcFamilyEnum X86ProcFamily;
67 /// Which PIC style to use
68 PICStyles::Style PICStyle;
70 const TargetMachine &TM;
72 /// SSE1, SSE2, SSE3, SSSE3, SSE41, SSE42, or none supported.
73 X86SSEEnum X86SSELevel;
75 /// MMX, 3DNow, 3DNow Athlon, or none supported.
76 X863DNowEnum X863DNowLevel;
78 /// True if the processor supports X87 instructions.
81 /// True if this processor has conditional move instructions
82 /// (generally pentium pro+).
85 /// True if the processor supports X86-64 instructions.
88 /// True if the processor supports POPCNT.
91 /// True if the processor supports SSE4A instructions.
94 /// Target has AES instructions
97 /// Target has FXSAVE/FXRESTOR instructions
100 /// Target has XSAVE instructions
103 /// Target has XSAVEOPT instructions
106 /// Target has XSAVEC instructions
109 /// Target has XSAVES instructions
112 /// Target has carry-less multiplication
115 /// Target has 3-operand fused multiply-add
118 /// Target has 4-operand fused multiply-add
121 /// Target has XOP instructions
124 /// Target has TBM instructions.
127 /// Target has LWP instructions
130 /// True if the processor has the MOVBE instruction.
133 /// True if the processor has the RDRAND instruction.
136 /// Processor has 16-bit floating point conversion instructions.
139 /// Processor has FS/GS base insturctions.
142 /// Processor has LZCNT instruction.
145 /// Processor has BMI1 instructions.
148 /// Processor has BMI2 instructions.
151 /// Processor has VBMI instructions.
154 /// Processor has Integer Fused Multiply Add
157 /// Processor has RTM instructions.
160 /// Processor has ADX instructions.
163 /// Processor has SHA instructions.
166 /// Processor has PRFCHW instructions.
169 /// Processor has RDSEED instructions.
172 /// Processor has LAHF/SAHF instructions.
175 /// Processor has MONITORX/MWAITX instructions.
178 /// Processor has Cache Line Zero instruction
181 /// Processor has Prefetch with intent to Write instruction
182 bool HasPFPREFETCHWT1;
184 /// True if BT (bit test) of memory instructions are slow.
187 /// True if SHLD instructions are slow.
190 /// True if the PMULLD instruction is slow compared to PMULLW/PMULHW and
194 /// True if unaligned memory accesses of 16-bytes are slow.
197 /// True if unaligned memory accesses of 32-bytes are slow.
200 /// True if SSE operations can have unaligned memory operands.
201 /// This may require setting a configuration bit in the processor.
202 bool HasSSEUnalignedMem;
204 /// True if this processor has the CMPXCHG16B instruction;
205 /// this is true for most x86-64 chips, but not the first AMD chips.
208 /// True if the LEA instruction should be used for adjusting
209 /// the stack pointer. This is an optimization for Intel Atom processors.
212 /// True if there is no performance penalty to writing only the lower parts
213 /// of a YMM or ZMM register without clearing the upper part.
214 bool HasFastPartialYMMorZMMWrite;
216 /// True if hardware SQRTSS instruction is at least as fast (latency) as
217 /// RSQRTSS followed by a Newton-Raphson iteration.
218 bool HasFastScalarFSQRT;
220 /// True if hardware SQRTPS/VSQRTPS instructions are at least as fast
221 /// (throughput) as RSQRTPS/VRSQRTPS followed by a Newton-Raphson iteration.
222 bool HasFastVectorFSQRT;
224 /// True if 8-bit divisions are significantly faster than
225 /// 32-bit divisions and should be used when possible.
226 bool HasSlowDivide32;
228 /// True if 32-bit divides are significantly faster than
229 /// 64-bit divisions and should be used when possible.
230 bool HasSlowDivide64;
232 /// True if LZCNT instruction is fast.
235 /// True if SHLD based rotate is fast.
236 bool HasFastSHLDRotate;
238 /// True if the processor has enhanced REP MOVSB/STOSB.
241 /// True if the short functions should be padded to prevent
242 /// a stall when returning too early.
243 bool PadShortFunctions;
245 /// True if the Calls with memory reference should be converted
246 /// to a register-based indirect call.
247 bool CallRegIndirect;
249 /// True if the LEA instruction inputs have to be ready at address generation
253 /// True if the LEA instruction with certain arguments is slow
256 /// True if the LEA instruction has all three source operands: base, index,
257 /// and offset or if the LEA instruction uses base and index registers where
258 /// the base is EBP, RBP,or R13
261 /// True if INC and DEC instructions are slow when writing to flags
264 /// Processor has AVX-512 PreFetch Instructions
267 /// Processor has AVX-512 Exponential and Reciprocal Instructions
270 /// Processor has AVX-512 Conflict Detection Instructions
273 /// Processor has AVX-512 population count Instructions
276 /// Processor has AVX-512 Doubleword and Quadword instructions
279 /// Processor has AVX-512 Byte and Word instructions
282 /// Processor has AVX-512 Vector Length eXtenstions
285 /// Processor has PKU extenstions
288 /// Processor supports MPX - Memory Protection Extensions
291 /// Processor has Software Guard Extensions
294 /// Processor supports Flush Cache Line instruction
297 /// Processor supports Cache Line Write Back instruction
300 /// Use software floating point for code generation.
303 /// The minimum alignment known to hold of the stack frame on
304 /// entry to the function and which must be maintained by every function.
305 unsigned stackAlignment;
307 /// Max. memset / memcpy size that is turned into rep/movs, rep/stos ops.
309 unsigned MaxInlineSizeThreshold;
311 /// What processor and OS we're targeting.
314 /// Instruction itineraries for scheduling
315 InstrItineraryData InstrItins;
317 /// Gather the accessor points to GlobalISel-related APIs.
318 /// This is used to avoid ifndefs spreading around while GISel is
319 /// an optional library.
320 std::unique_ptr<GISelAccessor> GISel;
323 /// Override the stack alignment.
324 unsigned StackAlignOverride;
326 /// True if compiling for 64-bit, false for 16-bit or 32-bit.
329 /// True if compiling for 32-bit, false for 16-bit or 64-bit.
332 /// True if compiling for 16-bit, false for 32-bit or 64-bit.
335 X86SelectionDAGInfo TSInfo;
336 // Ordering here is important. X86InstrInfo initializes X86RegisterInfo which
337 // X86TargetLowering needs.
338 X86InstrInfo InstrInfo;
339 X86TargetLowering TLInfo;
340 X86FrameLowering FrameLowering;
343 /// This constructor initializes the data members to match that
344 /// of the specified triple.
346 X86Subtarget(const Triple &TT, StringRef CPU, StringRef FS,
347 const X86TargetMachine &TM, unsigned StackAlignOverride);
349 /// This object will take onwership of \p GISelAccessor.
350 void setGISelAccessor(GISelAccessor &GISel) { this->GISel.reset(&GISel); }
352 const X86TargetLowering *getTargetLowering() const override {
356 const X86InstrInfo *getInstrInfo() const override { return &InstrInfo; }
358 const X86FrameLowering *getFrameLowering() const override {
359 return &FrameLowering;
362 const X86SelectionDAGInfo *getSelectionDAGInfo() const override {
366 const X86RegisterInfo *getRegisterInfo() const override {
367 return &getInstrInfo()->getRegisterInfo();
370 /// Returns the minimum alignment known to hold of the
371 /// stack frame on entry to the function and which must be maintained by every
372 /// function for this subtarget.
373 unsigned getStackAlignment() const { return stackAlignment; }
375 /// Returns the maximum memset / memcpy size
376 /// that still makes it profitable to inline the call.
377 unsigned getMaxInlineSizeThreshold() const { return MaxInlineSizeThreshold; }
379 /// ParseSubtargetFeatures - Parses features string setting specified
380 /// subtarget options. Definition of function is auto generated by tblgen.
381 void ParseSubtargetFeatures(StringRef CPU, StringRef FS);
383 /// Methods used by Global ISel
384 const CallLowering *getCallLowering() const override;
385 const InstructionSelector *getInstructionSelector() const override;
386 const LegalizerInfo *getLegalizerInfo() const override;
387 const RegisterBankInfo *getRegBankInfo() const override;
390 /// Initialize the full set of dependencies so we can use an initializer
391 /// list for X86Subtarget.
392 X86Subtarget &initializeSubtargetDependencies(StringRef CPU, StringRef FS);
393 void initializeEnvironment();
394 void initSubtargetFeatures(StringRef CPU, StringRef FS);
397 /// Is this x86_64? (disregarding specific ABI / programming model)
398 bool is64Bit() const {
402 bool is32Bit() const {
406 bool is16Bit() const {
410 /// Is this x86_64 with the ILP32 programming model (x32 ABI)?
411 bool isTarget64BitILP32() const {
412 return In64BitMode && (TargetTriple.getEnvironment() == Triple::GNUX32 ||
413 TargetTriple.isOSNaCl());
416 /// Is this x86_64 with the LP64 programming model (standard AMD64, no x32)?
417 bool isTarget64BitLP64() const {
418 return In64BitMode && (TargetTriple.getEnvironment() != Triple::GNUX32 &&
419 !TargetTriple.isOSNaCl());
422 PICStyles::Style getPICStyle() const { return PICStyle; }
423 void setPICStyle(PICStyles::Style Style) { PICStyle = Style; }
425 bool hasX87() const { return HasX87; }
426 bool hasCMov() const { return HasCMov; }
427 bool hasSSE1() const { return X86SSELevel >= SSE1; }
428 bool hasSSE2() const { return X86SSELevel >= SSE2; }
429 bool hasSSE3() const { return X86SSELevel >= SSE3; }
430 bool hasSSSE3() const { return X86SSELevel >= SSSE3; }
431 bool hasSSE41() const { return X86SSELevel >= SSE41; }
432 bool hasSSE42() const { return X86SSELevel >= SSE42; }
433 bool hasAVX() const { return X86SSELevel >= AVX; }
434 bool hasAVX2() const { return X86SSELevel >= AVX2; }
435 bool hasAVX512() const { return X86SSELevel >= AVX512F; }
436 bool hasFp256() const { return hasAVX(); }
437 bool hasInt256() const { return hasAVX2(); }
438 bool hasSSE4A() const { return HasSSE4A; }
439 bool hasMMX() const { return X863DNowLevel >= MMX; }
440 bool has3DNow() const { return X863DNowLevel >= ThreeDNow; }
441 bool has3DNowA() const { return X863DNowLevel >= ThreeDNowA; }
442 bool hasPOPCNT() const { return HasPOPCNT; }
443 bool hasAES() const { return HasAES; }
444 bool hasFXSR() const { return HasFXSR; }
445 bool hasXSAVE() const { return HasXSAVE; }
446 bool hasXSAVEOPT() const { return HasXSAVEOPT; }
447 bool hasXSAVEC() const { return HasXSAVEC; }
448 bool hasXSAVES() const { return HasXSAVES; }
449 bool hasPCLMUL() const { return HasPCLMUL; }
450 // Prefer FMA4 to FMA - its better for commutation/memory folding and
451 // has equal or better performance on all supported targets.
452 bool hasFMA() const { return (HasFMA || hasAVX512()) && !HasFMA4; }
453 bool hasFMA4() const { return HasFMA4; }
454 bool hasAnyFMA() const { return hasFMA() || hasFMA4(); }
455 bool hasXOP() const { return HasXOP; }
456 bool hasTBM() const { return HasTBM; }
457 bool hasLWP() const { return HasLWP; }
458 bool hasMOVBE() const { return HasMOVBE; }
459 bool hasRDRAND() const { return HasRDRAND; }
460 bool hasF16C() const { return HasF16C; }
461 bool hasFSGSBase() const { return HasFSGSBase; }
462 bool hasLZCNT() const { return HasLZCNT; }
463 bool hasBMI() const { return HasBMI; }
464 bool hasBMI2() const { return HasBMI2; }
465 bool hasVBMI() const { return HasVBMI; }
466 bool hasIFMA() const { return HasIFMA; }
467 bool hasRTM() const { return HasRTM; }
468 bool hasADX() const { return HasADX; }
469 bool hasSHA() const { return HasSHA; }
470 bool hasPRFCHW() const { return HasPRFCHW; }
471 bool hasRDSEED() const { return HasRDSEED; }
472 bool hasLAHFSAHF() const { return HasLAHFSAHF; }
473 bool hasMWAITX() const { return HasMWAITX; }
474 bool hasCLZERO() const { return HasCLZERO; }
475 bool isBTMemSlow() const { return IsBTMemSlow; }
476 bool isSHLDSlow() const { return IsSHLDSlow; }
477 bool isPMULLDSlow() const { return IsPMULLDSlow; }
478 bool isUnalignedMem16Slow() const { return IsUAMem16Slow; }
479 bool isUnalignedMem32Slow() const { return IsUAMem32Slow; }
480 bool hasSSEUnalignedMem() const { return HasSSEUnalignedMem; }
481 bool hasCmpxchg16b() const { return HasCmpxchg16b; }
482 bool useLeaForSP() const { return UseLeaForSP; }
483 bool hasFastPartialYMMorZMMWrite() const {
484 return HasFastPartialYMMorZMMWrite;
486 bool hasFastScalarFSQRT() const { return HasFastScalarFSQRT; }
487 bool hasFastVectorFSQRT() const { return HasFastVectorFSQRT; }
488 bool hasFastLZCNT() const { return HasFastLZCNT; }
489 bool hasFastSHLDRotate() const { return HasFastSHLDRotate; }
490 bool hasERMSB() const { return HasERMSB; }
491 bool hasSlowDivide32() const { return HasSlowDivide32; }
492 bool hasSlowDivide64() const { return HasSlowDivide64; }
493 bool padShortFunctions() const { return PadShortFunctions; }
494 bool callRegIndirect() const { return CallRegIndirect; }
495 bool LEAusesAG() const { return LEAUsesAG; }
496 bool slowLEA() const { return SlowLEA; }
497 bool slow3OpsLEA() const { return Slow3OpsLEA; }
498 bool slowIncDec() const { return SlowIncDec; }
499 bool hasCDI() const { return HasCDI; }
500 bool hasVPOPCNTDQ() const { return HasVPOPCNTDQ; }
501 bool hasPFI() const { return HasPFI; }
502 bool hasERI() const { return HasERI; }
503 bool hasDQI() const { return HasDQI; }
504 bool hasBWI() const { return HasBWI; }
505 bool hasVLX() const { return HasVLX; }
506 bool hasPKU() const { return HasPKU; }
507 bool hasMPX() const { return HasMPX; }
508 bool hasCLFLUSHOPT() const { return HasCLFLUSHOPT; }
510 bool isXRaySupported() const override { return is64Bit(); }
512 bool isAtom() const { return X86ProcFamily == IntelAtom; }
513 bool isSLM() const { return X86ProcFamily == IntelSLM; }
514 bool useSoftFloat() const { return UseSoftFloat; }
516 /// Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
517 /// no-sse2). There isn't any reason to disable it if the target processor
519 bool hasMFence() const { return hasSSE2() || is64Bit(); }
521 const Triple &getTargetTriple() const { return TargetTriple; }
523 bool isTargetDarwin() const { return TargetTriple.isOSDarwin(); }
524 bool isTargetFreeBSD() const { return TargetTriple.isOSFreeBSD(); }
525 bool isTargetDragonFly() const { return TargetTriple.isOSDragonFly(); }
526 bool isTargetSolaris() const { return TargetTriple.isOSSolaris(); }
527 bool isTargetPS4() const { return TargetTriple.isPS4CPU(); }
529 bool isTargetELF() const { return TargetTriple.isOSBinFormatELF(); }
530 bool isTargetCOFF() const { return TargetTriple.isOSBinFormatCOFF(); }
531 bool isTargetMachO() const { return TargetTriple.isOSBinFormatMachO(); }
533 bool isTargetLinux() const { return TargetTriple.isOSLinux(); }
534 bool isTargetKFreeBSD() const { return TargetTriple.isOSKFreeBSD(); }
535 bool isTargetGlibc() const { return TargetTriple.isOSGlibc(); }
536 bool isTargetAndroid() const { return TargetTriple.isAndroid(); }
537 bool isTargetNaCl() const { return TargetTriple.isOSNaCl(); }
538 bool isTargetNaCl32() const { return isTargetNaCl() && !is64Bit(); }
539 bool isTargetNaCl64() const { return isTargetNaCl() && is64Bit(); }
540 bool isTargetMCU() const { return TargetTriple.isOSIAMCU(); }
541 bool isTargetFuchsia() const { return TargetTriple.isOSFuchsia(); }
543 bool isTargetWindowsMSVC() const {
544 return TargetTriple.isWindowsMSVCEnvironment();
547 bool isTargetKnownWindowsMSVC() const {
548 return TargetTriple.isKnownWindowsMSVCEnvironment();
551 bool isTargetWindowsCoreCLR() const {
552 return TargetTriple.isWindowsCoreCLREnvironment();
555 bool isTargetWindowsCygwin() const {
556 return TargetTriple.isWindowsCygwinEnvironment();
559 bool isTargetWindowsGNU() const {
560 return TargetTriple.isWindowsGNUEnvironment();
563 bool isTargetWindowsItanium() const {
564 return TargetTriple.isWindowsItaniumEnvironment();
567 bool isTargetCygMing() const { return TargetTriple.isOSCygMing(); }
569 bool isOSWindows() const { return TargetTriple.isOSWindows(); }
571 bool isTargetWin64() const {
572 return In64BitMode && TargetTriple.isOSWindows();
575 bool isTargetWin32() const {
576 return !In64BitMode && (isTargetCygMing() || isTargetKnownWindowsMSVC());
579 bool isPICStyleGOT() const { return PICStyle == PICStyles::GOT; }
580 bool isPICStyleRIPRel() const { return PICStyle == PICStyles::RIPRel; }
582 bool isPICStyleStubPIC() const {
583 return PICStyle == PICStyles::StubPIC;
586 bool isPositionIndependent() const { return TM.isPositionIndependent(); }
588 bool isCallingConvWin64(CallingConv::ID CC) const {
590 // On Win64, all these conventions just use the default convention.
592 case CallingConv::Fast:
593 case CallingConv::X86_FastCall:
594 case CallingConv::X86_StdCall:
595 case CallingConv::X86_ThisCall:
596 case CallingConv::X86_VectorCall:
597 case CallingConv::Intel_OCL_BI:
598 return isTargetWin64();
599 // This convention allows using the Win64 convention on other targets.
600 case CallingConv::Win64:
602 // This convention allows using the SysV convention on Windows targets.
603 case CallingConv::X86_64_SysV:
605 // Otherwise, who knows what this is.
611 /// Classify a global variable reference for the current subtarget according
612 /// to how we should reference it in a non-pcrel context.
613 unsigned char classifyLocalReference(const GlobalValue *GV) const;
615 unsigned char classifyGlobalReference(const GlobalValue *GV,
616 const Module &M) const;
617 unsigned char classifyGlobalReference(const GlobalValue *GV) const;
619 /// Classify a global function reference for the current subtarget.
620 unsigned char classifyGlobalFunctionReference(const GlobalValue *GV,
621 const Module &M) const;
622 unsigned char classifyGlobalFunctionReference(const GlobalValue *GV) const;
624 /// Classify a blockaddress reference for the current subtarget according to
625 /// how we should reference it in a non-pcrel context.
626 unsigned char classifyBlockAddressReference() const;
628 /// Return true if the subtarget allows calls to immediate address.
629 bool isLegalToCallImmediateAddr() const;
631 /// This function returns the name of a function which has an interface
632 /// like the non-standard bzero function, if such a function exists on
633 /// the current subtarget and it is considered prefereable over
634 /// memset with zero passed as the second argument. Otherwise it
636 const char *getBZeroEntry() const;
638 /// This function returns true if the target has sincos() routine in its
639 /// compiler runtime or math libraries.
640 bool hasSinCos() const;
642 /// Enable the MachineScheduler pass for all X86 subtargets.
643 bool enableMachineScheduler() const override { return true; }
645 // TODO: Update the regression tests and return true.
646 bool supportPrintSchedInfo() const override { return false; }
648 bool enableEarlyIfConversion() const override;
650 /// Return the instruction itineraries based on the subtarget selection.
651 const InstrItineraryData *getInstrItineraryData() const override {
655 AntiDepBreakMode getAntiDepBreakMode() const override {
656 return TargetSubtargetInfo::ANTIDEP_CRITICAL;
660 } // end namespace llvm
662 #endif // LLVM_LIB_TARGET_X86_X86SUBTARGET_H