Temporarily revert r68552. This was causing a failure in the self-hosting LLVM
[llvm/msp430.git] / lib / Target / X86 / X86RegisterInfo.cpp
blob310290213f72084971d4a364ea61ab874a7c4801
1 //===- X86RegisterInfo.cpp - X86 Register Information -----------*- C++ -*-===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "X86.h"
17 #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/MachineLocation.h"
31 #include "llvm/CodeGen/MachineModuleInfo.h"
32 #include "llvm/CodeGen/MachineRegisterInfo.h"
33 #include "llvm/Target/TargetAsmInfo.h"
34 #include "llvm/Target/TargetFrameInfo.h"
35 #include "llvm/Target/TargetInstrInfo.h"
36 #include "llvm/Target/TargetMachine.h"
37 #include "llvm/Target/TargetOptions.h"
38 #include "llvm/ADT/BitVector.h"
39 #include "llvm/ADT/STLExtras.h"
40 #include "llvm/Support/Compiler.h"
41 using namespace llvm;
43 X86RegisterInfo::X86RegisterInfo(X86TargetMachine &tm,
44 const TargetInstrInfo &tii)
45 : X86GenRegisterInfo(tm.getSubtarget<X86Subtarget>().is64Bit() ?
46 X86::ADJCALLSTACKDOWN64 :
47 X86::ADJCALLSTACKDOWN32,
48 tm.getSubtarget<X86Subtarget>().is64Bit() ?
49 X86::ADJCALLSTACKUP64 :
50 X86::ADJCALLSTACKUP32),
51 TM(tm), TII(tii) {
52 // Cache some information.
53 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
54 Is64Bit = Subtarget->is64Bit();
55 IsWin64 = Subtarget->isTargetWin64();
56 StackAlign = TM.getFrameInfo()->getStackAlignment();
57 if (Is64Bit) {
58 SlotSize = 8;
59 StackPtr = X86::RSP;
60 FramePtr = X86::RBP;
61 } else {
62 SlotSize = 4;
63 StackPtr = X86::ESP;
64 FramePtr = X86::EBP;
68 // getDwarfRegNum - This function maps LLVM register identifiers to the
69 // Dwarf specific numbering, used in debug info and exception tables.
71 int X86RegisterInfo::getDwarfRegNum(unsigned RegNo, bool isEH) const {
72 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
73 unsigned Flavour = DWARFFlavour::X86_64;
74 if (!Subtarget->is64Bit()) {
75 if (Subtarget->isTargetDarwin()) {
76 if (isEH)
77 Flavour = DWARFFlavour::X86_32_DarwinEH;
78 else
79 Flavour = DWARFFlavour::X86_32_Generic;
80 } else if (Subtarget->isTargetCygMing()) {
81 // Unsupported by now, just quick fallback
82 Flavour = DWARFFlavour::X86_32_Generic;
83 } else {
84 Flavour = DWARFFlavour::X86_32_Generic;
88 return X86GenRegisterInfo::getDwarfRegNumFull(RegNo, Flavour);
91 // getX86RegNum - This function maps LLVM register identifiers to their X86
92 // specific numbering, which is used in various places encoding instructions.
94 unsigned X86RegisterInfo::getX86RegNum(unsigned RegNo) {
95 switch(RegNo) {
96 case X86::RAX: case X86::EAX: case X86::AX: case X86::AL: return N86::EAX;
97 case X86::RCX: case X86::ECX: case X86::CX: case X86::CL: return N86::ECX;
98 case X86::RDX: case X86::EDX: case X86::DX: case X86::DL: return N86::EDX;
99 case X86::RBX: case X86::EBX: case X86::BX: case X86::BL: return N86::EBX;
100 case X86::RSP: case X86::ESP: case X86::SP: case X86::SPL: case X86::AH:
101 return N86::ESP;
102 case X86::RBP: case X86::EBP: case X86::BP: case X86::BPL: case X86::CH:
103 return N86::EBP;
104 case X86::RSI: case X86::ESI: case X86::SI: case X86::SIL: case X86::DH:
105 return N86::ESI;
106 case X86::RDI: case X86::EDI: case X86::DI: case X86::DIL: case X86::BH:
107 return N86::EDI;
109 case X86::R8: case X86::R8D: case X86::R8W: case X86::R8B:
110 return N86::EAX;
111 case X86::R9: case X86::R9D: case X86::R9W: case X86::R9B:
112 return N86::ECX;
113 case X86::R10: case X86::R10D: case X86::R10W: case X86::R10B:
114 return N86::EDX;
115 case X86::R11: case X86::R11D: case X86::R11W: case X86::R11B:
116 return N86::EBX;
117 case X86::R12: case X86::R12D: case X86::R12W: case X86::R12B:
118 return N86::ESP;
119 case X86::R13: case X86::R13D: case X86::R13W: case X86::R13B:
120 return N86::EBP;
121 case X86::R14: case X86::R14D: case X86::R14W: case X86::R14B:
122 return N86::ESI;
123 case X86::R15: case X86::R15D: case X86::R15W: case X86::R15B:
124 return N86::EDI;
126 case X86::ST0: case X86::ST1: case X86::ST2: case X86::ST3:
127 case X86::ST4: case X86::ST5: case X86::ST6: case X86::ST7:
128 return RegNo-X86::ST0;
130 case X86::XMM0: case X86::XMM8: case X86::MM0:
131 return 0;
132 case X86::XMM1: case X86::XMM9: case X86::MM1:
133 return 1;
134 case X86::XMM2: case X86::XMM10: case X86::MM2:
135 return 2;
136 case X86::XMM3: case X86::XMM11: case X86::MM3:
137 return 3;
138 case X86::XMM4: case X86::XMM12: case X86::MM4:
139 return 4;
140 case X86::XMM5: case X86::XMM13: case X86::MM5:
141 return 5;
142 case X86::XMM6: case X86::XMM14: case X86::MM6:
143 return 6;
144 case X86::XMM7: case X86::XMM15: case X86::MM7:
145 return 7;
147 default:
148 assert(isVirtualRegister(RegNo) && "Unknown physical register!");
149 assert(0 && "Register allocator hasn't allocated reg correctly yet!");
150 return 0;
154 const TargetRegisterClass *X86RegisterInfo::getPointerRegClass() const {
155 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
156 if (Subtarget->is64Bit())
157 return &X86::GR64RegClass;
158 else
159 return &X86::GR32RegClass;
162 const TargetRegisterClass *
163 X86RegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
164 if (RC == &X86::CCRRegClass) {
165 if (Is64Bit)
166 return &X86::GR64RegClass;
167 else
168 return &X86::GR32RegClass;
170 return NULL;
173 const unsigned *
174 X86RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
175 bool callsEHReturn = false;
177 if (MF) {
178 const MachineFrameInfo *MFI = MF->getFrameInfo();
179 const MachineModuleInfo *MMI = MFI->getMachineModuleInfo();
180 callsEHReturn = (MMI ? MMI->callsEHReturn() : false);
183 static const unsigned CalleeSavedRegs32Bit[] = {
184 X86::ESI, X86::EDI, X86::EBX, X86::EBP, 0
187 static const unsigned CalleeSavedRegs32EHRet[] = {
188 X86::EAX, X86::EDX, X86::ESI, X86::EDI, X86::EBX, X86::EBP, 0
191 static const unsigned CalleeSavedRegs64Bit[] = {
192 X86::RBX, X86::R12, X86::R13, X86::R14, X86::R15, X86::RBP, 0
195 static const unsigned CalleeSavedRegs64EHRet[] = {
196 X86::RAX, X86::RDX, X86::RBX, X86::R12,
197 X86::R13, X86::R14, X86::R15, X86::RBP, 0
200 static const unsigned CalleeSavedRegsWin64[] = {
201 X86::RBX, X86::RBP, X86::RDI, X86::RSI,
202 X86::R12, X86::R13, X86::R14, X86::R15,
203 X86::XMM6, X86::XMM7, X86::XMM8, X86::XMM9,
204 X86::XMM10, X86::XMM11, X86::XMM12, X86::XMM13,
205 X86::XMM14, X86::XMM15, 0
208 if (Is64Bit) {
209 if (IsWin64)
210 return CalleeSavedRegsWin64;
211 else
212 return (callsEHReturn ? CalleeSavedRegs64EHRet : CalleeSavedRegs64Bit);
213 } else {
214 return (callsEHReturn ? CalleeSavedRegs32EHRet : CalleeSavedRegs32Bit);
218 const TargetRegisterClass* const*
219 X86RegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
220 bool callsEHReturn = false;
222 if (MF) {
223 const MachineFrameInfo *MFI = MF->getFrameInfo();
224 const MachineModuleInfo *MMI = MFI->getMachineModuleInfo();
225 callsEHReturn = (MMI ? MMI->callsEHReturn() : false);
228 static const TargetRegisterClass * const CalleeSavedRegClasses32Bit[] = {
229 &X86::GR32RegClass, &X86::GR32RegClass,
230 &X86::GR32RegClass, &X86::GR32RegClass, 0
232 static const TargetRegisterClass * const CalleeSavedRegClasses32EHRet[] = {
233 &X86::GR32RegClass, &X86::GR32RegClass,
234 &X86::GR32RegClass, &X86::GR32RegClass,
235 &X86::GR32RegClass, &X86::GR32RegClass, 0
237 static const TargetRegisterClass * const CalleeSavedRegClasses64Bit[] = {
238 &X86::GR64RegClass, &X86::GR64RegClass,
239 &X86::GR64RegClass, &X86::GR64RegClass,
240 &X86::GR64RegClass, &X86::GR64RegClass, 0
242 static const TargetRegisterClass * const CalleeSavedRegClasses64EHRet[] = {
243 &X86::GR64RegClass, &X86::GR64RegClass,
244 &X86::GR64RegClass, &X86::GR64RegClass,
245 &X86::GR64RegClass, &X86::GR64RegClass,
246 &X86::GR64RegClass, &X86::GR64RegClass, 0
248 static const TargetRegisterClass * const CalleeSavedRegClassesWin64[] = {
249 &X86::GR64RegClass, &X86::GR64RegClass,
250 &X86::GR64RegClass, &X86::GR64RegClass,
251 &X86::GR64RegClass, &X86::GR64RegClass,
252 &X86::GR64RegClass, &X86::GR64RegClass,
253 &X86::VR128RegClass, &X86::VR128RegClass,
254 &X86::VR128RegClass, &X86::VR128RegClass,
255 &X86::VR128RegClass, &X86::VR128RegClass,
256 &X86::VR128RegClass, &X86::VR128RegClass,
257 &X86::VR128RegClass, &X86::VR128RegClass, 0
260 if (Is64Bit) {
261 if (IsWin64)
262 return CalleeSavedRegClassesWin64;
263 else
264 return (callsEHReturn ?
265 CalleeSavedRegClasses64EHRet : CalleeSavedRegClasses64Bit);
266 } else {
267 return (callsEHReturn ?
268 CalleeSavedRegClasses32EHRet : CalleeSavedRegClasses32Bit);
272 BitVector X86RegisterInfo::getReservedRegs(const MachineFunction &MF) const {
273 BitVector Reserved(getNumRegs());
274 // Set the stack-pointer register and its aliases as reserved.
275 Reserved.set(X86::RSP);
276 Reserved.set(X86::ESP);
277 Reserved.set(X86::SP);
278 Reserved.set(X86::SPL);
279 // Set the frame-pointer register and its aliases as reserved if needed.
280 if (hasFP(MF)) {
281 Reserved.set(X86::RBP);
282 Reserved.set(X86::EBP);
283 Reserved.set(X86::BP);
284 Reserved.set(X86::BPL);
286 // Mark the x87 stack registers as reserved, since they don't
287 // behave normally with respect to liveness. We don't fully
288 // model the effects of x87 stack pushes and pops after
289 // stackification.
290 Reserved.set(X86::ST0);
291 Reserved.set(X86::ST1);
292 Reserved.set(X86::ST2);
293 Reserved.set(X86::ST3);
294 Reserved.set(X86::ST4);
295 Reserved.set(X86::ST5);
296 Reserved.set(X86::ST6);
297 Reserved.set(X86::ST7);
298 return Reserved;
301 //===----------------------------------------------------------------------===//
302 // Stack Frame Processing methods
303 //===----------------------------------------------------------------------===//
305 static unsigned calculateMaxStackAlignment(const MachineFrameInfo *FFI) {
306 unsigned MaxAlign = 0;
307 for (int i = FFI->getObjectIndexBegin(),
308 e = FFI->getObjectIndexEnd(); i != e; ++i) {
309 if (FFI->isDeadObjectIndex(i))
310 continue;
311 unsigned Align = FFI->getObjectAlignment(i);
312 MaxAlign = std::max(MaxAlign, Align);
315 return MaxAlign;
318 // hasFP - Return true if the specified function should have a dedicated frame
319 // pointer register. This is true if the function has variable sized allocas or
320 // if frame pointer elimination is disabled.
322 bool X86RegisterInfo::hasFP(const MachineFunction &MF) const {
323 const MachineFrameInfo *MFI = MF.getFrameInfo();
324 const MachineModuleInfo *MMI = MFI->getMachineModuleInfo();
326 return (NoFramePointerElim ||
327 needsStackRealignment(MF) ||
328 MFI->hasVarSizedObjects() ||
329 MFI->isFrameAddressTaken() ||
330 MF.getInfo<X86MachineFunctionInfo>()->getForceFramePointer() ||
331 (MMI && MMI->callsUnwindInit()));
334 bool X86RegisterInfo::needsStackRealignment(const MachineFunction &MF) const {
335 const MachineFrameInfo *MFI = MF.getFrameInfo();
337 // FIXME: Currently we don't support stack realignment for functions with
338 // variable-sized allocas
339 return (RealignStack &&
340 (MFI->getMaxAlignment() > StackAlign &&
341 !MFI->hasVarSizedObjects()));
344 bool X86RegisterInfo::hasReservedCallFrame(MachineFunction &MF) const {
345 return !MF.getFrameInfo()->hasVarSizedObjects();
349 X86RegisterInfo::getFrameIndexOffset(MachineFunction &MF, int FI) const {
350 int Offset = MF.getFrameInfo()->getObjectOffset(FI) + SlotSize;
351 uint64_t StackSize = MF.getFrameInfo()->getStackSize();
353 if (needsStackRealignment(MF)) {
354 if (FI < 0)
355 // Skip the saved EBP
356 Offset += SlotSize;
357 else {
358 unsigned Align = MF.getFrameInfo()->getObjectAlignment(FI);
359 assert( (-(Offset + StackSize)) % Align == 0);
360 Align = 0;
361 return Offset + StackSize;
364 // FIXME: Support tail calls
365 } else {
366 if (!hasFP(MF))
367 return Offset + StackSize;
369 // Skip the saved EBP
370 Offset += SlotSize;
372 // Skip the RETADDR move area
373 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
374 int TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta();
375 if (TailCallReturnAddrDelta < 0) Offset -= TailCallReturnAddrDelta;
378 return Offset;
381 void X86RegisterInfo::
382 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
383 MachineBasicBlock::iterator I) const {
384 if (!hasReservedCallFrame(MF)) {
385 // If the stack pointer can be changed after prologue, turn the
386 // adjcallstackup instruction into a 'sub ESP, <amt>' and the
387 // adjcallstackdown instruction into 'add ESP, <amt>'
388 // TODO: consider using push / pop instead of sub + store / add
389 MachineInstr *Old = I;
390 uint64_t Amount = Old->getOperand(0).getImm();
391 if (Amount != 0) {
392 // We need to keep the stack aligned properly. To do this, we round the
393 // amount of space needed for the outgoing arguments up to the next
394 // alignment boundary.
395 Amount = (Amount+StackAlign-1)/StackAlign*StackAlign;
397 MachineInstr *New = 0;
398 if (Old->getOpcode() == getCallFrameSetupOpcode()) {
399 New = BuildMI(MF, Old->getDebugLoc(),
400 TII.get(Is64Bit ? X86::SUB64ri32 : X86::SUB32ri),
401 StackPtr).addReg(StackPtr).addImm(Amount);
402 } else {
403 assert(Old->getOpcode() == getCallFrameDestroyOpcode());
404 // factor out the amount the callee already popped.
405 uint64_t CalleeAmt = Old->getOperand(1).getImm();
406 Amount -= CalleeAmt;
407 if (Amount) {
408 unsigned Opc = (Amount < 128) ?
409 (Is64Bit ? X86::ADD64ri8 : X86::ADD32ri8) :
410 (Is64Bit ? X86::ADD64ri32 : X86::ADD32ri);
411 New = BuildMI(MF, Old->getDebugLoc(), TII.get(Opc), StackPtr)
412 .addReg(StackPtr).addImm(Amount);
416 if (New) {
417 // The EFLAGS implicit def is dead.
418 New->getOperand(3).setIsDead();
420 // Replace the pseudo instruction with a new instruction...
421 MBB.insert(I, New);
424 } else if (I->getOpcode() == getCallFrameDestroyOpcode()) {
425 // If we are performing frame pointer elimination and if the callee pops
426 // something off the stack pointer, add it back. We do this until we have
427 // more advanced stack pointer tracking ability.
428 if (uint64_t CalleeAmt = I->getOperand(1).getImm()) {
429 unsigned Opc = (CalleeAmt < 128) ?
430 (Is64Bit ? X86::SUB64ri8 : X86::SUB32ri8) :
431 (Is64Bit ? X86::SUB64ri32 : X86::SUB32ri);
432 MachineInstr *Old = I;
433 MachineInstr *New =
434 BuildMI(MF, Old->getDebugLoc(), TII.get(Opc),
435 StackPtr).addReg(StackPtr).addImm(CalleeAmt);
436 // The EFLAGS implicit def is dead.
437 New->getOperand(3).setIsDead();
439 MBB.insert(I, New);
443 MBB.erase(I);
446 void X86RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
447 int SPAdj, RegScavenger *RS) const{
448 assert(SPAdj == 0 && "Unexpected");
450 unsigned i = 0;
451 MachineInstr &MI = *II;
452 MachineFunction &MF = *MI.getParent()->getParent();
453 while (!MI.getOperand(i).isFI()) {
454 ++i;
455 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
458 int FrameIndex = MI.getOperand(i).getIndex();
460 unsigned BasePtr;
461 if (needsStackRealignment(MF))
462 BasePtr = (FrameIndex < 0 ? FramePtr : StackPtr);
463 else
464 BasePtr = (hasFP(MF) ? FramePtr : StackPtr);
466 // This must be part of a four operand memory reference. Replace the
467 // FrameIndex with base register with EBP. Add an offset to the offset.
468 MI.getOperand(i).ChangeToRegister(BasePtr, false);
470 // Now add the frame object offset to the offset from EBP.
471 if (MI.getOperand(i+3).isImm()) {
472 // Offset is a 32-bit integer.
473 int Offset = getFrameIndexOffset(MF, FrameIndex) +
474 (int)(MI.getOperand(i+3).getImm());
476 MI.getOperand(i+3).ChangeToImmediate(Offset);
477 } else {
478 // Offset is symbolic. This is extremely rare.
479 uint64_t Offset = getFrameIndexOffset(MF, FrameIndex) +
480 (uint64_t)MI.getOperand(i+3).getOffset();
481 MI.getOperand(i+3).setOffset(Offset);
485 void
486 X86RegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
487 RegScavenger *RS) const {
488 MachineFrameInfo *FFI = MF.getFrameInfo();
490 // Calculate and set max stack object alignment early, so we can decide
491 // whether we will need stack realignment (and thus FP).
492 unsigned MaxAlign = std::max(FFI->getMaxAlignment(),
493 calculateMaxStackAlignment(FFI));
495 FFI->setMaxAlignment(MaxAlign);
498 void
499 X86RegisterInfo::processFunctionBeforeFrameFinalized(MachineFunction &MF) const{
500 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
501 int32_t TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta();
502 if (TailCallReturnAddrDelta < 0) {
503 // create RETURNADDR area
504 // arg
505 // arg
506 // RETADDR
507 // { ...
508 // RETADDR area
509 // ...
510 // }
511 // [EBP]
512 MF.getFrameInfo()->
513 CreateFixedObject(-TailCallReturnAddrDelta,
514 (-1*SlotSize)+TailCallReturnAddrDelta);
516 if (hasFP(MF)) {
517 assert((TailCallReturnAddrDelta <= 0) &&
518 "The Delta should always be zero or negative");
519 // Create a frame entry for the EBP register that must be saved.
520 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize,
521 (int)SlotSize * -2+
522 TailCallReturnAddrDelta);
523 assert(FrameIdx == MF.getFrameInfo()->getObjectIndexBegin() &&
524 "Slot for EBP register must be last in order to be found!");
525 FrameIdx = 0;
529 /// emitSPUpdate - Emit a series of instructions to increment / decrement the
530 /// stack pointer by a constant value.
531 static
532 void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
533 unsigned StackPtr, int64_t NumBytes, bool Is64Bit,
534 const TargetInstrInfo &TII) {
535 bool isSub = NumBytes < 0;
536 uint64_t Offset = isSub ? -NumBytes : NumBytes;
537 unsigned Opc = isSub
538 ? ((Offset < 128) ?
539 (Is64Bit ? X86::SUB64ri8 : X86::SUB32ri8) :
540 (Is64Bit ? X86::SUB64ri32 : X86::SUB32ri))
541 : ((Offset < 128) ?
542 (Is64Bit ? X86::ADD64ri8 : X86::ADD32ri8) :
543 (Is64Bit ? X86::ADD64ri32 : X86::ADD32ri));
544 uint64_t Chunk = (1LL << 31) - 1;
545 DebugLoc DL = (MBBI != MBB.end() ? MBBI->getDebugLoc() :
546 DebugLoc::getUnknownLoc());
548 while (Offset) {
549 uint64_t ThisVal = (Offset > Chunk) ? Chunk : Offset;
550 MachineInstr *MI =
551 BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr)
552 .addReg(StackPtr).addImm(ThisVal);
553 // The EFLAGS implicit def is dead.
554 MI->getOperand(3).setIsDead();
555 Offset -= ThisVal;
559 // mergeSPUpdatesUp - Merge two stack-manipulating instructions upper iterator.
560 static
561 void mergeSPUpdatesUp(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
562 unsigned StackPtr, uint64_t *NumBytes = NULL) {
563 if (MBBI == MBB.begin()) return;
565 MachineBasicBlock::iterator PI = prior(MBBI);
566 unsigned Opc = PI->getOpcode();
567 if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 ||
568 Opc == X86::ADD32ri || Opc == X86::ADD32ri8) &&
569 PI->getOperand(0).getReg() == StackPtr) {
570 if (NumBytes)
571 *NumBytes += PI->getOperand(2).getImm();
572 MBB.erase(PI);
573 } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 ||
574 Opc == X86::SUB32ri || Opc == X86::SUB32ri8) &&
575 PI->getOperand(0).getReg() == StackPtr) {
576 if (NumBytes)
577 *NumBytes -= PI->getOperand(2).getImm();
578 MBB.erase(PI);
582 // mergeSPUpdatesUp - Merge two stack-manipulating instructions lower iterator.
583 static
584 void mergeSPUpdatesDown(MachineBasicBlock &MBB,
585 MachineBasicBlock::iterator &MBBI,
586 unsigned StackPtr, uint64_t *NumBytes = NULL) {
587 return;
589 if (MBBI == MBB.end()) return;
591 MachineBasicBlock::iterator NI = next(MBBI);
592 if (NI == MBB.end()) return;
594 unsigned Opc = NI->getOpcode();
595 if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 ||
596 Opc == X86::ADD32ri || Opc == X86::ADD32ri8) &&
597 NI->getOperand(0).getReg() == StackPtr) {
598 if (NumBytes)
599 *NumBytes -= NI->getOperand(2).getImm();
600 MBB.erase(NI);
601 MBBI = NI;
602 } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 ||
603 Opc == X86::SUB32ri || Opc == X86::SUB32ri8) &&
604 NI->getOperand(0).getReg() == StackPtr) {
605 if (NumBytes)
606 *NumBytes += NI->getOperand(2).getImm();
607 MBB.erase(NI);
608 MBBI = NI;
612 /// mergeSPUpdates - Checks the instruction before/after the passed
613 /// instruction. If it is an ADD/SUB instruction it is deleted
614 /// argument and the stack adjustment is returned as a positive value for ADD
615 /// and a negative for SUB.
616 static int mergeSPUpdates(MachineBasicBlock &MBB,
617 MachineBasicBlock::iterator &MBBI,
618 unsigned StackPtr,
619 bool doMergeWithPrevious) {
621 if ((doMergeWithPrevious && MBBI == MBB.begin()) ||
622 (!doMergeWithPrevious && MBBI == MBB.end()))
623 return 0;
625 int Offset = 0;
627 MachineBasicBlock::iterator PI = doMergeWithPrevious ? prior(MBBI) : MBBI;
628 MachineBasicBlock::iterator NI = doMergeWithPrevious ? 0 : next(MBBI);
629 unsigned Opc = PI->getOpcode();
630 if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 ||
631 Opc == X86::ADD32ri || Opc == X86::ADD32ri8) &&
632 PI->getOperand(0).getReg() == StackPtr){
633 Offset += PI->getOperand(2).getImm();
634 MBB.erase(PI);
635 if (!doMergeWithPrevious) MBBI = NI;
636 } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 ||
637 Opc == X86::SUB32ri || Opc == X86::SUB32ri8) &&
638 PI->getOperand(0).getReg() == StackPtr) {
639 Offset -= PI->getOperand(2).getImm();
640 MBB.erase(PI);
641 if (!doMergeWithPrevious) MBBI = NI;
644 return Offset;
647 void X86RegisterInfo::emitFrameMoves(MachineFunction &MF,
648 unsigned FrameLabelId,
649 unsigned ReadyLabelId) const {
650 MachineFrameInfo *MFI = MF.getFrameInfo();
651 MachineModuleInfo *MMI = MFI->getMachineModuleInfo();
652 if (!MMI)
653 return;
655 uint64_t StackSize = MFI->getStackSize();
656 std::vector<MachineMove> &Moves = MMI->getFrameMoves();
657 const TargetData *TD = MF.getTarget().getTargetData();
659 // Calculate amount of bytes used for return address storing
660 int stackGrowth =
661 (MF.getTarget().getFrameInfo()->getStackGrowthDirection() ==
662 TargetFrameInfo::StackGrowsUp ?
663 TD->getPointerSize() : -TD->getPointerSize());
665 if (StackSize) {
666 // Show update of SP.
667 if (hasFP(MF)) {
668 // Adjust SP
669 MachineLocation SPDst(MachineLocation::VirtualFP);
670 MachineLocation SPSrc(MachineLocation::VirtualFP, 2*stackGrowth);
671 Moves.push_back(MachineMove(FrameLabelId, SPDst, SPSrc));
672 } else {
673 MachineLocation SPDst(MachineLocation::VirtualFP);
674 MachineLocation SPSrc(MachineLocation::VirtualFP,
675 -StackSize+stackGrowth);
676 Moves.push_back(MachineMove(FrameLabelId, SPDst, SPSrc));
678 } else {
679 //FIXME: Verify & implement for FP
680 MachineLocation SPDst(StackPtr);
681 MachineLocation SPSrc(StackPtr, stackGrowth);
682 Moves.push_back(MachineMove(FrameLabelId, SPDst, SPSrc));
685 // Add callee saved registers to move list.
686 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
688 // FIXME: This is dirty hack. The code itself is pretty mess right now.
689 // It should be rewritten from scratch and generalized sometimes.
691 // Determine maximum offset (minumum due to stack growth)
692 int64_t MaxOffset = 0;
693 for (unsigned I = 0, E = CSI.size(); I!=E; ++I)
694 MaxOffset = std::min(MaxOffset,
695 MFI->getObjectOffset(CSI[I].getFrameIdx()));
697 // Calculate offsets
698 int64_t saveAreaOffset = (hasFP(MF) ? 3 : 2)*stackGrowth;
699 for (unsigned I = 0, E = CSI.size(); I!=E; ++I) {
700 int64_t Offset = MFI->getObjectOffset(CSI[I].getFrameIdx());
701 unsigned Reg = CSI[I].getReg();
702 Offset = (MaxOffset-Offset+saveAreaOffset);
703 MachineLocation CSDst(MachineLocation::VirtualFP, Offset);
704 MachineLocation CSSrc(Reg);
705 Moves.push_back(MachineMove(FrameLabelId, CSDst, CSSrc));
708 if (hasFP(MF)) {
709 // Save FP
710 MachineLocation FPDst(MachineLocation::VirtualFP, 2*stackGrowth);
711 MachineLocation FPSrc(FramePtr);
712 Moves.push_back(MachineMove(ReadyLabelId, FPDst, FPSrc));
715 MachineLocation FPDst(hasFP(MF) ? FramePtr : StackPtr);
716 MachineLocation FPSrc(MachineLocation::VirtualFP);
717 Moves.push_back(MachineMove(ReadyLabelId, FPDst, FPSrc));
721 void X86RegisterInfo::emitPrologue(MachineFunction &MF) const {
722 MachineBasicBlock &MBB = MF.front(); // Prolog goes in entry BB
723 MachineFrameInfo *MFI = MF.getFrameInfo();
724 const Function* Fn = MF.getFunction();
725 const X86Subtarget* Subtarget = &MF.getTarget().getSubtarget<X86Subtarget>();
726 MachineModuleInfo *MMI = MFI->getMachineModuleInfo();
727 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
728 MachineBasicBlock::iterator MBBI = MBB.begin();
729 bool needsFrameMoves = (MMI && MMI->hasDebugInfo()) ||
730 !Fn->doesNotThrow() ||
731 UnwindTablesMandatory;
732 DebugLoc DL = (MBBI != MBB.end() ? MBBI->getDebugLoc() :
733 DebugLoc::getUnknownLoc());
735 // Prepare for frame info.
736 unsigned FrameLabelId = 0;
738 // Get the number of bytes to allocate from the FrameInfo.
739 uint64_t StackSize = MFI->getStackSize();
741 // Get desired stack alignment
742 uint64_t MaxAlign = MFI->getMaxAlignment();
744 // Add RETADDR move area to callee saved frame size.
745 int TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta();
746 if (TailCallReturnAddrDelta < 0)
747 X86FI->setCalleeSavedFrameSize(
748 X86FI->getCalleeSavedFrameSize() +(-TailCallReturnAddrDelta));
750 // If this is x86-64 and the Red Zone is not disabled, if we are a leaf
751 // function, and use up to 128 bytes of stack space, don't have a frame
752 // pointer, calls, or dynamic alloca then we do not need to adjust the
753 // stack pointer (we fit in the Red Zone).
754 if (Is64Bit && !DisableRedZone &&
755 !needsStackRealignment(MF) &&
756 !MFI->hasVarSizedObjects() && // No dynamic alloca.
757 !MFI->hasCalls()) { // No calls.
758 uint64_t MinSize = X86FI->getCalleeSavedFrameSize();
759 if (hasFP(MF)) MinSize += SlotSize;
760 StackSize = std::max(MinSize,
761 StackSize > 128 ? StackSize - 128 : 0);
762 MFI->setStackSize(StackSize);
765 // Insert stack pointer adjustment for later moving of return addr. Only
766 // applies to tail call optimized functions where the callee argument stack
767 // size is bigger than the callers.
768 if (TailCallReturnAddrDelta < 0) {
769 MachineInstr *MI =
770 BuildMI(MBB, MBBI, DL, TII.get(Is64Bit? X86::SUB64ri32 : X86::SUB32ri),
771 StackPtr).addReg(StackPtr).addImm(-TailCallReturnAddrDelta);
772 // The EFLAGS implicit def is dead.
773 MI->getOperand(3).setIsDead();
776 uint64_t NumBytes = 0;
777 if (hasFP(MF)) {
778 // Calculate required stack adjustment
779 uint64_t FrameSize = StackSize - SlotSize;
780 if (needsStackRealignment(MF))
781 FrameSize = (FrameSize + MaxAlign - 1)/MaxAlign*MaxAlign;
783 NumBytes = FrameSize - X86FI->getCalleeSavedFrameSize();
785 // Get the offset of the stack slot for the EBP register... which is
786 // guaranteed to be the last slot by processFunctionBeforeFrameFinalized.
787 // Update the frame offset adjustment.
788 MFI->setOffsetAdjustment(-NumBytes);
790 // Save EBP into the appropriate stack slot...
791 BuildMI(MBB, MBBI, DL, TII.get(Is64Bit ? X86::PUSH64r : X86::PUSH32r))
792 .addReg(FramePtr, /*isDef=*/false, /*isImp=*/false, /*isKill=*/true);
794 if (needsFrameMoves) {
795 // Mark effective beginning of when frame pointer becomes valid.
796 FrameLabelId = MMI->NextLabelID();
797 BuildMI(MBB, MBBI, DL, TII.get(X86::DBG_LABEL)).addImm(FrameLabelId);
800 // Update EBP with the new base value...
801 BuildMI(MBB, MBBI, DL,
802 TII.get(Is64Bit ? X86::MOV64rr : X86::MOV32rr), FramePtr)
803 .addReg(StackPtr);
805 // Mark the FramePtr as live-in in every block except the entry.
806 for (MachineFunction::iterator I = next(MF.begin()), E = MF.end();
807 I != E; ++I)
808 I->addLiveIn(FramePtr);
810 // Realign stack
811 if (needsStackRealignment(MF)) {
812 MachineInstr *MI =
813 BuildMI(MBB, MBBI, DL,
814 TII.get(Is64Bit ? X86::AND64ri32 : X86::AND32ri),
815 StackPtr).addReg(StackPtr).addImm(-MaxAlign);
816 // The EFLAGS implicit def is dead.
817 MI->getOperand(3).setIsDead();
819 } else {
820 NumBytes = StackSize - X86FI->getCalleeSavedFrameSize();
823 unsigned ReadyLabelId = 0;
824 if (needsFrameMoves) {
825 // Mark effective beginning of when frame pointer is ready.
826 ReadyLabelId = MMI->NextLabelID();
827 BuildMI(MBB, MBBI, DL, TII.get(X86::DBG_LABEL)).addImm(ReadyLabelId);
830 // Skip the callee-saved push instructions.
831 while (MBBI != MBB.end() &&
832 (MBBI->getOpcode() == X86::PUSH32r ||
833 MBBI->getOpcode() == X86::PUSH64r))
834 ++MBBI;
836 if (MBBI != MBB.end())
837 DL = MBBI->getDebugLoc();
839 if (NumBytes) { // adjust stack pointer: ESP -= numbytes
840 if (NumBytes >= 4096 && Subtarget->isTargetCygMing()) {
841 // Check, whether EAX is livein for this function
842 bool isEAXAlive = false;
843 for (MachineRegisterInfo::livein_iterator
844 II = MF.getRegInfo().livein_begin(),
845 EE = MF.getRegInfo().livein_end(); (II != EE) && !isEAXAlive; ++II) {
846 unsigned Reg = II->first;
847 isEAXAlive = (Reg == X86::EAX || Reg == X86::AX ||
848 Reg == X86::AH || Reg == X86::AL);
851 // Function prologue calls _alloca to probe the stack when allocating
852 // more than 4k bytes in one go. Touching the stack at 4K increments is
853 // necessary to ensure that the guard pages used by the OS virtual memory
854 // manager are allocated in correct sequence.
855 if (!isEAXAlive) {
856 BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32ri), X86::EAX)
857 .addImm(NumBytes);
858 BuildMI(MBB, MBBI, DL, TII.get(X86::CALLpcrel32))
859 .addExternalSymbol("_alloca");
860 } else {
861 // Save EAX
862 BuildMI(MBB, MBBI, DL, TII.get(X86::PUSH32r))
863 .addReg(X86::EAX, /*isDef=*/false, /*isImp=*/false, /*isKill=*/true);
864 // Allocate NumBytes-4 bytes on stack. We'll also use 4 already
865 // allocated bytes for EAX.
866 BuildMI(MBB, MBBI, DL,
867 TII.get(X86::MOV32ri), X86::EAX).addImm(NumBytes-4);
868 BuildMI(MBB, MBBI, DL, TII.get(X86::CALLpcrel32))
869 .addExternalSymbol("_alloca");
870 // Restore EAX
871 MachineInstr *MI = addRegOffset(BuildMI(MF, DL, TII.get(X86::MOV32rm),
872 X86::EAX),
873 StackPtr, false, NumBytes-4);
874 MBB.insert(MBBI, MI);
876 } else {
877 // If there is an SUB32ri of ESP immediately before this instruction,
878 // merge the two. This can be the case when tail call elimination is
879 // enabled and the callee has more arguments then the caller.
880 NumBytes -= mergeSPUpdates(MBB, MBBI, StackPtr, true);
881 // If there is an ADD32ri or SUB32ri of ESP immediately after this
882 // instruction, merge the two instructions.
883 mergeSPUpdatesDown(MBB, MBBI, StackPtr, &NumBytes);
885 if (NumBytes)
886 emitSPUpdate(MBB, MBBI, StackPtr, -(int64_t)NumBytes, Is64Bit, TII);
890 if (needsFrameMoves)
891 emitFrameMoves(MF, FrameLabelId, ReadyLabelId);
894 void X86RegisterInfo::emitEpilogue(MachineFunction &MF,
895 MachineBasicBlock &MBB) const {
896 const MachineFrameInfo *MFI = MF.getFrameInfo();
897 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
898 MachineBasicBlock::iterator MBBI = prior(MBB.end());
899 unsigned RetOpcode = MBBI->getOpcode();
900 DebugLoc DL = MBBI->getDebugLoc();
902 switch (RetOpcode) {
903 case X86::RET:
904 case X86::RETI:
905 case X86::TCRETURNdi:
906 case X86::TCRETURNri:
907 case X86::TCRETURNri64:
908 case X86::TCRETURNdi64:
909 case X86::EH_RETURN:
910 case X86::EH_RETURN64:
911 case X86::TAILJMPd:
912 case X86::TAILJMPr:
913 case X86::TAILJMPm: break; // These are ok
914 default:
915 assert(0 && "Can only insert epilog into returning blocks");
918 // Get the number of bytes to allocate from the FrameInfo
919 uint64_t StackSize = MFI->getStackSize();
920 uint64_t MaxAlign = MFI->getMaxAlignment();
921 unsigned CSSize = X86FI->getCalleeSavedFrameSize();
922 uint64_t NumBytes = 0;
924 if (hasFP(MF)) {
925 // Calculate required stack adjustment
926 uint64_t FrameSize = StackSize - SlotSize;
927 if (needsStackRealignment(MF))
928 FrameSize = (FrameSize + MaxAlign - 1)/MaxAlign*MaxAlign;
930 NumBytes = FrameSize - CSSize;
932 // pop EBP.
933 BuildMI(MBB, MBBI, DL,
934 TII.get(Is64Bit ? X86::POP64r : X86::POP32r), FramePtr);
935 } else {
936 NumBytes = StackSize - CSSize;
939 // Skip the callee-saved pop instructions.
940 MachineBasicBlock::iterator LastCSPop = MBBI;
941 while (MBBI != MBB.begin()) {
942 MachineBasicBlock::iterator PI = prior(MBBI);
943 unsigned Opc = PI->getOpcode();
944 if (Opc != X86::POP32r && Opc != X86::POP64r &&
945 !PI->getDesc().isTerminator())
946 break;
947 --MBBI;
950 DL = MBBI->getDebugLoc();
952 // If there is an ADD32ri or SUB32ri of ESP immediately before this
953 // instruction, merge the two instructions.
954 if (NumBytes || MFI->hasVarSizedObjects())
955 mergeSPUpdatesUp(MBB, MBBI, StackPtr, &NumBytes);
957 // If dynamic alloca is used, then reset esp to point to the last callee-saved
958 // slot before popping them off! Same applies for the case, when stack was
959 // realigned
960 if (needsStackRealignment(MF)) {
961 // We cannot use LEA here, because stack pointer was realigned. We need to
962 // deallocate local frame back
963 if (CSSize) {
964 emitSPUpdate(MBB, MBBI, StackPtr, NumBytes, Is64Bit, TII);
965 MBBI = prior(LastCSPop);
968 BuildMI(MBB, MBBI, DL,
969 TII.get(Is64Bit ? X86::MOV64rr : X86::MOV32rr),
970 StackPtr).addReg(FramePtr);
971 } else if (MFI->hasVarSizedObjects()) {
972 if (CSSize) {
973 unsigned Opc = Is64Bit ? X86::LEA64r : X86::LEA32r;
974 MachineInstr *MI = addRegOffset(BuildMI(MF, DL, TII.get(Opc), StackPtr),
975 FramePtr, false, -CSSize);
976 MBB.insert(MBBI, MI);
977 } else
978 BuildMI(MBB, MBBI, DL, TII.get(Is64Bit ? X86::MOV64rr : X86::MOV32rr),
979 StackPtr).addReg(FramePtr);
981 } else {
982 // adjust stack pointer back: ESP += numbytes
983 if (NumBytes)
984 emitSPUpdate(MBB, MBBI, StackPtr, NumBytes, Is64Bit, TII);
987 // We're returning from function via eh_return.
988 if (RetOpcode == X86::EH_RETURN || RetOpcode == X86::EH_RETURN64) {
989 MBBI = prior(MBB.end());
990 MachineOperand &DestAddr = MBBI->getOperand(0);
991 assert(DestAddr.isReg() && "Offset should be in register!");
992 BuildMI(MBB, MBBI, DL,
993 TII.get(Is64Bit ? X86::MOV64rr : X86::MOV32rr),
994 StackPtr).addReg(DestAddr.getReg());
995 // Tail call return: adjust the stack pointer and jump to callee
996 } else if (RetOpcode == X86::TCRETURNri || RetOpcode == X86::TCRETURNdi ||
997 RetOpcode== X86::TCRETURNri64 || RetOpcode == X86::TCRETURNdi64) {
998 MBBI = prior(MBB.end());
999 MachineOperand &JumpTarget = MBBI->getOperand(0);
1000 MachineOperand &StackAdjust = MBBI->getOperand(1);
1001 assert(StackAdjust.isImm() && "Expecting immediate value.");
1003 // Adjust stack pointer.
1004 int StackAdj = StackAdjust.getImm();
1005 int MaxTCDelta = X86FI->getTCReturnAddrDelta();
1006 int Offset = 0;
1007 assert(MaxTCDelta <= 0 && "MaxTCDelta should never be positive");
1008 // Incoporate the retaddr area.
1009 Offset = StackAdj-MaxTCDelta;
1010 assert(Offset >= 0 && "Offset should never be negative");
1012 if (Offset) {
1013 // Check for possible merge with preceeding ADD instruction.
1014 Offset += mergeSPUpdates(MBB, MBBI, StackPtr, true);
1015 emitSPUpdate(MBB, MBBI, StackPtr, Offset, Is64Bit, TII);
1018 // Jump to label or value in register.
1019 if (RetOpcode == X86::TCRETURNdi|| RetOpcode == X86::TCRETURNdi64)
1020 BuildMI(MBB, MBBI, DL, TII.get(X86::TAILJMPd)).
1021 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset());
1022 else if (RetOpcode== X86::TCRETURNri64)
1023 BuildMI(MBB, MBBI, DL, TII.get(X86::TAILJMPr64), JumpTarget.getReg());
1024 else
1025 BuildMI(MBB, MBBI, DL, TII.get(X86::TAILJMPr), JumpTarget.getReg());
1027 // Delete the pseudo instruction TCRETURN.
1028 MBB.erase(MBBI);
1029 } else if ((RetOpcode == X86::RET || RetOpcode == X86::RETI) &&
1030 (X86FI->getTCReturnAddrDelta() < 0)) {
1031 // Add the return addr area delta back since we are not tail calling.
1032 int delta = -1*X86FI->getTCReturnAddrDelta();
1033 MBBI = prior(MBB.end());
1034 // Check for possible merge with preceeding ADD instruction.
1035 delta += mergeSPUpdates(MBB, MBBI, StackPtr, true);
1036 emitSPUpdate(MBB, MBBI, StackPtr, delta, Is64Bit, TII);
1040 unsigned X86RegisterInfo::getRARegister() const {
1041 if (Is64Bit)
1042 return X86::RIP; // Should have dwarf #16
1043 else
1044 return X86::EIP; // Should have dwarf #8
1047 unsigned X86RegisterInfo::getFrameRegister(MachineFunction &MF) const {
1048 return hasFP(MF) ? FramePtr : StackPtr;
1051 void X86RegisterInfo::getInitialFrameState(std::vector<MachineMove> &Moves)
1052 const {
1053 // Calculate amount of bytes used for return address storing
1054 int stackGrowth = (Is64Bit ? -8 : -4);
1056 // Initial state of the frame pointer is esp+4.
1057 MachineLocation Dst(MachineLocation::VirtualFP);
1058 MachineLocation Src(StackPtr, stackGrowth);
1059 Moves.push_back(MachineMove(0, Dst, Src));
1061 // Add return address to move list
1062 MachineLocation CSDst(StackPtr, stackGrowth);
1063 MachineLocation CSSrc(getRARegister());
1064 Moves.push_back(MachineMove(0, CSDst, CSSrc));
1067 unsigned X86RegisterInfo::getEHExceptionRegister() const {
1068 assert(0 && "What is the exception register");
1069 return 0;
1072 unsigned X86RegisterInfo::getEHHandlerRegister() const {
1073 assert(0 && "What is the exception handler register");
1074 return 0;
1077 namespace llvm {
1078 unsigned getX86SubSuperRegister(unsigned Reg, MVT VT, bool High) {
1079 switch (VT.getSimpleVT()) {
1080 default: return Reg;
1081 case MVT::i8:
1082 if (High) {
1083 switch (Reg) {
1084 default: return 0;
1085 case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
1086 return X86::AH;
1087 case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
1088 return X86::DH;
1089 case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
1090 return X86::CH;
1091 case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
1092 return X86::BH;
1094 } else {
1095 switch (Reg) {
1096 default: return 0;
1097 case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
1098 return X86::AL;
1099 case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
1100 return X86::DL;
1101 case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
1102 return X86::CL;
1103 case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
1104 return X86::BL;
1105 case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI:
1106 return X86::SIL;
1107 case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI:
1108 return X86::DIL;
1109 case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP:
1110 return X86::BPL;
1111 case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP:
1112 return X86::SPL;
1113 case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8:
1114 return X86::R8B;
1115 case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9:
1116 return X86::R9B;
1117 case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10:
1118 return X86::R10B;
1119 case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11:
1120 return X86::R11B;
1121 case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12:
1122 return X86::R12B;
1123 case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13:
1124 return X86::R13B;
1125 case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14:
1126 return X86::R14B;
1127 case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15:
1128 return X86::R15B;
1131 case MVT::i16:
1132 switch (Reg) {
1133 default: return Reg;
1134 case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
1135 return X86::AX;
1136 case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
1137 return X86::DX;
1138 case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
1139 return X86::CX;
1140 case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
1141 return X86::BX;
1142 case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI:
1143 return X86::SI;
1144 case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI:
1145 return X86::DI;
1146 case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP:
1147 return X86::BP;
1148 case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP:
1149 return X86::SP;
1150 case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8:
1151 return X86::R8W;
1152 case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9:
1153 return X86::R9W;
1154 case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10:
1155 return X86::R10W;
1156 case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11:
1157 return X86::R11W;
1158 case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12:
1159 return X86::R12W;
1160 case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13:
1161 return X86::R13W;
1162 case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14:
1163 return X86::R14W;
1164 case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15:
1165 return X86::R15W;
1167 case MVT::i32:
1168 switch (Reg) {
1169 default: return Reg;
1170 case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
1171 return X86::EAX;
1172 case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
1173 return X86::EDX;
1174 case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
1175 return X86::ECX;
1176 case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
1177 return X86::EBX;
1178 case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI:
1179 return X86::ESI;
1180 case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI:
1181 return X86::EDI;
1182 case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP:
1183 return X86::EBP;
1184 case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP:
1185 return X86::ESP;
1186 case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8:
1187 return X86::R8D;
1188 case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9:
1189 return X86::R9D;
1190 case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10:
1191 return X86::R10D;
1192 case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11:
1193 return X86::R11D;
1194 case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12:
1195 return X86::R12D;
1196 case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13:
1197 return X86::R13D;
1198 case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14:
1199 return X86::R14D;
1200 case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15:
1201 return X86::R15D;
1203 case MVT::i64:
1204 switch (Reg) {
1205 default: return Reg;
1206 case X86::AH: case X86::AL: case X86::AX: case X86::EAX: case X86::RAX:
1207 return X86::RAX;
1208 case X86::DH: case X86::DL: case X86::DX: case X86::EDX: case X86::RDX:
1209 return X86::RDX;
1210 case X86::CH: case X86::CL: case X86::CX: case X86::ECX: case X86::RCX:
1211 return X86::RCX;
1212 case X86::BH: case X86::BL: case X86::BX: case X86::EBX: case X86::RBX:
1213 return X86::RBX;
1214 case X86::SIL: case X86::SI: case X86::ESI: case X86::RSI:
1215 return X86::RSI;
1216 case X86::DIL: case X86::DI: case X86::EDI: case X86::RDI:
1217 return X86::RDI;
1218 case X86::BPL: case X86::BP: case X86::EBP: case X86::RBP:
1219 return X86::RBP;
1220 case X86::SPL: case X86::SP: case X86::ESP: case X86::RSP:
1221 return X86::RSP;
1222 case X86::R8B: case X86::R8W: case X86::R8D: case X86::R8:
1223 return X86::R8;
1224 case X86::R9B: case X86::R9W: case X86::R9D: case X86::R9:
1225 return X86::R9;
1226 case X86::R10B: case X86::R10W: case X86::R10D: case X86::R10:
1227 return X86::R10;
1228 case X86::R11B: case X86::R11W: case X86::R11D: case X86::R11:
1229 return X86::R11;
1230 case X86::R12B: case X86::R12W: case X86::R12D: case X86::R12:
1231 return X86::R12;
1232 case X86::R13B: case X86::R13W: case X86::R13D: case X86::R13:
1233 return X86::R13;
1234 case X86::R14B: case X86::R14W: case X86::R14D: case X86::R14:
1235 return X86::R14;
1236 case X86::R15B: case X86::R15W: case X86::R15D: case X86::R15:
1237 return X86::R15;
1241 return Reg;
1245 #include "X86GenRegisterInfo.inc"
1247 namespace {
1248 struct VISIBILITY_HIDDEN MSAC : public MachineFunctionPass {
1249 static char ID;
1250 MSAC() : MachineFunctionPass(&ID) {}
1252 virtual bool runOnMachineFunction(MachineFunction &MF) {
1253 MachineFrameInfo *FFI = MF.getFrameInfo();
1254 MachineRegisterInfo &RI = MF.getRegInfo();
1256 // Calculate max stack alignment of all already allocated stack objects.
1257 unsigned MaxAlign = calculateMaxStackAlignment(FFI);
1259 // Be over-conservative: scan over all vreg defs and find, whether vector
1260 // registers are used. If yes - there is probability, that vector register
1261 // will be spilled and thus stack needs to be aligned properly.
1262 for (unsigned RegNum = TargetRegisterInfo::FirstVirtualRegister;
1263 RegNum < RI.getLastVirtReg(); ++RegNum)
1264 MaxAlign = std::max(MaxAlign, RI.getRegClass(RegNum)->getAlignment());
1266 FFI->setMaxAlignment(MaxAlign);
1268 return false;
1271 virtual const char *getPassName() const {
1272 return "X86 Maximal Stack Alignment Calculator";
1276 char MSAC::ID = 0;
1279 FunctionPass*
1280 llvm::createX86MaxStackAlignmentCalculatorPass() { return new MSAC(); }