1//===-- X86Subtarget.h - Define Subtarget for the X86 ----------*- C++ -*--===//
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 declares the X86 specific subclass of TargetSubtargetInfo.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_TARGET_X86_X86SUBTARGET_H
14#define LLVM_LIB_TARGET_X86_X86SUBTARGET_H
15
16#include "X86FrameLowering.h"
17#include "X86ISelLowering.h"
18#include "X86InstrInfo.h"
19#include "X86SelectionDAGInfo.h"
20#include "llvm/CodeGen/TargetSubtargetInfo.h"
21#include "llvm/IR/CallingConv.h"
22#include "llvm/TargetParser/Triple.h"
23#include <bitset>
24#include <climits>
25#include <cstdint>
26#include <memory>
27#include <optional>
28
29#define OPTIONS_STRUCT_DECL
30#include "X86Options.inc"
31
32#define GET_SUBTARGETINFO_HEADER
33#include "X86GenSubtargetInfo.inc"
34
35namespace llvm {
36
37class CallLowering;
38class GlobalValue;
39class InstructionSelector;
40class LegalizerInfo;
41class RegisterBankInfo;
42class StringRef;
43class TargetMachine;
44
45/// The X86 backend supports a number of different styles of PIC.
46///
47namespace PICStyles {
48
49enum class Style {
50 StubPIC, // Used on i386-darwin in pic mode.
51 GOT, // Used on 32 bit elf on when in pic mode.
52 RIPRel, // Used on X86-64 when in pic mode.
53 None // Set when not in pic mode.
54};
55
56} // end namespace PICStyles
57
58class X86Subtarget final : public X86GenSubtargetInfo {
59 const X86Options &CLOpts;
60
61 enum X86SSEEnum {
62 NoSSE, SSE1, SSE2, SSE3, SSSE3, SSE41, SSE42, AVX, AVX2, AVX512
63 };
64
65 /// Which PIC style to use
66 PICStyles::Style PICStyle;
67
68 const TargetMachine &TM;
69
70 /// SSE1, SSE2, SSE3, SSSE3, SSE41, SSE42, or none supported.
71 X86SSEEnum X86SSELevel = NoSSE;
72
73#define GET_SUBTARGETINFO_MACRO(ATTRIBUTE, DEFAULT, GETTER) \
74 bool ATTRIBUTE = DEFAULT;
75#include "X86GenSubtargetInfo.inc"
76 /// ReservedRReg R#i is not available as a general purpose register.
77 std::bitset<X86::NUM_TARGET_REGS> ReservedRReg;
78
79 /// The minimum alignment known to hold of the stack frame on
80 /// entry to the function and which must be maintained by every function.
81 Align stackAlignment = Align(4);
82
83 Align TileConfigAlignment = Align(4);
84
85 /// Max. memset / memcpy size that is turned into rep/movs, rep/stos ops.
86 ///
87 // FIXME: this is a known good value for Yonah. How about others?
88 unsigned MaxInlineSizeThreshold = 128;
89
90 /// What processor and OS we're targeting.
91 Triple TargetTriple;
92
93 /// GlobalISel related APIs.
94 std::unique_ptr<CallLowering> CallLoweringInfo;
95 std::unique_ptr<LegalizerInfo> Legalizer;
96 std::unique_ptr<RegisterBankInfo> RegBankInfo;
97 std::unique_ptr<InstructionSelector> InstSelector;
98
99 /// Override the stack alignment.
100 MaybeAlign StackAlignOverride;
101
102 /// Preferred vector width from function attribute.
103 unsigned PreferVectorWidthOverride;
104
105 /// Resolved preferred vector width from function attribute and subtarget
106 /// features.
107 unsigned PreferVectorWidth = UINT32_MAX;
108
109 /// Required vector width from function attribute.
110 unsigned RequiredVectorWidth;
111
112 bool HasUserReservedRegisters;
113
114 X86SelectionDAGInfo TSInfo;
115 // Ordering here is important. X86InstrInfo initializes X86RegisterInfo which
116 // X86TargetLowering needs.
117 X86InstrInfo InstrInfo;
118 X86TargetLowering TLInfo;
119 X86FrameLowering FrameLowering;
120
121public:
122 /// This constructor initializes the data members to match that
123 /// of the specified triple.
124 ///
125 X86Subtarget(const Triple &TT, StringRef CPU, StringRef TuneCPU, StringRef FS,
126 const X86TargetMachine &TM, MaybeAlign StackAlignOverride,
127 unsigned PreferVectorWidthOverride,
128 unsigned RequiredVectorWidth);
129 ~X86Subtarget() override;
130
131 const X86Options &getCLOpts() const { return CLOpts; }
132
133 const X86TargetLowering *getTargetLowering() const override {
134 return &TLInfo;
135 }
136
137 const X86InstrInfo *getInstrInfo() const override { return &InstrInfo; }
138
139 const X86FrameLowering *getFrameLowering() const override {
140 return &FrameLowering;
141 }
142
143 const X86SelectionDAGInfo *getSelectionDAGInfo() const override {
144 return &TSInfo;
145 }
146
147 const X86RegisterInfo *getRegisterInfo() const override {
148 return &getInstrInfo()->getRegisterInfo();
149 }
150
151 unsigned getTileConfigSize() const { return 64; }
152 Align getTileConfigAlignment() const { return TileConfigAlignment; }
153
154 /// Returns the minimum alignment known to hold of the
155 /// stack frame on entry to the function and which must be maintained by every
156 /// function for this subtarget.
157 Align getStackAlignment() const { return stackAlignment; }
158
159 /// Returns the maximum memset / memcpy size
160 /// that still makes it profitable to inline the call.
161 unsigned getMaxInlineSizeThreshold() const { return MaxInlineSizeThreshold; }
162
163 /// ParseSubtargetFeatures - Parses features string setting specified
164 /// subtarget options. Definition of function is auto generated by tblgen.
165 void ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS);
166
167 /// Methods used by Global ISel
168 const CallLowering *getCallLowering() const override;
169 InstructionSelector *getInstructionSelector() const override;
170 const LegalizerInfo *getLegalizerInfo() const override;
171 const RegisterBankInfo *getRegBankInfo() const override;
172
173 bool isRegisterReservedByUser(Register i) const override {
174 return ReservedRReg[i.id()];
175 }
176 bool hasUserReservedRegisters() const { return HasUserReservedRegisters; }
177
178private:
179 /// Initialize the full set of dependencies so we can use an initializer
180 /// list for X86Subtarget.
181 X86Subtarget &initializeSubtargetDependencies(StringRef CPU,
182 StringRef TuneCPU,
183 StringRef FS);
184 void initSubtargetFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS);
185
186public:
187
188#define GET_SUBTARGETINFO_MACRO(ATTRIBUTE, DEFAULT, GETTER) \
189 bool GETTER() const { return ATTRIBUTE; }
190#include "X86GenSubtargetInfo.inc"
191
192 /// Is this x86_64 with the ILP32 programming model (x32 ABI)?
193 bool isTarget64BitILP32() const { return Is64Bit && IsX32; }
194
195 /// Is this x86_64 with the LP64 programming model (standard AMD64, no x32)?
196 bool isTarget64BitLP64() const { return Is64Bit && !IsX32; }
197
198 PICStyles::Style getPICStyle() const { return PICStyle; }
199 void setPICStyle(PICStyles::Style Style) { PICStyle = Style; }
200
201 bool canUseCMPXCHG8B() const { return hasCX8(); }
202 bool canUseCMPXCHG16B() const {
203 // CX16 is just the CPUID bit, instruction requires 64-bit mode too.
204 return hasCX16() && is64Bit();
205 }
206 // SSE codegen depends on cmovs, and all SSE1+ processors support them.
207 // All 64-bit processors support cmov.
208 bool canUseCMOV() const { return hasCMOV() || hasSSE1() || is64Bit(); }
209 bool hasSSE1() const { return X86SSELevel >= SSE1; }
210 bool hasSSE2() const { return X86SSELevel >= SSE2; }
211 bool hasSSE3() const { return X86SSELevel >= SSE3; }
212 bool hasSSSE3() const { return X86SSELevel >= SSSE3; }
213 bool hasSSE41() const { return X86SSELevel >= SSE41; }
214 bool hasSSE42() const { return X86SSELevel >= SSE42; }
215 bool hasAVX() const { return X86SSELevel >= AVX; }
216 bool hasAVX2() const { return X86SSELevel >= AVX2; }
217 bool hasAVX512() const { return X86SSELevel >= AVX512; }
218 bool hasInt256() const { return hasAVX2(); }
219 bool hasAnyFMA() const { return hasFMA() || hasFMA4(); }
220 bool useAPXFastCC() const { return hasEGPR() && hasAVX10_1(); }
221 bool hasPrefetchW() const {
222 // The PREFETCHW instruction was added with 3DNow but later CPUs gave it
223 // its own CPUID bit as part of deprecating 3DNow.
224 return hasPRFCHW();
225 }
226 bool hasSSEPrefetch() const {
227 // We also implicitly enable these when we have a write prefix supporting
228 // cache level OR if we have prfchw.
229 return hasSSE1() || hasPRFCHW() || hasPREFETCHI();
230 }
231 bool canUseLAHFSAHF() const { return hasLAHFSAHF64() || !is64Bit(); }
232 // These are generic getters that OR together all of the thunk types
233 // supported by the subtarget. Therefore useIndirectThunk*() will return true
234 // if any respective thunk feature is enabled.
235 bool useIndirectThunkCalls() const {
236 return useRetpolineIndirectCalls() || useLVIControlFlowIntegrity();
237 }
238 bool useIndirectThunkBranches() const {
239 return useRetpolineIndirectBranches() || useLVIControlFlowIntegrity();
240 }
241
242 unsigned getPreferVectorWidth() const { return PreferVectorWidth; }
243 unsigned getRequiredVectorWidth() const { return RequiredVectorWidth; }
244
245 // Helper functions to determine when we should allow widening to 512-bit
246 // during codegen.
247 // TODO: Currently we're always allowing widening on CPUs without VLX,
248 // because for many cases we don't have a better option.
249 bool canExtendTo512DQ() const {
250 return hasAVX512() && (!hasVLX() || getPreferVectorWidth() >= 512);
251 }
252 bool canExtendTo512BW() const {
253 return hasBWI() && canExtendTo512DQ();
254 }
255
256 bool hasNoDomainDelay() const { return NoDomainDelay; }
257 bool hasNoDomainDelayMov() const {
258 return hasNoDomainDelay() || NoDomainDelayMov;
259 }
260 bool hasNoDomainDelayBlend() const {
261 return hasNoDomainDelay() || NoDomainDelayBlend;
262 }
263 bool hasNoDomainDelayShuffle() const {
264 return hasNoDomainDelay() || NoDomainDelayShuffle;
265 }
266
267 // If there are no 512-bit vectors and we prefer not to use 512-bit registers,
268 // disable them in the legalizer.
269 bool useAVX512Regs() const {
270 return hasAVX512() && (canExtendTo512DQ() || RequiredVectorWidth > 256);
271 }
272
273 bool useLight256BitInstructions() const {
274 return getPreferVectorWidth() >= 256 || AllowLight256Bit;
275 }
276
277 bool useBWIRegs() const {
278 return hasBWI() && useAVX512Regs();
279 }
280
281 // Returns true if the destination register of a BSF/BSR instruction is
282 // not touched if the source register is zero.
283 // NOTE: i32->i64 implicit zext isn't guaranteed by BSR/BSF pass through.
284 bool hasBitScanPassThrough() const { return is64Bit(); }
285
286 bool isXRaySupported() const override { return is64Bit(); }
287
288 /// Use clflush if we have SSE2 or we're on x86-64 (even if we asked for
289 /// no-sse2). There isn't any reason to disable it if the target processor
290 /// supports it.
291 bool hasCLFLUSH() const { return hasSSE2() || is64Bit(); }
292
293 /// Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
294 /// no-sse2). There isn't any reason to disable it if the target processor
295 /// supports it.
296 bool hasMFence() const { return hasSSE2() || is64Bit(); }
297
298 /// Avoid use of `mfence` for`fence seq_cst`, and instead use `lock or`.
299 bool avoidMFence() const { return is64Bit(); }
300
301 const Triple &getTargetTriple() const { return TargetTriple; }
302
303 bool isTargetDarwin() const { return TargetTriple.isOSDarwin(); }
304 bool isTargetFreeBSD() const { return TargetTriple.isOSFreeBSD(); }
305 bool isTargetDragonFly() const { return TargetTriple.isOSDragonFly(); }
306 bool isTargetSolaris() const { return TargetTriple.isOSSolaris(); }
307 bool isTargetPS() const { return TargetTriple.isPS(); }
308
309 bool isTargetELF() const { return TargetTriple.isOSBinFormatELF(); }
310 bool isTargetCOFF() const { return TargetTriple.isOSBinFormatCOFF(); }
311 bool isTargetMachO() const { return TargetTriple.isOSBinFormatMachO(); }
312
313 bool isTargetLinux() const { return TargetTriple.isOSLinux(); }
314 bool isTargetKFreeBSD() const { return TargetTriple.isOSKFreeBSD(); }
315 bool isTargetHurd() const { return TargetTriple.isOSHurd(); }
316 bool isTargetGlibc() const { return TargetTriple.isOSGlibc(); }
317 bool isTargetMusl() const { return TargetTriple.isMusl(); }
318 bool isTargetAndroid() const { return TargetTriple.isAndroid(); }
319 bool isTargetMCU() const { return TargetTriple.isOSIAMCU(); }
320 bool isTargetFuchsia() const { return TargetTriple.isOSFuchsia(); }
321
322 bool isLFI() const { return TargetTriple.isLFI(); }
323
324 bool isTargetWindowsMSVC() const {
325 return TargetTriple.isWindowsMSVCEnvironment();
326 }
327
328 bool isTargetWindowsCoreCLR() const {
329 return TargetTriple.isWindowsCoreCLREnvironment();
330 }
331
332 bool isTargetWindowsCygwin() const {
333 return TargetTriple.isWindowsCygwinEnvironment();
334 }
335
336 bool isTargetWindowsGNU() const {
337 return TargetTriple.isWindowsGNUEnvironment();
338 }
339
340 bool isTargetWindowsItanium() const {
341 return TargetTriple.isWindowsItaniumEnvironment();
342 }
343
344 bool isTargetCygMing() const { return TargetTriple.isOSCygMing(); }
345
346 bool isUEFI() const { return TargetTriple.isUEFI(); }
347
348 bool isOSWindows() const { return TargetTriple.isOSWindows(); }
349
350 bool isOSWindowsOrUEFI() const { return TargetTriple.isOSWindowsOrUEFI(); }
351
352 bool isTargetUEFI64() const { return Is64Bit && isUEFI(); }
353
354 bool isTargetWin64() const { return Is64Bit && isOSWindows(); }
355
356 bool isTargetWin32() const { return !Is64Bit && isOSWindows(); }
357
358 bool isPICStyleGOT() const { return PICStyle == PICStyles::Style::GOT; }
359 bool isPICStyleRIPRel() const { return PICStyle == PICStyles::Style::RIPRel; }
360
361 bool isPICStyleStubPIC() const {
362 return PICStyle == PICStyles::Style::StubPIC;
363 }
364
365 bool isPositionIndependent() const;
366
367 bool isCallingConvWin64(CallingConv::ID CC) const {
368 switch (CC) {
369 // On Win64, all these conventions just use the default convention.
370 case CallingConv::C:
371 case CallingConv::Fast:
372 case CallingConv::Tail:
373 return isTargetWin64() || isTargetUEFI64();
374 case CallingConv::Swift:
375 case CallingConv::SwiftTail:
376 case CallingConv::X86_FastCall:
377 case CallingConv::X86_StdCall:
378 case CallingConv::X86_ThisCall:
379 case CallingConv::X86_VectorCall:
380 case CallingConv::Intel_OCL_BI:
381 return isTargetWin64();
382 // This convention allows using the Win64 convention on other targets.
383 case CallingConv::Win64:
384 return true;
385 // This convention allows using the SysV convention on Windows targets.
386 case CallingConv::X86_64_SysV:
387 return false;
388 // Otherwise, who knows what this is.
389 default:
390 return false;
391 }
392 }
393
394 /// Classify a global variable reference for the current subtarget according
395 /// to how we should reference it in a non-pcrel context.
396 unsigned char classifyLocalReference(const GlobalValue *GV) const;
397
398 unsigned char classifyGlobalReference(const GlobalValue *GV,
399 const Module &M) const;
400 unsigned char classifyGlobalReference(const GlobalValue *GV) const;
401
402 /// Classify a global function reference for the current subtarget.
403 unsigned char classifyGlobalFunctionReference(const GlobalValue *GV,
404 const Module &M) const;
405 unsigned char
406 classifyGlobalFunctionReference(const GlobalValue *GV) const override;
407
408 /// Classify a blockaddress reference for the current subtarget according to
409 /// how we should reference it in a non-pcrel context.
410 unsigned char classifyBlockAddressReference() const;
411
412 /// Return true if the subtarget allows calls to immediate address.
413 bool isLegalToCallImmediateAddr() const;
414
415 /// Return whether FrameLowering should always set the "extended frame
416 /// present" bit in FP, or set it based on a symbol in the runtime.
417 bool swiftAsyncContextIsDynamicallySet() const {
418 // Older OS versions (particularly system unwinders) are confused by the
419 // Swift extended frame, so when building code that might be run on them we
420 // must dynamically query the concurrency library to determine whether
421 // extended frames should be flagged as present.
422 const Triple &TT = getTargetTriple();
423
424 unsigned Major = TT.getOSVersion().getMajor();
425 switch(TT.getOS()) {
426 default:
427 return false;
428 case Triple::IOS:
429 case Triple::TvOS:
430 return Major < 15;
431 case Triple::WatchOS:
432 return Major < 8;
433 case Triple::MacOSX:
434 case Triple::Darwin:
435 return Major < 12;
436 }
437 }
438
439 /// If we are using indirect thunks, we need to expand indirectbr to avoid it
440 /// lowering to an actual indirect jump.
441 bool enableIndirectBrExpand() const override {
442 return useIndirectThunkBranches();
443 }
444
445 /// Enable the MachineScheduler pass for all X86 subtargets.
446 bool enableMachineScheduler() const override { return true; }
447
448 bool enableEarlyIfConversion() const override;
449
450 void getPostRAMutations(std::vector<std::unique_ptr<ScheduleDAGMutation>>
451 &Mutations) const override;
452
453 AntiDepBreakMode getAntiDepBreakMode() const override {
454 return TargetSubtargetInfo::ANTIDEP_CRITICAL;
455 }
456};
457
458} // end namespace llvm
459
460#endif // LLVM_LIB_TARGET_X86_X86SUBTARGET_H
461