diff options
Diffstat (limited to 'llvm/lib/Target/X86')
-rw-r--r-- | llvm/lib/Target/X86/X86CallFrameOptimization.cpp | 18 | ||||
-rw-r--r-- | llvm/lib/Target/X86/X86FrameLowering.cpp | 119 | ||||
-rw-r--r-- | llvm/lib/Target/X86/X86FrameLowering.h | 2 |
3 files changed, 131 insertions, 8 deletions
diff --git a/llvm/lib/Target/X86/X86CallFrameOptimization.cpp b/llvm/lib/Target/X86/X86CallFrameOptimization.cpp index 765af67de16..a7d157b0dad 100644 --- a/llvm/lib/Target/X86/X86CallFrameOptimization.cpp +++ b/llvm/lib/Target/X86/X86CallFrameOptimization.cpp @@ -234,6 +234,12 @@ bool X86CallFrameOptimization::runOnMachineFunction(MachineFunction &MF) { assert(isPowerOf2_32(SlotSize) && "Expect power of 2 stack slot size"); Log2SlotSize = Log2_32(SlotSize); + // Set initial incoming and outgoing cfa offset and register values for basic + // blocks. This is done here because this pass runs before PEI and can insert + // CFI instructions. + // TODO: Find a better solution to this problem. + TFL->initializeCFIInfo(MF); + if (skipFunction(*MF.getFunction()) || !isLegal(MF)) return false; @@ -536,11 +542,13 @@ void X86CallFrameOptimization::adjustCallSequence(MachineFunction &MF, // For debugging, when using SP-based CFA, we need to adjust the CFA // offset after each push. // TODO: This is needed only if we require precise CFA. - if (!TFL->hasFP(MF)) - TFL->BuildCFI( - MBB, std::next(Push), DL, - MCCFIInstruction::createAdjustCfaOffset(nullptr, SlotSize)); - + if (!TFL->hasFP(MF)) { + TFL->BuildCFI(MBB, std::next(Push), DL, + MCCFIInstruction::createAdjustCfaOffset(nullptr, SlotSize)); + // Update the CFI information for MBB and it's successors. + MBB.updateCFIInfo(std::next(Push)); + MBB.updateCFIInfoSucc(); + } MBB.erase(MOV); } diff --git a/llvm/lib/Target/X86/X86FrameLowering.cpp b/llvm/lib/Target/X86/X86FrameLowering.cpp index e3aa227702b..feac493aa90 100644 --- a/llvm/lib/Target/X86/X86FrameLowering.cpp +++ b/llvm/lib/Target/X86/X86FrameLowering.cpp @@ -958,7 +958,8 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF, ? getX86SubSuperRegister(FramePtr, 64) : FramePtr; unsigned BasePtr = TRI->getBaseRegister(); bool HasWinCFI = false; - + bool InsertedCFI = false; + // Debug location must be unknown since the first debug location is used // to determine the end of the prologue. DebugLoc DL; @@ -1093,6 +1094,9 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF, assert(StackSize); BuildCFI(MBB, MBBI, DL, MCCFIInstruction::createDefCfaOffset(nullptr, 2 * stackGrowth)); + MBB.setDefOffset(true); + MBB.updateCFIInfo(std::prev(MBBI)); + InsertedCFI = true; // Change the rule for the FramePtr to be an "offset" rule. unsigned DwarfFramePtr = TRI->getDwarfRegNum(MachineFramePtr, true); @@ -1121,6 +1125,9 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF, unsigned DwarfFramePtr = TRI->getDwarfRegNum(MachineFramePtr, true); BuildCFI(MBB, MBBI, DL, MCCFIInstruction::createDefCfaRegister( nullptr, DwarfFramePtr)); + MBB.setDefRegister(true); + MBB.updateCFIInfo(std::prev(MBBI)); + InsertedCFI = true; } } } else { @@ -1152,6 +1159,9 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF, assert(StackSize); BuildCFI(MBB, MBBI, DL, MCCFIInstruction::createDefCfaOffset(nullptr, StackOffset)); + MBB.setDefOffset(true); + MBB.updateCFIInfo(std::prev(MBBI)); + InsertedCFI = true; StackOffset += stackGrowth; } @@ -1417,6 +1427,9 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF, assert(StackSize); BuildCFI(MBB, MBBI, DL, MCCFIInstruction::createDefCfaOffset( nullptr, -StackSize + stackGrowth)); + MBB.setDefOffset(true); + MBB.updateCFIInfo(std::prev(MBBI)); + InsertedCFI = true; } // Emit DWARF info specifying the offsets of the callee-saved registers. @@ -1438,6 +1451,9 @@ void X86FrameLowering::emitPrologue(MachineFunction &MF, // At this point we know if the function has WinCFI or not. MF.setHasWinCFI(HasWinCFI); + + if (InsertedCFI) + MBB.updateCFIInfoSucc(); } bool X86FrameLowering::canUseLEAForSPInEpilogue( @@ -1548,6 +1564,12 @@ void X86FrameLowering::emitEpilogue(MachineFunction &MF, unsigned CSSize = X86FI->getCalleeSavedFrameSize(); uint64_t NumBytes = 0; + bool NeedsDwarfCFI = (MF.getMMI().hasDebugInfo() || + MF.getFunction()->needsUnwindTableEntry()) && + (!MF.getSubtarget<X86Subtarget>().isTargetDarwin() && + !MF.getSubtarget<X86Subtarget>().isOSWindows()); + bool InsertedCFI = false; + if (RetOpcode && *RetOpcode == X86::CATCHRET) { // SEH shouldn't use catchret. assert(!isAsynchronousEHPersonality( @@ -1582,6 +1604,17 @@ void X86FrameLowering::emitEpilogue(MachineFunction &MF, BuildMI(MBB, MBBI, DL, TII.get(Is64Bit ? X86::POP64r : X86::POP32r), MachineFramePtr) .setMIFlag(MachineInstr::FrameDestroy); + if (NeedsDwarfCFI) { + unsigned DwarfStackPtr = + TRI->getDwarfRegNum(Is64Bit ? X86::RSP : X86::ESP, true); + BuildCFI(MBB, MBBI, DL, MCCFIInstruction::createDefCfa( + nullptr, DwarfStackPtr, -SlotSize)); + --MBBI; + MBB.setDefOffset(true); + MBB.setDefRegister(true); + MBB.updateCFIInfo(MBBI); + InsertedCFI = true; + } } else { NumBytes = StackSize - CSSize; } @@ -1666,6 +1699,14 @@ void X86FrameLowering::emitEpilogue(MachineFunction &MF, } else if (NumBytes) { // Adjust stack pointer back: ESP += numbytes. emitSPUpdate(MBB, MBBI, NumBytes, /*InEpilogue=*/true); + if (!hasFP(MF) && NeedsDwarfCFI) { + // Define the current CFA rule to use the provided offset. + BuildCFI(MBB, MBBI, DL, MCCFIInstruction::createDefCfaOffset( + nullptr, -CSSize - SlotSize)); + MBB.setDefOffset(true); + MBB.updateCFIInfo(std::prev(MBBI)); + InsertedCFI = true; + } --MBBI; } @@ -1678,6 +1719,26 @@ void X86FrameLowering::emitEpilogue(MachineFunction &MF, if (NeedsWinCFI && MF.hasWinCFI()) BuildMI(MBB, MBBI, DL, TII.get(X86::SEH_Epilogue)); + if (!hasFP(MF) && NeedsDwarfCFI) { + MBBI = FirstCSPop; + int64_t Offset = -CSSize - SlotSize; + // Mark callee-saved pop instruction. + // Define the current CFA rule to use the provided offset. + while (MBBI != MBB.end()) { + MachineBasicBlock::iterator PI = MBBI; + unsigned Opc = PI->getOpcode(); + ++MBBI; + if (Opc == X86::POP32r || Opc == X86::POP64r) { + Offset += SlotSize; + BuildCFI(MBB, MBBI, DL, + MCCFIInstruction::createDefCfaOffset(nullptr, Offset)); + MBB.setDefOffset(true); + MBB.updateCFIInfo(std::prev(MBBI)); + InsertedCFI = true; + } + } + } + if (!RetOpcode || !isTailCallOpcode(*RetOpcode)) { // Add the return addr area delta back since we are not tail calling. int Offset = -1 * X86FI->getTCReturnAddrDelta(); @@ -1690,6 +1751,9 @@ void X86FrameLowering::emitEpilogue(MachineFunction &MF, emitSPUpdate(MBB, MBBI, Offset, /*InEpilogue=*/true); } } + + if (InsertedCFI) + MBB.updateCFIInfoSucc(); } int X86FrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI, @@ -2364,6 +2428,19 @@ void X86FrameLowering::adjustForSegmentedStacks( checkMBB->addSuccessor(allocMBB); checkMBB->addSuccessor(&PrologueMBB); + int InitialOffset = TRI->getSlotSize(); + unsigned InitialRegister = TRI->getDwarfRegNum(StackPtr, true); + // Set CFI info for checkMBB. + checkMBB->setIncomingCFAOffset(InitialOffset); + checkMBB->setIncomingCFARegister(InitialRegister); + checkMBB->setOutgoingCFAOffset(InitialOffset); + checkMBB->setOutgoingCFARegister(InitialRegister); + // Set CFI info for allocMBB. + allocMBB->setIncomingCFAOffset(InitialOffset); + allocMBB->setIncomingCFARegister(InitialRegister); + allocMBB->setOutgoingCFAOffset(InitialOffset); + allocMBB->setOutgoingCFARegister(InitialRegister); + #ifdef EXPENSIVE_CHECKS MF.verify(); #endif @@ -2535,6 +2612,19 @@ void X86FrameLowering::adjustForHiPEPrologue( stackCheckMBB->addSuccessor(incStackMBB, {1, 100}); incStackMBB->addSuccessor(&PrologueMBB, {99, 100}); incStackMBB->addSuccessor(incStackMBB, {1, 100}); + + int InitialOffset = TRI->getSlotSize(); + unsigned InitialRegister = TRI->getDwarfRegNum(StackPtr, true); + // Set CFI info to stackCheckMBB. + stackCheckMBB->setIncomingCFAOffset(InitialOffset); + stackCheckMBB->setIncomingCFARegister(InitialRegister); + stackCheckMBB->setOutgoingCFAOffset(InitialOffset); + stackCheckMBB->setOutgoingCFARegister(InitialRegister); + // Set CFI info to incStackMBB. + incStackMBB->setIncomingCFAOffset(InitialOffset); + incStackMBB->setIncomingCFARegister(InitialRegister); + incStackMBB->setOutgoingCFAOffset(InitialOffset); + incStackMBB->setOutgoingCFARegister(InitialRegister); } #ifdef EXPENSIVE_CHECKS MF.verify(); @@ -2640,6 +2730,7 @@ eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, bool WindowsCFI = MF.getTarget().getMCAsmInfo()->usesWindowsCFI(); bool DwarfCFI = !WindowsCFI && (MMI.hasDebugInfo() || Fn->needsUnwindTableEntry()); + bool InsertedCFI = false; // If we have any exception handlers in this function, and we adjust // the SP before calls, we may need to indicate this to the unwinder @@ -2665,10 +2756,12 @@ eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, // TODO: This is needed only if we require precise CFA. // If this is a callee-pop calling convention, emit a CFA adjust for // the amount the callee popped. - if (isDestroy && InternalAmt && DwarfCFI && !hasFP(MF)) + if (isDestroy && InternalAmt && DwarfCFI && !hasFP(MF)) { BuildCFI(MBB, InsertPos, DL, MCCFIInstruction::createAdjustCfaOffset(nullptr, -InternalAmt)); - + MBB.updateCFIInfo(std::prev(InsertPos)); + InsertedCFI = true; + } // Add Amount to SP to destroy a frame, or subtract to setup. int64_t StackAdjustment = isDestroy ? Amount : -Amount; int64_t CfaAdjustment = -StackAdjustment; @@ -2702,9 +2795,13 @@ eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, BuildCFI(MBB, InsertPos, DL, MCCFIInstruction::createAdjustCfaOffset(nullptr, CfaAdjustment)); + MBB.updateCFIInfo(std::prev(InsertPos)); + InsertedCFI = true; } } + if (InsertedCFI) MBB.updateCFIInfoSucc(); + return I; } @@ -2826,6 +2923,22 @@ MachineBasicBlock::iterator X86FrameLowering::restoreWin32EHStackPointers( return MBBI; } +void X86FrameLowering::initializeCFIInfo(MachineFunction &MF) const { + int InitialOffset = TRI->getSlotSize(); + unsigned InitialRegister = TRI->getDwarfRegNum(StackPtr, true); + // Initialize CFI info if it hasn't already been initialized. + for (auto &MBB : MF) { + if (MBB.getIncomingCFAOffset() == -1) + MBB.setIncomingCFAOffset(InitialOffset); + if (MBB.getOutgoingCFAOffset() == -1) + MBB.setOutgoingCFAOffset(InitialOffset); + if (MBB.getIncomingCFARegister() == 0) + MBB.setIncomingCFARegister(InitialRegister); + if (MBB.getOutgoingCFARegister() == 0) + MBB.setOutgoingCFARegister(InitialRegister); + } +} + namespace { // Struct used by orderFrameObjects to help sort the stack objects. struct X86FrameSortingObject { diff --git a/llvm/lib/Target/X86/X86FrameLowering.h b/llvm/lib/Target/X86/X86FrameLowering.h index 7d214cabad5..60edf842980 100644 --- a/llvm/lib/Target/X86/X86FrameLowering.h +++ b/llvm/lib/Target/X86/X86FrameLowering.h @@ -177,6 +177,8 @@ public: MachineBasicBlock::iterator MBBI, const DebugLoc &DL, bool RestoreSP = false) const; + void initializeCFIInfo(MachineFunction &MF) const override; + private: uint64_t calculateMaxStackAlign(const MachineFunction &MF) const; |