From d9245ca1a1aa96371a2513c91a3e2a1f26b16e22 Mon Sep 17 00:00:00 2001 From: Evan Cheng Date: Fri, 14 Apr 2006 07:26:43 +0000 Subject: [PATCH] We were not adjusting the frame size to ensure proper alignment when alloca / vla are present in the function. This causes a crash when a leaf function allocates space on the stack used to store / load with 128-bit SSE instructions. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@27698 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/X86/X86RegisterInfo.cpp | 53 +++++++++++++----------------- 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/lib/Target/X86/X86RegisterInfo.cpp b/lib/Target/X86/X86RegisterInfo.cpp index f9c095da3ea..63770357b80 100644 --- a/lib/Target/X86/X86RegisterInfo.cpp +++ b/lib/Target/X86/X86RegisterInfo.cpp @@ -573,17 +573,34 @@ void X86RegisterInfo::emitPrologue(MachineFunction &MF) const { // Get the number of bytes to allocate from the FrameInfo unsigned NumBytes = MFI->getStackSize(); + if (MFI->hasCalls() || MF.getFrameInfo()->hasVarSizedObjects()) { + // When we have no frame pointer, we reserve argument space for call sites + // in the function immediately on entry to the current function. This + // eliminates the need for add/sub ESP brackets around call sites. + // + if (!hasFP(MF)) + NumBytes += MFI->getMaxCallFrameSize(); + + // Round the size to a multiple of the alignment (don't forget the 4 byte + // offset though). + unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment(); + NumBytes = ((NumBytes+4)+Align-1)/Align*Align - 4; + } + + // Update frame info to pretend that this is part of the stack... + MFI->setStackSize(NumBytes); + + if (NumBytes) { // adjust stack pointer: ESP -= numbytes + unsigned Opc = NumBytes < 128 ? X86::SUB32ri8 : X86::SUB32ri; + MI = BuildMI(Opc, 1, X86::ESP,MachineOperand::UseAndDef).addImm(NumBytes); + MBB.insert(MBBI, MI); + } + if (hasFP(MF)) { // Get the offset of the stack slot for the EBP register... which is // guaranteed to be the last slot by processFunctionBeforeFrameFinalized. int EBPOffset = MFI->getObjectOffset(MFI->getObjectIndexBegin())+4; - if (NumBytes) { // adjust stack pointer: ESP -= numbytes - unsigned Opc = NumBytes < 128 ? X86::SUB32ri8 : X86::SUB32ri; - MI = BuildMI(Opc, 1, X86::ESP,MachineOperand::UseAndDef).addImm(NumBytes); - MBB.insert(MBBI, MI); - } - // Save EBP into the appropriate stack slot... MI = addRegOffset(BuildMI(X86::MOV32mr, 5), // mov [ESP-], EBP X86::ESP, EBPOffset+NumBytes).addReg(X86::EBP); @@ -596,30 +613,6 @@ void X86RegisterInfo::emitPrologue(MachineFunction &MF) const { MI = addRegOffset(BuildMI(X86::LEA32r, 5, X86::EBP), X86::ESP,NumBytes-4); MBB.insert(MBBI, MI); - - } else { - if (MFI->hasCalls()) { - // When we have no frame pointer, we reserve argument space for call sites - // in the function immediately on entry to the current function. This - // eliminates the need for add/sub ESP brackets around call sites. - // - NumBytes += MFI->getMaxCallFrameSize(); - - // Round the size to a multiple of the alignment (don't forget the 4 byte - // offset though). - unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment(); - NumBytes = ((NumBytes+4)+Align-1)/Align*Align - 4; - } - - // Update frame info to pretend that this is part of the stack... - MFI->setStackSize(NumBytes); - - if (NumBytes) { - // adjust stack pointer: ESP -= numbytes - unsigned Opc = NumBytes < 128 ? X86::SUB32ri8 : X86::SUB32ri; - MI= BuildMI(Opc, 1, X86::ESP, MachineOperand::UseAndDef).addImm(NumBytes); - MBB.insert(MBBI, MI); - } } } -- 2.34.1