LLVM 24.0.0git
X86RegisterInfo.cpp
Go to the documentation of this file.
1//===-- X86RegisterInfo.cpp - X86 Register 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 contains the X86 implementation of the TargetRegisterInfo class.
10// This file is responsible for the frame pointer elimination optimization
11// on X86.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86RegisterInfo.h"
16#include "X86FrameLowering.h"
18#include "X86Subtarget.h"
19#include "llvm/ADT/BitVector.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/SmallSet.h"
30#include "llvm/IR/Function.h"
31#include "llvm/IR/Type.h"
32#include "llvm/MC/MCContext.h"
36
37using namespace llvm;
38
39#define GET_REGINFO_TARGET_DESC
40#include "X86GenRegisterInfo.inc"
41
42static cl::opt<bool>
43EnableBasePointer("x86-use-base-pointer", cl::Hidden, cl::init(true),
44 cl::desc("Enable use of a base pointer for complex stack frames"));
45
46static cl::opt<bool>
47 DisableRegAllocNDDHints("x86-disable-regalloc-hints-for-ndd", cl::Hidden,
48 cl::init(false),
49 cl::desc("Disable two address hints for register "
50 "allocation"));
51
53 "x86-setjmp-csr-warning-threshold", cl::Hidden, cl::init(50),
54 cl::desc("Basic block count threshold for emitting a warning about "
55 "callee-saved registers reserved due to setjmp"));
56
58
60 : X86GenRegisterInfo((TT.isX86_64() ? X86::RIP : X86::EIP),
61 X86_MC::getDwarfRegFlavour(TT, false),
62 X86_MC::getDwarfRegFlavour(TT, true),
63 (TT.isX86_64() ? X86::RIP : X86::EIP)) {
65
66 // Cache some information.
67 Is64Bit = TT.isX86_64();
68 IsTarget64BitLP64 = Is64Bit && !TT.isX32();
69 IsWin64 = Is64Bit && TT.isOSWindows();
70 IsUEFI64 = Is64Bit && TT.isUEFI();
71
72 // Use a callee-saved register as the base pointer. These registers must
73 // not conflict with any ABI requirements. For example, in 32-bit mode PIC
74 // requires GOT in the EBX register before function calls via PLT GOT pointer.
75 if (Is64Bit) {
76 SlotSize = 8;
77 // This matches the simplified 32-bit pointer code in the data layout
78 // computation.
79 // FIXME: Should use the data layout?
80 bool Use64BitReg = !TT.isX32();
81 StackPtr = Use64BitReg ? X86::RSP : X86::ESP;
82 FramePtr = Use64BitReg ? X86::RBP : X86::EBP;
83 BasePtr = Use64BitReg ? X86::RBX : X86::EBX;
84 } else {
85 SlotSize = 4;
86 StackPtr = X86::ESP;
87 FramePtr = X86::EBP;
88 BasePtr = X86::ESI;
89 }
90}
91
94 unsigned Idx) const {
95 // The sub_8bit sub-register index is more constrained in 32-bit mode.
96 // It behaves just like the sub_8bit_hi index.
97 if (!Is64Bit && Idx == X86::sub_8bit)
98 Idx = X86::sub_8bit_hi;
99
100 // Forward to TableGen's default version.
101 return X86GenRegisterInfo::getSubClassWithSubReg(RC, Idx);
102}
103
106 const TargetRegisterClass *B,
107 unsigned SubIdx) const {
108 // The sub_8bit sub-register index is more constrained in 32-bit mode.
109 if (!Is64Bit && SubIdx == X86::sub_8bit) {
110 A = X86GenRegisterInfo::getSubClassWithSubReg(A, X86::sub_8bit_hi);
111 if (!A)
112 return nullptr;
113 }
114 return X86GenRegisterInfo::getMatchingSuperRegClass(A, B, SubIdx);
115}
116
119 const MachineFunction &MF) const {
120 // Don't allow super-classes of GR8_NOREX. This class is only used after
121 // extracting sub_8bit_hi sub-registers. The H sub-registers cannot be copied
122 // to the full GR8 register class in 64-bit mode, so we cannot allow the
123 // reigster class inflation.
124 //
125 // The GR8_NOREX class is always used in a way that won't be constrained to a
126 // sub-class, so sub-classes like GR8_ABCD_L are allowed to expand to the
127 // full GR8 class.
128 if (RC == &X86::GR8_NOREXRegClass)
129 return RC;
130
131 // Keep using non-rex2 register class when APX feature (EGPR/NDD/NF) is not
132 // enabled for relocation.
134 return RC;
135
136 const X86Subtarget &Subtarget = MF.getSubtarget<X86Subtarget>();
137
138 const TargetRegisterClass *Super = RC;
139 auto I = RC->superclasses().begin();
140 auto E = RC->superclasses().end();
141 do {
142 switch (Super->getID()) {
143 case X86::FR32RegClassID:
144 case X86::FR64RegClassID:
145 // If AVX-512 isn't supported we should only inflate to these classes.
146 if (!Subtarget.hasAVX512() &&
147 getRegSizeInBits(*Super) == getRegSizeInBits(*RC))
148 return Super;
149 break;
150 case X86::VR128RegClassID:
151 case X86::VR256RegClassID:
152 // If VLX isn't supported we should only inflate to these classes.
153 if (!Subtarget.hasVLX() &&
154 getRegSizeInBits(*Super) == getRegSizeInBits(*RC))
155 return Super;
156 break;
157 case X86::VR128XRegClassID:
158 case X86::VR256XRegClassID:
159 // If VLX isn't support we shouldn't inflate to these classes.
160 if (Subtarget.hasVLX() &&
161 getRegSizeInBits(*Super) == getRegSizeInBits(*RC))
162 return Super;
163 break;
164 case X86::FR32XRegClassID:
165 case X86::FR64XRegClassID:
166 // If AVX-512 isn't support we shouldn't inflate to these classes.
167 if (Subtarget.hasAVX512() &&
168 getRegSizeInBits(*Super) == getRegSizeInBits(*RC))
169 return Super;
170 break;
171 case X86::GR8RegClassID:
172 case X86::GR16RegClassID:
173 case X86::GR32RegClassID:
174 case X86::GR64RegClassID:
175 case X86::GR8_NOREX2RegClassID:
176 case X86::GR16_NOREX2RegClassID:
177 case X86::GR32_NOREX2RegClassID:
178 case X86::GR64_NOREX2RegClassID:
179 case X86::RFP32RegClassID:
180 case X86::RFP64RegClassID:
181 case X86::RFP80RegClassID:
182 case X86::VR512_0_15RegClassID:
183 case X86::VR512RegClassID:
184 // Don't return a super-class that would shrink the spill size.
185 // That can happen with the vector and float classes.
186 if (getRegSizeInBits(*Super) == getRegSizeInBits(*RC))
187 return Super;
188 }
189 if (I != E) {
190 Super = getRegClass(*I);
191 ++I;
192 } else {
193 Super = nullptr;
194 }
195 } while (Super);
196 return RC;
197}
198
201 assert(Kind == 0 && "this should only be used for default cases");
202 if (IsTarget64BitLP64)
203 return &X86::GR64RegClass;
204 // If the target is 64bit but we have been told to use 32bit addresses,
205 // we can still use 64-bit register as long as we know the high bits
206 // are zeros.
207 // Reflect that in the returned register class.
208 return Is64Bit ? &X86::LOW32_ADDR_ACCESSRegClass : &X86::GR32RegClass;
209}
210
213 if (RC == &X86::CCRRegClass) {
214 if (Is64Bit)
215 return &X86::GR64RegClass;
216 else
217 return &X86::GR32RegClass;
218 }
219 return RC;
220}
221
222unsigned
224 MachineFunction &MF) const {
225 const X86FrameLowering *TFI = getFrameLowering(MF);
226
227 unsigned FPDiff = TFI->hasFP(MF) ? 1 : 0;
228 switch (RC->getID()) {
229 default:
230 return 0;
231 case X86::GR32RegClassID:
232 return 4 - FPDiff;
233 case X86::GR64RegClassID:
234 return 12 - FPDiff;
235 case X86::VR128RegClassID:
236 return Is64Bit ? 10 : 4;
237 case X86::VR64RegClassID:
238 return 4;
239 }
240}
241
242const MCPhysReg *
244 assert(MF && "MachineFunction required");
245
246 const X86Subtarget &Subtarget = MF->getSubtarget<X86Subtarget>();
247 const Function &F = MF->getFunction();
248 bool HasSSE = Subtarget.hasSSE1();
249 bool HasAVX = Subtarget.hasAVX();
250 bool HasAVX512 = Subtarget.hasAVX512();
251 bool HasEGPR = Subtarget.hasEGPR();
252 bool CallsEHReturn = MF->callsEHReturn();
253
254 CallingConv::ID CC = F.getCallingConv();
255
256 // If attribute NoCallerSavedRegisters exists then we set X86_INTR calling
257 // convention because it has the CSR list.
258 if (MF->getFunction().hasFnAttribute("no_caller_saved_registers"))
260
261 // If atribute specified, override the CSRs normally specified by the
262 // calling convention and use the empty set instead.
263 if (MF->getFunction().hasFnAttribute("no_callee_saved_registers"))
264 return CSR_NoRegs_SaveList;
265
266 switch (CC) {
267 case CallingConv::GHC:
269 return CSR_NoRegs_SaveList;
271 if (HasAVX)
272 return CSR_64_AllRegs_AVX_SaveList;
273 return CSR_64_AllRegs_SaveList;
275 if (IsWin64)
276 return HasEGPR ? CSR_Win64_APX_RT_MostRegs_SaveList
277 : CSR_Win64_RT_MostRegs_SaveList;
278 return CSR_64_RT_MostRegs_SaveList;
280 if (HasAVX)
281 return CSR_64_RT_AllRegs_AVX_SaveList;
282 return CSR_64_RT_AllRegs_SaveList;
284 return CSR_64_NoneRegs_SaveList;
286 if (Is64Bit)
287 return MF->getInfo<X86MachineFunctionInfo>()->isSplitCSR() ?
288 CSR_64_CXX_TLS_Darwin_PE_SaveList : CSR_64_TLS_Darwin_SaveList;
289 break;
291 if (HasAVX512 && IsWin64)
292 return HasEGPR ? CSR_Win64_APX_Intel_OCL_BI_AVX512_SaveList
293 : CSR_Win64_Intel_OCL_BI_AVX512_SaveList;
294 if (HasAVX512 && Is64Bit)
295 return CSR_64_Intel_OCL_BI_AVX512_SaveList;
296 if (HasAVX && IsWin64)
297 return HasEGPR ? CSR_Win64_APX_Intel_OCL_BI_AVX_SaveList
298 : CSR_Win64_Intel_OCL_BI_AVX_SaveList;
299 if (HasAVX && Is64Bit)
300 return CSR_64_Intel_OCL_BI_AVX_SaveList;
301 if (!HasAVX && !IsWin64 && Is64Bit)
302 return CSR_64_Intel_OCL_BI_SaveList;
303 break;
304 }
306 if (Is64Bit) {
307 if (IsWin64) {
308 if (HasSSE)
309 return HasEGPR ? CSR_Win64_APX_RegCall_SaveList
310 : CSR_Win64_RegCall_SaveList;
311 return CSR_Win64_RegCall_NoSSE_SaveList;
312 }
313 return HasSSE ? CSR_SysV64_RegCall_SaveList
314 : CSR_SysV64_RegCall_NoSSE_SaveList;
315 }
316 return HasSSE ? CSR_32_RegCall_SaveList : CSR_32_RegCall_NoSSE_SaveList;
318 assert(!Is64Bit && "CFGuard check mechanism only used on 32-bit X86");
319 return HasSSE ? CSR_Win32_CFGuard_Check_SaveList
320 : CSR_Win32_CFGuard_Check_NoSSE_SaveList;
322 if (Is64Bit)
323 return CSR_64_MostRegs_SaveList;
324 break;
326 if (HasSSE)
327 return HasEGPR ? CSR_Win64_APX_SaveList : CSR_Win64_SaveList;
328 return CSR_Win64_NoSSE_SaveList;
330 if (!Is64Bit)
331 return CSR_32_SaveList;
332 if (IsWin64)
333 return HasEGPR ? CSR_Win64_APX_SwiftTail_SaveList
334 : CSR_Win64_SwiftTail_SaveList;
335 return CSR_64_SwiftTail_SaveList;
337 if (CallsEHReturn)
338 return CSR_64EHRet_SaveList;
339 return CSR_64_SaveList;
341 if (Is64Bit) {
342 if (HasAVX512)
343 return CSR_64_AllRegs_AVX512_SaveList;
344 if (HasAVX)
345 return CSR_64_AllRegs_AVX_SaveList;
346 if (HasSSE)
347 return CSR_64_AllRegs_SaveList;
348 return CSR_64_AllRegs_NoSSE_SaveList;
349 }
350 if (HasAVX512)
351 return CSR_32_AllRegs_AVX512_SaveList;
352 if (HasAVX)
353 return CSR_32_AllRegs_AVX_SaveList;
354 if (HasSSE)
355 return CSR_32_AllRegs_SSE_SaveList;
356 return CSR_32_AllRegs_SaveList;
357 default:
358 break;
359 }
360
361 if (Is64Bit) {
362 bool IsSwiftCC = Subtarget.getTargetLowering()->supportSwiftError() &&
363 F.getAttributes().hasAttrSomewhere(Attribute::SwiftError);
364 if (IsSwiftCC) {
365 if (IsWin64)
366 return HasEGPR ? CSR_Win64_APX_SwiftError_SaveList
367 : CSR_Win64_SwiftError_SaveList;
368 return CSR_64_SwiftError_SaveList;
369 }
370
371 if (IsWin64 || IsUEFI64) {
372 if (HasSSE)
373 return HasEGPR ? CSR_Win64_APX_SaveList : CSR_Win64_SaveList;
374 return CSR_Win64_NoSSE_SaveList;
375 }
376 if (CallsEHReturn)
377 return CSR_64EHRet_SaveList;
378 return CSR_64_SaveList;
379 }
380
381 return CallsEHReturn ? CSR_32EHRet_SaveList : CSR_32_SaveList;
382}
383
384const MCPhysReg *
386 return Is64Bit ? CSR_IPRA_64_SaveList : CSR_IPRA_32_SaveList;
387}
388
390 const MachineFunction *MF) const {
391 assert(MF && "Invalid MachineFunction pointer.");
394 return CSR_64_CXX_TLS_Darwin_ViaCopy_SaveList;
395 return nullptr;
396}
397
398const uint32_t *
400 CallingConv::ID CC) const {
401 const X86Subtarget &Subtarget = MF.getSubtarget<X86Subtarget>();
402 bool HasSSE = Subtarget.hasSSE1();
403 bool HasAVX = Subtarget.hasAVX();
404 bool HasAVX512 = Subtarget.hasAVX512();
405 bool HasEGPR = Subtarget.hasEGPR();
406
407 switch (CC) {
408 case CallingConv::GHC:
410 return CSR_NoRegs_RegMask;
412 if (HasAVX)
413 return CSR_64_AllRegs_AVX_RegMask;
414 return CSR_64_AllRegs_RegMask;
416 if (IsWin64)
417 return HasEGPR ? CSR_Win64_APX_RT_MostRegs_RegMask
418 : CSR_Win64_RT_MostRegs_RegMask;
419 return CSR_64_RT_MostRegs_RegMask;
421 if (HasAVX)
422 return CSR_64_RT_AllRegs_AVX_RegMask;
423 return CSR_64_RT_AllRegs_RegMask;
425 return CSR_64_NoneRegs_RegMask;
427 if (Is64Bit)
428 return CSR_64_TLS_Darwin_RegMask;
429 break;
431 if (HasAVX512 && IsWin64)
432 return HasEGPR ? CSR_Win64_APX_Intel_OCL_BI_AVX512_RegMask
433 : CSR_Win64_Intel_OCL_BI_AVX512_RegMask;
434 if (HasAVX512 && Is64Bit)
435 return CSR_64_Intel_OCL_BI_AVX512_RegMask;
436 if (HasAVX && IsWin64)
437 return HasEGPR ? CSR_Win64_APX_Intel_OCL_BI_AVX_RegMask
438 : CSR_Win64_Intel_OCL_BI_AVX_RegMask;
439 if (HasAVX && Is64Bit)
440 return CSR_64_Intel_OCL_BI_AVX_RegMask;
441 if (!HasAVX && !IsWin64 && Is64Bit)
442 return CSR_64_Intel_OCL_BI_RegMask;
443 break;
444 }
446 if (Is64Bit) {
447 if (IsWin64) {
448 if (HasSSE)
449 return HasEGPR ? CSR_Win64_APX_RegCall_RegMask
450 : CSR_Win64_RegCall_RegMask;
451 return CSR_Win64_RegCall_NoSSE_RegMask;
452 }
453 return HasSSE ? CSR_SysV64_RegCall_RegMask
454 : CSR_SysV64_RegCall_NoSSE_RegMask;
455 }
456 return HasSSE ? CSR_32_RegCall_RegMask : CSR_32_RegCall_NoSSE_RegMask;
458 if (Is64Bit) {
459 if (HasSSE)
460 return HasEGPR ? CSR_Win64_APX_CFGuard_Check_RegMask
461 : CSR_Win64_CFGuard_Check_RegMask;
462 return CSR_Win64_CFGuard_Check_NoSSE_RegMask;
463 }
464 return HasSSE ? CSR_Win32_CFGuard_Check_RegMask
465 : CSR_Win32_CFGuard_Check_NoSSE_RegMask;
467 if (Is64Bit)
468 return CSR_64_MostRegs_RegMask;
469 break;
471 return HasEGPR ? CSR_Win64_APX_RegMask : CSR_Win64_RegMask;
473 if (!Is64Bit)
474 return CSR_32_RegMask;
475 if (IsWin64)
476 return HasEGPR ? CSR_Win64_APX_SwiftTail_RegMask
477 : CSR_Win64_SwiftTail_RegMask;
478 return CSR_64_SwiftTail_RegMask;
480 return CSR_64_RegMask;
482 if (Is64Bit) {
483 if (HasAVX512)
484 return CSR_64_AllRegs_AVX512_RegMask;
485 if (HasAVX)
486 return CSR_64_AllRegs_AVX_RegMask;
487 if (HasSSE)
488 return CSR_64_AllRegs_RegMask;
489 return CSR_64_AllRegs_NoSSE_RegMask;
490 }
491 if (HasAVX512)
492 return CSR_32_AllRegs_AVX512_RegMask;
493 if (HasAVX)
494 return CSR_32_AllRegs_AVX_RegMask;
495 if (HasSSE)
496 return CSR_32_AllRegs_SSE_RegMask;
497 return CSR_32_AllRegs_RegMask;
498 default:
499 break;
500 }
501
502 // Unlike getCalleeSavedRegs(), we don't have MMI so we can't check
503 // callsEHReturn().
504 if (Is64Bit) {
505 const Function &F = MF.getFunction();
506 bool IsSwiftCC = Subtarget.getTargetLowering()->supportSwiftError() &&
507 F.getAttributes().hasAttrSomewhere(Attribute::SwiftError);
508 if (IsSwiftCC) {
509 if (IsWin64)
510 return HasEGPR ? CSR_Win64_APX_SwiftError_RegMask
511 : CSR_Win64_SwiftError_RegMask;
512 return CSR_64_SwiftError_RegMask;
513 }
514
515 if (IsWin64 || IsUEFI64)
516 return HasEGPR ? CSR_Win64_APX_RegMask : CSR_Win64_RegMask;
517 return CSR_64_RegMask;
518 }
519
520 return CSR_32_RegMask;
521}
522
523const uint32_t*
525 return CSR_NoRegs_RegMask;
526}
527
529 return CSR_64_TLS_Darwin_RegMask;
530}
531
533 BitVector Reserved(getNumRegs());
534 const X86FrameLowering *TFI = getFrameLowering(MF);
535
536 // Set the floating point control register as reserved.
537 Reserved.set(X86::FPCW);
538
539 // Set the floating point status register as reserved.
540 Reserved.set(X86::FPSW);
541
542 // Set the SIMD floating point control register as reserved.
543 Reserved.set(X86::MXCSR);
544
545 // Set the stack-pointer register and its aliases as reserved.
546 for (const MCPhysReg &SubReg : subregs_inclusive(X86::RSP))
547 Reserved.set(SubReg);
548
549 // Set the Shadow Stack Pointer as reserved.
550 Reserved.set(X86::SSP);
551
552 auto &ST = MF.getSubtarget<X86Subtarget>();
553 if (ST.hasUserReservedRegisters()) {
554 if (ST.is64Bit()) {
555 // Set r# as reserved register if user required.
556 for (unsigned Reg = X86::R8; Reg <= X86::R15; ++Reg)
557 if (ST.isRegisterReservedByUser(Reg))
558 for (const MCPhysReg &SubReg : subregs_inclusive(Reg))
559 Reserved.set(SubReg);
560 if (ST.hasEGPR())
561 for (unsigned Reg = X86::R16; Reg <= X86::R31; ++Reg)
562 if (ST.isRegisterReservedByUser(Reg))
563 for (const MCPhysReg &SubReg : subregs_inclusive(Reg))
564 Reserved.set(SubReg);
565 } else {
566 if (ST.isRegisterReservedByUser(X86::EDI))
567 for (const MCPhysReg &SubReg : sub_and_superregs_inclusive(X86::EDI))
568 Reserved.set(SubReg);
569 }
570 }
571
572 // Set the instruction pointer register and its aliases as reserved.
573 for (const MCPhysReg &SubReg : subregs_inclusive(X86::RIP))
574 Reserved.set(SubReg);
575
576 // Set the frame-pointer register and its aliases as reserved if needed.
577 if (TFI->hasFP(MF) || MF.framePointerIsReserved()) {
580 SMLoc(),
581 "Frame pointer clobbered by function invoke is not supported.");
582
583 for (const MCPhysReg &SubReg : subregs_inclusive(X86::RBP))
584 Reserved.set(SubReg);
585 }
586
587 // Set the base-pointer register and its aliases as reserved if needed.
588 if (hasBasePointer(MF)) {
591 "Stack realignment in presence of dynamic "
592 "allocas is not supported with "
593 "this calling convention.");
594
596 for (const MCPhysReg &SubReg : subregs_inclusive(BasePtr))
597 Reserved.set(SubReg);
598 }
599
600 // Mark the segment registers as reserved.
601 Reserved.set(X86::CS);
602 Reserved.set(X86::SS);
603 Reserved.set(X86::DS);
604 Reserved.set(X86::ES);
605 Reserved.set(X86::FS);
606 Reserved.set(X86::GS);
607
608 // Mark the floating point stack registers as reserved.
609 for (unsigned n = 0; n != 8; ++n)
610 Reserved.set(X86::ST0 + n);
611
612 // Without usable x87 (soft float or -mno-x87), reserve the allocatable FPn
613 // pseudos (FP0-FP6; FP7 is already non-allocatable) so they aren't scrubbed.
614 if (ST.useSoftFloat() || !ST.hasX87())
615 for (unsigned n = 0; n != 7; ++n)
616 Reserved.set(X86::FP0 + n);
617
618 // Reserve the registers that only exist in 64-bit mode.
619 if (!Is64Bit) {
620 // These 8-bit registers are part of the x86-64 extension even though their
621 // super-registers are old 32-bits.
622 Reserved.set(X86::SIL);
623 Reserved.set(X86::DIL);
624 Reserved.set(X86::BPL);
625 Reserved.set(X86::SPL);
626 Reserved.set(X86::SIH);
627 Reserved.set(X86::DIH);
628 Reserved.set(X86::BPH);
629 Reserved.set(X86::SPH);
630
631 for (unsigned n = 0; n != 8; ++n) {
632 // R8, R9, ...
633 for (MCRegAliasIterator AI(X86::R8 + n, this, true); AI.isValid(); ++AI)
634 Reserved.set(*AI);
635
636 // XMM8, XMM9, ...
637 for (MCRegAliasIterator AI(X86::XMM8 + n, this, true); AI.isValid(); ++AI)
638 Reserved.set(*AI);
639 }
640 }
641 if (!Is64Bit || !MF.getSubtarget<X86Subtarget>().hasAVX512()) {
642 for (unsigned n = 0; n != 16; ++n) {
643 for (MCRegAliasIterator AI(X86::XMM16 + n, this, true); AI.isValid();
644 ++AI)
645 Reserved.set(*AI);
646 }
647 }
648
649 // Reserve the extended general purpose registers.
650 if (!Is64Bit || !MF.getSubtarget<X86Subtarget>().hasEGPR())
651 Reserved.set(X86::R16, X86::R31WH + 1);
652
653 // Due to specifics of setjmp unwinding in Win64 APX ABI, the unwinder
654 // cannot restore R30/R31. Reserve them to prevent register allocation.
655 // https://learn.microsoft.com/en-us/cpp/build/x64-calling-convention#setjmplongjmp
656 if (MF.exposesReturnsTwice() && ST.isTargetWin64()) {
657 unsigned NumReservedCSRs = 0;
658 for (unsigned Reg = X86::R16; Reg <= X86::R31; ++Reg)
659 if (isCalleeSavedPhysReg(Reg, MF)) {
660 ++NumReservedCSRs;
661 for (const MCPhysReg &SubReg : subregs_inclusive(Reg))
662 Reserved.set(SubReg);
663 }
664 if (NumReservedCSRs && MF.size() > SetjmpCSRWarningThreshold &&
667 SMLoc(), Twine(NumReservedCSRs) +
668 " callee-saved register(s) reserved due to setjmp in '" +
669 MF.getName() +
670 "'; this may impact performance in large functions");
671 }
672 }
673
675 for (MCRegAliasIterator AI(X86::R14, this, true); AI.isValid(); ++AI)
676 Reserved.set(*AI);
677 for (MCRegAliasIterator AI(X86::R15, this, true); AI.isValid(); ++AI)
678 Reserved.set(*AI);
679 }
680
681 // Reserve registers for LFI sandboxing.
682 if (MF.getSubtarget<X86Subtarget>().isLFI()) {
683 for (MCRegAliasIterator AI(X86::R11, this, true); AI.isValid(); ++AI)
684 Reserved.set(*AI);
685 for (MCRegAliasIterator AI(X86::R14, this, true); AI.isValid(); ++AI)
686 Reserved.set(*AI);
687 for (MCRegAliasIterator AI(X86::R15, this, true); AI.isValid(); ++AI)
688 Reserved.set(*AI);
689 }
690
691 assert(checkAllSuperRegsMarked(Reserved,
692 {X86::SIL, X86::DIL, X86::BPL, X86::SPL,
693 X86::SIH, X86::DIH, X86::BPH, X86::SPH}));
694 return Reserved;
695}
696
698 // All existing Intel CPUs that support AMX support AVX512 and all existing
699 // Intel CPUs that support APX support AMX. AVX512 implies AVX.
700 //
701 // We enumerate the registers in X86GenRegisterInfo.inc in this order:
702 //
703 // Registers before AVX512,
704 // AVX512 registers (X/YMM16-31, ZMM0-31, K registers)
705 // AMX registers (TMM)
706 // APX registers (R16-R31)
707 //
708 // and try to return the minimum number of registers supported by the target.
709 static_assert((X86::R15WH + 1 == X86::YMM0) && (X86::YMM15 + 1 == X86::K0) &&
710 (X86::K6_K7 + 1 == X86::TMMCFG) &&
711 (X86::TMM7 + 1 == X86::R16) &&
712 (X86::R31WH + 1 == X86::NUM_TARGET_REGS),
713 "Register number may be incorrect");
714
715 const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
716 if (ST.hasEGPR())
717 return X86::NUM_TARGET_REGS;
718 if (ST.hasAMXTILE())
719 return X86::TMM7 + 1;
720 if (ST.hasAVX512())
721 return X86::K6_K7 + 1;
722 if (ST.hasAVX())
723 return X86::YMM15 + 1;
724 return X86::R15WH + 1;
725}
726
728 MCRegister Reg) const {
729 const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
730 const TargetRegisterInfo &TRI = *ST.getRegisterInfo();
731 auto IsSubReg = [&](MCRegister RegA, MCRegister RegB) {
732 return TRI.isSuperOrSubRegisterEq(RegA, RegB);
733 };
734
735 if (!ST.is64Bit())
736 return llvm::any_of(
737 SmallVector<MCRegister>{X86::EAX, X86::ECX, X86::EDX},
738 [&](MCRegister &RegA) { return IsSubReg(RegA, Reg); }) ||
739 (ST.hasMMX() && X86::VR64RegClass.contains(Reg));
740
742
743 if (CC == CallingConv::X86_64_SysV && IsSubReg(X86::RAX, Reg))
744 return true;
745
746 if (llvm::any_of(
747 SmallVector<MCRegister>{X86::RDX, X86::RCX, X86::R8, X86::R9},
748 [&](MCRegister &RegA) { return IsSubReg(RegA, Reg); }))
749 return true;
750
751 if (CC != CallingConv::Win64 &&
752 llvm::any_of(SmallVector<MCRegister>{X86::RDI, X86::RSI},
753 [&](MCRegister &RegA) { return IsSubReg(RegA, Reg); }))
754 return true;
755
756 if (ST.hasSSE1() &&
757 llvm::any_of(SmallVector<MCRegister>{X86::XMM0, X86::XMM1, X86::XMM2,
758 X86::XMM3, X86::XMM4, X86::XMM5,
759 X86::XMM6, X86::XMM7},
760 [&](MCRegister &RegA) { return IsSubReg(RegA, Reg); }))
761 return true;
762
763 return X86GenRegisterInfo::isArgumentRegister(MF, Reg);
764}
765
767 MCRegister PhysReg) const {
768 const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
769 const TargetRegisterInfo &TRI = *ST.getRegisterInfo();
770
771 // Stack pointer.
772 if (TRI.isSuperOrSubRegisterEq(X86::RSP, PhysReg))
773 return true;
774
775 // Don't use the frame pointer if it's being used.
776 const X86FrameLowering &TFI = *getFrameLowering(MF);
777 if (TFI.hasFP(MF) && TRI.isSuperOrSubRegisterEq(X86::RBP, PhysReg))
778 return true;
779
780 return X86GenRegisterInfo::isFixedRegister(MF, PhysReg);
781}
782
784 return RC->getID() == X86::TILERegClassID;
785}
786
788 // Check if the EFLAGS register is marked as live-out. This shouldn't happen,
789 // because the calling convention defines the EFLAGS register as NOT
790 // preserved.
791 //
792 // Unfortunatelly the EFLAGS show up as live-out after branch folding. Adding
793 // an assert to track this and clear the register afterwards to avoid
794 // unnecessary crashes during release builds.
795 assert(!(Mask[X86::EFLAGS / 32] & (1U << (X86::EFLAGS % 32))) &&
796 "EFLAGS are not live-out from a patchpoint.");
797
798 // Also clean other registers that don't need preserving (IP).
799 for (auto Reg : {X86::EFLAGS, X86::RIP, X86::EIP, X86::IP})
800 Mask[Reg / 32] &= ~(1U << (Reg % 32));
801}
802
803//===----------------------------------------------------------------------===//
804// Stack Frame Processing methods
805//===----------------------------------------------------------------------===//
806
807static bool CantUseSP(const MachineFrameInfo &MFI) {
808 return MFI.hasVarSizedObjects() || MFI.hasOpaqueSPAdjustment();
809}
810
813 // We have a virtual register to reference argument, and don't need base
814 // pointer.
815 if (X86FI->getStackPtrSaveMI() != nullptr)
816 return false;
817
818 if (X86FI->hasPreallocatedCall())
819 return true;
820
821 const MachineFrameInfo &MFI = MF.getFrameInfo();
822
824 return false;
825
826 // When we need stack realignment, we can't address the stack from the frame
827 // pointer. When we have dynamic allocas or stack-adjusting inline asm, we
828 // can't address variables from the stack pointer. MS inline asm can
829 // reference locals while also adjusting the stack pointer. When we can't
830 // use both the SP and the FP, we need a separate base pointer register.
831 bool CantUseFP = hasStackRealignment(MF);
832 return CantUseFP && CantUseSP(MFI);
833}
834
837 return false;
838
839 const MachineFrameInfo &MFI = MF.getFrameInfo();
840 const MachineRegisterInfo *MRI = &MF.getRegInfo();
841
842 // Stack realignment requires a frame pointer. If we already started
843 // register allocation with frame pointer elimination, it is too late now.
844 if (!MRI->canReserveReg(FramePtr))
845 return false;
846
847 // If a base pointer is necessary. Check that it isn't too late to reserve
848 // it.
849 if (CantUseSP(MFI))
850 return MRI->canReserveReg(BasePtr);
851 return true;
852}
853
856 return true;
857
858 return !Is64Bit && MF.getFunction().getCallingConv() == CallingConv::X86_INTR;
859}
860
861// tryOptimizeLEAtoMOV - helper function that tries to replace a LEA instruction
862// of the form 'lea (%esp), %ebx' --> 'mov %esp, %ebx'.
863// TODO: In this case we should be really trying first to entirely eliminate
864// this instruction which is a plain copy.
866 MachineInstr &MI = *II;
867 unsigned Opc = II->getOpcode();
868 // Check if this is a LEA of the form 'lea (%esp), %ebx'
869 if ((Opc != X86::LEA32r && Opc != X86::LEA64r && Opc != X86::LEA64_32r) ||
870 MI.getOperand(2).getImm() != 1 ||
871 MI.getOperand(3).getReg() != X86::NoRegister ||
872 MI.getOperand(4).getImm() != 0 ||
873 MI.getOperand(5).getReg() != X86::NoRegister)
874 return false;
875 Register BasePtr = MI.getOperand(1).getReg();
876 // In X32 mode, ensure the base-pointer is a 32-bit operand, so the LEA will
877 // be replaced with a 32-bit operand MOV which will zero extend the upper
878 // 32-bits of the super register.
879 if (Opc == X86::LEA64_32r)
880 BasePtr = getX86SubSuperRegister(BasePtr, 32);
881 Register NewDestReg = MI.getOperand(0).getReg();
882 const X86InstrInfo *TII =
883 MI.getParent()->getParent()->getSubtarget<X86Subtarget>().getInstrInfo();
884 TII->copyPhysReg(*MI.getParent(), II, MI.getDebugLoc(), NewDestReg, BasePtr,
885 MI.getOperand(1).isKill());
886 MI.eraseFromParent();
887 return true;
888}
889
891 switch (MI.getOpcode()) {
892 case X86::CATCHRET:
893 case X86::CLEANUPRET:
894 return true;
895 default:
896 return false;
897 }
898 llvm_unreachable("impossible");
899}
900
902 unsigned FIOperandNum,
903 Register BaseReg,
904 int FIOffset) const {
905 MachineInstr &MI = *II;
906 unsigned Opc = MI.getOpcode();
907 if (Opc == TargetOpcode::LOCAL_ESCAPE) {
908 MachineOperand &FI = MI.getOperand(FIOperandNum);
909 FI.ChangeToImmediate(FIOffset);
910 return;
911 }
912
913 MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false);
914
915 // The frame index format for stackmaps and patchpoints is different from the
916 // X86 format. It only has a FI and an offset.
917 if (Opc == TargetOpcode::STACKMAP || Opc == TargetOpcode::PATCHPOINT) {
918 assert(BasePtr == FramePtr && "Expected the FP as base register");
919 int64_t Offset = MI.getOperand(FIOperandNum + 1).getImm() + FIOffset;
920 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
921 return;
922 }
923
924 if (MI.getOperand(FIOperandNum + 3).isImm()) {
925 // Offset is a 32-bit integer.
926 int Imm = (int)(MI.getOperand(FIOperandNum + 3).getImm());
927 int Offset = FIOffset + Imm;
928 assert((!Is64Bit || isInt<32>((long long)FIOffset + Imm)) &&
929 "Requesting 64-bit offset in 32-bit immediate!");
930 if (Offset != 0)
931 MI.getOperand(FIOperandNum + 3).ChangeToImmediate(Offset);
932 } else {
933 // Offset is symbolic. This is extremely rare.
934 uint64_t Offset =
935 FIOffset + (uint64_t)MI.getOperand(FIOperandNum + 3).getOffset();
936 MI.getOperand(FIOperandNum + 3).setOffset(Offset);
937 }
938}
939
940bool
942 int SPAdj, unsigned FIOperandNum,
943 RegScavenger *RS) const {
944 MachineInstr &MI = *II;
945 MachineBasicBlock &MBB = *MI.getParent();
946 MachineFunction &MF = *MBB.getParent();
947 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
948 bool IsEHFuncletEpilogue = MBBI == MBB.end() ? false
950 const X86FrameLowering *TFI = getFrameLowering(MF);
951 int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
952
953 // Determine base register and offset.
954 int64_t FIOffset;
955 Register BasePtr;
956 if (MI.isReturn()) {
957 assert((!hasStackRealignment(MF) ||
958 MF.getFrameInfo().isFixedObjectIndex(FrameIndex)) &&
959 "Return instruction can only reference SP relative frame objects");
960 FIOffset =
961 TFI->getFrameIndexReferenceSP(MF, FrameIndex, BasePtr, 0).getFixed();
962 } else if (TFI->Is64Bit && (MBB.isEHFuncletEntry() || IsEHFuncletEpilogue)) {
963 FIOffset = TFI->getWin64EHFrameIndexRef(MF, FrameIndex, BasePtr);
964 } else {
965 FIOffset = TFI->getFrameIndexReference(MF, FrameIndex, BasePtr).getFixed();
966 }
967
968 // LOCAL_ESCAPE uses a single offset, with no register. It only works in the
969 // simple FP case, and doesn't work with stack realignment. On 32-bit, the
970 // offset is from the traditional base pointer location. On 64-bit, the
971 // offset is from the SP at the end of the prologue, not the FP location. This
972 // matches the behavior of llvm.frameaddress.
973 unsigned Opc = MI.getOpcode();
974 if (Opc == TargetOpcode::LOCAL_ESCAPE) {
975 MachineOperand &FI = MI.getOperand(FIOperandNum);
976 FI.ChangeToImmediate(FIOffset);
977 return false;
978 }
979
980 // For LEA64_32r when BasePtr is 32-bits (X32) we can use full-size 64-bit
981 // register as source operand, semantic is the same and destination is
982 // 32-bits. It saves one byte per lea in code since 0x67 prefix is avoided.
983 // Don't change BasePtr since it is used later for stack adjustment.
984 Register MachineBasePtr = BasePtr;
985 if (Opc == X86::LEA64_32r && X86::GR32RegClass.contains(BasePtr))
986 MachineBasePtr = getX86SubSuperRegister(BasePtr, 64);
987
988 // This must be part of a four operand memory reference. Replace the
989 // FrameIndex with base register. Add an offset to the offset.
990 MI.getOperand(FIOperandNum).ChangeToRegister(MachineBasePtr, false);
991
992 if (BasePtr == StackPtr)
993 FIOffset += SPAdj;
994
995 // The frame index format for stackmaps and patchpoints is different from the
996 // X86 format. It only has a FI and an offset.
997 if (Opc == TargetOpcode::STACKMAP || Opc == TargetOpcode::PATCHPOINT) {
998 assert(BasePtr == FramePtr && "Expected the FP as base register");
999 int64_t Offset = MI.getOperand(FIOperandNum + 1).getImm() + FIOffset;
1000 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
1001 return false;
1002 }
1003
1004 if (MI.getOperand(FIOperandNum+3).isImm()) {
1005 const X86InstrInfo *TII = MF.getSubtarget<X86Subtarget>().getInstrInfo();
1006 const DebugLoc &DL = MI.getDebugLoc();
1007 int64_t Imm = MI.getOperand(FIOperandNum + 3).getImm();
1008 int64_t Offset = FIOffset + Imm;
1009 bool FitsIn32Bits = isInt<32>(Offset);
1010 // If the offset will not fit in a 32-bit displacement, then for 64-bit
1011 // targets, scavenge a register to hold it. Otherwise...
1012 if (Is64Bit && !FitsIn32Bits) {
1013 assert(RS && "RegisterScavenger was NULL");
1014
1015 RS->enterBasicBlockEnd(MBB);
1016 RS->backward(std::next(II));
1017
1018 Register ScratchReg = RS->scavengeRegisterBackwards(
1019 X86::GR64RegClass, II, /*RestoreAfter=*/false, /*SPAdj=*/0,
1020 /*AllowSpill=*/true);
1021 assert(ScratchReg != 0 && "scratch reg was 0");
1022 RS->setRegUsed(ScratchReg);
1023
1024 BuildMI(MBB, II, DL, TII->get(X86::MOV64ri), ScratchReg).addImm(Offset);
1025
1026 MI.getOperand(FIOperandNum + 3).setImm(0);
1027 if (MI.getOperand(FIOperandNum + 2).getReg() == X86::NoRegister) {
1028 MI.getOperand(FIOperandNum + 2).setReg(ScratchReg);
1029 } else {
1030 // The index register slot is already in use, fold the offset into
1031 // the base register instead. LEA does not clobber EFLAGS.
1032 BuildMI(MBB, II, DL, TII->get(X86::LEA64r), ScratchReg)
1033 .addReg(MachineBasePtr)
1034 .addImm(1)
1035 .addReg(ScratchReg)
1036 .addImm(0)
1037 .addReg(X86::NoRegister);
1038 MI.getOperand(FIOperandNum).setReg(ScratchReg);
1039 }
1040
1041 return false;
1042 }
1043
1044 // ... for 32-bit targets, this is a bug!
1045 if (!Is64Bit && !FitsIn32Bits) {
1046 MI.emitGenericError("64-bit offset calculated but target is 32-bit");
1047 // Trap so that the instruction verification pass does not fail if run.
1048 BuildMI(MBB, MBBI, DL, TII->get(X86::TRAP));
1049 return false;
1050 }
1051
1052 if (Offset != 0 || !tryOptimizeLEAtoMOV(II))
1053 MI.getOperand(FIOperandNum + 3).ChangeToImmediate(Offset);
1054 } else {
1055 // Offset is symbolic. This is extremely rare.
1056 uint64_t Offset = FIOffset +
1057 (uint64_t)MI.getOperand(FIOperandNum+3).getOffset();
1058 MI.getOperand(FIOperandNum + 3).setOffset(Offset);
1059 }
1060 return false;
1061}
1062
1065 const MachineFunction *MF = MBB.getParent();
1066 const MachineRegisterInfo &MRI = MF->getRegInfo();
1067 if (MF->callsEHReturn())
1068 return 0;
1069
1070 if (MBBI == MBB.end())
1071 return 0;
1072
1073 switch (MBBI->getOpcode()) {
1074 default:
1075 return 0;
1076 case TargetOpcode::PATCHABLE_RET:
1077 case X86::RET:
1078 case X86::RET32:
1079 case X86::RET64:
1080 case X86::RETI32:
1081 case X86::RETI64:
1082 case X86::TCRETURNdi:
1083 case X86::TCRETURNri:
1084 case X86::TCRETURN_WIN64ri:
1085 case X86::TCRETURN_HIPE32ri:
1086 case X86::TCRETURNmi:
1087 case X86::TCRETURNdi64:
1088 case X86::TCRETURNri64:
1089 case X86::TCRETURNri64_ImpCall:
1090 case X86::TCRETURNmi64:
1091 case X86::TCRETURN_WINmi64:
1092 case X86::EH_RETURN:
1093 case X86::EH_RETURN64: {
1094 LiveRegUnits LRU(*this);
1095 LRU.addLiveOuts(MBB);
1096 LRU.stepBackward(*MBBI);
1097
1098 const TargetRegisterClass &RC =
1099 Is64Bit ? X86::GR64_NOSPRegClass : X86::GR32_NOSPRegClass;
1100 for (MCRegister Reg : RC) {
1101 if (LRU.available(Reg) && !MRI.isReserved(Reg))
1102 return Reg;
1103 }
1104 }
1105 }
1106
1107 return 0;
1108}
1109
1111 const X86FrameLowering *TFI = getFrameLowering(MF);
1112 return TFI->hasFP(MF) ? FramePtr : StackPtr;
1113}
1114
1117 const X86Subtarget &Subtarget = MF.getSubtarget<X86Subtarget>();
1118 Register FrameReg = getFrameRegister(MF);
1119 if (Subtarget.isTarget64BitILP32())
1120 FrameReg = getX86SubSuperRegister(FrameReg, 32);
1121 return FrameReg;
1122}
1123
1126 const X86Subtarget &Subtarget = MF.getSubtarget<X86Subtarget>();
1127 Register StackReg = getStackRegister();
1128 if (Subtarget.isTarget64BitILP32())
1129 StackReg = getX86SubSuperRegister(StackReg, 32);
1130 return StackReg;
1131}
1132
1134 const MachineRegisterInfo *MRI) {
1135 if (VRM->hasShape(VirtReg))
1136 return VRM->getShape(VirtReg);
1137
1138 const MachineOperand &Def = *MRI->def_begin(VirtReg);
1139 MachineInstr *MI = const_cast<MachineInstr *>(Def.getParent());
1140 unsigned OpCode = MI->getOpcode();
1141 switch (OpCode) {
1142 default:
1143 llvm_unreachable("Unexpected machine instruction on tile register!");
1144 break;
1145 case X86::COPY: {
1146 Register SrcReg = MI->getOperand(1).getReg();
1147 ShapeT Shape = getTileShape(SrcReg, VRM, MRI);
1148 VRM->assignVirt2Shape(VirtReg, Shape);
1149 return Shape;
1150 }
1151 // We only collect the tile shape that is defined.
1152 case X86::PTILELOADDV:
1153 case X86::PTILELOADDT1V:
1154 case X86::PTDPBSSDV:
1155 case X86::PTDPBSUDV:
1156 case X86::PTDPBUSDV:
1157 case X86::PTDPBUUDV:
1158 case X86::PTILEZEROV:
1159 case X86::PTDPBF16PSV:
1160 case X86::PTDPFP16PSV:
1161 case X86::PTCMMIMFP16PSV:
1162 case X86::PTCMMRLFP16PSV:
1163 case X86::PTILELOADDRSV:
1164 case X86::PTILELOADDRST1V:
1165 case X86::PTDPBF8PSV:
1166 case X86::PTDPBHF8PSV:
1167 case X86::PTDPHBF8PSV:
1168 case X86::PTDPHF8PSV: {
1169 MachineOperand &MO1 = MI->getOperand(1);
1170 MachineOperand &MO2 = MI->getOperand(2);
1171 ShapeT Shape(&MO1, &MO2, MRI);
1172 VRM->assignVirt2Shape(VirtReg, Shape);
1173 return Shape;
1174 }
1175 }
1176}
1177
1179 ArrayRef<MCPhysReg> Order,
1181 const MachineFunction &MF,
1182 const VirtRegMap *VRM,
1183 const LiveRegMatrix *Matrix) const {
1184 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1185 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
1186 bool BaseImplRetVal = TargetRegisterInfo::getRegAllocationHints(
1187 VirtReg, Order, Hints, MF, VRM, Matrix);
1188 const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
1189 const TargetRegisterInfo &TRI = *ST.getRegisterInfo();
1190
1191 unsigned ID = RC.getID();
1192
1193 if (!VRM)
1194 return BaseImplRetVal;
1195
1196 if (ID != X86::TILERegClassID) {
1197 if (DisableRegAllocNDDHints || !ST.hasNDD() ||
1198 !TRI.isGeneralPurposeRegisterClass(&RC))
1199 return BaseImplRetVal;
1200
1201 // Add any two address hints after any copy hints.
1202 SmallSet<unsigned, 4> TwoAddrHints;
1203
1204 auto TryAddNDDHint = [&](const MachineOperand &MO) {
1205 Register Reg = MO.getReg();
1206 Register PhysReg = Reg.isPhysical() ? Reg : Register(VRM->getPhys(Reg));
1207 if (PhysReg && !MRI->isReserved(PhysReg) && !is_contained(Hints, PhysReg))
1208 TwoAddrHints.insert(PhysReg);
1209 };
1210
1211 // NDD instructions is compressible when Op0 is allocated to the same
1212 // physic register as Op1 (or Op2 if it's commutable).
1213 for (auto &MO : MRI->reg_nodbg_operands(VirtReg)) {
1214 const MachineInstr &MI = *MO.getParent();
1215 if (!X86::getNonNDVariant(MI.getOpcode()))
1216 continue;
1217 unsigned OpIdx = MI.getOperandNo(&MO);
1218 if (OpIdx == 0) {
1219 assert(MI.getOperand(1).isReg());
1220 TryAddNDDHint(MI.getOperand(1));
1221 if (MI.isCommutable()) {
1222 assert(MI.getOperand(2).isReg());
1223 TryAddNDDHint(MI.getOperand(2));
1224 }
1225 } else if (OpIdx == 1) {
1226 TryAddNDDHint(MI.getOperand(0));
1227 } else if (MI.isCommutable() && OpIdx == 2) {
1228 TryAddNDDHint(MI.getOperand(0));
1229 }
1230 }
1231
1232 for (MCPhysReg OrderReg : Order)
1233 if (TwoAddrHints.count(OrderReg))
1234 Hints.push_back(OrderReg);
1235
1236 return BaseImplRetVal;
1237 }
1238
1239 ShapeT VirtShape = getTileShape(VirtReg, const_cast<VirtRegMap *>(VRM), MRI);
1240 auto AddHint = [&](MCPhysReg PhysReg) {
1241 Register VReg = Matrix->getOneVReg(PhysReg);
1242 if (VReg == MCRegister::NoRegister) { // Not allocated yet
1243 Hints.push_back(PhysReg);
1244 return;
1245 }
1246 ShapeT PhysShape = getTileShape(VReg, const_cast<VirtRegMap *>(VRM), MRI);
1247 if (PhysShape == VirtShape)
1248 Hints.push_back(PhysReg);
1249 };
1250
1251 SmallSet<MCPhysReg, 4> CopyHints(llvm::from_range, Hints);
1252 Hints.clear();
1253 for (auto Hint : CopyHints) {
1254 if (RC.contains(Hint) && !MRI->isReserved(Hint))
1255 AddHint(Hint);
1256 }
1257 for (MCPhysReg PhysReg : Order) {
1258 if (!CopyHints.count(PhysReg) && RC.contains(PhysReg) &&
1259 !MRI->isReserved(PhysReg))
1260 AddHint(PhysReg);
1261 }
1262
1263#define DEBUG_TYPE "tile-hint"
1264 LLVM_DEBUG({
1265 dbgs() << "Hints for virtual register " << format_hex(VirtReg, 8) << "\n";
1266 for (auto Hint : Hints) {
1267 dbgs() << "tmm" << Hint << ",";
1268 }
1269 dbgs() << "\n";
1270 });
1271#undef DEBUG_TYPE
1272
1273 return true;
1274}
1275
1277 const TargetRegisterClass *RC) const {
1278 switch (RC->getID()) {
1279 default:
1280 return RC;
1281 case X86::GR8RegClassID:
1282 return &X86::GR8_NOREX2RegClass;
1283 case X86::GR16RegClassID:
1284 return &X86::GR16_NOREX2RegClass;
1285 case X86::GR32RegClassID:
1286 return &X86::GR32_NOREX2RegClass;
1287 case X86::GR64RegClassID:
1288 return &X86::GR64_NOREX2RegClass;
1289 case X86::GR32_NOSPRegClassID:
1290 return &X86::GR32_NOREX2_NOSPRegClass;
1291 case X86::GR64_NOSPRegClassID:
1292 return &X86::GR64_NOREX2_NOSPRegClass;
1293 }
1294}
1295
1297 switch (RC->getID()) {
1298 default:
1299 return false;
1300 case X86::GR8_NOREX2RegClassID:
1301 case X86::GR16_NOREX2RegClassID:
1302 case X86::GR32_NOREX2RegClassID:
1303 case X86::GR64_NOREX2RegClassID:
1304 case X86::GR32_NOREX2_NOSPRegClassID:
1305 case X86::GR64_NOREX2_NOSPRegClassID:
1306 case X86::GR64_with_sub_16bit_in_GR16_NOREX2RegClassID:
1307 return true;
1308 }
1309}
static const TargetRegisterClass * getRegClass(const MachineInstr &MI, Register Reg)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned Imm
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
This file implements the BitVector class.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Live Register Matrix
static cl::opt< bool > EnableBasePointer("m68k-use-base-pointer", cl::Hidden, cl::init(true), cl::desc("Enable use of a base pointer for complex stack frames"))
static bool CantUseSP(const MachineFrameInfo &MFI)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
uint64_t IntrinsicInst * II
This file declares the machine register scavenger class.
This file contains some templates that are useful if you are working with the STL at all.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:484
This file defines the SmallSet class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
cl::opt< bool > X86EnableAPXForRelocation
static cl::opt< unsigned > SetjmpCSRWarningThreshold("x86-setjmp-csr-warning-threshold", cl::Hidden, cl::init(50), cl::desc("Basic block count threshold for emitting a warning about " "callee-saved registers reserved due to setjmp"))
static cl::opt< bool > EnableBasePointer("x86-use-base-pointer", cl::Hidden, cl::init(true), cl::desc("Enable use of a base pointer for complex stack frames"))
static bool tryOptimizeLEAtoMOV(MachineBasicBlock::iterator II)
static cl::opt< bool > DisableRegAllocNDDHints("x86-disable-regalloc-hints-for-ndd", cl::Hidden, cl::init(false), cl::desc("Disable two address hints for register " "allocation"))
static ShapeT getTileShape(Register VirtReg, VirtRegMap *VRM, const MachineRegisterInfo *MRI)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:130
iterator begin() const
Definition ArrayRef.h:129
A debug info location.
Definition DebugLoc.h:126
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:273
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:730
A set of register units used to track register liveness.
bool available(MCRegister Reg) const
Returns true if no part of physical register Reg is live.
LLVM_ABI void stepBackward(const MachineInstr &MI)
Updates liveness when stepping backwards over the instruction MI.
LLVM_ABI void addLiveOuts(const MachineBasicBlock &MBB)
Adds registers living out of block MBB.
LLVM_ABI void reportWarning(SMLoc L, const Twine &Msg)
LLVM_ABI void reportError(SMLoc L, const Twine &Msg)
MCRegAliasIterator enumerates all registers aliasing Reg.
ArrayRef< unsigned > superclasses() const
Returns a list of super-classes.
unsigned getID() const
getID() - Return the register class ID number.
bool contains(MCRegister Reg) const
contains - Return true if the specified register is included in this register class.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
static constexpr unsigned NoRegister
Definition MCRegister.h:60
MachineInstrBundleIterator< MachineInstr > iterator
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
bool hasOpaqueSPAdjustment() const
Returns true if the function contains opaque dynamic stack adjustments.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
bool exposesReturnsTwice() const
exposesReturnsTwice - Returns true if the function calls setjmp or any other similar functions with a...
LLVM_ABI bool framePointerIsReserved() const
Returns true if the frame pointer must always either point to a new frame record or be un-modified in...
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MCContext & getContext() const
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
LLVM_ABI void ChangeToImmediate(int64_t ImmVal, unsigned TargetFlags=0)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
bool isReserved(MCRegister PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
def_iterator def_begin(Register RegNo) const
bool reservedRegsFrozen() const
reservedRegsFrozen - Returns true after freezeReservedRegs() was called to ensure the set of reserved...
bool canReserveReg(MCRegister PhysReg) const
canReserveReg - Returns true if PhysReg can be used as a reserved register.
iterator_range< reg_nodbg_iterator > reg_nodbg_operands(Register Reg) const
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
Represents a location in source code.
Definition SMLoc.h:22
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition SmallSet.h:134
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition SmallSet.h:176
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
static StackOffset getFixed(int64_t Fixed)
Definition TypeSize.h:39
bool hasFP(const MachineFunction &MF) const
hasFP - Return true if the specified function should have a dedicated frame pointer register.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual bool canRealignStack(const MachineFunction &MF) const
True if the stack can be realigned for the target.
virtual bool shouldRealignStack(const MachineFunction &MF) const
True if storage within the function requires the stack pointer to be aligned more than the normal cal...
virtual bool getRegAllocationHints(Register VirtReg, ArrayRef< MCPhysReg > Order, SmallVectorImpl< MCPhysReg > &Hints, const MachineFunction &MF, const VirtRegMap *VRM=nullptr, const LiveRegMatrix *Matrix=nullptr) const
Get a list of 'hint' registers that the register allocator should try first when allocating a physica...
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
bool hasShape(Register virtReg) const
Definition VirtRegMap.h:102
ShapeT getShape(Register virtReg) const
Definition VirtRegMap.h:106
MCRegister getPhys(Register virtReg) const
returns the physical register mapped to the specified virtual register
Definition VirtRegMap.h:91
void assignVirt2Shape(Register virtReg, ShapeT shape)
Definition VirtRegMap.h:111
StackOffset getFrameIndexReferenceSP(const MachineFunction &MF, int FI, Register &SPReg, int Adjustment) const
StackOffset getFrameIndexReference(const MachineFunction &MF, int FI, Register &FrameReg) const override
getFrameIndexReference - This method should return the base register and offset used to reference a f...
bool Is64Bit
Is64Bit implies that x86_64 instructions are available.
int getWin64EHFrameIndexRef(const MachineFunction &MF, int FI, Register &SPReg) const
X86MachineFunctionInfo - This class is derived from MachineFunction and contains private X86 target-s...
MachineInstr * getStackPtrSaveMI() const
bool hasBasePointer(const MachineFunction &MF) const
const TargetRegisterClass * getPointerRegClass(unsigned Kind=0) const override
getPointerRegClass - Returns a TargetRegisterClass used for pointer values.
const MCPhysReg * getCalleeSavedRegsViaCopy(const MachineFunction *MF) const
bool canRealignStack(const MachineFunction &MF) const override
BitVector getReservedRegs(const MachineFunction &MF) const override
getReservedRegs - Returns a bitset indexed by physical register number indicating if a register is a ...
Register getPtrSizedFrameRegister(const MachineFunction &MF) const
bool shouldRealignStack(const MachineFunction &MF) const override
unsigned getNumSupportedRegs(const MachineFunction &MF) const override
Return the number of registers for the function.
const MCPhysReg * getIPRACSRegs(const MachineFunction *MF) const override
getIPRACSRegs - This API can be removed when rbp is safe to optimized out when IPRA is on.
Register getFrameRegister(const MachineFunction &MF) const override
unsigned findDeadCallerSavedReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI) const
findDeadCallerSavedReg - Return a caller-saved register that isn't live when it reaches the "return" ...
const uint32_t * getDarwinTLSCallPreservedMask() const
bool isTileRegisterClass(const TargetRegisterClass *RC) const
Return true if it is tile register class.
bool isNonRex2RegClass(const TargetRegisterClass *RC) const
const uint32_t * getCallPreservedMask(const MachineFunction &MF, CallingConv::ID) const override
Register getPtrSizedStackRegister(const MachineFunction &MF) const
bool isArgumentRegister(const MachineFunction &MF, MCRegister Reg) const override
isArgumentReg - Returns true if Reg can be used as an argument to a function.
Register getStackRegister() const
const TargetRegisterClass * getLargestLegalSuperClass(const TargetRegisterClass *RC, const MachineFunction &MF) const override
const TargetRegisterClass * getMatchingSuperRegClass(const TargetRegisterClass *A, const TargetRegisterClass *B, unsigned Idx) const override
getMatchingSuperRegClass - Return a subclass of the specified register class A so that each register ...
unsigned getRegPressureLimit(const TargetRegisterClass *RC, MachineFunction &MF) const override
const TargetRegisterClass * getCrossCopyRegClass(const TargetRegisterClass *RC) const override
getCrossCopyRegClass - Returns a legal register class to copy a register in the specified class to or...
X86RegisterInfo(const Triple &TT)
const TargetRegisterClass * constrainRegClassToNonRex2(const TargetRegisterClass *RC) const
Register getBaseRegister() const
bool getRegAllocationHints(Register VirtReg, ArrayRef< MCPhysReg > Order, SmallVectorImpl< MCPhysReg > &Hints, const MachineFunction &MF, const VirtRegMap *VRM, const LiveRegMatrix *Matrix) const override
void eliminateFrameIndex(MachineBasicBlock::iterator II, unsigned FIOperandNum, Register BaseReg, int FIOffset) const
const uint32_t * getNoPreservedMask() const override
bool isFixedRegister(const MachineFunction &MF, MCRegister PhysReg) const override
Returns true if PhysReg is a fixed register.
const TargetRegisterClass * getSubClassWithSubReg(const TargetRegisterClass *RC, unsigned Idx) const override
const MCPhysReg * getCalleeSavedRegs(const MachineFunction *MF) const override
getCalleeSavedRegs - Return a null-terminated list of all of the callee-save registers on this target...
void adjustStackMapLiveOutMask(uint32_t *Mask) const override
bool isLFI() const
bool hasSSE1() const
const X86TargetLowering * getTargetLowering() const override
bool isTarget64BitILP32() const
Is this x86_64 with the ILP32 programming model (x32 ABI)?
bool hasAVX512() const
bool hasAVX() const
bool supportSwiftError() const override
Return true if the target supports swifterror attribute.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ X86_64_SysV
The C convention as specified in the x86-64 supplement to the System V ABI, used on most non-Windows ...
@ HiPE
Used by the High-Performance Erlang Compiler (HiPE).
Definition CallingConv.h:53
@ CFGuard_Check
Special calling convention on Windows for calling the Control Guard Check ICall funtion.
Definition CallingConv.h:82
@ PreserveMost
Used for runtime calls that preserves most registers.
Definition CallingConv.h:63
@ AnyReg
OBSOLETED - Used for stack based JavaScript calls.
Definition CallingConv.h:60
@ CXX_FAST_TLS
Used for access functions.
Definition CallingConv.h:72
@ X86_INTR
x86 hardware interrupt context.
@ GHC
Used by the Glasgow Haskell Compiler (GHC).
Definition CallingConv.h:50
@ Cold
Attempts to make code in the caller as efficient as possible under the assumption that the call is no...
Definition CallingConv.h:47
@ PreserveAll
Used for runtime calls that preserves (almost) all registers.
Definition CallingConv.h:66
@ Intel_OCL_BI
Used for Intel OpenCL built-ins.
@ PreserveNone
Used for runtime calls that preserves none general registers.
Definition CallingConv.h:90
@ Win64
The C convention as implemented on Windows/x86-64 and AArch64.
@ SwiftTail
This follows the Swift calling convention in how arguments are passed but guarantees tail calls will ...
Definition CallingConv.h:87
@ GRAAL
Used by GraalVM. Two additional registers are reserved.
@ X86_RegCall
Register calling convention used for parameters transfer optimization.
void initLLVMToSEHAndCVRegMapping(MCRegisterInfo *MRI)
Define some predicates that are used for node matching.
unsigned getNonNDVariant(unsigned Opc)
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
MCRegister getX86SubSuperRegister(MCRegister Reg, unsigned Size, bool High=false)
constexpr from_range_t from_range
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
static bool isFuncletReturnInstr(const MachineInstr &MI)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
Definition Format.h:164
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58