131d157ae1ac2cd9c787dc3c1d28e64c682803844Jia Liu//===-- X86FrameLowering.cpp - X86 Frame Information ----------------------===// 233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov// 333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov// The LLVM Compiler Infrastructure 433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov// 533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov// This file is distributed under the University of Illinois Open Source 633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov// License. See LICENSE.TXT for details. 733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov// 833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov//===----------------------------------------------------------------------===// 933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov// 1016c29b5f285f375be53dabaa73e3e91107485fe4Anton Korobeynikov// This file contains the X86 implementation of TargetFrameLowering class. 1133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov// 1233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov//===----------------------------------------------------------------------===// 1333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 1416c29b5f285f375be53dabaa73e3e91107485fe4Anton Korobeynikov#include "X86FrameLowering.h" 1533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov#include "X86InstrBuilder.h" 1633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov#include "X86InstrInfo.h" 1733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov#include "X86MachineFunctionInfo.h" 1876927d758657b3a511c73467ec5a7288795c1513Rafael Espindola#include "X86Subtarget.h" 19d9e3385ced2dc887e2fe8e1c071bd2611e4d3edeAnton Korobeynikov#include "X86TargetMachine.h" 20d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/SmallSet.h" 2133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov#include "llvm/CodeGen/MachineFrameInfo.h" 2233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov#include "llvm/CodeGen/MachineFunction.h" 2333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov#include "llvm/CodeGen/MachineInstrBuilder.h" 2433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov#include "llvm/CodeGen/MachineModuleInfo.h" 2533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov#include "llvm/CodeGen/MachineRegisterInfo.h" 260b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DataLayout.h" 270b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Function.h" 28f0adba9a7ec8a3031876575a6ffb7db5f1b6f855Rafael Espindola#include "llvm/MC/MCAsmInfo.h" 296a6b8c3e96b9e1ca7092eafd0cfb219cbbfbdfc4Bill Wendling#include "llvm/MC/MCSymbol.h" 3033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov#include "llvm/Support/CommandLine.h" 31d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Target/TargetOptions.h" 3233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 3333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikovusing namespace llvm; 3433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 3533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov// FIXME: completely move here. 3633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikovextern cl::opt<bool> ForceStackAlign; 3733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 3816c29b5f285f375be53dabaa73e3e91107485fe4Anton Korobeynikovbool X86FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { 39d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov return !MF.getFrameInfo()->hasVarSizedObjects(); 40d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov} 41d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov 42d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov/// hasFP - Return true if the specified function should have a dedicated frame 43d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov/// pointer register. This is true if the function has variable sized allocas 44d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov/// or if frame pointer elimination is disabled. 4516c29b5f285f375be53dabaa73e3e91107485fe4Anton Korobeynikovbool X86FrameLowering::hasFP(const MachineFunction &MF) const { 46d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov const MachineFrameInfo *MFI = MF.getFrameInfo(); 47d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov const MachineModuleInfo &MMI = MF.getMMI(); 483fb6eca0cd4c2b31b06543ca2ac60b9c91c2264dChad Rosier const TargetRegisterInfo *RegInfo = TM.getRegisterInfo(); 49d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov 508a8d479214745c82ef00f08d4e4f1c173b5f9ce2Nick Lewycky return (MF.getTarget().Options.DisableFramePointerElim(MF) || 513fb6eca0cd4c2b31b06543ca2ac60b9c91c2264dChad Rosier RegInfo->needsStackRealignment(MF) || 52d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov MFI->hasVarSizedObjects() || 53b56606274d43c7a3e01b18a08d1115fbf2889996Chad Rosier MFI->isFrameAddressTaken() || MF.hasMSInlineAsm() || 54d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov MF.getInfo<X86MachineFunctionInfo>()->getForceFramePointer() || 55e208c491726bb1efbfc4fc05a9f73ad808432979Jakob Stoklund Olesen MMI.callsUnwindInit() || MMI.callsEHReturn()); 56d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov} 57d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov 58700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Benderskystatic unsigned getSUBriOpcode(unsigned IsLP64, int64_t Imm) { 59700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky if (IsLP64) { 6033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (isInt<8>(Imm)) 6133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov return X86::SUB64ri8; 6233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov return X86::SUB64ri32; 6333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } else { 6433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (isInt<8>(Imm)) 6533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov return X86::SUB32ri8; 6633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov return X86::SUB32ri; 6733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 6833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov} 6933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 7016221a60a00e52b078f6164ba4475c6e8e918e4bEli Benderskystatic unsigned getADDriOpcode(unsigned IsLP64, int64_t Imm) { 7116221a60a00e52b078f6164ba4475c6e8e918e4bEli Bendersky if (IsLP64) { 7233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (isInt<8>(Imm)) 7333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov return X86::ADD64ri8; 7433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov return X86::ADD64ri32; 7533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } else { 7633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (isInt<8>(Imm)) 7733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov return X86::ADD32ri8; 7833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov return X86::ADD32ri; 7933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 8033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov} 8133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 8216221a60a00e52b078f6164ba4475c6e8e918e4bEli Benderskystatic unsigned getLEArOpcode(unsigned IsLP64) { 8316221a60a00e52b078f6164ba4475c6e8e918e4bEli Bendersky return IsLP64 ? X86::LEA64r : X86::LEA32r; 84de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng} 85de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng 867158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng/// findDeadCallerSavedReg - Return a caller-saved register that isn't live 877158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng/// when it reaches the "return" instruction. We can then pop a stack object 887158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng/// to this register without worry about clobbering it. 897158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Chengstatic unsigned findDeadCallerSavedReg(MachineBasicBlock &MBB, 907158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng MachineBasicBlock::iterator &MBBI, 917158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng const TargetRegisterInfo &TRI, 927158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng bool Is64Bit) { 937158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng const MachineFunction *MF = MBB.getParent(); 947158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng const Function *F = MF->getFunction(); 957158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng if (!F || MF->getMMI().callsEHReturn()) 967158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng return 0; 977158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng 98e4fd907e72a599eddfa7a81eac4366b5b82523e3Craig Topper static const uint16_t CallerSavedRegs32Bit[] = { 9932a183c84ad0cbe492119c37f1a7941ace61dd79Andrew Trick X86::EAX, X86::EDX, X86::ECX, 0 1007158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng }; 1017158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng 102e4fd907e72a599eddfa7a81eac4366b5b82523e3Craig Topper static const uint16_t CallerSavedRegs64Bit[] = { 1037158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI, 10432a183c84ad0cbe492119c37f1a7941ace61dd79Andrew Trick X86::R8, X86::R9, X86::R10, X86::R11, 0 1057158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng }; 1067158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng 1077158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng unsigned Opc = MBBI->getOpcode(); 1087158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng switch (Opc) { 1097158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng default: return 0; 1107158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng case X86::RET: 1117158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng case X86::RETI: 1127158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng case X86::TCRETURNdi: 1137158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng case X86::TCRETURNri: 1147158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng case X86::TCRETURNmi: 1157158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng case X86::TCRETURNdi64: 1167158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng case X86::TCRETURNri64: 1177158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng case X86::TCRETURNmi64: 1187158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng case X86::EH_RETURN: 1197158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng case X86::EH_RETURN64: { 120e4fd907e72a599eddfa7a81eac4366b5b82523e3Craig Topper SmallSet<uint16_t, 8> Uses; 1217158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng for (unsigned i = 0, e = MBBI->getNumOperands(); i != e; ++i) { 1227158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng MachineOperand &MO = MBBI->getOperand(i); 1237158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng if (!MO.isReg() || MO.isDef()) 1247158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng continue; 1257158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng unsigned Reg = MO.getReg(); 1267158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng if (!Reg) 1277158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng continue; 128396618b43a85e12d290a90b181c6af5d7c0c5f11Jakob Stoklund Olesen for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI) 129396618b43a85e12d290a90b181c6af5d7c0c5f11Jakob Stoklund Olesen Uses.insert(*AI); 1307158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng } 1317158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng 132e4fd907e72a599eddfa7a81eac4366b5b82523e3Craig Topper const uint16_t *CS = Is64Bit ? CallerSavedRegs64Bit : CallerSavedRegs32Bit; 1337158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng for (; *CS; ++CS) 1347158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng if (!Uses.count(*CS)) 1357158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng return *CS; 1367158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng } 1377158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng } 1387158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng 1397158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng return 0; 1407158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng} 1417158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng 1427158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng 14333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov/// emitSPUpdate - Emit a series of instructions to increment / decrement the 14433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov/// stack pointer by a constant value. 14533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikovstatic 14633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikovvoid emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, 1477158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng unsigned StackPtr, int64_t NumBytes, 1482a1b60d791522d73be91d4281c90d25bd5e3d117Eli Bendersky bool Is64Bit, bool IsLP64, bool UseLEA, 14976ad43c6e1619ed4c087b8ccb2cd573eb9d7093eEric Christopher const TargetInstrInfo &TII, const TargetRegisterInfo &TRI) { 15033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov bool isSub = NumBytes < 0; 15133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov uint64_t Offset = isSub ? -NumBytes : NumBytes; 152de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng unsigned Opc; 153de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng if (UseLEA) 15416221a60a00e52b078f6164ba4475c6e8e918e4bEli Bendersky Opc = getLEArOpcode(IsLP64); 155de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng else 156de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng Opc = isSub 1572a1b60d791522d73be91d4281c90d25bd5e3d117Eli Bendersky ? getSUBriOpcode(IsLP64, Offset) 1582a1b60d791522d73be91d4281c90d25bd5e3d117Eli Bendersky : getADDriOpcode(IsLP64, Offset); 159de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng 16033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov uint64_t Chunk = (1LL << 31) - 1; 16176ad43c6e1619ed4c087b8ccb2cd573eb9d7093eEric Christopher DebugLoc DL = MBB.findDebugLoc(MBBI); 16233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 16333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov while (Offset) { 16433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov uint64_t ThisVal = (Offset > Chunk) ? Chunk : Offset; 1657158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng if (ThisVal == (Is64Bit ? 8 : 4)) { 1667158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng // Use push / pop instead. 1677158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng unsigned Reg = isSub 1681e08cd1eaef8acbcfaf7db48d859a29583c29897Dale Johannesen ? (unsigned)(Is64Bit ? X86::RAX : X86::EAX) 1697158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng : findDeadCallerSavedReg(MBB, MBBI, TRI, Is64Bit); 1707158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng if (Reg) { 1717158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng Opc = isSub 1727158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng ? (Is64Bit ? X86::PUSH64r : X86::PUSH32r) 1737158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng : (Is64Bit ? X86::POP64r : X86::POP32r); 174aff232a5941c9ffb7ad52e08f81ad53794fed56bCharles Davis MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(Opc)) 1757158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng .addReg(Reg, getDefRegState(!isSub) | getUndefRegState(isSub)); 176aff232a5941c9ffb7ad52e08f81ad53794fed56bCharles Davis if (isSub) 177aff232a5941c9ffb7ad52e08f81ad53794fed56bCharles Davis MI->setFlag(MachineInstr::FrameSetup); 1787158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng Offset -= ThisVal; 1797158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng continue; 1807158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng } 1817158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng } 1827158e08b8e619f4dcac9834c57f5f8afd6eea2ebEvan Cheng 183de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng MachineInstr *MI = NULL; 184de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng 185de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng if (UseLEA) { 186de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng MI = addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr), 187de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng StackPtr, false, isSub ? -ThisVal : ThisVal); 188de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng } else { 189de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng MI = BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr) 190de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng .addReg(StackPtr) 191de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng .addImm(ThisVal); 192de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng MI->getOperand(3).setIsDead(); // The EFLAGS implicit def is dead. 193de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng } 194de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng 195aff232a5941c9ffb7ad52e08f81ad53794fed56bCharles Davis if (isSub) 196aff232a5941c9ffb7ad52e08f81ad53794fed56bCharles Davis MI->setFlag(MachineInstr::FrameSetup); 197de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng 19833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov Offset -= ThisVal; 19933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 20033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov} 20133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 20233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov/// mergeSPUpdatesUp - Merge two stack-manipulating instructions upper iterator. 20333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikovstatic 20433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikovvoid mergeSPUpdatesUp(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, 20533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov unsigned StackPtr, uint64_t *NumBytes = NULL) { 20633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (MBBI == MBB.begin()) return; 20733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 20833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MachineBasicBlock::iterator PI = prior(MBBI); 20933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov unsigned Opc = PI->getOpcode(); 21033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 || 211de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng Opc == X86::ADD32ri || Opc == X86::ADD32ri8 || 212de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng Opc == X86::LEA32r || Opc == X86::LEA64_32r) && 21333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov PI->getOperand(0).getReg() == StackPtr) { 21433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (NumBytes) 21533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov *NumBytes += PI->getOperand(2).getImm(); 21633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MBB.erase(PI); 21733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 || 21833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov Opc == X86::SUB32ri || Opc == X86::SUB32ri8) && 21933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov PI->getOperand(0).getReg() == StackPtr) { 22033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (NumBytes) 22133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov *NumBytes -= PI->getOperand(2).getImm(); 22233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MBB.erase(PI); 22333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 22433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov} 22533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 22633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov/// mergeSPUpdatesDown - Merge two stack-manipulating instructions lower iterator. 22733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikovstatic 22833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikovvoid mergeSPUpdatesDown(MachineBasicBlock &MBB, 22933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MachineBasicBlock::iterator &MBBI, 23033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov unsigned StackPtr, uint64_t *NumBytes = NULL) { 231fc9261279aa140542cf9b7c2c384d000ad97aca0Sanjoy Das // FIXME: THIS ISN'T RUN!!! 23233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov return; 23333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 23433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (MBBI == MBB.end()) return; 23533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 23633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MachineBasicBlock::iterator NI = llvm::next(MBBI); 23733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (NI == MBB.end()) return; 23833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 23933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov unsigned Opc = NI->getOpcode(); 24033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 || 24133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov Opc == X86::ADD32ri || Opc == X86::ADD32ri8) && 24233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov NI->getOperand(0).getReg() == StackPtr) { 24333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (NumBytes) 24433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov *NumBytes -= NI->getOperand(2).getImm(); 24533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MBB.erase(NI); 24633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MBBI = NI; 24733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 || 24833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov Opc == X86::SUB32ri || Opc == X86::SUB32ri8) && 24933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov NI->getOperand(0).getReg() == StackPtr) { 25033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (NumBytes) 25133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov *NumBytes += NI->getOperand(2).getImm(); 25233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MBB.erase(NI); 25333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MBBI = NI; 25433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 25533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov} 25633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 25733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov/// mergeSPUpdates - Checks the instruction before/after the passed 258de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng/// instruction. If it is an ADD/SUB/LEA instruction it is deleted argument and the 259de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng/// stack adjustment is returned as a positive value for ADD/LEA and a negative for 26033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov/// SUB. 26133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikovstatic int mergeSPUpdates(MachineBasicBlock &MBB, 26233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MachineBasicBlock::iterator &MBBI, 26333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov unsigned StackPtr, 26433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov bool doMergeWithPrevious) { 26533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if ((doMergeWithPrevious && MBBI == MBB.begin()) || 26633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov (!doMergeWithPrevious && MBBI == MBB.end())) 26733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov return 0; 26833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 26933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MachineBasicBlock::iterator PI = doMergeWithPrevious ? prior(MBBI) : MBBI; 27033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MachineBasicBlock::iterator NI = doMergeWithPrevious ? 0 : llvm::next(MBBI); 27133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov unsigned Opc = PI->getOpcode(); 27233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov int Offset = 0; 27333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 27433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 || 275de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng Opc == X86::ADD32ri || Opc == X86::ADD32ri8 || 276de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng Opc == X86::LEA32r || Opc == X86::LEA64_32r) && 27733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov PI->getOperand(0).getReg() == StackPtr){ 27833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov Offset += PI->getOperand(2).getImm(); 27933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MBB.erase(PI); 28033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (!doMergeWithPrevious) MBBI = NI; 28133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 || 28233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov Opc == X86::SUB32ri || Opc == X86::SUB32ri8) && 28333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov PI->getOperand(0).getReg() == StackPtr) { 28433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov Offset -= PI->getOperand(2).getImm(); 28533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MBB.erase(PI); 28633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (!doMergeWithPrevious) MBBI = NI; 28733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 28833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 28933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov return Offset; 29033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov} 29133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 29233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikovstatic bool isEAXLiveIn(MachineFunction &MF) { 29333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov for (MachineRegisterInfo::livein_iterator II = MF.getRegInfo().livein_begin(), 29433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov EE = MF.getRegInfo().livein_end(); II != EE; ++II) { 29533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov unsigned Reg = II->first; 29633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 29733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (Reg == X86::EAX || Reg == X86::AX || 29833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov Reg == X86::AH || Reg == X86::AL) 29933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov return true; 30033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 30133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 30233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov return false; 30333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov} 30433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 30516c29b5f285f375be53dabaa73e3e91107485fe4Anton Korobeynikovvoid X86FrameLowering::emitCalleeSavedFrameMoves(MachineFunction &MF, 30609b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling MCSymbol *Label, 30709b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling unsigned FramePtr) const { 30833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MachineFrameInfo *MFI = MF.getFrameInfo(); 30933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MachineModuleInfo &MMI = MF.getMMI(); 31099cb622041a0839c7dfcf0263c5102a305a0fdb5Bill Wendling const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo(); 31133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 31233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Add callee saved registers to move list. 31333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); 31433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (CSI.empty()) return; 31533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 316aa3c2c09d9d5bc67c6ca2fbc6697257b15476684Michael Liao const X86RegisterInfo *RegInfo = TM.getRegisterInfo(); 317d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov bool HasFP = hasFP(MF); 31833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 31933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Calculate amount of bytes used for return address storing. 320aa3c2c09d9d5bc67c6ca2fbc6697257b15476684Michael Liao int stackGrowth = -RegInfo->getSlotSize(); 32133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 32233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // FIXME: This is dirty hack. The code itself is pretty mess right now. 32333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // It should be rewritten from scratch and generalized sometimes. 32433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 3257a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner // Determine maximum offset (minimum due to stack growth). 32633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov int64_t MaxOffset = 0; 32733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov for (std::vector<CalleeSavedInfo>::const_iterator 32833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov I = CSI.begin(), E = CSI.end(); I != E; ++I) 32933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MaxOffset = std::min(MaxOffset, 33033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MFI->getObjectOffset(I->getFrameIdx())); 33133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 33233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Calculate offsets. 33333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov int64_t saveAreaOffset = (HasFP ? 3 : 2) * stackGrowth; 33433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov for (std::vector<CalleeSavedInfo>::const_iterator 33533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov I = CSI.begin(), E = CSI.end(); I != E; ++I) { 33633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov int64_t Offset = MFI->getObjectOffset(I->getFrameIdx()); 33733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov unsigned Reg = I->getReg(); 33833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov Offset = MaxOffset - Offset + saveAreaOffset; 33933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 34033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Don't output a new machine move if we're re-saving the frame 34133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // pointer. This happens when the PrologEpilogInserter has inserted an extra 34233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // "PUSH" of the frame pointer -- the "emitPrologue" method automatically 34333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // generates one when frame pointers are used. If we generate a "machine 34433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // move" for this extra "PUSH", the linker will lose track of the fact that 34533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // the frame pointer should have the value of the first "PUSH" when it's 34633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // trying to unwind. 3472763538609fd455d63c192b320c73fb5d48c3e47NAKAMURA Takumi // 34833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // FIXME: This looks inelegant. It's possibly correct, but it's covering up 34933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // another bug. I.e., one where we generate a prolog like this: 35033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // 35133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // pushl %ebp 35233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // movl %esp, %ebp 35333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // pushl %ebp 35433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // pushl %esi 35533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // ... 35633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // 35733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // The immediate re-push of EBP is unnecessary. At the least, it's an 35833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // optimization bug. EBP can be used as a scratch register in certain 35933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // cases, but probably not when we have a frame pointer. 36033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (HasFP && FramePtr == Reg) 36133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov continue; 36233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 36399cb622041a0839c7dfcf0263c5102a305a0fdb5Bill Wendling unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true); 3646b67ffd68bb2e555b1b512a809f3c82c68f3debeRafael Espindola MMI.addFrameInst(MCCFIInstruction::createOffset(Label, DwarfReg, Offset)); 36533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 36633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov} 36733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 36809b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling/// getCompactUnwindRegNum - Get the compact unwind number for a given 36909b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling/// register. The number corresponds to the enum lists in 37009b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling/// compact_unwind_encoding.h. 3711f4b796b49d13075531ed43b35824ecc9d757467Bill Wendlingstatic int getCompactUnwindRegNum(unsigned Reg, bool is64Bit) { 3721f4b796b49d13075531ed43b35824ecc9d757467Bill Wendling static const uint16_t CU32BitRegs[] = { 3731f4b796b49d13075531ed43b35824ecc9d757467Bill Wendling X86::EBX, X86::ECX, X86::EDX, X86::EDI, X86::ESI, X86::EBP, 0 3741f4b796b49d13075531ed43b35824ecc9d757467Bill Wendling }; 3751f4b796b49d13075531ed43b35824ecc9d757467Bill Wendling static const uint16_t CU64BitRegs[] = { 3761f4b796b49d13075531ed43b35824ecc9d757467Bill Wendling X86::RBX, X86::R12, X86::R13, X86::R14, X86::R15, X86::RBP, 0 3771f4b796b49d13075531ed43b35824ecc9d757467Bill Wendling }; 3781f4b796b49d13075531ed43b35824ecc9d757467Bill Wendling const uint16_t *CURegs = is64Bit ? CU64BitRegs : CU32BitRegs; 37910e412ec6b761952b95710203e6d2d89f4fee53aBill Wendling for (int Idx = 1; *CURegs; ++CURegs, ++Idx) 38009b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling if (*CURegs == Reg) 38109b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling return Idx; 38209b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 38309b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling return -1; 38409b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling} 38509b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 38657a3cd2f326269dd1431cf430db50239a93450f2Bill Wendling// Number of registers that can be saved in a compact unwind encoding. 38757a3cd2f326269dd1431cf430db50239a93450f2Bill Wendling#define CU_NUM_SAVED_REGS 6 38857a3cd2f326269dd1431cf430db50239a93450f2Bill Wendling 38909b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling/// encodeCompactUnwindRegistersWithoutFrame - Create the permutation encoding 39009b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling/// used with frameless stacks. It is passed the number of registers to be saved 39109b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling/// and an array of the registers saved. 39257a3cd2f326269dd1431cf430db50239a93450f2Bill Wendlingstatic uint32_t 39357a3cd2f326269dd1431cf430db50239a93450f2Bill WendlingencodeCompactUnwindRegistersWithoutFrame(unsigned SavedRegs[CU_NUM_SAVED_REGS], 39457a3cd2f326269dd1431cf430db50239a93450f2Bill Wendling unsigned RegCount, bool Is64Bit) { 39509b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // The saved registers are numbered from 1 to 6. In order to encode the order 39609b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // in which they were saved, we re-number them according to their place in the 39709b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // register order. The re-numbering is relative to the last re-numbered 39809b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // register. E.g., if we have registers {6, 2, 4, 5} saved in that order: 39909b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // 40009b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // Orig Re-Num 40109b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // ---- ------ 40209b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // 6 6 40309b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // 2 2 40409b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // 4 3 40509b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // 5 3 40609b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // 40710e412ec6b761952b95710203e6d2d89f4fee53aBill Wendling for (unsigned i = 0; i != CU_NUM_SAVED_REGS; ++i) { 4081f4b796b49d13075531ed43b35824ecc9d757467Bill Wendling int CUReg = getCompactUnwindRegNum(SavedRegs[i], Is64Bit); 40909b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling if (CUReg == -1) return ~0U; 41009b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling SavedRegs[i] = CUReg; 41179df986c60094e9ea29f081295aea08b1680a999Bill Wendling } 41209b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 41310e412ec6b761952b95710203e6d2d89f4fee53aBill Wendling // Reverse the list. 41410e412ec6b761952b95710203e6d2d89f4fee53aBill Wendling std::swap(SavedRegs[0], SavedRegs[5]); 41510e412ec6b761952b95710203e6d2d89f4fee53aBill Wendling std::swap(SavedRegs[1], SavedRegs[4]); 41610e412ec6b761952b95710203e6d2d89f4fee53aBill Wendling std::swap(SavedRegs[2], SavedRegs[3]); 41710e412ec6b761952b95710203e6d2d89f4fee53aBill Wendling 41857a3cd2f326269dd1431cf430db50239a93450f2Bill Wendling uint32_t RenumRegs[CU_NUM_SAVED_REGS]; 41957a3cd2f326269dd1431cf430db50239a93450f2Bill Wendling for (unsigned i = CU_NUM_SAVED_REGS - RegCount; i < CU_NUM_SAVED_REGS; ++i) { 42009b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling unsigned Countless = 0; 42157a3cd2f326269dd1431cf430db50239a93450f2Bill Wendling for (unsigned j = CU_NUM_SAVED_REGS - RegCount; j < i; ++j) 42209b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling if (SavedRegs[j] < SavedRegs[i]) 42309b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling ++Countless; 42409b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 42509b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling RenumRegs[i] = SavedRegs[i] - Countless - 1; 42609b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling } 42709b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 42809b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // Take the renumbered values and encode them into a 10-bit number. 42909b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling uint32_t permutationEncoding = 0; 43009b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling switch (RegCount) { 43109b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling case 6: 43209b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling permutationEncoding |= 120 * RenumRegs[0] + 24 * RenumRegs[1] 43309b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling + 6 * RenumRegs[2] + 2 * RenumRegs[3] 43409b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling + RenumRegs[4]; 43509b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling break; 43609b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling case 5: 43709b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling permutationEncoding |= 120 * RenumRegs[1] + 24 * RenumRegs[2] 43809b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling + 6 * RenumRegs[3] + 2 * RenumRegs[4] 43909b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling + RenumRegs[5]; 44009b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling break; 44109b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling case 4: 44209b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling permutationEncoding |= 60 * RenumRegs[2] + 12 * RenumRegs[3] 44309b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling + 3 * RenumRegs[4] + RenumRegs[5]; 44409b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling break; 44509b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling case 3: 44609b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling permutationEncoding |= 20 * RenumRegs[3] + 4 * RenumRegs[4] 44709b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling + RenumRegs[5]; 44809b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling break; 44909b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling case 2: 45009b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling permutationEncoding |= 5 * RenumRegs[4] + RenumRegs[5]; 45109b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling break; 45209b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling case 1: 45309b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling permutationEncoding |= RenumRegs[5]; 45409b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling break; 45509b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling } 45609b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 45709b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling assert((permutationEncoding & 0x3FF) == permutationEncoding && 45809b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling "Invalid compact register encoding!"); 45909b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling return permutationEncoding; 46009b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling} 46109b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 46209b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling/// encodeCompactUnwindRegistersWithFrame - Return the registers encoded for a 46309b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling/// compact encoding with a frame pointer. 46457a3cd2f326269dd1431cf430db50239a93450f2Bill Wendlingstatic uint32_t 46557a3cd2f326269dd1431cf430db50239a93450f2Bill WendlingencodeCompactUnwindRegistersWithFrame(unsigned SavedRegs[CU_NUM_SAVED_REGS], 46657a3cd2f326269dd1431cf430db50239a93450f2Bill Wendling bool Is64Bit) { 46709b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // Encode the registers in the order they were saved, 3-bits per register. The 46886b1a7d61413aed40a68f98f1e8f17fd79ebd7a2Bill Wendling // registers are numbered from 1 to CU_NUM_SAVED_REGS. 46909b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling uint32_t RegEnc = 0; 470b4ee5168abd0580a29f5c9becce26e3ea7bb2b8dBill Wendling for (int I = CU_NUM_SAVED_REGS - 1, Idx = 0; I != -1; --I) { 47109b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling unsigned Reg = SavedRegs[I]; 47286b1a7d61413aed40a68f98f1e8f17fd79ebd7a2Bill Wendling if (Reg == 0) continue; 47386b1a7d61413aed40a68f98f1e8f17fd79ebd7a2Bill Wendling 4741f4b796b49d13075531ed43b35824ecc9d757467Bill Wendling int CURegNum = getCompactUnwindRegNum(Reg, Is64Bit); 47586b1a7d61413aed40a68f98f1e8f17fd79ebd7a2Bill Wendling if (CURegNum == -1) return ~0U; 47680caf9c2737d4f1bf5bae3a283fe9d538f5e2970Bill Wendling 47780caf9c2737d4f1bf5bae3a283fe9d538f5e2970Bill Wendling // Encode the 3-bit register number in order, skipping over 3-bits for each 47880caf9c2737d4f1bf5bae3a283fe9d538f5e2970Bill Wendling // register. 47986b1a7d61413aed40a68f98f1e8f17fd79ebd7a2Bill Wendling RegEnc |= (CURegNum & 0x7) << (Idx++ * 3); 48009b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling } 48109b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 482dec1f996152d4292133e81527ad710fbc1280946Jakob Stoklund Olesen assert((RegEnc & 0x3FFFF) == RegEnc && "Invalid compact register encoding!"); 48309b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling return RegEnc; 48409b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling} 48509b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 48609b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendlinguint32_t X86FrameLowering::getCompactUnwindEncoding(MachineFunction &MF) const { 48709b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling const X86RegisterInfo *RegInfo = TM.getRegisterInfo(); 48809b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling unsigned FramePtr = RegInfo->getFrameRegister(MF); 48909b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling unsigned StackPtr = RegInfo->getStackRegister(); 49009b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 49109b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling bool Is64Bit = STI.is64Bit(); 49209b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling bool HasFP = hasFP(MF); 49309b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 49457a3cd2f326269dd1431cf430db50239a93450f2Bill Wendling unsigned SavedRegs[CU_NUM_SAVED_REGS] = { 0, 0, 0, 0, 0, 0 }; 49510e412ec6b761952b95710203e6d2d89f4fee53aBill Wendling unsigned SavedRegIdx = 0; 49609b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 49709b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling unsigned OffsetSize = (Is64Bit ? 8 : 4); 49809b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 49909b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling unsigned PushInstr = (Is64Bit ? X86::PUSH64r : X86::PUSH32r); 50009b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling unsigned PushInstrSize = 1; 50109b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling unsigned MoveInstr = (Is64Bit ? X86::MOV64rr : X86::MOV32rr); 50209b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling unsigned MoveInstrSize = (Is64Bit ? 3 : 2); 50309b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling unsigned SubtractInstrIdx = (Is64Bit ? 3 : 2); 50409b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 505de77055a68a3fc495e01b682f00059af3e38822eBill Wendling unsigned StackDivide = (Is64Bit ? 8 : 4); 506de77055a68a3fc495e01b682f00059af3e38822eBill Wendling 50709b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling unsigned InstrOffset = 0; 50809b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling unsigned StackAdjust = 0; 50957a3cd2f326269dd1431cf430db50239a93450f2Bill Wendling unsigned StackSize = 0; 51009b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 51109b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling MachineBasicBlock &MBB = MF.front(); // Prologue is in entry BB. 51209b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling bool ExpectEnd = false; 51309b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling for (MachineBasicBlock::iterator 51409b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling MBBI = MBB.begin(), MBBE = MBB.end(); MBBI != MBBE; ++MBBI) { 51509b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling MachineInstr &MI = *MBBI; 51609b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling unsigned Opc = MI.getOpcode(); 51709b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling if (Opc == X86::PROLOG_LABEL) continue; 51809b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling if (!MI.getFlag(MachineInstr::FrameSetup)) break; 51909b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 52009b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // We don't exect any more prolog instructions. 52189ec1c5c9c744c125b61145ed59783eb5c68ebf8Bill Wendling if (ExpectEnd) return CU::UNWIND_MODE_DWARF; 52209b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 52309b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling if (Opc == PushInstr) { 52409b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // If there are too many saved registers, we cannot use compact encoding. 52589ec1c5c9c744c125b61145ed59783eb5c68ebf8Bill Wendling if (SavedRegIdx >= CU_NUM_SAVED_REGS) return CU::UNWIND_MODE_DWARF; 52609b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 527edfef3bd27d6269d473fbc570e8c2be02b4070dfBill Wendling unsigned Reg = MI.getOperand(0).getReg(); 528edfef3bd27d6269d473fbc570e8c2be02b4070dfBill Wendling if (Reg == (Is64Bit ? X86::RAX : X86::EAX)) { 529edfef3bd27d6269d473fbc570e8c2be02b4070dfBill Wendling ExpectEnd = true; 530edfef3bd27d6269d473fbc570e8c2be02b4070dfBill Wendling continue; 531edfef3bd27d6269d473fbc570e8c2be02b4070dfBill Wendling } 532edfef3bd27d6269d473fbc570e8c2be02b4070dfBill Wendling 53310e412ec6b761952b95710203e6d2d89f4fee53aBill Wendling SavedRegs[SavedRegIdx++] = MI.getOperand(0).getReg(); 53457a3cd2f326269dd1431cf430db50239a93450f2Bill Wendling StackAdjust += OffsetSize; 53509b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling InstrOffset += PushInstrSize; 53609b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling } else if (Opc == MoveInstr) { 53709b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling unsigned SrcReg = MI.getOperand(1).getReg(); 53809b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling unsigned DstReg = MI.getOperand(0).getReg(); 53909b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 54009b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling if (DstReg != FramePtr || SrcReg != StackPtr) 54189ec1c5c9c744c125b61145ed59783eb5c68ebf8Bill Wendling return CU::UNWIND_MODE_DWARF; 54209b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 54357a3cd2f326269dd1431cf430db50239a93450f2Bill Wendling StackAdjust = 0; 54409b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling memset(SavedRegs, 0, sizeof(SavedRegs)); 54510e412ec6b761952b95710203e6d2d89f4fee53aBill Wendling SavedRegIdx = 0; 54609b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling InstrOffset += MoveInstrSize; 54784d518af1991f581b748c4d11dbeb1c54573556bBill Wendling } else if (Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 || 54884d518af1991f581b748c4d11dbeb1c54573556bBill Wendling Opc == X86::SUB32ri || Opc == X86::SUB32ri8) { 54957a3cd2f326269dd1431cf430db50239a93450f2Bill Wendling if (StackSize) 55057a3cd2f326269dd1431cf430db50239a93450f2Bill Wendling // We already have a stack size. 55189ec1c5c9c744c125b61145ed59783eb5c68ebf8Bill Wendling return CU::UNWIND_MODE_DWARF; 55209b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 55309b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling if (!MI.getOperand(0).isReg() || 55409b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling MI.getOperand(0).getReg() != MI.getOperand(1).getReg() || 55509b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling MI.getOperand(0).getReg() != StackPtr || !MI.getOperand(2).isImm()) 55609b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // We need this to be a stack adjustment pointer. Something like: 55709b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // 55809b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // %RSP<def> = SUB64ri8 %RSP, 48 55989ec1c5c9c744c125b61145ed59783eb5c68ebf8Bill Wendling return CU::UNWIND_MODE_DWARF; 56009b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 56157a3cd2f326269dd1431cf430db50239a93450f2Bill Wendling StackSize = MI.getOperand(2).getImm() / StackDivide; 56209b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling SubtractInstrIdx += InstrOffset; 56309b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling ExpectEnd = true; 56409b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling } 56509b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling } 56609b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 56709b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // Encode that we are using EBP/RBP as the frame pointer. 56809b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling uint32_t CompactUnwindEncoding = 0; 56957a3cd2f326269dd1431cf430db50239a93450f2Bill Wendling StackAdjust /= StackDivide; 57009b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling if (HasFP) { 57157a3cd2f326269dd1431cf430db50239a93450f2Bill Wendling if ((StackAdjust & 0xFF) != StackAdjust) 57209b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // Offset was too big for compact encoding. 57389ec1c5c9c744c125b61145ed59783eb5c68ebf8Bill Wendling return CU::UNWIND_MODE_DWARF; 57409b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 57509b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // Get the encoding of the saved registers when we have a frame pointer. 57609b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling uint32_t RegEnc = encodeCompactUnwindRegistersWithFrame(SavedRegs, Is64Bit); 57789ec1c5c9c744c125b61145ed59783eb5c68ebf8Bill Wendling if (RegEnc == ~0U) return CU::UNWIND_MODE_DWARF; 57809b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 57989ec1c5c9c744c125b61145ed59783eb5c68ebf8Bill Wendling CompactUnwindEncoding |= CU::UNWIND_MODE_BP_FRAME; 58057a3cd2f326269dd1431cf430db50239a93450f2Bill Wendling CompactUnwindEncoding |= (StackAdjust & 0xFF) << 16; 58189ec1c5c9c744c125b61145ed59783eb5c68ebf8Bill Wendling CompactUnwindEncoding |= RegEnc & CU::UNWIND_BP_FRAME_REGISTERS; 58209b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling } else { 583b3ec329c14fa3d37b5963f188db54ba03c6b97c0Bill Wendling ++StackAdjust; 584b3ec329c14fa3d37b5963f188db54ba03c6b97c0Bill Wendling uint32_t TotalStackSize = StackAdjust + StackSize; 585581ac2723c5cb5182114092591eb5e21ceea2e77Bill Wendling if ((TotalStackSize & 0xFF) == TotalStackSize) { 5865b2c4978ce56455689c515c9d74cc1d92871f3bbBill Wendling // Frameless stack with a small stack size. 58789ec1c5c9c744c125b61145ed59783eb5c68ebf8Bill Wendling CompactUnwindEncoding |= CU::UNWIND_MODE_STACK_IMMD; 5885b2c4978ce56455689c515c9d74cc1d92871f3bbBill Wendling 5895b2c4978ce56455689c515c9d74cc1d92871f3bbBill Wendling // Encode the stack size. 590581ac2723c5cb5182114092591eb5e21ceea2e77Bill Wendling CompactUnwindEncoding |= (TotalStackSize & 0xFF) << 16; 59109b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling } else { 59257a3cd2f326269dd1431cf430db50239a93450f2Bill Wendling if ((StackAdjust & 0x7) != StackAdjust) 59309b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // The extra stack adjustments are too big for us to handle. 59489ec1c5c9c744c125b61145ed59783eb5c68ebf8Bill Wendling return CU::UNWIND_MODE_DWARF; 59509b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 59609b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // Frameless stack with an offset too large for us to encode compactly. 59789ec1c5c9c744c125b61145ed59783eb5c68ebf8Bill Wendling CompactUnwindEncoding |= CU::UNWIND_MODE_STACK_IND; 59809b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 59909b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // Encode the offset to the nnnnnn value in the 'subl $nnnnnn, ESP' 60009b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // instruction. 60109b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling CompactUnwindEncoding |= (SubtractInstrIdx & 0xFF) << 16; 60209b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 60357a3cd2f326269dd1431cf430db50239a93450f2Bill Wendling // Encode any extra stack stack adjustments (done via push instructions). 60457a3cd2f326269dd1431cf430db50239a93450f2Bill Wendling CompactUnwindEncoding |= (StackAdjust & 0x7) << 13; 60509b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling } 60609b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 6075b2c4978ce56455689c515c9d74cc1d92871f3bbBill Wendling // Encode the number of registers saved. 60810e412ec6b761952b95710203e6d2d89f4fee53aBill Wendling CompactUnwindEncoding |= (SavedRegIdx & 0x7) << 10; 60975e14e0ebd29bb2b1893948037c0ae5df4e09a41Bill Wendling 61009b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // Get the encoding of the saved registers when we don't have a frame 61109b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // pointer. 61257a3cd2f326269dd1431cf430db50239a93450f2Bill Wendling uint32_t RegEnc = 61310e412ec6b761952b95710203e6d2d89f4fee53aBill Wendling encodeCompactUnwindRegistersWithoutFrame(SavedRegs, SavedRegIdx, 61457a3cd2f326269dd1431cf430db50239a93450f2Bill Wendling Is64Bit); 61589ec1c5c9c744c125b61145ed59783eb5c68ebf8Bill Wendling if (RegEnc == ~0U) return CU::UNWIND_MODE_DWARF; 6165b2c4978ce56455689c515c9d74cc1d92871f3bbBill Wendling 6175b2c4978ce56455689c515c9d74cc1d92871f3bbBill Wendling // Encode the register encoding. 61889ec1c5c9c744c125b61145ed59783eb5c68ebf8Bill Wendling CompactUnwindEncoding |= 61989ec1c5c9c744c125b61145ed59783eb5c68ebf8Bill Wendling RegEnc & CU::UNWIND_FRAMELESS_STACK_REG_PERMUTATION; 62009b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling } 62109b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 62209b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling return CompactUnwindEncoding; 62309b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling} 62409b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 625677689cf5dc65404645462464682a0696cc84532Nadav Rotem/// usesTheStack - This function checks if any of the users of EFLAGS 626d0696ef8c33b9b2504e89bc0aab2ea99a6c90756Nadav Rotem/// copies the EFLAGS. We know that the code that lowers COPY of EFLAGS has 627d0696ef8c33b9b2504e89bc0aab2ea99a6c90756Nadav Rotem/// to use the stack, and if we don't adjust the stack we clobber the first 628d0696ef8c33b9b2504e89bc0aab2ea99a6c90756Nadav Rotem/// frame index. 629677689cf5dc65404645462464682a0696cc84532Nadav Rotem/// See X86InstrInfo::copyPhysReg. 630677689cf5dc65404645462464682a0696cc84532Nadav Rotemstatic bool usesTheStack(MachineFunction &MF) { 631d0696ef8c33b9b2504e89bc0aab2ea99a6c90756Nadav Rotem MachineRegisterInfo &MRI = MF.getRegInfo(); 632d0696ef8c33b9b2504e89bc0aab2ea99a6c90756Nadav Rotem 633d0696ef8c33b9b2504e89bc0aab2ea99a6c90756Nadav Rotem for (MachineRegisterInfo::reg_iterator ri = MRI.reg_begin(X86::EFLAGS), 634d0696ef8c33b9b2504e89bc0aab2ea99a6c90756Nadav Rotem re = MRI.reg_end(); ri != re; ++ri) 635d0696ef8c33b9b2504e89bc0aab2ea99a6c90756Nadav Rotem if (ri->isCopy()) 636d0696ef8c33b9b2504e89bc0aab2ea99a6c90756Nadav Rotem return true; 637d0696ef8c33b9b2504e89bc0aab2ea99a6c90756Nadav Rotem 638d0696ef8c33b9b2504e89bc0aab2ea99a6c90756Nadav Rotem return false; 639d0696ef8c33b9b2504e89bc0aab2ea99a6c90756Nadav Rotem} 640d0696ef8c33b9b2504e89bc0aab2ea99a6c90756Nadav Rotem 64133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov/// emitPrologue - Push callee-saved registers onto the stack, which 64233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov/// automatically adjust the stack pointer. Adjust the stack pointer to allocate 64333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov/// space for local variables. Also emit labels used by the exception handler to 64433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov/// generate the exception handling frames. 64516c29b5f285f375be53dabaa73e3e91107485fe4Anton Korobeynikovvoid X86FrameLowering::emitPrologue(MachineFunction &MF) const { 64633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MachineBasicBlock &MBB = MF.front(); // Prologue goes in entry BB. 64733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MachineBasicBlock::iterator MBBI = MBB.begin(); 64833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MachineFrameInfo *MFI = MF.getFrameInfo(); 64933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov const Function *Fn = MF.getFunction(); 650d9e3385ced2dc887e2fe8e1c071bd2611e4d3edeAnton Korobeynikov const X86RegisterInfo *RegInfo = TM.getRegisterInfo(); 651d9e3385ced2dc887e2fe8e1c071bd2611e4d3edeAnton Korobeynikov const X86InstrInfo &TII = *TM.getInstrInfo(); 65233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MachineModuleInfo &MMI = MF.getMMI(); 65333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); 65433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov bool needsFrameMoves = MMI.hasDebugInfo() || 655fc2bb8c4448fa884d79e437cc2d2627a7d7740a8Rafael Espindola Fn->needsUnwindTableEntry(); 65633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov uint64_t MaxAlign = MFI->getMaxAlignment(); // Desired stack alignment. 65733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov uint64_t StackSize = MFI->getStackSize(); // Number of bytes to allocate. 658d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov bool HasFP = hasFP(MF); 65933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov bool Is64Bit = STI.is64Bit(); 6602a1b60d791522d73be91d4281c90d25bd5e3d117Eli Bendersky bool IsLP64 = STI.isTarget64BitLP64(); 66133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov bool IsWin64 = STI.isTargetWin64(); 662de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng bool UseLEA = STI.useLeaForSP(); 66333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov unsigned StackAlign = getStackAlignment(); 66433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov unsigned SlotSize = RegInfo->getSlotSize(); 66533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov unsigned FramePtr = RegInfo->getFrameRegister(MF); 66633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov unsigned StackPtr = RegInfo->getStackRegister(); 6673f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier unsigned BasePtr = RegInfo->getBaseRegister(); 66833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov DebugLoc DL; 66933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 67033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // If we're forcing a stack realignment we can't rely on just the frame 67133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // info, we need to know the ABI stack alignment as well in case we 67233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // have a call out. Otherwise just make sure we have some alignment - we'll 67333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // go with the minimum SlotSize. 67433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (ForceStackAlign) { 67533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (MFI->hasCalls()) 67633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MaxAlign = (StackAlign > MaxAlign) ? StackAlign : MaxAlign; 67733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov else if (MaxAlign < SlotSize) 67833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MaxAlign = SlotSize; 67933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 68033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 68133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Add RETADDR move area to callee saved frame size. 68233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov int TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta(); 68333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (TailCallReturnAddrDelta < 0) 68433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov X86FI->setCalleeSavedFrameSize( 68533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov X86FI->getCalleeSavedFrameSize() - TailCallReturnAddrDelta); 68633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 68733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // If this is x86-64 and the Red Zone is not disabled, if we are a leaf 68833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // function, and use up to 128 bytes of stack space, don't have a frame 68933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // pointer, calls, or dynamic alloca then we do not need to adjust the 690d0696ef8c33b9b2504e89bc0aab2ea99a6c90756Nadav Rotem // stack pointer (we fit in the Red Zone). We also check that we don't 691d0696ef8c33b9b2504e89bc0aab2ea99a6c90756Nadav Rotem // push and pop from the stack. 692831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling if (Is64Bit && !Fn->getAttributes().hasAttribute(AttributeSet::FunctionIndex, 693831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling Attribute::NoRedZone) && 69433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov !RegInfo->needsStackRealignment(MF) && 6958a8d479214745c82ef00f08d4e4f1c173b5f9ce2Nick Lewycky !MFI->hasVarSizedObjects() && // No dynamic alloca. 6968a8d479214745c82ef00f08d4e4f1c173b5f9ce2Nick Lewycky !MFI->adjustsStack() && // No calls. 6978a8d479214745c82ef00f08d4e4f1c173b5f9ce2Nick Lewycky !IsWin64 && // Win64 has no Red Zone 698677689cf5dc65404645462464682a0696cc84532Nadav Rotem !usesTheStack(MF) && // Don't push and pop. 6998a8d479214745c82ef00f08d4e4f1c173b5f9ce2Nick Lewycky !MF.getTarget().Options.EnableSegmentedStacks) { // Regular stack 70033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov uint64_t MinSize = X86FI->getCalleeSavedFrameSize(); 70133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (HasFP) MinSize += SlotSize; 70233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov StackSize = std::max(MinSize, StackSize > 128 ? StackSize - 128 : 0); 70333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MFI->setStackSize(StackSize); 70433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 70533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 70633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Insert stack pointer adjustment for later moving of return addr. Only 70733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // applies to tail call optimized functions where the callee argument stack 70833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // size is bigger than the callers. 70933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (TailCallReturnAddrDelta < 0) { 71033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MachineInstr *MI = 71133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov BuildMI(MBB, MBBI, DL, 7122a1b60d791522d73be91d4281c90d25bd5e3d117Eli Bendersky TII.get(getSUBriOpcode(IsLP64, -TailCallReturnAddrDelta)), 71333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov StackPtr) 71433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov .addReg(StackPtr) 715aff232a5941c9ffb7ad52e08f81ad53794fed56bCharles Davis .addImm(-TailCallReturnAddrDelta) 716aff232a5941c9ffb7ad52e08f81ad53794fed56bCharles Davis .setMIFlag(MachineInstr::FrameSetup); 71733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MI->getOperand(3).setIsDead(); // The EFLAGS implicit def is dead. 71833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 71933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 72033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Mapping for machine moves: 72133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // 72233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // DST: VirtualFP AND 72333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // SRC: VirtualFP => DW_CFA_def_cfa_offset 72433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // ELSE => DW_CFA_def_cfa 72533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // 72633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // SRC: VirtualFP AND 72733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // DST: Register => DW_CFA_def_cfa_register 72833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // 72933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // ELSE 73033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // OFFSET < 0 => DW_CFA_offset_extended_sf 73133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // REG < 64 => DW_CFA_offset + Reg 73233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // ELSE => DW_CFA_offset_extended 73333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 73433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov uint64_t NumBytes = 0; 735aa3c2c09d9d5bc67c6ca2fbc6697257b15476684Michael Liao int stackGrowth = -SlotSize; 73633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 73733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (HasFP) { 73833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Calculate required stack adjustment. 73933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov uint64_t FrameSize = StackSize - SlotSize; 74099a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov if (RegInfo->needsStackRealignment(MF)) { 74199a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov // Callee-saved registers are pushed on stack before the stack 74299a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov // is realigned. 74399a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov FrameSize -= X86FI->getCalleeSavedFrameSize(); 74499a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov NumBytes = (FrameSize + MaxAlign - 1) / MaxAlign * MaxAlign; 74599a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov } else { 74699a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov NumBytes = FrameSize - X86FI->getCalleeSavedFrameSize(); 74799a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov } 74833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 74933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Get the offset of the stack slot for the EBP register, which is 75033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // guaranteed to be the last slot by processFunctionBeforeFrameFinalized. 75133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Update the frame offset adjustment. 75233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MFI->setOffsetAdjustment(-NumBytes); 75333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 75433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Save EBP/RBP into the appropriate stack slot. 75533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov BuildMI(MBB, MBBI, DL, TII.get(Is64Bit ? X86::PUSH64r : X86::PUSH32r)) 756aff232a5941c9ffb7ad52e08f81ad53794fed56bCharles Davis .addReg(FramePtr, RegState::Kill) 757aff232a5941c9ffb7ad52e08f81ad53794fed56bCharles Davis .setMIFlag(MachineInstr::FrameSetup); 75833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 75933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (needsFrameMoves) { 76033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Mark the place where EBP/RBP was saved. 76133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MCSymbol *FrameLabel = MMI.getContext().CreateTempSymbol(); 762fb4eb165d66b8ca5ae09c37d98d3ed937454b6afBill Wendling BuildMI(MBB, MBBI, DL, TII.get(X86::PROLOG_LABEL)) 763fb4eb165d66b8ca5ae09c37d98d3ed937454b6afBill Wendling .addSym(FrameLabel); 76433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 76533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Define the current CFA rule to use the provided offset. 766377b2270124f6f566c0f291bcb02f7755b642c2cRafael Espindola assert(StackSize); 7676b67ffd68bb2e555b1b512a809f3c82c68f3debeRafael Espindola MMI.addFrameInst( 7686b67ffd68bb2e555b1b512a809f3c82c68f3debeRafael Espindola MCCFIInstruction::createDefCfaOffset(FrameLabel, 2 * stackGrowth)); 76933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 77033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Change the rule for the FramePtr to be an "offset" rule. 7716b67ffd68bb2e555b1b512a809f3c82c68f3debeRafael Espindola unsigned DwarfFramePtr = RegInfo->getDwarfRegNum(FramePtr, true); 7726b67ffd68bb2e555b1b512a809f3c82c68f3debeRafael Espindola MMI.addFrameInst(MCCFIInstruction::createOffset(FrameLabel, DwarfFramePtr, 7736b67ffd68bb2e555b1b512a809f3c82c68f3debeRafael Espindola 2 * stackGrowth)); 77433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 77533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 77609b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // Update EBP with the new base value. 77733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov BuildMI(MBB, MBBI, DL, 77833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov TII.get(Is64Bit ? X86::MOV64rr : X86::MOV32rr), FramePtr) 779aff232a5941c9ffb7ad52e08f81ad53794fed56bCharles Davis .addReg(StackPtr) 780aff232a5941c9ffb7ad52e08f81ad53794fed56bCharles Davis .setMIFlag(MachineInstr::FrameSetup); 78133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 78233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (needsFrameMoves) { 78333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Mark effective beginning of when frame pointer becomes valid. 78433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MCSymbol *FrameLabel = MMI.getContext().CreateTempSymbol(); 785fb4eb165d66b8ca5ae09c37d98d3ed937454b6afBill Wendling BuildMI(MBB, MBBI, DL, TII.get(X86::PROLOG_LABEL)) 786fb4eb165d66b8ca5ae09c37d98d3ed937454b6afBill Wendling .addSym(FrameLabel); 78733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 78833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Define the current CFA to use the EBP/RBP register. 7896b67ffd68bb2e555b1b512a809f3c82c68f3debeRafael Espindola unsigned DwarfFramePtr = RegInfo->getDwarfRegNum(FramePtr, true); 7906b67ffd68bb2e555b1b512a809f3c82c68f3debeRafael Espindola MMI.addFrameInst( 7916b67ffd68bb2e555b1b512a809f3c82c68f3debeRafael Espindola MCCFIInstruction::createDefCfaRegister(FrameLabel, DwarfFramePtr)); 79233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 79333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 79433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Mark the FramePtr as live-in in every block except the entry. 79533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov for (MachineFunction::iterator I = llvm::next(MF.begin()), E = MF.end(); 79633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov I != E; ++I) 79733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov I->addLiveIn(FramePtr); 79833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } else { 79933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov NumBytes = StackSize - X86FI->getCalleeSavedFrameSize(); 80033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 80133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 80233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Skip the callee-saved push instructions. 80333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov bool PushedRegs = false; 80433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov int StackOffset = 2 * stackGrowth; 80533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 80633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov while (MBBI != MBB.end() && 80733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov (MBBI->getOpcode() == X86::PUSH32r || 80833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MBBI->getOpcode() == X86::PUSH64r)) { 80933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov PushedRegs = true; 810fb4eb165d66b8ca5ae09c37d98d3ed937454b6afBill Wendling MBBI->setFlag(MachineInstr::FrameSetup); 81133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov ++MBBI; 81233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 81333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (!HasFP && needsFrameMoves) { 81433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Mark callee-saved push instruction. 81533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MCSymbol *Label = MMI.getContext().CreateTempSymbol(); 81633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov BuildMI(MBB, MBBI, DL, TII.get(X86::PROLOG_LABEL)).addSym(Label); 81733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 81833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Define the current CFA rule to use the provided offset. 8190ed9f1fd8687ad32364b56898e6ebe1515e0e41cRafael Espindola assert(StackSize); 8206b67ffd68bb2e555b1b512a809f3c82c68f3debeRafael Espindola MMI.addFrameInst( 8216b67ffd68bb2e555b1b512a809f3c82c68f3debeRafael Espindola MCCFIInstruction::createDefCfaOffset(Label, StackOffset)); 82233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov StackOffset += stackGrowth; 82333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 82433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 82533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 82699a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov // Realign stack after we pushed callee-saved registers (so that we'll be 82799a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov // able to calculate their offsets from the frame pointer). 82899a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov 82999a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov // NOTE: We push the registers before realigning the stack, so 83099a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov // vector callee-saved (xmm) registers may be saved w/o proper 83199a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov // alignment in this way. However, currently these regs are saved in 83299a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov // stack slots (see X86FrameLowering::spillCalleeSavedRegisters()), so 83399a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov // this shouldn't be a problem. 83499a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov if (RegInfo->needsStackRealignment(MF)) { 83599a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov assert(HasFP && "There should be a frame pointer if stack is realigned."); 83699a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov MachineInstr *MI = 83799a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov BuildMI(MBB, MBBI, DL, 83899a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov TII.get(Is64Bit ? X86::AND64ri32 : X86::AND32ri), StackPtr) 83999a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov .addReg(StackPtr) 84099a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov .addImm(-MaxAlign) 84199a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov .setMIFlag(MachineInstr::FrameSetup); 84299a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov 84399a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov // The EFLAGS implicit def is dead. 84499a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov MI->getOperand(3).setIsDead(); 84599a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov } 84699a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov 84733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // If there is an SUB32ri of ESP immediately before this instruction, merge 84833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // the two. This can be the case when tail call elimination is enabled and 84933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // the callee has more arguments then the caller. 85033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov NumBytes -= mergeSPUpdates(MBB, MBBI, StackPtr, true); 85133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 85233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // If there is an ADD32ri or SUB32ri of ESP immediately after this 85333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // instruction, merge the two instructions. 85433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov mergeSPUpdatesDown(MBB, MBBI, StackPtr, &NumBytes); 85533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 85633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Adjust stack pointer: ESP -= numbytes. 85733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 85833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Windows and cygwin/mingw require a prologue helper routine when allocating 85933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // more than 4K bytes on the stack. Windows uses __chkstk and cygwin/mingw 86033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // uses __alloca. __alloca and the 32-bit version of __chkstk will probe the 86133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // stack and adjust the stack pointer in one go. The 64-bit version of 86233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // __chkstk is only responsible for probing the stack. The 64-bit prologue is 86333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // responsible for adjusting the stack pointer. Touching the stack at 4K 86433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // increments is necessary to ensure that the guard pages used by the OS 86533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // virtual memory manager are allocated in correct sequence. 866a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi if (NumBytes >= 4096 && STI.isTargetCOFF() && !STI.isTargetEnvMacho()) { 867a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi const char *StackProbeSymbol; 868a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi bool isSPUpdateNeeded = false; 869a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi 870a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi if (Is64Bit) { 871a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi if (STI.isTargetCygMing()) 872a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi StackProbeSymbol = "___chkstk"; 873a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi else { 874a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi StackProbeSymbol = "__chkstk"; 875a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi isSPUpdateNeeded = true; 876a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi } 877a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi } else if (STI.isTargetCygMing()) 878a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi StackProbeSymbol = "_alloca"; 879a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi else 880a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi StackProbeSymbol = "_chkstk"; 881a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi 88233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Check whether EAX is livein for this function. 88333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov bool isEAXAlive = isEAXLiveIn(MF); 88433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 885a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi if (isEAXAlive) { 886a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi // Sanity check that EAX is not livein for this function. 887a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi // It should not be, so throw an assert. 888a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi assert(!Is64Bit && "EAX is livein in x64 case!"); 889a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi 89033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Save EAX 89133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov BuildMI(MBB, MBBI, DL, TII.get(X86::PUSH32r)) 892fb4eb165d66b8ca5ae09c37d98d3ed937454b6afBill Wendling .addReg(X86::EAX, RegState::Kill) 893fb4eb165d66b8ca5ae09c37d98d3ed937454b6afBill Wendling .setMIFlag(MachineInstr::FrameSetup); 894a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi } 89533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 896a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi if (Is64Bit) { 897a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi // Handle the 64-bit Windows ABI case where we need to call __chkstk. 898a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi // Function prologue is responsible for adjusting the stack pointer. 899a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64ri), X86::RAX) 900fb4eb165d66b8ca5ae09c37d98d3ed937454b6afBill Wendling .addImm(NumBytes) 901fb4eb165d66b8ca5ae09c37d98d3ed937454b6afBill Wendling .setMIFlag(MachineInstr::FrameSetup); 902a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi } else { 903a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi // Allocate NumBytes-4 bytes on stack in case of isEAXAlive. 904a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi // We'll also use 4 already allocated bytes for EAX. 90533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32ri), X86::EAX) 906fb4eb165d66b8ca5ae09c37d98d3ed937454b6afBill Wendling .addImm(isEAXAlive ? NumBytes - 4 : NumBytes) 907fb4eb165d66b8ca5ae09c37d98d3ed937454b6afBill Wendling .setMIFlag(MachineInstr::FrameSetup); 908a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi } 909a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi 910a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi BuildMI(MBB, MBBI, DL, 911a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi TII.get(Is64Bit ? X86::W64ALLOCA : X86::CALLpcrel32)) 912a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi .addExternalSymbol(StackProbeSymbol) 913a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi .addReg(StackPtr, RegState::Define | RegState::Implicit) 914fb4eb165d66b8ca5ae09c37d98d3ed937454b6afBill Wendling .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit) 915fb4eb165d66b8ca5ae09c37d98d3ed937454b6afBill Wendling .setMIFlag(MachineInstr::FrameSetup); 916a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi 9174010110ccf21be0517034b6ccf9493628afaad77Nico Rieck // MSVC x64's __chkstk does not adjust %rsp itself. 9184010110ccf21be0517034b6ccf9493628afaad77Nico Rieck // It also does not clobber %rax so we can reuse it when adjusting %rsp. 9194010110ccf21be0517034b6ccf9493628afaad77Nico Rieck if (isSPUpdateNeeded) { 9204010110ccf21be0517034b6ccf9493628afaad77Nico Rieck BuildMI(MBB, MBBI, DL, TII.get(X86::SUB64rr), StackPtr) 9214010110ccf21be0517034b6ccf9493628afaad77Nico Rieck .addReg(StackPtr) 9224010110ccf21be0517034b6ccf9493628afaad77Nico Rieck .addReg(X86::RAX) 9234010110ccf21be0517034b6ccf9493628afaad77Nico Rieck .setMIFlag(MachineInstr::FrameSetup); 9244010110ccf21be0517034b6ccf9493628afaad77Nico Rieck } 925a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi 926a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi if (isEAXAlive) { 927a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi // Restore EAX 928a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi MachineInstr *MI = addRegOffset(BuildMI(MF, DL, TII.get(X86::MOV32rm), 929a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi X86::EAX), 930a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi StackPtr, false, NumBytes - 4); 931fb4eb165d66b8ca5ae09c37d98d3ed937454b6afBill Wendling MI->setFlag(MachineInstr::FrameSetup); 932a2e0762fae050464494a50c8b5d53ac2f4ba738cNAKAMURA Takumi MBB.insert(MBBI, MI); 93333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 93433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } else if (NumBytes) 9352a1b60d791522d73be91d4281c90d25bd5e3d117Eli Bendersky emitSPUpdate(MBB, MBBI, StackPtr, -(int64_t)NumBytes, Is64Bit, IsLP64, 93676ad43c6e1619ed4c087b8ccb2cd573eb9d7093eEric Christopher UseLEA, TII, *RegInfo); 93733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 9383f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier // If we need a base pointer, set it up here. It's whatever the value 9393f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier // of the stack pointer is at this point. Any variable size objects 9403f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier // will be allocated after this, so we can still use the base pointer 9413f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier // to reference locals. 9423f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier if (RegInfo->hasBasePointer(MF)) { 9433f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier // Update the frame pointer with the current stack pointer. 9443f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier unsigned Opc = Is64Bit ? X86::MOV64rr : X86::MOV32rr; 9453f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier BuildMI(MBB, MBBI, DL, TII.get(Opc), BasePtr) 9463f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier .addReg(StackPtr) 9473f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier .setMIFlag(MachineInstr::FrameSetup); 9483f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier } 9493f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier 950f0adba9a7ec8a3031876575a6ffb7db5f1b6f855Rafael Espindola if (( (!HasFP && NumBytes) || PushedRegs) && needsFrameMoves) { 95133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Mark end of stack pointer adjustment. 95233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MCSymbol *Label = MMI.getContext().CreateTempSymbol(); 953fb4eb165d66b8ca5ae09c37d98d3ed937454b6afBill Wendling BuildMI(MBB, MBBI, DL, TII.get(X86::PROLOG_LABEL)) 954fb4eb165d66b8ca5ae09c37d98d3ed937454b6afBill Wendling .addSym(Label); 95533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 95633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (!HasFP && NumBytes) { 95733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Define the current CFA rule to use the provided offset. 958377b2270124f6f566c0f291bcb02f7755b642c2cRafael Espindola assert(StackSize); 9596b67ffd68bb2e555b1b512a809f3c82c68f3debeRafael Espindola MMI.addFrameInst(MCCFIInstruction::createDefCfaOffset( 9606b67ffd68bb2e555b1b512a809f3c82c68f3debeRafael Espindola Label, -StackSize + stackGrowth)); 96133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 96233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 96333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Emit DWARF info specifying the offsets of the callee-saved registers. 96433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (PushedRegs) 96533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov emitCalleeSavedFrameMoves(MF, Label, HasFP ? FramePtr : StackPtr); 96633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 96709b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling 96809b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling // Darwin 10.7 and greater has support for compact unwind encoding. 969c8725d11f8756c57bdbceccc61062a9d560261c5Bill Wendling if (STI.getTargetTriple().isMacOSX() && 970ac86d43eae8bbfe5284d8e5fa17c141d1a7b1194Eli Friedman !STI.getTargetTriple().isMacOSXVersionLT(10, 7)) 97109b02c8dddf822e84c30cc08e7759d6ceebb2334Bill Wendling MMI.setCompactUnwindEncoding(getCompactUnwindEncoding(MF)); 97233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov} 97333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 97416c29b5f285f375be53dabaa73e3e91107485fe4Anton Korobeynikovvoid X86FrameLowering::emitEpilogue(MachineFunction &MF, 9753c2f0a11cce5a1e828e20675fa8467b624795e0aNick Lewycky MachineBasicBlock &MBB) const { 97633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov const MachineFrameInfo *MFI = MF.getFrameInfo(); 97733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); 978d9e3385ced2dc887e2fe8e1c071bd2611e4d3edeAnton Korobeynikov const X86RegisterInfo *RegInfo = TM.getRegisterInfo(); 979d9e3385ced2dc887e2fe8e1c071bd2611e4d3edeAnton Korobeynikov const X86InstrInfo &TII = *TM.getInstrInfo(); 9804f28c1c71450c711e96aa283de53739d8b4504cdJakob Stoklund Olesen MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); 9814f28c1c71450c711e96aa283de53739d8b4504cdJakob Stoklund Olesen assert(MBBI != MBB.end() && "Returning block has no instructions"); 98233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov unsigned RetOpcode = MBBI->getOpcode(); 98333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov DebugLoc DL = MBBI->getDebugLoc(); 98433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov bool Is64Bit = STI.is64Bit(); 9852a1b60d791522d73be91d4281c90d25bd5e3d117Eli Bendersky bool IsLP64 = STI.isTarget64BitLP64(); 986de1df103b9c578d0a1609054a5944342c5d0ba23Evan Cheng bool UseLEA = STI.useLeaForSP(); 98733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov unsigned StackAlign = getStackAlignment(); 98833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov unsigned SlotSize = RegInfo->getSlotSize(); 98933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov unsigned FramePtr = RegInfo->getFrameRegister(MF); 99033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov unsigned StackPtr = RegInfo->getStackRegister(); 99133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 99233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov switch (RetOpcode) { 99333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov default: 99433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov llvm_unreachable("Can only insert epilog into returning blocks"); 99533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov case X86::RET: 99633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov case X86::RETI: 99733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov case X86::TCRETURNdi: 99833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov case X86::TCRETURNri: 99933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov case X86::TCRETURNmi: 100033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov case X86::TCRETURNdi64: 100133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov case X86::TCRETURNri64: 100233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov case X86::TCRETURNmi64: 100333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov case X86::EH_RETURN: 100433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov case X86::EH_RETURN64: 100533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov break; // These are ok 100633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 100733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 100833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Get the number of bytes to allocate from the FrameInfo. 100933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov uint64_t StackSize = MFI->getStackSize(); 101033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov uint64_t MaxAlign = MFI->getMaxAlignment(); 101133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov unsigned CSSize = X86FI->getCalleeSavedFrameSize(); 101233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov uint64_t NumBytes = 0; 101333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 101433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // If we're forcing a stack realignment we can't rely on just the frame 101533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // info, we need to know the ABI stack alignment as well in case we 101633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // have a call out. Otherwise just make sure we have some alignment - we'll 101733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // go with the minimum. 101833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (ForceStackAlign) { 101933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (MFI->hasCalls()) 102033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MaxAlign = (StackAlign > MaxAlign) ? StackAlign : MaxAlign; 102133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov else 102233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MaxAlign = MaxAlign ? MaxAlign : 4; 102333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 102433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 1025d0c38176690e9602a93a20a43f1bd084564a8116Anton Korobeynikov if (hasFP(MF)) { 102633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Calculate required stack adjustment. 102733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov uint64_t FrameSize = StackSize - SlotSize; 102899a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov if (RegInfo->needsStackRealignment(MF)) { 102999a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov // Callee-saved registers were pushed on stack before the stack 103099a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov // was realigned. 103199a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov FrameSize -= CSSize; 103299a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov NumBytes = (FrameSize + MaxAlign - 1) / MaxAlign * MaxAlign; 103399a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov } else { 103499a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov NumBytes = FrameSize - CSSize; 103599a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov } 103633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 103733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Pop EBP. 103833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov BuildMI(MBB, MBBI, DL, 103933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov TII.get(Is64Bit ? X86::POP64r : X86::POP32r), FramePtr); 104033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } else { 104133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov NumBytes = StackSize - CSSize; 104233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 104333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 104433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Skip the callee-saved pop instructions. 104533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov while (MBBI != MBB.begin()) { 104633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MachineBasicBlock::iterator PI = prior(MBBI); 104733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov unsigned Opc = PI->getOpcode(); 104833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 10494f28c1c71450c711e96aa283de53739d8b4504cdJakob Stoklund Olesen if (Opc != X86::POP32r && Opc != X86::POP64r && Opc != X86::DBG_VALUE && 10505a96b3dad2f634c9081c8b2b6c2575441dc5a2bdEvan Cheng !PI->isTerminator()) 105133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov break; 105233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 105333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov --MBBI; 105433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 105599a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov MachineBasicBlock::iterator FirstCSPop = MBBI; 105633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 105733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov DL = MBBI->getDebugLoc(); 105833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 105933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // If there is an ADD32ri or SUB32ri of ESP immediately before this 106033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // instruction, merge the two instructions. 106133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (NumBytes || MFI->hasVarSizedObjects()) 106233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov mergeSPUpdatesUp(MBB, MBBI, StackPtr, &NumBytes); 106333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 106433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // If dynamic alloca is used, then reset esp to point to the last callee-saved 106533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // slot before popping them off! Same applies for the case, when stack was 106633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // realigned. 106799a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov if (RegInfo->needsStackRealignment(MF) || MFI->hasVarSizedObjects()) { 106899a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov if (RegInfo->needsStackRealignment(MF)) 106999a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov MBBI = FirstCSPop; 107099a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov if (CSSize != 0) { 107116221a60a00e52b078f6164ba4475c6e8e918e4bEli Bendersky unsigned Opc = getLEArOpcode(IsLP64); 107299a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr), 107399a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov FramePtr, false, -CSSize); 107433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } else { 107599a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov unsigned Opc = (Is64Bit ? X86::MOV64rr : X86::MOV32rr); 107699a92f269d4ea6f13a9858bb883e13382d021120Alexey Samsonov BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr) 107733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov .addReg(FramePtr); 107833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 107933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } else if (NumBytes) { 108033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Adjust stack pointer back: ESP += numbytes. 10812a1b60d791522d73be91d4281c90d25bd5e3d117Eli Bendersky emitSPUpdate(MBB, MBBI, StackPtr, NumBytes, Is64Bit, IsLP64, UseLEA, 10822a1b60d791522d73be91d4281c90d25bd5e3d117Eli Bendersky TII, *RegInfo); 108333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 108433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 108533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // We're returning from function via eh_return. 108633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (RetOpcode == X86::EH_RETURN || RetOpcode == X86::EH_RETURN64) { 10874f28c1c71450c711e96aa283de53739d8b4504cdJakob Stoklund Olesen MBBI = MBB.getLastNonDebugInstr(); 108833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MachineOperand &DestAddr = MBBI->getOperand(0); 108933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov assert(DestAddr.isReg() && "Offset should be in register!"); 109033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov BuildMI(MBB, MBBI, DL, 109133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov TII.get(Is64Bit ? X86::MOV64rr : X86::MOV32rr), 109233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov StackPtr).addReg(DestAddr.getReg()); 109333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } else if (RetOpcode == X86::TCRETURNri || RetOpcode == X86::TCRETURNdi || 109433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov RetOpcode == X86::TCRETURNmi || 109533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov RetOpcode == X86::TCRETURNri64 || RetOpcode == X86::TCRETURNdi64 || 109633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov RetOpcode == X86::TCRETURNmi64) { 109733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov bool isMem = RetOpcode == X86::TCRETURNmi || RetOpcode == X86::TCRETURNmi64; 109833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Tail call return: adjust the stack pointer and jump to callee. 1099f7ca976e74eafeeab0e9097f0fb07d6bb447415bJakob Stoklund Olesen MBBI = MBB.getLastNonDebugInstr(); 110033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MachineOperand &JumpTarget = MBBI->getOperand(0); 110133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MachineOperand &StackAdjust = MBBI->getOperand(isMem ? 5 : 1); 110233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov assert(StackAdjust.isImm() && "Expecting immediate value."); 110333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 110433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Adjust stack pointer. 110533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov int StackAdj = StackAdjust.getImm(); 110633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov int MaxTCDelta = X86FI->getTCReturnAddrDelta(); 110733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov int Offset = 0; 110833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov assert(MaxTCDelta <= 0 && "MaxTCDelta should never be positive"); 110933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 111033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Incoporate the retaddr area. 111133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov Offset = StackAdj-MaxTCDelta; 111233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov assert(Offset >= 0 && "Offset should never be negative"); 111333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 111433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (Offset) { 11157a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner // Check for possible merge with preceding ADD instruction. 111633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov Offset += mergeSPUpdates(MBB, MBBI, StackPtr, true); 11172a1b60d791522d73be91d4281c90d25bd5e3d117Eli Bendersky emitSPUpdate(MBB, MBBI, StackPtr, Offset, Is64Bit, IsLP64, 11182a1b60d791522d73be91d4281c90d25bd5e3d117Eli Bendersky UseLEA, TII, *RegInfo); 111933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 112033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 112133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Jump to label or value in register. 112233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov if (RetOpcode == X86::TCRETURNdi || RetOpcode == X86::TCRETURNdi64) { 11233d2125c9dbac695c93f42c0f59fd040e413fd711Evan Cheng MachineInstrBuilder MIB = 11243d2125c9dbac695c93f42c0f59fd040e413fd711Evan Cheng BuildMI(MBB, MBBI, DL, TII.get((RetOpcode == X86::TCRETURNdi) 11253d2125c9dbac695c93f42c0f59fd040e413fd711Evan Cheng ? X86::TAILJMPd : X86::TAILJMPd64)); 11263d2125c9dbac695c93f42c0f59fd040e413fd711Evan Cheng if (JumpTarget.isGlobal()) 11273d2125c9dbac695c93f42c0f59fd040e413fd711Evan Cheng MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(), 11283d2125c9dbac695c93f42c0f59fd040e413fd711Evan Cheng JumpTarget.getTargetFlags()); 11293d2125c9dbac695c93f42c0f59fd040e413fd711Evan Cheng else { 11303d2125c9dbac695c93f42c0f59fd040e413fd711Evan Cheng assert(JumpTarget.isSymbol()); 11313d2125c9dbac695c93f42c0f59fd040e413fd711Evan Cheng MIB.addExternalSymbol(JumpTarget.getSymbolName(), 11323d2125c9dbac695c93f42c0f59fd040e413fd711Evan Cheng JumpTarget.getTargetFlags()); 11333d2125c9dbac695c93f42c0f59fd040e413fd711Evan Cheng } 113433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } else if (RetOpcode == X86::TCRETURNmi || RetOpcode == X86::TCRETURNmi64) { 113533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MachineInstrBuilder MIB = 113633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov BuildMI(MBB, MBBI, DL, TII.get((RetOpcode == X86::TCRETURNmi) 113733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov ? X86::TAILJMPm : X86::TAILJMPm64)); 113833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov for (unsigned i = 0; i != 5; ++i) 113933464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MIB.addOperand(MBBI->getOperand(i)); 114033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } else if (RetOpcode == X86::TCRETURNri64) { 114133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov BuildMI(MBB, MBBI, DL, TII.get(X86::TAILJMPr64)). 114233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov addReg(JumpTarget.getReg(), RegState::Kill); 114333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } else { 114433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov BuildMI(MBB, MBBI, DL, TII.get(X86::TAILJMPr)). 114533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov addReg(JumpTarget.getReg(), RegState::Kill); 114633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 114733464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 114833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MachineInstr *NewMI = prior(MBBI); 1149be06aacaa9a270384599bbfa850b967e9996b9fbJakob Stoklund Olesen NewMI->copyImplicitOps(MF, MBBI); 115033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 115133464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Delete the pseudo instruction TCRETURN. 115233464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov MBB.erase(MBBI); 115333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } else if ((RetOpcode == X86::RET || RetOpcode == X86::RETI) && 115433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov (X86FI->getTCReturnAddrDelta() < 0)) { 115533464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov // Add the return addr area delta back since we are not tail calling. 115633464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov int delta = -1*X86FI->getTCReturnAddrDelta(); 11574f28c1c71450c711e96aa283de53739d8b4504cdJakob Stoklund Olesen MBBI = MBB.getLastNonDebugInstr(); 115833464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov 11597a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner // Check for possible merge with preceding ADD instruction. 116033464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov delta += mergeSPUpdates(MBB, MBBI, StackPtr, true); 11612a1b60d791522d73be91d4281c90d25bd5e3d117Eli Bendersky emitSPUpdate(MBB, MBBI, StackPtr, delta, Is64Bit, IsLP64, UseLEA, TII, 11622a1b60d791522d73be91d4281c90d25bd5e3d117Eli Bendersky *RegInfo); 116333464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov } 116433464912237efaa0ed7060829e66b59055bdd48bAnton Korobeynikov} 1165d9e3385ced2dc887e2fe8e1c071bd2611e4d3edeAnton Korobeynikov 116616c29b5f285f375be53dabaa73e3e91107485fe4Anton Korobeynikovint X86FrameLowering::getFrameIndexOffset(const MachineFunction &MF, int FI) const { 11673fb6eca0cd4c2b31b06543ca2ac60b9c91c2264dChad Rosier const X86RegisterInfo *RegInfo = 116882f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov static_cast<const X86RegisterInfo*>(MF.getTarget().getRegisterInfo()); 116982f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov const MachineFrameInfo *MFI = MF.getFrameInfo(); 117082f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov int Offset = MFI->getObjectOffset(FI) - getOffsetOfLocalArea(); 117182f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov uint64_t StackSize = MFI->getStackSize(); 117282f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov 11733f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier if (RegInfo->hasBasePointer(MF)) { 11743f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier assert (hasFP(MF) && "VLAs and dynamic stack realign, but no FP?!"); 11753f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier if (FI < 0) { 11763f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier // Skip the saved EBP. 11773f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier return Offset + RegInfo->getSlotSize(); 11783f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier } else { 11793f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier assert((-(Offset + StackSize)) % MFI->getObjectAlignment(FI) == 0); 11803f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier return Offset + StackSize; 11813f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier } 11823f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier } else if (RegInfo->needsStackRealignment(MF)) { 118382f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov if (FI < 0) { 118482f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov // Skip the saved EBP. 11853fb6eca0cd4c2b31b06543ca2ac60b9c91c2264dChad Rosier return Offset + RegInfo->getSlotSize(); 118682f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov } else { 118717001ce25cc205ac1cd2604492c2bce310964220Duncan Sands assert((-(Offset + StackSize)) % MFI->getObjectAlignment(FI) == 0); 118882f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov return Offset + StackSize; 118982f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov } 119082f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov // FIXME: Support tail calls 119182f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov } else { 119282f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov if (!hasFP(MF)) 119382f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov return Offset + StackSize; 119482f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov 119582f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov // Skip the saved EBP. 11963fb6eca0cd4c2b31b06543ca2ac60b9c91c2264dChad Rosier Offset += RegInfo->getSlotSize(); 119782f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov 119882f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov // Skip the RETADDR move area 119982f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov const X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); 120082f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov int TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta(); 120182f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov if (TailCallReturnAddrDelta < 0) 120282f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov Offset -= TailCallReturnAddrDelta; 120382f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov } 120482f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov 120582f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov return Offset; 120682f58740c76b42af8370247b23677a0318f6dde8Anton Korobeynikov} 1207cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov 1208d07d06ceef942c478c0f75a4c4d7442e61ddff1dAlexey Samsonovint X86FrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI, 1209d07d06ceef942c478c0f75a4c4d7442e61ddff1dAlexey Samsonov unsigned &FrameReg) const { 12103fb6eca0cd4c2b31b06543ca2ac60b9c91c2264dChad Rosier const X86RegisterInfo *RegInfo = 1211d07d06ceef942c478c0f75a4c4d7442e61ddff1dAlexey Samsonov static_cast<const X86RegisterInfo*>(MF.getTarget().getRegisterInfo()); 1212d07d06ceef942c478c0f75a4c4d7442e61ddff1dAlexey Samsonov // We can't calculate offset from frame pointer if the stack is realigned, 12133f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier // so enforce usage of stack/base pointer. The base pointer is used when we 12143f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier // have dynamic allocas in addition to dynamic realignment. 12153f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier if (RegInfo->hasBasePointer(MF)) 12163f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier FrameReg = RegInfo->getBaseRegister(); 12173f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier else if (RegInfo->needsStackRealignment(MF)) 12183f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier FrameReg = RegInfo->getStackRegister(); 12193f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier else 12203f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier FrameReg = RegInfo->getFrameRegister(MF); 1221d07d06ceef942c478c0f75a4c4d7442e61ddff1dAlexey Samsonov return getFrameIndexOffset(MF, FI); 1222d07d06ceef942c478c0f75a4c4d7442e61ddff1dAlexey Samsonov} 1223d07d06ceef942c478c0f75a4c4d7442e61ddff1dAlexey Samsonov 122416c29b5f285f375be53dabaa73e3e91107485fe4Anton Korobeynikovbool X86FrameLowering::spillCalleeSavedRegisters(MachineBasicBlock &MBB, 1225cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov MachineBasicBlock::iterator MI, 1226cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov const std::vector<CalleeSavedInfo> &CSI, 1227cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov const TargetRegisterInfo *TRI) const { 1228cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov if (CSI.empty()) 1229cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov return false; 1230cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov 1231cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov DebugLoc DL = MBB.findDebugLoc(MI); 1232cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov 1233cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov MachineFunction &MF = *MBB.getParent(); 1234cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov 1235cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov unsigned SlotSize = STI.is64Bit() ? 8 : 4; 1236cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov unsigned FPReg = TRI->getFrameRegister(MF); 1237cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov unsigned CalleeFrameSize = 0; 1238cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov 1239cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); 1240cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); 1241cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov 1242419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi // Push GPRs. It increases frame size. 1243cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov unsigned Opc = STI.is64Bit() ? X86::PUSH64r : X86::PUSH32r; 1244cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov for (unsigned i = CSI.size(); i != 0; --i) { 1245cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov unsigned Reg = CSI[i-1].getReg(); 1246419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi if (!X86::GR64RegClass.contains(Reg) && 1247419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi !X86::GR32RegClass.contains(Reg)) 1248419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi continue; 1249cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov // Add the callee-saved register as live-in. It's killed at the spill. 1250cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov MBB.addLiveIn(Reg); 1251cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov if (Reg == FPReg) 1252cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov // X86RegisterInfo::emitPrologue will handle spilling of frame register. 1253cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov continue; 1254419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi CalleeFrameSize += SlotSize; 1255aff232a5941c9ffb7ad52e08f81ad53794fed56bCharles Davis BuildMI(MBB, MI, DL, TII.get(Opc)).addReg(Reg, RegState::Kill) 1256aff232a5941c9ffb7ad52e08f81ad53794fed56bCharles Davis .setMIFlag(MachineInstr::FrameSetup); 1257cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov } 1258cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov 1259cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov X86FI->setCalleeSavedFrameSize(CalleeFrameSize); 1260419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi 1261419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi // Make XMM regs spilled. X86 does not have ability of push/pop XMM. 1262419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi // It can be done by spilling XMMs to stack frame. 1263419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi // Note that only Win64 ABI might spill XMMs. 1264419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi for (unsigned i = CSI.size(); i != 0; --i) { 1265419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi unsigned Reg = CSI[i-1].getReg(); 1266419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi if (X86::GR64RegClass.contains(Reg) || 1267419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi X86::GR32RegClass.contains(Reg)) 1268419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi continue; 1269419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi // Add the callee-saved register as live-in. It's killed at the spill. 1270419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi MBB.addLiveIn(Reg); 1271419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); 1272419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi TII.storeRegToStackSlot(MBB, MI, Reg, true, CSI[i-1].getFrameIdx(), 1273419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi RC, TRI); 1274419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi } 1275419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi 1276cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov return true; 1277cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov} 1278cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov 127916c29b5f285f375be53dabaa73e3e91107485fe4Anton Korobeynikovbool X86FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, 1280cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov MachineBasicBlock::iterator MI, 1281cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov const std::vector<CalleeSavedInfo> &CSI, 1282cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov const TargetRegisterInfo *TRI) const { 1283cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov if (CSI.empty()) 1284cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov return false; 1285cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov 1286cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov DebugLoc DL = MBB.findDebugLoc(MI); 1287cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov 1288cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov MachineFunction &MF = *MBB.getParent(); 1289cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov const TargetInstrInfo &TII = *MF.getTarget().getInstrInfo(); 1290419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi 1291419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi // Reload XMMs from stack frame. 1292419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 1293419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi unsigned Reg = CSI[i].getReg(); 1294419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi if (X86::GR64RegClass.contains(Reg) || 1295419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi X86::GR32RegClass.contains(Reg)) 1296419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi continue; 1297419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg); 1298419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi TII.loadRegFromStackSlot(MBB, MI, Reg, CSI[i].getFrameIdx(), 1299419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi RC, TRI); 1300419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi } 1301419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi 1302419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi // POP GPRs. 1303cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov unsigned FPReg = TRI->getFrameRegister(MF); 1304cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov unsigned Opc = STI.is64Bit() ? X86::POP64r : X86::POP32r; 1305cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 1306cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov unsigned Reg = CSI[i].getReg(); 1307419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi if (!X86::GR64RegClass.contains(Reg) && 1308419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi !X86::GR32RegClass.contains(Reg)) 1309419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi continue; 1310cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov if (Reg == FPReg) 1311cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov // X86RegisterInfo::emitEpilogue will handle restoring of frame register. 1312cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov continue; 1313419f23278388a8999b5c0bbe73ce823f63747f28NAKAMURA Takumi BuildMI(MBB, MI, DL, TII.get(Opc), Reg); 1314cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov } 1315cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov return true; 1316cd775ceff0b25a0b026f643a7990c2924bd310a3Anton Korobeynikov} 131794c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov 131894c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikovvoid 131916c29b5f285f375be53dabaa73e3e91107485fe4Anton KorobeynikovX86FrameLowering::processFunctionBeforeCalleeSavedScan(MachineFunction &MF, 132094c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov RegScavenger *RS) const { 132194c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov MachineFrameInfo *MFI = MF.getFrameInfo(); 132294c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov const X86RegisterInfo *RegInfo = TM.getRegisterInfo(); 132394c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov unsigned SlotSize = RegInfo->getSlotSize(); 132494c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov 132594c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); 1326a54b66277e5d4bdd55825eb9378f07334692020cTim Northover int64_t TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta(); 132794c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov 132894c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov if (TailCallReturnAddrDelta < 0) { 132994c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov // create RETURNADDR area 133094c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov // arg 133194c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov // arg 133294c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov // RETADDR 133394c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov // { ... 133494c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov // RETADDR area 133594c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov // ... 133694c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov // } 133794c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov // [EBP] 133894c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov MFI->CreateFixedObject(-TailCallReturnAddrDelta, 1339a54b66277e5d4bdd55825eb9378f07334692020cTim Northover TailCallReturnAddrDelta - SlotSize, true); 134094c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov } 134194c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov 134294c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov if (hasFP(MF)) { 134394c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov assert((TailCallReturnAddrDelta <= 0) && 134494c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov "The Delta should always be zero or negative"); 134516c29b5f285f375be53dabaa73e3e91107485fe4Anton Korobeynikov const TargetFrameLowering &TFI = *MF.getTarget().getFrameLowering(); 134694c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov 134794c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov // Create a frame entry for the EBP register that must be saved. 134894c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov int FrameIdx = MFI->CreateFixedObject(SlotSize, 134994c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov -(int)SlotSize + 135094c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov TFI.getOffsetOfLocalArea() + 135194c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov TailCallReturnAddrDelta, 135294c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov true); 135394c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov assert(FrameIdx == MFI->getObjectIndexBegin() && 135494c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov "Slot for EBP register must be last in order to be found!"); 135517001ce25cc205ac1cd2604492c2bce310964220Duncan Sands (void)FrameIdx; 135694c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov } 13573f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier 13583f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier // Spill the BasePtr if it's used. 13593f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier if (RegInfo->hasBasePointer(MF)) 13603f0dbab963197cadb32f70e1ee1a106fe35f5c8eChad Rosier MF.getRegInfo().setPhysRegUsed(RegInfo->getBaseRegister()); 136194c5ae08750f314bc3cf1bf882b686244a3927d9Anton Korobeynikov} 136276927d758657b3a511c73467ec5a7288795c1513Rafael Espindola 136376927d758657b3a511c73467ec5a7288795c1513Rafael Espindolastatic bool 136476927d758657b3a511c73467ec5a7288795c1513Rafael EspindolaHasNestArgument(const MachineFunction *MF) { 136576927d758657b3a511c73467ec5a7288795c1513Rafael Espindola const Function *F = MF->getFunction(); 136676927d758657b3a511c73467ec5a7288795c1513Rafael Espindola for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); 136776927d758657b3a511c73467ec5a7288795c1513Rafael Espindola I != E; I++) { 136876927d758657b3a511c73467ec5a7288795c1513Rafael Espindola if (I->hasNestAttr()) 136976927d758657b3a511c73467ec5a7288795c1513Rafael Espindola return true; 137076927d758657b3a511c73467ec5a7288795c1513Rafael Espindola } 137176927d758657b3a511c73467ec5a7288795c1513Rafael Espindola return false; 137276927d758657b3a511c73467ec5a7288795c1513Rafael Espindola} 137376927d758657b3a511c73467ec5a7288795c1513Rafael Espindola 137498fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer/// GetScratchRegister - Get a temp register for performing work in the 137598fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer/// segmented stack and the Erlang/HiPE stack prologue. Depending on platform 137698fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer/// and the properties of the function either one or two registers will be 137798fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer/// needed. Set primary to true for the first register, false for the second. 137876927d758657b3a511c73467ec5a7288795c1513Rafael Espindolastatic unsigned 13792028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael EspindolaGetScratchRegister(bool Is64Bit, const MachineFunction &MF, bool Primary) { 138098fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer CallingConv::ID CallingConvention = MF.getFunction()->getCallingConv(); 138198fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer 138298fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer // Erlang stuff. 138398fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer if (CallingConvention == CallingConv::HiPE) { 138498fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer if (Is64Bit) 138598fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer return Primary ? X86::R14 : X86::R13; 138698fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer else 138798fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer return Primary ? X86::EBX : X86::EDI; 138898fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer } 138998fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer 13904d6ccb5f68cd7c6418a209f1fa4dbade569e4493David Blaikie if (Is64Bit) 13912028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola return Primary ? X86::R11 : X86::R12; 13924d6ccb5f68cd7c6418a209f1fa4dbade569e4493David Blaikie 13934d6ccb5f68cd7c6418a209f1fa4dbade569e4493David Blaikie bool IsNested = HasNestArgument(&MF); 13944d6ccb5f68cd7c6418a209f1fa4dbade569e4493David Blaikie 13954d6ccb5f68cd7c6418a209f1fa4dbade569e4493David Blaikie if (CallingConvention == CallingConv::X86_FastCall || 13964d6ccb5f68cd7c6418a209f1fa4dbade569e4493David Blaikie CallingConvention == CallingConv::Fast) { 13974d6ccb5f68cd7c6418a209f1fa4dbade569e4493David Blaikie if (IsNested) 13984d6ccb5f68cd7c6418a209f1fa4dbade569e4493David Blaikie report_fatal_error("Segmented stacks does not support fastcall with " 13994d6ccb5f68cd7c6418a209f1fa4dbade569e4493David Blaikie "nested function."); 14004d6ccb5f68cd7c6418a209f1fa4dbade569e4493David Blaikie return Primary ? X86::EAX : X86::ECX; 140176927d758657b3a511c73467ec5a7288795c1513Rafael Espindola } 14024d6ccb5f68cd7c6418a209f1fa4dbade569e4493David Blaikie if (IsNested) 14034d6ccb5f68cd7c6418a209f1fa4dbade569e4493David Blaikie return Primary ? X86::EDX : X86::EAX; 14044d6ccb5f68cd7c6418a209f1fa4dbade569e4493David Blaikie return Primary ? X86::ECX : X86::EAX; 140576927d758657b3a511c73467ec5a7288795c1513Rafael Espindola} 140676927d758657b3a511c73467ec5a7288795c1513Rafael Espindola 1407199ce33b3bfa0b6293946c1b835da2a1fbc8cab4Sanjoy Das// The stack limit in the TCB is set to this many bytes above the actual stack 1408199ce33b3bfa0b6293946c1b835da2a1fbc8cab4Sanjoy Das// limit. 1409199ce33b3bfa0b6293946c1b835da2a1fbc8cab4Sanjoy Dasstatic const uint64_t kSplitStackAvailable = 256; 1410199ce33b3bfa0b6293946c1b835da2a1fbc8cab4Sanjoy Das 141176927d758657b3a511c73467ec5a7288795c1513Rafael Espindolavoid 141276927d758657b3a511c73467ec5a7288795c1513Rafael EspindolaX86FrameLowering::adjustForSegmentedStacks(MachineFunction &MF) const { 141376927d758657b3a511c73467ec5a7288795c1513Rafael Espindola MachineBasicBlock &prologueMBB = MF.front(); 141476927d758657b3a511c73467ec5a7288795c1513Rafael Espindola MachineFrameInfo *MFI = MF.getFrameInfo(); 141576927d758657b3a511c73467ec5a7288795c1513Rafael Espindola const X86InstrInfo &TII = *TM.getInstrInfo(); 141676927d758657b3a511c73467ec5a7288795c1513Rafael Espindola uint64_t StackSize; 141776927d758657b3a511c73467ec5a7288795c1513Rafael Espindola bool Is64Bit = STI.is64Bit(); 141876927d758657b3a511c73467ec5a7288795c1513Rafael Espindola unsigned TlsReg, TlsOffset; 141976927d758657b3a511c73467ec5a7288795c1513Rafael Espindola DebugLoc DL; 142076927d758657b3a511c73467ec5a7288795c1513Rafael Espindola 14212028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola unsigned ScratchReg = GetScratchRegister(Is64Bit, MF, true); 142276927d758657b3a511c73467ec5a7288795c1513Rafael Espindola assert(!MF.getRegInfo().isLiveIn(ScratchReg) && 142376927d758657b3a511c73467ec5a7288795c1513Rafael Espindola "Scratch register is live-in"); 142476927d758657b3a511c73467ec5a7288795c1513Rafael Espindola 142576927d758657b3a511c73467ec5a7288795c1513Rafael Espindola if (MF.getFunction()->isVarArg()) 142676927d758657b3a511c73467ec5a7288795c1513Rafael Espindola report_fatal_error("Segmented stacks do not support vararg functions."); 1427b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer if (!STI.isTargetLinux() && !STI.isTargetDarwin() && 1428b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer !STI.isTargetWin32() && !STI.isTargetFreeBSD()) 142985b9d43d4c7a4a5e6e7da651a159353a9a00e227Rafael Espindola report_fatal_error("Segmented stacks not supported on this platform."); 143076927d758657b3a511c73467ec5a7288795c1513Rafael Espindola 143176927d758657b3a511c73467ec5a7288795c1513Rafael Espindola MachineBasicBlock *allocMBB = MF.CreateMachineBasicBlock(); 143276927d758657b3a511c73467ec5a7288795c1513Rafael Espindola MachineBasicBlock *checkMBB = MF.CreateMachineBasicBlock(); 143376927d758657b3a511c73467ec5a7288795c1513Rafael Espindola X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); 143476927d758657b3a511c73467ec5a7288795c1513Rafael Espindola bool IsNested = false; 143576927d758657b3a511c73467ec5a7288795c1513Rafael Espindola 143676927d758657b3a511c73467ec5a7288795c1513Rafael Espindola // We need to know if the function has a nest argument only in 64 bit mode. 143776927d758657b3a511c73467ec5a7288795c1513Rafael Espindola if (Is64Bit) 143876927d758657b3a511c73467ec5a7288795c1513Rafael Espindola IsNested = HasNestArgument(&MF); 143976927d758657b3a511c73467ec5a7288795c1513Rafael Espindola 14404e68054b20725f6ec1cac33630258f749fe5debeBill Wendling // The MOV R10, RAX needs to be in a different block, since the RET we emit in 14414e68054b20725f6ec1cac33630258f749fe5debeBill Wendling // allocMBB needs to be last (terminating) instruction. 14424e68054b20725f6ec1cac33630258f749fe5debeBill Wendling 144376927d758657b3a511c73467ec5a7288795c1513Rafael Espindola for (MachineBasicBlock::livein_iterator i = prologueMBB.livein_begin(), 144476927d758657b3a511c73467ec5a7288795c1513Rafael Espindola e = prologueMBB.livein_end(); i != e; i++) { 144576927d758657b3a511c73467ec5a7288795c1513Rafael Espindola allocMBB->addLiveIn(*i); 144676927d758657b3a511c73467ec5a7288795c1513Rafael Espindola checkMBB->addLiveIn(*i); 144776927d758657b3a511c73467ec5a7288795c1513Rafael Espindola } 144876927d758657b3a511c73467ec5a7288795c1513Rafael Espindola 1449e840e88239cf92a065cbf5f5b9c7d18bc139c0e1Rafael Espindola if (IsNested) 145076927d758657b3a511c73467ec5a7288795c1513Rafael Espindola allocMBB->addLiveIn(X86::R10); 145176927d758657b3a511c73467ec5a7288795c1513Rafael Espindola 145276927d758657b3a511c73467ec5a7288795c1513Rafael Espindola MF.push_front(allocMBB); 145376927d758657b3a511c73467ec5a7288795c1513Rafael Espindola MF.push_front(checkMBB); 145476927d758657b3a511c73467ec5a7288795c1513Rafael Espindola 145576927d758657b3a511c73467ec5a7288795c1513Rafael Espindola // Eventually StackSize will be calculated by a link-time pass; which will 145676927d758657b3a511c73467ec5a7288795c1513Rafael Espindola // also decide whether checking code needs to be injected into this particular 145776927d758657b3a511c73467ec5a7288795c1513Rafael Espindola // prologue. 145876927d758657b3a511c73467ec5a7288795c1513Rafael Espindola StackSize = MFI->getStackSize(); 145976927d758657b3a511c73467ec5a7288795c1513Rafael Espindola 14602028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola // When the frame size is less than 256 we just compare the stack 14612028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola // boundary directly to the value of the stack pointer, per gcc. 14622028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola bool CompareStackPointer = StackSize < kSplitStackAvailable; 14632028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola 146476927d758657b3a511c73467ec5a7288795c1513Rafael Espindola // Read the limit off the current stacklet off the stack_guard location. 146576927d758657b3a511c73467ec5a7288795c1513Rafael Espindola if (Is64Bit) { 1466b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer if (STI.isTargetLinux()) { 14672028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola TlsReg = X86::FS; 14682028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola TlsOffset = 0x70; 1469b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer } else if (STI.isTargetDarwin()) { 14702028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola TlsReg = X86::GS; 14712028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola TlsOffset = 0x60 + 90*8; // See pthread_machdep.h. Steal TLS slot 90. 1472b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer } else if (STI.isTargetFreeBSD()) { 147385b9d43d4c7a4a5e6e7da651a159353a9a00e227Rafael Espindola TlsReg = X86::FS; 147485b9d43d4c7a4a5e6e7da651a159353a9a00e227Rafael Espindola TlsOffset = 0x18; 1475e4d18de5d15c6c1d4d279788b35d0cd1ab237e82Rafael Espindola } else { 1476e4d18de5d15c6c1d4d279788b35d0cd1ab237e82Rafael Espindola report_fatal_error("Segmented stacks not supported on this platform."); 14772028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola } 147876927d758657b3a511c73467ec5a7288795c1513Rafael Espindola 14792028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola if (CompareStackPointer) 1480199ce33b3bfa0b6293946c1b835da2a1fbc8cab4Sanjoy Das ScratchReg = X86::RSP; 1481199ce33b3bfa0b6293946c1b835da2a1fbc8cab4Sanjoy Das else 1482199ce33b3bfa0b6293946c1b835da2a1fbc8cab4Sanjoy Das BuildMI(checkMBB, DL, TII.get(X86::LEA64r), ScratchReg).addReg(X86::RSP) 1483014f7a3b3798580d5aac80b83bcb67e03d302fa4Rafael Espindola .addImm(1).addReg(0).addImm(-StackSize).addReg(0); 1484199ce33b3bfa0b6293946c1b835da2a1fbc8cab4Sanjoy Das 148576927d758657b3a511c73467ec5a7288795c1513Rafael Espindola BuildMI(checkMBB, DL, TII.get(X86::CMP64rm)).addReg(ScratchReg) 1486014f7a3b3798580d5aac80b83bcb67e03d302fa4Rafael Espindola .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg); 148776927d758657b3a511c73467ec5a7288795c1513Rafael Espindola } else { 1488b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer if (STI.isTargetLinux()) { 1489e4d18de5d15c6c1d4d279788b35d0cd1ab237e82Rafael Espindola TlsReg = X86::GS; 1490e4d18de5d15c6c1d4d279788b35d0cd1ab237e82Rafael Espindola TlsOffset = 0x30; 1491b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer } else if (STI.isTargetDarwin()) { 1492e4d18de5d15c6c1d4d279788b35d0cd1ab237e82Rafael Espindola TlsReg = X86::GS; 1493e4d18de5d15c6c1d4d279788b35d0cd1ab237e82Rafael Espindola TlsOffset = 0x48 + 90*4; 1494b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer } else if (STI.isTargetWin32()) { 1495e4d18de5d15c6c1d4d279788b35d0cd1ab237e82Rafael Espindola TlsReg = X86::FS; 1496e4d18de5d15c6c1d4d279788b35d0cd1ab237e82Rafael Espindola TlsOffset = 0x14; // pvArbitrary, reserved for application use 1497b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer } else if (STI.isTargetFreeBSD()) { 149885b9d43d4c7a4a5e6e7da651a159353a9a00e227Rafael Espindola report_fatal_error("Segmented stacks not supported on FreeBSD i386."); 1499e4d18de5d15c6c1d4d279788b35d0cd1ab237e82Rafael Espindola } else { 1500e4d18de5d15c6c1d4d279788b35d0cd1ab237e82Rafael Espindola report_fatal_error("Segmented stacks not supported on this platform."); 1501e4d18de5d15c6c1d4d279788b35d0cd1ab237e82Rafael Espindola } 150276927d758657b3a511c73467ec5a7288795c1513Rafael Espindola 15032028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola if (CompareStackPointer) 1504199ce33b3bfa0b6293946c1b835da2a1fbc8cab4Sanjoy Das ScratchReg = X86::ESP; 1505199ce33b3bfa0b6293946c1b835da2a1fbc8cab4Sanjoy Das else 1506199ce33b3bfa0b6293946c1b835da2a1fbc8cab4Sanjoy Das BuildMI(checkMBB, DL, TII.get(X86::LEA32r), ScratchReg).addReg(X86::ESP) 1507014f7a3b3798580d5aac80b83bcb67e03d302fa4Rafael Espindola .addImm(1).addReg(0).addImm(-StackSize).addReg(0); 1508199ce33b3bfa0b6293946c1b835da2a1fbc8cab4Sanjoy Das 1509b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer if (STI.isTargetLinux() || STI.isTargetWin32()) { 15102028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola BuildMI(checkMBB, DL, TII.get(X86::CMP32rm)).addReg(ScratchReg) 15112028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg); 1512b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer } else if (STI.isTargetDarwin()) { 15132028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola 15142028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola // TlsOffset doesn't fit into a mod r/m byte so we need an extra register 15152028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola unsigned ScratchReg2; 15162028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola bool SaveScratch2; 15172028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola if (CompareStackPointer) { 15182028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola // The primary scratch register is available for holding the TLS offset 15192028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola ScratchReg2 = GetScratchRegister(Is64Bit, MF, true); 15202028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola SaveScratch2 = false; 15212028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola } else { 15222028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola // Need to use a second register to hold the TLS offset 15232028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola ScratchReg2 = GetScratchRegister(Is64Bit, MF, false); 15242028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola 15252028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola // Unfortunately, with fastcc the second scratch register may hold an arg 15262028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola SaveScratch2 = MF.getRegInfo().isLiveIn(ScratchReg2); 15272028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola } 15282028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola 15292028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola // If Scratch2 is live-in then it needs to be saved 15302028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola assert((!MF.getRegInfo().isLiveIn(ScratchReg2) || SaveScratch2) && 15312028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola "Scratch register is live-in and not saved"); 15322028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola 15332028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola if (SaveScratch2) 15342028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola BuildMI(checkMBB, DL, TII.get(X86::PUSH32r)) 15352028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola .addReg(ScratchReg2, RegState::Kill); 15362028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola 15372028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola BuildMI(checkMBB, DL, TII.get(X86::MOV32ri), ScratchReg2) 15382028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola .addImm(TlsOffset); 15392028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola BuildMI(checkMBB, DL, TII.get(X86::CMP32rm)) 15402028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola .addReg(ScratchReg) 15412028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola .addReg(ScratchReg2).addImm(1).addReg(0) 15422028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola .addImm(0) 15432028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola .addReg(TlsReg); 15442028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola 15452028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola if (SaveScratch2) 15462028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola BuildMI(checkMBB, DL, TII.get(X86::POP32r), ScratchReg2); 15472028b793e1fd1a8dd4d99b0b7c9972865d5e806aRafael Espindola } 154876927d758657b3a511c73467ec5a7288795c1513Rafael Espindola } 154976927d758657b3a511c73467ec5a7288795c1513Rafael Espindola 155076927d758657b3a511c73467ec5a7288795c1513Rafael Espindola // This jump is taken if SP >= (Stacklet Limit + Stack Space required). 155176927d758657b3a511c73467ec5a7288795c1513Rafael Espindola // It jumps to normal execution of the function body. 1552313c7038319422cff0b2ea1015e180575cab4b7aRafael Espindola BuildMI(checkMBB, DL, TII.get(X86::JA_4)).addMBB(&prologueMBB); 155376927d758657b3a511c73467ec5a7288795c1513Rafael Espindola 155476927d758657b3a511c73467ec5a7288795c1513Rafael Espindola // On 32 bit we first push the arguments size and then the frame size. On 64 155576927d758657b3a511c73467ec5a7288795c1513Rafael Espindola // bit, we pass the stack frame size in r10 and the argument size in r11. 155676927d758657b3a511c73467ec5a7288795c1513Rafael Espindola if (Is64Bit) { 155776927d758657b3a511c73467ec5a7288795c1513Rafael Espindola // Functions with nested arguments use R10, so it needs to be saved across 155876927d758657b3a511c73467ec5a7288795c1513Rafael Espindola // the call to _morestack 155976927d758657b3a511c73467ec5a7288795c1513Rafael Espindola 156076927d758657b3a511c73467ec5a7288795c1513Rafael Espindola if (IsNested) 156176927d758657b3a511c73467ec5a7288795c1513Rafael Espindola BuildMI(allocMBB, DL, TII.get(X86::MOV64rr), X86::RAX).addReg(X86::R10); 156276927d758657b3a511c73467ec5a7288795c1513Rafael Espindola 156376927d758657b3a511c73467ec5a7288795c1513Rafael Espindola BuildMI(allocMBB, DL, TII.get(X86::MOV64ri), X86::R10) 156476927d758657b3a511c73467ec5a7288795c1513Rafael Espindola .addImm(StackSize); 156576927d758657b3a511c73467ec5a7288795c1513Rafael Espindola BuildMI(allocMBB, DL, TII.get(X86::MOV64ri), X86::R11) 156676927d758657b3a511c73467ec5a7288795c1513Rafael Espindola .addImm(X86FI->getArgumentStackSize()); 156776927d758657b3a511c73467ec5a7288795c1513Rafael Espindola MF.getRegInfo().setPhysRegUsed(X86::R10); 156876927d758657b3a511c73467ec5a7288795c1513Rafael Espindola MF.getRegInfo().setPhysRegUsed(X86::R11); 156976927d758657b3a511c73467ec5a7288795c1513Rafael Espindola } else { 157076927d758657b3a511c73467ec5a7288795c1513Rafael Espindola BuildMI(allocMBB, DL, TII.get(X86::PUSHi32)) 157176927d758657b3a511c73467ec5a7288795c1513Rafael Espindola .addImm(X86FI->getArgumentStackSize()); 157276927d758657b3a511c73467ec5a7288795c1513Rafael Espindola BuildMI(allocMBB, DL, TII.get(X86::PUSHi32)) 157376927d758657b3a511c73467ec5a7288795c1513Rafael Espindola .addImm(StackSize); 157476927d758657b3a511c73467ec5a7288795c1513Rafael Espindola } 157576927d758657b3a511c73467ec5a7288795c1513Rafael Espindola 157676927d758657b3a511c73467ec5a7288795c1513Rafael Espindola // __morestack is in libgcc 157776927d758657b3a511c73467ec5a7288795c1513Rafael Espindola if (Is64Bit) 157876927d758657b3a511c73467ec5a7288795c1513Rafael Espindola BuildMI(allocMBB, DL, TII.get(X86::CALL64pcrel32)) 157976927d758657b3a511c73467ec5a7288795c1513Rafael Espindola .addExternalSymbol("__morestack"); 158076927d758657b3a511c73467ec5a7288795c1513Rafael Espindola else 158176927d758657b3a511c73467ec5a7288795c1513Rafael Espindola BuildMI(allocMBB, DL, TII.get(X86::CALLpcrel32)) 158276927d758657b3a511c73467ec5a7288795c1513Rafael Espindola .addExternalSymbol("__morestack"); 158376927d758657b3a511c73467ec5a7288795c1513Rafael Espindola 15844e68054b20725f6ec1cac33630258f749fe5debeBill Wendling if (IsNested) 1585e840e88239cf92a065cbf5f5b9c7d18bc139c0e1Rafael Espindola BuildMI(allocMBB, DL, TII.get(X86::MORESTACK_RET_RESTORE_R10)); 1586e840e88239cf92a065cbf5f5b9c7d18bc139c0e1Rafael Espindola else 1587e840e88239cf92a065cbf5f5b9c7d18bc139c0e1Rafael Espindola BuildMI(allocMBB, DL, TII.get(X86::MORESTACK_RET)); 158876927d758657b3a511c73467ec5a7288795c1513Rafael Espindola 1589e840e88239cf92a065cbf5f5b9c7d18bc139c0e1Rafael Espindola allocMBB->addSuccessor(&prologueMBB); 159082222c20be24adda7c218f3fdaf2e0ae049c955bBill Wendling 159176927d758657b3a511c73467ec5a7288795c1513Rafael Espindola checkMBB->addSuccessor(allocMBB); 159276927d758657b3a511c73467ec5a7288795c1513Rafael Espindola checkMBB->addSuccessor(&prologueMBB); 159376927d758657b3a511c73467ec5a7288795c1513Rafael Espindola 159451f0c7641983469cbd29f8862a121645471a885aJakob Stoklund Olesen#ifdef XDEBUG 159576927d758657b3a511c73467ec5a7288795c1513Rafael Espindola MF.verify(); 159676927d758657b3a511c73467ec5a7288795c1513Rafael Espindola#endif 159776927d758657b3a511c73467ec5a7288795c1513Rafael Espindola} 159898fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer 15992d1035def15030ae404b227747d0a28454ea32a9Yiannis Tsiouris/// Erlang programs may need a special prologue to handle the stack size they 16002d1035def15030ae404b227747d0a28454ea32a9Yiannis Tsiouris/// might need at runtime. That is because Erlang/OTP does not implement a C 16012d1035def15030ae404b227747d0a28454ea32a9Yiannis Tsiouris/// stack but uses a custom implementation of hybrid stack/heap architecture. 16022d1035def15030ae404b227747d0a28454ea32a9Yiannis Tsiouris/// (for more information see Eric Stenman's Ph.D. thesis: 16032d1035def15030ae404b227747d0a28454ea32a9Yiannis Tsiouris/// http://publications.uu.se/uu/fulltext/nbn_se_uu_diva-2688.pdf) 16042d1035def15030ae404b227747d0a28454ea32a9Yiannis Tsiouris/// 16052d1035def15030ae404b227747d0a28454ea32a9Yiannis Tsiouris/// CheckStack: 16062d1035def15030ae404b227747d0a28454ea32a9Yiannis Tsiouris/// temp0 = sp - MaxStack 16072d1035def15030ae404b227747d0a28454ea32a9Yiannis Tsiouris/// if( temp0 < SP_LIMIT(P) ) goto IncStack else goto OldStart 16082d1035def15030ae404b227747d0a28454ea32a9Yiannis Tsiouris/// OldStart: 16092d1035def15030ae404b227747d0a28454ea32a9Yiannis Tsiouris/// ... 16102d1035def15030ae404b227747d0a28454ea32a9Yiannis Tsiouris/// IncStack: 16112d1035def15030ae404b227747d0a28454ea32a9Yiannis Tsiouris/// call inc_stack # doubles the stack space 16122d1035def15030ae404b227747d0a28454ea32a9Yiannis Tsiouris/// temp0 = sp - MaxStack 16132d1035def15030ae404b227747d0a28454ea32a9Yiannis Tsiouris/// if( temp0 < SP_LIMIT(P) ) goto IncStack else goto OldStart 161498fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramervoid X86FrameLowering::adjustForHiPEPrologue(MachineFunction &MF) const { 161598fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer const X86InstrInfo &TII = *TM.getInstrInfo(); 161698fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer MachineFrameInfo *MFI = MF.getFrameInfo(); 1617b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer const unsigned SlotSize = TM.getRegisterInfo()->getSlotSize(); 161898fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer const bool Is64Bit = STI.is64Bit(); 161998fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer DebugLoc DL; 162098fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer // HiPE-specific values 162198fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer const unsigned HipeLeafWords = 24; 162298fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer const unsigned CCRegisteredArgs = Is64Bit ? 6 : 5; 162398fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer const unsigned Guaranteed = HipeLeafWords * SlotSize; 1624b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer unsigned CallerStkArity = MF.getFunction()->arg_size() > CCRegisteredArgs ? 1625b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer MF.getFunction()->arg_size() - CCRegisteredArgs : 0; 1626b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer unsigned MaxStack = MFI->getStackSize() + CallerStkArity*SlotSize + SlotSize; 162798fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer 1628b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer assert(STI.isTargetLinux() && 162998fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer "HiPE prologue is only supported on Linux operating systems."); 163098fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer 163198fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer // Compute the largest caller's frame that is needed to fit the callees' 163298fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer // frames. This 'MaxStack' is computed from: 163398fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer // 163498fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer // a) the fixed frame size, which is the space needed for all spilled temps, 163598fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer // b) outgoing on-stack parameter areas, and 163698fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer // c) the minimum stack space this function needs to make available for the 163798fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer // functions it calls (a tunable ABI property). 163898fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer if (MFI->hasCalls()) { 163998fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer unsigned MoreStackForCalls = 0; 164098fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer 164198fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer for (MachineFunction::iterator MBBI = MF.begin(), MBBE = MF.end(); 164298fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer MBBI != MBBE; ++MBBI) 164398fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer for (MachineBasicBlock::iterator MI = MBBI->begin(), ME = MBBI->end(); 1644b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer MI != ME; ++MI) { 1645b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer if (!MI->isCall()) 1646b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer continue; 1647b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer 1648b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer // Get callee operand. 1649b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer const MachineOperand &MO = MI->getOperand(0); 1650b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer 1651b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer // Only take account of global function calls (no closures etc.). 1652b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer if (!MO.isGlobal()) 1653b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer continue; 1654b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer 1655b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer const Function *F = dyn_cast<Function>(MO.getGlobal()); 1656b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer if (!F) 1657b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer continue; 1658b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer 1659b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer // Do not update 'MaxStack' for primitive and built-in functions 1660b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer // (encoded with names either starting with "erlang."/"bif_" or not 1661b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer // having a ".", such as a simple <Module>.<Function>.<Arity>, or an 1662b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer // "_", such as the BIF "suspend_0") as they are executed on another 1663b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer // stack. 1664b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer if (F->getName().find("erlang.") != StringRef::npos || 1665b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer F->getName().find("bif_") != StringRef::npos || 1666b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer F->getName().find_first_of("._") == StringRef::npos) 1667b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer continue; 1668b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer 1669b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer unsigned CalleeStkArity = 1670b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer F->arg_size() > CCRegisteredArgs ? F->arg_size()-CCRegisteredArgs : 0; 1671b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer if (HipeLeafWords - 1 > CalleeStkArity) 1672b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer MoreStackForCalls = std::max(MoreStackForCalls, 1673b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer (HipeLeafWords - 1 - CalleeStkArity) * SlotSize); 1674b1e1d5d4a575f5e5b4ceb7af68f33e75695ee959Benjamin Kramer } 167598fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer MaxStack += MoreStackForCalls; 167698fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer } 167798fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer 167898fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer // If the stack frame needed is larger than the guaranteed then runtime checks 167998fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer // and calls to "inc_stack_0" BIF should be inserted in the assembly prologue. 168098fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer if (MaxStack > Guaranteed) { 168198fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer MachineBasicBlock &prologueMBB = MF.front(); 168298fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer MachineBasicBlock *stackCheckMBB = MF.CreateMachineBasicBlock(); 168398fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer MachineBasicBlock *incStackMBB = MF.CreateMachineBasicBlock(); 168498fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer 168598fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer for (MachineBasicBlock::livein_iterator I = prologueMBB.livein_begin(), 168698fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer E = prologueMBB.livein_end(); I != E; I++) { 168798fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer stackCheckMBB->addLiveIn(*I); 168898fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer incStackMBB->addLiveIn(*I); 168998fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer } 169098fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer 169198fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer MF.push_front(incStackMBB); 169298fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer MF.push_front(stackCheckMBB); 169398fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer 169498fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer unsigned ScratchReg, SPReg, PReg, SPLimitOffset; 169598fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer unsigned LEAop, CMPop, CALLop; 169698fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer if (Is64Bit) { 169798fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer SPReg = X86::RSP; 169898fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer PReg = X86::RBP; 169998fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer LEAop = X86::LEA64r; 170098fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer CMPop = X86::CMP64rm; 170198fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer CALLop = X86::CALL64pcrel32; 170298fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer SPLimitOffset = 0x90; 170398fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer } else { 170498fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer SPReg = X86::ESP; 170598fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer PReg = X86::EBP; 170698fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer LEAop = X86::LEA32r; 170798fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer CMPop = X86::CMP32rm; 170898fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer CALLop = X86::CALLpcrel32; 170998fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer SPLimitOffset = 0x4c; 171098fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer } 171198fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer 171298fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer ScratchReg = GetScratchRegister(Is64Bit, MF, true); 171398fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer assert(!MF.getRegInfo().isLiveIn(ScratchReg) && 171498fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer "HiPE prologue scratch register is live-in"); 171598fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer 171698fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer // Create new MBB for StackCheck: 171798fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer addRegOffset(BuildMI(stackCheckMBB, DL, TII.get(LEAop), ScratchReg), 171898fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer SPReg, false, -MaxStack); 171998fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer // SPLimitOffset is in a fixed heap location (pointed by BP). 172098fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer addRegOffset(BuildMI(stackCheckMBB, DL, TII.get(CMPop)) 172198fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer .addReg(ScratchReg), PReg, false, SPLimitOffset); 172298fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer BuildMI(stackCheckMBB, DL, TII.get(X86::JAE_4)).addMBB(&prologueMBB); 172398fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer 172498fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer // Create new MBB for IncStack: 172598fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer BuildMI(incStackMBB, DL, TII.get(CALLop)). 172698fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer addExternalSymbol("inc_stack_0"); 172798fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer addRegOffset(BuildMI(incStackMBB, DL, TII.get(LEAop), ScratchReg), 172898fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer SPReg, false, -MaxStack); 172998fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer addRegOffset(BuildMI(incStackMBB, DL, TII.get(CMPop)) 173098fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer .addReg(ScratchReg), PReg, false, SPLimitOffset); 173198fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer BuildMI(incStackMBB, DL, TII.get(X86::JLE_4)).addMBB(incStackMBB); 173298fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer 173398fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer stackCheckMBB->addSuccessor(&prologueMBB, 99); 173498fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer stackCheckMBB->addSuccessor(incStackMBB, 1); 173598fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer incStackMBB->addSuccessor(&prologueMBB, 99); 173698fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer incStackMBB->addSuccessor(incStackMBB, 1); 173798fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer } 173898fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer#ifdef XDEBUG 173998fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer MF.verify(); 174098fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer#endif 174198fbe27ac8f0766ea94b89b8c03418131b72bea4Benjamin Kramer} 1742700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky 1743700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Benderskyvoid X86FrameLowering:: 1744700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli BenderskyeliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, 1745700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky MachineBasicBlock::iterator I) const { 1746700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky const X86InstrInfo &TII = *TM.getInstrInfo(); 1747700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky const X86RegisterInfo &RegInfo = *TM.getRegisterInfo(); 1748700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky unsigned StackPtr = RegInfo.getStackRegister(); 1749700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky bool reseveCallFrame = hasReservedCallFrame(MF); 1750700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky int Opcode = I->getOpcode(); 1751700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky bool isDestroy = Opcode == TII.getCallFrameDestroyOpcode(); 1752700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky bool IsLP64 = STI.isTarget64BitLP64(); 1753700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky DebugLoc DL = I->getDebugLoc(); 1754700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky uint64_t Amount = !reseveCallFrame ? I->getOperand(0).getImm() : 0; 1755700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky uint64_t CalleeAmt = isDestroy ? I->getOperand(1).getImm() : 0; 1756700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky I = MBB.erase(I); 1757700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky 1758700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky if (!reseveCallFrame) { 1759700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky // If the stack pointer can be changed after prologue, turn the 1760700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky // adjcallstackup instruction into a 'sub ESP, <amt>' and the 1761700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky // adjcallstackdown instruction into 'add ESP, <amt>' 1762700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky // TODO: consider using push / pop instead of sub + store / add 1763700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky if (Amount == 0) 1764700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky return; 1765700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky 1766700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky // We need to keep the stack aligned properly. To do this, we round the 1767700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky // amount of space needed for the outgoing arguments up to the next 1768700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky // alignment boundary. 1769700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky unsigned StackAlign = TM.getFrameLowering()->getStackAlignment(); 1770700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky Amount = (Amount + StackAlign - 1) / StackAlign * StackAlign; 1771700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky 1772700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky MachineInstr *New = 0; 1773700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky if (Opcode == TII.getCallFrameSetupOpcode()) { 1774700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky New = BuildMI(MF, DL, TII.get(getSUBriOpcode(IsLP64, Amount)), 1775700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky StackPtr) 1776700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky .addReg(StackPtr) 1777700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky .addImm(Amount); 1778700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky } else { 1779700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky assert(Opcode == TII.getCallFrameDestroyOpcode()); 1780700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky 1781700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky // Factor out the amount the callee already popped. 1782700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky Amount -= CalleeAmt; 1783700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky 1784700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky if (Amount) { 1785700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky unsigned Opc = getADDriOpcode(IsLP64, Amount); 1786700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky New = BuildMI(MF, DL, TII.get(Opc), StackPtr) 1787700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky .addReg(StackPtr).addImm(Amount); 1788700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky } 1789700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky } 1790700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky 1791700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky if (New) { 1792700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky // The EFLAGS implicit def is dead. 1793700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky New->getOperand(3).setIsDead(); 1794700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky 1795700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky // Replace the pseudo instruction with a new instruction. 1796700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky MBB.insert(I, New); 1797700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky } 1798700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky 1799700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky return; 1800700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky } 1801700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky 1802700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky if (Opcode == TII.getCallFrameDestroyOpcode() && CalleeAmt) { 1803700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky // If we are performing frame pointer elimination and if the callee pops 1804700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky // something off the stack pointer, add it back. We do this until we have 1805700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky // more advanced stack pointer tracking ability. 1806700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky unsigned Opc = getSUBriOpcode(IsLP64, CalleeAmt); 1807700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky MachineInstr *New = BuildMI(MF, DL, TII.get(Opc), StackPtr) 1808700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky .addReg(StackPtr).addImm(CalleeAmt); 1809700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky 1810700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky // The EFLAGS implicit def is dead. 1811700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky New->getOperand(3).setIsDead(); 1812700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky 1813700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky // We are not tracking the stack pointer adjustment by the callee, so make 1814700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky // sure we restore the stack pointer immediately after the call, there may 1815700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky // be spill code inserted between the CALL and ADJCALLSTACKUP instructions. 1816700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky MachineBasicBlock::iterator B = MBB.begin(); 1817700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky while (I != B && !llvm::prior(I)->isCall()) 1818700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky --I; 1819700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky MBB.insert(I, New); 1820700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky } 1821700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky} 1822700ed80d3da5e98e05ceb90e9bfb66058581a6dbEli Bendersky 1823