1 //===-- X86RegisterInfo.cpp - X86 Register Information --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the X86 implementation of the TargetRegisterInfo class.
11 // This file is responsible for the frame pointer elimination optimization
14 //===----------------------------------------------------------------------===//
16 #include "X86RegisterInfo.h"
18 #include "X86InstrBuilder.h"
19 #include "X86MachineFunctionInfo.h"
20 #include "X86Subtarget.h"
21 #include "X86TargetMachine.h"
22 #include "llvm/Constants.h"
23 #include "llvm/Function.h"
24 #include "llvm/Type.h"
25 #include "llvm/CodeGen/ValueTypes.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineFunctionPass.h"
29 #include "llvm/CodeGen/MachineFrameInfo.h"
30 #include "llvm/CodeGen/MachineModuleInfo.h"
31 #include "llvm/CodeGen/MachineRegisterInfo.h"
32 #include "llvm/MC/MCAsmInfo.h"
33 #include "llvm/Target/TargetFrameLowering.h"
34 #include "llvm/Target/TargetInstrInfo.h"
35 #include "llvm/Target/TargetMachine.h"
36 #include "llvm/Target/TargetOptions.h"
37 #include "llvm/ADT/BitVector.h"
38 #include "llvm/ADT/STLExtras.h"
39 #include "llvm/Support/ErrorHandling.h"
40 #include "llvm/Support/CommandLine.h"
42 #define GET_REGINFO_TARGET_DESC
43 #include "X86GenRegisterInfo.inc"
48 ForceStackAlign("force-align-stack",
49 cl::desc("Force align the stack to the minimum alignment"
50 " needed for the function."),
51 cl::init(false), cl::Hidden);
54 EnableBasePointer("x86-use-base-pointer", cl::Hidden, cl::init(true),
55 cl::desc("Enable use of a base pointer for complex stack frames"));
57 X86RegisterInfo::X86RegisterInfo(X86TargetMachine &tm,
58 const TargetInstrInfo &tii)
59 : X86GenRegisterInfo(tm.getSubtarget<X86Subtarget>().is64Bit()
60 ? X86::RIP : X86::EIP,
61 X86_MC::getDwarfRegFlavour(tm.getTargetTriple(), false),
62 X86_MC::getDwarfRegFlavour(tm.getTargetTriple(), true)),
64 X86_MC::InitLLVM2SEHRegisterMapping(this);
66 // Cache some information.
67 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
68 Is64Bit = Subtarget->is64Bit();
69 IsWin64 = Subtarget->isTargetWin64();
80 // Use a callee-saved register as the base pointer. These registers must
81 // not conflict with any ABI requirements. For example, in 32-bit mode PIC
82 // requires GOT in the EBX register before function calls via PLT GOT pointer.
83 BasePtr = Is64Bit ? X86::RBX : X86::ESI;
86 /// getCompactUnwindRegNum - This function maps the register to the number for
87 /// compact unwind encoding. Return -1 if the register isn't valid.
88 int X86RegisterInfo::getCompactUnwindRegNum(unsigned RegNum, bool isEH) const {
89 switch (getLLVMRegNum(RegNum, isEH)) {
90 case X86::EBX: case X86::RBX: return 1;
91 case X86::ECX: case X86::R12: return 2;
92 case X86::EDX: case X86::R13: return 3;
93 case X86::EDI: case X86::R14: return 4;
94 case X86::ESI: case X86::R15: return 5;
95 case X86::EBP: case X86::RBP: return 6;
102 X86RegisterInfo::trackLivenessAfterRegAlloc(const MachineFunction &MF) const {
103 // Only enable when post-RA scheduling is enabled and this is needed.
104 return TM.getSubtargetImpl()->postRAScheduler();
108 X86RegisterInfo::getSEHRegNum(unsigned i) const {
109 return getEncodingValue(i);
112 const TargetRegisterClass *
113 X86RegisterInfo::getSubClassWithSubReg(const TargetRegisterClass *RC,
114 unsigned Idx) const {
115 // The sub_8bit sub-register index is more constrained in 32-bit mode.
116 // It behaves just like the sub_8bit_hi index.
117 if (!Is64Bit && Idx == X86::sub_8bit)
118 Idx = X86::sub_8bit_hi;
120 // Forward to TableGen's default version.
121 return X86GenRegisterInfo::getSubClassWithSubReg(RC, Idx);
124 const TargetRegisterClass *
125 X86RegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A,
126 const TargetRegisterClass *B,
127 unsigned SubIdx) const {
128 // The sub_8bit sub-register index is more constrained in 32-bit mode.
129 if (!Is64Bit && SubIdx == X86::sub_8bit) {
130 A = X86GenRegisterInfo::getSubClassWithSubReg(A, X86::sub_8bit_hi);
134 return X86GenRegisterInfo::getMatchingSuperRegClass(A, B, SubIdx);
137 const TargetRegisterClass*
138 X86RegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC) const{
139 // Don't allow super-classes of GR8_NOREX. This class is only used after
140 // extrating sub_8bit_hi sub-registers. The H sub-registers cannot be copied
141 // to the full GR8 register class in 64-bit mode, so we cannot allow the
142 // reigster class inflation.
144 // The GR8_NOREX class is always used in a way that won't be constrained to a
145 // sub-class, so sub-classes like GR8_ABCD_L are allowed to expand to the
147 if (RC == &X86::GR8_NOREXRegClass)
150 const TargetRegisterClass *Super = RC;
151 TargetRegisterClass::sc_iterator I = RC->getSuperClasses();
153 switch (Super->getID()) {
154 case X86::GR8RegClassID:
155 case X86::GR16RegClassID:
156 case X86::GR32RegClassID:
157 case X86::GR64RegClassID:
158 case X86::FR32RegClassID:
159 case X86::FR64RegClassID:
160 case X86::RFP32RegClassID:
161 case X86::RFP64RegClassID:
162 case X86::RFP80RegClassID:
163 case X86::VR128RegClassID:
164 case X86::VR256RegClassID:
165 // Don't return a super-class that would shrink the spill size.
166 // That can happen with the vector and float classes.
167 if (Super->getSize() == RC->getSize())
175 const TargetRegisterClass *
176 X86RegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)
179 default: llvm_unreachable("Unexpected Kind in getPointerRegClass!");
180 case 0: // Normal GPRs.
181 if (TM.getSubtarget<X86Subtarget>().is64Bit())
182 return &X86::GR64RegClass;
183 return &X86::GR32RegClass;
184 case 1: // Normal GPRs except the stack pointer (for encoding reasons).
185 if (TM.getSubtarget<X86Subtarget>().is64Bit())
186 return &X86::GR64_NOSPRegClass;
187 return &X86::GR32_NOSPRegClass;
188 case 2: // Available for tailcall (not callee-saved GPRs).
189 if (TM.getSubtarget<X86Subtarget>().isTargetWin64())
190 return &X86::GR64_TCW64RegClass;
191 if (TM.getSubtarget<X86Subtarget>().is64Bit())
192 return &X86::GR64_TCRegClass;
193 return &X86::GR32_TCRegClass;
197 const TargetRegisterClass *
198 X86RegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
199 if (RC == &X86::CCRRegClass) {
201 return &X86::GR64RegClass;
203 return &X86::GR32RegClass;
209 X86RegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
210 MachineFunction &MF) const {
211 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
213 unsigned FPDiff = TFI->hasFP(MF) ? 1 : 0;
214 switch (RC->getID()) {
217 case X86::GR32RegClassID:
219 case X86::GR64RegClassID:
221 case X86::VR128RegClassID:
222 return TM.getSubtarget<X86Subtarget>().is64Bit() ? 10 : 4;
223 case X86::VR64RegClassID:
229 X86RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
230 bool callsEHReturn = false;
231 bool ghcCall = false;
232 bool oclBiCall = false;
233 bool HasAVX = TM.getSubtarget<X86Subtarget>().hasAVX();
236 callsEHReturn = MF->getMMI().callsEHReturn();
237 const Function *F = MF->getFunction();
238 ghcCall = (F ? F->getCallingConv() == CallingConv::GHC : false);
239 oclBiCall = (F ? F->getCallingConv() == CallingConv::Intel_OCL_BI : false);
243 return CSR_NoRegs_SaveList;
245 if (HasAVX && IsWin64)
246 return CSR_Win64_Intel_OCL_BI_AVX_SaveList;
247 if (HasAVX && Is64Bit)
248 return CSR_64_Intel_OCL_BI_AVX_SaveList;
249 if (!HasAVX && !IsWin64 && Is64Bit)
250 return CSR_64_Intel_OCL_BI_SaveList;
254 return CSR_Win64_SaveList;
256 return CSR_64EHRet_SaveList;
257 return CSR_64_SaveList;
260 return CSR_32EHRet_SaveList;
261 return CSR_32_SaveList;
265 X86RegisterInfo::getCallPreservedMask(CallingConv::ID CC) const {
266 bool HasAVX = TM.getSubtarget<X86Subtarget>().hasAVX();
268 if (CC == CallingConv::Intel_OCL_BI) {
269 if (IsWin64 && HasAVX)
270 return CSR_Win64_Intel_OCL_BI_AVX_RegMask;
271 if (Is64Bit && HasAVX)
272 return CSR_64_Intel_OCL_BI_AVX_RegMask;
273 if (!HasAVX && !IsWin64 && Is64Bit)
274 return CSR_64_Intel_OCL_BI_RegMask;
276 if (CC == CallingConv::GHC)
277 return CSR_NoRegs_RegMask;
279 return CSR_32_RegMask;
281 return CSR_Win64_RegMask;
282 return CSR_64_RegMask;
286 X86RegisterInfo::getNoPreservedMask() const {
287 return CSR_NoRegs_RegMask;
290 BitVector X86RegisterInfo::getReservedRegs(const MachineFunction &MF) const {
291 BitVector Reserved(getNumRegs());
292 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
294 // Set the stack-pointer register and its aliases as reserved.
295 Reserved.set(X86::RSP);
296 for (MCSubRegIterator I(X86::RSP, this); I.isValid(); ++I)
299 // Set the instruction pointer register and its aliases as reserved.
300 Reserved.set(X86::RIP);
301 for (MCSubRegIterator I(X86::RIP, this); I.isValid(); ++I)
304 // Set the frame-pointer register and its aliases as reserved if needed.
305 if (TFI->hasFP(MF)) {
306 Reserved.set(X86::RBP);
307 for (MCSubRegIterator I(X86::RBP, this); I.isValid(); ++I)
311 // Set the base-pointer register and its aliases as reserved if needed.
312 if (hasBasePointer(MF)) {
313 CallingConv::ID CC = MF.getFunction()->getCallingConv();
314 const uint32_t* RegMask = getCallPreservedMask(CC);
315 if (MachineOperand::clobbersPhysReg(RegMask, getBaseRegister()))
317 "Stack realignment in presence of dynamic allocas is not supported with"
318 "this calling convention.");
320 Reserved.set(getBaseRegister());
321 for (MCSubRegIterator I(getBaseRegister(), this); I.isValid(); ++I)
325 // Mark the segment registers as reserved.
326 Reserved.set(X86::CS);
327 Reserved.set(X86::SS);
328 Reserved.set(X86::DS);
329 Reserved.set(X86::ES);
330 Reserved.set(X86::FS);
331 Reserved.set(X86::GS);
333 // Mark the floating point stack registers as reserved.
334 Reserved.set(X86::ST0);
335 Reserved.set(X86::ST1);
336 Reserved.set(X86::ST2);
337 Reserved.set(X86::ST3);
338 Reserved.set(X86::ST4);
339 Reserved.set(X86::ST5);
340 Reserved.set(X86::ST6);
341 Reserved.set(X86::ST7);
343 // Reserve the registers that only exist in 64-bit mode.
345 // These 8-bit registers are part of the x86-64 extension even though their
346 // super-registers are old 32-bits.
347 Reserved.set(X86::SIL);
348 Reserved.set(X86::DIL);
349 Reserved.set(X86::BPL);
350 Reserved.set(X86::SPL);
352 for (unsigned n = 0; n != 8; ++n) {
354 static const uint16_t GPR64[] = {
355 X86::R8, X86::R9, X86::R10, X86::R11,
356 X86::R12, X86::R13, X86::R14, X86::R15
358 for (MCRegAliasIterator AI(GPR64[n], this, true); AI.isValid(); ++AI)
362 assert(X86::XMM15 == X86::XMM8+7);
363 for (MCRegAliasIterator AI(X86::XMM8 + n, this, true); AI.isValid(); ++AI)
371 //===----------------------------------------------------------------------===//
372 // Stack Frame Processing methods
373 //===----------------------------------------------------------------------===//
375 bool X86RegisterInfo::hasBasePointer(const MachineFunction &MF) const {
376 const MachineFrameInfo *MFI = MF.getFrameInfo();
378 if (!EnableBasePointer)
381 // When we need stack realignment and there are dynamic allocas, we can't
382 // reference off of the stack pointer, so we reserve a base pointer.
383 if (needsStackRealignment(MF) && MFI->hasVarSizedObjects())
389 bool X86RegisterInfo::canRealignStack(const MachineFunction &MF) const {
390 const MachineFrameInfo *MFI = MF.getFrameInfo();
391 const MachineRegisterInfo *MRI = &MF.getRegInfo();
392 if (!MF.getTarget().Options.RealignStack)
395 // Stack realignment requires a frame pointer. If we already started
396 // register allocation with frame pointer elimination, it is too late now.
397 if (!MRI->canReserveReg(FramePtr))
400 // If a base pointer is necessary. Check that it isn't too late to reserve
402 if (MFI->hasVarSizedObjects())
403 return MRI->canReserveReg(BasePtr);
407 bool X86RegisterInfo::needsStackRealignment(const MachineFunction &MF) const {
408 const MachineFrameInfo *MFI = MF.getFrameInfo();
409 const Function *F = MF.getFunction();
410 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
411 bool requiresRealignment =
412 ((MFI->getMaxAlignment() > StackAlign) ||
413 F->getFnAttributes().hasAttribute(Attributes::StackAlignment));
415 // If we've requested that we force align the stack do so now.
417 return canRealignStack(MF);
419 return requiresRealignment && canRealignStack(MF);
422 bool X86RegisterInfo::hasReservedSpillSlot(const MachineFunction &MF,
423 unsigned Reg, int &FrameIdx) const {
424 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
426 if (Reg == FramePtr && TFI->hasFP(MF)) {
427 FrameIdx = MF.getFrameInfo()->getObjectIndexBegin();
433 static unsigned getSUBriOpcode(unsigned is64Bit, int64_t Imm) {
436 return X86::SUB64ri8;
437 return X86::SUB64ri32;
440 return X86::SUB32ri8;
445 static unsigned getADDriOpcode(unsigned is64Bit, int64_t Imm) {
448 return X86::ADD64ri8;
449 return X86::ADD64ri32;
452 return X86::ADD32ri8;
457 void X86RegisterInfo::
458 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
459 MachineBasicBlock::iterator I) const {
460 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
461 bool reseveCallFrame = TFI->hasReservedCallFrame(MF);
462 int Opcode = I->getOpcode();
463 bool isDestroy = Opcode == TII.getCallFrameDestroyOpcode();
464 DebugLoc DL = I->getDebugLoc();
465 uint64_t Amount = !reseveCallFrame ? I->getOperand(0).getImm() : 0;
466 uint64_t CalleeAmt = isDestroy ? I->getOperand(1).getImm() : 0;
469 if (!reseveCallFrame) {
470 // If the stack pointer can be changed after prologue, turn the
471 // adjcallstackup instruction into a 'sub ESP, <amt>' and the
472 // adjcallstackdown instruction into 'add ESP, <amt>'
473 // TODO: consider using push / pop instead of sub + store / add
477 // We need to keep the stack aligned properly. To do this, we round the
478 // amount of space needed for the outgoing arguments up to the next
479 // alignment boundary.
480 unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
481 Amount = (Amount + StackAlign - 1) / StackAlign * StackAlign;
483 MachineInstr *New = 0;
484 if (Opcode == TII.getCallFrameSetupOpcode()) {
485 New = BuildMI(MF, DL, TII.get(getSUBriOpcode(Is64Bit, Amount)),
490 assert(Opcode == TII.getCallFrameDestroyOpcode());
492 // Factor out the amount the callee already popped.
496 unsigned Opc = getADDriOpcode(Is64Bit, Amount);
497 New = BuildMI(MF, DL, TII.get(Opc), StackPtr)
498 .addReg(StackPtr).addImm(Amount);
503 // The EFLAGS implicit def is dead.
504 New->getOperand(3).setIsDead();
506 // Replace the pseudo instruction with a new instruction.
513 if (Opcode == TII.getCallFrameDestroyOpcode() && CalleeAmt) {
514 // If we are performing frame pointer elimination and if the callee pops
515 // something off the stack pointer, add it back. We do this until we have
516 // more advanced stack pointer tracking ability.
517 unsigned Opc = getSUBriOpcode(Is64Bit, CalleeAmt);
518 MachineInstr *New = BuildMI(MF, DL, TII.get(Opc), StackPtr)
519 .addReg(StackPtr).addImm(CalleeAmt);
521 // The EFLAGS implicit def is dead.
522 New->getOperand(3).setIsDead();
524 // We are not tracking the stack pointer adjustment by the callee, so make
525 // sure we restore the stack pointer immediately after the call, there may
526 // be spill code inserted between the CALL and ADJCALLSTACKUP instructions.
527 MachineBasicBlock::iterator B = MBB.begin();
528 while (I != B && !llvm::prior(I)->isCall())
535 X86RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
536 int SPAdj, RegScavenger *RS) const {
537 assert(SPAdj == 0 && "Unexpected");
540 MachineInstr &MI = *II;
541 MachineFunction &MF = *MI.getParent()->getParent();
542 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
544 while (!MI.getOperand(i).isFI()) {
546 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
549 int FrameIndex = MI.getOperand(i).getIndex();
552 unsigned Opc = MI.getOpcode();
553 bool AfterFPPop = Opc == X86::TAILJMPm64 || Opc == X86::TAILJMPm;
554 if (hasBasePointer(MF))
555 BasePtr = (FrameIndex < 0 ? FramePtr : getBaseRegister());
556 else if (needsStackRealignment(MF))
557 BasePtr = (FrameIndex < 0 ? FramePtr : StackPtr);
561 BasePtr = (TFI->hasFP(MF) ? FramePtr : StackPtr);
563 // This must be part of a four operand memory reference. Replace the
564 // FrameIndex with base register with EBP. Add an offset to the offset.
565 MI.getOperand(i).ChangeToRegister(BasePtr, false);
567 // Now add the frame object offset to the offset from EBP.
570 // Tail call jmp happens after FP is popped.
571 const MachineFrameInfo *MFI = MF.getFrameInfo();
572 FIOffset = MFI->getObjectOffset(FrameIndex) - TFI->getOffsetOfLocalArea();
574 FIOffset = TFI->getFrameIndexOffset(MF, FrameIndex);
576 if (MI.getOperand(i+3).isImm()) {
577 // Offset is a 32-bit integer.
578 int Imm = (int)(MI.getOperand(i + 3).getImm());
579 int Offset = FIOffset + Imm;
580 assert((!Is64Bit || isInt<32>((long long)FIOffset + Imm)) &&
581 "Requesting 64-bit offset in 32-bit immediate!");
582 MI.getOperand(i + 3).ChangeToImmediate(Offset);
584 // Offset is symbolic. This is extremely rare.
585 uint64_t Offset = FIOffset + (uint64_t)MI.getOperand(i+3).getOffset();
586 MI.getOperand(i+3).setOffset(Offset);
590 unsigned X86RegisterInfo::getFrameRegister(const MachineFunction &MF) const {
591 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
592 return TFI->hasFP(MF) ? FramePtr : StackPtr;
595 unsigned X86RegisterInfo::getEHExceptionRegister() const {
596 llvm_unreachable("What is the exception register");
599 unsigned X86RegisterInfo::getEHHandlerRegister() const {
600 llvm_unreachable("What is the exception handler register");
604 unsigned getX86SubSuperRegister(unsigned Reg, MVT::SimpleValueType VT,
607 default: llvm_unreachable("Unexpected VT");
611 default: return getX86SubSuperRegister(Reg, MVT::i64, High);
612 case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
614 case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
616 case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
618 case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
623 default: llvm_unreachable("Unexpected register");
624 case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
626 case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
628 case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
630 case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
632 case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI:
634 case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI:
636 case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP:
638 case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP:
640 case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8:
642 case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9:
644 case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10:
646 case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11:
648 case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12:
650 case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13:
652 case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14:
654 case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15:
660 default: llvm_unreachable("Unexpected register");
661 case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
663 case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
665 case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
667 case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
669 case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI:
671 case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI:
673 case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP:
675 case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP:
677 case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8:
679 case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9:
681 case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10:
683 case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11:
685 case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12:
687 case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13:
689 case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14:
691 case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15:
696 default: llvm_unreachable("Unexpected register");
697 case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
699 case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
701 case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
703 case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
705 case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI:
707 case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI:
709 case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP:
711 case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP:
713 case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8:
715 case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9:
717 case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10:
719 case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11:
721 case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12:
723 case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13:
725 case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14:
727 case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15:
731 // For 64-bit mode if we've requested a "high" register and the
732 // Q or r constraints we want one of these high registers or
733 // just the register name otherwise.
736 case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI:
738 case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI:
740 case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP:
742 case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP:
748 default: llvm_unreachable("Unexpected register");
749 case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
751 case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
753 case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
755 case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
757 case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI:
759 case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI:
761 case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP:
763 case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP:
765 case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8:
767 case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9:
769 case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10:
771 case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11:
773 case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12:
775 case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13:
777 case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14:
779 case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15:
787 struct MSAH : public MachineFunctionPass {
789 MSAH() : MachineFunctionPass(ID) {}
791 virtual bool runOnMachineFunction(MachineFunction &MF) {
792 const X86TargetMachine *TM =
793 static_cast<const X86TargetMachine *>(&MF.getTarget());
794 const TargetFrameLowering *TFI = TM->getFrameLowering();
795 MachineRegisterInfo &RI = MF.getRegInfo();
796 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
797 unsigned StackAlignment = TFI->getStackAlignment();
799 // Be over-conservative: scan over all vreg defs and find whether vector
800 // registers are used. If yes, there is a possibility that vector register
801 // will be spilled and thus require dynamic stack realignment.
802 for (unsigned i = 0, e = RI.getNumVirtRegs(); i != e; ++i) {
803 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
804 if (RI.getRegClass(Reg)->getAlignment() > StackAlignment) {
805 FuncInfo->setForceFramePointer(true);
813 virtual const char *getPassName() const {
814 return "X86 Maximal Stack Alignment Check";
817 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
818 AU.setPreservesCFG();
819 MachineFunctionPass::getAnalysisUsage(AU);
827 llvm::createX86MaxStackAlignmentHeuristicPass() { return new MSAH(); }