Put the shiny new MCSubRegIterator to work.
[oota-llvm.git] / lib / Target / X86 / X86RegisterInfo.cpp
1 //===-- X86RegisterInfo.cpp - X86 Register Information --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the X86 implementation of the TargetRegisterInfo class.
11 // This file is responsible for the frame pointer elimination optimization
12 // on X86.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "X86RegisterInfo.h"
17 #include "X86.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"
41
42 #define GET_REGINFO_TARGET_DESC
43 #include "X86GenRegisterInfo.inc"
44
45 using namespace llvm;
46
47 cl::opt<bool>
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);
52
53 X86RegisterInfo::X86RegisterInfo(X86TargetMachine &tm,
54                                  const TargetInstrInfo &tii)
55   : X86GenRegisterInfo(tm.getSubtarget<X86Subtarget>().is64Bit()
56                          ? X86::RIP : X86::EIP,
57                        X86_MC::getDwarfRegFlavour(tm.getTargetTriple(), false),
58                        X86_MC::getDwarfRegFlavour(tm.getTargetTriple(), true)),
59                        TM(tm), TII(tii) {
60   X86_MC::InitLLVM2SEHRegisterMapping(this);
61
62   // Cache some information.
63   const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
64   Is64Bit = Subtarget->is64Bit();
65   IsWin64 = Subtarget->isTargetWin64();
66
67   if (Is64Bit) {
68     SlotSize = 8;
69     StackPtr = X86::RSP;
70     FramePtr = X86::RBP;
71   } else {
72     SlotSize = 4;
73     StackPtr = X86::ESP;
74     FramePtr = X86::EBP;
75   }
76 }
77
78 /// getCompactUnwindRegNum - This function maps the register to the number for
79 /// compact unwind encoding. Return -1 if the register isn't valid.
80 int X86RegisterInfo::getCompactUnwindRegNum(unsigned RegNum, bool isEH) const {
81   switch (getLLVMRegNum(RegNum, isEH)) {
82   case X86::EBX: case X86::RBX: return 1;
83   case X86::ECX: case X86::R12: return 2;
84   case X86::EDX: case X86::R13: return 3;
85   case X86::EDI: case X86::R14: return 4;
86   case X86::ESI: case X86::R15: return 5;
87   case X86::EBP: case X86::RBP: return 6;
88   }
89
90   return -1;
91 }
92
93 bool
94 X86RegisterInfo::trackLivenessAfterRegAlloc(const MachineFunction &MF) const {
95   // Only enable when post-RA scheduling is enabled and this is needed.
96   return TM.getSubtargetImpl()->postRAScheduler();
97 }
98
99 int
100 X86RegisterInfo::getSEHRegNum(unsigned i) const {
101   int reg = X86_MC::getX86RegNum(i);
102   switch (i) {
103   case X86::R8:  case X86::R8D:  case X86::R8W:  case X86::R8B:
104   case X86::R9:  case X86::R9D:  case X86::R9W:  case X86::R9B:
105   case X86::R10: case X86::R10D: case X86::R10W: case X86::R10B:
106   case X86::R11: case X86::R11D: case X86::R11W: case X86::R11B:
107   case X86::R12: case X86::R12D: case X86::R12W: case X86::R12B:
108   case X86::R13: case X86::R13D: case X86::R13W: case X86::R13B:
109   case X86::R14: case X86::R14D: case X86::R14W: case X86::R14B:
110   case X86::R15: case X86::R15D: case X86::R15W: case X86::R15B:
111   case X86::XMM8: case X86::XMM9: case X86::XMM10: case X86::XMM11:
112   case X86::XMM12: case X86::XMM13: case X86::XMM14: case X86::XMM15:
113   case X86::YMM8: case X86::YMM9: case X86::YMM10: case X86::YMM11:
114   case X86::YMM12: case X86::YMM13: case X86::YMM14: case X86::YMM15:
115     reg += 8;
116   }
117   return reg;
118 }
119
120 const TargetRegisterClass *
121 X86RegisterInfo::getSubClassWithSubReg(const TargetRegisterClass *RC,
122                                        unsigned Idx) const {
123   // The sub_8bit sub-register index is more constrained in 32-bit mode.
124   // It behaves just like the sub_8bit_hi index.
125   if (!Is64Bit && Idx == X86::sub_8bit)
126     Idx = X86::sub_8bit_hi;
127
128   // Forward to TableGen's default version.
129   return X86GenRegisterInfo::getSubClassWithSubReg(RC, Idx);
130 }
131
132 const TargetRegisterClass *
133 X86RegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A,
134                                           const TargetRegisterClass *B,
135                                           unsigned SubIdx) const {
136   // The sub_8bit sub-register index is more constrained in 32-bit mode.
137   if (!Is64Bit && SubIdx == X86::sub_8bit) {
138     A = X86GenRegisterInfo::getSubClassWithSubReg(A, X86::sub_8bit_hi);
139     if (!A)
140       return 0;
141   }
142   return X86GenRegisterInfo::getMatchingSuperRegClass(A, B, SubIdx);
143 }
144
145 const TargetRegisterClass*
146 X86RegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC) const{
147   // Don't allow super-classes of GR8_NOREX.  This class is only used after
148   // extrating sub_8bit_hi sub-registers.  The H sub-registers cannot be copied
149   // to the full GR8 register class in 64-bit mode, so we cannot allow the
150   // reigster class inflation.
151   //
152   // The GR8_NOREX class is always used in a way that won't be constrained to a
153   // sub-class, so sub-classes like GR8_ABCD_L are allowed to expand to the
154   // full GR8 class.
155   if (RC == &X86::GR8_NOREXRegClass)
156     return RC;
157
158   const TargetRegisterClass *Super = RC;
159   TargetRegisterClass::sc_iterator I = RC->getSuperClasses();
160   do {
161     switch (Super->getID()) {
162     case X86::GR8RegClassID:
163     case X86::GR16RegClassID:
164     case X86::GR32RegClassID:
165     case X86::GR64RegClassID:
166     case X86::FR32RegClassID:
167     case X86::FR64RegClassID:
168     case X86::RFP32RegClassID:
169     case X86::RFP64RegClassID:
170     case X86::RFP80RegClassID:
171     case X86::VR128RegClassID:
172     case X86::VR256RegClassID:
173       // Don't return a super-class that would shrink the spill size.
174       // That can happen with the vector and float classes.
175       if (Super->getSize() == RC->getSize())
176         return Super;
177     }
178     Super = *I++;
179   } while (Super);
180   return RC;
181 }
182
183 const TargetRegisterClass *
184 X86RegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)
185                                                                          const {
186   switch (Kind) {
187   default: llvm_unreachable("Unexpected Kind in getPointerRegClass!");
188   case 0: // Normal GPRs.
189     if (TM.getSubtarget<X86Subtarget>().is64Bit())
190       return &X86::GR64RegClass;
191     return &X86::GR32RegClass;
192   case 1: // Normal GPRs except the stack pointer (for encoding reasons).
193     if (TM.getSubtarget<X86Subtarget>().is64Bit())
194       return &X86::GR64_NOSPRegClass;
195     return &X86::GR32_NOSPRegClass;
196   case 2: // Available for tailcall (not callee-saved GPRs).
197     if (TM.getSubtarget<X86Subtarget>().isTargetWin64())
198       return &X86::GR64_TCW64RegClass;
199     if (TM.getSubtarget<X86Subtarget>().is64Bit())
200       return &X86::GR64_TCRegClass;
201     return &X86::GR32_TCRegClass;
202   }
203 }
204
205 const TargetRegisterClass *
206 X86RegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
207   if (RC == &X86::CCRRegClass) {
208     if (Is64Bit)
209       return &X86::GR64RegClass;
210     else
211       return &X86::GR32RegClass;
212   }
213   return RC;
214 }
215
216 unsigned
217 X86RegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
218                                      MachineFunction &MF) const {
219   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
220
221   unsigned FPDiff = TFI->hasFP(MF) ? 1 : 0;
222   switch (RC->getID()) {
223   default:
224     return 0;
225   case X86::GR32RegClassID:
226     return 4 - FPDiff;
227   case X86::GR64RegClassID:
228     return 12 - FPDiff;
229   case X86::VR128RegClassID:
230     return TM.getSubtarget<X86Subtarget>().is64Bit() ? 10 : 4;
231   case X86::VR64RegClassID:
232     return 4;
233   }
234 }
235
236 const uint16_t *
237 X86RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
238   bool callsEHReturn = false;
239   bool ghcCall = false;
240
241   if (MF) {
242     callsEHReturn = MF->getMMI().callsEHReturn();
243     const Function *F = MF->getFunction();
244     ghcCall = (F ? F->getCallingConv() == CallingConv::GHC : false);
245   }
246
247   if (ghcCall)
248     return CSR_NoRegs_SaveList;
249   if (Is64Bit) {
250     if (IsWin64)
251       return CSR_Win64_SaveList;
252     if (callsEHReturn)
253       return CSR_64EHRet_SaveList;
254     return CSR_64_SaveList;
255   }
256   if (callsEHReturn)
257     return CSR_32EHRet_SaveList;
258   return CSR_32_SaveList;
259 }
260
261 const uint32_t*
262 X86RegisterInfo::getCallPreservedMask(CallingConv::ID CC) const {
263   if (CC == CallingConv::GHC)
264     return CSR_NoRegs_RegMask;
265   if (!Is64Bit)
266     return CSR_32_RegMask;
267   if (IsWin64)
268     return CSR_Win64_RegMask;
269   return CSR_64_RegMask;
270 }
271
272 BitVector X86RegisterInfo::getReservedRegs(const MachineFunction &MF) const {
273   BitVector Reserved(getNumRegs());
274   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
275
276   // Set the stack-pointer register and its aliases as reserved.
277   Reserved.set(X86::RSP);
278   for (MCSubRegIterator I(X86::RSP, this); I.isValid(); ++I)
279     Reserved.set(*I);
280
281   // Set the instruction pointer register and its aliases as reserved.
282   Reserved.set(X86::RIP);
283   for (MCSubRegIterator I(X86::RIP, this); I.isValid(); ++I)
284     Reserved.set(*I);
285
286   // Set the frame-pointer register and its aliases as reserved if needed.
287   if (TFI->hasFP(MF)) {
288     Reserved.set(X86::RBP);
289     for (MCSubRegIterator I(X86::RBP, this); I.isValid(); ++I)
290       Reserved.set(*I);
291   }
292
293   // Mark the segment registers as reserved.
294   Reserved.set(X86::CS);
295   Reserved.set(X86::SS);
296   Reserved.set(X86::DS);
297   Reserved.set(X86::ES);
298   Reserved.set(X86::FS);
299   Reserved.set(X86::GS);
300
301   // Mark the floating point stack registers as reserved.
302   Reserved.set(X86::ST0);
303   Reserved.set(X86::ST1);
304   Reserved.set(X86::ST2);
305   Reserved.set(X86::ST3);
306   Reserved.set(X86::ST4);
307   Reserved.set(X86::ST5);
308   Reserved.set(X86::ST6);
309   Reserved.set(X86::ST7);
310
311   // Reserve the registers that only exist in 64-bit mode.
312   if (!Is64Bit) {
313     // These 8-bit registers are part of the x86-64 extension even though their
314     // super-registers are old 32-bits.
315     Reserved.set(X86::SIL);
316     Reserved.set(X86::DIL);
317     Reserved.set(X86::BPL);
318     Reserved.set(X86::SPL);
319
320     for (unsigned n = 0; n != 8; ++n) {
321       // R8, R9, ...
322       static const uint16_t GPR64[] = {
323         X86::R8,  X86::R9,  X86::R10, X86::R11,
324         X86::R12, X86::R13, X86::R14, X86::R15
325       };
326       for (const uint16_t *AI = getOverlaps(GPR64[n]); unsigned Reg = *AI; ++AI)
327         Reserved.set(Reg);
328
329       // XMM8, XMM9, ...
330       assert(X86::XMM15 == X86::XMM8+7);
331       for (const uint16_t *AI = getOverlaps(X86::XMM8 + n); unsigned Reg = *AI;
332            ++AI)
333         Reserved.set(Reg);
334     }
335   }
336
337   return Reserved;
338 }
339
340 //===----------------------------------------------------------------------===//
341 // Stack Frame Processing methods
342 //===----------------------------------------------------------------------===//
343
344 bool X86RegisterInfo::canRealignStack(const MachineFunction &MF) const {
345   const MachineFrameInfo *MFI = MF.getFrameInfo();
346   return (MF.getTarget().Options.RealignStack &&
347           !MFI->hasVarSizedObjects());
348 }
349
350 bool X86RegisterInfo::needsStackRealignment(const MachineFunction &MF) const {
351   const MachineFrameInfo *MFI = MF.getFrameInfo();
352   const Function *F = MF.getFunction();
353   unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
354   bool requiresRealignment = ((MFI->getMaxAlignment() > StackAlign) ||
355                                F->hasFnAttr(Attribute::StackAlignment));
356
357   // FIXME: Currently we don't support stack realignment for functions with
358   //        variable-sized allocas.
359   // FIXME: It's more complicated than this...
360   if (0 && requiresRealignment && MFI->hasVarSizedObjects())
361     report_fatal_error(
362       "Stack realignment in presence of dynamic allocas is not supported");
363
364   // If we've requested that we force align the stack do so now.
365   if (ForceStackAlign)
366     return canRealignStack(MF);
367
368   return requiresRealignment && canRealignStack(MF);
369 }
370
371 bool X86RegisterInfo::hasReservedSpillSlot(const MachineFunction &MF,
372                                            unsigned Reg, int &FrameIdx) const {
373   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
374
375   if (Reg == FramePtr && TFI->hasFP(MF)) {
376     FrameIdx = MF.getFrameInfo()->getObjectIndexBegin();
377     return true;
378   }
379   return false;
380 }
381
382 static unsigned getSUBriOpcode(unsigned is64Bit, int64_t Imm) {
383   if (is64Bit) {
384     if (isInt<8>(Imm))
385       return X86::SUB64ri8;
386     return X86::SUB64ri32;
387   } else {
388     if (isInt<8>(Imm))
389       return X86::SUB32ri8;
390     return X86::SUB32ri;
391   }
392 }
393
394 static unsigned getADDriOpcode(unsigned is64Bit, int64_t Imm) {
395   if (is64Bit) {
396     if (isInt<8>(Imm))
397       return X86::ADD64ri8;
398     return X86::ADD64ri32;
399   } else {
400     if (isInt<8>(Imm))
401       return X86::ADD32ri8;
402     return X86::ADD32ri;
403   }
404 }
405
406 void X86RegisterInfo::
407 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
408                               MachineBasicBlock::iterator I) const {
409   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
410   bool reseveCallFrame = TFI->hasReservedCallFrame(MF);
411   int Opcode = I->getOpcode();
412   bool isDestroy = Opcode == TII.getCallFrameDestroyOpcode();
413   DebugLoc DL = I->getDebugLoc();
414   uint64_t Amount = !reseveCallFrame ? I->getOperand(0).getImm() : 0;
415   uint64_t CalleeAmt = isDestroy ? I->getOperand(1).getImm() : 0;
416   I = MBB.erase(I);
417
418   if (!reseveCallFrame) {
419     // If the stack pointer can be changed after prologue, turn the
420     // adjcallstackup instruction into a 'sub ESP, <amt>' and the
421     // adjcallstackdown instruction into 'add ESP, <amt>'
422     // TODO: consider using push / pop instead of sub + store / add
423     if (Amount == 0)
424       return;
425
426     // We need to keep the stack aligned properly.  To do this, we round the
427     // amount of space needed for the outgoing arguments up to the next
428     // alignment boundary.
429     unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
430     Amount = (Amount + StackAlign - 1) / StackAlign * StackAlign;
431
432     MachineInstr *New = 0;
433     if (Opcode == TII.getCallFrameSetupOpcode()) {
434       New = BuildMI(MF, DL, TII.get(getSUBriOpcode(Is64Bit, Amount)),
435                     StackPtr)
436         .addReg(StackPtr)
437         .addImm(Amount);
438     } else {
439       assert(Opcode == TII.getCallFrameDestroyOpcode());
440
441       // Factor out the amount the callee already popped.
442       Amount -= CalleeAmt;
443
444       if (Amount) {
445         unsigned Opc = getADDriOpcode(Is64Bit, Amount);
446         New = BuildMI(MF, DL, TII.get(Opc), StackPtr)
447           .addReg(StackPtr).addImm(Amount);
448       }
449     }
450
451     if (New) {
452       // The EFLAGS implicit def is dead.
453       New->getOperand(3).setIsDead();
454
455       // Replace the pseudo instruction with a new instruction.
456       MBB.insert(I, New);
457     }
458
459     return;
460   }
461
462   if (Opcode == TII.getCallFrameDestroyOpcode() && CalleeAmt) {
463     // If we are performing frame pointer elimination and if the callee pops
464     // something off the stack pointer, add it back.  We do this until we have
465     // more advanced stack pointer tracking ability.
466     unsigned Opc = getSUBriOpcode(Is64Bit, CalleeAmt);
467     MachineInstr *New = BuildMI(MF, DL, TII.get(Opc), StackPtr)
468       .addReg(StackPtr).addImm(CalleeAmt);
469
470     // The EFLAGS implicit def is dead.
471     New->getOperand(3).setIsDead();
472
473     // We are not tracking the stack pointer adjustment by the callee, so make
474     // sure we restore the stack pointer immediately after the call, there may
475     // be spill code inserted between the CALL and ADJCALLSTACKUP instructions.
476     MachineBasicBlock::iterator B = MBB.begin();
477     while (I != B && !llvm::prior(I)->isCall())
478       --I;
479     MBB.insert(I, New);
480   }
481 }
482
483 void
484 X86RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
485                                      int SPAdj, RegScavenger *RS) const{
486   assert(SPAdj == 0 && "Unexpected");
487
488   unsigned i = 0;
489   MachineInstr &MI = *II;
490   MachineFunction &MF = *MI.getParent()->getParent();
491   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
492
493   while (!MI.getOperand(i).isFI()) {
494     ++i;
495     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
496   }
497
498   int FrameIndex = MI.getOperand(i).getIndex();
499   unsigned BasePtr;
500
501   unsigned Opc = MI.getOpcode();
502   bool AfterFPPop = Opc == X86::TAILJMPm64 || Opc == X86::TAILJMPm;
503   if (needsStackRealignment(MF))
504     BasePtr = (FrameIndex < 0 ? FramePtr : StackPtr);
505   else if (AfterFPPop)
506     BasePtr = StackPtr;
507   else
508     BasePtr = (TFI->hasFP(MF) ? FramePtr : StackPtr);
509
510   // This must be part of a four operand memory reference.  Replace the
511   // FrameIndex with base register with EBP.  Add an offset to the offset.
512   MI.getOperand(i).ChangeToRegister(BasePtr, false);
513
514   // Now add the frame object offset to the offset from EBP.
515   int FIOffset;
516   if (AfterFPPop) {
517     // Tail call jmp happens after FP is popped.
518     const MachineFrameInfo *MFI = MF.getFrameInfo();
519     FIOffset = MFI->getObjectOffset(FrameIndex) - TFI->getOffsetOfLocalArea();
520   } else
521     FIOffset = TFI->getFrameIndexOffset(MF, FrameIndex);
522
523   if (MI.getOperand(i+3).isImm()) {
524     // Offset is a 32-bit integer.
525     int Imm = (int)(MI.getOperand(i + 3).getImm());
526     int Offset = FIOffset + Imm;
527     assert((!Is64Bit || isInt<32>((long long)FIOffset + Imm)) &&
528            "Requesting 64-bit offset in 32-bit immediate!");
529     MI.getOperand(i + 3).ChangeToImmediate(Offset);
530   } else {
531     // Offset is symbolic. This is extremely rare.
532     uint64_t Offset = FIOffset + (uint64_t)MI.getOperand(i+3).getOffset();
533     MI.getOperand(i+3).setOffset(Offset);
534   }
535 }
536
537 unsigned X86RegisterInfo::getFrameRegister(const MachineFunction &MF) const {
538   const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
539   return TFI->hasFP(MF) ? FramePtr : StackPtr;
540 }
541
542 unsigned X86RegisterInfo::getEHExceptionRegister() const {
543   llvm_unreachable("What is the exception register");
544 }
545
546 unsigned X86RegisterInfo::getEHHandlerRegister() const {
547   llvm_unreachable("What is the exception handler register");
548 }
549
550 namespace llvm {
551 unsigned getX86SubSuperRegister(unsigned Reg, EVT VT, bool High) {
552   switch (VT.getSimpleVT().SimpleTy) {
553   default: return Reg;
554   case MVT::i8:
555     if (High) {
556       switch (Reg) {
557       default: return getX86SubSuperRegister(Reg, MVT::i64, High);
558       case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
559         return X86::AH;
560       case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
561         return X86::DH;
562       case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
563         return X86::CH;
564       case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
565         return X86::BH;
566       }
567     } else {
568       switch (Reg) {
569       default: return 0;
570       case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
571         return X86::AL;
572       case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
573         return X86::DL;
574       case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
575         return X86::CL;
576       case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
577         return X86::BL;
578       case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI:
579         return X86::SIL;
580       case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI:
581         return X86::DIL;
582       case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP:
583         return X86::BPL;
584       case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP:
585         return X86::SPL;
586       case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8:
587         return X86::R8B;
588       case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9:
589         return X86::R9B;
590       case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10:
591         return X86::R10B;
592       case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11:
593         return X86::R11B;
594       case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12:
595         return X86::R12B;
596       case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13:
597         return X86::R13B;
598       case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14:
599         return X86::R14B;
600       case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15:
601         return X86::R15B;
602       }
603     }
604   case MVT::i16:
605     switch (Reg) {
606     default: return Reg;
607     case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
608       return X86::AX;
609     case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
610       return X86::DX;
611     case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
612       return X86::CX;
613     case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
614       return X86::BX;
615     case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI:
616       return X86::SI;
617     case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI:
618       return X86::DI;
619     case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP:
620       return X86::BP;
621     case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP:
622       return X86::SP;
623     case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8:
624       return X86::R8W;
625     case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9:
626       return X86::R9W;
627     case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10:
628       return X86::R10W;
629     case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11:
630       return X86::R11W;
631     case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12:
632       return X86::R12W;
633     case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13:
634       return X86::R13W;
635     case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14:
636       return X86::R14W;
637     case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15:
638       return X86::R15W;
639     }
640   case MVT::i32:
641     switch (Reg) {
642     default: return Reg;
643     case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
644       return X86::EAX;
645     case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
646       return X86::EDX;
647     case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
648       return X86::ECX;
649     case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
650       return X86::EBX;
651     case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI:
652       return X86::ESI;
653     case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI:
654       return X86::EDI;
655     case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP:
656       return X86::EBP;
657     case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP:
658       return X86::ESP;
659     case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8:
660       return X86::R8D;
661     case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9:
662       return X86::R9D;
663     case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10:
664       return X86::R10D;
665     case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11:
666       return X86::R11D;
667     case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12:
668       return X86::R12D;
669     case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13:
670       return X86::R13D;
671     case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14:
672       return X86::R14D;
673     case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15:
674       return X86::R15D;
675     }
676   case MVT::i64:
677     // For 64-bit mode if we've requested a "high" register and the
678     // Q or r constraints we want one of these high registers or
679     // just the register name otherwise.
680     if (High) {
681       switch (Reg) {
682       case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI:
683         return X86::SI;
684       case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI:
685         return X86::DI;
686       case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP:
687         return X86::BP;
688       case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP:
689         return X86::SP;
690       // Fallthrough.
691       }
692     }
693     switch (Reg) {
694     default: return Reg;
695     case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
696       return X86::RAX;
697     case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
698       return X86::RDX;
699     case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
700       return X86::RCX;
701     case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
702       return X86::RBX;
703     case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI:
704       return X86::RSI;
705     case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI:
706       return X86::RDI;
707     case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP:
708       return X86::RBP;
709     case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP:
710       return X86::RSP;
711     case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8:
712       return X86::R8;
713     case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9:
714       return X86::R9;
715     case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10:
716       return X86::R10;
717     case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11:
718       return X86::R11;
719     case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12:
720       return X86::R12;
721     case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13:
722       return X86::R13;
723     case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14:
724       return X86::R14;
725     case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15:
726       return X86::R15;
727     }
728   }
729 }
730 }
731
732 namespace {
733   struct MSAH : public MachineFunctionPass {
734     static char ID;
735     MSAH() : MachineFunctionPass(ID) {}
736
737     virtual bool runOnMachineFunction(MachineFunction &MF) {
738       const X86TargetMachine *TM =
739         static_cast<const X86TargetMachine *>(&MF.getTarget());
740       const TargetFrameLowering *TFI = TM->getFrameLowering();
741       MachineRegisterInfo &RI = MF.getRegInfo();
742       X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
743       unsigned StackAlignment = TFI->getStackAlignment();
744
745       // Be over-conservative: scan over all vreg defs and find whether vector
746       // registers are used. If yes, there is a possibility that vector register
747       // will be spilled and thus require dynamic stack realignment.
748       for (unsigned i = 0, e = RI.getNumVirtRegs(); i != e; ++i) {
749         unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
750         if (RI.getRegClass(Reg)->getAlignment() > StackAlignment) {
751           FuncInfo->setForceFramePointer(true);
752           return true;
753         }
754       }
755       // Nothing to do
756       return false;
757     }
758
759     virtual const char *getPassName() const {
760       return "X86 Maximal Stack Alignment Check";
761     }
762
763     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
764       AU.setPreservesCFG();
765       MachineFunctionPass::getAnalysisUsage(AU);
766     }
767   };
768
769   char MSAH::ID = 0;
770 }
771
772 FunctionPass*
773 llvm::createX86MaxStackAlignmentHeuristicPass() { return new MSAH(); }