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"
35
36using namespace llvm;
37
38#define GET_REGINFO_TARGET_DESC
39#include "X86GenRegisterInfo.inc"
40
42 : X86GenRegisterInfo((TT.isX86_64() ? X86::RIP : X86::EIP),
43 X86_MC::getDwarfRegFlavour(TT, false),
44 X86_MC::getDwarfRegFlavour(TT, true),
45 (TT.isX86_64() ? X86::RIP : X86::EIP)) {
47
48 // Cache some information.
49 Is64Bit = TT.isX86_64();
50 IsWin64 = Is64Bit && TT.isOSWindows();
51 IsUEFI64 = Is64Bit && TT.isUEFI();
52
53 // Use a callee-saved register as the base pointer. These registers must
54 // not conflict with any ABI requirements. For example, in 32-bit mode PIC
55 // requires GOT in the EBX register before function calls via PLT GOT pointer.
56 if (Is64Bit) {
57 SlotSize = 8;
58 // This matches the simplified 32-bit pointer code in the data layout
59 // computation.
60 // FIXME: Should use the data layout?
61 bool Use64BitReg = !TT.isX32();
62 StackPtr = Use64BitReg ? X86::RSP : X86::ESP;
63 FramePtr = Use64BitReg ? X86::RBP : X86::EBP;
64 BasePtr = Use64BitReg ? X86::RBX : X86::EBX;
65 } else {
66 SlotSize = 4;
67 StackPtr = X86::ESP;
68 FramePtr = X86::EBP;
69 BasePtr = X86::ESI;
70 }
71}
72
75 unsigned Idx) const {
76 // The sub_8bit sub-register index is more constrained in 32-bit mode.
77 // It behaves just like the sub_8bit_hi index.
78 if (!Is64Bit && Idx == X86::sub_8bit)
79 Idx = X86::sub_8bit_hi;
80
81 // Forward to TableGen's default version.
82 return X86GenRegisterInfo::getSubClassWithSubReg(RC, Idx);
83}
84
88 unsigned SubIdx) const {
89 // The sub_8bit sub-register index is more constrained in 32-bit mode.
90 if (!Is64Bit && SubIdx == X86::sub_8bit) {
91 A = X86GenRegisterInfo::getSubClassWithSubReg(A, X86::sub_8bit_hi);
92 if (!A)
93 return nullptr;
94 }
95 return X86GenRegisterInfo::getMatchingSuperRegClass(A, B, SubIdx);
96}
97
100 const MachineFunction &MF) const {
101 // Don't allow super-classes of GR8_NOREX. This class is only used after
102 // extracting sub_8bit_hi sub-registers. The H sub-registers cannot be copied
103 // to the full GR8 register class in 64-bit mode, so we cannot allow the
104 // reigster class inflation.
105 //
106 // The GR8_NOREX class is always used in a way that won't be constrained to a
107 // sub-class, so sub-classes like GR8_ABCD_L are allowed to expand to the
108 // full GR8 class.
109 if (RC == &X86::GR8_NOREXRegClass)
110 return RC;
111
112 // Keep using non-rex2 register class when APX feature (EGPR/NDD/NF) is not
113 // enabled for relocation.
115 if (!Subtarget.getCLOpts().enable_apx_for_relocation && isNonRex2RegClass(RC))
116 return RC;
117
118 const TargetRegisterClass *Super = RC;
119 auto I = RC->superclasses().begin();
120 auto E = RC->superclasses().end();
121 do {
122 switch (Super->getID()) {
123 case X86::FR32RegClassID:
124 case X86::FR64RegClassID:
125 // If AVX-512 isn't supported we should only inflate to these classes.
126 if (!Subtarget.hasAVX512() &&
127 getRegSizeInBits(*Super) == getRegSizeInBits(*RC))
128 return Super;
129 break;
130 case X86::VR128RegClassID:
131 case X86::VR256RegClassID:
132 // If VLX isn't supported we should only inflate to these classes.
133 if (!Subtarget.hasVLX() &&
134 getRegSizeInBits(*Super) == getRegSizeInBits(*RC))
135 return Super;
136 break;
137 case X86::VR128XRegClassID:
138 case X86::VR256XRegClassID:
139 // If VLX isn't support we shouldn't inflate to these classes.
140 if (Subtarget.hasVLX() &&
141 getRegSizeInBits(*Super) == getRegSizeInBits(*RC))
142 return Super;
143 break;
144 case X86::FR32XRegClassID:
145 case X86::FR64XRegClassID:
146 // If AVX-512 isn't support we shouldn't inflate to these classes.
147 if (Subtarget.hasAVX512() &&
148 getRegSizeInBits(*Super) == getRegSizeInBits(*RC))
149 return Super;
150 break;
151 case X86::GR8RegClassID:
152 case X86::GR16RegClassID:
153 case X86::GR32RegClassID:
154 case X86::GR64RegClassID:
155 case X86::GR8_NOREX2RegClassID:
156 case X86::GR16_NOREX2RegClassID:
157 case X86::GR32_NOREX2RegClassID:
158 case X86::GR64_NOREX2RegClassID:
159 case X86::RFP32RegClassID:
160 case X86::RFP64RegClassID:
161 case X86::RFP80RegClassID:
162 case X86::VR512_0_15RegClassID:
163 case X86::VR512RegClassID:
164 // Don't return a super-class that would shrink the spill size.
165 // That can happen with the vector and float classes.
166 if (getRegSizeInBits(*Super) == getRegSizeInBits(*RC))
167 return Super;
168 }
169 if (I != E) {
170 Super = getRegClass(*I);
171 ++I;
172 } else {
173 Super = nullptr;
174 }
175 } while (Super);
176 return RC;
177}
178
181 if (RC == &X86::CCRRegClass) {
182 if (Is64Bit)
183 return &X86::GR64RegClass;
184 else
185 return &X86::GR32RegClass;
186 }
187 return RC;
188}
189
190unsigned
192 MachineFunction &MF) const {
193 const X86FrameLowering *TFI = getFrameLowering(MF);
194
195 unsigned FPDiff = TFI->hasFP(MF) ? 1 : 0;
196 switch (RC->getID()) {
197 default:
198 return 0;
199 case X86::GR32RegClassID:
200 return 4 - FPDiff;
201 case X86::GR64RegClassID:
202 return 12 - FPDiff;
203 case X86::VR128RegClassID:
204 return Is64Bit ? 10 : 4;
205 case X86::VR64RegClassID:
206 return 4;
207 }
208}
209
210const MCPhysReg *
212 assert(MF && "MachineFunction required");
213
215 const Function &F = MF->getFunction();
216 bool HasSSE = Subtarget.hasSSE1();
217 bool HasAVX = Subtarget.hasAVX();
218 bool HasAVX512 = Subtarget.hasAVX512();
219 bool HasEGPR = Subtarget.hasEGPR();
220 bool CallsEHReturn = MF->callsEHReturn();
221
222 CallingConv::ID CC = F.getCallingConv();
223
224 // If attribute NoCallerSavedRegisters exists then we set X86_INTR calling
225 // convention because it has the CSR list.
226 if (MF->getFunction().hasFnAttribute("no_caller_saved_registers"))
228
229 // If atribute specified, override the CSRs normally specified by the
230 // calling convention and use the empty set instead.
231 if (MF->getFunction().hasFnAttribute("no_callee_saved_registers"))
232 return CSR_NoRegs_SaveList;
233
234 switch (CC) {
235 case CallingConv::GHC:
237 return CSR_NoRegs_SaveList;
239 if (HasAVX)
240 return HasEGPR ? CSR_64_APX_AllRegs_AVX_SaveList
241 : CSR_64_AllRegs_AVX_SaveList;
242 return HasEGPR ? CSR_64_APX_AllRegs_SaveList : CSR_64_AllRegs_SaveList;
244 if (IsWin64)
245 return HasEGPR ? CSR_Win64_APX_RT_MostRegs_SaveList
246 : CSR_Win64_RT_MostRegs_SaveList;
247 return CSR_64_RT_MostRegs_SaveList;
249 if (HasAVX)
250 return CSR_64_RT_AllRegs_AVX_SaveList;
251 return CSR_64_RT_AllRegs_SaveList;
253 return CSR_64_NoneRegs_SaveList;
255 if (Is64Bit)
256 return MF->getInfo<X86MachineFunctionInfo>()->isSplitCSR() ?
257 CSR_64_CXX_TLS_Darwin_PE_SaveList : CSR_64_TLS_Darwin_SaveList;
258 break;
260 if (HasAVX512 && IsWin64)
261 return HasEGPR ? CSR_Win64_APX_Intel_OCL_BI_AVX512_SaveList
262 : CSR_Win64_Intel_OCL_BI_AVX512_SaveList;
263 if (HasAVX512 && Is64Bit)
264 return CSR_64_Intel_OCL_BI_AVX512_SaveList;
265 if (HasAVX && IsWin64)
266 return HasEGPR ? CSR_Win64_APX_Intel_OCL_BI_AVX_SaveList
267 : CSR_Win64_Intel_OCL_BI_AVX_SaveList;
268 if (HasAVX && Is64Bit)
269 return CSR_64_Intel_OCL_BI_AVX_SaveList;
270 if (!HasAVX && !IsWin64 && Is64Bit)
271 return CSR_64_Intel_OCL_BI_SaveList;
272 break;
273 }
275 if (Is64Bit) {
276 if (IsWin64) {
277 if (HasSSE)
278 return HasEGPR ? CSR_Win64_APX_RegCall_SaveList
279 : CSR_Win64_RegCall_SaveList;
280 return CSR_Win64_RegCall_NoSSE_SaveList;
281 }
282 return HasSSE ? CSR_SysV64_RegCall_SaveList
283 : CSR_SysV64_RegCall_NoSSE_SaveList;
284 }
285 return HasSSE ? CSR_32_RegCall_SaveList : CSR_32_RegCall_NoSSE_SaveList;
287 assert(!Is64Bit && "CFGuard check mechanism only used on 32-bit X86");
288 return HasSSE ? CSR_Win32_CFGuard_Check_SaveList
289 : CSR_Win32_CFGuard_Check_NoSSE_SaveList;
291 if (Is64Bit)
292 return CSR_64_MostRegs_SaveList;
293 break;
295 if (HasSSE)
296 return HasEGPR ? CSR_Win64_APX_SaveList : CSR_Win64_SaveList;
297 return CSR_Win64_NoSSE_SaveList;
299 if (!Is64Bit)
300 return CSR_32_SaveList;
301 if (IsWin64)
302 return HasEGPR ? CSR_Win64_APX_SwiftTail_SaveList
303 : CSR_Win64_SwiftTail_SaveList;
304 return CSR_64_SwiftTail_SaveList;
306 if (CallsEHReturn)
307 return CSR_64EHRet_SaveList;
308 return CSR_64_SaveList;
310 if (Is64Bit) {
311 if (HasAVX512)
312 return HasEGPR ? CSR_64_APX_AllRegs_AVX512_SaveList
313 : CSR_64_AllRegs_AVX512_SaveList;
314 if (HasAVX)
315 return HasEGPR ? CSR_64_APX_AllRegs_AVX_SaveList
316 : CSR_64_AllRegs_AVX_SaveList;
317 if (HasSSE)
318 return HasEGPR ? CSR_64_APX_AllRegs_SaveList : CSR_64_AllRegs_SaveList;
319 return HasEGPR ? CSR_64_APX_AllRegs_NoSSE_SaveList
320 : CSR_64_AllRegs_NoSSE_SaveList;
321 }
322 if (HasAVX512)
323 return CSR_32_AllRegs_AVX512_SaveList;
324 if (HasAVX)
325 return CSR_32_AllRegs_AVX_SaveList;
326 if (HasSSE)
327 return CSR_32_AllRegs_SSE_SaveList;
328 return CSR_32_AllRegs_SaveList;
329 default:
330 break;
331 }
332
333 if (Is64Bit) {
334 bool IsSwiftCC = Subtarget.getTargetLowering()->supportSwiftError() &&
335 F.getAttributes().hasAttrSomewhere(Attribute::SwiftError);
336 if (IsSwiftCC) {
337 if (IsWin64)
338 return HasEGPR ? CSR_Win64_APX_SwiftError_SaveList
339 : CSR_Win64_SwiftError_SaveList;
340 return CSR_64_SwiftError_SaveList;
341 }
342
343 if (IsWin64 || IsUEFI64) {
344 if (HasSSE)
345 return HasEGPR ? CSR_Win64_APX_SaveList : CSR_Win64_SaveList;
346 return CSR_Win64_NoSSE_SaveList;
347 }
348 if (CallsEHReturn)
349 return CSR_64EHRet_SaveList;
350 return CSR_64_SaveList;
351 }
352
353 return CallsEHReturn ? CSR_32EHRet_SaveList : CSR_32_SaveList;
354}
355
356const MCPhysReg *
358 return Is64Bit ? CSR_IPRA_64_SaveList : CSR_IPRA_32_SaveList;
359}
360
362 const MachineFunction *MF) const {
363 assert(MF && "Invalid MachineFunction pointer.");
366 return CSR_64_CXX_TLS_Darwin_ViaCopy_SaveList;
367 return nullptr;
368}
369
370const uint32_t *
372 CallingConv::ID CC) const {
374 bool HasSSE = Subtarget.hasSSE1();
375 bool HasAVX = Subtarget.hasAVX();
376 bool HasAVX512 = Subtarget.hasAVX512();
377 bool HasEGPR = Subtarget.hasEGPR();
378
379 switch (CC) {
380 case CallingConv::GHC:
382 return CSR_NoRegs_RegMask;
384 // Callers keep R16-R31 clobbered: callees built without EGPR or by older
385 // compilers may not save them (see CSR_64_APX_AllRegs).
386 if (HasAVX)
387 return CSR_64_AllRegs_AVX_RegMask;
388 return CSR_64_AllRegs_RegMask;
390 if (IsWin64)
391 return HasEGPR ? CSR_Win64_APX_RT_MostRegs_RegMask
392 : CSR_Win64_RT_MostRegs_RegMask;
393 return CSR_64_RT_MostRegs_RegMask;
395 if (HasAVX)
396 return CSR_64_RT_AllRegs_AVX_RegMask;
397 return CSR_64_RT_AllRegs_RegMask;
399 return CSR_64_NoneRegs_RegMask;
401 if (Is64Bit)
402 return CSR_64_TLS_Darwin_RegMask;
403 break;
405 if (HasAVX512 && IsWin64)
406 return HasEGPR ? CSR_Win64_APX_Intel_OCL_BI_AVX512_RegMask
407 : CSR_Win64_Intel_OCL_BI_AVX512_RegMask;
408 if (HasAVX512 && Is64Bit)
409 return CSR_64_Intel_OCL_BI_AVX512_RegMask;
410 if (HasAVX && IsWin64)
411 return HasEGPR ? CSR_Win64_APX_Intel_OCL_BI_AVX_RegMask
412 : CSR_Win64_Intel_OCL_BI_AVX_RegMask;
413 if (HasAVX && Is64Bit)
414 return CSR_64_Intel_OCL_BI_AVX_RegMask;
415 if (!HasAVX && !IsWin64 && Is64Bit)
416 return CSR_64_Intel_OCL_BI_RegMask;
417 break;
418 }
420 if (Is64Bit) {
421 if (IsWin64) {
422 if (HasSSE)
423 return HasEGPR ? CSR_Win64_APX_RegCall_RegMask
424 : CSR_Win64_RegCall_RegMask;
425 return CSR_Win64_RegCall_NoSSE_RegMask;
426 }
427 return HasSSE ? CSR_SysV64_RegCall_RegMask
428 : CSR_SysV64_RegCall_NoSSE_RegMask;
429 }
430 return HasSSE ? CSR_32_RegCall_RegMask : CSR_32_RegCall_NoSSE_RegMask;
432 if (Is64Bit) {
433 if (HasSSE)
434 return HasEGPR ? CSR_Win64_APX_CFGuard_Check_RegMask
435 : CSR_Win64_CFGuard_Check_RegMask;
436 return CSR_Win64_CFGuard_Check_NoSSE_RegMask;
437 }
438 return HasSSE ? CSR_Win32_CFGuard_Check_RegMask
439 : CSR_Win32_CFGuard_Check_NoSSE_RegMask;
441 if (Is64Bit)
442 return CSR_64_MostRegs_RegMask;
443 break;
445 return HasEGPR ? CSR_Win64_APX_RegMask : CSR_Win64_RegMask;
447 if (!Is64Bit)
448 return CSR_32_RegMask;
449 if (IsWin64)
450 return HasEGPR ? CSR_Win64_APX_SwiftTail_RegMask
451 : CSR_Win64_SwiftTail_RegMask;
452 return CSR_64_SwiftTail_RegMask;
454 return CSR_64_RegMask;
456 // Callers keep R16-R31 clobbered: callees built without EGPR or by older
457 // compilers may not save them (see CSR_64_APX_AllRegs).
458 if (Is64Bit) {
459 if (HasAVX512)
460 return CSR_64_AllRegs_AVX512_RegMask;
461 if (HasAVX)
462 return CSR_64_AllRegs_AVX_RegMask;
463 if (HasSSE)
464 return CSR_64_AllRegs_RegMask;
465 return CSR_64_AllRegs_NoSSE_RegMask;
466 }
467 if (HasAVX512)
468 return CSR_32_AllRegs_AVX512_RegMask;
469 if (HasAVX)
470 return CSR_32_AllRegs_AVX_RegMask;
471 if (HasSSE)
472 return CSR_32_AllRegs_SSE_RegMask;
473 return CSR_32_AllRegs_RegMask;
474 default:
475 break;
476 }
477
478 // Unlike getCalleeSavedRegs(), we don't have MMI so we can't check
479 // callsEHReturn().
480 if (Is64Bit) {
481 const Function &F = MF.getFunction();
482 bool IsSwiftCC = Subtarget.getTargetLowering()->supportSwiftError() &&
483 F.getAttributes().hasAttrSomewhere(Attribute::SwiftError);
484 if (IsSwiftCC) {
485 if (IsWin64)
486 return HasEGPR ? CSR_Win64_APX_SwiftError_RegMask
487 : CSR_Win64_SwiftError_RegMask;
488 return CSR_64_SwiftError_RegMask;
489 }
490
491 if (IsWin64 || IsUEFI64)
492 return HasEGPR ? CSR_Win64_APX_RegMask : CSR_Win64_RegMask;
493 return CSR_64_RegMask;
494 }
495
496 return CSR_32_RegMask;
497}
498
499const uint32_t*
501 return CSR_NoRegs_RegMask;
502}
503
505 return CSR_64_TLS_Darwin_RegMask;
506}
507
509 BitVector Reserved(getNumRegs());
510 const X86FrameLowering *TFI = getFrameLowering(MF);
511
512 // Set the floating point control register as reserved.
513 Reserved.set(X86::FPCW);
514
515 // Set the floating point status register as reserved.
516 Reserved.set(X86::FPSW);
517
518 // Set the SIMD floating point control register as reserved.
519 Reserved.set(X86::MXCSR);
520
521 // Set the stack-pointer register and its aliases as reserved.
522 for (const MCPhysReg &SubReg : subregs_inclusive(X86::RSP))
523 Reserved.set(SubReg);
524
525 // Set the Shadow Stack Pointer as reserved.
526 Reserved.set(X86::SSP);
527
528 auto &ST = MF.getSubtarget<X86Subtarget>();
529 if (ST.hasUserReservedRegisters()) {
530 if (ST.is64Bit()) {
531 // Set r# as reserved register if user required.
532 for (unsigned Reg = X86::R8; Reg <= X86::R15; ++Reg)
533 if (ST.isRegisterReservedByUser(Reg))
534 for (const MCPhysReg &SubReg : subregs_inclusive(Reg))
535 Reserved.set(SubReg);
536 if (ST.hasEGPR())
537 for (unsigned Reg = X86::R16; Reg <= X86::R31; ++Reg)
538 if (ST.isRegisterReservedByUser(Reg))
539 for (const MCPhysReg &SubReg : subregs_inclusive(Reg))
540 Reserved.set(SubReg);
541 } else {
542 if (ST.isRegisterReservedByUser(X86::EDI))
543 for (const MCPhysReg &SubReg : sub_and_superregs_inclusive(X86::EDI))
544 Reserved.set(SubReg);
545 }
546 }
547
548 // Set the instruction pointer register and its aliases as reserved.
549 for (const MCPhysReg &SubReg : subregs_inclusive(X86::RIP))
550 Reserved.set(SubReg);
551
552 // Set the frame-pointer register and its aliases as reserved if needed.
553 if (TFI->hasFP(MF) || MF.framePointerIsReserved()) {
556 SMLoc(),
557 "Frame pointer clobbered by function invoke is not supported.");
558
559 for (const MCPhysReg &SubReg : subregs_inclusive(X86::RBP))
560 Reserved.set(SubReg);
561 }
562
563 // Set the base-pointer register and its aliases as reserved if needed.
564 if (hasBasePointer(MF)) {
567 "Stack realignment in presence of dynamic "
568 "allocas is not supported with "
569 "this calling convention.");
570
572 for (const MCPhysReg &SubReg : subregs_inclusive(BasePtr))
573 Reserved.set(SubReg);
574 }
575
576 // Mark the segment registers as reserved.
577 Reserved.set(X86::CS);
578 Reserved.set(X86::SS);
579 Reserved.set(X86::DS);
580 Reserved.set(X86::ES);
581 Reserved.set(X86::FS);
582 Reserved.set(X86::GS);
583
584 // Mark the floating point stack registers as reserved.
585 for (unsigned n = 0; n != 8; ++n)
586 Reserved.set(X86::ST0 + n);
587
588 // Without usable x87 (soft float or -mno-x87), reserve the allocatable FPn
589 // pseudos (FP0-FP6; FP7 is already non-allocatable) so they aren't scrubbed.
590 if (ST.useSoftFloat() || !ST.hasX87())
591 for (unsigned n = 0; n != 7; ++n)
592 Reserved.set(X86::FP0 + n);
593
594 // Reserve the registers that only exist in 64-bit mode.
595 if (!Is64Bit) {
596 // These 8-bit registers are part of the x86-64 extension even though their
597 // super-registers are old 32-bits.
598 Reserved.set(X86::SIL);
599 Reserved.set(X86::DIL);
600 Reserved.set(X86::BPL);
601 Reserved.set(X86::SPL);
602 Reserved.set(X86::SIH);
603 Reserved.set(X86::DIH);
604 Reserved.set(X86::BPH);
605 Reserved.set(X86::SPH);
606
607 for (unsigned n = 0; n != 8; ++n) {
608 // R8, R9, ...
609 for (MCRegAliasIterator AI(X86::R8 + n, this, true); AI.isValid(); ++AI)
610 Reserved.set(*AI);
611
612 // XMM8, XMM9, ...
613 for (MCRegAliasIterator AI(X86::XMM8 + n, this, true); AI.isValid(); ++AI)
614 Reserved.set(*AI);
615 }
616 }
617 if (!Is64Bit || !MF.getSubtarget<X86Subtarget>().hasAVX512()) {
618 for (unsigned n = 0; n != 16; ++n) {
619 for (MCRegAliasIterator AI(X86::XMM16 + n, this, true); AI.isValid();
620 ++AI)
621 Reserved.set(*AI);
622 }
623 }
624
625 // Reserve the extended general purpose registers.
626 if (!Is64Bit || !MF.getSubtarget<X86Subtarget>().hasEGPR())
627 Reserved.set(X86::R16, X86::R31WH + 1);
628
629 // Due to specifics of setjmp unwinding in Win64 APX ABI, the unwinder
630 // cannot restore R30/R31. Reserve them to prevent register allocation.
631 // https://learn.microsoft.com/en-us/cpp/build/x64-calling-convention#setjmplongjmp
632 if (MF.exposesReturnsTwice() && ST.isTargetWin64()) {
633 unsigned NumReservedCSRs = 0;
634 for (unsigned Reg = X86::R16; Reg <= X86::R31; ++Reg)
635 if (isCalleeSavedPhysReg(Reg, MF)) {
636 ++NumReservedCSRs;
637 for (const MCPhysReg &SubReg : subregs_inclusive(Reg))
638 Reserved.set(SubReg);
639 }
640 if (NumReservedCSRs &&
641 MF.size() > ST.getCLOpts().setjmp_csr_warning_threshold &&
644 SMLoc(), Twine(NumReservedCSRs) +
645 " callee-saved register(s) reserved due to setjmp in '" +
646 MF.getName() +
647 "'; this may impact performance in large functions");
648 }
649 }
650
652 for (MCRegAliasIterator AI(X86::R14, this, true); AI.isValid(); ++AI)
653 Reserved.set(*AI);
654 for (MCRegAliasIterator AI(X86::R15, this, true); AI.isValid(); ++AI)
655 Reserved.set(*AI);
656 }
657
658 // Reserve registers for LFI sandboxing.
659 if (MF.getSubtarget<X86Subtarget>().isLFI()) {
660 for (MCRegAliasIterator AI(X86::R11, this, true); AI.isValid(); ++AI)
661 Reserved.set(*AI);
662 for (MCRegAliasIterator AI(X86::R14, this, true); AI.isValid(); ++AI)
663 Reserved.set(*AI);
664 for (MCRegAliasIterator AI(X86::R15, this, true); AI.isValid(); ++AI)
665 Reserved.set(*AI);
666 }
667
668 assert(checkAllSuperRegsMarked(Reserved,
669 {X86::SIL, X86::DIL, X86::BPL, X86::SPL,
670 X86::SIH, X86::DIH, X86::BPH, X86::SPH}));
671 return Reserved;
672}
673
675 // All existing Intel CPUs that support AMX support AVX512 and all existing
676 // Intel CPUs that support APX support AMX. AVX512 implies AVX.
677 //
678 // We enumerate the registers in X86GenRegisterInfo.inc in this order:
679 //
680 // Registers before AVX512,
681 // AVX512 registers (X/YMM16-31, ZMM0-31, K registers)
682 // AMX registers (TMM)
683 // APX registers (R16-R31)
684 //
685 // and try to return the minimum number of registers supported by the target.
686 static_assert((X86::R15WH + 1 == X86::YMM0) && (X86::YMM15 + 1 == X86::K0) &&
687 (X86::K6_K7 + 1 == X86::TMMCFG) &&
688 (X86::TMM7 + 1 == X86::R16) &&
689 (X86::R31WH + 1 == X86::NUM_TARGET_REGS),
690 "Register number may be incorrect");
691
692 const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
693 if (ST.hasEGPR())
694 return X86::NUM_TARGET_REGS;
695 if (ST.hasAMXTILE())
696 return X86::TMM7 + 1;
697 if (ST.hasAVX512())
698 return X86::K6_K7 + 1;
699 if (ST.hasAVX())
700 return X86::YMM15 + 1;
701 return X86::R15WH + 1;
702}
703
705 MCRegister Reg) const {
706 const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
707 const TargetRegisterInfo &TRI = *ST.getRegisterInfo();
708 auto IsSubReg = [&](MCRegister RegA, MCRegister RegB) {
709 return TRI.isSuperOrSubRegisterEq(RegA, RegB);
710 };
711
712 if (!ST.is64Bit())
713 return llvm::any_of(
714 SmallVector<MCRegister>{X86::EAX, X86::ECX, X86::EDX},
715 [&](MCRegister &RegA) { return IsSubReg(RegA, Reg); }) ||
716 (ST.hasMMX() && X86::VR64RegClass.contains(Reg));
717
719
721 IsSubReg(X86::RAX, Reg))
722 return true;
723
726 ArrayRef<MCPhysReg>{X86::R11, X86::R12, X86::R13, X86::R14, X86::R15},
727 [&](MCPhysReg RegA) { return IsSubReg(RegA, Reg); }))
728 return true;
729
730 if (llvm::any_of(
731 SmallVector<MCRegister>{X86::RDX, X86::RCX, X86::R8, X86::R9},
732 [&](MCRegister &RegA) { return IsSubReg(RegA, Reg); }))
733 return true;
734
735 if (CC != CallingConv::Win64 &&
736 llvm::any_of(SmallVector<MCRegister>{X86::RDI, X86::RSI},
737 [&](MCRegister &RegA) { return IsSubReg(RegA, Reg); }))
738 return true;
739
740 if (ST.hasSSE1() &&
741 llvm::any_of(SmallVector<MCRegister>{X86::XMM0, X86::XMM1, X86::XMM2,
742 X86::XMM3, X86::XMM4, X86::XMM5,
743 X86::XMM6, X86::XMM7},
744 [&](MCRegister &RegA) { return IsSubReg(RegA, Reg); }))
745 return true;
746
747 return X86GenRegisterInfo::isArgumentRegister(MF, Reg);
748}
749
751 MCRegister PhysReg) const {
752 const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
753 const TargetRegisterInfo &TRI = *ST.getRegisterInfo();
754
755 // Stack pointer.
756 if (TRI.isSuperOrSubRegisterEq(X86::RSP, PhysReg))
757 return true;
758
759 // Don't use the frame pointer if it's being used.
760 const X86FrameLowering &TFI = *getFrameLowering(MF);
761 if (TFI.hasFP(MF) && TRI.isSuperOrSubRegisterEq(X86::RBP, PhysReg))
762 return true;
763
764 return X86GenRegisterInfo::isFixedRegister(MF, PhysReg);
765}
766
768 return RC->getID() == X86::TILERegClassID;
769}
770
772 // Check if the EFLAGS register is marked as live-out. This shouldn't happen,
773 // because the calling convention defines the EFLAGS register as NOT
774 // preserved.
775 //
776 // Unfortunatelly the EFLAGS show up as live-out after branch folding. Adding
777 // an assert to track this and clear the register afterwards to avoid
778 // unnecessary crashes during release builds.
779 assert(!(Mask[X86::EFLAGS / 32] & (1U << (X86::EFLAGS % 32))) &&
780 "EFLAGS are not live-out from a patchpoint.");
781
782 // Also clean other registers that don't need preserving (IP).
783 for (auto Reg : {X86::EFLAGS, X86::RIP, X86::EIP, X86::IP})
784 Mask[Reg / 32] &= ~(1U << (Reg % 32));
785}
786
787//===----------------------------------------------------------------------===//
788// Stack Frame Processing methods
789//===----------------------------------------------------------------------===//
790
791static bool CantUseSP(const MachineFrameInfo &MFI) {
792 return MFI.hasVarSizedObjects() || MFI.hasOpaqueSPAdjustment();
793}
794
797 // We have a virtual register to reference argument, and don't need base
798 // pointer.
799 if (X86FI->getStackPtrSaveMI() != nullptr)
800 return false;
801
802 if (X86FI->hasPreallocatedCall())
803 return true;
804
805 const MachineFrameInfo &MFI = MF.getFrameInfo();
806
807 if (!MF.getSubtarget<X86Subtarget>().getCLOpts().use_base_pointer)
808 return false;
809
810 // When we need stack realignment, we can't address the stack from the frame
811 // pointer. When we have dynamic allocas or stack-adjusting inline asm, we
812 // can't address variables from the stack pointer. MS inline asm can
813 // reference locals while also adjusting the stack pointer. When we can't
814 // use both the SP and the FP, we need a separate base pointer register.
815 bool CantUseFP = hasStackRealignment(MF);
816 return CantUseFP && CantUseSP(MFI);
817}
818
821 return false;
822
823 const MachineFrameInfo &MFI = MF.getFrameInfo();
824 const MachineRegisterInfo *MRI = &MF.getRegInfo();
825
826 // Stack realignment requires a frame pointer. If we already started
827 // register allocation with frame pointer elimination, it is too late now.
828 if (!MRI->canReserveReg(FramePtr))
829 return false;
830
831 // If a base pointer is necessary. Check that it isn't too late to reserve
832 // it.
833 if (CantUseSP(MFI))
834 return MRI->canReserveReg(BasePtr);
835 return true;
836}
837
840 return true;
841
842 return !Is64Bit && MF.getFunction().getCallingConv() == CallingConv::X86_INTR;
843}
844
845// tryOptimizeLEAtoMOV - helper function that tries to replace a LEA instruction
846// of the form 'lea (%esp), %ebx' --> 'mov %esp, %ebx'.
847// TODO: In this case we should be really trying first to entirely eliminate
848// this instruction which is a plain copy.
850 MachineInstr &MI = *II;
851 unsigned Opc = II->getOpcode();
852 // Check if this is a LEA of the form 'lea (%esp), %ebx'
853 if ((Opc != X86::LEA32r && Opc != X86::LEA64r && Opc != X86::LEA64_32r) ||
854 MI.getOperand(2).getImm() != 1 ||
855 MI.getOperand(3).getReg() != X86::NoRegister ||
856 MI.getOperand(4).getImm() != 0 ||
857 MI.getOperand(5).getReg() != X86::NoRegister)
858 return false;
859 Register BasePtr = MI.getOperand(1).getReg();
860 // In X32 mode, ensure the base-pointer is a 32-bit operand, so the LEA will
861 // be replaced with a 32-bit operand MOV which will zero extend the upper
862 // 32-bits of the super register.
863 if (Opc == X86::LEA64_32r)
864 BasePtr = getX86SubSuperRegister(BasePtr, 32);
865 Register NewDestReg = MI.getOperand(0).getReg();
866 const X86InstrInfo *TII =
867 MI.getParent()->getParent()->getSubtarget<X86Subtarget>().getInstrInfo();
868 TII->copyPhysReg(*MI.getParent(), II, MI.getDebugLoc(), NewDestReg, BasePtr,
869 MI.getOperand(1).isKill());
870 MI.eraseFromParent();
871 return true;
872}
873
875 switch (MI.getOpcode()) {
876 case X86::CATCHRET:
877 case X86::CLEANUPRET:
878 return true;
879 default:
880 return false;
881 }
882 llvm_unreachable("impossible");
883}
884
886 unsigned FIOperandNum,
887 Register BaseReg,
888 int FIOffset) const {
889 MachineInstr &MI = *II;
890 unsigned Opc = MI.getOpcode();
891 if (Opc == TargetOpcode::LOCAL_ESCAPE) {
892 MachineOperand &FI = MI.getOperand(FIOperandNum);
893 FI.ChangeToImmediate(FIOffset);
894 return;
895 }
896
897 MI.getOperand(FIOperandNum).ChangeToRegister(BaseReg, false);
898
899 // The frame index format for stackmaps and patchpoints is different from the
900 // X86 format. It only has a FI and an offset.
901 if (Opc == TargetOpcode::STACKMAP || Opc == TargetOpcode::PATCHPOINT) {
902 assert(BasePtr == FramePtr && "Expected the FP as base register");
903 int64_t Offset = MI.getOperand(FIOperandNum + 1).getImm() + FIOffset;
904 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
905 return;
906 }
907
908 if (MI.getOperand(FIOperandNum + 3).isImm()) {
909 // Offset is a 32-bit integer.
910 int Imm = (int)(MI.getOperand(FIOperandNum + 3).getImm());
911 int Offset = FIOffset + Imm;
912 assert((!Is64Bit || isInt<32>((long long)FIOffset + Imm)) &&
913 "Requesting 64-bit offset in 32-bit immediate!");
914 if (Offset != 0)
915 MI.getOperand(FIOperandNum + 3).ChangeToImmediate(Offset);
916 } else {
917 // Offset is symbolic. This is extremely rare.
918 uint64_t Offset =
919 FIOffset + (uint64_t)MI.getOperand(FIOperandNum + 3).getOffset();
920 MI.getOperand(FIOperandNum + 3).setOffset(Offset);
921 }
922}
923
924bool
926 int SPAdj, unsigned FIOperandNum,
927 RegScavenger *RS) const {
928 MachineInstr &MI = *II;
929 MachineBasicBlock &MBB = *MI.getParent();
930 MachineFunction &MF = *MBB.getParent();
931 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
932 bool IsEHFuncletEpilogue = MBBI == MBB.end() ? false
934 const X86FrameLowering *TFI = getFrameLowering(MF);
935 int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
936
937 // Determine base register and offset.
938 int64_t FIOffset;
939 Register BasePtr;
940 if (MI.isReturn()) {
941 assert((!hasStackRealignment(MF) ||
942 MF.getFrameInfo().isFixedObjectIndex(FrameIndex)) &&
943 "Return instruction can only reference SP relative frame objects");
944 FIOffset =
945 TFI->getFrameIndexReferenceSP(MF, FrameIndex, BasePtr, 0).getFixed();
946 } else if (TFI->Is64Bit && (MBB.isEHFuncletEntry() || IsEHFuncletEpilogue)) {
947 FIOffset = TFI->getWin64EHFrameIndexRef(MF, FrameIndex, BasePtr);
948 } else {
949 FIOffset = TFI->getFrameIndexReference(MF, FrameIndex, BasePtr).getFixed();
950 }
951
952 // LOCAL_ESCAPE uses a single offset, with no register. It only works in the
953 // simple FP case, and doesn't work with stack realignment. On 32-bit, the
954 // offset is from the traditional base pointer location. On 64-bit, the
955 // offset is from the SP at the end of the prologue, not the FP location. This
956 // matches the behavior of llvm.frameaddress.
957 unsigned Opc = MI.getOpcode();
958 if (Opc == TargetOpcode::LOCAL_ESCAPE) {
959 MachineOperand &FI = MI.getOperand(FIOperandNum);
960 FI.ChangeToImmediate(FIOffset);
961 return false;
962 }
963
964 // For LEA64_32r when BasePtr is 32-bits (X32) we can use full-size 64-bit
965 // register as source operand, semantic is the same and destination is
966 // 32-bits. It saves one byte per lea in code since 0x67 prefix is avoided.
967 // Don't change BasePtr since it is used later for stack adjustment.
968 Register MachineBasePtr = BasePtr;
969 if (Opc == X86::LEA64_32r && X86::GR32RegClass.contains(BasePtr))
970 MachineBasePtr = getX86SubSuperRegister(BasePtr, 64);
971
972 // This must be part of a four operand memory reference. Replace the
973 // FrameIndex with base register. Add an offset to the offset.
974 MI.getOperand(FIOperandNum).ChangeToRegister(MachineBasePtr, false);
975
976 if (BasePtr == StackPtr)
977 FIOffset += SPAdj;
978
979 // The frame index format for stackmaps and patchpoints is different from the
980 // X86 format. It only has a FI and an offset.
981 if (Opc == TargetOpcode::STACKMAP || Opc == TargetOpcode::PATCHPOINT) {
982 assert(BasePtr == FramePtr && "Expected the FP as base register");
983 int64_t Offset = MI.getOperand(FIOperandNum + 1).getImm() + FIOffset;
984 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
985 return false;
986 }
987
988 if (MI.getOperand(FIOperandNum+3).isImm()) {
989 const X86InstrInfo *TII = MF.getSubtarget<X86Subtarget>().getInstrInfo();
990 const DebugLoc &DL = MI.getDebugLoc();
991 int64_t Imm = MI.getOperand(FIOperandNum + 3).getImm();
992 int64_t Offset = FIOffset + Imm;
993 bool FitsIn32Bits = isInt<32>(Offset);
994 // If the offset will not fit in a 32-bit displacement, then for 64-bit
995 // targets, scavenge a register to hold it. Otherwise...
996 if (Is64Bit && !FitsIn32Bits) {
997 assert(RS && "RegisterScavenger was NULL");
998
999 RS->enterBasicBlockEnd(MBB);
1000 RS->backward(std::next(II));
1001
1002 Register ScratchReg = RS->scavengeRegisterBackwards(
1003 X86::GR64RegClass, II, /*RestoreAfter=*/false, /*SPAdj=*/0,
1004 /*AllowSpill=*/true);
1005 assert(ScratchReg != 0 && "scratch reg was 0");
1006 RS->setRegUsed(ScratchReg);
1007
1008 BuildMI(MBB, II, DL, TII->get(X86::MOV64ri), ScratchReg).addImm(Offset);
1009
1010 MI.getOperand(FIOperandNum + 3).setImm(0);
1011 if (MI.getOperand(FIOperandNum + 2).getReg() == X86::NoRegister) {
1012 MI.getOperand(FIOperandNum + 2).setReg(ScratchReg);
1013 } else {
1014 // The index register slot is already in use, fold the offset into
1015 // the base register instead. LEA does not clobber EFLAGS.
1016 BuildMI(MBB, II, DL, TII->get(X86::LEA64r), ScratchReg)
1017 .addReg(MachineBasePtr)
1018 .addImm(1)
1019 .addReg(ScratchReg)
1020 .addImm(0)
1021 .addReg(X86::NoRegister);
1022 MI.getOperand(FIOperandNum).setReg(ScratchReg);
1023 }
1024
1025 return false;
1026 }
1027
1028 // ... for 32-bit targets, this is a bug!
1029 if (!Is64Bit && !FitsIn32Bits) {
1030 MI.emitGenericError("64-bit offset calculated but target is 32-bit");
1031 // Trap so that the instruction verification pass does not fail if run.
1032 BuildMI(MBB, MBBI, DL, TII->get(X86::TRAP));
1033 return false;
1034 }
1035
1036 if (Offset != 0 || !tryOptimizeLEAtoMOV(II))
1037 MI.getOperand(FIOperandNum + 3).ChangeToImmediate(Offset);
1038 } else {
1039 // Offset is symbolic. This is extremely rare.
1040 uint64_t Offset = FIOffset +
1041 (uint64_t)MI.getOperand(FIOperandNum+3).getOffset();
1042 MI.getOperand(FIOperandNum + 3).setOffset(Offset);
1043 }
1044 return false;
1045}
1046
1049 const MachineFunction *MF = MBB.getParent();
1050 const MachineRegisterInfo &MRI = MF->getRegInfo();
1051 if (MF->callsEHReturn())
1052 return 0;
1053
1054 if (MBBI == MBB.end())
1055 return 0;
1056
1057 switch (MBBI->getOpcode()) {
1058 default:
1059 return 0;
1060 case TargetOpcode::PATCHABLE_RET:
1061 case X86::RET:
1062 case X86::RET32:
1063 case X86::RET64:
1064 case X86::RETI32:
1065 case X86::RETI64:
1066 case X86::TCRETURNdi:
1067 case X86::TCRETURNri:
1068 case X86::TCRETURN_WIN64ri:
1069 case X86::TCRETURN_HIPE32ri:
1070 case X86::TCRETURNmi:
1071 case X86::TCRETURNdi64:
1072 case X86::TCRETURNri64:
1073 case X86::TCRETURNri64_ImpCall:
1074 case X86::TCRETURNmi64:
1075 case X86::TCRETURN_WINmi64:
1076 case X86::EH_RETURN:
1077 case X86::EH_RETURN64: {
1078 LiveRegUnits LRU(*this);
1079 LRU.addLiveOuts(MBB);
1080 LRU.stepBackward(*MBBI);
1081
1082 const TargetRegisterClass &RC =
1083 Is64Bit ? X86::GR64_NOSPRegClass : X86::GR32_NOSPRegClass;
1084 for (MCRegister Reg : RC) {
1085 if (LRU.available(Reg) && !MRI.isReserved(Reg))
1086 return Reg;
1087 }
1088 }
1089 }
1090
1091 return 0;
1092}
1093
1095 const X86FrameLowering *TFI = getFrameLowering(MF);
1096 return TFI->hasFP(MF) ? FramePtr : StackPtr;
1097}
1098
1102 Register FrameReg = getFrameRegister(MF);
1103 if (Subtarget.isTarget64BitILP32())
1104 FrameReg = getX86SubSuperRegister(FrameReg, 32);
1105 return FrameReg;
1106}
1107
1111 Register StackReg = getStackRegister();
1112 if (Subtarget.isTarget64BitILP32())
1113 StackReg = getX86SubSuperRegister(StackReg, 32);
1114 return StackReg;
1115}
1116
1118 const MachineRegisterInfo *MRI) {
1119 if (VRM->hasShape(VirtReg))
1120 return VRM->getShape(VirtReg);
1121
1122 const MachineOperand &Def = *MRI->def_begin(VirtReg);
1123 MachineInstr *MI = const_cast<MachineInstr *>(Def.getParent());
1124 unsigned OpCode = MI->getOpcode();
1125 switch (OpCode) {
1126 default:
1127 llvm_unreachable("Unexpected machine instruction on tile register!");
1128 break;
1129 case X86::COPY: {
1130 Register SrcReg = MI->getOperand(1).getReg();
1131 ShapeT Shape = getTileShape(SrcReg, VRM, MRI);
1132 VRM->assignVirt2Shape(VirtReg, Shape);
1133 return Shape;
1134 }
1135 // We only collect the tile shape that is defined.
1136 case X86::PTILELOADDV:
1137 case X86::PTILELOADDT1V:
1138 case X86::PTDPBSSDV:
1139 case X86::PTDPBSUDV:
1140 case X86::PTDPBUSDV:
1141 case X86::PTDPBUUDV:
1142 case X86::PTILEZEROV:
1143 case X86::PTDPBF16PSV:
1144 case X86::PTDPFP16PSV:
1145 case X86::PTCMMIMFP16PSV:
1146 case X86::PTCMMRLFP16PSV:
1147 case X86::PTILELOADDRSV:
1148 case X86::PTILELOADDRST1V:
1149 case X86::PTDPBF8PSV:
1150 case X86::PTDPBHF8PSV:
1151 case X86::PTDPHBF8PSV:
1152 case X86::PTDPHF8PSV: {
1153 MachineOperand &MO1 = MI->getOperand(1);
1154 MachineOperand &MO2 = MI->getOperand(2);
1155 ShapeT Shape(&MO1, &MO2, MRI);
1156 VRM->assignVirt2Shape(VirtReg, Shape);
1157 return Shape;
1158 }
1159 }
1160}
1161
1163 Register VirtReg, ArrayRef<MCPhysReg> Order,
1165 const VirtRegMap *VRM, const LiveRegMatrix *Matrix) const {
1166 const MachineRegisterInfo *MRI = &MF.getRegInfo();
1167 const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
1168 bool BaseImplRetVal = TargetRegisterInfo::getRegAllocationHints(
1169 VirtReg, Order, Hints, MF, VRM, Matrix);
1170 const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
1171 const TargetRegisterInfo &TRI = *ST.getRegisterInfo();
1172
1173 unsigned ID = RC.getID();
1174
1175 if (!VRM)
1176 return BaseImplRetVal;
1177
1178 if (ID != X86::TILERegClassID) {
1179 if (ST.getCLOpts().disable_regalloc_hints_for_ndd || !ST.hasNDD() ||
1180 !TRI.isGeneralPurposeRegisterClass(&RC))
1181 return BaseImplRetVal;
1182
1183 // Add any two address hints after any copy hints.
1184 SmallSet<unsigned, 4> TwoAddrHints;
1185
1186 auto TryAddNDDHint = [&](const MachineOperand &MO) {
1187 Register Reg = MO.getReg();
1188 Register PhysReg = Reg.isPhysical() ? Reg : Register(VRM->getPhys(Reg));
1189 if (PhysReg && !MRI->isReserved(PhysReg) && !Hints.contains(PhysReg))
1190 TwoAddrHints.insert(PhysReg);
1191 };
1192
1193 // NDD instructions is compressible when Op0 is allocated to the same
1194 // physic register as Op1 (or Op2 if it's commutable).
1195 for (auto &MO : MRI->reg_nodbg_operands(VirtReg)) {
1196 const MachineInstr &MI = *MO.getParent();
1197 if (!X86::getNonNDVariant(MI.getOpcode()))
1198 continue;
1199 unsigned OpIdx = MI.getOperandNo(&MO);
1200 if (OpIdx == 0) {
1201 assert(MI.getOperand(1).isReg());
1202 TryAddNDDHint(MI.getOperand(1));
1203 if (MI.isCommutable()) {
1204 assert(MI.getOperand(2).isReg());
1205 TryAddNDDHint(MI.getOperand(2));
1206 }
1207 } else if (OpIdx == 1) {
1208 TryAddNDDHint(MI.getOperand(0));
1209 } else if (MI.isCommutable() && OpIdx == 2) {
1210 TryAddNDDHint(MI.getOperand(0));
1211 }
1212 }
1213
1214 for (MCPhysReg OrderReg : Order)
1215 if (TwoAddrHints.count(OrderReg))
1216 Hints.insert(OrderReg);
1217
1218 return BaseImplRetVal;
1219 }
1220
1221 ShapeT VirtShape = getTileShape(VirtReg, const_cast<VirtRegMap *>(VRM), MRI);
1222 auto AddHint = [&](MCPhysReg PhysReg) {
1223 Register VReg = Matrix->getOneVReg(PhysReg);
1224 if (VReg == MCRegister::NoRegister) { // Not allocated yet
1225 Hints.insert(PhysReg);
1226 return;
1227 }
1228 ShapeT PhysShape = getTileShape(VReg, const_cast<VirtRegMap *>(VRM), MRI);
1229 if (PhysShape == VirtShape)
1230 Hints.insert(PhysReg);
1231 };
1232
1233 SmallSetVector<MCPhysReg, 16> CopyHints(Hints);
1234 Hints.clear();
1235 for (auto Hint : CopyHints) {
1236 if (RC.contains(Hint) && !MRI->isReserved(Hint))
1237 AddHint(Hint);
1238 }
1239 for (MCPhysReg PhysReg : Order) {
1240 if (!CopyHints.contains(PhysReg) && RC.contains(PhysReg) &&
1241 !MRI->isReserved(PhysReg))
1242 AddHint(PhysReg);
1243 }
1244
1245#define DEBUG_TYPE "tile-hint"
1246 LLVM_DEBUG({
1247 dbgs() << "Hints for virtual register " << format_hex(VirtReg, 8) << "\n";
1248 for (auto Hint : Hints) {
1249 dbgs() << "tmm" << Hint << ",";
1250 }
1251 dbgs() << "\n";
1252 });
1253#undef DEBUG_TYPE
1254
1255 return true;
1256}
1257
1259 const TargetRegisterClass *RC) const {
1260 switch (RC->getID()) {
1261 default:
1262 return RC;
1263 case X86::GR8RegClassID:
1264 return &X86::GR8_NOREX2RegClass;
1265 case X86::GR16RegClassID:
1266 return &X86::GR16_NOREX2RegClass;
1267 case X86::GR32RegClassID:
1268 return &X86::GR32_NOREX2RegClass;
1269 case X86::GR64RegClassID:
1270 return &X86::GR64_NOREX2RegClass;
1271 case X86::GR32_NOSPRegClassID:
1272 return &X86::GR32_NOREX2_NOSPRegClass;
1273 case X86::GR64_NOSPRegClassID:
1274 return &X86::GR64_NOREX2_NOSPRegClass;
1275 }
1276}
1277
1279 switch (RC->getID()) {
1280 default:
1281 return false;
1282 case X86::GR8_NOREX2RegClassID:
1283 case X86::GR16_NOREX2RegClassID:
1284 case X86::GR32_NOREX2RegClassID:
1285 case X86::GR64_NOREX2RegClassID:
1286 case X86::GR32_NOREX2_NOSPRegClassID:
1287 case X86::GR64_NOREX2_NOSPRegClassID:
1288 case X86::GR64_with_sub_16bit_in_GR16_NOREX2RegClassID:
1289 return true;
1290 }
1291}
1292
1294 // PPX push/pop pairs don't access memory, but they still execute, so use a
1295 // non-zero cost to let RAGreedy keep CSR saves off hot paths.
1296 const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
1297 if (ST.is64Bit() && ST.hasPPX())
1298 return 1;
1299
1300 // push + pop.
1301 return 2;
1302}
const AArch64Subtarget & Subtarget
static const TargetRegisterClass * getRegClass(const MachineInstr &MI, Register Reg)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const AArch64InstrInfo & TII
uint64_t 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")
IRTranslator LLVM IR MI
Live Register Matrix
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
RISCVCC::CondCode CC
MachineRegisterInfo & MRI
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:479
This file defines the SmallSet class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
static bool tryOptimizeLEAtoMOV(MachineBasicBlock::iterator II)
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:127
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:731
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...
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,...
bool reservedRegsFrozen() const
reservedRegsFrozen - Returns true after freezeReservedRegs() was called to ensure the set of reserved...
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
bool contains(const_arg_type key) const
Check if the SetVector contains the given key.
Definition SetVector.h:258
void clear()
Completely clear the SetVector.
Definition SetVector.h:273
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:157
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:346
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 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:40
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, SmallSetVector< MCPhysReg, 16 > &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:100
ShapeT getShape(Register virtReg) const
Definition VirtRegMap.h:104
MCRegister getPhys(Register virtReg) const
returns the physical register mapped to the specified virtual register
Definition VirtRegMap.h:89
void assignVirt2Shape(Register virtReg, ShapeT shape)
Definition VirtRegMap.h:109
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 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
unsigned getCSRFirstUseCost(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
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.
bool getRegAllocationHints(Register VirtReg, ArrayRef< MCPhysReg > Order, SmallSetVector< MCPhysReg, 16 > &Hints, const MachineFunction &MF, const VirtRegMap *VRM, const LiveRegMatrix *Matrix) const override
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 hasAVX512() const
const X86Options & getCLOpts() const
#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)
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)
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:1762
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
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58