1*0b57cec5SDimitry Andric //===-- X86FrameLowering.cpp - X86 Frame Information ----------------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This file contains the X86 implementation of TargetFrameLowering class. 10*0b57cec5SDimitry Andric // 11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12*0b57cec5SDimitry Andric 13*0b57cec5SDimitry Andric #include "X86FrameLowering.h" 14*0b57cec5SDimitry Andric #include "X86InstrBuilder.h" 15*0b57cec5SDimitry Andric #include "X86InstrInfo.h" 16*0b57cec5SDimitry Andric #include "X86MachineFunctionInfo.h" 17*0b57cec5SDimitry Andric #include "X86Subtarget.h" 18*0b57cec5SDimitry Andric #include "X86TargetMachine.h" 19*0b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h" 20*0b57cec5SDimitry Andric #include "llvm/Analysis/EHPersonalities.h" 21*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 22*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 23*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 24*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h" 25*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 26*0b57cec5SDimitry Andric #include "llvm/CodeGen/WinEHFuncInfo.h" 27*0b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 28*0b57cec5SDimitry Andric #include "llvm/IR/Function.h" 29*0b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h" 30*0b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h" 31*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 32*0b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h" 33*0b57cec5SDimitry Andric #include <cstdlib> 34*0b57cec5SDimitry Andric 35*0b57cec5SDimitry Andric using namespace llvm; 36*0b57cec5SDimitry Andric 37*0b57cec5SDimitry Andric X86FrameLowering::X86FrameLowering(const X86Subtarget &STI, 38*0b57cec5SDimitry Andric unsigned StackAlignOverride) 39*0b57cec5SDimitry Andric : TargetFrameLowering(StackGrowsDown, StackAlignOverride, 40*0b57cec5SDimitry Andric STI.is64Bit() ? -8 : -4), 41*0b57cec5SDimitry Andric STI(STI), TII(*STI.getInstrInfo()), TRI(STI.getRegisterInfo()) { 42*0b57cec5SDimitry Andric // Cache a bunch of frame-related predicates for this subtarget. 43*0b57cec5SDimitry Andric SlotSize = TRI->getSlotSize(); 44*0b57cec5SDimitry Andric Is64Bit = STI.is64Bit(); 45*0b57cec5SDimitry Andric IsLP64 = STI.isTarget64BitLP64(); 46*0b57cec5SDimitry Andric // standard x86_64 and NaCl use 64-bit frame/stack pointers, x32 - 32-bit. 47*0b57cec5SDimitry Andric Uses64BitFramePtr = STI.isTarget64BitLP64() || STI.isTargetNaCl64(); 48*0b57cec5SDimitry Andric StackPtr = TRI->getStackRegister(); 49*0b57cec5SDimitry Andric } 50*0b57cec5SDimitry Andric 51*0b57cec5SDimitry Andric bool X86FrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { 52*0b57cec5SDimitry Andric return !MF.getFrameInfo().hasVarSizedObjects() && 53*0b57cec5SDimitry Andric !MF.getInfo<X86MachineFunctionInfo>()->getHasPushSequences(); 54*0b57cec5SDimitry Andric } 55*0b57cec5SDimitry Andric 56*0b57cec5SDimitry Andric /// canSimplifyCallFramePseudos - If there is a reserved call frame, the 57*0b57cec5SDimitry Andric /// call frame pseudos can be simplified. Having a FP, as in the default 58*0b57cec5SDimitry Andric /// implementation, is not sufficient here since we can't always use it. 59*0b57cec5SDimitry Andric /// Use a more nuanced condition. 60*0b57cec5SDimitry Andric bool 61*0b57cec5SDimitry Andric X86FrameLowering::canSimplifyCallFramePseudos(const MachineFunction &MF) const { 62*0b57cec5SDimitry Andric return hasReservedCallFrame(MF) || 63*0b57cec5SDimitry Andric (hasFP(MF) && !TRI->needsStackRealignment(MF)) || 64*0b57cec5SDimitry Andric TRI->hasBasePointer(MF); 65*0b57cec5SDimitry Andric } 66*0b57cec5SDimitry Andric 67*0b57cec5SDimitry Andric // needsFrameIndexResolution - Do we need to perform FI resolution for 68*0b57cec5SDimitry Andric // this function. Normally, this is required only when the function 69*0b57cec5SDimitry Andric // has any stack objects. However, FI resolution actually has another job, 70*0b57cec5SDimitry Andric // not apparent from the title - it resolves callframesetup/destroy 71*0b57cec5SDimitry Andric // that were not simplified earlier. 72*0b57cec5SDimitry Andric // So, this is required for x86 functions that have push sequences even 73*0b57cec5SDimitry Andric // when there are no stack objects. 74*0b57cec5SDimitry Andric bool 75*0b57cec5SDimitry Andric X86FrameLowering::needsFrameIndexResolution(const MachineFunction &MF) const { 76*0b57cec5SDimitry Andric return MF.getFrameInfo().hasStackObjects() || 77*0b57cec5SDimitry Andric MF.getInfo<X86MachineFunctionInfo>()->getHasPushSequences(); 78*0b57cec5SDimitry Andric } 79*0b57cec5SDimitry Andric 80*0b57cec5SDimitry Andric /// hasFP - Return true if the specified function should have a dedicated frame 81*0b57cec5SDimitry Andric /// pointer register. This is true if the function has variable sized allocas 82*0b57cec5SDimitry Andric /// or if frame pointer elimination is disabled. 83*0b57cec5SDimitry Andric bool X86FrameLowering::hasFP(const MachineFunction &MF) const { 84*0b57cec5SDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo(); 85*0b57cec5SDimitry Andric return (MF.getTarget().Options.DisableFramePointerElim(MF) || 86*0b57cec5SDimitry Andric TRI->needsStackRealignment(MF) || 87*0b57cec5SDimitry Andric MFI.hasVarSizedObjects() || 88*0b57cec5SDimitry Andric MFI.isFrameAddressTaken() || MFI.hasOpaqueSPAdjustment() || 89*0b57cec5SDimitry Andric MF.getInfo<X86MachineFunctionInfo>()->getForceFramePointer() || 90*0b57cec5SDimitry Andric MF.callsUnwindInit() || MF.hasEHFunclets() || MF.callsEHReturn() || 91*0b57cec5SDimitry Andric MFI.hasStackMap() || MFI.hasPatchPoint() || 92*0b57cec5SDimitry Andric MFI.hasCopyImplyingStackAdjustment()); 93*0b57cec5SDimitry Andric } 94*0b57cec5SDimitry Andric 95*0b57cec5SDimitry Andric static unsigned getSUBriOpcode(unsigned IsLP64, int64_t Imm) { 96*0b57cec5SDimitry Andric if (IsLP64) { 97*0b57cec5SDimitry Andric if (isInt<8>(Imm)) 98*0b57cec5SDimitry Andric return X86::SUB64ri8; 99*0b57cec5SDimitry Andric return X86::SUB64ri32; 100*0b57cec5SDimitry Andric } else { 101*0b57cec5SDimitry Andric if (isInt<8>(Imm)) 102*0b57cec5SDimitry Andric return X86::SUB32ri8; 103*0b57cec5SDimitry Andric return X86::SUB32ri; 104*0b57cec5SDimitry Andric } 105*0b57cec5SDimitry Andric } 106*0b57cec5SDimitry Andric 107*0b57cec5SDimitry Andric static unsigned getADDriOpcode(unsigned IsLP64, int64_t Imm) { 108*0b57cec5SDimitry Andric if (IsLP64) { 109*0b57cec5SDimitry Andric if (isInt<8>(Imm)) 110*0b57cec5SDimitry Andric return X86::ADD64ri8; 111*0b57cec5SDimitry Andric return X86::ADD64ri32; 112*0b57cec5SDimitry Andric } else { 113*0b57cec5SDimitry Andric if (isInt<8>(Imm)) 114*0b57cec5SDimitry Andric return X86::ADD32ri8; 115*0b57cec5SDimitry Andric return X86::ADD32ri; 116*0b57cec5SDimitry Andric } 117*0b57cec5SDimitry Andric } 118*0b57cec5SDimitry Andric 119*0b57cec5SDimitry Andric static unsigned getSUBrrOpcode(unsigned isLP64) { 120*0b57cec5SDimitry Andric return isLP64 ? X86::SUB64rr : X86::SUB32rr; 121*0b57cec5SDimitry Andric } 122*0b57cec5SDimitry Andric 123*0b57cec5SDimitry Andric static unsigned getADDrrOpcode(unsigned isLP64) { 124*0b57cec5SDimitry Andric return isLP64 ? X86::ADD64rr : X86::ADD32rr; 125*0b57cec5SDimitry Andric } 126*0b57cec5SDimitry Andric 127*0b57cec5SDimitry Andric static unsigned getANDriOpcode(bool IsLP64, int64_t Imm) { 128*0b57cec5SDimitry Andric if (IsLP64) { 129*0b57cec5SDimitry Andric if (isInt<8>(Imm)) 130*0b57cec5SDimitry Andric return X86::AND64ri8; 131*0b57cec5SDimitry Andric return X86::AND64ri32; 132*0b57cec5SDimitry Andric } 133*0b57cec5SDimitry Andric if (isInt<8>(Imm)) 134*0b57cec5SDimitry Andric return X86::AND32ri8; 135*0b57cec5SDimitry Andric return X86::AND32ri; 136*0b57cec5SDimitry Andric } 137*0b57cec5SDimitry Andric 138*0b57cec5SDimitry Andric static unsigned getLEArOpcode(unsigned IsLP64) { 139*0b57cec5SDimitry Andric return IsLP64 ? X86::LEA64r : X86::LEA32r; 140*0b57cec5SDimitry Andric } 141*0b57cec5SDimitry Andric 142*0b57cec5SDimitry Andric /// findDeadCallerSavedReg - Return a caller-saved register that isn't live 143*0b57cec5SDimitry Andric /// when it reaches the "return" instruction. We can then pop a stack object 144*0b57cec5SDimitry Andric /// to this register without worry about clobbering it. 145*0b57cec5SDimitry Andric static unsigned findDeadCallerSavedReg(MachineBasicBlock &MBB, 146*0b57cec5SDimitry Andric MachineBasicBlock::iterator &MBBI, 147*0b57cec5SDimitry Andric const X86RegisterInfo *TRI, 148*0b57cec5SDimitry Andric bool Is64Bit) { 149*0b57cec5SDimitry Andric const MachineFunction *MF = MBB.getParent(); 150*0b57cec5SDimitry Andric if (MF->callsEHReturn()) 151*0b57cec5SDimitry Andric return 0; 152*0b57cec5SDimitry Andric 153*0b57cec5SDimitry Andric const TargetRegisterClass &AvailableRegs = *TRI->getGPRsForTailCall(*MF); 154*0b57cec5SDimitry Andric 155*0b57cec5SDimitry Andric if (MBBI == MBB.end()) 156*0b57cec5SDimitry Andric return 0; 157*0b57cec5SDimitry Andric 158*0b57cec5SDimitry Andric switch (MBBI->getOpcode()) { 159*0b57cec5SDimitry Andric default: return 0; 160*0b57cec5SDimitry Andric case TargetOpcode::PATCHABLE_RET: 161*0b57cec5SDimitry Andric case X86::RET: 162*0b57cec5SDimitry Andric case X86::RETL: 163*0b57cec5SDimitry Andric case X86::RETQ: 164*0b57cec5SDimitry Andric case X86::RETIL: 165*0b57cec5SDimitry Andric case X86::RETIQ: 166*0b57cec5SDimitry Andric case X86::TCRETURNdi: 167*0b57cec5SDimitry Andric case X86::TCRETURNri: 168*0b57cec5SDimitry Andric case X86::TCRETURNmi: 169*0b57cec5SDimitry Andric case X86::TCRETURNdi64: 170*0b57cec5SDimitry Andric case X86::TCRETURNri64: 171*0b57cec5SDimitry Andric case X86::TCRETURNmi64: 172*0b57cec5SDimitry Andric case X86::EH_RETURN: 173*0b57cec5SDimitry Andric case X86::EH_RETURN64: { 174*0b57cec5SDimitry Andric SmallSet<uint16_t, 8> Uses; 175*0b57cec5SDimitry Andric for (unsigned i = 0, e = MBBI->getNumOperands(); i != e; ++i) { 176*0b57cec5SDimitry Andric MachineOperand &MO = MBBI->getOperand(i); 177*0b57cec5SDimitry Andric if (!MO.isReg() || MO.isDef()) 178*0b57cec5SDimitry Andric continue; 179*0b57cec5SDimitry Andric unsigned Reg = MO.getReg(); 180*0b57cec5SDimitry Andric if (!Reg) 181*0b57cec5SDimitry Andric continue; 182*0b57cec5SDimitry Andric for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) 183*0b57cec5SDimitry Andric Uses.insert(*AI); 184*0b57cec5SDimitry Andric } 185*0b57cec5SDimitry Andric 186*0b57cec5SDimitry Andric for (auto CS : AvailableRegs) 187*0b57cec5SDimitry Andric if (!Uses.count(CS) && CS != X86::RIP && CS != X86::RSP && 188*0b57cec5SDimitry Andric CS != X86::ESP) 189*0b57cec5SDimitry Andric return CS; 190*0b57cec5SDimitry Andric } 191*0b57cec5SDimitry Andric } 192*0b57cec5SDimitry Andric 193*0b57cec5SDimitry Andric return 0; 194*0b57cec5SDimitry Andric } 195*0b57cec5SDimitry Andric 196*0b57cec5SDimitry Andric static bool isEAXLiveIn(MachineBasicBlock &MBB) { 197*0b57cec5SDimitry Andric for (MachineBasicBlock::RegisterMaskPair RegMask : MBB.liveins()) { 198*0b57cec5SDimitry Andric unsigned Reg = RegMask.PhysReg; 199*0b57cec5SDimitry Andric 200*0b57cec5SDimitry Andric if (Reg == X86::RAX || Reg == X86::EAX || Reg == X86::AX || 201*0b57cec5SDimitry Andric Reg == X86::AH || Reg == X86::AL) 202*0b57cec5SDimitry Andric return true; 203*0b57cec5SDimitry Andric } 204*0b57cec5SDimitry Andric 205*0b57cec5SDimitry Andric return false; 206*0b57cec5SDimitry Andric } 207*0b57cec5SDimitry Andric 208*0b57cec5SDimitry Andric /// Check if the flags need to be preserved before the terminators. 209*0b57cec5SDimitry Andric /// This would be the case, if the eflags is live-in of the region 210*0b57cec5SDimitry Andric /// composed by the terminators or live-out of that region, without 211*0b57cec5SDimitry Andric /// being defined by a terminator. 212*0b57cec5SDimitry Andric static bool 213*0b57cec5SDimitry Andric flagsNeedToBePreservedBeforeTheTerminators(const MachineBasicBlock &MBB) { 214*0b57cec5SDimitry Andric for (const MachineInstr &MI : MBB.terminators()) { 215*0b57cec5SDimitry Andric bool BreakNext = false; 216*0b57cec5SDimitry Andric for (const MachineOperand &MO : MI.operands()) { 217*0b57cec5SDimitry Andric if (!MO.isReg()) 218*0b57cec5SDimitry Andric continue; 219*0b57cec5SDimitry Andric unsigned Reg = MO.getReg(); 220*0b57cec5SDimitry Andric if (Reg != X86::EFLAGS) 221*0b57cec5SDimitry Andric continue; 222*0b57cec5SDimitry Andric 223*0b57cec5SDimitry Andric // This terminator needs an eflags that is not defined 224*0b57cec5SDimitry Andric // by a previous another terminator: 225*0b57cec5SDimitry Andric // EFLAGS is live-in of the region composed by the terminators. 226*0b57cec5SDimitry Andric if (!MO.isDef()) 227*0b57cec5SDimitry Andric return true; 228*0b57cec5SDimitry Andric // This terminator defines the eflags, i.e., we don't need to preserve it. 229*0b57cec5SDimitry Andric // However, we still need to check this specific terminator does not 230*0b57cec5SDimitry Andric // read a live-in value. 231*0b57cec5SDimitry Andric BreakNext = true; 232*0b57cec5SDimitry Andric } 233*0b57cec5SDimitry Andric // We found a definition of the eflags, no need to preserve them. 234*0b57cec5SDimitry Andric if (BreakNext) 235*0b57cec5SDimitry Andric return false; 236*0b57cec5SDimitry Andric } 237*0b57cec5SDimitry Andric 238*0b57cec5SDimitry Andric // None of the terminators use or define the eflags. 239*0b57cec5SDimitry Andric // Check if they are live-out, that would imply we need to preserve them. 240*0b57cec5SDimitry Andric for (const MachineBasicBlock *Succ : MBB.successors()) 241*0b57cec5SDimitry Andric if (Succ->isLiveIn(X86::EFLAGS)) 242*0b57cec5SDimitry Andric return true; 243*0b57cec5SDimitry Andric 244*0b57cec5SDimitry Andric return false; 245*0b57cec5SDimitry Andric } 246*0b57cec5SDimitry Andric 247*0b57cec5SDimitry Andric /// emitSPUpdate - Emit a series of instructions to increment / decrement the 248*0b57cec5SDimitry Andric /// stack pointer by a constant value. 249*0b57cec5SDimitry Andric void X86FrameLowering::emitSPUpdate(MachineBasicBlock &MBB, 250*0b57cec5SDimitry Andric MachineBasicBlock::iterator &MBBI, 251*0b57cec5SDimitry Andric const DebugLoc &DL, 252*0b57cec5SDimitry Andric int64_t NumBytes, bool InEpilogue) const { 253*0b57cec5SDimitry Andric bool isSub = NumBytes < 0; 254*0b57cec5SDimitry Andric uint64_t Offset = isSub ? -NumBytes : NumBytes; 255*0b57cec5SDimitry Andric MachineInstr::MIFlag Flag = 256*0b57cec5SDimitry Andric isSub ? MachineInstr::FrameSetup : MachineInstr::FrameDestroy; 257*0b57cec5SDimitry Andric 258*0b57cec5SDimitry Andric uint64_t Chunk = (1LL << 31) - 1; 259*0b57cec5SDimitry Andric 260*0b57cec5SDimitry Andric if (Offset > Chunk) { 261*0b57cec5SDimitry Andric // Rather than emit a long series of instructions for large offsets, 262*0b57cec5SDimitry Andric // load the offset into a register and do one sub/add 263*0b57cec5SDimitry Andric unsigned Reg = 0; 264*0b57cec5SDimitry Andric unsigned Rax = (unsigned)(Is64Bit ? X86::RAX : X86::EAX); 265*0b57cec5SDimitry Andric 266*0b57cec5SDimitry Andric if (isSub && !isEAXLiveIn(MBB)) 267*0b57cec5SDimitry Andric Reg = Rax; 268*0b57cec5SDimitry Andric else 269*0b57cec5SDimitry Andric Reg = findDeadCallerSavedReg(MBB, MBBI, TRI, Is64Bit); 270*0b57cec5SDimitry Andric 271*0b57cec5SDimitry Andric unsigned MovRIOpc = Is64Bit ? X86::MOV64ri : X86::MOV32ri; 272*0b57cec5SDimitry Andric unsigned AddSubRROpc = 273*0b57cec5SDimitry Andric isSub ? getSUBrrOpcode(Is64Bit) : getADDrrOpcode(Is64Bit); 274*0b57cec5SDimitry Andric if (Reg) { 275*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(MovRIOpc), Reg) 276*0b57cec5SDimitry Andric .addImm(Offset) 277*0b57cec5SDimitry Andric .setMIFlag(Flag); 278*0b57cec5SDimitry Andric MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(AddSubRROpc), StackPtr) 279*0b57cec5SDimitry Andric .addReg(StackPtr) 280*0b57cec5SDimitry Andric .addReg(Reg); 281*0b57cec5SDimitry Andric MI->getOperand(3).setIsDead(); // The EFLAGS implicit def is dead. 282*0b57cec5SDimitry Andric return; 283*0b57cec5SDimitry Andric } else if (Offset > 8 * Chunk) { 284*0b57cec5SDimitry Andric // If we would need more than 8 add or sub instructions (a >16GB stack 285*0b57cec5SDimitry Andric // frame), it's worth spilling RAX to materialize this immediate. 286*0b57cec5SDimitry Andric // pushq %rax 287*0b57cec5SDimitry Andric // movabsq +-$Offset+-SlotSize, %rax 288*0b57cec5SDimitry Andric // addq %rsp, %rax 289*0b57cec5SDimitry Andric // xchg %rax, (%rsp) 290*0b57cec5SDimitry Andric // movq (%rsp), %rsp 291*0b57cec5SDimitry Andric assert(Is64Bit && "can't have 32-bit 16GB stack frame"); 292*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(X86::PUSH64r)) 293*0b57cec5SDimitry Andric .addReg(Rax, RegState::Kill) 294*0b57cec5SDimitry Andric .setMIFlag(Flag); 295*0b57cec5SDimitry Andric // Subtract is not commutative, so negate the offset and always use add. 296*0b57cec5SDimitry Andric // Subtract 8 less and add 8 more to account for the PUSH we just did. 297*0b57cec5SDimitry Andric if (isSub) 298*0b57cec5SDimitry Andric Offset = -(Offset - SlotSize); 299*0b57cec5SDimitry Andric else 300*0b57cec5SDimitry Andric Offset = Offset + SlotSize; 301*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(MovRIOpc), Rax) 302*0b57cec5SDimitry Andric .addImm(Offset) 303*0b57cec5SDimitry Andric .setMIFlag(Flag); 304*0b57cec5SDimitry Andric MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(X86::ADD64rr), Rax) 305*0b57cec5SDimitry Andric .addReg(Rax) 306*0b57cec5SDimitry Andric .addReg(StackPtr); 307*0b57cec5SDimitry Andric MI->getOperand(3).setIsDead(); // The EFLAGS implicit def is dead. 308*0b57cec5SDimitry Andric // Exchange the new SP in RAX with the top of the stack. 309*0b57cec5SDimitry Andric addRegOffset( 310*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(X86::XCHG64rm), Rax).addReg(Rax), 311*0b57cec5SDimitry Andric StackPtr, false, 0); 312*0b57cec5SDimitry Andric // Load new SP from the top of the stack into RSP. 313*0b57cec5SDimitry Andric addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64rm), StackPtr), 314*0b57cec5SDimitry Andric StackPtr, false, 0); 315*0b57cec5SDimitry Andric return; 316*0b57cec5SDimitry Andric } 317*0b57cec5SDimitry Andric } 318*0b57cec5SDimitry Andric 319*0b57cec5SDimitry Andric while (Offset) { 320*0b57cec5SDimitry Andric uint64_t ThisVal = std::min(Offset, Chunk); 321*0b57cec5SDimitry Andric if (ThisVal == SlotSize) { 322*0b57cec5SDimitry Andric // Use push / pop for slot sized adjustments as a size optimization. We 323*0b57cec5SDimitry Andric // need to find a dead register when using pop. 324*0b57cec5SDimitry Andric unsigned Reg = isSub 325*0b57cec5SDimitry Andric ? (unsigned)(Is64Bit ? X86::RAX : X86::EAX) 326*0b57cec5SDimitry Andric : findDeadCallerSavedReg(MBB, MBBI, TRI, Is64Bit); 327*0b57cec5SDimitry Andric if (Reg) { 328*0b57cec5SDimitry Andric unsigned Opc = isSub 329*0b57cec5SDimitry Andric ? (Is64Bit ? X86::PUSH64r : X86::PUSH32r) 330*0b57cec5SDimitry Andric : (Is64Bit ? X86::POP64r : X86::POP32r); 331*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(Opc)) 332*0b57cec5SDimitry Andric .addReg(Reg, getDefRegState(!isSub) | getUndefRegState(isSub)) 333*0b57cec5SDimitry Andric .setMIFlag(Flag); 334*0b57cec5SDimitry Andric Offset -= ThisVal; 335*0b57cec5SDimitry Andric continue; 336*0b57cec5SDimitry Andric } 337*0b57cec5SDimitry Andric } 338*0b57cec5SDimitry Andric 339*0b57cec5SDimitry Andric BuildStackAdjustment(MBB, MBBI, DL, isSub ? -ThisVal : ThisVal, InEpilogue) 340*0b57cec5SDimitry Andric .setMIFlag(Flag); 341*0b57cec5SDimitry Andric 342*0b57cec5SDimitry Andric Offset -= ThisVal; 343*0b57cec5SDimitry Andric } 344*0b57cec5SDimitry Andric } 345*0b57cec5SDimitry Andric 346*0b57cec5SDimitry Andric MachineInstrBuilder X86FrameLowering::BuildStackAdjustment( 347*0b57cec5SDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, 348*0b57cec5SDimitry Andric const DebugLoc &DL, int64_t Offset, bool InEpilogue) const { 349*0b57cec5SDimitry Andric assert(Offset != 0 && "zero offset stack adjustment requested"); 350*0b57cec5SDimitry Andric 351*0b57cec5SDimitry Andric // On Atom, using LEA to adjust SP is preferred, but using it in the epilogue 352*0b57cec5SDimitry Andric // is tricky. 353*0b57cec5SDimitry Andric bool UseLEA; 354*0b57cec5SDimitry Andric if (!InEpilogue) { 355*0b57cec5SDimitry Andric // Check if inserting the prologue at the beginning 356*0b57cec5SDimitry Andric // of MBB would require to use LEA operations. 357*0b57cec5SDimitry Andric // We need to use LEA operations if EFLAGS is live in, because 358*0b57cec5SDimitry Andric // it means an instruction will read it before it gets defined. 359*0b57cec5SDimitry Andric UseLEA = STI.useLeaForSP() || MBB.isLiveIn(X86::EFLAGS); 360*0b57cec5SDimitry Andric } else { 361*0b57cec5SDimitry Andric // If we can use LEA for SP but we shouldn't, check that none 362*0b57cec5SDimitry Andric // of the terminators uses the eflags. Otherwise we will insert 363*0b57cec5SDimitry Andric // a ADD that will redefine the eflags and break the condition. 364*0b57cec5SDimitry Andric // Alternatively, we could move the ADD, but this may not be possible 365*0b57cec5SDimitry Andric // and is an optimization anyway. 366*0b57cec5SDimitry Andric UseLEA = canUseLEAForSPInEpilogue(*MBB.getParent()); 367*0b57cec5SDimitry Andric if (UseLEA && !STI.useLeaForSP()) 368*0b57cec5SDimitry Andric UseLEA = flagsNeedToBePreservedBeforeTheTerminators(MBB); 369*0b57cec5SDimitry Andric // If that assert breaks, that means we do not do the right thing 370*0b57cec5SDimitry Andric // in canUseAsEpilogue. 371*0b57cec5SDimitry Andric assert((UseLEA || !flagsNeedToBePreservedBeforeTheTerminators(MBB)) && 372*0b57cec5SDimitry Andric "We shouldn't have allowed this insertion point"); 373*0b57cec5SDimitry Andric } 374*0b57cec5SDimitry Andric 375*0b57cec5SDimitry Andric MachineInstrBuilder MI; 376*0b57cec5SDimitry Andric if (UseLEA) { 377*0b57cec5SDimitry Andric MI = addRegOffset(BuildMI(MBB, MBBI, DL, 378*0b57cec5SDimitry Andric TII.get(getLEArOpcode(Uses64BitFramePtr)), 379*0b57cec5SDimitry Andric StackPtr), 380*0b57cec5SDimitry Andric StackPtr, false, Offset); 381*0b57cec5SDimitry Andric } else { 382*0b57cec5SDimitry Andric bool IsSub = Offset < 0; 383*0b57cec5SDimitry Andric uint64_t AbsOffset = IsSub ? -Offset : Offset; 384*0b57cec5SDimitry Andric unsigned Opc = IsSub ? getSUBriOpcode(Uses64BitFramePtr, AbsOffset) 385*0b57cec5SDimitry Andric : getADDriOpcode(Uses64BitFramePtr, AbsOffset); 386*0b57cec5SDimitry Andric MI = BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr) 387*0b57cec5SDimitry Andric .addReg(StackPtr) 388*0b57cec5SDimitry Andric .addImm(AbsOffset); 389*0b57cec5SDimitry Andric MI->getOperand(3).setIsDead(); // The EFLAGS implicit def is dead. 390*0b57cec5SDimitry Andric } 391*0b57cec5SDimitry Andric return MI; 392*0b57cec5SDimitry Andric } 393*0b57cec5SDimitry Andric 394*0b57cec5SDimitry Andric int X86FrameLowering::mergeSPUpdates(MachineBasicBlock &MBB, 395*0b57cec5SDimitry Andric MachineBasicBlock::iterator &MBBI, 396*0b57cec5SDimitry Andric bool doMergeWithPrevious) const { 397*0b57cec5SDimitry Andric if ((doMergeWithPrevious && MBBI == MBB.begin()) || 398*0b57cec5SDimitry Andric (!doMergeWithPrevious && MBBI == MBB.end())) 399*0b57cec5SDimitry Andric return 0; 400*0b57cec5SDimitry Andric 401*0b57cec5SDimitry Andric MachineBasicBlock::iterator PI = doMergeWithPrevious ? std::prev(MBBI) : MBBI; 402*0b57cec5SDimitry Andric 403*0b57cec5SDimitry Andric PI = skipDebugInstructionsBackward(PI, MBB.begin()); 404*0b57cec5SDimitry Andric // It is assumed that ADD/SUB/LEA instruction is succeded by one CFI 405*0b57cec5SDimitry Andric // instruction, and that there are no DBG_VALUE or other instructions between 406*0b57cec5SDimitry Andric // ADD/SUB/LEA and its corresponding CFI instruction. 407*0b57cec5SDimitry Andric /* TODO: Add support for the case where there are multiple CFI instructions 408*0b57cec5SDimitry Andric below the ADD/SUB/LEA, e.g.: 409*0b57cec5SDimitry Andric ... 410*0b57cec5SDimitry Andric add 411*0b57cec5SDimitry Andric cfi_def_cfa_offset 412*0b57cec5SDimitry Andric cfi_offset 413*0b57cec5SDimitry Andric ... 414*0b57cec5SDimitry Andric */ 415*0b57cec5SDimitry Andric if (doMergeWithPrevious && PI != MBB.begin() && PI->isCFIInstruction()) 416*0b57cec5SDimitry Andric PI = std::prev(PI); 417*0b57cec5SDimitry Andric 418*0b57cec5SDimitry Andric unsigned Opc = PI->getOpcode(); 419*0b57cec5SDimitry Andric int Offset = 0; 420*0b57cec5SDimitry Andric 421*0b57cec5SDimitry Andric if ((Opc == X86::ADD64ri32 || Opc == X86::ADD64ri8 || 422*0b57cec5SDimitry Andric Opc == X86::ADD32ri || Opc == X86::ADD32ri8) && 423*0b57cec5SDimitry Andric PI->getOperand(0).getReg() == StackPtr){ 424*0b57cec5SDimitry Andric assert(PI->getOperand(1).getReg() == StackPtr); 425*0b57cec5SDimitry Andric Offset = PI->getOperand(2).getImm(); 426*0b57cec5SDimitry Andric } else if ((Opc == X86::LEA32r || Opc == X86::LEA64_32r) && 427*0b57cec5SDimitry Andric PI->getOperand(0).getReg() == StackPtr && 428*0b57cec5SDimitry Andric PI->getOperand(1).getReg() == StackPtr && 429*0b57cec5SDimitry Andric PI->getOperand(2).getImm() == 1 && 430*0b57cec5SDimitry Andric PI->getOperand(3).getReg() == X86::NoRegister && 431*0b57cec5SDimitry Andric PI->getOperand(5).getReg() == X86::NoRegister) { 432*0b57cec5SDimitry Andric // For LEAs we have: def = lea SP, FI, noreg, Offset, noreg. 433*0b57cec5SDimitry Andric Offset = PI->getOperand(4).getImm(); 434*0b57cec5SDimitry Andric } else if ((Opc == X86::SUB64ri32 || Opc == X86::SUB64ri8 || 435*0b57cec5SDimitry Andric Opc == X86::SUB32ri || Opc == X86::SUB32ri8) && 436*0b57cec5SDimitry Andric PI->getOperand(0).getReg() == StackPtr) { 437*0b57cec5SDimitry Andric assert(PI->getOperand(1).getReg() == StackPtr); 438*0b57cec5SDimitry Andric Offset = -PI->getOperand(2).getImm(); 439*0b57cec5SDimitry Andric } else 440*0b57cec5SDimitry Andric return 0; 441*0b57cec5SDimitry Andric 442*0b57cec5SDimitry Andric PI = MBB.erase(PI); 443*0b57cec5SDimitry Andric if (PI != MBB.end() && PI->isCFIInstruction()) PI = MBB.erase(PI); 444*0b57cec5SDimitry Andric if (!doMergeWithPrevious) 445*0b57cec5SDimitry Andric MBBI = skipDebugInstructionsForward(PI, MBB.end()); 446*0b57cec5SDimitry Andric 447*0b57cec5SDimitry Andric return Offset; 448*0b57cec5SDimitry Andric } 449*0b57cec5SDimitry Andric 450*0b57cec5SDimitry Andric void X86FrameLowering::BuildCFI(MachineBasicBlock &MBB, 451*0b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI, 452*0b57cec5SDimitry Andric const DebugLoc &DL, 453*0b57cec5SDimitry Andric const MCCFIInstruction &CFIInst) const { 454*0b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 455*0b57cec5SDimitry Andric unsigned CFIIndex = MF.addFrameInst(CFIInst); 456*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION)) 457*0b57cec5SDimitry Andric .addCFIIndex(CFIIndex); 458*0b57cec5SDimitry Andric } 459*0b57cec5SDimitry Andric 460*0b57cec5SDimitry Andric void X86FrameLowering::emitCalleeSavedFrameMoves( 461*0b57cec5SDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, 462*0b57cec5SDimitry Andric const DebugLoc &DL) const { 463*0b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 464*0b57cec5SDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 465*0b57cec5SDimitry Andric MachineModuleInfo &MMI = MF.getMMI(); 466*0b57cec5SDimitry Andric const MCRegisterInfo *MRI = MMI.getContext().getRegisterInfo(); 467*0b57cec5SDimitry Andric 468*0b57cec5SDimitry Andric // Add callee saved registers to move list. 469*0b57cec5SDimitry Andric const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo(); 470*0b57cec5SDimitry Andric if (CSI.empty()) return; 471*0b57cec5SDimitry Andric 472*0b57cec5SDimitry Andric // Calculate offsets. 473*0b57cec5SDimitry Andric for (std::vector<CalleeSavedInfo>::const_iterator 474*0b57cec5SDimitry Andric I = CSI.begin(), E = CSI.end(); I != E; ++I) { 475*0b57cec5SDimitry Andric int64_t Offset = MFI.getObjectOffset(I->getFrameIdx()); 476*0b57cec5SDimitry Andric unsigned Reg = I->getReg(); 477*0b57cec5SDimitry Andric 478*0b57cec5SDimitry Andric unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true); 479*0b57cec5SDimitry Andric BuildCFI(MBB, MBBI, DL, 480*0b57cec5SDimitry Andric MCCFIInstruction::createOffset(nullptr, DwarfReg, Offset)); 481*0b57cec5SDimitry Andric } 482*0b57cec5SDimitry Andric } 483*0b57cec5SDimitry Andric 484*0b57cec5SDimitry Andric void X86FrameLowering::emitStackProbe(MachineFunction &MF, 485*0b57cec5SDimitry Andric MachineBasicBlock &MBB, 486*0b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI, 487*0b57cec5SDimitry Andric const DebugLoc &DL, bool InProlog) const { 488*0b57cec5SDimitry Andric const X86Subtarget &STI = MF.getSubtarget<X86Subtarget>(); 489*0b57cec5SDimitry Andric if (STI.isTargetWindowsCoreCLR()) { 490*0b57cec5SDimitry Andric if (InProlog) { 491*0b57cec5SDimitry Andric emitStackProbeInlineStub(MF, MBB, MBBI, DL, true); 492*0b57cec5SDimitry Andric } else { 493*0b57cec5SDimitry Andric emitStackProbeInline(MF, MBB, MBBI, DL, false); 494*0b57cec5SDimitry Andric } 495*0b57cec5SDimitry Andric } else { 496*0b57cec5SDimitry Andric emitStackProbeCall(MF, MBB, MBBI, DL, InProlog); 497*0b57cec5SDimitry Andric } 498*0b57cec5SDimitry Andric } 499*0b57cec5SDimitry Andric 500*0b57cec5SDimitry Andric void X86FrameLowering::inlineStackProbe(MachineFunction &MF, 501*0b57cec5SDimitry Andric MachineBasicBlock &PrologMBB) const { 502*0b57cec5SDimitry Andric const StringRef ChkStkStubSymbol = "__chkstk_stub"; 503*0b57cec5SDimitry Andric MachineInstr *ChkStkStub = nullptr; 504*0b57cec5SDimitry Andric 505*0b57cec5SDimitry Andric for (MachineInstr &MI : PrologMBB) { 506*0b57cec5SDimitry Andric if (MI.isCall() && MI.getOperand(0).isSymbol() && 507*0b57cec5SDimitry Andric ChkStkStubSymbol == MI.getOperand(0).getSymbolName()) { 508*0b57cec5SDimitry Andric ChkStkStub = &MI; 509*0b57cec5SDimitry Andric break; 510*0b57cec5SDimitry Andric } 511*0b57cec5SDimitry Andric } 512*0b57cec5SDimitry Andric 513*0b57cec5SDimitry Andric if (ChkStkStub != nullptr) { 514*0b57cec5SDimitry Andric assert(!ChkStkStub->isBundled() && 515*0b57cec5SDimitry Andric "Not expecting bundled instructions here"); 516*0b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = std::next(ChkStkStub->getIterator()); 517*0b57cec5SDimitry Andric assert(std::prev(MBBI) == ChkStkStub && 518*0b57cec5SDimitry Andric "MBBI expected after __chkstk_stub."); 519*0b57cec5SDimitry Andric DebugLoc DL = PrologMBB.findDebugLoc(MBBI); 520*0b57cec5SDimitry Andric emitStackProbeInline(MF, PrologMBB, MBBI, DL, true); 521*0b57cec5SDimitry Andric ChkStkStub->eraseFromParent(); 522*0b57cec5SDimitry Andric } 523*0b57cec5SDimitry Andric } 524*0b57cec5SDimitry Andric 525*0b57cec5SDimitry Andric void X86FrameLowering::emitStackProbeInline(MachineFunction &MF, 526*0b57cec5SDimitry Andric MachineBasicBlock &MBB, 527*0b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI, 528*0b57cec5SDimitry Andric const DebugLoc &DL, 529*0b57cec5SDimitry Andric bool InProlog) const { 530*0b57cec5SDimitry Andric const X86Subtarget &STI = MF.getSubtarget<X86Subtarget>(); 531*0b57cec5SDimitry Andric assert(STI.is64Bit() && "different expansion needed for 32 bit"); 532*0b57cec5SDimitry Andric assert(STI.isTargetWindowsCoreCLR() && "custom expansion expects CoreCLR"); 533*0b57cec5SDimitry Andric const TargetInstrInfo &TII = *STI.getInstrInfo(); 534*0b57cec5SDimitry Andric const BasicBlock *LLVM_BB = MBB.getBasicBlock(); 535*0b57cec5SDimitry Andric 536*0b57cec5SDimitry Andric // RAX contains the number of bytes of desired stack adjustment. 537*0b57cec5SDimitry Andric // The handling here assumes this value has already been updated so as to 538*0b57cec5SDimitry Andric // maintain stack alignment. 539*0b57cec5SDimitry Andric // 540*0b57cec5SDimitry Andric // We need to exit with RSP modified by this amount and execute suitable 541*0b57cec5SDimitry Andric // page touches to notify the OS that we're growing the stack responsibly. 542*0b57cec5SDimitry Andric // All stack probing must be done without modifying RSP. 543*0b57cec5SDimitry Andric // 544*0b57cec5SDimitry Andric // MBB: 545*0b57cec5SDimitry Andric // SizeReg = RAX; 546*0b57cec5SDimitry Andric // ZeroReg = 0 547*0b57cec5SDimitry Andric // CopyReg = RSP 548*0b57cec5SDimitry Andric // Flags, TestReg = CopyReg - SizeReg 549*0b57cec5SDimitry Andric // FinalReg = !Flags.Ovf ? TestReg : ZeroReg 550*0b57cec5SDimitry Andric // LimitReg = gs magic thread env access 551*0b57cec5SDimitry Andric // if FinalReg >= LimitReg goto ContinueMBB 552*0b57cec5SDimitry Andric // RoundBB: 553*0b57cec5SDimitry Andric // RoundReg = page address of FinalReg 554*0b57cec5SDimitry Andric // LoopMBB: 555*0b57cec5SDimitry Andric // LoopReg = PHI(LimitReg,ProbeReg) 556*0b57cec5SDimitry Andric // ProbeReg = LoopReg - PageSize 557*0b57cec5SDimitry Andric // [ProbeReg] = 0 558*0b57cec5SDimitry Andric // if (ProbeReg > RoundReg) goto LoopMBB 559*0b57cec5SDimitry Andric // ContinueMBB: 560*0b57cec5SDimitry Andric // RSP = RSP - RAX 561*0b57cec5SDimitry Andric // [rest of original MBB] 562*0b57cec5SDimitry Andric 563*0b57cec5SDimitry Andric // Set up the new basic blocks 564*0b57cec5SDimitry Andric MachineBasicBlock *RoundMBB = MF.CreateMachineBasicBlock(LLVM_BB); 565*0b57cec5SDimitry Andric MachineBasicBlock *LoopMBB = MF.CreateMachineBasicBlock(LLVM_BB); 566*0b57cec5SDimitry Andric MachineBasicBlock *ContinueMBB = MF.CreateMachineBasicBlock(LLVM_BB); 567*0b57cec5SDimitry Andric 568*0b57cec5SDimitry Andric MachineFunction::iterator MBBIter = std::next(MBB.getIterator()); 569*0b57cec5SDimitry Andric MF.insert(MBBIter, RoundMBB); 570*0b57cec5SDimitry Andric MF.insert(MBBIter, LoopMBB); 571*0b57cec5SDimitry Andric MF.insert(MBBIter, ContinueMBB); 572*0b57cec5SDimitry Andric 573*0b57cec5SDimitry Andric // Split MBB and move the tail portion down to ContinueMBB. 574*0b57cec5SDimitry Andric MachineBasicBlock::iterator BeforeMBBI = std::prev(MBBI); 575*0b57cec5SDimitry Andric ContinueMBB->splice(ContinueMBB->begin(), &MBB, MBBI, MBB.end()); 576*0b57cec5SDimitry Andric ContinueMBB->transferSuccessorsAndUpdatePHIs(&MBB); 577*0b57cec5SDimitry Andric 578*0b57cec5SDimitry Andric // Some useful constants 579*0b57cec5SDimitry Andric const int64_t ThreadEnvironmentStackLimit = 0x10; 580*0b57cec5SDimitry Andric const int64_t PageSize = 0x1000; 581*0b57cec5SDimitry Andric const int64_t PageMask = ~(PageSize - 1); 582*0b57cec5SDimitry Andric 583*0b57cec5SDimitry Andric // Registers we need. For the normal case we use virtual 584*0b57cec5SDimitry Andric // registers. For the prolog expansion we use RAX, RCX and RDX. 585*0b57cec5SDimitry Andric MachineRegisterInfo &MRI = MF.getRegInfo(); 586*0b57cec5SDimitry Andric const TargetRegisterClass *RegClass = &X86::GR64RegClass; 587*0b57cec5SDimitry Andric const Register SizeReg = InProlog ? X86::RAX 588*0b57cec5SDimitry Andric : MRI.createVirtualRegister(RegClass), 589*0b57cec5SDimitry Andric ZeroReg = InProlog ? X86::RCX 590*0b57cec5SDimitry Andric : MRI.createVirtualRegister(RegClass), 591*0b57cec5SDimitry Andric CopyReg = InProlog ? X86::RDX 592*0b57cec5SDimitry Andric : MRI.createVirtualRegister(RegClass), 593*0b57cec5SDimitry Andric TestReg = InProlog ? X86::RDX 594*0b57cec5SDimitry Andric : MRI.createVirtualRegister(RegClass), 595*0b57cec5SDimitry Andric FinalReg = InProlog ? X86::RDX 596*0b57cec5SDimitry Andric : MRI.createVirtualRegister(RegClass), 597*0b57cec5SDimitry Andric RoundedReg = InProlog ? X86::RDX 598*0b57cec5SDimitry Andric : MRI.createVirtualRegister(RegClass), 599*0b57cec5SDimitry Andric LimitReg = InProlog ? X86::RCX 600*0b57cec5SDimitry Andric : MRI.createVirtualRegister(RegClass), 601*0b57cec5SDimitry Andric JoinReg = InProlog ? X86::RCX 602*0b57cec5SDimitry Andric : MRI.createVirtualRegister(RegClass), 603*0b57cec5SDimitry Andric ProbeReg = InProlog ? X86::RCX 604*0b57cec5SDimitry Andric : MRI.createVirtualRegister(RegClass); 605*0b57cec5SDimitry Andric 606*0b57cec5SDimitry Andric // SP-relative offsets where we can save RCX and RDX. 607*0b57cec5SDimitry Andric int64_t RCXShadowSlot = 0; 608*0b57cec5SDimitry Andric int64_t RDXShadowSlot = 0; 609*0b57cec5SDimitry Andric 610*0b57cec5SDimitry Andric // If inlining in the prolog, save RCX and RDX. 611*0b57cec5SDimitry Andric if (InProlog) { 612*0b57cec5SDimitry Andric // Compute the offsets. We need to account for things already 613*0b57cec5SDimitry Andric // pushed onto the stack at this point: return address, frame 614*0b57cec5SDimitry Andric // pointer (if used), and callee saves. 615*0b57cec5SDimitry Andric X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); 616*0b57cec5SDimitry Andric const int64_t CalleeSaveSize = X86FI->getCalleeSavedFrameSize(); 617*0b57cec5SDimitry Andric const bool HasFP = hasFP(MF); 618*0b57cec5SDimitry Andric 619*0b57cec5SDimitry Andric // Check if we need to spill RCX and/or RDX. 620*0b57cec5SDimitry Andric // Here we assume that no earlier prologue instruction changes RCX and/or 621*0b57cec5SDimitry Andric // RDX, so checking the block live-ins is enough. 622*0b57cec5SDimitry Andric const bool IsRCXLiveIn = MBB.isLiveIn(X86::RCX); 623*0b57cec5SDimitry Andric const bool IsRDXLiveIn = MBB.isLiveIn(X86::RDX); 624*0b57cec5SDimitry Andric int64_t InitSlot = 8 + CalleeSaveSize + (HasFP ? 8 : 0); 625*0b57cec5SDimitry Andric // Assign the initial slot to both registers, then change RDX's slot if both 626*0b57cec5SDimitry Andric // need to be spilled. 627*0b57cec5SDimitry Andric if (IsRCXLiveIn) 628*0b57cec5SDimitry Andric RCXShadowSlot = InitSlot; 629*0b57cec5SDimitry Andric if (IsRDXLiveIn) 630*0b57cec5SDimitry Andric RDXShadowSlot = InitSlot; 631*0b57cec5SDimitry Andric if (IsRDXLiveIn && IsRCXLiveIn) 632*0b57cec5SDimitry Andric RDXShadowSlot += 8; 633*0b57cec5SDimitry Andric // Emit the saves if needed. 634*0b57cec5SDimitry Andric if (IsRCXLiveIn) 635*0b57cec5SDimitry Andric addRegOffset(BuildMI(&MBB, DL, TII.get(X86::MOV64mr)), X86::RSP, false, 636*0b57cec5SDimitry Andric RCXShadowSlot) 637*0b57cec5SDimitry Andric .addReg(X86::RCX); 638*0b57cec5SDimitry Andric if (IsRDXLiveIn) 639*0b57cec5SDimitry Andric addRegOffset(BuildMI(&MBB, DL, TII.get(X86::MOV64mr)), X86::RSP, false, 640*0b57cec5SDimitry Andric RDXShadowSlot) 641*0b57cec5SDimitry Andric .addReg(X86::RDX); 642*0b57cec5SDimitry Andric } else { 643*0b57cec5SDimitry Andric // Not in the prolog. Copy RAX to a virtual reg. 644*0b57cec5SDimitry Andric BuildMI(&MBB, DL, TII.get(X86::MOV64rr), SizeReg).addReg(X86::RAX); 645*0b57cec5SDimitry Andric } 646*0b57cec5SDimitry Andric 647*0b57cec5SDimitry Andric // Add code to MBB to check for overflow and set the new target stack pointer 648*0b57cec5SDimitry Andric // to zero if so. 649*0b57cec5SDimitry Andric BuildMI(&MBB, DL, TII.get(X86::XOR64rr), ZeroReg) 650*0b57cec5SDimitry Andric .addReg(ZeroReg, RegState::Undef) 651*0b57cec5SDimitry Andric .addReg(ZeroReg, RegState::Undef); 652*0b57cec5SDimitry Andric BuildMI(&MBB, DL, TII.get(X86::MOV64rr), CopyReg).addReg(X86::RSP); 653*0b57cec5SDimitry Andric BuildMI(&MBB, DL, TII.get(X86::SUB64rr), TestReg) 654*0b57cec5SDimitry Andric .addReg(CopyReg) 655*0b57cec5SDimitry Andric .addReg(SizeReg); 656*0b57cec5SDimitry Andric BuildMI(&MBB, DL, TII.get(X86::CMOV64rr), FinalReg) 657*0b57cec5SDimitry Andric .addReg(TestReg) 658*0b57cec5SDimitry Andric .addReg(ZeroReg) 659*0b57cec5SDimitry Andric .addImm(X86::COND_B); 660*0b57cec5SDimitry Andric 661*0b57cec5SDimitry Andric // FinalReg now holds final stack pointer value, or zero if 662*0b57cec5SDimitry Andric // allocation would overflow. Compare against the current stack 663*0b57cec5SDimitry Andric // limit from the thread environment block. Note this limit is the 664*0b57cec5SDimitry Andric // lowest touched page on the stack, not the point at which the OS 665*0b57cec5SDimitry Andric // will cause an overflow exception, so this is just an optimization 666*0b57cec5SDimitry Andric // to avoid unnecessarily touching pages that are below the current 667*0b57cec5SDimitry Andric // SP but already committed to the stack by the OS. 668*0b57cec5SDimitry Andric BuildMI(&MBB, DL, TII.get(X86::MOV64rm), LimitReg) 669*0b57cec5SDimitry Andric .addReg(0) 670*0b57cec5SDimitry Andric .addImm(1) 671*0b57cec5SDimitry Andric .addReg(0) 672*0b57cec5SDimitry Andric .addImm(ThreadEnvironmentStackLimit) 673*0b57cec5SDimitry Andric .addReg(X86::GS); 674*0b57cec5SDimitry Andric BuildMI(&MBB, DL, TII.get(X86::CMP64rr)).addReg(FinalReg).addReg(LimitReg); 675*0b57cec5SDimitry Andric // Jump if the desired stack pointer is at or above the stack limit. 676*0b57cec5SDimitry Andric BuildMI(&MBB, DL, TII.get(X86::JCC_1)).addMBB(ContinueMBB).addImm(X86::COND_AE); 677*0b57cec5SDimitry Andric 678*0b57cec5SDimitry Andric // Add code to roundMBB to round the final stack pointer to a page boundary. 679*0b57cec5SDimitry Andric RoundMBB->addLiveIn(FinalReg); 680*0b57cec5SDimitry Andric BuildMI(RoundMBB, DL, TII.get(X86::AND64ri32), RoundedReg) 681*0b57cec5SDimitry Andric .addReg(FinalReg) 682*0b57cec5SDimitry Andric .addImm(PageMask); 683*0b57cec5SDimitry Andric BuildMI(RoundMBB, DL, TII.get(X86::JMP_1)).addMBB(LoopMBB); 684*0b57cec5SDimitry Andric 685*0b57cec5SDimitry Andric // LimitReg now holds the current stack limit, RoundedReg page-rounded 686*0b57cec5SDimitry Andric // final RSP value. Add code to loopMBB to decrement LimitReg page-by-page 687*0b57cec5SDimitry Andric // and probe until we reach RoundedReg. 688*0b57cec5SDimitry Andric if (!InProlog) { 689*0b57cec5SDimitry Andric BuildMI(LoopMBB, DL, TII.get(X86::PHI), JoinReg) 690*0b57cec5SDimitry Andric .addReg(LimitReg) 691*0b57cec5SDimitry Andric .addMBB(RoundMBB) 692*0b57cec5SDimitry Andric .addReg(ProbeReg) 693*0b57cec5SDimitry Andric .addMBB(LoopMBB); 694*0b57cec5SDimitry Andric } 695*0b57cec5SDimitry Andric 696*0b57cec5SDimitry Andric LoopMBB->addLiveIn(JoinReg); 697*0b57cec5SDimitry Andric addRegOffset(BuildMI(LoopMBB, DL, TII.get(X86::LEA64r), ProbeReg), JoinReg, 698*0b57cec5SDimitry Andric false, -PageSize); 699*0b57cec5SDimitry Andric 700*0b57cec5SDimitry Andric // Probe by storing a byte onto the stack. 701*0b57cec5SDimitry Andric BuildMI(LoopMBB, DL, TII.get(X86::MOV8mi)) 702*0b57cec5SDimitry Andric .addReg(ProbeReg) 703*0b57cec5SDimitry Andric .addImm(1) 704*0b57cec5SDimitry Andric .addReg(0) 705*0b57cec5SDimitry Andric .addImm(0) 706*0b57cec5SDimitry Andric .addReg(0) 707*0b57cec5SDimitry Andric .addImm(0); 708*0b57cec5SDimitry Andric 709*0b57cec5SDimitry Andric LoopMBB->addLiveIn(RoundedReg); 710*0b57cec5SDimitry Andric BuildMI(LoopMBB, DL, TII.get(X86::CMP64rr)) 711*0b57cec5SDimitry Andric .addReg(RoundedReg) 712*0b57cec5SDimitry Andric .addReg(ProbeReg); 713*0b57cec5SDimitry Andric BuildMI(LoopMBB, DL, TII.get(X86::JCC_1)).addMBB(LoopMBB).addImm(X86::COND_NE); 714*0b57cec5SDimitry Andric 715*0b57cec5SDimitry Andric MachineBasicBlock::iterator ContinueMBBI = ContinueMBB->getFirstNonPHI(); 716*0b57cec5SDimitry Andric 717*0b57cec5SDimitry Andric // If in prolog, restore RDX and RCX. 718*0b57cec5SDimitry Andric if (InProlog) { 719*0b57cec5SDimitry Andric if (RCXShadowSlot) // It means we spilled RCX in the prologue. 720*0b57cec5SDimitry Andric addRegOffset(BuildMI(*ContinueMBB, ContinueMBBI, DL, 721*0b57cec5SDimitry Andric TII.get(X86::MOV64rm), X86::RCX), 722*0b57cec5SDimitry Andric X86::RSP, false, RCXShadowSlot); 723*0b57cec5SDimitry Andric if (RDXShadowSlot) // It means we spilled RDX in the prologue. 724*0b57cec5SDimitry Andric addRegOffset(BuildMI(*ContinueMBB, ContinueMBBI, DL, 725*0b57cec5SDimitry Andric TII.get(X86::MOV64rm), X86::RDX), 726*0b57cec5SDimitry Andric X86::RSP, false, RDXShadowSlot); 727*0b57cec5SDimitry Andric } 728*0b57cec5SDimitry Andric 729*0b57cec5SDimitry Andric // Now that the probing is done, add code to continueMBB to update 730*0b57cec5SDimitry Andric // the stack pointer for real. 731*0b57cec5SDimitry Andric ContinueMBB->addLiveIn(SizeReg); 732*0b57cec5SDimitry Andric BuildMI(*ContinueMBB, ContinueMBBI, DL, TII.get(X86::SUB64rr), X86::RSP) 733*0b57cec5SDimitry Andric .addReg(X86::RSP) 734*0b57cec5SDimitry Andric .addReg(SizeReg); 735*0b57cec5SDimitry Andric 736*0b57cec5SDimitry Andric // Add the control flow edges we need. 737*0b57cec5SDimitry Andric MBB.addSuccessor(ContinueMBB); 738*0b57cec5SDimitry Andric MBB.addSuccessor(RoundMBB); 739*0b57cec5SDimitry Andric RoundMBB->addSuccessor(LoopMBB); 740*0b57cec5SDimitry Andric LoopMBB->addSuccessor(ContinueMBB); 741*0b57cec5SDimitry Andric LoopMBB->addSuccessor(LoopMBB); 742*0b57cec5SDimitry Andric 743*0b57cec5SDimitry Andric // Mark all the instructions added to the prolog as frame setup. 744*0b57cec5SDimitry Andric if (InProlog) { 745*0b57cec5SDimitry Andric for (++BeforeMBBI; BeforeMBBI != MBB.end(); ++BeforeMBBI) { 746*0b57cec5SDimitry Andric BeforeMBBI->setFlag(MachineInstr::FrameSetup); 747*0b57cec5SDimitry Andric } 748*0b57cec5SDimitry Andric for (MachineInstr &MI : *RoundMBB) { 749*0b57cec5SDimitry Andric MI.setFlag(MachineInstr::FrameSetup); 750*0b57cec5SDimitry Andric } 751*0b57cec5SDimitry Andric for (MachineInstr &MI : *LoopMBB) { 752*0b57cec5SDimitry Andric MI.setFlag(MachineInstr::FrameSetup); 753*0b57cec5SDimitry Andric } 754*0b57cec5SDimitry Andric for (MachineBasicBlock::iterator CMBBI = ContinueMBB->begin(); 755*0b57cec5SDimitry Andric CMBBI != ContinueMBBI; ++CMBBI) { 756*0b57cec5SDimitry Andric CMBBI->setFlag(MachineInstr::FrameSetup); 757*0b57cec5SDimitry Andric } 758*0b57cec5SDimitry Andric } 759*0b57cec5SDimitry Andric } 760*0b57cec5SDimitry Andric 761*0b57cec5SDimitry Andric void X86FrameLowering::emitStackProbeCall(MachineFunction &MF, 762*0b57cec5SDimitry Andric MachineBasicBlock &MBB, 763*0b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI, 764*0b57cec5SDimitry Andric const DebugLoc &DL, 765*0b57cec5SDimitry Andric bool InProlog) const { 766*0b57cec5SDimitry Andric bool IsLargeCodeModel = MF.getTarget().getCodeModel() == CodeModel::Large; 767*0b57cec5SDimitry Andric 768*0b57cec5SDimitry Andric // FIXME: Add retpoline support and remove this. 769*0b57cec5SDimitry Andric if (Is64Bit && IsLargeCodeModel && STI.useRetpolineIndirectCalls()) 770*0b57cec5SDimitry Andric report_fatal_error("Emitting stack probe calls on 64-bit with the large " 771*0b57cec5SDimitry Andric "code model and retpoline not yet implemented."); 772*0b57cec5SDimitry Andric 773*0b57cec5SDimitry Andric unsigned CallOp; 774*0b57cec5SDimitry Andric if (Is64Bit) 775*0b57cec5SDimitry Andric CallOp = IsLargeCodeModel ? X86::CALL64r : X86::CALL64pcrel32; 776*0b57cec5SDimitry Andric else 777*0b57cec5SDimitry Andric CallOp = X86::CALLpcrel32; 778*0b57cec5SDimitry Andric 779*0b57cec5SDimitry Andric StringRef Symbol = STI.getTargetLowering()->getStackProbeSymbolName(MF); 780*0b57cec5SDimitry Andric 781*0b57cec5SDimitry Andric MachineInstrBuilder CI; 782*0b57cec5SDimitry Andric MachineBasicBlock::iterator ExpansionMBBI = std::prev(MBBI); 783*0b57cec5SDimitry Andric 784*0b57cec5SDimitry Andric // All current stack probes take AX and SP as input, clobber flags, and 785*0b57cec5SDimitry Andric // preserve all registers. x86_64 probes leave RSP unmodified. 786*0b57cec5SDimitry Andric if (Is64Bit && MF.getTarget().getCodeModel() == CodeModel::Large) { 787*0b57cec5SDimitry Andric // For the large code model, we have to call through a register. Use R11, 788*0b57cec5SDimitry Andric // as it is scratch in all supported calling conventions. 789*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64ri), X86::R11) 790*0b57cec5SDimitry Andric .addExternalSymbol(MF.createExternalSymbolName(Symbol)); 791*0b57cec5SDimitry Andric CI = BuildMI(MBB, MBBI, DL, TII.get(CallOp)).addReg(X86::R11); 792*0b57cec5SDimitry Andric } else { 793*0b57cec5SDimitry Andric CI = BuildMI(MBB, MBBI, DL, TII.get(CallOp)) 794*0b57cec5SDimitry Andric .addExternalSymbol(MF.createExternalSymbolName(Symbol)); 795*0b57cec5SDimitry Andric } 796*0b57cec5SDimitry Andric 797*0b57cec5SDimitry Andric unsigned AX = Uses64BitFramePtr ? X86::RAX : X86::EAX; 798*0b57cec5SDimitry Andric unsigned SP = Uses64BitFramePtr ? X86::RSP : X86::ESP; 799*0b57cec5SDimitry Andric CI.addReg(AX, RegState::Implicit) 800*0b57cec5SDimitry Andric .addReg(SP, RegState::Implicit) 801*0b57cec5SDimitry Andric .addReg(AX, RegState::Define | RegState::Implicit) 802*0b57cec5SDimitry Andric .addReg(SP, RegState::Define | RegState::Implicit) 803*0b57cec5SDimitry Andric .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit); 804*0b57cec5SDimitry Andric 805*0b57cec5SDimitry Andric if (STI.isTargetWin64() || !STI.isOSWindows()) { 806*0b57cec5SDimitry Andric // MSVC x32's _chkstk and cygwin/mingw's _alloca adjust %esp themselves. 807*0b57cec5SDimitry Andric // MSVC x64's __chkstk and cygwin/mingw's ___chkstk_ms do not adjust %rsp 808*0b57cec5SDimitry Andric // themselves. They also does not clobber %rax so we can reuse it when 809*0b57cec5SDimitry Andric // adjusting %rsp. 810*0b57cec5SDimitry Andric // All other platforms do not specify a particular ABI for the stack probe 811*0b57cec5SDimitry Andric // function, so we arbitrarily define it to not adjust %esp/%rsp itself. 812*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(getSUBrrOpcode(Uses64BitFramePtr)), SP) 813*0b57cec5SDimitry Andric .addReg(SP) 814*0b57cec5SDimitry Andric .addReg(AX); 815*0b57cec5SDimitry Andric } 816*0b57cec5SDimitry Andric 817*0b57cec5SDimitry Andric if (InProlog) { 818*0b57cec5SDimitry Andric // Apply the frame setup flag to all inserted instrs. 819*0b57cec5SDimitry Andric for (++ExpansionMBBI; ExpansionMBBI != MBBI; ++ExpansionMBBI) 820*0b57cec5SDimitry Andric ExpansionMBBI->setFlag(MachineInstr::FrameSetup); 821*0b57cec5SDimitry Andric } 822*0b57cec5SDimitry Andric } 823*0b57cec5SDimitry Andric 824*0b57cec5SDimitry Andric void X86FrameLowering::emitStackProbeInlineStub( 825*0b57cec5SDimitry Andric MachineFunction &MF, MachineBasicBlock &MBB, 826*0b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool InProlog) const { 827*0b57cec5SDimitry Andric 828*0b57cec5SDimitry Andric assert(InProlog && "ChkStkStub called outside prolog!"); 829*0b57cec5SDimitry Andric 830*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(X86::CALLpcrel32)) 831*0b57cec5SDimitry Andric .addExternalSymbol("__chkstk_stub"); 832*0b57cec5SDimitry Andric } 833*0b57cec5SDimitry Andric 834*0b57cec5SDimitry Andric static unsigned calculateSetFPREG(uint64_t SPAdjust) { 835*0b57cec5SDimitry Andric // Win64 ABI has a less restrictive limitation of 240; 128 works equally well 836*0b57cec5SDimitry Andric // and might require smaller successive adjustments. 837*0b57cec5SDimitry Andric const uint64_t Win64MaxSEHOffset = 128; 838*0b57cec5SDimitry Andric uint64_t SEHFrameOffset = std::min(SPAdjust, Win64MaxSEHOffset); 839*0b57cec5SDimitry Andric // Win64 ABI requires 16-byte alignment for the UWOP_SET_FPREG opcode. 840*0b57cec5SDimitry Andric return SEHFrameOffset & -16; 841*0b57cec5SDimitry Andric } 842*0b57cec5SDimitry Andric 843*0b57cec5SDimitry Andric // If we're forcing a stack realignment we can't rely on just the frame 844*0b57cec5SDimitry Andric // info, we need to know the ABI stack alignment as well in case we 845*0b57cec5SDimitry Andric // have a call out. Otherwise just make sure we have some alignment - we'll 846*0b57cec5SDimitry Andric // go with the minimum SlotSize. 847*0b57cec5SDimitry Andric uint64_t X86FrameLowering::calculateMaxStackAlign(const MachineFunction &MF) const { 848*0b57cec5SDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo(); 849*0b57cec5SDimitry Andric uint64_t MaxAlign = MFI.getMaxAlignment(); // Desired stack alignment. 850*0b57cec5SDimitry Andric unsigned StackAlign = getStackAlignment(); 851*0b57cec5SDimitry Andric if (MF.getFunction().hasFnAttribute("stackrealign")) { 852*0b57cec5SDimitry Andric if (MFI.hasCalls()) 853*0b57cec5SDimitry Andric MaxAlign = (StackAlign > MaxAlign) ? StackAlign : MaxAlign; 854*0b57cec5SDimitry Andric else if (MaxAlign < SlotSize) 855*0b57cec5SDimitry Andric MaxAlign = SlotSize; 856*0b57cec5SDimitry Andric } 857*0b57cec5SDimitry Andric return MaxAlign; 858*0b57cec5SDimitry Andric } 859*0b57cec5SDimitry Andric 860*0b57cec5SDimitry Andric void X86FrameLowering::BuildStackAlignAND(MachineBasicBlock &MBB, 861*0b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI, 862*0b57cec5SDimitry Andric const DebugLoc &DL, unsigned Reg, 863*0b57cec5SDimitry Andric uint64_t MaxAlign) const { 864*0b57cec5SDimitry Andric uint64_t Val = -MaxAlign; 865*0b57cec5SDimitry Andric unsigned AndOp = getANDriOpcode(Uses64BitFramePtr, Val); 866*0b57cec5SDimitry Andric MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(AndOp), Reg) 867*0b57cec5SDimitry Andric .addReg(Reg) 868*0b57cec5SDimitry Andric .addImm(Val) 869*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 870*0b57cec5SDimitry Andric 871*0b57cec5SDimitry Andric // The EFLAGS implicit def is dead. 872*0b57cec5SDimitry Andric MI->getOperand(3).setIsDead(); 873*0b57cec5SDimitry Andric } 874*0b57cec5SDimitry Andric 875*0b57cec5SDimitry Andric bool X86FrameLowering::has128ByteRedZone(const MachineFunction& MF) const { 876*0b57cec5SDimitry Andric // x86-64 (non Win64) has a 128 byte red zone which is guaranteed not to be 877*0b57cec5SDimitry Andric // clobbered by any interrupt handler. 878*0b57cec5SDimitry Andric assert(&STI == &MF.getSubtarget<X86Subtarget>() && 879*0b57cec5SDimitry Andric "MF used frame lowering for wrong subtarget"); 880*0b57cec5SDimitry Andric const Function &Fn = MF.getFunction(); 881*0b57cec5SDimitry Andric const bool IsWin64CC = STI.isCallingConvWin64(Fn.getCallingConv()); 882*0b57cec5SDimitry Andric return Is64Bit && !IsWin64CC && !Fn.hasFnAttribute(Attribute::NoRedZone); 883*0b57cec5SDimitry Andric } 884*0b57cec5SDimitry Andric 885*0b57cec5SDimitry Andric 886*0b57cec5SDimitry Andric /// emitPrologue - Push callee-saved registers onto the stack, which 887*0b57cec5SDimitry Andric /// automatically adjust the stack pointer. Adjust the stack pointer to allocate 888*0b57cec5SDimitry Andric /// space for local variables. Also emit labels used by the exception handler to 889*0b57cec5SDimitry Andric /// generate the exception handling frames. 890*0b57cec5SDimitry Andric 891*0b57cec5SDimitry Andric /* 892*0b57cec5SDimitry Andric Here's a gist of what gets emitted: 893*0b57cec5SDimitry Andric 894*0b57cec5SDimitry Andric ; Establish frame pointer, if needed 895*0b57cec5SDimitry Andric [if needs FP] 896*0b57cec5SDimitry Andric push %rbp 897*0b57cec5SDimitry Andric .cfi_def_cfa_offset 16 898*0b57cec5SDimitry Andric .cfi_offset %rbp, -16 899*0b57cec5SDimitry Andric .seh_pushreg %rpb 900*0b57cec5SDimitry Andric mov %rsp, %rbp 901*0b57cec5SDimitry Andric .cfi_def_cfa_register %rbp 902*0b57cec5SDimitry Andric 903*0b57cec5SDimitry Andric ; Spill general-purpose registers 904*0b57cec5SDimitry Andric [for all callee-saved GPRs] 905*0b57cec5SDimitry Andric pushq %<reg> 906*0b57cec5SDimitry Andric [if not needs FP] 907*0b57cec5SDimitry Andric .cfi_def_cfa_offset (offset from RETADDR) 908*0b57cec5SDimitry Andric .seh_pushreg %<reg> 909*0b57cec5SDimitry Andric 910*0b57cec5SDimitry Andric ; If the required stack alignment > default stack alignment 911*0b57cec5SDimitry Andric ; rsp needs to be re-aligned. This creates a "re-alignment gap" 912*0b57cec5SDimitry Andric ; of unknown size in the stack frame. 913*0b57cec5SDimitry Andric [if stack needs re-alignment] 914*0b57cec5SDimitry Andric and $MASK, %rsp 915*0b57cec5SDimitry Andric 916*0b57cec5SDimitry Andric ; Allocate space for locals 917*0b57cec5SDimitry Andric [if target is Windows and allocated space > 4096 bytes] 918*0b57cec5SDimitry Andric ; Windows needs special care for allocations larger 919*0b57cec5SDimitry Andric ; than one page. 920*0b57cec5SDimitry Andric mov $NNN, %rax 921*0b57cec5SDimitry Andric call ___chkstk_ms/___chkstk 922*0b57cec5SDimitry Andric sub %rax, %rsp 923*0b57cec5SDimitry Andric [else] 924*0b57cec5SDimitry Andric sub $NNN, %rsp 925*0b57cec5SDimitry Andric 926*0b57cec5SDimitry Andric [if needs FP] 927*0b57cec5SDimitry Andric .seh_stackalloc (size of XMM spill slots) 928*0b57cec5SDimitry Andric .seh_setframe %rbp, SEHFrameOffset ; = size of all spill slots 929*0b57cec5SDimitry Andric [else] 930*0b57cec5SDimitry Andric .seh_stackalloc NNN 931*0b57cec5SDimitry Andric 932*0b57cec5SDimitry Andric ; Spill XMMs 933*0b57cec5SDimitry Andric ; Note, that while only Windows 64 ABI specifies XMMs as callee-preserved, 934*0b57cec5SDimitry Andric ; they may get spilled on any platform, if the current function 935*0b57cec5SDimitry Andric ; calls @llvm.eh.unwind.init 936*0b57cec5SDimitry Andric [if needs FP] 937*0b57cec5SDimitry Andric [for all callee-saved XMM registers] 938*0b57cec5SDimitry Andric movaps %<xmm reg>, -MMM(%rbp) 939*0b57cec5SDimitry Andric [for all callee-saved XMM registers] 940*0b57cec5SDimitry Andric .seh_savexmm %<xmm reg>, (-MMM + SEHFrameOffset) 941*0b57cec5SDimitry Andric ; i.e. the offset relative to (%rbp - SEHFrameOffset) 942*0b57cec5SDimitry Andric [else] 943*0b57cec5SDimitry Andric [for all callee-saved XMM registers] 944*0b57cec5SDimitry Andric movaps %<xmm reg>, KKK(%rsp) 945*0b57cec5SDimitry Andric [for all callee-saved XMM registers] 946*0b57cec5SDimitry Andric .seh_savexmm %<xmm reg>, KKK 947*0b57cec5SDimitry Andric 948*0b57cec5SDimitry Andric .seh_endprologue 949*0b57cec5SDimitry Andric 950*0b57cec5SDimitry Andric [if needs base pointer] 951*0b57cec5SDimitry Andric mov %rsp, %rbx 952*0b57cec5SDimitry Andric [if needs to restore base pointer] 953*0b57cec5SDimitry Andric mov %rsp, -MMM(%rbp) 954*0b57cec5SDimitry Andric 955*0b57cec5SDimitry Andric ; Emit CFI info 956*0b57cec5SDimitry Andric [if needs FP] 957*0b57cec5SDimitry Andric [for all callee-saved registers] 958*0b57cec5SDimitry Andric .cfi_offset %<reg>, (offset from %rbp) 959*0b57cec5SDimitry Andric [else] 960*0b57cec5SDimitry Andric .cfi_def_cfa_offset (offset from RETADDR) 961*0b57cec5SDimitry Andric [for all callee-saved registers] 962*0b57cec5SDimitry Andric .cfi_offset %<reg>, (offset from %rsp) 963*0b57cec5SDimitry Andric 964*0b57cec5SDimitry Andric Notes: 965*0b57cec5SDimitry Andric - .seh directives are emitted only for Windows 64 ABI 966*0b57cec5SDimitry Andric - .cv_fpo directives are emitted on win32 when emitting CodeView 967*0b57cec5SDimitry Andric - .cfi directives are emitted for all other ABIs 968*0b57cec5SDimitry Andric - for 32-bit code, substitute %e?? registers for %r?? 969*0b57cec5SDimitry Andric */ 970*0b57cec5SDimitry Andric 971*0b57cec5SDimitry Andric void X86FrameLowering::emitPrologue(MachineFunction &MF, 972*0b57cec5SDimitry Andric MachineBasicBlock &MBB) const { 973*0b57cec5SDimitry Andric assert(&STI == &MF.getSubtarget<X86Subtarget>() && 974*0b57cec5SDimitry Andric "MF used frame lowering for wrong subtarget"); 975*0b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.begin(); 976*0b57cec5SDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 977*0b57cec5SDimitry Andric const Function &Fn = MF.getFunction(); 978*0b57cec5SDimitry Andric MachineModuleInfo &MMI = MF.getMMI(); 979*0b57cec5SDimitry Andric X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); 980*0b57cec5SDimitry Andric uint64_t MaxAlign = calculateMaxStackAlign(MF); // Desired stack alignment. 981*0b57cec5SDimitry Andric uint64_t StackSize = MFI.getStackSize(); // Number of bytes to allocate. 982*0b57cec5SDimitry Andric bool IsFunclet = MBB.isEHFuncletEntry(); 983*0b57cec5SDimitry Andric EHPersonality Personality = EHPersonality::Unknown; 984*0b57cec5SDimitry Andric if (Fn.hasPersonalityFn()) 985*0b57cec5SDimitry Andric Personality = classifyEHPersonality(Fn.getPersonalityFn()); 986*0b57cec5SDimitry Andric bool FnHasClrFunclet = 987*0b57cec5SDimitry Andric MF.hasEHFunclets() && Personality == EHPersonality::CoreCLR; 988*0b57cec5SDimitry Andric bool IsClrFunclet = IsFunclet && FnHasClrFunclet; 989*0b57cec5SDimitry Andric bool HasFP = hasFP(MF); 990*0b57cec5SDimitry Andric bool IsWin64Prologue = MF.getTarget().getMCAsmInfo()->usesWindowsCFI(); 991*0b57cec5SDimitry Andric bool NeedsWin64CFI = IsWin64Prologue && Fn.needsUnwindTableEntry(); 992*0b57cec5SDimitry Andric // FIXME: Emit FPO data for EH funclets. 993*0b57cec5SDimitry Andric bool NeedsWinFPO = 994*0b57cec5SDimitry Andric !IsFunclet && STI.isTargetWin32() && MMI.getModule()->getCodeViewFlag(); 995*0b57cec5SDimitry Andric bool NeedsWinCFI = NeedsWin64CFI || NeedsWinFPO; 996*0b57cec5SDimitry Andric bool NeedsDwarfCFI = 997*0b57cec5SDimitry Andric !IsWin64Prologue && (MMI.hasDebugInfo() || Fn.needsUnwindTableEntry()); 998*0b57cec5SDimitry Andric unsigned FramePtr = TRI->getFrameRegister(MF); 999*0b57cec5SDimitry Andric const unsigned MachineFramePtr = 1000*0b57cec5SDimitry Andric STI.isTarget64BitILP32() 1001*0b57cec5SDimitry Andric ? getX86SubSuperRegister(FramePtr, 64) : FramePtr; 1002*0b57cec5SDimitry Andric unsigned BasePtr = TRI->getBaseRegister(); 1003*0b57cec5SDimitry Andric bool HasWinCFI = false; 1004*0b57cec5SDimitry Andric 1005*0b57cec5SDimitry Andric // Debug location must be unknown since the first debug location is used 1006*0b57cec5SDimitry Andric // to determine the end of the prologue. 1007*0b57cec5SDimitry Andric DebugLoc DL; 1008*0b57cec5SDimitry Andric 1009*0b57cec5SDimitry Andric // Add RETADDR move area to callee saved frame size. 1010*0b57cec5SDimitry Andric int TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta(); 1011*0b57cec5SDimitry Andric if (TailCallReturnAddrDelta && IsWin64Prologue) 1012*0b57cec5SDimitry Andric report_fatal_error("Can't handle guaranteed tail call under win64 yet"); 1013*0b57cec5SDimitry Andric 1014*0b57cec5SDimitry Andric if (TailCallReturnAddrDelta < 0) 1015*0b57cec5SDimitry Andric X86FI->setCalleeSavedFrameSize( 1016*0b57cec5SDimitry Andric X86FI->getCalleeSavedFrameSize() - TailCallReturnAddrDelta); 1017*0b57cec5SDimitry Andric 1018*0b57cec5SDimitry Andric bool UseStackProbe = !STI.getTargetLowering()->getStackProbeSymbolName(MF).empty(); 1019*0b57cec5SDimitry Andric 1020*0b57cec5SDimitry Andric // The default stack probe size is 4096 if the function has no stackprobesize 1021*0b57cec5SDimitry Andric // attribute. 1022*0b57cec5SDimitry Andric unsigned StackProbeSize = 4096; 1023*0b57cec5SDimitry Andric if (Fn.hasFnAttribute("stack-probe-size")) 1024*0b57cec5SDimitry Andric Fn.getFnAttribute("stack-probe-size") 1025*0b57cec5SDimitry Andric .getValueAsString() 1026*0b57cec5SDimitry Andric .getAsInteger(0, StackProbeSize); 1027*0b57cec5SDimitry Andric 1028*0b57cec5SDimitry Andric // Re-align the stack on 64-bit if the x86-interrupt calling convention is 1029*0b57cec5SDimitry Andric // used and an error code was pushed, since the x86-64 ABI requires a 16-byte 1030*0b57cec5SDimitry Andric // stack alignment. 1031*0b57cec5SDimitry Andric if (Fn.getCallingConv() == CallingConv::X86_INTR && Is64Bit && 1032*0b57cec5SDimitry Andric Fn.arg_size() == 2) { 1033*0b57cec5SDimitry Andric StackSize += 8; 1034*0b57cec5SDimitry Andric MFI.setStackSize(StackSize); 1035*0b57cec5SDimitry Andric emitSPUpdate(MBB, MBBI, DL, -8, /*InEpilogue=*/false); 1036*0b57cec5SDimitry Andric } 1037*0b57cec5SDimitry Andric 1038*0b57cec5SDimitry Andric // If this is x86-64 and the Red Zone is not disabled, if we are a leaf 1039*0b57cec5SDimitry Andric // function, and use up to 128 bytes of stack space, don't have a frame 1040*0b57cec5SDimitry Andric // pointer, calls, or dynamic alloca then we do not need to adjust the 1041*0b57cec5SDimitry Andric // stack pointer (we fit in the Red Zone). We also check that we don't 1042*0b57cec5SDimitry Andric // push and pop from the stack. 1043*0b57cec5SDimitry Andric if (has128ByteRedZone(MF) && 1044*0b57cec5SDimitry Andric !TRI->needsStackRealignment(MF) && 1045*0b57cec5SDimitry Andric !MFI.hasVarSizedObjects() && // No dynamic alloca. 1046*0b57cec5SDimitry Andric !MFI.adjustsStack() && // No calls. 1047*0b57cec5SDimitry Andric !UseStackProbe && // No stack probes. 1048*0b57cec5SDimitry Andric !MFI.hasCopyImplyingStackAdjustment() && // Don't push and pop. 1049*0b57cec5SDimitry Andric !MF.shouldSplitStack()) { // Regular stack 1050*0b57cec5SDimitry Andric uint64_t MinSize = X86FI->getCalleeSavedFrameSize(); 1051*0b57cec5SDimitry Andric if (HasFP) MinSize += SlotSize; 1052*0b57cec5SDimitry Andric X86FI->setUsesRedZone(MinSize > 0 || StackSize > 0); 1053*0b57cec5SDimitry Andric StackSize = std::max(MinSize, StackSize > 128 ? StackSize - 128 : 0); 1054*0b57cec5SDimitry Andric MFI.setStackSize(StackSize); 1055*0b57cec5SDimitry Andric } 1056*0b57cec5SDimitry Andric 1057*0b57cec5SDimitry Andric // Insert stack pointer adjustment for later moving of return addr. Only 1058*0b57cec5SDimitry Andric // applies to tail call optimized functions where the callee argument stack 1059*0b57cec5SDimitry Andric // size is bigger than the callers. 1060*0b57cec5SDimitry Andric if (TailCallReturnAddrDelta < 0) { 1061*0b57cec5SDimitry Andric BuildStackAdjustment(MBB, MBBI, DL, TailCallReturnAddrDelta, 1062*0b57cec5SDimitry Andric /*InEpilogue=*/false) 1063*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 1064*0b57cec5SDimitry Andric } 1065*0b57cec5SDimitry Andric 1066*0b57cec5SDimitry Andric // Mapping for machine moves: 1067*0b57cec5SDimitry Andric // 1068*0b57cec5SDimitry Andric // DST: VirtualFP AND 1069*0b57cec5SDimitry Andric // SRC: VirtualFP => DW_CFA_def_cfa_offset 1070*0b57cec5SDimitry Andric // ELSE => DW_CFA_def_cfa 1071*0b57cec5SDimitry Andric // 1072*0b57cec5SDimitry Andric // SRC: VirtualFP AND 1073*0b57cec5SDimitry Andric // DST: Register => DW_CFA_def_cfa_register 1074*0b57cec5SDimitry Andric // 1075*0b57cec5SDimitry Andric // ELSE 1076*0b57cec5SDimitry Andric // OFFSET < 0 => DW_CFA_offset_extended_sf 1077*0b57cec5SDimitry Andric // REG < 64 => DW_CFA_offset + Reg 1078*0b57cec5SDimitry Andric // ELSE => DW_CFA_offset_extended 1079*0b57cec5SDimitry Andric 1080*0b57cec5SDimitry Andric uint64_t NumBytes = 0; 1081*0b57cec5SDimitry Andric int stackGrowth = -SlotSize; 1082*0b57cec5SDimitry Andric 1083*0b57cec5SDimitry Andric // Find the funclet establisher parameter 1084*0b57cec5SDimitry Andric unsigned Establisher = X86::NoRegister; 1085*0b57cec5SDimitry Andric if (IsClrFunclet) 1086*0b57cec5SDimitry Andric Establisher = Uses64BitFramePtr ? X86::RCX : X86::ECX; 1087*0b57cec5SDimitry Andric else if (IsFunclet) 1088*0b57cec5SDimitry Andric Establisher = Uses64BitFramePtr ? X86::RDX : X86::EDX; 1089*0b57cec5SDimitry Andric 1090*0b57cec5SDimitry Andric if (IsWin64Prologue && IsFunclet && !IsClrFunclet) { 1091*0b57cec5SDimitry Andric // Immediately spill establisher into the home slot. 1092*0b57cec5SDimitry Andric // The runtime cares about this. 1093*0b57cec5SDimitry Andric // MOV64mr %rdx, 16(%rsp) 1094*0b57cec5SDimitry Andric unsigned MOVmr = Uses64BitFramePtr ? X86::MOV64mr : X86::MOV32mr; 1095*0b57cec5SDimitry Andric addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(MOVmr)), StackPtr, true, 16) 1096*0b57cec5SDimitry Andric .addReg(Establisher) 1097*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 1098*0b57cec5SDimitry Andric MBB.addLiveIn(Establisher); 1099*0b57cec5SDimitry Andric } 1100*0b57cec5SDimitry Andric 1101*0b57cec5SDimitry Andric if (HasFP) { 1102*0b57cec5SDimitry Andric assert(MF.getRegInfo().isReserved(MachineFramePtr) && "FP reserved"); 1103*0b57cec5SDimitry Andric 1104*0b57cec5SDimitry Andric // Calculate required stack adjustment. 1105*0b57cec5SDimitry Andric uint64_t FrameSize = StackSize - SlotSize; 1106*0b57cec5SDimitry Andric // If required, include space for extra hidden slot for stashing base pointer. 1107*0b57cec5SDimitry Andric if (X86FI->getRestoreBasePointer()) 1108*0b57cec5SDimitry Andric FrameSize += SlotSize; 1109*0b57cec5SDimitry Andric 1110*0b57cec5SDimitry Andric NumBytes = FrameSize - X86FI->getCalleeSavedFrameSize(); 1111*0b57cec5SDimitry Andric 1112*0b57cec5SDimitry Andric // Callee-saved registers are pushed on stack before the stack is realigned. 1113*0b57cec5SDimitry Andric if (TRI->needsStackRealignment(MF) && !IsWin64Prologue) 1114*0b57cec5SDimitry Andric NumBytes = alignTo(NumBytes, MaxAlign); 1115*0b57cec5SDimitry Andric 1116*0b57cec5SDimitry Andric // Save EBP/RBP into the appropriate stack slot. 1117*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(Is64Bit ? X86::PUSH64r : X86::PUSH32r)) 1118*0b57cec5SDimitry Andric .addReg(MachineFramePtr, RegState::Kill) 1119*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 1120*0b57cec5SDimitry Andric 1121*0b57cec5SDimitry Andric if (NeedsDwarfCFI) { 1122*0b57cec5SDimitry Andric // Mark the place where EBP/RBP was saved. 1123*0b57cec5SDimitry Andric // Define the current CFA rule to use the provided offset. 1124*0b57cec5SDimitry Andric assert(StackSize); 1125*0b57cec5SDimitry Andric BuildCFI(MBB, MBBI, DL, 1126*0b57cec5SDimitry Andric MCCFIInstruction::createDefCfaOffset(nullptr, 2 * stackGrowth)); 1127*0b57cec5SDimitry Andric 1128*0b57cec5SDimitry Andric // Change the rule for the FramePtr to be an "offset" rule. 1129*0b57cec5SDimitry Andric unsigned DwarfFramePtr = TRI->getDwarfRegNum(MachineFramePtr, true); 1130*0b57cec5SDimitry Andric BuildCFI(MBB, MBBI, DL, MCCFIInstruction::createOffset( 1131*0b57cec5SDimitry Andric nullptr, DwarfFramePtr, 2 * stackGrowth)); 1132*0b57cec5SDimitry Andric } 1133*0b57cec5SDimitry Andric 1134*0b57cec5SDimitry Andric if (NeedsWinCFI) { 1135*0b57cec5SDimitry Andric HasWinCFI = true; 1136*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_PushReg)) 1137*0b57cec5SDimitry Andric .addImm(FramePtr) 1138*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 1139*0b57cec5SDimitry Andric } 1140*0b57cec5SDimitry Andric 1141*0b57cec5SDimitry Andric if (!IsWin64Prologue && !IsFunclet) { 1142*0b57cec5SDimitry Andric // Update EBP with the new base value. 1143*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, 1144*0b57cec5SDimitry Andric TII.get(Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr), 1145*0b57cec5SDimitry Andric FramePtr) 1146*0b57cec5SDimitry Andric .addReg(StackPtr) 1147*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 1148*0b57cec5SDimitry Andric 1149*0b57cec5SDimitry Andric if (NeedsDwarfCFI) { 1150*0b57cec5SDimitry Andric // Mark effective beginning of when frame pointer becomes valid. 1151*0b57cec5SDimitry Andric // Define the current CFA to use the EBP/RBP register. 1152*0b57cec5SDimitry Andric unsigned DwarfFramePtr = TRI->getDwarfRegNum(MachineFramePtr, true); 1153*0b57cec5SDimitry Andric BuildCFI(MBB, MBBI, DL, MCCFIInstruction::createDefCfaRegister( 1154*0b57cec5SDimitry Andric nullptr, DwarfFramePtr)); 1155*0b57cec5SDimitry Andric } 1156*0b57cec5SDimitry Andric 1157*0b57cec5SDimitry Andric if (NeedsWinFPO) { 1158*0b57cec5SDimitry Andric // .cv_fpo_setframe $FramePtr 1159*0b57cec5SDimitry Andric HasWinCFI = true; 1160*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_SetFrame)) 1161*0b57cec5SDimitry Andric .addImm(FramePtr) 1162*0b57cec5SDimitry Andric .addImm(0) 1163*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 1164*0b57cec5SDimitry Andric } 1165*0b57cec5SDimitry Andric } 1166*0b57cec5SDimitry Andric } else { 1167*0b57cec5SDimitry Andric assert(!IsFunclet && "funclets without FPs not yet implemented"); 1168*0b57cec5SDimitry Andric NumBytes = StackSize - X86FI->getCalleeSavedFrameSize(); 1169*0b57cec5SDimitry Andric } 1170*0b57cec5SDimitry Andric 1171*0b57cec5SDimitry Andric // Update the offset adjustment, which is mainly used by codeview to translate 1172*0b57cec5SDimitry Andric // from ESP to VFRAME relative local variable offsets. 1173*0b57cec5SDimitry Andric if (!IsFunclet) { 1174*0b57cec5SDimitry Andric if (HasFP && TRI->needsStackRealignment(MF)) 1175*0b57cec5SDimitry Andric MFI.setOffsetAdjustment(-NumBytes); 1176*0b57cec5SDimitry Andric else 1177*0b57cec5SDimitry Andric MFI.setOffsetAdjustment(-StackSize); 1178*0b57cec5SDimitry Andric } 1179*0b57cec5SDimitry Andric 1180*0b57cec5SDimitry Andric // For EH funclets, only allocate enough space for outgoing calls. Save the 1181*0b57cec5SDimitry Andric // NumBytes value that we would've used for the parent frame. 1182*0b57cec5SDimitry Andric unsigned ParentFrameNumBytes = NumBytes; 1183*0b57cec5SDimitry Andric if (IsFunclet) 1184*0b57cec5SDimitry Andric NumBytes = getWinEHFuncletFrameSize(MF); 1185*0b57cec5SDimitry Andric 1186*0b57cec5SDimitry Andric // Skip the callee-saved push instructions. 1187*0b57cec5SDimitry Andric bool PushedRegs = false; 1188*0b57cec5SDimitry Andric int StackOffset = 2 * stackGrowth; 1189*0b57cec5SDimitry Andric 1190*0b57cec5SDimitry Andric while (MBBI != MBB.end() && 1191*0b57cec5SDimitry Andric MBBI->getFlag(MachineInstr::FrameSetup) && 1192*0b57cec5SDimitry Andric (MBBI->getOpcode() == X86::PUSH32r || 1193*0b57cec5SDimitry Andric MBBI->getOpcode() == X86::PUSH64r)) { 1194*0b57cec5SDimitry Andric PushedRegs = true; 1195*0b57cec5SDimitry Andric unsigned Reg = MBBI->getOperand(0).getReg(); 1196*0b57cec5SDimitry Andric ++MBBI; 1197*0b57cec5SDimitry Andric 1198*0b57cec5SDimitry Andric if (!HasFP && NeedsDwarfCFI) { 1199*0b57cec5SDimitry Andric // Mark callee-saved push instruction. 1200*0b57cec5SDimitry Andric // Define the current CFA rule to use the provided offset. 1201*0b57cec5SDimitry Andric assert(StackSize); 1202*0b57cec5SDimitry Andric BuildCFI(MBB, MBBI, DL, 1203*0b57cec5SDimitry Andric MCCFIInstruction::createDefCfaOffset(nullptr, StackOffset)); 1204*0b57cec5SDimitry Andric StackOffset += stackGrowth; 1205*0b57cec5SDimitry Andric } 1206*0b57cec5SDimitry Andric 1207*0b57cec5SDimitry Andric if (NeedsWinCFI) { 1208*0b57cec5SDimitry Andric HasWinCFI = true; 1209*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_PushReg)) 1210*0b57cec5SDimitry Andric .addImm(Reg) 1211*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 1212*0b57cec5SDimitry Andric } 1213*0b57cec5SDimitry Andric } 1214*0b57cec5SDimitry Andric 1215*0b57cec5SDimitry Andric // Realign stack after we pushed callee-saved registers (so that we'll be 1216*0b57cec5SDimitry Andric // able to calculate their offsets from the frame pointer). 1217*0b57cec5SDimitry Andric // Don't do this for Win64, it needs to realign the stack after the prologue. 1218*0b57cec5SDimitry Andric if (!IsWin64Prologue && !IsFunclet && TRI->needsStackRealignment(MF)) { 1219*0b57cec5SDimitry Andric assert(HasFP && "There should be a frame pointer if stack is realigned."); 1220*0b57cec5SDimitry Andric BuildStackAlignAND(MBB, MBBI, DL, StackPtr, MaxAlign); 1221*0b57cec5SDimitry Andric 1222*0b57cec5SDimitry Andric if (NeedsWinCFI) { 1223*0b57cec5SDimitry Andric HasWinCFI = true; 1224*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_StackAlign)) 1225*0b57cec5SDimitry Andric .addImm(MaxAlign) 1226*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 1227*0b57cec5SDimitry Andric } 1228*0b57cec5SDimitry Andric } 1229*0b57cec5SDimitry Andric 1230*0b57cec5SDimitry Andric // If there is an SUB32ri of ESP immediately before this instruction, merge 1231*0b57cec5SDimitry Andric // the two. This can be the case when tail call elimination is enabled and 1232*0b57cec5SDimitry Andric // the callee has more arguments then the caller. 1233*0b57cec5SDimitry Andric NumBytes -= mergeSPUpdates(MBB, MBBI, true); 1234*0b57cec5SDimitry Andric 1235*0b57cec5SDimitry Andric // Adjust stack pointer: ESP -= numbytes. 1236*0b57cec5SDimitry Andric 1237*0b57cec5SDimitry Andric // Windows and cygwin/mingw require a prologue helper routine when allocating 1238*0b57cec5SDimitry Andric // more than 4K bytes on the stack. Windows uses __chkstk and cygwin/mingw 1239*0b57cec5SDimitry Andric // uses __alloca. __alloca and the 32-bit version of __chkstk will probe the 1240*0b57cec5SDimitry Andric // stack and adjust the stack pointer in one go. The 64-bit version of 1241*0b57cec5SDimitry Andric // __chkstk is only responsible for probing the stack. The 64-bit prologue is 1242*0b57cec5SDimitry Andric // responsible for adjusting the stack pointer. Touching the stack at 4K 1243*0b57cec5SDimitry Andric // increments is necessary to ensure that the guard pages used by the OS 1244*0b57cec5SDimitry Andric // virtual memory manager are allocated in correct sequence. 1245*0b57cec5SDimitry Andric uint64_t AlignedNumBytes = NumBytes; 1246*0b57cec5SDimitry Andric if (IsWin64Prologue && !IsFunclet && TRI->needsStackRealignment(MF)) 1247*0b57cec5SDimitry Andric AlignedNumBytes = alignTo(AlignedNumBytes, MaxAlign); 1248*0b57cec5SDimitry Andric if (AlignedNumBytes >= StackProbeSize && UseStackProbe) { 1249*0b57cec5SDimitry Andric assert(!X86FI->getUsesRedZone() && 1250*0b57cec5SDimitry Andric "The Red Zone is not accounted for in stack probes"); 1251*0b57cec5SDimitry Andric 1252*0b57cec5SDimitry Andric // Check whether EAX is livein for this block. 1253*0b57cec5SDimitry Andric bool isEAXAlive = isEAXLiveIn(MBB); 1254*0b57cec5SDimitry Andric 1255*0b57cec5SDimitry Andric if (isEAXAlive) { 1256*0b57cec5SDimitry Andric if (Is64Bit) { 1257*0b57cec5SDimitry Andric // Save RAX 1258*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(X86::PUSH64r)) 1259*0b57cec5SDimitry Andric .addReg(X86::RAX, RegState::Kill) 1260*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 1261*0b57cec5SDimitry Andric } else { 1262*0b57cec5SDimitry Andric // Save EAX 1263*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(X86::PUSH32r)) 1264*0b57cec5SDimitry Andric .addReg(X86::EAX, RegState::Kill) 1265*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 1266*0b57cec5SDimitry Andric } 1267*0b57cec5SDimitry Andric } 1268*0b57cec5SDimitry Andric 1269*0b57cec5SDimitry Andric if (Is64Bit) { 1270*0b57cec5SDimitry Andric // Handle the 64-bit Windows ABI case where we need to call __chkstk. 1271*0b57cec5SDimitry Andric // Function prologue is responsible for adjusting the stack pointer. 1272*0b57cec5SDimitry Andric int Alloc = isEAXAlive ? NumBytes - 8 : NumBytes; 1273*0b57cec5SDimitry Andric if (isUInt<32>(Alloc)) { 1274*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32ri), X86::EAX) 1275*0b57cec5SDimitry Andric .addImm(Alloc) 1276*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 1277*0b57cec5SDimitry Andric } else if (isInt<32>(Alloc)) { 1278*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64ri32), X86::RAX) 1279*0b57cec5SDimitry Andric .addImm(Alloc) 1280*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 1281*0b57cec5SDimitry Andric } else { 1282*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64ri), X86::RAX) 1283*0b57cec5SDimitry Andric .addImm(Alloc) 1284*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 1285*0b57cec5SDimitry Andric } 1286*0b57cec5SDimitry Andric } else { 1287*0b57cec5SDimitry Andric // Allocate NumBytes-4 bytes on stack in case of isEAXAlive. 1288*0b57cec5SDimitry Andric // We'll also use 4 already allocated bytes for EAX. 1289*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32ri), X86::EAX) 1290*0b57cec5SDimitry Andric .addImm(isEAXAlive ? NumBytes - 4 : NumBytes) 1291*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 1292*0b57cec5SDimitry Andric } 1293*0b57cec5SDimitry Andric 1294*0b57cec5SDimitry Andric // Call __chkstk, __chkstk_ms, or __alloca. 1295*0b57cec5SDimitry Andric emitStackProbe(MF, MBB, MBBI, DL, true); 1296*0b57cec5SDimitry Andric 1297*0b57cec5SDimitry Andric if (isEAXAlive) { 1298*0b57cec5SDimitry Andric // Restore RAX/EAX 1299*0b57cec5SDimitry Andric MachineInstr *MI; 1300*0b57cec5SDimitry Andric if (Is64Bit) 1301*0b57cec5SDimitry Andric MI = addRegOffset(BuildMI(MF, DL, TII.get(X86::MOV64rm), X86::RAX), 1302*0b57cec5SDimitry Andric StackPtr, false, NumBytes - 8); 1303*0b57cec5SDimitry Andric else 1304*0b57cec5SDimitry Andric MI = addRegOffset(BuildMI(MF, DL, TII.get(X86::MOV32rm), X86::EAX), 1305*0b57cec5SDimitry Andric StackPtr, false, NumBytes - 4); 1306*0b57cec5SDimitry Andric MI->setFlag(MachineInstr::FrameSetup); 1307*0b57cec5SDimitry Andric MBB.insert(MBBI, MI); 1308*0b57cec5SDimitry Andric } 1309*0b57cec5SDimitry Andric } else if (NumBytes) { 1310*0b57cec5SDimitry Andric emitSPUpdate(MBB, MBBI, DL, -(int64_t)NumBytes, /*InEpilogue=*/false); 1311*0b57cec5SDimitry Andric } 1312*0b57cec5SDimitry Andric 1313*0b57cec5SDimitry Andric if (NeedsWinCFI && NumBytes) { 1314*0b57cec5SDimitry Andric HasWinCFI = true; 1315*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_StackAlloc)) 1316*0b57cec5SDimitry Andric .addImm(NumBytes) 1317*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 1318*0b57cec5SDimitry Andric } 1319*0b57cec5SDimitry Andric 1320*0b57cec5SDimitry Andric int SEHFrameOffset = 0; 1321*0b57cec5SDimitry Andric unsigned SPOrEstablisher; 1322*0b57cec5SDimitry Andric if (IsFunclet) { 1323*0b57cec5SDimitry Andric if (IsClrFunclet) { 1324*0b57cec5SDimitry Andric // The establisher parameter passed to a CLR funclet is actually a pointer 1325*0b57cec5SDimitry Andric // to the (mostly empty) frame of its nearest enclosing funclet; we have 1326*0b57cec5SDimitry Andric // to find the root function establisher frame by loading the PSPSym from 1327*0b57cec5SDimitry Andric // the intermediate frame. 1328*0b57cec5SDimitry Andric unsigned PSPSlotOffset = getPSPSlotOffsetFromSP(MF); 1329*0b57cec5SDimitry Andric MachinePointerInfo NoInfo; 1330*0b57cec5SDimitry Andric MBB.addLiveIn(Establisher); 1331*0b57cec5SDimitry Andric addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64rm), Establisher), 1332*0b57cec5SDimitry Andric Establisher, false, PSPSlotOffset) 1333*0b57cec5SDimitry Andric .addMemOperand(MF.getMachineMemOperand( 1334*0b57cec5SDimitry Andric NoInfo, MachineMemOperand::MOLoad, SlotSize, SlotSize)); 1335*0b57cec5SDimitry Andric ; 1336*0b57cec5SDimitry Andric // Save the root establisher back into the current funclet's (mostly 1337*0b57cec5SDimitry Andric // empty) frame, in case a sub-funclet or the GC needs it. 1338*0b57cec5SDimitry Andric addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64mr)), StackPtr, 1339*0b57cec5SDimitry Andric false, PSPSlotOffset) 1340*0b57cec5SDimitry Andric .addReg(Establisher) 1341*0b57cec5SDimitry Andric .addMemOperand( 1342*0b57cec5SDimitry Andric MF.getMachineMemOperand(NoInfo, MachineMemOperand::MOStore | 1343*0b57cec5SDimitry Andric MachineMemOperand::MOVolatile, 1344*0b57cec5SDimitry Andric SlotSize, SlotSize)); 1345*0b57cec5SDimitry Andric } 1346*0b57cec5SDimitry Andric SPOrEstablisher = Establisher; 1347*0b57cec5SDimitry Andric } else { 1348*0b57cec5SDimitry Andric SPOrEstablisher = StackPtr; 1349*0b57cec5SDimitry Andric } 1350*0b57cec5SDimitry Andric 1351*0b57cec5SDimitry Andric if (IsWin64Prologue && HasFP) { 1352*0b57cec5SDimitry Andric // Set RBP to a small fixed offset from RSP. In the funclet case, we base 1353*0b57cec5SDimitry Andric // this calculation on the incoming establisher, which holds the value of 1354*0b57cec5SDimitry Andric // RSP from the parent frame at the end of the prologue. 1355*0b57cec5SDimitry Andric SEHFrameOffset = calculateSetFPREG(ParentFrameNumBytes); 1356*0b57cec5SDimitry Andric if (SEHFrameOffset) 1357*0b57cec5SDimitry Andric addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::LEA64r), FramePtr), 1358*0b57cec5SDimitry Andric SPOrEstablisher, false, SEHFrameOffset); 1359*0b57cec5SDimitry Andric else 1360*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64rr), FramePtr) 1361*0b57cec5SDimitry Andric .addReg(SPOrEstablisher); 1362*0b57cec5SDimitry Andric 1363*0b57cec5SDimitry Andric // If this is not a funclet, emit the CFI describing our frame pointer. 1364*0b57cec5SDimitry Andric if (NeedsWinCFI && !IsFunclet) { 1365*0b57cec5SDimitry Andric assert(!NeedsWinFPO && "this setframe incompatible with FPO data"); 1366*0b57cec5SDimitry Andric HasWinCFI = true; 1367*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_SetFrame)) 1368*0b57cec5SDimitry Andric .addImm(FramePtr) 1369*0b57cec5SDimitry Andric .addImm(SEHFrameOffset) 1370*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 1371*0b57cec5SDimitry Andric if (isAsynchronousEHPersonality(Personality)) 1372*0b57cec5SDimitry Andric MF.getWinEHFuncInfo()->SEHSetFrameOffset = SEHFrameOffset; 1373*0b57cec5SDimitry Andric } 1374*0b57cec5SDimitry Andric } else if (IsFunclet && STI.is32Bit()) { 1375*0b57cec5SDimitry Andric // Reset EBP / ESI to something good for funclets. 1376*0b57cec5SDimitry Andric MBBI = restoreWin32EHStackPointers(MBB, MBBI, DL); 1377*0b57cec5SDimitry Andric // If we're a catch funclet, we can be returned to via catchret. Save ESP 1378*0b57cec5SDimitry Andric // into the registration node so that the runtime will restore it for us. 1379*0b57cec5SDimitry Andric if (!MBB.isCleanupFuncletEntry()) { 1380*0b57cec5SDimitry Andric assert(Personality == EHPersonality::MSVC_CXX); 1381*0b57cec5SDimitry Andric unsigned FrameReg; 1382*0b57cec5SDimitry Andric int FI = MF.getWinEHFuncInfo()->EHRegNodeFrameIndex; 1383*0b57cec5SDimitry Andric int64_t EHRegOffset = getFrameIndexReference(MF, FI, FrameReg); 1384*0b57cec5SDimitry Andric // ESP is the first field, so no extra displacement is needed. 1385*0b57cec5SDimitry Andric addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32mr)), FrameReg, 1386*0b57cec5SDimitry Andric false, EHRegOffset) 1387*0b57cec5SDimitry Andric .addReg(X86::ESP); 1388*0b57cec5SDimitry Andric } 1389*0b57cec5SDimitry Andric } 1390*0b57cec5SDimitry Andric 1391*0b57cec5SDimitry Andric while (MBBI != MBB.end() && MBBI->getFlag(MachineInstr::FrameSetup)) { 1392*0b57cec5SDimitry Andric const MachineInstr &FrameInstr = *MBBI; 1393*0b57cec5SDimitry Andric ++MBBI; 1394*0b57cec5SDimitry Andric 1395*0b57cec5SDimitry Andric if (NeedsWinCFI) { 1396*0b57cec5SDimitry Andric int FI; 1397*0b57cec5SDimitry Andric if (unsigned Reg = TII.isStoreToStackSlot(FrameInstr, FI)) { 1398*0b57cec5SDimitry Andric if (X86::FR64RegClass.contains(Reg)) { 1399*0b57cec5SDimitry Andric unsigned IgnoredFrameReg; 1400*0b57cec5SDimitry Andric int Offset = getFrameIndexReference(MF, FI, IgnoredFrameReg); 1401*0b57cec5SDimitry Andric Offset += SEHFrameOffset; 1402*0b57cec5SDimitry Andric 1403*0b57cec5SDimitry Andric HasWinCFI = true; 1404*0b57cec5SDimitry Andric assert(!NeedsWinFPO && "SEH_SaveXMM incompatible with FPO data"); 1405*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_SaveXMM)) 1406*0b57cec5SDimitry Andric .addImm(Reg) 1407*0b57cec5SDimitry Andric .addImm(Offset) 1408*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 1409*0b57cec5SDimitry Andric } 1410*0b57cec5SDimitry Andric } 1411*0b57cec5SDimitry Andric } 1412*0b57cec5SDimitry Andric } 1413*0b57cec5SDimitry Andric 1414*0b57cec5SDimitry Andric if (NeedsWinCFI && HasWinCFI) 1415*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_EndPrologue)) 1416*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 1417*0b57cec5SDimitry Andric 1418*0b57cec5SDimitry Andric if (FnHasClrFunclet && !IsFunclet) { 1419*0b57cec5SDimitry Andric // Save the so-called Initial-SP (i.e. the value of the stack pointer 1420*0b57cec5SDimitry Andric // immediately after the prolog) into the PSPSlot so that funclets 1421*0b57cec5SDimitry Andric // and the GC can recover it. 1422*0b57cec5SDimitry Andric unsigned PSPSlotOffset = getPSPSlotOffsetFromSP(MF); 1423*0b57cec5SDimitry Andric auto PSPInfo = MachinePointerInfo::getFixedStack( 1424*0b57cec5SDimitry Andric MF, MF.getWinEHFuncInfo()->PSPSymFrameIdx); 1425*0b57cec5SDimitry Andric addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64mr)), StackPtr, false, 1426*0b57cec5SDimitry Andric PSPSlotOffset) 1427*0b57cec5SDimitry Andric .addReg(StackPtr) 1428*0b57cec5SDimitry Andric .addMemOperand(MF.getMachineMemOperand( 1429*0b57cec5SDimitry Andric PSPInfo, MachineMemOperand::MOStore | MachineMemOperand::MOVolatile, 1430*0b57cec5SDimitry Andric SlotSize, SlotSize)); 1431*0b57cec5SDimitry Andric } 1432*0b57cec5SDimitry Andric 1433*0b57cec5SDimitry Andric // Realign stack after we spilled callee-saved registers (so that we'll be 1434*0b57cec5SDimitry Andric // able to calculate their offsets from the frame pointer). 1435*0b57cec5SDimitry Andric // Win64 requires aligning the stack after the prologue. 1436*0b57cec5SDimitry Andric if (IsWin64Prologue && TRI->needsStackRealignment(MF)) { 1437*0b57cec5SDimitry Andric assert(HasFP && "There should be a frame pointer if stack is realigned."); 1438*0b57cec5SDimitry Andric BuildStackAlignAND(MBB, MBBI, DL, SPOrEstablisher, MaxAlign); 1439*0b57cec5SDimitry Andric } 1440*0b57cec5SDimitry Andric 1441*0b57cec5SDimitry Andric // We already dealt with stack realignment and funclets above. 1442*0b57cec5SDimitry Andric if (IsFunclet && STI.is32Bit()) 1443*0b57cec5SDimitry Andric return; 1444*0b57cec5SDimitry Andric 1445*0b57cec5SDimitry Andric // If we need a base pointer, set it up here. It's whatever the value 1446*0b57cec5SDimitry Andric // of the stack pointer is at this point. Any variable size objects 1447*0b57cec5SDimitry Andric // will be allocated after this, so we can still use the base pointer 1448*0b57cec5SDimitry Andric // to reference locals. 1449*0b57cec5SDimitry Andric if (TRI->hasBasePointer(MF)) { 1450*0b57cec5SDimitry Andric // Update the base pointer with the current stack pointer. 1451*0b57cec5SDimitry Andric unsigned Opc = Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr; 1452*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(Opc), BasePtr) 1453*0b57cec5SDimitry Andric .addReg(SPOrEstablisher) 1454*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 1455*0b57cec5SDimitry Andric if (X86FI->getRestoreBasePointer()) { 1456*0b57cec5SDimitry Andric // Stash value of base pointer. Saving RSP instead of EBP shortens 1457*0b57cec5SDimitry Andric // dependence chain. Used by SjLj EH. 1458*0b57cec5SDimitry Andric unsigned Opm = Uses64BitFramePtr ? X86::MOV64mr : X86::MOV32mr; 1459*0b57cec5SDimitry Andric addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(Opm)), 1460*0b57cec5SDimitry Andric FramePtr, true, X86FI->getRestoreBasePointerOffset()) 1461*0b57cec5SDimitry Andric .addReg(SPOrEstablisher) 1462*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 1463*0b57cec5SDimitry Andric } 1464*0b57cec5SDimitry Andric 1465*0b57cec5SDimitry Andric if (X86FI->getHasSEHFramePtrSave() && !IsFunclet) { 1466*0b57cec5SDimitry Andric // Stash the value of the frame pointer relative to the base pointer for 1467*0b57cec5SDimitry Andric // Win32 EH. This supports Win32 EH, which does the inverse of the above: 1468*0b57cec5SDimitry Andric // it recovers the frame pointer from the base pointer rather than the 1469*0b57cec5SDimitry Andric // other way around. 1470*0b57cec5SDimitry Andric unsigned Opm = Uses64BitFramePtr ? X86::MOV64mr : X86::MOV32mr; 1471*0b57cec5SDimitry Andric unsigned UsedReg; 1472*0b57cec5SDimitry Andric int Offset = 1473*0b57cec5SDimitry Andric getFrameIndexReference(MF, X86FI->getSEHFramePtrSaveIndex(), UsedReg); 1474*0b57cec5SDimitry Andric assert(UsedReg == BasePtr); 1475*0b57cec5SDimitry Andric addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(Opm)), UsedReg, true, Offset) 1476*0b57cec5SDimitry Andric .addReg(FramePtr) 1477*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 1478*0b57cec5SDimitry Andric } 1479*0b57cec5SDimitry Andric } 1480*0b57cec5SDimitry Andric 1481*0b57cec5SDimitry Andric if (((!HasFP && NumBytes) || PushedRegs) && NeedsDwarfCFI) { 1482*0b57cec5SDimitry Andric // Mark end of stack pointer adjustment. 1483*0b57cec5SDimitry Andric if (!HasFP && NumBytes) { 1484*0b57cec5SDimitry Andric // Define the current CFA rule to use the provided offset. 1485*0b57cec5SDimitry Andric assert(StackSize); 1486*0b57cec5SDimitry Andric BuildCFI(MBB, MBBI, DL, MCCFIInstruction::createDefCfaOffset( 1487*0b57cec5SDimitry Andric nullptr, -StackSize + stackGrowth)); 1488*0b57cec5SDimitry Andric } 1489*0b57cec5SDimitry Andric 1490*0b57cec5SDimitry Andric // Emit DWARF info specifying the offsets of the callee-saved registers. 1491*0b57cec5SDimitry Andric emitCalleeSavedFrameMoves(MBB, MBBI, DL); 1492*0b57cec5SDimitry Andric } 1493*0b57cec5SDimitry Andric 1494*0b57cec5SDimitry Andric // X86 Interrupt handling function cannot assume anything about the direction 1495*0b57cec5SDimitry Andric // flag (DF in EFLAGS register). Clear this flag by creating "cld" instruction 1496*0b57cec5SDimitry Andric // in each prologue of interrupt handler function. 1497*0b57cec5SDimitry Andric // 1498*0b57cec5SDimitry Andric // FIXME: Create "cld" instruction only in these cases: 1499*0b57cec5SDimitry Andric // 1. The interrupt handling function uses any of the "rep" instructions. 1500*0b57cec5SDimitry Andric // 2. Interrupt handling function calls another function. 1501*0b57cec5SDimitry Andric // 1502*0b57cec5SDimitry Andric if (Fn.getCallingConv() == CallingConv::X86_INTR) 1503*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(X86::CLD)) 1504*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 1505*0b57cec5SDimitry Andric 1506*0b57cec5SDimitry Andric // At this point we know if the function has WinCFI or not. 1507*0b57cec5SDimitry Andric MF.setHasWinCFI(HasWinCFI); 1508*0b57cec5SDimitry Andric } 1509*0b57cec5SDimitry Andric 1510*0b57cec5SDimitry Andric bool X86FrameLowering::canUseLEAForSPInEpilogue( 1511*0b57cec5SDimitry Andric const MachineFunction &MF) const { 1512*0b57cec5SDimitry Andric // We can't use LEA instructions for adjusting the stack pointer if we don't 1513*0b57cec5SDimitry Andric // have a frame pointer in the Win64 ABI. Only ADD instructions may be used 1514*0b57cec5SDimitry Andric // to deallocate the stack. 1515*0b57cec5SDimitry Andric // This means that we can use LEA for SP in two situations: 1516*0b57cec5SDimitry Andric // 1. We *aren't* using the Win64 ABI which means we are free to use LEA. 1517*0b57cec5SDimitry Andric // 2. We *have* a frame pointer which means we are permitted to use LEA. 1518*0b57cec5SDimitry Andric return !MF.getTarget().getMCAsmInfo()->usesWindowsCFI() || hasFP(MF); 1519*0b57cec5SDimitry Andric } 1520*0b57cec5SDimitry Andric 1521*0b57cec5SDimitry Andric static bool isFuncletReturnInstr(MachineInstr &MI) { 1522*0b57cec5SDimitry Andric switch (MI.getOpcode()) { 1523*0b57cec5SDimitry Andric case X86::CATCHRET: 1524*0b57cec5SDimitry Andric case X86::CLEANUPRET: 1525*0b57cec5SDimitry Andric return true; 1526*0b57cec5SDimitry Andric default: 1527*0b57cec5SDimitry Andric return false; 1528*0b57cec5SDimitry Andric } 1529*0b57cec5SDimitry Andric llvm_unreachable("impossible"); 1530*0b57cec5SDimitry Andric } 1531*0b57cec5SDimitry Andric 1532*0b57cec5SDimitry Andric // CLR funclets use a special "Previous Stack Pointer Symbol" slot on the 1533*0b57cec5SDimitry Andric // stack. It holds a pointer to the bottom of the root function frame. The 1534*0b57cec5SDimitry Andric // establisher frame pointer passed to a nested funclet may point to the 1535*0b57cec5SDimitry Andric // (mostly empty) frame of its parent funclet, but it will need to find 1536*0b57cec5SDimitry Andric // the frame of the root function to access locals. To facilitate this, 1537*0b57cec5SDimitry Andric // every funclet copies the pointer to the bottom of the root function 1538*0b57cec5SDimitry Andric // frame into a PSPSym slot in its own (mostly empty) stack frame. Using the 1539*0b57cec5SDimitry Andric // same offset for the PSPSym in the root function frame that's used in the 1540*0b57cec5SDimitry Andric // funclets' frames allows each funclet to dynamically accept any ancestor 1541*0b57cec5SDimitry Andric // frame as its establisher argument (the runtime doesn't guarantee the 1542*0b57cec5SDimitry Andric // immediate parent for some reason lost to history), and also allows the GC, 1543*0b57cec5SDimitry Andric // which uses the PSPSym for some bookkeeping, to find it in any funclet's 1544*0b57cec5SDimitry Andric // frame with only a single offset reported for the entire method. 1545*0b57cec5SDimitry Andric unsigned 1546*0b57cec5SDimitry Andric X86FrameLowering::getPSPSlotOffsetFromSP(const MachineFunction &MF) const { 1547*0b57cec5SDimitry Andric const WinEHFuncInfo &Info = *MF.getWinEHFuncInfo(); 1548*0b57cec5SDimitry Andric unsigned SPReg; 1549*0b57cec5SDimitry Andric int Offset = getFrameIndexReferencePreferSP(MF, Info.PSPSymFrameIdx, SPReg, 1550*0b57cec5SDimitry Andric /*IgnoreSPUpdates*/ true); 1551*0b57cec5SDimitry Andric assert(Offset >= 0 && SPReg == TRI->getStackRegister()); 1552*0b57cec5SDimitry Andric return static_cast<unsigned>(Offset); 1553*0b57cec5SDimitry Andric } 1554*0b57cec5SDimitry Andric 1555*0b57cec5SDimitry Andric unsigned 1556*0b57cec5SDimitry Andric X86FrameLowering::getWinEHFuncletFrameSize(const MachineFunction &MF) const { 1557*0b57cec5SDimitry Andric // This is the size of the pushed CSRs. 1558*0b57cec5SDimitry Andric unsigned CSSize = 1559*0b57cec5SDimitry Andric MF.getInfo<X86MachineFunctionInfo>()->getCalleeSavedFrameSize(); 1560*0b57cec5SDimitry Andric // This is the amount of stack a funclet needs to allocate. 1561*0b57cec5SDimitry Andric unsigned UsedSize; 1562*0b57cec5SDimitry Andric EHPersonality Personality = 1563*0b57cec5SDimitry Andric classifyEHPersonality(MF.getFunction().getPersonalityFn()); 1564*0b57cec5SDimitry Andric if (Personality == EHPersonality::CoreCLR) { 1565*0b57cec5SDimitry Andric // CLR funclets need to hold enough space to include the PSPSym, at the 1566*0b57cec5SDimitry Andric // same offset from the stack pointer (immediately after the prolog) as it 1567*0b57cec5SDimitry Andric // resides at in the main function. 1568*0b57cec5SDimitry Andric UsedSize = getPSPSlotOffsetFromSP(MF) + SlotSize; 1569*0b57cec5SDimitry Andric } else { 1570*0b57cec5SDimitry Andric // Other funclets just need enough stack for outgoing call arguments. 1571*0b57cec5SDimitry Andric UsedSize = MF.getFrameInfo().getMaxCallFrameSize(); 1572*0b57cec5SDimitry Andric } 1573*0b57cec5SDimitry Andric // RBP is not included in the callee saved register block. After pushing RBP, 1574*0b57cec5SDimitry Andric // everything is 16 byte aligned. Everything we allocate before an outgoing 1575*0b57cec5SDimitry Andric // call must also be 16 byte aligned. 1576*0b57cec5SDimitry Andric unsigned FrameSizeMinusRBP = alignTo(CSSize + UsedSize, getStackAlignment()); 1577*0b57cec5SDimitry Andric // Subtract out the size of the callee saved registers. This is how much stack 1578*0b57cec5SDimitry Andric // each funclet will allocate. 1579*0b57cec5SDimitry Andric return FrameSizeMinusRBP - CSSize; 1580*0b57cec5SDimitry Andric } 1581*0b57cec5SDimitry Andric 1582*0b57cec5SDimitry Andric static bool isTailCallOpcode(unsigned Opc) { 1583*0b57cec5SDimitry Andric return Opc == X86::TCRETURNri || Opc == X86::TCRETURNdi || 1584*0b57cec5SDimitry Andric Opc == X86::TCRETURNmi || 1585*0b57cec5SDimitry Andric Opc == X86::TCRETURNri64 || Opc == X86::TCRETURNdi64 || 1586*0b57cec5SDimitry Andric Opc == X86::TCRETURNmi64; 1587*0b57cec5SDimitry Andric } 1588*0b57cec5SDimitry Andric 1589*0b57cec5SDimitry Andric void X86FrameLowering::emitEpilogue(MachineFunction &MF, 1590*0b57cec5SDimitry Andric MachineBasicBlock &MBB) const { 1591*0b57cec5SDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo(); 1592*0b57cec5SDimitry Andric X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); 1593*0b57cec5SDimitry Andric MachineBasicBlock::iterator Terminator = MBB.getFirstTerminator(); 1594*0b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = Terminator; 1595*0b57cec5SDimitry Andric DebugLoc DL; 1596*0b57cec5SDimitry Andric if (MBBI != MBB.end()) 1597*0b57cec5SDimitry Andric DL = MBBI->getDebugLoc(); 1598*0b57cec5SDimitry Andric // standard x86_64 and NaCl use 64-bit frame/stack pointers, x32 - 32-bit. 1599*0b57cec5SDimitry Andric const bool Is64BitILP32 = STI.isTarget64BitILP32(); 1600*0b57cec5SDimitry Andric unsigned FramePtr = TRI->getFrameRegister(MF); 1601*0b57cec5SDimitry Andric unsigned MachineFramePtr = 1602*0b57cec5SDimitry Andric Is64BitILP32 ? getX86SubSuperRegister(FramePtr, 64) : FramePtr; 1603*0b57cec5SDimitry Andric 1604*0b57cec5SDimitry Andric bool IsWin64Prologue = MF.getTarget().getMCAsmInfo()->usesWindowsCFI(); 1605*0b57cec5SDimitry Andric bool NeedsWin64CFI = 1606*0b57cec5SDimitry Andric IsWin64Prologue && MF.getFunction().needsUnwindTableEntry(); 1607*0b57cec5SDimitry Andric bool IsFunclet = MBBI == MBB.end() ? false : isFuncletReturnInstr(*MBBI); 1608*0b57cec5SDimitry Andric 1609*0b57cec5SDimitry Andric // Get the number of bytes to allocate from the FrameInfo. 1610*0b57cec5SDimitry Andric uint64_t StackSize = MFI.getStackSize(); 1611*0b57cec5SDimitry Andric uint64_t MaxAlign = calculateMaxStackAlign(MF); 1612*0b57cec5SDimitry Andric unsigned CSSize = X86FI->getCalleeSavedFrameSize(); 1613*0b57cec5SDimitry Andric bool HasFP = hasFP(MF); 1614*0b57cec5SDimitry Andric uint64_t NumBytes = 0; 1615*0b57cec5SDimitry Andric 1616*0b57cec5SDimitry Andric bool NeedsDwarfCFI = 1617*0b57cec5SDimitry Andric (!MF.getTarget().getTargetTriple().isOSDarwin() && 1618*0b57cec5SDimitry Andric !MF.getTarget().getTargetTriple().isOSWindows()) && 1619*0b57cec5SDimitry Andric (MF.getMMI().hasDebugInfo() || MF.getFunction().needsUnwindTableEntry()); 1620*0b57cec5SDimitry Andric 1621*0b57cec5SDimitry Andric if (IsFunclet) { 1622*0b57cec5SDimitry Andric assert(HasFP && "EH funclets without FP not yet implemented"); 1623*0b57cec5SDimitry Andric NumBytes = getWinEHFuncletFrameSize(MF); 1624*0b57cec5SDimitry Andric } else if (HasFP) { 1625*0b57cec5SDimitry Andric // Calculate required stack adjustment. 1626*0b57cec5SDimitry Andric uint64_t FrameSize = StackSize - SlotSize; 1627*0b57cec5SDimitry Andric NumBytes = FrameSize - CSSize; 1628*0b57cec5SDimitry Andric 1629*0b57cec5SDimitry Andric // Callee-saved registers were pushed on stack before the stack was 1630*0b57cec5SDimitry Andric // realigned. 1631*0b57cec5SDimitry Andric if (TRI->needsStackRealignment(MF) && !IsWin64Prologue) 1632*0b57cec5SDimitry Andric NumBytes = alignTo(FrameSize, MaxAlign); 1633*0b57cec5SDimitry Andric } else { 1634*0b57cec5SDimitry Andric NumBytes = StackSize - CSSize; 1635*0b57cec5SDimitry Andric } 1636*0b57cec5SDimitry Andric uint64_t SEHStackAllocAmt = NumBytes; 1637*0b57cec5SDimitry Andric 1638*0b57cec5SDimitry Andric if (HasFP) { 1639*0b57cec5SDimitry Andric // Pop EBP. 1640*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(Is64Bit ? X86::POP64r : X86::POP32r), 1641*0b57cec5SDimitry Andric MachineFramePtr) 1642*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameDestroy); 1643*0b57cec5SDimitry Andric if (NeedsDwarfCFI) { 1644*0b57cec5SDimitry Andric unsigned DwarfStackPtr = 1645*0b57cec5SDimitry Andric TRI->getDwarfRegNum(Is64Bit ? X86::RSP : X86::ESP, true); 1646*0b57cec5SDimitry Andric BuildCFI(MBB, MBBI, DL, MCCFIInstruction::createDefCfa( 1647*0b57cec5SDimitry Andric nullptr, DwarfStackPtr, -SlotSize)); 1648*0b57cec5SDimitry Andric --MBBI; 1649*0b57cec5SDimitry Andric } 1650*0b57cec5SDimitry Andric } 1651*0b57cec5SDimitry Andric 1652*0b57cec5SDimitry Andric MachineBasicBlock::iterator FirstCSPop = MBBI; 1653*0b57cec5SDimitry Andric // Skip the callee-saved pop instructions. 1654*0b57cec5SDimitry Andric while (MBBI != MBB.begin()) { 1655*0b57cec5SDimitry Andric MachineBasicBlock::iterator PI = std::prev(MBBI); 1656*0b57cec5SDimitry Andric unsigned Opc = PI->getOpcode(); 1657*0b57cec5SDimitry Andric 1658*0b57cec5SDimitry Andric if (Opc != X86::DBG_VALUE && !PI->isTerminator()) { 1659*0b57cec5SDimitry Andric if ((Opc != X86::POP32r || !PI->getFlag(MachineInstr::FrameDestroy)) && 1660*0b57cec5SDimitry Andric (Opc != X86::POP64r || !PI->getFlag(MachineInstr::FrameDestroy))) 1661*0b57cec5SDimitry Andric break; 1662*0b57cec5SDimitry Andric FirstCSPop = PI; 1663*0b57cec5SDimitry Andric } 1664*0b57cec5SDimitry Andric 1665*0b57cec5SDimitry Andric --MBBI; 1666*0b57cec5SDimitry Andric } 1667*0b57cec5SDimitry Andric MBBI = FirstCSPop; 1668*0b57cec5SDimitry Andric 1669*0b57cec5SDimitry Andric if (IsFunclet && Terminator->getOpcode() == X86::CATCHRET) 1670*0b57cec5SDimitry Andric emitCatchRetReturnValue(MBB, FirstCSPop, &*Terminator); 1671*0b57cec5SDimitry Andric 1672*0b57cec5SDimitry Andric if (MBBI != MBB.end()) 1673*0b57cec5SDimitry Andric DL = MBBI->getDebugLoc(); 1674*0b57cec5SDimitry Andric 1675*0b57cec5SDimitry Andric // If there is an ADD32ri or SUB32ri of ESP immediately before this 1676*0b57cec5SDimitry Andric // instruction, merge the two instructions. 1677*0b57cec5SDimitry Andric if (NumBytes || MFI.hasVarSizedObjects()) 1678*0b57cec5SDimitry Andric NumBytes += mergeSPUpdates(MBB, MBBI, true); 1679*0b57cec5SDimitry Andric 1680*0b57cec5SDimitry Andric // If dynamic alloca is used, then reset esp to point to the last callee-saved 1681*0b57cec5SDimitry Andric // slot before popping them off! Same applies for the case, when stack was 1682*0b57cec5SDimitry Andric // realigned. Don't do this if this was a funclet epilogue, since the funclets 1683*0b57cec5SDimitry Andric // will not do realignment or dynamic stack allocation. 1684*0b57cec5SDimitry Andric if ((TRI->needsStackRealignment(MF) || MFI.hasVarSizedObjects()) && 1685*0b57cec5SDimitry Andric !IsFunclet) { 1686*0b57cec5SDimitry Andric if (TRI->needsStackRealignment(MF)) 1687*0b57cec5SDimitry Andric MBBI = FirstCSPop; 1688*0b57cec5SDimitry Andric unsigned SEHFrameOffset = calculateSetFPREG(SEHStackAllocAmt); 1689*0b57cec5SDimitry Andric uint64_t LEAAmount = 1690*0b57cec5SDimitry Andric IsWin64Prologue ? SEHStackAllocAmt - SEHFrameOffset : -CSSize; 1691*0b57cec5SDimitry Andric 1692*0b57cec5SDimitry Andric // There are only two legal forms of epilogue: 1693*0b57cec5SDimitry Andric // - add SEHAllocationSize, %rsp 1694*0b57cec5SDimitry Andric // - lea SEHAllocationSize(%FramePtr), %rsp 1695*0b57cec5SDimitry Andric // 1696*0b57cec5SDimitry Andric // 'mov %FramePtr, %rsp' will not be recognized as an epilogue sequence. 1697*0b57cec5SDimitry Andric // However, we may use this sequence if we have a frame pointer because the 1698*0b57cec5SDimitry Andric // effects of the prologue can safely be undone. 1699*0b57cec5SDimitry Andric if (LEAAmount != 0) { 1700*0b57cec5SDimitry Andric unsigned Opc = getLEArOpcode(Uses64BitFramePtr); 1701*0b57cec5SDimitry Andric addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr), 1702*0b57cec5SDimitry Andric FramePtr, false, LEAAmount); 1703*0b57cec5SDimitry Andric --MBBI; 1704*0b57cec5SDimitry Andric } else { 1705*0b57cec5SDimitry Andric unsigned Opc = (Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr); 1706*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(Opc), StackPtr) 1707*0b57cec5SDimitry Andric .addReg(FramePtr); 1708*0b57cec5SDimitry Andric --MBBI; 1709*0b57cec5SDimitry Andric } 1710*0b57cec5SDimitry Andric } else if (NumBytes) { 1711*0b57cec5SDimitry Andric // Adjust stack pointer back: ESP += numbytes. 1712*0b57cec5SDimitry Andric emitSPUpdate(MBB, MBBI, DL, NumBytes, /*InEpilogue=*/true); 1713*0b57cec5SDimitry Andric if (!hasFP(MF) && NeedsDwarfCFI) { 1714*0b57cec5SDimitry Andric // Define the current CFA rule to use the provided offset. 1715*0b57cec5SDimitry Andric BuildCFI(MBB, MBBI, DL, MCCFIInstruction::createDefCfaOffset( 1716*0b57cec5SDimitry Andric nullptr, -CSSize - SlotSize)); 1717*0b57cec5SDimitry Andric } 1718*0b57cec5SDimitry Andric --MBBI; 1719*0b57cec5SDimitry Andric } 1720*0b57cec5SDimitry Andric 1721*0b57cec5SDimitry Andric // Windows unwinder will not invoke function's exception handler if IP is 1722*0b57cec5SDimitry Andric // either in prologue or in epilogue. This behavior causes a problem when a 1723*0b57cec5SDimitry Andric // call immediately precedes an epilogue, because the return address points 1724*0b57cec5SDimitry Andric // into the epilogue. To cope with that, we insert an epilogue marker here, 1725*0b57cec5SDimitry Andric // then replace it with a 'nop' if it ends up immediately after a CALL in the 1726*0b57cec5SDimitry Andric // final emitted code. 1727*0b57cec5SDimitry Andric if (NeedsWin64CFI && MF.hasWinCFI()) 1728*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_Epilogue)); 1729*0b57cec5SDimitry Andric 1730*0b57cec5SDimitry Andric if (!hasFP(MF) && NeedsDwarfCFI) { 1731*0b57cec5SDimitry Andric MBBI = FirstCSPop; 1732*0b57cec5SDimitry Andric int64_t Offset = -CSSize - SlotSize; 1733*0b57cec5SDimitry Andric // Mark callee-saved pop instruction. 1734*0b57cec5SDimitry Andric // Define the current CFA rule to use the provided offset. 1735*0b57cec5SDimitry Andric while (MBBI != MBB.end()) { 1736*0b57cec5SDimitry Andric MachineBasicBlock::iterator PI = MBBI; 1737*0b57cec5SDimitry Andric unsigned Opc = PI->getOpcode(); 1738*0b57cec5SDimitry Andric ++MBBI; 1739*0b57cec5SDimitry Andric if (Opc == X86::POP32r || Opc == X86::POP64r) { 1740*0b57cec5SDimitry Andric Offset += SlotSize; 1741*0b57cec5SDimitry Andric BuildCFI(MBB, MBBI, DL, 1742*0b57cec5SDimitry Andric MCCFIInstruction::createDefCfaOffset(nullptr, Offset)); 1743*0b57cec5SDimitry Andric } 1744*0b57cec5SDimitry Andric } 1745*0b57cec5SDimitry Andric } 1746*0b57cec5SDimitry Andric 1747*0b57cec5SDimitry Andric if (Terminator == MBB.end() || !isTailCallOpcode(Terminator->getOpcode())) { 1748*0b57cec5SDimitry Andric // Add the return addr area delta back since we are not tail calling. 1749*0b57cec5SDimitry Andric int Offset = -1 * X86FI->getTCReturnAddrDelta(); 1750*0b57cec5SDimitry Andric assert(Offset >= 0 && "TCDelta should never be positive"); 1751*0b57cec5SDimitry Andric if (Offset) { 1752*0b57cec5SDimitry Andric // Check for possible merge with preceding ADD instruction. 1753*0b57cec5SDimitry Andric Offset += mergeSPUpdates(MBB, Terminator, true); 1754*0b57cec5SDimitry Andric emitSPUpdate(MBB, Terminator, DL, Offset, /*InEpilogue=*/true); 1755*0b57cec5SDimitry Andric } 1756*0b57cec5SDimitry Andric } 1757*0b57cec5SDimitry Andric } 1758*0b57cec5SDimitry Andric 1759*0b57cec5SDimitry Andric int X86FrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI, 1760*0b57cec5SDimitry Andric unsigned &FrameReg) const { 1761*0b57cec5SDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo(); 1762*0b57cec5SDimitry Andric 1763*0b57cec5SDimitry Andric bool IsFixed = MFI.isFixedObjectIndex(FI); 1764*0b57cec5SDimitry Andric // We can't calculate offset from frame pointer if the stack is realigned, 1765*0b57cec5SDimitry Andric // so enforce usage of stack/base pointer. The base pointer is used when we 1766*0b57cec5SDimitry Andric // have dynamic allocas in addition to dynamic realignment. 1767*0b57cec5SDimitry Andric if (TRI->hasBasePointer(MF)) 1768*0b57cec5SDimitry Andric FrameReg = IsFixed ? TRI->getFramePtr() : TRI->getBaseRegister(); 1769*0b57cec5SDimitry Andric else if (TRI->needsStackRealignment(MF)) 1770*0b57cec5SDimitry Andric FrameReg = IsFixed ? TRI->getFramePtr() : TRI->getStackRegister(); 1771*0b57cec5SDimitry Andric else 1772*0b57cec5SDimitry Andric FrameReg = TRI->getFrameRegister(MF); 1773*0b57cec5SDimitry Andric 1774*0b57cec5SDimitry Andric // Offset will hold the offset from the stack pointer at function entry to the 1775*0b57cec5SDimitry Andric // object. 1776*0b57cec5SDimitry Andric // We need to factor in additional offsets applied during the prologue to the 1777*0b57cec5SDimitry Andric // frame, base, and stack pointer depending on which is used. 1778*0b57cec5SDimitry Andric int Offset = MFI.getObjectOffset(FI) - getOffsetOfLocalArea(); 1779*0b57cec5SDimitry Andric const X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); 1780*0b57cec5SDimitry Andric unsigned CSSize = X86FI->getCalleeSavedFrameSize(); 1781*0b57cec5SDimitry Andric uint64_t StackSize = MFI.getStackSize(); 1782*0b57cec5SDimitry Andric bool HasFP = hasFP(MF); 1783*0b57cec5SDimitry Andric bool IsWin64Prologue = MF.getTarget().getMCAsmInfo()->usesWindowsCFI(); 1784*0b57cec5SDimitry Andric int64_t FPDelta = 0; 1785*0b57cec5SDimitry Andric 1786*0b57cec5SDimitry Andric // In an x86 interrupt, remove the offset we added to account for the return 1787*0b57cec5SDimitry Andric // address from any stack object allocated in the caller's frame. Interrupts 1788*0b57cec5SDimitry Andric // do not have a standard return address. Fixed objects in the current frame, 1789*0b57cec5SDimitry Andric // such as SSE register spills, should not get this treatment. 1790*0b57cec5SDimitry Andric if (MF.getFunction().getCallingConv() == CallingConv::X86_INTR && 1791*0b57cec5SDimitry Andric Offset >= 0) { 1792*0b57cec5SDimitry Andric Offset += getOffsetOfLocalArea(); 1793*0b57cec5SDimitry Andric } 1794*0b57cec5SDimitry Andric 1795*0b57cec5SDimitry Andric if (IsWin64Prologue) { 1796*0b57cec5SDimitry Andric assert(!MFI.hasCalls() || (StackSize % 16) == 8); 1797*0b57cec5SDimitry Andric 1798*0b57cec5SDimitry Andric // Calculate required stack adjustment. 1799*0b57cec5SDimitry Andric uint64_t FrameSize = StackSize - SlotSize; 1800*0b57cec5SDimitry Andric // If required, include space for extra hidden slot for stashing base pointer. 1801*0b57cec5SDimitry Andric if (X86FI->getRestoreBasePointer()) 1802*0b57cec5SDimitry Andric FrameSize += SlotSize; 1803*0b57cec5SDimitry Andric uint64_t NumBytes = FrameSize - CSSize; 1804*0b57cec5SDimitry Andric 1805*0b57cec5SDimitry Andric uint64_t SEHFrameOffset = calculateSetFPREG(NumBytes); 1806*0b57cec5SDimitry Andric if (FI && FI == X86FI->getFAIndex()) 1807*0b57cec5SDimitry Andric return -SEHFrameOffset; 1808*0b57cec5SDimitry Andric 1809*0b57cec5SDimitry Andric // FPDelta is the offset from the "traditional" FP location of the old base 1810*0b57cec5SDimitry Andric // pointer followed by return address and the location required by the 1811*0b57cec5SDimitry Andric // restricted Win64 prologue. 1812*0b57cec5SDimitry Andric // Add FPDelta to all offsets below that go through the frame pointer. 1813*0b57cec5SDimitry Andric FPDelta = FrameSize - SEHFrameOffset; 1814*0b57cec5SDimitry Andric assert((!MFI.hasCalls() || (FPDelta % 16) == 0) && 1815*0b57cec5SDimitry Andric "FPDelta isn't aligned per the Win64 ABI!"); 1816*0b57cec5SDimitry Andric } 1817*0b57cec5SDimitry Andric 1818*0b57cec5SDimitry Andric 1819*0b57cec5SDimitry Andric if (TRI->hasBasePointer(MF)) { 1820*0b57cec5SDimitry Andric assert(HasFP && "VLAs and dynamic stack realign, but no FP?!"); 1821*0b57cec5SDimitry Andric if (FI < 0) { 1822*0b57cec5SDimitry Andric // Skip the saved EBP. 1823*0b57cec5SDimitry Andric return Offset + SlotSize + FPDelta; 1824*0b57cec5SDimitry Andric } else { 1825*0b57cec5SDimitry Andric assert((-(Offset + StackSize)) % MFI.getObjectAlignment(FI) == 0); 1826*0b57cec5SDimitry Andric return Offset + StackSize; 1827*0b57cec5SDimitry Andric } 1828*0b57cec5SDimitry Andric } else if (TRI->needsStackRealignment(MF)) { 1829*0b57cec5SDimitry Andric if (FI < 0) { 1830*0b57cec5SDimitry Andric // Skip the saved EBP. 1831*0b57cec5SDimitry Andric return Offset + SlotSize + FPDelta; 1832*0b57cec5SDimitry Andric } else { 1833*0b57cec5SDimitry Andric assert((-(Offset + StackSize)) % MFI.getObjectAlignment(FI) == 0); 1834*0b57cec5SDimitry Andric return Offset + StackSize; 1835*0b57cec5SDimitry Andric } 1836*0b57cec5SDimitry Andric // FIXME: Support tail calls 1837*0b57cec5SDimitry Andric } else { 1838*0b57cec5SDimitry Andric if (!HasFP) 1839*0b57cec5SDimitry Andric return Offset + StackSize; 1840*0b57cec5SDimitry Andric 1841*0b57cec5SDimitry Andric // Skip the saved EBP. 1842*0b57cec5SDimitry Andric Offset += SlotSize; 1843*0b57cec5SDimitry Andric 1844*0b57cec5SDimitry Andric // Skip the RETADDR move area 1845*0b57cec5SDimitry Andric int TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta(); 1846*0b57cec5SDimitry Andric if (TailCallReturnAddrDelta < 0) 1847*0b57cec5SDimitry Andric Offset -= TailCallReturnAddrDelta; 1848*0b57cec5SDimitry Andric } 1849*0b57cec5SDimitry Andric 1850*0b57cec5SDimitry Andric return Offset + FPDelta; 1851*0b57cec5SDimitry Andric } 1852*0b57cec5SDimitry Andric 1853*0b57cec5SDimitry Andric int X86FrameLowering::getFrameIndexReferenceSP(const MachineFunction &MF, 1854*0b57cec5SDimitry Andric int FI, unsigned &FrameReg, 1855*0b57cec5SDimitry Andric int Adjustment) const { 1856*0b57cec5SDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo(); 1857*0b57cec5SDimitry Andric FrameReg = TRI->getStackRegister(); 1858*0b57cec5SDimitry Andric return MFI.getObjectOffset(FI) - getOffsetOfLocalArea() + Adjustment; 1859*0b57cec5SDimitry Andric } 1860*0b57cec5SDimitry Andric 1861*0b57cec5SDimitry Andric int 1862*0b57cec5SDimitry Andric X86FrameLowering::getFrameIndexReferencePreferSP(const MachineFunction &MF, 1863*0b57cec5SDimitry Andric int FI, unsigned &FrameReg, 1864*0b57cec5SDimitry Andric bool IgnoreSPUpdates) const { 1865*0b57cec5SDimitry Andric 1866*0b57cec5SDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo(); 1867*0b57cec5SDimitry Andric // Does not include any dynamic realign. 1868*0b57cec5SDimitry Andric const uint64_t StackSize = MFI.getStackSize(); 1869*0b57cec5SDimitry Andric // LLVM arranges the stack as follows: 1870*0b57cec5SDimitry Andric // ... 1871*0b57cec5SDimitry Andric // ARG2 1872*0b57cec5SDimitry Andric // ARG1 1873*0b57cec5SDimitry Andric // RETADDR 1874*0b57cec5SDimitry Andric // PUSH RBP <-- RBP points here 1875*0b57cec5SDimitry Andric // PUSH CSRs 1876*0b57cec5SDimitry Andric // ~~~~~~~ <-- possible stack realignment (non-win64) 1877*0b57cec5SDimitry Andric // ... 1878*0b57cec5SDimitry Andric // STACK OBJECTS 1879*0b57cec5SDimitry Andric // ... <-- RSP after prologue points here 1880*0b57cec5SDimitry Andric // ~~~~~~~ <-- possible stack realignment (win64) 1881*0b57cec5SDimitry Andric // 1882*0b57cec5SDimitry Andric // if (hasVarSizedObjects()): 1883*0b57cec5SDimitry Andric // ... <-- "base pointer" (ESI/RBX) points here 1884*0b57cec5SDimitry Andric // DYNAMIC ALLOCAS 1885*0b57cec5SDimitry Andric // ... <-- RSP points here 1886*0b57cec5SDimitry Andric // 1887*0b57cec5SDimitry Andric // Case 1: In the simple case of no stack realignment and no dynamic 1888*0b57cec5SDimitry Andric // allocas, both "fixed" stack objects (arguments and CSRs) are addressable 1889*0b57cec5SDimitry Andric // with fixed offsets from RSP. 1890*0b57cec5SDimitry Andric // 1891*0b57cec5SDimitry Andric // Case 2: In the case of stack realignment with no dynamic allocas, fixed 1892*0b57cec5SDimitry Andric // stack objects are addressed with RBP and regular stack objects with RSP. 1893*0b57cec5SDimitry Andric // 1894*0b57cec5SDimitry Andric // Case 3: In the case of dynamic allocas and stack realignment, RSP is used 1895*0b57cec5SDimitry Andric // to address stack arguments for outgoing calls and nothing else. The "base 1896*0b57cec5SDimitry Andric // pointer" points to local variables, and RBP points to fixed objects. 1897*0b57cec5SDimitry Andric // 1898*0b57cec5SDimitry Andric // In cases 2 and 3, we can only answer for non-fixed stack objects, and the 1899*0b57cec5SDimitry Andric // answer we give is relative to the SP after the prologue, and not the 1900*0b57cec5SDimitry Andric // SP in the middle of the function. 1901*0b57cec5SDimitry Andric 1902*0b57cec5SDimitry Andric if (MFI.isFixedObjectIndex(FI) && TRI->needsStackRealignment(MF) && 1903*0b57cec5SDimitry Andric !STI.isTargetWin64()) 1904*0b57cec5SDimitry Andric return getFrameIndexReference(MF, FI, FrameReg); 1905*0b57cec5SDimitry Andric 1906*0b57cec5SDimitry Andric // If !hasReservedCallFrame the function might have SP adjustement in the 1907*0b57cec5SDimitry Andric // body. So, even though the offset is statically known, it depends on where 1908*0b57cec5SDimitry Andric // we are in the function. 1909*0b57cec5SDimitry Andric if (!IgnoreSPUpdates && !hasReservedCallFrame(MF)) 1910*0b57cec5SDimitry Andric return getFrameIndexReference(MF, FI, FrameReg); 1911*0b57cec5SDimitry Andric 1912*0b57cec5SDimitry Andric // We don't handle tail calls, and shouldn't be seeing them either. 1913*0b57cec5SDimitry Andric assert(MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta() >= 0 && 1914*0b57cec5SDimitry Andric "we don't handle this case!"); 1915*0b57cec5SDimitry Andric 1916*0b57cec5SDimitry Andric // This is how the math works out: 1917*0b57cec5SDimitry Andric // 1918*0b57cec5SDimitry Andric // %rsp grows (i.e. gets lower) left to right. Each box below is 1919*0b57cec5SDimitry Andric // one word (eight bytes). Obj0 is the stack slot we're trying to 1920*0b57cec5SDimitry Andric // get to. 1921*0b57cec5SDimitry Andric // 1922*0b57cec5SDimitry Andric // ---------------------------------- 1923*0b57cec5SDimitry Andric // | BP | Obj0 | Obj1 | ... | ObjN | 1924*0b57cec5SDimitry Andric // ---------------------------------- 1925*0b57cec5SDimitry Andric // ^ ^ ^ ^ 1926*0b57cec5SDimitry Andric // A B C E 1927*0b57cec5SDimitry Andric // 1928*0b57cec5SDimitry Andric // A is the incoming stack pointer. 1929*0b57cec5SDimitry Andric // (B - A) is the local area offset (-8 for x86-64) [1] 1930*0b57cec5SDimitry Andric // (C - A) is the Offset returned by MFI.getObjectOffset for Obj0 [2] 1931*0b57cec5SDimitry Andric // 1932*0b57cec5SDimitry Andric // |(E - B)| is the StackSize (absolute value, positive). For a 1933*0b57cec5SDimitry Andric // stack that grown down, this works out to be (B - E). [3] 1934*0b57cec5SDimitry Andric // 1935*0b57cec5SDimitry Andric // E is also the value of %rsp after stack has been set up, and we 1936*0b57cec5SDimitry Andric // want (C - E) -- the value we can add to %rsp to get to Obj0. Now 1937*0b57cec5SDimitry Andric // (C - E) == (C - A) - (B - A) + (B - E) 1938*0b57cec5SDimitry Andric // { Using [1], [2] and [3] above } 1939*0b57cec5SDimitry Andric // == getObjectOffset - LocalAreaOffset + StackSize 1940*0b57cec5SDimitry Andric 1941*0b57cec5SDimitry Andric return getFrameIndexReferenceSP(MF, FI, FrameReg, StackSize); 1942*0b57cec5SDimitry Andric } 1943*0b57cec5SDimitry Andric 1944*0b57cec5SDimitry Andric bool X86FrameLowering::assignCalleeSavedSpillSlots( 1945*0b57cec5SDimitry Andric MachineFunction &MF, const TargetRegisterInfo *TRI, 1946*0b57cec5SDimitry Andric std::vector<CalleeSavedInfo> &CSI) const { 1947*0b57cec5SDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 1948*0b57cec5SDimitry Andric X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); 1949*0b57cec5SDimitry Andric 1950*0b57cec5SDimitry Andric unsigned CalleeSavedFrameSize = 0; 1951*0b57cec5SDimitry Andric int SpillSlotOffset = getOffsetOfLocalArea() + X86FI->getTCReturnAddrDelta(); 1952*0b57cec5SDimitry Andric 1953*0b57cec5SDimitry Andric int64_t TailCallReturnAddrDelta = X86FI->getTCReturnAddrDelta(); 1954*0b57cec5SDimitry Andric 1955*0b57cec5SDimitry Andric if (TailCallReturnAddrDelta < 0) { 1956*0b57cec5SDimitry Andric // create RETURNADDR area 1957*0b57cec5SDimitry Andric // arg 1958*0b57cec5SDimitry Andric // arg 1959*0b57cec5SDimitry Andric // RETADDR 1960*0b57cec5SDimitry Andric // { ... 1961*0b57cec5SDimitry Andric // RETADDR area 1962*0b57cec5SDimitry Andric // ... 1963*0b57cec5SDimitry Andric // } 1964*0b57cec5SDimitry Andric // [EBP] 1965*0b57cec5SDimitry Andric MFI.CreateFixedObject(-TailCallReturnAddrDelta, 1966*0b57cec5SDimitry Andric TailCallReturnAddrDelta - SlotSize, true); 1967*0b57cec5SDimitry Andric } 1968*0b57cec5SDimitry Andric 1969*0b57cec5SDimitry Andric // Spill the BasePtr if it's used. 1970*0b57cec5SDimitry Andric if (this->TRI->hasBasePointer(MF)) { 1971*0b57cec5SDimitry Andric // Allocate a spill slot for EBP if we have a base pointer and EH funclets. 1972*0b57cec5SDimitry Andric if (MF.hasEHFunclets()) { 1973*0b57cec5SDimitry Andric int FI = MFI.CreateSpillStackObject(SlotSize, SlotSize); 1974*0b57cec5SDimitry Andric X86FI->setHasSEHFramePtrSave(true); 1975*0b57cec5SDimitry Andric X86FI->setSEHFramePtrSaveIndex(FI); 1976*0b57cec5SDimitry Andric } 1977*0b57cec5SDimitry Andric } 1978*0b57cec5SDimitry Andric 1979*0b57cec5SDimitry Andric if (hasFP(MF)) { 1980*0b57cec5SDimitry Andric // emitPrologue always spills frame register the first thing. 1981*0b57cec5SDimitry Andric SpillSlotOffset -= SlotSize; 1982*0b57cec5SDimitry Andric MFI.CreateFixedSpillStackObject(SlotSize, SpillSlotOffset); 1983*0b57cec5SDimitry Andric 1984*0b57cec5SDimitry Andric // Since emitPrologue and emitEpilogue will handle spilling and restoring of 1985*0b57cec5SDimitry Andric // the frame register, we can delete it from CSI list and not have to worry 1986*0b57cec5SDimitry Andric // about avoiding it later. 1987*0b57cec5SDimitry Andric unsigned FPReg = TRI->getFrameRegister(MF); 1988*0b57cec5SDimitry Andric for (unsigned i = 0; i < CSI.size(); ++i) { 1989*0b57cec5SDimitry Andric if (TRI->regsOverlap(CSI[i].getReg(),FPReg)) { 1990*0b57cec5SDimitry Andric CSI.erase(CSI.begin() + i); 1991*0b57cec5SDimitry Andric break; 1992*0b57cec5SDimitry Andric } 1993*0b57cec5SDimitry Andric } 1994*0b57cec5SDimitry Andric } 1995*0b57cec5SDimitry Andric 1996*0b57cec5SDimitry Andric // Assign slots for GPRs. It increases frame size. 1997*0b57cec5SDimitry Andric for (unsigned i = CSI.size(); i != 0; --i) { 1998*0b57cec5SDimitry Andric unsigned Reg = CSI[i - 1].getReg(); 1999*0b57cec5SDimitry Andric 2000*0b57cec5SDimitry Andric if (!X86::GR64RegClass.contains(Reg) && !X86::GR32RegClass.contains(Reg)) 2001*0b57cec5SDimitry Andric continue; 2002*0b57cec5SDimitry Andric 2003*0b57cec5SDimitry Andric SpillSlotOffset -= SlotSize; 2004*0b57cec5SDimitry Andric CalleeSavedFrameSize += SlotSize; 2005*0b57cec5SDimitry Andric 2006*0b57cec5SDimitry Andric int SlotIndex = MFI.CreateFixedSpillStackObject(SlotSize, SpillSlotOffset); 2007*0b57cec5SDimitry Andric CSI[i - 1].setFrameIdx(SlotIndex); 2008*0b57cec5SDimitry Andric } 2009*0b57cec5SDimitry Andric 2010*0b57cec5SDimitry Andric X86FI->setCalleeSavedFrameSize(CalleeSavedFrameSize); 2011*0b57cec5SDimitry Andric MFI.setCVBytesOfCalleeSavedRegisters(CalleeSavedFrameSize); 2012*0b57cec5SDimitry Andric 2013*0b57cec5SDimitry Andric // Assign slots for XMMs. 2014*0b57cec5SDimitry Andric for (unsigned i = CSI.size(); i != 0; --i) { 2015*0b57cec5SDimitry Andric unsigned Reg = CSI[i - 1].getReg(); 2016*0b57cec5SDimitry Andric if (X86::GR64RegClass.contains(Reg) || X86::GR32RegClass.contains(Reg)) 2017*0b57cec5SDimitry Andric continue; 2018*0b57cec5SDimitry Andric 2019*0b57cec5SDimitry Andric // If this is k-register make sure we lookup via the largest legal type. 2020*0b57cec5SDimitry Andric MVT VT = MVT::Other; 2021*0b57cec5SDimitry Andric if (X86::VK16RegClass.contains(Reg)) 2022*0b57cec5SDimitry Andric VT = STI.hasBWI() ? MVT::v64i1 : MVT::v16i1; 2023*0b57cec5SDimitry Andric 2024*0b57cec5SDimitry Andric const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT); 2025*0b57cec5SDimitry Andric unsigned Size = TRI->getSpillSize(*RC); 2026*0b57cec5SDimitry Andric unsigned Align = TRI->getSpillAlignment(*RC); 2027*0b57cec5SDimitry Andric // ensure alignment 2028*0b57cec5SDimitry Andric SpillSlotOffset -= std::abs(SpillSlotOffset) % Align; 2029*0b57cec5SDimitry Andric // spill into slot 2030*0b57cec5SDimitry Andric SpillSlotOffset -= Size; 2031*0b57cec5SDimitry Andric int SlotIndex = MFI.CreateFixedSpillStackObject(Size, SpillSlotOffset); 2032*0b57cec5SDimitry Andric CSI[i - 1].setFrameIdx(SlotIndex); 2033*0b57cec5SDimitry Andric MFI.ensureMaxAlignment(Align); 2034*0b57cec5SDimitry Andric } 2035*0b57cec5SDimitry Andric 2036*0b57cec5SDimitry Andric return true; 2037*0b57cec5SDimitry Andric } 2038*0b57cec5SDimitry Andric 2039*0b57cec5SDimitry Andric bool X86FrameLowering::spillCalleeSavedRegisters( 2040*0b57cec5SDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, 2041*0b57cec5SDimitry Andric const std::vector<CalleeSavedInfo> &CSI, 2042*0b57cec5SDimitry Andric const TargetRegisterInfo *TRI) const { 2043*0b57cec5SDimitry Andric DebugLoc DL = MBB.findDebugLoc(MI); 2044*0b57cec5SDimitry Andric 2045*0b57cec5SDimitry Andric // Don't save CSRs in 32-bit EH funclets. The caller saves EBX, EBP, ESI, EDI 2046*0b57cec5SDimitry Andric // for us, and there are no XMM CSRs on Win32. 2047*0b57cec5SDimitry Andric if (MBB.isEHFuncletEntry() && STI.is32Bit() && STI.isOSWindows()) 2048*0b57cec5SDimitry Andric return true; 2049*0b57cec5SDimitry Andric 2050*0b57cec5SDimitry Andric // Push GPRs. It increases frame size. 2051*0b57cec5SDimitry Andric const MachineFunction &MF = *MBB.getParent(); 2052*0b57cec5SDimitry Andric unsigned Opc = STI.is64Bit() ? X86::PUSH64r : X86::PUSH32r; 2053*0b57cec5SDimitry Andric for (unsigned i = CSI.size(); i != 0; --i) { 2054*0b57cec5SDimitry Andric unsigned Reg = CSI[i - 1].getReg(); 2055*0b57cec5SDimitry Andric 2056*0b57cec5SDimitry Andric if (!X86::GR64RegClass.contains(Reg) && !X86::GR32RegClass.contains(Reg)) 2057*0b57cec5SDimitry Andric continue; 2058*0b57cec5SDimitry Andric 2059*0b57cec5SDimitry Andric const MachineRegisterInfo &MRI = MF.getRegInfo(); 2060*0b57cec5SDimitry Andric bool isLiveIn = MRI.isLiveIn(Reg); 2061*0b57cec5SDimitry Andric if (!isLiveIn) 2062*0b57cec5SDimitry Andric MBB.addLiveIn(Reg); 2063*0b57cec5SDimitry Andric 2064*0b57cec5SDimitry Andric // Decide whether we can add a kill flag to the use. 2065*0b57cec5SDimitry Andric bool CanKill = !isLiveIn; 2066*0b57cec5SDimitry Andric // Check if any subregister is live-in 2067*0b57cec5SDimitry Andric if (CanKill) { 2068*0b57cec5SDimitry Andric for (MCRegAliasIterator AReg(Reg, TRI, false); AReg.isValid(); ++AReg) { 2069*0b57cec5SDimitry Andric if (MRI.isLiveIn(*AReg)) { 2070*0b57cec5SDimitry Andric CanKill = false; 2071*0b57cec5SDimitry Andric break; 2072*0b57cec5SDimitry Andric } 2073*0b57cec5SDimitry Andric } 2074*0b57cec5SDimitry Andric } 2075*0b57cec5SDimitry Andric 2076*0b57cec5SDimitry Andric // Do not set a kill flag on values that are also marked as live-in. This 2077*0b57cec5SDimitry Andric // happens with the @llvm-returnaddress intrinsic and with arguments 2078*0b57cec5SDimitry Andric // passed in callee saved registers. 2079*0b57cec5SDimitry Andric // Omitting the kill flags is conservatively correct even if the live-in 2080*0b57cec5SDimitry Andric // is not used after all. 2081*0b57cec5SDimitry Andric BuildMI(MBB, MI, DL, TII.get(Opc)).addReg(Reg, getKillRegState(CanKill)) 2082*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 2083*0b57cec5SDimitry Andric } 2084*0b57cec5SDimitry Andric 2085*0b57cec5SDimitry Andric // Make XMM regs spilled. X86 does not have ability of push/pop XMM. 2086*0b57cec5SDimitry Andric // It can be done by spilling XMMs to stack frame. 2087*0b57cec5SDimitry Andric for (unsigned i = CSI.size(); i != 0; --i) { 2088*0b57cec5SDimitry Andric unsigned Reg = CSI[i-1].getReg(); 2089*0b57cec5SDimitry Andric if (X86::GR64RegClass.contains(Reg) || X86::GR32RegClass.contains(Reg)) 2090*0b57cec5SDimitry Andric continue; 2091*0b57cec5SDimitry Andric 2092*0b57cec5SDimitry Andric // If this is k-register make sure we lookup via the largest legal type. 2093*0b57cec5SDimitry Andric MVT VT = MVT::Other; 2094*0b57cec5SDimitry Andric if (X86::VK16RegClass.contains(Reg)) 2095*0b57cec5SDimitry Andric VT = STI.hasBWI() ? MVT::v64i1 : MVT::v16i1; 2096*0b57cec5SDimitry Andric 2097*0b57cec5SDimitry Andric // Add the callee-saved register as live-in. It's killed at the spill. 2098*0b57cec5SDimitry Andric MBB.addLiveIn(Reg); 2099*0b57cec5SDimitry Andric const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT); 2100*0b57cec5SDimitry Andric 2101*0b57cec5SDimitry Andric TII.storeRegToStackSlot(MBB, MI, Reg, true, CSI[i - 1].getFrameIdx(), RC, 2102*0b57cec5SDimitry Andric TRI); 2103*0b57cec5SDimitry Andric --MI; 2104*0b57cec5SDimitry Andric MI->setFlag(MachineInstr::FrameSetup); 2105*0b57cec5SDimitry Andric ++MI; 2106*0b57cec5SDimitry Andric } 2107*0b57cec5SDimitry Andric 2108*0b57cec5SDimitry Andric return true; 2109*0b57cec5SDimitry Andric } 2110*0b57cec5SDimitry Andric 2111*0b57cec5SDimitry Andric void X86FrameLowering::emitCatchRetReturnValue(MachineBasicBlock &MBB, 2112*0b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI, 2113*0b57cec5SDimitry Andric MachineInstr *CatchRet) const { 2114*0b57cec5SDimitry Andric // SEH shouldn't use catchret. 2115*0b57cec5SDimitry Andric assert(!isAsynchronousEHPersonality(classifyEHPersonality( 2116*0b57cec5SDimitry Andric MBB.getParent()->getFunction().getPersonalityFn())) && 2117*0b57cec5SDimitry Andric "SEH should not use CATCHRET"); 2118*0b57cec5SDimitry Andric DebugLoc DL = CatchRet->getDebugLoc(); 2119*0b57cec5SDimitry Andric MachineBasicBlock *CatchRetTarget = CatchRet->getOperand(0).getMBB(); 2120*0b57cec5SDimitry Andric 2121*0b57cec5SDimitry Andric // Fill EAX/RAX with the address of the target block. 2122*0b57cec5SDimitry Andric if (STI.is64Bit()) { 2123*0b57cec5SDimitry Andric // LEA64r CatchRetTarget(%rip), %rax 2124*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(X86::LEA64r), X86::RAX) 2125*0b57cec5SDimitry Andric .addReg(X86::RIP) 2126*0b57cec5SDimitry Andric .addImm(0) 2127*0b57cec5SDimitry Andric .addReg(0) 2128*0b57cec5SDimitry Andric .addMBB(CatchRetTarget) 2129*0b57cec5SDimitry Andric .addReg(0); 2130*0b57cec5SDimitry Andric } else { 2131*0b57cec5SDimitry Andric // MOV32ri $CatchRetTarget, %eax 2132*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32ri), X86::EAX) 2133*0b57cec5SDimitry Andric .addMBB(CatchRetTarget); 2134*0b57cec5SDimitry Andric } 2135*0b57cec5SDimitry Andric 2136*0b57cec5SDimitry Andric // Record that we've taken the address of CatchRetTarget and no longer just 2137*0b57cec5SDimitry Andric // reference it in a terminator. 2138*0b57cec5SDimitry Andric CatchRetTarget->setHasAddressTaken(); 2139*0b57cec5SDimitry Andric } 2140*0b57cec5SDimitry Andric 2141*0b57cec5SDimitry Andric bool X86FrameLowering::restoreCalleeSavedRegisters(MachineBasicBlock &MBB, 2142*0b57cec5SDimitry Andric MachineBasicBlock::iterator MI, 2143*0b57cec5SDimitry Andric std::vector<CalleeSavedInfo> &CSI, 2144*0b57cec5SDimitry Andric const TargetRegisterInfo *TRI) const { 2145*0b57cec5SDimitry Andric if (CSI.empty()) 2146*0b57cec5SDimitry Andric return false; 2147*0b57cec5SDimitry Andric 2148*0b57cec5SDimitry Andric if (MI != MBB.end() && isFuncletReturnInstr(*MI) && STI.isOSWindows()) { 2149*0b57cec5SDimitry Andric // Don't restore CSRs in 32-bit EH funclets. Matches 2150*0b57cec5SDimitry Andric // spillCalleeSavedRegisters. 2151*0b57cec5SDimitry Andric if (STI.is32Bit()) 2152*0b57cec5SDimitry Andric return true; 2153*0b57cec5SDimitry Andric // Don't restore CSRs before an SEH catchret. SEH except blocks do not form 2154*0b57cec5SDimitry Andric // funclets. emitEpilogue transforms these to normal jumps. 2155*0b57cec5SDimitry Andric if (MI->getOpcode() == X86::CATCHRET) { 2156*0b57cec5SDimitry Andric const Function &F = MBB.getParent()->getFunction(); 2157*0b57cec5SDimitry Andric bool IsSEH = isAsynchronousEHPersonality( 2158*0b57cec5SDimitry Andric classifyEHPersonality(F.getPersonalityFn())); 2159*0b57cec5SDimitry Andric if (IsSEH) 2160*0b57cec5SDimitry Andric return true; 2161*0b57cec5SDimitry Andric } 2162*0b57cec5SDimitry Andric } 2163*0b57cec5SDimitry Andric 2164*0b57cec5SDimitry Andric DebugLoc DL = MBB.findDebugLoc(MI); 2165*0b57cec5SDimitry Andric 2166*0b57cec5SDimitry Andric // Reload XMMs from stack frame. 2167*0b57cec5SDimitry Andric for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 2168*0b57cec5SDimitry Andric unsigned Reg = CSI[i].getReg(); 2169*0b57cec5SDimitry Andric if (X86::GR64RegClass.contains(Reg) || 2170*0b57cec5SDimitry Andric X86::GR32RegClass.contains(Reg)) 2171*0b57cec5SDimitry Andric continue; 2172*0b57cec5SDimitry Andric 2173*0b57cec5SDimitry Andric // If this is k-register make sure we lookup via the largest legal type. 2174*0b57cec5SDimitry Andric MVT VT = MVT::Other; 2175*0b57cec5SDimitry Andric if (X86::VK16RegClass.contains(Reg)) 2176*0b57cec5SDimitry Andric VT = STI.hasBWI() ? MVT::v64i1 : MVT::v16i1; 2177*0b57cec5SDimitry Andric 2178*0b57cec5SDimitry Andric const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT); 2179*0b57cec5SDimitry Andric TII.loadRegFromStackSlot(MBB, MI, Reg, CSI[i].getFrameIdx(), RC, TRI); 2180*0b57cec5SDimitry Andric } 2181*0b57cec5SDimitry Andric 2182*0b57cec5SDimitry Andric // POP GPRs. 2183*0b57cec5SDimitry Andric unsigned Opc = STI.is64Bit() ? X86::POP64r : X86::POP32r; 2184*0b57cec5SDimitry Andric for (unsigned i = 0, e = CSI.size(); i != e; ++i) { 2185*0b57cec5SDimitry Andric unsigned Reg = CSI[i].getReg(); 2186*0b57cec5SDimitry Andric if (!X86::GR64RegClass.contains(Reg) && 2187*0b57cec5SDimitry Andric !X86::GR32RegClass.contains(Reg)) 2188*0b57cec5SDimitry Andric continue; 2189*0b57cec5SDimitry Andric 2190*0b57cec5SDimitry Andric BuildMI(MBB, MI, DL, TII.get(Opc), Reg) 2191*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameDestroy); 2192*0b57cec5SDimitry Andric } 2193*0b57cec5SDimitry Andric return true; 2194*0b57cec5SDimitry Andric } 2195*0b57cec5SDimitry Andric 2196*0b57cec5SDimitry Andric void X86FrameLowering::determineCalleeSaves(MachineFunction &MF, 2197*0b57cec5SDimitry Andric BitVector &SavedRegs, 2198*0b57cec5SDimitry Andric RegScavenger *RS) const { 2199*0b57cec5SDimitry Andric TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS); 2200*0b57cec5SDimitry Andric 2201*0b57cec5SDimitry Andric // Spill the BasePtr if it's used. 2202*0b57cec5SDimitry Andric if (TRI->hasBasePointer(MF)){ 2203*0b57cec5SDimitry Andric unsigned BasePtr = TRI->getBaseRegister(); 2204*0b57cec5SDimitry Andric if (STI.isTarget64BitILP32()) 2205*0b57cec5SDimitry Andric BasePtr = getX86SubSuperRegister(BasePtr, 64); 2206*0b57cec5SDimitry Andric SavedRegs.set(BasePtr); 2207*0b57cec5SDimitry Andric } 2208*0b57cec5SDimitry Andric } 2209*0b57cec5SDimitry Andric 2210*0b57cec5SDimitry Andric static bool 2211*0b57cec5SDimitry Andric HasNestArgument(const MachineFunction *MF) { 2212*0b57cec5SDimitry Andric const Function &F = MF->getFunction(); 2213*0b57cec5SDimitry Andric for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); 2214*0b57cec5SDimitry Andric I != E; I++) { 2215*0b57cec5SDimitry Andric if (I->hasNestAttr()) 2216*0b57cec5SDimitry Andric return true; 2217*0b57cec5SDimitry Andric } 2218*0b57cec5SDimitry Andric return false; 2219*0b57cec5SDimitry Andric } 2220*0b57cec5SDimitry Andric 2221*0b57cec5SDimitry Andric /// GetScratchRegister - Get a temp register for performing work in the 2222*0b57cec5SDimitry Andric /// segmented stack and the Erlang/HiPE stack prologue. Depending on platform 2223*0b57cec5SDimitry Andric /// and the properties of the function either one or two registers will be 2224*0b57cec5SDimitry Andric /// needed. Set primary to true for the first register, false for the second. 2225*0b57cec5SDimitry Andric static unsigned 2226*0b57cec5SDimitry Andric GetScratchRegister(bool Is64Bit, bool IsLP64, const MachineFunction &MF, bool Primary) { 2227*0b57cec5SDimitry Andric CallingConv::ID CallingConvention = MF.getFunction().getCallingConv(); 2228*0b57cec5SDimitry Andric 2229*0b57cec5SDimitry Andric // Erlang stuff. 2230*0b57cec5SDimitry Andric if (CallingConvention == CallingConv::HiPE) { 2231*0b57cec5SDimitry Andric if (Is64Bit) 2232*0b57cec5SDimitry Andric return Primary ? X86::R14 : X86::R13; 2233*0b57cec5SDimitry Andric else 2234*0b57cec5SDimitry Andric return Primary ? X86::EBX : X86::EDI; 2235*0b57cec5SDimitry Andric } 2236*0b57cec5SDimitry Andric 2237*0b57cec5SDimitry Andric if (Is64Bit) { 2238*0b57cec5SDimitry Andric if (IsLP64) 2239*0b57cec5SDimitry Andric return Primary ? X86::R11 : X86::R12; 2240*0b57cec5SDimitry Andric else 2241*0b57cec5SDimitry Andric return Primary ? X86::R11D : X86::R12D; 2242*0b57cec5SDimitry Andric } 2243*0b57cec5SDimitry Andric 2244*0b57cec5SDimitry Andric bool IsNested = HasNestArgument(&MF); 2245*0b57cec5SDimitry Andric 2246*0b57cec5SDimitry Andric if (CallingConvention == CallingConv::X86_FastCall || 2247*0b57cec5SDimitry Andric CallingConvention == CallingConv::Fast) { 2248*0b57cec5SDimitry Andric if (IsNested) 2249*0b57cec5SDimitry Andric report_fatal_error("Segmented stacks does not support fastcall with " 2250*0b57cec5SDimitry Andric "nested function."); 2251*0b57cec5SDimitry Andric return Primary ? X86::EAX : X86::ECX; 2252*0b57cec5SDimitry Andric } 2253*0b57cec5SDimitry Andric if (IsNested) 2254*0b57cec5SDimitry Andric return Primary ? X86::EDX : X86::EAX; 2255*0b57cec5SDimitry Andric return Primary ? X86::ECX : X86::EAX; 2256*0b57cec5SDimitry Andric } 2257*0b57cec5SDimitry Andric 2258*0b57cec5SDimitry Andric // The stack limit in the TCB is set to this many bytes above the actual stack 2259*0b57cec5SDimitry Andric // limit. 2260*0b57cec5SDimitry Andric static const uint64_t kSplitStackAvailable = 256; 2261*0b57cec5SDimitry Andric 2262*0b57cec5SDimitry Andric void X86FrameLowering::adjustForSegmentedStacks( 2263*0b57cec5SDimitry Andric MachineFunction &MF, MachineBasicBlock &PrologueMBB) const { 2264*0b57cec5SDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 2265*0b57cec5SDimitry Andric uint64_t StackSize; 2266*0b57cec5SDimitry Andric unsigned TlsReg, TlsOffset; 2267*0b57cec5SDimitry Andric DebugLoc DL; 2268*0b57cec5SDimitry Andric 2269*0b57cec5SDimitry Andric // To support shrink-wrapping we would need to insert the new blocks 2270*0b57cec5SDimitry Andric // at the right place and update the branches to PrologueMBB. 2271*0b57cec5SDimitry Andric assert(&(*MF.begin()) == &PrologueMBB && "Shrink-wrapping not supported yet"); 2272*0b57cec5SDimitry Andric 2273*0b57cec5SDimitry Andric unsigned ScratchReg = GetScratchRegister(Is64Bit, IsLP64, MF, true); 2274*0b57cec5SDimitry Andric assert(!MF.getRegInfo().isLiveIn(ScratchReg) && 2275*0b57cec5SDimitry Andric "Scratch register is live-in"); 2276*0b57cec5SDimitry Andric 2277*0b57cec5SDimitry Andric if (MF.getFunction().isVarArg()) 2278*0b57cec5SDimitry Andric report_fatal_error("Segmented stacks do not support vararg functions."); 2279*0b57cec5SDimitry Andric if (!STI.isTargetLinux() && !STI.isTargetDarwin() && !STI.isTargetWin32() && 2280*0b57cec5SDimitry Andric !STI.isTargetWin64() && !STI.isTargetFreeBSD() && 2281*0b57cec5SDimitry Andric !STI.isTargetDragonFly()) 2282*0b57cec5SDimitry Andric report_fatal_error("Segmented stacks not supported on this platform."); 2283*0b57cec5SDimitry Andric 2284*0b57cec5SDimitry Andric // Eventually StackSize will be calculated by a link-time pass; which will 2285*0b57cec5SDimitry Andric // also decide whether checking code needs to be injected into this particular 2286*0b57cec5SDimitry Andric // prologue. 2287*0b57cec5SDimitry Andric StackSize = MFI.getStackSize(); 2288*0b57cec5SDimitry Andric 2289*0b57cec5SDimitry Andric // Do not generate a prologue for leaf functions with a stack of size zero. 2290*0b57cec5SDimitry Andric // For non-leaf functions we have to allow for the possibility that the 2291*0b57cec5SDimitry Andric // callis to a non-split function, as in PR37807. This function could also 2292*0b57cec5SDimitry Andric // take the address of a non-split function. When the linker tries to adjust 2293*0b57cec5SDimitry Andric // its non-existent prologue, it would fail with an error. Mark the object 2294*0b57cec5SDimitry Andric // file so that such failures are not errors. See this Go language bug-report 2295*0b57cec5SDimitry Andric // https://go-review.googlesource.com/c/go/+/148819/ 2296*0b57cec5SDimitry Andric if (StackSize == 0 && !MFI.hasTailCall()) { 2297*0b57cec5SDimitry Andric MF.getMMI().setHasNosplitStack(true); 2298*0b57cec5SDimitry Andric return; 2299*0b57cec5SDimitry Andric } 2300*0b57cec5SDimitry Andric 2301*0b57cec5SDimitry Andric MachineBasicBlock *allocMBB = MF.CreateMachineBasicBlock(); 2302*0b57cec5SDimitry Andric MachineBasicBlock *checkMBB = MF.CreateMachineBasicBlock(); 2303*0b57cec5SDimitry Andric X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); 2304*0b57cec5SDimitry Andric bool IsNested = false; 2305*0b57cec5SDimitry Andric 2306*0b57cec5SDimitry Andric // We need to know if the function has a nest argument only in 64 bit mode. 2307*0b57cec5SDimitry Andric if (Is64Bit) 2308*0b57cec5SDimitry Andric IsNested = HasNestArgument(&MF); 2309*0b57cec5SDimitry Andric 2310*0b57cec5SDimitry Andric // The MOV R10, RAX needs to be in a different block, since the RET we emit in 2311*0b57cec5SDimitry Andric // allocMBB needs to be last (terminating) instruction. 2312*0b57cec5SDimitry Andric 2313*0b57cec5SDimitry Andric for (const auto &LI : PrologueMBB.liveins()) { 2314*0b57cec5SDimitry Andric allocMBB->addLiveIn(LI); 2315*0b57cec5SDimitry Andric checkMBB->addLiveIn(LI); 2316*0b57cec5SDimitry Andric } 2317*0b57cec5SDimitry Andric 2318*0b57cec5SDimitry Andric if (IsNested) 2319*0b57cec5SDimitry Andric allocMBB->addLiveIn(IsLP64 ? X86::R10 : X86::R10D); 2320*0b57cec5SDimitry Andric 2321*0b57cec5SDimitry Andric MF.push_front(allocMBB); 2322*0b57cec5SDimitry Andric MF.push_front(checkMBB); 2323*0b57cec5SDimitry Andric 2324*0b57cec5SDimitry Andric // When the frame size is less than 256 we just compare the stack 2325*0b57cec5SDimitry Andric // boundary directly to the value of the stack pointer, per gcc. 2326*0b57cec5SDimitry Andric bool CompareStackPointer = StackSize < kSplitStackAvailable; 2327*0b57cec5SDimitry Andric 2328*0b57cec5SDimitry Andric // Read the limit off the current stacklet off the stack_guard location. 2329*0b57cec5SDimitry Andric if (Is64Bit) { 2330*0b57cec5SDimitry Andric if (STI.isTargetLinux()) { 2331*0b57cec5SDimitry Andric TlsReg = X86::FS; 2332*0b57cec5SDimitry Andric TlsOffset = IsLP64 ? 0x70 : 0x40; 2333*0b57cec5SDimitry Andric } else if (STI.isTargetDarwin()) { 2334*0b57cec5SDimitry Andric TlsReg = X86::GS; 2335*0b57cec5SDimitry Andric TlsOffset = 0x60 + 90*8; // See pthread_machdep.h. Steal TLS slot 90. 2336*0b57cec5SDimitry Andric } else if (STI.isTargetWin64()) { 2337*0b57cec5SDimitry Andric TlsReg = X86::GS; 2338*0b57cec5SDimitry Andric TlsOffset = 0x28; // pvArbitrary, reserved for application use 2339*0b57cec5SDimitry Andric } else if (STI.isTargetFreeBSD()) { 2340*0b57cec5SDimitry Andric TlsReg = X86::FS; 2341*0b57cec5SDimitry Andric TlsOffset = 0x18; 2342*0b57cec5SDimitry Andric } else if (STI.isTargetDragonFly()) { 2343*0b57cec5SDimitry Andric TlsReg = X86::FS; 2344*0b57cec5SDimitry Andric TlsOffset = 0x20; // use tls_tcb.tcb_segstack 2345*0b57cec5SDimitry Andric } else { 2346*0b57cec5SDimitry Andric report_fatal_error("Segmented stacks not supported on this platform."); 2347*0b57cec5SDimitry Andric } 2348*0b57cec5SDimitry Andric 2349*0b57cec5SDimitry Andric if (CompareStackPointer) 2350*0b57cec5SDimitry Andric ScratchReg = IsLP64 ? X86::RSP : X86::ESP; 2351*0b57cec5SDimitry Andric else 2352*0b57cec5SDimitry Andric BuildMI(checkMBB, DL, TII.get(IsLP64 ? X86::LEA64r : X86::LEA64_32r), ScratchReg).addReg(X86::RSP) 2353*0b57cec5SDimitry Andric .addImm(1).addReg(0).addImm(-StackSize).addReg(0); 2354*0b57cec5SDimitry Andric 2355*0b57cec5SDimitry Andric BuildMI(checkMBB, DL, TII.get(IsLP64 ? X86::CMP64rm : X86::CMP32rm)).addReg(ScratchReg) 2356*0b57cec5SDimitry Andric .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg); 2357*0b57cec5SDimitry Andric } else { 2358*0b57cec5SDimitry Andric if (STI.isTargetLinux()) { 2359*0b57cec5SDimitry Andric TlsReg = X86::GS; 2360*0b57cec5SDimitry Andric TlsOffset = 0x30; 2361*0b57cec5SDimitry Andric } else if (STI.isTargetDarwin()) { 2362*0b57cec5SDimitry Andric TlsReg = X86::GS; 2363*0b57cec5SDimitry Andric TlsOffset = 0x48 + 90*4; 2364*0b57cec5SDimitry Andric } else if (STI.isTargetWin32()) { 2365*0b57cec5SDimitry Andric TlsReg = X86::FS; 2366*0b57cec5SDimitry Andric TlsOffset = 0x14; // pvArbitrary, reserved for application use 2367*0b57cec5SDimitry Andric } else if (STI.isTargetDragonFly()) { 2368*0b57cec5SDimitry Andric TlsReg = X86::FS; 2369*0b57cec5SDimitry Andric TlsOffset = 0x10; // use tls_tcb.tcb_segstack 2370*0b57cec5SDimitry Andric } else if (STI.isTargetFreeBSD()) { 2371*0b57cec5SDimitry Andric report_fatal_error("Segmented stacks not supported on FreeBSD i386."); 2372*0b57cec5SDimitry Andric } else { 2373*0b57cec5SDimitry Andric report_fatal_error("Segmented stacks not supported on this platform."); 2374*0b57cec5SDimitry Andric } 2375*0b57cec5SDimitry Andric 2376*0b57cec5SDimitry Andric if (CompareStackPointer) 2377*0b57cec5SDimitry Andric ScratchReg = X86::ESP; 2378*0b57cec5SDimitry Andric else 2379*0b57cec5SDimitry Andric BuildMI(checkMBB, DL, TII.get(X86::LEA32r), ScratchReg).addReg(X86::ESP) 2380*0b57cec5SDimitry Andric .addImm(1).addReg(0).addImm(-StackSize).addReg(0); 2381*0b57cec5SDimitry Andric 2382*0b57cec5SDimitry Andric if (STI.isTargetLinux() || STI.isTargetWin32() || STI.isTargetWin64() || 2383*0b57cec5SDimitry Andric STI.isTargetDragonFly()) { 2384*0b57cec5SDimitry Andric BuildMI(checkMBB, DL, TII.get(X86::CMP32rm)).addReg(ScratchReg) 2385*0b57cec5SDimitry Andric .addReg(0).addImm(0).addReg(0).addImm(TlsOffset).addReg(TlsReg); 2386*0b57cec5SDimitry Andric } else if (STI.isTargetDarwin()) { 2387*0b57cec5SDimitry Andric 2388*0b57cec5SDimitry Andric // TlsOffset doesn't fit into a mod r/m byte so we need an extra register. 2389*0b57cec5SDimitry Andric unsigned ScratchReg2; 2390*0b57cec5SDimitry Andric bool SaveScratch2; 2391*0b57cec5SDimitry Andric if (CompareStackPointer) { 2392*0b57cec5SDimitry Andric // The primary scratch register is available for holding the TLS offset. 2393*0b57cec5SDimitry Andric ScratchReg2 = GetScratchRegister(Is64Bit, IsLP64, MF, true); 2394*0b57cec5SDimitry Andric SaveScratch2 = false; 2395*0b57cec5SDimitry Andric } else { 2396*0b57cec5SDimitry Andric // Need to use a second register to hold the TLS offset 2397*0b57cec5SDimitry Andric ScratchReg2 = GetScratchRegister(Is64Bit, IsLP64, MF, false); 2398*0b57cec5SDimitry Andric 2399*0b57cec5SDimitry Andric // Unfortunately, with fastcc the second scratch register may hold an 2400*0b57cec5SDimitry Andric // argument. 2401*0b57cec5SDimitry Andric SaveScratch2 = MF.getRegInfo().isLiveIn(ScratchReg2); 2402*0b57cec5SDimitry Andric } 2403*0b57cec5SDimitry Andric 2404*0b57cec5SDimitry Andric // If Scratch2 is live-in then it needs to be saved. 2405*0b57cec5SDimitry Andric assert((!MF.getRegInfo().isLiveIn(ScratchReg2) || SaveScratch2) && 2406*0b57cec5SDimitry Andric "Scratch register is live-in and not saved"); 2407*0b57cec5SDimitry Andric 2408*0b57cec5SDimitry Andric if (SaveScratch2) 2409*0b57cec5SDimitry Andric BuildMI(checkMBB, DL, TII.get(X86::PUSH32r)) 2410*0b57cec5SDimitry Andric .addReg(ScratchReg2, RegState::Kill); 2411*0b57cec5SDimitry Andric 2412*0b57cec5SDimitry Andric BuildMI(checkMBB, DL, TII.get(X86::MOV32ri), ScratchReg2) 2413*0b57cec5SDimitry Andric .addImm(TlsOffset); 2414*0b57cec5SDimitry Andric BuildMI(checkMBB, DL, TII.get(X86::CMP32rm)) 2415*0b57cec5SDimitry Andric .addReg(ScratchReg) 2416*0b57cec5SDimitry Andric .addReg(ScratchReg2).addImm(1).addReg(0) 2417*0b57cec5SDimitry Andric .addImm(0) 2418*0b57cec5SDimitry Andric .addReg(TlsReg); 2419*0b57cec5SDimitry Andric 2420*0b57cec5SDimitry Andric if (SaveScratch2) 2421*0b57cec5SDimitry Andric BuildMI(checkMBB, DL, TII.get(X86::POP32r), ScratchReg2); 2422*0b57cec5SDimitry Andric } 2423*0b57cec5SDimitry Andric } 2424*0b57cec5SDimitry Andric 2425*0b57cec5SDimitry Andric // This jump is taken if SP >= (Stacklet Limit + Stack Space required). 2426*0b57cec5SDimitry Andric // It jumps to normal execution of the function body. 2427*0b57cec5SDimitry Andric BuildMI(checkMBB, DL, TII.get(X86::JCC_1)).addMBB(&PrologueMBB).addImm(X86::COND_A); 2428*0b57cec5SDimitry Andric 2429*0b57cec5SDimitry Andric // On 32 bit we first push the arguments size and then the frame size. On 64 2430*0b57cec5SDimitry Andric // bit, we pass the stack frame size in r10 and the argument size in r11. 2431*0b57cec5SDimitry Andric if (Is64Bit) { 2432*0b57cec5SDimitry Andric // Functions with nested arguments use R10, so it needs to be saved across 2433*0b57cec5SDimitry Andric // the call to _morestack 2434*0b57cec5SDimitry Andric 2435*0b57cec5SDimitry Andric const unsigned RegAX = IsLP64 ? X86::RAX : X86::EAX; 2436*0b57cec5SDimitry Andric const unsigned Reg10 = IsLP64 ? X86::R10 : X86::R10D; 2437*0b57cec5SDimitry Andric const unsigned Reg11 = IsLP64 ? X86::R11 : X86::R11D; 2438*0b57cec5SDimitry Andric const unsigned MOVrr = IsLP64 ? X86::MOV64rr : X86::MOV32rr; 2439*0b57cec5SDimitry Andric const unsigned MOVri = IsLP64 ? X86::MOV64ri : X86::MOV32ri; 2440*0b57cec5SDimitry Andric 2441*0b57cec5SDimitry Andric if (IsNested) 2442*0b57cec5SDimitry Andric BuildMI(allocMBB, DL, TII.get(MOVrr), RegAX).addReg(Reg10); 2443*0b57cec5SDimitry Andric 2444*0b57cec5SDimitry Andric BuildMI(allocMBB, DL, TII.get(MOVri), Reg10) 2445*0b57cec5SDimitry Andric .addImm(StackSize); 2446*0b57cec5SDimitry Andric BuildMI(allocMBB, DL, TII.get(MOVri), Reg11) 2447*0b57cec5SDimitry Andric .addImm(X86FI->getArgumentStackSize()); 2448*0b57cec5SDimitry Andric } else { 2449*0b57cec5SDimitry Andric BuildMI(allocMBB, DL, TII.get(X86::PUSHi32)) 2450*0b57cec5SDimitry Andric .addImm(X86FI->getArgumentStackSize()); 2451*0b57cec5SDimitry Andric BuildMI(allocMBB, DL, TII.get(X86::PUSHi32)) 2452*0b57cec5SDimitry Andric .addImm(StackSize); 2453*0b57cec5SDimitry Andric } 2454*0b57cec5SDimitry Andric 2455*0b57cec5SDimitry Andric // __morestack is in libgcc 2456*0b57cec5SDimitry Andric if (Is64Bit && MF.getTarget().getCodeModel() == CodeModel::Large) { 2457*0b57cec5SDimitry Andric // Under the large code model, we cannot assume that __morestack lives 2458*0b57cec5SDimitry Andric // within 2^31 bytes of the call site, so we cannot use pc-relative 2459*0b57cec5SDimitry Andric // addressing. We cannot perform the call via a temporary register, 2460*0b57cec5SDimitry Andric // as the rax register may be used to store the static chain, and all 2461*0b57cec5SDimitry Andric // other suitable registers may be either callee-save or used for 2462*0b57cec5SDimitry Andric // parameter passing. We cannot use the stack at this point either 2463*0b57cec5SDimitry Andric // because __morestack manipulates the stack directly. 2464*0b57cec5SDimitry Andric // 2465*0b57cec5SDimitry Andric // To avoid these issues, perform an indirect call via a read-only memory 2466*0b57cec5SDimitry Andric // location containing the address. 2467*0b57cec5SDimitry Andric // 2468*0b57cec5SDimitry Andric // This solution is not perfect, as it assumes that the .rodata section 2469*0b57cec5SDimitry Andric // is laid out within 2^31 bytes of each function body, but this seems 2470*0b57cec5SDimitry Andric // to be sufficient for JIT. 2471*0b57cec5SDimitry Andric // FIXME: Add retpoline support and remove the error here.. 2472*0b57cec5SDimitry Andric if (STI.useRetpolineIndirectCalls()) 2473*0b57cec5SDimitry Andric report_fatal_error("Emitting morestack calls on 64-bit with the large " 2474*0b57cec5SDimitry Andric "code model and retpoline not yet implemented."); 2475*0b57cec5SDimitry Andric BuildMI(allocMBB, DL, TII.get(X86::CALL64m)) 2476*0b57cec5SDimitry Andric .addReg(X86::RIP) 2477*0b57cec5SDimitry Andric .addImm(0) 2478*0b57cec5SDimitry Andric .addReg(0) 2479*0b57cec5SDimitry Andric .addExternalSymbol("__morestack_addr") 2480*0b57cec5SDimitry Andric .addReg(0); 2481*0b57cec5SDimitry Andric MF.getMMI().setUsesMorestackAddr(true); 2482*0b57cec5SDimitry Andric } else { 2483*0b57cec5SDimitry Andric if (Is64Bit) 2484*0b57cec5SDimitry Andric BuildMI(allocMBB, DL, TII.get(X86::CALL64pcrel32)) 2485*0b57cec5SDimitry Andric .addExternalSymbol("__morestack"); 2486*0b57cec5SDimitry Andric else 2487*0b57cec5SDimitry Andric BuildMI(allocMBB, DL, TII.get(X86::CALLpcrel32)) 2488*0b57cec5SDimitry Andric .addExternalSymbol("__morestack"); 2489*0b57cec5SDimitry Andric } 2490*0b57cec5SDimitry Andric 2491*0b57cec5SDimitry Andric if (IsNested) 2492*0b57cec5SDimitry Andric BuildMI(allocMBB, DL, TII.get(X86::MORESTACK_RET_RESTORE_R10)); 2493*0b57cec5SDimitry Andric else 2494*0b57cec5SDimitry Andric BuildMI(allocMBB, DL, TII.get(X86::MORESTACK_RET)); 2495*0b57cec5SDimitry Andric 2496*0b57cec5SDimitry Andric allocMBB->addSuccessor(&PrologueMBB); 2497*0b57cec5SDimitry Andric 2498*0b57cec5SDimitry Andric checkMBB->addSuccessor(allocMBB, BranchProbability::getZero()); 2499*0b57cec5SDimitry Andric checkMBB->addSuccessor(&PrologueMBB, BranchProbability::getOne()); 2500*0b57cec5SDimitry Andric 2501*0b57cec5SDimitry Andric #ifdef EXPENSIVE_CHECKS 2502*0b57cec5SDimitry Andric MF.verify(); 2503*0b57cec5SDimitry Andric #endif 2504*0b57cec5SDimitry Andric } 2505*0b57cec5SDimitry Andric 2506*0b57cec5SDimitry Andric /// Lookup an ERTS parameter in the !hipe.literals named metadata node. 2507*0b57cec5SDimitry Andric /// HiPE provides Erlang Runtime System-internal parameters, such as PCB offsets 2508*0b57cec5SDimitry Andric /// to fields it needs, through a named metadata node "hipe.literals" containing 2509*0b57cec5SDimitry Andric /// name-value pairs. 2510*0b57cec5SDimitry Andric static unsigned getHiPELiteral( 2511*0b57cec5SDimitry Andric NamedMDNode *HiPELiteralsMD, const StringRef LiteralName) { 2512*0b57cec5SDimitry Andric for (int i = 0, e = HiPELiteralsMD->getNumOperands(); i != e; ++i) { 2513*0b57cec5SDimitry Andric MDNode *Node = HiPELiteralsMD->getOperand(i); 2514*0b57cec5SDimitry Andric if (Node->getNumOperands() != 2) continue; 2515*0b57cec5SDimitry Andric MDString *NodeName = dyn_cast<MDString>(Node->getOperand(0)); 2516*0b57cec5SDimitry Andric ValueAsMetadata *NodeVal = dyn_cast<ValueAsMetadata>(Node->getOperand(1)); 2517*0b57cec5SDimitry Andric if (!NodeName || !NodeVal) continue; 2518*0b57cec5SDimitry Andric ConstantInt *ValConst = dyn_cast_or_null<ConstantInt>(NodeVal->getValue()); 2519*0b57cec5SDimitry Andric if (ValConst && NodeName->getString() == LiteralName) { 2520*0b57cec5SDimitry Andric return ValConst->getZExtValue(); 2521*0b57cec5SDimitry Andric } 2522*0b57cec5SDimitry Andric } 2523*0b57cec5SDimitry Andric 2524*0b57cec5SDimitry Andric report_fatal_error("HiPE literal " + LiteralName 2525*0b57cec5SDimitry Andric + " required but not provided"); 2526*0b57cec5SDimitry Andric } 2527*0b57cec5SDimitry Andric 2528*0b57cec5SDimitry Andric /// Erlang programs may need a special prologue to handle the stack size they 2529*0b57cec5SDimitry Andric /// might need at runtime. That is because Erlang/OTP does not implement a C 2530*0b57cec5SDimitry Andric /// stack but uses a custom implementation of hybrid stack/heap architecture. 2531*0b57cec5SDimitry Andric /// (for more information see Eric Stenman's Ph.D. thesis: 2532*0b57cec5SDimitry Andric /// http://publications.uu.se/uu/fulltext/nbn_se_uu_diva-2688.pdf) 2533*0b57cec5SDimitry Andric /// 2534*0b57cec5SDimitry Andric /// CheckStack: 2535*0b57cec5SDimitry Andric /// temp0 = sp - MaxStack 2536*0b57cec5SDimitry Andric /// if( temp0 < SP_LIMIT(P) ) goto IncStack else goto OldStart 2537*0b57cec5SDimitry Andric /// OldStart: 2538*0b57cec5SDimitry Andric /// ... 2539*0b57cec5SDimitry Andric /// IncStack: 2540*0b57cec5SDimitry Andric /// call inc_stack # doubles the stack space 2541*0b57cec5SDimitry Andric /// temp0 = sp - MaxStack 2542*0b57cec5SDimitry Andric /// if( temp0 < SP_LIMIT(P) ) goto IncStack else goto OldStart 2543*0b57cec5SDimitry Andric void X86FrameLowering::adjustForHiPEPrologue( 2544*0b57cec5SDimitry Andric MachineFunction &MF, MachineBasicBlock &PrologueMBB) const { 2545*0b57cec5SDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 2546*0b57cec5SDimitry Andric DebugLoc DL; 2547*0b57cec5SDimitry Andric 2548*0b57cec5SDimitry Andric // To support shrink-wrapping we would need to insert the new blocks 2549*0b57cec5SDimitry Andric // at the right place and update the branches to PrologueMBB. 2550*0b57cec5SDimitry Andric assert(&(*MF.begin()) == &PrologueMBB && "Shrink-wrapping not supported yet"); 2551*0b57cec5SDimitry Andric 2552*0b57cec5SDimitry Andric // HiPE-specific values 2553*0b57cec5SDimitry Andric NamedMDNode *HiPELiteralsMD = MF.getMMI().getModule() 2554*0b57cec5SDimitry Andric ->getNamedMetadata("hipe.literals"); 2555*0b57cec5SDimitry Andric if (!HiPELiteralsMD) 2556*0b57cec5SDimitry Andric report_fatal_error( 2557*0b57cec5SDimitry Andric "Can't generate HiPE prologue without runtime parameters"); 2558*0b57cec5SDimitry Andric const unsigned HipeLeafWords 2559*0b57cec5SDimitry Andric = getHiPELiteral(HiPELiteralsMD, 2560*0b57cec5SDimitry Andric Is64Bit ? "AMD64_LEAF_WORDS" : "X86_LEAF_WORDS"); 2561*0b57cec5SDimitry Andric const unsigned CCRegisteredArgs = Is64Bit ? 6 : 5; 2562*0b57cec5SDimitry Andric const unsigned Guaranteed = HipeLeafWords * SlotSize; 2563*0b57cec5SDimitry Andric unsigned CallerStkArity = MF.getFunction().arg_size() > CCRegisteredArgs ? 2564*0b57cec5SDimitry Andric MF.getFunction().arg_size() - CCRegisteredArgs : 0; 2565*0b57cec5SDimitry Andric unsigned MaxStack = MFI.getStackSize() + CallerStkArity*SlotSize + SlotSize; 2566*0b57cec5SDimitry Andric 2567*0b57cec5SDimitry Andric assert(STI.isTargetLinux() && 2568*0b57cec5SDimitry Andric "HiPE prologue is only supported on Linux operating systems."); 2569*0b57cec5SDimitry Andric 2570*0b57cec5SDimitry Andric // Compute the largest caller's frame that is needed to fit the callees' 2571*0b57cec5SDimitry Andric // frames. This 'MaxStack' is computed from: 2572*0b57cec5SDimitry Andric // 2573*0b57cec5SDimitry Andric // a) the fixed frame size, which is the space needed for all spilled temps, 2574*0b57cec5SDimitry Andric // b) outgoing on-stack parameter areas, and 2575*0b57cec5SDimitry Andric // c) the minimum stack space this function needs to make available for the 2576*0b57cec5SDimitry Andric // functions it calls (a tunable ABI property). 2577*0b57cec5SDimitry Andric if (MFI.hasCalls()) { 2578*0b57cec5SDimitry Andric unsigned MoreStackForCalls = 0; 2579*0b57cec5SDimitry Andric 2580*0b57cec5SDimitry Andric for (auto &MBB : MF) { 2581*0b57cec5SDimitry Andric for (auto &MI : MBB) { 2582*0b57cec5SDimitry Andric if (!MI.isCall()) 2583*0b57cec5SDimitry Andric continue; 2584*0b57cec5SDimitry Andric 2585*0b57cec5SDimitry Andric // Get callee operand. 2586*0b57cec5SDimitry Andric const MachineOperand &MO = MI.getOperand(0); 2587*0b57cec5SDimitry Andric 2588*0b57cec5SDimitry Andric // Only take account of global function calls (no closures etc.). 2589*0b57cec5SDimitry Andric if (!MO.isGlobal()) 2590*0b57cec5SDimitry Andric continue; 2591*0b57cec5SDimitry Andric 2592*0b57cec5SDimitry Andric const Function *F = dyn_cast<Function>(MO.getGlobal()); 2593*0b57cec5SDimitry Andric if (!F) 2594*0b57cec5SDimitry Andric continue; 2595*0b57cec5SDimitry Andric 2596*0b57cec5SDimitry Andric // Do not update 'MaxStack' for primitive and built-in functions 2597*0b57cec5SDimitry Andric // (encoded with names either starting with "erlang."/"bif_" or not 2598*0b57cec5SDimitry Andric // having a ".", such as a simple <Module>.<Function>.<Arity>, or an 2599*0b57cec5SDimitry Andric // "_", such as the BIF "suspend_0") as they are executed on another 2600*0b57cec5SDimitry Andric // stack. 2601*0b57cec5SDimitry Andric if (F->getName().find("erlang.") != StringRef::npos || 2602*0b57cec5SDimitry Andric F->getName().find("bif_") != StringRef::npos || 2603*0b57cec5SDimitry Andric F->getName().find_first_of("._") == StringRef::npos) 2604*0b57cec5SDimitry Andric continue; 2605*0b57cec5SDimitry Andric 2606*0b57cec5SDimitry Andric unsigned CalleeStkArity = 2607*0b57cec5SDimitry Andric F->arg_size() > CCRegisteredArgs ? F->arg_size()-CCRegisteredArgs : 0; 2608*0b57cec5SDimitry Andric if (HipeLeafWords - 1 > CalleeStkArity) 2609*0b57cec5SDimitry Andric MoreStackForCalls = std::max(MoreStackForCalls, 2610*0b57cec5SDimitry Andric (HipeLeafWords - 1 - CalleeStkArity) * SlotSize); 2611*0b57cec5SDimitry Andric } 2612*0b57cec5SDimitry Andric } 2613*0b57cec5SDimitry Andric MaxStack += MoreStackForCalls; 2614*0b57cec5SDimitry Andric } 2615*0b57cec5SDimitry Andric 2616*0b57cec5SDimitry Andric // If the stack frame needed is larger than the guaranteed then runtime checks 2617*0b57cec5SDimitry Andric // and calls to "inc_stack_0" BIF should be inserted in the assembly prologue. 2618*0b57cec5SDimitry Andric if (MaxStack > Guaranteed) { 2619*0b57cec5SDimitry Andric MachineBasicBlock *stackCheckMBB = MF.CreateMachineBasicBlock(); 2620*0b57cec5SDimitry Andric MachineBasicBlock *incStackMBB = MF.CreateMachineBasicBlock(); 2621*0b57cec5SDimitry Andric 2622*0b57cec5SDimitry Andric for (const auto &LI : PrologueMBB.liveins()) { 2623*0b57cec5SDimitry Andric stackCheckMBB->addLiveIn(LI); 2624*0b57cec5SDimitry Andric incStackMBB->addLiveIn(LI); 2625*0b57cec5SDimitry Andric } 2626*0b57cec5SDimitry Andric 2627*0b57cec5SDimitry Andric MF.push_front(incStackMBB); 2628*0b57cec5SDimitry Andric MF.push_front(stackCheckMBB); 2629*0b57cec5SDimitry Andric 2630*0b57cec5SDimitry Andric unsigned ScratchReg, SPReg, PReg, SPLimitOffset; 2631*0b57cec5SDimitry Andric unsigned LEAop, CMPop, CALLop; 2632*0b57cec5SDimitry Andric SPLimitOffset = getHiPELiteral(HiPELiteralsMD, "P_NSP_LIMIT"); 2633*0b57cec5SDimitry Andric if (Is64Bit) { 2634*0b57cec5SDimitry Andric SPReg = X86::RSP; 2635*0b57cec5SDimitry Andric PReg = X86::RBP; 2636*0b57cec5SDimitry Andric LEAop = X86::LEA64r; 2637*0b57cec5SDimitry Andric CMPop = X86::CMP64rm; 2638*0b57cec5SDimitry Andric CALLop = X86::CALL64pcrel32; 2639*0b57cec5SDimitry Andric } else { 2640*0b57cec5SDimitry Andric SPReg = X86::ESP; 2641*0b57cec5SDimitry Andric PReg = X86::EBP; 2642*0b57cec5SDimitry Andric LEAop = X86::LEA32r; 2643*0b57cec5SDimitry Andric CMPop = X86::CMP32rm; 2644*0b57cec5SDimitry Andric CALLop = X86::CALLpcrel32; 2645*0b57cec5SDimitry Andric } 2646*0b57cec5SDimitry Andric 2647*0b57cec5SDimitry Andric ScratchReg = GetScratchRegister(Is64Bit, IsLP64, MF, true); 2648*0b57cec5SDimitry Andric assert(!MF.getRegInfo().isLiveIn(ScratchReg) && 2649*0b57cec5SDimitry Andric "HiPE prologue scratch register is live-in"); 2650*0b57cec5SDimitry Andric 2651*0b57cec5SDimitry Andric // Create new MBB for StackCheck: 2652*0b57cec5SDimitry Andric addRegOffset(BuildMI(stackCheckMBB, DL, TII.get(LEAop), ScratchReg), 2653*0b57cec5SDimitry Andric SPReg, false, -MaxStack); 2654*0b57cec5SDimitry Andric // SPLimitOffset is in a fixed heap location (pointed by BP). 2655*0b57cec5SDimitry Andric addRegOffset(BuildMI(stackCheckMBB, DL, TII.get(CMPop)) 2656*0b57cec5SDimitry Andric .addReg(ScratchReg), PReg, false, SPLimitOffset); 2657*0b57cec5SDimitry Andric BuildMI(stackCheckMBB, DL, TII.get(X86::JCC_1)).addMBB(&PrologueMBB).addImm(X86::COND_AE); 2658*0b57cec5SDimitry Andric 2659*0b57cec5SDimitry Andric // Create new MBB for IncStack: 2660*0b57cec5SDimitry Andric BuildMI(incStackMBB, DL, TII.get(CALLop)). 2661*0b57cec5SDimitry Andric addExternalSymbol("inc_stack_0"); 2662*0b57cec5SDimitry Andric addRegOffset(BuildMI(incStackMBB, DL, TII.get(LEAop), ScratchReg), 2663*0b57cec5SDimitry Andric SPReg, false, -MaxStack); 2664*0b57cec5SDimitry Andric addRegOffset(BuildMI(incStackMBB, DL, TII.get(CMPop)) 2665*0b57cec5SDimitry Andric .addReg(ScratchReg), PReg, false, SPLimitOffset); 2666*0b57cec5SDimitry Andric BuildMI(incStackMBB, DL, TII.get(X86::JCC_1)).addMBB(incStackMBB).addImm(X86::COND_LE); 2667*0b57cec5SDimitry Andric 2668*0b57cec5SDimitry Andric stackCheckMBB->addSuccessor(&PrologueMBB, {99, 100}); 2669*0b57cec5SDimitry Andric stackCheckMBB->addSuccessor(incStackMBB, {1, 100}); 2670*0b57cec5SDimitry Andric incStackMBB->addSuccessor(&PrologueMBB, {99, 100}); 2671*0b57cec5SDimitry Andric incStackMBB->addSuccessor(incStackMBB, {1, 100}); 2672*0b57cec5SDimitry Andric } 2673*0b57cec5SDimitry Andric #ifdef EXPENSIVE_CHECKS 2674*0b57cec5SDimitry Andric MF.verify(); 2675*0b57cec5SDimitry Andric #endif 2676*0b57cec5SDimitry Andric } 2677*0b57cec5SDimitry Andric 2678*0b57cec5SDimitry Andric bool X86FrameLowering::adjustStackWithPops(MachineBasicBlock &MBB, 2679*0b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI, 2680*0b57cec5SDimitry Andric const DebugLoc &DL, 2681*0b57cec5SDimitry Andric int Offset) const { 2682*0b57cec5SDimitry Andric 2683*0b57cec5SDimitry Andric if (Offset <= 0) 2684*0b57cec5SDimitry Andric return false; 2685*0b57cec5SDimitry Andric 2686*0b57cec5SDimitry Andric if (Offset % SlotSize) 2687*0b57cec5SDimitry Andric return false; 2688*0b57cec5SDimitry Andric 2689*0b57cec5SDimitry Andric int NumPops = Offset / SlotSize; 2690*0b57cec5SDimitry Andric // This is only worth it if we have at most 2 pops. 2691*0b57cec5SDimitry Andric if (NumPops != 1 && NumPops != 2) 2692*0b57cec5SDimitry Andric return false; 2693*0b57cec5SDimitry Andric 2694*0b57cec5SDimitry Andric // Handle only the trivial case where the adjustment directly follows 2695*0b57cec5SDimitry Andric // a call. This is the most common one, anyway. 2696*0b57cec5SDimitry Andric if (MBBI == MBB.begin()) 2697*0b57cec5SDimitry Andric return false; 2698*0b57cec5SDimitry Andric MachineBasicBlock::iterator Prev = std::prev(MBBI); 2699*0b57cec5SDimitry Andric if (!Prev->isCall() || !Prev->getOperand(1).isRegMask()) 2700*0b57cec5SDimitry Andric return false; 2701*0b57cec5SDimitry Andric 2702*0b57cec5SDimitry Andric unsigned Regs[2]; 2703*0b57cec5SDimitry Andric unsigned FoundRegs = 0; 2704*0b57cec5SDimitry Andric 2705*0b57cec5SDimitry Andric auto &MRI = MBB.getParent()->getRegInfo(); 2706*0b57cec5SDimitry Andric auto RegMask = Prev->getOperand(1); 2707*0b57cec5SDimitry Andric 2708*0b57cec5SDimitry Andric auto &RegClass = 2709*0b57cec5SDimitry Andric Is64Bit ? X86::GR64_NOREX_NOSPRegClass : X86::GR32_NOREX_NOSPRegClass; 2710*0b57cec5SDimitry Andric // Try to find up to NumPops free registers. 2711*0b57cec5SDimitry Andric for (auto Candidate : RegClass) { 2712*0b57cec5SDimitry Andric 2713*0b57cec5SDimitry Andric // Poor man's liveness: 2714*0b57cec5SDimitry Andric // Since we're immediately after a call, any register that is clobbered 2715*0b57cec5SDimitry Andric // by the call and not defined by it can be considered dead. 2716*0b57cec5SDimitry Andric if (!RegMask.clobbersPhysReg(Candidate)) 2717*0b57cec5SDimitry Andric continue; 2718*0b57cec5SDimitry Andric 2719*0b57cec5SDimitry Andric // Don't clobber reserved registers 2720*0b57cec5SDimitry Andric if (MRI.isReserved(Candidate)) 2721*0b57cec5SDimitry Andric continue; 2722*0b57cec5SDimitry Andric 2723*0b57cec5SDimitry Andric bool IsDef = false; 2724*0b57cec5SDimitry Andric for (const MachineOperand &MO : Prev->implicit_operands()) { 2725*0b57cec5SDimitry Andric if (MO.isReg() && MO.isDef() && 2726*0b57cec5SDimitry Andric TRI->isSuperOrSubRegisterEq(MO.getReg(), Candidate)) { 2727*0b57cec5SDimitry Andric IsDef = true; 2728*0b57cec5SDimitry Andric break; 2729*0b57cec5SDimitry Andric } 2730*0b57cec5SDimitry Andric } 2731*0b57cec5SDimitry Andric 2732*0b57cec5SDimitry Andric if (IsDef) 2733*0b57cec5SDimitry Andric continue; 2734*0b57cec5SDimitry Andric 2735*0b57cec5SDimitry Andric Regs[FoundRegs++] = Candidate; 2736*0b57cec5SDimitry Andric if (FoundRegs == (unsigned)NumPops) 2737*0b57cec5SDimitry Andric break; 2738*0b57cec5SDimitry Andric } 2739*0b57cec5SDimitry Andric 2740*0b57cec5SDimitry Andric if (FoundRegs == 0) 2741*0b57cec5SDimitry Andric return false; 2742*0b57cec5SDimitry Andric 2743*0b57cec5SDimitry Andric // If we found only one free register, but need two, reuse the same one twice. 2744*0b57cec5SDimitry Andric while (FoundRegs < (unsigned)NumPops) 2745*0b57cec5SDimitry Andric Regs[FoundRegs++] = Regs[0]; 2746*0b57cec5SDimitry Andric 2747*0b57cec5SDimitry Andric for (int i = 0; i < NumPops; ++i) 2748*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, 2749*0b57cec5SDimitry Andric TII.get(STI.is64Bit() ? X86::POP64r : X86::POP32r), Regs[i]); 2750*0b57cec5SDimitry Andric 2751*0b57cec5SDimitry Andric return true; 2752*0b57cec5SDimitry Andric } 2753*0b57cec5SDimitry Andric 2754*0b57cec5SDimitry Andric MachineBasicBlock::iterator X86FrameLowering:: 2755*0b57cec5SDimitry Andric eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, 2756*0b57cec5SDimitry Andric MachineBasicBlock::iterator I) const { 2757*0b57cec5SDimitry Andric bool reserveCallFrame = hasReservedCallFrame(MF); 2758*0b57cec5SDimitry Andric unsigned Opcode = I->getOpcode(); 2759*0b57cec5SDimitry Andric bool isDestroy = Opcode == TII.getCallFrameDestroyOpcode(); 2760*0b57cec5SDimitry Andric DebugLoc DL = I->getDebugLoc(); 2761*0b57cec5SDimitry Andric uint64_t Amount = !reserveCallFrame ? TII.getFrameSize(*I) : 0; 2762*0b57cec5SDimitry Andric uint64_t InternalAmt = (isDestroy || Amount) ? TII.getFrameAdjustment(*I) : 0; 2763*0b57cec5SDimitry Andric I = MBB.erase(I); 2764*0b57cec5SDimitry Andric auto InsertPos = skipDebugInstructionsForward(I, MBB.end()); 2765*0b57cec5SDimitry Andric 2766*0b57cec5SDimitry Andric if (!reserveCallFrame) { 2767*0b57cec5SDimitry Andric // If the stack pointer can be changed after prologue, turn the 2768*0b57cec5SDimitry Andric // adjcallstackup instruction into a 'sub ESP, <amt>' and the 2769*0b57cec5SDimitry Andric // adjcallstackdown instruction into 'add ESP, <amt>' 2770*0b57cec5SDimitry Andric 2771*0b57cec5SDimitry Andric // We need to keep the stack aligned properly. To do this, we round the 2772*0b57cec5SDimitry Andric // amount of space needed for the outgoing arguments up to the next 2773*0b57cec5SDimitry Andric // alignment boundary. 2774*0b57cec5SDimitry Andric unsigned StackAlign = getStackAlignment(); 2775*0b57cec5SDimitry Andric Amount = alignTo(Amount, StackAlign); 2776*0b57cec5SDimitry Andric 2777*0b57cec5SDimitry Andric MachineModuleInfo &MMI = MF.getMMI(); 2778*0b57cec5SDimitry Andric const Function &F = MF.getFunction(); 2779*0b57cec5SDimitry Andric bool WindowsCFI = MF.getTarget().getMCAsmInfo()->usesWindowsCFI(); 2780*0b57cec5SDimitry Andric bool DwarfCFI = !WindowsCFI && 2781*0b57cec5SDimitry Andric (MMI.hasDebugInfo() || F.needsUnwindTableEntry()); 2782*0b57cec5SDimitry Andric 2783*0b57cec5SDimitry Andric // If we have any exception handlers in this function, and we adjust 2784*0b57cec5SDimitry Andric // the SP before calls, we may need to indicate this to the unwinder 2785*0b57cec5SDimitry Andric // using GNU_ARGS_SIZE. Note that this may be necessary even when 2786*0b57cec5SDimitry Andric // Amount == 0, because the preceding function may have set a non-0 2787*0b57cec5SDimitry Andric // GNU_ARGS_SIZE. 2788*0b57cec5SDimitry Andric // TODO: We don't need to reset this between subsequent functions, 2789*0b57cec5SDimitry Andric // if it didn't change. 2790*0b57cec5SDimitry Andric bool HasDwarfEHHandlers = !WindowsCFI && !MF.getLandingPads().empty(); 2791*0b57cec5SDimitry Andric 2792*0b57cec5SDimitry Andric if (HasDwarfEHHandlers && !isDestroy && 2793*0b57cec5SDimitry Andric MF.getInfo<X86MachineFunctionInfo>()->getHasPushSequences()) 2794*0b57cec5SDimitry Andric BuildCFI(MBB, InsertPos, DL, 2795*0b57cec5SDimitry Andric MCCFIInstruction::createGnuArgsSize(nullptr, Amount)); 2796*0b57cec5SDimitry Andric 2797*0b57cec5SDimitry Andric if (Amount == 0) 2798*0b57cec5SDimitry Andric return I; 2799*0b57cec5SDimitry Andric 2800*0b57cec5SDimitry Andric // Factor out the amount that gets handled inside the sequence 2801*0b57cec5SDimitry Andric // (Pushes of argument for frame setup, callee pops for frame destroy) 2802*0b57cec5SDimitry Andric Amount -= InternalAmt; 2803*0b57cec5SDimitry Andric 2804*0b57cec5SDimitry Andric // TODO: This is needed only if we require precise CFA. 2805*0b57cec5SDimitry Andric // If this is a callee-pop calling convention, emit a CFA adjust for 2806*0b57cec5SDimitry Andric // the amount the callee popped. 2807*0b57cec5SDimitry Andric if (isDestroy && InternalAmt && DwarfCFI && !hasFP(MF)) 2808*0b57cec5SDimitry Andric BuildCFI(MBB, InsertPos, DL, 2809*0b57cec5SDimitry Andric MCCFIInstruction::createAdjustCfaOffset(nullptr, -InternalAmt)); 2810*0b57cec5SDimitry Andric 2811*0b57cec5SDimitry Andric // Add Amount to SP to destroy a frame, or subtract to setup. 2812*0b57cec5SDimitry Andric int64_t StackAdjustment = isDestroy ? Amount : -Amount; 2813*0b57cec5SDimitry Andric 2814*0b57cec5SDimitry Andric if (StackAdjustment) { 2815*0b57cec5SDimitry Andric // Merge with any previous or following adjustment instruction. Note: the 2816*0b57cec5SDimitry Andric // instructions merged with here do not have CFI, so their stack 2817*0b57cec5SDimitry Andric // adjustments do not feed into CfaAdjustment. 2818*0b57cec5SDimitry Andric StackAdjustment += mergeSPUpdates(MBB, InsertPos, true); 2819*0b57cec5SDimitry Andric StackAdjustment += mergeSPUpdates(MBB, InsertPos, false); 2820*0b57cec5SDimitry Andric 2821*0b57cec5SDimitry Andric if (StackAdjustment) { 2822*0b57cec5SDimitry Andric if (!(F.hasMinSize() && 2823*0b57cec5SDimitry Andric adjustStackWithPops(MBB, InsertPos, DL, StackAdjustment))) 2824*0b57cec5SDimitry Andric BuildStackAdjustment(MBB, InsertPos, DL, StackAdjustment, 2825*0b57cec5SDimitry Andric /*InEpilogue=*/false); 2826*0b57cec5SDimitry Andric } 2827*0b57cec5SDimitry Andric } 2828*0b57cec5SDimitry Andric 2829*0b57cec5SDimitry Andric if (DwarfCFI && !hasFP(MF)) { 2830*0b57cec5SDimitry Andric // If we don't have FP, but need to generate unwind information, 2831*0b57cec5SDimitry Andric // we need to set the correct CFA offset after the stack adjustment. 2832*0b57cec5SDimitry Andric // How much we adjust the CFA offset depends on whether we're emitting 2833*0b57cec5SDimitry Andric // CFI only for EH purposes or for debugging. EH only requires the CFA 2834*0b57cec5SDimitry Andric // offset to be correct at each call site, while for debugging we want 2835*0b57cec5SDimitry Andric // it to be more precise. 2836*0b57cec5SDimitry Andric 2837*0b57cec5SDimitry Andric int64_t CfaAdjustment = -StackAdjustment; 2838*0b57cec5SDimitry Andric // TODO: When not using precise CFA, we also need to adjust for the 2839*0b57cec5SDimitry Andric // InternalAmt here. 2840*0b57cec5SDimitry Andric if (CfaAdjustment) { 2841*0b57cec5SDimitry Andric BuildCFI(MBB, InsertPos, DL, 2842*0b57cec5SDimitry Andric MCCFIInstruction::createAdjustCfaOffset(nullptr, 2843*0b57cec5SDimitry Andric CfaAdjustment)); 2844*0b57cec5SDimitry Andric } 2845*0b57cec5SDimitry Andric } 2846*0b57cec5SDimitry Andric 2847*0b57cec5SDimitry Andric return I; 2848*0b57cec5SDimitry Andric } 2849*0b57cec5SDimitry Andric 2850*0b57cec5SDimitry Andric if (isDestroy && InternalAmt) { 2851*0b57cec5SDimitry Andric // If we are performing frame pointer elimination and if the callee pops 2852*0b57cec5SDimitry Andric // something off the stack pointer, add it back. We do this until we have 2853*0b57cec5SDimitry Andric // more advanced stack pointer tracking ability. 2854*0b57cec5SDimitry Andric // We are not tracking the stack pointer adjustment by the callee, so make 2855*0b57cec5SDimitry Andric // sure we restore the stack pointer immediately after the call, there may 2856*0b57cec5SDimitry Andric // be spill code inserted between the CALL and ADJCALLSTACKUP instructions. 2857*0b57cec5SDimitry Andric MachineBasicBlock::iterator CI = I; 2858*0b57cec5SDimitry Andric MachineBasicBlock::iterator B = MBB.begin(); 2859*0b57cec5SDimitry Andric while (CI != B && !std::prev(CI)->isCall()) 2860*0b57cec5SDimitry Andric --CI; 2861*0b57cec5SDimitry Andric BuildStackAdjustment(MBB, CI, DL, -InternalAmt, /*InEpilogue=*/false); 2862*0b57cec5SDimitry Andric } 2863*0b57cec5SDimitry Andric 2864*0b57cec5SDimitry Andric return I; 2865*0b57cec5SDimitry Andric } 2866*0b57cec5SDimitry Andric 2867*0b57cec5SDimitry Andric bool X86FrameLowering::canUseAsPrologue(const MachineBasicBlock &MBB) const { 2868*0b57cec5SDimitry Andric assert(MBB.getParent() && "Block is not attached to a function!"); 2869*0b57cec5SDimitry Andric const MachineFunction &MF = *MBB.getParent(); 2870*0b57cec5SDimitry Andric return !TRI->needsStackRealignment(MF) || !MBB.isLiveIn(X86::EFLAGS); 2871*0b57cec5SDimitry Andric } 2872*0b57cec5SDimitry Andric 2873*0b57cec5SDimitry Andric bool X86FrameLowering::canUseAsEpilogue(const MachineBasicBlock &MBB) const { 2874*0b57cec5SDimitry Andric assert(MBB.getParent() && "Block is not attached to a function!"); 2875*0b57cec5SDimitry Andric 2876*0b57cec5SDimitry Andric // Win64 has strict requirements in terms of epilogue and we are 2877*0b57cec5SDimitry Andric // not taking a chance at messing with them. 2878*0b57cec5SDimitry Andric // I.e., unless this block is already an exit block, we can't use 2879*0b57cec5SDimitry Andric // it as an epilogue. 2880*0b57cec5SDimitry Andric if (STI.isTargetWin64() && !MBB.succ_empty() && !MBB.isReturnBlock()) 2881*0b57cec5SDimitry Andric return false; 2882*0b57cec5SDimitry Andric 2883*0b57cec5SDimitry Andric if (canUseLEAForSPInEpilogue(*MBB.getParent())) 2884*0b57cec5SDimitry Andric return true; 2885*0b57cec5SDimitry Andric 2886*0b57cec5SDimitry Andric // If we cannot use LEA to adjust SP, we may need to use ADD, which 2887*0b57cec5SDimitry Andric // clobbers the EFLAGS. Check that we do not need to preserve it, 2888*0b57cec5SDimitry Andric // otherwise, conservatively assume this is not 2889*0b57cec5SDimitry Andric // safe to insert the epilogue here. 2890*0b57cec5SDimitry Andric return !flagsNeedToBePreservedBeforeTheTerminators(MBB); 2891*0b57cec5SDimitry Andric } 2892*0b57cec5SDimitry Andric 2893*0b57cec5SDimitry Andric bool X86FrameLowering::enableShrinkWrapping(const MachineFunction &MF) const { 2894*0b57cec5SDimitry Andric // If we may need to emit frameless compact unwind information, give 2895*0b57cec5SDimitry Andric // up as this is currently broken: PR25614. 2896*0b57cec5SDimitry Andric return (MF.getFunction().hasFnAttribute(Attribute::NoUnwind) || hasFP(MF)) && 2897*0b57cec5SDimitry Andric // The lowering of segmented stack and HiPE only support entry blocks 2898*0b57cec5SDimitry Andric // as prologue blocks: PR26107. 2899*0b57cec5SDimitry Andric // This limitation may be lifted if we fix: 2900*0b57cec5SDimitry Andric // - adjustForSegmentedStacks 2901*0b57cec5SDimitry Andric // - adjustForHiPEPrologue 2902*0b57cec5SDimitry Andric MF.getFunction().getCallingConv() != CallingConv::HiPE && 2903*0b57cec5SDimitry Andric !MF.shouldSplitStack(); 2904*0b57cec5SDimitry Andric } 2905*0b57cec5SDimitry Andric 2906*0b57cec5SDimitry Andric MachineBasicBlock::iterator X86FrameLowering::restoreWin32EHStackPointers( 2907*0b57cec5SDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, 2908*0b57cec5SDimitry Andric const DebugLoc &DL, bool RestoreSP) const { 2909*0b57cec5SDimitry Andric assert(STI.isTargetWindowsMSVC() && "funclets only supported in MSVC env"); 2910*0b57cec5SDimitry Andric assert(STI.isTargetWin32() && "EBP/ESI restoration only required on win32"); 2911*0b57cec5SDimitry Andric assert(STI.is32Bit() && !Uses64BitFramePtr && 2912*0b57cec5SDimitry Andric "restoring EBP/ESI on non-32-bit target"); 2913*0b57cec5SDimitry Andric 2914*0b57cec5SDimitry Andric MachineFunction &MF = *MBB.getParent(); 2915*0b57cec5SDimitry Andric unsigned FramePtr = TRI->getFrameRegister(MF); 2916*0b57cec5SDimitry Andric unsigned BasePtr = TRI->getBaseRegister(); 2917*0b57cec5SDimitry Andric WinEHFuncInfo &FuncInfo = *MF.getWinEHFuncInfo(); 2918*0b57cec5SDimitry Andric X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>(); 2919*0b57cec5SDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 2920*0b57cec5SDimitry Andric 2921*0b57cec5SDimitry Andric // FIXME: Don't set FrameSetup flag in catchret case. 2922*0b57cec5SDimitry Andric 2923*0b57cec5SDimitry Andric int FI = FuncInfo.EHRegNodeFrameIndex; 2924*0b57cec5SDimitry Andric int EHRegSize = MFI.getObjectSize(FI); 2925*0b57cec5SDimitry Andric 2926*0b57cec5SDimitry Andric if (RestoreSP) { 2927*0b57cec5SDimitry Andric // MOV32rm -EHRegSize(%ebp), %esp 2928*0b57cec5SDimitry Andric addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32rm), X86::ESP), 2929*0b57cec5SDimitry Andric X86::EBP, true, -EHRegSize) 2930*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 2931*0b57cec5SDimitry Andric } 2932*0b57cec5SDimitry Andric 2933*0b57cec5SDimitry Andric unsigned UsedReg; 2934*0b57cec5SDimitry Andric int EHRegOffset = getFrameIndexReference(MF, FI, UsedReg); 2935*0b57cec5SDimitry Andric int EndOffset = -EHRegOffset - EHRegSize; 2936*0b57cec5SDimitry Andric FuncInfo.EHRegNodeEndOffset = EndOffset; 2937*0b57cec5SDimitry Andric 2938*0b57cec5SDimitry Andric if (UsedReg == FramePtr) { 2939*0b57cec5SDimitry Andric // ADD $offset, %ebp 2940*0b57cec5SDimitry Andric unsigned ADDri = getADDriOpcode(false, EndOffset); 2941*0b57cec5SDimitry Andric BuildMI(MBB, MBBI, DL, TII.get(ADDri), FramePtr) 2942*0b57cec5SDimitry Andric .addReg(FramePtr) 2943*0b57cec5SDimitry Andric .addImm(EndOffset) 2944*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup) 2945*0b57cec5SDimitry Andric ->getOperand(3) 2946*0b57cec5SDimitry Andric .setIsDead(); 2947*0b57cec5SDimitry Andric assert(EndOffset >= 0 && 2948*0b57cec5SDimitry Andric "end of registration object above normal EBP position!"); 2949*0b57cec5SDimitry Andric } else if (UsedReg == BasePtr) { 2950*0b57cec5SDimitry Andric // LEA offset(%ebp), %esi 2951*0b57cec5SDimitry Andric addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::LEA32r), BasePtr), 2952*0b57cec5SDimitry Andric FramePtr, false, EndOffset) 2953*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 2954*0b57cec5SDimitry Andric // MOV32rm SavedEBPOffset(%esi), %ebp 2955*0b57cec5SDimitry Andric assert(X86FI->getHasSEHFramePtrSave()); 2956*0b57cec5SDimitry Andric int Offset = 2957*0b57cec5SDimitry Andric getFrameIndexReference(MF, X86FI->getSEHFramePtrSaveIndex(), UsedReg); 2958*0b57cec5SDimitry Andric assert(UsedReg == BasePtr); 2959*0b57cec5SDimitry Andric addRegOffset(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV32rm), FramePtr), 2960*0b57cec5SDimitry Andric UsedReg, true, Offset) 2961*0b57cec5SDimitry Andric .setMIFlag(MachineInstr::FrameSetup); 2962*0b57cec5SDimitry Andric } else { 2963*0b57cec5SDimitry Andric llvm_unreachable("32-bit frames with WinEH must use FramePtr or BasePtr"); 2964*0b57cec5SDimitry Andric } 2965*0b57cec5SDimitry Andric return MBBI; 2966*0b57cec5SDimitry Andric } 2967*0b57cec5SDimitry Andric 2968*0b57cec5SDimitry Andric int X86FrameLowering::getInitialCFAOffset(const MachineFunction &MF) const { 2969*0b57cec5SDimitry Andric return TRI->getSlotSize(); 2970*0b57cec5SDimitry Andric } 2971*0b57cec5SDimitry Andric 2972*0b57cec5SDimitry Andric unsigned X86FrameLowering::getInitialCFARegister(const MachineFunction &MF) 2973*0b57cec5SDimitry Andric const { 2974*0b57cec5SDimitry Andric return TRI->getDwarfRegNum(StackPtr, true); 2975*0b57cec5SDimitry Andric } 2976*0b57cec5SDimitry Andric 2977*0b57cec5SDimitry Andric namespace { 2978*0b57cec5SDimitry Andric // Struct used by orderFrameObjects to help sort the stack objects. 2979*0b57cec5SDimitry Andric struct X86FrameSortingObject { 2980*0b57cec5SDimitry Andric bool IsValid = false; // true if we care about this Object. 2981*0b57cec5SDimitry Andric unsigned ObjectIndex = 0; // Index of Object into MFI list. 2982*0b57cec5SDimitry Andric unsigned ObjectSize = 0; // Size of Object in bytes. 2983*0b57cec5SDimitry Andric unsigned ObjectAlignment = 1; // Alignment of Object in bytes. 2984*0b57cec5SDimitry Andric unsigned ObjectNumUses = 0; // Object static number of uses. 2985*0b57cec5SDimitry Andric }; 2986*0b57cec5SDimitry Andric 2987*0b57cec5SDimitry Andric // The comparison function we use for std::sort to order our local 2988*0b57cec5SDimitry Andric // stack symbols. The current algorithm is to use an estimated 2989*0b57cec5SDimitry Andric // "density". This takes into consideration the size and number of 2990*0b57cec5SDimitry Andric // uses each object has in order to roughly minimize code size. 2991*0b57cec5SDimitry Andric // So, for example, an object of size 16B that is referenced 5 times 2992*0b57cec5SDimitry Andric // will get higher priority than 4 4B objects referenced 1 time each. 2993*0b57cec5SDimitry Andric // It's not perfect and we may be able to squeeze a few more bytes out of 2994*0b57cec5SDimitry Andric // it (for example : 0(esp) requires fewer bytes, symbols allocated at the 2995*0b57cec5SDimitry Andric // fringe end can have special consideration, given their size is less 2996*0b57cec5SDimitry Andric // important, etc.), but the algorithmic complexity grows too much to be 2997*0b57cec5SDimitry Andric // worth the extra gains we get. This gets us pretty close. 2998*0b57cec5SDimitry Andric // The final order leaves us with objects with highest priority going 2999*0b57cec5SDimitry Andric // at the end of our list. 3000*0b57cec5SDimitry Andric struct X86FrameSortingComparator { 3001*0b57cec5SDimitry Andric inline bool operator()(const X86FrameSortingObject &A, 3002*0b57cec5SDimitry Andric const X86FrameSortingObject &B) { 3003*0b57cec5SDimitry Andric uint64_t DensityAScaled, DensityBScaled; 3004*0b57cec5SDimitry Andric 3005*0b57cec5SDimitry Andric // For consistency in our comparison, all invalid objects are placed 3006*0b57cec5SDimitry Andric // at the end. This also allows us to stop walking when we hit the 3007*0b57cec5SDimitry Andric // first invalid item after it's all sorted. 3008*0b57cec5SDimitry Andric if (!A.IsValid) 3009*0b57cec5SDimitry Andric return false; 3010*0b57cec5SDimitry Andric if (!B.IsValid) 3011*0b57cec5SDimitry Andric return true; 3012*0b57cec5SDimitry Andric 3013*0b57cec5SDimitry Andric // The density is calculated by doing : 3014*0b57cec5SDimitry Andric // (double)DensityA = A.ObjectNumUses / A.ObjectSize 3015*0b57cec5SDimitry Andric // (double)DensityB = B.ObjectNumUses / B.ObjectSize 3016*0b57cec5SDimitry Andric // Since this approach may cause inconsistencies in 3017*0b57cec5SDimitry Andric // the floating point <, >, == comparisons, depending on the floating 3018*0b57cec5SDimitry Andric // point model with which the compiler was built, we're going 3019*0b57cec5SDimitry Andric // to scale both sides by multiplying with 3020*0b57cec5SDimitry Andric // A.ObjectSize * B.ObjectSize. This ends up factoring away 3021*0b57cec5SDimitry Andric // the division and, with it, the need for any floating point 3022*0b57cec5SDimitry Andric // arithmetic. 3023*0b57cec5SDimitry Andric DensityAScaled = static_cast<uint64_t>(A.ObjectNumUses) * 3024*0b57cec5SDimitry Andric static_cast<uint64_t>(B.ObjectSize); 3025*0b57cec5SDimitry Andric DensityBScaled = static_cast<uint64_t>(B.ObjectNumUses) * 3026*0b57cec5SDimitry Andric static_cast<uint64_t>(A.ObjectSize); 3027*0b57cec5SDimitry Andric 3028*0b57cec5SDimitry Andric // If the two densities are equal, prioritize highest alignment 3029*0b57cec5SDimitry Andric // objects. This allows for similar alignment objects 3030*0b57cec5SDimitry Andric // to be packed together (given the same density). 3031*0b57cec5SDimitry Andric // There's room for improvement here, also, since we can pack 3032*0b57cec5SDimitry Andric // similar alignment (different density) objects next to each 3033*0b57cec5SDimitry Andric // other to save padding. This will also require further 3034*0b57cec5SDimitry Andric // complexity/iterations, and the overall gain isn't worth it, 3035*0b57cec5SDimitry Andric // in general. Something to keep in mind, though. 3036*0b57cec5SDimitry Andric if (DensityAScaled == DensityBScaled) 3037*0b57cec5SDimitry Andric return A.ObjectAlignment < B.ObjectAlignment; 3038*0b57cec5SDimitry Andric 3039*0b57cec5SDimitry Andric return DensityAScaled < DensityBScaled; 3040*0b57cec5SDimitry Andric } 3041*0b57cec5SDimitry Andric }; 3042*0b57cec5SDimitry Andric } // namespace 3043*0b57cec5SDimitry Andric 3044*0b57cec5SDimitry Andric // Order the symbols in the local stack. 3045*0b57cec5SDimitry Andric // We want to place the local stack objects in some sort of sensible order. 3046*0b57cec5SDimitry Andric // The heuristic we use is to try and pack them according to static number 3047*0b57cec5SDimitry Andric // of uses and size of object in order to minimize code size. 3048*0b57cec5SDimitry Andric void X86FrameLowering::orderFrameObjects( 3049*0b57cec5SDimitry Andric const MachineFunction &MF, SmallVectorImpl<int> &ObjectsToAllocate) const { 3050*0b57cec5SDimitry Andric const MachineFrameInfo &MFI = MF.getFrameInfo(); 3051*0b57cec5SDimitry Andric 3052*0b57cec5SDimitry Andric // Don't waste time if there's nothing to do. 3053*0b57cec5SDimitry Andric if (ObjectsToAllocate.empty()) 3054*0b57cec5SDimitry Andric return; 3055*0b57cec5SDimitry Andric 3056*0b57cec5SDimitry Andric // Create an array of all MFI objects. We won't need all of these 3057*0b57cec5SDimitry Andric // objects, but we're going to create a full array of them to make 3058*0b57cec5SDimitry Andric // it easier to index into when we're counting "uses" down below. 3059*0b57cec5SDimitry Andric // We want to be able to easily/cheaply access an object by simply 3060*0b57cec5SDimitry Andric // indexing into it, instead of having to search for it every time. 3061*0b57cec5SDimitry Andric std::vector<X86FrameSortingObject> SortingObjects(MFI.getObjectIndexEnd()); 3062*0b57cec5SDimitry Andric 3063*0b57cec5SDimitry Andric // Walk the objects we care about and mark them as such in our working 3064*0b57cec5SDimitry Andric // struct. 3065*0b57cec5SDimitry Andric for (auto &Obj : ObjectsToAllocate) { 3066*0b57cec5SDimitry Andric SortingObjects[Obj].IsValid = true; 3067*0b57cec5SDimitry Andric SortingObjects[Obj].ObjectIndex = Obj; 3068*0b57cec5SDimitry Andric SortingObjects[Obj].ObjectAlignment = MFI.getObjectAlignment(Obj); 3069*0b57cec5SDimitry Andric // Set the size. 3070*0b57cec5SDimitry Andric int ObjectSize = MFI.getObjectSize(Obj); 3071*0b57cec5SDimitry Andric if (ObjectSize == 0) 3072*0b57cec5SDimitry Andric // Variable size. Just use 4. 3073*0b57cec5SDimitry Andric SortingObjects[Obj].ObjectSize = 4; 3074*0b57cec5SDimitry Andric else 3075*0b57cec5SDimitry Andric SortingObjects[Obj].ObjectSize = ObjectSize; 3076*0b57cec5SDimitry Andric } 3077*0b57cec5SDimitry Andric 3078*0b57cec5SDimitry Andric // Count the number of uses for each object. 3079*0b57cec5SDimitry Andric for (auto &MBB : MF) { 3080*0b57cec5SDimitry Andric for (auto &MI : MBB) { 3081*0b57cec5SDimitry Andric if (MI.isDebugInstr()) 3082*0b57cec5SDimitry Andric continue; 3083*0b57cec5SDimitry Andric for (const MachineOperand &MO : MI.operands()) { 3084*0b57cec5SDimitry Andric // Check to see if it's a local stack symbol. 3085*0b57cec5SDimitry Andric if (!MO.isFI()) 3086*0b57cec5SDimitry Andric continue; 3087*0b57cec5SDimitry Andric int Index = MO.getIndex(); 3088*0b57cec5SDimitry Andric // Check to see if it falls within our range, and is tagged 3089*0b57cec5SDimitry Andric // to require ordering. 3090*0b57cec5SDimitry Andric if (Index >= 0 && Index < MFI.getObjectIndexEnd() && 3091*0b57cec5SDimitry Andric SortingObjects[Index].IsValid) 3092*0b57cec5SDimitry Andric SortingObjects[Index].ObjectNumUses++; 3093*0b57cec5SDimitry Andric } 3094*0b57cec5SDimitry Andric } 3095*0b57cec5SDimitry Andric } 3096*0b57cec5SDimitry Andric 3097*0b57cec5SDimitry Andric // Sort the objects using X86FrameSortingAlgorithm (see its comment for 3098*0b57cec5SDimitry Andric // info). 3099*0b57cec5SDimitry Andric llvm::stable_sort(SortingObjects, X86FrameSortingComparator()); 3100*0b57cec5SDimitry Andric 3101*0b57cec5SDimitry Andric // Now modify the original list to represent the final order that 3102*0b57cec5SDimitry Andric // we want. The order will depend on whether we're going to access them 3103*0b57cec5SDimitry Andric // from the stack pointer or the frame pointer. For SP, the list should 3104*0b57cec5SDimitry Andric // end up with the END containing objects that we want with smaller offsets. 3105*0b57cec5SDimitry Andric // For FP, it should be flipped. 3106*0b57cec5SDimitry Andric int i = 0; 3107*0b57cec5SDimitry Andric for (auto &Obj : SortingObjects) { 3108*0b57cec5SDimitry Andric // All invalid items are sorted at the end, so it's safe to stop. 3109*0b57cec5SDimitry Andric if (!Obj.IsValid) 3110*0b57cec5SDimitry Andric break; 3111*0b57cec5SDimitry Andric ObjectsToAllocate[i++] = Obj.ObjectIndex; 3112*0b57cec5SDimitry Andric } 3113*0b57cec5SDimitry Andric 3114*0b57cec5SDimitry Andric // Flip it if we're accessing off of the FP. 3115*0b57cec5SDimitry Andric if (!TRI->needsStackRealignment(MF) && hasFP(MF)) 3116*0b57cec5SDimitry Andric std::reverse(ObjectsToAllocate.begin(), ObjectsToAllocate.end()); 3117*0b57cec5SDimitry Andric } 3118*0b57cec5SDimitry Andric 3119*0b57cec5SDimitry Andric 3120*0b57cec5SDimitry Andric unsigned X86FrameLowering::getWinEHParentFrameOffset(const MachineFunction &MF) const { 3121*0b57cec5SDimitry Andric // RDX, the parent frame pointer, is homed into 16(%rsp) in the prologue. 3122*0b57cec5SDimitry Andric unsigned Offset = 16; 3123*0b57cec5SDimitry Andric // RBP is immediately pushed. 3124*0b57cec5SDimitry Andric Offset += SlotSize; 3125*0b57cec5SDimitry Andric // All callee-saved registers are then pushed. 3126*0b57cec5SDimitry Andric Offset += MF.getInfo<X86MachineFunctionInfo>()->getCalleeSavedFrameSize(); 3127*0b57cec5SDimitry Andric // Every funclet allocates enough stack space for the largest outgoing call. 3128*0b57cec5SDimitry Andric Offset += getWinEHFuncletFrameSize(MF); 3129*0b57cec5SDimitry Andric return Offset; 3130*0b57cec5SDimitry Andric } 3131*0b57cec5SDimitry Andric 3132*0b57cec5SDimitry Andric void X86FrameLowering::processFunctionBeforeFrameFinalized( 3133*0b57cec5SDimitry Andric MachineFunction &MF, RegScavenger *RS) const { 3134*0b57cec5SDimitry Andric // Mark the function as not having WinCFI. We will set it back to true in 3135*0b57cec5SDimitry Andric // emitPrologue if it gets called and emits CFI. 3136*0b57cec5SDimitry Andric MF.setHasWinCFI(false); 3137*0b57cec5SDimitry Andric 3138*0b57cec5SDimitry Andric // If this function isn't doing Win64-style C++ EH, we don't need to do 3139*0b57cec5SDimitry Andric // anything. 3140*0b57cec5SDimitry Andric const Function &F = MF.getFunction(); 3141*0b57cec5SDimitry Andric if (!STI.is64Bit() || !MF.hasEHFunclets() || 3142*0b57cec5SDimitry Andric classifyEHPersonality(F.getPersonalityFn()) != EHPersonality::MSVC_CXX) 3143*0b57cec5SDimitry Andric return; 3144*0b57cec5SDimitry Andric 3145*0b57cec5SDimitry Andric // Win64 C++ EH needs to allocate the UnwindHelp object at some fixed offset 3146*0b57cec5SDimitry Andric // relative to RSP after the prologue. Find the offset of the last fixed 3147*0b57cec5SDimitry Andric // object, so that we can allocate a slot immediately following it. If there 3148*0b57cec5SDimitry Andric // were no fixed objects, use offset -SlotSize, which is immediately after the 3149*0b57cec5SDimitry Andric // return address. Fixed objects have negative frame indices. 3150*0b57cec5SDimitry Andric MachineFrameInfo &MFI = MF.getFrameInfo(); 3151*0b57cec5SDimitry Andric WinEHFuncInfo &EHInfo = *MF.getWinEHFuncInfo(); 3152*0b57cec5SDimitry Andric int64_t MinFixedObjOffset = -SlotSize; 3153*0b57cec5SDimitry Andric for (int I = MFI.getObjectIndexBegin(); I < 0; ++I) 3154*0b57cec5SDimitry Andric MinFixedObjOffset = std::min(MinFixedObjOffset, MFI.getObjectOffset(I)); 3155*0b57cec5SDimitry Andric 3156*0b57cec5SDimitry Andric for (WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap) { 3157*0b57cec5SDimitry Andric for (WinEHHandlerType &H : TBME.HandlerArray) { 3158*0b57cec5SDimitry Andric int FrameIndex = H.CatchObj.FrameIndex; 3159*0b57cec5SDimitry Andric if (FrameIndex != INT_MAX) { 3160*0b57cec5SDimitry Andric // Ensure alignment. 3161*0b57cec5SDimitry Andric unsigned Align = MFI.getObjectAlignment(FrameIndex); 3162*0b57cec5SDimitry Andric MinFixedObjOffset -= std::abs(MinFixedObjOffset) % Align; 3163*0b57cec5SDimitry Andric MinFixedObjOffset -= MFI.getObjectSize(FrameIndex); 3164*0b57cec5SDimitry Andric MFI.setObjectOffset(FrameIndex, MinFixedObjOffset); 3165*0b57cec5SDimitry Andric } 3166*0b57cec5SDimitry Andric } 3167*0b57cec5SDimitry Andric } 3168*0b57cec5SDimitry Andric 3169*0b57cec5SDimitry Andric // Ensure alignment. 3170*0b57cec5SDimitry Andric MinFixedObjOffset -= std::abs(MinFixedObjOffset) % 8; 3171*0b57cec5SDimitry Andric int64_t UnwindHelpOffset = MinFixedObjOffset - SlotSize; 3172*0b57cec5SDimitry Andric int UnwindHelpFI = 3173*0b57cec5SDimitry Andric MFI.CreateFixedObject(SlotSize, UnwindHelpOffset, /*IsImmutable=*/false); 3174*0b57cec5SDimitry Andric EHInfo.UnwindHelpFrameIdx = UnwindHelpFI; 3175*0b57cec5SDimitry Andric 3176*0b57cec5SDimitry Andric // Store -2 into UnwindHelp on function entry. We have to scan forwards past 3177*0b57cec5SDimitry Andric // other frame setup instructions. 3178*0b57cec5SDimitry Andric MachineBasicBlock &MBB = MF.front(); 3179*0b57cec5SDimitry Andric auto MBBI = MBB.begin(); 3180*0b57cec5SDimitry Andric while (MBBI != MBB.end() && MBBI->getFlag(MachineInstr::FrameSetup)) 3181*0b57cec5SDimitry Andric ++MBBI; 3182*0b57cec5SDimitry Andric 3183*0b57cec5SDimitry Andric DebugLoc DL = MBB.findDebugLoc(MBBI); 3184*0b57cec5SDimitry Andric addFrameReference(BuildMI(MBB, MBBI, DL, TII.get(X86::MOV64mi32)), 3185*0b57cec5SDimitry Andric UnwindHelpFI) 3186*0b57cec5SDimitry Andric .addImm(-2); 3187*0b57cec5SDimitry Andric } 3188